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