]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/ufs/UFSStoreState.cc
5be2a46e01c5205ba18c6489214cfe9f50a77c9d
2 * Copyright (C) 1996-2015 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 Storage Manager UFS Interface */
12 #include "DiskIO/DiskFile.h"
13 #include "DiskIO/DiskIOStrategy.h"
14 #include "DiskIO/ReadRequest.h"
15 #include "DiskIO/WriteRequest.h"
17 #include "SquidList.h"
19 #include "store/Disk.h"
20 #include "UFSStoreState.h"
21 #include "UFSStrategy.h"
23 CBDATA_NAMESPACED_CLASS_INIT(Fs::Ufs
,UFSStoreState
);
26 Fs::Ufs::UFSStoreState::ioCompletedNotification()
30 debugs(79, 3, "UFSStoreState::ioCompletedNotification: dirno " <<
31 swap_dirn
<< ", fileno "<< std::setfill('0') << std::hex
<<
32 std::setw(8) << swap_filen
<< " status "<< std::setfill(' ') <<
33 std::dec
<< theFile
->error());
35 assert (FILE_MODE(mode
) == O_RDONLY
);
43 debugs(79, 3, "UFSStoreState::ioCompletedNotification: dirno " <<
44 swap_dirn
<< ", fileno "<< std::setfill('0') << std::hex
<<
45 std::setw(8) << swap_filen
<< " status "<< std::setfill(' ') <<
46 std::dec
<< theFile
->error());
53 assert (!(closing
||opening
));
54 debugs(79, 3, "diskd::ioCompleted: dirno " << swap_dirn
<< ", fileno "<<
55 std::setfill('0') << std::hex
<< std::setw(8) << swap_filen
<<
56 " status "<< std::setfill(' ') << std::dec
<< theFile
->error());
58 /* Ok, notification past open means an error has occured */
59 assert (theFile
->error());
64 Fs::Ufs::UFSStoreState::openDone()
67 debugs(0, DBG_CRITICAL
, HERE
<< "already closing in openDone()!?");
69 if (theFile
->error()) {
74 if (FILE_MODE(mode
) == O_WRONLY
) {
77 } else if ((FILE_MODE(mode
) == O_RDONLY
) && !closing
) {
82 if (flags
.try_closing
)
85 debugs(79, 3, "UFSStoreState::openDone: exiting");
89 Fs::Ufs::UFSStoreState::closeCompleted()
92 debugs(79, 3, "UFSStoreState::closeCompleted: dirno " << swap_dirn
<<
93 ", fileno "<< std::setfill('0') << std::hex
<< std::setw(8) <<
94 swap_filen
<< " status "<< std::setfill(' ') << std::dec
<<
97 if (theFile
->error()) {
98 debugs(79,3,HERE
<< "theFile->error() ret " << theFile
->error());
99 doCloseCallback(DISK_ERROR
);
101 doCloseCallback(DISK_OK
);
109 * This close function is called by the higher layer when it has finished
110 * reading/writing everything, or otherwise wants to close the swap
111 * file. In the case of writing and using aufs storage, close() might
112 * be called before any/all data is written, and even before the open
113 * callback occurs. Thus, we use our tryClosing() method, which knows
114 * when it is safe to actually signal the lower layer for closing.
117 Fs::Ufs::UFSStoreState::close(int)
119 debugs(79, 3, "UFSStoreState::close: dirno " << swap_dirn
<< ", fileno "<<
120 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
);
121 tryClosing(); // UFS does not distinguish different closure types
125 Fs::Ufs::UFSStoreState::read_(char *buf
, size_t size
, off_t aOffset
, STRCB
* aCallback
, void *aCallbackData
)
127 assert(read
.callback
== NULL
);
128 assert(read
.callback_data
== NULL
);
133 if (!theFile
->canRead()) {
134 debugs(79, 3, "UFSStoreState::read_: queueing read because theFile can't read");
135 queueRead (buf
, size
, aOffset
, aCallback
, aCallbackData
);
139 read
.callback
= aCallback
;
140 read
.callback_data
= cbdataReference(aCallbackData
);
141 debugs(79, 3, "UFSStoreState::read_: dirno " << swap_dirn
<< ", fileno "<<
142 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
);
146 theFile
->read(new ReadRequest(buf
,aOffset
,size
));
151 * This, the public write interface, places the write request at the end
152 * of the pending_writes queue to ensure correct ordering of writes.
153 * We could optimize things a little if there are no other pending
154 * writes and just do the write directly. But for now we'll keep the
155 * code simpler and always go through the pending_writes queue.
158 Fs::Ufs::UFSStoreState::write(char const *buf
, size_t size
, off_t aOffset
, FREE
* free_func
)
160 debugs(79, 3, "UFSStoreState::write: dirn " << swap_dirn
<< ", fileno "<<
161 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
);
163 if (theFile
->error()) {
164 debugs(79, DBG_IMPORTANT
,HERE
<< "avoid write on theFile with error");
165 debugs(79, DBG_IMPORTANT
,HERE
<< "calling free_func for " << (void*) buf
);
166 free_func((void*)buf
);
170 queueWrite(buf
, size
, aOffset
, free_func
);
177 * This, the private write method, calls the lower level write for the
178 * first write request in the pending_writes queue. doWrite() is only
179 * called by drainWriteQueue().
182 Fs::Ufs::UFSStoreState::doWrite()
184 debugs(79, 3, HERE
<< this << " UFSStoreState::doWrite");
186 assert(theFile
->canWrite());
188 _queued_write
*q
= (_queued_write
*)linklistShift(&pending_writes
);
191 debugs(79, 3, HERE
<< this << " UFSStoreState::doWrite queue is empty");
195 if (theFile
->error()) {
196 debugs(79, DBG_IMPORTANT
,HERE
<< "avoid write on theFile with error");
197 debugs(79,3,HERE
<< "calling free_func for " << (void*) q
->buf
);
200 * Note "free_func" is memNodeWriteComplete(), which doesn't
201 * really free the memory. Instead it clears the node's
202 * write_pending flag.
204 q
->free_func((void*)q
->buf
);
211 * UFSStoreState has a 'writing' flag that we used to set here,
212 * but it wasn't really used anywhere. In fact, some lower
213 * layers such as DISKD allow multiple outstanding writes, which
214 * makes the boolean writing flag meaningless. We would need
215 * a counter to keep track of writes going out and write callbacks
216 * coming in. For now let's just not use the writing flag at
219 debugs(79, 3, HERE
<< this << " calling theFile->write(" << q
->size
<< ")");
221 theFile
->write(new WriteRequest(q
->buf
, q
->offset
, q
->size
, q
->free_func
));
226 Fs::Ufs::UFSStoreState::readCompleted(const char *buf
, int len
, int, RefCount
<ReadRequest
> result
)
228 assert (result
.getRaw());
230 debugs(79, 3, "UFSStoreState::readCompleted: dirno " << swap_dirn
<<
231 ", fileno "<< std::setfill('0') << std::hex
<< std::setw(8) <<
232 swap_filen
<< " len "<< std::setfill(' ') << std::dec
<< len
);
237 STRCB
*callback_
= read
.callback
;
241 read
.callback
= NULL
;
246 * diskd IO queues closes via the diskd queue. So close callbacks
247 * occur strictly after reads and writes.
248 * ufs doesn't queue, it simply completes, so close callbacks occur
249 * strictly after reads and writes.
250 * aufs performs closes syncronously, so close events must be managed
251 * to force strict ordering.
252 * The below does this:
253 * closing is set when theFile->close() has been called, and close only triggers
254 * when no io's are pending.
255 * writeCompleted likewise.
257 if (!closing
&& cbdataReferenceValidDone(read
.callback_data
, &cbdata
)) {
258 if (len
> 0 && read_buf
!= buf
)
259 memcpy(read_buf
, buf
, len
);
261 callback_(cbdata
, read_buf
, len
, this);
264 if (flags
.try_closing
|| (theFile
!= NULL
&& theFile
->error()) )
269 Fs::Ufs::UFSStoreState::writeCompleted(int, size_t len
, RefCount
<WriteRequest
>)
271 debugs(79, 3, HERE
<< "dirno " << swap_dirn
<< ", fileno " <<
272 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
<<
276 * See doWrites() for why we don't update UFSStoreState::writing
282 if (theFile
->error()) {
283 debugs(79,2,HERE
<< " detected an error, will try to close");
289 * Kick any pending write/close operations alive
295 Fs::Ufs::UFSStoreState::doCloseCallback(int errflag
)
297 debugs(79, 3, "storeUfsIOCallback: errflag=" << errflag
);
300 * When we signal the higher layer with this callback, it might unlock
301 * the StoreEntry and its associated data. We must "free" any queued
302 * I/Os (especially writes) now, otherwise the StoreEntry's mem_node's
303 * will have their write_pending flag set, and we'll get an assertion.
306 STIOCB
*theCallback
= callback
;
311 if (cbdataReferenceValidDone(callback_data
, &cbdata
) && theCallback
)
312 theCallback(cbdata
, errflag
, this);
315 * We are finished with theFile since the lower layer signalled
316 * us that the file has been closed. This must be the last line,
317 * as theFile may be the only object holding us in memory.
319 theFile
= NULL
; // refcounted
322 /* ============= THE REAL UFS CODE ================ */
324 Fs::Ufs::UFSStoreState::UFSStoreState(SwapDir
* SD
, StoreEntry
* anEntry
, STIOCB
* cbIo
, void *data
) :
325 StoreIOState(NULL
, cbIo
, data
),
332 pending_writes(NULL
),
335 // StoreIOState inherited members
336 swap_filen
= anEntry
->swap_filen
;
337 swap_dirn
= SD
->index
;
341 flags
.write_draining
= false;
342 flags
.try_closing
= false;
345 Fs::Ufs::UFSStoreState::~UFSStoreState()
347 assert(pending_reads
== NULL
);
348 assert(pending_writes
== NULL
);
352 Fs::Ufs::UFSStoreState::freePending()
356 while ((qr
= (_queued_read
*)linklistShift(&pending_reads
))) {
357 cbdataReferenceDone(qr
->callback_data
);
361 debugs(79,3,HERE
<< "UFSStoreState::freePending: freed pending reads");
365 while ((qw
= (_queued_write
*)linklistShift(&pending_writes
))) {
367 qw
->free_func(const_cast<char *>(qw
->buf
));
371 debugs(79,3,HERE
<< "UFSStoreState::freePending: freed pending writes");
375 Fs::Ufs::UFSStoreState::kickReadQueue()
377 _queued_read
*q
= (_queued_read
*)linklistShift(&pending_reads
);
382 debugs(79, 3, "UFSStoreState::kickReadQueue: reading queued request of " << q
->size
<< " bytes");
386 if (cbdataReferenceValidDone(q
->callback_data
, &cbdata
)) {
387 read_(q
->buf
, q
->size
, q
->offset
, q
->callback
, cbdata
);
389 debugs(79, 2, "UFSStoreState::kickReadQueue: this: " << this << " cbdataReferenceValidDone returned false." << " closing: " << closing
<< " flags.try_closing: " << flags
.try_closing
);
400 Fs::Ufs::UFSStoreState::queueRead(char *buf
, size_t size
, off_t aOffset
, STRCB
*callback_
, void *callback_data_
)
402 debugs(79, 3, "UFSStoreState::queueRead: queueing read");
404 assert (pending_reads
== NULL
);
405 _queued_read
*q
= new _queued_read
;
409 q
->callback
= callback_
;
410 q
->callback_data
= cbdataReference(callback_data_
);
411 linklistPush(&pending_reads
, q
);
416 * drainWriteQueue() is a loop around doWrite().
419 Fs::Ufs::UFSStoreState::drainWriteQueue()
423 * We might find that flags.write_draining is already set
424 * because schemes like diskd can process I/O acks
425 * before sending another I/O request. e.g. the following
426 * sequence of events: open request -> write request ->
427 * drainWriteQueue() -> queue full -> callbacks -> openDone() ->
430 if (flags
.write_draining
)
433 if (!theFile
->canWrite())
436 flags
.write_draining
= true;
438 while (pending_writes
!= NULL
) {
442 flags
.write_draining
= false;
444 if (flags
.try_closing
)
450 * This blows. DiskThreadsDiskFile::close() won't actually do the close
451 * if ioInProgress() is true. So we have to check it here. Maybe someday
452 * DiskThreadsDiskFile::close() will be modified to have a return value,
453 * or will remember to do the close for us.
456 Fs::Ufs::UFSStoreState::tryClosing()
458 debugs(79,3,HERE
<< this << " tryClosing()" <<
459 " closing = " << closing
<<
460 " flags.try_closing = " << flags
.try_closing
<<
461 " ioInProgress = " << theFile
->ioInProgress());
463 if (theFile
->ioInProgress()) {
464 debugs(79, 3, HERE
<< this <<
465 " won't close since ioInProgress is true, bailing");
466 flags
.try_closing
= true;
471 flags
.try_closing
= false;
476 Fs::Ufs::UFSStoreState::queueWrite(char const *buf
, size_t size
, off_t aOffset
, FREE
* free_func
)
478 debugs(79, 3, HERE
<< this << " UFSStoreState::queueWrite: queueing write of size " << size
);
481 q
= new _queued_write
;
485 q
->free_func
= free_func
;
486 linklistPush(&pending_writes
, q
);