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