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