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