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