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