xref: /dflybsd-src/sys/vfs/hammer/hammer_io.c (revision b1c20cfa90fe1b1b6ab1a34b5ea98c33b5336ec9)
166325755SMatthew Dillon /*
2b84de5afSMatthew Dillon  * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
366325755SMatthew Dillon  *
466325755SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
566325755SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
666325755SMatthew Dillon  *
766325755SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
866325755SMatthew Dillon  * modification, are permitted provided that the following conditions
966325755SMatthew Dillon  * are met:
1066325755SMatthew Dillon  *
1166325755SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
1266325755SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1366325755SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
1466325755SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
1566325755SMatthew Dillon  *    the documentation and/or other materials provided with the
1666325755SMatthew Dillon  *    distribution.
1766325755SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
1866325755SMatthew Dillon  *    contributors may be used to endorse or promote products derived
1966325755SMatthew Dillon  *    from this software without specific, prior written permission.
2066325755SMatthew Dillon  *
2166325755SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2266325755SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2366325755SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2466325755SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2566325755SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2666325755SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2766325755SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2866325755SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2966325755SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3066325755SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3166325755SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3266325755SMatthew Dillon  * SUCH DAMAGE.
3366325755SMatthew Dillon  *
34e83ca595SMatthew Dillon  * $DragonFly: src/sys/vfs/hammer/hammer_io.c,v 1.55 2008/09/15 17:02:49 dillon Exp $
3566325755SMatthew Dillon  */
3666325755SMatthew Dillon /*
3766325755SMatthew Dillon  * IO Primitives and buffer cache management
3866325755SMatthew Dillon  *
3966325755SMatthew Dillon  * All major data-tracking structures in HAMMER contain a struct hammer_io
4066325755SMatthew Dillon  * which is used to manage their backing store.  We use filesystem buffers
4166325755SMatthew Dillon  * for backing store and we leave them passively associated with their
4266325755SMatthew Dillon  * HAMMER structures.
4366325755SMatthew Dillon  *
449f5097dcSMatthew Dillon  * If the kernel tries to destroy a passively associated buf which we cannot
4566325755SMatthew Dillon  * yet let go we set B_LOCKED in the buffer and then actively released it
4666325755SMatthew Dillon  * later when we can.
4766325755SMatthew Dillon  */
4866325755SMatthew Dillon 
4966325755SMatthew Dillon #include "hammer.h"
5066325755SMatthew Dillon #include <sys/fcntl.h>
5166325755SMatthew Dillon #include <sys/nlookup.h>
5266325755SMatthew Dillon #include <sys/buf.h>
5366325755SMatthew Dillon #include <sys/buf2.h>
5466325755SMatthew Dillon 
5510a5d1baSMatthew Dillon static void hammer_io_modify(hammer_io_t io, int count);
56055f5ff8SMatthew Dillon static void hammer_io_deallocate(struct buf *bp);
571b0ab2c3SMatthew Dillon #if 0
581b0ab2c3SMatthew Dillon static void hammer_io_direct_read_complete(struct bio *nbio);
591b0ab2c3SMatthew Dillon #endif
601b0ab2c3SMatthew Dillon static void hammer_io_direct_write_complete(struct bio *nbio);
6143c665aeSMatthew Dillon static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
62cdb6e4e6SMatthew Dillon static void hammer_io_set_modlist(struct hammer_io *io);
63748efb59SMatthew Dillon static void hammer_io_flush_mark(hammer_volume_t volume);
64748efb59SMatthew Dillon static void hammer_io_flush_sync_done(struct bio *bio);
65748efb59SMatthew Dillon 
66055f5ff8SMatthew Dillon 
67055f5ff8SMatthew Dillon /*
6810a5d1baSMatthew Dillon  * Initialize a new, already-zero'd hammer_io structure, or reinitialize
6910a5d1baSMatthew Dillon  * an existing hammer_io structure which may have switched to another type.
70055f5ff8SMatthew Dillon  */
71055f5ff8SMatthew Dillon void
72748efb59SMatthew Dillon hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
73055f5ff8SMatthew Dillon {
74748efb59SMatthew Dillon 	io->volume = volume;
75748efb59SMatthew Dillon 	io->hmp = volume->io.hmp;
76055f5ff8SMatthew Dillon 	io->type = type;
77055f5ff8SMatthew Dillon }
78055f5ff8SMatthew Dillon 
7966325755SMatthew Dillon /*
80fbc6e32aSMatthew Dillon  * Helper routine to disassociate a buffer cache buffer from an I/O
81ecca949aSMatthew Dillon  * structure.  The buffer is unlocked and marked appropriate for reclamation.
82055f5ff8SMatthew Dillon  *
83055f5ff8SMatthew Dillon  * The io may have 0 or 1 references depending on who called us.  The
84055f5ff8SMatthew Dillon  * caller is responsible for dealing with the refs.
85055f5ff8SMatthew Dillon  *
86055f5ff8SMatthew Dillon  * This call can only be made when no action is required on the buffer.
87ecca949aSMatthew Dillon  *
88ecca949aSMatthew Dillon  * The caller must own the buffer and the IO must indicate that the
89ecca949aSMatthew Dillon  * structure no longer owns it (io.released != 0).
9066325755SMatthew Dillon  */
9166325755SMatthew Dillon static void
92ecca949aSMatthew Dillon hammer_io_disassociate(hammer_io_structure_t iou)
9366325755SMatthew Dillon {
94055f5ff8SMatthew Dillon 	struct buf *bp = iou->io.bp;
9566325755SMatthew Dillon 
96ecca949aSMatthew Dillon 	KKASSERT(iou->io.released);
97b58c6388SMatthew Dillon 	KKASSERT(iou->io.modified == 0);
98af209b0fSMatthew Dillon 	KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
994d75d829SMatthew Dillon 	buf_dep_init(bp);
100055f5ff8SMatthew Dillon 	iou->io.bp = NULL;
1019f5097dcSMatthew Dillon 
1029f5097dcSMatthew Dillon 	/*
1039f5097dcSMatthew Dillon 	 * If the buffer was locked someone wanted to get rid of it.
1049f5097dcSMatthew Dillon 	 */
105a99b9ea2SMatthew Dillon 	if (bp->b_flags & B_LOCKED) {
106a99b9ea2SMatthew Dillon 		--hammer_count_io_locked;
107d8971d2bSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
108a99b9ea2SMatthew Dillon 	}
109ecca949aSMatthew Dillon 	if (iou->io.reclaim) {
110cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
111cebe9493SMatthew Dillon 		iou->io.reclaim = 0;
112ecca949aSMatthew Dillon 	}
11366325755SMatthew Dillon 
114055f5ff8SMatthew Dillon 	switch(iou->io.type) {
11566325755SMatthew Dillon 	case HAMMER_STRUCTURE_VOLUME:
116055f5ff8SMatthew Dillon 		iou->volume.ondisk = NULL;
11766325755SMatthew Dillon 		break;
11810a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_DATA_BUFFER:
11910a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_META_BUFFER:
12010a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_UNDO_BUFFER:
121055f5ff8SMatthew Dillon 		iou->buffer.ondisk = NULL;
12266325755SMatthew Dillon 		break;
12366325755SMatthew Dillon 	}
12466325755SMatthew Dillon }
125fbc6e32aSMatthew Dillon 
126fbc6e32aSMatthew Dillon /*
127055f5ff8SMatthew Dillon  * Wait for any physical IO to complete
128fbc6e32aSMatthew Dillon  */
1291b0ab2c3SMatthew Dillon void
130055f5ff8SMatthew Dillon hammer_io_wait(hammer_io_t io)
131fbc6e32aSMatthew Dillon {
132055f5ff8SMatthew Dillon 	if (io->running) {
133055f5ff8SMatthew Dillon 		crit_enter();
134055f5ff8SMatthew Dillon 		tsleep_interlock(io);
135055f5ff8SMatthew Dillon 		io->waiting = 1;
136055f5ff8SMatthew Dillon 		for (;;) {
137055f5ff8SMatthew Dillon 			tsleep(io, 0, "hmrflw", 0);
138055f5ff8SMatthew Dillon 			if (io->running == 0)
139055f5ff8SMatthew Dillon 				break;
140055f5ff8SMatthew Dillon 			tsleep_interlock(io);
141055f5ff8SMatthew Dillon 			io->waiting = 1;
142055f5ff8SMatthew Dillon 			if (io->running == 0)
143055f5ff8SMatthew Dillon 				break;
144055f5ff8SMatthew Dillon 		}
145055f5ff8SMatthew Dillon 		crit_exit();
146055f5ff8SMatthew Dillon 	}
147055f5ff8SMatthew Dillon }
148055f5ff8SMatthew Dillon 
149af209b0fSMatthew Dillon /*
150af209b0fSMatthew Dillon  * Wait for all hammer_io-initated write I/O's to complete.  This is not
151af209b0fSMatthew Dillon  * supposed to count direct I/O's but some can leak through (for
152af209b0fSMatthew Dillon  * non-full-sized direct I/Os).
153af209b0fSMatthew Dillon  */
154af209b0fSMatthew Dillon void
155af209b0fSMatthew Dillon hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
156af209b0fSMatthew Dillon {
157748efb59SMatthew Dillon 	hammer_io_flush_sync(hmp);
158af209b0fSMatthew Dillon 	crit_enter();
159f5a07a7aSMatthew Dillon 	while (hmp->io_running_space)
160f5a07a7aSMatthew Dillon 		tsleep(&hmp->io_running_space, 0, ident, 0);
161af209b0fSMatthew Dillon 	crit_exit();
162af209b0fSMatthew Dillon }
163af209b0fSMatthew Dillon 
1642f85fa4dSMatthew Dillon #define HAMMER_MAXRA	4
1652f85fa4dSMatthew Dillon 
16661aeeb33SMatthew Dillon /*
16710a5d1baSMatthew Dillon  * Load bp for a HAMMER structure.  The io must be exclusively locked by
16810a5d1baSMatthew Dillon  * the caller.
1692f85fa4dSMatthew Dillon  *
170a99b9ea2SMatthew Dillon  * This routine is mostly used on meta-data and small-data blocks.  Generally
171a99b9ea2SMatthew Dillon  * speaking HAMMER assumes some locality of reference and will cluster
172a99b9ea2SMatthew Dillon  * a 64K read.
173af209b0fSMatthew Dillon  *
174af209b0fSMatthew Dillon  * Note that clustering occurs at the device layer, not the logical layer.
175af209b0fSMatthew Dillon  * If the buffers do not apply to the current operation they may apply to
176af209b0fSMatthew Dillon  * some other.
17766325755SMatthew Dillon  */
17866325755SMatthew Dillon int
1792f85fa4dSMatthew Dillon hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
18066325755SMatthew Dillon {
18166325755SMatthew Dillon 	struct buf *bp;
18266325755SMatthew Dillon 	int   error;
18366325755SMatthew Dillon 
18466325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
185f5a07a7aSMatthew Dillon 		hammer_count_io_running_read += io->bytes;
186ce0138a6SMatthew Dillon 		if (hammer_cluster_enable) {
187ce0138a6SMatthew Dillon 			error = cluster_read(devvp, limit,
188ce0138a6SMatthew Dillon 					     io->offset, io->bytes,
189af209b0fSMatthew Dillon 					     HAMMER_CLUSTER_SIZE,
190af209b0fSMatthew Dillon 					     HAMMER_CLUSTER_BUFS, &io->bp);
191ce0138a6SMatthew Dillon 		} else {
1924a2796f3SMatthew Dillon 			error = bread(devvp, io->offset, io->bytes, &io->bp);
193ce0138a6SMatthew Dillon 		}
194ce0138a6SMatthew Dillon 		hammer_stats_disk_read += io->bytes;
195f5a07a7aSMatthew Dillon 		hammer_count_io_running_read -= io->bytes;
196cdb6e4e6SMatthew Dillon 
197cdb6e4e6SMatthew Dillon 		/*
198cdb6e4e6SMatthew Dillon 		 * The code generally assumes b_ops/b_dep has been set-up,
199cdb6e4e6SMatthew Dillon 		 * even if we error out here.
200cdb6e4e6SMatthew Dillon 		 */
20166325755SMatthew Dillon 		bp = io->bp;
20266325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
203af209b0fSMatthew Dillon 		KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
20466325755SMatthew Dillon 		LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
20566325755SMatthew Dillon 		BUF_KERNPROC(bp);
20610a5d1baSMatthew Dillon 		KKASSERT(io->modified == 0);
20710a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
20810a5d1baSMatthew Dillon 		KKASSERT(io->waiting == 0);
20966325755SMatthew Dillon 		io->released = 0;	/* we hold an active lock on bp */
21066325755SMatthew Dillon 	} else {
21166325755SMatthew Dillon 		error = 0;
21266325755SMatthew Dillon 	}
21366325755SMatthew Dillon 	return(error);
21466325755SMatthew Dillon }
21566325755SMatthew Dillon 
21666325755SMatthew Dillon /*
21766325755SMatthew Dillon  * Similar to hammer_io_read() but returns a zero'd out buffer instead.
21810a5d1baSMatthew Dillon  * Must be called with the IO exclusively locked.
219055f5ff8SMatthew Dillon  *
22010a5d1baSMatthew Dillon  * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
22110a5d1baSMatthew Dillon  * I/O by forcing the buffer to not be in a released state before calling
22210a5d1baSMatthew Dillon  * it.
22310a5d1baSMatthew Dillon  *
22410a5d1baSMatthew Dillon  * This function will also mark the IO as modified but it will not
22510a5d1baSMatthew Dillon  * increment the modify_refs count.
22666325755SMatthew Dillon  */
22766325755SMatthew Dillon int
22866325755SMatthew Dillon hammer_io_new(struct vnode *devvp, struct hammer_io *io)
22966325755SMatthew Dillon {
23066325755SMatthew Dillon 	struct buf *bp;
23166325755SMatthew Dillon 
23266325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
2334a2796f3SMatthew Dillon 		io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
23466325755SMatthew Dillon 		bp = io->bp;
23566325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
236af209b0fSMatthew Dillon 		KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
23766325755SMatthew Dillon 		LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
238055f5ff8SMatthew Dillon 		io->released = 0;
23910a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
240055f5ff8SMatthew Dillon 		io->waiting = 0;
24166325755SMatthew Dillon 		BUF_KERNPROC(bp);
24266325755SMatthew Dillon 	} else {
24366325755SMatthew Dillon 		if (io->released) {
24466325755SMatthew Dillon 			regetblk(bp);
24566325755SMatthew Dillon 			BUF_KERNPROC(bp);
246d113fda1SMatthew Dillon 			io->released = 0;
24766325755SMatthew Dillon 		}
24866325755SMatthew Dillon 	}
24910a5d1baSMatthew Dillon 	hammer_io_modify(io, 0);
25066325755SMatthew Dillon 	vfs_bio_clrbuf(bp);
25166325755SMatthew Dillon 	return(0);
25266325755SMatthew Dillon }
25366325755SMatthew Dillon 
25466325755SMatthew Dillon /*
25547637bffSMatthew Dillon  * Remove potential device level aliases against buffers managed by high level
256362ec2dcSMatthew Dillon  * vnodes.  Aliases can also be created due to mixed buffer sizes or via
257362ec2dcSMatthew Dillon  * direct access to the backing store device.
258e469566bSMatthew Dillon  *
259e469566bSMatthew Dillon  * This is nasty because the buffers are also VMIO-backed.  Even if a buffer
260e469566bSMatthew Dillon  * does not exist its backing VM pages might, and we have to invalidate
261e469566bSMatthew Dillon  * those as well or a getblk() will reinstate them.
262362ec2dcSMatthew Dillon  *
263362ec2dcSMatthew Dillon  * Buffer cache buffers associated with hammer_buffers cannot be
264362ec2dcSMatthew Dillon  * invalidated.
26547637bffSMatthew Dillon  */
266362ec2dcSMatthew Dillon int
26747637bffSMatthew Dillon hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
26847637bffSMatthew Dillon {
269cebe9493SMatthew Dillon 	hammer_io_structure_t iou;
27047637bffSMatthew Dillon 	hammer_off_t phys_offset;
27147637bffSMatthew Dillon 	struct buf *bp;
272362ec2dcSMatthew Dillon 	int error;
27347637bffSMatthew Dillon 
27447637bffSMatthew Dillon 	phys_offset = volume->ondisk->vol_buf_beg +
27547637bffSMatthew Dillon 		      (zone2_offset & HAMMER_OFF_SHORT_MASK);
2764a2796f3SMatthew Dillon 	crit_enter();
277*b1c20cfaSMatthew Dillon 	if ((bp = findblk(volume->devvp, phys_offset, FINDBLK_TEST)) != NULL)
2784a2796f3SMatthew Dillon 		bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
279e469566bSMatthew Dillon 	else
280e469566bSMatthew Dillon 		bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
281cebe9493SMatthew Dillon 	if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
282362ec2dcSMatthew Dillon #if 0
2835c8d05e2SMatthew Dillon 		hammer_ref(&iou->io.lock);
2844a2796f3SMatthew Dillon 		hammer_io_clear_modify(&iou->io, 1);
285cebe9493SMatthew Dillon 		bundirty(bp);
286e83ca595SMatthew Dillon 		iou->io.released = 0;
287e83ca595SMatthew Dillon 		BUF_KERNPROC(bp);
288cebe9493SMatthew Dillon 		iou->io.reclaim = 1;
2895c8d05e2SMatthew Dillon 		iou->io.waitdep = 1;
290e83ca595SMatthew Dillon 		KKASSERT(iou->io.lock.refs == 1);
2915c8d05e2SMatthew Dillon 		hammer_rel_buffer(&iou->buffer, 0);
2925c8d05e2SMatthew Dillon 		/*hammer_io_deallocate(bp);*/
293362ec2dcSMatthew Dillon #endif
29404b04ca6SMatthew Dillon 		bqrelse(bp);
295362ec2dcSMatthew Dillon 		error = EAGAIN;
2960832c9bbSMatthew Dillon 	} else {
297cebe9493SMatthew Dillon 		KKASSERT((bp->b_flags & B_LOCKED) == 0);
298cebe9493SMatthew Dillon 		bundirty(bp);
299cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
300ecca949aSMatthew Dillon 		brelse(bp);
301362ec2dcSMatthew Dillon 		error = 0;
302e83ca595SMatthew Dillon 	}
3034a2796f3SMatthew Dillon 	crit_exit();
304362ec2dcSMatthew Dillon 	return(error);
3050832c9bbSMatthew Dillon }
30647637bffSMatthew Dillon 
30747637bffSMatthew Dillon /*
308b3deaf57SMatthew Dillon  * This routine is called on the last reference to a hammer structure.
309ecca949aSMatthew Dillon  * The io is usually interlocked with io.loading and io.refs must be 1.
310b3deaf57SMatthew Dillon  *
311ecca949aSMatthew Dillon  * This routine may return a non-NULL bp to the caller for dispoal.  Disposal
312ecca949aSMatthew Dillon  * simply means the caller finishes decrementing the ref-count on the
313ecca949aSMatthew Dillon  * IO structure then brelse()'s the bp.  The bp may or may not still be
314ecca949aSMatthew Dillon  * passively associated with the IO.
315ecca949aSMatthew Dillon  *
316ecca949aSMatthew Dillon  * The only requirement here is that modified meta-data and volume-header
317ecca949aSMatthew Dillon  * buffer may NOT be disassociated from the IO structure, and consequently
318ecca949aSMatthew Dillon  * we also leave such buffers actively associated with the IO if they already
319ecca949aSMatthew Dillon  * are (since the kernel can't do anything with them anyway).  Only the
320ecca949aSMatthew Dillon  * flusher is allowed to write such buffers out.  Modified pure-data and
321ecca949aSMatthew Dillon  * undo buffers are returned to the kernel but left passively associated
322ecca949aSMatthew Dillon  * so we can track when the kernel writes the bp out.
32366325755SMatthew Dillon  */
324ecca949aSMatthew Dillon struct buf *
32509ac686bSMatthew Dillon hammer_io_release(struct hammer_io *io, int flush)
32666325755SMatthew Dillon {
3279f5097dcSMatthew Dillon 	union hammer_io_structure *iou = (void *)io;
32866325755SMatthew Dillon 	struct buf *bp;
32966325755SMatthew Dillon 
330fbc6e32aSMatthew Dillon 	if ((bp = io->bp) == NULL)
331ecca949aSMatthew Dillon 		return(NULL);
332fbc6e32aSMatthew Dillon 
3330b075555SMatthew Dillon 	/*
33410a5d1baSMatthew Dillon 	 * Try to flush a dirty IO to disk if asked to by the
33510a5d1baSMatthew Dillon 	 * caller or if the kernel tried to flush the buffer in the past.
3360b075555SMatthew Dillon 	 *
33710a5d1baSMatthew Dillon 	 * Kernel-initiated flushes are only allowed for pure-data buffers.
33810a5d1baSMatthew Dillon 	 * meta-data and volume buffers can only be flushed explicitly
33910a5d1baSMatthew Dillon 	 * by HAMMER.
340055f5ff8SMatthew Dillon 	 */
34110a5d1baSMatthew Dillon 	if (io->modified) {
34209ac686bSMatthew Dillon 		if (flush) {
343055f5ff8SMatthew Dillon 			hammer_io_flush(io);
34410a5d1baSMatthew Dillon 		} else if (bp->b_flags & B_LOCKED) {
34510a5d1baSMatthew Dillon 			switch(io->type) {
34610a5d1baSMatthew Dillon 			case HAMMER_STRUCTURE_DATA_BUFFER:
34710a5d1baSMatthew Dillon 			case HAMMER_STRUCTURE_UNDO_BUFFER:
34810a5d1baSMatthew Dillon 				hammer_io_flush(io);
34910a5d1baSMatthew Dillon 				break;
35010a5d1baSMatthew Dillon 			default:
35110a5d1baSMatthew Dillon 				break;
35210a5d1baSMatthew Dillon 			}
35310a5d1baSMatthew Dillon 		} /* else no explicit request to flush the buffer */
35410a5d1baSMatthew Dillon 	}
355055f5ff8SMatthew Dillon 
356055f5ff8SMatthew Dillon 	/*
3575c8d05e2SMatthew Dillon 	 * Wait for the IO to complete if asked to.  This occurs when
3585c8d05e2SMatthew Dillon 	 * the buffer must be disposed of definitively during an umount
3595c8d05e2SMatthew Dillon 	 * or buffer invalidation.
360055f5ff8SMatthew Dillon 	 */
361b58c6388SMatthew Dillon 	if (io->waitdep && io->running) {
362055f5ff8SMatthew Dillon 		hammer_io_wait(io);
363055f5ff8SMatthew Dillon 	}
364055f5ff8SMatthew Dillon 
365055f5ff8SMatthew Dillon 	/*
36610a5d1baSMatthew Dillon 	 * Return control of the buffer to the kernel (with the provisio
36710a5d1baSMatthew Dillon 	 * that our bioops can override kernel decisions with regards to
36810a5d1baSMatthew Dillon 	 * the buffer).
369055f5ff8SMatthew Dillon 	 */
370cebe9493SMatthew Dillon 	if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
37110a5d1baSMatthew Dillon 		/*
37210a5d1baSMatthew Dillon 		 * Always disassociate the bp if an explicit flush
37310a5d1baSMatthew Dillon 		 * was requested and the IO completed with no error
37410a5d1baSMatthew Dillon 		 * (so unmount can really clean up the structure).
37510a5d1baSMatthew Dillon 		 */
376055f5ff8SMatthew Dillon 		if (io->released) {
377055f5ff8SMatthew Dillon 			regetblk(bp);
37846fe7ae1SMatthew Dillon 			BUF_KERNPROC(bp);
379ecca949aSMatthew Dillon 		} else {
380ecca949aSMatthew Dillon 			io->released = 1;
381055f5ff8SMatthew Dillon 		}
382ecca949aSMatthew Dillon 		hammer_io_disassociate((hammer_io_structure_t)io);
383ecca949aSMatthew Dillon 		/* return the bp */
384055f5ff8SMatthew Dillon 	} else if (io->modified) {
38510a5d1baSMatthew Dillon 		/*
386ecca949aSMatthew Dillon 		 * Only certain IO types can be released to the kernel if
387ecca949aSMatthew Dillon 		 * the buffer has been modified.
388ecca949aSMatthew Dillon 		 *
389ecca949aSMatthew Dillon 		 * volume and meta-data IO types may only be explicitly
390ecca949aSMatthew Dillon 		 * flushed by HAMMER.
39110a5d1baSMatthew Dillon 		 */
39210a5d1baSMatthew Dillon 		switch(io->type) {
39310a5d1baSMatthew Dillon 		case HAMMER_STRUCTURE_DATA_BUFFER:
39410a5d1baSMatthew Dillon 		case HAMMER_STRUCTURE_UNDO_BUFFER:
395b58c6388SMatthew Dillon 			if (io->released == 0) {
396055f5ff8SMatthew Dillon 				io->released = 1;
397055f5ff8SMatthew Dillon 				bdwrite(bp);
398055f5ff8SMatthew Dillon 			}
39910a5d1baSMatthew Dillon 			break;
40010a5d1baSMatthew Dillon 		default:
40110a5d1baSMatthew Dillon 			break;
40210a5d1baSMatthew Dillon 		}
403ecca949aSMatthew Dillon 		bp = NULL;	/* bp left associated */
404055f5ff8SMatthew Dillon 	} else if (io->released == 0) {
40510a5d1baSMatthew Dillon 		/*
40610a5d1baSMatthew Dillon 		 * Clean buffers can be generally released to the kernel.
40710a5d1baSMatthew Dillon 		 * We leave the bp passively associated with the HAMMER
40810a5d1baSMatthew Dillon 		 * structure and use bioops to disconnect it later on
40910a5d1baSMatthew Dillon 		 * if the kernel wants to discard the buffer.
410ecca949aSMatthew Dillon 		 *
411ecca949aSMatthew Dillon 		 * We can steal the structure's ownership of the bp.
41210a5d1baSMatthew Dillon 		 */
413ecca949aSMatthew Dillon 		io->released = 1;
4149f5097dcSMatthew Dillon 		if (bp->b_flags & B_LOCKED) {
415ecca949aSMatthew Dillon 			hammer_io_disassociate(iou);
416ecca949aSMatthew Dillon 			/* return the bp */
4179f5097dcSMatthew Dillon 		} else {
418cebe9493SMatthew Dillon 			if (io->reclaim) {
419ecca949aSMatthew Dillon 				hammer_io_disassociate(iou);
420ecca949aSMatthew Dillon 				/* return the bp */
421cebe9493SMatthew Dillon 			} else {
422ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
4239f5097dcSMatthew Dillon 			}
424cebe9493SMatthew Dillon 		}
42519b97e01SMatthew Dillon 	} else {
42619b97e01SMatthew Dillon 		/*
427af209b0fSMatthew Dillon 		 * A released buffer is passively associate with our
428af209b0fSMatthew Dillon 		 * hammer_io structure.  The kernel cannot destroy it
429af209b0fSMatthew Dillon 		 * without making a bioops call.  If the kernel (B_LOCKED)
430af209b0fSMatthew Dillon 		 * or we (reclaim) requested that the buffer be destroyed
431af209b0fSMatthew Dillon 		 * we destroy it, otherwise we do a quick get/release to
432af209b0fSMatthew Dillon 		 * reset its position in the kernel's LRU list.
433af209b0fSMatthew Dillon 		 *
434af209b0fSMatthew Dillon 		 * Leaving the buffer passively associated allows us to
435af209b0fSMatthew Dillon 		 * use the kernel's LRU buffer flushing mechanisms rather
436af209b0fSMatthew Dillon 		 * then rolling our own.
437cb51be26SMatthew Dillon 		 *
438cb51be26SMatthew Dillon 		 * XXX there are two ways of doing this.  We can re-acquire
439cb51be26SMatthew Dillon 		 * and passively release to reset the LRU, or not.
44019b97e01SMatthew Dillon 		 */
441af209b0fSMatthew Dillon 		if (io->running == 0) {
44219b97e01SMatthew Dillon 			regetblk(bp);
443cebe9493SMatthew Dillon 			if ((bp->b_flags & B_LOCKED) || io->reclaim) {
444ecca949aSMatthew Dillon 				hammer_io_disassociate(iou);
445ecca949aSMatthew Dillon 				/* return the bp */
4469f5097dcSMatthew Dillon 			} else {
447ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
448ecca949aSMatthew Dillon 			}
449ecca949aSMatthew Dillon 		} else {
450ecca949aSMatthew Dillon 			/*
451ecca949aSMatthew Dillon 			 * bp is left passively associated but we do not
452ecca949aSMatthew Dillon 			 * try to reacquire it.  Interactions with the io
453ecca949aSMatthew Dillon 			 * structure will occur on completion of the bp's
454ecca949aSMatthew Dillon 			 * I/O.
455ecca949aSMatthew Dillon 			 */
456ecca949aSMatthew Dillon 			bp = NULL;
45719b97e01SMatthew Dillon 		}
4589f5097dcSMatthew Dillon 	}
459ecca949aSMatthew Dillon 	return(bp);
460055f5ff8SMatthew Dillon }
461055f5ff8SMatthew Dillon 
462055f5ff8SMatthew Dillon /*
463b33e2cc0SMatthew Dillon  * This routine is called with a locked IO when a flush is desired and
464b33e2cc0SMatthew Dillon  * no other references to the structure exists other then ours.  This
465b33e2cc0SMatthew Dillon  * routine is ONLY called when HAMMER believes it is safe to flush a
466b33e2cc0SMatthew Dillon  * potentially modified buffer out.
4670b075555SMatthew Dillon  */
4680b075555SMatthew Dillon void
469055f5ff8SMatthew Dillon hammer_io_flush(struct hammer_io *io)
4700b075555SMatthew Dillon {
471055f5ff8SMatthew Dillon 	struct buf *bp;
472055f5ff8SMatthew Dillon 
473055f5ff8SMatthew Dillon 	/*
47410a5d1baSMatthew Dillon 	 * Degenerate case - nothing to flush if nothing is dirty.
475055f5ff8SMatthew Dillon 	 */
476b58c6388SMatthew Dillon 	if (io->modified == 0) {
477055f5ff8SMatthew Dillon 		return;
478b58c6388SMatthew Dillon 	}
479055f5ff8SMatthew Dillon 
480055f5ff8SMatthew Dillon 	KKASSERT(io->bp);
4819f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs <= 0);
482055f5ff8SMatthew Dillon 
483b33e2cc0SMatthew Dillon 	/*
48477062c8aSMatthew Dillon 	 * Acquire ownership of the bp, particularly before we clear our
48577062c8aSMatthew Dillon 	 * modified flag.
48677062c8aSMatthew Dillon 	 *
48777062c8aSMatthew Dillon 	 * We are going to bawrite() this bp.  Don't leave a window where
48877062c8aSMatthew Dillon 	 * io->released is set, we actually own the bp rather then our
48977062c8aSMatthew Dillon 	 * buffer.
49077062c8aSMatthew Dillon 	 */
49177062c8aSMatthew Dillon 	bp = io->bp;
49277062c8aSMatthew Dillon 	if (io->released) {
49377062c8aSMatthew Dillon 		regetblk(bp);
49477062c8aSMatthew Dillon 		/* BUF_KERNPROC(io->bp); */
49577062c8aSMatthew Dillon 		/* io->released = 0; */
49677062c8aSMatthew Dillon 		KKASSERT(io->released);
49777062c8aSMatthew Dillon 		KKASSERT(io->bp == bp);
49877062c8aSMatthew Dillon 	}
49977062c8aSMatthew Dillon 	io->released = 1;
50077062c8aSMatthew Dillon 
50177062c8aSMatthew Dillon 	/*
50210a5d1baSMatthew Dillon 	 * Acquire exclusive access to the bp and then clear the modified
50310a5d1baSMatthew Dillon 	 * state of the buffer prior to issuing I/O to interlock any
50410a5d1baSMatthew Dillon 	 * modifications made while the I/O is in progress.  This shouldn't
50510a5d1baSMatthew Dillon 	 * happen anyway but losing data would be worse.  The modified bit
50610a5d1baSMatthew Dillon 	 * will be rechecked after the IO completes.
50710a5d1baSMatthew Dillon 	 *
5084a2796f3SMatthew Dillon 	 * NOTE: This call also finalizes the buffer's content (inval == 0).
5094a2796f3SMatthew Dillon 	 *
510b33e2cc0SMatthew Dillon 	 * This is only legal when lock.refs == 1 (otherwise we might clear
511b33e2cc0SMatthew Dillon 	 * the modified bit while there are still users of the cluster
512b33e2cc0SMatthew Dillon 	 * modifying the data).
513b33e2cc0SMatthew Dillon 	 *
514b33e2cc0SMatthew Dillon 	 * Do this before potentially blocking so any attempt to modify the
515b33e2cc0SMatthew Dillon 	 * ondisk while we are blocked blocks waiting for us.
516b33e2cc0SMatthew Dillon 	 */
5175c8d05e2SMatthew Dillon 	hammer_ref(&io->lock);
5184a2796f3SMatthew Dillon 	hammer_io_clear_modify(io, 0);
5195c8d05e2SMatthew Dillon 	hammer_unref(&io->lock);
520bcac4bbbSMatthew Dillon 
521bcac4bbbSMatthew Dillon 	/*
52210a5d1baSMatthew Dillon 	 * Transfer ownership to the kernel and initiate I/O.
52310a5d1baSMatthew Dillon 	 */
524055f5ff8SMatthew Dillon 	io->running = 1;
525f5a07a7aSMatthew Dillon 	io->hmp->io_running_space += io->bytes;
526f5a07a7aSMatthew Dillon 	hammer_count_io_running_write += io->bytes;
527055f5ff8SMatthew Dillon 	bawrite(bp);
528748efb59SMatthew Dillon 	hammer_io_flush_mark(io->volume);
529055f5ff8SMatthew Dillon }
530055f5ff8SMatthew Dillon 
531055f5ff8SMatthew Dillon /************************************************************************
532055f5ff8SMatthew Dillon  *				BUFFER DIRTYING				*
533055f5ff8SMatthew Dillon  ************************************************************************
534055f5ff8SMatthew Dillon  *
535055f5ff8SMatthew Dillon  * These routines deal with dependancies created when IO buffers get
536055f5ff8SMatthew Dillon  * modified.  The caller must call hammer_modify_*() on a referenced
537055f5ff8SMatthew Dillon  * HAMMER structure prior to modifying its on-disk data.
538055f5ff8SMatthew Dillon  *
539055f5ff8SMatthew Dillon  * Any intent to modify an IO buffer acquires the related bp and imposes
540055f5ff8SMatthew Dillon  * various write ordering dependancies.
541055f5ff8SMatthew Dillon  */
542055f5ff8SMatthew Dillon 
543055f5ff8SMatthew Dillon /*
54410a5d1baSMatthew Dillon  * Mark a HAMMER structure as undergoing modification.  Meta-data buffers
54510a5d1baSMatthew Dillon  * are locked until the flusher can deal with them, pure data buffers
54610a5d1baSMatthew Dillon  * can be written out.
547055f5ff8SMatthew Dillon  */
54810a5d1baSMatthew Dillon static
549b58c6388SMatthew Dillon void
55010a5d1baSMatthew Dillon hammer_io_modify(hammer_io_t io, int count)
551055f5ff8SMatthew Dillon {
55246fe7ae1SMatthew Dillon 	/*
5539f5097dcSMatthew Dillon 	 * io->modify_refs must be >= 0
5549f5097dcSMatthew Dillon 	 */
5559f5097dcSMatthew Dillon 	while (io->modify_refs < 0) {
5569f5097dcSMatthew Dillon 		io->waitmod = 1;
5579f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
5589f5097dcSMatthew Dillon 	}
5599f5097dcSMatthew Dillon 
5609f5097dcSMatthew Dillon 	/*
56146fe7ae1SMatthew Dillon 	 * Shortcut if nothing to do.
56246fe7ae1SMatthew Dillon 	 */
563055f5ff8SMatthew Dillon 	KKASSERT(io->lock.refs != 0 && io->bp != NULL);
56410a5d1baSMatthew Dillon 	io->modify_refs += count;
565b58c6388SMatthew Dillon 	if (io->modified && io->released == 0)
566b58c6388SMatthew Dillon 		return;
56746fe7ae1SMatthew Dillon 
568055f5ff8SMatthew Dillon 	hammer_lock_ex(&io->lock);
56910a5d1baSMatthew Dillon 	if (io->modified == 0) {
570cdb6e4e6SMatthew Dillon 		hammer_io_set_modlist(io);
57146fe7ae1SMatthew Dillon 		io->modified = 1;
57210a5d1baSMatthew Dillon 	}
573055f5ff8SMatthew Dillon 	if (io->released) {
574055f5ff8SMatthew Dillon 		regetblk(io->bp);
575055f5ff8SMatthew Dillon 		BUF_KERNPROC(io->bp);
576055f5ff8SMatthew Dillon 		io->released = 0;
57746fe7ae1SMatthew Dillon 		KKASSERT(io->modified != 0);
578055f5ff8SMatthew Dillon 	}
579055f5ff8SMatthew Dillon 	hammer_unlock(&io->lock);
5800b075555SMatthew Dillon }
5810b075555SMatthew Dillon 
58210a5d1baSMatthew Dillon static __inline
58310a5d1baSMatthew Dillon void
58410a5d1baSMatthew Dillon hammer_io_modify_done(hammer_io_t io)
58510a5d1baSMatthew Dillon {
58610a5d1baSMatthew Dillon 	KKASSERT(io->modify_refs > 0);
58710a5d1baSMatthew Dillon 	--io->modify_refs;
5889f5097dcSMatthew Dillon 	if (io->modify_refs == 0 && io->waitmod) {
5899f5097dcSMatthew Dillon 		io->waitmod = 0;
5909f5097dcSMatthew Dillon 		wakeup(io);
5919f5097dcSMatthew Dillon 	}
5929f5097dcSMatthew Dillon }
5939f5097dcSMatthew Dillon 
5949f5097dcSMatthew Dillon void
5959f5097dcSMatthew Dillon hammer_io_write_interlock(hammer_io_t io)
5969f5097dcSMatthew Dillon {
5979f5097dcSMatthew Dillon 	while (io->modify_refs != 0) {
5989f5097dcSMatthew Dillon 		io->waitmod = 1;
5999f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
6009f5097dcSMatthew Dillon 	}
6019f5097dcSMatthew Dillon 	io->modify_refs = -1;
6029f5097dcSMatthew Dillon }
6039f5097dcSMatthew Dillon 
6049f5097dcSMatthew Dillon void
6059f5097dcSMatthew Dillon hammer_io_done_interlock(hammer_io_t io)
6069f5097dcSMatthew Dillon {
6079f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs == -1);
6089f5097dcSMatthew Dillon 	io->modify_refs = 0;
6099f5097dcSMatthew Dillon 	if (io->waitmod) {
6109f5097dcSMatthew Dillon 		io->waitmod = 0;
6119f5097dcSMatthew Dillon 		wakeup(io);
6129f5097dcSMatthew Dillon 	}
61310a5d1baSMatthew Dillon }
61410a5d1baSMatthew Dillon 
6152f85fa4dSMatthew Dillon /*
6162f85fa4dSMatthew Dillon  * Caller intends to modify a volume's ondisk structure.
6172f85fa4dSMatthew Dillon  *
6182f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
6192f85fa4dSMatthew Dillon  * sync_lock.
6202f85fa4dSMatthew Dillon  */
6210b075555SMatthew Dillon void
62236f82b23SMatthew Dillon hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
62336f82b23SMatthew Dillon 		     void *base, int len)
6240b075555SMatthew Dillon {
6252f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
626055f5ff8SMatthew Dillon 
6272f85fa4dSMatthew Dillon 	hammer_io_modify(&volume->io, 1);
62847197d71SMatthew Dillon 	if (len) {
62947197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
63047197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
631059819e3SMatthew Dillon 		hammer_generate_undo(trans, &volume->io,
63247197d71SMatthew Dillon 			 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
63347197d71SMatthew Dillon 			 base, len);
634055f5ff8SMatthew Dillon 	}
635055f5ff8SMatthew Dillon }
636055f5ff8SMatthew Dillon 
637055f5ff8SMatthew Dillon /*
6382f85fa4dSMatthew Dillon  * Caller intends to modify a buffer's ondisk structure.
6392f85fa4dSMatthew Dillon  *
6402f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
6412f85fa4dSMatthew Dillon  * sync_lock.
642055f5ff8SMatthew Dillon  */
643055f5ff8SMatthew Dillon void
64436f82b23SMatthew Dillon hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
64536f82b23SMatthew Dillon 		     void *base, int len)
64646fe7ae1SMatthew Dillon {
6472f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
6482f85fa4dSMatthew Dillon 
64910a5d1baSMatthew Dillon 	hammer_io_modify(&buffer->io, 1);
65047197d71SMatthew Dillon 	if (len) {
65147197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
65247197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
653059819e3SMatthew Dillon 		hammer_generate_undo(trans, &buffer->io,
65434d829f7SMatthew Dillon 				     buffer->zone2_offset + rel_offset,
65547197d71SMatthew Dillon 				     base, len);
65647197d71SMatthew Dillon 	}
65746fe7ae1SMatthew Dillon }
65846fe7ae1SMatthew Dillon 
65910a5d1baSMatthew Dillon void
66010a5d1baSMatthew Dillon hammer_modify_volume_done(hammer_volume_t volume)
66110a5d1baSMatthew Dillon {
66210a5d1baSMatthew Dillon 	hammer_io_modify_done(&volume->io);
66310a5d1baSMatthew Dillon }
66410a5d1baSMatthew Dillon 
66510a5d1baSMatthew Dillon void
66610a5d1baSMatthew Dillon hammer_modify_buffer_done(hammer_buffer_t buffer)
66710a5d1baSMatthew Dillon {
66810a5d1baSMatthew Dillon 	hammer_io_modify_done(&buffer->io);
66910a5d1baSMatthew Dillon }
67010a5d1baSMatthew Dillon 
67146fe7ae1SMatthew Dillon /*
6724a2796f3SMatthew Dillon  * Mark an entity as not being dirty any more and finalize any
6734a2796f3SMatthew Dillon  * delayed adjustments to the buffer.
6744a2796f3SMatthew Dillon  *
6754a2796f3SMatthew Dillon  * Delayed adjustments are an important performance enhancement, allowing
6764a2796f3SMatthew Dillon  * us to avoid recalculating B-Tree node CRCs over and over again when
6774a2796f3SMatthew Dillon  * making bulk-modifications to the B-Tree.
6784a2796f3SMatthew Dillon  *
6794a2796f3SMatthew Dillon  * If inval is non-zero delayed adjustments are ignored.
6805c8d05e2SMatthew Dillon  *
6815c8d05e2SMatthew Dillon  * This routine may dereference related btree nodes and cause the
6825c8d05e2SMatthew Dillon  * buffer to be dereferenced.  The caller must own a reference on io.
68361aeeb33SMatthew Dillon  */
68461aeeb33SMatthew Dillon void
6854a2796f3SMatthew Dillon hammer_io_clear_modify(struct hammer_io *io, int inval)
68661aeeb33SMatthew Dillon {
6874a2796f3SMatthew Dillon 	if (io->modified == 0)
6884a2796f3SMatthew Dillon 		return;
6894a2796f3SMatthew Dillon 
6904a2796f3SMatthew Dillon 	/*
6914a2796f3SMatthew Dillon 	 * Take us off the mod-list and clear the modified bit.
6924a2796f3SMatthew Dillon 	 */
693cebe9493SMatthew Dillon 	KKASSERT(io->mod_list != NULL);
694cebe9493SMatthew Dillon 	if (io->mod_list == &io->hmp->volu_list ||
695cebe9493SMatthew Dillon 	    io->mod_list == &io->hmp->meta_list) {
696f5a07a7aSMatthew Dillon 		io->hmp->locked_dirty_space -= io->bytes;
697f5a07a7aSMatthew Dillon 		hammer_count_dirtybufspace -= io->bytes;
698cebe9493SMatthew Dillon 	}
699cebe9493SMatthew Dillon 	TAILQ_REMOVE(io->mod_list, io, mod_entry);
700cebe9493SMatthew Dillon 	io->mod_list = NULL;
70161aeeb33SMatthew Dillon 	io->modified = 0;
7024a2796f3SMatthew Dillon 
7034a2796f3SMatthew Dillon 	/*
7044a2796f3SMatthew Dillon 	 * If this bit is not set there are no delayed adjustments.
7054a2796f3SMatthew Dillon 	 */
7064a2796f3SMatthew Dillon 	if (io->gencrc == 0)
7074a2796f3SMatthew Dillon 		return;
7084a2796f3SMatthew Dillon 	io->gencrc = 0;
7094a2796f3SMatthew Dillon 
7104a2796f3SMatthew Dillon 	/*
7114a2796f3SMatthew Dillon 	 * Finalize requested CRCs.  The NEEDSCRC flag also holds a reference
7124a2796f3SMatthew Dillon 	 * on the node (& underlying buffer).  Release the node after clearing
7134a2796f3SMatthew Dillon 	 * the flag.
7144a2796f3SMatthew Dillon 	 */
7154a2796f3SMatthew Dillon 	if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
7164a2796f3SMatthew Dillon 		hammer_buffer_t buffer = (void *)io;
7174a2796f3SMatthew Dillon 		hammer_node_t node;
7184a2796f3SMatthew Dillon 
7194a2796f3SMatthew Dillon restart:
7204a2796f3SMatthew Dillon 		TAILQ_FOREACH(node, &buffer->clist, entry) {
7214a2796f3SMatthew Dillon 			if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
7224a2796f3SMatthew Dillon 				continue;
7234a2796f3SMatthew Dillon 			node->flags &= ~HAMMER_NODE_NEEDSCRC;
7244a2796f3SMatthew Dillon 			KKASSERT(node->ondisk);
7254a2796f3SMatthew Dillon 			if (inval == 0)
7264a2796f3SMatthew Dillon 				node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
7274a2796f3SMatthew Dillon 			hammer_rel_node(node);
7284a2796f3SMatthew Dillon 			goto restart;
72961aeeb33SMatthew Dillon 		}
73061aeeb33SMatthew Dillon 	}
7315c8d05e2SMatthew Dillon 	/* caller must still have ref on io */
7325c8d05e2SMatthew Dillon 	KKASSERT(io->lock.refs > 0);
7334a2796f3SMatthew Dillon }
7344a2796f3SMatthew Dillon 
735cebe9493SMatthew Dillon /*
736cebe9493SMatthew Dillon  * Clear the IO's modify list.  Even though the IO is no longer modified
737cebe9493SMatthew Dillon  * it may still be on the lose_list.  This routine is called just before
738cebe9493SMatthew Dillon  * the governing hammer_buffer is destroyed.
739cebe9493SMatthew Dillon  */
740cebe9493SMatthew Dillon void
741cebe9493SMatthew Dillon hammer_io_clear_modlist(struct hammer_io *io)
742cebe9493SMatthew Dillon {
7434a2796f3SMatthew Dillon 	KKASSERT(io->modified == 0);
744cebe9493SMatthew Dillon 	if (io->mod_list) {
745a99b9ea2SMatthew Dillon 		crit_enter();	/* biodone race against list */
746cebe9493SMatthew Dillon 		KKASSERT(io->mod_list == &io->hmp->lose_list);
747cebe9493SMatthew Dillon 		TAILQ_REMOVE(io->mod_list, io, mod_entry);
748cebe9493SMatthew Dillon 		io->mod_list = NULL;
749a99b9ea2SMatthew Dillon 		crit_exit();
750cebe9493SMatthew Dillon 	}
75166325755SMatthew Dillon }
75266325755SMatthew Dillon 
753cdb6e4e6SMatthew Dillon static void
754cdb6e4e6SMatthew Dillon hammer_io_set_modlist(struct hammer_io *io)
755cdb6e4e6SMatthew Dillon {
756cdb6e4e6SMatthew Dillon 	struct hammer_mount *hmp = io->hmp;
757cdb6e4e6SMatthew Dillon 
758cdb6e4e6SMatthew Dillon 	KKASSERT(io->mod_list == NULL);
759cdb6e4e6SMatthew Dillon 
760cdb6e4e6SMatthew Dillon 	switch(io->type) {
761cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_VOLUME:
762cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->volu_list;
763cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
764cdb6e4e6SMatthew Dillon 		hammer_count_dirtybufspace += io->bytes;
765cdb6e4e6SMatthew Dillon 		break;
766cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_META_BUFFER:
767cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->meta_list;
768cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
769cdb6e4e6SMatthew Dillon 		hammer_count_dirtybufspace += io->bytes;
770cdb6e4e6SMatthew Dillon 		break;
771cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_UNDO_BUFFER:
772cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->undo_list;
773cdb6e4e6SMatthew Dillon 		break;
774cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_DATA_BUFFER:
775cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->data_list;
776cdb6e4e6SMatthew Dillon 		break;
777cdb6e4e6SMatthew Dillon 	}
778cdb6e4e6SMatthew Dillon 	TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
779cdb6e4e6SMatthew Dillon }
780cdb6e4e6SMatthew Dillon 
781055f5ff8SMatthew Dillon /************************************************************************
782055f5ff8SMatthew Dillon  *				HAMMER_BIOOPS				*
783055f5ff8SMatthew Dillon  ************************************************************************
784055f5ff8SMatthew Dillon  *
785055f5ff8SMatthew Dillon  */
786055f5ff8SMatthew Dillon 
787055f5ff8SMatthew Dillon /*
788055f5ff8SMatthew Dillon  * Pre-IO initiation kernel callback - cluster build only
789055f5ff8SMatthew Dillon  */
790055f5ff8SMatthew Dillon static void
791055f5ff8SMatthew Dillon hammer_io_start(struct buf *bp)
792055f5ff8SMatthew Dillon {
793055f5ff8SMatthew Dillon }
794055f5ff8SMatthew Dillon 
795055f5ff8SMatthew Dillon /*
7967bc5b8c2SMatthew Dillon  * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
797b33e2cc0SMatthew Dillon  *
798b33e2cc0SMatthew Dillon  * NOTE: HAMMER may modify a buffer after initiating I/O.  The modified bit
799b33e2cc0SMatthew Dillon  * may also be set if we were marking a cluster header open.  Only remove
800b33e2cc0SMatthew Dillon  * our dependancy if the modified bit is clear.
801055f5ff8SMatthew Dillon  */
80266325755SMatthew Dillon static void
80366325755SMatthew Dillon hammer_io_complete(struct buf *bp)
80466325755SMatthew Dillon {
805055f5ff8SMatthew Dillon 	union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
806fbc6e32aSMatthew Dillon 
807055f5ff8SMatthew Dillon 	KKASSERT(iou->io.released == 1);
808055f5ff8SMatthew Dillon 
809bf3b416bSMatthew Dillon 	/*
810bf3b416bSMatthew Dillon 	 * Deal with people waiting for I/O to drain
811bf3b416bSMatthew Dillon 	 */
812f90dde4cSMatthew Dillon 	if (iou->io.running) {
813cdb6e4e6SMatthew Dillon 		/*
814cdb6e4e6SMatthew Dillon 		 * Deal with critical write errors.  Once a critical error
815cdb6e4e6SMatthew Dillon 		 * has been flagged in hmp the UNDO FIFO will not be updated.
816cdb6e4e6SMatthew Dillon 		 * That way crash recover will give us a consistent
817cdb6e4e6SMatthew Dillon 		 * filesystem.
818cdb6e4e6SMatthew Dillon 		 *
819cdb6e4e6SMatthew Dillon 		 * Because of this we can throw away failed UNDO buffers.  If
820cdb6e4e6SMatthew Dillon 		 * we throw away META or DATA buffers we risk corrupting
821cdb6e4e6SMatthew Dillon 		 * the now read-only version of the filesystem visible to
822cdb6e4e6SMatthew Dillon 		 * the user.  Clear B_ERROR so the buffer is not re-dirtied
823cdb6e4e6SMatthew Dillon 		 * by the kernel and ref the io so it doesn't get thrown
824cdb6e4e6SMatthew Dillon 		 * away.
825cdb6e4e6SMatthew Dillon 		 */
826cdb6e4e6SMatthew Dillon 		if (bp->b_flags & B_ERROR) {
827cdb6e4e6SMatthew Dillon 			hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
828cdb6e4e6SMatthew Dillon 					      "while flushing meta-data");
829cdb6e4e6SMatthew Dillon 			switch(iou->io.type) {
830cdb6e4e6SMatthew Dillon 			case HAMMER_STRUCTURE_UNDO_BUFFER:
831cdb6e4e6SMatthew Dillon 				break;
832cdb6e4e6SMatthew Dillon 			default:
833cdb6e4e6SMatthew Dillon 				if (iou->io.ioerror == 0) {
834cdb6e4e6SMatthew Dillon 					iou->io.ioerror = 1;
835cdb6e4e6SMatthew Dillon 					if (iou->io.lock.refs == 0)
836cdb6e4e6SMatthew Dillon 						++hammer_count_refedbufs;
837cdb6e4e6SMatthew Dillon 					hammer_ref(&iou->io.lock);
838cdb6e4e6SMatthew Dillon 				}
839cdb6e4e6SMatthew Dillon 				break;
840cdb6e4e6SMatthew Dillon 			}
841cdb6e4e6SMatthew Dillon 			bp->b_flags &= ~B_ERROR;
842cdb6e4e6SMatthew Dillon 			bundirty(bp);
843cdb6e4e6SMatthew Dillon #if 0
844cdb6e4e6SMatthew Dillon 			hammer_io_set_modlist(&iou->io);
845cdb6e4e6SMatthew Dillon 			iou->io.modified = 1;
846cdb6e4e6SMatthew Dillon #endif
847cdb6e4e6SMatthew Dillon 		}
848ce0138a6SMatthew Dillon 		hammer_stats_disk_write += iou->io.bytes;
849f5a07a7aSMatthew Dillon 		hammer_count_io_running_write -= iou->io.bytes;
850f5a07a7aSMatthew Dillon 		iou->io.hmp->io_running_space -= iou->io.bytes;
851f5a07a7aSMatthew Dillon 		if (iou->io.hmp->io_running_space == 0)
852f5a07a7aSMatthew Dillon 			wakeup(&iou->io.hmp->io_running_space);
853f5a07a7aSMatthew Dillon 		KKASSERT(iou->io.hmp->io_running_space >= 0);
854f90dde4cSMatthew Dillon 		iou->io.running = 0;
855ce0138a6SMatthew Dillon 	} else {
856ce0138a6SMatthew Dillon 		hammer_stats_disk_read += iou->io.bytes;
857f90dde4cSMatthew Dillon 	}
858f90dde4cSMatthew Dillon 
859055f5ff8SMatthew Dillon 	if (iou->io.waiting) {
860055f5ff8SMatthew Dillon 		iou->io.waiting = 0;
861055f5ff8SMatthew Dillon 		wakeup(iou);
862055f5ff8SMatthew Dillon 	}
863055f5ff8SMatthew Dillon 
864055f5ff8SMatthew Dillon 	/*
865bf3b416bSMatthew Dillon 	 * If B_LOCKED is set someone wanted to deallocate the bp at some
866bf3b416bSMatthew Dillon 	 * point, do it now if refs has become zero.
867055f5ff8SMatthew Dillon 	 */
868055f5ff8SMatthew Dillon 	if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
869b33e2cc0SMatthew Dillon 		KKASSERT(iou->io.modified == 0);
870a99b9ea2SMatthew Dillon 		--hammer_count_io_locked;
871d5ef456eSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
872055f5ff8SMatthew Dillon 		hammer_io_deallocate(bp);
873055f5ff8SMatthew Dillon 		/* structure may be dead now */
874fbc6e32aSMatthew Dillon 	}
87566325755SMatthew Dillon }
87666325755SMatthew Dillon 
87766325755SMatthew Dillon /*
87866325755SMatthew Dillon  * Callback from kernel when it wishes to deallocate a passively
87910a5d1baSMatthew Dillon  * associated structure.  This mostly occurs with clean buffers
88010a5d1baSMatthew Dillon  * but it may be possible for a holding structure to be marked dirty
8817bc5b8c2SMatthew Dillon  * while its buffer is passively associated.  The caller owns the bp.
88266325755SMatthew Dillon  *
88366325755SMatthew Dillon  * If we cannot disassociate we set B_LOCKED to prevent the buffer
88466325755SMatthew Dillon  * from getting reused.
88546fe7ae1SMatthew Dillon  *
88646fe7ae1SMatthew Dillon  * WARNING: Because this can be called directly by getnewbuf we cannot
88746fe7ae1SMatthew Dillon  * recurse into the tree.  If a bp cannot be immediately disassociated
88846fe7ae1SMatthew Dillon  * our only recourse is to set B_LOCKED.
8897bc5b8c2SMatthew Dillon  *
8907bc5b8c2SMatthew Dillon  * WARNING: This may be called from an interrupt via hammer_io_complete()
89166325755SMatthew Dillon  */
89266325755SMatthew Dillon static void
89366325755SMatthew Dillon hammer_io_deallocate(struct buf *bp)
89466325755SMatthew Dillon {
895055f5ff8SMatthew Dillon 	hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
89666325755SMatthew Dillon 
897055f5ff8SMatthew Dillon 	KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
89846fe7ae1SMatthew Dillon 	if (iou->io.lock.refs > 0 || iou->io.modified) {
89910a5d1baSMatthew Dillon 		/*
90010a5d1baSMatthew Dillon 		 * It is not legal to disassociate a modified buffer.  This
90110a5d1baSMatthew Dillon 		 * case really shouldn't ever occur.
90210a5d1baSMatthew Dillon 		 */
903055f5ff8SMatthew Dillon 		bp->b_flags |= B_LOCKED;
904a99b9ea2SMatthew Dillon 		++hammer_count_io_locked;
905055f5ff8SMatthew Dillon 	} else {
90610a5d1baSMatthew Dillon 		/*
90710a5d1baSMatthew Dillon 		 * Disassociate the BP.  If the io has no refs left we
90810a5d1baSMatthew Dillon 		 * have to add it to the loose list.
90910a5d1baSMatthew Dillon 		 */
910ecca949aSMatthew Dillon 		hammer_io_disassociate(iou);
911ecca949aSMatthew Dillon 		if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
912ecca949aSMatthew Dillon 			KKASSERT(iou->io.bp == NULL);
91310a5d1baSMatthew Dillon 			KKASSERT(iou->io.mod_list == NULL);
914a99b9ea2SMatthew Dillon 			crit_enter();	/* biodone race against list */
91510a5d1baSMatthew Dillon 			iou->io.mod_list = &iou->io.hmp->lose_list;
91610a5d1baSMatthew Dillon 			TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
917a99b9ea2SMatthew Dillon 			crit_exit();
91866325755SMatthew Dillon 		}
91966325755SMatthew Dillon 	}
92066325755SMatthew Dillon }
92166325755SMatthew Dillon 
92266325755SMatthew Dillon static int
92366325755SMatthew Dillon hammer_io_fsync(struct vnode *vp)
92466325755SMatthew Dillon {
92566325755SMatthew Dillon 	return(0);
92666325755SMatthew Dillon }
92766325755SMatthew Dillon 
92866325755SMatthew Dillon /*
92966325755SMatthew Dillon  * NOTE: will not be called unless we tell the kernel about the
93066325755SMatthew Dillon  * bioops.  Unused... we use the mount's VFS_SYNC instead.
93166325755SMatthew Dillon  */
93266325755SMatthew Dillon static int
93366325755SMatthew Dillon hammer_io_sync(struct mount *mp)
93466325755SMatthew Dillon {
93566325755SMatthew Dillon 	return(0);
93666325755SMatthew Dillon }
93766325755SMatthew Dillon 
93866325755SMatthew Dillon static void
93966325755SMatthew Dillon hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
94066325755SMatthew Dillon {
94166325755SMatthew Dillon }
94266325755SMatthew Dillon 
94366325755SMatthew Dillon /*
94466325755SMatthew Dillon  * I/O pre-check for reading and writing.  HAMMER only uses this for
94566325755SMatthew Dillon  * B_CACHE buffers so checkread just shouldn't happen, but if it does
94666325755SMatthew Dillon  * allow it.
94766325755SMatthew Dillon  *
948fbc6e32aSMatthew Dillon  * Writing is a different case.  We don't want the kernel to try to write
949fbc6e32aSMatthew Dillon  * out a buffer that HAMMER may be modifying passively or which has a
95010a5d1baSMatthew Dillon  * dependancy.  In addition, kernel-demanded writes can only proceed for
95110a5d1baSMatthew Dillon  * certain types of buffers (i.e. UNDO and DATA types).  Other dirty
95210a5d1baSMatthew Dillon  * buffer types can only be explicitly written by the flusher.
953fbc6e32aSMatthew Dillon  *
95410a5d1baSMatthew Dillon  * checkwrite will only be called for bdwrite()n buffers.  If we return
95510a5d1baSMatthew Dillon  * success the kernel is guaranteed to initiate the buffer write.
95666325755SMatthew Dillon  */
95766325755SMatthew Dillon static int
95866325755SMatthew Dillon hammer_io_checkread(struct buf *bp)
95966325755SMatthew Dillon {
96066325755SMatthew Dillon 	return(0);
96166325755SMatthew Dillon }
96266325755SMatthew Dillon 
96366325755SMatthew Dillon static int
96466325755SMatthew Dillon hammer_io_checkwrite(struct buf *bp)
96566325755SMatthew Dillon {
96610a5d1baSMatthew Dillon 	hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
96766325755SMatthew Dillon 
96877062c8aSMatthew Dillon 	/*
96977062c8aSMatthew Dillon 	 * This shouldn't happen under normal operation.
97077062c8aSMatthew Dillon 	 */
97177062c8aSMatthew Dillon 	if (io->type == HAMMER_STRUCTURE_VOLUME ||
97277062c8aSMatthew Dillon 	    io->type == HAMMER_STRUCTURE_META_BUFFER) {
97377062c8aSMatthew Dillon 		if (!panicstr)
97477062c8aSMatthew Dillon 			panic("hammer_io_checkwrite: illegal buffer");
975a99b9ea2SMatthew Dillon 		if ((bp->b_flags & B_LOCKED) == 0) {
97677062c8aSMatthew Dillon 			bp->b_flags |= B_LOCKED;
977a99b9ea2SMatthew Dillon 			++hammer_count_io_locked;
978a99b9ea2SMatthew Dillon 		}
97977062c8aSMatthew Dillon 		return(1);
98077062c8aSMatthew Dillon 	}
981c9b9e29dSMatthew Dillon 
982fbc6e32aSMatthew Dillon 	/*
98310a5d1baSMatthew Dillon 	 * We can only clear the modified bit if the IO is not currently
98410a5d1baSMatthew Dillon 	 * undergoing modification.  Otherwise we may miss changes.
9855c8d05e2SMatthew Dillon 	 *
9865c8d05e2SMatthew Dillon 	 * Only data and undo buffers can reach here.  These buffers do
9875c8d05e2SMatthew Dillon 	 * not have terminal crc functions but we temporarily reference
9885c8d05e2SMatthew Dillon 	 * the IO anyway, just in case.
989b33e2cc0SMatthew Dillon 	 */
9905c8d05e2SMatthew Dillon 	if (io->modify_refs == 0 && io->modified) {
9915c8d05e2SMatthew Dillon 		hammer_ref(&io->lock);
9924a2796f3SMatthew Dillon 		hammer_io_clear_modify(io, 0);
9935c8d05e2SMatthew Dillon 		hammer_unref(&io->lock);
9945c8d05e2SMatthew Dillon 	} else if (io->modified) {
9955c8d05e2SMatthew Dillon 		KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
9965c8d05e2SMatthew Dillon 	}
997f90dde4cSMatthew Dillon 
998f90dde4cSMatthew Dillon 	/*
999f90dde4cSMatthew Dillon 	 * The kernel is going to start the IO, set io->running.
1000f90dde4cSMatthew Dillon 	 */
1001f90dde4cSMatthew Dillon 	KKASSERT(io->running == 0);
1002f90dde4cSMatthew Dillon 	io->running = 1;
1003f5a07a7aSMatthew Dillon 	io->hmp->io_running_space += io->bytes;
1004f5a07a7aSMatthew Dillon 	hammer_count_io_running_write += io->bytes;
1005055f5ff8SMatthew Dillon 	return(0);
1006055f5ff8SMatthew Dillon }
100766325755SMatthew Dillon 
10088cd0a023SMatthew Dillon /*
100966325755SMatthew Dillon  * Return non-zero if we wish to delay the kernel's attempt to flush
101066325755SMatthew Dillon  * this buffer to disk.
101166325755SMatthew Dillon  */
101266325755SMatthew Dillon static int
101366325755SMatthew Dillon hammer_io_countdeps(struct buf *bp, int n)
101466325755SMatthew Dillon {
101566325755SMatthew Dillon 	return(0);
101666325755SMatthew Dillon }
101766325755SMatthew Dillon 
101866325755SMatthew Dillon struct bio_ops hammer_bioops = {
101966325755SMatthew Dillon 	.io_start	= hammer_io_start,
102066325755SMatthew Dillon 	.io_complete	= hammer_io_complete,
102166325755SMatthew Dillon 	.io_deallocate	= hammer_io_deallocate,
102266325755SMatthew Dillon 	.io_fsync	= hammer_io_fsync,
102366325755SMatthew Dillon 	.io_sync	= hammer_io_sync,
102466325755SMatthew Dillon 	.io_movedeps	= hammer_io_movedeps,
102566325755SMatthew Dillon 	.io_countdeps	= hammer_io_countdeps,
102666325755SMatthew Dillon 	.io_checkread	= hammer_io_checkread,
102766325755SMatthew Dillon 	.io_checkwrite	= hammer_io_checkwrite,
102866325755SMatthew Dillon };
102966325755SMatthew Dillon 
103047637bffSMatthew Dillon /************************************************************************
103147637bffSMatthew Dillon  *				DIRECT IO OPS 				*
103247637bffSMatthew Dillon  ************************************************************************
103347637bffSMatthew Dillon  *
103447637bffSMatthew Dillon  * These functions operate directly on the buffer cache buffer associated
103547637bffSMatthew Dillon  * with a front-end vnode rather then a back-end device vnode.
103647637bffSMatthew Dillon  */
103747637bffSMatthew Dillon 
103847637bffSMatthew Dillon /*
103947637bffSMatthew Dillon  * Read a buffer associated with a front-end vnode directly from the
10401b0ab2c3SMatthew Dillon  * disk media.  The bio may be issued asynchronously.  If leaf is non-NULL
10411b0ab2c3SMatthew Dillon  * we validate the CRC.
1042a99b9ea2SMatthew Dillon  *
10431b0ab2c3SMatthew Dillon  * We must check for the presence of a HAMMER buffer to handle the case
10441b0ab2c3SMatthew Dillon  * where the reblocker has rewritten the data (which it does via the HAMMER
10451b0ab2c3SMatthew Dillon  * buffer system, not via the high-level vnode buffer cache), but not yet
10461b0ab2c3SMatthew Dillon  * committed the buffer to the media.
104747637bffSMatthew Dillon  */
104847637bffSMatthew Dillon int
10491b0ab2c3SMatthew Dillon hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
10501b0ab2c3SMatthew Dillon 		      hammer_btree_leaf_elm_t leaf)
105147637bffSMatthew Dillon {
10521b0ab2c3SMatthew Dillon 	hammer_off_t buf_offset;
105347637bffSMatthew Dillon 	hammer_off_t zone2_offset;
105447637bffSMatthew Dillon 	hammer_volume_t volume;
105547637bffSMatthew Dillon 	struct buf *bp;
105647637bffSMatthew Dillon 	struct bio *nbio;
105747637bffSMatthew Dillon 	int vol_no;
105847637bffSMatthew Dillon 	int error;
105947637bffSMatthew Dillon 
10601b0ab2c3SMatthew Dillon 	buf_offset = bio->bio_offset;
10611b0ab2c3SMatthew Dillon 	KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
10621b0ab2c3SMatthew Dillon 		 HAMMER_ZONE_LARGE_DATA);
10634a2796f3SMatthew Dillon 
10641b0ab2c3SMatthew Dillon 	/*
10651b0ab2c3SMatthew Dillon 	 * The buffer cache may have an aliased buffer (the reblocker can
10661b0ab2c3SMatthew Dillon 	 * write them).  If it does we have to sync any dirty data before
10671b0ab2c3SMatthew Dillon 	 * we can build our direct-read.  This is a non-critical code path.
10681b0ab2c3SMatthew Dillon 	 */
10691b0ab2c3SMatthew Dillon 	bp = bio->bio_buf;
10701b0ab2c3SMatthew Dillon 	hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
10711b0ab2c3SMatthew Dillon 
10721b0ab2c3SMatthew Dillon 	/*
10731b0ab2c3SMatthew Dillon 	 * Resolve to a zone-2 offset.  The conversion just requires
10741b0ab2c3SMatthew Dillon 	 * munging the top 4 bits but we want to abstract it anyway
10751b0ab2c3SMatthew Dillon 	 * so the blockmap code can verify the zone assignment.
10761b0ab2c3SMatthew Dillon 	 */
10771b0ab2c3SMatthew Dillon 	zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
10781b0ab2c3SMatthew Dillon 	if (error)
10791b0ab2c3SMatthew Dillon 		goto done;
108043c665aeSMatthew Dillon 	KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
108143c665aeSMatthew Dillon 		 HAMMER_ZONE_RAW_BUFFER);
108243c665aeSMatthew Dillon 
10831b0ab2c3SMatthew Dillon 	/*
10841b0ab2c3SMatthew Dillon 	 * Resolve volume and raw-offset for 3rd level bio.  The
10851b0ab2c3SMatthew Dillon 	 * offset will be specific to the volume.
10861b0ab2c3SMatthew Dillon 	 */
108747637bffSMatthew Dillon 	vol_no = HAMMER_VOL_DECODE(zone2_offset);
108847637bffSMatthew Dillon 	volume = hammer_get_volume(hmp, vol_no, &error);
108947637bffSMatthew Dillon 	if (error == 0 && zone2_offset >= volume->maxbuf_off)
109047637bffSMatthew Dillon 		error = EIO;
109143c665aeSMatthew Dillon 
109247637bffSMatthew Dillon 	if (error == 0) {
1093e469566bSMatthew Dillon 		/*
1094e469566bSMatthew Dillon 		 * 3rd level bio
1095e469566bSMatthew Dillon 		 */
109647637bffSMatthew Dillon 		nbio = push_bio(bio);
109747637bffSMatthew Dillon 		nbio->bio_offset = volume->ondisk->vol_buf_beg +
1098e469566bSMatthew Dillon 				   (zone2_offset & HAMMER_OFF_SHORT_MASK);
10991b0ab2c3SMatthew Dillon #if 0
11001b0ab2c3SMatthew Dillon 		/*
11011b0ab2c3SMatthew Dillon 		 * XXX disabled - our CRC check doesn't work if the OS
11021b0ab2c3SMatthew Dillon 		 * does bogus_page replacement on the direct-read.
11031b0ab2c3SMatthew Dillon 		 */
11041b0ab2c3SMatthew Dillon 		if (leaf && hammer_verify_data) {
11051b0ab2c3SMatthew Dillon 			nbio->bio_done = hammer_io_direct_read_complete;
11061b0ab2c3SMatthew Dillon 			nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
11071b0ab2c3SMatthew Dillon 		}
11081b0ab2c3SMatthew Dillon #endif
1109ce0138a6SMatthew Dillon 		hammer_stats_disk_read += bp->b_bufsize;
111047637bffSMatthew Dillon 		vn_strategy(volume->devvp, nbio);
111147637bffSMatthew Dillon 	}
111247637bffSMatthew Dillon 	hammer_rel_volume(volume, 0);
11131b0ab2c3SMatthew Dillon done:
111447637bffSMatthew Dillon 	if (error) {
1115cebe9493SMatthew Dillon 		kprintf("hammer_direct_read: failed @ %016llx\n",
1116973c11b9SMatthew Dillon 			(long long)zone2_offset);
111747637bffSMatthew Dillon 		bp->b_error = error;
111847637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
111947637bffSMatthew Dillon 		biodone(bio);
112047637bffSMatthew Dillon 	}
112147637bffSMatthew Dillon 	return(error);
112247637bffSMatthew Dillon }
112347637bffSMatthew Dillon 
11241b0ab2c3SMatthew Dillon #if 0
11251b0ab2c3SMatthew Dillon /*
11261b0ab2c3SMatthew Dillon  * On completion of the BIO this callback must check the data CRC
11271b0ab2c3SMatthew Dillon  * and chain to the previous bio.
11281b0ab2c3SMatthew Dillon  */
11291b0ab2c3SMatthew Dillon static
11301b0ab2c3SMatthew Dillon void
11311b0ab2c3SMatthew Dillon hammer_io_direct_read_complete(struct bio *nbio)
11321b0ab2c3SMatthew Dillon {
11331b0ab2c3SMatthew Dillon 	struct bio *obio;
11341b0ab2c3SMatthew Dillon 	struct buf *bp;
11351b0ab2c3SMatthew Dillon 	u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
11361b0ab2c3SMatthew Dillon 
11371b0ab2c3SMatthew Dillon 	bp = nbio->bio_buf;
11381b0ab2c3SMatthew Dillon 	if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
11391b0ab2c3SMatthew Dillon 		kprintf("HAMMER: data_crc error @%016llx/%d\n",
11401b0ab2c3SMatthew Dillon 			nbio->bio_offset, bp->b_bufsize);
11411b0ab2c3SMatthew Dillon 		if (hammer_debug_debug)
11421b0ab2c3SMatthew Dillon 			Debugger("");
11431b0ab2c3SMatthew Dillon 		bp->b_flags |= B_ERROR;
11441b0ab2c3SMatthew Dillon 		bp->b_error = EIO;
11451b0ab2c3SMatthew Dillon 	}
11461b0ab2c3SMatthew Dillon 	obio = pop_bio(nbio);
11471b0ab2c3SMatthew Dillon 	biodone(obio);
11481b0ab2c3SMatthew Dillon }
11491b0ab2c3SMatthew Dillon #endif
11501b0ab2c3SMatthew Dillon 
115147637bffSMatthew Dillon /*
115247637bffSMatthew Dillon  * Write a buffer associated with a front-end vnode directly to the
115347637bffSMatthew Dillon  * disk media.  The bio may be issued asynchronously.
11541b0ab2c3SMatthew Dillon  *
11551b0ab2c3SMatthew Dillon  * The BIO is associated with the specified record and RECF_DIRECT_IO
1156e469566bSMatthew Dillon  * is set.  The recorded is added to its object.
115747637bffSMatthew Dillon  */
115847637bffSMatthew Dillon int
11591b0ab2c3SMatthew Dillon hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
116047637bffSMatthew Dillon 		       struct bio *bio)
116147637bffSMatthew Dillon {
11621b0ab2c3SMatthew Dillon 	hammer_btree_leaf_elm_t leaf = &record->leaf;
11630832c9bbSMatthew Dillon 	hammer_off_t buf_offset;
116447637bffSMatthew Dillon 	hammer_off_t zone2_offset;
116547637bffSMatthew Dillon 	hammer_volume_t volume;
11660832c9bbSMatthew Dillon 	hammer_buffer_t buffer;
116747637bffSMatthew Dillon 	struct buf *bp;
116847637bffSMatthew Dillon 	struct bio *nbio;
11690832c9bbSMatthew Dillon 	char *ptr;
117047637bffSMatthew Dillon 	int vol_no;
117147637bffSMatthew Dillon 	int error;
117247637bffSMatthew Dillon 
11730832c9bbSMatthew Dillon 	buf_offset = leaf->data_offset;
11740832c9bbSMatthew Dillon 
11750832c9bbSMatthew Dillon 	KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
117647637bffSMatthew Dillon 	KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
117747637bffSMatthew Dillon 
11780832c9bbSMatthew Dillon 	if ((buf_offset & HAMMER_BUFMASK) == 0 &&
11794a2796f3SMatthew Dillon 	    leaf->data_len >= HAMMER_BUFSIZE) {
11800832c9bbSMatthew Dillon 		/*
11810832c9bbSMatthew Dillon 		 * We are using the vnode's bio to write directly to the
11820832c9bbSMatthew Dillon 		 * media, any hammer_buffer at the same zone-X offset will
11830832c9bbSMatthew Dillon 		 * now have stale data.
11840832c9bbSMatthew Dillon 		 */
11850832c9bbSMatthew Dillon 		zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
118647637bffSMatthew Dillon 		vol_no = HAMMER_VOL_DECODE(zone2_offset);
118747637bffSMatthew Dillon 		volume = hammer_get_volume(hmp, vol_no, &error);
118847637bffSMatthew Dillon 
118947637bffSMatthew Dillon 		if (error == 0 && zone2_offset >= volume->maxbuf_off)
119047637bffSMatthew Dillon 			error = EIO;
119147637bffSMatthew Dillon 		if (error == 0) {
11920832c9bbSMatthew Dillon 			bp = bio->bio_buf;
11934a2796f3SMatthew Dillon 			KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1194e469566bSMatthew Dillon 			/*
11954a2796f3SMatthew Dillon 			hammer_del_buffers(hmp, buf_offset,
11964a2796f3SMatthew Dillon 					   zone2_offset, bp->b_bufsize);
1197e469566bSMatthew Dillon 			*/
11981b0ab2c3SMatthew Dillon 
119943c665aeSMatthew Dillon 			/*
120043c665aeSMatthew Dillon 			 * Second level bio - cached zone2 offset.
12011b0ab2c3SMatthew Dillon 			 *
12021b0ab2c3SMatthew Dillon 			 * (We can put our bio_done function in either the
12031b0ab2c3SMatthew Dillon 			 *  2nd or 3rd level).
120443c665aeSMatthew Dillon 			 */
120547637bffSMatthew Dillon 			nbio = push_bio(bio);
120643c665aeSMatthew Dillon 			nbio->bio_offset = zone2_offset;
12071b0ab2c3SMatthew Dillon 			nbio->bio_done = hammer_io_direct_write_complete;
12081b0ab2c3SMatthew Dillon 			nbio->bio_caller_info1.ptr = record;
1209e469566bSMatthew Dillon 			record->zone2_offset = zone2_offset;
1210e469566bSMatthew Dillon 			record->flags |= HAMMER_RECF_DIRECT_IO |
1211e469566bSMatthew Dillon 					 HAMMER_RECF_DIRECT_INVAL;
121243c665aeSMatthew Dillon 
121343c665aeSMatthew Dillon 			/*
121443c665aeSMatthew Dillon 			 * Third level bio - raw offset specific to the
121543c665aeSMatthew Dillon 			 * correct volume.
121643c665aeSMatthew Dillon 			 */
121743c665aeSMatthew Dillon 			zone2_offset &= HAMMER_OFF_SHORT_MASK;
121843c665aeSMatthew Dillon 			nbio = push_bio(nbio);
121947637bffSMatthew Dillon 			nbio->bio_offset = volume->ondisk->vol_buf_beg +
12200832c9bbSMatthew Dillon 					   zone2_offset;
1221ce0138a6SMatthew Dillon 			hammer_stats_disk_write += bp->b_bufsize;
122247637bffSMatthew Dillon 			vn_strategy(volume->devvp, nbio);
1223748efb59SMatthew Dillon 			hammer_io_flush_mark(volume);
122447637bffSMatthew Dillon 		}
122547637bffSMatthew Dillon 		hammer_rel_volume(volume, 0);
12260832c9bbSMatthew Dillon 	} else {
12271b0ab2c3SMatthew Dillon 		/*
12281b0ab2c3SMatthew Dillon 		 * Must fit in a standard HAMMER buffer.  In this case all
12291b0ab2c3SMatthew Dillon 		 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
12301b0ab2c3SMatthew Dillon 		 * does not need to be set-up.
12311b0ab2c3SMatthew Dillon 		 */
12320832c9bbSMatthew Dillon 		KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
12330832c9bbSMatthew Dillon 		buffer = NULL;
12340832c9bbSMatthew Dillon 		ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
12350832c9bbSMatthew Dillon 		if (error == 0) {
12360832c9bbSMatthew Dillon 			bp = bio->bio_buf;
12377bc5b8c2SMatthew Dillon 			bp->b_flags |= B_AGE;
12380832c9bbSMatthew Dillon 			hammer_io_modify(&buffer->io, 1);
12390832c9bbSMatthew Dillon 			bcopy(bp->b_data, ptr, leaf->data_len);
12400832c9bbSMatthew Dillon 			hammer_io_modify_done(&buffer->io);
12417bc5b8c2SMatthew Dillon 			hammer_rel_buffer(buffer, 0);
12420832c9bbSMatthew Dillon 			bp->b_resid = 0;
12430832c9bbSMatthew Dillon 			biodone(bio);
12440832c9bbSMatthew Dillon 		}
124547637bffSMatthew Dillon 	}
1246e469566bSMatthew Dillon 	if (error == 0) {
1247e469566bSMatthew Dillon 		/*
1248e469566bSMatthew Dillon 		 * The record is all setup now, add it.  Potential conflics
1249e469566bSMatthew Dillon 		 * have already been dealt with.
1250e469566bSMatthew Dillon 		 */
1251e469566bSMatthew Dillon 		error = hammer_mem_add(record);
1252e469566bSMatthew Dillon 		KKASSERT(error == 0);
1253e469566bSMatthew Dillon 	} else {
1254e469566bSMatthew Dillon 		/*
12553214ade6SMatthew Dillon 		 * Major suckage occured.  Also note:  The record was never added
12563214ade6SMatthew Dillon 		 * to the tree so we do not have to worry about the backend.
1257e469566bSMatthew Dillon 		 */
1258cebe9493SMatthew Dillon 		kprintf("hammer_direct_write: failed @ %016llx\n",
1259973c11b9SMatthew Dillon 			(long long)leaf->data_offset);
126047637bffSMatthew Dillon 		bp = bio->bio_buf;
126147637bffSMatthew Dillon 		bp->b_resid = 0;
126247637bffSMatthew Dillon 		bp->b_error = EIO;
126347637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
126447637bffSMatthew Dillon 		biodone(bio);
1265e469566bSMatthew Dillon 		record->flags |= HAMMER_RECF_DELETED_FE;
1266e469566bSMatthew Dillon 		hammer_rel_mem_record(record);
126747637bffSMatthew Dillon 	}
126847637bffSMatthew Dillon 	return(error);
126947637bffSMatthew Dillon }
127047637bffSMatthew Dillon 
127143c665aeSMatthew Dillon /*
12721b0ab2c3SMatthew Dillon  * On completion of the BIO this callback must disconnect
12731b0ab2c3SMatthew Dillon  * it from the hammer_record and chain to the previous bio.
1274cdb6e4e6SMatthew Dillon  *
1275cdb6e4e6SMatthew Dillon  * An I/O error forces the mount to read-only.  Data buffers
1276cdb6e4e6SMatthew Dillon  * are not B_LOCKED like meta-data buffers are, so we have to
1277cdb6e4e6SMatthew Dillon  * throw the buffer away to prevent the kernel from retrying.
12781b0ab2c3SMatthew Dillon  */
12791b0ab2c3SMatthew Dillon static
12801b0ab2c3SMatthew Dillon void
12811b0ab2c3SMatthew Dillon hammer_io_direct_write_complete(struct bio *nbio)
12821b0ab2c3SMatthew Dillon {
12831b0ab2c3SMatthew Dillon 	struct bio *obio;
1284e469566bSMatthew Dillon 	struct buf *bp;
12851b0ab2c3SMatthew Dillon 	hammer_record_t record = nbio->bio_caller_info1.ptr;
12861b0ab2c3SMatthew Dillon 
1287e469566bSMatthew Dillon 	bp = nbio->bio_buf;
12881b0ab2c3SMatthew Dillon 	obio = pop_bio(nbio);
1289e469566bSMatthew Dillon 	if (bp->b_flags & B_ERROR) {
1290cdb6e4e6SMatthew Dillon 		hammer_critical_error(record->ip->hmp, record->ip,
1291e469566bSMatthew Dillon 				      bp->b_error,
1292cdb6e4e6SMatthew Dillon 				      "while writing bulk data");
1293e469566bSMatthew Dillon 		bp->b_flags |= B_INVAL;
1294cdb6e4e6SMatthew Dillon 	}
12951b0ab2c3SMatthew Dillon 	biodone(obio);
1296e469566bSMatthew Dillon 
1297e469566bSMatthew Dillon 	KKASSERT(record != NULL);
1298e469566bSMatthew Dillon 	KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
12991b0ab2c3SMatthew Dillon 	record->flags &= ~HAMMER_RECF_DIRECT_IO;
13001b0ab2c3SMatthew Dillon 	if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
13011b0ab2c3SMatthew Dillon 		record->flags &= ~HAMMER_RECF_DIRECT_WAIT;
13021b0ab2c3SMatthew Dillon 		wakeup(&record->flags);
13031b0ab2c3SMatthew Dillon 	}
13041b0ab2c3SMatthew Dillon }
13051b0ab2c3SMatthew Dillon 
13061b0ab2c3SMatthew Dillon 
13071b0ab2c3SMatthew Dillon /*
13081b0ab2c3SMatthew Dillon  * This is called before a record is either committed to the B-Tree
1309e469566bSMatthew Dillon  * or destroyed, to resolve any associated direct-IO.
13101b0ab2c3SMatthew Dillon  *
1311e469566bSMatthew Dillon  * (1) We must wait for any direct-IO related to the record to complete.
1312e469566bSMatthew Dillon  *
1313e469566bSMatthew Dillon  * (2) We must remove any buffer cache aliases for data accessed via
1314e469566bSMatthew Dillon  *     leaf->data_offset or zone2_offset so non-direct-IO consumers
1315e469566bSMatthew Dillon  *     (the mirroring and reblocking code) do not see stale data.
13161b0ab2c3SMatthew Dillon  */
13171b0ab2c3SMatthew Dillon void
13181b0ab2c3SMatthew Dillon hammer_io_direct_wait(hammer_record_t record)
13191b0ab2c3SMatthew Dillon {
1320e469566bSMatthew Dillon 	/*
1321e469566bSMatthew Dillon 	 * Wait for I/O to complete
1322e469566bSMatthew Dillon 	 */
1323e469566bSMatthew Dillon 	if (record->flags & HAMMER_RECF_DIRECT_IO) {
13241b0ab2c3SMatthew Dillon 		crit_enter();
13251b0ab2c3SMatthew Dillon 		while (record->flags & HAMMER_RECF_DIRECT_IO) {
13261b0ab2c3SMatthew Dillon 			record->flags |= HAMMER_RECF_DIRECT_WAIT;
13271b0ab2c3SMatthew Dillon 			tsleep(&record->flags, 0, "hmdiow", 0);
13281b0ab2c3SMatthew Dillon 		}
13291b0ab2c3SMatthew Dillon 		crit_exit();
13301b0ab2c3SMatthew Dillon 	}
13311b0ab2c3SMatthew Dillon 
13321b0ab2c3SMatthew Dillon 	/*
1333362ec2dcSMatthew Dillon 	 * Invalidate any related buffer cache aliases associated with the
1334362ec2dcSMatthew Dillon 	 * backing device.  This is needed because the buffer cache buffer
1335362ec2dcSMatthew Dillon 	 * for file data is associated with the file vnode, not the backing
1336362ec2dcSMatthew Dillon 	 * device vnode.
1337362ec2dcSMatthew Dillon 	 *
1338362ec2dcSMatthew Dillon 	 * XXX I do not think this case can occur any more now that
1339362ec2dcSMatthew Dillon 	 * reservations ensure that all such buffers are removed before
1340362ec2dcSMatthew Dillon 	 * an area can be reused.
1341e469566bSMatthew Dillon 	 */
1342e469566bSMatthew Dillon 	if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1343e469566bSMatthew Dillon 		KKASSERT(record->leaf.data_offset);
1344362ec2dcSMatthew Dillon 		hammer_del_buffers(record->ip->hmp, record->leaf.data_offset,
1345362ec2dcSMatthew Dillon 				   record->zone2_offset, record->leaf.data_len,
1346362ec2dcSMatthew Dillon 				   1);
1347e469566bSMatthew Dillon 		record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1348e469566bSMatthew Dillon 	}
1349e469566bSMatthew Dillon }
1350e469566bSMatthew Dillon 
1351e469566bSMatthew Dillon /*
135243c665aeSMatthew Dillon  * This is called to remove the second-level cached zone-2 offset from
135343c665aeSMatthew Dillon  * frontend buffer cache buffers, now stale due to a data relocation.
135443c665aeSMatthew Dillon  * These offsets are generated by cluster_read() via VOP_BMAP, or directly
135543c665aeSMatthew Dillon  * by hammer_vop_strategy_read().
135643c665aeSMatthew Dillon  *
135743c665aeSMatthew Dillon  * This is rather nasty because here we have something like the reblocker
135843c665aeSMatthew Dillon  * scanning the raw B-Tree with no held references on anything, really,
135943c665aeSMatthew Dillon  * other then a shared lock on the B-Tree node, and we have to access the
136043c665aeSMatthew Dillon  * frontend's buffer cache to check for and clean out the association.
136143c665aeSMatthew Dillon  * Specifically, if the reblocker is moving data on the disk, these cached
136243c665aeSMatthew Dillon  * offsets will become invalid.
136343c665aeSMatthew Dillon  *
136443c665aeSMatthew Dillon  * Only data record types associated with the large-data zone are subject
136543c665aeSMatthew Dillon  * to direct-io and need to be checked.
136643c665aeSMatthew Dillon  *
136743c665aeSMatthew Dillon  */
136843c665aeSMatthew Dillon void
136943c665aeSMatthew Dillon hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
137043c665aeSMatthew Dillon {
137143c665aeSMatthew Dillon 	struct hammer_inode_info iinfo;
137243c665aeSMatthew Dillon 	int zone;
137343c665aeSMatthew Dillon 
137443c665aeSMatthew Dillon 	if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
137543c665aeSMatthew Dillon 		return;
137643c665aeSMatthew Dillon 	zone = HAMMER_ZONE_DECODE(leaf->data_offset);
137743c665aeSMatthew Dillon 	if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
137843c665aeSMatthew Dillon 		return;
137943c665aeSMatthew Dillon 	iinfo.obj_id = leaf->base.obj_id;
138043c665aeSMatthew Dillon 	iinfo.obj_asof = 0;	/* unused */
138143c665aeSMatthew Dillon 	iinfo.obj_localization = leaf->base.localization &
13825a930e66SMatthew Dillon 				 HAMMER_LOCALIZE_PSEUDOFS_MASK;
138343c665aeSMatthew Dillon 	iinfo.u.leaf = leaf;
138443c665aeSMatthew Dillon 	hammer_scan_inode_snapshots(hmp, &iinfo,
138543c665aeSMatthew Dillon 				    hammer_io_direct_uncache_callback,
138643c665aeSMatthew Dillon 				    leaf);
138743c665aeSMatthew Dillon }
138843c665aeSMatthew Dillon 
138943c665aeSMatthew Dillon static int
139043c665aeSMatthew Dillon hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
139143c665aeSMatthew Dillon {
139243c665aeSMatthew Dillon 	hammer_inode_info_t iinfo = data;
139343c665aeSMatthew Dillon 	hammer_off_t data_offset;
139443c665aeSMatthew Dillon 	hammer_off_t file_offset;
139543c665aeSMatthew Dillon 	struct vnode *vp;
139643c665aeSMatthew Dillon 	struct buf *bp;
139743c665aeSMatthew Dillon 	int blksize;
139843c665aeSMatthew Dillon 
139943c665aeSMatthew Dillon 	if (ip->vp == NULL)
140043c665aeSMatthew Dillon 		return(0);
140143c665aeSMatthew Dillon 	data_offset = iinfo->u.leaf->data_offset;
140243c665aeSMatthew Dillon 	file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
140343c665aeSMatthew Dillon 	blksize = iinfo->u.leaf->data_len;
140443c665aeSMatthew Dillon 	KKASSERT((blksize & HAMMER_BUFMASK) == 0);
140543c665aeSMatthew Dillon 
140643c665aeSMatthew Dillon 	hammer_ref(&ip->lock);
140743c665aeSMatthew Dillon 	if (hammer_get_vnode(ip, &vp) == 0) {
1408*b1c20cfaSMatthew Dillon 		if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
140943c665aeSMatthew Dillon 		    bp->b_bio2.bio_offset != NOOFFSET) {
141043c665aeSMatthew Dillon 			bp = getblk(ip->vp, file_offset, blksize, 0, 0);
141143c665aeSMatthew Dillon 			bp->b_bio2.bio_offset = NOOFFSET;
141243c665aeSMatthew Dillon 			brelse(bp);
141343c665aeSMatthew Dillon 		}
141443c665aeSMatthew Dillon 		vput(vp);
141543c665aeSMatthew Dillon 	}
141643c665aeSMatthew Dillon 	hammer_rel_inode(ip, 0);
141743c665aeSMatthew Dillon 	return(0);
141843c665aeSMatthew Dillon }
141947637bffSMatthew Dillon 
1420748efb59SMatthew Dillon 
1421748efb59SMatthew Dillon /*
1422748efb59SMatthew Dillon  * This function is called when writes may have occured on the volume,
1423748efb59SMatthew Dillon  * indicating that the device may be holding cached writes.
1424748efb59SMatthew Dillon  */
1425748efb59SMatthew Dillon static void
1426748efb59SMatthew Dillon hammer_io_flush_mark(hammer_volume_t volume)
1427748efb59SMatthew Dillon {
1428748efb59SMatthew Dillon 	volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1429748efb59SMatthew Dillon }
1430748efb59SMatthew Dillon 
1431748efb59SMatthew Dillon /*
1432748efb59SMatthew Dillon  * This function ensures that the device has flushed any cached writes out.
1433748efb59SMatthew Dillon  */
1434748efb59SMatthew Dillon void
1435748efb59SMatthew Dillon hammer_io_flush_sync(hammer_mount_t hmp)
1436748efb59SMatthew Dillon {
1437748efb59SMatthew Dillon 	hammer_volume_t volume;
1438748efb59SMatthew Dillon 	struct buf *bp_base = NULL;
1439748efb59SMatthew Dillon 	struct buf *bp;
1440748efb59SMatthew Dillon 
1441748efb59SMatthew Dillon 	RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1442748efb59SMatthew Dillon 		if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1443748efb59SMatthew Dillon 			volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1444748efb59SMatthew Dillon 			bp = getpbuf(NULL);
1445748efb59SMatthew Dillon 			bp->b_bio1.bio_offset = 0;
1446748efb59SMatthew Dillon 			bp->b_bufsize = 0;
1447748efb59SMatthew Dillon 			bp->b_bcount = 0;
1448748efb59SMatthew Dillon 			bp->b_cmd = BUF_CMD_FLUSH;
1449748efb59SMatthew Dillon 			bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1450748efb59SMatthew Dillon 			bp->b_bio1.bio_done = hammer_io_flush_sync_done;
1451748efb59SMatthew Dillon 			bp->b_flags |= B_ASYNC;
1452748efb59SMatthew Dillon 			bp_base = bp;
1453748efb59SMatthew Dillon 			vn_strategy(volume->devvp, &bp->b_bio1);
1454748efb59SMatthew Dillon 		}
1455748efb59SMatthew Dillon 	}
1456748efb59SMatthew Dillon 	while ((bp = bp_base) != NULL) {
1457748efb59SMatthew Dillon 		bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1458748efb59SMatthew Dillon 		while (bp->b_cmd != BUF_CMD_DONE) {
1459748efb59SMatthew Dillon 			crit_enter();
1460748efb59SMatthew Dillon 			tsleep_interlock(&bp->b_cmd);
1461748efb59SMatthew Dillon 			if (bp->b_cmd != BUF_CMD_DONE)
1462748efb59SMatthew Dillon 				tsleep(&bp->b_cmd, 0, "hmrFLS", 0);
1463748efb59SMatthew Dillon 			crit_exit();
1464748efb59SMatthew Dillon 		}
1465748efb59SMatthew Dillon 		bp->b_flags &= ~B_ASYNC;
1466748efb59SMatthew Dillon 		relpbuf(bp, NULL);
1467748efb59SMatthew Dillon 	}
1468748efb59SMatthew Dillon }
1469748efb59SMatthew Dillon 
1470748efb59SMatthew Dillon /*
1471748efb59SMatthew Dillon  * Callback to deal with completed flush commands to the device.
1472748efb59SMatthew Dillon  */
1473748efb59SMatthew Dillon static void
1474748efb59SMatthew Dillon hammer_io_flush_sync_done(struct bio *bio)
1475748efb59SMatthew Dillon {
1476748efb59SMatthew Dillon 	struct buf *bp;
1477748efb59SMatthew Dillon 
1478748efb59SMatthew Dillon 	bp = bio->bio_buf;
1479748efb59SMatthew Dillon 	bp->b_cmd = BUF_CMD_DONE;
1480748efb59SMatthew Dillon 	wakeup(&bp->b_cmd);
1481748efb59SMatthew Dillon }
1482748efb59SMatthew Dillon 
1483