]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/rock/RockIoState.cc
34964d2882d44a094976e74859fb276b9b5463f8
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"
22 #include "MemObject.h"
24 #include "Transients.h"
26 Rock::IoState::IoState(Rock::SwapDir::Pointer
&aDir
,
28 StoreIOState::STFNCB
*cbFile
,
29 StoreIOState::STIOCB
*cbIo
,
31 readableAnchor_(NULL
),
32 writeableAnchor_(NULL
),
35 slotSize(dir
->slotSize
),
41 // anchor, swap_filen, and swap_dirn are set by the caller
42 file_callback
= cbFile
;
44 callback_data
= cbdataReference(data
);
45 ++store_open_disk_fd
; // TODO: use a dedicated counter?
46 //theFile is set by SwapDir because it depends on DiskIOStrategy
49 Rock::IoState::~IoState()
53 // The dir map entry may still be open for reading at the point because
54 // the map entry lock is associated with StoreEntry, not IoState.
55 // assert(!readableAnchor_);
56 assert(shutting_down
|| !writeableAnchor_
);
59 cbdataReferenceDone(callback_data
);
62 e
->unlock("rock I/O");
66 Rock::IoState::file(const RefCount
<DiskFile
> &aFile
)
69 assert(aFile
!= NULL
);
73 const Ipc::StoreMapAnchor
&
74 Rock::IoState::readAnchor() const
76 assert(readableAnchor_
);
77 return *readableAnchor_
;
81 Rock::IoState::writeAnchor()
83 assert(writeableAnchor_
);
84 return *writeableAnchor_
;
87 /// convenience wrapper returning the map slot we are reading now
88 const Ipc::StoreMapSlice
&
89 Rock::IoState::currentReadableSlice() const
91 return dir
->map
->readableSlice(swap_filen
, sidCurrent
);
95 Rock::IoState::read_(char *buf
, size_t len
, off_t coreOff
, STRCB
*cb
, void *data
)
97 debugs(79, 7, swap_filen
<< " reads from " << coreOff
);
99 assert(theFile
!= NULL
);
100 assert(coreOff
>= 0);
102 // if we are dealing with the first read or
103 // if the offset went backwords, start searching from the beginning
104 if (sidCurrent
< 0 || coreOff
< objOffset
) {
105 sidCurrent
= readAnchor().start
;
109 while (sidCurrent
>= 0 && coreOff
>= objOffset
+ currentReadableSlice().size
) {
110 objOffset
+= currentReadableSlice().size
;
111 sidCurrent
= currentReadableSlice().next
;
114 assert(read
.callback
== NULL
);
115 assert(read
.callback_data
== NULL
);
117 read
.callback_data
= cbdataReference(data
);
119 // punt if read offset is too big (because of client bugs or collapsing)
120 if (sidCurrent
< 0) {
121 debugs(79, 5, "no " << coreOff
<< " in " << *e
);
122 callReaderBack(buf
, 0);
128 static_cast<size_t>(objOffset
+ currentReadableSlice().size
- coreOff
));
129 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
130 theFile
->read(new ReadRequest(::ReadRequest(buf
,
131 diskOffset
+ sizeof(DbCellHeader
) + coreOff
- objOffset
, len
), this));
135 Rock::IoState::callReaderBack(const char *buf
, int rlen
)
137 debugs(79, 5, rlen
<< " bytes for " << *e
);
138 StoreIOState::STRCB
*callb
= read
.callback
;
140 read
.callback
= NULL
;
142 if (cbdataReferenceValidDone(read
.callback_data
, &cbdata
))
143 callb(cbdata
, buf
, rlen
, this);
146 /// wraps tryWrite() to handle deep write failures centrally and safely
148 Rock::IoState::write(char const *buf
, size_t size
, off_t coreOff
, FREE
*dtor
)
150 bool success
= false;
152 tryWrite(buf
, size
, coreOff
);
154 } catch (const std::exception
&ex
) { // TODO: should we catch ... as well?
155 debugs(79, 2, "db write error: " << ex
.what());
157 finishedWriting(DISK_ERROR
);
158 // 'this' might be gone beyond this point; fall through to free buf
161 // careful: 'this' might be gone here
164 (dtor
)(const_cast<char*>(buf
)); // cast due to a broken API?
170 * Possibly send data to be written to disk:
171 * We only write data when full slot is accumulated or when close() is called.
172 * We buffer, in part, to avoid forcing OS to _read_ old unwritten portions of
173 * the slot when the write does not end at the page or sector boundary.
176 Rock::IoState::tryWrite(char const *buf
, size_t size
, off_t coreOff
)
178 debugs(79, 7, swap_filen
<< " writes " << size
<< " more");
180 // either this is the first write or append; we do not support write gaps
181 assert(!coreOff
|| coreOff
== -1);
183 // allocate the first slice during the first write
185 assert(sidCurrent
< 0);
186 sidCurrent
= reserveSlotForWriting(); // throws on failures
187 assert(sidCurrent
>= 0);
188 writeAnchor().start
= sidCurrent
;
191 // buffer incoming data in slot buffer and write overflowing or final slots
192 // quit when no data left or we stopped writing on reentrant error
193 while (size
> 0 && theFile
!= NULL
) {
194 assert(sidCurrent
>= 0);
195 const size_t processed
= writeToBuffer(buf
, size
);
198 const bool overflow
= size
> 0;
200 // We do not write a full buffer without overflow because
201 // we would not yet know what to set the nextSlot to.
203 const SlotId sidNext
= reserveSlotForWriting(); // throws
204 assert(sidNext
>= 0);
205 writeToDisk(sidNext
);
206 } else if (Store::Root().transientReaders(*e
)) {
207 // write partial buffer for all remote hit readers to see
208 writeBufToDisk(-1, false);
214 /// Buffers incoming data for the current slot.
215 /// \return the number of bytes buffered
217 Rock::IoState::writeToBuffer(char const *buf
, size_t size
)
219 // do not buffer a cell header for nothing
224 // will fill the header in writeToDisk when the next slot is known
225 theBuf
.appended(sizeof(DbCellHeader
));
228 size_t forCurrentSlot
= min(size
, static_cast<size_t>(theBuf
.spaceSize()));
229 theBuf
.append(buf
, forCurrentSlot
);
230 offset_
+= forCurrentSlot
; // so that Core thinks we wrote it
231 return forCurrentSlot
;
234 /// write what was buffered during write() calls
235 /// negative sidNext means this is the last write request for this entry
237 Rock::IoState::writeToDisk(const SlotId sidNext
)
239 assert(theFile
!= NULL
);
240 assert(theBuf
.size
>= sizeof(DbCellHeader
));
242 // TODO: if DiskIO module is mmap-based, we should be writing whole pages
243 // to avoid triggering read-page;new_head+old_tail;write-page overheads
245 writeBufToDisk(sidNext
, sidNext
< 0);
248 sidCurrent
= sidNext
;
251 /// creates and submits a request to write current slot buffer to disk
252 /// eof is true if and only this is the last slot
254 Rock::IoState::writeBufToDisk(const SlotId sidNext
, bool eof
)
256 // no slots after the last/eof slot (but partial slots may have a nil next)
257 assert(!eof
|| sidNext
< 0);
259 // finalize db cell header
261 memcpy(header
.key
, e
->key
, sizeof(header
.key
));
262 header
.firstSlot
= writeAnchor().start
;
263 header
.nextSlot
= sidNext
;
264 header
.payloadSize
= theBuf
.size
- sizeof(DbCellHeader
);
265 header
.entrySize
= eof
? offset_
: 0; // storeSwapOutFileClosed sets swap_file_sz after write
266 header
.version
= writeAnchor().basics
.timestamp
;
268 // copy finalized db cell header into buffer
269 memcpy(theBuf
.mem
, &header
, sizeof(DbCellHeader
));
271 // and now allocate another buffer for the WriteRequest so that
272 // we can support concurrent WriteRequests (and to ease cleaning)
273 // TODO: should we limit the number of outstanding requests?
275 void *wBuf
= memAllocBuf(theBuf
.size
, &wBufCap
);
276 memcpy(wBuf
, theBuf
.mem
, theBuf
.size
);
278 const uint64_t diskOffset
= dir
->diskOffset(sidCurrent
);
279 debugs(79, 5, HERE
<< swap_filen
<< " at " << diskOffset
<< '+' <<
282 WriteRequest
*const r
= new WriteRequest(
283 ::WriteRequest(static_cast<char*>(wBuf
), diskOffset
, theBuf
.size
,
284 memFreeBufFunc(wBufCap
)), this);
285 r
->sidCurrent
= sidCurrent
;
286 r
->sidNext
= sidNext
;
289 // theFile->write may call writeCompleted immediatelly
293 /// finds and returns a free db slot to fill or throws
295 Rock::IoState::reserveSlotForWriting()
297 Ipc::Mem::PageId pageId
;
298 if (dir
->useFreeSlot(pageId
))
299 return pageId
.number
-1;
301 // This may happen when the number of available db slots is close to the
302 // number of concurrent requests reading or writing those slots, which may
303 // happen when the db is "small" compared to the request traffic OR when we
304 // are rebuilding and have not loaded "many" entries or empty slots yet.
305 throw TexcHere("ran out of free db slots");
309 Rock::IoState::finishedWriting(const int errFlag
)
311 // we incremented offset_ while accumulating data in write()
312 // we do not reset writeableAnchor_ here because we still keep the lock
313 CollapsedForwarding::Broadcast(*e
);
318 Rock::IoState::close(int how
)
320 debugs(79, 3, swap_filen
<< " offset: " << offset_
<< " how: " << how
<<
321 " buf: " << theBuf
.size
<< " callback: " << callback
);
324 debugs(79, 3, "I/O already canceled");
326 // We keep writeableAnchor_ after callBack() on I/O errors.
327 assert(!readableAnchor_
);
333 assert(theBuf
.size
> 0); // we never flush last bytes on our own
334 writeToDisk(-1); // flush last, yet unwritten slot to disk
335 return; // writeCompleted() will callBack()
338 assert(writeableAnchor_
);
339 dir
->writeError(*e
); // abort a partially stored entry
340 finishedWriting(DISK_ERROR
);
349 /// close callback (STIOCB) dialer: breaks dependencies and
350 /// counts IOState concurrency level
351 class StoreIOStateCb
: public CallDialer
354 StoreIOStateCb(StoreIOState::STIOCB
*cb
, void *data
, int err
, const Rock::IoState::Pointer
&anSio
):
361 callback_data
= cbdataReference(data
);
364 StoreIOStateCb(const StoreIOStateCb
&cb
):
370 callback
= cb
.callback
;
371 callback_data
= cbdataReference(cb
.callback_data
);
374 virtual ~StoreIOStateCb() {
375 cbdataReferenceDone(callback_data
); // may be nil already
378 void dial(AsyncCall
&call
) {
380 if (cbdataReferenceValidDone(callback_data
, &cbd
) && callback
)
381 callback(cbd
, errflag
, sio
.getRaw());
384 bool canDial(AsyncCall
&call
) const {
385 return cbdataReferenceValid(callback_data
) && callback
;
388 virtual void print(std::ostream
&os
) const {
389 os
<< '(' << callback_data
<< ", err=" << errflag
<< ')';
393 StoreIOStateCb
&operator =(const StoreIOStateCb
&cb
); // not defined
395 StoreIOState::STIOCB
*callback
;
398 Rock::IoState::Pointer sio
;
402 Rock::IoState::callBack(int errflag
)
404 debugs(79,3, HERE
<< "errflag=" << errflag
);
407 AsyncCall::Pointer call
= asyncCall(79,3, "SomeIoStateCloseCb",
408 StoreIOStateCb(callback
, callback_data
, errflag
, this));
409 ScheduleCallHere(call
);
412 cbdataReferenceDone(callback_data
);