5842b563aef21368a3aba5332ec9d36c5b90dd9a
[dragonfly.git] / sys / vfs / hammer / hammer_io.c
blob5842b563aef21368a3aba5332ec9d36c5b90dd9a
1 /*
2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
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:
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.
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.
34 * $DragonFly: src/sys/vfs/hammer/hammer_io.c,v 1.55 2008/09/15 17:02:49 dillon Exp $
37 * IO Primitives and buffer cache management
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.
44 * If the kernel tries to destroy a passively associated buf which we cannot
45 * yet let go we set B_LOCKED in the buffer and then actively released it
46 * later when we can.
49 #include "hammer.h"
50 #include <sys/fcntl.h>
51 #include <sys/nlookup.h>
52 #include <sys/buf.h>
53 #include <sys/buf2.h>
55 static void hammer_io_modify(hammer_io_t io, int count);
56 static void hammer_io_deallocate(struct buf *bp);
57 #if 0
58 static void hammer_io_direct_read_complete(struct bio *nbio);
59 #endif
60 static void hammer_io_direct_write_complete(struct bio *nbio);
61 static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
62 static void hammer_io_set_modlist(struct hammer_io *io);
63 static void hammer_io_flush_mark(hammer_volume_t volume);
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.
70 void
71 hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
73 io->volume = volume;
74 io->hmp = volume->io.hmp;
75 io->type = type;
79 * Helper routine to disassociate a buffer cache buffer from an I/O
80 * structure. The buffer is unlocked and marked appropriate for reclamation.
82 * The io may have 0 or 1 references depending on who called us. The
83 * caller is responsible for dealing with the refs.
85 * This call can only be made when no action is required on the buffer.
87 * The caller must own the buffer and the IO must indicate that the
88 * structure no longer owns it (io.released != 0).
90 static void
91 hammer_io_disassociate(hammer_io_structure_t iou)
93 struct buf *bp = iou->io.bp;
95 KKASSERT(iou->io.released);
96 KKASSERT(iou->io.modified == 0);
97 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
98 buf_dep_init(bp);
99 iou->io.bp = NULL;
102 * If the buffer was locked someone wanted to get rid of it.
104 if (bp->b_flags & B_LOCKED) {
105 --hammer_count_io_locked;
106 bp->b_flags &= ~B_LOCKED;
108 if (iou->io.reclaim) {
109 bp->b_flags |= B_NOCACHE|B_RELBUF;
110 iou->io.reclaim = 0;
113 switch(iou->io.type) {
114 case HAMMER_STRUCTURE_VOLUME:
115 iou->volume.ondisk = NULL;
116 break;
117 case HAMMER_STRUCTURE_DATA_BUFFER:
118 case HAMMER_STRUCTURE_META_BUFFER:
119 case HAMMER_STRUCTURE_UNDO_BUFFER:
120 iou->buffer.ondisk = NULL;
121 break;
122 case HAMMER_STRUCTURE_DUMMY:
123 panic("hammer_io_disassociate: bad io type");
124 break;
129 * Wait for any physical IO to complete
131 * XXX we aren't interlocked against a spinlock or anything so there
132 * is a small window in the interlock / io->running == 0 test.
134 void
135 hammer_io_wait(hammer_io_t io)
137 if (io->running) {
138 for (;;) {
139 io->waiting = 1;
140 tsleep_interlock(io, 0);
141 if (io->running == 0)
142 break;
143 tsleep(io, PINTERLOCKED, "hmrflw", hz);
144 if (io->running == 0)
145 break;
151 * Wait for all currently queued HAMMER-initiated I/Os to complete.
153 * This is not supposed to count direct I/O's but some can leak
154 * through (for non-full-sized direct I/Os).
156 void
157 hammer_io_wait_all(hammer_mount_t hmp, const char *ident, int doflush)
159 struct hammer_io iodummy;
160 hammer_io_t io;
163 * Degenerate case, no I/O is running
165 crit_enter();
166 if (TAILQ_EMPTY(&hmp->iorun_list)) {
167 crit_exit();
168 if (doflush)
169 hammer_io_flush_sync(hmp);
170 return;
172 bzero(&iodummy, sizeof(iodummy));
173 iodummy.type = HAMMER_STRUCTURE_DUMMY;
176 * Add placemarker and then wait until it becomes the head of
177 * the list.
179 TAILQ_INSERT_TAIL(&hmp->iorun_list, &iodummy, iorun_entry);
180 while (TAILQ_FIRST(&hmp->iorun_list) != &iodummy) {
181 tsleep(&iodummy, 0, ident, 0);
185 * Chain in case several placemarkers are present.
187 TAILQ_REMOVE(&hmp->iorun_list, &iodummy, iorun_entry);
188 io = TAILQ_FIRST(&hmp->iorun_list);
189 if (io && io->type == HAMMER_STRUCTURE_DUMMY)
190 wakeup(io);
191 crit_exit();
193 if (doflush)
194 hammer_io_flush_sync(hmp);
198 * Clear a flagged error condition on a I/O buffer. The caller must hold
199 * its own ref on the buffer.
201 void
202 hammer_io_clear_error(struct hammer_io *io)
204 if (io->ioerror) {
205 io->ioerror = 0;
206 hammer_unref(&io->lock);
207 KKASSERT(io->lock.refs > 0);
212 * This is an advisory function only which tells the buffer cache
213 * the bp is not a meta-data buffer, even though it is backed by
214 * a block device.
216 * This is used by HAMMER's reblocking code to avoid trying to
217 * swapcache the filesystem's data when it is read or written
218 * by the reblocking code.
220 void
221 hammer_io_notmeta(hammer_buffer_t buffer)
223 buffer->io.bp->b_flags |= B_NOTMETA;
227 #define HAMMER_MAXRA 4
230 * Load bp for a HAMMER structure. The io must be exclusively locked by
231 * the caller.
233 * This routine is mostly used on meta-data and small-data blocks. Generally
234 * speaking HAMMER assumes some locality of reference and will cluster
235 * a 64K read.
237 * Note that clustering occurs at the device layer, not the logical layer.
238 * If the buffers do not apply to the current operation they may apply to
239 * some other.
242 hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
244 struct buf *bp;
245 int error;
247 if ((bp = io->bp) == NULL) {
248 hammer_count_io_running_read += io->bytes;
249 if (hammer_cluster_enable) {
250 error = cluster_read(devvp, limit,
251 io->offset, io->bytes,
252 HAMMER_CLUSTER_SIZE,
253 HAMMER_CLUSTER_BUFS, &io->bp);
254 } else {
255 error = bread(devvp, io->offset, io->bytes, &io->bp);
257 hammer_stats_disk_read += io->bytes;
258 hammer_count_io_running_read -= io->bytes;
261 * The code generally assumes b_ops/b_dep has been set-up,
262 * even if we error out here.
264 bp = io->bp;
265 bp->b_ops = &hammer_bioops;
266 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
267 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
268 BUF_KERNPROC(bp);
269 KKASSERT(io->modified == 0);
270 KKASSERT(io->running == 0);
271 KKASSERT(io->waiting == 0);
272 io->released = 0; /* we hold an active lock on bp */
273 } else {
274 error = 0;
276 return(error);
280 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
281 * Must be called with the IO exclusively locked.
283 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
284 * I/O by forcing the buffer to not be in a released state before calling
285 * it.
287 * This function will also mark the IO as modified but it will not
288 * increment the modify_refs count.
291 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
293 struct buf *bp;
295 if ((bp = io->bp) == NULL) {
296 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
297 bp = io->bp;
298 bp->b_ops = &hammer_bioops;
299 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
300 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
301 io->released = 0;
302 KKASSERT(io->running == 0);
303 io->waiting = 0;
304 BUF_KERNPROC(bp);
305 } else {
306 if (io->released) {
307 regetblk(bp);
308 BUF_KERNPROC(bp);
309 io->released = 0;
312 hammer_io_modify(io, 0);
313 vfs_bio_clrbuf(bp);
314 return(0);
318 * Advance the activity count on the underlying buffer because
319 * HAMMER does not getblk/brelse on every access.
321 void
322 hammer_io_advance(struct hammer_io *io)
324 if (io->bp)
325 buf_act_advance(io->bp);
329 * Remove potential device level aliases against buffers managed by high level
330 * vnodes. Aliases can also be created due to mixed buffer sizes or via
331 * direct access to the backing store device.
333 * This is nasty because the buffers are also VMIO-backed. Even if a buffer
334 * does not exist its backing VM pages might, and we have to invalidate
335 * those as well or a getblk() will reinstate them.
337 * Buffer cache buffers associated with hammer_buffers cannot be
338 * invalidated.
341 hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
343 hammer_io_structure_t iou;
344 hammer_off_t phys_offset;
345 struct buf *bp;
346 int error;
348 phys_offset = volume->ondisk->vol_buf_beg +
349 (zone2_offset & HAMMER_OFF_SHORT_MASK);
350 crit_enter();
351 if ((bp = findblk(volume->devvp, phys_offset, FINDBLK_TEST)) != NULL)
352 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
353 else
354 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
355 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
356 #if 0
357 hammer_ref(&iou->io.lock);
358 hammer_io_clear_modify(&iou->io, 1);
359 bundirty(bp);
360 iou->io.released = 0;
361 BUF_KERNPROC(bp);
362 iou->io.reclaim = 1;
363 iou->io.waitdep = 1;
364 KKASSERT(iou->io.lock.refs == 1);
365 hammer_rel_buffer(&iou->buffer, 0);
366 /*hammer_io_deallocate(bp);*/
367 #endif
368 bqrelse(bp);
369 error = EAGAIN;
370 } else {
371 KKASSERT((bp->b_flags & B_LOCKED) == 0);
372 bundirty(bp);
373 bp->b_flags |= B_NOCACHE|B_RELBUF;
374 brelse(bp);
375 error = 0;
377 crit_exit();
378 return(error);
382 * This routine is called on the last reference to a hammer structure.
383 * The io is usually interlocked with io.loading and io.refs must be 1.
385 * This routine may return a non-NULL bp to the caller for dispoal. Disposal
386 * simply means the caller finishes decrementing the ref-count on the
387 * IO structure then brelse()'s the bp. The bp may or may not still be
388 * passively associated with the IO.
390 * The only requirement here is that modified meta-data and volume-header
391 * buffer may NOT be disassociated from the IO structure, and consequently
392 * we also leave such buffers actively associated with the IO if they already
393 * are (since the kernel can't do anything with them anyway). Only the
394 * flusher is allowed to write such buffers out. Modified pure-data and
395 * undo buffers are returned to the kernel but left passively associated
396 * so we can track when the kernel writes the bp out.
398 struct buf *
399 hammer_io_release(struct hammer_io *io, int flush)
401 union hammer_io_structure *iou = (void *)io;
402 struct buf *bp;
404 if ((bp = io->bp) == NULL)
405 return(NULL);
408 * Try to flush a dirty IO to disk if asked to by the
409 * caller or if the kernel tried to flush the buffer in the past.
411 * Kernel-initiated flushes are only allowed for pure-data buffers.
412 * meta-data and volume buffers can only be flushed explicitly
413 * by HAMMER.
415 if (io->modified) {
416 if (flush) {
417 hammer_io_flush(io, 0);
418 } else if (bp->b_flags & B_LOCKED) {
419 switch(io->type) {
420 case HAMMER_STRUCTURE_DATA_BUFFER:
421 hammer_io_flush(io, 0);
422 break;
423 case HAMMER_STRUCTURE_UNDO_BUFFER:
424 hammer_io_flush(io, hammer_undo_reclaim(io));
425 break;
426 default:
427 break;
429 } /* else no explicit request to flush the buffer */
433 * Wait for the IO to complete if asked to. This occurs when
434 * the buffer must be disposed of definitively during an umount
435 * or buffer invalidation.
437 if (io->waitdep && io->running) {
438 hammer_io_wait(io);
442 * Return control of the buffer to the kernel (with the provisio
443 * that our bioops can override kernel decisions with regards to
444 * the buffer).
446 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
448 * Always disassociate the bp if an explicit flush
449 * was requested and the IO completed with no error
450 * (so unmount can really clean up the structure).
452 if (io->released) {
453 regetblk(bp);
454 BUF_KERNPROC(bp);
455 } else {
456 io->released = 1;
458 hammer_io_disassociate((hammer_io_structure_t)io);
459 /* return the bp */
460 } else if (io->modified) {
462 * Only certain IO types can be released to the kernel if
463 * the buffer has been modified.
465 * volume and meta-data IO types may only be explicitly
466 * flushed by HAMMER.
468 switch(io->type) {
469 case HAMMER_STRUCTURE_DATA_BUFFER:
470 case HAMMER_STRUCTURE_UNDO_BUFFER:
471 if (io->released == 0) {
472 io->released = 1;
473 bdwrite(bp);
475 break;
476 default:
477 break;
479 bp = NULL; /* bp left associated */
480 } else if (io->released == 0) {
482 * Clean buffers can be generally released to the kernel.
483 * We leave the bp passively associated with the HAMMER
484 * structure and use bioops to disconnect it later on
485 * if the kernel wants to discard the buffer.
487 * We can steal the structure's ownership of the bp.
489 io->released = 1;
490 if (bp->b_flags & B_LOCKED) {
491 hammer_io_disassociate(iou);
492 /* return the bp */
493 } else {
494 if (io->reclaim) {
495 hammer_io_disassociate(iou);
496 /* return the bp */
497 } else {
498 /* return the bp (bp passively associated) */
501 } else {
503 * A released buffer is passively associate with our
504 * hammer_io structure. The kernel cannot destroy it
505 * without making a bioops call. If the kernel (B_LOCKED)
506 * or we (reclaim) requested that the buffer be destroyed
507 * we destroy it, otherwise we do a quick get/release to
508 * reset its position in the kernel's LRU list.
510 * Leaving the buffer passively associated allows us to
511 * use the kernel's LRU buffer flushing mechanisms rather
512 * then rolling our own.
514 * XXX there are two ways of doing this. We can re-acquire
515 * and passively release to reset the LRU, or not.
517 if (io->running == 0) {
518 regetblk(bp);
519 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
520 hammer_io_disassociate(iou);
521 /* return the bp */
522 } else {
523 /* return the bp (bp passively associated) */
525 } else {
527 * bp is left passively associated but we do not
528 * try to reacquire it. Interactions with the io
529 * structure will occur on completion of the bp's
530 * I/O.
532 bp = NULL;
535 return(bp);
539 * This routine is called with a locked IO when a flush is desired and
540 * no other references to the structure exists other then ours. This
541 * routine is ONLY called when HAMMER believes it is safe to flush a
542 * potentially modified buffer out.
544 void
545 hammer_io_flush(struct hammer_io *io, int reclaim)
547 struct buf *bp;
550 * Degenerate case - nothing to flush if nothing is dirty.
552 if (io->modified == 0) {
553 return;
556 KKASSERT(io->bp);
557 KKASSERT(io->modify_refs <= 0);
560 * Acquire ownership of the bp, particularly before we clear our
561 * modified flag.
563 * We are going to bawrite() this bp. Don't leave a window where
564 * io->released is set, we actually own the bp rather then our
565 * buffer.
567 bp = io->bp;
568 if (io->released) {
569 regetblk(bp);
570 /* BUF_KERNPROC(io->bp); */
571 /* io->released = 0; */
572 KKASSERT(io->released);
573 KKASSERT(io->bp == bp);
575 io->released = 1;
577 if (reclaim) {
578 io->reclaim = 1;
579 if ((bp->b_flags & B_LOCKED) == 0) {
580 bp->b_flags |= B_LOCKED;
581 ++hammer_count_io_locked;
586 * Acquire exclusive access to the bp and then clear the modified
587 * state of the buffer prior to issuing I/O to interlock any
588 * modifications made while the I/O is in progress. This shouldn't
589 * happen anyway but losing data would be worse. The modified bit
590 * will be rechecked after the IO completes.
592 * NOTE: This call also finalizes the buffer's content (inval == 0).
594 * This is only legal when lock.refs == 1 (otherwise we might clear
595 * the modified bit while there are still users of the cluster
596 * modifying the data).
598 * Do this before potentially blocking so any attempt to modify the
599 * ondisk while we are blocked blocks waiting for us.
601 hammer_ref(&io->lock);
602 hammer_io_clear_modify(io, 0);
603 hammer_unref(&io->lock);
605 if (hammer_debug_io & 0x0002)
606 kprintf("hammer io_write %016jx\n", bp->b_bio1.bio_offset);
609 * Transfer ownership to the kernel and initiate I/O.
611 io->running = 1;
612 io->hmp->io_running_space += io->bytes;
613 TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
614 hammer_count_io_running_write += io->bytes;
615 bawrite(bp);
616 hammer_io_flush_mark(io->volume);
619 /************************************************************************
620 * BUFFER DIRTYING *
621 ************************************************************************
623 * These routines deal with dependancies created when IO buffers get
624 * modified. The caller must call hammer_modify_*() on a referenced
625 * HAMMER structure prior to modifying its on-disk data.
627 * Any intent to modify an IO buffer acquires the related bp and imposes
628 * various write ordering dependancies.
632 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
633 * are locked until the flusher can deal with them, pure data buffers
634 * can be written out.
636 static
637 void
638 hammer_io_modify(hammer_io_t io, int count)
641 * io->modify_refs must be >= 0
643 while (io->modify_refs < 0) {
644 io->waitmod = 1;
645 tsleep(io, 0, "hmrmod", 0);
649 * Shortcut if nothing to do.
651 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
652 io->modify_refs += count;
653 if (io->modified && io->released == 0)
654 return;
656 hammer_lock_ex(&io->lock);
657 if (io->modified == 0) {
658 hammer_io_set_modlist(io);
659 io->modified = 1;
661 if (io->released) {
662 regetblk(io->bp);
663 BUF_KERNPROC(io->bp);
664 io->released = 0;
665 KKASSERT(io->modified != 0);
667 hammer_unlock(&io->lock);
670 static __inline
671 void
672 hammer_io_modify_done(hammer_io_t io)
674 KKASSERT(io->modify_refs > 0);
675 --io->modify_refs;
676 if (io->modify_refs == 0 && io->waitmod) {
677 io->waitmod = 0;
678 wakeup(io);
682 void
683 hammer_io_write_interlock(hammer_io_t io)
685 while (io->modify_refs != 0) {
686 io->waitmod = 1;
687 tsleep(io, 0, "hmrmod", 0);
689 io->modify_refs = -1;
692 void
693 hammer_io_done_interlock(hammer_io_t io)
695 KKASSERT(io->modify_refs == -1);
696 io->modify_refs = 0;
697 if (io->waitmod) {
698 io->waitmod = 0;
699 wakeup(io);
704 * Caller intends to modify a volume's ondisk structure.
706 * This is only allowed if we are the flusher or we have a ref on the
707 * sync_lock.
709 void
710 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
711 void *base, int len)
713 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
715 hammer_io_modify(&volume->io, 1);
716 if (len) {
717 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
718 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
719 hammer_generate_undo(trans,
720 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
721 base, len);
726 * Caller intends to modify a buffer's ondisk structure.
728 * This is only allowed if we are the flusher or we have a ref on the
729 * sync_lock.
731 void
732 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
733 void *base, int len)
735 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
737 hammer_io_modify(&buffer->io, 1);
738 if (len) {
739 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
740 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
741 hammer_generate_undo(trans,
742 buffer->zone2_offset + rel_offset,
743 base, len);
747 void
748 hammer_modify_volume_done(hammer_volume_t volume)
750 hammer_io_modify_done(&volume->io);
753 void
754 hammer_modify_buffer_done(hammer_buffer_t buffer)
756 hammer_io_modify_done(&buffer->io);
760 * Mark an entity as not being dirty any more and finalize any
761 * delayed adjustments to the buffer.
763 * Delayed adjustments are an important performance enhancement, allowing
764 * us to avoid recalculating B-Tree node CRCs over and over again when
765 * making bulk-modifications to the B-Tree.
767 * If inval is non-zero delayed adjustments are ignored.
769 * This routine may dereference related btree nodes and cause the
770 * buffer to be dereferenced. The caller must own a reference on io.
772 void
773 hammer_io_clear_modify(struct hammer_io *io, int inval)
775 if (io->modified == 0)
776 return;
779 * Take us off the mod-list and clear the modified bit.
781 KKASSERT(io->mod_list != NULL);
782 if (io->mod_list == &io->hmp->volu_list ||
783 io->mod_list == &io->hmp->meta_list) {
784 io->hmp->locked_dirty_space -= io->bytes;
785 hammer_count_dirtybufspace -= io->bytes;
787 TAILQ_REMOVE(io->mod_list, io, mod_entry);
788 io->mod_list = NULL;
789 io->modified = 0;
792 * If this bit is not set there are no delayed adjustments.
794 if (io->gencrc == 0)
795 return;
796 io->gencrc = 0;
799 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
800 * on the node (& underlying buffer). Release the node after clearing
801 * the flag.
803 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
804 hammer_buffer_t buffer = (void *)io;
805 hammer_node_t node;
807 restart:
808 TAILQ_FOREACH(node, &buffer->clist, entry) {
809 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
810 continue;
811 node->flags &= ~HAMMER_NODE_NEEDSCRC;
812 KKASSERT(node->ondisk);
813 if (inval == 0)
814 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
815 hammer_rel_node(node);
816 goto restart;
819 /* caller must still have ref on io */
820 KKASSERT(io->lock.refs > 0);
824 * Clear the IO's modify list. Even though the IO is no longer modified
825 * it may still be on the lose_list. This routine is called just before
826 * the governing hammer_buffer is destroyed.
828 void
829 hammer_io_clear_modlist(struct hammer_io *io)
831 KKASSERT(io->modified == 0);
832 if (io->mod_list) {
833 crit_enter(); /* biodone race against list */
834 KKASSERT(io->mod_list == &io->hmp->lose_list);
835 TAILQ_REMOVE(io->mod_list, io, mod_entry);
836 io->mod_list = NULL;
837 crit_exit();
841 static void
842 hammer_io_set_modlist(struct hammer_io *io)
844 struct hammer_mount *hmp = io->hmp;
846 KKASSERT(io->mod_list == NULL);
848 switch(io->type) {
849 case HAMMER_STRUCTURE_VOLUME:
850 io->mod_list = &hmp->volu_list;
851 hmp->locked_dirty_space += io->bytes;
852 hammer_count_dirtybufspace += io->bytes;
853 break;
854 case HAMMER_STRUCTURE_META_BUFFER:
855 io->mod_list = &hmp->meta_list;
856 hmp->locked_dirty_space += io->bytes;
857 hammer_count_dirtybufspace += io->bytes;
858 break;
859 case HAMMER_STRUCTURE_UNDO_BUFFER:
860 io->mod_list = &hmp->undo_list;
861 break;
862 case HAMMER_STRUCTURE_DATA_BUFFER:
863 io->mod_list = &hmp->data_list;
864 break;
865 case HAMMER_STRUCTURE_DUMMY:
866 panic("hammer_io_disassociate: bad io type");
867 break;
869 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
872 /************************************************************************
873 * HAMMER_BIOOPS *
874 ************************************************************************
879 * Pre-IO initiation kernel callback - cluster build only
881 static void
882 hammer_io_start(struct buf *bp)
887 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
889 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
890 * may also be set if we were marking a cluster header open. Only remove
891 * our dependancy if the modified bit is clear.
893 static void
894 hammer_io_complete(struct buf *bp)
896 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
897 struct hammer_io *ionext;
899 KKASSERT(iou->io.released == 1);
902 * Deal with people waiting for I/O to drain
904 if (iou->io.running) {
906 * Deal with critical write errors. Once a critical error
907 * has been flagged in hmp the UNDO FIFO will not be updated.
908 * That way crash recover will give us a consistent
909 * filesystem.
911 * Because of this we can throw away failed UNDO buffers. If
912 * we throw away META or DATA buffers we risk corrupting
913 * the now read-only version of the filesystem visible to
914 * the user. Clear B_ERROR so the buffer is not re-dirtied
915 * by the kernel and ref the io so it doesn't get thrown
916 * away.
918 if (bp->b_flags & B_ERROR) {
919 hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
920 "while flushing meta-data");
921 switch(iou->io.type) {
922 case HAMMER_STRUCTURE_UNDO_BUFFER:
923 break;
924 default:
925 if (iou->io.ioerror == 0) {
926 iou->io.ioerror = 1;
927 if (iou->io.lock.refs == 0)
928 ++hammer_count_refedbufs;
929 hammer_ref(&iou->io.lock);
931 break;
933 bp->b_flags &= ~B_ERROR;
934 bundirty(bp);
935 #if 0
936 hammer_io_set_modlist(&iou->io);
937 iou->io.modified = 1;
938 #endif
940 hammer_stats_disk_write += iou->io.bytes;
941 hammer_count_io_running_write -= iou->io.bytes;
942 iou->io.hmp->io_running_space -= iou->io.bytes;
943 KKASSERT(iou->io.hmp->io_running_space >= 0);
944 iou->io.running = 0;
947 * Remove from iorun list and wakeup any multi-io waiter(s).
949 if (TAILQ_FIRST(&iou->io.hmp->iorun_list) == &iou->io) {
950 ionext = TAILQ_NEXT(&iou->io, iorun_entry);
951 if (ionext && ionext->type == HAMMER_STRUCTURE_DUMMY)
952 wakeup(ionext);
954 TAILQ_REMOVE(&iou->io.hmp->iorun_list, &iou->io, iorun_entry);
955 } else {
956 hammer_stats_disk_read += iou->io.bytes;
959 if (iou->io.waiting) {
960 iou->io.waiting = 0;
961 wakeup(iou);
965 * If B_LOCKED is set someone wanted to deallocate the bp at some
966 * point, do it now if refs has become zero.
968 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
969 KKASSERT(iou->io.modified == 0);
970 --hammer_count_io_locked;
971 bp->b_flags &= ~B_LOCKED;
972 hammer_io_deallocate(bp);
973 /* structure may be dead now */
978 * Callback from kernel when it wishes to deallocate a passively
979 * associated structure. This mostly occurs with clean buffers
980 * but it may be possible for a holding structure to be marked dirty
981 * while its buffer is passively associated. The caller owns the bp.
983 * If we cannot disassociate we set B_LOCKED to prevent the buffer
984 * from getting reused.
986 * WARNING: Because this can be called directly by getnewbuf we cannot
987 * recurse into the tree. If a bp cannot be immediately disassociated
988 * our only recourse is to set B_LOCKED.
990 * WARNING: This may be called from an interrupt via hammer_io_complete()
992 static void
993 hammer_io_deallocate(struct buf *bp)
995 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
997 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
998 if (iou->io.lock.refs > 0 || iou->io.modified) {
1000 * It is not legal to disassociate a modified buffer. This
1001 * case really shouldn't ever occur.
1003 bp->b_flags |= B_LOCKED;
1004 ++hammer_count_io_locked;
1005 } else {
1007 * Disassociate the BP. If the io has no refs left we
1008 * have to add it to the loose list.
1010 hammer_io_disassociate(iou);
1011 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
1012 KKASSERT(iou->io.bp == NULL);
1013 KKASSERT(iou->io.mod_list == NULL);
1014 crit_enter(); /* biodone race against list */
1015 iou->io.mod_list = &iou->io.hmp->lose_list;
1016 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
1017 crit_exit();
1022 static int
1023 hammer_io_fsync(struct vnode *vp)
1025 return(0);
1029 * NOTE: will not be called unless we tell the kernel about the
1030 * bioops. Unused... we use the mount's VFS_SYNC instead.
1032 static int
1033 hammer_io_sync(struct mount *mp)
1035 return(0);
1038 static void
1039 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
1044 * I/O pre-check for reading and writing. HAMMER only uses this for
1045 * B_CACHE buffers so checkread just shouldn't happen, but if it does
1046 * allow it.
1048 * Writing is a different case. We don't want the kernel to try to write
1049 * out a buffer that HAMMER may be modifying passively or which has a
1050 * dependancy. In addition, kernel-demanded writes can only proceed for
1051 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
1052 * buffer types can only be explicitly written by the flusher.
1054 * checkwrite will only be called for bdwrite()n buffers. If we return
1055 * success the kernel is guaranteed to initiate the buffer write.
1057 static int
1058 hammer_io_checkread(struct buf *bp)
1060 return(0);
1063 static int
1064 hammer_io_checkwrite(struct buf *bp)
1066 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
1069 * This shouldn't happen under normal operation.
1071 if (io->type == HAMMER_STRUCTURE_VOLUME ||
1072 io->type == HAMMER_STRUCTURE_META_BUFFER) {
1073 if (!panicstr)
1074 panic("hammer_io_checkwrite: illegal buffer");
1075 if ((bp->b_flags & B_LOCKED) == 0) {
1076 bp->b_flags |= B_LOCKED;
1077 ++hammer_count_io_locked;
1079 return(1);
1083 * We can only clear the modified bit if the IO is not currently
1084 * undergoing modification. Otherwise we may miss changes.
1086 * Only data and undo buffers can reach here. These buffers do
1087 * not have terminal crc functions but we temporarily reference
1088 * the IO anyway, just in case.
1090 if (io->modify_refs == 0 && io->modified) {
1091 hammer_ref(&io->lock);
1092 hammer_io_clear_modify(io, 0);
1093 hammer_unref(&io->lock);
1094 } else if (io->modified) {
1095 KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
1099 * The kernel is going to start the IO, set io->running.
1101 KKASSERT(io->running == 0);
1102 io->running = 1;
1103 io->hmp->io_running_space += io->bytes;
1104 TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
1105 hammer_count_io_running_write += io->bytes;
1106 return(0);
1110 * Return non-zero if we wish to delay the kernel's attempt to flush
1111 * this buffer to disk.
1113 static int
1114 hammer_io_countdeps(struct buf *bp, int n)
1116 return(0);
1119 struct bio_ops hammer_bioops = {
1120 .io_start = hammer_io_start,
1121 .io_complete = hammer_io_complete,
1122 .io_deallocate = hammer_io_deallocate,
1123 .io_fsync = hammer_io_fsync,
1124 .io_sync = hammer_io_sync,
1125 .io_movedeps = hammer_io_movedeps,
1126 .io_countdeps = hammer_io_countdeps,
1127 .io_checkread = hammer_io_checkread,
1128 .io_checkwrite = hammer_io_checkwrite,
1131 /************************************************************************
1132 * DIRECT IO OPS *
1133 ************************************************************************
1135 * These functions operate directly on the buffer cache buffer associated
1136 * with a front-end vnode rather then a back-end device vnode.
1140 * Read a buffer associated with a front-end vnode directly from the
1141 * disk media. The bio may be issued asynchronously. If leaf is non-NULL
1142 * we validate the CRC.
1144 * We must check for the presence of a HAMMER buffer to handle the case
1145 * where the reblocker has rewritten the data (which it does via the HAMMER
1146 * buffer system, not via the high-level vnode buffer cache), but not yet
1147 * committed the buffer to the media.
1150 hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1151 hammer_btree_leaf_elm_t leaf)
1153 hammer_off_t buf_offset;
1154 hammer_off_t zone2_offset;
1155 hammer_volume_t volume;
1156 struct buf *bp;
1157 struct bio *nbio;
1158 int vol_no;
1159 int error;
1161 buf_offset = bio->bio_offset;
1162 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1163 HAMMER_ZONE_LARGE_DATA);
1166 * The buffer cache may have an aliased buffer (the reblocker can
1167 * write them). If it does we have to sync any dirty data before
1168 * we can build our direct-read. This is a non-critical code path.
1170 bp = bio->bio_buf;
1171 hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
1174 * Resolve to a zone-2 offset. The conversion just requires
1175 * munging the top 4 bits but we want to abstract it anyway
1176 * so the blockmap code can verify the zone assignment.
1178 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1179 if (error)
1180 goto done;
1181 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1182 HAMMER_ZONE_RAW_BUFFER);
1185 * Resolve volume and raw-offset for 3rd level bio. The
1186 * offset will be specific to the volume.
1188 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1189 volume = hammer_get_volume(hmp, vol_no, &error);
1190 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1191 error = EIO;
1193 if (error == 0) {
1195 * 3rd level bio
1197 nbio = push_bio(bio);
1198 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1199 (zone2_offset & HAMMER_OFF_SHORT_MASK);
1200 #if 0
1202 * XXX disabled - our CRC check doesn't work if the OS
1203 * does bogus_page replacement on the direct-read.
1205 if (leaf && hammer_verify_data) {
1206 nbio->bio_done = hammer_io_direct_read_complete;
1207 nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1209 #endif
1210 hammer_stats_disk_read += bp->b_bufsize;
1211 vn_strategy(volume->devvp, nbio);
1213 hammer_rel_volume(volume, 0);
1214 done:
1215 if (error) {
1216 kprintf("hammer_direct_read: failed @ %016llx\n",
1217 (long long)zone2_offset);
1218 bp->b_error = error;
1219 bp->b_flags |= B_ERROR;
1220 biodone(bio);
1222 return(error);
1225 #if 0
1227 * On completion of the BIO this callback must check the data CRC
1228 * and chain to the previous bio.
1230 static
1231 void
1232 hammer_io_direct_read_complete(struct bio *nbio)
1234 struct bio *obio;
1235 struct buf *bp;
1236 u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1238 bp = nbio->bio_buf;
1239 if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1240 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1241 nbio->bio_offset, bp->b_bufsize);
1242 if (hammer_debug_critical)
1243 Debugger("data_crc on read");
1244 bp->b_flags |= B_ERROR;
1245 bp->b_error = EIO;
1247 obio = pop_bio(nbio);
1248 biodone(obio);
1250 #endif
1253 * Write a buffer associated with a front-end vnode directly to the
1254 * disk media. The bio may be issued asynchronously.
1256 * The BIO is associated with the specified record and RECF_DIRECT_IO
1257 * is set. The recorded is added to its object.
1260 hammer_io_direct_write(hammer_mount_t hmp, struct bio *bio,
1261 hammer_record_t record)
1263 hammer_btree_leaf_elm_t leaf = &record->leaf;
1264 hammer_off_t buf_offset;
1265 hammer_off_t zone2_offset;
1266 hammer_volume_t volume;
1267 hammer_buffer_t buffer;
1268 struct buf *bp;
1269 struct bio *nbio;
1270 char *ptr;
1271 int vol_no;
1272 int error;
1274 buf_offset = leaf->data_offset;
1276 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
1277 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1280 * Issue or execute the I/O. The new memory record must replace
1281 * the old one before the I/O completes, otherwise a reaquisition of
1282 * the buffer will load the old media data instead of the new.
1284 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
1285 leaf->data_len >= HAMMER_BUFSIZE) {
1287 * We are using the vnode's bio to write directly to the
1288 * media, any hammer_buffer at the same zone-X offset will
1289 * now have stale data.
1291 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1292 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1293 volume = hammer_get_volume(hmp, vol_no, &error);
1295 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1296 error = EIO;
1297 if (error == 0) {
1298 bp = bio->bio_buf;
1299 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1301 hammer_del_buffers(hmp, buf_offset,
1302 zone2_offset, bp->b_bufsize);
1306 * Second level bio - cached zone2 offset.
1308 * (We can put our bio_done function in either the
1309 * 2nd or 3rd level).
1311 nbio = push_bio(bio);
1312 nbio->bio_offset = zone2_offset;
1313 nbio->bio_done = hammer_io_direct_write_complete;
1314 nbio->bio_caller_info1.ptr = record;
1315 record->zone2_offset = zone2_offset;
1316 record->flags |= HAMMER_RECF_DIRECT_IO |
1317 HAMMER_RECF_DIRECT_INVAL;
1320 * Third level bio - raw offset specific to the
1321 * correct volume.
1323 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1324 nbio = push_bio(nbio);
1325 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1326 zone2_offset;
1327 hammer_stats_disk_write += bp->b_bufsize;
1328 hammer_ip_replace_bulk(hmp, record);
1329 vn_strategy(volume->devvp, nbio);
1330 hammer_io_flush_mark(volume);
1332 hammer_rel_volume(volume, 0);
1333 } else {
1335 * Must fit in a standard HAMMER buffer. In this case all
1336 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1337 * does not need to be set-up.
1339 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1340 buffer = NULL;
1341 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1342 if (error == 0) {
1343 bp = bio->bio_buf;
1344 bp->b_flags |= B_AGE;
1345 hammer_io_modify(&buffer->io, 1);
1346 bcopy(bp->b_data, ptr, leaf->data_len);
1347 hammer_io_modify_done(&buffer->io);
1348 hammer_rel_buffer(buffer, 0);
1349 bp->b_resid = 0;
1350 hammer_ip_replace_bulk(hmp, record);
1351 biodone(bio);
1354 if (error) {
1356 * Major suckage occured. Also note: The record was
1357 * never added to the tree so we do not have to worry
1358 * about the backend.
1360 kprintf("hammer_direct_write: failed @ %016llx\n",
1361 (long long)leaf->data_offset);
1362 bp = bio->bio_buf;
1363 bp->b_resid = 0;
1364 bp->b_error = EIO;
1365 bp->b_flags |= B_ERROR;
1366 biodone(bio);
1367 record->flags |= HAMMER_RECF_DELETED_FE;
1368 hammer_rel_mem_record(record);
1370 return(error);
1374 * On completion of the BIO this callback must disconnect
1375 * it from the hammer_record and chain to the previous bio.
1377 * An I/O error forces the mount to read-only. Data buffers
1378 * are not B_LOCKED like meta-data buffers are, so we have to
1379 * throw the buffer away to prevent the kernel from retrying.
1381 static
1382 void
1383 hammer_io_direct_write_complete(struct bio *nbio)
1385 struct bio *obio;
1386 struct buf *bp;
1387 hammer_record_t record = nbio->bio_caller_info1.ptr;
1389 bp = nbio->bio_buf;
1390 obio = pop_bio(nbio);
1391 if (bp->b_flags & B_ERROR) {
1392 hammer_critical_error(record->ip->hmp, record->ip,
1393 bp->b_error,
1394 "while writing bulk data");
1395 bp->b_flags |= B_INVAL;
1397 biodone(obio);
1399 KKASSERT(record != NULL);
1400 KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1401 if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1402 record->flags &= ~(HAMMER_RECF_DIRECT_IO |
1403 HAMMER_RECF_DIRECT_WAIT);
1404 /* record can disappear once DIRECT_IO flag is cleared */
1405 wakeup(&record->flags);
1406 } else {
1407 record->flags &= ~HAMMER_RECF_DIRECT_IO;
1408 /* record can disappear once DIRECT_IO flag is cleared */
1414 * This is called before a record is either committed to the B-Tree
1415 * or destroyed, to resolve any associated direct-IO.
1417 * (1) We must wait for any direct-IO related to the record to complete.
1419 * (2) We must remove any buffer cache aliases for data accessed via
1420 * leaf->data_offset or zone2_offset so non-direct-IO consumers
1421 * (the mirroring and reblocking code) do not see stale data.
1423 void
1424 hammer_io_direct_wait(hammer_record_t record)
1427 * Wait for I/O to complete
1429 if (record->flags & HAMMER_RECF_DIRECT_IO) {
1430 crit_enter();
1431 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1432 record->flags |= HAMMER_RECF_DIRECT_WAIT;
1433 tsleep(&record->flags, 0, "hmdiow", 0);
1435 crit_exit();
1439 * Invalidate any related buffer cache aliases associated with the
1440 * backing device. This is needed because the buffer cache buffer
1441 * for file data is associated with the file vnode, not the backing
1442 * device vnode.
1444 * XXX I do not think this case can occur any more now that
1445 * reservations ensure that all such buffers are removed before
1446 * an area can be reused.
1448 if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1449 KKASSERT(record->leaf.data_offset);
1450 hammer_del_buffers(record->ip->hmp, record->leaf.data_offset,
1451 record->zone2_offset, record->leaf.data_len,
1453 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1458 * This is called to remove the second-level cached zone-2 offset from
1459 * frontend buffer cache buffers, now stale due to a data relocation.
1460 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1461 * by hammer_vop_strategy_read().
1463 * This is rather nasty because here we have something like the reblocker
1464 * scanning the raw B-Tree with no held references on anything, really,
1465 * other then a shared lock on the B-Tree node, and we have to access the
1466 * frontend's buffer cache to check for and clean out the association.
1467 * Specifically, if the reblocker is moving data on the disk, these cached
1468 * offsets will become invalid.
1470 * Only data record types associated with the large-data zone are subject
1471 * to direct-io and need to be checked.
1474 void
1475 hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1477 struct hammer_inode_info iinfo;
1478 int zone;
1480 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1481 return;
1482 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1483 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1484 return;
1485 iinfo.obj_id = leaf->base.obj_id;
1486 iinfo.obj_asof = 0; /* unused */
1487 iinfo.obj_localization = leaf->base.localization &
1488 HAMMER_LOCALIZE_PSEUDOFS_MASK;
1489 iinfo.u.leaf = leaf;
1490 hammer_scan_inode_snapshots(hmp, &iinfo,
1491 hammer_io_direct_uncache_callback,
1492 leaf);
1495 static int
1496 hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1498 hammer_inode_info_t iinfo = data;
1499 hammer_off_t data_offset;
1500 hammer_off_t file_offset;
1501 struct vnode *vp;
1502 struct buf *bp;
1503 int blksize;
1505 if (ip->vp == NULL)
1506 return(0);
1507 data_offset = iinfo->u.leaf->data_offset;
1508 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1509 blksize = iinfo->u.leaf->data_len;
1510 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1512 hammer_ref(&ip->lock);
1513 if (hammer_get_vnode(ip, &vp) == 0) {
1514 if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
1515 bp->b_bio2.bio_offset != NOOFFSET) {
1516 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1517 bp->b_bio2.bio_offset = NOOFFSET;
1518 brelse(bp);
1520 vput(vp);
1522 hammer_rel_inode(ip, 0);
1523 return(0);
1528 * This function is called when writes may have occured on the volume,
1529 * indicating that the device may be holding cached writes.
1531 static void
1532 hammer_io_flush_mark(hammer_volume_t volume)
1534 volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1538 * This function ensures that the device has flushed any cached writes out.
1540 void
1541 hammer_io_flush_sync(hammer_mount_t hmp)
1543 hammer_volume_t volume;
1544 struct buf *bp_base = NULL;
1545 struct buf *bp;
1547 RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1548 if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1549 volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1550 bp = getpbuf(NULL);
1551 bp->b_bio1.bio_offset = 0;
1552 bp->b_bufsize = 0;
1553 bp->b_bcount = 0;
1554 bp->b_cmd = BUF_CMD_FLUSH;
1555 bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1556 bp->b_bio1.bio_done = biodone_sync;
1557 bp->b_bio1.bio_flags |= BIO_SYNC;
1558 bp_base = bp;
1559 vn_strategy(volume->devvp, &bp->b_bio1);
1562 while ((bp = bp_base) != NULL) {
1563 bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1564 biowait(&bp->b_bio1, "hmrFLS");
1565 relpbuf(bp, NULL);