xref: /csrg-svn/sys/ufs/lfs/lfs_segment.c (revision 53530)
151188Sbostic /*
251188Sbostic  * Copyright (c) 1991 Regents of the University of California.
351188Sbostic  * All rights reserved.
451188Sbostic  *
551188Sbostic  * %sccs.include.redist.c%
651188Sbostic  *
7*53530Sheideman  *	@(#)lfs_segment.c	7.18 (Berkeley) 05/14/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/specdev.h>
2251490Sbostic #include <sys/fifo.h>
2351490Sbostic #include <sys/malloc.h>
2451490Sbostic #include <sys/mount.h>
2551188Sbostic 
2651499Sbostic #include <ufs/ufs/quota.h>
2751499Sbostic #include <ufs/ufs/inode.h>
2851499Sbostic #include <ufs/ufs/dir.h>
2951499Sbostic #include <ufs/ufs/ufsmount.h>
3051490Sbostic 
3151499Sbostic #include <ufs/lfs/lfs.h>
3251499Sbostic #include <ufs/lfs/lfs_extern.h>
3351490Sbostic 
3451860Sbostic /* In-memory description of a segment about to be written. */
3551860Sbostic struct segment {
3652085Sbostic 	struct buf	**bpp;		/* pointer to buffer array */
3752085Sbostic 	struct buf	**cbpp;		/* pointer to next available bp */
3852085Sbostic 	struct buf	*ibp;		/* buffer pointer to inode page */
3952085Sbostic 	struct finfo	*fip;		/* current fileinfo pointer */
4051860Sbostic 	void	*segsum;		/* segment summary info */
4151860Sbostic 	u_long	ninodes;		/* number of inodes in this segment */
4251860Sbostic 	u_long	seg_bytes_left;		/* bytes left in segment */
4351860Sbostic 	u_long	sum_bytes_left;		/* bytes left in summary block */
4451860Sbostic 	u_long	seg_number;		/* number of this segment */
4551860Sbostic #define	SEGM_CKP	0x01		/* doing a checkpoint */
4651860Sbostic 	u_long	seg_flags;		/* run-time flags for this segment */
4751860Sbostic };
4851860Sbostic 
4951188Sbostic /*
5051860Sbostic  * Determine if it's OK to start a partial in this segment, or if we need
5151860Sbostic  * to go on to a new segment.
5251301Sbostic  */
5351860Sbostic #define	LFS_PARTIAL_FITS(fs) \
5451860Sbostic 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
5551860Sbostic 	1 << (fs)->lfs_fsbtodb)
5651188Sbostic 
5753347Sbostic void	 lfs_callback __P((struct buf *));
5852085Sbostic void	 lfs_gather __P((struct lfs *, struct segment *,
5952085Sbostic 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
6052085Sbostic void	 lfs_initseg __P((struct lfs *, struct segment *));
6152085Sbostic void	 lfs_iset __P((struct inode *, daddr_t, time_t));
6252085Sbostic int	 lfs_match_data __P((struct lfs *, struct buf *));
6352085Sbostic int	 lfs_match_dindir __P((struct lfs *, struct buf *));
6452085Sbostic int	 lfs_match_indir __P((struct lfs *, struct buf *));
6552085Sbostic int	 lfs_match_tindir __P((struct lfs *, struct buf *));
6652085Sbostic struct buf *
6752688Sbostic 	 lfs_newbuf __P((struct lfs *, daddr_t, size_t));
6852077Sbostic void	 lfs_newseg __P((struct lfs *));
6952085Sbostic void	 lfs_shellsort __P((struct buf **, daddr_t *, register int));
7052077Sbostic void	 lfs_updatemeta __P((struct lfs *,
7152085Sbostic 	    struct segment *, struct vnode *, daddr_t *, struct buf **, int));
7252085Sbostic void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
7352085Sbostic void	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
7452085Sbostic void	 lfs_writeseg __P((struct lfs *, struct segment *));
7552085Sbostic void	 lfs_writesuper __P((struct lfs *, struct segment *));
7651188Sbostic 
7751860Sbostic int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
7851860Sbostic 
7952328Sbostic /*
8052328Sbostic  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
8152328Sbostic  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
8252328Sbostic  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
8352328Sbostic  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
8452328Sbostic  */
8552328Sbostic 
8651188Sbostic int
8752328Sbostic lfs_vflush(vp)
8852328Sbostic 	struct vnode *vp;
8952328Sbostic {
9052328Sbostic 	struct inode *ip;
9152328Sbostic 	struct lfs *fs;
9252328Sbostic 	struct mount *mp;
9352328Sbostic 	struct segment *sp;
9452328Sbostic 	int error, s;
9552328Sbostic 
9652328Sbostic #ifdef VERBOSE
9752328Sbostic 	printf("lfs_vflush\n");
9852328Sbostic #endif
9952328Sbostic 	mp = vp->v_mount;
10052328Sbostic 	fs = VFSTOUFS(mp)->um_lfs;
10152328Sbostic 
10252328Sbostic 	/*
10352328Sbostic 	 * XXX
10452328Sbostic 	 * check flags?
10552328Sbostic 	 * mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY) ||
10652328Sbostic 	 */
10752328Sbostic 	if (vfs_busy(mp))
10852328Sbostic 		return (0);
10952328Sbostic 
11052328Sbostic 	/*
11152328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
11252328Sbostic 	 * the maximum possible number of buffers which can be described in a
11352328Sbostic 	 * single summary block.
11452328Sbostic 	 */
11552328Sbostic 	sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
11652328Sbostic 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
11752328Sbostic 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
11852328Sbostic 	sp->seg_flags = SEGM_CKP;
11952328Sbostic 	lfs_initseg(fs, sp);
12052328Sbostic 
12152328Sbostic 	/*
12252328Sbostic 	 * Keep a cumulative count of the outstanding I/O operations.  If the
12352328Sbostic 	 * disk drive catches up with us it could go to zero before we finish,
12452328Sbostic 	 * so we artificially increment it by one until we've scheduled all of
12552328Sbostic 	 * the writes we intend to do.
12652328Sbostic 	 */
12752328Sbostic 	s = splbio();
12852688Sbostic 	++fs->lfs_iocount;
12952328Sbostic 	splx(s);
13052328Sbostic 
13152328Sbostic 	if (vp->v_dirtyblkhd != NULL)
13252328Sbostic 		lfs_writefile(fs, sp, vp);
13352328Sbostic 	ip = VTOI(vp);
13452328Sbostic 	lfs_writeinode(fs, sp, ip);
13552328Sbostic 	ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
13652328Sbostic 
13752328Sbostic 	lfs_writeseg(fs, sp);
13852328Sbostic 
13952328Sbostic 	/*
14052328Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
14152328Sbostic 	 * moment, the user's process hangs around so we can sleep.
14252328Sbostic 	 */
14352328Sbostic 	s = splbio();
14452328Sbostic 	if (--fs->lfs_iocount && (error =
14552995Sbostic 	    tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0))) {
14652995Sbostic 		free(sp->bpp, M_SEGMENT);
14752995Sbostic 		free(sp, M_SEGMENT);
14852328Sbostic 		return (error);
14952995Sbostic 	}
15052328Sbostic 	splx(s);
15152328Sbostic 	vfs_unbusy(mp);
15252328Sbostic 
15352995Sbostic 	/*
15452995Sbostic 	 * XXX
15552995Sbostic 	 * Should be writing a checkpoint?
15652995Sbostic 	 */
15752328Sbostic 	free(sp->bpp, M_SEGMENT);
15852328Sbostic 	free(sp, M_SEGMENT);
15952328Sbostic 
16052328Sbostic 	return (0);
16152328Sbostic }
16252328Sbostic 
16352328Sbostic int
16451215Sbostic lfs_segwrite(mp, do_ckp)
16552085Sbostic 	struct mount *mp;
16651860Sbostic 	int do_ckp;			/* Do a checkpoint. */
16751188Sbostic {
168*53530Sheideman 	USES_VOP_ISLOCKED;
16952085Sbostic 	struct inode *ip;
17051499Sbostic 	struct lfs *fs;
17152085Sbostic 	struct segment *sp;
17252085Sbostic 	struct vnode *vp;
17352328Sbostic 	int error, islocked, s;
17451188Sbostic 
17551860Sbostic #ifdef VERBOSE
17651860Sbostic 	printf("lfs_segwrite\n");
17751860Sbostic #endif
17852328Sbostic 	fs = VFSTOUFS(mp)->um_lfs;
17952085Sbostic 
18051860Sbostic 	/*
18152328Sbostic 	 * Allocate a segment structure and enough space to hold pointers to
18252328Sbostic 	 * the maximum possible number of buffers which can be described in a
18352328Sbostic 	 * single summary block.
18452328Sbostic 	 */
18552328Sbostic 	sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
18652328Sbostic 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
18752328Sbostic 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
18852328Sbostic 	sp->seg_flags = do_ckp ? SEGM_CKP : 0;
18952328Sbostic 	lfs_initseg(fs, sp);
19052328Sbostic 
19152328Sbostic 	/*
19252688Sbostic 	 * Keep a cumulative count of the outstanding I/O operations.  If the
19352688Sbostic 	 * disk drive catches up with us it could go to zero before we finish,
19452688Sbostic 	 * so we artificially increment it by one until we've scheduled all of
19552688Sbostic 	 * the writes we intend to do.  If not a checkpoint, we never do the
19652688Sbostic 	 * final decrement, avoiding the wakeup in the callback routine.
19751860Sbostic 	 */
19852688Sbostic 	s = splbio();
19952688Sbostic 	++fs->lfs_iocount;
20052688Sbostic 	splx(s);
20151342Sbostic 
20252328Sbostic loop:	for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
20351188Sbostic 		/*
20451188Sbostic 		 * If the vnode that we are about to sync is no longer
20551188Sbostic 		 * associated with this mount point, start over.
20651188Sbostic 		 */
20751188Sbostic 		if (vp->v_mount != mp)
20851188Sbostic 			goto loop;
20951860Sbostic 
21052328Sbostic 		islocked = VOP_ISLOCKED(vp);
21151860Sbostic 
21252077Sbostic 		/*
21352077Sbostic 		 * XXX
21452077Sbostic 		 * This is wrong, I think -- we should just wait until we
21552077Sbostic 		 * get the vnode and go on.  Probably going to reschedule
21652077Sbostic 		 * all of the writes we already scheduled...
21752077Sbostic 		 */
21852328Sbostic 		if (islocked)
21952328Sbostic 			VREF(vp);
22052328Sbostic 		else if (vget(vp))
22152085Sbostic {
22252085Sbostic printf("lfs_segment: failed to get vnode (tell Keith)!\n");
22351188Sbostic 			goto loop;
22452085Sbostic }
22552328Sbostic 		/*
22652328Sbostic 		 * Write the inode/file if dirty and it's not the
22752328Sbostic 		 * the IFILE.
22852328Sbostic 		 */
22952328Sbostic 		ip = VTOI(vp);
23052328Sbostic 		if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) ||
23152328Sbostic 		    vp->v_dirtyblkhd != NULL) &&
23252328Sbostic 		    ip->i_number != LFS_IFILE_INUM) {
23352328Sbostic 			if (vp->v_dirtyblkhd != NULL)
23452328Sbostic 				lfs_writefile(fs, sp, vp);
23552328Sbostic 			lfs_writeinode(fs, sp, ip);
23652328Sbostic 			ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
23752328Sbostic 		}
23852328Sbostic 		if (islocked)
23952328Sbostic 			vrele(vp);
24052328Sbostic 		else
24152328Sbostic 			vput(vp);
24251188Sbostic 	}
24351860Sbostic 	if (do_ckp) {
24452077Sbostic 		vp = fs->lfs_ivnode;
24552077Sbostic 		while (vget(vp));
24652077Sbostic 		ip = VTOI(vp);
24752077Sbostic 		if (vp->v_dirtyblkhd != NULL)
24852077Sbostic 			lfs_writefile(fs, sp, vp);
24952077Sbostic 		lfs_writeinode(fs, sp, ip);
25052077Sbostic 		ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
25152077Sbostic 		vput(vp);
25251860Sbostic 	}
25351342Sbostic 	lfs_writeseg(fs, sp);
25451342Sbostic 
25551215Sbostic 	/*
25651860Sbostic 	 * If the I/O count is non-zero, sleep until it reaches zero.  At the
25751860Sbostic 	 * moment, the user's process hangs around so we can sleep.
25851215Sbostic 	 */
25952688Sbostic 	s = splbio();
26052688Sbostic 	--fs->lfs_iocount;
26151860Sbostic 	if (do_ckp) {
26252688Sbostic 		if (fs->lfs_iocount && (error =
26352995Sbostic 		    tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs sync", 0))) {
26452995Sbostic 			free(sp->bpp, M_SEGMENT);
26552995Sbostic 			free(sp, M_SEGMENT);
26651915Sbostic 			return (error);
26752995Sbostic 		}
26851860Sbostic 		splx(s);
26951860Sbostic 		lfs_writesuper(fs, sp);
27052688Sbostic 	} else
27152688Sbostic 		splx(s);
27251215Sbostic 
27351927Sbostic 	free(sp->bpp, M_SEGMENT);
27451927Sbostic 	free(sp, M_SEGMENT);
27551215Sbostic 
27651860Sbostic 	return (0);
27751188Sbostic }
27851188Sbostic 
27951860Sbostic /*
28051860Sbostic  * Write the dirty blocks associated with a vnode.
28151860Sbostic  */
28252077Sbostic void
28351860Sbostic lfs_writefile(fs, sp, vp)
28451499Sbostic 	struct lfs *fs;
28552085Sbostic 	struct segment *sp;
28652085Sbostic 	struct vnode *vp;
28751188Sbostic {
28851860Sbostic 	struct buf *bp;
28952085Sbostic 	struct finfo *fip;
29051860Sbostic 	IFILE *ifp;
29151188Sbostic 
29251860Sbostic #ifdef VERBOSE
29351860Sbostic 	printf("lfs_writefile\n");
29451860Sbostic #endif
29552085Sbostic 	if (sp->seg_bytes_left < fs->lfs_bsize ||
29652085Sbostic 	    sp->sum_bytes_left < sizeof(struct finfo)) {
29752085Sbostic 		lfs_writeseg(fs, sp);
29852085Sbostic 		lfs_initseg(fs, sp);
29952085Sbostic 	}
30052085Sbostic 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
30151215Sbostic 
30252085Sbostic 	fip = sp->fip;
30352085Sbostic 	fip->fi_nblocks = 0;
30452085Sbostic 	fip->fi_ino = VTOI(vp)->i_number;
30552085Sbostic 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
30652085Sbostic 	fip->fi_version = ifp->if_version;
30752085Sbostic 	brelse(bp);
30851188Sbostic 
30952085Sbostic 	/*
31052085Sbostic 	 * It may not be necessary to write the meta-data blocks at this point,
31152085Sbostic 	 * as the roll-forward recovery code should be able to reconstruct the
31252085Sbostic 	 * list.
31352085Sbostic 	 */
31452085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_data);
31552085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_indir);
31652085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_dindir);
31751860Sbostic #ifdef TRIPLE
31852085Sbostic 	lfs_gather(fs, sp, vp, lfs_match_tindir);
31951860Sbostic #endif
32051342Sbostic 
32152085Sbostic 	fip = sp->fip;
32251860Sbostic #ifdef META
32352085Sbostic 	printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks);
32451860Sbostic #endif
32552085Sbostic 	if (fip->fi_nblocks != 0) {
32652085Sbostic 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
32752085Sbostic 		sp->fip =
32852085Sbostic 		    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
32952085Sbostic 		    sizeof(daddr_t) * (fip->fi_nblocks - 1));
33052682Sstaelin 	} else
33152682Sstaelin 		sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
33251215Sbostic }
33351215Sbostic 
33452077Sbostic void
33551915Sbostic lfs_writeinode(fs, sp, ip)
33651915Sbostic 	struct lfs *fs;
33752085Sbostic 	struct segment *sp;
33852085Sbostic 	struct inode *ip;
33951915Sbostic {
34052085Sbostic 	struct buf *bp, *ibp;
34152077Sbostic 	IFILE *ifp;
34252682Sstaelin 	SEGUSE *sup;
34352682Sstaelin 	daddr_t daddr;
34452077Sbostic 	ino_t ino;
34551915Sbostic 	int ndx;
34651915Sbostic 
34751915Sbostic #ifdef VERBOSE
34851915Sbostic 	printf("lfs_writeinode\n");
34951915Sbostic #endif
35051915Sbostic 	/* Allocate a new inode block if necessary. */
35151915Sbostic 	if (sp->ibp == NULL) {
35251915Sbostic 		/* Allocate a new segment if necessary. */
35351915Sbostic 		if (sp->seg_bytes_left < fs->lfs_bsize ||
35451915Sbostic 		    sp->sum_bytes_left < sizeof(daddr_t)) {
35551915Sbostic 			lfs_writeseg(fs, sp);
35651915Sbostic 			lfs_initseg(fs, sp);
35751915Sbostic 		}
35851915Sbostic 
35951915Sbostic 		/* Get next inode block. */
36052682Sstaelin 		daddr = fs->lfs_offset;
36151915Sbostic 		fs->lfs_offset += fsbtodb(fs, 1);
36251915Sbostic 		sp->ibp = *sp->cbpp++ =
36352688Sbostic 		    lfs_newbuf(fs, daddr, fs->lfs_bsize);
36451915Sbostic 
36552688Sbostic 		/* Set remaining space counters. */
36651915Sbostic 		sp->seg_bytes_left -= fs->lfs_bsize;
36751915Sbostic 		sp->sum_bytes_left -= sizeof(daddr_t);
36852077Sbostic 		ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
36951915Sbostic 		    sp->ninodes / INOPB(fs) - 1;
37052682Sstaelin 		((daddr_t *)(sp->segsum))[ndx] = daddr;
37151915Sbostic 	}
37251915Sbostic 
37352085Sbostic 	/* Update the inode times and copy the inode onto the inode page. */
37452077Sbostic 	ITIMES(ip, &time, &time);
37551915Sbostic 	bp = sp->ibp;
37652085Sbostic 	bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
37751915Sbostic 
37851915Sbostic 	/* Increment inode count in segment summary block. */
37951915Sbostic 	++((SEGSUM *)(sp->segsum))->ss_ninos;
38051915Sbostic 
38151915Sbostic 	/* If this page is full, set flag to allocate a new page. */
38251915Sbostic 	if (++sp->ninodes % INOPB(fs) == 0)
38351915Sbostic 		sp->ibp = NULL;
38451915Sbostic 
38551915Sbostic 	/*
38652077Sbostic 	 * If updating the ifile, update the super-block.  Update the disk
38752077Sbostic 	 * address and access times for this inode in the ifile.
38851915Sbostic 	 */
38952077Sbostic 	ino = ip->i_number;
39052077Sbostic 	if (ino == LFS_IFILE_INUM)
39151915Sbostic 		fs->lfs_idaddr = bp->b_blkno;
39252077Sbostic 
39352077Sbostic 	LFS_IENTRY(ifp, fs, ino, ibp);
39452682Sstaelin 	daddr = ifp->if_daddr;
39552077Sbostic 	ifp->if_daddr = bp->b_blkno;
39652085Sbostic 	LFS_UBWRITE(ibp);
39752682Sstaelin 
39852682Sstaelin 	if (daddr != LFS_UNUSED_DADDR) {
39952682Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
40052682Sstaelin #ifdef DIAGNOSTIC
40152682Sstaelin 		if (sup->su_nbytes < sizeof(struct dinode))
40252819Sbostic 			/* XXX -- Change to a panic. */
40352819Sbostic 			printf("lfs: negative bytes (segment %d)\n",
40452682Sstaelin 			    datosn(fs, daddr));
40552682Sstaelin #endif
40652682Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
40752682Sstaelin 		LFS_UBWRITE(bp);
40852682Sstaelin 	}
40951915Sbostic }
41051915Sbostic 
41152077Sbostic void
41251215Sbostic lfs_gather(fs, sp, vp, match)
41351499Sbostic 	struct lfs *fs;
41452085Sbostic 	struct segment *sp;
41552085Sbostic 	struct vnode *vp;
41652085Sbostic 	int (*match) __P((struct lfs *, struct buf *));
41751215Sbostic {
41852085Sbostic 	struct buf **bpp, *bp, *nbp;
41952085Sbostic 	struct finfo *fip;
42052085Sbostic 	struct inode *ip;
42151215Sbostic 	daddr_t *lbp, *start_lbp;
42251342Sbostic 	u_long version;
42351342Sbostic 	int s;
42451215Sbostic 
42551860Sbostic #ifdef VERBOSE
42651860Sbostic 	printf("lfs_gather\n");
42751860Sbostic #endif
42851215Sbostic 	ip = VTOI(vp);
42951215Sbostic 	bpp = sp->cbpp;
43051215Sbostic 	fip = sp->fip;
43151215Sbostic 	start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks];
43251215Sbostic 
43353145Sstaelin loop:	s = splbio();
43451215Sbostic 	for (bp = vp->v_dirtyblkhd; bp; bp = nbp) {
43551215Sbostic 		nbp = bp->b_blockf;
43651915Sbostic 		/*
43751915Sbostic 		 * XXX
43852682Sstaelin 		 * Should sleep on any BUSY buffer if doing an fsync?
43951915Sbostic 		 */
44052328Sbostic 		if (bp->b_flags & B_BUSY || !match(fs, bp))
44151215Sbostic 			continue;
44251342Sbostic #ifdef DIAGNOSTIC
44351860Sbostic 		if (!(bp->b_flags & B_DELWRI))
44451915Sbostic 			panic("lfs_gather: bp not B_DELWRI");
44551860Sbostic 		if (!(bp->b_flags & B_LOCKED))
44651915Sbostic 			panic("lfs_gather: bp not B_LOCKED");
44751342Sbostic #endif
44851860Sbostic 		/*
44951860Sbostic 		 * If full, finish this segment.  We may be doing I/O, so
45051860Sbostic 		 * release and reacquire the splbio().
45151860Sbostic 		 */
45251342Sbostic 		if (sp->sum_bytes_left < sizeof(daddr_t) ||
45351215Sbostic 		    sp->seg_bytes_left < fs->lfs_bsize) {
45451215Sbostic 			splx(s);
45551342Sbostic 			lfs_updatemeta(fs,
45651860Sbostic 			    sp, vp, start_lbp, bpp, lbp - start_lbp);
45751215Sbostic 
45851342Sbostic 			/* Add the current file to the segment summary. */
45951342Sbostic 			++((SEGSUM *)(sp->segsum))->ss_nfinfo;
46051215Sbostic 
46151342Sbostic 			version = fip->fi_version;
46251860Sbostic 			lfs_writeseg(fs, sp);
46351915Sbostic 			lfs_initseg(fs, sp);
46451342Sbostic 
46551215Sbostic 			fip = sp->fip;
46651342Sbostic 			fip->fi_version = version;
46751215Sbostic 			fip->fi_ino = ip->i_number;
46851342Sbostic 			start_lbp = lbp = fip->fi_blocks;
46951342Sbostic 
47052682Sstaelin 			sp->sum_bytes_left -=
47152682Sstaelin 			    sizeof(struct finfo) - sizeof(daddr_t);
47252682Sstaelin 
47351215Sbostic 			bpp = sp->cbpp;
47453145Sstaelin 			goto loop;
47551215Sbostic 		}
47652682Sstaelin 
47752682Sstaelin 		/* Insert into the buffer list, update the FINFO block. */
47852682Sstaelin 		*sp->cbpp++ = bp;
47952682Sstaelin 		++fip->fi_nblocks;
48052682Sstaelin 		*lbp++ = bp->b_lblkno;
48152682Sstaelin 
48252682Sstaelin 		sp->sum_bytes_left -= sizeof(daddr_t);
48352682Sstaelin 		sp->seg_bytes_left -= bp->b_bufsize;
48451188Sbostic 	}
48551215Sbostic 	splx(s);
48651860Sbostic 	lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp);
48751188Sbostic }
48851188Sbostic 
48951342Sbostic /*
49051342Sbostic  * Update the metadata that points to the blocks listed in the FINFO
49151188Sbostic  * array.
49251188Sbostic  */
49352077Sbostic void
49451860Sbostic lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks)
49551499Sbostic 	struct lfs *fs;
49652085Sbostic 	struct segment *sp;
49752085Sbostic 	struct vnode *vp;
49851215Sbostic 	daddr_t *lbp;
49952085Sbostic 	struct buf **bpp;
50051215Sbostic 	int nblocks;
50151188Sbostic {
502*53530Sheideman 	USES_VOP_BWRITE;
50351915Sbostic 	SEGUSE *sup;
50452085Sbostic 	struct buf *bp;
50551860Sbostic 	INDIR a[NIADDR], *ap;
50652085Sbostic 	struct inode *ip;
50751915Sbostic 	daddr_t daddr, lbn, off;
50851860Sbostic 	int db_per_fsb, error, i, num;
50951188Sbostic 
51051860Sbostic #ifdef VERBOSE
51151860Sbostic 	printf("lfs_updatemeta\n");
51251860Sbostic #endif
51351342Sbostic 	if (nblocks == 0)
51451215Sbostic 		return;
51551215Sbostic 
51651915Sbostic 	/* Sort the blocks. */
51752077Sbostic 	lfs_shellsort(bpp, lbp, nblocks);
51851215Sbostic 
51951915Sbostic 	/*
52051915Sbostic 	 * Assign disk addresses, and update references to the logical
52151915Sbostic 	 * block and the segment usage information.
52251915Sbostic 	 */
52351860Sbostic 	db_per_fsb = fsbtodb(fs, 1);
52451915Sbostic 	for (i = nblocks; i--; ++bpp) {
52551915Sbostic 		lbn = *lbp++;
52651915Sbostic 		(*bpp)->b_blkno = off = fs->lfs_offset;
52751860Sbostic 		fs->lfs_offset += db_per_fsb;
52851215Sbostic 
52951860Sbostic 		if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num))
53052085Sbostic 			panic("lfs_updatemeta: lfs_bmaparray %d", error);
53151860Sbostic 		ip = VTOI(vp);
53251860Sbostic 		switch (num) {
53351860Sbostic 		case 0:
53451915Sbostic 			ip->i_db[lbn] = off;
53551860Sbostic 			break;
53651860Sbostic 		case 1:
53751915Sbostic 			ip->i_ib[a[0].in_off] = off;
53851860Sbostic 			break;
53951860Sbostic 		default:
54051860Sbostic 			ap = &a[num - 1];
54151860Sbostic 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
54251860Sbostic 				panic("lfs_updatemeta: bread bno %d",
54351860Sbostic 				    ap->in_lbn);
54451915Sbostic 			bp->b_un.b_daddr[ap->in_off] = off;
545*53530Sheideman 			VOP_BWRITE(bp);
54651188Sbostic 		}
54751915Sbostic 
54851915Sbostic 		/* Update segment usage information. */
54951915Sbostic 		if (daddr != UNASSIGNED) {
55051915Sbostic 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
55151915Sbostic #ifdef DIAGNOSTIC
55251915Sbostic 			if (sup->su_nbytes < fs->lfs_bsize)
55352819Sbostic 				/* XXX -- Change to a panic. */
55452819Sbostic 				printf("lfs: negative bytes (segment %d)\n",
55551915Sbostic 				    datosn(fs, daddr));
55651915Sbostic #endif
55751915Sbostic 			sup->su_nbytes -= fs->lfs_bsize;
55852085Sbostic 			LFS_UBWRITE(bp);
55951915Sbostic 		}
56051188Sbostic 	}
56151188Sbostic }
56251188Sbostic 
56351915Sbostic /*
56451915Sbostic  * Start a new segment.
56551915Sbostic  */
56652077Sbostic void
56751915Sbostic lfs_initseg(fs, sp)
56851499Sbostic 	struct lfs *fs;
56952085Sbostic 	struct segment *sp;
57051188Sbostic {
57151915Sbostic 	SEGUSE *sup;
57251915Sbostic 	SEGSUM *ssp;
57351915Sbostic 	struct buf *bp;
57451915Sbostic 	daddr_t lbn, *lbnp;
57551215Sbostic 
57651860Sbostic #ifdef VERBOSE
57751915Sbostic 	printf("lfs_initseg\n");
57851860Sbostic #endif
57951915Sbostic 	/* Advance to the next segment. */
58051927Sbostic 	if (!LFS_PARTIAL_FITS(fs)) {
58152682Sstaelin 		/* Wake up any cleaning procs waiting on this file system. */
58252688Sbostic 		wakeup(&fs->lfs_nextseg);
58352688Sbostic 		wakeup(&lfs_allclean_wakeup);
58452682Sstaelin 
58551927Sbostic 		lfs_newseg(fs);
58651927Sbostic 		fs->lfs_offset = fs->lfs_curseg;
58751915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
58851915Sbostic 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
58951915Sbostic 
59051915Sbostic 		/*
59151927Sbostic 		 * If the segment contains a superblock, update the offset
59251927Sbostic 		 * and summary address to skip over it.
59351915Sbostic 		 */
59452077Sbostic 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
59551927Sbostic 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
59651915Sbostic 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
59751915Sbostic 			sp->seg_bytes_left -= LFS_SBPAD;
59851215Sbostic 		}
59952085Sbostic 		brelse(bp);
60051915Sbostic 	} else {
60151915Sbostic 		sp->seg_number = datosn(fs, fs->lfs_curseg);
60251915Sbostic 		sp->seg_bytes_left = (fs->lfs_dbpseg -
60351915Sbostic 		    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
60451915Sbostic 	}
60551342Sbostic 
60651915Sbostic 	sp->ibp = NULL;
60751915Sbostic 	sp->ninodes = 0;
60851342Sbostic 
60951915Sbostic 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
61051915Sbostic 	sp->cbpp = sp->bpp;
61152688Sbostic 	*sp->cbpp = lfs_newbuf(fs, fs->lfs_offset, LFS_SUMMARY_SIZE);
61251915Sbostic 	sp->segsum = (*sp->cbpp)->b_un.b_addr;
61351915Sbostic 	++sp->cbpp;
61451915Sbostic 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
61551342Sbostic 
61651915Sbostic 	/* Set point to SEGSUM, initialize it. */
61751915Sbostic 	ssp = sp->segsum;
61851915Sbostic 	ssp->ss_next = fs->lfs_nextseg;
61951915Sbostic 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
62051342Sbostic 
62151915Sbostic 	/* Set pointer to first FINFO, initialize it. */
62252085Sbostic 	sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
62351915Sbostic 	sp->fip->fi_nblocks = 0;
62451342Sbostic 
62551915Sbostic 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
62651915Sbostic 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
62751915Sbostic }
62851342Sbostic 
62951915Sbostic /*
63051915Sbostic  * Return the next segment to write.
63151915Sbostic  */
63252077Sbostic void
63351915Sbostic lfs_newseg(fs)
63451915Sbostic 	struct lfs *fs;
63551915Sbostic {
63651927Sbostic 	CLEANERINFO *cip;
63751915Sbostic 	SEGUSE *sup;
63851915Sbostic 	struct buf *bp;
63951927Sbostic 	int curseg, isdirty, sn;
64051915Sbostic 
64151915Sbostic #ifdef VERBOSE
64251915Sbostic 	printf("lfs_newseg\n");
64351915Sbostic #endif
64451927Sbostic 	/*
64551927Sbostic 	 * Turn off the active bit for the current segment, turn on the
64651927Sbostic 	 * active and dirty bits for the next segment, update the cleaner
64751927Sbostic 	 * info.  Set the current segment to the next segment, get a new
64851927Sbostic 	 * next segment.
64951927Sbostic 	 */
65051927Sbostic 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp);
65151927Sbostic 	sup->su_flags &= ~SEGUSE_ACTIVE;
65252085Sbostic 	LFS_UBWRITE(bp);
65351927Sbostic 
65451927Sbostic 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
65551927Sbostic 	sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY;
65652085Sbostic 	LFS_UBWRITE(bp);
65751927Sbostic 
65851927Sbostic 	LFS_CLEANERINFO(cip, fs, bp);
65951927Sbostic 	--cip->clean;
66051927Sbostic 	++cip->dirty;
66152085Sbostic 	LFS_UBWRITE(bp);
66251927Sbostic 
66351927Sbostic 	fs->lfs_lastseg = fs->lfs_curseg;
66451927Sbostic 	fs->lfs_curseg = fs->lfs_nextseg;
66551927Sbostic 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
66651915Sbostic 		sn = (sn + 1) % fs->lfs_nseg;
66751927Sbostic 		if (sn == curseg)
66851915Sbostic 			panic("lfs_nextseg: no clean segments");
66951915Sbostic 		LFS_SEGENTRY(sup, fs, sn, bp);
67051915Sbostic 		isdirty = sup->su_flags & SEGUSE_DIRTY;
67152085Sbostic 		brelse(bp);
67251915Sbostic 		if (!isdirty)
67351915Sbostic 			break;
67451915Sbostic 	}
67551927Sbostic 	fs->lfs_nextseg = sntoda(fs, sn);
67651188Sbostic }
67751188Sbostic 
67852077Sbostic void
67951188Sbostic lfs_writeseg(fs, sp)
68051499Sbostic 	struct lfs *fs;
68152085Sbostic 	struct segment *sp;
68251188Sbostic {
68352688Sbostic 	struct buf **bpp, *bp, *cbp;
68451188Sbostic 	SEGUSE *sup;
68552085Sbostic 	SEGSUM *ssp;
68651860Sbostic 	dev_t i_dev;
68751860Sbostic 	u_long *datap, *dp;
68852688Sbostic 	size_t size;
68952688Sbostic 	int ch_per_blk, i, nblocks, num, s, (*strategy)__P((struct buf *));
69052688Sbostic 	char *p;
69151188Sbostic 
69251860Sbostic #ifdef VERBOSE
69351860Sbostic 	printf("lfs_writeseg\n");
69451860Sbostic #endif
69552085Sbostic 	if ((nblocks = sp->cbpp - sp->bpp) == 0)
69652085Sbostic 		return;
69752085Sbostic 
69851188Sbostic 	/*
69952085Sbostic 	 * Compute checksum across data and then across summary; the first
70052085Sbostic 	 * block (the summary block) is skipped.  Set the create time here
70152085Sbostic 	 * so that it's guaranteed to be later than the inode mod times.
70251860Sbostic 	 *
70351860Sbostic 	 * XXX
70451860Sbostic 	 * Fix this to do it inline, instead of malloc/copy.
70551188Sbostic 	 */
70651860Sbostic 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
70751915Sbostic 	for (bpp = sp->bpp, i = nblocks - 1; i--;)
70851915Sbostic 		*dp++ = (*++bpp)->b_un.b_words[0];
70952085Sbostic 	ssp = (SEGSUM *)sp->segsum;
71052103Sbostic 	ssp->ss_create = time.tv_sec;
71152085Sbostic 	ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long));
71252085Sbostic 	ssp->ss_sumsum =
71352085Sbostic 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
71451927Sbostic 	free(datap, M_SEGMENT);
71551188Sbostic 
71651860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
71751860Sbostic 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy;
71851301Sbostic 
71952688Sbostic 	/*
72052688Sbostic 	 * When we simply write the blocks we lose a rotation for every block
72152688Sbostic 	 * written.  To avoid this problem, we allocate memory in chunks, copy
72252688Sbostic 	 * the buffers into the chunk and write the chunk.  56K was chosen as
72352688Sbostic 	 * some driver/controllers can't handle unsigned 16 bit transfers.
72452688Sbostic 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
72552688Sbostic 	 * and brelse the buffer (which will move them to the LRU list).  Add
72652688Sbostic 	 * the B_CALL flag to the buffer header so we can count I/O's for the
72752688Sbostic 	 * checkpoints and so we can release the allocated memory.
72852688Sbostic 	 *
72952688Sbostic 	 * XXX
73052688Sbostic 	 * This should be removed if the new virtual memory system allows us to
73152688Sbostic 	 * easily make the buffers contiguous in kernel memory and if that's
73252688Sbostic 	 * fast enough.
73352688Sbostic 	 */
73452688Sbostic #define	LFS_CHUNKSIZE	(56 * 1024)
73552688Sbostic 	ch_per_blk = LFS_CHUNKSIZE / fs->lfs_bsize;
73652688Sbostic 	for (bpp = sp->bpp, i = nblocks; i;) {
73752688Sbostic 		num = ch_per_blk;
73852688Sbostic 		if (num > i)
73952688Sbostic 			num = i;
74052688Sbostic 		i -= num;
74152688Sbostic 		size = num * fs->lfs_bsize;
74252688Sbostic 
74352688Sbostic 		cbp = lfs_newbuf(fs, (*bpp)->b_blkno, 0);
74452688Sbostic 		cbp->b_dev = i_dev;
74552688Sbostic 		cbp->b_flags = B_ASYNC | B_BUSY | B_CALL;
74652688Sbostic 		cbp->b_iodone = lfs_callback;
74752688Sbostic 		cbp->b_saveaddr = cbp->b_un.b_addr;
74852688Sbostic 		cbp->b_un.b_addr = malloc(size, M_SEGMENT, M_WAITOK);
74952688Sbostic 
75052688Sbostic 		s = splbio();
75152688Sbostic 		++fs->lfs_iocount;
75252688Sbostic 		for (p = cbp->b_un.b_addr; num--;) {
75352688Sbostic 			bp = *bpp++;
75452688Sbostic 			bcopy(bp->b_un.b_addr, p, bp->b_bcount);
75552688Sbostic 			p += bp->b_bcount;
75652688Sbostic 			bp->b_flags &=
75752688Sbostic 			    ~(B_DONE | B_ERROR | B_READ | B_DELWRI | B_LOCKED);
75852688Sbostic 			if (!(bp->b_flags & B_NOCACHE)) {
75952688Sbostic 				bremfree(bp);
76052688Sbostic 				reassignbuf(bp, bp->b_vp);
76152688Sbostic 			}
76252688Sbostic 			brelse(bp);
76351860Sbostic 		}
76452688Sbostic 		splx(s);
76552688Sbostic 		cbp->b_bcount = p - cbp->b_un.b_addr;
76652688Sbostic 		(strategy)(cbp);
76751860Sbostic 	}
76852077Sbostic 
76952682Sstaelin 	/* Update the segment usage information. */
77052682Sstaelin 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
77152682Sstaelin 	sup->su_nbytes += nblocks - 1 -
77252682Sstaelin 	    (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift;
77352682Sstaelin 	sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
77452682Sstaelin 	sup->su_lastmod = time.tv_sec;
77552682Sstaelin 	LFS_UBWRITE(bp);
77651188Sbostic }
77751188Sbostic 
77852077Sbostic void
77951860Sbostic lfs_writesuper(fs, sp)
78051499Sbostic 	struct lfs *fs;
78152085Sbostic 	struct segment *sp;
78251301Sbostic {
78352085Sbostic 	struct buf *bp;
78451860Sbostic 	dev_t i_dev;
78552085Sbostic 	int (*strategy) __P((struct buf *));
78651301Sbostic 
78751860Sbostic #ifdef VERBOSE
78851860Sbostic 	printf("lfs_writesuper\n");
78951860Sbostic #endif
79051860Sbostic 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
79151860Sbostic 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy;
79251356Sbostic 
79351342Sbostic 	/* Checksum the superblock and copy it into a buffer. */
79451499Sbostic 	fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
79552688Sbostic 	bp = lfs_newbuf(fs, fs->lfs_sboffs[0], LFS_SBPAD);
79651860Sbostic 	*bp->b_un.b_lfs = *fs;
79751215Sbostic 
79851356Sbostic 	/* Write the first superblock (wait). */
79951860Sbostic 	bp->b_dev = i_dev;
80051915Sbostic 	bp->b_flags |= B_BUSY;
80151860Sbostic 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
80251342Sbostic 	(strategy)(bp);
80351215Sbostic 	biowait(bp);
80451342Sbostic 
80551356Sbostic 	/* Write the second superblock (don't wait). */
80651215Sbostic 	bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1];
80751915Sbostic 	bp->b_flags |= B_ASYNC | B_BUSY;
80851860Sbostic 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
80951342Sbostic 	(strategy)(bp);
81051215Sbostic }
81151215Sbostic 
81251342Sbostic /*
81351342Sbostic  * Logical block number match routines used when traversing the dirty block
81451342Sbostic  * chain.
81551342Sbostic  */
81652077Sbostic int
81752077Sbostic lfs_match_data(fs, bp)
81851860Sbostic 	struct lfs *fs;
81952085Sbostic 	struct buf *bp;
82051215Sbostic {
82151342Sbostic 	return (bp->b_lblkno >= 0);
82251215Sbostic }
82351215Sbostic 
82452077Sbostic int
82552077Sbostic lfs_match_indir(fs, bp)
82651860Sbostic 	struct lfs *fs;
82752085Sbostic 	struct buf *bp;
82851215Sbostic {
82951860Sbostic 	int lbn;
83051860Sbostic 
83151860Sbostic 	lbn = bp->b_lblkno;
83251860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
83351215Sbostic }
83451215Sbostic 
83552077Sbostic int
83652077Sbostic lfs_match_dindir(fs, bp)
83751860Sbostic 	struct lfs *fs;
83852085Sbostic 	struct buf *bp;
83951215Sbostic {
84051860Sbostic 	int lbn;
84151860Sbostic 
84251860Sbostic 	lbn = bp->b_lblkno;
84351860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
84451215Sbostic }
84551215Sbostic 
84652077Sbostic int
84752077Sbostic lfs_match_tindir(fs, bp)
84851499Sbostic 	struct lfs *fs;
84952085Sbostic 	struct buf *bp;
85051342Sbostic {
85151860Sbostic 	int lbn;
85251342Sbostic 
85351860Sbostic 	lbn = bp->b_lblkno;
85451860Sbostic 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
85551860Sbostic }
85651342Sbostic 
85751860Sbostic /*
85851860Sbostic  * Allocate a new buffer header.
85951860Sbostic  */
86052085Sbostic struct buf *
86152688Sbostic lfs_newbuf(fs, daddr, size)
86251860Sbostic 	struct lfs *fs;
86351860Sbostic 	daddr_t daddr;
86451860Sbostic 	size_t size;
86551860Sbostic {
86652085Sbostic 	struct buf *bp;
86751342Sbostic 
86851860Sbostic #ifdef VERBOSE
86951860Sbostic 	printf("lfs_newbuf\n");
87051860Sbostic #endif
87151860Sbostic 	bp = getnewbuf();
87251860Sbostic 	bremhash(bp);
87351860Sbostic 	bgetvp(fs->lfs_ivnode, bp);
87451860Sbostic 	bp->b_bcount = 0;
87551860Sbostic 	bp->b_lblkno = daddr;
87651860Sbostic 	bp->b_blkno = daddr;
87751860Sbostic 	bp->b_error = 0;
87851860Sbostic 	bp->b_resid = 0;
87952688Sbostic 	if (size)
88052688Sbostic 		allocbuf(bp, size);
88151860Sbostic 	bp->b_flags |= B_NOCACHE;
88252688Sbostic 	bp->b_saveaddr = NULL;
88351915Sbostic 	binshash(bp, &bfreelist[BQ_AGE]);
88451860Sbostic 	return (bp);
88551860Sbostic }
88651342Sbostic 
88753347Sbostic void
88851860Sbostic lfs_callback(bp)
88952085Sbostic 	struct buf *bp;
89051860Sbostic {
89151860Sbostic 	struct lfs *fs;
89251342Sbostic 
89351860Sbostic 	fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
89451860Sbostic #ifdef DIAGNOSTIC
89551860Sbostic 	if (fs->lfs_iocount == 0)
89651860Sbostic 		panic("lfs_callback: zero iocount\n");
89751860Sbostic #endif
89851860Sbostic 	if (--fs->lfs_iocount == 0)
89952688Sbostic 		wakeup(&fs->lfs_iocount);
90051915Sbostic 
90152688Sbostic 	if (bp->b_saveaddr) {
90252688Sbostic 		free(bp->b_un.b_addr, M_SEGMENT);
90352688Sbostic 		bp->b_un.b_addr = bp->b_saveaddr;
90452819Sbostic 		bp->b_saveaddr = NULL;
90552688Sbostic 	}
90651860Sbostic 	brelse(bp);
90751860Sbostic }
90851342Sbostic 
90951215Sbostic /*
91051188Sbostic  * Shellsort (diminishing increment sort) from Data Structures and
91151188Sbostic  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
91251188Sbostic  * see also Knuth Vol. 3, page 84.  The increments are selected from
91351188Sbostic  * formula (8), page 95.  Roughly O(N^3/2).
91451188Sbostic  */
91551188Sbostic /*
91651188Sbostic  * This is our own private copy of shellsort because we want to sort
91751188Sbostic  * two parallel arrays (the array of buffer pointers and the array of
91851188Sbostic  * logical block numbers) simultaneously.  Note that we cast the array
91951188Sbostic  * of logical block numbers to a unsigned in this routine so that the
92051188Sbostic  * negative block numbers (meta data blocks) sort AFTER the data blocks.
92151188Sbostic  */
92252077Sbostic void
92352077Sbostic lfs_shellsort(bp_array, lb_array, nmemb)
92452085Sbostic 	struct buf **bp_array;
92551215Sbostic 	daddr_t *lb_array;
92651188Sbostic 	register int nmemb;
92751188Sbostic {
92851188Sbostic 	static int __rsshell_increments[] = { 4, 1, 0 };
92951188Sbostic 	register int incr, *incrp, t1, t2;
93052085Sbostic 	struct buf *bp_temp;
93151188Sbostic 	u_long lb_temp;
93251188Sbostic 
93351188Sbostic 	for (incrp = __rsshell_increments; incr = *incrp++;)
93451188Sbostic 		for (t1 = incr; t1 < nmemb; ++t1)
93551188Sbostic 			for (t2 = t1 - incr; t2 >= 0;)
93651188Sbostic 				if (lb_array[t2] > lb_array[t2 + incr]) {
93751188Sbostic 					lb_temp = lb_array[t2];
93851188Sbostic 					lb_array[t2] = lb_array[t2 + incr];
93951188Sbostic 					lb_array[t2 + incr] = lb_temp;
94051188Sbostic 					bp_temp = bp_array[t2];
94151188Sbostic 					bp_array[t2] = bp_array[t2 + incr];
94251188Sbostic 					bp_array[t2 + incr] = bp_temp;
94351188Sbostic 					t2 -= incr;
94451188Sbostic 				} else
94551188Sbostic 					break;
94651188Sbostic }
947