2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sys/vfs/hammer/hammer_io.c,v 1.46 2008/06/23 07:31:14 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
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
50 #include <sys/fcntl.h>
51 #include <sys/nlookup.h>
55 static void hammer_io_modify(hammer_io_t io, int count);
56 static void hammer_io_deallocate(struct buf *bp);
57 static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
60 * Initialize a new, already-zero'd hammer_io structure, or reinitialize
61 * an existing hammer_io structure which may have switched to another type.
64 hammer_io_init(hammer_io_t io, hammer_mount_t hmp, enum hammer_io_type type)
71 * Helper routine to disassociate a buffer cache buffer from an I/O
74 * The io may have 0 or 1 references depending on who called us. The
75 * caller is responsible for dealing with the refs.
77 * This call can only be made when no action is required on the buffer.
78 * HAMMER must own the buffer (released == 0) since we mess around with it.
81 hammer_io_disassociate(hammer_io_structure_t iou, int elseit)
83 struct buf *bp = iou->io.bp;
85 KKASSERT(iou->io.modified == 0);
86 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
91 * If the buffer was locked someone wanted to get rid of it.
93 if (bp->b_flags & B_LOCKED) {
94 --hammer_count_io_locked;
95 bp->b_flags &= ~B_LOCKED;
99 * elseit is 0 when called from the kernel path when the io
100 * might have no references.
103 KKASSERT(iou->io.released == 0);
104 iou->io.released = 1;
106 bp->b_flags |= B_NOCACHE|B_RELBUF;
109 KKASSERT(iou->io.released);
113 switch(iou->io.type) {
114 case HAMMER_STRUCTURE_VOLUME:
115 iou->volume.ondisk = NULL;
117 case HAMMER_STRUCTURE_DATA_BUFFER:
118 case HAMMER_STRUCTURE_META_BUFFER:
119 case HAMMER_STRUCTURE_UNDO_BUFFER:
120 iou->buffer.ondisk = NULL;
126 * Wait for any physical IO to complete
129 hammer_io_wait(hammer_io_t io)
133 tsleep_interlock(io);
136 tsleep(io, 0, "hmrflw", 0);
137 if (io->running == 0)
139 tsleep_interlock(io);
141 if (io->running == 0)
149 * Wait for all hammer_io-initated write I/O's to complete. This is not
150 * supposed to count direct I/O's but some can leak through (for
151 * non-full-sized direct I/Os).
154 hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
157 while (hmp->io_running_count)
158 tsleep(&hmp->io_running_count, 0, ident, 0);
162 #define HAMMER_MAXRA 4
165 * Load bp for a HAMMER structure. The io must be exclusively locked by
168 * This routine is mostly used on meta-data and small-data blocks. Generally
169 * speaking HAMMER assumes some locality of reference and will cluster
172 * Note that clustering occurs at the device layer, not the logical layer.
173 * If the buffers do not apply to the current operation they may apply to
177 hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
182 if ((bp = io->bp) == NULL) {
183 ++hammer_count_io_running_read;
185 error = cluster_read(devvp, limit, io->offset, io->bytes,
187 HAMMER_CLUSTER_BUFS, &io->bp);
189 error = bread(devvp, io->offset, io->bytes, &io->bp);
191 --hammer_count_io_running_read;
194 bp->b_ops = &hammer_bioops;
195 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
196 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
199 KKASSERT(io->modified == 0);
200 KKASSERT(io->running == 0);
201 KKASSERT(io->waiting == 0);
202 io->released = 0; /* we hold an active lock on bp */
210 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
211 * Must be called with the IO exclusively locked.
213 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
214 * I/O by forcing the buffer to not be in a released state before calling
217 * This function will also mark the IO as modified but it will not
218 * increment the modify_refs count.
221 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
225 if ((bp = io->bp) == NULL) {
226 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
228 bp->b_ops = &hammer_bioops;
229 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
230 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
232 KKASSERT(io->running == 0);
242 hammer_io_modify(io, 0);
248 * Remove potential device level aliases against buffers managed by high level
252 hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
254 hammer_io_structure_t iou;
255 hammer_off_t phys_offset;
258 phys_offset = volume->ondisk->vol_buf_beg +
259 (zone2_offset & HAMMER_OFF_SHORT_MASK);
261 if ((bp = findblk(volume->devvp, phys_offset)) != NULL) {
262 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
263 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
264 hammer_io_clear_modify(&iou->io, 1);
267 hammer_io_deallocate(bp);
269 KKASSERT((bp->b_flags & B_LOCKED) == 0);
271 bp->b_flags |= B_NOCACHE|B_RELBUF;
279 * This routine is called on the last reference to a hammer structure.
280 * The io is usually locked exclusively (but may not be during unmount).
282 * This routine is responsible for the disposition of the buffer cache
283 * buffer backing the IO. Only pure-data and undo buffers can be handed
284 * back to the kernel. Volume and meta-data buffers must be retained
285 * by HAMMER until explicitly flushed by the backend.
288 hammer_io_release(struct hammer_io *io, int flush)
290 union hammer_io_structure *iou = (void *)io;
293 if ((bp = io->bp) == NULL)
297 * Try to flush a dirty IO to disk if asked to by the
298 * caller or if the kernel tried to flush the buffer in the past.
300 * Kernel-initiated flushes are only allowed for pure-data buffers.
301 * meta-data and volume buffers can only be flushed explicitly
307 } else if (bp->b_flags & B_LOCKED) {
309 case HAMMER_STRUCTURE_DATA_BUFFER:
310 case HAMMER_STRUCTURE_UNDO_BUFFER:
316 } /* else no explicit request to flush the buffer */
320 * Wait for the IO to complete if asked to.
322 if (io->waitdep && io->running) {
327 * Return control of the buffer to the kernel (with the provisio
328 * that our bioops can override kernel decisions with regards to
331 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
333 * Always disassociate the bp if an explicit flush
334 * was requested and the IO completed with no error
335 * (so unmount can really clean up the structure).
342 hammer_io_disassociate((hammer_io_structure_t)io, 1);
343 } else if (io->modified) {
345 * Only certain IO types can be released to the kernel.
346 * volume and meta-data IO types must be explicitly flushed
350 case HAMMER_STRUCTURE_DATA_BUFFER:
351 case HAMMER_STRUCTURE_UNDO_BUFFER:
352 if (io->released == 0) {
360 } else if (io->released == 0) {
362 * Clean buffers can be generally released to the kernel.
363 * We leave the bp passively associated with the HAMMER
364 * structure and use bioops to disconnect it later on
365 * if the kernel wants to discard the buffer.
367 if (bp->b_flags & B_LOCKED) {
368 hammer_io_disassociate(iou, 1);
371 hammer_io_disassociate(iou, 1);
379 * A released buffer is passively associate with our
380 * hammer_io structure. The kernel cannot destroy it
381 * without making a bioops call. If the kernel (B_LOCKED)
382 * or we (reclaim) requested that the buffer be destroyed
383 * we destroy it, otherwise we do a quick get/release to
384 * reset its position in the kernel's LRU list.
386 * Leaving the buffer passively associated allows us to
387 * use the kernel's LRU buffer flushing mechanisms rather
388 * then rolling our own.
390 * XXX there are two ways of doing this. We can re-acquire
391 * and passively release to reset the LRU, or not.
394 if (io->running == 0) {
396 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
399 hammer_io_disassociate(iou, 1);
409 * This routine is called with a locked IO when a flush is desired and
410 * no other references to the structure exists other then ours. This
411 * routine is ONLY called when HAMMER believes it is safe to flush a
412 * potentially modified buffer out.
415 hammer_io_flush(struct hammer_io *io)
420 * Degenerate case - nothing to flush if nothing is dirty.
422 if (io->modified == 0) {
427 KKASSERT(io->modify_refs <= 0);
430 * Acquire ownership of the bp, particularly before we clear our
433 * We are going to bawrite() this bp. Don't leave a window where
434 * io->released is set, we actually own the bp rather then our
440 /* BUF_KERNPROC(io->bp); */
441 /* io->released = 0; */
442 KKASSERT(io->released);
443 KKASSERT(io->bp == bp);
448 * Acquire exclusive access to the bp and then clear the modified
449 * state of the buffer prior to issuing I/O to interlock any
450 * modifications made while the I/O is in progress. This shouldn't
451 * happen anyway but losing data would be worse. The modified bit
452 * will be rechecked after the IO completes.
454 * NOTE: This call also finalizes the buffer's content (inval == 0).
456 * This is only legal when lock.refs == 1 (otherwise we might clear
457 * the modified bit while there are still users of the cluster
458 * modifying the data).
460 * Do this before potentially blocking so any attempt to modify the
461 * ondisk while we are blocked blocks waiting for us.
463 hammer_io_clear_modify(io, 0);
466 * Transfer ownership to the kernel and initiate I/O.
469 ++io->hmp->io_running_count;
470 ++hammer_count_io_running_write;
474 /************************************************************************
476 ************************************************************************
478 * These routines deal with dependancies created when IO buffers get
479 * modified. The caller must call hammer_modify_*() on a referenced
480 * HAMMER structure prior to modifying its on-disk data.
482 * Any intent to modify an IO buffer acquires the related bp and imposes
483 * various write ordering dependancies.
487 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
488 * are locked until the flusher can deal with them, pure data buffers
489 * can be written out.
493 hammer_io_modify(hammer_io_t io, int count)
495 struct hammer_mount *hmp = io->hmp;
498 * io->modify_refs must be >= 0
500 while (io->modify_refs < 0) {
502 tsleep(io, 0, "hmrmod", 0);
506 * Shortcut if nothing to do.
508 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
509 io->modify_refs += count;
510 if (io->modified && io->released == 0)
513 hammer_lock_ex(&io->lock);
514 if (io->modified == 0) {
515 KKASSERT(io->mod_list == NULL);
517 case HAMMER_STRUCTURE_VOLUME:
518 io->mod_list = &hmp->volu_list;
519 ++hmp->locked_dirty_count;
520 ++hammer_count_dirtybufs;
522 case HAMMER_STRUCTURE_META_BUFFER:
523 io->mod_list = &hmp->meta_list;
524 ++hmp->locked_dirty_count;
525 ++hammer_count_dirtybufs;
527 case HAMMER_STRUCTURE_UNDO_BUFFER:
528 io->mod_list = &hmp->undo_list;
530 case HAMMER_STRUCTURE_DATA_BUFFER:
531 io->mod_list = &hmp->data_list;
534 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
539 BUF_KERNPROC(io->bp);
541 KKASSERT(io->modified != 0);
543 hammer_unlock(&io->lock);
548 hammer_io_modify_done(hammer_io_t io)
550 KKASSERT(io->modify_refs > 0);
552 if (io->modify_refs == 0 && io->waitmod) {
559 hammer_io_write_interlock(hammer_io_t io)
561 while (io->modify_refs != 0) {
563 tsleep(io, 0, "hmrmod", 0);
565 io->modify_refs = -1;
569 hammer_io_done_interlock(hammer_io_t io)
571 KKASSERT(io->modify_refs == -1);
580 * Caller intends to modify a volume's ondisk structure.
582 * This is only allowed if we are the flusher or we have a ref on the
586 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
589 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
591 hammer_io_modify(&volume->io, 1);
593 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
594 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
595 hammer_generate_undo(trans, &volume->io,
596 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
602 * Caller intends to modify a buffer's ondisk structure.
604 * This is only allowed if we are the flusher or we have a ref on the
608 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
611 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
613 hammer_io_modify(&buffer->io, 1);
615 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
616 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
617 hammer_generate_undo(trans, &buffer->io,
618 buffer->zone2_offset + rel_offset,
624 hammer_modify_volume_done(hammer_volume_t volume)
626 hammer_io_modify_done(&volume->io);
630 hammer_modify_buffer_done(hammer_buffer_t buffer)
632 hammer_io_modify_done(&buffer->io);
636 * Mark an entity as not being dirty any more and finalize any
637 * delayed adjustments to the buffer.
639 * Delayed adjustments are an important performance enhancement, allowing
640 * us to avoid recalculating B-Tree node CRCs over and over again when
641 * making bulk-modifications to the B-Tree.
643 * If inval is non-zero delayed adjustments are ignored.
646 hammer_io_clear_modify(struct hammer_io *io, int inval)
648 if (io->modified == 0)
652 * Take us off the mod-list and clear the modified bit.
654 KKASSERT(io->mod_list != NULL);
655 if (io->mod_list == &io->hmp->volu_list ||
656 io->mod_list == &io->hmp->meta_list) {
657 --io->hmp->locked_dirty_count;
658 --hammer_count_dirtybufs;
660 TAILQ_REMOVE(io->mod_list, io, mod_entry);
665 * If this bit is not set there are no delayed adjustments.
672 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
673 * on the node (& underlying buffer). Release the node after clearing
676 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
677 hammer_buffer_t buffer = (void *)io;
681 TAILQ_FOREACH(node, &buffer->clist, entry) {
682 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
684 node->flags &= ~HAMMER_NODE_NEEDSCRC;
685 KKASSERT(node->ondisk);
687 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
688 hammer_rel_node(node);
696 * Clear the IO's modify list. Even though the IO is no longer modified
697 * it may still be on the lose_list. This routine is called just before
698 * the governing hammer_buffer is destroyed.
701 hammer_io_clear_modlist(struct hammer_io *io)
703 KKASSERT(io->modified == 0);
705 crit_enter(); /* biodone race against list */
706 KKASSERT(io->mod_list == &io->hmp->lose_list);
707 TAILQ_REMOVE(io->mod_list, io, mod_entry);
713 /************************************************************************
715 ************************************************************************
720 * Pre-IO initiation kernel callback - cluster build only
723 hammer_io_start(struct buf *bp)
728 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
730 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
731 * may also be set if we were marking a cluster header open. Only remove
732 * our dependancy if the modified bit is clear.
735 hammer_io_complete(struct buf *bp)
737 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
739 KKASSERT(iou->io.released == 1);
742 * Deal with people waiting for I/O to drain
744 if (iou->io.running) {
745 --hammer_count_io_running_write;
746 if (--iou->io.hmp->io_running_count == 0)
747 wakeup(&iou->io.hmp->io_running_count);
748 KKASSERT(iou->io.hmp->io_running_count >= 0);
752 if (iou->io.waiting) {
758 * If B_LOCKED is set someone wanted to deallocate the bp at some
759 * point, do it now if refs has become zero.
761 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
762 KKASSERT(iou->io.modified == 0);
763 --hammer_count_io_locked;
764 bp->b_flags &= ~B_LOCKED;
765 hammer_io_deallocate(bp);
766 /* structure may be dead now */
771 * Callback from kernel when it wishes to deallocate a passively
772 * associated structure. This mostly occurs with clean buffers
773 * but it may be possible for a holding structure to be marked dirty
774 * while its buffer is passively associated. The caller owns the bp.
776 * If we cannot disassociate we set B_LOCKED to prevent the buffer
777 * from getting reused.
779 * WARNING: Because this can be called directly by getnewbuf we cannot
780 * recurse into the tree. If a bp cannot be immediately disassociated
781 * our only recourse is to set B_LOCKED.
783 * WARNING: This may be called from an interrupt via hammer_io_complete()
786 hammer_io_deallocate(struct buf *bp)
788 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
790 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
791 if (iou->io.lock.refs > 0 || iou->io.modified) {
793 * It is not legal to disassociate a modified buffer. This
794 * case really shouldn't ever occur.
796 bp->b_flags |= B_LOCKED;
797 ++hammer_count_io_locked;
800 * Disassociate the BP. If the io has no refs left we
801 * have to add it to the loose list.
803 hammer_io_disassociate(iou, 0);
804 if (iou->io.bp == NULL &&
805 iou->io.type != HAMMER_STRUCTURE_VOLUME) {
806 KKASSERT(iou->io.mod_list == NULL);
807 crit_enter(); /* biodone race against list */
808 iou->io.mod_list = &iou->io.hmp->lose_list;
809 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
816 hammer_io_fsync(struct vnode *vp)
822 * NOTE: will not be called unless we tell the kernel about the
823 * bioops. Unused... we use the mount's VFS_SYNC instead.
826 hammer_io_sync(struct mount *mp)
832 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
837 * I/O pre-check for reading and writing. HAMMER only uses this for
838 * B_CACHE buffers so checkread just shouldn't happen, but if it does
841 * Writing is a different case. We don't want the kernel to try to write
842 * out a buffer that HAMMER may be modifying passively or which has a
843 * dependancy. In addition, kernel-demanded writes can only proceed for
844 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
845 * buffer types can only be explicitly written by the flusher.
847 * checkwrite will only be called for bdwrite()n buffers. If we return
848 * success the kernel is guaranteed to initiate the buffer write.
851 hammer_io_checkread(struct buf *bp)
857 hammer_io_checkwrite(struct buf *bp)
859 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
862 * This shouldn't happen under normal operation.
864 if (io->type == HAMMER_STRUCTURE_VOLUME ||
865 io->type == HAMMER_STRUCTURE_META_BUFFER) {
867 panic("hammer_io_checkwrite: illegal buffer");
868 if ((bp->b_flags & B_LOCKED) == 0) {
869 bp->b_flags |= B_LOCKED;
870 ++hammer_count_io_locked;
876 * We can only clear the modified bit if the IO is not currently
877 * undergoing modification. Otherwise we may miss changes.
879 if (io->modify_refs == 0 && io->modified)
880 hammer_io_clear_modify(io, 0);
883 * The kernel is going to start the IO, set io->running.
885 KKASSERT(io->running == 0);
887 ++io->hmp->io_running_count;
888 ++hammer_count_io_running_write;
893 * Return non-zero if we wish to delay the kernel's attempt to flush
894 * this buffer to disk.
897 hammer_io_countdeps(struct buf *bp, int n)
902 struct bio_ops hammer_bioops = {
903 .io_start = hammer_io_start,
904 .io_complete = hammer_io_complete,
905 .io_deallocate = hammer_io_deallocate,
906 .io_fsync = hammer_io_fsync,
907 .io_sync = hammer_io_sync,
908 .io_movedeps = hammer_io_movedeps,
909 .io_countdeps = hammer_io_countdeps,
910 .io_checkread = hammer_io_checkread,
911 .io_checkwrite = hammer_io_checkwrite,
914 /************************************************************************
916 ************************************************************************
918 * These functions operate directly on the buffer cache buffer associated
919 * with a front-end vnode rather then a back-end device vnode.
923 * Read a buffer associated with a front-end vnode directly from the
924 * disk media. The bio may be issued asynchronously.
926 * A second-level bio already resolved to a zone-2 offset (typically by
927 * the BMAP code, or by a previous hammer_io_direct_write()), is passed.
930 hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio)
932 hammer_off_t zone2_offset;
933 hammer_volume_t volume;
939 zone2_offset = bio->bio_offset;
941 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
942 HAMMER_ZONE_RAW_BUFFER);
944 vol_no = HAMMER_VOL_DECODE(zone2_offset);
945 volume = hammer_get_volume(hmp, vol_no, &error);
946 if (error == 0 && zone2_offset >= volume->maxbuf_off)
950 * Third level bio - raw offset specific to the
954 zone2_offset &= HAMMER_OFF_SHORT_MASK;
956 nbio = push_bio(bio);
957 nbio->bio_offset = volume->ondisk->vol_buf_beg +
959 vn_strategy(volume->devvp, nbio);
961 hammer_rel_volume(volume, 0);
964 kprintf("hammer_direct_read: failed @ %016llx\n",
968 bp->b_flags |= B_ERROR;
975 * Write a buffer associated with a front-end vnode directly to the
976 * disk media. The bio may be issued asynchronously.
979 hammer_io_direct_write(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf,
982 hammer_off_t buf_offset;
983 hammer_off_t zone2_offset;
984 hammer_volume_t volume;
985 hammer_buffer_t buffer;
992 buf_offset = leaf->data_offset;
994 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
995 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
997 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
998 leaf->data_len >= HAMMER_BUFSIZE) {
1000 * We are using the vnode's bio to write directly to the
1001 * media, any hammer_buffer at the same zone-X offset will
1002 * now have stale data.
1004 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1005 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1006 volume = hammer_get_volume(hmp, vol_no, &error);
1008 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1012 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1013 hammer_del_buffers(hmp, buf_offset,
1014 zone2_offset, bp->b_bufsize);
1016 * Second level bio - cached zone2 offset.
1018 nbio = push_bio(bio);
1019 nbio->bio_offset = zone2_offset;
1022 * Third level bio - raw offset specific to the
1025 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1026 nbio = push_bio(nbio);
1027 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1029 vn_strategy(volume->devvp, nbio);
1031 hammer_rel_volume(volume, 0);
1033 /* must fit in a standard HAMMER buffer */
1034 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1036 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1039 bp->b_flags |= B_AGE;
1040 hammer_io_modify(&buffer->io, 1);
1041 bcopy(bp->b_data, ptr, leaf->data_len);
1042 hammer_io_modify_done(&buffer->io);
1043 hammer_rel_buffer(buffer, 0);
1049 kprintf("hammer_direct_write: failed @ %016llx\n",
1054 bp->b_flags |= B_ERROR;
1061 * This is called to remove the second-level cached zone-2 offset from
1062 * frontend buffer cache buffers, now stale due to a data relocation.
1063 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1064 * by hammer_vop_strategy_read().
1066 * This is rather nasty because here we have something like the reblocker
1067 * scanning the raw B-Tree with no held references on anything, really,
1068 * other then a shared lock on the B-Tree node, and we have to access the
1069 * frontend's buffer cache to check for and clean out the association.
1070 * Specifically, if the reblocker is moving data on the disk, these cached
1071 * offsets will become invalid.
1073 * Only data record types associated with the large-data zone are subject
1074 * to direct-io and need to be checked.
1078 hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1080 struct hammer_inode_info iinfo;
1083 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1085 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1086 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1088 iinfo.obj_id = leaf->base.obj_id;
1089 iinfo.obj_asof = 0; /* unused */
1090 iinfo.obj_localization = leaf->base.localization &
1091 HAMMER_LOCALIZE_PSEUDOFS_MASK;
1092 iinfo.u.leaf = leaf;
1093 hammer_scan_inode_snapshots(hmp, &iinfo,
1094 hammer_io_direct_uncache_callback,
1099 hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1101 hammer_inode_info_t iinfo = data;
1102 hammer_off_t data_offset;
1103 hammer_off_t file_offset;
1110 data_offset = iinfo->u.leaf->data_offset;
1111 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1112 blksize = iinfo->u.leaf->data_len;
1113 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1115 hammer_ref(&ip->lock);
1116 if (hammer_get_vnode(ip, &vp) == 0) {
1117 if ((bp = findblk(ip->vp, file_offset)) != NULL &&
1118 bp->b_bio2.bio_offset != NOOFFSET) {
1119 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1120 bp->b_bio2.bio_offset = NOOFFSET;
1125 hammer_rel_inode(ip, 0);