]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 1996-2015 The Squid Software Foundation and contributors | |
3 | * | |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
9 | /* DEBUG: section 06 Disk I/O Routines */ | |
10 | ||
11 | #include "squid.h" | |
12 | #include "comm/Loops.h" | |
13 | #include "disk.h" | |
14 | #include "fd.h" | |
15 | #include "fde.h" | |
16 | #include "globals.h" | |
17 | #include "MemBuf.h" | |
18 | #include "profiler/Profiler.h" | |
19 | #include "StatCounters.h" | |
20 | ||
21 | #include <cerrno> | |
22 | ||
23 | static PF diskHandleRead; | |
24 | static PF diskHandleWrite; | |
25 | ||
26 | #if _SQUID_WINDOWS_ || _SQUID_OS2_ | |
27 | static int | |
28 | diskWriteIsComplete(int fd) | |
29 | { | |
30 | return fd_table[fd].disk.write_q ? 0 : 1; | |
31 | } | |
32 | ||
33 | #endif | |
34 | ||
35 | void | |
36 | disk_init(void) | |
37 | { | |
38 | (void) 0; | |
39 | } | |
40 | ||
41 | /* hack needed on SunStudio to avoid linkage convention mismatch */ | |
42 | static void cxx_xfree(void *ptr) | |
43 | { | |
44 | xfree(ptr); | |
45 | } | |
46 | ||
47 | /* | |
48 | * opens a disk file specified by 'path'. This function always | |
49 | * blocks! There is no callback. | |
50 | */ | |
51 | int | |
52 | file_open(const char *path, int mode) | |
53 | { | |
54 | int fd; | |
55 | PROF_start(file_open); | |
56 | ||
57 | if (FILE_MODE(mode) == O_WRONLY) | |
58 | mode |= O_APPEND; | |
59 | ||
60 | errno = 0; | |
61 | ||
62 | fd = open(path, mode, 0644); | |
63 | ||
64 | ++ statCounter.syscalls.disk.opens; | |
65 | ||
66 | if (fd < 0) { | |
67 | debugs(50, 3, "file_open: error opening file " << path << ": " << xstrerror()); | |
68 | fd = DISK_ERROR; | |
69 | } else { | |
70 | debugs(6, 5, "file_open: FD " << fd); | |
71 | commSetCloseOnExec(fd); | |
72 | fd_open(fd, FD_FILE, path); | |
73 | } | |
74 | ||
75 | PROF_stop(file_open); | |
76 | return fd; | |
77 | } | |
78 | ||
79 | /* close a disk file. */ | |
80 | void | |
81 | file_close(int fd) | |
82 | { | |
83 | fde *F = &fd_table[fd]; | |
84 | PF *read_callback; | |
85 | PROF_start(file_close); | |
86 | assert(fd >= 0); | |
87 | assert(F->flags.open); | |
88 | ||
89 | if ((read_callback = F->read_handler)) { | |
90 | F->read_handler = NULL; | |
91 | read_callback(-1, F->read_data); | |
92 | } | |
93 | ||
94 | if (F->flags.write_daemon) { | |
95 | #if _SQUID_WINDOWS_ || _SQUID_OS2_ | |
96 | /* | |
97 | * on some operating systems, you can not delete or rename | |
98 | * open files, so we won't allow delayed close. | |
99 | */ | |
100 | while (!diskWriteIsComplete(fd)) | |
101 | diskHandleWrite(fd, NULL); | |
102 | #else | |
103 | F->flags.close_request = true; | |
104 | debugs(6, 2, "file_close: FD " << fd << ", delaying close"); | |
105 | PROF_stop(file_close); | |
106 | return; | |
107 | #endif | |
108 | ||
109 | } | |
110 | ||
111 | /* | |
112 | * Assert there is no write callback. Otherwise we might be | |
113 | * leaking write state data by closing the descriptor | |
114 | */ | |
115 | assert(F->write_handler == NULL); | |
116 | ||
117 | #if CALL_FSYNC_BEFORE_CLOSE | |
118 | ||
119 | fsync(fd); | |
120 | ||
121 | #endif | |
122 | ||
123 | close(fd); | |
124 | ||
125 | debugs(6, F->flags.close_request ? 2 : 5, "file_close: FD " << fd << " really closing\n"); | |
126 | ||
127 | fd_close(fd); | |
128 | ||
129 | ++ statCounter.syscalls.disk.closes; | |
130 | ||
131 | PROF_stop(file_close); | |
132 | } | |
133 | ||
134 | /* | |
135 | * This function has the purpose of combining multiple writes. This is | |
136 | * to facilitate the ASYNC_IO option since it can only guarantee 1 | |
137 | * write to a file per trip around the comm.c select() loop. That's bad | |
138 | * because more than 1 write can be made to the access.log file per | |
139 | * trip, and so this code is purely designed to help batch multiple | |
140 | * sequential writes to the access.log file. Squid will never issue | |
141 | * multiple writes for any other file type during 1 trip around the | |
142 | * select() loop. --SLF | |
143 | */ | |
144 | static void | |
145 | diskCombineWrites(_fde_disk *fdd) | |
146 | { | |
147 | /* | |
148 | * We need to combine multiple write requests on an FD's write | |
149 | * queue But only if we don't need to seek() in between them, ugh! | |
150 | * XXX This currently ignores any seeks (file_offset) | |
151 | */ | |
152 | ||
153 | if (fdd->write_q != NULL && fdd->write_q->next != NULL) { | |
154 | int len = 0; | |
155 | ||
156 | for (dwrite_q *q = fdd->write_q; q != NULL; q = q->next) | |
157 | len += q->len - q->buf_offset; | |
158 | ||
159 | dwrite_q *wq = (dwrite_q *)memAllocate(MEM_DWRITE_Q); | |
160 | ||
161 | wq->buf = (char *)xmalloc(len); | |
162 | ||
163 | wq->len = 0; | |
164 | ||
165 | wq->buf_offset = 0; | |
166 | ||
167 | wq->next = NULL; | |
168 | ||
169 | wq->free_func = cxx_xfree; | |
170 | ||
171 | while (fdd->write_q != NULL) { | |
172 | dwrite_q *q = fdd->write_q; | |
173 | ||
174 | len = q->len - q->buf_offset; | |
175 | memcpy(wq->buf + wq->len, q->buf + q->buf_offset, len); | |
176 | wq->len += len; | |
177 | fdd->write_q = q->next; | |
178 | ||
179 | if (q->free_func) | |
180 | q->free_func(q->buf); | |
181 | ||
182 | memFree(q, MEM_DWRITE_Q); | |
183 | }; | |
184 | ||
185 | fdd->write_q_tail = wq; | |
186 | ||
187 | fdd->write_q = wq; | |
188 | } | |
189 | } | |
190 | ||
191 | /* write handler */ | |
192 | static void | |
193 | diskHandleWrite(int fd, void *) | |
194 | { | |
195 | int len = 0; | |
196 | fde *F = &fd_table[fd]; | |
197 | ||
198 | _fde_disk *fdd = &F->disk; | |
199 | dwrite_q *q = fdd->write_q; | |
200 | int status = DISK_OK; | |
201 | bool do_close; | |
202 | ||
203 | if (NULL == q) | |
204 | return; | |
205 | ||
206 | PROF_start(diskHandleWrite); | |
207 | ||
208 | debugs(6, 3, "diskHandleWrite: FD " << fd); | |
209 | ||
210 | F->flags.write_daemon = false; | |
211 | ||
212 | assert(fdd->write_q != NULL); | |
213 | ||
214 | assert(fdd->write_q->len > fdd->write_q->buf_offset); | |
215 | ||
216 | debugs(6, 3, "diskHandleWrite: FD " << fd << " writing " << | |
217 | (fdd->write_q->len - fdd->write_q->buf_offset) << " bytes at " << | |
218 | fdd->write_q->file_offset); | |
219 | ||
220 | errno = 0; | |
221 | ||
222 | if (fdd->write_q->file_offset != -1) | |
223 | lseek(fd, fdd->write_q->file_offset, SEEK_SET); /* XXX ignore return? */ | |
224 | ||
225 | len = FD_WRITE_METHOD(fd, | |
226 | fdd->write_q->buf + fdd->write_q->buf_offset, | |
227 | fdd->write_q->len - fdd->write_q->buf_offset); | |
228 | ||
229 | debugs(6, 3, "diskHandleWrite: FD " << fd << " len = " << len); | |
230 | ||
231 | ++ statCounter.syscalls.disk.writes; | |
232 | ||
233 | fd_bytes(fd, len, FD_WRITE); | |
234 | ||
235 | if (len < 0) { | |
236 | if (!ignoreErrno(errno)) { | |
237 | status = errno == ENOSPC ? DISK_NO_SPACE_LEFT : DISK_ERROR; | |
238 | debugs(50, DBG_IMPORTANT, "diskHandleWrite: FD " << fd << ": disk write error: " << xstrerror()); | |
239 | ||
240 | /* | |
241 | * If there is no write callback, then this file is | |
242 | * most likely something important like a log file, or | |
243 | * an interprocess pipe. Its not a swapfile. We feel | |
244 | * that a write failure on a log file is rather important, | |
245 | * and Squid doesn't otherwise deal with this condition. | |
246 | * So to get the administrators attention, we exit with | |
247 | * a fatal message. | |
248 | */ | |
249 | ||
250 | if (fdd->wrt_handle == NULL) | |
251 | fatal("Write failure -- check your disk space and cache.log"); | |
252 | ||
253 | /* | |
254 | * If there is a write failure, then we notify the | |
255 | * upper layer via the callback, at the end of this | |
256 | * function. Meanwhile, flush all pending buffers | |
257 | * here. Let the upper layer decide how to handle the | |
258 | * failure. This will prevent experiencing multiple, | |
259 | * repeated write failures for the same FD because of | |
260 | * the queued data. | |
261 | */ | |
262 | do { | |
263 | fdd->write_q = q->next; | |
264 | ||
265 | if (q->free_func) | |
266 | q->free_func(q->buf); | |
267 | ||
268 | if (q) { | |
269 | memFree(q, MEM_DWRITE_Q); | |
270 | q = NULL; | |
271 | } | |
272 | } while ((q = fdd->write_q)); | |
273 | } | |
274 | ||
275 | len = 0; | |
276 | } | |
277 | ||
278 | if (q != NULL) { | |
279 | /* q might become NULL from write failure above */ | |
280 | q->buf_offset += len; | |
281 | ||
282 | if (q->buf_offset > q->len) | |
283 | debugs(50, DBG_IMPORTANT, "diskHandleWriteComplete: q->buf_offset > q->len (" << | |
284 | q << "," << (int) q->buf_offset << ", " << q->len << ", " << | |
285 | len << " FD " << fd << ")"); | |
286 | ||
287 | assert(q->buf_offset <= q->len); | |
288 | ||
289 | if (q->buf_offset == q->len) { | |
290 | /* complete write */ | |
291 | fdd->write_q = q->next; | |
292 | ||
293 | if (q->free_func) | |
294 | q->free_func(q->buf); | |
295 | ||
296 | if (q) { | |
297 | memFree(q, MEM_DWRITE_Q); | |
298 | q = NULL; | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
303 | if (fdd->write_q == NULL) { | |
304 | /* no more data */ | |
305 | fdd->write_q_tail = NULL; | |
306 | } else { | |
307 | /* another block is queued */ | |
308 | diskCombineWrites(fdd); | |
309 | Comm::SetSelect(fd, COMM_SELECT_WRITE, diskHandleWrite, NULL, 0); | |
310 | F->flags.write_daemon = true; | |
311 | } | |
312 | ||
313 | do_close = F->flags.close_request; | |
314 | ||
315 | if (fdd->wrt_handle) { | |
316 | DWCB *callback = fdd->wrt_handle; | |
317 | void *cbdata; | |
318 | fdd->wrt_handle = NULL; | |
319 | ||
320 | if (cbdataReferenceValidDone(fdd->wrt_handle_data, &cbdata)) { | |
321 | callback(fd, status, len, cbdata); | |
322 | /* | |
323 | * NOTE, this callback can close the FD, so we must | |
324 | * not touch 'F', 'fdd', etc. after this. | |
325 | */ | |
326 | PROF_stop(diskHandleWrite); | |
327 | return; | |
328 | /* XXX But what about close_request??? */ | |
329 | } | |
330 | } | |
331 | ||
332 | if (do_close) | |
333 | file_close(fd); | |
334 | ||
335 | PROF_stop(diskHandleWrite); | |
336 | } | |
337 | ||
338 | /* write block to a file */ | |
339 | /* write back queue. Only one writer at a time. */ | |
340 | /* call a handle when writing is complete. */ | |
341 | void | |
342 | file_write(int fd, | |
343 | off_t file_offset, | |
344 | void const *ptr_to_buf, | |
345 | int len, | |
346 | DWCB * handle, | |
347 | void *handle_data, | |
348 | FREE * free_func) | |
349 | { | |
350 | dwrite_q *wq = NULL; | |
351 | fde *F = &fd_table[fd]; | |
352 | PROF_start(file_write); | |
353 | assert(fd >= 0); | |
354 | assert(F->flags.open); | |
355 | /* if we got here. Caller is eligible to write. */ | |
356 | wq = (dwrite_q *)memAllocate(MEM_DWRITE_Q); | |
357 | wq->file_offset = file_offset; | |
358 | wq->buf = (char *)ptr_to_buf; | |
359 | wq->len = len; | |
360 | wq->buf_offset = 0; | |
361 | wq->next = NULL; | |
362 | wq->free_func = free_func; | |
363 | ||
364 | if (!F->disk.wrt_handle_data) { | |
365 | F->disk.wrt_handle = handle; | |
366 | F->disk.wrt_handle_data = cbdataReference(handle_data); | |
367 | } else { | |
368 | /* Detect if there is multiple concurrent users of this fd.. we only support one callback */ | |
369 | assert(F->disk.wrt_handle_data == handle_data && F->disk.wrt_handle == handle); | |
370 | } | |
371 | ||
372 | /* add to queue */ | |
373 | if (F->disk.write_q == NULL) { | |
374 | /* empty queue */ | |
375 | F->disk.write_q = F->disk.write_q_tail = wq; | |
376 | } else { | |
377 | F->disk.write_q_tail->next = wq; | |
378 | F->disk.write_q_tail = wq; | |
379 | } | |
380 | ||
381 | if (!F->flags.write_daemon) { | |
382 | diskHandleWrite(fd, NULL); | |
383 | } | |
384 | ||
385 | PROF_stop(file_write); | |
386 | } | |
387 | ||
388 | /* | |
389 | * a wrapper around file_write to allow for MemBuf to be file_written | |
390 | * in a snap | |
391 | */ | |
392 | void | |
393 | file_write_mbuf(int fd, off_t off, MemBuf mb, DWCB * handler, void *handler_data) | |
394 | { | |
395 | file_write(fd, off, mb.buf, mb.size, handler, handler_data, mb.freeFunc()); | |
396 | } | |
397 | ||
398 | /* Read from FD */ | |
399 | static void | |
400 | diskHandleRead(int fd, void *data) | |
401 | { | |
402 | dread_ctrl *ctrl_dat = (dread_ctrl *)data; | |
403 | fde *F = &fd_table[fd]; | |
404 | int len; | |
405 | int rc = DISK_OK; | |
406 | /* | |
407 | * FD < 0 indicates premature close; we just have to free | |
408 | * the state data. | |
409 | */ | |
410 | ||
411 | if (fd < 0) { | |
412 | memFree(ctrl_dat, MEM_DREAD_CTRL); | |
413 | return; | |
414 | } | |
415 | ||
416 | PROF_start(diskHandleRead); | |
417 | ||
418 | #if WRITES_MAINTAIN_DISK_OFFSET | |
419 | if (F->disk.offset != ctrl_dat->offset) { | |
420 | #else | |
421 | { | |
422 | #endif | |
423 | debugs(6, 3, "diskHandleRead: FD " << fd << " seeking to offset " << ctrl_dat->offset); | |
424 | lseek(fd, ctrl_dat->offset, SEEK_SET); /* XXX ignore return? */ | |
425 | ++ statCounter.syscalls.disk.seeks; | |
426 | F->disk.offset = ctrl_dat->offset; | |
427 | } | |
428 | ||
429 | errno = 0; | |
430 | len = FD_READ_METHOD(fd, ctrl_dat->buf, ctrl_dat->req_len); | |
431 | ||
432 | if (len > 0) | |
433 | F->disk.offset += len; | |
434 | ||
435 | ++ statCounter.syscalls.disk.reads; | |
436 | ||
437 | fd_bytes(fd, len, FD_READ); | |
438 | ||
439 | if (len < 0) { | |
440 | if (ignoreErrno(errno)) { | |
441 | Comm::SetSelect(fd, COMM_SELECT_READ, diskHandleRead, ctrl_dat, 0); | |
442 | PROF_stop(diskHandleRead); | |
443 | return; | |
444 | } | |
445 | ||
446 | debugs(50, DBG_IMPORTANT, "diskHandleRead: FD " << fd << ": " << xstrerror()); | |
447 | len = 0; | |
448 | rc = DISK_ERROR; | |
449 | } else if (len == 0) { | |
450 | rc = DISK_EOF; | |
451 | } | |
452 | ||
453 | if (cbdataReferenceValid(ctrl_dat->client_data)) | |
454 | ctrl_dat->handler(fd, ctrl_dat->buf, len, rc, ctrl_dat->client_data); | |
455 | ||
456 | cbdataReferenceDone(ctrl_dat->client_data); | |
457 | ||
458 | memFree(ctrl_dat, MEM_DREAD_CTRL); | |
459 | ||
460 | PROF_stop(diskHandleRead); | |
461 | } | |
462 | ||
463 | /* start read operation */ | |
464 | /* buffer must be allocated from the caller. | |
465 | * It must have at least req_len space in there. | |
466 | * call handler when a reading is complete. */ | |
467 | void | |
468 | file_read(int fd, char *buf, int req_len, off_t offset, DRCB * handler, void *client_data) | |
469 | { | |
470 | dread_ctrl *ctrl_dat; | |
471 | PROF_start(file_read); | |
472 | assert(fd >= 0); | |
473 | ctrl_dat = (dread_ctrl *)memAllocate(MEM_DREAD_CTRL); | |
474 | ctrl_dat->fd = fd; | |
475 | ctrl_dat->offset = offset; | |
476 | ctrl_dat->req_len = req_len; | |
477 | ctrl_dat->buf = buf; | |
478 | ctrl_dat->end_of_file = 0; | |
479 | ctrl_dat->handler = handler; | |
480 | ctrl_dat->client_data = cbdataReference(client_data); | |
481 | diskHandleRead(fd, ctrl_dat); | |
482 | PROF_stop(file_read); | |
483 | } | |
484 | ||
485 | void | |
486 | safeunlink(const char *s, int quiet) | |
487 | { | |
488 | ++ statCounter.syscalls.disk.unlinks; | |
489 | ||
490 | if (unlink(s) < 0 && !quiet) | |
491 | debugs(50, DBG_IMPORTANT, "safeunlink: Couldn't delete " << s << ": " << xstrerror()); | |
492 | } | |
493 | ||
494 | /* | |
495 | * Same as rename(2) but complains if something goes wrong; | |
496 | * the caller is responsible for handing and explaining the | |
497 | * consequences of errors. | |
498 | */ | |
499 | int | |
500 | xrename(const char *from, const char *to) | |
501 | { | |
502 | debugs(21, 2, "xrename: renaming " << from << " to " << to); | |
503 | #if _SQUID_OS2_ || _SQUID_WINDOWS_ | |
504 | remove(to); | |
505 | #endif | |
506 | ||
507 | if (0 == rename(from, to)) | |
508 | return 0; | |
509 | ||
510 | debugs(21, errno == ENOENT ? 2 : 1, "xrename: Cannot rename " << from << " to " << to << ": " << xstrerror()); | |
511 | ||
512 | return -1; | |
513 | } | |
514 |