xref: /csrg-svn/sys/ufs/lfs/lfs_segment.c (revision 65242)
151188Sbostic /*
263375Sbostic  * Copyright (c) 1991, 1993
363375Sbostic  *	The Regents of the University of California.  All rights reserved.
451188Sbostic  *
551188Sbostic  * %sccs.include.redist.c%
651188Sbostic  *
7*65242Smckusick  *	@(#)lfs_segment.c	8.4 (Berkeley) 12/30/93
851188Sbostic  */
951188Sbostic 
1051490Sbostic #include <sys/param.h>
1151490Sbostic #include <sys/systm.h>
1251490Sbostic #include <sys/namei.h>
1352085Sbostic #include <sys/kernel.h>
1451490Sbostic #include <sys/resourcevar.h>
1551490Sbostic #include <sys/file.h>
1651490Sbostic #include <sys/stat.h>
1751490Sbostic #include <sys/buf.h>
1851490Sbostic #include <sys/proc.h>
1951490Sbostic #include <sys/conf.h>
2051490Sbostic #include <sys/vnode.h>
2151490Sbostic #include <sys/malloc.h>
2251490Sbostic #include <sys/mount.h>
2351188Sbostic 
2455033Smckusick #include <miscfs/specfs/specdev.h>
2555033Smckusick #include <miscfs/fifofs/fifo.h>
2655033Smckusick 
2751499Sbostic #include <ufs/ufs/quota.h>
2851499Sbostic #include <ufs/ufs/inode.h>
2951499Sbostic #include <ufs/ufs/dir.h>
3051499Sbostic #include <ufs/ufs/ufsmount.h>
3156478Smargo #include <ufs/ufs/ufs_extern.h>
3251490Sbostic 
3351499Sbostic #include <ufs/lfs/lfs.h>
3451499Sbostic #include <ufs/lfs/lfs_extern.h>
3551490Sbostic 
3657072Smargo extern int count_lock_queue __P((void));
3757072Smargo 
3855940Sbostic #define MAX_ACTIVE	10
3951188Sbostic /*
4051860Sbostic  * Determine if it's OK to start a partial in this segment, or if we need
4151860Sbostic  * to go on to a new segment.
4251301Sbostic  */
4351860Sbostic #define	LFS_PARTIAL_FITS(fs) \
4451860Sbostic 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
4551860Sbostic 	1 << (fs)->lfs_fsbtodb)
4651188Sbostic 
4753347Sbostic void	 lfs_callback __P((struct buf *));
4852085Sbostic void	 lfs_gather __P((struct lfs *, struct segment *,
4952085Sbostic 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
5055940Sbostic int	 lfs_gatherblock __P((struct segment *, struct buf *, int *));
5152085Sbostic void	 lfs_iset __P((struct inode *, daddr_t, time_t));
5252085Sbostic int	 lfs_match_data __P((struct lfs *, struct buf *));
5352085Sbostic int	 lfs_match_dindir __P((struct lfs *, struct buf *));
5452085Sbostic int	 lfs_match_indir __P((struct lfs *, struct buf *));
5552085Sbostic int	 lfs_match_tindir __P((struct lfs *, struct buf *));
5652077Sbostic void	 lfs_newseg __P((struct lfs *));
5752085Sbostic void	 lfs_shellsort __P((struct buf **, daddr_t *, register int));
5855940Sbostic void	 lfs_supercallback __P((struct buf *));
5956027Sbostic void	 lfs_updatemeta __P((struct segment *));
6057072Smargo int	 lfs_vref __P((struct vnode *));
6157072Smargo void	 lfs_vunref __P((struct vnode *));
6252085Sbostic void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
6354264Sbostic int	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
6454264Sbostic int	 lfs_writeseg __P((struct lfs *, struct segment *));
6557072Smargo void	 lfs_writesuper __P((struct lfs *));
6654264Sbostic void	 lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
6754264Sbostic 	    struct segment *sp, int dirops));
6851188Sbostic 
6951860Sbostic int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
7051860Sbostic 
7157072Smargo /* Statistics Counters */
7257072Smargo #define DOSTATS
7357072Smargo struct lfs_stats lfs_stats;
7457072Smargo 
7557072Smargo /* op values to lfs_writevnodes */
7657072Smargo #define	VN_REG	0
7757072Smargo #define	VN_DIROP	1
7857072Smargo #define	VN_EMPTY	2
7957072Smargo 
8052328Sbostic /*
8152328Sbostic  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
8252328Sbostic  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
8352328Sbostic  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
8452328Sbostic  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
8552328Sbostic  */
8652328Sbostic 
8751188Sbostic int
8852328Sbostic lfs_vflush(vp)
8952328Sbostic 	struct vnode *vp;
9052328Sbostic {
9152328Sbostic 	struct inode *ip;
9252328Sbostic 	struct lfs *fs;
9352328Sbostic 	struct segment *sp;
9452328Sbostic 	int error, s;
9552328Sbostic 
9654690Sbostic 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
9756159Smargo 	if (fs->lfs_nactive > MAX_ACTIVE)
9857072Smargo 		return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP));
9957072Smargo 	lfs_seglock(fs, SEGM_SYNC);
10057072Smargo 	sp = fs->lfs_sp;
10156159Smargo 
10252328Sbostic 
10357072Smargo 	ip = VTOI(vp);
104*65242Smckusick 	if (vp->v_dirtyblkhd.lh_first == NULL)
10557072Smargo 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
10652328Sbostic 
10755551Sbostic 	do {
10855551Sbostic 		do {
109*65242Smckusick 			if (vp->v_dirtyblkhd.lh_first != NULL)
11055551Sbostic 				lfs_writefile(fs, sp, vp);
11155551Sbostic 		} while (lfs_writeinode(fs, sp, ip));
11252328Sbostic 
11355551Sbostic 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
11452328Sbostic 
11557072Smargo #ifdef DOSTATS
11657072Smargo 	++lfs_stats.nwrites;
11757072Smargo 	if (sp->seg_flags & SEGM_SYNC)
11857072Smargo 		++lfs_stats.nsync_writes;
11957072Smargo 	if (sp->seg_flags & SEGM_CKP)
12057072Smargo 		++lfs_stats.ncheckpoints;
12157072Smargo #endif
12254690Sbostic 	lfs_segunlock(fs);
12352328Sbostic 	return (0);
12452328Sbostic }
12552328Sbostic 
12654264Sbostic void
12757072Smargo lfs_writevnodes(fs, mp, sp, op)
12854264Sbostic 	struct lfs *fs;
12954264Sbostic 	struct mount *mp;
13054264Sbostic 	struct segment *sp;
13157072Smargo 	int op;
13254264Sbostic {
13354264Sbostic 	struct inode *ip;
13454264Sbostic 	struct vnode *vp;
13557072Smargo 	int error, s, active;
13654264Sbostic 
137*65242Smckusick loop:
138*65242Smckusick 	for (vp = mp->mnt_vnodelist.lh_first;
139*65242Smckusick 	     vp != NULL;
140*65242Smckusick 	     vp = vp->v_mntvnodes.le_next) {
14154264Sbostic 		/*
14254264Sbostic 		 * If the vnode that we are about to sync is no longer
14354264Sbostic 		 * associated with this mount point, start over.
14454264Sbostic 		 */
14554264Sbostic 		if (vp->v_mount != mp)
14654264Sbostic 			goto loop;
14754264Sbostic 
14857072Smargo 		/* XXX ignore dirops for now
14957072Smargo 		if (op == VN_DIROP && !(vp->v_flag & VDIROP) ||
15057072Smargo 		    op != VN_DIROP && (vp->v_flag & VDIROP))
15154264Sbostic 			continue;
15257072Smargo 		*/
15354264Sbostic 
154*65242Smckusick 		if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first)
15557072Smargo 			continue;
15657072Smargo 
15757072Smargo 		if (vp->v_type == VNON)
15857072Smargo 			continue;
15957072Smargo 
16057072Smargo 		if (lfs_vref(vp))
16157072Smargo 			continue;
16257072Smargo 
16354264Sbostic 		/*
16454264Sbostic 		 * Write the inode/file if dirty and it's not the
16554264Sbostic 		 * the IFILE.
16654264Sbostic 		 */
16754264Sbostic 		ip = VTOI(vp);
16864611Sbostic 		if ((ip->i_flag &
16964611Sbostic 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) ||
170*65242Smckusick 		    vp->v_dirtyblkhd.lh_first != NULL) &&
17154264Sbostic 		    ip->i_number != LFS_IFILE_INUM) {
172*65242Smckusick 			if (vp->v_dirtyblkhd.lh_first != NULL)
17354264Sbostic 				lfs_writefile(fs, sp, vp);
17454264Sbostic 			(void) lfs_writeinode(fs, sp, ip);
17554264Sbostic 		}
17654264Sbostic 		vp->v_flag &= ~VDIROP;
17757072Smargo 		lfs_vunref(vp);
17854264Sbostic 	}
17954264Sbostic }
18054264Sbostic 
18152328Sbostic int
18257072Smargo lfs_segwrite(mp, flags)
18352085Sbostic 	struct mount *mp;
18457072Smargo 	int flags;			/* Do a checkpoint. */
18551188Sbostic {
18655592Sbostic 	struct buf *bp;
18752085Sbostic 	struct inode *ip;
18851499Sbostic 	struct lfs *fs;
18952085Sbostic 	struct segment *sp;
19052085Sbostic 	struct vnode *vp;
19155592Sbostic 	SEGUSE *segusep;
19255592Sbostic 	daddr_t ibno;
19355940Sbostic 	CLEANERINFO *cip;
19455940Sbostic 	int clean, error, i, s;
19557072Smargo 	int do_ckp;
19651188Sbostic 
19752328Sbostic 	fs = VFSTOUFS(mp)->um_lfs;
19855940Sbostic 
19955940Sbostic  	/*
20055940Sbostic  	 * If we have fewer than 2 clean segments, wait until cleaner
20155940Sbostic 	 * writes.
20255940Sbostic  	 */
20355940Sbostic 	do {
20455940Sbostic 		LFS_CLEANERINFO(cip, fs, bp);
20555940Sbostic 		clean = cip->clean;
20655940Sbostic 		brelse(bp);
20755940Sbostic 		if (clean <= 2) {
20855940Sbostic 			printf ("segs clean: %d\n", clean);
20955940Sbostic 			wakeup(&lfs_allclean_wakeup);
21055940Sbostic 			if (error = tsleep(&fs->lfs_avail, PRIBIO + 1,
21155940Sbostic 			    "lfs writer", 0))
21255940Sbostic 				return (error);
21355940Sbostic 		}
21455940Sbostic 	} while (clean <= 2 );
21552085Sbostic 
21651860Sbostic 	/*
21752328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
21852328Sbostic 	 * the maximum possible number of buffers which can be described in a
21952328Sbostic 	 * single summary block.
22052328Sbostic 	 */
22157072Smargo 	do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE;
22257072Smargo 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
22357072Smargo 	sp = fs->lfs_sp;
22452328Sbostic 
22557072Smargo 	lfs_writevnodes(fs, mp, sp, VN_REG);
22651342Sbostic 
22757072Smargo 	/* XXX ignore ordering of dirops for now */
22857072Smargo 	/* XXX
22954264Sbostic 	fs->lfs_writer = 1;
23054264Sbostic 	if (fs->lfs_dirops && (error =
23154264Sbostic 	    tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
23254264Sbostic 		free(sp->bpp, M_SEGMENT);
23354264Sbostic 		free(sp, M_SEGMENT);
23454264Sbostic 		fs->lfs_writer = 0;
23555551Sbostic 		return (error);
23654264Sbostic 	}
23751860Sbostic 
23857072Smargo 	lfs_writevnodes(fs, mp, sp, VN_DIROP);
23957072Smargo 	*/
24051860Sbostic 
24154264Sbostic 	/*
24255592Sbostic 	 * If we are doing a checkpoint, mark everything since the
24355592Sbostic 	 * last checkpoint as no longer ACTIVE.
24454264Sbostic 	 */
24555592Sbostic 	if (do_ckp)
24655592Sbostic 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
24755592Sbostic 		     --ibno >= fs->lfs_cleansz; ) {
24855592Sbostic 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
24955592Sbostic 			    NOCRED, &bp))
25055592Sbostic 
25155592Sbostic 				panic("lfs: ifile read");
25264526Sbostic 			segusep = (SEGUSE *)bp->b_data;
25355592Sbostic 			for (i = fs->lfs_sepb; i--; segusep++)
25455592Sbostic 				segusep->su_flags &= ~SEGUSE_ACTIVE;
25555592Sbostic 
25655940Sbostic 			error = VOP_BWRITE(bp);
25755592Sbostic 		}
25855592Sbostic 
25954264Sbostic 	if (do_ckp || fs->lfs_doifile) {
26056086Sbostic redo:
26154264Sbostic 		vp = fs->lfs_ivnode;
262*65242Smckusick 		while (vget(vp, 1));
26352328Sbostic 		ip = VTOI(vp);
264*65242Smckusick 		if (vp->v_dirtyblkhd.lh_first != NULL)
26555592Sbostic 			lfs_writefile(fs, sp, vp);
26655592Sbostic 		(void)lfs_writeinode(fs, sp, ip);
26752077Sbostic 		vput(vp);
26857072Smargo 		if (lfs_writeseg(fs, sp) && do_ckp)
26956086Sbostic 			goto redo;
27054264Sbostic 	} else
27154264Sbostic 		(void) lfs_writeseg(fs, sp);
27251342Sbostic 
27351215Sbostic 	/*
27451860Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
27551860Sbostic 	 * moment, the user's process hangs around so we can sleep.
27651215Sbostic 	 */
27757072Smargo 	/* XXX ignore dirops for now
27854264Sbostic 	fs->lfs_writer = 0;
27954264Sbostic 	fs->lfs_doifile = 0;
28054264Sbostic 	wakeup(&fs->lfs_dirops);
28157072Smargo 	*/
28254264Sbostic 
28357072Smargo #ifdef DOSTATS
28457072Smargo 	++lfs_stats.nwrites;
28557072Smargo 	if (sp->seg_flags & SEGM_SYNC)
28657072Smargo 		++lfs_stats.nsync_writes;
28757072Smargo 	if (sp->seg_flags & SEGM_CKP)
28857072Smargo 		++lfs_stats.ncheckpoints;
28957072Smargo #endif
29054690Sbostic 	lfs_segunlock(fs);
29151860Sbostic 	return (0);
29251188Sbostic }
29351188Sbostic 
29451860Sbostic /*
29551860Sbostic  * Write the dirty blocks associated with a vnode.
29651860Sbostic  */
29752077Sbostic void
29851860Sbostic lfs_writefile(fs, sp, vp)
29951499Sbostic 	struct lfs *fs;
30052085Sbostic 	struct segment *sp;
30152085Sbostic 	struct vnode *vp;
30251188Sbostic {
30351860Sbostic 	struct buf *bp;
30452085Sbostic 	struct finfo *fip;
30551860Sbostic 	IFILE *ifp;
30651188Sbostic 
30752085Sbostic 	if (sp->seg_bytes_left < fs->lfs_bsize ||
30857072Smargo 	    sp->sum_bytes_left < sizeof(struct finfo))
30954264Sbostic 		(void) lfs_writeseg(fs, sp);
31057072Smargo 
31152085Sbostic 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
31256478Smargo 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
31351215Sbostic 
31452085Sbostic 	fip = sp->fip;
31552085Sbostic 	fip->fi_nblocks = 0;
31652085Sbostic 	fip->fi_ino = VTOI(vp)->i_number;
31752085Sbostic 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
31852085Sbostic 	fip->fi_version = ifp->if_version;
31952085Sbostic 	brelse(bp);
32051188Sbostic 
32152085Sbostic 	/*
32252085Sbostic 	 * It may not be necessary to write the meta-data blocks at this point,
32352085Sbostic 	 * as the roll-forward recovery code should be able to reconstruct the
32452085Sbostic 	 * list.
32552085Sbostic 	 */
32652085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_data);
32752085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_indir);
32852085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_dindir);
32951860Sbostic #ifdef TRIPLE
33052085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_tindir);
33151860Sbostic #endif
33251342Sbostic 
33352085Sbostic 	fip = sp->fip;
33452085Sbostic 	if (fip->fi_nblocks != 0) {
33552085Sbostic 		sp->fip =
33652085Sbostic 		    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
33752085Sbostic 		    sizeof(daddr_t) * (fip->fi_nblocks - 1));
33855940Sbostic 		sp->start_lbp = &sp->fip->fi_blocks[0];
33956478Smargo 	} else {
34052682Sstaelin 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
34156478Smargo 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
34256478Smargo 	}
34351215Sbostic }
34451215Sbostic 
34554264Sbostic int
34651915Sbostic lfs_writeinode(fs, sp, ip)
34751915Sbostic 	struct lfs *fs;
34852085Sbostic 	struct segment *sp;
34952085Sbostic 	struct inode *ip;
35051915Sbostic {
35152085Sbostic 	struct buf *bp, *ibp;
35252077Sbostic 	IFILE *ifp;
35352682Sstaelin 	SEGUSE *sup;
35452682Sstaelin 	daddr_t daddr;
35552077Sbostic 	ino_t ino;
35657072Smargo 	int error, i, ndx;
35754264Sbostic 	int redo_ifile = 0;
35851915Sbostic 
35964611Sbostic 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)))
36056190Smargo 		return(0);
36155940Sbostic 
36251915Sbostic 	/* Allocate a new inode block if necessary. */
36351915Sbostic 	if (sp->ibp == NULL) {
36451915Sbostic 		/* Allocate a new segment if necessary. */
36551915Sbostic 		if (sp->seg_bytes_left < fs->lfs_bsize ||
36657072Smargo 		    sp->sum_bytes_left < sizeof(daddr_t))
36754264Sbostic 			(void) lfs_writeseg(fs, sp);
36851915Sbostic 
36951915Sbostic 		/* Get next inode block. */
37052682Sstaelin 		daddr = fs->lfs_offset;
37151915Sbostic 		fs->lfs_offset += fsbtodb(fs, 1);
37251915Sbostic 		sp->ibp = *sp->cbpp++ =
37356056Sbostic 		    lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
37456056Sbostic 		    fs->lfs_bsize);
37557072Smargo 		/* Zero out inode numbers */
37657072Smargo 		for (i = 0; i < INOPB(fs); ++i)
37764526Sbostic 			((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
37855940Sbostic 		++sp->start_bpp;
37955940Sbostic 		fs->lfs_avail -= fsbtodb(fs, 1);
38052688Sbostic 		/* Set remaining space counters. */
38151915Sbostic 		sp->seg_bytes_left -= fs->lfs_bsize;
38251915Sbostic 		sp->sum_bytes_left -= sizeof(daddr_t);
38352077Sbostic 		ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
38451915Sbostic 		    sp->ninodes / INOPB(fs) - 1;
38552682Sstaelin 		((daddr_t *)(sp->segsum))[ndx] = daddr;
38651915Sbostic 	}
38751915Sbostic 
38852085Sbostic 	/* Update the inode times and copy the inode onto the inode page. */
38964611Sbostic 	if (ip->i_flag & IN_MODIFIED)
39056056Sbostic 		--fs->lfs_uinodes;
39152077Sbostic 	ITIMES(ip, &time, &time);
39264611Sbostic 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
39351915Sbostic 	bp = sp->ibp;
39464526Sbostic 	((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] = ip->i_din;
39551915Sbostic 	/* Increment inode count in segment summary block. */
39651915Sbostic 	++((SEGSUM *)(sp->segsum))->ss_ninos;
39751915Sbostic 
39851915Sbostic 	/* If this page is full, set flag to allocate a new page. */
39951915Sbostic 	if (++sp->ninodes % INOPB(fs) == 0)
40051915Sbostic 		sp->ibp = NULL;
40151915Sbostic 
40251915Sbostic 	/*
40352077Sbostic 	 * If updating the ifile, update the super-block.  Update the disk
40452077Sbostic 	 * address and access times for this inode in the ifile.
40551915Sbostic 	 */
40652077Sbostic 	ino = ip->i_number;
40755696Sbostic 	if (ino == LFS_IFILE_INUM) {
40855696Sbostic 		daddr = fs->lfs_idaddr;
40951915Sbostic 		fs->lfs_idaddr = bp->b_blkno;
41055696Sbostic 	} else {
41155696Sbostic 		LFS_IENTRY(ifp, fs, ino, ibp);
41255696Sbostic 		daddr = ifp->if_daddr;
41355696Sbostic 		ifp->if_daddr = bp->b_blkno;
41455940Sbostic 		error = VOP_BWRITE(ibp);
41555696Sbostic 	}
41652077Sbostic 
41754264Sbostic 	/*
41854264Sbostic 	 * No need to update segment usage if there was no former inode address
41954264Sbostic 	 * or if the last inode address is in the current partial segment.
42054264Sbostic 	 */
42154264Sbostic 	if (daddr != LFS_UNUSED_DADDR &&
42255803Sbostic 	    !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
42352682Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
42452682Sstaelin #ifdef DIAGNOSTIC
42554264Sbostic 		if (sup->su_nbytes < sizeof(struct dinode)) {
42652819Sbostic 			/* XXX -- Change to a panic. */
42752819Sbostic 			printf("lfs: negative bytes (segment %d)\n",
42852682Sstaelin 			    datosn(fs, daddr));
42954264Sbostic 			panic("negative bytes");
43054264Sbostic 		}
43152682Sstaelin #endif
43252682Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
43356069Sbostic 		redo_ifile =
43456069Sbostic 		    (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
43555940Sbostic 		error = VOP_BWRITE(bp);
43652682Sstaelin 	}
43755551Sbostic 	return (redo_ifile);
43851915Sbostic }
43951915Sbostic 
44055940Sbostic int
44155940Sbostic lfs_gatherblock(sp, bp, sptr)
44255940Sbostic 	struct segment *sp;
44355940Sbostic 	struct buf *bp;
44455940Sbostic 	int *sptr;
44555940Sbostic {
44655940Sbostic 	struct lfs *fs;
44755940Sbostic 	int version;
44855940Sbostic 
44955940Sbostic 	/*
45055940Sbostic 	 * If full, finish this segment.  We may be doing I/O, so
45155940Sbostic 	 * release and reacquire the splbio().
45255940Sbostic 	 */
45356027Sbostic #ifdef DIAGNOSTIC
45456027Sbostic 	if (sp->vp == NULL)
45556027Sbostic 		panic ("lfs_gatherblock: Null vp in segment");
45656027Sbostic #endif
45755940Sbostic 	fs = sp->fs;
45855940Sbostic 	if (sp->sum_bytes_left < sizeof(daddr_t) ||
45955940Sbostic 	    sp->seg_bytes_left < fs->lfs_bsize) {
46055940Sbostic 		if (sptr)
46155940Sbostic 			splx(*sptr);
46256027Sbostic 		lfs_updatemeta(sp);
46355940Sbostic 
46455940Sbostic 		version = sp->fip->fi_version;
46555940Sbostic 		(void) lfs_writeseg(fs, sp);
46655940Sbostic 
46755940Sbostic 		sp->fip->fi_version = version;
46856027Sbostic 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
46956478Smargo 		/* Add the current file to the segment summary. */
47056478Smargo 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
47155940Sbostic 		sp->sum_bytes_left -=
47255940Sbostic 		    sizeof(struct finfo) - sizeof(daddr_t);
47355940Sbostic 
47455940Sbostic 		if (sptr)
47555940Sbostic 			*sptr = splbio();
47655940Sbostic 		return(1);
47755940Sbostic 	}
47855940Sbostic 
47955940Sbostic 	/* Insert into the buffer list, update the FINFO block. */
48055940Sbostic 	bp->b_flags |= B_GATHERED;
48155940Sbostic 	*sp->cbpp++ = bp;
48255940Sbostic 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
48355940Sbostic 
48455940Sbostic 	sp->sum_bytes_left -= sizeof(daddr_t);
48557072Smargo 	sp->seg_bytes_left -= fs->lfs_bsize;
48655940Sbostic 	return(0);
48755940Sbostic }
48855940Sbostic 
48952077Sbostic void
49051215Sbostic lfs_gather(fs, sp, vp, match)
49151499Sbostic 	struct lfs *fs;
49252085Sbostic 	struct segment *sp;
49352085Sbostic 	struct vnode *vp;
49452085Sbostic 	int (*match) __P((struct lfs *, struct buf *));
49551215Sbostic {
49655940Sbostic 	struct buf *bp;
49751342Sbostic 	int s;
49851215Sbostic 
49956027Sbostic 	sp->vp = vp;
50055940Sbostic 	s = splbio();
501*65242Smckusick loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
50254264Sbostic 		if (bp->b_flags & B_BUSY || !match(fs, bp) ||
50354264Sbostic 		    bp->b_flags & B_GATHERED)
50451215Sbostic 			continue;
50551342Sbostic #ifdef DIAGNOSTIC
50651860Sbostic 		if (!(bp->b_flags & B_DELWRI))
50751915Sbostic 			panic("lfs_gather: bp not B_DELWRI");
50851860Sbostic 		if (!(bp->b_flags & B_LOCKED))
50951915Sbostic 			panic("lfs_gather: bp not B_LOCKED");
51051342Sbostic #endif
51155940Sbostic 		if (lfs_gatherblock(sp, bp, &s))
51253145Sstaelin 			goto loop;
51351188Sbostic 	}
51451215Sbostic 	splx(s);
51556027Sbostic 	lfs_updatemeta(sp);
51656027Sbostic 	sp->vp = NULL;
51751188Sbostic }
51851188Sbostic 
51955940Sbostic 
52051342Sbostic /*
52151342Sbostic  * Update the metadata that points to the blocks listed in the FINFO
52251188Sbostic  * array.
52351188Sbostic  */
52452077Sbostic void
52556027Sbostic lfs_updatemeta(sp)
52652085Sbostic 	struct segment *sp;
52751188Sbostic {
52851915Sbostic 	SEGUSE *sup;
52952085Sbostic 	struct buf *bp;
53055940Sbostic 	struct lfs *fs;
53156027Sbostic 	struct vnode *vp;
53256478Smargo 	struct indir a[NIADDR + 2], *ap;
53352085Sbostic 	struct inode *ip;
53451915Sbostic 	daddr_t daddr, lbn, off;
53555940Sbostic 	int db_per_fsb, error, i, nblocks, num;
53651188Sbostic 
53756027Sbostic 	vp = sp->vp;
53855940Sbostic 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
53956027Sbostic 	if (vp == NULL || nblocks == 0)
54051215Sbostic 		return;
54151215Sbostic 
54251915Sbostic 	/* Sort the blocks. */
54355940Sbostic 	if (!(sp->seg_flags & SEGM_CLEAN))
54455940Sbostic 		lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
54551215Sbostic 
54651915Sbostic 	/*
54751915Sbostic 	 * Assign disk addresses, and update references to the logical
54851915Sbostic 	 * block and the segment usage information.
54951915Sbostic 	 */
55055940Sbostic 	fs = sp->fs;
55151860Sbostic 	db_per_fsb = fsbtodb(fs, 1);
55255940Sbostic 	for (i = nblocks; i--; ++sp->start_bpp) {
55355940Sbostic 		lbn = *sp->start_lbp++;
55455940Sbostic 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
55551860Sbostic 		fs->lfs_offset += db_per_fsb;
55651215Sbostic 
55756478Smargo 		if (error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL))
55856478Smargo 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
55951860Sbostic 		ip = VTOI(vp);
56051860Sbostic 		switch (num) {
56151860Sbostic 		case 0:
56251915Sbostic 			ip->i_db[lbn] = off;
56351860Sbostic 			break;
56451860Sbostic 		case 1:
56551915Sbostic 			ip->i_ib[a[0].in_off] = off;
56651860Sbostic 			break;
56751860Sbostic 		default:
56851860Sbostic 			ap = &a[num - 1];
56951860Sbostic 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
57051860Sbostic 				panic("lfs_updatemeta: bread bno %d",
57151860Sbostic 				    ap->in_lbn);
57255458Sbostic 			/*
57355458Sbostic 			 * Bread may create a new indirect block which needs
57455458Sbostic 			 * to get counted for the inode.
57555458Sbostic 			 */
57655592Sbostic 			if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
57755940Sbostic printf ("Updatemeta allocating indirect block: shouldn't happen\n");
57855458Sbostic 				ip->i_blocks += btodb(fs->lfs_bsize);
57955592Sbostic 				fs->lfs_bfree -= btodb(fs->lfs_bsize);
58055592Sbostic 			}
58164526Sbostic 			((daddr_t *)bp->b_data)[ap->in_off] = off;
58253530Sheideman 			VOP_BWRITE(bp);
58351188Sbostic 		}
58451915Sbostic 
58551915Sbostic 		/* Update segment usage information. */
58657072Smargo 		if (daddr != UNASSIGNED &&
58757072Smargo 		    !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
58851915Sbostic 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
58951915Sbostic #ifdef DIAGNOSTIC
59054264Sbostic 			if (sup->su_nbytes < fs->lfs_bsize) {
59152819Sbostic 				/* XXX -- Change to a panic. */
59252819Sbostic 				printf("lfs: negative bytes (segment %d)\n",
59351915Sbostic 				    datosn(fs, daddr));
59454264Sbostic 				panic ("Negative Bytes");
59554264Sbostic 			}
59651915Sbostic #endif
59751915Sbostic 			sup->su_nbytes -= fs->lfs_bsize;
59855940Sbostic 			error = VOP_BWRITE(bp);
59951915Sbostic 		}
60051188Sbostic 	}
60151188Sbostic }
60251188Sbostic 
60351915Sbostic /*
60451915Sbostic  * Start a new segment.
60551915Sbostic  */
60657072Smargo int
60757072Smargo lfs_initseg(fs)
60851499Sbostic 	struct lfs *fs;
60957072Smargo {
61052085Sbostic 	struct segment *sp;
61151915Sbostic 	SEGUSE *sup;
61251915Sbostic 	SEGSUM *ssp;
61351915Sbostic 	struct buf *bp;
61451915Sbostic 	daddr_t lbn, *lbnp;
61557072Smargo 	int repeat;
61651215Sbostic 
61757072Smargo 	sp = fs->lfs_sp;
61857072Smargo 
61957072Smargo 	repeat = 0;
62051915Sbostic 	/* Advance to the next segment. */
62151927Sbostic 	if (!LFS_PARTIAL_FITS(fs)) {
62252682Sstaelin 		/* Wake up any cleaning procs waiting on this file system. */
62352688Sbostic 		wakeup(&lfs_allclean_wakeup);
62452682Sstaelin 
62551927Sbostic 		lfs_newseg(fs);
62657072Smargo 		repeat = 1;
62751927Sbostic 		fs->lfs_offset = fs->lfs_curseg;
62851915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
62951915Sbostic 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
63051915Sbostic 
63151915Sbostic 		/*
63251927Sbostic 		 * If the segment contains a superblock, update the offset
63351927Sbostic 		 * and summary address to skip over it.
63451915Sbostic 		 */
63552077Sbostic 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
63651927Sbostic 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
63751915Sbostic 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
63851915Sbostic 			sp->seg_bytes_left -= LFS_SBPAD;
63951215Sbostic 		}
64052085Sbostic 		brelse(bp);
64151915Sbostic 	} else {
64251915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
64351915Sbostic 		sp->seg_bytes_left = (fs->lfs_dbpseg -
64451915Sbostic 		    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
64551915Sbostic 	}
64654264Sbostic 	fs->lfs_lastpseg = fs->lfs_offset;
64751342Sbostic 
64855940Sbostic 	sp->fs = fs;
64951915Sbostic 	sp->ibp = NULL;
65051915Sbostic 	sp->ninodes = 0;
65151342Sbostic 
65251915Sbostic 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
65351915Sbostic 	sp->cbpp = sp->bpp;
65456056Sbostic 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
65556056Sbostic 	     LFS_SUMMARY_SIZE);
65664526Sbostic 	sp->segsum = (*sp->cbpp)->b_data;
65757072Smargo 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
65855940Sbostic 	sp->start_bpp = ++sp->cbpp;
65951915Sbostic 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
66051342Sbostic 
66151915Sbostic 	/* Set point to SEGSUM, initialize it. */
66251915Sbostic 	ssp = sp->segsum;
66351915Sbostic 	ssp->ss_next = fs->lfs_nextseg;
66451915Sbostic 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
66551342Sbostic 
66651915Sbostic 	/* Set pointer to first FINFO, initialize it. */
66752085Sbostic 	sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
66851915Sbostic 	sp->fip->fi_nblocks = 0;
66955940Sbostic 	sp->start_lbp = &sp->fip->fi_blocks[0];
67051342Sbostic 
67151915Sbostic 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
67251915Sbostic 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
67357072Smargo 
67457072Smargo 	return(repeat);
67551915Sbostic }
67651342Sbostic 
67751915Sbostic /*
67851915Sbostic  * Return the next segment to write.
67951915Sbostic  */
68052077Sbostic void
68151915Sbostic lfs_newseg(fs)
68251915Sbostic 	struct lfs *fs;
68351915Sbostic {
68451927Sbostic 	CLEANERINFO *cip;
68551915Sbostic 	SEGUSE *sup;
68651915Sbostic 	struct buf *bp;
68755940Sbostic 	int curseg, error, isdirty, sn;
68851915Sbostic 
68955592Sbostic         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
69056159Smargo         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
69156056Sbostic 	sup->su_nbytes = 0;
69256056Sbostic 	sup->su_nsums = 0;
69356056Sbostic 	sup->su_ninos = 0;
69455940Sbostic         (void) VOP_BWRITE(bp);
69551927Sbostic 
69651927Sbostic 	LFS_CLEANERINFO(cip, fs, bp);
69751927Sbostic 	--cip->clean;
69851927Sbostic 	++cip->dirty;
69955940Sbostic 	(void) VOP_BWRITE(bp);
70051927Sbostic 
70151927Sbostic 	fs->lfs_lastseg = fs->lfs_curseg;
70251927Sbostic 	fs->lfs_curseg = fs->lfs_nextseg;
70351927Sbostic 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
70451915Sbostic 		sn = (sn + 1) % fs->lfs_nseg;
70551927Sbostic 		if (sn == curseg)
70651915Sbostic 			panic("lfs_nextseg: no clean segments");
70751915Sbostic 		LFS_SEGENTRY(sup, fs, sn, bp);
70851915Sbostic 		isdirty = sup->su_flags & SEGUSE_DIRTY;
70952085Sbostic 		brelse(bp);
71051915Sbostic 		if (!isdirty)
71151915Sbostic 			break;
71251915Sbostic 	}
71355592Sbostic 
71455940Sbostic 	++fs->lfs_nactive;
71551927Sbostic 	fs->lfs_nextseg = sntoda(fs, sn);
71657072Smargo #ifdef DOSTATS
71757072Smargo 	++lfs_stats.segsused;
71857072Smargo #endif
71951188Sbostic }
72051188Sbostic 
72154264Sbostic int
72251188Sbostic lfs_writeseg(fs, sp)
72351499Sbostic 	struct lfs *fs;
72452085Sbostic 	struct segment *sp;
72551188Sbostic {
72655940Sbostic 	extern int locked_queue_count;
72752688Sbostic 	struct buf **bpp, *bp, *cbp;
72851188Sbostic 	SEGUSE *sup;
72952085Sbostic 	SEGSUM *ssp;
73051860Sbostic 	dev_t i_dev;
73154264Sbostic 	size_t size;
73251860Sbostic 	u_long *datap, *dp;
73355940Sbostic 	int ch_per_blk, do_again, error, i, nblocks, num, s;
73454264Sbostic 	int (*strategy)__P((struct vop_strategy_args *));
73554690Sbostic 	struct vop_strategy_args vop_strategy_a;
73655592Sbostic 	u_short ninos;
73752688Sbostic 	char *p;
73851188Sbostic 
73955940Sbostic 	/*
74055940Sbostic 	 * If there are no buffers other than the segment summary to write
74155940Sbostic 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
74255940Sbostic 	 * even if there aren't any buffers, you need to write the superblock.
74355940Sbostic 	 */
74457072Smargo 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
74555551Sbostic 		return (0);
74652085Sbostic 
74756159Smargo 	ssp = (SEGSUM *)sp->segsum;
74856159Smargo 
74956159Smargo 	/* Update the segment usage information. */
75056159Smargo 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
75156159Smargo 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
75256159Smargo 	sup->su_nbytes += nblocks - 1 - ninos << fs->lfs_bshift;
75356159Smargo 	sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
75456159Smargo 	sup->su_nbytes += LFS_SUMMARY_SIZE;
75556159Smargo 	sup->su_lastmod = time.tv_sec;
75656159Smargo 	sup->su_ninos += ninos;
75756159Smargo 	++sup->su_nsums;
75856159Smargo 	do_again = !(bp->b_flags & B_GATHERED);
75956159Smargo 	(void)VOP_BWRITE(bp);
76051188Sbostic 	/*
76152085Sbostic 	 * Compute checksum across data and then across summary; the first
76252085Sbostic 	 * block (the summary block) is skipped.  Set the create time here
76352085Sbostic 	 * so that it's guaranteed to be later than the inode mod times.
76451860Sbostic 	 *
76551860Sbostic 	 * XXX
76651860Sbostic 	 * Fix this to do it inline, instead of malloc/copy.
76751188Sbostic 	 */
76851860Sbostic 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
76956159Smargo 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
77056159Smargo 		if ((*++bpp)->b_flags & B_INVAL) {
77156159Smargo 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
77256159Smargo 				panic("lfs_writeseg: copyin failed");
77356159Smargo 		} else
77464526Sbostic 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
77556159Smargo 	}
77652103Sbostic 	ssp->ss_create = time.tv_sec;
77755803Sbostic 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
77852085Sbostic 	ssp->ss_sumsum =
77952085Sbostic 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
78051927Sbostic 	free(datap, M_SEGMENT);
78156159Smargo #ifdef DIAGNOSTIC
78256159Smargo 	if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
78356159Smargo 		panic("lfs_writeseg: No diskspace for summary");
78456159Smargo #endif
78555592Sbostic 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
78654264Sbostic 
78751860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
78853574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
78951301Sbostic 
79052688Sbostic 	/*
79152688Sbostic 	 * When we simply write the blocks we lose a rotation for every block
79252688Sbostic 	 * written.  To avoid this problem, we allocate memory in chunks, copy
79357072Smargo 	 * the buffers into the chunk and write the chunk.  MAXPHYS is the
79457072Smargo 	 * largest size I/O devices can handle.
79552688Sbostic 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
79652688Sbostic 	 * and brelse the buffer (which will move them to the LRU list).  Add
79752688Sbostic 	 * the B_CALL flag to the buffer header so we can count I/O's for the
79852688Sbostic 	 * checkpoints and so we can release the allocated memory.
79952688Sbostic 	 *
80052688Sbostic 	 * XXX
80152688Sbostic 	 * This should be removed if the new virtual memory system allows us to
80252688Sbostic 	 * easily make the buffers contiguous in kernel memory and if that's
80352688Sbostic 	 * fast enough.
80452688Sbostic 	 */
80557072Smargo 	ch_per_blk = MAXPHYS / fs->lfs_bsize;
80652688Sbostic 	for (bpp = sp->bpp, i = nblocks; i;) {
80752688Sbostic 		num = ch_per_blk;
80852688Sbostic 		if (num > i)
80952688Sbostic 			num = i;
81052688Sbostic 		i -= num;
81152688Sbostic 		size = num * fs->lfs_bsize;
81252688Sbostic 
81356056Sbostic 		cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
81456056Sbostic 		    (*bpp)->b_blkno, size);
81552688Sbostic 		cbp->b_dev = i_dev;
81655940Sbostic 		cbp->b_flags |= B_ASYNC | B_BUSY;
81752688Sbostic 
81852688Sbostic 		s = splbio();
81952688Sbostic 		++fs->lfs_iocount;
82064526Sbostic 		for (p = cbp->b_data; num--;) {
82152688Sbostic 			bp = *bpp++;
82255940Sbostic 			/*
82355940Sbostic 			 * Fake buffers from the cleaner are marked as B_INVAL.
82455940Sbostic 			 * We need to copy the data from user space rather than
82555940Sbostic 			 * from the buffer indicated.
82655940Sbostic 			 * XXX == what do I do on an error?
82755940Sbostic 			 */
82855940Sbostic 			if (bp->b_flags & B_INVAL) {
82955940Sbostic 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
83055940Sbostic 					panic("lfs_writeseg: copyin failed");
83155940Sbostic 			} else
83264526Sbostic 				bcopy(bp->b_data, p, bp->b_bcount);
83352688Sbostic 			p += bp->b_bcount;
83455940Sbostic 			if (bp->b_flags & B_LOCKED)
83555940Sbostic 				--locked_queue_count;
83655940Sbostic 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
83754264Sbostic 			     B_LOCKED | B_GATHERED);
83855940Sbostic 			if (bp->b_flags & B_CALL) {
83955940Sbostic 				/* if B_CALL, it was created with newbuf */
84055940Sbostic 				brelvp(bp);
84157072Smargo 				if (!(bp->b_flags & B_INVAL))
84264526Sbostic 					free(bp->b_data, M_SEGMENT);
84355940Sbostic 				free(bp, M_SEGMENT);
84455940Sbostic 			} else {
84552688Sbostic 				bremfree(bp);
84656478Smargo 				bp->b_flags |= B_DONE;
84752688Sbostic 				reassignbuf(bp, bp->b_vp);
84855940Sbostic 				brelse(bp);
84952688Sbostic 			}
85051860Sbostic 		}
85156069Sbostic 		++cbp->b_vp->v_numoutput;
85252688Sbostic 		splx(s);
85364526Sbostic 		cbp->b_bcount = p - (char *)cbp->b_data;
85456056Sbostic 		/*
85556056Sbostic 		 * XXXX This is a gross and disgusting hack.  Since these
85656056Sbostic 		 * buffers are physically addressed, they hang off the
85756056Sbostic 		 * device vnode (devvp).  As a result, they have no way
85856056Sbostic 		 * of getting to the LFS superblock or lfs structure to
85956056Sbostic 		 * keep track of the number of I/O's pending.  So, I am
86056056Sbostic 		 * going to stuff the fs into the saveaddr field of
86156056Sbostic 		 * the buffer (yuk).
86256056Sbostic 		 */
86356056Sbostic 		cbp->b_saveaddr = (caddr_t)fs;
86453574Sheideman 		vop_strategy_a.a_desc = VDESC(vop_strategy);
86553574Sheideman 		vop_strategy_a.a_bp = cbp;
86653574Sheideman 		(strategy)(&vop_strategy_a);
86751860Sbostic 	}
86857072Smargo 	/*
86957072Smargo 	 * XXX
87057072Smargo 	 * Vinvalbuf can move locked buffers off the locked queue
87157072Smargo 	 * and we have no way of knowing about this.  So, after
87257072Smargo 	 * doing a big write, we recalculate how many bufers are
87357072Smargo 	 * really still left on the locked queue.
87457072Smargo 	 */
87557072Smargo 	locked_queue_count = count_lock_queue();
87657072Smargo 	wakeup(&locked_queue_count);
87757072Smargo #ifdef DOSTATS
87857072Smargo 	++lfs_stats.psegwrites;
87957072Smargo 	lfs_stats.blocktot += nblocks - 1;
88057072Smargo 	if (fs->lfs_sp->seg_flags & SEGM_SYNC)
88157072Smargo 		++lfs_stats.psyncwrites;
88257072Smargo 	if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
88357072Smargo 		++lfs_stats.pcleanwrites;
88457072Smargo 		lfs_stats.cleanblocks += nblocks - 1;
88557072Smargo 	}
88657072Smargo #endif
88757072Smargo 	return (lfs_initseg(fs) || do_again);
88851188Sbostic }
88951188Sbostic 
89052077Sbostic void
89157072Smargo lfs_writesuper(fs)
89251499Sbostic 	struct lfs *fs;
89351301Sbostic {
89452085Sbostic 	struct buf *bp;
89551860Sbostic 	dev_t i_dev;
89653574Sheideman 	int (*strategy) __P((struct vop_strategy_args *));
89756069Sbostic 	int s;
89854690Sbostic 	struct vop_strategy_args vop_strategy_a;
89951301Sbostic 
90051860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
90153574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
90251356Sbostic 
90351342Sbostic 	/* Checksum the superblock and copy it into a buffer. */
90451499Sbostic 	fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
90556056Sbostic 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
90656056Sbostic 	    LFS_SBPAD);
90764526Sbostic 	*(struct lfs *)bp->b_data = *fs;
90851215Sbostic 
90957072Smargo 	/* XXX Toggle between first two superblocks; for now just write first */
91051860Sbostic 	bp->b_dev = i_dev;
91157072Smargo 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
91257072Smargo 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
91357072Smargo 	bp->b_iodone = lfs_supercallback;
91453574Sheideman 	vop_strategy_a.a_desc = VDESC(vop_strategy);
91553574Sheideman 	vop_strategy_a.a_bp = bp;
91656069Sbostic 	s = splbio();
91757072Smargo 	++bp->b_vp->v_numoutput;
91856069Sbostic 	splx(s);
91953574Sheideman 	(strategy)(&vop_strategy_a);
92051215Sbostic }
92151215Sbostic 
92251342Sbostic /*
92351342Sbostic  * Logical block number match routines used when traversing the dirty block
92451342Sbostic  * chain.
92551342Sbostic  */
92652077Sbostic int
92752077Sbostic lfs_match_data(fs, bp)
92851860Sbostic 	struct lfs *fs;
92952085Sbostic 	struct buf *bp;
93051215Sbostic {
93151342Sbostic 	return (bp->b_lblkno >= 0);
93251215Sbostic }
93351215Sbostic 
93452077Sbostic int
93552077Sbostic lfs_match_indir(fs, bp)
93651860Sbostic 	struct lfs *fs;
93752085Sbostic 	struct buf *bp;
93851215Sbostic {
93951860Sbostic 	int lbn;
94051860Sbostic 
94151860Sbostic 	lbn = bp->b_lblkno;
94251860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
94351215Sbostic }
94451215Sbostic 
94552077Sbostic int
94652077Sbostic lfs_match_dindir(fs, bp)
94751860Sbostic 	struct lfs *fs;
94852085Sbostic 	struct buf *bp;
94951215Sbostic {
95051860Sbostic 	int lbn;
95151860Sbostic 
95251860Sbostic 	lbn = bp->b_lblkno;
95351860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
95451215Sbostic }
95551215Sbostic 
95652077Sbostic int
95752077Sbostic lfs_match_tindir(fs, bp)
95851499Sbostic 	struct lfs *fs;
95952085Sbostic 	struct buf *bp;
96051342Sbostic {
96151860Sbostic 	int lbn;
96251342Sbostic 
96351860Sbostic 	lbn = bp->b_lblkno;
96451860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
96551860Sbostic }
96651342Sbostic 
96751860Sbostic /*
96851860Sbostic  * Allocate a new buffer header.
96951860Sbostic  */
97052085Sbostic struct buf *
97155940Sbostic lfs_newbuf(vp, daddr, size)
97255940Sbostic 	struct vnode *vp;
97351860Sbostic 	daddr_t daddr;
97451860Sbostic 	size_t size;
97551860Sbostic {
97652085Sbostic 	struct buf *bp;
97755940Sbostic 	size_t nbytes;
97851342Sbostic 
97955940Sbostic 	nbytes = roundup(size, DEV_BSIZE);
98057072Smargo 	bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
98157072Smargo 	bzero(bp, sizeof(struct buf));
98257072Smargo 	if (nbytes)
98364526Sbostic 		bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
98455940Sbostic 	bgetvp(vp, bp);
98555940Sbostic 	bp->b_bufsize = size;
98655940Sbostic 	bp->b_bcount = size;
98751860Sbostic 	bp->b_lblkno = daddr;
98851860Sbostic 	bp->b_blkno = daddr;
98951860Sbostic 	bp->b_error = 0;
99051860Sbostic 	bp->b_resid = 0;
99155940Sbostic 	bp->b_iodone = lfs_callback;
99256027Sbostic 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
99351860Sbostic 	return (bp);
99451860Sbostic }
99551342Sbostic 
99653347Sbostic void
99751860Sbostic lfs_callback(bp)
99852085Sbostic 	struct buf *bp;
99951860Sbostic {
100051860Sbostic 	struct lfs *fs;
100151342Sbostic 
100256056Sbostic 	fs = (struct lfs *)bp->b_saveaddr;
100351860Sbostic #ifdef DIAGNOSTIC
100451860Sbostic 	if (fs->lfs_iocount == 0)
100551860Sbostic 		panic("lfs_callback: zero iocount\n");
100651860Sbostic #endif
100751860Sbostic 	if (--fs->lfs_iocount == 0)
100852688Sbostic 		wakeup(&fs->lfs_iocount);
100951915Sbostic 
101055940Sbostic 	brelvp(bp);
101164526Sbostic 	free(bp->b_data, M_SEGMENT);
101255940Sbostic 	free(bp, M_SEGMENT);
101351860Sbostic }
101451342Sbostic 
101555940Sbostic void
101655940Sbostic lfs_supercallback(bp)
101755940Sbostic 	struct buf *bp;
101855940Sbostic {
101955940Sbostic 	brelvp(bp);
102064526Sbostic 	free(bp->b_data, M_SEGMENT);
102155940Sbostic 	free(bp, M_SEGMENT);
102255940Sbostic }
102355940Sbostic 
102451215Sbostic /*
102551188Sbostic  * Shellsort (diminishing increment sort) from Data Structures and
102651188Sbostic  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
102751188Sbostic  * see also Knuth Vol. 3, page 84.  The increments are selected from
102851188Sbostic  * formula (8), page 95.  Roughly O(N^3/2).
102951188Sbostic  */
103051188Sbostic /*
103151188Sbostic  * This is our own private copy of shellsort because we want to sort
103251188Sbostic  * two parallel arrays (the array of buffer pointers and the array of
103351188Sbostic  * logical block numbers) simultaneously.  Note that we cast the array
103451188Sbostic  * of logical block numbers to a unsigned in this routine so that the
103551188Sbostic  * negative block numbers (meta data blocks) sort AFTER the data blocks.
103651188Sbostic  */
103752077Sbostic void
103852077Sbostic lfs_shellsort(bp_array, lb_array, nmemb)
103952085Sbostic 	struct buf **bp_array;
104051215Sbostic 	daddr_t *lb_array;
104151188Sbostic 	register int nmemb;
104251188Sbostic {
104351188Sbostic 	static int __rsshell_increments[] = { 4, 1, 0 };
104451188Sbostic 	register int incr, *incrp, t1, t2;
104552085Sbostic 	struct buf *bp_temp;
104651188Sbostic 	u_long lb_temp;
104751188Sbostic 
104851188Sbostic 	for (incrp = __rsshell_increments; incr = *incrp++;)
104951188Sbostic 		for (t1 = incr; t1 < nmemb; ++t1)
105051188Sbostic 			for (t2 = t1 - incr; t2 >= 0;)
105151188Sbostic 				if (lb_array[t2] > lb_array[t2 + incr]) {
105251188Sbostic 					lb_temp = lb_array[t2];
105351188Sbostic 					lb_array[t2] = lb_array[t2 + incr];
105451188Sbostic 					lb_array[t2 + incr] = lb_temp;
105551188Sbostic 					bp_temp = bp_array[t2];
105651188Sbostic 					bp_array[t2] = bp_array[t2 + incr];
105751188Sbostic 					bp_array[t2 + incr] = bp_temp;
105851188Sbostic 					t2 -= incr;
105951188Sbostic 				} else
106051188Sbostic 					break;
106151188Sbostic }
106255940Sbostic 
106357072Smargo /*
1064*65242Smckusick  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
106557072Smargo  */
106657072Smargo lfs_vref(vp)
106757072Smargo 	register struct vnode *vp;
106857072Smargo {
106957072Smargo 
107057072Smargo 	if (vp->v_flag & VXLOCK)
107157072Smargo 		return(1);
1072*65242Smckusick 	return (vget(vp, 0));
107357072Smargo }
107457072Smargo 
107557072Smargo void
107657072Smargo lfs_vunref(vp)
107757072Smargo 	register struct vnode *vp;
107857072Smargo {
1079*65242Smckusick 	extern int lfs_no_inactive;
108057072Smargo 
108157072Smargo 	/*
1082*65242Smckusick 	 * This is vrele except that we do not want to VOP_INACTIVE
1083*65242Smckusick 	 * this vnode. Rather than inline vrele here, we use a global
1084*65242Smckusick 	 * flag to tell lfs_inactive not to run. Yes, its gross.
108557072Smargo 	 */
1086*65242Smckusick 	lfs_no_inactive = 1;
1087*65242Smckusick 	vrele(vp);
1088*65242Smckusick 	lfs_no_inactive = 0;
108957072Smargo }
1090