3 * $Id: store.cc,v 1.537 2001/02/22 20:53:15 wessels Exp $
5 * DEBUG: section 20 Storage Manager
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 #define REBUILD_TIMESTAMP_DELTA_MAX 2
40 #define STORE_IN_MEM_BUCKETS (229)
42 const char *memStatusStr
[] =
48 const char *pingStatusStr
[] =
55 const char *storeStatusStr
[] =
61 const char *swapStatusStr
[] =
68 typedef struct lock_ctrl_t
{
74 extern OBJH storeIOStats
;
77 * local function prototypes
79 static int storeEntryValidLength(const StoreEntry
*);
80 static void storeGetMemSpace(int);
81 static void storeHashDelete(StoreEntry
*);
82 static MemObject
*new_MemObject(const char *, const char *);
83 static void destroy_MemObject(StoreEntry
*);
84 static FREE destroy_StoreEntry
;
85 static void storePurgeMem(StoreEntry
*);
86 static void storeEntryReferenced(StoreEntry
*);
87 static void storeEntryDereferenced(StoreEntry
*);
88 static int getKeyCounter(void);
89 static int storeKeepInMemory(const StoreEntry
*);
90 static OBJH storeCheckCachableStats
;
91 static EVH storeLateRelease
;
96 static Stack LateReleaseStack
;
98 #if URL_CHECKSUM_DEBUG
100 url_checksum(const char *url
)
104 static unsigned char digest
[16];
106 MD5Update(&M
, (unsigned char *) url
, strlen(url
));
107 MD5Final(digest
, &M
);
108 xmemcpy(&ck
, digest
, sizeof(ck
));
114 new_MemObject(const char *url
, const char *log_url
)
116 MemObject
*mem
= memAllocate(MEM_MEMOBJECT
);
117 mem
->reply
= httpReplyCreate();
118 mem
->url
= xstrdup(url
);
119 #if URL_CHECKSUM_DEBUG
120 mem
->chksum
= url_checksum(mem
->url
);
122 mem
->log_url
= xstrdup(log_url
);
125 /* XXX account log_url */
126 debug(20, 3) ("new_MemObject: returning %p\n", mem
);
131 new_StoreEntry(int mem_obj_flag
, const char *url
, const char *log_url
)
133 StoreEntry
*e
= NULL
;
134 e
= memAllocate(MEM_STOREENTRY
);
136 e
->mem_obj
= new_MemObject(url
, log_url
);
137 debug(20, 3) ("new_StoreEntry: returning %p\n", e
);
138 e
->expires
= e
->lastmod
= e
->lastref
= e
->timestamp
= -1;
145 destroy_MemObject(StoreEntry
* e
)
147 MemObject
*mem
= e
->mem_obj
;
148 const Ctx ctx
= ctx_enter(mem
->url
);
149 debug(20, 3) ("destroy_MemObject: destroying %p\n", mem
);
150 #if URL_CHECKSUM_DEBUG
151 assert(mem
->chksum
== url_checksum(mem
->url
));
155 assert(mem
->swapout
.sio
== NULL
);
156 stmemFree(&mem
->data_hdr
);
159 * There is no way to abort FD-less clients, so they might
160 * still have mem->clients set if mem->fd == -1
162 assert(mem
->fd
== -1 || mem
->clients
.head
== NULL
);
163 httpReplyDestroy(mem
->reply
);
164 requestUnlink(mem
->request
);
166 ctx_exit(ctx
); /* must exit before we free mem->url */
168 safe_free(mem
->log_url
); /* XXX account log_url */
169 memFree(mem
, MEM_MEMOBJECT
);
173 destroy_StoreEntry(void *data
)
175 StoreEntry
*e
= data
;
176 debug(20, 3) ("destroy_StoreEntry: destroying %p\n", e
);
179 destroy_MemObject(e
);
181 assert(e
->hash
.key
== NULL
);
182 memFree(e
, MEM_STOREENTRY
);
185 /* ----- INTERFACE BETWEEN STORAGE MANAGER AND HASH TABLE FUNCTIONS --------- */
188 storeHashInsert(StoreEntry
* e
, const cache_key
* key
)
190 debug(20, 3) ("storeHashInsert: Inserting Entry %p key '%s'\n",
191 e
, storeKeyText(key
));
192 e
->hash
.key
= storeKeyDup(key
);
193 hash_join(store_table
, &e
->hash
);
197 storeHashDelete(StoreEntry
* e
)
199 hash_remove_link(store_table
, &e
->hash
);
200 storeKeyFree(e
->hash
.key
);
204 /* -------------------------------------------------------------------------- */
207 /* get rid of memory copy of the object */
208 /* Only call this if storeCheckPurgeMem(e) returns 1 */
210 storePurgeMem(StoreEntry
* e
)
212 if (e
->mem_obj
== NULL
)
214 debug(20, 3) ("storePurgeMem: Freeing memory-copy of %s\n",
215 storeKeyText(e
->hash
.key
));
216 storeSetMemStatus(e
, NOT_IN_MEMORY
);
217 destroy_MemObject(e
);
218 if (e
->swap_status
!= SWAPOUT_DONE
)
223 storeEntryReferenced(StoreEntry
* e
)
227 /* Notify the fs that we're referencing this object again */
228 if (e
->swap_dirn
> -1) {
229 SD
= INDEXSD(e
->swap_dirn
);
233 /* Notify the memory cache that we're referencing this object again */
235 if (mem_policy
->Referenced
)
236 mem_policy
->Referenced(mem_policy
, e
, &e
->mem_obj
->repl
);
241 storeEntryDereferenced(StoreEntry
* e
)
245 /* Notify the fs that we're not referencing this object any more */
246 if (e
->swap_filen
> -1) {
247 SD
= INDEXSD(e
->swap_dirn
);
248 if (SD
->unrefobj
!= NULL
)
251 /* Notify the memory cache that we're not referencing this object any more */
253 if (mem_policy
->Dereferenced
)
254 mem_policy
->Dereferenced(mem_policy
, e
, &e
->mem_obj
->repl
);
259 storeLockObject(StoreEntry
* e
)
262 debug(20, 3) ("storeLockObject: key '%s' count=%d\n",
263 storeKeyText(e
->hash
.key
), (int) e
->lock_count
);
264 e
->lastref
= squid_curtime
;
265 storeEntryReferenced(e
);
269 storeReleaseRequest(StoreEntry
* e
)
271 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
))
273 debug(20, 3) ("storeReleaseRequest: '%s'\n", storeKeyText(e
->hash
.key
));
274 EBIT_SET(e
->flags
, RELEASE_REQUEST
);
276 * Clear cachable flag here because we might get called before
277 * anyone else even looks at the cachability flag. Also, this
278 * prevents httpMakePublic from really setting a public key.
280 EBIT_CLR(e
->flags
, ENTRY_CACHABLE
);
281 storeSetPrivateKey(e
);
284 /* unlock object, return -1 if object get released after unlock
285 * otherwise lock_count */
287 storeUnlockObject(StoreEntry
* e
)
290 debug(20, 3) ("storeUnlockObject: key '%s' count=%d\n",
291 storeKeyText(e
->hash
.key
), e
->lock_count
);
293 return (int) e
->lock_count
;
294 if (e
->store_status
== STORE_PENDING
)
295 EBIT_SET(e
->flags
, RELEASE_REQUEST
);
296 assert(storePendingNClients(e
) == 0);
297 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
))
299 else if (storeKeepInMemory(e
)) {
300 storeEntryDereferenced(e
);
301 storeSetMemStatus(e
, IN_MEMORY
);
302 requestUnlink(e
->mem_obj
->request
);
303 e
->mem_obj
->request
= NULL
;
306 storeEntryDereferenced(e
);
307 if (EBIT_TEST(e
->flags
, KEY_PRIVATE
))
308 debug(20, 1) ("WARNING: %s:%d: found KEY_PRIVATE\n", __FILE__
, __LINE__
);
313 /* Lookup an object in the cache.
314 * return just a reference to object, don't start swapping in yet. */
316 storeGet(const cache_key
* key
)
318 debug(20, 3) ("storeGet: looking up %s\n", storeKeyText(key
));
319 return (StoreEntry
*) hash_lookup(store_table
, key
);
323 storeGetPublic(const char *uri
, const method_t method
)
325 return storeGet(storeKeyPublic(uri
, method
));
331 static int key_counter
= 0;
332 if (++key_counter
< 0)
338 storeSetPrivateKey(StoreEntry
* e
)
340 const cache_key
*newkey
;
341 MemObject
*mem
= e
->mem_obj
;
342 if (e
->hash
.key
&& EBIT_TEST(e
->flags
, KEY_PRIVATE
))
343 return; /* is already private */
345 if (e
->swap_filen
> -1)
346 storeDirSwapLog(e
, SWAP_LOG_DEL
);
350 mem
->id
= getKeyCounter();
351 newkey
= storeKeyPrivate(mem
->url
, mem
->method
, mem
->id
);
353 newkey
= storeKeyPrivate("JUNK", METHOD_NONE
, getKeyCounter());
355 assert(hash_lookup(store_table
, newkey
) == NULL
);
356 EBIT_SET(e
->flags
, KEY_PRIVATE
);
357 storeHashInsert(e
, newkey
);
361 storeSetPublicKey(StoreEntry
* e
)
363 StoreEntry
*e2
= NULL
;
364 const cache_key
*newkey
;
365 MemObject
*mem
= e
->mem_obj
;
366 if (e
->hash
.key
&& !EBIT_TEST(e
->flags
, KEY_PRIVATE
))
367 return; /* is already public */
370 * We can't make RELEASE_REQUEST objects public. Depending on
371 * when RELEASE_REQUEST gets set, we might not be swapping out
372 * the object. If we're not swapping out, then subsequent
373 * store clients won't be able to access object data which has
374 * been freed from memory.
376 * If RELEASE_REQUEST is set, then ENTRY_CACHABLE should not
377 * be set, and storeSetPublicKey() should not be called.
379 #if MORE_DEBUG_OUTPUT
380 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
))
381 debug(20, 1) ("assertion failed: RELEASE key %s, url %s\n",
382 e
->hash
.key
, mem
->url
);
384 assert(!EBIT_TEST(e
->flags
, RELEASE_REQUEST
));
385 newkey
= storeKeyPublic(mem
->url
, mem
->method
);
386 if ((e2
= (StoreEntry
*) hash_lookup(store_table
, newkey
))) {
387 debug(20, 3) ("storeSetPublicKey: Making old '%s' private.\n", mem
->url
);
388 storeSetPrivateKey(e2
);
390 newkey
= storeKeyPublic(mem
->url
, mem
->method
);
394 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
395 storeHashInsert(e
, newkey
);
396 if (e
->swap_filen
> -1)
397 storeDirSwapLog(e
, SWAP_LOG_ADD
);
401 storeCreateEntry(const char *url
, const char *log_url
, request_flags flags
, method_t method
)
403 StoreEntry
*e
= NULL
;
404 MemObject
*mem
= NULL
;
405 debug(20, 3) ("storeCreateEntry: '%s'\n", url
);
407 e
= new_StoreEntry(STORE_ENTRY_WITH_MEMOBJ
, url
, log_url
);
408 e
->lock_count
= 1; /* Note lock here w/o calling storeLock() */
410 mem
->method
= method
;
411 if (neighbors_do_private_keys
|| !flags
.hierarchical
)
412 storeSetPrivateKey(e
);
414 storeSetPublicKey(e
);
415 if (flags
.cachable
) {
416 EBIT_SET(e
->flags
, ENTRY_CACHABLE
);
417 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
419 EBIT_CLR(e
->flags
, ENTRY_CACHABLE
);
420 storeReleaseRequest(e
);
422 e
->store_status
= STORE_PENDING
;
423 storeSetMemStatus(e
, NOT_IN_MEMORY
);
424 e
->swap_status
= SWAPOUT_NONE
;
428 e
->lastref
= squid_curtime
;
429 e
->timestamp
= -1; /* set in storeTimestampsSet() */
430 e
->ping_status
= PING_NONE
;
431 EBIT_SET(e
->flags
, ENTRY_VALIDATED
);
435 /* Mark object as expired */
437 storeExpireNow(StoreEntry
* e
)
439 debug(20, 3) ("storeExpireNow: '%s'\n", storeKeyText(e
->hash
.key
));
440 e
->expires
= squid_curtime
;
443 /* Append incoming data from a primary server to an entry. */
445 storeAppend(StoreEntry
* e
, const char *buf
, int len
)
447 MemObject
*mem
= e
->mem_obj
;
450 assert(e
->store_status
== STORE_PENDING
);
452 debug(20, 5) ("storeAppend: appending %d bytes for '%s'\n",
454 storeKeyText(e
->hash
.key
));
455 storeGetMemSpace(len
);
456 stmemAppend(&mem
->data_hdr
, buf
, len
);
457 mem
->inmem_hi
+= len
;
459 if (EBIT_TEST(e
->flags
, DELAY_SENDING
))
467 storeAppendPrintf(StoreEntry
* e
, const char *fmt
,...)
469 storeAppendPrintf(va_alist
)
478 StoreEntry
*e
= NULL
;
479 const char *fmt
= NULL
;
481 e
= va_arg(args
, StoreEntry
*);
482 fmt
= va_arg(args
, char *);
484 storeAppendVPrintf(e
, fmt
, args
);
488 /* used be storeAppendPrintf and Packer */
490 storeAppendVPrintf(StoreEntry
* e
, const char *fmt
, va_list vargs
)
492 LOCAL_ARRAY(char, buf
, 4096);
494 vsnprintf(buf
, 4096, fmt
, vargs
);
495 storeAppend(e
, buf
, strlen(buf
));
498 struct _store_check_cachable_hist
{
501 int not_entry_cachable
;
503 int wrong_content_length
;
508 int too_many_open_files
;
509 int too_many_open_fds
;
514 } store_check_cachable_hist
;
517 storeTooManyDiskFilesOpen(void)
519 if (Config
.max_open_disk_fds
== 0)
521 if (store_open_disk_fd
> Config
.max_open_disk_fds
)
527 storeCheckTooSmall(StoreEntry
* e
)
529 MemObject
*mem
= e
->mem_obj
;
530 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
532 if (STORE_OK
== e
->store_status
)
533 if (mem
->object_sz
< Config
.Store
.minObjectSize
)
535 if (mem
->reply
->content_length
> -1)
536 if (mem
->reply
->content_length
< (int) Config
.Store
.minObjectSize
)
542 storeCheckCachable(StoreEntry
* e
)
544 #if CACHE_ALL_METHODS
545 if (e
->mem_obj
->method
!= METHOD_GET
) {
546 debug(20, 2) ("storeCheckCachable: NO: non-GET method\n");
547 store_check_cachable_hist
.no
.non_get
++;
550 if (!EBIT_TEST(e
->flags
, ENTRY_CACHABLE
)) {
551 debug(20, 2) ("storeCheckCachable: NO: not cachable\n");
552 store_check_cachable_hist
.no
.not_entry_cachable
++;
553 } else if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
)) {
554 debug(20, 2) ("storeCheckCachable: NO: release requested\n");
555 store_check_cachable_hist
.no
.release_request
++;
556 } else if (e
->store_status
== STORE_OK
&& EBIT_TEST(e
->flags
, ENTRY_BAD_LENGTH
)) {
557 debug(20, 2) ("storeCheckCachable: NO: wrong content-length\n");
558 store_check_cachable_hist
.no
.wrong_content_length
++;
559 } else if (EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
)) {
560 debug(20, 3) ("storeCheckCachable: NO: negative cached\n");
561 store_check_cachable_hist
.no
.negative_cached
++;
562 return 0; /* avoid release call below */
563 } else if ((e
->mem_obj
->reply
->content_length
> 0 &&
564 e
->mem_obj
->reply
->content_length
> Config
.Store
.maxObjectSize
) ||
565 e
->mem_obj
->inmem_hi
> Config
.Store
.maxObjectSize
) {
566 debug(20, 2) ("storeCheckCachable: NO: too big\n");
567 store_check_cachable_hist
.no
.too_big
++;
568 } else if (e
->mem_obj
->reply
->content_length
> (int) Config
.Store
.maxObjectSize
) {
569 debug(20, 2) ("storeCheckCachable: NO: too big\n");
570 store_check_cachable_hist
.no
.too_big
++;
571 } else if (storeCheckTooSmall(e
)) {
572 debug(20, 2) ("storeCheckCachable: NO: too small\n");
573 store_check_cachable_hist
.no
.too_small
++;
574 } else if (EBIT_TEST(e
->flags
, KEY_PRIVATE
)) {
575 debug(20, 3) ("storeCheckCachable: NO: private key\n");
576 store_check_cachable_hist
.no
.private_key
++;
577 } else if (e
->swap_status
!= SWAPOUT_NONE
) {
579 * here we checked the swap_status because the remaining
580 * cases are only relevant only if we haven't started swapping
581 * out the object yet.
584 } else if (storeTooManyDiskFilesOpen()) {
585 debug(20, 2) ("storeCheckCachable: NO: too many disk files open\n");
586 store_check_cachable_hist
.no
.too_many_open_files
++;
587 } else if (fdNFree() < RESERVED_FD
) {
588 debug(20, 2) ("storeCheckCachable: NO: too many FD's open\n");
589 store_check_cachable_hist
.no
.too_many_open_fds
++;
591 store_check_cachable_hist
.yes
.Default
++;
594 storeReleaseRequest(e
);
595 EBIT_CLR(e
->flags
, ENTRY_CACHABLE
);
600 storeCheckCachableStats(StoreEntry
* sentry
)
602 storeAppendPrintf(sentry
, "Category\t Count\n");
604 storeAppendPrintf(sentry
, "no.non_get\t%d\n",
605 store_check_cachable_hist
.no
.non_get
);
606 storeAppendPrintf(sentry
, "no.not_entry_cachable\t%d\n",
607 store_check_cachable_hist
.no
.not_entry_cachable
);
608 storeAppendPrintf(sentry
, "no.release_request\t%d\n",
609 store_check_cachable_hist
.no
.release_request
);
610 storeAppendPrintf(sentry
, "no.wrong_content_length\t%d\n",
611 store_check_cachable_hist
.no
.wrong_content_length
);
612 storeAppendPrintf(sentry
, "no.negative_cached\t%d\n",
613 store_check_cachable_hist
.no
.negative_cached
);
614 storeAppendPrintf(sentry
, "no.too_big\t%d\n",
615 store_check_cachable_hist
.no
.too_big
);
616 storeAppendPrintf(sentry
, "no.too_small\t%d\n",
617 store_check_cachable_hist
.no
.too_small
);
618 storeAppendPrintf(sentry
, "no.private_key\t%d\n",
619 store_check_cachable_hist
.no
.private_key
);
620 storeAppendPrintf(sentry
, "no.too_many_open_files\t%d\n",
621 store_check_cachable_hist
.no
.too_many_open_files
);
622 storeAppendPrintf(sentry
, "no.too_many_open_fds\t%d\n",
623 store_check_cachable_hist
.no
.too_many_open_fds
);
624 storeAppendPrintf(sentry
, "yes.default\t%d\n",
625 store_check_cachable_hist
.yes
.Default
);
628 /* Complete transfer into the local cache. */
630 storeComplete(StoreEntry
* e
)
632 debug(20, 3) ("storeComplete: '%s'\n", storeKeyText(e
->hash
.key
));
633 if (e
->store_status
!= STORE_PENDING
) {
635 * if we're not STORE_PENDING, then probably we got aborted
636 * and there should be NO clients on this entry
638 assert(EBIT_TEST(e
->flags
, ENTRY_ABORTED
));
639 assert(e
->mem_obj
->nclients
== 0);
642 e
->mem_obj
->object_sz
= e
->mem_obj
->inmem_hi
;
643 e
->store_status
= STORE_OK
;
644 assert(e
->mem_status
== NOT_IN_MEMORY
);
645 if (!storeEntryValidLength(e
)) {
646 EBIT_SET(e
->flags
, ENTRY_BAD_LENGTH
);
647 storeReleaseRequest(e
);
649 #if USE_CACHE_DIGESTS
650 if (e
->mem_obj
->request
)
651 e
->mem_obj
->request
->hier
.store_complete_stop
= current_time
;
654 * We used to call InvokeHandlers, then storeSwapOut. However,
655 * Madhukar Reddy <myreddy@persistence.com> reported that
656 * responses without content length would sometimes get released
657 * in client_side, thinking that the response is incomplete.
664 * Someone wants to abort this transfer. Set the reason in the
665 * request structure, call the server-side callback and mark the
666 * entry for releasing
669 storeAbort(StoreEntry
* e
)
671 MemObject
*mem
= e
->mem_obj
;
672 assert(e
->store_status
== STORE_PENDING
);
674 debug(20, 6) ("storeAbort: %s\n", storeKeyText(e
->hash
.key
));
675 storeLockObject(e
); /* lock while aborting */
676 storeNegativeCache(e
);
677 storeReleaseRequest(e
);
678 EBIT_SET(e
->flags
, ENTRY_ABORTED
);
679 storeSetMemStatus(e
, NOT_IN_MEMORY
);
680 e
->store_status
= STORE_OK
;
682 * We assign an object length here. The only other place we assign
683 * the object length is in storeComplete()
685 mem
->object_sz
= mem
->inmem_hi
;
686 /* Notify the server side */
687 if (mem
->abort
.callback
) {
688 eventAdd("mem->abort.callback",
693 mem
->abort
.callback
= NULL
;
694 mem
->abort
.data
= NULL
;
696 /* Notify the client side */
698 /* Close any swapout file */
699 storeSwapOutFileClose(e
);
700 storeUnlockObject(e
); /* unlock */
703 /* Clear Memory storage to accommodate the given object len */
705 storeGetMemSpace(int size
)
707 StoreEntry
*e
= NULL
;
709 static time_t last_check
= 0;
711 RemovalPurgeWalker
*walker
;
712 if (squid_curtime
== last_check
)
714 last_check
= squid_curtime
;
715 pages_needed
= (size
/ SM_PAGE_SIZE
) + 1;
716 if (memInUse(MEM_STMEM_BUF
) + pages_needed
< store_pages_max
)
718 debug(20, 2) ("storeGetMemSpace: Starting, need %d pages\n", pages_needed
);
719 /* XXX what to set as max_scan here? */
720 walker
= mem_policy
->PurgeInit(mem_policy
, 100000);
721 while ((e
= walker
->Next(walker
))) {
724 if (memInUse(MEM_STMEM_BUF
) + pages_needed
< store_pages_max
)
727 walker
->Done(walker
);
728 debug(20, 3) ("storeGetMemSpace stats:\n");
729 debug(20, 3) (" %6d HOT objects\n", hot_obj_count
);
730 debug(20, 3) (" %6d were released\n", released
);
733 /* The maximum objects to scan for maintain storage space */
734 #define MAINTAIN_MAX_SCAN 1024
735 #define MAINTAIN_MAX_REMOVE 64
738 * This routine is to be called by main loop in main.c.
739 * It removes expired objects on only one bucket for each time called.
740 * returns the number of objects removed
742 * This should get called 1/s from main().
745 storeMaintainSwapSpace(void *datanotused
)
749 static time_t last_warn_time
= 0;
752 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
753 /* call the maintain function .. */
755 /* XXX FixMe: This should be done "in parallell" on the different
756 * cache_dirs, not one at a time.
758 if (SD
->maintainfs
!= NULL
)
761 if (store_swap_size
> Config
.Swap
.maxSize
) {
762 if (squid_curtime
- last_warn_time
> 10) {
763 debug(20, 0) ("WARNING: Disk space over limit: %d KB > %d KB\n",
764 store_swap_size
, Config
.Swap
.maxSize
);
765 last_warn_time
= squid_curtime
;
768 /* Reregister a maintain event .. */
769 eventAdd("MaintainSwapSpace", storeMaintainSwapSpace
, NULL
, 1.0, 1);
773 /* release an object from a cache */
775 storeRelease(StoreEntry
* e
)
777 debug(20, 3) ("storeRelease: Releasing: '%s'\n", storeKeyText(e
->hash
.key
));
778 /* If, for any reason we can't discard this object because of an
779 * outstanding request, mark it for pending release */
780 if (storeEntryLocked(e
)) {
782 debug(20, 3) ("storeRelease: Only setting RELEASE_REQUEST bit\n");
783 storeReleaseRequest(e
);
786 if (store_dirs_rebuilding
&& e
->swap_filen
> -1) {
787 storeSetPrivateKey(e
);
789 storeSetMemStatus(e
, NOT_IN_MEMORY
);
790 destroy_MemObject(e
);
792 if (e
->swap_filen
> -1) {
794 * Fake a call to storeLockObject(). When rebuilding is done,
795 * we'll just call storeUnlockObject() on these.
798 EBIT_SET(e
->flags
, RELEASE_REQUEST
);
799 stackPush(&LateReleaseStack
, e
);
802 destroy_StoreEntry(e
);
805 storeLog(STORE_LOG_RELEASE
, e
);
806 if (e
->swap_filen
> -1) {
808 if (e
->swap_status
== SWAPOUT_DONE
)
809 if (EBIT_TEST(e
->flags
, ENTRY_VALIDATED
))
810 storeDirUpdateSwapSize(&Config
.cacheSwap
.swapDirs
[e
->swap_dirn
], e
->swap_file_sz
, -1);
811 if (!EBIT_TEST(e
->flags
, KEY_PRIVATE
))
812 storeDirSwapLog(e
, SWAP_LOG_DEL
);
814 /* From 2.4. I think we do this in storeUnlink? */
815 storeSwapFileNumberSet(e
, -1);
818 storeSetMemStatus(e
, NOT_IN_MEMORY
);
819 destroy_StoreEntry(e
);
823 storeLateRelease(void *unused
)
828 if (store_dirs_rebuilding
) {
829 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
832 for (i
= 0; i
< 10; i
++) {
833 e
= stackPop(&LateReleaseStack
);
836 debug(20, 1) ("storeLateRelease: released %d objects\n", n
);
839 storeUnlockObject(e
);
842 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 0.0, 1);
845 /* return 1 if a store entry is locked */
847 storeEntryLocked(const StoreEntry
* e
)
851 if (e
->swap_status
== SWAPOUT_WRITING
)
853 if (e
->store_status
== STORE_PENDING
)
856 * SPECIAL, PUBLIC entries should be "locked"
858 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
859 if (!EBIT_TEST(e
->flags
, KEY_PRIVATE
))
865 storeEntryValidLength(const StoreEntry
* e
)
868 const HttpReply
*reply
;
869 assert(e
->mem_obj
!= NULL
);
870 reply
= e
->mem_obj
->reply
;
871 debug(20, 3) ("storeEntryValidLength: Checking '%s'\n", storeKeyText(e
->hash
.key
));
872 debug(20, 5) ("storeEntryValidLength: object_len = %d\n",
874 debug(20, 5) ("storeEntryValidLength: hdr_sz = %d\n",
876 debug(20, 5) ("storeEntryValidLength: content_length = %d\n",
877 reply
->content_length
);
878 if (reply
->content_length
< 0) {
879 debug(20, 5) ("storeEntryValidLength: Unspecified content length: %s\n",
880 storeKeyText(e
->hash
.key
));
883 if (reply
->hdr_sz
== 0) {
884 debug(20, 5) ("storeEntryValidLength: Zero header size: %s\n",
885 storeKeyText(e
->hash
.key
));
888 if (e
->mem_obj
->method
== METHOD_HEAD
) {
889 debug(20, 5) ("storeEntryValidLength: HEAD request: %s\n",
890 storeKeyText(e
->hash
.key
));
893 if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
895 if (reply
->sline
.status
== HTTP_NO_CONTENT
)
897 diff
= reply
->hdr_sz
+ reply
->content_length
- objectLen(e
);
900 debug(20, 3) ("storeEntryValidLength: %d bytes too %s; '%s'\n",
901 diff
< 0 ? -diff
: diff
,
902 diff
< 0 ? "big" : "small",
903 storeKeyText(e
->hash
.key
));
908 storeInitHashValues(void)
911 /* Calculate size of hash table (maximum currently 64k buckets). */
912 i
= Config
.Swap
.maxSize
/ Config
.Store
.avgObjectSize
;
913 debug(20, 1) ("Swap maxSize %d KB, estimated %d objects\n",
914 Config
.Swap
.maxSize
, i
);
915 i
/= Config
.Store
.objectsPerBucket
;
916 debug(20, 1) ("Target number of buckets: %d\n", i
);
917 /* ideally the full scan period should be configurable, for the
918 * moment it remains at approximately 24 hours. */
919 store_hash_buckets
= storeKeyHashBuckets(i
);
920 debug(20, 1) ("Using %d Store buckets\n", store_hash_buckets
);
921 debug(20, 1) ("Max Mem size: %d KB\n", Config
.memMaxSize
>> 10);
922 debug(20, 1) ("Max Swap size: %d KB\n", Config
.Swap
.maxSize
);
929 storeInitHashValues();
930 store_table
= hash_create(storeKeyHashCmp
,
931 store_hash_buckets
, storeKeyHashHash
);
932 mem_policy
= createRemovalPolicy(Config
.memPolicy
);
935 stackInit(&LateReleaseStack
);
936 eventAdd("storeLateRelease", storeLateRelease
, NULL
, 1.0, 1);
939 cachemgrRegister("storedir",
940 "Store Directory Stats",
941 storeDirStats
, 0, 1);
942 cachemgrRegister("store_check_cachable_stats",
943 "storeCheckCachable() Stats",
944 storeCheckCachableStats
, 0, 1);
945 cachemgrRegister("store_io",
946 "Store IO Interface Stats",
953 store_swap_high
= (long) (((float) Config
.Swap
.maxSize
*
954 (float) Config
.Swap
.highWaterMark
) / (float) 100);
955 store_swap_low
= (long) (((float) Config
.Swap
.maxSize
*
956 (float) Config
.Swap
.lowWaterMark
) / (float) 100);
957 store_pages_max
= Config
.memMaxSize
/ SM_PAGE_SIZE
;
961 storeKeepInMemory(const StoreEntry
* e
)
963 MemObject
*mem
= e
->mem_obj
;
966 if (mem
->data_hdr
.head
== NULL
)
968 return mem
->inmem_lo
== 0;
972 storeNegativeCache(StoreEntry
* e
)
974 e
->expires
= squid_curtime
+ Config
.negativeTtl
;
975 EBIT_SET(e
->flags
, ENTRY_NEGCACHED
);
979 storeFreeMemory(void)
981 hashFreeItems(store_table
, destroy_StoreEntry
);
982 hashFreeMemory(store_table
);
984 #if USE_CACHE_DIGESTS
986 cacheDigestDestroy(store_digest
);
992 expiresMoreThan(time_t expires
, time_t when
)
994 if (expires
< 0) /* No Expires given */
996 return (expires
> (squid_curtime
+ when
));
1000 storeEntryValidToSend(StoreEntry
* e
)
1002 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
))
1004 if (EBIT_TEST(e
->flags
, ENTRY_NEGCACHED
))
1005 if (e
->expires
<= squid_curtime
)
1007 if (EBIT_TEST(e
->flags
, ENTRY_ABORTED
))
1013 storeTimestampsSet(StoreEntry
* entry
)
1015 const HttpReply
*reply
= entry
->mem_obj
->reply
;
1016 time_t served_date
= reply
->date
;
1017 int age
= httpHeaderGetInt(&reply
->header
, HDR_AGE
);
1019 * The timestamp calculations below tries to mimic the properties
1020 * of the age calculation in RFC2616 section 13.2.3. The implementaion
1021 * isn't complete, and the most notable exception from the RFC is that
1022 * this does not account for response_delay, but it probably does
1023 * not matter much as this is calculated immediately when the headers
1024 * are received, not when the whole response has been received.
1026 /* make sure that 0 <= served_date <= squid_curtime */
1027 if (served_date
< 0 || served_date
> squid_curtime
)
1028 served_date
= squid_curtime
;
1030 * Compensate with Age header if origin server clock is ahead
1031 * of us and there is a cache in between us and the origin
1032 * server. But DONT compensate if the age value is larger than
1033 * squid_curtime because it results in a negative served_date.
1035 if (age
> squid_curtime
- served_date
)
1036 if (squid_curtime
< age
)
1037 served_date
= squid_curtime
- age
;
1038 entry
->expires
= reply
->expires
;
1039 entry
->lastmod
= reply
->last_modified
;
1040 entry
->timestamp
= served_date
;
1044 storeRegisterAbort(StoreEntry
* e
, STABH
* cb
, void *data
)
1046 MemObject
*mem
= e
->mem_obj
;
1048 assert(mem
->abort
.callback
== NULL
);
1049 mem
->abort
.callback
= cb
;
1050 mem
->abort
.data
= data
;
1054 storeUnregisterAbort(StoreEntry
* e
)
1056 MemObject
*mem
= e
->mem_obj
;
1058 mem
->abort
.callback
= NULL
;
1062 storeMemObjectDump(MemObject
* mem
)
1064 debug(20, 1) ("MemObject->data.head: %p\n",
1065 mem
->data_hdr
.head
);
1066 debug(20, 1) ("MemObject->data.tail: %p\n",
1067 mem
->data_hdr
.tail
);
1068 debug(20, 1) ("MemObject->data.origin_offset: %d\n",
1069 mem
->data_hdr
.origin_offset
);
1070 debug(20, 1) ("MemObject->start_ping: %d.%06d\n",
1071 (int) mem
->start_ping
.tv_sec
,
1072 (int) mem
->start_ping
.tv_usec
);
1073 debug(20, 1) ("MemObject->inmem_hi: %d\n",
1074 (int) mem
->inmem_hi
);
1075 debug(20, 1) ("MemObject->inmem_lo: %d\n",
1076 (int) mem
->inmem_lo
);
1077 debug(20, 1) ("MemObject->clients: %p\n",
1079 debug(20, 1) ("MemObject->nclients: %d\n",
1081 debug(20, 1) ("MemObject->reply: %p\n",
1083 debug(20, 1) ("MemObject->request: %p\n",
1085 debug(20, 1) ("MemObject->log_url: %p %s\n",
1087 checkNullString(mem
->log_url
));
1091 storeEntryDump(const StoreEntry
* e
, int l
)
1093 debug(20, l
) ("StoreEntry->key: %s\n", storeKeyText(e
->hash
.key
));
1094 debug(20, l
) ("StoreEntry->next: %p\n", e
->hash
.next
);
1095 debug(20, l
) ("StoreEntry->mem_obj: %p\n", e
->mem_obj
);
1096 debug(20, l
) ("StoreEntry->timestamp: %d\n", (int) e
->timestamp
);
1097 debug(20, l
) ("StoreEntry->lastref: %d\n", (int) e
->lastref
);
1098 debug(20, l
) ("StoreEntry->expires: %d\n", (int) e
->expires
);
1099 debug(20, l
) ("StoreEntry->lastmod: %d\n", (int) e
->lastmod
);
1100 debug(20, l
) ("StoreEntry->swap_file_sz: %d\n", (int) e
->swap_file_sz
);
1101 debug(20, l
) ("StoreEntry->refcount: %d\n", e
->refcount
);
1102 debug(20, l
) ("StoreEntry->flags: %s\n", storeEntryFlags(e
));
1103 debug(20, l
) ("StoreEntry->swap_dirn: %d\n", (int) e
->swap_dirn
);
1104 debug(20, l
) ("StoreEntry->swap_filen: %d\n", (int) e
->swap_filen
);
1105 debug(20, l
) ("StoreEntry->lock_count: %d\n", (int) e
->lock_count
);
1106 debug(20, l
) ("StoreEntry->mem_status: %d\n", (int) e
->mem_status
);
1107 debug(20, l
) ("StoreEntry->ping_status: %d\n", (int) e
->ping_status
);
1108 debug(20, l
) ("StoreEntry->store_status: %d\n", (int) e
->store_status
);
1109 debug(20, l
) ("StoreEntry->swap_status: %d\n", (int) e
->swap_status
);
1113 * NOTE, this function assumes only two mem states
1116 storeSetMemStatus(StoreEntry
* e
, int new_status
)
1118 MemObject
*mem
= e
->mem_obj
;
1119 if (new_status
== e
->mem_status
)
1121 assert(mem
!= NULL
);
1122 if (new_status
== IN_MEMORY
) {
1123 assert(mem
->inmem_lo
== 0);
1124 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1125 debug(20, 4) ("storeSetMemStatus: not inserting special %s into policy\n",
1128 mem_policy
->Add(mem_policy
, e
, &mem
->repl
);
1129 debug(20, 4) ("storeSetMemStatus: inserted mem node %s\n",
1134 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1135 debug(20, 4) ("storeSetMemStatus: special entry %s\n",
1138 mem_policy
->Remove(mem_policy
, e
, &mem
->repl
);
1139 debug(20, 4) ("storeSetMemStatus: removed mem node %s\n",
1144 e
->mem_status
= new_status
;
1148 storeUrl(const StoreEntry
* e
)
1151 return "[null_entry]";
1152 else if (e
->mem_obj
== NULL
)
1153 return "[null_mem_obj]";
1155 return e
->mem_obj
->url
;
1159 storeCreateMemObject(StoreEntry
* e
, const char *url
, const char *log_url
)
1163 e
->mem_obj
= new_MemObject(url
, log_url
);
1166 /* this just sets DELAY_SENDING */
1168 storeBuffer(StoreEntry
* e
)
1170 EBIT_SET(e
->flags
, DELAY_SENDING
);
1173 /* this just clears DELAY_SENDING and Invokes the handlers */
1175 storeBufferFlush(StoreEntry
* e
)
1177 EBIT_CLR(e
->flags
, DELAY_SENDING
);
1183 objectLen(const StoreEntry
* e
)
1185 assert(e
->mem_obj
!= NULL
);
1186 return e
->mem_obj
->object_sz
;
1190 contentLen(const StoreEntry
* e
)
1192 assert(e
->mem_obj
!= NULL
);
1193 assert(e
->mem_obj
->reply
!= NULL
);
1194 return e
->mem_obj
->object_sz
- e
->mem_obj
->reply
->hdr_sz
;
1198 storeEntryReply(StoreEntry
* e
)
1202 if (NULL
== e
->mem_obj
)
1204 return e
->mem_obj
->reply
;
1208 storeEntryReset(StoreEntry
* e
)
1210 MemObject
*mem
= e
->mem_obj
;
1211 debug(20, 3) ("storeEntryReset: %s\n", storeUrl(e
));
1212 assert(mem
->swapout
.sio
== NULL
);
1213 stmemFree(&mem
->data_hdr
);
1214 mem
->inmem_hi
= mem
->inmem_lo
= 0;
1215 httpReplyDestroy(mem
->reply
);
1216 mem
->reply
= httpReplyCreate();
1217 e
->expires
= e
->lastmod
= e
->timestamp
= -1;
1223 * This routine calls the SETUP routine for each fs type.
1224 * I don't know where the best place for this is, and I'm not going to shuffle
1225 * around large chunks of code right now (that can be done once its working.)
1236 * similar to above, but is called when a graceful shutdown is to occur
1237 * of each fs module.
1244 while (storefs_list
[i
].typestr
!= NULL
) {
1245 storefs_list
[i
].donefunc();
1251 * called to add another store fs module
1254 storeFsAdd(char *type
, STSETUP
* setup
)
1257 /* find the number of currently known storefs types */
1258 for (i
= 0; storefs_list
&& storefs_list
[i
].typestr
; i
++) {
1259 assert(strcmp(storefs_list
[i
].typestr
, type
) != 0);
1261 /* add the new type */
1262 storefs_list
= xrealloc(storefs_list
, (i
+ 2) * sizeof(storefs_entry_t
));
1263 memset(&storefs_list
[i
+ 1], 0, sizeof(storefs_entry_t
));
1264 storefs_list
[i
].typestr
= type
;
1265 /* Call the FS to set up capabilities and initialize the FS driver */
1266 setup(&storefs_list
[i
]);
1270 * called to add another store removal policy module
1273 storeReplAdd(char *type
, REMOVALPOLICYCREATE
* create
)
1276 /* find the number of currently known repl types */
1277 for (i
= 0; storerepl_list
&& storerepl_list
[i
].typestr
; i
++) {
1278 assert(strcmp(storerepl_list
[i
].typestr
, type
) != 0);
1280 /* add the new type */
1281 storerepl_list
= xrealloc(storerepl_list
, (i
+ 2) * sizeof(storerepl_entry_t
));
1282 memset(&storerepl_list
[i
+ 1], 0, sizeof(storerepl_entry_t
));
1283 storerepl_list
[i
].typestr
= type
;
1284 storerepl_list
[i
].create
= create
;
1288 * Create a removal policy instance
1291 createRemovalPolicy(RemovalPolicySettings
* settings
)
1293 storerepl_entry_t
*r
;
1294 for (r
= storerepl_list
; r
&& r
->typestr
; r
++) {
1295 if (strcmp(r
->typestr
, settings
->type
) == 0)
1296 return r
->create(settings
->args
);
1298 debug(20, 1) ("ERROR: Unknown policy %s\n", settings
->type
);
1299 debug(20, 1) ("ERROR: Be sure to have set cache_replacement_policy\n");
1300 debug(20, 1) ("ERROR: and memory_replacement_policy in squid.conf!\n");
1301 fatalf("ERROR: Unknown policy %s\n", settings
->type
);
1302 return NULL
; /* NOTREACHED */
1307 storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
)
1309 if (e
->swap_file_number
== filn
)
1313 storeDirMapBitReset(e
->swap_file_number
);
1314 storeDirLRUDelete(e
);
1315 e
->swap_file_number
= -1;
1317 assert(-1 == e
->swap_file_number
);
1318 storeDirMapBitSet(e
->swap_file_number
= filn
);