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