Home | History | Annotate | Line # | Download | only in lfs
lfs_segment.c revision 1.23.2.9
      1 /*	$NetBSD: lfs_segment.c,v 1.23.2.9 2000/01/15 18:01:45 he Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_segment.c	8.10 (Berkeley) 6/10/95
     71  */
     72 
     73 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str))
     74 
     75 #include "opt_ddb.h"
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/namei.h>
     79 #include <sys/kernel.h>
     80 #include <sys/resourcevar.h>
     81 #include <sys/file.h>
     82 #include <sys/stat.h>
     83 #include <sys/buf.h>
     84 #include <sys/proc.h>
     85 #include <sys/conf.h>
     86 #include <sys/vnode.h>
     87 #include <sys/malloc.h>
     88 #include <sys/mount.h>
     89 
     90 #include <miscfs/specfs/specdev.h>
     91 #include <miscfs/fifofs/fifo.h>
     92 
     93 #include <ufs/ufs/quota.h>
     94 #include <ufs/ufs/inode.h>
     95 #include <ufs/ufs/dir.h>
     96 #include <ufs/ufs/ufsmount.h>
     97 #include <ufs/ufs/ufs_extern.h>
     98 
     99 #include <ufs/lfs/lfs.h>
    100 #include <ufs/lfs/lfs_extern.h>
    101 
    102 extern int count_lock_queue __P((void));
    103 extern struct simplelock vnode_free_list_slock;		/* XXX */
    104 extern TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* XXX */
    105 
    106 /*
    107  * Determine if it's OK to start a partial in this segment, or if we need
    108  * to go on to a new segment.
    109  */
    110 #define	LFS_PARTIAL_FITS(fs) \
    111 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
    112 	1 << (fs)->lfs_fsbtodb)
    113 
    114 void	 lfs_callback __P((struct buf *));
    115 int	 lfs_gather __P((struct lfs *, struct segment *,
    116 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
    117 int	 lfs_gatherblock __P((struct segment *, struct buf *, int *));
    118 void	 lfs_iset __P((struct inode *, ufs_daddr_t, time_t));
    119 int	 lfs_match_fake __P((struct lfs *, struct buf *));
    120 int	 lfs_match_data __P((struct lfs *, struct buf *));
    121 int	 lfs_match_dindir __P((struct lfs *, struct buf *));
    122 int	 lfs_match_indir __P((struct lfs *, struct buf *));
    123 int	 lfs_match_tindir __P((struct lfs *, struct buf *));
    124 void	 lfs_newseg __P((struct lfs *));
    125 void	 lfs_shellsort __P((struct buf **, ufs_daddr_t *, register int));
    126 void	 lfs_supercallback __P((struct buf *));
    127 void	 lfs_updatemeta __P((struct segment *));
    128 int	 lfs_vref __P((struct vnode *));
    129 void	 lfs_vunref __P((struct vnode *));
    130 void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
    131 int	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
    132 int	 lfs_writeseg __P((struct lfs *, struct segment *));
    133 void	 lfs_writesuper __P((struct lfs *, daddr_t));
    134 int	 lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
    135 	    struct segment *sp, int dirops));
    136 
    137 int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
    138 int	lfs_writeindir = 1;             /* whether to flush indir on non-ckp */
    139 int	lfs_clean_vnhead = 0;		/* Allow freeing to head of vn list */
    140 int	lfs_dirvcount = 0;		/* # active dirops */
    141 
    142 /* Statistics Counters */
    143 int lfs_dostats = 1;
    144 struct lfs_stats lfs_stats;
    145 
    146 /* op values to lfs_writevnodes */
    147 #define	VN_REG	        0
    148 #define	VN_DIROP	1
    149 #define	VN_EMPTY	2
    150 #define VN_CLEAN        3
    151 
    152 #define LFS_MAX_ACTIVE          10
    153 
    154 /*
    155  * XXX KS - Set modification time on the Ifile, so the cleaner can
    156  * read the fs mod time off of it.  We don't set IN_UPDATE here,
    157  * since we don't really need this to be flushed to disk (and in any
    158  * case that wouldn't happen to the Ifile until we checkpoint).
    159  */
    160 void
    161 lfs_imtime(fs)
    162 	struct lfs *fs;
    163 {
    164 	struct timespec ts;
    165 	struct inode *ip;
    166 
    167 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    168 	ip = VTOI(fs->lfs_ivnode);
    169 	ip->i_ffs_mtime = ts.tv_sec;
    170 	ip->i_ffs_mtimensec = ts.tv_nsec;
    171 }
    172 
    173 /*
    174  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
    175  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
    176  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
    177  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
    178  */
    179 
    180 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
    181 #define IS_FLUSHING(fs,vp)  ((fs)->lfs_flushvp == (vp))
    182 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
    183 
    184 int
    185 lfs_vflush(vp)
    186 	struct vnode *vp;
    187 {
    188 	struct inode *ip;
    189 	struct lfs *fs;
    190 	struct segment *sp;
    191 	struct buf *bp, *nbp, *tbp, *tnbp;
    192 	int error, s;
    193 
    194 	ip = VTOI(vp);
    195 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
    196 
    197 	if(ip->i_flag & IN_CLEANING) {
    198 #ifdef DEBUG_LFS
    199 		ivndebug(vp,"vflush/in_cleaning");
    200 #endif
    201 		ip->i_flag &= ~IN_CLEANING;
    202 		if(ip->i_flag & IN_MODIFIED) {
    203 			fs->lfs_uinodes--;
    204 		} else
    205 			ip->i_flag |= IN_MODIFIED;
    206 		/*
    207 		 * Toss any cleaning buffers that have real counterparts
    208 		 * to avoid losing new data
    209 		 */
    210 		s = splbio();
    211 		for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
    212 			nbp = bp->b_vnbufs.le_next;
    213 			if(bp->b_flags & B_CALL) {
    214 				for(tbp=vp->v_dirtyblkhd.lh_first; tbp;
    215 				    tbp=tnbp)
    216 				{
    217 					tnbp = tbp->b_vnbufs.le_next;
    218 					if(tbp->b_vp == bp->b_vp
    219 					   && tbp->b_lblkno == bp->b_lblkno
    220 					   && tbp != bp)
    221 					{
    222 						lfs_freebuf(bp);
    223 					}
    224 				}
    225 			}
    226 		}
    227 		splx(s);
    228 	}
    229 
    230 	/* If the node is being written, wait until that is done */
    231 	if(WRITEINPROG(vp)) {
    232 #ifdef DEBUG_LFS
    233 		ivndebug(vp,"vflush/writeinprog");
    234 #endif
    235 		tsleep(vp, PRIBIO+1, "lfs_vw", 0);
    236 	}
    237 
    238 	/* Protect against VXLOCK deadlock in vinvalbuf() */
    239 	lfs_seglock(fs, SEGM_SYNC);
    240 
    241 	/* If we're supposed to flush a freed inode, just toss it */
    242 	/* XXX - seglock, so these buffers can't be gathered, right? */
    243 	if(ip->i_ffs_mode == 0) {
    244 		printf("lfs_vflush: ino %d is freed, not flushing\n",
    245 			ip->i_number);
    246 		s = splbio();
    247 		for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
    248 			nbp = bp->b_vnbufs.le_next;
    249 			/* Copied from lfs_writeseg */
    250 			if (bp->b_flags & B_CALL) {
    251 				/* if B_CALL, it was created with newbuf */
    252 				lfs_freebuf(bp);
    253 			} else {
    254 				bremfree(bp);
    255 				bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
    256                                          B_LOCKED | B_GATHERED);
    257 				bp->b_flags |= B_DONE;
    258 				reassignbuf(bp, vp);
    259 				brelse(bp);
    260 			}
    261 		}
    262 		splx(s);
    263 		if(ip->i_flag & IN_CLEANING)
    264 			fs->lfs_uinodes--;
    265 		if(ip->i_flag & IN_MODIFIED)
    266 			fs->lfs_uinodes--;
    267 		ip->i_flag &= ~(IN_MODIFIED|IN_UPDATE|IN_ACCESS|IN_CHANGE|IN_CLEANING);
    268 		printf("lfs_vflush: done not flushing ino %d\n",
    269 			ip->i_number);
    270 		lfs_segunlock(fs);
    271 		return 0;
    272 	}
    273 
    274 	SET_FLUSHING(fs,vp);
    275 	if (fs->lfs_nactive > LFS_MAX_ACTIVE) {
    276 		error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP);
    277 		CLR_FLUSHING(fs,vp);
    278 		lfs_segunlock(fs);
    279 		return error;
    280 	}
    281 	sp = fs->lfs_sp;
    282 
    283 	if (vp->v_dirtyblkhd.lh_first == NULL) {
    284 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
    285 	} else if((ip->i_flag & IN_CLEANING) && (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
    286 #ifdef DEBUG_LFS
    287 		ivndebug(vp,"vflush/clean");
    288 #endif
    289 		lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
    290 	}
    291 	else if(lfs_dostats) {
    292 		if(vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & (IN_MODIFIED|IN_UPDATE|IN_ACCESS|IN_CHANGE|IN_CLEANING)))
    293 			++lfs_stats.vflush_invoked;
    294 #ifdef DEBUG_LFS
    295 		ivndebug(vp,"vflush");
    296 #endif
    297 	}
    298 
    299 #ifdef DIAGNOSTIC
    300 	/* XXX KS This actually can happen right now, though it shouldn't(?) */
    301 	if(vp->v_flag & VDIROP) {
    302 		printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
    303 		/* panic("VDIROP being flushed...this can\'t happen"); */
    304 	}
    305 	if(vp->v_usecount<0) {
    306 		printf("usecount=%ld\n",vp->v_usecount);
    307 		panic("lfs_vflush: usecount<0");
    308 	}
    309 #endif
    310 
    311 	do {
    312 		do {
    313 			if (vp->v_dirtyblkhd.lh_first != NULL)
    314 				lfs_writefile(fs, sp, vp);
    315 		} while (lfs_writeinode(fs, sp, ip));
    316 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
    317 
    318 	if(lfs_dostats) {
    319 		++lfs_stats.nwrites;
    320 		if (sp->seg_flags & SEGM_SYNC)
    321 			++lfs_stats.nsync_writes;
    322 		if (sp->seg_flags & SEGM_CKP)
    323 			++lfs_stats.ncheckpoints;
    324 	}
    325 	lfs_segunlock(fs);
    326 
    327 	CLR_FLUSHING(fs,vp);
    328 	return (0);
    329 }
    330 
    331 #ifdef DEBUG_LFS_VERBOSE
    332 # define vndebug(vp,str) if(VTOI(vp)->i_flag & IN_CLEANING) printf("not writing ino %d because %s (op %d)\n",VTOI(vp)->i_number,(str),op)
    333 #else
    334 # define vndebug(vp,str)
    335 #endif
    336 
    337 int
    338 lfs_writevnodes(fs, mp, sp, op)
    339 	struct lfs *fs;
    340 	struct mount *mp;
    341 	struct segment *sp;
    342 	int op;
    343 {
    344 	struct inode *ip;
    345 	struct vnode *vp;
    346 	int inodes_written=0, only_cleaning;
    347 
    348 #ifndef LFS_NO_BACKVP_HACK
    349 	/* BEGIN HACK */
    350 #define	VN_OFFSET	(((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
    351 #define	BACK_VP(VP)	((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
    352 #define	BEG_OF_VLIST	((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
    353 
    354 	/* Find last vnode. */
    355  loop:	for (vp = mp->mnt_vnodelist.lh_first;
    356 	     vp && vp->v_mntvnodes.le_next != NULL;
    357 	     vp = vp->v_mntvnodes.le_next);
    358 	for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
    359 #else
    360 	loop:
    361 	for (vp = mp->mnt_vnodelist.lh_first;
    362 	     vp != NULL;
    363 	     vp = vp->v_mntvnodes.le_next) {
    364 #endif
    365 		/*
    366 		 * If the vnode that we are about to sync is no longer
    367 		 * associated with this mount point, start over.
    368 		 */
    369 		if (vp->v_mount != mp)
    370 			goto loop;
    371 
    372 		ip = VTOI(vp);
    373 		if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
    374 		    (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
    375 			vndebug(vp,"dirop");
    376 			continue;
    377 		}
    378 
    379 		if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) {
    380 			vndebug(vp,"empty");
    381 			continue;
    382 		}
    383 
    384 		if (vp->v_type == VNON) {
    385 			continue;
    386 		}
    387 
    388 		if(op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
    389 		   && vp != fs->lfs_flushvp
    390 		   && !(ip->i_flag & IN_CLEANING)) {
    391 			vndebug(vp,"cleaning");
    392 			continue;
    393 		}
    394 
    395 		if (lfs_vref(vp)) {
    396 			vndebug(vp,"vref");
    397 			continue;
    398 		}
    399 
    400 #if 0 /* XXX KS - if we skip the ifile, things could go badly for us. */
    401 		if(WRITEINPROG(vp)) {
    402 			lfs_vunref(vp);
    403 #ifdef DEBUG_LFS
    404 			ivndebug(vp,"writevnodes/writeinprog");
    405 #endif
    406 			continue;
    407 		}
    408 #endif
    409 		only_cleaning = 0;
    410 		/*
    411 		 * Write the inode/file if dirty and it's not the
    412 		 * the IFILE.
    413 		 */
    414 		if ((ip->i_flag &
    415 		     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE | IN_CLEANING) ||
    416 		     vp->v_dirtyblkhd.lh_first != NULL))
    417 		{
    418 			only_cleaning = ((ip->i_flag & (IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE|IN_CLEANING))==IN_CLEANING);
    419 
    420 			if(ip->i_number != LFS_IFILE_INUM
    421 			   && vp->v_dirtyblkhd.lh_first != NULL)
    422 			{
    423 				lfs_writefile(fs, sp, vp);
    424 			}
    425 			if(vp->v_dirtyblkhd.lh_first != NULL) {
    426 				if(WRITEINPROG(vp)) {
    427 #ifdef DEBUG_LFS
    428 					ivndebug(vp,"writevnodes/write2");
    429 #endif
    430 				} else if(!(ip->i_flag & (IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE|IN_CLEANING))) {
    431 #ifdef DEBUG_LFS
    432 					printf("<%d>",ip->i_number);
    433 #endif
    434 					ip->i_flag |= IN_MODIFIED;
    435 					++fs->lfs_uinodes;
    436 				}
    437 			}
    438 			(void) lfs_writeinode(fs, sp, ip);
    439 			inodes_written++;
    440 		}
    441 
    442 		if(vp->v_flag & VDIROP) {
    443 			--lfs_dirvcount;
    444 			vp->v_flag &= ~VDIROP;
    445 			wakeup(&lfs_dirvcount);
    446 			lfs_vunref(vp);
    447 		}
    448 
    449 		if(lfs_clean_vnhead && only_cleaning)
    450 			lfs_vunref_head(vp);
    451 		else
    452 			lfs_vunref(vp);
    453 	}
    454 	return inodes_written;
    455 }
    456 
    457 int
    458 lfs_segwrite(mp, flags)
    459 	struct mount *mp;
    460 	int flags;			/* Do a checkpoint. */
    461 {
    462 	struct buf *bp;
    463 	struct inode *ip;
    464 	struct lfs *fs;
    465 	struct segment *sp;
    466 	struct vnode *vp;
    467 	SEGUSE *segusep;
    468 	ufs_daddr_t ibno;
    469 	int do_ckp, error, i;
    470 	int writer_set = 0;
    471 	int need_unlock = 0;
    472 
    473 	fs = VFSTOUFS(mp)->um_lfs;
    474 
    475 	lfs_imtime(fs);
    476 
    477 	/*
    478 	 * If we are not the cleaner, and we have fewer than MIN_FREE_SEGS
    479 	 * clean segments, wait until cleaner writes.
    480 	 */
    481 	if(!(flags & SEGM_CLEAN)
    482 	   && (!fs->lfs_seglock || !(fs->lfs_sp->seg_flags & SEGM_CLEAN)))
    483 	{
    484 		do {
    485 			if (fs->lfs_nclean <= MIN_FREE_SEGS
    486 			    || fs->lfs_avail <= 0)
    487 			{
    488 				wakeup(&lfs_allclean_wakeup);
    489 				wakeup(&fs->lfs_nextseg);
    490 				error = tsleep(&fs->lfs_avail, PRIBIO + 1,
    491 					       "lfs_avail", 0);
    492 				if (error) {
    493 					return (error);
    494 				}
    495 			}
    496 		} while (fs->lfs_nclean <= MIN_FREE_SEGS || fs->lfs_avail <= 0);
    497 	}
    498 
    499 	/*
    500 	 * Allocate a segment structure and enough space to hold pointers to
    501 	 * the maximum possible number of buffers which can be described in a
    502 	 * single summary block.
    503 	 */
    504 	do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
    505 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
    506 	sp = fs->lfs_sp;
    507 
    508 	/*
    509 	 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
    510 	 * in which case we have to flush *all* buffers off of this vnode.
    511 	 * We don't care about other nodes, but write any non-dirop nodes
    512 	 * anyway in anticipation of another getnewvnode().
    513 	 *
    514 	 * If we're cleaning we only write cleaning and ifile blocks, and
    515 	 * no dirops, since otherwise we'd risk corruption in a crash.
    516 	 */
    517 	if(sp->seg_flags & SEGM_CLEAN)
    518 		lfs_writevnodes(fs, mp, sp, VN_CLEAN);
    519 	else {
    520 		lfs_writevnodes(fs, mp, sp, VN_REG);
    521 		if(!fs->lfs_dirops || !fs->lfs_flushvp) {
    522 			while(fs->lfs_dirops)
    523 				if((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
    524 						"lfs writer", 0)))
    525 				{
    526 					free(sp->bpp, M_SEGMENT);
    527 					free(sp, M_SEGMENT);
    528 					return (error);
    529 				}
    530 			fs->lfs_writer++;
    531 			writer_set=1;
    532 			lfs_writevnodes(fs, mp, sp, VN_DIROP);
    533 			((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
    534 		}
    535 	}
    536 
    537 	/*
    538 	 * If we are doing a checkpoint, mark everything since the
    539 	 * last checkpoint as no longer ACTIVE.
    540 	 */
    541 	if (do_ckp) {
    542 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
    543 		     --ibno >= fs->lfs_cleansz; ) {
    544 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
    545 
    546 				panic("lfs_segwrite: ifile read");
    547 			segusep = (SEGUSE *)bp->b_data;
    548 			for (i = fs->lfs_sepb; i--; segusep++)
    549 				segusep->su_flags &= ~SEGUSE_ACTIVE;
    550 
    551 			/* But the current segment is still ACTIVE */
    552 			if (fs->lfs_curseg/fs->lfs_sepb==(ibno-fs->lfs_cleansz))
    553 				((SEGUSE *)(bp->b_data))[fs->lfs_curseg%fs->lfs_sepb].su_flags |= SEGUSE_ACTIVE;
    554 			error = VOP_BWRITE(bp);
    555 		}
    556 	}
    557 
    558 	if (do_ckp || fs->lfs_doifile) {
    559 	redo:
    560 		vp = fs->lfs_ivnode;
    561 		/*
    562 		 * Depending on the circumstances of our calling, the ifile
    563 		 * inode might be locked.  If it is, and if it is locked by
    564 		 * us, we should VREF instead of vget here.
    565 		 */
    566 		need_unlock = 0;
    567 		if(VOP_ISLOCKED(vp)
    568 		   && VTOI(vp)->i_lock.lk_lockholder == curproc->p_pid) {
    569 			VREF(vp);
    570 		} else {
    571 			while (vget(vp, LK_EXCLUSIVE))
    572 				continue;
    573 			need_unlock = 1;
    574 		}
    575 		ip = VTOI(vp);
    576 		if (vp->v_dirtyblkhd.lh_first != NULL)
    577 			lfs_writefile(fs, sp, vp);
    578 		(void)lfs_writeinode(fs, sp, ip);
    579 
    580 		/* Only vput if we used vget() above. */
    581 		if(need_unlock)
    582 			vput(vp);
    583 		else
    584 			vrele(vp);
    585 
    586 		if (lfs_writeseg(fs, sp) && do_ckp)
    587 			goto redo;
    588 	} else {
    589 		(void) lfs_writeseg(fs, sp);
    590 	}
    591 
    592 	/*
    593 	 * If the I/O count is non-zero, sleep until it reaches zero.
    594 	 * At the moment, the user's process hangs around so we can
    595 	 * sleep.
    596 	 */
    597 	fs->lfs_doifile = 0;
    598 	if(writer_set && --fs->lfs_writer==0)
    599 		wakeup(&fs->lfs_dirops);
    600 
    601 	if(lfs_dostats) {
    602 		++lfs_stats.nwrites;
    603 		if (sp->seg_flags & SEGM_SYNC)
    604 			++lfs_stats.nsync_writes;
    605 		if (sp->seg_flags & SEGM_CKP)
    606 			++lfs_stats.ncheckpoints;
    607 	}
    608 	lfs_segunlock(fs);
    609 	return (0);
    610 }
    611 
    612 /*
    613  * Write the dirty blocks associated with a vnode.
    614  */
    615 void
    616 lfs_writefile(fs, sp, vp)
    617 	struct lfs *fs;
    618 	struct segment *sp;
    619 	struct vnode *vp;
    620 {
    621 	struct buf *bp;
    622 	struct finfo *fip;
    623 	IFILE *ifp;
    624 
    625 
    626 	if (sp->seg_bytes_left < fs->lfs_bsize ||
    627 	    sp->sum_bytes_left < sizeof(struct finfo))
    628 		(void) lfs_writeseg(fs, sp);
    629 
    630 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
    631 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    632 
    633 	if(vp->v_flag & VDIROP)
    634 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
    635 
    636 	fip = sp->fip;
    637 	fip->fi_nblocks = 0;
    638 	fip->fi_ino = VTOI(vp)->i_number;
    639 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
    640 	fip->fi_version = ifp->if_version;
    641 	brelse(bp);
    642 
    643 	if(sp->seg_flags & SEGM_CLEAN)
    644 	{
    645 		lfs_gather(fs, sp, vp, lfs_match_fake);
    646 		/*
    647 		 * For a file being flushed, we need to write *all* blocks.
    648 		 * This means writing the cleaning blocks first, and then
    649 		 * immediately following with any non-cleaning blocks.
    650 		 * The same is true of the Ifile since checkpoints assume
    651 		 * that all valid Ifile blocks are written.
    652 		 */
    653 	   	if(IS_FLUSHING(fs,vp) || VTOI(vp)->i_number == LFS_IFILE_INUM)
    654 			lfs_gather(fs, sp, vp, lfs_match_data);
    655 	} else
    656 		lfs_gather(fs, sp, vp, lfs_match_data);
    657 
    658 	/*
    659 	 * It may not be necessary to write the meta-data blocks at this point,
    660 	 * as the roll-forward recovery code should be able to reconstruct the
    661 	 * list.
    662 	 *
    663 	 * We have to write them anyway, though, under two conditions: (1) the
    664 	 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
    665 	 * checkpointing.
    666 	 */
    667 	if(lfs_writeindir
    668 	   || IS_FLUSHING(fs,vp)
    669 	   || (sp->seg_flags & SEGM_CKP))
    670 	{
    671 		lfs_gather(fs, sp, vp, lfs_match_indir);
    672 		lfs_gather(fs, sp, vp, lfs_match_dindir);
    673 		lfs_gather(fs, sp, vp, lfs_match_tindir);
    674 	}
    675 	fip = sp->fip;
    676 	if (fip->fi_nblocks != 0) {
    677 		sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
    678 				   sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
    679 		sp->start_lbp = &sp->fip->fi_blocks[0];
    680 	} else {
    681 		sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
    682 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
    683 	}
    684 }
    685 
    686 int
    687 lfs_writeinode(fs, sp, ip)
    688 	struct lfs *fs;
    689 	struct segment *sp;
    690 	struct inode *ip;
    691 {
    692 	struct buf *bp, *ibp;
    693 	IFILE *ifp;
    694 	SEGUSE *sup;
    695 	ufs_daddr_t daddr;
    696 	ino_t ino;
    697 	int error, i, ndx;
    698 	int redo_ifile = 0;
    699 	struct timespec ts;
    700 	int gotblk=0;
    701 
    702 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE | IN_CLEANING)))
    703 		return(0);
    704 
    705 	/* Allocate a new inode block if necessary. */
    706 	if ((ip->i_number != LFS_IFILE_INUM || sp->idp==NULL) && sp->ibp == NULL) {
    707 		/* Allocate a new segment if necessary. */
    708 		if (sp->seg_bytes_left < fs->lfs_bsize ||
    709 		    sp->sum_bytes_left < sizeof(ufs_daddr_t))
    710 			(void) lfs_writeseg(fs, sp);
    711 
    712 		/* Get next inode block. */
    713 		daddr = fs->lfs_offset;
    714 		fs->lfs_offset += fsbtodb(fs, 1);
    715 		sp->ibp = *sp->cbpp++ =
    716 			getblk(VTOI(fs->lfs_ivnode)->i_devvp, daddr, fs->lfs_bsize, 0, 0);
    717 		gotblk++;
    718 
    719 		/* Zero out inode numbers */
    720 		for (i = 0; i < INOPB(fs); ++i)
    721 			((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
    722 
    723 		++sp->start_bpp;
    724 		fs->lfs_avail -= fsbtodb(fs, 1);
    725 		/* Set remaining space counters. */
    726 		sp->seg_bytes_left -= fs->lfs_bsize;
    727 		sp->sum_bytes_left -= sizeof(ufs_daddr_t);
    728 		ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
    729 			sp->ninodes / INOPB(fs) - 1;
    730 		((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
    731 	}
    732 
    733 	/* Update the inode times and copy the inode onto the inode page. */
    734 	if (ip->i_flag & (IN_CLEANING|IN_MODIFIED))
    735 		--fs->lfs_uinodes;
    736 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    737 	LFS_ITIMES(ip, &ts, &ts, &ts);
    738 
    739 	if(ip->i_flag & IN_CLEANING)
    740 		ip->i_flag &= ~IN_CLEANING;
    741 	else
    742 		ip->i_flag &= ~(IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE);
    743 
    744 	/*
    745 	 * If this is the Ifile, and we've already written the Ifile in this
    746 	 * partial segment, just overwrite it (it's not on disk yet) and
    747 	 * continue.
    748 	 *
    749 	 * XXX we know that the bp that we get the second time around has
    750 	 * already been gathered.
    751 	 */
    752 	if(ip->i_number == LFS_IFILE_INUM && sp->idp) {
    753 		*(sp->idp) = ip->i_din.ffs_din;
    754 		return 0;
    755 	}
    756 
    757 	bp = sp->ibp;
    758 	((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] =
    759 		ip->i_din.ffs_din;
    760 
    761 	if(ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
    762 		sp->idp = ((struct dinode *)bp->b_data)+(sp->ninodes % INOPB(fs));
    763 	if(gotblk) {
    764 		bp->b_flags |= B_LOCKED;
    765 		brelse(bp);
    766 	}
    767 
    768 	/* Increment inode count in segment summary block. */
    769 	++((SEGSUM *)(sp->segsum))->ss_ninos;
    770 
    771 	/* If this page is full, set flag to allocate a new page. */
    772 	if (++sp->ninodes % INOPB(fs) == 0)
    773 		sp->ibp = NULL;
    774 
    775 	/*
    776 	 * If updating the ifile, update the super-block.  Update the disk
    777 	 * address and access times for this inode in the ifile.
    778 	 */
    779 	ino = ip->i_number;
    780 	if (ino == LFS_IFILE_INUM) {
    781 		daddr = fs->lfs_idaddr;
    782 		fs->lfs_idaddr = bp->b_blkno;
    783 	} else {
    784 		LFS_IENTRY(ifp, fs, ino, ibp);
    785 		daddr = ifp->if_daddr;
    786 		ifp->if_daddr = bp->b_blkno;
    787 #ifdef LFS_DEBUG_NEXTFREE
    788 		if(ino > 3 && ifp->if_nextfree) {
    789 			vprint("lfs_writeinode",ITOV(ip));
    790 			printf("lfs_writeinode: updating free ino %d\n",
    791 				ip->i_number);
    792 		}
    793 #endif
    794 		error = VOP_BWRITE(ibp);
    795 	}
    796 
    797 	/*
    798 	 * No need to update segment usage if there was no former inode address
    799 	 * or if the last inode address is in the current partial segment.
    800 	 */
    801 	if (daddr != LFS_UNUSED_DADDR &&
    802 	    !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
    803 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
    804 #ifdef DIAGNOSTIC
    805 		if (sup->su_nbytes < DINODE_SIZE) {
    806 			/* XXX -- Change to a panic. */
    807 			printf("lfs_writeinode: negative bytes (segment %d short by %d)\n",
    808 			       datosn(fs, daddr), (int)DINODE_SIZE - sup->su_nbytes);
    809 			panic("lfs_writeinode: negative bytes");
    810 			sup->su_nbytes = DINODE_SIZE;
    811 		}
    812 #endif
    813 		sup->su_nbytes -= DINODE_SIZE;
    814 		redo_ifile =
    815 			(ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
    816 		error = VOP_BWRITE(bp);
    817 	}
    818 	return (redo_ifile);
    819 }
    820 
    821 int
    822 lfs_gatherblock(sp, bp, sptr)
    823 	struct segment *sp;
    824 	struct buf *bp;
    825 	int *sptr;
    826 {
    827 	struct lfs *fs;
    828 	int version;
    829 
    830 	/*
    831 	 * If full, finish this segment.  We may be doing I/O, so
    832 	 * release and reacquire the splbio().
    833 	 */
    834 #ifdef DIAGNOSTIC
    835 	if (sp->vp == NULL)
    836 		panic ("lfs_gatherblock: Null vp in segment");
    837 #endif
    838 	fs = sp->fs;
    839 	if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
    840 	    sp->seg_bytes_left < bp->b_bcount) {
    841 		if (sptr)
    842 			splx(*sptr);
    843 		lfs_updatemeta(sp);
    844 
    845 		version = sp->fip->fi_version;
    846 		(void) lfs_writeseg(fs, sp);
    847 
    848 		sp->fip->fi_version = version;
    849 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
    850 		/* Add the current file to the segment summary. */
    851 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    852 		sp->sum_bytes_left -=
    853 			sizeof(struct finfo) - sizeof(ufs_daddr_t);
    854 
    855 		if (sptr)
    856 			*sptr = splbio();
    857 		return(1);
    858 	}
    859 
    860 #ifdef DEBUG
    861 	if(bp->b_flags & B_GATHERED) {
    862 		printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
    863 		       sp->fip->fi_ino, bp->b_lblkno);
    864 		return(0);
    865 	}
    866 #endif
    867 	/* Insert into the buffer list, update the FINFO block. */
    868 	bp->b_flags |= B_GATHERED;
    869 	*sp->cbpp++ = bp;
    870 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
    871 
    872 	sp->sum_bytes_left -= sizeof(ufs_daddr_t);
    873 	sp->seg_bytes_left -= bp->b_bcount;
    874 	return(0);
    875 }
    876 
    877 int
    878 lfs_gather(fs, sp, vp, match)
    879 	struct lfs *fs;
    880 	struct segment *sp;
    881 	struct vnode *vp;
    882 	int (*match) __P((struct lfs *, struct buf *));
    883 {
    884 	struct buf *bp;
    885 	int s, count=0;
    886 
    887 	sp->vp = vp;
    888 	s = splbio();
    889 
    890 #ifndef LFS_NO_BACKBUF_HACK
    891 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
    892 #else /* LFS_NO_BACKBUF_HACK */
    893 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
    894 # define	BUF_OFFSET	(((void *)&bp->b_vnbufs.le_next) - (void *)bp)
    895 # define	BACK_BUF(BP)	((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
    896 # define	BEG_OF_LIST	((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
    897 /* Find last buffer. */
    898 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
    899 	    bp = bp->b_vnbufs.le_next);
    900 	for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
    901 #endif /* LFS_NO_BACKBUF_HACK */
    902 		if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
    903 			continue;
    904 		if(vp->v_type == VBLK) {
    905 			/* For block devices, just write the blocks. */
    906 			/* XXX Do we really need to even do this? */
    907 #ifdef DEBUG_LFS
    908 			if(count==0)
    909 				printf("BLK(");
    910 			printf(".");
    911 #endif
    912 			/* Get the block before bwrite, so we don't corrupt the free list */
    913 			bp->b_flags |= B_BUSY;
    914 			bremfree(bp);
    915 			bwrite(bp);
    916 		} else {
    917 #ifdef DIAGNOSTIC
    918 			if (!(bp->b_flags & B_DELWRI))
    919 				panic("lfs_gather: bp not B_DELWRI");
    920 			if (!(bp->b_flags & B_LOCKED)) {
    921 				printf("lfs_gather: lbn %d blk %d not B_LOCKED\n", bp->b_lblkno, bp->b_blkno);
    922 				VOP_PRINT(bp->b_vp);
    923 				panic("lfs_gather: bp not B_LOCKED");
    924 			}
    925 #endif
    926 			if (lfs_gatherblock(sp, bp, &s)) {
    927 				goto loop;
    928 			}
    929 		}
    930 		count++;
    931 	}
    932 	splx(s);
    933 #ifdef DEBUG_LFS
    934 	if(vp->v_type == VBLK && count)
    935 		printf(")\n");
    936 #endif
    937 	lfs_updatemeta(sp);
    938 	sp->vp = NULL;
    939 	return count;
    940 }
    941 
    942 /*
    943  * Update the metadata that points to the blocks listed in the FINFO
    944  * array.
    945  */
    946 void
    947 lfs_updatemeta(sp)
    948 	struct segment *sp;
    949 {
    950 	SEGUSE *sup;
    951 	struct buf *bp;
    952 	struct lfs *fs;
    953 	struct vnode *vp;
    954 	struct indir a[NIADDR + 2], *ap;
    955 	struct inode *ip;
    956 	ufs_daddr_t daddr, lbn, off;
    957 	int error, i, nblocks, num;
    958 
    959 	vp = sp->vp;
    960 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
    961 	if (nblocks < 0)
    962 		panic("This is a bad thing\n");
    963 	if (vp == NULL || nblocks == 0)
    964 		return;
    965 
    966 	/* Sort the blocks. */
    967 	/*
    968 	 * XXX KS - We have to sort even if the blocks come from the
    969 	 * cleaner, because there might be other pending blocks on the
    970 	 * same inode...and if we don't sort, and there are fragments
    971 	 * present, blocks may be written in the wrong place.
    972 	 */
    973 	/* if (!(sp->seg_flags & SEGM_CLEAN)) */
    974 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
    975 
    976 	/*
    977 	 * Record the length of the last block in case it's a fragment.
    978 	 * If there are indirect blocks present, they sort last.  An
    979 	 * indirect block will be lfs_bsize and its presence indicates
    980 	 * that you cannot have fragments.
    981 	 */
    982 	sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
    983 
    984 	/*
    985 	 * Assign disk addresses, and update references to the logical
    986 	 * block and the segment usage information.
    987 	 */
    988 	fs = sp->fs;
    989 	for (i = nblocks; i--; ++sp->start_bpp) {
    990 		lbn = *sp->start_lbp++;
    991 
    992 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
    993 		if((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) {
    994 			printf("lfs_updatemeta: ino %d blk %d has same lbn and daddr\n", VTOI(vp)->i_number, off);
    995 		}
    996 		fs->lfs_offset +=
    997 			fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
    998 		error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
    999 		if (error)
   1000 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
   1001 		ip = VTOI(vp);
   1002 		switch (num) {
   1003 		case 0:
   1004 			ip->i_ffs_db[lbn] = off;
   1005 			break;
   1006 		case 1:
   1007 			ip->i_ffs_ib[a[0].in_off] = off;
   1008 			break;
   1009 		default:
   1010 			ap = &a[num - 1];
   1011 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
   1012 				panic("lfs_updatemeta: bread bno %d",
   1013 				      ap->in_lbn);
   1014 			/*
   1015 			 * Bread may create a new (indirect) block which needs
   1016 			 * to get counted for the inode.
   1017 			 */
   1018 			if (/* bp->b_blkno == -1 && */
   1019 			    !(bp->b_flags & (B_DELWRI|B_DONE))) {
   1020 				ip->i_ffs_blocks += fsbtodb(fs, 1);
   1021 				fs->lfs_bfree -= fragstodb(fs, fs->lfs_frag);
   1022 			}
   1023 			((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
   1024 			VOP_BWRITE(bp);
   1025 		}
   1026 		/* Update segment usage information. */
   1027 		if (daddr != UNASSIGNED && !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
   1028 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
   1029 #ifdef DIAGNOSTIC
   1030 			if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
   1031 				/* XXX -- Change to a panic. */
   1032 				printf("lfs_updatemeta: negative bytes (segment %d short by %ld)\n",
   1033 				       datosn(fs, daddr), (*sp->start_bpp)->b_bcount - sup->su_nbytes);
   1034 				printf("lfs_updatemeta: ino %d, lbn %d, addr = %x\n",
   1035 				       VTOI(sp->vp)->i_number, (*sp->start_bpp)->b_lblkno, daddr);
   1036 				panic("lfs_updatemeta: negative bytes");
   1037 				sup->su_nbytes = (*sp->start_bpp)->b_bcount;
   1038 			}
   1039 #endif
   1040 			sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
   1041 			error = VOP_BWRITE(bp);
   1042 		}
   1043 	}
   1044 }
   1045 
   1046 /*
   1047  * Start a new segment.
   1048  */
   1049 int
   1050 lfs_initseg(fs)
   1051 	struct lfs *fs;
   1052 {
   1053 	struct segment *sp;
   1054 	SEGUSE *sup;
   1055 	SEGSUM *ssp;
   1056 	struct buf *bp;
   1057 	int repeat;
   1058 
   1059 	sp = fs->lfs_sp;
   1060 
   1061 	repeat = 0;
   1062 	/* Advance to the next segment. */
   1063 	if (!LFS_PARTIAL_FITS(fs)) {
   1064 		/* Wake up any cleaning procs waiting on this file system. */
   1065 		wakeup(&lfs_allclean_wakeup);
   1066 		wakeup(&fs->lfs_nextseg);
   1067 		lfs_newseg(fs);
   1068 		repeat = 1;
   1069 		fs->lfs_offset = fs->lfs_curseg;
   1070 		sp->seg_number = datosn(fs, fs->lfs_curseg);
   1071 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
   1072 		/*
   1073 		 * If the segment contains a superblock, update the offset
   1074 		 * and summary address to skip over it.
   1075 		 */
   1076 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1077 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
   1078 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
   1079 			sp->seg_bytes_left -= LFS_SBPAD;
   1080 		}
   1081 		brelse(bp);
   1082 	} else {
   1083 		sp->seg_number = datosn(fs, fs->lfs_curseg);
   1084 		sp->seg_bytes_left = (fs->lfs_dbpseg -
   1085 				      (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
   1086 	}
   1087 	fs->lfs_lastpseg = fs->lfs_offset;
   1088 
   1089 	sp->fs = fs;
   1090 	sp->ibp = NULL;
   1091 	sp->idp = NULL;
   1092 	sp->ninodes = 0;
   1093 
   1094 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
   1095 	sp->cbpp = sp->bpp;
   1096 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
   1097 			       fs->lfs_offset, LFS_SUMMARY_SIZE);
   1098 	sp->segsum = (*sp->cbpp)->b_data;
   1099 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
   1100 	sp->start_bpp = ++sp->cbpp;
   1101 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
   1102 
   1103 	/* Set point to SEGSUM, initialize it. */
   1104 	ssp = sp->segsum;
   1105 	ssp->ss_next = fs->lfs_nextseg;
   1106 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
   1107 	ssp->ss_magic = SS_MAGIC;
   1108 
   1109 	/* Set pointer to first FINFO, initialize it. */
   1110 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
   1111 	sp->fip->fi_nblocks = 0;
   1112 	sp->start_lbp = &sp->fip->fi_blocks[0];
   1113 	sp->fip->fi_lastlength = 0;
   1114 
   1115 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
   1116 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
   1117 
   1118 	return(repeat);
   1119 }
   1120 
   1121 /*
   1122  * Return the next segment to write.
   1123  */
   1124 void
   1125 lfs_newseg(fs)
   1126 	struct lfs *fs;
   1127 {
   1128 	CLEANERINFO *cip;
   1129 	SEGUSE *sup;
   1130 	struct buf *bp;
   1131 	int curseg, isdirty, sn;
   1132 
   1133 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
   1134 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1135 	sup->su_nbytes = 0;
   1136 	sup->su_nsums = 0;
   1137 	sup->su_ninos = 0;
   1138 	(void) VOP_BWRITE(bp);
   1139 
   1140 	LFS_CLEANERINFO(cip, fs, bp);
   1141 	--cip->clean;
   1142 	++cip->dirty;
   1143 	fs->lfs_nclean = cip->clean;
   1144 	(void) VOP_BWRITE(bp);
   1145 
   1146 	fs->lfs_lastseg = fs->lfs_curseg;
   1147 	fs->lfs_curseg = fs->lfs_nextseg;
   1148 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
   1149 		sn = (sn + 1) % fs->lfs_nseg;
   1150 		if (sn == curseg)
   1151 			panic("lfs_nextseg: no clean segments");
   1152 		LFS_SEGENTRY(sup, fs, sn, bp);
   1153 		isdirty = sup->su_flags & SEGUSE_DIRTY;
   1154 		brelse(bp);
   1155 		if (!isdirty)
   1156 			break;
   1157 	}
   1158 
   1159 	++fs->lfs_nactive;
   1160 	fs->lfs_nextseg = sntoda(fs, sn);
   1161 	if(lfs_dostats) {
   1162 		++lfs_stats.segsused;
   1163 	}
   1164 }
   1165 
   1166 int
   1167 lfs_writeseg(fs, sp)
   1168 	struct lfs *fs;
   1169 	struct segment *sp;
   1170 {
   1171 	extern int locked_queue_count;
   1172 	extern long locked_queue_bytes;
   1173 	struct buf **bpp, *bp, *cbp;
   1174 	SEGUSE *sup;
   1175 	SEGSUM *ssp;
   1176 	dev_t i_dev;
   1177 	u_long *datap, *dp;
   1178 	int do_again, i, nblocks, s;
   1179 #ifdef LFS_TRACK_IOS
   1180 	int j;
   1181 #endif
   1182 	int (*strategy)__P((void *));
   1183 	struct vop_strategy_args vop_strategy_a;
   1184 	u_short ninos;
   1185 	struct vnode *devvp;
   1186 	char *p;
   1187 	struct vnode *vn;
   1188 	struct inode *ip;
   1189 #if defined(DEBUG) && defined(LFS_PROPELLER)
   1190 	static int propeller;
   1191 	char propstring[4] = "-\\|/";
   1192 
   1193 	printf("%c\b",propstring[propeller++]);
   1194 	if(propeller==4)
   1195 		propeller = 0;
   1196 #endif
   1197 
   1198 	/*
   1199 	 * If there are no buffers other than the segment summary to write
   1200 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
   1201 	 * even if there aren't any buffers, you need to write the superblock.
   1202 	 */
   1203 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
   1204 		return (0);
   1205 
   1206 #ifdef DEBUG_LFS
   1207 	lfs_check_bpp(fs,sp,__FILE__,__LINE__);
   1208 #endif
   1209 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1210 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
   1211 
   1212 	/* Update the segment usage information. */
   1213 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1214 
   1215 	/* Loop through all blocks, except the segment summary. */
   1216 	for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
   1217 		if((*bpp)->b_vp != devvp)
   1218 			sup->su_nbytes += (*bpp)->b_bcount;
   1219 	}
   1220 
   1221 	ssp = (SEGSUM *)sp->segsum;
   1222 
   1223 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
   1224 	sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
   1225 	/* sup->su_nbytes += LFS_SUMMARY_SIZE; */
   1226 	sup->su_lastmod = time.tv_sec;
   1227 	sup->su_ninos += ninos;
   1228 	++sup->su_nsums;
   1229 
   1230 	do_again = !(bp->b_flags & B_GATHERED);
   1231 	(void)VOP_BWRITE(bp);
   1232 	/*
   1233 	 * Compute checksum across data and then across summary; the first
   1234 	 * block (the summary block) is skipped.  Set the create time here
   1235 	 * so that it's guaranteed to be later than the inode mod times.
   1236 	 *
   1237 	 * XXX
   1238 	 * Fix this to do it inline, instead of malloc/copy.
   1239 	 */
   1240 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
   1241 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
   1242 		if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1243 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
   1244 				panic("lfs_writeseg: copyin failed [1]: ino %d blk %d", VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno);
   1245 		} else {
   1246 			if( !((*bpp)->b_flags & B_CALL) ) {
   1247 				/*
   1248 				 * Before we record data for a checksm,
   1249 				 * make sure the data won't change in between
   1250 				 * the checksum calculation and the write,
   1251 				 * by marking the buffer B_BUSY.  It will
   1252 				 * be freed later by brelse().
   1253 				 */
   1254 			again:
   1255 				s = splbio();
   1256 				if((*bpp)->b_flags & B_BUSY) {
   1257 #ifdef DEBUG
   1258 					printf("lfs_writeseg: avoiding potential data summary corruption for ino %d, lbn %d\n",
   1259 					       VTOI((*bpp)->b_vp)->i_number,
   1260 					       bp->b_lblkno);
   1261 #endif
   1262 					(*bpp)->b_flags |= B_WANTED;
   1263 					tsleep((*bpp), (PRIBIO + 1),
   1264 					       "lfs_writeseg", 0);
   1265 					splx(s);
   1266 					goto again;
   1267 				}
   1268 				(*bpp)->b_flags |= B_BUSY;
   1269 				splx(s);
   1270 			}
   1271 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
   1272 		}
   1273 	}
   1274 	ssp->ss_create = time.tv_sec;
   1275 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
   1276 	ssp->ss_sumsum =
   1277 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
   1278 	free(datap, M_SEGMENT);
   1279 #ifdef DIAGNOSTIC
   1280 	if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
   1281 		panic("lfs_writeseg: No diskspace for summary");
   1282 #endif
   1283 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
   1284 
   1285 	strategy = devvp->v_op[VOFFSET(vop_strategy)];
   1286 
   1287 	/*
   1288 	 * When we simply write the blocks we lose a rotation for every block
   1289 	 * written.  To avoid this problem, we allocate memory in chunks, copy
   1290 	 * the buffers into the chunk and write the chunk.  CHUNKSIZE is the
   1291 	 * largest size I/O devices can handle.
   1292 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
   1293 	 * and brelse the buffer (which will move them to the LRU list).  Add
   1294 	 * the B_CALL flag to the buffer header so we can count I/O's for the
   1295 	 * checkpoints and so we can release the allocated memory.
   1296 	 *
   1297 	 * XXX
   1298 	 * This should be removed if the new virtual memory system allows us to
   1299 	 * easily make the buffers contiguous in kernel memory and if that's
   1300 	 * fast enough.
   1301 	 */
   1302 
   1303 #define CHUNKSIZE MAXPHYS
   1304 
   1305 	if(devvp==NULL)
   1306 		panic("devvp is NULL");
   1307 	for (bpp = sp->bpp,i = nblocks; i;) {
   1308 		cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE);
   1309 		cbp->b_dev = i_dev;
   1310 		cbp->b_flags |= B_ASYNC | B_BUSY;
   1311 		cbp->b_bcount = 0;
   1312 
   1313 #ifdef DIAGNOSTIC
   1314 		if(datosn(fs,(*bpp)->b_blkno + ((*bpp)->b_bcount - 1)/DEV_BSIZE) != datosn(fs,cbp->b_blkno)) {
   1315 			panic("lfs_writeseg: Segment overwrite");
   1316 		}
   1317 #endif
   1318 
   1319 		s = splbio();
   1320 		if(fs->lfs_iocount >= LFS_THROTTLE) {
   1321 			tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
   1322 		}
   1323 		++fs->lfs_iocount;
   1324 #ifdef LFS_TRACK_IOS
   1325 		for(j=0;j<LFS_THROTTLE;j++) {
   1326 			if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) {
   1327 				fs->lfs_pending[j] = cbp->b_blkno;
   1328 				break;
   1329 			}
   1330 		}
   1331 #endif /* LFS_TRACK_IOS */
   1332 		for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
   1333 			bp = *bpp;
   1334 
   1335 			if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
   1336 				break;
   1337 
   1338 			/*
   1339 			 * Fake buffers from the cleaner are marked as B_INVAL.
   1340 			 * We need to copy the data from user space rather than
   1341 			 * from the buffer indicated.
   1342 			 * XXX == what do I do on an error?
   1343 			 */
   1344 			if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1345 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
   1346 					panic("lfs_writeseg: copyin failed [2]");
   1347 			} else
   1348 				bcopy(bp->b_data, p, bp->b_bcount);
   1349 			p += bp->b_bcount;
   1350 			cbp->b_bcount += bp->b_bcount;
   1351 			if (bp->b_flags & B_LOCKED) {
   1352 				--locked_queue_count;
   1353 				locked_queue_bytes -= bp->b_bufsize;
   1354 			}
   1355 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
   1356 					 B_LOCKED | B_GATHERED);
   1357 			vn = bp->b_vp;
   1358 			if (bp->b_flags & B_CALL) {
   1359 				/* if B_CALL, it was created with newbuf */
   1360 				lfs_freebuf(bp);
   1361 			} else {
   1362 				bremfree(bp);
   1363 				bp->b_flags |= B_DONE;
   1364 				if(vn)
   1365 					reassignbuf(bp, vn);
   1366 				brelse(bp);
   1367 			}
   1368 			if(bp->b_flags & B_NEEDCOMMIT) { /* XXX */
   1369 				bp->b_flags &= ~B_NEEDCOMMIT;
   1370 				wakeup(bp);
   1371 			}
   1372 
   1373 			bpp++;
   1374 
   1375 			/*
   1376 			 * If this is the last block for this vnode, but
   1377 			 * there are other blocks on its dirty list,
   1378 			 * set IN_MODIFIED/IN_CLEANING depending on what
   1379 			 * sort of block.  Only do this for our mount point,
   1380 			 * not for, e.g., inode blocks that are attached to
   1381 			 * the devvp.
   1382 			 */
   1383 			if(i>1 && vn && *bpp && (*bpp)->b_vp != vn
   1384 			   && (*bpp)->b_vp && (bp=vn->v_dirtyblkhd.lh_first)!=NULL &&
   1385 			   vn->v_mount == fs->lfs_ivnode->v_mount)
   1386 			{
   1387 				ip = VTOI(vn);
   1388 #ifdef DEBUG_LFS
   1389 				printf("lfs_writeseg: marking ino %d\n",ip->i_number);
   1390 #endif
   1391 		       		if(!(ip->i_flag & (IN_CLEANING|IN_MODIFIED))) {
   1392 					fs->lfs_uinodes++;
   1393 					if(bp->b_flags & B_CALL)
   1394 						ip->i_flag |= IN_CLEANING;
   1395 					else
   1396 						ip->i_flag |= IN_MODIFIED;
   1397 				}
   1398 			}
   1399 			/* if(vn->v_dirtyblkhd.lh_first == NULL) */
   1400 				wakeup(vn);
   1401 		}
   1402 		++cbp->b_vp->v_numoutput;
   1403 		splx(s);
   1404 		/*
   1405 		 * XXXX This is a gross and disgusting hack.  Since these
   1406 		 * buffers are physically addressed, they hang off the
   1407 		 * device vnode (devvp).  As a result, they have no way
   1408 		 * of getting to the LFS superblock or lfs structure to
   1409 		 * keep track of the number of I/O's pending.  So, I am
   1410 		 * going to stuff the fs into the saveaddr field of
   1411 		 * the buffer (yuk).
   1412 		 */
   1413 		cbp->b_saveaddr = (caddr_t)fs;
   1414 		vop_strategy_a.a_desc = VDESC(vop_strategy);
   1415 		vop_strategy_a.a_bp = cbp;
   1416 		(strategy)(&vop_strategy_a);
   1417 	}
   1418 	/*
   1419 	 * XXX
   1420 	 * Vinvalbuf can move locked buffers off the locked queue
   1421 	 * and we have no way of knowing about this.  So, after
   1422 	 * doing a big write, we recalculate how many buffers are
   1423 	 * really still left on the locked queue.
   1424 	 */
   1425 	lfs_countlocked(&locked_queue_count,&locked_queue_bytes);
   1426 	wakeup(&locked_queue_count);
   1427 	if(lfs_dostats) {
   1428 		++lfs_stats.psegwrites;
   1429 		lfs_stats.blocktot += nblocks - 1;
   1430 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
   1431 			++lfs_stats.psyncwrites;
   1432 		if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
   1433 			++lfs_stats.pcleanwrites;
   1434 			lfs_stats.cleanblocks += nblocks - 1;
   1435 		}
   1436 	}
   1437 	return (lfs_initseg(fs) || do_again);
   1438 }
   1439 
   1440 void
   1441 lfs_writesuper(fs, daddr)
   1442 	struct lfs *fs;
   1443 	daddr_t daddr;
   1444 {
   1445 	struct buf *bp;
   1446 	dev_t i_dev;
   1447 	int (*strategy) __P((void *));
   1448 	int s;
   1449 	struct vop_strategy_args vop_strategy_a;
   1450 
   1451 #ifdef LFS_CANNOT_ROLLFW
   1452 	/*
   1453 	 * If we can write one superblock while another is in
   1454 	 * progress, we risk not having a complete checkpoint if we crash.
   1455 	 * So, block here if a superblock write is in progress.
   1456 	 */
   1457 	s = splbio();
   1458 	while(fs->lfs_sbactive) {
   1459 		tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
   1460 	}
   1461 	fs->lfs_sbactive = daddr;
   1462 	splx(s);
   1463 #endif
   1464 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1465 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
   1466 
   1467 	/* Set timestamp of this version of the superblock */
   1468 	fs->lfs_tstamp = time.tv_sec;
   1469 
   1470 	/* Checksum the superblock and copy it into a buffer. */
   1471 	fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
   1472 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD);
   1473 	*(struct dlfs *)bp->b_data = fs->lfs_dlfs;
   1474 
   1475 	bp->b_dev = i_dev;
   1476 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
   1477 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
   1478 	bp->b_iodone = lfs_supercallback;
   1479 	/* XXX KS - same nasty hack as above */
   1480 	bp->b_saveaddr = (caddr_t)fs;
   1481 
   1482 	vop_strategy_a.a_desc = VDESC(vop_strategy);
   1483 	vop_strategy_a.a_bp = bp;
   1484 	s = splbio();
   1485 	++bp->b_vp->v_numoutput;
   1486 	splx(s);
   1487 	(strategy)(&vop_strategy_a);
   1488 }
   1489 
   1490 /*
   1491  * Logical block number match routines used when traversing the dirty block
   1492  * chain.
   1493  */
   1494 int
   1495 lfs_match_fake(fs, bp)
   1496 	struct lfs *fs;
   1497 	struct buf *bp;
   1498 {
   1499 	return (bp->b_flags & B_CALL);
   1500 }
   1501 
   1502 int
   1503 lfs_match_data(fs, bp)
   1504 	struct lfs *fs;
   1505 	struct buf *bp;
   1506 {
   1507 	return (bp->b_lblkno >= 0);
   1508 }
   1509 
   1510 int
   1511 lfs_match_indir(fs, bp)
   1512 	struct lfs *fs;
   1513 	struct buf *bp;
   1514 {
   1515 	int lbn;
   1516 
   1517 	lbn = bp->b_lblkno;
   1518 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
   1519 }
   1520 
   1521 int
   1522 lfs_match_dindir(fs, bp)
   1523 	struct lfs *fs;
   1524 	struct buf *bp;
   1525 {
   1526 	int lbn;
   1527 
   1528 	lbn = bp->b_lblkno;
   1529 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
   1530 }
   1531 
   1532 int
   1533 lfs_match_tindir(fs, bp)
   1534 	struct lfs *fs;
   1535 	struct buf *bp;
   1536 {
   1537 	int lbn;
   1538 
   1539 	lbn = bp->b_lblkno;
   1540 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
   1541 }
   1542 
   1543 /*
   1544  * XXX - The only buffers that are going to hit these functions are the
   1545  * segment write blocks, or the segment summaries, or the superblocks.
   1546  *
   1547  * All of the above are created by lfs_newbuf, and so do not need to be
   1548  * released via brelse.
   1549  */
   1550 void
   1551 lfs_callback(bp)
   1552 	struct buf *bp;
   1553 {
   1554 	struct lfs *fs;
   1555 #ifdef LFS_TRACK_IOS
   1556 	int j;
   1557 #endif
   1558 
   1559 	fs = (struct lfs *)bp->b_saveaddr;
   1560 #ifdef DIAGNOSTIC
   1561 	if (fs->lfs_iocount == 0)
   1562 		panic("lfs_callback: zero iocount\n");
   1563 #endif
   1564 	if (--fs->lfs_iocount < LFS_THROTTLE)
   1565 		wakeup(&fs->lfs_iocount);
   1566 #ifdef LFS_TRACK_IOS
   1567 	for(j=0;j<LFS_THROTTLE;j++) {
   1568 		if(fs->lfs_pending[j]==bp->b_blkno) {
   1569 			fs->lfs_pending[j] = LFS_UNUSED_DADDR;
   1570 			wakeup(&(fs->lfs_pending[j]));
   1571 			break;
   1572 		}
   1573 	}
   1574 #endif /* LFS_TRACK_IOS */
   1575 
   1576 	lfs_freebuf(bp);
   1577 }
   1578 
   1579 void
   1580 lfs_supercallback(bp)
   1581 	struct buf *bp;
   1582 {
   1583 #ifdef LFS_CANNOT_ROLLFW
   1584 	struct lfs *fs;
   1585 
   1586 	fs = (struct lfs *)bp->b_saveaddr;
   1587 	fs->lfs_sbactive=NULL;
   1588 	wakeup(&fs->lfs_sbactive);
   1589 #endif
   1590 	lfs_freebuf(bp);
   1591 }
   1592 
   1593 /*
   1594  * Shellsort (diminishing increment sort) from Data Structures and
   1595  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
   1596  * see also Knuth Vol. 3, page 84.  The increments are selected from
   1597  * formula (8), page 95.  Roughly O(N^3/2).
   1598  */
   1599 /*
   1600  * This is our own private copy of shellsort because we want to sort
   1601  * two parallel arrays (the array of buffer pointers and the array of
   1602  * logical block numbers) simultaneously.  Note that we cast the array
   1603  * of logical block numbers to a unsigned in this routine so that the
   1604  * negative block numbers (meta data blocks) sort AFTER the data blocks.
   1605  */
   1606 
   1607 void
   1608 lfs_shellsort(bp_array, lb_array, nmemb)
   1609 	struct buf **bp_array;
   1610 	ufs_daddr_t *lb_array;
   1611 	register int nmemb;
   1612 {
   1613 	static int __rsshell_increments[] = { 4, 1, 0 };
   1614 	register int incr, *incrp, t1, t2;
   1615 	struct buf *bp_temp;
   1616 	u_long lb_temp;
   1617 
   1618 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
   1619 		for (t1 = incr; t1 < nmemb; ++t1)
   1620 			for (t2 = t1 - incr; t2 >= 0;)
   1621 				if (lb_array[t2] > lb_array[t2 + incr]) {
   1622 					lb_temp = lb_array[t2];
   1623 					lb_array[t2] = lb_array[t2 + incr];
   1624 					lb_array[t2 + incr] = lb_temp;
   1625 					bp_temp = bp_array[t2];
   1626 					bp_array[t2] = bp_array[t2 + incr];
   1627 					bp_array[t2 + incr] = bp_temp;
   1628 					t2 -= incr;
   1629 				} else
   1630 					break;
   1631 }
   1632 
   1633 /*
   1634  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
   1635  */
   1636 int
   1637 lfs_vref(vp)
   1638 	register struct vnode *vp;
   1639 {
   1640 	/*
   1641 	 * If we return 1 here during a flush, we risk vinvalbuf() not
   1642 	 * being able to flush all of the pages from this vnode, which
   1643 	 * will cause it to panic.  So, return 0 if a flush is in progress.
   1644 	 */
   1645 	if (vp->v_flag & VXLOCK) {
   1646 		if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1647 			return 0;
   1648 		}
   1649 		return(1);
   1650 	}
   1651 	return (vget(vp, 0));
   1652 }
   1653 
   1654 /*
   1655  * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
   1656  * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
   1657  */
   1658 void
   1659 lfs_vunref(vp)
   1660 	register struct vnode *vp;
   1661 {
   1662 	/*
   1663 	 * Analogous to lfs_vref, if the node is flushing, fake it.
   1664 	 */
   1665 	if((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1666 		return;
   1667 	}
   1668 
   1669 	simple_lock(&vp->v_interlock);
   1670 #ifdef DIAGNOSTIC
   1671 	if(vp->v_usecount<=0) {
   1672 		printf("lfs_vunref: flags are 0x%lx\n", vp->v_flag);
   1673 		printf("lfs_vunref: usecount = %ld\n", vp->v_usecount);
   1674 		panic("lfs_vunref: v_usecount<0");
   1675 	}
   1676 #endif
   1677 	vp->v_usecount--;
   1678 	if (vp->v_usecount > 0) {
   1679 		simple_unlock(&vp->v_interlock);
   1680 		return;
   1681 	}
   1682 #ifdef DIAGNOSTIC
   1683 	if(VOP_ISLOCKED(vp))
   1684 		panic("lfs_vunref: vnode locked");
   1685 #endif
   1686 	/*
   1687 	 * insert at tail of LRU list
   1688 	 */
   1689 	simple_lock(&vnode_free_list_slock);
   1690 	TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
   1691 	simple_unlock(&vnode_free_list_slock);
   1692 	simple_unlock(&vp->v_interlock);
   1693 }
   1694 
   1695 /*
   1696  * We use this when we have vnodes that were loaded in solely for cleaning.
   1697  * There is no reason to believe that these vnodes will be referenced again
   1698  * soon, since the cleaning process is unrelated to normal filesystem
   1699  * activity.  Putting cleaned vnodes at the tail of the list has the effect
   1700  * of flushing the vnode LRU.  So, put vnodes that were loaded only for
   1701  * cleaning at the head of the list, instead.
   1702  */
   1703 void
   1704 lfs_vunref_head(vp)
   1705 	register struct vnode *vp;
   1706 {
   1707 	simple_lock(&vp->v_interlock);
   1708 #ifdef DIAGNOSTIC
   1709 	if(vp->v_usecount==0) {
   1710 		panic("lfs_vunref: v_usecount<0");
   1711 	}
   1712 #endif
   1713 	vp->v_usecount--;
   1714 	if (vp->v_usecount > 0) {
   1715 		simple_unlock(&vp->v_interlock);
   1716 		return;
   1717 	}
   1718 #ifdef DIAGNOSTIC
   1719 	if(VOP_ISLOCKED(vp))
   1720 		panic("lfs_vunref_head: vnode locked");
   1721 #endif
   1722 	/*
   1723 	 * insert at head of LRU list
   1724 	 */
   1725 	simple_lock(&vnode_free_list_slock);
   1726 	TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
   1727 	simple_unlock(&vnode_free_list_slock);
   1728 	simple_unlock(&vp->v_interlock);
   1729 }
   1730 
   1731