Home | History | Annotate | Line # | Download | only in lfs
lfs_syscalls.c revision 1.71
      1 /*	$NetBSD: lfs_syscalls.c,v 1.71 2002/08/03 00:12:49 itojun 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.71 2002/08/03 00:12:49 itojun 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 	if ((u_int) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
    621 		return (EINVAL);
    622 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    623 	if ((error = copyin(SCARG(uap, blkiov), blkiov,
    624 			    blkcnt * sizeof(BLOCK_INFO))) != 0)
    625 		goto out;
    626 
    627 	if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0)
    628 		copyout(blkiov, SCARG(uap, blkiov),
    629 			blkcnt * sizeof(BLOCK_INFO));
    630     out:
    631 	free(blkiov, M_SEGMENT);
    632 	return error;
    633 }
    634 #else
    635 int
    636 sys_lfs_bmapv(struct proc *p, void *v, register_t *retval)
    637 {
    638 	struct sys_lfs_bmapv_args /* {
    639 		syscallarg(fsid_t *) fsidp;
    640 		syscallarg(struct block_info *) blkiov;
    641 		syscallarg(int) blkcnt;
    642 	} */ *uap = v;
    643 	BLOCK_INFO *blkiov;
    644 	BLOCK_INFO_15 *blkiov15;
    645 	int i, blkcnt, error;
    646 	fsid_t fsid;
    647 
    648 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    649 		return (error);
    650 
    651 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    652 		return (error);
    653 
    654 	blkcnt = SCARG(uap, blkcnt);
    655 	if ((u_int) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
    656 		return (EINVAL);
    657 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    658 	blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
    659 	if ((error = copyin(SCARG(uap, blkiov), blkiov15,
    660 			    blkcnt * sizeof(BLOCK_INFO_15))) != 0)
    661 		goto out;
    662 
    663 	for (i = 0; i < blkcnt; i++) {
    664 		blkiov[i].bi_inode     = blkiov15[i].bi_inode;
    665 		blkiov[i].bi_lbn       = blkiov15[i].bi_lbn;
    666 		blkiov[i].bi_daddr     = blkiov15[i].bi_daddr;
    667 		blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
    668 		blkiov[i].bi_version   = blkiov15[i].bi_version;
    669 		blkiov[i].bi_bp        = blkiov15[i].bi_bp;
    670 		blkiov[i].bi_size      = blkiov15[i].bi_size;
    671 	}
    672 
    673 	if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0) {
    674 		for (i = 0; i < blkcnt; i++) {
    675 			blkiov15[i].bi_inode     = blkiov[i].bi_inode;
    676 			blkiov15[i].bi_lbn       = blkiov[i].bi_lbn;
    677 			blkiov15[i].bi_daddr     = blkiov[i].bi_daddr;
    678 			blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
    679 			blkiov15[i].bi_version   = blkiov[i].bi_version;
    680 			blkiov15[i].bi_bp        = blkiov[i].bi_bp;
    681 			blkiov15[i].bi_size      = blkiov[i].bi_size;
    682 		}
    683 		copyout(blkiov15, SCARG(uap, blkiov),
    684 			blkcnt * sizeof(BLOCK_INFO_15));
    685 	}
    686     out:
    687 	free(blkiov, M_SEGMENT);
    688 	free(blkiov15, M_SEGMENT);
    689 	return error;
    690 }
    691 #endif
    692 
    693 static int
    694 lfs_bmapv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
    695 {
    696 	BLOCK_INFO *blkp;
    697 	IFILE *ifp;
    698 	struct buf *bp;
    699 	struct inode *ip = NULL;
    700 	struct lfs *fs;
    701 	struct mount *mntp;
    702 	struct ufsmount *ump;
    703 	struct vnode *vp;
    704 	ino_t lastino;
    705 	ufs_daddr_t v_daddr;
    706 	int cnt, error, need_unlock = 0;
    707 	int numlocked = 0, numrefed = 0;
    708 
    709 	lfs_cleaner_pid = p->p_pid;
    710 
    711 	if ((mntp = vfs_getvfs(fsidp)) == NULL)
    712 		return (ENOENT);
    713 
    714 	ump = VFSTOUFS(mntp);
    715 	if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
    716 		return (error);
    717 
    718 	cnt = blkcnt;
    719 
    720 	fs = VFSTOUFS(mntp)->um_lfs;
    721 
    722 	error = 0;
    723 
    724 	/* these were inside the initialization for the for loop */
    725 	v_daddr = LFS_UNUSED_DADDR;
    726 	lastino = LFS_UNUSED_INUM;
    727 	for (blkp = blkiov; cnt--; ++blkp)
    728 	{
    729 		/*
    730 		 * Get the IFILE entry (only once) and see if the file still
    731 		 * exists.
    732 		 */
    733 		if (lastino != blkp->bi_inode) {
    734 			/*
    735 			 * Finish the old file, if there was one.  The presence
    736 			 * of a usable vnode in vp is signaled by a valid
    737 			 * v_daddr.
    738 			 */
    739 			if (v_daddr != LFS_UNUSED_DADDR) {
    740 				if (need_unlock) {
    741 					VOP_UNLOCK(vp, 0);
    742 					numlocked--;
    743 				}
    744 				lfs_vunref(vp);
    745 				numrefed--;
    746 			}
    747 
    748 			/*
    749 			 * Start a new file
    750 			 */
    751 			lastino = blkp->bi_inode;
    752 			if (blkp->bi_inode == LFS_IFILE_INUM)
    753 				v_daddr = fs->lfs_idaddr;
    754 			else {
    755 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    756 				v_daddr = ifp->if_daddr;
    757 				brelse(bp);
    758 			}
    759 			if (v_daddr == LFS_UNUSED_DADDR) {
    760 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    761 				continue;
    762 			}
    763 			/*
    764 			 * A regular call to VFS_VGET could deadlock
    765 			 * here.  Instead, we try an unlocked access.
    766 			 */
    767 			vp = ufs_ihashlookup(ump->um_dev, blkp->bi_inode);
    768 			if (vp != NULL && !(vp->v_flag & VXLOCK)) {
    769 				ip = VTOI(vp);
    770 				if (lfs_vref(vp)) {
    771 					v_daddr = LFS_UNUSED_DADDR;
    772 					need_unlock = 0;
    773 					continue;
    774 				}
    775 				numrefed++;
    776 				if (VOP_ISLOCKED(vp)) {
    777 #ifdef DEBUG_LFS
    778 					printf("lfs_bmapv: inode %d inlocked\n",ip->i_number);
    779 #endif
    780 					v_daddr = LFS_UNUSED_DADDR;
    781 					need_unlock = 0;
    782 					lfs_vunref(vp);
    783 					--numrefed;
    784 					continue;
    785 				} else {
    786 					vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    787 					need_unlock = FVG_UNLOCK;
    788 					numlocked++;
    789 				}
    790 			} else {
    791 				error = VFS_VGET(mntp, blkp->bi_inode, &vp);
    792 				if (error) {
    793 #ifdef DEBUG_LFS
    794 					printf("lfs_bmapv: vget of ino %d failed with %d",blkp->bi_inode,error);
    795 #endif
    796 					v_daddr = LFS_UNUSED_DADDR;
    797 					need_unlock = 0;
    798 					continue;
    799 				} else {
    800 					need_unlock = FVG_PUT;
    801 					numlocked++;
    802 					numrefed++;
    803 				}
    804 			}
    805 			ip = VTOI(vp);
    806 		} else if (v_daddr == LFS_UNUSED_DADDR) {
    807 			/*
    808 			 * This can only happen if the vnode is dead.
    809 			 * Keep going.  Note that we DO NOT set the
    810 			 * bi_addr to anything -- if we failed to get
    811 			 * the vnode, for example, we want to assume
    812 			 * conservatively that all of its blocks *are*
    813 			 * located in the segment in question.
    814 			 * lfs_markv will throw them out if we are
    815 			 * wrong.
    816 			 */
    817 			/* blkp->bi_daddr = LFS_UNUSED_DADDR; */
    818 			continue;
    819 		}
    820 
    821 		/* Past this point we are guaranteed that vp, ip are valid. */
    822 
    823 		if (blkp->bi_lbn == LFS_UNUSED_LBN) {
    824 			/*
    825 			 * We just want the inode address, which is
    826 			 * conveniently in v_daddr.
    827 			 */
    828 			blkp->bi_daddr = v_daddr;
    829 		} else {
    830 			error = VOP_BMAP(vp, blkp->bi_lbn, NULL,
    831 					 &(blkp->bi_daddr), NULL);
    832 			if (error)
    833 			{
    834 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    835 				continue;
    836 			}
    837 			blkp->bi_daddr = dbtofsb(fs, blkp->bi_daddr);
    838 			/* Fill in the block size, too */
    839 			blkp->bi_size = blksize(fs, ip, blkp->bi_lbn);
    840 		}
    841 	}
    842 
    843 	/*
    844 	 * Finish the old file, if there was one.  The presence
    845 	 * of a usable vnode in vp is signaled by a valid v_daddr.
    846 	 */
    847 	if (v_daddr != LFS_UNUSED_DADDR) {
    848 		if (need_unlock) {
    849 			VOP_UNLOCK(vp, 0);
    850 			numlocked--;
    851 		}
    852 		lfs_vunref(vp);
    853 		numrefed--;
    854 	}
    855 
    856 	if (numlocked != 0 || numrefed != 0) {
    857 		panic("lfs_bmapv: numlocked=%d numrefed=%d", numlocked,
    858 		      numrefed);
    859 	}
    860 
    861 	vfs_unbusy(mntp);
    862 
    863 	return 0;
    864 }
    865 
    866 /*
    867  * sys_lfs_segclean:
    868  *
    869  * Mark the segment clean.
    870  *
    871  *  0 on success
    872  * -1/errno is return on error.
    873  */
    874 int
    875 sys_lfs_segclean(struct proc *p, void *v, register_t *retval)
    876 {
    877 	struct sys_lfs_segclean_args /* {
    878 		syscallarg(fsid_t *) fsidp;
    879 		syscallarg(u_long) segment;
    880 	} */ *uap = v;
    881 	CLEANERINFO *cip;
    882 	SEGUSE *sup;
    883 	struct buf *bp;
    884 	struct mount *mntp;
    885 	struct lfs *fs;
    886 	fsid_t fsid;
    887 	int error;
    888 	unsigned long segnum;
    889 
    890 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    891 		return (error);
    892 
    893 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    894 		return (error);
    895 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    896 		return (ENOENT);
    897 
    898 	fs = VFSTOUFS(mntp)->um_lfs;
    899 	segnum = SCARG(uap, segment);
    900 
    901 	if (dtosn(fs, fs->lfs_curseg) == segnum)
    902 		return (EBUSY);
    903 
    904 	if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
    905 		return (error);
    906 #ifdef LFS_AGGRESSIVE_SEGLOCK
    907 	lfs_seglock(fs, SEGM_PROT);
    908 #endif
    909 	LFS_SEGENTRY(sup, fs, segnum, bp);
    910 	if (sup->su_nbytes) {
    911 		printf("lfs_segclean: not cleaning segment %lu: %d live bytes\n",
    912 			segnum, sup->su_nbytes);
    913 		brelse(bp);
    914 #ifdef LFS_AGGRESSIVE_SEGLOCK
    915 		lfs_segunlock(fs);
    916 #endif
    917 		vfs_unbusy(mntp);
    918 		return (EBUSY);
    919 	}
    920 	if (sup->su_flags & SEGUSE_ACTIVE) {
    921 		brelse(bp);
    922 #ifdef LFS_AGGRESSIVE_SEGLOCK
    923 		lfs_segunlock(fs);
    924 #endif
    925 		vfs_unbusy(mntp);
    926 		return (EBUSY);
    927 	}
    928 	if (!(sup->su_flags & SEGUSE_DIRTY)) {
    929 		brelse(bp);
    930 #ifdef LFS_AGGRESSIVE_SEGLOCK
    931 		lfs_segunlock(fs);
    932 #endif
    933 		vfs_unbusy(mntp);
    934 		return (EALREADY);
    935 	}
    936 
    937 	fs->lfs_avail += segtod(fs, 1);
    938 	if (sup->su_flags & SEGUSE_SUPERBLOCK)
    939 		fs->lfs_avail -= btofsb(fs, LFS_SBPAD);
    940 	if (fs->lfs_version > 1 && segnum == 0 &&
    941 	    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
    942 		fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
    943 	fs->lfs_bfree += sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
    944 		btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    945 	fs->lfs_dmeta -= sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
    946 		btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    947 	if (fs->lfs_dmeta < 0)
    948 		fs->lfs_dmeta = 0;
    949 	sup->su_flags &= ~SEGUSE_DIRTY;
    950 	(void) LFS_BWRITE_LOG(bp);
    951 
    952 	LFS_CLEANERINFO(cip, fs, bp);
    953 	++cip->clean;
    954 	--cip->dirty;
    955 	fs->lfs_nclean = cip->clean;
    956 	cip->bfree = fs->lfs_bfree;
    957 	cip->avail = fs->lfs_avail - fs->lfs_ravail;
    958 	(void) LFS_BWRITE_LOG(bp);
    959 	wakeup(&fs->lfs_avail);
    960 #ifdef LFS_AGGRESSIVE_SEGLOCK
    961 	lfs_segunlock(fs);
    962 #endif
    963 	vfs_unbusy(mntp);
    964 
    965 	return (0);
    966 }
    967 
    968 /*
    969  * sys_lfs_segwait:
    970  *
    971  * This will block until a segment in file system fsid is written.  A timeout
    972  * in milliseconds may be specified which will awake the cleaner automatically.
    973  * An fsid of -1 means any file system, and a timeout of 0 means forever.
    974  *
    975  *  0 on success
    976  *  1 on timeout
    977  * -1/errno is return on error.
    978  */
    979 int
    980 sys_lfs_segwait(struct proc *p, void *v, register_t *retval)
    981 {
    982 	struct sys_lfs_segwait_args /* {
    983 		syscallarg(fsid_t *) fsidp;
    984 		syscallarg(struct timeval *) tv;
    985 	} */ *uap = v;
    986 	struct mount *mntp;
    987 	struct timeval atv;
    988 	fsid_t fsid;
    989 	void *addr;
    990 	u_long timeout;
    991 	int error, s;
    992 
    993 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    994 		return (error);
    995 	}
    996 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    997 		return (error);
    998 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    999 		addr = &lfs_allclean_wakeup;
   1000 	else
   1001 		addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
   1002 
   1003 	if (SCARG(uap, tv)) {
   1004 		error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
   1005 		if (error)
   1006 			return (error);
   1007 		if (itimerfix(&atv))
   1008 			return (EINVAL);
   1009 		/*
   1010 		 * XXX THIS COULD SLEEP FOREVER IF TIMEOUT IS {0,0}!
   1011 		 * XXX IS THAT WHAT IS INTENDED?
   1012 		 */
   1013 		s = splclock();
   1014 		timeradd(&atv, &time, &atv);
   1015 		timeout = hzto(&atv);
   1016 		splx(s);
   1017 	} else
   1018 		timeout = 0;
   1019 
   1020 	error = tsleep(addr, PCATCH | PUSER, "segment", timeout);
   1021 	return (error == ERESTART ? EINTR : 0);
   1022 }
   1023 
   1024 /*
   1025  * VFS_VGET call specialized for the cleaner.  The cleaner already knows the
   1026  * daddr from the ifile, so don't look it up again.  If the cleaner is
   1027  * processing IINFO structures, it may have the ondisk inode already, so
   1028  * don't go retrieving it again.
   1029  *
   1030  * If we find the vnode on the hash chain, then it may be locked by another
   1031  * process; so we set (*need_unlock) to zero.
   1032  *
   1033  * If we don't, we call ufs_ihashins, which locks the inode, and we set
   1034  * (*need_unlock) to non-zero.
   1035  *
   1036  * In either case we lfs_vref, and it is the caller's responsibility to
   1037  * lfs_vunref and VOP_UNLOCK (if necessary) when finished.
   1038  */
   1039 extern struct lock ufs_hashlock;
   1040 
   1041 int
   1042 lfs_fasthashget(dev_t dev, ino_t ino, int *need_unlock, struct vnode **vpp)
   1043 {
   1044 	struct inode *ip;
   1045 
   1046 	/*
   1047 	 * This is playing fast and loose.  Someone may have the inode
   1048 	 * locked, in which case they are going to be distinctly unhappy
   1049 	 * if we trash something.
   1050 	 */
   1051 	if ((*vpp = ufs_ihashlookup(dev, ino)) != NULL) {
   1052 		if ((*vpp)->v_flag & VXLOCK) {
   1053 			printf("lfs_fastvget: vnode VXLOCKed for ino %d\n",
   1054 			       ino);
   1055 			clean_vnlocked++;
   1056 #ifdef LFS_EAGAIN_FAIL
   1057 			return EAGAIN;
   1058 #endif
   1059 		}
   1060 		ip = VTOI(*vpp);
   1061 		if (lfs_vref(*vpp)) {
   1062 			clean_inlocked++;
   1063 			return EAGAIN;
   1064 		}
   1065 		if (VOP_ISLOCKED(*vpp)) {
   1066 #ifdef DEBUG_LFS
   1067 			printf("lfs_fastvget: ino %d inlocked by pid %d\n",
   1068 			       ip->i_number, (*vpp)->v_lock.lk_lockholder);
   1069 #endif
   1070 			clean_inlocked++;
   1071 #ifdef LFS_EAGAIN_FAIL
   1072 			lfs_vunref(*vpp);
   1073 			return EAGAIN;
   1074 #endif /* LFS_EAGAIN_FAIL */
   1075 		} else {
   1076 			vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
   1077 			*need_unlock |= FVG_UNLOCK;
   1078 		}
   1079 	} else
   1080 		*vpp = NULL;
   1081 
   1082 	return (0);
   1083 }
   1084 
   1085 int
   1086 lfs_fastvget(struct mount *mp, ino_t ino, ufs_daddr_t daddr, struct vnode **vpp, struct dinode *dinp, int *need_unlock)
   1087 {
   1088 	struct inode *ip;
   1089 	struct dinode *dip;
   1090 	struct vnode *vp;
   1091 	struct ufsmount *ump;
   1092 	dev_t dev;
   1093 	int i, error, retries;
   1094 	struct buf *bp;
   1095 	struct lfs *fs;
   1096 
   1097 	ump = VFSTOUFS(mp);
   1098 	dev = ump->um_dev;
   1099 	fs = ump->um_lfs;
   1100 	*need_unlock = 0;
   1101 
   1102 	/*
   1103 	 * Wait until the filesystem is fully mounted before allowing vget
   1104 	 * to complete.  This prevents possible problems with roll-forward.
   1105 	 */
   1106 	while (fs->lfs_flags & LFS_NOTYET) {
   1107 		tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_fnotyet", 0);
   1108 	}
   1109 	/*
   1110 	 * This is playing fast and loose.  Someone may have the inode
   1111 	 * locked, in which case they are going to be distinctly unhappy
   1112 	 * if we trash something.
   1113 	 */
   1114 
   1115 	error = lfs_fasthashget(dev, ino, need_unlock, vpp);
   1116 	if (error != 0 || *vpp != NULL)
   1117 		return (error);
   1118 
   1119 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
   1120 		*vpp = NULL;
   1121 		return (error);
   1122 	}
   1123 
   1124 	do {
   1125 		error = lfs_fasthashget(dev, ino, need_unlock, vpp);
   1126 		if (error != 0 || *vpp != NULL) {
   1127 			ungetnewvnode(vp);
   1128 			return (error);
   1129 		}
   1130 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1131 
   1132 	/* Allocate new vnode/inode. */
   1133 	lfs_vcreate(mp, ino, vp);
   1134 
   1135 	/*
   1136 	 * Put it onto its hash chain and lock it so that other requests for
   1137 	 * this inode will block if they arrive while we are sleeping waiting
   1138 	 * for old data structures to be purged or for the contents of the
   1139 	 * disk portion of this inode to be read.
   1140 	 */
   1141 	ip = VTOI(vp);
   1142 	ufs_ihashins(ip);
   1143 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1144 
   1145 	/*
   1146 	 * XXX
   1147 	 * This may not need to be here, logically it should go down with
   1148 	 * the i_devvp initialization.
   1149 	 * Ask Kirk.
   1150 	 */
   1151 	ip->i_lfs = fs;
   1152 
   1153 	/* Read in the disk contents for the inode, copy into the inode. */
   1154 	if (dinp) {
   1155 		error = copyin(dinp, &ip->i_din.ffs_din, DINODE_SIZE);
   1156 		if (error) {
   1157 			printf("lfs_fastvget: dinode copyin failed for ino %d\n", ino);
   1158 			ufs_ihashrem(ip);
   1159 
   1160 			/* Unlock and discard unneeded inode. */
   1161 			lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
   1162 			lfs_vunref(vp);
   1163 			*vpp = NULL;
   1164 			return (error);
   1165 		}
   1166 		if (ip->i_number != ino)
   1167 			panic("lfs_fastvget: I was fed the wrong inode!");
   1168 	} else {
   1169 		retries = 0;
   1170 	    again:
   1171 		error = bread(ump->um_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
   1172 			      NOCRED, &bp);
   1173 		if (error) {
   1174 			printf("lfs_fastvget: bread failed with %d\n",error);
   1175 			/*
   1176 			 * The inode does not contain anything useful, so it
   1177 			 * would be misleading to leave it on its hash chain.
   1178 			 * Iput() will return it to the free list.
   1179 			 */
   1180 			ufs_ihashrem(ip);
   1181 
   1182 			/* Unlock and discard unneeded inode. */
   1183 			lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
   1184 			lfs_vunref(vp);
   1185 			brelse(bp);
   1186 			*vpp = NULL;
   1187 			return (error);
   1188 		}
   1189 		dip = lfs_ifind(ump->um_lfs, ino, bp);
   1190 		if (dip == NULL) {
   1191 			/* Assume write has not completed yet; try again */
   1192 			bp->b_flags |= B_INVAL;
   1193 			brelse(bp);
   1194 			++retries;
   1195 			if (retries > LFS_IFIND_RETRIES)
   1196 				panic("lfs_fastvget: dinode not found");
   1197 			printf("lfs_fastvget: dinode not found, retrying...\n");
   1198 			goto again;
   1199 		}
   1200 		ip->i_din.ffs_din = *dip;
   1201 		brelse(bp);
   1202 	}
   1203 	ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1204 	ip->i_lfs_effnblks = ip->i_ffs_blocks;
   1205 	ip->i_lfs_osize = ip->i_ffs_size;
   1206 
   1207 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
   1208 	for (i = 0; i < NDADDR; i++)
   1209 		if (ip->i_ffs_db[i] != 0)
   1210 			ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
   1211 
   1212 	/*
   1213 	 * Initialize the vnode from the inode, check for aliases.  In all
   1214 	 * cases re-init ip, the underlying vnode/inode may have changed.
   1215 	 */
   1216 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   1217 #ifdef DEBUG_LFS
   1218 	if (vp->v_type == VNON) {
   1219 		printf("lfs_fastvget: ino %d is type VNON! (ifmt=%o, dinp=%p)\n",
   1220 		       ip->i_number, (ip->i_ffs_mode & IFMT) >> 12, dinp);
   1221 		lfs_dump_dinode(&ip->i_din.ffs_din);
   1222 #ifdef DDB
   1223 		Debugger();
   1224 #endif
   1225 	}
   1226 #endif /* DEBUG_LFS */
   1227 	/*
   1228 	 * Finish inode initialization now that aliasing has been resolved.
   1229 	 */
   1230 
   1231 	genfs_node_init(vp, &lfs_genfsops);
   1232 	ip->i_devvp = ump->um_devvp;
   1233 	VREF(ip->i_devvp);
   1234 	*vpp = vp;
   1235 	*need_unlock |= FVG_PUT;
   1236 
   1237 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1238 
   1239 	return (0);
   1240 }
   1241 
   1242 struct buf *
   1243 lfs_fakebuf(struct lfs *fs, struct vnode *vp, int lbn, size_t size, caddr_t uaddr)
   1244 {
   1245 	struct buf *bp;
   1246 	int error;
   1247 
   1248 #ifndef ALLOW_VFLUSH_CORRUPTION
   1249 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size);
   1250 	error = copyin(uaddr, bp->b_data, size);
   1251 	if (error) {
   1252 		lfs_freebuf(bp);
   1253 		return NULL;
   1254 	}
   1255 #else
   1256 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, 0);
   1257 	bp->b_flags |= B_INVAL;
   1258 	bp->b_saveaddr = uaddr;
   1259 #endif
   1260 #if 0
   1261 	bp->b_saveaddr = (caddr_t)fs;
   1262 	++fs->lfs_iocount;
   1263 #endif
   1264 	bp->b_bufsize = size;
   1265 	bp->b_bcount = size;
   1266 	return (bp);
   1267 }
   1268