Home | History | Annotate | Line # | Download | only in lfs
lfs_syscalls.c revision 1.86
      1 /*	$NetBSD: lfs_syscalls.c,v 1.86 2003/03/08 23:18:54 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. 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.86 2003/03/08 23:18:54 perseant 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 extern int lfs_subsys_pages;
    110 extern struct simplelock lfs_subsys_lock;
    111 
    112 /*
    113  * Definitions for the buffer free lists.
    114  */
    115 #define BQUEUES		4		/* number of free buffer queues */
    116 
    117 #define BQ_LOCKED	0		/* super-blocks &c */
    118 #define BQ_LRU		1		/* lru, useful buffers */
    119 #define BQ_AGE		2		/* rubbish */
    120 #define BQ_EMPTY	3		/* buffer headers with no memory */
    121 
    122 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    123 
    124 #define LFS_FORCE_WRITE UNASSIGNED
    125 
    126 #define LFS_VREF_THRESHOLD 128
    127 
    128 /*
    129  * sys_lfs_markv:
    130  *
    131  * This will mark inodes and blocks dirty, so they are written into the log.
    132  * It will block until all the blocks have been written.  The segment create
    133  * time passed in the block_info and inode_info structures is used to decide
    134  * if the data is valid for each block (in case some process dirtied a block
    135  * or inode that is being cleaned between the determination that a block is
    136  * live and the lfs_markv call).
    137  *
    138  *  0 on success
    139  * -1/errno is return on error.
    140  */
    141 #ifdef USE_64BIT_SYSCALLS
    142 int
    143 sys_lfs_markv(struct proc *p, void *v, register_t *retval)
    144 {
    145 	struct sys_lfs_markv_args /* {
    146 		syscallarg(fsid_t *) fsidp;
    147 		syscallarg(struct block_info *) blkiov;
    148 		syscallarg(int) blkcnt;
    149 	} */ *uap = v;
    150 	BLOCK_INFO *blkiov;
    151 	int blkcnt, error;
    152 	fsid_t fsid;
    153 
    154 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    155 		return (error);
    156 
    157 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    158 		return (error);
    159 
    160 	blkcnt = SCARG(uap, blkcnt);
    161 	if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
    162 		return (EINVAL);
    163 
    164 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    165 	if ((error = copyin(SCARG(uap, blkiov), blkiov,
    166 			    blkcnt * sizeof(BLOCK_INFO))) != 0)
    167 		goto out;
    168 
    169 	if ((error = lfs_markv(p, &fsid, blkiov, blkcnt)) == 0)
    170 		copyout(blkiov, SCARG(uap, blkiov),
    171 			blkcnt * sizeof(BLOCK_INFO));
    172     out:
    173 	free(blkiov, M_SEGMENT);
    174 	return error;
    175 }
    176 #else
    177 int
    178 sys_lfs_markv(struct lwp *l, void *v, register_t *retval)
    179 {
    180 	struct sys_lfs_markv_args /* {
    181 		syscallarg(fsid_t *) fsidp;
    182 		syscallarg(struct block_info *) blkiov;
    183 		syscallarg(int) blkcnt;
    184 	} */ *uap = v;
    185 	BLOCK_INFO *blkiov;
    186 	BLOCK_INFO_15 *blkiov15;
    187 	int i, blkcnt, error;
    188 	fsid_t fsid;
    189 
    190 	if ((error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
    191 		return (error);
    192 
    193 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    194 		return (error);
    195 
    196 	blkcnt = SCARG(uap, blkcnt);
    197 	if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
    198 		return (EINVAL);
    199 
    200 	blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    201 	blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
    202 	if ((error = copyin(SCARG(uap, blkiov), blkiov15,
    203 			    blkcnt * sizeof(BLOCK_INFO_15))) != 0)
    204 		goto out;
    205 
    206 	for (i = 0; i < blkcnt; i++) {
    207 		blkiov[i].bi_inode     = blkiov15[i].bi_inode;
    208 		blkiov[i].bi_lbn       = blkiov15[i].bi_lbn;
    209 		blkiov[i].bi_daddr     = blkiov15[i].bi_daddr;
    210 		blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
    211 		blkiov[i].bi_version   = blkiov15[i].bi_version;
    212 		blkiov[i].bi_bp	       = blkiov15[i].bi_bp;
    213 		blkiov[i].bi_size      = blkiov15[i].bi_size;
    214 	}
    215 
    216 	if ((error = lfs_markv(l->l_proc, &fsid, blkiov, blkcnt)) == 0) {
    217 		for (i = 0; i < blkcnt; i++) {
    218 			blkiov15[i].bi_inode	 = blkiov[i].bi_inode;
    219 			blkiov15[i].bi_lbn	 = blkiov[i].bi_lbn;
    220 			blkiov15[i].bi_daddr	 = blkiov[i].bi_daddr;
    221 			blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
    222 			blkiov15[i].bi_version	 = blkiov[i].bi_version;
    223 			blkiov15[i].bi_bp	 = blkiov[i].bi_bp;
    224 			blkiov15[i].bi_size	 = blkiov[i].bi_size;
    225 		}
    226 		copyout(blkiov15, SCARG(uap, blkiov),
    227 			blkcnt * sizeof(BLOCK_INFO_15));
    228 	}
    229     out:
    230 	free(blkiov, M_SEGMENT);
    231 	free(blkiov15, M_SEGMENT);
    232 	return error;
    233 }
    234 #endif
    235 
    236 #define	LFS_MARKV_MAX_BLOCKS	(LFS_MAX_BUFS)
    237 
    238 int
    239 lfs_markv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
    240 {
    241 	BLOCK_INFO *blkp;
    242 	IFILE *ifp;
    243 	struct buf *bp, *nbp;
    244 	struct inode *ip = NULL;
    245 	struct lfs *fs;
    246 	struct mount *mntp;
    247 	struct vnode *vp;
    248 #ifdef DEBUG_LFS
    249 	int vputc = 0, iwritten = 0;
    250 #endif
    251 	ino_t lastino;
    252 	daddr_t b_daddr, v_daddr;
    253 	int cnt, error;
    254 	int do_again = 0;
    255 	int s;
    256 #ifdef CHECK_COPYIN
    257 	int i;
    258 #endif /* CHECK_COPYIN */
    259 	int numrefed = 0;
    260 	ino_t maxino;
    261 	size_t obsize;
    262 
    263 	/* number of blocks/inodes that we have already bwrite'ed */
    264 	int nblkwritten, ninowritten;
    265 
    266 	if ((mntp = vfs_getvfs(fsidp)) == NULL)
    267 		return (ENOENT);
    268 
    269 	fs = VFSTOUFS(mntp)->um_lfs;
    270 	maxino = (fragstoblks(fs, fsbtofrags(fs, VTOI(fs->lfs_ivnode)->i_ffs_blocks)) -
    271 		      fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    272 
    273 	cnt = blkcnt;
    274 
    275 	if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
    276 		return (error);
    277 
    278 	/*
    279 	 * This seglock is just to prevent the fact that we might have to sleep
    280 	 * from allowing the possibility that our blocks might become
    281 	 * invalid.
    282 	 *
    283 	 * It is also important to note here that unless we specify SEGM_CKP,
    284 	 * any Ifile blocks that we might be asked to clean will never get
    285 	 * to the disk.
    286 	 */
    287 	lfs_seglock(fs, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
    288 
    289 	/* Mark blocks/inodes dirty.  */
    290 	error = 0;
    291 
    292 #ifdef DEBUG_LFS
    293 	/* Run through and count the inodes */
    294 	lastino = LFS_UNUSED_INUM;
    295 	for (blkp = blkiov; cnt--; ++blkp) {
    296 		if (lastino != blkp->bi_inode) {
    297 			lastino = blkp->bi_inode;
    298 			vputc++;
    299 		}
    300 	}
    301 	cnt = blkcnt;
    302 	printf("[%d/",vputc);
    303 	iwritten = 0;
    304 #endif /* DEBUG_LFS */
    305 	/* these were inside the initialization for the for loop */
    306 	v_daddr = LFS_UNUSED_DADDR;
    307 	lastino = LFS_UNUSED_INUM;
    308 	nblkwritten = ninowritten = 0;
    309 	for (blkp = blkiov; cnt--; ++blkp)
    310 	{
    311 		if (blkp->bi_daddr == LFS_FORCE_WRITE)
    312 			printf("lfs_markv: warning: force-writing ino %d "
    313 			       "lbn %lld\n",
    314 			    blkp->bi_inode, (long long)blkp->bi_lbn);
    315 		/* Bounds-check incoming data, avoid panic for failed VGET */
    316 		if (blkp->bi_inode <= 0 || blkp->bi_inode >= maxino) {
    317 			error = EINVAL;
    318 			goto again;
    319 		}
    320 		/*
    321 		 * Get the IFILE entry (only once) and see if the file still
    322 		 * exists.
    323 		 */
    324 		if (lastino != blkp->bi_inode) {
    325 			/*
    326 			 * Finish the old file, if there was one.  The presence
    327 			 * of a usable vnode in vp is signaled by a valid v_daddr.
    328 			 */
    329 			if (v_daddr != LFS_UNUSED_DADDR) {
    330 #ifdef DEBUG_LFS
    331 				if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
    332 					iwritten++;
    333 #endif
    334 				lfs_vunref(vp);
    335 				numrefed--;
    336 			}
    337 
    338 			/*
    339 			 * Start a new file
    340 			 */
    341 			lastino = blkp->bi_inode;
    342 			if (blkp->bi_inode == LFS_IFILE_INUM)
    343 				v_daddr = fs->lfs_idaddr;
    344 			else {
    345 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    346 				/* XXX fix for force write */
    347 				v_daddr = ifp->if_daddr;
    348 				brelse(bp);
    349 			}
    350 			/* Don't force-write the ifile */
    351 			if (blkp->bi_inode == LFS_IFILE_INUM
    352 			    && blkp->bi_daddr == LFS_FORCE_WRITE)
    353 			{
    354 				continue;
    355 			}
    356 			if (v_daddr == LFS_UNUSED_DADDR
    357 			    && blkp->bi_daddr != LFS_FORCE_WRITE)
    358 			{
    359 				continue;
    360 			}
    361 
    362 			/* Get the vnode/inode. */
    363 			error = lfs_fastvget(mntp, blkp->bi_inode, v_daddr,
    364 					   &vp,
    365 					   (blkp->bi_lbn == LFS_UNUSED_LBN
    366 					    ? blkp->bi_bp
    367 					    : NULL));
    368 
    369 			if (!error) {
    370 				numrefed++;
    371 			}
    372 			if (error) {
    373 #ifdef DEBUG_LFS
    374 				printf("lfs_markv: lfs_fastvget failed with %d (ino %d, segment %d)\n",
    375 				       error, blkp->bi_inode,
    376 				       dtosn(fs, blkp->bi_daddr));
    377 #endif /* DEBUG_LFS */
    378 				/*
    379 				 * If we got EAGAIN, that means that the
    380 				 * Inode was locked.  This is
    381 				 * recoverable: just clean the rest of
    382 				 * this segment, and let the cleaner try
    383 				 * again with another.	(When the
    384 				 * cleaner runs again, this segment will
    385 				 * sort high on the list, since it is
    386 				 * now almost entirely empty.) But, we
    387 				 * still set v_daddr = LFS_UNUSED_ADDR
    388 				 * so as not to test this over and over
    389 				 * again.
    390 				 */
    391 				if (error == EAGAIN) {
    392 					error = 0;
    393 					do_again++;
    394 				}
    395 #ifdef DIAGNOSTIC
    396 				else if (error != ENOENT)
    397 					panic("lfs_markv VFS_VGET FAILED");
    398 #endif
    399 				/* lastino = LFS_UNUSED_INUM; */
    400 				v_daddr = LFS_UNUSED_DADDR;
    401 				vp = NULL;
    402 				ip = NULL;
    403 				continue;
    404 			}
    405 			ip = VTOI(vp);
    406 			ninowritten++;
    407 		} else if (v_daddr == LFS_UNUSED_DADDR) {
    408 			/*
    409 			 * This can only happen if the vnode is dead (or
    410 			 * in any case we can't get it...e.g., it is
    411 			 * inlocked).  Keep going.
    412 			 */
    413 			continue;
    414 		}
    415 
    416 		/* Past this point we are guaranteed that vp, ip are valid. */
    417 
    418 		/* If this BLOCK_INFO didn't contain a block, keep going. */
    419 		if (blkp->bi_lbn == LFS_UNUSED_LBN) {
    420 			/* XXX need to make sure that the inode gets written in this case */
    421 			/* XXX but only write the inode if it's the right one */
    422 			if (blkp->bi_inode != LFS_IFILE_INUM) {
    423 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    424 				if (ifp->if_daddr == blkp->bi_daddr
    425 				   || blkp->bi_daddr == LFS_FORCE_WRITE)
    426 				{
    427 					LFS_SET_UINO(ip, IN_CLEANING);
    428 				}
    429 				brelse(bp);
    430 			}
    431 			continue;
    432 		}
    433 
    434 		b_daddr = 0;
    435 		if (blkp->bi_daddr != LFS_FORCE_WRITE) {
    436 			if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
    437 			    dbtofsb(fs, b_daddr) != blkp->bi_daddr)
    438 			{
    439 				if (dtosn(fs,dbtofsb(fs, b_daddr))
    440 				   == dtosn(fs,blkp->bi_daddr))
    441 				{
    442 					printf("lfs_markv: wrong da same seg: %llx vs %llx\n",
    443 					       (long long)blkp->bi_daddr, (long long)dbtofsb(fs, b_daddr));
    444 				}
    445 				do_again++;
    446 				continue;
    447 			}
    448 		}
    449 
    450 		/*
    451 		 * Check block sizes.  The blocks being cleaned come from
    452 		 * disk, so they should have the same size as their on-disk
    453 		 * counterparts.
    454 		 */
    455 		if (blkp->bi_lbn >= 0)
    456 			obsize = blksize(fs, ip, blkp->bi_lbn);
    457 		else
    458 			obsize = fs->lfs_bsize;
    459 		/* Check for fragment size change */
    460 		if (blkp->bi_lbn >= 0 && blkp->bi_lbn < NDADDR) {
    461 			obsize = ip->i_lfs_fragsize[blkp->bi_lbn];
    462 		}
    463 		if (obsize != blkp->bi_size) {
    464 			printf("lfs_markv: ino %d lbn %lld wrong size (%ld != %d), try again\n",
    465 				blkp->bi_inode, (long long)blkp->bi_lbn,
    466 				(long) obsize, blkp->bi_size);
    467 			do_again++;
    468 			continue;
    469 		}
    470 
    471 		/*
    472 		 * If we get to here, then we are keeping the block.  If
    473 		 * it is an indirect block, we want to actually put it
    474 		 * in the buffer cache so that it can be updated in the
    475 		 * finish_meta section.	 If it's not, we need to
    476 		 * allocate a fake buffer so that writeseg can perform
    477 		 * the copyin and write the buffer.
    478 		 */
    479 		if (ip->i_number != LFS_IFILE_INUM && blkp->bi_lbn >= 0) {
    480 			/* Data Block */
    481 			bp = lfs_fakebuf(fs, vp, blkp->bi_lbn,
    482 					 blkp->bi_size, blkp->bi_bp);
    483 			/* Pretend we used bread() to get it */
    484 			bp->b_blkno = fsbtodb(fs, blkp->bi_daddr);
    485 		} else {
    486 			/* Indirect block or ifile */
    487 			if (blkp->bi_size != fs->lfs_bsize &&
    488 			    ip->i_number != LFS_IFILE_INUM)
    489 				panic("lfs_markv: partial indirect block?"
    490 				    " size=%d\n", blkp->bi_size);
    491 			bp = getblk(vp, blkp->bi_lbn, blkp->bi_size, 0, 0);
    492 			if (!(bp->b_flags & (B_DONE|B_DELWRI))) { /* B_CACHE */
    493 				/*
    494 				 * The block in question was not found
    495 				 * in the cache; i.e., the block that
    496 				 * getblk() returned is empty.	So, we
    497 				 * can (and should) copy in the
    498 				 * contents, because we've already
    499 				 * determined that this was the right
    500 				 * version of this block on disk.
    501 				 *
    502 				 * And, it can't have changed underneath
    503 				 * us, because we have the segment lock.
    504 				 */
    505 				error = copyin(blkp->bi_bp, bp->b_data, blkp->bi_size);
    506 				if (error)
    507 					goto err2;
    508 			}
    509 		}
    510 		if ((error = lfs_bwrite_ext(bp,BW_CLEAN)) != 0)
    511 			goto err2;
    512 
    513 		nblkwritten++;
    514 		/*
    515 		 * XXX should account indirect blocks and ifile pages as well
    516 		 */
    517 		if (nblkwritten + lblkno(fs, ninowritten * DINODE_SIZE)
    518 		    > LFS_MARKV_MAX_BLOCKS) {
    519 #ifdef DEBUG_LFS
    520 			printf("lfs_markv: writing %d blks %d inos\n",
    521 			    nblkwritten, ninowritten);
    522 #endif
    523 			lfs_segwrite(mntp, SEGM_CLEAN);
    524 			nblkwritten = ninowritten = 0;
    525 		}
    526 	}
    527 
    528 	/*
    529 	 * Finish the old file, if there was one
    530 	 */
    531 	if (v_daddr != LFS_UNUSED_DADDR) {
    532 #ifdef DEBUG_LFS
    533 		if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
    534 			iwritten++;
    535 #endif
    536 		lfs_vunref(vp);
    537 		numrefed--;
    538 	}
    539 
    540 #ifdef DEBUG_LFS
    541 	printf("%d]",iwritten);
    542 	if (numrefed != 0) {
    543 		panic("lfs_markv: numrefed=%d", numrefed);
    544 	}
    545 #endif
    546 
    547 #ifdef DEBUG_LFS
    548 	printf("lfs_markv: writing %d blks %d inos (check point)\n",
    549 	    nblkwritten, ninowritten);
    550 #endif
    551 	/*
    552 	 * The last write has to be SEGM_SYNC, because of calling semantics.
    553 	 * It also has to be SEGM_CKP, because otherwise we could write
    554 	 * over the newly cleaned data contained in a checkpoint, and then
    555 	 * we'd be unhappy at recovery time.
    556 	 */
    557 	lfs_segwrite(mntp, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
    558 
    559 	lfs_segunlock(fs);
    560 
    561 	vfs_unbusy(mntp);
    562 	if (error)
    563 		return (error);
    564 	else if (do_again)
    565 		return EAGAIN;
    566 
    567 	return 0;
    568 
    569  err2:
    570 	printf("lfs_markv err2\n");
    571 	lfs_vunref(vp);
    572 	--numrefed;
    573 
    574 	/* Free up fakebuffers -- have to take these from the LOCKED list */
    575  again:
    576 	s = splbio();
    577 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp; bp = nbp) {
    578 		nbp = bp->b_freelist.tqe_next;
    579 		if (LFS_IS_MALLOC_BUF(bp)) {
    580 			if (bp->b_flags & B_BUSY) { /* not bloody likely */
    581 				bp->b_flags |= B_WANTED;
    582 				tsleep(bp, PRIBIO+1, "markv", 0);
    583 				splx(s);
    584 				goto again;
    585 			}
    586 			if (bp->b_flags & B_DELWRI)
    587 				fs->lfs_avail += btofsb(fs, bp->b_bcount);
    588 			bremfree(bp);
    589 			splx(s);
    590 			brelse(bp);
    591 			s = splbio();
    592 		}
    593 	}
    594 	splx(s);
    595 	lfs_segunlock(fs);
    596 	vfs_unbusy(mntp);
    597 #ifdef DEBUG_LFS
    598 	if (numrefed != 0) {
    599 		panic("lfs_markv: numrefed=%d", numrefed);
    600 	}
    601 #endif
    602 
    603 	return (error);
    604 }
    605 
    606 /*
    607  * sys_lfs_bmapv:
    608  *
    609  * This will fill in the current disk address for arrays of blocks.
    610  *
    611  *  0 on success
    612  * -1/errno is return on error.
    613  */
    614 #ifdef USE_64BIT_SYSCALLS
    615 int
    616 sys_lfs_bmapv(struct proc *p, void *v, register_t *retval)
    617 {
    618 	struct sys_lfs_bmapv_args /* {
    619 		syscallarg(fsid_t *) fsidp;
    620 		syscallarg(struct block_info *) blkiov;
    621 		syscallarg(int) blkcnt;
    622 	} */ *uap = v;
    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 ((u_int) 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(p, &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 proc *p, 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 = p->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 dinode *dinp)
   1074 {
   1075 	struct inode *ip;
   1076 	struct dinode *dip;
   1077 	struct vnode *vp;
   1078 	struct ufsmount *ump;
   1079 	dev_t dev;
   1080 	int i, 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.ffs_din, DINODE_SIZE);
   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.ffs_din = *dip;
   1187 		brelse(bp);
   1188 	}
   1189 	ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1190 	ip->i_lfs_effnblks = ip->i_ffs_blocks;
   1191 	ip->i_lfs_osize = ip->i_ffs_size;
   1192 
   1193 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
   1194 	for (i = 0; i < NDADDR; i++)
   1195 		if (ip->i_ffs_db[i] != 0)
   1196 			ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
   1197 
   1198 	/*
   1199 	 * Initialize the vnode from the inode, check for aliases.  In all
   1200 	 * cases re-init ip, the underlying vnode/inode may have changed.
   1201 	 */
   1202 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   1203 #ifdef DEBUG_LFS
   1204 	if (vp->v_type == VNON) {
   1205 		printf("lfs_fastvget: ino %d is type VNON! (ifmt=%o, dinp=%p)\n",
   1206 		       ip->i_number, (ip->i_ffs_mode & IFMT) >> 12, dinp);
   1207 		lfs_dump_dinode(&ip->i_din.ffs_din);
   1208 #ifdef DDB
   1209 		Debugger();
   1210 #endif
   1211 	}
   1212 #endif /* DEBUG_LFS */
   1213 	/*
   1214 	 * Finish inode initialization now that aliasing has been resolved.
   1215 	 */
   1216 
   1217 	genfs_node_init(vp, &lfs_genfsops);
   1218 	ip->i_devvp = ump->um_devvp;
   1219 	VREF(ip->i_devvp);
   1220 	*vpp = vp;
   1221 	KASSERT(VOP_ISLOCKED(vp));
   1222 	VOP_UNLOCK(vp, 0);
   1223 
   1224 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1225 
   1226 	return (0);
   1227 }
   1228 
   1229 /*
   1230  * Make up a "fake" cleaner buffer, copy the data from userland into it.
   1231  */
   1232 struct buf *
   1233 lfs_fakebuf(struct lfs *fs, struct vnode *vp, int lbn, size_t size, caddr_t uaddr)
   1234 {
   1235 	struct buf *bp;
   1236 	int error;
   1237 
   1238 	KASSERT(VTOI(vp)->i_number != LFS_IFILE_INUM);
   1239 
   1240 	bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size, LFS_NB_CLEAN);
   1241 	error = copyin(uaddr, bp->b_data, size);
   1242 	if (error) {
   1243 		lfs_freebuf(fs, bp);
   1244 		return NULL;
   1245 	}
   1246 	KDASSERT(bp->b_iodone == lfs_callback);
   1247 
   1248 #if 0
   1249 	bp->b_saveaddr = (caddr_t)fs;
   1250 	++fs->lfs_iocount;
   1251 #endif
   1252 	bp->b_bufsize = size;
   1253 	bp->b_bcount = size;
   1254 	return (bp);
   1255 }
   1256