]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/disk.cc
3 * $Id: disk.cc,v 1.173 2007/04/30 16:56:09 wessels Exp $
5 * DEBUG: section 6 Disk I/O Routines
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
40 static PF diskHandleRead
;
41 static PF diskHandleWrite
;
43 #if defined(_SQUID_WIN32_) || defined(_SQUID_OS2_)
45 diskWriteIsComplete(int fd
)
47 return fd_table
[fd
].disk
.write_q
? 0 : 1;
59 * opens a disk file specified by 'path'. This function always
60 * blocks! There is no callback.
63 file_open(const char *path
, int mode
)
66 PROF_start(file_open
);
68 if (FILE_MODE(mode
) == O_WRONLY
)
73 fd
= open(path
, mode
, 0644);
75 statCounter
.syscalls
.disk
.opens
++;
78 debugs(50, 3, "file_open: error opening file " << path
<< ": " << xstrerror());
81 debugs(6, 5, "file_open: FD " << fd
);
82 commSetCloseOnExec(fd
);
83 fd_open(fd
, FD_FILE
, path
);
91 /* close a disk file. */
95 fde
*F
= &fd_table
[fd
];
97 PROF_start(file_close
);
99 assert(F
->flags
.open
);
101 if ((read_callback
= F
->read_handler
)) {
102 F
->read_handler
= NULL
;
103 read_callback(-1, F
->read_data
);
106 if (F
->flags
.write_daemon
) {
107 #if defined(_SQUID_WIN32_) || defined(_SQUID_OS2_)
109 * on some operating systems, you can not delete or rename
110 * open files, so we won't allow delayed close.
113 while (!diskWriteIsComplete(fd
))
114 diskHandleWrite(fd
, NULL
);
118 F
->flags
.close_request
= 1;
120 debugs(6, 2, "file_close: FD " << fd
<< ", delaying close");
122 PROF_stop(file_close
);
131 * Assert there is no write callback. Otherwise we might be
132 * leaking write state data by closing the descriptor
134 assert(F
->write_handler
== NULL
);
136 F
->flags
.closing
= 1;
138 #if CALL_FSYNC_BEFORE_CLOSE
146 debugs(6, F
->flags
.close_request
? 2 : 5, "file_close: FD " << fd
<< " really closing\n");
150 statCounter
.syscalls
.disk
.closes
++;
152 PROF_stop(file_close
);
156 * This function has the purpose of combining multiple writes. This is
157 * to facilitate the ASYNC_IO option since it can only guarantee 1
158 * write to a file per trip around the comm.c select() loop. That's bad
159 * because more than 1 write can be made to the access.log file per
160 * trip, and so this code is purely designed to help batch multiple
161 * sequential writes to the access.log file. Squid will never issue
162 * multiple writes for any other file type during 1 trip around the
163 * select() loop. --SLF
167 diskCombineWrites(struct _fde_disk
*fdd
)
173 * We need to combine multiple write requests on an FD's write
174 * queue But only if we don't need to seek() in between them, ugh!
175 * XXX This currently ignores any seeks (file_offset)
178 if (fdd
->write_q
!= NULL
&& fdd
->write_q
->next
!= NULL
)
182 for (q
= fdd
->write_q
; q
!= NULL
; q
= q
->next
)
183 len
+= q
->len
- q
->buf_offset
;
185 wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
187 wq
->buf
= (char *)xmalloc(len
);
195 wq
->free_func
= xfree
;
199 len
= q
->len
- q
->buf_offset
;
200 xmemcpy(wq
->buf
+ wq
->len
, q
->buf
+ q
->buf_offset
, len
);
202 fdd
->write_q
= q
->next
;
205 (q
->free_func
) (q
->buf
);
208 memFree(q
, MEM_DWRITE_Q
);
211 } while (fdd
->write_q
!= NULL
);
213 fdd
->write_q_tail
= wq
;
221 diskHandleWrite(int fd
, void *notused
)
224 fde
*F
= &fd_table
[fd
];
226 struct _fde_disk
*fdd
= &F
->disk
;
227 dwrite_q
*q
= fdd
->write_q
;
228 int status
= DISK_OK
;
234 PROF_start(diskHandleWrite
);
236 debugs(6, 3, "diskHandleWrite: FD " << fd
);
238 F
->flags
.write_daemon
= 0;
240 assert(fdd
->write_q
!= NULL
);
242 assert(fdd
->write_q
->len
> fdd
->write_q
->buf_offset
);
244 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " writing " << (fdd
->write_q
->len
- fdd
->write_q
->buf_offset
) << " bytes");
248 if (fdd
->write_q
->file_offset
!= -1)
249 lseek(fd
, fdd
->write_q
->file_offset
, SEEK_SET
);
251 len
= FD_WRITE_METHOD(fd
,
252 fdd
->write_q
->buf
+ fdd
->write_q
->buf_offset
,
253 fdd
->write_q
->len
- fdd
->write_q
->buf_offset
);
255 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " len = " << len
);
257 statCounter
.syscalls
.disk
.writes
++;
259 fd_bytes(fd
, len
, FD_WRITE
);
262 if (!ignoreErrno(errno
)) {
263 status
= errno
== ENOSPC
? DISK_NO_SPACE_LEFT
: DISK_ERROR
;
264 debugs(50, 1, "diskHandleWrite: FD " << fd
<< ": disk write error: " << xstrerror());
267 * If there is no write callback, then this file is
268 * most likely something important like a log file, or
269 * an interprocess pipe. Its not a swapfile. We feel
270 * that a write failure on a log file is rather important,
271 * and Squid doesn't otherwise deal with this condition.
272 * So to get the administrators attention, we exit with
276 if (fdd
->wrt_handle
== NULL
)
277 fatal("Write failure -- check your disk space and cache.log");
280 * If there is a write failure, then we notify the
281 * upper layer via the callback, at the end of this
282 * function. Meanwhile, flush all pending buffers
283 * here. Let the upper layer decide how to handle the
284 * failure. This will prevent experiencing multiple,
285 * repeated write failures for the same FD because of
289 fdd
->write_q
= q
->next
;
292 (q
->free_func
) (q
->buf
);
295 memFree(q
, MEM_DWRITE_Q
);
298 } while ((q
= fdd
->write_q
));
305 /* q might become NULL from write failure above */
306 q
->buf_offset
+= len
;
308 if (q
->buf_offset
> q
->len
)
309 debugs(50, 1, "diskHandleWriteComplete: q->buf_offset > q->len (" <<
310 q
<< "," << (int) q
->buf_offset
<< ", " << q
->len
<< ", " <<
311 len
<< " FD " << fd
<< ")");
314 assert(q
->buf_offset
<= q
->len
);
316 if (q
->buf_offset
== q
->len
) {
318 fdd
->write_q
= q
->next
;
321 (q
->free_func
) (q
->buf
);
324 memFree(q
, MEM_DWRITE_Q
);
330 if (fdd
->write_q
== NULL
) {
332 fdd
->write_q_tail
= NULL
;
334 /* another block is queued */
335 diskCombineWrites(fdd
);
336 commSetSelect(fd
, COMM_SELECT_WRITE
, diskHandleWrite
, NULL
, 0);
337 F
->flags
.write_daemon
= 1;
340 do_close
= F
->flags
.close_request
;
342 if (fdd
->wrt_handle
) {
343 DWCB
*callback
= fdd
->wrt_handle
;
345 fdd
->wrt_handle
= NULL
;
347 if (cbdataReferenceValidDone(fdd
->wrt_handle_data
, &cbdata
)) {
348 callback(fd
, status
, len
, cbdata
);
350 * NOTE, this callback can close the FD, so we must
351 * not touch 'F', 'fdd', etc. after this.
353 PROF_stop(diskHandleWrite
);
355 /* XXX But what about close_request??? */
362 PROF_stop(diskHandleWrite
);
366 /* write block to a file */
367 /* write back queue. Only one writer at a time. */
368 /* call a handle when writing is complete. */
372 void const *ptr_to_buf
,
379 fde
*F
= &fd_table
[fd
];
380 PROF_start(file_write
);
382 assert(F
->flags
.open
);
383 /* if we got here. Caller is eligible to write. */
384 wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
385 wq
->file_offset
= file_offset
;
386 wq
->buf
= (char *)ptr_to_buf
;
390 wq
->free_func
= free_func
;
392 if (!F
->disk
.wrt_handle_data
) {
393 F
->disk
.wrt_handle
= handle
;
394 F
->disk
.wrt_handle_data
= cbdataReference(handle_data
);
396 /* Detect if there is multiple concurrent users of this fd.. we only support one callback */
397 assert(F
->disk
.wrt_handle_data
== handle_data
&& F
->disk
.wrt_handle
== handle
);
401 if (F
->disk
.write_q
== NULL
) {
403 F
->disk
.write_q
= F
->disk
.write_q_tail
= wq
;
405 F
->disk
.write_q_tail
->next
= wq
;
406 F
->disk
.write_q_tail
= wq
;
409 if (!F
->flags
.write_daemon
) {
410 diskHandleWrite(fd
, NULL
);
413 PROF_stop(file_write
);
417 * a wrapper around file_write to allow for MemBuf to be file_written
421 file_write_mbuf(int fd
, off_t off
, MemBuf mb
, DWCB
* handler
, void *handler_data
)
423 file_write(fd
, off
, mb
.buf
, mb
.size
, handler
, handler_data
, mb
.freeFunc());
428 diskHandleRead(int fd
, void *data
)
430 dread_ctrl
*ctrl_dat
= (dread_ctrl
*)data
;
431 fde
*F
= &fd_table
[fd
];
435 * FD < 0 indicates premature close; we just have to free
440 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
444 PROF_start(diskHandleRead
);
446 if (F
->disk
.offset
!= ctrl_dat
->offset
) {
447 debugs(6, 3, "diskHandleRead: FD " << fd
<< " seeking to offset " << ctrl_dat
->offset
);
448 lseek(fd
, ctrl_dat
->offset
, SEEK_SET
); /* XXX ignore return? */
449 statCounter
.syscalls
.disk
.seeks
++;
450 F
->disk
.offset
= ctrl_dat
->offset
;
454 len
= FD_READ_METHOD(fd
, ctrl_dat
->buf
, ctrl_dat
->req_len
);
457 F
->disk
.offset
+= len
;
459 statCounter
.syscalls
.disk
.reads
++;
461 fd_bytes(fd
, len
, FD_READ
);
464 if (ignoreErrno(errno
)) {
465 commSetSelect(fd
, COMM_SELECT_READ
, diskHandleRead
, ctrl_dat
, 0);
466 PROF_stop(diskHandleRead
);
470 debugs(50, 1, "diskHandleRead: FD " << fd
<< ": " << xstrerror());
473 } else if (len
== 0) {
477 if (cbdataReferenceValid(ctrl_dat
->client_data
))
478 ctrl_dat
->handler(fd
, ctrl_dat
->buf
, len
, rc
, ctrl_dat
->client_data
);
480 cbdataReferenceDone(ctrl_dat
->client_data
);
482 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
484 PROF_stop(diskHandleRead
);
488 /* start read operation */
489 /* buffer must be allocated from the caller.
490 * It must have at least req_len space in there.
491 * call handler when a reading is complete. */
493 file_read(int fd
, char *buf
, int req_len
, off_t offset
, DRCB
* handler
, void *client_data
)
495 dread_ctrl
*ctrl_dat
;
496 PROF_start(file_read
);
498 ctrl_dat
= (dread_ctrl
*)memAllocate(MEM_DREAD_CTRL
);
500 ctrl_dat
->offset
= offset
;
501 ctrl_dat
->req_len
= req_len
;
503 ctrl_dat
->end_of_file
= 0;
504 ctrl_dat
->handler
= handler
;
505 ctrl_dat
->client_data
= cbdataReference(client_data
);
506 diskHandleRead(fd
, ctrl_dat
);
507 PROF_stop(file_read
);
511 safeunlink(const char *s
, int quiet
)
513 statCounter
.syscalls
.disk
.unlinks
++;
515 if (unlink(s
) < 0 && !quiet
)
516 debugs(50, 1, "safeunlink: Couldn't delete " << s
<< ": " << xstrerror());
520 * Same as rename(2) but complains if something goes wrong;
521 * the caller is responsible for handing and explaining the
522 * consequences of errors.
525 xrename(const char *from
, const char *to
)
527 debugs(21, 2, "xrename: renaming " << from
<< " to " << to
);
528 #if defined (_SQUID_OS2_) || defined (_SQUID_WIN32_)
535 if (0 == rename(from
, to
))
538 debugs(21, errno
== ENOENT
? 2 : 1, "xrename: Cannot rename " << from
<< " to " << to
<< ": " << xstrerror());