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