]>
Commit | Line | Data |
---|---|---|
b9ae18aa | 1 | |
2 | /* | |
ac332e9e | 3 | * $Id: DiskThreadsDiskFile.cc,v 1.8 2006/06/17 16:36:15 serassio Exp $ |
b9ae18aa | 4 | * |
5 | * DEBUG: section 79 Disk IO Routines | |
6 | * AUTHOR: Robert Collins | |
7 | * | |
8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
9 | * ---------------------------------------------------------- | |
10 | * | |
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. | |
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. | |
24 | * | |
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. | |
29 | * | |
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 | * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org> | |
35 | */ | |
36 | ||
37 | ||
38 | #include "squid.h" | |
39 | #include "DiskThreadsDiskFile.h" | |
40 | #include "Store.h" | |
b9ae18aa | 41 | #include "Generic.h" |
42 | #include "DiskIO/IORequestor.h" | |
43 | #include "DiskIO/ReadRequest.h" | |
44 | #include "DiskIO/WriteRequest.h" | |
45 | ||
46 | /* === PUBLIC =========================================================== */ | |
47 | ||
48 | CBDATA_CLASS_INIT(DiskThreadsDiskFile); | |
49 | void * | |
50 | DiskThreadsDiskFile::operator new (size_t) | |
51 | { | |
52 | CBDATA_INIT_TYPE(DiskThreadsDiskFile); | |
53 | DiskThreadsDiskFile *result = cbdataAlloc(DiskThreadsDiskFile); | |
e65c313f | 54 | /* |
55 | * We used to call squidaio_init() here, but if the first transaction | |
56 | * is to unlink a file (e.g., if Squid starts up over the disk space | |
57 | * limit) then "squidaio" won't be initialized yet. | |
58 | */ | |
d06925a4 | 59 | |
aa625860 | 60 | return result; |
b9ae18aa | 61 | } |
62 | ||
63 | void | |
64 | DiskThreadsDiskFile::operator delete (void *address) | |
65 | { | |
66 | DiskThreadsDiskFile *t = static_cast<DiskThreadsDiskFile *>(address); | |
aa625860 | 67 | cbdataFree(t); |
b9ae18aa | 68 | } |
69 | ||
70 | DiskThreadsDiskFile::DiskThreadsDiskFile (char const *aPath, DiskThreadsIOStrategy *anIO):fd(-1), errorOccured (false), IO(anIO), | |
71 | inProgressIOs (0) | |
72 | { | |
73 | assert (aPath); | |
74 | debug (79,3)("UFSFile::UFSFile: %s\n", aPath); | |
75 | path_ = xstrdup (aPath); | |
76 | } | |
77 | ||
78 | DiskThreadsDiskFile::~DiskThreadsDiskFile() | |
79 | { | |
80 | safe_free (path_); | |
81 | doClose(); | |
82 | } | |
83 | ||
84 | void | |
85 | DiskThreadsDiskFile::open (int flags, mode_t mode, IORequestor::Pointer callback) | |
86 | { | |
87 | statCounter.syscalls.disk.opens++; | |
88 | #if !ASYNC_OPEN | |
89 | ||
90 | fd = file_open(path_, flags); | |
91 | ||
92 | if (fd < 0) { | |
93 | debug(79, 3) ("DiskThreadsDiskFile::open: got failure (%d)\n", errno); | |
94 | errorOccured = true; | |
95 | return; | |
96 | } | |
97 | ||
98 | #endif | |
99 | Opening_FD++; | |
100 | ||
101 | ioRequestor = callback; | |
102 | ||
103 | ++inProgressIOs; | |
104 | ||
105 | #if ASYNC_OPEN | |
106 | ||
107 | aioOpen(path_, flags, mode, DiskThreadsDiskFile::OpenDone, this); | |
108 | ||
109 | #else | |
110 | ||
111 | openDone(fd, NULL, fd, 0); | |
112 | ||
113 | #endif | |
114 | } | |
115 | ||
116 | void | |
117 | DiskThreadsDiskFile::read(ReadRequest * request) | |
118 | { | |
119 | debugs(79, 3, "DiskThreadsDiskFile::read: " << this << ", size " << request->len); | |
120 | assert (fd > -1); | |
121 | assert (ioRequestor.getRaw()); | |
122 | statCounter.syscalls.disk.reads++; | |
123 | ++inProgressIOs; | |
124 | #if ASYNC_READ | |
125 | ||
126 | aioRead(fd, request->offset, request->len, ReadDone, new IoResult<ReadRequest>(this, request)); | |
127 | #else | |
128 | ||
129 | file_read(fd, request->buf, request->len, request->offset, ReadDone, new IoResult<ReadRequest>(this, request)); | |
130 | #endif | |
131 | } | |
132 | ||
133 | void | |
134 | DiskThreadsDiskFile::create (int flags, mode_t mode, IORequestor::Pointer callback) | |
135 | { | |
136 | statCounter.syscalls.disk.opens++; | |
137 | #if !ASYNC_CREATE | |
138 | ||
139 | int fd = file_open(path_, flags); | |
140 | ||
141 | if (fd < 0) { | |
ac97716e | 142 | debug(79, 3) ("DiskThreadsDiskFile::create: got failure (%d)\n", errno); |
b9ae18aa | 143 | errorOccured = true; |
144 | return; | |
145 | } | |
146 | ||
147 | #endif | |
148 | Opening_FD++; | |
149 | ||
150 | ioRequestor = callback; | |
151 | ||
152 | ++inProgressIOs; | |
153 | ||
154 | #if ASYNC_CREATE | |
155 | ||
156 | aioOpen(path_, flags, mode, DiskThreadsDiskFile::OpenDone, this); | |
157 | ||
158 | #else | |
159 | ||
160 | openDone (fd, NULL, fd, 0); | |
161 | ||
162 | #endif | |
163 | } | |
164 | ||
165 | bool | |
166 | DiskThreadsDiskFile::error() const | |
167 | { | |
168 | return errorOccured; | |
169 | } | |
170 | ||
171 | void | |
172 | DiskThreadsDiskFile::OpenDone(int fd, void *cbdata, const char *buf, int aio_return, int aio_errno) | |
173 | { | |
174 | DiskThreadsDiskFile *myFile = static_cast<DiskThreadsDiskFile *>(cbdata); | |
175 | myFile->openDone (fd, buf, aio_return, aio_errno); | |
176 | } | |
177 | ||
178 | void | |
179 | DiskThreadsDiskFile::openDone(int unused, const char *unused2, int anFD, int errflag) | |
180 | { | |
181 | debug(79, 3) ("DiskThreadsDiskFile::openDone: FD %d, errflag %d\n", anFD, errflag); | |
182 | Opening_FD--; | |
183 | ||
184 | fd = anFD; | |
185 | ||
186 | if (errflag || fd < 0) { | |
187 | errno = errflag; | |
188 | debug(79, 0) ("DiskThreadsDiskFile::openDone: %s\n", xstrerror()); | |
189 | debug(79, 1) ("\t%s\n", path_); | |
190 | errorOccured = true; | |
191 | } else { | |
192 | store_open_disk_fd++; | |
193 | commSetCloseOnExec(fd); | |
194 | fd_open(fd, FD_FILE, path_); | |
195 | } | |
196 | ||
b9ae18aa | 197 | IORequestor::Pointer t = ioRequestor; |
198 | --inProgressIOs; | |
199 | t->ioCompletedNotification(); | |
ac97716e | 200 | |
201 | debug(79, 3) ("DiskThreadsDiskFile::openDone: exiting\n"); | |
b9ae18aa | 202 | } |
203 | ||
204 | void DiskThreadsDiskFile::doClose() | |
205 | { | |
206 | if (fd > -1) { | |
207 | statCounter.syscalls.disk.closes++; | |
d06925a4 | 208 | #if ASYNC_CLOSE |
209 | ||
b9ae18aa | 210 | aioClose(fd); |
211 | fd_close(fd); | |
d06925a4 | 212 | #else |
213 | ||
214 | aioCancel(fd); | |
215 | file_close(fd); | |
216 | #endif | |
217 | ||
b9ae18aa | 218 | store_open_disk_fd--; |
219 | fd = -1; | |
220 | } | |
221 | } | |
222 | ||
223 | void | |
224 | DiskThreadsDiskFile::close () | |
225 | { | |
226 | debug (79,3)("DiskThreadsDiskFile::close: %p closing for %p\n", this, ioRequestor.getRaw()); | |
227 | ||
228 | if (!ioInProgress()) { | |
229 | doClose(); | |
ac97716e | 230 | assert (ioRequestor != NULL); |
b9ae18aa | 231 | ioRequestor->closeCompleted(); |
ac97716e | 232 | return; |
233 | } else { | |
234 | debugs(79,0,HERE << "DiskThreadsDiskFile::close: " << | |
235 | "did NOT close because ioInProgress() is true. now what?"); | |
b9ae18aa | 236 | } |
237 | } | |
238 | ||
239 | bool | |
240 | DiskThreadsDiskFile::canRead() const | |
241 | { | |
242 | debug (79,3)("DiskThreadsDiskFile::canRead: fd is %d\n",fd); | |
243 | return fd > -1; | |
244 | } | |
245 | ||
246 | void | |
247 | DiskThreadsDiskFile::write(WriteRequest * writeRequest) | |
248 | { | |
ac332e9e | 249 | debug(79, 3) ("DiskThreadsDiskFile::write: FD %d\n", fd); |
b9ae18aa | 250 | statCounter.syscalls.disk.writes++; |
251 | ++inProgressIOs; | |
252 | #if ASYNC_WRITE | |
253 | ||
254 | aioWrite(fd, writeRequest->offset, (char *)writeRequest->buf, writeRequest->len, WriteDone, new IoResult<WriteRequest>(this, writeRequest), | |
255 | writeRequest->free_func); | |
256 | #else | |
257 | ||
258 | file_write(fd, writeRequest->offset, (char *)writeRequest->buf, writeRequest->len, WriteDone, new IoResult<WriteRequest>(this, writeRequest), | |
259 | writeRequest->free_func); | |
260 | #endif | |
261 | } | |
262 | ||
263 | bool | |
264 | DiskThreadsDiskFile::canWrite() const | |
265 | { | |
266 | return fd > -1; | |
267 | } | |
268 | ||
269 | bool | |
270 | DiskThreadsDiskFile::ioInProgress()const | |
271 | { | |
272 | return inProgressIOs > 0; | |
273 | } | |
274 | ||
275 | /* === STATIC =========================================================== */ | |
276 | ||
277 | #if ASYNC_READ | |
278 | void | |
279 | DiskThreadsDiskFile::ReadDone(int fd, void *my_data, const char *buf, int len, int errflag) | |
280 | #else | |
281 | void | |
282 | DiskThreadsDiskFile::ReadDone(int fd, const char *buf, int len, int errflag, void *my_data) | |
283 | #endif | |
284 | { | |
285 | IoResult<ReadRequest> * result = static_cast<IoResult<ReadRequest> *>(my_data); | |
286 | assert (result); | |
287 | result->file->readDone(fd, buf, len, errflag, result->request); | |
288 | delete result; | |
289 | } | |
290 | ||
291 | void | |
292 | DiskThreadsDiskFile::readDone(int rvfd, const char *buf, int len, int errflag, ReadRequest::Pointer request) | |
293 | { | |
294 | debug (79,3)("DiskThreadsDiskFile::readDone: FD %d\n",rvfd); | |
295 | assert (fd == rvfd); | |
296 | ||
297 | ssize_t rlen; | |
298 | ||
299 | if (errflag) { | |
300 | debug(79, 3) ("DiskThreadsDiskFile::readDone: got failure (%d)\n", errflag); | |
301 | rlen = -1; | |
302 | } else { | |
303 | rlen = (ssize_t) len; | |
304 | } | |
305 | ||
306 | #if ASYNC_READ | |
307 | /* translate errflag from errno to Squid disk error */ | |
308 | errno = errflag; | |
309 | ||
310 | if (errflag) | |
311 | errflag = DISK_ERROR; | |
312 | else | |
313 | errflag = DISK_OK; | |
314 | ||
315 | #else | |
316 | ||
317 | if (errflag == DISK_EOF) | |
318 | errflag = DISK_OK; /* EOF is signalled by len == 0, not errors... */ | |
319 | ||
320 | #endif | |
321 | ||
322 | --inProgressIOs; | |
323 | ||
324 | ioRequestor->readCompleted(buf, rlen, errflag, request); | |
325 | } | |
326 | ||
327 | void | |
328 | DiskThreadsDiskFile:: | |
329 | #if ASYNC_WRITE | |
a37ea9a1 | 330 | WriteDone(int fd, void *my_data, const char *buf, int len, int errflag) |
b9ae18aa | 331 | #else |
332 | WriteDone(int fd, int errflag, size_t len, void *my_data) | |
333 | #endif | |
334 | { | |
335 | IoResult<WriteRequest> * result = static_cast<IoResult<WriteRequest> *>(my_data); | |
336 | assert (result); | |
337 | result->file->writeDone(fd, errflag, len, result->request); | |
338 | delete result; | |
339 | } | |
340 | ||
341 | void | |
342 | DiskThreadsDiskFile::writeDone (int rvfd, int errflag, size_t len, WriteRequest::Pointer request) | |
343 | { | |
344 | assert (rvfd == fd); | |
345 | static int loop_detect = 0; | |
b9ae18aa | 346 | |
347 | #if ASYNC_WRITE | |
348 | /* Translate from errno to Squid disk error */ | |
b9ae18aa | 349 | |
350 | if (errflag) | |
a37ea9a1 | 351 | errflag = errflag == ENOSPC ? DISK_NO_SPACE_LEFT : DISK_ERROR; |
b9ae18aa | 352 | else |
353 | errflag = DISK_OK; | |
354 | ||
355 | #endif | |
356 | ||
ac332e9e | 357 | debug(79, 3) ("DiskThreadsDiskFile::writeDone: FD %d, len %ld, err=%d\n", |
a37ea9a1 | 358 | fd, (long int) len, errflag); |
359 | ||
b9ae18aa | 360 | assert(++loop_detect < 10); |
361 | ||
362 | --inProgressIOs; | |
363 | ||
364 | ioRequestor->writeCompleted(errflag, len, request); | |
365 | ||
366 | --loop_detect; | |
367 | } | |
368 | ||
369 | template <class RT> | |
370 | cbdata_type IoResult<RT>::CBDATA_IoResult = CBDATA_UNKNOWN; | |
371 | ||
372 | template<class RT> | |
373 | void * | |
374 | IoResult<RT>::operator new (size_t) | |
375 | { | |
376 | CBDATA_INIT_TYPE(IoResult); | |
377 | IoResult<RT> *result = cbdataAlloc(IoResult); | |
378 | return result; | |
379 | } | |
380 | ||
381 | template <class RT> | |
382 | void | |
383 | IoResult<RT>::operator delete (void *address) | |
384 | { | |
385 | cbdataFree(address); | |
386 | } | |
387 |