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