]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/rock/RockIoState.cc
2 * DEBUG: section 79 Disk IO Routines
6 #include "base/TextException.h"
7 #include "DiskIO/DiskIOModule.h"
8 #include "DiskIO/DiskIOStrategy.h"
9 #include "DiskIO/WriteRequest.h"
10 #include "fs/rock/RockIoState.h"
11 #include "fs/rock/RockIoRequests.h"
12 #include "fs/rock/RockSwapDir.h"
14 #include "MemObject.h"
18 Rock::IoState::IoState(Rock::SwapDir::Pointer
&aDir
,
20 StoreIOState::STFNCB
*cbFile
,
21 StoreIOState::STIOCB
*cbIo
,
23 readableAnchor_(NULL
),
24 writeableAnchor_(NULL
),
27 slotSize(dir
->slotSize
),
33 // anchor, swap_filen, and swap_dirn are set by the caller
34 file_callback
= cbFile
;
36 callback_data
= cbdataReference(data
);
37 ++store_open_disk_fd
; // TODO: use a dedicated counter?
38 //theFile is set by SwapDir because it depends on DiskIOStrategy
41 Rock::IoState::~IoState()
45 // The dir map entry may still be open for reading at the point because
46 // the map entry lock is associated with StoreEntry, not IoState.
47 // assert(!readableAnchor_);
48 assert(!writeableAnchor_
);
51 cbdataReferenceDone(callback_data
);
54 e
->unlock("rock I/O");
58 Rock::IoState::file(const RefCount
<DiskFile
> &aFile
)
61 assert(aFile
!= NULL
);
65 const Ipc::StoreMapAnchor
&
66 Rock::IoState::readAnchor() const
68 assert(readableAnchor_
);
69 return *readableAnchor_
;
73 Rock::IoState::writeAnchor()
75 assert(writeableAnchor_
);
76 return *writeableAnchor_
;
79 /// convenience wrapper returning the map slot we are reading now
80 const Ipc::StoreMapSlice
&
81 Rock::IoState::currentReadableSlice() const
83 return dir
->map
->readableSlice(swap_filen
, sidCurrent
);
87 Rock::IoState::read_(char *buf
, size_t len
, off_t coreOff
, STRCB
*cb
, void *data
)
89 debugs(79, 7, swap_filen
<< " reads from " << coreOff
);
91 assert(theFile
!= NULL
);
94 // if we are dealing with the first read or
95 // if the offset went backwords, start searching from the beginning
96 if (sidCurrent
< 0 || coreOff
< objOffset
) {
97 sidCurrent
= readAnchor().start
;
101 while (coreOff
>= objOffset
+ currentReadableSlice().size
) {
102 objOffset
+= currentReadableSlice().size
;
103 sidCurrent
= currentReadableSlice().next
;
104 assert(sidCurrent
>= 0); // XXX: handle "read offset too big" error
109 static_cast<size_t>(objOffset
+ currentReadableSlice().size
- coreOff
));
111 assert(read
.callback
== NULL
);
112 assert(read
.callback_data
== NULL
);
114 read
.callback_data
= cbdataReference(data
);
116 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
117 theFile
->read(new ReadRequest(::ReadRequest(buf
,
118 diskOffset
+ sizeof(DbCellHeader
) + coreOff
- objOffset
, len
), this));
121 /// wraps tryWrite() to handle deep write failures centrally and safely
123 Rock::IoState::write(char const *buf
, size_t size
, off_t coreOff
, FREE
*dtor
)
125 bool success
= false;
127 tryWrite(buf
, size
, coreOff
);
129 } catch (const std::exception
&ex
) { // TODO: should we catch ... as well?
130 debugs(79, 2, "db write error: " << ex
.what());
131 dir
->writeError(swap_filen
);
132 finishedWriting(DISK_ERROR
);
133 // 'this' might be gone beyond this point; fall through to free buf
136 // careful: 'this' might be gone here
139 (dtor
)(const_cast<char*>(buf
)); // cast due to a broken API?
144 /** We only write data when full slot is accumulated or when close() is called.
145 We buffer, in part, to avoid forcing OS to _read_ old unwritten portions of
146 the slot when the write does not end at the page or sector boundary. */
148 Rock::IoState::tryWrite(char const *buf
, size_t size
, off_t coreOff
)
150 debugs(79, 7, swap_filen
<< " writes " << size
<< " more");
152 // either this is the first write or append; we do not support write gaps
153 assert(!coreOff
|| coreOff
== -1);
155 // allocate the first slice diring the first write
157 assert(sidCurrent
< 0);
158 sidCurrent
= reserveSlotForWriting(); // throws on failures
159 assert(sidCurrent
>= 0);
160 writeAnchor().start
= sidCurrent
;
163 // buffer incoming data in slot buffer and write overflowing or final slots
164 // quit when no data left or we stopped writing on reentrant error
165 while (size
> 0 && theFile
!= NULL
) {
166 assert(sidCurrent
>= 0);
167 const size_t processed
= writeToBuffer(buf
, size
);
170 const bool overflow
= size
> 0;
172 // We do not write a full buffer without overflow because
173 // we would not yet know what to set the nextSlot to.
175 const SlotId sidNext
= reserveSlotForWriting(); // throws
176 assert(sidNext
>= 0);
177 writeToDisk(sidNext
);
182 /// Buffers incoming data for the current slot.
183 /// Returns the number of bytes buffered.
185 Rock::IoState::writeToBuffer(char const *buf
, size_t size
)
187 // do not buffer a cell header for nothing
192 // will fill the header in writeToDisk when the next slot is known
193 theBuf
.appended(sizeof(DbCellHeader
));
196 size_t forCurrentSlot
= min(size
, static_cast<size_t>(theBuf
.spaceSize()));
197 theBuf
.append(buf
, forCurrentSlot
);
198 offset_
+= forCurrentSlot
; // so that Core thinks we wrote it
199 return forCurrentSlot
;
202 /// write what was buffered during write() calls
203 /// negative sidNext means this is the last write request for this entry
205 Rock::IoState::writeToDisk(const SlotId sidNext
)
207 assert(theFile
!= NULL
);
208 assert(theBuf
.size
>= sizeof(DbCellHeader
));
210 if (sidNext
< 0) { // we are writing the last slot
211 e
->swap_file_sz
= offset_
;
212 writeAnchor().basics
.swap_file_sz
= offset_
; // would not hurt, right?
215 // TODO: if DiskIO module is mmap-based, we should be writing whole pages
216 // to avoid triggering read-page;new_head+old_tail;write-page overheads
218 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
219 debugs(79, 5, HERE
<< swap_filen
<< " at " << diskOffset
<< '+' <<
222 // finalize map slice
223 Ipc::StoreMap::Slice
&slice
=
224 dir
->map
->writeableSlice(swap_filen
, sidCurrent
);
225 slice
.next
= sidNext
;
226 slice
.size
= theBuf
.size
- sizeof(DbCellHeader
);
228 // finalize db cell header
230 memcpy(header
.key
, e
->key
, sizeof(header
.key
));
231 header
.firstSlot
= writeAnchor().start
;
232 header
.nextSlot
= sidNext
;
233 header
.payloadSize
= theBuf
.size
- sizeof(DbCellHeader
);
234 header
.entrySize
= e
->swap_file_sz
; // may still be zero unless sidNext < 0
235 header
.version
= writeAnchor().basics
.timestamp
;
237 // copy finalized db cell header into buffer
238 memcpy(theBuf
.mem
, &header
, sizeof(DbCellHeader
));
240 // and now allocate another buffer for the WriteRequest so that
241 // we can support concurrent WriteRequests (and to ease cleaning)
242 // TODO: should we limit the number of outstanding requests?
244 void *wBuf
= memAllocBuf(theBuf
.size
, &wBufCap
);
245 memcpy(wBuf
, theBuf
.mem
, theBuf
.size
);
247 WriteRequest
*const r
= new WriteRequest(
248 ::WriteRequest(static_cast<char*>(wBuf
), diskOffset
, theBuf
.size
,
249 memFreeBufFunc(wBufCap
)), this, sidNext
< 0);
252 sidCurrent
= sidNext
;
254 // theFile->write may call writeCompleted immediatelly
258 /// finds and returns a free db slot to fill or throws
260 Rock::IoState::reserveSlotForWriting()
262 Ipc::Mem::PageId pageId
;
263 if (dir
->useFreeSlot(pageId
))
264 return pageId
.number
-1;
266 // This may happen when the number of available db slots is close to the
267 // number of concurrent requests reading or writing those slots, which may
268 // happen when the db is "small" compared to the request traffic OR when we
269 // are rebuilding and have not loaded "many" entries or empty slots yet.
270 throw TexcHere("ran out of free db slots");
274 Rock::IoState::finishedWriting(const int errFlag
)
276 // we incremented offset_ while accumulating data in write()
277 writeableAnchor_
= NULL
;
282 Rock::IoState::close(int how
)
284 debugs(79, 3, swap_filen
<< " offset: " << offset_
<< " how: " << how
<<
285 " buf: " << theBuf
.size
<< " callback: " << callback
);
288 debugs(79, 3, "I/O already canceled");
290 assert(!writeableAnchor_
);
291 assert(!readableAnchor_
);
297 assert(theBuf
.size
> 0); // we never flush last bytes on our own
298 writeToDisk(-1); // flush last, yet unwritten slot to disk
299 return; // writeCompleted() will callBack()
302 assert(writeableAnchor_
);
303 dir
->writeError(swap_filen
); // abort a partially stored entry
304 finishedWriting(DISK_ERROR
);
313 /// close callback (STIOCB) dialer: breaks dependencies and
314 /// counts IOState concurrency level
315 class StoreIOStateCb
: public CallDialer
318 StoreIOStateCb(StoreIOState::STIOCB
*cb
, void *data
, int err
, const Rock::IoState::Pointer
&anSio
):
325 callback_data
= cbdataReference(data
);
328 StoreIOStateCb(const StoreIOStateCb
&cb
):
334 callback
= cb
.callback
;
335 callback_data
= cbdataReference(cb
.callback_data
);
338 virtual ~StoreIOStateCb() {
339 cbdataReferenceDone(callback_data
); // may be nil already
342 void dial(AsyncCall
&call
) {
344 if (cbdataReferenceValidDone(callback_data
, &cbd
) && callback
)
345 callback(cbd
, errflag
, sio
.getRaw());
348 bool canDial(AsyncCall
&call
) const {
349 return cbdataReferenceValid(callback_data
) && callback
;
352 virtual void print(std::ostream
&os
) const {
353 os
<< '(' << callback_data
<< ", err=" << errflag
<< ')';
357 StoreIOStateCb
&operator =(const StoreIOStateCb
&cb
); // not defined
359 StoreIOState::STIOCB
*callback
;
362 Rock::IoState::Pointer sio
;
366 Rock::IoState::callBack(int errflag
)
368 debugs(79,3, HERE
<< "errflag=" << errflag
);
371 AsyncCall::Pointer call
= asyncCall(79,3, "SomeIoStateCloseCb",
372 StoreIOStateCb(callback
, callback_data
, errflag
, this));
373 ScheduleCallHere(call
);
376 cbdataReferenceDone(callback_data
);