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