]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fs_io.cc
2 * Copyright (C) 1996-2023 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 "StatCounters.h"
22 static PF diskHandleRead
;
23 static PF diskHandleWrite
;
25 #if _SQUID_WINDOWS_ || _SQUID_OS2_
27 diskWriteIsComplete(int fd
)
29 return fd_table
[fd
].disk
.write_q
? 0 : 1;
34 /* hack needed on SunStudio to avoid linkage convention mismatch */
35 static void cxx_xfree(void *ptr
)
41 * opens a disk file specified by 'path'. This function always
42 * blocks! There is no callback.
45 file_open(const char *path
, int mode
)
49 if (FILE_MODE(mode
) == O_WRONLY
)
54 fd
= open(path
, mode
, 0644);
56 ++ statCounter
.syscalls
.disk
.opens
;
60 debugs(50, 3, "error opening file " << path
<< ": " << xstrerr(xerrno
));
63 debugs(6, 5, "FD " << fd
);
64 commSetCloseOnExec(fd
);
65 fd_open(fd
, FD_FILE
, path
);
71 /* close a disk file. */
75 fde
*F
= &fd_table
[fd
];
78 assert(F
->flags
.open
);
80 if ((read_callback
= F
->read_handler
)) {
81 F
->read_handler
= nullptr;
82 read_callback(-1, F
->read_data
);
85 if (F
->flags
.write_daemon
) {
86 #if _SQUID_WINDOWS_ || _SQUID_OS2_
88 * on some operating systems, you can not delete or rename
89 * open files, so we won't allow delayed close.
91 while (!diskWriteIsComplete(fd
))
92 diskHandleWrite(fd
, NULL
);
94 F
->flags
.close_request
= true;
95 debugs(6, 2, "file_close: FD " << fd
<< ", delaying close");
102 * Assert there is no write callback. Otherwise we might be
103 * leaking write state data by closing the descriptor
105 assert(F
->write_handler
== nullptr);
109 debugs(6, F
->flags
.close_request
? 2 : 5, "file_close: FD " << fd
<< " really closing");
113 ++ statCounter
.syscalls
.disk
.closes
;
117 * This function has the purpose of combining multiple writes. This is
118 * to facilitate the ASYNC_IO option since it can only guarantee 1
119 * write to a file per trip around the comm.c select() loop. That's bad
120 * because more than 1 write can be made to the access.log file per
121 * trip, and so this code is purely designed to help batch multiple
122 * sequential writes to the access.log file. Squid will never issue
123 * multiple writes for any other file type during 1 trip around the
124 * select() loop. --SLF
127 diskCombineWrites(_fde_disk
*fdd
)
130 * We need to combine multiple write requests on an FD's write
131 * queue But only if we don't need to seek() in between them, ugh!
132 * XXX This currently ignores any seeks (file_offset)
135 if (fdd
->write_q
!= nullptr && fdd
->write_q
->next
!= nullptr) {
138 for (dwrite_q
*q
= fdd
->write_q
; q
!= nullptr; q
= q
->next
)
139 len
+= q
->len
- q
->buf_offset
;
141 dwrite_q
*wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
143 wq
->buf
= (char *)xmalloc(len
);
151 wq
->free_func
= cxx_xfree
;
153 while (fdd
->write_q
!= nullptr) {
154 dwrite_q
*q
= fdd
->write_q
;
156 len
= q
->len
- q
->buf_offset
;
157 memcpy(wq
->buf
+ wq
->len
, q
->buf
+ q
->buf_offset
, len
);
159 fdd
->write_q
= q
->next
;
162 q
->free_func(q
->buf
);
164 memFree(q
, MEM_DWRITE_Q
);
167 fdd
->write_q_tail
= wq
;
175 diskHandleWrite(int fd
, void *)
178 fde
*F
= &fd_table
[fd
];
180 _fde_disk
*fdd
= &F
->disk
;
181 dwrite_q
*q
= fdd
->write_q
;
182 int status
= DISK_OK
;
188 debugs(6, 3, "diskHandleWrite: FD " << fd
);
190 F
->flags
.write_daemon
= false;
192 assert(fdd
->write_q
!= nullptr);
194 assert(fdd
->write_q
->len
> fdd
->write_q
->buf_offset
);
196 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " writing " <<
197 (fdd
->write_q
->len
- fdd
->write_q
->buf_offset
) << " bytes at " <<
198 fdd
->write_q
->file_offset
);
202 if (fdd
->write_q
->file_offset
!= -1) {
204 if (lseek(fd
, fdd
->write_q
->file_offset
, SEEK_SET
) == -1) {
206 debugs(50, DBG_IMPORTANT
, "ERROR: in seek for FD " << fd
<< ": " << xstrerr(xerrno
));
207 // XXX: handle error?
211 len
= FD_WRITE_METHOD(fd
,
212 fdd
->write_q
->buf
+ fdd
->write_q
->buf_offset
,
213 fdd
->write_q
->len
- fdd
->write_q
->buf_offset
);
214 const auto xerrno
= errno
;
216 debugs(6, 3, "diskHandleWrite: FD " << fd
<< " len = " << len
);
218 ++ statCounter
.syscalls
.disk
.writes
;
220 fd_bytes(fd
, len
, FD_WRITE
);
223 if (!ignoreErrno(xerrno
)) {
224 status
= xerrno
== ENOSPC
? DISK_NO_SPACE_LEFT
: DISK_ERROR
;
225 debugs(50, DBG_IMPORTANT
, "ERROR: diskHandleWrite: FD " << fd
<< ": disk write failure: " << xstrerr(xerrno
));
228 * If there is no write callback, then this file is
229 * most likely something important like a log file, or
230 * an interprocess pipe. Its not a swapfile. We feel
231 * that a write failure on a log file is rather important,
232 * and Squid doesn't otherwise deal with this condition.
233 * So to get the administrators attention, we exit with
237 if (fdd
->wrt_handle
== nullptr)
238 fatal("Write failure -- check your disk space and cache.log");
241 * If there is a write failure, then we notify the
242 * upper layer via the callback, at the end of this
243 * function. Meanwhile, flush all pending buffers
244 * here. Let the upper layer decide how to handle the
245 * failure. This will prevent experiencing multiple,
246 * repeated write failures for the same FD because of
250 fdd
->write_q
= q
->next
;
253 q
->free_func(q
->buf
);
256 memFree(q
, MEM_DWRITE_Q
);
259 } while ((q
= fdd
->write_q
));
266 /* q might become NULL from write failure above */
267 q
->buf_offset
+= len
;
269 if (q
->buf_offset
> q
->len
)
270 debugs(50, DBG_IMPORTANT
, "diskHandleWriteComplete: q->buf_offset > q->len (" <<
271 q
<< "," << (int) q
->buf_offset
<< ", " << q
->len
<< ", " <<
272 len
<< " FD " << fd
<< ")");
274 assert(q
->buf_offset
<= q
->len
);
276 if (q
->buf_offset
== q
->len
) {
278 fdd
->write_q
= q
->next
;
281 q
->free_func(q
->buf
);
284 memFree(q
, MEM_DWRITE_Q
);
290 if (fdd
->write_q
== nullptr) {
292 fdd
->write_q_tail
= nullptr;
294 /* another block is queued */
295 diskCombineWrites(fdd
);
296 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, diskHandleWrite
, nullptr, 0);
297 F
->flags
.write_daemon
= true;
300 do_close
= F
->flags
.close_request
;
302 if (fdd
->wrt_handle
) {
303 DWCB
*callback
= fdd
->wrt_handle
;
305 fdd
->wrt_handle
= nullptr;
307 if (cbdataReferenceValidDone(fdd
->wrt_handle_data
, &cbdata
)) {
308 callback(fd
, status
, len
, cbdata
);
310 * NOTE, this callback can close the FD, so we must
311 * not touch 'F', 'fdd', etc. after this.
314 /* XXX But what about close_request??? */
322 /* write block to a file */
323 /* write back queue. Only one writer at a time. */
324 /* call a handle when writing is complete. */
328 void const *ptr_to_buf
,
334 dwrite_q
*wq
= nullptr;
335 fde
*F
= &fd_table
[fd
];
337 assert(F
->flags
.open
);
338 /* if we got here. Caller is eligible to write. */
339 wq
= (dwrite_q
*)memAllocate(MEM_DWRITE_Q
);
340 wq
->file_offset
= file_offset
;
341 wq
->buf
= (char *)ptr_to_buf
;
345 wq
->free_func
= free_func
;
347 if (!F
->disk
.wrt_handle_data
) {
348 F
->disk
.wrt_handle
= handle
;
349 F
->disk
.wrt_handle_data
= cbdataReference(handle_data
);
351 /* Detect if there is multiple concurrent users of this fd.. we only support one callback */
352 assert(F
->disk
.wrt_handle_data
== handle_data
&& F
->disk
.wrt_handle
== handle
);
356 if (F
->disk
.write_q
== nullptr) {
358 F
->disk
.write_q
= F
->disk
.write_q_tail
= wq
;
360 F
->disk
.write_q_tail
->next
= wq
;
361 F
->disk
.write_q_tail
= wq
;
364 if (!F
->flags
.write_daemon
) {
365 diskHandleWrite(fd
, nullptr);
370 * a wrapper around file_write to allow for MemBuf to be file_written
374 file_write_mbuf(int fd
, off_t off
, MemBuf mb
, DWCB
* handler
, void *handler_data
)
376 file_write(fd
, off
, mb
.buf
, mb
.size
, handler
, handler_data
, mb
.freeFunc());
381 diskHandleRead(int fd
, void *data
)
383 dread_ctrl
*ctrl_dat
= (dread_ctrl
*)data
;
384 fde
*F
= &fd_table
[fd
];
390 * FD < 0 indicates premature close; we just have to free
395 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
399 #if WRITES_MAINTAIN_DISK_OFFSET
400 if (F
->disk
.offset
!= ctrl_dat
->offset
) {
404 debugs(6, 3, "diskHandleRead: FD " << fd
<< " seeking to offset " << ctrl_dat
->offset
);
406 if (lseek(fd
, ctrl_dat
->offset
, SEEK_SET
) == -1) {
408 // shouldn't happen, let's detect that
409 debugs(50, DBG_IMPORTANT
, "ERROR: in seek for FD " << fd
<< ": " << xstrerr(xerrno
));
410 // XXX handle failures?
412 ++ statCounter
.syscalls
.disk
.seeks
;
413 F
->disk
.offset
= ctrl_dat
->offset
;
417 len
= FD_READ_METHOD(fd
, ctrl_dat
->buf
, ctrl_dat
->req_len
);
421 F
->disk
.offset
+= len
;
423 ++ statCounter
.syscalls
.disk
.reads
;
425 fd_bytes(fd
, len
, FD_READ
);
428 if (ignoreErrno(xerrno
)) {
429 Comm::SetSelect(fd
, COMM_SELECT_READ
, diskHandleRead
, ctrl_dat
, 0);
433 debugs(50, DBG_IMPORTANT
, "diskHandleRead: FD " << fd
<< ": " << xstrerr(xerrno
));
436 } else if (len
== 0) {
440 if (cbdataReferenceValid(ctrl_dat
->client_data
))
441 ctrl_dat
->handler(fd
, ctrl_dat
->buf
, len
, rc
, ctrl_dat
->client_data
);
443 cbdataReferenceDone(ctrl_dat
->client_data
);
445 memFree(ctrl_dat
, MEM_DREAD_CTRL
);
448 /* start read operation */
449 /* buffer must be allocated from the caller.
450 * It must have at least req_len space in there.
451 * call handler when a reading is complete. */
453 file_read(int fd
, char *buf
, int req_len
, off_t offset
, DRCB
* handler
, void *client_data
)
455 dread_ctrl
*ctrl_dat
;
457 ctrl_dat
= (dread_ctrl
*)memAllocate(MEM_DREAD_CTRL
);
459 ctrl_dat
->offset
= offset
;
460 ctrl_dat
->req_len
= req_len
;
462 ctrl_dat
->end_of_file
= 0;
463 ctrl_dat
->handler
= handler
;
464 ctrl_dat
->client_data
= cbdataReference(client_data
);
465 diskHandleRead(fd
, ctrl_dat
);
469 safeunlink(const char *s
, int quiet
)
471 ++ statCounter
.syscalls
.disk
.unlinks
;
473 if (unlink(s
) < 0 && !quiet
) {
475 debugs(50, DBG_IMPORTANT
, "ERROR: safeunlink: Could not delete " << s
<< ": " << xstrerr(xerrno
));
480 FileRename(const SBuf
&from
, const SBuf
&to
)
482 debugs(21, 2, "renaming " << from
<< " to " << to
);
484 // non-const copy for c_str()
486 // ensure c_str() lifetimes even if `to` and `from` share memory
487 SBuf
to2(to
.rawContent(), to
.length());
489 #if _SQUID_OS2_ || _SQUID_WINDOWS_
493 if (rename(from2
.c_str(), to2
.c_str()) == 0)
497 debugs(21, (xerrno
== ENOENT
? 2 : DBG_IMPORTANT
), "ERROR: Cannot rename " << from
<< " to " << to
<< ": " << xstrerr(xerrno
));
503 fsBlockSize(const char *path
, int *blksize
)
507 if (xstatvfs(path
, &sfs
)) {
509 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerr(xerrno
));
514 *blksize
= (int) sfs
.f_frsize
;
516 // Sanity check; make sure we have a meaningful value.
523 #define fsbtoblk(num, fsbs, bs) \
524 (((fsbs) != 0 && (fsbs) < (bs)) ? \
525 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
527 fsStats(const char *path
, int *totl_kb
, int *free_kb
, int *totl_in
, int *free_in
)
531 if (xstatvfs(path
, &sfs
)) {
533 debugs(50, DBG_IMPORTANT
, "" << path
<< ": " << xstrerr(xerrno
));
537 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_frsize
, 1024);
538 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_frsize
, 1024);
539 *totl_in
= (int) sfs
.f_files
;
540 *free_in
= (int) sfs
.f_ffree
;