Home | History | Annotate | Line # | Download | only in lfs
lfs_syscalls.c revision 1.67
      1 /*	$NetBSD: lfs_syscalls.c,v 1.67 2002/06/16 00:13:16 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.67 2002/06/16 00:13:16 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_CLEAN | SEGM_CKP | SEGM_SYNC);
    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 				do_again++;
    448 				continue;
    449 			}
    450 		}
    451 		/*
    452 		 * If we got to here, then we are keeping the block.  If
    453 		 * it is an indirect block, we want to actually put it
    454 		 * in the buffer cache so that it can be updated in the
    455 		 * finish_meta section.  If it's not, we need to
    456 		 * allocate a fake buffer so that writeseg can perform
    457 		 * the copyin and write the buffer.
    458 		 */
    459 		/*
    460 		 * XXX - if the block we are reading has been *extended* since
    461 		 * it was written to disk, then we risk throwing away
    462 		 * the extension in bread()/getblk().  Check the size
    463 		 * here.
    464 		 */
    465 		if (blkp->bi_size < fs->lfs_bsize) {
    466 			s = splbio();
    467 			bp = incore(vp, blkp->bi_lbn);
    468 			if (bp && bp->b_bcount > blkp->bi_size) {
    469 				splx(s);
    470 				printf("lfs_markv: ino %d lbn %d fragment size changed (%ld > %d), try again\n",
    471 					blkp->bi_inode, blkp->bi_lbn,
    472 					bp->b_bcount, blkp->bi_size);
    473 				do_again++;
    474 				continue;
    475 				/* blkp->bi_size = bp->b_bcount; */
    476 			}
    477 			splx(s);
    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 #ifdef DEBUG
    726 		if (dtosn(fs, fs->lfs_curseg) == dtosn(fs, blkp->bi_daddr)) {
    727 			printf("lfs_bmapv: attempt to clean current segment? (#%d)\n",
    728 			       dtosn(fs, fs->lfs_curseg));
    729 			vfs_unbusy(mntp);
    730 			return (EBUSY);
    731 		}
    732 #endif /* DEBUG */
    733 		/*
    734 		 * Get the IFILE entry (only once) and see if the file still
    735 		 * exists.
    736 		 */
    737 		if (lastino != blkp->bi_inode) {
    738 			/*
    739 			 * Finish the old file, if there was one.  The presence
    740 			 * of a usable vnode in vp is signaled by a valid
    741 			 * v_daddr.
    742 			 */
    743 			if (v_daddr != LFS_UNUSED_DADDR) {
    744 				if (need_unlock) {
    745 					VOP_UNLOCK(vp, 0);
    746 					numlocked--;
    747 				}
    748 				lfs_vunref(vp);
    749 				numrefed--;
    750 			}
    751 
    752 			/*
    753 			 * Start a new file
    754 			 */
    755 			lastino = blkp->bi_inode;
    756 			if (blkp->bi_inode == LFS_IFILE_INUM)
    757 				v_daddr = fs->lfs_idaddr;
    758 			else {
    759 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    760 				v_daddr = ifp->if_daddr;
    761 				brelse(bp);
    762 			}
    763 			if (v_daddr == LFS_UNUSED_DADDR) {
    764 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    765 				continue;
    766 			}
    767 			/*
    768 			 * A regular call to VFS_VGET could deadlock
    769 			 * here.  Instead, we try an unlocked access.
    770 			 */
    771 			vp = ufs_ihashlookup(ump->um_dev, blkp->bi_inode);
    772 			if (vp != NULL && !(vp->v_flag & VXLOCK)) {
    773 				ip = VTOI(vp);
    774 				if (lfs_vref(vp)) {
    775 					v_daddr = LFS_UNUSED_DADDR;
    776 					need_unlock = 0;
    777 					continue;
    778 				}
    779 				numrefed++;
    780 				if (VOP_ISLOCKED(vp)) {
    781 #ifdef DEBUG_LFS
    782 					printf("lfs_bmapv: inode %d inlocked\n",ip->i_number);
    783 #endif
    784 					v_daddr = LFS_UNUSED_DADDR;
    785 					need_unlock = 0;
    786 					lfs_vunref(vp);
    787 					--numrefed;
    788 					continue;
    789 				} else {
    790 					vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    791 					need_unlock = FVG_UNLOCK;
    792 					numlocked++;
    793 				}
    794 			} else {
    795 				error = VFS_VGET(mntp, blkp->bi_inode, &vp);
    796 				if (error) {
    797 #ifdef DEBUG_LFS
    798 					printf("lfs_bmapv: vget of ino %d failed with %d",blkp->bi_inode,error);
    799 #endif
    800 					v_daddr = LFS_UNUSED_DADDR;
    801 					need_unlock = 0;
    802 					continue;
    803 				} else {
    804 					need_unlock = FVG_PUT;
    805 					numlocked++;
    806 					numrefed++;
    807 				}
    808 			}
    809 			ip = VTOI(vp);
    810 		} else if (v_daddr == LFS_UNUSED_DADDR) {
    811 			/*
    812 			 * This can only happen if the vnode is dead.
    813 			 * Keep going.  Note that we DO NOT set the
    814 			 * bi_addr to anything -- if we failed to get
    815 			 * the vnode, for example, we want to assume
    816 			 * conservatively that all of its blocks *are*
    817 			 * located in the segment in question.
    818 			 * lfs_markv will throw them out if we are
    819 			 * wrong.
    820 			 */
    821 			/* blkp->bi_daddr = LFS_UNUSED_DADDR; */
    822 			continue;
    823 		}
    824 
    825 		/* Past this point we are guaranteed that vp, ip are valid. */
    826 
    827 		if (blkp->bi_lbn == LFS_UNUSED_LBN) {
    828 			/*
    829 			 * We just want the inode address, which is
    830 			 * conveniently in v_daddr.
    831 			 */
    832 			blkp->bi_daddr = v_daddr;
    833 		} else {
    834 			error = VOP_BMAP(vp, blkp->bi_lbn, NULL,
    835 					 &(blkp->bi_daddr), NULL);
    836 			if (error)
    837 			{
    838 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    839 				continue;
    840 			}
    841 			blkp->bi_daddr = dbtofsb(fs, blkp->bi_daddr);
    842 			/* Fill in the block size, too */
    843 			blkp->bi_size = blksize(fs, ip, blkp->bi_lbn);
    844 		}
    845 	}
    846 
    847 	/*
    848 	 * Finish the old file, if there was one.  The presence
    849 	 * of a usable vnode in vp is signaled by a valid v_daddr.
    850 	 */
    851 	if (v_daddr != LFS_UNUSED_DADDR) {
    852 		if (need_unlock) {
    853 			VOP_UNLOCK(vp, 0);
    854 			numlocked--;
    855 		}
    856 		lfs_vunref(vp);
    857 		numrefed--;
    858 	}
    859 
    860 	if (numlocked != 0 || numrefed != 0) {
    861 		panic("lfs_bmapv: numlocked=%d numrefed=%d", numlocked,
    862 		      numrefed);
    863 	}
    864 
    865 	vfs_unbusy(mntp);
    866 
    867 	return 0;
    868 }
    869 
    870 /*
    871  * sys_lfs_segclean:
    872  *
    873  * Mark the segment clean.
    874  *
    875  *  0 on success
    876  * -1/errno is return on error.
    877  */
    878 int
    879 sys_lfs_segclean(struct proc *p, void *v, register_t *retval)
    880 {
    881 	struct sys_lfs_segclean_args /* {
    882 		syscallarg(fsid_t *) fsidp;
    883 		syscallarg(u_long) segment;
    884 	} */ *uap = v;
    885 	CLEANERINFO *cip;
    886 	SEGUSE *sup;
    887 	struct buf *bp;
    888 	struct mount *mntp;
    889 	struct lfs *fs;
    890 	fsid_t fsid;
    891 	int error;
    892 	unsigned long segnum;
    893 
    894 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    895 		return (error);
    896 
    897 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    898 		return (error);
    899 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    900 		return (ENOENT);
    901 
    902 	fs = VFSTOUFS(mntp)->um_lfs;
    903 	segnum = SCARG(uap, segment);
    904 
    905 	if (dtosn(fs, fs->lfs_curseg) == segnum)
    906 		return (EBUSY);
    907 
    908 	if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
    909 		return (error);
    910 #ifdef LFS_AGGRESSIVE_SEGLOCK
    911 	lfs_seglock(fs, SEGM_PROT);
    912 #endif
    913 	LFS_SEGENTRY(sup, fs, segnum, bp);
    914 	if (sup->su_nbytes) {
    915 		printf("lfs_segclean: not cleaning segment %lu: %d live bytes\n",
    916 			segnum, sup->su_nbytes);
    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_ACTIVE) {
    925 		brelse(bp);
    926 #ifdef LFS_AGGRESSIVE_SEGLOCK
    927 		lfs_segunlock(fs);
    928 #endif
    929 		vfs_unbusy(mntp);
    930 		return (EBUSY);
    931 	}
    932 	if (!(sup->su_flags & SEGUSE_DIRTY)) {
    933 		brelse(bp);
    934 #ifdef LFS_AGGRESSIVE_SEGLOCK
    935 		lfs_segunlock(fs);
    936 #endif
    937 		vfs_unbusy(mntp);
    938 		return (EALREADY);
    939 	}
    940 
    941 	fs->lfs_avail += segtod(fs, 1);
    942 	if (sup->su_flags & SEGUSE_SUPERBLOCK)
    943 		fs->lfs_avail -= btofsb(fs, LFS_SBPAD);
    944 	if (fs->lfs_version > 1 && segnum == 0 &&
    945 	    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
    946 		fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
    947 	fs->lfs_bfree += sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
    948 		btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    949 	fs->lfs_dmeta -= sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
    950 		btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    951 	if (fs->lfs_dmeta < 0)
    952 		fs->lfs_dmeta = 0;
    953 	sup->su_flags &= ~SEGUSE_DIRTY;
    954 	(void) LFS_BWRITE_LOG(bp);
    955 
    956 	LFS_CLEANERINFO(cip, fs, bp);
    957 	++cip->clean;
    958 	--cip->dirty;
    959 	fs->lfs_nclean = cip->clean;
    960 	cip->bfree = fs->lfs_bfree;
    961 	cip->avail = fs->lfs_avail - fs->lfs_ravail;
    962 	(void) LFS_BWRITE_LOG(bp);
    963 	wakeup(&fs->lfs_avail);
    964 #ifdef LFS_AGGRESSIVE_SEGLOCK
    965 	lfs_segunlock(fs);
    966 #endif
    967 	vfs_unbusy(mntp);
    968 
    969 	return (0);
    970 }
    971 
    972 /*
    973  * sys_lfs_segwait:
    974  *
    975  * This will block until a segment in file system fsid is written.  A timeout
    976  * in milliseconds may be specified which will awake the cleaner automatically.
    977  * An fsid of -1 means any file system, and a timeout of 0 means forever.
    978  *
    979  *  0 on success
    980  *  1 on timeout
    981  * -1/errno is return on error.
    982  */
    983 int
    984 sys_lfs_segwait(struct proc *p, void *v, register_t *retval)
    985 {
    986 	struct sys_lfs_segwait_args /* {
    987 		syscallarg(fsid_t *) fsidp;
    988 		syscallarg(struct timeval *) tv;
    989 	} */ *uap = v;
    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 dinode *dip;
   1094 	struct vnode *vp;
   1095 	struct ufsmount *ump;
   1096 	dev_t dev;
   1097 	int error, retries;
   1098 	struct buf *bp;
   1099 	struct lfs *fs;
   1100 
   1101 	ump = VFSTOUFS(mp);
   1102 	dev = ump->um_dev;
   1103 	fs = ump->um_lfs;
   1104 	*need_unlock = 0;
   1105 
   1106 	/*
   1107 	 * Wait until the filesystem is fully mounted before allowing vget
   1108 	 * to complete.  This prevents possible problems with roll-forward.
   1109 	 */
   1110 	while (fs->lfs_flags & LFS_NOTYET) {
   1111 		tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_fnotyet", 0);
   1112 	}
   1113 	/*
   1114 	 * This is playing fast and loose.  Someone may have the inode
   1115 	 * locked, in which case they are going to be distinctly unhappy
   1116 	 * if we trash something.
   1117 	 */
   1118 
   1119 	error = lfs_fasthashget(dev, ino, need_unlock, vpp);
   1120 	if (error != 0 || *vpp != NULL)
   1121 		return (error);
   1122 
   1123 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
   1124 		*vpp = NULL;
   1125 		return (error);
   1126 	}
   1127 
   1128 	do {
   1129 		error = lfs_fasthashget(dev, ino, need_unlock, vpp);
   1130 		if (error != 0 || *vpp != NULL) {
   1131 			ungetnewvnode(vp);
   1132 			return (error);
   1133 		}
   1134 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1135 
   1136 	/* Allocate new vnode/inode. */
   1137 	lfs_vcreate(mp, ino, vp);
   1138 
   1139 	/*
   1140 	 * Put it onto its hash chain and lock it so that other requests for
   1141 	 * this inode will block if they arrive while we are sleeping waiting
   1142 	 * for old data structures to be purged or for the contents of the
   1143 	 * disk portion of this inode to be read.
   1144 	 */
   1145 	ip = VTOI(vp);
   1146 	ufs_ihashins(ip);
   1147 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1148 
   1149 	/*
   1150 	 * XXX
   1151 	 * This may not need to be here, logically it should go down with
   1152 	 * the i_devvp initialization.
   1153 	 * Ask Kirk.
   1154 	 */
   1155 	ip->i_lfs = fs;
   1156 
   1157 	/* Read in the disk contents for the inode, copy into the inode. */
   1158 	if (dinp) {
   1159 		error = copyin(dinp, &ip->i_din.ffs_din, DINODE_SIZE);
   1160 		if (error) {
   1161 			printf("lfs_fastvget: dinode copyin failed for ino %d\n", ino);
   1162 			ufs_ihashrem(ip);
   1163 
   1164 			/* Unlock and discard unneeded inode. */
   1165 			lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
   1166 			lfs_vunref(vp);
   1167 			*vpp = NULL;
   1168 			return (error);
   1169 		}
   1170 		if (ip->i_number != ino)
   1171 			panic("lfs_fastvget: I was fed the wrong inode!");
   1172 	} else {
   1173 		retries = 0;
   1174 	    again:
   1175 		error = bread(ump->um_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
   1176 			      NOCRED, &bp);
   1177 		if (error) {
   1178 			printf("lfs_fastvget: bread failed with %d\n",error);
   1179 			/*
   1180 			 * The inode does not contain anything useful, so it
   1181 			 * would be misleading to leave it on its hash chain.
   1182 			 * Iput() will return it to the free list.
   1183 			 */
   1184 			ufs_ihashrem(ip);
   1185 
   1186 			/* Unlock and discard unneeded inode. */
   1187 			lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
   1188 			lfs_vunref(vp);
   1189 			brelse(bp);
   1190 			*vpp = NULL;
   1191 			return (error);
   1192 		}
   1193 		dip = lfs_ifind(ump->um_lfs, ino, bp);
   1194 		if (dip == NULL) {
   1195 			/* Assume write has not completed yet; try again */
   1196 			bp->b_flags |= B_INVAL;
   1197 			brelse(bp);
   1198 			++retries;
   1199 			if (retries > LFS_IFIND_RETRIES)
   1200 				panic("lfs_fastvget: dinode not found");
   1201 			printf("lfs_fastvget: dinode not found, retrying...\n");
   1202 			goto again;
   1203 		}
   1204 		ip->i_din.ffs_din = *dip;
   1205 		brelse(bp);
   1206 	}
   1207 	ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1208 	ip->i_lfs_effnblks = ip->i_ffs_blocks;
   1209 
   1210 	/*
   1211 	 * Initialize the vnode from the inode, check for aliases.  In all
   1212 	 * cases re-init ip, the underlying vnode/inode may have changed.
   1213 	 */
   1214 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   1215 #ifdef DEBUG_LFS
   1216 	if (vp->v_type == VNON) {
   1217 		printf("lfs_fastvget: ino %d is type VNON! (ifmt=%o, dinp=%p)\n",
   1218 		       ip->i_number, (ip->i_ffs_mode & IFMT) >> 12, dinp);
   1219 		lfs_dump_dinode(&ip->i_din.ffs_din);
   1220 #ifdef DDB
   1221 		Debugger();
   1222 #endif
   1223 	}
   1224 #endif /* DEBUG_LFS */
   1225 	/*
   1226 	 * Finish inode initialization now that aliasing has been resolved.
   1227 	 */
   1228 
   1229 	genfs_node_init(vp, &lfs_genfsops);
   1230 	ip->i_devvp = ump->um_devvp;
   1231 	VREF(ip->i_devvp);
   1232 	*vpp = vp;
   1233 	*need_unlock |= FVG_PUT;
   1234 
   1235 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1236 
   1237 	return (0);
   1238 }
   1239 
   1240 struct buf *
   1241 lfs_fakebuf(struct lfs *fs, struct vnode *vp, int lbn, size_t size, caddr_t uaddr)
   1242 {
   1243 	struct buf *bp;
   1244 	int error;
   1245 
   1246 #ifndef ALLOW_VFLUSH_CORRUPTION
   1247 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size);
   1248 	error = copyin(uaddr, bp->b_data, size);
   1249 	if (error) {
   1250 		lfs_freebuf(bp);
   1251 		return NULL;
   1252 	}
   1253 #else
   1254 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, 0);
   1255 	bp->b_flags |= B_INVAL;
   1256 	bp->b_saveaddr = uaddr;
   1257 #endif
   1258 #if 0
   1259 	bp->b_saveaddr = (caddr_t)fs;
   1260 	++fs->lfs_iocount;
   1261 #endif
   1262 	bp->b_bufsize = size;
   1263 	bp->b_bcount = size;
   1264 	return (bp);
   1265 }
   1266