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