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