]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/ufs/UFSStoreState.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / fs / ufs / UFSStoreState.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 79 Storage Manager UFS Interface
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
19 *
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.
24 *
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.
29 *
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.
33 *
34 */
35
36 #include "squid.h"
37 #include "Store.h"
38 #include "Generic.h"
39 #include "DiskIO/DiskFile.h"
40 #include "DiskIO/DiskIOStrategy.h"
41 #include "DiskIO/ReadRequest.h"
42 #include "DiskIO/WriteRequest.h"
43 #include "protos.h"
44 #include "SwapDir.h"
45 #include "UFSStrategy.h"
46 #include "UFSStoreState.h"
47
48 CBDATA_NAMESPACED_CLASS_INIT(Fs::Ufs,UFSStoreState);
49
50 void *
51 Fs::Ufs::UFSStoreState::operator new (size_t)
52 {
53 CBDATA_INIT_TYPE(UFSStoreState);
54 return cbdataAlloc(UFSStoreState);
55 }
56
57 void
58 Fs::Ufs::UFSStoreState::operator delete (void *address)
59 {
60 cbdataFree(address);
61 }
62
63 void
64 Fs::Ufs::UFSStoreState::ioCompletedNotification()
65 {
66 if (opening) {
67 opening = false;
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());
72
73 assert (FILE_MODE(mode) == O_RDONLY);
74 openDone();
75
76 return;
77 }
78
79 if (creating) {
80 creating = false;
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());
85
86 openDone();
87
88 return;
89 }
90
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());
95
96 /* Ok, notification past open means an error has occured */
97 assert (theFile->error());
98 tryClosing();
99 }
100
101 void
102 Fs::Ufs::UFSStoreState::openDone()
103 {
104 if (closing)
105 debugs(0, DBG_CRITICAL, HERE << "already closing in openDone()!?");
106
107 if (theFile->error()) {
108 tryClosing();
109 return;
110 }
111
112 if (FILE_MODE(mode) == O_WRONLY) {
113 drainWriteQueue();
114
115 } else if ((FILE_MODE(mode) == O_RDONLY) && !closing) {
116 if (kickReadQueue())
117 return;
118 }
119
120 if (flags.try_closing)
121 tryClosing();
122
123 debugs(79, 3, "UFSStoreState::openDone: exiting");
124 }
125
126 void
127 Fs::Ufs::UFSStoreState::closeCompleted()
128 {
129 assert (closing);
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 <<
133 theFile->error());
134
135 if (theFile->error()) {
136 debugs(79,3,HERE<< "theFile->error() ret " << theFile->error());
137 doCloseCallback(DISK_ERROR);
138 } else {
139 doCloseCallback(DISK_OK);
140 }
141
142 closing = false;
143 }
144
145 /*
146 * DPW 2006-05-24
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.
153 */
154 void
155 Fs::Ufs::UFSStoreState::close(int)
156 {
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
160 }
161
162 void
163 Fs::Ufs::UFSStoreState::read_(char *buf, size_t size, off_t aOffset, STRCB * aCallback, void *aCallbackData)
164 {
165 assert(read.callback == NULL);
166 assert(read.callback_data == NULL);
167 assert(!reading);
168 assert(!closing);
169 assert (aCallback);
170
171 if (!theFile->canRead()) {
172 debugs(79, 3, "UFSStoreState::read_: queueing read because theFile can't read");
173 queueRead (buf, size, aOffset, aCallback, aCallbackData);
174 return;
175 }
176
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);
181 offset_ = aOffset;
182 read_buf = buf;
183 reading = true;
184 theFile->read(new ReadRequest(buf,aOffset,size));
185 }
186
187 /*
188 * DPW 2006-05-24
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.
194 */
195 void
196 Fs::Ufs::UFSStoreState::write(char const *buf, size_t size, off_t aOffset, FREE * free_func)
197 {
198 debugs(79, 3, "UFSStoreState::write: dirn " << swap_dirn << ", fileno "<<
199 std::setfill('0') << std::hex << std::uppercase << std::setw(8) << swap_filen);
200
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);
205 return;
206 }
207
208 queueWrite(buf, size, aOffset, free_func);
209 drainWriteQueue();
210 }
211
212 /*
213 * DPW 2006-05-24
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().
217 */
218 void
219 Fs::Ufs::UFSStoreState::doWrite()
220 {
221 debugs(79, 3, HERE << this << " UFSStoreState::doWrite");
222
223 assert(theFile->canWrite());
224
225 _queued_write *q = (_queued_write *)linklistShift(&pending_writes);
226
227 if (q == NULL) {
228 debugs(79, 3, HERE << this << " UFSStoreState::doWrite queue is empty");
229 return;
230 }
231
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);
235 /*
236 * DPW 2006-05-24
237 * Note "free_func" is memNodeWriteComplete(), which doesn't
238 * really free the memory. Instead it clears the node's
239 * write_pending flag.
240 */
241 q->free_func((void*)q->buf);
242 delete q;
243 return;
244 }
245
246 /*
247 * DPW 2006-05-24
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
254 * all.
255 */
256 debugs(79, 3, HERE << this << " calling theFile->write(" << q->size << ")");
257
258 theFile->write(new WriteRequest(q->buf, q->offset, q->size, q->free_func));
259 delete q;
260 }
261
262 void
263 Fs::Ufs::UFSStoreState::readCompleted(const char *buf, int len, int errflag, RefCount<ReadRequest> result)
264 {
265 assert (result.getRaw());
266 reading = false;
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);
270
271 if (len > 0)
272 offset_ += len;
273
274 STRCB *callback_ = read.callback;
275
276 assert(callback_);
277
278 read.callback = NULL;
279
280 void *cbdata;
281
282 /* A note:
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.
293 */
294 if (!closing && cbdataReferenceValidDone(read.callback_data, &cbdata)) {
295 if (len > 0 && read_buf != buf)
296 memcpy(read_buf, buf, len);
297
298 callback_(cbdata, read_buf, len, this);
299 }
300
301 if (flags.try_closing || (theFile != NULL && theFile->error()) )
302 tryClosing();
303 }
304
305 void
306 Fs::Ufs::UFSStoreState::writeCompleted(int errflag, size_t len, RefCount<WriteRequest> writeRequest)
307 {
308 debugs(79, 3, HERE << "dirno " << swap_dirn << ", fileno " <<
309 std::setfill('0') << std::hex << std::uppercase << std::setw(8) << swap_filen <<
310 ", len " << len);
311 /*
312 * DPW 2006-05-24
313 * See doWrites() for why we don't update UFSStoreState::writing
314 * here anymore.
315 */
316
317 offset_ += len;
318
319 if (theFile->error()) {
320 debugs(79,2,HERE << " detected an error, will try to close");
321 tryClosing();
322 }
323
324 /*
325 * HNO 2009-07-24
326 * Kick any pending write/close operations alive
327 */
328 drainWriteQueue();
329 }
330
331 void
332 Fs::Ufs::UFSStoreState::doCloseCallback(int errflag)
333 {
334 debugs(79, 3, "storeUfsIOCallback: errflag=" << errflag);
335 /*
336 * DPW 2006-05-24
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.
341 */
342 freePending();
343 STIOCB *theCallback = callback;
344 callback = NULL;
345
346 void *cbdata;
347
348 if (cbdataReferenceValidDone(callback_data, &cbdata) && theCallback)
349 theCallback(cbdata, errflag, this);
350
351 /*
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.
355 */
356 theFile = NULL; // refcounted
357 }
358
359 /* ============= THE REAL UFS CODE ================ */
360
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)
362 {
363 swap_filen = anEntry->swap_filen;
364 swap_dirn = SD->index;
365 mode = O_BINARY;
366 callback = callback_;
367 callback_data = cbdataReference(callback_data_);
368 e = anEntry;
369 flags.write_draining = false;
370 flags.try_closing = false;
371 }
372
373 Fs::Ufs::UFSStoreState::~UFSStoreState()
374 {
375 assert(pending_reads == NULL);
376 assert(pending_writes == NULL);
377 }
378
379 void
380 Fs::Ufs::UFSStoreState::freePending()
381 {
382 _queued_read *qr;
383
384 while ((qr = (_queued_read *)linklistShift(&pending_reads))) {
385 cbdataReferenceDone(qr->callback_data);
386 delete qr;
387 }
388
389 debugs(79,3,HERE << "UFSStoreState::freePending: freed pending reads");
390
391 _queued_write *qw;
392
393 while ((qw = (_queued_write *)linklistShift(&pending_writes))) {
394 if (qw->free_func)
395 qw->free_func(const_cast<char *>(qw->buf));
396 delete qw;
397 }
398
399 debugs(79,3,HERE << "UFSStoreState::freePending: freed pending writes");
400 }
401
402 bool
403 Fs::Ufs::UFSStoreState::kickReadQueue()
404 {
405 _queued_read *q = (_queued_read *)linklistShift(&pending_reads);
406
407 if (NULL == q)
408 return false;
409
410 debugs(79, 3, "UFSStoreState::kickReadQueue: reading queued request of " << q->size << " bytes");
411
412 void *cbdata;
413
414 if (cbdataReferenceValidDone(q->callback_data, &cbdata)) {
415 read_(q->buf, q->size, q->offset, q->callback, cbdata);
416 } else {
417 debugs(79, 2, "UFSStoreState::kickReadQueue: this: " << this << " cbdataReferenceValidDone returned false." << " closing: " << closing << " flags.try_closing: " << flags.try_closing);
418 delete q;
419 return false;
420 }
421
422 delete q;
423
424 return true;
425 }
426
427 void
428 Fs::Ufs::UFSStoreState::queueRead(char *buf, size_t size, off_t aOffset, STRCB *callback_, void *callback_data_)
429 {
430 debugs(79, 3, "UFSStoreState::queueRead: queueing read");
431 assert(opening);
432 assert (pending_reads == NULL);
433 _queued_read *q = new _queued_read;
434 q->buf = buf;
435 q->size = size;
436 q->offset = aOffset;
437 q->callback = callback_;
438 q->callback_data = cbdataReference(callback_data_);
439 linklistPush(&pending_reads, q);
440 }
441
442 /*
443 * DPW 2006-05-24
444 * drainWriteQueue() is a loop around doWrite().
445 */
446 void
447 Fs::Ufs::UFSStoreState::drainWriteQueue()
448 {
449 /*
450 * DPW 2007-04-12
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() ->
456 * drainWriteQueue().
457 */
458 if (flags.write_draining)
459 return;
460
461 if (!theFile->canWrite())
462 return;
463
464 flags.write_draining = true;
465
466 while (pending_writes != NULL) {
467 doWrite();
468 }
469
470 flags.write_draining = false;
471
472 if (flags.try_closing)
473 tryClosing();
474 }
475
476 /*
477 * DPW 2006-05-24
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.
482 */
483 void
484 Fs::Ufs::UFSStoreState::tryClosing()
485 {
486 debugs(79,3,HERE << this << " tryClosing()" <<
487 " closing = " << closing <<
488 " flags.try_closing = " << flags.try_closing <<
489 " ioInProgress = " << theFile->ioInProgress());
490
491 if (theFile->ioInProgress()) {
492 debugs(79, 3, HERE << this <<
493 " won't close since ioInProgress is true, bailing");
494 flags.try_closing = true;
495 return;
496 }
497
498 closing = true;
499 flags.try_closing = false;
500 theFile->close();
501 }
502
503 void
504 Fs::Ufs::UFSStoreState::queueWrite(char const *buf, size_t size, off_t aOffset, FREE * free_func)
505 {
506 debugs(79, 3, HERE << this << " UFSStoreState::queueWrite: queueing write of size " << size);
507
508 _queued_write *q;
509 q = new _queued_write;
510 q->buf = buf;
511 q->size = size;
512 q->offset = aOffset;
513 q->free_func = free_func;
514 linklistPush(&pending_writes, q);
515 }
516