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