📄 store_io_aufs.c
字号:
/* * $Id: store_io_aufs.c,v 1.33 2006/09/09 16:04:38 serassio Exp $ * * DEBUG: section 79 Squid-side AUFS I/O functions. * * SQUID Web Proxy Cache http://www.squid-cache.org/ * ---------------------------------------------------------- * * Squid is the result of efforts by numerous individuals from * the Internet community; see the CONTRIBUTORS file for full * details. Many organizations have provided support for Squid's * development; see the SPONSORS file for full details. Squid is * Copyrighted (C) 2001 by the Regents of the University of * California; see the COPYRIGHT file for full details. Squid * incorporates software developed and/or copyrighted by other * sources; see the CREDITS file for full details. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. * */#include "squid.h"#include "async_io.h"#include "store_asyncufs.h"#if ASYNC_READstatic AIOCB storeAufsReadDone;#elsestatic DRCB storeAufsReadDone;#endif#if ASYNC_WRITEstatic AIOCB storeAufsWriteDone;#elsestatic DWCB storeAufsWriteDone;#endifstatic void storeAufsIOCallback(storeIOState * sio, int errflag);static AIOCB storeAufsOpenDone;static int storeAufsNeedCompletetion(storeIOState *);static int storeAufsKickWriteQueue(storeIOState * sio);static CBDUNL storeAufsIOFreeEntry;CBDATA_TYPE(storeIOState);/* === PUBLIC =========================================================== *//* open for reading */storeIOState *storeAufsOpen(SwapDir * SD, StoreEntry * e, STFNCB * file_callback, STIOCB * callback, void *callback_data){ sfileno f = e->swap_filen; char *path = storeAufsDirFullPath(SD, f, NULL); storeIOState *sio;#if !ASYNC_OPEN int fd;#endif debug(79, 3) ("storeAufsOpen: fileno %08X\n", f); /* * we should detect some 'too many files open' condition and return * NULL here. */#ifdef MAGIC2 if (aioQueueSize() > MAGIC2) return NULL;#endif#if !ASYNC_OPEN fd = file_open(path, O_RDONLY | O_BINARY | O_NOATIME); if (fd < 0) { debug(79, 3) ("storeAufsOpen: got failure (%d)\n", errno); return NULL; }#endif CBDATA_INIT_TYPE_FREECB(storeIOState, storeAufsIOFreeEntry); sio = cbdataAlloc(storeIOState); sio->fsstate = memPoolAlloc(squidaio_state_pool); ((squidaiostate_t *) (sio->fsstate))->fd = -1; ((squidaiostate_t *) (sio->fsstate))->flags.opening = 1; sio->swap_filen = f; sio->swap_dirn = SD->index; sio->mode = O_RDONLY | O_BINARY; sio->callback = callback; sio->callback_data = callback_data; sio->e = e; cbdataLock(callback_data); Opening_FD++; statCounter.syscalls.disk.opens++;#if ASYNC_OPEN aioOpen(path, O_RDONLY | O_BINARY | O_NOATIME, 0644, storeAufsOpenDone, sio);#else storeAufsOpenDone(fd, sio, fd, 0);#endif return sio;}/* open for creating */storeIOState *storeAufsCreate(SwapDir * SD, StoreEntry * e, STFNCB * file_callback, STIOCB * callback, void *callback_data){ char *path; storeIOState *sio; sfileno filn; sdirno dirn;#if !ASYNC_CREATE int fd;#endif /* Allocate a number */ dirn = SD->index; filn = storeAufsDirMapBitAllocate(SD); path = storeAufsDirFullPath(SD, filn, NULL); debug(79, 3) ("storeAufsCreate: fileno %08X\n", filn); /* * we should detect some 'too many files open' condition and return * NULL here. */#ifdef MAGIC2 if (aioQueueSize() > MAGIC2) return NULL;#endif#if !ASYNC_CREATE fd = file_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY); if (fd < 0) { debug(79, 3) ("storeAufsCreate: got failure (%d)\n", errno); return NULL; }#endif CBDATA_INIT_TYPE_FREECB(storeIOState, storeAufsIOFreeEntry); sio = cbdataAlloc(storeIOState); sio->fsstate = memPoolAlloc(squidaio_state_pool); ((squidaiostate_t *) (sio->fsstate))->fd = -1; ((squidaiostate_t *) (sio->fsstate))->flags.opening = 1; sio->swap_filen = filn; sio->swap_dirn = dirn; sio->mode = O_WRONLY | O_BINARY; sio->callback = callback; sio->callback_data = callback_data; sio->e = (StoreEntry *) e; cbdataLock(callback_data); Opening_FD++; statCounter.syscalls.disk.opens++;#if ASYNC_CREATE aioOpen(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644, storeAufsOpenDone, sio);#else storeAufsOpenDone(fd, sio, fd, 0);#endif /* now insert into the replacement policy */ storeAufsDirReplAdd(SD, e); return sio;}/* Close */voidstoreAufsClose(SwapDir * SD, storeIOState * sio){ squidaiostate_t *aiostate = (squidaiostate_t *) sio->fsstate; debug(79, 3) ("storeAufsClose: dirno %d, fileno %08X, FD %d\n", sio->swap_dirn, sio->swap_filen, aiostate->fd); if (storeAufsNeedCompletetion(sio)) { aiostate->flags.close_request = 1; return; } storeAufsIOCallback(sio, DISK_OK);}/* Read */voidstoreAufsRead(SwapDir * SD, storeIOState * sio, char *buf, size_t size, squid_off_t offset, STRCB * callback, void *callback_data){ squidaiostate_t *aiostate = (squidaiostate_t *) sio->fsstate; assert(sio->read.callback == NULL); assert(sio->read.callback_data == NULL); assert(!aiostate->flags.reading); if (aiostate->fd < 0) { struct _queued_read *q; debug(79, 3) ("storeAufsRead: queueing read because FD < 0\n"); assert(aiostate->flags.opening); assert(aiostate->pending_reads == NULL); q = memPoolAlloc(aufs_qread_pool); q->buf = buf; q->size = size; q->offset = (off_t) offset; q->callback = callback; q->callback_data = callback_data; cbdataLock(q->callback_data); linklistPush(&(aiostate->pending_reads), q); return; } sio->read.callback = callback; sio->read.callback_data = callback_data; aiostate->read_buf = buf; cbdataLock(callback_data); debug(79, 3) ("storeAufsRead: dirno %d, fileno %08X, FD %d\n", sio->swap_dirn, sio->swap_filen, aiostate->fd); sio->offset = offset; aiostate->flags.reading = 1;#if ASYNC_READ aioRead(aiostate->fd, (off_t) offset, size, storeAufsReadDone, sio); statCounter.syscalls.disk.reads++;#else file_read(aiostate->fd, buf, size, (off_t) offset, storeAufsReadDone, sio); /* file_read() increments syscalls.disk.reads */#endif}/* Write */voidstoreAufsWrite(SwapDir * SD, storeIOState * sio, char *buf, size_t size, squid_off_t offset, FREE * free_func){ squidaiostate_t *aiostate = (squidaiostate_t *) sio->fsstate; debug(79, 3) ("storeAufsWrite: dirno %d, fileno %08X, FD %d\n", sio->swap_dirn, sio->swap_filen, aiostate->fd); if (aiostate->fd < 0) { /* disk file not opened yet */ struct _queued_write *q; assert(aiostate->flags.opening); q = memPoolAlloc(aufs_qwrite_pool); q->buf = buf; q->size = size; q->offset = (off_t) offset; q->free_func = free_func; linklistPush(&(aiostate->pending_writes), q); return; }#if ASYNC_WRITE if (aiostate->flags.writing) { struct _queued_write *q; debug(79, 3) ("storeAufsWrite: queuing write\n"); q = memPoolAlloc(aufs_qwrite_pool); q->buf = buf; q->size = size; q->offset = (off_t) offset; q->free_func = free_func; linklistPush(&(aiostate->pending_writes), q); return; } aiostate->flags.writing = 1; aioWrite(aiostate->fd, (off_t) offset, buf, size, storeAufsWriteDone, sio, free_func); statCounter.syscalls.disk.writes++;#else file_write(aiostate->fd, (off_t) offset, buf, size, storeAufsWriteDone, sio, free_func); /* file_write() increments syscalls.disk.writes */#endif}/* Unlink */voidstoreAufsUnlink(SwapDir * SD, StoreEntry * e){ debug(79, 3) ("storeAufsUnlink: dirno %d, fileno %08X\n", SD->index, e->swap_filen); storeAufsDirReplRemove(e); storeAufsDirMapBitReset(SD, e->swap_filen); storeAufsDirUnlinkFile(SD, e->swap_filen); statCounter.syscalls.disk.unlinks++;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -