xref: /csrg-svn/sys/ufs/lfs/lfs_segment.c (revision 55458)
151188Sbostic /*
251188Sbostic  * Copyright (c) 1991 Regents of the University of California.
351188Sbostic  * All rights reserved.
451188Sbostic  *
551188Sbostic  * %sccs.include.redist.c%
651188Sbostic  *
7*55458Sbostic  *	@(#)lfs_segment.c	7.23 (Berkeley) 07/20/92
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>
3151490Sbostic 
3251499Sbostic #include <ufs/lfs/lfs.h>
3351499Sbostic #include <ufs/lfs/lfs_extern.h>
3451490Sbostic 
3551860Sbostic /* In-memory description of a segment about to be written. */
3651860Sbostic struct segment {
3752085Sbostic 	struct buf	**bpp;		/* pointer to buffer array */
3852085Sbostic 	struct buf	**cbpp;		/* pointer to next available bp */
3952085Sbostic 	struct buf	*ibp;		/* buffer pointer to inode page */
4052085Sbostic 	struct finfo	*fip;		/* current fileinfo pointer */
4151860Sbostic 	void	*segsum;		/* segment summary info */
4251860Sbostic 	u_long	ninodes;		/* number of inodes in this segment */
4351860Sbostic 	u_long	seg_bytes_left;		/* bytes left in segment */
4451860Sbostic 	u_long	sum_bytes_left;		/* bytes left in summary block */
4551860Sbostic 	u_long	seg_number;		/* number of this segment */
4651860Sbostic #define	SEGM_CKP	0x01		/* doing a checkpoint */
4751860Sbostic 	u_long	seg_flags;		/* run-time flags for this segment */
4851860Sbostic };
4951860Sbostic 
5051188Sbostic /*
5151860Sbostic  * Determine if it's OK to start a partial in this segment, or if we need
5251860Sbostic  * to go on to a new segment.
5351301Sbostic  */
5451860Sbostic #define	LFS_PARTIAL_FITS(fs) \
5551860Sbostic 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
5651860Sbostic 	1 << (fs)->lfs_fsbtodb)
5751188Sbostic 
5853347Sbostic void	 lfs_callback __P((struct buf *));
5952085Sbostic void	 lfs_gather __P((struct lfs *, struct segment *,
6052085Sbostic 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
6152085Sbostic void	 lfs_initseg __P((struct lfs *, struct segment *));
6252085Sbostic void	 lfs_iset __P((struct inode *, daddr_t, time_t));
6352085Sbostic int	 lfs_match_data __P((struct lfs *, struct buf *));
6452085Sbostic int	 lfs_match_dindir __P((struct lfs *, struct buf *));
6552085Sbostic int	 lfs_match_indir __P((struct lfs *, struct buf *));
6652085Sbostic int	 lfs_match_tindir __P((struct lfs *, struct buf *));
6752085Sbostic struct buf *
6852688Sbostic 	 lfs_newbuf __P((struct lfs *, daddr_t, size_t));
6952077Sbostic void	 lfs_newseg __P((struct lfs *));
7052085Sbostic void	 lfs_shellsort __P((struct buf **, daddr_t *, register int));
7152077Sbostic void	 lfs_updatemeta __P((struct lfs *,
7252085Sbostic 	    struct segment *, struct vnode *, daddr_t *, struct buf **, int));
7352085Sbostic void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
7454264Sbostic int	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
7554264Sbostic int	 lfs_writeseg __P((struct lfs *, struct segment *));
7652085Sbostic void	 lfs_writesuper __P((struct lfs *, struct segment *));
7754264Sbostic void	 lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
7854264Sbostic 	    struct segment *sp, int dirops));
7951188Sbostic 
8051860Sbostic int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
8151860Sbostic 
8252328Sbostic /*
8352328Sbostic  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
8452328Sbostic  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
8552328Sbostic  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
8652328Sbostic  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
8752328Sbostic  */
8852328Sbostic 
8951188Sbostic int
9052328Sbostic lfs_vflush(vp)
9152328Sbostic 	struct vnode *vp;
9252328Sbostic {
9352328Sbostic 	struct inode *ip;
9452328Sbostic 	struct lfs *fs;
9552328Sbostic 	struct segment *sp;
9652328Sbostic 	int error, s;
9752328Sbostic 
9852328Sbostic #ifdef VERBOSE
9952328Sbostic 	printf("lfs_vflush\n");
10052328Sbostic #endif
10154690Sbostic 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
10254690Sbostic 	lfs_seglock(fs);
10352328Sbostic 
10452328Sbostic 	/*
10552328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
10652328Sbostic 	 * the maximum possible number of buffers which can be described in a
10752328Sbostic 	 * single summary block.
10852328Sbostic 	 */
10952328Sbostic 	sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
11052328Sbostic 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
11152328Sbostic 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
11252328Sbostic 	sp->seg_flags = SEGM_CKP;
11352328Sbostic 	lfs_initseg(fs, sp);
11452328Sbostic 
11552328Sbostic 	/*
11652328Sbostic 	 * Keep a cumulative count of the outstanding I/O operations.  If the
11752328Sbostic 	 * disk drive catches up with us it could go to zero before we finish,
11852328Sbostic 	 * so we artificially increment it by one until we've scheduled all of
11952328Sbostic 	 * the writes we intend to do.
12052328Sbostic 	 */
12152328Sbostic 	s = splbio();
12252688Sbostic 	++fs->lfs_iocount;
12352328Sbostic 	splx(s);
12452328Sbostic 
12552328Sbostic 	if (vp->v_dirtyblkhd != NULL)
12652328Sbostic 		lfs_writefile(fs, sp, vp);
12752328Sbostic 	ip = VTOI(vp);
12854264Sbostic 	(void) lfs_writeinode(fs, sp, ip);
12952328Sbostic 	ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
13052328Sbostic 
13154690Sbostic 	(void)lfs_writeseg(fs, sp);
13252328Sbostic 
13352328Sbostic 	/*
13452328Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
13552328Sbostic 	 * moment, the user's process hangs around so we can sleep.
13652328Sbostic 	 */
13752328Sbostic 	s = splbio();
13852328Sbostic 	if (--fs->lfs_iocount && (error =
13952995Sbostic 	    tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0))) {
14052995Sbostic 		free(sp->bpp, M_SEGMENT);
14152995Sbostic 		free(sp, M_SEGMENT);
14252328Sbostic 		return (error);
14352995Sbostic 	}
14452328Sbostic 	splx(s);
14554690Sbostic 	lfs_segunlock(fs);
14652328Sbostic 
14752995Sbostic 	/*
14852995Sbostic 	 * XXX
14952995Sbostic 	 * Should be writing a checkpoint?
15052995Sbostic 	 */
15152328Sbostic 	free(sp->bpp, M_SEGMENT);
15252328Sbostic 	free(sp, M_SEGMENT);
15352328Sbostic 
15452328Sbostic 	return (0);
15552328Sbostic }
15652328Sbostic 
15754264Sbostic void
15854264Sbostic lfs_writevnodes(fs, mp, sp, dirops)
15954264Sbostic 	struct lfs *fs;
16054264Sbostic 	struct mount *mp;
16154264Sbostic 	struct segment *sp;
16254264Sbostic 	int dirops;
16354264Sbostic {
16454264Sbostic 	struct inode *ip;
16554264Sbostic 	struct vnode *vp;
16654264Sbostic 	int error, s;
16754264Sbostic 
16854264Sbostic loop:	for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
16954264Sbostic 		/*
17054264Sbostic 		 * If the vnode that we are about to sync is no longer
17154264Sbostic 		 * associated with this mount point, start over.
17254264Sbostic 		 */
17354264Sbostic 		if (vp->v_mount != mp)
17454264Sbostic 			goto loop;
17554264Sbostic 
17654264Sbostic 		if (dirops && !(vp->v_flag & VDIROP) ||
17754264Sbostic 		    !dirops && (vp->v_flag & VDIROP))
17854264Sbostic 			continue;
17954264Sbostic 		/*
18054264Sbostic 		 * XXX
18154264Sbostic 		 * Up the ref count so we don't get tossed out of
18254264Sbostic 		 * memory.
18354264Sbostic 		 */
18454264Sbostic 		VREF(vp);
18554264Sbostic 
18654264Sbostic 		/*
18754264Sbostic 		 * Write the inode/file if dirty and it's not the
18854264Sbostic 		 * the IFILE.
18954264Sbostic 		 */
19054264Sbostic 		ip = VTOI(vp);
19154264Sbostic 		if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) ||
19254264Sbostic 		    vp->v_dirtyblkhd != NULL) &&
19354264Sbostic 		    ip->i_number != LFS_IFILE_INUM) {
19454264Sbostic 			if (vp->v_dirtyblkhd != NULL)
19554264Sbostic 				lfs_writefile(fs, sp, vp);
19654264Sbostic 			(void) lfs_writeinode(fs, sp, ip);
19754264Sbostic 			ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
19854264Sbostic 		}
19954264Sbostic 		vp->v_flag &= ~VDIROP;
20054264Sbostic 		vrele(vp);
20154264Sbostic 	}
20254264Sbostic }
20354264Sbostic 
20452328Sbostic int
20551215Sbostic lfs_segwrite(mp, do_ckp)
20652085Sbostic 	struct mount *mp;
20751860Sbostic 	int do_ckp;			/* Do a checkpoint. */
20851188Sbostic {
20952085Sbostic 	struct inode *ip;
21051499Sbostic 	struct lfs *fs;
21152085Sbostic 	struct segment *sp;
21252085Sbostic 	struct vnode *vp;
21354264Sbostic 	int error, s;
21451188Sbostic 
21551860Sbostic #ifdef VERBOSE
21651860Sbostic 	printf("lfs_segwrite\n");
21751860Sbostic #endif
21852328Sbostic 	fs = VFSTOUFS(mp)->um_lfs;
21954690Sbostic 	lfs_seglock(fs);
22052085Sbostic 
22151860Sbostic 	/*
22252328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
22352328Sbostic 	 * the maximum possible number of buffers which can be described in a
22452328Sbostic 	 * single summary block.
22552328Sbostic 	 */
22652328Sbostic 	sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
22752328Sbostic 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
22852328Sbostic 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
22952328Sbostic 	sp->seg_flags = do_ckp ? SEGM_CKP : 0;
23052328Sbostic 	lfs_initseg(fs, sp);
23152328Sbostic 
23252328Sbostic 	/*
23352688Sbostic 	 * Keep a cumulative count of the outstanding I/O operations.  If the
23452688Sbostic 	 * disk drive catches up with us it could go to zero before we finish,
23552688Sbostic 	 * so we artificially increment it by one until we've scheduled all of
23652688Sbostic 	 * the writes we intend to do.  If not a checkpoint, we never do the
23752688Sbostic 	 * final decrement, avoiding the wakeup in the callback routine.
23851860Sbostic 	 */
23952688Sbostic 	s = splbio();
24054264Sbostic 	fs->lfs_iocount++;
24152688Sbostic 	splx(s);
24251342Sbostic 
24354264Sbostic 	lfs_writevnodes(fs, mp, sp, 0);
24454264Sbostic 	s = splbio();
24554264Sbostic 	fs->lfs_writer = 1;
24654264Sbostic 	if (fs->lfs_dirops && (error =
24754264Sbostic 	    tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
24854264Sbostic 		free(sp->bpp, M_SEGMENT);
24954264Sbostic 		free(sp, M_SEGMENT);
25054264Sbostic 		fs->lfs_writer = 0;
25154264Sbostic 		splx(s);
25254264Sbostic 		return(error);
25354264Sbostic 	}
25454264Sbostic 	splx(s);
25551860Sbostic 
25654264Sbostic 	lfs_writevnodes(fs, mp, sp, 1);
25751860Sbostic 
25854264Sbostic 	/*
25954264Sbostic 	 * If this is a checkpoint, we need to loop on both the ifile and
26054264Sbostic 	 * the writeseg to make sure that we don't end up with any dirty
26154264Sbostic 	 * buffers left when this is all over.
26254264Sbostic 	 */
26354264Sbostic 	if (do_ckp || fs->lfs_doifile) {
26454264Sbostic redo:
26554264Sbostic 		vp = fs->lfs_ivnode;
26654264Sbostic 		while (vget(vp));
26752328Sbostic 		ip = VTOI(vp);
26854264Sbostic 		do {
26952328Sbostic 			if (vp->v_dirtyblkhd != NULL)
27052328Sbostic 				lfs_writefile(fs, sp, vp);
27154264Sbostic 		} while (lfs_writeinode(fs, sp, ip) && do_ckp);
27252077Sbostic 		ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
27352077Sbostic 		vput(vp);
27454264Sbostic 		if (lfs_writeseg(fs, sp) && do_ckp) {
27554264Sbostic 			lfs_initseg(fs, sp);
27654264Sbostic 			goto redo;
27754264Sbostic 		}
27854264Sbostic 	} else
27954264Sbostic 		(void) lfs_writeseg(fs, sp);
28051342Sbostic 
28151215Sbostic 	/*
28251860Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
28351860Sbostic 	 * moment, the user's process hangs around so we can sleep.
28451215Sbostic 	 */
28552688Sbostic 	s = splbio();
28652688Sbostic 	--fs->lfs_iocount;
28754264Sbostic 	fs->lfs_writer = 0;
28854264Sbostic 	fs->lfs_doifile = 0;
28954264Sbostic 	wakeup(&fs->lfs_dirops);
29054264Sbostic 
29151860Sbostic 	if (do_ckp) {
29252688Sbostic 		if (fs->lfs_iocount && (error =
29352995Sbostic 		    tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs sync", 0))) {
29452995Sbostic 			free(sp->bpp, M_SEGMENT);
29552995Sbostic 			free(sp, M_SEGMENT);
29651915Sbostic 			return (error);
29752995Sbostic 		}
29851860Sbostic 		splx(s);
29951860Sbostic 		lfs_writesuper(fs, sp);
30052688Sbostic 	} else
30152688Sbostic 		splx(s);
30251215Sbostic 
30354690Sbostic 	lfs_segunlock(fs);
30454690Sbostic 
30551927Sbostic 	free(sp->bpp, M_SEGMENT);
30651927Sbostic 	free(sp, M_SEGMENT);
30751215Sbostic 
30851860Sbostic 	return (0);
30951188Sbostic }
31051188Sbostic 
31151860Sbostic /*
31251860Sbostic  * Write the dirty blocks associated with a vnode.
31351860Sbostic  */
31452077Sbostic void
31551860Sbostic lfs_writefile(fs, sp, vp)
31651499Sbostic 	struct lfs *fs;
31752085Sbostic 	struct segment *sp;
31852085Sbostic 	struct vnode *vp;
31951188Sbostic {
32051860Sbostic 	struct buf *bp;
32152085Sbostic 	struct finfo *fip;
32251860Sbostic 	IFILE *ifp;
32351188Sbostic 
32451860Sbostic #ifdef VERBOSE
32551860Sbostic 	printf("lfs_writefile\n");
32651860Sbostic #endif
32752085Sbostic 	if (sp->seg_bytes_left < fs->lfs_bsize ||
32852085Sbostic 	    sp->sum_bytes_left < sizeof(struct finfo)) {
32954264Sbostic 		(void) lfs_writeseg(fs, sp);
33052085Sbostic 		lfs_initseg(fs, sp);
33152085Sbostic 	}
33252085Sbostic 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
33351215Sbostic 
33452085Sbostic 	fip = sp->fip;
33552085Sbostic 	fip->fi_nblocks = 0;
33652085Sbostic 	fip->fi_ino = VTOI(vp)->i_number;
33752085Sbostic 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
33852085Sbostic 	fip->fi_version = ifp->if_version;
33952085Sbostic 	brelse(bp);
34051188Sbostic 
34152085Sbostic 	/*
34252085Sbostic 	 * It may not be necessary to write the meta-data blocks at this point,
34352085Sbostic 	 * as the roll-forward recovery code should be able to reconstruct the
34452085Sbostic 	 * list.
34552085Sbostic 	 */
34652085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_data);
34752085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_indir);
34852085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_dindir);
34951860Sbostic #ifdef TRIPLE
35052085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_tindir);
35151860Sbostic #endif
35251342Sbostic 
35352085Sbostic 	fip = sp->fip;
35451860Sbostic #ifdef META
35552085Sbostic 	printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks);
35651860Sbostic #endif
35752085Sbostic 	if (fip->fi_nblocks != 0) {
35852085Sbostic 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
35952085Sbostic 		sp->fip =
36052085Sbostic 		    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
36152085Sbostic 		    sizeof(daddr_t) * (fip->fi_nblocks - 1));
36252682Sstaelin 	} else
36352682Sstaelin 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
36451215Sbostic }
36551215Sbostic 
36654264Sbostic int
36751915Sbostic lfs_writeinode(fs, sp, ip)
36851915Sbostic 	struct lfs *fs;
36952085Sbostic 	struct segment *sp;
37052085Sbostic 	struct inode *ip;
37151915Sbostic {
37252085Sbostic 	struct buf *bp, *ibp;
37352077Sbostic 	IFILE *ifp;
37452682Sstaelin 	SEGUSE *sup;
37552682Sstaelin 	daddr_t daddr;
37652077Sbostic 	ino_t ino;
37751915Sbostic 	int ndx;
37854264Sbostic 	int redo_ifile = 0;
37951915Sbostic 
38051915Sbostic #ifdef VERBOSE
38151915Sbostic 	printf("lfs_writeinode\n");
38251915Sbostic #endif
38351915Sbostic 	/* Allocate a new inode block if necessary. */
38451915Sbostic 	if (sp->ibp == NULL) {
38551915Sbostic 		/* Allocate a new segment if necessary. */
38651915Sbostic 		if (sp->seg_bytes_left < fs->lfs_bsize ||
38751915Sbostic 		    sp->sum_bytes_left < sizeof(daddr_t)) {
38854264Sbostic 			(void) lfs_writeseg(fs, sp);
38951915Sbostic 			lfs_initseg(fs, sp);
39051915Sbostic 		}
39151915Sbostic 
39251915Sbostic 		/* Get next inode block. */
39352682Sstaelin 		daddr = fs->lfs_offset;
39451915Sbostic 		fs->lfs_offset += fsbtodb(fs, 1);
39551915Sbostic 		sp->ibp = *sp->cbpp++ =
39652688Sbostic 		    lfs_newbuf(fs, daddr, fs->lfs_bsize);
39751915Sbostic 
39852688Sbostic 		/* Set remaining space counters. */
39951915Sbostic 		sp->seg_bytes_left -= fs->lfs_bsize;
40051915Sbostic 		sp->sum_bytes_left -= sizeof(daddr_t);
40152077Sbostic 		ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
40251915Sbostic 		    sp->ninodes / INOPB(fs) - 1;
40352682Sstaelin 		((daddr_t *)(sp->segsum))[ndx] = daddr;
40451915Sbostic 	}
40551915Sbostic 
40652085Sbostic 	/* Update the inode times and copy the inode onto the inode page. */
40752077Sbostic 	ITIMES(ip, &time, &time);
40851915Sbostic 	bp = sp->ibp;
40952085Sbostic 	bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
41051915Sbostic 
41151915Sbostic 	/* Increment inode count in segment summary block. */
41251915Sbostic 	++((SEGSUM *)(sp->segsum))->ss_ninos;
41351915Sbostic 
41451915Sbostic 	/* If this page is full, set flag to allocate a new page. */
41551915Sbostic 	if (++sp->ninodes % INOPB(fs) == 0)
41651915Sbostic 		sp->ibp = NULL;
41751915Sbostic 
41851915Sbostic 	/*
41952077Sbostic 	 * If updating the ifile, update the super-block.  Update the disk
42052077Sbostic 	 * address and access times for this inode in the ifile.
42151915Sbostic 	 */
42252077Sbostic 	ino = ip->i_number;
42352077Sbostic 	if (ino == LFS_IFILE_INUM)
42451915Sbostic 		fs->lfs_idaddr = bp->b_blkno;
42552077Sbostic 
42652077Sbostic 	LFS_IENTRY(ifp, fs, ino, ibp);
42752682Sstaelin 	daddr = ifp->if_daddr;
42852077Sbostic 	ifp->if_daddr = bp->b_blkno;
42952085Sbostic 	LFS_UBWRITE(ibp);
43054264Sbostic 	redo_ifile = (ino == LFS_IFILE_INUM && !(ibp->b_flags & B_GATHERED));
43152682Sstaelin 
43254264Sbostic 	/*
43354264Sbostic 	 * No need to update segment usage if there was no former inode address
43454264Sbostic 	 * or if the last inode address is in the current partial segment.
43554264Sbostic 	 */
43654264Sbostic 	if (daddr != LFS_UNUSED_DADDR &&
43754264Sbostic 	    !(daddr >= fs->lfs_curseg && daddr <= ifp->if_daddr) ) {
43852682Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
43952682Sstaelin #ifdef DIAGNOSTIC
44054264Sbostic 		if (sup->su_nbytes < sizeof(struct dinode)) {
44152819Sbostic 			/* XXX -- Change to a panic. */
44252819Sbostic 			printf("lfs: negative bytes (segment %d)\n",
44352682Sstaelin 			    datosn(fs, daddr));
44454264Sbostic 			panic("negative bytes");
44554264Sbostic 		}
44652682Sstaelin #endif
44752682Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
44852682Sstaelin 		LFS_UBWRITE(bp);
44954264Sbostic 		redo_ifile |=
45054264Sbostic 		    (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
45152682Sstaelin 	}
45254264Sbostic 	return(redo_ifile);
45351915Sbostic }
45451915Sbostic 
45552077Sbostic void
45651215Sbostic lfs_gather(fs, sp, vp, match)
45751499Sbostic 	struct lfs *fs;
45852085Sbostic 	struct segment *sp;
45952085Sbostic 	struct vnode *vp;
46052085Sbostic 	int (*match) __P((struct lfs *, struct buf *));
46151215Sbostic {
46254264Sbostic 	struct buf **bpp, *bp;
46354264Sbostic struct buf *lastbp;
46452085Sbostic 	struct finfo *fip;
46552085Sbostic 	struct inode *ip;
46651215Sbostic 	daddr_t *lbp, *start_lbp;
46751342Sbostic 	u_long version;
46851342Sbostic 	int s;
46951215Sbostic 
47051860Sbostic #ifdef VERBOSE
47151860Sbostic 	printf("lfs_gather\n");
47251860Sbostic #endif
47351215Sbostic 	ip = VTOI(vp);
47451215Sbostic 	bpp = sp->cbpp;
47551215Sbostic 	fip = sp->fip;
47651215Sbostic 	start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks];
47751215Sbostic 
47853145Sstaelin loop:	s = splbio();
47954264Sbostic 	lastbp = NULL;
48054264Sbostic 	for (bp = vp->v_dirtyblkhd; bp; lastbp = bp, bp = bp->b_blockf) {
48154264Sbostic 		if (bp->b_flags & B_BUSY || !match(fs, bp) ||
48254264Sbostic 		    bp->b_flags & B_GATHERED)
48351215Sbostic 			continue;
48451342Sbostic #ifdef DIAGNOSTIC
48551860Sbostic 		if (!(bp->b_flags & B_DELWRI))
48651915Sbostic 			panic("lfs_gather: bp not B_DELWRI");
48751860Sbostic 		if (!(bp->b_flags & B_LOCKED))
48851915Sbostic 			panic("lfs_gather: bp not B_LOCKED");
48951342Sbostic #endif
49051860Sbostic 		/*
49151860Sbostic 		 * If full, finish this segment.  We may be doing I/O, so
49251860Sbostic 		 * release and reacquire the splbio().
49351860Sbostic 		 */
49451342Sbostic 		if (sp->sum_bytes_left < sizeof(daddr_t) ||
49551215Sbostic 		    sp->seg_bytes_left < fs->lfs_bsize) {
49651215Sbostic 			splx(s);
49751342Sbostic 			lfs_updatemeta(fs,
49851860Sbostic 			    sp, vp, start_lbp, bpp, lbp - start_lbp);
49951215Sbostic 
50051342Sbostic 			/* Add the current file to the segment summary. */
50151342Sbostic 			++((SEGSUM *)(sp->segsum))->ss_nfinfo;
50251215Sbostic 
50351342Sbostic 			version = fip->fi_version;
50454264Sbostic 			(void) lfs_writeseg(fs, sp);
50551915Sbostic 			lfs_initseg(fs, sp);
50651342Sbostic 
50751215Sbostic 			fip = sp->fip;
50851342Sbostic 			fip->fi_version = version;
50951215Sbostic 			fip->fi_ino = ip->i_number;
51051342Sbostic 			start_lbp = lbp = fip->fi_blocks;
51151342Sbostic 
51252682Sstaelin 			sp->sum_bytes_left -=
51352682Sstaelin 			    sizeof(struct finfo) - sizeof(daddr_t);
51452682Sstaelin 
51551215Sbostic 			bpp = sp->cbpp;
51653145Sstaelin 			goto loop;
51751215Sbostic 		}
51852682Sstaelin 
51952682Sstaelin 		/* Insert into the buffer list, update the FINFO block. */
52054264Sbostic 		bp->b_flags |= B_GATHERED;
52152682Sstaelin 		*sp->cbpp++ = bp;
52252682Sstaelin 		++fip->fi_nblocks;
52352682Sstaelin 		*lbp++ = bp->b_lblkno;
52452682Sstaelin 
52552682Sstaelin 		sp->sum_bytes_left -= sizeof(daddr_t);
52652682Sstaelin 		sp->seg_bytes_left -= bp->b_bufsize;
52751188Sbostic 	}
52851215Sbostic 	splx(s);
52951860Sbostic 	lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp);
53051188Sbostic }
53151188Sbostic 
53251342Sbostic /*
53351342Sbostic  * Update the metadata that points to the blocks listed in the FINFO
53451188Sbostic  * array.
53551188Sbostic  */
53652077Sbostic void
53751860Sbostic lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks)
53851499Sbostic 	struct lfs *fs;
53952085Sbostic 	struct segment *sp;
54052085Sbostic 	struct vnode *vp;
54151215Sbostic 	daddr_t *lbp;
54252085Sbostic 	struct buf **bpp;
54351215Sbostic 	int nblocks;
54451188Sbostic {
54551915Sbostic 	SEGUSE *sup;
54652085Sbostic 	struct buf *bp;
54751860Sbostic 	INDIR a[NIADDR], *ap;
54852085Sbostic 	struct inode *ip;
54951915Sbostic 	daddr_t daddr, lbn, off;
55051860Sbostic 	int db_per_fsb, error, i, num;
55151188Sbostic 
55251860Sbostic #ifdef VERBOSE
55351860Sbostic 	printf("lfs_updatemeta\n");
55451860Sbostic #endif
55551342Sbostic 	if (nblocks == 0)
55651215Sbostic 		return;
55751215Sbostic 
55851915Sbostic 	/* Sort the blocks. */
55952077Sbostic 	lfs_shellsort(bpp, lbp, nblocks);
56051215Sbostic 
56151915Sbostic 	/*
56251915Sbostic 	 * Assign disk addresses, and update references to the logical
56351915Sbostic 	 * block and the segment usage information.
56451915Sbostic 	 */
56551860Sbostic 	db_per_fsb = fsbtodb(fs, 1);
56651915Sbostic 	for (i = nblocks; i--; ++bpp) {
56751915Sbostic 		lbn = *lbp++;
56851915Sbostic 		(*bpp)->b_blkno = off = fs->lfs_offset;
56951860Sbostic 		fs->lfs_offset += db_per_fsb;
57051215Sbostic 
57151860Sbostic 		if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num))
57252085Sbostic 			panic("lfs_updatemeta: lfs_bmaparray %d", error);
57351860Sbostic 		ip = VTOI(vp);
57451860Sbostic 		switch (num) {
57551860Sbostic 		case 0:
57651915Sbostic 			ip->i_db[lbn] = off;
57751860Sbostic 			break;
57851860Sbostic 		case 1:
57951915Sbostic 			ip->i_ib[a[0].in_off] = off;
58051860Sbostic 			break;
58151860Sbostic 		default:
58251860Sbostic 			ap = &a[num - 1];
58351860Sbostic 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
58451860Sbostic 				panic("lfs_updatemeta: bread bno %d",
58551860Sbostic 				    ap->in_lbn);
586*55458Sbostic 			/*
587*55458Sbostic 			 * Bread may create a new indirect block which needs
588*55458Sbostic 			 * to get counted for the inode.
589*55458Sbostic 			 */
590*55458Sbostic 			if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE))
591*55458Sbostic 				ip->i_blocks += btodb(fs->lfs_bsize);
59251915Sbostic 			bp->b_un.b_daddr[ap->in_off] = off;
59353530Sheideman 			VOP_BWRITE(bp);
59451188Sbostic 		}
59551915Sbostic 
59651915Sbostic 		/* Update segment usage information. */
59751915Sbostic 		if (daddr != UNASSIGNED) {
59851915Sbostic 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
59951915Sbostic #ifdef DIAGNOSTIC
60054264Sbostic 			if (sup->su_nbytes < fs->lfs_bsize) {
60152819Sbostic 				/* XXX -- Change to a panic. */
60252819Sbostic 				printf("lfs: negative bytes (segment %d)\n",
60351915Sbostic 				    datosn(fs, daddr));
60454264Sbostic 				panic ("Negative Bytes");
60554264Sbostic 			}
60651915Sbostic #endif
60751915Sbostic 			sup->su_nbytes -= fs->lfs_bsize;
60852085Sbostic 			LFS_UBWRITE(bp);
60951915Sbostic 		}
61051188Sbostic 	}
61151188Sbostic }
61251188Sbostic 
61351915Sbostic /*
61451915Sbostic  * Start a new segment.
61551915Sbostic  */
61652077Sbostic void
61751915Sbostic lfs_initseg(fs, sp)
61851499Sbostic 	struct lfs *fs;
61952085Sbostic 	struct segment *sp;
62051188Sbostic {
62151915Sbostic 	SEGUSE *sup;
62251915Sbostic 	SEGSUM *ssp;
62351915Sbostic 	struct buf *bp;
62451915Sbostic 	daddr_t lbn, *lbnp;
62551215Sbostic 
62651860Sbostic #ifdef VERBOSE
62751915Sbostic 	printf("lfs_initseg\n");
62851860Sbostic #endif
62951915Sbostic 	/* Advance to the next segment. */
63051927Sbostic 	if (!LFS_PARTIAL_FITS(fs)) {
63152682Sstaelin 		/* Wake up any cleaning procs waiting on this file system. */
63252688Sbostic 		wakeup(&fs->lfs_nextseg);
63352688Sbostic 		wakeup(&lfs_allclean_wakeup);
63452682Sstaelin 
63551927Sbostic 		lfs_newseg(fs);
63651927Sbostic 		fs->lfs_offset = fs->lfs_curseg;
63751915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
63851915Sbostic 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
63951915Sbostic 
64051915Sbostic 		/*
64151927Sbostic 		 * If the segment contains a superblock, update the offset
64251927Sbostic 		 * and summary address to skip over it.
64351915Sbostic 		 */
64452077Sbostic 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
64551927Sbostic 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
64651915Sbostic 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
64751915Sbostic 			sp->seg_bytes_left -= LFS_SBPAD;
64851215Sbostic 		}
64952085Sbostic 		brelse(bp);
65051915Sbostic 	} else {
65151915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
65251915Sbostic 		sp->seg_bytes_left = (fs->lfs_dbpseg -
65351915Sbostic 		    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
65451915Sbostic 	}
65554264Sbostic 	fs->lfs_lastpseg = fs->lfs_offset;
65651342Sbostic 
65751915Sbostic 	sp->ibp = NULL;
65851915Sbostic 	sp->ninodes = 0;
65951342Sbostic 
66051915Sbostic 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
66151915Sbostic 	sp->cbpp = sp->bpp;
66252688Sbostic 	*sp->cbpp = lfs_newbuf(fs, fs->lfs_offset, LFS_SUMMARY_SIZE);
66351915Sbostic 	sp->segsum = (*sp->cbpp)->b_un.b_addr;
66451915Sbostic 	++sp->cbpp;
66551915Sbostic 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
66651342Sbostic 
66751915Sbostic 	/* Set point to SEGSUM, initialize it. */
66851915Sbostic 	ssp = sp->segsum;
66951915Sbostic 	ssp->ss_next = fs->lfs_nextseg;
67051915Sbostic 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
67151342Sbostic 
67251915Sbostic 	/* Set pointer to first FINFO, initialize it. */
67352085Sbostic 	sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
67451915Sbostic 	sp->fip->fi_nblocks = 0;
67551342Sbostic 
67651915Sbostic 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
67751915Sbostic 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
67851915Sbostic }
67951342Sbostic 
68051915Sbostic /*
68151915Sbostic  * Return the next segment to write.
68251915Sbostic  */
68352077Sbostic void
68451915Sbostic lfs_newseg(fs)
68551915Sbostic 	struct lfs *fs;
68651915Sbostic {
68751927Sbostic 	CLEANERINFO *cip;
68851915Sbostic 	SEGUSE *sup;
68951915Sbostic 	struct buf *bp;
69051927Sbostic 	int curseg, isdirty, sn;
69151915Sbostic 
69251915Sbostic #ifdef VERBOSE
69351915Sbostic 	printf("lfs_newseg\n");
69451915Sbostic #endif
69551927Sbostic 	/*
69651927Sbostic 	 * Turn off the active bit for the current segment, turn on the
69751927Sbostic 	 * active and dirty bits for the next segment, update the cleaner
69851927Sbostic 	 * info.  Set the current segment to the next segment, get a new
69951927Sbostic 	 * next segment.
70051927Sbostic 	 */
70151927Sbostic 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp);
70251927Sbostic 	sup->su_flags &= ~SEGUSE_ACTIVE;
70352085Sbostic 	LFS_UBWRITE(bp);
70451927Sbostic 
70551927Sbostic 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
70654264Sbostic 	sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_LIVELOG;
70752085Sbostic 	LFS_UBWRITE(bp);
70851927Sbostic 
70951927Sbostic 	LFS_CLEANERINFO(cip, fs, bp);
71051927Sbostic 	--cip->clean;
71151927Sbostic 	++cip->dirty;
71252085Sbostic 	LFS_UBWRITE(bp);
71351927Sbostic 
71451927Sbostic 	fs->lfs_lastseg = fs->lfs_curseg;
71551927Sbostic 	fs->lfs_curseg = fs->lfs_nextseg;
71651927Sbostic 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
71751915Sbostic 		sn = (sn + 1) % fs->lfs_nseg;
71851927Sbostic 		if (sn == curseg)
71951915Sbostic 			panic("lfs_nextseg: no clean segments");
72051915Sbostic 		LFS_SEGENTRY(sup, fs, sn, bp);
72151915Sbostic 		isdirty = sup->su_flags & SEGUSE_DIRTY;
72252085Sbostic 		brelse(bp);
72351915Sbostic 		if (!isdirty)
72451915Sbostic 			break;
72551915Sbostic 	}
72651927Sbostic 	fs->lfs_nextseg = sntoda(fs, sn);
72751188Sbostic }
72851188Sbostic 
72954264Sbostic int
73051188Sbostic lfs_writeseg(fs, sp)
73151499Sbostic 	struct lfs *fs;
73252085Sbostic 	struct segment *sp;
73351188Sbostic {
73452688Sbostic 	struct buf **bpp, *bp, *cbp;
73551188Sbostic 	SEGUSE *sup;
73652085Sbostic 	SEGSUM *ssp;
73751860Sbostic 	dev_t i_dev;
73854264Sbostic 	size_t size;
73951860Sbostic 	u_long *datap, *dp;
74054264Sbostic 	int ch_per_blk, do_again, i, nblocks, num, s;
74154264Sbostic 	int (*strategy)__P((struct vop_strategy_args *));
74254690Sbostic 	struct vop_strategy_args vop_strategy_a;
74352688Sbostic 	char *p;
74451188Sbostic 
74551860Sbostic #ifdef VERBOSE
74651860Sbostic 	printf("lfs_writeseg\n");
74751860Sbostic #endif
74854264Sbostic 	/* Checkpoint always writes superblock, even if no data blocks. */
74954264Sbostic 	if ((nblocks = sp->cbpp - sp->bpp) == 0 && !(sp->seg_flags & SEGM_CKP))
75052085Sbostic 		return;
75152085Sbostic 
75251188Sbostic 	/*
75352085Sbostic 	 * Compute checksum across data and then across summary; the first
75452085Sbostic 	 * block (the summary block) is skipped.  Set the create time here
75552085Sbostic 	 * so that it's guaranteed to be later than the inode mod times.
75651860Sbostic 	 *
75751860Sbostic 	 * XXX
75851860Sbostic 	 * Fix this to do it inline, instead of malloc/copy.
75951188Sbostic 	 */
76051860Sbostic 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
76151915Sbostic 	for (bpp = sp->bpp, i = nblocks - 1; i--;)
76251915Sbostic 		*dp++ = (*++bpp)->b_un.b_words[0];
76352085Sbostic 	ssp = (SEGSUM *)sp->segsum;
76452103Sbostic 	ssp->ss_create = time.tv_sec;
76552085Sbostic 	ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long));
76652085Sbostic 	ssp->ss_sumsum =
76752085Sbostic 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
76851927Sbostic 	free(datap, M_SEGMENT);
76951188Sbostic 
77054264Sbostic 	/* Update the segment usage information. */
77154264Sbostic 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
77254264Sbostic 	sup->su_nbytes += nblocks - 1 -
77354264Sbostic 	    (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift;
77454264Sbostic 	sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
77554264Sbostic 	sup->su_lastmod = time.tv_sec;
77654264Sbostic 	LFS_UBWRITE(bp);
77754264Sbostic 	do_again = !(bp->b_flags & B_GATHERED);
77854264Sbostic 
77951860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
78053574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
78151301Sbostic 
78252688Sbostic 	/*
78352688Sbostic 	 * When we simply write the blocks we lose a rotation for every block
78452688Sbostic 	 * written.  To avoid this problem, we allocate memory in chunks, copy
78552688Sbostic 	 * the buffers into the chunk and write the chunk.  56K was chosen as
78652688Sbostic 	 * some driver/controllers can't handle unsigned 16 bit transfers.
78752688Sbostic 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
78852688Sbostic 	 * and brelse the buffer (which will move them to the LRU list).  Add
78952688Sbostic 	 * the B_CALL flag to the buffer header so we can count I/O's for the
79052688Sbostic 	 * checkpoints and so we can release the allocated memory.
79152688Sbostic 	 *
79252688Sbostic 	 * XXX
79352688Sbostic 	 * This should be removed if the new virtual memory system allows us to
79452688Sbostic 	 * easily make the buffers contiguous in kernel memory and if that's
79552688Sbostic 	 * fast enough.
79652688Sbostic 	 */
79752688Sbostic #define	LFS_CHUNKSIZE	(56 * 1024)
79852688Sbostic 	ch_per_blk = LFS_CHUNKSIZE / fs->lfs_bsize;
79952688Sbostic 	for (bpp = sp->bpp, i = nblocks; i;) {
80052688Sbostic 		num = ch_per_blk;
80152688Sbostic 		if (num > i)
80252688Sbostic 			num = i;
80352688Sbostic 		i -= num;
80452688Sbostic 		size = num * fs->lfs_bsize;
80552688Sbostic 
80652688Sbostic 		cbp = lfs_newbuf(fs, (*bpp)->b_blkno, 0);
80752688Sbostic 		cbp->b_dev = i_dev;
80852688Sbostic 		cbp->b_flags = B_ASYNC | B_BUSY | B_CALL;
80952688Sbostic 		cbp->b_iodone = lfs_callback;
81052688Sbostic 		cbp->b_saveaddr = cbp->b_un.b_addr;
81152688Sbostic 		cbp->b_un.b_addr = malloc(size, M_SEGMENT, M_WAITOK);
81252688Sbostic 
81352688Sbostic 		s = splbio();
81452688Sbostic 		++fs->lfs_iocount;
81552688Sbostic 		for (p = cbp->b_un.b_addr; num--;) {
81652688Sbostic 			bp = *bpp++;
81752688Sbostic 			bcopy(bp->b_un.b_addr, p, bp->b_bcount);
81852688Sbostic 			p += bp->b_bcount;
81954264Sbostic 			bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI |
82054264Sbostic 			     B_LOCKED | B_GATHERED);
82154264Sbostic 			if (!(bp->b_flags & (B_NOCACHE | B_INVAL))) {
82252688Sbostic 				bremfree(bp);
82352688Sbostic 				reassignbuf(bp, bp->b_vp);
82452688Sbostic 			}
82552688Sbostic 			brelse(bp);
82651860Sbostic 		}
82752688Sbostic 		splx(s);
82852688Sbostic 		cbp->b_bcount = p - cbp->b_un.b_addr;
82953574Sheideman 		vop_strategy_a.a_desc = VDESC(vop_strategy);
83053574Sheideman 		vop_strategy_a.a_bp = cbp;
83153574Sheideman 		(strategy)(&vop_strategy_a);
83251860Sbostic 	}
83354264Sbostic 	return(do_again);
83451188Sbostic }
83551188Sbostic 
83652077Sbostic void
83751860Sbostic lfs_writesuper(fs, sp)
83851499Sbostic 	struct lfs *fs;
83952085Sbostic 	struct segment *sp;
84051301Sbostic {
84152085Sbostic 	struct buf *bp;
84251860Sbostic 	dev_t i_dev;
84353574Sheideman 	int (*strategy) __P((struct vop_strategy_args *));
84454690Sbostic 	struct vop_strategy_args vop_strategy_a;
84551301Sbostic 
84651860Sbostic #ifdef VERBOSE
84751860Sbostic 	printf("lfs_writesuper\n");
84851860Sbostic #endif
84951860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
85053574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
85151356Sbostic 
85251342Sbostic 	/* Checksum the superblock and copy it into a buffer. */
85351499Sbostic 	fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
85452688Sbostic 	bp = lfs_newbuf(fs, fs->lfs_sboffs[0], LFS_SBPAD);
85551860Sbostic 	*bp->b_un.b_lfs = *fs;
85651215Sbostic 
85751356Sbostic 	/* Write the first superblock (wait). */
85851860Sbostic 	bp->b_dev = i_dev;
85951915Sbostic 	bp->b_flags |= B_BUSY;
86051860Sbostic 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
86153574Sheideman 	vop_strategy_a.a_desc = VDESC(vop_strategy);
86253574Sheideman 	vop_strategy_a.a_bp = bp;
86353574Sheideman 	(strategy)(&vop_strategy_a);
86451215Sbostic 	biowait(bp);
86551342Sbostic 
86651356Sbostic 	/* Write the second superblock (don't wait). */
86751215Sbostic 	bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1];
86851915Sbostic 	bp->b_flags |= B_ASYNC | B_BUSY;
86951860Sbostic 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
87053574Sheideman 	(strategy)(&vop_strategy_a);
87151215Sbostic }
87251215Sbostic 
87351342Sbostic /*
87451342Sbostic  * Logical block number match routines used when traversing the dirty block
87551342Sbostic  * chain.
87651342Sbostic  */
87752077Sbostic int
87852077Sbostic lfs_match_data(fs, bp)
87951860Sbostic 	struct lfs *fs;
88052085Sbostic 	struct buf *bp;
88151215Sbostic {
88251342Sbostic 	return (bp->b_lblkno >= 0);
88351215Sbostic }
88451215Sbostic 
88552077Sbostic int
88652077Sbostic lfs_match_indir(fs, bp)
88751860Sbostic 	struct lfs *fs;
88852085Sbostic 	struct buf *bp;
88951215Sbostic {
89051860Sbostic 	int lbn;
89151860Sbostic 
89251860Sbostic 	lbn = bp->b_lblkno;
89351860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
89451215Sbostic }
89551215Sbostic 
89652077Sbostic int
89752077Sbostic lfs_match_dindir(fs, bp)
89851860Sbostic 	struct lfs *fs;
89952085Sbostic 	struct buf *bp;
90051215Sbostic {
90151860Sbostic 	int lbn;
90251860Sbostic 
90351860Sbostic 	lbn = bp->b_lblkno;
90451860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
90551215Sbostic }
90651215Sbostic 
90752077Sbostic int
90852077Sbostic lfs_match_tindir(fs, bp)
90951499Sbostic 	struct lfs *fs;
91052085Sbostic 	struct buf *bp;
91151342Sbostic {
91251860Sbostic 	int lbn;
91351342Sbostic 
91451860Sbostic 	lbn = bp->b_lblkno;
91551860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
91651860Sbostic }
91751342Sbostic 
91851860Sbostic /*
91951860Sbostic  * Allocate a new buffer header.
92051860Sbostic  */
92152085Sbostic struct buf *
92252688Sbostic lfs_newbuf(fs, daddr, size)
92351860Sbostic 	struct lfs *fs;
92451860Sbostic 	daddr_t daddr;
92551860Sbostic 	size_t size;
92651860Sbostic {
92752085Sbostic 	struct buf *bp;
92851342Sbostic 
92951860Sbostic #ifdef VERBOSE
93051860Sbostic 	printf("lfs_newbuf\n");
93151860Sbostic #endif
93251860Sbostic 	bp = getnewbuf();
93351860Sbostic 	bremhash(bp);
93451860Sbostic 	bgetvp(fs->lfs_ivnode, bp);
93551860Sbostic 	bp->b_bcount = 0;
93651860Sbostic 	bp->b_lblkno = daddr;
93751860Sbostic 	bp->b_blkno = daddr;
93851860Sbostic 	bp->b_error = 0;
93951860Sbostic 	bp->b_resid = 0;
94052688Sbostic 	if (size)
94152688Sbostic 		allocbuf(bp, size);
94251860Sbostic 	bp->b_flags |= B_NOCACHE;
94352688Sbostic 	bp->b_saveaddr = NULL;
94451915Sbostic 	binshash(bp, &bfreelist[BQ_AGE]);
94551860Sbostic 	return (bp);
94651860Sbostic }
94751342Sbostic 
94853347Sbostic void
94951860Sbostic lfs_callback(bp)
95052085Sbostic 	struct buf *bp;
95151860Sbostic {
95251860Sbostic 	struct lfs *fs;
95351342Sbostic 
95451860Sbostic 	fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
95551860Sbostic #ifdef DIAGNOSTIC
95651860Sbostic 	if (fs->lfs_iocount == 0)
95751860Sbostic 		panic("lfs_callback: zero iocount\n");
95851860Sbostic #endif
95951860Sbostic 	if (--fs->lfs_iocount == 0)
96052688Sbostic 		wakeup(&fs->lfs_iocount);
96151915Sbostic 
96252688Sbostic 	if (bp->b_saveaddr) {
96352688Sbostic 		free(bp->b_un.b_addr, M_SEGMENT);
96452688Sbostic 		bp->b_un.b_addr = bp->b_saveaddr;
96552819Sbostic 		bp->b_saveaddr = NULL;
96652688Sbostic 	}
96751860Sbostic 	brelse(bp);
96851860Sbostic }
96951342Sbostic 
97051215Sbostic /*
97151188Sbostic  * Shellsort (diminishing increment sort) from Data Structures and
97251188Sbostic  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
97351188Sbostic  * see also Knuth Vol. 3, page 84.  The increments are selected from
97451188Sbostic  * formula (8), page 95.  Roughly O(N^3/2).
97551188Sbostic  */
97651188Sbostic /*
97751188Sbostic  * This is our own private copy of shellsort because we want to sort
97851188Sbostic  * two parallel arrays (the array of buffer pointers and the array of
97951188Sbostic  * logical block numbers) simultaneously.  Note that we cast the array
98051188Sbostic  * of logical block numbers to a unsigned in this routine so that the
98151188Sbostic  * negative block numbers (meta data blocks) sort AFTER the data blocks.
98251188Sbostic  */
98352077Sbostic void
98452077Sbostic lfs_shellsort(bp_array, lb_array, nmemb)
98552085Sbostic 	struct buf **bp_array;
98651215Sbostic 	daddr_t *lb_array;
98751188Sbostic 	register int nmemb;
98851188Sbostic {
98951188Sbostic 	static int __rsshell_increments[] = { 4, 1, 0 };
99051188Sbostic 	register int incr, *incrp, t1, t2;
99152085Sbostic 	struct buf *bp_temp;
99251188Sbostic 	u_long lb_temp;
99351188Sbostic 
99451188Sbostic 	for (incrp = __rsshell_increments; incr = *incrp++;)
99551188Sbostic 		for (t1 = incr; t1 < nmemb; ++t1)
99651188Sbostic 			for (t2 = t1 - incr; t2 >= 0;)
99751188Sbostic 				if (lb_array[t2] > lb_array[t2 + incr]) {
99851188Sbostic 					lb_temp = lb_array[t2];
99951188Sbostic 					lb_array[t2] = lb_array[t2 + incr];
100051188Sbostic 					lb_array[t2 + incr] = lb_temp;
100151188Sbostic 					bp_temp = bp_array[t2];
100251188Sbostic 					bp_array[t2] = bp_array[t2 + incr];
100351188Sbostic 					bp_array[t2 + incr] = bp_temp;
100451188Sbostic 					t2 -= incr;
100551188Sbostic 				} else
100651188Sbostic 					break;
100751188Sbostic }
1008