xref: /csrg-svn/sys/ufs/lfs/lfs_segment.c (revision 64611)
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*64611Sbostic  *	@(#)lfs_segment.c	8.3 (Berkeley) 09/23/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);
10457072Smargo 	if (vp->v_dirtyblkhd.le_next == NULL)
10557072Smargo 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
10652328Sbostic 
10755551Sbostic 	do {
10855551Sbostic 		do {
10956478Smargo 			if (vp->v_dirtyblkhd.le_next != 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 
13754264Sbostic loop:	for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
13854264Sbostic 		/*
13954264Sbostic 		 * If the vnode that we are about to sync is no longer
14054264Sbostic 		 * associated with this mount point, start over.
14154264Sbostic 		 */
14254264Sbostic 		if (vp->v_mount != mp)
14354264Sbostic 			goto loop;
14454264Sbostic 
14557072Smargo 		/* XXX ignore dirops for now
14657072Smargo 		if (op == VN_DIROP && !(vp->v_flag & VDIROP) ||
14757072Smargo 		    op != VN_DIROP && (vp->v_flag & VDIROP))
14854264Sbostic 			continue;
14957072Smargo 		*/
15054264Sbostic 
15157072Smargo 		if (op == VN_EMPTY && vp->v_dirtyblkhd.le_next)
15257072Smargo 			continue;
15357072Smargo 
15457072Smargo 		if (vp->v_type == VNON)
15557072Smargo 			continue;
15657072Smargo 
15757072Smargo 		if (lfs_vref(vp))
15857072Smargo 			continue;
15957072Smargo 
16054264Sbostic 		/*
16154264Sbostic 		 * Write the inode/file if dirty and it's not the
16254264Sbostic 		 * the IFILE.
16354264Sbostic 		 */
16454264Sbostic 		ip = VTOI(vp);
165*64611Sbostic 		if ((ip->i_flag &
166*64611Sbostic 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) ||
16756478Smargo 		    vp->v_dirtyblkhd.le_next != NULL) &&
16854264Sbostic 		    ip->i_number != LFS_IFILE_INUM) {
16956478Smargo 			if (vp->v_dirtyblkhd.le_next != NULL)
17054264Sbostic 				lfs_writefile(fs, sp, vp);
17154264Sbostic 			(void) lfs_writeinode(fs, sp, ip);
17254264Sbostic 		}
17354264Sbostic 		vp->v_flag &= ~VDIROP;
17457072Smargo 		lfs_vunref(vp);
17554264Sbostic 	}
17654264Sbostic }
17754264Sbostic 
17852328Sbostic int
17957072Smargo lfs_segwrite(mp, flags)
18052085Sbostic 	struct mount *mp;
18157072Smargo 	int flags;			/* Do a checkpoint. */
18251188Sbostic {
18355592Sbostic 	struct buf *bp;
18452085Sbostic 	struct inode *ip;
18551499Sbostic 	struct lfs *fs;
18652085Sbostic 	struct segment *sp;
18752085Sbostic 	struct vnode *vp;
18855592Sbostic 	SEGUSE *segusep;
18955592Sbostic 	daddr_t ibno;
19055940Sbostic 	CLEANERINFO *cip;
19155940Sbostic 	int clean, error, i, s;
19257072Smargo 	int do_ckp;
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;
25954264Sbostic 		while (vget(vp));
26052328Sbostic 		ip = VTOI(vp);
26156478Smargo 		if (vp->v_dirtyblkhd.le_next != 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 
30852085Sbostic 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(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) +
33452085Sbostic 		    sizeof(daddr_t) * (fip->fi_nblocks - 1));
33555940Sbostic 		sp->start_lbp = &sp->fip->fi_blocks[0];
33656478Smargo 	} else {
33752682Sstaelin 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(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;
35152682Sstaelin 	daddr_t daddr;
35252077Sbostic 	ino_t ino;
35357072Smargo 	int error, i, ndx;
35454264Sbostic 	int redo_ifile = 0;
35551915Sbostic 
356*64611Sbostic 	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 ||
36357072Smargo 		    sp->sum_bytes_left < sizeof(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;
37951915Sbostic 		sp->sum_bytes_left -= sizeof(daddr_t);
38052077Sbostic 		ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
38151915Sbostic 		    sp->ninodes / INOPB(fs) - 1;
38252682Sstaelin 		((daddr_t *)(sp->segsum))[ndx] = daddr;
38351915Sbostic 	}
38451915Sbostic 
38552085Sbostic 	/* Update the inode times and copy the inode onto the inode page. */
386*64611Sbostic 	if (ip->i_flag & IN_MODIFIED)
38756056Sbostic 		--fs->lfs_uinodes;
38852077Sbostic 	ITIMES(ip, &time, &time);
389*64611Sbostic 	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;
45555940Sbostic 	if (sp->sum_bytes_left < sizeof(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 -=
46955940Sbostic 		    sizeof(struct finfo) - sizeof(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 
48155940Sbostic 	sp->sum_bytes_left -= sizeof(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();
49856478Smargo loop:	for (bp = vp->v_dirtyblkhd.le_next; bp; bp = bp->b_vnbufs.qe_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;
53151915Sbostic 	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 			}
57864526Sbostic 			((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;
61151915Sbostic 	daddr_t lbn, *lbnp;
61257072Smargo 	int repeat;
61351215Sbostic 
61457072Smargo 	sp = fs->lfs_sp;
61557072Smargo 
61657072Smargo 	repeat = 0;
61751915Sbostic 	/* Advance to the next segment. */
61851927Sbostic 	if (!LFS_PARTIAL_FITS(fs)) {
61952682Sstaelin 		/* Wake up any cleaning procs waiting on this file system. */
62052688Sbostic 		wakeup(&lfs_allclean_wakeup);
62152682Sstaelin 
62251927Sbostic 		lfs_newseg(fs);
62357072Smargo 		repeat = 1;
62451927Sbostic 		fs->lfs_offset = fs->lfs_curseg;
62551915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
62651915Sbostic 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
62751915Sbostic 
62851915Sbostic 		/*
62951927Sbostic 		 * If the segment contains a superblock, update the offset
63051927Sbostic 		 * and summary address to skip over it.
63151915Sbostic 		 */
63252077Sbostic 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
63351927Sbostic 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
63451915Sbostic 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
63551915Sbostic 			sp->seg_bytes_left -= LFS_SBPAD;
63651215Sbostic 		}
63752085Sbostic 		brelse(bp);
63851915Sbostic 	} else {
63951915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
64051915Sbostic 		sp->seg_bytes_left = (fs->lfs_dbpseg -
64151915Sbostic 		    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
64251915Sbostic 	}
64354264Sbostic 	fs->lfs_lastpseg = fs->lfs_offset;
64451342Sbostic 
64555940Sbostic 	sp->fs = fs;
64651915Sbostic 	sp->ibp = NULL;
64751915Sbostic 	sp->ninodes = 0;
64851342Sbostic 
64951915Sbostic 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
65051915Sbostic 	sp->cbpp = sp->bpp;
65156056Sbostic 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
65256056Sbostic 	     LFS_SUMMARY_SIZE);
65364526Sbostic 	sp->segsum = (*sp->cbpp)->b_data;
65457072Smargo 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
65555940Sbostic 	sp->start_bpp = ++sp->cbpp;
65651915Sbostic 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
65751342Sbostic 
65851915Sbostic 	/* Set point to SEGSUM, initialize it. */
65951915Sbostic 	ssp = sp->segsum;
66051915Sbostic 	ssp->ss_next = fs->lfs_nextseg;
66151915Sbostic 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
66251342Sbostic 
66351915Sbostic 	/* Set pointer to first FINFO, initialize it. */
66452085Sbostic 	sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
66551915Sbostic 	sp->fip->fi_nblocks = 0;
66655940Sbostic 	sp->start_lbp = &sp->fip->fi_blocks[0];
66751342Sbostic 
66851915Sbostic 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
66951915Sbostic 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
67057072Smargo 
67157072Smargo 	return(repeat);
67251915Sbostic }
67351342Sbostic 
67451915Sbostic /*
67551915Sbostic  * Return the next segment to write.
67651915Sbostic  */
67752077Sbostic void
67851915Sbostic lfs_newseg(fs)
67951915Sbostic 	struct lfs *fs;
68051915Sbostic {
68151927Sbostic 	CLEANERINFO *cip;
68251915Sbostic 	SEGUSE *sup;
68351915Sbostic 	struct buf *bp;
68455940Sbostic 	int curseg, error, isdirty, sn;
68551915Sbostic 
68655592Sbostic         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
68756159Smargo         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
68856056Sbostic 	sup->su_nbytes = 0;
68956056Sbostic 	sup->su_nsums = 0;
69056056Sbostic 	sup->su_ninos = 0;
69155940Sbostic         (void) VOP_BWRITE(bp);
69251927Sbostic 
69351927Sbostic 	LFS_CLEANERINFO(cip, fs, bp);
69451927Sbostic 	--cip->clean;
69551927Sbostic 	++cip->dirty;
69655940Sbostic 	(void) VOP_BWRITE(bp);
69751927Sbostic 
69851927Sbostic 	fs->lfs_lastseg = fs->lfs_curseg;
69951927Sbostic 	fs->lfs_curseg = fs->lfs_nextseg;
70051927Sbostic 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
70151915Sbostic 		sn = (sn + 1) % fs->lfs_nseg;
70251927Sbostic 		if (sn == curseg)
70351915Sbostic 			panic("lfs_nextseg: no clean segments");
70451915Sbostic 		LFS_SEGENTRY(sup, fs, sn, bp);
70551915Sbostic 		isdirty = sup->su_flags & SEGUSE_DIRTY;
70652085Sbostic 		brelse(bp);
70751915Sbostic 		if (!isdirty)
70851915Sbostic 			break;
70951915Sbostic 	}
71055592Sbostic 
71155940Sbostic 	++fs->lfs_nactive;
71251927Sbostic 	fs->lfs_nextseg = sntoda(fs, sn);
71357072Smargo #ifdef DOSTATS
71457072Smargo 	++lfs_stats.segsused;
71557072Smargo #endif
71651188Sbostic }
71751188Sbostic 
71854264Sbostic int
71951188Sbostic lfs_writeseg(fs, sp)
72051499Sbostic 	struct lfs *fs;
72152085Sbostic 	struct segment *sp;
72251188Sbostic {
72355940Sbostic 	extern int locked_queue_count;
72452688Sbostic 	struct buf **bpp, *bp, *cbp;
72551188Sbostic 	SEGUSE *sup;
72652085Sbostic 	SEGSUM *ssp;
72751860Sbostic 	dev_t i_dev;
72854264Sbostic 	size_t size;
72951860Sbostic 	u_long *datap, *dp;
73055940Sbostic 	int ch_per_blk, do_again, error, i, nblocks, num, s;
73154264Sbostic 	int (*strategy)__P((struct vop_strategy_args *));
73254690Sbostic 	struct vop_strategy_args vop_strategy_a;
73355592Sbostic 	u_short ninos;
73452688Sbostic 	char *p;
73551188Sbostic 
73655940Sbostic 	/*
73755940Sbostic 	 * If there are no buffers other than the segment summary to write
73855940Sbostic 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
73955940Sbostic 	 * even if there aren't any buffers, you need to write the superblock.
74055940Sbostic 	 */
74157072Smargo 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
74255551Sbostic 		return (0);
74352085Sbostic 
74456159Smargo 	ssp = (SEGSUM *)sp->segsum;
74556159Smargo 
74656159Smargo 	/* Update the segment usage information. */
74756159Smargo 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
74856159Smargo 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
74956159Smargo 	sup->su_nbytes += nblocks - 1 - ninos << fs->lfs_bshift;
75056159Smargo 	sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
75156159Smargo 	sup->su_nbytes += LFS_SUMMARY_SIZE;
75256159Smargo 	sup->su_lastmod = time.tv_sec;
75356159Smargo 	sup->su_ninos += ninos;
75456159Smargo 	++sup->su_nsums;
75556159Smargo 	do_again = !(bp->b_flags & B_GATHERED);
75656159Smargo 	(void)VOP_BWRITE(bp);
75751188Sbostic 	/*
75852085Sbostic 	 * Compute checksum across data and then across summary; the first
75952085Sbostic 	 * block (the summary block) is skipped.  Set the create time here
76052085Sbostic 	 * so that it's guaranteed to be later than the inode mod times.
76151860Sbostic 	 *
76251860Sbostic 	 * XXX
76351860Sbostic 	 * Fix this to do it inline, instead of malloc/copy.
76451188Sbostic 	 */
76551860Sbostic 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
76656159Smargo 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
76756159Smargo 		if ((*++bpp)->b_flags & B_INVAL) {
76856159Smargo 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
76956159Smargo 				panic("lfs_writeseg: copyin failed");
77056159Smargo 		} else
77164526Sbostic 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
77256159Smargo 	}
77352103Sbostic 	ssp->ss_create = time.tv_sec;
77455803Sbostic 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
77552085Sbostic 	ssp->ss_sumsum =
77652085Sbostic 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
77751927Sbostic 	free(datap, M_SEGMENT);
77856159Smargo #ifdef DIAGNOSTIC
77956159Smargo 	if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
78056159Smargo 		panic("lfs_writeseg: No diskspace for summary");
78156159Smargo #endif
78255592Sbostic 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
78354264Sbostic 
78451860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
78553574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
78651301Sbostic 
78752688Sbostic 	/*
78852688Sbostic 	 * When we simply write the blocks we lose a rotation for every block
78952688Sbostic 	 * written.  To avoid this problem, we allocate memory in chunks, copy
79057072Smargo 	 * the buffers into the chunk and write the chunk.  MAXPHYS is the
79157072Smargo 	 * largest size I/O devices can handle.
79252688Sbostic 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
79352688Sbostic 	 * and brelse the buffer (which will move them to the LRU list).  Add
79452688Sbostic 	 * the B_CALL flag to the buffer header so we can count I/O's for the
79552688Sbostic 	 * checkpoints and so we can release the allocated memory.
79652688Sbostic 	 *
79752688Sbostic 	 * XXX
79852688Sbostic 	 * This should be removed if the new virtual memory system allows us to
79952688Sbostic 	 * easily make the buffers contiguous in kernel memory and if that's
80052688Sbostic 	 * fast enough.
80152688Sbostic 	 */
80257072Smargo 	ch_per_blk = MAXPHYS / fs->lfs_bsize;
80352688Sbostic 	for (bpp = sp->bpp, i = nblocks; i;) {
80452688Sbostic 		num = ch_per_blk;
80552688Sbostic 		if (num > i)
80652688Sbostic 			num = i;
80752688Sbostic 		i -= num;
80852688Sbostic 		size = num * fs->lfs_bsize;
80952688Sbostic 
81056056Sbostic 		cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
81156056Sbostic 		    (*bpp)->b_blkno, size);
81252688Sbostic 		cbp->b_dev = i_dev;
81355940Sbostic 		cbp->b_flags |= B_ASYNC | B_BUSY;
81452688Sbostic 
81552688Sbostic 		s = splbio();
81652688Sbostic 		++fs->lfs_iocount;
81764526Sbostic 		for (p = cbp->b_data; num--;) {
81852688Sbostic 			bp = *bpp++;
81955940Sbostic 			/*
82055940Sbostic 			 * Fake buffers from the cleaner are marked as B_INVAL.
82155940Sbostic 			 * We need to copy the data from user space rather than
82255940Sbostic 			 * from the buffer indicated.
82355940Sbostic 			 * XXX == what do I do on an error?
82455940Sbostic 			 */
82555940Sbostic 			if (bp->b_flags & B_INVAL) {
82655940Sbostic 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
82755940Sbostic 					panic("lfs_writeseg: copyin failed");
82855940Sbostic 			} else
82964526Sbostic 				bcopy(bp->b_data, p, bp->b_bcount);
83052688Sbostic 			p += bp->b_bcount;
83155940Sbostic 			if (bp->b_flags & B_LOCKED)
83255940Sbostic 				--locked_queue_count;
83355940Sbostic 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
83454264Sbostic 			     B_LOCKED | B_GATHERED);
83555940Sbostic 			if (bp->b_flags & B_CALL) {
83655940Sbostic 				/* if B_CALL, it was created with newbuf */
83755940Sbostic 				brelvp(bp);
83857072Smargo 				if (!(bp->b_flags & B_INVAL))
83964526Sbostic 					free(bp->b_data, M_SEGMENT);
84055940Sbostic 				free(bp, M_SEGMENT);
84155940Sbostic 			} else {
84252688Sbostic 				bremfree(bp);
84356478Smargo 				bp->b_flags |= B_DONE;
84452688Sbostic 				reassignbuf(bp, bp->b_vp);
84555940Sbostic 				brelse(bp);
84652688Sbostic 			}
84751860Sbostic 		}
84856069Sbostic 		++cbp->b_vp->v_numoutput;
84952688Sbostic 		splx(s);
85064526Sbostic 		cbp->b_bcount = p - (char *)cbp->b_data;
85156056Sbostic 		/*
85256056Sbostic 		 * XXXX This is a gross and disgusting hack.  Since these
85356056Sbostic 		 * buffers are physically addressed, they hang off the
85456056Sbostic 		 * device vnode (devvp).  As a result, they have no way
85556056Sbostic 		 * of getting to the LFS superblock or lfs structure to
85656056Sbostic 		 * keep track of the number of I/O's pending.  So, I am
85756056Sbostic 		 * going to stuff the fs into the saveaddr field of
85856056Sbostic 		 * the buffer (yuk).
85956056Sbostic 		 */
86056056Sbostic 		cbp->b_saveaddr = (caddr_t)fs;
86153574Sheideman 		vop_strategy_a.a_desc = VDESC(vop_strategy);
86253574Sheideman 		vop_strategy_a.a_bp = cbp;
86353574Sheideman 		(strategy)(&vop_strategy_a);
86451860Sbostic 	}
86557072Smargo 	/*
86657072Smargo 	 * XXX
86757072Smargo 	 * Vinvalbuf can move locked buffers off the locked queue
86857072Smargo 	 * and we have no way of knowing about this.  So, after
86957072Smargo 	 * doing a big write, we recalculate how many bufers are
87057072Smargo 	 * really still left on the locked queue.
87157072Smargo 	 */
87257072Smargo 	locked_queue_count = count_lock_queue();
87357072Smargo 	wakeup(&locked_queue_count);
87457072Smargo #ifdef DOSTATS
87557072Smargo 	++lfs_stats.psegwrites;
87657072Smargo 	lfs_stats.blocktot += nblocks - 1;
87757072Smargo 	if (fs->lfs_sp->seg_flags & SEGM_SYNC)
87857072Smargo 		++lfs_stats.psyncwrites;
87957072Smargo 	if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
88057072Smargo 		++lfs_stats.pcleanwrites;
88157072Smargo 		lfs_stats.cleanblocks += nblocks - 1;
88257072Smargo 	}
88357072Smargo #endif
88457072Smargo 	return (lfs_initseg(fs) || do_again);
88551188Sbostic }
88651188Sbostic 
88752077Sbostic void
88857072Smargo lfs_writesuper(fs)
88951499Sbostic 	struct lfs *fs;
89051301Sbostic {
89152085Sbostic 	struct buf *bp;
89251860Sbostic 	dev_t i_dev;
89353574Sheideman 	int (*strategy) __P((struct vop_strategy_args *));
89456069Sbostic 	int s;
89554690Sbostic 	struct vop_strategy_args vop_strategy_a;
89651301Sbostic 
89751860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
89853574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
89951356Sbostic 
90051342Sbostic 	/* Checksum the superblock and copy it into a buffer. */
90151499Sbostic 	fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
90256056Sbostic 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
90356056Sbostic 	    LFS_SBPAD);
90464526Sbostic 	*(struct lfs *)bp->b_data = *fs;
90551215Sbostic 
90657072Smargo 	/* XXX Toggle between first two superblocks; for now just write first */
90751860Sbostic 	bp->b_dev = i_dev;
90857072Smargo 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
90957072Smargo 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
91057072Smargo 	bp->b_iodone = lfs_supercallback;
91153574Sheideman 	vop_strategy_a.a_desc = VDESC(vop_strategy);
91253574Sheideman 	vop_strategy_a.a_bp = bp;
91356069Sbostic 	s = splbio();
91457072Smargo 	++bp->b_vp->v_numoutput;
91556069Sbostic 	splx(s);
91653574Sheideman 	(strategy)(&vop_strategy_a);
91751215Sbostic }
91851215Sbostic 
91951342Sbostic /*
92051342Sbostic  * Logical block number match routines used when traversing the dirty block
92151342Sbostic  * chain.
92251342Sbostic  */
92352077Sbostic int
92452077Sbostic lfs_match_data(fs, bp)
92551860Sbostic 	struct lfs *fs;
92652085Sbostic 	struct buf *bp;
92751215Sbostic {
92851342Sbostic 	return (bp->b_lblkno >= 0);
92951215Sbostic }
93051215Sbostic 
93152077Sbostic int
93252077Sbostic lfs_match_indir(fs, bp)
93351860Sbostic 	struct lfs *fs;
93452085Sbostic 	struct buf *bp;
93551215Sbostic {
93651860Sbostic 	int lbn;
93751860Sbostic 
93851860Sbostic 	lbn = bp->b_lblkno;
93951860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
94051215Sbostic }
94151215Sbostic 
94252077Sbostic int
94352077Sbostic lfs_match_dindir(fs, bp)
94451860Sbostic 	struct lfs *fs;
94552085Sbostic 	struct buf *bp;
94651215Sbostic {
94751860Sbostic 	int lbn;
94851860Sbostic 
94951860Sbostic 	lbn = bp->b_lblkno;
95051860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
95151215Sbostic }
95251215Sbostic 
95352077Sbostic int
95452077Sbostic lfs_match_tindir(fs, bp)
95551499Sbostic 	struct lfs *fs;
95652085Sbostic 	struct buf *bp;
95751342Sbostic {
95851860Sbostic 	int lbn;
95951342Sbostic 
96051860Sbostic 	lbn = bp->b_lblkno;
96151860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
96251860Sbostic }
96351342Sbostic 
96451860Sbostic /*
96551860Sbostic  * Allocate a new buffer header.
96651860Sbostic  */
96752085Sbostic struct buf *
96855940Sbostic lfs_newbuf(vp, daddr, size)
96955940Sbostic 	struct vnode *vp;
97051860Sbostic 	daddr_t daddr;
97151860Sbostic 	size_t size;
97251860Sbostic {
97352085Sbostic 	struct buf *bp;
97455940Sbostic 	size_t nbytes;
97551342Sbostic 
97655940Sbostic 	nbytes = roundup(size, DEV_BSIZE);
97757072Smargo 	bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
97857072Smargo 	bzero(bp, sizeof(struct buf));
97957072Smargo 	if (nbytes)
98064526Sbostic 		bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
98155940Sbostic 	bgetvp(vp, bp);
98255940Sbostic 	bp->b_bufsize = size;
98355940Sbostic 	bp->b_bcount = size;
98451860Sbostic 	bp->b_lblkno = daddr;
98551860Sbostic 	bp->b_blkno = daddr;
98651860Sbostic 	bp->b_error = 0;
98751860Sbostic 	bp->b_resid = 0;
98855940Sbostic 	bp->b_iodone = lfs_callback;
98956027Sbostic 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
99051860Sbostic 	return (bp);
99151860Sbostic }
99251342Sbostic 
99353347Sbostic void
99451860Sbostic lfs_callback(bp)
99552085Sbostic 	struct buf *bp;
99651860Sbostic {
99751860Sbostic 	struct lfs *fs;
99851342Sbostic 
99956056Sbostic 	fs = (struct lfs *)bp->b_saveaddr;
100051860Sbostic #ifdef DIAGNOSTIC
100151860Sbostic 	if (fs->lfs_iocount == 0)
100251860Sbostic 		panic("lfs_callback: zero iocount\n");
100351860Sbostic #endif
100451860Sbostic 	if (--fs->lfs_iocount == 0)
100552688Sbostic 		wakeup(&fs->lfs_iocount);
100651915Sbostic 
100755940Sbostic 	brelvp(bp);
100864526Sbostic 	free(bp->b_data, M_SEGMENT);
100955940Sbostic 	free(bp, M_SEGMENT);
101051860Sbostic }
101151342Sbostic 
101255940Sbostic void
101355940Sbostic lfs_supercallback(bp)
101455940Sbostic 	struct buf *bp;
101555940Sbostic {
101655940Sbostic 	brelvp(bp);
101764526Sbostic 	free(bp->b_data, M_SEGMENT);
101855940Sbostic 	free(bp, M_SEGMENT);
101955940Sbostic }
102055940Sbostic 
102151215Sbostic /*
102251188Sbostic  * Shellsort (diminishing increment sort) from Data Structures and
102351188Sbostic  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
102451188Sbostic  * see also Knuth Vol. 3, page 84.  The increments are selected from
102551188Sbostic  * formula (8), page 95.  Roughly O(N^3/2).
102651188Sbostic  */
102751188Sbostic /*
102851188Sbostic  * This is our own private copy of shellsort because we want to sort
102951188Sbostic  * two parallel arrays (the array of buffer pointers and the array of
103051188Sbostic  * logical block numbers) simultaneously.  Note that we cast the array
103151188Sbostic  * of logical block numbers to a unsigned in this routine so that the
103251188Sbostic  * negative block numbers (meta data blocks) sort AFTER the data blocks.
103351188Sbostic  */
103452077Sbostic void
103552077Sbostic lfs_shellsort(bp_array, lb_array, nmemb)
103652085Sbostic 	struct buf **bp_array;
103751215Sbostic 	daddr_t *lb_array;
103851188Sbostic 	register int nmemb;
103951188Sbostic {
104051188Sbostic 	static int __rsshell_increments[] = { 4, 1, 0 };
104151188Sbostic 	register int incr, *incrp, t1, t2;
104252085Sbostic 	struct buf *bp_temp;
104351188Sbostic 	u_long lb_temp;
104451188Sbostic 
104551188Sbostic 	for (incrp = __rsshell_increments; incr = *incrp++;)
104651188Sbostic 		for (t1 = incr; t1 < nmemb; ++t1)
104751188Sbostic 			for (t2 = t1 - incr; t2 >= 0;)
104851188Sbostic 				if (lb_array[t2] > lb_array[t2 + incr]) {
104951188Sbostic 					lb_temp = lb_array[t2];
105051188Sbostic 					lb_array[t2] = lb_array[t2 + incr];
105151188Sbostic 					lb_array[t2 + incr] = lb_temp;
105251188Sbostic 					bp_temp = bp_array[t2];
105351188Sbostic 					bp_array[t2] = bp_array[t2 + incr];
105451188Sbostic 					bp_array[t2 + incr] = bp_temp;
105551188Sbostic 					t2 -= incr;
105651188Sbostic 				} else
105751188Sbostic 					break;
105851188Sbostic }
105955940Sbostic 
106057072Smargo /*
106157072Smargo  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, bump the
106257072Smargo  * ref count, removing the vnode from the free list if it is on it.
106357072Smargo  */
106457072Smargo lfs_vref(vp)
106557072Smargo 	register struct vnode *vp;
106657072Smargo {
106757072Smargo 	register struct vnode *vq;
106857072Smargo 	extern struct vnode *vfreeh;
106957072Smargo 	extern struct vnode **vfreet;
107057072Smargo 
107157072Smargo 	if (vp->v_flag & VXLOCK)
107257072Smargo 		return(1);
107357072Smargo 
107457072Smargo 	if (vp->v_usecount == 0) {
107557072Smargo 		if (vq = vp->v_freef)
107657072Smargo 			vq->v_freeb = vp->v_freeb;
107757072Smargo 		else
107857072Smargo 			vfreet = vp->v_freeb;
107957072Smargo 		*vp->v_freeb = vq;
108057072Smargo 		vp->v_freef = NULL;
108157072Smargo 		vp->v_freeb = NULL;
108257072Smargo 	}
108357072Smargo 	VREF(vp);
108457072Smargo 	return (0);
108557072Smargo }
108657072Smargo 
108757072Smargo void
108857072Smargo lfs_vunref(vp)
108957072Smargo 	register struct vnode *vp;
109057072Smargo {
109157072Smargo 	extern struct vnode *vfreeh;
109257072Smargo 	extern struct vnode **vfreet;
109357072Smargo 
109457072Smargo 	--vp->v_usecount;
109557072Smargo 
109657072Smargo 	/*
109757072Smargo 	 * return to free list
109857072Smargo 	 */
109957072Smargo 	if (vp->v_usecount == 0) {
110057072Smargo 		*vfreet = vp;
110157072Smargo 		vp->v_freeb = vfreet;
110257072Smargo 		vp->v_freef = NULL;
110357072Smargo 		vfreet = &vp->v_freef;
110457072Smargo 	}
110557072Smargo 	return;
110657072Smargo }
1107