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