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