]>
Commit | Line | Data |
---|---|---|
b9ae18aa | 1 | |
2 | /* | |
abb2a3d9 | 3 | * $Id: AIODiskFile.cc,v 1.3 2006/09/04 20:15:22 serassio Exp $ |
b9ae18aa | 4 | * |
5 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
6 | * ---------------------------------------------------------- | |
7 | * | |
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. | |
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. | |
21 | * | |
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. | |
26 | * | |
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 | |
29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
30 | * | |
31 | * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org> | |
32 | */ | |
33 | /* | |
34 | * Author: Adrian Chadd <adrian@squid-cache.org> | |
35 | * | |
36 | * These routines are simple plugin replacements for the file_* routines | |
37 | * in disk.c . They back-end into the POSIX AIO routines to provide | |
38 | * a nice and simple async IO framework for COSS. | |
39 | * | |
40 | * AIO is suitable for COSS - the only sync operations that the standard | |
41 | * supports are read/write, and since COSS works on a single file | |
42 | * per storedir it should work just fine. | |
43 | */ | |
44 | ||
45 | #include "squid.h" | |
46 | #include "AIODiskFile.h" | |
47 | #include "AIODiskIOStrategy.h" | |
48 | #include "DiskIO/IORequestor.h" | |
49 | #include "DiskIO/ReadRequest.h" | |
50 | #include "DiskIO/WriteRequest.h" | |
51 | ||
52 | CBDATA_CLASS_INIT(AIODiskFile); | |
53 | void * | |
54 | AIODiskFile::operator new (size_t) | |
55 | { | |
56 | CBDATA_INIT_TYPE(AIODiskFile); | |
57 | return cbdataAlloc(AIODiskFile); | |
58 | } | |
59 | ||
60 | void | |
61 | AIODiskFile::operator delete (void *address) | |
62 | { | |
63 | cbdataFree(address); | |
64 | } | |
65 | ||
66 | AIODiskFile::AIODiskFile (char const *aPath, AIODiskIOStrategy *aStrategy) : fd(-1), closed(true), error_(false) | |
67 | { | |
68 | assert (aPath); | |
69 | path = aPath; | |
70 | strategy = aStrategy; | |
71 | debug (79,3)("AIODiskFile::AIODiskFile: %s\n", aPath); | |
72 | } | |
73 | ||
74 | AIODiskFile::~AIODiskFile() | |
75 | {} | |
76 | ||
77 | void | |
78 | AIODiskFile::error(bool const &aBool) | |
79 | { | |
80 | error_ = aBool; | |
81 | } | |
82 | ||
83 | void | |
84 | AIODiskFile::open (int flags, mode_t mode, IORequestor::Pointer callback) | |
85 | { | |
86 | /* Simulate async calls */ | |
abb2a3d9 | 87 | #ifdef _SQUID_WIN32_ |
88 | fd = aio_open(path.buf(), flags); | |
89 | #else | |
90 | ||
b9ae18aa | 91 | fd = file_open(path.buf() , flags); |
abb2a3d9 | 92 | #endif |
93 | ||
b9ae18aa | 94 | ioRequestor = callback; |
95 | ||
96 | if (fd < 0) { | |
97 | debug(79, 3) ("BlockingFile::open: got failure (%d)\n", errno); | |
98 | error(true); | |
99 | } else { | |
100 | closed = false; | |
101 | store_open_disk_fd++; | |
102 | debug(79, 3) ("BlockingFile::open: opened FD %d\n", fd); | |
103 | } | |
104 | ||
105 | callback->ioCompletedNotification(); | |
106 | } | |
107 | ||
108 | void | |
109 | AIODiskFile::create (int flags, mode_t mode, RefCount<IORequestor> callback) | |
110 | { | |
111 | /* We use the same logic path for open */ | |
112 | open(flags, mode, callback); | |
113 | } | |
114 | ||
115 | void | |
116 | AIODiskFile::read(ReadRequest *request) | |
117 | { | |
118 | int slot; | |
119 | async_queue_entry_t *qe; | |
120 | ||
121 | assert(strategy->aq.aq_state == AQ_STATE_SETUP); | |
122 | ||
123 | /* Find a free slot */ | |
124 | slot = strategy->findSlot(); | |
125 | ||
126 | if (slot < 0) { | |
127 | /* No free slot? Callback error, and return */ | |
128 | fatal("Aiee! out of aiocb slots! - FIXME and wrap file_read\n"); | |
129 | debug(79, 1) ("WARNING: out of aiocb slots!\n"); | |
130 | /* fall back to blocking method */ | |
131 | // file_read(fd, request->buf, request->len, request->offset, callback, data); | |
132 | return; | |
133 | } | |
134 | ||
135 | /* Mark slot as ours */ | |
136 | qe = &strategy->aq.aq_queue[slot]; | |
137 | ||
138 | qe->aq_e_state = AQ_ENTRY_USED; | |
139 | ||
140 | qe->aq_e_callback_data = cbdataReference(request); | |
141 | ||
142 | qe->theFile = cbdataReference(this); | |
143 | ||
144 | qe->aq_e_type = AQ_ENTRY_READ; | |
145 | ||
146 | qe->aq_e_free = NULL; | |
147 | ||
148 | qe->aq_e_buf = request->buf; | |
149 | ||
150 | qe->aq_e_fd = getFD(); | |
151 | ||
152 | qe->aq_e_aiocb.aio_fildes = getFD(); | |
153 | ||
154 | qe->aq_e_aiocb.aio_nbytes = request->len; | |
155 | ||
156 | qe->aq_e_aiocb.aio_offset = request->offset; | |
157 | ||
158 | qe->aq_e_aiocb.aio_buf = request->buf; | |
159 | ||
160 | /* Account */ | |
161 | strategy->aq.aq_numpending++; | |
162 | ||
163 | /* Initiate aio */ | |
164 | if (aio_read(&qe->aq_e_aiocb) < 0) { | |
165 | fatalf("Aiee! aio_read() returned error (%d) FIXME and wrap file_read !\n", errno); | |
166 | debug(79, 1) ("WARNING: aio_read() returned error: %s\n", xstrerror()); | |
167 | /* fall back to blocking method */ | |
168 | // file_read(fd, request->buf, request->len, request->offset, callback, data); | |
169 | } | |
170 | ||
171 | } | |
172 | ||
173 | void | |
174 | AIODiskFile::write(WriteRequest *request) | |
175 | { | |
176 | int slot; | |
177 | async_queue_entry_t *qe; | |
178 | ||
179 | assert(strategy->aq.aq_state == AQ_STATE_SETUP); | |
180 | ||
181 | /* Find a free slot */ | |
182 | slot = strategy->findSlot(); | |
183 | ||
184 | if (slot < 0) { | |
185 | /* No free slot? Callback error, and return */ | |
186 | fatal("Aiee! out of aiocb slots FIXME and wrap file_write !\n"); | |
187 | debug(79, 1) ("WARNING: out of aiocb slots!\n"); | |
188 | /* fall back to blocking method */ | |
189 | // file_write(fd, offset, buf, len, callback, data, freefunc); | |
190 | return; | |
191 | } | |
192 | ||
193 | /* Mark slot as ours */ | |
194 | qe = &strategy->aq.aq_queue[slot]; | |
195 | ||
196 | qe->aq_e_state = AQ_ENTRY_USED; | |
197 | ||
198 | qe->aq_e_callback_data = cbdataReference(request); | |
199 | ||
200 | qe->theFile = cbdataReference(this); | |
201 | ||
202 | qe->aq_e_type = AQ_ENTRY_WRITE; | |
203 | ||
204 | qe->aq_e_free = request->free_func; | |
205 | ||
206 | qe->aq_e_buf = (void *)request->buf; | |
207 | ||
208 | qe->aq_e_fd = fd; | |
209 | ||
210 | qe->aq_e_aiocb.aio_fildes = fd; | |
211 | ||
212 | qe->aq_e_aiocb.aio_nbytes = request->len; | |
213 | ||
214 | qe->aq_e_aiocb.aio_offset = request->offset; | |
215 | ||
216 | qe->aq_e_aiocb.aio_buf = (void *)request->buf; | |
217 | ||
218 | /* Account */ | |
219 | ++strategy->aq.aq_numpending; | |
220 | ||
221 | /* Initiate aio */ | |
222 | if (aio_write(&qe->aq_e_aiocb) < 0) { | |
abb2a3d9 | 223 | fatalf("Aiee! aio_write() returned error (%d) FIXME and wrap file_write !\n", errno); |
b9ae18aa | 224 | debug(79, 1) ("WARNING: aio_write() returned error: %s\n", xstrerror()); |
225 | /* fall back to blocking method */ | |
226 | // file_write(fd, offset, buf, len, callback, data, freefunc); | |
227 | } | |
228 | } | |
229 | ||
230 | void | |
231 | AIODiskFile::close () | |
232 | { | |
233 | assert (!closed); | |
abb2a3d9 | 234 | #ifdef _SQUID_WIN32_ |
235 | ||
236 | aio_close(fd); | |
237 | #else | |
238 | ||
b9ae18aa | 239 | file_close(fd); |
abb2a3d9 | 240 | #endif |
241 | ||
b9ae18aa | 242 | fd = -1; |
243 | closed = true; | |
c6062184 | 244 | assert (ioRequestor != NULL); |
b9ae18aa | 245 | ioRequestor->closeCompleted(); |
246 | } | |
247 | ||
248 | bool | |
249 | AIODiskFile::canRead() const | |
250 | { | |
251 | return true; | |
252 | } | |
253 | ||
254 | bool | |
255 | AIODiskFile::canWrite() const | |
256 | { | |
257 | return true; | |
258 | } | |
259 | ||
260 | int | |
261 | AIODiskFile::getFD() const | |
262 | { | |
263 | return fd; | |
264 | } | |
265 | ||
266 | bool | |
267 | AIODiskFile::error() const | |
268 | { | |
269 | return error_; | |
270 | } | |
271 | ||
272 | bool | |
273 | AIODiskFile::ioInProgress() const | |
274 | { | |
275 | return false; | |
276 | } |