HAMMER VFS - Handle critical I/O errors without panicing
[dragonfly.git] / sys / vfs / hammer / hammer_io.c
CommitLineData
66325755 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
66325755
MD
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
e83ca595 34 * $DragonFly: src/sys/vfs/hammer/hammer_io.c,v 1.55 2008/09/15 17:02:49 dillon Exp $
66325755
MD
35 */
36/*
37 * IO Primitives and buffer cache management
38 *
39 * All major data-tracking structures in HAMMER contain a struct hammer_io
40 * which is used to manage their backing store. We use filesystem buffers
41 * for backing store and we leave them passively associated with their
42 * HAMMER structures.
43 *
9f5097dc 44 * If the kernel tries to destroy a passively associated buf which we cannot
66325755
MD
45 * yet let go we set B_LOCKED in the buffer and then actively released it
46 * later when we can.
47 */
48
49#include "hammer.h"
50#include <sys/fcntl.h>
51#include <sys/nlookup.h>
52#include <sys/buf.h>
53#include <sys/buf2.h>
54
10a5d1ba 55static void hammer_io_modify(hammer_io_t io, int count);
055f5ff8 56static void hammer_io_deallocate(struct buf *bp);
1b0ab2c3
MD
57#if 0
58static void hammer_io_direct_read_complete(struct bio *nbio);
59#endif
60static void hammer_io_direct_write_complete(struct bio *nbio);
43c665ae 61static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
cdb6e4e6 62static void hammer_io_set_modlist(struct hammer_io *io);
748efb59 63static void hammer_io_flush_mark(hammer_volume_t volume);
748efb59 64
055f5ff8
MD
65
66/*
10a5d1ba
MD
67 * Initialize a new, already-zero'd hammer_io structure, or reinitialize
68 * an existing hammer_io structure which may have switched to another type.
055f5ff8
MD
69 */
70void
748efb59 71hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
055f5ff8 72{
748efb59
MD
73 io->volume = volume;
74 io->hmp = volume->io.hmp;
055f5ff8 75 io->type = type;
055f5ff8
MD
76}
77
66325755 78/*
fbc6e32a 79 * Helper routine to disassociate a buffer cache buffer from an I/O
ecca949a 80 * structure. The buffer is unlocked and marked appropriate for reclamation.
055f5ff8
MD
81 *
82 * The io may have 0 or 1 references depending on who called us. The
83 * caller is responsible for dealing with the refs.
84 *
85 * This call can only be made when no action is required on the buffer.
ecca949a
MD
86 *
87 * The caller must own the buffer and the IO must indicate that the
88 * structure no longer owns it (io.released != 0).
66325755
MD
89 */
90static void
ecca949a 91hammer_io_disassociate(hammer_io_structure_t iou)
66325755 92{
055f5ff8 93 struct buf *bp = iou->io.bp;
66325755 94
ecca949a 95 KKASSERT(iou->io.released);
b58c6388 96 KKASSERT(iou->io.modified == 0);
af209b0f 97 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
4d75d829 98 buf_dep_init(bp);
055f5ff8 99 iou->io.bp = NULL;
9f5097dc
MD
100
101 /*
102 * If the buffer was locked someone wanted to get rid of it.
103 */
a99b9ea2
MD
104 if (bp->b_flags & B_LOCKED) {
105 --hammer_count_io_locked;
9f5097dc 106 bp->b_flags &= ~B_LOCKED;
a99b9ea2 107 }
ecca949a
MD
108 if (iou->io.reclaim) {
109 bp->b_flags |= B_NOCACHE|B_RELBUF;
110 iou->io.reclaim = 0;
055f5ff8 111 }
66325755 112
055f5ff8 113 switch(iou->io.type) {
66325755 114 case HAMMER_STRUCTURE_VOLUME:
055f5ff8 115 iou->volume.ondisk = NULL;
66325755 116 break;
10a5d1ba
MD
117 case HAMMER_STRUCTURE_DATA_BUFFER:
118 case HAMMER_STRUCTURE_META_BUFFER:
119 case HAMMER_STRUCTURE_UNDO_BUFFER:
055f5ff8 120 iou->buffer.ondisk = NULL;
66325755
MD
121 break;
122 }
fbc6e32a
MD
123}
124
125/*
055f5ff8 126 * Wait for any physical IO to complete
ae8e83e6
MD
127 *
128 * XXX we aren't interlocked against a spinlock or anything so there
129 * is a small window in the interlock / io->running == 0 test.
fbc6e32a 130 */
1b0ab2c3 131void
055f5ff8 132hammer_io_wait(hammer_io_t io)
fbc6e32a 133{
055f5ff8 134 if (io->running) {
055f5ff8 135 for (;;) {
ae8e83e6
MD
136 io->waiting = 1;
137 tsleep_interlock(io, 0);
055f5ff8
MD
138 if (io->running == 0)
139 break;
ae8e83e6 140 tsleep(io, PINTERLOCKED, "hmrflw", hz);
055f5ff8
MD
141 if (io->running == 0)
142 break;
143 }
66325755
MD
144 }
145}
146
af209b0f
MD
147/*
148 * Wait for all hammer_io-initated write I/O's to complete. This is not
149 * supposed to count direct I/O's but some can leak through (for
150 * non-full-sized direct I/Os).
151 */
152void
153hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
154{
748efb59 155 hammer_io_flush_sync(hmp);
af209b0f 156 crit_enter();
f5a07a7a
MD
157 while (hmp->io_running_space)
158 tsleep(&hmp->io_running_space, 0, ident, 0);
af209b0f
MD
159 crit_exit();
160}
161
2faf0737
MD
162/*
163 * Clear a flagged error condition on a I/O buffer. The caller must hold
164 * its own ref on the buffer.
165 */
166void
167hammer_io_clear_error(struct hammer_io *io)
168{
169 if (io->ioerror) {
170 io->ioerror = 0;
171 hammer_unref(&io->lock);
172 KKASSERT(io->lock.refs > 0);
173 }
174}
175
176
2f85fa4d
MD
177#define HAMMER_MAXRA 4
178
66325755 179/*
10a5d1ba
MD
180 * Load bp for a HAMMER structure. The io must be exclusively locked by
181 * the caller.
2f85fa4d 182 *
a99b9ea2
MD
183 * This routine is mostly used on meta-data and small-data blocks. Generally
184 * speaking HAMMER assumes some locality of reference and will cluster
185 * a 64K read.
af209b0f
MD
186 *
187 * Note that clustering occurs at the device layer, not the logical layer.
188 * If the buffers do not apply to the current operation they may apply to
189 * some other.
66325755
MD
190 */
191int
2f85fa4d 192hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
66325755
MD
193{
194 struct buf *bp;
2f85fa4d 195 int error;
66325755
MD
196
197 if ((bp = io->bp) == NULL) {
f5a07a7a 198 hammer_count_io_running_read += io->bytes;
ce0138a6
MD
199 if (hammer_cluster_enable) {
200 error = cluster_read(devvp, limit,
201 io->offset, io->bytes,
202 HAMMER_CLUSTER_SIZE,
203 HAMMER_CLUSTER_BUFS, &io->bp);
204 } else {
205 error = bread(devvp, io->offset, io->bytes, &io->bp);
206 }
207 hammer_stats_disk_read += io->bytes;
f5a07a7a 208 hammer_count_io_running_read -= io->bytes;
cdb6e4e6
MD
209
210 /*
211 * The code generally assumes b_ops/b_dep has been set-up,
212 * even if we error out here.
213 */
214 bp = io->bp;
215 bp->b_ops = &hammer_bioops;
216 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
217 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
218 BUF_KERNPROC(bp);
10a5d1ba
MD
219 KKASSERT(io->modified == 0);
220 KKASSERT(io->running == 0);
221 KKASSERT(io->waiting == 0);
66325755
MD
222 io->released = 0; /* we hold an active lock on bp */
223 } else {
224 error = 0;
225 }
226 return(error);
227}
228
229/*
230 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
10a5d1ba
MD
231 * Must be called with the IO exclusively locked.
232 *
66325755 233 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
10a5d1ba
MD
234 * I/O by forcing the buffer to not be in a released state before calling
235 * it.
055f5ff8 236 *
10a5d1ba
MD
237 * This function will also mark the IO as modified but it will not
238 * increment the modify_refs count.
66325755
MD
239 */
240int
241hammer_io_new(struct vnode *devvp, struct hammer_io *io)
242{
243 struct buf *bp;
244
245 if ((bp = io->bp) == NULL) {
4a2796f3 246 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
66325755
MD
247 bp = io->bp;
248 bp->b_ops = &hammer_bioops;
af209b0f 249 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
66325755 250 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
055f5ff8 251 io->released = 0;
10a5d1ba 252 KKASSERT(io->running == 0);
055f5ff8 253 io->waiting = 0;
66325755
MD
254 BUF_KERNPROC(bp);
255 } else {
256 if (io->released) {
257 regetblk(bp);
66325755 258 BUF_KERNPROC(bp);
d113fda1 259 io->released = 0;
66325755
MD
260 }
261 }
10a5d1ba 262 hammer_io_modify(io, 0);
66325755
MD
263 vfs_bio_clrbuf(bp);
264 return(0);
265}
266
0e8bd897
MD
267/*
268 * Advance the activity count on the underlying buffer because
269 * HAMMER does not getblk/brelse on every access.
270 */
271void
272hammer_io_advance(struct hammer_io *io)
273{
274 if (io->bp)
275 buf_act_advance(io->bp);
276}
277
47637bff
MD
278/*
279 * Remove potential device level aliases against buffers managed by high level
362ec2dc
MD
280 * vnodes. Aliases can also be created due to mixed buffer sizes or via
281 * direct access to the backing store device.
e469566b
MD
282 *
283 * This is nasty because the buffers are also VMIO-backed. Even if a buffer
284 * does not exist its backing VM pages might, and we have to invalidate
285 * those as well or a getblk() will reinstate them.
362ec2dc
MD
286 *
287 * Buffer cache buffers associated with hammer_buffers cannot be
288 * invalidated.
47637bff 289 */
362ec2dc 290int
47637bff
MD
291hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
292{
cebe9493 293 hammer_io_structure_t iou;
47637bff
MD
294 hammer_off_t phys_offset;
295 struct buf *bp;
362ec2dc 296 int error;
47637bff
MD
297
298 phys_offset = volume->ondisk->vol_buf_beg +
299 (zone2_offset & HAMMER_OFF_SHORT_MASK);
4a2796f3 300 crit_enter();
b1c20cfa 301 if ((bp = findblk(volume->devvp, phys_offset, FINDBLK_TEST)) != NULL)
4a2796f3 302 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
e469566b
MD
303 else
304 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
305 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
362ec2dc 306#if 0
5c8d05e2 307 hammer_ref(&iou->io.lock);
e469566b
MD
308 hammer_io_clear_modify(&iou->io, 1);
309 bundirty(bp);
e83ca595
MD
310 iou->io.released = 0;
311 BUF_KERNPROC(bp);
e469566b 312 iou->io.reclaim = 1;
5c8d05e2 313 iou->io.waitdep = 1;
e83ca595 314 KKASSERT(iou->io.lock.refs == 1);
5c8d05e2
MD
315 hammer_rel_buffer(&iou->buffer, 0);
316 /*hammer_io_deallocate(bp);*/
362ec2dc 317#endif
04b04ca6 318 bqrelse(bp);
362ec2dc 319 error = EAGAIN;
e469566b
MD
320 } else {
321 KKASSERT((bp->b_flags & B_LOCKED) == 0);
322 bundirty(bp);
323 bp->b_flags |= B_NOCACHE|B_RELBUF;
e83ca595 324 brelse(bp);
362ec2dc 325 error = 0;
47637bff 326 }
4a2796f3 327 crit_exit();
362ec2dc 328 return(error);
47637bff
MD
329}
330
fbc6e32a 331/*
b3deaf57 332 * This routine is called on the last reference to a hammer structure.
ecca949a 333 * The io is usually interlocked with io.loading and io.refs must be 1.
b3deaf57 334 *
ecca949a
MD
335 * This routine may return a non-NULL bp to the caller for dispoal. Disposal
336 * simply means the caller finishes decrementing the ref-count on the
337 * IO structure then brelse()'s the bp. The bp may or may not still be
338 * passively associated with the IO.
339 *
340 * The only requirement here is that modified meta-data and volume-header
341 * buffer may NOT be disassociated from the IO structure, and consequently
342 * we also leave such buffers actively associated with the IO if they already
343 * are (since the kernel can't do anything with them anyway). Only the
344 * flusher is allowed to write such buffers out. Modified pure-data and
345 * undo buffers are returned to the kernel but left passively associated
346 * so we can track when the kernel writes the bp out.
66325755 347 */
ecca949a 348struct buf *
09ac686b 349hammer_io_release(struct hammer_io *io, int flush)
66325755 350{
9f5097dc 351 union hammer_io_structure *iou = (void *)io;
66325755
MD
352 struct buf *bp;
353
055f5ff8 354 if ((bp = io->bp) == NULL)
ecca949a 355 return(NULL);
fbc6e32a 356
055f5ff8 357 /*
10a5d1ba
MD
358 * Try to flush a dirty IO to disk if asked to by the
359 * caller or if the kernel tried to flush the buffer in the past.
055f5ff8 360 *
10a5d1ba
MD
361 * Kernel-initiated flushes are only allowed for pure-data buffers.
362 * meta-data and volume buffers can only be flushed explicitly
363 * by HAMMER.
055f5ff8 364 */
10a5d1ba 365 if (io->modified) {
09ac686b 366 if (flush) {
710733a6 367 hammer_io_flush(io, 0);
10a5d1ba
MD
368 } else if (bp->b_flags & B_LOCKED) {
369 switch(io->type) {
370 case HAMMER_STRUCTURE_DATA_BUFFER:
710733a6
MD
371 hammer_io_flush(io, 0);
372 break;
10a5d1ba 373 case HAMMER_STRUCTURE_UNDO_BUFFER:
710733a6 374 hammer_io_flush(io, hammer_undo_reclaim(io));
10a5d1ba
MD
375 break;
376 default:
377 break;
378 }
379 } /* else no explicit request to flush the buffer */
380 }
fbc6e32a 381
055f5ff8 382 /*
5c8d05e2
MD
383 * Wait for the IO to complete if asked to. This occurs when
384 * the buffer must be disposed of definitively during an umount
385 * or buffer invalidation.
055f5ff8 386 */
b58c6388 387 if (io->waitdep && io->running) {
055f5ff8
MD
388 hammer_io_wait(io);
389 }
390
391 /*
10a5d1ba
MD
392 * Return control of the buffer to the kernel (with the provisio
393 * that our bioops can override kernel decisions with regards to
394 * the buffer).
055f5ff8 395 */
cebe9493 396 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
10a5d1ba
MD
397 /*
398 * Always disassociate the bp if an explicit flush
399 * was requested and the IO completed with no error
400 * (so unmount can really clean up the structure).
401 */
055f5ff8 402 if (io->released) {
b3deaf57 403 regetblk(bp);
46fe7ae1 404 BUF_KERNPROC(bp);
ecca949a
MD
405 } else {
406 io->released = 1;
055f5ff8 407 }
ecca949a
MD
408 hammer_io_disassociate((hammer_io_structure_t)io);
409 /* return the bp */
055f5ff8 410 } else if (io->modified) {
10a5d1ba 411 /*
ecca949a
MD
412 * Only certain IO types can be released to the kernel if
413 * the buffer has been modified.
414 *
415 * volume and meta-data IO types may only be explicitly
416 * flushed by HAMMER.
10a5d1ba
MD
417 */
418 switch(io->type) {
419 case HAMMER_STRUCTURE_DATA_BUFFER:
420 case HAMMER_STRUCTURE_UNDO_BUFFER:
421 if (io->released == 0) {
422 io->released = 1;
423 bdwrite(bp);
424 }
425 break;
426 default:
427 break;
055f5ff8 428 }
ecca949a 429 bp = NULL; /* bp left associated */
055f5ff8 430 } else if (io->released == 0) {
10a5d1ba
MD
431 /*
432 * Clean buffers can be generally released to the kernel.
433 * We leave the bp passively associated with the HAMMER
434 * structure and use bioops to disconnect it later on
435 * if the kernel wants to discard the buffer.
ecca949a
MD
436 *
437 * We can steal the structure's ownership of the bp.
10a5d1ba 438 */
ecca949a 439 io->released = 1;
9f5097dc 440 if (bp->b_flags & B_LOCKED) {
ecca949a
MD
441 hammer_io_disassociate(iou);
442 /* return the bp */
9f5097dc 443 } else {
cebe9493 444 if (io->reclaim) {
ecca949a
MD
445 hammer_io_disassociate(iou);
446 /* return the bp */
cebe9493 447 } else {
ecca949a 448 /* return the bp (bp passively associated) */
cebe9493 449 }
9f5097dc 450 }
19b97e01
MD
451 } else {
452 /*
af209b0f
MD
453 * A released buffer is passively associate with our
454 * hammer_io structure. The kernel cannot destroy it
455 * without making a bioops call. If the kernel (B_LOCKED)
456 * or we (reclaim) requested that the buffer be destroyed
457 * we destroy it, otherwise we do a quick get/release to
458 * reset its position in the kernel's LRU list.
459 *
460 * Leaving the buffer passively associated allows us to
461 * use the kernel's LRU buffer flushing mechanisms rather
462 * then rolling our own.
cb51be26
MD
463 *
464 * XXX there are two ways of doing this. We can re-acquire
465 * and passively release to reset the LRU, or not.
19b97e01 466 */
af209b0f 467 if (io->running == 0) {
19b97e01 468 regetblk(bp);
cebe9493 469 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
ecca949a
MD
470 hammer_io_disassociate(iou);
471 /* return the bp */
9f5097dc 472 } else {
ecca949a 473 /* return the bp (bp passively associated) */
9f5097dc 474 }
ecca949a
MD
475 } else {
476 /*
477 * bp is left passively associated but we do not
478 * try to reacquire it. Interactions with the io
479 * structure will occur on completion of the bp's
480 * I/O.
481 */
482 bp = NULL;
19b97e01 483 }
66325755 484 }
ecca949a 485 return(bp);
66325755
MD
486}
487
fbc6e32a 488/*
b33e2cc0
MD
489 * This routine is called with a locked IO when a flush is desired and
490 * no other references to the structure exists other then ours. This
491 * routine is ONLY called when HAMMER believes it is safe to flush a
492 * potentially modified buffer out.
fbc6e32a
MD
493 */
494void
710733a6 495hammer_io_flush(struct hammer_io *io, int reclaim)
fbc6e32a
MD
496{
497 struct buf *bp;
fbc6e32a 498
055f5ff8 499 /*
10a5d1ba 500 * Degenerate case - nothing to flush if nothing is dirty.
055f5ff8 501 */
b58c6388 502 if (io->modified == 0) {
055f5ff8 503 return;
b58c6388 504 }
055f5ff8
MD
505
506 KKASSERT(io->bp);
9f5097dc 507 KKASSERT(io->modify_refs <= 0);
055f5ff8 508
77062c8a
MD
509 /*
510 * Acquire ownership of the bp, particularly before we clear our
511 * modified flag.
512 *
513 * We are going to bawrite() this bp. Don't leave a window where
514 * io->released is set, we actually own the bp rather then our
515 * buffer.
516 */
517 bp = io->bp;
518 if (io->released) {
519 regetblk(bp);
520 /* BUF_KERNPROC(io->bp); */
521 /* io->released = 0; */
522 KKASSERT(io->released);
523 KKASSERT(io->bp == bp);
524 }
525 io->released = 1;
526
710733a6
MD
527 if (reclaim) {
528 io->reclaim = 1;
529 if ((bp->b_flags & B_LOCKED) == 0) {
530 bp->b_flags |= B_LOCKED;
531 ++hammer_count_io_locked;
532 }
533 }
534
b33e2cc0 535 /*
10a5d1ba
MD
536 * Acquire exclusive access to the bp and then clear the modified
537 * state of the buffer prior to issuing I/O to interlock any
538 * modifications made while the I/O is in progress. This shouldn't
539 * happen anyway but losing data would be worse. The modified bit
540 * will be rechecked after the IO completes.
541 *
4a2796f3
MD
542 * NOTE: This call also finalizes the buffer's content (inval == 0).
543 *
b33e2cc0
MD
544 * This is only legal when lock.refs == 1 (otherwise we might clear
545 * the modified bit while there are still users of the cluster
546 * modifying the data).
547 *
b33e2cc0
MD
548 * Do this before potentially blocking so any attempt to modify the
549 * ondisk while we are blocked blocks waiting for us.
550 */
5c8d05e2 551 hammer_ref(&io->lock);
4a2796f3 552 hammer_io_clear_modify(io, 0);
5c8d05e2 553 hammer_unref(&io->lock);
bcac4bbb 554
10a5d1ba
MD
555 /*
556 * Transfer ownership to the kernel and initiate I/O.
557 */
055f5ff8 558 io->running = 1;
f5a07a7a
MD
559 io->hmp->io_running_space += io->bytes;
560 hammer_count_io_running_write += io->bytes;
055f5ff8 561 bawrite(bp);
748efb59 562 hammer_io_flush_mark(io->volume);
fbc6e32a
MD
563}
564
055f5ff8
MD
565/************************************************************************
566 * BUFFER DIRTYING *
567 ************************************************************************
568 *
569 * These routines deal with dependancies created when IO buffers get
570 * modified. The caller must call hammer_modify_*() on a referenced
571 * HAMMER structure prior to modifying its on-disk data.
0b075555 572 *
055f5ff8
MD
573 * Any intent to modify an IO buffer acquires the related bp and imposes
574 * various write ordering dependancies.
0b075555 575 */
055f5ff8
MD
576
577/*
10a5d1ba
MD
578 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
579 * are locked until the flusher can deal with them, pure data buffers
580 * can be written out.
055f5ff8 581 */
10a5d1ba 582static
b58c6388 583void
10a5d1ba 584hammer_io_modify(hammer_io_t io, int count)
0b075555 585{
9f5097dc
MD
586 /*
587 * io->modify_refs must be >= 0
588 */
589 while (io->modify_refs < 0) {
590 io->waitmod = 1;
591 tsleep(io, 0, "hmrmod", 0);
592 }
593
46fe7ae1
MD
594 /*
595 * Shortcut if nothing to do.
596 */
0b075555 597 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
10a5d1ba 598 io->modify_refs += count;
b58c6388
MD
599 if (io->modified && io->released == 0)
600 return;
46fe7ae1
MD
601
602 hammer_lock_ex(&io->lock);
10a5d1ba 603 if (io->modified == 0) {
cdb6e4e6 604 hammer_io_set_modlist(io);
10a5d1ba
MD
605 io->modified = 1;
606 }
46fe7ae1
MD
607 if (io->released) {
608 regetblk(io->bp);
609 BUF_KERNPROC(io->bp);
610 io->released = 0;
611 KKASSERT(io->modified != 0);
612 }
46fe7ae1 613 hammer_unlock(&io->lock);
055f5ff8
MD
614}
615
10a5d1ba
MD
616static __inline
617void
618hammer_io_modify_done(hammer_io_t io)
619{
620 KKASSERT(io->modify_refs > 0);
621 --io->modify_refs;
9f5097dc
MD
622 if (io->modify_refs == 0 && io->waitmod) {
623 io->waitmod = 0;
624 wakeup(io);
625 }
626}
627
628void
629hammer_io_write_interlock(hammer_io_t io)
630{
631 while (io->modify_refs != 0) {
632 io->waitmod = 1;
633 tsleep(io, 0, "hmrmod", 0);
634 }
635 io->modify_refs = -1;
636}
637
638void
639hammer_io_done_interlock(hammer_io_t io)
640{
641 KKASSERT(io->modify_refs == -1);
642 io->modify_refs = 0;
643 if (io->waitmod) {
644 io->waitmod = 0;
645 wakeup(io);
646 }
10a5d1ba
MD
647}
648
2f85fa4d
MD
649/*
650 * Caller intends to modify a volume's ondisk structure.
651 *
652 * This is only allowed if we are the flusher or we have a ref on the
653 * sync_lock.
654 */
055f5ff8 655void
36f82b23
MD
656hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
657 void *base, int len)
055f5ff8 658{
2f85fa4d 659 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
055f5ff8 660
2f85fa4d 661 hammer_io_modify(&volume->io, 1);
47197d71
MD
662 if (len) {
663 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
664 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
02428fb6 665 hammer_generate_undo(trans,
47197d71
MD
666 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
667 base, len);
668 }
0b075555
MD
669}
670
055f5ff8 671/*
2f85fa4d
MD
672 * Caller intends to modify a buffer's ondisk structure.
673 *
674 * This is only allowed if we are the flusher or we have a ref on the
675 * sync_lock.
055f5ff8 676 */
0b075555 677void
36f82b23
MD
678hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
679 void *base, int len)
46fe7ae1 680{
2f85fa4d
MD
681 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
682
10a5d1ba 683 hammer_io_modify(&buffer->io, 1);
47197d71
MD
684 if (len) {
685 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
686 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
02428fb6 687 hammer_generate_undo(trans,
34d829f7 688 buffer->zone2_offset + rel_offset,
47197d71
MD
689 base, len);
690 }
46fe7ae1
MD
691}
692
10a5d1ba
MD
693void
694hammer_modify_volume_done(hammer_volume_t volume)
695{
696 hammer_io_modify_done(&volume->io);
697}
698
699void
700hammer_modify_buffer_done(hammer_buffer_t buffer)
701{
702 hammer_io_modify_done(&buffer->io);
703}
704
61aeeb33 705/*
4a2796f3
MD
706 * Mark an entity as not being dirty any more and finalize any
707 * delayed adjustments to the buffer.
708 *
709 * Delayed adjustments are an important performance enhancement, allowing
710 * us to avoid recalculating B-Tree node CRCs over and over again when
711 * making bulk-modifications to the B-Tree.
712 *
713 * If inval is non-zero delayed adjustments are ignored.
5c8d05e2
MD
714 *
715 * This routine may dereference related btree nodes and cause the
716 * buffer to be dereferenced. The caller must own a reference on io.
61aeeb33
MD
717 */
718void
4a2796f3 719hammer_io_clear_modify(struct hammer_io *io, int inval)
61aeeb33 720{
4a2796f3
MD
721 if (io->modified == 0)
722 return;
723
724 /*
725 * Take us off the mod-list and clear the modified bit.
726 */
727 KKASSERT(io->mod_list != NULL);
728 if (io->mod_list == &io->hmp->volu_list ||
729 io->mod_list == &io->hmp->meta_list) {
f5a07a7a
MD
730 io->hmp->locked_dirty_space -= io->bytes;
731 hammer_count_dirtybufspace -= io->bytes;
4a2796f3
MD
732 }
733 TAILQ_REMOVE(io->mod_list, io, mod_entry);
734 io->mod_list = NULL;
735 io->modified = 0;
736
737 /*
738 * If this bit is not set there are no delayed adjustments.
739 */
740 if (io->gencrc == 0)
741 return;
742 io->gencrc = 0;
743
744 /*
745 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
746 * on the node (& underlying buffer). Release the node after clearing
747 * the flag.
748 */
749 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
750 hammer_buffer_t buffer = (void *)io;
751 hammer_node_t node;
752
753restart:
754 TAILQ_FOREACH(node, &buffer->clist, entry) {
755 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
756 continue;
757 node->flags &= ~HAMMER_NODE_NEEDSCRC;
758 KKASSERT(node->ondisk);
759 if (inval == 0)
760 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
761 hammer_rel_node(node);
762 goto restart;
61aeeb33 763 }
cebe9493 764 }
5c8d05e2
MD
765 /* caller must still have ref on io */
766 KKASSERT(io->lock.refs > 0);
cebe9493
MD
767}
768
769/*
770 * Clear the IO's modify list. Even though the IO is no longer modified
771 * it may still be on the lose_list. This routine is called just before
772 * the governing hammer_buffer is destroyed.
773 */
774void
775hammer_io_clear_modlist(struct hammer_io *io)
776{
4a2796f3 777 KKASSERT(io->modified == 0);
cebe9493 778 if (io->mod_list) {
a99b9ea2 779 crit_enter(); /* biodone race against list */
cebe9493
MD
780 KKASSERT(io->mod_list == &io->hmp->lose_list);
781 TAILQ_REMOVE(io->mod_list, io, mod_entry);
782 io->mod_list = NULL;
a99b9ea2 783 crit_exit();
61aeeb33
MD
784 }
785}
786
cdb6e4e6
MD
787static void
788hammer_io_set_modlist(struct hammer_io *io)
789{
790 struct hammer_mount *hmp = io->hmp;
791
792 KKASSERT(io->mod_list == NULL);
793
794 switch(io->type) {
795 case HAMMER_STRUCTURE_VOLUME:
796 io->mod_list = &hmp->volu_list;
797 hmp->locked_dirty_space += io->bytes;
798 hammer_count_dirtybufspace += io->bytes;
799 break;
800 case HAMMER_STRUCTURE_META_BUFFER:
801 io->mod_list = &hmp->meta_list;
802 hmp->locked_dirty_space += io->bytes;
803 hammer_count_dirtybufspace += io->bytes;
804 break;
805 case HAMMER_STRUCTURE_UNDO_BUFFER:
806 io->mod_list = &hmp->undo_list;
807 break;
808 case HAMMER_STRUCTURE_DATA_BUFFER:
809 io->mod_list = &hmp->data_list;
810 break;
811 }
812 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
813}
814
055f5ff8
MD
815/************************************************************************
816 * HAMMER_BIOOPS *
817 ************************************************************************
818 *
66325755
MD
819 */
820
821/*
055f5ff8 822 * Pre-IO initiation kernel callback - cluster build only
66325755 823 */
66325755
MD
824static void
825hammer_io_start(struct buf *bp)
826{
827}
828
055f5ff8 829/*
7bc5b8c2 830 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
b33e2cc0
MD
831 *
832 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
833 * may also be set if we were marking a cluster header open. Only remove
834 * our dependancy if the modified bit is clear.
055f5ff8 835 */
66325755
MD
836static void
837hammer_io_complete(struct buf *bp)
838{
055f5ff8
MD
839 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
840
841 KKASSERT(iou->io.released == 1);
fbc6e32a 842
bf3b416b
MD
843 /*
844 * Deal with people waiting for I/O to drain
845 */
f90dde4c 846 if (iou->io.running) {
cdb6e4e6
MD
847 /*
848 * Deal with critical write errors. Once a critical error
849 * has been flagged in hmp the UNDO FIFO will not be updated.
850 * That way crash recover will give us a consistent
851 * filesystem.
852 *
853 * Because of this we can throw away failed UNDO buffers. If
854 * we throw away META or DATA buffers we risk corrupting
855 * the now read-only version of the filesystem visible to
856 * the user. Clear B_ERROR so the buffer is not re-dirtied
857 * by the kernel and ref the io so it doesn't get thrown
858 * away.
859 */
860 if (bp->b_flags & B_ERROR) {
861 hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
862 "while flushing meta-data");
863 switch(iou->io.type) {
864 case HAMMER_STRUCTURE_UNDO_BUFFER:
865 break;
866 default:
867 if (iou->io.ioerror == 0) {
868 iou->io.ioerror = 1;
869 if (iou->io.lock.refs == 0)
870 ++hammer_count_refedbufs;
871 hammer_ref(&iou->io.lock);
872 }
873 break;
874 }
875 bp->b_flags &= ~B_ERROR;
876 bundirty(bp);
877#if 0
878 hammer_io_set_modlist(&iou->io);
879 iou->io.modified = 1;
880#endif
881 }
ce0138a6 882 hammer_stats_disk_write += iou->io.bytes;
f5a07a7a
MD
883 hammer_count_io_running_write -= iou->io.bytes;
884 iou->io.hmp->io_running_space -= iou->io.bytes;
885 if (iou->io.hmp->io_running_space == 0)
886 wakeup(&iou->io.hmp->io_running_space);
887 KKASSERT(iou->io.hmp->io_running_space >= 0);
f90dde4c 888 iou->io.running = 0;
ce0138a6
MD
889 } else {
890 hammer_stats_disk_read += iou->io.bytes;
f90dde4c
MD
891 }
892
055f5ff8
MD
893 if (iou->io.waiting) {
894 iou->io.waiting = 0;
895 wakeup(iou);
896 }
897
898 /*
bf3b416b
MD
899 * If B_LOCKED is set someone wanted to deallocate the bp at some
900 * point, do it now if refs has become zero.
055f5ff8
MD
901 */
902 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
b33e2cc0 903 KKASSERT(iou->io.modified == 0);
a99b9ea2 904 --hammer_count_io_locked;
d5ef456e 905 bp->b_flags &= ~B_LOCKED;
055f5ff8
MD
906 hammer_io_deallocate(bp);
907 /* structure may be dead now */
908 }
66325755
MD
909}
910
911/*
912 * Callback from kernel when it wishes to deallocate a passively
10a5d1ba
MD
913 * associated structure. This mostly occurs with clean buffers
914 * but it may be possible for a holding structure to be marked dirty
7bc5b8c2 915 * while its buffer is passively associated. The caller owns the bp.
66325755
MD
916 *
917 * If we cannot disassociate we set B_LOCKED to prevent the buffer
918 * from getting reused.
46fe7ae1
MD
919 *
920 * WARNING: Because this can be called directly by getnewbuf we cannot
921 * recurse into the tree. If a bp cannot be immediately disassociated
922 * our only recourse is to set B_LOCKED.
7bc5b8c2
MD
923 *
924 * WARNING: This may be called from an interrupt via hammer_io_complete()
66325755
MD
925 */
926static void
927hammer_io_deallocate(struct buf *bp)
928{
055f5ff8 929 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
66325755 930
055f5ff8 931 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
46fe7ae1 932 if (iou->io.lock.refs > 0 || iou->io.modified) {
10a5d1ba
MD
933 /*
934 * It is not legal to disassociate a modified buffer. This
935 * case really shouldn't ever occur.
936 */
055f5ff8 937 bp->b_flags |= B_LOCKED;
a99b9ea2 938 ++hammer_count_io_locked;
055f5ff8 939 } else {
10a5d1ba
MD
940 /*
941 * Disassociate the BP. If the io has no refs left we
942 * have to add it to the loose list.
943 */
ecca949a
MD
944 hammer_io_disassociate(iou);
945 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
946 KKASSERT(iou->io.bp == NULL);
10a5d1ba 947 KKASSERT(iou->io.mod_list == NULL);
a99b9ea2 948 crit_enter(); /* biodone race against list */
10a5d1ba
MD
949 iou->io.mod_list = &iou->io.hmp->lose_list;
950 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
a99b9ea2 951 crit_exit();
66325755 952 }
66325755 953 }
66325755
MD
954}
955
956static int
957hammer_io_fsync(struct vnode *vp)
958{
959 return(0);
960}
961
962/*
963 * NOTE: will not be called unless we tell the kernel about the
964 * bioops. Unused... we use the mount's VFS_SYNC instead.
965 */
966static int
967hammer_io_sync(struct mount *mp)
968{
969 return(0);
970}
971
972static void
973hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
974{
975}
976
977/*
978 * I/O pre-check for reading and writing. HAMMER only uses this for
979 * B_CACHE buffers so checkread just shouldn't happen, but if it does
980 * allow it.
981 *
fbc6e32a
MD
982 * Writing is a different case. We don't want the kernel to try to write
983 * out a buffer that HAMMER may be modifying passively or which has a
10a5d1ba
MD
984 * dependancy. In addition, kernel-demanded writes can only proceed for
985 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
986 * buffer types can only be explicitly written by the flusher.
fbc6e32a 987 *
10a5d1ba
MD
988 * checkwrite will only be called for bdwrite()n buffers. If we return
989 * success the kernel is guaranteed to initiate the buffer write.
66325755
MD
990 */
991static int
992hammer_io_checkread(struct buf *bp)
993{
994 return(0);
995}
996
997static int
998hammer_io_checkwrite(struct buf *bp)
999{
10a5d1ba 1000 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
66325755 1001
77062c8a
MD
1002 /*
1003 * This shouldn't happen under normal operation.
1004 */
1005 if (io->type == HAMMER_STRUCTURE_VOLUME ||
1006 io->type == HAMMER_STRUCTURE_META_BUFFER) {
1007 if (!panicstr)
1008 panic("hammer_io_checkwrite: illegal buffer");
a99b9ea2
MD
1009 if ((bp->b_flags & B_LOCKED) == 0) {
1010 bp->b_flags |= B_LOCKED;
1011 ++hammer_count_io_locked;
1012 }
77062c8a
MD
1013 return(1);
1014 }
c9b9e29d 1015
b33e2cc0 1016 /*
10a5d1ba
MD
1017 * We can only clear the modified bit if the IO is not currently
1018 * undergoing modification. Otherwise we may miss changes.
5c8d05e2
MD
1019 *
1020 * Only data and undo buffers can reach here. These buffers do
1021 * not have terminal crc functions but we temporarily reference
1022 * the IO anyway, just in case.
b33e2cc0 1023 */
5c8d05e2
MD
1024 if (io->modify_refs == 0 && io->modified) {
1025 hammer_ref(&io->lock);
4a2796f3 1026 hammer_io_clear_modify(io, 0);
5c8d05e2
MD
1027 hammer_unref(&io->lock);
1028 } else if (io->modified) {
1029 KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
1030 }
f90dde4c
MD
1031
1032 /*
1033 * The kernel is going to start the IO, set io->running.
1034 */
1035 KKASSERT(io->running == 0);
1036 io->running = 1;
f5a07a7a
MD
1037 io->hmp->io_running_space += io->bytes;
1038 hammer_count_io_running_write += io->bytes;
055f5ff8 1039 return(0);
66325755
MD
1040}
1041
66325755
MD
1042/*
1043 * Return non-zero if we wish to delay the kernel's attempt to flush
1044 * this buffer to disk.
1045 */
1046static int
1047hammer_io_countdeps(struct buf *bp, int n)
1048{
1049 return(0);
1050}
1051
1052struct bio_ops hammer_bioops = {
1053 .io_start = hammer_io_start,
1054 .io_complete = hammer_io_complete,
1055 .io_deallocate = hammer_io_deallocate,
1056 .io_fsync = hammer_io_fsync,
1057 .io_sync = hammer_io_sync,
1058 .io_movedeps = hammer_io_movedeps,
1059 .io_countdeps = hammer_io_countdeps,
1060 .io_checkread = hammer_io_checkread,
1061 .io_checkwrite = hammer_io_checkwrite,
1062};
1063
47637bff
MD
1064/************************************************************************
1065 * DIRECT IO OPS *
1066 ************************************************************************
1067 *
1068 * These functions operate directly on the buffer cache buffer associated
1069 * with a front-end vnode rather then a back-end device vnode.
1070 */
1071
1072/*
1073 * Read a buffer associated with a front-end vnode directly from the
1b0ab2c3
MD
1074 * disk media. The bio may be issued asynchronously. If leaf is non-NULL
1075 * we validate the CRC.
a99b9ea2 1076 *
1b0ab2c3
MD
1077 * We must check for the presence of a HAMMER buffer to handle the case
1078 * where the reblocker has rewritten the data (which it does via the HAMMER
1079 * buffer system, not via the high-level vnode buffer cache), but not yet
1080 * committed the buffer to the media.
47637bff
MD
1081 */
1082int
1b0ab2c3
MD
1083hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1084 hammer_btree_leaf_elm_t leaf)
47637bff 1085{
1b0ab2c3 1086 hammer_off_t buf_offset;
47637bff
MD
1087 hammer_off_t zone2_offset;
1088 hammer_volume_t volume;
1089 struct buf *bp;
1090 struct bio *nbio;
1091 int vol_no;
1092 int error;
1093
1b0ab2c3
MD
1094 buf_offset = bio->bio_offset;
1095 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1096 HAMMER_ZONE_LARGE_DATA);
1097
1098 /*
1099 * The buffer cache may have an aliased buffer (the reblocker can
1100 * write them). If it does we have to sync any dirty data before
1101 * we can build our direct-read. This is a non-critical code path.
1102 */
1103 bp = bio->bio_buf;
1104 hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
4a2796f3 1105
1b0ab2c3
MD
1106 /*
1107 * Resolve to a zone-2 offset. The conversion just requires
1108 * munging the top 4 bits but we want to abstract it anyway
1109 * so the blockmap code can verify the zone assignment.
1110 */
1111 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1112 if (error)
1113 goto done;
43c665ae
MD
1114 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1115 HAMMER_ZONE_RAW_BUFFER);
1116
1b0ab2c3
MD
1117 /*
1118 * Resolve volume and raw-offset for 3rd level bio. The
1119 * offset will be specific to the volume.
1120 */
43c665ae
MD
1121 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1122 volume = hammer_get_volume(hmp, vol_no, &error);
1123 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1124 error = EIO;
1125
47637bff 1126 if (error == 0) {
e469566b
MD
1127 /*
1128 * 3rd level bio
1129 */
43c665ae
MD
1130 nbio = push_bio(bio);
1131 nbio->bio_offset = volume->ondisk->vol_buf_beg +
e469566b 1132 (zone2_offset & HAMMER_OFF_SHORT_MASK);
1b0ab2c3
MD
1133#if 0
1134 /*
1135 * XXX disabled - our CRC check doesn't work if the OS
1136 * does bogus_page replacement on the direct-read.
1137 */
1138 if (leaf && hammer_verify_data) {
1139 nbio->bio_done = hammer_io_direct_read_complete;
1140 nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1141 }
1142#endif
ce0138a6 1143 hammer_stats_disk_read += bp->b_bufsize;
43c665ae 1144 vn_strategy(volume->devvp, nbio);
47637bff 1145 }
43c665ae 1146 hammer_rel_volume(volume, 0);
1b0ab2c3 1147done:
47637bff 1148 if (error) {
cebe9493 1149 kprintf("hammer_direct_read: failed @ %016llx\n",
973c11b9 1150 (long long)zone2_offset);
47637bff
MD
1151 bp->b_error = error;
1152 bp->b_flags |= B_ERROR;
1153 biodone(bio);
1154 }
1155 return(error);
1156}
1157
1b0ab2c3
MD
1158#if 0
1159/*
1160 * On completion of the BIO this callback must check the data CRC
1161 * and chain to the previous bio.
1162 */
1163static
1164void
1165hammer_io_direct_read_complete(struct bio *nbio)
1166{
1167 struct bio *obio;
1168 struct buf *bp;
1169 u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1170
1171 bp = nbio->bio_buf;
1172 if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1173 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1174 nbio->bio_offset, bp->b_bufsize);
fc73edd8
MD
1175 if (hammer_debug_critical)
1176 Debugger("data_crc on read");
1b0ab2c3
MD
1177 bp->b_flags |= B_ERROR;
1178 bp->b_error = EIO;
1179 }
1180 obio = pop_bio(nbio);
1181 biodone(obio);
1182}
1183#endif
1184
47637bff
MD
1185/*
1186 * Write a buffer associated with a front-end vnode directly to the
1187 * disk media. The bio may be issued asynchronously.
1b0ab2c3
MD
1188 *
1189 * The BIO is associated with the specified record and RECF_DIRECT_IO
e469566b 1190 * is set. The recorded is added to its object.
47637bff
MD
1191 */
1192int
1b0ab2c3 1193hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
47637bff
MD
1194 struct bio *bio)
1195{
1b0ab2c3 1196 hammer_btree_leaf_elm_t leaf = &record->leaf;
0832c9bb 1197 hammer_off_t buf_offset;
47637bff
MD
1198 hammer_off_t zone2_offset;
1199 hammer_volume_t volume;
0832c9bb 1200 hammer_buffer_t buffer;
47637bff
MD
1201 struct buf *bp;
1202 struct bio *nbio;
0832c9bb 1203 char *ptr;
47637bff
MD
1204 int vol_no;
1205 int error;
1206
0832c9bb
MD
1207 buf_offset = leaf->data_offset;
1208
1209 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
47637bff
MD
1210 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1211
0832c9bb 1212 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
4a2796f3 1213 leaf->data_len >= HAMMER_BUFSIZE) {
0832c9bb
MD
1214 /*
1215 * We are using the vnode's bio to write directly to the
1216 * media, any hammer_buffer at the same zone-X offset will
1217 * now have stale data.
1218 */
1219 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
47637bff
MD
1220 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1221 volume = hammer_get_volume(hmp, vol_no, &error);
1222
1223 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1224 error = EIO;
1225 if (error == 0) {
0832c9bb 1226 bp = bio->bio_buf;
4a2796f3 1227 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
e469566b 1228 /*
4a2796f3
MD
1229 hammer_del_buffers(hmp, buf_offset,
1230 zone2_offset, bp->b_bufsize);
e469566b 1231 */
1b0ab2c3 1232
43c665ae
MD
1233 /*
1234 * Second level bio - cached zone2 offset.
1b0ab2c3
MD
1235 *
1236 * (We can put our bio_done function in either the
1237 * 2nd or 3rd level).
43c665ae 1238 */
47637bff 1239 nbio = push_bio(bio);
43c665ae 1240 nbio->bio_offset = zone2_offset;
1b0ab2c3
MD
1241 nbio->bio_done = hammer_io_direct_write_complete;
1242 nbio->bio_caller_info1.ptr = record;
e469566b
MD
1243 record->zone2_offset = zone2_offset;
1244 record->flags |= HAMMER_RECF_DIRECT_IO |
1245 HAMMER_RECF_DIRECT_INVAL;
43c665ae
MD
1246
1247 /*
1248 * Third level bio - raw offset specific to the
1249 * correct volume.
1250 */
1251 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1252 nbio = push_bio(nbio);
47637bff 1253 nbio->bio_offset = volume->ondisk->vol_buf_beg +
0832c9bb 1254 zone2_offset;
ce0138a6 1255 hammer_stats_disk_write += bp->b_bufsize;
47637bff 1256 vn_strategy(volume->devvp, nbio);
748efb59 1257 hammer_io_flush_mark(volume);
47637bff
MD
1258 }
1259 hammer_rel_volume(volume, 0);
0832c9bb 1260 } else {
1b0ab2c3
MD
1261 /*
1262 * Must fit in a standard HAMMER buffer. In this case all
1263 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1264 * does not need to be set-up.
1265 */
0832c9bb
MD
1266 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1267 buffer = NULL;
1268 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1269 if (error == 0) {
0832c9bb 1270 bp = bio->bio_buf;
7bc5b8c2 1271 bp->b_flags |= B_AGE;
0832c9bb
MD
1272 hammer_io_modify(&buffer->io, 1);
1273 bcopy(bp->b_data, ptr, leaf->data_len);
1274 hammer_io_modify_done(&buffer->io);
7bc5b8c2 1275 hammer_rel_buffer(buffer, 0);
0832c9bb
MD
1276 bp->b_resid = 0;
1277 biodone(bio);
1278 }
47637bff 1279 }
e469566b
MD
1280 if (error == 0) {
1281 /*
1282 * The record is all setup now, add it. Potential conflics
1283 * have already been dealt with.
1284 */
1285 error = hammer_mem_add(record);
1286 KKASSERT(error == 0);
1287 } else {
1288 /*
3214ade6
MD
1289 * Major suckage occured. Also note: The record was never added
1290 * to the tree so we do not have to worry about the backend.
e469566b 1291 */
cebe9493 1292 kprintf("hammer_direct_write: failed @ %016llx\n",
973c11b9 1293 (long long)leaf->data_offset);
47637bff
MD
1294 bp = bio->bio_buf;
1295 bp->b_resid = 0;
1296 bp->b_error = EIO;
1297 bp->b_flags |= B_ERROR;
1298 biodone(bio);
e469566b
MD
1299 record->flags |= HAMMER_RECF_DELETED_FE;
1300 hammer_rel_mem_record(record);
47637bff
MD
1301 }
1302 return(error);
1303}
1304
1b0ab2c3
MD
1305/*
1306 * On completion of the BIO this callback must disconnect
1307 * it from the hammer_record and chain to the previous bio.
cdb6e4e6
MD
1308 *
1309 * An I/O error forces the mount to read-only. Data buffers
1310 * are not B_LOCKED like meta-data buffers are, so we have to
1311 * throw the buffer away to prevent the kernel from retrying.
1b0ab2c3
MD
1312 */
1313static
1314void
1315hammer_io_direct_write_complete(struct bio *nbio)
1316{
1317 struct bio *obio;
e469566b 1318 struct buf *bp;
1b0ab2c3
MD
1319 hammer_record_t record = nbio->bio_caller_info1.ptr;
1320
e469566b 1321 bp = nbio->bio_buf;
1b0ab2c3 1322 obio = pop_bio(nbio);
e469566b 1323 if (bp->b_flags & B_ERROR) {
cdb6e4e6 1324 hammer_critical_error(record->ip->hmp, record->ip,
e469566b 1325 bp->b_error,
cdb6e4e6 1326 "while writing bulk data");
e469566b 1327 bp->b_flags |= B_INVAL;
cdb6e4e6 1328 }
1b0ab2c3 1329 biodone(obio);
e469566b
MD
1330
1331 KKASSERT(record != NULL);
1332 KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1b0ab2c3 1333 if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
de996e86
MD
1334 record->flags &= ~(HAMMER_RECF_DIRECT_IO |
1335 HAMMER_RECF_DIRECT_WAIT);
1336 /* record can disappear once DIRECT_IO flag is cleared */
1b0ab2c3 1337 wakeup(&record->flags);
de996e86
MD
1338 } else {
1339 record->flags &= ~HAMMER_RECF_DIRECT_IO;
1340 /* record can disappear once DIRECT_IO flag is cleared */
1b0ab2c3
MD
1341 }
1342}
1343
1344
1345/*
1346 * This is called before a record is either committed to the B-Tree
e469566b 1347 * or destroyed, to resolve any associated direct-IO.
1b0ab2c3 1348 *
e469566b
MD
1349 * (1) We must wait for any direct-IO related to the record to complete.
1350 *
1351 * (2) We must remove any buffer cache aliases for data accessed via
1352 * leaf->data_offset or zone2_offset so non-direct-IO consumers
1353 * (the mirroring and reblocking code) do not see stale data.
1b0ab2c3
MD
1354 */
1355void
1356hammer_io_direct_wait(hammer_record_t record)
1357{
e469566b
MD
1358 /*
1359 * Wait for I/O to complete
1360 */
1361 if (record->flags & HAMMER_RECF_DIRECT_IO) {
1362 crit_enter();
1363 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1364 record->flags |= HAMMER_RECF_DIRECT_WAIT;
1365 tsleep(&record->flags, 0, "hmdiow", 0);
1366 }
1367 crit_exit();
1368 }
1369
1370 /*
362ec2dc
MD
1371 * Invalidate any related buffer cache aliases associated with the
1372 * backing device. This is needed because the buffer cache buffer
1373 * for file data is associated with the file vnode, not the backing
1374 * device vnode.
1375 *
1376 * XXX I do not think this case can occur any more now that
1377 * reservations ensure that all such buffers are removed before
1378 * an area can be reused.
e469566b
MD
1379 */
1380 if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1381 KKASSERT(record->leaf.data_offset);
362ec2dc
MD
1382 hammer_del_buffers(record->ip->hmp, record->leaf.data_offset,
1383 record->zone2_offset, record->leaf.data_len,
1384 1);
e469566b 1385 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1b0ab2c3 1386 }
1b0ab2c3
MD
1387}
1388
43c665ae
MD
1389/*
1390 * This is called to remove the second-level cached zone-2 offset from
1391 * frontend buffer cache buffers, now stale due to a data relocation.
1392 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1393 * by hammer_vop_strategy_read().
1394 *
1395 * This is rather nasty because here we have something like the reblocker
1396 * scanning the raw B-Tree with no held references on anything, really,
1397 * other then a shared lock on the B-Tree node, and we have to access the
1398 * frontend's buffer cache to check for and clean out the association.
1399 * Specifically, if the reblocker is moving data on the disk, these cached
1400 * offsets will become invalid.
1401 *
1402 * Only data record types associated with the large-data zone are subject
1403 * to direct-io and need to be checked.
1404 *
1405 */
1406void
1407hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1408{
1409 struct hammer_inode_info iinfo;
1410 int zone;
1411
1412 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1413 return;
1414 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1415 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1416 return;
1417 iinfo.obj_id = leaf->base.obj_id;
1418 iinfo.obj_asof = 0; /* unused */
1419 iinfo.obj_localization = leaf->base.localization &
5a930e66 1420 HAMMER_LOCALIZE_PSEUDOFS_MASK;
43c665ae
MD
1421 iinfo.u.leaf = leaf;
1422 hammer_scan_inode_snapshots(hmp, &iinfo,
1423 hammer_io_direct_uncache_callback,
1424 leaf);
1425}
1426
1427static int
1428hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1429{
1430 hammer_inode_info_t iinfo = data;
1431 hammer_off_t data_offset;
1432 hammer_off_t file_offset;
1433 struct vnode *vp;
1434 struct buf *bp;
1435 int blksize;
1436
1437 if (ip->vp == NULL)
1438 return(0);
1439 data_offset = iinfo->u.leaf->data_offset;
1440 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1441 blksize = iinfo->u.leaf->data_len;
1442 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1443
1444 hammer_ref(&ip->lock);
1445 if (hammer_get_vnode(ip, &vp) == 0) {
b1c20cfa 1446 if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
43c665ae
MD
1447 bp->b_bio2.bio_offset != NOOFFSET) {
1448 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1449 bp->b_bio2.bio_offset = NOOFFSET;
1450 brelse(bp);
1451 }
1452 vput(vp);
1453 }
1454 hammer_rel_inode(ip, 0);
1455 return(0);
1456}
47637bff 1457
748efb59
MD
1458
1459/*
1460 * This function is called when writes may have occured on the volume,
1461 * indicating that the device may be holding cached writes.
1462 */
1463static void
1464hammer_io_flush_mark(hammer_volume_t volume)
1465{
1466 volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1467}
1468
1469/*
1470 * This function ensures that the device has flushed any cached writes out.
1471 */
1472void
1473hammer_io_flush_sync(hammer_mount_t hmp)
1474{
1475 hammer_volume_t volume;
1476 struct buf *bp_base = NULL;
1477 struct buf *bp;
1478
1479 RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1480 if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1481 volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1482 bp = getpbuf(NULL);
1483 bp->b_bio1.bio_offset = 0;
1484 bp->b_bufsize = 0;
1485 bp->b_bcount = 0;
1486 bp->b_cmd = BUF_CMD_FLUSH;
1487 bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
ae8e83e6
MD
1488 bp->b_bio1.bio_done = biodone_sync;
1489 bp->b_bio1.bio_flags |= BIO_SYNC;
748efb59
MD
1490 bp_base = bp;
1491 vn_strategy(volume->devvp, &bp->b_bio1);
1492 }
1493 }
1494 while ((bp = bp_base) != NULL) {
1495 bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
ae8e83e6 1496 biowait(&bp->b_bio1, "hmrFLS");
748efb59
MD
1497 relpbuf(bp, NULL);
1498 }
1499}