Home | History | Annotate | Line # | Download | only in lfs
lfs_segment.c revision 1.6
      1  1.6  christos /*	$NetBSD: lfs_segment.c,v 1.6 1996/10/10 17:21:25 christos Exp $	*/
      2  1.2       cgd 
      3  1.1   mycroft /*
      4  1.1   mycroft  * Copyright (c) 1991, 1993
      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.2       cgd  *	@(#)lfs_segment.c	8.5 (Berkeley) 1/4/94
     36  1.1   mycroft  */
     37  1.1   mycroft 
     38  1.1   mycroft #include <sys/param.h>
     39  1.1   mycroft #include <sys/systm.h>
     40  1.1   mycroft #include <sys/namei.h>
     41  1.1   mycroft #include <sys/kernel.h>
     42  1.1   mycroft #include <sys/resourcevar.h>
     43  1.1   mycroft #include <sys/file.h>
     44  1.1   mycroft #include <sys/stat.h>
     45  1.1   mycroft #include <sys/buf.h>
     46  1.1   mycroft #include <sys/proc.h>
     47  1.1   mycroft #include <sys/conf.h>
     48  1.1   mycroft #include <sys/vnode.h>
     49  1.1   mycroft #include <sys/malloc.h>
     50  1.1   mycroft #include <sys/mount.h>
     51  1.1   mycroft 
     52  1.1   mycroft #include <miscfs/specfs/specdev.h>
     53  1.1   mycroft #include <miscfs/fifofs/fifo.h>
     54  1.1   mycroft 
     55  1.1   mycroft #include <ufs/ufs/quota.h>
     56  1.1   mycroft #include <ufs/ufs/inode.h>
     57  1.1   mycroft #include <ufs/ufs/dir.h>
     58  1.1   mycroft #include <ufs/ufs/ufsmount.h>
     59  1.1   mycroft #include <ufs/ufs/ufs_extern.h>
     60  1.1   mycroft 
     61  1.1   mycroft #include <ufs/lfs/lfs.h>
     62  1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     63  1.1   mycroft 
     64  1.1   mycroft extern int count_lock_queue __P((void));
     65  1.1   mycroft 
     66  1.1   mycroft #define MAX_ACTIVE	10
     67  1.1   mycroft /*
     68  1.1   mycroft  * Determine if it's OK to start a partial in this segment, or if we need
     69  1.1   mycroft  * to go on to a new segment.
     70  1.1   mycroft  */
     71  1.1   mycroft #define	LFS_PARTIAL_FITS(fs) \
     72  1.1   mycroft 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
     73  1.1   mycroft 	1 << (fs)->lfs_fsbtodb)
     74  1.1   mycroft 
     75  1.1   mycroft void	 lfs_callback __P((struct buf *));
     76  1.1   mycroft void	 lfs_gather __P((struct lfs *, struct segment *,
     77  1.1   mycroft 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
     78  1.1   mycroft int	 lfs_gatherblock __P((struct segment *, struct buf *, int *));
     79  1.1   mycroft void	 lfs_iset __P((struct inode *, daddr_t, time_t));
     80  1.1   mycroft int	 lfs_match_data __P((struct lfs *, struct buf *));
     81  1.1   mycroft int	 lfs_match_dindir __P((struct lfs *, struct buf *));
     82  1.1   mycroft int	 lfs_match_indir __P((struct lfs *, struct buf *));
     83  1.1   mycroft int	 lfs_match_tindir __P((struct lfs *, struct buf *));
     84  1.1   mycroft void	 lfs_newseg __P((struct lfs *));
     85  1.1   mycroft void	 lfs_shellsort __P((struct buf **, daddr_t *, register int));
     86  1.1   mycroft void	 lfs_supercallback __P((struct buf *));
     87  1.1   mycroft void	 lfs_updatemeta __P((struct segment *));
     88  1.1   mycroft int	 lfs_vref __P((struct vnode *));
     89  1.1   mycroft void	 lfs_vunref __P((struct vnode *));
     90  1.1   mycroft void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
     91  1.1   mycroft int	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
     92  1.1   mycroft int	 lfs_writeseg __P((struct lfs *, struct segment *));
     93  1.1   mycroft void	 lfs_writesuper __P((struct lfs *));
     94  1.1   mycroft void	 lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
     95  1.1   mycroft 	    struct segment *sp, int dirops));
     96  1.1   mycroft 
     97  1.1   mycroft int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
     98  1.1   mycroft 
     99  1.1   mycroft /* Statistics Counters */
    100  1.1   mycroft #define DOSTATS
    101  1.1   mycroft struct lfs_stats lfs_stats;
    102  1.1   mycroft 
    103  1.1   mycroft /* op values to lfs_writevnodes */
    104  1.1   mycroft #define	VN_REG	0
    105  1.1   mycroft #define	VN_DIROP	1
    106  1.1   mycroft #define	VN_EMPTY	2
    107  1.1   mycroft 
    108  1.1   mycroft /*
    109  1.1   mycroft  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
    110  1.1   mycroft  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
    111  1.1   mycroft  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
    112  1.1   mycroft  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
    113  1.1   mycroft  */
    114  1.1   mycroft 
    115  1.1   mycroft int
    116  1.1   mycroft lfs_vflush(vp)
    117  1.1   mycroft 	struct vnode *vp;
    118  1.1   mycroft {
    119  1.1   mycroft 	struct inode *ip;
    120  1.1   mycroft 	struct lfs *fs;
    121  1.1   mycroft 	struct segment *sp;
    122  1.1   mycroft 
    123  1.1   mycroft 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
    124  1.1   mycroft 	if (fs->lfs_nactive > MAX_ACTIVE)
    125  1.1   mycroft 		return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP));
    126  1.1   mycroft 	lfs_seglock(fs, SEGM_SYNC);
    127  1.1   mycroft 	sp = fs->lfs_sp;
    128  1.1   mycroft 
    129  1.1   mycroft 
    130  1.1   mycroft 	ip = VTOI(vp);
    131  1.1   mycroft 	if (vp->v_dirtyblkhd.lh_first == NULL)
    132  1.1   mycroft 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
    133  1.1   mycroft 
    134  1.1   mycroft 	do {
    135  1.1   mycroft 		do {
    136  1.1   mycroft 			if (vp->v_dirtyblkhd.lh_first != NULL)
    137  1.1   mycroft 				lfs_writefile(fs, sp, vp);
    138  1.1   mycroft 		} while (lfs_writeinode(fs, sp, ip));
    139  1.1   mycroft 
    140  1.1   mycroft 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
    141  1.1   mycroft 
    142  1.1   mycroft #ifdef DOSTATS
    143  1.1   mycroft 	++lfs_stats.nwrites;
    144  1.1   mycroft 	if (sp->seg_flags & SEGM_SYNC)
    145  1.1   mycroft 		++lfs_stats.nsync_writes;
    146  1.1   mycroft 	if (sp->seg_flags & SEGM_CKP)
    147  1.1   mycroft 		++lfs_stats.ncheckpoints;
    148  1.1   mycroft #endif
    149  1.1   mycroft 	lfs_segunlock(fs);
    150  1.1   mycroft 	return (0);
    151  1.1   mycroft }
    152  1.1   mycroft 
    153  1.1   mycroft void
    154  1.1   mycroft lfs_writevnodes(fs, mp, sp, op)
    155  1.1   mycroft 	struct lfs *fs;
    156  1.1   mycroft 	struct mount *mp;
    157  1.1   mycroft 	struct segment *sp;
    158  1.1   mycroft 	int op;
    159  1.1   mycroft {
    160  1.1   mycroft 	struct inode *ip;
    161  1.1   mycroft 	struct vnode *vp;
    162  1.1   mycroft 
    163  1.1   mycroft loop:
    164  1.1   mycroft 	for (vp = mp->mnt_vnodelist.lh_first;
    165  1.1   mycroft 	     vp != NULL;
    166  1.1   mycroft 	     vp = vp->v_mntvnodes.le_next) {
    167  1.1   mycroft 		/*
    168  1.1   mycroft 		 * If the vnode that we are about to sync is no longer
    169  1.1   mycroft 		 * associated with this mount point, start over.
    170  1.1   mycroft 		 */
    171  1.1   mycroft 		if (vp->v_mount != mp)
    172  1.1   mycroft 			goto loop;
    173  1.1   mycroft 
    174  1.1   mycroft 		/* XXX ignore dirops for now
    175  1.1   mycroft 		if (op == VN_DIROP && !(vp->v_flag & VDIROP) ||
    176  1.1   mycroft 		    op != VN_DIROP && (vp->v_flag & VDIROP))
    177  1.1   mycroft 			continue;
    178  1.1   mycroft 		*/
    179  1.1   mycroft 
    180  1.1   mycroft 		if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first)
    181  1.1   mycroft 			continue;
    182  1.1   mycroft 
    183  1.1   mycroft 		if (vp->v_type == VNON)
    184  1.1   mycroft 			continue;
    185  1.1   mycroft 
    186  1.1   mycroft 		if (lfs_vref(vp))
    187  1.1   mycroft 			continue;
    188  1.1   mycroft 
    189  1.1   mycroft 		/*
    190  1.1   mycroft 		 * Write the inode/file if dirty and it's not the
    191  1.1   mycroft 		 * the IFILE.
    192  1.1   mycroft 		 */
    193  1.1   mycroft 		ip = VTOI(vp);
    194  1.1   mycroft 		if ((ip->i_flag &
    195  1.1   mycroft 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) ||
    196  1.1   mycroft 		    vp->v_dirtyblkhd.lh_first != NULL) &&
    197  1.1   mycroft 		    ip->i_number != LFS_IFILE_INUM) {
    198  1.1   mycroft 			if (vp->v_dirtyblkhd.lh_first != NULL)
    199  1.1   mycroft 				lfs_writefile(fs, sp, vp);
    200  1.1   mycroft 			(void) lfs_writeinode(fs, sp, ip);
    201  1.1   mycroft 		}
    202  1.1   mycroft 		vp->v_flag &= ~VDIROP;
    203  1.1   mycroft 		lfs_vunref(vp);
    204  1.1   mycroft 	}
    205  1.1   mycroft }
    206  1.1   mycroft 
    207  1.1   mycroft int
    208  1.1   mycroft lfs_segwrite(mp, flags)
    209  1.1   mycroft 	struct mount *mp;
    210  1.1   mycroft 	int flags;			/* Do a checkpoint. */
    211  1.1   mycroft {
    212  1.1   mycroft 	struct buf *bp;
    213  1.1   mycroft 	struct inode *ip;
    214  1.1   mycroft 	struct lfs *fs;
    215  1.1   mycroft 	struct segment *sp;
    216  1.1   mycroft 	struct vnode *vp;
    217  1.1   mycroft 	SEGUSE *segusep;
    218  1.1   mycroft 	daddr_t ibno;
    219  1.1   mycroft 	CLEANERINFO *cip;
    220  1.1   mycroft 	int clean, do_ckp, error, i;
    221  1.1   mycroft 
    222  1.1   mycroft 	fs = VFSTOUFS(mp)->um_lfs;
    223  1.1   mycroft 
    224  1.1   mycroft  	/*
    225  1.1   mycroft  	 * If we have fewer than 2 clean segments, wait until cleaner
    226  1.1   mycroft 	 * writes.
    227  1.1   mycroft  	 */
    228  1.1   mycroft 	do {
    229  1.1   mycroft 		LFS_CLEANERINFO(cip, fs, bp);
    230  1.1   mycroft 		clean = cip->clean;
    231  1.1   mycroft 		brelse(bp);
    232  1.1   mycroft 		if (clean <= 2) {
    233  1.6  christos 			kprintf ("segs clean: %d\n", clean);
    234  1.1   mycroft 			wakeup(&lfs_allclean_wakeup);
    235  1.4  christos 			error = tsleep(&fs->lfs_avail, PRIBIO + 1,
    236  1.4  christos 				       "lfs writer", 0);
    237  1.4  christos 			if (error)
    238  1.1   mycroft 				return (error);
    239  1.1   mycroft 		}
    240  1.1   mycroft 	} while (clean <= 2 );
    241  1.1   mycroft 
    242  1.1   mycroft 	/*
    243  1.1   mycroft 	 * Allocate a segment structure and enough space to hold pointers to
    244  1.1   mycroft 	 * the maximum possible number of buffers which can be described in a
    245  1.1   mycroft 	 * single summary block.
    246  1.1   mycroft 	 */
    247  1.1   mycroft 	do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE;
    248  1.1   mycroft 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
    249  1.1   mycroft 	sp = fs->lfs_sp;
    250  1.1   mycroft 
    251  1.1   mycroft 	lfs_writevnodes(fs, mp, sp, VN_REG);
    252  1.1   mycroft 
    253  1.1   mycroft 	/* XXX ignore ordering of dirops for now */
    254  1.1   mycroft 	/* XXX
    255  1.1   mycroft 	fs->lfs_writer = 1;
    256  1.1   mycroft 	if (fs->lfs_dirops && (error =
    257  1.1   mycroft 	    tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
    258  1.1   mycroft 		free(sp->bpp, M_SEGMENT);
    259  1.1   mycroft 		free(sp, M_SEGMENT);
    260  1.1   mycroft 		fs->lfs_writer = 0;
    261  1.1   mycroft 		return (error);
    262  1.1   mycroft 	}
    263  1.1   mycroft 
    264  1.1   mycroft 	lfs_writevnodes(fs, mp, sp, VN_DIROP);
    265  1.1   mycroft 	*/
    266  1.1   mycroft 
    267  1.1   mycroft 	/*
    268  1.1   mycroft 	 * If we are doing a checkpoint, mark everything since the
    269  1.1   mycroft 	 * last checkpoint as no longer ACTIVE.
    270  1.1   mycroft 	 */
    271  1.1   mycroft 	if (do_ckp)
    272  1.1   mycroft 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
    273  1.1   mycroft 		     --ibno >= fs->lfs_cleansz; ) {
    274  1.1   mycroft 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
    275  1.1   mycroft 			    NOCRED, &bp))
    276  1.1   mycroft 
    277  1.1   mycroft 				panic("lfs: ifile read");
    278  1.1   mycroft 			segusep = (SEGUSE *)bp->b_data;
    279  1.1   mycroft 			for (i = fs->lfs_sepb; i--; segusep++)
    280  1.1   mycroft 				segusep->su_flags &= ~SEGUSE_ACTIVE;
    281  1.1   mycroft 
    282  1.1   mycroft 			error = VOP_BWRITE(bp);
    283  1.1   mycroft 		}
    284  1.1   mycroft 
    285  1.1   mycroft 	if (do_ckp || fs->lfs_doifile) {
    286  1.1   mycroft redo:
    287  1.1   mycroft 		vp = fs->lfs_ivnode;
    288  1.1   mycroft 		while (vget(vp, 1));
    289  1.1   mycroft 		ip = VTOI(vp);
    290  1.1   mycroft 		if (vp->v_dirtyblkhd.lh_first != NULL)
    291  1.1   mycroft 			lfs_writefile(fs, sp, vp);
    292  1.1   mycroft 		(void)lfs_writeinode(fs, sp, ip);
    293  1.1   mycroft 		vput(vp);
    294  1.1   mycroft 		if (lfs_writeseg(fs, sp) && do_ckp)
    295  1.1   mycroft 			goto redo;
    296  1.1   mycroft 	} else
    297  1.1   mycroft 		(void) lfs_writeseg(fs, sp);
    298  1.1   mycroft 
    299  1.1   mycroft 	/*
    300  1.1   mycroft 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
    301  1.1   mycroft 	 * moment, the user's process hangs around so we can sleep.
    302  1.1   mycroft 	 */
    303  1.1   mycroft 	/* XXX ignore dirops for now
    304  1.1   mycroft 	fs->lfs_writer = 0;
    305  1.1   mycroft 	fs->lfs_doifile = 0;
    306  1.1   mycroft 	wakeup(&fs->lfs_dirops);
    307  1.1   mycroft 	*/
    308  1.1   mycroft 
    309  1.1   mycroft #ifdef DOSTATS
    310  1.1   mycroft 	++lfs_stats.nwrites;
    311  1.1   mycroft 	if (sp->seg_flags & SEGM_SYNC)
    312  1.1   mycroft 		++lfs_stats.nsync_writes;
    313  1.1   mycroft 	if (sp->seg_flags & SEGM_CKP)
    314  1.1   mycroft 		++lfs_stats.ncheckpoints;
    315  1.1   mycroft #endif
    316  1.1   mycroft 	lfs_segunlock(fs);
    317  1.1   mycroft 	return (0);
    318  1.1   mycroft }
    319  1.1   mycroft 
    320  1.1   mycroft /*
    321  1.1   mycroft  * Write the dirty blocks associated with a vnode.
    322  1.1   mycroft  */
    323  1.1   mycroft void
    324  1.1   mycroft lfs_writefile(fs, sp, vp)
    325  1.1   mycroft 	struct lfs *fs;
    326  1.1   mycroft 	struct segment *sp;
    327  1.1   mycroft 	struct vnode *vp;
    328  1.1   mycroft {
    329  1.1   mycroft 	struct buf *bp;
    330  1.1   mycroft 	struct finfo *fip;
    331  1.1   mycroft 	IFILE *ifp;
    332  1.1   mycroft 
    333  1.1   mycroft 	if (sp->seg_bytes_left < fs->lfs_bsize ||
    334  1.1   mycroft 	    sp->sum_bytes_left < sizeof(struct finfo))
    335  1.1   mycroft 		(void) lfs_writeseg(fs, sp);
    336  1.1   mycroft 
    337  1.1   mycroft 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
    338  1.1   mycroft 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    339  1.1   mycroft 
    340  1.1   mycroft 	fip = sp->fip;
    341  1.1   mycroft 	fip->fi_nblocks = 0;
    342  1.1   mycroft 	fip->fi_ino = VTOI(vp)->i_number;
    343  1.1   mycroft 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
    344  1.1   mycroft 	fip->fi_version = ifp->if_version;
    345  1.1   mycroft 	brelse(bp);
    346  1.1   mycroft 
    347  1.1   mycroft 	/*
    348  1.1   mycroft 	 * It may not be necessary to write the meta-data blocks at this point,
    349  1.1   mycroft 	 * as the roll-forward recovery code should be able to reconstruct the
    350  1.1   mycroft 	 * list.
    351  1.1   mycroft 	 */
    352  1.1   mycroft 	lfs_gather(fs, sp, vp, lfs_match_data);
    353  1.1   mycroft 	lfs_gather(fs, sp, vp, lfs_match_indir);
    354  1.1   mycroft 	lfs_gather(fs, sp, vp, lfs_match_dindir);
    355  1.1   mycroft #ifdef TRIPLE
    356  1.1   mycroft 	lfs_gather(fs, sp, vp, lfs_match_tindir);
    357  1.1   mycroft #endif
    358  1.1   mycroft 
    359  1.1   mycroft 	fip = sp->fip;
    360  1.1   mycroft 	if (fip->fi_nblocks != 0) {
    361  1.1   mycroft 		sp->fip =
    362  1.1   mycroft 		    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
    363  1.1   mycroft 		    sizeof(daddr_t) * (fip->fi_nblocks - 1));
    364  1.1   mycroft 		sp->start_lbp = &sp->fip->fi_blocks[0];
    365  1.1   mycroft 	} else {
    366  1.1   mycroft 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
    367  1.1   mycroft 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
    368  1.1   mycroft 	}
    369  1.1   mycroft }
    370  1.1   mycroft 
    371  1.1   mycroft int
    372  1.1   mycroft lfs_writeinode(fs, sp, ip)
    373  1.1   mycroft 	struct lfs *fs;
    374  1.1   mycroft 	struct segment *sp;
    375  1.1   mycroft 	struct inode *ip;
    376  1.1   mycroft {
    377  1.1   mycroft 	struct buf *bp, *ibp;
    378  1.1   mycroft 	IFILE *ifp;
    379  1.1   mycroft 	SEGUSE *sup;
    380  1.1   mycroft 	daddr_t daddr;
    381  1.1   mycroft 	ino_t ino;
    382  1.1   mycroft 	int error, i, ndx;
    383  1.1   mycroft 	int redo_ifile = 0;
    384  1.5   mycroft 	struct timespec ts;
    385  1.1   mycroft 
    386  1.1   mycroft 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)))
    387  1.1   mycroft 		return(0);
    388  1.1   mycroft 
    389  1.1   mycroft 	/* Allocate a new inode block if necessary. */
    390  1.1   mycroft 	if (sp->ibp == NULL) {
    391  1.1   mycroft 		/* Allocate a new segment if necessary. */
    392  1.1   mycroft 		if (sp->seg_bytes_left < fs->lfs_bsize ||
    393  1.1   mycroft 		    sp->sum_bytes_left < sizeof(daddr_t))
    394  1.1   mycroft 			(void) lfs_writeseg(fs, sp);
    395  1.1   mycroft 
    396  1.1   mycroft 		/* Get next inode block. */
    397  1.1   mycroft 		daddr = fs->lfs_offset;
    398  1.1   mycroft 		fs->lfs_offset += fsbtodb(fs, 1);
    399  1.1   mycroft 		sp->ibp = *sp->cbpp++ =
    400  1.1   mycroft 		    lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
    401  1.1   mycroft 		    fs->lfs_bsize);
    402  1.1   mycroft 		/* Zero out inode numbers */
    403  1.1   mycroft 		for (i = 0; i < INOPB(fs); ++i)
    404  1.1   mycroft 			((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
    405  1.1   mycroft 		++sp->start_bpp;
    406  1.1   mycroft 		fs->lfs_avail -= fsbtodb(fs, 1);
    407  1.1   mycroft 		/* Set remaining space counters. */
    408  1.1   mycroft 		sp->seg_bytes_left -= fs->lfs_bsize;
    409  1.1   mycroft 		sp->sum_bytes_left -= sizeof(daddr_t);
    410  1.1   mycroft 		ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
    411  1.1   mycroft 		    sp->ninodes / INOPB(fs) - 1;
    412  1.1   mycroft 		((daddr_t *)(sp->segsum))[ndx] = daddr;
    413  1.1   mycroft 	}
    414  1.1   mycroft 
    415  1.1   mycroft 	/* Update the inode times and copy the inode onto the inode page. */
    416  1.1   mycroft 	if (ip->i_flag & IN_MODIFIED)
    417  1.1   mycroft 		--fs->lfs_uinodes;
    418  1.5   mycroft 	TIMESPEC_TO_TIMEVAL(&time, &ts);
    419  1.5   mycroft 	ITIMES(ip, &ts, &ts, &ts);
    420  1.1   mycroft 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    421  1.1   mycroft 	bp = sp->ibp;
    422  1.1   mycroft 	((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] = ip->i_din;
    423  1.1   mycroft 	/* Increment inode count in segment summary block. */
    424  1.1   mycroft 	++((SEGSUM *)(sp->segsum))->ss_ninos;
    425  1.1   mycroft 
    426  1.1   mycroft 	/* If this page is full, set flag to allocate a new page. */
    427  1.1   mycroft 	if (++sp->ninodes % INOPB(fs) == 0)
    428  1.1   mycroft 		sp->ibp = NULL;
    429  1.1   mycroft 
    430  1.1   mycroft 	/*
    431  1.1   mycroft 	 * If updating the ifile, update the super-block.  Update the disk
    432  1.1   mycroft 	 * address and access times for this inode in the ifile.
    433  1.1   mycroft 	 */
    434  1.1   mycroft 	ino = ip->i_number;
    435  1.1   mycroft 	if (ino == LFS_IFILE_INUM) {
    436  1.1   mycroft 		daddr = fs->lfs_idaddr;
    437  1.1   mycroft 		fs->lfs_idaddr = bp->b_blkno;
    438  1.1   mycroft 	} else {
    439  1.1   mycroft 		LFS_IENTRY(ifp, fs, ino, ibp);
    440  1.1   mycroft 		daddr = ifp->if_daddr;
    441  1.1   mycroft 		ifp->if_daddr = bp->b_blkno;
    442  1.1   mycroft 		error = VOP_BWRITE(ibp);
    443  1.1   mycroft 	}
    444  1.1   mycroft 
    445  1.1   mycroft 	/*
    446  1.1   mycroft 	 * No need to update segment usage if there was no former inode address
    447  1.1   mycroft 	 * or if the last inode address is in the current partial segment.
    448  1.1   mycroft 	 */
    449  1.1   mycroft 	if (daddr != LFS_UNUSED_DADDR &&
    450  1.1   mycroft 	    !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
    451  1.1   mycroft 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
    452  1.1   mycroft #ifdef DIAGNOSTIC
    453  1.1   mycroft 		if (sup->su_nbytes < sizeof(struct dinode)) {
    454  1.1   mycroft 			/* XXX -- Change to a panic. */
    455  1.6  christos 			kprintf("lfs: negative bytes (segment %d)\n",
    456  1.1   mycroft 			    datosn(fs, daddr));
    457  1.1   mycroft 			panic("negative bytes");
    458  1.1   mycroft 		}
    459  1.1   mycroft #endif
    460  1.1   mycroft 		sup->su_nbytes -= sizeof(struct dinode);
    461  1.1   mycroft 		redo_ifile =
    462  1.1   mycroft 		    (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
    463  1.1   mycroft 		error = VOP_BWRITE(bp);
    464  1.1   mycroft 	}
    465  1.1   mycroft 	return (redo_ifile);
    466  1.1   mycroft }
    467  1.1   mycroft 
    468  1.1   mycroft int
    469  1.1   mycroft lfs_gatherblock(sp, bp, sptr)
    470  1.1   mycroft 	struct segment *sp;
    471  1.1   mycroft 	struct buf *bp;
    472  1.1   mycroft 	int *sptr;
    473  1.1   mycroft {
    474  1.1   mycroft 	struct lfs *fs;
    475  1.1   mycroft 	int version;
    476  1.1   mycroft 
    477  1.1   mycroft 	/*
    478  1.1   mycroft 	 * If full, finish this segment.  We may be doing I/O, so
    479  1.1   mycroft 	 * release and reacquire the splbio().
    480  1.1   mycroft 	 */
    481  1.1   mycroft #ifdef DIAGNOSTIC
    482  1.1   mycroft 	if (sp->vp == NULL)
    483  1.1   mycroft 		panic ("lfs_gatherblock: Null vp in segment");
    484  1.1   mycroft #endif
    485  1.1   mycroft 	fs = sp->fs;
    486  1.1   mycroft 	if (sp->sum_bytes_left < sizeof(daddr_t) ||
    487  1.1   mycroft 	    sp->seg_bytes_left < fs->lfs_bsize) {
    488  1.1   mycroft 		if (sptr)
    489  1.1   mycroft 			splx(*sptr);
    490  1.1   mycroft 		lfs_updatemeta(sp);
    491  1.1   mycroft 
    492  1.1   mycroft 		version = sp->fip->fi_version;
    493  1.1   mycroft 		(void) lfs_writeseg(fs, sp);
    494  1.1   mycroft 
    495  1.1   mycroft 		sp->fip->fi_version = version;
    496  1.1   mycroft 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
    497  1.1   mycroft 		/* Add the current file to the segment summary. */
    498  1.1   mycroft 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    499  1.1   mycroft 		sp->sum_bytes_left -=
    500  1.1   mycroft 		    sizeof(struct finfo) - sizeof(daddr_t);
    501  1.1   mycroft 
    502  1.1   mycroft 		if (sptr)
    503  1.1   mycroft 			*sptr = splbio();
    504  1.1   mycroft 		return(1);
    505  1.1   mycroft 	}
    506  1.1   mycroft 
    507  1.1   mycroft 	/* Insert into the buffer list, update the FINFO block. */
    508  1.1   mycroft 	bp->b_flags |= B_GATHERED;
    509  1.1   mycroft 	*sp->cbpp++ = bp;
    510  1.1   mycroft 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
    511  1.1   mycroft 
    512  1.1   mycroft 	sp->sum_bytes_left -= sizeof(daddr_t);
    513  1.1   mycroft 	sp->seg_bytes_left -= fs->lfs_bsize;
    514  1.1   mycroft 	return(0);
    515  1.1   mycroft }
    516  1.1   mycroft 
    517  1.1   mycroft void
    518  1.1   mycroft lfs_gather(fs, sp, vp, match)
    519  1.1   mycroft 	struct lfs *fs;
    520  1.1   mycroft 	struct segment *sp;
    521  1.1   mycroft 	struct vnode *vp;
    522  1.1   mycroft 	int (*match) __P((struct lfs *, struct buf *));
    523  1.1   mycroft {
    524  1.1   mycroft 	struct buf *bp;
    525  1.1   mycroft 	int s;
    526  1.1   mycroft 
    527  1.1   mycroft 	sp->vp = vp;
    528  1.1   mycroft 	s = splbio();
    529  1.1   mycroft loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
    530  1.1   mycroft 		if (bp->b_flags & B_BUSY || !match(fs, bp) ||
    531  1.1   mycroft 		    bp->b_flags & B_GATHERED)
    532  1.1   mycroft 			continue;
    533  1.1   mycroft #ifdef DIAGNOSTIC
    534  1.1   mycroft 		if (!(bp->b_flags & B_DELWRI))
    535  1.1   mycroft 			panic("lfs_gather: bp not B_DELWRI");
    536  1.1   mycroft 		if (!(bp->b_flags & B_LOCKED))
    537  1.1   mycroft 			panic("lfs_gather: bp not B_LOCKED");
    538  1.1   mycroft #endif
    539  1.1   mycroft 		if (lfs_gatherblock(sp, bp, &s))
    540  1.1   mycroft 			goto loop;
    541  1.1   mycroft 	}
    542  1.1   mycroft 	splx(s);
    543  1.1   mycroft 	lfs_updatemeta(sp);
    544  1.1   mycroft 	sp->vp = NULL;
    545  1.1   mycroft }
    546  1.1   mycroft 
    547  1.1   mycroft 
    548  1.1   mycroft /*
    549  1.1   mycroft  * Update the metadata that points to the blocks listed in the FINFO
    550  1.1   mycroft  * array.
    551  1.1   mycroft  */
    552  1.1   mycroft void
    553  1.1   mycroft lfs_updatemeta(sp)
    554  1.1   mycroft 	struct segment *sp;
    555  1.1   mycroft {
    556  1.1   mycroft 	SEGUSE *sup;
    557  1.1   mycroft 	struct buf *bp;
    558  1.1   mycroft 	struct lfs *fs;
    559  1.1   mycroft 	struct vnode *vp;
    560  1.1   mycroft 	struct indir a[NIADDR + 2], *ap;
    561  1.1   mycroft 	struct inode *ip;
    562  1.1   mycroft 	daddr_t daddr, lbn, off;
    563  1.1   mycroft 	int db_per_fsb, error, i, nblocks, num;
    564  1.1   mycroft 
    565  1.1   mycroft 	vp = sp->vp;
    566  1.1   mycroft 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
    567  1.1   mycroft 	if (vp == NULL || nblocks == 0)
    568  1.1   mycroft 		return;
    569  1.1   mycroft 
    570  1.1   mycroft 	/* Sort the blocks. */
    571  1.1   mycroft 	if (!(sp->seg_flags & SEGM_CLEAN))
    572  1.1   mycroft 		lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
    573  1.1   mycroft 
    574  1.1   mycroft 	/*
    575  1.1   mycroft 	 * Assign disk addresses, and update references to the logical
    576  1.1   mycroft 	 * block and the segment usage information.
    577  1.1   mycroft 	 */
    578  1.1   mycroft 	fs = sp->fs;
    579  1.1   mycroft 	db_per_fsb = fsbtodb(fs, 1);
    580  1.1   mycroft 	for (i = nblocks; i--; ++sp->start_bpp) {
    581  1.1   mycroft 		lbn = *sp->start_lbp++;
    582  1.1   mycroft 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
    583  1.1   mycroft 		fs->lfs_offset += db_per_fsb;
    584  1.1   mycroft 
    585  1.4  christos 		error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
    586  1.4  christos 		if (error)
    587  1.1   mycroft 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
    588  1.1   mycroft 		ip = VTOI(vp);
    589  1.1   mycroft 		switch (num) {
    590  1.1   mycroft 		case 0:
    591  1.1   mycroft 			ip->i_db[lbn] = off;
    592  1.1   mycroft 			break;
    593  1.1   mycroft 		case 1:
    594  1.1   mycroft 			ip->i_ib[a[0].in_off] = off;
    595  1.1   mycroft 			break;
    596  1.1   mycroft 		default:
    597  1.1   mycroft 			ap = &a[num - 1];
    598  1.1   mycroft 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
    599  1.1   mycroft 				panic("lfs_updatemeta: bread bno %d",
    600  1.1   mycroft 				    ap->in_lbn);
    601  1.1   mycroft 			/*
    602  1.1   mycroft 			 * Bread may create a new indirect block which needs
    603  1.1   mycroft 			 * to get counted for the inode.
    604  1.1   mycroft 			 */
    605  1.1   mycroft 			if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
    606  1.6  christos kprintf ("Updatemeta allocating indirect block: shouldn't happen\n");
    607  1.1   mycroft 				ip->i_blocks += btodb(fs->lfs_bsize);
    608  1.1   mycroft 				fs->lfs_bfree -= btodb(fs->lfs_bsize);
    609  1.1   mycroft 			}
    610  1.1   mycroft 			((daddr_t *)bp->b_data)[ap->in_off] = off;
    611  1.1   mycroft 			VOP_BWRITE(bp);
    612  1.1   mycroft 		}
    613  1.1   mycroft 
    614  1.1   mycroft 		/* Update segment usage information. */
    615  1.1   mycroft 		if (daddr != UNASSIGNED &&
    616  1.1   mycroft 		    !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
    617  1.1   mycroft 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
    618  1.1   mycroft #ifdef DIAGNOSTIC
    619  1.1   mycroft 			if (sup->su_nbytes < fs->lfs_bsize) {
    620  1.1   mycroft 				/* XXX -- Change to a panic. */
    621  1.6  christos 				kprintf("lfs: negative bytes (segment %d)\n",
    622  1.1   mycroft 				    datosn(fs, daddr));
    623  1.1   mycroft 				panic ("Negative Bytes");
    624  1.1   mycroft 			}
    625  1.1   mycroft #endif
    626  1.1   mycroft 			sup->su_nbytes -= fs->lfs_bsize;
    627  1.1   mycroft 			error = VOP_BWRITE(bp);
    628  1.1   mycroft 		}
    629  1.1   mycroft 	}
    630  1.1   mycroft }
    631  1.1   mycroft 
    632  1.1   mycroft /*
    633  1.1   mycroft  * Start a new segment.
    634  1.1   mycroft  */
    635  1.1   mycroft int
    636  1.1   mycroft lfs_initseg(fs)
    637  1.1   mycroft 	struct lfs *fs;
    638  1.1   mycroft {
    639  1.1   mycroft 	struct segment *sp;
    640  1.1   mycroft 	SEGUSE *sup;
    641  1.1   mycroft 	SEGSUM *ssp;
    642  1.1   mycroft 	struct buf *bp;
    643  1.1   mycroft 	int repeat;
    644  1.1   mycroft 
    645  1.1   mycroft 	sp = fs->lfs_sp;
    646  1.1   mycroft 
    647  1.1   mycroft 	repeat = 0;
    648  1.1   mycroft 	/* Advance to the next segment. */
    649  1.1   mycroft 	if (!LFS_PARTIAL_FITS(fs)) {
    650  1.1   mycroft 		/* Wake up any cleaning procs waiting on this file system. */
    651  1.1   mycroft 		wakeup(&lfs_allclean_wakeup);
    652  1.1   mycroft 
    653  1.1   mycroft 		lfs_newseg(fs);
    654  1.1   mycroft 		repeat = 1;
    655  1.1   mycroft 		fs->lfs_offset = fs->lfs_curseg;
    656  1.1   mycroft 		sp->seg_number = datosn(fs, fs->lfs_curseg);
    657  1.1   mycroft 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
    658  1.1   mycroft 
    659  1.1   mycroft 		/*
    660  1.1   mycroft 		 * If the segment contains a superblock, update the offset
    661  1.1   mycroft 		 * and summary address to skip over it.
    662  1.1   mycroft 		 */
    663  1.1   mycroft 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
    664  1.1   mycroft 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
    665  1.1   mycroft 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
    666  1.1   mycroft 			sp->seg_bytes_left -= LFS_SBPAD;
    667  1.1   mycroft 		}
    668  1.1   mycroft 		brelse(bp);
    669  1.1   mycroft 	} else {
    670  1.1   mycroft 		sp->seg_number = datosn(fs, fs->lfs_curseg);
    671  1.1   mycroft 		sp->seg_bytes_left = (fs->lfs_dbpseg -
    672  1.1   mycroft 		    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
    673  1.1   mycroft 	}
    674  1.1   mycroft 	fs->lfs_lastpseg = fs->lfs_offset;
    675  1.1   mycroft 
    676  1.1   mycroft 	sp->fs = fs;
    677  1.1   mycroft 	sp->ibp = NULL;
    678  1.1   mycroft 	sp->ninodes = 0;
    679  1.1   mycroft 
    680  1.1   mycroft 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
    681  1.1   mycroft 	sp->cbpp = sp->bpp;
    682  1.1   mycroft 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
    683  1.1   mycroft 	     LFS_SUMMARY_SIZE);
    684  1.1   mycroft 	sp->segsum = (*sp->cbpp)->b_data;
    685  1.1   mycroft 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
    686  1.1   mycroft 	sp->start_bpp = ++sp->cbpp;
    687  1.1   mycroft 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
    688  1.1   mycroft 
    689  1.1   mycroft 	/* Set point to SEGSUM, initialize it. */
    690  1.1   mycroft 	ssp = sp->segsum;
    691  1.1   mycroft 	ssp->ss_next = fs->lfs_nextseg;
    692  1.1   mycroft 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
    693  1.1   mycroft 
    694  1.1   mycroft 	/* Set pointer to first FINFO, initialize it. */
    695  1.3       cgd 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
    696  1.1   mycroft 	sp->fip->fi_nblocks = 0;
    697  1.1   mycroft 	sp->start_lbp = &sp->fip->fi_blocks[0];
    698  1.1   mycroft 
    699  1.1   mycroft 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
    700  1.1   mycroft 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
    701  1.1   mycroft 
    702  1.1   mycroft 	return(repeat);
    703  1.1   mycroft }
    704  1.1   mycroft 
    705  1.1   mycroft /*
    706  1.1   mycroft  * Return the next segment to write.
    707  1.1   mycroft  */
    708  1.1   mycroft void
    709  1.1   mycroft lfs_newseg(fs)
    710  1.1   mycroft 	struct lfs *fs;
    711  1.1   mycroft {
    712  1.1   mycroft 	CLEANERINFO *cip;
    713  1.1   mycroft 	SEGUSE *sup;
    714  1.1   mycroft 	struct buf *bp;
    715  1.1   mycroft 	int curseg, isdirty, sn;
    716  1.1   mycroft 
    717  1.1   mycroft         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
    718  1.1   mycroft         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
    719  1.1   mycroft 	sup->su_nbytes = 0;
    720  1.1   mycroft 	sup->su_nsums = 0;
    721  1.1   mycroft 	sup->su_ninos = 0;
    722  1.1   mycroft         (void) VOP_BWRITE(bp);
    723  1.1   mycroft 
    724  1.1   mycroft 	LFS_CLEANERINFO(cip, fs, bp);
    725  1.1   mycroft 	--cip->clean;
    726  1.1   mycroft 	++cip->dirty;
    727  1.1   mycroft 	(void) VOP_BWRITE(bp);
    728  1.1   mycroft 
    729  1.1   mycroft 	fs->lfs_lastseg = fs->lfs_curseg;
    730  1.1   mycroft 	fs->lfs_curseg = fs->lfs_nextseg;
    731  1.1   mycroft 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
    732  1.1   mycroft 		sn = (sn + 1) % fs->lfs_nseg;
    733  1.1   mycroft 		if (sn == curseg)
    734  1.1   mycroft 			panic("lfs_nextseg: no clean segments");
    735  1.1   mycroft 		LFS_SEGENTRY(sup, fs, sn, bp);
    736  1.1   mycroft 		isdirty = sup->su_flags & SEGUSE_DIRTY;
    737  1.1   mycroft 		brelse(bp);
    738  1.1   mycroft 		if (!isdirty)
    739  1.1   mycroft 			break;
    740  1.1   mycroft 	}
    741  1.1   mycroft 
    742  1.1   mycroft 	++fs->lfs_nactive;
    743  1.1   mycroft 	fs->lfs_nextseg = sntoda(fs, sn);
    744  1.1   mycroft #ifdef DOSTATS
    745  1.1   mycroft 	++lfs_stats.segsused;
    746  1.1   mycroft #endif
    747  1.1   mycroft }
    748  1.1   mycroft 
    749  1.1   mycroft int
    750  1.1   mycroft lfs_writeseg(fs, sp)
    751  1.1   mycroft 	struct lfs *fs;
    752  1.1   mycroft 	struct segment *sp;
    753  1.1   mycroft {
    754  1.1   mycroft 	extern int locked_queue_count;
    755  1.1   mycroft 	struct buf **bpp, *bp, *cbp;
    756  1.1   mycroft 	SEGUSE *sup;
    757  1.1   mycroft 	SEGSUM *ssp;
    758  1.1   mycroft 	dev_t i_dev;
    759  1.1   mycroft 	size_t size;
    760  1.1   mycroft 	u_long *datap, *dp;
    761  1.1   mycroft 	int ch_per_blk, do_again, i, nblocks, num, s;
    762  1.4  christos 	int (*strategy)__P((void *));
    763  1.1   mycroft 	struct vop_strategy_args vop_strategy_a;
    764  1.1   mycroft 	u_short ninos;
    765  1.1   mycroft 	char *p;
    766  1.1   mycroft 
    767  1.1   mycroft 	/*
    768  1.1   mycroft 	 * If there are no buffers other than the segment summary to write
    769  1.1   mycroft 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
    770  1.1   mycroft 	 * even if there aren't any buffers, you need to write the superblock.
    771  1.1   mycroft 	 */
    772  1.1   mycroft 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
    773  1.1   mycroft 		return (0);
    774  1.1   mycroft 
    775  1.1   mycroft 	ssp = (SEGSUM *)sp->segsum;
    776  1.1   mycroft 
    777  1.1   mycroft 	/* Update the segment usage information. */
    778  1.1   mycroft 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
    779  1.1   mycroft 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
    780  1.4  christos 	sup->su_nbytes += (nblocks - 1 - ninos) << fs->lfs_bshift;
    781  1.1   mycroft 	sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
    782  1.1   mycroft 	sup->su_nbytes += LFS_SUMMARY_SIZE;
    783  1.1   mycroft 	sup->su_lastmod = time.tv_sec;
    784  1.1   mycroft 	sup->su_ninos += ninos;
    785  1.1   mycroft 	++sup->su_nsums;
    786  1.1   mycroft 	do_again = !(bp->b_flags & B_GATHERED);
    787  1.1   mycroft 	(void)VOP_BWRITE(bp);
    788  1.1   mycroft 	/*
    789  1.1   mycroft 	 * Compute checksum across data and then across summary; the first
    790  1.1   mycroft 	 * block (the summary block) is skipped.  Set the create time here
    791  1.1   mycroft 	 * so that it's guaranteed to be later than the inode mod times.
    792  1.1   mycroft 	 *
    793  1.1   mycroft 	 * XXX
    794  1.1   mycroft 	 * Fix this to do it inline, instead of malloc/copy.
    795  1.1   mycroft 	 */
    796  1.1   mycroft 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
    797  1.1   mycroft 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
    798  1.1   mycroft 		if ((*++bpp)->b_flags & B_INVAL) {
    799  1.1   mycroft 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
    800  1.1   mycroft 				panic("lfs_writeseg: copyin failed");
    801  1.1   mycroft 		} else
    802  1.1   mycroft 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
    803  1.1   mycroft 	}
    804  1.1   mycroft 	ssp->ss_create = time.tv_sec;
    805  1.1   mycroft 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
    806  1.1   mycroft 	ssp->ss_sumsum =
    807  1.1   mycroft 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
    808  1.1   mycroft 	free(datap, M_SEGMENT);
    809  1.1   mycroft #ifdef DIAGNOSTIC
    810  1.1   mycroft 	if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
    811  1.1   mycroft 		panic("lfs_writeseg: No diskspace for summary");
    812  1.1   mycroft #endif
    813  1.1   mycroft 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
    814  1.1   mycroft 
    815  1.1   mycroft 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
    816  1.1   mycroft 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
    817  1.1   mycroft 
    818  1.1   mycroft 	/*
    819  1.1   mycroft 	 * When we simply write the blocks we lose a rotation for every block
    820  1.1   mycroft 	 * written.  To avoid this problem, we allocate memory in chunks, copy
    821  1.1   mycroft 	 * the buffers into the chunk and write the chunk.  MAXPHYS is the
    822  1.1   mycroft 	 * largest size I/O devices can handle.
    823  1.1   mycroft 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
    824  1.1   mycroft 	 * and brelse the buffer (which will move them to the LRU list).  Add
    825  1.1   mycroft 	 * the B_CALL flag to the buffer header so we can count I/O's for the
    826  1.1   mycroft 	 * checkpoints and so we can release the allocated memory.
    827  1.1   mycroft 	 *
    828  1.1   mycroft 	 * XXX
    829  1.1   mycroft 	 * This should be removed if the new virtual memory system allows us to
    830  1.1   mycroft 	 * easily make the buffers contiguous in kernel memory and if that's
    831  1.1   mycroft 	 * fast enough.
    832  1.1   mycroft 	 */
    833  1.1   mycroft 	ch_per_blk = MAXPHYS / fs->lfs_bsize;
    834  1.1   mycroft 	for (bpp = sp->bpp, i = nblocks; i;) {
    835  1.1   mycroft 		num = ch_per_blk;
    836  1.1   mycroft 		if (num > i)
    837  1.1   mycroft 			num = i;
    838  1.1   mycroft 		i -= num;
    839  1.1   mycroft 		size = num * fs->lfs_bsize;
    840  1.1   mycroft 
    841  1.1   mycroft 		cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
    842  1.1   mycroft 		    (*bpp)->b_blkno, size);
    843  1.1   mycroft 		cbp->b_dev = i_dev;
    844  1.1   mycroft 		cbp->b_flags |= B_ASYNC | B_BUSY;
    845  1.1   mycroft 
    846  1.1   mycroft 		s = splbio();
    847  1.1   mycroft 		++fs->lfs_iocount;
    848  1.1   mycroft 		for (p = cbp->b_data; num--;) {
    849  1.1   mycroft 			bp = *bpp++;
    850  1.1   mycroft 			/*
    851  1.1   mycroft 			 * Fake buffers from the cleaner are marked as B_INVAL.
    852  1.1   mycroft 			 * We need to copy the data from user space rather than
    853  1.1   mycroft 			 * from the buffer indicated.
    854  1.1   mycroft 			 * XXX == what do I do on an error?
    855  1.1   mycroft 			 */
    856  1.1   mycroft 			if (bp->b_flags & B_INVAL) {
    857  1.1   mycroft 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
    858  1.1   mycroft 					panic("lfs_writeseg: copyin failed");
    859  1.1   mycroft 			} else
    860  1.1   mycroft 				bcopy(bp->b_data, p, bp->b_bcount);
    861  1.1   mycroft 			p += bp->b_bcount;
    862  1.1   mycroft 			if (bp->b_flags & B_LOCKED)
    863  1.1   mycroft 				--locked_queue_count;
    864  1.1   mycroft 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
    865  1.1   mycroft 			     B_LOCKED | B_GATHERED);
    866  1.1   mycroft 			if (bp->b_flags & B_CALL) {
    867  1.1   mycroft 				/* if B_CALL, it was created with newbuf */
    868  1.1   mycroft 				brelvp(bp);
    869  1.1   mycroft 				if (!(bp->b_flags & B_INVAL))
    870  1.1   mycroft 					free(bp->b_data, M_SEGMENT);
    871  1.1   mycroft 				free(bp, M_SEGMENT);
    872  1.1   mycroft 			} else {
    873  1.1   mycroft 				bremfree(bp);
    874  1.1   mycroft 				bp->b_flags |= B_DONE;
    875  1.1   mycroft 				reassignbuf(bp, bp->b_vp);
    876  1.1   mycroft 				brelse(bp);
    877  1.1   mycroft 			}
    878  1.1   mycroft 		}
    879  1.1   mycroft 		++cbp->b_vp->v_numoutput;
    880  1.1   mycroft 		splx(s);
    881  1.1   mycroft 		cbp->b_bcount = p - (char *)cbp->b_data;
    882  1.1   mycroft 		/*
    883  1.1   mycroft 		 * XXXX This is a gross and disgusting hack.  Since these
    884  1.1   mycroft 		 * buffers are physically addressed, they hang off the
    885  1.1   mycroft 		 * device vnode (devvp).  As a result, they have no way
    886  1.1   mycroft 		 * of getting to the LFS superblock or lfs structure to
    887  1.1   mycroft 		 * keep track of the number of I/O's pending.  So, I am
    888  1.1   mycroft 		 * going to stuff the fs into the saveaddr field of
    889  1.1   mycroft 		 * the buffer (yuk).
    890  1.1   mycroft 		 */
    891  1.1   mycroft 		cbp->b_saveaddr = (caddr_t)fs;
    892  1.1   mycroft 		vop_strategy_a.a_desc = VDESC(vop_strategy);
    893  1.1   mycroft 		vop_strategy_a.a_bp = cbp;
    894  1.1   mycroft 		(strategy)(&vop_strategy_a);
    895  1.1   mycroft 	}
    896  1.1   mycroft 	/*
    897  1.1   mycroft 	 * XXX
    898  1.1   mycroft 	 * Vinvalbuf can move locked buffers off the locked queue
    899  1.1   mycroft 	 * and we have no way of knowing about this.  So, after
    900  1.1   mycroft 	 * doing a big write, we recalculate how many bufers are
    901  1.1   mycroft 	 * really still left on the locked queue.
    902  1.1   mycroft 	 */
    903  1.1   mycroft 	locked_queue_count = count_lock_queue();
    904  1.1   mycroft 	wakeup(&locked_queue_count);
    905  1.1   mycroft #ifdef DOSTATS
    906  1.1   mycroft 	++lfs_stats.psegwrites;
    907  1.1   mycroft 	lfs_stats.blocktot += nblocks - 1;
    908  1.1   mycroft 	if (fs->lfs_sp->seg_flags & SEGM_SYNC)
    909  1.1   mycroft 		++lfs_stats.psyncwrites;
    910  1.1   mycroft 	if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
    911  1.1   mycroft 		++lfs_stats.pcleanwrites;
    912  1.1   mycroft 		lfs_stats.cleanblocks += nblocks - 1;
    913  1.1   mycroft 	}
    914  1.1   mycroft #endif
    915  1.1   mycroft 	return (lfs_initseg(fs) || do_again);
    916  1.1   mycroft }
    917  1.1   mycroft 
    918  1.1   mycroft void
    919  1.1   mycroft lfs_writesuper(fs)
    920  1.1   mycroft 	struct lfs *fs;
    921  1.1   mycroft {
    922  1.1   mycroft 	struct buf *bp;
    923  1.1   mycroft 	dev_t i_dev;
    924  1.4  christos 	int (*strategy) __P((void *));
    925  1.1   mycroft 	int s;
    926  1.1   mycroft 	struct vop_strategy_args vop_strategy_a;
    927  1.1   mycroft 
    928  1.1   mycroft 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
    929  1.1   mycroft 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
    930  1.1   mycroft 
    931  1.1   mycroft 	/* Checksum the superblock and copy it into a buffer. */
    932  1.1   mycroft 	fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
    933  1.1   mycroft 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
    934  1.1   mycroft 	    LFS_SBPAD);
    935  1.1   mycroft 	*(struct lfs *)bp->b_data = *fs;
    936  1.1   mycroft 
    937  1.1   mycroft 	/* XXX Toggle between first two superblocks; for now just write first */
    938  1.1   mycroft 	bp->b_dev = i_dev;
    939  1.1   mycroft 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
    940  1.1   mycroft 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
    941  1.1   mycroft 	bp->b_iodone = lfs_supercallback;
    942  1.1   mycroft 	vop_strategy_a.a_desc = VDESC(vop_strategy);
    943  1.1   mycroft 	vop_strategy_a.a_bp = bp;
    944  1.1   mycroft 	s = splbio();
    945  1.1   mycroft 	++bp->b_vp->v_numoutput;
    946  1.1   mycroft 	splx(s);
    947  1.1   mycroft 	(strategy)(&vop_strategy_a);
    948  1.1   mycroft }
    949  1.1   mycroft 
    950  1.1   mycroft /*
    951  1.1   mycroft  * Logical block number match routines used when traversing the dirty block
    952  1.1   mycroft  * chain.
    953  1.1   mycroft  */
    954  1.1   mycroft int
    955  1.1   mycroft lfs_match_data(fs, bp)
    956  1.1   mycroft 	struct lfs *fs;
    957  1.1   mycroft 	struct buf *bp;
    958  1.1   mycroft {
    959  1.1   mycroft 	return (bp->b_lblkno >= 0);
    960  1.1   mycroft }
    961  1.1   mycroft 
    962  1.1   mycroft int
    963  1.1   mycroft lfs_match_indir(fs, bp)
    964  1.1   mycroft 	struct lfs *fs;
    965  1.1   mycroft 	struct buf *bp;
    966  1.1   mycroft {
    967  1.1   mycroft 	int lbn;
    968  1.1   mycroft 
    969  1.1   mycroft 	lbn = bp->b_lblkno;
    970  1.1   mycroft 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
    971  1.1   mycroft }
    972  1.1   mycroft 
    973  1.1   mycroft int
    974  1.1   mycroft lfs_match_dindir(fs, bp)
    975  1.1   mycroft 	struct lfs *fs;
    976  1.1   mycroft 	struct buf *bp;
    977  1.1   mycroft {
    978  1.1   mycroft 	int lbn;
    979  1.1   mycroft 
    980  1.1   mycroft 	lbn = bp->b_lblkno;
    981  1.1   mycroft 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
    982  1.1   mycroft }
    983  1.1   mycroft 
    984  1.1   mycroft int
    985  1.1   mycroft lfs_match_tindir(fs, bp)
    986  1.1   mycroft 	struct lfs *fs;
    987  1.1   mycroft 	struct buf *bp;
    988  1.1   mycroft {
    989  1.1   mycroft 	int lbn;
    990  1.1   mycroft 
    991  1.1   mycroft 	lbn = bp->b_lblkno;
    992  1.1   mycroft 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
    993  1.1   mycroft }
    994  1.1   mycroft 
    995  1.1   mycroft /*
    996  1.1   mycroft  * Allocate a new buffer header.
    997  1.1   mycroft  */
    998  1.1   mycroft struct buf *
    999  1.1   mycroft lfs_newbuf(vp, daddr, size)
   1000  1.1   mycroft 	struct vnode *vp;
   1001  1.1   mycroft 	daddr_t daddr;
   1002  1.1   mycroft 	size_t size;
   1003  1.1   mycroft {
   1004  1.1   mycroft 	struct buf *bp;
   1005  1.1   mycroft 	size_t nbytes;
   1006  1.1   mycroft 
   1007  1.1   mycroft 	nbytes = roundup(size, DEV_BSIZE);
   1008  1.1   mycroft 	bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
   1009  1.1   mycroft 	bzero(bp, sizeof(struct buf));
   1010  1.1   mycroft 	if (nbytes)
   1011  1.1   mycroft 		bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
   1012  1.1   mycroft 	bgetvp(vp, bp);
   1013  1.1   mycroft 	bp->b_bufsize = size;
   1014  1.1   mycroft 	bp->b_bcount = size;
   1015  1.1   mycroft 	bp->b_lblkno = daddr;
   1016  1.1   mycroft 	bp->b_blkno = daddr;
   1017  1.1   mycroft 	bp->b_error = 0;
   1018  1.1   mycroft 	bp->b_resid = 0;
   1019  1.1   mycroft 	bp->b_iodone = lfs_callback;
   1020  1.1   mycroft 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
   1021  1.1   mycroft 	return (bp);
   1022  1.1   mycroft }
   1023  1.1   mycroft 
   1024  1.1   mycroft void
   1025  1.1   mycroft lfs_callback(bp)
   1026  1.1   mycroft 	struct buf *bp;
   1027  1.1   mycroft {
   1028  1.1   mycroft 	struct lfs *fs;
   1029  1.1   mycroft 
   1030  1.1   mycroft 	fs = (struct lfs *)bp->b_saveaddr;
   1031  1.1   mycroft #ifdef DIAGNOSTIC
   1032  1.1   mycroft 	if (fs->lfs_iocount == 0)
   1033  1.1   mycroft 		panic("lfs_callback: zero iocount\n");
   1034  1.1   mycroft #endif
   1035  1.1   mycroft 	if (--fs->lfs_iocount == 0)
   1036  1.1   mycroft 		wakeup(&fs->lfs_iocount);
   1037  1.1   mycroft 
   1038  1.1   mycroft 	brelvp(bp);
   1039  1.1   mycroft 	free(bp->b_data, M_SEGMENT);
   1040  1.1   mycroft 	free(bp, M_SEGMENT);
   1041  1.1   mycroft }
   1042  1.1   mycroft 
   1043  1.1   mycroft void
   1044  1.1   mycroft lfs_supercallback(bp)
   1045  1.1   mycroft 	struct buf *bp;
   1046  1.1   mycroft {
   1047  1.1   mycroft 	brelvp(bp);
   1048  1.1   mycroft 	free(bp->b_data, M_SEGMENT);
   1049  1.1   mycroft 	free(bp, M_SEGMENT);
   1050  1.1   mycroft }
   1051  1.1   mycroft 
   1052  1.1   mycroft /*
   1053  1.1   mycroft  * Shellsort (diminishing increment sort) from Data Structures and
   1054  1.1   mycroft  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
   1055  1.1   mycroft  * see also Knuth Vol. 3, page 84.  The increments are selected from
   1056  1.1   mycroft  * formula (8), page 95.  Roughly O(N^3/2).
   1057  1.1   mycroft  */
   1058  1.1   mycroft /*
   1059  1.1   mycroft  * This is our own private copy of shellsort because we want to sort
   1060  1.1   mycroft  * two parallel arrays (the array of buffer pointers and the array of
   1061  1.1   mycroft  * logical block numbers) simultaneously.  Note that we cast the array
   1062  1.1   mycroft  * of logical block numbers to a unsigned in this routine so that the
   1063  1.1   mycroft  * negative block numbers (meta data blocks) sort AFTER the data blocks.
   1064  1.1   mycroft  */
   1065  1.1   mycroft void
   1066  1.1   mycroft lfs_shellsort(bp_array, lb_array, nmemb)
   1067  1.1   mycroft 	struct buf **bp_array;
   1068  1.1   mycroft 	daddr_t *lb_array;
   1069  1.1   mycroft 	register int nmemb;
   1070  1.1   mycroft {
   1071  1.1   mycroft 	static int __rsshell_increments[] = { 4, 1, 0 };
   1072  1.1   mycroft 	register int incr, *incrp, t1, t2;
   1073  1.1   mycroft 	struct buf *bp_temp;
   1074  1.1   mycroft 	u_long lb_temp;
   1075  1.1   mycroft 
   1076  1.4  christos 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
   1077  1.1   mycroft 		for (t1 = incr; t1 < nmemb; ++t1)
   1078  1.1   mycroft 			for (t2 = t1 - incr; t2 >= 0;)
   1079  1.1   mycroft 				if (lb_array[t2] > lb_array[t2 + incr]) {
   1080  1.1   mycroft 					lb_temp = lb_array[t2];
   1081  1.1   mycroft 					lb_array[t2] = lb_array[t2 + incr];
   1082  1.1   mycroft 					lb_array[t2 + incr] = lb_temp;
   1083  1.1   mycroft 					bp_temp = bp_array[t2];
   1084  1.1   mycroft 					bp_array[t2] = bp_array[t2 + incr];
   1085  1.1   mycroft 					bp_array[t2 + incr] = bp_temp;
   1086  1.1   mycroft 					t2 -= incr;
   1087  1.1   mycroft 				} else
   1088  1.1   mycroft 					break;
   1089  1.1   mycroft }
   1090  1.1   mycroft 
   1091  1.1   mycroft /*
   1092  1.1   mycroft  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
   1093  1.1   mycroft  */
   1094  1.4  christos int
   1095  1.1   mycroft lfs_vref(vp)
   1096  1.1   mycroft 	register struct vnode *vp;
   1097  1.1   mycroft {
   1098  1.1   mycroft 
   1099  1.1   mycroft 	if (vp->v_flag & VXLOCK)
   1100  1.1   mycroft 		return(1);
   1101  1.1   mycroft 	return (vget(vp, 0));
   1102  1.1   mycroft }
   1103  1.1   mycroft 
   1104  1.1   mycroft void
   1105  1.1   mycroft lfs_vunref(vp)
   1106  1.1   mycroft 	register struct vnode *vp;
   1107  1.1   mycroft {
   1108  1.1   mycroft 	extern int lfs_no_inactive;
   1109  1.1   mycroft 
   1110  1.1   mycroft 	/*
   1111  1.1   mycroft 	 * This is vrele except that we do not want to VOP_INACTIVE
   1112  1.1   mycroft 	 * this vnode. Rather than inline vrele here, we use a global
   1113  1.1   mycroft 	 * flag to tell lfs_inactive not to run. Yes, its gross.
   1114  1.1   mycroft 	 */
   1115  1.1   mycroft 	lfs_no_inactive = 1;
   1116  1.1   mycroft 	vrele(vp);
   1117  1.1   mycroft 	lfs_no_inactive = 0;
   1118  1.1   mycroft }
   1119