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