]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_client.cc
Prep for 3.3.12 and 3.4.4
[thirdparty/squid.git] / src / store_client.cc
CommitLineData
9cef6668 1
2/*
2f44bd34 3 * DEBUG: section 90 Storage Manager Client-Side Interface
9cef6668 4 * AUTHOR: Duane Wessels
5 *
2b6662ba 6 * SQUID Web Proxy Cache http://www.squid-cache.org/
9cef6668 7 * ----------------------------------------------------------
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.
9cef6668 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 *
9cef6668 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 *
9cef6668 28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
2f44bd34 32 * Portions copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
9cef6668 33 */
34
582c2af2 35#include "squid.h"
a553a5a3 36#include "event.h"
22bbd840 37#include "globals.h"
528b2c61 38#include "HttpReply.h"
582c2af2
FC
39#include "HttpRequest.h"
40#include "MemBuf.h"
528b2c61 41#include "MemObject.h"
b6149797 42#include "mime_header.h"
582c2af2 43#include "profiler/Profiler.h"
4d5904f7 44#include "SquidConfig.h"
e4f1fdae 45#include "StatCounters.h"
582c2af2 46#include "Store.h"
f82b5c64 47#include "store_swapin.h"
602d9612 48#include "StoreClient.h"
528b2c61 49#include "StoreMeta.h"
50#include "StoreMetaUnpacker.h"
9a0a18de 51#if USE_DELAY_POOLS
b67e2c8c 52#include "DelayPools.h"
53#endif
c8be6d7b 54
e3ef2b09 55/*
56 * NOTE: 'Header' refers to the swapfile metadata header.
528b2c61 57 * 'OBJHeader' refers to the object header, with cannonical
58 * processed object headers (which may derive from FTP/HTTP etc
59 * upstream protocols
e3ef2b09 60 * 'Body' refers to the swapfile body, which is the full
61 * HTTP reply (including HTTP headers and body).
62 */
4fcc8876 63static StoreIOState::STRCB storeClientReadBody;
64static StoreIOState::STRCB storeClientReadHeader;
f09f5b26 65static void storeClientCopy2(StoreEntry * e, store_client * sc);
d6f51e3c 66static EVH storeClientCopyEvent;
ae6568e7 67static bool CheckQuickAbortIsReasonable(StoreEntry * entry);
77b32a34 68static void CheckQuickAbort(StoreEntry * entry);
f09f5b26 69
b001e822 70CBDATA_CLASS_INIT(store_client);
528b2c61 71
528b2c61 72bool
47f6e231 73store_client::memReaderHasLowerOffset(int64_t anOffset) const
528b2c61 74{
75 return getType() == STORE_MEM_CLIENT && copyInto.offset < anOffset;
76}
77
78int
79store_client::getType() const
80{
81 return type;
82}
83
06d2839d 84#if STORE_CLIENT_LIST_DEBUG
fa80a8ef 85static store_client *
f09f5b26 86storeClientListSearch(const MemObject * mem, void *data)
87{
06d2839d 88 dlink_node *node;
89 store_client *sc = NULL;
62e76326 90
06d2839d 91 for (node = mem->clients.head; node; node = node->next) {
62e76326 92 sc = node->data;
93
94 if (sc->owner == data)
95 return sc;
f09f5b26 96 }
62e76326 97
06d2839d 98 return NULL;
f09f5b26 99}
c8be6d7b 100
101int
102storeClientIsThisAClient(store_client * sc, void *someClient)
103{
104 return sc->owner == someClient;
105}
62e76326 106
06d2839d 107#endif
924f73bc 108#include "HttpRequest.h"
f09f5b26 109
110/* add client with fd to client list */
06d2839d 111store_client *
f09f5b26 112storeClientListAdd(StoreEntry * e, void *data)
113{
114 MemObject *mem = e->mem_obj;
f09f5b26 115 store_client *sc;
116 assert(mem);
06d2839d 117#if STORE_CLIENT_LIST_DEBUG
62e76326 118
f09f5b26 119 if (storeClientListSearch(mem, data) != NULL)
62e76326 120 /* XXX die! */
121 assert(1 == 0);
122
6b8e7481 123#endif
62e76326 124
528b2c61 125 sc = new store_client (e);
62e76326 126
528b2c61 127 mem->addClient(sc);
62e76326 128
06d2839d 129 return sc;
f09f5b26 130}
131
528b2c61 132void
133store_client::callback(ssize_t sz, bool error)
b04e66e0 134{
63326655 135 size_t bSz = 0;
62e76326 136
63326655
AJ
137 if (sz >= 0 && !error)
138 bSz = sz;
139
140 StoreIOBuffer result(bSz, 0 ,copyInto.data);
141
142 if (sz < 0 || error)
62e76326 143 result.flags.error = 1;
62e76326 144
528b2c61 145 result.offset = cmp_offset;
90703668 146 assert(_callback.pending());
63326655 147 cmp_offset = copyInto.offset + bSz;
528b2c61 148 STCB *temphandler = _callback.callback_handler;
149 void *cbdata = _callback.callback_data;
150 _callback = Callback(NULL, NULL);
151 copyInto.data = NULL;
62e76326 152
528b2c61 153 if (cbdataReferenceValid(cbdata))
62e76326 154 temphandler(cbdata, result);
155
528b2c61 156 cbdataReferenceDone(cbdata);
b04e66e0 157}
158
f115fadd 159static void
160storeClientCopyEvent(void *data)
161{
e6ccf245 162 store_client *sc = (store_client *)data;
bf8fe701 163 debugs(90, 3, "storeClientCopyEvent: Running");
528b2c61 164 assert (sc->flags.copy_event_pending);
3dd52a0b 165 sc->flags.copy_event_pending = false;
62e76326 166
90703668 167 if (!sc->_callback.pending())
62e76326 168 return;
169
f115fadd 170 storeClientCopy2(sc->entry, sc);
f115fadd 171}
172
b67e2c8c 173store_client::store_client(StoreEntry *e) : entry (e)
9a0a18de 174#if USE_DELAY_POOLS
62e76326 175 , delayId()
b67e2c8c 176#endif
62e76326 177 , type (e->storeClientType())
178 , object_ok(true)
528b2c61 179{
180 cmp_offset = 0;
3dd52a0b 181 flags.disk_io_pending = false;
5db6bf73 182 ++ entry->refcount;
62e76326 183
528b2c61 184 if (getType() == STORE_DISK_CLIENT)
62e76326 185 /* assert we'll be able to get the data we want */
02a2d80b 186 /* maybe we should open swapin_sio here */
5b55f1f1 187 assert(entry->swap_filen > -1 || entry->swappingOut());
62e76326 188
528b2c61 189#if STORE_CLIENT_LIST_DEBUG
62e76326 190
528b2c61 191 owner = cbdataReference(data);
62e76326 192
528b2c61 193#endif
194}
195
2f44bd34 196store_client::~store_client()
62e76326 197{}
2f44bd34 198
f09f5b26 199/* copy bytes requested by the client */
200void
a4b8110e 201storeClientCopy(store_client * sc,
62e76326 202 StoreEntry * e,
203 StoreIOBuffer copyInto,
204 STCB * callback,
205 void *data)
f09f5b26 206{
528b2c61 207 assert (sc != NULL);
208 sc->copy(e, copyInto,callback,data);
209}
210
211void
212store_client::copy(StoreEntry * anEntry,
62e76326 213 StoreIOBuffer copyRequest,
214 STCB * callback_fn,
215 void *data)
528b2c61 216{
217 assert (anEntry == entry);
218 assert (callback_fn);
219 assert (data);
220 assert(!EBIT_TEST(entry->flags, ENTRY_ABORTED));
bf8fe701 221 debugs(90, 3, "store_client::copy: " << entry->getMD5Text() << ", from " <<
47f6e231 222 copyRequest.offset << ", for length " <<
bf8fe701 223 (int) copyRequest.length << ", cb " << callback_fn << ", cbdata " <<
224 data);
225
06d2839d 226#if STORE_CLIENT_LIST_DEBUG
62e76326 227
528b2c61 228 assert(this == storeClientListSearch(entry->mem_obj, data));
06d2839d 229#endif
62e76326 230
90703668 231 assert(!_callback.pending());
528b2c61 232#if ONLYCONTIGUOUSREQUESTS
62e76326 233
528b2c61 234 assert(cmp_offset == copyRequest.offset);
235#endif
236 /* range requests will skip into the body */
237 cmp_offset = copyRequest.offset;
238 _callback = Callback (callback_fn, cbdataReference(data));
239 copyInto.data = copyRequest.data;
240 copyInto.length = copyRequest.length;
241 copyInto.offset = copyRequest.offset;
242
1d5161bd 243 static bool copying (false);
244 assert (!copying);
245 copying = true;
246 PROF_start(storeClient_kickReads);
a46d2c0e 247 /* we might be blocking comm reads due to readahead limits
248 * now we have a new offset, trigger those reads...
249 */
250 entry->mem_obj->kickReads();
1d5161bd 251 PROF_stop(storeClient_kickReads);
252 copying = false;
a46d2c0e 253
4475555f
AR
254 anEntry->lock("store_client::copy"); // see deletion note below
255
528b2c61 256 storeClientCopy2(entry, this);
0ad2b63b 257
4475555f
AR
258 // Bug 3480: This store_client object may be deleted now if, for example,
259 // the client rejects the hit response copied above. Use on-stack pointers!
260
0ad2b63b 261#if USE_ADAPTATION
4475555f 262 anEntry->kickProducer();
0ad2b63b 263#endif
4475555f 264 anEntry->unlock("store_client::copy");
1809cebd 265
1809cebd 266 // Add no code here. This object may no longer exist.
f09f5b26 267}
268
f25d697f
AR
269/// Whether there is (or will be) more entry data for us.
270bool
271store_client::moreToSend() const
07304bf9 272{
f25d697f
AR
273 if (entry->store_status == STORE_PENDING)
274 return true; // there may be more coming
275
276 /* STORE_OK, including aborted entries: no more data is coming */
277
278 const int64_t len = entry->objectLen();
62e76326 279
f25d697f
AR
280 // If we do not know the entry length, then we have to open the swap file,
281 // which is only possible if there is one AND if we are allowed to use it.
282 const bool canSwapIn = entry->swap_filen >= 0 &&
283 getType() == STORE_DISK_CLIENT;
284 if (len < 0)
285 return canSwapIn;
62e76326 286
f25d697f
AR
287 if (copyInto.offset >= len)
288 return false; // sent everything there is
62e76326 289
f25d697f
AR
290 if (canSwapIn)
291 return true; // if we lack prefix, we can swap it in
62e76326 292
f25d697f
AR
293 // If we cannot swap in, make sure we have what we want in RAM. Otherwise,
294 // scheduleRead calls scheduleDiskRead which asserts on STORE_MEM_CLIENTs.
295 const MemObject *mem = entry->mem_obj;
296 return mem &&
9d4e9cfb 297 mem->inmem_lo <= copyInto.offset && copyInto.offset < mem->endOffset();
07304bf9 298}
299
f09f5b26 300static void
301storeClientCopy2(StoreEntry * e, store_client * sc)
302{
528b2c61 303 /* reentrancy not allowed - note this could lead to
304 * dropped events
305 */
62e76326 306
fa80a8ef 307 if (sc->flags.copy_event_pending) {
62e76326 308 return;
fa80a8ef 309 }
62e76326 310
db1cd23c 311 if (EBIT_TEST(e->flags, ENTRY_FWD_HDR_WAIT)) {
bf8fe701 312 debugs(90, 5, "storeClientCopy2: returning because ENTRY_FWD_HDR_WAIT set");
62e76326 313 return;
db1cd23c 314 }
62e76326 315
67fd69de 316 if (sc->flags.store_copying) {
3dd52a0b 317 sc->flags.copy_event_pending = true;
bf8fe701 318 debugs(90, 3, "storeClientCopy2: Queueing storeClientCopyEvent()");
62e76326 319 eventAdd("storeClientCopyEvent", storeClientCopyEvent, sc, 0.0, 0);
320 return;
67fd69de 321 }
62e76326 322
bf8fe701 323 debugs(90, 3, "storeClientCopy2: " << e->getMD5Text());
90703668 324 assert(sc->_callback.pending());
0bb129ee 325 /*
b7fe0ab0 326 * We used to check for ENTRY_ABORTED here. But there were some
0bb129ee 327 * problems. For example, we might have a slow client (or two) and
328 * the server-side is reading far ahead and swapping to disk. Even
329 * if the server-side aborts, we want to give the client(s)
330 * everything we got before the abort condition occurred.
331 */
528b2c61 332 /* Warning: doCopy may indirectly free itself in callbacks,
7e6b941f 333 * hence the lock to keep it active for the duration of
fa80a8ef 334 * this function
1809cebd
AR
335 * XXX: Locking does not prevent calling sc destructor (it only prevents
336 * freeing sc memory) so sc may become invalid from C++ p.o.v.
0d9d2c24 337 *
fa80a8ef 338 */
7e6b941f 339 cbdataInternalLock(sc);
3dd52a0b 340 assert (!sc->flags.store_copying);
528b2c61 341 sc->doCopy(e);
3dd52a0b 342 assert (!sc->flags.store_copying);
7e6b941f 343 cbdataInternalUnlock(sc);
cfac48c2 344}
345
528b2c61 346void
347store_client::doCopy(StoreEntry *anEntry)
cfac48c2 348{
528b2c61 349 assert (anEntry == entry);
3dd52a0b 350 flags.store_copying = true;
528b2c61 351 MemObject *mem = entry->mem_obj;
cd748f27 352
bf8fe701 353 debugs(33, 5, "store_client::doCopy: co: " <<
47f6e231 354 copyInto.offset << ", hi: " <<
355 mem->endOffset());
add2192d 356
f25d697f 357 if (!moreToSend()) {
62e76326 358 /* There is no more to send! */
26ac0430 359 debugs(33, 3, HERE << "There is no more to send!");
62e76326 360 callback(0);
3dd52a0b 361 flags.store_copying = false;
62e76326 362 return;
cfac48c2 363 }
62e76326 364
add2192d 365 /* Check that we actually have data */
528b2c61 366 if (anEntry->store_status == STORE_PENDING && copyInto.offset >= mem->endOffset()) {
bf8fe701 367 debugs(90, 3, "store_client::doCopy: Waiting for more");
3dd52a0b 368 flags.store_copying = false;
62e76326 369 return;
cfac48c2 370 }
62e76326 371
cfac48c2 372 /*
373 * Slight weirdness here. We open a swapin file for any
374 * STORE_DISK_CLIENT, even if we can copy the requested chunk
375 * from memory in the next block. We must try to open the
376 * swapin file before sending any data to the client side. If
377 * we postpone the open, and then can not open the file later
378 * on, the client loses big time. Its transfer just gets cut
379 * off. Better to open it early (while the client side handler
380 * is clientCacheHit) so that we can fall back to a cache miss
381 * if needed.
382 */
fa80a8ef 383
85a4b153 384 if (STORE_DISK_CLIENT == getType() && swapin_sio == NULL)
4e70dae3 385 startSwapin();
386 else
387 scheduleRead();
388}
389
390void
391store_client::startSwapin()
392{
bf8fe701 393 debugs(90, 3, "store_client::doCopy: Need to open swap in file");
4e70dae3 394 /* gotta open the swapin file */
395
396 if (storeTooManyDiskFilesOpen()) {
397 /* yuck -- this causes a TCP_SWAPFAIL_MISS on the client side */
398 fail();
3dd52a0b 399 flags.store_copying = false;
4e70dae3 400 return;
401 } else if (!flags.disk_io_pending) {
402 /* Don't set store_io_pending here */
403 storeSwapInStart(this);
62e76326 404
85a4b153 405 if (swapin_sio == NULL) {
62e76326 406 fail();
3dd52a0b 407 flags.store_copying = false;
62e76326 408 return;
62e76326 409 }
62e76326 410
4e70dae3 411 /*
412 * If the open succeeds we either copy from memory, or
413 * schedule a disk read in the next block.
414 */
415 scheduleRead();
416
417 return;
418 } else {
e0236918 419 debugs(90, DBG_IMPORTANT, "WARNING: Averted multiple fd operation (1)");
3dd52a0b 420 flags.store_copying = false;
62e76326 421 return;
cfac48c2 422 }
4e70dae3 423}
62e76326 424
4e70dae3 425void
426store_client::scheduleRead()
427{
428 MemObject *mem = entry->mem_obj;
429
430 if (copyInto.offset >= mem->inmem_lo && copyInto.offset < mem->endOffset())
431 scheduleMemRead();
432 else
433 scheduleDiskRead();
434}
435
436void
437store_client::scheduleDiskRead()
438{
cd748f27 439 /* What the client wants is not in memory. Schedule a disk read */
528b2c61 440 assert(STORE_DISK_CLIENT == getType());
62e76326 441
528b2c61 442 assert(!flags.disk_io_pending);
62e76326 443
bf8fe701 444 debugs(90, 3, "store_client::doCopy: reading from STORE");
62e76326 445
528b2c61 446 fileRead();
62e76326 447
3dd52a0b 448 flags.store_copying = false;
f09f5b26 449}
450
4e70dae3 451void
452store_client::scheduleMemRead()
453{
454 /* What the client wants is in memory */
455 /* Old style */
bf8fe701 456 debugs(90, 3, "store_client::doCopy: Copying normal from memory");
90703668 457 size_t sz = entry->mem_obj->data_hdr.copy(copyInto);
4e70dae3 458 callback(sz);
3dd52a0b 459 flags.store_copying = false;
4e70dae3 460}
461
528b2c61 462void
463store_client::fileRead()
f09f5b26 464{
528b2c61 465 MemObject *mem = entry->mem_obj;
466
90703668 467 assert(_callback.pending());
528b2c61 468 assert(!flags.disk_io_pending);
3dd52a0b 469 flags.disk_io_pending = true;
62e76326 470
528b2c61 471 if (mem->swap_hdr_sz != 0)
62e76326 472 if (entry->swap_status == SWAPOUT_WRITING)
47f6e231 473 assert(mem->swapout.sio->offset() > copyInto.offset + (int64_t)mem->swap_hdr_sz);
62e76326 474
528b2c61 475 storeRead(swapin_sio,
62e76326 476 copyInto.data,
477 copyInto.length,
478 copyInto.offset + mem->swap_hdr_sz,
479 mem->swap_hdr_sz == 0 ? storeClientReadHeader
480 : storeClientReadBody,
481 this);
f09f5b26 482}
483
beae59b0
HN
484void
485store_client::readBody(const char *buf, ssize_t len)
486{
487 int parsed_header = 0;
488
489 // Don't assert disk_io_pending here.. may be called by read_header
3dd52a0b 490 flags.disk_io_pending = false;
beae59b0 491 assert(_callback.pending());
4a7a3d56 492 debugs(90, 3, "storeClientReadBody: len " << len << "");
62e76326 493
9b769c67 494 if (copyInto.offset == 0 && len > 0 && entry->getReply()->sline.status() == Http::scNone) {
62e76326 495 /* Our structure ! */
beae59b0 496 HttpReply *rep = (HttpReply *) entry->getReply(); // bypass const
06a5ae20 497
beae59b0 498 if (!rep->parseCharBuf(copyInto.data, headersEnd(copyInto.data, len))) {
fa84c01d 499 debugs(90, DBG_CRITICAL, "Could not parse headers from on disk object");
beae59b0 500 } else {
3196f526
HN
501 parsed_header = 1;
502 }
06a5ae20 503 }
62e76326 504
beae59b0 505 const HttpReply *rep = entry->getReply();
ff4b33f4 506 if (len > 0 && rep && entry->mem_obj->inmem_lo == 0 && entry->objectLen() <= (int64_t)Config.Store.maxInMemObjSize && Config.onoff.memory_cache_disk) {
6d3c2758
HN
507 storeGetMemSpace(len);
508 // The above may start to free our object so we need to check again
509 if (entry->mem_obj->inmem_lo == 0) {
510 /* Copy read data back into memory.
55759ffb 511 * copyInto.offset includes headers, which is what mem cache needs
6d3c2758 512 */
619da1e9 513 int64_t mem_offset = entry->mem_obj->endOffset();
6d3c2758 514 if ((copyInto.offset == mem_offset) || (parsed_header && mem_offset == rep->hdr_sz)) {
55759ffb 515 entry->mem_obj->write(StoreIOBuffer(len, copyInto.offset, copyInto.data));
6d3c2758 516 }
3196f526 517 }
beae59b0
HN
518 }
519
520 callback(len);
528b2c61 521}
522
523void
62e76326 524store_client::fail()
528b2c61 525{
526 object_ok = false;
db2ff94c 527 /* synchronous open failures callback from the store,
528 * before startSwapin detects the failure.
529 * TODO: fix this inconsistent behaviour - probably by
26ac0430 530 * having storeSwapInStart become a callback functions,
db2ff94c 531 * not synchronous
532 */
533
90703668 534 if (_callback.pending())
db2ff94c 535 callback(0, true);
f09f5b26 536}
537
e3ef2b09 538static void
e5de8b13 539storeClientReadHeader(void *data, const char *buf, ssize_t len, StoreIOState::Pointer self)
e3ef2b09 540{
e6ccf245 541 store_client *sc = (store_client *)data;
528b2c61 542 sc->readHeader(buf, len);
543}
544
beae59b0
HN
545static void
546storeClientReadBody(void *data, const char *buf, ssize_t len, StoreIOState::Pointer self)
547{
548 store_client *sc = (store_client *)data;
549 sc->readBody(buf, len);
550}
551
528b2c61 552void
553store_client::unpackHeader(char const *buf, ssize_t len)
554{
4a7a3d56 555 debugs(90, 3, "store_client::unpackHeader: len " << len << "");
62e76326 556
e3ef2b09 557 if (len < 0) {
bf8fe701 558 debugs(90, 3, "store_client::unpackHeader: " << xstrerror() << "");
62e76326 559 fail();
560 return;
e3ef2b09 561 }
62e76326 562
528b2c61 563 int swap_hdr_sz = 0;
564 StoreMetaUnpacker aBuilder(buf, len, &swap_hdr_sz);
62e76326 565
528b2c61 566 if (!aBuilder.isBufferSane()) {
62e76326 567 /* oops, bad disk file? */
e0236918 568 debugs(90, DBG_IMPORTANT, "WARNING: swapfile header inconsistent with available data");
62e76326 569 fail();
570 return;
9bc73deb 571 }
62e76326 572
528b2c61 573 tlv *tlv_list = aBuilder.createStoreMeta ();
62e76326 574
e3ef2b09 575 if (tlv_list == NULL) {
e0236918 576 debugs(90, DBG_IMPORTANT, "WARNING: failed to unpack meta data");
62e76326 577 fail();
578 return;
e3ef2b09 579 }
62e76326 580
e3ef2b09 581 /*
7e3ce7b9 582 * Check the meta data and make sure we got the right object.
e3ef2b09 583 */
528b2c61 584 for (tlv *t = tlv_list; t; t = t->next) {
62e76326 585 if (!t->checkConsistency(entry)) {
586 storeSwapTLVFree(tlv_list);
587 fail();
588 return;
589 }
7e3ce7b9 590 }
62e76326 591
07304bf9 592 storeSwapTLVFree(tlv_list);
528b2c61 593
aa1a691e 594 assert(swap_hdr_sz >= 0);
528b2c61 595 entry->mem_obj->swap_hdr_sz = swap_hdr_sz;
3587dde2
AR
596 if (entry->swap_file_sz > 0) { // collapsed hits may not know swap_file_sz
597 assert(entry->swap_file_sz >= static_cast<uint64_t>(swap_hdr_sz));
598 entry->mem_obj->object_sz = entry->swap_file_sz - swap_hdr_sz;
599 }
aa1a691e
AR
600 debugs(90, 5, "store_client::unpackHeader: swap_file_sz=" <<
601 entry->swap_file_sz << "( " << swap_hdr_sz << " + " <<
602 entry->mem_obj->object_sz << ")");
528b2c61 603}
604
605void
606store_client::readHeader(char const *buf, ssize_t len)
607{
608 MemObject *const mem = entry->mem_obj;
62e76326 609
528b2c61 610 assert(flags.disk_io_pending);
3dd52a0b 611 flags.disk_io_pending = false;
90703668 612 assert(_callback.pending());
528b2c61 613
614 unpackHeader (buf, len);
62e76326 615
528b2c61 616 if (!object_ok)
62e76326 617 return;
618
e3ef2b09 619 /*
620 * If our last read got some data the client wants, then give
621 * it to them, otherwise schedule another read.
622 */
528b2c61 623 size_t body_sz = len - mem->swap_hdr_sz;
62e76326 624
47f6e231 625 if (copyInto.offset < static_cast<int64_t>(body_sz)) {
62e76326 626 /*
627 * we have (part of) what they want
628 */
d85c3078 629 size_t copy_sz = min(copyInto.length, body_sz);
4a7a3d56 630 debugs(90, 3, "storeClientReadHeader: copying " << copy_sz << " bytes of body");
41d00cd3 631 memmove(copyInto.data, copyInto.data + mem->swap_hdr_sz, copy_sz);
62e76326 632
3196f526 633 readBody(copyInto.data, copy_sz);
528b2c61 634
62e76326 635 return;
e3ef2b09 636 }
62e76326 637
e3ef2b09 638 /*
639 * we don't have what the client wants, but at least we now
640 * know the swap header size.
641 */
528b2c61 642 fileRead();
e3ef2b09 643}
644
f09f5b26 645int
a4b8110e 646storeClientCopyPending(store_client * sc, StoreEntry * e, void *data)
f09f5b26 647{
06d2839d 648#if STORE_CLIENT_LIST_DEBUG
649 assert(sc == storeClientListSearch(e->mem_obj, data));
edce4d98 650#endif
651#ifndef SILLY_CODE
62e76326 652
edce4d98 653 assert(sc);
06d2839d 654#endif
62e76326 655
06d2839d 656 assert(sc->entry == e);
edce4d98 657#if SILLY_CODE
62e76326 658
f09f5b26 659 if (sc == NULL)
62e76326 660 return 0;
661
edce4d98 662#endif
62e76326 663
90703668 664 if (!sc->_callback.pending())
62e76326 665 return 0;
666
f09f5b26 667 return 1;
668}
669
06d2839d 670/*
671 * This routine hasn't been optimised to take advantage of the
672 * passed sc. Yet.
673 */
f09f5b26 674int
a4b8110e 675storeUnregister(store_client * sc, StoreEntry * e, void *data)
f09f5b26 676{
677 MemObject *mem = e->mem_obj;
06d2839d 678#if STORE_CLIENT_LIST_DEBUG
62e76326 679
06d2839d 680 assert(sc == storeClientListSearch(e->mem_obj, data));
681#endif
62e76326 682
f09f5b26 683 if (mem == NULL)
62e76326 684 return 0;
685
26ac0430 686 debugs(90, 3, "storeUnregister: called for '" << e->getMD5Text() << "'");
62e76326 687
2f44bd34 688 if (sc == NULL) {
bf8fe701 689 debugs(90, 3, "storeUnregister: No matching client for '" << e->getMD5Text() << "'");
62e76326 690 return 0;
2f44bd34 691 }
62e76326 692
0e3f3e0d 693 if (mem->clientCount() == 0) {
bf8fe701 694 debugs(90, 3, "storeUnregister: Consistency failure - store client being unregistered is not in the mem object's list for '" << e->getMD5Text() << "'");
62e76326 695 return 0;
2f44bd34 696 }
62e76326 697
06d2839d 698 dlinkDelete(&sc->node, &mem->clients);
5e263176 699 -- mem->nclients;
62e76326 700
f09f5b26 701 if (e->store_status == STORE_OK && e->swap_status != SWAPOUT_DONE)
c07cbbf4 702 e->swapOut();
62e76326 703
85a4b153 704 if (sc->swapin_sio != NULL) {
aa1a691e 705 storeClose(sc->swapin_sio, StoreIOState::readerDone);
62e76326 706 sc->swapin_sio = NULL;
5db6bf73 707 ++statCounter.swap.ins;
eb824054 708 }
62e76326 709
90703668 710 if (sc->_callback.pending()) {
62e76326 711 /* callback with ssize = -1 to indicate unexpected termination */
c877c0bc 712 debugs(90, 3, "store_client for " << *e << " has a callback");
62e76326 713 sc->fail();
f09f5b26 714 }
62e76326 715
fa80a8ef 716#if STORE_CLIENT_LIST_DEBUG
717 cbdataReferenceDone(sc->owner);
62e76326 718
fa80a8ef 719#endif
62e76326 720
528b2c61 721 delete sc;
62e76326 722
1bfe9ade 723 assert(e->locked());
178c7e33
AR
724 // An entry locked by others may be unlocked (and destructed) by others, so
725 // we must lock again to safely dereference e after CheckQuickAbort().
726 e->lock("storeUnregister");
62e76326 727
77b32a34 728 if (mem->nclients == 0)
62e76326 729 CheckQuickAbort(e);
a46d2c0e 730 else
731 mem->kickReads();
62e76326 732
0ad2b63b
CT
733#if USE_ADAPTATION
734 e->kickProducer();
735#endif
736
178c7e33 737 e->unlock("storeUnregister");
f09f5b26 738 return 1;
739}
740
f09f5b26 741/* Call handlers waiting for data to be appended to E. */
742void
d88e3c49 743StoreEntry::invokeHandlers()
f09f5b26 744{
528b2c61 745 /* Commit what we can to disk, if appropriate */
d88e3c49 746 swapOut();
f09f5b26 747 int i = 0;
f09f5b26 748 store_client *sc;
06d2839d 749 dlink_node *nx = NULL;
750 dlink_node *node;
751
0bc8db64 752 PROF_start(InvokeHandlers);
9ea37c79 753
bf8fe701 754 debugs(90, 3, "InvokeHandlers: " << getMD5Text() );
f09f5b26 755 /* walk the entire list looking for valid callbacks */
62e76326 756
d88e3c49 757 for (node = mem_obj->clients.head; node; node = nx) {
62e76326 758 sc = (store_client *)node->data;
759 nx = node->next;
5db6bf73
FC
760 debugs(90, 3, "StoreEntry::InvokeHandlers: checking client #" << i );
761 ++i;
62e76326 762
90703668 763 if (!sc->_callback.pending())
62e76326 764 continue;
765
766 if (sc->flags.disk_io_pending)
767 continue;
768
d88e3c49 769 storeClientCopy2(this, sc);
f09f5b26 770 }
0bc8db64 771 PROF_stop(InvokeHandlers);
f09f5b26 772}
773
22bbd840 774// Does not account for remote readers/clients.
f09f5b26 775int
776storePendingNClients(const StoreEntry * e)
777{
f09f5b26 778 MemObject *mem = e->mem_obj;
36547bcf 779 int npend = NULL == mem ? 0 : mem->nclients;
bf8fe701 780 debugs(90, 3, "storePendingNClients: returning " << npend);
f09f5b26 781 return npend;
782}
77b32a34 783
ae6568e7
AJ
784/* return true if the request should be aborted */
785static bool
786CheckQuickAbortIsReasonable(StoreEntry * entry)
77b32a34 787{
528b2c61 788 MemObject * const mem = entry->mem_obj;
77b32a34 789 assert(mem);
ae6568e7 790 debugs(90, 3, "entry=" << entry << ", mem=" << mem);
62e76326 791
45e5102d 792 if (mem->request && !mem->request->flags.cachable) {
ae6568e7
AJ
793 debugs(90, 3, "quick-abort? YES !mem->request->flags.cachable");
794 return true;
77b32a34 795 }
62e76326 796
77b32a34 797 if (EBIT_TEST(entry->flags, KEY_PRIVATE)) {
ae6568e7
AJ
798 debugs(90, 3, "quick-abort? YES KEY_PRIVATE");
799 return true;
77b32a34 800 }
62e76326 801
47f6e231 802 int64_t expectlen = entry->getReply()->content_length + entry->getReply()->hdr_sz;
0e3f3e0d 803
ae6568e7 804 if (expectlen < 0) {
2324cda2 805 /* expectlen is < 0 if *no* information about the object has been received */
ae6568e7
AJ
806 debugs(90, 3, "quick-abort? YES no object data received yet");
807 return true;
808 }
0e3f3e0d 809
ae6568e7 810 int64_t curlen = mem->endOffset();
62e76326 811
47f6e231 812 if (Config.quickAbort.min < 0) {
ae6568e7
AJ
813 debugs(90, 3, "quick-abort? NO disabled");
814 return false;
77b32a34 815 }
62e76326 816
ae6568e7 817 if (mem->request && mem->request->range && mem->request->getRangeOffsetLimit() < 0) {
ab275c7b 818 /* Don't abort if the admin has configured range_ofset -1 to download fully for caching. */
ae6568e7
AJ
819 debugs(90, 3, "quick-abort? NO admin configured range replies to full-download");
820 return false;
ab275c7b
AJ
821 }
822
77b32a34 823 if (curlen > expectlen) {
f22b0af0 824 debugs(90, 3, "quick-abort? YES bad content length (" << curlen << " of " << expectlen << " bytes received)");
ae6568e7 825 return true;
77b32a34 826 }
62e76326 827
47f6e231 828 if ((expectlen - curlen) < (Config.quickAbort.min << 10)) {
ae6568e7
AJ
829 debugs(90, 3, "quick-abort? NO only a little more object left to receive");
830 return false;
77b32a34 831 }
62e76326 832
77b32a34 833 if ((expectlen - curlen) > (Config.quickAbort.max << 10)) {
ae6568e7
AJ
834 debugs(90, 3, "quick-abort? YES too much left to go");
835 return true;
77b32a34 836 }
62e76326 837
77b32a34 838 if (expectlen < 100) {
ae6568e7
AJ
839 debugs(90, 3, "quick-abort? NO avoid FPE");
840 return false;
77b32a34 841 }
62e76326 842
47f6e231 843 if ((curlen / (expectlen / 100)) > (Config.quickAbort.pct)) {
ae6568e7
AJ
844 debugs(90, 3, "quick-abort? NO past point of no return");
845 return false;
77b32a34 846 }
62e76326 847
ae6568e7
AJ
848 debugs(90, 3, "quick-abort? YES default");
849 return true;
77b32a34 850}
851
22bbd840
AR
852/// Aborts a swapping-out entry if nobody needs it any more _and_
853/// continuing swap out is not reasonable per CheckQuickAbortIsReasonable().
77b32a34 854static void
855CheckQuickAbort(StoreEntry * entry)
856{
528b2c61 857 assert (entry);
62e76326 858
77b32a34 859 if (storePendingNClients(entry) > 0)
62e76326 860 return;
861
22bbd840
AR
862 if (!shutting_down && Store::Root().transientReaders(*entry))
863 return;
864
77b32a34 865 if (entry->store_status != STORE_PENDING)
62e76326 866 return;
867
986ebffc 868 if (EBIT_TEST(entry->flags, ENTRY_SPECIAL))
62e76326 869 return;
870
ae6568e7 871 if (!CheckQuickAbortIsReasonable(entry))
62e76326 872 return;
873
bfb55b6f 874 entry->abort();
77b32a34 875}
c8be6d7b 876
877void
fcc35180 878store_client::dumpStats(MemBuf * output, int clientNumber) const
c8be6d7b 879{
90703668 880 if (_callback.pending())
62e76326 881 return;
882
2fe7eff9 883 output->Printf("\tClient #%d, %p\n", clientNumber, _callback.callback_data);
62e76326 884
c91ca3ce 885 output->Printf("\t\tcopy_offset: %" PRId64 "\n",
47f6e231 886 copyInto.offset);
62e76326 887
2fe7eff9 888 output->Printf("\t\tcopy_size: %d\n",
889 (int) copyInto.length);
62e76326 890
2fe7eff9 891 output->Printf("\t\tflags:");
62e76326 892
528b2c61 893 if (flags.disk_io_pending)
2fe7eff9 894 output->Printf(" disk_io_pending");
62e76326 895
528b2c61 896 if (flags.store_copying)
2fe7eff9 897 output->Printf(" store_copying");
62e76326 898
528b2c61 899 if (flags.copy_event_pending)
2fe7eff9 900 output->Printf(" copy_event_pending");
62e76326 901
2fe7eff9 902 output->Printf("\n");
528b2c61 903}
c8be6d7b 904
528b2c61 905bool
90703668 906store_client::Callback::pending() const
528b2c61 907{
90703668 908 return callback_handler && callback_data;
c8be6d7b 909}
528b2c61 910
911store_client::Callback::Callback(STCB *function, void *data) : callback_handler(function), callback_data (data) {}
b67e2c8c 912
9a0a18de 913#if USE_DELAY_POOLS
b67e2c8c 914void
915store_client::setDelayId(DelayId delay_id)
916{
917 delayId = delay_id;
918}
515ec4dc 919#endif