xref: /dflybsd-src/sys/vfs/hammer/hammer_io.c (revision 374a548a53b60d032d0c5bc632d64ac100c235f3)
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);
606b482339STomohiro Kusumi static void hammer_io_set_modlist(hammer_io_t 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
hammer_mod_rb_compare(hammer_io_t io1,hammer_io_t io2)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
hammer_io_init(hammer_io_t io,hammer_volume_t volume,hammer_io_type_t type)917251986aSTomohiro Kusumi hammer_io_init(hammer_io_t io, hammer_volume_t volume, hammer_io_type_t type)
92055f5ff8SMatthew Dillon {
93748efb59SMatthew Dillon 	io->volume = volume;
94748efb59SMatthew Dillon 	io->hmp = volume->io.hmp;
95055f5ff8SMatthew Dillon 	io->type = type;
96055f5ff8SMatthew Dillon }
97055f5ff8SMatthew Dillon 
984fcdc8b4STomohiro Kusumi hammer_io_type_t
hammer_zone_to_iotype(int zone)994fcdc8b4STomohiro Kusumi hammer_zone_to_iotype(int zone)
1004fcdc8b4STomohiro Kusumi {
1014fcdc8b4STomohiro Kusumi 	hammer_io_type_t iotype;
1024fcdc8b4STomohiro Kusumi 
1034fcdc8b4STomohiro Kusumi 	switch(zone) {
1044fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_RAW_VOLUME_INDEX:
1057fb33ff0STomohiro Kusumi 		iotype = HAMMER_IOTYPE_VOLUME;
1064fcdc8b4STomohiro Kusumi 		break;
1074fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_RAW_BUFFER_INDEX:
1084fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_FREEMAP_INDEX:
1094fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_BTREE_INDEX:
1104fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_META_INDEX:
1117fb33ff0STomohiro Kusumi 		iotype = HAMMER_IOTYPE_META_BUFFER;
1124fcdc8b4STomohiro Kusumi 		break;
1134fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_UNDO_INDEX:
1147fb33ff0STomohiro Kusumi 		iotype = HAMMER_IOTYPE_UNDO_BUFFER;
1154fcdc8b4STomohiro Kusumi 		break;
1164fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_LARGE_DATA_INDEX:
1174fcdc8b4STomohiro Kusumi 	case HAMMER_ZONE_SMALL_DATA_INDEX:
1187fb33ff0STomohiro Kusumi 		iotype = HAMMER_IOTYPE_DATA_BUFFER;
1194fcdc8b4STomohiro Kusumi 		break;
1204fcdc8b4STomohiro Kusumi 	default:
1217fb33ff0STomohiro Kusumi 		iotype = HAMMER_IOTYPE_DUMMY;
1224fcdc8b4STomohiro Kusumi 		break;
1234fcdc8b4STomohiro Kusumi 	}
1244fcdc8b4STomohiro Kusumi 
1254fcdc8b4STomohiro Kusumi 	return(iotype);
1264fcdc8b4STomohiro Kusumi }
1274fcdc8b4STomohiro Kusumi 
1284fcdc8b4STomohiro Kusumi static const char*
hammer_io_to_iostring(hammer_io_t io)1294fcdc8b4STomohiro Kusumi hammer_io_to_iostring(hammer_io_t io)
1304fcdc8b4STomohiro Kusumi {
1314fcdc8b4STomohiro Kusumi 	const char *iostr = NULL;
1324fcdc8b4STomohiro Kusumi 
1334fcdc8b4STomohiro Kusumi 	switch(io->type) {
1347fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_VOLUME:
1354fcdc8b4STomohiro Kusumi 		iostr = "volume";
1364fcdc8b4STomohiro Kusumi 		break;
1377fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_META_BUFFER:
1384fcdc8b4STomohiro Kusumi 		switch(HAMMER_ZONE(HAMMER_ITOB(io)->zoneX_offset)) {
1394fcdc8b4STomohiro Kusumi 		case HAMMER_ZONE_RAW_BUFFER:
1404fcdc8b4STomohiro Kusumi 			iostr = "meta/raw_buffer";
1414fcdc8b4STomohiro Kusumi 			break;
1424fcdc8b4STomohiro Kusumi 		case HAMMER_ZONE_FREEMAP:
1434fcdc8b4STomohiro Kusumi 			iostr = "meta/freemap";
1444fcdc8b4STomohiro Kusumi 			break;
1454fcdc8b4STomohiro Kusumi 		case HAMMER_ZONE_BTREE:
1464fcdc8b4STomohiro Kusumi 			iostr = "meta/btree";
1474fcdc8b4STomohiro Kusumi 			break;
1484fcdc8b4STomohiro Kusumi 		case HAMMER_ZONE_META:
1494fcdc8b4STomohiro Kusumi 			iostr = "meta/meta";
1504fcdc8b4STomohiro Kusumi 			break;
1514fcdc8b4STomohiro Kusumi 		}
1524fcdc8b4STomohiro Kusumi 		break;
1537fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_UNDO_BUFFER:
1544fcdc8b4STomohiro Kusumi 		iostr = "undo";
1554fcdc8b4STomohiro Kusumi 		break;
1567fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_DATA_BUFFER:
1574fcdc8b4STomohiro Kusumi 		switch(HAMMER_ZONE(HAMMER_ITOB(io)->zoneX_offset)) {
1584fcdc8b4STomohiro Kusumi 		case HAMMER_ZONE_LARGE_DATA:
1594fcdc8b4STomohiro Kusumi 			iostr = "data/large_data";
1604fcdc8b4STomohiro Kusumi 			break;
1614fcdc8b4STomohiro Kusumi 		case HAMMER_ZONE_SMALL_DATA:
1624fcdc8b4STomohiro Kusumi 			iostr = "data/small_data";
1634fcdc8b4STomohiro Kusumi 			break;
1644fcdc8b4STomohiro Kusumi 		}
1654fcdc8b4STomohiro Kusumi 		break;
1667fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_DUMMY:
1674fcdc8b4STomohiro Kusumi 		iostr = "dummy";
1684fcdc8b4STomohiro Kusumi 		break;
1694fcdc8b4STomohiro Kusumi 	default:
1704fcdc8b4STomohiro Kusumi 		hpanic("bad io type");
1714fcdc8b4STomohiro Kusumi 		break;
1724fcdc8b4STomohiro Kusumi 	}
1734fcdc8b4STomohiro Kusumi 
1744fcdc8b4STomohiro Kusumi 	return(iostr);
1754fcdc8b4STomohiro Kusumi }
1764fcdc8b4STomohiro Kusumi 
17766325755SMatthew Dillon /*
178fbc6e32aSMatthew Dillon  * Helper routine to disassociate a buffer cache buffer from an I/O
17977912481SMatthew Dillon  * structure.  The io must be interlocked and marked appropriately for
180b0aab9b9SMatthew Dillon  * reclamation.
181055f5ff8SMatthew Dillon  *
182b0aab9b9SMatthew Dillon  * The io must be in a released state with the io->bp owned and
183b0aab9b9SMatthew Dillon  * locked by the caller of this function.  When not called from an
184b0aab9b9SMatthew Dillon  * io_deallocate() this cannot race an io_deallocate() since the
185b0aab9b9SMatthew Dillon  * kernel would be unable to get the buffer lock in that case.
18677912481SMatthew Dillon  * (The released state in this case means we own the bp, not the
18777912481SMatthew Dillon  * hammer_io structure).
18877912481SMatthew Dillon  *
18977912481SMatthew Dillon  * The io may have 0 or 1 references depending on who called us.  The
19077912481SMatthew Dillon  * caller is responsible for dealing with the refs.
191b0aab9b9SMatthew Dillon  *
192055f5ff8SMatthew Dillon  * This call can only be made when no action is required on the buffer.
193ecca949aSMatthew Dillon  *
19477912481SMatthew Dillon  * This function is guaranteed not to race against anything because we
19577912481SMatthew Dillon  * own both the io lock and the bp lock and are interlocked with no
19677912481SMatthew Dillon  * references.
19766325755SMatthew Dillon  */
19866325755SMatthew Dillon static void
hammer_io_disassociate(hammer_io_t io)199ff66f880STomohiro Kusumi hammer_io_disassociate(hammer_io_t io)
20066325755SMatthew Dillon {
201ff66f880STomohiro Kusumi 	struct buf *bp = io->bp;
20266325755SMatthew Dillon 
203ff66f880STomohiro Kusumi 	KKASSERT(io->released);
204ff66f880STomohiro Kusumi 	KKASSERT(io->modified == 0);
2050ae73f43STomohiro Kusumi 	KKASSERT(hammer_buf_peek_io(bp) == io);
2064d75d829SMatthew Dillon 	buf_dep_init(bp);
207ff66f880STomohiro Kusumi 	io->bp = NULL;
2089f5097dcSMatthew Dillon 
2099f5097dcSMatthew Dillon 	/*
2109f5097dcSMatthew Dillon 	 * If the buffer was locked someone wanted to get rid of it.
2119f5097dcSMatthew Dillon 	 */
212a99b9ea2SMatthew Dillon 	if (bp->b_flags & B_LOCKED) {
213b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, -1);
214d8971d2bSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
215a99b9ea2SMatthew Dillon 	}
216ff66f880STomohiro Kusumi 	if (io->reclaim) {
217cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
218ff66f880STomohiro Kusumi 		io->reclaim = 0;
219ecca949aSMatthew Dillon 	}
22066325755SMatthew Dillon 
221ff66f880STomohiro Kusumi 	switch(io->type) {
2227fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_VOLUME:
223ff66f880STomohiro Kusumi 		HAMMER_ITOV(io)->ondisk = NULL;
22466325755SMatthew Dillon 		break;
2257fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_DATA_BUFFER:
2267fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_META_BUFFER:
2277fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_UNDO_BUFFER:
228ff66f880STomohiro Kusumi 		HAMMER_ITOB(io)->ondisk = NULL;
22966325755SMatthew Dillon 		break;
2307fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_DUMMY:
231903fdd05STomohiro Kusumi 		hpanic("bad io type");
232eddadaeeSMatthew Dillon 		break;
23366325755SMatthew Dillon 	}
23466325755SMatthew Dillon }
235fbc6e32aSMatthew Dillon 
236fbc6e32aSMatthew Dillon /*
237055f5ff8SMatthew Dillon  * Wait for any physical IO to complete
238ae8e83e6SMatthew Dillon  *
239ae8e83e6SMatthew Dillon  * XXX we aren't interlocked against a spinlock or anything so there
240ae8e83e6SMatthew Dillon  *     is a small window in the interlock / io->running == 0 test.
241fbc6e32aSMatthew Dillon  */
2421b0ab2c3SMatthew Dillon void
hammer_io_wait(hammer_io_t io)243055f5ff8SMatthew Dillon hammer_io_wait(hammer_io_t io)
244fbc6e32aSMatthew Dillon {
245055f5ff8SMatthew Dillon 	if (io->running) {
246b0aab9b9SMatthew Dillon 		hammer_mount_t hmp = io->hmp;
247b0aab9b9SMatthew Dillon 
248b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
249b0aab9b9SMatthew Dillon 		while (io->running) {
250ae8e83e6SMatthew Dillon 			io->waiting = 1;
251ae8e83e6SMatthew Dillon 			tsleep_interlock(io, 0);
252b0aab9b9SMatthew Dillon 			if (io->running)
253ae8e83e6SMatthew Dillon 				tsleep(io, PINTERLOCKED, "hmrflw", hz);
254055f5ff8SMatthew Dillon 		}
255b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
256055f5ff8SMatthew Dillon 	}
257055f5ff8SMatthew Dillon }
258055f5ff8SMatthew Dillon 
259af209b0fSMatthew Dillon /*
260eddadaeeSMatthew Dillon  * Wait for all currently queued HAMMER-initiated I/Os to complete.
261eddadaeeSMatthew Dillon  *
262eddadaeeSMatthew Dillon  * This is not supposed to count direct I/O's but some can leak
263eddadaeeSMatthew Dillon  * through (for non-full-sized direct I/Os).
264af209b0fSMatthew Dillon  */
265af209b0fSMatthew Dillon void
hammer_io_wait_all(hammer_mount_t hmp,const char * ident,int doflush)266eddadaeeSMatthew Dillon hammer_io_wait_all(hammer_mount_t hmp, const char *ident, int doflush)
267af209b0fSMatthew Dillon {
268eddadaeeSMatthew Dillon 	struct hammer_io iodummy;
269eddadaeeSMatthew Dillon 	hammer_io_t io;
270eddadaeeSMatthew Dillon 
271eddadaeeSMatthew Dillon 	/*
272eddadaeeSMatthew Dillon 	 * Degenerate case, no I/O is running
273eddadaeeSMatthew Dillon 	 */
274b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
275eddadaeeSMatthew Dillon 	if (TAILQ_EMPTY(&hmp->iorun_list)) {
276b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
277eddadaeeSMatthew Dillon 		if (doflush)
278eddadaeeSMatthew Dillon 			hammer_io_flush_sync(hmp);
279eddadaeeSMatthew Dillon 		return;
280eddadaeeSMatthew Dillon 	}
281eddadaeeSMatthew Dillon 	bzero(&iodummy, sizeof(iodummy));
2827fb33ff0STomohiro Kusumi 	iodummy.type = HAMMER_IOTYPE_DUMMY;
283eddadaeeSMatthew Dillon 
284eddadaeeSMatthew Dillon 	/*
285eddadaeeSMatthew Dillon 	 * Add placemarker and then wait until it becomes the head of
286eddadaeeSMatthew Dillon 	 * the list.
287eddadaeeSMatthew Dillon 	 */
288eddadaeeSMatthew Dillon 	TAILQ_INSERT_TAIL(&hmp->iorun_list, &iodummy, iorun_entry);
289eddadaeeSMatthew Dillon 	while (TAILQ_FIRST(&hmp->iorun_list) != &iodummy) {
290eddadaeeSMatthew Dillon 		tsleep(&iodummy, 0, ident, 0);
291eddadaeeSMatthew Dillon 	}
292eddadaeeSMatthew Dillon 
293eddadaeeSMatthew Dillon 	/*
294eddadaeeSMatthew Dillon 	 * Chain in case several placemarkers are present.
295eddadaeeSMatthew Dillon 	 */
296eddadaeeSMatthew Dillon 	TAILQ_REMOVE(&hmp->iorun_list, &iodummy, iorun_entry);
297eddadaeeSMatthew Dillon 	io = TAILQ_FIRST(&hmp->iorun_list);
2987fb33ff0STomohiro Kusumi 	if (io && io->type == HAMMER_IOTYPE_DUMMY)
299eddadaeeSMatthew Dillon 		wakeup(io);
300b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
301eddadaeeSMatthew Dillon 
302eddadaeeSMatthew Dillon 	if (doflush)
303eddadaeeSMatthew Dillon 		hammer_io_flush_sync(hmp);
304af209b0fSMatthew Dillon }
305af209b0fSMatthew Dillon 
3062faf0737SMatthew Dillon /*
3072faf0737SMatthew Dillon  * Clear a flagged error condition on a I/O buffer.  The caller must hold
3082faf0737SMatthew Dillon  * its own ref on the buffer.
3092faf0737SMatthew Dillon  */
3102faf0737SMatthew Dillon void
hammer_io_clear_error(hammer_io_t io)3116b482339STomohiro Kusumi hammer_io_clear_error(hammer_io_t io)
3122faf0737SMatthew Dillon {
31377912481SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
31477912481SMatthew Dillon 
31577912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
3162faf0737SMatthew Dillon 	if (io->ioerror) {
3172faf0737SMatthew Dillon 		io->ioerror = 0;
318250aec18SMatthew Dillon 		hammer_rel(&io->lock);
319250aec18SMatthew Dillon 		KKASSERT(hammer_isactive(&io->lock));
3202faf0737SMatthew Dillon 	}
32177912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
32277912481SMatthew Dillon }
32377912481SMatthew Dillon 
32477912481SMatthew Dillon void
hammer_io_clear_error_noassert(hammer_io_t io)3256b482339STomohiro Kusumi hammer_io_clear_error_noassert(hammer_io_t io)
32677912481SMatthew Dillon {
32777912481SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
32877912481SMatthew Dillon 
32977912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
33077912481SMatthew Dillon 	if (io->ioerror) {
33177912481SMatthew Dillon 		io->ioerror = 0;
33277912481SMatthew Dillon 		hammer_rel(&io->lock);
33377912481SMatthew Dillon 	}
33477912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
3352faf0737SMatthew Dillon }
3362faf0737SMatthew Dillon 
337b8a41159SMatthew Dillon /*
338b8a41159SMatthew Dillon  * This is an advisory function only which tells the buffer cache
339b8a41159SMatthew Dillon  * the bp is not a meta-data buffer, even though it is backed by
340b8a41159SMatthew Dillon  * a block device.
341b8a41159SMatthew Dillon  *
342b8a41159SMatthew Dillon  * This is used by HAMMER's reblocking code to avoid trying to
343b8a41159SMatthew Dillon  * swapcache the filesystem's data when it is read or written
344b8a41159SMatthew Dillon  * by the reblocking code.
345b0aab9b9SMatthew Dillon  *
346b0aab9b9SMatthew Dillon  * The caller has a ref on the buffer preventing the bp from
347b0aab9b9SMatthew Dillon  * being disassociated from it.
348b8a41159SMatthew Dillon  */
349b8a41159SMatthew Dillon void
hammer_io_notmeta(hammer_buffer_t buffer)350b8a41159SMatthew Dillon hammer_io_notmeta(hammer_buffer_t buffer)
351b8a41159SMatthew Dillon {
352b0aab9b9SMatthew Dillon 	if ((buffer->io.bp->b_flags & B_NOTMETA) == 0) {
353b0aab9b9SMatthew Dillon 		hammer_mount_t hmp = buffer->io.hmp;
354b0aab9b9SMatthew Dillon 
355b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
356b8a41159SMatthew Dillon 		buffer->io.bp->b_flags |= B_NOTMETA;
357b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
358b0aab9b9SMatthew Dillon 	}
359b8a41159SMatthew Dillon }
360b8a41159SMatthew Dillon 
36161aeeb33SMatthew Dillon /*
36210a5d1baSMatthew Dillon  * Load bp for a HAMMER structure.  The io must be exclusively locked by
36310a5d1baSMatthew Dillon  * the caller.
3642f85fa4dSMatthew Dillon  *
365a99b9ea2SMatthew Dillon  * This routine is mostly used on meta-data and small-data blocks.  Generally
366b7de8aa5SMatthew Dillon  * speaking HAMMER assumes some locality of reference and will cluster.
367af209b0fSMatthew Dillon  *
368b7de8aa5SMatthew Dillon  * Note that the caller (hammer_ondisk.c) may place further restrictions
369b7de8aa5SMatthew Dillon  * on clusterability via the limit (in bytes).  Typically large-data
370b7de8aa5SMatthew Dillon  * zones cannot be clustered due to their mixed buffer sizes.  This is
371b7de8aa5SMatthew Dillon  * not an issue since such clustering occurs in hammer_vnops at the
372b7de8aa5SMatthew Dillon  * regular file layer, whereas this is the buffered block device layer.
373b0aab9b9SMatthew Dillon  *
374b0aab9b9SMatthew Dillon  * No I/O callbacks can occur while we hold the buffer locked.
37566325755SMatthew Dillon  */
37666325755SMatthew Dillon int
hammer_io_read(struct vnode * devvp,hammer_io_t io,int limit)3776b482339STomohiro Kusumi hammer_io_read(struct vnode *devvp, hammer_io_t io, int limit)
37866325755SMatthew Dillon {
37966325755SMatthew Dillon 	struct buf *bp;
38066325755SMatthew Dillon 	int   error;
38166325755SMatthew Dillon 
38266325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
383e7d75765SMatthew Dillon 		int hce = hammer_cluster_enable;
384e7d75765SMatthew Dillon 
3853583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_io_running_read, io->bytes);
386e7d75765SMatthew Dillon 		if (hce && limit > io->bytes) {
387b7de8aa5SMatthew Dillon 			error = cluster_read(devvp, io->offset + limit,
388ce0138a6SMatthew Dillon 					     io->offset, io->bytes,
389af209b0fSMatthew Dillon 					     HAMMER_CLUSTER_SIZE,
390e7d75765SMatthew Dillon 					     HAMMER_CLUSTER_SIZE * hce,
391364c022cSMatthew Dillon 					     &io->bp);
392ce0138a6SMatthew Dillon 		} else {
3934a2796f3SMatthew Dillon 			error = bread(devvp, io->offset, io->bytes, &io->bp);
394ce0138a6SMatthew Dillon 		}
395ce0138a6SMatthew Dillon 		hammer_stats_disk_read += io->bytes;
3963583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_io_running_read, -io->bytes);
397cdb6e4e6SMatthew Dillon 
398cdb6e4e6SMatthew Dillon 		/*
399cdb6e4e6SMatthew Dillon 		 * The code generally assumes b_ops/b_dep has been set-up,
400cdb6e4e6SMatthew Dillon 		 * even if we error out here.
401cdb6e4e6SMatthew Dillon 		 */
40266325755SMatthew Dillon 		bp = io->bp;
4033b2afb67SMatthew Dillon 		if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IOISSUED)) {
40465d9d14fSTomohiro Kusumi 			hdkprintf("zone2_offset %016jx %s\n",
40524c8374aSMatthew Dillon 				(intmax_t)bp->b_bio2.bio_offset,
4064fcdc8b4STomohiro Kusumi 				hammer_io_to_iostring(io));
40724c8374aSMatthew Dillon 		}
4083b2afb67SMatthew Dillon 		bp->b_flags &= ~B_IOISSUED;
40966325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
410b0aab9b9SMatthew Dillon 
4110ae73f43STomohiro Kusumi 		hammer_buf_attach_io(bp, io); /* locked by the io lock */
41266325755SMatthew Dillon 		BUF_KERNPROC(bp);
41310a5d1baSMatthew Dillon 		KKASSERT(io->modified == 0);
41410a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
41510a5d1baSMatthew Dillon 		KKASSERT(io->waiting == 0);
41666325755SMatthew Dillon 		io->released = 0;	/* we hold an active lock on bp */
41766325755SMatthew Dillon 	} else {
41866325755SMatthew Dillon 		error = 0;
41966325755SMatthew Dillon 	}
42066325755SMatthew Dillon 	return(error);
42166325755SMatthew Dillon }
42266325755SMatthew Dillon 
42366325755SMatthew Dillon /*
42466325755SMatthew Dillon  * Similar to hammer_io_read() but returns a zero'd out buffer instead.
42510a5d1baSMatthew Dillon  * Must be called with the IO exclusively locked.
426055f5ff8SMatthew Dillon  *
42710a5d1baSMatthew Dillon  * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
42810a5d1baSMatthew Dillon  * I/O by forcing the buffer to not be in a released state before calling
42910a5d1baSMatthew Dillon  * it.
43010a5d1baSMatthew Dillon  *
43110a5d1baSMatthew Dillon  * This function will also mark the IO as modified but it will not
43210a5d1baSMatthew Dillon  * increment the modify_refs count.
433b0aab9b9SMatthew Dillon  *
434b0aab9b9SMatthew Dillon  * No I/O callbacks can occur while we hold the buffer locked.
43566325755SMatthew Dillon  */
43666325755SMatthew Dillon int
hammer_io_new(struct vnode * devvp,hammer_io_t io)4376b482339STomohiro Kusumi hammer_io_new(struct vnode *devvp, hammer_io_t io)
43866325755SMatthew Dillon {
43966325755SMatthew Dillon 	struct buf *bp;
44066325755SMatthew Dillon 
44166325755SMatthew Dillon 	if ((bp = io->bp) == NULL) {
4424a2796f3SMatthew Dillon 		io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
44366325755SMatthew Dillon 		bp = io->bp;
44466325755SMatthew Dillon 		bp->b_ops = &hammer_bioops;
445b0aab9b9SMatthew Dillon 
4460ae73f43STomohiro Kusumi 		hammer_buf_attach_io(bp, io); /* locked by the io lock */
447055f5ff8SMatthew Dillon 		io->released = 0;
44810a5d1baSMatthew Dillon 		KKASSERT(io->running == 0);
449055f5ff8SMatthew Dillon 		io->waiting = 0;
45066325755SMatthew Dillon 		BUF_KERNPROC(bp);
45166325755SMatthew Dillon 	} else {
45266325755SMatthew Dillon 		if (io->released) {
45366325755SMatthew Dillon 			regetblk(bp);
45466325755SMatthew Dillon 			BUF_KERNPROC(bp);
455d113fda1SMatthew Dillon 			io->released = 0;
45666325755SMatthew Dillon 		}
45766325755SMatthew Dillon 	}
45810a5d1baSMatthew Dillon 	hammer_io_modify(io, 0);
45966325755SMatthew Dillon 	vfs_bio_clrbuf(bp);
46066325755SMatthew Dillon 	return(0);
46166325755SMatthew Dillon }
46266325755SMatthew Dillon 
46366325755SMatthew Dillon /*
4640e8bd897SMatthew Dillon  * Advance the activity count on the underlying buffer because
4650e8bd897SMatthew Dillon  * HAMMER does not getblk/brelse on every access.
466b0aab9b9SMatthew Dillon  *
467b0aab9b9SMatthew Dillon  * The io->bp cannot go away while the buffer is referenced.
4680e8bd897SMatthew Dillon  */
4690e8bd897SMatthew Dillon void
hammer_io_advance(hammer_io_t io)4706b482339STomohiro Kusumi hammer_io_advance(hammer_io_t io)
4710e8bd897SMatthew Dillon {
4720e8bd897SMatthew Dillon 	if (io->bp)
4730e8bd897SMatthew Dillon 		buf_act_advance(io->bp);
4740e8bd897SMatthew Dillon }
4750e8bd897SMatthew Dillon 
4760e8bd897SMatthew Dillon /*
47747637bffSMatthew Dillon  * Remove potential device level aliases against buffers managed by high level
478362ec2dcSMatthew Dillon  * vnodes.  Aliases can also be created due to mixed buffer sizes or via
479362ec2dcSMatthew Dillon  * direct access to the backing store device.
480e469566bSMatthew Dillon  *
481e469566bSMatthew Dillon  * This is nasty because the buffers are also VMIO-backed.  Even if a buffer
482e469566bSMatthew Dillon  * does not exist its backing VM pages might, and we have to invalidate
483e469566bSMatthew Dillon  * those as well or a getblk() will reinstate them.
484362ec2dcSMatthew Dillon  *
485362ec2dcSMatthew Dillon  * Buffer cache buffers associated with hammer_buffers cannot be
486362ec2dcSMatthew Dillon  * invalidated.
48747637bffSMatthew Dillon  */
488362ec2dcSMatthew Dillon int
hammer_io_inval(hammer_volume_t volume,hammer_off_t zone2_offset)48947637bffSMatthew Dillon hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
49047637bffSMatthew Dillon {
491ff66f880STomohiro Kusumi 	hammer_io_t io;
492b0aab9b9SMatthew Dillon 	hammer_mount_t hmp;
49347637bffSMatthew Dillon 	hammer_off_t phys_offset;
49447637bffSMatthew Dillon 	struct buf *bp;
495362ec2dcSMatthew Dillon 	int error;
49647637bffSMatthew Dillon 
497b0aab9b9SMatthew Dillon 	hmp = volume->io.hmp;
498b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
499b0aab9b9SMatthew Dillon 
5009c90dba2SMatthew Dillon 	/*
5013b98d912SMatthew Dillon 	 * If a device buffer already exists for the specified physical
5023b98d912SMatthew Dillon 	 * offset use that, otherwise instantiate a buffer to cover any
5033b98d912SMatthew Dillon 	 * related VM pages, set BNOCACHE, and brelse().
5049c90dba2SMatthew Dillon 	 */
505516655e8STomohiro Kusumi 	phys_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset);
5063b98d912SMatthew Dillon 	if ((bp = findblk(volume->devvp, phys_offset, 0)) != NULL)
5073b98d912SMatthew Dillon 		bremfree(bp);
508e469566bSMatthew Dillon 	else
509e469566bSMatthew Dillon 		bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
510b0aab9b9SMatthew Dillon 
5110ae73f43STomohiro Kusumi 	if ((io = hammer_buf_peek_io(bp)) != NULL) {
512362ec2dcSMatthew Dillon #if 0
513ff66f880STomohiro Kusumi 		hammer_ref(&io->lock);
514ff66f880STomohiro Kusumi 		hammer_io_clear_modify(io, 1);
515cebe9493SMatthew Dillon 		bundirty(bp);
516ff66f880STomohiro Kusumi 		io->released = 0;
517e83ca595SMatthew Dillon 		BUF_KERNPROC(bp);
518ff66f880STomohiro Kusumi 		io->reclaim = 1;
519ff66f880STomohiro Kusumi 		io->waitdep = 1;	/* XXX this is a fs_token field */
520ff66f880STomohiro Kusumi 		KKASSERT(hammer_isactive(&io->lock) == 1);
521ff66f880STomohiro Kusumi 		hammer_rel_buffer(HAMMER_ITOB(io), 0);
5225c8d05e2SMatthew Dillon 		/*hammer_io_deallocate(bp);*/
523362ec2dcSMatthew Dillon #endif
52404b04ca6SMatthew Dillon 		bqrelse(bp);
525362ec2dcSMatthew Dillon 		error = EAGAIN;
5260832c9bbSMatthew Dillon 	} else {
527cebe9493SMatthew Dillon 		KKASSERT((bp->b_flags & B_LOCKED) == 0);
528cebe9493SMatthew Dillon 		bundirty(bp);
529cebe9493SMatthew Dillon 		bp->b_flags |= B_NOCACHE|B_RELBUF;
530ecca949aSMatthew Dillon 		brelse(bp);
531362ec2dcSMatthew Dillon 		error = 0;
532e83ca595SMatthew Dillon 	}
533b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
534362ec2dcSMatthew Dillon 	return(error);
5350832c9bbSMatthew Dillon }
53647637bffSMatthew Dillon 
53747637bffSMatthew Dillon /*
538b3deaf57SMatthew Dillon  * This routine is called on the last reference to a hammer structure.
539250aec18SMatthew Dillon  * The io must be interlocked with a refcount of zero.  The hammer structure
540250aec18SMatthew Dillon  * will remain interlocked on return.
541b3deaf57SMatthew Dillon  *
542250aec18SMatthew Dillon  * This routine may return a non-NULL bp to the caller for dispoal.
543250aec18SMatthew Dillon  * The caller typically brelse()'s the bp.
544250aec18SMatthew Dillon  *
545250aec18SMatthew Dillon  * The bp may or may not still be passively associated with the IO.  It
546250aec18SMatthew Dillon  * will remain passively associated if it is unreleasable (e.g. a modified
547250aec18SMatthew Dillon  * meta-data buffer).
548ecca949aSMatthew Dillon  *
549ecca949aSMatthew Dillon  * The only requirement here is that modified meta-data and volume-header
550ecca949aSMatthew Dillon  * buffer may NOT be disassociated from the IO structure, and consequently
551ecca949aSMatthew Dillon  * we also leave such buffers actively associated with the IO if they already
552ecca949aSMatthew Dillon  * are (since the kernel can't do anything with them anyway).  Only the
553ecca949aSMatthew Dillon  * flusher is allowed to write such buffers out.  Modified pure-data and
554ecca949aSMatthew Dillon  * undo buffers are returned to the kernel but left passively associated
555ecca949aSMatthew Dillon  * so we can track when the kernel writes the bp out.
55666325755SMatthew Dillon  */
557ecca949aSMatthew Dillon struct buf *
hammer_io_release(hammer_io_t io,int flush)5586b482339STomohiro Kusumi hammer_io_release(hammer_io_t io, int flush)
55966325755SMatthew Dillon {
56066325755SMatthew Dillon 	struct buf *bp;
56166325755SMatthew Dillon 
562fbc6e32aSMatthew Dillon 	if ((bp = io->bp) == NULL)
563ecca949aSMatthew Dillon 		return(NULL);
564fbc6e32aSMatthew Dillon 
5650b075555SMatthew Dillon 	/*
56610a5d1baSMatthew Dillon 	 * Try to flush a dirty IO to disk if asked to by the
56710a5d1baSMatthew Dillon 	 * caller or if the kernel tried to flush the buffer in the past.
5680b075555SMatthew Dillon 	 *
56910a5d1baSMatthew Dillon 	 * Kernel-initiated flushes are only allowed for pure-data buffers.
57010a5d1baSMatthew Dillon 	 * meta-data and volume buffers can only be flushed explicitly
57110a5d1baSMatthew Dillon 	 * by HAMMER.
572055f5ff8SMatthew Dillon 	 */
57310a5d1baSMatthew Dillon 	if (io->modified) {
57409ac686bSMatthew Dillon 		if (flush) {
575710733a6SMatthew Dillon 			hammer_io_flush(io, 0);
57610a5d1baSMatthew Dillon 		} else if (bp->b_flags & B_LOCKED) {
57710a5d1baSMatthew Dillon 			switch(io->type) {
5787fb33ff0STomohiro Kusumi 			case HAMMER_IOTYPE_DATA_BUFFER:
579710733a6SMatthew Dillon 				hammer_io_flush(io, 0);
580710733a6SMatthew Dillon 				break;
5817fb33ff0STomohiro Kusumi 			case HAMMER_IOTYPE_UNDO_BUFFER:
582710733a6SMatthew Dillon 				hammer_io_flush(io, hammer_undo_reclaim(io));
58310a5d1baSMatthew Dillon 				break;
58410a5d1baSMatthew Dillon 			default:
58510a5d1baSMatthew Dillon 				break;
58610a5d1baSMatthew Dillon 			}
58710a5d1baSMatthew Dillon 		} /* else no explicit request to flush the buffer */
58810a5d1baSMatthew Dillon 	}
589055f5ff8SMatthew Dillon 
590055f5ff8SMatthew Dillon 	/*
5915c8d05e2SMatthew Dillon 	 * Wait for the IO to complete if asked to.  This occurs when
5925c8d05e2SMatthew Dillon 	 * the buffer must be disposed of definitively during an umount
5935c8d05e2SMatthew Dillon 	 * or buffer invalidation.
594055f5ff8SMatthew Dillon 	 */
595b58c6388SMatthew Dillon 	if (io->waitdep && io->running) {
596055f5ff8SMatthew Dillon 		hammer_io_wait(io);
597055f5ff8SMatthew Dillon 	}
598055f5ff8SMatthew Dillon 
599055f5ff8SMatthew Dillon 	/*
60010a5d1baSMatthew Dillon 	 * Return control of the buffer to the kernel (with the provisio
60110a5d1baSMatthew Dillon 	 * that our bioops can override kernel decisions with regards to
60210a5d1baSMatthew Dillon 	 * the buffer).
603055f5ff8SMatthew Dillon 	 */
604cebe9493SMatthew Dillon 	if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
60510a5d1baSMatthew Dillon 		/*
60610a5d1baSMatthew Dillon 		 * Always disassociate the bp if an explicit flush
60710a5d1baSMatthew Dillon 		 * was requested and the IO completed with no error
60810a5d1baSMatthew Dillon 		 * (so unmount can really clean up the structure).
60910a5d1baSMatthew Dillon 		 */
610055f5ff8SMatthew Dillon 		if (io->released) {
611055f5ff8SMatthew Dillon 			regetblk(bp);
61246fe7ae1SMatthew Dillon 			BUF_KERNPROC(bp);
613ecca949aSMatthew Dillon 		} else {
614ecca949aSMatthew Dillon 			io->released = 1;
615055f5ff8SMatthew Dillon 		}
616ff66f880STomohiro Kusumi 		hammer_io_disassociate(io);
617ecca949aSMatthew Dillon 		/* return the bp */
618055f5ff8SMatthew Dillon 	} else if (io->modified) {
61910a5d1baSMatthew Dillon 		/*
620ecca949aSMatthew Dillon 		 * Only certain IO types can be released to the kernel if
621ecca949aSMatthew Dillon 		 * the buffer has been modified.
622ecca949aSMatthew Dillon 		 *
623ecca949aSMatthew Dillon 		 * volume and meta-data IO types may only be explicitly
624ecca949aSMatthew Dillon 		 * flushed by HAMMER.
62510a5d1baSMatthew Dillon 		 */
62610a5d1baSMatthew Dillon 		switch(io->type) {
6277fb33ff0STomohiro Kusumi 		case HAMMER_IOTYPE_DATA_BUFFER:
6287fb33ff0STomohiro Kusumi 		case HAMMER_IOTYPE_UNDO_BUFFER:
629b58c6388SMatthew Dillon 			if (io->released == 0) {
630055f5ff8SMatthew Dillon 				io->released = 1;
6319de13b88SMatthew Dillon 				bp->b_flags |= B_CLUSTEROK;
632055f5ff8SMatthew Dillon 				bdwrite(bp);
633055f5ff8SMatthew Dillon 			}
63410a5d1baSMatthew Dillon 			break;
63510a5d1baSMatthew Dillon 		default:
63610a5d1baSMatthew Dillon 			break;
63710a5d1baSMatthew Dillon 		}
638ecca949aSMatthew Dillon 		bp = NULL;	/* bp left associated */
639055f5ff8SMatthew Dillon 	} else if (io->released == 0) {
64010a5d1baSMatthew Dillon 		/*
64110a5d1baSMatthew Dillon 		 * Clean buffers can be generally released to the kernel.
64210a5d1baSMatthew Dillon 		 * We leave the bp passively associated with the HAMMER
64310a5d1baSMatthew Dillon 		 * structure and use bioops to disconnect it later on
64410a5d1baSMatthew Dillon 		 * if the kernel wants to discard the buffer.
645ecca949aSMatthew Dillon 		 *
646ecca949aSMatthew Dillon 		 * We can steal the structure's ownership of the bp.
64710a5d1baSMatthew Dillon 		 */
648ecca949aSMatthew Dillon 		io->released = 1;
6499f5097dcSMatthew Dillon 		if (bp->b_flags & B_LOCKED) {
650ff66f880STomohiro Kusumi 			hammer_io_disassociate(io);
651ecca949aSMatthew Dillon 			/* return the bp */
6529f5097dcSMatthew Dillon 		} else {
653cebe9493SMatthew Dillon 			if (io->reclaim) {
654ff66f880STomohiro Kusumi 				hammer_io_disassociate(io);
655ecca949aSMatthew Dillon 				/* return the bp */
656cebe9493SMatthew Dillon 			} else {
657ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
6589f5097dcSMatthew Dillon 			}
659cebe9493SMatthew Dillon 		}
66019b97e01SMatthew Dillon 	} else {
66119b97e01SMatthew Dillon 		/*
662af209b0fSMatthew Dillon 		 * A released buffer is passively associate with our
663af209b0fSMatthew Dillon 		 * hammer_io structure.  The kernel cannot destroy it
664af209b0fSMatthew Dillon 		 * without making a bioops call.  If the kernel (B_LOCKED)
665af209b0fSMatthew Dillon 		 * or we (reclaim) requested that the buffer be destroyed
666af209b0fSMatthew Dillon 		 * we destroy it, otherwise we do a quick get/release to
667af209b0fSMatthew Dillon 		 * reset its position in the kernel's LRU list.
668af209b0fSMatthew Dillon 		 *
669af209b0fSMatthew Dillon 		 * Leaving the buffer passively associated allows us to
670af209b0fSMatthew Dillon 		 * use the kernel's LRU buffer flushing mechanisms rather
671af209b0fSMatthew Dillon 		 * then rolling our own.
672cb51be26SMatthew Dillon 		 *
673cb51be26SMatthew Dillon 		 * XXX there are two ways of doing this.  We can re-acquire
674cb51be26SMatthew Dillon 		 * and passively release to reset the LRU, or not.
67519b97e01SMatthew Dillon 		 */
676af209b0fSMatthew Dillon 		if (io->running == 0) {
67719b97e01SMatthew Dillon 			regetblk(bp);
678cebe9493SMatthew Dillon 			if ((bp->b_flags & B_LOCKED) || io->reclaim) {
679ff66f880STomohiro Kusumi 				hammer_io_disassociate(io);
680ecca949aSMatthew Dillon 				/* return the bp */
6819f5097dcSMatthew Dillon 			} else {
682ecca949aSMatthew Dillon 				/* return the bp (bp passively associated) */
683ecca949aSMatthew Dillon 			}
684ecca949aSMatthew Dillon 		} else {
685ecca949aSMatthew Dillon 			/*
686ecca949aSMatthew Dillon 			 * bp is left passively associated but we do not
687ecca949aSMatthew Dillon 			 * try to reacquire it.  Interactions with the io
688ecca949aSMatthew Dillon 			 * structure will occur on completion of the bp's
689ecca949aSMatthew Dillon 			 * I/O.
690ecca949aSMatthew Dillon 			 */
691ecca949aSMatthew Dillon 			bp = NULL;
69219b97e01SMatthew Dillon 		}
6939f5097dcSMatthew Dillon 	}
694ecca949aSMatthew Dillon 	return(bp);
695055f5ff8SMatthew Dillon }
696055f5ff8SMatthew Dillon 
697055f5ff8SMatthew Dillon /*
698b33e2cc0SMatthew Dillon  * This routine is called with a locked IO when a flush is desired and
699b33e2cc0SMatthew Dillon  * no other references to the structure exists other then ours.  This
700b33e2cc0SMatthew Dillon  * routine is ONLY called when HAMMER believes it is safe to flush a
701b33e2cc0SMatthew Dillon  * potentially modified buffer out.
70277912481SMatthew Dillon  *
70377912481SMatthew Dillon  * The locked io or io reference prevents a flush from being initiated
70477912481SMatthew Dillon  * by the kernel.
7050b075555SMatthew Dillon  */
7060b075555SMatthew Dillon void
hammer_io_flush(hammer_io_t io,int reclaim)7076b482339STomohiro Kusumi hammer_io_flush(hammer_io_t io, int reclaim)
7080b075555SMatthew Dillon {
709055f5ff8SMatthew Dillon 	struct buf *bp;
71077912481SMatthew Dillon 	hammer_mount_t hmp;
711055f5ff8SMatthew Dillon 
712055f5ff8SMatthew Dillon 	/*
71310a5d1baSMatthew Dillon 	 * Degenerate case - nothing to flush if nothing is dirty.
714055f5ff8SMatthew Dillon 	 */
715b0aab9b9SMatthew Dillon 	if (io->modified == 0)
716055f5ff8SMatthew Dillon 		return;
717055f5ff8SMatthew Dillon 
718055f5ff8SMatthew Dillon 	KKASSERT(io->bp);
7199f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs <= 0);
720055f5ff8SMatthew Dillon 
721b33e2cc0SMatthew Dillon 	/*
72277062c8aSMatthew Dillon 	 * Acquire ownership of the bp, particularly before we clear our
72377062c8aSMatthew Dillon 	 * modified flag.
72477062c8aSMatthew Dillon 	 *
72577062c8aSMatthew Dillon 	 * We are going to bawrite() this bp.  Don't leave a window where
72677062c8aSMatthew Dillon 	 * io->released is set, we actually own the bp rather then our
72777062c8aSMatthew Dillon 	 * buffer.
728b0aab9b9SMatthew Dillon 	 *
729b0aab9b9SMatthew Dillon 	 * The io_token should not be required here as only
73077062c8aSMatthew Dillon 	 */
73177912481SMatthew Dillon 	hmp = io->hmp;
73277062c8aSMatthew Dillon 	bp = io->bp;
73377062c8aSMatthew Dillon 	if (io->released) {
73477062c8aSMatthew Dillon 		regetblk(bp);
73577062c8aSMatthew Dillon 		/* BUF_KERNPROC(io->bp); */
73677062c8aSMatthew Dillon 		/* io->released = 0; */
73777062c8aSMatthew Dillon 		KKASSERT(io->released);
73877062c8aSMatthew Dillon 		KKASSERT(io->bp == bp);
739b0aab9b9SMatthew Dillon 	} else {
74077062c8aSMatthew Dillon 		io->released = 1;
741b0aab9b9SMatthew Dillon 	}
74277062c8aSMatthew Dillon 
743710733a6SMatthew Dillon 	if (reclaim) {
744710733a6SMatthew Dillon 		io->reclaim = 1;
745710733a6SMatthew Dillon 		if ((bp->b_flags & B_LOCKED) == 0) {
746710733a6SMatthew Dillon 			bp->b_flags |= B_LOCKED;
747b0aab9b9SMatthew Dillon 			atomic_add_int(&hammer_count_io_locked, 1);
748710733a6SMatthew Dillon 		}
749710733a6SMatthew Dillon 	}
750710733a6SMatthew Dillon 
75177062c8aSMatthew Dillon 	/*
75210a5d1baSMatthew Dillon 	 * Acquire exclusive access to the bp and then clear the modified
75310a5d1baSMatthew Dillon 	 * state of the buffer prior to issuing I/O to interlock any
75410a5d1baSMatthew Dillon 	 * modifications made while the I/O is in progress.  This shouldn't
75510a5d1baSMatthew Dillon 	 * happen anyway but losing data would be worse.  The modified bit
75610a5d1baSMatthew Dillon 	 * will be rechecked after the IO completes.
75710a5d1baSMatthew Dillon 	 *
7584a2796f3SMatthew Dillon 	 * NOTE: This call also finalizes the buffer's content (inval == 0).
7594a2796f3SMatthew Dillon 	 *
760b33e2cc0SMatthew Dillon 	 * This is only legal when lock.refs == 1 (otherwise we might clear
761b33e2cc0SMatthew Dillon 	 * the modified bit while there are still users of the cluster
762b33e2cc0SMatthew Dillon 	 * modifying the data).
763b33e2cc0SMatthew Dillon 	 *
764b33e2cc0SMatthew Dillon 	 * Do this before potentially blocking so any attempt to modify the
765b33e2cc0SMatthew Dillon 	 * ondisk while we are blocked blocks waiting for us.
766b33e2cc0SMatthew Dillon 	 */
7675c8d05e2SMatthew Dillon 	hammer_ref(&io->lock);
7684a2796f3SMatthew Dillon 	hammer_io_clear_modify(io, 0);
769250aec18SMatthew Dillon 	hammer_rel(&io->lock);
770bcac4bbbSMatthew Dillon 
7716367d0f9SMatthew Dillon 	if (hammer_debug_io & 0x0002)
77211605a5cSTomohiro Kusumi 		hdkprintf("%016jx\n", bp->b_bio1.bio_offset);
7736367d0f9SMatthew Dillon 
774bcac4bbbSMatthew Dillon 	/*
77510a5d1baSMatthew Dillon 	 * Transfer ownership to the kernel and initiate I/O.
776b0aab9b9SMatthew Dillon 	 *
777b0aab9b9SMatthew Dillon 	 * NOTE: We do not hold io_token so an atomic op is required to
778b0aab9b9SMatthew Dillon 	 *	 update io_running_space.
77910a5d1baSMatthew Dillon 	 */
780055f5ff8SMatthew Dillon 	io->running = 1;
7813583bbb4SMatthew Dillon 	atomic_add_long(&hmp->io_running_space, io->bytes);
7823583bbb4SMatthew Dillon 	atomic_add_long(&hammer_count_io_running_write, io->bytes);
78377912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
78477912481SMatthew Dillon 	TAILQ_INSERT_TAIL(&hmp->iorun_list, io, iorun_entry);
78577912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
7869de13b88SMatthew Dillon 	cluster_awrite(bp);
787748efb59SMatthew Dillon 	hammer_io_flush_mark(io->volume);
788055f5ff8SMatthew Dillon }
789055f5ff8SMatthew Dillon 
790055f5ff8SMatthew Dillon /************************************************************************
791055f5ff8SMatthew Dillon  *				BUFFER DIRTYING				*
792055f5ff8SMatthew Dillon  ************************************************************************
793055f5ff8SMatthew Dillon  *
794055f5ff8SMatthew Dillon  * These routines deal with dependancies created when IO buffers get
795055f5ff8SMatthew Dillon  * modified.  The caller must call hammer_modify_*() on a referenced
796055f5ff8SMatthew Dillon  * HAMMER structure prior to modifying its on-disk data.
797055f5ff8SMatthew Dillon  *
798055f5ff8SMatthew Dillon  * Any intent to modify an IO buffer acquires the related bp and imposes
799055f5ff8SMatthew Dillon  * various write ordering dependancies.
800055f5ff8SMatthew Dillon  */
801055f5ff8SMatthew Dillon 
802055f5ff8SMatthew Dillon /*
80310a5d1baSMatthew Dillon  * Mark a HAMMER structure as undergoing modification.  Meta-data buffers
80410a5d1baSMatthew Dillon  * are locked until the flusher can deal with them, pure data buffers
80510a5d1baSMatthew Dillon  * can be written out.
80677912481SMatthew Dillon  *
80777912481SMatthew Dillon  * The referenced io prevents races.
808055f5ff8SMatthew Dillon  */
80910a5d1baSMatthew Dillon static
810b58c6388SMatthew Dillon void
hammer_io_modify(hammer_io_t io,int count)81110a5d1baSMatthew Dillon hammer_io_modify(hammer_io_t io, int count)
812055f5ff8SMatthew Dillon {
81346fe7ae1SMatthew Dillon 	/*
8149f5097dcSMatthew Dillon 	 * io->modify_refs must be >= 0
8159f5097dcSMatthew Dillon 	 */
8169f5097dcSMatthew Dillon 	while (io->modify_refs < 0) {
8179f5097dcSMatthew Dillon 		io->waitmod = 1;
8189f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
8199f5097dcSMatthew Dillon 	}
8209f5097dcSMatthew Dillon 
8219f5097dcSMatthew Dillon 	/*
82246fe7ae1SMatthew Dillon 	 * Shortcut if nothing to do.
82346fe7ae1SMatthew Dillon 	 */
824250aec18SMatthew Dillon 	KKASSERT(hammer_isactive(&io->lock) && io->bp != NULL);
82510a5d1baSMatthew Dillon 	io->modify_refs += count;
826b58c6388SMatthew Dillon 	if (io->modified && io->released == 0)
827b58c6388SMatthew Dillon 		return;
82846fe7ae1SMatthew Dillon 
82977912481SMatthew Dillon 	/*
83077912481SMatthew Dillon 	 * NOTE: It is important not to set the modified bit
83177912481SMatthew Dillon 	 *	 until after we have acquired the bp or we risk
83277912481SMatthew Dillon 	 *	 racing against checkwrite.
83377912481SMatthew Dillon 	 */
834055f5ff8SMatthew Dillon 	hammer_lock_ex(&io->lock);
835055f5ff8SMatthew Dillon 	if (io->released) {
836055f5ff8SMatthew Dillon 		regetblk(io->bp);
837055f5ff8SMatthew Dillon 		BUF_KERNPROC(io->bp);
838055f5ff8SMatthew Dillon 		io->released = 0;
83977912481SMatthew Dillon 	}
84077912481SMatthew Dillon 	if (io->modified == 0) {
84177912481SMatthew Dillon 		hammer_io_set_modlist(io);
84277912481SMatthew Dillon 		io->modified = 1;
843055f5ff8SMatthew Dillon 	}
844055f5ff8SMatthew Dillon 	hammer_unlock(&io->lock);
8450b075555SMatthew Dillon }
8460b075555SMatthew Dillon 
84710a5d1baSMatthew Dillon static __inline
84810a5d1baSMatthew Dillon void
hammer_io_modify_done(hammer_io_t io)84910a5d1baSMatthew Dillon hammer_io_modify_done(hammer_io_t io)
85010a5d1baSMatthew Dillon {
85110a5d1baSMatthew Dillon 	KKASSERT(io->modify_refs > 0);
85210a5d1baSMatthew Dillon 	--io->modify_refs;
8539f5097dcSMatthew Dillon 	if (io->modify_refs == 0 && io->waitmod) {
8549f5097dcSMatthew Dillon 		io->waitmod = 0;
8559f5097dcSMatthew Dillon 		wakeup(io);
8569f5097dcSMatthew Dillon 	}
8579f5097dcSMatthew Dillon }
8589f5097dcSMatthew Dillon 
85977912481SMatthew Dillon /*
86077912481SMatthew Dillon  * The write interlock blocks other threads trying to modify a buffer
86177912481SMatthew Dillon  * (they block in hammer_io_modify()) after us, or blocks us while other
86277912481SMatthew Dillon  * threads are in the middle of modifying a buffer.
86377912481SMatthew Dillon  *
86477912481SMatthew Dillon  * The caller also has a ref on the io, however if we are not careful
86577912481SMatthew Dillon  * we will race bioops callbacks (checkwrite).  To deal with this
86677912481SMatthew Dillon  * we must at least acquire and release the io_token, and it is probably
86777912481SMatthew Dillon  * better to hold it through the setting of modify_refs.
86877912481SMatthew Dillon  */
8699f5097dcSMatthew Dillon void
hammer_io_write_interlock(hammer_io_t io)8709f5097dcSMatthew Dillon hammer_io_write_interlock(hammer_io_t io)
8719f5097dcSMatthew Dillon {
87277912481SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
87377912481SMatthew Dillon 
87477912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
8759f5097dcSMatthew Dillon 	while (io->modify_refs != 0) {
8769f5097dcSMatthew Dillon 		io->waitmod = 1;
8779f5097dcSMatthew Dillon 		tsleep(io, 0, "hmrmod", 0);
8789f5097dcSMatthew Dillon 	}
8799f5097dcSMatthew Dillon 	io->modify_refs = -1;
88077912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
8819f5097dcSMatthew Dillon }
8829f5097dcSMatthew Dillon 
8839f5097dcSMatthew Dillon void
hammer_io_done_interlock(hammer_io_t io)8849f5097dcSMatthew Dillon hammer_io_done_interlock(hammer_io_t io)
8859f5097dcSMatthew Dillon {
8869f5097dcSMatthew Dillon 	KKASSERT(io->modify_refs == -1);
8879f5097dcSMatthew Dillon 	io->modify_refs = 0;
8889f5097dcSMatthew Dillon 	if (io->waitmod) {
8899f5097dcSMatthew Dillon 		io->waitmod = 0;
8909f5097dcSMatthew Dillon 		wakeup(io);
8919f5097dcSMatthew Dillon 	}
89210a5d1baSMatthew Dillon }
89310a5d1baSMatthew Dillon 
8942f85fa4dSMatthew Dillon /*
8952f85fa4dSMatthew Dillon  * Caller intends to modify a volume's ondisk structure.
8962f85fa4dSMatthew Dillon  *
8972f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
8982f85fa4dSMatthew Dillon  * sync_lock.
8992f85fa4dSMatthew Dillon  */
9000b075555SMatthew Dillon void
hammer_modify_volume(hammer_transaction_t trans,hammer_volume_t volume,void * base,int len)90136f82b23SMatthew Dillon hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
90236f82b23SMatthew Dillon 		     void *base, int len)
9030b075555SMatthew Dillon {
9042f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
905055f5ff8SMatthew Dillon 
9062f85fa4dSMatthew Dillon 	hammer_io_modify(&volume->io, 1);
90747197d71SMatthew Dillon 	if (len) {
90847197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
90947197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
91002428fb6SMatthew Dillon 		hammer_generate_undo(trans,
91147197d71SMatthew Dillon 			 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
91247197d71SMatthew Dillon 			 base, len);
913055f5ff8SMatthew Dillon 	}
914055f5ff8SMatthew Dillon }
915055f5ff8SMatthew Dillon 
916055f5ff8SMatthew Dillon /*
9172f85fa4dSMatthew Dillon  * Caller intends to modify a buffer's ondisk structure.
9182f85fa4dSMatthew Dillon  *
9192f85fa4dSMatthew Dillon  * This is only allowed if we are the flusher or we have a ref on the
9202f85fa4dSMatthew Dillon  * sync_lock.
921055f5ff8SMatthew Dillon  */
922055f5ff8SMatthew Dillon void
hammer_modify_buffer(hammer_transaction_t trans,hammer_buffer_t buffer,void * base,int len)92336f82b23SMatthew Dillon hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
92436f82b23SMatthew Dillon 		     void *base, int len)
92546fe7ae1SMatthew Dillon {
9262f85fa4dSMatthew Dillon 	KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
9272f85fa4dSMatthew Dillon 
92810a5d1baSMatthew Dillon 	hammer_io_modify(&buffer->io, 1);
92947197d71SMatthew Dillon 	if (len) {
93047197d71SMatthew Dillon 		intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
93147197d71SMatthew Dillon 		KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
93202428fb6SMatthew Dillon 		hammer_generate_undo(trans,
93334d829f7SMatthew Dillon 				     buffer->zone2_offset + rel_offset,
93447197d71SMatthew Dillon 				     base, len);
93547197d71SMatthew Dillon 	}
93646fe7ae1SMatthew Dillon }
93746fe7ae1SMatthew Dillon 
93810a5d1baSMatthew Dillon void
hammer_modify_volume_done(hammer_volume_t volume)93910a5d1baSMatthew Dillon hammer_modify_volume_done(hammer_volume_t volume)
94010a5d1baSMatthew Dillon {
94110a5d1baSMatthew Dillon 	hammer_io_modify_done(&volume->io);
94210a5d1baSMatthew Dillon }
94310a5d1baSMatthew Dillon 
94410a5d1baSMatthew Dillon void
hammer_modify_buffer_done(hammer_buffer_t buffer)94510a5d1baSMatthew Dillon hammer_modify_buffer_done(hammer_buffer_t buffer)
94610a5d1baSMatthew Dillon {
94710a5d1baSMatthew Dillon 	hammer_io_modify_done(&buffer->io);
94810a5d1baSMatthew Dillon }
94910a5d1baSMatthew Dillon 
95046fe7ae1SMatthew Dillon /*
9514a2796f3SMatthew Dillon  * Mark an entity as not being dirty any more and finalize any
9524a2796f3SMatthew Dillon  * delayed adjustments to the buffer.
9534a2796f3SMatthew Dillon  *
9544a2796f3SMatthew Dillon  * Delayed adjustments are an important performance enhancement, allowing
9554a2796f3SMatthew Dillon  * us to avoid recalculating B-Tree node CRCs over and over again when
9564a2796f3SMatthew Dillon  * making bulk-modifications to the B-Tree.
9574a2796f3SMatthew Dillon  *
9584a2796f3SMatthew Dillon  * If inval is non-zero delayed adjustments are ignored.
9595c8d05e2SMatthew Dillon  *
9605c8d05e2SMatthew Dillon  * This routine may dereference related btree nodes and cause the
9615c8d05e2SMatthew Dillon  * buffer to be dereferenced.  The caller must own a reference on io.
96261aeeb33SMatthew Dillon  */
96361aeeb33SMatthew Dillon void
hammer_io_clear_modify(hammer_io_t io,int inval)9646b482339STomohiro Kusumi hammer_io_clear_modify(hammer_io_t io, int inval)
96561aeeb33SMatthew Dillon {
96677912481SMatthew Dillon 	hammer_mount_t hmp;
96777912481SMatthew Dillon 
96877912481SMatthew Dillon 	/*
9691afb73cfSMatthew Dillon 	 * io_token is needed to avoid races on mod_root
97077912481SMatthew Dillon 	 */
9714a2796f3SMatthew Dillon 	if (io->modified == 0)
9724a2796f3SMatthew Dillon 		return;
97377912481SMatthew Dillon 	hmp = io->hmp;
97477912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
97577912481SMatthew Dillon 	if (io->modified == 0) {
97677912481SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
97777912481SMatthew Dillon 		return;
97877912481SMatthew Dillon 	}
9794a2796f3SMatthew Dillon 
9804a2796f3SMatthew Dillon 	/*
9814a2796f3SMatthew Dillon 	 * Take us off the mod-list and clear the modified bit.
9824a2796f3SMatthew Dillon 	 */
9831afb73cfSMatthew Dillon 	KKASSERT(io->mod_root != NULL);
9841afb73cfSMatthew Dillon 	if (io->mod_root == &io->hmp->volu_root ||
9851afb73cfSMatthew Dillon 	    io->mod_root == &io->hmp->meta_root) {
986f5a07a7aSMatthew Dillon 		io->hmp->locked_dirty_space -= io->bytes;
9873583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_dirtybufspace, -io->bytes);
988cebe9493SMatthew Dillon 	}
9891afb73cfSMatthew Dillon 	RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io);
9901afb73cfSMatthew Dillon 	io->mod_root = NULL;
99161aeeb33SMatthew Dillon 	io->modified = 0;
9924a2796f3SMatthew Dillon 
99377912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
99477912481SMatthew Dillon 
9954a2796f3SMatthew Dillon 	/*
9964a2796f3SMatthew Dillon 	 * If this bit is not set there are no delayed adjustments.
9974a2796f3SMatthew Dillon 	 */
9984a2796f3SMatthew Dillon 	if (io->gencrc == 0)
9994a2796f3SMatthew Dillon 		return;
10004a2796f3SMatthew Dillon 	io->gencrc = 0;
10014a2796f3SMatthew Dillon 
10024a2796f3SMatthew Dillon 	/*
10034a2796f3SMatthew Dillon 	 * Finalize requested CRCs.  The NEEDSCRC flag also holds a reference
10044a2796f3SMatthew Dillon 	 * on the node (& underlying buffer).  Release the node after clearing
10054a2796f3SMatthew Dillon 	 * the flag.
10064a2796f3SMatthew Dillon 	 */
10077fb33ff0STomohiro Kusumi 	if (io->type == HAMMER_IOTYPE_META_BUFFER) {
1008195f6076STomohiro Kusumi 		hammer_buffer_t buffer = HAMMER_ITOB(io);
10094a2796f3SMatthew Dillon 		hammer_node_t node;
10104a2796f3SMatthew Dillon 
10114a2796f3SMatthew Dillon restart:
1012c242ffecSTomohiro Kusumi 		TAILQ_FOREACH(node, &buffer->node_list, entry) {
10134a2796f3SMatthew Dillon 			if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
10144a2796f3SMatthew Dillon 				continue;
10154a2796f3SMatthew Dillon 			node->flags &= ~HAMMER_NODE_NEEDSCRC;
10164a2796f3SMatthew Dillon 			KKASSERT(node->ondisk);
10174a2796f3SMatthew Dillon 			if (inval == 0)
10184c09d9c4SMatthew Dillon 				hammer_crc_set_btree(hmp->version, node->ondisk);
10194a2796f3SMatthew Dillon 			hammer_rel_node(node);
10204a2796f3SMatthew Dillon 			goto restart;
102161aeeb33SMatthew Dillon 		}
102261aeeb33SMatthew Dillon 	}
10235c8d05e2SMatthew Dillon 	/* caller must still have ref on io */
1024250aec18SMatthew Dillon 	KKASSERT(hammer_isactive(&io->lock));
10254a2796f3SMatthew Dillon }
10264a2796f3SMatthew Dillon 
1027cebe9493SMatthew Dillon /*
1028cebe9493SMatthew Dillon  * Clear the IO's modify list.  Even though the IO is no longer modified
10291afb73cfSMatthew Dillon  * it may still be on the lose_root.  This routine is called just before
1030cebe9493SMatthew Dillon  * the governing hammer_buffer is destroyed.
1031b0aab9b9SMatthew Dillon  *
10321afb73cfSMatthew Dillon  * mod_root requires io_token protection.
1033cebe9493SMatthew Dillon  */
1034cebe9493SMatthew Dillon void
hammer_io_clear_modlist(hammer_io_t io)10356b482339STomohiro Kusumi hammer_io_clear_modlist(hammer_io_t io)
1036cebe9493SMatthew Dillon {
1037b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
1038b0aab9b9SMatthew Dillon 
10394a2796f3SMatthew Dillon 	KKASSERT(io->modified == 0);
10401afb73cfSMatthew Dillon 	if (io->mod_root) {
1041b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
10421afb73cfSMatthew Dillon 		if (io->mod_root) {
10431afb73cfSMatthew Dillon 			KKASSERT(io->mod_root == &io->hmp->lose_root);
10441afb73cfSMatthew Dillon 			RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io);
10451afb73cfSMatthew Dillon 			io->mod_root = NULL;
1046b0aab9b9SMatthew Dillon 		}
1047b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
1048cebe9493SMatthew Dillon 	}
104966325755SMatthew Dillon }
105066325755SMatthew Dillon 
1051cdb6e4e6SMatthew Dillon static void
hammer_io_set_modlist(hammer_io_t io)10526b482339STomohiro Kusumi hammer_io_set_modlist(hammer_io_t io)
1053cdb6e4e6SMatthew Dillon {
1054ba2be8e9STomohiro Kusumi 	hammer_mount_t hmp = io->hmp;
1055cdb6e4e6SMatthew Dillon 
105677912481SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
10571afb73cfSMatthew Dillon 	KKASSERT(io->mod_root == NULL);
1058cdb6e4e6SMatthew Dillon 
1059cdb6e4e6SMatthew Dillon 	switch(io->type) {
10607fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_VOLUME:
10611afb73cfSMatthew Dillon 		io->mod_root = &hmp->volu_root;
1062cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
10633583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_dirtybufspace, io->bytes);
1064cdb6e4e6SMatthew Dillon 		break;
10657fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_META_BUFFER:
10661afb73cfSMatthew Dillon 		io->mod_root = &hmp->meta_root;
1067cdb6e4e6SMatthew Dillon 		hmp->locked_dirty_space += io->bytes;
10683583bbb4SMatthew Dillon 		atomic_add_long(&hammer_count_dirtybufspace, io->bytes);
1069cdb6e4e6SMatthew Dillon 		break;
10707fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_UNDO_BUFFER:
10711afb73cfSMatthew Dillon 		io->mod_root = &hmp->undo_root;
1072cdb6e4e6SMatthew Dillon 		break;
10737fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_DATA_BUFFER:
10741afb73cfSMatthew Dillon 		io->mod_root = &hmp->data_root;
1075cdb6e4e6SMatthew Dillon 		break;
10767fb33ff0STomohiro Kusumi 	case HAMMER_IOTYPE_DUMMY:
1077903fdd05STomohiro Kusumi 		hpanic("bad io type");
10781afb73cfSMatthew Dillon 		break; /* NOT REACHED */
1079cdb6e4e6SMatthew Dillon 	}
10801afb73cfSMatthew Dillon 	if (RB_INSERT(hammer_mod_rb_tree, io->mod_root, io)) {
1081b49481fbSTomohiro Kusumi 		hpanic("duplicate entry @ %d:%015jx",
1082b49481fbSTomohiro Kusumi 			io->volume->vol_no, io->offset);
10831afb73cfSMatthew Dillon 		/* NOT REACHED */
10841afb73cfSMatthew Dillon 	}
108577912481SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
1086cdb6e4e6SMatthew Dillon }
1087cdb6e4e6SMatthew Dillon 
1088055f5ff8SMatthew Dillon /************************************************************************
1089055f5ff8SMatthew Dillon  *				HAMMER_BIOOPS				*
1090055f5ff8SMatthew Dillon  ************************************************************************
1091055f5ff8SMatthew Dillon  *
1092055f5ff8SMatthew Dillon  */
1093055f5ff8SMatthew Dillon 
1094055f5ff8SMatthew Dillon /*
1095055f5ff8SMatthew Dillon  * Pre-IO initiation kernel callback - cluster build only
1096b0aab9b9SMatthew Dillon  *
1097b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1098055f5ff8SMatthew Dillon  */
1099055f5ff8SMatthew Dillon static void
hammer_io_start(struct buf * bp)1100055f5ff8SMatthew Dillon hammer_io_start(struct buf *bp)
1101055f5ff8SMatthew Dillon {
1102b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
1103055f5ff8SMatthew Dillon }
1104055f5ff8SMatthew Dillon 
1105055f5ff8SMatthew Dillon /*
11067bc5b8c2SMatthew Dillon  * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
1107b33e2cc0SMatthew Dillon  *
110877912481SMatthew Dillon  * NOTE: HAMMER may modify a data buffer after we have initiated write
110977912481SMatthew Dillon  *	 I/O.
111077912481SMatthew Dillon  *
111177912481SMatthew Dillon  * NOTE: MPSAFE callback
1112b0aab9b9SMatthew Dillon  *
1113b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1114055f5ff8SMatthew Dillon  */
111566325755SMatthew Dillon static void
hammer_io_complete(struct buf * bp)111666325755SMatthew Dillon hammer_io_complete(struct buf *bp)
111766325755SMatthew Dillon {
11180ae73f43STomohiro Kusumi 	hammer_io_t io = hammer_buf_peek_io(bp);
1119ba2be8e9STomohiro Kusumi 	hammer_mount_t hmp = io->hmp;
11206b482339STomohiro Kusumi 	hammer_io_t ionext;
1121fbc6e32aSMatthew Dillon 
1122b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
1123b0aab9b9SMatthew Dillon 
1124ff66f880STomohiro Kusumi 	KKASSERT(io->released == 1);
1125055f5ff8SMatthew Dillon 
1126bf3b416bSMatthew Dillon 	/*
1127bf3b416bSMatthew Dillon 	 * Deal with people waiting for I/O to drain
1128bf3b416bSMatthew Dillon 	 */
1129ff66f880STomohiro Kusumi 	if (io->running) {
1130cdb6e4e6SMatthew Dillon 		/*
1131cdb6e4e6SMatthew Dillon 		 * Deal with critical write errors.  Once a critical error
1132cdb6e4e6SMatthew Dillon 		 * has been flagged in hmp the UNDO FIFO will not be updated.
1133cdb6e4e6SMatthew Dillon 		 * That way crash recover will give us a consistent
1134cdb6e4e6SMatthew Dillon 		 * filesystem.
1135cdb6e4e6SMatthew Dillon 		 *
1136cdb6e4e6SMatthew Dillon 		 * Because of this we can throw away failed UNDO buffers.  If
1137cdb6e4e6SMatthew Dillon 		 * we throw away META or DATA buffers we risk corrupting
1138cdb6e4e6SMatthew Dillon 		 * the now read-only version of the filesystem visible to
1139cdb6e4e6SMatthew Dillon 		 * the user.  Clear B_ERROR so the buffer is not re-dirtied
1140cdb6e4e6SMatthew Dillon 		 * by the kernel and ref the io so it doesn't get thrown
1141cdb6e4e6SMatthew Dillon 		 * away.
1142cdb6e4e6SMatthew Dillon 		 */
1143cdb6e4e6SMatthew Dillon 		if (bp->b_flags & B_ERROR) {
114477912481SMatthew Dillon 			lwkt_gettoken(&hmp->fs_token);
1145ba298df1SMatthew Dillon 			hammer_critical_error(hmp, NULL, bp->b_error,
1146cdb6e4e6SMatthew Dillon 					      "while flushing meta-data");
114777912481SMatthew Dillon 			lwkt_reltoken(&hmp->fs_token);
114877912481SMatthew Dillon 
1149ff66f880STomohiro Kusumi 			switch(io->type) {
11507fb33ff0STomohiro Kusumi 			case HAMMER_IOTYPE_UNDO_BUFFER:
1151cdb6e4e6SMatthew Dillon 				break;
1152cdb6e4e6SMatthew Dillon 			default:
1153ff66f880STomohiro Kusumi 				if (io->ioerror == 0) {
1154ff66f880STomohiro Kusumi 					io->ioerror = 1;
1155ff66f880STomohiro Kusumi 					hammer_ref(&io->lock);
1156cdb6e4e6SMatthew Dillon 				}
1157cdb6e4e6SMatthew Dillon 				break;
1158cdb6e4e6SMatthew Dillon 			}
1159cdb6e4e6SMatthew Dillon 			bp->b_flags &= ~B_ERROR;
1160cdb6e4e6SMatthew Dillon 			bundirty(bp);
1161cdb6e4e6SMatthew Dillon #if 0
1162ff66f880STomohiro Kusumi 			hammer_io_set_modlist(io);
1163ff66f880STomohiro Kusumi 			io->modified = 1;
1164cdb6e4e6SMatthew Dillon #endif
1165cdb6e4e6SMatthew Dillon 		}
1166ff66f880STomohiro Kusumi 		hammer_stats_disk_write += io->bytes;
1167ff66f880STomohiro Kusumi 		atomic_add_long(&hammer_count_io_running_write, -io->bytes);
1168ff66f880STomohiro Kusumi 		atomic_add_long(&hmp->io_running_space, -io->bytes);
1169ba298df1SMatthew Dillon 		KKASSERT(hmp->io_running_space >= 0);
1170ff66f880STomohiro Kusumi 		io->running = 0;
1171eddadaeeSMatthew Dillon 
1172eddadaeeSMatthew Dillon 		/*
1173eddadaeeSMatthew Dillon 		 * Remove from iorun list and wakeup any multi-io waiter(s).
1174eddadaeeSMatthew Dillon 		 */
1175ff66f880STomohiro Kusumi 		if (TAILQ_FIRST(&hmp->iorun_list) == io) {
1176ff66f880STomohiro Kusumi 			ionext = TAILQ_NEXT(io, iorun_entry);
11777fb33ff0STomohiro Kusumi 			if (ionext && ionext->type == HAMMER_IOTYPE_DUMMY)
1178eddadaeeSMatthew Dillon 				wakeup(ionext);
1179eddadaeeSMatthew Dillon 		}
1180ff66f880STomohiro Kusumi 		TAILQ_REMOVE(&hmp->iorun_list, io, iorun_entry);
1181ce0138a6SMatthew Dillon 	} else {
1182ff66f880STomohiro Kusumi 		hammer_stats_disk_read += io->bytes;
1183f90dde4cSMatthew Dillon 	}
1184f90dde4cSMatthew Dillon 
1185ff66f880STomohiro Kusumi 	if (io->waiting) {
1186ff66f880STomohiro Kusumi 		io->waiting = 0;
1187ff66f880STomohiro Kusumi 		wakeup(io);
1188055f5ff8SMatthew Dillon 	}
1189055f5ff8SMatthew Dillon 
1190055f5ff8SMatthew Dillon 	/*
1191bf3b416bSMatthew Dillon 	 * If B_LOCKED is set someone wanted to deallocate the bp at some
1192250aec18SMatthew Dillon 	 * point, try to do it now.  The operation will fail if there are
1193250aec18SMatthew Dillon 	 * refs or if hammer_io_deallocate() is unable to gain the
1194250aec18SMatthew Dillon 	 * interlock.
1195055f5ff8SMatthew Dillon 	 */
1196250aec18SMatthew Dillon 	if (bp->b_flags & B_LOCKED) {
1197b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, -1);
1198d5ef456eSMatthew Dillon 		bp->b_flags &= ~B_LOCKED;
1199055f5ff8SMatthew Dillon 		hammer_io_deallocate(bp);
1200055f5ff8SMatthew Dillon 		/* structure may be dead now */
1201fbc6e32aSMatthew Dillon 	}
1202b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
120366325755SMatthew Dillon }
120466325755SMatthew Dillon 
120566325755SMatthew Dillon /*
120666325755SMatthew Dillon  * Callback from kernel when it wishes to deallocate a passively
120710a5d1baSMatthew Dillon  * associated structure.  This mostly occurs with clean buffers
120810a5d1baSMatthew Dillon  * but it may be possible for a holding structure to be marked dirty
12097bc5b8c2SMatthew Dillon  * while its buffer is passively associated.  The caller owns the bp.
121066325755SMatthew Dillon  *
121166325755SMatthew Dillon  * If we cannot disassociate we set B_LOCKED to prevent the buffer
121266325755SMatthew Dillon  * from getting reused.
121346fe7ae1SMatthew Dillon  *
121446fe7ae1SMatthew Dillon  * WARNING: Because this can be called directly by getnewbuf we cannot
121546fe7ae1SMatthew Dillon  * recurse into the tree.  If a bp cannot be immediately disassociated
121646fe7ae1SMatthew Dillon  * our only recourse is to set B_LOCKED.
12177bc5b8c2SMatthew Dillon  *
12187bc5b8c2SMatthew Dillon  * WARNING: This may be called from an interrupt via hammer_io_complete()
1219b0aab9b9SMatthew Dillon  *
1220b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
122166325755SMatthew Dillon  */
122266325755SMatthew Dillon static void
hammer_io_deallocate(struct buf * bp)122366325755SMatthew Dillon hammer_io_deallocate(struct buf *bp)
122466325755SMatthew Dillon {
12250ae73f43STomohiro Kusumi 	hammer_io_t io = hammer_buf_peek_io(bp);
1226b0aab9b9SMatthew Dillon 	hammer_mount_t hmp;
1227b0aab9b9SMatthew Dillon 
1228ff66f880STomohiro Kusumi 	hmp = io->hmp;
1229b0aab9b9SMatthew Dillon 
1230b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
123166325755SMatthew Dillon 
1232ff66f880STomohiro Kusumi 	KKASSERT((bp->b_flags & B_LOCKED) == 0 && io->running == 0);
1233ff66f880STomohiro Kusumi 	if (hammer_try_interlock_norefs(&io->lock) == 0) {
1234250aec18SMatthew Dillon 		/*
1235250aec18SMatthew Dillon 		 * We cannot safely disassociate a bp from a referenced
1236250aec18SMatthew Dillon 		 * or interlocked HAMMER structure.
1237250aec18SMatthew Dillon 		 */
1238250aec18SMatthew Dillon 		bp->b_flags |= B_LOCKED;
1239b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, 1);
1240ff66f880STomohiro Kusumi 	} else if (io->modified) {
124110a5d1baSMatthew Dillon 		/*
124210a5d1baSMatthew Dillon 		 * It is not legal to disassociate a modified buffer.  This
124310a5d1baSMatthew Dillon 		 * case really shouldn't ever occur.
124410a5d1baSMatthew Dillon 		 */
1245055f5ff8SMatthew Dillon 		bp->b_flags |= B_LOCKED;
1246b0aab9b9SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, 1);
1247ff66f880STomohiro Kusumi 		hammer_put_interlock(&io->lock, 0);
1248055f5ff8SMatthew Dillon 	} else {
124910a5d1baSMatthew Dillon 		/*
125010a5d1baSMatthew Dillon 		 * Disassociate the BP.  If the io has no refs left we
1251b0aab9b9SMatthew Dillon 		 * have to add it to the loose list.  The kernel has
1252b0aab9b9SMatthew Dillon 		 * locked the buffer and therefore our io must be
1253b0aab9b9SMatthew Dillon 		 * in a released state.
125410a5d1baSMatthew Dillon 		 */
1255ff66f880STomohiro Kusumi 		hammer_io_disassociate(io);
12567fb33ff0STomohiro Kusumi 		if (io->type != HAMMER_IOTYPE_VOLUME) {
1257ff66f880STomohiro Kusumi 			KKASSERT(io->bp == NULL);
1258ff66f880STomohiro Kusumi 			KKASSERT(io->mod_root == NULL);
1259ff66f880STomohiro Kusumi 			io->mod_root = &hmp->lose_root;
1260b49481fbSTomohiro Kusumi 			if (RB_INSERT(hammer_mod_rb_tree, io->mod_root, io)) {
1261b49481fbSTomohiro Kusumi 				hpanic("duplicate entry @ %d:%015jx",
1262b49481fbSTomohiro Kusumi 					io->volume->vol_no, io->offset);
1263b49481fbSTomohiro Kusumi 				/* NOT REACHED */
1264b49481fbSTomohiro Kusumi 			}
12651afb73cfSMatthew Dillon 		}
1266ff66f880STomohiro Kusumi 		hammer_put_interlock(&io->lock, 1);
126766325755SMatthew Dillon 	}
1268b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
126966325755SMatthew Dillon }
127066325755SMatthew Dillon 
1271b0aab9b9SMatthew Dillon /*
1272b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1273b0aab9b9SMatthew Dillon  */
127466325755SMatthew Dillon static int
hammer_io_fsync(struct vnode * vp)127566325755SMatthew Dillon hammer_io_fsync(struct vnode *vp)
127666325755SMatthew Dillon {
1277b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
127866325755SMatthew Dillon 	return(0);
127966325755SMatthew Dillon }
128066325755SMatthew Dillon 
128166325755SMatthew Dillon /*
128266325755SMatthew Dillon  * NOTE: will not be called unless we tell the kernel about the
128366325755SMatthew Dillon  * bioops.  Unused... we use the mount's VFS_SYNC instead.
1284b0aab9b9SMatthew Dillon  *
1285b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
128666325755SMatthew Dillon  */
128766325755SMatthew Dillon static int
hammer_io_sync(struct mount * mp)128866325755SMatthew Dillon hammer_io_sync(struct mount *mp)
128966325755SMatthew Dillon {
1290b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
129166325755SMatthew Dillon 	return(0);
129266325755SMatthew Dillon }
129366325755SMatthew Dillon 
1294b0aab9b9SMatthew Dillon /*
1295b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1296b0aab9b9SMatthew Dillon  */
129766325755SMatthew Dillon static void
hammer_io_movedeps(struct buf * bp1,struct buf * bp2)129866325755SMatthew Dillon hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
129966325755SMatthew Dillon {
1300b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
130166325755SMatthew Dillon }
130266325755SMatthew Dillon 
130366325755SMatthew Dillon /*
130466325755SMatthew Dillon  * I/O pre-check for reading and writing.  HAMMER only uses this for
130566325755SMatthew Dillon  * B_CACHE buffers so checkread just shouldn't happen, but if it does
130666325755SMatthew Dillon  * allow it.
130766325755SMatthew Dillon  *
1308fbc6e32aSMatthew Dillon  * Writing is a different case.  We don't want the kernel to try to write
1309fbc6e32aSMatthew Dillon  * out a buffer that HAMMER may be modifying passively or which has a
131010a5d1baSMatthew Dillon  * dependancy.  In addition, kernel-demanded writes can only proceed for
131110a5d1baSMatthew Dillon  * certain types of buffers (i.e. UNDO and DATA types).  Other dirty
131210a5d1baSMatthew Dillon  * buffer types can only be explicitly written by the flusher.
1313fbc6e32aSMatthew Dillon  *
131410a5d1baSMatthew Dillon  * checkwrite will only be called for bdwrite()n buffers.  If we return
131510a5d1baSMatthew Dillon  * success the kernel is guaranteed to initiate the buffer write.
1316b0aab9b9SMatthew Dillon  *
1317b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
131866325755SMatthew Dillon  */
131966325755SMatthew Dillon static int
hammer_io_checkread(struct buf * bp)132066325755SMatthew Dillon hammer_io_checkread(struct buf *bp)
132166325755SMatthew Dillon {
1322b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
132366325755SMatthew Dillon 	return(0);
132466325755SMatthew Dillon }
132566325755SMatthew Dillon 
1326b0aab9b9SMatthew Dillon /*
132777912481SMatthew Dillon  * The kernel is asking us whether it can write out a dirty buffer or not.
132877912481SMatthew Dillon  *
1329b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
1330b0aab9b9SMatthew Dillon  */
133166325755SMatthew Dillon static int
hammer_io_checkwrite(struct buf * bp)133266325755SMatthew Dillon hammer_io_checkwrite(struct buf *bp)
133366325755SMatthew Dillon {
13340ae73f43STomohiro Kusumi 	hammer_io_t io = hammer_buf_peek_io(bp);
1335b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = io->hmp;
133666325755SMatthew Dillon 
133777062c8aSMatthew Dillon 	/*
133877062c8aSMatthew Dillon 	 * This shouldn't happen under normal operation.
133977062c8aSMatthew Dillon 	 */
1340b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
13417fb33ff0STomohiro Kusumi 	if (io->type == HAMMER_IOTYPE_VOLUME ||
13427fb33ff0STomohiro Kusumi 	    io->type == HAMMER_IOTYPE_META_BUFFER) {
134377062c8aSMatthew Dillon 		if (!panicstr)
1344903fdd05STomohiro Kusumi 			hpanic("illegal buffer");
1345a99b9ea2SMatthew Dillon 		if ((bp->b_flags & B_LOCKED) == 0) {
134677062c8aSMatthew Dillon 			bp->b_flags |= B_LOCKED;
1347b0aab9b9SMatthew Dillon 			atomic_add_int(&hammer_count_io_locked, 1);
1348a99b9ea2SMatthew Dillon 		}
1349b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
135077062c8aSMatthew Dillon 		return(1);
135177062c8aSMatthew Dillon 	}
1352c9b9e29dSMatthew Dillon 
1353fbc6e32aSMatthew Dillon 	/*
135477912481SMatthew Dillon 	 * We have to be able to interlock the IO to safely modify any
135577912481SMatthew Dillon 	 * of its fields without holding the fs_token.  If we can't lock
135677912481SMatthew Dillon 	 * it then we are racing someone.
135777912481SMatthew Dillon 	 *
135877912481SMatthew Dillon 	 * Our ownership of the bp lock prevents the io from being ripped
135977912481SMatthew Dillon 	 * out from under us.
136077912481SMatthew Dillon 	 */
136177912481SMatthew Dillon 	if (hammer_try_interlock_norefs(&io->lock) == 0) {
136277912481SMatthew Dillon 		bp->b_flags |= B_LOCKED;
136377912481SMatthew Dillon 		atomic_add_int(&hammer_count_io_locked, 1);
136477912481SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
136577912481SMatthew Dillon 		return(1);
136677912481SMatthew Dillon 	}
136777912481SMatthew Dillon 
136877912481SMatthew Dillon 	/*
136977912481SMatthew Dillon 	 * The modified bit must be cleared prior to the initiation of
137077912481SMatthew Dillon 	 * any IO (returning 0 initiates the IO).  Because this is a
137177912481SMatthew Dillon 	 * normal data buffer hammer_io_clear_modify() runs through a
137277912481SMatthew Dillon 	 * simple degenerate case.
137377912481SMatthew Dillon 	 *
137477912481SMatthew Dillon 	 * Return 0 will cause the kernel to initiate the IO, and we
137577912481SMatthew Dillon 	 * must normally clear the modified bit before we begin.  If
137677912481SMatthew Dillon 	 * the io has modify_refs we do not clear the modified bit,
137777912481SMatthew Dillon 	 * otherwise we may miss changes.
13785c8d05e2SMatthew Dillon 	 *
13795c8d05e2SMatthew Dillon 	 * Only data and undo buffers can reach here.  These buffers do
13805c8d05e2SMatthew Dillon 	 * not have terminal crc functions but we temporarily reference
13815c8d05e2SMatthew Dillon 	 * the IO anyway, just in case.
1382b33e2cc0SMatthew Dillon 	 */
13835c8d05e2SMatthew Dillon 	if (io->modify_refs == 0 && io->modified) {
13845c8d05e2SMatthew Dillon 		hammer_ref(&io->lock);
13854a2796f3SMatthew Dillon 		hammer_io_clear_modify(io, 0);
1386250aec18SMatthew Dillon 		hammer_rel(&io->lock);
13875c8d05e2SMatthew Dillon 	} else if (io->modified) {
13887fb33ff0STomohiro Kusumi 		KKASSERT(io->type == HAMMER_IOTYPE_DATA_BUFFER);
13895c8d05e2SMatthew Dillon 	}
1390f90dde4cSMatthew Dillon 
1391f90dde4cSMatthew Dillon 	/*
1392f90dde4cSMatthew Dillon 	 * The kernel is going to start the IO, set io->running.
1393f90dde4cSMatthew Dillon 	 */
1394f90dde4cSMatthew Dillon 	KKASSERT(io->running == 0);
1395f90dde4cSMatthew Dillon 	io->running = 1;
13963583bbb4SMatthew Dillon 	atomic_add_long(&io->hmp->io_running_space, io->bytes);
13973583bbb4SMatthew Dillon 	atomic_add_long(&hammer_count_io_running_write, io->bytes);
1398eddadaeeSMatthew Dillon 	TAILQ_INSERT_TAIL(&io->hmp->iorun_list, io, iorun_entry);
1399b0aab9b9SMatthew Dillon 
140077912481SMatthew Dillon 	hammer_put_interlock(&io->lock, 1);
1401b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
1402b0aab9b9SMatthew Dillon 
1403055f5ff8SMatthew Dillon 	return(0);
1404055f5ff8SMatthew Dillon }
140566325755SMatthew Dillon 
14068cd0a023SMatthew Dillon /*
140766325755SMatthew Dillon  * Return non-zero if we wish to delay the kernel's attempt to flush
140866325755SMatthew Dillon  * this buffer to disk.
1409b0aab9b9SMatthew Dillon  *
1410b0aab9b9SMatthew Dillon  * bioops callback - hold io_token
141166325755SMatthew Dillon  */
141266325755SMatthew Dillon static int
hammer_io_countdeps(struct buf * bp,int n)141366325755SMatthew Dillon hammer_io_countdeps(struct buf *bp, int n)
141466325755SMatthew Dillon {
1415b0aab9b9SMatthew Dillon 	/* nothing to do, so io_token not needed */
141666325755SMatthew Dillon 	return(0);
141766325755SMatthew Dillon }
141866325755SMatthew Dillon 
1419e397030bSTomohiro Kusumi static struct bio_ops hammer_bioops = {
142066325755SMatthew Dillon 	.io_start	= hammer_io_start,
142166325755SMatthew Dillon 	.io_complete	= hammer_io_complete,
142266325755SMatthew Dillon 	.io_deallocate	= hammer_io_deallocate,
142366325755SMatthew Dillon 	.io_fsync	= hammer_io_fsync,
142466325755SMatthew Dillon 	.io_sync	= hammer_io_sync,
142566325755SMatthew Dillon 	.io_movedeps	= hammer_io_movedeps,
142666325755SMatthew Dillon 	.io_countdeps	= hammer_io_countdeps,
142766325755SMatthew Dillon 	.io_checkread	= hammer_io_checkread,
142866325755SMatthew Dillon 	.io_checkwrite	= hammer_io_checkwrite,
142966325755SMatthew Dillon };
143066325755SMatthew Dillon 
143147637bffSMatthew Dillon /************************************************************************
143247637bffSMatthew Dillon  *				DIRECT IO OPS 				*
143347637bffSMatthew Dillon  ************************************************************************
143447637bffSMatthew Dillon  *
143547637bffSMatthew Dillon  * These functions operate directly on the buffer cache buffer associated
143647637bffSMatthew Dillon  * with a front-end vnode rather then a back-end device vnode.
143747637bffSMatthew Dillon  */
143847637bffSMatthew Dillon 
143947637bffSMatthew Dillon /*
144047637bffSMatthew Dillon  * Read a buffer associated with a front-end vnode directly from the
14411b0ab2c3SMatthew Dillon  * disk media.  The bio may be issued asynchronously.  If leaf is non-NULL
14421b0ab2c3SMatthew Dillon  * we validate the CRC.
1443a99b9ea2SMatthew Dillon  *
14441b0ab2c3SMatthew Dillon  * We must check for the presence of a HAMMER buffer to handle the case
14451b0ab2c3SMatthew Dillon  * where the reblocker has rewritten the data (which it does via the HAMMER
14461b0ab2c3SMatthew Dillon  * buffer system, not via the high-level vnode buffer cache), but not yet
14471b0ab2c3SMatthew Dillon  * committed the buffer to the media.
144847637bffSMatthew Dillon  */
144947637bffSMatthew Dillon int
hammer_io_direct_read(hammer_mount_t hmp,struct bio * bio,hammer_btree_leaf_elm_t leaf)14501b0ab2c3SMatthew Dillon hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
14511b0ab2c3SMatthew Dillon 		      hammer_btree_leaf_elm_t leaf)
145247637bffSMatthew Dillon {
14531b0ab2c3SMatthew Dillon 	hammer_off_t buf_offset;
145447637bffSMatthew Dillon 	hammer_off_t zone2_offset;
145547637bffSMatthew Dillon 	hammer_volume_t volume;
145647637bffSMatthew Dillon 	struct buf *bp;
145747637bffSMatthew Dillon 	struct bio *nbio;
145847637bffSMatthew Dillon 	int vol_no;
145947637bffSMatthew Dillon 	int error;
146047637bffSMatthew Dillon 
14611b0ab2c3SMatthew Dillon 	buf_offset = bio->bio_offset;
1462e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_large_data(buf_offset));
14634a2796f3SMatthew Dillon 
14641b0ab2c3SMatthew Dillon 	/*
14651b0ab2c3SMatthew Dillon 	 * The buffer cache may have an aliased buffer (the reblocker can
14661b0ab2c3SMatthew Dillon 	 * write them).  If it does we have to sync any dirty data before
14671b0ab2c3SMatthew Dillon 	 * we can build our direct-read.  This is a non-critical code path.
14681b0ab2c3SMatthew Dillon 	 */
14691b0ab2c3SMatthew Dillon 	bp = bio->bio_buf;
14701b0ab2c3SMatthew Dillon 	hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
14711b0ab2c3SMatthew Dillon 
14721b0ab2c3SMatthew Dillon 	/*
14731b0ab2c3SMatthew Dillon 	 * Resolve to a zone-2 offset.  The conversion just requires
14741b0ab2c3SMatthew Dillon 	 * munging the top 4 bits but we want to abstract it anyway
14751b0ab2c3SMatthew Dillon 	 * so the blockmap code can verify the zone assignment.
14761b0ab2c3SMatthew Dillon 	 */
14771b0ab2c3SMatthew Dillon 	zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
14781b0ab2c3SMatthew Dillon 	if (error)
14791b0ab2c3SMatthew Dillon 		goto done;
1480e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_raw_buffer(zone2_offset));
148143c665aeSMatthew Dillon 
14821b0ab2c3SMatthew Dillon 	/*
14831b0ab2c3SMatthew Dillon 	 * Resolve volume and raw-offset for 3rd level bio.  The
14841b0ab2c3SMatthew Dillon 	 * offset will be specific to the volume.
14851b0ab2c3SMatthew Dillon 	 */
148647637bffSMatthew Dillon 	vol_no = HAMMER_VOL_DECODE(zone2_offset);
148747637bffSMatthew Dillon 	volume = hammer_get_volume(hmp, vol_no, &error);
148847637bffSMatthew Dillon 	if (error == 0 && zone2_offset >= volume->maxbuf_off)
148947637bffSMatthew Dillon 		error = EIO;
149043c665aeSMatthew Dillon 
149147637bffSMatthew Dillon 	if (error == 0) {
1492e469566bSMatthew Dillon 		/*
149365d9d14fSTomohiro Kusumi 		 * 3rd level bio (the caller has already pushed once)
1494e469566bSMatthew Dillon 		 */
149547637bffSMatthew Dillon 		nbio = push_bio(bio);
1496516655e8STomohiro Kusumi 		nbio->bio_offset = hammer_xlate_to_phys(volume->ondisk,
1497516655e8STomohiro Kusumi 							zone2_offset);
1498ce0138a6SMatthew Dillon 		hammer_stats_disk_read += bp->b_bufsize;
149947637bffSMatthew Dillon 		vn_strategy(volume->devvp, nbio);
150047637bffSMatthew Dillon 	}
150147637bffSMatthew Dillon 	hammer_rel_volume(volume, 0);
15021b0ab2c3SMatthew Dillon done:
150347637bffSMatthew Dillon 	if (error) {
150435a5249bSTomohiro Kusumi 		hdkprintf("failed @ %016jx\n", (intmax_t)zone2_offset);
150547637bffSMatthew Dillon 		bp->b_error = error;
150647637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
150747637bffSMatthew Dillon 		biodone(bio);
150847637bffSMatthew Dillon 	}
150947637bffSMatthew Dillon 	return(error);
151047637bffSMatthew Dillon }
151147637bffSMatthew Dillon 
15129a98f3ccSMatthew Dillon /*
15139a98f3ccSMatthew Dillon  * This works similarly to hammer_io_direct_read() except instead of
15149a98f3ccSMatthew Dillon  * directly reading from the device into the bio we instead indirectly
15159a98f3ccSMatthew Dillon  * read through the device's buffer cache and then copy the data into
15169a98f3ccSMatthew Dillon  * the bio.
15179a98f3ccSMatthew Dillon  *
15189a98f3ccSMatthew Dillon  * If leaf is non-NULL and validation is enabled, the CRC will be checked.
15199a98f3ccSMatthew Dillon  *
15209a98f3ccSMatthew Dillon  * This routine also executes asynchronously.  It allows hammer strategy
15219a98f3ccSMatthew Dillon  * calls to operate asynchronously when in double_buffer mode (in addition
15229a98f3ccSMatthew Dillon  * to operating asynchronously when in normal mode).
15239a98f3ccSMatthew Dillon  */
15249a98f3ccSMatthew Dillon int
hammer_io_indirect_read(hammer_mount_t hmp,struct bio * bio,hammer_btree_leaf_elm_t leaf)15259a98f3ccSMatthew Dillon hammer_io_indirect_read(hammer_mount_t hmp, struct bio *bio,
15269a98f3ccSMatthew Dillon 			hammer_btree_leaf_elm_t leaf)
15279a98f3ccSMatthew Dillon {
15289a98f3ccSMatthew Dillon 	hammer_off_t buf_offset;
15299a98f3ccSMatthew Dillon 	hammer_off_t zone2_offset;
15309a98f3ccSMatthew Dillon 	hammer_volume_t volume;
15319a98f3ccSMatthew Dillon 	struct buf *bp;
15329a98f3ccSMatthew Dillon 	int vol_no;
15339a98f3ccSMatthew Dillon 	int error;
15349a98f3ccSMatthew Dillon 
15359a98f3ccSMatthew Dillon 	buf_offset = bio->bio_offset;
1536e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_large_data(buf_offset));
15379a98f3ccSMatthew Dillon 
15389a98f3ccSMatthew Dillon 	/*
15399a98f3ccSMatthew Dillon 	 * The buffer cache may have an aliased buffer (the reblocker can
15409a98f3ccSMatthew Dillon 	 * write them).  If it does we have to sync any dirty data before
15419a98f3ccSMatthew Dillon 	 * we can build our direct-read.  This is a non-critical code path.
15429a98f3ccSMatthew Dillon 	 */
15439a98f3ccSMatthew Dillon 	bp = bio->bio_buf;
15449a98f3ccSMatthew Dillon 	hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
15459a98f3ccSMatthew Dillon 
15469a98f3ccSMatthew Dillon 	/*
15479a98f3ccSMatthew Dillon 	 * Resolve to a zone-2 offset.  The conversion just requires
15489a98f3ccSMatthew Dillon 	 * munging the top 4 bits but we want to abstract it anyway
15499a98f3ccSMatthew Dillon 	 * so the blockmap code can verify the zone assignment.
15509a98f3ccSMatthew Dillon 	 */
15519a98f3ccSMatthew Dillon 	zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
15529a98f3ccSMatthew Dillon 	if (error)
15539a98f3ccSMatthew Dillon 		goto done;
1554e1545c47STomohiro Kusumi 	KKASSERT(hammer_is_zone_raw_buffer(zone2_offset));
15559a98f3ccSMatthew Dillon 
15569a98f3ccSMatthew Dillon 	/*
15579a98f3ccSMatthew Dillon 	 * Resolve volume and raw-offset for 3rd level bio.  The
15589a98f3ccSMatthew Dillon 	 * offset will be specific to the volume.
15599a98f3ccSMatthew Dillon 	 */
15609a98f3ccSMatthew Dillon 	vol_no = HAMMER_VOL_DECODE(zone2_offset);
15619a98f3ccSMatthew Dillon 	volume = hammer_get_volume(hmp, vol_no, &error);
15629a98f3ccSMatthew Dillon 	if (error == 0 && zone2_offset >= volume->maxbuf_off)
15639a98f3ccSMatthew Dillon 		error = EIO;
15649a98f3ccSMatthew Dillon 
15659a98f3ccSMatthew Dillon 	if (error == 0) {
15669a98f3ccSMatthew Dillon 		/*
15679a98f3ccSMatthew Dillon 		 * Convert to the raw volume->devvp offset and acquire
15689a98f3ccSMatthew Dillon 		 * the buf, issuing async I/O if necessary.
15699a98f3ccSMatthew Dillon 		 */
1570e7d75765SMatthew Dillon 		hammer_off_t limit;
1571e7d75765SMatthew Dillon 		int hce;
1572e7d75765SMatthew Dillon 
1573516655e8STomohiro Kusumi 		buf_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset);
15749a98f3ccSMatthew Dillon 
15759a98f3ccSMatthew Dillon 		if (leaf && hammer_verify_data) {
15769a98f3ccSMatthew Dillon 			bio->bio_caller_info1.uvalue32 = leaf->data_crc;
15779a98f3ccSMatthew Dillon 			bio->bio_caller_info2.index = 1;
15789a98f3ccSMatthew Dillon 		} else {
15799a98f3ccSMatthew Dillon 			bio->bio_caller_info2.index = 0;
15809a98f3ccSMatthew Dillon 		}
15814c09d9c4SMatthew Dillon 		bio->bio_caller_info3.ptr = hmp;
1582e7d75765SMatthew Dillon 
1583e7d75765SMatthew Dillon 		hce = hammer_cluster_enable;
1584e7d75765SMatthew Dillon 		if (hce > 0) {
15854eb78dd2STomohiro Kusumi 			limit = HAMMER_BIGBLOCK_DOALIGN(zone2_offset);
1586e7d75765SMatthew Dillon 			limit -= zone2_offset;
1587e7d75765SMatthew Dillon 			cluster_readcb(volume->devvp, limit, buf_offset,
1588e7d75765SMatthew Dillon 				       bp->b_bufsize,
1589*374a548aSMatthew Dillon 				       B_NOTMETA,
1590e7d75765SMatthew Dillon 				       HAMMER_CLUSTER_SIZE,
1591e7d75765SMatthew Dillon 				       HAMMER_CLUSTER_SIZE * hce,
1592e7d75765SMatthew Dillon 				       hammer_indirect_callback,
1593e7d75765SMatthew Dillon 				       bio);
1594e7d75765SMatthew Dillon 		} else {
15959a98f3ccSMatthew Dillon 			breadcb(volume->devvp, buf_offset, bp->b_bufsize,
1596*374a548aSMatthew Dillon 				B_NOTMETA,
15979a98f3ccSMatthew Dillon 				hammer_indirect_callback, bio);
15989a98f3ccSMatthew Dillon 		}
1599e7d75765SMatthew Dillon 	}
16009a98f3ccSMatthew Dillon 	hammer_rel_volume(volume, 0);
16019a98f3ccSMatthew Dillon done:
16029a98f3ccSMatthew Dillon 	if (error) {
160335a5249bSTomohiro Kusumi 		hdkprintf("failed @ %016jx\n", (intmax_t)zone2_offset);
16049a98f3ccSMatthew Dillon 		bp->b_error = error;
16059a98f3ccSMatthew Dillon 		bp->b_flags |= B_ERROR;
16069a98f3ccSMatthew Dillon 		biodone(bio);
16079a98f3ccSMatthew Dillon 	}
16089a98f3ccSMatthew Dillon 	return(error);
16099a98f3ccSMatthew Dillon }
16109a98f3ccSMatthew Dillon 
16119a98f3ccSMatthew Dillon /*
16129a98f3ccSMatthew Dillon  * Indirect callback on completion.  bio/bp specify the device-backed
16139a98f3ccSMatthew Dillon  * buffer.  bio->bio_caller_info1.ptr holds obio.
16149a98f3ccSMatthew Dillon  *
16159a98f3ccSMatthew Dillon  * obio/obp is the original regular file buffer.  obio->bio_caller_info*
16169a98f3ccSMatthew Dillon  * contains the crc specification.
16179a98f3ccSMatthew Dillon  *
16189a98f3ccSMatthew Dillon  * We are responsible for calling bpdone() and bqrelse() on bio/bp, and
16199a98f3ccSMatthew Dillon  * for calling biodone() on obio.
16209a98f3ccSMatthew Dillon  */
16219a98f3ccSMatthew Dillon static void
hammer_indirect_callback(struct bio * bio)16229a98f3ccSMatthew Dillon hammer_indirect_callback(struct bio *bio)
16239a98f3ccSMatthew Dillon {
16249a98f3ccSMatthew Dillon 	struct buf *bp = bio->bio_buf;
16259a98f3ccSMatthew Dillon 	struct buf *obp;
16269a98f3ccSMatthew Dillon 	struct bio *obio;
16274c09d9c4SMatthew Dillon 	hammer_mount_t hmp;
16289a98f3ccSMatthew Dillon 
16299a98f3ccSMatthew Dillon 	/*
16309a98f3ccSMatthew Dillon 	 * If BIO_DONE is already set the device buffer was already
16319a98f3ccSMatthew Dillon 	 * fully valid (B_CACHE).  If it is not set then I/O was issued
16329a98f3ccSMatthew Dillon 	 * and we have to run I/O completion as the last bio.
16339a98f3ccSMatthew Dillon 	 *
16349a98f3ccSMatthew Dillon 	 * Nobody is waiting for our device I/O to complete, we are
16359a98f3ccSMatthew Dillon 	 * responsible for bqrelse()ing it which means we also have to do
16369a98f3ccSMatthew Dillon 	 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
16379a98f3ccSMatthew Dillon 	 * may have set).
16389a98f3ccSMatthew Dillon 	 *
16399a98f3ccSMatthew Dillon 	 * Any preexisting device buffer should match the requested size,
1640a981af19STomohiro Kusumi 	 * but due to big-block recycling and other factors there is some
16419a98f3ccSMatthew Dillon 	 * fragility there, so we assert that the device buffer covers
16429a98f3ccSMatthew Dillon 	 * the request.
16439a98f3ccSMatthew Dillon 	 */
16449a98f3ccSMatthew Dillon 	if ((bio->bio_flags & BIO_DONE) == 0)
16459a98f3ccSMatthew Dillon 		bpdone(bp, 0);
16469a98f3ccSMatthew Dillon 	bio->bio_flags &= ~(BIO_DONE | BIO_SYNC);
16479a98f3ccSMatthew Dillon 
16489a98f3ccSMatthew Dillon 	obio = bio->bio_caller_info1.ptr;
16499a98f3ccSMatthew Dillon 	obp = obio->bio_buf;
16504c09d9c4SMatthew Dillon 	hmp = obio->bio_caller_info3.ptr;
16519a98f3ccSMatthew Dillon 
16529a98f3ccSMatthew Dillon 	if (bp->b_flags & B_ERROR) {
16534c09d9c4SMatthew Dillon 		/*
16544c09d9c4SMatthew Dillon 		 * Error from block device
16554c09d9c4SMatthew Dillon 		 */
16569a98f3ccSMatthew Dillon 		obp->b_flags |= B_ERROR;
16579a98f3ccSMatthew Dillon 		obp->b_error = bp->b_error;
16589a98f3ccSMatthew Dillon 	} else if (obio->bio_caller_info2.index &&
16599a98f3ccSMatthew Dillon 		   obio->bio_caller_info1.uvalue32 !=
16604c09d9c4SMatthew Dillon 		    hammer_datacrc(hmp->version,
16614c09d9c4SMatthew Dillon 				   bp->b_data, obp->b_bufsize) &&
16624c09d9c4SMatthew Dillon 		    obio->bio_caller_info1.uvalue32 !=
16634c09d9c4SMatthew Dillon 		    hammer_datacrc(HAMMER_VOL_VERSION_SIX,
16644c09d9c4SMatthew Dillon 				   bp->b_data, obp->b_bufsize)) {
16654c09d9c4SMatthew Dillon 		/*
16664c09d9c4SMatthew Dillon 		 * CRC error.  First check against current hammer version,
16674c09d9c4SMatthew Dillon 		 * then back-off and check against version 6 (the original
16684c09d9c4SMatthew Dillon 		 * crc).
16694c09d9c4SMatthew Dillon 		 */
16709a98f3ccSMatthew Dillon 		obp->b_flags |= B_ERROR;
16719a98f3ccSMatthew Dillon 		obp->b_error = EIO;
16729a98f3ccSMatthew Dillon 	} else {
16734c09d9c4SMatthew Dillon 		/*
16744c09d9c4SMatthew Dillon 		 * Everything is ok
16754c09d9c4SMatthew Dillon 		 */
16769a98f3ccSMatthew Dillon 		KKASSERT(bp->b_bufsize >= obp->b_bufsize);
16779a98f3ccSMatthew Dillon 		bcopy(bp->b_data, obp->b_data, obp->b_bufsize);
16789a98f3ccSMatthew Dillon 		obp->b_resid = 0;
16799a98f3ccSMatthew Dillon 		obp->b_flags |= B_AGE;
16809a98f3ccSMatthew Dillon 	}
16819a98f3ccSMatthew Dillon 	biodone(obio);
16829a98f3ccSMatthew Dillon 	bqrelse(bp);
16839a98f3ccSMatthew Dillon }
16849a98f3ccSMatthew Dillon 
168547637bffSMatthew Dillon /*
168647637bffSMatthew Dillon  * Write a buffer associated with a front-end vnode directly to the
168747637bffSMatthew Dillon  * disk media.  The bio may be issued asynchronously.
16881b0ab2c3SMatthew Dillon  *
168977912481SMatthew Dillon  * The BIO is associated with the specified record and RECG_DIRECT_IO
1690e469566bSMatthew Dillon  * is set.  The recorded is added to its object.
169147637bffSMatthew Dillon  */
169247637bffSMatthew Dillon int
hammer_io_direct_write(hammer_mount_t hmp,struct bio * bio,hammer_record_t record)16936362a262SMatthew Dillon hammer_io_direct_write(hammer_mount_t hmp, struct bio *bio,
16946362a262SMatthew Dillon 		       hammer_record_t record)
169547637bffSMatthew Dillon {
16961b0ab2c3SMatthew Dillon 	hammer_btree_leaf_elm_t leaf = &record->leaf;
16970832c9bbSMatthew Dillon 	hammer_off_t buf_offset;
169847637bffSMatthew Dillon 	hammer_off_t zone2_offset;
169947637bffSMatthew Dillon 	hammer_volume_t volume;
17000832c9bbSMatthew Dillon 	hammer_buffer_t buffer;
170147637bffSMatthew Dillon 	struct buf *bp;
170247637bffSMatthew Dillon 	struct bio *nbio;
17030832c9bbSMatthew Dillon 	char *ptr;
170447637bffSMatthew Dillon 	int vol_no;
170547637bffSMatthew Dillon 	int error;
170647637bffSMatthew Dillon 
17070832c9bbSMatthew Dillon 	buf_offset = leaf->data_offset;
17080832c9bbSMatthew Dillon 
1709f6d29b27STomohiro Kusumi 	KKASSERT(hammer_is_zone_record(buf_offset));
171047637bffSMatthew Dillon 	KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
171147637bffSMatthew Dillon 
17126362a262SMatthew Dillon 	/*
17136362a262SMatthew Dillon 	 * Issue or execute the I/O.  The new memory record must replace
17146362a262SMatthew Dillon 	 * the old one before the I/O completes, otherwise a reaquisition of
17156362a262SMatthew Dillon 	 * the buffer will load the old media data instead of the new.
17166362a262SMatthew Dillon 	 */
17170832c9bbSMatthew Dillon 	if ((buf_offset & HAMMER_BUFMASK) == 0 &&
17184a2796f3SMatthew Dillon 	    leaf->data_len >= HAMMER_BUFSIZE) {
17190832c9bbSMatthew Dillon 		/*
17200832c9bbSMatthew Dillon 		 * We are using the vnode's bio to write directly to the
17210832c9bbSMatthew Dillon 		 * media, any hammer_buffer at the same zone-X offset will
17220832c9bbSMatthew Dillon 		 * now have stale data.
17230832c9bbSMatthew Dillon 		 */
17240832c9bbSMatthew Dillon 		zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
172547637bffSMatthew Dillon 		vol_no = HAMMER_VOL_DECODE(zone2_offset);
172647637bffSMatthew Dillon 		volume = hammer_get_volume(hmp, vol_no, &error);
172747637bffSMatthew Dillon 
172847637bffSMatthew Dillon 		if (error == 0 && zone2_offset >= volume->maxbuf_off)
172947637bffSMatthew Dillon 			error = EIO;
173047637bffSMatthew Dillon 		if (error == 0) {
17310832c9bbSMatthew Dillon 			bp = bio->bio_buf;
17324a2796f3SMatthew Dillon 			KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
17331b0ab2c3SMatthew Dillon 
173443c665aeSMatthew Dillon 			/*
173543c665aeSMatthew Dillon 			 * Second level bio - cached zone2 offset.
17361b0ab2c3SMatthew Dillon 			 *
17371b0ab2c3SMatthew Dillon 			 * (We can put our bio_done function in either the
17381b0ab2c3SMatthew Dillon 			 *  2nd or 3rd level).
173943c665aeSMatthew Dillon 			 */
174047637bffSMatthew Dillon 			nbio = push_bio(bio);
174143c665aeSMatthew Dillon 			nbio->bio_offset = zone2_offset;
17421b0ab2c3SMatthew Dillon 			nbio->bio_done = hammer_io_direct_write_complete;
17431b0ab2c3SMatthew Dillon 			nbio->bio_caller_info1.ptr = record;
1744e469566bSMatthew Dillon 			record->zone2_offset = zone2_offset;
174577912481SMatthew Dillon 			record->gflags |= HAMMER_RECG_DIRECT_IO |
174677912481SMatthew Dillon 					 HAMMER_RECG_DIRECT_INVAL;
174743c665aeSMatthew Dillon 
174843c665aeSMatthew Dillon 			/*
174943c665aeSMatthew Dillon 			 * Third level bio - raw offset specific to the
175043c665aeSMatthew Dillon 			 * correct volume.
175143c665aeSMatthew Dillon 			 */
175243c665aeSMatthew Dillon 			nbio = push_bio(nbio);
1753516655e8STomohiro Kusumi 			nbio->bio_offset = hammer_xlate_to_phys(volume->ondisk,
1754516655e8STomohiro Kusumi 								zone2_offset);
1755ce0138a6SMatthew Dillon 			hammer_stats_disk_write += bp->b_bufsize;
17566362a262SMatthew Dillon 			hammer_ip_replace_bulk(hmp, record);
175747637bffSMatthew Dillon 			vn_strategy(volume->devvp, nbio);
1758748efb59SMatthew Dillon 			hammer_io_flush_mark(volume);
175947637bffSMatthew Dillon 		}
176047637bffSMatthew Dillon 		hammer_rel_volume(volume, 0);
17610832c9bbSMatthew Dillon 	} else {
17621b0ab2c3SMatthew Dillon 		/*
17631b0ab2c3SMatthew Dillon 		 * Must fit in a standard HAMMER buffer.  In this case all
176477912481SMatthew Dillon 		 * consumers use the HAMMER buffer system and RECG_DIRECT_IO
17651b0ab2c3SMatthew Dillon 		 * does not need to be set-up.
17661b0ab2c3SMatthew Dillon 		 */
17670832c9bbSMatthew Dillon 		KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
17680832c9bbSMatthew Dillon 		buffer = NULL;
17690832c9bbSMatthew Dillon 		ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
17700832c9bbSMatthew Dillon 		if (error == 0) {
17710832c9bbSMatthew Dillon 			bp = bio->bio_buf;
17727bc5b8c2SMatthew Dillon 			bp->b_flags |= B_AGE;
17730832c9bbSMatthew Dillon 			hammer_io_modify(&buffer->io, 1);
17740832c9bbSMatthew Dillon 			bcopy(bp->b_data, ptr, leaf->data_len);
17750832c9bbSMatthew Dillon 			hammer_io_modify_done(&buffer->io);
17767bc5b8c2SMatthew Dillon 			hammer_rel_buffer(buffer, 0);
17770832c9bbSMatthew Dillon 			bp->b_resid = 0;
17786362a262SMatthew Dillon 			hammer_ip_replace_bulk(hmp, record);
17790832c9bbSMatthew Dillon 			biodone(bio);
17800832c9bbSMatthew Dillon 		}
178147637bffSMatthew Dillon 	}
17826362a262SMatthew Dillon 	if (error) {
1783e469566bSMatthew Dillon 		/*
17846362a262SMatthew Dillon 		 * Major suckage occured.  Also note:  The record was
17856362a262SMatthew Dillon 		 * never added to the tree so we do not have to worry
17866362a262SMatthew Dillon 		 * about the backend.
1787e469566bSMatthew Dillon 		 */
178835a5249bSTomohiro Kusumi 		hdkprintf("failed @ %016jx\n", (intmax_t)leaf->data_offset);
178947637bffSMatthew Dillon 		bp = bio->bio_buf;
179047637bffSMatthew Dillon 		bp->b_resid = 0;
179147637bffSMatthew Dillon 		bp->b_error = EIO;
179247637bffSMatthew Dillon 		bp->b_flags |= B_ERROR;
179347637bffSMatthew Dillon 		biodone(bio);
1794e469566bSMatthew Dillon 		record->flags |= HAMMER_RECF_DELETED_FE;
1795e469566bSMatthew Dillon 		hammer_rel_mem_record(record);
179647637bffSMatthew Dillon 	}
179747637bffSMatthew Dillon 	return(error);
179847637bffSMatthew Dillon }
179947637bffSMatthew Dillon 
180043c665aeSMatthew Dillon /*
18011b0ab2c3SMatthew Dillon  * On completion of the BIO this callback must disconnect
18021b0ab2c3SMatthew Dillon  * it from the hammer_record and chain to the previous bio.
1803cdb6e4e6SMatthew Dillon  *
1804cdb6e4e6SMatthew Dillon  * An I/O error forces the mount to read-only.  Data buffers
1805cdb6e4e6SMatthew Dillon  * are not B_LOCKED like meta-data buffers are, so we have to
1806cdb6e4e6SMatthew Dillon  * throw the buffer away to prevent the kernel from retrying.
180777912481SMatthew Dillon  *
180877912481SMatthew Dillon  * NOTE: MPSAFE callback, only modify fields we have explicit
180977912481SMatthew Dillon  *	 access to (the bp and the record->gflags).
18101b0ab2c3SMatthew Dillon  */
18111b0ab2c3SMatthew Dillon static
18121b0ab2c3SMatthew Dillon void
hammer_io_direct_write_complete(struct bio * nbio)18131b0ab2c3SMatthew Dillon hammer_io_direct_write_complete(struct bio *nbio)
18141b0ab2c3SMatthew Dillon {
18151b0ab2c3SMatthew Dillon 	struct bio *obio;
1816e469566bSMatthew Dillon 	struct buf *bp;
1817b0aab9b9SMatthew Dillon 	hammer_record_t record;
1818b0aab9b9SMatthew Dillon 	hammer_mount_t hmp;
1819b0aab9b9SMatthew Dillon 
1820b0aab9b9SMatthew Dillon 	record = nbio->bio_caller_info1.ptr;
1821b0aab9b9SMatthew Dillon 	KKASSERT(record != NULL);
1822b0aab9b9SMatthew Dillon 	hmp = record->ip->hmp;
1823b0aab9b9SMatthew Dillon 
1824b0aab9b9SMatthew Dillon 	lwkt_gettoken(&hmp->io_token);
18251b0ab2c3SMatthew Dillon 
1826e469566bSMatthew Dillon 	bp = nbio->bio_buf;
18271b0ab2c3SMatthew Dillon 	obio = pop_bio(nbio);
1828e469566bSMatthew Dillon 	if (bp->b_flags & B_ERROR) {
182977912481SMatthew Dillon 		lwkt_gettoken(&hmp->fs_token);
1830653fa4cdSTomohiro Kusumi 		hammer_critical_error(hmp, record->ip, bp->b_error,
1831cdb6e4e6SMatthew Dillon 				      "while writing bulk data");
183277912481SMatthew Dillon 		lwkt_reltoken(&hmp->fs_token);
1833e469566bSMatthew Dillon 		bp->b_flags |= B_INVAL;
1834cdb6e4e6SMatthew Dillon 	}
1835e469566bSMatthew Dillon 
183677912481SMatthew Dillon 	KKASSERT(record->gflags & HAMMER_RECG_DIRECT_IO);
183777912481SMatthew Dillon 	if (record->gflags & HAMMER_RECG_DIRECT_WAIT) {
183877912481SMatthew Dillon 		record->gflags &= ~(HAMMER_RECG_DIRECT_IO |
183977912481SMatthew Dillon 				    HAMMER_RECG_DIRECT_WAIT);
1840de996e86SMatthew Dillon 		/* record can disappear once DIRECT_IO flag is cleared */
18411b0ab2c3SMatthew Dillon 		wakeup(&record->flags);
1842de996e86SMatthew Dillon 	} else {
184377912481SMatthew Dillon 		record->gflags &= ~HAMMER_RECG_DIRECT_IO;
1844de996e86SMatthew Dillon 		/* record can disappear once DIRECT_IO flag is cleared */
18451b0ab2c3SMatthew Dillon 	}
1846237baba3SMatthew Dillon 
1847b0aab9b9SMatthew Dillon 	lwkt_reltoken(&hmp->io_token);
1848237baba3SMatthew Dillon 
1849237baba3SMatthew Dillon 	biodone(obio);
18501b0ab2c3SMatthew Dillon }
18511b0ab2c3SMatthew Dillon 
18521b0ab2c3SMatthew Dillon 
18531b0ab2c3SMatthew Dillon /*
18541b0ab2c3SMatthew Dillon  * This is called before a record is either committed to the B-Tree
1855e469566bSMatthew Dillon  * or destroyed, to resolve any associated direct-IO.
18561b0ab2c3SMatthew Dillon  *
1857e469566bSMatthew Dillon  * (1) We must wait for any direct-IO related to the record to complete.
1858e469566bSMatthew Dillon  *
1859e469566bSMatthew Dillon  * (2) We must remove any buffer cache aliases for data accessed via
1860e469566bSMatthew Dillon  *     leaf->data_offset or zone2_offset so non-direct-IO consumers
1861e469566bSMatthew Dillon  *     (the mirroring and reblocking code) do not see stale data.
18621b0ab2c3SMatthew Dillon  */
18631b0ab2c3SMatthew Dillon void
hammer_io_direct_wait(hammer_record_t record)18641b0ab2c3SMatthew Dillon hammer_io_direct_wait(hammer_record_t record)
18651b0ab2c3SMatthew Dillon {
1866b0aab9b9SMatthew Dillon 	hammer_mount_t hmp = record->ip->hmp;
1867b0aab9b9SMatthew Dillon 
1868e469566bSMatthew Dillon 	/*
1869e469566bSMatthew Dillon 	 * Wait for I/O to complete
1870e469566bSMatthew Dillon 	 */
187177912481SMatthew Dillon 	if (record->gflags & HAMMER_RECG_DIRECT_IO) {
1872b0aab9b9SMatthew Dillon 		lwkt_gettoken(&hmp->io_token);
187377912481SMatthew Dillon 		while (record->gflags & HAMMER_RECG_DIRECT_IO) {
187477912481SMatthew Dillon 			record->gflags |= HAMMER_RECG_DIRECT_WAIT;
18751b0ab2c3SMatthew Dillon 			tsleep(&record->flags, 0, "hmdiow", 0);
18761b0ab2c3SMatthew Dillon 		}
1877b0aab9b9SMatthew Dillon 		lwkt_reltoken(&hmp->io_token);
18781b0ab2c3SMatthew Dillon 	}
18791b0ab2c3SMatthew Dillon 
18801b0ab2c3SMatthew Dillon 	/*
1881362ec2dcSMatthew Dillon 	 * Invalidate any related buffer cache aliases associated with the
1882362ec2dcSMatthew Dillon 	 * backing device.  This is needed because the buffer cache buffer
1883362ec2dcSMatthew Dillon 	 * for file data is associated with the file vnode, not the backing
1884362ec2dcSMatthew Dillon 	 * device vnode.
1885362ec2dcSMatthew Dillon 	 *
1886362ec2dcSMatthew Dillon 	 * XXX I do not think this case can occur any more now that
1887362ec2dcSMatthew Dillon 	 * reservations ensure that all such buffers are removed before
1888362ec2dcSMatthew Dillon 	 * an area can be reused.
1889e469566bSMatthew Dillon 	 */
189077912481SMatthew Dillon 	if (record->gflags & HAMMER_RECG_DIRECT_INVAL) {
1891e469566bSMatthew Dillon 		KKASSERT(record->leaf.data_offset);
1892b0aab9b9SMatthew Dillon 		hammer_del_buffers(hmp, record->leaf.data_offset,
1893362ec2dcSMatthew Dillon 				   record->zone2_offset, record->leaf.data_len,
1894362ec2dcSMatthew Dillon 				   1);
189577912481SMatthew Dillon 		record->gflags &= ~HAMMER_RECG_DIRECT_INVAL;
1896e469566bSMatthew Dillon 	}
1897e469566bSMatthew Dillon }
1898e469566bSMatthew Dillon 
1899e469566bSMatthew Dillon /*
190043c665aeSMatthew Dillon  * This is called to remove the second-level cached zone-2 offset from
190143c665aeSMatthew Dillon  * frontend buffer cache buffers, now stale due to a data relocation.
190243c665aeSMatthew Dillon  * These offsets are generated by cluster_read() via VOP_BMAP, or directly
190343c665aeSMatthew Dillon  * by hammer_vop_strategy_read().
190443c665aeSMatthew Dillon  *
190543c665aeSMatthew Dillon  * This is rather nasty because here we have something like the reblocker
190643c665aeSMatthew Dillon  * scanning the raw B-Tree with no held references on anything, really,
190743c665aeSMatthew Dillon  * other then a shared lock on the B-Tree node, and we have to access the
190843c665aeSMatthew Dillon  * frontend's buffer cache to check for and clean out the association.
190943c665aeSMatthew Dillon  * Specifically, if the reblocker is moving data on the disk, these cached
191043c665aeSMatthew Dillon  * offsets will become invalid.
191143c665aeSMatthew Dillon  *
191243c665aeSMatthew Dillon  * Only data record types associated with the large-data zone are subject
191343c665aeSMatthew Dillon  * to direct-io and need to be checked.
191443c665aeSMatthew Dillon  *
191543c665aeSMatthew Dillon  */
191643c665aeSMatthew Dillon void
hammer_io_direct_uncache(hammer_mount_t hmp,hammer_btree_leaf_elm_t leaf)191743c665aeSMatthew Dillon hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
191843c665aeSMatthew Dillon {
191943c665aeSMatthew Dillon 	struct hammer_inode_info iinfo;
192043c665aeSMatthew Dillon 	int zone;
192143c665aeSMatthew Dillon 
192243c665aeSMatthew Dillon 	if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
192343c665aeSMatthew Dillon 		return;
192443c665aeSMatthew Dillon 	zone = HAMMER_ZONE_DECODE(leaf->data_offset);
192543c665aeSMatthew Dillon 	if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
192643c665aeSMatthew Dillon 		return;
192743c665aeSMatthew Dillon 	iinfo.obj_id = leaf->base.obj_id;
192843c665aeSMatthew Dillon 	iinfo.obj_asof = 0;	/* unused */
192943c665aeSMatthew Dillon 	iinfo.obj_localization = leaf->base.localization &
19305a930e66SMatthew Dillon 				 HAMMER_LOCALIZE_PSEUDOFS_MASK;
193143c665aeSMatthew Dillon 	iinfo.u.leaf = leaf;
193243c665aeSMatthew Dillon 	hammer_scan_inode_snapshots(hmp, &iinfo,
193343c665aeSMatthew Dillon 				    hammer_io_direct_uncache_callback,
193443c665aeSMatthew Dillon 				    leaf);
193543c665aeSMatthew Dillon }
193643c665aeSMatthew Dillon 
193743c665aeSMatthew Dillon static int
hammer_io_direct_uncache_callback(hammer_inode_t ip,void * data)193843c665aeSMatthew Dillon hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
193943c665aeSMatthew Dillon {
194043c665aeSMatthew Dillon 	hammer_inode_info_t iinfo = data;
194143c665aeSMatthew Dillon 	hammer_off_t file_offset;
194243c665aeSMatthew Dillon 	struct vnode *vp;
194343c665aeSMatthew Dillon 	struct buf *bp;
194443c665aeSMatthew Dillon 	int blksize;
194543c665aeSMatthew Dillon 
194643c665aeSMatthew Dillon 	if (ip->vp == NULL)
194743c665aeSMatthew Dillon 		return(0);
194843c665aeSMatthew Dillon 	file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
194943c665aeSMatthew Dillon 	blksize = iinfo->u.leaf->data_len;
195043c665aeSMatthew Dillon 	KKASSERT((blksize & HAMMER_BUFMASK) == 0);
195143c665aeSMatthew Dillon 
19529c90dba2SMatthew Dillon 	/*
19539c90dba2SMatthew Dillon 	 * Warning: FINDBLK_TEST return stable storage but not stable
19549c90dba2SMatthew Dillon 	 *	    contents.  It happens to be ok in this case.
19559c90dba2SMatthew Dillon 	 */
195643c665aeSMatthew Dillon 	hammer_ref(&ip->lock);
195743c665aeSMatthew Dillon 	if (hammer_get_vnode(ip, &vp) == 0) {
1958b1c20cfaSMatthew Dillon 		if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
195943c665aeSMatthew Dillon 		    bp->b_bio2.bio_offset != NOOFFSET) {
196043c665aeSMatthew Dillon 			bp = getblk(ip->vp, file_offset, blksize, 0, 0);
196143c665aeSMatthew Dillon 			bp->b_bio2.bio_offset = NOOFFSET;
196243c665aeSMatthew Dillon 			brelse(bp);
196343c665aeSMatthew Dillon 		}
196443c665aeSMatthew Dillon 		vput(vp);
196543c665aeSMatthew Dillon 	}
196643c665aeSMatthew Dillon 	hammer_rel_inode(ip, 0);
196743c665aeSMatthew Dillon 	return(0);
196843c665aeSMatthew Dillon }
196947637bffSMatthew Dillon 
1970748efb59SMatthew Dillon 
1971748efb59SMatthew Dillon /*
1972748efb59SMatthew Dillon  * This function is called when writes may have occured on the volume,
1973748efb59SMatthew Dillon  * indicating that the device may be holding cached writes.
1974748efb59SMatthew Dillon  */
19759e6939a5STomohiro Kusumi static __inline void
hammer_io_flush_mark(hammer_volume_t volume)1976748efb59SMatthew Dillon hammer_io_flush_mark(hammer_volume_t volume)
1977748efb59SMatthew Dillon {
197877912481SMatthew Dillon 	atomic_set_int(&volume->vol_flags, HAMMER_VOLF_NEEDFLUSH);
1979748efb59SMatthew Dillon }
1980748efb59SMatthew Dillon 
1981748efb59SMatthew Dillon /*
1982748efb59SMatthew Dillon  * This function ensures that the device has flushed any cached writes out.
1983748efb59SMatthew Dillon  */
1984748efb59SMatthew Dillon void
hammer_io_flush_sync(hammer_mount_t hmp)1985748efb59SMatthew Dillon hammer_io_flush_sync(hammer_mount_t hmp)
1986748efb59SMatthew Dillon {
1987748efb59SMatthew Dillon 	hammer_volume_t volume;
1988748efb59SMatthew Dillon 	struct buf *bp_base = NULL;
1989748efb59SMatthew Dillon 	struct buf *bp;
1990748efb59SMatthew Dillon 
1991748efb59SMatthew Dillon 	RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1992748efb59SMatthew Dillon 		if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
199377912481SMatthew Dillon 			atomic_clear_int(&volume->vol_flags,
199477912481SMatthew Dillon 					 HAMMER_VOLF_NEEDFLUSH);
1995748efb59SMatthew Dillon 			bp = getpbuf(NULL);
1996748efb59SMatthew Dillon 			bp->b_bio1.bio_offset = 0;
1997748efb59SMatthew Dillon 			bp->b_bufsize = 0;
1998748efb59SMatthew Dillon 			bp->b_bcount = 0;
1999748efb59SMatthew Dillon 			bp->b_cmd = BUF_CMD_FLUSH;
2000748efb59SMatthew Dillon 			bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
2001ae8e83e6SMatthew Dillon 			bp->b_bio1.bio_done = biodone_sync;
2002ae8e83e6SMatthew Dillon 			bp->b_bio1.bio_flags |= BIO_SYNC;
2003748efb59SMatthew Dillon 			bp_base = bp;
2004748efb59SMatthew Dillon 			vn_strategy(volume->devvp, &bp->b_bio1);
2005748efb59SMatthew Dillon 		}
2006748efb59SMatthew Dillon 	}
2007748efb59SMatthew Dillon 	while ((bp = bp_base) != NULL) {
2008748efb59SMatthew Dillon 		bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
2009ae8e83e6SMatthew Dillon 		biowait(&bp->b_bio1, "hmrFLS");
2010748efb59SMatthew Dillon 		relpbuf(bp, NULL);
2011748efb59SMatthew Dillon 	}
2012748efb59SMatthew Dillon }
2013ba298df1SMatthew Dillon 
2014ba298df1SMatthew Dillon /*
2015ba298df1SMatthew Dillon  * Limit the amount of backlog which we allow to build up
2016ba298df1SMatthew Dillon  */
2017ba298df1SMatthew Dillon void
hammer_io_limit_backlog(hammer_mount_t hmp)2018ba298df1SMatthew Dillon hammer_io_limit_backlog(hammer_mount_t hmp)
2019ba298df1SMatthew Dillon {
20203038a8caSMatthew Dillon 	waitrunningbufspace();
2021ba298df1SMatthew Dillon }
2022