Home | History | Annotate | Line # | Download | only in kern
vfs_wapbl.c revision 1.69
      1 /*	$NetBSD: vfs_wapbl.c,v 1.69 2016/05/07 17:12:22 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wasabi Systems, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This implements file system independent write ahead filesystem logging.
     34  */
     35 
     36 #define WAPBL_INTERNAL
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: vfs_wapbl.c,v 1.69 2016/05/07 17:12:22 riastradh Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/bitops.h>
     43 #include <sys/time.h>
     44 #include <sys/wapbl.h>
     45 #include <sys/wapbl_replay.h>
     46 
     47 #ifdef _KERNEL
     48 
     49 #include <sys/atomic.h>
     50 #include <sys/conf.h>
     51 #include <sys/file.h>
     52 #include <sys/kauth.h>
     53 #include <sys/kernel.h>
     54 #include <sys/module.h>
     55 #include <sys/mount.h>
     56 #include <sys/mutex.h>
     57 #include <sys/namei.h>
     58 #include <sys/proc.h>
     59 #include <sys/resourcevar.h>
     60 #include <sys/sysctl.h>
     61 #include <sys/uio.h>
     62 #include <sys/vnode.h>
     63 
     64 #include <miscfs/specfs/specdev.h>
     65 
     66 #define	wapbl_alloc(s) kmem_alloc((s), KM_SLEEP)
     67 #define	wapbl_free(a, s) kmem_free((a), (s))
     68 #define	wapbl_calloc(n, s) kmem_zalloc((n)*(s), KM_SLEEP)
     69 
     70 static struct sysctllog *wapbl_sysctl;
     71 static int wapbl_flush_disk_cache = 1;
     72 static int wapbl_verbose_commit = 0;
     73 
     74 static inline size_t wapbl_space_free(size_t, off_t, off_t);
     75 
     76 #else /* !_KERNEL */
     77 
     78 #include <assert.h>
     79 #include <errno.h>
     80 #include <stdbool.h>
     81 #include <stdio.h>
     82 #include <stdlib.h>
     83 #include <string.h>
     84 
     85 #define	KDASSERT(x) assert(x)
     86 #define	KASSERT(x) assert(x)
     87 #define	wapbl_alloc(s) malloc(s)
     88 #define	wapbl_free(a, s) free(a)
     89 #define	wapbl_calloc(n, s) calloc((n), (s))
     90 
     91 #endif /* !_KERNEL */
     92 
     93 /*
     94  * INTERNAL DATA STRUCTURES
     95  */
     96 
     97 /*
     98  * This structure holds per-mount log information.
     99  *
    100  * Legend:	a = atomic access only
    101  *		r = read-only after init
    102  *		l = rwlock held
    103  *		m = mutex held
    104  *		lm = rwlock held writing or mutex held
    105  *		u = unlocked access ok
    106  *		b = bufcache_lock held
    107  */
    108 LIST_HEAD(wapbl_ino_head, wapbl_ino);
    109 struct wapbl {
    110 	struct vnode *wl_logvp;	/* r:	log here */
    111 	struct vnode *wl_devvp;	/* r:	log on this device */
    112 	struct mount *wl_mount;	/* r:	mountpoint wl is associated with */
    113 	daddr_t wl_logpbn;	/* r:	Physical block number of start of log */
    114 	int wl_log_dev_bshift;	/* r:	logarithm of device block size of log
    115 					device */
    116 	int wl_fs_dev_bshift;	/* r:	logarithm of device block size of
    117 					filesystem device */
    118 
    119 	unsigned wl_lock_count;	/* m:	Count of transactions in progress */
    120 
    121 	size_t wl_circ_size; 	/* r:	Number of bytes in buffer of log */
    122 	size_t wl_circ_off;	/* r:	Number of bytes reserved at start */
    123 
    124 	size_t wl_bufcount_max;	/* r:	Number of buffers reserved for log */
    125 	size_t wl_bufbytes_max;	/* r:	Number of buf bytes reserved for log */
    126 
    127 	off_t wl_head;		/* l:	Byte offset of log head */
    128 	off_t wl_tail;		/* l:	Byte offset of log tail */
    129 	/*
    130 	 * head == tail == 0 means log is empty
    131 	 * head == tail != 0 means log is full
    132 	 * see assertions in wapbl_advance() for other boundary conditions.
    133 	 * only truncate moves the tail, except when flush sets it to
    134 	 * wl_header_size only flush moves the head, except when truncate
    135 	 * sets it to 0.
    136 	 */
    137 
    138 	struct wapbl_wc_header *wl_wc_header;	/* l	*/
    139 	void *wl_wc_scratch;	/* l:	scratch space (XXX: por que?!?) */
    140 
    141 	kmutex_t wl_mtx;	/* u:	short-term lock */
    142 	krwlock_t wl_rwlock;	/* u:	File system transaction lock */
    143 
    144 	/*
    145 	 * Must be held while accessing
    146 	 * wl_count or wl_bufs or head or tail
    147 	 */
    148 
    149 	/*
    150 	 * Callback called from within the flush routine to flush any extra
    151 	 * bits.  Note that flush may be skipped without calling this if
    152 	 * there are no outstanding buffers in the transaction.
    153 	 */
    154 #if _KERNEL
    155 	wapbl_flush_fn_t wl_flush;	/* r	*/
    156 	wapbl_flush_fn_t wl_flush_abort;/* r	*/
    157 #endif
    158 
    159 	size_t wl_bufbytes;	/* m:	Byte count of pages in wl_bufs */
    160 	size_t wl_bufcount;	/* m:	Count of buffers in wl_bufs */
    161 	size_t wl_bcount;	/* m:	Total bcount of wl_bufs */
    162 
    163 	LIST_HEAD(, buf) wl_bufs; /* m:	Buffers in current transaction */
    164 
    165 	kcondvar_t wl_reclaimable_cv;	/* m (obviously) */
    166 	size_t wl_reclaimable_bytes; /* m:	Amount of space available for
    167 						reclamation by truncate */
    168 	int wl_error_count;	/* m:	# of wl_entries with errors */
    169 	size_t wl_reserved_bytes; /* never truncate log smaller than this */
    170 
    171 #ifdef WAPBL_DEBUG_BUFBYTES
    172 	size_t wl_unsynced_bufbytes; /* Byte count of unsynced buffers */
    173 #endif
    174 
    175 	daddr_t *wl_deallocblks;/* lm:	address of block */
    176 	int *wl_dealloclens;	/* lm:	size of block */
    177 	int wl_dealloccnt;	/* lm:	total count */
    178 	int wl_dealloclim;	/* l:	max count */
    179 
    180 	/* hashtable of inode numbers for allocated but unlinked inodes */
    181 	/* synch ??? */
    182 	struct wapbl_ino_head *wl_inohash;
    183 	u_long wl_inohashmask;
    184 	int wl_inohashcnt;
    185 
    186 	SIMPLEQ_HEAD(, wapbl_entry) wl_entries; /* On disk transaction
    187 						   accounting */
    188 
    189 	u_char *wl_buffer;	/* l:   buffer for wapbl_buffered_write() */
    190 	daddr_t wl_buffer_dblk;	/* l:   buffer disk block address */
    191 	size_t wl_buffer_used;	/* l:   buffer current use */
    192 };
    193 
    194 #ifdef WAPBL_DEBUG_PRINT
    195 int wapbl_debug_print = WAPBL_DEBUG_PRINT;
    196 #endif
    197 
    198 /****************************************************************/
    199 #ifdef _KERNEL
    200 
    201 #ifdef WAPBL_DEBUG
    202 struct wapbl *wapbl_debug_wl;
    203 #endif
    204 
    205 static int wapbl_write_commit(struct wapbl *wl, off_t head, off_t tail);
    206 static int wapbl_write_blocks(struct wapbl *wl, off_t *offp);
    207 static int wapbl_write_revocations(struct wapbl *wl, off_t *offp);
    208 static int wapbl_write_inodes(struct wapbl *wl, off_t *offp);
    209 #endif /* _KERNEL */
    210 
    211 static int wapbl_replay_process(struct wapbl_replay *wr, off_t, off_t);
    212 
    213 static inline size_t wapbl_space_used(size_t avail, off_t head,
    214 	off_t tail);
    215 
    216 #ifdef _KERNEL
    217 
    218 static struct pool wapbl_entry_pool;
    219 
    220 #define	WAPBL_INODETRK_SIZE 83
    221 static int wapbl_ino_pool_refcount;
    222 static struct pool wapbl_ino_pool;
    223 struct wapbl_ino {
    224 	LIST_ENTRY(wapbl_ino) wi_hash;
    225 	ino_t wi_ino;
    226 	mode_t wi_mode;
    227 };
    228 
    229 static void wapbl_inodetrk_init(struct wapbl *wl, u_int size);
    230 static void wapbl_inodetrk_free(struct wapbl *wl);
    231 static struct wapbl_ino *wapbl_inodetrk_get(struct wapbl *wl, ino_t ino);
    232 
    233 static size_t wapbl_transaction_len(struct wapbl *wl);
    234 static inline size_t wapbl_transaction_inodes_len(struct wapbl *wl);
    235 
    236 #if 0
    237 int wapbl_replay_verify(struct wapbl_replay *, struct vnode *);
    238 #endif
    239 
    240 static int wapbl_replay_isopen1(struct wapbl_replay *);
    241 
    242 /*
    243  * This is useful for debugging.  If set, the log will
    244  * only be truncated when necessary.
    245  */
    246 int wapbl_lazy_truncate = 0;
    247 
    248 struct wapbl_ops wapbl_ops = {
    249 	.wo_wapbl_discard	= wapbl_discard,
    250 	.wo_wapbl_replay_isopen	= wapbl_replay_isopen1,
    251 	.wo_wapbl_replay_can_read = wapbl_replay_can_read,
    252 	.wo_wapbl_replay_read	= wapbl_replay_read,
    253 	.wo_wapbl_add_buf	= wapbl_add_buf,
    254 	.wo_wapbl_remove_buf	= wapbl_remove_buf,
    255 	.wo_wapbl_resize_buf	= wapbl_resize_buf,
    256 	.wo_wapbl_begin		= wapbl_begin,
    257 	.wo_wapbl_end		= wapbl_end,
    258 	.wo_wapbl_junlock_assert= wapbl_junlock_assert,
    259 
    260 	/* XXX: the following is only used to say "this is a wapbl buf" */
    261 	.wo_wapbl_biodone	= wapbl_biodone,
    262 };
    263 
    264 static int
    265 wapbl_sysctl_init(void)
    266 {
    267 	int rv;
    268 	const struct sysctlnode *rnode, *cnode;
    269 
    270 	wapbl_sysctl = NULL;
    271 
    272 	rv = sysctl_createv(&wapbl_sysctl, 0, NULL, &rnode,
    273 		       CTLFLAG_PERMANENT,
    274 		       CTLTYPE_NODE, "wapbl",
    275 		       SYSCTL_DESCR("WAPBL journaling options"),
    276 		       NULL, 0, NULL, 0,
    277 		       CTL_VFS, CTL_CREATE, CTL_EOL);
    278 	if (rv)
    279 		return rv;
    280 
    281 	rv = sysctl_createv(&wapbl_sysctl, 0, &rnode, &cnode,
    282 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    283 		       CTLTYPE_INT, "flush_disk_cache",
    284 		       SYSCTL_DESCR("flush disk cache"),
    285 		       NULL, 0, &wapbl_flush_disk_cache, 0,
    286 		       CTL_CREATE, CTL_EOL);
    287 	if (rv)
    288 		return rv;
    289 
    290 	rv = sysctl_createv(&wapbl_sysctl, 0, &rnode, &cnode,
    291 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    292 		       CTLTYPE_INT, "verbose_commit",
    293 		       SYSCTL_DESCR("show time and size of wapbl log commits"),
    294 		       NULL, 0, &wapbl_verbose_commit, 0,
    295 		       CTL_CREATE, CTL_EOL);
    296 	return rv;
    297 }
    298 
    299 static void
    300 wapbl_init(void)
    301 {
    302 
    303 	pool_init(&wapbl_entry_pool, sizeof(struct wapbl_entry), 0, 0, 0,
    304 	    "wapblentrypl", &pool_allocator_kmem, IPL_VM);
    305 
    306 	wapbl_sysctl_init();
    307 }
    308 
    309 static int
    310 wapbl_fini(bool interface)
    311 {
    312 
    313 	if (wapbl_sysctl != NULL)
    314 		 sysctl_teardown(&wapbl_sysctl);
    315 
    316 	pool_destroy(&wapbl_entry_pool);
    317 
    318 	return 0;
    319 }
    320 
    321 static int
    322 wapbl_start_flush_inodes(struct wapbl *wl, struct wapbl_replay *wr)
    323 {
    324 	int error, i;
    325 
    326 	WAPBL_PRINTF(WAPBL_PRINT_REPLAY,
    327 	    ("wapbl_start: reusing log with %d inodes\n", wr->wr_inodescnt));
    328 
    329 	/*
    330 	 * Its only valid to reuse the replay log if its
    331 	 * the same as the new log we just opened.
    332 	 */
    333 	KDASSERT(!wapbl_replay_isopen(wr));
    334 	KASSERT(wl->wl_devvp->v_type == VBLK);
    335 	KASSERT(wr->wr_devvp->v_type == VBLK);
    336 	KASSERT(wl->wl_devvp->v_rdev == wr->wr_devvp->v_rdev);
    337 	KASSERT(wl->wl_logpbn == wr->wr_logpbn);
    338 	KASSERT(wl->wl_circ_size == wr->wr_circ_size);
    339 	KASSERT(wl->wl_circ_off == wr->wr_circ_off);
    340 	KASSERT(wl->wl_log_dev_bshift == wr->wr_log_dev_bshift);
    341 	KASSERT(wl->wl_fs_dev_bshift == wr->wr_fs_dev_bshift);
    342 
    343 	wl->wl_wc_header->wc_generation = wr->wr_generation + 1;
    344 
    345 	for (i = 0; i < wr->wr_inodescnt; i++)
    346 		wapbl_register_inode(wl, wr->wr_inodes[i].wr_inumber,
    347 		    wr->wr_inodes[i].wr_imode);
    348 
    349 	/* Make sure new transaction won't overwrite old inodes list */
    350 	KDASSERT(wapbl_transaction_len(wl) <=
    351 	    wapbl_space_free(wl->wl_circ_size, wr->wr_inodeshead,
    352 	    wr->wr_inodestail));
    353 
    354 	wl->wl_head = wl->wl_tail = wr->wr_inodeshead;
    355 	wl->wl_reclaimable_bytes = wl->wl_reserved_bytes =
    356 	    wapbl_transaction_len(wl);
    357 
    358 	error = wapbl_write_inodes(wl, &wl->wl_head);
    359 	if (error)
    360 		return error;
    361 
    362 	KASSERT(wl->wl_head != wl->wl_tail);
    363 	KASSERT(wl->wl_head != 0);
    364 
    365 	return 0;
    366 }
    367 
    368 int
    369 wapbl_start(struct wapbl ** wlp, struct mount *mp, struct vnode *vp,
    370 	daddr_t off, size_t count, size_t blksize, struct wapbl_replay *wr,
    371 	wapbl_flush_fn_t flushfn, wapbl_flush_fn_t flushabortfn)
    372 {
    373 	struct wapbl *wl;
    374 	struct vnode *devvp;
    375 	daddr_t logpbn;
    376 	int error;
    377 	int log_dev_bshift = ilog2(blksize);
    378 	int fs_dev_bshift = log_dev_bshift;
    379 	int run;
    380 
    381 	WAPBL_PRINTF(WAPBL_PRINT_OPEN, ("wapbl_start: vp=%p off=%" PRId64
    382 	    " count=%zu blksize=%zu\n", vp, off, count, blksize));
    383 
    384 	if (log_dev_bshift > fs_dev_bshift) {
    385 		WAPBL_PRINTF(WAPBL_PRINT_OPEN,
    386 			("wapbl: log device's block size cannot be larger "
    387 			 "than filesystem's\n"));
    388 		/*
    389 		 * Not currently implemented, although it could be if
    390 		 * needed someday.
    391 		 */
    392 		return ENOSYS;
    393 	}
    394 
    395 	if (off < 0)
    396 		return EINVAL;
    397 
    398 	if (blksize < DEV_BSIZE)
    399 		return EINVAL;
    400 	if (blksize % DEV_BSIZE)
    401 		return EINVAL;
    402 
    403 	/* XXXTODO: verify that the full load is writable */
    404 
    405 	/*
    406 	 * XXX check for minimum log size
    407 	 * minimum is governed by minimum amount of space
    408 	 * to complete a transaction. (probably truncate)
    409 	 */
    410 	/* XXX for now pick something minimal */
    411 	if ((count * blksize) < MAXPHYS) {
    412 		return ENOSPC;
    413 	}
    414 
    415 	if ((error = VOP_BMAP(vp, off, &devvp, &logpbn, &run)) != 0) {
    416 		return error;
    417 	}
    418 
    419 	wl = wapbl_calloc(1, sizeof(*wl));
    420 	rw_init(&wl->wl_rwlock);
    421 	mutex_init(&wl->wl_mtx, MUTEX_DEFAULT, IPL_NONE);
    422 	cv_init(&wl->wl_reclaimable_cv, "wapblrec");
    423 	LIST_INIT(&wl->wl_bufs);
    424 	SIMPLEQ_INIT(&wl->wl_entries);
    425 
    426 	wl->wl_logvp = vp;
    427 	wl->wl_devvp = devvp;
    428 	wl->wl_mount = mp;
    429 	wl->wl_logpbn = logpbn;
    430 	wl->wl_log_dev_bshift = log_dev_bshift;
    431 	wl->wl_fs_dev_bshift = fs_dev_bshift;
    432 
    433 	wl->wl_flush = flushfn;
    434 	wl->wl_flush_abort = flushabortfn;
    435 
    436 	/* Reserve two log device blocks for the commit headers */
    437 	wl->wl_circ_off = 2<<wl->wl_log_dev_bshift;
    438 	wl->wl_circ_size = ((count * blksize) - wl->wl_circ_off);
    439 	/* truncate the log usage to a multiple of log_dev_bshift */
    440 	wl->wl_circ_size >>= wl->wl_log_dev_bshift;
    441 	wl->wl_circ_size <<= wl->wl_log_dev_bshift;
    442 
    443 	/*
    444 	 * wl_bufbytes_max limits the size of the in memory transaction space.
    445 	 * - Since buffers are allocated and accounted for in units of
    446 	 *   PAGE_SIZE it is required to be a multiple of PAGE_SIZE
    447 	 *   (i.e. 1<<PAGE_SHIFT)
    448 	 * - Since the log device has to be written in units of
    449 	 *   1<<wl_log_dev_bshift it is required to be a mulitple of
    450 	 *   1<<wl_log_dev_bshift.
    451 	 * - Since filesystem will provide data in units of 1<<wl_fs_dev_bshift,
    452 	 *   it is convenient to be a multiple of 1<<wl_fs_dev_bshift.
    453 	 * Therefore it must be multiple of the least common multiple of those
    454 	 * three quantities.  Fortunately, all of those quantities are
    455 	 * guaranteed to be a power of two, and the least common multiple of
    456 	 * a set of numbers which are all powers of two is simply the maximum
    457 	 * of those numbers.  Finally, the maximum logarithm of a power of two
    458 	 * is the same as the log of the maximum power of two.  So we can do
    459 	 * the following operations to size wl_bufbytes_max:
    460 	 */
    461 
    462 	/* XXX fix actual number of pages reserved per filesystem. */
    463 	wl->wl_bufbytes_max = MIN(wl->wl_circ_size, buf_memcalc() / 2);
    464 
    465 	/* Round wl_bufbytes_max to the largest power of two constraint */
    466 	wl->wl_bufbytes_max >>= PAGE_SHIFT;
    467 	wl->wl_bufbytes_max <<= PAGE_SHIFT;
    468 	wl->wl_bufbytes_max >>= wl->wl_log_dev_bshift;
    469 	wl->wl_bufbytes_max <<= wl->wl_log_dev_bshift;
    470 	wl->wl_bufbytes_max >>= wl->wl_fs_dev_bshift;
    471 	wl->wl_bufbytes_max <<= wl->wl_fs_dev_bshift;
    472 
    473 	/* XXX maybe use filesystem fragment size instead of 1024 */
    474 	/* XXX fix actual number of buffers reserved per filesystem. */
    475 	wl->wl_bufcount_max = (nbuf / 2) * 1024;
    476 
    477 	/* XXX tie this into resource estimation */
    478 	wl->wl_dealloclim = wl->wl_bufbytes_max / mp->mnt_stat.f_bsize / 2;
    479 
    480 	wl->wl_deallocblks = wapbl_alloc(sizeof(*wl->wl_deallocblks) *
    481 	    wl->wl_dealloclim);
    482 	wl->wl_dealloclens = wapbl_alloc(sizeof(*wl->wl_dealloclens) *
    483 	    wl->wl_dealloclim);
    484 
    485 	wl->wl_buffer = wapbl_alloc(MAXPHYS);
    486 	wl->wl_buffer_used = 0;
    487 
    488 	wapbl_inodetrk_init(wl, WAPBL_INODETRK_SIZE);
    489 
    490 	/* Initialize the commit header */
    491 	{
    492 		struct wapbl_wc_header *wc;
    493 		size_t len = 1 << wl->wl_log_dev_bshift;
    494 		wc = wapbl_calloc(1, len);
    495 		wc->wc_type = WAPBL_WC_HEADER;
    496 		wc->wc_len = len;
    497 		wc->wc_circ_off = wl->wl_circ_off;
    498 		wc->wc_circ_size = wl->wl_circ_size;
    499 		/* XXX wc->wc_fsid */
    500 		wc->wc_log_dev_bshift = wl->wl_log_dev_bshift;
    501 		wc->wc_fs_dev_bshift = wl->wl_fs_dev_bshift;
    502 		wl->wl_wc_header = wc;
    503 		wl->wl_wc_scratch = wapbl_alloc(len);
    504 	}
    505 
    506 	/*
    507 	 * if there was an existing set of unlinked but
    508 	 * allocated inodes, preserve it in the new
    509 	 * log.
    510 	 */
    511 	if (wr && wr->wr_inodescnt) {
    512 		error = wapbl_start_flush_inodes(wl, wr);
    513 		if (error)
    514 			goto errout;
    515 	}
    516 
    517 	error = wapbl_write_commit(wl, wl->wl_head, wl->wl_tail);
    518 	if (error) {
    519 		goto errout;
    520 	}
    521 
    522 	*wlp = wl;
    523 #if defined(WAPBL_DEBUG)
    524 	wapbl_debug_wl = wl;
    525 #endif
    526 
    527 	return 0;
    528  errout:
    529 	wapbl_discard(wl);
    530 	wapbl_free(wl->wl_wc_scratch, wl->wl_wc_header->wc_len);
    531 	wapbl_free(wl->wl_wc_header, wl->wl_wc_header->wc_len);
    532 	wapbl_free(wl->wl_deallocblks,
    533 	    sizeof(*wl->wl_deallocblks) * wl->wl_dealloclim);
    534 	wapbl_free(wl->wl_dealloclens,
    535 	    sizeof(*wl->wl_dealloclens) * wl->wl_dealloclim);
    536 	wapbl_free(wl->wl_buffer, MAXPHYS);
    537 	wapbl_inodetrk_free(wl);
    538 	wapbl_free(wl, sizeof(*wl));
    539 
    540 	return error;
    541 }
    542 
    543 /*
    544  * Like wapbl_flush, only discards the transaction
    545  * completely
    546  */
    547 
    548 void
    549 wapbl_discard(struct wapbl *wl)
    550 {
    551 	struct wapbl_entry *we;
    552 	struct buf *bp;
    553 	int i;
    554 
    555 	/*
    556 	 * XXX we may consider using upgrade here
    557 	 * if we want to call flush from inside a transaction
    558 	 */
    559 	rw_enter(&wl->wl_rwlock, RW_WRITER);
    560 	wl->wl_flush(wl->wl_mount, wl->wl_deallocblks, wl->wl_dealloclens,
    561 	    wl->wl_dealloccnt);
    562 
    563 #ifdef WAPBL_DEBUG_PRINT
    564 	{
    565 		pid_t pid = -1;
    566 		lwpid_t lid = -1;
    567 		if (curproc)
    568 			pid = curproc->p_pid;
    569 		if (curlwp)
    570 			lid = curlwp->l_lid;
    571 #ifdef WAPBL_DEBUG_BUFBYTES
    572 		WAPBL_PRINTF(WAPBL_PRINT_DISCARD,
    573 		    ("wapbl_discard: thread %d.%d discarding "
    574 		    "transaction\n"
    575 		    "\tbufcount=%zu bufbytes=%zu bcount=%zu "
    576 		    "deallocs=%d inodes=%d\n"
    577 		    "\terrcnt = %u, reclaimable=%zu reserved=%zu "
    578 		    "unsynced=%zu\n",
    579 		    pid, lid, wl->wl_bufcount, wl->wl_bufbytes,
    580 		    wl->wl_bcount, wl->wl_dealloccnt,
    581 		    wl->wl_inohashcnt, wl->wl_error_count,
    582 		    wl->wl_reclaimable_bytes, wl->wl_reserved_bytes,
    583 		    wl->wl_unsynced_bufbytes));
    584 		SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
    585 			WAPBL_PRINTF(WAPBL_PRINT_DISCARD,
    586 			    ("\tentry: bufcount = %zu, reclaimable = %zu, "
    587 			     "error = %d, unsynced = %zu\n",
    588 			     we->we_bufcount, we->we_reclaimable_bytes,
    589 			     we->we_error, we->we_unsynced_bufbytes));
    590 		}
    591 #else /* !WAPBL_DEBUG_BUFBYTES */
    592 		WAPBL_PRINTF(WAPBL_PRINT_DISCARD,
    593 		    ("wapbl_discard: thread %d.%d discarding transaction\n"
    594 		    "\tbufcount=%zu bufbytes=%zu bcount=%zu "
    595 		    "deallocs=%d inodes=%d\n"
    596 		    "\terrcnt = %u, reclaimable=%zu reserved=%zu\n",
    597 		    pid, lid, wl->wl_bufcount, wl->wl_bufbytes,
    598 		    wl->wl_bcount, wl->wl_dealloccnt,
    599 		    wl->wl_inohashcnt, wl->wl_error_count,
    600 		    wl->wl_reclaimable_bytes, wl->wl_reserved_bytes));
    601 		SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
    602 			WAPBL_PRINTF(WAPBL_PRINT_DISCARD,
    603 			    ("\tentry: bufcount = %zu, reclaimable = %zu, "
    604 			     "error = %d\n",
    605 			     we->we_bufcount, we->we_reclaimable_bytes,
    606 			     we->we_error));
    607 		}
    608 #endif /* !WAPBL_DEBUG_BUFBYTES */
    609 	}
    610 #endif /* WAPBL_DEBUG_PRINT */
    611 
    612 	for (i = 0; i <= wl->wl_inohashmask; i++) {
    613 		struct wapbl_ino_head *wih;
    614 		struct wapbl_ino *wi;
    615 
    616 		wih = &wl->wl_inohash[i];
    617 		while ((wi = LIST_FIRST(wih)) != NULL) {
    618 			LIST_REMOVE(wi, wi_hash);
    619 			pool_put(&wapbl_ino_pool, wi);
    620 			KASSERT(wl->wl_inohashcnt > 0);
    621 			wl->wl_inohashcnt--;
    622 		}
    623 	}
    624 
    625 	/*
    626 	 * clean buffer list
    627 	 */
    628 	mutex_enter(&bufcache_lock);
    629 	mutex_enter(&wl->wl_mtx);
    630 	while ((bp = LIST_FIRST(&wl->wl_bufs)) != NULL) {
    631 		if (bbusy(bp, 0, 0, &wl->wl_mtx) == 0) {
    632 			/*
    633 			 * The buffer will be unlocked and
    634 			 * removed from the transaction in brelse
    635 			 */
    636 			mutex_exit(&wl->wl_mtx);
    637 			brelsel(bp, 0);
    638 			mutex_enter(&wl->wl_mtx);
    639 		}
    640 	}
    641 	mutex_exit(&wl->wl_mtx);
    642 	mutex_exit(&bufcache_lock);
    643 
    644 	/*
    645 	 * Remove references to this wl from wl_entries, free any which
    646 	 * no longer have buffers, others will be freed in wapbl_biodone
    647 	 * when they no longer have any buffers.
    648 	 */
    649 	while ((we = SIMPLEQ_FIRST(&wl->wl_entries)) != NULL) {
    650 		SIMPLEQ_REMOVE_HEAD(&wl->wl_entries, we_entries);
    651 		/* XXX should we be accumulating wl_error_count
    652 		 * and increasing reclaimable bytes ? */
    653 		we->we_wapbl = NULL;
    654 		if (we->we_bufcount == 0) {
    655 #ifdef WAPBL_DEBUG_BUFBYTES
    656 			KASSERT(we->we_unsynced_bufbytes == 0);
    657 #endif
    658 			pool_put(&wapbl_entry_pool, we);
    659 		}
    660 	}
    661 
    662 	/* Discard list of deallocs */
    663 	wl->wl_dealloccnt = 0;
    664 	/* XXX should we clear wl_reserved_bytes? */
    665 
    666 	KASSERT(wl->wl_bufbytes == 0);
    667 	KASSERT(wl->wl_bcount == 0);
    668 	KASSERT(wl->wl_bufcount == 0);
    669 	KASSERT(LIST_EMPTY(&wl->wl_bufs));
    670 	KASSERT(SIMPLEQ_EMPTY(&wl->wl_entries));
    671 	KASSERT(wl->wl_inohashcnt == 0);
    672 
    673 	rw_exit(&wl->wl_rwlock);
    674 }
    675 
    676 int
    677 wapbl_stop(struct wapbl *wl, int force)
    678 {
    679 	int error;
    680 
    681 	WAPBL_PRINTF(WAPBL_PRINT_OPEN, ("wapbl_stop called\n"));
    682 	error = wapbl_flush(wl, 1);
    683 	if (error) {
    684 		if (force)
    685 			wapbl_discard(wl);
    686 		else
    687 			return error;
    688 	}
    689 
    690 	/* Unlinked inodes persist after a flush */
    691 	if (wl->wl_inohashcnt) {
    692 		if (force) {
    693 			wapbl_discard(wl);
    694 		} else {
    695 			return EBUSY;
    696 		}
    697 	}
    698 
    699 	KASSERT(wl->wl_bufbytes == 0);
    700 	KASSERT(wl->wl_bcount == 0);
    701 	KASSERT(wl->wl_bufcount == 0);
    702 	KASSERT(LIST_EMPTY(&wl->wl_bufs));
    703 	KASSERT(wl->wl_dealloccnt == 0);
    704 	KASSERT(SIMPLEQ_EMPTY(&wl->wl_entries));
    705 	KASSERT(wl->wl_inohashcnt == 0);
    706 
    707 	wapbl_free(wl->wl_wc_scratch, wl->wl_wc_header->wc_len);
    708 	wapbl_free(wl->wl_wc_header, wl->wl_wc_header->wc_len);
    709 	wapbl_free(wl->wl_deallocblks,
    710 	    sizeof(*wl->wl_deallocblks) * wl->wl_dealloclim);
    711 	wapbl_free(wl->wl_dealloclens,
    712 	    sizeof(*wl->wl_dealloclens) * wl->wl_dealloclim);
    713 	wapbl_free(wl->wl_buffer, MAXPHYS);
    714 	wapbl_inodetrk_free(wl);
    715 
    716 	cv_destroy(&wl->wl_reclaimable_cv);
    717 	mutex_destroy(&wl->wl_mtx);
    718 	rw_destroy(&wl->wl_rwlock);
    719 	wapbl_free(wl, sizeof(*wl));
    720 
    721 	return 0;
    722 }
    723 
    724 static int
    725 wapbl_doio(void *data, size_t len, struct vnode *devvp, daddr_t pbn, int flags)
    726 {
    727 	struct pstats *pstats = curlwp->l_proc->p_stats;
    728 	struct buf *bp;
    729 	int error;
    730 
    731 	KASSERT((flags & ~(B_WRITE | B_READ)) == 0);
    732 	KASSERT(devvp->v_type == VBLK);
    733 
    734 	if ((flags & (B_WRITE | B_READ)) == B_WRITE) {
    735 		mutex_enter(devvp->v_interlock);
    736 		devvp->v_numoutput++;
    737 		mutex_exit(devvp->v_interlock);
    738 		pstats->p_ru.ru_oublock++;
    739 	} else {
    740 		pstats->p_ru.ru_inblock++;
    741 	}
    742 
    743 	bp = getiobuf(devvp, true);
    744 	bp->b_flags = flags;
    745 	bp->b_cflags = BC_BUSY; /* silly & dubious */
    746 	bp->b_dev = devvp->v_rdev;
    747 	bp->b_data = data;
    748 	bp->b_bufsize = bp->b_resid = bp->b_bcount = len;
    749 	bp->b_blkno = pbn;
    750 	BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    751 
    752 	WAPBL_PRINTF(WAPBL_PRINT_IO,
    753 	    ("wapbl_doio: %s %d bytes at block %"PRId64" on dev 0x%"PRIx64"\n",
    754 	    BUF_ISWRITE(bp) ? "write" : "read", bp->b_bcount,
    755 	    bp->b_blkno, bp->b_dev));
    756 
    757 	VOP_STRATEGY(devvp, bp);
    758 
    759 	error = biowait(bp);
    760 	putiobuf(bp);
    761 
    762 	if (error) {
    763 		WAPBL_PRINTF(WAPBL_PRINT_ERROR,
    764 		    ("wapbl_doio: %s %zu bytes at block %" PRId64
    765 		    " on dev 0x%"PRIx64" failed with error %d\n",
    766 		    (((flags & (B_WRITE | B_READ)) == B_WRITE) ?
    767 		     "write" : "read"),
    768 		    len, pbn, devvp->v_rdev, error));
    769 	}
    770 
    771 	return error;
    772 }
    773 
    774 int
    775 wapbl_write(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
    776 {
    777 
    778 	return wapbl_doio(data, len, devvp, pbn, B_WRITE);
    779 }
    780 
    781 int
    782 wapbl_read(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
    783 {
    784 
    785 	return wapbl_doio(data, len, devvp, pbn, B_READ);
    786 }
    787 
    788 /*
    789  * Flush buffered data if any.
    790  */
    791 static int
    792 wapbl_buffered_flush(struct wapbl *wl)
    793 {
    794 	int error;
    795 
    796 	if (wl->wl_buffer_used == 0)
    797 		return 0;
    798 
    799 	error = wapbl_doio(wl->wl_buffer, wl->wl_buffer_used,
    800 	    wl->wl_devvp, wl->wl_buffer_dblk, B_WRITE);
    801 	wl->wl_buffer_used = 0;
    802 
    803 	return error;
    804 }
    805 
    806 /*
    807  * Write data to the log.
    808  * Try to coalesce writes and emit MAXPHYS aligned blocks.
    809  */
    810 static int
    811 wapbl_buffered_write(void *data, size_t len, struct wapbl *wl, daddr_t pbn)
    812 {
    813 	int error;
    814 	size_t resid;
    815 
    816 	/*
    817 	 * If not adjacent to buffered data flush first.  Disk block
    818 	 * address is always valid for non-empty buffer.
    819 	 */
    820 	if (wl->wl_buffer_used > 0 &&
    821 	    pbn != wl->wl_buffer_dblk + btodb(wl->wl_buffer_used)) {
    822 		error = wapbl_buffered_flush(wl);
    823 		if (error)
    824 			return error;
    825 	}
    826 	/*
    827 	 * If this write goes to an empty buffer we have to
    828 	 * save the disk block address first.
    829 	 */
    830 	if (wl->wl_buffer_used == 0)
    831 		wl->wl_buffer_dblk = pbn;
    832 	/*
    833 	 * Remaining space so this buffer ends on a MAXPHYS boundary.
    834 	 *
    835 	 * Cannot become less or equal zero as the buffer would have been
    836 	 * flushed on the last call then.
    837 	 */
    838 	resid = MAXPHYS - dbtob(wl->wl_buffer_dblk % btodb(MAXPHYS)) -
    839 	    wl->wl_buffer_used;
    840 	KASSERT(resid > 0);
    841 	KASSERT(dbtob(btodb(resid)) == resid);
    842 	if (len >= resid) {
    843 		memcpy(wl->wl_buffer + wl->wl_buffer_used, data, resid);
    844 		wl->wl_buffer_used += resid;
    845 		error = wapbl_doio(wl->wl_buffer, wl->wl_buffer_used,
    846 		    wl->wl_devvp, wl->wl_buffer_dblk, B_WRITE);
    847 		data = (uint8_t *)data + resid;
    848 		len -= resid;
    849 		wl->wl_buffer_dblk = pbn + btodb(resid);
    850 		wl->wl_buffer_used = 0;
    851 		if (error)
    852 			return error;
    853 	}
    854 	KASSERT(len < MAXPHYS);
    855 	if (len > 0) {
    856 		memcpy(wl->wl_buffer + wl->wl_buffer_used, data, len);
    857 		wl->wl_buffer_used += len;
    858 	}
    859 
    860 	return 0;
    861 }
    862 
    863 /*
    864  * Off is byte offset returns new offset for next write
    865  * handles log wraparound
    866  */
    867 static int
    868 wapbl_circ_write(struct wapbl *wl, void *data, size_t len, off_t *offp)
    869 {
    870 	size_t slen;
    871 	off_t off = *offp;
    872 	int error;
    873 	daddr_t pbn;
    874 
    875 	KDASSERT(((len >> wl->wl_log_dev_bshift) <<
    876 	    wl->wl_log_dev_bshift) == len);
    877 
    878 	if (off < wl->wl_circ_off)
    879 		off = wl->wl_circ_off;
    880 	slen = wl->wl_circ_off + wl->wl_circ_size - off;
    881 	if (slen < len) {
    882 		pbn = wl->wl_logpbn + (off >> wl->wl_log_dev_bshift);
    883 #ifdef _KERNEL
    884 		pbn = btodb(pbn << wl->wl_log_dev_bshift);
    885 #endif
    886 		error = wapbl_buffered_write(data, slen, wl, pbn);
    887 		if (error)
    888 			return error;
    889 		data = (uint8_t *)data + slen;
    890 		len -= slen;
    891 		off = wl->wl_circ_off;
    892 	}
    893 	pbn = wl->wl_logpbn + (off >> wl->wl_log_dev_bshift);
    894 #ifdef _KERNEL
    895 	pbn = btodb(pbn << wl->wl_log_dev_bshift);
    896 #endif
    897 	error = wapbl_buffered_write(data, len, wl, pbn);
    898 	if (error)
    899 		return error;
    900 	off += len;
    901 	if (off >= wl->wl_circ_off + wl->wl_circ_size)
    902 		off = wl->wl_circ_off;
    903 	*offp = off;
    904 	return 0;
    905 }
    906 
    907 /****************************************************************/
    908 
    909 int
    910 wapbl_begin(struct wapbl *wl, const char *file, int line)
    911 {
    912 	int doflush;
    913 	unsigned lockcount;
    914 
    915 	KDASSERT(wl);
    916 
    917 	/*
    918 	 * XXX this needs to be made much more sophisticated.
    919 	 * perhaps each wapbl_begin could reserve a specified
    920 	 * number of buffers and bytes.
    921 	 */
    922 	mutex_enter(&wl->wl_mtx);
    923 	lockcount = wl->wl_lock_count;
    924 	doflush = ((wl->wl_bufbytes + (lockcount * MAXPHYS)) >
    925 		   wl->wl_bufbytes_max / 2) ||
    926 		  ((wl->wl_bufcount + (lockcount * 10)) >
    927 		   wl->wl_bufcount_max / 2) ||
    928 		  (wapbl_transaction_len(wl) > wl->wl_circ_size / 2) ||
    929 		  (wl->wl_dealloccnt >= (wl->wl_dealloclim / 2));
    930 	mutex_exit(&wl->wl_mtx);
    931 
    932 	if (doflush) {
    933 		WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
    934 		    ("force flush lockcnt=%d bufbytes=%zu "
    935 		    "(max=%zu) bufcount=%zu (max=%zu) "
    936 		    "dealloccnt %d (lim=%d)\n",
    937 		    lockcount, wl->wl_bufbytes,
    938 		    wl->wl_bufbytes_max, wl->wl_bufcount,
    939 		    wl->wl_bufcount_max,
    940 		    wl->wl_dealloccnt, wl->wl_dealloclim));
    941 	}
    942 
    943 	if (doflush) {
    944 		int error = wapbl_flush(wl, 0);
    945 		if (error)
    946 			return error;
    947 	}
    948 
    949 	rw_enter(&wl->wl_rwlock, RW_READER);
    950 	mutex_enter(&wl->wl_mtx);
    951 	wl->wl_lock_count++;
    952 	mutex_exit(&wl->wl_mtx);
    953 
    954 #if defined(WAPBL_DEBUG_PRINT)
    955 	WAPBL_PRINTF(WAPBL_PRINT_TRANSACTION,
    956 	    ("wapbl_begin thread %d.%d with bufcount=%zu "
    957 	    "bufbytes=%zu bcount=%zu at %s:%d\n",
    958 	    curproc->p_pid, curlwp->l_lid, wl->wl_bufcount,
    959 	    wl->wl_bufbytes, wl->wl_bcount, file, line));
    960 #endif
    961 
    962 	return 0;
    963 }
    964 
    965 void
    966 wapbl_end(struct wapbl *wl)
    967 {
    968 
    969 #if defined(WAPBL_DEBUG_PRINT)
    970 	WAPBL_PRINTF(WAPBL_PRINT_TRANSACTION,
    971 	     ("wapbl_end thread %d.%d with bufcount=%zu "
    972 	      "bufbytes=%zu bcount=%zu\n",
    973 	      curproc->p_pid, curlwp->l_lid, wl->wl_bufcount,
    974 	      wl->wl_bufbytes, wl->wl_bcount));
    975 #endif
    976 
    977 	/*
    978 	 * XXX this could be handled more gracefully, perhaps place
    979 	 * only a partial transaction in the log and allow the
    980 	 * remaining to flush without the protection of the journal.
    981 	 */
    982 	KASSERTMSG((wapbl_transaction_len(wl) <=
    983 		(wl->wl_circ_size - wl->wl_reserved_bytes)),
    984 	    "wapbl_end: current transaction too big to flush");
    985 
    986 	mutex_enter(&wl->wl_mtx);
    987 	KASSERT(wl->wl_lock_count > 0);
    988 	wl->wl_lock_count--;
    989 	mutex_exit(&wl->wl_mtx);
    990 
    991 	rw_exit(&wl->wl_rwlock);
    992 }
    993 
    994 void
    995 wapbl_add_buf(struct wapbl *wl, struct buf * bp)
    996 {
    997 
    998 	KASSERT(bp->b_cflags & BC_BUSY);
    999 	KASSERT(bp->b_vp);
   1000 
   1001 	wapbl_jlock_assert(wl);
   1002 
   1003 #if 0
   1004 	/*
   1005 	 * XXX this might be an issue for swapfiles.
   1006 	 * see uvm_swap.c:1702
   1007 	 *
   1008 	 * XXX2 why require it then?  leap of semantics?
   1009 	 */
   1010 	KASSERT((bp->b_cflags & BC_NOCACHE) == 0);
   1011 #endif
   1012 
   1013 	mutex_enter(&wl->wl_mtx);
   1014 	if (bp->b_flags & B_LOCKED) {
   1015 		LIST_REMOVE(bp, b_wapbllist);
   1016 		WAPBL_PRINTF(WAPBL_PRINT_BUFFER2,
   1017 		   ("wapbl_add_buf thread %d.%d re-adding buf %p "
   1018 		    "with %d bytes %d bcount\n",
   1019 		    curproc->p_pid, curlwp->l_lid, bp, bp->b_bufsize,
   1020 		    bp->b_bcount));
   1021 	} else {
   1022 		/* unlocked by dirty buffers shouldn't exist */
   1023 		KASSERT(!(bp->b_oflags & BO_DELWRI));
   1024 		wl->wl_bufbytes += bp->b_bufsize;
   1025 		wl->wl_bcount += bp->b_bcount;
   1026 		wl->wl_bufcount++;
   1027 		WAPBL_PRINTF(WAPBL_PRINT_BUFFER,
   1028 		   ("wapbl_add_buf thread %d.%d adding buf %p "
   1029 		    "with %d bytes %d bcount\n",
   1030 		    curproc->p_pid, curlwp->l_lid, bp, bp->b_bufsize,
   1031 		    bp->b_bcount));
   1032 	}
   1033 	LIST_INSERT_HEAD(&wl->wl_bufs, bp, b_wapbllist);
   1034 	mutex_exit(&wl->wl_mtx);
   1035 
   1036 	bp->b_flags |= B_LOCKED;
   1037 }
   1038 
   1039 static void
   1040 wapbl_remove_buf_locked(struct wapbl * wl, struct buf *bp)
   1041 {
   1042 
   1043 	KASSERT(mutex_owned(&wl->wl_mtx));
   1044 	KASSERT(bp->b_cflags & BC_BUSY);
   1045 	wapbl_jlock_assert(wl);
   1046 
   1047 #if 0
   1048 	/*
   1049 	 * XXX this might be an issue for swapfiles.
   1050 	 * see uvm_swap.c:1725
   1051 	 *
   1052 	 * XXXdeux: see above
   1053 	 */
   1054 	KASSERT((bp->b_flags & BC_NOCACHE) == 0);
   1055 #endif
   1056 	KASSERT(bp->b_flags & B_LOCKED);
   1057 
   1058 	WAPBL_PRINTF(WAPBL_PRINT_BUFFER,
   1059 	   ("wapbl_remove_buf thread %d.%d removing buf %p with "
   1060 	    "%d bytes %d bcount\n",
   1061 	    curproc->p_pid, curlwp->l_lid, bp, bp->b_bufsize, bp->b_bcount));
   1062 
   1063 	KASSERT(wl->wl_bufbytes >= bp->b_bufsize);
   1064 	wl->wl_bufbytes -= bp->b_bufsize;
   1065 	KASSERT(wl->wl_bcount >= bp->b_bcount);
   1066 	wl->wl_bcount -= bp->b_bcount;
   1067 	KASSERT(wl->wl_bufcount > 0);
   1068 	wl->wl_bufcount--;
   1069 	KASSERT((wl->wl_bufcount == 0) == (wl->wl_bufbytes == 0));
   1070 	KASSERT((wl->wl_bufcount == 0) == (wl->wl_bcount == 0));
   1071 	LIST_REMOVE(bp, b_wapbllist);
   1072 
   1073 	bp->b_flags &= ~B_LOCKED;
   1074 }
   1075 
   1076 /* called from brelsel() in vfs_bio among other places */
   1077 void
   1078 wapbl_remove_buf(struct wapbl * wl, struct buf *bp)
   1079 {
   1080 
   1081 	mutex_enter(&wl->wl_mtx);
   1082 	wapbl_remove_buf_locked(wl, bp);
   1083 	mutex_exit(&wl->wl_mtx);
   1084 }
   1085 
   1086 void
   1087 wapbl_resize_buf(struct wapbl *wl, struct buf *bp, long oldsz, long oldcnt)
   1088 {
   1089 
   1090 	KASSERT(bp->b_cflags & BC_BUSY);
   1091 
   1092 	/*
   1093 	 * XXX: why does this depend on B_LOCKED?  otherwise the buf
   1094 	 * is not for a transaction?  if so, why is this called in the
   1095 	 * first place?
   1096 	 */
   1097 	if (bp->b_flags & B_LOCKED) {
   1098 		mutex_enter(&wl->wl_mtx);
   1099 		wl->wl_bufbytes += bp->b_bufsize - oldsz;
   1100 		wl->wl_bcount += bp->b_bcount - oldcnt;
   1101 		mutex_exit(&wl->wl_mtx);
   1102 	}
   1103 }
   1104 
   1105 #endif /* _KERNEL */
   1106 
   1107 /****************************************************************/
   1108 /* Some utility inlines */
   1109 
   1110 static inline size_t
   1111 wapbl_space_used(size_t avail, off_t head, off_t tail)
   1112 {
   1113 
   1114 	if (tail == 0) {
   1115 		KASSERT(head == 0);
   1116 		return 0;
   1117 	}
   1118 	return ((head + (avail - 1) - tail) % avail) + 1;
   1119 }
   1120 
   1121 #ifdef _KERNEL
   1122 /* This is used to advance the pointer at old to new value at old+delta */
   1123 static inline off_t
   1124 wapbl_advance(size_t size, size_t off, off_t oldoff, size_t delta)
   1125 {
   1126 	off_t newoff;
   1127 
   1128 	/* Define acceptable ranges for inputs. */
   1129 	KASSERT(delta <= (size_t)size);
   1130 	KASSERT((oldoff == 0) || ((size_t)oldoff >= off));
   1131 	KASSERT(oldoff < (off_t)(size + off));
   1132 
   1133 	if ((oldoff == 0) && (delta != 0))
   1134 		newoff = off + delta;
   1135 	else if ((oldoff + delta) < (size + off))
   1136 		newoff = oldoff + delta;
   1137 	else
   1138 		newoff = (oldoff + delta) - size;
   1139 
   1140 	/* Note some interesting axioms */
   1141 	KASSERT((delta != 0) || (newoff == oldoff));
   1142 	KASSERT((delta == 0) || (newoff != 0));
   1143 	KASSERT((delta != (size)) || (newoff == oldoff));
   1144 
   1145 	/* Define acceptable ranges for output. */
   1146 	KASSERT((newoff == 0) || ((size_t)newoff >= off));
   1147 	KASSERT((size_t)newoff < (size + off));
   1148 	return newoff;
   1149 }
   1150 
   1151 static inline size_t
   1152 wapbl_space_free(size_t avail, off_t head, off_t tail)
   1153 {
   1154 
   1155 	return avail - wapbl_space_used(avail, head, tail);
   1156 }
   1157 
   1158 static inline void
   1159 wapbl_advance_head(size_t size, size_t off, size_t delta, off_t *headp,
   1160 		   off_t *tailp)
   1161 {
   1162 	off_t head = *headp;
   1163 	off_t tail = *tailp;
   1164 
   1165 	KASSERT(delta <= wapbl_space_free(size, head, tail));
   1166 	head = wapbl_advance(size, off, head, delta);
   1167 	if ((tail == 0) && (head != 0))
   1168 		tail = off;
   1169 	*headp = head;
   1170 	*tailp = tail;
   1171 }
   1172 
   1173 static inline void
   1174 wapbl_advance_tail(size_t size, size_t off, size_t delta, off_t *headp,
   1175 		   off_t *tailp)
   1176 {
   1177 	off_t head = *headp;
   1178 	off_t tail = *tailp;
   1179 
   1180 	KASSERT(delta <= wapbl_space_used(size, head, tail));
   1181 	tail = wapbl_advance(size, off, tail, delta);
   1182 	if (head == tail) {
   1183 		head = tail = 0;
   1184 	}
   1185 	*headp = head;
   1186 	*tailp = tail;
   1187 }
   1188 
   1189 
   1190 /****************************************************************/
   1191 
   1192 /*
   1193  * Remove transactions whose buffers are completely flushed to disk.
   1194  * Will block until at least minfree space is available.
   1195  * only intended to be called from inside wapbl_flush and therefore
   1196  * does not protect against commit races with itself or with flush.
   1197  */
   1198 static int
   1199 wapbl_truncate(struct wapbl *wl, size_t minfree, int waitonly)
   1200 {
   1201 	size_t delta;
   1202 	size_t avail;
   1203 	off_t head;
   1204 	off_t tail;
   1205 	int error = 0;
   1206 
   1207 	KASSERT(minfree <= (wl->wl_circ_size - wl->wl_reserved_bytes));
   1208 	KASSERT(rw_write_held(&wl->wl_rwlock));
   1209 
   1210 	mutex_enter(&wl->wl_mtx);
   1211 
   1212 	/*
   1213 	 * First check to see if we have to do a commit
   1214 	 * at all.
   1215 	 */
   1216 	avail = wapbl_space_free(wl->wl_circ_size, wl->wl_head, wl->wl_tail);
   1217 	if (minfree < avail) {
   1218 		mutex_exit(&wl->wl_mtx);
   1219 		return 0;
   1220 	}
   1221 	minfree -= avail;
   1222 	while ((wl->wl_error_count == 0) &&
   1223 	    (wl->wl_reclaimable_bytes < minfree)) {
   1224         	WAPBL_PRINTF(WAPBL_PRINT_TRUNCATE,
   1225                    ("wapbl_truncate: sleeping on %p wl=%p bytes=%zd "
   1226 		    "minfree=%zd\n",
   1227                     &wl->wl_reclaimable_bytes, wl, wl->wl_reclaimable_bytes,
   1228 		    minfree));
   1229 
   1230 		cv_wait(&wl->wl_reclaimable_cv, &wl->wl_mtx);
   1231 	}
   1232 	if (wl->wl_reclaimable_bytes < minfree) {
   1233 		KASSERT(wl->wl_error_count);
   1234 		/* XXX maybe get actual error from buffer instead someday? */
   1235 		error = EIO;
   1236 	}
   1237 	head = wl->wl_head;
   1238 	tail = wl->wl_tail;
   1239 	delta = wl->wl_reclaimable_bytes;
   1240 
   1241 	/* If all of of the entries are flushed, then be sure to keep
   1242 	 * the reserved bytes reserved.  Watch out for discarded transactions,
   1243 	 * which could leave more bytes reserved than are reclaimable.
   1244 	 */
   1245 	if (SIMPLEQ_EMPTY(&wl->wl_entries) &&
   1246 	    (delta >= wl->wl_reserved_bytes)) {
   1247 		delta -= wl->wl_reserved_bytes;
   1248 	}
   1249 	wapbl_advance_tail(wl->wl_circ_size, wl->wl_circ_off, delta, &head,
   1250 			   &tail);
   1251 	KDASSERT(wl->wl_reserved_bytes <=
   1252 		wapbl_space_used(wl->wl_circ_size, head, tail));
   1253 	mutex_exit(&wl->wl_mtx);
   1254 
   1255 	if (error)
   1256 		return error;
   1257 
   1258 	if (waitonly)
   1259 		return 0;
   1260 
   1261 	/*
   1262 	 * This is where head, tail and delta are unprotected
   1263 	 * from races against itself or flush.  This is ok since
   1264 	 * we only call this routine from inside flush itself.
   1265 	 *
   1266 	 * XXX: how can it race against itself when accessed only
   1267 	 * from behind the write-locked rwlock?
   1268 	 */
   1269 	error = wapbl_write_commit(wl, head, tail);
   1270 	if (error)
   1271 		return error;
   1272 
   1273 	wl->wl_head = head;
   1274 	wl->wl_tail = tail;
   1275 
   1276 	mutex_enter(&wl->wl_mtx);
   1277 	KASSERT(wl->wl_reclaimable_bytes >= delta);
   1278 	wl->wl_reclaimable_bytes -= delta;
   1279 	mutex_exit(&wl->wl_mtx);
   1280 	WAPBL_PRINTF(WAPBL_PRINT_TRUNCATE,
   1281 	    ("wapbl_truncate thread %d.%d truncating %zu bytes\n",
   1282 	    curproc->p_pid, curlwp->l_lid, delta));
   1283 
   1284 	return 0;
   1285 }
   1286 
   1287 /****************************************************************/
   1288 
   1289 void
   1290 wapbl_biodone(struct buf *bp)
   1291 {
   1292 	struct wapbl_entry *we = bp->b_private;
   1293 	struct wapbl *wl = we->we_wapbl;
   1294 #ifdef WAPBL_DEBUG_BUFBYTES
   1295 	const int bufsize = bp->b_bufsize;
   1296 #endif
   1297 
   1298 	/*
   1299 	 * Handle possible flushing of buffers after log has been
   1300 	 * decomissioned.
   1301 	 */
   1302 	if (!wl) {
   1303 		KASSERT(we->we_bufcount > 0);
   1304 		we->we_bufcount--;
   1305 #ifdef WAPBL_DEBUG_BUFBYTES
   1306 		KASSERT(we->we_unsynced_bufbytes >= bufsize);
   1307 		we->we_unsynced_bufbytes -= bufsize;
   1308 #endif
   1309 
   1310 		if (we->we_bufcount == 0) {
   1311 #ifdef WAPBL_DEBUG_BUFBYTES
   1312 			KASSERT(we->we_unsynced_bufbytes == 0);
   1313 #endif
   1314 			pool_put(&wapbl_entry_pool, we);
   1315 		}
   1316 
   1317 		brelse(bp, 0);
   1318 		return;
   1319 	}
   1320 
   1321 #ifdef ohbother
   1322 	KDASSERT(bp->b_oflags & BO_DONE);
   1323 	KDASSERT(!(bp->b_oflags & BO_DELWRI));
   1324 	KDASSERT(bp->b_flags & B_ASYNC);
   1325 	KDASSERT(bp->b_cflags & BC_BUSY);
   1326 	KDASSERT(!(bp->b_flags & B_LOCKED));
   1327 	KDASSERT(!(bp->b_flags & B_READ));
   1328 	KDASSERT(!(bp->b_cflags & BC_INVAL));
   1329 	KDASSERT(!(bp->b_cflags & BC_NOCACHE));
   1330 #endif
   1331 
   1332 	if (bp->b_error) {
   1333 #ifdef notyet /* Can't currently handle possible dirty buffer reuse */
   1334 		/*
   1335 		 * XXXpooka: interfaces not fully updated
   1336 		 * Note: this was not enabled in the original patch
   1337 		 * against netbsd4 either.  I don't know if comment
   1338 		 * above is true or not.
   1339 		 */
   1340 
   1341 		/*
   1342 		 * If an error occurs, report the error and leave the
   1343 		 * buffer as a delayed write on the LRU queue.
   1344 		 * restarting the write would likely result in
   1345 		 * an error spinloop, so let it be done harmlessly
   1346 		 * by the syncer.
   1347 		 */
   1348 		bp->b_flags &= ~(B_DONE);
   1349 		simple_unlock(&bp->b_interlock);
   1350 
   1351 		if (we->we_error == 0) {
   1352 			mutex_enter(&wl->wl_mtx);
   1353 			wl->wl_error_count++;
   1354 			mutex_exit(&wl->wl_mtx);
   1355 			cv_broadcast(&wl->wl_reclaimable_cv);
   1356 		}
   1357 		we->we_error = bp->b_error;
   1358 		bp->b_error = 0;
   1359 		brelse(bp);
   1360 		return;
   1361 #else
   1362 		/* For now, just mark the log permanently errored out */
   1363 
   1364 		mutex_enter(&wl->wl_mtx);
   1365 		if (wl->wl_error_count == 0) {
   1366 			wl->wl_error_count++;
   1367 			cv_broadcast(&wl->wl_reclaimable_cv);
   1368 		}
   1369 		mutex_exit(&wl->wl_mtx);
   1370 #endif
   1371 	}
   1372 
   1373 	/*
   1374 	 * Release the buffer here. wapbl_flush() may wait for the
   1375 	 * log to become empty and we better unbusy the buffer before
   1376 	 * wapbl_flush() returns.
   1377 	 */
   1378 	brelse(bp, 0);
   1379 
   1380 	mutex_enter(&wl->wl_mtx);
   1381 
   1382 	KASSERT(we->we_bufcount > 0);
   1383 	we->we_bufcount--;
   1384 #ifdef WAPBL_DEBUG_BUFBYTES
   1385 	KASSERT(we->we_unsynced_bufbytes >= bufsize);
   1386 	we->we_unsynced_bufbytes -= bufsize;
   1387 	KASSERT(wl->wl_unsynced_bufbytes >= bufsize);
   1388 	wl->wl_unsynced_bufbytes -= bufsize;
   1389 #endif
   1390 
   1391 	/*
   1392 	 * If the current transaction can be reclaimed, start
   1393 	 * at the beginning and reclaim any consecutive reclaimable
   1394 	 * transactions.  If we successfully reclaim anything,
   1395 	 * then wakeup anyone waiting for the reclaim.
   1396 	 */
   1397 	if (we->we_bufcount == 0) {
   1398 		size_t delta = 0;
   1399 		int errcnt = 0;
   1400 #ifdef WAPBL_DEBUG_BUFBYTES
   1401 		KDASSERT(we->we_unsynced_bufbytes == 0);
   1402 #endif
   1403 		/*
   1404 		 * clear any posted error, since the buffer it came from
   1405 		 * has successfully flushed by now
   1406 		 */
   1407 		while ((we = SIMPLEQ_FIRST(&wl->wl_entries)) &&
   1408 		       (we->we_bufcount == 0)) {
   1409 			delta += we->we_reclaimable_bytes;
   1410 			if (we->we_error)
   1411 				errcnt++;
   1412 			SIMPLEQ_REMOVE_HEAD(&wl->wl_entries, we_entries);
   1413 			pool_put(&wapbl_entry_pool, we);
   1414 		}
   1415 
   1416 		if (delta) {
   1417 			wl->wl_reclaimable_bytes += delta;
   1418 			KASSERT(wl->wl_error_count >= errcnt);
   1419 			wl->wl_error_count -= errcnt;
   1420 			cv_broadcast(&wl->wl_reclaimable_cv);
   1421 		}
   1422 	}
   1423 
   1424 	mutex_exit(&wl->wl_mtx);
   1425 }
   1426 
   1427 /*
   1428  * Write transactions to disk + start I/O for contents
   1429  */
   1430 int
   1431 wapbl_flush(struct wapbl *wl, int waitfor)
   1432 {
   1433 	struct buf *bp;
   1434 	struct wapbl_entry *we;
   1435 	off_t off;
   1436 	off_t head;
   1437 	off_t tail;
   1438 	size_t delta = 0;
   1439 	size_t flushsize;
   1440 	size_t reserved;
   1441 	int error = 0;
   1442 
   1443 	/*
   1444 	 * Do a quick check to see if a full flush can be skipped
   1445 	 * This assumes that the flush callback does not need to be called
   1446 	 * unless there are other outstanding bufs.
   1447 	 */
   1448 	if (!waitfor) {
   1449 		size_t nbufs;
   1450 		mutex_enter(&wl->wl_mtx);	/* XXX need mutex here to
   1451 						   protect the KASSERTS */
   1452 		nbufs = wl->wl_bufcount;
   1453 		KASSERT((wl->wl_bufcount == 0) == (wl->wl_bufbytes == 0));
   1454 		KASSERT((wl->wl_bufcount == 0) == (wl->wl_bcount == 0));
   1455 		mutex_exit(&wl->wl_mtx);
   1456 		if (nbufs == 0)
   1457 			return 0;
   1458 	}
   1459 
   1460 	/*
   1461 	 * XXX we may consider using LK_UPGRADE here
   1462 	 * if we want to call flush from inside a transaction
   1463 	 */
   1464 	rw_enter(&wl->wl_rwlock, RW_WRITER);
   1465 	wl->wl_flush(wl->wl_mount, wl->wl_deallocblks, wl->wl_dealloclens,
   1466 	    wl->wl_dealloccnt);
   1467 
   1468 	/*
   1469 	 * Now that we are fully locked and flushed,
   1470 	 * do another check for nothing to do.
   1471 	 */
   1472 	if (wl->wl_bufcount == 0) {
   1473 		goto wait_out;
   1474 	}
   1475 
   1476 #if 0
   1477 	WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
   1478 		     ("wapbl_flush thread %d.%d flushing entries with "
   1479 		      "bufcount=%zu bufbytes=%zu\n",
   1480 		      curproc->p_pid, curlwp->l_lid, wl->wl_bufcount,
   1481 		      wl->wl_bufbytes));
   1482 #endif
   1483 
   1484 	/* Calculate amount of space needed to flush */
   1485 	flushsize = wapbl_transaction_len(wl);
   1486 	if (wapbl_verbose_commit) {
   1487 		struct timespec ts;
   1488 		getnanotime(&ts);
   1489 		printf("%s: %lld.%09ld this transaction = %zu bytes\n",
   1490 		    __func__, (long long)ts.tv_sec,
   1491 		    (long)ts.tv_nsec, flushsize);
   1492 	}
   1493 
   1494 	if (flushsize > (wl->wl_circ_size - wl->wl_reserved_bytes)) {
   1495 		/*
   1496 		 * XXX this could be handled more gracefully, perhaps place
   1497 		 * only a partial transaction in the log and allow the
   1498 		 * remaining to flush without the protection of the journal.
   1499 		 */
   1500 		panic("wapbl_flush: current transaction too big to flush");
   1501 	}
   1502 
   1503 	error = wapbl_truncate(wl, flushsize, 0);
   1504 	if (error)
   1505 		goto out;
   1506 
   1507 	off = wl->wl_head;
   1508 	KASSERT((off == 0) || ((off >= wl->wl_circ_off) &&
   1509 	                      (off < wl->wl_circ_off + wl->wl_circ_size)));
   1510 	error = wapbl_write_blocks(wl, &off);
   1511 	if (error)
   1512 		goto out;
   1513 	error = wapbl_write_revocations(wl, &off);
   1514 	if (error)
   1515 		goto out;
   1516 	error = wapbl_write_inodes(wl, &off);
   1517 	if (error)
   1518 		goto out;
   1519 
   1520 	reserved = 0;
   1521 	if (wl->wl_inohashcnt)
   1522 		reserved = wapbl_transaction_inodes_len(wl);
   1523 
   1524 	head = wl->wl_head;
   1525 	tail = wl->wl_tail;
   1526 
   1527 	wapbl_advance_head(wl->wl_circ_size, wl->wl_circ_off, flushsize,
   1528 	    &head, &tail);
   1529 #ifdef WAPBL_DEBUG
   1530 	if (head != off) {
   1531 		panic("lost head! head=%"PRIdMAX" tail=%" PRIdMAX
   1532 		      " off=%"PRIdMAX" flush=%zu",
   1533 		      (intmax_t)head, (intmax_t)tail, (intmax_t)off,
   1534 		      flushsize);
   1535 	}
   1536 #else
   1537 	KASSERT(head == off);
   1538 #endif
   1539 
   1540 	/* Opportunistically move the tail forward if we can */
   1541 	if (!wapbl_lazy_truncate) {
   1542 		mutex_enter(&wl->wl_mtx);
   1543 		delta = wl->wl_reclaimable_bytes;
   1544 		mutex_exit(&wl->wl_mtx);
   1545 		wapbl_advance_tail(wl->wl_circ_size, wl->wl_circ_off, delta,
   1546 		    &head, &tail);
   1547 	}
   1548 
   1549 	error = wapbl_write_commit(wl, head, tail);
   1550 	if (error)
   1551 		goto out;
   1552 
   1553 	we = pool_get(&wapbl_entry_pool, PR_WAITOK);
   1554 
   1555 #ifdef WAPBL_DEBUG_BUFBYTES
   1556 	WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
   1557 		("wapbl_flush: thread %d.%d head+=%zu tail+=%zu used=%zu"
   1558 		 " unsynced=%zu"
   1559 		 "\n\tbufcount=%zu bufbytes=%zu bcount=%zu deallocs=%d "
   1560 		 "inodes=%d\n",
   1561 		 curproc->p_pid, curlwp->l_lid, flushsize, delta,
   1562 		 wapbl_space_used(wl->wl_circ_size, head, tail),
   1563 		 wl->wl_unsynced_bufbytes, wl->wl_bufcount,
   1564 		 wl->wl_bufbytes, wl->wl_bcount, wl->wl_dealloccnt,
   1565 		 wl->wl_inohashcnt));
   1566 #else
   1567 	WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
   1568 		("wapbl_flush: thread %d.%d head+=%zu tail+=%zu used=%zu"
   1569 		 "\n\tbufcount=%zu bufbytes=%zu bcount=%zu deallocs=%d "
   1570 		 "inodes=%d\n",
   1571 		 curproc->p_pid, curlwp->l_lid, flushsize, delta,
   1572 		 wapbl_space_used(wl->wl_circ_size, head, tail),
   1573 		 wl->wl_bufcount, wl->wl_bufbytes, wl->wl_bcount,
   1574 		 wl->wl_dealloccnt, wl->wl_inohashcnt));
   1575 #endif
   1576 
   1577 
   1578 	mutex_enter(&bufcache_lock);
   1579 	mutex_enter(&wl->wl_mtx);
   1580 
   1581 	wl->wl_reserved_bytes = reserved;
   1582 	wl->wl_head = head;
   1583 	wl->wl_tail = tail;
   1584 	KASSERT(wl->wl_reclaimable_bytes >= delta);
   1585 	wl->wl_reclaimable_bytes -= delta;
   1586 	wl->wl_dealloccnt = 0;
   1587 #ifdef WAPBL_DEBUG_BUFBYTES
   1588 	wl->wl_unsynced_bufbytes += wl->wl_bufbytes;
   1589 #endif
   1590 
   1591 	we->we_wapbl = wl;
   1592 	we->we_bufcount = wl->wl_bufcount;
   1593 #ifdef WAPBL_DEBUG_BUFBYTES
   1594 	we->we_unsynced_bufbytes = wl->wl_bufbytes;
   1595 #endif
   1596 	we->we_reclaimable_bytes = flushsize;
   1597 	we->we_error = 0;
   1598 	SIMPLEQ_INSERT_TAIL(&wl->wl_entries, we, we_entries);
   1599 
   1600 	/*
   1601 	 * this flushes bufs in reverse order than they were queued
   1602 	 * it shouldn't matter, but if we care we could use TAILQ instead.
   1603 	 * XXX Note they will get put on the lru queue when they flush
   1604 	 * so we might actually want to change this to preserve order.
   1605 	 */
   1606 	while ((bp = LIST_FIRST(&wl->wl_bufs)) != NULL) {
   1607 		if (bbusy(bp, 0, 0, &wl->wl_mtx)) {
   1608 			continue;
   1609 		}
   1610 		bp->b_iodone = wapbl_biodone;
   1611 		bp->b_private = we;
   1612 		bremfree(bp);
   1613 		wapbl_remove_buf_locked(wl, bp);
   1614 		mutex_exit(&wl->wl_mtx);
   1615 		mutex_exit(&bufcache_lock);
   1616 		bawrite(bp);
   1617 		mutex_enter(&bufcache_lock);
   1618 		mutex_enter(&wl->wl_mtx);
   1619 	}
   1620 	mutex_exit(&wl->wl_mtx);
   1621 	mutex_exit(&bufcache_lock);
   1622 
   1623 #if 0
   1624 	WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
   1625 		     ("wapbl_flush thread %d.%d done flushing entries...\n",
   1626 		     curproc->p_pid, curlwp->l_lid));
   1627 #endif
   1628 
   1629  wait_out:
   1630 
   1631 	/*
   1632 	 * If the waitfor flag is set, don't return until everything is
   1633 	 * fully flushed and the on disk log is empty.
   1634 	 */
   1635 	if (waitfor) {
   1636 		error = wapbl_truncate(wl, wl->wl_circ_size -
   1637 			wl->wl_reserved_bytes, wapbl_lazy_truncate);
   1638 	}
   1639 
   1640  out:
   1641 	if (error) {
   1642 		wl->wl_flush_abort(wl->wl_mount, wl->wl_deallocblks,
   1643 		    wl->wl_dealloclens, wl->wl_dealloccnt);
   1644 	}
   1645 
   1646 #ifdef WAPBL_DEBUG_PRINT
   1647 	if (error) {
   1648 		pid_t pid = -1;
   1649 		lwpid_t lid = -1;
   1650 		if (curproc)
   1651 			pid = curproc->p_pid;
   1652 		if (curlwp)
   1653 			lid = curlwp->l_lid;
   1654 		mutex_enter(&wl->wl_mtx);
   1655 #ifdef WAPBL_DEBUG_BUFBYTES
   1656 		WAPBL_PRINTF(WAPBL_PRINT_ERROR,
   1657 		    ("wapbl_flush: thread %d.%d aborted flush: "
   1658 		    "error = %d\n"
   1659 		    "\tbufcount=%zu bufbytes=%zu bcount=%zu "
   1660 		    "deallocs=%d inodes=%d\n"
   1661 		    "\terrcnt = %d, reclaimable=%zu reserved=%zu "
   1662 		    "unsynced=%zu\n",
   1663 		    pid, lid, error, wl->wl_bufcount,
   1664 		    wl->wl_bufbytes, wl->wl_bcount,
   1665 		    wl->wl_dealloccnt, wl->wl_inohashcnt,
   1666 		    wl->wl_error_count, wl->wl_reclaimable_bytes,
   1667 		    wl->wl_reserved_bytes, wl->wl_unsynced_bufbytes));
   1668 		SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
   1669 			WAPBL_PRINTF(WAPBL_PRINT_ERROR,
   1670 			    ("\tentry: bufcount = %zu, reclaimable = %zu, "
   1671 			     "error = %d, unsynced = %zu\n",
   1672 			     we->we_bufcount, we->we_reclaimable_bytes,
   1673 			     we->we_error, we->we_unsynced_bufbytes));
   1674 		}
   1675 #else
   1676 		WAPBL_PRINTF(WAPBL_PRINT_ERROR,
   1677 		    ("wapbl_flush: thread %d.%d aborted flush: "
   1678 		     "error = %d\n"
   1679 		     "\tbufcount=%zu bufbytes=%zu bcount=%zu "
   1680 		     "deallocs=%d inodes=%d\n"
   1681 		     "\terrcnt = %d, reclaimable=%zu reserved=%zu\n",
   1682 		     pid, lid, error, wl->wl_bufcount,
   1683 		     wl->wl_bufbytes, wl->wl_bcount,
   1684 		     wl->wl_dealloccnt, wl->wl_inohashcnt,
   1685 		     wl->wl_error_count, wl->wl_reclaimable_bytes,
   1686 		     wl->wl_reserved_bytes));
   1687 		SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
   1688 			WAPBL_PRINTF(WAPBL_PRINT_ERROR,
   1689 			    ("\tentry: bufcount = %zu, reclaimable = %zu, "
   1690 			     "error = %d\n", we->we_bufcount,
   1691 			     we->we_reclaimable_bytes, we->we_error));
   1692 		}
   1693 #endif
   1694 		mutex_exit(&wl->wl_mtx);
   1695 	}
   1696 #endif
   1697 
   1698 	rw_exit(&wl->wl_rwlock);
   1699 	return error;
   1700 }
   1701 
   1702 /****************************************************************/
   1703 
   1704 void
   1705 wapbl_jlock_assert(struct wapbl *wl)
   1706 {
   1707 
   1708 	KASSERT(rw_lock_held(&wl->wl_rwlock));
   1709 }
   1710 
   1711 void
   1712 wapbl_junlock_assert(struct wapbl *wl)
   1713 {
   1714 
   1715 	KASSERT(!rw_write_held(&wl->wl_rwlock));
   1716 }
   1717 
   1718 /****************************************************************/
   1719 
   1720 /* locks missing */
   1721 void
   1722 wapbl_print(struct wapbl *wl,
   1723 		int full,
   1724 		void (*pr)(const char *, ...))
   1725 {
   1726 	struct buf *bp;
   1727 	struct wapbl_entry *we;
   1728 	(*pr)("wapbl %p", wl);
   1729 	(*pr)("\nlogvp = %p, devvp = %p, logpbn = %"PRId64"\n",
   1730 	      wl->wl_logvp, wl->wl_devvp, wl->wl_logpbn);
   1731 	(*pr)("circ = %zu, header = %zu, head = %"PRIdMAX" tail = %"PRIdMAX"\n",
   1732 	      wl->wl_circ_size, wl->wl_circ_off,
   1733 	      (intmax_t)wl->wl_head, (intmax_t)wl->wl_tail);
   1734 	(*pr)("fs_dev_bshift = %d, log_dev_bshift = %d\n",
   1735 	      wl->wl_log_dev_bshift, wl->wl_fs_dev_bshift);
   1736 #ifdef WAPBL_DEBUG_BUFBYTES
   1737 	(*pr)("bufcount = %zu, bufbytes = %zu bcount = %zu reclaimable = %zu "
   1738 	      "reserved = %zu errcnt = %d unsynced = %zu\n",
   1739 	      wl->wl_bufcount, wl->wl_bufbytes, wl->wl_bcount,
   1740 	      wl->wl_reclaimable_bytes, wl->wl_reserved_bytes,
   1741 				wl->wl_error_count, wl->wl_unsynced_bufbytes);
   1742 #else
   1743 	(*pr)("bufcount = %zu, bufbytes = %zu bcount = %zu reclaimable = %zu "
   1744 	      "reserved = %zu errcnt = %d\n", wl->wl_bufcount, wl->wl_bufbytes,
   1745 	      wl->wl_bcount, wl->wl_reclaimable_bytes, wl->wl_reserved_bytes,
   1746 				wl->wl_error_count);
   1747 #endif
   1748 	(*pr)("\tdealloccnt = %d, dealloclim = %d\n",
   1749 	      wl->wl_dealloccnt, wl->wl_dealloclim);
   1750 	(*pr)("\tinohashcnt = %d, inohashmask = 0x%08x\n",
   1751 	      wl->wl_inohashcnt, wl->wl_inohashmask);
   1752 	(*pr)("entries:\n");
   1753 	SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
   1754 #ifdef WAPBL_DEBUG_BUFBYTES
   1755 		(*pr)("\tbufcount = %zu, reclaimable = %zu, error = %d, "
   1756 		      "unsynced = %zu\n",
   1757 		      we->we_bufcount, we->we_reclaimable_bytes,
   1758 		      we->we_error, we->we_unsynced_bufbytes);
   1759 #else
   1760 		(*pr)("\tbufcount = %zu, reclaimable = %zu, error = %d\n",
   1761 		      we->we_bufcount, we->we_reclaimable_bytes, we->we_error);
   1762 #endif
   1763 	}
   1764 	if (full) {
   1765 		int cnt = 0;
   1766 		(*pr)("bufs =");
   1767 		LIST_FOREACH(bp, &wl->wl_bufs, b_wapbllist) {
   1768 			if (!LIST_NEXT(bp, b_wapbllist)) {
   1769 				(*pr)(" %p", bp);
   1770 			} else if ((++cnt % 6) == 0) {
   1771 				(*pr)(" %p,\n\t", bp);
   1772 			} else {
   1773 				(*pr)(" %p,", bp);
   1774 			}
   1775 		}
   1776 		(*pr)("\n");
   1777 
   1778 		(*pr)("dealloced blks = ");
   1779 		{
   1780 			int i;
   1781 			cnt = 0;
   1782 			for (i = 0; i < wl->wl_dealloccnt; i++) {
   1783 				(*pr)(" %"PRId64":%d,",
   1784 				      wl->wl_deallocblks[i],
   1785 				      wl->wl_dealloclens[i]);
   1786 				if ((++cnt % 4) == 0) {
   1787 					(*pr)("\n\t");
   1788 				}
   1789 			}
   1790 		}
   1791 		(*pr)("\n");
   1792 
   1793 		(*pr)("registered inodes = ");
   1794 		{
   1795 			int i;
   1796 			cnt = 0;
   1797 			for (i = 0; i <= wl->wl_inohashmask; i++) {
   1798 				struct wapbl_ino_head *wih;
   1799 				struct wapbl_ino *wi;
   1800 
   1801 				wih = &wl->wl_inohash[i];
   1802 				LIST_FOREACH(wi, wih, wi_hash) {
   1803 					if (wi->wi_ino == 0)
   1804 						continue;
   1805 					(*pr)(" %"PRIu64"/0%06"PRIo32",",
   1806 					    wi->wi_ino, wi->wi_mode);
   1807 					if ((++cnt % 4) == 0) {
   1808 						(*pr)("\n\t");
   1809 					}
   1810 				}
   1811 			}
   1812 			(*pr)("\n");
   1813 		}
   1814 	}
   1815 }
   1816 
   1817 #if defined(WAPBL_DEBUG) || defined(DDB)
   1818 void
   1819 wapbl_dump(struct wapbl *wl)
   1820 {
   1821 #if defined(WAPBL_DEBUG)
   1822 	if (!wl)
   1823 		wl = wapbl_debug_wl;
   1824 #endif
   1825 	if (!wl)
   1826 		return;
   1827 	wapbl_print(wl, 1, printf);
   1828 }
   1829 #endif
   1830 
   1831 /****************************************************************/
   1832 
   1833 void
   1834 wapbl_register_deallocation(struct wapbl *wl, daddr_t blk, int len)
   1835 {
   1836 
   1837 	wapbl_jlock_assert(wl);
   1838 
   1839 	mutex_enter(&wl->wl_mtx);
   1840 	/* XXX should eventually instead tie this into resource estimation */
   1841 	/*
   1842 	 * XXX this panic needs locking/mutex analysis and the
   1843 	 * ability to cope with the failure.
   1844 	 */
   1845 	/* XXX this XXX doesn't have enough XXX */
   1846 	if (__predict_false(wl->wl_dealloccnt >= wl->wl_dealloclim))
   1847 		panic("wapbl_register_deallocation: out of resources");
   1848 
   1849 	wl->wl_deallocblks[wl->wl_dealloccnt] = blk;
   1850 	wl->wl_dealloclens[wl->wl_dealloccnt] = len;
   1851 	wl->wl_dealloccnt++;
   1852 	WAPBL_PRINTF(WAPBL_PRINT_ALLOC,
   1853 	    ("wapbl_register_deallocation: blk=%"PRId64" len=%d\n", blk, len));
   1854 	mutex_exit(&wl->wl_mtx);
   1855 }
   1856 
   1857 /****************************************************************/
   1858 
   1859 static void
   1860 wapbl_inodetrk_init(struct wapbl *wl, u_int size)
   1861 {
   1862 
   1863 	wl->wl_inohash = hashinit(size, HASH_LIST, true, &wl->wl_inohashmask);
   1864 	if (atomic_inc_uint_nv(&wapbl_ino_pool_refcount) == 1) {
   1865 		pool_init(&wapbl_ino_pool, sizeof(struct wapbl_ino), 0, 0, 0,
   1866 		    "wapblinopl", &pool_allocator_nointr, IPL_NONE);
   1867 	}
   1868 }
   1869 
   1870 static void
   1871 wapbl_inodetrk_free(struct wapbl *wl)
   1872 {
   1873 
   1874 	/* XXX this KASSERT needs locking/mutex analysis */
   1875 	KASSERT(wl->wl_inohashcnt == 0);
   1876 	hashdone(wl->wl_inohash, HASH_LIST, wl->wl_inohashmask);
   1877 	if (atomic_dec_uint_nv(&wapbl_ino_pool_refcount) == 0) {
   1878 		pool_destroy(&wapbl_ino_pool);
   1879 	}
   1880 }
   1881 
   1882 static struct wapbl_ino *
   1883 wapbl_inodetrk_get(struct wapbl *wl, ino_t ino)
   1884 {
   1885 	struct wapbl_ino_head *wih;
   1886 	struct wapbl_ino *wi;
   1887 
   1888 	KASSERT(mutex_owned(&wl->wl_mtx));
   1889 
   1890 	wih = &wl->wl_inohash[ino & wl->wl_inohashmask];
   1891 	LIST_FOREACH(wi, wih, wi_hash) {
   1892 		if (ino == wi->wi_ino)
   1893 			return wi;
   1894 	}
   1895 	return 0;
   1896 }
   1897 
   1898 void
   1899 wapbl_register_inode(struct wapbl *wl, ino_t ino, mode_t mode)
   1900 {
   1901 	struct wapbl_ino_head *wih;
   1902 	struct wapbl_ino *wi;
   1903 
   1904 	wi = pool_get(&wapbl_ino_pool, PR_WAITOK);
   1905 
   1906 	mutex_enter(&wl->wl_mtx);
   1907 	if (wapbl_inodetrk_get(wl, ino) == NULL) {
   1908 		wi->wi_ino = ino;
   1909 		wi->wi_mode = mode;
   1910 		wih = &wl->wl_inohash[ino & wl->wl_inohashmask];
   1911 		LIST_INSERT_HEAD(wih, wi, wi_hash);
   1912 		wl->wl_inohashcnt++;
   1913 		WAPBL_PRINTF(WAPBL_PRINT_INODE,
   1914 		    ("wapbl_register_inode: ino=%"PRId64"\n", ino));
   1915 		mutex_exit(&wl->wl_mtx);
   1916 	} else {
   1917 		mutex_exit(&wl->wl_mtx);
   1918 		pool_put(&wapbl_ino_pool, wi);
   1919 	}
   1920 }
   1921 
   1922 void
   1923 wapbl_unregister_inode(struct wapbl *wl, ino_t ino, mode_t mode)
   1924 {
   1925 	struct wapbl_ino *wi;
   1926 
   1927 	mutex_enter(&wl->wl_mtx);
   1928 	wi = wapbl_inodetrk_get(wl, ino);
   1929 	if (wi) {
   1930 		WAPBL_PRINTF(WAPBL_PRINT_INODE,
   1931 		    ("wapbl_unregister_inode: ino=%"PRId64"\n", ino));
   1932 		KASSERT(wl->wl_inohashcnt > 0);
   1933 		wl->wl_inohashcnt--;
   1934 		LIST_REMOVE(wi, wi_hash);
   1935 		mutex_exit(&wl->wl_mtx);
   1936 
   1937 		pool_put(&wapbl_ino_pool, wi);
   1938 	} else {
   1939 		mutex_exit(&wl->wl_mtx);
   1940 	}
   1941 }
   1942 
   1943 /****************************************************************/
   1944 
   1945 static inline size_t
   1946 wapbl_transaction_inodes_len(struct wapbl *wl)
   1947 {
   1948 	int blocklen = 1<<wl->wl_log_dev_bshift;
   1949 	int iph;
   1950 
   1951 	/* Calculate number of inodes described in a inodelist header */
   1952 	iph = (blocklen - offsetof(struct wapbl_wc_inodelist, wc_inodes)) /
   1953 	    sizeof(((struct wapbl_wc_inodelist *)0)->wc_inodes[0]);
   1954 
   1955 	KASSERT(iph > 0);
   1956 
   1957 	return MAX(1, howmany(wl->wl_inohashcnt, iph)) * blocklen;
   1958 }
   1959 
   1960 
   1961 /* Calculate amount of space a transaction will take on disk */
   1962 static size_t
   1963 wapbl_transaction_len(struct wapbl *wl)
   1964 {
   1965 	int blocklen = 1<<wl->wl_log_dev_bshift;
   1966 	size_t len;
   1967 	int bph;
   1968 
   1969 	/* Calculate number of blocks described in a blocklist header */
   1970 	bph = (blocklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) /
   1971 	    sizeof(((struct wapbl_wc_blocklist *)0)->wc_blocks[0]);
   1972 
   1973 	KASSERT(bph > 0);
   1974 
   1975 	len = wl->wl_bcount;
   1976 	len += howmany(wl->wl_bufcount, bph) * blocklen;
   1977 	len += howmany(wl->wl_dealloccnt, bph) * blocklen;
   1978 	len += wapbl_transaction_inodes_len(wl);
   1979 
   1980 	return len;
   1981 }
   1982 
   1983 /*
   1984  * wapbl_cache_sync: issue DIOCCACHESYNC
   1985  */
   1986 static int
   1987 wapbl_cache_sync(struct wapbl *wl, const char *msg)
   1988 {
   1989 	const bool verbose = wapbl_verbose_commit >= 2;
   1990 	struct bintime start_time;
   1991 	int force = 1;
   1992 	int error;
   1993 
   1994 	if (!wapbl_flush_disk_cache) {
   1995 		return 0;
   1996 	}
   1997 	if (verbose) {
   1998 		bintime(&start_time);
   1999 	}
   2000 	error = VOP_IOCTL(wl->wl_devvp, DIOCCACHESYNC, &force,
   2001 	    FWRITE, FSCRED);
   2002 	if (error) {
   2003 		WAPBL_PRINTF(WAPBL_PRINT_ERROR,
   2004 		    ("wapbl_cache_sync: DIOCCACHESYNC on dev 0x%x "
   2005 		    "returned %d\n", wl->wl_devvp->v_rdev, error));
   2006 	}
   2007 	if (verbose) {
   2008 		struct bintime d;
   2009 		struct timespec ts;
   2010 
   2011 		bintime(&d);
   2012 		bintime_sub(&d, &start_time);
   2013 		bintime2timespec(&d, &ts);
   2014 		printf("wapbl_cache_sync: %s: dev 0x%jx %ju.%09lu\n",
   2015 		    msg, (uintmax_t)wl->wl_devvp->v_rdev,
   2016 		    (uintmax_t)ts.tv_sec, ts.tv_nsec);
   2017 	}
   2018 	return error;
   2019 }
   2020 
   2021 /*
   2022  * Perform commit operation
   2023  *
   2024  * Note that generation number incrementation needs to
   2025  * be protected against racing with other invocations
   2026  * of wapbl_write_commit.  This is ok since this routine
   2027  * is only invoked from wapbl_flush
   2028  */
   2029 static int
   2030 wapbl_write_commit(struct wapbl *wl, off_t head, off_t tail)
   2031 {
   2032 	struct wapbl_wc_header *wc = wl->wl_wc_header;
   2033 	struct timespec ts;
   2034 	int error;
   2035 	daddr_t pbn;
   2036 
   2037 	error = wapbl_buffered_flush(wl);
   2038 	if (error)
   2039 		return error;
   2040 	/*
   2041 	 * flush disk cache to ensure that blocks we've written are actually
   2042 	 * written to the stable storage before the commit header.
   2043 	 *
   2044 	 * XXX Calc checksum here, instead we do this for now
   2045 	 */
   2046 	wapbl_cache_sync(wl, "1");
   2047 
   2048 	wc->wc_head = head;
   2049 	wc->wc_tail = tail;
   2050 	wc->wc_checksum = 0;
   2051 	wc->wc_version = 1;
   2052 	getnanotime(&ts);
   2053 	wc->wc_time = ts.tv_sec;
   2054 	wc->wc_timensec = ts.tv_nsec;
   2055 
   2056 	WAPBL_PRINTF(WAPBL_PRINT_WRITE,
   2057 	    ("wapbl_write_commit: head = %"PRIdMAX "tail = %"PRIdMAX"\n",
   2058 	    (intmax_t)head, (intmax_t)tail));
   2059 
   2060 	/*
   2061 	 * write the commit header.
   2062 	 *
   2063 	 * XXX if generation will rollover, then first zero
   2064 	 * over second commit header before trying to write both headers.
   2065 	 */
   2066 
   2067 	pbn = wl->wl_logpbn + (wc->wc_generation % 2);
   2068 #ifdef _KERNEL
   2069 	pbn = btodb(pbn << wc->wc_log_dev_bshift);
   2070 #endif
   2071 	error = wapbl_buffered_write(wc, wc->wc_len, wl, pbn);
   2072 	if (error)
   2073 		return error;
   2074 	error = wapbl_buffered_flush(wl);
   2075 	if (error)
   2076 		return error;
   2077 
   2078 	/*
   2079 	 * flush disk cache to ensure that the commit header is actually
   2080 	 * written before meta data blocks.
   2081 	 */
   2082 	wapbl_cache_sync(wl, "2");
   2083 
   2084 	/*
   2085 	 * If the generation number was zero, write it out a second time.
   2086 	 * This handles initialization and generation number rollover
   2087 	 */
   2088 	if (wc->wc_generation++ == 0) {
   2089 		error = wapbl_write_commit(wl, head, tail);
   2090 		/*
   2091 		 * This panic should be able to be removed if we do the
   2092 		 * zero'ing mentioned above, and we are certain to roll
   2093 		 * back generation number on failure.
   2094 		 */
   2095 		if (error)
   2096 			panic("wapbl_write_commit: error writing duplicate "
   2097 			      "log header: %d", error);
   2098 	}
   2099 	return 0;
   2100 }
   2101 
   2102 /* Returns new offset value */
   2103 static int
   2104 wapbl_write_blocks(struct wapbl *wl, off_t *offp)
   2105 {
   2106 	struct wapbl_wc_blocklist *wc =
   2107 	    (struct wapbl_wc_blocklist *)wl->wl_wc_scratch;
   2108 	int blocklen = 1<<wl->wl_log_dev_bshift;
   2109 	int bph;
   2110 	struct buf *bp;
   2111 	off_t off = *offp;
   2112 	int error;
   2113 	size_t padding;
   2114 
   2115 	KASSERT(rw_write_held(&wl->wl_rwlock));
   2116 
   2117 	bph = (blocklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) /
   2118 	    sizeof(((struct wapbl_wc_blocklist *)0)->wc_blocks[0]);
   2119 
   2120 	bp = LIST_FIRST(&wl->wl_bufs);
   2121 
   2122 	while (bp) {
   2123 		int cnt;
   2124 		struct buf *obp = bp;
   2125 
   2126 		KASSERT(bp->b_flags & B_LOCKED);
   2127 
   2128 		wc->wc_type = WAPBL_WC_BLOCKS;
   2129 		wc->wc_len = blocklen;
   2130 		wc->wc_blkcount = 0;
   2131 		while (bp && (wc->wc_blkcount < bph)) {
   2132 			/*
   2133 			 * Make sure all the physical block numbers are up to
   2134 			 * date.  If this is not always true on a given
   2135 			 * filesystem, then VOP_BMAP must be called.  We
   2136 			 * could call VOP_BMAP here, or else in the filesystem
   2137 			 * specific flush callback, although neither of those
   2138 			 * solutions allow us to take the vnode lock.  If a
   2139 			 * filesystem requires that we must take the vnode lock
   2140 			 * to call VOP_BMAP, then we can probably do it in
   2141 			 * bwrite when the vnode lock should already be held
   2142 			 * by the invoking code.
   2143 			 */
   2144 			KASSERT((bp->b_vp->v_type == VBLK) ||
   2145 				 (bp->b_blkno != bp->b_lblkno));
   2146 			KASSERT(bp->b_blkno > 0);
   2147 
   2148 			wc->wc_blocks[wc->wc_blkcount].wc_daddr = bp->b_blkno;
   2149 			wc->wc_blocks[wc->wc_blkcount].wc_dlen = bp->b_bcount;
   2150 			wc->wc_len += bp->b_bcount;
   2151 			wc->wc_blkcount++;
   2152 			bp = LIST_NEXT(bp, b_wapbllist);
   2153 		}
   2154 		if (wc->wc_len % blocklen != 0) {
   2155 			padding = blocklen - wc->wc_len % blocklen;
   2156 			wc->wc_len += padding;
   2157 		} else {
   2158 			padding = 0;
   2159 		}
   2160 
   2161 		WAPBL_PRINTF(WAPBL_PRINT_WRITE,
   2162 		    ("wapbl_write_blocks: len = %u (padding %zu) off = %"PRIdMAX"\n",
   2163 		    wc->wc_len, padding, (intmax_t)off));
   2164 
   2165 		error = wapbl_circ_write(wl, wc, blocklen, &off);
   2166 		if (error)
   2167 			return error;
   2168 		bp = obp;
   2169 		cnt = 0;
   2170 		while (bp && (cnt++ < bph)) {
   2171 			error = wapbl_circ_write(wl, bp->b_data,
   2172 			    bp->b_bcount, &off);
   2173 			if (error)
   2174 				return error;
   2175 			bp = LIST_NEXT(bp, b_wapbllist);
   2176 		}
   2177 		if (padding) {
   2178 			void *zero;
   2179 
   2180 			zero = wapbl_alloc(padding);
   2181 			memset(zero, 0, padding);
   2182 			error = wapbl_circ_write(wl, zero, padding, &off);
   2183 			wapbl_free(zero, padding);
   2184 			if (error)
   2185 				return error;
   2186 		}
   2187 	}
   2188 	*offp = off;
   2189 	return 0;
   2190 }
   2191 
   2192 static int
   2193 wapbl_write_revocations(struct wapbl *wl, off_t *offp)
   2194 {
   2195 	struct wapbl_wc_blocklist *wc =
   2196 	    (struct wapbl_wc_blocklist *)wl->wl_wc_scratch;
   2197 	int i;
   2198 	int blocklen = 1<<wl->wl_log_dev_bshift;
   2199 	int bph;
   2200 	off_t off = *offp;
   2201 	int error;
   2202 
   2203 	if (wl->wl_dealloccnt == 0)
   2204 		return 0;
   2205 
   2206 	bph = (blocklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) /
   2207 	    sizeof(((struct wapbl_wc_blocklist *)0)->wc_blocks[0]);
   2208 
   2209 	i = 0;
   2210 	while (i < wl->wl_dealloccnt) {
   2211 		wc->wc_type = WAPBL_WC_REVOCATIONS;
   2212 		wc->wc_len = blocklen;
   2213 		wc->wc_blkcount = 0;
   2214 		while ((i < wl->wl_dealloccnt) && (wc->wc_blkcount < bph)) {
   2215 			wc->wc_blocks[wc->wc_blkcount].wc_daddr =
   2216 			    wl->wl_deallocblks[i];
   2217 			wc->wc_blocks[wc->wc_blkcount].wc_dlen =
   2218 			    wl->wl_dealloclens[i];
   2219 			wc->wc_blkcount++;
   2220 			i++;
   2221 		}
   2222 		WAPBL_PRINTF(WAPBL_PRINT_WRITE,
   2223 		    ("wapbl_write_revocations: len = %u off = %"PRIdMAX"\n",
   2224 		    wc->wc_len, (intmax_t)off));
   2225 		error = wapbl_circ_write(wl, wc, blocklen, &off);
   2226 		if (error)
   2227 			return error;
   2228 	}
   2229 	*offp = off;
   2230 	return 0;
   2231 }
   2232 
   2233 static int
   2234 wapbl_write_inodes(struct wapbl *wl, off_t *offp)
   2235 {
   2236 	struct wapbl_wc_inodelist *wc =
   2237 	    (struct wapbl_wc_inodelist *)wl->wl_wc_scratch;
   2238 	int i;
   2239 	int blocklen = 1 << wl->wl_log_dev_bshift;
   2240 	off_t off = *offp;
   2241 	int error;
   2242 
   2243 	struct wapbl_ino_head *wih;
   2244 	struct wapbl_ino *wi;
   2245 	int iph;
   2246 
   2247 	iph = (blocklen - offsetof(struct wapbl_wc_inodelist, wc_inodes)) /
   2248 	    sizeof(((struct wapbl_wc_inodelist *)0)->wc_inodes[0]);
   2249 
   2250 	i = 0;
   2251 	wih = &wl->wl_inohash[0];
   2252 	wi = 0;
   2253 	do {
   2254 		wc->wc_type = WAPBL_WC_INODES;
   2255 		wc->wc_len = blocklen;
   2256 		wc->wc_inocnt = 0;
   2257 		wc->wc_clear = (i == 0);
   2258 		while ((i < wl->wl_inohashcnt) && (wc->wc_inocnt < iph)) {
   2259 			while (!wi) {
   2260 				KASSERT((wih - &wl->wl_inohash[0])
   2261 				    <= wl->wl_inohashmask);
   2262 				wi = LIST_FIRST(wih++);
   2263 			}
   2264 			wc->wc_inodes[wc->wc_inocnt].wc_inumber = wi->wi_ino;
   2265 			wc->wc_inodes[wc->wc_inocnt].wc_imode = wi->wi_mode;
   2266 			wc->wc_inocnt++;
   2267 			i++;
   2268 			wi = LIST_NEXT(wi, wi_hash);
   2269 		}
   2270 		WAPBL_PRINTF(WAPBL_PRINT_WRITE,
   2271 		    ("wapbl_write_inodes: len = %u off = %"PRIdMAX"\n",
   2272 		    wc->wc_len, (intmax_t)off));
   2273 		error = wapbl_circ_write(wl, wc, blocklen, &off);
   2274 		if (error)
   2275 			return error;
   2276 	} while (i < wl->wl_inohashcnt);
   2277 
   2278 	*offp = off;
   2279 	return 0;
   2280 }
   2281 
   2282 #endif /* _KERNEL */
   2283 
   2284 /****************************************************************/
   2285 
   2286 struct wapbl_blk {
   2287 	LIST_ENTRY(wapbl_blk) wb_hash;
   2288 	daddr_t wb_blk;
   2289 	off_t wb_off; /* Offset of this block in the log */
   2290 };
   2291 #define	WAPBL_BLKPOOL_MIN 83
   2292 
   2293 static void
   2294 wapbl_blkhash_init(struct wapbl_replay *wr, u_int size)
   2295 {
   2296 	if (size < WAPBL_BLKPOOL_MIN)
   2297 		size = WAPBL_BLKPOOL_MIN;
   2298 	KASSERT(wr->wr_blkhash == 0);
   2299 #ifdef _KERNEL
   2300 	wr->wr_blkhash = hashinit(size, HASH_LIST, true, &wr->wr_blkhashmask);
   2301 #else /* ! _KERNEL */
   2302 	/* Manually implement hashinit */
   2303 	{
   2304 		unsigned long i, hashsize;
   2305 		for (hashsize = 1; hashsize < size; hashsize <<= 1)
   2306 			continue;
   2307 		wr->wr_blkhash = wapbl_alloc(hashsize * sizeof(*wr->wr_blkhash));
   2308 		for (i = 0; i < hashsize; i++)
   2309 			LIST_INIT(&wr->wr_blkhash[i]);
   2310 		wr->wr_blkhashmask = hashsize - 1;
   2311 	}
   2312 #endif /* ! _KERNEL */
   2313 }
   2314 
   2315 static void
   2316 wapbl_blkhash_free(struct wapbl_replay *wr)
   2317 {
   2318 	KASSERT(wr->wr_blkhashcnt == 0);
   2319 #ifdef _KERNEL
   2320 	hashdone(wr->wr_blkhash, HASH_LIST, wr->wr_blkhashmask);
   2321 #else /* ! _KERNEL */
   2322 	wapbl_free(wr->wr_blkhash,
   2323 	    (wr->wr_blkhashmask + 1) * sizeof(*wr->wr_blkhash));
   2324 #endif /* ! _KERNEL */
   2325 }
   2326 
   2327 static struct wapbl_blk *
   2328 wapbl_blkhash_get(struct wapbl_replay *wr, daddr_t blk)
   2329 {
   2330 	struct wapbl_blk_head *wbh;
   2331 	struct wapbl_blk *wb;
   2332 	wbh = &wr->wr_blkhash[blk & wr->wr_blkhashmask];
   2333 	LIST_FOREACH(wb, wbh, wb_hash) {
   2334 		if (blk == wb->wb_blk)
   2335 			return wb;
   2336 	}
   2337 	return 0;
   2338 }
   2339 
   2340 static void
   2341 wapbl_blkhash_ins(struct wapbl_replay *wr, daddr_t blk, off_t off)
   2342 {
   2343 	struct wapbl_blk_head *wbh;
   2344 	struct wapbl_blk *wb;
   2345 	wb = wapbl_blkhash_get(wr, blk);
   2346 	if (wb) {
   2347 		KASSERT(wb->wb_blk == blk);
   2348 		wb->wb_off = off;
   2349 	} else {
   2350 		wb = wapbl_alloc(sizeof(*wb));
   2351 		wb->wb_blk = blk;
   2352 		wb->wb_off = off;
   2353 		wbh = &wr->wr_blkhash[blk & wr->wr_blkhashmask];
   2354 		LIST_INSERT_HEAD(wbh, wb, wb_hash);
   2355 		wr->wr_blkhashcnt++;
   2356 	}
   2357 }
   2358 
   2359 static void
   2360 wapbl_blkhash_rem(struct wapbl_replay *wr, daddr_t blk)
   2361 {
   2362 	struct wapbl_blk *wb = wapbl_blkhash_get(wr, blk);
   2363 	if (wb) {
   2364 		KASSERT(wr->wr_blkhashcnt > 0);
   2365 		wr->wr_blkhashcnt--;
   2366 		LIST_REMOVE(wb, wb_hash);
   2367 		wapbl_free(wb, sizeof(*wb));
   2368 	}
   2369 }
   2370 
   2371 static void
   2372 wapbl_blkhash_clear(struct wapbl_replay *wr)
   2373 {
   2374 	unsigned long i;
   2375 	for (i = 0; i <= wr->wr_blkhashmask; i++) {
   2376 		struct wapbl_blk *wb;
   2377 
   2378 		while ((wb = LIST_FIRST(&wr->wr_blkhash[i]))) {
   2379 			KASSERT(wr->wr_blkhashcnt > 0);
   2380 			wr->wr_blkhashcnt--;
   2381 			LIST_REMOVE(wb, wb_hash);
   2382 			wapbl_free(wb, sizeof(*wb));
   2383 		}
   2384 	}
   2385 	KASSERT(wr->wr_blkhashcnt == 0);
   2386 }
   2387 
   2388 /****************************************************************/
   2389 
   2390 static int
   2391 wapbl_circ_read(struct wapbl_replay *wr, void *data, size_t len, off_t *offp)
   2392 {
   2393 	size_t slen;
   2394 	off_t off = *offp;
   2395 	int error;
   2396 	daddr_t pbn;
   2397 
   2398 	KASSERT(((len >> wr->wr_log_dev_bshift) <<
   2399 	    wr->wr_log_dev_bshift) == len);
   2400 
   2401 	if (off < wr->wr_circ_off)
   2402 		off = wr->wr_circ_off;
   2403 	slen = wr->wr_circ_off + wr->wr_circ_size - off;
   2404 	if (slen < len) {
   2405 		pbn = wr->wr_logpbn + (off >> wr->wr_log_dev_bshift);
   2406 #ifdef _KERNEL
   2407 		pbn = btodb(pbn << wr->wr_log_dev_bshift);
   2408 #endif
   2409 		error = wapbl_read(data, slen, wr->wr_devvp, pbn);
   2410 		if (error)
   2411 			return error;
   2412 		data = (uint8_t *)data + slen;
   2413 		len -= slen;
   2414 		off = wr->wr_circ_off;
   2415 	}
   2416 	pbn = wr->wr_logpbn + (off >> wr->wr_log_dev_bshift);
   2417 #ifdef _KERNEL
   2418 	pbn = btodb(pbn << wr->wr_log_dev_bshift);
   2419 #endif
   2420 	error = wapbl_read(data, len, wr->wr_devvp, pbn);
   2421 	if (error)
   2422 		return error;
   2423 	off += len;
   2424 	if (off >= wr->wr_circ_off + wr->wr_circ_size)
   2425 		off = wr->wr_circ_off;
   2426 	*offp = off;
   2427 	return 0;
   2428 }
   2429 
   2430 static void
   2431 wapbl_circ_advance(struct wapbl_replay *wr, size_t len, off_t *offp)
   2432 {
   2433 	size_t slen;
   2434 	off_t off = *offp;
   2435 
   2436 	KASSERT(((len >> wr->wr_log_dev_bshift) <<
   2437 	    wr->wr_log_dev_bshift) == len);
   2438 
   2439 	if (off < wr->wr_circ_off)
   2440 		off = wr->wr_circ_off;
   2441 	slen = wr->wr_circ_off + wr->wr_circ_size - off;
   2442 	if (slen < len) {
   2443 		len -= slen;
   2444 		off = wr->wr_circ_off;
   2445 	}
   2446 	off += len;
   2447 	if (off >= wr->wr_circ_off + wr->wr_circ_size)
   2448 		off = wr->wr_circ_off;
   2449 	*offp = off;
   2450 }
   2451 
   2452 /****************************************************************/
   2453 
   2454 int
   2455 wapbl_replay_start(struct wapbl_replay **wrp, struct vnode *vp,
   2456 	daddr_t off, size_t count, size_t blksize)
   2457 {
   2458 	struct wapbl_replay *wr;
   2459 	int error;
   2460 	struct vnode *devvp;
   2461 	daddr_t logpbn;
   2462 	uint8_t *scratch;
   2463 	struct wapbl_wc_header *wch;
   2464 	struct wapbl_wc_header *wch2;
   2465 	/* Use this until we read the actual log header */
   2466 	int log_dev_bshift = ilog2(blksize);
   2467 	size_t used;
   2468 	daddr_t pbn;
   2469 
   2470 	WAPBL_PRINTF(WAPBL_PRINT_REPLAY,
   2471 	    ("wapbl_replay_start: vp=%p off=%"PRId64 " count=%zu blksize=%zu\n",
   2472 	    vp, off, count, blksize));
   2473 
   2474 	if (off < 0)
   2475 		return EINVAL;
   2476 
   2477 	if (blksize < DEV_BSIZE)
   2478 		return EINVAL;
   2479 	if (blksize % DEV_BSIZE)
   2480 		return EINVAL;
   2481 
   2482 #ifdef _KERNEL
   2483 #if 0
   2484 	/* XXX vp->v_size isn't reliably set for VBLK devices,
   2485 	 * especially root.  However, we might still want to verify
   2486 	 * that the full load is readable */
   2487 	if ((off + count) * blksize > vp->v_size)
   2488 		return EINVAL;
   2489 #endif
   2490 	if ((error = VOP_BMAP(vp, off, &devvp, &logpbn, 0)) != 0) {
   2491 		return error;
   2492 	}
   2493 #else /* ! _KERNEL */
   2494 	devvp = vp;
   2495 	logpbn = off;
   2496 #endif /* ! _KERNEL */
   2497 
   2498 	scratch = wapbl_alloc(MAXBSIZE);
   2499 
   2500 	pbn = logpbn;
   2501 #ifdef _KERNEL
   2502 	pbn = btodb(pbn << log_dev_bshift);
   2503 #endif
   2504 	error = wapbl_read(scratch, 2<<log_dev_bshift, devvp, pbn);
   2505 	if (error)
   2506 		goto errout;
   2507 
   2508 	wch = (struct wapbl_wc_header *)scratch;
   2509 	wch2 =
   2510 	    (struct wapbl_wc_header *)(scratch + (1<<log_dev_bshift));
   2511 	/* XXX verify checksums and magic numbers */
   2512 	if (wch->wc_type != WAPBL_WC_HEADER) {
   2513 		printf("Unrecognized wapbl magic: 0x%08x\n", wch->wc_type);
   2514 		error = EFTYPE;
   2515 		goto errout;
   2516 	}
   2517 
   2518 	if (wch2->wc_generation > wch->wc_generation)
   2519 		wch = wch2;
   2520 
   2521 	wr = wapbl_calloc(1, sizeof(*wr));
   2522 
   2523 	wr->wr_logvp = vp;
   2524 	wr->wr_devvp = devvp;
   2525 	wr->wr_logpbn = logpbn;
   2526 
   2527 	wr->wr_scratch = scratch;
   2528 
   2529 	wr->wr_log_dev_bshift = wch->wc_log_dev_bshift;
   2530 	wr->wr_fs_dev_bshift = wch->wc_fs_dev_bshift;
   2531 	wr->wr_circ_off = wch->wc_circ_off;
   2532 	wr->wr_circ_size = wch->wc_circ_size;
   2533 	wr->wr_generation = wch->wc_generation;
   2534 
   2535 	used = wapbl_space_used(wch->wc_circ_size, wch->wc_head, wch->wc_tail);
   2536 
   2537 	WAPBL_PRINTF(WAPBL_PRINT_REPLAY,
   2538 	    ("wapbl_replay: head=%"PRId64" tail=%"PRId64" off=%"PRId64
   2539 	    " len=%"PRId64" used=%zu\n",
   2540 	    wch->wc_head, wch->wc_tail, wch->wc_circ_off,
   2541 	    wch->wc_circ_size, used));
   2542 
   2543 	wapbl_blkhash_init(wr, (used >> wch->wc_fs_dev_bshift));
   2544 
   2545 	error = wapbl_replay_process(wr, wch->wc_head, wch->wc_tail);
   2546 	if (error) {
   2547 		wapbl_replay_stop(wr);
   2548 		wapbl_replay_free(wr);
   2549 		return error;
   2550 	}
   2551 
   2552 	*wrp = wr;
   2553 	return 0;
   2554 
   2555  errout:
   2556 	wapbl_free(scratch, MAXBSIZE);
   2557 	return error;
   2558 }
   2559 
   2560 void
   2561 wapbl_replay_stop(struct wapbl_replay *wr)
   2562 {
   2563 
   2564 	if (!wapbl_replay_isopen(wr))
   2565 		return;
   2566 
   2567 	WAPBL_PRINTF(WAPBL_PRINT_REPLAY, ("wapbl_replay_stop called\n"));
   2568 
   2569 	wapbl_free(wr->wr_scratch, MAXBSIZE);
   2570 	wr->wr_scratch = NULL;
   2571 
   2572 	wr->wr_logvp = NULL;
   2573 
   2574 	wapbl_blkhash_clear(wr);
   2575 	wapbl_blkhash_free(wr);
   2576 }
   2577 
   2578 void
   2579 wapbl_replay_free(struct wapbl_replay *wr)
   2580 {
   2581 
   2582 	KDASSERT(!wapbl_replay_isopen(wr));
   2583 
   2584 	if (wr->wr_inodes)
   2585 		wapbl_free(wr->wr_inodes,
   2586 		    wr->wr_inodescnt * sizeof(wr->wr_inodes[0]));
   2587 	wapbl_free(wr, sizeof(*wr));
   2588 }
   2589 
   2590 #ifdef _KERNEL
   2591 int
   2592 wapbl_replay_isopen1(struct wapbl_replay *wr)
   2593 {
   2594 
   2595 	return wapbl_replay_isopen(wr);
   2596 }
   2597 #endif
   2598 
   2599 /*
   2600  * calculate the disk address for the i'th block in the wc_blockblist
   2601  * offset by j blocks of size blen.
   2602  *
   2603  * wc_daddr is always a kernel disk address in DEV_BSIZE units that
   2604  * was written to the journal.
   2605  *
   2606  * The kernel needs that address plus the offset in DEV_BSIZE units.
   2607  *
   2608  * Userland needs that address plus the offset in blen units.
   2609  *
   2610  */
   2611 static daddr_t
   2612 wapbl_block_daddr(struct wapbl_wc_blocklist *wc, int i, int j, int blen)
   2613 {
   2614 	daddr_t pbn;
   2615 
   2616 #ifdef _KERNEL
   2617 	pbn = wc->wc_blocks[i].wc_daddr + btodb(j * blen);
   2618 #else
   2619 	pbn = dbtob(wc->wc_blocks[i].wc_daddr) / blen + j;
   2620 #endif
   2621 
   2622 	return pbn;
   2623 }
   2624 
   2625 static void
   2626 wapbl_replay_process_blocks(struct wapbl_replay *wr, off_t *offp)
   2627 {
   2628 	struct wapbl_wc_blocklist *wc =
   2629 	    (struct wapbl_wc_blocklist *)wr->wr_scratch;
   2630 	int fsblklen = 1 << wr->wr_fs_dev_bshift;
   2631 	int i, j, n;
   2632 
   2633 	for (i = 0; i < wc->wc_blkcount; i++) {
   2634 		/*
   2635 		 * Enter each physical block into the hashtable independently.
   2636 		 */
   2637 		n = wc->wc_blocks[i].wc_dlen >> wr->wr_fs_dev_bshift;
   2638 		for (j = 0; j < n; j++) {
   2639 			wapbl_blkhash_ins(wr, wapbl_block_daddr(wc, i, j, fsblklen),
   2640 			    *offp);
   2641 			wapbl_circ_advance(wr, fsblklen, offp);
   2642 		}
   2643 	}
   2644 }
   2645 
   2646 static void
   2647 wapbl_replay_process_revocations(struct wapbl_replay *wr)
   2648 {
   2649 	struct wapbl_wc_blocklist *wc =
   2650 	    (struct wapbl_wc_blocklist *)wr->wr_scratch;
   2651 	int fsblklen = 1 << wr->wr_fs_dev_bshift;
   2652 	int i, j, n;
   2653 
   2654 	for (i = 0; i < wc->wc_blkcount; i++) {
   2655 		/*
   2656 		 * Remove any blocks found from the hashtable.
   2657 		 */
   2658 		n = wc->wc_blocks[i].wc_dlen >> wr->wr_fs_dev_bshift;
   2659 		for (j = 0; j < n; j++)
   2660 			wapbl_blkhash_rem(wr, wapbl_block_daddr(wc, i, j, fsblklen));
   2661 	}
   2662 }
   2663 
   2664 static void
   2665 wapbl_replay_process_inodes(struct wapbl_replay *wr, off_t oldoff, off_t newoff)
   2666 {
   2667 	struct wapbl_wc_inodelist *wc =
   2668 	    (struct wapbl_wc_inodelist *)wr->wr_scratch;
   2669 	void *new_inodes;
   2670 	const size_t oldsize = wr->wr_inodescnt * sizeof(wr->wr_inodes[0]);
   2671 
   2672 	KASSERT(sizeof(wr->wr_inodes[0]) == sizeof(wc->wc_inodes[0]));
   2673 
   2674 	/*
   2675 	 * Keep track of where we found this so location won't be
   2676 	 * overwritten.
   2677 	 */
   2678 	if (wc->wc_clear) {
   2679 		wr->wr_inodestail = oldoff;
   2680 		wr->wr_inodescnt = 0;
   2681 		if (wr->wr_inodes != NULL) {
   2682 			wapbl_free(wr->wr_inodes, oldsize);
   2683 			wr->wr_inodes = NULL;
   2684 		}
   2685 	}
   2686 	wr->wr_inodeshead = newoff;
   2687 	if (wc->wc_inocnt == 0)
   2688 		return;
   2689 
   2690 	new_inodes = wapbl_alloc((wr->wr_inodescnt + wc->wc_inocnt) *
   2691 	    sizeof(wr->wr_inodes[0]));
   2692 	if (wr->wr_inodes != NULL) {
   2693 		memcpy(new_inodes, wr->wr_inodes, oldsize);
   2694 		wapbl_free(wr->wr_inodes, oldsize);
   2695 	}
   2696 	wr->wr_inodes = new_inodes;
   2697 	memcpy(&wr->wr_inodes[wr->wr_inodescnt], wc->wc_inodes,
   2698 	    wc->wc_inocnt * sizeof(wr->wr_inodes[0]));
   2699 	wr->wr_inodescnt += wc->wc_inocnt;
   2700 }
   2701 
   2702 static int
   2703 wapbl_replay_process(struct wapbl_replay *wr, off_t head, off_t tail)
   2704 {
   2705 	off_t off;
   2706 	int error;
   2707 
   2708 	int logblklen = 1 << wr->wr_log_dev_bshift;
   2709 
   2710 	wapbl_blkhash_clear(wr);
   2711 
   2712 	off = tail;
   2713 	while (off != head) {
   2714 		struct wapbl_wc_null *wcn;
   2715 		off_t saveoff = off;
   2716 		error = wapbl_circ_read(wr, wr->wr_scratch, logblklen, &off);
   2717 		if (error)
   2718 			goto errout;
   2719 		wcn = (struct wapbl_wc_null *)wr->wr_scratch;
   2720 		switch (wcn->wc_type) {
   2721 		case WAPBL_WC_BLOCKS:
   2722 			wapbl_replay_process_blocks(wr, &off);
   2723 			break;
   2724 
   2725 		case WAPBL_WC_REVOCATIONS:
   2726 			wapbl_replay_process_revocations(wr);
   2727 			break;
   2728 
   2729 		case WAPBL_WC_INODES:
   2730 			wapbl_replay_process_inodes(wr, saveoff, off);
   2731 			break;
   2732 
   2733 		default:
   2734 			printf("Unrecognized wapbl type: 0x%08x\n",
   2735 			       wcn->wc_type);
   2736  			error = EFTYPE;
   2737 			goto errout;
   2738 		}
   2739 		wapbl_circ_advance(wr, wcn->wc_len, &saveoff);
   2740 		if (off != saveoff) {
   2741 			printf("wapbl_replay: corrupted records\n");
   2742 			error = EFTYPE;
   2743 			goto errout;
   2744 		}
   2745 	}
   2746 	return 0;
   2747 
   2748  errout:
   2749 	wapbl_blkhash_clear(wr);
   2750 	return error;
   2751 }
   2752 
   2753 #if 0
   2754 int
   2755 wapbl_replay_verify(struct wapbl_replay *wr, struct vnode *fsdevvp)
   2756 {
   2757 	off_t off;
   2758 	int mismatchcnt = 0;
   2759 	int logblklen = 1 << wr->wr_log_dev_bshift;
   2760 	int fsblklen = 1 << wr->wr_fs_dev_bshift;
   2761 	void *scratch1 = wapbl_alloc(MAXBSIZE);
   2762 	void *scratch2 = wapbl_alloc(MAXBSIZE);
   2763 	int error = 0;
   2764 
   2765 	KDASSERT(wapbl_replay_isopen(wr));
   2766 
   2767 	off = wch->wc_tail;
   2768 	while (off != wch->wc_head) {
   2769 		struct wapbl_wc_null *wcn;
   2770 #ifdef DEBUG
   2771 		off_t saveoff = off;
   2772 #endif
   2773 		error = wapbl_circ_read(wr, wr->wr_scratch, logblklen, &off);
   2774 		if (error)
   2775 			goto out;
   2776 		wcn = (struct wapbl_wc_null *)wr->wr_scratch;
   2777 		switch (wcn->wc_type) {
   2778 		case WAPBL_WC_BLOCKS:
   2779 			{
   2780 				struct wapbl_wc_blocklist *wc =
   2781 				    (struct wapbl_wc_blocklist *)wr->wr_scratch;
   2782 				int i;
   2783 				for (i = 0; i < wc->wc_blkcount; i++) {
   2784 					int foundcnt = 0;
   2785 					int dirtycnt = 0;
   2786 					int j, n;
   2787 					/*
   2788 					 * Check each physical block into the
   2789 					 * hashtable independently
   2790 					 */
   2791 					n = wc->wc_blocks[i].wc_dlen >>
   2792 					    wch->wc_fs_dev_bshift;
   2793 					for (j = 0; j < n; j++) {
   2794 						struct wapbl_blk *wb =
   2795 						   wapbl_blkhash_get(wr,
   2796 						   wapbl_block_daddr(wc, i, j, fsblklen));
   2797 						if (wb && (wb->wb_off == off)) {
   2798 							foundcnt++;
   2799 							error =
   2800 							    wapbl_circ_read(wr,
   2801 							    scratch1, fsblklen,
   2802 							    &off);
   2803 							if (error)
   2804 								goto out;
   2805 							error =
   2806 							    wapbl_read(scratch2,
   2807 							    fsblklen, fsdevvp,
   2808 							    wb->wb_blk);
   2809 							if (error)
   2810 								goto out;
   2811 							if (memcmp(scratch1,
   2812 								   scratch2,
   2813 								   fsblklen)) {
   2814 								printf(
   2815 		"wapbl_verify: mismatch block %"PRId64" at off %"PRIdMAX"\n",
   2816 		wb->wb_blk, (intmax_t)off);
   2817 								dirtycnt++;
   2818 								mismatchcnt++;
   2819 							}
   2820 						} else {
   2821 							wapbl_circ_advance(wr,
   2822 							    fsblklen, &off);
   2823 						}
   2824 					}
   2825 #if 0
   2826 					/*
   2827 					 * If all of the blocks in an entry
   2828 					 * are clean, then remove all of its
   2829 					 * blocks from the hashtable since they
   2830 					 * never will need replay.
   2831 					 */
   2832 					if ((foundcnt != 0) &&
   2833 					    (dirtycnt == 0)) {
   2834 						off = saveoff;
   2835 						wapbl_circ_advance(wr,
   2836 						    logblklen, &off);
   2837 						for (j = 0; j < n; j++) {
   2838 							struct wapbl_blk *wb =
   2839 							   wapbl_blkhash_get(wr,
   2840 							   wapbl_block_daddr(wc, i, j, fsblklen));
   2841 							if (wb &&
   2842 							  (wb->wb_off == off)) {
   2843 								wapbl_blkhash_rem(wr, wb->wb_blk);
   2844 							}
   2845 							wapbl_circ_advance(wr,
   2846 							    fsblklen, &off);
   2847 						}
   2848 					}
   2849 #endif
   2850 				}
   2851 			}
   2852 			break;
   2853 		case WAPBL_WC_REVOCATIONS:
   2854 		case WAPBL_WC_INODES:
   2855 			break;
   2856 		default:
   2857 			KASSERT(0);
   2858 		}
   2859 #ifdef DEBUG
   2860 		wapbl_circ_advance(wr, wcn->wc_len, &saveoff);
   2861 		KASSERT(off == saveoff);
   2862 #endif
   2863 	}
   2864  out:
   2865 	wapbl_free(scratch1, MAXBSIZE);
   2866 	wapbl_free(scratch2, MAXBSIZE);
   2867 	if (!error && mismatchcnt)
   2868 		error = EFTYPE;
   2869 	return error;
   2870 }
   2871 #endif
   2872 
   2873 int
   2874 wapbl_replay_write(struct wapbl_replay *wr, struct vnode *fsdevvp)
   2875 {
   2876 	struct wapbl_blk *wb;
   2877 	size_t i;
   2878 	off_t off;
   2879 	void *scratch;
   2880 	int error = 0;
   2881 	int fsblklen = 1 << wr->wr_fs_dev_bshift;
   2882 
   2883 	KDASSERT(wapbl_replay_isopen(wr));
   2884 
   2885 	scratch = wapbl_alloc(MAXBSIZE);
   2886 
   2887 	for (i = 0; i <= wr->wr_blkhashmask; ++i) {
   2888 		LIST_FOREACH(wb, &wr->wr_blkhash[i], wb_hash) {
   2889 			off = wb->wb_off;
   2890 			error = wapbl_circ_read(wr, scratch, fsblklen, &off);
   2891 			if (error)
   2892 				break;
   2893 			error = wapbl_write(scratch, fsblklen, fsdevvp,
   2894 			    wb->wb_blk);
   2895 			if (error)
   2896 				break;
   2897 		}
   2898 	}
   2899 
   2900 	wapbl_free(scratch, MAXBSIZE);
   2901 	return error;
   2902 }
   2903 
   2904 int
   2905 wapbl_replay_can_read(struct wapbl_replay *wr, daddr_t blk, long len)
   2906 {
   2907 	int fsblklen = 1 << wr->wr_fs_dev_bshift;
   2908 
   2909 	KDASSERT(wapbl_replay_isopen(wr));
   2910 	KASSERT((len % fsblklen) == 0);
   2911 
   2912 	while (len != 0) {
   2913 		struct wapbl_blk *wb = wapbl_blkhash_get(wr, blk);
   2914 		if (wb)
   2915 			return 1;
   2916 		len -= fsblklen;
   2917 	}
   2918 	return 0;
   2919 }
   2920 
   2921 int
   2922 wapbl_replay_read(struct wapbl_replay *wr, void *data, daddr_t blk, long len)
   2923 {
   2924 	int fsblklen = 1 << wr->wr_fs_dev_bshift;
   2925 
   2926 	KDASSERT(wapbl_replay_isopen(wr));
   2927 
   2928 	KASSERT((len % fsblklen) == 0);
   2929 
   2930 	while (len != 0) {
   2931 		struct wapbl_blk *wb = wapbl_blkhash_get(wr, blk);
   2932 		if (wb) {
   2933 			off_t off = wb->wb_off;
   2934 			int error;
   2935 			error = wapbl_circ_read(wr, data, fsblklen, &off);
   2936 			if (error)
   2937 				return error;
   2938 		}
   2939 		data = (uint8_t *)data + fsblklen;
   2940 		len -= fsblklen;
   2941 		blk++;
   2942 	}
   2943 	return 0;
   2944 }
   2945 
   2946 #ifdef _KERNEL
   2947 
   2948 MODULE(MODULE_CLASS_VFS, wapbl, NULL);
   2949 
   2950 static int
   2951 wapbl_modcmd(modcmd_t cmd, void *arg)
   2952 {
   2953 
   2954 	switch (cmd) {
   2955 	case MODULE_CMD_INIT:
   2956 		wapbl_init();
   2957 		return 0;
   2958 	case MODULE_CMD_FINI:
   2959 		return wapbl_fini(true);
   2960 	default:
   2961 		return ENOTTY;
   2962 	}
   2963 }
   2964 #endif /* _KERNEL */
   2965