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