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