Home | History | Annotate | Line # | Download | only in lfs
lfs_rfw.c revision 1.33
      1  1.33      maxv /*	$NetBSD: lfs_rfw.c,v 1.33 2018/12/10 14:46:25 maxv 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  *
     19   1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  perseant  */
     31   1.1  perseant 
     32   1.2  perseant #include <sys/cdefs.h>
     33  1.33      maxv __KERNEL_RCSID(0, "$NetBSD: lfs_rfw.c,v 1.33 2018/12/10 14:46:25 maxv Exp $");
     34   1.2  perseant 
     35   1.2  perseant #if defined(_KERNEL_OPT)
     36   1.2  perseant #include "opt_quota.h"
     37   1.2  perseant #endif
     38   1.2  perseant 
     39   1.2  perseant #include <sys/param.h>
     40   1.2  perseant #include <sys/systm.h>
     41   1.2  perseant #include <sys/namei.h>
     42   1.2  perseant #include <sys/proc.h>
     43   1.2  perseant #include <sys/kernel.h>
     44   1.2  perseant #include <sys/vnode.h>
     45   1.2  perseant #include <sys/mount.h>
     46   1.2  perseant #include <sys/kthread.h>
     47   1.2  perseant #include <sys/buf.h>
     48   1.2  perseant #include <sys/device.h>
     49   1.2  perseant #include <sys/file.h>
     50   1.2  perseant #include <sys/disklabel.h>
     51   1.2  perseant #include <sys/ioctl.h>
     52   1.2  perseant #include <sys/errno.h>
     53   1.2  perseant #include <sys/malloc.h>
     54   1.2  perseant #include <sys/pool.h>
     55   1.2  perseant #include <sys/socket.h>
     56   1.2  perseant #include <sys/syslog.h>
     57   1.2  perseant #include <uvm/uvm_extern.h>
     58   1.2  perseant #include <sys/sysctl.h>
     59   1.2  perseant #include <sys/conf.h>
     60   1.2  perseant #include <sys/kauth.h>
     61   1.2  perseant 
     62   1.2  perseant #include <miscfs/specfs/specdev.h>
     63   1.2  perseant 
     64  1.14  dholland #include <ufs/lfs/ulfs_quotacommon.h>
     65  1.14  dholland #include <ufs/lfs/ulfs_inode.h>
     66  1.14  dholland #include <ufs/lfs/ulfsmount.h>
     67  1.14  dholland #include <ufs/lfs/ulfs_extern.h>
     68   1.2  perseant 
     69   1.2  perseant #include <uvm/uvm.h>
     70   1.2  perseant #include <uvm/uvm_stat.h>
     71   1.2  perseant #include <uvm/uvm_pager.h>
     72   1.2  perseant #include <uvm/uvm_pdaemon.h>
     73   1.2  perseant 
     74   1.2  perseant #include <ufs/lfs/lfs.h>
     75  1.24  dholland #include <ufs/lfs/lfs_accessors.h>
     76  1.18  dholland #include <ufs/lfs/lfs_kernel.h>
     77   1.2  perseant #include <ufs/lfs/lfs_extern.h>
     78   1.2  perseant 
     79   1.2  perseant #include <miscfs/genfs/genfs.h>
     80   1.2  perseant #include <miscfs/genfs/genfs_node.h>
     81   1.2  perseant 
     82   1.1  perseant /*
     83   1.1  perseant  * Roll-forward code.
     84   1.1  perseant  */
     85   1.1  perseant static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
     86   1.1  perseant     kauth_cred_t, int, int *, struct lwp *);
     87   1.1  perseant 
     88   1.3  perseant extern int lfs_do_rfw;
     89   1.3  perseant 
     90   1.1  perseant /*
     91   1.1  perseant  * Allocate a particular inode with a particular version number, freeing
     92   1.1  perseant  * any previous versions of this inode that may have gone before.
     93   1.1  perseant  * Used by the roll-forward code.
     94   1.1  perseant  *
     95   1.1  perseant  * XXX this function does not have appropriate locking to be used on a live fs;
     96   1.1  perseant  * XXX but something similar could probably be used for an "undelete" call.
     97   1.1  perseant  *
     98   1.1  perseant  * Called with the Ifile inode locked.
     99   1.1  perseant  */
    100   1.1  perseant int
    101   1.1  perseant lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
    102   1.1  perseant 	      struct vnode **vpp)
    103   1.1  perseant {
    104  1.20   hannken 	struct vattr va;
    105   1.1  perseant 	struct vnode *vp;
    106   1.1  perseant 	struct inode *ip;
    107   1.1  perseant 	int error;
    108   1.1  perseant 
    109   1.1  perseant 	ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
    110   1.1  perseant 
    111   1.1  perseant 	/*
    112   1.1  perseant 	 * First, just try a vget. If the version number is the one we want,
    113   1.1  perseant 	 * we don't have to do anything else.  If the version number is wrong,
    114   1.1  perseant 	 * take appropriate action.
    115   1.1  perseant 	 */
    116   1.1  perseant 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    117   1.1  perseant 	if (error == 0) {
    118   1.1  perseant 		DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
    119   1.1  perseant 
    120   1.1  perseant 		*vpp = vp;
    121   1.1  perseant 		ip = VTOI(vp);
    122   1.1  perseant 		if (ip->i_gen == vers)
    123   1.1  perseant 			return 0;
    124   1.1  perseant 		else if (ip->i_gen < vers) {
    125   1.8        he 			lfs_truncate(vp, (off_t)0, 0, NOCRED);
    126  1.31  dholland 			ip->i_gen = vers;
    127  1.31  dholland 			lfs_dino_setgen(fs, ip->i_din, vers);
    128   1.1  perseant 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    129   1.1  perseant 			return 0;
    130   1.1  perseant 		} else {
    131   1.1  perseant 			DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
    132  1.31  dholland 			       ino, vers, lfs_dino_getgen(fs, ip->i_din)));
    133   1.1  perseant 			vput(vp);
    134   1.1  perseant 			*vpp = NULLVP;
    135   1.1  perseant 			return EEXIST;
    136   1.1  perseant 		}
    137   1.1  perseant 	}
    138   1.1  perseant 
    139  1.20   hannken 	/* Not found, create as regular file. */
    140  1.20   hannken 	vattr_null(&va);
    141  1.20   hannken 	va.va_type = VREG;
    142  1.20   hannken 	va.va_mode = 0;
    143  1.20   hannken 	va.va_fileid = ino;
    144  1.20   hannken 	va.va_gen = vers;
    145  1.20   hannken 	error = vcache_new(fs->lfs_ivnode->v_mount, NULL, &va, NOCRED, &vp);
    146  1.20   hannken 	if (error)
    147  1.20   hannken 		return error;
    148  1.20   hannken 	error = vn_lock(vp, LK_EXCLUSIVE);
    149  1.20   hannken 	if (error) {
    150  1.20   hannken 		vrele(vp);
    151  1.20   hannken 		*vpp = NULLVP;
    152  1.20   hannken 		return error;
    153   1.1  perseant 	}
    154  1.20   hannken 	ip = VTOI(vp);
    155  1.31  dholland 	ip->i_nlink = 1;
    156  1.31  dholland 	lfs_dino_setnlink(fs, ip->i_din, 1);
    157   1.1  perseant 	*vpp = vp;
    158  1.20   hannken 	return 0;
    159   1.1  perseant }
    160   1.1  perseant 
    161   1.1  perseant /*
    162   1.1  perseant  * Load the appropriate indirect block, and change the appropriate pointer.
    163   1.1  perseant  * Mark the block dirty.  Do segment and avail accounting.
    164   1.1  perseant  */
    165   1.1  perseant static int
    166   1.1  perseant update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
    167   1.1  perseant 	    daddr_t ndaddr, size_t size, struct lwp *l)
    168   1.1  perseant {
    169   1.1  perseant 	int error;
    170   1.1  perseant 	struct vnode *vp;
    171   1.1  perseant 	struct inode *ip;
    172   1.1  perseant #ifdef DEBUG
    173   1.1  perseant 	daddr_t odaddr;
    174  1.15  dholland 	struct indir a[ULFS_NIADDR];
    175   1.1  perseant 	int num;
    176   1.1  perseant 	int i;
    177   1.1  perseant #endif /* DEBUG */
    178   1.1  perseant 	struct buf *bp;
    179   1.1  perseant 	SEGUSE *sup;
    180   1.1  perseant 
    181   1.1  perseant 	KASSERT(lbn >= 0);	/* no indirect blocks */
    182   1.1  perseant 
    183   1.1  perseant 	if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
    184   1.1  perseant 		DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
    185   1.1  perseant 		      " returned %d\n", ino, error));
    186   1.1  perseant 		return error;
    187   1.1  perseant 	}
    188   1.1  perseant 
    189  1.23  dholland 	if ((error = lfs_balloc(vp, (lbn << lfs_sb_getbshift(fs)), size,
    190   1.1  perseant 				NOCRED, 0, &bp)) != 0) {
    191   1.1  perseant 		vput(vp);
    192   1.1  perseant 		return (error);
    193   1.1  perseant 	}
    194   1.1  perseant 	/* No need to write, the block is already on disk */
    195   1.9        ad 	if (bp->b_oflags & BO_DELWRI) {
    196   1.1  perseant 		LFS_UNLOCK_BUF(bp);
    197  1.22  dholland 		lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
    198  1.22  dholland 		/* XXX should this wake up fs->lfs_availsleep? */
    199   1.1  perseant 	}
    200   1.4        ad 	brelse(bp, BC_INVAL);
    201   1.1  perseant 
    202   1.1  perseant 	/*
    203   1.1  perseant 	 * Extend the file, if it is not large enough already.
    204   1.1  perseant 	 * XXX this is not exactly right, we don't know how much of the
    205   1.1  perseant 	 * XXX last block is actually used.  We hope that an inode will
    206   1.1  perseant 	 * XXX appear later to give the correct size.
    207   1.1  perseant 	 */
    208   1.1  perseant 	ip = VTOI(vp);
    209  1.23  dholland 	if (ip->i_size <= (lbn << lfs_sb_getbshift(fs))) {
    210   1.1  perseant 		u_int64_t newsize;
    211   1.1  perseant 
    212  1.31  dholland 		if (lbn < ULFS_NDADDR) {
    213  1.31  dholland 			newsize = (lbn << lfs_sb_getbshift(fs)) +
    214  1.22  dholland 				(size - lfs_sb_getfsize(fs)) + 1;
    215  1.31  dholland 		} else {
    216  1.31  dholland 			newsize = (lbn << lfs_sb_getbshift(fs)) + 1;
    217  1.31  dholland 		}
    218  1.31  dholland 		lfs_dino_setsize(fs, ip->i_din, newsize);
    219   1.1  perseant 
    220   1.1  perseant 		if (ip->i_size < newsize) {
    221   1.1  perseant 			ip->i_size = newsize;
    222   1.1  perseant 			/*
    223   1.1  perseant 			 * tell vm our new size for the case the inode won't
    224   1.1  perseant 			 * appear later.
    225   1.1  perseant 			 */
    226   1.1  perseant 			uvm_vnp_setsize(vp, newsize);
    227   1.1  perseant 		}
    228   1.1  perseant 	}
    229   1.1  perseant 
    230   1.1  perseant 	lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
    231   1.1  perseant 
    232  1.17  christos 	LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
    233   1.1  perseant 	sup->su_nbytes += size;
    234  1.17  christos 	LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
    235   1.1  perseant 
    236   1.1  perseant 	/* differences here should be due to UNWRITTEN indirect blocks. */
    237  1.17  christos 	KASSERT((lfs_lblkno(fs, ip->i_size) > ULFS_NDADDR &&
    238  1.31  dholland 	    ip->i_lfs_effnblks == lfs_dino_getblocks(fs, ip->i_din)) ||
    239  1.31  dholland 	    ip->i_lfs_effnblks >= lfs_dino_getblocks(fs, ip->i_din));
    240   1.1  perseant 
    241   1.1  perseant #ifdef DEBUG
    242   1.1  perseant 	/* Now look again to make sure it worked */
    243  1.15  dholland 	ulfs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
    244   1.1  perseant 	for (i = num; i > 0; i--) {
    245   1.1  perseant 		if (!a[i].in_exists)
    246   1.1  perseant 			panic("update_meta: absent %d lv indirect block", i);
    247   1.1  perseant 	}
    248  1.17  christos 	if (LFS_DBTOFSB(fs, odaddr) != ndaddr)
    249   1.1  perseant 		DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
    250   1.1  perseant 		      PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
    251   1.1  perseant #endif /* DEBUG */
    252   1.1  perseant 	vput(vp);
    253   1.1  perseant 	return 0;
    254   1.1  perseant }
    255   1.1  perseant 
    256  1.30  dholland /*
    257  1.30  dholland  * Copy some the fields of the dinode as needed by update_inoblk().
    258  1.30  dholland  */
    259  1.30  dholland static void
    260  1.30  dholland update_inoblk_copy_dinode(struct lfs *fs,
    261  1.30  dholland     union lfs_dinode *dstu, const union lfs_dinode *srcu)
    262  1.30  dholland {
    263  1.30  dholland 	if (fs->lfs_is64) {
    264  1.30  dholland 		struct lfs64_dinode *dst = &dstu->u_64;
    265  1.30  dholland 		const struct lfs64_dinode *src = &srcu->u_64;
    266  1.30  dholland 		unsigned i;
    267  1.30  dholland 
    268  1.30  dholland 		/*
    269  1.30  dholland 		 * Copy everything but the block pointers and di_blocks.
    270  1.30  dholland 		 * XXX what about di_extb?
    271  1.30  dholland 		 */
    272  1.30  dholland 		dst->di_mode = src->di_mode;
    273  1.30  dholland 		dst->di_nlink = src->di_nlink;
    274  1.30  dholland 		dst->di_uid = src->di_uid;
    275  1.30  dholland 		dst->di_gid = src->di_gid;
    276  1.30  dholland 		dst->di_blksize = src->di_blksize;
    277  1.30  dholland 		dst->di_size = src->di_size;
    278  1.30  dholland 		dst->di_atime = src->di_atime;
    279  1.30  dholland 		dst->di_mtime = src->di_mtime;
    280  1.30  dholland 		dst->di_ctime = src->di_ctime;
    281  1.30  dholland 		dst->di_birthtime = src->di_birthtime;
    282  1.30  dholland 		dst->di_mtimensec = src->di_mtimensec;
    283  1.30  dholland 		dst->di_atimensec = src->di_atimensec;
    284  1.30  dholland 		dst->di_ctimensec = src->di_ctimensec;
    285  1.30  dholland 		dst->di_birthnsec = src->di_birthnsec;
    286  1.30  dholland 		dst->di_gen = src->di_gen;
    287  1.30  dholland 		dst->di_kernflags = src->di_kernflags;
    288  1.30  dholland 		dst->di_flags = src->di_flags;
    289  1.30  dholland 		dst->di_extsize = src->di_extsize;
    290  1.30  dholland 		dst->di_modrev = src->di_modrev;
    291  1.30  dholland 		dst->di_inumber = src->di_inumber;
    292  1.30  dholland 		for (i = 0; i < __arraycount(src->di_spare); i++) {
    293  1.30  dholland 			dst->di_spare[i] = src->di_spare[i];
    294  1.30  dholland 		}
    295  1.30  dholland 	} else {
    296  1.30  dholland 		struct lfs32_dinode *dst = &dstu->u_32;
    297  1.30  dholland 		const struct lfs32_dinode *src = &srcu->u_32;
    298  1.30  dholland 
    299  1.30  dholland 		/* Get mode, link count, size, and times */
    300  1.30  dholland 		memcpy(dst, src, offsetof(struct lfs32_dinode, di_db[0]));
    301  1.30  dholland 
    302  1.30  dholland 		/* Then the rest, except di_blocks */
    303  1.30  dholland 		dst->di_flags = src->di_flags;
    304  1.30  dholland 		dst->di_gen = src->di_gen;
    305  1.30  dholland 		dst->di_uid = src->di_uid;
    306  1.30  dholland 		dst->di_gid = src->di_gid;
    307  1.30  dholland 		dst->di_modrev = src->di_modrev;
    308  1.30  dholland 	}
    309  1.30  dholland }
    310  1.30  dholland 
    311   1.1  perseant static int
    312   1.1  perseant update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
    313   1.1  perseant 	      struct lwp *l)
    314   1.1  perseant {
    315   1.1  perseant 	struct vnode *devvp, *vp;
    316   1.1  perseant 	struct inode *ip;
    317  1.29  dholland 	union lfs_dinode *dip;
    318   1.1  perseant 	struct buf *dbp, *ibp;
    319   1.1  perseant 	int error;
    320   1.1  perseant 	daddr_t daddr;
    321   1.1  perseant 	IFILE *ifp;
    322   1.1  perseant 	SEGUSE *sup;
    323  1.29  dholland 	unsigned i, num;
    324   1.1  perseant 
    325   1.1  perseant 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    326   1.1  perseant 
    327   1.1  perseant 	/*
    328   1.1  perseant 	 * Get the inode, update times and perms.
    329   1.1  perseant 	 * DO NOT update disk blocks, we do that separately.
    330   1.1  perseant 	 */
    331  1.23  dholland 	error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getibsize(fs),
    332  1.19      maxv 	    0, &dbp);
    333   1.1  perseant 	if (error) {
    334   1.1  perseant 		DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
    335   1.1  perseant 		return error;
    336   1.1  perseant 	}
    337  1.29  dholland 	num = LFS_INOPB(fs);
    338  1.29  dholland 	for (i = num; i-- > 0; ) {
    339  1.30  dholland 		dip = DINO_IN_BLOCK(fs, dbp->b_data, i);
    340  1.29  dholland 		if (lfs_dino_getinumber(fs, dip) > LFS_IFILE_INUM) {
    341  1.29  dholland 			error = lfs_rf_valloc(fs, lfs_dino_getinumber(fs, dip),
    342  1.29  dholland 					      lfs_dino_getgen(fs, dip),
    343   1.1  perseant 					      l, &vp);
    344   1.1  perseant 			if (error) {
    345   1.1  perseant 				DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
    346   1.1  perseant 				      " returned %d\n", error));
    347   1.1  perseant 				continue;
    348   1.1  perseant 			}
    349   1.1  perseant 			ip = VTOI(vp);
    350  1.29  dholland 			if (lfs_dino_getsize(fs, dip) != ip->i_size)
    351  1.29  dholland 				lfs_truncate(vp, lfs_dino_getsize(fs, dip), 0,
    352  1.29  dholland 					     NOCRED);
    353  1.30  dholland 			update_inoblk_copy_dinode(fs, ip->i_din, dip);
    354   1.1  perseant 
    355  1.29  dholland 			ip->i_flags = lfs_dino_getflags(fs, dip);
    356  1.29  dholland 			ip->i_gen = lfs_dino_getgen(fs, dip);
    357  1.29  dholland 			ip->i_uid = lfs_dino_getuid(fs, dip);
    358  1.29  dholland 			ip->i_gid = lfs_dino_getgid(fs, dip);
    359  1.29  dholland 
    360  1.29  dholland 			ip->i_mode = lfs_dino_getmode(fs, dip);
    361  1.29  dholland 			ip->i_nlink = lfs_dino_getnlink(fs, dip);
    362  1.29  dholland 			ip->i_size = lfs_dino_getsize(fs, dip);
    363   1.1  perseant 
    364   1.1  perseant 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    365   1.1  perseant 
    366   1.1  perseant 			/* Re-initialize to get type right */
    367  1.15  dholland 			ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
    368   1.1  perseant 				  &vp);
    369   1.1  perseant 			vput(vp);
    370   1.1  perseant 
    371   1.1  perseant 			/* Record change in location */
    372  1.29  dholland 			LFS_IENTRY(ifp, fs, lfs_dino_getinumber(fs, dip), ibp);
    373  1.26  dholland 			daddr = lfs_if_getdaddr(fs, ifp);
    374  1.26  dholland 			lfs_if_setdaddr(fs, ifp, LFS_DBTOFSB(fs, dbp->b_blkno));
    375   1.1  perseant 			error = LFS_BWRITE_LOG(ibp); /* Ifile */
    376   1.1  perseant 			/* And do segment accounting */
    377  1.17  christos 			if (lfs_dtosn(fs, daddr) != lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno))) {
    378   1.1  perseant 				if (daddr > 0) {
    379  1.17  christos 					LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, daddr),
    380   1.1  perseant 						     ibp);
    381  1.29  dholland 					sup->su_nbytes -= DINOSIZE(fs);
    382   1.1  perseant 					LFS_WRITESEGENTRY(sup, fs,
    383  1.17  christos 							  lfs_dtosn(fs, daddr),
    384   1.1  perseant 							  ibp);
    385   1.1  perseant 				}
    386  1.17  christos 				LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
    387   1.1  perseant 					     ibp);
    388  1.29  dholland 				sup->su_nbytes += DINOSIZE(fs);
    389   1.1  perseant 				LFS_WRITESEGENTRY(sup, fs,
    390  1.17  christos 						  lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
    391   1.1  perseant 						  ibp);
    392   1.1  perseant 			}
    393   1.1  perseant 		}
    394   1.1  perseant 	}
    395   1.4        ad 	brelse(dbp, BC_AGE);
    396   1.1  perseant 
    397   1.1  perseant 	return 0;
    398   1.1  perseant }
    399   1.1  perseant 
    400   1.1  perseant #define CHECK_CKSUM   0x0001  /* Check the checksum to make sure it's valid */
    401   1.1  perseant #define CHECK_UPDATE  0x0002  /* Update Ifile for new data blocks / inodes */
    402   1.1  perseant 
    403   1.1  perseant static daddr_t
    404   1.1  perseant check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
    405   1.1  perseant 	     kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
    406   1.1  perseant {
    407   1.1  perseant 	struct vnode *devvp;
    408   1.1  perseant 	struct buf *bp, *dbp;
    409   1.1  perseant 	int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
    410   1.1  perseant 	SEGSUM *ssp;
    411   1.1  perseant 	u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
    412   1.1  perseant 	daddr_t oldoffset;
    413  1.32  dholland 	IINFO *iip;
    414   1.1  perseant 	FINFO *fip;
    415   1.1  perseant 	SEGUSE *sup;
    416   1.1  perseant 	size_t size;
    417  1.27  dholland 	uint32_t datasum, foundsum;
    418   1.1  perseant 
    419   1.1  perseant 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    420   1.1  perseant 	/*
    421   1.1  perseant 	 * If the segment has a superblock and we're at the top
    422   1.1  perseant 	 * of the segment, skip the superblock.
    423   1.1  perseant 	 */
    424  1.17  christos 	if (lfs_sntod(fs, lfs_dtosn(fs, offset)) == offset) {
    425  1.17  christos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    426   1.1  perseant 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
    427  1.17  christos 			offset += lfs_btofsb(fs, LFS_SBPAD);
    428   1.4        ad 		brelse(bp, 0);
    429   1.1  perseant 	}
    430   1.1  perseant 
    431   1.1  perseant 	/* Read in the segment summary */
    432  1.23  dholland 	error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getsumsize(fs),
    433  1.19      maxv 	    0, &bp);
    434   1.1  perseant 	if (error)
    435   1.1  perseant 		return -1;
    436   1.1  perseant 
    437   1.1  perseant 	/* Check summary checksum */
    438   1.1  perseant 	ssp = (SEGSUM *)bp->b_data;
    439   1.1  perseant 	if (flags & CHECK_CKSUM) {
    440  1.27  dholland 		size_t sumstart;
    441  1.27  dholland 
    442  1.27  dholland 		sumstart = lfs_ss_getsumstart(fs);
    443  1.27  dholland 		if (lfs_ss_getsumsum(fs, ssp) !=
    444  1.27  dholland 		    cksum((char *)ssp + sumstart,
    445  1.27  dholland 			  lfs_sb_getsumsize(fs) - sumstart)) {
    446   1.1  perseant 			DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
    447   1.1  perseant 			offset = -1;
    448   1.1  perseant 			goto err1;
    449   1.1  perseant 		}
    450  1.27  dholland 		if (lfs_ss_getnfinfo(fs, ssp) == 0 &&
    451  1.27  dholland 		    lfs_ss_getninos(fs, ssp) == 0) {
    452   1.1  perseant 			DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
    453   1.1  perseant 			offset = -1;
    454   1.1  perseant 			goto err1;
    455   1.1  perseant 		}
    456  1.27  dholland 		if (lfs_ss_getcreate(fs, ssp) < lfs_sb_gettstamp(fs)) {
    457   1.1  perseant 			DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
    458   1.1  perseant 			offset = -1;
    459   1.1  perseant 			goto err1;
    460   1.1  perseant 		}
    461   1.1  perseant 	}
    462  1.25  dholland 	if (lfs_sb_getversion(fs) > 1) {
    463  1.27  dholland 		if (lfs_ss_getserial(fs, ssp) != nextserial) {
    464   1.1  perseant 			DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
    465   1.1  perseant 			      "\n", offset));
    466   1.1  perseant 			offset = -1;
    467   1.1  perseant 			goto err1;
    468   1.1  perseant 		}
    469  1.27  dholland 		if (lfs_ss_getident(fs, ssp) != lfs_sb_getident(fs)) {
    470   1.1  perseant 			DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
    471  1.27  dholland 			      PRIx64 "\n", lfs_ss_getident(fs, ssp),
    472  1.27  dholland 			      lfs_sb_getident(fs), offset));
    473   1.1  perseant 			offset = -1;
    474   1.1  perseant 			goto err1;
    475   1.1  perseant 		}
    476   1.1  perseant 	}
    477   1.1  perseant 	if (pseg_flags)
    478  1.27  dholland 		*pseg_flags = lfs_ss_getflags(fs, ssp);
    479   1.1  perseant 	oldoffset = offset;
    480  1.23  dholland 	offset += lfs_btofsb(fs, lfs_sb_getsumsize(fs));
    481   1.1  perseant 
    482  1.27  dholland 	ninos = howmany(lfs_ss_getninos(fs, ssp), LFS_INOPB(fs));
    483  1.32  dholland 	iip = SEGSUM_IINFOSTART(fs, bp->b_data);
    484   1.1  perseant 	if (flags & CHECK_CKSUM) {
    485   1.1  perseant 		/* Count blocks */
    486   1.1  perseant 		nblocks = 0;
    487  1.27  dholland 		fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
    488  1.27  dholland 		for (i = 0; i < lfs_ss_getnfinfo(fs, ssp); ++i) {
    489  1.28  dholland 			nblocks += lfs_fi_getnblocks(fs, fip);
    490  1.28  dholland 			if (lfs_fi_getnblocks(fs, fip) <= 0)
    491   1.1  perseant 				break;
    492  1.27  dholland 			fip = NEXT_FINFO(fs, fip);
    493   1.1  perseant 		}
    494   1.1  perseant 		nblocks += ninos;
    495   1.1  perseant 		/* Create the sum array */
    496  1.21  dholland 		datap = dp = malloc(nblocks * sizeof(u_long),
    497  1.21  dholland 				    M_SEGMENT, M_WAITOK);
    498   1.1  perseant 	}
    499   1.1  perseant 
    500   1.1  perseant 	/* Handle individual blocks */
    501  1.27  dholland 	fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
    502  1.27  dholland 	for (i = 0; i < lfs_ss_getnfinfo(fs, ssp) || ninos; ++i) {
    503   1.1  perseant 		/* Inode block? */
    504  1.32  dholland 		if (ninos && lfs_ii_getblock(fs, iip) == offset) {
    505   1.1  perseant 			if (flags & CHECK_CKSUM) {
    506   1.1  perseant 				/* Read in the head and add to the buffer */
    507  1.22  dholland 				error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getbsize(fs),
    508  1.19      maxv 					      0, &dbp);
    509   1.1  perseant 				if (error) {
    510   1.1  perseant 					offset = -1;
    511   1.1  perseant 					goto err2;
    512   1.1  perseant 				}
    513  1.28  dholland 				/* XXX this can't be right, on-disk u_long? */
    514   1.1  perseant 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    515   1.4        ad 				brelse(dbp, BC_AGE);
    516   1.1  perseant 			}
    517   1.1  perseant 			if (flags & CHECK_UPDATE) {
    518   1.1  perseant 				if ((error = update_inoblk(fs, offset, cred, l))
    519   1.1  perseant 				    != 0) {
    520   1.1  perseant 					offset = -1;
    521   1.1  perseant 					goto err2;
    522   1.1  perseant 				}
    523   1.1  perseant 			}
    524  1.23  dholland 			offset += lfs_btofsb(fs, lfs_sb_getibsize(fs));
    525  1.32  dholland 			iip = NEXTLOWER_IINFO(fs, iip);
    526   1.1  perseant 			--ninos;
    527  1.32  dholland 			--i; /* compensate for ++i in loop header */
    528   1.1  perseant 			continue;
    529   1.1  perseant 		}
    530  1.22  dholland 		size = lfs_sb_getbsize(fs);
    531  1.28  dholland 		for (j = 0; j < lfs_fi_getnblocks(fs, fip); ++j) {
    532  1.28  dholland 			if (j == lfs_fi_getnblocks(fs, fip) - 1)
    533  1.28  dholland 				size = lfs_fi_getlastlength(fs, fip);
    534   1.1  perseant 			if (flags & CHECK_CKSUM) {
    535  1.17  christos 				error = bread(devvp, LFS_FSBTODB(fs, offset), size,
    536  1.19      maxv 				    0, &dbp);
    537   1.1  perseant 				if (error) {
    538   1.1  perseant 					offset = -1;
    539   1.1  perseant 					goto err2;
    540   1.1  perseant 				}
    541   1.1  perseant 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    542   1.4        ad 				brelse(dbp, BC_AGE);
    543   1.1  perseant 			}
    544   1.1  perseant 			/* Account for and update any direct blocks */
    545   1.1  perseant 			if ((flags & CHECK_UPDATE) &&
    546  1.28  dholland 			   lfs_fi_getino(fs, fip) > LFS_IFILE_INUM &&
    547  1.28  dholland 			   lfs_fi_getblock(fs, fip, j) >= 0) {
    548  1.28  dholland 				update_meta(fs, lfs_fi_getino(fs, fip),
    549  1.28  dholland 					    lfs_fi_getversion(fs, fip),
    550  1.28  dholland 					    lfs_fi_getblock(fs, fip, j),
    551  1.28  dholland 					    offset, size, l);
    552   1.1  perseant 			}
    553  1.17  christos 			offset += lfs_btofsb(fs, size);
    554   1.1  perseant 		}
    555  1.27  dholland 		fip = NEXT_FINFO(fs, fip);
    556   1.1  perseant 	}
    557   1.1  perseant 	/* Checksum the array, compare */
    558  1.27  dholland 	datasum = lfs_ss_getdatasum(fs, ssp);
    559  1.27  dholland 	foundsum = cksum(datap, nblocks * sizeof(u_long));
    560  1.27  dholland 	if ((flags & CHECK_CKSUM) && datasum != foundsum) {
    561   1.1  perseant 		DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
    562   1.1  perseant 		      " (wanted %x got %x)\n",
    563  1.27  dholland 		      offset, datasum, foundsum));
    564   1.1  perseant 		offset = -1;
    565   1.1  perseant 		goto err2;
    566   1.1  perseant 	}
    567   1.1  perseant 
    568   1.1  perseant 	/* If we're at the end of the segment, move to the next */
    569  1.23  dholland 	if (lfs_dtosn(fs, offset + lfs_btofsb(fs, lfs_sb_getsumsize(fs) + lfs_sb_getbsize(fs))) !=
    570  1.17  christos 	   lfs_dtosn(fs, offset)) {
    571  1.27  dholland 		if (lfs_dtosn(fs, offset) == lfs_dtosn(fs, lfs_ss_getnext(fs, ssp))) {
    572   1.1  perseant 			offset = -1;
    573   1.1  perseant 			goto err2;
    574   1.1  perseant 		}
    575  1.27  dholland 		offset = lfs_ss_getnext(fs, ssp);
    576   1.1  perseant 		DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
    577  1.17  christos 		       " -> segment %d\n", offset, lfs_dtosn(fs,offset)));
    578   1.1  perseant 	}
    579   1.1  perseant 
    580   1.1  perseant 	if (flags & CHECK_UPDATE) {
    581  1.22  dholland 		lfs_sb_subavail(fs, offset - oldoffset);
    582   1.1  perseant 		/* Don't clog the buffer queue */
    583   1.9        ad 		mutex_enter(&lfs_lock);
    584   1.1  perseant 		if (locked_queue_count > LFS_MAX_BUFS ||
    585   1.1  perseant 		    locked_queue_bytes > LFS_MAX_BYTES) {
    586   1.1  perseant 			lfs_flush(fs, SEGM_CKP, 0);
    587   1.1  perseant 		}
    588   1.9        ad 		mutex_exit(&lfs_lock);
    589   1.1  perseant 	}
    590   1.1  perseant 
    591   1.1  perseant     err2:
    592   1.1  perseant 	if (flags & CHECK_CKSUM)
    593   1.1  perseant 		free(datap, M_SEGMENT);
    594   1.1  perseant     err1:
    595   1.7        ad 	brelse(bp, BC_AGE);
    596   1.1  perseant 
    597   1.1  perseant 	/* XXX should we update the serial number even for bad psegs? */
    598  1.25  dholland 	if ((flags & CHECK_UPDATE) && offset > 0 && lfs_sb_getversion(fs) > 1)
    599  1.22  dholland 		lfs_sb_setserial(fs, nextserial);
    600   1.1  perseant 	return offset;
    601   1.1  perseant }
    602   1.1  perseant 
    603   1.1  perseant void
    604   1.2  perseant lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
    605   1.1  perseant {
    606   1.3  perseant 	int flags, dirty;
    607   1.3  perseant 	daddr_t offset, oldoffset, lastgoodpseg;
    608   1.3  perseant 	int sn, curseg, do_rollforward;
    609   1.3  perseant 	struct proc *p;
    610   1.3  perseant 	kauth_cred_t cred;
    611   1.3  perseant 	SEGUSE *sup;
    612   1.3  perseant 	struct buf *bp;
    613   1.3  perseant 
    614   1.3  perseant 	p = l ? l->l_proc : NULL;
    615   1.3  perseant 	cred = p ? p->p_cred : NOCRED;
    616   1.1  perseant 
    617   1.1  perseant 	/*
    618   1.1  perseant 	 * Roll forward.
    619   1.1  perseant 	 *
    620   1.1  perseant 	 * We don't roll forward for v1 filesystems, because
    621   1.1  perseant 	 * of the danger that the clock was turned back between the last
    622   1.1  perseant 	 * checkpoint and crash.  This would roll forward garbage.
    623   1.1  perseant 	 *
    624   1.1  perseant 	 * v2 filesystems don't have this problem because they use a
    625   1.1  perseant 	 * monotonically increasing serial number instead of a timestamp.
    626   1.1  perseant 	 */
    627  1.23  dholland 	do_rollforward = (!(lfs_sb_getpflags(fs) & LFS_PF_CLEAN) &&
    628  1.25  dholland 			  lfs_do_rfw && lfs_sb_getversion(fs) > 1 && p != NULL);
    629   1.1  perseant 	if (do_rollforward) {
    630   1.1  perseant 		u_int64_t nextserial;
    631   1.1  perseant 		/*
    632   1.1  perseant 		 * Phase I: Find the address of the last good partial
    633   1.1  perseant 		 * segment that was written after the checkpoint.  Mark
    634   1.1  perseant 		 * the segments in question dirty, so they won't be
    635   1.1  perseant 		 * reallocated.
    636   1.1  perseant 		 */
    637  1.22  dholland 		lastgoodpseg = oldoffset = offset = lfs_sb_getoffset(fs);
    638   1.1  perseant 		flags = 0x0;
    639   1.1  perseant 		DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
    640   1.1  perseant 		      PRIx64 "\n", offset));
    641  1.17  christos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    642   1.1  perseant 		if (!(sup->su_flags & SEGUSE_DIRTY))
    643  1.23  dholland 			lfs_sb_subnclean(fs, 1);
    644   1.1  perseant 		sup->su_flags |= SEGUSE_DIRTY;
    645  1.17  christos 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
    646  1.22  dholland 		nextserial = lfs_sb_getserial(fs) + 1;
    647   1.1  perseant 		while ((offset = check_segsum(fs, offset, nextserial,
    648   1.1  perseant 		    cred, CHECK_CKSUM, &flags, l)) > 0) {
    649   1.1  perseant 			nextserial++;
    650  1.17  christos 			if (lfs_sntod(fs, oldoffset) != lfs_sntod(fs, offset)) {
    651  1.17  christos 				LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
    652   1.1  perseant 					     bp);
    653   1.1  perseant 				if (!(sup->su_flags & SEGUSE_DIRTY))
    654  1.23  dholland 					lfs_sb_subnclean(fs, 1);
    655   1.1  perseant 				sup->su_flags |= SEGUSE_DIRTY;
    656  1.17  christos 				LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
    657   1.1  perseant 					     bp);
    658   1.1  perseant 			}
    659   1.1  perseant 
    660   1.1  perseant 			DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
    661   1.1  perseant 			      PRIx64 "\n", offset));
    662   1.1  perseant 			if (flags & SS_DIROP) {
    663   1.1  perseant 				DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
    664   1.1  perseant 				      PRIx64 "\n", oldoffset));
    665   1.8        he 				if (!(flags & SS_CONT)) {
    666   1.1  perseant 				     DLOG((DLOG_RF, "lfs_mountfs: dirops end "
    667   1.1  perseant 					   "at 0x%" PRIx64 "\n", oldoffset));
    668   1.8        he 				}
    669   1.1  perseant 			}
    670   1.1  perseant 			if (!(flags & SS_CONT))
    671   1.1  perseant 				lastgoodpseg = offset;
    672   1.1  perseant 			oldoffset = offset;
    673   1.1  perseant 		}
    674   1.1  perseant 		if (flags & SS_CONT) {
    675   1.1  perseant 			DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
    676   1.1  perseant 			      "dirops discarded\n"));
    677   1.1  perseant 		}
    678   1.1  perseant 		DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
    679   1.1  perseant 		      "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
    680  1.22  dholland 		oldoffset = lfs_sb_getoffset(fs);
    681  1.22  dholland 		if (lfs_sb_getoffset(fs) != lastgoodpseg) {
    682   1.1  perseant 			/* Don't overwrite what we're trying to preserve */
    683  1.22  dholland 			offset = lfs_sb_getoffset(fs);
    684  1.22  dholland 			lfs_sb_setoffset(fs, lastgoodpseg);
    685  1.22  dholland 			lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_dtosn(fs, lfs_sb_getoffset(fs))));
    686  1.22  dholland 			for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs));;) {
    687  1.23  dholland 				sn = (sn + 1) % lfs_sb_getnseg(fs);
    688   1.1  perseant 				if (sn == curseg)
    689   1.1  perseant 					panic("lfs_mountfs: no clean segments");
    690   1.1  perseant 				LFS_SEGENTRY(sup, fs, sn, bp);
    691   1.1  perseant 				dirty = (sup->su_flags & SEGUSE_DIRTY);
    692   1.4        ad 				brelse(bp, 0);
    693   1.1  perseant 				if (!dirty)
    694   1.1  perseant 					break;
    695   1.1  perseant 			}
    696  1.22  dholland 			lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
    697   1.1  perseant 
    698   1.1  perseant 			/*
    699   1.1  perseant 			 * Phase II: Roll forward from the first superblock.
    700   1.1  perseant 			 */
    701   1.1  perseant 			while (offset != lastgoodpseg) {
    702   1.1  perseant 				DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
    703   1.1  perseant 				      PRIx64 "\n", offset));
    704   1.1  perseant 				offset = check_segsum(fs, offset,
    705  1.22  dholland 				    lfs_sb_getserial(fs) + 1, cred, CHECK_UPDATE,
    706   1.1  perseant 				    NULL, l);
    707   1.1  perseant 			}
    708   1.1  perseant 
    709   1.1  perseant 			/*
    710   1.1  perseant 			 * Finish: flush our changes to disk.
    711   1.1  perseant 			 */
    712   1.1  perseant 			lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
    713   1.1  perseant 			DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
    714  1.22  dholland 			      "recovered %jd blocks\n",
    715  1.22  dholland 			      (intmax_t)(lastgoodpseg - oldoffset)));
    716   1.1  perseant 		}
    717   1.1  perseant 		DLOG((DLOG_RF, "LFS roll forward complete\n"));
    718   1.1  perseant 	}
    719   1.1  perseant }
    720