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