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