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