xref: /csrg-svn/sys/ufs/lfs/lfs_segment.c (revision 68550)
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*68550Smckusick  *	@(#)lfs_segment.c	8.7 (Berkeley) 03/21/95
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 *));
51*68550Smckusick void	 lfs_iset __P((struct inode *, ufs_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 *));
57*68550Smckusick void	 lfs_shellsort __P((struct buf **, ufs_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 
9554690Sbostic 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
9656159Smargo 	if (fs->lfs_nactive > MAX_ACTIVE)
9757072Smargo 		return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP));
9857072Smargo 	lfs_seglock(fs, SEGM_SYNC);
9957072Smargo 	sp = fs->lfs_sp;
10056159Smargo 
10152328Sbostic 
10257072Smargo 	ip = VTOI(vp);
10365242Smckusick 	if (vp->v_dirtyblkhd.lh_first == NULL)
10457072Smargo 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
10552328Sbostic 
10655551Sbostic 	do {
10755551Sbostic 		do {
10865242Smckusick 			if (vp->v_dirtyblkhd.lh_first != NULL)
10955551Sbostic 				lfs_writefile(fs, sp, vp);
11055551Sbostic 		} while (lfs_writeinode(fs, sp, ip));
11152328Sbostic 
11255551Sbostic 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
11352328Sbostic 
11457072Smargo #ifdef DOSTATS
11557072Smargo 	++lfs_stats.nwrites;
11657072Smargo 	if (sp->seg_flags & SEGM_SYNC)
11757072Smargo 		++lfs_stats.nsync_writes;
11857072Smargo 	if (sp->seg_flags & SEGM_CKP)
11957072Smargo 		++lfs_stats.ncheckpoints;
12057072Smargo #endif
12154690Sbostic 	lfs_segunlock(fs);
12252328Sbostic 	return (0);
12352328Sbostic }
12452328Sbostic 
12554264Sbostic void
12657072Smargo lfs_writevnodes(fs, mp, sp, op)
12754264Sbostic 	struct lfs *fs;
12854264Sbostic 	struct mount *mp;
12954264Sbostic 	struct segment *sp;
13057072Smargo 	int op;
13154264Sbostic {
13254264Sbostic 	struct inode *ip;
13354264Sbostic 	struct vnode *vp;
13454264Sbostic 
13565242Smckusick loop:
13665242Smckusick 	for (vp = mp->mnt_vnodelist.lh_first;
13765242Smckusick 	     vp != NULL;
13865242Smckusick 	     vp = vp->v_mntvnodes.le_next) {
13954264Sbostic 		/*
14054264Sbostic 		 * If the vnode that we are about to sync is no longer
14154264Sbostic 		 * associated with this mount point, start over.
14254264Sbostic 		 */
14354264Sbostic 		if (vp->v_mount != mp)
14454264Sbostic 			goto loop;
14554264Sbostic 
14657072Smargo 		/* XXX ignore dirops for now
14757072Smargo 		if (op == VN_DIROP && !(vp->v_flag & VDIROP) ||
14857072Smargo 		    op != VN_DIROP && (vp->v_flag & VDIROP))
14954264Sbostic 			continue;
15057072Smargo 		*/
15154264Sbostic 
15265242Smckusick 		if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first)
15357072Smargo 			continue;
15457072Smargo 
15557072Smargo 		if (vp->v_type == VNON)
15657072Smargo 			continue;
15757072Smargo 
15857072Smargo 		if (lfs_vref(vp))
15957072Smargo 			continue;
16057072Smargo 
16154264Sbostic 		/*
16254264Sbostic 		 * Write the inode/file if dirty and it's not the
16354264Sbostic 		 * the IFILE.
16454264Sbostic 		 */
16554264Sbostic 		ip = VTOI(vp);
16664611Sbostic 		if ((ip->i_flag &
16764611Sbostic 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) ||
16865242Smckusick 		    vp->v_dirtyblkhd.lh_first != NULL) &&
16954264Sbostic 		    ip->i_number != LFS_IFILE_INUM) {
17065242Smckusick 			if (vp->v_dirtyblkhd.lh_first != NULL)
17154264Sbostic 				lfs_writefile(fs, sp, vp);
17254264Sbostic 			(void) lfs_writeinode(fs, sp, ip);
17354264Sbostic 		}
17454264Sbostic 		vp->v_flag &= ~VDIROP;
17557072Smargo 		lfs_vunref(vp);
17654264Sbostic 	}
17754264Sbostic }
17854264Sbostic 
17952328Sbostic int
18057072Smargo lfs_segwrite(mp, flags)
18152085Sbostic 	struct mount *mp;
18257072Smargo 	int flags;			/* Do a checkpoint. */
18351188Sbostic {
18455592Sbostic 	struct buf *bp;
18552085Sbostic 	struct inode *ip;
18651499Sbostic 	struct lfs *fs;
18752085Sbostic 	struct segment *sp;
18852085Sbostic 	struct vnode *vp;
18955592Sbostic 	SEGUSE *segusep;
190*68550Smckusick 	ufs_daddr_t ibno;
19155940Sbostic 	CLEANERINFO *cip;
19265473Sbostic 	int clean, do_ckp, error, i;
19351188Sbostic 
19452328Sbostic 	fs = VFSTOUFS(mp)->um_lfs;
19555940Sbostic 
19655940Sbostic  	/*
19755940Sbostic  	 * If we have fewer than 2 clean segments, wait until cleaner
19855940Sbostic 	 * writes.
19955940Sbostic  	 */
20055940Sbostic 	do {
20155940Sbostic 		LFS_CLEANERINFO(cip, fs, bp);
20255940Sbostic 		clean = cip->clean;
20355940Sbostic 		brelse(bp);
20455940Sbostic 		if (clean <= 2) {
20555940Sbostic 			printf ("segs clean: %d\n", clean);
20655940Sbostic 			wakeup(&lfs_allclean_wakeup);
20755940Sbostic 			if (error = tsleep(&fs->lfs_avail, PRIBIO + 1,
20855940Sbostic 			    "lfs writer", 0))
20955940Sbostic 				return (error);
21055940Sbostic 		}
21155940Sbostic 	} while (clean <= 2 );
21252085Sbostic 
21351860Sbostic 	/*
21452328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
21552328Sbostic 	 * the maximum possible number of buffers which can be described in a
21652328Sbostic 	 * single summary block.
21752328Sbostic 	 */
21857072Smargo 	do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE;
21957072Smargo 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
22057072Smargo 	sp = fs->lfs_sp;
22152328Sbostic 
22257072Smargo 	lfs_writevnodes(fs, mp, sp, VN_REG);
22351342Sbostic 
22457072Smargo 	/* XXX ignore ordering of dirops for now */
22557072Smargo 	/* XXX
22654264Sbostic 	fs->lfs_writer = 1;
22754264Sbostic 	if (fs->lfs_dirops && (error =
22854264Sbostic 	    tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
22954264Sbostic 		free(sp->bpp, M_SEGMENT);
23054264Sbostic 		free(sp, M_SEGMENT);
23154264Sbostic 		fs->lfs_writer = 0;
23255551Sbostic 		return (error);
23354264Sbostic 	}
23451860Sbostic 
23557072Smargo 	lfs_writevnodes(fs, mp, sp, VN_DIROP);
23657072Smargo 	*/
23751860Sbostic 
23854264Sbostic 	/*
23955592Sbostic 	 * If we are doing a checkpoint, mark everything since the
24055592Sbostic 	 * last checkpoint as no longer ACTIVE.
24154264Sbostic 	 */
24255592Sbostic 	if (do_ckp)
24355592Sbostic 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
24455592Sbostic 		     --ibno >= fs->lfs_cleansz; ) {
24555592Sbostic 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
24655592Sbostic 			    NOCRED, &bp))
24755592Sbostic 
24855592Sbostic 				panic("lfs: ifile read");
24964526Sbostic 			segusep = (SEGUSE *)bp->b_data;
25055592Sbostic 			for (i = fs->lfs_sepb; i--; segusep++)
25155592Sbostic 				segusep->su_flags &= ~SEGUSE_ACTIVE;
25255592Sbostic 
25355940Sbostic 			error = VOP_BWRITE(bp);
25455592Sbostic 		}
25555592Sbostic 
25654264Sbostic 	if (do_ckp || fs->lfs_doifile) {
25756086Sbostic redo:
25854264Sbostic 		vp = fs->lfs_ivnode;
25965242Smckusick 		while (vget(vp, 1));
26052328Sbostic 		ip = VTOI(vp);
26165242Smckusick 		if (vp->v_dirtyblkhd.lh_first != NULL)
26255592Sbostic 			lfs_writefile(fs, sp, vp);
26355592Sbostic 		(void)lfs_writeinode(fs, sp, ip);
26452077Sbostic 		vput(vp);
26557072Smargo 		if (lfs_writeseg(fs, sp) && do_ckp)
26656086Sbostic 			goto redo;
26754264Sbostic 	} else
26854264Sbostic 		(void) lfs_writeseg(fs, sp);
26951342Sbostic 
27051215Sbostic 	/*
27151860Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
27251860Sbostic 	 * moment, the user's process hangs around so we can sleep.
27351215Sbostic 	 */
27457072Smargo 	/* XXX ignore dirops for now
27554264Sbostic 	fs->lfs_writer = 0;
27654264Sbostic 	fs->lfs_doifile = 0;
27754264Sbostic 	wakeup(&fs->lfs_dirops);
27857072Smargo 	*/
27954264Sbostic 
28057072Smargo #ifdef DOSTATS
28157072Smargo 	++lfs_stats.nwrites;
28257072Smargo 	if (sp->seg_flags & SEGM_SYNC)
28357072Smargo 		++lfs_stats.nsync_writes;
28457072Smargo 	if (sp->seg_flags & SEGM_CKP)
28557072Smargo 		++lfs_stats.ncheckpoints;
28657072Smargo #endif
28754690Sbostic 	lfs_segunlock(fs);
28851860Sbostic 	return (0);
28951188Sbostic }
29051188Sbostic 
29151860Sbostic /*
29251860Sbostic  * Write the dirty blocks associated with a vnode.
29351860Sbostic  */
29452077Sbostic void
29551860Sbostic lfs_writefile(fs, sp, vp)
29651499Sbostic 	struct lfs *fs;
29752085Sbostic 	struct segment *sp;
29852085Sbostic 	struct vnode *vp;
29951188Sbostic {
30051860Sbostic 	struct buf *bp;
30152085Sbostic 	struct finfo *fip;
30251860Sbostic 	IFILE *ifp;
30351188Sbostic 
30452085Sbostic 	if (sp->seg_bytes_left < fs->lfs_bsize ||
30557072Smargo 	    sp->sum_bytes_left < sizeof(struct finfo))
30654264Sbostic 		(void) lfs_writeseg(fs, sp);
30757072Smargo 
308*68550Smckusick 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
30956478Smargo 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
31051215Sbostic 
31152085Sbostic 	fip = sp->fip;
31252085Sbostic 	fip->fi_nblocks = 0;
31352085Sbostic 	fip->fi_ino = VTOI(vp)->i_number;
31452085Sbostic 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
31552085Sbostic 	fip->fi_version = ifp->if_version;
31652085Sbostic 	brelse(bp);
31751188Sbostic 
31852085Sbostic 	/*
31952085Sbostic 	 * It may not be necessary to write the meta-data blocks at this point,
32052085Sbostic 	 * as the roll-forward recovery code should be able to reconstruct the
32152085Sbostic 	 * list.
32252085Sbostic 	 */
32352085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_data);
32452085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_indir);
32552085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_dindir);
32651860Sbostic #ifdef TRIPLE
32752085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_tindir);
32851860Sbostic #endif
32951342Sbostic 
33052085Sbostic 	fip = sp->fip;
33152085Sbostic 	if (fip->fi_nblocks != 0) {
33252085Sbostic 		sp->fip =
33352085Sbostic 		    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
334*68550Smckusick 		    sizeof(ufs_daddr_t) * (fip->fi_nblocks - 1));
33555940Sbostic 		sp->start_lbp = &sp->fip->fi_blocks[0];
33656478Smargo 	} else {
337*68550Smckusick 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(ufs_daddr_t);
33856478Smargo 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
33956478Smargo 	}
34051215Sbostic }
34151215Sbostic 
34254264Sbostic int
34351915Sbostic lfs_writeinode(fs, sp, ip)
34451915Sbostic 	struct lfs *fs;
34552085Sbostic 	struct segment *sp;
34652085Sbostic 	struct inode *ip;
34751915Sbostic {
34852085Sbostic 	struct buf *bp, *ibp;
34952077Sbostic 	IFILE *ifp;
35052682Sstaelin 	SEGUSE *sup;
351*68550Smckusick 	ufs_daddr_t daddr;
35252077Sbostic 	ino_t ino;
35357072Smargo 	int error, i, ndx;
35454264Sbostic 	int redo_ifile = 0;
35551915Sbostic 
35664611Sbostic 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)))
35756190Smargo 		return(0);
35855940Sbostic 
35951915Sbostic 	/* Allocate a new inode block if necessary. */
36051915Sbostic 	if (sp->ibp == NULL) {
36151915Sbostic 		/* Allocate a new segment if necessary. */
36251915Sbostic 		if (sp->seg_bytes_left < fs->lfs_bsize ||
363*68550Smckusick 		    sp->sum_bytes_left < sizeof(ufs_daddr_t))
36454264Sbostic 			(void) lfs_writeseg(fs, sp);
36551915Sbostic 
36651915Sbostic 		/* Get next inode block. */
36752682Sstaelin 		daddr = fs->lfs_offset;
36851915Sbostic 		fs->lfs_offset += fsbtodb(fs, 1);
36951915Sbostic 		sp->ibp = *sp->cbpp++ =
37056056Sbostic 		    lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
37156056Sbostic 		    fs->lfs_bsize);
37257072Smargo 		/* Zero out inode numbers */
37357072Smargo 		for (i = 0; i < INOPB(fs); ++i)
37464526Sbostic 			((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
37555940Sbostic 		++sp->start_bpp;
37655940Sbostic 		fs->lfs_avail -= fsbtodb(fs, 1);
37752688Sbostic 		/* Set remaining space counters. */
37851915Sbostic 		sp->seg_bytes_left -= fs->lfs_bsize;
379*68550Smckusick 		sp->sum_bytes_left -= sizeof(ufs_daddr_t);
380*68550Smckusick 		ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
38151915Sbostic 		    sp->ninodes / INOPB(fs) - 1;
382*68550Smckusick 		((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
38351915Sbostic 	}
38451915Sbostic 
38552085Sbostic 	/* Update the inode times and copy the inode onto the inode page. */
38664611Sbostic 	if (ip->i_flag & IN_MODIFIED)
38756056Sbostic 		--fs->lfs_uinodes;
38852077Sbostic 	ITIMES(ip, &time, &time);
38964611Sbostic 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
39051915Sbostic 	bp = sp->ibp;
39164526Sbostic 	((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] = ip->i_din;
39251915Sbostic 	/* Increment inode count in segment summary block. */
39351915Sbostic 	++((SEGSUM *)(sp->segsum))->ss_ninos;
39451915Sbostic 
39551915Sbostic 	/* If this page is full, set flag to allocate a new page. */
39651915Sbostic 	if (++sp->ninodes % INOPB(fs) == 0)
39751915Sbostic 		sp->ibp = NULL;
39851915Sbostic 
39951915Sbostic 	/*
40052077Sbostic 	 * If updating the ifile, update the super-block.  Update the disk
40152077Sbostic 	 * address and access times for this inode in the ifile.
40251915Sbostic 	 */
40352077Sbostic 	ino = ip->i_number;
40455696Sbostic 	if (ino == LFS_IFILE_INUM) {
40555696Sbostic 		daddr = fs->lfs_idaddr;
40651915Sbostic 		fs->lfs_idaddr = bp->b_blkno;
40755696Sbostic 	} else {
40855696Sbostic 		LFS_IENTRY(ifp, fs, ino, ibp);
40955696Sbostic 		daddr = ifp->if_daddr;
41055696Sbostic 		ifp->if_daddr = bp->b_blkno;
41155940Sbostic 		error = VOP_BWRITE(ibp);
41255696Sbostic 	}
41352077Sbostic 
41454264Sbostic 	/*
41554264Sbostic 	 * No need to update segment usage if there was no former inode address
41654264Sbostic 	 * or if the last inode address is in the current partial segment.
41754264Sbostic 	 */
41854264Sbostic 	if (daddr != LFS_UNUSED_DADDR &&
41955803Sbostic 	    !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
42052682Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
42152682Sstaelin #ifdef DIAGNOSTIC
42254264Sbostic 		if (sup->su_nbytes < sizeof(struct dinode)) {
42352819Sbostic 			/* XXX -- Change to a panic. */
42452819Sbostic 			printf("lfs: negative bytes (segment %d)\n",
42552682Sstaelin 			    datosn(fs, daddr));
42654264Sbostic 			panic("negative bytes");
42754264Sbostic 		}
42852682Sstaelin #endif
42952682Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
43056069Sbostic 		redo_ifile =
43156069Sbostic 		    (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
43255940Sbostic 		error = VOP_BWRITE(bp);
43352682Sstaelin 	}
43455551Sbostic 	return (redo_ifile);
43551915Sbostic }
43651915Sbostic 
43755940Sbostic int
43855940Sbostic lfs_gatherblock(sp, bp, sptr)
43955940Sbostic 	struct segment *sp;
44055940Sbostic 	struct buf *bp;
44155940Sbostic 	int *sptr;
44255940Sbostic {
44355940Sbostic 	struct lfs *fs;
44455940Sbostic 	int version;
44555940Sbostic 
44655940Sbostic 	/*
44755940Sbostic 	 * If full, finish this segment.  We may be doing I/O, so
44855940Sbostic 	 * release and reacquire the splbio().
44955940Sbostic 	 */
45056027Sbostic #ifdef DIAGNOSTIC
45156027Sbostic 	if (sp->vp == NULL)
45256027Sbostic 		panic ("lfs_gatherblock: Null vp in segment");
45356027Sbostic #endif
45455940Sbostic 	fs = sp->fs;
455*68550Smckusick 	if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
45655940Sbostic 	    sp->seg_bytes_left < fs->lfs_bsize) {
45755940Sbostic 		if (sptr)
45855940Sbostic 			splx(*sptr);
45956027Sbostic 		lfs_updatemeta(sp);
46055940Sbostic 
46155940Sbostic 		version = sp->fip->fi_version;
46255940Sbostic 		(void) lfs_writeseg(fs, sp);
46355940Sbostic 
46455940Sbostic 		sp->fip->fi_version = version;
46556027Sbostic 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
46656478Smargo 		/* Add the current file to the segment summary. */
46756478Smargo 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
46855940Sbostic 		sp->sum_bytes_left -=
469*68550Smckusick 		    sizeof(struct finfo) - sizeof(ufs_daddr_t);
47055940Sbostic 
47155940Sbostic 		if (sptr)
47255940Sbostic 			*sptr = splbio();
47355940Sbostic 		return(1);
47455940Sbostic 	}
47555940Sbostic 
47655940Sbostic 	/* Insert into the buffer list, update the FINFO block. */
47755940Sbostic 	bp->b_flags |= B_GATHERED;
47855940Sbostic 	*sp->cbpp++ = bp;
47955940Sbostic 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
48055940Sbostic 
481*68550Smckusick 	sp->sum_bytes_left -= sizeof(ufs_daddr_t);
48257072Smargo 	sp->seg_bytes_left -= fs->lfs_bsize;
48355940Sbostic 	return(0);
48455940Sbostic }
48555940Sbostic 
48652077Sbostic void
48751215Sbostic lfs_gather(fs, sp, vp, match)
48851499Sbostic 	struct lfs *fs;
48952085Sbostic 	struct segment *sp;
49052085Sbostic 	struct vnode *vp;
49152085Sbostic 	int (*match) __P((struct lfs *, struct buf *));
49251215Sbostic {
49355940Sbostic 	struct buf *bp;
49451342Sbostic 	int s;
49551215Sbostic 
49656027Sbostic 	sp->vp = vp;
49755940Sbostic 	s = splbio();
49865242Smckusick loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
49954264Sbostic 		if (bp->b_flags & B_BUSY || !match(fs, bp) ||
50054264Sbostic 		    bp->b_flags & B_GATHERED)
50151215Sbostic 			continue;
50251342Sbostic #ifdef DIAGNOSTIC
50351860Sbostic 		if (!(bp->b_flags & B_DELWRI))
50451915Sbostic 			panic("lfs_gather: bp not B_DELWRI");
50551860Sbostic 		if (!(bp->b_flags & B_LOCKED))
50651915Sbostic 			panic("lfs_gather: bp not B_LOCKED");
50751342Sbostic #endif
50855940Sbostic 		if (lfs_gatherblock(sp, bp, &s))
50953145Sstaelin 			goto loop;
51051188Sbostic 	}
51151215Sbostic 	splx(s);
51256027Sbostic 	lfs_updatemeta(sp);
51356027Sbostic 	sp->vp = NULL;
51451188Sbostic }
51551188Sbostic 
51655940Sbostic 
51751342Sbostic /*
51851342Sbostic  * Update the metadata that points to the blocks listed in the FINFO
51951188Sbostic  * array.
52051188Sbostic  */
52152077Sbostic void
52256027Sbostic lfs_updatemeta(sp)
52352085Sbostic 	struct segment *sp;
52451188Sbostic {
52551915Sbostic 	SEGUSE *sup;
52652085Sbostic 	struct buf *bp;
52755940Sbostic 	struct lfs *fs;
52856027Sbostic 	struct vnode *vp;
52956478Smargo 	struct indir a[NIADDR + 2], *ap;
53052085Sbostic 	struct inode *ip;
531*68550Smckusick 	ufs_daddr_t daddr, lbn, off;
53255940Sbostic 	int db_per_fsb, error, i, nblocks, num;
53351188Sbostic 
53456027Sbostic 	vp = sp->vp;
53555940Sbostic 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
53656027Sbostic 	if (vp == NULL || nblocks == 0)
53751215Sbostic 		return;
53851215Sbostic 
53951915Sbostic 	/* Sort the blocks. */
54055940Sbostic 	if (!(sp->seg_flags & SEGM_CLEAN))
54155940Sbostic 		lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
54251215Sbostic 
54351915Sbostic 	/*
54451915Sbostic 	 * Assign disk addresses, and update references to the logical
54551915Sbostic 	 * block and the segment usage information.
54651915Sbostic 	 */
54755940Sbostic 	fs = sp->fs;
54851860Sbostic 	db_per_fsb = fsbtodb(fs, 1);
54955940Sbostic 	for (i = nblocks; i--; ++sp->start_bpp) {
55055940Sbostic 		lbn = *sp->start_lbp++;
55155940Sbostic 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
55251860Sbostic 		fs->lfs_offset += db_per_fsb;
55351215Sbostic 
55456478Smargo 		if (error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL))
55556478Smargo 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
55651860Sbostic 		ip = VTOI(vp);
55751860Sbostic 		switch (num) {
55851860Sbostic 		case 0:
55951915Sbostic 			ip->i_db[lbn] = off;
56051860Sbostic 			break;
56151860Sbostic 		case 1:
56251915Sbostic 			ip->i_ib[a[0].in_off] = off;
56351860Sbostic 			break;
56451860Sbostic 		default:
56551860Sbostic 			ap = &a[num - 1];
56651860Sbostic 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
56751860Sbostic 				panic("lfs_updatemeta: bread bno %d",
56851860Sbostic 				    ap->in_lbn);
56955458Sbostic 			/*
57055458Sbostic 			 * Bread may create a new indirect block which needs
57155458Sbostic 			 * to get counted for the inode.
57255458Sbostic 			 */
57355592Sbostic 			if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
57455940Sbostic printf ("Updatemeta allocating indirect block: shouldn't happen\n");
57555458Sbostic 				ip->i_blocks += btodb(fs->lfs_bsize);
57655592Sbostic 				fs->lfs_bfree -= btodb(fs->lfs_bsize);
57755592Sbostic 			}
578*68550Smckusick 			((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
57953530Sheideman 			VOP_BWRITE(bp);
58051188Sbostic 		}
58151915Sbostic 
58251915Sbostic 		/* Update segment usage information. */
58357072Smargo 		if (daddr != UNASSIGNED &&
58457072Smargo 		    !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
58551915Sbostic 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
58651915Sbostic #ifdef DIAGNOSTIC
58754264Sbostic 			if (sup->su_nbytes < fs->lfs_bsize) {
58852819Sbostic 				/* XXX -- Change to a panic. */
58952819Sbostic 				printf("lfs: negative bytes (segment %d)\n",
59051915Sbostic 				    datosn(fs, daddr));
59154264Sbostic 				panic ("Negative Bytes");
59254264Sbostic 			}
59351915Sbostic #endif
59451915Sbostic 			sup->su_nbytes -= fs->lfs_bsize;
59555940Sbostic 			error = VOP_BWRITE(bp);
59651915Sbostic 		}
59751188Sbostic 	}
59851188Sbostic }
59951188Sbostic 
60051915Sbostic /*
60151915Sbostic  * Start a new segment.
60251915Sbostic  */
60357072Smargo int
60457072Smargo lfs_initseg(fs)
60551499Sbostic 	struct lfs *fs;
60657072Smargo {
60752085Sbostic 	struct segment *sp;
60851915Sbostic 	SEGUSE *sup;
60951915Sbostic 	SEGSUM *ssp;
61051915Sbostic 	struct buf *bp;
61157072Smargo 	int repeat;
61251215Sbostic 
61357072Smargo 	sp = fs->lfs_sp;
61457072Smargo 
61557072Smargo 	repeat = 0;
61651915Sbostic 	/* Advance to the next segment. */
61751927Sbostic 	if (!LFS_PARTIAL_FITS(fs)) {
61852682Sstaelin 		/* Wake up any cleaning procs waiting on this file system. */
61952688Sbostic 		wakeup(&lfs_allclean_wakeup);
62052682Sstaelin 
62151927Sbostic 		lfs_newseg(fs);
62257072Smargo 		repeat = 1;
62351927Sbostic 		fs->lfs_offset = fs->lfs_curseg;
62451915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
62551915Sbostic 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
62651915Sbostic 
62751915Sbostic 		/*
62851927Sbostic 		 * If the segment contains a superblock, update the offset
62951927Sbostic 		 * and summary address to skip over it.
63051915Sbostic 		 */
63152077Sbostic 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
63251927Sbostic 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
63351915Sbostic 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
63451915Sbostic 			sp->seg_bytes_left -= LFS_SBPAD;
63551215Sbostic 		}
63652085Sbostic 		brelse(bp);
63751915Sbostic 	} else {
63851915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
63951915Sbostic 		sp->seg_bytes_left = (fs->lfs_dbpseg -
64051915Sbostic 		    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
64151915Sbostic 	}
64254264Sbostic 	fs->lfs_lastpseg = fs->lfs_offset;
64351342Sbostic 
64455940Sbostic 	sp->fs = fs;
64551915Sbostic 	sp->ibp = NULL;
64651915Sbostic 	sp->ninodes = 0;
64751342Sbostic 
64851915Sbostic 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
64951915Sbostic 	sp->cbpp = sp->bpp;
65056056Sbostic 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
65156056Sbostic 	     LFS_SUMMARY_SIZE);
65264526Sbostic 	sp->segsum = (*sp->cbpp)->b_data;
65357072Smargo 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
65455940Sbostic 	sp->start_bpp = ++sp->cbpp;
65551915Sbostic 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
65651342Sbostic 
65751915Sbostic 	/* Set point to SEGSUM, initialize it. */
65851915Sbostic 	ssp = sp->segsum;
65951915Sbostic 	ssp->ss_next = fs->lfs_nextseg;
66051915Sbostic 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
66151342Sbostic 
66251915Sbostic 	/* Set pointer to first FINFO, initialize it. */
66368119Smckusick 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
66451915Sbostic 	sp->fip->fi_nblocks = 0;
66555940Sbostic 	sp->start_lbp = &sp->fip->fi_blocks[0];
66651342Sbostic 
66751915Sbostic 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
66851915Sbostic 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
66957072Smargo 
67057072Smargo 	return(repeat);
67151915Sbostic }
67251342Sbostic 
67351915Sbostic /*
67451915Sbostic  * Return the next segment to write.
67551915Sbostic  */
67652077Sbostic void
67751915Sbostic lfs_newseg(fs)
67851915Sbostic 	struct lfs *fs;
67951915Sbostic {
68051927Sbostic 	CLEANERINFO *cip;
68151915Sbostic 	SEGUSE *sup;
68251915Sbostic 	struct buf *bp;
68365473Sbostic 	int curseg, isdirty, sn;
68451915Sbostic 
68555592Sbostic         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
68656159Smargo         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
68756056Sbostic 	sup->su_nbytes = 0;
68856056Sbostic 	sup->su_nsums = 0;
68956056Sbostic 	sup->su_ninos = 0;
69055940Sbostic         (void) VOP_BWRITE(bp);
69151927Sbostic 
69251927Sbostic 	LFS_CLEANERINFO(cip, fs, bp);
69351927Sbostic 	--cip->clean;
69451927Sbostic 	++cip->dirty;
69555940Sbostic 	(void) VOP_BWRITE(bp);
69651927Sbostic 
69751927Sbostic 	fs->lfs_lastseg = fs->lfs_curseg;
69851927Sbostic 	fs->lfs_curseg = fs->lfs_nextseg;
69951927Sbostic 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
70051915Sbostic 		sn = (sn + 1) % fs->lfs_nseg;
70151927Sbostic 		if (sn == curseg)
70251915Sbostic 			panic("lfs_nextseg: no clean segments");
70351915Sbostic 		LFS_SEGENTRY(sup, fs, sn, bp);
70451915Sbostic 		isdirty = sup->su_flags & SEGUSE_DIRTY;
70552085Sbostic 		brelse(bp);
70651915Sbostic 		if (!isdirty)
70751915Sbostic 			break;
70851915Sbostic 	}
70955592Sbostic 
71055940Sbostic 	++fs->lfs_nactive;
71151927Sbostic 	fs->lfs_nextseg = sntoda(fs, sn);
71257072Smargo #ifdef DOSTATS
71357072Smargo 	++lfs_stats.segsused;
71457072Smargo #endif
71551188Sbostic }
71651188Sbostic 
71754264Sbostic int
71851188Sbostic lfs_writeseg(fs, sp)
71951499Sbostic 	struct lfs *fs;
72052085Sbostic 	struct segment *sp;
72151188Sbostic {
72255940Sbostic 	extern int locked_queue_count;
72352688Sbostic 	struct buf **bpp, *bp, *cbp;
72451188Sbostic 	SEGUSE *sup;
72552085Sbostic 	SEGSUM *ssp;
72651860Sbostic 	dev_t i_dev;
72754264Sbostic 	size_t size;
72851860Sbostic 	u_long *datap, *dp;
72965473Sbostic 	int ch_per_blk, do_again, i, nblocks, num, s;
73054264Sbostic 	int (*strategy)__P((struct vop_strategy_args *));
73154690Sbostic 	struct vop_strategy_args vop_strategy_a;
73255592Sbostic 	u_short ninos;
73352688Sbostic 	char *p;
73451188Sbostic 
73555940Sbostic 	/*
73655940Sbostic 	 * If there are no buffers other than the segment summary to write
73755940Sbostic 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
73855940Sbostic 	 * even if there aren't any buffers, you need to write the superblock.
73955940Sbostic 	 */
74057072Smargo 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
74155551Sbostic 		return (0);
74252085Sbostic 
74356159Smargo 	ssp = (SEGSUM *)sp->segsum;
74456159Smargo 
74556159Smargo 	/* Update the segment usage information. */
74656159Smargo 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
74756159Smargo 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
74856159Smargo 	sup->su_nbytes += nblocks - 1 - ninos << fs->lfs_bshift;
74956159Smargo 	sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
75056159Smargo 	sup->su_nbytes += LFS_SUMMARY_SIZE;
75156159Smargo 	sup->su_lastmod = time.tv_sec;
75256159Smargo 	sup->su_ninos += ninos;
75356159Smargo 	++sup->su_nsums;
75456159Smargo 	do_again = !(bp->b_flags & B_GATHERED);
75556159Smargo 	(void)VOP_BWRITE(bp);
75651188Sbostic 	/*
75752085Sbostic 	 * Compute checksum across data and then across summary; the first
75852085Sbostic 	 * block (the summary block) is skipped.  Set the create time here
75952085Sbostic 	 * so that it's guaranteed to be later than the inode mod times.
76051860Sbostic 	 *
76151860Sbostic 	 * XXX
76251860Sbostic 	 * Fix this to do it inline, instead of malloc/copy.
76351188Sbostic 	 */
76451860Sbostic 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
76556159Smargo 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
76656159Smargo 		if ((*++bpp)->b_flags & B_INVAL) {
76756159Smargo 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
76856159Smargo 				panic("lfs_writeseg: copyin failed");
76956159Smargo 		} else
77064526Sbostic 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
77156159Smargo 	}
77252103Sbostic 	ssp->ss_create = time.tv_sec;
77355803Sbostic 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
77452085Sbostic 	ssp->ss_sumsum =
77552085Sbostic 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
77651927Sbostic 	free(datap, M_SEGMENT);
77756159Smargo #ifdef DIAGNOSTIC
77856159Smargo 	if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
77956159Smargo 		panic("lfs_writeseg: No diskspace for summary");
78056159Smargo #endif
78155592Sbostic 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
78254264Sbostic 
78351860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
78453574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
78551301Sbostic 
78652688Sbostic 	/*
78752688Sbostic 	 * When we simply write the blocks we lose a rotation for every block
78852688Sbostic 	 * written.  To avoid this problem, we allocate memory in chunks, copy
78957072Smargo 	 * the buffers into the chunk and write the chunk.  MAXPHYS is the
79057072Smargo 	 * largest size I/O devices can handle.
79152688Sbostic 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
79252688Sbostic 	 * and brelse the buffer (which will move them to the LRU list).  Add
79352688Sbostic 	 * the B_CALL flag to the buffer header so we can count I/O's for the
79452688Sbostic 	 * checkpoints and so we can release the allocated memory.
79552688Sbostic 	 *
79652688Sbostic 	 * XXX
79752688Sbostic 	 * This should be removed if the new virtual memory system allows us to
79852688Sbostic 	 * easily make the buffers contiguous in kernel memory and if that's
79952688Sbostic 	 * fast enough.
80052688Sbostic 	 */
80157072Smargo 	ch_per_blk = MAXPHYS / fs->lfs_bsize;
80252688Sbostic 	for (bpp = sp->bpp, i = nblocks; i;) {
80352688Sbostic 		num = ch_per_blk;
80452688Sbostic 		if (num > i)
80552688Sbostic 			num = i;
80652688Sbostic 		i -= num;
80752688Sbostic 		size = num * fs->lfs_bsize;
80852688Sbostic 
80956056Sbostic 		cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
81056056Sbostic 		    (*bpp)->b_blkno, size);
81152688Sbostic 		cbp->b_dev = i_dev;
81255940Sbostic 		cbp->b_flags |= B_ASYNC | B_BUSY;
81352688Sbostic 
81452688Sbostic 		s = splbio();
81552688Sbostic 		++fs->lfs_iocount;
81664526Sbostic 		for (p = cbp->b_data; num--;) {
81752688Sbostic 			bp = *bpp++;
81855940Sbostic 			/*
81955940Sbostic 			 * Fake buffers from the cleaner are marked as B_INVAL.
82055940Sbostic 			 * We need to copy the data from user space rather than
82155940Sbostic 			 * from the buffer indicated.
82255940Sbostic 			 * XXX == what do I do on an error?
82355940Sbostic 			 */
82455940Sbostic 			if (bp->b_flags & B_INVAL) {
82555940Sbostic 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
82655940Sbostic 					panic("lfs_writeseg: copyin failed");
82755940Sbostic 			} else
82864526Sbostic 				bcopy(bp->b_data, p, bp->b_bcount);
82952688Sbostic 			p += bp->b_bcount;
83055940Sbostic 			if (bp->b_flags & B_LOCKED)
83155940Sbostic 				--locked_queue_count;
83255940Sbostic 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
83354264Sbostic 			     B_LOCKED | B_GATHERED);
83455940Sbostic 			if (bp->b_flags & B_CALL) {
83555940Sbostic 				/* if B_CALL, it was created with newbuf */
83655940Sbostic 				brelvp(bp);
83757072Smargo 				if (!(bp->b_flags & B_INVAL))
83864526Sbostic 					free(bp->b_data, M_SEGMENT);
83955940Sbostic 				free(bp, M_SEGMENT);
84055940Sbostic 			} else {
84152688Sbostic 				bremfree(bp);
84256478Smargo 				bp->b_flags |= B_DONE;
84352688Sbostic 				reassignbuf(bp, bp->b_vp);
84455940Sbostic 				brelse(bp);
84552688Sbostic 			}
84651860Sbostic 		}
84756069Sbostic 		++cbp->b_vp->v_numoutput;
84852688Sbostic 		splx(s);
84964526Sbostic 		cbp->b_bcount = p - (char *)cbp->b_data;
85056056Sbostic 		/*
85156056Sbostic 		 * XXXX This is a gross and disgusting hack.  Since these
85256056Sbostic 		 * buffers are physically addressed, they hang off the
85356056Sbostic 		 * device vnode (devvp).  As a result, they have no way
85456056Sbostic 		 * of getting to the LFS superblock or lfs structure to
85556056Sbostic 		 * keep track of the number of I/O's pending.  So, I am
85656056Sbostic 		 * going to stuff the fs into the saveaddr field of
85756056Sbostic 		 * the buffer (yuk).
85856056Sbostic 		 */
85956056Sbostic 		cbp->b_saveaddr = (caddr_t)fs;
86053574Sheideman 		vop_strategy_a.a_desc = VDESC(vop_strategy);
86153574Sheideman 		vop_strategy_a.a_bp = cbp;
86253574Sheideman 		(strategy)(&vop_strategy_a);
86351860Sbostic 	}
86457072Smargo 	/*
86557072Smargo 	 * XXX
86657072Smargo 	 * Vinvalbuf can move locked buffers off the locked queue
86757072Smargo 	 * and we have no way of knowing about this.  So, after
86857072Smargo 	 * doing a big write, we recalculate how many bufers are
86957072Smargo 	 * really still left on the locked queue.
87057072Smargo 	 */
87157072Smargo 	locked_queue_count = count_lock_queue();
87257072Smargo 	wakeup(&locked_queue_count);
87357072Smargo #ifdef DOSTATS
87457072Smargo 	++lfs_stats.psegwrites;
87557072Smargo 	lfs_stats.blocktot += nblocks - 1;
87657072Smargo 	if (fs->lfs_sp->seg_flags & SEGM_SYNC)
87757072Smargo 		++lfs_stats.psyncwrites;
87857072Smargo 	if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
87957072Smargo 		++lfs_stats.pcleanwrites;
88057072Smargo 		lfs_stats.cleanblocks += nblocks - 1;
88157072Smargo 	}
88257072Smargo #endif
88357072Smargo 	return (lfs_initseg(fs) || do_again);
88451188Sbostic }
88551188Sbostic 
88652077Sbostic void
88757072Smargo lfs_writesuper(fs)
88851499Sbostic 	struct lfs *fs;
88951301Sbostic {
89052085Sbostic 	struct buf *bp;
89151860Sbostic 	dev_t i_dev;
89253574Sheideman 	int (*strategy) __P((struct vop_strategy_args *));
89356069Sbostic 	int s;
89454690Sbostic 	struct vop_strategy_args vop_strategy_a;
89551301Sbostic 
89651860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
89753574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
89851356Sbostic 
89951342Sbostic 	/* Checksum the superblock and copy it into a buffer. */
90051499Sbostic 	fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
90156056Sbostic 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
90256056Sbostic 	    LFS_SBPAD);
90364526Sbostic 	*(struct lfs *)bp->b_data = *fs;
90451215Sbostic 
90557072Smargo 	/* XXX Toggle between first two superblocks; for now just write first */
90651860Sbostic 	bp->b_dev = i_dev;
90757072Smargo 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
90857072Smargo 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
90957072Smargo 	bp->b_iodone = lfs_supercallback;
91053574Sheideman 	vop_strategy_a.a_desc = VDESC(vop_strategy);
91153574Sheideman 	vop_strategy_a.a_bp = bp;
91256069Sbostic 	s = splbio();
91357072Smargo 	++bp->b_vp->v_numoutput;
91456069Sbostic 	splx(s);
91553574Sheideman 	(strategy)(&vop_strategy_a);
91651215Sbostic }
91751215Sbostic 
91851342Sbostic /*
91951342Sbostic  * Logical block number match routines used when traversing the dirty block
92051342Sbostic  * chain.
92151342Sbostic  */
92252077Sbostic int
92352077Sbostic lfs_match_data(fs, bp)
92451860Sbostic 	struct lfs *fs;
92552085Sbostic 	struct buf *bp;
92651215Sbostic {
92751342Sbostic 	return (bp->b_lblkno >= 0);
92851215Sbostic }
92951215Sbostic 
93052077Sbostic int
93152077Sbostic lfs_match_indir(fs, bp)
93251860Sbostic 	struct lfs *fs;
93352085Sbostic 	struct buf *bp;
93451215Sbostic {
93551860Sbostic 	int lbn;
93651860Sbostic 
93751860Sbostic 	lbn = bp->b_lblkno;
93851860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
93951215Sbostic }
94051215Sbostic 
94152077Sbostic int
94252077Sbostic lfs_match_dindir(fs, bp)
94351860Sbostic 	struct lfs *fs;
94452085Sbostic 	struct buf *bp;
94551215Sbostic {
94651860Sbostic 	int lbn;
94751860Sbostic 
94851860Sbostic 	lbn = bp->b_lblkno;
94951860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
95051215Sbostic }
95151215Sbostic 
95252077Sbostic int
95352077Sbostic lfs_match_tindir(fs, bp)
95451499Sbostic 	struct lfs *fs;
95552085Sbostic 	struct buf *bp;
95651342Sbostic {
95751860Sbostic 	int lbn;
95851342Sbostic 
95951860Sbostic 	lbn = bp->b_lblkno;
96051860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
96151860Sbostic }
96251342Sbostic 
96351860Sbostic /*
96451860Sbostic  * Allocate a new buffer header.
96551860Sbostic  */
96652085Sbostic struct buf *
96755940Sbostic lfs_newbuf(vp, daddr, size)
96855940Sbostic 	struct vnode *vp;
969*68550Smckusick 	ufs_daddr_t daddr;
97051860Sbostic 	size_t size;
97151860Sbostic {
97252085Sbostic 	struct buf *bp;
97355940Sbostic 	size_t nbytes;
97451342Sbostic 
97555940Sbostic 	nbytes = roundup(size, DEV_BSIZE);
97657072Smargo 	bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
97757072Smargo 	bzero(bp, sizeof(struct buf));
97857072Smargo 	if (nbytes)
97964526Sbostic 		bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
98055940Sbostic 	bgetvp(vp, bp);
98155940Sbostic 	bp->b_bufsize = size;
98255940Sbostic 	bp->b_bcount = size;
98351860Sbostic 	bp->b_lblkno = daddr;
98451860Sbostic 	bp->b_blkno = daddr;
98551860Sbostic 	bp->b_error = 0;
98651860Sbostic 	bp->b_resid = 0;
98755940Sbostic 	bp->b_iodone = lfs_callback;
98856027Sbostic 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
98951860Sbostic 	return (bp);
99051860Sbostic }
99151342Sbostic 
99253347Sbostic void
99351860Sbostic lfs_callback(bp)
99452085Sbostic 	struct buf *bp;
99551860Sbostic {
99651860Sbostic 	struct lfs *fs;
99751342Sbostic 
99856056Sbostic 	fs = (struct lfs *)bp->b_saveaddr;
99951860Sbostic #ifdef DIAGNOSTIC
100051860Sbostic 	if (fs->lfs_iocount == 0)
100151860Sbostic 		panic("lfs_callback: zero iocount\n");
100251860Sbostic #endif
100351860Sbostic 	if (--fs->lfs_iocount == 0)
100452688Sbostic 		wakeup(&fs->lfs_iocount);
100551915Sbostic 
100655940Sbostic 	brelvp(bp);
100764526Sbostic 	free(bp->b_data, M_SEGMENT);
100855940Sbostic 	free(bp, M_SEGMENT);
100951860Sbostic }
101051342Sbostic 
101155940Sbostic void
101255940Sbostic lfs_supercallback(bp)
101355940Sbostic 	struct buf *bp;
101455940Sbostic {
101555940Sbostic 	brelvp(bp);
101664526Sbostic 	free(bp->b_data, M_SEGMENT);
101755940Sbostic 	free(bp, M_SEGMENT);
101855940Sbostic }
101955940Sbostic 
102051215Sbostic /*
102151188Sbostic  * Shellsort (diminishing increment sort) from Data Structures and
102251188Sbostic  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
102351188Sbostic  * see also Knuth Vol. 3, page 84.  The increments are selected from
102451188Sbostic  * formula (8), page 95.  Roughly O(N^3/2).
102551188Sbostic  */
102651188Sbostic /*
102751188Sbostic  * This is our own private copy of shellsort because we want to sort
102851188Sbostic  * two parallel arrays (the array of buffer pointers and the array of
102951188Sbostic  * logical block numbers) simultaneously.  Note that we cast the array
103051188Sbostic  * of logical block numbers to a unsigned in this routine so that the
103151188Sbostic  * negative block numbers (meta data blocks) sort AFTER the data blocks.
103251188Sbostic  */
103352077Sbostic void
103452077Sbostic lfs_shellsort(bp_array, lb_array, nmemb)
103552085Sbostic 	struct buf **bp_array;
1036*68550Smckusick 	ufs_daddr_t *lb_array;
103751188Sbostic 	register int nmemb;
103851188Sbostic {
103951188Sbostic 	static int __rsshell_increments[] = { 4, 1, 0 };
104051188Sbostic 	register int incr, *incrp, t1, t2;
104152085Sbostic 	struct buf *bp_temp;
104251188Sbostic 	u_long lb_temp;
104351188Sbostic 
104451188Sbostic 	for (incrp = __rsshell_increments; incr = *incrp++;)
104551188Sbostic 		for (t1 = incr; t1 < nmemb; ++t1)
104651188Sbostic 			for (t2 = t1 - incr; t2 >= 0;)
104751188Sbostic 				if (lb_array[t2] > lb_array[t2 + incr]) {
104851188Sbostic 					lb_temp = lb_array[t2];
104951188Sbostic 					lb_array[t2] = lb_array[t2 + incr];
105051188Sbostic 					lb_array[t2 + incr] = lb_temp;
105151188Sbostic 					bp_temp = bp_array[t2];
105251188Sbostic 					bp_array[t2] = bp_array[t2 + incr];
105351188Sbostic 					bp_array[t2 + incr] = bp_temp;
105451188Sbostic 					t2 -= incr;
105551188Sbostic 				} else
105651188Sbostic 					break;
105751188Sbostic }
105855940Sbostic 
105957072Smargo /*
106065242Smckusick  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
106157072Smargo  */
106257072Smargo lfs_vref(vp)
106357072Smargo 	register struct vnode *vp;
106457072Smargo {
106557072Smargo 
106657072Smargo 	if (vp->v_flag & VXLOCK)
106757072Smargo 		return(1);
106865242Smckusick 	return (vget(vp, 0));
106957072Smargo }
107057072Smargo 
107157072Smargo void
107257072Smargo lfs_vunref(vp)
107357072Smargo 	register struct vnode *vp;
107457072Smargo {
107565242Smckusick 	extern int lfs_no_inactive;
107657072Smargo 
107757072Smargo 	/*
107865242Smckusick 	 * This is vrele except that we do not want to VOP_INACTIVE
107965242Smckusick 	 * this vnode. Rather than inline vrele here, we use a global
108065242Smckusick 	 * flag to tell lfs_inactive not to run. Yes, its gross.
108157072Smargo 	 */
108265242Smckusick 	lfs_no_inactive = 1;
108365242Smckusick 	vrele(vp);
108465242Smckusick 	lfs_no_inactive = 0;
108557072Smargo }
1086