]> git.ipfire.org Git - thirdparty/squid.git/blame - src/fs/ufs/store_io_ufs.cc
Updated copyright
[thirdparty/squid.git] / src / fs / ufs / store_io_ufs.cc
CommitLineData
cd748f27 1
2/*
2b6662ba 3 * $Id: store_io_ufs.cc,v 1.6 2001/01/12 00:37:35 wessels Exp $
cd748f27 4 *
5 * DEBUG: section 79 Storage Manager UFS Interface
6 * AUTHOR: Duane Wessels
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
cd748f27 9 * ----------------------------------------------------------
10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
cd748f27 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
2b6662ba 24 *
cd748f27 25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
2b6662ba 29 *
cd748f27 30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36#include "squid.h"
37#include "store_ufs.h"
38
39
40static DRCB storeUfsReadDone;
41static DWCB storeUfsWriteDone;
42static void storeUfsIOCallback(storeIOState * sio, int errflag);
28c60158 43static CBDUNL storeUfsIOFreeEntry;
cd748f27 44
45/* === PUBLIC =========================================================== */
46
47storeIOState *
a4b8110e 48storeUfsOpen(SwapDir * SD, StoreEntry * e, STFNCB * file_callback,
49 STIOCB * callback, void *callback_data)
cd748f27 50{
51 sfileno f = e->swap_filen;
52 char *path = storeUfsDirFullPath(SD, f, NULL);
53 storeIOState *sio;
54 struct stat sb;
55 int fd;
56 debug(79, 3) ("storeUfsOpen: fileno %08X\n", f);
c4aefe96 57 fd = file_open(path, O_RDONLY | O_BINARY);
cd748f27 58 if (fd < 0) {
59 debug(79, 3) ("storeUfsOpen: got failure (%d)\n", errno);
60 return NULL;
61 }
62 debug(79, 3) ("storeUfsOpen: opened FD %d\n", fd);
28c60158 63 sio = CBDATA_ALLOC(storeIOState, storeUfsIOFreeEntry);
cd748f27 64 sio->fsstate = memPoolAlloc(ufs_state_pool);
65
66 sio->swap_filen = f;
67 sio->swap_dirn = SD->index;
68 sio->mode = O_RDONLY;
69 sio->callback = callback;
70 sio->callback_data = callback_data;
71 cbdataLock(callback_data);
72 sio->e = e;
a4b8110e 73 ((ufsstate_t *) (sio->fsstate))->fd = fd;
74 ((ufsstate_t *) (sio->fsstate))->flags.writing = 0;
75 ((ufsstate_t *) (sio->fsstate))->flags.reading = 0;
76 ((ufsstate_t *) (sio->fsstate))->flags.close_request = 0;
cd748f27 77 if (fstat(fd, &sb) == 0)
a4b8110e 78 sio->st_size = sb.st_size;
cd748f27 79 store_open_disk_fd++;
80
81 /* We should update the heap/dlink position here ! */
82 return sio;
83}
84
85storeIOState *
a4b8110e 86storeUfsCreate(SwapDir * SD, StoreEntry * e, STFNCB * file_callback, STIOCB * callback, void *callback_data)
cd748f27 87{
88 storeIOState *sio;
89 int fd;
c4aefe96 90 int mode = (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY);
cd748f27 91 char *path;
a4b8110e 92 ufsinfo_t *ufsinfo = (ufsinfo_t *) SD->fsdata;
cd748f27 93 sfileno filn;
94 sdirno dirn;
95
96 /* Allocate a number */
97 dirn = SD->index;
98 filn = storeUfsDirMapBitAllocate(SD);
99 ufsinfo->suggest = filn + 1;
100 /* Shouldn't we handle a 'bitmap full' error here? */
101 path = storeUfsDirFullPath(SD, filn, NULL);
102
103 debug(79, 3) ("storeUfsCreate: fileno %08X\n", filn);
104 fd = file_open(path, mode);
105 if (fd < 0) {
a4b8110e 106 debug(79, 3) ("storeUfsCreate: got failure (%d)\n", errno);
107 return NULL;
cd748f27 108 }
109 debug(79, 3) ("storeUfsCreate: opened FD %d\n", fd);
28c60158 110 sio = CBDATA_ALLOC(storeIOState, storeUfsIOFreeEntry);
cd748f27 111 sio->fsstate = memPoolAlloc(ufs_state_pool);
112
113 sio->swap_filen = filn;
114 sio->swap_dirn = dirn;
115 sio->mode = mode;
116 sio->callback = callback;
117 sio->callback_data = callback_data;
118 cbdataLock(callback_data);
119 sio->e = (StoreEntry *) e;
a4b8110e 120 ((ufsstate_t *) (sio->fsstate))->fd = fd;
121 ((ufsstate_t *) (sio->fsstate))->flags.writing = 0;
122 ((ufsstate_t *) (sio->fsstate))->flags.reading = 0;
123 ((ufsstate_t *) (sio->fsstate))->flags.close_request = 0;
cd748f27 124 store_open_disk_fd++;
125
126 /* now insert into the replacement policy */
127 storeUfsDirReplAdd(SD, e);
128 return sio;
129}
130
131void
a4b8110e 132storeUfsClose(SwapDir * SD, storeIOState * sio)
cd748f27 133{
a4b8110e 134 ufsstate_t *ufsstate = (ufsstate_t *) sio->fsstate;
cd748f27 135
136 debug(79, 3) ("storeUfsClose: dirno %d, fileno %08X, FD %d\n",
137 sio->swap_dirn, sio->swap_filen, ufsstate->fd);
138 if (ufsstate->flags.reading || ufsstate->flags.writing) {
139 ufsstate->flags.close_request = 1;
140 return;
141 }
142 storeUfsIOCallback(sio, 0);
143}
144
145void
a4b8110e 146storeUfsRead(SwapDir * SD, storeIOState * sio, char *buf, size_t size, off_t offset, STRCB * callback, void *callback_data)
cd748f27 147{
a4b8110e 148 ufsstate_t *ufsstate = (ufsstate_t *) sio->fsstate;
cd748f27 149
150 assert(sio->read.callback == NULL);
151 assert(sio->read.callback_data == NULL);
152 sio->read.callback = callback;
153 sio->read.callback_data = callback_data;
154 cbdataLock(callback_data);
155 debug(79, 3) ("storeUfsRead: dirno %d, fileno %08X, FD %d\n",
156 sio->swap_dirn, sio->swap_filen, ufsstate->fd);
157 sio->offset = offset;
158 ufsstate->flags.reading = 1;
159 file_read(ufsstate->fd,
160 buf,
161 size,
162 offset,
163 storeUfsReadDone,
164 sio);
165}
166
167void
a4b8110e 168storeUfsWrite(SwapDir * SD, storeIOState * sio, char *buf, size_t size, off_t offset, FREE * free_func)
cd748f27 169{
a4b8110e 170 ufsstate_t *ufsstate = (ufsstate_t *) sio->fsstate;
cd748f27 171 debug(79, 3) ("storeUfsWrite: dirn %d, fileno %08X, FD %d\n", sio->swap_dirn, sio->swap_filen, ufsstate->fd);
172 ufsstate->flags.writing = 1;
173 file_write(ufsstate->fd,
174 offset,
175 buf,
176 size,
177 storeUfsWriteDone,
178 sio,
179 free_func);
180}
181
182void
a4b8110e 183storeUfsUnlink(SwapDir * SD, StoreEntry * e)
cd748f27 184{
185 debug(79, 3) ("storeUfsUnlink: fileno %08X\n", e->swap_filen);
186 storeUfsDirReplRemove(e);
93041634 187 storeUfsDirMapBitReset(SD, e->swap_filen);
cd748f27 188 storeUfsDirUnlinkFile(SD, e->swap_filen);
189}
190
191/* === STATIC =========================================================== */
192
193static void
194storeUfsReadDone(int fd, const char *buf, int len, int errflag, void *my_data)
195{
196 storeIOState *sio = my_data;
a4b8110e 197 ufsstate_t *ufsstate = (ufsstate_t *) sio->fsstate;
cd748f27 198 STRCB *callback = sio->read.callback;
199 void *their_data = sio->read.callback_data;
200 ssize_t rlen;
201
202 debug(79, 3) ("storeUfsReadDone: dirno %d, fileno %08X, FD %d, len %d\n",
203 sio->swap_dirn, sio->swap_filen, fd, len);
204 ufsstate->flags.reading = 0;
205 if (errflag) {
206 debug(79, 3) ("storeUfsReadDone: got failure (%d)\n", errflag);
a4b8110e 207 rlen = -1;
cd748f27 208 } else {
a4b8110e 209 rlen = (ssize_t) len;
210 sio->offset += len;
cd748f27 211 }
212 assert(callback);
213 assert(their_data);
214 sio->read.callback = NULL;
215 sio->read.callback_data = NULL;
216 if (cbdataValid(their_data))
217 callback(their_data, buf, (size_t) rlen);
218 cbdataUnlock(their_data);
219}
220
221static void
222storeUfsWriteDone(int fd, int errflag, size_t len, void *my_data)
223{
224 storeIOState *sio = my_data;
a4b8110e 225 ufsstate_t *ufsstate = (ufsstate_t *) sio->fsstate;
cd748f27 226 debug(79, 3) ("storeUfsWriteDone: dirno %d, fileno %08X, FD %d, len %d\n",
227 sio->swap_dirn, sio->swap_filen, fd, len);
228 ufsstate->flags.writing = 0;
229 if (errflag) {
230 debug(79, 0) ("storeUfsWriteDone: got failure (%d)\n", errflag);
231 storeUfsIOCallback(sio, errflag);
232 return;
233 }
234 sio->offset += len;
235 if (ufsstate->flags.close_request)
236 storeUfsIOCallback(sio, errflag);
237}
238
239static void
240storeUfsIOCallback(storeIOState * sio, int errflag)
241{
a4b8110e 242 ufsstate_t *ufsstate = (ufsstate_t *) sio->fsstate;
cd748f27 243 debug(79, 3) ("storeUfsIOCallback: errflag=%d\n", errflag);
244 if (ufsstate->fd > -1) {
245 file_close(ufsstate->fd);
246 store_open_disk_fd--;
247 }
248 if (cbdataValid(sio->callback_data))
249 sio->callback(sio->callback_data, errflag, sio);
250 cbdataUnlock(sio->callback_data);
251 sio->callback_data = NULL;
252 sio->callback = NULL;
253 cbdataFree(sio);
254}
255
256
257/*
28c60158 258 * Clean up any references from the SIO before it get's released.
cd748f27 259 */
260static void
28c60158 261storeUfsIOFreeEntry(void *sio)
cd748f27 262{
a4b8110e 263 memPoolFree(ufs_state_pool, ((storeIOState *) sio)->fsstate);
cd748f27 264}