]> git.ipfire.org Git - thirdparty/squid.git/blame - src/DiskIO/AIO/AIODiskFile.cc
Store API and layout polishing. No functionality changes intended.
[thirdparty/squid.git] / src / DiskIO / AIO / AIODiskFile.cc
CommitLineData
b9ae18aa 1/*
bde978a6 2 * Copyright (C) 1996-2015 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"
2745fea5 25#include "fs_io.h"
d9691f09
AJ
26#include "DiskIO/AIO/AIODiskFile.h"
27#include "DiskIO/AIO/AIODiskIOStrategy.h"
b9ae18aa 28#include "DiskIO/IORequestor.h"
29#include "DiskIO/ReadRequest.h"
30#include "DiskIO/WriteRequest.h"
67679543 31#include "globals.h"
b9ae18aa 32
1a30fdf5 33#include <cerrno>
21d845b1 34
b9ae18aa 35CBDATA_CLASS_INIT(AIODiskFile);
b9ae18aa 36
63be0a78 37AIODiskFile::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
45AIODiskFile::~AIODiskFile()
46{}
47
48void
49AIODiskFile::error(bool const &aBool)
50{
51 error_ = aBool;
52}
53
54void
ced8def3 55AIODiskFile::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
78void
63be0a78 79AIODiskFile::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
85void
86AIODiskFile::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) {
135 fatalf("Aiee! aio_read() returned error (%d) FIXME and wrap file_read !\n", errno);
e0236918 136 debugs(79, DBG_IMPORTANT, "WARNING: aio_read() returned error: " << xstrerror());
b9ae18aa 137 /* fall back to blocking method */
bb790702 138 // file_read(fd, request->buf, request->len, request->offset, callback, data);
b9ae18aa 139 }
140
141}
142
143void
144AIODiskFile::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");
e0236918 157 debugs(79, DBG_IMPORTANT, "WARNING: out of aiocb slots!");
b9ae18aa 158 /* fall back to blocking method */
bb790702 159 // file_write(fd, offset, buf, len, callback, data, freefunc);
b9ae18aa 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) {
abb2a3d9 193 fatalf("Aiee! aio_write() returned error (%d) FIXME and wrap file_write !\n", errno);
e0236918 194 debugs(79, DBG_IMPORTANT, "WARNING: aio_write() returned error: " << xstrerror());
b9ae18aa 195 /* fall back to blocking method */
bb790702 196 // file_write(fd, offset, buf, len, callback, data, freefunc);
b9ae18aa 197 }
198}
199
200void
201AIODiskFile::close ()
202{
203 assert (!closed);
be266cb2 204#if _SQUID_WINDOWS_
abb2a3d9 205 aio_close(fd);
206#else
b9ae18aa 207 file_close(fd);
abb2a3d9 208#endif
209
b9ae18aa 210 fd = -1;
211 closed = true;
c6062184 212 assert (ioRequestor != NULL);
b9ae18aa 213 ioRequestor->closeCompleted();
214}
215
216bool
217AIODiskFile::canRead() const
218{
219 return true;
220}
221
222bool
223AIODiskFile::canWrite() const
224{
225 return true;
226}
227
228int
229AIODiskFile::getFD() const
230{
231 return fd;
232}
233
234bool
235AIODiskFile::error() const
236{
237 return error_;
238}
239
240bool
241AIODiskFile::ioInProgress() const
242{
243 return false;
244}
f53969cc 245