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