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