vfs_wapbl.c revision 1.73 1 /* $NetBSD: vfs_wapbl.c,v 1.73 2016/05/07 20:39:33 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.73 2016/05/07 20:39:33 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(bool interface)
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. If wait is true, also wait for all the logged writes to
1540 * complete so that the log is empty on return.
1541 *
1542 * On failure, call the file system's wl_flush_abort callback.
1543 */
1544 int
1545 wapbl_flush(struct wapbl *wl, int waitfor)
1546 {
1547 struct buf *bp;
1548 struct wapbl_entry *we;
1549 off_t off;
1550 off_t head;
1551 off_t tail;
1552 size_t delta = 0;
1553 size_t flushsize;
1554 size_t reserved;
1555 int error = 0;
1556
1557 /*
1558 * Do a quick check to see if a full flush can be skipped
1559 * This assumes that the flush callback does not need to be called
1560 * unless there are other outstanding bufs.
1561 */
1562 if (!waitfor) {
1563 size_t nbufs;
1564 mutex_enter(&wl->wl_mtx); /* XXX need mutex here to
1565 protect the KASSERTS */
1566 nbufs = wl->wl_bufcount;
1567 KASSERT((wl->wl_bufcount == 0) == (wl->wl_bufbytes == 0));
1568 KASSERT((wl->wl_bufcount == 0) == (wl->wl_bcount == 0));
1569 mutex_exit(&wl->wl_mtx);
1570 if (nbufs == 0)
1571 return 0;
1572 }
1573
1574 /*
1575 * XXX we may consider using LK_UPGRADE here
1576 * if we want to call flush from inside a transaction
1577 */
1578 rw_enter(&wl->wl_rwlock, RW_WRITER);
1579 wl->wl_flush(wl->wl_mount, wl->wl_deallocblks, wl->wl_dealloclens,
1580 wl->wl_dealloccnt);
1581
1582 /*
1583 * Now that we are fully locked and flushed,
1584 * do another check for nothing to do.
1585 */
1586 if (wl->wl_bufcount == 0) {
1587 goto wait_out;
1588 }
1589
1590 #if 0
1591 WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
1592 ("wapbl_flush thread %d.%d flushing entries with "
1593 "bufcount=%zu bufbytes=%zu\n",
1594 curproc->p_pid, curlwp->l_lid, wl->wl_bufcount,
1595 wl->wl_bufbytes));
1596 #endif
1597
1598 /* Calculate amount of space needed to flush */
1599 flushsize = wapbl_transaction_len(wl);
1600 if (wapbl_verbose_commit) {
1601 struct timespec ts;
1602 getnanotime(&ts);
1603 printf("%s: %lld.%09ld this transaction = %zu bytes\n",
1604 __func__, (long long)ts.tv_sec,
1605 (long)ts.tv_nsec, flushsize);
1606 }
1607
1608 if (flushsize > (wl->wl_circ_size - wl->wl_reserved_bytes)) {
1609 /*
1610 * XXX this could be handled more gracefully, perhaps place
1611 * only a partial transaction in the log and allow the
1612 * remaining to flush without the protection of the journal.
1613 */
1614 panic("wapbl_flush: current transaction too big to flush");
1615 }
1616
1617 error = wapbl_truncate(wl, flushsize);
1618 if (error)
1619 goto out;
1620
1621 off = wl->wl_head;
1622 KASSERT((off == 0) || (off >= wl->wl_circ_off));
1623 KASSERT((off == 0) || (off < wl->wl_circ_off + wl->wl_circ_size));
1624 error = wapbl_write_blocks(wl, &off);
1625 if (error)
1626 goto out;
1627 error = wapbl_write_revocations(wl, &off);
1628 if (error)
1629 goto out;
1630 error = wapbl_write_inodes(wl, &off);
1631 if (error)
1632 goto out;
1633
1634 reserved = 0;
1635 if (wl->wl_inohashcnt)
1636 reserved = wapbl_transaction_inodes_len(wl);
1637
1638 head = wl->wl_head;
1639 tail = wl->wl_tail;
1640
1641 wapbl_advance_head(wl->wl_circ_size, wl->wl_circ_off, flushsize,
1642 &head, &tail);
1643
1644 KASSERTMSG(head == off,
1645 "lost head! head=%"PRIdMAX" tail=%" PRIdMAX
1646 " off=%"PRIdMAX" flush=%zu",
1647 (intmax_t)head, (intmax_t)tail, (intmax_t)off,
1648 flushsize);
1649
1650 /* Opportunistically move the tail forward if we can */
1651 mutex_enter(&wl->wl_mtx);
1652 delta = wl->wl_reclaimable_bytes;
1653 mutex_exit(&wl->wl_mtx);
1654 wapbl_advance_tail(wl->wl_circ_size, wl->wl_circ_off, delta,
1655 &head, &tail);
1656
1657 error = wapbl_write_commit(wl, head, tail);
1658 if (error)
1659 goto out;
1660
1661 we = pool_get(&wapbl_entry_pool, PR_WAITOK);
1662
1663 #ifdef WAPBL_DEBUG_BUFBYTES
1664 WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
1665 ("wapbl_flush: thread %d.%d head+=%zu tail+=%zu used=%zu"
1666 " unsynced=%zu"
1667 "\n\tbufcount=%zu bufbytes=%zu bcount=%zu deallocs=%d "
1668 "inodes=%d\n",
1669 curproc->p_pid, curlwp->l_lid, flushsize, delta,
1670 wapbl_space_used(wl->wl_circ_size, head, tail),
1671 wl->wl_unsynced_bufbytes, wl->wl_bufcount,
1672 wl->wl_bufbytes, wl->wl_bcount, wl->wl_dealloccnt,
1673 wl->wl_inohashcnt));
1674 #else
1675 WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
1676 ("wapbl_flush: thread %d.%d head+=%zu tail+=%zu used=%zu"
1677 "\n\tbufcount=%zu bufbytes=%zu bcount=%zu deallocs=%d "
1678 "inodes=%d\n",
1679 curproc->p_pid, curlwp->l_lid, flushsize, delta,
1680 wapbl_space_used(wl->wl_circ_size, head, tail),
1681 wl->wl_bufcount, wl->wl_bufbytes, wl->wl_bcount,
1682 wl->wl_dealloccnt, wl->wl_inohashcnt));
1683 #endif
1684
1685
1686 mutex_enter(&bufcache_lock);
1687 mutex_enter(&wl->wl_mtx);
1688
1689 wl->wl_reserved_bytes = reserved;
1690 wl->wl_head = head;
1691 wl->wl_tail = tail;
1692 KASSERT(wl->wl_reclaimable_bytes >= delta);
1693 wl->wl_reclaimable_bytes -= delta;
1694 wl->wl_dealloccnt = 0;
1695 #ifdef WAPBL_DEBUG_BUFBYTES
1696 wl->wl_unsynced_bufbytes += wl->wl_bufbytes;
1697 #endif
1698
1699 we->we_wapbl = wl;
1700 we->we_bufcount = wl->wl_bufcount;
1701 #ifdef WAPBL_DEBUG_BUFBYTES
1702 we->we_unsynced_bufbytes = wl->wl_bufbytes;
1703 #endif
1704 we->we_reclaimable_bytes = flushsize;
1705 we->we_error = 0;
1706 SIMPLEQ_INSERT_TAIL(&wl->wl_entries, we, we_entries);
1707
1708 /*
1709 * this flushes bufs in reverse order than they were queued
1710 * it shouldn't matter, but if we care we could use TAILQ instead.
1711 * XXX Note they will get put on the lru queue when they flush
1712 * so we might actually want to change this to preserve order.
1713 */
1714 while ((bp = LIST_FIRST(&wl->wl_bufs)) != NULL) {
1715 if (bbusy(bp, 0, 0, &wl->wl_mtx)) {
1716 continue;
1717 }
1718 bp->b_iodone = wapbl_biodone;
1719 bp->b_private = we;
1720 bremfree(bp);
1721 wapbl_remove_buf_locked(wl, bp);
1722 mutex_exit(&wl->wl_mtx);
1723 mutex_exit(&bufcache_lock);
1724 bawrite(bp);
1725 mutex_enter(&bufcache_lock);
1726 mutex_enter(&wl->wl_mtx);
1727 }
1728 mutex_exit(&wl->wl_mtx);
1729 mutex_exit(&bufcache_lock);
1730
1731 #if 0
1732 WAPBL_PRINTF(WAPBL_PRINT_FLUSH,
1733 ("wapbl_flush thread %d.%d done flushing entries...\n",
1734 curproc->p_pid, curlwp->l_lid));
1735 #endif
1736
1737 wait_out:
1738
1739 /*
1740 * If the waitfor flag is set, don't return until everything is
1741 * fully flushed and the on disk log is empty.
1742 */
1743 if (waitfor) {
1744 error = wapbl_truncate(wl, wl->wl_circ_size -
1745 wl->wl_reserved_bytes);
1746 }
1747
1748 out:
1749 if (error) {
1750 wl->wl_flush_abort(wl->wl_mount, wl->wl_deallocblks,
1751 wl->wl_dealloclens, wl->wl_dealloccnt);
1752 }
1753
1754 #ifdef WAPBL_DEBUG_PRINT
1755 if (error) {
1756 pid_t pid = -1;
1757 lwpid_t lid = -1;
1758 if (curproc)
1759 pid = curproc->p_pid;
1760 if (curlwp)
1761 lid = curlwp->l_lid;
1762 mutex_enter(&wl->wl_mtx);
1763 #ifdef WAPBL_DEBUG_BUFBYTES
1764 WAPBL_PRINTF(WAPBL_PRINT_ERROR,
1765 ("wapbl_flush: thread %d.%d aborted flush: "
1766 "error = %d\n"
1767 "\tbufcount=%zu bufbytes=%zu bcount=%zu "
1768 "deallocs=%d inodes=%d\n"
1769 "\terrcnt = %d, reclaimable=%zu reserved=%zu "
1770 "unsynced=%zu\n",
1771 pid, lid, error, wl->wl_bufcount,
1772 wl->wl_bufbytes, wl->wl_bcount,
1773 wl->wl_dealloccnt, wl->wl_inohashcnt,
1774 wl->wl_error_count, wl->wl_reclaimable_bytes,
1775 wl->wl_reserved_bytes, wl->wl_unsynced_bufbytes));
1776 SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
1777 WAPBL_PRINTF(WAPBL_PRINT_ERROR,
1778 ("\tentry: bufcount = %zu, reclaimable = %zu, "
1779 "error = %d, unsynced = %zu\n",
1780 we->we_bufcount, we->we_reclaimable_bytes,
1781 we->we_error, we->we_unsynced_bufbytes));
1782 }
1783 #else
1784 WAPBL_PRINTF(WAPBL_PRINT_ERROR,
1785 ("wapbl_flush: thread %d.%d aborted flush: "
1786 "error = %d\n"
1787 "\tbufcount=%zu bufbytes=%zu bcount=%zu "
1788 "deallocs=%d inodes=%d\n"
1789 "\terrcnt = %d, reclaimable=%zu reserved=%zu\n",
1790 pid, lid, error, wl->wl_bufcount,
1791 wl->wl_bufbytes, wl->wl_bcount,
1792 wl->wl_dealloccnt, wl->wl_inohashcnt,
1793 wl->wl_error_count, wl->wl_reclaimable_bytes,
1794 wl->wl_reserved_bytes));
1795 SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
1796 WAPBL_PRINTF(WAPBL_PRINT_ERROR,
1797 ("\tentry: bufcount = %zu, reclaimable = %zu, "
1798 "error = %d\n", we->we_bufcount,
1799 we->we_reclaimable_bytes, we->we_error));
1800 }
1801 #endif
1802 mutex_exit(&wl->wl_mtx);
1803 }
1804 #endif
1805
1806 rw_exit(&wl->wl_rwlock);
1807 return error;
1808 }
1809
1810 /****************************************************************/
1811
1812 void
1813 wapbl_jlock_assert(struct wapbl *wl)
1814 {
1815
1816 KASSERT(rw_lock_held(&wl->wl_rwlock));
1817 }
1818
1819 void
1820 wapbl_junlock_assert(struct wapbl *wl)
1821 {
1822
1823 KASSERT(!rw_write_held(&wl->wl_rwlock));
1824 }
1825
1826 /****************************************************************/
1827
1828 /* locks missing */
1829 void
1830 wapbl_print(struct wapbl *wl,
1831 int full,
1832 void (*pr)(const char *, ...))
1833 {
1834 struct buf *bp;
1835 struct wapbl_entry *we;
1836 (*pr)("wapbl %p", wl);
1837 (*pr)("\nlogvp = %p, devvp = %p, logpbn = %"PRId64"\n",
1838 wl->wl_logvp, wl->wl_devvp, wl->wl_logpbn);
1839 (*pr)("circ = %zu, header = %zu, head = %"PRIdMAX" tail = %"PRIdMAX"\n",
1840 wl->wl_circ_size, wl->wl_circ_off,
1841 (intmax_t)wl->wl_head, (intmax_t)wl->wl_tail);
1842 (*pr)("fs_dev_bshift = %d, log_dev_bshift = %d\n",
1843 wl->wl_log_dev_bshift, wl->wl_fs_dev_bshift);
1844 #ifdef WAPBL_DEBUG_BUFBYTES
1845 (*pr)("bufcount = %zu, bufbytes = %zu bcount = %zu reclaimable = %zu "
1846 "reserved = %zu errcnt = %d unsynced = %zu\n",
1847 wl->wl_bufcount, wl->wl_bufbytes, wl->wl_bcount,
1848 wl->wl_reclaimable_bytes, wl->wl_reserved_bytes,
1849 wl->wl_error_count, wl->wl_unsynced_bufbytes);
1850 #else
1851 (*pr)("bufcount = %zu, bufbytes = %zu bcount = %zu reclaimable = %zu "
1852 "reserved = %zu errcnt = %d\n", wl->wl_bufcount, wl->wl_bufbytes,
1853 wl->wl_bcount, wl->wl_reclaimable_bytes, wl->wl_reserved_bytes,
1854 wl->wl_error_count);
1855 #endif
1856 (*pr)("\tdealloccnt = %d, dealloclim = %d\n",
1857 wl->wl_dealloccnt, wl->wl_dealloclim);
1858 (*pr)("\tinohashcnt = %d, inohashmask = 0x%08x\n",
1859 wl->wl_inohashcnt, wl->wl_inohashmask);
1860 (*pr)("entries:\n");
1861 SIMPLEQ_FOREACH(we, &wl->wl_entries, we_entries) {
1862 #ifdef WAPBL_DEBUG_BUFBYTES
1863 (*pr)("\tbufcount = %zu, reclaimable = %zu, error = %d, "
1864 "unsynced = %zu\n",
1865 we->we_bufcount, we->we_reclaimable_bytes,
1866 we->we_error, we->we_unsynced_bufbytes);
1867 #else
1868 (*pr)("\tbufcount = %zu, reclaimable = %zu, error = %d\n",
1869 we->we_bufcount, we->we_reclaimable_bytes, we->we_error);
1870 #endif
1871 }
1872 if (full) {
1873 int cnt = 0;
1874 (*pr)("bufs =");
1875 LIST_FOREACH(bp, &wl->wl_bufs, b_wapbllist) {
1876 if (!LIST_NEXT(bp, b_wapbllist)) {
1877 (*pr)(" %p", bp);
1878 } else if ((++cnt % 6) == 0) {
1879 (*pr)(" %p,\n\t", bp);
1880 } else {
1881 (*pr)(" %p,", bp);
1882 }
1883 }
1884 (*pr)("\n");
1885
1886 (*pr)("dealloced blks = ");
1887 {
1888 int i;
1889 cnt = 0;
1890 for (i = 0; i < wl->wl_dealloccnt; i++) {
1891 (*pr)(" %"PRId64":%d,",
1892 wl->wl_deallocblks[i],
1893 wl->wl_dealloclens[i]);
1894 if ((++cnt % 4) == 0) {
1895 (*pr)("\n\t");
1896 }
1897 }
1898 }
1899 (*pr)("\n");
1900
1901 (*pr)("registered inodes = ");
1902 {
1903 int i;
1904 cnt = 0;
1905 for (i = 0; i <= wl->wl_inohashmask; i++) {
1906 struct wapbl_ino_head *wih;
1907 struct wapbl_ino *wi;
1908
1909 wih = &wl->wl_inohash[i];
1910 LIST_FOREACH(wi, wih, wi_hash) {
1911 if (wi->wi_ino == 0)
1912 continue;
1913 (*pr)(" %"PRIu64"/0%06"PRIo32",",
1914 wi->wi_ino, wi->wi_mode);
1915 if ((++cnt % 4) == 0) {
1916 (*pr)("\n\t");
1917 }
1918 }
1919 }
1920 (*pr)("\n");
1921 }
1922 }
1923 }
1924
1925 #if defined(WAPBL_DEBUG) || defined(DDB)
1926 void
1927 wapbl_dump(struct wapbl *wl)
1928 {
1929 #if defined(WAPBL_DEBUG)
1930 if (!wl)
1931 wl = wapbl_debug_wl;
1932 #endif
1933 if (!wl)
1934 return;
1935 wapbl_print(wl, 1, printf);
1936 }
1937 #endif
1938
1939 /****************************************************************/
1940
1941 void
1942 wapbl_register_deallocation(struct wapbl *wl, daddr_t blk, int len)
1943 {
1944
1945 wapbl_jlock_assert(wl);
1946
1947 mutex_enter(&wl->wl_mtx);
1948 /* XXX should eventually instead tie this into resource estimation */
1949 /*
1950 * XXX this panic needs locking/mutex analysis and the
1951 * ability to cope with the failure.
1952 */
1953 /* XXX this XXX doesn't have enough XXX */
1954 if (__predict_false(wl->wl_dealloccnt >= wl->wl_dealloclim))
1955 panic("wapbl_register_deallocation: out of resources");
1956
1957 wl->wl_deallocblks[wl->wl_dealloccnt] = blk;
1958 wl->wl_dealloclens[wl->wl_dealloccnt] = len;
1959 wl->wl_dealloccnt++;
1960 WAPBL_PRINTF(WAPBL_PRINT_ALLOC,
1961 ("wapbl_register_deallocation: blk=%"PRId64" len=%d\n", blk, len));
1962 mutex_exit(&wl->wl_mtx);
1963 }
1964
1965 /****************************************************************/
1966
1967 static void
1968 wapbl_inodetrk_init(struct wapbl *wl, u_int size)
1969 {
1970
1971 wl->wl_inohash = hashinit(size, HASH_LIST, true, &wl->wl_inohashmask);
1972 if (atomic_inc_uint_nv(&wapbl_ino_pool_refcount) == 1) {
1973 pool_init(&wapbl_ino_pool, sizeof(struct wapbl_ino), 0, 0, 0,
1974 "wapblinopl", &pool_allocator_nointr, IPL_NONE);
1975 }
1976 }
1977
1978 static void
1979 wapbl_inodetrk_free(struct wapbl *wl)
1980 {
1981
1982 /* XXX this KASSERT needs locking/mutex analysis */
1983 KASSERT(wl->wl_inohashcnt == 0);
1984 hashdone(wl->wl_inohash, HASH_LIST, wl->wl_inohashmask);
1985 if (atomic_dec_uint_nv(&wapbl_ino_pool_refcount) == 0) {
1986 pool_destroy(&wapbl_ino_pool);
1987 }
1988 }
1989
1990 static struct wapbl_ino *
1991 wapbl_inodetrk_get(struct wapbl *wl, ino_t ino)
1992 {
1993 struct wapbl_ino_head *wih;
1994 struct wapbl_ino *wi;
1995
1996 KASSERT(mutex_owned(&wl->wl_mtx));
1997
1998 wih = &wl->wl_inohash[ino & wl->wl_inohashmask];
1999 LIST_FOREACH(wi, wih, wi_hash) {
2000 if (ino == wi->wi_ino)
2001 return wi;
2002 }
2003 return 0;
2004 }
2005
2006 void
2007 wapbl_register_inode(struct wapbl *wl, ino_t ino, mode_t mode)
2008 {
2009 struct wapbl_ino_head *wih;
2010 struct wapbl_ino *wi;
2011
2012 wi = pool_get(&wapbl_ino_pool, PR_WAITOK);
2013
2014 mutex_enter(&wl->wl_mtx);
2015 if (wapbl_inodetrk_get(wl, ino) == NULL) {
2016 wi->wi_ino = ino;
2017 wi->wi_mode = mode;
2018 wih = &wl->wl_inohash[ino & wl->wl_inohashmask];
2019 LIST_INSERT_HEAD(wih, wi, wi_hash);
2020 wl->wl_inohashcnt++;
2021 WAPBL_PRINTF(WAPBL_PRINT_INODE,
2022 ("wapbl_register_inode: ino=%"PRId64"\n", ino));
2023 mutex_exit(&wl->wl_mtx);
2024 } else {
2025 mutex_exit(&wl->wl_mtx);
2026 pool_put(&wapbl_ino_pool, wi);
2027 }
2028 }
2029
2030 void
2031 wapbl_unregister_inode(struct wapbl *wl, ino_t ino, mode_t mode)
2032 {
2033 struct wapbl_ino *wi;
2034
2035 mutex_enter(&wl->wl_mtx);
2036 wi = wapbl_inodetrk_get(wl, ino);
2037 if (wi) {
2038 WAPBL_PRINTF(WAPBL_PRINT_INODE,
2039 ("wapbl_unregister_inode: ino=%"PRId64"\n", ino));
2040 KASSERT(wl->wl_inohashcnt > 0);
2041 wl->wl_inohashcnt--;
2042 LIST_REMOVE(wi, wi_hash);
2043 mutex_exit(&wl->wl_mtx);
2044
2045 pool_put(&wapbl_ino_pool, wi);
2046 } else {
2047 mutex_exit(&wl->wl_mtx);
2048 }
2049 }
2050
2051 /****************************************************************/
2052
2053 /*
2054 * wapbl_transaction_inodes_len(wl)
2055 *
2056 * Calculate the number of bytes required for inode registration
2057 * log records in wl.
2058 */
2059 static inline size_t
2060 wapbl_transaction_inodes_len(struct wapbl *wl)
2061 {
2062 int blocklen = 1<<wl->wl_log_dev_bshift;
2063 int iph;
2064
2065 /* Calculate number of inodes described in a inodelist header */
2066 iph = (blocklen - offsetof(struct wapbl_wc_inodelist, wc_inodes)) /
2067 sizeof(((struct wapbl_wc_inodelist *)0)->wc_inodes[0]);
2068
2069 KASSERT(iph > 0);
2070
2071 return MAX(1, howmany(wl->wl_inohashcnt, iph)) * blocklen;
2072 }
2073
2074
2075 /*
2076 * wapbl_transaction_len(wl)
2077 *
2078 * Calculate number of bytes required for all log records in wl.
2079 */
2080 static size_t
2081 wapbl_transaction_len(struct wapbl *wl)
2082 {
2083 int blocklen = 1<<wl->wl_log_dev_bshift;
2084 size_t len;
2085 int bph;
2086
2087 /* Calculate number of blocks described in a blocklist header */
2088 bph = (blocklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) /
2089 sizeof(((struct wapbl_wc_blocklist *)0)->wc_blocks[0]);
2090
2091 KASSERT(bph > 0);
2092
2093 len = wl->wl_bcount;
2094 len += howmany(wl->wl_bufcount, bph) * blocklen;
2095 len += howmany(wl->wl_dealloccnt, bph) * blocklen;
2096 len += wapbl_transaction_inodes_len(wl);
2097
2098 return len;
2099 }
2100
2101 /*
2102 * wapbl_cache_sync(wl, msg)
2103 *
2104 * Issue DIOCCACHESYNC to wl->wl_devvp.
2105 *
2106 * If sysctl(vfs.wapbl.verbose_commit) >= 2, print a message
2107 * including msg about the duration of the cache sync.
2108 */
2109 static int
2110 wapbl_cache_sync(struct wapbl *wl, const char *msg)
2111 {
2112 const bool verbose = wapbl_verbose_commit >= 2;
2113 struct bintime start_time;
2114 int force = 1;
2115 int error;
2116
2117 if (!wapbl_flush_disk_cache) {
2118 return 0;
2119 }
2120 if (verbose) {
2121 bintime(&start_time);
2122 }
2123 error = VOP_IOCTL(wl->wl_devvp, DIOCCACHESYNC, &force,
2124 FWRITE, FSCRED);
2125 if (error) {
2126 WAPBL_PRINTF(WAPBL_PRINT_ERROR,
2127 ("wapbl_cache_sync: DIOCCACHESYNC on dev 0x%x "
2128 "returned %d\n", wl->wl_devvp->v_rdev, error));
2129 }
2130 if (verbose) {
2131 struct bintime d;
2132 struct timespec ts;
2133
2134 bintime(&d);
2135 bintime_sub(&d, &start_time);
2136 bintime2timespec(&d, &ts);
2137 printf("wapbl_cache_sync: %s: dev 0x%jx %ju.%09lu\n",
2138 msg, (uintmax_t)wl->wl_devvp->v_rdev,
2139 (uintmax_t)ts.tv_sec, ts.tv_nsec);
2140 }
2141 return error;
2142 }
2143
2144 /*
2145 * wapbl_write_commit(wl, head, tail)
2146 *
2147 * Issue a disk cache sync to wait for all pending writes to the
2148 * log to complete, and then synchronously commit the current
2149 * circular queue head and tail to the log, in the next of two
2150 * locations for commit headers on disk.
2151 *
2152 * Increment the generation number. If the generation number
2153 * rolls over to zero, then a subsequent commit would appear to
2154 * have an older generation than this one -- in that case, issue a
2155 * duplicate commit to avoid this.
2156 *
2157 * => Caller must have exclusive access to wl, either by holding
2158 * wl->wl_rwlock for writer or by being wapbl_start before anyone
2159 * else has seen wl.
2160 */
2161 static int
2162 wapbl_write_commit(struct wapbl *wl, off_t head, off_t tail)
2163 {
2164 struct wapbl_wc_header *wc = wl->wl_wc_header;
2165 struct timespec ts;
2166 int error;
2167 daddr_t pbn;
2168
2169 error = wapbl_buffered_flush(wl);
2170 if (error)
2171 return error;
2172 /*
2173 * flush disk cache to ensure that blocks we've written are actually
2174 * written to the stable storage before the commit header.
2175 *
2176 * XXX Calc checksum here, instead we do this for now
2177 */
2178 wapbl_cache_sync(wl, "1");
2179
2180 wc->wc_head = head;
2181 wc->wc_tail = tail;
2182 wc->wc_checksum = 0;
2183 wc->wc_version = 1;
2184 getnanotime(&ts);
2185 wc->wc_time = ts.tv_sec;
2186 wc->wc_timensec = ts.tv_nsec;
2187
2188 WAPBL_PRINTF(WAPBL_PRINT_WRITE,
2189 ("wapbl_write_commit: head = %"PRIdMAX "tail = %"PRIdMAX"\n",
2190 (intmax_t)head, (intmax_t)tail));
2191
2192 /*
2193 * write the commit header.
2194 *
2195 * XXX if generation will rollover, then first zero
2196 * over second commit header before trying to write both headers.
2197 */
2198
2199 pbn = wl->wl_logpbn + (wc->wc_generation % 2);
2200 #ifdef _KERNEL
2201 pbn = btodb(pbn << wc->wc_log_dev_bshift);
2202 #endif
2203 error = wapbl_buffered_write(wc, wc->wc_len, wl, pbn);
2204 if (error)
2205 return error;
2206 error = wapbl_buffered_flush(wl);
2207 if (error)
2208 return error;
2209
2210 /*
2211 * flush disk cache to ensure that the commit header is actually
2212 * written before meta data blocks.
2213 */
2214 wapbl_cache_sync(wl, "2");
2215
2216 /*
2217 * If the generation number was zero, write it out a second time.
2218 * This handles initialization and generation number rollover
2219 */
2220 if (wc->wc_generation++ == 0) {
2221 error = wapbl_write_commit(wl, head, tail);
2222 /*
2223 * This panic should be able to be removed if we do the
2224 * zero'ing mentioned above, and we are certain to roll
2225 * back generation number on failure.
2226 */
2227 if (error)
2228 panic("wapbl_write_commit: error writing duplicate "
2229 "log header: %d", error);
2230 }
2231 return 0;
2232 }
2233
2234 /*
2235 * wapbl_write_blocks(wl, offp)
2236 *
2237 * Write all pending physical blocks in the current transaction
2238 * from wapbl_add_buf to the log on disk, adding to the circular
2239 * queue head at byte offset *offp, and returning the new head's
2240 * byte offset in *offp.
2241 */
2242 static int
2243 wapbl_write_blocks(struct wapbl *wl, off_t *offp)
2244 {
2245 struct wapbl_wc_blocklist *wc =
2246 (struct wapbl_wc_blocklist *)wl->wl_wc_scratch;
2247 int blocklen = 1<<wl->wl_log_dev_bshift;
2248 int bph;
2249 struct buf *bp;
2250 off_t off = *offp;
2251 int error;
2252 size_t padding;
2253
2254 KASSERT(rw_write_held(&wl->wl_rwlock));
2255
2256 bph = (blocklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) /
2257 sizeof(((struct wapbl_wc_blocklist *)0)->wc_blocks[0]);
2258
2259 bp = LIST_FIRST(&wl->wl_bufs);
2260
2261 while (bp) {
2262 int cnt;
2263 struct buf *obp = bp;
2264
2265 KASSERT(bp->b_flags & B_LOCKED);
2266
2267 wc->wc_type = WAPBL_WC_BLOCKS;
2268 wc->wc_len = blocklen;
2269 wc->wc_blkcount = 0;
2270 while (bp && (wc->wc_blkcount < bph)) {
2271 /*
2272 * Make sure all the physical block numbers are up to
2273 * date. If this is not always true on a given
2274 * filesystem, then VOP_BMAP must be called. We
2275 * could call VOP_BMAP here, or else in the filesystem
2276 * specific flush callback, although neither of those
2277 * solutions allow us to take the vnode lock. If a
2278 * filesystem requires that we must take the vnode lock
2279 * to call VOP_BMAP, then we can probably do it in
2280 * bwrite when the vnode lock should already be held
2281 * by the invoking code.
2282 */
2283 KASSERT((bp->b_vp->v_type == VBLK) ||
2284 (bp->b_blkno != bp->b_lblkno));
2285 KASSERT(bp->b_blkno > 0);
2286
2287 wc->wc_blocks[wc->wc_blkcount].wc_daddr = bp->b_blkno;
2288 wc->wc_blocks[wc->wc_blkcount].wc_dlen = bp->b_bcount;
2289 wc->wc_len += bp->b_bcount;
2290 wc->wc_blkcount++;
2291 bp = LIST_NEXT(bp, b_wapbllist);
2292 }
2293 if (wc->wc_len % blocklen != 0) {
2294 padding = blocklen - wc->wc_len % blocklen;
2295 wc->wc_len += padding;
2296 } else {
2297 padding = 0;
2298 }
2299
2300 WAPBL_PRINTF(WAPBL_PRINT_WRITE,
2301 ("wapbl_write_blocks: len = %u (padding %zu) off = %"PRIdMAX"\n",
2302 wc->wc_len, padding, (intmax_t)off));
2303
2304 error = wapbl_circ_write(wl, wc, blocklen, &off);
2305 if (error)
2306 return error;
2307 bp = obp;
2308 cnt = 0;
2309 while (bp && (cnt++ < bph)) {
2310 error = wapbl_circ_write(wl, bp->b_data,
2311 bp->b_bcount, &off);
2312 if (error)
2313 return error;
2314 bp = LIST_NEXT(bp, b_wapbllist);
2315 }
2316 if (padding) {
2317 void *zero;
2318
2319 zero = wapbl_alloc(padding);
2320 memset(zero, 0, padding);
2321 error = wapbl_circ_write(wl, zero, padding, &off);
2322 wapbl_free(zero, padding);
2323 if (error)
2324 return error;
2325 }
2326 }
2327 *offp = off;
2328 return 0;
2329 }
2330
2331 /*
2332 * wapbl_write_revocations(wl, offp)
2333 *
2334 * Write all pending deallocations in the current transaction from
2335 * wapbl_register_deallocation to the log on disk, adding to the
2336 * circular queue's head at byte offset *offp, and returning the
2337 * new head's byte offset in *offp.
2338 */
2339 static int
2340 wapbl_write_revocations(struct wapbl *wl, off_t *offp)
2341 {
2342 struct wapbl_wc_blocklist *wc =
2343 (struct wapbl_wc_blocklist *)wl->wl_wc_scratch;
2344 int i;
2345 int blocklen = 1<<wl->wl_log_dev_bshift;
2346 int bph;
2347 off_t off = *offp;
2348 int error;
2349
2350 if (wl->wl_dealloccnt == 0)
2351 return 0;
2352
2353 bph = (blocklen - offsetof(struct wapbl_wc_blocklist, wc_blocks)) /
2354 sizeof(((struct wapbl_wc_blocklist *)0)->wc_blocks[0]);
2355
2356 i = 0;
2357 while (i < wl->wl_dealloccnt) {
2358 wc->wc_type = WAPBL_WC_REVOCATIONS;
2359 wc->wc_len = blocklen;
2360 wc->wc_blkcount = 0;
2361 while ((i < wl->wl_dealloccnt) && (wc->wc_blkcount < bph)) {
2362 wc->wc_blocks[wc->wc_blkcount].wc_daddr =
2363 wl->wl_deallocblks[i];
2364 wc->wc_blocks[wc->wc_blkcount].wc_dlen =
2365 wl->wl_dealloclens[i];
2366 wc->wc_blkcount++;
2367 i++;
2368 }
2369 WAPBL_PRINTF(WAPBL_PRINT_WRITE,
2370 ("wapbl_write_revocations: len = %u off = %"PRIdMAX"\n",
2371 wc->wc_len, (intmax_t)off));
2372 error = wapbl_circ_write(wl, wc, blocklen, &off);
2373 if (error)
2374 return error;
2375 }
2376 *offp = off;
2377 return 0;
2378 }
2379
2380 /*
2381 * wapbl_write_inodes(wl, offp)
2382 *
2383 * Write all pending inode allocations in the current transaction
2384 * from wapbl_register_inode to the log on disk, adding to the
2385 * circular queue's head at byte offset *offp and returning the
2386 * new head's byte offset in *offp.
2387 */
2388 static int
2389 wapbl_write_inodes(struct wapbl *wl, off_t *offp)
2390 {
2391 struct wapbl_wc_inodelist *wc =
2392 (struct wapbl_wc_inodelist *)wl->wl_wc_scratch;
2393 int i;
2394 int blocklen = 1 << wl->wl_log_dev_bshift;
2395 off_t off = *offp;
2396 int error;
2397
2398 struct wapbl_ino_head *wih;
2399 struct wapbl_ino *wi;
2400 int iph;
2401
2402 iph = (blocklen - offsetof(struct wapbl_wc_inodelist, wc_inodes)) /
2403 sizeof(((struct wapbl_wc_inodelist *)0)->wc_inodes[0]);
2404
2405 i = 0;
2406 wih = &wl->wl_inohash[0];
2407 wi = 0;
2408 do {
2409 wc->wc_type = WAPBL_WC_INODES;
2410 wc->wc_len = blocklen;
2411 wc->wc_inocnt = 0;
2412 wc->wc_clear = (i == 0);
2413 while ((i < wl->wl_inohashcnt) && (wc->wc_inocnt < iph)) {
2414 while (!wi) {
2415 KASSERT((wih - &wl->wl_inohash[0])
2416 <= wl->wl_inohashmask);
2417 wi = LIST_FIRST(wih++);
2418 }
2419 wc->wc_inodes[wc->wc_inocnt].wc_inumber = wi->wi_ino;
2420 wc->wc_inodes[wc->wc_inocnt].wc_imode = wi->wi_mode;
2421 wc->wc_inocnt++;
2422 i++;
2423 wi = LIST_NEXT(wi, wi_hash);
2424 }
2425 WAPBL_PRINTF(WAPBL_PRINT_WRITE,
2426 ("wapbl_write_inodes: len = %u off = %"PRIdMAX"\n",
2427 wc->wc_len, (intmax_t)off));
2428 error = wapbl_circ_write(wl, wc, blocklen, &off);
2429 if (error)
2430 return error;
2431 } while (i < wl->wl_inohashcnt);
2432
2433 *offp = off;
2434 return 0;
2435 }
2436
2437 #endif /* _KERNEL */
2438
2439 /****************************************************************/
2440
2441 struct wapbl_blk {
2442 LIST_ENTRY(wapbl_blk) wb_hash;
2443 daddr_t wb_blk;
2444 off_t wb_off; /* Offset of this block in the log */
2445 };
2446 #define WAPBL_BLKPOOL_MIN 83
2447
2448 static void
2449 wapbl_blkhash_init(struct wapbl_replay *wr, u_int size)
2450 {
2451 if (size < WAPBL_BLKPOOL_MIN)
2452 size = WAPBL_BLKPOOL_MIN;
2453 KASSERT(wr->wr_blkhash == 0);
2454 #ifdef _KERNEL
2455 wr->wr_blkhash = hashinit(size, HASH_LIST, true, &wr->wr_blkhashmask);
2456 #else /* ! _KERNEL */
2457 /* Manually implement hashinit */
2458 {
2459 unsigned long i, hashsize;
2460 for (hashsize = 1; hashsize < size; hashsize <<= 1)
2461 continue;
2462 wr->wr_blkhash = wapbl_alloc(hashsize * sizeof(*wr->wr_blkhash));
2463 for (i = 0; i < hashsize; i++)
2464 LIST_INIT(&wr->wr_blkhash[i]);
2465 wr->wr_blkhashmask = hashsize - 1;
2466 }
2467 #endif /* ! _KERNEL */
2468 }
2469
2470 static void
2471 wapbl_blkhash_free(struct wapbl_replay *wr)
2472 {
2473 KASSERT(wr->wr_blkhashcnt == 0);
2474 #ifdef _KERNEL
2475 hashdone(wr->wr_blkhash, HASH_LIST, wr->wr_blkhashmask);
2476 #else /* ! _KERNEL */
2477 wapbl_free(wr->wr_blkhash,
2478 (wr->wr_blkhashmask + 1) * sizeof(*wr->wr_blkhash));
2479 #endif /* ! _KERNEL */
2480 }
2481
2482 static struct wapbl_blk *
2483 wapbl_blkhash_get(struct wapbl_replay *wr, daddr_t blk)
2484 {
2485 struct wapbl_blk_head *wbh;
2486 struct wapbl_blk *wb;
2487 wbh = &wr->wr_blkhash[blk & wr->wr_blkhashmask];
2488 LIST_FOREACH(wb, wbh, wb_hash) {
2489 if (blk == wb->wb_blk)
2490 return wb;
2491 }
2492 return 0;
2493 }
2494
2495 static void
2496 wapbl_blkhash_ins(struct wapbl_replay *wr, daddr_t blk, off_t off)
2497 {
2498 struct wapbl_blk_head *wbh;
2499 struct wapbl_blk *wb;
2500 wb = wapbl_blkhash_get(wr, blk);
2501 if (wb) {
2502 KASSERT(wb->wb_blk == blk);
2503 wb->wb_off = off;
2504 } else {
2505 wb = wapbl_alloc(sizeof(*wb));
2506 wb->wb_blk = blk;
2507 wb->wb_off = off;
2508 wbh = &wr->wr_blkhash[blk & wr->wr_blkhashmask];
2509 LIST_INSERT_HEAD(wbh, wb, wb_hash);
2510 wr->wr_blkhashcnt++;
2511 }
2512 }
2513
2514 static void
2515 wapbl_blkhash_rem(struct wapbl_replay *wr, daddr_t blk)
2516 {
2517 struct wapbl_blk *wb = wapbl_blkhash_get(wr, blk);
2518 if (wb) {
2519 KASSERT(wr->wr_blkhashcnt > 0);
2520 wr->wr_blkhashcnt--;
2521 LIST_REMOVE(wb, wb_hash);
2522 wapbl_free(wb, sizeof(*wb));
2523 }
2524 }
2525
2526 static void
2527 wapbl_blkhash_clear(struct wapbl_replay *wr)
2528 {
2529 unsigned long i;
2530 for (i = 0; i <= wr->wr_blkhashmask; i++) {
2531 struct wapbl_blk *wb;
2532
2533 while ((wb = LIST_FIRST(&wr->wr_blkhash[i]))) {
2534 KASSERT(wr->wr_blkhashcnt > 0);
2535 wr->wr_blkhashcnt--;
2536 LIST_REMOVE(wb, wb_hash);
2537 wapbl_free(wb, sizeof(*wb));
2538 }
2539 }
2540 KASSERT(wr->wr_blkhashcnt == 0);
2541 }
2542
2543 /****************************************************************/
2544
2545 /*
2546 * wapbl_circ_read(wr, data, len, offp)
2547 *
2548 * Read len bytes into data from the circular queue of wr,
2549 * starting at the linear byte offset *offp, and returning the new
2550 * linear byte offset in *offp.
2551 *
2552 * If the starting linear byte offset precedes wr->wr_circ_off,
2553 * the read instead begins at wr->wr_circ_off. XXX WTF? This
2554 * should be a KASSERT, not a conditional.
2555 */
2556 static int
2557 wapbl_circ_read(struct wapbl_replay *wr, void *data, size_t len, off_t *offp)
2558 {
2559 size_t slen;
2560 off_t off = *offp;
2561 int error;
2562 daddr_t pbn;
2563
2564 KASSERT(((len >> wr->wr_log_dev_bshift) <<
2565 wr->wr_log_dev_bshift) == len);
2566
2567 if (off < wr->wr_circ_off)
2568 off = wr->wr_circ_off;
2569 slen = wr->wr_circ_off + wr->wr_circ_size - off;
2570 if (slen < len) {
2571 pbn = wr->wr_logpbn + (off >> wr->wr_log_dev_bshift);
2572 #ifdef _KERNEL
2573 pbn = btodb(pbn << wr->wr_log_dev_bshift);
2574 #endif
2575 error = wapbl_read(data, slen, wr->wr_devvp, pbn);
2576 if (error)
2577 return error;
2578 data = (uint8_t *)data + slen;
2579 len -= slen;
2580 off = wr->wr_circ_off;
2581 }
2582 pbn = wr->wr_logpbn + (off >> wr->wr_log_dev_bshift);
2583 #ifdef _KERNEL
2584 pbn = btodb(pbn << wr->wr_log_dev_bshift);
2585 #endif
2586 error = wapbl_read(data, len, wr->wr_devvp, pbn);
2587 if (error)
2588 return error;
2589 off += len;
2590 if (off >= wr->wr_circ_off + wr->wr_circ_size)
2591 off = wr->wr_circ_off;
2592 *offp = off;
2593 return 0;
2594 }
2595
2596 /*
2597 * wapbl_circ_advance(wr, len, offp)
2598 *
2599 * Compute the linear byte offset of the circular queue of wr that
2600 * is len bytes past *offp, and store it in *offp.
2601 *
2602 * This is as if wapbl_circ_read, but without actually reading
2603 * anything.
2604 *
2605 * If the starting linear byte offset precedes wr->wr_circ_off, it
2606 * is taken to be wr->wr_circ_off instead. XXX WTF? This should
2607 * be a KASSERT, not a conditional.
2608 */
2609 static void
2610 wapbl_circ_advance(struct wapbl_replay *wr, size_t len, off_t *offp)
2611 {
2612 size_t slen;
2613 off_t off = *offp;
2614
2615 KASSERT(((len >> wr->wr_log_dev_bshift) <<
2616 wr->wr_log_dev_bshift) == len);
2617
2618 if (off < wr->wr_circ_off)
2619 off = wr->wr_circ_off;
2620 slen = wr->wr_circ_off + wr->wr_circ_size - off;
2621 if (slen < len) {
2622 len -= slen;
2623 off = wr->wr_circ_off;
2624 }
2625 off += len;
2626 if (off >= wr->wr_circ_off + wr->wr_circ_size)
2627 off = wr->wr_circ_off;
2628 *offp = off;
2629 }
2630
2631 /****************************************************************/
2632
2633 int
2634 wapbl_replay_start(struct wapbl_replay **wrp, struct vnode *vp,
2635 daddr_t off, size_t count, size_t blksize)
2636 {
2637 struct wapbl_replay *wr;
2638 int error;
2639 struct vnode *devvp;
2640 daddr_t logpbn;
2641 uint8_t *scratch;
2642 struct wapbl_wc_header *wch;
2643 struct wapbl_wc_header *wch2;
2644 /* Use this until we read the actual log header */
2645 int log_dev_bshift = ilog2(blksize);
2646 size_t used;
2647 daddr_t pbn;
2648
2649 WAPBL_PRINTF(WAPBL_PRINT_REPLAY,
2650 ("wapbl_replay_start: vp=%p off=%"PRId64 " count=%zu blksize=%zu\n",
2651 vp, off, count, blksize));
2652
2653 if (off < 0)
2654 return EINVAL;
2655
2656 if (blksize < DEV_BSIZE)
2657 return EINVAL;
2658 if (blksize % DEV_BSIZE)
2659 return EINVAL;
2660
2661 #ifdef _KERNEL
2662 #if 0
2663 /* XXX vp->v_size isn't reliably set for VBLK devices,
2664 * especially root. However, we might still want to verify
2665 * that the full load is readable */
2666 if ((off + count) * blksize > vp->v_size)
2667 return EINVAL;
2668 #endif
2669 if ((error = VOP_BMAP(vp, off, &devvp, &logpbn, 0)) != 0) {
2670 return error;
2671 }
2672 #else /* ! _KERNEL */
2673 devvp = vp;
2674 logpbn = off;
2675 #endif /* ! _KERNEL */
2676
2677 scratch = wapbl_alloc(MAXBSIZE);
2678
2679 pbn = logpbn;
2680 #ifdef _KERNEL
2681 pbn = btodb(pbn << log_dev_bshift);
2682 #endif
2683 error = wapbl_read(scratch, 2<<log_dev_bshift, devvp, pbn);
2684 if (error)
2685 goto errout;
2686
2687 wch = (struct wapbl_wc_header *)scratch;
2688 wch2 =
2689 (struct wapbl_wc_header *)(scratch + (1<<log_dev_bshift));
2690 /* XXX verify checksums and magic numbers */
2691 if (wch->wc_type != WAPBL_WC_HEADER) {
2692 printf("Unrecognized wapbl magic: 0x%08x\n", wch->wc_type);
2693 error = EFTYPE;
2694 goto errout;
2695 }
2696
2697 if (wch2->wc_generation > wch->wc_generation)
2698 wch = wch2;
2699
2700 wr = wapbl_calloc(1, sizeof(*wr));
2701
2702 wr->wr_logvp = vp;
2703 wr->wr_devvp = devvp;
2704 wr->wr_logpbn = logpbn;
2705
2706 wr->wr_scratch = scratch;
2707
2708 wr->wr_log_dev_bshift = wch->wc_log_dev_bshift;
2709 wr->wr_fs_dev_bshift = wch->wc_fs_dev_bshift;
2710 wr->wr_circ_off = wch->wc_circ_off;
2711 wr->wr_circ_size = wch->wc_circ_size;
2712 wr->wr_generation = wch->wc_generation;
2713
2714 used = wapbl_space_used(wch->wc_circ_size, wch->wc_head, wch->wc_tail);
2715
2716 WAPBL_PRINTF(WAPBL_PRINT_REPLAY,
2717 ("wapbl_replay: head=%"PRId64" tail=%"PRId64" off=%"PRId64
2718 " len=%"PRId64" used=%zu\n",
2719 wch->wc_head, wch->wc_tail, wch->wc_circ_off,
2720 wch->wc_circ_size, used));
2721
2722 wapbl_blkhash_init(wr, (used >> wch->wc_fs_dev_bshift));
2723
2724 error = wapbl_replay_process(wr, wch->wc_head, wch->wc_tail);
2725 if (error) {
2726 wapbl_replay_stop(wr);
2727 wapbl_replay_free(wr);
2728 return error;
2729 }
2730
2731 *wrp = wr;
2732 return 0;
2733
2734 errout:
2735 wapbl_free(scratch, MAXBSIZE);
2736 return error;
2737 }
2738
2739 void
2740 wapbl_replay_stop(struct wapbl_replay *wr)
2741 {
2742
2743 if (!wapbl_replay_isopen(wr))
2744 return;
2745
2746 WAPBL_PRINTF(WAPBL_PRINT_REPLAY, ("wapbl_replay_stop called\n"));
2747
2748 wapbl_free(wr->wr_scratch, MAXBSIZE);
2749 wr->wr_scratch = NULL;
2750
2751 wr->wr_logvp = NULL;
2752
2753 wapbl_blkhash_clear(wr);
2754 wapbl_blkhash_free(wr);
2755 }
2756
2757 void
2758 wapbl_replay_free(struct wapbl_replay *wr)
2759 {
2760
2761 KDASSERT(!wapbl_replay_isopen(wr));
2762
2763 if (wr->wr_inodes)
2764 wapbl_free(wr->wr_inodes,
2765 wr->wr_inodescnt * sizeof(wr->wr_inodes[0]));
2766 wapbl_free(wr, sizeof(*wr));
2767 }
2768
2769 #ifdef _KERNEL
2770 int
2771 wapbl_replay_isopen1(struct wapbl_replay *wr)
2772 {
2773
2774 return wapbl_replay_isopen(wr);
2775 }
2776 #endif
2777
2778 /*
2779 * calculate the disk address for the i'th block in the wc_blockblist
2780 * offset by j blocks of size blen.
2781 *
2782 * wc_daddr is always a kernel disk address in DEV_BSIZE units that
2783 * was written to the journal.
2784 *
2785 * The kernel needs that address plus the offset in DEV_BSIZE units.
2786 *
2787 * Userland needs that address plus the offset in blen units.
2788 *
2789 */
2790 static daddr_t
2791 wapbl_block_daddr(struct wapbl_wc_blocklist *wc, int i, int j, int blen)
2792 {
2793 daddr_t pbn;
2794
2795 #ifdef _KERNEL
2796 pbn = wc->wc_blocks[i].wc_daddr + btodb(j * blen);
2797 #else
2798 pbn = dbtob(wc->wc_blocks[i].wc_daddr) / blen + j;
2799 #endif
2800
2801 return pbn;
2802 }
2803
2804 static void
2805 wapbl_replay_process_blocks(struct wapbl_replay *wr, off_t *offp)
2806 {
2807 struct wapbl_wc_blocklist *wc =
2808 (struct wapbl_wc_blocklist *)wr->wr_scratch;
2809 int fsblklen = 1 << wr->wr_fs_dev_bshift;
2810 int i, j, n;
2811
2812 for (i = 0; i < wc->wc_blkcount; i++) {
2813 /*
2814 * Enter each physical block into the hashtable independently.
2815 */
2816 n = wc->wc_blocks[i].wc_dlen >> wr->wr_fs_dev_bshift;
2817 for (j = 0; j < n; j++) {
2818 wapbl_blkhash_ins(wr, wapbl_block_daddr(wc, i, j, fsblklen),
2819 *offp);
2820 wapbl_circ_advance(wr, fsblklen, offp);
2821 }
2822 }
2823 }
2824
2825 static void
2826 wapbl_replay_process_revocations(struct wapbl_replay *wr)
2827 {
2828 struct wapbl_wc_blocklist *wc =
2829 (struct wapbl_wc_blocklist *)wr->wr_scratch;
2830 int fsblklen = 1 << wr->wr_fs_dev_bshift;
2831 int i, j, n;
2832
2833 for (i = 0; i < wc->wc_blkcount; i++) {
2834 /*
2835 * Remove any blocks found from the hashtable.
2836 */
2837 n = wc->wc_blocks[i].wc_dlen >> wr->wr_fs_dev_bshift;
2838 for (j = 0; j < n; j++)
2839 wapbl_blkhash_rem(wr, wapbl_block_daddr(wc, i, j, fsblklen));
2840 }
2841 }
2842
2843 static void
2844 wapbl_replay_process_inodes(struct wapbl_replay *wr, off_t oldoff, off_t newoff)
2845 {
2846 struct wapbl_wc_inodelist *wc =
2847 (struct wapbl_wc_inodelist *)wr->wr_scratch;
2848 void *new_inodes;
2849 const size_t oldsize = wr->wr_inodescnt * sizeof(wr->wr_inodes[0]);
2850
2851 KASSERT(sizeof(wr->wr_inodes[0]) == sizeof(wc->wc_inodes[0]));
2852
2853 /*
2854 * Keep track of where we found this so location won't be
2855 * overwritten.
2856 */
2857 if (wc->wc_clear) {
2858 wr->wr_inodestail = oldoff;
2859 wr->wr_inodescnt = 0;
2860 if (wr->wr_inodes != NULL) {
2861 wapbl_free(wr->wr_inodes, oldsize);
2862 wr->wr_inodes = NULL;
2863 }
2864 }
2865 wr->wr_inodeshead = newoff;
2866 if (wc->wc_inocnt == 0)
2867 return;
2868
2869 new_inodes = wapbl_alloc((wr->wr_inodescnt + wc->wc_inocnt) *
2870 sizeof(wr->wr_inodes[0]));
2871 if (wr->wr_inodes != NULL) {
2872 memcpy(new_inodes, wr->wr_inodes, oldsize);
2873 wapbl_free(wr->wr_inodes, oldsize);
2874 }
2875 wr->wr_inodes = new_inodes;
2876 memcpy(&wr->wr_inodes[wr->wr_inodescnt], wc->wc_inodes,
2877 wc->wc_inocnt * sizeof(wr->wr_inodes[0]));
2878 wr->wr_inodescnt += wc->wc_inocnt;
2879 }
2880
2881 static int
2882 wapbl_replay_process(struct wapbl_replay *wr, off_t head, off_t tail)
2883 {
2884 off_t off;
2885 int error;
2886
2887 int logblklen = 1 << wr->wr_log_dev_bshift;
2888
2889 wapbl_blkhash_clear(wr);
2890
2891 off = tail;
2892 while (off != head) {
2893 struct wapbl_wc_null *wcn;
2894 off_t saveoff = off;
2895 error = wapbl_circ_read(wr, wr->wr_scratch, logblklen, &off);
2896 if (error)
2897 goto errout;
2898 wcn = (struct wapbl_wc_null *)wr->wr_scratch;
2899 switch (wcn->wc_type) {
2900 case WAPBL_WC_BLOCKS:
2901 wapbl_replay_process_blocks(wr, &off);
2902 break;
2903
2904 case WAPBL_WC_REVOCATIONS:
2905 wapbl_replay_process_revocations(wr);
2906 break;
2907
2908 case WAPBL_WC_INODES:
2909 wapbl_replay_process_inodes(wr, saveoff, off);
2910 break;
2911
2912 default:
2913 printf("Unrecognized wapbl type: 0x%08x\n",
2914 wcn->wc_type);
2915 error = EFTYPE;
2916 goto errout;
2917 }
2918 wapbl_circ_advance(wr, wcn->wc_len, &saveoff);
2919 if (off != saveoff) {
2920 printf("wapbl_replay: corrupted records\n");
2921 error = EFTYPE;
2922 goto errout;
2923 }
2924 }
2925 return 0;
2926
2927 errout:
2928 wapbl_blkhash_clear(wr);
2929 return error;
2930 }
2931
2932 #if 0
2933 int
2934 wapbl_replay_verify(struct wapbl_replay *wr, struct vnode *fsdevvp)
2935 {
2936 off_t off;
2937 int mismatchcnt = 0;
2938 int logblklen = 1 << wr->wr_log_dev_bshift;
2939 int fsblklen = 1 << wr->wr_fs_dev_bshift;
2940 void *scratch1 = wapbl_alloc(MAXBSIZE);
2941 void *scratch2 = wapbl_alloc(MAXBSIZE);
2942 int error = 0;
2943
2944 KDASSERT(wapbl_replay_isopen(wr));
2945
2946 off = wch->wc_tail;
2947 while (off != wch->wc_head) {
2948 struct wapbl_wc_null *wcn;
2949 #ifdef DEBUG
2950 off_t saveoff = off;
2951 #endif
2952 error = wapbl_circ_read(wr, wr->wr_scratch, logblklen, &off);
2953 if (error)
2954 goto out;
2955 wcn = (struct wapbl_wc_null *)wr->wr_scratch;
2956 switch (wcn->wc_type) {
2957 case WAPBL_WC_BLOCKS:
2958 {
2959 struct wapbl_wc_blocklist *wc =
2960 (struct wapbl_wc_blocklist *)wr->wr_scratch;
2961 int i;
2962 for (i = 0; i < wc->wc_blkcount; i++) {
2963 int foundcnt = 0;
2964 int dirtycnt = 0;
2965 int j, n;
2966 /*
2967 * Check each physical block into the
2968 * hashtable independently
2969 */
2970 n = wc->wc_blocks[i].wc_dlen >>
2971 wch->wc_fs_dev_bshift;
2972 for (j = 0; j < n; j++) {
2973 struct wapbl_blk *wb =
2974 wapbl_blkhash_get(wr,
2975 wapbl_block_daddr(wc, i, j, fsblklen));
2976 if (wb && (wb->wb_off == off)) {
2977 foundcnt++;
2978 error =
2979 wapbl_circ_read(wr,
2980 scratch1, fsblklen,
2981 &off);
2982 if (error)
2983 goto out;
2984 error =
2985 wapbl_read(scratch2,
2986 fsblklen, fsdevvp,
2987 wb->wb_blk);
2988 if (error)
2989 goto out;
2990 if (memcmp(scratch1,
2991 scratch2,
2992 fsblklen)) {
2993 printf(
2994 "wapbl_verify: mismatch block %"PRId64" at off %"PRIdMAX"\n",
2995 wb->wb_blk, (intmax_t)off);
2996 dirtycnt++;
2997 mismatchcnt++;
2998 }
2999 } else {
3000 wapbl_circ_advance(wr,
3001 fsblklen, &off);
3002 }
3003 }
3004 #if 0
3005 /*
3006 * If all of the blocks in an entry
3007 * are clean, then remove all of its
3008 * blocks from the hashtable since they
3009 * never will need replay.
3010 */
3011 if ((foundcnt != 0) &&
3012 (dirtycnt == 0)) {
3013 off = saveoff;
3014 wapbl_circ_advance(wr,
3015 logblklen, &off);
3016 for (j = 0; j < n; j++) {
3017 struct wapbl_blk *wb =
3018 wapbl_blkhash_get(wr,
3019 wapbl_block_daddr(wc, i, j, fsblklen));
3020 if (wb &&
3021 (wb->wb_off == off)) {
3022 wapbl_blkhash_rem(wr, wb->wb_blk);
3023 }
3024 wapbl_circ_advance(wr,
3025 fsblklen, &off);
3026 }
3027 }
3028 #endif
3029 }
3030 }
3031 break;
3032 case WAPBL_WC_REVOCATIONS:
3033 case WAPBL_WC_INODES:
3034 break;
3035 default:
3036 KASSERT(0);
3037 }
3038 #ifdef DEBUG
3039 wapbl_circ_advance(wr, wcn->wc_len, &saveoff);
3040 KASSERT(off == saveoff);
3041 #endif
3042 }
3043 out:
3044 wapbl_free(scratch1, MAXBSIZE);
3045 wapbl_free(scratch2, MAXBSIZE);
3046 if (!error && mismatchcnt)
3047 error = EFTYPE;
3048 return error;
3049 }
3050 #endif
3051
3052 int
3053 wapbl_replay_write(struct wapbl_replay *wr, struct vnode *fsdevvp)
3054 {
3055 struct wapbl_blk *wb;
3056 size_t i;
3057 off_t off;
3058 void *scratch;
3059 int error = 0;
3060 int fsblklen = 1 << wr->wr_fs_dev_bshift;
3061
3062 KDASSERT(wapbl_replay_isopen(wr));
3063
3064 scratch = wapbl_alloc(MAXBSIZE);
3065
3066 for (i = 0; i <= wr->wr_blkhashmask; ++i) {
3067 LIST_FOREACH(wb, &wr->wr_blkhash[i], wb_hash) {
3068 off = wb->wb_off;
3069 error = wapbl_circ_read(wr, scratch, fsblklen, &off);
3070 if (error)
3071 break;
3072 error = wapbl_write(scratch, fsblklen, fsdevvp,
3073 wb->wb_blk);
3074 if (error)
3075 break;
3076 }
3077 }
3078
3079 wapbl_free(scratch, MAXBSIZE);
3080 return error;
3081 }
3082
3083 int
3084 wapbl_replay_can_read(struct wapbl_replay *wr, daddr_t blk, long len)
3085 {
3086 int fsblklen = 1 << wr->wr_fs_dev_bshift;
3087
3088 KDASSERT(wapbl_replay_isopen(wr));
3089 KASSERT((len % fsblklen) == 0);
3090
3091 while (len != 0) {
3092 struct wapbl_blk *wb = wapbl_blkhash_get(wr, blk);
3093 if (wb)
3094 return 1;
3095 len -= fsblklen;
3096 }
3097 return 0;
3098 }
3099
3100 int
3101 wapbl_replay_read(struct wapbl_replay *wr, void *data, daddr_t blk, long len)
3102 {
3103 int fsblklen = 1 << wr->wr_fs_dev_bshift;
3104
3105 KDASSERT(wapbl_replay_isopen(wr));
3106
3107 KASSERT((len % fsblklen) == 0);
3108
3109 while (len != 0) {
3110 struct wapbl_blk *wb = wapbl_blkhash_get(wr, blk);
3111 if (wb) {
3112 off_t off = wb->wb_off;
3113 int error;
3114 error = wapbl_circ_read(wr, data, fsblklen, &off);
3115 if (error)
3116 return error;
3117 }
3118 data = (uint8_t *)data + fsblklen;
3119 len -= fsblklen;
3120 blk++;
3121 }
3122 return 0;
3123 }
3124
3125 #ifdef _KERNEL
3126
3127 MODULE(MODULE_CLASS_VFS, wapbl, NULL);
3128
3129 static int
3130 wapbl_modcmd(modcmd_t cmd, void *arg)
3131 {
3132
3133 switch (cmd) {
3134 case MODULE_CMD_INIT:
3135 wapbl_init();
3136 return 0;
3137 case MODULE_CMD_FINI:
3138 return wapbl_fini(true);
3139 default:
3140 return ENOTTY;
3141 }
3142 }
3143 #endif /* _KERNEL */
3144