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