xref: /csrg-svn/sys/ufs/lfs/lfs_segment.c (revision 63375)
151188Sbostic /*
2*63375Sbostic  * Copyright (c) 1991, 1993
3*63375Sbostic  *	The Regents of the University of California.  All rights reserved.
451188Sbostic  *
551188Sbostic  * %sccs.include.redist.c%
651188Sbostic  *
7*63375Sbostic  *	@(#)lfs_segment.c	8.1 (Berkeley) 06/11/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);
16554264Sbostic 		if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) ||
16656478Smargo 		    vp->v_dirtyblkhd.le_next != NULL) &&
16754264Sbostic 		    ip->i_number != LFS_IFILE_INUM) {
16856478Smargo 			if (vp->v_dirtyblkhd.le_next != NULL)
16954264Sbostic 				lfs_writefile(fs, sp, vp);
17054264Sbostic 			(void) lfs_writeinode(fs, sp, ip);
17154264Sbostic 		}
17254264Sbostic 		vp->v_flag &= ~VDIROP;
17357072Smargo 		lfs_vunref(vp);
17454264Sbostic 	}
17554264Sbostic }
17654264Sbostic 
17752328Sbostic int
17857072Smargo lfs_segwrite(mp, flags)
17952085Sbostic 	struct mount *mp;
18057072Smargo 	int flags;			/* Do a checkpoint. */
18151188Sbostic {
18255592Sbostic 	struct buf *bp;
18352085Sbostic 	struct inode *ip;
18451499Sbostic 	struct lfs *fs;
18552085Sbostic 	struct segment *sp;
18652085Sbostic 	struct vnode *vp;
18755592Sbostic 	SEGUSE *segusep;
18855592Sbostic 	daddr_t ibno;
18955940Sbostic 	CLEANERINFO *cip;
19055940Sbostic 	int clean, error, i, s;
19157072Smargo 	int do_ckp;
19251188Sbostic 
19352328Sbostic 	fs = VFSTOUFS(mp)->um_lfs;
19455940Sbostic 
19555940Sbostic  	/*
19655940Sbostic  	 * If we have fewer than 2 clean segments, wait until cleaner
19755940Sbostic 	 * writes.
19855940Sbostic  	 */
19955940Sbostic 	do {
20055940Sbostic 		LFS_CLEANERINFO(cip, fs, bp);
20155940Sbostic 		clean = cip->clean;
20255940Sbostic 		brelse(bp);
20355940Sbostic 		if (clean <= 2) {
20455940Sbostic 			printf ("segs clean: %d\n", clean);
20555940Sbostic 			wakeup(&lfs_allclean_wakeup);
20655940Sbostic 			if (error = tsleep(&fs->lfs_avail, PRIBIO + 1,
20755940Sbostic 			    "lfs writer", 0))
20855940Sbostic 				return (error);
20955940Sbostic 		}
21055940Sbostic 	} while (clean <= 2 );
21152085Sbostic 
21251860Sbostic 	/*
21352328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
21452328Sbostic 	 * the maximum possible number of buffers which can be described in a
21552328Sbostic 	 * single summary block.
21652328Sbostic 	 */
21757072Smargo 	do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE;
21857072Smargo 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
21957072Smargo 	sp = fs->lfs_sp;
22052328Sbostic 
22157072Smargo 	lfs_writevnodes(fs, mp, sp, VN_REG);
22251342Sbostic 
22357072Smargo 	/* XXX ignore ordering of dirops for now */
22457072Smargo 	/* XXX
22554264Sbostic 	fs->lfs_writer = 1;
22654264Sbostic 	if (fs->lfs_dirops && (error =
22754264Sbostic 	    tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
22854264Sbostic 		free(sp->bpp, M_SEGMENT);
22954264Sbostic 		free(sp, M_SEGMENT);
23054264Sbostic 		fs->lfs_writer = 0;
23155551Sbostic 		return (error);
23254264Sbostic 	}
23351860Sbostic 
23457072Smargo 	lfs_writevnodes(fs, mp, sp, VN_DIROP);
23557072Smargo 	*/
23651860Sbostic 
23754264Sbostic 	/*
23855592Sbostic 	 * If we are doing a checkpoint, mark everything since the
23955592Sbostic 	 * last checkpoint as no longer ACTIVE.
24054264Sbostic 	 */
24155592Sbostic 	if (do_ckp)
24255592Sbostic 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
24355592Sbostic 		     --ibno >= fs->lfs_cleansz; ) {
24455592Sbostic 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
24555592Sbostic 			    NOCRED, &bp))
24655592Sbostic 
24755592Sbostic 				panic("lfs: ifile read");
24855592Sbostic 			segusep = (SEGUSE *)bp->b_un.b_addr;
24955592Sbostic 			for (i = fs->lfs_sepb; i--; segusep++)
25055592Sbostic 				segusep->su_flags &= ~SEGUSE_ACTIVE;
25155592Sbostic 
25255940Sbostic 			error = VOP_BWRITE(bp);
25355592Sbostic 		}
25455592Sbostic 
25554264Sbostic 	if (do_ckp || fs->lfs_doifile) {
25656086Sbostic redo:
25754264Sbostic 		vp = fs->lfs_ivnode;
25854264Sbostic 		while (vget(vp));
25952328Sbostic 		ip = VTOI(vp);
26056478Smargo 		if (vp->v_dirtyblkhd.le_next != NULL)
26155592Sbostic 			lfs_writefile(fs, sp, vp);
26255592Sbostic 		(void)lfs_writeinode(fs, sp, ip);
26352077Sbostic 		vput(vp);
26457072Smargo 		if (lfs_writeseg(fs, sp) && do_ckp)
26556086Sbostic 			goto redo;
26654264Sbostic 	} else
26754264Sbostic 		(void) lfs_writeseg(fs, sp);
26851342Sbostic 
26951215Sbostic 	/*
27051860Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
27151860Sbostic 	 * moment, the user's process hangs around so we can sleep.
27251215Sbostic 	 */
27357072Smargo 	/* XXX ignore dirops for now
27454264Sbostic 	fs->lfs_writer = 0;
27554264Sbostic 	fs->lfs_doifile = 0;
27654264Sbostic 	wakeup(&fs->lfs_dirops);
27757072Smargo 	*/
27854264Sbostic 
27957072Smargo #ifdef DOSTATS
28057072Smargo 	++lfs_stats.nwrites;
28157072Smargo 	if (sp->seg_flags & SEGM_SYNC)
28257072Smargo 		++lfs_stats.nsync_writes;
28357072Smargo 	if (sp->seg_flags & SEGM_CKP)
28457072Smargo 		++lfs_stats.ncheckpoints;
28557072Smargo #endif
28654690Sbostic 	lfs_segunlock(fs);
28751860Sbostic 	return (0);
28851188Sbostic }
28951188Sbostic 
29051860Sbostic /*
29151860Sbostic  * Write the dirty blocks associated with a vnode.
29251860Sbostic  */
29352077Sbostic void
29451860Sbostic lfs_writefile(fs, sp, vp)
29551499Sbostic 	struct lfs *fs;
29652085Sbostic 	struct segment *sp;
29752085Sbostic 	struct vnode *vp;
29851188Sbostic {
29951860Sbostic 	struct buf *bp;
30052085Sbostic 	struct finfo *fip;
30151860Sbostic 	IFILE *ifp;
30251188Sbostic 
30352085Sbostic 	if (sp->seg_bytes_left < fs->lfs_bsize ||
30457072Smargo 	    sp->sum_bytes_left < sizeof(struct finfo))
30554264Sbostic 		(void) lfs_writeseg(fs, sp);
30657072Smargo 
30752085Sbostic 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
30856478Smargo 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
30951215Sbostic 
31052085Sbostic 	fip = sp->fip;
31152085Sbostic 	fip->fi_nblocks = 0;
31252085Sbostic 	fip->fi_ino = VTOI(vp)->i_number;
31352085Sbostic 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
31452085Sbostic 	fip->fi_version = ifp->if_version;
31552085Sbostic 	brelse(bp);
31651188Sbostic 
31752085Sbostic 	/*
31852085Sbostic 	 * It may not be necessary to write the meta-data blocks at this point,
31952085Sbostic 	 * as the roll-forward recovery code should be able to reconstruct the
32052085Sbostic 	 * list.
32152085Sbostic 	 */
32252085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_data);
32352085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_indir);
32452085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_dindir);
32551860Sbostic #ifdef TRIPLE
32652085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_tindir);
32751860Sbostic #endif
32851342Sbostic 
32952085Sbostic 	fip = sp->fip;
33052085Sbostic 	if (fip->fi_nblocks != 0) {
33152085Sbostic 		sp->fip =
33252085Sbostic 		    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
33352085Sbostic 		    sizeof(daddr_t) * (fip->fi_nblocks - 1));
33455940Sbostic 		sp->start_lbp = &sp->fip->fi_blocks[0];
33556478Smargo 	} else {
33652682Sstaelin 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
33756478Smargo 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
33856478Smargo 	}
33951215Sbostic }
34051215Sbostic 
34154264Sbostic int
34251915Sbostic lfs_writeinode(fs, sp, ip)
34351915Sbostic 	struct lfs *fs;
34452085Sbostic 	struct segment *sp;
34552085Sbostic 	struct inode *ip;
34651915Sbostic {
34752085Sbostic 	struct buf *bp, *ibp;
34852077Sbostic 	IFILE *ifp;
34952682Sstaelin 	SEGUSE *sup;
35052682Sstaelin 	daddr_t daddr;
35152077Sbostic 	ino_t ino;
35257072Smargo 	int error, i, ndx;
35354264Sbostic 	int redo_ifile = 0;
35451915Sbostic 
35555940Sbostic 	if (!(ip->i_flag & (IMOD | IACC | IUPD | ICHG)))
35656190Smargo 		return(0);
35755940Sbostic 
35851915Sbostic 	/* Allocate a new inode block if necessary. */
35951915Sbostic 	if (sp->ibp == NULL) {
36051915Sbostic 		/* Allocate a new segment if necessary. */
36151915Sbostic 		if (sp->seg_bytes_left < fs->lfs_bsize ||
36257072Smargo 		    sp->sum_bytes_left < sizeof(daddr_t))
36354264Sbostic 			(void) lfs_writeseg(fs, sp);
36451915Sbostic 
36551915Sbostic 		/* Get next inode block. */
36652682Sstaelin 		daddr = fs->lfs_offset;
36751915Sbostic 		fs->lfs_offset += fsbtodb(fs, 1);
36851915Sbostic 		sp->ibp = *sp->cbpp++ =
36956056Sbostic 		    lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
37056056Sbostic 		    fs->lfs_bsize);
37157072Smargo 		/* Zero out inode numbers */
37257072Smargo 		for (i = 0; i < INOPB(fs); ++i)
37357072Smargo 			sp->ibp->b_un.b_dino[i].di_inumber = 0;
37455940Sbostic 		++sp->start_bpp;
37555940Sbostic 		fs->lfs_avail -= fsbtodb(fs, 1);
37652688Sbostic 		/* Set remaining space counters. */
37751915Sbostic 		sp->seg_bytes_left -= fs->lfs_bsize;
37851915Sbostic 		sp->sum_bytes_left -= sizeof(daddr_t);
37952077Sbostic 		ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
38051915Sbostic 		    sp->ninodes / INOPB(fs) - 1;
38152682Sstaelin 		((daddr_t *)(sp->segsum))[ndx] = daddr;
38251915Sbostic 	}
38351915Sbostic 
38452085Sbostic 	/* Update the inode times and copy the inode onto the inode page. */
38556056Sbostic 	if (ip->i_flag & IMOD)
38656056Sbostic 		--fs->lfs_uinodes;
38752077Sbostic 	ITIMES(ip, &time, &time);
38855940Sbostic 	ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG);
38951915Sbostic 	bp = sp->ibp;
39052085Sbostic 	bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
39151915Sbostic 	/* Increment inode count in segment summary block. */
39251915Sbostic 	++((SEGSUM *)(sp->segsum))->ss_ninos;
39351915Sbostic 
39451915Sbostic 	/* If this page is full, set flag to allocate a new page. */
39551915Sbostic 	if (++sp->ninodes % INOPB(fs) == 0)
39651915Sbostic 		sp->ibp = NULL;
39751915Sbostic 
39851915Sbostic 	/*
39952077Sbostic 	 * If updating the ifile, update the super-block.  Update the disk
40052077Sbostic 	 * address and access times for this inode in the ifile.
40151915Sbostic 	 */
40252077Sbostic 	ino = ip->i_number;
40355696Sbostic 	if (ino == LFS_IFILE_INUM) {
40455696Sbostic 		daddr = fs->lfs_idaddr;
40551915Sbostic 		fs->lfs_idaddr = bp->b_blkno;
40655696Sbostic 	} else {
40755696Sbostic 		LFS_IENTRY(ifp, fs, ino, ibp);
40855696Sbostic 		daddr = ifp->if_daddr;
40955696Sbostic 		ifp->if_daddr = bp->b_blkno;
41055940Sbostic 		error = VOP_BWRITE(ibp);
41155696Sbostic 	}
41252077Sbostic 
41354264Sbostic 	/*
41454264Sbostic 	 * No need to update segment usage if there was no former inode address
41554264Sbostic 	 * or if the last inode address is in the current partial segment.
41654264Sbostic 	 */
41754264Sbostic 	if (daddr != LFS_UNUSED_DADDR &&
41855803Sbostic 	    !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
41952682Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
42052682Sstaelin #ifdef DIAGNOSTIC
42154264Sbostic 		if (sup->su_nbytes < sizeof(struct dinode)) {
42252819Sbostic 			/* XXX -- Change to a panic. */
42352819Sbostic 			printf("lfs: negative bytes (segment %d)\n",
42452682Sstaelin 			    datosn(fs, daddr));
42554264Sbostic 			panic("negative bytes");
42654264Sbostic 		}
42752682Sstaelin #endif
42852682Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
42956069Sbostic 		redo_ifile =
43056069Sbostic 		    (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
43155940Sbostic 		error = VOP_BWRITE(bp);
43252682Sstaelin 	}
43355551Sbostic 	return (redo_ifile);
43451915Sbostic }
43551915Sbostic 
43655940Sbostic int
43755940Sbostic lfs_gatherblock(sp, bp, sptr)
43855940Sbostic 	struct segment *sp;
43955940Sbostic 	struct buf *bp;
44055940Sbostic 	int *sptr;
44155940Sbostic {
44255940Sbostic 	struct lfs *fs;
44355940Sbostic 	int version;
44455940Sbostic 
44555940Sbostic 	/*
44655940Sbostic 	 * If full, finish this segment.  We may be doing I/O, so
44755940Sbostic 	 * release and reacquire the splbio().
44855940Sbostic 	 */
44956027Sbostic #ifdef DIAGNOSTIC
45056027Sbostic 	if (sp->vp == NULL)
45156027Sbostic 		panic ("lfs_gatherblock: Null vp in segment");
45256027Sbostic #endif
45355940Sbostic 	fs = sp->fs;
45455940Sbostic 	if (sp->sum_bytes_left < sizeof(daddr_t) ||
45555940Sbostic 	    sp->seg_bytes_left < fs->lfs_bsize) {
45655940Sbostic 		if (sptr)
45755940Sbostic 			splx(*sptr);
45856027Sbostic 		lfs_updatemeta(sp);
45955940Sbostic 
46055940Sbostic 		version = sp->fip->fi_version;
46155940Sbostic 		(void) lfs_writeseg(fs, sp);
46255940Sbostic 
46355940Sbostic 		sp->fip->fi_version = version;
46456027Sbostic 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
46556478Smargo 		/* Add the current file to the segment summary. */
46656478Smargo 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
46755940Sbostic 		sp->sum_bytes_left -=
46855940Sbostic 		    sizeof(struct finfo) - sizeof(daddr_t);
46955940Sbostic 
47055940Sbostic 		if (sptr)
47155940Sbostic 			*sptr = splbio();
47255940Sbostic 		return(1);
47355940Sbostic 	}
47455940Sbostic 
47555940Sbostic 	/* Insert into the buffer list, update the FINFO block. */
47655940Sbostic 	bp->b_flags |= B_GATHERED;
47755940Sbostic 	*sp->cbpp++ = bp;
47855940Sbostic 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
47955940Sbostic 
48055940Sbostic 	sp->sum_bytes_left -= sizeof(daddr_t);
48157072Smargo 	sp->seg_bytes_left -= fs->lfs_bsize;
48255940Sbostic 	return(0);
48355940Sbostic }
48455940Sbostic 
48552077Sbostic void
48651215Sbostic lfs_gather(fs, sp, vp, match)
48751499Sbostic 	struct lfs *fs;
48852085Sbostic 	struct segment *sp;
48952085Sbostic 	struct vnode *vp;
49052085Sbostic 	int (*match) __P((struct lfs *, struct buf *));
49151215Sbostic {
49255940Sbostic 	struct buf *bp;
49351342Sbostic 	int s;
49451215Sbostic 
49556027Sbostic 	sp->vp = vp;
49655940Sbostic 	s = splbio();
49756478Smargo loop:	for (bp = vp->v_dirtyblkhd.le_next; bp; bp = bp->b_vnbufs.qe_next) {
49854264Sbostic 		if (bp->b_flags & B_BUSY || !match(fs, bp) ||
49954264Sbostic 		    bp->b_flags & B_GATHERED)
50051215Sbostic 			continue;
50151342Sbostic #ifdef DIAGNOSTIC
50251860Sbostic 		if (!(bp->b_flags & B_DELWRI))
50351915Sbostic 			panic("lfs_gather: bp not B_DELWRI");
50451860Sbostic 		if (!(bp->b_flags & B_LOCKED))
50551915Sbostic 			panic("lfs_gather: bp not B_LOCKED");
50651342Sbostic #endif
50755940Sbostic 		if (lfs_gatherblock(sp, bp, &s))
50853145Sstaelin 			goto loop;
50951188Sbostic 	}
51051215Sbostic 	splx(s);
51156027Sbostic 	lfs_updatemeta(sp);
51256027Sbostic 	sp->vp = NULL;
51351188Sbostic }
51451188Sbostic 
51555940Sbostic 
51651342Sbostic /*
51751342Sbostic  * Update the metadata that points to the blocks listed in the FINFO
51851188Sbostic  * array.
51951188Sbostic  */
52052077Sbostic void
52156027Sbostic lfs_updatemeta(sp)
52252085Sbostic 	struct segment *sp;
52351188Sbostic {
52451915Sbostic 	SEGUSE *sup;
52552085Sbostic 	struct buf *bp;
52655940Sbostic 	struct lfs *fs;
52756027Sbostic 	struct vnode *vp;
52856478Smargo 	struct indir a[NIADDR + 2], *ap;
52952085Sbostic 	struct inode *ip;
53051915Sbostic 	daddr_t daddr, lbn, off;
53155940Sbostic 	int db_per_fsb, error, i, nblocks, num;
53251188Sbostic 
53356027Sbostic 	vp = sp->vp;
53455940Sbostic 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
53556027Sbostic 	if (vp == NULL || nblocks == 0)
53651215Sbostic 		return;
53751215Sbostic 
53851915Sbostic 	/* Sort the blocks. */
53955940Sbostic 	if (!(sp->seg_flags & SEGM_CLEAN))
54055940Sbostic 		lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
54151215Sbostic 
54251915Sbostic 	/*
54351915Sbostic 	 * Assign disk addresses, and update references to the logical
54451915Sbostic 	 * block and the segment usage information.
54551915Sbostic 	 */
54655940Sbostic 	fs = sp->fs;
54751860Sbostic 	db_per_fsb = fsbtodb(fs, 1);
54855940Sbostic 	for (i = nblocks; i--; ++sp->start_bpp) {
54955940Sbostic 		lbn = *sp->start_lbp++;
55055940Sbostic 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
55151860Sbostic 		fs->lfs_offset += db_per_fsb;
55251215Sbostic 
55356478Smargo 		if (error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL))
55456478Smargo 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
55551860Sbostic 		ip = VTOI(vp);
55651860Sbostic 		switch (num) {
55751860Sbostic 		case 0:
55851915Sbostic 			ip->i_db[lbn] = off;
55951860Sbostic 			break;
56051860Sbostic 		case 1:
56151915Sbostic 			ip->i_ib[a[0].in_off] = off;
56251860Sbostic 			break;
56351860Sbostic 		default:
56451860Sbostic 			ap = &a[num - 1];
56551860Sbostic 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
56651860Sbostic 				panic("lfs_updatemeta: bread bno %d",
56751860Sbostic 				    ap->in_lbn);
56855458Sbostic 			/*
56955458Sbostic 			 * Bread may create a new indirect block which needs
57055458Sbostic 			 * to get counted for the inode.
57155458Sbostic 			 */
57255592Sbostic 			if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
57355940Sbostic printf ("Updatemeta allocating indirect block: shouldn't happen\n");
57455458Sbostic 				ip->i_blocks += btodb(fs->lfs_bsize);
57555592Sbostic 				fs->lfs_bfree -= btodb(fs->lfs_bsize);
57655592Sbostic 			}
57751915Sbostic 			bp->b_un.b_daddr[ap->in_off] = off;
57853530Sheideman 			VOP_BWRITE(bp);
57951188Sbostic 		}
58051915Sbostic 
58151915Sbostic 		/* Update segment usage information. */
58257072Smargo 		if (daddr != UNASSIGNED &&
58357072Smargo 		    !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
58451915Sbostic 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
58551915Sbostic #ifdef DIAGNOSTIC
58654264Sbostic 			if (sup->su_nbytes < fs->lfs_bsize) {
58752819Sbostic 				/* XXX -- Change to a panic. */
58852819Sbostic 				printf("lfs: negative bytes (segment %d)\n",
58951915Sbostic 				    datosn(fs, daddr));
59054264Sbostic 				panic ("Negative Bytes");
59154264Sbostic 			}
59251915Sbostic #endif
59351915Sbostic 			sup->su_nbytes -= fs->lfs_bsize;
59455940Sbostic 			error = VOP_BWRITE(bp);
59551915Sbostic 		}
59651188Sbostic 	}
59751188Sbostic }
59851188Sbostic 
59951915Sbostic /*
60051915Sbostic  * Start a new segment.
60151915Sbostic  */
60257072Smargo int
60357072Smargo lfs_initseg(fs)
60451499Sbostic 	struct lfs *fs;
60557072Smargo {
60652085Sbostic 	struct segment *sp;
60751915Sbostic 	SEGUSE *sup;
60851915Sbostic 	SEGSUM *ssp;
60951915Sbostic 	struct buf *bp;
61051915Sbostic 	daddr_t lbn, *lbnp;
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);
65251915Sbostic 	sp->segsum = (*sp->cbpp)->b_un.b_addr;
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. */
66352085Sbostic 	sp->fip = (struct finfo *)(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;
68355940Sbostic 	int curseg, error, 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;
72955940Sbostic 	int ch_per_blk, do_again, error, 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
77056159Smargo 			*dp++ = (*bpp)->b_un.b_words[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;
81652688Sbostic 		for (p = cbp->b_un.b_addr; 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
82855940Sbostic 				bcopy(bp->b_un.b_addr, 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))
83857072Smargo 					free(bp->b_un.b_addr, 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);
84952688Sbostic 		cbp->b_bcount = p - cbp->b_un.b_addr;
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);
90351860Sbostic 	*bp->b_un.b_lfs = *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;
96951860Sbostic 	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)
97957072Smargo 		bp->b_un.b_addr =
98057072Smargo 		    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);
100857072Smargo 	free(bp->b_un.b_addr, M_SEGMENT);
100955940Sbostic 	free(bp, M_SEGMENT);
101051860Sbostic }
101151342Sbostic 
101255940Sbostic void
101355940Sbostic lfs_supercallback(bp)
101455940Sbostic 	struct buf *bp;
101555940Sbostic {
101655940Sbostic 	brelvp(bp);
101757072Smargo 	free(bp->b_un.b_addr, 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