Home | History | Annotate | Line # | Download | only in lfs
lfs_rfw.c revision 1.28
      1 /*	$NetBSD: lfs_rfw.c,v 1.28 2015/08/12 18:27: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.28 2015/08/12 18:27: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 	struct ulfs1_dinode *dip;
    260 	struct buf *dbp, *ibp;
    261 	int error;
    262 	daddr_t daddr;
    263 	IFILE *ifp;
    264 	SEGUSE *sup;
    265 
    266 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    267 
    268 	/*
    269 	 * Get the inode, update times and perms.
    270 	 * DO NOT update disk blocks, we do that separately.
    271 	 */
    272 	error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getibsize(fs),
    273 	    0, &dbp);
    274 	if (error) {
    275 		DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
    276 		return error;
    277 	}
    278 	dip = ((struct ulfs1_dinode *)(dbp->b_data)) + LFS_INOPB(fs);
    279 	while (--dip >= (struct ulfs1_dinode *)dbp->b_data) {
    280 		if (dip->di_inumber > LFS_IFILE_INUM) {
    281 			error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
    282 					      l, &vp);
    283 			if (error) {
    284 				DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
    285 				      " returned %d\n", error));
    286 				continue;
    287 			}
    288 			ip = VTOI(vp);
    289 			if (dip->di_size != ip->i_size)
    290 				lfs_truncate(vp, dip->di_size, 0, NOCRED);
    291 			/* Get mode, link count, size, and times */
    292 			memcpy(ip->i_din.ffs1_din, dip,
    293 			       offsetof(struct ulfs1_dinode, di_db[0]));
    294 
    295 			/* Then the rest, except di_blocks */
    296 			ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
    297 			ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
    298 			ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
    299 			ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
    300 
    301 			ip->i_mode = ip->i_ffs1_mode;
    302 			ip->i_nlink = ip->i_ffs1_nlink;
    303 			ip->i_size = ip->i_ffs1_size;
    304 
    305 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    306 
    307 			/* Re-initialize to get type right */
    308 			ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
    309 				  &vp);
    310 			vput(vp);
    311 
    312 			/* Record change in location */
    313 			LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
    314 			daddr = lfs_if_getdaddr(fs, ifp);
    315 			lfs_if_setdaddr(fs, ifp, LFS_DBTOFSB(fs, dbp->b_blkno));
    316 			error = LFS_BWRITE_LOG(ibp); /* Ifile */
    317 			/* And do segment accounting */
    318 			if (lfs_dtosn(fs, daddr) != lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno))) {
    319 				if (daddr > 0) {
    320 					LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, daddr),
    321 						     ibp);
    322 					sup->su_nbytes -= sizeof (struct ulfs1_dinode);
    323 					LFS_WRITESEGENTRY(sup, fs,
    324 							  lfs_dtosn(fs, daddr),
    325 							  ibp);
    326 				}
    327 				LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
    328 					     ibp);
    329 				sup->su_nbytes += sizeof (struct ulfs1_dinode);
    330 				LFS_WRITESEGENTRY(sup, fs,
    331 						  lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
    332 						  ibp);
    333 			}
    334 		}
    335 	}
    336 	brelse(dbp, BC_AGE);
    337 
    338 	return 0;
    339 }
    340 
    341 #define CHECK_CKSUM   0x0001  /* Check the checksum to make sure it's valid */
    342 #define CHECK_UPDATE  0x0002  /* Update Ifile for new data blocks / inodes */
    343 
    344 static daddr_t
    345 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
    346 	     kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
    347 {
    348 	struct vnode *devvp;
    349 	struct buf *bp, *dbp;
    350 	int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
    351 	SEGSUM *ssp;
    352 	u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
    353 	daddr_t oldoffset;
    354 	int32_t *iaddr;	/* XXX ondisk32 */
    355 	FINFO *fip;
    356 	SEGUSE *sup;
    357 	size_t size;
    358 	uint32_t datasum, foundsum;
    359 
    360 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    361 	/*
    362 	 * If the segment has a superblock and we're at the top
    363 	 * of the segment, skip the superblock.
    364 	 */
    365 	if (lfs_sntod(fs, lfs_dtosn(fs, offset)) == offset) {
    366 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    367 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
    368 			offset += lfs_btofsb(fs, LFS_SBPAD);
    369 		brelse(bp, 0);
    370 	}
    371 
    372 	/* Read in the segment summary */
    373 	error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getsumsize(fs),
    374 	    0, &bp);
    375 	if (error)
    376 		return -1;
    377 
    378 	/* Check summary checksum */
    379 	ssp = (SEGSUM *)bp->b_data;
    380 	if (flags & CHECK_CKSUM) {
    381 		size_t sumstart;
    382 
    383 		sumstart = lfs_ss_getsumstart(fs);
    384 		if (lfs_ss_getsumsum(fs, ssp) !=
    385 		    cksum((char *)ssp + sumstart,
    386 			  lfs_sb_getsumsize(fs) - sumstart)) {
    387 			DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
    388 			offset = -1;
    389 			goto err1;
    390 		}
    391 		if (lfs_ss_getnfinfo(fs, ssp) == 0 &&
    392 		    lfs_ss_getninos(fs, ssp) == 0) {
    393 			DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
    394 			offset = -1;
    395 			goto err1;
    396 		}
    397 		if (lfs_ss_getcreate(fs, ssp) < lfs_sb_gettstamp(fs)) {
    398 			DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
    399 			offset = -1;
    400 			goto err1;
    401 		}
    402 	}
    403 	if (lfs_sb_getversion(fs) > 1) {
    404 		if (lfs_ss_getserial(fs, ssp) != nextserial) {
    405 			DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
    406 			      "\n", offset));
    407 			offset = -1;
    408 			goto err1;
    409 		}
    410 		if (lfs_ss_getident(fs, ssp) != lfs_sb_getident(fs)) {
    411 			DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
    412 			      PRIx64 "\n", lfs_ss_getident(fs, ssp),
    413 			      lfs_sb_getident(fs), offset));
    414 			offset = -1;
    415 			goto err1;
    416 		}
    417 	}
    418 	if (pseg_flags)
    419 		*pseg_flags = lfs_ss_getflags(fs, ssp);
    420 	oldoffset = offset;
    421 	offset += lfs_btofsb(fs, lfs_sb_getsumsize(fs));
    422 
    423 	ninos = howmany(lfs_ss_getninos(fs, ssp), LFS_INOPB(fs));
    424 	/* XXX ondisk32 */
    425 	iaddr = (int32_t *)((char*)bp->b_data + lfs_sb_getsumsize(fs) - sizeof(int32_t));
    426 	if (flags & CHECK_CKSUM) {
    427 		/* Count blocks */
    428 		nblocks = 0;
    429 		fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
    430 		for (i = 0; i < lfs_ss_getnfinfo(fs, ssp); ++i) {
    431 			nblocks += lfs_fi_getnblocks(fs, fip);
    432 			if (lfs_fi_getnblocks(fs, fip) <= 0)
    433 				break;
    434 			fip = NEXT_FINFO(fs, fip);
    435 		}
    436 		nblocks += ninos;
    437 		/* Create the sum array */
    438 		datap = dp = malloc(nblocks * sizeof(u_long),
    439 				    M_SEGMENT, M_WAITOK);
    440 	}
    441 
    442 	/* Handle individual blocks */
    443 	fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
    444 	for (i = 0; i < lfs_ss_getnfinfo(fs, ssp) || ninos; ++i) {
    445 		/* Inode block? */
    446 		if (ninos && *iaddr == offset) {
    447 			if (flags & CHECK_CKSUM) {
    448 				/* Read in the head and add to the buffer */
    449 				error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getbsize(fs),
    450 					      0, &dbp);
    451 				if (error) {
    452 					offset = -1;
    453 					goto err2;
    454 				}
    455 				/* XXX this can't be right, on-disk u_long? */
    456 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    457 				brelse(dbp, BC_AGE);
    458 			}
    459 			if (flags & CHECK_UPDATE) {
    460 				if ((error = update_inoblk(fs, offset, cred, l))
    461 				    != 0) {
    462 					offset = -1;
    463 					goto err2;
    464 				}
    465 			}
    466 			offset += lfs_btofsb(fs, lfs_sb_getibsize(fs));
    467 			--iaddr;
    468 			--ninos;
    469 			--i; /* compensate */
    470 			continue;
    471 		}
    472 		size = lfs_sb_getbsize(fs);
    473 		for (j = 0; j < lfs_fi_getnblocks(fs, fip); ++j) {
    474 			if (j == lfs_fi_getnblocks(fs, fip) - 1)
    475 				size = lfs_fi_getlastlength(fs, fip);
    476 			if (flags & CHECK_CKSUM) {
    477 				error = bread(devvp, LFS_FSBTODB(fs, offset), size,
    478 				    0, &dbp);
    479 				if (error) {
    480 					offset = -1;
    481 					goto err2;
    482 				}
    483 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    484 				brelse(dbp, BC_AGE);
    485 			}
    486 			/* Account for and update any direct blocks */
    487 			if ((flags & CHECK_UPDATE) &&
    488 			   lfs_fi_getino(fs, fip) > LFS_IFILE_INUM &&
    489 			   lfs_fi_getblock(fs, fip, j) >= 0) {
    490 				update_meta(fs, lfs_fi_getino(fs, fip),
    491 					    lfs_fi_getversion(fs, fip),
    492 					    lfs_fi_getblock(fs, fip, j),
    493 					    offset, size, l);
    494 			}
    495 			offset += lfs_btofsb(fs, size);
    496 		}
    497 		fip = NEXT_FINFO(fs, fip);
    498 	}
    499 	/* Checksum the array, compare */
    500 	datasum = lfs_ss_getdatasum(fs, ssp);
    501 	foundsum = cksum(datap, nblocks * sizeof(u_long));
    502 	if ((flags & CHECK_CKSUM) && datasum != foundsum) {
    503 		DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
    504 		      " (wanted %x got %x)\n",
    505 		      offset, datasum, foundsum));
    506 		offset = -1;
    507 		goto err2;
    508 	}
    509 
    510 	/* If we're at the end of the segment, move to the next */
    511 	if (lfs_dtosn(fs, offset + lfs_btofsb(fs, lfs_sb_getsumsize(fs) + lfs_sb_getbsize(fs))) !=
    512 	   lfs_dtosn(fs, offset)) {
    513 		if (lfs_dtosn(fs, offset) == lfs_dtosn(fs, lfs_ss_getnext(fs, ssp))) {
    514 			offset = -1;
    515 			goto err2;
    516 		}
    517 		offset = lfs_ss_getnext(fs, ssp);
    518 		DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
    519 		       " -> segment %d\n", offset, lfs_dtosn(fs,offset)));
    520 	}
    521 
    522 	if (flags & CHECK_UPDATE) {
    523 		lfs_sb_subavail(fs, offset - oldoffset);
    524 		/* Don't clog the buffer queue */
    525 		mutex_enter(&lfs_lock);
    526 		if (locked_queue_count > LFS_MAX_BUFS ||
    527 		    locked_queue_bytes > LFS_MAX_BYTES) {
    528 			lfs_flush(fs, SEGM_CKP, 0);
    529 		}
    530 		mutex_exit(&lfs_lock);
    531 	}
    532 
    533     err2:
    534 	if (flags & CHECK_CKSUM)
    535 		free(datap, M_SEGMENT);
    536     err1:
    537 	brelse(bp, BC_AGE);
    538 
    539 	/* XXX should we update the serial number even for bad psegs? */
    540 	if ((flags & CHECK_UPDATE) && offset > 0 && lfs_sb_getversion(fs) > 1)
    541 		lfs_sb_setserial(fs, nextserial);
    542 	return offset;
    543 }
    544 
    545 void
    546 lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
    547 {
    548 	int flags, dirty;
    549 	daddr_t offset, oldoffset, lastgoodpseg;
    550 	int sn, curseg, do_rollforward;
    551 	struct proc *p;
    552 	kauth_cred_t cred;
    553 	SEGUSE *sup;
    554 	struct buf *bp;
    555 
    556 	p = l ? l->l_proc : NULL;
    557 	cred = p ? p->p_cred : NOCRED;
    558 
    559 	/*
    560 	 * Roll forward.
    561 	 *
    562 	 * We don't roll forward for v1 filesystems, because
    563 	 * of the danger that the clock was turned back between the last
    564 	 * checkpoint and crash.  This would roll forward garbage.
    565 	 *
    566 	 * v2 filesystems don't have this problem because they use a
    567 	 * monotonically increasing serial number instead of a timestamp.
    568 	 */
    569 	do_rollforward = (!(lfs_sb_getpflags(fs) & LFS_PF_CLEAN) &&
    570 			  lfs_do_rfw && lfs_sb_getversion(fs) > 1 && p != NULL);
    571 	if (do_rollforward) {
    572 		u_int64_t nextserial;
    573 		/*
    574 		 * Phase I: Find the address of the last good partial
    575 		 * segment that was written after the checkpoint.  Mark
    576 		 * the segments in question dirty, so they won't be
    577 		 * reallocated.
    578 		 */
    579 		lastgoodpseg = oldoffset = offset = lfs_sb_getoffset(fs);
    580 		flags = 0x0;
    581 		DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
    582 		      PRIx64 "\n", offset));
    583 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    584 		if (!(sup->su_flags & SEGUSE_DIRTY))
    585 			lfs_sb_subnclean(fs, 1);
    586 		sup->su_flags |= SEGUSE_DIRTY;
    587 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    588 		nextserial = lfs_sb_getserial(fs) + 1;
    589 		while ((offset = check_segsum(fs, offset, nextserial,
    590 		    cred, CHECK_CKSUM, &flags, l)) > 0) {
    591 			nextserial++;
    592 			if (lfs_sntod(fs, oldoffset) != lfs_sntod(fs, offset)) {
    593 				LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
    594 					     bp);
    595 				if (!(sup->su_flags & SEGUSE_DIRTY))
    596 					lfs_sb_subnclean(fs, 1);
    597 				sup->su_flags |= SEGUSE_DIRTY;
    598 				LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
    599 					     bp);
    600 			}
    601 
    602 			DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
    603 			      PRIx64 "\n", offset));
    604 			if (flags & SS_DIROP) {
    605 				DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
    606 				      PRIx64 "\n", oldoffset));
    607 				if (!(flags & SS_CONT)) {
    608 				     DLOG((DLOG_RF, "lfs_mountfs: dirops end "
    609 					   "at 0x%" PRIx64 "\n", oldoffset));
    610 				}
    611 			}
    612 			if (!(flags & SS_CONT))
    613 				lastgoodpseg = offset;
    614 			oldoffset = offset;
    615 		}
    616 		if (flags & SS_CONT) {
    617 			DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
    618 			      "dirops discarded\n"));
    619 		}
    620 		DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
    621 		      "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
    622 		oldoffset = lfs_sb_getoffset(fs);
    623 		if (lfs_sb_getoffset(fs) != lastgoodpseg) {
    624 			/* Don't overwrite what we're trying to preserve */
    625 			offset = lfs_sb_getoffset(fs);
    626 			lfs_sb_setoffset(fs, lastgoodpseg);
    627 			lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_dtosn(fs, lfs_sb_getoffset(fs))));
    628 			for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs));;) {
    629 				sn = (sn + 1) % lfs_sb_getnseg(fs);
    630 				if (sn == curseg)
    631 					panic("lfs_mountfs: no clean segments");
    632 				LFS_SEGENTRY(sup, fs, sn, bp);
    633 				dirty = (sup->su_flags & SEGUSE_DIRTY);
    634 				brelse(bp, 0);
    635 				if (!dirty)
    636 					break;
    637 			}
    638 			lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
    639 
    640 			/*
    641 			 * Phase II: Roll forward from the first superblock.
    642 			 */
    643 			while (offset != lastgoodpseg) {
    644 				DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
    645 				      PRIx64 "\n", offset));
    646 				offset = check_segsum(fs, offset,
    647 				    lfs_sb_getserial(fs) + 1, cred, CHECK_UPDATE,
    648 				    NULL, l);
    649 			}
    650 
    651 			/*
    652 			 * Finish: flush our changes to disk.
    653 			 */
    654 			lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
    655 			DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
    656 			      "recovered %jd blocks\n",
    657 			      (intmax_t)(lastgoodpseg - oldoffset)));
    658 		}
    659 		DLOG((DLOG_RF, "LFS roll forward complete\n"));
    660 	}
    661 }
    662