xref: /csrg-svn/sys/ufs/lfs/lfs_segment.c (revision 56027)
151188Sbostic /*
251188Sbostic  * Copyright (c) 1991 Regents of the University of California.
351188Sbostic  * All rights reserved.
451188Sbostic  *
551188Sbostic  * %sccs.include.redist.c%
651188Sbostic  *
7*56027Sbostic  *	@(#)lfs_segment.c	7.30 (Berkeley) 08/25/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 
3555940Sbostic #define MAX_ACTIVE	10
3651188Sbostic /*
3751860Sbostic  * Determine if it's OK to start a partial in this segment, or if we need
3851860Sbostic  * to go on to a new segment.
3951301Sbostic  */
4051860Sbostic #define	LFS_PARTIAL_FITS(fs) \
4151860Sbostic 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
4251860Sbostic 	1 << (fs)->lfs_fsbtodb)
4351188Sbostic 
4453347Sbostic void	 lfs_callback __P((struct buf *));
4552085Sbostic void	 lfs_gather __P((struct lfs *, struct segment *,
4652085Sbostic 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
4755940Sbostic int	 lfs_gatherblock __P((struct segment *, struct buf *, int *));
4852085Sbostic void	 lfs_initseg __P((struct lfs *, struct segment *));
4952085Sbostic void	 lfs_iset __P((struct inode *, daddr_t, time_t));
5052085Sbostic int	 lfs_match_data __P((struct lfs *, struct buf *));
5152085Sbostic int	 lfs_match_dindir __P((struct lfs *, struct buf *));
5252085Sbostic int	 lfs_match_indir __P((struct lfs *, struct buf *));
5352085Sbostic int	 lfs_match_tindir __P((struct lfs *, struct buf *));
5452077Sbostic void	 lfs_newseg __P((struct lfs *));
5552085Sbostic void	 lfs_shellsort __P((struct buf **, daddr_t *, register int));
5655940Sbostic void	 lfs_supercallback __P((struct buf *));
57*56027Sbostic void	 lfs_updatemeta __P((struct segment *));
5852085Sbostic void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
5954264Sbostic int	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
6054264Sbostic int	 lfs_writeseg __P((struct lfs *, struct segment *));
6152085Sbostic void	 lfs_writesuper __P((struct lfs *, struct segment *));
6254264Sbostic void	 lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
6354264Sbostic 	    struct segment *sp, int dirops));
6451188Sbostic 
6551860Sbostic int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
6651860Sbostic 
6752328Sbostic /*
6852328Sbostic  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
6952328Sbostic  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
7052328Sbostic  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
7152328Sbostic  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
7252328Sbostic  */
7352328Sbostic 
7451188Sbostic int
7552328Sbostic lfs_vflush(vp)
7652328Sbostic 	struct vnode *vp;
7752328Sbostic {
7852328Sbostic 	struct inode *ip;
7952328Sbostic 	struct lfs *fs;
8052328Sbostic 	struct segment *sp;
8152328Sbostic 	int error, s;
8252328Sbostic 
8354690Sbostic 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
8454690Sbostic 	lfs_seglock(fs);
8552328Sbostic 
8652328Sbostic 	/*
8752328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
8852328Sbostic 	 * the maximum possible number of buffers which can be described in a
8952328Sbostic 	 * single summary block.
9052328Sbostic 	 */
9152328Sbostic 	sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
9252328Sbostic 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
9352328Sbostic 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
9452328Sbostic 	sp->seg_flags = SEGM_CKP;
95*56027Sbostic 	sp->vp = NULL;
9652328Sbostic 
9752328Sbostic 	/*
9852328Sbostic 	 * Keep a cumulative count of the outstanding I/O operations.  If the
9952328Sbostic 	 * disk drive catches up with us it could go to zero before we finish,
10052328Sbostic 	 * so we artificially increment it by one until we've scheduled all of
10152328Sbostic 	 * the writes we intend to do.
10252328Sbostic 	 */
10352328Sbostic 	s = splbio();
10452688Sbostic 	++fs->lfs_iocount;
10552328Sbostic 	splx(s);
10652328Sbostic 
10752328Sbostic 	ip = VTOI(vp);
10855551Sbostic 	do {
10955803Sbostic 		lfs_initseg(fs, sp);
11055551Sbostic 		do {
11155551Sbostic 			if (vp->v_dirtyblkhd != NULL)
11255551Sbostic 				lfs_writefile(fs, sp, vp);
11355551Sbostic 		} while (lfs_writeinode(fs, sp, ip));
11452328Sbostic 
11555551Sbostic 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
11652328Sbostic 
11752328Sbostic 	/*
11852328Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
11952328Sbostic 	 * moment, the user's process hangs around so we can sleep.
12052328Sbostic 	 */
12152328Sbostic 	s = splbio();
12252328Sbostic 	if (--fs->lfs_iocount && (error =
12352995Sbostic 	    tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0))) {
12452995Sbostic 		free(sp->bpp, M_SEGMENT);
12552995Sbostic 		free(sp, M_SEGMENT);
12652328Sbostic 		return (error);
12752995Sbostic 	}
12852328Sbostic 	splx(s);
12954690Sbostic 	lfs_segunlock(fs);
13052328Sbostic 
13152995Sbostic 	/*
13252995Sbostic 	 * XXX
13352995Sbostic 	 * Should be writing a checkpoint?
13452995Sbostic 	 */
13552328Sbostic 	free(sp->bpp, M_SEGMENT);
13652328Sbostic 	free(sp, M_SEGMENT);
13752328Sbostic 
13852328Sbostic 	return (0);
13952328Sbostic }
14052328Sbostic 
14154264Sbostic void
14254264Sbostic lfs_writevnodes(fs, mp, sp, dirops)
14354264Sbostic 	struct lfs *fs;
14454264Sbostic 	struct mount *mp;
14554264Sbostic 	struct segment *sp;
14654264Sbostic 	int dirops;
14754264Sbostic {
14854264Sbostic 	struct inode *ip;
14954264Sbostic 	struct vnode *vp;
15054264Sbostic 	int error, s;
15154264Sbostic 
15254264Sbostic loop:	for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
15354264Sbostic 		/*
15454264Sbostic 		 * If the vnode that we are about to sync is no longer
15554264Sbostic 		 * associated with this mount point, start over.
15654264Sbostic 		 */
15754264Sbostic 		if (vp->v_mount != mp)
15854264Sbostic 			goto loop;
15954264Sbostic 
16054264Sbostic 		if (dirops && !(vp->v_flag & VDIROP) ||
16154264Sbostic 		    !dirops && (vp->v_flag & VDIROP))
16254264Sbostic 			continue;
16354264Sbostic 		/*
16454264Sbostic 		 * XXX
16554264Sbostic 		 * Up the ref count so we don't get tossed out of
16654264Sbostic 		 * memory.
16754264Sbostic 		 */
16854264Sbostic 		VREF(vp);
16954264Sbostic 
17054264Sbostic 		/*
17154264Sbostic 		 * Write the inode/file if dirty and it's not the
17254264Sbostic 		 * the IFILE.
17354264Sbostic 		 */
17454264Sbostic 		ip = VTOI(vp);
17554264Sbostic 		if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) ||
17654264Sbostic 		    vp->v_dirtyblkhd != NULL) &&
17754264Sbostic 		    ip->i_number != LFS_IFILE_INUM) {
17854264Sbostic 			if (vp->v_dirtyblkhd != NULL)
17954264Sbostic 				lfs_writefile(fs, sp, vp);
18054264Sbostic 			(void) lfs_writeinode(fs, sp, ip);
18154264Sbostic 		}
18254264Sbostic 		vp->v_flag &= ~VDIROP;
18354264Sbostic 		vrele(vp);
18454264Sbostic 	}
18554264Sbostic }
18654264Sbostic 
18752328Sbostic int
18851215Sbostic lfs_segwrite(mp, do_ckp)
18952085Sbostic 	struct mount *mp;
19051860Sbostic 	int do_ckp;			/* Do a checkpoint. */
19151188Sbostic {
19255592Sbostic 	struct buf *bp;
19352085Sbostic 	struct inode *ip;
19451499Sbostic 	struct lfs *fs;
19552085Sbostic 	struct segment *sp;
19652085Sbostic 	struct vnode *vp;
19755592Sbostic 	SEGUSE *segusep;
19855592Sbostic 	daddr_t ibno;
19955940Sbostic 	CLEANERINFO *cip;
20055940Sbostic 	int clean, error, i, s;
20151188Sbostic 
20252328Sbostic 	fs = VFSTOUFS(mp)->um_lfs;
20355940Sbostic 
20455940Sbostic  	/*
20555940Sbostic  	 * If we have fewer than 2 clean segments, wait until cleaner
20655940Sbostic 	 * writes.
20755940Sbostic  	 */
20855940Sbostic 	do {
20955940Sbostic 		LFS_CLEANERINFO(cip, fs, bp);
21055940Sbostic 		clean = cip->clean;
21155940Sbostic 		brelse(bp);
21255940Sbostic 		if (clean <= 2) {
21355940Sbostic 			printf ("segs clean: %d\n", clean);
21455940Sbostic 			wakeup(&lfs_allclean_wakeup);
21555940Sbostic 			if (error = tsleep(&fs->lfs_avail, PRIBIO + 1,
21655940Sbostic 			    "lfs writer", 0))
21755940Sbostic 				return (error);
21855940Sbostic 		}
21955940Sbostic 	} while (clean <= 2 );
22054690Sbostic 	lfs_seglock(fs);
22152085Sbostic 
22251860Sbostic 	/*
22352328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
22452328Sbostic 	 * the maximum possible number of buffers which can be described in a
22552328Sbostic 	 * single summary block.
22652328Sbostic 	 */
22755940Sbostic 	do_ckp = do_ckp || fs->lfs_nactive > MAX_ACTIVE;
22852328Sbostic 	sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
22952328Sbostic 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
23052328Sbostic 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
23152328Sbostic 	sp->seg_flags = do_ckp ? SEGM_CKP : 0;
232*56027Sbostic 	sp->vp = NULL;
23352328Sbostic 	lfs_initseg(fs, sp);
23452328Sbostic 
23552328Sbostic 	/*
23652688Sbostic 	 * Keep a cumulative count of the outstanding I/O operations.  If the
23752688Sbostic 	 * disk drive catches up with us it could go to zero before we finish,
23852688Sbostic 	 * so we artificially increment it by one until we've scheduled all of
23952688Sbostic 	 * the writes we intend to do.  If not a checkpoint, we never do the
24052688Sbostic 	 * final decrement, avoiding the wakeup in the callback routine.
24151860Sbostic 	 */
24252688Sbostic 	s = splbio();
24355551Sbostic 	++fs->lfs_iocount;
24452688Sbostic 	splx(s);
24551342Sbostic 
24654264Sbostic 	lfs_writevnodes(fs, mp, sp, 0);
24754264Sbostic 	fs->lfs_writer = 1;
24854264Sbostic 	if (fs->lfs_dirops && (error =
24954264Sbostic 	    tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
25054264Sbostic 		free(sp->bpp, M_SEGMENT);
25154264Sbostic 		free(sp, M_SEGMENT);
25254264Sbostic 		fs->lfs_writer = 0;
25355551Sbostic 		return (error);
25454264Sbostic 	}
25551860Sbostic 
25654264Sbostic 	lfs_writevnodes(fs, mp, sp, 1);
25751860Sbostic 
25854264Sbostic 	/*
25955592Sbostic 	 * If we are doing a checkpoint, mark everything since the
26055592Sbostic 	 * last checkpoint as no longer ACTIVE.
26154264Sbostic 	 */
26255592Sbostic 	if (do_ckp)
26355592Sbostic 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
26455592Sbostic 		     --ibno >= fs->lfs_cleansz; ) {
26555592Sbostic 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
26655592Sbostic 			    NOCRED, &bp))
26755592Sbostic 
26855592Sbostic 				panic("lfs: ifile read");
26955592Sbostic 			segusep = (SEGUSE *)bp->b_un.b_addr;
27055592Sbostic 			for (i = fs->lfs_sepb; i--; segusep++)
27155592Sbostic 				segusep->su_flags &= ~SEGUSE_ACTIVE;
27255592Sbostic 
27355940Sbostic 			error = VOP_BWRITE(bp);
27455592Sbostic 		}
27555592Sbostic 
27654264Sbostic 	if (do_ckp || fs->lfs_doifile) {
27754264Sbostic 		vp = fs->lfs_ivnode;
27854264Sbostic 		while (vget(vp));
27952328Sbostic 		ip = VTOI(vp);
28055592Sbostic 		if (vp->v_dirtyblkhd != NULL)
28155592Sbostic 			lfs_writefile(fs, sp, vp);
28255592Sbostic 		(void)lfs_writeinode(fs, sp, ip);
28352077Sbostic 		vput(vp);
28455592Sbostic 		/*
28555592Sbostic 		 * This should never happen because we just guaranteed
28655592Sbostic 		 * that all the segment usage table blocks are dirty, so
28755592Sbostic 		 * no new ones should get written.
28855592Sbostic 		 */
28955592Sbostic 		if (lfs_writeseg(fs, sp) && do_ckp)
29055592Sbostic 			panic("lfs_segwrite: created dirty blocks on ckp");
29154264Sbostic 	} else
29254264Sbostic 		(void) lfs_writeseg(fs, sp);
29351342Sbostic 
29451215Sbostic 	/*
29551860Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
29651860Sbostic 	 * moment, the user's process hangs around so we can sleep.
29751215Sbostic 	 */
29854264Sbostic 	fs->lfs_writer = 0;
29954264Sbostic 	fs->lfs_doifile = 0;
30054264Sbostic 	wakeup(&fs->lfs_dirops);
30154264Sbostic 
30255551Sbostic 	s = splbio();
30355551Sbostic 	--fs->lfs_iocount;
30451860Sbostic 	if (do_ckp) {
30552688Sbostic 		if (fs->lfs_iocount && (error =
30652995Sbostic 		    tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs sync", 0))) {
30752995Sbostic 			free(sp->bpp, M_SEGMENT);
30852995Sbostic 			free(sp, M_SEGMENT);
30951915Sbostic 			return (error);
31052995Sbostic 		}
31151860Sbostic 		splx(s);
31255940Sbostic 		fs->lfs_nactive = 0;
31351860Sbostic 		lfs_writesuper(fs, sp);
31452688Sbostic 	} else
31552688Sbostic 		splx(s);
31651215Sbostic 
31754690Sbostic 	lfs_segunlock(fs);
31854690Sbostic 
31951927Sbostic 	free(sp->bpp, M_SEGMENT);
32051927Sbostic 	free(sp, M_SEGMENT);
32151215Sbostic 
32251860Sbostic 	return (0);
32351188Sbostic }
32451188Sbostic 
32551860Sbostic /*
32651860Sbostic  * Write the dirty blocks associated with a vnode.
32751860Sbostic  */
32852077Sbostic void
32951860Sbostic lfs_writefile(fs, sp, vp)
33051499Sbostic 	struct lfs *fs;
33152085Sbostic 	struct segment *sp;
33252085Sbostic 	struct vnode *vp;
33351188Sbostic {
33451860Sbostic 	struct buf *bp;
33552085Sbostic 	struct finfo *fip;
33651860Sbostic 	IFILE *ifp;
33751188Sbostic 
33852085Sbostic 	if (sp->seg_bytes_left < fs->lfs_bsize ||
33952085Sbostic 	    sp->sum_bytes_left < sizeof(struct finfo)) {
34054264Sbostic 		(void) lfs_writeseg(fs, sp);
34152085Sbostic 		lfs_initseg(fs, sp);
34252085Sbostic 	}
34352085Sbostic 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
34451215Sbostic 
34552085Sbostic 	fip = sp->fip;
34652085Sbostic 	fip->fi_nblocks = 0;
34752085Sbostic 	fip->fi_ino = VTOI(vp)->i_number;
34852085Sbostic 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
34952085Sbostic 	fip->fi_version = ifp->if_version;
35052085Sbostic 	brelse(bp);
35151188Sbostic 
35252085Sbostic 	/*
35352085Sbostic 	 * It may not be necessary to write the meta-data blocks at this point,
35452085Sbostic 	 * as the roll-forward recovery code should be able to reconstruct the
35552085Sbostic 	 * list.
35652085Sbostic 	 */
35752085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_data);
35852085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_indir);
35952085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_dindir);
36051860Sbostic #ifdef TRIPLE
36152085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_tindir);
36251860Sbostic #endif
36351342Sbostic 
36452085Sbostic 	fip = sp->fip;
36551860Sbostic #ifdef META
36652085Sbostic 	printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks);
36751860Sbostic #endif
36852085Sbostic 	if (fip->fi_nblocks != 0) {
36952085Sbostic 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
37052085Sbostic 		sp->fip =
37152085Sbostic 		    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
37252085Sbostic 		    sizeof(daddr_t) * (fip->fi_nblocks - 1));
37355940Sbostic 		sp->start_lbp = &sp->fip->fi_blocks[0];
37452682Sstaelin 	} else
37552682Sstaelin 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
37651215Sbostic }
37751215Sbostic 
37854264Sbostic int
37951915Sbostic lfs_writeinode(fs, sp, ip)
38051915Sbostic 	struct lfs *fs;
38152085Sbostic 	struct segment *sp;
38252085Sbostic 	struct inode *ip;
38351915Sbostic {
38452085Sbostic 	struct buf *bp, *ibp;
38552077Sbostic 	IFILE *ifp;
38652682Sstaelin 	SEGUSE *sup;
38752682Sstaelin 	daddr_t daddr;
38852077Sbostic 	ino_t ino;
38955940Sbostic 	int error, ndx;
39054264Sbostic 	int redo_ifile = 0;
39151915Sbostic 
39255940Sbostic 	if (!(ip->i_flag & (IMOD | IACC | IUPD | ICHG)))
39355940Sbostic 		return;
39455940Sbostic 
39551915Sbostic 	/* Allocate a new inode block if necessary. */
39651915Sbostic 	if (sp->ibp == NULL) {
39751915Sbostic 		/* Allocate a new segment if necessary. */
39851915Sbostic 		if (sp->seg_bytes_left < fs->lfs_bsize ||
39951915Sbostic 		    sp->sum_bytes_left < sizeof(daddr_t)) {
40054264Sbostic 			(void) lfs_writeseg(fs, sp);
40151915Sbostic 			lfs_initseg(fs, sp);
40251915Sbostic 		}
40351915Sbostic 
40451915Sbostic 		/* Get next inode block. */
40552682Sstaelin 		daddr = fs->lfs_offset;
40651915Sbostic 		fs->lfs_offset += fsbtodb(fs, 1);
40751915Sbostic 		sp->ibp = *sp->cbpp++ =
40855940Sbostic 		    lfs_newbuf(fs->lfs_ivnode, daddr, fs->lfs_bsize);
40955940Sbostic 		++sp->start_bpp;
41055940Sbostic 		fs->lfs_avail -= fsbtodb(fs, 1);
41152688Sbostic 		/* Set remaining space counters. */
41251915Sbostic 		sp->seg_bytes_left -= fs->lfs_bsize;
41351915Sbostic 		sp->sum_bytes_left -= sizeof(daddr_t);
41452077Sbostic 		ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
41551915Sbostic 		    sp->ninodes / INOPB(fs) - 1;
41652682Sstaelin 		((daddr_t *)(sp->segsum))[ndx] = daddr;
41751915Sbostic 	}
41851915Sbostic 
41952085Sbostic 	/* Update the inode times and copy the inode onto the inode page. */
42052077Sbostic 	ITIMES(ip, &time, &time);
42155940Sbostic 	ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG);
42255940Sbostic 	--fs->lfs_uinodes;
42351915Sbostic 	bp = sp->ibp;
42452085Sbostic 	bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
42551915Sbostic 	/* Increment inode count in segment summary block. */
42651915Sbostic 	++((SEGSUM *)(sp->segsum))->ss_ninos;
42751915Sbostic 
42851915Sbostic 	/* If this page is full, set flag to allocate a new page. */
42951915Sbostic 	if (++sp->ninodes % INOPB(fs) == 0)
43051915Sbostic 		sp->ibp = NULL;
43151915Sbostic 
43251915Sbostic 	/*
43352077Sbostic 	 * If updating the ifile, update the super-block.  Update the disk
43452077Sbostic 	 * address and access times for this inode in the ifile.
43551915Sbostic 	 */
43652077Sbostic 	ino = ip->i_number;
43755696Sbostic 	if (ino == LFS_IFILE_INUM) {
43855696Sbostic 		daddr = fs->lfs_idaddr;
43951915Sbostic 		fs->lfs_idaddr = bp->b_blkno;
44055696Sbostic 	} else {
44155696Sbostic 		LFS_IENTRY(ifp, fs, ino, ibp);
44255696Sbostic 		daddr = ifp->if_daddr;
44355696Sbostic 		ifp->if_daddr = bp->b_blkno;
44455940Sbostic 		error = VOP_BWRITE(ibp);
44555696Sbostic 	}
44652077Sbostic 
44754264Sbostic 	/*
44854264Sbostic 	 * No need to update segment usage if there was no former inode address
44954264Sbostic 	 * or if the last inode address is in the current partial segment.
45054264Sbostic 	 */
45154264Sbostic 	if (daddr != LFS_UNUSED_DADDR &&
45255803Sbostic 	    !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
45352682Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
45452682Sstaelin #ifdef DIAGNOSTIC
45554264Sbostic 		if (sup->su_nbytes < sizeof(struct dinode)) {
45652819Sbostic 			/* XXX -- Change to a panic. */
45752819Sbostic 			printf("lfs: negative bytes (segment %d)\n",
45852682Sstaelin 			    datosn(fs, daddr));
45954264Sbostic 			panic("negative bytes");
46054264Sbostic 		}
46152682Sstaelin #endif
46252682Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
46355940Sbostic 		error = VOP_BWRITE(bp);
46455696Sbostic 		redo_ifile = (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
46552682Sstaelin 	}
46655551Sbostic 	return (redo_ifile);
46751915Sbostic }
46851915Sbostic 
46955940Sbostic int
47055940Sbostic lfs_gatherblock(sp, bp, sptr)
47155940Sbostic 	struct segment *sp;
47255940Sbostic 	struct buf *bp;
47355940Sbostic 	int *sptr;
47455940Sbostic {
47555940Sbostic 	struct lfs *fs;
47655940Sbostic 	int version;
47755940Sbostic 
47855940Sbostic 	/*
47955940Sbostic 	 * If full, finish this segment.  We may be doing I/O, so
48055940Sbostic 	 * release and reacquire the splbio().
48155940Sbostic 	 */
482*56027Sbostic #ifdef DIAGNOSTIC
483*56027Sbostic 	if (sp->vp == NULL)
484*56027Sbostic 		panic ("lfs_gatherblock: Null vp in segment");
485*56027Sbostic #endif
48655940Sbostic 	fs = sp->fs;
48755940Sbostic 	if (sp->sum_bytes_left < sizeof(daddr_t) ||
48855940Sbostic 	    sp->seg_bytes_left < fs->lfs_bsize) {
48955940Sbostic 		if (sptr)
49055940Sbostic 			splx(*sptr);
491*56027Sbostic 		lfs_updatemeta(sp);
49255940Sbostic 
49355940Sbostic 		/* Add the current file to the segment summary. */
49455940Sbostic 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
49555940Sbostic 
49655940Sbostic 		version = sp->fip->fi_version;
49755940Sbostic 		(void) lfs_writeseg(fs, sp);
49855940Sbostic 		lfs_initseg(fs, sp);
49955940Sbostic 
50055940Sbostic 		sp->fip->fi_version = version;
501*56027Sbostic 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
50255940Sbostic 
50355940Sbostic 		sp->sum_bytes_left -=
50455940Sbostic 		    sizeof(struct finfo) - sizeof(daddr_t);
50555940Sbostic 
50655940Sbostic 		if (sptr)
50755940Sbostic 			*sptr = splbio();
50855940Sbostic 		return(1);
50955940Sbostic 	}
51055940Sbostic 
51155940Sbostic 	/* Insert into the buffer list, update the FINFO block. */
51255940Sbostic 	bp->b_flags |= B_GATHERED;
51355940Sbostic 	*sp->cbpp++ = bp;
51455940Sbostic 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
51555940Sbostic 
51655940Sbostic 	sp->sum_bytes_left -= sizeof(daddr_t);
51755940Sbostic 	sp->seg_bytes_left -= bp->b_bufsize;
51855940Sbostic 	return(0);
51955940Sbostic }
52055940Sbostic 
52152077Sbostic void
52251215Sbostic lfs_gather(fs, sp, vp, match)
52351499Sbostic 	struct lfs *fs;
52452085Sbostic 	struct segment *sp;
52552085Sbostic 	struct vnode *vp;
52652085Sbostic 	int (*match) __P((struct lfs *, struct buf *));
52751215Sbostic {
52855940Sbostic 	struct buf *bp;
52951342Sbostic 	int s;
53051215Sbostic 
531*56027Sbostic 	sp->vp = vp;
53255940Sbostic 	s = splbio();
53355940Sbostic loop:	for (bp = vp->v_dirtyblkhd; bp; bp = bp->b_blockf) {
53454264Sbostic 		if (bp->b_flags & B_BUSY || !match(fs, bp) ||
53554264Sbostic 		    bp->b_flags & B_GATHERED)
53651215Sbostic 			continue;
53751342Sbostic #ifdef DIAGNOSTIC
53851860Sbostic 		if (!(bp->b_flags & B_DELWRI))
53951915Sbostic 			panic("lfs_gather: bp not B_DELWRI");
54051860Sbostic 		if (!(bp->b_flags & B_LOCKED))
54151915Sbostic 			panic("lfs_gather: bp not B_LOCKED");
54251342Sbostic #endif
54355940Sbostic 		if (lfs_gatherblock(sp, bp, &s))
54453145Sstaelin 			goto loop;
54551188Sbostic 	}
54651215Sbostic 	splx(s);
547*56027Sbostic 	lfs_updatemeta(sp);
548*56027Sbostic 	sp->vp = NULL;
54951188Sbostic }
55051188Sbostic 
55155940Sbostic 
55251342Sbostic /*
55351342Sbostic  * Update the metadata that points to the blocks listed in the FINFO
55451188Sbostic  * array.
55551188Sbostic  */
55652077Sbostic void
557*56027Sbostic lfs_updatemeta(sp)
55852085Sbostic 	struct segment *sp;
55951188Sbostic {
56051915Sbostic 	SEGUSE *sup;
56152085Sbostic 	struct buf *bp;
56255940Sbostic 	struct lfs *fs;
563*56027Sbostic 	struct vnode *vp;
56451860Sbostic 	INDIR a[NIADDR], *ap;
56552085Sbostic 	struct inode *ip;
56651915Sbostic 	daddr_t daddr, lbn, off;
56755940Sbostic 	int db_per_fsb, error, i, nblocks, num;
56851188Sbostic 
569*56027Sbostic 	vp = sp->vp;
57055940Sbostic 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
571*56027Sbostic 	if (vp == NULL || nblocks == 0)
57251215Sbostic 		return;
57351215Sbostic 
57451915Sbostic 	/* Sort the blocks. */
57555940Sbostic 	if (!(sp->seg_flags & SEGM_CLEAN))
57655940Sbostic 		lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
57751215Sbostic 
57851915Sbostic 	/*
57951915Sbostic 	 * Assign disk addresses, and update references to the logical
58051915Sbostic 	 * block and the segment usage information.
58151915Sbostic 	 */
58255940Sbostic 	fs = sp->fs;
58351860Sbostic 	db_per_fsb = fsbtodb(fs, 1);
58455940Sbostic 	for (i = nblocks; i--; ++sp->start_bpp) {
58555940Sbostic 		lbn = *sp->start_lbp++;
58655940Sbostic 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
58751860Sbostic 		fs->lfs_offset += db_per_fsb;
58851215Sbostic 
58951860Sbostic 		if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num))
59052085Sbostic 			panic("lfs_updatemeta: lfs_bmaparray %d", error);
59151860Sbostic 		ip = VTOI(vp);
59251860Sbostic 		switch (num) {
59351860Sbostic 		case 0:
59451915Sbostic 			ip->i_db[lbn] = off;
59551860Sbostic 			break;
59651860Sbostic 		case 1:
59751915Sbostic 			ip->i_ib[a[0].in_off] = off;
59851860Sbostic 			break;
59951860Sbostic 		default:
60051860Sbostic 			ap = &a[num - 1];
60151860Sbostic 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
60251860Sbostic 				panic("lfs_updatemeta: bread bno %d",
60351860Sbostic 				    ap->in_lbn);
60455458Sbostic 			/*
60555458Sbostic 			 * Bread may create a new indirect block which needs
60655458Sbostic 			 * to get counted for the inode.
60755458Sbostic 			 */
60855592Sbostic 			if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
60955940Sbostic printf ("Updatemeta allocating indirect block: shouldn't happen\n");
61055458Sbostic 				ip->i_blocks += btodb(fs->lfs_bsize);
61155592Sbostic 				fs->lfs_bfree -= btodb(fs->lfs_bsize);
61255592Sbostic 			}
61351915Sbostic 			bp->b_un.b_daddr[ap->in_off] = off;
61453530Sheideman 			VOP_BWRITE(bp);
61551188Sbostic 		}
61651915Sbostic 
61751915Sbostic 		/* Update segment usage information. */
61851915Sbostic 		if (daddr != UNASSIGNED) {
61951915Sbostic 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
62051915Sbostic #ifdef DIAGNOSTIC
62154264Sbostic 			if (sup->su_nbytes < fs->lfs_bsize) {
62252819Sbostic 				/* XXX -- Change to a panic. */
62352819Sbostic 				printf("lfs: negative bytes (segment %d)\n",
62451915Sbostic 				    datosn(fs, daddr));
62554264Sbostic 				panic ("Negative Bytes");
62654264Sbostic 			}
62751915Sbostic #endif
62851915Sbostic 			sup->su_nbytes -= fs->lfs_bsize;
62955940Sbostic 			error = VOP_BWRITE(bp);
63051915Sbostic 		}
63151188Sbostic 	}
63251188Sbostic }
63351188Sbostic 
63451915Sbostic /*
63551915Sbostic  * Start a new segment.
63651915Sbostic  */
63752077Sbostic void
63851915Sbostic lfs_initseg(fs, sp)
63951499Sbostic 	struct lfs *fs;
64052085Sbostic 	struct segment *sp;
64151188Sbostic {
64251915Sbostic 	SEGUSE *sup;
64351915Sbostic 	SEGSUM *ssp;
64451915Sbostic 	struct buf *bp;
64551915Sbostic 	daddr_t lbn, *lbnp;
64651215Sbostic 
64751915Sbostic 	/* Advance to the next segment. */
64851927Sbostic 	if (!LFS_PARTIAL_FITS(fs)) {
64952682Sstaelin 		/* Wake up any cleaning procs waiting on this file system. */
65052688Sbostic 		wakeup(&fs->lfs_nextseg);
65152688Sbostic 		wakeup(&lfs_allclean_wakeup);
65252682Sstaelin 
65351927Sbostic 		lfs_newseg(fs);
65451927Sbostic 		fs->lfs_offset = fs->lfs_curseg;
65551915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
65651915Sbostic 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
65751915Sbostic 
65851915Sbostic 		/*
65951927Sbostic 		 * If the segment contains a superblock, update the offset
66051927Sbostic 		 * and summary address to skip over it.
66151915Sbostic 		 */
66252077Sbostic 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
66351927Sbostic 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
66451915Sbostic 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
66551915Sbostic 			sp->seg_bytes_left -= LFS_SBPAD;
66651215Sbostic 		}
66752085Sbostic 		brelse(bp);
66851915Sbostic 	} else {
66951915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
67051915Sbostic 		sp->seg_bytes_left = (fs->lfs_dbpseg -
67151915Sbostic 		    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
67251915Sbostic 	}
67354264Sbostic 	fs->lfs_lastpseg = fs->lfs_offset;
67451342Sbostic 
67555940Sbostic 	sp->fs = fs;
67651915Sbostic 	sp->ibp = NULL;
67751915Sbostic 	sp->ninodes = 0;
67851342Sbostic 
67951915Sbostic 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
68051915Sbostic 	sp->cbpp = sp->bpp;
68155940Sbostic 	*sp->cbpp =
68255940Sbostic 	    lfs_newbuf(fs->lfs_ivnode, fs->lfs_offset, LFS_SUMMARY_SIZE);
68351915Sbostic 	sp->segsum = (*sp->cbpp)->b_un.b_addr;
68455940Sbostic 	sp->start_bpp = ++sp->cbpp;
68551915Sbostic 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
68651342Sbostic 
68751915Sbostic 	/* Set point to SEGSUM, initialize it. */
68851915Sbostic 	ssp = sp->segsum;
68951915Sbostic 	ssp->ss_next = fs->lfs_nextseg;
69051915Sbostic 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
69151342Sbostic 
69251915Sbostic 	/* Set pointer to first FINFO, initialize it. */
69352085Sbostic 	sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
69451915Sbostic 	sp->fip->fi_nblocks = 0;
69555940Sbostic 	sp->start_lbp = &sp->fip->fi_blocks[0];
69651342Sbostic 
69751915Sbostic 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
69851915Sbostic 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
69951915Sbostic }
70051342Sbostic 
70151915Sbostic /*
70251915Sbostic  * Return the next segment to write.
70351915Sbostic  */
70452077Sbostic void
70551915Sbostic lfs_newseg(fs)
70651915Sbostic 	struct lfs *fs;
70751915Sbostic {
70851927Sbostic 	CLEANERINFO *cip;
70951915Sbostic 	SEGUSE *sup;
71051915Sbostic 	struct buf *bp;
71155940Sbostic 	int curseg, error, isdirty, sn;
71251915Sbostic 
71355592Sbostic         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
71455592Sbostic         sup->su_flags |= SEGUSE_DIRTY;
71555940Sbostic         (void) VOP_BWRITE(bp);
71651927Sbostic 
71751927Sbostic 	LFS_CLEANERINFO(cip, fs, bp);
71851927Sbostic 	--cip->clean;
71951927Sbostic 	++cip->dirty;
72055940Sbostic 	(void) VOP_BWRITE(bp);
72151927Sbostic 
72251927Sbostic 	fs->lfs_lastseg = fs->lfs_curseg;
72351927Sbostic 	fs->lfs_curseg = fs->lfs_nextseg;
72451927Sbostic 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
72551915Sbostic 		sn = (sn + 1) % fs->lfs_nseg;
72651927Sbostic 		if (sn == curseg)
72751915Sbostic 			panic("lfs_nextseg: no clean segments");
72851915Sbostic 		LFS_SEGENTRY(sup, fs, sn, bp);
72951915Sbostic 		isdirty = sup->su_flags & SEGUSE_DIRTY;
73052085Sbostic 		brelse(bp);
73151915Sbostic 		if (!isdirty)
73251915Sbostic 			break;
73351915Sbostic 	}
73455592Sbostic 
73555940Sbostic 	++fs->lfs_nactive;
73651927Sbostic 	fs->lfs_nextseg = sntoda(fs, sn);
73751188Sbostic }
73851188Sbostic 
73954264Sbostic int
74051188Sbostic lfs_writeseg(fs, sp)
74151499Sbostic 	struct lfs *fs;
74252085Sbostic 	struct segment *sp;
74351188Sbostic {
74455940Sbostic 	extern int locked_queue_count;
74552688Sbostic 	struct buf **bpp, *bp, *cbp;
74651188Sbostic 	SEGUSE *sup;
74752085Sbostic 	SEGSUM *ssp;
74851860Sbostic 	dev_t i_dev;
74954264Sbostic 	size_t size;
75051860Sbostic 	u_long *datap, *dp;
75155940Sbostic 	int ch_per_blk, do_again, error, i, nblocks, num, s;
75254264Sbostic 	int (*strategy)__P((struct vop_strategy_args *));
75354690Sbostic 	struct vop_strategy_args vop_strategy_a;
75455592Sbostic 	u_short ninos;
75552688Sbostic 	char *p;
75651188Sbostic 
75755940Sbostic 	/*
75855940Sbostic 	 * If there are no buffers other than the segment summary to write
75955940Sbostic 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
76055940Sbostic 	 * even if there aren't any buffers, you need to write the superblock.
76155940Sbostic 	 */
76255940Sbostic 	if ((nblocks = sp->cbpp - sp->bpp) == 1 && !(sp->seg_flags & SEGM_CKP))
76355551Sbostic 		return (0);
76452085Sbostic 
76551188Sbostic 	/*
76652085Sbostic 	 * Compute checksum across data and then across summary; the first
76752085Sbostic 	 * block (the summary block) is skipped.  Set the create time here
76852085Sbostic 	 * so that it's guaranteed to be later than the inode mod times.
76951860Sbostic 	 *
77051860Sbostic 	 * XXX
77151860Sbostic 	 * Fix this to do it inline, instead of malloc/copy.
77251188Sbostic 	 */
77351860Sbostic 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
77451915Sbostic 	for (bpp = sp->bpp, i = nblocks - 1; i--;)
77551915Sbostic 		*dp++ = (*++bpp)->b_un.b_words[0];
77652085Sbostic 	ssp = (SEGSUM *)sp->segsum;
77752103Sbostic 	ssp->ss_create = time.tv_sec;
77855803Sbostic 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
77952085Sbostic 	ssp->ss_sumsum =
78052085Sbostic 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
78151927Sbostic 	free(datap, M_SEGMENT);
78251188Sbostic 
78354264Sbostic 	/* Update the segment usage information. */
78454264Sbostic 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
78555592Sbostic 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
78655592Sbostic 	sup->su_nbytes += nblocks - 1 - ninos << fs->lfs_bshift;
78754264Sbostic 	sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
78855803Sbostic 	sup->su_nbytes += LFS_SUMMARY_SIZE;
78954264Sbostic 	sup->su_lastmod = time.tv_sec;
79055592Sbostic 	sup->su_flags |= SEGUSE_ACTIVE;
79155592Sbostic 	sup->su_ninos += ninos;
79255592Sbostic 	++sup->su_nsums;
79355940Sbostic 	(void)VOP_BWRITE(bp);
79455592Sbostic 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
79554264Sbostic 	do_again = !(bp->b_flags & B_GATHERED);
79654264Sbostic 
79751860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
79853574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
79951301Sbostic 
80052688Sbostic 	/*
80152688Sbostic 	 * When we simply write the blocks we lose a rotation for every block
80252688Sbostic 	 * written.  To avoid this problem, we allocate memory in chunks, copy
80352688Sbostic 	 * the buffers into the chunk and write the chunk.  56K was chosen as
80452688Sbostic 	 * some driver/controllers can't handle unsigned 16 bit transfers.
80552688Sbostic 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
80652688Sbostic 	 * and brelse the buffer (which will move them to the LRU list).  Add
80752688Sbostic 	 * the B_CALL flag to the buffer header so we can count I/O's for the
80852688Sbostic 	 * checkpoints and so we can release the allocated memory.
80952688Sbostic 	 *
81052688Sbostic 	 * XXX
81152688Sbostic 	 * This should be removed if the new virtual memory system allows us to
81252688Sbostic 	 * easily make the buffers contiguous in kernel memory and if that's
81352688Sbostic 	 * fast enough.
81452688Sbostic 	 */
81552688Sbostic #define	LFS_CHUNKSIZE	(56 * 1024)
81652688Sbostic 	ch_per_blk = LFS_CHUNKSIZE / fs->lfs_bsize;
81752688Sbostic 	for (bpp = sp->bpp, i = nblocks; i;) {
81852688Sbostic 		num = ch_per_blk;
81952688Sbostic 		if (num > i)
82052688Sbostic 			num = i;
82152688Sbostic 		i -= num;
82252688Sbostic 		size = num * fs->lfs_bsize;
82352688Sbostic 
82455940Sbostic 		cbp = lfs_newbuf(fs->lfs_ivnode, (*bpp)->b_blkno, size);
82552688Sbostic 		cbp->b_dev = i_dev;
82655940Sbostic 		cbp->b_flags |= B_ASYNC | B_BUSY;
82752688Sbostic 
82852688Sbostic 		s = splbio();
82952688Sbostic 		++fs->lfs_iocount;
83052688Sbostic 		for (p = cbp->b_un.b_addr; num--;) {
83152688Sbostic 			bp = *bpp++;
83255940Sbostic 			/*
83355940Sbostic 			 * Fake buffers from the cleaner are marked as B_INVAL.
83455940Sbostic 			 * We need to copy the data from user space rather than
83555940Sbostic 			 * from the buffer indicated.
83655940Sbostic 			 * XXX == what do I do on an error?
83755940Sbostic 			 */
83855940Sbostic 			if (bp->b_flags & B_INVAL) {
83955940Sbostic 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
84055940Sbostic 					panic("lfs_writeseg: copyin failed");
84155940Sbostic 			} else
84255940Sbostic 				bcopy(bp->b_un.b_addr, p, bp->b_bcount);
84352688Sbostic 			p += bp->b_bcount;
84455940Sbostic 			if (bp->b_flags & B_LOCKED)
84555940Sbostic 				--locked_queue_count;
84655940Sbostic 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
84754264Sbostic 			     B_LOCKED | B_GATHERED);
84855940Sbostic 			if (bp->b_flags & B_CALL) {
84955940Sbostic 				/* if B_CALL, it was created with newbuf */
85055940Sbostic 				brelvp(bp);
85155940Sbostic 				free(bp, M_SEGMENT);
85255940Sbostic 			} else {
85352688Sbostic 				bremfree(bp);
85452688Sbostic 				reassignbuf(bp, bp->b_vp);
85555940Sbostic 				brelse(bp);
85652688Sbostic 			}
85751860Sbostic 		}
85852688Sbostic 		splx(s);
85952688Sbostic 		cbp->b_bcount = p - cbp->b_un.b_addr;
86053574Sheideman 		vop_strategy_a.a_desc = VDESC(vop_strategy);
86153574Sheideman 		vop_strategy_a.a_bp = cbp;
86253574Sheideman 		(strategy)(&vop_strategy_a);
86351860Sbostic 	}
86455551Sbostic 	return (do_again);
86551188Sbostic }
86651188Sbostic 
86752077Sbostic void
86851860Sbostic lfs_writesuper(fs, sp)
86951499Sbostic 	struct lfs *fs;
87052085Sbostic 	struct segment *sp;
87151301Sbostic {
87252085Sbostic 	struct buf *bp;
87351860Sbostic 	dev_t i_dev;
87453574Sheideman 	int (*strategy) __P((struct vop_strategy_args *));
87554690Sbostic 	struct vop_strategy_args vop_strategy_a;
87651301Sbostic 
87751860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
87853574Sheideman 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
87951356Sbostic 
88051342Sbostic 	/* Checksum the superblock and copy it into a buffer. */
88151499Sbostic 	fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
88255940Sbostic 	bp = lfs_newbuf(fs->lfs_ivnode, fs->lfs_sboffs[0], LFS_SBPAD);
88351860Sbostic 	*bp->b_un.b_lfs = *fs;
88451215Sbostic 
88551356Sbostic 	/* Write the first superblock (wait). */
88651860Sbostic 	bp->b_dev = i_dev;
88751915Sbostic 	bp->b_flags |= B_BUSY;
88855940Sbostic 	bp->b_flags &= ~(B_DONE | B_CALL | B_ERROR | B_READ | B_DELWRI);
88953574Sheideman 	vop_strategy_a.a_desc = VDESC(vop_strategy);
89053574Sheideman 	vop_strategy_a.a_bp = bp;
89153574Sheideman 	(strategy)(&vop_strategy_a);
89251215Sbostic 	biowait(bp);
89351342Sbostic 
89451356Sbostic 	/* Write the second superblock (don't wait). */
89551215Sbostic 	bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1];
89655940Sbostic 	bp->b_flags |= B_CALL | B_ASYNC | B_BUSY;
89751860Sbostic 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
89855940Sbostic 	bp->b_iodone = lfs_supercallback;
89953574Sheideman 	(strategy)(&vop_strategy_a);
90051215Sbostic }
90151215Sbostic 
90251342Sbostic /*
90351342Sbostic  * Logical block number match routines used when traversing the dirty block
90451342Sbostic  * chain.
90551342Sbostic  */
90652077Sbostic int
90752077Sbostic lfs_match_data(fs, bp)
90851860Sbostic 	struct lfs *fs;
90952085Sbostic 	struct buf *bp;
91051215Sbostic {
91151342Sbostic 	return (bp->b_lblkno >= 0);
91251215Sbostic }
91351215Sbostic 
91452077Sbostic int
91552077Sbostic lfs_match_indir(fs, bp)
91651860Sbostic 	struct lfs *fs;
91752085Sbostic 	struct buf *bp;
91851215Sbostic {
91951860Sbostic 	int lbn;
92051860Sbostic 
92151860Sbostic 	lbn = bp->b_lblkno;
92251860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
92351215Sbostic }
92451215Sbostic 
92552077Sbostic int
92652077Sbostic lfs_match_dindir(fs, bp)
92751860Sbostic 	struct lfs *fs;
92852085Sbostic 	struct buf *bp;
92951215Sbostic {
93051860Sbostic 	int lbn;
93151860Sbostic 
93251860Sbostic 	lbn = bp->b_lblkno;
93351860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
93451215Sbostic }
93551215Sbostic 
93652077Sbostic int
93752077Sbostic lfs_match_tindir(fs, bp)
93851499Sbostic 	struct lfs *fs;
93952085Sbostic 	struct buf *bp;
94051342Sbostic {
94151860Sbostic 	int lbn;
94251342Sbostic 
94351860Sbostic 	lbn = bp->b_lblkno;
94451860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
94551860Sbostic }
94651342Sbostic 
94751860Sbostic /*
94851860Sbostic  * Allocate a new buffer header.
94951860Sbostic  */
95052085Sbostic struct buf *
95155940Sbostic lfs_newbuf(vp, daddr, size)
95255940Sbostic 	struct vnode *vp;
95351860Sbostic 	daddr_t daddr;
95451860Sbostic 	size_t size;
95551860Sbostic {
95652085Sbostic 	struct buf *bp;
95755940Sbostic 	size_t nbytes;
95851342Sbostic 
95955940Sbostic 	nbytes = roundup(size, DEV_BSIZE);
96055940Sbostic 	bp = malloc(sizeof(struct buf) + nbytes, M_SEGMENT, M_WAITOK);
96155940Sbostic 	bzero(bp, sizeof(struct buf));
96255940Sbostic 	bgetvp(vp, bp);
96355940Sbostic 	bp->b_un.b_addr = (caddr_t)(bp + 1);
96455940Sbostic 	bp->b_bufsize = size;
96555940Sbostic 	bp->b_bcount = size;
96651860Sbostic 	bp->b_lblkno = daddr;
96751860Sbostic 	bp->b_blkno = daddr;
96851860Sbostic 	bp->b_error = 0;
96951860Sbostic 	bp->b_resid = 0;
97055940Sbostic 	bp->b_iodone = lfs_callback;
971*56027Sbostic 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
97251860Sbostic 	return (bp);
97351860Sbostic }
97451342Sbostic 
97553347Sbostic void
97651860Sbostic lfs_callback(bp)
97752085Sbostic 	struct buf *bp;
97851860Sbostic {
97951860Sbostic 	struct lfs *fs;
98051342Sbostic 
98151860Sbostic 	fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
98251860Sbostic #ifdef DIAGNOSTIC
98351860Sbostic 	if (fs->lfs_iocount == 0)
98451860Sbostic 		panic("lfs_callback: zero iocount\n");
98551860Sbostic #endif
98651860Sbostic 	if (--fs->lfs_iocount == 0)
98752688Sbostic 		wakeup(&fs->lfs_iocount);
98851915Sbostic 
98955940Sbostic 	brelvp(bp);
99055940Sbostic 	free(bp, M_SEGMENT);
99151860Sbostic }
99251342Sbostic 
99355940Sbostic void
99455940Sbostic lfs_supercallback(bp)
99555940Sbostic 	struct buf *bp;
99655940Sbostic {
99755940Sbostic 	brelvp(bp);
99855940Sbostic 	free(bp, M_SEGMENT);
99955940Sbostic }
100055940Sbostic 
100151215Sbostic /*
100251188Sbostic  * Shellsort (diminishing increment sort) from Data Structures and
100351188Sbostic  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
100451188Sbostic  * see also Knuth Vol. 3, page 84.  The increments are selected from
100551188Sbostic  * formula (8), page 95.  Roughly O(N^3/2).
100651188Sbostic  */
100751188Sbostic /*
100851188Sbostic  * This is our own private copy of shellsort because we want to sort
100951188Sbostic  * two parallel arrays (the array of buffer pointers and the array of
101051188Sbostic  * logical block numbers) simultaneously.  Note that we cast the array
101151188Sbostic  * of logical block numbers to a unsigned in this routine so that the
101251188Sbostic  * negative block numbers (meta data blocks) sort AFTER the data blocks.
101351188Sbostic  */
101452077Sbostic void
101552077Sbostic lfs_shellsort(bp_array, lb_array, nmemb)
101652085Sbostic 	struct buf **bp_array;
101751215Sbostic 	daddr_t *lb_array;
101851188Sbostic 	register int nmemb;
101951188Sbostic {
102051188Sbostic 	static int __rsshell_increments[] = { 4, 1, 0 };
102151188Sbostic 	register int incr, *incrp, t1, t2;
102252085Sbostic 	struct buf *bp_temp;
102351188Sbostic 	u_long lb_temp;
102451188Sbostic 
102551188Sbostic 	for (incrp = __rsshell_increments; incr = *incrp++;)
102651188Sbostic 		for (t1 = incr; t1 < nmemb; ++t1)
102751188Sbostic 			for (t2 = t1 - incr; t2 >= 0;)
102851188Sbostic 				if (lb_array[t2] > lb_array[t2 + incr]) {
102951188Sbostic 					lb_temp = lb_array[t2];
103051188Sbostic 					lb_array[t2] = lb_array[t2 + incr];
103151188Sbostic 					lb_array[t2 + incr] = lb_temp;
103251188Sbostic 					bp_temp = bp_array[t2];
103351188Sbostic 					bp_array[t2] = bp_array[t2 + incr];
103451188Sbostic 					bp_array[t2 + incr] = bp_temp;
103551188Sbostic 					t2 -= incr;
103651188Sbostic 				} else
103751188Sbostic 					break;
103851188Sbostic }
103955940Sbostic 
1040