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