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