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