Home | History | Annotate | Line # | Download | only in lfs
lfs_syscalls.c revision 1.148
      1 /*	$NetBSD: lfs_syscalls.c,v 1.148 2013/07/28 01:05:52 dholland Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007, 2007, 2008
      5  *    The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Konrad E. Schroder <perseant (at) hhhh.org>.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 /*-
     33  * Copyright (c) 1991, 1993, 1994
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)lfs_syscalls.c	8.10 (Berkeley) 5/14/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: lfs_syscalls.c,v 1.148 2013/07/28 01:05:52 dholland Exp $");
     65 
     66 #ifndef LFS
     67 # define LFS		/* for prototypes in syscallargs.h */
     68 #endif
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/proc.h>
     73 #include <sys/buf.h>
     74 #include <sys/mount.h>
     75 #include <sys/vnode.h>
     76 #include <sys/kernel.h>
     77 #include <sys/kauth.h>
     78 #include <sys/syscallargs.h>
     79 
     80 #include <ufs/lfs/ulfs_inode.h>
     81 #include <ufs/lfs/ulfsmount.h>
     82 #include <ufs/lfs/ulfs_extern.h>
     83 
     84 #include <ufs/lfs/lfs.h>
     85 #include <ufs/lfs/lfs_kernel.h>
     86 #include <ufs/lfs/lfs_extern.h>
     87 
     88 struct buf *lfs_fakebuf(struct lfs *, struct vnode *, int, size_t, void *);
     89 int lfs_fasthashget(dev_t, ino_t, struct vnode **);
     90 
     91 pid_t lfs_cleaner_pid = 0;
     92 
     93 /*
     94  * sys_lfs_markv:
     95  *
     96  * This will mark inodes and blocks dirty, so they are written into the log.
     97  * It will block until all the blocks have been written.  The segment create
     98  * time passed in the block_info and inode_info structures is used to decide
     99  * if the data is valid for each block (in case some process dirtied a block
    100  * or inode that is being cleaned between the determination that a block is
    101  * live and the lfs_markv call).
    102  *
    103  *  0 on success
    104  * -1/errno is return on error.
    105  */
    106 #ifdef USE_64BIT_SYSCALLS
    107 int
    108 sys_lfs_markv(struct lwp *l, const struct sys_lfs_markv_args *uap, register_t *retval)
    109 {
    110 	/* {
    111 		syscallarg(fsid_t *) fsidp;
    112 		syscallarg(struct block_info *) blkiov;
    113 		syscallarg(int) blkcnt;
    114 	} */
    115 	BLOCK_INFO *blkiov;
    116 	int blkcnt, error;
    117 	fsid_t fsid;
    118 	struct lfs *fs;
    119 	struct mount *mntp;
    120 
    121 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
    122 	    KAUTH_REQ_SYSTEM_LFS_MARKV, NULL, NULL, NULL);
    123 	if (error)
    124 		return (error);
    125 
    126 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    127 		return (error);
    128 
    129 	if ((mntp = vfs_getvfs(fsidp)) == NULL)
    130 		return (ENOENT);
    131 	fs = VFSTOULFS(mntp)->um_lfs;
    132 
    133 	blkcnt = SCARG(uap, blkcnt);
    134 	if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
    135 		return (EINVAL);
    136 
    137 	KERNEL_LOCK(1, NULL);
    138 	blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
    139 	if ((error = copyin(SCARG(uap, blkiov), blkiov,
    140 			    blkcnt * sizeof(BLOCK_INFO))) != 0)
    141 		goto out;
    142 
    143 	if ((error = lfs_markv(p, &fsid, blkiov, blkcnt)) == 0)
    144 		copyout(blkiov, SCARG(uap, blkiov),
    145 			blkcnt * sizeof(BLOCK_INFO));
    146     out:
    147 	lfs_free(fs, blkiov, LFS_NB_BLKIOV);
    148 	KERNEL_UNLOCK_ONE(NULL);
    149 	return error;
    150 }
    151 #else
    152 int
    153 sys_lfs_markv(struct lwp *l, const struct sys_lfs_markv_args *uap, register_t *retval)
    154 {
    155 	/* {
    156 		syscallarg(fsid_t *) fsidp;
    157 		syscallarg(struct block_info *) blkiov;
    158 		syscallarg(int) blkcnt;
    159 	} */
    160 	BLOCK_INFO *blkiov;
    161 	BLOCK_INFO_15 *blkiov15;
    162 	int i, blkcnt, error;
    163 	fsid_t fsid;
    164 	struct lfs *fs;
    165 	struct mount *mntp;
    166 
    167 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
    168 	    KAUTH_REQ_SYSTEM_LFS_MARKV, NULL, NULL, NULL);
    169 	if (error)
    170 		return (error);
    171 
    172 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    173 		return (error);
    174 
    175 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    176 		return (ENOENT);
    177 	fs = VFSTOULFS(mntp)->um_lfs;
    178 
    179 	blkcnt = SCARG(uap, blkcnt);
    180 	if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
    181 		return (EINVAL);
    182 
    183 	KERNEL_LOCK(1, NULL);
    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 	KERNEL_UNLOCK_ONE(NULL);
    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,
    225     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 = VFSTOULFS(mntp)->um_lfs;
    249 
    250 	if (fs->lfs_ronly)
    251 		return EROFS;
    252 
    253 	maxino = (lfs_fragstoblks(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, 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, 0);
    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 				      lfs_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 		/* Can't clean VU_DIROP directories in case of truncation */
    371 		/* XXX - maybe we should mark removed dirs specially? */
    372 		if (vp->v_type == VDIR && (vp->v_uflag & VU_DIROP)) {
    373 			do_again++;
    374 			continue;
    375 		}
    376 
    377 		/* If this BLOCK_INFO didn't contain a block, keep going. */
    378 		if (blkp->bi_lbn == LFS_UNUSED_LBN) {
    379 			/* XXX need to make sure that the inode gets written in this case */
    380 			/* XXX but only write the inode if it's the right one */
    381 			if (blkp->bi_inode != LFS_IFILE_INUM) {
    382 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    383 				if (ifp->if_daddr == blkp->bi_daddr) {
    384 					mutex_enter(&lfs_lock);
    385 					LFS_SET_UINO(ip, IN_CLEANING);
    386 					mutex_exit(&lfs_lock);
    387 				}
    388 				brelse(bp, 0);
    389 			}
    390 			continue;
    391 		}
    392 
    393 		b_daddr = 0;
    394 		if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
    395 		    LFS_DBTOFSB(fs, b_daddr) != blkp->bi_daddr)
    396 		{
    397 			if (lfs_dtosn(fs, LFS_DBTOFSB(fs, b_daddr)) ==
    398 			    lfs_dtosn(fs, blkp->bi_daddr))
    399 			{
    400 				DLOG((DLOG_CLEAN, "lfs_markv: wrong da same seg: %llx vs %llx\n",
    401 				      (long long)blkp->bi_daddr, (long long)LFS_DBTOFSB(fs, b_daddr)));
    402 			}
    403 			do_again++;
    404 			continue;
    405 		}
    406 
    407 		/*
    408 		 * Check block sizes.  The blocks being cleaned come from
    409 		 * disk, so they should have the same size as their on-disk
    410 		 * counterparts.
    411 		 */
    412 		if (blkp->bi_lbn >= 0)
    413 			obsize = lfs_blksize(fs, ip, blkp->bi_lbn);
    414 		else
    415 			obsize = fs->lfs_bsize;
    416 		/* Check for fragment size change */
    417 		if (blkp->bi_lbn >= 0 && blkp->bi_lbn < ULFS_NDADDR) {
    418 			obsize = ip->i_lfs_fragsize[blkp->bi_lbn];
    419 		}
    420 		if (obsize != blkp->bi_size) {
    421 			DLOG((DLOG_CLEAN, "lfs_markv: ino %d lbn %lld wrong"
    422 			      " size (%ld != %d), try again\n",
    423 			      blkp->bi_inode, (long long)blkp->bi_lbn,
    424 			      (long) obsize, blkp->bi_size));
    425 			do_again++;
    426 			continue;
    427 		}
    428 
    429 		/*
    430 		 * If we get to here, then we are keeping the block.  If
    431 		 * it is an indirect block, we want to actually put it
    432 		 * in the buffer cache so that it can be updated in the
    433 		 * finish_meta section.	 If it's not, we need to
    434 		 * allocate a fake buffer so that writeseg can perform
    435 		 * the copyin and write the buffer.
    436 		 */
    437 		if (ip->i_number != LFS_IFILE_INUM && blkp->bi_lbn >= 0) {
    438 			/* Data Block */
    439 			bp = lfs_fakebuf(fs, vp, blkp->bi_lbn,
    440 					 blkp->bi_size, blkp->bi_bp);
    441 			/* Pretend we used bread() to get it */
    442 			bp->b_blkno = LFS_FSBTODB(fs, blkp->bi_daddr);
    443 		} else {
    444 			/* Indirect block or ifile */
    445 			if (blkp->bi_size != fs->lfs_bsize &&
    446 			    ip->i_number != LFS_IFILE_INUM)
    447 				panic("lfs_markv: partial indirect block?"
    448 				    " size=%d\n", blkp->bi_size);
    449 			bp = getblk(vp, blkp->bi_lbn, blkp->bi_size, 0, 0);
    450 			if (!(bp->b_oflags & (BO_DONE|BO_DELWRI))) {
    451 				/*
    452 				 * The block in question was not found
    453 				 * in the cache; i.e., the block that
    454 				 * getblk() returned is empty.	So, we
    455 				 * can (and should) copy in the
    456 				 * contents, because we've already
    457 				 * determined that this was the right
    458 				 * version of this block on disk.
    459 				 *
    460 				 * And, it can't have changed underneath
    461 				 * us, because we have the segment lock.
    462 				 */
    463 				error = copyin(blkp->bi_bp, bp->b_data, blkp->bi_size);
    464 				if (error)
    465 					goto err2;
    466 			}
    467 		}
    468 		if ((error = lfs_bwrite_ext(bp, BW_CLEAN)) != 0)
    469 			goto err2;
    470 
    471 		nblkwritten++;
    472 		/*
    473 		 * XXX should account indirect blocks and ifile pages as well
    474 		 */
    475 		if (nblkwritten + lfs_lblkno(fs, ninowritten * sizeof (struct ulfs1_dinode))
    476 		    > LFS_MARKV_MAX_BLOCKS) {
    477 			DLOG((DLOG_CLEAN, "lfs_markv: writing %d blks %d inos\n",
    478 			      nblkwritten, ninowritten));
    479 			lfs_segwrite(mntp, SEGM_CLEAN);
    480 			nblkwritten = ninowritten = 0;
    481 		}
    482 	}
    483 
    484 	/*
    485 	 * Finish the old file, if there was one
    486 	 */
    487 	if (v_daddr != LFS_UNUSED_DADDR) {
    488 		lfs_vunref(vp);
    489 		numrefed--;
    490 	}
    491 
    492 #ifdef DIAGNOSTIC
    493 	if (numrefed != 0)
    494 		panic("lfs_markv: numrefed=%d", numrefed);
    495 #endif
    496 	DLOG((DLOG_CLEAN, "lfs_markv: writing %d blks %d inos (check point)\n",
    497 	      nblkwritten, ninowritten));
    498 
    499 	/*
    500 	 * The last write has to be SEGM_SYNC, because of calling semantics.
    501 	 * It also has to be SEGM_CKP, because otherwise we could write
    502 	 * over the newly cleaned data contained in a checkpoint, and then
    503 	 * we'd be unhappy at recovery time.
    504 	 */
    505 	lfs_segwrite(mntp, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
    506 
    507 	lfs_segunlock(fs);
    508 
    509 	vfs_unbusy(mntp, false, NULL);
    510 	if (error)
    511 		return (error);
    512 	else if (do_again)
    513 		return EAGAIN;
    514 
    515 	return 0;
    516 
    517 err2:
    518 	DLOG((DLOG_CLEAN, "lfs_markv err2\n"));
    519 
    520 	/*
    521 	 * XXX we're here because copyin() failed.
    522 	 * XXX it means that we can't trust the cleanerd.  too bad.
    523 	 * XXX how can we recover from this?
    524 	 */
    525 
    526 err3:
    527 	KERNEL_UNLOCK_ONE(NULL);
    528 	/*
    529 	 * XXX should do segwrite here anyway?
    530 	 */
    531 
    532 	if (v_daddr != LFS_UNUSED_DADDR) {
    533 		lfs_vunref(vp);
    534 		--numrefed;
    535 	}
    536 
    537 	lfs_segunlock(fs);
    538 	vfs_unbusy(mntp, false, NULL);
    539 #ifdef DIAGNOSTIC
    540 	if (numrefed != 0)
    541 		panic("lfs_markv: numrefed=%d", numrefed);
    542 #endif
    543 
    544 	return (error);
    545 }
    546 
    547 /*
    548  * sys_lfs_bmapv:
    549  *
    550  * This will fill in the current disk address for arrays of blocks.
    551  *
    552  *  0 on success
    553  * -1/errno is return on error.
    554  */
    555 #ifdef USE_64BIT_SYSCALLS
    556 int
    557 sys_lfs_bmapv(struct lwp *l, const struct sys_lfs_bmapv_args *uap, register_t *retval)
    558 {
    559 	/* {
    560 		syscallarg(fsid_t *) fsidp;
    561 		syscallarg(struct block_info *) blkiov;
    562 		syscallarg(int) blkcnt;
    563 	} */
    564 	BLOCK_INFO *blkiov;
    565 	int blkcnt, error;
    566 	fsid_t fsid;
    567 	struct lfs *fs;
    568 	struct mount *mntp;
    569 
    570 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
    571 	    KAUTH_REQ_SYSTEM_LFS_BMAPV, NULL, NULL, NULL);
    572 	if (error)
    573 		return (error);
    574 
    575 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    576 		return (error);
    577 
    578 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    579 		return (ENOENT);
    580 	fs = VFSTOULFS(mntp)->um_lfs;
    581 
    582 	blkcnt = SCARG(uap, blkcnt);
    583 	if ((u_int) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
    584 		return (EINVAL);
    585 	KERNEL_LOCK(1, NULL);
    586 	blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
    587 	if ((error = copyin(SCARG(uap, blkiov), blkiov,
    588 			    blkcnt * sizeof(BLOCK_INFO))) != 0)
    589 		goto out;
    590 
    591 	if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0)
    592 		copyout(blkiov, SCARG(uap, blkiov),
    593 			blkcnt * sizeof(BLOCK_INFO));
    594     out:
    595 	lfs_free(fs, blkiov, LFS_NB_BLKIOV);
    596 	KERNEL_UNLOCK_ONE(NULL);
    597 	return error;
    598 }
    599 #else
    600 int
    601 sys_lfs_bmapv(struct lwp *l, const struct sys_lfs_bmapv_args *uap, register_t *retval)
    602 {
    603 	/* {
    604 		syscallarg(fsid_t *) fsidp;
    605 		syscallarg(struct block_info *) blkiov;
    606 		syscallarg(int) blkcnt;
    607 	} */
    608 	BLOCK_INFO *blkiov;
    609 	BLOCK_INFO_15 *blkiov15;
    610 	int i, blkcnt, error;
    611 	fsid_t fsid;
    612 	struct lfs *fs;
    613 	struct mount *mntp;
    614 
    615 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
    616 	    KAUTH_REQ_SYSTEM_LFS_BMAPV, NULL, NULL, NULL);
    617 	if (error)
    618 		return (error);
    619 
    620 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    621 		return (error);
    622 
    623 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    624 		return (ENOENT);
    625 	fs = VFSTOULFS(mntp)->um_lfs;
    626 
    627 	blkcnt = SCARG(uap, blkcnt);
    628 	if ((size_t) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
    629 		return (EINVAL);
    630 	KERNEL_LOCK(1, NULL);
    631 	blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
    632 	blkiov15 = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO_15), LFS_NB_BLKIOV);
    633 	if ((error = copyin(SCARG(uap, blkiov), blkiov15,
    634 			    blkcnt * sizeof(BLOCK_INFO_15))) != 0)
    635 		goto out;
    636 
    637 	for (i = 0; i < blkcnt; i++) {
    638 		blkiov[i].bi_inode     = blkiov15[i].bi_inode;
    639 		blkiov[i].bi_lbn       = blkiov15[i].bi_lbn;
    640 		blkiov[i].bi_daddr     = blkiov15[i].bi_daddr;
    641 		blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
    642 		blkiov[i].bi_version   = blkiov15[i].bi_version;
    643 		blkiov[i].bi_bp	       = blkiov15[i].bi_bp;
    644 		blkiov[i].bi_size      = blkiov15[i].bi_size;
    645 	}
    646 
    647 	if ((error = lfs_bmapv(l->l_proc, &fsid, blkiov, blkcnt)) == 0) {
    648 		for (i = 0; i < blkcnt; i++) {
    649 			blkiov15[i].bi_inode	 = blkiov[i].bi_inode;
    650 			blkiov15[i].bi_lbn	 = blkiov[i].bi_lbn;
    651 			blkiov15[i].bi_daddr	 = blkiov[i].bi_daddr;
    652 			blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
    653 			blkiov15[i].bi_version	 = blkiov[i].bi_version;
    654 			blkiov15[i].bi_bp	 = blkiov[i].bi_bp;
    655 			blkiov15[i].bi_size	 = blkiov[i].bi_size;
    656 		}
    657 		copyout(blkiov15, SCARG(uap, blkiov),
    658 			blkcnt * sizeof(BLOCK_INFO_15));
    659 	}
    660     out:
    661 	lfs_free(fs, blkiov, LFS_NB_BLKIOV);
    662 	lfs_free(fs, blkiov15, LFS_NB_BLKIOV);
    663 	KERNEL_UNLOCK_ONE(NULL);
    664 	return error;
    665 }
    666 #endif
    667 
    668 int
    669 lfs_bmapv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
    670 {
    671 	BLOCK_INFO *blkp;
    672 	IFILE *ifp;
    673 	struct buf *bp;
    674 	struct inode *ip = NULL;
    675 	struct lfs *fs;
    676 	struct mount *mntp;
    677 	struct ulfsmount *ump;
    678 	struct vnode *vp;
    679 	ino_t lastino;
    680 	daddr_t v_daddr;
    681 	int cnt, error;
    682 	int numrefed = 0;
    683 
    684 	lfs_cleaner_pid = p->p_pid;
    685 
    686 	if ((mntp = vfs_getvfs(fsidp)) == NULL)
    687 		return (ENOENT);
    688 
    689 	ump = VFSTOULFS(mntp);
    690 	if ((error = vfs_busy(mntp, NULL)) != 0)
    691 		return (error);
    692 
    693 	cnt = blkcnt;
    694 
    695 	fs = VFSTOULFS(mntp)->um_lfs;
    696 
    697 	error = 0;
    698 
    699 	/* these were inside the initialization for the for loop */
    700 	v_daddr = LFS_UNUSED_DADDR;
    701 	lastino = LFS_UNUSED_INUM;
    702 	for (blkp = blkiov; cnt--; ++blkp)
    703 	{
    704 		/*
    705 		 * Get the IFILE entry (only once) and see if the file still
    706 		 * exists.
    707 		 */
    708 		if (lastino != blkp->bi_inode) {
    709 			/*
    710 			 * Finish the old file, if there was one.  The presence
    711 			 * of a usable vnode in vp is signaled by a valid
    712 			 * v_daddr.
    713 			 */
    714 			if (v_daddr != LFS_UNUSED_DADDR) {
    715 				lfs_vunref(vp);
    716 				if (VTOI(vp)->i_lfs_iflags & LFSI_BMAP)
    717 					vrecycle(vp, NULL, NULL);
    718 				numrefed--;
    719 			}
    720 
    721 			/*
    722 			 * Start a new file
    723 			 */
    724 			lastino = blkp->bi_inode;
    725 			if (blkp->bi_inode == LFS_IFILE_INUM)
    726 				v_daddr = fs->lfs_idaddr;
    727 			else {
    728 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    729 				v_daddr = ifp->if_daddr;
    730 				brelse(bp, 0);
    731 			}
    732 			if (v_daddr == LFS_UNUSED_DADDR) {
    733 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    734 				continue;
    735 			}
    736 			/*
    737 			 * A regular call to VFS_VGET could deadlock
    738 			 * here.  Instead, we try an unlocked access.
    739 			 */
    740 			mutex_enter(&ulfs_ihash_lock);
    741 			vp = ulfs_ihashlookup(ump->um_dev, blkp->bi_inode);
    742 			if (vp != NULL && !(vp->v_iflag & VI_XLOCK)) {
    743 				ip = VTOI(vp);
    744 				mutex_enter(vp->v_interlock);
    745 				mutex_exit(&ulfs_ihash_lock);
    746 				if (lfs_vref(vp)) {
    747 					v_daddr = LFS_UNUSED_DADDR;
    748 					continue;
    749 				}
    750 				numrefed++;
    751 			} else {
    752 				mutex_exit(&ulfs_ihash_lock);
    753 				/*
    754 				 * Don't VFS_VGET if we're being unmounted,
    755 				 * since we hold vfs_busy().
    756 				 */
    757 				if (mntp->mnt_iflag & IMNT_UNMOUNT) {
    758 					v_daddr = LFS_UNUSED_DADDR;
    759 					continue;
    760 				}
    761 				error = VFS_VGET(mntp, blkp->bi_inode, &vp);
    762 				if (error) {
    763 					DLOG((DLOG_CLEAN, "lfs_bmapv: vget ino"
    764 					      "%d failed with %d",
    765 					      blkp->bi_inode,error));
    766 					v_daddr = LFS_UNUSED_DADDR;
    767 					continue;
    768 				} else {
    769 					KASSERT(VOP_ISLOCKED(vp));
    770 					VTOI(vp)->i_lfs_iflags |= LFSI_BMAP;
    771 					VOP_UNLOCK(vp);
    772 					numrefed++;
    773 				}
    774 			}
    775 			ip = VTOI(vp);
    776 		} else if (v_daddr == LFS_UNUSED_DADDR) {
    777 			/*
    778 			 * This can only happen if the vnode is dead.
    779 			 * Keep going.	Note that we DO NOT set the
    780 			 * bi_addr to anything -- if we failed to get
    781 			 * the vnode, for example, we want to assume
    782 			 * conservatively that all of its blocks *are*
    783 			 * located in the segment in question.
    784 			 * lfs_markv will throw them out if we are
    785 			 * wrong.
    786 			 */
    787 			/* blkp->bi_daddr = LFS_UNUSED_DADDR; */
    788 			continue;
    789 		}
    790 
    791 		/* Past this point we are guaranteed that vp, ip are valid. */
    792 
    793 		if (blkp->bi_lbn == LFS_UNUSED_LBN) {
    794 			/*
    795 			 * We just want the inode address, which is
    796 			 * conveniently in v_daddr.
    797 			 */
    798 			blkp->bi_daddr = v_daddr;
    799 		} else {
    800 			daddr_t bi_daddr;
    801 
    802 			/* XXX ondisk32 */
    803 			error = VOP_BMAP(vp, blkp->bi_lbn, NULL,
    804 					 &bi_daddr, NULL);
    805 			if (error)
    806 			{
    807 				blkp->bi_daddr = LFS_UNUSED_DADDR;
    808 				continue;
    809 			}
    810 			blkp->bi_daddr = LFS_DBTOFSB(fs, bi_daddr);
    811 			/* Fill in the block size, too */
    812 			if (blkp->bi_lbn >= 0)
    813 				blkp->bi_size = lfs_blksize(fs, ip, blkp->bi_lbn);
    814 			else
    815 				blkp->bi_size = fs->lfs_bsize;
    816 		}
    817 	}
    818 
    819 	/*
    820 	 * Finish the old file, if there was one.  The presence
    821 	 * of a usable vnode in vp is signaled by a valid v_daddr.
    822 	 */
    823 	if (v_daddr != LFS_UNUSED_DADDR) {
    824 		lfs_vunref(vp);
    825 		/* Recycle as above. */
    826 		if (ip->i_lfs_iflags & LFSI_BMAP)
    827 			vrecycle(vp, NULL, NULL);
    828 		numrefed--;
    829 	}
    830 
    831 #ifdef DIAGNOSTIC
    832 	if (numrefed != 0)
    833 		panic("lfs_bmapv: numrefed=%d", numrefed);
    834 #endif
    835 
    836 	vfs_unbusy(mntp, false, NULL);
    837 
    838 	return 0;
    839 }
    840 
    841 /*
    842  * sys_lfs_segclean:
    843  *
    844  * Mark the segment clean.
    845  *
    846  *  0 on success
    847  * -1/errno is return on error.
    848  */
    849 int
    850 sys_lfs_segclean(struct lwp *l, const struct sys_lfs_segclean_args *uap, register_t *retval)
    851 {
    852 	/* {
    853 		syscallarg(fsid_t *) fsidp;
    854 		syscallarg(u_long) segment;
    855 	} */
    856 	struct lfs *fs;
    857 	struct mount *mntp;
    858 	fsid_t fsid;
    859 	int error;
    860 	unsigned long segnum;
    861 
    862 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
    863 	    KAUTH_REQ_SYSTEM_LFS_SEGCLEAN, NULL, NULL, NULL);
    864 	if (error)
    865 		return (error);
    866 
    867 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    868 		return (error);
    869 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    870 		return (ENOENT);
    871 
    872 	fs = VFSTOULFS(mntp)->um_lfs;
    873 	segnum = SCARG(uap, segment);
    874 
    875 	if ((error = vfs_busy(mntp, NULL)) != 0)
    876 		return (error);
    877 
    878 	KERNEL_LOCK(1, NULL);
    879 	lfs_seglock(fs, SEGM_PROT);
    880 	error = lfs_do_segclean(fs, segnum);
    881 	lfs_segunlock(fs);
    882 	KERNEL_UNLOCK_ONE(NULL);
    883 	vfs_unbusy(mntp, false, NULL);
    884 	return error;
    885 }
    886 
    887 /*
    888  * Actually mark the segment clean.
    889  * Must be called with the segment lock held.
    890  */
    891 int
    892 lfs_do_segclean(struct lfs *fs, unsigned long segnum)
    893 {
    894 	extern int lfs_dostats;
    895 	struct buf *bp;
    896 	CLEANERINFO *cip;
    897 	SEGUSE *sup;
    898 
    899 	if (lfs_dtosn(fs, fs->lfs_curseg) == segnum) {
    900 		return (EBUSY);
    901 	}
    902 
    903 	LFS_SEGENTRY(sup, fs, segnum, bp);
    904 	if (sup->su_nbytes) {
    905 		DLOG((DLOG_CLEAN, "lfs_segclean: not cleaning segment %lu:"
    906 		      " %d live bytes\n", segnum, sup->su_nbytes));
    907 		brelse(bp, 0);
    908 		return (EBUSY);
    909 	}
    910 	if (sup->su_flags & SEGUSE_ACTIVE) {
    911 		DLOG((DLOG_CLEAN, "lfs_segclean: not cleaning segment %lu:"
    912 		      " segment is active\n", segnum));
    913 		brelse(bp, 0);
    914 		return (EBUSY);
    915 	}
    916 	if (!(sup->su_flags & SEGUSE_DIRTY)) {
    917 		DLOG((DLOG_CLEAN, "lfs_segclean: not cleaning segment %lu:"
    918 		      " segment is already clean\n", segnum));
    919 		brelse(bp, 0);
    920 		return (EALREADY);
    921 	}
    922 
    923 	fs->lfs_avail += lfs_segtod(fs, 1);
    924 	if (sup->su_flags & SEGUSE_SUPERBLOCK)
    925 		fs->lfs_avail -= lfs_btofsb(fs, LFS_SBPAD);
    926 	if (fs->lfs_version > 1 && segnum == 0 &&
    927 	    fs->lfs_start < lfs_btofsb(fs, LFS_LABELPAD))
    928 		fs->lfs_avail -= lfs_btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
    929 	mutex_enter(&lfs_lock);
    930 	fs->lfs_bfree += sup->su_nsums * lfs_btofsb(fs, fs->lfs_sumsize) +
    931 		lfs_btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    932 	fs->lfs_dmeta -= sup->su_nsums * lfs_btofsb(fs, fs->lfs_sumsize) +
    933 		lfs_btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
    934 	if (fs->lfs_dmeta < 0)
    935 		fs->lfs_dmeta = 0;
    936 	mutex_exit(&lfs_lock);
    937 	sup->su_flags &= ~SEGUSE_DIRTY;
    938 	LFS_WRITESEGENTRY(sup, fs, segnum, bp);
    939 
    940 	LFS_CLEANERINFO(cip, fs, bp);
    941 	++cip->clean;
    942 	--cip->dirty;
    943 	fs->lfs_nclean = cip->clean;
    944 	cip->bfree = fs->lfs_bfree;
    945 	mutex_enter(&lfs_lock);
    946 	cip->avail = fs->lfs_avail - fs->lfs_ravail - fs->lfs_favail;
    947 	wakeup(&fs->lfs_avail);
    948 	mutex_exit(&lfs_lock);
    949 	(void) LFS_BWRITE_LOG(bp);
    950 
    951 	if (lfs_dostats)
    952 		++lfs_stats.segs_reclaimed;
    953 
    954 	return (0);
    955 }
    956 
    957 /*
    958  * This will block until a segment in file system fsid is written.  A timeout
    959  * in milliseconds may be specified which will awake the cleaner automatically.
    960  * An fsid of -1 means any file system, and a timeout of 0 means forever.
    961  */
    962 int
    963 lfs_segwait(fsid_t *fsidp, struct timeval *tv)
    964 {
    965 	struct mount *mntp;
    966 	void *addr;
    967 	u_long timeout;
    968 	int error;
    969 
    970 	KERNEL_LOCK(1, NULL);
    971 	if (fsidp == NULL || (mntp = vfs_getvfs(fsidp)) == NULL)
    972 		addr = &lfs_allclean_wakeup;
    973 	else
    974 		addr = &VFSTOULFS(mntp)->um_lfs->lfs_nextseg;
    975 	/*
    976 	 * XXX THIS COULD SLEEP FOREVER IF TIMEOUT IS {0,0}!
    977 	 * XXX IS THAT WHAT IS INTENDED?
    978 	 */
    979 	timeout = tvtohz(tv);
    980 	error = tsleep(addr, PCATCH | PVFS, "segment", timeout);
    981 	KERNEL_UNLOCK_ONE(NULL);
    982 	return (error == ERESTART ? EINTR : 0);
    983 }
    984 
    985 /*
    986  * sys_lfs_segwait:
    987  *
    988  * System call wrapper around lfs_segwait().
    989  *
    990  *  0 on success
    991  *  1 on timeout
    992  * -1/errno is return on error.
    993  */
    994 int
    995 sys___lfs_segwait50(struct lwp *l, const struct sys___lfs_segwait50_args *uap,
    996     register_t *retval)
    997 {
    998 	/* {
    999 		syscallarg(fsid_t *) fsidp;
   1000 		syscallarg(struct timeval *) tv;
   1001 	} */
   1002 	struct timeval atv;
   1003 	fsid_t fsid;
   1004 	int error;
   1005 
   1006 	/* XXX need we be su to segwait? */
   1007 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
   1008 	    KAUTH_REQ_SYSTEM_LFS_SEGWAIT, NULL, NULL, NULL);
   1009 	if (error)
   1010 		return (error);
   1011 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
   1012 		return (error);
   1013 
   1014 	if (SCARG(uap, tv)) {
   1015 		error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
   1016 		if (error)
   1017 			return (error);
   1018 		if (itimerfix(&atv))
   1019 			return (EINVAL);
   1020 	} else /* NULL or invalid */
   1021 		atv.tv_sec = atv.tv_usec = 0;
   1022 	return lfs_segwait(&fsid, &atv);
   1023 }
   1024 
   1025 /*
   1026  * VFS_VGET call specialized for the cleaner.  The cleaner already knows the
   1027  * daddr from the ifile, so don't look it up again.  If the cleaner is
   1028  * processing IINFO structures, it may have the ondisk inode already, so
   1029  * don't go retrieving it again.
   1030  *
   1031  * we lfs_vref, and it is the caller's responsibility to lfs_vunref
   1032  * when finished.
   1033  */
   1034 
   1035 int
   1036 lfs_fasthashget(dev_t dev, ino_t ino, struct vnode **vpp)
   1037 {
   1038 	struct vnode *vp;
   1039 
   1040 	mutex_enter(&ulfs_ihash_lock);
   1041 	if ((vp = ulfs_ihashlookup(dev, ino)) != NULL) {
   1042 		mutex_enter(vp->v_interlock);
   1043 		mutex_exit(&ulfs_ihash_lock);
   1044 		if (vp->v_iflag & VI_XLOCK) {
   1045 			DLOG((DLOG_CLEAN, "lfs_fastvget: ino %d VI_XLOCK\n",
   1046 			      ino));
   1047 			lfs_stats.clean_vnlocked++;
   1048 			mutex_exit(vp->v_interlock);
   1049 			return EAGAIN;
   1050 		}
   1051 		if (lfs_vref(vp)) {
   1052 			DLOG((DLOG_CLEAN, "lfs_fastvget: lfs_vref failed"
   1053 			      " for ino %d\n", ino));
   1054 			lfs_stats.clean_inlocked++;
   1055 			return EAGAIN;
   1056 		}
   1057 	} else {
   1058 		mutex_exit(&ulfs_ihash_lock);
   1059 	}
   1060 	*vpp = vp;
   1061 
   1062 	return (0);
   1063 }
   1064 
   1065 int
   1066 lfs_fastvget(struct mount *mp, ino_t ino, daddr_t daddr, struct vnode **vpp,
   1067 	     struct ulfs1_dinode *dinp)
   1068 {
   1069 	struct inode *ip;
   1070 	struct ulfs1_dinode *dip;
   1071 	struct vnode *vp;
   1072 	struct ulfsmount *ump;
   1073 	dev_t dev;
   1074 	int error, retries;
   1075 	struct buf *bp;
   1076 	struct lfs *fs;
   1077 
   1078 	ump = VFSTOULFS(mp);
   1079 	dev = ump->um_dev;
   1080 	fs = ump->um_lfs;
   1081 
   1082 	/*
   1083 	 * Wait until the filesystem is fully mounted before allowing vget
   1084 	 * to complete.	 This prevents possible problems with roll-forward.
   1085 	 */
   1086 	mutex_enter(&lfs_lock);
   1087 	while (fs->lfs_flags & LFS_NOTYET) {
   1088 		mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_fnotyet", 0,
   1089 			&lfs_lock);
   1090 	}
   1091 	mutex_exit(&lfs_lock);
   1092 
   1093 	/*
   1094 	 * This is playing fast and loose.  Someone may have the inode
   1095 	 * locked, in which case they are going to be distinctly unhappy
   1096 	 * if we trash something.
   1097 	 */
   1098 
   1099 	error = lfs_fasthashget(dev, ino, vpp);
   1100 	if (error != 0 || *vpp != NULL)
   1101 		return (error);
   1102 
   1103 	/*
   1104 	 * getnewvnode(9) will call vfs_busy, which will block if the
   1105 	 * filesystem is being unmounted; but umount(9) is waiting for
   1106 	 * us because we're already holding the fs busy.
   1107 	 * XXXMP
   1108 	 */
   1109 	if (mp->mnt_iflag & IMNT_UNMOUNT) {
   1110 		*vpp = NULL;
   1111 		return EDEADLK;
   1112 	}
   1113 	error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, NULL, &vp);
   1114 	if (error) {
   1115 		*vpp = NULL;
   1116 		return (error);
   1117 	}
   1118 
   1119 	mutex_enter(&ulfs_hashlock);
   1120 	error = lfs_fasthashget(dev, ino, vpp);
   1121 	if (error != 0 || *vpp != NULL) {
   1122 		mutex_exit(&ulfs_hashlock);
   1123 		ungetnewvnode(vp);
   1124 		return (error);
   1125 	}
   1126 
   1127 	/* Allocate new vnode/inode. */
   1128 	lfs_vcreate(mp, ino, vp);
   1129 
   1130 	/*
   1131 	 * Put it onto its hash chain and lock it so that other requests for
   1132 	 * this inode will block if they arrive while we are sleeping waiting
   1133 	 * for old data structures to be purged or for the contents of the
   1134 	 * disk portion of this inode to be read.
   1135 	 */
   1136 	ip = VTOI(vp);
   1137 	ulfs_ihashins(ip);
   1138 	mutex_exit(&ulfs_hashlock);
   1139 
   1140 #ifdef notyet
   1141 	/* Not found in the cache => this vnode was loaded only for cleaning. */
   1142 	ip->i_lfs_iflags |= LFSI_BMAP;
   1143 #endif
   1144 
   1145 	/*
   1146 	 * XXX
   1147 	 * This may not need to be here, logically it should go down with
   1148 	 * the i_devvp initialization.
   1149 	 * Ask Kirk.
   1150 	 */
   1151 	ip->i_lfs = fs;
   1152 
   1153 	/* Read in the disk contents for the inode, copy into the inode. */
   1154 	if (dinp) {
   1155 		error = copyin(dinp, ip->i_din.ffs1_din, sizeof (struct ulfs1_dinode));
   1156 		if (error) {
   1157 			DLOG((DLOG_CLEAN, "lfs_fastvget: dinode copyin failed"
   1158 			      " for ino %d\n", ino));
   1159 			ulfs_ihashrem(ip);
   1160 
   1161 			/* Unlock and discard unneeded inode. */
   1162 			VOP_UNLOCK(vp);
   1163 			lfs_vunref(vp);
   1164 			*vpp = NULL;
   1165 			return (error);
   1166 		}
   1167 		if (ip->i_number != ino)
   1168 			panic("lfs_fastvget: I was fed the wrong inode!");
   1169 	} else {
   1170 		retries = 0;
   1171 	    again:
   1172 		error = bread(ump->um_devvp, LFS_FSBTODB(fs, daddr), fs->lfs_ibsize,
   1173 			      NOCRED, 0, &bp);
   1174 		if (error) {
   1175 			DLOG((DLOG_CLEAN, "lfs_fastvget: bread failed (%d)\n",
   1176 			      error));
   1177 			/*
   1178 			 * The inode does not contain anything useful, so it
   1179 			 * would be misleading to leave it on its hash chain.
   1180 			 * Iput() will return it to the free list.
   1181 			 */
   1182 			ulfs_ihashrem(ip);
   1183 
   1184 			/* Unlock and discard unneeded inode. */
   1185 			VOP_UNLOCK(vp);
   1186 			lfs_vunref(vp);
   1187 			*vpp = NULL;
   1188 			return (error);
   1189 		}
   1190 		dip = lfs_ifind(ump->um_lfs, ino, bp);
   1191 		if (dip == NULL) {
   1192 			/* Assume write has not completed yet; try again */
   1193 			brelse(bp, BC_INVAL);
   1194 			++retries;
   1195 			if (retries > LFS_IFIND_RETRIES)
   1196 				panic("lfs_fastvget: dinode not found");
   1197 			DLOG((DLOG_CLEAN, "lfs_fastvget: dinode not found,"
   1198 			      " retrying...\n"));
   1199 			goto again;
   1200 		}
   1201 		*ip->i_din.ffs1_din = *dip;
   1202 		brelse(bp, 0);
   1203 	}
   1204 	lfs_vinit(mp, &vp);
   1205 
   1206 	*vpp = vp;
   1207 
   1208 	KASSERT(VOP_ISLOCKED(vp));
   1209 	VOP_UNLOCK(vp);
   1210 
   1211 	return (0);
   1212 }
   1213 
   1214 /*
   1215  * Make up a "fake" cleaner buffer, copy the data from userland into it.
   1216  */
   1217 struct buf *
   1218 lfs_fakebuf(struct lfs *fs, struct vnode *vp, int lbn, size_t size, void *uaddr)
   1219 {
   1220 	struct buf *bp;
   1221 	int error;
   1222 
   1223 	KASSERT(VTOI(vp)->i_number != LFS_IFILE_INUM);
   1224 
   1225 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size, LFS_NB_CLEAN);
   1226 	error = copyin(uaddr, bp->b_data, size);
   1227 	if (error) {
   1228 		lfs_freebuf(fs, bp);
   1229 		return NULL;
   1230 	}
   1231 	KDASSERT(bp->b_iodone == lfs_callback);
   1232 
   1233 #if 0
   1234 	mutex_enter(&lfs_lock);
   1235 	++fs->lfs_iocount;
   1236 	mutex_exit(&lfs_lock);
   1237 #endif
   1238 	bp->b_bufsize = size;
   1239 	bp->b_bcount = size;
   1240 	return (bp);
   1241 }
   1242