]>
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);
141 /// wraps tryWrite() to handle deep write failures centrally and safely
143 Rock::IoState::write(char const *buf
, size_t size
, off_t coreOff
, FREE
*dtor
)
145 bool success
= false;
147 tryWrite(buf
, size
, coreOff
);
149 } catch (const std::exception
&ex
) { // TODO: should we catch ... as well?
150 debugs(79, 2, "db write error: " << ex
.what());
152 finishedWriting(DISK_ERROR
);
153 // 'this' might be gone beyond this point; fall through to free buf
156 // careful: 'this' might be gone here
159 (dtor
)(const_cast<char*>(buf
)); // cast due to a broken API?
165 * Possibly send data to be written to disk:
166 * We only write data when full slot is accumulated or when close() is called.
167 * We buffer, in part, to avoid forcing OS to _read_ old unwritten portions of
168 * the slot when the write does not end at the page or sector boundary.
171 Rock::IoState::tryWrite(char const *buf
, size_t size
, off_t coreOff
)
173 debugs(79, 7, swap_filen
<< " writes " << size
<< " more");
175 // either this is the first write or append; we do not support write gaps
176 assert(!coreOff
|| coreOff
== -1);
178 // allocate the first slice during the first write
180 assert(sidCurrent
< 0);
181 sidCurrent
= reserveSlotForWriting(); // throws on failures
182 assert(sidCurrent
>= 0);
183 writeAnchor().start
= sidCurrent
;
186 // buffer incoming data in slot buffer and write overflowing or final slots
187 // quit when no data left or we stopped writing on reentrant error
188 while (size
> 0 && theFile
!= NULL
) {
189 assert(sidCurrent
>= 0);
190 const size_t processed
= writeToBuffer(buf
, size
);
193 const bool overflow
= size
> 0;
195 // We do not write a full buffer without overflow because
196 // we would not yet know what to set the nextSlot to.
198 const SlotId sidNext
= reserveSlotForWriting(); // throws
199 assert(sidNext
>= 0);
200 writeToDisk(sidNext
);
201 } else if (Store::Root().transientReaders(*e
)) {
202 // write partial buffer for all remote hit readers to see
203 writeBufToDisk(-1, false);
209 /// Buffers incoming data for the current slot.
210 /// \return the number of bytes buffered
212 Rock::IoState::writeToBuffer(char const *buf
, size_t size
)
214 // do not buffer a cell header for nothing
219 // will fill the header in writeToDisk when the next slot is known
220 theBuf
.appended(sizeof(DbCellHeader
));
223 size_t forCurrentSlot
= min(size
, static_cast<size_t>(theBuf
.spaceSize()));
224 theBuf
.append(buf
, forCurrentSlot
);
225 offset_
+= forCurrentSlot
; // so that Core thinks we wrote it
226 return forCurrentSlot
;
229 /// write what was buffered during write() calls
230 /// negative sidNext means this is the last write request for this entry
232 Rock::IoState::writeToDisk(const SlotId sidNext
)
234 assert(theFile
!= NULL
);
235 assert(theBuf
.size
>= sizeof(DbCellHeader
));
237 // TODO: if DiskIO module is mmap-based, we should be writing whole pages
238 // to avoid triggering read-page;new_head+old_tail;write-page overheads
240 writeBufToDisk(sidNext
, sidNext
< 0);
243 sidCurrent
= sidNext
;
246 /// creates and submits a request to write current slot buffer to disk
247 /// eof is true if and only this is the last slot
249 Rock::IoState::writeBufToDisk(const SlotId sidNext
, bool eof
)
251 // no slots after the last/eof slot (but partial slots may have a nil next)
252 assert(!eof
|| sidNext
< 0);
254 // finalize db cell header
256 memcpy(header
.key
, e
->key
, sizeof(header
.key
));
257 header
.firstSlot
= writeAnchor().start
;
258 header
.nextSlot
= sidNext
;
259 header
.payloadSize
= theBuf
.size
- sizeof(DbCellHeader
);
260 header
.entrySize
= eof
? offset_
: 0; // storeSwapOutFileClosed sets swap_file_sz after write
261 header
.version
= writeAnchor().basics
.timestamp
;
263 // copy finalized db cell header into buffer
264 memcpy(theBuf
.mem
, &header
, sizeof(DbCellHeader
));
266 // and now allocate another buffer for the WriteRequest so that
267 // we can support concurrent WriteRequests (and to ease cleaning)
268 // TODO: should we limit the number of outstanding requests?
270 void *wBuf
= memAllocBuf(theBuf
.size
, &wBufCap
);
271 memcpy(wBuf
, theBuf
.mem
, theBuf
.size
);
273 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
274 debugs(79, 5, HERE
<< swap_filen
<< " at " << diskOffset
<< '+' <<
277 WriteRequest
*const r
= new WriteRequest(
278 ::WriteRequest(static_cast<char*>(wBuf
), diskOffset
, theBuf
.size
,
279 memFreeBufFunc(wBufCap
)), this);
280 r
->sidCurrent
= sidCurrent
;
281 r
->sidNext
= sidNext
;
284 // theFile->write may call writeCompleted immediatelly
288 /// finds and returns a free db slot to fill or throws
290 Rock::IoState::reserveSlotForWriting()
292 Ipc::Mem::PageId pageId
;
293 if (dir
->useFreeSlot(pageId
))
294 return pageId
.number
-1;
296 // This may happen when the number of available db slots is close to the
297 // number of concurrent requests reading or writing those slots, which may
298 // happen when the db is "small" compared to the request traffic OR when we
299 // are rebuilding and have not loaded "many" entries or empty slots yet.
300 throw TexcHere("ran out of free db slots");
304 Rock::IoState::finishedWriting(const int errFlag
)
306 // we incremented offset_ while accumulating data in write()
307 // we do not reset writeableAnchor_ here because we still keep the lock
308 CollapsedForwarding::Broadcast(*e
);
313 Rock::IoState::close(int how
)
315 debugs(79, 3, swap_filen
<< " offset: " << offset_
<< " how: " << how
<<
316 " buf: " << theBuf
.size
<< " callback: " << callback
);
319 debugs(79, 3, "I/O already canceled");
321 // We keep writeableAnchor_ after callBack() on I/O errors.
322 assert(!readableAnchor_
);
328 assert(theBuf
.size
> 0); // we never flush last bytes on our own
329 writeToDisk(-1); // flush last, yet unwritten slot to disk
330 return; // writeCompleted() will callBack()
333 assert(writeableAnchor_
);
334 dir
->writeError(*e
); // abort a partially stored entry
335 finishedWriting(DISK_ERROR
);
344 /// close callback (STIOCB) dialer: breaks dependencies and
345 /// counts IOState concurrency level
346 class StoreIOStateCb
: public CallDialer
349 StoreIOStateCb(StoreIOState::STIOCB
*cb
, void *data
, int err
, const Rock::IoState::Pointer
&anSio
):
356 callback_data
= cbdataReference(data
);
359 StoreIOStateCb(const StoreIOStateCb
&cb
):
365 callback
= cb
.callback
;
366 callback_data
= cbdataReference(cb
.callback_data
);
369 virtual ~StoreIOStateCb() {
370 cbdataReferenceDone(callback_data
); // may be nil already
373 void dial(AsyncCall
&call
) {
375 if (cbdataReferenceValidDone(callback_data
, &cbd
) && callback
)
376 callback(cbd
, errflag
, sio
.getRaw());
379 bool canDial(AsyncCall
&call
) const {
380 return cbdataReferenceValid(callback_data
) && callback
;
383 virtual void print(std::ostream
&os
) const {
384 os
<< '(' << callback_data
<< ", err=" << errflag
<< ')';
388 StoreIOStateCb
&operator =(const StoreIOStateCb
&cb
); // not defined
390 StoreIOState::STIOCB
*callback
;
393 Rock::IoState::Pointer sio
;
397 Rock::IoState::callBack(int errflag
)
399 debugs(79,3, HERE
<< "errflag=" << errflag
);
402 AsyncCall::Pointer call
= asyncCall(79,3, "SomeIoStateCloseCb",
403 StoreIOStateCb(callback
, callback_data
, errflag
, this));
404 ScheduleCallHere(call
);
407 cbdataReferenceDone(callback_data
);