]>
Commit | Line | Data |
---|---|---|
b9ae18aa | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
b9ae18aa | 3 | * |
bbc27441 AJ |
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. | |
b9ae18aa | 7 | */ |
63be0a78 | 8 | |
a151895d | 9 | /* DEBUG: section 79 Disk IO Routines */ |
bbc27441 | 10 | |
63be0a78 | 11 | /** |
bbc27441 | 12 | * \par |
b9ae18aa | 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 | * | |
bbc27441 | 17 | * \par |
b9ae18aa | 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 | ||
582c2af2 | 23 | #include "squid.h" |
d9691f09 | 24 | #include "Debug.h" |
d9691f09 AJ |
25 | #include "DiskIO/AIO/AIODiskFile.h" |
26 | #include "DiskIO/AIO/AIODiskIOStrategy.h" | |
b9ae18aa | 27 | #include "DiskIO/IORequestor.h" |
28 | #include "DiskIO/ReadRequest.h" | |
29 | #include "DiskIO/WriteRequest.h" | |
b3f7fd88 | 30 | #include "fs_io.h" |
67679543 | 31 | #include "globals.h" |
b9ae18aa | 32 | |
1a30fdf5 | 33 | #include <cerrno> |
21d845b1 | 34 | |
b9ae18aa | 35 | CBDATA_CLASS_INIT(AIODiskFile); |
b9ae18aa | 36 | |
63be0a78 | 37 | AIODiskFile::AIODiskFile(char const *aPath, AIODiskIOStrategy *aStrategy) : fd(-1), closed(true), error_(false) |
b9ae18aa | 38 | { |
39 | assert (aPath); | |
40 | path = aPath; | |
41 | strategy = aStrategy; | |
bf8fe701 | 42 | debugs(79, 3, "AIODiskFile::AIODiskFile: " << aPath); |
b9ae18aa | 43 | } |
44 | ||
45 | AIODiskFile::~AIODiskFile() | |
46 | {} | |
47 | ||
48 | void | |
49 | AIODiskFile::error(bool const &aBool) | |
50 | { | |
51 | error_ = aBool; | |
52 | } | |
53 | ||
54 | void | |
ced8def3 | 55 | AIODiskFile::open(int flags, mode_t, RefCount<IORequestor> callback) |
b9ae18aa | 56 | { |
57 | /* Simulate async calls */ | |
be266cb2 | 58 | #if _SQUID_WINDOWS_ |
a7a42b14 | 59 | fd = aio_open(path.termedBuf(), flags); |
abb2a3d9 | 60 | #else |
a7a42b14 | 61 | fd = file_open(path.termedBuf() , flags); |
abb2a3d9 | 62 | #endif |
63 | ||
b9ae18aa | 64 | ioRequestor = callback; |
65 | ||
66 | if (fd < 0) { | |
63be0a78 | 67 | debugs(79, 3, HERE << ": got failure (" << errno << ")"); |
b9ae18aa | 68 | error(true); |
69 | } else { | |
70 | closed = false; | |
cb4185f1 | 71 | ++store_open_disk_fd; |
63be0a78 | 72 | debugs(79, 3, HERE << ": opened FD " << fd); |
b9ae18aa | 73 | } |
74 | ||
75 | callback->ioCompletedNotification(); | |
76 | } | |
77 | ||
78 | void | |
63be0a78 | 79 | AIODiskFile::create(int flags, mode_t mode, RefCount<IORequestor> callback) |
b9ae18aa | 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"); | |
e0236918 | 99 | debugs(79, DBG_IMPORTANT, "WARNING: out of aiocb slots!"); |
b9ae18aa | 100 | /* fall back to blocking method */ |
bb790702 | 101 | // file_read(fd, request->buf, request->len, request->offset, callback, data); |
b9ae18aa | 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 */ | |
cb4185f1 | 131 | ++ strategy->aq.aq_numpending; |
b9ae18aa | 132 | |
133 | /* Initiate aio */ | |
134 | if (aio_read(&qe->aq_e_aiocb) < 0) { | |
b69e9ffa AJ |
135 | int xerrno = errno; |
136 | fatalf("Aiee! aio_read() returned error (%d) FIXME and wrap file_read !\n", xerrno); | |
137 | debugs(79, DBG_IMPORTANT, "WARNING: aio_read() returned error: " << xstrerr(xerrno)); | |
b9ae18aa | 138 | /* fall back to blocking method */ |
bb790702 | 139 | // file_read(fd, request->buf, request->len, request->offset, callback, data); |
b9ae18aa | 140 | } |
141 | ||
142 | } | |
143 | ||
144 | void | |
145 | AIODiskFile::write(WriteRequest *request) | |
146 | { | |
147 | int slot; | |
148 | async_queue_entry_t *qe; | |
149 | ||
150 | assert(strategy->aq.aq_state == AQ_STATE_SETUP); | |
151 | ||
152 | /* Find a free slot */ | |
153 | slot = strategy->findSlot(); | |
154 | ||
155 | if (slot < 0) { | |
156 | /* No free slot? Callback error, and return */ | |
157 | fatal("Aiee! out of aiocb slots FIXME and wrap file_write !\n"); | |
e0236918 | 158 | debugs(79, DBG_IMPORTANT, "WARNING: out of aiocb slots!"); |
b9ae18aa | 159 | /* fall back to blocking method */ |
bb790702 | 160 | // file_write(fd, offset, buf, len, callback, data, freefunc); |
b9ae18aa | 161 | return; |
162 | } | |
163 | ||
164 | /* Mark slot as ours */ | |
165 | qe = &strategy->aq.aq_queue[slot]; | |
166 | ||
167 | qe->aq_e_state = AQ_ENTRY_USED; | |
168 | ||
169 | qe->aq_e_callback_data = cbdataReference(request); | |
170 | ||
171 | qe->theFile = cbdataReference(this); | |
172 | ||
173 | qe->aq_e_type = AQ_ENTRY_WRITE; | |
174 | ||
175 | qe->aq_e_free = request->free_func; | |
176 | ||
177 | qe->aq_e_buf = (void *)request->buf; | |
178 | ||
179 | qe->aq_e_fd = fd; | |
180 | ||
181 | qe->aq_e_aiocb.aio_fildes = fd; | |
182 | ||
183 | qe->aq_e_aiocb.aio_nbytes = request->len; | |
184 | ||
185 | qe->aq_e_aiocb.aio_offset = request->offset; | |
186 | ||
187 | qe->aq_e_aiocb.aio_buf = (void *)request->buf; | |
188 | ||
189 | /* Account */ | |
190 | ++strategy->aq.aq_numpending; | |
191 | ||
192 | /* Initiate aio */ | |
193 | if (aio_write(&qe->aq_e_aiocb) < 0) { | |
b69e9ffa AJ |
194 | int xerrno = errno; |
195 | fatalf("Aiee! aio_write() returned error (%d) FIXME and wrap file_write !\n", xerrno); | |
196 | debugs(79, DBG_IMPORTANT, "WARNING: aio_write() returned error: " << xstrerr(xerrno)); | |
b9ae18aa | 197 | /* fall back to blocking method */ |
bb790702 | 198 | // file_write(fd, offset, buf, len, callback, data, freefunc); |
b9ae18aa | 199 | } |
200 | } | |
201 | ||
202 | void | |
203 | AIODiskFile::close () | |
204 | { | |
205 | assert (!closed); | |
be266cb2 | 206 | #if _SQUID_WINDOWS_ |
abb2a3d9 | 207 | aio_close(fd); |
208 | #else | |
b9ae18aa | 209 | file_close(fd); |
abb2a3d9 | 210 | #endif |
211 | ||
b9ae18aa | 212 | fd = -1; |
213 | closed = true; | |
c6062184 | 214 | assert (ioRequestor != NULL); |
b9ae18aa | 215 | ioRequestor->closeCompleted(); |
216 | } | |
217 | ||
218 | bool | |
219 | AIODiskFile::canRead() const | |
220 | { | |
221 | return true; | |
222 | } | |
223 | ||
224 | bool | |
225 | AIODiskFile::canWrite() const | |
226 | { | |
227 | return true; | |
228 | } | |
229 | ||
230 | int | |
231 | AIODiskFile::getFD() const | |
232 | { | |
233 | return fd; | |
234 | } | |
235 | ||
236 | bool | |
237 | AIODiskFile::error() const | |
238 | { | |
239 | return error_; | |
240 | } | |
241 | ||
242 | bool | |
243 | AIODiskFile::ioInProgress() const | |
244 | { | |
245 | return false; | |
246 | } | |
f53969cc | 247 |