]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_swapout.cc
Bug 419: Hop by Hop headers MUST NOT be forwarded (attempt 2)
[thirdparty/squid.git] / src / store_swapout.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 20 Storage Manager Swapout Functions
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
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 *
34 */
35
36 #include "squid.h"
37 #include "cbdata.h"
38 #include "StoreClient.h"
39 #include "Store.h"
40 /* FIXME: Abstract the use of this more */
41 #include "mem_node.h"
42 #include "MemObject.h"
43 #include "SwapDir.h"
44
45 static void storeSwapOutStart(StoreEntry * e);
46 static StoreIOState::STIOCB storeSwapOutFileClosed;
47 static StoreIOState::STFNCB storeSwapOutFileNotify;
48
49 /* start swapping object to disk */
50 static void
51 storeSwapOutStart(StoreEntry * e)
52 {
53 MemObject *mem = e->mem_obj;
54 StoreIOState::Pointer sio;
55 assert(mem);
56 /* Build the swap metadata, so the filesystem will know how much
57 * metadata there is to store
58 */
59 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e->url() << "' to dirno " <<
60 e->swap_dirn << ", fileno " << std::hex << std::setw(8) << std::setfill('0') <<
61 std::uppercase << e->swap_filen);
62 e->swap_status = SWAPOUT_WRITING;
63 /* If we start swapping out objects with OutOfBand Metadata,
64 * then this code needs changing
65 */
66 /* Create the swap file */
67 generic_cbdata *c = new generic_cbdata(e);
68 sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
69
70 if (sio == NULL) {
71 e->swap_status = SWAPOUT_NONE;
72 delete c;
73 storeLog(STORE_LOG_SWAPOUTFAIL, e);
74 return;
75 }
76
77 mem->swapout.sio = sio;
78 /* Don't lock until after create, or the replacement
79 * code might get confused */
80
81 e->lock();
82 /* Pick up the file number if it was assigned immediately */
83 e->swap_filen = mem->swapout.sio->swap_filen;
84
85 e->swap_dirn = mem->swapout.sio->swap_dirn;
86
87 /* write out the swap metadata */
88 /* TODO: make some sort of data,size refcounted immutable buffer
89 * for use by this sort of function.
90 */
91 char const *buf = e->getSerialisedMetaData ();
92
93 /* If we start swapping out with out of band metadata, this assert
94 * will catch it - this code needs to be adjusted if that happens
95 */
96 assert (buf);
97
98 storeIOWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree);
99 }
100
101 static void
102 storeSwapOutFileNotify(void *data, int errflag, StoreIOState::Pointer self)
103 {
104 generic_cbdata *c = (generic_cbdata *)data;
105 StoreEntry *e = (StoreEntry *)c->data;
106 MemObject *mem = e->mem_obj;
107 assert(e->swap_status == SWAPOUT_WRITING);
108 assert(mem);
109 assert(mem->swapout.sio == self);
110 assert(errflag == 0);
111 e->swap_filen = mem->swapout.sio->swap_filen;
112 e->swap_dirn = mem->swapout.sio->swap_dirn;
113 }
114
115 static void
116 doPages(StoreEntry *anEntry)
117 {
118 MemObject *mem = anEntry->mem_obj;
119
120 do {
121 /*
122 * Evil hack time.
123 * We are paging out to disk in page size chunks. however, later on when
124 * we update the queue position, we might not have a page (I *think*),
125 * so we do the actual page update here.
126 */
127
128 if (mem->swapout.memnode == NULL) {
129 /* We need to swap out the first page */
130 mem->swapout.memnode = const_cast<mem_node *>(mem->data_hdr.start());
131 } else {
132 /* We need to swap out the next page */
133 /* 20030636 RBC - we don't have ->next anymore.
134 * But we do have the next location */
135 mem->swapout.memnode = mem->data_hdr.getBlockContainingLocation (mem->swapout.memnode->end());
136 }
137
138 /*
139 * Get the length of this buffer. We are assuming(!) that the buffer
140 * length won't change on this buffer, or things are going to be very
141 * strange. I think that after the copy to a buffer is done, the buffer
142 * size should stay fixed regardless so that this code isn't confused,
143 * but we can look at this at a later date or whenever the code results
144 * in bad swapouts, whichever happens first. :-)
145 */
146 ssize_t swap_buf_len = mem->swapout.memnode->nodeBuffer.length;
147
148 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
149
150 assert(swap_buf_len > 0);
151
152 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
153
154 mem->swapout.queue_offset += swap_buf_len;
155
156 storeIOWrite(mem->swapout.sio,
157 mem->data_hdr.NodeGet(mem->swapout.memnode),
158 swap_buf_len,
159 -1,
160 memNodeWriteComplete);
161
162 /* the storeWrite() call might generate an error */
163 if (anEntry->swap_status != SWAPOUT_WRITING)
164 break;
165
166 int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
167
168 if (anEntry->store_status == STORE_PENDING)
169 if (swapout_size < SM_PAGE_SIZE)
170 break;
171
172 if (swapout_size <= 0)
173 return;
174 } while (true);
175 }
176
177
178 /* This routine is called every time data is sent to the client side.
179 * It's overhead is therefor, significant.
180 */
181 void
182 StoreEntry::swapOut()
183 {
184 if (!mem_obj)
185 return;
186
187 if (!swapoutPossible())
188 return;
189
190 debugs(20, 7, HERE << "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
191 debugs(20, 7, HERE << "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
192 debugs(20, 7, HERE << "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
193
194 if (mem_obj->swapout.sio != NULL)
195 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
196
197 int64_t swapout_maxsize = mem_obj->endOffset() - mem_obj->swapout.queue_offset;
198
199 assert(swapout_maxsize >= 0);
200
201 int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
202
203 debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
204
205 /*
206 * Grab the swapout_size and check to see whether we're going to defer
207 * the swapout based upon size
208 */
209 if ((store_status != STORE_OK) && (swapout_maxsize < store_maxobjsize)) {
210 /*
211 * NOTE: the store_maxobjsize here is the max of optional
212 * max-size values from 'cache_dir' lines. It is not the
213 * same as 'maximum_object_size'. By default, store_maxobjsize
214 * will be set to -1. However, I am worried that this
215 * deferance may consume a lot of memory in some cases.
216 * It would be good to make this decision based on reply
217 * content-length, rather than wait to accumulate huge
218 * amounts of object data in memory.
219 */
220 debugs(20, 5, "storeSwapOut: Deferring starting swapping out");
221 return;
222 }
223
224 trimMemory();
225 #if SIZEOF_OFF_T <= 4
226
227 if (mem_obj->endOffset() > 0x7FFF0000) {
228 debugs(20, 0, "WARNING: preventing off_t overflow for " << url());
229 abort();
230 return;
231 }
232
233 #endif
234 if (swap_status == SWAPOUT_WRITING)
235 assert(mem_obj->inmem_lo <= mem_obj->objectBytesOnDisk() );
236
237 if (!swapOutAble())
238 return;
239
240 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
241
242 if (swapout_maxsize == 0) {
243 if (store_status == STORE_OK)
244 swapOutFileClose();
245
246 return; /* Nevermore! */
247 }
248
249 if (store_status == STORE_PENDING) {
250 /* wait for a full block to write */
251
252 if (swapout_maxsize < SM_PAGE_SIZE)
253 return;
254
255 /*
256 * Wait until we are below the disk FD limit, only if the
257 * next server-side read won't be deferred.
258 */
259 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
260 return;
261 }
262
263 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
264 if (swap_status == SWAPOUT_NONE) {
265 assert(mem_obj->swapout.sio == NULL);
266 assert(mem_obj->inmem_lo == 0);
267
268 if (checkCachable())
269 storeSwapOutStart(this);
270 else
271 return;
272
273 /* ENTRY_CACHABLE will be cleared and we'll never get here again */
274 }
275
276 if (mem_obj->swapout.sio == NULL)
277 return;
278
279 doPages(this);
280
281 if (mem_obj->swapout.sio == NULL)
282 /* oops, we're not swapping out any more */
283 return;
284
285 if (store_status == STORE_OK) {
286 /*
287 * If the state is STORE_OK, then all data must have been given
288 * to the filesystem at this point because storeSwapOut() is
289 * not going to be called again for this entry.
290 */
291 assert(mem_obj->endOffset() == mem_obj->swapout.queue_offset);
292 swapOutFileClose();
293 }
294 }
295
296 void
297 StoreEntry::swapOutFileClose()
298 {
299 assert(mem_obj != NULL);
300 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text());
301 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
302
303 if (mem_obj->swapout.sio == NULL)
304 return;
305
306 storeClose(mem_obj->swapout.sio);
307 }
308
309 static void
310 storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
311 {
312 generic_cbdata *c = (generic_cbdata *)data;
313 StoreEntry *e = (StoreEntry *)c->data;
314 MemObject *mem = e->mem_obj;
315 assert(mem->swapout.sio == self);
316 assert(e->swap_status == SWAPOUT_WRITING);
317 cbdataFree(c);
318
319 if (errflag) {
320 debugs(20, 1, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
321 std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
322 e->swap_filen << ", errflag=" << errflag);
323 debugs(20, 1, "\t" << xstrerror());
324
325 if (errflag == DISK_NO_SPACE_LEFT) {
326 /* FIXME: this should be handle by the link from store IO to
327 * Store, rather than being a top level API call.
328 */
329 e->store()->diskFull();
330 storeConfigure();
331 }
332
333 if (e->swap_filen > 0)
334 e->unlink();
335
336 e->swap_filen = -1;
337
338 e->swap_dirn = -1;
339
340 e->swap_status = SWAPOUT_NONE;
341
342 e->releaseRequest();
343 } else {
344 /* swapping complete */
345 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
346 e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
347 std::uppercase << e->swap_filen);
348 e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
349 e->swap_status = SWAPOUT_DONE;
350 e->store()->updateSize(e->swap_file_sz, 1);
351
352 if (e->checkCachable()) {
353 storeLog(STORE_LOG_SWAPOUT, e);
354 storeDirSwapLog(e, SWAP_LOG_ADD);
355 }
356
357 statCounter.swap.outs++;
358 }
359
360 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
361 mem->swapout.sio = NULL;
362 e->unlock();
363 }
364
365 /*
366 * Is this entry a candidate for writing to disk?
367 */
368 bool
369 StoreEntry::swapOutAble() const
370 {
371 dlink_node *node;
372
373 if (mem_obj->swapout.sio != NULL)
374 return true;
375
376 if (mem_obj->inmem_lo > 0)
377 return false;
378
379 /*
380 * If there are DISK clients, we must write to disk
381 * even if its not cachable
382 * RBC: Surely we should not create disk client on non cacheable objects?
383 * therefore this should be an assert?
384 * RBC 20030708: We can use disk to avoid mem races, so this shouldn't be
385 * an assert.
386 */
387 for (node = mem_obj->clients.head; node; node = node->next) {
388 if (((store_client *) node->data)->getType() == STORE_DISK_CLIENT)
389 return true;
390 }
391
392 /* Don't pollute the disk with icons and other special entries */
393 if (EBIT_TEST(flags, ENTRY_SPECIAL))
394 return false;
395
396 if (!EBIT_TEST(flags, ENTRY_CACHABLE))
397 return false;
398
399 if (!mem_obj->isContiguous())
400 return false;
401
402 return true;
403 }