xref: /dflybsd-src/sys/vfs/hammer/hammer_io.c (revision e1545c477fc52b4da47e1a41fed485a2c74a68cd)
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  */
3466325755SMatthew Dillon /*
3566325755SMatthew Dillon  * IO Primitives and buffer cache management
3666325755SMatthew Dillon  *
3766325755SMatthew Dillon  * All major data-tracking structures in HAMMER contain a struct hammer_io
3866325755SMatthew Dillon  * which is used to manage their backing store.  We use filesystem buffers
3966325755SMatthew Dillon  * for backing store and we leave them passively associated with their
4066325755SMatthew Dillon  * HAMMER structures.
4166325755SMatthew Dillon  *
429f5097dcSMatthew Dillon  * If the kernel tries to destroy a passively associated buf which we cannot
4366325755SMatthew Dillon  * yet let go we set B_LOCKED in the buffer and then actively released it
4466325755SMatthew Dillon  * later when we can.
4577912481SMatthew Dillon  *
4677912481SMatthew Dillon  * The io_token is required for anything which might race bioops and bio_done
4777912481SMatthew Dillon  * callbacks, with one exception: A successful hammer_try_interlock_norefs().
4877912481SMatthew Dillon  * the fs_token will be held in all other cases.
4966325755SMatthew Dillon  */
5066325755SMatthew Dillon 
5197fb61c0STomohiro Kusumi #include <sys/buf2.h>
5254341a3bSMatthew Dillon 
53b45803e3STomohiro Kusumi #include "hammer.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);
579a98f3ccSMatthew Dillon static void hammer_indirect_callback(struct bio *bio);
581b0ab2c3SMatthew Dillon static void hammer_io_direct_write_complete(struct bio *nbio);
5943c665aeSMatthew Dillon static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
60cdb6e4e6SMatthew Dillon static void hammer_io_set_modlist(struct hammer_io *io);
619e6939a5STomohiro Kusumi static __inline void hammer_io_flush_mark(hammer_volume_t volume);
62e397030bSTomohiro Kusumi static struct bio_ops hammer_bioops;
63748efb59SMatthew Dillon 
641afb73cfSMatthew Dillon static int
651afb73cfSMatthew Dillon hammer_mod_rb_compare(hammer_io_t io1, hammer_io_t io2)
661afb73cfSMatthew Dillon {
671afb73cfSMatthew Dillon 	hammer_off_t io1_offset;
681afb73cfSMatthew Dillon 	hammer_off_t io2_offset;
691afb73cfSMatthew Dillon 
70b49481fbSTomohiro Kusumi 	/*
71b49481fbSTomohiro Kusumi 	 * Encoded offsets are neither valid block device offsets
72b49481fbSTomohiro Kusumi 	 * nor valid zone-X offsets.
73b49481fbSTomohiro Kusumi 	 */
74b49481fbSTomohiro Kusumi 	io1_offset = HAMMER_ENCODE(0, io1->volume->vol_no, io1->offset);
75b49481fbSTomohiro Kusumi 	io2_offset = HAMMER_ENCODE(0, io2->volume->vol_no, io2->offset);
761afb73cfSMatthew Dillon 
771afb73cfSMatthew Dillon 	if (io1_offset < io2_offset)
781afb73cfSMatthew Dillon 		return(-1);
791afb73cfSMatthew Dillon 	if (io1_offset > io2_offset)
801afb73cfSMatthew Dillon 		return(1);
811afb73cfSMatthew Dillon 	return(0);
821afb73cfSMatthew Dillon }
831afb73cfSMatthew Dillon 
841afb73cfSMatthew Dillon RB_GENERATE(hammer_mod_rb_tree, hammer_io, rb_node, hammer_mod_rb_compare);
851afb73cfSMatthew Dillon 
86055f5ff8SMatthew Dillon /*
8710a5d1baSMatthew Dillon  * Initialize a new, already-zero'd hammer_io structure, or reinitialize
8810a5d1baSMatthew Dillon  * an existing hammer_io structure which may have switched to another type.
89055f5ff8SMatthew Dillon  */
90055f5ff8SMatthew Dillon void
91748efb59SMatthew Dillon hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
92055f5ff8SMatthew Dillon {
93748efb59SMatthew Dillon 	io->volume = volume;
94748efb59SMatthew Dillon 	io->hmp = volume->io.hmp;
95055f5ff8SMatthew Dillon 	io->type = type;
96055f5ff8SMatthew Dillon }
97055f5ff8SMatthew Dillon 
9866325755SMatthew Dillon /*
99fbc6e32aSMatthew Dillon  * Helper routine to disassociate a buffer cache buffer from an I/O
10077912481SMatthew Dillon  * structure.  The io must be interlocked and marked appropriately for
101b0aab9b9SMatthew Dillon  * reclamation.
102055f5ff8SMatthew Dillon  *
103b0aab9b9SMatthew Dillon  * The io must be in a released state with the io->bp owned and
104b0aab9b9SMatthew Dillon  * locked by the caller of this function.  When not called from an
105b0aab9b9SMatthew Dillon  * io_deallocate() this cannot race an io_deallocate() since the
106b0aab9b9SMatthew Dillon  * kernel would be unable to get the buffer lock in that case.
10777912481SMatthew Dillon  * (The released state in this case means we own the bp, not the
10877912481SMatthew Dillon  * hammer_io structure).
10977912481SMatthew Dillon  *
11077912481SMatthew Dillon  * The io may have 0 or 1 references depending on who called us.  The
11177912481SMatthew Dillon  * caller is responsible for dealing with the refs.
112b0aab9b9SMatthew Dillon  *
113055f5ff8SMatthew Dillon  * This call can only be made when no action is required on the buffer.
114ecca949aSMatthew Dillon  *
11577912481SMatthew Dillon  * This function is guaranteed not to race against anything because we
11677912481SMatthew Dillon  * own both the io lock and the bp lock and are interlocked with no
11777912481SMatthew Dillon  * references.
11866325755SMatthew Dillon  */
11966325755SMatthew Dillon static void
120ff66f880STomohiro Kusumi hammer_io_disassociate(hammer_io_t io)
12166325755SMatthew Dillon {
122ff66f880STomohiro Kusumi 	struct buf *bp = io->bp;
12366325755SMatthew Dillon 
124ff66f880STomohiro Kusumi 	KKASSERT(io->released);
125ff66f880STomohiro Kusumi 	KKASSERT(io->modified == 0);
1260ae73f43STomohiro Kusumi 	KKASSERT(hammer_buf_peek_io(bp) == io);
1274d75d829SMatthew Dillon 	buf_dep_init(bp);
128ff66f880STomohiro Kusumi 	io->bp = NULL;
1299f5097dcSMatthew Dillon 
1309f5097dcSMatthew Dillon 	/*
1319f5097dcSMatthew Dillon 	 * If the buffer was locked someone wanted to get rid of it.
1329f5097dcSMatthew Dillon 	 */
133a99b9ea2SMatthew Dillon 	if (bp->b_flags & B_LOCKED) {
134b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, -1);
135d8971d2bSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
136a99b9ea2SMatthew Dillon 	}
137ff66f880STomohiro Kusumi 	if (io->reclaim) {
138cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
139ff66f880STomohiro Kusumi 		io->reclaim = 0;
140ecca949aSMatthew Dillon 	}
14166325755SMatthew Dillon 
142ff66f880STomohiro Kusumi 	switch(io->type) {
14366325755SMatthew Dillon 	case HAMMER_STRUCTURE_VOLUME:
144ff66f880STomohiro Kusumi 		HAMMER_ITOV(io)->ondisk = NULL;
14566325755SMatthew Dillon 		break;
14610a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_DATA_BUFFER:
14710a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_META_BUFFER:
14810a5d1baSMatthew Dillon 	case HAMMER_STRUCTURE_UNDO_BUFFER:
149ff66f880STomohiro Kusumi 		HAMMER_ITOB(io)->ondisk = NULL;
15066325755SMatthew Dillon 		break;
151eddadaeeSMatthew Dillon 	case HAMMER_STRUCTURE_DUMMY:
152903fdd05STomohiro Kusumi 		hpanic("bad io type");
153eddadaeeSMatthew Dillon 		break;
15466325755SMatthew Dillon 	}
15566325755SMatthew Dillon }
156fbc6e32aSMatthew Dillon 
157fbc6e32aSMatthew Dillon /*
158055f5ff8SMatthew Dillon  * Wait for any physical IO to complete
159ae8e83e6SMatthew Dillon  *
160ae8e83e6SMatthew Dillon  * XXX we aren't interlocked against a spinlock or anything so there
161ae8e83e6SMatthew Dillon  *     is a small window in the interlock / io->running == 0 test.
162fbc6e32aSMatthew Dillon  */
1631b0ab2c3SMatthew Dillon void
164055f5ff8SMatthew Dillon hammer_io_wait(hammer_io_t io)
165fbc6e32aSMatthew Dillon {
166055f5ff8SMatthew Dillon 	if (io->running) {
167b0aab9b9SMatthew Dillon 		hammer_mount_t hmp = io->hmp;
168b0aab9b9SMatthew Dillon 
169b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
170b0aab9b9SMatthew Dillon 		while (io->running) {
171ae8e83e6SMatthew Dillon 			io->waiting = 1;
172ae8e83e6SMatthew Dillon 			tsleep_interlock(io, 0);
173b0aab9b9SMatthew Dillon 			if (io->running)
174ae8e83e6SMatthew Dillon 				tsleep(io, PINTERLOCKED, "hmrflw", hz);
175055f5ff8SMatthew Dillon 		}
176b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
177055f5ff8SMatthew Dillon 	}
178055f5ff8SMatthew Dillon }
179055f5ff8SMatthew Dillon 
180af209b0fSMatthew Dillon /*
181eddadaeeSMatthew Dillon  * Wait for all currently queued HAMMER-initiated I/Os to complete.
182eddadaeeSMatthew Dillon  *
183eddadaeeSMatthew Dillon  * This is not supposed to count direct I/O's but some can leak
184eddadaeeSMatthew Dillon  * through (for non-full-sized direct I/Os).
185af209b0fSMatthew Dillon  */
186af209b0fSMatthew Dillon void
187eddadaeeSMatthew Dillon hammer_io_wait_all(hammer_mount_t hmp, const char *ident, int doflush)
188af209b0fSMatthew Dillon {
189eddadaeeSMatthew Dillon 	struct hammer_io iodummy;
190eddadaeeSMatthew Dillon 	hammer_io_t io;
191eddadaeeSMatthew Dillon 
192eddadaeeSMatthew Dillon 	/*
193eddadaeeSMatthew Dillon 	 * Degenerate case, no I/O is running
194eddadaeeSMatthew Dillon 	 */
195b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
196eddadaeeSMatthew Dillon 	if (TAILQ_EMPTY(&hmp->iorun_list)) {
197b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
198eddadaeeSMatthew Dillon 		if (doflush)
199eddadaeeSMatthew Dillon 			hammer_io_flush_sync(hmp);
200eddadaeeSMatthew Dillon 		return;
201eddadaeeSMatthew Dillon 	}
202eddadaeeSMatthew Dillon 	bzero(&iodummy, sizeof(iodummy));
203eddadaeeSMatthew Dillon 	iodummy.type = HAMMER_STRUCTURE_DUMMY;
204eddadaeeSMatthew Dillon 
205eddadaeeSMatthew Dillon 	/*
206eddadaeeSMatthew Dillon 	 * Add placemarker and then wait until it becomes the head of
207eddadaeeSMatthew Dillon 	 * the list.
208eddadaeeSMatthew Dillon 	 */
209eddadaeeSMatthew Dillon 	TAILQ_INSERT_TAIL(&hmp->iorun_list, &iodummy, iorun_entry);
210eddadaeeSMatthew Dillon 	while (TAILQ_FIRST(&hmp->iorun_list) != &iodummy) {
211eddadaeeSMatthew Dillon 		tsleep(&iodummy, 0, ident, 0);
212eddadaeeSMatthew Dillon 	}
213eddadaeeSMatthew Dillon 
214eddadaeeSMatthew Dillon 	/*
215eddadaeeSMatthew Dillon 	 * Chain in case several placemarkers are present.
216eddadaeeSMatthew Dillon 	 */
217eddadaeeSMatthew Dillon 	TAILQ_REMOVE(&hmp->iorun_list, &iodummy, iorun_entry);
218eddadaeeSMatthew Dillon 	io = TAILQ_FIRST(&hmp->iorun_list);
219eddadaeeSMatthew Dillon 	if (io && io->type == HAMMER_STRUCTURE_DUMMY)
220eddadaeeSMatthew Dillon 		wakeup(io);
221b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
222eddadaeeSMatthew Dillon 
223eddadaeeSMatthew Dillon 	if (doflush)
224eddadaeeSMatthew Dillon 		hammer_io_flush_sync(hmp);
225af209b0fSMatthew Dillon }
226af209b0fSMatthew Dillon 
2272faf0737SMatthew Dillon /*
2282faf0737SMatthew Dillon  * Clear a flagged error condition on a I/O buffer.  The caller must hold
2292faf0737SMatthew Dillon  * its own ref on the buffer.
2302faf0737SMatthew Dillon  */
2312faf0737SMatthew Dillon void
2322faf0737SMatthew Dillon hammer_io_clear_error(struct hammer_io *io)
2332faf0737SMatthew Dillon {
23477912481SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
23577912481SMatthew Dillon 
23677912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
2372faf0737SMatthew Dillon 	if (io->ioerror) {
2382faf0737SMatthew Dillon 		io->ioerror = 0;
239250aec18SMatthew Dillon 		hammer_rel(&io->lock);
240250aec18SMatthew Dillon 		KKASSERT(hammer_isactive(&io->lock));
2412faf0737SMatthew Dillon 	}
24277912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
24377912481SMatthew Dillon }
24477912481SMatthew Dillon 
24577912481SMatthew Dillon void
24677912481SMatthew Dillon hammer_io_clear_error_noassert(struct hammer_io *io)
24777912481SMatthew Dillon {
24877912481SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
24977912481SMatthew Dillon 
25077912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
25177912481SMatthew Dillon 	if (io->ioerror) {
25277912481SMatthew Dillon 		io->ioerror = 0;
25377912481SMatthew Dillon 		hammer_rel(&io->lock);
25477912481SMatthew Dillon 	}
25577912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
2562faf0737SMatthew Dillon }
2572faf0737SMatthew Dillon 
258b8a41159SMatthew Dillon /*
259b8a41159SMatthew Dillon  * This is an advisory function only which tells the buffer cache
260b8a41159SMatthew Dillon  * the bp is not a meta-data buffer, even though it is backed by
261b8a41159SMatthew Dillon  * a block device.
262b8a41159SMatthew Dillon  *
263b8a41159SMatthew Dillon  * This is used by HAMMER's reblocking code to avoid trying to
264b8a41159SMatthew Dillon  * swapcache the filesystem's data when it is read or written
265b8a41159SMatthew Dillon  * by the reblocking code.
266b0aab9b9SMatthew Dillon  *
267b0aab9b9SMatthew Dillon  * The caller has a ref on the buffer preventing the bp from
268b0aab9b9SMatthew Dillon  * being disassociated from it.
269b8a41159SMatthew Dillon  */
270b8a41159SMatthew Dillon void
271b8a41159SMatthew Dillon hammer_io_notmeta(hammer_buffer_t buffer)
272b8a41159SMatthew Dillon {
273b0aab9b9SMatthew Dillon 	if ((buffer->io.bp->b_flags & B_NOTMETA) == 0) {
274b0aab9b9SMatthew Dillon 		hammer_mount_t hmp = buffer->io.hmp;
275b0aab9b9SMatthew Dillon 
276b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
277b8a41159SMatthew Dillon 		buffer->io.bp->b_flags |= B_NOTMETA;
278b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
279b0aab9b9SMatthew Dillon 	}
280b8a41159SMatthew Dillon }
281b8a41159SMatthew Dillon 
28261aeeb33SMatthew Dillon /*
28310a5d1baSMatthew Dillon  * Load bp for a HAMMER structure.  The io must be exclusively locked by
28410a5d1baSMatthew Dillon  * the caller.
2852f85fa4dSMatthew Dillon  *
286a99b9ea2SMatthew Dillon  * This routine is mostly used on meta-data and small-data blocks.  Generally
287b7de8aa5SMatthew Dillon  * speaking HAMMER assumes some locality of reference and will cluster.
288af209b0fSMatthew Dillon  *
289b7de8aa5SMatthew Dillon  * Note that the caller (hammer_ondisk.c) may place further restrictions
290b7de8aa5SMatthew Dillon  * on clusterability via the limit (in bytes).  Typically large-data
291b7de8aa5SMatthew Dillon  * zones cannot be clustered due to their mixed buffer sizes.  This is
292b7de8aa5SMatthew Dillon  * not an issue since such clustering occurs in hammer_vnops at the
293b7de8aa5SMatthew Dillon  * regular file layer, whereas this is the buffered block device layer.
294b0aab9b9SMatthew Dillon  *
295b0aab9b9SMatthew Dillon  * No I/O callbacks can occur while we hold the buffer locked.
29666325755SMatthew Dillon  */
29766325755SMatthew Dillon int
298b7de8aa5SMatthew Dillon hammer_io_read(struct vnode *devvp, struct hammer_io *io, int limit)
29966325755SMatthew Dillon {
30066325755SMatthew Dillon 	struct buf *bp;
30166325755SMatthew Dillon 	int   error;
30266325755SMatthew Dillon 
30366325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
3043583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_io_running_read, io->bytes);
305b7de8aa5SMatthew Dillon 		if (hammer_cluster_enable && limit > io->bytes) {
306b7de8aa5SMatthew Dillon 			error = cluster_read(devvp, io->offset + limit,
307ce0138a6SMatthew Dillon 					     io->offset, io->bytes,
308af209b0fSMatthew Dillon 					     HAMMER_CLUSTER_SIZE,
309364c022cSMatthew Dillon 					     HAMMER_CLUSTER_SIZE,
310364c022cSMatthew Dillon 					     &io->bp);
311ce0138a6SMatthew Dillon 		} else {
3124a2796f3SMatthew Dillon 			error = bread(devvp, io->offset, io->bytes, &io->bp);
313ce0138a6SMatthew Dillon 		}
314ce0138a6SMatthew Dillon 		hammer_stats_disk_read += io->bytes;
3153583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_io_running_read, -io->bytes);
316cdb6e4e6SMatthew Dillon 
317cdb6e4e6SMatthew Dillon 		/*
318cdb6e4e6SMatthew Dillon 		 * The code generally assumes b_ops/b_dep has been set-up,
319cdb6e4e6SMatthew Dillon 		 * even if we error out here.
320cdb6e4e6SMatthew Dillon 		 */
32166325755SMatthew Dillon 		bp = io->bp;
32224c8374aSMatthew Dillon 		if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
32324c8374aSMatthew Dillon 			const char *metatype;
32424c8374aSMatthew Dillon 
32524c8374aSMatthew Dillon 			switch(io->type) {
32624c8374aSMatthew Dillon 			case HAMMER_STRUCTURE_VOLUME:
32724c8374aSMatthew Dillon 				metatype = "volume";
32824c8374aSMatthew Dillon 				break;
32924c8374aSMatthew Dillon 			case HAMMER_STRUCTURE_META_BUFFER:
3305bce7157STomohiro Kusumi 				switch(HAMMER_ZONE(HAMMER_ITOB(io)->zoneX_offset)) {
33124c8374aSMatthew Dillon 				case HAMMER_ZONE_BTREE:
33224c8374aSMatthew Dillon 					metatype = "btree";
33324c8374aSMatthew Dillon 					break;
33424c8374aSMatthew Dillon 				case HAMMER_ZONE_META:
33524c8374aSMatthew Dillon 					metatype = "meta";
33624c8374aSMatthew Dillon 					break;
33724c8374aSMatthew Dillon 				case HAMMER_ZONE_FREEMAP:
33824c8374aSMatthew Dillon 					metatype = "freemap";
33924c8374aSMatthew Dillon 					break;
34024c8374aSMatthew Dillon 				default:
34124c8374aSMatthew Dillon 					metatype = "meta?";
34224c8374aSMatthew Dillon 					break;
34324c8374aSMatthew Dillon 				}
34424c8374aSMatthew Dillon 				break;
34524c8374aSMatthew Dillon 			case HAMMER_STRUCTURE_DATA_BUFFER:
34624c8374aSMatthew Dillon 				metatype = "data";
34724c8374aSMatthew Dillon 				break;
34824c8374aSMatthew Dillon 			case HAMMER_STRUCTURE_UNDO_BUFFER:
34924c8374aSMatthew Dillon 				metatype = "undo";
35024c8374aSMatthew Dillon 				break;
35124c8374aSMatthew Dillon 			default:
35224c8374aSMatthew Dillon 				metatype = "unknown";
35324c8374aSMatthew Dillon 				break;
35424c8374aSMatthew Dillon 			}
35565d9d14fSTomohiro Kusumi 			hdkprintf("zone2_offset %016jx %s\n",
35624c8374aSMatthew Dillon 				(intmax_t)bp->b_bio2.bio_offset,
35724c8374aSMatthew Dillon 				metatype);
35824c8374aSMatthew Dillon 		}
35924c8374aSMatthew Dillon 		bp->b_flags &= ~B_IODEBUG;
36066325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
361b0aab9b9SMatthew Dillon 
3620ae73f43STomohiro Kusumi 		hammer_buf_attach_io(bp, io); /* locked by the io lock */
36366325755SMatthew Dillon 		BUF_KERNPROC(bp);
36410a5d1baSMatthew Dillon 		KKASSERT(io->modified == 0);
36510a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
36610a5d1baSMatthew Dillon 		KKASSERT(io->waiting == 0);
36766325755SMatthew Dillon 		io->released = 0;	/* we hold an active lock on bp */
36866325755SMatthew Dillon 	} else {
36966325755SMatthew Dillon 		error = 0;
37066325755SMatthew Dillon 	}
37166325755SMatthew Dillon 	return(error);
37266325755SMatthew Dillon }
37366325755SMatthew Dillon 
37466325755SMatthew Dillon /*
37566325755SMatthew Dillon  * Similar to hammer_io_read() but returns a zero'd out buffer instead.
37610a5d1baSMatthew Dillon  * Must be called with the IO exclusively locked.
377055f5ff8SMatthew Dillon  *
37810a5d1baSMatthew Dillon  * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
37910a5d1baSMatthew Dillon  * I/O by forcing the buffer to not be in a released state before calling
38010a5d1baSMatthew Dillon  * it.
38110a5d1baSMatthew Dillon  *
38210a5d1baSMatthew Dillon  * This function will also mark the IO as modified but it will not
38310a5d1baSMatthew Dillon  * increment the modify_refs count.
384b0aab9b9SMatthew Dillon  *
385b0aab9b9SMatthew Dillon  * No I/O callbacks can occur while we hold the buffer locked.
38666325755SMatthew Dillon  */
38766325755SMatthew Dillon int
38866325755SMatthew Dillon hammer_io_new(struct vnode *devvp, struct hammer_io *io)
38966325755SMatthew Dillon {
39066325755SMatthew Dillon 	struct buf *bp;
39166325755SMatthew Dillon 
39266325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
3934a2796f3SMatthew Dillon 		io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
39466325755SMatthew Dillon 		bp = io->bp;
39566325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
396b0aab9b9SMatthew Dillon 
3970ae73f43STomohiro Kusumi 		hammer_buf_attach_io(bp, io); /* locked by the io lock */
398055f5ff8SMatthew Dillon 		io->released = 0;
39910a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
400055f5ff8SMatthew Dillon 		io->waiting = 0;
40166325755SMatthew Dillon 		BUF_KERNPROC(bp);
40266325755SMatthew Dillon 	} else {
40366325755SMatthew Dillon 		if (io->released) {
40466325755SMatthew Dillon 			regetblk(bp);
40566325755SMatthew Dillon 			BUF_KERNPROC(bp);
406d113fda1SMatthew Dillon 			io->released = 0;
40766325755SMatthew Dillon 		}
40866325755SMatthew Dillon 	}
40910a5d1baSMatthew Dillon 	hammer_io_modify(io, 0);
41066325755SMatthew Dillon 	vfs_bio_clrbuf(bp);
41166325755SMatthew Dillon 	return(0);
41266325755SMatthew Dillon }
41366325755SMatthew Dillon 
41466325755SMatthew Dillon /*
4150e8bd897SMatthew Dillon  * Advance the activity count on the underlying buffer because
4160e8bd897SMatthew Dillon  * HAMMER does not getblk/brelse on every access.
417b0aab9b9SMatthew Dillon  *
418b0aab9b9SMatthew Dillon  * The io->bp cannot go away while the buffer is referenced.
4190e8bd897SMatthew Dillon  */
4200e8bd897SMatthew Dillon void
4210e8bd897SMatthew Dillon hammer_io_advance(struct hammer_io *io)
4220e8bd897SMatthew Dillon {
4230e8bd897SMatthew Dillon 	if (io->bp)
4240e8bd897SMatthew Dillon 		buf_act_advance(io->bp);
4250e8bd897SMatthew Dillon }
4260e8bd897SMatthew Dillon 
4270e8bd897SMatthew Dillon /*
42847637bffSMatthew Dillon  * Remove potential device level aliases against buffers managed by high level
429362ec2dcSMatthew Dillon  * vnodes.  Aliases can also be created due to mixed buffer sizes or via
430362ec2dcSMatthew Dillon  * direct access to the backing store device.
431e469566bSMatthew Dillon  *
432e469566bSMatthew Dillon  * This is nasty because the buffers are also VMIO-backed.  Even if a buffer
433e469566bSMatthew Dillon  * does not exist its backing VM pages might, and we have to invalidate
434e469566bSMatthew Dillon  * those as well or a getblk() will reinstate them.
435362ec2dcSMatthew Dillon  *
436362ec2dcSMatthew Dillon  * Buffer cache buffers associated with hammer_buffers cannot be
437362ec2dcSMatthew Dillon  * invalidated.
43847637bffSMatthew Dillon  */
439362ec2dcSMatthew Dillon int
44047637bffSMatthew Dillon hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
44147637bffSMatthew Dillon {
442ff66f880STomohiro Kusumi 	hammer_io_t io;
443b0aab9b9SMatthew Dillon 	hammer_mount_t hmp;
44447637bffSMatthew Dillon 	hammer_off_t phys_offset;
44547637bffSMatthew Dillon 	struct buf *bp;
446362ec2dcSMatthew Dillon 	int error;
44747637bffSMatthew Dillon 
448b0aab9b9SMatthew Dillon 	hmp = volume->io.hmp;
449b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
450b0aab9b9SMatthew Dillon 
4519c90dba2SMatthew Dillon 	/*
4523b98d912SMatthew Dillon 	 * If a device buffer already exists for the specified physical
4533b98d912SMatthew Dillon 	 * offset use that, otherwise instantiate a buffer to cover any
4543b98d912SMatthew Dillon 	 * related VM pages, set BNOCACHE, and brelse().
4559c90dba2SMatthew Dillon 	 */
456516655e8STomohiro Kusumi 	phys_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset);
4573b98d912SMatthew Dillon 	if ((bp = findblk(volume->devvp, phys_offset, 0)) != NULL)
4583b98d912SMatthew Dillon 		bremfree(bp);
459e469566bSMatthew Dillon 	else
460e469566bSMatthew Dillon 		bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
461b0aab9b9SMatthew Dillon 
4620ae73f43STomohiro Kusumi 	if ((io = hammer_buf_peek_io(bp)) != NULL) {
463362ec2dcSMatthew Dillon #if 0
464ff66f880STomohiro Kusumi 		hammer_ref(&io->lock);
465ff66f880STomohiro Kusumi 		hammer_io_clear_modify(io, 1);
466cebe9493SMatthew Dillon 		bundirty(bp);
467ff66f880STomohiro Kusumi 		io->released = 0;
468e83ca595SMatthew Dillon 		BUF_KERNPROC(bp);
469ff66f880STomohiro Kusumi 		io->reclaim = 1;
470ff66f880STomohiro Kusumi 		io->waitdep = 1;	/* XXX this is a fs_token field */
471ff66f880STomohiro Kusumi 		KKASSERT(hammer_isactive(&io->lock) == 1);
472ff66f880STomohiro Kusumi 		hammer_rel_buffer(HAMMER_ITOB(io), 0);
4735c8d05e2SMatthew Dillon 		/*hammer_io_deallocate(bp);*/
474362ec2dcSMatthew Dillon #endif
47504b04ca6SMatthew Dillon 		bqrelse(bp);
476362ec2dcSMatthew Dillon 		error = EAGAIN;
4770832c9bbSMatthew Dillon 	} else {
478cebe9493SMatthew Dillon 		KKASSERT((bp->b_flags & B_LOCKED) == 0);
479cebe9493SMatthew Dillon 		bundirty(bp);
480cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
481ecca949aSMatthew Dillon 		brelse(bp);
482362ec2dcSMatthew Dillon 		error = 0;
483e83ca595SMatthew Dillon 	}
484b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
485362ec2dcSMatthew Dillon 	return(error);
4860832c9bbSMatthew Dillon }
48747637bffSMatthew Dillon 
48847637bffSMatthew Dillon /*
489b3deaf57SMatthew Dillon  * This routine is called on the last reference to a hammer structure.
490250aec18SMatthew Dillon  * The io must be interlocked with a refcount of zero.  The hammer structure
491250aec18SMatthew Dillon  * will remain interlocked on return.
492b3deaf57SMatthew Dillon  *
493250aec18SMatthew Dillon  * This routine may return a non-NULL bp to the caller for dispoal.
494250aec18SMatthew Dillon  * The caller typically brelse()'s the bp.
495250aec18SMatthew Dillon  *
496250aec18SMatthew Dillon  * The bp may or may not still be passively associated with the IO.  It
497250aec18SMatthew Dillon  * will remain passively associated if it is unreleasable (e.g. a modified
498250aec18SMatthew Dillon  * meta-data buffer).
499ecca949aSMatthew Dillon  *
500ecca949aSMatthew Dillon  * The only requirement here is that modified meta-data and volume-header
501ecca949aSMatthew Dillon  * buffer may NOT be disassociated from the IO structure, and consequently
502ecca949aSMatthew Dillon  * we also leave such buffers actively associated with the IO if they already
503ecca949aSMatthew Dillon  * are (since the kernel can't do anything with them anyway).  Only the
504ecca949aSMatthew Dillon  * flusher is allowed to write such buffers out.  Modified pure-data and
505ecca949aSMatthew Dillon  * undo buffers are returned to the kernel but left passively associated
506ecca949aSMatthew Dillon  * so we can track when the kernel writes the bp out.
50766325755SMatthew Dillon  */
508ecca949aSMatthew Dillon struct buf *
50909ac686bSMatthew Dillon hammer_io_release(struct hammer_io *io, int flush)
51066325755SMatthew Dillon {
51166325755SMatthew Dillon 	struct buf *bp;
51266325755SMatthew Dillon 
513fbc6e32aSMatthew Dillon 	if ((bp = io->bp) == NULL)
514ecca949aSMatthew Dillon 		return(NULL);
515fbc6e32aSMatthew Dillon 
5160b075555SMatthew Dillon 	/*
51710a5d1baSMatthew Dillon 	 * Try to flush a dirty IO to disk if asked to by the
51810a5d1baSMatthew Dillon 	 * caller or if the kernel tried to flush the buffer in the past.
5190b075555SMatthew Dillon 	 *
52010a5d1baSMatthew Dillon 	 * Kernel-initiated flushes are only allowed for pure-data buffers.
52110a5d1baSMatthew Dillon 	 * meta-data and volume buffers can only be flushed explicitly
52210a5d1baSMatthew Dillon 	 * by HAMMER.
523055f5ff8SMatthew Dillon 	 */
52410a5d1baSMatthew Dillon 	if (io->modified) {
52509ac686bSMatthew Dillon 		if (flush) {
526710733a6SMatthew Dillon 			hammer_io_flush(io, 0);
52710a5d1baSMatthew Dillon 		} else if (bp->b_flags & B_LOCKED) {
52810a5d1baSMatthew Dillon 			switch(io->type) {
52910a5d1baSMatthew Dillon 			case HAMMER_STRUCTURE_DATA_BUFFER:
530710733a6SMatthew Dillon 				hammer_io_flush(io, 0);
531710733a6SMatthew Dillon 				break;
53210a5d1baSMatthew Dillon 			case HAMMER_STRUCTURE_UNDO_BUFFER:
533710733a6SMatthew Dillon 				hammer_io_flush(io, hammer_undo_reclaim(io));
53410a5d1baSMatthew Dillon 				break;
53510a5d1baSMatthew Dillon 			default:
53610a5d1baSMatthew Dillon 				break;
53710a5d1baSMatthew Dillon 			}
53810a5d1baSMatthew Dillon 		} /* else no explicit request to flush the buffer */
53910a5d1baSMatthew Dillon 	}
540055f5ff8SMatthew Dillon 
541055f5ff8SMatthew Dillon 	/*
5425c8d05e2SMatthew Dillon 	 * Wait for the IO to complete if asked to.  This occurs when
5435c8d05e2SMatthew Dillon 	 * the buffer must be disposed of definitively during an umount
5445c8d05e2SMatthew Dillon 	 * or buffer invalidation.
545055f5ff8SMatthew Dillon 	 */
546b58c6388SMatthew Dillon 	if (io->waitdep && io->running) {
547055f5ff8SMatthew Dillon 		hammer_io_wait(io);
548055f5ff8SMatthew Dillon 	}
549055f5ff8SMatthew Dillon 
550055f5ff8SMatthew Dillon 	/*
55110a5d1baSMatthew Dillon 	 * Return control of the buffer to the kernel (with the provisio
55210a5d1baSMatthew Dillon 	 * that our bioops can override kernel decisions with regards to
55310a5d1baSMatthew Dillon 	 * the buffer).
554055f5ff8SMatthew Dillon 	 */
555cebe9493SMatthew Dillon 	if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
55610a5d1baSMatthew Dillon 		/*
55710a5d1baSMatthew Dillon 		 * Always disassociate the bp if an explicit flush
55810a5d1baSMatthew Dillon 		 * was requested and the IO completed with no error
55910a5d1baSMatthew Dillon 		 * (so unmount can really clean up the structure).
56010a5d1baSMatthew Dillon 		 */
561055f5ff8SMatthew Dillon 		if (io->released) {
562055f5ff8SMatthew Dillon 			regetblk(bp);
56346fe7ae1SMatthew Dillon 			BUF_KERNPROC(bp);
564ecca949aSMatthew Dillon 		} else {
565ecca949aSMatthew Dillon 			io->released = 1;
566055f5ff8SMatthew Dillon 		}
567ff66f880STomohiro Kusumi 		hammer_io_disassociate(io);
568ecca949aSMatthew Dillon 		/* return the bp */
569055f5ff8SMatthew Dillon 	} else if (io->modified) {
57010a5d1baSMatthew Dillon 		/*
571ecca949aSMatthew Dillon 		 * Only certain IO types can be released to the kernel if
572ecca949aSMatthew Dillon 		 * the buffer has been modified.
573ecca949aSMatthew Dillon 		 *
574ecca949aSMatthew Dillon 		 * volume and meta-data IO types may only be explicitly
575ecca949aSMatthew Dillon 		 * flushed by HAMMER.
57610a5d1baSMatthew Dillon 		 */
57710a5d1baSMatthew Dillon 		switch(io->type) {
57810a5d1baSMatthew Dillon 		case HAMMER_STRUCTURE_DATA_BUFFER:
57910a5d1baSMatthew Dillon 		case HAMMER_STRUCTURE_UNDO_BUFFER:
580b58c6388SMatthew Dillon 			if (io->released == 0) {
581055f5ff8SMatthew Dillon 				io->released = 1;
5829de13b88SMatthew Dillon 				bp->b_flags |= B_CLUSTEROK;
583055f5ff8SMatthew Dillon 				bdwrite(bp);
584055f5ff8SMatthew Dillon 			}
58510a5d1baSMatthew Dillon 			break;
58610a5d1baSMatthew Dillon 		default:
58710a5d1baSMatthew Dillon 			break;
58810a5d1baSMatthew Dillon 		}
589ecca949aSMatthew Dillon 		bp = NULL;	/* bp left associated */
590055f5ff8SMatthew Dillon 	} else if (io->released == 0) {
59110a5d1baSMatthew Dillon 		/*
59210a5d1baSMatthew Dillon 		 * Clean buffers can be generally released to the kernel.
59310a5d1baSMatthew Dillon 		 * We leave the bp passively associated with the HAMMER
59410a5d1baSMatthew Dillon 		 * structure and use bioops to disconnect it later on
59510a5d1baSMatthew Dillon 		 * if the kernel wants to discard the buffer.
596ecca949aSMatthew Dillon 		 *
597ecca949aSMatthew Dillon 		 * We can steal the structure's ownership of the bp.
59810a5d1baSMatthew Dillon 		 */
599ecca949aSMatthew Dillon 		io->released = 1;
6009f5097dcSMatthew Dillon 		if (bp->b_flags & B_LOCKED) {
601ff66f880STomohiro Kusumi 			hammer_io_disassociate(io);
602ecca949aSMatthew Dillon 			/* return the bp */
6039f5097dcSMatthew Dillon 		} else {
604cebe9493SMatthew Dillon 			if (io->reclaim) {
605ff66f880STomohiro Kusumi 				hammer_io_disassociate(io);
606ecca949aSMatthew Dillon 				/* return the bp */
607cebe9493SMatthew Dillon 			} else {
608ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
6099f5097dcSMatthew Dillon 			}
610cebe9493SMatthew Dillon 		}
61119b97e01SMatthew Dillon 	} else {
61219b97e01SMatthew Dillon 		/*
613af209b0fSMatthew Dillon 		 * A released buffer is passively associate with our
614af209b0fSMatthew Dillon 		 * hammer_io structure.  The kernel cannot destroy it
615af209b0fSMatthew Dillon 		 * without making a bioops call.  If the kernel (B_LOCKED)
616af209b0fSMatthew Dillon 		 * or we (reclaim) requested that the buffer be destroyed
617af209b0fSMatthew Dillon 		 * we destroy it, otherwise we do a quick get/release to
618af209b0fSMatthew Dillon 		 * reset its position in the kernel's LRU list.
619af209b0fSMatthew Dillon 		 *
620af209b0fSMatthew Dillon 		 * Leaving the buffer passively associated allows us to
621af209b0fSMatthew Dillon 		 * use the kernel's LRU buffer flushing mechanisms rather
622af209b0fSMatthew Dillon 		 * then rolling our own.
623cb51be26SMatthew Dillon 		 *
624cb51be26SMatthew Dillon 		 * XXX there are two ways of doing this.  We can re-acquire
625cb51be26SMatthew Dillon 		 * and passively release to reset the LRU, or not.
62619b97e01SMatthew Dillon 		 */
627af209b0fSMatthew Dillon 		if (io->running == 0) {
62819b97e01SMatthew Dillon 			regetblk(bp);
629cebe9493SMatthew Dillon 			if ((bp->b_flags & B_LOCKED) || io->reclaim) {
630ff66f880STomohiro Kusumi 				hammer_io_disassociate(io);
631ecca949aSMatthew Dillon 				/* return the bp */
6329f5097dcSMatthew Dillon 			} else {
633ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
634ecca949aSMatthew Dillon 			}
635ecca949aSMatthew Dillon 		} else {
636ecca949aSMatthew Dillon 			/*
637ecca949aSMatthew Dillon 			 * bp is left passively associated but we do not
638ecca949aSMatthew Dillon 			 * try to reacquire it.  Interactions with the io
639ecca949aSMatthew Dillon 			 * structure will occur on completion of the bp's
640ecca949aSMatthew Dillon 			 * I/O.
641ecca949aSMatthew Dillon 			 */
642ecca949aSMatthew Dillon 			bp = NULL;
64319b97e01SMatthew Dillon 		}
6449f5097dcSMatthew Dillon 	}
645ecca949aSMatthew Dillon 	return(bp);
646055f5ff8SMatthew Dillon }
647055f5ff8SMatthew Dillon 
648055f5ff8SMatthew Dillon /*
649b33e2cc0SMatthew Dillon  * This routine is called with a locked IO when a flush is desired and
650b33e2cc0SMatthew Dillon  * no other references to the structure exists other then ours.  This
651b33e2cc0SMatthew Dillon  * routine is ONLY called when HAMMER believes it is safe to flush a
652b33e2cc0SMatthew Dillon  * potentially modified buffer out.
65377912481SMatthew Dillon  *
65477912481SMatthew Dillon  * The locked io or io reference prevents a flush from being initiated
65577912481SMatthew Dillon  * by the kernel.
6560b075555SMatthew Dillon  */
6570b075555SMatthew Dillon void
658710733a6SMatthew Dillon hammer_io_flush(struct hammer_io *io, int reclaim)
6590b075555SMatthew Dillon {
660055f5ff8SMatthew Dillon 	struct buf *bp;
66177912481SMatthew Dillon 	hammer_mount_t hmp;
662055f5ff8SMatthew Dillon 
663055f5ff8SMatthew Dillon 	/*
66410a5d1baSMatthew Dillon 	 * Degenerate case - nothing to flush if nothing is dirty.
665055f5ff8SMatthew Dillon 	 */
666b0aab9b9SMatthew Dillon 	if (io->modified == 0)
667055f5ff8SMatthew Dillon 		return;
668055f5ff8SMatthew Dillon 
669055f5ff8SMatthew Dillon 	KKASSERT(io->bp);
6709f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs <= 0);
671055f5ff8SMatthew Dillon 
672b33e2cc0SMatthew Dillon 	/*
67377062c8aSMatthew Dillon 	 * Acquire ownership of the bp, particularly before we clear our
67477062c8aSMatthew Dillon 	 * modified flag.
67577062c8aSMatthew Dillon 	 *
67677062c8aSMatthew Dillon 	 * We are going to bawrite() this bp.  Don't leave a window where
67777062c8aSMatthew Dillon 	 * io->released is set, we actually own the bp rather then our
67877062c8aSMatthew Dillon 	 * buffer.
679b0aab9b9SMatthew Dillon 	 *
680b0aab9b9SMatthew Dillon 	 * The io_token should not be required here as only
68177062c8aSMatthew Dillon 	 */
68277912481SMatthew Dillon 	hmp = io->hmp;
68377062c8aSMatthew Dillon 	bp = io->bp;
68477062c8aSMatthew Dillon 	if (io->released) {
68577062c8aSMatthew Dillon 		regetblk(bp);
68677062c8aSMatthew Dillon 		/* BUF_KERNPROC(io->bp); */
68777062c8aSMatthew Dillon 		/* io->released = 0; */
68877062c8aSMatthew Dillon 		KKASSERT(io->released);
68977062c8aSMatthew Dillon 		KKASSERT(io->bp == bp);
690b0aab9b9SMatthew Dillon 	} else {
69177062c8aSMatthew Dillon 		io->released = 1;
692b0aab9b9SMatthew Dillon 	}
69377062c8aSMatthew Dillon 
694710733a6SMatthew Dillon 	if (reclaim) {
695710733a6SMatthew Dillon 		io->reclaim = 1;
696710733a6SMatthew Dillon 		if ((bp->b_flags & B_LOCKED) == 0) {
697710733a6SMatthew Dillon 			bp->b_flags |= B_LOCKED;
698b0aab9b9SMatthew Dillon 			atomic_add_int(&hammer_count_io_locked, 1);
699710733a6SMatthew Dillon 		}
700710733a6SMatthew Dillon 	}
701710733a6SMatthew Dillon 
70277062c8aSMatthew Dillon 	/*
70310a5d1baSMatthew Dillon 	 * Acquire exclusive access to the bp and then clear the modified
70410a5d1baSMatthew Dillon 	 * state of the buffer prior to issuing I/O to interlock any
70510a5d1baSMatthew Dillon 	 * modifications made while the I/O is in progress.  This shouldn't
70610a5d1baSMatthew Dillon 	 * happen anyway but losing data would be worse.  The modified bit
70710a5d1baSMatthew Dillon 	 * will be rechecked after the IO completes.
70810a5d1baSMatthew Dillon 	 *
7094a2796f3SMatthew Dillon 	 * NOTE: This call also finalizes the buffer's content (inval == 0).
7104a2796f3SMatthew Dillon 	 *
711b33e2cc0SMatthew Dillon 	 * This is only legal when lock.refs == 1 (otherwise we might clear
712b33e2cc0SMatthew Dillon 	 * the modified bit while there are still users of the cluster
713b33e2cc0SMatthew Dillon 	 * modifying the data).
714b33e2cc0SMatthew Dillon 	 *
715b33e2cc0SMatthew Dillon 	 * Do this before potentially blocking so any attempt to modify the
716b33e2cc0SMatthew Dillon 	 * ondisk while we are blocked blocks waiting for us.
717b33e2cc0SMatthew Dillon 	 */
7185c8d05e2SMatthew Dillon 	hammer_ref(&io->lock);
7194a2796f3SMatthew Dillon 	hammer_io_clear_modify(io, 0);
720250aec18SMatthew Dillon 	hammer_rel(&io->lock);
721bcac4bbbSMatthew Dillon 
7226367d0f9SMatthew Dillon 	if (hammer_debug_io & 0x0002)
72311605a5cSTomohiro Kusumi 		hdkprintf("%016jx\n", bp->b_bio1.bio_offset);
7246367d0f9SMatthew Dillon 
725bcac4bbbSMatthew Dillon 	/*
72610a5d1baSMatthew Dillon 	 * Transfer ownership to the kernel and initiate I/O.
727b0aab9b9SMatthew Dillon 	 *
728b0aab9b9SMatthew Dillon 	 * NOTE: We do not hold io_token so an atomic op is required to
729b0aab9b9SMatthew Dillon 	 *	 update io_running_space.
73010a5d1baSMatthew Dillon 	 */
731055f5ff8SMatthew Dillon 	io->running = 1;
7323583bbb4SMatthew Dillon 	atomic_add_long(&hmp->io_running_space, io->bytes);
7333583bbb4SMatthew Dillon 	atomic_add_long(&hammer_count_io_running_write, io->bytes);
73477912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
73577912481SMatthew Dillon 	TAILQ_INSERT_TAIL(&hmp->iorun_list, io, iorun_entry);
73677912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
7379de13b88SMatthew Dillon 	cluster_awrite(bp);
738748efb59SMatthew Dillon 	hammer_io_flush_mark(io->volume);
739055f5ff8SMatthew Dillon }
740055f5ff8SMatthew Dillon 
741055f5ff8SMatthew Dillon /************************************************************************
742055f5ff8SMatthew Dillon  *				BUFFER DIRTYING				*
743055f5ff8SMatthew Dillon  ************************************************************************
744055f5ff8SMatthew Dillon  *
745055f5ff8SMatthew Dillon  * These routines deal with dependancies created when IO buffers get
746055f5ff8SMatthew Dillon  * modified.  The caller must call hammer_modify_*() on a referenced
747055f5ff8SMatthew Dillon  * HAMMER structure prior to modifying its on-disk data.
748055f5ff8SMatthew Dillon  *
749055f5ff8SMatthew Dillon  * Any intent to modify an IO buffer acquires the related bp and imposes
750055f5ff8SMatthew Dillon  * various write ordering dependancies.
751055f5ff8SMatthew Dillon  */
752055f5ff8SMatthew Dillon 
753055f5ff8SMatthew Dillon /*
75410a5d1baSMatthew Dillon  * Mark a HAMMER structure as undergoing modification.  Meta-data buffers
75510a5d1baSMatthew Dillon  * are locked until the flusher can deal with them, pure data buffers
75610a5d1baSMatthew Dillon  * can be written out.
75777912481SMatthew Dillon  *
75877912481SMatthew Dillon  * The referenced io prevents races.
759055f5ff8SMatthew Dillon  */
76010a5d1baSMatthew Dillon static
761b58c6388SMatthew Dillon void
76210a5d1baSMatthew Dillon hammer_io_modify(hammer_io_t io, int count)
763055f5ff8SMatthew Dillon {
76446fe7ae1SMatthew Dillon 	/*
7659f5097dcSMatthew Dillon 	 * io->modify_refs must be >= 0
7669f5097dcSMatthew Dillon 	 */
7679f5097dcSMatthew Dillon 	while (io->modify_refs < 0) {
7689f5097dcSMatthew Dillon 		io->waitmod = 1;
7699f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
7709f5097dcSMatthew Dillon 	}
7719f5097dcSMatthew Dillon 
7729f5097dcSMatthew Dillon 	/*
77346fe7ae1SMatthew Dillon 	 * Shortcut if nothing to do.
77446fe7ae1SMatthew Dillon 	 */
775250aec18SMatthew Dillon 	KKASSERT(hammer_isactive(&io->lock) && io->bp != NULL);
77610a5d1baSMatthew Dillon 	io->modify_refs += count;
777b58c6388SMatthew Dillon 	if (io->modified && io->released == 0)
778b58c6388SMatthew Dillon 		return;
77946fe7ae1SMatthew Dillon 
78077912481SMatthew Dillon 	/*
78177912481SMatthew Dillon 	 * NOTE: It is important not to set the modified bit
78277912481SMatthew Dillon 	 *	 until after we have acquired the bp or we risk
78377912481SMatthew Dillon 	 *	 racing against checkwrite.
78477912481SMatthew Dillon 	 */
785055f5ff8SMatthew Dillon 	hammer_lock_ex(&io->lock);
786055f5ff8SMatthew Dillon 	if (io->released) {
787055f5ff8SMatthew Dillon 		regetblk(io->bp);
788055f5ff8SMatthew Dillon 		BUF_KERNPROC(io->bp);
789055f5ff8SMatthew Dillon 		io->released = 0;
79077912481SMatthew Dillon 	}
79177912481SMatthew Dillon 	if (io->modified == 0) {
79277912481SMatthew Dillon 		hammer_io_set_modlist(io);
79377912481SMatthew Dillon 		io->modified = 1;
794055f5ff8SMatthew Dillon 	}
795055f5ff8SMatthew Dillon 	hammer_unlock(&io->lock);
7960b075555SMatthew Dillon }
7970b075555SMatthew Dillon 
79810a5d1baSMatthew Dillon static __inline
79910a5d1baSMatthew Dillon void
80010a5d1baSMatthew Dillon hammer_io_modify_done(hammer_io_t io)
80110a5d1baSMatthew Dillon {
80210a5d1baSMatthew Dillon 	KKASSERT(io->modify_refs > 0);
80310a5d1baSMatthew Dillon 	--io->modify_refs;
8049f5097dcSMatthew Dillon 	if (io->modify_refs == 0 && io->waitmod) {
8059f5097dcSMatthew Dillon 		io->waitmod = 0;
8069f5097dcSMatthew Dillon 		wakeup(io);
8079f5097dcSMatthew Dillon 	}
8089f5097dcSMatthew Dillon }
8099f5097dcSMatthew Dillon 
81077912481SMatthew Dillon /*
81177912481SMatthew Dillon  * The write interlock blocks other threads trying to modify a buffer
81277912481SMatthew Dillon  * (they block in hammer_io_modify()) after us, or blocks us while other
81377912481SMatthew Dillon  * threads are in the middle of modifying a buffer.
81477912481SMatthew Dillon  *
81577912481SMatthew Dillon  * The caller also has a ref on the io, however if we are not careful
81677912481SMatthew Dillon  * we will race bioops callbacks (checkwrite).  To deal with this
81777912481SMatthew Dillon  * we must at least acquire and release the io_token, and it is probably
81877912481SMatthew Dillon  * better to hold it through the setting of modify_refs.
81977912481SMatthew Dillon  */
8209f5097dcSMatthew Dillon void
8219f5097dcSMatthew Dillon hammer_io_write_interlock(hammer_io_t io)
8229f5097dcSMatthew Dillon {
82377912481SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
82477912481SMatthew Dillon 
82577912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
8269f5097dcSMatthew Dillon 	while (io->modify_refs != 0) {
8279f5097dcSMatthew Dillon 		io->waitmod = 1;
8289f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
8299f5097dcSMatthew Dillon 	}
8309f5097dcSMatthew Dillon 	io->modify_refs = -1;
83177912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
8329f5097dcSMatthew Dillon }
8339f5097dcSMatthew Dillon 
8349f5097dcSMatthew Dillon void
8359f5097dcSMatthew Dillon hammer_io_done_interlock(hammer_io_t io)
8369f5097dcSMatthew Dillon {
8379f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs == -1);
8389f5097dcSMatthew Dillon 	io->modify_refs = 0;
8399f5097dcSMatthew Dillon 	if (io->waitmod) {
8409f5097dcSMatthew Dillon 		io->waitmod = 0;
8419f5097dcSMatthew Dillon 		wakeup(io);
8429f5097dcSMatthew Dillon 	}
84310a5d1baSMatthew Dillon }
84410a5d1baSMatthew Dillon 
8452f85fa4dSMatthew Dillon /*
8462f85fa4dSMatthew Dillon  * Caller intends to modify a volume's ondisk structure.
8472f85fa4dSMatthew Dillon  *
8482f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
8492f85fa4dSMatthew Dillon  * sync_lock.
8502f85fa4dSMatthew Dillon  */
8510b075555SMatthew Dillon void
85236f82b23SMatthew Dillon hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
85336f82b23SMatthew Dillon 		     void *base, int len)
8540b075555SMatthew Dillon {
8552f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
856055f5ff8SMatthew Dillon 
8572f85fa4dSMatthew Dillon 	hammer_io_modify(&volume->io, 1);
85847197d71SMatthew Dillon 	if (len) {
85947197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
86047197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
86102428fb6SMatthew Dillon 		hammer_generate_undo(trans,
86247197d71SMatthew Dillon 			 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
86347197d71SMatthew Dillon 			 base, len);
864055f5ff8SMatthew Dillon 	}
865055f5ff8SMatthew Dillon }
866055f5ff8SMatthew Dillon 
867055f5ff8SMatthew Dillon /*
8682f85fa4dSMatthew Dillon  * Caller intends to modify a buffer's ondisk structure.
8692f85fa4dSMatthew Dillon  *
8702f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
8712f85fa4dSMatthew Dillon  * sync_lock.
872055f5ff8SMatthew Dillon  */
873055f5ff8SMatthew Dillon void
87436f82b23SMatthew Dillon hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
87536f82b23SMatthew Dillon 		     void *base, int len)
87646fe7ae1SMatthew Dillon {
8772f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
8782f85fa4dSMatthew Dillon 
87910a5d1baSMatthew Dillon 	hammer_io_modify(&buffer->io, 1);
88047197d71SMatthew Dillon 	if (len) {
88147197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
88247197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
88302428fb6SMatthew Dillon 		hammer_generate_undo(trans,
88434d829f7SMatthew Dillon 				     buffer->zone2_offset + rel_offset,
88547197d71SMatthew Dillon 				     base, len);
88647197d71SMatthew Dillon 	}
88746fe7ae1SMatthew Dillon }
88846fe7ae1SMatthew Dillon 
88910a5d1baSMatthew Dillon void
89010a5d1baSMatthew Dillon hammer_modify_volume_done(hammer_volume_t volume)
89110a5d1baSMatthew Dillon {
89210a5d1baSMatthew Dillon 	hammer_io_modify_done(&volume->io);
89310a5d1baSMatthew Dillon }
89410a5d1baSMatthew Dillon 
89510a5d1baSMatthew Dillon void
89610a5d1baSMatthew Dillon hammer_modify_buffer_done(hammer_buffer_t buffer)
89710a5d1baSMatthew Dillon {
89810a5d1baSMatthew Dillon 	hammer_io_modify_done(&buffer->io);
89910a5d1baSMatthew Dillon }
90010a5d1baSMatthew Dillon 
90146fe7ae1SMatthew Dillon /*
9024a2796f3SMatthew Dillon  * Mark an entity as not being dirty any more and finalize any
9034a2796f3SMatthew Dillon  * delayed adjustments to the buffer.
9044a2796f3SMatthew Dillon  *
9054a2796f3SMatthew Dillon  * Delayed adjustments are an important performance enhancement, allowing
9064a2796f3SMatthew Dillon  * us to avoid recalculating B-Tree node CRCs over and over again when
9074a2796f3SMatthew Dillon  * making bulk-modifications to the B-Tree.
9084a2796f3SMatthew Dillon  *
9094a2796f3SMatthew Dillon  * If inval is non-zero delayed adjustments are ignored.
9105c8d05e2SMatthew Dillon  *
9115c8d05e2SMatthew Dillon  * This routine may dereference related btree nodes and cause the
9125c8d05e2SMatthew Dillon  * buffer to be dereferenced.  The caller must own a reference on io.
91361aeeb33SMatthew Dillon  */
91461aeeb33SMatthew Dillon void
9154a2796f3SMatthew Dillon hammer_io_clear_modify(struct hammer_io *io, int inval)
91661aeeb33SMatthew Dillon {
91777912481SMatthew Dillon 	hammer_mount_t hmp;
91877912481SMatthew Dillon 
91977912481SMatthew Dillon 	/*
9201afb73cfSMatthew Dillon 	 * io_token is needed to avoid races on mod_root
92177912481SMatthew Dillon 	 */
9224a2796f3SMatthew Dillon 	if (io->modified == 0)
9234a2796f3SMatthew Dillon 		return;
92477912481SMatthew Dillon 	hmp = io->hmp;
92577912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
92677912481SMatthew Dillon 	if (io->modified == 0) {
92777912481SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
92877912481SMatthew Dillon 		return;
92977912481SMatthew Dillon 	}
9304a2796f3SMatthew Dillon 
9314a2796f3SMatthew Dillon 	/*
9324a2796f3SMatthew Dillon 	 * Take us off the mod-list and clear the modified bit.
9334a2796f3SMatthew Dillon 	 */
9341afb73cfSMatthew Dillon 	KKASSERT(io->mod_root != NULL);
9351afb73cfSMatthew Dillon 	if (io->mod_root == &io->hmp->volu_root ||
9361afb73cfSMatthew Dillon 	    io->mod_root == &io->hmp->meta_root) {
937f5a07a7aSMatthew Dillon 		io->hmp->locked_dirty_space -= io->bytes;
9383583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_dirtybufspace, -io->bytes);
939cebe9493SMatthew Dillon 	}
9401afb73cfSMatthew Dillon 	RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io);
9411afb73cfSMatthew Dillon 	io->mod_root = NULL;
94261aeeb33SMatthew Dillon 	io->modified = 0;
9434a2796f3SMatthew Dillon 
94477912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
94577912481SMatthew Dillon 
9464a2796f3SMatthew Dillon 	/*
9474a2796f3SMatthew Dillon 	 * If this bit is not set there are no delayed adjustments.
9484a2796f3SMatthew Dillon 	 */
9494a2796f3SMatthew Dillon 	if (io->gencrc == 0)
9504a2796f3SMatthew Dillon 		return;
9514a2796f3SMatthew Dillon 	io->gencrc = 0;
9524a2796f3SMatthew Dillon 
9534a2796f3SMatthew Dillon 	/*
9544a2796f3SMatthew Dillon 	 * Finalize requested CRCs.  The NEEDSCRC flag also holds a reference
9554a2796f3SMatthew Dillon 	 * on the node (& underlying buffer).  Release the node after clearing
9564a2796f3SMatthew Dillon 	 * the flag.
9574a2796f3SMatthew Dillon 	 */
9584a2796f3SMatthew Dillon 	if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
959195f6076STomohiro Kusumi 		hammer_buffer_t buffer = HAMMER_ITOB(io);
9604a2796f3SMatthew Dillon 		hammer_node_t node;
9614a2796f3SMatthew Dillon 
9624a2796f3SMatthew Dillon restart:
963c242ffecSTomohiro Kusumi 		TAILQ_FOREACH(node, &buffer->node_list, entry) {
9644a2796f3SMatthew Dillon 			if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
9654a2796f3SMatthew Dillon 				continue;
9664a2796f3SMatthew Dillon 			node->flags &= ~HAMMER_NODE_NEEDSCRC;
9674a2796f3SMatthew Dillon 			KKASSERT(node->ondisk);
9684a2796f3SMatthew Dillon 			if (inval == 0)
969af3e4d3eSTomohiro Kusumi 				hammer_crc_set_btree(node->ondisk);
9704a2796f3SMatthew Dillon 			hammer_rel_node(node);
9714a2796f3SMatthew Dillon 			goto restart;
97261aeeb33SMatthew Dillon 		}
97361aeeb33SMatthew Dillon 	}
9745c8d05e2SMatthew Dillon 	/* caller must still have ref on io */
975250aec18SMatthew Dillon 	KKASSERT(hammer_isactive(&io->lock));
9764a2796f3SMatthew Dillon }
9774a2796f3SMatthew Dillon 
978cebe9493SMatthew Dillon /*
979cebe9493SMatthew Dillon  * Clear the IO's modify list.  Even though the IO is no longer modified
9801afb73cfSMatthew Dillon  * it may still be on the lose_root.  This routine is called just before
981cebe9493SMatthew Dillon  * the governing hammer_buffer is destroyed.
982b0aab9b9SMatthew Dillon  *
9831afb73cfSMatthew Dillon  * mod_root requires io_token protection.
984cebe9493SMatthew Dillon  */
985cebe9493SMatthew Dillon void
986cebe9493SMatthew Dillon hammer_io_clear_modlist(struct hammer_io *io)
987cebe9493SMatthew Dillon {
988b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
989b0aab9b9SMatthew Dillon 
9904a2796f3SMatthew Dillon 	KKASSERT(io->modified == 0);
9911afb73cfSMatthew Dillon 	if (io->mod_root) {
992b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
9931afb73cfSMatthew Dillon 		if (io->mod_root) {
9941afb73cfSMatthew Dillon 			KKASSERT(io->mod_root == &io->hmp->lose_root);
9951afb73cfSMatthew Dillon 			RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io);
9961afb73cfSMatthew Dillon 			io->mod_root = NULL;
997b0aab9b9SMatthew Dillon 		}
998b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
999cebe9493SMatthew Dillon 	}
100066325755SMatthew Dillon }
100166325755SMatthew Dillon 
1002cdb6e4e6SMatthew Dillon static void
1003cdb6e4e6SMatthew Dillon hammer_io_set_modlist(struct hammer_io *io)
1004cdb6e4e6SMatthew Dillon {
1005cdb6e4e6SMatthew Dillon 	struct hammer_mount *hmp = io->hmp;
1006cdb6e4e6SMatthew Dillon 
100777912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
10081afb73cfSMatthew Dillon 	KKASSERT(io->mod_root == NULL);
1009cdb6e4e6SMatthew Dillon 
1010cdb6e4e6SMatthew Dillon 	switch(io->type) {
1011cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_VOLUME:
10121afb73cfSMatthew Dillon 		io->mod_root = &hmp->volu_root;
1013cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
10143583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_dirtybufspace, io->bytes);
1015cdb6e4e6SMatthew Dillon 		break;
1016cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_META_BUFFER:
10171afb73cfSMatthew Dillon 		io->mod_root = &hmp->meta_root;
1018cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
10193583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_dirtybufspace, io->bytes);
1020cdb6e4e6SMatthew Dillon 		break;
1021cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_UNDO_BUFFER:
10221afb73cfSMatthew Dillon 		io->mod_root = &hmp->undo_root;
1023cdb6e4e6SMatthew Dillon 		break;
1024cdb6e4e6SMatthew Dillon 	case HAMMER_STRUCTURE_DATA_BUFFER:
10251afb73cfSMatthew Dillon 		io->mod_root = &hmp->data_root;
1026cdb6e4e6SMatthew Dillon 		break;
1027eddadaeeSMatthew Dillon 	case HAMMER_STRUCTURE_DUMMY:
1028903fdd05STomohiro Kusumi 		hpanic("bad io type");
10291afb73cfSMatthew Dillon 		break; /* NOT REACHED */
1030cdb6e4e6SMatthew Dillon 	}
10311afb73cfSMatthew Dillon 	if (RB_INSERT(hammer_mod_rb_tree, io->mod_root, io)) {
1032b49481fbSTomohiro Kusumi 		hpanic("duplicate entry @ %d:%015jx",
1033b49481fbSTomohiro Kusumi 			io->volume->vol_no, io->offset);
10341afb73cfSMatthew Dillon 		/* NOT REACHED */
10351afb73cfSMatthew Dillon 	}
103677912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
1037cdb6e4e6SMatthew Dillon }
1038cdb6e4e6SMatthew Dillon 
1039055f5ff8SMatthew Dillon /************************************************************************
1040055f5ff8SMatthew Dillon  *				HAMMER_BIOOPS				*
1041055f5ff8SMatthew Dillon  ************************************************************************
1042055f5ff8SMatthew Dillon  *
1043055f5ff8SMatthew Dillon  */
1044055f5ff8SMatthew Dillon 
1045055f5ff8SMatthew Dillon /*
1046055f5ff8SMatthew Dillon  * Pre-IO initiation kernel callback - cluster build only
1047b0aab9b9SMatthew Dillon  *
1048b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1049055f5ff8SMatthew Dillon  */
1050055f5ff8SMatthew Dillon static void
1051055f5ff8SMatthew Dillon hammer_io_start(struct buf *bp)
1052055f5ff8SMatthew Dillon {
1053b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
1054055f5ff8SMatthew Dillon }
1055055f5ff8SMatthew Dillon 
1056055f5ff8SMatthew Dillon /*
10577bc5b8c2SMatthew Dillon  * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
1058b33e2cc0SMatthew Dillon  *
105977912481SMatthew Dillon  * NOTE: HAMMER may modify a data buffer after we have initiated write
106077912481SMatthew Dillon  *	 I/O.
106177912481SMatthew Dillon  *
106277912481SMatthew Dillon  * NOTE: MPSAFE callback
1063b0aab9b9SMatthew Dillon  *
1064b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1065055f5ff8SMatthew Dillon  */
106666325755SMatthew Dillon static void
106766325755SMatthew Dillon hammer_io_complete(struct buf *bp)
106866325755SMatthew Dillon {
10690ae73f43STomohiro Kusumi 	hammer_io_t io = hammer_buf_peek_io(bp);
1070ff66f880STomohiro Kusumi 	struct hammer_mount *hmp = io->hmp;
1071eddadaeeSMatthew Dillon 	struct hammer_io *ionext;
1072fbc6e32aSMatthew Dillon 
1073b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
1074b0aab9b9SMatthew Dillon 
1075ff66f880STomohiro Kusumi 	KKASSERT(io->released == 1);
1076055f5ff8SMatthew Dillon 
1077bf3b416bSMatthew Dillon 	/*
1078bf3b416bSMatthew Dillon 	 * Deal with people waiting for I/O to drain
1079bf3b416bSMatthew Dillon 	 */
1080ff66f880STomohiro Kusumi 	if (io->running) {
1081cdb6e4e6SMatthew Dillon 		/*
1082cdb6e4e6SMatthew Dillon 		 * Deal with critical write errors.  Once a critical error
1083cdb6e4e6SMatthew Dillon 		 * has been flagged in hmp the UNDO FIFO will not be updated.
1084cdb6e4e6SMatthew Dillon 		 * That way crash recover will give us a consistent
1085cdb6e4e6SMatthew Dillon 		 * filesystem.
1086cdb6e4e6SMatthew Dillon 		 *
1087cdb6e4e6SMatthew Dillon 		 * Because of this we can throw away failed UNDO buffers.  If
1088cdb6e4e6SMatthew Dillon 		 * we throw away META or DATA buffers we risk corrupting
1089cdb6e4e6SMatthew Dillon 		 * the now read-only version of the filesystem visible to
1090cdb6e4e6SMatthew Dillon 		 * the user.  Clear B_ERROR so the buffer is not re-dirtied
1091cdb6e4e6SMatthew Dillon 		 * by the kernel and ref the io so it doesn't get thrown
1092cdb6e4e6SMatthew Dillon 		 * away.
1093cdb6e4e6SMatthew Dillon 		 */
1094cdb6e4e6SMatthew Dillon 		if (bp->b_flags & B_ERROR) {
109577912481SMatthew Dillon 			lwkt_gettoken(&hmp->fs_token);
1096ba298df1SMatthew Dillon 			hammer_critical_error(hmp, NULL, bp->b_error,
1097cdb6e4e6SMatthew Dillon 					      "while flushing meta-data");
109877912481SMatthew Dillon 			lwkt_reltoken(&hmp->fs_token);
109977912481SMatthew Dillon 
1100ff66f880STomohiro Kusumi 			switch(io->type) {
1101cdb6e4e6SMatthew Dillon 			case HAMMER_STRUCTURE_UNDO_BUFFER:
1102cdb6e4e6SMatthew Dillon 				break;
1103cdb6e4e6SMatthew Dillon 			default:
1104ff66f880STomohiro Kusumi 				if (io->ioerror == 0) {
1105ff66f880STomohiro Kusumi 					io->ioerror = 1;
1106ff66f880STomohiro Kusumi 					hammer_ref(&io->lock);
1107cdb6e4e6SMatthew Dillon 				}
1108cdb6e4e6SMatthew Dillon 				break;
1109cdb6e4e6SMatthew Dillon 			}
1110cdb6e4e6SMatthew Dillon 			bp->b_flags &= ~B_ERROR;
1111cdb6e4e6SMatthew Dillon 			bundirty(bp);
1112cdb6e4e6SMatthew Dillon #if 0
1113ff66f880STomohiro Kusumi 			hammer_io_set_modlist(io);
1114ff66f880STomohiro Kusumi 			io->modified = 1;
1115cdb6e4e6SMatthew Dillon #endif
1116cdb6e4e6SMatthew Dillon 		}
1117ff66f880STomohiro Kusumi 		hammer_stats_disk_write += io->bytes;
1118ff66f880STomohiro Kusumi 		atomic_add_long(&hammer_count_io_running_write, -io->bytes);
1119ff66f880STomohiro Kusumi 		atomic_add_long(&hmp->io_running_space, -io->bytes);
1120ba298df1SMatthew Dillon 		KKASSERT(hmp->io_running_space >= 0);
1121ff66f880STomohiro Kusumi 		io->running = 0;
1122eddadaeeSMatthew Dillon 
1123eddadaeeSMatthew Dillon 		/*
1124eddadaeeSMatthew Dillon 		 * Remove from iorun list and wakeup any multi-io waiter(s).
1125eddadaeeSMatthew Dillon 		 */
1126ff66f880STomohiro Kusumi 		if (TAILQ_FIRST(&hmp->iorun_list) == io) {
1127ff66f880STomohiro Kusumi 			ionext = TAILQ_NEXT(io, iorun_entry);
1128eddadaeeSMatthew Dillon 			if (ionext && ionext->type == HAMMER_STRUCTURE_DUMMY)
1129eddadaeeSMatthew Dillon 				wakeup(ionext);
1130eddadaeeSMatthew Dillon 		}
1131ff66f880STomohiro Kusumi 		TAILQ_REMOVE(&hmp->iorun_list, io, iorun_entry);
1132ce0138a6SMatthew Dillon 	} else {
1133ff66f880STomohiro Kusumi 		hammer_stats_disk_read += io->bytes;
1134f90dde4cSMatthew Dillon 	}
1135f90dde4cSMatthew Dillon 
1136ff66f880STomohiro Kusumi 	if (io->waiting) {
1137ff66f880STomohiro Kusumi 		io->waiting = 0;
1138ff66f880STomohiro Kusumi 		wakeup(io);
1139055f5ff8SMatthew Dillon 	}
1140055f5ff8SMatthew Dillon 
1141055f5ff8SMatthew Dillon 	/*
1142bf3b416bSMatthew Dillon 	 * If B_LOCKED is set someone wanted to deallocate the bp at some
1143250aec18SMatthew Dillon 	 * point, try to do it now.  The operation will fail if there are
1144250aec18SMatthew Dillon 	 * refs or if hammer_io_deallocate() is unable to gain the
1145250aec18SMatthew Dillon 	 * interlock.
1146055f5ff8SMatthew Dillon 	 */
1147250aec18SMatthew Dillon 	if (bp->b_flags & B_LOCKED) {
1148b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, -1);
1149d5ef456eSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
1150055f5ff8SMatthew Dillon 		hammer_io_deallocate(bp);
1151055f5ff8SMatthew Dillon 		/* structure may be dead now */
1152fbc6e32aSMatthew Dillon 	}
1153b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
115466325755SMatthew Dillon }
115566325755SMatthew Dillon 
115666325755SMatthew Dillon /*
115766325755SMatthew Dillon  * Callback from kernel when it wishes to deallocate a passively
115810a5d1baSMatthew Dillon  * associated structure.  This mostly occurs with clean buffers
115910a5d1baSMatthew Dillon  * but it may be possible for a holding structure to be marked dirty
11607bc5b8c2SMatthew Dillon  * while its buffer is passively associated.  The caller owns the bp.
116166325755SMatthew Dillon  *
116266325755SMatthew Dillon  * If we cannot disassociate we set B_LOCKED to prevent the buffer
116366325755SMatthew Dillon  * from getting reused.
116446fe7ae1SMatthew Dillon  *
116546fe7ae1SMatthew Dillon  * WARNING: Because this can be called directly by getnewbuf we cannot
116646fe7ae1SMatthew Dillon  * recurse into the tree.  If a bp cannot be immediately disassociated
116746fe7ae1SMatthew Dillon  * our only recourse is to set B_LOCKED.
11687bc5b8c2SMatthew Dillon  *
11697bc5b8c2SMatthew Dillon  * WARNING: This may be called from an interrupt via hammer_io_complete()
1170b0aab9b9SMatthew Dillon  *
1171b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
117266325755SMatthew Dillon  */
117366325755SMatthew Dillon static void
117466325755SMatthew Dillon hammer_io_deallocate(struct buf *bp)
117566325755SMatthew Dillon {
11760ae73f43STomohiro Kusumi 	hammer_io_t io = hammer_buf_peek_io(bp);
1177b0aab9b9SMatthew Dillon 	hammer_mount_t hmp;
1178b0aab9b9SMatthew Dillon 
1179ff66f880STomohiro Kusumi 	hmp = io->hmp;
1180b0aab9b9SMatthew Dillon 
1181b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
118266325755SMatthew Dillon 
1183ff66f880STomohiro Kusumi 	KKASSERT((bp->b_flags & B_LOCKED) == 0 && io->running == 0);
1184ff66f880STomohiro Kusumi 	if (hammer_try_interlock_norefs(&io->lock) == 0) {
1185250aec18SMatthew Dillon 		/*
1186250aec18SMatthew Dillon 		 * We cannot safely disassociate a bp from a referenced
1187250aec18SMatthew Dillon 		 * or interlocked HAMMER structure.
1188250aec18SMatthew Dillon 		 */
1189250aec18SMatthew Dillon 		bp->b_flags |= B_LOCKED;
1190b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, 1);
1191ff66f880STomohiro Kusumi 	} else if (io->modified) {
119210a5d1baSMatthew Dillon 		/*
119310a5d1baSMatthew Dillon 		 * It is not legal to disassociate a modified buffer.  This
119410a5d1baSMatthew Dillon 		 * case really shouldn't ever occur.
119510a5d1baSMatthew Dillon 		 */
1196055f5ff8SMatthew Dillon 		bp->b_flags |= B_LOCKED;
1197b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, 1);
1198ff66f880STomohiro Kusumi 		hammer_put_interlock(&io->lock, 0);
1199055f5ff8SMatthew Dillon 	} else {
120010a5d1baSMatthew Dillon 		/*
120110a5d1baSMatthew Dillon 		 * Disassociate the BP.  If the io has no refs left we
1202b0aab9b9SMatthew Dillon 		 * have to add it to the loose list.  The kernel has
1203b0aab9b9SMatthew Dillon 		 * locked the buffer and therefore our io must be
1204b0aab9b9SMatthew Dillon 		 * in a released state.
120510a5d1baSMatthew Dillon 		 */
1206ff66f880STomohiro Kusumi 		hammer_io_disassociate(io);
1207ff66f880STomohiro Kusumi 		if (io->type != HAMMER_STRUCTURE_VOLUME) {
1208ff66f880STomohiro Kusumi 			KKASSERT(io->bp == NULL);
1209ff66f880STomohiro Kusumi 			KKASSERT(io->mod_root == NULL);
1210ff66f880STomohiro Kusumi 			io->mod_root = &hmp->lose_root;
1211b49481fbSTomohiro Kusumi 			if (RB_INSERT(hammer_mod_rb_tree, io->mod_root, io)) {
1212b49481fbSTomohiro Kusumi 				hpanic("duplicate entry @ %d:%015jx",
1213b49481fbSTomohiro Kusumi 					io->volume->vol_no, io->offset);
1214b49481fbSTomohiro Kusumi 				/* NOT REACHED */
1215b49481fbSTomohiro Kusumi 			}
12161afb73cfSMatthew Dillon 		}
1217ff66f880STomohiro Kusumi 		hammer_put_interlock(&io->lock, 1);
121866325755SMatthew Dillon 	}
1219b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
122066325755SMatthew Dillon }
122166325755SMatthew Dillon 
1222b0aab9b9SMatthew Dillon /*
1223b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1224b0aab9b9SMatthew Dillon  */
122566325755SMatthew Dillon static int
122666325755SMatthew Dillon hammer_io_fsync(struct vnode *vp)
122766325755SMatthew Dillon {
1228b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
122966325755SMatthew Dillon 	return(0);
123066325755SMatthew Dillon }
123166325755SMatthew Dillon 
123266325755SMatthew Dillon /*
123366325755SMatthew Dillon  * NOTE: will not be called unless we tell the kernel about the
123466325755SMatthew Dillon  * bioops.  Unused... we use the mount's VFS_SYNC instead.
1235b0aab9b9SMatthew Dillon  *
1236b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
123766325755SMatthew Dillon  */
123866325755SMatthew Dillon static int
123966325755SMatthew Dillon hammer_io_sync(struct mount *mp)
124066325755SMatthew Dillon {
1241b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
124266325755SMatthew Dillon 	return(0);
124366325755SMatthew Dillon }
124466325755SMatthew Dillon 
1245b0aab9b9SMatthew Dillon /*
1246b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1247b0aab9b9SMatthew Dillon  */
124866325755SMatthew Dillon static void
124966325755SMatthew Dillon hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
125066325755SMatthew Dillon {
1251b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
125266325755SMatthew Dillon }
125366325755SMatthew Dillon 
125466325755SMatthew Dillon /*
125566325755SMatthew Dillon  * I/O pre-check for reading and writing.  HAMMER only uses this for
125666325755SMatthew Dillon  * B_CACHE buffers so checkread just shouldn't happen, but if it does
125766325755SMatthew Dillon  * allow it.
125866325755SMatthew Dillon  *
1259fbc6e32aSMatthew Dillon  * Writing is a different case.  We don't want the kernel to try to write
1260fbc6e32aSMatthew Dillon  * out a buffer that HAMMER may be modifying passively or which has a
126110a5d1baSMatthew Dillon  * dependancy.  In addition, kernel-demanded writes can only proceed for
126210a5d1baSMatthew Dillon  * certain types of buffers (i.e. UNDO and DATA types).  Other dirty
126310a5d1baSMatthew Dillon  * buffer types can only be explicitly written by the flusher.
1264fbc6e32aSMatthew Dillon  *
126510a5d1baSMatthew Dillon  * checkwrite will only be called for bdwrite()n buffers.  If we return
126610a5d1baSMatthew Dillon  * success the kernel is guaranteed to initiate the buffer write.
1267b0aab9b9SMatthew Dillon  *
1268b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
126966325755SMatthew Dillon  */
127066325755SMatthew Dillon static int
127166325755SMatthew Dillon hammer_io_checkread(struct buf *bp)
127266325755SMatthew Dillon {
1273b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
127466325755SMatthew Dillon 	return(0);
127566325755SMatthew Dillon }
127666325755SMatthew Dillon 
1277b0aab9b9SMatthew Dillon /*
127877912481SMatthew Dillon  * The kernel is asking us whether it can write out a dirty buffer or not.
127977912481SMatthew Dillon  *
1280b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1281b0aab9b9SMatthew Dillon  */
128266325755SMatthew Dillon static int
128366325755SMatthew Dillon hammer_io_checkwrite(struct buf *bp)
128466325755SMatthew Dillon {
12850ae73f43STomohiro Kusumi 	hammer_io_t io = hammer_buf_peek_io(bp);
1286b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
128766325755SMatthew Dillon 
128877062c8aSMatthew Dillon 	/*
128977062c8aSMatthew Dillon 	 * This shouldn't happen under normal operation.
129077062c8aSMatthew Dillon 	 */
1291b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
129277062c8aSMatthew Dillon 	if (io->type == HAMMER_STRUCTURE_VOLUME ||
129377062c8aSMatthew Dillon 	    io->type == HAMMER_STRUCTURE_META_BUFFER) {
129477062c8aSMatthew Dillon 		if (!panicstr)
1295903fdd05STomohiro Kusumi 			hpanic("illegal buffer");
1296a99b9ea2SMatthew Dillon 		if ((bp->b_flags & B_LOCKED) == 0) {
129777062c8aSMatthew Dillon 			bp->b_flags |= B_LOCKED;
1298b0aab9b9SMatthew Dillon 			atomic_add_int(&hammer_count_io_locked, 1);
1299a99b9ea2SMatthew Dillon 		}
1300b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
130177062c8aSMatthew Dillon 		return(1);
130277062c8aSMatthew Dillon 	}
1303c9b9e29dSMatthew Dillon 
1304fbc6e32aSMatthew Dillon 	/*
130577912481SMatthew Dillon 	 * We have to be able to interlock the IO to safely modify any
130677912481SMatthew Dillon 	 * of its fields without holding the fs_token.  If we can't lock
130777912481SMatthew Dillon 	 * it then we are racing someone.
130877912481SMatthew Dillon 	 *
130977912481SMatthew Dillon 	 * Our ownership of the bp lock prevents the io from being ripped
131077912481SMatthew Dillon 	 * out from under us.
131177912481SMatthew Dillon 	 */
131277912481SMatthew Dillon 	if (hammer_try_interlock_norefs(&io->lock) == 0) {
131377912481SMatthew Dillon 		bp->b_flags |= B_LOCKED;
131477912481SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, 1);
131577912481SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
131677912481SMatthew Dillon 		return(1);
131777912481SMatthew Dillon 	}
131877912481SMatthew Dillon 
131977912481SMatthew Dillon 	/*
132077912481SMatthew Dillon 	 * The modified bit must be cleared prior to the initiation of
132177912481SMatthew Dillon 	 * any IO (returning 0 initiates the IO).  Because this is a
132277912481SMatthew Dillon 	 * normal data buffer hammer_io_clear_modify() runs through a
132377912481SMatthew Dillon 	 * simple degenerate case.
132477912481SMatthew Dillon 	 *
132577912481SMatthew Dillon 	 * Return 0 will cause the kernel to initiate the IO, and we
132677912481SMatthew Dillon 	 * must normally clear the modified bit before we begin.  If
132777912481SMatthew Dillon 	 * the io has modify_refs we do not clear the modified bit,
132877912481SMatthew Dillon 	 * otherwise we may miss changes.
13295c8d05e2SMatthew Dillon 	 *
13305c8d05e2SMatthew Dillon 	 * Only data and undo buffers can reach here.  These buffers do
13315c8d05e2SMatthew Dillon 	 * not have terminal crc functions but we temporarily reference
13325c8d05e2SMatthew Dillon 	 * the IO anyway, just in case.
1333b33e2cc0SMatthew Dillon 	 */
13345c8d05e2SMatthew Dillon 	if (io->modify_refs == 0 && io->modified) {
13355c8d05e2SMatthew Dillon 		hammer_ref(&io->lock);
13364a2796f3SMatthew Dillon 		hammer_io_clear_modify(io, 0);
1337250aec18SMatthew Dillon 		hammer_rel(&io->lock);
13385c8d05e2SMatthew Dillon 	} else if (io->modified) {
13395c8d05e2SMatthew Dillon 		KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
13405c8d05e2SMatthew Dillon 	}
1341f90dde4cSMatthew Dillon 
1342f90dde4cSMatthew Dillon 	/*
1343f90dde4cSMatthew Dillon 	 * The kernel is going to start the IO, set io->running.
1344f90dde4cSMatthew Dillon 	 */
1345f90dde4cSMatthew Dillon 	KKASSERT(io->running == 0);
1346f90dde4cSMatthew Dillon 	io->running = 1;
13473583bbb4SMatthew Dillon 	atomic_add_long(&io->hmp->io_running_space, io->bytes);
13483583bbb4SMatthew Dillon 	atomic_add_long(&hammer_count_io_running_write, io->bytes);
1349eddadaeeSMatthew Dillon 	TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
1350b0aab9b9SMatthew Dillon 
135177912481SMatthew Dillon 	hammer_put_interlock(&io->lock, 1);
1352b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
1353b0aab9b9SMatthew Dillon 
1354055f5ff8SMatthew Dillon 	return(0);
1355055f5ff8SMatthew Dillon }
135666325755SMatthew Dillon 
13578cd0a023SMatthew Dillon /*
135866325755SMatthew Dillon  * Return non-zero if we wish to delay the kernel's attempt to flush
135966325755SMatthew Dillon  * this buffer to disk.
1360b0aab9b9SMatthew Dillon  *
1361b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
136266325755SMatthew Dillon  */
136366325755SMatthew Dillon static int
136466325755SMatthew Dillon hammer_io_countdeps(struct buf *bp, int n)
136566325755SMatthew Dillon {
1366b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
136766325755SMatthew Dillon 	return(0);
136866325755SMatthew Dillon }
136966325755SMatthew Dillon 
1370e397030bSTomohiro Kusumi static struct bio_ops hammer_bioops = {
137166325755SMatthew Dillon 	.io_start	= hammer_io_start,
137266325755SMatthew Dillon 	.io_complete	= hammer_io_complete,
137366325755SMatthew Dillon 	.io_deallocate	= hammer_io_deallocate,
137466325755SMatthew Dillon 	.io_fsync	= hammer_io_fsync,
137566325755SMatthew Dillon 	.io_sync	= hammer_io_sync,
137666325755SMatthew Dillon 	.io_movedeps	= hammer_io_movedeps,
137766325755SMatthew Dillon 	.io_countdeps	= hammer_io_countdeps,
137866325755SMatthew Dillon 	.io_checkread	= hammer_io_checkread,
137966325755SMatthew Dillon 	.io_checkwrite	= hammer_io_checkwrite,
138066325755SMatthew Dillon };
138166325755SMatthew Dillon 
138247637bffSMatthew Dillon /************************************************************************
138347637bffSMatthew Dillon  *				DIRECT IO OPS 				*
138447637bffSMatthew Dillon  ************************************************************************
138547637bffSMatthew Dillon  *
138647637bffSMatthew Dillon  * These functions operate directly on the buffer cache buffer associated
138747637bffSMatthew Dillon  * with a front-end vnode rather then a back-end device vnode.
138847637bffSMatthew Dillon  */
138947637bffSMatthew Dillon 
139047637bffSMatthew Dillon /*
139147637bffSMatthew Dillon  * Read a buffer associated with a front-end vnode directly from the
13921b0ab2c3SMatthew Dillon  * disk media.  The bio may be issued asynchronously.  If leaf is non-NULL
13931b0ab2c3SMatthew Dillon  * we validate the CRC.
1394a99b9ea2SMatthew Dillon  *
13951b0ab2c3SMatthew Dillon  * We must check for the presence of a HAMMER buffer to handle the case
13961b0ab2c3SMatthew Dillon  * where the reblocker has rewritten the data (which it does via the HAMMER
13971b0ab2c3SMatthew Dillon  * buffer system, not via the high-level vnode buffer cache), but not yet
13981b0ab2c3SMatthew Dillon  * committed the buffer to the media.
139947637bffSMatthew Dillon  */
140047637bffSMatthew Dillon int
14011b0ab2c3SMatthew Dillon hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
14021b0ab2c3SMatthew Dillon 		      hammer_btree_leaf_elm_t leaf)
140347637bffSMatthew Dillon {
14041b0ab2c3SMatthew Dillon 	hammer_off_t buf_offset;
140547637bffSMatthew Dillon 	hammer_off_t zone2_offset;
140647637bffSMatthew Dillon 	hammer_volume_t volume;
140747637bffSMatthew Dillon 	struct buf *bp;
140847637bffSMatthew Dillon 	struct bio *nbio;
140947637bffSMatthew Dillon 	int vol_no;
141047637bffSMatthew Dillon 	int error;
141147637bffSMatthew Dillon 
14121b0ab2c3SMatthew Dillon 	buf_offset = bio->bio_offset;
1413*e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_large_data(buf_offset));
14144a2796f3SMatthew Dillon 
14151b0ab2c3SMatthew Dillon 	/*
14161b0ab2c3SMatthew Dillon 	 * The buffer cache may have an aliased buffer (the reblocker can
14171b0ab2c3SMatthew Dillon 	 * write them).  If it does we have to sync any dirty data before
14181b0ab2c3SMatthew Dillon 	 * we can build our direct-read.  This is a non-critical code path.
14191b0ab2c3SMatthew Dillon 	 */
14201b0ab2c3SMatthew Dillon 	bp = bio->bio_buf;
14211b0ab2c3SMatthew Dillon 	hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
14221b0ab2c3SMatthew Dillon 
14231b0ab2c3SMatthew Dillon 	/*
14241b0ab2c3SMatthew Dillon 	 * Resolve to a zone-2 offset.  The conversion just requires
14251b0ab2c3SMatthew Dillon 	 * munging the top 4 bits but we want to abstract it anyway
14261b0ab2c3SMatthew Dillon 	 * so the blockmap code can verify the zone assignment.
14271b0ab2c3SMatthew Dillon 	 */
14281b0ab2c3SMatthew Dillon 	zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
14291b0ab2c3SMatthew Dillon 	if (error)
14301b0ab2c3SMatthew Dillon 		goto done;
1431*e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_raw_buffer(zone2_offset));
143243c665aeSMatthew Dillon 
14331b0ab2c3SMatthew Dillon 	/*
14341b0ab2c3SMatthew Dillon 	 * Resolve volume and raw-offset for 3rd level bio.  The
14351b0ab2c3SMatthew Dillon 	 * offset will be specific to the volume.
14361b0ab2c3SMatthew Dillon 	 */
143747637bffSMatthew Dillon 	vol_no = HAMMER_VOL_DECODE(zone2_offset);
143847637bffSMatthew Dillon 	volume = hammer_get_volume(hmp, vol_no, &error);
143947637bffSMatthew Dillon 	if (error == 0 && zone2_offset >= volume->maxbuf_off)
144047637bffSMatthew Dillon 		error = EIO;
144143c665aeSMatthew Dillon 
144247637bffSMatthew Dillon 	if (error == 0) {
1443e469566bSMatthew Dillon 		/*
144465d9d14fSTomohiro Kusumi 		 * 3rd level bio (the caller has already pushed once)
1445e469566bSMatthew Dillon 		 */
144647637bffSMatthew Dillon 		nbio = push_bio(bio);
1447516655e8STomohiro Kusumi 		nbio->bio_offset = hammer_xlate_to_phys(volume->ondisk,
1448516655e8STomohiro Kusumi 							zone2_offset);
1449ce0138a6SMatthew Dillon 		hammer_stats_disk_read += bp->b_bufsize;
145047637bffSMatthew Dillon 		vn_strategy(volume->devvp, nbio);
145147637bffSMatthew Dillon 	}
145247637bffSMatthew Dillon 	hammer_rel_volume(volume, 0);
14531b0ab2c3SMatthew Dillon done:
145447637bffSMatthew Dillon 	if (error) {
145511605a5cSTomohiro Kusumi 		hdkprintf("failed @ %016llx\n", (long long)zone2_offset);
145647637bffSMatthew Dillon 		bp->b_error = error;
145747637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
145847637bffSMatthew Dillon 		biodone(bio);
145947637bffSMatthew Dillon 	}
146047637bffSMatthew Dillon 	return(error);
146147637bffSMatthew Dillon }
146247637bffSMatthew Dillon 
14639a98f3ccSMatthew Dillon /*
14649a98f3ccSMatthew Dillon  * This works similarly to hammer_io_direct_read() except instead of
14659a98f3ccSMatthew Dillon  * directly reading from the device into the bio we instead indirectly
14669a98f3ccSMatthew Dillon  * read through the device's buffer cache and then copy the data into
14679a98f3ccSMatthew Dillon  * the bio.
14689a98f3ccSMatthew Dillon  *
14699a98f3ccSMatthew Dillon  * If leaf is non-NULL and validation is enabled, the CRC will be checked.
14709a98f3ccSMatthew Dillon  *
14719a98f3ccSMatthew Dillon  * This routine also executes asynchronously.  It allows hammer strategy
14729a98f3ccSMatthew Dillon  * calls to operate asynchronously when in double_buffer mode (in addition
14739a98f3ccSMatthew Dillon  * to operating asynchronously when in normal mode).
14749a98f3ccSMatthew Dillon  */
14759a98f3ccSMatthew Dillon int
14769a98f3ccSMatthew Dillon hammer_io_indirect_read(hammer_mount_t hmp, struct bio *bio,
14779a98f3ccSMatthew Dillon 			hammer_btree_leaf_elm_t leaf)
14789a98f3ccSMatthew Dillon {
14799a98f3ccSMatthew Dillon 	hammer_off_t buf_offset;
14809a98f3ccSMatthew Dillon 	hammer_off_t zone2_offset;
14819a98f3ccSMatthew Dillon 	hammer_volume_t volume;
14829a98f3ccSMatthew Dillon 	struct buf *bp;
14839a98f3ccSMatthew Dillon 	int vol_no;
14849a98f3ccSMatthew Dillon 	int error;
14859a98f3ccSMatthew Dillon 
14869a98f3ccSMatthew Dillon 	buf_offset = bio->bio_offset;
1487*e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_large_data(buf_offset));
14889a98f3ccSMatthew Dillon 
14899a98f3ccSMatthew Dillon 	/*
14909a98f3ccSMatthew Dillon 	 * The buffer cache may have an aliased buffer (the reblocker can
14919a98f3ccSMatthew Dillon 	 * write them).  If it does we have to sync any dirty data before
14929a98f3ccSMatthew Dillon 	 * we can build our direct-read.  This is a non-critical code path.
14939a98f3ccSMatthew Dillon 	 */
14949a98f3ccSMatthew Dillon 	bp = bio->bio_buf;
14959a98f3ccSMatthew Dillon 	hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
14969a98f3ccSMatthew Dillon 
14979a98f3ccSMatthew Dillon 	/*
14989a98f3ccSMatthew Dillon 	 * Resolve to a zone-2 offset.  The conversion just requires
14999a98f3ccSMatthew Dillon 	 * munging the top 4 bits but we want to abstract it anyway
15009a98f3ccSMatthew Dillon 	 * so the blockmap code can verify the zone assignment.
15019a98f3ccSMatthew Dillon 	 */
15029a98f3ccSMatthew Dillon 	zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
15039a98f3ccSMatthew Dillon 	if (error)
15049a98f3ccSMatthew Dillon 		goto done;
1505*e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_raw_buffer(zone2_offset));
15069a98f3ccSMatthew Dillon 
15079a98f3ccSMatthew Dillon 	/*
15089a98f3ccSMatthew Dillon 	 * Resolve volume and raw-offset for 3rd level bio.  The
15099a98f3ccSMatthew Dillon 	 * offset will be specific to the volume.
15109a98f3ccSMatthew Dillon 	 */
15119a98f3ccSMatthew Dillon 	vol_no = HAMMER_VOL_DECODE(zone2_offset);
15129a98f3ccSMatthew Dillon 	volume = hammer_get_volume(hmp, vol_no, &error);
15139a98f3ccSMatthew Dillon 	if (error == 0 && zone2_offset >= volume->maxbuf_off)
15149a98f3ccSMatthew Dillon 		error = EIO;
15159a98f3ccSMatthew Dillon 
15169a98f3ccSMatthew Dillon 	if (error == 0) {
15179a98f3ccSMatthew Dillon 		/*
15189a98f3ccSMatthew Dillon 		 * Convert to the raw volume->devvp offset and acquire
15199a98f3ccSMatthew Dillon 		 * the buf, issuing async I/O if necessary.
15209a98f3ccSMatthew Dillon 		 */
1521516655e8STomohiro Kusumi 		buf_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset);
15229a98f3ccSMatthew Dillon 
15239a98f3ccSMatthew Dillon 		if (leaf && hammer_verify_data) {
15249a98f3ccSMatthew Dillon 			bio->bio_caller_info1.uvalue32 = leaf->data_crc;
15259a98f3ccSMatthew Dillon 			bio->bio_caller_info2.index = 1;
15269a98f3ccSMatthew Dillon 		} else {
15279a98f3ccSMatthew Dillon 			bio->bio_caller_info2.index = 0;
15289a98f3ccSMatthew Dillon 		}
15299a98f3ccSMatthew Dillon 		breadcb(volume->devvp, buf_offset, bp->b_bufsize,
15309a98f3ccSMatthew Dillon 			hammer_indirect_callback, bio);
15319a98f3ccSMatthew Dillon 	}
15329a98f3ccSMatthew Dillon 	hammer_rel_volume(volume, 0);
15339a98f3ccSMatthew Dillon done:
15349a98f3ccSMatthew Dillon 	if (error) {
153511605a5cSTomohiro Kusumi 		hdkprintf("failed @ %016llx\n", (long long)zone2_offset);
15369a98f3ccSMatthew Dillon 		bp->b_error = error;
15379a98f3ccSMatthew Dillon 		bp->b_flags |= B_ERROR;
15389a98f3ccSMatthew Dillon 		biodone(bio);
15399a98f3ccSMatthew Dillon 	}
15409a98f3ccSMatthew Dillon 	return(error);
15419a98f3ccSMatthew Dillon }
15429a98f3ccSMatthew Dillon 
15439a98f3ccSMatthew Dillon /*
15449a98f3ccSMatthew Dillon  * Indirect callback on completion.  bio/bp specify the device-backed
15459a98f3ccSMatthew Dillon  * buffer.  bio->bio_caller_info1.ptr holds obio.
15469a98f3ccSMatthew Dillon  *
15479a98f3ccSMatthew Dillon  * obio/obp is the original regular file buffer.  obio->bio_caller_info*
15489a98f3ccSMatthew Dillon  * contains the crc specification.
15499a98f3ccSMatthew Dillon  *
15509a98f3ccSMatthew Dillon  * We are responsible for calling bpdone() and bqrelse() on bio/bp, and
15519a98f3ccSMatthew Dillon  * for calling biodone() on obio.
15529a98f3ccSMatthew Dillon  */
15539a98f3ccSMatthew Dillon static void
15549a98f3ccSMatthew Dillon hammer_indirect_callback(struct bio *bio)
15559a98f3ccSMatthew Dillon {
15569a98f3ccSMatthew Dillon 	struct buf *bp = bio->bio_buf;
15579a98f3ccSMatthew Dillon 	struct buf *obp;
15589a98f3ccSMatthew Dillon 	struct bio *obio;
15599a98f3ccSMatthew Dillon 
15609a98f3ccSMatthew Dillon 	/*
15619a98f3ccSMatthew Dillon 	 * If BIO_DONE is already set the device buffer was already
15629a98f3ccSMatthew Dillon 	 * fully valid (B_CACHE).  If it is not set then I/O was issued
15639a98f3ccSMatthew Dillon 	 * and we have to run I/O completion as the last bio.
15649a98f3ccSMatthew Dillon 	 *
15659a98f3ccSMatthew Dillon 	 * Nobody is waiting for our device I/O to complete, we are
15669a98f3ccSMatthew Dillon 	 * responsible for bqrelse()ing it which means we also have to do
15679a98f3ccSMatthew Dillon 	 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
15689a98f3ccSMatthew Dillon 	 * may have set).
15699a98f3ccSMatthew Dillon 	 *
15709a98f3ccSMatthew Dillon 	 * Any preexisting device buffer should match the requested size,
1571a981af19STomohiro Kusumi 	 * but due to big-block recycling and other factors there is some
15729a98f3ccSMatthew Dillon 	 * fragility there, so we assert that the device buffer covers
15739a98f3ccSMatthew Dillon 	 * the request.
15749a98f3ccSMatthew Dillon 	 */
15759a98f3ccSMatthew Dillon 	if ((bio->bio_flags & BIO_DONE) == 0)
15769a98f3ccSMatthew Dillon 		bpdone(bp, 0);
15779a98f3ccSMatthew Dillon 	bio->bio_flags &= ~(BIO_DONE | BIO_SYNC);
15789a98f3ccSMatthew Dillon 
15799a98f3ccSMatthew Dillon 	obio = bio->bio_caller_info1.ptr;
15809a98f3ccSMatthew Dillon 	obp = obio->bio_buf;
15819a98f3ccSMatthew Dillon 
15829a98f3ccSMatthew Dillon 	if (bp->b_flags & B_ERROR) {
15839a98f3ccSMatthew Dillon 		obp->b_flags |= B_ERROR;
15849a98f3ccSMatthew Dillon 		obp->b_error = bp->b_error;
15859a98f3ccSMatthew Dillon 	} else if (obio->bio_caller_info2.index &&
15869a98f3ccSMatthew Dillon 		   obio->bio_caller_info1.uvalue32 !=
15879a98f3ccSMatthew Dillon 		    crc32(bp->b_data, bp->b_bufsize)) {
15889a98f3ccSMatthew Dillon 		obp->b_flags |= B_ERROR;
15899a98f3ccSMatthew Dillon 		obp->b_error = EIO;
15909a98f3ccSMatthew Dillon 	} else {
15919a98f3ccSMatthew Dillon 		KKASSERT(bp->b_bufsize >= obp->b_bufsize);
15929a98f3ccSMatthew Dillon 		bcopy(bp->b_data, obp->b_data, obp->b_bufsize);
15939a98f3ccSMatthew Dillon 		obp->b_resid = 0;
15949a98f3ccSMatthew Dillon 		obp->b_flags |= B_AGE;
15959a98f3ccSMatthew Dillon 	}
15969a98f3ccSMatthew Dillon 	biodone(obio);
15979a98f3ccSMatthew Dillon 	bqrelse(bp);
15989a98f3ccSMatthew Dillon }
15999a98f3ccSMatthew Dillon 
160047637bffSMatthew Dillon /*
160147637bffSMatthew Dillon  * Write a buffer associated with a front-end vnode directly to the
160247637bffSMatthew Dillon  * disk media.  The bio may be issued asynchronously.
16031b0ab2c3SMatthew Dillon  *
160477912481SMatthew Dillon  * The BIO is associated with the specified record and RECG_DIRECT_IO
1605e469566bSMatthew Dillon  * is set.  The recorded is added to its object.
160647637bffSMatthew Dillon  */
160747637bffSMatthew Dillon int
16086362a262SMatthew Dillon hammer_io_direct_write(hammer_mount_t hmp, struct bio *bio,
16096362a262SMatthew Dillon 		       hammer_record_t record)
161047637bffSMatthew Dillon {
16111b0ab2c3SMatthew Dillon 	hammer_btree_leaf_elm_t leaf = &record->leaf;
16120832c9bbSMatthew Dillon 	hammer_off_t buf_offset;
161347637bffSMatthew Dillon 	hammer_off_t zone2_offset;
161447637bffSMatthew Dillon 	hammer_volume_t volume;
16150832c9bbSMatthew Dillon 	hammer_buffer_t buffer;
161647637bffSMatthew Dillon 	struct buf *bp;
161747637bffSMatthew Dillon 	struct bio *nbio;
16180832c9bbSMatthew Dillon 	char *ptr;
161947637bffSMatthew Dillon 	int vol_no;
162047637bffSMatthew Dillon 	int error;
162147637bffSMatthew Dillon 
16220832c9bbSMatthew Dillon 	buf_offset = leaf->data_offset;
16230832c9bbSMatthew Dillon 
1624b0cce327STomohiro Kusumi 	KKASSERT(hammer_is_zone2_mapped_index(
1625b0cce327STomohiro Kusumi 		HAMMER_ZONE_DECODE(buf_offset)));
162647637bffSMatthew Dillon 	KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
162747637bffSMatthew Dillon 
16286362a262SMatthew Dillon 	/*
16296362a262SMatthew Dillon 	 * Issue or execute the I/O.  The new memory record must replace
16306362a262SMatthew Dillon 	 * the old one before the I/O completes, otherwise a reaquisition of
16316362a262SMatthew Dillon 	 * the buffer will load the old media data instead of the new.
16326362a262SMatthew Dillon 	 */
16330832c9bbSMatthew Dillon 	if ((buf_offset & HAMMER_BUFMASK) == 0 &&
16344a2796f3SMatthew Dillon 	    leaf->data_len >= HAMMER_BUFSIZE) {
16350832c9bbSMatthew Dillon 		/*
16360832c9bbSMatthew Dillon 		 * We are using the vnode's bio to write directly to the
16370832c9bbSMatthew Dillon 		 * media, any hammer_buffer at the same zone-X offset will
16380832c9bbSMatthew Dillon 		 * now have stale data.
16390832c9bbSMatthew Dillon 		 */
16400832c9bbSMatthew Dillon 		zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
164147637bffSMatthew Dillon 		vol_no = HAMMER_VOL_DECODE(zone2_offset);
164247637bffSMatthew Dillon 		volume = hammer_get_volume(hmp, vol_no, &error);
164347637bffSMatthew Dillon 
164447637bffSMatthew Dillon 		if (error == 0 && zone2_offset >= volume->maxbuf_off)
164547637bffSMatthew Dillon 			error = EIO;
164647637bffSMatthew Dillon 		if (error == 0) {
16470832c9bbSMatthew Dillon 			bp = bio->bio_buf;
16484a2796f3SMatthew Dillon 			KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1649e469566bSMatthew Dillon 			/*
16504a2796f3SMatthew Dillon 			hammer_del_buffers(hmp, buf_offset,
16514a2796f3SMatthew Dillon 					   zone2_offset, bp->b_bufsize);
1652e469566bSMatthew Dillon 			*/
16531b0ab2c3SMatthew Dillon 
165443c665aeSMatthew Dillon 			/*
165543c665aeSMatthew Dillon 			 * Second level bio - cached zone2 offset.
16561b0ab2c3SMatthew Dillon 			 *
16571b0ab2c3SMatthew Dillon 			 * (We can put our bio_done function in either the
16581b0ab2c3SMatthew Dillon 			 *  2nd or 3rd level).
165943c665aeSMatthew Dillon 			 */
166047637bffSMatthew Dillon 			nbio = push_bio(bio);
166143c665aeSMatthew Dillon 			nbio->bio_offset = zone2_offset;
16621b0ab2c3SMatthew Dillon 			nbio->bio_done = hammer_io_direct_write_complete;
16631b0ab2c3SMatthew Dillon 			nbio->bio_caller_info1.ptr = record;
1664e469566bSMatthew Dillon 			record->zone2_offset = zone2_offset;
166577912481SMatthew Dillon 			record->gflags |= HAMMER_RECG_DIRECT_IO |
166677912481SMatthew Dillon 					 HAMMER_RECG_DIRECT_INVAL;
166743c665aeSMatthew Dillon 
166843c665aeSMatthew Dillon 			/*
166943c665aeSMatthew Dillon 			 * Third level bio - raw offset specific to the
167043c665aeSMatthew Dillon 			 * correct volume.
167143c665aeSMatthew Dillon 			 */
167243c665aeSMatthew Dillon 			nbio = push_bio(nbio);
1673516655e8STomohiro Kusumi 			nbio->bio_offset = hammer_xlate_to_phys(volume->ondisk,
1674516655e8STomohiro Kusumi 								zone2_offset);
1675ce0138a6SMatthew Dillon 			hammer_stats_disk_write += bp->b_bufsize;
16766362a262SMatthew Dillon 			hammer_ip_replace_bulk(hmp, record);
167747637bffSMatthew Dillon 			vn_strategy(volume->devvp, nbio);
1678748efb59SMatthew Dillon 			hammer_io_flush_mark(volume);
167947637bffSMatthew Dillon 		}
168047637bffSMatthew Dillon 		hammer_rel_volume(volume, 0);
16810832c9bbSMatthew Dillon 	} else {
16821b0ab2c3SMatthew Dillon 		/*
16831b0ab2c3SMatthew Dillon 		 * Must fit in a standard HAMMER buffer.  In this case all
168477912481SMatthew Dillon 		 * consumers use the HAMMER buffer system and RECG_DIRECT_IO
16851b0ab2c3SMatthew Dillon 		 * does not need to be set-up.
16861b0ab2c3SMatthew Dillon 		 */
16870832c9bbSMatthew Dillon 		KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
16880832c9bbSMatthew Dillon 		buffer = NULL;
16890832c9bbSMatthew Dillon 		ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
16900832c9bbSMatthew Dillon 		if (error == 0) {
16910832c9bbSMatthew Dillon 			bp = bio->bio_buf;
16927bc5b8c2SMatthew Dillon 			bp->b_flags |= B_AGE;
16930832c9bbSMatthew Dillon 			hammer_io_modify(&buffer->io, 1);
16940832c9bbSMatthew Dillon 			bcopy(bp->b_data, ptr, leaf->data_len);
16950832c9bbSMatthew Dillon 			hammer_io_modify_done(&buffer->io);
16967bc5b8c2SMatthew Dillon 			hammer_rel_buffer(buffer, 0);
16970832c9bbSMatthew Dillon 			bp->b_resid = 0;
16986362a262SMatthew Dillon 			hammer_ip_replace_bulk(hmp, record);
16990832c9bbSMatthew Dillon 			biodone(bio);
17000832c9bbSMatthew Dillon 		}
170147637bffSMatthew Dillon 	}
17026362a262SMatthew Dillon 	if (error) {
1703e469566bSMatthew Dillon 		/*
17046362a262SMatthew Dillon 		 * Major suckage occured.  Also note:  The record was
17056362a262SMatthew Dillon 		 * never added to the tree so we do not have to worry
17066362a262SMatthew Dillon 		 * about the backend.
1707e469566bSMatthew Dillon 		 */
170811605a5cSTomohiro Kusumi 		hdkprintf("failed @ %016llx\n", (long long)leaf->data_offset);
170947637bffSMatthew Dillon 		bp = bio->bio_buf;
171047637bffSMatthew Dillon 		bp->b_resid = 0;
171147637bffSMatthew Dillon 		bp->b_error = EIO;
171247637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
171347637bffSMatthew Dillon 		biodone(bio);
1714e469566bSMatthew Dillon 		record->flags |= HAMMER_RECF_DELETED_FE;
1715e469566bSMatthew Dillon 		hammer_rel_mem_record(record);
171647637bffSMatthew Dillon 	}
171747637bffSMatthew Dillon 	return(error);
171847637bffSMatthew Dillon }
171947637bffSMatthew Dillon 
172043c665aeSMatthew Dillon /*
17211b0ab2c3SMatthew Dillon  * On completion of the BIO this callback must disconnect
17221b0ab2c3SMatthew Dillon  * it from the hammer_record and chain to the previous bio.
1723cdb6e4e6SMatthew Dillon  *
1724cdb6e4e6SMatthew Dillon  * An I/O error forces the mount to read-only.  Data buffers
1725cdb6e4e6SMatthew Dillon  * are not B_LOCKED like meta-data buffers are, so we have to
1726cdb6e4e6SMatthew Dillon  * throw the buffer away to prevent the kernel from retrying.
172777912481SMatthew Dillon  *
172877912481SMatthew Dillon  * NOTE: MPSAFE callback, only modify fields we have explicit
172977912481SMatthew Dillon  *	 access to (the bp and the record->gflags).
17301b0ab2c3SMatthew Dillon  */
17311b0ab2c3SMatthew Dillon static
17321b0ab2c3SMatthew Dillon void
17331b0ab2c3SMatthew Dillon hammer_io_direct_write_complete(struct bio *nbio)
17341b0ab2c3SMatthew Dillon {
17351b0ab2c3SMatthew Dillon 	struct bio *obio;
1736e469566bSMatthew Dillon 	struct buf *bp;
1737b0aab9b9SMatthew Dillon 	hammer_record_t record;
1738b0aab9b9SMatthew Dillon 	hammer_mount_t hmp;
1739b0aab9b9SMatthew Dillon 
1740b0aab9b9SMatthew Dillon 	record = nbio->bio_caller_info1.ptr;
1741b0aab9b9SMatthew Dillon 	KKASSERT(record != NULL);
1742b0aab9b9SMatthew Dillon 	hmp = record->ip->hmp;
1743b0aab9b9SMatthew Dillon 
1744b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
17451b0ab2c3SMatthew Dillon 
1746e469566bSMatthew Dillon 	bp = nbio->bio_buf;
17471b0ab2c3SMatthew Dillon 	obio = pop_bio(nbio);
1748e469566bSMatthew Dillon 	if (bp->b_flags & B_ERROR) {
174977912481SMatthew Dillon 		lwkt_gettoken(&hmp->fs_token);
1750653fa4cdSTomohiro Kusumi 		hammer_critical_error(hmp, record->ip, bp->b_error,
1751cdb6e4e6SMatthew Dillon 				      "while writing bulk data");
175277912481SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
1753e469566bSMatthew Dillon 		bp->b_flags |= B_INVAL;
1754cdb6e4e6SMatthew Dillon 	}
17551b0ab2c3SMatthew Dillon 	biodone(obio);
1756e469566bSMatthew Dillon 
175777912481SMatthew Dillon 	KKASSERT(record->gflags & HAMMER_RECG_DIRECT_IO);
175877912481SMatthew Dillon 	if (record->gflags & HAMMER_RECG_DIRECT_WAIT) {
175977912481SMatthew Dillon 		record->gflags &= ~(HAMMER_RECG_DIRECT_IO |
176077912481SMatthew Dillon 				    HAMMER_RECG_DIRECT_WAIT);
1761de996e86SMatthew Dillon 		/* record can disappear once DIRECT_IO flag is cleared */
17621b0ab2c3SMatthew Dillon 		wakeup(&record->flags);
1763de996e86SMatthew Dillon 	} else {
176477912481SMatthew Dillon 		record->gflags &= ~HAMMER_RECG_DIRECT_IO;
1765de996e86SMatthew Dillon 		/* record can disappear once DIRECT_IO flag is cleared */
17661b0ab2c3SMatthew Dillon 	}
1767b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
17681b0ab2c3SMatthew Dillon }
17691b0ab2c3SMatthew Dillon 
17701b0ab2c3SMatthew Dillon 
17711b0ab2c3SMatthew Dillon /*
17721b0ab2c3SMatthew Dillon  * This is called before a record is either committed to the B-Tree
1773e469566bSMatthew Dillon  * or destroyed, to resolve any associated direct-IO.
17741b0ab2c3SMatthew Dillon  *
1775e469566bSMatthew Dillon  * (1) We must wait for any direct-IO related to the record to complete.
1776e469566bSMatthew Dillon  *
1777e469566bSMatthew Dillon  * (2) We must remove any buffer cache aliases for data accessed via
1778e469566bSMatthew Dillon  *     leaf->data_offset or zone2_offset so non-direct-IO consumers
1779e469566bSMatthew Dillon  *     (the mirroring and reblocking code) do not see stale data.
17801b0ab2c3SMatthew Dillon  */
17811b0ab2c3SMatthew Dillon void
17821b0ab2c3SMatthew Dillon hammer_io_direct_wait(hammer_record_t record)
17831b0ab2c3SMatthew Dillon {
1784b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = record->ip->hmp;
1785b0aab9b9SMatthew Dillon 
1786e469566bSMatthew Dillon 	/*
1787e469566bSMatthew Dillon 	 * Wait for I/O to complete
1788e469566bSMatthew Dillon 	 */
178977912481SMatthew Dillon 	if (record->gflags & HAMMER_RECG_DIRECT_IO) {
1790b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
179177912481SMatthew Dillon 		while (record->gflags & HAMMER_RECG_DIRECT_IO) {
179277912481SMatthew Dillon 			record->gflags |= HAMMER_RECG_DIRECT_WAIT;
17931b0ab2c3SMatthew Dillon 			tsleep(&record->flags, 0, "hmdiow", 0);
17941b0ab2c3SMatthew Dillon 		}
1795b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
17961b0ab2c3SMatthew Dillon 	}
17971b0ab2c3SMatthew Dillon 
17981b0ab2c3SMatthew Dillon 	/*
1799362ec2dcSMatthew Dillon 	 * Invalidate any related buffer cache aliases associated with the
1800362ec2dcSMatthew Dillon 	 * backing device.  This is needed because the buffer cache buffer
1801362ec2dcSMatthew Dillon 	 * for file data is associated with the file vnode, not the backing
1802362ec2dcSMatthew Dillon 	 * device vnode.
1803362ec2dcSMatthew Dillon 	 *
1804362ec2dcSMatthew Dillon 	 * XXX I do not think this case can occur any more now that
1805362ec2dcSMatthew Dillon 	 * reservations ensure that all such buffers are removed before
1806362ec2dcSMatthew Dillon 	 * an area can be reused.
1807e469566bSMatthew Dillon 	 */
180877912481SMatthew Dillon 	if (record->gflags & HAMMER_RECG_DIRECT_INVAL) {
1809e469566bSMatthew Dillon 		KKASSERT(record->leaf.data_offset);
1810b0aab9b9SMatthew Dillon 		hammer_del_buffers(hmp, record->leaf.data_offset,
1811362ec2dcSMatthew Dillon 				   record->zone2_offset, record->leaf.data_len,
1812362ec2dcSMatthew Dillon 				   1);
181377912481SMatthew Dillon 		record->gflags &= ~HAMMER_RECG_DIRECT_INVAL;
1814e469566bSMatthew Dillon 	}
1815e469566bSMatthew Dillon }
1816e469566bSMatthew Dillon 
1817e469566bSMatthew Dillon /*
181843c665aeSMatthew Dillon  * This is called to remove the second-level cached zone-2 offset from
181943c665aeSMatthew Dillon  * frontend buffer cache buffers, now stale due to a data relocation.
182043c665aeSMatthew Dillon  * These offsets are generated by cluster_read() via VOP_BMAP, or directly
182143c665aeSMatthew Dillon  * by hammer_vop_strategy_read().
182243c665aeSMatthew Dillon  *
182343c665aeSMatthew Dillon  * This is rather nasty because here we have something like the reblocker
182443c665aeSMatthew Dillon  * scanning the raw B-Tree with no held references on anything, really,
182543c665aeSMatthew Dillon  * other then a shared lock on the B-Tree node, and we have to access the
182643c665aeSMatthew Dillon  * frontend's buffer cache to check for and clean out the association.
182743c665aeSMatthew Dillon  * Specifically, if the reblocker is moving data on the disk, these cached
182843c665aeSMatthew Dillon  * offsets will become invalid.
182943c665aeSMatthew Dillon  *
183043c665aeSMatthew Dillon  * Only data record types associated with the large-data zone are subject
183143c665aeSMatthew Dillon  * to direct-io and need to be checked.
183243c665aeSMatthew Dillon  *
183343c665aeSMatthew Dillon  */
183443c665aeSMatthew Dillon void
183543c665aeSMatthew Dillon hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
183643c665aeSMatthew Dillon {
183743c665aeSMatthew Dillon 	struct hammer_inode_info iinfo;
183843c665aeSMatthew Dillon 	int zone;
183943c665aeSMatthew Dillon 
184043c665aeSMatthew Dillon 	if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
184143c665aeSMatthew Dillon 		return;
184243c665aeSMatthew Dillon 	zone = HAMMER_ZONE_DECODE(leaf->data_offset);
184343c665aeSMatthew Dillon 	if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
184443c665aeSMatthew Dillon 		return;
184543c665aeSMatthew Dillon 	iinfo.obj_id = leaf->base.obj_id;
184643c665aeSMatthew Dillon 	iinfo.obj_asof = 0;	/* unused */
184743c665aeSMatthew Dillon 	iinfo.obj_localization = leaf->base.localization &
18485a930e66SMatthew Dillon 				 HAMMER_LOCALIZE_PSEUDOFS_MASK;
184943c665aeSMatthew Dillon 	iinfo.u.leaf = leaf;
185043c665aeSMatthew Dillon 	hammer_scan_inode_snapshots(hmp, &iinfo,
185143c665aeSMatthew Dillon 				    hammer_io_direct_uncache_callback,
185243c665aeSMatthew Dillon 				    leaf);
185343c665aeSMatthew Dillon }
185443c665aeSMatthew Dillon 
185543c665aeSMatthew Dillon static int
185643c665aeSMatthew Dillon hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
185743c665aeSMatthew Dillon {
185843c665aeSMatthew Dillon 	hammer_inode_info_t iinfo = data;
185943c665aeSMatthew Dillon 	hammer_off_t file_offset;
186043c665aeSMatthew Dillon 	struct vnode *vp;
186143c665aeSMatthew Dillon 	struct buf *bp;
186243c665aeSMatthew Dillon 	int blksize;
186343c665aeSMatthew Dillon 
186443c665aeSMatthew Dillon 	if (ip->vp == NULL)
186543c665aeSMatthew Dillon 		return(0);
186643c665aeSMatthew Dillon 	file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
186743c665aeSMatthew Dillon 	blksize = iinfo->u.leaf->data_len;
186843c665aeSMatthew Dillon 	KKASSERT((blksize & HAMMER_BUFMASK) == 0);
186943c665aeSMatthew Dillon 
18709c90dba2SMatthew Dillon 	/*
18719c90dba2SMatthew Dillon 	 * Warning: FINDBLK_TEST return stable storage but not stable
18729c90dba2SMatthew Dillon 	 *	    contents.  It happens to be ok in this case.
18739c90dba2SMatthew Dillon 	 */
187443c665aeSMatthew Dillon 	hammer_ref(&ip->lock);
187543c665aeSMatthew Dillon 	if (hammer_get_vnode(ip, &vp) == 0) {
1876b1c20cfaSMatthew Dillon 		if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
187743c665aeSMatthew Dillon 		    bp->b_bio2.bio_offset != NOOFFSET) {
187843c665aeSMatthew Dillon 			bp = getblk(ip->vp, file_offset, blksize, 0, 0);
187943c665aeSMatthew Dillon 			bp->b_bio2.bio_offset = NOOFFSET;
188043c665aeSMatthew Dillon 			brelse(bp);
188143c665aeSMatthew Dillon 		}
188243c665aeSMatthew Dillon 		vput(vp);
188343c665aeSMatthew Dillon 	}
188443c665aeSMatthew Dillon 	hammer_rel_inode(ip, 0);
188543c665aeSMatthew Dillon 	return(0);
188643c665aeSMatthew Dillon }
188747637bffSMatthew Dillon 
1888748efb59SMatthew Dillon 
1889748efb59SMatthew Dillon /*
1890748efb59SMatthew Dillon  * This function is called when writes may have occured on the volume,
1891748efb59SMatthew Dillon  * indicating that the device may be holding cached writes.
1892748efb59SMatthew Dillon  */
18939e6939a5STomohiro Kusumi static __inline void
1894748efb59SMatthew Dillon hammer_io_flush_mark(hammer_volume_t volume)
1895748efb59SMatthew Dillon {
189677912481SMatthew Dillon 	atomic_set_int(&volume->vol_flags, HAMMER_VOLF_NEEDFLUSH);
1897748efb59SMatthew Dillon }
1898748efb59SMatthew Dillon 
1899748efb59SMatthew Dillon /*
1900748efb59SMatthew Dillon  * This function ensures that the device has flushed any cached writes out.
1901748efb59SMatthew Dillon  */
1902748efb59SMatthew Dillon void
1903748efb59SMatthew Dillon hammer_io_flush_sync(hammer_mount_t hmp)
1904748efb59SMatthew Dillon {
1905748efb59SMatthew Dillon 	hammer_volume_t volume;
1906748efb59SMatthew Dillon 	struct buf *bp_base = NULL;
1907748efb59SMatthew Dillon 	struct buf *bp;
1908748efb59SMatthew Dillon 
1909748efb59SMatthew Dillon 	RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1910748efb59SMatthew Dillon 		if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
191177912481SMatthew Dillon 			atomic_clear_int(&volume->vol_flags,
191277912481SMatthew Dillon 					 HAMMER_VOLF_NEEDFLUSH);
1913748efb59SMatthew Dillon 			bp = getpbuf(NULL);
1914748efb59SMatthew Dillon 			bp->b_bio1.bio_offset = 0;
1915748efb59SMatthew Dillon 			bp->b_bufsize = 0;
1916748efb59SMatthew Dillon 			bp->b_bcount = 0;
1917748efb59SMatthew Dillon 			bp->b_cmd = BUF_CMD_FLUSH;
1918748efb59SMatthew Dillon 			bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1919ae8e83e6SMatthew Dillon 			bp->b_bio1.bio_done = biodone_sync;
1920ae8e83e6SMatthew Dillon 			bp->b_bio1.bio_flags |= BIO_SYNC;
1921748efb59SMatthew Dillon 			bp_base = bp;
1922748efb59SMatthew Dillon 			vn_strategy(volume->devvp, &bp->b_bio1);
1923748efb59SMatthew Dillon 		}
1924748efb59SMatthew Dillon 	}
1925748efb59SMatthew Dillon 	while ((bp = bp_base) != NULL) {
1926748efb59SMatthew Dillon 		bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1927ae8e83e6SMatthew Dillon 		biowait(&bp->b_bio1, "hmrFLS");
1928748efb59SMatthew Dillon 		relpbuf(bp, NULL);
1929748efb59SMatthew Dillon 	}
1930748efb59SMatthew Dillon }
1931ba298df1SMatthew Dillon 
1932ba298df1SMatthew Dillon /*
1933ba298df1SMatthew Dillon  * Limit the amount of backlog which we allow to build up
1934ba298df1SMatthew Dillon  */
1935ba298df1SMatthew Dillon void
1936ba298df1SMatthew Dillon hammer_io_limit_backlog(hammer_mount_t hmp)
1937ba298df1SMatthew Dillon {
19383038a8caSMatthew Dillon 	waitrunningbufspace();
1939ba298df1SMatthew Dillon }
1940