]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fs_io.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 06 Disk I/O Routines */
12 #include "comm/Loops.h"
18 #include "profiler/Profiler.h"
19 #include "StatCounters.h"
23 static PF diskHandleRead
;
24 static PF diskHandleWrite
;
26 #if _SQUID_WINDOWS_ || _SQUID_OS2_
28 diskWriteIsComplete(int fd
)
30 return fd_table
[fd
].disk
.write_q
? 0 : 1;
35 /* hack needed on SunStudio to avoid linkage convention mismatch */
36 static void cxx_xfree(void *ptr
)
42 * opens a disk file specified by 'path'. This function always
43 * blocks! There is no callback.
46 file_open(const char *path
, int mode
)
49 PROF_start(file_open
);
51 if (FILE_MODE(mode
) == O_WRONLY
)
56 fd
= open(path
, mode
, 0644);
58 ++ statCounter
.syscalls
.disk
.opens
;
61 debugs(50, 3, "file_open: error opening file " << path
<< ": " << xstrerror());
64 debugs(6, 5, "file_open: FD " << fd
);
65 commSetCloseOnExec(fd
);
66 fd_open(fd
, FD_FILE
, path
);
73 /* close a disk file. */
77 fde
*F
= &fd_table
[fd
];
79 PROF_start(file_close
);
81 assert(F
->flags
.open
);
83 if ((read_callback
= F
->read_handler
)) {
84 F
->read_handler
= NULL
;
85 read_callback(-1, F
->read_data
);
88 if (F
->flags
.write_daemon
) {
89 #if _SQUID_WINDOWS_ || _SQUID_OS2_
91 * on some operating systems, you can not delete or rename
92 * open files, so we won't allow delayed close.
94 while (!diskWriteIsComplete(fd
))
95 diskHandleWrite(fd
, NULL
);
97 F
->flags
.close_request
= true;
98 debugs(6, 2, "file_close: FD " << fd
<< ", delaying close");
99 PROF_stop(file_close
);
106 * Assert there is no write callback. Otherwise we might be
107 * leaking write state data by closing the descriptor
109 assert(F
->write_handler
== NULL
);
111 #if CALL_FSYNC_BEFORE_CLOSE
119 debugs(6, F
->flags
.close_request
? 2 : 5, "file_close: FD " << fd
<< " really closing\n");
123 ++ statCounter
.syscalls
.disk
.closes
;
125 PROF_stop(file_close
);
129 * This function has the purpose of combining multiple writes. This is
130 * to facilitate the ASYNC_IO option since it can only guarantee 1
131 * write to a file per trip around the comm.c select() loop. That's bad
132 * because more than 1 write can be made to the access.log file per
133 * trip, and so this code is purely designed to help batch multiple
134 * sequential writes to the access.log file. Squid will never issue
135 * multiple writes for any other file type during 1 trip around the
136 * select() loop. --SLF
139 diskCombineWrites(_fde_disk
*fdd
)
142 * We need to combine multiple write requests on an FD's write
143 * queue But only if we don't need to seek() in between them, ugh!
144 * XXX This currently ignores any seeks (file_offset)
147 if (fdd
->write_q
!= NULL
&& fdd
->write_q
->next
!= NULL
) {
150 for (dwrite_q
*q
= fdd
->write_q
; q
!= NULL
; q
= q
->next
)
151 len
+= q
->len
- q
->buf_offset
;
153 dwrite_q
*wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
155 wq
->buf
= (char *)xmalloc(len
);
163 wq
->free_func
= cxx_xfree
;
165 while (fdd
->write_q
!= NULL
) {
166 dwrite_q
*q
= fdd
->write_q
;
168 len
= q
->len
- q
->buf_offset
;
169 memcpy(wq
->buf
+ wq
->len
, q
->buf
+ q
->buf_offset
, len
);
171 fdd
->write_q
= q
->next
;
174 q
->free_func(q
->buf
);
176 memFree(q
, MEM_DWRITE_Q
);
179 fdd
->write_q_tail
= wq
;
187 diskHandleWrite(int fd
, void *)
190 fde
*F
= &fd_table
[fd
];
192 _fde_disk
*fdd
= &F
->disk
;
193 dwrite_q
*q
= fdd
->write_q
;
194 int status
= DISK_OK
;
200 PROF_start(diskHandleWrite
);
202 debugs(6, 3, "diskHandleWrite: FD " << fd
);
204 F
->flags
.write_daemon
= false;
206 assert(fdd
->write_q
!= NULL
);
208 assert(fdd
->write_q
->len
> fdd
->write_q
->buf_offset
);
210 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " writing " <<
211 (fdd
->write_q
->len
- fdd
->write_q
->buf_offset
) << " bytes at " <<
212 fdd
->write_q
->file_offset
);
216 if (fdd
->write_q
->file_offset
!= -1) {
218 if (lseek(fd
, fdd
->write_q
->file_offset
, SEEK_SET
) == -1) {
219 debugs(50, DBG_IMPORTANT
, "error in seek for fd " << fd
<< ": " << xstrerror());
220 // XXX: handle error?
224 len
= FD_WRITE_METHOD(fd
,
225 fdd
->write_q
->buf
+ fdd
->write_q
->buf_offset
,
226 fdd
->write_q
->len
- fdd
->write_q
->buf_offset
);
228 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " len = " << len
);
230 ++ statCounter
.syscalls
.disk
.writes
;
232 fd_bytes(fd
, len
, FD_WRITE
);
235 if (!ignoreErrno(errno
)) {
236 status
= errno
== ENOSPC
? DISK_NO_SPACE_LEFT
: DISK_ERROR
;
237 debugs(50, DBG_IMPORTANT
, "diskHandleWrite: FD " << fd
<< ": disk write error: " << xstrerror());
240 * If there is no write callback, then this file is
241 * most likely something important like a log file, or
242 * an interprocess pipe. Its not a swapfile. We feel
243 * that a write failure on a log file is rather important,
244 * and Squid doesn't otherwise deal with this condition.
245 * So to get the administrators attention, we exit with
249 if (fdd
->wrt_handle
== NULL
)
250 fatal("Write failure -- check your disk space and cache.log");
253 * If there is a write failure, then we notify the
254 * upper layer via the callback, at the end of this
255 * function. Meanwhile, flush all pending buffers
256 * here. Let the upper layer decide how to handle the
257 * failure. This will prevent experiencing multiple,
258 * repeated write failures for the same FD because of
262 fdd
->write_q
= q
->next
;
265 q
->free_func(q
->buf
);
268 memFree(q
, MEM_DWRITE_Q
);
271 } while ((q
= fdd
->write_q
));
278 /* q might become NULL from write failure above */
279 q
->buf_offset
+= len
;
281 if (q
->buf_offset
> q
->len
)
282 debugs(50, DBG_IMPORTANT
, "diskHandleWriteComplete: q->buf_offset > q->len (" <<
283 q
<< "," << (int) q
->buf_offset
<< ", " << q
->len
<< ", " <<
284 len
<< " FD " << fd
<< ")");
286 assert(q
->buf_offset
<= q
->len
);
288 if (q
->buf_offset
== q
->len
) {
290 fdd
->write_q
= q
->next
;
293 q
->free_func(q
->buf
);
296 memFree(q
, MEM_DWRITE_Q
);
302 if (fdd
->write_q
== NULL
) {
304 fdd
->write_q_tail
= NULL
;
306 /* another block is queued */
307 diskCombineWrites(fdd
);
308 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, diskHandleWrite
, NULL
, 0);
309 F
->flags
.write_daemon
= true;
312 do_close
= F
->flags
.close_request
;
314 if (fdd
->wrt_handle
) {
315 DWCB
*callback
= fdd
->wrt_handle
;
317 fdd
->wrt_handle
= NULL
;
319 if (cbdataReferenceValidDone(fdd
->wrt_handle_data
, &cbdata
)) {
320 callback(fd
, status
, len
, cbdata
);
322 * NOTE, this callback can close the FD, so we must
323 * not touch 'F', 'fdd', etc. after this.
325 PROF_stop(diskHandleWrite
);
327 /* XXX But what about close_request??? */
334 PROF_stop(diskHandleWrite
);
337 /* write block to a file */
338 /* write back queue. Only one writer at a time. */
339 /* call a handle when writing is complete. */
343 void const *ptr_to_buf
,
350 fde
*F
= &fd_table
[fd
];
351 PROF_start(file_write
);
353 assert(F
->flags
.open
);
354 /* if we got here. Caller is eligible to write. */
355 wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
356 wq
->file_offset
= file_offset
;
357 wq
->buf
= (char *)ptr_to_buf
;
361 wq
->free_func
= free_func
;
363 if (!F
->disk
.wrt_handle_data
) {
364 F
->disk
.wrt_handle
= handle
;
365 F
->disk
.wrt_handle_data
= cbdataReference(handle_data
);
367 /* Detect if there is multiple concurrent users of this fd.. we only support one callback */
368 assert(F
->disk
.wrt_handle_data
== handle_data
&& F
->disk
.wrt_handle
== handle
);
372 if (F
->disk
.write_q
== NULL
) {
374 F
->disk
.write_q
= F
->disk
.write_q_tail
= wq
;
376 F
->disk
.write_q_tail
->next
= wq
;
377 F
->disk
.write_q_tail
= wq
;
380 if (!F
->flags
.write_daemon
) {
381 diskHandleWrite(fd
, NULL
);
384 PROF_stop(file_write
);
388 * a wrapper around file_write to allow for MemBuf to be file_written
392 file_write_mbuf(int fd
, off_t off
, MemBuf mb
, DWCB
* handler
, void *handler_data
)
394 file_write(fd
, off
, mb
.buf
, mb
.size
, handler
, handler_data
, mb
.freeFunc());
399 diskHandleRead(int fd
, void *data
)
401 dread_ctrl
*ctrl_dat
= (dread_ctrl
*)data
;
402 fde
*F
= &fd_table
[fd
];
406 * FD < 0 indicates premature close; we just have to free
411 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
415 PROF_start(diskHandleRead
);
417 #if WRITES_MAINTAIN_DISK_OFFSET
418 if (F
->disk
.offset
!= ctrl_dat
->offset
) {
422 debugs(6, 3, "diskHandleRead: FD " << fd
<< " seeking to offset " << ctrl_dat
->offset
);
424 if (lseek(fd
, ctrl_dat
->offset
, SEEK_SET
) == -1) {
425 // shouldn't happen, let's detect that
426 debugs(50, DBG_IMPORTANT
, "error in seek for fd " << fd
<< ": " << xstrerror());
427 // XXX handle failures?
429 ++ statCounter
.syscalls
.disk
.seeks
;
430 F
->disk
.offset
= ctrl_dat
->offset
;
434 len
= FD_READ_METHOD(fd
, ctrl_dat
->buf
, ctrl_dat
->req_len
);
437 F
->disk
.offset
+= len
;
439 ++ statCounter
.syscalls
.disk
.reads
;
441 fd_bytes(fd
, len
, FD_READ
);
444 if (ignoreErrno(errno
)) {
445 Comm::SetSelect(fd
, COMM_SELECT_READ
, diskHandleRead
, ctrl_dat
, 0);
446 PROF_stop(diskHandleRead
);
450 debugs(50, DBG_IMPORTANT
, "diskHandleRead: FD " << fd
<< ": " << xstrerror());
453 } else if (len
== 0) {
457 if (cbdataReferenceValid(ctrl_dat
->client_data
))
458 ctrl_dat
->handler(fd
, ctrl_dat
->buf
, len
, rc
, ctrl_dat
->client_data
);
460 cbdataReferenceDone(ctrl_dat
->client_data
);
462 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
464 PROF_stop(diskHandleRead
);
467 /* start read operation */
468 /* buffer must be allocated from the caller.
469 * It must have at least req_len space in there.
470 * call handler when a reading is complete. */
472 file_read(int fd
, char *buf
, int req_len
, off_t offset
, DRCB
* handler
, void *client_data
)
474 dread_ctrl
*ctrl_dat
;
475 PROF_start(file_read
);
477 ctrl_dat
= (dread_ctrl
*)memAllocate(MEM_DREAD_CTRL
);
479 ctrl_dat
->offset
= offset
;
480 ctrl_dat
->req_len
= req_len
;
482 ctrl_dat
->end_of_file
= 0;
483 ctrl_dat
->handler
= handler
;
484 ctrl_dat
->client_data
= cbdataReference(client_data
);
485 diskHandleRead(fd
, ctrl_dat
);
486 PROF_stop(file_read
);
490 safeunlink(const char *s
, int quiet
)
492 ++ statCounter
.syscalls
.disk
.unlinks
;
494 if (unlink(s
) < 0 && !quiet
)
495 debugs(50, DBG_IMPORTANT
, "safeunlink: Couldn't delete " << s
<< ": " << xstrerror());
499 * Same as rename(2) but complains if something goes wrong;
500 * the caller is responsible for handing and explaining the
501 * consequences of errors.
504 xrename(const char *from
, const char *to
)
506 debugs(21, 2, "xrename: renaming " << from
<< " to " << to
);
507 #if _SQUID_OS2_ || _SQUID_WINDOWS_
511 if (0 == rename(from
, to
))
514 debugs(21, errno
== ENOENT
? 2 : 1, "xrename: Cannot rename " << from
<< " to " << to
<< ": " << xstrerror());
520 fsBlockSize(const char *path
, int *blksize
)
524 if (xstatvfs(path
, &sfs
)) {
525 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
530 *blksize
= (int) sfs
.f_frsize
;
532 // Sanity check; make sure we have a meaningful value.
539 #define fsbtoblk(num, fsbs, bs) \
540 (((fsbs) != 0 && (fsbs) < (bs)) ? \
541 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
543 fsStats(const char *path
, int *totl_kb
, int *free_kb
, int *totl_in
, int *free_in
)
547 if (xstatvfs(path
, &sfs
)) {
548 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerror());
552 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_frsize
, 1024);
553 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_frsize
, 1024);
554 *totl_in
= (int) sfs
.f_files
;
555 *free_in
= (int) sfs
.f_ffree
;