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