]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_swapout.cc
MODIO_1 commit. This change (including documentation) implements a more
[thirdparty/squid.git] / src / store_swapout.cc
1
2 /*
3 * $Id: store_swapout.cc,v 1.65 2000/05/03 17:15:44 adrian Exp $
4 *
5 * DEBUG: section 20 Storage Manager Swapout Functions
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * 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
38 static off_t storeSwapOutObjectBytesOnDisk(const MemObject *);
39 static void storeSwapOutStart(StoreEntry * e);
40 static STIOCB storeSwapOutFileClosed;
41 static STIOCB storeSwapOutFileNotify;
42
43 /* start swapping object to disk */
44 static void
45 storeSwapOutStart(StoreEntry * e)
46 {
47 generic_cbdata *c;
48 MemObject *mem = e->mem_obj;
49 int swap_hdr_sz = 0;
50 tlv *tlv_list;
51 char *buf;
52 assert(mem);
53 /* Build the swap metadata, so the filesystem will know how much
54 * metadata there is to store
55 */
56 debug(20, 5) ("storeSwapOutStart: Begin SwapOut '%s' to dirno %d, fileno %08X\n",
57 storeUrl(e), e->swap_dirn, e->swap_filen);
58 e->swap_status = SWAPOUT_WRITING;
59 tlv_list = storeSwapMetaBuild(e);
60 buf = storeSwapMetaPack(tlv_list, &swap_hdr_sz);
61 storeSwapTLVFree(tlv_list);
62 mem->swap_hdr_sz = (size_t) swap_hdr_sz;
63 /* Create the swap file */
64 c = xcalloc(1, sizeof(*c));
65 c->data = e;
66 cbdataAdd(c, cbdataXfree, 0);
67 mem->swapout.sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
68 if (NULL == mem->swapout.sio) {
69 e->swap_status = SWAPOUT_NONE;
70 cbdataFree(c);
71 xfree(buf);
72 return;
73 }
74 storeLockObject(e); /* Don't lock until after create, or the replacement
75 * code might get confused */
76 /* Pick up the file number if it was assigned immediately */
77 e->swap_filen = mem->swapout.sio->swap_filen;
78 e->swap_dirn = mem->swapout.sio->swap_dirn;
79 /* write out the swap metadata */
80 cbdataLock(mem->swapout.sio);
81 storeWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree);
82 }
83
84 static void
85 storeSwapOutFileNotify(void *data, int errflag, storeIOState * sio)
86 {
87 generic_cbdata *c = data;
88 StoreEntry *e = c->data;
89 MemObject *mem = e->mem_obj;
90 assert(e->swap_status == SWAPOUT_WRITING);
91 assert(mem);
92 assert(mem->swapout.sio == sio);
93 assert(errflag == 0);
94 e->swap_filen = mem->swapout.sio->swap_filen;
95 e->swap_dirn = mem->swapout.sio->swap_dirn;
96 }
97
98 void
99 storeSwapOut(StoreEntry * e)
100 {
101 MemObject *mem = e->mem_obj;
102 off_t lowest_offset;
103 off_t new_mem_lo;
104 off_t on_disk = 0;
105 size_t swapout_size;
106 ssize_t swap_buf_len;
107 if (mem == NULL)
108 return;
109 /* should we swap something out to disk? */
110 debug(20, 7) ("storeSwapOut: %s\n", storeUrl(e));
111 debug(20, 7) ("storeSwapOut: store_status = %s\n",
112 storeStatusStr[e->store_status]);
113 if (EBIT_TEST(e->flags, ENTRY_ABORTED)) {
114 assert(EBIT_TEST(e->flags, RELEASE_REQUEST));
115 storeSwapOutFileClose(e);
116 return;
117 }
118 debug(20, 7) ("storeSwapOut: mem->inmem_lo = %d\n",
119 (int) mem->inmem_lo);
120 debug(20, 7) ("storeSwapOut: mem->inmem_hi = %d\n",
121 (int) mem->inmem_hi);
122 debug(20, 7) ("storeSwapOut: swapout.queue_offset = %d\n",
123 (int) mem->swapout.queue_offset);
124 if (mem->swapout.sio)
125 debug(20, 7) ("storeSwapOut: storeOffset() = %d\n",
126 (int) storeOffset(mem->swapout.sio));
127 assert(mem->inmem_hi >= mem->swapout.queue_offset);
128 lowest_offset = storeLowestMemReaderOffset(e);
129 debug(20, 7) ("storeSwapOut: lowest_offset = %d\n",
130 (int) lowest_offset);
131 /*
132 * Grab the swapout_size and check to see whether we're going to defer
133 * the swapout based upon size
134 */
135 swapout_size = (size_t) (mem->inmem_hi - mem->swapout.queue_offset);
136 if ((e->store_status != STORE_OK) && (swapout_size < store_maxobjsize)) {
137 debug (20, 5) ("storeSwapOut: Deferring starting swapping out\n");
138 return;
139 }
140 /*
141 * Careful. lowest_offset can be greater than inmem_hi, such
142 * as in the case of a range request.
143 */
144 if (mem->inmem_hi < lowest_offset)
145 new_mem_lo = lowest_offset;
146 else if (mem->inmem_hi - lowest_offset > SM_PAGE_SIZE)
147 new_mem_lo = lowest_offset;
148 else
149 new_mem_lo = mem->inmem_lo;
150 assert(new_mem_lo >= mem->inmem_lo);
151 if (storeSwapOutAble(e)) {
152 /*
153 * We should only free up to what we know has been written
154 * to disk, not what has been queued for writing. Otherwise
155 * there will be a chunk of the data which is not in memory
156 * and is not yet on disk.
157 */
158 if ((on_disk = storeSwapOutObjectBytesOnDisk(mem)) < new_mem_lo)
159 new_mem_lo = on_disk;
160 } else if (new_mem_lo > 0) {
161 /*
162 * Its not swap-able, and we're about to delete a chunk,
163 * so we must make it PRIVATE. This is tricky/ugly because
164 * for the most part, we treat swapable == cachable here.
165 */
166 storeReleaseRequest(e);
167 }
168 stmemFreeDataUpto(&mem->data_hdr, new_mem_lo);
169 mem->inmem_lo = new_mem_lo;
170 if (e->swap_status == SWAPOUT_WRITING)
171 assert(mem->inmem_lo <= on_disk);
172 if (!storeSwapOutAble(e))
173 return;
174 debug(20, 7) ("storeSwapOut: swapout_size = %d\n",
175 (int) swapout_size);
176 if (swapout_size == 0) {
177 if (e->store_status == STORE_OK)
178 storeSwapOutFileClose(e);
179 return; /* Nevermore! */
180 }
181 if (e->store_status == STORE_PENDING) {
182 /* wait for a full block to write */
183 if (swapout_size < SM_PAGE_SIZE)
184 return;
185 /*
186 * Wait until we are below the disk FD limit, only if the
187 * next server-side read won't be deferred.
188 */
189 if (storeTooManyDiskFilesOpen() && !fwdCheckDeferRead(-1, e))
190 return;
191 }
192 /*
193 * Don't start swapping out until its either all in memory, or bigger
194 * than the maximum object size (so we pick a -1 maxobjsize fs)
195 */
196 if ((e->store_status != STORE_OK) && (swapout_size < store_maxobjsize)) {
197 debug (20, 5) ("storeSwapOut: Deferring starting swapping out\n");
198 return;
199 }
200 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
201 if (e->swap_status == SWAPOUT_NONE) {
202 assert(mem->swapout.sio == NULL);
203 assert(mem->inmem_lo == 0);
204 if (storeCheckCachable(e))
205 storeSwapOutStart(e);
206 else
207 return;
208 /* ENTRY_CACHABLE will be cleared and we'll never get here again */
209 }
210 if (NULL == mem->swapout.sio)
211 return;
212 do {
213 /*
214 * Evil hack time.
215 * We are paging out to disk in page size chunks. however, later on when
216 * we update the queue position, we might not have a page (I *think*),
217 * so we do the actual page update here.
218 */
219
220 if (mem->swapout.memnode == NULL) {
221 /* We need to swap out the first page */
222 mem->swapout.memnode = mem->data_hdr.head;
223 } else {
224 /* We need to swap out the next page */
225 mem->swapout.memnode = mem->swapout.memnode->next;
226 }
227 /*
228 * Get the length of this buffer. We are assuming(!) that the buffer
229 * length won't change on this buffer, or things are going to be very
230 * strange. I think that after the copy to a buffer is done, the buffer
231 * size should stay fixed regardless so that this code isn't confused,
232 * but we can look at this at a later date or whenever the code results
233 * in bad swapouts, whichever happens first. :-)
234 */
235 swap_buf_len = mem->swapout.memnode->len;
236
237 debug(20, 3) ("storeSwapOut: swap_buf_len = %d\n", (int) swap_buf_len);
238 assert(swap_buf_len > 0);
239 debug(20, 3) ("storeSwapOut: swapping out %d bytes from %d\n",
240 swap_buf_len, (int) mem->swapout.queue_offset);
241 mem->swapout.queue_offset += swap_buf_len;
242 storeWrite(mem->swapout.sio, mem->swapout.memnode->data, swap_buf_len, -1, NULL);
243 /* the storeWrite() call might generate an error */
244 if (e->swap_status != SWAPOUT_WRITING)
245 break;
246 swapout_size = (size_t) (mem->inmem_hi - mem->swapout.queue_offset);
247 if (e->store_status == STORE_PENDING)
248 if (swapout_size < SM_PAGE_SIZE)
249 break;
250 } while (swapout_size > 0);
251 if (NULL == mem->swapout.sio)
252 /* oops, we're not swapping out any more */
253 return;
254 if (e->store_status == STORE_OK) {
255 /*
256 * If the state is STORE_OK, then all data must have been given
257 * to the filesystem at this point because storeSwapOut() is
258 * not going to be called again for this entry.
259 */
260 assert(mem->inmem_hi == mem->swapout.queue_offset);
261 storeSwapOutFileClose(e);
262 }
263 }
264
265 void
266 storeSwapOutFileClose(StoreEntry * e)
267 {
268 MemObject *mem = e->mem_obj;
269 assert(mem != NULL);
270 debug(20, 3) ("storeSwapOutFileClose: %s\n", storeKeyText(e->key));
271 debug(20, 3) ("storeSwapOutFileClose: sio = %p\n", mem->swapout.sio);
272 if (mem->swapout.sio == NULL)
273 return;
274 storeClose(mem->swapout.sio);
275 }
276
277 static void
278 storeSwapOutFileClosed(void *data, int errflag, storeIOState * sio)
279 {
280 generic_cbdata *c = data;
281 StoreEntry *e = c->data;
282 MemObject *mem = e->mem_obj;
283 assert(e->swap_status == SWAPOUT_WRITING);
284 cbdataFree(c);
285 if (errflag) {
286 debug(20, 1) ("storeSwapOutFileClosed: dirno %d, swapfile %08X, errflag=%d\n\t%s\n",
287 e->swap_dirn, e->swap_filen, errflag, xstrerror());
288 if (errflag == DISK_NO_SPACE_LEFT) {
289 storeDirDiskFull(e->swap_dirn);
290 storeDirConfigure();
291 storeConfigure();
292 }
293 if (e->swap_filen > 0)
294 storeUnlink(e);
295 e->swap_filen = -1;
296 e->swap_dirn = -1;
297 e->swap_status = SWAPOUT_NONE;
298 storeReleaseRequest(e);
299 } else {
300 /* swapping complete */
301 debug(20, 3) ("storeSwapOutFileClosed: SwapOut complete: '%s' to %d, %08X\n",
302 storeUrl(e), e->swap_dirn, e->swap_filen);
303 e->swap_file_sz = objectLen(e) + mem->swap_hdr_sz;
304 e->swap_status = SWAPOUT_DONE;
305 storeDirUpdateSwapSize(&Config.cacheSwap.swapDirs[e->swap_dirn], e->swap_file_sz, 1);
306 if (storeCheckCachable(e)) {
307 storeLog(STORE_LOG_SWAPOUT, e);
308 storeDirSwapLog(e, SWAP_LOG_ADD);
309 }
310 }
311 debug(20, 3) ("storeSwapOutFileClosed: %s:%d\n", __FILE__, __LINE__);
312 mem->swapout.sio = NULL;
313 cbdataUnlock(sio);
314 storeUnlockObject(e);
315 }
316
317 /*
318 * How much of the object data is on the disk?
319 */
320 static off_t
321 storeSwapOutObjectBytesOnDisk(const MemObject * mem)
322 {
323 /*
324 * NOTE: storeOffset() represents the disk file size,
325 * not the amount of object data on disk.
326 *
327 * If we don't have at least 'swap_hdr_sz' bytes
328 * then none of the object data is on disk.
329 *
330 * This should still be safe if swap_hdr_sz == 0,
331 * meaning we haven't even opened the swapout file
332 * yet.
333 */
334 off_t nwritten;
335 if (mem->swapout.sio == NULL)
336 return 0;
337 nwritten = storeOffset(mem->swapout.sio);
338 if (nwritten <= mem->swap_hdr_sz)
339 return 0;
340 return nwritten - mem->swap_hdr_sz;
341 }
342
343 /*
344 * Is this entry a candidate for writing to disk?
345 */
346 int
347 storeSwapOutAble(const StoreEntry * e)
348 {
349 store_client *sc;
350 if (e->mem_obj->swapout.sio != NULL)
351 return 1;
352 if (e->mem_obj->inmem_lo > 0)
353 return 0;
354 /*
355 * If there are DISK clients, we must write to disk
356 * even if its not cachable
357 */
358 for (sc = e->mem_obj->clients; sc; sc = sc->next)
359 if (sc->type == STORE_DISK_CLIENT)
360 return 1;
361 if (store_dirs_rebuilding)
362 if (!EBIT_TEST(e->flags, ENTRY_SPECIAL))
363 return 0;
364 return EBIT_TEST(e->flags, ENTRY_CACHABLE);
365 }