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