]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store.cc
Merge SBufList
[thirdparty/squid.git] / src / store.cc
1
2 /*
3 * DEBUG: section 20 Storage Manager
4 * AUTHOR: Harvest Derived
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 */
33
34 #include "squid.h"
35 #include "CacheDigest.h"
36 #include "CacheManager.h"
37 #include "comm/Connection.h"
38 #include "ETag.h"
39 #include "event.h"
40 #include "fde.h"
41 #include "globals.h"
42 #include "http.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
45 #include "mem_node.h"
46 #include "MemObject.h"
47 #include "mgr/Registration.h"
48 #include "mgr/StoreIoAction.h"
49 #include "profiler/Profiler.h"
50 #include "repl_modules.h"
51 #include "RequestFlags.h"
52 #include "SquidConfig.h"
53 #include "SquidTime.h"
54 #include "Stack.h"
55 #include "StatCounters.h"
56 #include "stmem.h"
57 #include "Store.h"
58 #include "store_digest.h"
59 #include "store_key_md5.h"
60 #include "store_key_md5.h"
61 #include "store_log.h"
62 #include "store_rebuild.h"
63 #include "StoreClient.h"
64 #include "StoreIOState.h"
65 #include "StoreMeta.h"
66 #include "StrList.h"
67 #include "swap_log_op.h"
68 #include "SwapDir.h"
69 #include "tools.h"
70 #if USE_DELAY_POOLS
71 #include "DelayPools.h"
72 #endif
73 #if HAVE_LIMITS_H
74 #include <limits.h>
75 #endif
76
77 static STMCB storeWriteComplete;
78
79 #define REBUILD_TIMESTAMP_DELTA_MAX 2
80
81 #define STORE_IN_MEM_BUCKETS (229)
82
83 /** \todo Convert these string constants to enum string-arrays generated */
84
85 const char *memStatusStr[] = {
86 "NOT_IN_MEMORY",
87 "IN_MEMORY"
88 };
89
90 const char *pingStatusStr[] = {
91 "PING_NONE",
92 "PING_WAITING",
93 "PING_DONE"
94 };
95
96 const char *storeStatusStr[] = {
97 "STORE_OK",
98 "STORE_PENDING"
99 };
100
101 const char *swapStatusStr[] = {
102 "SWAPOUT_NONE",
103 "SWAPOUT_WRITING",
104 "SWAPOUT_DONE"
105 };
106
107 /*
108 * This defines an repl type
109 */
110
111 typedef struct _storerepl_entry storerepl_entry_t;
112
113 struct _storerepl_entry {
114 const char *typestr;
115 REMOVALPOLICYCREATE *create;
116 };
117
118 static storerepl_entry_t *storerepl_list = NULL;
119
120 /*
121 * local function prototypes
122 */
123 static int getKeyCounter(void);
124 static OBJH storeCheckCachableStats;
125 static EVH storeLateRelease;
126
127 /*
128 * local variables
129 */
130 static Stack<StoreEntry*> LateReleaseStack;
131 MemAllocator *StoreEntry::pool = NULL;
132
133 StorePointer Store::CurrentRoot = NULL;
134
135 void
136 Store::Root(Store * aRoot)
137 {
138 CurrentRoot = aRoot;
139 }
140
141 void
142 Store::Root(StorePointer aRoot)
143 {
144 Root(aRoot.getRaw());
145 }
146
147 void
148 Store::Stats(StoreEntry * output)
149 {
150 assert (output);
151 Root().stat(*output);
152 }
153
154 void
155 Store::create()
156 {}
157
158 void
159 Store::diskFull()
160 {}
161
162 void
163 Store::sync()
164 {}
165
166 void
167 Store::unlink (StoreEntry &anEntry)
168 {
169 fatal("Store::unlink on invalid Store\n");
170 }
171
172 void *
173 StoreEntry::operator new (size_t bytecount)
174 {
175 assert (bytecount == sizeof (StoreEntry));
176
177 if (!pool) {
178 pool = memPoolCreate ("StoreEntry", bytecount);
179 pool->setChunkSize(2048 * 1024);
180 }
181
182 return pool->alloc();
183 }
184
185 void
186 StoreEntry::operator delete (void *address)
187 {
188 pool->freeOne(address);
189 }
190
191 void
192 StoreEntry::makePublic()
193 {
194 /* This object can be cached for a long time */
195
196 if (EBIT_TEST(flags, ENTRY_CACHABLE))
197 setPublicKey();
198 }
199
200 void
201 StoreEntry::makePrivate()
202 {
203 /* This object should never be cached at all */
204 expireNow();
205 releaseRequest(); /* delete object when not used */
206 /* releaseRequest clears ENTRY_CACHABLE flag */
207 }
208
209 void
210 StoreEntry::cacheNegatively()
211 {
212 /* This object may be negatively cached */
213 negativeCache();
214
215 if (EBIT_TEST(flags, ENTRY_CACHABLE))
216 setPublicKey();
217 }
218
219 size_t
220 StoreEntry::inUseCount()
221 {
222 if (!pool)
223 return 0;
224 return pool->getInUseCount();
225 }
226
227 const char *
228 StoreEntry::getMD5Text() const
229 {
230 return storeKeyText((const cache_key *)key);
231 }
232
233 #include "comm.h"
234
235 void
236 StoreEntry::DeferReader(void *theContext, CommRead const &aRead)
237 {
238 StoreEntry *anEntry = (StoreEntry *)theContext;
239 anEntry->delayAwareRead(aRead.conn,
240 aRead.buf,
241 aRead.len,
242 aRead.callback);
243 }
244
245 void
246 StoreEntry::delayAwareRead(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer callback)
247 {
248 size_t amountToRead = bytesWanted(Range<size_t>(0, len));
249 /* sketch: readdeferer* = getdeferer.
250 * ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
251 */
252
253 if (amountToRead == 0) {
254 assert (mem_obj);
255 /* read ahead limit */
256 /* Perhaps these two calls should both live in MemObject */
257 #if USE_DELAY_POOLS
258 if (!mem_obj->readAheadPolicyCanRead()) {
259 #endif
260 mem_obj->delayRead(DeferredRead(DeferReader, this, CommRead(conn, buf, len, callback)));
261 return;
262 #if USE_DELAY_POOLS
263 }
264
265 /* delay id limit */
266 mem_obj->mostBytesAllowed().delayRead(DeferredRead(DeferReader, this, CommRead(conn, buf, len, callback)));
267 return;
268
269 #endif
270
271 }
272
273 if (fd_table[conn->fd].closing()) {
274 // Readers must have closing callbacks if they want to be notified. No
275 // readers appeared to care around 2009/12/14 as they skipped reading
276 // for other reasons. Closing may already be true at the delyaAwareRead
277 // call time or may happen while we wait after delayRead() above.
278 debugs(20, 3, HERE << "wont read from closing " << conn << " for " <<
279 callback);
280 return; // the read callback will never be called
281 }
282
283 comm_read(conn, buf, amountToRead, callback);
284 }
285
286 size_t
287 StoreEntry::bytesWanted (Range<size_t> const aRange, bool ignoreDelayPools) const
288 {
289 if (mem_obj == NULL)
290 return aRange.end;
291
292 #if URL_CHECKSUM_DEBUG
293
294 mem_obj->checkUrlChecksum();
295
296 #endif
297
298 if (!mem_obj->readAheadPolicyCanRead())
299 return 0;
300
301 return mem_obj->mostBytesWanted(aRange.end, ignoreDelayPools);
302 }
303
304 bool
305 StoreEntry::checkDeferRead(int fd) const
306 {
307 return (bytesWanted(Range<size_t>(0,INT_MAX)) == 0);
308 }
309
310 void
311 StoreEntry::setNoDelay (bool const newValue)
312 {
313 if (mem_obj)
314 mem_obj->setNoDelay(newValue);
315 }
316
317 store_client_t
318 StoreEntry::storeClientType() const
319 {
320 /* The needed offset isn't in memory
321 * XXX TODO: this is wrong for range requests
322 * as the needed offset may *not* be 0, AND
323 * offset 0 in the memory object is the HTTP headers.
324 */
325
326 if (mem_status == IN_MEMORY && Config.memShared && IamWorkerProcess()) {
327 // clients of an object cached in shared memory are memory clients
328 return STORE_MEM_CLIENT;
329 }
330
331 assert(mem_obj);
332
333 if (mem_obj->inmem_lo)
334 return STORE_DISK_CLIENT;
335
336 if (EBIT_TEST(flags, ENTRY_ABORTED)) {
337 /* I don't think we should be adding clients to aborted entries */
338 debugs(20, DBG_IMPORTANT, "storeClientType: adding to ENTRY_ABORTED entry");
339 return STORE_MEM_CLIENT;
340 }
341
342 if (store_status == STORE_OK) {
343 /* the object has completed. */
344
345 if (mem_obj->inmem_lo == 0 && !isEmpty()) {
346 if (swap_status == SWAPOUT_DONE) {
347 debugs(20,7, HERE << mem_obj << " lo: " << mem_obj->inmem_lo << " hi: " << mem_obj->endOffset() << " size: " << mem_obj->object_sz);
348 if (mem_obj->endOffset() == mem_obj->object_sz) {
349 /* hot object fully swapped in */
350 return STORE_MEM_CLIENT;
351 }
352 } else {
353 /* Memory-only, or currently being swapped out */
354 return STORE_MEM_CLIENT;
355 }
356 }
357 return STORE_DISK_CLIENT;
358 }
359
360 /* here and past, entry is STORE_PENDING */
361 /*
362 * If this is the first client, let it be the mem client
363 */
364 if (mem_obj->nclients == 1)
365 return STORE_MEM_CLIENT;
366
367 /*
368 * If there is no disk file to open yet, we must make this a
369 * mem client. If we can't open the swapin file before writing
370 * to the client, there is no guarantee that we will be able
371 * to open it later when we really need it.
372 */
373 if (swap_status == SWAPOUT_NONE)
374 return STORE_MEM_CLIENT;
375
376 /*
377 * otherwise, make subsequent clients read from disk so they
378 * can not delay the first, and vice-versa.
379 */
380 return STORE_DISK_CLIENT;
381 }
382
383 StoreEntry::StoreEntry() :
384 mem_obj(NULL),
385 hidden_mem_obj(NULL),
386 timestamp(-1),
387 lastref(-1),
388 expires(-1),
389 lastmod(-1),
390 swap_file_sz(0),
391 refcount(0),
392 flags(0),
393 swap_filen(-1),
394 swap_dirn(-1),
395 lock_count(0),
396 mem_status(NOT_IN_MEMORY),
397 ping_status(PING_NONE),
398 store_status(STORE_PENDING),
399 swap_status(SWAPOUT_NONE)
400 {
401 debugs(20, 3, HERE << "new StoreEntry " << this);
402 }
403
404 StoreEntry::StoreEntry(const char *aUrl, const char *aLogUrl) :
405 mem_obj(NULL),
406 hidden_mem_obj(NULL),
407 timestamp(-1),
408 lastref(-1),
409 expires(-1),
410 lastmod(-1),
411 swap_file_sz(0),
412 refcount(0),
413 flags(0),
414 swap_filen(-1),
415 swap_dirn(-1),
416 lock_count(0),
417 mem_status(NOT_IN_MEMORY),
418 ping_status(PING_NONE),
419 store_status(STORE_PENDING),
420 swap_status(SWAPOUT_NONE)
421 {
422 debugs(20, 3, HERE << "new StoreEntry " << this);
423 mem_obj = new MemObject(aUrl, aLogUrl);
424 }
425
426 StoreEntry::~StoreEntry()
427 {
428 if (swap_filen >= 0) {
429 SwapDir &sd = dynamic_cast<SwapDir&>(*store());
430 sd.disconnect(*this);
431 }
432 delete hidden_mem_obj;
433 }
434
435 #if USE_ADAPTATION
436 void
437 StoreEntry::deferProducer(const AsyncCall::Pointer &producer)
438 {
439 if (!deferredProducer)
440 deferredProducer = producer;
441 else
442 debugs(20, 5, HERE << "Deferred producer call is allready set to: " <<
443 *deferredProducer << ", requested call: " << *producer);
444 }
445
446 void
447 StoreEntry::kickProducer()
448 {
449 if (deferredProducer != NULL) {
450 ScheduleCallHere(deferredProducer);
451 deferredProducer = NULL;
452 }
453 }
454 #endif
455
456 void
457 StoreEntry::destroyMemObject()
458 {
459 debugs(20, 3, HERE << "destroyMemObject " << mem_obj);
460 setMemStatus(NOT_IN_MEMORY);
461 MemObject *mem = mem_obj;
462 mem_obj = NULL;
463 delete mem;
464 delete hidden_mem_obj;
465 hidden_mem_obj = NULL;
466 }
467
468 void
469 StoreEntry::hideMemObject()
470 {
471 debugs(20, 3, HERE << "hiding " << mem_obj);
472 assert(mem_obj);
473 assert(!hidden_mem_obj);
474 hidden_mem_obj = mem_obj;
475 mem_obj = NULL;
476 }
477
478 void
479 destroyStoreEntry(void *data)
480 {
481 debugs(20, 3, HERE << "destroyStoreEntry: destroying " << data);
482 StoreEntry *e = static_cast<StoreEntry *>(static_cast<hash_link *>(data));
483 assert(e != NULL);
484
485 if (e == NullStoreEntry::getInstance())
486 return;
487
488 e->destroyMemObject();
489
490 e->hashDelete();
491
492 assert(e->key == NULL);
493
494 delete e;
495 }
496
497 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
498
499 void
500 StoreEntry::hashInsert(const cache_key * someKey)
501 {
502 debugs(20, 3, "StoreEntry::hashInsert: Inserting Entry " << this << " key '" << storeKeyText(someKey) << "'");
503 key = storeKeyDup(someKey);
504 hash_join(store_table, this);
505 }
506
507 void
508 StoreEntry::hashDelete()
509 {
510 hash_remove_link(store_table, this);
511 storeKeyFree((const cache_key *)key);
512 key = NULL;
513 }
514
515 /* -------------------------------------------------------------------------- */
516
517 /* get rid of memory copy of the object */
518 void
519 StoreEntry::purgeMem()
520 {
521 if (mem_obj == NULL)
522 return;
523
524 debugs(20, 3, "StoreEntry::purgeMem: Freeing memory-copy of " << getMD5Text());
525
526 destroyMemObject();
527
528 if (swap_status != SWAPOUT_DONE)
529 release();
530 }
531
532 /* RBC 20050104 this is wrong- memory ref counting
533 * is not at all equivalent to the store 'usage' concept
534 * which the replacement policies should be acting upon.
535 * specifically, object iteration within stores needs
536 * memory ref counting to prevent race conditions,
537 * but this should not influence store replacement.
538 */
539 void
540
541 StoreEntry::lock()
542 {
543 ++lock_count;
544 debugs(20, 3, "StoreEntry::lock: key '" << getMD5Text() <<"' count=" <<
545 lock_count );
546 lastref = squid_curtime;
547 Store::Root().reference(*this);
548 }
549
550 void
551 StoreEntry::setReleaseFlag()
552 {
553 if (EBIT_TEST(flags, RELEASE_REQUEST))
554 return;
555
556 debugs(20, 3, "StoreEntry::setReleaseFlag: '" << getMD5Text() << "'");
557
558 EBIT_SET(flags, RELEASE_REQUEST);
559 }
560
561 void
562 StoreEntry::releaseRequest()
563 {
564 if (EBIT_TEST(flags, RELEASE_REQUEST))
565 return;
566
567 setReleaseFlag();
568
569 /*
570 * Clear cachable flag here because we might get called before
571 * anyone else even looks at the cachability flag. Also, this
572 * prevents httpMakePublic from really setting a public key.
573 */
574 EBIT_CLR(flags, ENTRY_CACHABLE);
575
576 setPrivateKey();
577 }
578
579 /* unlock object, return -1 if object get released after unlock
580 * otherwise lock_count */
581 int
582 StoreEntry::unlock()
583 {
584 --lock_count;
585 debugs(20, 3, "StoreEntry::unlock: key '" << getMD5Text() << "' count=" << lock_count);
586
587 if (lock_count)
588 return (int) lock_count;
589
590 if (store_status == STORE_PENDING)
591 setReleaseFlag();
592
593 assert(storePendingNClients(this) == 0);
594
595 if (EBIT_TEST(flags, RELEASE_REQUEST)) {
596 this->release();
597 return 0;
598 }
599
600 if (EBIT_TEST(flags, KEY_PRIVATE))
601 debugs(20, DBG_IMPORTANT, "WARNING: " << __FILE__ << ":" << __LINE__ << ": found KEY_PRIVATE");
602
603 Store::Root().handleIdleEntry(*this); // may delete us
604 return 0;
605 }
606
607 void
608 StoreEntry::getPublicByRequestMethod (StoreClient *aClient, HttpRequest * request, const HttpRequestMethod& method)
609 {
610 assert (aClient);
611 StoreEntry *result = storeGetPublicByRequestMethod( request, method);
612
613 if (!result)
614 aClient->created (NullStoreEntry::getInstance());
615 else
616 aClient->created (result);
617 }
618
619 void
620 StoreEntry::getPublicByRequest (StoreClient *aClient, HttpRequest * request)
621 {
622 assert (aClient);
623 StoreEntry *result = storeGetPublicByRequest (request);
624
625 if (!result)
626 result = NullStoreEntry::getInstance();
627
628 aClient->created (result);
629 }
630
631 void
632 StoreEntry::getPublic (StoreClient *aClient, const char *uri, const HttpRequestMethod& method)
633 {
634 assert (aClient);
635 StoreEntry *result = storeGetPublic (uri, method);
636
637 if (!result)
638 result = NullStoreEntry::getInstance();
639
640 aClient->created (result);
641 }
642
643 StoreEntry *
644 storeGetPublic(const char *uri, const HttpRequestMethod& method)
645 {
646 return Store::Root().get(storeKeyPublic(uri, method));
647 }
648
649 StoreEntry *
650 storeGetPublicByRequestMethod(HttpRequest * req, const HttpRequestMethod& method)
651 {
652 return Store::Root().get(storeKeyPublicByRequestMethod(req, method));
653 }
654
655 StoreEntry *
656 storeGetPublicByRequest(HttpRequest * req)
657 {
658 StoreEntry *e = storeGetPublicByRequestMethod(req, req->method);
659
660 if (e == NULL && req->method == Http::METHOD_HEAD)
661 /* We can generate a HEAD reply from a cached GET object */
662 e = storeGetPublicByRequestMethod(req, Http::METHOD_GET);
663
664 return e;
665 }
666
667 static int
668 getKeyCounter(void)
669 {
670 static int key_counter = 0;
671
672 if (++key_counter < 0)
673 key_counter = 1;
674
675 return key_counter;
676 }
677
678 /* RBC 20050104 AFAICT this should become simpler:
679 * rather than reinserting with a special key it should be marked
680 * as 'released' and then cleaned up when refcounting indicates.
681 * the StoreHashIndex could well implement its 'released' in the
682 * current manner.
683 * Also, clean log writing should skip over ia,t
684 * Otherwise, we need a 'remove from the index but not the store
685 * concept'.
686 */
687 void
688 StoreEntry::setPrivateKey()
689 {
690 const cache_key *newkey;
691
692 if (key && EBIT_TEST(flags, KEY_PRIVATE))
693 return; /* is already private */
694
695 if (key) {
696 if (swap_filen > -1)
697 storeDirSwapLog(this, SWAP_LOG_DEL);
698
699 hashDelete();
700 }
701
702 if (mem_obj != NULL) {
703 mem_obj->id = getKeyCounter();
704 newkey = storeKeyPrivate(mem_obj->url, mem_obj->method, mem_obj->id);
705 } else {
706 newkey = storeKeyPrivate("JUNK", Http::METHOD_NONE, getKeyCounter());
707 }
708
709 assert(hash_lookup(store_table, newkey) == NULL);
710 EBIT_SET(flags, KEY_PRIVATE);
711 hashInsert(newkey);
712 }
713
714 void
715 StoreEntry::setPublicKey()
716 {
717 StoreEntry *e2 = NULL;
718 const cache_key *newkey;
719
720 if (key && !EBIT_TEST(flags, KEY_PRIVATE))
721 return; /* is already public */
722
723 assert(mem_obj);
724
725 /*
726 * We can't make RELEASE_REQUEST objects public. Depending on
727 * when RELEASE_REQUEST gets set, we might not be swapping out
728 * the object. If we're not swapping out, then subsequent
729 * store clients won't be able to access object data which has
730 * been freed from memory.
731 *
732 * If RELEASE_REQUEST is set, then ENTRY_CACHABLE should not
733 * be set, and StoreEntry::setPublicKey() should not be called.
734 */
735 #if MORE_DEBUG_OUTPUT
736
737 if (EBIT_TEST(flags, RELEASE_REQUEST))
738 debugs(20, DBG_IMPORTANT, "assertion failed: RELEASE key " << key << ", url " << mem_obj->url);
739
740 #endif
741
742 assert(!EBIT_TEST(flags, RELEASE_REQUEST));
743
744 if (mem_obj->request) {
745 HttpRequest *request = mem_obj->request;
746
747 if (!mem_obj->vary_headers) {
748 /* First handle the case where the object no longer varies */
749 safe_free(request->vary_headers);
750 } else {
751 if (request->vary_headers && strcmp(request->vary_headers, mem_obj->vary_headers) != 0) {
752 /* Oops.. the variance has changed. Kill the base object
753 * to record the new variance key
754 */
755 safe_free(request->vary_headers); /* free old "bad" variance key */
756 StoreEntry *pe = storeGetPublic(mem_obj->url, mem_obj->method);
757
758 if (pe)
759 pe->release();
760 }
761
762 /* Make sure the request knows the variance status */
763 if (!request->vary_headers) {
764 const char *vary = httpMakeVaryMark(request, mem_obj->getReply());
765
766 if (vary)
767 request->vary_headers = xstrdup(vary);
768 }
769 }
770
771 // TODO: storeGetPublic() calls below may create unlocked entries.
772 // We should add/use storeHas() API or lock/unlock those entries.
773 if (mem_obj->vary_headers && !storeGetPublic(mem_obj->url, mem_obj->method)) {
774 /* Create "vary" base object */
775 String vary;
776 StoreEntry *pe = storeCreateEntry(mem_obj->url, mem_obj->log_url, request->flags, request->method);
777 /* We are allowed to do this typecast */
778 HttpReply *rep = new HttpReply;
779 rep->setHeaders(Http::scOkay, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime + 100000);
780 vary = mem_obj->getReply()->header.getList(HDR_VARY);
781
782 if (vary.size()) {
783 /* Again, we own this structure layout */
784 rep->header.putStr(HDR_VARY, vary.termedBuf());
785 vary.clean();
786 }
787
788 #if X_ACCELERATOR_VARY
789 vary = mem_obj->getReply()->header.getList(HDR_X_ACCELERATOR_VARY);
790
791 if (vary.size() > 0) {
792 /* Again, we own this structure layout */
793 rep->header.putStr(HDR_X_ACCELERATOR_VARY, vary.termedBuf());
794 vary.clean();
795 }
796
797 #endif
798 pe->replaceHttpReply(rep, false); // no write until key is public
799
800 pe->timestampsSet();
801
802 pe->makePublic();
803
804 pe->startWriting(); // after makePublic()
805
806 pe->complete();
807
808 pe->unlock();
809 }
810
811 newkey = storeKeyPublicByRequest(mem_obj->request);
812 } else
813 newkey = storeKeyPublic(mem_obj->url, mem_obj->method);
814
815 if ((e2 = (StoreEntry *) hash_lookup(store_table, newkey))) {
816 debugs(20, 3, "StoreEntry::setPublicKey: Making old '" << mem_obj->url << "' private.");
817 e2->setPrivateKey();
818 e2->release();
819
820 if (mem_obj->request)
821 newkey = storeKeyPublicByRequest(mem_obj->request);
822 else
823 newkey = storeKeyPublic(mem_obj->url, mem_obj->method);
824 }
825
826 if (key)
827 hashDelete();
828
829 EBIT_CLR(flags, KEY_PRIVATE);
830
831 hashInsert(newkey);
832
833 if (swap_filen > -1)
834 storeDirSwapLog(this, SWAP_LOG_ADD);
835 }
836
837 StoreEntry *
838 storeCreateEntry(const char *url, const char *log_url, const RequestFlags &flags, const HttpRequestMethod& method)
839 {
840 StoreEntry *e = NULL;
841 MemObject *mem = NULL;
842 debugs(20, 3, "storeCreateEntry: '" << url << "'");
843
844 e = new StoreEntry(url, log_url);
845 e->lock_count = 1; /* Note lock here w/o calling storeLock() */
846 mem = e->mem_obj;
847 mem->method = method;
848
849 if (neighbors_do_private_keys || !flags.hierarchical)
850 e->setPrivateKey();
851 else
852 e->setPublicKey();
853
854 if (flags.cachable) {
855 EBIT_SET(e->flags, ENTRY_CACHABLE);
856 EBIT_CLR(e->flags, RELEASE_REQUEST);
857 } else {
858 /* StoreEntry::releaseRequest() clears ENTRY_CACHABLE */
859 e->releaseRequest();
860 }
861
862 e->store_status = STORE_PENDING;
863 e->setMemStatus(NOT_IN_MEMORY);
864 e->refcount = 0;
865 e->lastref = squid_curtime;
866 e->timestamp = -1; /* set in StoreEntry::timestampsSet() */
867 e->ping_status = PING_NONE;
868 EBIT_SET(e->flags, ENTRY_VALIDATED);
869 return e;
870 }
871
872 /* Mark object as expired */
873 void
874 StoreEntry::expireNow()
875 {
876 debugs(20, 3, "StoreEntry::expireNow: '" << getMD5Text() << "'");
877 expires = squid_curtime;
878 }
879
880 void
881 storeWriteComplete (void *data, StoreIOBuffer wroteBuffer)
882 {
883 PROF_start(storeWriteComplete);
884 StoreEntry *e = (StoreEntry *)data;
885
886 if (EBIT_TEST(e->flags, DELAY_SENDING)) {
887 PROF_stop(storeWriteComplete);
888 return;
889 }
890
891 e->invokeHandlers();
892 PROF_stop(storeWriteComplete);
893 }
894
895 void
896 StoreEntry::write (StoreIOBuffer writeBuffer)
897 {
898 assert(mem_obj != NULL);
899 /* This assert will change when we teach the store to update */
900 PROF_start(StoreEntry_write);
901 assert(store_status == STORE_PENDING);
902
903 debugs(20, 5, "storeWrite: writing " << writeBuffer.length << " bytes for '" << getMD5Text() << "'");
904 PROF_stop(StoreEntry_write);
905 storeGetMemSpace(writeBuffer.length);
906 mem_obj->write (writeBuffer, storeWriteComplete, this);
907 }
908
909 /* Append incoming data from a primary server to an entry. */
910 void
911 StoreEntry::append(char const *buf, int len)
912 {
913 assert(mem_obj != NULL);
914 assert(len >= 0);
915 assert(store_status == STORE_PENDING);
916
917 StoreIOBuffer tempBuffer;
918 tempBuffer.data = (char *)buf;
919 tempBuffer.length = len;
920 /*
921 * XXX sigh, offset might be < 0 here, but it gets "corrected"
922 * later. This offset crap is such a mess.
923 */
924 tempBuffer.offset = mem_obj->endOffset() - (getReply() ? getReply()->hdr_sz : 0);
925 write(tempBuffer);
926 }
927
928 void
929 storeAppendPrintf(StoreEntry * e, const char *fmt,...)
930 {
931 va_list args;
932 va_start(args, fmt);
933
934 storeAppendVPrintf(e, fmt, args);
935 va_end(args);
936 }
937
938 /* used be storeAppendPrintf and Packer */
939 void
940 storeAppendVPrintf(StoreEntry * e, const char *fmt, va_list vargs)
941 {
942 LOCAL_ARRAY(char, buf, 4096);
943 buf[0] = '\0';
944 vsnprintf(buf, 4096, fmt, vargs);
945 e->append(buf, strlen(buf));
946 }
947
948 struct _store_check_cachable_hist {
949
950 struct {
951 int non_get;
952 int not_entry_cachable;
953 int wrong_content_length;
954 int negative_cached;
955 int too_big;
956 int too_small;
957 int private_key;
958 int too_many_open_files;
959 int too_many_open_fds;
960 } no;
961
962 struct {
963 int Default;
964 } yes;
965 } store_check_cachable_hist;
966
967 int
968 storeTooManyDiskFilesOpen(void)
969 {
970 if (Config.max_open_disk_fds == 0)
971 return 0;
972
973 if (store_open_disk_fd > Config.max_open_disk_fds)
974 return 1;
975
976 return 0;
977 }
978
979 int
980 StoreEntry::checkTooSmall()
981 {
982 if (EBIT_TEST(flags, ENTRY_SPECIAL))
983 return 0;
984
985 if (STORE_OK == store_status)
986 if (mem_obj->object_sz < 0 ||
987 mem_obj->object_sz < Config.Store.minObjectSize)
988 return 1;
989 if (getReply()->content_length > -1)
990 if (getReply()->content_length < Config.Store.minObjectSize)
991 return 1;
992 return 0;
993 }
994
995 // TODO: remove checks already performed by swapoutPossible()
996 // TODO: move "too many open..." checks outside -- we are called too early/late
997 int
998 StoreEntry::checkCachable()
999 {
1000 #if CACHE_ALL_METHODS
1001
1002 if (mem_obj->method != Http::METHOD_GET) {
1003 debugs(20, 2, "StoreEntry::checkCachable: NO: non-GET method");
1004 ++store_check_cachable_hist.no.non_get;
1005 } else
1006 #endif
1007 if (store_status == STORE_OK && EBIT_TEST(flags, ENTRY_BAD_LENGTH)) {
1008 debugs(20, 2, "StoreEntry::checkCachable: NO: wrong content-length");
1009 ++store_check_cachable_hist.no.wrong_content_length;
1010 } else if (!EBIT_TEST(flags, ENTRY_CACHABLE)) {
1011 debugs(20, 2, "StoreEntry::checkCachable: NO: not cachable");
1012 ++store_check_cachable_hist.no.not_entry_cachable;
1013 } else if (EBIT_TEST(flags, ENTRY_NEGCACHED)) {
1014 debugs(20, 3, "StoreEntry::checkCachable: NO: negative cached");
1015 ++store_check_cachable_hist.no.negative_cached;
1016 return 0; /* avoid release call below */
1017 } else if ((getReply()->content_length > 0 &&
1018 getReply()->content_length > store_maxobjsize) ||
1019 mem_obj->endOffset() > store_maxobjsize) {
1020 debugs(20, 2, "StoreEntry::checkCachable: NO: too big");
1021 ++store_check_cachable_hist.no.too_big;
1022 } else if (checkTooSmall()) {
1023 debugs(20, 2, "StoreEntry::checkCachable: NO: too small");
1024 ++store_check_cachable_hist.no.too_small;
1025 } else if (EBIT_TEST(flags, KEY_PRIVATE)) {
1026 debugs(20, 3, "StoreEntry::checkCachable: NO: private key");
1027 ++store_check_cachable_hist.no.private_key;
1028 } else if (swap_status != SWAPOUT_NONE) {
1029 /*
1030 * here we checked the swap_status because the remaining
1031 * cases are only relevant only if we haven't started swapping
1032 * out the object yet.
1033 */
1034 return 1;
1035 } else if (storeTooManyDiskFilesOpen()) {
1036 debugs(20, 2, "StoreEntry::checkCachable: NO: too many disk files open");
1037 ++store_check_cachable_hist.no.too_many_open_files;
1038 } else if (fdNFree() < RESERVED_FD) {
1039 debugs(20, 2, "StoreEntry::checkCachable: NO: too many FD's open");
1040 ++store_check_cachable_hist.no.too_many_open_fds;
1041 } else {
1042 ++store_check_cachable_hist.yes.Default;
1043 return 1;
1044 }
1045
1046 releaseRequest();
1047 /* StoreEntry::releaseRequest() cleared ENTRY_CACHABLE */
1048 return 0;
1049 }
1050
1051 void
1052 storeCheckCachableStats(StoreEntry *sentry)
1053 {
1054 storeAppendPrintf(sentry, "Category\t Count\n");
1055
1056 #if CACHE_ALL_METHODS
1057
1058 storeAppendPrintf(sentry, "no.non_get\t%d\n",
1059 store_check_cachable_hist.no.non_get);
1060 #endif
1061
1062 storeAppendPrintf(sentry, "no.not_entry_cachable\t%d\n",
1063 store_check_cachable_hist.no.not_entry_cachable);
1064 storeAppendPrintf(sentry, "no.wrong_content_length\t%d\n",
1065 store_check_cachable_hist.no.wrong_content_length);
1066 storeAppendPrintf(sentry, "no.negative_cached\t%d\n",
1067 store_check_cachable_hist.no.negative_cached);
1068 storeAppendPrintf(sentry, "no.too_big\t%d\n",
1069 store_check_cachable_hist.no.too_big);
1070 storeAppendPrintf(sentry, "no.too_small\t%d\n",
1071 store_check_cachable_hist.no.too_small);
1072 storeAppendPrintf(sentry, "no.private_key\t%d\n",
1073 store_check_cachable_hist.no.private_key);
1074 storeAppendPrintf(sentry, "no.too_many_open_files\t%d\n",
1075 store_check_cachable_hist.no.too_many_open_files);
1076 storeAppendPrintf(sentry, "no.too_many_open_fds\t%d\n",
1077 store_check_cachable_hist.no.too_many_open_fds);
1078 storeAppendPrintf(sentry, "yes.default\t%d\n",
1079 store_check_cachable_hist.yes.Default);
1080 }
1081
1082 void
1083 StoreEntry::complete()
1084 {
1085 debugs(20, 3, "storeComplete: '" << getMD5Text() << "'");
1086
1087 if (store_status != STORE_PENDING) {
1088 /*
1089 * if we're not STORE_PENDING, then probably we got aborted
1090 * and there should be NO clients on this entry
1091 */
1092 assert(EBIT_TEST(flags, ENTRY_ABORTED));
1093 assert(mem_obj->nclients == 0);
1094 return;
1095 }
1096
1097 /* This is suspect: mem obj offsets include the headers. do we adjust for that
1098 * in use of object_sz?
1099 */
1100 mem_obj->object_sz = mem_obj->endOffset();
1101
1102 store_status = STORE_OK;
1103
1104 assert(mem_status == NOT_IN_MEMORY);
1105
1106 if (!validLength()) {
1107 EBIT_SET(flags, ENTRY_BAD_LENGTH);
1108 releaseRequest();
1109 }
1110
1111 #if USE_CACHE_DIGESTS
1112 if (mem_obj->request)
1113 mem_obj->request->hier.store_complete_stop = current_time;
1114
1115 #endif
1116 /*
1117 * We used to call invokeHandlers, then storeSwapOut. However,
1118 * Madhukar Reddy <myreddy@persistence.com> reported that
1119 * responses without content length would sometimes get released
1120 * in client_side, thinking that the response is incomplete.
1121 */
1122 invokeHandlers();
1123 }
1124
1125 /*
1126 * Someone wants to abort this transfer. Set the reason in the
1127 * request structure, call the server-side callback and mark the
1128 * entry for releasing
1129 */
1130 void
1131 StoreEntry::abort()
1132 {
1133 ++statCounter.aborted_requests;
1134 assert(store_status == STORE_PENDING);
1135 assert(mem_obj != NULL);
1136 debugs(20, 6, "storeAbort: " << getMD5Text());
1137
1138 lock(); /* lock while aborting */
1139 negativeCache();
1140
1141 releaseRequest();
1142
1143 EBIT_SET(flags, ENTRY_ABORTED);
1144
1145 setMemStatus(NOT_IN_MEMORY);
1146
1147 store_status = STORE_OK;
1148
1149 /* Notify the server side */
1150
1151 /*
1152 * DPW 2007-05-07
1153 * Should we check abort.data for validity?
1154 */
1155 if (mem_obj->abort.callback) {
1156 if (!cbdataReferenceValid(mem_obj->abort.data))
1157 debugs(20, DBG_IMPORTANT,HERE << "queueing event when abort.data is not valid");
1158 eventAdd("mem_obj->abort.callback",
1159 mem_obj->abort.callback,
1160 mem_obj->abort.data,
1161 0.0,
1162 true);
1163 unregisterAbort();
1164 }
1165
1166 /* XXX Should we reverse these two, so that there is no
1167 * unneeded disk swapping triggered?
1168 */
1169 /* Notify the client side */
1170 invokeHandlers();
1171
1172 // abort swap out, invalidating what was created so far (release follows)
1173 swapOutFileClose(StoreIOState::writerGone);
1174
1175 unlock(); /* unlock */
1176 }
1177
1178 /**
1179 * Clear Memory storage to accommodate the given object len
1180 */
1181 void
1182 storeGetMemSpace(int size)
1183 {
1184 PROF_start(storeGetMemSpace);
1185 StoreEntry *e = NULL;
1186 int released = 0;
1187 static time_t last_check = 0;
1188 size_t pages_needed;
1189 RemovalPurgeWalker *walker;
1190
1191 if (squid_curtime == last_check) {
1192 PROF_stop(storeGetMemSpace);
1193 return;
1194 }
1195
1196 last_check = squid_curtime;
1197
1198 pages_needed = (size + SM_PAGE_SIZE-1) / SM_PAGE_SIZE;
1199
1200 if (mem_node::InUseCount() + pages_needed < store_pages_max) {
1201 PROF_stop(storeGetMemSpace);
1202 return;
1203 }
1204
1205 debugs(20, 2, "storeGetMemSpace: Starting, need " << pages_needed <<
1206 " pages");
1207
1208 /* XXX what to set as max_scan here? */
1209 walker = mem_policy->PurgeInit(mem_policy, 100000);
1210
1211 while ((e = walker->Next(walker))) {
1212 e->purgeMem();
1213 ++released;
1214
1215 if (mem_node::InUseCount() + pages_needed < store_pages_max)
1216 break;
1217 }
1218
1219 walker->Done(walker);
1220 debugs(20, 3, "storeGetMemSpace stats:");
1221 debugs(20, 3, " " << std::setw(6) << hot_obj_count << " HOT objects");
1222 debugs(20, 3, " " << std::setw(6) << released << " were released");
1223 PROF_stop(storeGetMemSpace);
1224 }
1225
1226 /* thunk through to Store::Root().maintain(). Note that this would be better still
1227 * if registered against the root store itself, but that requires more complex
1228 * update logic - bigger fish to fry first. Long term each store when
1229 * it becomes active will self register
1230 */
1231 void
1232 Store::Maintain(void *notused)
1233 {
1234 Store::Root().maintain();
1235
1236 /* Reregister a maintain event .. */
1237 eventAdd("MaintainSwapSpace", Maintain, NULL, 1.0, 1);
1238
1239 }
1240
1241 /* The maximum objects to scan for maintain storage space */
1242 #define MAINTAIN_MAX_SCAN 1024
1243 #define MAINTAIN_MAX_REMOVE 64
1244
1245 /*
1246 * This routine is to be called by main loop in main.c.
1247 * It removes expired objects on only one bucket for each time called.
1248 *
1249 * This should get called 1/s from main().
1250 */
1251 void
1252 StoreController::maintain()
1253 {
1254 static time_t last_warn_time = 0;
1255
1256 PROF_start(storeMaintainSwapSpace);
1257 swapDir->maintain();
1258
1259 /* this should be emitted by the oversize dir, not globally */
1260
1261 if (Store::Root().currentSize() > Store::Root().maxSize()) {
1262 if (squid_curtime - last_warn_time > 10) {
1263 debugs(20, DBG_CRITICAL, "WARNING: Disk space over limit: "
1264 << Store::Root().currentSize() / 1024.0 << " KB > "
1265 << (Store::Root().maxSize() >> 10) << " KB");
1266 last_warn_time = squid_curtime;
1267 }
1268 }
1269
1270 PROF_stop(storeMaintainSwapSpace);
1271 }
1272
1273 /* release an object from a cache */
1274 void
1275 StoreEntry::release()
1276 {
1277 PROF_start(storeRelease);
1278 debugs(20, 3, "storeRelease: Releasing: '" << getMD5Text() << "'");
1279 /* If, for any reason we can't discard this object because of an
1280 * outstanding request, mark it for pending release */
1281
1282 if (locked()) {
1283 expireNow();
1284 debugs(20, 3, "storeRelease: Only setting RELEASE_REQUEST bit");
1285 releaseRequest();
1286 PROF_stop(storeRelease);
1287 return;
1288 }
1289
1290 if (StoreController::store_dirs_rebuilding && swap_filen > -1) {
1291 setPrivateKey();
1292
1293 if (mem_obj)
1294 destroyMemObject();
1295
1296 if (swap_filen > -1) {
1297 /*
1298 * Fake a call to StoreEntry->lock() When rebuilding is done,
1299 * we'll just call StoreEntry->unlock() on these.
1300 */
1301 ++lock_count;
1302 setReleaseFlag();
1303 LateReleaseStack.push_back(this);
1304 } else {
1305 destroyStoreEntry(static_cast<hash_link *>(this));
1306 // "this" is no longer valid
1307 }
1308
1309 PROF_stop(storeRelease);
1310 return;
1311 }
1312
1313 storeLog(STORE_LOG_RELEASE, this);
1314
1315 if (swap_filen > -1) {
1316 // log before unlink() below clears swap_filen
1317 if (!EBIT_TEST(flags, KEY_PRIVATE))
1318 storeDirSwapLog(this, SWAP_LOG_DEL);
1319
1320 unlink();
1321 }
1322
1323 setMemStatus(NOT_IN_MEMORY);
1324 destroyStoreEntry(static_cast<hash_link *>(this));
1325 PROF_stop(storeRelease);
1326 }
1327
1328 static void
1329 storeLateRelease(void *unused)
1330 {
1331 StoreEntry *e;
1332 int i;
1333 static int n = 0;
1334
1335 if (StoreController::store_dirs_rebuilding) {
1336 eventAdd("storeLateRelease", storeLateRelease, NULL, 1.0, 1);
1337 return;
1338 }
1339
1340 for (i = 0; i < 10; ++i) {
1341 e = LateReleaseStack.count ? LateReleaseStack.pop() : NULL;
1342
1343 if (e == NULL) {
1344 /* done! */
1345 debugs(20, DBG_IMPORTANT, "storeLateRelease: released " << n << " objects");
1346 return;
1347 }
1348
1349 e->unlock();
1350 ++n;
1351 }
1352
1353 eventAdd("storeLateRelease", storeLateRelease, NULL, 0.0, 1);
1354 }
1355
1356 /* return 1 if a store entry is locked */
1357 int
1358 StoreEntry::locked() const
1359 {
1360 if (lock_count)
1361 return 1;
1362
1363 if (swap_status == SWAPOUT_WRITING)
1364 return 1;
1365
1366 if (store_status == STORE_PENDING)
1367 return 1;
1368
1369 /*
1370 * SPECIAL, PUBLIC entries should be "locked"
1371 */
1372 if (EBIT_TEST(flags, ENTRY_SPECIAL))
1373 if (!EBIT_TEST(flags, KEY_PRIVATE))
1374 return 1;
1375
1376 return 0;
1377 }
1378
1379 bool
1380 StoreEntry::validLength() const
1381 {
1382 int64_t diff;
1383 const HttpReply *reply;
1384 assert(mem_obj != NULL);
1385 reply = getReply();
1386 debugs(20, 3, "storeEntryValidLength: Checking '" << getMD5Text() << "'");
1387 debugs(20, 5, "storeEntryValidLength: object_len = " <<
1388 objectLen());
1389 debugs(20, 5, "storeEntryValidLength: hdr_sz = " << reply->hdr_sz);
1390 debugs(20, 5, "storeEntryValidLength: content_length = " << reply->content_length);
1391
1392 if (reply->content_length < 0) {
1393 debugs(20, 5, "storeEntryValidLength: Unspecified content length: " << getMD5Text());
1394 return 1;
1395 }
1396
1397 if (reply->hdr_sz == 0) {
1398 debugs(20, 5, "storeEntryValidLength: Zero header size: " << getMD5Text());
1399 return 1;
1400 }
1401
1402 if (mem_obj->method == Http::METHOD_HEAD) {
1403 debugs(20, 5, "storeEntryValidLength: HEAD request: " << getMD5Text());
1404 return 1;
1405 }
1406
1407 if (reply->sline.status() == Http::scNotModified)
1408 return 1;
1409
1410 if (reply->sline.status() == Http::scNoContent)
1411 return 1;
1412
1413 diff = reply->hdr_sz + reply->content_length - objectLen();
1414
1415 if (diff == 0)
1416 return 1;
1417
1418 debugs(20, 3, "storeEntryValidLength: " << (diff < 0 ? -diff : diff) << " bytes too " << (diff < 0 ? "big" : "small") <<"; '" << getMD5Text() << "'" );
1419
1420 return 0;
1421 }
1422
1423 static void
1424 storeRegisterWithCacheManager(void)
1425 {
1426 Mgr::RegisterAction("storedir", "Store Directory Stats", Store::Stats, 0, 1);
1427 Mgr::RegisterAction("store_io", "Store IO Interface Stats", &Mgr::StoreIoAction::Create, 0, 1);
1428 Mgr::RegisterAction("store_check_cachable_stats", "storeCheckCachable() Stats",
1429 storeCheckCachableStats, 0, 1);
1430 }
1431
1432 void
1433 storeInit(void)
1434 {
1435 storeKeyInit();
1436 mem_policy = createRemovalPolicy(Config.memPolicy);
1437 storeDigestInit();
1438 storeLogOpen();
1439 eventAdd("storeLateRelease", storeLateRelease, NULL, 1.0, 1);
1440 Store::Root().init();
1441 storeRebuildStart();
1442
1443 storeRegisterWithCacheManager();
1444 }
1445
1446 void
1447 storeConfigure(void)
1448 {
1449 store_swap_high = (long) (((float) Store::Root().maxSize() *
1450 (float) Config.Swap.highWaterMark) / (float) 100);
1451 store_swap_low = (long) (((float) Store::Root().maxSize() *
1452 (float) Config.Swap.lowWaterMark) / (float) 100);
1453 store_pages_max = Config.memMaxSize / sizeof(mem_node);
1454 }
1455
1456 bool
1457 StoreEntry::memoryCachable() const
1458 {
1459 if (mem_obj == NULL)
1460 return 0;
1461
1462 if (mem_obj->data_hdr.size() == 0)
1463 return 0;
1464
1465 if (mem_obj->inmem_lo != 0)
1466 return 0;
1467
1468 if (!Config.onoff.memory_cache_first && swap_status == SWAPOUT_DONE && refcount == 1)
1469 return 0;
1470
1471 return 1;
1472 }
1473
1474 int
1475 StoreEntry::checkNegativeHit() const
1476 {
1477 if (!EBIT_TEST(flags, ENTRY_NEGCACHED))
1478 return 0;
1479
1480 if (expires <= squid_curtime)
1481 return 0;
1482
1483 if (store_status != STORE_OK)
1484 return 0;
1485
1486 return 1;
1487 }
1488
1489 /**
1490 * Set object for negative caching.
1491 * Preserves any expiry information given by the server.
1492 * In absence of proper expiry info it will set to expire immediately,
1493 * or with HTTP-violations enabled the configured negative-TTL is observed
1494 */
1495 void
1496 StoreEntry::negativeCache()
1497 {
1498 // XXX: should make the default for expires 0 instead of -1
1499 // so we can distinguish "Expires: -1" from nothing.
1500 if (expires <= 0)
1501 #if USE_HTTP_VIOLATIONS
1502 expires = squid_curtime + Config.negativeTtl;
1503 #else
1504 expires = squid_curtime;
1505 #endif
1506 EBIT_SET(flags, ENTRY_NEGCACHED);
1507 }
1508
1509 void
1510 storeFreeMemory(void)
1511 {
1512 Store::Root(NULL);
1513 #if USE_CACHE_DIGESTS
1514
1515 if (store_digest)
1516 cacheDigestDestroy(store_digest);
1517
1518 #endif
1519
1520 store_digest = NULL;
1521 }
1522
1523 int
1524 expiresMoreThan(time_t expires, time_t when)
1525 {
1526 if (expires < 0) /* No Expires given */
1527 return 1;
1528
1529 return (expires > (squid_curtime + when));
1530 }
1531
1532 int
1533 StoreEntry::validToSend() const
1534 {
1535 if (EBIT_TEST(flags, RELEASE_REQUEST))
1536 return 0;
1537
1538 if (EBIT_TEST(flags, ENTRY_NEGCACHED))
1539 if (expires <= squid_curtime)
1540 return 0;
1541
1542 if (EBIT_TEST(flags, ENTRY_ABORTED))
1543 return 0;
1544
1545 return 1;
1546 }
1547
1548 void
1549 StoreEntry::timestampsSet()
1550 {
1551 const HttpReply *reply = getReply();
1552 time_t served_date = reply->date;
1553 int age = reply->header.getInt(HDR_AGE);
1554 /* Compute the timestamp, mimicking RFC2616 section 13.2.3. */
1555 /* make sure that 0 <= served_date <= squid_curtime */
1556
1557 if (served_date < 0 || served_date > squid_curtime)
1558 served_date = squid_curtime;
1559
1560 /* Bug 1791:
1561 * If the returned Date: is more than 24 hours older than
1562 * the squid_curtime, then one of us needs to use NTP to set our
1563 * clock. We'll pretend that our clock is right.
1564 */
1565 else if (served_date < (squid_curtime - 24 * 60 * 60) )
1566 served_date = squid_curtime;
1567
1568 /*
1569 * Compensate with Age header if origin server clock is ahead
1570 * of us and there is a cache in between us and the origin
1571 * server. But DONT compensate if the age value is larger than
1572 * squid_curtime because it results in a negative served_date.
1573 */
1574 if (age > squid_curtime - served_date)
1575 if (squid_curtime > age)
1576 served_date = squid_curtime - age;
1577
1578 // compensate for Squid-to-server and server-to-Squid delays
1579 if (mem_obj && mem_obj->request) {
1580 const time_t request_sent =
1581 mem_obj->request->hier.peer_http_request_sent.tv_sec;
1582 if (0 < request_sent && request_sent < squid_curtime)
1583 served_date -= (squid_curtime - request_sent);
1584 }
1585
1586 if (reply->expires > 0 && reply->date > -1)
1587 expires = served_date + (reply->expires - reply->date);
1588 else
1589 expires = reply->expires;
1590
1591 lastmod = reply->last_modified;
1592
1593 timestamp = served_date;
1594 }
1595
1596 void
1597 StoreEntry::registerAbort(STABH * cb, void *data)
1598 {
1599 assert(mem_obj);
1600 assert(mem_obj->abort.callback == NULL);
1601 mem_obj->abort.callback = cb;
1602 mem_obj->abort.data = cbdataReference(data);
1603 }
1604
1605 void
1606 StoreEntry::unregisterAbort()
1607 {
1608 assert(mem_obj);
1609 if (mem_obj->abort.callback) {
1610 mem_obj->abort.callback = NULL;
1611 cbdataReferenceDone(mem_obj->abort.data);
1612 }
1613 }
1614
1615 void
1616 StoreEntry::dump(int l) const
1617 {
1618 debugs(20, l, "StoreEntry->key: " << getMD5Text());
1619 debugs(20, l, "StoreEntry->next: " << next);
1620 debugs(20, l, "StoreEntry->mem_obj: " << mem_obj);
1621 debugs(20, l, "StoreEntry->timestamp: " << timestamp);
1622 debugs(20, l, "StoreEntry->lastref: " << lastref);
1623 debugs(20, l, "StoreEntry->expires: " << expires);
1624 debugs(20, l, "StoreEntry->lastmod: " << lastmod);
1625 debugs(20, l, "StoreEntry->swap_file_sz: " << swap_file_sz);
1626 debugs(20, l, "StoreEntry->refcount: " << refcount);
1627 debugs(20, l, "StoreEntry->flags: " << storeEntryFlags(this));
1628 debugs(20, l, "StoreEntry->swap_dirn: " << swap_dirn);
1629 debugs(20, l, "StoreEntry->swap_filen: " << swap_filen);
1630 debugs(20, l, "StoreEntry->lock_count: " << lock_count);
1631 debugs(20, l, "StoreEntry->mem_status: " << mem_status);
1632 debugs(20, l, "StoreEntry->ping_status: " << ping_status);
1633 debugs(20, l, "StoreEntry->store_status: " << store_status);
1634 debugs(20, l, "StoreEntry->swap_status: " << swap_status);
1635 }
1636
1637 /*
1638 * NOTE, this function assumes only two mem states
1639 */
1640 void
1641 StoreEntry::setMemStatus(mem_status_t new_status)
1642 {
1643 if (new_status == mem_status)
1644 return;
1645
1646 // are we using a shared memory cache?
1647 if (Config.memShared && IamWorkerProcess()) {
1648 // enumerate calling cases if shared memory is enabled
1649 assert(new_status != IN_MEMORY || EBIT_TEST(flags, ENTRY_SPECIAL));
1650 // This method was designed to update replacement policy, not to
1651 // actually purge something from the memory cache (TODO: rename?).
1652 // Shared memory cache does not have a policy that needs updates.
1653 mem_status = new_status;
1654 return;
1655 }
1656
1657 assert(mem_obj != NULL);
1658
1659 if (new_status == IN_MEMORY) {
1660 assert(mem_obj->inmem_lo == 0);
1661
1662 if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
1663 debugs(20, 4, "StoreEntry::setMemStatus: not inserting special " << mem_obj->url << " into policy");
1664 } else {
1665 mem_policy->Add(mem_policy, this, &mem_obj->repl);
1666 debugs(20, 4, "StoreEntry::setMemStatus: inserted mem node " << mem_obj->url << " key: " << getMD5Text());
1667 }
1668
1669 ++hot_obj_count; // TODO: maintain for the shared hot cache as well
1670 } else {
1671 if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
1672 debugs(20, 4, "StoreEntry::setMemStatus: special entry " << mem_obj->url);
1673 } else {
1674 mem_policy->Remove(mem_policy, this, &mem_obj->repl);
1675 debugs(20, 4, "StoreEntry::setMemStatus: removed mem node " << mem_obj->url);
1676 }
1677
1678 --hot_obj_count;
1679 }
1680
1681 mem_status = new_status;
1682 }
1683
1684 const char *
1685 StoreEntry::url() const
1686 {
1687 if (this == NULL)
1688 return "[null_entry]";
1689 else if (mem_obj == NULL)
1690 return "[null_mem_obj]";
1691 else
1692 return mem_obj->url;
1693 }
1694
1695 void
1696 StoreEntry::createMemObject(const char *aUrl, const char *aLogUrl)
1697 {
1698 debugs(20, 3, "A mem_obj create attempted using : " << aUrl);
1699
1700 if (mem_obj)
1701 return;
1702
1703 if (hidden_mem_obj) {
1704 debugs(20, 3, HERE << "restoring " << hidden_mem_obj);
1705 mem_obj = hidden_mem_obj;
1706 hidden_mem_obj = NULL;
1707 mem_obj->resetUrls(aUrl, aLogUrl);
1708 return;
1709 }
1710
1711 mem_obj = new MemObject(aUrl, aLogUrl);
1712 }
1713
1714 /* this just sets DELAY_SENDING */
1715 void
1716 StoreEntry::buffer()
1717 {
1718 EBIT_SET(flags, DELAY_SENDING);
1719 }
1720
1721 /* this just clears DELAY_SENDING and Invokes the handlers */
1722 void
1723 StoreEntry::flush()
1724 {
1725 if (EBIT_TEST(flags, DELAY_SENDING)) {
1726 EBIT_CLR(flags, DELAY_SENDING);
1727 invokeHandlers();
1728 }
1729 }
1730
1731 int64_t
1732 StoreEntry::objectLen() const
1733 {
1734 assert(mem_obj != NULL);
1735 return mem_obj->object_sz;
1736 }
1737
1738 int64_t
1739 StoreEntry::contentLen() const
1740 {
1741 assert(mem_obj != NULL);
1742 assert(getReply() != NULL);
1743 return objectLen() - getReply()->hdr_sz;
1744 }
1745
1746 HttpReply const *
1747 StoreEntry::getReply () const
1748 {
1749 if (NULL == mem_obj)
1750 return NULL;
1751
1752 return mem_obj->getReply();
1753 }
1754
1755 void
1756 StoreEntry::reset()
1757 {
1758 assert (mem_obj);
1759 debugs(20, 3, "StoreEntry::reset: " << url());
1760 mem_obj->reset();
1761 HttpReply *rep = (HttpReply *) getReply(); // bypass const
1762 rep->reset();
1763 expires = lastmod = timestamp = -1;
1764 }
1765
1766 /*
1767 * storeFsInit
1768 *
1769 * This routine calls the SETUP routine for each fs type.
1770 * I don't know where the best place for this is, and I'm not going to shuffle
1771 * around large chunks of code right now (that can be done once its working.)
1772 */
1773 void
1774 storeFsInit(void)
1775 {
1776 storeReplSetup();
1777 }
1778
1779 /*
1780 * called to add another store removal policy module
1781 */
1782 void
1783 storeReplAdd(const char *type, REMOVALPOLICYCREATE * create)
1784 {
1785 int i;
1786
1787 /* find the number of currently known repl types */
1788 for (i = 0; storerepl_list && storerepl_list[i].typestr; ++i) {
1789 if (strcmp(storerepl_list[i].typestr, type) == 0) {
1790 debugs(20, DBG_IMPORTANT, "WARNING: Trying to load store replacement policy " << type << " twice.");
1791 return;
1792 }
1793 }
1794
1795 /* add the new type */
1796 storerepl_list = static_cast<storerepl_entry_t *>(xrealloc(storerepl_list, (i + 2) * sizeof(storerepl_entry_t)));
1797
1798 memset(&storerepl_list[i + 1], 0, sizeof(storerepl_entry_t));
1799
1800 storerepl_list[i].typestr = type;
1801
1802 storerepl_list[i].create = create;
1803 }
1804
1805 /*
1806 * Create a removal policy instance
1807 */
1808 RemovalPolicy *
1809 createRemovalPolicy(RemovalPolicySettings * settings)
1810 {
1811 storerepl_entry_t *r;
1812
1813 for (r = storerepl_list; r && r->typestr; ++r) {
1814 if (strcmp(r->typestr, settings->type) == 0)
1815 return r->create(settings->args);
1816 }
1817
1818 debugs(20, DBG_IMPORTANT, "ERROR: Unknown policy " << settings->type);
1819 debugs(20, DBG_IMPORTANT, "ERROR: Be sure to have set cache_replacement_policy");
1820 debugs(20, DBG_IMPORTANT, "ERROR: and memory_replacement_policy in squid.conf!");
1821 fatalf("ERROR: Unknown policy %s\n", settings->type);
1822 return NULL; /* NOTREACHED */
1823 }
1824
1825 #if 0
1826 void
1827 storeSwapFileNumberSet(StoreEntry * e, sfileno filn)
1828 {
1829 if (e->swap_file_number == filn)
1830 return;
1831
1832 if (filn < 0) {
1833 assert(-1 == filn);
1834 storeDirMapBitReset(e->swap_file_number);
1835 storeDirLRUDelete(e);
1836 e->swap_file_number = -1;
1837 } else {
1838 assert(-1 == e->swap_file_number);
1839 storeDirMapBitSet(e->swap_file_number = filn);
1840 storeDirLRUAdd(e);
1841 }
1842 }
1843
1844 #endif
1845
1846 /*
1847 * Replace a store entry with
1848 * a new reply. This eats the reply.
1849 */
1850 void
1851 StoreEntry::replaceHttpReply(HttpReply *rep, bool andStartWriting)
1852 {
1853 debugs(20, 3, "StoreEntry::replaceHttpReply: " << url());
1854
1855 if (!mem_obj) {
1856 debugs(20, DBG_CRITICAL, "Attempt to replace object with no in-memory representation");
1857 return;
1858 }
1859
1860 mem_obj->replaceHttpReply(rep);
1861
1862 if (andStartWriting)
1863 startWriting();
1864 }
1865
1866 void
1867 StoreEntry::startWriting()
1868 {
1869 Packer p;
1870
1871 /* TODO: when we store headers serparately remove the header portion */
1872 /* TODO: mark the length of the headers ? */
1873 /* We ONLY want the headers */
1874 packerToStoreInit(&p, this);
1875
1876 assert (isEmpty());
1877 assert(mem_obj);
1878
1879 const HttpReply *rep = getReply();
1880 assert(rep);
1881
1882 rep->packHeadersInto(&p);
1883 mem_obj->markEndOfReplyHeaders();
1884
1885 rep->body.packInto(&p);
1886
1887 packerClean(&p);
1888 }
1889
1890 char const *
1891 StoreEntry::getSerialisedMetaData()
1892 {
1893 StoreMeta *tlv_list = storeSwapMetaBuild(this);
1894 int swap_hdr_sz;
1895 char *result = storeSwapMetaPack(tlv_list, &swap_hdr_sz);
1896 storeSwapTLVFree(tlv_list);
1897 assert (swap_hdr_sz >= 0);
1898 mem_obj->swap_hdr_sz = (size_t) swap_hdr_sz;
1899 return result;
1900 }
1901
1902 void
1903 StoreEntry::trimMemory(const bool preserveSwappable)
1904 {
1905 /*
1906 * DPW 2007-05-09
1907 * Bug #1943. We must not let go any data for IN_MEMORY
1908 * objects. We have to wait until the mem_status changes.
1909 */
1910 if (mem_status == IN_MEMORY)
1911 return;
1912
1913 if (EBIT_TEST(flags, ENTRY_SPECIAL))
1914 return; // cannot trim because we do not load them again
1915
1916 if (!preserveSwappable) {
1917 if (mem_obj->policyLowestOffsetToKeep(0) == 0) {
1918 /* Nothing to do */
1919 return;
1920 }
1921 /*
1922 * Its not swap-able, and we're about to delete a chunk,
1923 * so we must make it PRIVATE. This is tricky/ugly because
1924 * for the most part, we treat swapable == cachable here.
1925 */
1926 releaseRequest();
1927 mem_obj->trimUnSwappable ();
1928 } else {
1929 mem_obj->trimSwappable ();
1930 }
1931 }
1932
1933 bool
1934 StoreEntry::modifiedSince(HttpRequest * request) const
1935 {
1936 int object_length;
1937 time_t mod_time = lastmod;
1938
1939 if (mod_time < 0)
1940 mod_time = timestamp;
1941
1942 debugs(88, 3, "modifiedSince: '" << url() << "'");
1943
1944 debugs(88, 3, "modifiedSince: mod_time = " << mod_time);
1945
1946 if (mod_time < 0)
1947 return true;
1948
1949 /* Find size of the object */
1950 object_length = getReply()->content_length;
1951
1952 if (object_length < 0)
1953 object_length = contentLen();
1954
1955 if (mod_time > request->ims) {
1956 debugs(88, 3, "--> YES: entry newer than client");
1957 return true;
1958 } else if (mod_time < request->ims) {
1959 debugs(88, 3, "--> NO: entry older than client");
1960 return false;
1961 } else if (request->imslen < 0) {
1962 debugs(88, 3, "--> NO: same LMT, no client length");
1963 return false;
1964 } else if (request->imslen == object_length) {
1965 debugs(88, 3, "--> NO: same LMT, same length");
1966 return false;
1967 } else {
1968 debugs(88, 3, "--> YES: same LMT, different length");
1969 return true;
1970 }
1971 }
1972
1973 bool
1974 StoreEntry::hasEtag(ETag &etag) const
1975 {
1976 if (const HttpReply *reply = getReply()) {
1977 etag = reply->header.getETag(HDR_ETAG);
1978 if (etag.str)
1979 return true;
1980 }
1981 return false;
1982 }
1983
1984 bool
1985 StoreEntry::hasIfMatchEtag(const HttpRequest &request) const
1986 {
1987 const String reqETags = request.header.getList(HDR_IF_MATCH);
1988 return hasOneOfEtags(reqETags, false);
1989 }
1990
1991 bool
1992 StoreEntry::hasIfNoneMatchEtag(const HttpRequest &request) const
1993 {
1994 const String reqETags = request.header.getList(HDR_IF_NONE_MATCH);
1995 // weak comparison is allowed only for HEAD or full-body GET requests
1996 const bool allowWeakMatch = !request.flags.isRanged &&
1997 (request.method == Http::METHOD_GET || request.method == Http::METHOD_HEAD);
1998 return hasOneOfEtags(reqETags, allowWeakMatch);
1999 }
2000
2001 /// whether at least one of the request ETags matches entity ETag
2002 bool
2003 StoreEntry::hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const
2004 {
2005 const ETag repETag = getReply()->header.getETag(HDR_ETAG);
2006 if (!repETag.str)
2007 return strListIsMember(&reqETags, "*", ',');
2008
2009 bool matched = false;
2010 const char *pos = NULL;
2011 const char *item;
2012 int ilen;
2013 while (!matched && strListGetItem(&reqETags, ',', &item, &ilen, &pos)) {
2014 if (!strncmp(item, "*", ilen))
2015 matched = true;
2016 else {
2017 String str;
2018 str.append(item, ilen);
2019 ETag reqETag;
2020 if (etagParseInit(&reqETag, str.termedBuf())) {
2021 matched = allowWeakMatch ? etagIsWeakEqual(repETag, reqETag) :
2022 etagIsStrongEqual(repETag, reqETag);
2023 }
2024 }
2025 }
2026 return matched;
2027 }
2028
2029 SwapDir::Pointer
2030 StoreEntry::store() const
2031 {
2032 assert(0 <= swap_dirn && swap_dirn < Config.cacheSwap.n_configured);
2033 return INDEXSD(swap_dirn);
2034 }
2035
2036 void
2037 StoreEntry::unlink()
2038 {
2039 store()->unlink(*this); // implies disconnect()
2040 swap_filen = -1;
2041 swap_dirn = -1;
2042 swap_status = SWAPOUT_NONE;
2043 }
2044
2045 /*
2046 * return true if the entry is in a state where
2047 * it can accept more data (ie with write() method)
2048 */
2049 bool
2050 StoreEntry::isAccepting() const
2051 {
2052 if (STORE_PENDING != store_status)
2053 return false;
2054
2055 if (EBIT_TEST(flags, ENTRY_ABORTED))
2056 return false;
2057
2058 return true;
2059 }
2060
2061 std::ostream &operator <<(std::ostream &os, const StoreEntry &e)
2062 {
2063 return os << e.swap_filen << '@' << e.swap_dirn << '=' <<
2064 e.mem_status << '/' << e.ping_status << '/' << e.store_status << '/' <<
2065 e.swap_status;
2066 }
2067
2068 /* NullStoreEntry */
2069
2070 NullStoreEntry NullStoreEntry::_instance;
2071
2072 NullStoreEntry *
2073 NullStoreEntry::getInstance()
2074 {
2075 return &_instance;
2076 }
2077
2078 char const *
2079 NullStoreEntry::getMD5Text() const
2080 {
2081 return "N/A";
2082 }
2083
2084 void
2085 NullStoreEntry::operator delete(void*)
2086 {
2087 fatal ("Attempt to delete NullStoreEntry\n");
2088 }
2089
2090 char const *
2091 NullStoreEntry::getSerialisedMetaData()
2092 {
2093 return NULL;
2094 }
2095
2096 #if !_USE_INLINE_
2097 #include "Store.cci"
2098 #endif