]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_swapout.cc
Various audit updates
[thirdparty/squid.git] / src / store_swapout.cc
CommitLineData
9cef6668 1
2/*
9cef6668 3 * DEBUG: section 20 Storage Manager Swapout Functions
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 *
32 */
33
582c2af2 34#include "squid.h"
aa839030 35#include "cbdata.h"
af69c635 36#include "globals.h"
e6ccf245 37#include "Store.h"
602d9612 38#include "StoreClient.h"
528b2c61 39/* FIXME: Abstract the use of this more */
40#include "mem_node.h"
41#include "MemObject.h"
4d5904f7 42#include "SquidConfig.h"
e4f1fdae 43#include "StatCounters.h"
10818c0a 44#include "store_log.h"
4b981814 45#include "swap_log_op.h"
602d9612 46#include "SwapDir.h"
f09f5b26 47
d54f0ab3 48static void storeSwapOutStart(StoreEntry * e);
4fcc8876 49static StoreIOState::STIOCB storeSwapOutFileClosed;
50static StoreIOState::STFNCB storeSwapOutFileNotify;
f09f5b26 51
daacd51f
AJ
52// wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
53static void xfree_cppwrapper(void *x)
54{
55 xfree(x);
56}
57
f09f5b26 58/* start swapping object to disk */
d54f0ab3 59static void
f09f5b26 60storeSwapOutStart(StoreEntry * e)
61{
2391a162 62 MemObject *mem = e->mem_obj;
d3b3ab85 63 StoreIOState::Pointer sio;
2391a162 64 assert(mem);
cd748f27 65 /* Build the swap metadata, so the filesystem will know how much
66 * metadata there is to store
67 */
26ac0430
AJ
68 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e->url() << "' to dirno " <<
69 e->swap_dirn << ", fileno " << std::hex << std::setw(8) << std::setfill('0') <<
bf8fe701 70 std::uppercase << e->swap_filen);
cd748f27 71 e->swap_status = SWAPOUT_WRITING;
0cdcf3d7 72 e->swapOutDecision(MemObject::SwapOut::swStarted);
528b2c61 73 /* If we start swapping out objects with OutOfBand Metadata,
74 * then this code needs changing
75 */
246e6cc1
AJ
76
77 /* TODO: make some sort of data,size refcounted immutable buffer
78 * and stop fooling ourselves with "const char*" buffers.
79 */
80
81 // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
82 const char *buf = e->getSerialisedMetaData ();
83 assert(buf);
84
cd748f27 85 /* Create the swap file */
aa839030 86 generic_cbdata *c = new generic_cbdata(e);
fa80a8ef 87 sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
62e76326 88
85a4b153 89 if (sio == NULL) {
62e76326 90 e->swap_status = SWAPOUT_NONE;
0cdcf3d7 91 e->swapOutDecision(MemObject::SwapOut::swImpossible);
aa839030 92 delete c;
246e6cc1 93 xfree((char*)buf);
62e76326 94 storeLog(STORE_LOG_SWAPOUTFAIL, e);
95 return;
3fc29499 96 }
62e76326 97
528b2c61 98 mem->swapout.sio = sio;
99 /* Don't lock until after create, or the replacement
100 * code might get confused */
34266cde 101
1bfe9ade 102 e->lock("storeSwapOutStart");
cd748f27 103 /* Pick up the file number if it was assigned immediately */
104 e->swap_filen = mem->swapout.sio->swap_filen;
34266cde 105
cd748f27 106 e->swap_dirn = mem->swapout.sio->swap_dirn;
34266cde 107
cd748f27 108 /* write out the swap metadata */
daacd51f 109 storeIOWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree_cppwrapper);
f09f5b26 110}
111
cd748f27 112static void
e5de8b13 113storeSwapOutFileNotify(void *data, int errflag, StoreIOState::Pointer self)
cd748f27 114{
e6ccf245 115 generic_cbdata *c = (generic_cbdata *)data;
116 StoreEntry *e = (StoreEntry *)c->data;
cd748f27 117 MemObject *mem = e->mem_obj;
118 assert(e->swap_status == SWAPOUT_WRITING);
119 assert(mem);
84177444 120 assert(mem->swapout.sio == self);
cd748f27 121 assert(errflag == 0);
f58bb2f4 122 assert(e->swap_filen < 0); // if this fails, call SwapDir::disconnect(e)
cd748f27 123 e->swap_filen = mem->swapout.sio->swap_filen;
124 e->swap_dirn = mem->swapout.sio->swap_dirn;
125}
126
90e8b325 127static bool
528b2c61 128doPages(StoreEntry *anEntry)
129{
130 MemObject *mem = anEntry->mem_obj;
62e76326 131
528b2c61 132 do {
aa1a691e
AR
133 // find the page containing the first byte we have not swapped out yet
134 mem_node *page =
135 mem->data_hdr.getBlockContainingLocation(mem->swapout.queue_offset);
62e76326 136
aa1a691e 137 if (!page)
90e8b325 138 break; // wait for more data to become available
aa1a691e
AR
139
140 // memNodeWriteComplete() and absence of buffer offset math below
141 // imply that we always write from the very beginning of the page
142 assert(page->start() == mem->swapout.queue_offset);
62e76326 143
144 /*
145 * Get the length of this buffer. We are assuming(!) that the buffer
146 * length won't change on this buffer, or things are going to be very
147 * strange. I think that after the copy to a buffer is done, the buffer
148 * size should stay fixed regardless so that this code isn't confused,
149 * but we can look at this at a later date or whenever the code results
150 * in bad swapouts, whichever happens first. :-)
151 */
aa1a691e 152 ssize_t swap_buf_len = page->nodeBuffer.length;
62e76326 153
4a7a3d56 154 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
62e76326 155
156 assert(swap_buf_len > 0);
157
4a7a3d56 158 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
62e76326 159
160 mem->swapout.queue_offset += swap_buf_len;
161
90e8b325
AR
162 // Quit if write() fails. Sio is going to call our callback, and that
163 // will cleanup, but, depending on the fs, that call may be async.
164 const bool ok = mem->swapout.sio->write(
9d4e9cfb
AR
165 mem->data_hdr.NodeGet(page),
166 swap_buf_len,
167 -1,
168 memNodeWriteComplete);
62e76326 169
90e8b325
AR
170 if (!ok || anEntry->swap_status != SWAPOUT_WRITING)
171 return false;
62e76326 172
47f6e231 173 int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
62e76326 174
175 if (anEntry->store_status == STORE_PENDING)
176 if (swapout_size < SM_PAGE_SIZE)
177 break;
178
179 if (swapout_size <= 0)
90e8b325 180 break;
528b2c61 181 } while (true);
90e8b325
AR
182
183 // either wait for more data or call swapOutFileClose()
184 return true;
528b2c61 185}
186
528b2c61 187/* This routine is called every time data is sent to the client side.
188 * It's overhead is therefor, significant.
189 */
f09f5b26 190void
c07cbbf4 191StoreEntry::swapOut()
f09f5b26 192{
c07cbbf4 193 if (!mem_obj)
62e76326 194 return;
195
5b55f1f1
CT
196 // this flag may change so we must check even if we are swappingOut
197 if (EBIT_TEST(flags, ENTRY_ABORTED)) {
198 assert(EBIT_TEST(flags, RELEASE_REQUEST));
199 // StoreEntry::abort() already closed the swap out file, if any
200 // no trimming: data producer must stop production if ENTRY_ABORTED
62e76326 201 return;
5b55f1f1
CT
202 }
203
204 const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
55bfc9fc 205
4475555f 206 Store::Root().memoryOut(*this, weAreOrMayBeSwappingOut);
5b55f1f1 207
2d4dea47 208 if (mem_obj->swapout.decision < MemObject::SwapOut::swPossible)
5b55f1f1 209 return; // nothing else to do
528b2c61 210
aa1a691e
AR
211 // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
212 // store_status == STORE_OK below means we got everything we wanted.
213
47f6e231 214 debugs(20, 7, HERE << "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
215 debugs(20, 7, HERE << "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
216 debugs(20, 7, HERE << "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
62e76326 217
85a4b153 218 if (mem_obj->swapout.sio != NULL)
26ac0430 219 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
62e76326 220
47f6e231 221 int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
62e76326 222
47f6e231 223 debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
62e76326 224
5aecb102 225#if SIZEOF_OFF_T <= 4
62e76326 226
c07cbbf4 227 if (mem_obj->endOffset() > 0x7FFF0000) {
fa84c01d 228 debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
bfb55b6f 229 abort();
62e76326 230 return;
adcceb47 231 }
62e76326 232
adcceb47 233#endif
c07cbbf4 234 if (swap_status == SWAPOUT_WRITING)
47f6e231 235 assert(mem_obj->inmem_lo <= mem_obj->objectBytesOnDisk() );
62e76326 236
5b55f1f1
CT
237 // buffered bytes we have not swapped out yet
238 const int64_t swapout_maxsize = mem_obj->availableForSwapOut();
239 assert(swapout_maxsize >= 0);
4a7a3d56 240 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
62e76326 241
aa1a691e
AR
242 if (swapout_maxsize == 0) { // swapped everything we got
243 if (store_status == STORE_OK) { // got everything we wanted
244 assert(mem_obj->object_sz >= 0);
245 swapOutFileClose(StoreIOState::wroteAll);
246 }
247 // else need more data to swap out
248 return;
1b72bda1 249 }
62e76326 250
c07cbbf4 251 if (store_status == STORE_PENDING) {
62e76326 252 /* wait for a full block to write */
253
254 if (swapout_maxsize < SM_PAGE_SIZE)
255 return;
256
257 /*
258 * Wait until we are below the disk FD limit, only if the
259 * next server-side read won't be deferred.
260 */
c07cbbf4 261 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
62e76326 262 return;
c47511fd 263 }
62e76326 264
2391a162 265 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
c07cbbf4 266 if (swap_status == SWAPOUT_NONE) {
267 assert(mem_obj->swapout.sio == NULL);
268 assert(mem_obj->inmem_lo == 0);
ddc9b32c 269 storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
f09f5b26 270 }
62e76326 271
c07cbbf4 272 if (mem_obj->swapout.sio == NULL)
62e76326 273 return;
274
90e8b325 275 if (!doPages(this))
62e76326 276 /* oops, we're not swapping out any more */
277 return;
278
c07cbbf4 279 if (store_status == STORE_OK) {
62e76326 280 /*
281 * If the state is STORE_OK, then all data must have been given
282 * to the filesystem at this point because storeSwapOut() is
283 * not going to be called again for this entry.
284 */
aa1a691e 285 assert(mem_obj->object_sz >= 0);
c07cbbf4 286 assert(mem_obj->endOffset() == mem_obj->swapout.queue_offset);
aa1a691e 287 swapOutFileClose(StoreIOState::wroteAll);
1792fbdb 288 }
f09f5b26 289}
290
291void
aa1a691e 292StoreEntry::swapOutFileClose(int how)
f09f5b26 293{
c07cbbf4 294 assert(mem_obj != NULL);
aa1a691e 295 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
bf8fe701 296 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
62e76326 297
c07cbbf4 298 if (mem_obj->swapout.sio == NULL)
62e76326 299 return;
300
aa1a691e 301 storeClose(mem_obj->swapout.sio, how);
f09f5b26 302}
303
304static void
e5de8b13 305storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
f09f5b26 306{
e6ccf245 307 generic_cbdata *c = (generic_cbdata *)data;
308 StoreEntry *e = (StoreEntry *)c->data;
25354045 309 MemObject *mem = e->mem_obj;
84177444 310 assert(mem->swapout.sio == self);
2391a162 311 assert(e->swap_status == SWAPOUT_WRITING);
312 cbdataFree(c);
62e76326 313
aa1a691e
AR
314 // if object_size is still unknown, the entry was probably aborted
315 if (errflag || e->objectLen() < 0) {
de4bc0ea 316 debugs(20, 2, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
26ac0430 317 std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
bf8fe701 318 e->swap_filen << ", errflag=" << errflag);
62e76326 319
320 if (errflag == DISK_NO_SPACE_LEFT) {
c8f4eac4 321 /* FIXME: this should be handle by the link from store IO to
322 * Store, rather than being a top level API call.
323 */
324 e->store()->diskFull();
62e76326 325 storeConfigure();
326 }
327
fa192c71 328 if (e->swap_filen >= 0)
c8f4eac4 329 e->unlink();
62e76326 330
f58bb2f4 331 assert(e->swap_status == SWAPOUT_NONE);
62e76326 332
d88e3c49 333 e->releaseRequest();
2391a162 334 } else {
62e76326 335 /* swapping complete */
26ac0430
AJ
336 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
337 e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
bf8fe701 338 std::uppercase << e->swap_filen);
aa1a691e
AR
339 debugs(20, 5, HERE << "swap_file_sz = " <<
340 e->objectLen() << " + " << mem->swap_hdr_sz);
8061c9b2 341
707fdc47 342 e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
62e76326 343 e->swap_status = SWAPOUT_DONE;
da9d3191 344 e->store()->swappedOut(*e);
62e76326 345
ddc9b32c
AR
346 // XXX: For some Stores, it is pointless to re-check cachability here
347 // and it leads to double counts in store_check_cachable_hist. We need
348 // another way to signal a completed but failed swapout. Or, better,
349 // each Store should handle its own logging and LOG state setting.
3900307b 350 if (e->checkCachable()) {
62e76326 351 storeLog(STORE_LOG_SWAPOUT, e);
352 storeDirSwapLog(e, SWAP_LOG_ADD);
353 }
354
e4f1fdae 355 ++statCounter.swap.outs;
f09f5b26 356 }
62e76326 357
bf8fe701 358 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
d3b3ab85 359 mem->swapout.sio = NULL;
1bfe9ade 360 e->unlock("storeSwapOutFileClosed");
f09f5b26 361}
61038223 362
c07cbbf4 363bool
5b55f1f1 364StoreEntry::mayStartSwapOut()
c2725718 365{
5b55f1f1
CT
366 // must be checked in the caller
367 assert(!EBIT_TEST(flags, ENTRY_ABORTED));
4094b311 368 assert(!swappingOut());
5b55f1f1
CT
369
370 if (!Config.cacheSwap.n_configured)
371 return false;
372
373 assert(mem_obj);
0cdcf3d7 374 const MemObject::SwapOut::Decision &decision = mem_obj->swapout.decision;
5b55f1f1 375
0e3b2ff0 376 // if we decided that starting is not possible, do not repeat same checks
5b55f1f1
CT
377 if (decision == MemObject::SwapOut::swImpossible) {
378 debugs(20, 3, HERE << " already rejected");
379 return false;
380 }
381
4094b311
AR
382 // if we swapped out already, do not start over
383 if (swap_status == SWAPOUT_DONE) {
539283df 384 debugs(20, 3, "already did");
0cdcf3d7 385 swapOutDecision(MemObject::SwapOut::swImpossible);
4094b311 386 return false;
5b55f1f1
CT
387 }
388
0e3b2ff0
AR
389 // if we stared swapping out already, do not start over
390 if (decision == MemObject::SwapOut::swStarted) {
391 debugs(20, 3, "already started");
0cdcf3d7 392 swapOutDecision(MemObject::SwapOut::swImpossible);
0e3b2ff0
AR
393 return false;
394 }
395
396 // if we decided that swapout is possible, do not repeat same checks
397 if (decision == MemObject::SwapOut::swPossible) {
398 debugs(20, 3, "already allowed");
399 return true;
400 }
401
5b55f1f1
CT
402 if (!checkCachable()) {
403 debugs(20, 3, HERE << "not cachable");
0cdcf3d7 404 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
405 return false;
406 }
407
408 if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
409 debugs(20, 3, HERE << url() << " SPECIAL");
0cdcf3d7 410 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
411 return false;
412 }
413
e0397786
AR
414 if (mem_obj->inmem_lo > 0) {
415 debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj->inmem_lo);
0cdcf3d7 416 swapOutDecision(MemObject::SwapOut::swImpossible);
e0397786
AR
417 return false;
418 }
419
420 if (!mem_obj->isContiguous()) {
421 debugs(20, 3, "storeSwapOut: not Contiguous");
0cdcf3d7 422 swapOutDecision(MemObject::SwapOut::swImpossible);
e0397786
AR
423 return false;
424 }
425
3c856e95
AR
426 // handle store_maxobjsize limit
427 {
5b55f1f1
CT
428 // TODO: add estimated store metadata size to be conservative
429
430 // use guaranteed maximum if it is known
431 const int64_t expectedEnd = mem_obj->expectedReplySize();
432 debugs(20, 7, HERE << "expectedEnd = " << expectedEnd);
433 if (expectedEnd > store_maxobjsize) {
434 debugs(20, 3, HERE << "will not fit: " << expectedEnd <<
435 " > " << store_maxobjsize);
0cdcf3d7 436 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
437 return false; // known to outgrow the limit eventually
438 }
439
440 // use current minimum (always known)
441 const int64_t currentEnd = mem_obj->endOffset();
442 if (currentEnd > store_maxobjsize) {
443 debugs(20, 3, HERE << "does not fit: " << currentEnd <<
444 " > " << store_maxobjsize);
0cdcf3d7 445 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
446 return false; // already does not fit and may only get bigger
447 }
448
e0397786
AR
449 // prevent final default swPossible answer for yet unknown length
450 if (expectedEnd < 0 && store_status != STORE_OK) {
451 const int64_t maxKnownSize = mem_obj->availableForSwapOut();
5b55f1f1 452 debugs(20, 7, HERE << "maxKnownSize= " << maxKnownSize);
2ee34428 453 /*
b51ec8c8
AJ
454 * NOTE: the store_maxobjsize here is the global maximum
455 * size of object cacheable in any of Squid cache stores
456 * both disk and memory stores.
457 *
458 * However, I am worried that this
2ee34428
A
459 * deferance may consume a lot of memory in some cases.
460 * Should we add an option to limit this memory consumption?
461 */
462 debugs(20, 5, HERE << "Deferring swapout start for " <<
463 (store_maxobjsize - maxKnownSize) << " bytes");
e0397786 464 return true; // may still fit, but no final decision yet
5b55f1f1 465 }
06d2839d 466 }
62e76326 467
0cdcf3d7 468 swapOutDecision(MemObject::SwapOut::swPossible);
c07cbbf4 469 return true;
c2725718 470}