]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/ufs/UFSStoreState.cc
5 * DEBUG: section 79 Storage Manager UFS Interface
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
39 #include "DiskIO/DiskFile.h"
40 #include "DiskIO/DiskIOStrategy.h"
41 #include "DiskIO/ReadRequest.h"
42 #include "DiskIO/WriteRequest.h"
43 #include "SquidList.h"
45 #include "UFSStrategy.h"
46 #include "UFSStoreState.h"
48 CBDATA_NAMESPACED_CLASS_INIT(Fs::Ufs
,UFSStoreState
);
51 Fs::Ufs::UFSStoreState::operator new (size_t)
53 CBDATA_INIT_TYPE(UFSStoreState
);
54 return cbdataAlloc(UFSStoreState
);
58 Fs::Ufs::UFSStoreState::operator delete (void *address
)
64 Fs::Ufs::UFSStoreState::ioCompletedNotification()
68 debugs(79, 3, "UFSStoreState::ioCompletedNotification: dirno " <<
69 swap_dirn
<< ", fileno "<< std::setfill('0') << std::hex
<<
70 std::setw(8) << swap_filen
<< " status "<< std::setfill(' ') <<
71 std::dec
<< theFile
->error());
73 assert (FILE_MODE(mode
) == O_RDONLY
);
81 debugs(79, 3, "UFSStoreState::ioCompletedNotification: dirno " <<
82 swap_dirn
<< ", fileno "<< std::setfill('0') << std::hex
<<
83 std::setw(8) << swap_filen
<< " status "<< std::setfill(' ') <<
84 std::dec
<< theFile
->error());
91 assert (!(closing
||opening
));
92 debugs(79, 3, "diskd::ioCompleted: dirno " << swap_dirn
<< ", fileno "<<
93 std::setfill('0') << std::hex
<< std::setw(8) << swap_filen
<<
94 " status "<< std::setfill(' ') << std::dec
<< theFile
->error());
96 /* Ok, notification past open means an error has occured */
97 assert (theFile
->error());
102 Fs::Ufs::UFSStoreState::openDone()
105 debugs(0, DBG_CRITICAL
, HERE
<< "already closing in openDone()!?");
107 if (theFile
->error()) {
112 if (FILE_MODE(mode
) == O_WRONLY
) {
115 } else if ((FILE_MODE(mode
) == O_RDONLY
) && !closing
) {
120 if (flags
.try_closing
)
123 debugs(79, 3, "UFSStoreState::openDone: exiting");
127 Fs::Ufs::UFSStoreState::closeCompleted()
130 debugs(79, 3, "UFSStoreState::closeCompleted: dirno " << swap_dirn
<<
131 ", fileno "<< std::setfill('0') << std::hex
<< std::setw(8) <<
132 swap_filen
<< " status "<< std::setfill(' ') << std::dec
<<
135 if (theFile
->error()) {
136 debugs(79,3,HERE
<< "theFile->error() ret " << theFile
->error());
137 doCloseCallback(DISK_ERROR
);
139 doCloseCallback(DISK_OK
);
147 * This close function is called by the higher layer when it has finished
148 * reading/writing everything, or otherwise wants to close the swap
149 * file. In the case of writing and using aufs storage, close() might
150 * be called before any/all data is written, and even before the open
151 * callback occurs. Thus, we use our tryClosing() method, which knows
152 * when it is safe to actually signal the lower layer for closing.
155 Fs::Ufs::UFSStoreState::close(int)
157 debugs(79, 3, "UFSStoreState::close: dirno " << swap_dirn
<< ", fileno "<<
158 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
);
159 tryClosing(); // UFS does not distinguish different closure types
163 Fs::Ufs::UFSStoreState::read_(char *buf
, size_t size
, off_t aOffset
, STRCB
* aCallback
, void *aCallbackData
)
165 assert(read
.callback
== NULL
);
166 assert(read
.callback_data
== NULL
);
171 if (!theFile
->canRead()) {
172 debugs(79, 3, "UFSStoreState::read_: queueing read because theFile can't read");
173 queueRead (buf
, size
, aOffset
, aCallback
, aCallbackData
);
177 read
.callback
= aCallback
;
178 read
.callback_data
= cbdataReference(aCallbackData
);
179 debugs(79, 3, "UFSStoreState::read_: dirno " << swap_dirn
<< ", fileno "<<
180 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
);
184 theFile
->read(new ReadRequest(buf
,aOffset
,size
));
189 * This, the public write interface, places the write request at the end
190 * of the pending_writes queue to ensure correct ordering of writes.
191 * We could optimize things a little if there are no other pending
192 * writes and just do the write directly. But for now we'll keep the
193 * code simpler and always go through the pending_writes queue.
196 Fs::Ufs::UFSStoreState::write(char const *buf
, size_t size
, off_t aOffset
, FREE
* free_func
)
198 debugs(79, 3, "UFSStoreState::write: dirn " << swap_dirn
<< ", fileno "<<
199 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
);
201 if (theFile
->error()) {
202 debugs(79, DBG_IMPORTANT
,HERE
<< "avoid write on theFile with error");
203 debugs(79, DBG_IMPORTANT
,HERE
<< "calling free_func for " << (void*) buf
);
204 free_func((void*)buf
);
208 queueWrite(buf
, size
, aOffset
, free_func
);
214 * This, the private write method, calls the lower level write for the
215 * first write request in the pending_writes queue. doWrite() is only
216 * called by drainWriteQueue().
219 Fs::Ufs::UFSStoreState::doWrite()
221 debugs(79, 3, HERE
<< this << " UFSStoreState::doWrite");
223 assert(theFile
->canWrite());
225 _queued_write
*q
= (_queued_write
*)linklistShift(&pending_writes
);
228 debugs(79, 3, HERE
<< this << " UFSStoreState::doWrite queue is empty");
232 if (theFile
->error()) {
233 debugs(79, DBG_IMPORTANT
,HERE
<< "avoid write on theFile with error");
234 debugs(79,3,HERE
<< "calling free_func for " << (void*) q
->buf
);
237 * Note "free_func" is memNodeWriteComplete(), which doesn't
238 * really free the memory. Instead it clears the node's
239 * write_pending flag.
241 q
->free_func((void*)q
->buf
);
248 * UFSStoreState has a 'writing' flag that we used to set here,
249 * but it wasn't really used anywhere. In fact, some lower
250 * layers such as DISKD allow multiple outstanding writes, which
251 * makes the boolean writing flag meaningless. We would need
252 * a counter to keep track of writes going out and write callbacks
253 * coming in. For now let's just not use the writing flag at
256 debugs(79, 3, HERE
<< this << " calling theFile->write(" << q
->size
<< ")");
258 theFile
->write(new WriteRequest(q
->buf
, q
->offset
, q
->size
, q
->free_func
));
263 Fs::Ufs::UFSStoreState::readCompleted(const char *buf
, int len
, int errflag
, RefCount
<ReadRequest
> result
)
265 assert (result
.getRaw());
267 debugs(79, 3, "UFSStoreState::readCompleted: dirno " << swap_dirn
<<
268 ", fileno "<< std::setfill('0') << std::hex
<< std::setw(8) <<
269 swap_filen
<< " len "<< std::setfill(' ') << std::dec
<< len
);
274 STRCB
*callback_
= read
.callback
;
278 read
.callback
= NULL
;
283 * diskd IO queues closes via the diskd queue. So close callbacks
284 * occur strictly after reads and writes.
285 * ufs doesn't queue, it simply completes, so close callbacks occur
286 * strictly after reads and writes.
287 * aufs performs closes syncronously, so close events must be managed
288 * to force strict ordering.
289 * The below does this:
290 * closing is set when theFile->close() has been called, and close only triggers
291 * when no io's are pending.
292 * writeCompleted likewise.
294 if (!closing
&& cbdataReferenceValidDone(read
.callback_data
, &cbdata
)) {
295 if (len
> 0 && read_buf
!= buf
)
296 memcpy(read_buf
, buf
, len
);
298 callback_(cbdata
, read_buf
, len
, this);
301 if (flags
.try_closing
|| (theFile
!= NULL
&& theFile
->error()) )
306 Fs::Ufs::UFSStoreState::writeCompleted(int errflag
, size_t len
, RefCount
<WriteRequest
> writeRequest
)
308 debugs(79, 3, HERE
<< "dirno " << swap_dirn
<< ", fileno " <<
309 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << swap_filen
<<
313 * See doWrites() for why we don't update UFSStoreState::writing
319 if (theFile
->error()) {
320 debugs(79,2,HERE
<< " detected an error, will try to close");
326 * Kick any pending write/close operations alive
332 Fs::Ufs::UFSStoreState::doCloseCallback(int errflag
)
334 debugs(79, 3, "storeUfsIOCallback: errflag=" << errflag
);
337 * When we signal the higher layer with this callback, it might unlock
338 * the StoreEntry and its associated data. We must "free" any queued
339 * I/Os (especially writes) now, otherwise the StoreEntry's mem_node's
340 * will have their write_pending flag set, and we'll get an assertion.
343 STIOCB
*theCallback
= callback
;
348 if (cbdataReferenceValidDone(callback_data
, &cbdata
) && theCallback
)
349 theCallback(cbdata
, errflag
, this);
352 * We are finished with theFile since the lower layer signalled
353 * us that the file has been closed. This must be the last line,
354 * as theFile may be the only object holding us in memory.
356 theFile
= NULL
; // refcounted
359 /* ============= THE REAL UFS CODE ================ */
361 Fs::Ufs::UFSStoreState::UFSStoreState(SwapDir
* SD
, StoreEntry
* anEntry
, STIOCB
* callback_
, void *callback_data_
) : opening (false), creating (false), closing (false), reading(false), writing(false), pending_reads(NULL
), pending_writes (NULL
)
363 swap_filen
= anEntry
->swap_filen
;
364 swap_dirn
= SD
->index
;
366 callback
= callback_
;
367 callback_data
= cbdataReference(callback_data_
);
369 flags
.write_draining
= false;
370 flags
.try_closing
= false;
373 Fs::Ufs::UFSStoreState::~UFSStoreState()
375 assert(pending_reads
== NULL
);
376 assert(pending_writes
== NULL
);
380 Fs::Ufs::UFSStoreState::freePending()
384 while ((qr
= (_queued_read
*)linklistShift(&pending_reads
))) {
385 cbdataReferenceDone(qr
->callback_data
);
389 debugs(79,3,HERE
<< "UFSStoreState::freePending: freed pending reads");
393 while ((qw
= (_queued_write
*)linklistShift(&pending_writes
))) {
395 qw
->free_func(const_cast<char *>(qw
->buf
));
399 debugs(79,3,HERE
<< "UFSStoreState::freePending: freed pending writes");
403 Fs::Ufs::UFSStoreState::kickReadQueue()
405 _queued_read
*q
= (_queued_read
*)linklistShift(&pending_reads
);
410 debugs(79, 3, "UFSStoreState::kickReadQueue: reading queued request of " << q
->size
<< " bytes");
414 if (cbdataReferenceValidDone(q
->callback_data
, &cbdata
)) {
415 read_(q
->buf
, q
->size
, q
->offset
, q
->callback
, cbdata
);
417 debugs(79, 2, "UFSStoreState::kickReadQueue: this: " << this << " cbdataReferenceValidDone returned false." << " closing: " << closing
<< " flags.try_closing: " << flags
.try_closing
);
428 Fs::Ufs::UFSStoreState::queueRead(char *buf
, size_t size
, off_t aOffset
, STRCB
*callback_
, void *callback_data_
)
430 debugs(79, 3, "UFSStoreState::queueRead: queueing read");
432 assert (pending_reads
== NULL
);
433 _queued_read
*q
= new _queued_read
;
437 q
->callback
= callback_
;
438 q
->callback_data
= cbdataReference(callback_data_
);
439 linklistPush(&pending_reads
, q
);
444 * drainWriteQueue() is a loop around doWrite().
447 Fs::Ufs::UFSStoreState::drainWriteQueue()
451 * We might find that flags.write_draining is already set
452 * because schemes like diskd can process I/O acks
453 * before sending another I/O request. e.g. the following
454 * sequence of events: open request -> write request ->
455 * drainWriteQueue() -> queue full -> callbacks -> openDone() ->
458 if (flags
.write_draining
)
461 if (!theFile
->canWrite())
464 flags
.write_draining
= true;
466 while (pending_writes
!= NULL
) {
470 flags
.write_draining
= false;
472 if (flags
.try_closing
)
478 * This blows. DiskThreadsDiskFile::close() won't actually do the close
479 * if ioInProgress() is true. So we have to check it here. Maybe someday
480 * DiskThreadsDiskFile::close() will be modified to have a return value,
481 * or will remember to do the close for us.
484 Fs::Ufs::UFSStoreState::tryClosing()
486 debugs(79,3,HERE
<< this << " tryClosing()" <<
487 " closing = " << closing
<<
488 " flags.try_closing = " << flags
.try_closing
<<
489 " ioInProgress = " << theFile
->ioInProgress());
491 if (theFile
->ioInProgress()) {
492 debugs(79, 3, HERE
<< this <<
493 " won't close since ioInProgress is true, bailing");
494 flags
.try_closing
= true;
499 flags
.try_closing
= false;
504 Fs::Ufs::UFSStoreState::queueWrite(char const *buf
, size_t size
, off_t aOffset
, FREE
* free_func
)
506 debugs(79, 3, HERE
<< this << " UFSStoreState::queueWrite: queueing write of size " << size
);
509 q
= new _queued_write
;
513 q
->free_func
= free_func
;
514 linklistPush(&pending_writes
, q
);