xref: /dflybsd-src/sys/vfs/hammer/hammer_io.c (revision eddadaee1e95dc7f038f9847210734017a7d08a3)
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 
65055f5ff8SMatthew Dillon 
66055f5ff8SMatthew Dillon /*
6710a5d1baSMatthew Dillon  * Initialize a new, already-zero'd hammer_io structure, or reinitialize
6810a5d1baSMatthew Dillon  * an existing hammer_io structure which may have switched to another type.
69055f5ff8SMatthew Dillon  */
70055f5ff8SMatthew Dillon void
71748efb59SMatthew Dillon hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
72055f5ff8SMatthew Dillon {
73748efb59SMatthew Dillon 	io->volume = volume;
74748efb59SMatthew Dillon 	io->hmp = volume->io.hmp;
75055f5ff8SMatthew Dillon 	io->type = type;
76055f5ff8SMatthew Dillon }
77055f5ff8SMatthew Dillon 
7866325755SMatthew Dillon /*
79fbc6e32aSMatthew Dillon  * Helper routine to disassociate a buffer cache buffer from an I/O
80ecca949aSMatthew Dillon  * structure.  The buffer is unlocked and marked appropriate for reclamation.
81055f5ff8SMatthew Dillon  *
82055f5ff8SMatthew Dillon  * The io may have 0 or 1 references depending on who called us.  The
83055f5ff8SMatthew Dillon  * caller is responsible for dealing with the refs.
84055f5ff8SMatthew Dillon  *
85055f5ff8SMatthew Dillon  * This call can only be made when no action is required on the buffer.
86ecca949aSMatthew Dillon  *
87ecca949aSMatthew Dillon  * The caller must own the buffer and the IO must indicate that the
88ecca949aSMatthew Dillon  * structure no longer owns it (io.released != 0).
8966325755SMatthew Dillon  */
9066325755SMatthew Dillon static void
91ecca949aSMatthew Dillon hammer_io_disassociate(hammer_io_structure_t iou)
9266325755SMatthew Dillon {
93055f5ff8SMatthew Dillon 	struct buf *bp = iou->io.bp;
9466325755SMatthew Dillon 
95ecca949aSMatthew Dillon 	KKASSERT(iou->io.released);
96b58c6388SMatthew Dillon 	KKASSERT(iou->io.modified == 0);
97af209b0fSMatthew Dillon 	KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
984d75d829SMatthew Dillon 	buf_dep_init(bp);
99055f5ff8SMatthew Dillon 	iou->io.bp = NULL;
1009f5097dcSMatthew Dillon 
1019f5097dcSMatthew Dillon 	/*
1029f5097dcSMatthew Dillon 	 * If the buffer was locked someone wanted to get rid of it.
1039f5097dcSMatthew Dillon 	 */
104a99b9ea2SMatthew Dillon 	if (bp->b_flags & B_LOCKED) {
105a99b9ea2SMatthew Dillon 		--hammer_count_io_locked;
106d8971d2bSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
107a99b9ea2SMatthew Dillon 	}
108ecca949aSMatthew Dillon 	if (iou->io.reclaim) {
109cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
110cebe9493SMatthew Dillon 		iou->io.reclaim = 0;
111ecca949aSMatthew Dillon 	}
11266325755SMatthew Dillon 
113055f5ff8SMatthew Dillon 	switch(iou->io.type) {
11466325755SMatthew Dillon 	case HAMMER_STRUCTURE_VOLUME:
115055f5ff8SMatthew Dillon 		iou->volume.ondisk = NULL;
11666325755SMatthew Dillon 		break;
11710a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_DATA_BUFFER:
11810a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_META_BUFFER:
11910a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_UNDO_BUFFER:
120055f5ff8SMatthew Dillon 		iou->buffer.ondisk = NULL;
12166325755SMatthew Dillon 		break;
122*eddadaeeSMatthew Dillon 	case HAMMER_STRUCTURE_DUMMY:
123*eddadaeeSMatthew Dillon 		panic("hammer_io_disassociate: bad io type");
124*eddadaeeSMatthew Dillon 		break;
12566325755SMatthew Dillon 	}
12666325755SMatthew Dillon }
127fbc6e32aSMatthew Dillon 
128fbc6e32aSMatthew Dillon /*
129055f5ff8SMatthew Dillon  * Wait for any physical IO to complete
130ae8e83e6SMatthew Dillon  *
131ae8e83e6SMatthew Dillon  * XXX we aren't interlocked against a spinlock or anything so there
132ae8e83e6SMatthew Dillon  *     is a small window in the interlock / io->running == 0 test.
133fbc6e32aSMatthew Dillon  */
1341b0ab2c3SMatthew Dillon void
135055f5ff8SMatthew Dillon hammer_io_wait(hammer_io_t io)
136fbc6e32aSMatthew Dillon {
137055f5ff8SMatthew Dillon 	if (io->running) {
138055f5ff8SMatthew Dillon 		for (;;) {
139ae8e83e6SMatthew Dillon 			io->waiting = 1;
140ae8e83e6SMatthew Dillon 			tsleep_interlock(io, 0);
141055f5ff8SMatthew Dillon 			if (io->running == 0)
142055f5ff8SMatthew Dillon 				break;
143ae8e83e6SMatthew Dillon 			tsleep(io, PINTERLOCKED, "hmrflw", hz);
144055f5ff8SMatthew Dillon 			if (io->running == 0)
145055f5ff8SMatthew Dillon 				break;
146055f5ff8SMatthew Dillon 		}
147055f5ff8SMatthew Dillon 	}
148055f5ff8SMatthew Dillon }
149055f5ff8SMatthew Dillon 
150af209b0fSMatthew Dillon /*
151*eddadaeeSMatthew Dillon  * Wait for all currently queued HAMMER-initiated I/Os to complete.
152*eddadaeeSMatthew Dillon  *
153*eddadaeeSMatthew Dillon  * This is not supposed to count direct I/O's but some can leak
154*eddadaeeSMatthew Dillon  * through (for non-full-sized direct I/Os).
155af209b0fSMatthew Dillon  */
156af209b0fSMatthew Dillon void
157*eddadaeeSMatthew Dillon hammer_io_wait_all(hammer_mount_t hmp, const char *ident, int doflush)
158af209b0fSMatthew Dillon {
159*eddadaeeSMatthew Dillon 	struct hammer_io iodummy;
160*eddadaeeSMatthew Dillon 	hammer_io_t io;
161*eddadaeeSMatthew Dillon 
162*eddadaeeSMatthew Dillon 	/*
163*eddadaeeSMatthew Dillon 	 * Degenerate case, no I/O is running
164*eddadaeeSMatthew Dillon 	 */
165af209b0fSMatthew Dillon 	crit_enter();
166*eddadaeeSMatthew Dillon 	if (TAILQ_EMPTY(&hmp->iorun_list)) {
167af209b0fSMatthew Dillon 		crit_exit();
168*eddadaeeSMatthew Dillon 		if (doflush)
169*eddadaeeSMatthew Dillon 			hammer_io_flush_sync(hmp);
170*eddadaeeSMatthew Dillon 		return;
171*eddadaeeSMatthew Dillon 	}
172*eddadaeeSMatthew Dillon 	bzero(&iodummy, sizeof(iodummy));
173*eddadaeeSMatthew Dillon 	iodummy.type = HAMMER_STRUCTURE_DUMMY;
174*eddadaeeSMatthew Dillon 
175*eddadaeeSMatthew Dillon 	/*
176*eddadaeeSMatthew Dillon 	 * Add placemarker and then wait until it becomes the head of
177*eddadaeeSMatthew Dillon 	 * the list.
178*eddadaeeSMatthew Dillon 	 */
179*eddadaeeSMatthew Dillon 	TAILQ_INSERT_TAIL(&hmp->iorun_list, &iodummy, iorun_entry);
180*eddadaeeSMatthew Dillon 	while (TAILQ_FIRST(&hmp->iorun_list) != &iodummy) {
181*eddadaeeSMatthew Dillon 		tsleep(&iodummy, 0, ident, 0);
182*eddadaeeSMatthew Dillon 	}
183*eddadaeeSMatthew Dillon 
184*eddadaeeSMatthew Dillon 	/*
185*eddadaeeSMatthew Dillon 	 * Chain in case several placemarkers are present.
186*eddadaeeSMatthew Dillon 	 */
187*eddadaeeSMatthew Dillon 	TAILQ_REMOVE(&hmp->iorun_list, &iodummy, iorun_entry);
188*eddadaeeSMatthew Dillon 	io = TAILQ_FIRST(&hmp->iorun_list);
189*eddadaeeSMatthew Dillon 	if (io && io->type == HAMMER_STRUCTURE_DUMMY)
190*eddadaeeSMatthew Dillon 		wakeup(io);
191*eddadaeeSMatthew Dillon 	crit_exit();
192*eddadaeeSMatthew Dillon 
193*eddadaeeSMatthew Dillon 	if (doflush)
194*eddadaeeSMatthew Dillon 		hammer_io_flush_sync(hmp);
195af209b0fSMatthew Dillon }
196af209b0fSMatthew Dillon 
1972faf0737SMatthew Dillon /*
1982faf0737SMatthew Dillon  * Clear a flagged error condition on a I/O buffer.  The caller must hold
1992faf0737SMatthew Dillon  * its own ref on the buffer.
2002faf0737SMatthew Dillon  */
2012faf0737SMatthew Dillon void
2022faf0737SMatthew Dillon hammer_io_clear_error(struct hammer_io *io)
2032faf0737SMatthew Dillon {
2042faf0737SMatthew Dillon 	if (io->ioerror) {
2052faf0737SMatthew Dillon 		io->ioerror = 0;
2062faf0737SMatthew Dillon 		hammer_unref(&io->lock);
2072faf0737SMatthew Dillon 		KKASSERT(io->lock.refs > 0);
2082faf0737SMatthew Dillon 	}
2092faf0737SMatthew Dillon }
2102faf0737SMatthew Dillon 
2112faf0737SMatthew Dillon 
2122f85fa4dSMatthew Dillon #define HAMMER_MAXRA	4
2132f85fa4dSMatthew Dillon 
21461aeeb33SMatthew Dillon /*
21510a5d1baSMatthew Dillon  * Load bp for a HAMMER structure.  The io must be exclusively locked by
21610a5d1baSMatthew Dillon  * the caller.
2172f85fa4dSMatthew Dillon  *
218a99b9ea2SMatthew Dillon  * This routine is mostly used on meta-data and small-data blocks.  Generally
219a99b9ea2SMatthew Dillon  * speaking HAMMER assumes some locality of reference and will cluster
220a99b9ea2SMatthew Dillon  * a 64K read.
221af209b0fSMatthew Dillon  *
222af209b0fSMatthew Dillon  * Note that clustering occurs at the device layer, not the logical layer.
223af209b0fSMatthew Dillon  * If the buffers do not apply to the current operation they may apply to
224af209b0fSMatthew Dillon  * some other.
22566325755SMatthew Dillon  */
22666325755SMatthew Dillon int
2272f85fa4dSMatthew Dillon hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
22866325755SMatthew Dillon {
22966325755SMatthew Dillon 	struct buf *bp;
23066325755SMatthew Dillon 	int   error;
23166325755SMatthew Dillon 
23266325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
233f5a07a7aSMatthew Dillon 		hammer_count_io_running_read += io->bytes;
234ce0138a6SMatthew Dillon 		if (hammer_cluster_enable) {
235ce0138a6SMatthew Dillon 			error = cluster_read(devvp, limit,
236ce0138a6SMatthew Dillon 					     io->offset, io->bytes,
237af209b0fSMatthew Dillon 					     HAMMER_CLUSTER_SIZE,
238af209b0fSMatthew Dillon 					     HAMMER_CLUSTER_BUFS, &io->bp);
239ce0138a6SMatthew Dillon 		} else {
2404a2796f3SMatthew Dillon 			error = bread(devvp, io->offset, io->bytes, &io->bp);
241ce0138a6SMatthew Dillon 		}
242ce0138a6SMatthew Dillon 		hammer_stats_disk_read += io->bytes;
243f5a07a7aSMatthew Dillon 		hammer_count_io_running_read -= io->bytes;
244cdb6e4e6SMatthew Dillon 
245cdb6e4e6SMatthew Dillon 		/*
246cdb6e4e6SMatthew Dillon 		 * The code generally assumes b_ops/b_dep has been set-up,
247cdb6e4e6SMatthew Dillon 		 * even if we error out here.
248cdb6e4e6SMatthew Dillon 		 */
24966325755SMatthew Dillon 		bp = io->bp;
25066325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
251af209b0fSMatthew Dillon 		KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
25266325755SMatthew Dillon 		LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
25366325755SMatthew Dillon 		BUF_KERNPROC(bp);
25410a5d1baSMatthew Dillon 		KKASSERT(io->modified == 0);
25510a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
25610a5d1baSMatthew Dillon 		KKASSERT(io->waiting == 0);
25766325755SMatthew Dillon 		io->released = 0;	/* we hold an active lock on bp */
25866325755SMatthew Dillon 	} else {
25966325755SMatthew Dillon 		error = 0;
26066325755SMatthew Dillon 	}
26166325755SMatthew Dillon 	return(error);
26266325755SMatthew Dillon }
26366325755SMatthew Dillon 
26466325755SMatthew Dillon /*
26566325755SMatthew Dillon  * Similar to hammer_io_read() but returns a zero'd out buffer instead.
26610a5d1baSMatthew Dillon  * Must be called with the IO exclusively locked.
267055f5ff8SMatthew Dillon  *
26810a5d1baSMatthew Dillon  * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
26910a5d1baSMatthew Dillon  * I/O by forcing the buffer to not be in a released state before calling
27010a5d1baSMatthew Dillon  * it.
27110a5d1baSMatthew Dillon  *
27210a5d1baSMatthew Dillon  * This function will also mark the IO as modified but it will not
27310a5d1baSMatthew Dillon  * increment the modify_refs count.
27466325755SMatthew Dillon  */
27566325755SMatthew Dillon int
27666325755SMatthew Dillon hammer_io_new(struct vnode *devvp, struct hammer_io *io)
27766325755SMatthew Dillon {
27866325755SMatthew Dillon 	struct buf *bp;
27966325755SMatthew Dillon 
28066325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
2814a2796f3SMatthew Dillon 		io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
28266325755SMatthew Dillon 		bp = io->bp;
28366325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
284af209b0fSMatthew Dillon 		KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
28566325755SMatthew Dillon 		LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
286055f5ff8SMatthew Dillon 		io->released = 0;
28710a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
288055f5ff8SMatthew Dillon 		io->waiting = 0;
28966325755SMatthew Dillon 		BUF_KERNPROC(bp);
29066325755SMatthew Dillon 	} else {
29166325755SMatthew Dillon 		if (io->released) {
29266325755SMatthew Dillon 			regetblk(bp);
29366325755SMatthew Dillon 			BUF_KERNPROC(bp);
294d113fda1SMatthew Dillon 			io->released = 0;
29566325755SMatthew Dillon 		}
29666325755SMatthew Dillon 	}
29710a5d1baSMatthew Dillon 	hammer_io_modify(io, 0);
29866325755SMatthew Dillon 	vfs_bio_clrbuf(bp);
29966325755SMatthew Dillon 	return(0);
30066325755SMatthew Dillon }
30166325755SMatthew Dillon 
30266325755SMatthew Dillon /*
3030e8bd897SMatthew Dillon  * Advance the activity count on the underlying buffer because
3040e8bd897SMatthew Dillon  * HAMMER does not getblk/brelse on every access.
3050e8bd897SMatthew Dillon  */
3060e8bd897SMatthew Dillon void
3070e8bd897SMatthew Dillon hammer_io_advance(struct hammer_io *io)
3080e8bd897SMatthew Dillon {
3090e8bd897SMatthew Dillon 	if (io->bp)
3100e8bd897SMatthew Dillon 		buf_act_advance(io->bp);
3110e8bd897SMatthew Dillon }
3120e8bd897SMatthew Dillon 
3130e8bd897SMatthew Dillon /*
31447637bffSMatthew Dillon  * Remove potential device level aliases against buffers managed by high level
315362ec2dcSMatthew Dillon  * vnodes.  Aliases can also be created due to mixed buffer sizes or via
316362ec2dcSMatthew Dillon  * direct access to the backing store device.
317e469566bSMatthew Dillon  *
318e469566bSMatthew Dillon  * This is nasty because the buffers are also VMIO-backed.  Even if a buffer
319e469566bSMatthew Dillon  * does not exist its backing VM pages might, and we have to invalidate
320e469566bSMatthew Dillon  * those as well or a getblk() will reinstate them.
321362ec2dcSMatthew Dillon  *
322362ec2dcSMatthew Dillon  * Buffer cache buffers associated with hammer_buffers cannot be
323362ec2dcSMatthew Dillon  * invalidated.
32447637bffSMatthew Dillon  */
325362ec2dcSMatthew Dillon int
32647637bffSMatthew Dillon hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
32747637bffSMatthew Dillon {
328cebe9493SMatthew Dillon 	hammer_io_structure_t iou;
32947637bffSMatthew Dillon 	hammer_off_t phys_offset;
33047637bffSMatthew Dillon 	struct buf *bp;
331362ec2dcSMatthew Dillon 	int error;
33247637bffSMatthew Dillon 
33347637bffSMatthew Dillon 	phys_offset = volume->ondisk->vol_buf_beg +
33447637bffSMatthew Dillon 		      (zone2_offset & HAMMER_OFF_SHORT_MASK);
3354a2796f3SMatthew Dillon 	crit_enter();
336b1c20cfaSMatthew Dillon 	if ((bp = findblk(volume->devvp, phys_offset, FINDBLK_TEST)) != NULL)
3374a2796f3SMatthew Dillon 		bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
338e469566bSMatthew Dillon 	else
339e469566bSMatthew Dillon 		bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
340cebe9493SMatthew Dillon 	if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
341362ec2dcSMatthew Dillon #if 0
3425c8d05e2SMatthew Dillon 		hammer_ref(&iou->io.lock);
3434a2796f3SMatthew Dillon 		hammer_io_clear_modify(&iou->io, 1);
344cebe9493SMatthew Dillon 		bundirty(bp);
345e83ca595SMatthew Dillon 		iou->io.released = 0;
346e83ca595SMatthew Dillon 		BUF_KERNPROC(bp);
347cebe9493SMatthew Dillon 		iou->io.reclaim = 1;
3485c8d05e2SMatthew Dillon 		iou->io.waitdep = 1;
349e83ca595SMatthew Dillon 		KKASSERT(iou->io.lock.refs == 1);
3505c8d05e2SMatthew Dillon 		hammer_rel_buffer(&iou->buffer, 0);
3515c8d05e2SMatthew Dillon 		/*hammer_io_deallocate(bp);*/
352362ec2dcSMatthew Dillon #endif
35304b04ca6SMatthew Dillon 		bqrelse(bp);
354362ec2dcSMatthew Dillon 		error = EAGAIN;
3550832c9bbSMatthew Dillon 	} else {
356cebe9493SMatthew Dillon 		KKASSERT((bp->b_flags & B_LOCKED) == 0);
357cebe9493SMatthew Dillon 		bundirty(bp);
358cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
359ecca949aSMatthew Dillon 		brelse(bp);
360362ec2dcSMatthew Dillon 		error = 0;
361e83ca595SMatthew Dillon 	}
3624a2796f3SMatthew Dillon 	crit_exit();
363362ec2dcSMatthew Dillon 	return(error);
3640832c9bbSMatthew Dillon }
36547637bffSMatthew Dillon 
36647637bffSMatthew Dillon /*
367b3deaf57SMatthew Dillon  * This routine is called on the last reference to a hammer structure.
368ecca949aSMatthew Dillon  * The io is usually interlocked with io.loading and io.refs must be 1.
369b3deaf57SMatthew Dillon  *
370ecca949aSMatthew Dillon  * This routine may return a non-NULL bp to the caller for dispoal.  Disposal
371ecca949aSMatthew Dillon  * simply means the caller finishes decrementing the ref-count on the
372ecca949aSMatthew Dillon  * IO structure then brelse()'s the bp.  The bp may or may not still be
373ecca949aSMatthew Dillon  * passively associated with the IO.
374ecca949aSMatthew Dillon  *
375ecca949aSMatthew Dillon  * The only requirement here is that modified meta-data and volume-header
376ecca949aSMatthew Dillon  * buffer may NOT be disassociated from the IO structure, and consequently
377ecca949aSMatthew Dillon  * we also leave such buffers actively associated with the IO if they already
378ecca949aSMatthew Dillon  * are (since the kernel can't do anything with them anyway).  Only the
379ecca949aSMatthew Dillon  * flusher is allowed to write such buffers out.  Modified pure-data and
380ecca949aSMatthew Dillon  * undo buffers are returned to the kernel but left passively associated
381ecca949aSMatthew Dillon  * so we can track when the kernel writes the bp out.
38266325755SMatthew Dillon  */
383ecca949aSMatthew Dillon struct buf *
38409ac686bSMatthew Dillon hammer_io_release(struct hammer_io *io, int flush)
38566325755SMatthew Dillon {
3869f5097dcSMatthew Dillon 	union hammer_io_structure *iou = (void *)io;
38766325755SMatthew Dillon 	struct buf *bp;
38866325755SMatthew Dillon 
389fbc6e32aSMatthew Dillon 	if ((bp = io->bp) == NULL)
390ecca949aSMatthew Dillon 		return(NULL);
391fbc6e32aSMatthew Dillon 
3920b075555SMatthew Dillon 	/*
39310a5d1baSMatthew Dillon 	 * Try to flush a dirty IO to disk if asked to by the
39410a5d1baSMatthew Dillon 	 * caller or if the kernel tried to flush the buffer in the past.
3950b075555SMatthew Dillon 	 *
39610a5d1baSMatthew Dillon 	 * Kernel-initiated flushes are only allowed for pure-data buffers.
39710a5d1baSMatthew Dillon 	 * meta-data and volume buffers can only be flushed explicitly
39810a5d1baSMatthew Dillon 	 * by HAMMER.
399055f5ff8SMatthew Dillon 	 */
40010a5d1baSMatthew Dillon 	if (io->modified) {
40109ac686bSMatthew Dillon 		if (flush) {
402710733a6SMatthew Dillon 			hammer_io_flush(io, 0);
40310a5d1baSMatthew Dillon 		} else if (bp->b_flags & B_LOCKED) {
40410a5d1baSMatthew Dillon 			switch(io->type) {
40510a5d1baSMatthew Dillon 			case HAMMER_STRUCTURE_DATA_BUFFER:
406710733a6SMatthew Dillon 				hammer_io_flush(io, 0);
407710733a6SMatthew Dillon 				break;
40810a5d1baSMatthew Dillon 			case HAMMER_STRUCTURE_UNDO_BUFFER:
409710733a6SMatthew Dillon 				hammer_io_flush(io, hammer_undo_reclaim(io));
41010a5d1baSMatthew Dillon 				break;
41110a5d1baSMatthew Dillon 			default:
41210a5d1baSMatthew Dillon 				break;
41310a5d1baSMatthew Dillon 			}
41410a5d1baSMatthew Dillon 		} /* else no explicit request to flush the buffer */
41510a5d1baSMatthew Dillon 	}
416055f5ff8SMatthew Dillon 
417055f5ff8SMatthew Dillon 	/*
4185c8d05e2SMatthew Dillon 	 * Wait for the IO to complete if asked to.  This occurs when
4195c8d05e2SMatthew Dillon 	 * the buffer must be disposed of definitively during an umount
4205c8d05e2SMatthew Dillon 	 * or buffer invalidation.
421055f5ff8SMatthew Dillon 	 */
422b58c6388SMatthew Dillon 	if (io->waitdep && io->running) {
423055f5ff8SMatthew Dillon 		hammer_io_wait(io);
424055f5ff8SMatthew Dillon 	}
425055f5ff8SMatthew Dillon 
426055f5ff8SMatthew Dillon 	/*
42710a5d1baSMatthew Dillon 	 * Return control of the buffer to the kernel (with the provisio
42810a5d1baSMatthew Dillon 	 * that our bioops can override kernel decisions with regards to
42910a5d1baSMatthew Dillon 	 * the buffer).
430055f5ff8SMatthew Dillon 	 */
431cebe9493SMatthew Dillon 	if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
43210a5d1baSMatthew Dillon 		/*
43310a5d1baSMatthew Dillon 		 * Always disassociate the bp if an explicit flush
43410a5d1baSMatthew Dillon 		 * was requested and the IO completed with no error
43510a5d1baSMatthew Dillon 		 * (so unmount can really clean up the structure).
43610a5d1baSMatthew Dillon 		 */
437055f5ff8SMatthew Dillon 		if (io->released) {
438055f5ff8SMatthew Dillon 			regetblk(bp);
43946fe7ae1SMatthew Dillon 			BUF_KERNPROC(bp);
440ecca949aSMatthew Dillon 		} else {
441ecca949aSMatthew Dillon 			io->released = 1;
442055f5ff8SMatthew Dillon 		}
443ecca949aSMatthew Dillon 		hammer_io_disassociate((hammer_io_structure_t)io);
444ecca949aSMatthew Dillon 		/* return the bp */
445055f5ff8SMatthew Dillon 	} else if (io->modified) {
44610a5d1baSMatthew Dillon 		/*
447ecca949aSMatthew Dillon 		 * Only certain IO types can be released to the kernel if
448ecca949aSMatthew Dillon 		 * the buffer has been modified.
449ecca949aSMatthew Dillon 		 *
450ecca949aSMatthew Dillon 		 * volume and meta-data IO types may only be explicitly
451ecca949aSMatthew Dillon 		 * flushed by HAMMER.
45210a5d1baSMatthew Dillon 		 */
45310a5d1baSMatthew Dillon 		switch(io->type) {
45410a5d1baSMatthew Dillon 		case HAMMER_STRUCTURE_DATA_BUFFER:
45510a5d1baSMatthew Dillon 		case HAMMER_STRUCTURE_UNDO_BUFFER:
456b58c6388SMatthew Dillon 			if (io->released == 0) {
457055f5ff8SMatthew Dillon 				io->released = 1;
458055f5ff8SMatthew Dillon 				bdwrite(bp);
459055f5ff8SMatthew Dillon 			}
46010a5d1baSMatthew Dillon 			break;
46110a5d1baSMatthew Dillon 		default:
46210a5d1baSMatthew Dillon 			break;
46310a5d1baSMatthew Dillon 		}
464ecca949aSMatthew Dillon 		bp = NULL;	/* bp left associated */
465055f5ff8SMatthew Dillon 	} else if (io->released == 0) {
46610a5d1baSMatthew Dillon 		/*
46710a5d1baSMatthew Dillon 		 * Clean buffers can be generally released to the kernel.
46810a5d1baSMatthew Dillon 		 * We leave the bp passively associated with the HAMMER
46910a5d1baSMatthew Dillon 		 * structure and use bioops to disconnect it later on
47010a5d1baSMatthew Dillon 		 * if the kernel wants to discard the buffer.
471ecca949aSMatthew Dillon 		 *
472ecca949aSMatthew Dillon 		 * We can steal the structure's ownership of the bp.
47310a5d1baSMatthew Dillon 		 */
474ecca949aSMatthew Dillon 		io->released = 1;
4759f5097dcSMatthew Dillon 		if (bp->b_flags & B_LOCKED) {
476ecca949aSMatthew Dillon 			hammer_io_disassociate(iou);
477ecca949aSMatthew Dillon 			/* return the bp */
4789f5097dcSMatthew Dillon 		} else {
479cebe9493SMatthew Dillon 			if (io->reclaim) {
480ecca949aSMatthew Dillon 				hammer_io_disassociate(iou);
481ecca949aSMatthew Dillon 				/* return the bp */
482cebe9493SMatthew Dillon 			} else {
483ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
4849f5097dcSMatthew Dillon 			}
485cebe9493SMatthew Dillon 		}
48619b97e01SMatthew Dillon 	} else {
48719b97e01SMatthew Dillon 		/*
488af209b0fSMatthew Dillon 		 * A released buffer is passively associate with our
489af209b0fSMatthew Dillon 		 * hammer_io structure.  The kernel cannot destroy it
490af209b0fSMatthew Dillon 		 * without making a bioops call.  If the kernel (B_LOCKED)
491af209b0fSMatthew Dillon 		 * or we (reclaim) requested that the buffer be destroyed
492af209b0fSMatthew Dillon 		 * we destroy it, otherwise we do a quick get/release to
493af209b0fSMatthew Dillon 		 * reset its position in the kernel's LRU list.
494af209b0fSMatthew Dillon 		 *
495af209b0fSMatthew Dillon 		 * Leaving the buffer passively associated allows us to
496af209b0fSMatthew Dillon 		 * use the kernel's LRU buffer flushing mechanisms rather
497af209b0fSMatthew Dillon 		 * then rolling our own.
498cb51be26SMatthew Dillon 		 *
499cb51be26SMatthew Dillon 		 * XXX there are two ways of doing this.  We can re-acquire
500cb51be26SMatthew Dillon 		 * and passively release to reset the LRU, or not.
50119b97e01SMatthew Dillon 		 */
502af209b0fSMatthew Dillon 		if (io->running == 0) {
50319b97e01SMatthew Dillon 			regetblk(bp);
504cebe9493SMatthew Dillon 			if ((bp->b_flags & B_LOCKED) || io->reclaim) {
505ecca949aSMatthew Dillon 				hammer_io_disassociate(iou);
506ecca949aSMatthew Dillon 				/* return the bp */
5079f5097dcSMatthew Dillon 			} else {
508ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
509ecca949aSMatthew Dillon 			}
510ecca949aSMatthew Dillon 		} else {
511ecca949aSMatthew Dillon 			/*
512ecca949aSMatthew Dillon 			 * bp is left passively associated but we do not
513ecca949aSMatthew Dillon 			 * try to reacquire it.  Interactions with the io
514ecca949aSMatthew Dillon 			 * structure will occur on completion of the bp's
515ecca949aSMatthew Dillon 			 * I/O.
516ecca949aSMatthew Dillon 			 */
517ecca949aSMatthew Dillon 			bp = NULL;
51819b97e01SMatthew Dillon 		}
5199f5097dcSMatthew Dillon 	}
520ecca949aSMatthew Dillon 	return(bp);
521055f5ff8SMatthew Dillon }
522055f5ff8SMatthew Dillon 
523055f5ff8SMatthew Dillon /*
524b33e2cc0SMatthew Dillon  * This routine is called with a locked IO when a flush is desired and
525b33e2cc0SMatthew Dillon  * no other references to the structure exists other then ours.  This
526b33e2cc0SMatthew Dillon  * routine is ONLY called when HAMMER believes it is safe to flush a
527b33e2cc0SMatthew Dillon  * potentially modified buffer out.
5280b075555SMatthew Dillon  */
5290b075555SMatthew Dillon void
530710733a6SMatthew Dillon hammer_io_flush(struct hammer_io *io, int reclaim)
5310b075555SMatthew Dillon {
532055f5ff8SMatthew Dillon 	struct buf *bp;
533055f5ff8SMatthew Dillon 
534055f5ff8SMatthew Dillon 	/*
53510a5d1baSMatthew Dillon 	 * Degenerate case - nothing to flush if nothing is dirty.
536055f5ff8SMatthew Dillon 	 */
537b58c6388SMatthew Dillon 	if (io->modified == 0) {
538055f5ff8SMatthew Dillon 		return;
539b58c6388SMatthew Dillon 	}
540055f5ff8SMatthew Dillon 
541055f5ff8SMatthew Dillon 	KKASSERT(io->bp);
5429f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs <= 0);
543055f5ff8SMatthew Dillon 
544b33e2cc0SMatthew Dillon 	/*
54577062c8aSMatthew Dillon 	 * Acquire ownership of the bp, particularly before we clear our
54677062c8aSMatthew Dillon 	 * modified flag.
54777062c8aSMatthew Dillon 	 *
54877062c8aSMatthew Dillon 	 * We are going to bawrite() this bp.  Don't leave a window where
54977062c8aSMatthew Dillon 	 * io->released is set, we actually own the bp rather then our
55077062c8aSMatthew Dillon 	 * buffer.
55177062c8aSMatthew Dillon 	 */
55277062c8aSMatthew Dillon 	bp = io->bp;
55377062c8aSMatthew Dillon 	if (io->released) {
55477062c8aSMatthew Dillon 		regetblk(bp);
55577062c8aSMatthew Dillon 		/* BUF_KERNPROC(io->bp); */
55677062c8aSMatthew Dillon 		/* io->released = 0; */
55777062c8aSMatthew Dillon 		KKASSERT(io->released);
55877062c8aSMatthew Dillon 		KKASSERT(io->bp == bp);
55977062c8aSMatthew Dillon 	}
56077062c8aSMatthew Dillon 	io->released = 1;
56177062c8aSMatthew Dillon 
562710733a6SMatthew Dillon 	if (reclaim) {
563710733a6SMatthew Dillon 		io->reclaim = 1;
564710733a6SMatthew Dillon 		if ((bp->b_flags & B_LOCKED) == 0) {
565710733a6SMatthew Dillon 			bp->b_flags |= B_LOCKED;
566710733a6SMatthew Dillon 			++hammer_count_io_locked;
567710733a6SMatthew Dillon 		}
568710733a6SMatthew Dillon 	}
569710733a6SMatthew Dillon 
57077062c8aSMatthew Dillon 	/*
57110a5d1baSMatthew Dillon 	 * Acquire exclusive access to the bp and then clear the modified
57210a5d1baSMatthew Dillon 	 * state of the buffer prior to issuing I/O to interlock any
57310a5d1baSMatthew Dillon 	 * modifications made while the I/O is in progress.  This shouldn't
57410a5d1baSMatthew Dillon 	 * happen anyway but losing data would be worse.  The modified bit
57510a5d1baSMatthew Dillon 	 * will be rechecked after the IO completes.
57610a5d1baSMatthew Dillon 	 *
5774a2796f3SMatthew Dillon 	 * NOTE: This call also finalizes the buffer's content (inval == 0).
5784a2796f3SMatthew Dillon 	 *
579b33e2cc0SMatthew Dillon 	 * This is only legal when lock.refs == 1 (otherwise we might clear
580b33e2cc0SMatthew Dillon 	 * the modified bit while there are still users of the cluster
581b33e2cc0SMatthew Dillon 	 * modifying the data).
582b33e2cc0SMatthew Dillon 	 *
583b33e2cc0SMatthew Dillon 	 * Do this before potentially blocking so any attempt to modify the
584b33e2cc0SMatthew Dillon 	 * ondisk while we are blocked blocks waiting for us.
585b33e2cc0SMatthew Dillon 	 */
5865c8d05e2SMatthew Dillon 	hammer_ref(&io->lock);
5874a2796f3SMatthew Dillon 	hammer_io_clear_modify(io, 0);
5885c8d05e2SMatthew Dillon 	hammer_unref(&io->lock);
589bcac4bbbSMatthew Dillon 
5906367d0f9SMatthew Dillon 	if (hammer_debug_io & 0x0002)
5916367d0f9SMatthew Dillon 		kprintf("hammer io_write %016jx\n", bp->b_bio1.bio_offset);
5926367d0f9SMatthew Dillon 
593bcac4bbbSMatthew Dillon 	/*
59410a5d1baSMatthew Dillon 	 * Transfer ownership to the kernel and initiate I/O.
59510a5d1baSMatthew Dillon 	 */
596055f5ff8SMatthew Dillon 	io->running = 1;
597f5a07a7aSMatthew Dillon 	io->hmp->io_running_space += io->bytes;
598*eddadaeeSMatthew Dillon 	TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
599f5a07a7aSMatthew Dillon 	hammer_count_io_running_write += io->bytes;
600055f5ff8SMatthew Dillon 	bawrite(bp);
601748efb59SMatthew Dillon 	hammer_io_flush_mark(io->volume);
602055f5ff8SMatthew Dillon }
603055f5ff8SMatthew Dillon 
604055f5ff8SMatthew Dillon /************************************************************************
605055f5ff8SMatthew Dillon  *				BUFFER DIRTYING				*
606055f5ff8SMatthew Dillon  ************************************************************************
607055f5ff8SMatthew Dillon  *
608055f5ff8SMatthew Dillon  * These routines deal with dependancies created when IO buffers get
609055f5ff8SMatthew Dillon  * modified.  The caller must call hammer_modify_*() on a referenced
610055f5ff8SMatthew Dillon  * HAMMER structure prior to modifying its on-disk data.
611055f5ff8SMatthew Dillon  *
612055f5ff8SMatthew Dillon  * Any intent to modify an IO buffer acquires the related bp and imposes
613055f5ff8SMatthew Dillon  * various write ordering dependancies.
614055f5ff8SMatthew Dillon  */
615055f5ff8SMatthew Dillon 
616055f5ff8SMatthew Dillon /*
61710a5d1baSMatthew Dillon  * Mark a HAMMER structure as undergoing modification.  Meta-data buffers
61810a5d1baSMatthew Dillon  * are locked until the flusher can deal with them, pure data buffers
61910a5d1baSMatthew Dillon  * can be written out.
620055f5ff8SMatthew Dillon  */
62110a5d1baSMatthew Dillon static
622b58c6388SMatthew Dillon void
62310a5d1baSMatthew Dillon hammer_io_modify(hammer_io_t io, int count)
624055f5ff8SMatthew Dillon {
62546fe7ae1SMatthew Dillon 	/*
6269f5097dcSMatthew Dillon 	 * io->modify_refs must be >= 0
6279f5097dcSMatthew Dillon 	 */
6289f5097dcSMatthew Dillon 	while (io->modify_refs < 0) {
6299f5097dcSMatthew Dillon 		io->waitmod = 1;
6309f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
6319f5097dcSMatthew Dillon 	}
6329f5097dcSMatthew Dillon 
6339f5097dcSMatthew Dillon 	/*
63446fe7ae1SMatthew Dillon 	 * Shortcut if nothing to do.
63546fe7ae1SMatthew Dillon 	 */
636055f5ff8SMatthew Dillon 	KKASSERT(io->lock.refs != 0 && io->bp != NULL);
63710a5d1baSMatthew Dillon 	io->modify_refs += count;
638b58c6388SMatthew Dillon 	if (io->modified && io->released == 0)
639b58c6388SMatthew Dillon 		return;
64046fe7ae1SMatthew Dillon 
641055f5ff8SMatthew Dillon 	hammer_lock_ex(&io->lock);
64210a5d1baSMatthew Dillon 	if (io->modified == 0) {
643cdb6e4e6SMatthew Dillon 		hammer_io_set_modlist(io);
64446fe7ae1SMatthew Dillon 		io->modified = 1;
64510a5d1baSMatthew Dillon 	}
646055f5ff8SMatthew Dillon 	if (io->released) {
647055f5ff8SMatthew Dillon 		regetblk(io->bp);
648055f5ff8SMatthew Dillon 		BUF_KERNPROC(io->bp);
649055f5ff8SMatthew Dillon 		io->released = 0;
65046fe7ae1SMatthew Dillon 		KKASSERT(io->modified != 0);
651055f5ff8SMatthew Dillon 	}
652055f5ff8SMatthew Dillon 	hammer_unlock(&io->lock);
6530b075555SMatthew Dillon }
6540b075555SMatthew Dillon 
65510a5d1baSMatthew Dillon static __inline
65610a5d1baSMatthew Dillon void
65710a5d1baSMatthew Dillon hammer_io_modify_done(hammer_io_t io)
65810a5d1baSMatthew Dillon {
65910a5d1baSMatthew Dillon 	KKASSERT(io->modify_refs > 0);
66010a5d1baSMatthew Dillon 	--io->modify_refs;
6619f5097dcSMatthew Dillon 	if (io->modify_refs == 0 && io->waitmod) {
6629f5097dcSMatthew Dillon 		io->waitmod = 0;
6639f5097dcSMatthew Dillon 		wakeup(io);
6649f5097dcSMatthew Dillon 	}
6659f5097dcSMatthew Dillon }
6669f5097dcSMatthew Dillon 
6679f5097dcSMatthew Dillon void
6689f5097dcSMatthew Dillon hammer_io_write_interlock(hammer_io_t io)
6699f5097dcSMatthew Dillon {
6709f5097dcSMatthew Dillon 	while (io->modify_refs != 0) {
6719f5097dcSMatthew Dillon 		io->waitmod = 1;
6729f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
6739f5097dcSMatthew Dillon 	}
6749f5097dcSMatthew Dillon 	io->modify_refs = -1;
6759f5097dcSMatthew Dillon }
6769f5097dcSMatthew Dillon 
6779f5097dcSMatthew Dillon void
6789f5097dcSMatthew Dillon hammer_io_done_interlock(hammer_io_t io)
6799f5097dcSMatthew Dillon {
6809f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs == -1);
6819f5097dcSMatthew Dillon 	io->modify_refs = 0;
6829f5097dcSMatthew Dillon 	if (io->waitmod) {
6839f5097dcSMatthew Dillon 		io->waitmod = 0;
6849f5097dcSMatthew Dillon 		wakeup(io);
6859f5097dcSMatthew Dillon 	}
68610a5d1baSMatthew Dillon }
68710a5d1baSMatthew Dillon 
6882f85fa4dSMatthew Dillon /*
6892f85fa4dSMatthew Dillon  * Caller intends to modify a volume's ondisk structure.
6902f85fa4dSMatthew Dillon  *
6912f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
6922f85fa4dSMatthew Dillon  * sync_lock.
6932f85fa4dSMatthew Dillon  */
6940b075555SMatthew Dillon void
69536f82b23SMatthew Dillon hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
69636f82b23SMatthew Dillon 		     void *base, int len)
6970b075555SMatthew Dillon {
6982f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
699055f5ff8SMatthew Dillon 
7002f85fa4dSMatthew Dillon 	hammer_io_modify(&volume->io, 1);
70147197d71SMatthew Dillon 	if (len) {
70247197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
70347197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
70402428fb6SMatthew Dillon 		hammer_generate_undo(trans,
70547197d71SMatthew Dillon 			 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
70647197d71SMatthew Dillon 			 base, len);
707055f5ff8SMatthew Dillon 	}
708055f5ff8SMatthew Dillon }
709055f5ff8SMatthew Dillon 
710055f5ff8SMatthew Dillon /*
7112f85fa4dSMatthew Dillon  * Caller intends to modify a buffer's ondisk structure.
7122f85fa4dSMatthew Dillon  *
7132f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
7142f85fa4dSMatthew Dillon  * sync_lock.
715055f5ff8SMatthew Dillon  */
716055f5ff8SMatthew Dillon void
71736f82b23SMatthew Dillon hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
71836f82b23SMatthew Dillon 		     void *base, int len)
71946fe7ae1SMatthew Dillon {
7202f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
7212f85fa4dSMatthew Dillon 
72210a5d1baSMatthew Dillon 	hammer_io_modify(&buffer->io, 1);
72347197d71SMatthew Dillon 	if (len) {
72447197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
72547197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
72602428fb6SMatthew Dillon 		hammer_generate_undo(trans,
72734d829f7SMatthew Dillon 				     buffer->zone2_offset + rel_offset,
72847197d71SMatthew Dillon 				     base, len);
72947197d71SMatthew Dillon 	}
73046fe7ae1SMatthew Dillon }
73146fe7ae1SMatthew Dillon 
73210a5d1baSMatthew Dillon void
73310a5d1baSMatthew Dillon hammer_modify_volume_done(hammer_volume_t volume)
73410a5d1baSMatthew Dillon {
73510a5d1baSMatthew Dillon 	hammer_io_modify_done(&volume->io);
73610a5d1baSMatthew Dillon }
73710a5d1baSMatthew Dillon 
73810a5d1baSMatthew Dillon void
73910a5d1baSMatthew Dillon hammer_modify_buffer_done(hammer_buffer_t buffer)
74010a5d1baSMatthew Dillon {
74110a5d1baSMatthew Dillon 	hammer_io_modify_done(&buffer->io);
74210a5d1baSMatthew Dillon }
74310a5d1baSMatthew Dillon 
74446fe7ae1SMatthew Dillon /*
7454a2796f3SMatthew Dillon  * Mark an entity as not being dirty any more and finalize any
7464a2796f3SMatthew Dillon  * delayed adjustments to the buffer.
7474a2796f3SMatthew Dillon  *
7484a2796f3SMatthew Dillon  * Delayed adjustments are an important performance enhancement, allowing
7494a2796f3SMatthew Dillon  * us to avoid recalculating B-Tree node CRCs over and over again when
7504a2796f3SMatthew Dillon  * making bulk-modifications to the B-Tree.
7514a2796f3SMatthew Dillon  *
7524a2796f3SMatthew Dillon  * If inval is non-zero delayed adjustments are ignored.
7535c8d05e2SMatthew Dillon  *
7545c8d05e2SMatthew Dillon  * This routine may dereference related btree nodes and cause the
7555c8d05e2SMatthew Dillon  * buffer to be dereferenced.  The caller must own a reference on io.
75661aeeb33SMatthew Dillon  */
75761aeeb33SMatthew Dillon void
7584a2796f3SMatthew Dillon hammer_io_clear_modify(struct hammer_io *io, int inval)
75961aeeb33SMatthew Dillon {
7604a2796f3SMatthew Dillon 	if (io->modified == 0)
7614a2796f3SMatthew Dillon 		return;
7624a2796f3SMatthew Dillon 
7634a2796f3SMatthew Dillon 	/*
7644a2796f3SMatthew Dillon 	 * Take us off the mod-list and clear the modified bit.
7654a2796f3SMatthew Dillon 	 */
766cebe9493SMatthew Dillon 	KKASSERT(io->mod_list != NULL);
767cebe9493SMatthew Dillon 	if (io->mod_list == &io->hmp->volu_list ||
768cebe9493SMatthew Dillon 	    io->mod_list == &io->hmp->meta_list) {
769f5a07a7aSMatthew Dillon 		io->hmp->locked_dirty_space -= io->bytes;
770f5a07a7aSMatthew Dillon 		hammer_count_dirtybufspace -= io->bytes;
771cebe9493SMatthew Dillon 	}
772cebe9493SMatthew Dillon 	TAILQ_REMOVE(io->mod_list, io, mod_entry);
773cebe9493SMatthew Dillon 	io->mod_list = NULL;
77461aeeb33SMatthew Dillon 	io->modified = 0;
7754a2796f3SMatthew Dillon 
7764a2796f3SMatthew Dillon 	/*
7774a2796f3SMatthew Dillon 	 * If this bit is not set there are no delayed adjustments.
7784a2796f3SMatthew Dillon 	 */
7794a2796f3SMatthew Dillon 	if (io->gencrc == 0)
7804a2796f3SMatthew Dillon 		return;
7814a2796f3SMatthew Dillon 	io->gencrc = 0;
7824a2796f3SMatthew Dillon 
7834a2796f3SMatthew Dillon 	/*
7844a2796f3SMatthew Dillon 	 * Finalize requested CRCs.  The NEEDSCRC flag also holds a reference
7854a2796f3SMatthew Dillon 	 * on the node (& underlying buffer).  Release the node after clearing
7864a2796f3SMatthew Dillon 	 * the flag.
7874a2796f3SMatthew Dillon 	 */
7884a2796f3SMatthew Dillon 	if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
7894a2796f3SMatthew Dillon 		hammer_buffer_t buffer = (void *)io;
7904a2796f3SMatthew Dillon 		hammer_node_t node;
7914a2796f3SMatthew Dillon 
7924a2796f3SMatthew Dillon restart:
7934a2796f3SMatthew Dillon 		TAILQ_FOREACH(node, &buffer->clist, entry) {
7944a2796f3SMatthew Dillon 			if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
7954a2796f3SMatthew Dillon 				continue;
7964a2796f3SMatthew Dillon 			node->flags &= ~HAMMER_NODE_NEEDSCRC;
7974a2796f3SMatthew Dillon 			KKASSERT(node->ondisk);
7984a2796f3SMatthew Dillon 			if (inval == 0)
7994a2796f3SMatthew Dillon 				node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
8004a2796f3SMatthew Dillon 			hammer_rel_node(node);
8014a2796f3SMatthew Dillon 			goto restart;
80261aeeb33SMatthew Dillon 		}
80361aeeb33SMatthew Dillon 	}
8045c8d05e2SMatthew Dillon 	/* caller must still have ref on io */
8055c8d05e2SMatthew Dillon 	KKASSERT(io->lock.refs > 0);
8064a2796f3SMatthew Dillon }
8074a2796f3SMatthew Dillon 
808cebe9493SMatthew Dillon /*
809cebe9493SMatthew Dillon  * Clear the IO's modify list.  Even though the IO is no longer modified
810cebe9493SMatthew Dillon  * it may still be on the lose_list.  This routine is called just before
811cebe9493SMatthew Dillon  * the governing hammer_buffer is destroyed.
812cebe9493SMatthew Dillon  */
813cebe9493SMatthew Dillon void
814cebe9493SMatthew Dillon hammer_io_clear_modlist(struct hammer_io *io)
815cebe9493SMatthew Dillon {
8164a2796f3SMatthew Dillon 	KKASSERT(io->modified == 0);
817cebe9493SMatthew Dillon 	if (io->mod_list) {
818a99b9ea2SMatthew Dillon 		crit_enter();	/* biodone race against list */
819cebe9493SMatthew Dillon 		KKASSERT(io->mod_list == &io->hmp->lose_list);
820cebe9493SMatthew Dillon 		TAILQ_REMOVE(io->mod_list, io, mod_entry);
821cebe9493SMatthew Dillon 		io->mod_list = NULL;
822a99b9ea2SMatthew Dillon 		crit_exit();
823cebe9493SMatthew Dillon 	}
82466325755SMatthew Dillon }
82566325755SMatthew Dillon 
826cdb6e4e6SMatthew Dillon static void
827cdb6e4e6SMatthew Dillon hammer_io_set_modlist(struct hammer_io *io)
828cdb6e4e6SMatthew Dillon {
829cdb6e4e6SMatthew Dillon 	struct hammer_mount *hmp = io->hmp;
830cdb6e4e6SMatthew Dillon 
831cdb6e4e6SMatthew Dillon 	KKASSERT(io->mod_list == NULL);
832cdb6e4e6SMatthew Dillon 
833cdb6e4e6SMatthew Dillon 	switch(io->type) {
834cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_VOLUME:
835cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->volu_list;
836cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
837cdb6e4e6SMatthew Dillon 		hammer_count_dirtybufspace += io->bytes;
838cdb6e4e6SMatthew Dillon 		break;
839cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_META_BUFFER:
840cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->meta_list;
841cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
842cdb6e4e6SMatthew Dillon 		hammer_count_dirtybufspace += io->bytes;
843cdb6e4e6SMatthew Dillon 		break;
844cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_UNDO_BUFFER:
845cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->undo_list;
846cdb6e4e6SMatthew Dillon 		break;
847cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_DATA_BUFFER:
848cdb6e4e6SMatthew Dillon 		io->mod_list = &hmp->data_list;
849cdb6e4e6SMatthew Dillon 		break;
850*eddadaeeSMatthew Dillon 	case HAMMER_STRUCTURE_DUMMY:
851*eddadaeeSMatthew Dillon 		panic("hammer_io_disassociate: bad io type");
852*eddadaeeSMatthew Dillon 		break;
853cdb6e4e6SMatthew Dillon 	}
854cdb6e4e6SMatthew Dillon 	TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
855cdb6e4e6SMatthew Dillon }
856cdb6e4e6SMatthew Dillon 
857055f5ff8SMatthew Dillon /************************************************************************
858055f5ff8SMatthew Dillon  *				HAMMER_BIOOPS				*
859055f5ff8SMatthew Dillon  ************************************************************************
860055f5ff8SMatthew Dillon  *
861055f5ff8SMatthew Dillon  */
862055f5ff8SMatthew Dillon 
863055f5ff8SMatthew Dillon /*
864055f5ff8SMatthew Dillon  * Pre-IO initiation kernel callback - cluster build only
865055f5ff8SMatthew Dillon  */
866055f5ff8SMatthew Dillon static void
867055f5ff8SMatthew Dillon hammer_io_start(struct buf *bp)
868055f5ff8SMatthew Dillon {
869055f5ff8SMatthew Dillon }
870055f5ff8SMatthew Dillon 
871055f5ff8SMatthew Dillon /*
8727bc5b8c2SMatthew Dillon  * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
873b33e2cc0SMatthew Dillon  *
874b33e2cc0SMatthew Dillon  * NOTE: HAMMER may modify a buffer after initiating I/O.  The modified bit
875b33e2cc0SMatthew Dillon  * may also be set if we were marking a cluster header open.  Only remove
876b33e2cc0SMatthew Dillon  * our dependancy if the modified bit is clear.
877055f5ff8SMatthew Dillon  */
87866325755SMatthew Dillon static void
87966325755SMatthew Dillon hammer_io_complete(struct buf *bp)
88066325755SMatthew Dillon {
881055f5ff8SMatthew Dillon 	union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
882*eddadaeeSMatthew Dillon 	struct hammer_io *ionext;
883fbc6e32aSMatthew Dillon 
884055f5ff8SMatthew Dillon 	KKASSERT(iou->io.released == 1);
885055f5ff8SMatthew Dillon 
886bf3b416bSMatthew Dillon 	/*
887bf3b416bSMatthew Dillon 	 * Deal with people waiting for I/O to drain
888bf3b416bSMatthew Dillon 	 */
889f90dde4cSMatthew Dillon 	if (iou->io.running) {
890cdb6e4e6SMatthew Dillon 		/*
891cdb6e4e6SMatthew Dillon 		 * Deal with critical write errors.  Once a critical error
892cdb6e4e6SMatthew Dillon 		 * has been flagged in hmp the UNDO FIFO will not be updated.
893cdb6e4e6SMatthew Dillon 		 * That way crash recover will give us a consistent
894cdb6e4e6SMatthew Dillon 		 * filesystem.
895cdb6e4e6SMatthew Dillon 		 *
896cdb6e4e6SMatthew Dillon 		 * Because of this we can throw away failed UNDO buffers.  If
897cdb6e4e6SMatthew Dillon 		 * we throw away META or DATA buffers we risk corrupting
898cdb6e4e6SMatthew Dillon 		 * the now read-only version of the filesystem visible to
899cdb6e4e6SMatthew Dillon 		 * the user.  Clear B_ERROR so the buffer is not re-dirtied
900cdb6e4e6SMatthew Dillon 		 * by the kernel and ref the io so it doesn't get thrown
901cdb6e4e6SMatthew Dillon 		 * away.
902cdb6e4e6SMatthew Dillon 		 */
903cdb6e4e6SMatthew Dillon 		if (bp->b_flags & B_ERROR) {
904cdb6e4e6SMatthew Dillon 			hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
905cdb6e4e6SMatthew Dillon 					      "while flushing meta-data");
906cdb6e4e6SMatthew Dillon 			switch(iou->io.type) {
907cdb6e4e6SMatthew Dillon 			case HAMMER_STRUCTURE_UNDO_BUFFER:
908cdb6e4e6SMatthew Dillon 				break;
909cdb6e4e6SMatthew Dillon 			default:
910cdb6e4e6SMatthew Dillon 				if (iou->io.ioerror == 0) {
911cdb6e4e6SMatthew Dillon 					iou->io.ioerror = 1;
912cdb6e4e6SMatthew Dillon 					if (iou->io.lock.refs == 0)
913cdb6e4e6SMatthew Dillon 						++hammer_count_refedbufs;
914cdb6e4e6SMatthew Dillon 					hammer_ref(&iou->io.lock);
915cdb6e4e6SMatthew Dillon 				}
916cdb6e4e6SMatthew Dillon 				break;
917cdb6e4e6SMatthew Dillon 			}
918cdb6e4e6SMatthew Dillon 			bp->b_flags &= ~B_ERROR;
919cdb6e4e6SMatthew Dillon 			bundirty(bp);
920cdb6e4e6SMatthew Dillon #if 0
921cdb6e4e6SMatthew Dillon 			hammer_io_set_modlist(&iou->io);
922cdb6e4e6SMatthew Dillon 			iou->io.modified = 1;
923cdb6e4e6SMatthew Dillon #endif
924cdb6e4e6SMatthew Dillon 		}
925ce0138a6SMatthew Dillon 		hammer_stats_disk_write += iou->io.bytes;
926f5a07a7aSMatthew Dillon 		hammer_count_io_running_write -= iou->io.bytes;
927f5a07a7aSMatthew Dillon 		iou->io.hmp->io_running_space -= iou->io.bytes;
928f5a07a7aSMatthew Dillon 		KKASSERT(iou->io.hmp->io_running_space >= 0);
929f90dde4cSMatthew Dillon 		iou->io.running = 0;
930*eddadaeeSMatthew Dillon 
931*eddadaeeSMatthew Dillon 		/*
932*eddadaeeSMatthew Dillon 		 * Remove from iorun list and wakeup any multi-io waiter(s).
933*eddadaeeSMatthew Dillon 		 */
934*eddadaeeSMatthew Dillon 		if (TAILQ_FIRST(&iou->io.hmp->iorun_list) == &iou->io) {
935*eddadaeeSMatthew Dillon 			ionext = TAILQ_NEXT(&iou->io, iorun_entry);
936*eddadaeeSMatthew Dillon 			if (ionext && ionext->type == HAMMER_STRUCTURE_DUMMY)
937*eddadaeeSMatthew Dillon 				wakeup(ionext);
938*eddadaeeSMatthew Dillon 		}
939*eddadaeeSMatthew Dillon 		TAILQ_REMOVE(&iou->io.hmp->iorun_list, &iou->io, iorun_entry);
940ce0138a6SMatthew Dillon 	} else {
941ce0138a6SMatthew Dillon 		hammer_stats_disk_read += iou->io.bytes;
942f90dde4cSMatthew Dillon 	}
943f90dde4cSMatthew Dillon 
944055f5ff8SMatthew Dillon 	if (iou->io.waiting) {
945055f5ff8SMatthew Dillon 		iou->io.waiting = 0;
946055f5ff8SMatthew Dillon 		wakeup(iou);
947055f5ff8SMatthew Dillon 	}
948055f5ff8SMatthew Dillon 
949055f5ff8SMatthew Dillon 	/*
950bf3b416bSMatthew Dillon 	 * If B_LOCKED is set someone wanted to deallocate the bp at some
951bf3b416bSMatthew Dillon 	 * point, do it now if refs has become zero.
952055f5ff8SMatthew Dillon 	 */
953055f5ff8SMatthew Dillon 	if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
954b33e2cc0SMatthew Dillon 		KKASSERT(iou->io.modified == 0);
955a99b9ea2SMatthew Dillon 		--hammer_count_io_locked;
956d5ef456eSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
957055f5ff8SMatthew Dillon 		hammer_io_deallocate(bp);
958055f5ff8SMatthew Dillon 		/* structure may be dead now */
959fbc6e32aSMatthew Dillon 	}
96066325755SMatthew Dillon }
96166325755SMatthew Dillon 
96266325755SMatthew Dillon /*
96366325755SMatthew Dillon  * Callback from kernel when it wishes to deallocate a passively
96410a5d1baSMatthew Dillon  * associated structure.  This mostly occurs with clean buffers
96510a5d1baSMatthew Dillon  * but it may be possible for a holding structure to be marked dirty
9667bc5b8c2SMatthew Dillon  * while its buffer is passively associated.  The caller owns the bp.
96766325755SMatthew Dillon  *
96866325755SMatthew Dillon  * If we cannot disassociate we set B_LOCKED to prevent the buffer
96966325755SMatthew Dillon  * from getting reused.
97046fe7ae1SMatthew Dillon  *
97146fe7ae1SMatthew Dillon  * WARNING: Because this can be called directly by getnewbuf we cannot
97246fe7ae1SMatthew Dillon  * recurse into the tree.  If a bp cannot be immediately disassociated
97346fe7ae1SMatthew Dillon  * our only recourse is to set B_LOCKED.
9747bc5b8c2SMatthew Dillon  *
9757bc5b8c2SMatthew Dillon  * WARNING: This may be called from an interrupt via hammer_io_complete()
97666325755SMatthew Dillon  */
97766325755SMatthew Dillon static void
97866325755SMatthew Dillon hammer_io_deallocate(struct buf *bp)
97966325755SMatthew Dillon {
980055f5ff8SMatthew Dillon 	hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
98166325755SMatthew Dillon 
982055f5ff8SMatthew Dillon 	KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
98346fe7ae1SMatthew Dillon 	if (iou->io.lock.refs > 0 || iou->io.modified) {
98410a5d1baSMatthew Dillon 		/*
98510a5d1baSMatthew Dillon 		 * It is not legal to disassociate a modified buffer.  This
98610a5d1baSMatthew Dillon 		 * case really shouldn't ever occur.
98710a5d1baSMatthew Dillon 		 */
988055f5ff8SMatthew Dillon 		bp->b_flags |= B_LOCKED;
989a99b9ea2SMatthew Dillon 		++hammer_count_io_locked;
990055f5ff8SMatthew Dillon 	} else {
99110a5d1baSMatthew Dillon 		/*
99210a5d1baSMatthew Dillon 		 * Disassociate the BP.  If the io has no refs left we
99310a5d1baSMatthew Dillon 		 * have to add it to the loose list.
99410a5d1baSMatthew Dillon 		 */
995ecca949aSMatthew Dillon 		hammer_io_disassociate(iou);
996ecca949aSMatthew Dillon 		if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
997ecca949aSMatthew Dillon 			KKASSERT(iou->io.bp == NULL);
99810a5d1baSMatthew Dillon 			KKASSERT(iou->io.mod_list == NULL);
999a99b9ea2SMatthew Dillon 			crit_enter();	/* biodone race against list */
100010a5d1baSMatthew Dillon 			iou->io.mod_list = &iou->io.hmp->lose_list;
100110a5d1baSMatthew Dillon 			TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
1002a99b9ea2SMatthew Dillon 			crit_exit();
100366325755SMatthew Dillon 		}
100466325755SMatthew Dillon 	}
100566325755SMatthew Dillon }
100666325755SMatthew Dillon 
100766325755SMatthew Dillon static int
100866325755SMatthew Dillon hammer_io_fsync(struct vnode *vp)
100966325755SMatthew Dillon {
101066325755SMatthew Dillon 	return(0);
101166325755SMatthew Dillon }
101266325755SMatthew Dillon 
101366325755SMatthew Dillon /*
101466325755SMatthew Dillon  * NOTE: will not be called unless we tell the kernel about the
101566325755SMatthew Dillon  * bioops.  Unused... we use the mount's VFS_SYNC instead.
101666325755SMatthew Dillon  */
101766325755SMatthew Dillon static int
101866325755SMatthew Dillon hammer_io_sync(struct mount *mp)
101966325755SMatthew Dillon {
102066325755SMatthew Dillon 	return(0);
102166325755SMatthew Dillon }
102266325755SMatthew Dillon 
102366325755SMatthew Dillon static void
102466325755SMatthew Dillon hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
102566325755SMatthew Dillon {
102666325755SMatthew Dillon }
102766325755SMatthew Dillon 
102866325755SMatthew Dillon /*
102966325755SMatthew Dillon  * I/O pre-check for reading and writing.  HAMMER only uses this for
103066325755SMatthew Dillon  * B_CACHE buffers so checkread just shouldn't happen, but if it does
103166325755SMatthew Dillon  * allow it.
103266325755SMatthew Dillon  *
1033fbc6e32aSMatthew Dillon  * Writing is a different case.  We don't want the kernel to try to write
1034fbc6e32aSMatthew Dillon  * out a buffer that HAMMER may be modifying passively or which has a
103510a5d1baSMatthew Dillon  * dependancy.  In addition, kernel-demanded writes can only proceed for
103610a5d1baSMatthew Dillon  * certain types of buffers (i.e. UNDO and DATA types).  Other dirty
103710a5d1baSMatthew Dillon  * buffer types can only be explicitly written by the flusher.
1038fbc6e32aSMatthew Dillon  *
103910a5d1baSMatthew Dillon  * checkwrite will only be called for bdwrite()n buffers.  If we return
104010a5d1baSMatthew Dillon  * success the kernel is guaranteed to initiate the buffer write.
104166325755SMatthew Dillon  */
104266325755SMatthew Dillon static int
104366325755SMatthew Dillon hammer_io_checkread(struct buf *bp)
104466325755SMatthew Dillon {
104566325755SMatthew Dillon 	return(0);
104666325755SMatthew Dillon }
104766325755SMatthew Dillon 
104866325755SMatthew Dillon static int
104966325755SMatthew Dillon hammer_io_checkwrite(struct buf *bp)
105066325755SMatthew Dillon {
105110a5d1baSMatthew Dillon 	hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
105266325755SMatthew Dillon 
105377062c8aSMatthew Dillon 	/*
105477062c8aSMatthew Dillon 	 * This shouldn't happen under normal operation.
105577062c8aSMatthew Dillon 	 */
105677062c8aSMatthew Dillon 	if (io->type == HAMMER_STRUCTURE_VOLUME ||
105777062c8aSMatthew Dillon 	    io->type == HAMMER_STRUCTURE_META_BUFFER) {
105877062c8aSMatthew Dillon 		if (!panicstr)
105977062c8aSMatthew Dillon 			panic("hammer_io_checkwrite: illegal buffer");
1060a99b9ea2SMatthew Dillon 		if ((bp->b_flags & B_LOCKED) == 0) {
106177062c8aSMatthew Dillon 			bp->b_flags |= B_LOCKED;
1062a99b9ea2SMatthew Dillon 			++hammer_count_io_locked;
1063a99b9ea2SMatthew Dillon 		}
106477062c8aSMatthew Dillon 		return(1);
106577062c8aSMatthew Dillon 	}
1066c9b9e29dSMatthew Dillon 
1067fbc6e32aSMatthew Dillon 	/*
106810a5d1baSMatthew Dillon 	 * We can only clear the modified bit if the IO is not currently
106910a5d1baSMatthew Dillon 	 * undergoing modification.  Otherwise we may miss changes.
10705c8d05e2SMatthew Dillon 	 *
10715c8d05e2SMatthew Dillon 	 * Only data and undo buffers can reach here.  These buffers do
10725c8d05e2SMatthew Dillon 	 * not have terminal crc functions but we temporarily reference
10735c8d05e2SMatthew Dillon 	 * the IO anyway, just in case.
1074b33e2cc0SMatthew Dillon 	 */
10755c8d05e2SMatthew Dillon 	if (io->modify_refs == 0 && io->modified) {
10765c8d05e2SMatthew Dillon 		hammer_ref(&io->lock);
10774a2796f3SMatthew Dillon 		hammer_io_clear_modify(io, 0);
10785c8d05e2SMatthew Dillon 		hammer_unref(&io->lock);
10795c8d05e2SMatthew Dillon 	} else if (io->modified) {
10805c8d05e2SMatthew Dillon 		KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
10815c8d05e2SMatthew Dillon 	}
1082f90dde4cSMatthew Dillon 
1083f90dde4cSMatthew Dillon 	/*
1084f90dde4cSMatthew Dillon 	 * The kernel is going to start the IO, set io->running.
1085f90dde4cSMatthew Dillon 	 */
1086f90dde4cSMatthew Dillon 	KKASSERT(io->running == 0);
1087f90dde4cSMatthew Dillon 	io->running = 1;
1088f5a07a7aSMatthew Dillon 	io->hmp->io_running_space += io->bytes;
1089*eddadaeeSMatthew Dillon 	TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
1090f5a07a7aSMatthew Dillon 	hammer_count_io_running_write += io->bytes;
1091055f5ff8SMatthew Dillon 	return(0);
1092055f5ff8SMatthew Dillon }
109366325755SMatthew Dillon 
10948cd0a023SMatthew Dillon /*
109566325755SMatthew Dillon  * Return non-zero if we wish to delay the kernel's attempt to flush
109666325755SMatthew Dillon  * this buffer to disk.
109766325755SMatthew Dillon  */
109866325755SMatthew Dillon static int
109966325755SMatthew Dillon hammer_io_countdeps(struct buf *bp, int n)
110066325755SMatthew Dillon {
110166325755SMatthew Dillon 	return(0);
110266325755SMatthew Dillon }
110366325755SMatthew Dillon 
110466325755SMatthew Dillon struct bio_ops hammer_bioops = {
110566325755SMatthew Dillon 	.io_start	= hammer_io_start,
110666325755SMatthew Dillon 	.io_complete	= hammer_io_complete,
110766325755SMatthew Dillon 	.io_deallocate	= hammer_io_deallocate,
110866325755SMatthew Dillon 	.io_fsync	= hammer_io_fsync,
110966325755SMatthew Dillon 	.io_sync	= hammer_io_sync,
111066325755SMatthew Dillon 	.io_movedeps	= hammer_io_movedeps,
111166325755SMatthew Dillon 	.io_countdeps	= hammer_io_countdeps,
111266325755SMatthew Dillon 	.io_checkread	= hammer_io_checkread,
111366325755SMatthew Dillon 	.io_checkwrite	= hammer_io_checkwrite,
111466325755SMatthew Dillon };
111566325755SMatthew Dillon 
111647637bffSMatthew Dillon /************************************************************************
111747637bffSMatthew Dillon  *				DIRECT IO OPS 				*
111847637bffSMatthew Dillon  ************************************************************************
111947637bffSMatthew Dillon  *
112047637bffSMatthew Dillon  * These functions operate directly on the buffer cache buffer associated
112147637bffSMatthew Dillon  * with a front-end vnode rather then a back-end device vnode.
112247637bffSMatthew Dillon  */
112347637bffSMatthew Dillon 
112447637bffSMatthew Dillon /*
112547637bffSMatthew Dillon  * Read a buffer associated with a front-end vnode directly from the
11261b0ab2c3SMatthew Dillon  * disk media.  The bio may be issued asynchronously.  If leaf is non-NULL
11271b0ab2c3SMatthew Dillon  * we validate the CRC.
1128a99b9ea2SMatthew Dillon  *
11291b0ab2c3SMatthew Dillon  * We must check for the presence of a HAMMER buffer to handle the case
11301b0ab2c3SMatthew Dillon  * where the reblocker has rewritten the data (which it does via the HAMMER
11311b0ab2c3SMatthew Dillon  * buffer system, not via the high-level vnode buffer cache), but not yet
11321b0ab2c3SMatthew Dillon  * committed the buffer to the media.
113347637bffSMatthew Dillon  */
113447637bffSMatthew Dillon int
11351b0ab2c3SMatthew Dillon hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
11361b0ab2c3SMatthew Dillon 		      hammer_btree_leaf_elm_t leaf)
113747637bffSMatthew Dillon {
11381b0ab2c3SMatthew Dillon 	hammer_off_t buf_offset;
113947637bffSMatthew Dillon 	hammer_off_t zone2_offset;
114047637bffSMatthew Dillon 	hammer_volume_t volume;
114147637bffSMatthew Dillon 	struct buf *bp;
114247637bffSMatthew Dillon 	struct bio *nbio;
114347637bffSMatthew Dillon 	int vol_no;
114447637bffSMatthew Dillon 	int error;
114547637bffSMatthew Dillon 
11461b0ab2c3SMatthew Dillon 	buf_offset = bio->bio_offset;
11471b0ab2c3SMatthew Dillon 	KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
11481b0ab2c3SMatthew Dillon 		 HAMMER_ZONE_LARGE_DATA);
11494a2796f3SMatthew Dillon 
11501b0ab2c3SMatthew Dillon 	/*
11511b0ab2c3SMatthew Dillon 	 * The buffer cache may have an aliased buffer (the reblocker can
11521b0ab2c3SMatthew Dillon 	 * write them).  If it does we have to sync any dirty data before
11531b0ab2c3SMatthew Dillon 	 * we can build our direct-read.  This is a non-critical code path.
11541b0ab2c3SMatthew Dillon 	 */
11551b0ab2c3SMatthew Dillon 	bp = bio->bio_buf;
11561b0ab2c3SMatthew Dillon 	hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
11571b0ab2c3SMatthew Dillon 
11581b0ab2c3SMatthew Dillon 	/*
11591b0ab2c3SMatthew Dillon 	 * Resolve to a zone-2 offset.  The conversion just requires
11601b0ab2c3SMatthew Dillon 	 * munging the top 4 bits but we want to abstract it anyway
11611b0ab2c3SMatthew Dillon 	 * so the blockmap code can verify the zone assignment.
11621b0ab2c3SMatthew Dillon 	 */
11631b0ab2c3SMatthew Dillon 	zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
11641b0ab2c3SMatthew Dillon 	if (error)
11651b0ab2c3SMatthew Dillon 		goto done;
116643c665aeSMatthew Dillon 	KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
116743c665aeSMatthew Dillon 		 HAMMER_ZONE_RAW_BUFFER);
116843c665aeSMatthew Dillon 
11691b0ab2c3SMatthew Dillon 	/*
11701b0ab2c3SMatthew Dillon 	 * Resolve volume and raw-offset for 3rd level bio.  The
11711b0ab2c3SMatthew Dillon 	 * offset will be specific to the volume.
11721b0ab2c3SMatthew Dillon 	 */
117347637bffSMatthew Dillon 	vol_no = HAMMER_VOL_DECODE(zone2_offset);
117447637bffSMatthew Dillon 	volume = hammer_get_volume(hmp, vol_no, &error);
117547637bffSMatthew Dillon 	if (error == 0 && zone2_offset >= volume->maxbuf_off)
117647637bffSMatthew Dillon 		error = EIO;
117743c665aeSMatthew Dillon 
117847637bffSMatthew Dillon 	if (error == 0) {
1179e469566bSMatthew Dillon 		/*
1180e469566bSMatthew Dillon 		 * 3rd level bio
1181e469566bSMatthew Dillon 		 */
118247637bffSMatthew Dillon 		nbio = push_bio(bio);
118347637bffSMatthew Dillon 		nbio->bio_offset = volume->ondisk->vol_buf_beg +
1184e469566bSMatthew Dillon 				   (zone2_offset & HAMMER_OFF_SHORT_MASK);
11851b0ab2c3SMatthew Dillon #if 0
11861b0ab2c3SMatthew Dillon 		/*
11871b0ab2c3SMatthew Dillon 		 * XXX disabled - our CRC check doesn't work if the OS
11881b0ab2c3SMatthew Dillon 		 * does bogus_page replacement on the direct-read.
11891b0ab2c3SMatthew Dillon 		 */
11901b0ab2c3SMatthew Dillon 		if (leaf && hammer_verify_data) {
11911b0ab2c3SMatthew Dillon 			nbio->bio_done = hammer_io_direct_read_complete;
11921b0ab2c3SMatthew Dillon 			nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
11931b0ab2c3SMatthew Dillon 		}
11941b0ab2c3SMatthew Dillon #endif
1195ce0138a6SMatthew Dillon 		hammer_stats_disk_read += bp->b_bufsize;
119647637bffSMatthew Dillon 		vn_strategy(volume->devvp, nbio);
119747637bffSMatthew Dillon 	}
119847637bffSMatthew Dillon 	hammer_rel_volume(volume, 0);
11991b0ab2c3SMatthew Dillon done:
120047637bffSMatthew Dillon 	if (error) {
1201cebe9493SMatthew Dillon 		kprintf("hammer_direct_read: failed @ %016llx\n",
1202973c11b9SMatthew Dillon 			(long long)zone2_offset);
120347637bffSMatthew Dillon 		bp->b_error = error;
120447637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
120547637bffSMatthew Dillon 		biodone(bio);
120647637bffSMatthew Dillon 	}
120747637bffSMatthew Dillon 	return(error);
120847637bffSMatthew Dillon }
120947637bffSMatthew Dillon 
12101b0ab2c3SMatthew Dillon #if 0
12111b0ab2c3SMatthew Dillon /*
12121b0ab2c3SMatthew Dillon  * On completion of the BIO this callback must check the data CRC
12131b0ab2c3SMatthew Dillon  * and chain to the previous bio.
12141b0ab2c3SMatthew Dillon  */
12151b0ab2c3SMatthew Dillon static
12161b0ab2c3SMatthew Dillon void
12171b0ab2c3SMatthew Dillon hammer_io_direct_read_complete(struct bio *nbio)
12181b0ab2c3SMatthew Dillon {
12191b0ab2c3SMatthew Dillon 	struct bio *obio;
12201b0ab2c3SMatthew Dillon 	struct buf *bp;
12211b0ab2c3SMatthew Dillon 	u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
12221b0ab2c3SMatthew Dillon 
12231b0ab2c3SMatthew Dillon 	bp = nbio->bio_buf;
12241b0ab2c3SMatthew Dillon 	if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
12251b0ab2c3SMatthew Dillon 		kprintf("HAMMER: data_crc error @%016llx/%d\n",
12261b0ab2c3SMatthew Dillon 			nbio->bio_offset, bp->b_bufsize);
1227fc73edd8SMatthew Dillon 		if (hammer_debug_critical)
1228fc73edd8SMatthew Dillon 			Debugger("data_crc on read");
12291b0ab2c3SMatthew Dillon 		bp->b_flags |= B_ERROR;
12301b0ab2c3SMatthew Dillon 		bp->b_error = EIO;
12311b0ab2c3SMatthew Dillon 	}
12321b0ab2c3SMatthew Dillon 	obio = pop_bio(nbio);
12331b0ab2c3SMatthew Dillon 	biodone(obio);
12341b0ab2c3SMatthew Dillon }
12351b0ab2c3SMatthew Dillon #endif
12361b0ab2c3SMatthew Dillon 
123747637bffSMatthew Dillon /*
123847637bffSMatthew Dillon  * Write a buffer associated with a front-end vnode directly to the
123947637bffSMatthew Dillon  * disk media.  The bio may be issued asynchronously.
12401b0ab2c3SMatthew Dillon  *
12411b0ab2c3SMatthew Dillon  * The BIO is associated with the specified record and RECF_DIRECT_IO
1242e469566bSMatthew Dillon  * is set.  The recorded is added to its object.
124347637bffSMatthew Dillon  */
124447637bffSMatthew Dillon int
12451b0ab2c3SMatthew Dillon hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
124647637bffSMatthew Dillon 		       struct bio *bio)
124747637bffSMatthew Dillon {
12481b0ab2c3SMatthew Dillon 	hammer_btree_leaf_elm_t leaf = &record->leaf;
12490832c9bbSMatthew Dillon 	hammer_off_t buf_offset;
125047637bffSMatthew Dillon 	hammer_off_t zone2_offset;
125147637bffSMatthew Dillon 	hammer_volume_t volume;
12520832c9bbSMatthew Dillon 	hammer_buffer_t buffer;
125347637bffSMatthew Dillon 	struct buf *bp;
125447637bffSMatthew Dillon 	struct bio *nbio;
12550832c9bbSMatthew Dillon 	char *ptr;
125647637bffSMatthew Dillon 	int vol_no;
125747637bffSMatthew Dillon 	int error;
125847637bffSMatthew Dillon 
12590832c9bbSMatthew Dillon 	buf_offset = leaf->data_offset;
12600832c9bbSMatthew Dillon 
12610832c9bbSMatthew Dillon 	KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
126247637bffSMatthew Dillon 	KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
126347637bffSMatthew Dillon 
12640832c9bbSMatthew Dillon 	if ((buf_offset & HAMMER_BUFMASK) == 0 &&
12654a2796f3SMatthew Dillon 	    leaf->data_len >= HAMMER_BUFSIZE) {
12660832c9bbSMatthew Dillon 		/*
12670832c9bbSMatthew Dillon 		 * We are using the vnode's bio to write directly to the
12680832c9bbSMatthew Dillon 		 * media, any hammer_buffer at the same zone-X offset will
12690832c9bbSMatthew Dillon 		 * now have stale data.
12700832c9bbSMatthew Dillon 		 */
12710832c9bbSMatthew Dillon 		zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
127247637bffSMatthew Dillon 		vol_no = HAMMER_VOL_DECODE(zone2_offset);
127347637bffSMatthew Dillon 		volume = hammer_get_volume(hmp, vol_no, &error);
127447637bffSMatthew Dillon 
127547637bffSMatthew Dillon 		if (error == 0 && zone2_offset >= volume->maxbuf_off)
127647637bffSMatthew Dillon 			error = EIO;
127747637bffSMatthew Dillon 		if (error == 0) {
12780832c9bbSMatthew Dillon 			bp = bio->bio_buf;
12794a2796f3SMatthew Dillon 			KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1280e469566bSMatthew Dillon 			/*
12814a2796f3SMatthew Dillon 			hammer_del_buffers(hmp, buf_offset,
12824a2796f3SMatthew Dillon 					   zone2_offset, bp->b_bufsize);
1283e469566bSMatthew Dillon 			*/
12841b0ab2c3SMatthew Dillon 
128543c665aeSMatthew Dillon 			/*
128643c665aeSMatthew Dillon 			 * Second level bio - cached zone2 offset.
12871b0ab2c3SMatthew Dillon 			 *
12881b0ab2c3SMatthew Dillon 			 * (We can put our bio_done function in either the
12891b0ab2c3SMatthew Dillon 			 *  2nd or 3rd level).
129043c665aeSMatthew Dillon 			 */
129147637bffSMatthew Dillon 			nbio = push_bio(bio);
129243c665aeSMatthew Dillon 			nbio->bio_offset = zone2_offset;
12931b0ab2c3SMatthew Dillon 			nbio->bio_done = hammer_io_direct_write_complete;
12941b0ab2c3SMatthew Dillon 			nbio->bio_caller_info1.ptr = record;
1295e469566bSMatthew Dillon 			record->zone2_offset = zone2_offset;
1296e469566bSMatthew Dillon 			record->flags |= HAMMER_RECF_DIRECT_IO |
1297e469566bSMatthew Dillon 					 HAMMER_RECF_DIRECT_INVAL;
129843c665aeSMatthew Dillon 
129943c665aeSMatthew Dillon 			/*
130043c665aeSMatthew Dillon 			 * Third level bio - raw offset specific to the
130143c665aeSMatthew Dillon 			 * correct volume.
130243c665aeSMatthew Dillon 			 */
130343c665aeSMatthew Dillon 			zone2_offset &= HAMMER_OFF_SHORT_MASK;
130443c665aeSMatthew Dillon 			nbio = push_bio(nbio);
130547637bffSMatthew Dillon 			nbio->bio_offset = volume->ondisk->vol_buf_beg +
13060832c9bbSMatthew Dillon 					   zone2_offset;
1307ce0138a6SMatthew Dillon 			hammer_stats_disk_write += bp->b_bufsize;
130847637bffSMatthew Dillon 			vn_strategy(volume->devvp, nbio);
1309748efb59SMatthew Dillon 			hammer_io_flush_mark(volume);
131047637bffSMatthew Dillon 		}
131147637bffSMatthew Dillon 		hammer_rel_volume(volume, 0);
13120832c9bbSMatthew Dillon 	} else {
13131b0ab2c3SMatthew Dillon 		/*
13141b0ab2c3SMatthew Dillon 		 * Must fit in a standard HAMMER buffer.  In this case all
13151b0ab2c3SMatthew Dillon 		 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
13161b0ab2c3SMatthew Dillon 		 * does not need to be set-up.
13171b0ab2c3SMatthew Dillon 		 */
13180832c9bbSMatthew Dillon 		KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
13190832c9bbSMatthew Dillon 		buffer = NULL;
13200832c9bbSMatthew Dillon 		ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
13210832c9bbSMatthew Dillon 		if (error == 0) {
13220832c9bbSMatthew Dillon 			bp = bio->bio_buf;
13237bc5b8c2SMatthew Dillon 			bp->b_flags |= B_AGE;
13240832c9bbSMatthew Dillon 			hammer_io_modify(&buffer->io, 1);
13250832c9bbSMatthew Dillon 			bcopy(bp->b_data, ptr, leaf->data_len);
13260832c9bbSMatthew Dillon 			hammer_io_modify_done(&buffer->io);
13277bc5b8c2SMatthew Dillon 			hammer_rel_buffer(buffer, 0);
13280832c9bbSMatthew Dillon 			bp->b_resid = 0;
13290832c9bbSMatthew Dillon 			biodone(bio);
13300832c9bbSMatthew Dillon 		}
133147637bffSMatthew Dillon 	}
1332e469566bSMatthew Dillon 	if (error == 0) {
1333e469566bSMatthew Dillon 		/*
1334e469566bSMatthew Dillon 		 * The record is all setup now, add it.  Potential conflics
1335e469566bSMatthew Dillon 		 * have already been dealt with.
1336e469566bSMatthew Dillon 		 */
1337e469566bSMatthew Dillon 		error = hammer_mem_add(record);
1338e469566bSMatthew Dillon 		KKASSERT(error == 0);
1339e469566bSMatthew Dillon 	} else {
1340e469566bSMatthew Dillon 		/*
13413214ade6SMatthew Dillon 		 * Major suckage occured.  Also note:  The record was never added
13423214ade6SMatthew Dillon 		 * to the tree so we do not have to worry about the backend.
1343e469566bSMatthew Dillon 		 */
1344cebe9493SMatthew Dillon 		kprintf("hammer_direct_write: failed @ %016llx\n",
1345973c11b9SMatthew Dillon 			(long long)leaf->data_offset);
134647637bffSMatthew Dillon 		bp = bio->bio_buf;
134747637bffSMatthew Dillon 		bp->b_resid = 0;
134847637bffSMatthew Dillon 		bp->b_error = EIO;
134947637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
135047637bffSMatthew Dillon 		biodone(bio);
1351e469566bSMatthew Dillon 		record->flags |= HAMMER_RECF_DELETED_FE;
1352e469566bSMatthew Dillon 		hammer_rel_mem_record(record);
135347637bffSMatthew Dillon 	}
135447637bffSMatthew Dillon 	return(error);
135547637bffSMatthew Dillon }
135647637bffSMatthew Dillon 
135743c665aeSMatthew Dillon /*
13581b0ab2c3SMatthew Dillon  * On completion of the BIO this callback must disconnect
13591b0ab2c3SMatthew Dillon  * it from the hammer_record and chain to the previous bio.
1360cdb6e4e6SMatthew Dillon  *
1361cdb6e4e6SMatthew Dillon  * An I/O error forces the mount to read-only.  Data buffers
1362cdb6e4e6SMatthew Dillon  * are not B_LOCKED like meta-data buffers are, so we have to
1363cdb6e4e6SMatthew Dillon  * throw the buffer away to prevent the kernel from retrying.
13641b0ab2c3SMatthew Dillon  */
13651b0ab2c3SMatthew Dillon static
13661b0ab2c3SMatthew Dillon void
13671b0ab2c3SMatthew Dillon hammer_io_direct_write_complete(struct bio *nbio)
13681b0ab2c3SMatthew Dillon {
13691b0ab2c3SMatthew Dillon 	struct bio *obio;
1370e469566bSMatthew Dillon 	struct buf *bp;
13711b0ab2c3SMatthew Dillon 	hammer_record_t record = nbio->bio_caller_info1.ptr;
13721b0ab2c3SMatthew Dillon 
1373e469566bSMatthew Dillon 	bp = nbio->bio_buf;
13741b0ab2c3SMatthew Dillon 	obio = pop_bio(nbio);
1375e469566bSMatthew Dillon 	if (bp->b_flags & B_ERROR) {
1376cdb6e4e6SMatthew Dillon 		hammer_critical_error(record->ip->hmp, record->ip,
1377e469566bSMatthew Dillon 				      bp->b_error,
1378cdb6e4e6SMatthew Dillon 				      "while writing bulk data");
1379e469566bSMatthew Dillon 		bp->b_flags |= B_INVAL;
1380cdb6e4e6SMatthew Dillon 	}
13811b0ab2c3SMatthew Dillon 	biodone(obio);
1382e469566bSMatthew Dillon 
1383e469566bSMatthew Dillon 	KKASSERT(record != NULL);
1384e469566bSMatthew Dillon 	KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
13851b0ab2c3SMatthew Dillon 	if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1386de996e86SMatthew Dillon 		record->flags &= ~(HAMMER_RECF_DIRECT_IO |
1387de996e86SMatthew Dillon 				   HAMMER_RECF_DIRECT_WAIT);
1388de996e86SMatthew Dillon 		/* record can disappear once DIRECT_IO flag is cleared */
13891b0ab2c3SMatthew Dillon 		wakeup(&record->flags);
1390de996e86SMatthew Dillon 	} else {
1391de996e86SMatthew Dillon 		record->flags &= ~HAMMER_RECF_DIRECT_IO;
1392de996e86SMatthew Dillon 		/* record can disappear once DIRECT_IO flag is cleared */
13931b0ab2c3SMatthew Dillon 	}
13941b0ab2c3SMatthew Dillon }
13951b0ab2c3SMatthew Dillon 
13961b0ab2c3SMatthew Dillon 
13971b0ab2c3SMatthew Dillon /*
13981b0ab2c3SMatthew Dillon  * This is called before a record is either committed to the B-Tree
1399e469566bSMatthew Dillon  * or destroyed, to resolve any associated direct-IO.
14001b0ab2c3SMatthew Dillon  *
1401e469566bSMatthew Dillon  * (1) We must wait for any direct-IO related to the record to complete.
1402e469566bSMatthew Dillon  *
1403e469566bSMatthew Dillon  * (2) We must remove any buffer cache aliases for data accessed via
1404e469566bSMatthew Dillon  *     leaf->data_offset or zone2_offset so non-direct-IO consumers
1405e469566bSMatthew Dillon  *     (the mirroring and reblocking code) do not see stale data.
14061b0ab2c3SMatthew Dillon  */
14071b0ab2c3SMatthew Dillon void
14081b0ab2c3SMatthew Dillon hammer_io_direct_wait(hammer_record_t record)
14091b0ab2c3SMatthew Dillon {
1410e469566bSMatthew Dillon 	/*
1411e469566bSMatthew Dillon 	 * Wait for I/O to complete
1412e469566bSMatthew Dillon 	 */
1413e469566bSMatthew Dillon 	if (record->flags & HAMMER_RECF_DIRECT_IO) {
14141b0ab2c3SMatthew Dillon 		crit_enter();
14151b0ab2c3SMatthew Dillon 		while (record->flags & HAMMER_RECF_DIRECT_IO) {
14161b0ab2c3SMatthew Dillon 			record->flags |= HAMMER_RECF_DIRECT_WAIT;
14171b0ab2c3SMatthew Dillon 			tsleep(&record->flags, 0, "hmdiow", 0);
14181b0ab2c3SMatthew Dillon 		}
14191b0ab2c3SMatthew Dillon 		crit_exit();
14201b0ab2c3SMatthew Dillon 	}
14211b0ab2c3SMatthew Dillon 
14221b0ab2c3SMatthew Dillon 	/*
1423362ec2dcSMatthew Dillon 	 * Invalidate any related buffer cache aliases associated with the
1424362ec2dcSMatthew Dillon 	 * backing device.  This is needed because the buffer cache buffer
1425362ec2dcSMatthew Dillon 	 * for file data is associated with the file vnode, not the backing
1426362ec2dcSMatthew Dillon 	 * device vnode.
1427362ec2dcSMatthew Dillon 	 *
1428362ec2dcSMatthew Dillon 	 * XXX I do not think this case can occur any more now that
1429362ec2dcSMatthew Dillon 	 * reservations ensure that all such buffers are removed before
1430362ec2dcSMatthew Dillon 	 * an area can be reused.
1431e469566bSMatthew Dillon 	 */
1432e469566bSMatthew Dillon 	if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1433e469566bSMatthew Dillon 		KKASSERT(record->leaf.data_offset);
1434362ec2dcSMatthew Dillon 		hammer_del_buffers(record->ip->hmp, record->leaf.data_offset,
1435362ec2dcSMatthew Dillon 				   record->zone2_offset, record->leaf.data_len,
1436362ec2dcSMatthew Dillon 				   1);
1437e469566bSMatthew Dillon 		record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1438e469566bSMatthew Dillon 	}
1439e469566bSMatthew Dillon }
1440e469566bSMatthew Dillon 
1441e469566bSMatthew Dillon /*
144243c665aeSMatthew Dillon  * This is called to remove the second-level cached zone-2 offset from
144343c665aeSMatthew Dillon  * frontend buffer cache buffers, now stale due to a data relocation.
144443c665aeSMatthew Dillon  * These offsets are generated by cluster_read() via VOP_BMAP, or directly
144543c665aeSMatthew Dillon  * by hammer_vop_strategy_read().
144643c665aeSMatthew Dillon  *
144743c665aeSMatthew Dillon  * This is rather nasty because here we have something like the reblocker
144843c665aeSMatthew Dillon  * scanning the raw B-Tree with no held references on anything, really,
144943c665aeSMatthew Dillon  * other then a shared lock on the B-Tree node, and we have to access the
145043c665aeSMatthew Dillon  * frontend's buffer cache to check for and clean out the association.
145143c665aeSMatthew Dillon  * Specifically, if the reblocker is moving data on the disk, these cached
145243c665aeSMatthew Dillon  * offsets will become invalid.
145343c665aeSMatthew Dillon  *
145443c665aeSMatthew Dillon  * Only data record types associated with the large-data zone are subject
145543c665aeSMatthew Dillon  * to direct-io and need to be checked.
145643c665aeSMatthew Dillon  *
145743c665aeSMatthew Dillon  */
145843c665aeSMatthew Dillon void
145943c665aeSMatthew Dillon hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
146043c665aeSMatthew Dillon {
146143c665aeSMatthew Dillon 	struct hammer_inode_info iinfo;
146243c665aeSMatthew Dillon 	int zone;
146343c665aeSMatthew Dillon 
146443c665aeSMatthew Dillon 	if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
146543c665aeSMatthew Dillon 		return;
146643c665aeSMatthew Dillon 	zone = HAMMER_ZONE_DECODE(leaf->data_offset);
146743c665aeSMatthew Dillon 	if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
146843c665aeSMatthew Dillon 		return;
146943c665aeSMatthew Dillon 	iinfo.obj_id = leaf->base.obj_id;
147043c665aeSMatthew Dillon 	iinfo.obj_asof = 0;	/* unused */
147143c665aeSMatthew Dillon 	iinfo.obj_localization = leaf->base.localization &
14725a930e66SMatthew Dillon 				 HAMMER_LOCALIZE_PSEUDOFS_MASK;
147343c665aeSMatthew Dillon 	iinfo.u.leaf = leaf;
147443c665aeSMatthew Dillon 	hammer_scan_inode_snapshots(hmp, &iinfo,
147543c665aeSMatthew Dillon 				    hammer_io_direct_uncache_callback,
147643c665aeSMatthew Dillon 				    leaf);
147743c665aeSMatthew Dillon }
147843c665aeSMatthew Dillon 
147943c665aeSMatthew Dillon static int
148043c665aeSMatthew Dillon hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
148143c665aeSMatthew Dillon {
148243c665aeSMatthew Dillon 	hammer_inode_info_t iinfo = data;
148343c665aeSMatthew Dillon 	hammer_off_t data_offset;
148443c665aeSMatthew Dillon 	hammer_off_t file_offset;
148543c665aeSMatthew Dillon 	struct vnode *vp;
148643c665aeSMatthew Dillon 	struct buf *bp;
148743c665aeSMatthew Dillon 	int blksize;
148843c665aeSMatthew Dillon 
148943c665aeSMatthew Dillon 	if (ip->vp == NULL)
149043c665aeSMatthew Dillon 		return(0);
149143c665aeSMatthew Dillon 	data_offset = iinfo->u.leaf->data_offset;
149243c665aeSMatthew Dillon 	file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
149343c665aeSMatthew Dillon 	blksize = iinfo->u.leaf->data_len;
149443c665aeSMatthew Dillon 	KKASSERT((blksize & HAMMER_BUFMASK) == 0);
149543c665aeSMatthew Dillon 
149643c665aeSMatthew Dillon 	hammer_ref(&ip->lock);
149743c665aeSMatthew Dillon 	if (hammer_get_vnode(ip, &vp) == 0) {
1498b1c20cfaSMatthew Dillon 		if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
149943c665aeSMatthew Dillon 		    bp->b_bio2.bio_offset != NOOFFSET) {
150043c665aeSMatthew Dillon 			bp = getblk(ip->vp, file_offset, blksize, 0, 0);
150143c665aeSMatthew Dillon 			bp->b_bio2.bio_offset = NOOFFSET;
150243c665aeSMatthew Dillon 			brelse(bp);
150343c665aeSMatthew Dillon 		}
150443c665aeSMatthew Dillon 		vput(vp);
150543c665aeSMatthew Dillon 	}
150643c665aeSMatthew Dillon 	hammer_rel_inode(ip, 0);
150743c665aeSMatthew Dillon 	return(0);
150843c665aeSMatthew Dillon }
150947637bffSMatthew Dillon 
1510748efb59SMatthew Dillon 
1511748efb59SMatthew Dillon /*
1512748efb59SMatthew Dillon  * This function is called when writes may have occured on the volume,
1513748efb59SMatthew Dillon  * indicating that the device may be holding cached writes.
1514748efb59SMatthew Dillon  */
1515748efb59SMatthew Dillon static void
1516748efb59SMatthew Dillon hammer_io_flush_mark(hammer_volume_t volume)
1517748efb59SMatthew Dillon {
1518748efb59SMatthew Dillon 	volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1519748efb59SMatthew Dillon }
1520748efb59SMatthew Dillon 
1521748efb59SMatthew Dillon /*
1522748efb59SMatthew Dillon  * This function ensures that the device has flushed any cached writes out.
1523748efb59SMatthew Dillon  */
1524748efb59SMatthew Dillon void
1525748efb59SMatthew Dillon hammer_io_flush_sync(hammer_mount_t hmp)
1526748efb59SMatthew Dillon {
1527748efb59SMatthew Dillon 	hammer_volume_t volume;
1528748efb59SMatthew Dillon 	struct buf *bp_base = NULL;
1529748efb59SMatthew Dillon 	struct buf *bp;
1530748efb59SMatthew Dillon 
1531748efb59SMatthew Dillon 	RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1532748efb59SMatthew Dillon 		if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1533748efb59SMatthew Dillon 			volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1534748efb59SMatthew Dillon 			bp = getpbuf(NULL);
1535748efb59SMatthew Dillon 			bp->b_bio1.bio_offset = 0;
1536748efb59SMatthew Dillon 			bp->b_bufsize = 0;
1537748efb59SMatthew Dillon 			bp->b_bcount = 0;
1538748efb59SMatthew Dillon 			bp->b_cmd = BUF_CMD_FLUSH;
1539748efb59SMatthew Dillon 			bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1540ae8e83e6SMatthew Dillon 			bp->b_bio1.bio_done = biodone_sync;
1541ae8e83e6SMatthew Dillon 			bp->b_bio1.bio_flags |= BIO_SYNC;
1542748efb59SMatthew Dillon 			bp_base = bp;
1543748efb59SMatthew Dillon 			vn_strategy(volume->devvp, &bp->b_bio1);
1544748efb59SMatthew Dillon 		}
1545748efb59SMatthew Dillon 	}
1546748efb59SMatthew Dillon 	while ((bp = bp_base) != NULL) {
1547748efb59SMatthew Dillon 		bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1548ae8e83e6SMatthew Dillon 		biowait(&bp->b_bio1, "hmrFLS");
1549748efb59SMatthew Dillon 		relpbuf(bp, NULL);
1550748efb59SMatthew Dillon 	}
1551748efb59SMatthew Dillon }
1552