]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/Mmapped/MmappedFile.cc
2 * DEBUG: section 47 Store Directory Routines
7 #include "DiskIO/IORequestor.h"
8 #include "DiskIO/Mmapped/MmappedFile.h"
9 #include "DiskIO/ReadRequest.h"
10 #include "DiskIO/WriteRequest.h"
24 CBDATA_CLASS_INIT(MmappedFile
);
26 // helper class to deal with mmap(2) offset alignment and other low-level specs
30 Mmapping(int fd
, size_t length
, int prot
, int flags
, off_t offset
);
33 void *map(); ///< calls mmap(2); returns usable buffer or nil on failure
34 bool unmap(); ///< unmaps previously mapped buffer, if any
37 const int fd
; ///< descriptor of the mmapped file
38 const size_t length
; ///< user-requested data length, needed for munmap
39 const int prot
; ///< mmap(2) "protection" flags
40 const int flags
; ///< other mmap(2) flags
41 const off_t offset
; ///< user-requested data offset
43 off_t delta
; ///< mapped buffer increment to hit user offset
44 void *buf
; ///< buffer returned by mmap, needed for munmap
48 MmappedFile::operator new(size_t sz
)
50 CBDATA_INIT_TYPE(MmappedFile
);
51 MmappedFile
*result
= cbdataAlloc(MmappedFile
);
52 /* Mark result as being owned - we want the refcounter to do the delete
58 MmappedFile::operator delete(void *address
)
60 MmappedFile
*t
= static_cast<MmappedFile
*>(address
);
64 MmappedFile::MmappedFile(char const *aPath
): fd(-1),
65 minOffset(0), maxOffset(-1), error_(false)
68 path_
= xstrdup(aPath
);
69 debugs(79,5, HERE
<< this << ' ' << path_
);
72 MmappedFile::~MmappedFile()
78 // XXX: almost a copy of BlockingFile::open
80 MmappedFile::open(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
)
84 /* Simulate async calls */
85 fd
= file_open(path_
, flags
);
86 ioRequestor
= callback
;
89 debugs(79,3, HERE
<< "open error: " << xstrerror());
93 debugs(79,3, HERE
<< "FD " << fd
);
95 // setup mapping boundaries
97 if (fstat(fd
, &sb
) == 0)
98 maxOffset
= sb
.st_size
; // we do not expect it to change
101 callback
->ioCompletedNotification();
105 * Alias for MmappedFile::open(...)
106 \copydoc MmappedFile::open(int flags, mode_t mode, RefCount<IORequestor> callback)
109 MmappedFile::create(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
)
111 /* We use the same logic path for open */
112 open(flags
, mode
, callback
);
115 void MmappedFile::doClose()
120 --store_open_disk_fd
;
127 debugs(79, 3, HERE
<< this << " closing for " << ioRequestor
);
129 assert(ioRequestor
!= NULL
);
130 ioRequestor
->closeCompleted();
134 MmappedFile::canRead() const
140 MmappedFile::canWrite() const
146 MmappedFile::error() const
152 MmappedFile::read(ReadRequest
*aRequest
)
154 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
155 aRequest
->offset
<< ")");
158 assert(ioRequestor
!= NULL
);
159 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
160 assert(aRequest
->offset
>= 0);
161 assert(!error_
); // TODO: propagate instead?
163 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
164 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
166 Mmapping
mapping(fd
, aRequest
->len
, PROT_READ
, MAP_PRIVATE
| MAP_NORESERVE
,
170 if (void *buf
= mapping
.map()) {
171 memcpy(aRequest
->buf
, buf
, aRequest
->len
);
172 done
= mapping
.unmap();
176 const ssize_t rlen
= error_
? -1 : (ssize_t
)aRequest
->len
;
177 const int errflag
= error_
? DISK_ERROR
:DISK_OK
;
178 ioRequestor
->readCompleted(aRequest
->buf
, rlen
, errflag
, aRequest
);
182 MmappedFile::write(WriteRequest
*aRequest
)
184 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
185 aRequest
->offset
<< ")");
188 assert(ioRequestor
!= NULL
);
189 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
190 assert(aRequest
->offset
>= 0);
191 assert(!error_
); // TODO: propagate instead?
193 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
194 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
196 const ssize_t written
=
197 pwrite(fd
, aRequest
->buf
, aRequest
->len
, aRequest
->offset
);
199 debugs(79, DBG_IMPORTANT
, HERE
<< "error: " << xstrerr(errno
));
201 } else if (static_cast<size_t>(written
) != aRequest
->len
) {
202 debugs(79, DBG_IMPORTANT
, HERE
<< "problem: " << written
<< " < " << aRequest
->len
);
206 if (aRequest
->free_func
)
207 (aRequest
->free_func
)(const_cast<char*>(aRequest
->buf
)); // broken API?
210 debugs(79,5, HERE
<< "wrote " << aRequest
->len
<< " to FD " << fd
<< " at " << aRequest
->offset
);
215 const ssize_t rlen
= error_
? 0 : (ssize_t
)aRequest
->len
;
216 const int errflag
= error_
? DISK_ERROR
:DISK_OK
;
217 ioRequestor
->writeCompleted(errflag
, rlen
, aRequest
);
220 /// we only support blocking I/O
222 MmappedFile::ioInProgress() const
227 Mmapping::Mmapping(int aFd
, size_t aLength
, int aProt
, int aFlags
, off_t anOffset
):
228 fd(aFd
), length(aLength
), prot(aProt
), flags(aFlags
), offset(anOffset
),
233 Mmapping::~Mmapping()
242 // mmap(2) requires that offset is a multiple of the page size
243 static const int pageSize
= getpagesize();
244 delta
= offset
% pageSize
;
246 buf
= mmap(NULL
, length
+ delta
, prot
, flags
, fd
, offset
- delta
);
248 if (buf
== MAP_FAILED
) {
249 const int errNo
= errno
;
250 debugs(79,3, HERE
<< "error FD " << fd
<< "mmap(" << length
<< '+' <<
251 delta
<< ", " << offset
<< '-' << delta
<< "): " << xstrerr(errNo
));
256 return static_cast<char*>(buf
) + delta
;
262 debugs(79,9, HERE
<< "FD " << fd
<<
263 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< ')');
265 if (!buf
) // forgot or failed to map
268 const bool error
= munmap(buf
, length
+ delta
) != 0;
270 const int errNo
= errno
;
271 debugs(79,3, HERE
<< "error FD " << fd
<<
272 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< "): " <<
273 "): " << xstrerr(errNo
));
279 // TODO: check MAP_NORESERVE, consider MAP_POPULATE and MAP_FIXED