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