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