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