Home | History | Annotate | Line # | Download | only in lfs
lfs_syscalls.c revision 1.67
      1  1.67  perseant /*	$NetBSD: lfs_syscalls.c,v 1.67 2002/06/16 00:13:16 perseant Exp $	*/
      2   1.3       cgd 
      3   1.1   mycroft /*-
      4  1.49  perseant  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5  1.22  perseant  * All rights reserved.
      6  1.22  perseant  *
      7  1.22  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.22  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  1.22  perseant  *
     10  1.22  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.22  perseant  * modification, are permitted provided that the following conditions
     12  1.22  perseant  * are met:
     13  1.22  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.22  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.22  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.22  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.22  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.22  perseant  * 3. All advertising materials mentioning features or use of this software
     19  1.22  perseant  *    must display the following acknowledgement:
     20  1.22  perseant  *      This product includes software developed by the NetBSD
     21  1.22  perseant  *      Foundation, Inc. and its contributors.
     22  1.22  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.22  perseant  *    contributors may be used to endorse or promote products derived
     24  1.22  perseant  *    from this software without specific prior written permission.
     25  1.22  perseant  *
     26  1.22  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.22  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.22  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.22  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.22  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.22  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.22  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.22  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.22  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.22  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.22  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37  1.22  perseant  */
     38  1.22  perseant /*-
     39   1.1   mycroft  * Copyright (c) 1991, 1993, 1994
     40   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     41   1.1   mycroft  *
     42   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43   1.1   mycroft  * modification, are permitted provided that the following conditions
     44   1.1   mycroft  * are met:
     45   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50   1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     51   1.1   mycroft  *    must display the following acknowledgement:
     52   1.1   mycroft  *	This product includes software developed by the University of
     53   1.1   mycroft  *	California, Berkeley and its contributors.
     54   1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     55   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     56   1.1   mycroft  *    without specific prior written permission.
     57   1.1   mycroft  *
     58   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1   mycroft  * SUCH DAMAGE.
     69   1.1   mycroft  *
     70  1.16      fvdl  *	@(#)lfs_syscalls.c	8.10 (Berkeley) 5/14/95
     71   1.1   mycroft  */
     72  1.61     lukem 
     73  1.61     lukem #include <sys/cdefs.h>
     74  1.67  perseant __KERNEL_RCSID(0, "$NetBSD: lfs_syscalls.c,v 1.67 2002/06/16 00:13:16 perseant Exp $");
     75  1.15   thorpej 
     76  1.55  jdolecek #define LFS		/* for prototypes in syscallargs.h */
     77   1.1   mycroft 
     78   1.1   mycroft #include <sys/param.h>
     79   1.5       cgd #include <sys/systm.h>
     80   1.1   mycroft #include <sys/proc.h>
     81   1.1   mycroft #include <sys/buf.h>
     82   1.1   mycroft #include <sys/mount.h>
     83   1.1   mycroft #include <sys/vnode.h>
     84   1.1   mycroft #include <sys/malloc.h>
     85   1.1   mycroft #include <sys/kernel.h>
     86   1.1   mycroft 
     87   1.5       cgd #include <sys/syscallargs.h>
     88   1.5       cgd 
     89   1.1   mycroft #include <ufs/ufs/inode.h>
     90   1.1   mycroft #include <ufs/ufs/ufsmount.h>
     91   1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     92   1.1   mycroft 
     93   1.1   mycroft #include <ufs/lfs/lfs.h>
     94   1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     95  1.10  christos 
     96  1.22  perseant /* Flags for return from lfs_fastvget */
     97  1.22  perseant #define FVG_UNLOCK 0x01	  /* Needs to be unlocked */
     98  1.22  perseant #define FVG_PUT	   0x02	  /* Needs to be vput() */
     99   1.1   mycroft 
    100  1.58  jdolecek /* Max block count for lfs_markv() */
    101  1.58  jdolecek #define MARKV_MAXBLKCNT		65536
    102  1.58  jdolecek 
    103  1.65  perseant struct buf *lfs_fakebuf(struct lfs *, struct vnode *, int, size_t, caddr_t);
    104  1.57  perseant int lfs_fasthashget(dev_t, ino_t, int *, struct vnode **);
    105   1.1   mycroft 
    106  1.16      fvdl int debug_cleaner = 0;
    107  1.16      fvdl int clean_vnlocked = 0;
    108  1.16      fvdl int clean_inlocked = 0;
    109  1.22  perseant int verbose_debug = 0;
    110  1.22  perseant 
    111  1.22  perseant pid_t lfs_cleaner_pid = 0;
    112  1.22  perseant 
    113  1.22  perseant /*
    114  1.22  perseant  * Definitions for the buffer free lists.
    115  1.22  perseant  */
    116  1.22  perseant #define BQUEUES		4		/* number of free buffer queues */
    117  1.22  perseant 
    118  1.22  perseant #define BQ_LOCKED	0		/* super-blocks &c */
    119  1.22  perseant #define BQ_LRU		1		/* lru, useful buffers */
    120  1.22  perseant #define BQ_AGE		2		/* rubbish */
    121  1.22  perseant #define BQ_EMPTY	3		/* buffer headers with no memory */
    122  1.22  perseant 
    123  1.22  perseant extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    124  1.22  perseant 
    125  1.22  perseant #define LFS_FORCE_WRITE UNASSIGNED
    126  1.22  perseant 
    127  1.22  perseant #define LFS_VREF_THRESHOLD 128
    128  1.16      fvdl 
    129  1.57  perseant static int lfs_bmapv(struct proc *, fsid_t *, BLOCK_INFO *, int);
    130  1.57  perseant static int lfs_markv(struct proc *, fsid_t *, BLOCK_INFO *, int);
    131  1.57  perseant 
    132   1.1   mycroft /*
    133  1.31  christos  * sys_lfs_markv:
    134   1.1   mycroft  *
    135   1.1   mycroft  * This will mark inodes and blocks dirty, so they are written into the log.
    136   1.1   mycroft  * It will block until all the blocks have been written.  The segment create
    137   1.1   mycroft  * time passed in the block_info and inode_info structures is used to decide
    138   1.1   mycroft  * if the data is valid for each block (in case some process dirtied a block
    139   1.1   mycroft  * or inode that is being cleaned between the determination that a block is
    140   1.1   mycroft  * live and the lfs_markv call).
    141   1.1   mycroft  *
    142   1.1   mycroft  *  0 on success
    143   1.1   mycroft  * -1/errno is return on error.
    144   1.1   mycroft  */
    145  1.57  perseant #ifdef USE_64BIT_SYSCALLS
    146   1.1   mycroft int
    147  1.57  perseant sys_lfs_markv(struct proc *p, void *v, register_t *retval)
    148   1.9   thorpej {
    149  1.32  drochner 	struct sys_lfs_markv_args /* {
    150   1.5       cgd 		syscallarg(fsid_t *) fsidp;
    151   1.5       cgd 		syscallarg(struct block_info *) blkiov;
    152   1.5       cgd 		syscallarg(int) blkcnt;
    153  1.32  drochner 	} */ *uap = v;
    154  1.57  perseant 	BLOCK_INFO *blkiov;
    155  1.57  perseant 	int blkcnt, error;
    156  1.57  perseant 	fsid_t fsid;
    157  1.57  perseant 
    158  1.57  perseant 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    159  1.57  perseant 		return (error);
    160  1.57  perseant 
    161  1.57  perseant 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    162  1.57  perseant 		return (error);
    163  1.57  perseant 
    164  1.57  perseant 	blkcnt = SCARG(uap, blkcnt);
    165  1.58  jdolecek 	if ((u_int) blkcnt > MARKV_MAXBLKCNT)
    166  1.58  jdolecek 		return (EINVAL);
    167  1.58  jdolecek 
    168  1.57  perseant 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    169  1.57  perseant 	if ((error = copyin(SCARG(uap, blkiov), blkiov,
    170  1.57  perseant 			    blkcnt * sizeof(BLOCK_INFO))) != 0)
    171  1.57  perseant 		goto out;
    172  1.57  perseant 
    173  1.57  perseant 	if ((error = lfs_markv(p, &fsid, blkiov, blkcnt)) == 0)
    174  1.57  perseant 		copyout(blkiov, SCARG(uap, blkiov),
    175  1.57  perseant 			blkcnt * sizeof(BLOCK_INFO));
    176  1.57  perseant     out:
    177  1.57  perseant 	free(blkiov, M_SEGMENT);
    178  1.57  perseant 	return error;
    179  1.57  perseant }
    180  1.57  perseant #else
    181  1.57  perseant int
    182  1.57  perseant sys_lfs_markv(struct proc *p, void *v, register_t *retval)
    183  1.57  perseant {
    184  1.57  perseant 	struct sys_lfs_markv_args /* {
    185  1.57  perseant 		syscallarg(fsid_t *) fsidp;
    186  1.57  perseant 		syscallarg(struct block_info *) blkiov;
    187  1.57  perseant 		syscallarg(int) blkcnt;
    188  1.57  perseant 	} */ *uap = v;
    189  1.57  perseant 	BLOCK_INFO *blkiov;
    190  1.57  perseant 	BLOCK_INFO_15 *blkiov15;
    191  1.57  perseant 	int i, blkcnt, error;
    192  1.57  perseant 	fsid_t fsid;
    193  1.57  perseant 
    194  1.57  perseant 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    195  1.57  perseant 		return (error);
    196  1.57  perseant 
    197  1.57  perseant 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    198  1.57  perseant 		return (error);
    199  1.57  perseant 
    200  1.57  perseant 	blkcnt = SCARG(uap, blkcnt);
    201  1.58  jdolecek 	if ((u_int) blkcnt > MARKV_MAXBLKCNT)
    202  1.58  jdolecek 		return (EINVAL);
    203  1.58  jdolecek 
    204  1.57  perseant 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    205  1.57  perseant 	blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
    206  1.57  perseant 	if ((error = copyin(SCARG(uap, blkiov), blkiov15,
    207  1.57  perseant 			    blkcnt * sizeof(BLOCK_INFO_15))) != 0)
    208  1.57  perseant 		goto out;
    209  1.57  perseant 
    210  1.57  perseant 	for (i = 0; i < blkcnt; i++) {
    211  1.57  perseant 		blkiov[i].bi_inode     = blkiov15[i].bi_inode;
    212  1.57  perseant 		blkiov[i].bi_lbn       = blkiov15[i].bi_lbn;
    213  1.57  perseant 		blkiov[i].bi_daddr     = blkiov15[i].bi_daddr;
    214  1.57  perseant 		blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
    215  1.57  perseant 		blkiov[i].bi_version   = blkiov15[i].bi_version;
    216  1.57  perseant 		blkiov[i].bi_bp        = blkiov15[i].bi_bp;
    217  1.57  perseant 		blkiov[i].bi_size      = blkiov15[i].bi_size;
    218  1.57  perseant 	}
    219  1.57  perseant 
    220  1.57  perseant 	if ((error = lfs_markv(p, &fsid, blkiov, blkcnt)) == 0) {
    221  1.57  perseant 		for (i = 0; i < blkcnt; i++) {
    222  1.57  perseant 			blkiov15[i].bi_inode     = blkiov[i].bi_inode;
    223  1.57  perseant 			blkiov15[i].bi_lbn       = blkiov[i].bi_lbn;
    224  1.57  perseant 			blkiov15[i].bi_daddr     = blkiov[i].bi_daddr;
    225  1.57  perseant 			blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
    226  1.57  perseant 			blkiov15[i].bi_version   = blkiov[i].bi_version;
    227  1.57  perseant 			blkiov15[i].bi_bp        = blkiov[i].bi_bp;
    228  1.57  perseant 			blkiov15[i].bi_size      = blkiov[i].bi_size;
    229  1.57  perseant 		}
    230  1.57  perseant 		copyout(blkiov15, SCARG(uap, blkiov),
    231  1.57  perseant 			blkcnt * sizeof(BLOCK_INFO_15));
    232  1.57  perseant 	}
    233  1.57  perseant     out:
    234  1.57  perseant 	free(blkiov, M_SEGMENT);
    235  1.57  perseant 	free(blkiov15, M_SEGMENT);
    236  1.57  perseant 	return error;
    237  1.57  perseant }
    238  1.57  perseant #endif
    239  1.57  perseant 
    240  1.57  perseant static int
    241  1.57  perseant lfs_markv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
    242  1.57  perseant {
    243   1.1   mycroft 	BLOCK_INFO *blkp;
    244   1.1   mycroft 	IFILE *ifp;
    245  1.22  perseant 	struct buf *bp, *nbp;
    246  1.10  christos 	struct inode *ip = NULL;
    247   1.1   mycroft 	struct lfs *fs;
    248   1.1   mycroft 	struct mount *mntp;
    249   1.1   mycroft 	struct vnode *vp;
    250  1.22  perseant #ifdef DEBUG_LFS
    251  1.62       chs 	int vputc = 0, iwritten = 0;
    252  1.22  perseant #endif
    253   1.1   mycroft 	ino_t lastino;
    254  1.16      fvdl 	ufs_daddr_t b_daddr, v_daddr;
    255  1.57  perseant 	int cnt, error, lfs_fastvget_unlock;
    256  1.62       chs 	int do_again = 0;
    257  1.22  perseant 	int s;
    258  1.22  perseant #ifdef CHECK_COPYIN
    259  1.22  perseant 	int i;
    260  1.22  perseant #endif /* CHECK_COPYIN */
    261  1.62       chs 	int numlocked = 0, numrefed = 0;
    262  1.49  perseant 	ino_t maxino;
    263   1.1   mycroft 
    264  1.57  perseant 	if ((mntp = vfs_getvfs(fsidp)) == NULL)
    265  1.53  perseant 		return (ENOENT);
    266   1.1   mycroft 
    267  1.22  perseant 	fs = VFSTOUFS(mntp)->um_lfs;
    268  1.57  perseant 	maxino = (fragstoblks(fs, fsbtofrags(fs, VTOI(fs->lfs_ivnode)->i_ffs_blocks)) -
    269  1.49  perseant 		      fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    270  1.49  perseant 
    271  1.57  perseant 	cnt = blkcnt;
    272  1.22  perseant 
    273  1.53  perseant 	if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
    274  1.53  perseant 		return (error);
    275  1.53  perseant 
    276  1.22  perseant 	/*
    277  1.22  perseant 	 * This seglock is just to prevent the fact that we might have to sleep
    278  1.22  perseant 	 * from allowing the possibility that our blocks might become
    279  1.22  perseant 	 * invalid.
    280  1.22  perseant 	 *
    281  1.22  perseant 	 * It is also important to note here that unless we specify SEGM_CKP,
    282  1.22  perseant 	 * any Ifile blocks that we might be asked to clean will never get
    283  1.22  perseant 	 * to the disk.
    284  1.22  perseant 	 */
    285  1.67  perseant 	lfs_seglock(fs, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
    286  1.22  perseant 
    287   1.1   mycroft 	/* Mark blocks/inodes dirty.  */
    288   1.1   mycroft 	error = 0;
    289   1.1   mycroft 
    290  1.22  perseant #ifdef DEBUG_LFS
    291  1.22  perseant 	/* Run through and count the inodes */
    292  1.22  perseant 	lastino = LFS_UNUSED_INUM;
    293  1.62       chs 	for (blkp = blkiov; cnt--; ++blkp) {
    294  1.62       chs 		if (lastino != blkp->bi_inode) {
    295  1.22  perseant 			lastino = blkp->bi_inode;
    296  1.22  perseant 			vputc++;
    297  1.22  perseant 		}
    298  1.22  perseant 	}
    299  1.57  perseant 	cnt = blkcnt;
    300  1.22  perseant 	printf("[%d/",vputc);
    301  1.62       chs 	iwritten = 0;
    302  1.22  perseant #endif /* DEBUG_LFS */
    303  1.22  perseant 	/* these were inside the initialization for the for loop */
    304  1.22  perseant 	v_daddr = LFS_UNUSED_DADDR;
    305  1.22  perseant 	lastino = LFS_UNUSED_INUM;
    306  1.57  perseant 	for (blkp = blkiov; cnt--; ++blkp)
    307  1.22  perseant 	{
    308  1.62       chs 		if (blkp->bi_daddr == LFS_FORCE_WRITE)
    309  1.29  perseant 			printf("lfs_markv: warning: force-writing ino %d lbn %d\n",
    310  1.29  perseant 			       blkp->bi_inode, blkp->bi_lbn);
    311  1.49  perseant 		/* Bounds-check incoming data, avoid panic for failed VGET */
    312  1.49  perseant 		if (blkp->bi_inode <= 0 || blkp->bi_inode >= maxino) {
    313  1.49  perseant 			error = EINVAL;
    314  1.49  perseant 			goto again;
    315  1.49  perseant 		}
    316   1.1   mycroft 		/*
    317   1.1   mycroft 		 * Get the IFILE entry (only once) and see if the file still
    318   1.1   mycroft 		 * exists.
    319   1.1   mycroft 		 */
    320   1.1   mycroft 		if (lastino != blkp->bi_inode) {
    321  1.22  perseant 			/*
    322  1.22  perseant 			 * Finish the old file, if there was one.  The presence
    323  1.22  perseant 			 * of a usable vnode in vp is signaled by a valid v_daddr.
    324  1.22  perseant 			 */
    325  1.62       chs 			if (v_daddr != LFS_UNUSED_DADDR) {
    326  1.30  perseant #ifdef DEBUG_LFS
    327  1.62       chs 				if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
    328  1.22  perseant 					iwritten++;
    329  1.22  perseant #endif
    330  1.62       chs 				if (lfs_fastvget_unlock) {
    331  1.43  perseant 					VOP_UNLOCK(vp, 0);
    332  1.22  perseant 					numlocked--;
    333   1.1   mycroft 				}
    334   1.1   mycroft 				lfs_vunref(vp);
    335  1.22  perseant 				numrefed--;
    336   1.1   mycroft 			}
    337   1.1   mycroft 
    338  1.22  perseant 			/*
    339  1.22  perseant 			 * Start a new file
    340  1.22  perseant 			 */
    341   1.1   mycroft 			lastino = blkp->bi_inode;
    342   1.1   mycroft 			if (blkp->bi_inode == LFS_IFILE_INUM)
    343   1.1   mycroft 				v_daddr = fs->lfs_idaddr;
    344   1.1   mycroft 			else {
    345   1.1   mycroft 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    346  1.22  perseant 				/* XXX fix for force write */
    347   1.1   mycroft 				v_daddr = ifp->if_daddr;
    348   1.1   mycroft 				brelse(bp);
    349   1.1   mycroft 			}
    350  1.22  perseant 			/* Don't force-write the ifile */
    351  1.22  perseant 			if (blkp->bi_inode == LFS_IFILE_INUM
    352  1.22  perseant 			    && blkp->bi_daddr == LFS_FORCE_WRITE)
    353  1.22  perseant 			{
    354  1.22  perseant 				continue;
    355  1.22  perseant 			}
    356  1.35  perseant 			if (v_daddr == LFS_UNUSED_DADDR
    357  1.35  perseant 			    && blkp->bi_daddr != LFS_FORCE_WRITE)
    358  1.22  perseant 			{
    359   1.1   mycroft 				continue;
    360  1.22  perseant 			}
    361   1.1   mycroft 
    362   1.1   mycroft 			/* Get the vnode/inode. */
    363  1.62       chs 			error = lfs_fastvget(mntp, blkp->bi_inode, v_daddr,
    364  1.22  perseant 					   &vp,
    365  1.62       chs 					   (blkp->bi_lbn == LFS_UNUSED_LBN
    366  1.22  perseant 					    ? blkp->bi_bp
    367  1.22  perseant 					    : NULL),
    368  1.22  perseant 					   &lfs_fastvget_unlock);
    369  1.62       chs 			if (lfs_fastvget_unlock)
    370  1.22  perseant 				numlocked++;
    371  1.22  perseant 
    372  1.62       chs 			if (!error) {
    373  1.22  perseant 				numrefed++;
    374  1.22  perseant 			}
    375  1.62       chs 			if (error) {
    376  1.43  perseant #ifdef DEBUG_LFS
    377  1.29  perseant 				printf("lfs_markv: lfs_fastvget failed with %d (ino %d, segment %d)\n",
    378  1.22  perseant 				       error, blkp->bi_inode,
    379  1.57  perseant 				       dtosn(fs, blkp->bi_daddr));
    380  1.43  perseant #endif /* DEBUG_LFS */
    381  1.22  perseant 				/*
    382  1.22  perseant 				 * If we got EAGAIN, that means that the
    383  1.22  perseant 				 * Inode was locked.  This is
    384  1.22  perseant 				 * recoverable: just clean the rest of
    385  1.22  perseant 				 * this segment, and let the cleaner try
    386  1.22  perseant 				 * again with another.  (When the
    387  1.22  perseant 				 * cleaner runs again, this segment will
    388  1.22  perseant 				 * sort high on the list, since it is
    389  1.22  perseant 				 * now almost entirely empty.) But, we
    390  1.22  perseant 				 * still set v_daddr = LFS_UNUSED_ADDR
    391  1.22  perseant 				 * so as not to test this over and over
    392  1.22  perseant 				 * again.
    393  1.22  perseant 				 */
    394  1.62       chs 				if (error == EAGAIN) {
    395  1.22  perseant 					error = 0;
    396  1.22  perseant 					do_again++;
    397  1.22  perseant 				}
    398  1.22  perseant #ifdef DIAGNOSTIC
    399  1.62       chs 				else if (error != ENOENT)
    400  1.22  perseant 					panic("lfs_markv VFS_VGET FAILED");
    401   1.1   mycroft #endif
    402  1.22  perseant 				/* lastino = LFS_UNUSED_INUM; */
    403   1.1   mycroft 				v_daddr = LFS_UNUSED_DADDR;
    404  1.22  perseant 				vp = NULL;
    405  1.22  perseant 				ip = NULL;
    406   1.1   mycroft 				continue;
    407  1.19        pk 			}
    408   1.1   mycroft 			ip = VTOI(vp);
    409  1.22  perseant 		} else if (v_daddr == LFS_UNUSED_DADDR) {
    410  1.22  perseant 			/*
    411  1.22  perseant 			 * This can only happen if the vnode is dead (or
    412  1.22  perseant 			 * in any case we can't get it...e.g., it is
    413  1.22  perseant 			 * inlocked).  Keep going.
    414  1.22  perseant 			 */
    415   1.1   mycroft 			continue;
    416  1.22  perseant 		}
    417  1.22  perseant 
    418  1.22  perseant 		/* Past this point we are guaranteed that vp, ip are valid. */
    419   1.1   mycroft 
    420   1.1   mycroft 		/* If this BLOCK_INFO didn't contain a block, keep going. */
    421  1.22  perseant 		if (blkp->bi_lbn == LFS_UNUSED_LBN) {
    422  1.22  perseant 			/* XXX need to make sure that the inode gets written in this case */
    423  1.22  perseant 			/* XXX but only write the inode if it's the right one */
    424  1.53  perseant 			if (blkp->bi_inode != LFS_IFILE_INUM) {
    425  1.53  perseant 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    426  1.62       chs 				if (ifp->if_daddr == blkp->bi_daddr
    427  1.22  perseant 				   || blkp->bi_daddr == LFS_FORCE_WRITE)
    428  1.22  perseant 				{
    429  1.47  perseant 					LFS_SET_UINO(ip, IN_CLEANING);
    430  1.22  perseant 				}
    431  1.53  perseant 				brelse(bp);
    432  1.53  perseant 			}
    433   1.1   mycroft 			continue;
    434  1.22  perseant 		}
    435  1.22  perseant 
    436  1.22  perseant 		b_daddr = 0;
    437  1.62       chs 		if (blkp->bi_daddr != LFS_FORCE_WRITE) {
    438  1.22  perseant 			if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
    439  1.57  perseant 			    dbtofsb(fs, b_daddr) != blkp->bi_daddr)
    440  1.22  perseant 			{
    441  1.62       chs 				if (dtosn(fs,dbtofsb(fs, b_daddr))
    442  1.57  perseant 				   == dtosn(fs,blkp->bi_daddr))
    443  1.22  perseant 				{
    444  1.29  perseant 					printf("lfs_markv: wrong da same seg: %x vs %x\n",
    445  1.57  perseant 					       blkp->bi_daddr, dbtofsb(fs, b_daddr));
    446  1.22  perseant 				}
    447  1.67  perseant 				do_again++;
    448  1.22  perseant 				continue;
    449  1.22  perseant 			}
    450  1.22  perseant 		}
    451  1.22  perseant 		/*
    452  1.22  perseant 		 * If we got to here, then we are keeping the block.  If
    453  1.22  perseant 		 * it is an indirect block, we want to actually put it
    454  1.22  perseant 		 * in the buffer cache so that it can be updated in the
    455  1.22  perseant 		 * finish_meta section.  If it's not, we need to
    456  1.22  perseant 		 * allocate a fake buffer so that writeseg can perform
    457  1.22  perseant 		 * the copyin and write the buffer.
    458  1.22  perseant 		 */
    459  1.22  perseant 		/*
    460  1.22  perseant 		 * XXX - if the block we are reading has been *extended* since
    461  1.22  perseant 		 * it was written to disk, then we risk throwing away
    462  1.22  perseant 		 * the extension in bread()/getblk().  Check the size
    463  1.22  perseant 		 * here.
    464  1.22  perseant 		 */
    465  1.62       chs 		if (blkp->bi_size < fs->lfs_bsize) {
    466  1.22  perseant 			s = splbio();
    467  1.22  perseant 			bp = incore(vp, blkp->bi_lbn);
    468  1.62       chs 			if (bp && bp->b_bcount > blkp->bi_size) {
    469  1.67  perseant 				splx(s);
    470  1.67  perseant 				printf("lfs_markv: ino %d lbn %d fragment size changed (%ld > %d), try again\n",
    471  1.67  perseant 					blkp->bi_inode, blkp->bi_lbn,
    472  1.67  perseant 					bp->b_bcount, blkp->bi_size);
    473  1.67  perseant 				do_again++;
    474  1.67  perseant 				continue;
    475  1.67  perseant 				/* blkp->bi_size = bp->b_bcount; */
    476  1.22  perseant 			}
    477  1.22  perseant 			splx(s);
    478  1.22  perseant 		}
    479  1.38  perseant 		if (ip->i_number != LFS_IFILE_INUM && blkp->bi_lbn >= 0) {
    480  1.38  perseant 			/* Data Block */
    481  1.65  perseant 			bp = lfs_fakebuf(fs, vp, blkp->bi_lbn,
    482  1.23  perseant 					 blkp->bi_size, blkp->bi_bp);
    483  1.23  perseant 			/* Pretend we used bread() to get it */
    484  1.57  perseant 			bp->b_blkno = fsbtodb(fs, blkp->bi_daddr);
    485  1.38  perseant 		} else {
    486  1.38  perseant 			/* Indirect block */
    487  1.22  perseant 			bp = getblk(vp, blkp->bi_lbn, blkp->bi_size, 0, 0);
    488  1.22  perseant 			if (!(bp->b_flags & (B_DONE|B_DELWRI))) { /* B_CACHE */
    489  1.22  perseant 				/*
    490  1.22  perseant 				 * The block in question was not found
    491  1.22  perseant 				 * in the cache; i.e., the block that
    492  1.22  perseant 				 * getblk() returned is empty.  So, we
    493  1.22  perseant 				 * can (and should) copy in the
    494  1.22  perseant 				 * contents, because we've already
    495  1.22  perseant 				 * determined that this was the right
    496  1.22  perseant 				 * version of this block on disk.
    497  1.22  perseant 				 *
    498  1.22  perseant 				 * And, it can't have changed underneath
    499  1.22  perseant 				 * us, because we have the segment lock.
    500  1.22  perseant 				 */
    501  1.22  perseant 				error = copyin(blkp->bi_bp, bp->b_data, blkp->bi_size);
    502  1.62       chs 				if (error)
    503  1.22  perseant 					goto err2;
    504  1.22  perseant 			}
    505  1.22  perseant 		}
    506  1.22  perseant 		if ((error = lfs_bwrite_ext(bp,BW_CLEAN)) != 0)
    507  1.22  perseant 			goto err2;
    508  1.22  perseant 	}
    509  1.22  perseant 
    510  1.22  perseant 	/*
    511  1.22  perseant 	 * Finish the old file, if there was one
    512  1.22  perseant 	 */
    513  1.62       chs 	if (v_daddr != LFS_UNUSED_DADDR) {
    514  1.22  perseant #ifdef DEBUG_LFS
    515  1.62       chs 		if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
    516  1.22  perseant 			iwritten++;
    517  1.22  perseant #endif
    518  1.62       chs 		if (lfs_fastvget_unlock) {
    519  1.43  perseant 			VOP_UNLOCK(vp, 0);
    520  1.22  perseant 			numlocked--;
    521  1.22  perseant 		}
    522  1.22  perseant 		lfs_vunref(vp);
    523  1.22  perseant 		numrefed--;
    524  1.22  perseant 	}
    525  1.22  perseant 
    526  1.22  perseant 	/*
    527  1.22  perseant 	 * The last write has to be SEGM_SYNC, because of calling semantics.
    528  1.22  perseant 	 * It also has to be SEGM_CKP, because otherwise we could write
    529  1.22  perseant 	 * over the newly cleaned data contained in a checkpoint, and then
    530  1.22  perseant 	 * we'd be unhappy at recovery time.
    531  1.22  perseant 	 */
    532  1.67  perseant 	lfs_segwrite(mntp, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
    533  1.22  perseant 
    534   1.1   mycroft 	lfs_segunlock(fs);
    535   1.1   mycroft 
    536  1.22  perseant #ifdef DEBUG_LFS
    537  1.22  perseant 	printf("%d]",iwritten);
    538  1.62       chs 	if (numlocked != 0 || numrefed != 0) {
    539  1.22  perseant 		panic("lfs_markv: numlocked=%d numrefed=%d", numlocked, numrefed);
    540  1.22  perseant 	}
    541  1.22  perseant #endif
    542  1.22  perseant 
    543  1.53  perseant 	vfs_unbusy(mntp);
    544  1.62       chs 	if (error)
    545  1.22  perseant 		return (error);
    546  1.62       chs 	else if (do_again)
    547  1.22  perseant 		return EAGAIN;
    548   1.1   mycroft 
    549  1.22  perseant 	return 0;
    550  1.22  perseant 
    551  1.22  perseant  err2:
    552  1.29  perseant 	printf("lfs_markv err2\n");
    553  1.62       chs 	if (lfs_fastvget_unlock) {
    554  1.53  perseant 		VOP_UNLOCK(vp, 0);
    555  1.53  perseant 		--numlocked;
    556  1.53  perseant 	}
    557  1.22  perseant 	lfs_vunref(vp);
    558  1.53  perseant 	--numrefed;
    559  1.53  perseant 
    560  1.22  perseant 	/* Free up fakebuffers -- have to take these from the LOCKED list */
    561  1.22  perseant  again:
    562  1.37      fvdl 	s = splbio();
    563  1.62       chs 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp; bp = nbp) {
    564  1.22  perseant 		nbp = bp->b_freelist.tqe_next;
    565  1.62       chs 		if (bp->b_flags & B_CALL) {
    566  1.62       chs 			if (bp->b_flags & B_BUSY) { /* not bloody likely */
    567  1.22  perseant 				bp->b_flags |= B_WANTED;
    568  1.22  perseant 				tsleep(bp, PRIBIO+1, "markv", 0);
    569  1.22  perseant 				splx(s);
    570  1.22  perseant 				goto again;
    571  1.22  perseant 			}
    572  1.62       chs 			if (bp->b_flags & B_DELWRI)
    573  1.57  perseant 				fs->lfs_avail += btofsb(fs, bp->b_bcount);
    574  1.22  perseant 			bremfree(bp);
    575  1.22  perseant 			splx(s);
    576  1.22  perseant 			brelse(bp);
    577  1.37      fvdl 			s = splbio();
    578  1.22  perseant 		}
    579  1.22  perseant 	}
    580  1.37      fvdl 	splx(s);
    581   1.1   mycroft 	lfs_segunlock(fs);
    582  1.53  perseant 	vfs_unbusy(mntp);
    583  1.53  perseant #ifdef DEBUG_LFS
    584  1.62       chs 	if (numlocked != 0 || numrefed != 0) {
    585  1.53  perseant 		panic("lfs_markv: numlocked=%d numrefed=%d", numlocked, numrefed);
    586  1.53  perseant 	}
    587  1.53  perseant #endif
    588  1.53  perseant 
    589  1.22  perseant 	return (error);
    590   1.1   mycroft }
    591   1.1   mycroft 
    592   1.1   mycroft /*
    593  1.31  christos  * sys_lfs_bmapv:
    594   1.1   mycroft  *
    595   1.1   mycroft  * This will fill in the current disk address for arrays of blocks.
    596   1.1   mycroft  *
    597   1.1   mycroft  *  0 on success
    598   1.1   mycroft  * -1/errno is return on error.
    599   1.1   mycroft  */
    600  1.57  perseant #ifdef USE_64BIT_SYSCALLS
    601  1.57  perseant int
    602  1.57  perseant sys_lfs_bmapv(struct proc *p, void *v, register_t *retval)
    603  1.57  perseant {
    604  1.57  perseant 	struct sys_lfs_bmapv_args /* {
    605  1.57  perseant 		syscallarg(fsid_t *) fsidp;
    606  1.57  perseant 		syscallarg(struct block_info *) blkiov;
    607  1.57  perseant 		syscallarg(int) blkcnt;
    608  1.57  perseant 	} */ *uap = v;
    609  1.57  perseant 	BLOCK_INFO *blkiov;
    610  1.57  perseant 	int blkcnt, error;
    611  1.57  perseant 	fsid_t fsid;
    612  1.22  perseant 
    613  1.57  perseant 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    614  1.57  perseant 		return (error);
    615  1.57  perseant 
    616  1.57  perseant 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    617  1.57  perseant 		return (error);
    618  1.57  perseant 
    619  1.57  perseant 	blkcnt = SCARG(uap, blkcnt);
    620  1.57  perseant 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    621  1.57  perseant 	if ((error = copyin(SCARG(uap, blkiov), blkiov,
    622  1.57  perseant 			    blkcnt * sizeof(BLOCK_INFO))) != 0)
    623  1.57  perseant 		goto out;
    624  1.57  perseant 
    625  1.57  perseant 	if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0)
    626  1.57  perseant 		copyout(blkiov, SCARG(uap, blkiov),
    627  1.57  perseant 			blkcnt * sizeof(BLOCK_INFO));
    628  1.57  perseant     out:
    629  1.57  perseant 	free(blkiov, M_SEGMENT);
    630  1.57  perseant 	return error;
    631  1.57  perseant }
    632  1.57  perseant #else
    633   1.1   mycroft int
    634  1.57  perseant sys_lfs_bmapv(struct proc *p, void *v, register_t *retval)
    635   1.9   thorpej {
    636  1.32  drochner 	struct sys_lfs_bmapv_args /* {
    637  1.32  drochner 		syscallarg(fsid_t *) fsidp;
    638  1.32  drochner 		syscallarg(struct block_info *) blkiov;
    639  1.32  drochner 		syscallarg(int) blkcnt;
    640  1.32  drochner 	} */ *uap = v;
    641  1.57  perseant 	BLOCK_INFO *blkiov;
    642  1.57  perseant 	BLOCK_INFO_15 *blkiov15;
    643  1.57  perseant 	int i, blkcnt, error;
    644  1.57  perseant 	fsid_t fsid;
    645  1.57  perseant 
    646  1.57  perseant 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    647  1.57  perseant 		return (error);
    648  1.57  perseant 
    649  1.57  perseant 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    650  1.57  perseant 		return (error);
    651  1.57  perseant 
    652  1.57  perseant 	blkcnt = SCARG(uap, blkcnt);
    653  1.57  perseant 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    654  1.57  perseant 	blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
    655  1.57  perseant 	if ((error = copyin(SCARG(uap, blkiov), blkiov15,
    656  1.57  perseant 			    blkcnt * sizeof(BLOCK_INFO_15))) != 0)
    657  1.57  perseant 		goto out;
    658  1.57  perseant 
    659  1.57  perseant 	for (i = 0; i < blkcnt; i++) {
    660  1.57  perseant 		blkiov[i].bi_inode     = blkiov15[i].bi_inode;
    661  1.57  perseant 		blkiov[i].bi_lbn       = blkiov15[i].bi_lbn;
    662  1.57  perseant 		blkiov[i].bi_daddr     = blkiov15[i].bi_daddr;
    663  1.57  perseant 		blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
    664  1.57  perseant 		blkiov[i].bi_version   = blkiov15[i].bi_version;
    665  1.57  perseant 		blkiov[i].bi_bp        = blkiov15[i].bi_bp;
    666  1.57  perseant 		blkiov[i].bi_size      = blkiov15[i].bi_size;
    667  1.57  perseant 	}
    668  1.57  perseant 
    669  1.57  perseant 	if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0) {
    670  1.57  perseant 		for (i = 0; i < blkcnt; i++) {
    671  1.57  perseant 			blkiov15[i].bi_inode     = blkiov[i].bi_inode;
    672  1.57  perseant 			blkiov15[i].bi_lbn       = blkiov[i].bi_lbn;
    673  1.57  perseant 			blkiov15[i].bi_daddr     = blkiov[i].bi_daddr;
    674  1.57  perseant 			blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
    675  1.57  perseant 			blkiov15[i].bi_version   = blkiov[i].bi_version;
    676  1.57  perseant 			blkiov15[i].bi_bp        = blkiov[i].bi_bp;
    677  1.57  perseant 			blkiov15[i].bi_size      = blkiov[i].bi_size;
    678  1.57  perseant 		}
    679  1.57  perseant 		copyout(blkiov15, SCARG(uap, blkiov),
    680  1.57  perseant 			blkcnt * sizeof(BLOCK_INFO_15));
    681  1.57  perseant 	}
    682  1.57  perseant     out:
    683  1.57  perseant 	free(blkiov, M_SEGMENT);
    684  1.57  perseant 	free(blkiov15, M_SEGMENT);
    685  1.57  perseant 	return error;
    686  1.57  perseant }
    687  1.57  perseant #endif
    688  1.57  perseant 
    689  1.57  perseant static int
    690  1.57  perseant lfs_bmapv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
    691  1.57  perseant {
    692   1.1   mycroft 	BLOCK_INFO *blkp;
    693  1.22  perseant 	IFILE *ifp;
    694  1.22  perseant 	struct buf *bp;
    695  1.22  perseant 	struct inode *ip = NULL;
    696  1.22  perseant 	struct lfs *fs;
    697   1.1   mycroft 	struct mount *mntp;
    698  1.16      fvdl 	struct ufsmount *ump;
    699   1.1   mycroft 	struct vnode *vp;
    700  1.22  perseant 	ino_t lastino;
    701  1.22  perseant 	ufs_daddr_t v_daddr;
    702  1.62       chs 	int cnt, error, need_unlock = 0;
    703  1.62       chs 	int numlocked = 0, numrefed = 0;
    704   1.1   mycroft 
    705  1.22  perseant 	lfs_cleaner_pid = p->p_pid;
    706  1.22  perseant 
    707  1.57  perseant 	if ((mntp = vfs_getvfs(fsidp)) == NULL)
    708  1.53  perseant 		return (ENOENT);
    709  1.22  perseant 
    710  1.22  perseant 	ump = VFSTOUFS(mntp);
    711  1.53  perseant 	if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
    712  1.53  perseant 		return (error);
    713  1.22  perseant 
    714  1.57  perseant 	cnt = blkcnt;
    715  1.22  perseant 
    716  1.22  perseant 	fs = VFSTOUFS(mntp)->um_lfs;
    717  1.22  perseant 
    718  1.22  perseant 	error = 0;
    719  1.22  perseant 
    720  1.22  perseant 	/* these were inside the initialization for the for loop */
    721  1.22  perseant 	v_daddr = LFS_UNUSED_DADDR;
    722  1.22  perseant 	lastino = LFS_UNUSED_INUM;
    723  1.57  perseant 	for (blkp = blkiov; cnt--; ++blkp)
    724  1.22  perseant 	{
    725  1.22  perseant #ifdef DEBUG
    726  1.57  perseant 		if (dtosn(fs, fs->lfs_curseg) == dtosn(fs, blkp->bi_daddr)) {
    727  1.29  perseant 			printf("lfs_bmapv: attempt to clean current segment? (#%d)\n",
    728  1.57  perseant 			       dtosn(fs, fs->lfs_curseg));
    729  1.53  perseant 			vfs_unbusy(mntp);
    730  1.22  perseant 			return (EBUSY);
    731  1.22  perseant 		}
    732  1.22  perseant #endif /* DEBUG */
    733  1.16      fvdl 		/*
    734  1.22  perseant 		 * Get the IFILE entry (only once) and see if the file still
    735  1.22  perseant 		 * exists.
    736  1.16      fvdl 		 */
    737  1.22  perseant 		if (lastino != blkp->bi_inode) {
    738  1.22  perseant 			/*
    739  1.22  perseant 			 * Finish the old file, if there was one.  The presence
    740  1.22  perseant 			 * of a usable vnode in vp is signaled by a valid
    741  1.22  perseant 			 * v_daddr.
    742  1.22  perseant 			 */
    743  1.62       chs 			if (v_daddr != LFS_UNUSED_DADDR) {
    744  1.62       chs 				if (need_unlock) {
    745  1.43  perseant 					VOP_UNLOCK(vp, 0);
    746  1.22  perseant 					numlocked--;
    747  1.22  perseant 				}
    748  1.22  perseant 				lfs_vunref(vp);
    749  1.22  perseant 				numrefed--;
    750  1.22  perseant 			}
    751  1.22  perseant 
    752  1.22  perseant 			/*
    753  1.22  perseant 			 * Start a new file
    754  1.22  perseant 			 */
    755  1.22  perseant 			lastino = blkp->bi_inode;
    756  1.22  perseant 			if (blkp->bi_inode == LFS_IFILE_INUM)
    757  1.22  perseant 				v_daddr = fs->lfs_idaddr;
    758  1.22  perseant 			else {
    759  1.22  perseant 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    760  1.22  perseant 				v_daddr = ifp->if_daddr;
    761  1.22  perseant 				brelse(bp);
    762  1.22  perseant 			}
    763  1.22  perseant 			if (v_daddr == LFS_UNUSED_DADDR) {
    764  1.22  perseant 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    765  1.22  perseant 				continue;
    766  1.22  perseant 			}
    767  1.22  perseant 			/*
    768  1.22  perseant 			 * A regular call to VFS_VGET could deadlock
    769  1.22  perseant 			 * here.  Instead, we try an unlocked access.
    770  1.22  perseant 			 */
    771  1.22  perseant 			vp = ufs_ihashlookup(ump->um_dev, blkp->bi_inode);
    772  1.24  perseant 			if (vp != NULL && !(vp->v_flag & VXLOCK)) {
    773  1.22  perseant 				ip = VTOI(vp);
    774  1.42  perseant 				if (lfs_vref(vp)) {
    775  1.42  perseant 					v_daddr = LFS_UNUSED_DADDR;
    776  1.42  perseant 					need_unlock = 0;
    777  1.42  perseant 					continue;
    778  1.42  perseant 				}
    779  1.43  perseant 				numrefed++;
    780  1.62       chs 				if (VOP_ISLOCKED(vp)) {
    781  1.43  perseant #ifdef DEBUG_LFS
    782  1.43  perseant 					printf("lfs_bmapv: inode %d inlocked\n",ip->i_number);
    783  1.43  perseant #endif
    784  1.43  perseant 					v_daddr = LFS_UNUSED_DADDR;
    785  1.22  perseant 					need_unlock = 0;
    786  1.43  perseant 					lfs_vunref(vp);
    787  1.43  perseant 					--numrefed;
    788  1.43  perseant 					continue;
    789  1.22  perseant 				} else {
    790  1.43  perseant 					vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    791  1.22  perseant 					need_unlock = FVG_UNLOCK;
    792  1.22  perseant 					numlocked++;
    793  1.22  perseant 				}
    794  1.22  perseant 			} else {
    795  1.22  perseant 				error = VFS_VGET(mntp, blkp->bi_inode, &vp);
    796  1.62       chs 				if (error) {
    797  1.24  perseant #ifdef DEBUG_LFS
    798  1.25  perseant 					printf("lfs_bmapv: vget of ino %d failed with %d",blkp->bi_inode,error);
    799  1.24  perseant #endif
    800  1.43  perseant 					v_daddr = LFS_UNUSED_DADDR;
    801  1.43  perseant 					need_unlock = 0;
    802  1.22  perseant 					continue;
    803  1.22  perseant 				} else {
    804  1.22  perseant 					need_unlock = FVG_PUT;
    805  1.22  perseant 					numlocked++;
    806  1.22  perseant 					numrefed++;
    807  1.22  perseant 				}
    808  1.22  perseant 			}
    809  1.22  perseant 			ip = VTOI(vp);
    810  1.22  perseant 		} else if (v_daddr == LFS_UNUSED_DADDR) {
    811  1.22  perseant 			/*
    812  1.22  perseant 			 * This can only happen if the vnode is dead.
    813  1.22  perseant 			 * Keep going.  Note that we DO NOT set the
    814  1.22  perseant 			 * bi_addr to anything -- if we failed to get
    815  1.22  perseant 			 * the vnode, for example, we want to assume
    816  1.22  perseant 			 * conservatively that all of its blocks *are*
    817  1.22  perseant 			 * located in the segment in question.
    818  1.22  perseant 			 * lfs_markv will throw them out if we are
    819  1.22  perseant 			 * wrong.
    820  1.22  perseant 			 */
    821  1.22  perseant 			/* blkp->bi_daddr = LFS_UNUSED_DADDR; */
    822  1.22  perseant 			continue;
    823  1.22  perseant 		}
    824  1.22  perseant 
    825  1.22  perseant 		/* Past this point we are guaranteed that vp, ip are valid. */
    826  1.22  perseant 
    827  1.62       chs 		if (blkp->bi_lbn == LFS_UNUSED_LBN) {
    828  1.22  perseant 			/*
    829  1.22  perseant 			 * We just want the inode address, which is
    830  1.22  perseant 			 * conveniently in v_daddr.
    831  1.22  perseant 			 */
    832  1.22  perseant 			blkp->bi_daddr = v_daddr;
    833  1.22  perseant 		} else {
    834  1.22  perseant 			error = VOP_BMAP(vp, blkp->bi_lbn, NULL,
    835  1.22  perseant 					 &(blkp->bi_daddr), NULL);
    836  1.62       chs 			if (error)
    837  1.22  perseant 			{
    838  1.22  perseant 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    839  1.22  perseant 				continue;
    840  1.22  perseant 			}
    841  1.57  perseant 			blkp->bi_daddr = dbtofsb(fs, blkp->bi_daddr);
    842  1.66  perseant 			/* Fill in the block size, too */
    843  1.66  perseant 			blkp->bi_size = blksize(fs, ip, blkp->bi_lbn);
    844  1.22  perseant 		}
    845  1.22  perseant 	}
    846  1.22  perseant 
    847  1.22  perseant 	/*
    848  1.22  perseant 	 * Finish the old file, if there was one.  The presence
    849  1.22  perseant 	 * of a usable vnode in vp is signaled by a valid v_daddr.
    850  1.22  perseant 	 */
    851  1.62       chs 	if (v_daddr != LFS_UNUSED_DADDR) {
    852  1.62       chs 		if (need_unlock) {
    853  1.43  perseant 			VOP_UNLOCK(vp, 0);
    854  1.22  perseant 			numlocked--;
    855  1.22  perseant 		}
    856  1.22  perseant 		lfs_vunref(vp);
    857  1.22  perseant 		numrefed--;
    858  1.22  perseant 	}
    859  1.22  perseant 
    860  1.62       chs 	if (numlocked != 0 || numrefed != 0) {
    861  1.22  perseant 		panic("lfs_bmapv: numlocked=%d numrefed=%d", numlocked,
    862  1.22  perseant 		      numrefed);
    863  1.22  perseant 	}
    864  1.22  perseant 
    865  1.53  perseant 	vfs_unbusy(mntp);
    866  1.22  perseant 
    867  1.22  perseant 	return 0;
    868   1.1   mycroft }
    869   1.1   mycroft 
    870   1.1   mycroft /*
    871  1.31  christos  * sys_lfs_segclean:
    872   1.1   mycroft  *
    873   1.1   mycroft  * Mark the segment clean.
    874   1.1   mycroft  *
    875   1.1   mycroft  *  0 on success
    876   1.1   mycroft  * -1/errno is return on error.
    877   1.1   mycroft  */
    878   1.1   mycroft int
    879  1.57  perseant sys_lfs_segclean(struct proc *p, void *v, register_t *retval)
    880   1.9   thorpej {
    881  1.32  drochner 	struct sys_lfs_segclean_args /* {
    882  1.32  drochner 		syscallarg(fsid_t *) fsidp;
    883  1.32  drochner 		syscallarg(u_long) segment;
    884  1.32  drochner 	} */ *uap = v;
    885   1.1   mycroft 	CLEANERINFO *cip;
    886   1.1   mycroft 	SEGUSE *sup;
    887   1.1   mycroft 	struct buf *bp;
    888   1.1   mycroft 	struct mount *mntp;
    889   1.1   mycroft 	struct lfs *fs;
    890   1.1   mycroft 	fsid_t fsid;
    891   1.1   mycroft 	int error;
    892  1.67  perseant 	unsigned long segnum;
    893  1.22  perseant 
    894  1.10  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    895   1.1   mycroft 		return (error);
    896  1.22  perseant 
    897  1.10  christos 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    898   1.1   mycroft 		return (error);
    899  1.16      fvdl 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    900  1.53  perseant 		return (ENOENT);
    901  1.22  perseant 
    902   1.1   mycroft 	fs = VFSTOUFS(mntp)->um_lfs;
    903  1.67  perseant 	segnum = SCARG(uap, segment);
    904  1.22  perseant 
    905  1.67  perseant 	if (dtosn(fs, fs->lfs_curseg) == segnum)
    906   1.1   mycroft 		return (EBUSY);
    907  1.22  perseant 
    908  1.53  perseant 	if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
    909  1.53  perseant 		return (error);
    910  1.65  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    911  1.65  perseant 	lfs_seglock(fs, SEGM_PROT);
    912  1.65  perseant #endif
    913  1.67  perseant 	LFS_SEGENTRY(sup, fs, segnum, bp);
    914  1.67  perseant 	if (sup->su_nbytes) {
    915  1.67  perseant 		printf("lfs_segclean: not cleaning segment %lu: %d live bytes\n",
    916  1.67  perseant 			segnum, sup->su_nbytes);
    917  1.67  perseant 		brelse(bp);
    918  1.67  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    919  1.67  perseant 		lfs_segunlock(fs);
    920  1.67  perseant #endif
    921  1.67  perseant 		vfs_unbusy(mntp);
    922  1.67  perseant 		return (EBUSY);
    923  1.67  perseant 	}
    924   1.1   mycroft 	if (sup->su_flags & SEGUSE_ACTIVE) {
    925   1.1   mycroft 		brelse(bp);
    926  1.65  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    927  1.65  perseant 		lfs_segunlock(fs);
    928  1.65  perseant #endif
    929  1.53  perseant 		vfs_unbusy(mntp);
    930   1.1   mycroft 		return (EBUSY);
    931  1.50  perseant 	}
    932  1.50  perseant 	if (!(sup->su_flags & SEGUSE_DIRTY)) {
    933  1.50  perseant 		brelse(bp);
    934  1.65  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    935  1.65  perseant 		lfs_segunlock(fs);
    936  1.65  perseant #endif
    937  1.53  perseant 		vfs_unbusy(mntp);
    938  1.50  perseant 		return (EALREADY);
    939   1.1   mycroft 	}
    940  1.22  perseant 
    941  1.57  perseant 	fs->lfs_avail += segtod(fs, 1);
    942  1.46  perseant 	if (sup->su_flags & SEGUSE_SUPERBLOCK)
    943  1.57  perseant 		fs->lfs_avail -= btofsb(fs, LFS_SBPAD);
    944  1.67  perseant 	if (fs->lfs_version > 1 && segnum == 0 &&
    945  1.57  perseant 	    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
    946  1.57  perseant 		fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
    947  1.57  perseant 	fs->lfs_bfree += sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
    948  1.57  perseant 		btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    949  1.57  perseant 	fs->lfs_dmeta -= sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
    950  1.57  perseant 		btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    951  1.43  perseant 	if (fs->lfs_dmeta < 0)
    952  1.43  perseant 		fs->lfs_dmeta = 0;
    953   1.1   mycroft 	sup->su_flags &= ~SEGUSE_DIRTY;
    954  1.65  perseant 	(void) LFS_BWRITE_LOG(bp);
    955  1.22  perseant 
    956   1.1   mycroft 	LFS_CLEANERINFO(cip, fs, bp);
    957   1.1   mycroft 	++cip->clean;
    958   1.1   mycroft 	--cip->dirty;
    959  1.22  perseant 	fs->lfs_nclean = cip->clean;
    960  1.49  perseant 	cip->bfree = fs->lfs_bfree;
    961  1.49  perseant 	cip->avail = fs->lfs_avail - fs->lfs_ravail;
    962  1.65  perseant 	(void) LFS_BWRITE_LOG(bp);
    963   1.1   mycroft 	wakeup(&fs->lfs_avail);
    964  1.65  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    965  1.65  perseant 	lfs_segunlock(fs);
    966  1.65  perseant #endif
    967  1.53  perseant 	vfs_unbusy(mntp);
    968  1.22  perseant 
    969   1.1   mycroft 	return (0);
    970   1.1   mycroft }
    971   1.1   mycroft 
    972   1.1   mycroft /*
    973  1.31  christos  * sys_lfs_segwait:
    974   1.1   mycroft  *
    975   1.1   mycroft  * This will block until a segment in file system fsid is written.  A timeout
    976   1.1   mycroft  * in milliseconds may be specified which will awake the cleaner automatically.
    977   1.1   mycroft  * An fsid of -1 means any file system, and a timeout of 0 means forever.
    978   1.1   mycroft  *
    979   1.1   mycroft  *  0 on success
    980   1.1   mycroft  *  1 on timeout
    981   1.1   mycroft  * -1/errno is return on error.
    982   1.1   mycroft  */
    983   1.1   mycroft int
    984  1.57  perseant sys_lfs_segwait(struct proc *p, void *v, register_t *retval)
    985   1.9   thorpej {
    986  1.32  drochner 	struct sys_lfs_segwait_args /* {
    987  1.32  drochner 		syscallarg(fsid_t *) fsidp;
    988  1.32  drochner 		syscallarg(struct timeval *) tv;
    989  1.32  drochner 	} */ *uap = v;
    990   1.1   mycroft 	struct mount *mntp;
    991   1.1   mycroft 	struct timeval atv;
    992   1.1   mycroft 	fsid_t fsid;
    993   1.1   mycroft 	void *addr;
    994   1.1   mycroft 	u_long timeout;
    995   1.1   mycroft 	int error, s;
    996  1.22  perseant 
    997  1.10  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    998   1.1   mycroft 		return (error);
    999   1.1   mycroft 	}
   1000  1.10  christos 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
   1001   1.1   mycroft 		return (error);
   1002  1.16      fvdl 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
   1003   1.1   mycroft 		addr = &lfs_allclean_wakeup;
   1004   1.1   mycroft 	else
   1005   1.1   mycroft 		addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
   1006  1.22  perseant 
   1007   1.5       cgd 	if (SCARG(uap, tv)) {
   1008  1.10  christos 		error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
   1009  1.10  christos 		if (error)
   1010   1.1   mycroft 			return (error);
   1011   1.1   mycroft 		if (itimerfix(&atv))
   1012   1.1   mycroft 			return (EINVAL);
   1013  1.48   thorpej 		/*
   1014  1.48   thorpej 		 * XXX THIS COULD SLEEP FOREVER IF TIMEOUT IS {0,0}!
   1015  1.48   thorpej 		 * XXX IS THAT WHAT IS INTENDED?
   1016  1.48   thorpej 		 */
   1017   1.1   mycroft 		s = splclock();
   1018   1.8   mycroft 		timeradd(&atv, &time, &atv);
   1019   1.1   mycroft 		timeout = hzto(&atv);
   1020   1.1   mycroft 		splx(s);
   1021   1.1   mycroft 	} else
   1022   1.1   mycroft 		timeout = 0;
   1023  1.22  perseant 
   1024   1.1   mycroft 	error = tsleep(addr, PCATCH | PUSER, "segment", timeout);
   1025   1.1   mycroft 	return (error == ERESTART ? EINTR : 0);
   1026   1.1   mycroft }
   1027   1.1   mycroft 
   1028   1.1   mycroft /*
   1029   1.1   mycroft  * VFS_VGET call specialized for the cleaner.  The cleaner already knows the
   1030   1.1   mycroft  * daddr from the ifile, so don't look it up again.  If the cleaner is
   1031   1.1   mycroft  * processing IINFO structures, it may have the ondisk inode already, so
   1032   1.1   mycroft  * don't go retrieving it again.
   1033  1.22  perseant  *
   1034  1.22  perseant  * If we find the vnode on the hash chain, then it may be locked by another
   1035  1.22  perseant  * process; so we set (*need_unlock) to zero.
   1036  1.22  perseant  *
   1037  1.22  perseant  * If we don't, we call ufs_ihashins, which locks the inode, and we set
   1038  1.22  perseant  * (*need_unlock) to non-zero.
   1039  1.22  perseant  *
   1040  1.22  perseant  * In either case we lfs_vref, and it is the caller's responsibility to
   1041  1.22  perseant  * lfs_vunref and VOP_UNLOCK (if necessary) when finished.
   1042   1.1   mycroft  */
   1043  1.22  perseant extern struct lock ufs_hashlock;
   1044  1.22  perseant 
   1045   1.1   mycroft int
   1046  1.57  perseant lfs_fasthashget(dev_t dev, ino_t ino, int *need_unlock, struct vnode **vpp)
   1047  1.44      fvdl {
   1048  1.44      fvdl 	struct inode *ip;
   1049  1.44      fvdl 
   1050  1.44      fvdl 	/*
   1051  1.44      fvdl 	 * This is playing fast and loose.  Someone may have the inode
   1052  1.44      fvdl 	 * locked, in which case they are going to be distinctly unhappy
   1053  1.44      fvdl 	 * if we trash something.
   1054  1.44      fvdl 	 */
   1055  1.44      fvdl 	if ((*vpp = ufs_ihashlookup(dev, ino)) != NULL) {
   1056  1.44      fvdl 		if ((*vpp)->v_flag & VXLOCK) {
   1057  1.46  perseant 			printf("lfs_fastvget: vnode VXLOCKed for ino %d\n",
   1058  1.46  perseant 			       ino);
   1059  1.44      fvdl 			clean_vnlocked++;
   1060  1.44      fvdl #ifdef LFS_EAGAIN_FAIL
   1061  1.44      fvdl 			return EAGAIN;
   1062  1.44      fvdl #endif
   1063  1.44      fvdl 		}
   1064  1.44      fvdl 		ip = VTOI(*vpp);
   1065  1.44      fvdl 		if (lfs_vref(*vpp)) {
   1066  1.44      fvdl 			clean_inlocked++;
   1067  1.44      fvdl 			return EAGAIN;
   1068  1.44      fvdl 		}
   1069  1.44      fvdl 		if (VOP_ISLOCKED(*vpp)) {
   1070  1.44      fvdl #ifdef DEBUG_LFS
   1071  1.44      fvdl 			printf("lfs_fastvget: ino %d inlocked by pid %d\n",
   1072  1.46  perseant 			       ip->i_number, (*vpp)->v_lock.lk_lockholder);
   1073  1.44      fvdl #endif
   1074  1.44      fvdl 			clean_inlocked++;
   1075  1.44      fvdl #ifdef LFS_EAGAIN_FAIL
   1076  1.44      fvdl 			lfs_vunref(*vpp);
   1077  1.44      fvdl 			return EAGAIN;
   1078  1.44      fvdl #endif /* LFS_EAGAIN_FAIL */
   1079  1.44      fvdl 		} else {
   1080  1.44      fvdl 			vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
   1081  1.44      fvdl 			*need_unlock |= FVG_UNLOCK;
   1082  1.44      fvdl 		}
   1083  1.44      fvdl 	} else
   1084  1.44      fvdl 		*vpp = NULL;
   1085  1.44      fvdl 
   1086  1.44      fvdl 	return (0);
   1087  1.44      fvdl }
   1088  1.44      fvdl 
   1089  1.44      fvdl int
   1090  1.57  perseant lfs_fastvget(struct mount *mp, ino_t ino, ufs_daddr_t daddr, struct vnode **vpp, struct dinode *dinp, int *need_unlock)
   1091   1.1   mycroft {
   1092  1.41  augustss 	struct inode *ip;
   1093  1.65  perseant 	struct dinode *dip;
   1094   1.1   mycroft 	struct vnode *vp;
   1095   1.1   mycroft 	struct ufsmount *ump;
   1096   1.1   mycroft 	dev_t dev;
   1097  1.65  perseant 	int error, retries;
   1098  1.22  perseant 	struct buf *bp;
   1099  1.57  perseant 	struct lfs *fs;
   1100  1.22  perseant 
   1101   1.1   mycroft 	ump = VFSTOUFS(mp);
   1102   1.1   mycroft 	dev = ump->um_dev;
   1103  1.57  perseant 	fs = ump->um_lfs;
   1104  1.22  perseant 	*need_unlock = 0;
   1105  1.54  perseant 
   1106  1.54  perseant 	/*
   1107  1.54  perseant 	 * Wait until the filesystem is fully mounted before allowing vget
   1108  1.54  perseant 	 * to complete.  This prevents possible problems with roll-forward.
   1109  1.54  perseant 	 */
   1110  1.62       chs 	while (fs->lfs_flags & LFS_NOTYET) {
   1111  1.57  perseant 		tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_fnotyet", 0);
   1112  1.54  perseant 	}
   1113  1.54  perseant 	/*
   1114  1.54  perseant 	 * This is playing fast and loose.  Someone may have the inode
   1115  1.54  perseant 	 * locked, in which case they are going to be distinctly unhappy
   1116  1.54  perseant 	 * if we trash something.
   1117  1.54  perseant 	 */
   1118  1.44      fvdl 
   1119  1.44      fvdl 	error = lfs_fasthashget(dev, ino, need_unlock, vpp);
   1120  1.44      fvdl 	if (error != 0 || *vpp != NULL)
   1121  1.44      fvdl 		return (error);
   1122  1.44      fvdl 
   1123  1.45      fvdl 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
   1124  1.44      fvdl 		*vpp = NULL;
   1125  1.44      fvdl 		return (error);
   1126  1.44      fvdl 	}
   1127  1.44      fvdl 
   1128  1.22  perseant 	do {
   1129  1.44      fvdl 		error = lfs_fasthashget(dev, ino, need_unlock, vpp);
   1130  1.44      fvdl 		if (error != 0 || *vpp != NULL) {
   1131  1.44      fvdl 			ungetnewvnode(vp);
   1132  1.44      fvdl 			return (error);
   1133  1.22  perseant 		}
   1134  1.22  perseant 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1135   1.1   mycroft 
   1136   1.1   mycroft 	/* Allocate new vnode/inode. */
   1137  1.44      fvdl 	lfs_vcreate(mp, ino, vp);
   1138  1.44      fvdl 
   1139   1.1   mycroft 	/*
   1140   1.1   mycroft 	 * Put it onto its hash chain and lock it so that other requests for
   1141   1.1   mycroft 	 * this inode will block if they arrive while we are sleeping waiting
   1142   1.1   mycroft 	 * for old data structures to be purged or for the contents of the
   1143   1.1   mycroft 	 * disk portion of this inode to be read.
   1144   1.1   mycroft 	 */
   1145   1.1   mycroft 	ip = VTOI(vp);
   1146   1.1   mycroft 	ufs_ihashins(ip);
   1147  1.22  perseant 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1148  1.22  perseant 
   1149   1.1   mycroft 	/*
   1150   1.1   mycroft 	 * XXX
   1151   1.1   mycroft 	 * This may not need to be here, logically it should go down with
   1152   1.1   mycroft 	 * the i_devvp initialization.
   1153   1.1   mycroft 	 * Ask Kirk.
   1154   1.1   mycroft 	 */
   1155  1.57  perseant 	ip->i_lfs = fs;
   1156   1.1   mycroft 
   1157   1.1   mycroft 	/* Read in the disk contents for the inode, copy into the inode. */
   1158  1.10  christos 	if (dinp) {
   1159  1.20   thorpej 		error = copyin(dinp, &ip->i_din.ffs_din, DINODE_SIZE);
   1160  1.22  perseant 		if (error) {
   1161  1.24  perseant 			printf("lfs_fastvget: dinode copyin failed for ino %d\n", ino);
   1162  1.22  perseant 			ufs_ihashrem(ip);
   1163  1.22  perseant 
   1164  1.22  perseant 			/* Unlock and discard unneeded inode. */
   1165  1.33  wrstuden 			lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
   1166  1.22  perseant 			lfs_vunref(vp);
   1167  1.22  perseant 			*vpp = NULL;
   1168   1.1   mycroft 			return (error);
   1169  1.22  perseant 		}
   1170  1.62       chs 		if (ip->i_number != ino)
   1171  1.22  perseant 			panic("lfs_fastvget: I was fed the wrong inode!");
   1172  1.22  perseant 	} else {
   1173  1.65  perseant 		retries = 0;
   1174  1.65  perseant 	    again:
   1175  1.57  perseant 		error = bread(ump->um_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
   1176  1.57  perseant 			      NOCRED, &bp);
   1177  1.10  christos 		if (error) {
   1178  1.29  perseant 			printf("lfs_fastvget: bread failed with %d\n",error);
   1179   1.1   mycroft 			/*
   1180   1.1   mycroft 			 * The inode does not contain anything useful, so it
   1181   1.1   mycroft 			 * would be misleading to leave it on its hash chain.
   1182   1.1   mycroft 			 * Iput() will return it to the free list.
   1183   1.1   mycroft 			 */
   1184   1.1   mycroft 			ufs_ihashrem(ip);
   1185  1.22  perseant 
   1186   1.1   mycroft 			/* Unlock and discard unneeded inode. */
   1187  1.33  wrstuden 			lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
   1188   1.1   mycroft 			lfs_vunref(vp);
   1189   1.1   mycroft 			brelse(bp);
   1190   1.1   mycroft 			*vpp = NULL;
   1191   1.1   mycroft 			return (error);
   1192   1.1   mycroft 		}
   1193  1.65  perseant 		dip = lfs_ifind(ump->um_lfs, ino, bp);
   1194  1.65  perseant 		if (dip == NULL) {
   1195  1.65  perseant 			/* Assume write has not completed yet; try again */
   1196  1.65  perseant 			bp->b_flags |= B_INVAL;
   1197  1.65  perseant 			brelse(bp);
   1198  1.65  perseant 			++retries;
   1199  1.65  perseant 			if (retries > LFS_IFIND_RETRIES)
   1200  1.65  perseant 				panic("lfs_fastvget: dinode not found");
   1201  1.65  perseant 			printf("lfs_fastvget: dinode not found, retrying...\n");
   1202  1.65  perseant 			goto again;
   1203  1.65  perseant 		}
   1204  1.65  perseant 		ip->i_din.ffs_din = *dip;
   1205   1.1   mycroft 		brelse(bp);
   1206   1.1   mycroft 	}
   1207  1.36  perseant 	ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1208  1.51    toshii 	ip->i_lfs_effnblks = ip->i_ffs_blocks;
   1209   1.1   mycroft 
   1210   1.1   mycroft 	/*
   1211   1.1   mycroft 	 * Initialize the vnode from the inode, check for aliases.  In all
   1212   1.1   mycroft 	 * cases re-init ip, the underlying vnode/inode may have changed.
   1213   1.1   mycroft 	 */
   1214  1.59       chs 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   1215  1.22  perseant #ifdef DEBUG_LFS
   1216  1.62       chs 	if (vp->v_type == VNON) {
   1217  1.22  perseant 		printf("lfs_fastvget: ino %d is type VNON! (ifmt=%o, dinp=%p)\n",
   1218  1.62       chs 		       ip->i_number, (ip->i_ffs_mode & IFMT) >> 12, dinp);
   1219  1.22  perseant 		lfs_dump_dinode(&ip->i_din.ffs_din);
   1220  1.22  perseant #ifdef DDB
   1221  1.22  perseant 		Debugger();
   1222  1.22  perseant #endif
   1223  1.22  perseant 	}
   1224  1.22  perseant #endif /* DEBUG_LFS */
   1225   1.1   mycroft 	/*
   1226   1.1   mycroft 	 * Finish inode initialization now that aliasing has been resolved.
   1227   1.1   mycroft 	 */
   1228  1.63       chs 
   1229  1.63       chs 	genfs_node_init(vp, &lfs_genfsops);
   1230   1.1   mycroft 	ip->i_devvp = ump->um_devvp;
   1231   1.1   mycroft 	VREF(ip->i_devvp);
   1232   1.1   mycroft 	*vpp = vp;
   1233  1.22  perseant 	*need_unlock |= FVG_PUT;
   1234  1.56  perseant 
   1235  1.56  perseant 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1236  1.22  perseant 
   1237   1.1   mycroft 	return (0);
   1238   1.1   mycroft }
   1239  1.22  perseant 
   1240   1.1   mycroft struct buf *
   1241  1.65  perseant lfs_fakebuf(struct lfs *fs, struct vnode *vp, int lbn, size_t size, caddr_t uaddr)
   1242   1.1   mycroft {
   1243   1.1   mycroft 	struct buf *bp;
   1244  1.25  perseant 	int error;
   1245  1.22  perseant 
   1246  1.25  perseant #ifndef ALLOW_VFLUSH_CORRUPTION
   1247  1.57  perseant 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size);
   1248  1.25  perseant 	error = copyin(uaddr, bp->b_data, size);
   1249  1.62       chs 	if (error) {
   1250  1.25  perseant 		lfs_freebuf(bp);
   1251  1.25  perseant 		return NULL;
   1252  1.22  perseant 	}
   1253  1.25  perseant #else
   1254  1.57  perseant 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, 0);
   1255  1.25  perseant 	bp->b_flags |= B_INVAL;
   1256   1.1   mycroft 	bp->b_saveaddr = uaddr;
   1257  1.25  perseant #endif
   1258  1.65  perseant #if 0
   1259  1.65  perseant 	bp->b_saveaddr = (caddr_t)fs;
   1260  1.65  perseant 	++fs->lfs_iocount;
   1261  1.65  perseant #endif
   1262   1.1   mycroft 	bp->b_bufsize = size;
   1263   1.1   mycroft 	bp->b_bcount = size;
   1264   1.1   mycroft 	return (bp);
   1265   1.1   mycroft }
   1266