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