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