]>
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 "CollapsedForwarding.h"
8 #include "DiskIO/DiskIOModule.h"
9 #include "DiskIO/DiskIOStrategy.h"
10 #include "DiskIO/WriteRequest.h"
11 #include "fs/rock/RockIoRequests.h"
12 #include "fs/rock/RockIoState.h"
13 #include "fs/rock/RockSwapDir.h"
16 #include "MemObject.h"
18 #include "Transients.h"
20 Rock::IoState::IoState(Rock::SwapDir::Pointer
&aDir
,
22 StoreIOState::STFNCB
*cbFile
,
23 StoreIOState::STIOCB
*cbIo
,
25 readableAnchor_(NULL
),
26 writeableAnchor_(NULL
),
29 slotSize(dir
->slotSize
),
35 // anchor, swap_filen, and swap_dirn are set by the caller
36 file_callback
= cbFile
;
38 callback_data
= cbdataReference(data
);
39 ++store_open_disk_fd
; // TODO: use a dedicated counter?
40 //theFile is set by SwapDir because it depends on DiskIOStrategy
43 Rock::IoState::~IoState()
47 // The dir map entry may still be open for reading at the point because
48 // the map entry lock is associated with StoreEntry, not IoState.
49 // assert(!readableAnchor_);
50 assert(shutting_down
|| !writeableAnchor_
);
53 cbdataReferenceDone(callback_data
);
56 e
->unlock("rock I/O");
60 Rock::IoState::file(const RefCount
<DiskFile
> &aFile
)
63 assert(aFile
!= NULL
);
67 const Ipc::StoreMapAnchor
&
68 Rock::IoState::readAnchor() const
70 assert(readableAnchor_
);
71 return *readableAnchor_
;
75 Rock::IoState::writeAnchor()
77 assert(writeableAnchor_
);
78 return *writeableAnchor_
;
81 /// convenience wrapper returning the map slot we are reading now
82 const Ipc::StoreMapSlice
&
83 Rock::IoState::currentReadableSlice() const
85 return dir
->map
->readableSlice(swap_filen
, sidCurrent
);
89 Rock::IoState::read_(char *buf
, size_t len
, off_t coreOff
, STRCB
*cb
, void *data
)
91 debugs(79, 7, swap_filen
<< " reads from " << coreOff
);
93 assert(theFile
!= NULL
);
96 // if we are dealing with the first read or
97 // if the offset went backwords, start searching from the beginning
98 if (sidCurrent
< 0 || coreOff
< objOffset
) {
99 sidCurrent
= readAnchor().start
;
103 while (sidCurrent
>= 0 && coreOff
>= objOffset
+ currentReadableSlice().size
) {
104 objOffset
+= currentReadableSlice().size
;
105 sidCurrent
= currentReadableSlice().next
;
108 assert(read
.callback
== NULL
);
109 assert(read
.callback_data
== NULL
);
111 read
.callback_data
= cbdataReference(data
);
113 // punt if read offset is too big (because of client bugs or collapsing)
114 if (sidCurrent
< 0) {
115 debugs(79, 5, "no " << coreOff
<< " in " << *e
);
116 callReaderBack(buf
, 0);
122 static_cast<size_t>(objOffset
+ currentReadableSlice().size
- coreOff
));
123 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
124 theFile
->read(new ReadRequest(::ReadRequest(buf
,
125 diskOffset
+ sizeof(DbCellHeader
) + coreOff
- objOffset
, len
), this));
129 Rock::IoState::callReaderBack(const char *buf
, int rlen
)
131 debugs(79, 5, rlen
<< " bytes for " << *e
);
132 StoreIOState::STRCB
*callb
= read
.callback
;
134 read
.callback
= NULL
;
136 if (cbdataReferenceValidDone(read
.callback_data
, &cbdata
))
137 callb(cbdata
, buf
, rlen
, this);
140 /// wraps tryWrite() to handle deep write failures centrally and safely
142 Rock::IoState::write(char const *buf
, size_t size
, off_t coreOff
, FREE
*dtor
)
144 bool success
= false;
146 tryWrite(buf
, size
, coreOff
);
148 } catch (const std::exception
&ex
) { // TODO: should we catch ... as well?
149 debugs(79, 2, "db write error: " << ex
.what());
151 finishedWriting(DISK_ERROR
);
152 // 'this' might be gone beyond this point; fall through to free buf
155 // careful: 'this' might be gone here
158 (dtor
)(const_cast<char*>(buf
)); // cast due to a broken API?
164 * Possibly send data to be written to disk:
165 * We only write data when full slot is accumulated or when close() is called.
166 * We buffer, in part, to avoid forcing OS to _read_ old unwritten portions of
167 * the slot when the write does not end at the page or sector boundary.
170 Rock::IoState::tryWrite(char const *buf
, size_t size
, off_t coreOff
)
172 debugs(79, 7, swap_filen
<< " writes " << size
<< " more");
174 // either this is the first write or append; we do not support write gaps
175 assert(!coreOff
|| coreOff
== -1);
177 // allocate the first slice during the first write
179 assert(sidCurrent
< 0);
180 sidCurrent
= reserveSlotForWriting(); // throws on failures
181 assert(sidCurrent
>= 0);
182 writeAnchor().start
= sidCurrent
;
185 // buffer incoming data in slot buffer and write overflowing or final slots
186 // quit when no data left or we stopped writing on reentrant error
187 while (size
> 0 && theFile
!= NULL
) {
188 assert(sidCurrent
>= 0);
189 const size_t processed
= writeToBuffer(buf
, size
);
192 const bool overflow
= size
> 0;
194 // We do not write a full buffer without overflow because
195 // we would not yet know what to set the nextSlot to.
197 const SlotId sidNext
= reserveSlotForWriting(); // throws
198 assert(sidNext
>= 0);
199 writeToDisk(sidNext
);
200 } else if (Store::Root().transientReaders(*e
)) {
201 // write partial buffer for all remote hit readers to see
202 writeBufToDisk(-1, false);
208 /// Buffers incoming data for the current slot.
209 /// \return the number of bytes buffered
211 Rock::IoState::writeToBuffer(char const *buf
, size_t size
)
213 // do not buffer a cell header for nothing
218 // will fill the header in writeToDisk when the next slot is known
219 theBuf
.appended(sizeof(DbCellHeader
));
222 size_t forCurrentSlot
= min(size
, static_cast<size_t>(theBuf
.spaceSize()));
223 theBuf
.append(buf
, forCurrentSlot
);
224 offset_
+= forCurrentSlot
; // so that Core thinks we wrote it
225 return forCurrentSlot
;
228 /// write what was buffered during write() calls
229 /// negative sidNext means this is the last write request for this entry
231 Rock::IoState::writeToDisk(const SlotId sidNext
)
233 assert(theFile
!= NULL
);
234 assert(theBuf
.size
>= sizeof(DbCellHeader
));
236 // TODO: if DiskIO module is mmap-based, we should be writing whole pages
237 // to avoid triggering read-page;new_head+old_tail;write-page overheads
239 writeBufToDisk(sidNext
, sidNext
< 0);
242 sidCurrent
= sidNext
;
245 /// creates and submits a request to write current slot buffer to disk
246 /// eof is true if and only this is the last slot
248 Rock::IoState::writeBufToDisk(const SlotId sidNext
, bool eof
)
250 // no slots after the last/eof slot (but partial slots may have a nil next)
251 assert(!eof
|| sidNext
< 0);
253 // finalize db cell header
255 memcpy(header
.key
, e
->key
, sizeof(header
.key
));
256 header
.firstSlot
= writeAnchor().start
;
257 header
.nextSlot
= sidNext
;
258 header
.payloadSize
= theBuf
.size
- sizeof(DbCellHeader
);
259 header
.entrySize
= eof
? offset_
: 0; // storeSwapOutFileClosed sets swap_file_sz after write
260 header
.version
= writeAnchor().basics
.timestamp
;
262 // copy finalized db cell header into buffer
263 memcpy(theBuf
.mem
, &header
, sizeof(DbCellHeader
));
265 // and now allocate another buffer for the WriteRequest so that
266 // we can support concurrent WriteRequests (and to ease cleaning)
267 // TODO: should we limit the number of outstanding requests?
269 void *wBuf
= memAllocBuf(theBuf
.size
, &wBufCap
);
270 memcpy(wBuf
, theBuf
.mem
, theBuf
.size
);
272 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
273 debugs(79, 5, HERE
<< swap_filen
<< " at " << diskOffset
<< '+' <<
276 WriteRequest
*const r
= new WriteRequest(
277 ::WriteRequest(static_cast<char*>(wBuf
), diskOffset
, theBuf
.size
,
278 memFreeBufFunc(wBufCap
)), this);
279 r
->sidCurrent
= sidCurrent
;
280 r
->sidNext
= sidNext
;
283 // theFile->write may call writeCompleted immediatelly
287 /// finds and returns a free db slot to fill or throws
289 Rock::IoState::reserveSlotForWriting()
291 Ipc::Mem::PageId pageId
;
292 if (dir
->useFreeSlot(pageId
))
293 return pageId
.number
-1;
295 // This may happen when the number of available db slots is close to the
296 // number of concurrent requests reading or writing those slots, which may
297 // happen when the db is "small" compared to the request traffic OR when we
298 // are rebuilding and have not loaded "many" entries or empty slots yet.
299 throw TexcHere("ran out of free db slots");
303 Rock::IoState::finishedWriting(const int errFlag
)
305 // we incremented offset_ while accumulating data in write()
306 // we do not reset writeableAnchor_ here because we still keep the lock
307 CollapsedForwarding::Broadcast(*e
);
312 Rock::IoState::close(int how
)
314 debugs(79, 3, swap_filen
<< " offset: " << offset_
<< " how: " << how
<<
315 " buf: " << theBuf
.size
<< " callback: " << callback
);
318 debugs(79, 3, "I/O already canceled");
320 // We keep writeableAnchor_ after callBack() on I/O errors.
321 assert(!readableAnchor_
);
327 assert(theBuf
.size
> 0); // we never flush last bytes on our own
328 writeToDisk(-1); // flush last, yet unwritten slot to disk
329 return; // writeCompleted() will callBack()
332 assert(writeableAnchor_
);
333 dir
->writeError(*e
); // abort a partially stored entry
334 finishedWriting(DISK_ERROR
);
343 /// close callback (STIOCB) dialer: breaks dependencies and
344 /// counts IOState concurrency level
345 class StoreIOStateCb
: public CallDialer
348 StoreIOStateCb(StoreIOState::STIOCB
*cb
, void *data
, int err
, const Rock::IoState::Pointer
&anSio
):
355 callback_data
= cbdataReference(data
);
358 StoreIOStateCb(const StoreIOStateCb
&cb
):
364 callback
= cb
.callback
;
365 callback_data
= cbdataReference(cb
.callback_data
);
368 virtual ~StoreIOStateCb() {
369 cbdataReferenceDone(callback_data
); // may be nil already
372 void dial(AsyncCall
&call
) {
374 if (cbdataReferenceValidDone(callback_data
, &cbd
) && callback
)
375 callback(cbd
, errflag
, sio
.getRaw());
378 bool canDial(AsyncCall
&call
) const {
379 return cbdataReferenceValid(callback_data
) && callback
;
382 virtual void print(std::ostream
&os
) const {
383 os
<< '(' << callback_data
<< ", err=" << errflag
<< ')';
387 StoreIOStateCb
&operator =(const StoreIOStateCb
&cb
); // not defined
389 StoreIOState::STIOCB
*callback
;
392 Rock::IoState::Pointer sio
;
396 Rock::IoState::callBack(int errflag
)
398 debugs(79,3, HERE
<< "errflag=" << errflag
);
401 AsyncCall::Pointer call
= asyncCall(79,3, "SomeIoStateCloseCb",
402 StoreIOStateCb(callback
, callback_data
, errflag
, this));
403 ScheduleCallHere(call
);
406 cbdataReferenceDone(callback_data
);