Home | History | Annotate | Line # | Download | only in lfs
lfs_segment.c revision 1.180
      1 /*	$NetBSD: lfs_segment.c,v 1.180 2006/05/18 23:15:09 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 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. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_segment.c	8.10 (Berkeley) 6/10/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_segment.c,v 1.180 2006/05/18 23:15:09 perseant Exp $");
     71 
     72 #ifdef DEBUG
     73 # define vndebug(vp, str) do {						\
     74 	if (VTOI(vp)->i_flag & IN_CLEANING)				\
     75 		DLOG((DLOG_WVNODE, "not writing ino %d because %s (op %d)\n", \
     76 		     VTOI(vp)->i_number, (str), op));			\
     77 } while(0)
     78 #else
     79 # define vndebug(vp, str)
     80 #endif
     81 #define ivndebug(vp, str) \
     82 	DLOG((DLOG_WVNODE, "ino %d: %s\n", VTOI(vp)->i_number, (str)))
     83 
     84 #if defined(_KERNEL_OPT)
     85 #include "opt_ddb.h"
     86 #endif
     87 
     88 #include <sys/param.h>
     89 #include <sys/systm.h>
     90 #include <sys/namei.h>
     91 #include <sys/kernel.h>
     92 #include <sys/resourcevar.h>
     93 #include <sys/file.h>
     94 #include <sys/stat.h>
     95 #include <sys/buf.h>
     96 #include <sys/proc.h>
     97 #include <sys/vnode.h>
     98 #include <sys/mount.h>
     99 #include <sys/kauth.h>
    100 
    101 #include <miscfs/specfs/specdev.h>
    102 #include <miscfs/fifofs/fifo.h>
    103 
    104 #include <ufs/ufs/inode.h>
    105 #include <ufs/ufs/dir.h>
    106 #include <ufs/ufs/ufsmount.h>
    107 #include <ufs/ufs/ufs_extern.h>
    108 
    109 #include <ufs/lfs/lfs.h>
    110 #include <ufs/lfs/lfs_extern.h>
    111 
    112 #include <uvm/uvm.h>
    113 #include <uvm/uvm_extern.h>
    114 
    115 MALLOC_DEFINE(M_SEGMENT, "LFS segment", "Segment for LFS");
    116 
    117 extern int count_lock_queue(void);
    118 extern struct simplelock vnode_free_list_slock;		/* XXX */
    119 extern struct simplelock bqueue_slock;			/* XXX */
    120 
    121 static void lfs_generic_callback(struct buf *, void (*)(struct buf *));
    122 static void lfs_super_aiodone(struct buf *);
    123 static void lfs_cluster_aiodone(struct buf *);
    124 static void lfs_cluster_callback(struct buf *);
    125 
    126 /*
    127  * Determine if it's OK to start a partial in this segment, or if we need
    128  * to go on to a new segment.
    129  */
    130 #define	LFS_PARTIAL_FITS(fs) \
    131 	((fs)->lfs_fsbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
    132 	fragstofsb((fs), (fs)->lfs_frag))
    133 
    134 /*
    135  * Figure out whether we should do a checkpoint write or go ahead with
    136  * an ordinary write.
    137  */
    138 #define LFS_SHOULD_CHECKPOINT(fs, flags) \
    139 	(fs->lfs_nactive > LFS_MAX_ACTIVE ||				\
    140 	 (flags & SEGM_CKP) ||						\
    141 	 fs->lfs_nclean < LFS_MAX_ACTIVE)
    142 
    143 int	 lfs_match_fake(struct lfs *, struct buf *);
    144 void	 lfs_newseg(struct lfs *);
    145 /* XXX ondisk32 */
    146 void	 lfs_shellsort(struct buf **, int32_t *, int, int);
    147 void	 lfs_supercallback(struct buf *);
    148 void	 lfs_updatemeta(struct segment *);
    149 void	 lfs_writesuper(struct lfs *, daddr_t);
    150 int	 lfs_writevnodes(struct lfs *fs, struct mount *mp,
    151 	    struct segment *sp, int dirops);
    152 
    153 int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
    154 int	lfs_writeindir = 1;		/* whether to flush indir on non-ckp */
    155 int	lfs_clean_vnhead = 0;		/* Allow freeing to head of vn list */
    156 int	lfs_dirvcount = 0;		/* # active dirops */
    157 
    158 /* Statistics Counters */
    159 int lfs_dostats = 1;
    160 struct lfs_stats lfs_stats;
    161 
    162 /* op values to lfs_writevnodes */
    163 #define	VN_REG		0
    164 #define	VN_DIROP	1
    165 #define	VN_EMPTY	2
    166 #define VN_CLEAN	3
    167 
    168 /*
    169  * XXX KS - Set modification time on the Ifile, so the cleaner can
    170  * read the fs mod time off of it.  We don't set IN_UPDATE here,
    171  * since we don't really need this to be flushed to disk (and in any
    172  * case that wouldn't happen to the Ifile until we checkpoint).
    173  */
    174 void
    175 lfs_imtime(struct lfs *fs)
    176 {
    177 	struct timespec ts;
    178 	struct inode *ip;
    179 
    180 	ASSERT_MAYBE_SEGLOCK(fs);
    181 	nanotime(&ts);
    182 	ip = VTOI(fs->lfs_ivnode);
    183 	ip->i_ffs1_mtime = ts.tv_sec;
    184 	ip->i_ffs1_mtimensec = ts.tv_nsec;
    185 }
    186 
    187 /*
    188  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
    189  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
    190  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
    191  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
    192  */
    193 
    194 #define IS_FLUSHING(fs,vp)  ((fs)->lfs_flushvp == (vp))
    195 
    196 int
    197 lfs_vflush(struct vnode *vp)
    198 {
    199 	struct inode *ip;
    200 	struct lfs *fs;
    201 	struct segment *sp;
    202 	struct buf *bp, *nbp, *tbp, *tnbp;
    203 	int error, s;
    204 	int flushed;
    205 	int relock;
    206 
    207 	ip = VTOI(vp);
    208 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
    209 	relock = 0;
    210 
    211     top:
    212 	ASSERT_NO_SEGLOCK(fs);
    213 	if (ip->i_flag & IN_CLEANING) {
    214 		ivndebug(vp,"vflush/in_cleaning");
    215 		LFS_CLR_UINO(ip, IN_CLEANING);
    216 		LFS_SET_UINO(ip, IN_MODIFIED);
    217 
    218 		/*
    219 		 * Toss any cleaning buffers that have real counterparts
    220 		 * to avoid losing new data.
    221 		 */
    222 		s = splbio();
    223 		for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    224 			nbp = LIST_NEXT(bp, b_vnbufs);
    225 			if (!LFS_IS_MALLOC_BUF(bp))
    226 				continue;
    227 			/*
    228 			 * Look for pages matching the range covered
    229 			 * by cleaning blocks.  It's okay if more dirty
    230 			 * pages appear, so long as none disappear out
    231 			 * from under us.
    232 			 */
    233 			if (bp->b_lblkno > 0 && vp->v_type == VREG &&
    234 			    vp != fs->lfs_ivnode) {
    235 				struct vm_page *pg;
    236 				voff_t off;
    237 
    238 				simple_lock(&vp->v_interlock);
    239 				for (off = lblktosize(fs, bp->b_lblkno);
    240 				     off < lblktosize(fs, bp->b_lblkno + 1);
    241 				     off += PAGE_SIZE) {
    242 					pg = uvm_pagelookup(&vp->v_uobj, off);
    243 					if (pg == NULL)
    244 						continue;
    245 					if ((pg->flags & PG_CLEAN) == 0 ||
    246 					    pmap_is_modified(pg)) {
    247 						fs->lfs_avail += btofsb(fs,
    248 							bp->b_bcount);
    249 						wakeup(&fs->lfs_avail);
    250 						lfs_freebuf(fs, bp);
    251 						bp = NULL;
    252 						simple_unlock(&vp->v_interlock);
    253 						goto nextbp;
    254 					}
    255 				}
    256 				simple_unlock(&vp->v_interlock);
    257 			}
    258 			for (tbp = LIST_FIRST(&vp->v_dirtyblkhd); tbp;
    259 			    tbp = tnbp)
    260 			{
    261 				tnbp = LIST_NEXT(tbp, b_vnbufs);
    262 				if (tbp->b_vp == bp->b_vp
    263 				   && tbp->b_lblkno == bp->b_lblkno
    264 				   && tbp != bp)
    265 				{
    266 					fs->lfs_avail += btofsb(fs,
    267 						bp->b_bcount);
    268 					wakeup(&fs->lfs_avail);
    269 					lfs_freebuf(fs, bp);
    270 					bp = NULL;
    271 					break;
    272 				}
    273 			}
    274 		    nextbp:
    275 			;
    276 		}
    277 		splx(s);
    278 	}
    279 
    280 	/* If the node is being written, wait until that is done */
    281 	simple_lock(&vp->v_interlock);
    282 	s = splbio();
    283 	if (WRITEINPROG(vp)) {
    284 		ivndebug(vp,"vflush/writeinprog");
    285 		ltsleep(vp, (PRIBIO+1), "lfs_vw", 0, &vp->v_interlock);
    286 	}
    287 	splx(s);
    288 	simple_unlock(&vp->v_interlock);
    289 
    290 	/* Protect against VXLOCK deadlock in vinvalbuf() */
    291 	lfs_seglock(fs, SEGM_SYNC);
    292 
    293 	/* If we're supposed to flush a freed inode, just toss it */
    294 	if (ip->i_lfs_iflags & LFSI_DELETED) {
    295 		DLOG((DLOG_VNODE, "lfs_vflush: ino %d freed, not flushing\n",
    296 		      ip->i_number));
    297 		s = splbio();
    298 		/* Drain v_numoutput */
    299 		simple_lock(&global_v_numoutput_slock);
    300 		while (vp->v_numoutput > 0) {
    301 			vp->v_flag |= VBWAIT;
    302 			ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vf4", 0,
    303 				&global_v_numoutput_slock);
    304 		}
    305 		simple_unlock(&global_v_numoutput_slock);
    306 		KASSERT(vp->v_numoutput == 0);
    307 
    308 		for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    309 			nbp = LIST_NEXT(bp, b_vnbufs);
    310 
    311 			KASSERT((bp->b_flags & B_GATHERED) == 0);
    312 			if (bp->b_flags & B_DELWRI) { /* XXX always true? */
    313 				fs->lfs_avail += btofsb(fs, bp->b_bcount);
    314 				wakeup(&fs->lfs_avail);
    315 			}
    316 			/* Copied from lfs_writeseg */
    317 			if (bp->b_flags & B_CALL) {
    318 				biodone(bp);
    319 			} else {
    320 				bremfree(bp);
    321 				LFS_UNLOCK_BUF(bp);
    322 				bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
    323 					 B_GATHERED);
    324 				bp->b_flags |= B_DONE;
    325 				reassignbuf(bp, vp);
    326 				brelse(bp);
    327 			}
    328 		}
    329 		splx(s);
    330 		LFS_CLR_UINO(ip, IN_CLEANING);
    331 		LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
    332 		ip->i_flag &= ~IN_ALLMOD;
    333 		DLOG((DLOG_VNODE, "lfs_vflush: done not flushing ino %d\n",
    334 		      ip->i_number));
    335 		lfs_segunlock(fs);
    336 
    337 		KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL);
    338 
    339 		return 0;
    340 	}
    341 
    342 	fs->lfs_flushvp = vp;
    343 	if (LFS_SHOULD_CHECKPOINT(fs, fs->lfs_sp->seg_flags)) {
    344 		error = lfs_segwrite(vp->v_mount, SEGM_CKP | SEGM_SYNC);
    345 		fs->lfs_flushvp = NULL;
    346 		KASSERT(fs->lfs_flushvp_fakevref == 0);
    347 		lfs_segunlock(fs);
    348 
    349 		/* Make sure that any pending buffers get written */
    350 		s = splbio();
    351 		simple_lock(&global_v_numoutput_slock);
    352 		while (vp->v_numoutput > 0) {
    353 			vp->v_flag |= VBWAIT;
    354 			ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vf3", 0,
    355 				&global_v_numoutput_slock);
    356 		}
    357 		simple_unlock(&global_v_numoutput_slock);
    358 		splx(s);
    359 
    360 		KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL);
    361 		KASSERT(vp->v_numoutput == 0);
    362 
    363 		return error;
    364 	}
    365 	sp = fs->lfs_sp;
    366 
    367 	flushed = 0;
    368 	if (VPISEMPTY(vp)) {
    369 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
    370 		++flushed;
    371 	} else if ((ip->i_flag & IN_CLEANING) &&
    372 		  (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
    373 		ivndebug(vp,"vflush/clean");
    374 		lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
    375 		++flushed;
    376 	} else if (lfs_dostats) {
    377 		if (!VPISEMPTY(vp) || (VTOI(vp)->i_flag & IN_ALLMOD))
    378 			++lfs_stats.vflush_invoked;
    379 		ivndebug(vp,"vflush");
    380 	}
    381 
    382 #ifdef DIAGNOSTIC
    383 	if (vp->v_flag & VDIROP) {
    384 		DLOG((DLOG_VNODE, "lfs_vflush: flushing VDIROP\n"));
    385 		/* panic("lfs_vflush: VDIROP being flushed...this can\'t happen"); */
    386 	}
    387 	if (vp->v_usecount < 0) {
    388 		printf("usecount=%ld\n", (long)vp->v_usecount);
    389 		panic("lfs_vflush: usecount<0");
    390 	}
    391 #endif
    392 
    393 	do {
    394 		do {
    395 			if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) {
    396 				relock = lfs_writefile(fs, sp, vp);
    397 				if (relock) {
    398 					/*
    399 					 * Might have to wait for the
    400 					 * cleaner to run; but we're
    401 					 * still not done with this vnode.
    402 					 */
    403 					lfs_writeinode(fs, sp, ip);
    404 					LFS_SET_UINO(ip, IN_MODIFIED);
    405 					lfs_writeseg(fs, sp);
    406 					lfs_segunlock(fs);
    407 					lfs_segunlock_relock(fs);
    408 					goto top;
    409 				}
    410 			}
    411 			/*
    412 			 * If we begin a new segment in the middle of writing
    413 			 * the Ifile, it creates an inconsistent checkpoint,
    414 			 * since the Ifile information for the new segment
    415 			 * is not up-to-date.  Take care of this here by
    416 			 * sending the Ifile through again in case there
    417 			 * are newly dirtied blocks.  But wait, there's more!
    418 			 * This second Ifile write could *also* cross a segment
    419 			 * boundary, if the first one was large.  The second
    420 			 * one is guaranteed to be no more than 8 blocks,
    421 			 * though (two segment blocks and supporting indirects)
    422 			 * so the third write *will not* cross the boundary.
    423 			 */
    424 			if (vp == fs->lfs_ivnode) {
    425 				lfs_writefile(fs, sp, vp);
    426 				lfs_writefile(fs, sp, vp);
    427 			}
    428 		} while (lfs_writeinode(fs, sp, ip));
    429 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
    430 
    431 	if (lfs_dostats) {
    432 		++lfs_stats.nwrites;
    433 		if (sp->seg_flags & SEGM_SYNC)
    434 			++lfs_stats.nsync_writes;
    435 		if (sp->seg_flags & SEGM_CKP)
    436 			++lfs_stats.ncheckpoints;
    437 	}
    438 	/*
    439 	 * If we were called from somewhere that has already held the seglock
    440 	 * (e.g., lfs_markv()), the lfs_segunlock will not wait for
    441 	 * the write to complete because we are still locked.
    442 	 * Since lfs_vflush() must return the vnode with no dirty buffers,
    443 	 * we must explicitly wait, if that is the case.
    444 	 *
    445 	 * We compare the iocount against 1, not 0, because it is
    446 	 * artificially incremented by lfs_seglock().
    447 	 */
    448 	simple_lock(&fs->lfs_interlock);
    449 	if (fs->lfs_seglock > 1) {
    450 		while (fs->lfs_iocount > 1)
    451 			(void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
    452 				     "lfs_vflush", 0, &fs->lfs_interlock);
    453 	}
    454 	simple_unlock(&fs->lfs_interlock);
    455 
    456 	lfs_segunlock(fs);
    457 
    458 	/* Wait for these buffers to be recovered by aiodoned */
    459 	s = splbio();
    460 	simple_lock(&global_v_numoutput_slock);
    461 	while (vp->v_numoutput > 0) {
    462 		vp->v_flag |= VBWAIT;
    463 		ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vf2", 0,
    464 			&global_v_numoutput_slock);
    465 	}
    466 	simple_unlock(&global_v_numoutput_slock);
    467 	splx(s);
    468 
    469 	KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL);
    470 	KASSERT(vp->v_numoutput == 0);
    471 
    472 	fs->lfs_flushvp = NULL;
    473 	KASSERT(fs->lfs_flushvp_fakevref == 0);
    474 
    475 	return (0);
    476 }
    477 
    478 int
    479 lfs_writevnodes(struct lfs *fs, struct mount *mp, struct segment *sp, int op)
    480 {
    481 	struct inode *ip;
    482 	struct vnode *vp, *nvp;
    483 	int inodes_written = 0, only_cleaning;
    484 	int error = 0;
    485 
    486 	ASSERT_SEGLOCK(fs);
    487 #ifndef LFS_NO_BACKVP_HACK
    488 	/* BEGIN HACK */
    489 #define	VN_OFFSET	\
    490 	(((caddr_t)&LIST_NEXT(vp, v_mntvnodes)) - (caddr_t)vp)
    491 #define	BACK_VP(VP)	\
    492 	((struct vnode *)(((caddr_t)(VP)->v_mntvnodes.le_prev) - VN_OFFSET))
    493 #define	BEG_OF_VLIST	\
    494 	((struct vnode *)(((caddr_t)&LIST_FIRST(&mp->mnt_vnodelist)) \
    495 	- VN_OFFSET))
    496 
    497 	/* Find last vnode. */
    498  loop:	for (vp = LIST_FIRST(&mp->mnt_vnodelist);
    499 	     vp && LIST_NEXT(vp, v_mntvnodes) != NULL;
    500 	     vp = LIST_NEXT(vp, v_mntvnodes));
    501 	for (; vp && vp != BEG_OF_VLIST; vp = nvp) {
    502 		nvp = BACK_VP(vp);
    503 #else
    504 	loop:
    505 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    506 		nvp = LIST_NEXT(vp, v_mntvnodes);
    507 #endif
    508 		/*
    509 		 * If the vnode that we are about to sync is no longer
    510 		 * associated with this mount point, start over.
    511 		 */
    512 		if (vp->v_mount != mp) {
    513 			DLOG((DLOG_VNODE, "lfs_writevnodes: starting over\n"));
    514 			/*
    515 			 * After this, pages might be busy
    516 			 * due to our own previous putpages.
    517 			 * Start actual segment write here to avoid deadlock.
    518 			 */
    519 			(void)lfs_writeseg(fs, sp);
    520 			goto loop;
    521 		}
    522 
    523 		if (vp->v_type == VNON) {
    524 			continue;
    525 		}
    526 
    527 		ip = VTOI(vp);
    528 		if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
    529 		    (op != VN_DIROP && op != VN_CLEAN &&
    530 		    (vp->v_flag & VDIROP))) {
    531 			vndebug(vp,"dirop");
    532 			continue;
    533 		}
    534 
    535 		if (op == VN_EMPTY && !VPISEMPTY(vp)) {
    536 			vndebug(vp,"empty");
    537 			continue;
    538 		}
    539 
    540 		if (op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
    541 		   && vp != fs->lfs_flushvp
    542 		   && !(ip->i_flag & IN_CLEANING)) {
    543 			vndebug(vp,"cleaning");
    544 			continue;
    545 		}
    546 
    547 		if (lfs_vref(vp)) {
    548 			vndebug(vp,"vref");
    549 			continue;
    550 		}
    551 
    552 		only_cleaning = 0;
    553 		/*
    554 		 * Write the inode/file if dirty and it's not the IFILE.
    555 		 */
    556 		if ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp)) {
    557 			only_cleaning =
    558 			    ((ip->i_flag & IN_ALLMOD) == IN_CLEANING);
    559 
    560 			if (ip->i_number != LFS_IFILE_INUM) {
    561 				error = lfs_writefile(fs, sp, vp);
    562 				if (error) {
    563 					lfs_vunref(vp);
    564 					if (error == EAGAIN) {
    565 						/*
    566 						 * This error from lfs_putpages
    567 						 * indicates we need to drop
    568 						 * the segment lock and start
    569 						 * over after the cleaner has
    570 						 * had a chance to run.
    571 						 */
    572 						lfs_writeinode(fs, sp, ip);
    573 						lfs_writeseg(fs, sp);
    574 						if (!VPISEMPTY(vp) &&
    575 						    !WRITEINPROG(vp) &&
    576 						    !(ip->i_flag & IN_ALLMOD))
    577 							LFS_SET_UINO(ip, IN_MODIFIED);
    578 						break;
    579 					}
    580 					error = 0; /* XXX not quite right */
    581 					continue;
    582 				}
    583 
    584 				if (!VPISEMPTY(vp)) {
    585 					if (WRITEINPROG(vp)) {
    586 						ivndebug(vp,"writevnodes/write2");
    587 					} else if (!(ip->i_flag & IN_ALLMOD)) {
    588 						LFS_SET_UINO(ip, IN_MODIFIED);
    589 					}
    590 				}
    591 				(void) lfs_writeinode(fs, sp, ip);
    592 				inodes_written++;
    593 			}
    594 		}
    595 
    596 		if (lfs_clean_vnhead && only_cleaning)
    597 			lfs_vunref_head(vp);
    598 		else
    599 			lfs_vunref(vp);
    600 	}
    601 	return error;
    602 }
    603 
    604 /*
    605  * Do a checkpoint.
    606  */
    607 int
    608 lfs_segwrite(struct mount *mp, int flags)
    609 {
    610 	struct buf *bp;
    611 	struct inode *ip;
    612 	struct lfs *fs;
    613 	struct segment *sp;
    614 	struct vnode *vp;
    615 	SEGUSE *segusep;
    616 	int do_ckp, did_ckp, error, s;
    617 	unsigned n, segleft, maxseg, sn, i, curseg;
    618 	int writer_set = 0;
    619 	int dirty;
    620 	int redo;
    621 	int um_error;
    622 
    623 	fs = VFSTOUFS(mp)->um_lfs;
    624 	ASSERT_MAYBE_SEGLOCK(fs);
    625 
    626 	if (fs->lfs_ronly)
    627 		return EROFS;
    628 
    629 	lfs_imtime(fs);
    630 
    631 	/*
    632 	 * Allocate a segment structure and enough space to hold pointers to
    633 	 * the maximum possible number of buffers which can be described in a
    634 	 * single summary block.
    635 	 */
    636 	do_ckp = LFS_SHOULD_CHECKPOINT(fs, flags);
    637 
    638 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
    639 	sp = fs->lfs_sp;
    640 
    641 	/*
    642 	 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
    643 	 * in which case we have to flush *all* buffers off of this vnode.
    644 	 * We don't care about other nodes, but write any non-dirop nodes
    645 	 * anyway in anticipation of another getnewvnode().
    646 	 *
    647 	 * If we're cleaning we only write cleaning and ifile blocks, and
    648 	 * no dirops, since otherwise we'd risk corruption in a crash.
    649 	 */
    650 	if (sp->seg_flags & SEGM_CLEAN)
    651 		lfs_writevnodes(fs, mp, sp, VN_CLEAN);
    652 	else if (!(sp->seg_flags & SEGM_FORCE_CKP)) {
    653 		do {
    654 			um_error = lfs_writevnodes(fs, mp, sp, VN_REG);
    655 			if (!fs->lfs_dirops || !fs->lfs_flushvp) {
    656 				if (!writer_set) {
    657 					lfs_writer_enter(fs, "lfs writer");
    658 					writer_set = 1;
    659 				}
    660 				error = lfs_writevnodes(fs, mp, sp, VN_DIROP);
    661 				if (um_error == 0)
    662 					um_error = error;
    663 				/* In case writevnodes errored out */
    664 				lfs_flush_dirops(fs);
    665 				((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
    666 				lfs_finalize_fs_seguse(fs);
    667 			}
    668 			if (do_ckp && um_error) {
    669 				lfs_segunlock_relock(fs);
    670 				sp = fs->lfs_sp;
    671 			}
    672 		} while (do_ckp && um_error != 0);
    673 	}
    674 
    675 	/*
    676 	 * If we are doing a checkpoint, mark everything since the
    677 	 * last checkpoint as no longer ACTIVE.
    678 	 */
    679 	if (do_ckp) {
    680 		segleft = fs->lfs_nseg;
    681 		curseg = 0;
    682 		for (n = 0; n < fs->lfs_segtabsz; n++) {
    683 			dirty = 0;
    684 			if (bread(fs->lfs_ivnode,
    685 			    fs->lfs_cleansz + n, fs->lfs_bsize, NOCRED, &bp))
    686 				panic("lfs_segwrite: ifile read");
    687 			segusep = (SEGUSE *)bp->b_data;
    688 			maxseg = min(segleft, fs->lfs_sepb);
    689 			for (i = 0; i < maxseg; i++) {
    690 				sn = curseg + i;
    691 				if (sn != dtosn(fs, fs->lfs_curseg) &&
    692 				    segusep->su_flags & SEGUSE_ACTIVE) {
    693 					segusep->su_flags &= ~SEGUSE_ACTIVE;
    694 					--fs->lfs_nactive;
    695 					++dirty;
    696 				}
    697 				fs->lfs_suflags[fs->lfs_activesb][sn] =
    698 					segusep->su_flags;
    699 				if (fs->lfs_version > 1)
    700 					++segusep;
    701 				else
    702 					segusep = (SEGUSE *)
    703 						((SEGUSE_V1 *)segusep + 1);
    704 			}
    705 
    706 			if (dirty)
    707 				error = LFS_BWRITE_LOG(bp); /* Ifile */
    708 			else
    709 				brelse(bp);
    710 			segleft -= fs->lfs_sepb;
    711 			curseg += fs->lfs_sepb;
    712 		}
    713 	}
    714 
    715 	LOCK_ASSERT(LFS_SEGLOCK_HELD(fs));
    716 
    717 	did_ckp = 0;
    718 	if (do_ckp || fs->lfs_doifile) {
    719 		vp = fs->lfs_ivnode;
    720 		vn_lock(vp, LK_EXCLUSIVE);
    721 		do {
    722 #ifdef DEBUG
    723 			LFS_ENTER_LOG("pretend", __FILE__, __LINE__, 0, 0, curproc->p_pid);
    724 #endif
    725 			simple_lock(&fs->lfs_interlock);
    726 			fs->lfs_flags &= ~LFS_IFDIRTY;
    727 			simple_unlock(&fs->lfs_interlock);
    728 
    729 			ip = VTOI(vp);
    730 
    731 			if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) {
    732 				/*
    733 				 * Ifile has no pages, so we don't need
    734 				 * to check error return here.
    735 				 */
    736 				lfs_writefile(fs, sp, vp);
    737 				/*
    738 				 * Ensure the Ifile takes the current segment
    739 				 * into account.  See comment in lfs_vflush.
    740 				 */
    741 				lfs_writefile(fs, sp, vp);
    742 				lfs_writefile(fs, sp, vp);
    743 			}
    744 
    745 			if (ip->i_flag & IN_ALLMOD)
    746 				++did_ckp;
    747 			redo = lfs_writeinode(fs, sp, ip);
    748 			redo += lfs_writeseg(fs, sp);
    749 			simple_lock(&fs->lfs_interlock);
    750 			redo += (fs->lfs_flags & LFS_IFDIRTY);
    751 			simple_unlock(&fs->lfs_interlock);
    752 		} while (redo && do_ckp);
    753 
    754 		/*
    755 		 * Unless we are unmounting, the Ifile may continue to have
    756 		 * dirty blocks even after a checkpoint, due to changes to
    757 		 * inodes' atime.  If we're checkpointing, it's "impossible"
    758 		 * for other parts of the Ifile to be dirty after the loop
    759 		 * above, since we hold the segment lock.
    760 		 */
    761 		s = splbio();
    762 		if (LIST_EMPTY(&vp->v_dirtyblkhd)) {
    763 			LFS_CLR_UINO(ip, IN_ALLMOD);
    764 		}
    765 #ifdef DIAGNOSTIC
    766 		else if (do_ckp) {
    767 			int do_panic = 0;
    768 			LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
    769 				if (bp->b_lblkno < fs->lfs_cleansz +
    770 				    fs->lfs_segtabsz &&
    771 				    !(bp->b_flags & B_GATHERED)) {
    772 					printf("ifile lbn %ld still dirty (flags %lx)\n",
    773 						(long)bp->b_lblkno,
    774 						(long)bp->b_flags);
    775 					++do_panic;
    776 				}
    777 			}
    778 			if (do_panic)
    779 				panic("dirty blocks");
    780 		}
    781 #endif
    782 		splx(s);
    783 		VOP_UNLOCK(vp, 0);
    784 	} else {
    785 		(void) lfs_writeseg(fs, sp);
    786 	}
    787 
    788 	/* Note Ifile no longer needs to be written */
    789 	fs->lfs_doifile = 0;
    790 	if (writer_set)
    791 		lfs_writer_leave(fs);
    792 
    793 	/*
    794 	 * If we didn't write the Ifile, we didn't really do anything.
    795 	 * That means that (1) there is a checkpoint on disk and (2)
    796 	 * nothing has changed since it was written.
    797 	 *
    798 	 * Take the flags off of the segment so that lfs_segunlock
    799 	 * doesn't have to write the superblock either.
    800 	 */
    801 	if (do_ckp && !did_ckp) {
    802 		sp->seg_flags &= ~SEGM_CKP;
    803 	}
    804 
    805 	if (lfs_dostats) {
    806 		++lfs_stats.nwrites;
    807 		if (sp->seg_flags & SEGM_SYNC)
    808 			++lfs_stats.nsync_writes;
    809 		if (sp->seg_flags & SEGM_CKP)
    810 			++lfs_stats.ncheckpoints;
    811 	}
    812 	lfs_segunlock(fs);
    813 	return (0);
    814 }
    815 
    816 /*
    817  * Write the dirty blocks associated with a vnode.
    818  */
    819 int
    820 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp)
    821 {
    822 	struct buf *bp;
    823 	struct finfo *fip;
    824 	struct inode *ip;
    825 	IFILE *ifp;
    826 	int i, frag, vers;
    827 	int error;
    828 
    829 	ASSERT_SEGLOCK(fs);
    830 	error = 0;
    831 	ip = VTOI(vp);
    832 
    833 	fip = sp->fip;
    834 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
    835 	vers = ifp->if_version;
    836 	brelse(bp);
    837 	lfs_acquire_finfo(fs, ip->i_number, vers);
    838 
    839 	if (vp->v_flag & VDIROP)
    840 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
    841 
    842 	if (sp->seg_flags & SEGM_CLEAN) {
    843 		lfs_gather(fs, sp, vp, lfs_match_fake);
    844 		/*
    845 		 * For a file being flushed, we need to write *all* blocks.
    846 		 * This means writing the cleaning blocks first, and then
    847 		 * immediately following with any non-cleaning blocks.
    848 		 * The same is true of the Ifile since checkpoints assume
    849 		 * that all valid Ifile blocks are written.
    850 		 */
    851 		if (IS_FLUSHING(fs, vp) || vp == fs->lfs_ivnode) {
    852 			lfs_gather(fs, sp, vp, lfs_match_data);
    853 			/*
    854 			 * Don't call VOP_PUTPAGES: if we're flushing,
    855 			 * we've already done it, and the Ifile doesn't
    856 			 * use the page cache.
    857 			 */
    858 		}
    859 	} else {
    860 		lfs_gather(fs, sp, vp, lfs_match_data);
    861 		/*
    862 		 * If we're flushing, we've already called VOP_PUTPAGES
    863 		 * so don't do it again.  Otherwise, we want to write
    864 		 * everything we've got.
    865 		 */
    866 		if (!IS_FLUSHING(fs, vp)) {
    867 			simple_lock(&vp->v_interlock);
    868 			error = VOP_PUTPAGES(vp, 0, 0,
    869 				PGO_CLEANIT | PGO_ALLPAGES | PGO_LOCKED);
    870 		}
    871 	}
    872 
    873 	/*
    874 	 * It may not be necessary to write the meta-data blocks at this point,
    875 	 * as the roll-forward recovery code should be able to reconstruct the
    876 	 * list.
    877 	 *
    878 	 * We have to write them anyway, though, under two conditions: (1) the
    879 	 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
    880 	 * checkpointing.
    881 	 *
    882 	 * BUT if we are cleaning, we might have indirect blocks that refer to
    883 	 * new blocks not being written yet, in addition to fragments being
    884 	 * moved out of a cleaned segment.  If that is the case, don't
    885 	 * write the indirect blocks, or the finfo will have a small block
    886 	 * in the middle of it!
    887 	 * XXX in this case isn't the inode size wrong too?
    888 	 */
    889 	frag = 0;
    890 	if (sp->seg_flags & SEGM_CLEAN) {
    891 		for (i = 0; i < NDADDR; i++)
    892 			if (ip->i_lfs_fragsize[i] > 0 &&
    893 			    ip->i_lfs_fragsize[i] < fs->lfs_bsize)
    894 				++frag;
    895 	}
    896 #ifdef DIAGNOSTIC
    897 	if (frag > 1)
    898 		panic("lfs_writefile: more than one fragment!");
    899 #endif
    900 	if (IS_FLUSHING(fs, vp) ||
    901 	    (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) {
    902 		lfs_gather(fs, sp, vp, lfs_match_indir);
    903 		lfs_gather(fs, sp, vp, lfs_match_dindir);
    904 		lfs_gather(fs, sp, vp, lfs_match_tindir);
    905 	}
    906 	fip = sp->fip;
    907 	lfs_release_finfo(fs);
    908 
    909 	return error;
    910 }
    911 
    912 int
    913 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
    914 {
    915 	struct buf *bp, *ibp;
    916 	struct ufs1_dinode *cdp;
    917 	IFILE *ifp;
    918 	SEGUSE *sup;
    919 	daddr_t daddr;
    920 	int32_t *daddrp;	/* XXX ondisk32 */
    921 	ino_t ino;
    922 	int error, i, ndx, fsb = 0;
    923 	int redo_ifile = 0;
    924 	int gotblk = 0;
    925 
    926 	ASSERT_SEGLOCK(fs);
    927 	if (!(ip->i_flag & IN_ALLMOD))
    928 		return (0);
    929 
    930 	/* Allocate a new inode block if necessary. */
    931 	if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
    932 	    sp->ibp == NULL) {
    933 		/* Allocate a new segment if necessary. */
    934 		if (sp->seg_bytes_left < fs->lfs_ibsize ||
    935 		    sp->sum_bytes_left < sizeof(int32_t))
    936 			(void) lfs_writeseg(fs, sp);
    937 
    938 		/* Get next inode block. */
    939 		daddr = fs->lfs_offset;
    940 		fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
    941 		sp->ibp = *sp->cbpp++ =
    942 			getblk(VTOI(fs->lfs_ivnode)->i_devvp,
    943 			    fsbtodb(fs, daddr), fs->lfs_ibsize, 0, 0);
    944 		gotblk++;
    945 
    946 		/* Zero out inode numbers */
    947 		for (i = 0; i < INOPB(fs); ++i)
    948 			((struct ufs1_dinode *)sp->ibp->b_data)[i].di_inumber =
    949 			    0;
    950 
    951 		++sp->start_bpp;
    952 		fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
    953 		/* Set remaining space counters. */
    954 		sp->seg_bytes_left -= fs->lfs_ibsize;
    955 		sp->sum_bytes_left -= sizeof(int32_t);
    956 		ndx = fs->lfs_sumsize / sizeof(int32_t) -
    957 			sp->ninodes / INOPB(fs) - 1;
    958 		((int32_t *)(sp->segsum))[ndx] = daddr;
    959 	}
    960 
    961 	/* Update the inode times and copy the inode onto the inode page. */
    962 	/* XXX kludge --- don't redirty the ifile just to put times on it */
    963 	if (ip->i_number != LFS_IFILE_INUM)
    964 		LFS_ITIMES(ip, NULL, NULL, NULL);
    965 
    966 	/*
    967 	 * If this is the Ifile, and we've already written the Ifile in this
    968 	 * partial segment, just overwrite it (it's not on disk yet) and
    969 	 * continue.
    970 	 *
    971 	 * XXX we know that the bp that we get the second time around has
    972 	 * already been gathered.
    973 	 */
    974 	if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
    975 		*(sp->idp) = *ip->i_din.ffs1_din;
    976 		ip->i_lfs_osize = ip->i_size;
    977 		return 0;
    978 	}
    979 
    980 	bp = sp->ibp;
    981 	cdp = ((struct ufs1_dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
    982 	*cdp = *ip->i_din.ffs1_din;
    983 
    984 	/* We can finish the segment accounting for truncations now */
    985 	lfs_finalize_ino_seguse(fs, ip);
    986 
    987 	/*
    988 	 * If we are cleaning, ensure that we don't write UNWRITTEN disk
    989 	 * addresses to disk; possibly change the on-disk record of
    990 	 * the inode size, either by reverting to the previous size
    991 	 * (in the case of cleaning) or by verifying the inode's block
    992 	 * holdings (in the case of files being allocated as they are being
    993 	 * written).
    994 	 * XXX By not writing UNWRITTEN blocks, we are making the lfs_avail
    995 	 * XXX count on disk wrong by the same amount.	We should be
    996 	 * XXX able to "borrow" from lfs_avail and return it after the
    997 	 * XXX Ifile is written.  See also in lfs_writeseg.
    998 	 */
    999 
   1000 	/* Check file size based on highest allocated block */
   1001 	if (((ip->i_ffs1_mode & IFMT) == IFREG ||
   1002 	     (ip->i_ffs1_mode & IFMT) == IFDIR) &&
   1003 	    ip->i_size > ((ip->i_lfs_hiblk + 1) << fs->lfs_bshift)) {
   1004 		cdp->di_size = (ip->i_lfs_hiblk + 1) << fs->lfs_bshift;
   1005 		DLOG((DLOG_SEG, "lfs_writeinode: ino %d size %" PRId64 " -> %"
   1006 		      PRId64 "\n", (int)ip->i_number, ip->i_size, cdp->di_size));
   1007 	}
   1008 	if (ip->i_lfs_effnblks != ip->i_ffs1_blocks) {
   1009 		if (ip->i_flags & IN_CLEANING)
   1010 			cdp->di_size = ip->i_lfs_osize;
   1011 		DLOG((DLOG_SEG, "lfs_writeinode: cleansing ino %d eff %d != nblk %d)"
   1012 		      " at %x\n", ip->i_number, ip->i_lfs_effnblks,
   1013 		      ip->i_ffs1_blocks, fs->lfs_offset));
   1014 		for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
   1015 		     daddrp++) {
   1016 			if (*daddrp == UNWRITTEN) {
   1017 				DLOG((DLOG_SEG, "lfs_writeinode: wiping UNWRITTEN\n"));
   1018 				*daddrp = 0;
   1019 			}
   1020 		}
   1021 	} else {
   1022 		/* If all blocks are going to disk, update "size on disk" */
   1023 		ip->i_lfs_osize = ip->i_size;
   1024 	}
   1025 
   1026 #ifdef DIAGNOSTIC
   1027 	/*
   1028 	 * Check dinode held blocks against dinode size.
   1029 	 * This should be identical to the check in lfs_vget().
   1030 	 */
   1031 	for (i = (cdp->di_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
   1032 	     i < NDADDR; i++) {
   1033 		KASSERT(i >= 0);
   1034 		if ((cdp->di_mode & IFMT) == IFLNK)
   1035 			continue;
   1036 		if (((cdp->di_mode & IFMT) == IFBLK ||
   1037 		     (cdp->di_mode & IFMT) == IFCHR) && i == 0)
   1038 			continue;
   1039 		if (cdp->di_db[i] != 0) {
   1040 # ifdef DEBUG
   1041 			lfs_dump_dinode(cdp);
   1042 # endif
   1043 			panic("writing inconsistent inode");
   1044 		}
   1045 	}
   1046 #endif /* DIAGNOSTIC */
   1047 
   1048 	if (ip->i_flag & IN_CLEANING)
   1049 		LFS_CLR_UINO(ip, IN_CLEANING);
   1050 	else {
   1051 		/* XXX IN_ALLMOD */
   1052 		LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
   1053 			     IN_UPDATE | IN_MODIFY);
   1054 		if (ip->i_lfs_effnblks == ip->i_ffs1_blocks)
   1055 			LFS_CLR_UINO(ip, IN_MODIFIED);
   1056 		else
   1057 			DLOG((DLOG_VNODE, "lfs_writeinode: ino %d: real blks=%d, "
   1058 			      "eff=%d\n", ip->i_number, ip->i_ffs1_blocks,
   1059 			      ip->i_lfs_effnblks));
   1060 	}
   1061 
   1062 	if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
   1063 		sp->idp = ((struct ufs1_dinode *)bp->b_data) +
   1064 			(sp->ninodes % INOPB(fs));
   1065 	if (gotblk) {
   1066 		LFS_LOCK_BUF(bp);
   1067 		brelse(bp);
   1068 	}
   1069 
   1070 	/* Increment inode count in segment summary block. */
   1071 	++((SEGSUM *)(sp->segsum))->ss_ninos;
   1072 
   1073 	/* If this page is full, set flag to allocate a new page. */
   1074 	if (++sp->ninodes % INOPB(fs) == 0)
   1075 		sp->ibp = NULL;
   1076 
   1077 	/*
   1078 	 * If updating the ifile, update the super-block.  Update the disk
   1079 	 * address and access times for this inode in the ifile.
   1080 	 */
   1081 	ino = ip->i_number;
   1082 	if (ino == LFS_IFILE_INUM) {
   1083 		daddr = fs->lfs_idaddr;
   1084 		fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
   1085 	} else {
   1086 		LFS_IENTRY(ifp, fs, ino, ibp);
   1087 		daddr = ifp->if_daddr;
   1088 		ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
   1089 		error = LFS_BWRITE_LOG(ibp); /* Ifile */
   1090 	}
   1091 
   1092 	/*
   1093 	 * The inode's last address should not be in the current partial
   1094 	 * segment, except under exceptional circumstances (lfs_writevnodes
   1095 	 * had to start over, and in the meantime more blocks were written
   1096 	 * to a vnode).	 Both inodes will be accounted to this segment
   1097 	 * in lfs_writeseg so we need to subtract the earlier version
   1098 	 * here anyway.	 The segment count can temporarily dip below
   1099 	 * zero here; keep track of how many duplicates we have in
   1100 	 * "dupino" so we don't panic below.
   1101 	 */
   1102 	if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno)) {
   1103 		++sp->ndupino;
   1104 		DLOG((DLOG_SEG, "lfs_writeinode: last inode addr in current pseg "
   1105 		      "(ino %d daddr 0x%llx) ndupino=%d\n", ino,
   1106 		      (long long)daddr, sp->ndupino));
   1107 	}
   1108 	/*
   1109 	 * Account the inode: it no longer belongs to its former segment,
   1110 	 * though it will not belong to the new segment until that segment
   1111 	 * is actually written.
   1112 	 */
   1113 	if (daddr != LFS_UNUSED_DADDR) {
   1114 		u_int32_t oldsn = dtosn(fs, daddr);
   1115 #ifdef DIAGNOSTIC
   1116 		int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0;
   1117 #endif
   1118 		LFS_SEGENTRY(sup, fs, oldsn, bp);
   1119 #ifdef DIAGNOSTIC
   1120 		if (sup->su_nbytes +
   1121 		    sizeof (struct ufs1_dinode) * ndupino
   1122 		      < sizeof (struct ufs1_dinode)) {
   1123 			printf("lfs_writeinode: negative bytes "
   1124 			       "(segment %" PRIu32 " short by %d, "
   1125 			       "oldsn=%" PRIu32 ", cursn=%" PRIu32
   1126 			       ", daddr=%" PRId64 ", su_nbytes=%u, "
   1127 			       "ndupino=%d)\n",
   1128 			       dtosn(fs, daddr),
   1129 			       (int)sizeof (struct ufs1_dinode) *
   1130 				   (1 - sp->ndupino) - sup->su_nbytes,
   1131 			       oldsn, sp->seg_number, daddr,
   1132 			       (unsigned int)sup->su_nbytes,
   1133 			       sp->ndupino);
   1134 			panic("lfs_writeinode: negative bytes");
   1135 			sup->su_nbytes = sizeof (struct ufs1_dinode);
   1136 		}
   1137 #endif
   1138 		DLOG((DLOG_SU, "seg %d -= %d for ino %d inode\n",
   1139 		      dtosn(fs, daddr), sizeof (struct ufs1_dinode), ino));
   1140 		sup->su_nbytes -= sizeof (struct ufs1_dinode);
   1141 		redo_ifile =
   1142 			(ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
   1143 		if (redo_ifile) {
   1144 			simple_lock(&fs->lfs_interlock);
   1145 			fs->lfs_flags |= LFS_IFDIRTY;
   1146 			simple_unlock(&fs->lfs_interlock);
   1147 		}
   1148 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
   1149 	}
   1150 	return (redo_ifile);
   1151 }
   1152 
   1153 int
   1154 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
   1155 {
   1156 	struct lfs *fs;
   1157 	int vers;
   1158 	int j, blksinblk;
   1159 
   1160 	ASSERT_SEGLOCK(sp->fs);
   1161 	/*
   1162 	 * If full, finish this segment.  We may be doing I/O, so
   1163 	 * release and reacquire the splbio().
   1164 	 */
   1165 #ifdef DIAGNOSTIC
   1166 	if (sp->vp == NULL)
   1167 		panic ("lfs_gatherblock: Null vp in segment");
   1168 #endif
   1169 	fs = sp->fs;
   1170 	blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
   1171 	if (sp->sum_bytes_left < sizeof(int32_t) * blksinblk ||
   1172 	    sp->seg_bytes_left < bp->b_bcount) {
   1173 		if (sptr)
   1174 			splx(*sptr);
   1175 		lfs_updatemeta(sp);
   1176 
   1177 		vers = sp->fip->fi_version;
   1178 		(void) lfs_writeseg(fs, sp);
   1179 
   1180 		/* Add the current file to the segment summary. */
   1181 		lfs_acquire_finfo(fs, VTOI(sp->vp)->i_number, vers);
   1182 
   1183 		if (sptr)
   1184 			*sptr = splbio();
   1185 		return (1);
   1186 	}
   1187 
   1188 	if (bp->b_flags & B_GATHERED) {
   1189 		DLOG((DLOG_SEG, "lfs_gatherblock: already gathered! Ino %d,"
   1190 		      " lbn %" PRId64 "\n",
   1191 		      sp->fip->fi_ino, bp->b_lblkno));
   1192 		return (0);
   1193 	}
   1194 
   1195 	/* Insert into the buffer list, update the FINFO block. */
   1196 	bp->b_flags |= B_GATHERED;
   1197 
   1198 	*sp->cbpp++ = bp;
   1199 	for (j = 0; j < blksinblk; j++) {
   1200 		sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
   1201 		/* This block's accounting moves from lfs_favail to lfs_avail */
   1202 		lfs_deregister_block(sp->vp, bp->b_lblkno + j);
   1203 	}
   1204 
   1205 	sp->sum_bytes_left -= sizeof(int32_t) * blksinblk;
   1206 	sp->seg_bytes_left -= bp->b_bcount;
   1207 	return (0);
   1208 }
   1209 
   1210 int
   1211 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp,
   1212     int (*match)(struct lfs *, struct buf *))
   1213 {
   1214 	struct buf *bp, *nbp;
   1215 	int s, count = 0;
   1216 
   1217 	ASSERT_SEGLOCK(fs);
   1218 	if (vp->v_type == VBLK)
   1219 		return 0;
   1220 	KASSERT(sp->vp == NULL);
   1221 	sp->vp = vp;
   1222 	s = splbio();
   1223 
   1224 #ifndef LFS_NO_BACKBUF_HACK
   1225 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
   1226 # define	BUF_OFFSET	\
   1227 	(((caddr_t)&LIST_NEXT(bp, b_vnbufs)) - (caddr_t)bp)
   1228 # define	BACK_BUF(BP)	\
   1229 	((struct buf *)(((caddr_t)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
   1230 # define	BEG_OF_LIST	\
   1231 	((struct buf *)(((caddr_t)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
   1232 
   1233 loop:
   1234 	/* Find last buffer. */
   1235 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd);
   1236 	     bp && LIST_NEXT(bp, b_vnbufs) != NULL;
   1237 	     bp = LIST_NEXT(bp, b_vnbufs))
   1238 		/* nothing */;
   1239 	for (; bp && bp != BEG_OF_LIST; bp = nbp) {
   1240 		nbp = BACK_BUF(bp);
   1241 #else /* LFS_NO_BACKBUF_HACK */
   1242 loop:
   1243 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
   1244 		nbp = LIST_NEXT(bp, b_vnbufs);
   1245 #endif /* LFS_NO_BACKBUF_HACK */
   1246 		if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) {
   1247 #ifdef DEBUG
   1248 			if (vp == fs->lfs_ivnode &&
   1249 			    (bp->b_flags & (B_BUSY|B_GATHERED)) == B_BUSY)
   1250 				DLOG((DLOG_SEG, "lfs_gather: ifile lbn %"
   1251 				      PRId64 " busy (%x)",
   1252 				      bp->b_lblkno, bp->b_flags));
   1253 #endif
   1254 			continue;
   1255 		}
   1256 #ifdef DIAGNOSTIC
   1257 # ifdef LFS_USE_B_INVAL
   1258 		if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
   1259 			DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64
   1260 			      " is B_INVAL\n", bp->b_lblkno));
   1261 			VOP_PRINT(bp->b_vp);
   1262 		}
   1263 # endif /* LFS_USE_B_INVAL */
   1264 		if (!(bp->b_flags & B_DELWRI))
   1265 			panic("lfs_gather: bp not B_DELWRI");
   1266 		if (!(bp->b_flags & B_LOCKED)) {
   1267 			DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64
   1268 			      " blk %" PRId64 " not B_LOCKED\n",
   1269 			      bp->b_lblkno,
   1270 			      dbtofsb(fs, bp->b_blkno)));
   1271 			VOP_PRINT(bp->b_vp);
   1272 			panic("lfs_gather: bp not B_LOCKED");
   1273 		}
   1274 #endif
   1275 		if (lfs_gatherblock(sp, bp, &s)) {
   1276 			goto loop;
   1277 		}
   1278 		count++;
   1279 	}
   1280 	splx(s);
   1281 	lfs_updatemeta(sp);
   1282 	KASSERT(sp->vp == vp);
   1283 	sp->vp = NULL;
   1284 	return count;
   1285 }
   1286 
   1287 #if DEBUG
   1288 # define DEBUG_OOFF(n) do {						\
   1289 	if (ooff == 0) {						\
   1290 		DLOG((DLOG_SEG, "lfs_updatemeta[%d]: warning: writing " \
   1291 			"ino %d lbn %" PRId64 " at 0x%" PRIx32		\
   1292 			", was 0x0 (or %" PRId64 ")\n",			\
   1293 			(n), ip->i_number, lbn, ndaddr, daddr));	\
   1294 	}								\
   1295 } while (0)
   1296 #else
   1297 # define DEBUG_OOFF(n)
   1298 #endif
   1299 
   1300 /*
   1301  * Change the given block's address to ndaddr, finding its previous
   1302  * location using ufs_bmaparray().
   1303  *
   1304  * Account for this change in the segment table.
   1305  *
   1306  * called with sp == NULL by roll-forwarding code.
   1307  */
   1308 void
   1309 lfs_update_single(struct lfs *fs, struct segment *sp, struct vnode *vp,
   1310     daddr_t lbn, int32_t ndaddr, int size)
   1311 {
   1312 	SEGUSE *sup;
   1313 	struct buf *bp;
   1314 	struct indir a[NIADDR + 2], *ap;
   1315 	struct inode *ip;
   1316 	daddr_t daddr, ooff;
   1317 	int num, error;
   1318 	int bb, osize, obb;
   1319 
   1320 	ASSERT_SEGLOCK(fs);
   1321 	KASSERT(sp == NULL || sp->vp == vp);
   1322 	ip = VTOI(vp);
   1323 
   1324 	error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL, NULL);
   1325 	if (error)
   1326 		panic("lfs_updatemeta: ufs_bmaparray returned %d", error);
   1327 
   1328 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
   1329 	KASSERT(daddr <= LFS_MAX_DADDR);
   1330 	if (daddr > 0)
   1331 		daddr = dbtofsb(fs, daddr);
   1332 
   1333 	bb = fragstofsb(fs, numfrags(fs, size));
   1334 	switch (num) {
   1335 	    case 0:
   1336 		    ooff = ip->i_ffs1_db[lbn];
   1337 		    DEBUG_OOFF(0);
   1338 		    if (ooff == UNWRITTEN)
   1339 			    ip->i_ffs1_blocks += bb;
   1340 		    else {
   1341 			    /* possible fragment truncation or extension */
   1342 			    obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
   1343 			    ip->i_ffs1_blocks += (bb - obb);
   1344 		    }
   1345 		    ip->i_ffs1_db[lbn] = ndaddr;
   1346 		    break;
   1347 	    case 1:
   1348 		    ooff = ip->i_ffs1_ib[a[0].in_off];
   1349 		    DEBUG_OOFF(1);
   1350 		    if (ooff == UNWRITTEN)
   1351 			    ip->i_ffs1_blocks += bb;
   1352 		    ip->i_ffs1_ib[a[0].in_off] = ndaddr;
   1353 		    break;
   1354 	    default:
   1355 		    ap = &a[num - 1];
   1356 		    if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
   1357 			    panic("lfs_updatemeta: bread bno %" PRId64,
   1358 				  ap->in_lbn);
   1359 
   1360 		    /* XXX ondisk32 */
   1361 		    ooff = ((int32_t *)bp->b_data)[ap->in_off];
   1362 		    DEBUG_OOFF(num);
   1363 		    if (ooff == UNWRITTEN)
   1364 			    ip->i_ffs1_blocks += bb;
   1365 		    /* XXX ondisk32 */
   1366 		    ((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
   1367 		    (void) VOP_BWRITE(bp);
   1368 	}
   1369 
   1370 	KASSERT(ooff == 0 || ooff == UNWRITTEN || ooff == daddr);
   1371 
   1372 	/* Update hiblk when extending the file */
   1373 	if (lbn > ip->i_lfs_hiblk)
   1374 		ip->i_lfs_hiblk = lbn;
   1375 
   1376 	/*
   1377 	 * Though we'd rather it couldn't, this *can* happen right now
   1378 	 * if cleaning blocks and regular blocks coexist.
   1379 	 */
   1380 	/* KASSERT(daddr < fs->lfs_lastpseg || daddr > ndaddr); */
   1381 
   1382 	/*
   1383 	 * Update segment usage information, based on old size
   1384 	 * and location.
   1385 	 */
   1386 	if (daddr > 0) {
   1387 		u_int32_t oldsn = dtosn(fs, daddr);
   1388 #ifdef DIAGNOSTIC
   1389 		int ndupino;
   1390 
   1391 		if (sp && sp->seg_number == oldsn) {
   1392 			ndupino = sp->ndupino;
   1393 		} else {
   1394 			ndupino = 0;
   1395 		}
   1396 #endif
   1397 		KASSERT(oldsn >= 0 && oldsn < fs->lfs_nseg);
   1398 		if (lbn >= 0 && lbn < NDADDR)
   1399 			osize = ip->i_lfs_fragsize[lbn];
   1400 		else
   1401 			osize = fs->lfs_bsize;
   1402 		LFS_SEGENTRY(sup, fs, oldsn, bp);
   1403 #ifdef DIAGNOSTIC
   1404 		if (sup->su_nbytes + sizeof (struct ufs1_dinode) * ndupino
   1405 		    < osize) {
   1406 			printf("lfs_updatemeta: negative bytes "
   1407 			       "(segment %" PRIu32 " short by %" PRId64
   1408 			       ")\n", dtosn(fs, daddr),
   1409 			       (int64_t)osize -
   1410 			       (sizeof (struct ufs1_dinode) * ndupino +
   1411 				sup->su_nbytes));
   1412 			printf("lfs_updatemeta: ino %llu, lbn %" PRId64
   1413 			       ", addr = 0x%" PRIx64 "\n",
   1414 			       (unsigned long long)ip->i_number, lbn, daddr);
   1415 			printf("lfs_updatemeta: ndupino=%d\n", ndupino);
   1416 			panic("lfs_updatemeta: negative bytes");
   1417 			sup->su_nbytes = osize -
   1418 			    sizeof (struct ufs1_dinode) * ndupino;
   1419 		}
   1420 #endif
   1421 		DLOG((DLOG_SU, "seg %" PRIu32 " -= %d for ino %d lbn %" PRId64
   1422 		      " db 0x%" PRIx64 "\n",
   1423 		      dtosn(fs, daddr), osize,
   1424 		      ip->i_number, lbn, daddr));
   1425 		sup->su_nbytes -= osize;
   1426 		if (!(bp->b_flags & B_GATHERED)) {
   1427 			simple_lock(&fs->lfs_interlock);
   1428 			fs->lfs_flags |= LFS_IFDIRTY;
   1429 			simple_unlock(&fs->lfs_interlock);
   1430 		}
   1431 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
   1432 	}
   1433 	/*
   1434 	 * Now that this block has a new address, and its old
   1435 	 * segment no longer owns it, we can forget about its
   1436 	 * old size.
   1437 	 */
   1438 	if (lbn >= 0 && lbn < NDADDR)
   1439 		ip->i_lfs_fragsize[lbn] = size;
   1440 }
   1441 
   1442 /*
   1443  * Update the metadata that points to the blocks listed in the FINFO
   1444  * array.
   1445  */
   1446 void
   1447 lfs_updatemeta(struct segment *sp)
   1448 {
   1449 	struct buf *sbp;
   1450 	struct lfs *fs;
   1451 	struct vnode *vp;
   1452 	daddr_t lbn;
   1453 	int i, nblocks, num;
   1454 	int bb;
   1455 	int bytesleft, size;
   1456 
   1457 	ASSERT_SEGLOCK(sp->fs);
   1458 	vp = sp->vp;
   1459 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
   1460 	KASSERT(nblocks >= 0);
   1461 	KASSERT(vp != NULL);
   1462 	if (nblocks == 0)
   1463 		return;
   1464 
   1465 	/*
   1466 	 * This count may be high due to oversize blocks from lfs_gop_write.
   1467 	 * Correct for this. (XXX we should be able to keep track of these.)
   1468 	 */
   1469 	fs = sp->fs;
   1470 	for (i = 0; i < nblocks; i++) {
   1471 		if (sp->start_bpp[i] == NULL) {
   1472 			DLOG((DLOG_SEG, "lfs_updatemeta: nblocks = %d, not %d\n", i, nblocks));
   1473 			nblocks = i;
   1474 			break;
   1475 		}
   1476 		num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
   1477 		KASSERT(sp->start_bpp[i]->b_lblkno >= 0 || num == 1);
   1478 		nblocks -= num - 1;
   1479 	}
   1480 
   1481 	KASSERT(vp->v_type == VREG ||
   1482 	   nblocks == &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp);
   1483 	KASSERT(nblocks == sp->cbpp - sp->start_bpp);
   1484 
   1485 	/*
   1486 	 * Sort the blocks.
   1487 	 *
   1488 	 * We have to sort even if the blocks come from the
   1489 	 * cleaner, because there might be other pending blocks on the
   1490 	 * same inode...and if we don't sort, and there are fragments
   1491 	 * present, blocks may be written in the wrong place.
   1492 	 */
   1493 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
   1494 
   1495 	/*
   1496 	 * Record the length of the last block in case it's a fragment.
   1497 	 * If there are indirect blocks present, they sort last.  An
   1498 	 * indirect block will be lfs_bsize and its presence indicates
   1499 	 * that you cannot have fragments.
   1500 	 *
   1501 	 * XXX This last is a lie.  A cleaned fragment can coexist with
   1502 	 * XXX a later indirect block.	This will continue to be
   1503 	 * XXX true until lfs_markv is fixed to do everything with
   1504 	 * XXX fake blocks (including fake inodes and fake indirect blocks).
   1505 	 */
   1506 	sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
   1507 		fs->lfs_bmask) + 1;
   1508 
   1509 	/*
   1510 	 * Assign disk addresses, and update references to the logical
   1511 	 * block and the segment usage information.
   1512 	 */
   1513 	for (i = nblocks; i--; ++sp->start_bpp) {
   1514 		sbp = *sp->start_bpp;
   1515 		lbn = *sp->start_lbp;
   1516 		KASSERT(sbp->b_lblkno == lbn);
   1517 
   1518 		sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
   1519 
   1520 		/*
   1521 		 * If we write a frag in the wrong place, the cleaner won't
   1522 		 * be able to correctly identify its size later, and the
   1523 		 * segment will be uncleanable.	 (Even worse, it will assume
   1524 		 * that the indirect block that actually ends the list
   1525 		 * is of a smaller size!)
   1526 		 */
   1527 		if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
   1528 			panic("lfs_updatemeta: fragment is not last block");
   1529 
   1530 		/*
   1531 		 * For each subblock in this possibly oversized block,
   1532 		 * update its address on disk.
   1533 		 */
   1534 		KASSERT(lbn >= 0 || sbp->b_bcount == fs->lfs_bsize);
   1535 		KASSERT(vp == sbp->b_vp);
   1536 		for (bytesleft = sbp->b_bcount; bytesleft > 0;
   1537 		     bytesleft -= fs->lfs_bsize) {
   1538 			size = MIN(bytesleft, fs->lfs_bsize);
   1539 			bb = fragstofsb(fs, numfrags(fs, size));
   1540 			lbn = *sp->start_lbp++;
   1541 			lfs_update_single(fs, sp, sp->vp, lbn, fs->lfs_offset,
   1542 			    size);
   1543 			fs->lfs_offset += bb;
   1544 		}
   1545 
   1546 	}
   1547 }
   1548 
   1549 /*
   1550  * Move lfs_offset to a segment earlier than sn.
   1551  */
   1552 int
   1553 lfs_rewind(struct lfs *fs, int newsn)
   1554 {
   1555 	int sn, osn, isdirty;
   1556 	struct buf *bp;
   1557 	SEGUSE *sup;
   1558 
   1559 	ASSERT_SEGLOCK(fs);
   1560 
   1561 	osn = dtosn(fs, fs->lfs_offset);
   1562 	if (osn < newsn)
   1563 		return 0;
   1564 
   1565 	/* lfs_avail eats the remaining space in this segment */
   1566 	fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset - fs->lfs_curseg);
   1567 
   1568 	/* Find a low-numbered segment */
   1569 	for (sn = 0; sn < fs->lfs_nseg; ++sn) {
   1570 		LFS_SEGENTRY(sup, fs, sn, bp);
   1571 		isdirty = sup->su_flags & SEGUSE_DIRTY;
   1572 		brelse(bp);
   1573 
   1574 		if (!isdirty)
   1575 			break;
   1576 	}
   1577 	if (sn == fs->lfs_nseg)
   1578 		panic("lfs_rewind: no clean segments");
   1579 	if (sn >= newsn)
   1580 		return ENOENT;
   1581 	fs->lfs_nextseg = sn;
   1582 	lfs_newseg(fs);
   1583 	fs->lfs_offset = fs->lfs_curseg;
   1584 
   1585 	return 0;
   1586 }
   1587 
   1588 /*
   1589  * Start a new partial segment.
   1590  *
   1591  * Return 1 when we entered to a new segment.
   1592  * Otherwise, return 0.
   1593  */
   1594 int
   1595 lfs_initseg(struct lfs *fs)
   1596 {
   1597 	struct segment *sp = fs->lfs_sp;
   1598 	SEGSUM *ssp;
   1599 	struct buf *sbp;	/* buffer for SEGSUM */
   1600 	int repeat = 0;		/* return value */
   1601 
   1602 	ASSERT_SEGLOCK(fs);
   1603 	/* Advance to the next segment. */
   1604 	if (!LFS_PARTIAL_FITS(fs)) {
   1605 		SEGUSE *sup;
   1606 		struct buf *bp;
   1607 
   1608 		/* lfs_avail eats the remaining space */
   1609 		fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
   1610 						   fs->lfs_curseg);
   1611 		/* Wake up any cleaning procs waiting on this file system. */
   1612 		wakeup(&lfs_allclean_wakeup);
   1613 		wakeup(&fs->lfs_nextseg);
   1614 		lfs_newseg(fs);
   1615 		repeat = 1;
   1616 		fs->lfs_offset = fs->lfs_curseg;
   1617 
   1618 		sp->seg_number = dtosn(fs, fs->lfs_curseg);
   1619 		sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
   1620 
   1621 		/*
   1622 		 * If the segment contains a superblock, update the offset
   1623 		 * and summary address to skip over it.
   1624 		 */
   1625 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1626 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
   1627 			fs->lfs_offset += btofsb(fs, LFS_SBPAD);
   1628 			sp->seg_bytes_left -= LFS_SBPAD;
   1629 		}
   1630 		brelse(bp);
   1631 		/* Segment zero could also contain the labelpad */
   1632 		if (fs->lfs_version > 1 && sp->seg_number == 0 &&
   1633 		    fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
   1634 			fs->lfs_offset +=
   1635 			    btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
   1636 			sp->seg_bytes_left -=
   1637 			    LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
   1638 		}
   1639 	} else {
   1640 		sp->seg_number = dtosn(fs, fs->lfs_curseg);
   1641 		sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
   1642 				      (fs->lfs_offset - fs->lfs_curseg));
   1643 	}
   1644 	fs->lfs_lastpseg = fs->lfs_offset;
   1645 
   1646 	/* Record first address of this partial segment */
   1647 	if (sp->seg_flags & SEGM_CLEAN) {
   1648 		fs->lfs_cleanint[fs->lfs_cleanind] = fs->lfs_offset;
   1649 		if (++fs->lfs_cleanind >= LFS_MAX_CLEANIND) {
   1650 			/* "1" is the artificial inc in lfs_seglock */
   1651 			simple_lock(&fs->lfs_interlock);
   1652 			while (fs->lfs_iocount > 1) {
   1653 				ltsleep(&fs->lfs_iocount, PRIBIO + 1,
   1654 				    "lfs_initseg", 0, &fs->lfs_interlock);
   1655 			}
   1656 			simple_unlock(&fs->lfs_interlock);
   1657 			fs->lfs_cleanind = 0;
   1658 		}
   1659 	}
   1660 
   1661 	sp->fs = fs;
   1662 	sp->ibp = NULL;
   1663 	sp->idp = NULL;
   1664 	sp->ninodes = 0;
   1665 	sp->ndupino = 0;
   1666 
   1667 	sp->cbpp = sp->bpp;
   1668 
   1669 	/* Get a new buffer for SEGSUM */
   1670 	sbp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
   1671 	    fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize, LFS_NB_SUMMARY);
   1672 
   1673 	/* ... and enter it into the buffer list. */
   1674 	*sp->cbpp = sbp;
   1675 	sp->cbpp++;
   1676 	fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
   1677 
   1678 	sp->start_bpp = sp->cbpp;
   1679 
   1680 	/* Set point to SEGSUM, initialize it. */
   1681 	ssp = sp->segsum = sbp->b_data;
   1682 	memset(ssp, 0, fs->lfs_sumsize);
   1683 	ssp->ss_next = fs->lfs_nextseg;
   1684 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
   1685 	ssp->ss_magic = SS_MAGIC;
   1686 
   1687 	/* Set pointer to first FINFO, initialize it. */
   1688 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + SEGSUM_SIZE(fs));
   1689 	sp->fip->fi_nblocks = 0;
   1690 	sp->start_lbp = &sp->fip->fi_blocks[0];
   1691 	sp->fip->fi_lastlength = 0;
   1692 
   1693 	sp->seg_bytes_left -= fs->lfs_sumsize;
   1694 	sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
   1695 
   1696 	return (repeat);
   1697 }
   1698 
   1699 /*
   1700  * Remove SEGUSE_INVAL from all segments.
   1701  */
   1702 void
   1703 lfs_unset_inval_all(struct lfs *fs)
   1704 {
   1705 	SEGUSE *sup;
   1706 	struct buf *bp;
   1707 	int i;
   1708 
   1709 	for (i = 0; i < fs->lfs_nseg; i++) {
   1710 		LFS_SEGENTRY(sup, fs, i, bp);
   1711 		if (sup->su_flags & SEGUSE_INVAL) {
   1712 			sup->su_flags &= ~SEGUSE_INVAL;
   1713 			VOP_BWRITE(bp);
   1714 		} else
   1715 			brelse(bp);
   1716 	}
   1717 }
   1718 
   1719 /*
   1720  * Return the next segment to write.
   1721  */
   1722 void
   1723 lfs_newseg(struct lfs *fs)
   1724 {
   1725 	CLEANERINFO *cip;
   1726 	SEGUSE *sup;
   1727 	struct buf *bp;
   1728 	int curseg, isdirty, sn, skip_inval;
   1729 
   1730 	ASSERT_SEGLOCK(fs);
   1731 
   1732 	/* Honor LFCNWRAPSTOP */
   1733 	simple_lock(&fs->lfs_interlock);
   1734 	if (fs->lfs_nowrap && fs->lfs_nextseg < fs->lfs_curseg) {
   1735 		wakeup(&fs->lfs_nowrap);
   1736 		ltsleep(&fs->lfs_nowrap, PVFS, "newseg", 0,
   1737 			&fs->lfs_interlock);
   1738 	}
   1739 	simple_unlock(&fs->lfs_interlock);
   1740 
   1741 	LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
   1742 	DLOG((DLOG_SU, "lfs_newseg: seg %d := 0 in newseg\n",
   1743 	      dtosn(fs, fs->lfs_nextseg)));
   1744 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1745 	sup->su_nbytes = 0;
   1746 	sup->su_nsums = 0;
   1747 	sup->su_ninos = 0;
   1748 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
   1749 
   1750 	LFS_CLEANERINFO(cip, fs, bp);
   1751 	--cip->clean;
   1752 	++cip->dirty;
   1753 	fs->lfs_nclean = cip->clean;
   1754 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
   1755 
   1756 	fs->lfs_lastseg = fs->lfs_curseg;
   1757 	fs->lfs_curseg = fs->lfs_nextseg;
   1758 	skip_inval = 1;
   1759 	for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
   1760 		sn = (sn + 1) % fs->lfs_nseg;
   1761 
   1762 		if (sn == curseg) {
   1763 			if (skip_inval)
   1764 				skip_inval = 0;
   1765 			else
   1766 				panic("lfs_nextseg: no clean segments");
   1767 		}
   1768 		LFS_SEGENTRY(sup, fs, sn, bp);
   1769 		isdirty = sup->su_flags & (SEGUSE_DIRTY | (skip_inval ? SEGUSE_INVAL : 0));
   1770 		/* Check SEGUSE_EMPTY as we go along */
   1771 		if (isdirty && sup->su_nbytes == 0 &&
   1772 		    !(sup->su_flags & SEGUSE_EMPTY))
   1773 			LFS_WRITESEGENTRY(sup, fs, sn, bp);
   1774 		else
   1775 			brelse(bp);
   1776 
   1777 		if (!isdirty)
   1778 			break;
   1779 	}
   1780 	if (skip_inval == 0)
   1781 		lfs_unset_inval_all(fs);
   1782 
   1783 	++fs->lfs_nactive;
   1784 	fs->lfs_nextseg = sntod(fs, sn);
   1785 	if (lfs_dostats) {
   1786 		++lfs_stats.segsused;
   1787 	}
   1788 }
   1789 
   1790 static struct buf *
   1791 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr, int n)
   1792 {
   1793 	struct lfs_cluster *cl;
   1794 	struct buf **bpp, *bp;
   1795 
   1796 	ASSERT_SEGLOCK(fs);
   1797 	cl = (struct lfs_cluster *)pool_get(&fs->lfs_clpool, PR_WAITOK);
   1798 	bpp = (struct buf **)pool_get(&fs->lfs_bpppool, PR_WAITOK);
   1799 	memset(cl, 0, sizeof(*cl));
   1800 	cl->fs = fs;
   1801 	cl->bpp = bpp;
   1802 	cl->bufcount = 0;
   1803 	cl->bufsize = 0;
   1804 
   1805 	/* If this segment is being written synchronously, note that */
   1806 	if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
   1807 		cl->flags |= LFS_CL_SYNC;
   1808 		cl->seg = fs->lfs_sp;
   1809 		++cl->seg->seg_iocount;
   1810 	}
   1811 
   1812 	/* Get an empty buffer header, or maybe one with something on it */
   1813 	bp = getiobuf();
   1814 	bp->b_flags = B_BUSY | B_CALL;
   1815 	bp->b_dev = NODEV;
   1816 	bp->b_blkno = bp->b_lblkno = addr;
   1817 	bp->b_iodone = lfs_cluster_callback;
   1818 	bp->b_private = cl;
   1819 	bp->b_vp = vp;
   1820 
   1821 	return bp;
   1822 }
   1823 
   1824 int
   1825 lfs_writeseg(struct lfs *fs, struct segment *sp)
   1826 {
   1827 	struct buf **bpp, *bp, *cbp, *newbp;
   1828 	SEGUSE *sup;
   1829 	SEGSUM *ssp;
   1830 	int i, s;
   1831 	int do_again, nblocks, byteoffset;
   1832 	size_t el_size;
   1833 	struct lfs_cluster *cl;
   1834 	u_short ninos;
   1835 	struct vnode *devvp;
   1836 	char *p = NULL;
   1837 	struct vnode *vp;
   1838 	int32_t *daddrp;	/* XXX ondisk32 */
   1839 	int changed;
   1840 	u_int32_t sum;
   1841 #ifdef DEBUG
   1842 	FINFO *fip;
   1843 	int findex;
   1844 #endif
   1845 
   1846 	ASSERT_SEGLOCK(fs);
   1847 	/*
   1848 	 * If there are no buffers other than the segment summary to write
   1849 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
   1850 	 * even if there aren't any buffers, you need to write the superblock.
   1851 	 */
   1852 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
   1853 		return (0);
   1854 
   1855 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
   1856 
   1857 	/* Update the segment usage information. */
   1858 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1859 
   1860 	/* Loop through all blocks, except the segment summary. */
   1861 	for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
   1862 		if ((*bpp)->b_vp != devvp) {
   1863 			sup->su_nbytes += (*bpp)->b_bcount;
   1864 			DLOG((DLOG_SU, "seg %" PRIu32 " += %ld for ino %d"
   1865 			      " lbn %" PRId64 " db 0x%" PRIx64 "\n",
   1866 			      sp->seg_number, (*bpp)->b_bcount,
   1867 			      VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno,
   1868 			      (*bpp)->b_blkno));
   1869 		}
   1870 	}
   1871 
   1872 	ssp = (SEGSUM *)sp->segsum;
   1873 
   1874 #ifdef DEBUG
   1875 	/* Check for zero-length FINFO entries. */
   1876 	fip = (struct finfo *)((caddr_t)ssp + SEGSUM_SIZE(fs));
   1877 	for (findex = 0; findex < ssp->ss_nfinfo; findex++) {
   1878 		KDASSERT(fip->fi_nblocks > 0);
   1879 		fip = (FINFO *)((caddr_t)fip + FINFOSIZE +
   1880 			sizeof(int32_t) * fip->fi_nblocks);
   1881 	}
   1882 #endif /* DEBUG */
   1883 
   1884 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
   1885 	DLOG((DLOG_SU, "seg %d += %d for %d inodes\n",
   1886 	      sp->seg_number, ssp->ss_ninos * sizeof (struct ufs1_dinode),
   1887 	      ssp->ss_ninos));
   1888 	sup->su_nbytes += ssp->ss_ninos * sizeof (struct ufs1_dinode);
   1889 	/* sup->su_nbytes += fs->lfs_sumsize; */
   1890 	if (fs->lfs_version == 1)
   1891 		sup->su_olastmod = time.tv_sec;
   1892 	else
   1893 		sup->su_lastmod = time.tv_sec;
   1894 	sup->su_ninos += ninos;
   1895 	++sup->su_nsums;
   1896 	fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
   1897 
   1898 	do_again = !(bp->b_flags & B_GATHERED);
   1899 	LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
   1900 
   1901 	/*
   1902 	 * Mark blocks B_BUSY, to prevent then from being changed between
   1903 	 * the checksum computation and the actual write.
   1904 	 *
   1905 	 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
   1906 	 * there are any, replace them with copies that have UNASSIGNED
   1907 	 * instead.
   1908 	 */
   1909 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
   1910 		++bpp;
   1911 		bp = *bpp;
   1912 		if (bp->b_flags & B_CALL) { /* UBC or malloced buffer */
   1913 			bp->b_flags |= B_BUSY;
   1914 			continue;
   1915 		}
   1916 
   1917 		simple_lock(&bp->b_interlock);
   1918 		s = splbio();
   1919 		while (bp->b_flags & B_BUSY) {
   1920 			DLOG((DLOG_SEG, "lfs_writeseg: avoiding potential"
   1921 			      " data summary corruption for ino %d, lbn %"
   1922 			      PRId64 "\n",
   1923 			      VTOI(bp->b_vp)->i_number, bp->b_lblkno));
   1924 			bp->b_flags |= B_WANTED;
   1925 			ltsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0,
   1926 				&bp->b_interlock);
   1927 			splx(s);
   1928 			s = splbio();
   1929 		}
   1930 		bp->b_flags |= B_BUSY;
   1931 		splx(s);
   1932 		simple_unlock(&bp->b_interlock);
   1933 
   1934 		/*
   1935 		 * Check and replace indirect block UNWRITTEN bogosity.
   1936 		 * XXX See comment in lfs_writefile.
   1937 		 */
   1938 		if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
   1939 		   VTOI(bp->b_vp)->i_ffs1_blocks !=
   1940 		   VTOI(bp->b_vp)->i_lfs_effnblks) {
   1941 			DLOG((DLOG_VNODE, "lfs_writeseg: cleansing ino %d (%d != %d)\n",
   1942 			      VTOI(bp->b_vp)->i_number,
   1943 			      VTOI(bp->b_vp)->i_lfs_effnblks,
   1944 			      VTOI(bp->b_vp)->i_ffs1_blocks));
   1945 			/* Make a copy we'll make changes to */
   1946 			newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
   1947 					   bp->b_bcount, LFS_NB_IBLOCK);
   1948 			newbp->b_blkno = bp->b_blkno;
   1949 			memcpy(newbp->b_data, bp->b_data,
   1950 			       newbp->b_bcount);
   1951 
   1952 			changed = 0;
   1953 			/* XXX ondisk32 */
   1954 			for (daddrp = (int32_t *)(newbp->b_data);
   1955 			     daddrp < (int32_t *)(newbp->b_data +
   1956 						  newbp->b_bcount); daddrp++) {
   1957 				if (*daddrp == UNWRITTEN) {
   1958 					++changed;
   1959 					*daddrp = 0;
   1960 				}
   1961 			}
   1962 			/*
   1963 			 * Get rid of the old buffer.  Don't mark it clean,
   1964 			 * though, if it still has dirty data on it.
   1965 			 */
   1966 			if (changed) {
   1967 				DLOG((DLOG_SEG, "lfs_writeseg: replacing UNWRITTEN(%d):"
   1968 				      " bp = %p newbp = %p\n", changed, bp,
   1969 				      newbp));
   1970 				*bpp = newbp;
   1971 				bp->b_flags &= ~(B_ERROR | B_GATHERED);
   1972 				if (bp->b_flags & B_CALL) {
   1973 					DLOG((DLOG_SEG, "lfs_writeseg: "
   1974 					      "indir bp should not be B_CALL\n"));
   1975 					s = splbio();
   1976 					biodone(bp);
   1977 					splx(s);
   1978 					bp = NULL;
   1979 				} else {
   1980 					/* Still on free list, leave it there */
   1981 					s = splbio();
   1982 					bp->b_flags &= ~B_BUSY;
   1983 					if (bp->b_flags & B_WANTED)
   1984 						wakeup(bp);
   1985 					splx(s);
   1986 					/*
   1987 					 * We have to re-decrement lfs_avail
   1988 					 * since this block is going to come
   1989 					 * back around to us in the next
   1990 					 * segment.
   1991 					 */
   1992 					fs->lfs_avail -=
   1993 					    btofsb(fs, bp->b_bcount);
   1994 				}
   1995 			} else {
   1996 				lfs_freebuf(fs, newbp);
   1997 			}
   1998 		}
   1999 	}
   2000 	/*
   2001 	 * Compute checksum across data and then across summary; the first
   2002 	 * block (the summary block) is skipped.  Set the create time here
   2003 	 * so that it's guaranteed to be later than the inode mod times.
   2004 	 */
   2005 	sum = 0;
   2006 	if (fs->lfs_version == 1)
   2007 		el_size = sizeof(u_long);
   2008 	else
   2009 		el_size = sizeof(u_int32_t);
   2010 	for (bpp = sp->bpp, i = nblocks - 1; i--; ) {
   2011 		++bpp;
   2012 		/* Loop through gop_write cluster blocks */
   2013 		for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
   2014 		     byteoffset += fs->lfs_bsize) {
   2015 #ifdef LFS_USE_B_INVAL
   2016 			if (((*bpp)->b_flags & (B_CALL | B_INVAL)) ==
   2017 			    (B_CALL | B_INVAL)) {
   2018 				if (copyin((caddr_t)(*bpp)->b_saveaddr +
   2019 					   byteoffset, dp, el_size)) {
   2020 					panic("lfs_writeseg: copyin failed [1]:"
   2021 						" ino %d blk %" PRId64,
   2022 						VTOI((*bpp)->b_vp)->i_number,
   2023 						(*bpp)->b_lblkno);
   2024 				}
   2025 			} else
   2026 #endif /* LFS_USE_B_INVAL */
   2027 			{
   2028 				sum = lfs_cksum_part(
   2029 				    (*bpp)->b_data + byteoffset, el_size, sum);
   2030 			}
   2031 		}
   2032 	}
   2033 	if (fs->lfs_version == 1)
   2034 		ssp->ss_ocreate = time.tv_sec;
   2035 	else {
   2036 		ssp->ss_create = time.tv_sec;
   2037 		ssp->ss_serial = ++fs->lfs_serial;
   2038 		ssp->ss_ident  = fs->lfs_ident;
   2039 	}
   2040 	ssp->ss_datasum = lfs_cksum_fold(sum);
   2041 	ssp->ss_sumsum = cksum(&ssp->ss_datasum,
   2042 	    fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
   2043 
   2044 	simple_lock(&fs->lfs_interlock);
   2045 	fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
   2046 			  btofsb(fs, fs->lfs_sumsize));
   2047 	fs->lfs_dmeta += (btofsb(fs, ninos * fs->lfs_ibsize) +
   2048 			  btofsb(fs, fs->lfs_sumsize));
   2049 	simple_unlock(&fs->lfs_interlock);
   2050 
   2051 	/*
   2052 	 * When we simply write the blocks we lose a rotation for every block
   2053 	 * written.  To avoid this problem, we cluster the buffers into a
   2054 	 * chunk and write the chunk.  MAXPHYS is the largest size I/O
   2055 	 * devices can handle, use that for the size of the chunks.
   2056 	 *
   2057 	 * Blocks that are already clusters (from GOP_WRITE), however, we
   2058 	 * don't bother to copy into other clusters.
   2059 	 */
   2060 
   2061 #define CHUNKSIZE MAXPHYS
   2062 
   2063 	if (devvp == NULL)
   2064 		panic("devvp is NULL");
   2065 	for (bpp = sp->bpp, i = nblocks; i;) {
   2066 		cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
   2067 		cl = cbp->b_private;
   2068 
   2069 		cbp->b_flags |= B_ASYNC | B_BUSY;
   2070 		cbp->b_bcount = 0;
   2071 
   2072 #if defined(DEBUG) && defined(DIAGNOSTIC)
   2073 		if (bpp - sp->bpp > (fs->lfs_sumsize - SEGSUM_SIZE(fs))
   2074 		    / sizeof(int32_t)) {
   2075 			panic("lfs_writeseg: real bpp overwrite");
   2076 		}
   2077 		if (bpp - sp->bpp > segsize(fs) / fs->lfs_fsize) {
   2078 			panic("lfs_writeseg: theoretical bpp overwrite");
   2079 		}
   2080 #endif
   2081 
   2082 		/*
   2083 		 * Construct the cluster.
   2084 		 */
   2085 		simple_lock(&fs->lfs_interlock);
   2086 		++fs->lfs_iocount;
   2087 		simple_unlock(&fs->lfs_interlock);
   2088 		while (i && cbp->b_bcount < CHUNKSIZE) {
   2089 			bp = *bpp;
   2090 
   2091 			if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
   2092 				break;
   2093 			if (cbp->b_bcount > 0 && !(cl->flags & LFS_CL_MALLOC))
   2094 				break;
   2095 
   2096 			/* Clusters from GOP_WRITE are expedited */
   2097 			if (bp->b_bcount > fs->lfs_bsize) {
   2098 				if (cbp->b_bcount > 0)
   2099 					/* Put in its own buffer */
   2100 					break;
   2101 				else {
   2102 					cbp->b_data = bp->b_data;
   2103 				}
   2104 			} else if (cbp->b_bcount == 0) {
   2105 				p = cbp->b_data = lfs_malloc(fs, CHUNKSIZE,
   2106 							     LFS_NB_CLUSTER);
   2107 				cl->flags |= LFS_CL_MALLOC;
   2108 			}
   2109 #ifdef DIAGNOSTIC
   2110 			if (dtosn(fs, dbtofsb(fs, bp->b_blkno +
   2111 					      btodb(bp->b_bcount - 1))) !=
   2112 			    sp->seg_number) {
   2113 				printf("blk size %d daddr %" PRIx64
   2114 				    " not in seg %d\n",
   2115 				    bp->b_bcount, bp->b_blkno,
   2116 				    sp->seg_number);
   2117 				panic("segment overwrite");
   2118 			}
   2119 #endif
   2120 
   2121 #ifdef LFS_USE_B_INVAL
   2122 			/*
   2123 			 * Fake buffers from the cleaner are marked as B_INVAL.
   2124 			 * We need to copy the data from user space rather than
   2125 			 * from the buffer indicated.
   2126 			 * XXX == what do I do on an error?
   2127 			 */
   2128 			if ((bp->b_flags & (B_CALL|B_INVAL)) ==
   2129 			    (B_CALL|B_INVAL)) {
   2130 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
   2131 					panic("lfs_writeseg: "
   2132 					    "copyin failed [2]");
   2133 			} else
   2134 #endif /* LFS_USE_B_INVAL */
   2135 			if (cl->flags & LFS_CL_MALLOC) {
   2136 				/* copy data into our cluster. */
   2137 				memcpy(p, bp->b_data, bp->b_bcount);
   2138 				p += bp->b_bcount;
   2139 			}
   2140 
   2141 			cbp->b_bcount += bp->b_bcount;
   2142 			cl->bufsize += bp->b_bcount;
   2143 
   2144 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | B_DONE);
   2145 			cl->bpp[cl->bufcount++] = bp;
   2146 			vp = bp->b_vp;
   2147 			s = splbio();
   2148 			reassignbuf(bp, vp);
   2149 			V_INCR_NUMOUTPUT(vp);
   2150 			splx(s);
   2151 
   2152 			bpp++;
   2153 			i--;
   2154 		}
   2155 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
   2156 			BIO_SETPRIO(cbp, BPRIO_TIMECRITICAL);
   2157 		else
   2158 			BIO_SETPRIO(cbp, BPRIO_TIMELIMITED);
   2159 		s = splbio();
   2160 		V_INCR_NUMOUTPUT(devvp);
   2161 		splx(s);
   2162 		VOP_STRATEGY(devvp, cbp);
   2163 		curproc->p_stats->p_ru.ru_oublock++;
   2164 	}
   2165 
   2166 	if (lfs_dostats) {
   2167 		++lfs_stats.psegwrites;
   2168 		lfs_stats.blocktot += nblocks - 1;
   2169 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
   2170 			++lfs_stats.psyncwrites;
   2171 		if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
   2172 			++lfs_stats.pcleanwrites;
   2173 			lfs_stats.cleanblocks += nblocks - 1;
   2174 		}
   2175 	}
   2176 	return (lfs_initseg(fs) || do_again);
   2177 }
   2178 
   2179 void
   2180 lfs_writesuper(struct lfs *fs, daddr_t daddr)
   2181 {
   2182 	struct buf *bp;
   2183 	int s;
   2184 	struct vnode *devvp = VTOI(fs->lfs_ivnode)->i_devvp;
   2185 
   2186 	ASSERT_MAYBE_SEGLOCK(fs);
   2187 #ifdef DIAGNOSTIC
   2188 	KASSERT(fs->lfs_magic == LFS_MAGIC);
   2189 #endif
   2190 	/*
   2191 	 * If we can write one superblock while another is in
   2192 	 * progress, we risk not having a complete checkpoint if we crash.
   2193 	 * So, block here if a superblock write is in progress.
   2194 	 */
   2195 	simple_lock(&fs->lfs_interlock);
   2196 	s = splbio();
   2197 	while (fs->lfs_sbactive) {
   2198 		ltsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0,
   2199 			&fs->lfs_interlock);
   2200 	}
   2201 	fs->lfs_sbactive = daddr;
   2202 	splx(s);
   2203 	simple_unlock(&fs->lfs_interlock);
   2204 
   2205 	/* Set timestamp of this version of the superblock */
   2206 	if (fs->lfs_version == 1)
   2207 		fs->lfs_otstamp = time.tv_sec;
   2208 	fs->lfs_tstamp = time.tv_sec;
   2209 
   2210 	/* Checksum the superblock and copy it into a buffer. */
   2211 	fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
   2212 	bp = lfs_newbuf(fs, devvp,
   2213 	    fsbtodb(fs, daddr), LFS_SBPAD, LFS_NB_SBLOCK);
   2214 	memset(bp->b_data + sizeof(struct dlfs), 0,
   2215 	    LFS_SBPAD - sizeof(struct dlfs));
   2216 	*(struct dlfs *)bp->b_data = fs->lfs_dlfs;
   2217 
   2218 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
   2219 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
   2220 	bp->b_iodone = lfs_supercallback;
   2221 
   2222 	if (fs->lfs_sp != NULL && fs->lfs_sp->seg_flags & SEGM_SYNC)
   2223 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   2224 	else
   2225 		BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
   2226 	curproc->p_stats->p_ru.ru_oublock++;
   2227 	s = splbio();
   2228 	V_INCR_NUMOUTPUT(bp->b_vp);
   2229 	splx(s);
   2230 	simple_lock(&fs->lfs_interlock);
   2231 	++fs->lfs_iocount;
   2232 	simple_unlock(&fs->lfs_interlock);
   2233 	VOP_STRATEGY(devvp, bp);
   2234 }
   2235 
   2236 /*
   2237  * Logical block number match routines used when traversing the dirty block
   2238  * chain.
   2239  */
   2240 int
   2241 lfs_match_fake(struct lfs *fs, struct buf *bp)
   2242 {
   2243 
   2244 	ASSERT_SEGLOCK(fs);
   2245 	return LFS_IS_MALLOC_BUF(bp);
   2246 }
   2247 
   2248 #if 0
   2249 int
   2250 lfs_match_real(struct lfs *fs, struct buf *bp)
   2251 {
   2252 
   2253 	ASSERT_SEGLOCK(fs);
   2254 	return (lfs_match_data(fs, bp) && !lfs_match_fake(fs, bp));
   2255 }
   2256 #endif
   2257 
   2258 int
   2259 lfs_match_data(struct lfs *fs, struct buf *bp)
   2260 {
   2261 
   2262 	ASSERT_SEGLOCK(fs);
   2263 	return (bp->b_lblkno >= 0);
   2264 }
   2265 
   2266 int
   2267 lfs_match_indir(struct lfs *fs, struct buf *bp)
   2268 {
   2269 	daddr_t lbn;
   2270 
   2271 	ASSERT_SEGLOCK(fs);
   2272 	lbn = bp->b_lblkno;
   2273 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
   2274 }
   2275 
   2276 int
   2277 lfs_match_dindir(struct lfs *fs, struct buf *bp)
   2278 {
   2279 	daddr_t lbn;
   2280 
   2281 	ASSERT_SEGLOCK(fs);
   2282 	lbn = bp->b_lblkno;
   2283 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
   2284 }
   2285 
   2286 int
   2287 lfs_match_tindir(struct lfs *fs, struct buf *bp)
   2288 {
   2289 	daddr_t lbn;
   2290 
   2291 	ASSERT_SEGLOCK(fs);
   2292 	lbn = bp->b_lblkno;
   2293 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
   2294 }
   2295 
   2296 /*
   2297  * XXX - The only buffers that are going to hit these functions are the
   2298  * segment write blocks, or the segment summaries, or the superblocks.
   2299  *
   2300  * All of the above are created by lfs_newbuf, and so do not need to be
   2301  * released via brelse.
   2302  */
   2303 void
   2304 lfs_callback(struct buf *bp)
   2305 {
   2306 	struct lfs *fs;
   2307 
   2308 	fs = bp->b_private;
   2309 	ASSERT_NO_SEGLOCK(fs);
   2310 	lfs_freebuf(fs, bp);
   2311 }
   2312 
   2313 static void
   2314 lfs_super_aiodone(struct buf *bp)
   2315 {
   2316 	struct lfs *fs;
   2317 
   2318 	fs = bp->b_private;
   2319 	ASSERT_NO_SEGLOCK(fs);
   2320 	simple_lock(&fs->lfs_interlock);
   2321 	fs->lfs_sbactive = 0;
   2322 	if (--fs->lfs_iocount <= 1)
   2323 		wakeup(&fs->lfs_iocount);
   2324 	simple_unlock(&fs->lfs_interlock);
   2325 	wakeup(&fs->lfs_sbactive);
   2326 	lfs_freebuf(fs, bp);
   2327 }
   2328 
   2329 static void
   2330 lfs_cluster_aiodone(struct buf *bp)
   2331 {
   2332 	struct lfs_cluster *cl;
   2333 	struct lfs *fs;
   2334 	struct buf *tbp, *fbp;
   2335 	struct vnode *vp, *devvp;
   2336 	struct inode *ip;
   2337 	int s, error=0;
   2338 
   2339 	if (bp->b_flags & B_ERROR)
   2340 		error = bp->b_error;
   2341 
   2342 	cl = bp->b_private;
   2343 	fs = cl->fs;
   2344 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
   2345 	ASSERT_NO_SEGLOCK(fs);
   2346 
   2347 	/* Put the pages back, and release the buffer */
   2348 	while (cl->bufcount--) {
   2349 		tbp = cl->bpp[cl->bufcount];
   2350 		KASSERT(tbp->b_flags & B_BUSY);
   2351 		if (error) {
   2352 			tbp->b_flags |= B_ERROR;
   2353 			tbp->b_error = error;
   2354 		}
   2355 
   2356 		/*
   2357 		 * We're done with tbp.	 If it has not been re-dirtied since
   2358 		 * the cluster was written, free it.  Otherwise, keep it on
   2359 		 * the locked list to be written again.
   2360 		 */
   2361 		vp = tbp->b_vp;
   2362 
   2363 		tbp->b_flags &= ~B_GATHERED;
   2364 
   2365 		LFS_BCLEAN_LOG(fs, tbp);
   2366 
   2367 		if (!(tbp->b_flags & B_CALL)) {
   2368 			KASSERT(tbp->b_flags & B_LOCKED);
   2369 			s = splbio();
   2370 			simple_lock(&bqueue_slock);
   2371 			bremfree(tbp);
   2372 			simple_unlock(&bqueue_slock);
   2373 			if (vp)
   2374 				reassignbuf(tbp, vp);
   2375 			splx(s);
   2376 			tbp->b_flags |= B_ASYNC; /* for biodone */
   2377 		}
   2378 
   2379 		if ((tbp->b_flags & (B_LOCKED | B_DELWRI)) == B_LOCKED)
   2380 			LFS_UNLOCK_BUF(tbp);
   2381 
   2382 		if (tbp->b_flags & B_DONE) {
   2383 			DLOG((DLOG_SEG, "blk %d biodone already (flags %lx)\n",
   2384 				cl->bufcount, (long)tbp->b_flags));
   2385 		}
   2386 
   2387 		if ((tbp->b_flags & B_CALL) && !LFS_IS_MALLOC_BUF(tbp)) {
   2388 			/*
   2389 			 * A buffer from the page daemon.
   2390 			 * We use the same iodone as it does,
   2391 			 * so we must manually disassociate its
   2392 			 * buffers from the vp.
   2393 			 */
   2394 			if (tbp->b_vp) {
   2395 				/* This is just silly */
   2396 				s = splbio();
   2397 				brelvp(tbp);
   2398 				tbp->b_vp = vp;
   2399 				splx(s);
   2400 			}
   2401 			/* Put it back the way it was */
   2402 			tbp->b_flags |= B_ASYNC;
   2403 			/* Master buffers have B_AGE */
   2404 			if (tbp->b_private == tbp)
   2405 				tbp->b_flags |= B_AGE;
   2406 		}
   2407 		s = splbio();
   2408 		biodone(tbp);
   2409 
   2410 		/*
   2411 		 * If this is the last block for this vnode, but
   2412 		 * there are other blocks on its dirty list,
   2413 		 * set IN_MODIFIED/IN_CLEANING depending on what
   2414 		 * sort of block.  Only do this for our mount point,
   2415 		 * not for, e.g., inode blocks that are attached to
   2416 		 * the devvp.
   2417 		 * XXX KS - Shouldn't we set *both* if both types
   2418 		 * of blocks are present (traverse the dirty list?)
   2419 		 */
   2420 		simple_lock(&global_v_numoutput_slock);
   2421 		if (vp != devvp && vp->v_numoutput == 0 &&
   2422 		    (fbp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL) {
   2423 			ip = VTOI(vp);
   2424 			DLOG((DLOG_SEG, "lfs_cluster_aiodone: mark ino %d\n",
   2425 			       ip->i_number));
   2426 			if (LFS_IS_MALLOC_BUF(fbp))
   2427 				LFS_SET_UINO(ip, IN_CLEANING);
   2428 			else
   2429 				LFS_SET_UINO(ip, IN_MODIFIED);
   2430 		}
   2431 		simple_unlock(&global_v_numoutput_slock);
   2432 		splx(s);
   2433 		wakeup(vp);
   2434 	}
   2435 
   2436 	/* Fix up the cluster buffer, and release it */
   2437 	if (cl->flags & LFS_CL_MALLOC)
   2438 		lfs_free(fs, bp->b_data, LFS_NB_CLUSTER);
   2439 	putiobuf(bp);
   2440 
   2441 	/* Note i/o done */
   2442 	if (cl->flags & LFS_CL_SYNC) {
   2443 		if (--cl->seg->seg_iocount == 0)
   2444 			wakeup(&cl->seg->seg_iocount);
   2445 	}
   2446 	simple_lock(&fs->lfs_interlock);
   2447 #ifdef DIAGNOSTIC
   2448 	if (fs->lfs_iocount == 0)
   2449 		panic("lfs_cluster_aiodone: zero iocount");
   2450 #endif
   2451 	if (--fs->lfs_iocount <= 1)
   2452 		wakeup(&fs->lfs_iocount);
   2453 	simple_unlock(&fs->lfs_interlock);
   2454 
   2455 	pool_put(&fs->lfs_bpppool, cl->bpp);
   2456 	cl->bpp = NULL;
   2457 	pool_put(&fs->lfs_clpool, cl);
   2458 }
   2459 
   2460 static void
   2461 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
   2462 {
   2463 	/* reset b_iodone for when this is a single-buf i/o. */
   2464 	bp->b_iodone = aiodone;
   2465 
   2466 	simple_lock(&uvm.aiodoned_lock);	/* locks uvm.aio_done */
   2467 	TAILQ_INSERT_TAIL(&uvm.aio_done, bp, b_freelist);
   2468 	wakeup(&uvm.aiodoned);
   2469 	simple_unlock(&uvm.aiodoned_lock);
   2470 }
   2471 
   2472 static void
   2473 lfs_cluster_callback(struct buf *bp)
   2474 {
   2475 
   2476 	lfs_generic_callback(bp, lfs_cluster_aiodone);
   2477 }
   2478 
   2479 void
   2480 lfs_supercallback(struct buf *bp)
   2481 {
   2482 
   2483 	lfs_generic_callback(bp, lfs_super_aiodone);
   2484 }
   2485 
   2486 /*
   2487  * Shellsort (diminishing increment sort) from Data Structures and
   2488  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
   2489  * see also Knuth Vol. 3, page 84.  The increments are selected from
   2490  * formula (8), page 95.  Roughly O(N^3/2).
   2491  */
   2492 /*
   2493  * This is our own private copy of shellsort because we want to sort
   2494  * two parallel arrays (the array of buffer pointers and the array of
   2495  * logical block numbers) simultaneously.  Note that we cast the array
   2496  * of logical block numbers to a unsigned in this routine so that the
   2497  * negative block numbers (meta data blocks) sort AFTER the data blocks.
   2498  */
   2499 
   2500 void
   2501 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb, int size)
   2502 {
   2503 	static int __rsshell_increments[] = { 4, 1, 0 };
   2504 	int incr, *incrp, t1, t2;
   2505 	struct buf *bp_temp;
   2506 
   2507 #ifdef DEBUG
   2508 	incr = 0;
   2509 	for (t1 = 0; t1 < nmemb; t1++) {
   2510 		for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
   2511 			if (lb_array[incr++] != bp_array[t1]->b_lblkno + t2) {
   2512 				/* dump before panic */
   2513 				printf("lfs_shellsort: nmemb=%d, size=%d\n",
   2514 				    nmemb, size);
   2515 				incr = 0;
   2516 				for (t1 = 0; t1 < nmemb; t1++) {
   2517 					const struct buf *bp = bp_array[t1];
   2518 
   2519 					printf("bp[%d]: lbn=%" PRIu64 ", size=%"
   2520 					    PRIu64 "\n", t1,
   2521 					    (uint64_t)bp->b_bcount,
   2522 					    (uint64_t)bp->b_lblkno);
   2523 					printf("lbns:");
   2524 					for (t2 = 0; t2 * size < bp->b_bcount;
   2525 					    t2++) {
   2526 						printf(" %" PRId32,
   2527 						    lb_array[incr++]);
   2528 					}
   2529 					printf("\n");
   2530 				}
   2531 				panic("lfs_shellsort: inconsistent input");
   2532 			}
   2533 		}
   2534 	}
   2535 #endif
   2536 
   2537 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
   2538 		for (t1 = incr; t1 < nmemb; ++t1)
   2539 			for (t2 = t1 - incr; t2 >= 0;)
   2540 				if ((u_int32_t)bp_array[t2]->b_lblkno >
   2541 				    (u_int32_t)bp_array[t2 + incr]->b_lblkno) {
   2542 					bp_temp = bp_array[t2];
   2543 					bp_array[t2] = bp_array[t2 + incr];
   2544 					bp_array[t2 + incr] = bp_temp;
   2545 					t2 -= incr;
   2546 				} else
   2547 					break;
   2548 
   2549 	/* Reform the list of logical blocks */
   2550 	incr = 0;
   2551 	for (t1 = 0; t1 < nmemb; t1++) {
   2552 		for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
   2553 			lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
   2554 		}
   2555 	}
   2556 }
   2557 
   2558 /*
   2559  * Call vget with LK_NOWAIT.  If we are the one who holds VXLOCK/VFREEING,
   2560  * however, we must press on.  Just fake success in that case.
   2561  */
   2562 int
   2563 lfs_vref(struct vnode *vp)
   2564 {
   2565 	int error;
   2566 	struct lfs *fs;
   2567 
   2568 	fs = VTOI(vp)->i_lfs;
   2569 
   2570 	ASSERT_MAYBE_SEGLOCK(fs);
   2571 
   2572 	/*
   2573 	 * If we return 1 here during a flush, we risk vinvalbuf() not
   2574 	 * being able to flush all of the pages from this vnode, which
   2575 	 * will cause it to panic.  So, return 0 if a flush is in progress.
   2576 	 */
   2577 	error = vget(vp, LK_NOWAIT);
   2578 	if (error == EBUSY && IS_FLUSHING(VTOI(vp)->i_lfs, vp)) {
   2579 		++fs->lfs_flushvp_fakevref;
   2580 		return 0;
   2581 	}
   2582 	return error;
   2583 }
   2584 
   2585 /*
   2586  * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
   2587  * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
   2588  */
   2589 void
   2590 lfs_vunref(struct vnode *vp)
   2591 {
   2592 	struct lfs *fs;
   2593 
   2594 	fs = VTOI(vp)->i_lfs;
   2595 	ASSERT_MAYBE_SEGLOCK(fs);
   2596 
   2597 	/*
   2598 	 * Analogous to lfs_vref, if the node is flushing, fake it.
   2599 	 */
   2600 	if (IS_FLUSHING(fs, vp) && fs->lfs_flushvp_fakevref) {
   2601 		--fs->lfs_flushvp_fakevref;
   2602 		return;
   2603 	}
   2604 
   2605 	simple_lock(&vp->v_interlock);
   2606 #ifdef DIAGNOSTIC
   2607 	if (vp->v_usecount <= 0) {
   2608 		printf("lfs_vunref: inum is %llu\n", (unsigned long long)
   2609 		    VTOI(vp)->i_number);
   2610 		printf("lfs_vunref: flags are 0x%lx\n", (u_long)vp->v_flag);
   2611 		printf("lfs_vunref: usecount = %ld\n", (long)vp->v_usecount);
   2612 		panic("lfs_vunref: v_usecount < 0");
   2613 	}
   2614 #endif
   2615 	vp->v_usecount--;
   2616 	if (vp->v_usecount > 0) {
   2617 		simple_unlock(&vp->v_interlock);
   2618 		return;
   2619 	}
   2620 	/*
   2621 	 * insert at tail of LRU list
   2622 	 */
   2623 	simple_lock(&vnode_free_list_slock);
   2624 	if (vp->v_holdcnt > 0)
   2625 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
   2626 	else
   2627 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
   2628 	simple_unlock(&vnode_free_list_slock);
   2629 	simple_unlock(&vp->v_interlock);
   2630 }
   2631 
   2632 /*
   2633  * We use this when we have vnodes that were loaded in solely for cleaning.
   2634  * There is no reason to believe that these vnodes will be referenced again
   2635  * soon, since the cleaning process is unrelated to normal filesystem
   2636  * activity.  Putting cleaned vnodes at the tail of the list has the effect
   2637  * of flushing the vnode LRU.  So, put vnodes that were loaded only for
   2638  * cleaning at the head of the list, instead.
   2639  */
   2640 void
   2641 lfs_vunref_head(struct vnode *vp)
   2642 {
   2643 
   2644 	ASSERT_SEGLOCK(VTOI(vp)->i_lfs);
   2645 	simple_lock(&vp->v_interlock);
   2646 #ifdef DIAGNOSTIC
   2647 	if (vp->v_usecount == 0) {
   2648 		panic("lfs_vunref: v_usecount<0");
   2649 	}
   2650 #endif
   2651 	vp->v_usecount--;
   2652 	if (vp->v_usecount > 0) {
   2653 		simple_unlock(&vp->v_interlock);
   2654 		return;
   2655 	}
   2656 	/*
   2657 	 * insert at head of LRU list
   2658 	 */
   2659 	simple_lock(&vnode_free_list_slock);
   2660 	if (vp->v_holdcnt > 0)
   2661 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
   2662 	else
   2663 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
   2664 	simple_unlock(&vnode_free_list_slock);
   2665 	simple_unlock(&vp->v_interlock);
   2666 }
   2667 
   2668 
   2669 /*
   2670  * Set up an FINFO entry for a new file.  The fip pointer is assumed to
   2671  * point at uninitialized space.
   2672  */
   2673 void
   2674 lfs_acquire_finfo(struct lfs *fs, ino_t ino, int vers)
   2675 {
   2676 	struct segment *sp = fs->lfs_sp;
   2677 
   2678 	if (sp->seg_bytes_left < fs->lfs_bsize ||
   2679 	    sp->sum_bytes_left < sizeof(struct finfo))
   2680 		(void) lfs_writeseg(fs, fs->lfs_sp);
   2681 
   2682 	sp->sum_bytes_left -= FINFOSIZE;
   2683 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
   2684 	sp->fip->fi_nblocks = 0;
   2685 	sp->fip->fi_ino = ino;
   2686 	sp->fip->fi_version = vers;
   2687 }
   2688 
   2689 /*
   2690  * Release the FINFO entry, either clearing out an unused entry or
   2691  * advancing us to the next available entry.
   2692  */
   2693 void
   2694 lfs_release_finfo(struct lfs *fs)
   2695 {
   2696 	struct segment *sp = fs->lfs_sp;
   2697 
   2698 	if (sp->fip->fi_nblocks != 0) {
   2699 		sp->fip = (FINFO*)((caddr_t)sp->fip + FINFOSIZE +
   2700 			sizeof(int32_t) * sp->fip->fi_nblocks);
   2701 		sp->start_lbp = &sp->fip->fi_blocks[0];
   2702 	} else {
   2703 		sp->sum_bytes_left += FINFOSIZE;
   2704 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
   2705 	}
   2706 }
   2707