]> git.ipfire.org Git - thirdparty/squid.git/blame - src/DiskIO/AIO/AIODiskFile.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / DiskIO / AIO / AIODiskFile.cc
CommitLineData
b9ae18aa 1/*
262a0e14 2 * $Id$
b9ae18aa 3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
26ac0430 20 *
b9ae18aa 21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26ac0430 25 *
b9ae18aa 26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
29 *
30 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
31 */
63be0a78 32
33/**
b9ae18aa 34 * Author: Adrian Chadd <adrian@squid-cache.org>
35 *
63be0a78 36 \par
b9ae18aa 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 *
63be0a78 41 \par
b9ae18aa 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
f7f3304a 47#include "squid-old.h"
b9ae18aa 48#include "AIODiskFile.h"
49#include "AIODiskIOStrategy.h"
50#include "DiskIO/IORequestor.h"
51#include "DiskIO/ReadRequest.h"
52#include "DiskIO/WriteRequest.h"
53
54CBDATA_CLASS_INIT(AIODiskFile);
55void *
63be0a78 56AIODiskFile::operator new(size_t unused)
b9ae18aa 57{
58 CBDATA_INIT_TYPE(AIODiskFile);
59 return cbdataAlloc(AIODiskFile);
60}
61
62void
63be0a78 63AIODiskFile::operator delete(void *address)
b9ae18aa 64{
65 cbdataFree(address);
66}
67
63be0a78 68AIODiskFile::AIODiskFile(char const *aPath, AIODiskIOStrategy *aStrategy) : fd(-1), closed(true), error_(false)
b9ae18aa 69{
70 assert (aPath);
71 path = aPath;
72 strategy = aStrategy;
bf8fe701 73 debugs(79, 3, "AIODiskFile::AIODiskFile: " << aPath);
b9ae18aa 74}
75
76AIODiskFile::~AIODiskFile()
77{}
78
79void
80AIODiskFile::error(bool const &aBool)
81{
82 error_ = aBool;
83}
84
85void
63be0a78 86AIODiskFile::open(int flags, mode_t mode, RefCount<IORequestor> callback)
b9ae18aa 87{
88 /* Simulate async calls */
be266cb2 89#if _SQUID_WINDOWS_
a7a42b14 90 fd = aio_open(path.termedBuf(), flags);
abb2a3d9 91#else
a7a42b14 92 fd = file_open(path.termedBuf() , flags);
abb2a3d9 93#endif
94
b9ae18aa 95 ioRequestor = callback;
96
97 if (fd < 0) {
63be0a78 98 debugs(79, 3, HERE << ": got failure (" << errno << ")");
b9ae18aa 99 error(true);
100 } else {
101 closed = false;
102 store_open_disk_fd++;
63be0a78 103 debugs(79, 3, HERE << ": opened FD " << fd);
b9ae18aa 104 }
105
106 callback->ioCompletedNotification();
107}
108
109void
63be0a78 110AIODiskFile::create(int flags, mode_t mode, RefCount<IORequestor> callback)
b9ae18aa 111{
112 /* We use the same logic path for open */
113 open(flags, mode, callback);
114}
115
116void
117AIODiskFile::read(ReadRequest *request)
118{
119 int slot;
120 async_queue_entry_t *qe;
121
122 assert(strategy->aq.aq_state == AQ_STATE_SETUP);
123
124 /* Find a free slot */
125 slot = strategy->findSlot();
126
127 if (slot < 0) {
128 /* No free slot? Callback error, and return */
129 fatal("Aiee! out of aiocb slots! - FIXME and wrap file_read\n");
bf8fe701 130 debugs(79, 1, "WARNING: out of aiocb slots!");
b9ae18aa 131 /* fall back to blocking method */
bb790702 132 // file_read(fd, request->buf, request->len, request->offset, callback, data);
b9ae18aa 133 return;
134 }
135
136 /* Mark slot as ours */
137 qe = &strategy->aq.aq_queue[slot];
138
139 qe->aq_e_state = AQ_ENTRY_USED;
140
141 qe->aq_e_callback_data = cbdataReference(request);
142
143 qe->theFile = cbdataReference(this);
144
145 qe->aq_e_type = AQ_ENTRY_READ;
146
147 qe->aq_e_free = NULL;
148
149 qe->aq_e_buf = request->buf;
150
151 qe->aq_e_fd = getFD();
152
153 qe->aq_e_aiocb.aio_fildes = getFD();
154
155 qe->aq_e_aiocb.aio_nbytes = request->len;
156
157 qe->aq_e_aiocb.aio_offset = request->offset;
158
159 qe->aq_e_aiocb.aio_buf = request->buf;
160
161 /* Account */
162 strategy->aq.aq_numpending++;
163
164 /* Initiate aio */
165 if (aio_read(&qe->aq_e_aiocb) < 0) {
166 fatalf("Aiee! aio_read() returned error (%d) FIXME and wrap file_read !\n", errno);
bf8fe701 167 debugs(79, 1, "WARNING: aio_read() returned error: " << xstrerror());
b9ae18aa 168 /* fall back to blocking method */
bb790702 169 // file_read(fd, request->buf, request->len, request->offset, callback, data);
b9ae18aa 170 }
171
172}
173
174void
175AIODiskFile::write(WriteRequest *request)
176{
177 int slot;
178 async_queue_entry_t *qe;
179
180 assert(strategy->aq.aq_state == AQ_STATE_SETUP);
181
182 /* Find a free slot */
183 slot = strategy->findSlot();
184
185 if (slot < 0) {
186 /* No free slot? Callback error, and return */
187 fatal("Aiee! out of aiocb slots FIXME and wrap file_write !\n");
bf8fe701 188 debugs(79, 1, "WARNING: out of aiocb slots!");
b9ae18aa 189 /* fall back to blocking method */
bb790702 190 // file_write(fd, offset, buf, len, callback, data, freefunc);
b9ae18aa 191 return;
192 }
193
194 /* Mark slot as ours */
195 qe = &strategy->aq.aq_queue[slot];
196
197 qe->aq_e_state = AQ_ENTRY_USED;
198
199 qe->aq_e_callback_data = cbdataReference(request);
200
201 qe->theFile = cbdataReference(this);
202
203 qe->aq_e_type = AQ_ENTRY_WRITE;
204
205 qe->aq_e_free = request->free_func;
206
207 qe->aq_e_buf = (void *)request->buf;
208
209 qe->aq_e_fd = fd;
210
211 qe->aq_e_aiocb.aio_fildes = fd;
212
213 qe->aq_e_aiocb.aio_nbytes = request->len;
214
215 qe->aq_e_aiocb.aio_offset = request->offset;
216
217 qe->aq_e_aiocb.aio_buf = (void *)request->buf;
218
219 /* Account */
220 ++strategy->aq.aq_numpending;
221
222 /* Initiate aio */
223 if (aio_write(&qe->aq_e_aiocb) < 0) {
abb2a3d9 224 fatalf("Aiee! aio_write() returned error (%d) FIXME and wrap file_write !\n", errno);
bf8fe701 225 debugs(79, 1, "WARNING: aio_write() returned error: " << xstrerror());
b9ae18aa 226 /* fall back to blocking method */
bb790702 227 // file_write(fd, offset, buf, len, callback, data, freefunc);
b9ae18aa 228 }
229}
230
231void
232AIODiskFile::close ()
233{
234 assert (!closed);
be266cb2 235#if _SQUID_WINDOWS_
abb2a3d9 236 aio_close(fd);
237#else
b9ae18aa 238 file_close(fd);
abb2a3d9 239#endif
240
b9ae18aa 241 fd = -1;
242 closed = true;
c6062184 243 assert (ioRequestor != NULL);
b9ae18aa 244 ioRequestor->closeCompleted();
245}
246
247bool
248AIODiskFile::canRead() const
249{
250 return true;
251}
252
253bool
254AIODiskFile::canWrite() const
255{
256 return true;
257}
258
259int
260AIODiskFile::getFD() const
261{
262 return fd;
263}
264
265bool
266AIODiskFile::error() const
267{
268 return error_;
269}
270
271bool
272AIODiskFile::ioInProgress() const
273{
274 return false;
275}