]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/Mmapped/MmappedFile.cc
4 * DEBUG: section 47 Store Directory Routines
9 #include "DiskIO/IORequestor.h"
10 #include "DiskIO/Mmapped/MmappedFile.h"
11 #include "DiskIO/ReadRequest.h"
12 #include "DiskIO/WriteRequest.h"
27 CBDATA_CLASS_INIT(MmappedFile
);
29 // helper class to deal with mmap(2) offset alignment and other low-level specs
33 Mmapping(int fd
, size_t length
, int prot
, int flags
, off_t offset
);
36 void *map(); ///< calls mmap(2); returns usable buffer or nil on failure
37 bool unmap(); ///< unmaps previously mapped buffer, if any
40 const int fd
; ///< descriptor of the mmapped file
41 const size_t length
; ///< user-requested data length, needed for munmap
42 const int prot
; ///< mmap(2) "protection" flags
43 const int flags
; ///< other mmap(2) flags
44 const off_t offset
; ///< user-requested data offset
46 off_t delta
; ///< mapped buffer increment to hit user offset
47 void *buf
; ///< buffer returned by mmap, needed for munmap
51 MmappedFile::operator new(size_t sz
)
53 CBDATA_INIT_TYPE(MmappedFile
);
54 MmappedFile
*result
= cbdataAlloc(MmappedFile
);
55 /* Mark result as being owned - we want the refcounter to do the delete
61 MmappedFile::operator delete(void *address
)
63 MmappedFile
*t
= static_cast<MmappedFile
*>(address
);
67 MmappedFile::MmappedFile(char const *aPath
): fd(-1),
68 minOffset(0), maxOffset(-1), error_(false)
71 path_
= xstrdup(aPath
);
72 debugs(79,5, HERE
<< this << ' ' << path_
);
75 MmappedFile::~MmappedFile()
81 // XXX: almost a copy of BlockingFile::open
83 MmappedFile::open(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
)
87 /* Simulate async calls */
88 fd
= file_open(path_
, flags
);
89 ioRequestor
= callback
;
92 debugs(79,3, HERE
<< "open error: " << xstrerror());
96 debugs(79,3, HERE
<< "FD " << fd
);
98 // setup mapping boundaries
100 if (fstat(fd
, &sb
) == 0)
101 maxOffset
= sb
.st_size
; // we do not expect it to change
104 callback
->ioCompletedNotification();
108 * Alias for MmappedFile::open(...)
109 \copydoc MmappedFile::open(int flags, mode_t mode, RefCount<IORequestor> callback)
112 MmappedFile::create(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
)
114 /* We use the same logic path for open */
115 open(flags
, mode
, callback
);
118 void MmappedFile::doClose()
123 --store_open_disk_fd
;
130 debugs(79, 3, HERE
<< this << " closing for " << ioRequestor
);
132 assert(ioRequestor
!= NULL
);
133 ioRequestor
->closeCompleted();
137 MmappedFile::canRead() const
143 MmappedFile::canWrite() const
149 MmappedFile::error() const
155 MmappedFile::read(ReadRequest
*aRequest
)
157 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
158 aRequest
->offset
<< ")");
161 assert(ioRequestor
!= NULL
);
162 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
163 assert(aRequest
->offset
>= 0);
164 assert(!error_
); // TODO: propagate instead?
166 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
167 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
169 Mmapping
mapping(fd
, aRequest
->len
, PROT_READ
, MAP_PRIVATE
| MAP_NORESERVE
,
173 if (void *buf
= mapping
.map()) {
174 memcpy(aRequest
->buf
, buf
, aRequest
->len
);
175 done
= mapping
.unmap();
179 const ssize_t rlen
= error_
? -1 : (ssize_t
)aRequest
->len
;
180 const int errflag
= error_
? DISK_ERROR
:DISK_OK
;
181 ioRequestor
->readCompleted(aRequest
->buf
, rlen
, errflag
, aRequest
);
185 MmappedFile::write(WriteRequest
*aRequest
)
187 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
188 aRequest
->offset
<< ")");
191 assert(ioRequestor
!= NULL
);
192 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
193 assert(aRequest
->offset
>= 0);
194 assert(!error_
); // TODO: propagate instead?
196 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
197 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
199 const ssize_t written
=
200 pwrite(fd
, aRequest
->buf
, aRequest
->len
, aRequest
->offset
);
202 debugs(79, DBG_IMPORTANT
, HERE
<< "error: " << xstrerr(errno
));
204 } else if (static_cast<size_t>(written
) != aRequest
->len
) {
205 debugs(79, DBG_IMPORTANT
, HERE
<< "problem: " << written
<< " < " << aRequest
->len
);
209 if (aRequest
->free_func
)
210 (aRequest
->free_func
)(const_cast<char*>(aRequest
->buf
)); // broken API?
213 debugs(79,5, HERE
<< "wrote " << aRequest
->len
<< " to FD " << fd
<< " at " << aRequest
->offset
);
218 const ssize_t rlen
= error_
? 0 : (ssize_t
)aRequest
->len
;
219 const int errflag
= error_
? DISK_ERROR
:DISK_OK
;
220 ioRequestor
->writeCompleted(errflag
, rlen
, aRequest
);
223 /// we only support blocking I/O
225 MmappedFile::ioInProgress() const
230 Mmapping::Mmapping(int aFd
, size_t aLength
, int aProt
, int aFlags
, off_t anOffset
):
231 fd(aFd
), length(aLength
), prot(aProt
), flags(aFlags
), offset(anOffset
),
236 Mmapping::~Mmapping()
245 // mmap(2) requires that offset is a multiple of the page size
246 static const int pageSize
= getpagesize();
247 delta
= offset
% pageSize
;
249 buf
= mmap(NULL
, length
+ delta
, prot
, flags
, fd
, offset
- delta
);
251 if (buf
== MAP_FAILED
) {
252 const int errNo
= errno
;
253 debugs(79,3, HERE
<< "error FD " << fd
<< "mmap(" << length
<< '+' <<
254 delta
<< ", " << offset
<< '-' << delta
<< "): " << xstrerr(errNo
));
259 return static_cast<char*>(buf
) + delta
;
265 debugs(79,9, HERE
<< "FD " << fd
<<
266 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< ')');
268 if (!buf
) // forgot or failed to map
271 const bool error
= munmap(buf
, length
+ delta
) != 0;
273 const int errNo
= errno
;
274 debugs(79,3, HERE
<< "error FD " << fd
<<
275 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< "): " <<
276 "): " << xstrerr(errNo
));
282 // TODO: check MAP_NORESERVE, consider MAP_POPULATE and MAP_FIXED