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