]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/rock/RockIoState.cc
63eddfd14371b1e11c1d3c6a7feba633c705896e
2 * Copyright (C) 1996-2014 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 79 Disk IO Routines */
12 #include "base/TextException.h"
13 #include "CollapsedForwarding.h"
14 #include "DiskIO/DiskIOModule.h"
15 #include "DiskIO/DiskIOStrategy.h"
16 #include "DiskIO/WriteRequest.h"
17 #include "fs/rock/RockIoRequests.h"
18 #include "fs/rock/RockIoState.h"
19 #include "fs/rock/RockSwapDir.h"
21 #include "MemObject.h"
23 #include "Transients.h"
25 Rock::IoState::IoState(Rock::SwapDir::Pointer
&aDir
,
27 StoreIOState::STFNCB
*cbFile
,
28 StoreIOState::STIOCB
*cbIo
,
30 readableAnchor_(NULL
),
31 writeableAnchor_(NULL
),
34 slotSize(dir
->slotSize
),
40 // anchor, swap_filen, and swap_dirn are set by the caller
41 file_callback
= cbFile
;
43 callback_data
= cbdataReference(data
);
44 ++store_open_disk_fd
; // TODO: use a dedicated counter?
45 //theFile is set by SwapDir because it depends on DiskIOStrategy
48 Rock::IoState::~IoState()
52 // The dir map entry may still be open for reading at the point because
53 // the map entry lock is associated with StoreEntry, not IoState.
54 // assert(!readableAnchor_);
55 assert(shutting_down
|| !writeableAnchor_
);
58 cbdataReferenceDone(callback_data
);
61 e
->unlock("rock I/O");
65 Rock::IoState::file(const RefCount
<DiskFile
> &aFile
)
68 assert(aFile
!= NULL
);
72 const Ipc::StoreMapAnchor
&
73 Rock::IoState::readAnchor() const
75 assert(readableAnchor_
);
76 return *readableAnchor_
;
80 Rock::IoState::writeAnchor()
82 assert(writeableAnchor_
);
83 return *writeableAnchor_
;
86 /// convenience wrapper returning the map slot we are reading now
87 const Ipc::StoreMapSlice
&
88 Rock::IoState::currentReadableSlice() const
90 return dir
->map
->readableSlice(swap_filen
, sidCurrent
);
94 Rock::IoState::read_(char *buf
, size_t len
, off_t coreOff
, STRCB
*cb
, void *data
)
96 debugs(79, 7, swap_filen
<< " reads from " << coreOff
);
98 assert(theFile
!= NULL
);
101 // if we are dealing with the first read or
102 // if the offset went backwords, start searching from the beginning
103 if (sidCurrent
< 0 || coreOff
< objOffset
) {
104 sidCurrent
= readAnchor().start
;
108 while (sidCurrent
>= 0 && coreOff
>= objOffset
+ currentReadableSlice().size
) {
109 objOffset
+= currentReadableSlice().size
;
110 sidCurrent
= currentReadableSlice().next
;
113 assert(read
.callback
== NULL
);
114 assert(read
.callback_data
== NULL
);
116 read
.callback_data
= cbdataReference(data
);
118 // punt if read offset is too big (because of client bugs or collapsing)
119 if (sidCurrent
< 0) {
120 debugs(79, 5, "no " << coreOff
<< " in " << *e
);
121 callReaderBack(buf
, 0);
127 static_cast<size_t>(objOffset
+ currentReadableSlice().size
- coreOff
));
128 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
129 theFile
->read(new ReadRequest(::ReadRequest(buf
,
130 diskOffset
+ sizeof(DbCellHeader
) + coreOff
- objOffset
, len
), this));
134 Rock::IoState::callReaderBack(const char *buf
, int rlen
)
136 debugs(79, 5, rlen
<< " bytes for " << *e
);
137 StoreIOState::STRCB
*callb
= read
.callback
;
139 read
.callback
= NULL
;
141 if (cbdataReferenceValidDone(read
.callback_data
, &cbdata
))
142 callb(cbdata
, buf
, rlen
, this);
145 /// wraps tryWrite() to handle deep write failures centrally and safely
147 Rock::IoState::write(char const *buf
, size_t size
, off_t coreOff
, FREE
*dtor
)
149 bool success
= false;
151 tryWrite(buf
, size
, coreOff
);
153 } catch (const std::exception
&ex
) { // TODO: should we catch ... as well?
154 debugs(79, 2, "db write error: " << ex
.what());
156 finishedWriting(DISK_ERROR
);
157 // 'this' might be gone beyond this point; fall through to free buf
160 // careful: 'this' might be gone here
163 (dtor
)(const_cast<char*>(buf
)); // cast due to a broken API?
169 * Possibly send data to be written to disk:
170 * We only write data when full slot is accumulated or when close() is called.
171 * We buffer, in part, to avoid forcing OS to _read_ old unwritten portions of
172 * the slot when the write does not end at the page or sector boundary.
175 Rock::IoState::tryWrite(char const *buf
, size_t size
, off_t coreOff
)
177 debugs(79, 7, swap_filen
<< " writes " << size
<< " more");
179 // either this is the first write or append; we do not support write gaps
180 assert(!coreOff
|| coreOff
== -1);
182 // allocate the first slice during the first write
184 assert(sidCurrent
< 0);
185 sidCurrent
= reserveSlotForWriting(); // throws on failures
186 assert(sidCurrent
>= 0);
187 writeAnchor().start
= sidCurrent
;
190 // buffer incoming data in slot buffer and write overflowing or final slots
191 // quit when no data left or we stopped writing on reentrant error
192 while (size
> 0 && theFile
!= NULL
) {
193 assert(sidCurrent
>= 0);
194 const size_t processed
= writeToBuffer(buf
, size
);
197 const bool overflow
= size
> 0;
199 // We do not write a full buffer without overflow because
200 // we would not yet know what to set the nextSlot to.
202 const SlotId sidNext
= reserveSlotForWriting(); // throws
203 assert(sidNext
>= 0);
204 writeToDisk(sidNext
);
205 } else if (Store::Root().transientReaders(*e
)) {
206 // write partial buffer for all remote hit readers to see
207 writeBufToDisk(-1, false);
213 /// Buffers incoming data for the current slot.
214 /// \return the number of bytes buffered
216 Rock::IoState::writeToBuffer(char const *buf
, size_t size
)
218 // do not buffer a cell header for nothing
223 // will fill the header in writeToDisk when the next slot is known
224 theBuf
.appended(sizeof(DbCellHeader
));
227 size_t forCurrentSlot
= min(size
, static_cast<size_t>(theBuf
.spaceSize()));
228 theBuf
.append(buf
, forCurrentSlot
);
229 offset_
+= forCurrentSlot
; // so that Core thinks we wrote it
230 return forCurrentSlot
;
233 /// write what was buffered during write() calls
234 /// negative sidNext means this is the last write request for this entry
236 Rock::IoState::writeToDisk(const SlotId sidNext
)
238 assert(theFile
!= NULL
);
239 assert(theBuf
.size
>= sizeof(DbCellHeader
));
241 // TODO: if DiskIO module is mmap-based, we should be writing whole pages
242 // to avoid triggering read-page;new_head+old_tail;write-page overheads
244 writeBufToDisk(sidNext
, sidNext
< 0);
247 sidCurrent
= sidNext
;
250 /// creates and submits a request to write current slot buffer to disk
251 /// eof is true if and only this is the last slot
253 Rock::IoState::writeBufToDisk(const SlotId sidNext
, bool eof
)
255 // no slots after the last/eof slot (but partial slots may have a nil next)
256 assert(!eof
|| sidNext
< 0);
258 // finalize db cell header
260 memcpy(header
.key
, e
->key
, sizeof(header
.key
));
261 header
.firstSlot
= writeAnchor().start
;
262 header
.nextSlot
= sidNext
;
263 header
.payloadSize
= theBuf
.size
- sizeof(DbCellHeader
);
264 header
.entrySize
= eof
? offset_
: 0; // storeSwapOutFileClosed sets swap_file_sz after write
265 header
.version
= writeAnchor().basics
.timestamp
;
267 // copy finalized db cell header into buffer
268 memcpy(theBuf
.mem
, &header
, sizeof(DbCellHeader
));
270 // and now allocate another buffer for the WriteRequest so that
271 // we can support concurrent WriteRequests (and to ease cleaning)
272 // TODO: should we limit the number of outstanding requests?
274 void *wBuf
= memAllocBuf(theBuf
.size
, &wBufCap
);
275 memcpy(wBuf
, theBuf
.mem
, theBuf
.size
);
277 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
278 debugs(79, 5, HERE
<< swap_filen
<< " at " << diskOffset
<< '+' <<
281 WriteRequest
*const r
= new WriteRequest(
282 ::WriteRequest(static_cast<char*>(wBuf
), diskOffset
, theBuf
.size
,
283 memFreeBufFunc(wBufCap
)), this);
284 r
->sidCurrent
= sidCurrent
;
285 r
->sidNext
= sidNext
;
288 // theFile->write may call writeCompleted immediatelly
292 /// finds and returns a free db slot to fill or throws
294 Rock::IoState::reserveSlotForWriting()
296 Ipc::Mem::PageId pageId
;
297 if (dir
->useFreeSlot(pageId
))
298 return pageId
.number
-1;
300 // This may happen when the number of available db slots is close to the
301 // number of concurrent requests reading or writing those slots, which may
302 // happen when the db is "small" compared to the request traffic OR when we
303 // are rebuilding and have not loaded "many" entries or empty slots yet.
304 throw TexcHere("ran out of free db slots");
308 Rock::IoState::finishedWriting(const int errFlag
)
310 // we incremented offset_ while accumulating data in write()
311 // we do not reset writeableAnchor_ here because we still keep the lock
312 CollapsedForwarding::Broadcast(*e
);
317 Rock::IoState::close(int how
)
319 debugs(79, 3, swap_filen
<< " offset: " << offset_
<< " how: " << how
<<
320 " buf: " << theBuf
.size
<< " callback: " << callback
);
323 debugs(79, 3, "I/O already canceled");
325 // We keep writeableAnchor_ after callBack() on I/O errors.
326 assert(!readableAnchor_
);
332 assert(theBuf
.size
> 0); // we never flush last bytes on our own
333 writeToDisk(-1); // flush last, yet unwritten slot to disk
334 return; // writeCompleted() will callBack()
337 assert(writeableAnchor_
);
338 dir
->writeError(*e
); // abort a partially stored entry
339 finishedWriting(DISK_ERROR
);
348 /// close callback (STIOCB) dialer: breaks dependencies and
349 /// counts IOState concurrency level
350 class StoreIOStateCb
: public CallDialer
353 StoreIOStateCb(StoreIOState::STIOCB
*cb
, void *data
, int err
, const Rock::IoState::Pointer
&anSio
):
360 callback_data
= cbdataReference(data
);
363 StoreIOStateCb(const StoreIOStateCb
&cb
):
369 callback
= cb
.callback
;
370 callback_data
= cbdataReference(cb
.callback_data
);
373 virtual ~StoreIOStateCb() {
374 cbdataReferenceDone(callback_data
); // may be nil already
377 void dial(AsyncCall
&) {
379 if (cbdataReferenceValidDone(callback_data
, &cbd
) && callback
)
380 callback(cbd
, errflag
, sio
.getRaw());
383 bool canDial(AsyncCall
&) const {
384 return cbdataReferenceValid(callback_data
) && callback
;
387 virtual void print(std::ostream
&os
) const {
388 os
<< '(' << callback_data
<< ", err=" << errflag
<< ')';
392 StoreIOStateCb
&operator =(const StoreIOStateCb
&); // not defined
394 StoreIOState::STIOCB
*callback
;
397 Rock::IoState::Pointer sio
;
401 Rock::IoState::callBack(int errflag
)
403 debugs(79,3, HERE
<< "errflag=" << errflag
);
406 AsyncCall::Pointer call
= asyncCall(79,3, "SomeIoStateCloseCb",
407 StoreIOStateCb(callback
, callback_data
, errflag
, this));
408 ScheduleCallHere(call
);
411 cbdataReferenceDone(callback_data
);