Home | History | Annotate | Line # | Download | only in lfs
lfs_rfw.c revision 1.9
      1 /*	$NetBSD: lfs_rfw.c,v 1.9 2008/01/02 11:49:11 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.9 2008/01/02 11:49:11 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);
    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 		mutex_enter(&lfs_lock);
    203 		--lfs_dirvcount;
    204 		--fs->lfs_dirvcount;
    205 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    206 		wakeup(&lfs_dirvcount);
    207 		wakeup(&fs->lfs_dirvcount);
    208 		mutex_exit(&lfs_lock);
    209 	}
    210 	*vpp = vp;
    211 	return error;
    212 }
    213 
    214 /*
    215  * Load the appropriate indirect block, and change the appropriate pointer.
    216  * Mark the block dirty.  Do segment and avail accounting.
    217  */
    218 static int
    219 update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
    220 	    daddr_t ndaddr, size_t size, struct lwp *l)
    221 {
    222 	int error;
    223 	struct vnode *vp;
    224 	struct inode *ip;
    225 #ifdef DEBUG
    226 	daddr_t odaddr;
    227 	struct indir a[NIADDR];
    228 	int num;
    229 	int i;
    230 #endif /* DEBUG */
    231 	struct buf *bp;
    232 	SEGUSE *sup;
    233 
    234 	KASSERT(lbn >= 0);	/* no indirect blocks */
    235 
    236 	if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
    237 		DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
    238 		      " returned %d\n", ino, error));
    239 		return error;
    240 	}
    241 
    242 	if ((error = lfs_balloc(vp, (lbn << fs->lfs_bshift), size,
    243 				NOCRED, 0, &bp)) != 0) {
    244 		vput(vp);
    245 		return (error);
    246 	}
    247 	/* No need to write, the block is already on disk */
    248 	if (bp->b_oflags & BO_DELWRI) {
    249 		LFS_UNLOCK_BUF(bp);
    250 		fs->lfs_avail += btofsb(fs, bp->b_bcount);
    251 	}
    252 	brelse(bp, BC_INVAL);
    253 
    254 	/*
    255 	 * Extend the file, if it is not large enough already.
    256 	 * XXX this is not exactly right, we don't know how much of the
    257 	 * XXX last block is actually used.  We hope that an inode will
    258 	 * XXX appear later to give the correct size.
    259 	 */
    260 	ip = VTOI(vp);
    261 	if (ip->i_size <= (lbn << fs->lfs_bshift)) {
    262 		u_int64_t newsize;
    263 
    264 		if (lbn < NDADDR)
    265 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) +
    266 				(size - fs->lfs_fsize) + 1;
    267 		else
    268 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) + 1;
    269 
    270 		if (ip->i_size < newsize) {
    271 			ip->i_size = newsize;
    272 			/*
    273 			 * tell vm our new size for the case the inode won't
    274 			 * appear later.
    275 			 */
    276 			uvm_vnp_setsize(vp, newsize);
    277 		}
    278 	}
    279 
    280 	lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
    281 
    282 	LFS_SEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
    283 	sup->su_nbytes += size;
    284 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
    285 
    286 	/* differences here should be due to UNWRITTEN indirect blocks. */
    287 	KASSERT((lblkno(fs, ip->i_size) > NDADDR &&
    288 	    ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
    289 	    ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
    290 
    291 #ifdef DEBUG
    292 	/* Now look again to make sure it worked */
    293 	ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
    294 	for (i = num; i > 0; i--) {
    295 		if (!a[i].in_exists)
    296 			panic("update_meta: absent %d lv indirect block", i);
    297 	}
    298 	if (dbtofsb(fs, odaddr) != ndaddr)
    299 		DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
    300 		      PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
    301 #endif /* DEBUG */
    302 	vput(vp);
    303 	return 0;
    304 }
    305 
    306 static int
    307 update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
    308 	      struct lwp *l)
    309 {
    310 	struct vnode *devvp, *vp;
    311 	struct inode *ip;
    312 	struct ufs1_dinode *dip;
    313 	struct buf *dbp, *ibp;
    314 	int error;
    315 	daddr_t daddr;
    316 	IFILE *ifp;
    317 	SEGUSE *sup;
    318 
    319 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    320 
    321 	/*
    322 	 * Get the inode, update times and perms.
    323 	 * DO NOT update disk blocks, we do that separately.
    324 	 */
    325 	error = bread(devvp, fsbtodb(fs, offset), fs->lfs_ibsize, cred, &dbp);
    326 	if (error) {
    327 		DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
    328 		return error;
    329 	}
    330 	dip = ((struct ufs1_dinode *)(dbp->b_data)) + INOPB(fs);
    331 	while (--dip >= (struct ufs1_dinode *)dbp->b_data) {
    332 		if (dip->di_inumber > LFS_IFILE_INUM) {
    333 			error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
    334 					      l, &vp);
    335 			if (error) {
    336 				DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
    337 				      " returned %d\n", error));
    338 				continue;
    339 			}
    340 			ip = VTOI(vp);
    341 			if (dip->di_size != ip->i_size)
    342 				lfs_truncate(vp, dip->di_size, 0, NOCRED);
    343 			/* Get mode, link count, size, and times */
    344 			memcpy(ip->i_din.ffs1_din, dip,
    345 			       offsetof(struct ufs1_dinode, di_db[0]));
    346 
    347 			/* Then the rest, except di_blocks */
    348 			ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
    349 			ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
    350 			ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
    351 			ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
    352 
    353 			ip->i_mode = ip->i_ffs1_mode;
    354 			ip->i_nlink = ip->i_ffs_effnlink = ip->i_ffs1_nlink;
    355 			ip->i_size = ip->i_ffs1_size;
    356 
    357 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    358 
    359 			/* Re-initialize to get type right */
    360 			ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
    361 				  &vp);
    362 			vput(vp);
    363 
    364 			/* Record change in location */
    365 			LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
    366 			daddr = ifp->if_daddr;
    367 			ifp->if_daddr = dbtofsb(fs, dbp->b_blkno);
    368 			error = LFS_BWRITE_LOG(ibp); /* Ifile */
    369 			/* And do segment accounting */
    370 			if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) {
    371 				if (daddr > 0) {
    372 					LFS_SEGENTRY(sup, fs, dtosn(fs, daddr),
    373 						     ibp);
    374 					sup->su_nbytes -= sizeof (struct ufs1_dinode);
    375 					LFS_WRITESEGENTRY(sup, fs,
    376 							  dtosn(fs, daddr),
    377 							  ibp);
    378 				}
    379 				LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
    380 					     ibp);
    381 				sup->su_nbytes += sizeof (struct ufs1_dinode);
    382 				LFS_WRITESEGENTRY(sup, fs,
    383 						  dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
    384 						  ibp);
    385 			}
    386 		}
    387 	}
    388 	brelse(dbp, BC_AGE);
    389 
    390 	return 0;
    391 }
    392 
    393 #define CHECK_CKSUM   0x0001  /* Check the checksum to make sure it's valid */
    394 #define CHECK_UPDATE  0x0002  /* Update Ifile for new data blocks / inodes */
    395 
    396 static daddr_t
    397 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
    398 	     kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
    399 {
    400 	struct vnode *devvp;
    401 	struct buf *bp, *dbp;
    402 	int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
    403 	SEGSUM *ssp;
    404 	u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
    405 	daddr_t oldoffset;
    406 	int32_t *iaddr;	/* XXX ondisk32 */
    407 	FINFO *fip;
    408 	SEGUSE *sup;
    409 	size_t size;
    410 
    411 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    412 	/*
    413 	 * If the segment has a superblock and we're at the top
    414 	 * of the segment, skip the superblock.
    415 	 */
    416 	if (sntod(fs, dtosn(fs, offset)) == offset) {
    417 		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
    418 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
    419 			offset += btofsb(fs, LFS_SBPAD);
    420 		brelse(bp, 0);
    421 	}
    422 
    423 	/* Read in the segment summary */
    424 	error = bread(devvp, fsbtodb(fs, offset), fs->lfs_sumsize, cred, &bp);
    425 	if (error)
    426 		return -1;
    427 
    428 	/* Check summary checksum */
    429 	ssp = (SEGSUM *)bp->b_data;
    430 	if (flags & CHECK_CKSUM) {
    431 		if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
    432 					   fs->lfs_sumsize -
    433 					   sizeof(ssp->ss_sumsum))) {
    434 			DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
    435 			offset = -1;
    436 			goto err1;
    437 		}
    438 		if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
    439 			DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
    440 			offset = -1;
    441 			goto err1;
    442 		}
    443 		if (ssp->ss_create < fs->lfs_tstamp) {
    444 			DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
    445 			offset = -1;
    446 			goto err1;
    447 		}
    448 	}
    449 	if (fs->lfs_version > 1) {
    450 		if (ssp->ss_serial != nextserial) {
    451 			DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
    452 			      "\n", offset));
    453 			offset = -1;
    454 			goto err1;
    455 		}
    456 		if (ssp->ss_ident != fs->lfs_ident) {
    457 			DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
    458 			      PRIx64 "\n", ssp->ss_ident, fs->lfs_ident, offset));
    459 			offset = -1;
    460 			goto err1;
    461 		}
    462 	}
    463 	if (pseg_flags)
    464 		*pseg_flags = ssp->ss_flags;
    465 	oldoffset = offset;
    466 	offset += btofsb(fs, fs->lfs_sumsize);
    467 
    468 	ninos = howmany(ssp->ss_ninos, INOPB(fs));
    469 	/* XXX ondisk32 */
    470 	iaddr = (int32_t *)((char*)bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
    471 	if (flags & CHECK_CKSUM) {
    472 		/* Count blocks */
    473 		nblocks = 0;
    474 		fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
    475 		for (i = 0; i < ssp->ss_nfinfo; ++i) {
    476 			nblocks += fip->fi_nblocks;
    477 			if (fip->fi_nblocks <= 0)
    478 				break;
    479 			/* XXX ondisk32 */
    480 			fip = (FINFO *)(((char *)fip) + FINFOSIZE +
    481 					(fip->fi_nblocks * sizeof(int32_t)));
    482 		}
    483 		nblocks += ninos;
    484 		/* Create the sum array */
    485 		datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
    486 					      M_SEGMENT, M_WAITOK);
    487 	}
    488 
    489 	/* Handle individual blocks */
    490 	fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
    491 	for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
    492 		/* Inode block? */
    493 		if (ninos && *iaddr == offset) {
    494 			if (flags & CHECK_CKSUM) {
    495 				/* Read in the head and add to the buffer */
    496 				error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize,
    497 					      cred, &dbp);
    498 				if (error) {
    499 					offset = -1;
    500 					goto err2;
    501 				}
    502 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    503 				brelse(dbp, BC_AGE);
    504 			}
    505 			if (flags & CHECK_UPDATE) {
    506 				if ((error = update_inoblk(fs, offset, cred, l))
    507 				    != 0) {
    508 					offset = -1;
    509 					goto err2;
    510 				}
    511 			}
    512 			offset += btofsb(fs, fs->lfs_ibsize);
    513 			--iaddr;
    514 			--ninos;
    515 			--i; /* compensate */
    516 			continue;
    517 		}
    518 		size = fs->lfs_bsize;
    519 		for (j = 0; j < fip->fi_nblocks; ++j) {
    520 			if (j == fip->fi_nblocks - 1)
    521 				size = fip->fi_lastlength;
    522 			if (flags & CHECK_CKSUM) {
    523 				error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp);
    524 				if (error) {
    525 					offset = -1;
    526 					goto err2;
    527 				}
    528 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    529 				brelse(dbp, BC_AGE);
    530 			}
    531 			/* Account for and update any direct blocks */
    532 			if ((flags & CHECK_UPDATE) &&
    533 			   fip->fi_ino > LFS_IFILE_INUM &&
    534 			   fip->fi_blocks[j] >= 0) {
    535 				update_meta(fs, fip->fi_ino, fip->fi_version,
    536 					    fip->fi_blocks[j], offset, size, l);
    537 			}
    538 			offset += btofsb(fs, size);
    539 		}
    540 		/* XXX ondisk32 */
    541 		fip = (FINFO *)(((char *)fip) + FINFOSIZE
    542 				+ fip->fi_nblocks * sizeof(int32_t));
    543 	}
    544 	/* Checksum the array, compare */
    545 	if ((flags & CHECK_CKSUM) &&
    546 	   ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
    547 	{
    548 		DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
    549 		      " (wanted %x got %x)\n",
    550 		      offset, ssp->ss_datasum, cksum(datap, nblocks *
    551 						     sizeof(u_long))));
    552 		offset = -1;
    553 		goto err2;
    554 	}
    555 
    556 	/* If we're at the end of the segment, move to the next */
    557 	if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
    558 	   dtosn(fs, offset)) {
    559 		if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) {
    560 			offset = -1;
    561 			goto err2;
    562 		}
    563 		offset = ssp->ss_next;
    564 		DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
    565 		       " -> segment %d\n", offset, dtosn(fs,offset)));
    566 	}
    567 
    568 	if (flags & CHECK_UPDATE) {
    569 		fs->lfs_avail -= (offset - oldoffset);
    570 		/* Don't clog the buffer queue */
    571 		mutex_enter(&lfs_lock);
    572 		if (locked_queue_count > LFS_MAX_BUFS ||
    573 		    locked_queue_bytes > LFS_MAX_BYTES) {
    574 			lfs_flush(fs, SEGM_CKP, 0);
    575 		}
    576 		mutex_exit(&lfs_lock);
    577 	}
    578 
    579     err2:
    580 	if (flags & CHECK_CKSUM)
    581 		free(datap, M_SEGMENT);
    582     err1:
    583 	brelse(bp, BC_AGE);
    584 
    585 	/* XXX should we update the serial number even for bad psegs? */
    586 	if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
    587 		fs->lfs_serial = nextserial;
    588 	return offset;
    589 }
    590 
    591 void
    592 lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
    593 {
    594 	int flags, dirty;
    595 	daddr_t offset, oldoffset, lastgoodpseg;
    596 	int sn, curseg, do_rollforward;
    597 	struct proc *p;
    598 	kauth_cred_t cred;
    599 	SEGUSE *sup;
    600 	struct buf *bp;
    601 
    602 	p = l ? l->l_proc : NULL;
    603 	cred = p ? p->p_cred : NOCRED;
    604 
    605 	/*
    606 	 * Roll forward.
    607 	 *
    608 	 * We don't roll forward for v1 filesystems, because
    609 	 * of the danger that the clock was turned back between the last
    610 	 * checkpoint and crash.  This would roll forward garbage.
    611 	 *
    612 	 * v2 filesystems don't have this problem because they use a
    613 	 * monotonically increasing serial number instead of a timestamp.
    614 	 */
    615 	do_rollforward = (!(fs->lfs_pflags & LFS_PF_CLEAN) &&
    616 			  lfs_do_rfw && fs->lfs_version > 1 && p != NULL);
    617 	if (do_rollforward) {
    618 		u_int64_t nextserial;
    619 		/*
    620 		 * Phase I: Find the address of the last good partial
    621 		 * segment that was written after the checkpoint.  Mark
    622 		 * the segments in question dirty, so they won't be
    623 		 * reallocated.
    624 		 */
    625 		lastgoodpseg = oldoffset = offset = fs->lfs_offset;
    626 		flags = 0x0;
    627 		DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
    628 		      PRIx64 "\n", offset));
    629 		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
    630 		if (!(sup->su_flags & SEGUSE_DIRTY))
    631 			--fs->lfs_nclean;
    632 		sup->su_flags |= SEGUSE_DIRTY;
    633 		LFS_WRITESEGENTRY(sup, fs, dtosn(fs, offset), bp);
    634 		nextserial = fs->lfs_serial + 1;
    635 		while ((offset = check_segsum(fs, offset, nextserial,
    636 		    cred, CHECK_CKSUM, &flags, l)) > 0) {
    637 			nextserial++;
    638 			if (sntod(fs, oldoffset) != sntod(fs, offset)) {
    639 				LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset),
    640 					     bp);
    641 				if (!(sup->su_flags & SEGUSE_DIRTY))
    642 					--fs->lfs_nclean;
    643 				sup->su_flags |= SEGUSE_DIRTY;
    644 				LFS_WRITESEGENTRY(sup, fs, dtosn(fs, oldoffset),
    645 					     bp);
    646 			}
    647 
    648 			DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
    649 			      PRIx64 "\n", offset));
    650 			if (flags & SS_DIROP) {
    651 				DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
    652 				      PRIx64 "\n", oldoffset));
    653 				if (!(flags & SS_CONT)) {
    654 				     DLOG((DLOG_RF, "lfs_mountfs: dirops end "
    655 					   "at 0x%" PRIx64 "\n", oldoffset));
    656 				}
    657 			}
    658 			if (!(flags & SS_CONT))
    659 				lastgoodpseg = offset;
    660 			oldoffset = offset;
    661 		}
    662 		if (flags & SS_CONT) {
    663 			DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
    664 			      "dirops discarded\n"));
    665 		}
    666 		DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
    667 		      "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
    668 		oldoffset = fs->lfs_offset;
    669 		if (fs->lfs_offset != lastgoodpseg) {
    670 			/* Don't overwrite what we're trying to preserve */
    671 			offset = fs->lfs_offset;
    672 			fs->lfs_offset = lastgoodpseg;
    673 			fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset));
    674 			for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) {
    675 				sn = (sn + 1) % fs->lfs_nseg;
    676 				if (sn == curseg)
    677 					panic("lfs_mountfs: no clean segments");
    678 				LFS_SEGENTRY(sup, fs, sn, bp);
    679 				dirty = (sup->su_flags & SEGUSE_DIRTY);
    680 				brelse(bp, 0);
    681 				if (!dirty)
    682 					break;
    683 			}
    684 			fs->lfs_nextseg = sntod(fs, sn);
    685 
    686 			/*
    687 			 * Phase II: Roll forward from the first superblock.
    688 			 */
    689 			while (offset != lastgoodpseg) {
    690 				DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
    691 				      PRIx64 "\n", offset));
    692 				offset = check_segsum(fs, offset,
    693 				    fs->lfs_serial + 1, cred, CHECK_UPDATE,
    694 				    NULL, l);
    695 			}
    696 
    697 			/*
    698 			 * Finish: flush our changes to disk.
    699 			 */
    700 			lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
    701 			DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
    702 			      "recovered %lld blocks\n",
    703 			      (long long)(lastgoodpseg - oldoffset)));
    704 		}
    705 		DLOG((DLOG_RF, "LFS roll forward complete\n"));
    706 	}
    707 }
    708