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