Home | History | Annotate | Line # | Download | only in lfs
lfs_syscalls.c revision 1.16
      1  1.16      fvdl /*	$NetBSD: lfs_syscalls.c,v 1.16 1998/03/01 02:23:25 fvdl Exp $	*/
      2   1.3       cgd 
      3   1.1   mycroft /*-
      4   1.1   mycroft  * Copyright (c) 1991, 1993, 1994
      5   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      8   1.1   mycroft  * modification, are permitted provided that the following conditions
      9   1.1   mycroft  * are met:
     10   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     11   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     12   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     15   1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     16   1.1   mycroft  *    must display the following acknowledgement:
     17   1.1   mycroft  *	This product includes software developed by the University of
     18   1.1   mycroft  *	California, Berkeley and its contributors.
     19   1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     20   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     21   1.1   mycroft  *    without specific prior written permission.
     22   1.1   mycroft  *
     23   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1   mycroft  * SUCH DAMAGE.
     34   1.1   mycroft  *
     35  1.16      fvdl  *	@(#)lfs_syscalls.c	8.10 (Berkeley) 5/14/95
     36   1.1   mycroft  */
     37  1.15   thorpej 
     38  1.15   thorpej #include "fs_lfs.h"		/* for prototypes in syscallargs.h */
     39   1.1   mycroft 
     40   1.1   mycroft #include <sys/param.h>
     41   1.5       cgd #include <sys/systm.h>
     42   1.1   mycroft #include <sys/proc.h>
     43   1.1   mycroft #include <sys/buf.h>
     44   1.1   mycroft #include <sys/mount.h>
     45   1.1   mycroft #include <sys/vnode.h>
     46   1.1   mycroft #include <sys/malloc.h>
     47   1.1   mycroft #include <sys/kernel.h>
     48   1.1   mycroft 
     49   1.5       cgd #include <sys/syscallargs.h>
     50   1.5       cgd 
     51   1.1   mycroft #include <ufs/ufs/quota.h>
     52   1.1   mycroft #include <ufs/ufs/inode.h>
     53   1.1   mycroft #include <ufs/ufs/ufsmount.h>
     54   1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     55   1.1   mycroft 
     56   1.1   mycroft #include <ufs/lfs/lfs.h>
     57   1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     58  1.10  christos 
     59   1.1   mycroft #define BUMP_FIP(SP) \
     60   1.1   mycroft 	(SP)->fip = (FINFO *) (&(SP)->fip->fi_blocks[(SP)->fip->fi_nblocks])
     61   1.1   mycroft 
     62   1.1   mycroft #define INC_FINFO(SP) ++((SEGSUM *)((SP)->segsum))->ss_nfinfo
     63   1.1   mycroft #define DEC_FINFO(SP) --((SEGSUM *)((SP)->segsum))->ss_nfinfo
     64   1.1   mycroft 
     65   1.1   mycroft /*
     66   1.1   mycroft  * Before committing to add something to a segment summary, make sure there
     67   1.1   mycroft  * is enough room.  S is the bytes added to the summary.
     68   1.1   mycroft  */
     69   1.1   mycroft #define	CHECK_SEG(s)			\
     70   1.1   mycroft if (sp->sum_bytes_left < (s)) {		\
     71   1.1   mycroft 	(void) lfs_writeseg(fs, sp);	\
     72   1.1   mycroft }
     73   1.1   mycroft struct buf *lfs_fakebuf __P((struct vnode *, int, size_t, caddr_t));
     74   1.1   mycroft 
     75  1.16      fvdl int debug_cleaner = 0;
     76  1.16      fvdl int clean_vnlocked = 0;
     77  1.16      fvdl int clean_inlocked = 0;
     78  1.16      fvdl 
     79   1.1   mycroft /*
     80   1.1   mycroft  * lfs_markv:
     81   1.1   mycroft  *
     82   1.1   mycroft  * This will mark inodes and blocks dirty, so they are written into the log.
     83   1.1   mycroft  * It will block until all the blocks have been written.  The segment create
     84   1.1   mycroft  * time passed in the block_info and inode_info structures is used to decide
     85   1.1   mycroft  * if the data is valid for each block (in case some process dirtied a block
     86   1.1   mycroft  * or inode that is being cleaned between the determination that a block is
     87   1.1   mycroft  * live and the lfs_markv call).
     88   1.1   mycroft  *
     89   1.1   mycroft  *  0 on success
     90   1.1   mycroft  * -1/errno is return on error.
     91   1.1   mycroft  */
     92   1.1   mycroft int
     93   1.9   thorpej lfs_markv(p, v, retval)
     94   1.1   mycroft 	struct proc *p;
     95   1.9   thorpej 	void *v;
     96   1.9   thorpej 	register_t *retval;
     97   1.9   thorpej {
     98   1.5       cgd 	struct lfs_markv_args /* {
     99   1.5       cgd 		syscallarg(fsid_t *) fsidp;
    100   1.5       cgd 		syscallarg(struct block_info *) blkiov;
    101   1.5       cgd 		syscallarg(int) blkcnt;
    102   1.9   thorpej 	} */ *uap = v;
    103   1.1   mycroft 	struct segment *sp;
    104   1.1   mycroft 	BLOCK_INFO *blkp;
    105   1.1   mycroft 	IFILE *ifp;
    106   1.1   mycroft 	struct buf *bp, **bpp;
    107  1.10  christos 	struct inode *ip = NULL;
    108   1.1   mycroft 	struct lfs *fs;
    109   1.1   mycroft 	struct mount *mntp;
    110   1.1   mycroft 	struct vnode *vp;
    111   1.1   mycroft 	fsid_t fsid;
    112   1.1   mycroft 	void *start;
    113   1.1   mycroft 	ino_t lastino;
    114  1.16      fvdl 	ufs_daddr_t b_daddr, v_daddr;
    115   1.1   mycroft 	u_long bsize;
    116   1.1   mycroft 	int cnt, error;
    117   1.1   mycroft 
    118  1.10  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    119   1.1   mycroft 		return (error);
    120   1.1   mycroft 
    121  1.10  christos 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    122   1.1   mycroft 		return (error);
    123  1.16      fvdl 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    124   1.1   mycroft 		return (EINVAL);
    125   1.1   mycroft 
    126   1.5       cgd 	cnt = SCARG(uap, blkcnt);
    127   1.1   mycroft 	start = malloc(cnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    128  1.10  christos 	error = copyin(SCARG(uap, blkiov), start, cnt * sizeof(BLOCK_INFO));
    129  1.10  christos 	if (error)
    130   1.1   mycroft 		goto err1;
    131   1.1   mycroft 
    132   1.1   mycroft 	/* Mark blocks/inodes dirty.  */
    133   1.1   mycroft 	fs = VFSTOUFS(mntp)->um_lfs;
    134   1.1   mycroft 	bsize = fs->lfs_bsize;
    135   1.1   mycroft 	error = 0;
    136   1.1   mycroft 
    137   1.1   mycroft 	lfs_seglock(fs, SEGM_SYNC | SEGM_CLEAN);
    138   1.1   mycroft 	sp = fs->lfs_sp;
    139   1.1   mycroft 	for (v_daddr = LFS_UNUSED_DADDR, lastino = LFS_UNUSED_INUM,
    140   1.1   mycroft 	    blkp = start; cnt--; ++blkp) {
    141   1.1   mycroft 		/*
    142   1.1   mycroft 		 * Get the IFILE entry (only once) and see if the file still
    143   1.1   mycroft 		 * exists.
    144   1.1   mycroft 		 */
    145   1.1   mycroft 		if (lastino != blkp->bi_inode) {
    146   1.1   mycroft 			if (lastino != LFS_UNUSED_INUM) {
    147   1.1   mycroft 				/* Finish up last file */
    148   1.1   mycroft 				if (sp->fip->fi_nblocks == 0) {
    149   1.1   mycroft 					DEC_FINFO(sp);
    150   1.1   mycroft 					sp->sum_bytes_left +=
    151  1.16      fvdl 					    sizeof(FINFO) - sizeof(ufs_daddr_t);
    152   1.1   mycroft 				} else {
    153   1.1   mycroft 					lfs_updatemeta(sp);
    154   1.1   mycroft 					BUMP_FIP(sp);
    155   1.1   mycroft 				}
    156   1.1   mycroft 
    157   1.1   mycroft 				lfs_writeinode(fs, sp, ip);
    158   1.1   mycroft 				lfs_vunref(vp);
    159   1.1   mycroft 			}
    160   1.1   mycroft 
    161   1.1   mycroft 			/* Start a new file */
    162   1.1   mycroft 			CHECK_SEG(sizeof(FINFO));
    163  1.16      fvdl 			sp->sum_bytes_left -= sizeof(FINFO) - sizeof(ufs_daddr_t);
    164   1.1   mycroft 			INC_FINFO(sp);
    165   1.1   mycroft 			sp->start_lbp = &sp->fip->fi_blocks[0];
    166   1.1   mycroft 			sp->vp = NULL;
    167   1.1   mycroft 			sp->fip->fi_version = blkp->bi_version;
    168   1.1   mycroft 			sp->fip->fi_nblocks = 0;
    169   1.1   mycroft 			sp->fip->fi_ino = blkp->bi_inode;
    170   1.1   mycroft 			lastino = blkp->bi_inode;
    171   1.1   mycroft 			if (blkp->bi_inode == LFS_IFILE_INUM)
    172   1.1   mycroft 				v_daddr = fs->lfs_idaddr;
    173   1.1   mycroft 			else {
    174   1.1   mycroft 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
    175   1.1   mycroft 				v_daddr = ifp->if_daddr;
    176   1.1   mycroft 				brelse(bp);
    177   1.1   mycroft 			}
    178   1.1   mycroft 			if (v_daddr == LFS_UNUSED_DADDR)
    179   1.1   mycroft 				continue;
    180   1.1   mycroft 
    181   1.1   mycroft 			/* Get the vnode/inode. */
    182   1.1   mycroft 			if (lfs_fastvget(mntp, blkp->bi_inode, v_daddr, &vp,
    183   1.1   mycroft 			    blkp->bi_lbn == LFS_UNUSED_LBN ?
    184   1.1   mycroft 			    blkp->bi_bp : NULL)) {
    185   1.1   mycroft #ifdef DIAGNOSTIC
    186  1.12  christos 				printf("lfs_markv: VFS_VGET failed (%d)\n",
    187   1.1   mycroft 				    blkp->bi_inode);
    188  1.16      fvdl 				panic("lfs_markv VFS_VGET FAILED");
    189   1.1   mycroft #endif
    190   1.1   mycroft 				lastino = LFS_UNUSED_INUM;
    191   1.1   mycroft 				v_daddr = LFS_UNUSED_DADDR;
    192   1.1   mycroft 				continue;
    193   1.1   mycroft 			}
    194   1.1   mycroft 			sp->vp = vp;
    195   1.1   mycroft 			ip = VTOI(vp);
    196   1.1   mycroft 		} else if (v_daddr == LFS_UNUSED_DADDR)
    197   1.1   mycroft 			continue;
    198   1.1   mycroft 
    199   1.1   mycroft 		/* If this BLOCK_INFO didn't contain a block, keep going. */
    200   1.1   mycroft 		if (blkp->bi_lbn == LFS_UNUSED_LBN)
    201   1.1   mycroft 			continue;
    202   1.1   mycroft 		if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
    203   1.1   mycroft 		    b_daddr != blkp->bi_daddr)
    204   1.1   mycroft 			continue;
    205   1.1   mycroft 		/*
    206   1.1   mycroft 		 * If we got to here, then we are keeping the block.  If it
    207   1.1   mycroft 		 * is an indirect block, we want to actually put it in the
    208   1.1   mycroft 		 * buffer cache so that it can be updated in the finish_meta
    209   1.1   mycroft 		 * section.  If it's not, we need to allocate a fake buffer
    210   1.1   mycroft 		 * so that writeseg can perform the copyin and write the buffer.
    211   1.1   mycroft 		 */
    212   1.1   mycroft 		if (blkp->bi_lbn >= 0)	/* Data Block */
    213   1.1   mycroft 			bp = lfs_fakebuf(vp, blkp->bi_lbn, bsize,
    214   1.1   mycroft 			    blkp->bi_bp);
    215   1.1   mycroft 		else {
    216   1.1   mycroft 			bp = getblk(vp, blkp->bi_lbn, bsize, 0, 0);
    217   1.1   mycroft 			if (!(bp->b_flags & (B_DELWRI | B_DONE | B_CACHE)) &&
    218   1.1   mycroft 			    (error = copyin(blkp->bi_bp, bp->b_data,
    219  1.16      fvdl 			    blkp->bi_size)))
    220   1.1   mycroft 				goto err2;
    221  1.14   thorpej 			if ((error = VOP_BWRITE(bp)) != 0)
    222   1.1   mycroft 				goto err2;
    223   1.1   mycroft 		}
    224   1.1   mycroft 		while (lfs_gatherblock(sp, bp, NULL));
    225   1.1   mycroft 	}
    226   1.1   mycroft 	if (sp->vp) {
    227   1.1   mycroft 		if (sp->fip->fi_nblocks == 0) {
    228   1.1   mycroft 			DEC_FINFO(sp);
    229   1.1   mycroft 			sp->sum_bytes_left +=
    230  1.16      fvdl 			    sizeof(FINFO) - sizeof(ufs_daddr_t);
    231   1.1   mycroft 		} else
    232   1.1   mycroft 			lfs_updatemeta(sp);
    233   1.1   mycroft 
    234   1.1   mycroft 		lfs_writeinode(fs, sp, ip);
    235   1.1   mycroft 		lfs_vunref(vp);
    236   1.1   mycroft 	}
    237   1.1   mycroft 	(void) lfs_writeseg(fs, sp);
    238   1.1   mycroft 	lfs_segunlock(fs);
    239   1.1   mycroft 	free(start, M_SEGMENT);
    240   1.1   mycroft 	return (error);
    241   1.1   mycroft 
    242   1.1   mycroft /*
    243   1.1   mycroft  * XXX
    244   1.1   mycroft  * If we come in to error 2, we might have indirect blocks that were
    245   1.1   mycroft  * updated and now have bad block pointers.  I don't know what to do
    246   1.1   mycroft  * about this.
    247   1.1   mycroft  */
    248   1.1   mycroft 
    249   1.1   mycroft err2:	lfs_vunref(vp);
    250   1.1   mycroft 	/* Free up fakebuffers */
    251   1.1   mycroft 	for (bpp = --sp->cbpp; bpp >= sp->bpp; --bpp)
    252   1.1   mycroft 		if ((*bpp)->b_flags & B_CALL) {
    253   1.1   mycroft 			brelvp(*bpp);
    254   1.1   mycroft 			free(*bpp, M_SEGMENT);
    255   1.1   mycroft 		} else
    256   1.1   mycroft 			brelse(*bpp);
    257   1.1   mycroft 	lfs_segunlock(fs);
    258   1.1   mycroft err1:
    259   1.1   mycroft 	free(start, M_SEGMENT);
    260   1.1   mycroft 	return (error);
    261   1.1   mycroft }
    262   1.1   mycroft 
    263   1.1   mycroft /*
    264   1.1   mycroft  * lfs_bmapv:
    265   1.1   mycroft  *
    266   1.1   mycroft  * This will fill in the current disk address for arrays of blocks.
    267   1.1   mycroft  *
    268   1.1   mycroft  *  0 on success
    269   1.1   mycroft  * -1/errno is return on error.
    270   1.1   mycroft  */
    271   1.1   mycroft int
    272   1.9   thorpej lfs_bmapv(p, v, retval)
    273   1.1   mycroft 	struct proc *p;
    274   1.9   thorpej 	void *v;
    275   1.9   thorpej 	register_t *retval;
    276   1.9   thorpej {
    277   1.5       cgd 	struct lfs_bmapv_args /* {
    278   1.5       cgd 		syscallarg(fsid_t *) fsidp;
    279   1.5       cgd 		syscallarg(struct block_info *) blkiov;
    280   1.5       cgd 		syscallarg(int) blkcnt;
    281   1.9   thorpej 	} */ *uap = v;
    282   1.1   mycroft 	BLOCK_INFO *blkp;
    283   1.1   mycroft 	struct mount *mntp;
    284  1.16      fvdl 	struct ufsmount *ump;
    285   1.1   mycroft 	struct vnode *vp;
    286   1.1   mycroft 	fsid_t fsid;
    287   1.1   mycroft 	void *start;
    288  1.16      fvdl 	ufs_daddr_t daddr;
    289   1.1   mycroft 	int cnt, error, step;
    290   1.1   mycroft 
    291  1.10  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    292   1.1   mycroft 		return (error);
    293   1.1   mycroft 
    294  1.10  christos 	error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t));
    295  1.10  christos 	if (error)
    296   1.1   mycroft 		return (error);
    297  1.16      fvdl 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    298   1.1   mycroft 		return (EINVAL);
    299   1.1   mycroft 
    300   1.5       cgd 	cnt = SCARG(uap, blkcnt);
    301   1.1   mycroft 	start = blkp = malloc(cnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
    302  1.10  christos 	error = copyin(SCARG(uap, blkiov), blkp, cnt * sizeof(BLOCK_INFO));
    303  1.10  christos 	if (error) {
    304   1.1   mycroft 		free(blkp, M_SEGMENT);
    305   1.1   mycroft 		return (error);
    306   1.1   mycroft 	}
    307   1.1   mycroft 
    308   1.1   mycroft 	for (step = cnt; step--; ++blkp) {
    309   1.1   mycroft 		if (blkp->bi_lbn == LFS_UNUSED_LBN)
    310   1.1   mycroft 			continue;
    311  1.16      fvdl 		/*
    312  1.16      fvdl 		 * A regular call to VFS_VGET could deadlock
    313  1.16      fvdl 		 * here.  Instead, we try an unlocked access.
    314  1.16      fvdl 		 */
    315  1.16      fvdl 		ump = VFSTOUFS(mntp);
    316  1.16      fvdl 		if ((vp =
    317  1.16      fvdl 		    ufs_ihashlookup(ump->um_dev, blkp->bi_inode)) != NULL) {
    318  1.16      fvdl 			if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &daddr, NULL))
    319  1.16      fvdl 				daddr = LFS_UNUSED_DADDR;
    320  1.16      fvdl 		} else if (VFS_VGET(mntp, blkp->bi_inode, &vp))
    321   1.1   mycroft 			daddr = LFS_UNUSED_DADDR;
    322   1.1   mycroft 		else {
    323   1.1   mycroft 			if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &daddr, NULL))
    324   1.1   mycroft 				daddr = LFS_UNUSED_DADDR;
    325   1.1   mycroft 			vput(vp);
    326   1.1   mycroft 		}
    327   1.1   mycroft 		blkp->bi_daddr = daddr;
    328   1.1   mycroft         }
    329   1.5       cgd 	copyout(start, SCARG(uap, blkiov), cnt * sizeof(BLOCK_INFO));
    330   1.1   mycroft 	free(start, M_SEGMENT);
    331   1.1   mycroft 	return (0);
    332   1.1   mycroft }
    333   1.1   mycroft 
    334   1.1   mycroft /*
    335   1.1   mycroft  * lfs_segclean:
    336   1.1   mycroft  *
    337   1.1   mycroft  * Mark the segment clean.
    338   1.1   mycroft  *
    339   1.1   mycroft  *  0 on success
    340   1.1   mycroft  * -1/errno is return on error.
    341   1.1   mycroft  */
    342   1.1   mycroft int
    343   1.9   thorpej lfs_segclean(p, v, retval)
    344   1.1   mycroft 	struct proc *p;
    345   1.9   thorpej 	void *v;
    346   1.9   thorpej 	register_t *retval;
    347   1.9   thorpej {
    348   1.5       cgd 	struct lfs_segclean_args /* {
    349   1.5       cgd 		syscallarg(fsid_t *) fsidp;
    350   1.5       cgd 		syscallarg(u_long) segment;
    351   1.9   thorpej 	} */ *uap = v;
    352   1.1   mycroft 	CLEANERINFO *cip;
    353   1.1   mycroft 	SEGUSE *sup;
    354   1.1   mycroft 	struct buf *bp;
    355   1.1   mycroft 	struct mount *mntp;
    356   1.1   mycroft 	struct lfs *fs;
    357   1.1   mycroft 	fsid_t fsid;
    358   1.1   mycroft 	int error;
    359   1.1   mycroft 
    360  1.10  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    361   1.1   mycroft 		return (error);
    362   1.1   mycroft 
    363  1.10  christos 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    364   1.1   mycroft 		return (error);
    365  1.16      fvdl 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    366   1.1   mycroft 		return (EINVAL);
    367   1.1   mycroft 
    368   1.1   mycroft 	fs = VFSTOUFS(mntp)->um_lfs;
    369   1.1   mycroft 
    370   1.5       cgd 	if (datosn(fs, fs->lfs_curseg) == SCARG(uap, segment))
    371   1.1   mycroft 		return (EBUSY);
    372   1.1   mycroft 
    373   1.5       cgd 	LFS_SEGENTRY(sup, fs, SCARG(uap, segment), bp);
    374   1.1   mycroft 	if (sup->su_flags & SEGUSE_ACTIVE) {
    375   1.1   mycroft 		brelse(bp);
    376   1.1   mycroft 		return (EBUSY);
    377   1.1   mycroft 	}
    378   1.1   mycroft 	fs->lfs_avail += fsbtodb(fs, fs->lfs_ssize) - 1;
    379   1.1   mycroft 	fs->lfs_bfree += (sup->su_nsums * LFS_SUMMARY_SIZE / DEV_BSIZE) +
    380   1.1   mycroft 	    sup->su_ninos * btodb(fs->lfs_bsize);
    381   1.1   mycroft 	sup->su_flags &= ~SEGUSE_DIRTY;
    382   1.1   mycroft 	(void) VOP_BWRITE(bp);
    383   1.1   mycroft 
    384   1.1   mycroft 	LFS_CLEANERINFO(cip, fs, bp);
    385   1.1   mycroft 	++cip->clean;
    386   1.1   mycroft 	--cip->dirty;
    387   1.1   mycroft 	(void) VOP_BWRITE(bp);
    388   1.1   mycroft 	wakeup(&fs->lfs_avail);
    389   1.1   mycroft 	return (0);
    390   1.1   mycroft }
    391   1.1   mycroft 
    392   1.1   mycroft /*
    393   1.1   mycroft  * lfs_segwait:
    394   1.1   mycroft  *
    395   1.1   mycroft  * This will block until a segment in file system fsid is written.  A timeout
    396   1.1   mycroft  * in milliseconds may be specified which will awake the cleaner automatically.
    397   1.1   mycroft  * An fsid of -1 means any file system, and a timeout of 0 means forever.
    398   1.1   mycroft  *
    399   1.1   mycroft  *  0 on success
    400   1.1   mycroft  *  1 on timeout
    401   1.1   mycroft  * -1/errno is return on error.
    402   1.1   mycroft  */
    403   1.1   mycroft int
    404   1.9   thorpej lfs_segwait(p, v, retval)
    405   1.1   mycroft 	struct proc *p;
    406   1.9   thorpej 	void *v;
    407   1.9   thorpej 	register_t *retval;
    408   1.9   thorpej {
    409   1.5       cgd 	struct lfs_segwait_args /* {
    410   1.5       cgd 		syscallarg(fsid_t *) fsidp;
    411   1.5       cgd 		syscallarg(struct timeval *) tv;
    412   1.9   thorpej 	} */ *uap = v;
    413   1.1   mycroft 	extern int lfs_allclean_wakeup;
    414   1.1   mycroft 	struct mount *mntp;
    415   1.1   mycroft 	struct timeval atv;
    416   1.1   mycroft 	fsid_t fsid;
    417   1.1   mycroft 	void *addr;
    418   1.1   mycroft 	u_long timeout;
    419   1.1   mycroft 	int error, s;
    420   1.1   mycroft 
    421  1.10  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    422   1.1   mycroft 		return (error);
    423   1.1   mycroft }
    424   1.1   mycroft #ifdef WHEN_QUADS_WORK
    425   1.5       cgd 	if (error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t)))
    426   1.1   mycroft 		return (error);
    427   1.1   mycroft 	if (fsid == (fsid_t)-1)
    428   1.1   mycroft 		addr = &lfs_allclean_wakeup;
    429   1.1   mycroft 	else {
    430  1.16      fvdl 		if ((mntp = vfs_getvfs(&fsid)) == NULL)
    431   1.1   mycroft 			return (EINVAL);
    432   1.1   mycroft 		addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
    433   1.1   mycroft 	}
    434   1.1   mycroft #else
    435  1.10  christos 	if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
    436   1.1   mycroft 		return (error);
    437  1.16      fvdl 	if ((mntp = vfs_getvfs(&fsid)) == NULL)
    438   1.1   mycroft 		addr = &lfs_allclean_wakeup;
    439   1.1   mycroft 	else
    440   1.1   mycroft 		addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
    441   1.1   mycroft #endif
    442   1.1   mycroft 
    443   1.5       cgd 	if (SCARG(uap, tv)) {
    444  1.10  christos 		error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
    445  1.10  christos 		if (error)
    446   1.1   mycroft 			return (error);
    447   1.1   mycroft 		if (itimerfix(&atv))
    448   1.1   mycroft 			return (EINVAL);
    449   1.1   mycroft 		s = splclock();
    450   1.8   mycroft 		timeradd(&atv, &time, &atv);
    451   1.1   mycroft 		timeout = hzto(&atv);
    452   1.1   mycroft 		splx(s);
    453   1.1   mycroft 	} else
    454   1.1   mycroft 		timeout = 0;
    455   1.1   mycroft 
    456   1.1   mycroft 	error = tsleep(addr, PCATCH | PUSER, "segment", timeout);
    457   1.1   mycroft 	return (error == ERESTART ? EINTR : 0);
    458   1.1   mycroft }
    459   1.1   mycroft 
    460   1.1   mycroft /*
    461   1.1   mycroft  * VFS_VGET call specialized for the cleaner.  The cleaner already knows the
    462   1.1   mycroft  * daddr from the ifile, so don't look it up again.  If the cleaner is
    463   1.1   mycroft  * processing IINFO structures, it may have the ondisk inode already, so
    464   1.1   mycroft  * don't go retrieving it again.
    465   1.1   mycroft  */
    466   1.1   mycroft int
    467   1.1   mycroft lfs_fastvget(mp, ino, daddr, vpp, dinp)
    468   1.1   mycroft 	struct mount *mp;
    469   1.1   mycroft 	ino_t ino;
    470  1.16      fvdl 	ufs_daddr_t daddr;
    471   1.1   mycroft 	struct vnode **vpp;
    472   1.1   mycroft 	struct dinode *dinp;
    473   1.1   mycroft {
    474   1.1   mycroft 	register struct inode *ip;
    475   1.1   mycroft 	struct vnode *vp;
    476   1.1   mycroft 	struct ufsmount *ump;
    477   1.1   mycroft 	struct buf *bp;
    478   1.1   mycroft 	dev_t dev;
    479   1.1   mycroft 	int error;
    480   1.1   mycroft 
    481   1.1   mycroft 	ump = VFSTOUFS(mp);
    482   1.1   mycroft 	dev = ump->um_dev;
    483   1.1   mycroft 	/*
    484   1.1   mycroft 	 * This is playing fast and loose.  Someone may have the inode
    485   1.1   mycroft 	 * locked, in which case they are going to be distinctly unhappy
    486   1.1   mycroft 	 * if we trash something.
    487   1.1   mycroft 	 */
    488   1.1   mycroft 	if ((*vpp = ufs_ihashlookup(dev, ino)) != NULL) {
    489   1.1   mycroft 		lfs_vref(*vpp);
    490   1.1   mycroft 		if ((*vpp)->v_flag & VXLOCK)
    491  1.16      fvdl 			clean_vnlocked++;
    492   1.1   mycroft 		ip = VTOI(*vpp);
    493  1.16      fvdl 		if (lockstatus(&ip->i_lock))
    494  1.16      fvdl 			clean_inlocked++;
    495  1.16      fvdl 		if (!(ip->i_flag & IN_MODIFIED))
    496   1.1   mycroft 			++ump->um_lfs->lfs_uinodes;
    497   1.1   mycroft 		ip->i_flag |= IN_MODIFIED;
    498   1.1   mycroft 		return (0);
    499   1.1   mycroft 	}
    500   1.1   mycroft 
    501   1.1   mycroft 	/* Allocate new vnode/inode. */
    502  1.10  christos 	if ((error = lfs_vcreate(mp, ino, &vp)) != 0) {
    503   1.1   mycroft 		*vpp = NULL;
    504   1.1   mycroft 		return (error);
    505   1.1   mycroft 	}
    506   1.1   mycroft 
    507   1.1   mycroft 	/*
    508   1.1   mycroft 	 * Put it onto its hash chain and lock it so that other requests for
    509   1.1   mycroft 	 * this inode will block if they arrive while we are sleeping waiting
    510   1.1   mycroft 	 * for old data structures to be purged or for the contents of the
    511   1.1   mycroft 	 * disk portion of this inode to be read.
    512   1.1   mycroft 	 */
    513   1.1   mycroft 	ip = VTOI(vp);
    514   1.1   mycroft 	ufs_ihashins(ip);
    515   1.1   mycroft 
    516   1.1   mycroft 	/*
    517   1.1   mycroft 	 * XXX
    518   1.1   mycroft 	 * This may not need to be here, logically it should go down with
    519   1.1   mycroft 	 * the i_devvp initialization.
    520   1.1   mycroft 	 * Ask Kirk.
    521   1.1   mycroft 	 */
    522   1.1   mycroft 	ip->i_lfs = ump->um_lfs;
    523   1.1   mycroft 
    524   1.1   mycroft 	/* Read in the disk contents for the inode, copy into the inode. */
    525  1.10  christos 	if (dinp) {
    526  1.13    bouyer 		error = copyin(dinp, &ip->i_din.ffs_din, sizeof(struct dinode));
    527  1.10  christos 		if (error)
    528   1.1   mycroft 			return (error);
    529  1.10  christos 	}
    530   1.1   mycroft 	else {
    531  1.10  christos 		error = bread(ump->um_devvp, daddr,
    532  1.10  christos 			      (int)ump->um_lfs->lfs_bsize, NOCRED, &bp);
    533  1.10  christos 		if (error) {
    534   1.1   mycroft 			/*
    535   1.1   mycroft 			 * The inode does not contain anything useful, so it
    536   1.1   mycroft 			 * would be misleading to leave it on its hash chain.
    537   1.1   mycroft 			 * Iput() will return it to the free list.
    538   1.1   mycroft 			 */
    539   1.1   mycroft 			ufs_ihashrem(ip);
    540   1.1   mycroft 
    541   1.1   mycroft 			/* Unlock and discard unneeded inode. */
    542   1.1   mycroft 			lfs_vunref(vp);
    543   1.1   mycroft 			brelse(bp);
    544   1.1   mycroft 			*vpp = NULL;
    545   1.1   mycroft 			return (error);
    546   1.1   mycroft 		}
    547  1.13    bouyer 		ip->i_din.ffs_din =
    548   1.1   mycroft 		    *lfs_ifind(ump->um_lfs, ino, (struct dinode *)bp->b_data);
    549   1.1   mycroft 		brelse(bp);
    550   1.1   mycroft 	}
    551   1.1   mycroft 
    552   1.1   mycroft 	/*
    553   1.1   mycroft 	 * Initialize the vnode from the inode, check for aliases.  In all
    554   1.1   mycroft 	 * cases re-init ip, the underlying vnode/inode may have changed.
    555   1.1   mycroft 	 */
    556  1.10  christos 	error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp);
    557  1.10  christos 	if (error) {
    558   1.1   mycroft 		lfs_vunref(vp);
    559   1.1   mycroft 		*vpp = NULL;
    560   1.1   mycroft 		return (error);
    561   1.1   mycroft 	}
    562   1.1   mycroft 	/*
    563   1.1   mycroft 	 * Finish inode initialization now that aliasing has been resolved.
    564   1.1   mycroft 	 */
    565   1.1   mycroft 	ip->i_devvp = ump->um_devvp;
    566   1.1   mycroft 	ip->i_flag |= IN_MODIFIED;
    567   1.1   mycroft 	++ump->um_lfs->lfs_uinodes;
    568   1.1   mycroft 	VREF(ip->i_devvp);
    569   1.1   mycroft 	*vpp = vp;
    570   1.1   mycroft 	return (0);
    571   1.1   mycroft }
    572   1.1   mycroft struct buf *
    573   1.1   mycroft lfs_fakebuf(vp, lbn, size, uaddr)
    574   1.1   mycroft 	struct vnode *vp;
    575   1.1   mycroft 	int lbn;
    576   1.1   mycroft 	size_t size;
    577   1.1   mycroft 	caddr_t uaddr;
    578   1.1   mycroft {
    579   1.1   mycroft 	struct buf *bp;
    580   1.1   mycroft 
    581   1.1   mycroft 	bp = lfs_newbuf(vp, lbn, 0);
    582   1.1   mycroft 	bp->b_saveaddr = uaddr;
    583   1.1   mycroft 	bp->b_bufsize = size;
    584   1.1   mycroft 	bp->b_bcount = size;
    585   1.1   mycroft 	bp->b_flags |= B_INVAL;
    586   1.1   mycroft 	return (bp);
    587   1.1   mycroft }
    588