]> git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/Mmapped/MmappedFile.cc
Merged from trunk
[thirdparty/squid.git] / src / DiskIO / Mmapped / MmappedFile.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 47 Store Directory Routines
5 */
6
7 #include "config.h"
8 #include "DiskIO/IORequestor.h"
9 #include "DiskIO/Mmapped/MmappedFile.h"
10 #include "DiskIO/ReadRequest.h"
11 #include "DiskIO/WriteRequest.h"
12 #include <sys/mman.h>
13
14 CBDATA_CLASS_INIT(MmappedFile);
15
16 // helper class to deal with mmap(2) offset alignment and other low-level specs
17 class Mmapping
18 {
19 public:
20 Mmapping(int fd, size_t length, int prot, int flags, off_t offset);
21 ~Mmapping();
22
23 void *map(); ///< calls mmap(2); returns usable buffer or nil on failure
24 bool unmap(); ///< unmaps previously mapped buffer, if any
25
26 private:
27 const int fd; ///< descriptor of the mmapped file
28 const size_t length; ///< user-requested data length, needed for munmap
29 const int prot; ///< mmap(2) "protection" flags
30 const int flags; ///< other mmap(2) flags
31 const off_t offset; ///< user-requested data offset
32
33 off_t delta; ///< mapped buffer increment to hit user offset
34 void *buf; ///< buffer returned by mmap, needed for munmap
35 };
36
37
38 void *
39 MmappedFile::operator new(size_t sz)
40 {
41 CBDATA_INIT_TYPE(MmappedFile);
42 MmappedFile *result = cbdataAlloc(MmappedFile);
43 /* Mark result as being owned - we want the refcounter to do the delete
44 * call */
45 return result;
46 }
47
48 void
49 MmappedFile::operator delete(void *address)
50 {
51 MmappedFile *t = static_cast<MmappedFile *>(address);
52 cbdataFree(t);
53 }
54
55 MmappedFile::MmappedFile(char const *aPath): fd(-1),
56 minOffset(0), maxOffset(-1), error_(false)
57 {
58 assert(aPath);
59 path_ = xstrdup(aPath);
60 debugs(79,5, HERE << this << ' ' << path_);
61 }
62
63 MmappedFile::~MmappedFile()
64 {
65 safe_free(path_);
66 doClose();
67 }
68
69 // XXX: almost a copy of BlockingFile::open
70 void
71 MmappedFile::open(int flags, mode_t mode, RefCount<IORequestor> callback)
72 {
73 assert(fd < 0);
74
75 /* Simulate async calls */
76 fd = file_open(path_ , flags);
77 ioRequestor = callback;
78
79 if (fd < 0) {
80 debugs(79,3, HERE << "open error: " << xstrerror());
81 error_ = true;
82 } else {
83 store_open_disk_fd++;
84 debugs(79,3, HERE << "FD " << fd);
85
86 // setup mapping boundaries
87 struct stat sb;
88 if (fstat(fd, &sb) == 0)
89 maxOffset = sb.st_size; // we do not expect it to change
90 }
91
92 callback->ioCompletedNotification();
93 }
94
95 /**
96 * Alias for MmappedFile::open(...)
97 \copydoc MmappedFile::open(int flags, mode_t mode, RefCount<IORequestor> callback)
98 */
99 void
100 MmappedFile::create(int flags, mode_t mode, RefCount<IORequestor> callback)
101 {
102 /* We use the same logic path for open */
103 open(flags, mode, callback);
104 }
105
106 void MmappedFile::doClose()
107 {
108 if (fd >= 0) {
109 file_close(fd);
110 fd = -1;
111 store_open_disk_fd--;
112 }
113 }
114
115 void
116 MmappedFile::close()
117 {
118 debugs(79, 3, HERE << this << " closing for " << ioRequestor);
119 doClose();
120 assert(ioRequestor != NULL);
121 ioRequestor->closeCompleted();
122 }
123
124 bool
125 MmappedFile::canRead() const
126 {
127 return fd >= 0;
128 }
129
130 bool
131 MmappedFile::canWrite() const
132 {
133 return fd >= 0;
134 }
135
136 bool
137 MmappedFile::error() const
138 {
139 return error_;
140 }
141
142 void
143 MmappedFile::read(ReadRequest *aRequest)
144 {
145 debugs(79,3, HERE << "(FD " << fd << ", " << aRequest->len << ", " <<
146 aRequest->offset << ")");
147
148 assert(fd >= 0);
149 assert(ioRequestor != NULL);
150 assert(aRequest->len >= 0);
151 assert(aRequest->len > 0); // TODO: work around mmap failures on zero-len?
152 assert(aRequest->offset >= 0);
153 assert(!error_); // TODO: propagate instead?
154
155 assert(minOffset < 0 || minOffset <= aRequest->offset);
156 assert(maxOffset < 0 || static_cast<uint64_t>(aRequest->offset + aRequest->len) <= static_cast<uint64_t>(maxOffset));
157
158 Mmapping mapping(fd, aRequest->len, PROT_READ, MAP_PRIVATE | MAP_NORESERVE,
159 aRequest->offset);
160
161 bool done = false;
162 if (void *buf = mapping.map()) {
163 memcpy(aRequest->buf, buf, aRequest->len);
164 done = mapping.unmap();
165 }
166 error_ = !done;
167
168 const ssize_t rlen = error_ ? -1 : (ssize_t)aRequest->len;
169 const int errflag = error_ ? DISK_ERROR :DISK_OK;
170 ioRequestor->readCompleted(aRequest->buf, rlen, errflag, aRequest);
171 }
172
173 void
174 MmappedFile::write(WriteRequest *aRequest)
175 {
176 debugs(79,3, HERE << "(FD " << fd << ", " << aRequest->len << ", " <<
177 aRequest->offset << ")");
178
179 assert(fd >= 0);
180 assert(ioRequestor != NULL);
181 assert(aRequest->len >= 0);
182 assert(aRequest->len > 0); // TODO: work around mmap failures on zero-len?
183 assert(aRequest->offset >= 0);
184 assert(!error_); // TODO: propagate instead?
185
186 assert(minOffset < 0 || minOffset <= aRequest->offset);
187 assert(maxOffset < 0 || static_cast<uint64_t>(aRequest->offset + aRequest->len) <= static_cast<uint64_t>(maxOffset));
188
189 const ssize_t written =
190 pwrite(fd, aRequest->buf, aRequest->len, aRequest->offset);
191 if (written < 0) {
192 debugs(79,1, HERE << "error: " << xstrerr(errno));
193 error_ = true;
194 } else if (static_cast<size_t>(written) != aRequest->len) {
195 debugs(79,1, HERE << "problem: " << written << " < " << aRequest->len);
196 error_ = true;
197 }
198
199 if (aRequest->free_func)
200 (aRequest->free_func)(const_cast<char*>(aRequest->buf)); // broken API?
201
202 if (!error_) {
203 debugs(79,5, HERE << "wrote " << aRequest->len << " to FD " << fd << " at " << aRequest->offset);
204 } else {
205 doClose();
206 }
207
208 const ssize_t rlen = error_ ? 0 : (ssize_t)aRequest->len;
209 const int errflag = error_ ? DISK_ERROR :DISK_OK;
210 ioRequestor->writeCompleted(errflag, rlen, aRequest);
211 }
212
213 /// we only support blocking I/O
214 bool
215 MmappedFile::ioInProgress() const
216 {
217 return false;
218 }
219
220 Mmapping::Mmapping(int aFd, size_t aLength, int aProt, int aFlags, off_t anOffset):
221 fd(aFd), length(aLength), prot(aProt), flags(aFlags), offset(anOffset),
222 delta(-1), buf(NULL)
223 {
224 }
225
226 Mmapping::~Mmapping()
227 {
228 if (buf)
229 unmap();
230 }
231
232 void *
233 Mmapping::map()
234 {
235 // mmap(2) requires that offset is a multiple of the page size
236 static const int pageSize = getpagesize();
237 delta = offset % pageSize;
238
239 buf = mmap(NULL, length + delta, prot, flags, fd, offset - delta);
240
241 if (buf == MAP_FAILED) {
242 const int errNo = errno;
243 debugs(79,3, HERE << "error FD " << fd << "mmap(" << length << '+' <<
244 delta << ", " << offset << '-' << delta << "): " << xstrerr(errNo));
245 buf = NULL;
246 return NULL;
247 }
248
249 return static_cast<char*>(buf) + delta;
250 }
251
252 bool
253 Mmapping::unmap()
254 {
255 debugs(79,9, HERE << "FD " << fd <<
256 " munmap(" << buf << ", " << length << '+' << delta << ')');
257
258 if (!buf) // forgot or failed to map
259 return false;
260
261 const bool error = munmap(buf, length + delta) != 0;
262 if (error) {
263 const int errNo = errno;
264 debugs(79,3, HERE << "error FD " << fd <<
265 " munmap(" << buf << ", " << length << '+' << delta << "): " <<
266 "): " << xstrerr(errNo));
267 }
268 buf = NULL;
269 return !error;
270 }
271
272 // TODO: check MAP_NORESERVE, consider MAP_POPULATE and MAP_FIXED