]>
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
52 MmappedFile::operator new(size_t sz
)
54 CBDATA_INIT_TYPE(MmappedFile
);
55 MmappedFile
*result
= cbdataAlloc(MmappedFile
);
56 /* Mark result as being owned - we want the refcounter to do the delete
62 MmappedFile::operator delete(void *address
)
64 MmappedFile
*t
= static_cast<MmappedFile
*>(address
);
68 MmappedFile::MmappedFile(char const *aPath
): fd(-1),
69 minOffset(0), maxOffset(-1), error_(false)
72 path_
= xstrdup(aPath
);
73 debugs(79,5, HERE
<< this << ' ' << path_
);
76 MmappedFile::~MmappedFile()
82 // XXX: almost a copy of BlockingFile::open
84 MmappedFile::open(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
)
88 /* Simulate async calls */
89 fd
= file_open(path_
, flags
);
90 ioRequestor
= callback
;
93 debugs(79,3, HERE
<< "open error: " << xstrerror());
97 debugs(79,3, HERE
<< "FD " << fd
);
99 // setup mapping boundaries
101 if (fstat(fd
, &sb
) == 0)
102 maxOffset
= sb
.st_size
; // we do not expect it to change
105 callback
->ioCompletedNotification();
109 * Alias for MmappedFile::open(...)
110 \copydoc MmappedFile::open(int flags, mode_t mode, RefCount<IORequestor> callback)
113 MmappedFile::create(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
)
115 /* We use the same logic path for open */
116 open(flags
, mode
, callback
);
119 void MmappedFile::doClose()
124 --store_open_disk_fd
;
131 debugs(79, 3, HERE
<< this << " closing for " << ioRequestor
);
133 assert(ioRequestor
!= NULL
);
134 ioRequestor
->closeCompleted();
138 MmappedFile::canRead() const
144 MmappedFile::canWrite() const
150 MmappedFile::error() const
156 MmappedFile::read(ReadRequest
*aRequest
)
158 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
159 aRequest
->offset
<< ")");
162 assert(ioRequestor
!= NULL
);
163 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
164 assert(aRequest
->offset
>= 0);
165 assert(!error_
); // TODO: propagate instead?
167 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
168 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
170 Mmapping
mapping(fd
, aRequest
->len
, PROT_READ
, MAP_PRIVATE
| MAP_NORESERVE
,
174 if (void *buf
= mapping
.map()) {
175 memcpy(aRequest
->buf
, buf
, aRequest
->len
);
176 done
= mapping
.unmap();
180 const ssize_t rlen
= error_
? -1 : (ssize_t
)aRequest
->len
;
181 const int errflag
= error_
? DISK_ERROR
:DISK_OK
;
182 ioRequestor
->readCompleted(aRequest
->buf
, rlen
, errflag
, aRequest
);
186 MmappedFile::write(WriteRequest
*aRequest
)
188 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
189 aRequest
->offset
<< ")");
192 assert(ioRequestor
!= NULL
);
193 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
194 assert(aRequest
->offset
>= 0);
195 assert(!error_
); // TODO: propagate instead?
197 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
198 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
200 const ssize_t written
=
201 pwrite(fd
, aRequest
->buf
, aRequest
->len
, aRequest
->offset
);
203 debugs(79, DBG_IMPORTANT
, HERE
<< "error: " << xstrerr(errno
));
205 } else if (static_cast<size_t>(written
) != aRequest
->len
) {
206 debugs(79, DBG_IMPORTANT
, HERE
<< "problem: " << written
<< " < " << aRequest
->len
);
210 if (aRequest
->free_func
)
211 (aRequest
->free_func
)(const_cast<char*>(aRequest
->buf
)); // broken API?
214 debugs(79,5, HERE
<< "wrote " << aRequest
->len
<< " to FD " << fd
<< " at " << aRequest
->offset
);
219 const ssize_t rlen
= error_
? 0 : (ssize_t
)aRequest
->len
;
220 const int errflag
= error_
? DISK_ERROR
:DISK_OK
;
221 ioRequestor
->writeCompleted(errflag
, rlen
, aRequest
);
224 /// we only support blocking I/O
226 MmappedFile::ioInProgress() const
231 Mmapping::Mmapping(int aFd
, size_t aLength
, int aProt
, int aFlags
, off_t anOffset
):
232 fd(aFd
), length(aLength
), prot(aProt
), flags(aFlags
), offset(anOffset
),
237 Mmapping::~Mmapping()
246 // mmap(2) requires that offset is a multiple of the page size
247 static const int pageSize
= getpagesize();
248 delta
= offset
% pageSize
;
250 buf
= mmap(NULL
, length
+ delta
, prot
, flags
, fd
, offset
- delta
);
252 if (buf
== MAP_FAILED
) {
253 const int errNo
= errno
;
254 debugs(79,3, HERE
<< "error FD " << fd
<< "mmap(" << length
<< '+' <<
255 delta
<< ", " << offset
<< '-' << delta
<< "): " << xstrerr(errNo
));
260 return static_cast<char*>(buf
) + delta
;
266 debugs(79,9, HERE
<< "FD " << fd
<<
267 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< ')');
269 if (!buf
) // forgot or failed to map
272 const bool error
= munmap(buf
, length
+ delta
) != 0;
274 const int errNo
= errno
;
275 debugs(79,3, HERE
<< "error FD " << fd
<<
276 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< "): " <<
277 "): " << xstrerr(errNo
));
283 // TODO: check MAP_NORESERVE, consider MAP_POPULATE and MAP_FIXED