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