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