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