]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/disk.cc
4 * DEBUG: section 06 Disk I/O Routines
5 * AUTHOR: Harvest Derived
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "squid-old.h"
36 #include "comm/Loops.h"
39 #include "StatCounters.h"
41 static PF diskHandleRead
;
42 static PF diskHandleWrite
;
44 #if _SQUID_WINDOWS_ || _SQUID_OS2_
46 diskWriteIsComplete(int fd
)
48 return fd_table
[fd
].disk
.write_q
? 0 : 1;
59 /* hack needed on SunStudio to avoid linkage convention mismatch */
60 static void cxx_xfree(void *ptr
)
66 * opens a disk file specified by 'path'. This function always
67 * blocks! There is no callback.
70 file_open(const char *path
, int mode
)
73 PROF_start(file_open
);
75 if (FILE_MODE(mode
) == O_WRONLY
)
80 fd
= open(path
, mode
, 0644);
82 statCounter
.syscalls
.disk
.opens
++;
85 debugs(50, 3, "file_open: error opening file " << path
<< ": " << xstrerror());
88 debugs(6, 5, "file_open: FD " << fd
);
89 commSetCloseOnExec(fd
);
90 fd_open(fd
, FD_FILE
, path
);
98 /* close a disk file. */
102 fde
*F
= &fd_table
[fd
];
104 PROF_start(file_close
);
106 assert(F
->flags
.open
);
108 if ((read_callback
= F
->read_handler
)) {
109 F
->read_handler
= NULL
;
110 read_callback(-1, F
->read_data
);
113 if (F
->flags
.write_daemon
) {
114 #if _SQUID_WINDOWS_ || _SQUID_OS2_
116 * on some operating systems, you can not delete or rename
117 * open files, so we won't allow delayed close.
119 while (!diskWriteIsComplete(fd
))
120 diskHandleWrite(fd
, NULL
);
122 F
->flags
.close_request
= 1;
123 debugs(6, 2, "file_close: FD " << fd
<< ", delaying close");
124 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 #if CALL_FSYNC_BEFORE_CLOSE
144 debugs(6, F
->flags
.close_request
? 2 : 5, "file_close: FD " << fd
<< " really closing\n");
148 statCounter
.syscalls
.disk
.closes
++;
150 PROF_stop(file_close
);
154 * This function has the purpose of combining multiple writes. This is
155 * to facilitate the ASYNC_IO option since it can only guarantee 1
156 * write to a file per trip around the comm.c select() loop. That's bad
157 * because more than 1 write can be made to the access.log file per
158 * trip, and so this code is purely designed to help batch multiple
159 * sequential writes to the access.log file. Squid will never issue
160 * multiple writes for any other file type during 1 trip around the
161 * select() loop. --SLF
164 diskCombineWrites(struct _fde_disk
*fdd
)
167 * We need to combine multiple write requests on an FD's write
168 * queue But only if we don't need to seek() in between them, ugh!
169 * XXX This currently ignores any seeks (file_offset)
172 if (fdd
->write_q
!= NULL
&& fdd
->write_q
->next
!= NULL
) {
175 for (dwrite_q
*q
= fdd
->write_q
; q
!= NULL
; q
= q
->next
)
176 len
+= q
->len
- q
->buf_offset
;
178 dwrite_q
*wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
180 wq
->buf
= (char *)xmalloc(len
);
188 wq
->free_func
= cxx_xfree
;
190 while (fdd
->write_q
!= NULL
) {
191 dwrite_q
*q
= fdd
->write_q
;
193 len
= q
->len
- q
->buf_offset
;
194 memcpy(wq
->buf
+ wq
->len
, q
->buf
+ q
->buf_offset
, len
);
196 fdd
->write_q
= q
->next
;
199 q
->free_func(q
->buf
);
201 memFree(q
, MEM_DWRITE_Q
);
204 fdd
->write_q_tail
= wq
;
212 diskHandleWrite(int fd
, void *notused
)
215 fde
*F
= &fd_table
[fd
];
217 struct _fde_disk
*fdd
= &F
->disk
;
218 dwrite_q
*q
= fdd
->write_q
;
219 int status
= DISK_OK
;
225 PROF_start(diskHandleWrite
);
227 debugs(6, 3, "diskHandleWrite: FD " << fd
);
229 F
->flags
.write_daemon
= 0;
231 assert(fdd
->write_q
!= NULL
);
233 assert(fdd
->write_q
->len
> fdd
->write_q
->buf_offset
);
235 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " writing " <<
236 (fdd
->write_q
->len
- fdd
->write_q
->buf_offset
) << " bytes at " <<
237 fdd
->write_q
->file_offset
);
241 if (fdd
->write_q
->file_offset
!= -1)
242 lseek(fd
, fdd
->write_q
->file_offset
, SEEK_SET
); /* XXX ignore return? */
244 len
= FD_WRITE_METHOD(fd
,
245 fdd
->write_q
->buf
+ fdd
->write_q
->buf_offset
,
246 fdd
->write_q
->len
- fdd
->write_q
->buf_offset
);
248 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " len = " << len
);
250 statCounter
.syscalls
.disk
.writes
++;
252 fd_bytes(fd
, len
, FD_WRITE
);
255 if (!ignoreErrno(errno
)) {
256 status
= errno
== ENOSPC
? DISK_NO_SPACE_LEFT
: DISK_ERROR
;
257 debugs(50, 1, "diskHandleWrite: FD " << fd
<< ": disk write error: " << xstrerror());
260 * If there is no write callback, then this file is
261 * most likely something important like a log file, or
262 * an interprocess pipe. Its not a swapfile. We feel
263 * that a write failure on a log file is rather important,
264 * and Squid doesn't otherwise deal with this condition.
265 * So to get the administrators attention, we exit with
269 if (fdd
->wrt_handle
== NULL
)
270 fatal("Write failure -- check your disk space and cache.log");
273 * If there is a write failure, then we notify the
274 * upper layer via the callback, at the end of this
275 * function. Meanwhile, flush all pending buffers
276 * here. Let the upper layer decide how to handle the
277 * failure. This will prevent experiencing multiple,
278 * repeated write failures for the same FD because of
282 fdd
->write_q
= q
->next
;
285 q
->free_func(q
->buf
);
288 memFree(q
, MEM_DWRITE_Q
);
291 } while ((q
= fdd
->write_q
));
298 /* q might become NULL from write failure above */
299 q
->buf_offset
+= len
;
301 if (q
->buf_offset
> q
->len
)
302 debugs(50, 1, "diskHandleWriteComplete: q->buf_offset > q->len (" <<
303 q
<< "," << (int) q
->buf_offset
<< ", " << q
->len
<< ", " <<
304 len
<< " FD " << fd
<< ")");
307 assert(q
->buf_offset
<= q
->len
);
309 if (q
->buf_offset
== q
->len
) {
311 fdd
->write_q
= q
->next
;
314 q
->free_func(q
->buf
);
317 memFree(q
, MEM_DWRITE_Q
);
323 if (fdd
->write_q
== NULL
) {
325 fdd
->write_q_tail
= NULL
;
327 /* another block is queued */
328 diskCombineWrites(fdd
);
329 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, diskHandleWrite
, NULL
, 0);
330 F
->flags
.write_daemon
= 1;
333 do_close
= F
->flags
.close_request
;
335 if (fdd
->wrt_handle
) {
336 DWCB
*callback
= fdd
->wrt_handle
;
338 fdd
->wrt_handle
= NULL
;
340 if (cbdataReferenceValidDone(fdd
->wrt_handle_data
, &cbdata
)) {
341 callback(fd
, status
, len
, cbdata
);
343 * NOTE, this callback can close the FD, so we must
344 * not touch 'F', 'fdd', etc. after this.
346 PROF_stop(diskHandleWrite
);
348 /* XXX But what about close_request??? */
355 PROF_stop(diskHandleWrite
);
359 /* write block to a file */
360 /* write back queue. Only one writer at a time. */
361 /* call a handle when writing is complete. */
365 void const *ptr_to_buf
,
372 fde
*F
= &fd_table
[fd
];
373 PROF_start(file_write
);
375 assert(F
->flags
.open
);
376 /* if we got here. Caller is eligible to write. */
377 wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
378 wq
->file_offset
= file_offset
;
379 wq
->buf
= (char *)ptr_to_buf
;
383 wq
->free_func
= free_func
;
385 if (!F
->disk
.wrt_handle_data
) {
386 F
->disk
.wrt_handle
= handle
;
387 F
->disk
.wrt_handle_data
= cbdataReference(handle_data
);
389 /* Detect if there is multiple concurrent users of this fd.. we only support one callback */
390 assert(F
->disk
.wrt_handle_data
== handle_data
&& F
->disk
.wrt_handle
== handle
);
394 if (F
->disk
.write_q
== NULL
) {
396 F
->disk
.write_q
= F
->disk
.write_q_tail
= wq
;
398 F
->disk
.write_q_tail
->next
= wq
;
399 F
->disk
.write_q_tail
= wq
;
402 if (!F
->flags
.write_daemon
) {
403 diskHandleWrite(fd
, NULL
);
406 PROF_stop(file_write
);
410 * a wrapper around file_write to allow for MemBuf to be file_written
414 file_write_mbuf(int fd
, off_t off
, MemBuf mb
, DWCB
* handler
, void *handler_data
)
416 file_write(fd
, off
, mb
.buf
, mb
.size
, handler
, handler_data
, mb
.freeFunc());
421 diskHandleRead(int fd
, void *data
)
423 dread_ctrl
*ctrl_dat
= (dread_ctrl
*)data
;
424 fde
*F
= &fd_table
[fd
];
428 * FD < 0 indicates premature close; we just have to free
433 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
437 PROF_start(diskHandleRead
);
439 #if WRITES_MAINTAIN_DISK_OFFSET
440 if (F
->disk
.offset
!= ctrl_dat
->offset
) {
444 debugs(6, 3, "diskHandleRead: FD " << fd
<< " seeking to offset " << ctrl_dat
->offset
);
445 lseek(fd
, ctrl_dat
->offset
, SEEK_SET
); /* XXX ignore return? */
446 ++statCounter
.syscalls
.disk
.seeks
;
447 F
->disk
.offset
= ctrl_dat
->offset
;
451 len
= FD_READ_METHOD(fd
, ctrl_dat
->buf
, ctrl_dat
->req_len
);
454 F
->disk
.offset
+= len
;
456 statCounter
.syscalls
.disk
.reads
++;
458 fd_bytes(fd
, len
, FD_READ
);
461 if (ignoreErrno(errno
)) {
462 Comm::SetSelect(fd
, COMM_SELECT_READ
, diskHandleRead
, ctrl_dat
, 0);
463 PROF_stop(diskHandleRead
);
467 debugs(50, 1, "diskHandleRead: FD " << fd
<< ": " << xstrerror());
470 } else if (len
== 0) {
474 if (cbdataReferenceValid(ctrl_dat
->client_data
))
475 ctrl_dat
->handler(fd
, ctrl_dat
->buf
, len
, rc
, ctrl_dat
->client_data
);
477 cbdataReferenceDone(ctrl_dat
->client_data
);
479 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
481 PROF_stop(diskHandleRead
);
485 /* start read operation */
486 /* buffer must be allocated from the caller.
487 * It must have at least req_len space in there.
488 * call handler when a reading is complete. */
490 file_read(int fd
, char *buf
, int req_len
, off_t offset
, DRCB
* handler
, void *client_data
)
492 dread_ctrl
*ctrl_dat
;
493 PROF_start(file_read
);
495 ctrl_dat
= (dread_ctrl
*)memAllocate(MEM_DREAD_CTRL
);
497 ctrl_dat
->offset
= offset
;
498 ctrl_dat
->req_len
= req_len
;
500 ctrl_dat
->end_of_file
= 0;
501 ctrl_dat
->handler
= handler
;
502 ctrl_dat
->client_data
= cbdataReference(client_data
);
503 diskHandleRead(fd
, ctrl_dat
);
504 PROF_stop(file_read
);
508 safeunlink(const char *s
, int quiet
)
510 statCounter
.syscalls
.disk
.unlinks
++;
512 if (unlink(s
) < 0 && !quiet
)
513 debugs(50, 1, "safeunlink: Couldn't delete " << s
<< ": " << xstrerror());
517 * Same as rename(2) but complains if something goes wrong;
518 * the caller is responsible for handing and explaining the
519 * consequences of errors.
522 xrename(const char *from
, const char *to
)
524 debugs(21, 2, "xrename: renaming " << from
<< " to " << to
);
525 #if _SQUID_OS2_ || _SQUID_WINDOWS_
529 if (0 == rename(from
, to
))
532 debugs(21, errno
== ENOENT
? 2 : 1, "xrename: Cannot rename " << from
<< " to " << to
<< ": " << xstrerror());