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