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