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.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
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);
58 static void hammer_io_direct_read_complete(struct bio *nbio);
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.
71 hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
74 io->hmp = volume->io.hmp;
79 * Determine if an io can be clustered for the storage cdev. We have to
80 * be careful to avoid creating overlapping buffers.
82 * (1) Any clustering is limited to within a largeblock, since going into
83 * an adjacent largeblock will change the zone.
85 * (2) The large-data zone can contain mixed buffer sizes. Other zones
86 * contain only HAMMER_BUFSIZE sized buffer sizes (16K).
89 hammer_io_clusterable(hammer_io_t io, hammer_off_t *limitp)
91 hammer_buffer_t buffer;
95 * Can't cluster non hammer_buffer_t's
97 if (io->type != HAMMER_STRUCTURE_DATA_BUFFER &&
98 io->type != HAMMER_STRUCTURE_META_BUFFER &&
99 io->type != HAMMER_STRUCTURE_UNDO_BUFFER) {
104 * We cannot cluster the large-data zone. This primarily targets
105 * the reblocker. The normal file handling code will still cluster
106 * file reads via file vnodes.
109 if ((buffer->zoneX_offset & HAMMER_OFF_ZONE_MASK) ==
110 HAMMER_ZONE_LARGE_DATA) {
115 * Do not allow the cluster operation to cross a largeblock
118 eoz = (io->offset + HAMMER_LARGEBLOCK_SIZE64 - 1) &
119 ~HAMMER_LARGEBLOCK_MASK64;
126 * Helper routine to disassociate a buffer cache buffer from an I/O
127 * structure. The buffer is unlocked and marked appropriate for reclamation.
129 * The io may have 0 or 1 references depending on who called us. The
130 * caller is responsible for dealing with the refs.
132 * This call can only be made when no action is required on the buffer.
134 * The caller must own the buffer and the IO must indicate that the
135 * structure no longer owns it (io.released != 0).
138 hammer_io_disassociate(hammer_io_structure_t iou)
140 struct buf *bp = iou->io.bp;
142 KKASSERT(iou->io.released);
143 KKASSERT(iou->io.modified == 0);
144 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
149 * If the buffer was locked someone wanted to get rid of it.
151 if (bp->b_flags & B_LOCKED) {
152 --hammer_count_io_locked;
153 bp->b_flags &= ~B_LOCKED;
155 if (iou->io.reclaim) {
156 bp->b_flags |= B_NOCACHE|B_RELBUF;
160 switch(iou->io.type) {
161 case HAMMER_STRUCTURE_VOLUME:
162 iou->volume.ondisk = NULL;
164 case HAMMER_STRUCTURE_DATA_BUFFER:
165 case HAMMER_STRUCTURE_META_BUFFER:
166 case HAMMER_STRUCTURE_UNDO_BUFFER:
167 iou->buffer.ondisk = NULL;
169 case HAMMER_STRUCTURE_DUMMY:
170 panic("hammer_io_disassociate: bad io type");
176 * Wait for any physical IO to complete
178 * XXX we aren't interlocked against a spinlock or anything so there
179 * is a small window in the interlock / io->running == 0 test.
182 hammer_io_wait(hammer_io_t io)
187 tsleep_interlock(io, 0);
188 if (io->running == 0)
190 tsleep(io, PINTERLOCKED, "hmrflw", hz);
191 if (io->running == 0)
198 * Wait for all currently queued HAMMER-initiated I/Os to complete.
200 * This is not supposed to count direct I/O's but some can leak
201 * through (for non-full-sized direct I/Os).
204 hammer_io_wait_all(hammer_mount_t hmp, const char *ident, int doflush)
206 struct hammer_io iodummy;
210 * Degenerate case, no I/O is running
213 if (TAILQ_EMPTY(&hmp->iorun_list)) {
216 hammer_io_flush_sync(hmp);
219 bzero(&iodummy, sizeof(iodummy));
220 iodummy.type = HAMMER_STRUCTURE_DUMMY;
223 * Add placemarker and then wait until it becomes the head of
226 TAILQ_INSERT_TAIL(&hmp->iorun_list, &iodummy, iorun_entry);
227 while (TAILQ_FIRST(&hmp->iorun_list) != &iodummy) {
228 tsleep(&iodummy, 0, ident, 0);
232 * Chain in case several placemarkers are present.
234 TAILQ_REMOVE(&hmp->iorun_list, &iodummy, iorun_entry);
235 io = TAILQ_FIRST(&hmp->iorun_list);
236 if (io && io->type == HAMMER_STRUCTURE_DUMMY)
241 hammer_io_flush_sync(hmp);
245 * Clear a flagged error condition on a I/O buffer. The caller must hold
246 * its own ref on the buffer.
249 hammer_io_clear_error(struct hammer_io *io)
253 hammer_rel(&io->lock);
254 KKASSERT(hammer_isactive(&io->lock));
259 * This is an advisory function only which tells the buffer cache
260 * the bp is not a meta-data buffer, even though it is backed by
263 * This is used by HAMMER's reblocking code to avoid trying to
264 * swapcache the filesystem's data when it is read or written
265 * by the reblocking code.
268 hammer_io_notmeta(hammer_buffer_t buffer)
270 buffer->io.bp->b_flags |= B_NOTMETA;
274 #define HAMMER_MAXRA 4
277 * Load bp for a HAMMER structure. The io must be exclusively locked by
280 * This routine is mostly used on meta-data and small-data blocks. Generally
281 * speaking HAMMER assumes some locality of reference and will cluster
284 * Note that clustering occurs at the device layer, not the logical layer.
285 * If the buffers do not apply to the current operation they may apply to
289 hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
294 if ((bp = io->bp) == NULL) {
295 hammer_count_io_running_read += io->bytes;
296 if (hammer_cluster_enable &&
297 hammer_io_clusterable(io, &limit)) {
298 error = cluster_read(devvp, limit,
299 io->offset, io->bytes,
301 HAMMER_CLUSTER_BUFS, &io->bp);
303 error = bread(devvp, io->offset, io->bytes, &io->bp);
305 hammer_stats_disk_read += io->bytes;
306 hammer_count_io_running_read -= io->bytes;
309 * The code generally assumes b_ops/b_dep has been set-up,
310 * even if we error out here.
313 bp->b_ops = &hammer_bioops;
314 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
315 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
317 KKASSERT(io->modified == 0);
318 KKASSERT(io->running == 0);
319 KKASSERT(io->waiting == 0);
320 io->released = 0; /* we hold an active lock on bp */
328 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
329 * Must be called with the IO exclusively locked.
331 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
332 * I/O by forcing the buffer to not be in a released state before calling
335 * This function will also mark the IO as modified but it will not
336 * increment the modify_refs count.
339 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
343 if ((bp = io->bp) == NULL) {
344 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
346 bp->b_ops = &hammer_bioops;
347 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
348 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
350 KKASSERT(io->running == 0);
360 hammer_io_modify(io, 0);
366 * Advance the activity count on the underlying buffer because
367 * HAMMER does not getblk/brelse on every access.
370 hammer_io_advance(struct hammer_io *io)
373 buf_act_advance(io->bp);
377 * Remove potential device level aliases against buffers managed by high level
378 * vnodes. Aliases can also be created due to mixed buffer sizes or via
379 * direct access to the backing store device.
381 * This is nasty because the buffers are also VMIO-backed. Even if a buffer
382 * does not exist its backing VM pages might, and we have to invalidate
383 * those as well or a getblk() will reinstate them.
385 * Buffer cache buffers associated with hammer_buffers cannot be
389 hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
391 hammer_io_structure_t iou;
392 hammer_off_t phys_offset;
396 phys_offset = volume->ondisk->vol_buf_beg +
397 (zone2_offset & HAMMER_OFF_SHORT_MASK);
399 if ((bp = findblk(volume->devvp, phys_offset, FINDBLK_TEST)) != NULL)
400 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
402 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
403 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
405 hammer_ref(&iou->io.lock);
406 hammer_io_clear_modify(&iou->io, 1);
408 iou->io.released = 0;
412 KKASSERT(hammer_isactive(&iou->io.lock) == 1);
413 hammer_rel_buffer(&iou->buffer, 0);
414 /*hammer_io_deallocate(bp);*/
419 KKASSERT((bp->b_flags & B_LOCKED) == 0);
421 bp->b_flags |= B_NOCACHE|B_RELBUF;
430 * This routine is called on the last reference to a hammer structure.
431 * The io must be interlocked with a refcount of zero. The hammer structure
432 * will remain interlocked on return.
434 * This routine may return a non-NULL bp to the caller for dispoal.
435 * The caller typically brelse()'s the bp.
437 * The bp may or may not still be passively associated with the IO. It
438 * will remain passively associated if it is unreleasable (e.g. a modified
441 * The only requirement here is that modified meta-data and volume-header
442 * buffer may NOT be disassociated from the IO structure, and consequently
443 * we also leave such buffers actively associated with the IO if they already
444 * are (since the kernel can't do anything with them anyway). Only the
445 * flusher is allowed to write such buffers out. Modified pure-data and
446 * undo buffers are returned to the kernel but left passively associated
447 * so we can track when the kernel writes the bp out.
450 hammer_io_release(struct hammer_io *io, int flush)
452 union hammer_io_structure *iou = (void *)io;
455 if ((bp = io->bp) == NULL)
459 * Try to flush a dirty IO to disk if asked to by the
460 * caller or if the kernel tried to flush the buffer in the past.
462 * Kernel-initiated flushes are only allowed for pure-data buffers.
463 * meta-data and volume buffers can only be flushed explicitly
468 hammer_io_flush(io, 0);
469 } else if (bp->b_flags & B_LOCKED) {
471 case HAMMER_STRUCTURE_DATA_BUFFER:
472 hammer_io_flush(io, 0);
474 case HAMMER_STRUCTURE_UNDO_BUFFER:
475 hammer_io_flush(io, hammer_undo_reclaim(io));
480 } /* else no explicit request to flush the buffer */
484 * Wait for the IO to complete if asked to. This occurs when
485 * the buffer must be disposed of definitively during an umount
486 * or buffer invalidation.
488 if (io->waitdep && io->running) {
493 * Return control of the buffer to the kernel (with the provisio
494 * that our bioops can override kernel decisions with regards to
497 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
499 * Always disassociate the bp if an explicit flush
500 * was requested and the IO completed with no error
501 * (so unmount can really clean up the structure).
509 hammer_io_disassociate((hammer_io_structure_t)io);
511 } else if (io->modified) {
513 * Only certain IO types can be released to the kernel if
514 * the buffer has been modified.
516 * volume and meta-data IO types may only be explicitly
520 case HAMMER_STRUCTURE_DATA_BUFFER:
521 case HAMMER_STRUCTURE_UNDO_BUFFER:
522 if (io->released == 0) {
530 bp = NULL; /* bp left associated */
531 } else if (io->released == 0) {
533 * Clean buffers can be generally released to the kernel.
534 * We leave the bp passively associated with the HAMMER
535 * structure and use bioops to disconnect it later on
536 * if the kernel wants to discard the buffer.
538 * We can steal the structure's ownership of the bp.
541 if (bp->b_flags & B_LOCKED) {
542 hammer_io_disassociate(iou);
546 hammer_io_disassociate(iou);
549 /* return the bp (bp passively associated) */
554 * A released buffer is passively associate with our
555 * hammer_io structure. The kernel cannot destroy it
556 * without making a bioops call. If the kernel (B_LOCKED)
557 * or we (reclaim) requested that the buffer be destroyed
558 * we destroy it, otherwise we do a quick get/release to
559 * reset its position in the kernel's LRU list.
561 * Leaving the buffer passively associated allows us to
562 * use the kernel's LRU buffer flushing mechanisms rather
563 * then rolling our own.
565 * XXX there are two ways of doing this. We can re-acquire
566 * and passively release to reset the LRU, or not.
568 if (io->running == 0) {
570 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
571 hammer_io_disassociate(iou);
574 /* return the bp (bp passively associated) */
578 * bp is left passively associated but we do not
579 * try to reacquire it. Interactions with the io
580 * structure will occur on completion of the bp's
590 * This routine is called with a locked IO when a flush is desired and
591 * no other references to the structure exists other then ours. This
592 * routine is ONLY called when HAMMER believes it is safe to flush a
593 * potentially modified buffer out.
596 hammer_io_flush(struct hammer_io *io, int reclaim)
601 * Degenerate case - nothing to flush if nothing is dirty.
603 if (io->modified == 0) {
608 KKASSERT(io->modify_refs <= 0);
611 * Acquire ownership of the bp, particularly before we clear our
614 * We are going to bawrite() this bp. Don't leave a window where
615 * io->released is set, we actually own the bp rather then our
621 /* BUF_KERNPROC(io->bp); */
622 /* io->released = 0; */
623 KKASSERT(io->released);
624 KKASSERT(io->bp == bp);
630 if ((bp->b_flags & B_LOCKED) == 0) {
631 bp->b_flags |= B_LOCKED;
632 ++hammer_count_io_locked;
637 * Acquire exclusive access to the bp and then clear the modified
638 * state of the buffer prior to issuing I/O to interlock any
639 * modifications made while the I/O is in progress. This shouldn't
640 * happen anyway but losing data would be worse. The modified bit
641 * will be rechecked after the IO completes.
643 * NOTE: This call also finalizes the buffer's content (inval == 0).
645 * This is only legal when lock.refs == 1 (otherwise we might clear
646 * the modified bit while there are still users of the cluster
647 * modifying the data).
649 * Do this before potentially blocking so any attempt to modify the
650 * ondisk while we are blocked blocks waiting for us.
652 hammer_ref(&io->lock);
653 hammer_io_clear_modify(io, 0);
654 hammer_rel(&io->lock);
656 if (hammer_debug_io & 0x0002)
657 kprintf("hammer io_write %016jx\n", bp->b_bio1.bio_offset);
660 * Transfer ownership to the kernel and initiate I/O.
663 io->hmp->io_running_space += io->bytes;
664 TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
665 hammer_count_io_running_write += io->bytes;
667 hammer_io_flush_mark(io->volume);
670 /************************************************************************
672 ************************************************************************
674 * These routines deal with dependancies created when IO buffers get
675 * modified. The caller must call hammer_modify_*() on a referenced
676 * HAMMER structure prior to modifying its on-disk data.
678 * Any intent to modify an IO buffer acquires the related bp and imposes
679 * various write ordering dependancies.
683 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
684 * are locked until the flusher can deal with them, pure data buffers
685 * can be written out.
689 hammer_io_modify(hammer_io_t io, int count)
692 * io->modify_refs must be >= 0
694 while (io->modify_refs < 0) {
696 tsleep(io, 0, "hmrmod", 0);
700 * Shortcut if nothing to do.
702 KKASSERT(hammer_isactive(&io->lock) && io->bp != NULL);
703 io->modify_refs += count;
704 if (io->modified && io->released == 0)
707 hammer_lock_ex(&io->lock);
708 if (io->modified == 0) {
709 hammer_io_set_modlist(io);
714 BUF_KERNPROC(io->bp);
716 KKASSERT(io->modified != 0);
718 hammer_unlock(&io->lock);
723 hammer_io_modify_done(hammer_io_t io)
725 KKASSERT(io->modify_refs > 0);
727 if (io->modify_refs == 0 && io->waitmod) {
734 hammer_io_write_interlock(hammer_io_t io)
736 while (io->modify_refs != 0) {
738 tsleep(io, 0, "hmrmod", 0);
740 io->modify_refs = -1;
744 hammer_io_done_interlock(hammer_io_t io)
746 KKASSERT(io->modify_refs == -1);
755 * Caller intends to modify a volume's ondisk structure.
757 * This is only allowed if we are the flusher or we have a ref on the
761 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
764 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
766 hammer_io_modify(&volume->io, 1);
768 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
769 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
770 hammer_generate_undo(trans,
771 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
777 * Caller intends to modify a buffer's ondisk structure.
779 * This is only allowed if we are the flusher or we have a ref on the
783 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
786 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
788 hammer_io_modify(&buffer->io, 1);
790 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
791 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
792 hammer_generate_undo(trans,
793 buffer->zone2_offset + rel_offset,
799 hammer_modify_volume_done(hammer_volume_t volume)
801 hammer_io_modify_done(&volume->io);
805 hammer_modify_buffer_done(hammer_buffer_t buffer)
807 hammer_io_modify_done(&buffer->io);
811 * Mark an entity as not being dirty any more and finalize any
812 * delayed adjustments to the buffer.
814 * Delayed adjustments are an important performance enhancement, allowing
815 * us to avoid recalculating B-Tree node CRCs over and over again when
816 * making bulk-modifications to the B-Tree.
818 * If inval is non-zero delayed adjustments are ignored.
820 * This routine may dereference related btree nodes and cause the
821 * buffer to be dereferenced. The caller must own a reference on io.
824 hammer_io_clear_modify(struct hammer_io *io, int inval)
826 if (io->modified == 0)
830 * Take us off the mod-list and clear the modified bit.
832 KKASSERT(io->mod_list != NULL);
833 if (io->mod_list == &io->hmp->volu_list ||
834 io->mod_list == &io->hmp->meta_list) {
835 io->hmp->locked_dirty_space -= io->bytes;
836 hammer_count_dirtybufspace -= io->bytes;
838 TAILQ_REMOVE(io->mod_list, io, mod_entry);
843 * If this bit is not set there are no delayed adjustments.
850 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
851 * on the node (& underlying buffer). Release the node after clearing
854 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
855 hammer_buffer_t buffer = (void *)io;
859 TAILQ_FOREACH(node, &buffer->clist, entry) {
860 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
862 node->flags &= ~HAMMER_NODE_NEEDSCRC;
863 KKASSERT(node->ondisk);
865 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
866 hammer_rel_node(node);
870 /* caller must still have ref on io */
871 KKASSERT(hammer_isactive(&io->lock));
875 * Clear the IO's modify list. Even though the IO is no longer modified
876 * it may still be on the lose_list. This routine is called just before
877 * the governing hammer_buffer is destroyed.
880 hammer_io_clear_modlist(struct hammer_io *io)
882 KKASSERT(io->modified == 0);
884 crit_enter(); /* biodone race against list */
885 KKASSERT(io->mod_list == &io->hmp->lose_list);
886 TAILQ_REMOVE(io->mod_list, io, mod_entry);
893 hammer_io_set_modlist(struct hammer_io *io)
895 struct hammer_mount *hmp = io->hmp;
897 KKASSERT(io->mod_list == NULL);
900 case HAMMER_STRUCTURE_VOLUME:
901 io->mod_list = &hmp->volu_list;
902 hmp->locked_dirty_space += io->bytes;
903 hammer_count_dirtybufspace += io->bytes;
905 case HAMMER_STRUCTURE_META_BUFFER:
906 io->mod_list = &hmp->meta_list;
907 hmp->locked_dirty_space += io->bytes;
908 hammer_count_dirtybufspace += io->bytes;
910 case HAMMER_STRUCTURE_UNDO_BUFFER:
911 io->mod_list = &hmp->undo_list;
913 case HAMMER_STRUCTURE_DATA_BUFFER:
914 io->mod_list = &hmp->data_list;
916 case HAMMER_STRUCTURE_DUMMY:
917 panic("hammer_io_disassociate: bad io type");
920 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
923 /************************************************************************
925 ************************************************************************
930 * Pre-IO initiation kernel callback - cluster build only
933 hammer_io_start(struct buf *bp)
938 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
940 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
941 * may also be set if we were marking a cluster header open. Only remove
942 * our dependancy if the modified bit is clear.
945 hammer_io_complete(struct buf *bp)
947 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
948 struct hammer_mount *hmp = iou->io.hmp;
949 struct hammer_io *ionext;
951 KKASSERT(iou->io.released == 1);
954 * Deal with people waiting for I/O to drain
956 if (iou->io.running) {
958 * Deal with critical write errors. Once a critical error
959 * has been flagged in hmp the UNDO FIFO will not be updated.
960 * That way crash recover will give us a consistent
963 * Because of this we can throw away failed UNDO buffers. If
964 * we throw away META or DATA buffers we risk corrupting
965 * the now read-only version of the filesystem visible to
966 * the user. Clear B_ERROR so the buffer is not re-dirtied
967 * by the kernel and ref the io so it doesn't get thrown
970 if (bp->b_flags & B_ERROR) {
971 hammer_critical_error(hmp, NULL, bp->b_error,
972 "while flushing meta-data");
973 switch(iou->io.type) {
974 case HAMMER_STRUCTURE_UNDO_BUFFER:
977 if (iou->io.ioerror == 0) {
979 hammer_ref(&iou->io.lock);
983 bp->b_flags &= ~B_ERROR;
986 hammer_io_set_modlist(&iou->io);
987 iou->io.modified = 1;
990 hammer_stats_disk_write += iou->io.bytes;
991 hammer_count_io_running_write -= iou->io.bytes;
992 hmp->io_running_space -= iou->io.bytes;
993 if (hmp->io_running_wakeup &&
994 hmp->io_running_space < hammer_limit_running_io / 2) {
995 hmp->io_running_wakeup = 0;
996 wakeup(&hmp->io_running_wakeup);
998 KKASSERT(hmp->io_running_space >= 0);
1002 * Remove from iorun list and wakeup any multi-io waiter(s).
1004 if (TAILQ_FIRST(&hmp->iorun_list) == &iou->io) {
1005 ionext = TAILQ_NEXT(&iou->io, iorun_entry);
1006 if (ionext && ionext->type == HAMMER_STRUCTURE_DUMMY)
1009 TAILQ_REMOVE(&hmp->iorun_list, &iou->io, iorun_entry);
1011 hammer_stats_disk_read += iou->io.bytes;
1014 if (iou->io.waiting) {
1015 iou->io.waiting = 0;
1020 * If B_LOCKED is set someone wanted to deallocate the bp at some
1021 * point, try to do it now. The operation will fail if there are
1022 * refs or if hammer_io_deallocate() is unable to gain the
1025 if (bp->b_flags & B_LOCKED) {
1026 --hammer_count_io_locked;
1027 bp->b_flags &= ~B_LOCKED;
1028 hammer_io_deallocate(bp);
1029 /* structure may be dead now */
1034 * Callback from kernel when it wishes to deallocate a passively
1035 * associated structure. This mostly occurs with clean buffers
1036 * but it may be possible for a holding structure to be marked dirty
1037 * while its buffer is passively associated. The caller owns the bp.
1039 * If we cannot disassociate we set B_LOCKED to prevent the buffer
1040 * from getting reused.
1042 * WARNING: Because this can be called directly by getnewbuf we cannot
1043 * recurse into the tree. If a bp cannot be immediately disassociated
1044 * our only recourse is to set B_LOCKED.
1046 * WARNING: This may be called from an interrupt via hammer_io_complete()
1049 hammer_io_deallocate(struct buf *bp)
1051 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
1053 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
1054 if (hammer_try_interlock_norefs(&iou->io.lock) == 0) {
1056 * We cannot safely disassociate a bp from a referenced
1057 * or interlocked HAMMER structure.
1059 bp->b_flags |= B_LOCKED;
1060 ++hammer_count_io_locked;
1061 } else if (iou->io.modified) {
1063 * It is not legal to disassociate a modified buffer. This
1064 * case really shouldn't ever occur.
1066 bp->b_flags |= B_LOCKED;
1067 ++hammer_count_io_locked;
1068 hammer_put_interlock(&iou->io.lock, 0);
1071 * Disassociate the BP. If the io has no refs left we
1072 * have to add it to the loose list.
1074 hammer_io_disassociate(iou);
1075 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
1076 KKASSERT(iou->io.bp == NULL);
1077 KKASSERT(iou->io.mod_list == NULL);
1078 crit_enter(); /* biodone race against list */
1079 iou->io.mod_list = &iou->io.hmp->lose_list;
1080 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
1083 hammer_put_interlock(&iou->io.lock, 1);
1088 hammer_io_fsync(struct vnode *vp)
1094 * NOTE: will not be called unless we tell the kernel about the
1095 * bioops. Unused... we use the mount's VFS_SYNC instead.
1098 hammer_io_sync(struct mount *mp)
1104 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
1109 * I/O pre-check for reading and writing. HAMMER only uses this for
1110 * B_CACHE buffers so checkread just shouldn't happen, but if it does
1113 * Writing is a different case. We don't want the kernel to try to write
1114 * out a buffer that HAMMER may be modifying passively or which has a
1115 * dependancy. In addition, kernel-demanded writes can only proceed for
1116 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
1117 * buffer types can only be explicitly written by the flusher.
1119 * checkwrite will only be called for bdwrite()n buffers. If we return
1120 * success the kernel is guaranteed to initiate the buffer write.
1123 hammer_io_checkread(struct buf *bp)
1129 hammer_io_checkwrite(struct buf *bp)
1131 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
1134 * This shouldn't happen under normal operation.
1136 if (io->type == HAMMER_STRUCTURE_VOLUME ||
1137 io->type == HAMMER_STRUCTURE_META_BUFFER) {
1139 panic("hammer_io_checkwrite: illegal buffer");
1140 if ((bp->b_flags & B_LOCKED) == 0) {
1141 bp->b_flags |= B_LOCKED;
1142 ++hammer_count_io_locked;
1148 * We can only clear the modified bit if the IO is not currently
1149 * undergoing modification. Otherwise we may miss changes.
1151 * Only data and undo buffers can reach here. These buffers do
1152 * not have terminal crc functions but we temporarily reference
1153 * the IO anyway, just in case.
1155 if (io->modify_refs == 0 && io->modified) {
1156 hammer_ref(&io->lock);
1157 hammer_io_clear_modify(io, 0);
1158 hammer_rel(&io->lock);
1159 } else if (io->modified) {
1160 KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
1164 * The kernel is going to start the IO, set io->running.
1166 KKASSERT(io->running == 0);
1168 io->hmp->io_running_space += io->bytes;
1169 TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
1170 hammer_count_io_running_write += io->bytes;
1175 * Return non-zero if we wish to delay the kernel's attempt to flush
1176 * this buffer to disk.
1179 hammer_io_countdeps(struct buf *bp, int n)
1184 struct bio_ops hammer_bioops = {
1185 .io_start = hammer_io_start,
1186 .io_complete = hammer_io_complete,
1187 .io_deallocate = hammer_io_deallocate,
1188 .io_fsync = hammer_io_fsync,
1189 .io_sync = hammer_io_sync,
1190 .io_movedeps = hammer_io_movedeps,
1191 .io_countdeps = hammer_io_countdeps,
1192 .io_checkread = hammer_io_checkread,
1193 .io_checkwrite = hammer_io_checkwrite,
1196 /************************************************************************
1198 ************************************************************************
1200 * These functions operate directly on the buffer cache buffer associated
1201 * with a front-end vnode rather then a back-end device vnode.
1205 * Read a buffer associated with a front-end vnode directly from the
1206 * disk media. The bio may be issued asynchronously. If leaf is non-NULL
1207 * we validate the CRC.
1209 * We must check for the presence of a HAMMER buffer to handle the case
1210 * where the reblocker has rewritten the data (which it does via the HAMMER
1211 * buffer system, not via the high-level vnode buffer cache), but not yet
1212 * committed the buffer to the media.
1215 hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1216 hammer_btree_leaf_elm_t leaf)
1218 hammer_off_t buf_offset;
1219 hammer_off_t zone2_offset;
1220 hammer_volume_t volume;
1226 buf_offset = bio->bio_offset;
1227 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1228 HAMMER_ZONE_LARGE_DATA);
1231 * The buffer cache may have an aliased buffer (the reblocker can
1232 * write them). If it does we have to sync any dirty data before
1233 * we can build our direct-read. This is a non-critical code path.
1236 hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
1239 * Resolve to a zone-2 offset. The conversion just requires
1240 * munging the top 4 bits but we want to abstract it anyway
1241 * so the blockmap code can verify the zone assignment.
1243 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1246 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1247 HAMMER_ZONE_RAW_BUFFER);
1250 * Resolve volume and raw-offset for 3rd level bio. The
1251 * offset will be specific to the volume.
1253 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1254 volume = hammer_get_volume(hmp, vol_no, &error);
1255 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1262 nbio = push_bio(bio);
1263 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1264 (zone2_offset & HAMMER_OFF_SHORT_MASK);
1267 * XXX disabled - our CRC check doesn't work if the OS
1268 * does bogus_page replacement on the direct-read.
1270 if (leaf && hammer_verify_data) {
1271 nbio->bio_done = hammer_io_direct_read_complete;
1272 nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1275 hammer_stats_disk_read += bp->b_bufsize;
1276 vn_strategy(volume->devvp, nbio);
1278 hammer_rel_volume(volume, 0);
1281 kprintf("hammer_direct_read: failed @ %016llx\n",
1282 (long long)zone2_offset);
1283 bp->b_error = error;
1284 bp->b_flags |= B_ERROR;
1292 * On completion of the BIO this callback must check the data CRC
1293 * and chain to the previous bio.
1297 hammer_io_direct_read_complete(struct bio *nbio)
1301 u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1304 if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1305 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1306 nbio->bio_offset, bp->b_bufsize);
1307 if (hammer_debug_critical)
1308 Debugger("data_crc on read");
1309 bp->b_flags |= B_ERROR;
1312 obio = pop_bio(nbio);
1318 * Write a buffer associated with a front-end vnode directly to the
1319 * disk media. The bio may be issued asynchronously.
1321 * The BIO is associated with the specified record and RECF_DIRECT_IO
1322 * is set. The recorded is added to its object.
1325 hammer_io_direct_write(hammer_mount_t hmp, struct bio *bio,
1326 hammer_record_t record)
1328 hammer_btree_leaf_elm_t leaf = &record->leaf;
1329 hammer_off_t buf_offset;
1330 hammer_off_t zone2_offset;
1331 hammer_volume_t volume;
1332 hammer_buffer_t buffer;
1339 buf_offset = leaf->data_offset;
1341 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
1342 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1345 * Issue or execute the I/O. The new memory record must replace
1346 * the old one before the I/O completes, otherwise a reaquisition of
1347 * the buffer will load the old media data instead of the new.
1349 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
1350 leaf->data_len >= HAMMER_BUFSIZE) {
1352 * We are using the vnode's bio to write directly to the
1353 * media, any hammer_buffer at the same zone-X offset will
1354 * now have stale data.
1356 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1357 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1358 volume = hammer_get_volume(hmp, vol_no, &error);
1360 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1364 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1366 hammer_del_buffers(hmp, buf_offset,
1367 zone2_offset, bp->b_bufsize);
1371 * Second level bio - cached zone2 offset.
1373 * (We can put our bio_done function in either the
1374 * 2nd or 3rd level).
1376 nbio = push_bio(bio);
1377 nbio->bio_offset = zone2_offset;
1378 nbio->bio_done = hammer_io_direct_write_complete;
1379 nbio->bio_caller_info1.ptr = record;
1380 record->zone2_offset = zone2_offset;
1381 record->flags |= HAMMER_RECF_DIRECT_IO |
1382 HAMMER_RECF_DIRECT_INVAL;
1385 * Third level bio - raw offset specific to the
1388 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1389 nbio = push_bio(nbio);
1390 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1392 hammer_stats_disk_write += bp->b_bufsize;
1393 hammer_ip_replace_bulk(hmp, record);
1394 vn_strategy(volume->devvp, nbio);
1395 hammer_io_flush_mark(volume);
1397 hammer_rel_volume(volume, 0);
1400 * Must fit in a standard HAMMER buffer. In this case all
1401 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1402 * does not need to be set-up.
1404 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1406 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1409 bp->b_flags |= B_AGE;
1410 hammer_io_modify(&buffer->io, 1);
1411 bcopy(bp->b_data, ptr, leaf->data_len);
1412 hammer_io_modify_done(&buffer->io);
1413 hammer_rel_buffer(buffer, 0);
1415 hammer_ip_replace_bulk(hmp, record);
1421 * Major suckage occured. Also note: The record was
1422 * never added to the tree so we do not have to worry
1423 * about the backend.
1425 kprintf("hammer_direct_write: failed @ %016llx\n",
1426 (long long)leaf->data_offset);
1430 bp->b_flags |= B_ERROR;
1432 record->flags |= HAMMER_RECF_DELETED_FE;
1433 hammer_rel_mem_record(record);
1439 * On completion of the BIO this callback must disconnect
1440 * it from the hammer_record and chain to the previous bio.
1442 * An I/O error forces the mount to read-only. Data buffers
1443 * are not B_LOCKED like meta-data buffers are, so we have to
1444 * throw the buffer away to prevent the kernel from retrying.
1448 hammer_io_direct_write_complete(struct bio *nbio)
1452 hammer_record_t record = nbio->bio_caller_info1.ptr;
1455 obio = pop_bio(nbio);
1456 if (bp->b_flags & B_ERROR) {
1457 hammer_critical_error(record->ip->hmp, record->ip,
1459 "while writing bulk data");
1460 bp->b_flags |= B_INVAL;
1464 KKASSERT(record != NULL);
1465 KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1466 if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1467 record->flags &= ~(HAMMER_RECF_DIRECT_IO |
1468 HAMMER_RECF_DIRECT_WAIT);
1469 /* record can disappear once DIRECT_IO flag is cleared */
1470 wakeup(&record->flags);
1472 record->flags &= ~HAMMER_RECF_DIRECT_IO;
1473 /* record can disappear once DIRECT_IO flag is cleared */
1479 * This is called before a record is either committed to the B-Tree
1480 * or destroyed, to resolve any associated direct-IO.
1482 * (1) We must wait for any direct-IO related to the record to complete.
1484 * (2) We must remove any buffer cache aliases for data accessed via
1485 * leaf->data_offset or zone2_offset so non-direct-IO consumers
1486 * (the mirroring and reblocking code) do not see stale data.
1489 hammer_io_direct_wait(hammer_record_t record)
1492 * Wait for I/O to complete
1494 if (record->flags & HAMMER_RECF_DIRECT_IO) {
1496 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1497 record->flags |= HAMMER_RECF_DIRECT_WAIT;
1498 tsleep(&record->flags, 0, "hmdiow", 0);
1504 * Invalidate any related buffer cache aliases associated with the
1505 * backing device. This is needed because the buffer cache buffer
1506 * for file data is associated with the file vnode, not the backing
1509 * XXX I do not think this case can occur any more now that
1510 * reservations ensure that all such buffers are removed before
1511 * an area can be reused.
1513 if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1514 KKASSERT(record->leaf.data_offset);
1515 hammer_del_buffers(record->ip->hmp, record->leaf.data_offset,
1516 record->zone2_offset, record->leaf.data_len,
1518 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1523 * This is called to remove the second-level cached zone-2 offset from
1524 * frontend buffer cache buffers, now stale due to a data relocation.
1525 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1526 * by hammer_vop_strategy_read().
1528 * This is rather nasty because here we have something like the reblocker
1529 * scanning the raw B-Tree with no held references on anything, really,
1530 * other then a shared lock on the B-Tree node, and we have to access the
1531 * frontend's buffer cache to check for and clean out the association.
1532 * Specifically, if the reblocker is moving data on the disk, these cached
1533 * offsets will become invalid.
1535 * Only data record types associated with the large-data zone are subject
1536 * to direct-io and need to be checked.
1540 hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1542 struct hammer_inode_info iinfo;
1545 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1547 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1548 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1550 iinfo.obj_id = leaf->base.obj_id;
1551 iinfo.obj_asof = 0; /* unused */
1552 iinfo.obj_localization = leaf->base.localization &
1553 HAMMER_LOCALIZE_PSEUDOFS_MASK;
1554 iinfo.u.leaf = leaf;
1555 hammer_scan_inode_snapshots(hmp, &iinfo,
1556 hammer_io_direct_uncache_callback,
1561 hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1563 hammer_inode_info_t iinfo = data;
1564 hammer_off_t data_offset;
1565 hammer_off_t file_offset;
1572 data_offset = iinfo->u.leaf->data_offset;
1573 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1574 blksize = iinfo->u.leaf->data_len;
1575 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1577 hammer_ref(&ip->lock);
1578 if (hammer_get_vnode(ip, &vp) == 0) {
1579 if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
1580 bp->b_bio2.bio_offset != NOOFFSET) {
1581 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1582 bp->b_bio2.bio_offset = NOOFFSET;
1587 hammer_rel_inode(ip, 0);
1593 * This function is called when writes may have occured on the volume,
1594 * indicating that the device may be holding cached writes.
1597 hammer_io_flush_mark(hammer_volume_t volume)
1599 volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1603 * This function ensures that the device has flushed any cached writes out.
1606 hammer_io_flush_sync(hammer_mount_t hmp)
1608 hammer_volume_t volume;
1609 struct buf *bp_base = NULL;
1612 RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1613 if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1614 volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1616 bp->b_bio1.bio_offset = 0;
1619 bp->b_cmd = BUF_CMD_FLUSH;
1620 bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1621 bp->b_bio1.bio_done = biodone_sync;
1622 bp->b_bio1.bio_flags |= BIO_SYNC;
1624 vn_strategy(volume->devvp, &bp->b_bio1);
1627 while ((bp = bp_base) != NULL) {
1628 bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1629 biowait(&bp->b_bio1, "hmrFLS");
1635 * Limit the amount of backlog which we allow to build up
1638 hammer_io_limit_backlog(hammer_mount_t hmp)
1640 while (hmp->io_running_space > hammer_limit_running_io) {
1641 hmp->io_running_wakeup = 1;
1642 tsleep(&hmp->io_running_wakeup, 0, "hmiolm", hz / 10);