]> git.ipfire.org Git - thirdparty/squid.git/blame - src/disk.cc
Reduce cache_effective_user was leaking $HOME memory
[thirdparty/squid.git] / src / disk.cc
CommitLineData
30a4f2a8 1/*
b510f3a1 2 * DEBUG: section 06 Disk I/O Routines
30a4f2a8 3 * AUTHOR: Harvest Derived
4 *
2b6662ba 5 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 6 * ----------------------------------------------------------
30a4f2a8 7 *
2b6662ba 8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
30a4f2a8 16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
26ac0430 21 *
30a4f2a8 22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26ac0430 26 *
30a4f2a8 27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
cbdec147 29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 30 *
30a4f2a8 31 */
ed43818f 32
582c2af2 33#include "squid.h"
d841c88d 34#include "comm/Loops.h"
65914896 35#include "disk.h"
c4ad1349 36#include "fd.h"
528b2c61 37#include "fde.h"
67679543 38#include "globals.h"
8a89c28f 39#include "Mem.h"
0eb49b6d 40#include "MemBuf.h"
582c2af2 41#include "profiler/Profiler.h"
e4f1fdae 42#include "StatCounters.h"
090089c4 43
21d845b1
FC
44#if HAVE_ERRNO_H
45#include <errno.h>
46#endif
47
95d15928 48static PF diskHandleRead;
49static PF diskHandleWrite;
24382924 50
be266cb2 51#if _SQUID_WINDOWS_ || _SQUID_OS2_
1a94f598 52static int
53diskWriteIsComplete(int fd)
54{
55 return fd_table[fd].disk.write_q ? 0 : 1;
56}
62e76326 57
1a94f598 58#endif
59
04cece06 60void
0673c0ba 61disk_init(void)
090089c4 62{
60df005c 63 (void) 0;
090089c4 64}
65
59a09b98
FC
66/* hack needed on SunStudio to avoid linkage convention mismatch */
67static void cxx_xfree(void *ptr)
68{
f673997d 69 xfree(ptr);
59a09b98
FC
70}
71
1a94f598 72/*
73 * opens a disk file specified by 'path'. This function always
74 * blocks! There is no callback.
75 */
684c2720 76int
1a94f598 77file_open(const char *path, int mode)
090089c4 78{
090089c4 79 int fd;
88bfe092 80 PROF_start(file_open);
62e76326 81
5d1a7121 82 if (FILE_MODE(mode) == O_WRONLY)
62e76326 83 mode |= O_APPEND;
84
b870e0b4 85 errno = 0;
62e76326 86
0a0bf5db 87 fd = open(path, mode, 0644);
62e76326 88
95dc7ff4 89 ++ statCounter.syscalls.disk.opens;
62e76326 90
0a0bf5db 91 if (fd < 0) {
bf8fe701 92 debugs(50, 3, "file_open: error opening file " << path << ": " << xstrerror());
62e76326 93 fd = DISK_ERROR;
2391a162 94 } else {
bf8fe701 95 debugs(6, 5, "file_open: FD " << fd);
62e76326 96 commSetCloseOnExec(fd);
97 fd_open(fd, FD_FILE, path);
090089c4 98 }
62e76326 99
88bfe092 100 PROF_stop(file_open);
2391a162 101 return fd;
0a0bf5db 102}
103
090089c4 104/* close a disk file. */
95d15928 105void
b8d8561b 106file_close(int fd)
090089c4 107{
76f87348 108 fde *F = &fd_table[fd];
2391a162 109 PF *read_callback;
88bfe092 110 PROF_start(file_close);
25354045 111 assert(fd >= 0);
60c0b5a2 112 assert(F->flags.open);
62e76326 113
2391a162 114 if ((read_callback = F->read_handler)) {
62e76326 115 F->read_handler = NULL;
116 read_callback(-1, F->read_data);
65d548bf 117 }
62e76326 118
0cd30ba5 119 if (F->flags.write_daemon) {
be266cb2 120#if _SQUID_WINDOWS_ || _SQUID_OS2_
62e76326 121 /*
122 * on some operating systems, you can not delete or rename
123 * open files, so we won't allow delayed close.
124 */
62e76326 125 while (!diskWriteIsComplete(fd))
126 diskHandleWrite(fd, NULL);
cd377065 127#else
be4d35dc 128 F->flags.close_request = true;
bf8fe701 129 debugs(6, 2, "file_close: FD " << fd << ", delaying close");
62e76326 130 PROF_stop(file_close);
62e76326 131 return;
cd377065 132#endif
62e76326 133
fb247d78 134 }
62e76326 135
65d548bf 136 /*
137 * Assert there is no write callback. Otherwise we might be
138 * leaking write state data by closing the descriptor
139 */
140 assert(F->write_handler == NULL);
62e76326 141
42f99d0d 142#if CALL_FSYNC_BEFORE_CLOSE
62e76326 143
42f99d0d 144 fsync(fd);
62e76326 145
42f99d0d 146#endif
62e76326 147
95d15928 148 close(fd);
62e76326 149
bf8fe701 150 debugs(6, F->flags.close_request ? 2 : 5, "file_close: FD " << fd << " really closing\n");
62e76326 151
6cf028ab 152 fd_close(fd);
62e76326 153
95dc7ff4 154 ++ statCounter.syscalls.disk.closes;
62e76326 155
88bfe092 156 PROF_stop(file_close);
090089c4 157}
158
f02b8498 159/*
160 * This function has the purpose of combining multiple writes. This is
161 * to facilitate the ASYNC_IO option since it can only guarantee 1
162 * write to a file per trip around the comm.c select() loop. That's bad
163 * because more than 1 write can be made to the access.log file per
164 * trip, and so this code is purely designed to help batch multiple
165 * sequential writes to the access.log file. Squid will never issue
166 * multiple writes for any other file type during 1 trip around the
167 * select() loop. --SLF
168 */
582b6456 169static void
5fed1735 170diskCombineWrites(_fde_disk *fdd)
090089c4 171{
f02b8498 172 /*
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)
176 */
62e76326 177
26ac0430 178 if (fdd->write_q != NULL && fdd->write_q->next != NULL) {
b115733c 179 int len = 0;
62e76326 180
b115733c 181 for (dwrite_q *q = fdd->write_q; q != NULL; q = q->next)
62e76326 182 len += q->len - q->buf_offset;
183
b115733c 184 dwrite_q *wq = (dwrite_q *)memAllocate(MEM_DWRITE_Q);
62e76326 185
186 wq->buf = (char *)xmalloc(len);
187
188 wq->len = 0;
189
190 wq->buf_offset = 0;
191
192 wq->next = NULL;
193
59a09b98 194 wq->free_func = cxx_xfree;
62e76326 195
b115733c
AJ
196 while (fdd->write_q != NULL) {
197 dwrite_q *q = fdd->write_q;
198
62e76326 199 len = q->len - q->buf_offset;
41d00cd3 200 memcpy(wq->buf + wq->len, q->buf + q->buf_offset, len);
62e76326 201 wq->len += len;
202 fdd->write_q = q->next;
203
204 if (q->free_func)
6ca34f6f 205 q->free_func(q->buf);
62e76326 206
b115733c
AJ
207 memFree(q, MEM_DWRITE_Q);
208 };
62e76326 209
210 fdd->write_q_tail = wq;
211
212 fdd->write_q = wq;
0a0bf5db 213 }
f02b8498 214}
215
216/* write handler */
217static void
218diskHandleWrite(int fd, void *notused)
219{
220 int len = 0;
f02b8498 221 fde *F = &fd_table[fd];
62e76326 222
5fed1735 223 _fde_disk *fdd = &F->disk;
2391a162 224 dwrite_q *q = fdd->write_q;
225 int status = DISK_OK;
be4d35dc 226 bool do_close;
62e76326 227
2391a162 228 if (NULL == q)
62e76326 229 return;
230
88bfe092 231 PROF_start(diskHandleWrite);
62e76326 232
bf8fe701 233 debugs(6, 3, "diskHandleWrite: FD " << fd);
62e76326 234
be4d35dc 235 F->flags.write_daemon = false;
62e76326 236
8350fe9b 237 assert(fdd->write_q != NULL);
62e76326 238
d377699f 239 assert(fdd->write_q->len > fdd->write_q->buf_offset);
62e76326 240
e2851fe7
AR
241 debugs(6, 3, "diskHandleWrite: FD " << fd << " writing " <<
242 (fdd->write_q->len - fdd->write_q->buf_offset) << " bytes at " <<
243 fdd->write_q->file_offset);
62e76326 244
b870e0b4 245 errno = 0;
62e76326 246
cd748f27 247 if (fdd->write_q->file_offset != -1)
e2851fe7 248 lseek(fd, fdd->write_q->file_offset, SEEK_SET); /* XXX ignore return? */
62e76326 249
1f7c9178 250 len = FD_WRITE_METHOD(fd,
62e76326 251 fdd->write_q->buf + fdd->write_q->buf_offset,
252 fdd->write_q->len - fdd->write_q->buf_offset);
253
bf8fe701 254 debugs(6, 3, "diskHandleWrite: FD " << fd << " len = " << len);
62e76326 255
95dc7ff4 256 ++ statCounter.syscalls.disk.writes;
62e76326 257
6cf028ab 258 fd_bytes(fd, len, FD_WRITE);
62e76326 259
0a0bf5db 260 if (len < 0) {
62e76326 261 if (!ignoreErrno(errno)) {
262 status = errno == ENOSPC ? DISK_NO_SPACE_LEFT : DISK_ERROR;
e0236918 263 debugs(50, DBG_IMPORTANT, "diskHandleWrite: FD " << fd << ": disk write error: " << xstrerror());
bf8fe701 264
62e76326 265 /*
266 * If there is no write callback, then this file is
267 * most likely something important like a log file, or
268 * an interprocess pipe. Its not a swapfile. We feel
269 * that a write failure on a log file is rather important,
270 * and Squid doesn't otherwise deal with this condition.
271 * So to get the administrators attention, we exit with
272 * a fatal message.
273 */
274
275 if (fdd->wrt_handle == NULL)
276 fatal("Write failure -- check your disk space and cache.log");
277
278 /*
279 * If there is a write failure, then we notify the
280 * upper layer via the callback, at the end of this
281 * function. Meanwhile, flush all pending buffers
282 * here. Let the upper layer decide how to handle the
283 * failure. This will prevent experiencing multiple,
284 * repeated write failures for the same FD because of
285 * the queued data.
286 */
287 do {
288 fdd->write_q = q->next;
289
290 if (q->free_func)
6ca34f6f 291 q->free_func(q->buf);
62e76326 292
293 if (q) {
294 memFree(q, MEM_DWRITE_Q);
295 q = NULL;
296 }
297 } while ((q = fdd->write_q));
298 }
299
300 len = 0;
0a0bf5db 301 }
62e76326 302
8350fe9b 303 if (q != NULL) {
62e76326 304 /* q might become NULL from write failure above */
305 q->buf_offset += len;
306
307 if (q->buf_offset > q->len)
e0236918 308 debugs(50, DBG_IMPORTANT, "diskHandleWriteComplete: q->buf_offset > q->len (" <<
bf8fe701 309 q << "," << (int) q->buf_offset << ", " << q->len << ", " <<
310 len << " FD " << fd << ")");
311
62e76326 312 assert(q->buf_offset <= q->len);
313
314 if (q->buf_offset == q->len) {
315 /* complete write */
316 fdd->write_q = q->next;
317
318 if (q->free_func)
6ca34f6f 319 q->free_func(q->buf);
62e76326 320
321 if (q) {
322 memFree(q, MEM_DWRITE_Q);
323 q = NULL;
324 }
325 }
090089c4 326 }
62e76326 327
de866d20 328 if (fdd->write_q == NULL) {
62e76326 329 /* no more data */
330 fdd->write_q_tail = NULL;
de866d20 331 } else {
62e76326 332 /* another block is queued */
333 diskCombineWrites(fdd);
d841c88d 334 Comm::SetSelect(fd, COMM_SELECT_WRITE, diskHandleWrite, NULL, 0);
be4d35dc 335 F->flags.write_daemon = true;
4a86108c 336 }
62e76326 337
0cd30ba5 338 do_close = F->flags.close_request;
62e76326 339
25354045 340 if (fdd->wrt_handle) {
62e76326 341 DWCB *callback = fdd->wrt_handle;
342 void *cbdata;
343 fdd->wrt_handle = NULL;
344
345 if (cbdataReferenceValidDone(fdd->wrt_handle_data, &cbdata)) {
346 callback(fd, status, len, cbdata);
347 /*
348 * NOTE, this callback can close the FD, so we must
349 * not touch 'F', 'fdd', etc. after this.
350 */
351 PROF_stop(diskHandleWrite);
352 return;
353 /* XXX But what about close_request??? */
354 }
25354045 355 }
62e76326 356
68c21f71 357 if (do_close)
62e76326 358 file_close(fd);
359
88bfe092 360 PROF_stop(diskHandleWrite);
090089c4 361}
362
090089c4 363/* write block to a file */
364/* write back queue. Only one writer at a time. */
365/* call a handle when writing is complete. */
e3ef2b09 366void
3ebcfaa1 367file_write(int fd,
62e76326 368 off_t file_offset,
369 void const *ptr_to_buf,
370 int len,
371 DWCB * handle,
372 void *handle_data,
373 FREE * free_func)
090089c4 374{
c6ac7aae 375 dwrite_q *wq = NULL;
48cc3fcf 376 fde *F = &fd_table[fd];
88bfe092 377 PROF_start(file_write);
48cc3fcf 378 assert(fd >= 0);
60c0b5a2 379 assert(F->flags.open);
090089c4 380 /* if we got here. Caller is eligible to write. */
e6ccf245 381 wq = (dwrite_q *)memAllocate(MEM_DWRITE_Q);
d377699f 382 wq->file_offset = file_offset;
e6ccf245 383 wq->buf = (char *)ptr_to_buf;
090089c4 384 wq->len = len;
d377699f 385 wq->buf_offset = 0;
090089c4 386 wq->next = NULL;
ed7f0b6a 387 wq->free_func = free_func;
62e76326 388
fa80a8ef 389 if (!F->disk.wrt_handle_data) {
62e76326 390 F->disk.wrt_handle = handle;
391 F->disk.wrt_handle_data = cbdataReference(handle_data);
fa80a8ef 392 } else {
62e76326 393 /* Detect if there is multiple concurrent users of this fd.. we only support one callback */
394 assert(F->disk.wrt_handle_data == handle_data && F->disk.wrt_handle == handle);
fa80a8ef 395 }
62e76326 396
090089c4 397 /* add to queue */
48cc3fcf 398 if (F->disk.write_q == NULL) {
62e76326 399 /* empty queue */
400 F->disk.write_q = F->disk.write_q_tail = wq;
090089c4 401 } else {
62e76326 402 F->disk.write_q_tail->next = wq;
403 F->disk.write_q_tail = wq;
090089c4 404 }
62e76326 405
0cd30ba5 406 if (!F->flags.write_daemon) {
62e76326 407 diskHandleWrite(fd, NULL);
429fdbec 408 }
62e76326 409
88bfe092 410 PROF_stop(file_write);
090089c4 411}
412
23b2b404 413/*
414 * a wrapper around file_write to allow for MemBuf to be file_written
415 * in a snap
416 */
137ee196 417void
418file_write_mbuf(int fd, off_t off, MemBuf mb, DWCB * handler, void *handler_data)
419{
2fe7eff9 420 file_write(fd, off, mb.buf, mb.size, handler, handler_data, mb.freeFunc());
137ee196 421}
090089c4 422
423/* Read from FD */
582b6456 424static void
425diskHandleRead(int fd, void *data)
090089c4 426{
e6ccf245 427 dread_ctrl *ctrl_dat = (dread_ctrl *)data;
edd2eb63 428 fde *F = &fd_table[fd];
090089c4 429 int len;
2391a162 430 int rc = DISK_OK;
65d548bf 431 /*
432 * FD < 0 indicates premature close; we just have to free
433 * the state data.
434 */
62e76326 435
65d548bf 436 if (fd < 0) {
62e76326 437 memFree(ctrl_dat, MEM_DREAD_CTRL);
438 return;
65d548bf 439 }
62e76326 440
88bfe092 441 PROF_start(diskHandleRead);
62e76326 442
034b5ea4 443#if WRITES_MAINTAIN_DISK_OFFSET
711982d8 444 if (F->disk.offset != ctrl_dat->offset) {
034b5ea4
AR
445#else
446 {
447#endif
4a7a3d56 448 debugs(6, 3, "diskHandleRead: FD " << fd << " seeking to offset " << ctrl_dat->offset);
62e76326 449 lseek(fd, ctrl_dat->offset, SEEK_SET); /* XXX ignore return? */
95dc7ff4 450 ++ statCounter.syscalls.disk.seeks;
62e76326 451 F->disk.offset = ctrl_dat->offset;
711982d8 452 }
62e76326 453
b870e0b4 454 errno = 0;
1f7c9178 455 len = FD_READ_METHOD(fd, ctrl_dat->buf, ctrl_dat->req_len);
62e76326 456
015b507a 457 if (len > 0)
62e76326 458 F->disk.offset += len;
459
95dc7ff4 460 ++ statCounter.syscalls.disk.reads;
62e76326 461
4f92c80c 462 fd_bytes(fd, len, FD_READ);
62e76326 463
0a0bf5db 464 if (len < 0) {
62e76326 465 if (ignoreErrno(errno)) {
d841c88d 466 Comm::SetSelect(fd, COMM_SELECT_READ, diskHandleRead, ctrl_dat, 0);
62e76326 467 PROF_stop(diskHandleRead);
468 return;
469 }
470
e0236918 471 debugs(50, DBG_IMPORTANT, "diskHandleRead: FD " << fd << ": " << xstrerror());
62e76326 472 len = 0;
473 rc = DISK_ERROR;
090089c4 474 } else if (len == 0) {
62e76326 475 rc = DISK_EOF;
090089c4 476 }
62e76326 477
fa80a8ef 478 if (cbdataReferenceValid(ctrl_dat->client_data))
62e76326 479 ctrl_dat->handler(fd, ctrl_dat->buf, len, rc, ctrl_dat->client_data);
480
fa80a8ef 481 cbdataReferenceDone(ctrl_dat->client_data);
62e76326 482
db1cd23c 483 memFree(ctrl_dat, MEM_DREAD_CTRL);
62e76326 484
88bfe092 485 PROF_stop(diskHandleRead);
090089c4 486}
487
090089c4 488/* start read operation */
62e76326 489/* buffer must be allocated from the caller.
26ac0430 490 * It must have at least req_len space in there.
090089c4 491 * call handler when a reading is complete. */
2391a162 492void
d377699f 493file_read(int fd, char *buf, int req_len, off_t offset, DRCB * handler, void *client_data)
090089c4 494{
495 dread_ctrl *ctrl_dat;
88bfe092 496 PROF_start(file_read);
711982d8 497 assert(fd >= 0);
e6ccf245 498 ctrl_dat = (dread_ctrl *)memAllocate(MEM_DREAD_CTRL);
090089c4 499 ctrl_dat->fd = fd;
500 ctrl_dat->offset = offset;
501 ctrl_dat->req_len = req_len;
502 ctrl_dat->buf = buf;
090089c4 503 ctrl_dat->end_of_file = 0;
504 ctrl_dat->handler = handler;
fa80a8ef 505 ctrl_dat->client_data = cbdataReference(client_data);
0a0bf5db 506 diskHandleRead(fd, ctrl_dat);
88bfe092 507 PROF_stop(file_read);
090089c4 508}
c8f4eac4 509
510void
511safeunlink(const char *s, int quiet)
512{
95dc7ff4 513 ++ statCounter.syscalls.disk.unlinks;
c8f4eac4 514
515 if (unlink(s) < 0 && !quiet)
e0236918 516 debugs(50, DBG_IMPORTANT, "safeunlink: Couldn't delete " << s << ": " << xstrerror());
c8f4eac4 517}
518
519/*
520 * Same as rename(2) but complains if something goes wrong;
26ac0430 521 * the caller is responsible for handing and explaining the
c8f4eac4 522 * consequences of errors.
523 */
524int
525xrename(const char *from, const char *to)
526{
bf8fe701 527 debugs(21, 2, "xrename: renaming " << from << " to " << to);
be266cb2 528#if _SQUID_OS2_ || _SQUID_WINDOWS_
6ca34f6f 529 remove(to);
c8f4eac4 530#endif
531
532 if (0 == rename(from, to))
533 return 0;
534
bf8fe701 535 debugs(21, errno == ENOENT ? 2 : 1, "xrename: Cannot rename " << from << " to " << to << ": " << xstrerror());
c8f4eac4 536
537 return -1;
538}
539