]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_client.cc
Summary: BUGFIX: ConnStateData needs to own itself.
[thirdparty/squid.git] / src / store_client.cc
CommitLineData
9cef6668 1
2/*
a2ac85d9 3 * $Id: store_client.cc,v 1.129 2003/07/11 01:40:37 robertc Exp $
9cef6668 4 *
2f44bd34 5 * DEBUG: section 90 Storage Manager Client-Side Interface
9cef6668 6 * AUTHOR: Duane Wessels
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9cef6668 9 * ----------------------------------------------------------
10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
9cef6668 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
2f44bd34 34 * Portions copyright (c) 2003 Robert Collins <robertc@squid-cache.org>
9cef6668 35 */
36
f09f5b26 37#include "squid.h"
c8be6d7b 38#include "StoreClient.h"
e6ccf245 39#include "Store.h"
528b2c61 40#include "HttpReply.h"
41#include "MemObject.h"
42#include "StoreMeta.h"
43#include "StoreMetaUnpacker.h"
b67e2c8c 44#if DELAY_POOLS
45#include "DelayPools.h"
46#endif
a2ac85d9 47#include "HttpRequest.h"
c8be6d7b 48
49CBDATA_TYPE(store_client);
f09f5b26 50
e3ef2b09 51/*
52 * NOTE: 'Header' refers to the swapfile metadata header.
528b2c61 53 * 'OBJHeader' refers to the object header, with cannonical
54 * processed object headers (which may derive from FTP/HTTP etc
55 * upstream protocols
e3ef2b09 56 * 'Body' refers to the swapfile body, which is the full
57 * HTTP reply (including HTTP headers and body).
58 */
2391a162 59static STRCB storeClientReadBody;
60static STRCB storeClientReadHeader;
f09f5b26 61static void storeClientCopy2(StoreEntry * e, store_client * sc);
d6f51e3c 62static EVH storeClientCopyEvent;
77b32a34 63static int CheckQuickAbort2(StoreEntry * entry);
64static void CheckQuickAbort(StoreEntry * entry);
f09f5b26 65
528b2c61 66MemPool *store_client::pool = NULL;
67
68void *
69store_client::operator new (size_t byteCount)
70{
71 /* derived classes with different sizes must implement their own new */
72 assert (byteCount == sizeof (store_client));
73 CBDATA_INIT_TYPE(store_client);
74 return cbdataAlloc(store_client);
75}
76
77void
78store_client::operator delete (void *address)
79{
101a3d6f 80 cbdataFree (address);
528b2c61 81}
82
83bool
84store_client::memReaderHasLowerOffset(off_t anOffset) const
85{
86 return getType() == STORE_MEM_CLIENT && copyInto.offset < anOffset;
87}
88
89int
90store_client::getType() const
91{
92 return type;
93}
94
06d2839d 95#if STORE_CLIENT_LIST_DEBUG
fa80a8ef 96static store_client *
f09f5b26 97storeClientListSearch(const MemObject * mem, void *data)
98{
06d2839d 99 dlink_node *node;
100 store_client *sc = NULL;
62e76326 101
06d2839d 102 for (node = mem->clients.head; node; node = node->next) {
62e76326 103 sc = node->data;
104
105 if (sc->owner == data)
106 return sc;
f09f5b26 107 }
62e76326 108
06d2839d 109 return NULL;
f09f5b26 110}
c8be6d7b 111
112int
113storeClientIsThisAClient(store_client * sc, void *someClient)
114{
115 return sc->owner == someClient;
116}
62e76326 117
06d2839d 118#endif
f09f5b26 119
120/* add client with fd to client list */
06d2839d 121store_client *
f09f5b26 122storeClientListAdd(StoreEntry * e, void *data)
123{
124 MemObject *mem = e->mem_obj;
f09f5b26 125 store_client *sc;
126 assert(mem);
06d2839d 127#if STORE_CLIENT_LIST_DEBUG
62e76326 128
f09f5b26 129 if (storeClientListSearch(mem, data) != NULL)
62e76326 130 /* XXX die! */
131 assert(1 == 0);
132
6b8e7481 133#endif
62e76326 134
528b2c61 135 sc = new store_client (e);
62e76326 136
528b2c61 137 mem->addClient(sc);
62e76326 138
06d2839d 139 return sc;
f09f5b26 140}
141
528b2c61 142void
143store_client::callback(ssize_t sz, bool error)
b04e66e0 144{
528b2c61 145 StoreIOBuffer result (sz, 0 ,copyInto.data);
62e76326 146
c8be6d7b 147 if (sz < 0) {
62e76326 148 result.flags.error = 1;
149 result.length = 0;
528b2c61 150 } else {
62e76326 151 result.flags.error = error ? 1 : 0;
c8be6d7b 152 }
62e76326 153
528b2c61 154 result.offset = cmp_offset;
155 assert(callbackPending());
156 cmp_offset = copyInto.offset + sz;
157 STCB *temphandler = _callback.callback_handler;
158 void *cbdata = _callback.callback_data;
159 _callback = Callback(NULL, NULL);
160 copyInto.data = NULL;
62e76326 161
528b2c61 162 if (cbdataReferenceValid(cbdata))
62e76326 163 temphandler(cbdata, result);
164
528b2c61 165 cbdataReferenceDone(cbdata);
b04e66e0 166}
167
f115fadd 168static void
169storeClientCopyEvent(void *data)
170{
e6ccf245 171 store_client *sc = (store_client *)data;
2f44bd34 172 debug(90, 3)("storeClientCopyEvent: Running\n");
528b2c61 173 assert (sc->flags.copy_event_pending);
67fd69de 174 sc->flags.copy_event_pending = 0;
62e76326 175
528b2c61 176 if (!sc->callbackPending())
62e76326 177 return;
178
f115fadd 179 storeClientCopy2(sc->entry, sc);
f115fadd 180}
181
b67e2c8c 182store_client::store_client(StoreEntry *e) : entry (e)
183#if DELAY_POOLS
62e76326 184 , delayId()
b67e2c8c 185#endif
62e76326 186 , type (e->storeClientType())
187 , object_ok(true)
528b2c61 188{
189 cmp_offset = 0;
190 flags.disk_io_pending = 0;
191 entry->refcount++;
62e76326 192
528b2c61 193 if (getType() == STORE_DISK_CLIENT)
62e76326 194 /* assert we'll be able to get the data we want */
195 /* maybe we should open swapin_fd here */
196 assert(entry->swap_filen > -1 || storeSwapOutAble(entry));
197
528b2c61 198#if STORE_CLIENT_LIST_DEBUG
62e76326 199
528b2c61 200 owner = cbdataReference(data);
62e76326 201
528b2c61 202#endif
203}
204
2f44bd34 205store_client::~store_client()
62e76326 206{}
2f44bd34 207
f09f5b26 208/* copy bytes requested by the client */
209void
a4b8110e 210storeClientCopy(store_client * sc,
62e76326 211 StoreEntry * e,
212 StoreIOBuffer copyInto,
213 STCB * callback,
214 void *data)
f09f5b26 215{
528b2c61 216 assert (sc != NULL);
217 sc->copy(e, copyInto,callback,data);
218}
219
220void
221store_client::copy(StoreEntry * anEntry,
62e76326 222 StoreIOBuffer copyRequest,
223 STCB * callback_fn,
224 void *data)
528b2c61 225{
226 assert (anEntry == entry);
227 assert (callback_fn);
228 assert (data);
229 assert(!EBIT_TEST(entry->flags, ENTRY_ABORTED));
2f44bd34 230 debug(90, 3)("store_client::copy: %s, from %lu, for length %d, cb %p, cbdata %p",
62e76326 231 entry->getMD5Text(),
232 (unsigned long) copyRequest.offset,
233 (int) copyRequest.length,
234 callback_fn,
235 data);
06d2839d 236#if STORE_CLIENT_LIST_DEBUG
62e76326 237
528b2c61 238 assert(this == storeClientListSearch(entry->mem_obj, data));
06d2839d 239#endif
62e76326 240
528b2c61 241 assert(!callbackPending());
242#if ONLYCONTIGUOUSREQUESTS
62e76326 243
528b2c61 244 assert(cmp_offset == copyRequest.offset);
245#endif
246 /* range requests will skip into the body */
247 cmp_offset = copyRequest.offset;
248 _callback = Callback (callback_fn, cbdataReference(data));
249 copyInto.data = copyRequest.data;
250 copyInto.length = copyRequest.length;
251 copyInto.offset = copyRequest.offset;
252
1d5161bd 253 static bool copying (false);
254 assert (!copying);
255 copying = true;
256 PROF_start(storeClient_kickReads);
a46d2c0e 257 /* we might be blocking comm reads due to readahead limits
258 * now we have a new offset, trigger those reads...
259 */
260 entry->mem_obj->kickReads();
1d5161bd 261 PROF_stop(storeClient_kickReads);
262 copying = false;
a46d2c0e 263
528b2c61 264 storeClientCopy2(entry, this);
f09f5b26 265}
266
07304bf9 267/*
268 * This function is used below to decide if we have any more data to
0bb129ee 269 * send to the client. If the store_status is STORE_PENDING, then we
b7fe0ab0 270 * do have more data to send. If its STORE_OK, then
0bb129ee 271 * we continue checking. If the object length is negative, then we
272 * don't know the real length and must open the swap file to find out.
273 * If the length is >= 0, then we compare it to the requested copy
274 * offset.
07304bf9 275 */
276static int
277storeClientNoMoreToSend(StoreEntry * e, store_client * sc)
278{
279 ssize_t len;
62e76326 280
0bb129ee 281 if (e->store_status == STORE_PENDING)
62e76326 282 return 0;
283
07304bf9 284 if ((len = objectLen(e)) < 0)
62e76326 285 return 0;
286
c8be6d7b 287 if (sc->copyInto.offset < len)
62e76326 288 return 0;
289
07304bf9 290 return 1;
291}
292
f09f5b26 293static void
294storeClientCopy2(StoreEntry * e, store_client * sc)
295{
528b2c61 296 /* reentrancy not allowed - note this could lead to
297 * dropped events
298 */
62e76326 299
fa80a8ef 300 if (sc->flags.copy_event_pending) {
62e76326 301 return;
fa80a8ef 302 }
62e76326 303
db1cd23c 304 if (EBIT_TEST(e->flags, ENTRY_FWD_HDR_WAIT)) {
62e76326 305 debug(90, 5)("storeClientCopy2: returning because ENTRY_FWD_HDR_WAIT set\n");
306 return;
db1cd23c 307 }
62e76326 308
67fd69de 309 if (sc->flags.store_copying) {
62e76326 310 sc->flags.copy_event_pending = 1;
311 debug(90, 3)("storeClientCopy2: Queueing storeClientCopyEvent()\n");
312 eventAdd("storeClientCopyEvent", storeClientCopyEvent, sc, 0.0, 0);
313 return;
67fd69de 314 }
62e76326 315
2f44bd34 316 debug(90, 3)("storeClientCopy2: %s\n", e->getMD5Text());
528b2c61 317 assert(sc->callbackPending());
0bb129ee 318 /*
b7fe0ab0 319 * We used to check for ENTRY_ABORTED here. But there were some
0bb129ee 320 * problems. For example, we might have a slow client (or two) and
321 * the server-side is reading far ahead and swapping to disk. Even
322 * if the server-side aborts, we want to give the client(s)
323 * everything we got before the abort condition occurred.
324 */
528b2c61 325 /* Warning: doCopy may indirectly free itself in callbacks,
fa80a8ef 326 * hence the cbdata reference to keep it active for the duration of
327 * this function
328 */
528b2c61 329 cbdataReference(sc);
330 assert (sc->flags.store_copying == 0);
331 sc->doCopy(e);
332 assert (sc->flags.store_copying == 0);
333 cbdataReferenceDone(sc);
cfac48c2 334}
335
528b2c61 336void
337store_client::doCopy(StoreEntry *anEntry)
cfac48c2 338{
528b2c61 339 assert (anEntry == entry);
340 flags.store_copying = 1;
341 MemObject *mem = entry->mem_obj;
cd748f27 342
528b2c61 343 debug(33, 5)("store_client::doCopy: co: %lu, hi: %ld\n", (unsigned long) copyInto.offset, (long int) mem->endOffset());
add2192d 344
528b2c61 345 if (storeClientNoMoreToSend(entry, this)) {
62e76326 346 /* There is no more to send! */
347 callback(0);
348 flags.store_copying = 0;
349 return;
cfac48c2 350 }
62e76326 351
add2192d 352 /* Check that we actually have data */
528b2c61 353 if (anEntry->store_status == STORE_PENDING && copyInto.offset >= mem->endOffset()) {
62e76326 354 debug(90, 3)("store_client::doCopy: Waiting for more\n");
355 flags.store_copying = 0;
356 return;
cfac48c2 357 }
62e76326 358
cfac48c2 359 /*
360 * Slight weirdness here. We open a swapin file for any
361 * STORE_DISK_CLIENT, even if we can copy the requested chunk
362 * from memory in the next block. We must try to open the
363 * swapin file before sending any data to the client side. If
364 * we postpone the open, and then can not open the file later
365 * on, the client loses big time. Its transfer just gets cut
366 * off. Better to open it early (while the client side handler
367 * is clientCacheHit) so that we can fall back to a cache miss
368 * if needed.
369 */
fa80a8ef 370
528b2c61 371 if (STORE_DISK_CLIENT == getType() && NULL == swapin_sio.getRaw()) {
62e76326 372 debug(90, 3)("store_client::doCopy: Need to open swap in file\n");
373 /* gotta open the swapin file */
374
375 if (storeTooManyDiskFilesOpen()) {
376 /* yuck -- this causes a TCP_SWAPFAIL_MISS on the client side */
377 fail();
378 flags.store_copying = 0;
379 return;
380 } else if (!flags.disk_io_pending) {
381 /* Don't set store_io_pending here */
382 storeSwapInStart(this);
383
384 if (NULL == swapin_sio.getRaw()) {
385 fail();
386 flags.store_copying = 0;
387 return;
388 }
389
390 /*
391 * If the open succeeds we either copy from memory, or
392 * schedule a disk read in the next block.
393 */
394 } else {
395 debug (90, 1)("WARNING: Averted multiple fd operation (1)\n");
396 flags.store_copying = 0;
397 return;
398 }
f09f5b26 399 }
62e76326 400
528b2c61 401 if (copyInto.offset >= mem->inmem_lo && copyInto.offset < mem->endOffset()) {
62e76326 402 /* What the client wants is in memory */
403 /* Old style */
404 debug(90, 3)("store_client::doCopy: Copying normal from memory\n");
405 size_t sz = mem->data_hdr.copy(copyInto.offset, copyInto.data,
406 copyInto.length);
407 callback(sz);
408 flags.store_copying = 0;
409 return;
cfac48c2 410 }
62e76326 411
cd748f27 412 /* What the client wants is not in memory. Schedule a disk read */
528b2c61 413 assert(STORE_DISK_CLIENT == getType());
62e76326 414
528b2c61 415 assert(!flags.disk_io_pending);
62e76326 416
2f44bd34 417 debug(90, 3)("store_client::doCopy: reading from STORE\n");
62e76326 418
528b2c61 419 fileRead();
62e76326 420
528b2c61 421 flags.store_copying = 0;
f09f5b26 422}
423
528b2c61 424void
425store_client::fileRead()
f09f5b26 426{
528b2c61 427 MemObject *mem = entry->mem_obj;
428
429 assert(callbackPending());
430 assert(!flags.disk_io_pending);
431 flags.disk_io_pending = 1;
62e76326 432
528b2c61 433 if (mem->swap_hdr_sz != 0)
62e76326 434 if (entry->swap_status == SWAPOUT_WRITING)
435 assert(mem->swapout.sio->offset() > copyInto.offset + (off_t)mem->swap_hdr_sz);
436
528b2c61 437 storeRead(swapin_sio,
62e76326 438 copyInto.data,
439 copyInto.length,
440 copyInto.offset + mem->swap_hdr_sz,
441 mem->swap_hdr_sz == 0 ? storeClientReadHeader
442 : storeClientReadBody,
443 this);
f09f5b26 444}
445
446static void
5bd1abac 447storeClientReadBody(void *data, const char *buf, ssize_t len)
f09f5b26 448{
e6ccf245 449 store_client *sc = (store_client *)data;
27002b34 450 assert(sc->flags.disk_io_pending);
f115fadd 451 sc->flags.disk_io_pending = 0;
528b2c61 452 assert(sc->callbackPending());
2f44bd34 453 debug(90, 3)("storeClientReadBody: len %d", (int) len);
62e76326 454
528b2c61 455 if (sc->copyInto.offset == 0 && len > 0 && sc->entry->getReply()->sline.status == 0)
62e76326 456 /* Our structure ! */
457 if (!httpReplyParse((HttpReply *)sc->entry->getReply(), sc->copyInto.data, headersEnd(sc->copyInto.data, len))) {
458 debug (90,0)("Could not parse headers from on disk object\n");
459 }
460
528b2c61 461 sc->callback(len);
462}
463
464void
62e76326 465store_client::fail()
528b2c61 466{
467 object_ok = false;
468 callback(0, true);
f09f5b26 469}
470
e3ef2b09 471static void
5bd1abac 472storeClientReadHeader(void *data, const char *buf, ssize_t len)
e3ef2b09 473{
e6ccf245 474 store_client *sc = (store_client *)data;
528b2c61 475 sc->readHeader(buf, len);
476}
477
478void
479store_client::unpackHeader(char const *buf, ssize_t len)
480{
2f44bd34 481 debug(90, 3)("store_client::unpackHeader: len %d", (int) len);
62e76326 482
e3ef2b09 483 if (len < 0) {
62e76326 484 debug(90, 3)("store_client::unpackHeader: %s", xstrerror());
485 fail();
486 return;
e3ef2b09 487 }
62e76326 488
528b2c61 489 int swap_hdr_sz = 0;
490 StoreMetaUnpacker aBuilder(buf, len, &swap_hdr_sz);
62e76326 491
528b2c61 492 if (!aBuilder.isBufferSane()) {
62e76326 493 /* oops, bad disk file? */
494 debug(90, 1) ("WARNING: swapfile header inconsistent with available data\n");
495 fail();
496 return;
9bc73deb 497 }
62e76326 498
528b2c61 499 tlv *tlv_list = aBuilder.createStoreMeta ();
62e76326 500
e3ef2b09 501 if (tlv_list == NULL) {
62e76326 502 debug(90, 1) ("WARNING: failed to unpack meta data\n");
503 fail();
504 return;
e3ef2b09 505 }
62e76326 506
e3ef2b09 507 /*
7e3ce7b9 508 * Check the meta data and make sure we got the right object.
e3ef2b09 509 */
528b2c61 510 for (tlv *t = tlv_list; t; t = t->next) {
62e76326 511 if (!t->checkConsistency(entry)) {
512 storeSwapTLVFree(tlv_list);
513 fail();
514 return;
515 }
7e3ce7b9 516 }
62e76326 517
07304bf9 518 storeSwapTLVFree(tlv_list);
528b2c61 519
520 entry->mem_obj->swap_hdr_sz = swap_hdr_sz;
521 entry->mem_obj->object_sz = entry->swap_file_sz - swap_hdr_sz;
522
523}
524
525void
526store_client::readHeader(char const *buf, ssize_t len)
527{
528 MemObject *const mem = entry->mem_obj;
62e76326 529
528b2c61 530 assert(flags.disk_io_pending);
531 flags.disk_io_pending = 0;
532 assert(callbackPending());
533
534 unpackHeader (buf, len);
62e76326 535
528b2c61 536 if (!object_ok)
62e76326 537 return;
538
e3ef2b09 539 /*
540 * If our last read got some data the client wants, then give
541 * it to them, otherwise schedule another read.
542 */
528b2c61 543 size_t body_sz = len - mem->swap_hdr_sz;
62e76326 544
528b2c61 545 if (static_cast<size_t>(copyInto.offset) < body_sz) {
62e76326 546 /*
547 * we have (part of) what they want
548 */
549 size_t copy_sz = XMIN(copyInto.length, body_sz)
550 ;
551 debug(90, 3) ("storeClientReadHeader: copying %d bytes of body\n",
552 (int) copy_sz);
553 xmemmove(copyInto.data, copyInto.data + mem->swap_hdr_sz, copy_sz);
554
555 if (copyInto.offset == 0 && len > 0 && entry->getReply()->sline.status == 0)
556 /* Our structure ! */
557 if (!httpReplyParse((HttpReply *)entry->getReply(), copyInto.data,
558 headersEnd(copyInto.data, copy_sz))) {
559 debug (90,0)("could not parse headers from on disk structure!\n");
560 }
528b2c61 561
562 callback(copy_sz);
62e76326 563 return;
e3ef2b09 564 }
62e76326 565
e3ef2b09 566 /*
567 * we don't have what the client wants, but at least we now
568 * know the swap header size.
569 */
528b2c61 570 fileRead();
e3ef2b09 571}
572
f09f5b26 573int
a4b8110e 574storeClientCopyPending(store_client * sc, StoreEntry * e, void *data)
f09f5b26 575{
06d2839d 576#if STORE_CLIENT_LIST_DEBUG
577 assert(sc == storeClientListSearch(e->mem_obj, data));
edce4d98 578#endif
579#ifndef SILLY_CODE
62e76326 580
edce4d98 581 assert(sc);
06d2839d 582#endif
62e76326 583
06d2839d 584 assert(sc->entry == e);
edce4d98 585#if SILLY_CODE
62e76326 586
f09f5b26 587 if (sc == NULL)
62e76326 588 return 0;
589
edce4d98 590#endif
62e76326 591
528b2c61 592 if (!sc->callbackPending())
62e76326 593 return 0;
594
f09f5b26 595 return 1;
596}
597
06d2839d 598/*
599 * This routine hasn't been optimised to take advantage of the
600 * passed sc. Yet.
601 */
f09f5b26 602int
a4b8110e 603storeUnregister(store_client * sc, StoreEntry * e, void *data)
f09f5b26 604{
605 MemObject *mem = e->mem_obj;
06d2839d 606#if STORE_CLIENT_LIST_DEBUG
62e76326 607
06d2839d 608 assert(sc == storeClientListSearch(e->mem_obj, data));
609#endif
62e76326 610
f09f5b26 611 if (mem == NULL)
62e76326 612 return 0;
613
2f44bd34 614 debug(90, 3) ("storeUnregister: called for '%s'\n", e->getMD5Text());
62e76326 615
2f44bd34 616 if (sc == NULL) {
62e76326 617 debug(90, 3) ("storeUnregister: No matching client for '%s'\n", e->getMD5Text());
618 return 0;
2f44bd34 619 }
62e76326 620
0e3f3e0d 621 if (mem->clientCount() == 0) {
62e76326 622 debug(90, 3) ("storeUnregister: Consistency failure - store client being unregistered is not in the mem object's list for '%s'\n", e->getMD5Text());
623 return 0;
2f44bd34 624 }
62e76326 625
0e3f3e0d 626 if (mem->clientIsFirst(sc)) {
62e76326 627 /*
628 * If we are unregistering the _first_ client for this
629 * entry, then we have to reset the client FD to -1.
630 */
631 mem->fd = -1;
4c454c5c 632 }
62e76326 633
06d2839d 634 dlinkDelete(&sc->node, &mem->clients);
f09f5b26 635 mem->nclients--;
62e76326 636
f09f5b26 637 if (e->store_status == STORE_OK && e->swap_status != SWAPOUT_DONE)
62e76326 638 storeSwapOut(e);
639
d3b3ab85 640 if (sc->swapin_sio.getRaw()) {
62e76326 641 storeClose(sc->swapin_sio);
642 sc->swapin_sio = NULL;
643 statCounter.swap.ins++;
eb824054 644 }
62e76326 645
646 if (sc->callbackPending()) {
647 /* callback with ssize = -1 to indicate unexpected termination */
648 debug(90, 3) ("storeUnregister: store_client for %s has a callback\n",
649 mem->url);
650 sc->fail();
f09f5b26 651 }
62e76326 652
fa80a8ef 653#if STORE_CLIENT_LIST_DEBUG
654 cbdataReferenceDone(sc->owner);
62e76326 655
fa80a8ef 656#endif
62e76326 657
528b2c61 658 delete sc;
62e76326 659
77b32a34 660 assert(e->lock_count > 0);
62e76326 661
77b32a34 662 if (mem->nclients == 0)
62e76326 663 CheckQuickAbort(e);
a46d2c0e 664 else
665 mem->kickReads();
62e76326 666
f09f5b26 667 return 1;
668}
669
670off_t
671storeLowestMemReaderOffset(const StoreEntry * entry)
672{
528b2c61 673 return entry->mem_obj->lowestMemReaderOffset();
f09f5b26 674}
675
676/* Call handlers waiting for data to be appended to E. */
677void
678InvokeHandlers(StoreEntry * e)
679{
528b2c61 680 /* Commit what we can to disk, if appropriate */
681 storeSwapOut (e);
f09f5b26 682 int i = 0;
683 MemObject *mem = e->mem_obj;
684 store_client *sc;
06d2839d 685 dlink_node *nx = NULL;
686 dlink_node *node;
687
2f44bd34 688 debug(90, 3) ("InvokeHandlers: %s\n", e->getMD5Text());
f09f5b26 689 /* walk the entire list looking for valid callbacks */
62e76326 690
06d2839d 691 for (node = mem->clients.head; node; node = nx) {
62e76326 692 sc = (store_client *)node->data;
693 nx = node->next;
694 debug(90, 3) ("InvokeHandlers: checking client #%d\n", i++);
695
696 if (!sc->callbackPending())
697 continue;
698
699 if (sc->flags.disk_io_pending)
700 continue;
701
702 storeClientCopy2(e, sc);
f09f5b26 703 }
704}
705
706int
707storePendingNClients(const StoreEntry * e)
708{
f09f5b26 709 MemObject *mem = e->mem_obj;
36547bcf 710 int npend = NULL == mem ? 0 : mem->nclients;
2f44bd34 711 debug(90, 3) ("storePendingNClients: returning %d\n", npend);
f09f5b26 712 return npend;
713}
77b32a34 714
715/* return 1 if the request should be aborted */
716static int
717CheckQuickAbort2(StoreEntry * entry)
718{
e6ccf245 719 size_t curlen;
720 size_t minlen;
721 size_t expectlen;
528b2c61 722 MemObject * const mem = entry->mem_obj;
77b32a34 723 assert(mem);
2f44bd34 724 debug(90, 3) ("CheckQuickAbort2: entry=%p, mem=%p\n", entry, mem);
62e76326 725
0e5bd28f 726 if (mem->request && !mem->request->flags.cachable) {
62e76326 727 debug(90, 3) ("CheckQuickAbort2: YES !mem->request->flags.cachable\n");
728 return 1;
77b32a34 729 }
62e76326 730
77b32a34 731 if (EBIT_TEST(entry->flags, KEY_PRIVATE)) {
62e76326 732 debug(90, 3) ("CheckQuickAbort2: YES KEY_PRIVATE\n");
733 return 1;
77b32a34 734 }
62e76326 735
528b2c61 736 expectlen = entry->getReply()->content_length + entry->getReply()->hdr_sz;
0e3f3e0d 737
738 if (expectlen < 0)
739 /* expectlen is < 0 if *no* information about the object has been recieved */
740 return 1;
741
528b2c61 742 curlen = (size_t) mem->endOffset ();
0e3f3e0d 743
e6ccf245 744 minlen = (size_t) Config.quickAbort.min << 10;
62e76326 745
77b32a34 746 if (minlen < 0) {
62e76326 747 debug(90, 3) ("CheckQuickAbort2: NO disabled\n");
748 return 0;
77b32a34 749 }
62e76326 750
77b32a34 751 if (curlen > expectlen) {
62e76326 752 debug(90, 3) ("CheckQuickAbort2: YES bad content length\n");
753 return 1;
77b32a34 754 }
62e76326 755
77b32a34 756 if ((expectlen - curlen) < minlen) {
62e76326 757 debug(90, 3) ("CheckQuickAbort2: NO only little more left\n");
758 return 0;
77b32a34 759 }
62e76326 760
77b32a34 761 if ((expectlen - curlen) > (Config.quickAbort.max << 10)) {
62e76326 762 debug(90, 3) ("CheckQuickAbort2: YES too much left to go\n");
763 return 1;
77b32a34 764 }
62e76326 765
77b32a34 766 if (expectlen < 100) {
62e76326 767 debug(90, 3) ("CheckQuickAbort2: NO avoid FPE\n");
768 return 0;
77b32a34 769 }
62e76326 770
e6ccf245 771 if ((curlen / (expectlen / 100)) > (size_t)Config.quickAbort.pct) {
62e76326 772 debug(90, 3) ("CheckQuickAbort2: NO past point of no return\n");
773 return 0;
77b32a34 774 }
62e76326 775
2f44bd34 776 debug(90, 3) ("CheckQuickAbort2: YES default, returning 1\n");
77b32a34 777 return 1;
778}
779
780static void
781CheckQuickAbort(StoreEntry * entry)
782{
528b2c61 783 assert (entry);
62e76326 784
77b32a34 785 if (storePendingNClients(entry) > 0)
62e76326 786 return;
787
77b32a34 788 if (entry->store_status != STORE_PENDING)
62e76326 789 return;
790
986ebffc 791 if (EBIT_TEST(entry->flags, ENTRY_SPECIAL))
62e76326 792 return;
793
77b32a34 794 if (CheckQuickAbort2(entry) == 0)
62e76326 795 return;
796
7197b20d 797 storeAbort(entry);
77b32a34 798}
c8be6d7b 799
800void
528b2c61 801store_client::dumpStats(StoreEntry * output, int clientNumber) const
c8be6d7b 802{
528b2c61 803 if (callbackPending())
62e76326 804 return;
805
528b2c61 806 storeAppendPrintf(output, "\tClient #%d, %p\n", clientNumber, _callback.callback_data);
62e76326 807
c8be6d7b 808 storeAppendPrintf(output, "\t\tcopy_offset: %lu\n",
62e76326 809 (unsigned long) copyInto.offset);
810
c8be6d7b 811 storeAppendPrintf(output, "\t\tcopy_size: %d\n",
62e76326 812 (int) copyInto.length);
813
c8be6d7b 814 storeAppendPrintf(output, "\t\tflags:");
62e76326 815
528b2c61 816 if (flags.disk_io_pending)
62e76326 817 storeAppendPrintf(output, " disk_io_pending");
818
528b2c61 819 if (flags.store_copying)
62e76326 820 storeAppendPrintf(output, " store_copying");
821
528b2c61 822 if (flags.copy_event_pending)
62e76326 823 storeAppendPrintf(output, " copy_event_pending");
824
c8be6d7b 825 storeAppendPrintf(output, "\n");
528b2c61 826}
c8be6d7b 827
528b2c61 828bool
829store_client::callbackPending() const
830{
831 return _callback.callback_handler && _callback.callback_data;
c8be6d7b 832}
528b2c61 833
834store_client::Callback::Callback(STCB *function, void *data) : callback_handler(function), callback_data (data) {}
b67e2c8c 835
515ec4dc 836#if DELAY_POOLS
b67e2c8c 837void
838store_client::setDelayId(DelayId delay_id)
839{
840 delayId = delay_id;
841}
62e76326 842
515ec4dc 843#endif