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