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