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