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