Home | History | Annotate | Line # | Download | only in lfs
lfs_rfw.c revision 1.21
      1 /*	$NetBSD: lfs_rfw.c,v 1.21 2015/07/16 08:31:45 dholland Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: lfs_rfw.c,v 1.21 2015/07/16 08:31:45 dholland Exp $");
     34 
     35 #if defined(_KERNEL_OPT)
     36 #include "opt_quota.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/namei.h>
     42 #include <sys/proc.h>
     43 #include <sys/kernel.h>
     44 #include <sys/vnode.h>
     45 #include <sys/mount.h>
     46 #include <sys/kthread.h>
     47 #include <sys/buf.h>
     48 #include <sys/device.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/file.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/errno.h>
     54 #include <sys/malloc.h>
     55 #include <sys/pool.h>
     56 #include <sys/socket.h>
     57 #include <sys/syslog.h>
     58 #include <uvm/uvm_extern.h>
     59 #include <sys/sysctl.h>
     60 #include <sys/conf.h>
     61 #include <sys/kauth.h>
     62 
     63 #include <miscfs/specfs/specdev.h>
     64 
     65 #include <ufs/lfs/ulfs_quotacommon.h>
     66 #include <ufs/lfs/ulfs_inode.h>
     67 #include <ufs/lfs/ulfsmount.h>
     68 #include <ufs/lfs/ulfs_extern.h>
     69 
     70 #include <uvm/uvm.h>
     71 #include <uvm/uvm_stat.h>
     72 #include <uvm/uvm_pager.h>
     73 #include <uvm/uvm_pdaemon.h>
     74 
     75 #include <ufs/lfs/lfs.h>
     76 #include <ufs/lfs/lfs_kernel.h>
     77 #include <ufs/lfs/lfs_extern.h>
     78 
     79 #include <miscfs/genfs/genfs.h>
     80 #include <miscfs/genfs/genfs_node.h>
     81 
     82 /*
     83  * Roll-forward code.
     84  */
     85 static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
     86     kauth_cred_t, int, int *, struct lwp *);
     87 
     88 extern int lfs_do_rfw;
     89 
     90 /*
     91  * Allocate a particular inode with a particular version number, freeing
     92  * any previous versions of this inode that may have gone before.
     93  * Used by the roll-forward code.
     94  *
     95  * XXX this function does not have appropriate locking to be used on a live fs;
     96  * XXX but something similar could probably be used for an "undelete" call.
     97  *
     98  * Called with the Ifile inode locked.
     99  */
    100 int
    101 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
    102 	      struct vnode **vpp)
    103 {
    104 	struct vattr va;
    105 	struct vnode *vp;
    106 	struct inode *ip;
    107 	int error;
    108 
    109 	ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
    110 
    111 	/*
    112 	 * First, just try a vget. If the version number is the one we want,
    113 	 * we don't have to do anything else.  If the version number is wrong,
    114 	 * take appropriate action.
    115 	 */
    116 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    117 	if (error == 0) {
    118 		DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
    119 
    120 		*vpp = vp;
    121 		ip = VTOI(vp);
    122 		if (ip->i_gen == vers)
    123 			return 0;
    124 		else if (ip->i_gen < vers) {
    125 			lfs_truncate(vp, (off_t)0, 0, NOCRED);
    126 			ip->i_gen = ip->i_ffs1_gen = vers;
    127 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    128 			return 0;
    129 		} else {
    130 			DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
    131 			       ino, vers, ip->i_ffs1_gen));
    132 			vput(vp);
    133 			*vpp = NULLVP;
    134 			return EEXIST;
    135 		}
    136 	}
    137 
    138 	/* Not found, create as regular file. */
    139 	vattr_null(&va);
    140 	va.va_type = VREG;
    141 	va.va_mode = 0;
    142 	va.va_fileid = ino;
    143 	va.va_gen = vers;
    144 	error = vcache_new(fs->lfs_ivnode->v_mount, NULL, &va, NOCRED, &vp);
    145 	if (error)
    146 		return error;
    147 	error = vn_lock(vp, LK_EXCLUSIVE);
    148 	if (error) {
    149 		vrele(vp);
    150 		*vpp = NULLVP;
    151 		return error;
    152 	}
    153 	ip = VTOI(vp);
    154 	ip->i_nlink = ip->i_ffs1_nlink = 1;
    155 	*vpp = vp;
    156 	return 0;
    157 }
    158 
    159 /*
    160  * Load the appropriate indirect block, and change the appropriate pointer.
    161  * Mark the block dirty.  Do segment and avail accounting.
    162  */
    163 static int
    164 update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
    165 	    daddr_t ndaddr, size_t size, struct lwp *l)
    166 {
    167 	int error;
    168 	struct vnode *vp;
    169 	struct inode *ip;
    170 #ifdef DEBUG
    171 	daddr_t odaddr;
    172 	struct indir a[ULFS_NIADDR];
    173 	int num;
    174 	int i;
    175 #endif /* DEBUG */
    176 	struct buf *bp;
    177 	SEGUSE *sup;
    178 
    179 	KASSERT(lbn >= 0);	/* no indirect blocks */
    180 
    181 	if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
    182 		DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
    183 		      " returned %d\n", ino, error));
    184 		return error;
    185 	}
    186 
    187 	if ((error = lfs_balloc(vp, (lbn << fs->lfs_bshift), size,
    188 				NOCRED, 0, &bp)) != 0) {
    189 		vput(vp);
    190 		return (error);
    191 	}
    192 	/* No need to write, the block is already on disk */
    193 	if (bp->b_oflags & BO_DELWRI) {
    194 		LFS_UNLOCK_BUF(bp);
    195 		fs->lfs_avail += lfs_btofsb(fs, bp->b_bcount);
    196 	}
    197 	brelse(bp, BC_INVAL);
    198 
    199 	/*
    200 	 * Extend the file, if it is not large enough already.
    201 	 * XXX this is not exactly right, we don't know how much of the
    202 	 * XXX last block is actually used.  We hope that an inode will
    203 	 * XXX appear later to give the correct size.
    204 	 */
    205 	ip = VTOI(vp);
    206 	if (ip->i_size <= (lbn << fs->lfs_bshift)) {
    207 		u_int64_t newsize;
    208 
    209 		if (lbn < ULFS_NDADDR)
    210 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) +
    211 				(size - fs->lfs_fsize) + 1;
    212 		else
    213 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) + 1;
    214 
    215 		if (ip->i_size < newsize) {
    216 			ip->i_size = newsize;
    217 			/*
    218 			 * tell vm our new size for the case the inode won't
    219 			 * appear later.
    220 			 */
    221 			uvm_vnp_setsize(vp, newsize);
    222 		}
    223 	}
    224 
    225 	lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
    226 
    227 	LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
    228 	sup->su_nbytes += size;
    229 	LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
    230 
    231 	/* differences here should be due to UNWRITTEN indirect blocks. */
    232 	KASSERT((lfs_lblkno(fs, ip->i_size) > ULFS_NDADDR &&
    233 	    ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
    234 	    ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
    235 
    236 #ifdef DEBUG
    237 	/* Now look again to make sure it worked */
    238 	ulfs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
    239 	for (i = num; i > 0; i--) {
    240 		if (!a[i].in_exists)
    241 			panic("update_meta: absent %d lv indirect block", i);
    242 	}
    243 	if (LFS_DBTOFSB(fs, odaddr) != ndaddr)
    244 		DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
    245 		      PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
    246 #endif /* DEBUG */
    247 	vput(vp);
    248 	return 0;
    249 }
    250 
    251 static int
    252 update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
    253 	      struct lwp *l)
    254 {
    255 	struct vnode *devvp, *vp;
    256 	struct inode *ip;
    257 	struct ulfs1_dinode *dip;
    258 	struct buf *dbp, *ibp;
    259 	int error;
    260 	daddr_t daddr;
    261 	IFILE *ifp;
    262 	SEGUSE *sup;
    263 
    264 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    265 
    266 	/*
    267 	 * Get the inode, update times and perms.
    268 	 * DO NOT update disk blocks, we do that separately.
    269 	 */
    270 	error = bread(devvp, LFS_FSBTODB(fs, offset), fs->lfs_ibsize,
    271 	    0, &dbp);
    272 	if (error) {
    273 		DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
    274 		return error;
    275 	}
    276 	dip = ((struct ulfs1_dinode *)(dbp->b_data)) + LFS_INOPB(fs);
    277 	while (--dip >= (struct ulfs1_dinode *)dbp->b_data) {
    278 		if (dip->di_inumber > LFS_IFILE_INUM) {
    279 			error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
    280 					      l, &vp);
    281 			if (error) {
    282 				DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
    283 				      " returned %d\n", error));
    284 				continue;
    285 			}
    286 			ip = VTOI(vp);
    287 			if (dip->di_size != ip->i_size)
    288 				lfs_truncate(vp, dip->di_size, 0, NOCRED);
    289 			/* Get mode, link count, size, and times */
    290 			memcpy(ip->i_din.ffs1_din, dip,
    291 			       offsetof(struct ulfs1_dinode, di_db[0]));
    292 
    293 			/* Then the rest, except di_blocks */
    294 			ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
    295 			ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
    296 			ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
    297 			ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
    298 
    299 			ip->i_mode = ip->i_ffs1_mode;
    300 			ip->i_nlink = ip->i_ffs1_nlink;
    301 			ip->i_size = ip->i_ffs1_size;
    302 
    303 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    304 
    305 			/* Re-initialize to get type right */
    306 			ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
    307 				  &vp);
    308 			vput(vp);
    309 
    310 			/* Record change in location */
    311 			LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
    312 			daddr = ifp->if_daddr;
    313 			ifp->if_daddr = LFS_DBTOFSB(fs, dbp->b_blkno);
    314 			error = LFS_BWRITE_LOG(ibp); /* Ifile */
    315 			/* And do segment accounting */
    316 			if (lfs_dtosn(fs, daddr) != lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno))) {
    317 				if (daddr > 0) {
    318 					LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, daddr),
    319 						     ibp);
    320 					sup->su_nbytes -= sizeof (struct ulfs1_dinode);
    321 					LFS_WRITESEGENTRY(sup, fs,
    322 							  lfs_dtosn(fs, daddr),
    323 							  ibp);
    324 				}
    325 				LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
    326 					     ibp);
    327 				sup->su_nbytes += sizeof (struct ulfs1_dinode);
    328 				LFS_WRITESEGENTRY(sup, fs,
    329 						  lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
    330 						  ibp);
    331 			}
    332 		}
    333 	}
    334 	brelse(dbp, BC_AGE);
    335 
    336 	return 0;
    337 }
    338 
    339 #define CHECK_CKSUM   0x0001  /* Check the checksum to make sure it's valid */
    340 #define CHECK_UPDATE  0x0002  /* Update Ifile for new data blocks / inodes */
    341 
    342 static daddr_t
    343 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
    344 	     kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
    345 {
    346 	struct vnode *devvp;
    347 	struct buf *bp, *dbp;
    348 	int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
    349 	SEGSUM *ssp;
    350 	u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
    351 	daddr_t oldoffset;
    352 	int32_t *iaddr;	/* XXX ondisk32 */
    353 	FINFO *fip;
    354 	SEGUSE *sup;
    355 	size_t size;
    356 
    357 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    358 	/*
    359 	 * If the segment has a superblock and we're at the top
    360 	 * of the segment, skip the superblock.
    361 	 */
    362 	if (lfs_sntod(fs, lfs_dtosn(fs, offset)) == offset) {
    363 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    364 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
    365 			offset += lfs_btofsb(fs, LFS_SBPAD);
    366 		brelse(bp, 0);
    367 	}
    368 
    369 	/* Read in the segment summary */
    370 	error = bread(devvp, LFS_FSBTODB(fs, offset), fs->lfs_sumsize,
    371 	    0, &bp);
    372 	if (error)
    373 		return -1;
    374 
    375 	/* Check summary checksum */
    376 	ssp = (SEGSUM *)bp->b_data;
    377 	if (flags & CHECK_CKSUM) {
    378 		if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
    379 					   fs->lfs_sumsize -
    380 					   sizeof(ssp->ss_sumsum))) {
    381 			DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
    382 			offset = -1;
    383 			goto err1;
    384 		}
    385 		if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
    386 			DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
    387 			offset = -1;
    388 			goto err1;
    389 		}
    390 		if (ssp->ss_create < fs->lfs_tstamp) {
    391 			DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
    392 			offset = -1;
    393 			goto err1;
    394 		}
    395 	}
    396 	if (fs->lfs_version > 1) {
    397 		if (ssp->ss_serial != nextserial) {
    398 			DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
    399 			      "\n", offset));
    400 			offset = -1;
    401 			goto err1;
    402 		}
    403 		if (ssp->ss_ident != fs->lfs_ident) {
    404 			DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
    405 			      PRIx64 "\n", ssp->ss_ident, fs->lfs_ident, offset));
    406 			offset = -1;
    407 			goto err1;
    408 		}
    409 	}
    410 	if (pseg_flags)
    411 		*pseg_flags = ssp->ss_flags;
    412 	oldoffset = offset;
    413 	offset += lfs_btofsb(fs, fs->lfs_sumsize);
    414 
    415 	ninos = howmany(ssp->ss_ninos, LFS_INOPB(fs));
    416 	/* XXX ondisk32 */
    417 	iaddr = (int32_t *)((char*)bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
    418 	if (flags & CHECK_CKSUM) {
    419 		/* Count blocks */
    420 		nblocks = 0;
    421 		fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
    422 		for (i = 0; i < ssp->ss_nfinfo; ++i) {
    423 			nblocks += fip->fi_nblocks;
    424 			if (fip->fi_nblocks <= 0)
    425 				break;
    426 			/* XXX ondisk32 */
    427 			fip = (FINFO *)(((char *)fip) + FINFOSIZE +
    428 					(fip->fi_nblocks * sizeof(int32_t)));
    429 		}
    430 		nblocks += ninos;
    431 		/* Create the sum array */
    432 		datap = dp = malloc(nblocks * sizeof(u_long),
    433 				    M_SEGMENT, M_WAITOK);
    434 	}
    435 
    436 	/* Handle individual blocks */
    437 	fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
    438 	for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
    439 		/* Inode block? */
    440 		if (ninos && *iaddr == offset) {
    441 			if (flags & CHECK_CKSUM) {
    442 				/* Read in the head and add to the buffer */
    443 				error = bread(devvp, LFS_FSBTODB(fs, offset), fs->lfs_bsize,
    444 					      0, &dbp);
    445 				if (error) {
    446 					offset = -1;
    447 					goto err2;
    448 				}
    449 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    450 				brelse(dbp, BC_AGE);
    451 			}
    452 			if (flags & CHECK_UPDATE) {
    453 				if ((error = update_inoblk(fs, offset, cred, l))
    454 				    != 0) {
    455 					offset = -1;
    456 					goto err2;
    457 				}
    458 			}
    459 			offset += lfs_btofsb(fs, fs->lfs_ibsize);
    460 			--iaddr;
    461 			--ninos;
    462 			--i; /* compensate */
    463 			continue;
    464 		}
    465 		size = fs->lfs_bsize;
    466 		for (j = 0; j < fip->fi_nblocks; ++j) {
    467 			if (j == fip->fi_nblocks - 1)
    468 				size = fip->fi_lastlength;
    469 			if (flags & CHECK_CKSUM) {
    470 				error = bread(devvp, LFS_FSBTODB(fs, offset), size,
    471 				    0, &dbp);
    472 				if (error) {
    473 					offset = -1;
    474 					goto err2;
    475 				}
    476 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    477 				brelse(dbp, BC_AGE);
    478 			}
    479 			/* Account for and update any direct blocks */
    480 			if ((flags & CHECK_UPDATE) &&
    481 			   fip->fi_ino > LFS_IFILE_INUM &&
    482 			   fip->fi_blocks[j] >= 0) {
    483 				update_meta(fs, fip->fi_ino, fip->fi_version,
    484 					    fip->fi_blocks[j], offset, size, l);
    485 			}
    486 			offset += lfs_btofsb(fs, size);
    487 		}
    488 		/* XXX ondisk32 */
    489 		fip = (FINFO *)(((char *)fip) + FINFOSIZE
    490 				+ fip->fi_nblocks * sizeof(int32_t));
    491 	}
    492 	/* Checksum the array, compare */
    493 	if ((flags & CHECK_CKSUM) &&
    494 	   ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
    495 	{
    496 		DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
    497 		      " (wanted %x got %x)\n",
    498 		      offset, ssp->ss_datasum, cksum(datap, nblocks *
    499 						     sizeof(u_long))));
    500 		offset = -1;
    501 		goto err2;
    502 	}
    503 
    504 	/* If we're at the end of the segment, move to the next */
    505 	if (lfs_dtosn(fs, offset + lfs_btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
    506 	   lfs_dtosn(fs, offset)) {
    507 		if (lfs_dtosn(fs, offset) == lfs_dtosn(fs, ssp->ss_next)) {
    508 			offset = -1;
    509 			goto err2;
    510 		}
    511 		offset = ssp->ss_next;
    512 		DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
    513 		       " -> segment %d\n", offset, lfs_dtosn(fs,offset)));
    514 	}
    515 
    516 	if (flags & CHECK_UPDATE) {
    517 		fs->lfs_avail -= (offset - oldoffset);
    518 		/* Don't clog the buffer queue */
    519 		mutex_enter(&lfs_lock);
    520 		if (locked_queue_count > LFS_MAX_BUFS ||
    521 		    locked_queue_bytes > LFS_MAX_BYTES) {
    522 			lfs_flush(fs, SEGM_CKP, 0);
    523 		}
    524 		mutex_exit(&lfs_lock);
    525 	}
    526 
    527     err2:
    528 	if (flags & CHECK_CKSUM)
    529 		free(datap, M_SEGMENT);
    530     err1:
    531 	brelse(bp, BC_AGE);
    532 
    533 	/* XXX should we update the serial number even for bad psegs? */
    534 	if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
    535 		fs->lfs_serial = nextserial;
    536 	return offset;
    537 }
    538 
    539 void
    540 lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
    541 {
    542 	int flags, dirty;
    543 	daddr_t offset, oldoffset, lastgoodpseg;
    544 	int sn, curseg, do_rollforward;
    545 	struct proc *p;
    546 	kauth_cred_t cred;
    547 	SEGUSE *sup;
    548 	struct buf *bp;
    549 
    550 	p = l ? l->l_proc : NULL;
    551 	cred = p ? p->p_cred : NOCRED;
    552 
    553 	/*
    554 	 * Roll forward.
    555 	 *
    556 	 * We don't roll forward for v1 filesystems, because
    557 	 * of the danger that the clock was turned back between the last
    558 	 * checkpoint and crash.  This would roll forward garbage.
    559 	 *
    560 	 * v2 filesystems don't have this problem because they use a
    561 	 * monotonically increasing serial number instead of a timestamp.
    562 	 */
    563 	do_rollforward = (!(fs->lfs_pflags & LFS_PF_CLEAN) &&
    564 			  lfs_do_rfw && fs->lfs_version > 1 && p != NULL);
    565 	if (do_rollforward) {
    566 		u_int64_t nextserial;
    567 		/*
    568 		 * Phase I: Find the address of the last good partial
    569 		 * segment that was written after the checkpoint.  Mark
    570 		 * the segments in question dirty, so they won't be
    571 		 * reallocated.
    572 		 */
    573 		lastgoodpseg = oldoffset = offset = fs->lfs_offset;
    574 		flags = 0x0;
    575 		DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
    576 		      PRIx64 "\n", offset));
    577 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    578 		if (!(sup->su_flags & SEGUSE_DIRTY))
    579 			--fs->lfs_nclean;
    580 		sup->su_flags |= SEGUSE_DIRTY;
    581 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    582 		nextserial = fs->lfs_serial + 1;
    583 		while ((offset = check_segsum(fs, offset, nextserial,
    584 		    cred, CHECK_CKSUM, &flags, l)) > 0) {
    585 			nextserial++;
    586 			if (lfs_sntod(fs, oldoffset) != lfs_sntod(fs, offset)) {
    587 				LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
    588 					     bp);
    589 				if (!(sup->su_flags & SEGUSE_DIRTY))
    590 					--fs->lfs_nclean;
    591 				sup->su_flags |= SEGUSE_DIRTY;
    592 				LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
    593 					     bp);
    594 			}
    595 
    596 			DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
    597 			      PRIx64 "\n", offset));
    598 			if (flags & SS_DIROP) {
    599 				DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
    600 				      PRIx64 "\n", oldoffset));
    601 				if (!(flags & SS_CONT)) {
    602 				     DLOG((DLOG_RF, "lfs_mountfs: dirops end "
    603 					   "at 0x%" PRIx64 "\n", oldoffset));
    604 				}
    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 = lfs_sntod(fs, lfs_dtosn(fs, fs->lfs_offset));
    622 			for (sn = curseg = lfs_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, 0);
    629 				if (!dirty)
    630 					break;
    631 			}
    632 			fs->lfs_nextseg = lfs_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