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