]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/Mmapped/MmappedFile.cc
9a0a52f2758437b5d9a18a8637dfc11e8abb70d6
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 47 Store Directory Routines */
13 #include "DiskIO/IORequestor.h"
14 #include "DiskIO/Mmapped/MmappedFile.h"
15 #include "DiskIO/ReadRequest.h"
16 #include "DiskIO/WriteRequest.h"
28 // Some systems such as Hurd provide mmap() API but do not support MAP_NORESERVE
30 #define MAP_NORESERVE 0
33 CBDATA_CLASS_INIT(MmappedFile
);
35 // helper class to deal with mmap(2) offset alignment and other low-level specs
39 Mmapping(int fd
, size_t length
, int prot
, int flags
, off_t offset
);
42 void *map(); ///< calls mmap(2); returns usable buffer or nil on failure
43 bool unmap(); ///< unmaps previously mapped buffer, if any
46 const int fd
; ///< descriptor of the mmapped file
47 const size_t length
; ///< user-requested data length, needed for munmap
48 const int prot
; ///< mmap(2) "protection" flags
49 const int flags
; ///< other mmap(2) flags
50 const off_t offset
; ///< user-requested data offset
52 off_t delta
; ///< mapped buffer increment to hit user offset
53 void *buf
; ///< buffer returned by mmap, needed for munmap
56 MmappedFile::MmappedFile(char const *aPath
): fd(-1),
57 minOffset(0), maxOffset(-1), error_(false)
60 path_
= xstrdup(aPath
);
61 debugs(79,5, HERE
<< this << ' ' << path_
);
64 MmappedFile::~MmappedFile()
70 // XXX: almost a copy of BlockingFile::open
72 MmappedFile::open(int flags
, mode_t
, RefCount
<IORequestor
> callback
)
76 /* Simulate async calls */
77 fd
= file_open(path_
, flags
);
78 ioRequestor
= callback
;
82 debugs(79,3, "open error: " << xstrerr(xerrno
));
86 debugs(79,3, "FD " << fd
);
88 // setup mapping boundaries
90 if (fstat(fd
, &sb
) == 0)
91 maxOffset
= sb
.st_size
; // we do not expect it to change
94 callback
->ioCompletedNotification();
98 * Alias for MmappedFile::open(...)
99 \copydoc MmappedFile::open(int flags, mode_t mode, RefCount<IORequestor> callback)
102 MmappedFile::create(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
)
104 /* We use the same logic path for open */
105 open(flags
, mode
, callback
);
108 void MmappedFile::doClose()
113 --store_open_disk_fd
;
120 debugs(79, 3, HERE
<< this << " closing for " << ioRequestor
);
122 assert(ioRequestor
!= NULL
);
123 ioRequestor
->closeCompleted();
127 MmappedFile::canRead() const
133 MmappedFile::canWrite() const
139 MmappedFile::error() const
145 MmappedFile::read(ReadRequest
*aRequest
)
147 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
148 aRequest
->offset
<< ")");
151 assert(ioRequestor
!= NULL
);
152 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
153 assert(aRequest
->offset
>= 0);
154 assert(!error_
); // TODO: propagate instead?
156 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
157 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
159 Mmapping
mapping(fd
, aRequest
->len
, PROT_READ
, MAP_PRIVATE
| MAP_NORESERVE
,
163 if (void *buf
= mapping
.map()) {
164 memcpy(aRequest
->buf
, buf
, aRequest
->len
);
165 done
= mapping
.unmap();
169 const ssize_t rlen
= error_
? -1 : (ssize_t
)aRequest
->len
;
170 const int errflag
= error_
? DISK_ERROR
:DISK_OK
;
171 ioRequestor
->readCompleted(aRequest
->buf
, rlen
, errflag
, aRequest
);
175 MmappedFile::write(WriteRequest
*aRequest
)
177 debugs(79,3, HERE
<< "(FD " << fd
<< ", " << aRequest
->len
<< ", " <<
178 aRequest
->offset
<< ")");
181 assert(ioRequestor
!= NULL
);
182 assert(aRequest
->len
> 0); // TODO: work around mmap failures on zero-len?
183 assert(aRequest
->offset
>= 0);
184 assert(!error_
); // TODO: propagate instead?
186 assert(minOffset
< 0 || minOffset
<= aRequest
->offset
);
187 assert(maxOffset
< 0 || static_cast<uint64_t>(aRequest
->offset
+ aRequest
->len
) <= static_cast<uint64_t>(maxOffset
));
189 const ssize_t written
=
190 pwrite(fd
, aRequest
->buf
, aRequest
->len
, aRequest
->offset
);
192 debugs(79, DBG_IMPORTANT
, HERE
<< "error: " << xstrerr(errno
));
194 } else if (static_cast<size_t>(written
) != aRequest
->len
) {
195 debugs(79, DBG_IMPORTANT
, HERE
<< "problem: " << written
<< " < " << aRequest
->len
);
199 if (aRequest
->free_func
)
200 (aRequest
->free_func
)(const_cast<char*>(aRequest
->buf
)); // broken API?
203 debugs(79,5, HERE
<< "wrote " << aRequest
->len
<< " to FD " << fd
<< " at " << aRequest
->offset
);
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
);
213 /// we only support blocking I/O
215 MmappedFile::ioInProgress() const
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
),
226 Mmapping::~Mmapping()
235 // mmap(2) requires that offset is a multiple of the page size
236 static const int pageSize
= getpagesize();
237 delta
= offset
% pageSize
;
239 buf
= mmap(NULL
, length
+ delta
, prot
, flags
, fd
, offset
- delta
);
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
));
249 return static_cast<char*>(buf
) + delta
;
255 debugs(79,9, HERE
<< "FD " << fd
<<
256 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< ')');
258 if (!buf
) // forgot or failed to map
261 const bool error
= munmap(buf
, length
+ delta
) != 0;
263 const int errNo
= errno
;
264 debugs(79,3, HERE
<< "error FD " << fd
<<
265 " munmap(" << buf
<< ", " << length
<< '+' << delta
<< "): " <<
266 "): " << xstrerr(errNo
));
272 // TODO: check MAP_NORESERVE, consider MAP_POPULATE and MAP_FIXED