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