lfs_rfw.c revision 1.29 1 /* $NetBSD: lfs_rfw.c,v 1.29 2015/08/12 18:28:01 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: lfs_rfw.c,v 1.29 2015/08/12 18:28:01 dholland Exp $");
34
35 #if defined(_KERNEL_OPT)
36 #include "opt_quota.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/proc.h>
43 #include <sys/kernel.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/kthread.h>
47 #include <sys/buf.h>
48 #include <sys/device.h>
49 #include <sys/mbuf.h>
50 #include <sys/file.h>
51 #include <sys/disklabel.h>
52 #include <sys/ioctl.h>
53 #include <sys/errno.h>
54 #include <sys/malloc.h>
55 #include <sys/pool.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
58 #include <uvm/uvm_extern.h>
59 #include <sys/sysctl.h>
60 #include <sys/conf.h>
61 #include <sys/kauth.h>
62
63 #include <miscfs/specfs/specdev.h>
64
65 #include <ufs/lfs/ulfs_quotacommon.h>
66 #include <ufs/lfs/ulfs_inode.h>
67 #include <ufs/lfs/ulfsmount.h>
68 #include <ufs/lfs/ulfs_extern.h>
69
70 #include <uvm/uvm.h>
71 #include <uvm/uvm_stat.h>
72 #include <uvm/uvm_pager.h>
73 #include <uvm/uvm_pdaemon.h>
74
75 #include <ufs/lfs/lfs.h>
76 #include <ufs/lfs/lfs_accessors.h>
77 #include <ufs/lfs/lfs_kernel.h>
78 #include <ufs/lfs/lfs_extern.h>
79
80 #include <miscfs/genfs/genfs.h>
81 #include <miscfs/genfs/genfs_node.h>
82
83 /*
84 * Roll-forward code.
85 */
86 static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
87 kauth_cred_t, int, int *, struct lwp *);
88
89 extern int lfs_do_rfw;
90
91 /*
92 * Allocate a particular inode with a particular version number, freeing
93 * any previous versions of this inode that may have gone before.
94 * Used by the roll-forward code.
95 *
96 * XXX this function does not have appropriate locking to be used on a live fs;
97 * XXX but something similar could probably be used for an "undelete" call.
98 *
99 * Called with the Ifile inode locked.
100 */
101 int
102 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
103 struct vnode **vpp)
104 {
105 struct vattr va;
106 struct vnode *vp;
107 struct inode *ip;
108 int error;
109
110 ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
111
112 /*
113 * First, just try a vget. If the version number is the one we want,
114 * we don't have to do anything else. If the version number is wrong,
115 * take appropriate action.
116 */
117 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
118 if (error == 0) {
119 DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
120
121 *vpp = vp;
122 ip = VTOI(vp);
123 if (ip->i_gen == vers)
124 return 0;
125 else if (ip->i_gen < vers) {
126 lfs_truncate(vp, (off_t)0, 0, NOCRED);
127 ip->i_gen = ip->i_ffs1_gen = vers;
128 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
129 return 0;
130 } else {
131 DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
132 ino, vers, ip->i_ffs1_gen));
133 vput(vp);
134 *vpp = NULLVP;
135 return EEXIST;
136 }
137 }
138
139 /* Not found, create as regular file. */
140 vattr_null(&va);
141 va.va_type = VREG;
142 va.va_mode = 0;
143 va.va_fileid = ino;
144 va.va_gen = vers;
145 error = vcache_new(fs->lfs_ivnode->v_mount, NULL, &va, NOCRED, &vp);
146 if (error)
147 return error;
148 error = vn_lock(vp, LK_EXCLUSIVE);
149 if (error) {
150 vrele(vp);
151 *vpp = NULLVP;
152 return error;
153 }
154 ip = VTOI(vp);
155 ip->i_nlink = ip->i_ffs1_nlink = 1;
156 *vpp = vp;
157 return 0;
158 }
159
160 /*
161 * Load the appropriate indirect block, and change the appropriate pointer.
162 * Mark the block dirty. Do segment and avail accounting.
163 */
164 static int
165 update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
166 daddr_t ndaddr, size_t size, struct lwp *l)
167 {
168 int error;
169 struct vnode *vp;
170 struct inode *ip;
171 #ifdef DEBUG
172 daddr_t odaddr;
173 struct indir a[ULFS_NIADDR];
174 int num;
175 int i;
176 #endif /* DEBUG */
177 struct buf *bp;
178 SEGUSE *sup;
179
180 KASSERT(lbn >= 0); /* no indirect blocks */
181
182 if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
183 DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
184 " returned %d\n", ino, error));
185 return error;
186 }
187
188 if ((error = lfs_balloc(vp, (lbn << lfs_sb_getbshift(fs)), size,
189 NOCRED, 0, &bp)) != 0) {
190 vput(vp);
191 return (error);
192 }
193 /* No need to write, the block is already on disk */
194 if (bp->b_oflags & BO_DELWRI) {
195 LFS_UNLOCK_BUF(bp);
196 lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
197 /* XXX should this wake up fs->lfs_availsleep? */
198 }
199 brelse(bp, BC_INVAL);
200
201 /*
202 * Extend the file, if it is not large enough already.
203 * XXX this is not exactly right, we don't know how much of the
204 * XXX last block is actually used. We hope that an inode will
205 * XXX appear later to give the correct size.
206 */
207 ip = VTOI(vp);
208 if (ip->i_size <= (lbn << lfs_sb_getbshift(fs))) {
209 u_int64_t newsize;
210
211 if (lbn < ULFS_NDADDR)
212 newsize = ip->i_ffs1_size = (lbn << lfs_sb_getbshift(fs)) +
213 (size - lfs_sb_getfsize(fs)) + 1;
214 else
215 newsize = ip->i_ffs1_size = (lbn << lfs_sb_getbshift(fs)) + 1;
216
217 if (ip->i_size < newsize) {
218 ip->i_size = newsize;
219 /*
220 * tell vm our new size for the case the inode won't
221 * appear later.
222 */
223 uvm_vnp_setsize(vp, newsize);
224 }
225 }
226
227 lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
228
229 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
230 sup->su_nbytes += size;
231 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
232
233 /* differences here should be due to UNWRITTEN indirect blocks. */
234 KASSERT((lfs_lblkno(fs, ip->i_size) > ULFS_NDADDR &&
235 ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
236 ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
237
238 #ifdef DEBUG
239 /* Now look again to make sure it worked */
240 ulfs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
241 for (i = num; i > 0; i--) {
242 if (!a[i].in_exists)
243 panic("update_meta: absent %d lv indirect block", i);
244 }
245 if (LFS_DBTOFSB(fs, odaddr) != ndaddr)
246 DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
247 PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
248 #endif /* DEBUG */
249 vput(vp);
250 return 0;
251 }
252
253 static int
254 update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
255 struct lwp *l)
256 {
257 struct vnode *devvp, *vp;
258 struct inode *ip;
259 union lfs_dinode *dip;
260 struct buf *dbp, *ibp;
261 int error;
262 daddr_t daddr;
263 IFILE *ifp;
264 SEGUSE *sup;
265 unsigned i, num;
266
267 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
268
269 /*
270 * Get the inode, update times and perms.
271 * DO NOT update disk blocks, we do that separately.
272 */
273 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getibsize(fs),
274 0, &dbp);
275 if (error) {
276 DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
277 return error;
278 }
279 num = LFS_INOPB(fs);
280 for (i = num; i-- > 0; ) {
281 dip = (union lfs_dinode *)((char *)dbp->b_data + i * DINOSIZE(fs));
282 if (lfs_dino_getinumber(fs, dip) > LFS_IFILE_INUM) {
283 error = lfs_rf_valloc(fs, lfs_dino_getinumber(fs, dip),
284 lfs_dino_getgen(fs, dip),
285 l, &vp);
286 if (error) {
287 DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
288 " returned %d\n", error));
289 continue;
290 }
291 ip = VTOI(vp);
292 if (lfs_dino_getsize(fs, dip) != ip->i_size)
293 lfs_truncate(vp, lfs_dino_getsize(fs, dip), 0,
294 NOCRED);
295 /* Get mode, link count, size, and times */
296 /* XXX: ugly, simplify */
297 if (fs->lfs_is64) {
298 /*
299 * XXX what about di_extb?
300 */
301 memcpy(ip->i_din.ffs2_din, dip,
302 offsetof(struct lfs64_dinode, di_db[0]));
303 /* Then the rest, except di_blocks */
304 ip->i_ffs2_modrev = dip->u_64.di_modrev;
305 ip->i_ffs2_inumber = dip->u_64.di_inumber;
306 memset(ip->i_din.ffs2_din->di_spare, 0,
307 sizeof(ip->i_din.ffs2_din->di_spare));
308 } else {
309 memcpy(ip->i_din.ffs1_din, dip,
310 offsetof(struct lfs32_dinode, di_db[0]));
311 /* Then the rest, except di_blocks */
312 ip->i_ffs1_flags = dip->u_32.di_flags;
313 ip->i_ffs1_gen = dip->u_32.di_gen;
314 ip->i_ffs1_uid = dip->u_32.di_uid;
315 ip->i_ffs1_gid = dip->u_32.di_gid;
316 ip->i_ffs1_modrev = dip->u_32.di_modrev;
317 }
318
319 /* Then the rest, except di_blocks */
320 ip->i_flags = lfs_dino_getflags(fs, dip);
321 ip->i_gen = lfs_dino_getgen(fs, dip);
322 ip->i_uid = lfs_dino_getuid(fs, dip);
323 ip->i_gid = lfs_dino_getgid(fs, dip);
324
325 ip->i_mode = lfs_dino_getmode(fs, dip);
326 ip->i_nlink = lfs_dino_getnlink(fs, dip);
327 ip->i_size = lfs_dino_getsize(fs, dip);
328
329 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
330
331 /* Re-initialize to get type right */
332 ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
333 &vp);
334 vput(vp);
335
336 /* Record change in location */
337 LFS_IENTRY(ifp, fs, lfs_dino_getinumber(fs, dip), ibp);
338 daddr = lfs_if_getdaddr(fs, ifp);
339 lfs_if_setdaddr(fs, ifp, LFS_DBTOFSB(fs, dbp->b_blkno));
340 error = LFS_BWRITE_LOG(ibp); /* Ifile */
341 /* And do segment accounting */
342 if (lfs_dtosn(fs, daddr) != lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno))) {
343 if (daddr > 0) {
344 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, daddr),
345 ibp);
346 sup->su_nbytes -= DINOSIZE(fs);
347 LFS_WRITESEGENTRY(sup, fs,
348 lfs_dtosn(fs, daddr),
349 ibp);
350 }
351 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
352 ibp);
353 sup->su_nbytes += DINOSIZE(fs);
354 LFS_WRITESEGENTRY(sup, fs,
355 lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
356 ibp);
357 }
358 }
359 }
360 brelse(dbp, BC_AGE);
361
362 return 0;
363 }
364
365 #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */
366 #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */
367
368 static daddr_t
369 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
370 kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
371 {
372 struct vnode *devvp;
373 struct buf *bp, *dbp;
374 int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
375 SEGSUM *ssp;
376 u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
377 daddr_t oldoffset;
378 int32_t *iaddr; /* XXX ondisk32 */
379 FINFO *fip;
380 SEGUSE *sup;
381 size_t size;
382 uint32_t datasum, foundsum;
383
384 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
385 /*
386 * If the segment has a superblock and we're at the top
387 * of the segment, skip the superblock.
388 */
389 if (lfs_sntod(fs, lfs_dtosn(fs, offset)) == offset) {
390 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
391 if (sup->su_flags & SEGUSE_SUPERBLOCK)
392 offset += lfs_btofsb(fs, LFS_SBPAD);
393 brelse(bp, 0);
394 }
395
396 /* Read in the segment summary */
397 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getsumsize(fs),
398 0, &bp);
399 if (error)
400 return -1;
401
402 /* Check summary checksum */
403 ssp = (SEGSUM *)bp->b_data;
404 if (flags & CHECK_CKSUM) {
405 size_t sumstart;
406
407 sumstart = lfs_ss_getsumstart(fs);
408 if (lfs_ss_getsumsum(fs, ssp) !=
409 cksum((char *)ssp + sumstart,
410 lfs_sb_getsumsize(fs) - sumstart)) {
411 DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
412 offset = -1;
413 goto err1;
414 }
415 if (lfs_ss_getnfinfo(fs, ssp) == 0 &&
416 lfs_ss_getninos(fs, ssp) == 0) {
417 DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
418 offset = -1;
419 goto err1;
420 }
421 if (lfs_ss_getcreate(fs, ssp) < lfs_sb_gettstamp(fs)) {
422 DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
423 offset = -1;
424 goto err1;
425 }
426 }
427 if (lfs_sb_getversion(fs) > 1) {
428 if (lfs_ss_getserial(fs, ssp) != nextserial) {
429 DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
430 "\n", offset));
431 offset = -1;
432 goto err1;
433 }
434 if (lfs_ss_getident(fs, ssp) != lfs_sb_getident(fs)) {
435 DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
436 PRIx64 "\n", lfs_ss_getident(fs, ssp),
437 lfs_sb_getident(fs), offset));
438 offset = -1;
439 goto err1;
440 }
441 }
442 if (pseg_flags)
443 *pseg_flags = lfs_ss_getflags(fs, ssp);
444 oldoffset = offset;
445 offset += lfs_btofsb(fs, lfs_sb_getsumsize(fs));
446
447 ninos = howmany(lfs_ss_getninos(fs, ssp), LFS_INOPB(fs));
448 /* XXX ondisk32 */
449 iaddr = (int32_t *)((char*)bp->b_data + lfs_sb_getsumsize(fs) - sizeof(int32_t));
450 if (flags & CHECK_CKSUM) {
451 /* Count blocks */
452 nblocks = 0;
453 fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
454 for (i = 0; i < lfs_ss_getnfinfo(fs, ssp); ++i) {
455 nblocks += lfs_fi_getnblocks(fs, fip);
456 if (lfs_fi_getnblocks(fs, fip) <= 0)
457 break;
458 fip = NEXT_FINFO(fs, fip);
459 }
460 nblocks += ninos;
461 /* Create the sum array */
462 datap = dp = malloc(nblocks * sizeof(u_long),
463 M_SEGMENT, M_WAITOK);
464 }
465
466 /* Handle individual blocks */
467 fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
468 for (i = 0; i < lfs_ss_getnfinfo(fs, ssp) || ninos; ++i) {
469 /* Inode block? */
470 if (ninos && *iaddr == offset) {
471 if (flags & CHECK_CKSUM) {
472 /* Read in the head and add to the buffer */
473 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getbsize(fs),
474 0, &dbp);
475 if (error) {
476 offset = -1;
477 goto err2;
478 }
479 /* XXX this can't be right, on-disk u_long? */
480 (*dp++) = ((u_long *)(dbp->b_data))[0];
481 brelse(dbp, BC_AGE);
482 }
483 if (flags & CHECK_UPDATE) {
484 if ((error = update_inoblk(fs, offset, cred, l))
485 != 0) {
486 offset = -1;
487 goto err2;
488 }
489 }
490 offset += lfs_btofsb(fs, lfs_sb_getibsize(fs));
491 --iaddr;
492 --ninos;
493 --i; /* compensate */
494 continue;
495 }
496 size = lfs_sb_getbsize(fs);
497 for (j = 0; j < lfs_fi_getnblocks(fs, fip); ++j) {
498 if (j == lfs_fi_getnblocks(fs, fip) - 1)
499 size = lfs_fi_getlastlength(fs, fip);
500 if (flags & CHECK_CKSUM) {
501 error = bread(devvp, LFS_FSBTODB(fs, offset), size,
502 0, &dbp);
503 if (error) {
504 offset = -1;
505 goto err2;
506 }
507 (*dp++) = ((u_long *)(dbp->b_data))[0];
508 brelse(dbp, BC_AGE);
509 }
510 /* Account for and update any direct blocks */
511 if ((flags & CHECK_UPDATE) &&
512 lfs_fi_getino(fs, fip) > LFS_IFILE_INUM &&
513 lfs_fi_getblock(fs, fip, j) >= 0) {
514 update_meta(fs, lfs_fi_getino(fs, fip),
515 lfs_fi_getversion(fs, fip),
516 lfs_fi_getblock(fs, fip, j),
517 offset, size, l);
518 }
519 offset += lfs_btofsb(fs, size);
520 }
521 fip = NEXT_FINFO(fs, fip);
522 }
523 /* Checksum the array, compare */
524 datasum = lfs_ss_getdatasum(fs, ssp);
525 foundsum = cksum(datap, nblocks * sizeof(u_long));
526 if ((flags & CHECK_CKSUM) && datasum != foundsum) {
527 DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
528 " (wanted %x got %x)\n",
529 offset, datasum, foundsum));
530 offset = -1;
531 goto err2;
532 }
533
534 /* If we're at the end of the segment, move to the next */
535 if (lfs_dtosn(fs, offset + lfs_btofsb(fs, lfs_sb_getsumsize(fs) + lfs_sb_getbsize(fs))) !=
536 lfs_dtosn(fs, offset)) {
537 if (lfs_dtosn(fs, offset) == lfs_dtosn(fs, lfs_ss_getnext(fs, ssp))) {
538 offset = -1;
539 goto err2;
540 }
541 offset = lfs_ss_getnext(fs, ssp);
542 DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
543 " -> segment %d\n", offset, lfs_dtosn(fs,offset)));
544 }
545
546 if (flags & CHECK_UPDATE) {
547 lfs_sb_subavail(fs, offset - oldoffset);
548 /* Don't clog the buffer queue */
549 mutex_enter(&lfs_lock);
550 if (locked_queue_count > LFS_MAX_BUFS ||
551 locked_queue_bytes > LFS_MAX_BYTES) {
552 lfs_flush(fs, SEGM_CKP, 0);
553 }
554 mutex_exit(&lfs_lock);
555 }
556
557 err2:
558 if (flags & CHECK_CKSUM)
559 free(datap, M_SEGMENT);
560 err1:
561 brelse(bp, BC_AGE);
562
563 /* XXX should we update the serial number even for bad psegs? */
564 if ((flags & CHECK_UPDATE) && offset > 0 && lfs_sb_getversion(fs) > 1)
565 lfs_sb_setserial(fs, nextserial);
566 return offset;
567 }
568
569 void
570 lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
571 {
572 int flags, dirty;
573 daddr_t offset, oldoffset, lastgoodpseg;
574 int sn, curseg, do_rollforward;
575 struct proc *p;
576 kauth_cred_t cred;
577 SEGUSE *sup;
578 struct buf *bp;
579
580 p = l ? l->l_proc : NULL;
581 cred = p ? p->p_cred : NOCRED;
582
583 /*
584 * Roll forward.
585 *
586 * We don't roll forward for v1 filesystems, because
587 * of the danger that the clock was turned back between the last
588 * checkpoint and crash. This would roll forward garbage.
589 *
590 * v2 filesystems don't have this problem because they use a
591 * monotonically increasing serial number instead of a timestamp.
592 */
593 do_rollforward = (!(lfs_sb_getpflags(fs) & LFS_PF_CLEAN) &&
594 lfs_do_rfw && lfs_sb_getversion(fs) > 1 && p != NULL);
595 if (do_rollforward) {
596 u_int64_t nextserial;
597 /*
598 * Phase I: Find the address of the last good partial
599 * segment that was written after the checkpoint. Mark
600 * the segments in question dirty, so they won't be
601 * reallocated.
602 */
603 lastgoodpseg = oldoffset = offset = lfs_sb_getoffset(fs);
604 flags = 0x0;
605 DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
606 PRIx64 "\n", offset));
607 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
608 if (!(sup->su_flags & SEGUSE_DIRTY))
609 lfs_sb_subnclean(fs, 1);
610 sup->su_flags |= SEGUSE_DIRTY;
611 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
612 nextserial = lfs_sb_getserial(fs) + 1;
613 while ((offset = check_segsum(fs, offset, nextserial,
614 cred, CHECK_CKSUM, &flags, l)) > 0) {
615 nextserial++;
616 if (lfs_sntod(fs, oldoffset) != lfs_sntod(fs, offset)) {
617 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
618 bp);
619 if (!(sup->su_flags & SEGUSE_DIRTY))
620 lfs_sb_subnclean(fs, 1);
621 sup->su_flags |= SEGUSE_DIRTY;
622 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
623 bp);
624 }
625
626 DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
627 PRIx64 "\n", offset));
628 if (flags & SS_DIROP) {
629 DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
630 PRIx64 "\n", oldoffset));
631 if (!(flags & SS_CONT)) {
632 DLOG((DLOG_RF, "lfs_mountfs: dirops end "
633 "at 0x%" PRIx64 "\n", oldoffset));
634 }
635 }
636 if (!(flags & SS_CONT))
637 lastgoodpseg = offset;
638 oldoffset = offset;
639 }
640 if (flags & SS_CONT) {
641 DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
642 "dirops discarded\n"));
643 }
644 DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
645 "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
646 oldoffset = lfs_sb_getoffset(fs);
647 if (lfs_sb_getoffset(fs) != lastgoodpseg) {
648 /* Don't overwrite what we're trying to preserve */
649 offset = lfs_sb_getoffset(fs);
650 lfs_sb_setoffset(fs, lastgoodpseg);
651 lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_dtosn(fs, lfs_sb_getoffset(fs))));
652 for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs));;) {
653 sn = (sn + 1) % lfs_sb_getnseg(fs);
654 if (sn == curseg)
655 panic("lfs_mountfs: no clean segments");
656 LFS_SEGENTRY(sup, fs, sn, bp);
657 dirty = (sup->su_flags & SEGUSE_DIRTY);
658 brelse(bp, 0);
659 if (!dirty)
660 break;
661 }
662 lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
663
664 /*
665 * Phase II: Roll forward from the first superblock.
666 */
667 while (offset != lastgoodpseg) {
668 DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
669 PRIx64 "\n", offset));
670 offset = check_segsum(fs, offset,
671 lfs_sb_getserial(fs) + 1, cred, CHECK_UPDATE,
672 NULL, l);
673 }
674
675 /*
676 * Finish: flush our changes to disk.
677 */
678 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
679 DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
680 "recovered %jd blocks\n",
681 (intmax_t)(lastgoodpseg - oldoffset)));
682 }
683 DLOG((DLOG_RF, "LFS roll forward complete\n"));
684 }
685 }
686