Home | History | Annotate | Line # | Download | only in lfs
lfs_segment.c revision 1.60
      1 /*	$NetBSD: lfs_segment.c,v 1.60 2000/11/12 02:13:51 toshii Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_segment.c	8.10 (Berkeley) 6/10/95
     71  */
     72 
     73 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str))
     74 
     75 #include "opt_ddb.h"
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/namei.h>
     79 #include <sys/kernel.h>
     80 #include <sys/resourcevar.h>
     81 #include <sys/file.h>
     82 #include <sys/stat.h>
     83 #include <sys/buf.h>
     84 #include <sys/proc.h>
     85 #include <sys/conf.h>
     86 #include <sys/vnode.h>
     87 #include <sys/malloc.h>
     88 #include <sys/mount.h>
     89 
     90 #include <miscfs/specfs/specdev.h>
     91 #include <miscfs/fifofs/fifo.h>
     92 
     93 #include <ufs/ufs/quota.h>
     94 #include <ufs/ufs/inode.h>
     95 #include <ufs/ufs/dir.h>
     96 #include <ufs/ufs/ufsmount.h>
     97 #include <ufs/ufs/ufs_extern.h>
     98 
     99 #include <ufs/lfs/lfs.h>
    100 #include <ufs/lfs/lfs_extern.h>
    101 
    102 extern int count_lock_queue __P((void));
    103 extern struct simplelock vnode_free_list_slock;		/* XXX */
    104 
    105 /*
    106  * Determine if it's OK to start a partial in this segment, or if we need
    107  * to go on to a new segment.
    108  */
    109 #define	LFS_PARTIAL_FITS(fs) \
    110 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
    111 	1 << (fs)->lfs_fsbtodb)
    112 
    113 void	 lfs_callback __P((struct buf *));
    114 int	 lfs_gather __P((struct lfs *, struct segment *,
    115 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
    116 int	 lfs_gatherblock __P((struct segment *, struct buf *, int *));
    117 void	 lfs_iset __P((struct inode *, ufs_daddr_t, time_t));
    118 int	 lfs_match_fake __P((struct lfs *, struct buf *));
    119 int	 lfs_match_data __P((struct lfs *, struct buf *));
    120 int	 lfs_match_dindir __P((struct lfs *, struct buf *));
    121 int	 lfs_match_indir __P((struct lfs *, struct buf *));
    122 int	 lfs_match_tindir __P((struct lfs *, struct buf *));
    123 void	 lfs_newseg __P((struct lfs *));
    124 void	 lfs_shellsort __P((struct buf **, ufs_daddr_t *, int));
    125 void	 lfs_supercallback __P((struct buf *));
    126 void	 lfs_updatemeta __P((struct segment *));
    127 int	 lfs_vref __P((struct vnode *));
    128 void	 lfs_vunref __P((struct vnode *));
    129 void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
    130 int	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
    131 int	 lfs_writeseg __P((struct lfs *, struct segment *));
    132 void	 lfs_writesuper __P((struct lfs *, daddr_t));
    133 int	 lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
    134 	    struct segment *sp, int dirops));
    135 
    136 int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
    137 int	lfs_writeindir = 1;             /* whether to flush indir on non-ckp */
    138 int	lfs_clean_vnhead = 0;		/* Allow freeing to head of vn list */
    139 int	lfs_dirvcount = 0;		/* # active dirops */
    140 
    141 /* Statistics Counters */
    142 int lfs_dostats = 1;
    143 struct lfs_stats lfs_stats;
    144 
    145 /* op values to lfs_writevnodes */
    146 #define	VN_REG	        0
    147 #define	VN_DIROP	1
    148 #define	VN_EMPTY	2
    149 #define VN_CLEAN        3
    150 
    151 #define LFS_MAX_ACTIVE          10
    152 
    153 /*
    154  * XXX KS - Set modification time on the Ifile, so the cleaner can
    155  * read the fs mod time off of it.  We don't set IN_UPDATE here,
    156  * since we don't really need this to be flushed to disk (and in any
    157  * case that wouldn't happen to the Ifile until we checkpoint).
    158  */
    159 void
    160 lfs_imtime(fs)
    161 	struct lfs *fs;
    162 {
    163 	struct timespec ts;
    164 	struct inode *ip;
    165 
    166 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    167 	ip = VTOI(fs->lfs_ivnode);
    168 	ip->i_ffs_mtime = ts.tv_sec;
    169 	ip->i_ffs_mtimensec = ts.tv_nsec;
    170 }
    171 
    172 /*
    173  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
    174  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
    175  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
    176  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
    177  */
    178 
    179 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
    180 #define IS_FLUSHING(fs,vp)  ((fs)->lfs_flushvp == (vp))
    181 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
    182 
    183 int
    184 lfs_vflush(vp)
    185 	struct vnode *vp;
    186 {
    187 	struct inode *ip;
    188 	struct lfs *fs;
    189 	struct segment *sp;
    190 	struct buf *bp, *nbp, *tbp, *tnbp;
    191 	int error, s;
    192 
    193 	ip = VTOI(vp);
    194 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
    195 
    196 	if(ip->i_flag & IN_CLEANING) {
    197 #ifdef DEBUG_LFS
    198 		ivndebug(vp,"vflush/in_cleaning");
    199 #endif
    200 		LFS_CLR_UINO(ip, IN_CLEANING);
    201 		LFS_SET_UINO(ip, IN_MODIFIED);
    202 
    203 		/*
    204 		 * Toss any cleaning buffers that have real counterparts
    205 		 * to avoid losing new data
    206 		 */
    207 		s = splbio();
    208 		for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
    209 			nbp = bp->b_vnbufs.le_next;
    210 			if(bp->b_flags & B_CALL) {
    211 				for(tbp=vp->v_dirtyblkhd.lh_first; tbp;
    212 				    tbp=tnbp)
    213 				{
    214 					tnbp = tbp->b_vnbufs.le_next;
    215 					if(tbp->b_vp == bp->b_vp
    216 					   && tbp->b_lblkno == bp->b_lblkno
    217 					   && tbp != bp)
    218 					{
    219 						lfs_freebuf(bp);
    220 					}
    221 				}
    222 			}
    223 		}
    224 		splx(s);
    225 	}
    226 
    227 	/* If the node is being written, wait until that is done */
    228 	if(WRITEINPROG(vp)) {
    229 #ifdef DEBUG_LFS
    230 		ivndebug(vp,"vflush/writeinprog");
    231 #endif
    232 		tsleep(vp, PRIBIO+1, "lfs_vw", 0);
    233 	}
    234 
    235 	/* Protect against VXLOCK deadlock in vinvalbuf() */
    236 	lfs_seglock(fs, SEGM_SYNC);
    237 
    238 	/* If we're supposed to flush a freed inode, just toss it */
    239 	/* XXX - seglock, so these buffers can't be gathered, right? */
    240 	if(ip->i_ffs_mode == 0) {
    241 		printf("lfs_vflush: ino %d is freed, not flushing\n",
    242 			ip->i_number);
    243 		s = splbio();
    244 		for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
    245 			nbp = bp->b_vnbufs.le_next;
    246 			/* Copied from lfs_writeseg */
    247 			if (bp->b_flags & B_CALL) {
    248 				/* if B_CALL, it was created with newbuf */
    249 				lfs_freebuf(bp);
    250 			} else {
    251 				bremfree(bp);
    252 				bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
    253                                          B_LOCKED | B_GATHERED);
    254 				bp->b_flags |= B_DONE;
    255 				reassignbuf(bp, vp);
    256 				brelse(bp);
    257 			}
    258 		}
    259 		splx(s);
    260 		LFS_CLR_UINO(ip, IN_CLEANING);
    261 		LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
    262 		ip->i_flag &= ~IN_ALLMOD;
    263 		printf("lfs_vflush: done not flushing ino %d\n",
    264 			ip->i_number);
    265 		lfs_segunlock(fs);
    266 		return 0;
    267 	}
    268 
    269 	SET_FLUSHING(fs,vp);
    270 	if (fs->lfs_nactive > LFS_MAX_ACTIVE) {
    271 		error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP);
    272 		CLR_FLUSHING(fs,vp);
    273 		lfs_segunlock(fs);
    274 		return error;
    275 	}
    276 	sp = fs->lfs_sp;
    277 
    278 	if (vp->v_dirtyblkhd.lh_first == NULL) {
    279 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
    280 	} else if((ip->i_flag & IN_CLEANING) &&
    281 		  (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
    282 #ifdef DEBUG_LFS
    283 		ivndebug(vp,"vflush/clean");
    284 #endif
    285 		lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
    286 	}
    287 	else if(lfs_dostats) {
    288 		if(vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & IN_ALLMOD))
    289 			++lfs_stats.vflush_invoked;
    290 #ifdef DEBUG_LFS
    291 		ivndebug(vp,"vflush");
    292 #endif
    293 	}
    294 
    295 #ifdef DIAGNOSTIC
    296 	/* XXX KS This actually can happen right now, though it shouldn't(?) */
    297 	if(vp->v_flag & VDIROP) {
    298 		printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
    299 		/* panic("VDIROP being flushed...this can\'t happen"); */
    300 	}
    301 	if(vp->v_usecount<0) {
    302 		printf("usecount=%ld\n",vp->v_usecount);
    303 		panic("lfs_vflush: usecount<0");
    304 	}
    305 #endif
    306 
    307 	do {
    308 		do {
    309 			if (vp->v_dirtyblkhd.lh_first != NULL)
    310 				lfs_writefile(fs, sp, vp);
    311 		} while (lfs_writeinode(fs, sp, ip));
    312 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
    313 
    314 	if(lfs_dostats) {
    315 		++lfs_stats.nwrites;
    316 		if (sp->seg_flags & SEGM_SYNC)
    317 			++lfs_stats.nsync_writes;
    318 		if (sp->seg_flags & SEGM_CKP)
    319 			++lfs_stats.ncheckpoints;
    320 	}
    321 	lfs_segunlock(fs);
    322 
    323 	CLR_FLUSHING(fs,vp);
    324 	return (0);
    325 }
    326 
    327 #ifdef DEBUG_LFS_VERBOSE
    328 # 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)
    329 #else
    330 # define vndebug(vp,str)
    331 #endif
    332 
    333 int
    334 lfs_writevnodes(fs, mp, sp, op)
    335 	struct lfs *fs;
    336 	struct mount *mp;
    337 	struct segment *sp;
    338 	int op;
    339 {
    340 	struct inode *ip;
    341 	struct vnode *vp;
    342 	int inodes_written=0, only_cleaning;
    343 	int needs_unlock;
    344 
    345 #ifndef LFS_NO_BACKVP_HACK
    346 	/* BEGIN HACK */
    347 #define	VN_OFFSET	(((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
    348 #define	BACK_VP(VP)	((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
    349 #define	BEG_OF_VLIST	((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
    350 
    351 	/* Find last vnode. */
    352  loop:	for (vp = mp->mnt_vnodelist.lh_first;
    353 	     vp && vp->v_mntvnodes.le_next != NULL;
    354 	     vp = vp->v_mntvnodes.le_next);
    355 	for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
    356 #else
    357 	loop:
    358 	for (vp = mp->mnt_vnodelist.lh_first;
    359 	     vp != NULL;
    360 	     vp = vp->v_mntvnodes.le_next) {
    361 #endif
    362 		/*
    363 		 * If the vnode that we are about to sync is no longer
    364 		 * associated with this mount point, start over.
    365 		 */
    366 		if (vp->v_mount != mp) {
    367 			printf("lfs_writevnodes: starting over\n");
    368 			goto loop;
    369 		}
    370 
    371 		ip = VTOI(vp);
    372 		if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
    373 		    (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
    374 			vndebug(vp,"dirop");
    375 			continue;
    376 		}
    377 
    378 		if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) {
    379 			vndebug(vp,"empty");
    380 			continue;
    381 		}
    382 
    383 		if (vp->v_type == VNON) {
    384 			continue;
    385 		}
    386 
    387 		if(op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
    388 		   && vp != fs->lfs_flushvp
    389 		   && !(ip->i_flag & IN_CLEANING)) {
    390 			vndebug(vp,"cleaning");
    391 			continue;
    392 		}
    393 
    394 		if (lfs_vref(vp)) {
    395 			vndebug(vp,"vref");
    396 			continue;
    397 		}
    398 
    399 		needs_unlock = 0;
    400 		if (VOP_ISLOCKED(vp)) {
    401 			if (vp != fs->lfs_ivnode &&
    402 			    vp->v_lock.lk_lockholder != curproc->p_pid) {
    403 #ifdef DEBUG_LFS
    404 				printf("lfs_writevnodes: not writing ino %d,"
    405 				       " locked by pid %d\n",
    406 				       VTOI(vp)->i_number,
    407 				       vp->v_lock.lk_lockholder);
    408 #endif
    409 				lfs_vunref(vp);
    410 				continue;
    411 			}
    412 		} else if (vp != fs->lfs_ivnode) {
    413 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    414 			needs_unlock = 1;
    415 		}
    416 
    417 		only_cleaning = 0;
    418 		/*
    419 		 * Write the inode/file if dirty and it's not the IFILE.
    420 		 */
    421 		if ((ip->i_flag & IN_ALLMOD) ||
    422 		     (vp->v_dirtyblkhd.lh_first != NULL))
    423 		{
    424 			only_cleaning = ((ip->i_flag & IN_ALLMOD)==IN_CLEANING);
    425 
    426 			if(ip->i_number != LFS_IFILE_INUM
    427 			   && vp->v_dirtyblkhd.lh_first != NULL)
    428 			{
    429 				lfs_writefile(fs, sp, vp);
    430 			}
    431 			if(vp->v_dirtyblkhd.lh_first != NULL) {
    432 				if(WRITEINPROG(vp)) {
    433 #ifdef DEBUG_LFS
    434 					ivndebug(vp,"writevnodes/write2");
    435 #endif
    436 				} else if(!(ip->i_flag & IN_ALLMOD)) {
    437 #ifdef DEBUG_LFS
    438 					printf("<%d>",ip->i_number);
    439 #endif
    440 					LFS_SET_UINO(ip, IN_MODIFIED);
    441 				}
    442 			}
    443 			(void) lfs_writeinode(fs, sp, ip);
    444 			inodes_written++;
    445 		}
    446 
    447 		if (needs_unlock)
    448 			VOP_UNLOCK(vp, 0);
    449 
    450 		if (lfs_clean_vnhead && only_cleaning)
    451 			lfs_vunref_head(vp);
    452 		else
    453 			lfs_vunref(vp);
    454 	}
    455 	return inodes_written;
    456 }
    457 
    458 int
    459 lfs_segwrite(mp, flags)
    460 	struct mount *mp;
    461 	int flags;			/* Do a checkpoint. */
    462 {
    463 	struct buf *bp;
    464 	struct inode *ip;
    465 	struct lfs *fs;
    466 	struct segment *sp;
    467 	struct vnode *vp;
    468 	SEGUSE *segusep;
    469 	CLEANERINFO *cip;
    470 	ufs_daddr_t ibno;
    471 	int do_ckp, error, i;
    472 	int writer_set = 0;
    473 
    474 	fs = VFSTOUFS(mp)->um_lfs;
    475 
    476 	if (fs->lfs_ronly)
    477 		return EROFS;
    478 
    479 	lfs_imtime(fs);
    480 
    481 #if 0
    482 	/*
    483 	 * If we are not the cleaner, and there is no space available,
    484 	 * wait until cleaner writes.
    485 	 */
    486 	if(!(flags & SEGM_CLEAN)
    487 	   && (!fs->lfs_seglock || !(fs->lfs_sp->seg_flags & SEGM_CLEAN)))
    488 	{
    489 		while (fs->lfs_avail <= 0) {
    490 			wakeup(&lfs_allclean_wakeup);
    491 			wakeup(&fs->lfs_nextseg);
    492 			error = tsleep(&fs->lfs_avail, PRIBIO + 1, "lfs_av2",
    493 				       0);
    494 			if (error) {
    495 				return (error);
    496 			}
    497 		}
    498 	}
    499 #endif
    500 	/*
    501 	 * Synchronize cleaner information
    502 	 */
    503 	LFS_CLEANERINFO(cip, fs, bp);
    504 	cip->bfree = fs->lfs_bfree;
    505 	cip->avail = fs->lfs_avail - fs->lfs_ravail;
    506 	(void) VOP_BWRITE(bp); /* Ifile */
    507 
    508 	/*
    509 	 * Allocate a segment structure and enough space to hold pointers to
    510 	 * the maximum possible number of buffers which can be described in a
    511 	 * single summary block.
    512 	 */
    513 	do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
    514 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
    515 	sp = fs->lfs_sp;
    516 
    517 	/*
    518 	 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
    519 	 * in which case we have to flush *all* buffers off of this vnode.
    520 	 * We don't care about other nodes, but write any non-dirop nodes
    521 	 * anyway in anticipation of another getnewvnode().
    522 	 *
    523 	 * If we're cleaning we only write cleaning and ifile blocks, and
    524 	 * no dirops, since otherwise we'd risk corruption in a crash.
    525 	 */
    526 	if(sp->seg_flags & SEGM_CLEAN)
    527 		lfs_writevnodes(fs, mp, sp, VN_CLEAN);
    528 	else {
    529 		lfs_writevnodes(fs, mp, sp, VN_REG);
    530 		if(!fs->lfs_dirops || !fs->lfs_flushvp) {
    531 			while(fs->lfs_dirops)
    532 				if((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
    533 						"lfs writer", 0)))
    534 				{
    535 					free(sp->bpp, M_SEGMENT);
    536 					free(sp, M_SEGMENT);
    537 					return (error);
    538 				}
    539 			fs->lfs_writer++;
    540 			writer_set=1;
    541 			lfs_writevnodes(fs, mp, sp, VN_DIROP);
    542 			((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
    543 		}
    544 	}
    545 
    546 	/*
    547 	 * If we are doing a checkpoint, mark everything since the
    548 	 * last checkpoint as no longer ACTIVE.
    549 	 */
    550 	if (do_ckp) {
    551 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
    552 		     --ibno >= fs->lfs_cleansz; ) {
    553 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
    554 
    555 				panic("lfs_segwrite: ifile read");
    556 			segusep = (SEGUSE *)bp->b_data;
    557 			for (i = fs->lfs_sepb; i--; segusep++)
    558 				segusep->su_flags &= ~SEGUSE_ACTIVE;
    559 
    560 			/* But the current segment is still ACTIVE */
    561 			segusep = (SEGUSE *)bp->b_data;
    562 			if (datosn(fs, fs->lfs_curseg) / fs->lfs_sepb ==
    563 			    (ibno-fs->lfs_cleansz))
    564 				segusep[datosn(fs, fs->lfs_curseg) %
    565 					fs->lfs_sepb].su_flags |= SEGUSE_ACTIVE;
    566 			error = VOP_BWRITE(bp); /* Ifile */
    567 		}
    568 	}
    569 
    570 	if (do_ckp || fs->lfs_doifile) {
    571 	redo:
    572 		vp = fs->lfs_ivnode;
    573 
    574 		vget(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
    575 
    576 		ip = VTOI(vp);
    577 		if (vp->v_dirtyblkhd.lh_first != NULL)
    578 			lfs_writefile(fs, sp, vp);
    579 		(void)lfs_writeinode(fs, sp, ip);
    580 
    581 		vput(vp);
    582 
    583 		if (lfs_writeseg(fs, sp) && do_ckp)
    584 			goto redo;
    585 	} else {
    586 		(void) lfs_writeseg(fs, sp);
    587 	}
    588 
    589 	/*
    590 	 * If the I/O count is non-zero, sleep until it reaches zero.
    591 	 * At the moment, the user's process hangs around so we can
    592 	 * sleep.
    593 	 */
    594 	fs->lfs_doifile = 0;
    595 	if(writer_set && --fs->lfs_writer==0)
    596 		wakeup(&fs->lfs_dirops);
    597 
    598 	if(lfs_dostats) {
    599 		++lfs_stats.nwrites;
    600 		if (sp->seg_flags & SEGM_SYNC)
    601 			++lfs_stats.nsync_writes;
    602 		if (sp->seg_flags & SEGM_CKP)
    603 			++lfs_stats.ncheckpoints;
    604 	}
    605 	lfs_segunlock(fs);
    606 	return (0);
    607 }
    608 
    609 /*
    610  * Write the dirty blocks associated with a vnode.
    611  */
    612 void
    613 lfs_writefile(fs, sp, vp)
    614 	struct lfs *fs;
    615 	struct segment *sp;
    616 	struct vnode *vp;
    617 {
    618 	struct buf *bp;
    619 	struct finfo *fip;
    620 	IFILE *ifp;
    621 
    622 
    623 	if (sp->seg_bytes_left < fs->lfs_bsize ||
    624 	    sp->sum_bytes_left < sizeof(struct finfo))
    625 		(void) lfs_writeseg(fs, sp);
    626 
    627 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
    628 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    629 
    630 	if(vp->v_flag & VDIROP)
    631 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
    632 
    633 	fip = sp->fip;
    634 	fip->fi_nblocks = 0;
    635 	fip->fi_ino = VTOI(vp)->i_number;
    636 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
    637 	fip->fi_version = ifp->if_version;
    638 	brelse(bp);
    639 
    640 	if(sp->seg_flags & SEGM_CLEAN)
    641 	{
    642 		lfs_gather(fs, sp, vp, lfs_match_fake);
    643 		/*
    644 		 * For a file being flushed, we need to write *all* blocks.
    645 		 * This means writing the cleaning blocks first, and then
    646 		 * immediately following with any non-cleaning blocks.
    647 		 * The same is true of the Ifile since checkpoints assume
    648 		 * that all valid Ifile blocks are written.
    649 		 */
    650 	   	if(IS_FLUSHING(fs,vp) || VTOI(vp)->i_number == LFS_IFILE_INUM)
    651 			lfs_gather(fs, sp, vp, lfs_match_data);
    652 	} else
    653 		lfs_gather(fs, sp, vp, lfs_match_data);
    654 
    655 	/*
    656 	 * It may not be necessary to write the meta-data blocks at this point,
    657 	 * as the roll-forward recovery code should be able to reconstruct the
    658 	 * list.
    659 	 *
    660 	 * We have to write them anyway, though, under two conditions: (1) the
    661 	 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
    662 	 * checkpointing.
    663 	 */
    664 	if(lfs_writeindir
    665 	   || IS_FLUSHING(fs,vp)
    666 	   || (sp->seg_flags & SEGM_CKP))
    667 	{
    668 		lfs_gather(fs, sp, vp, lfs_match_indir);
    669 		lfs_gather(fs, sp, vp, lfs_match_dindir);
    670 		lfs_gather(fs, sp, vp, lfs_match_tindir);
    671 	}
    672 	fip = sp->fip;
    673 	if (fip->fi_nblocks != 0) {
    674 		sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
    675 				   sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
    676 		sp->start_lbp = &sp->fip->fi_blocks[0];
    677 	} else {
    678 		sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
    679 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
    680 	}
    681 }
    682 
    683 int
    684 lfs_writeinode(fs, sp, ip)
    685 	struct lfs *fs;
    686 	struct segment *sp;
    687 	struct inode *ip;
    688 {
    689 	struct buf *bp, *ibp;
    690 	struct dinode *cdp;
    691 	IFILE *ifp;
    692 	SEGUSE *sup;
    693 	ufs_daddr_t daddr;
    694 	daddr_t *daddrp;
    695 	ino_t ino;
    696 	int error, i, ndx;
    697 	int redo_ifile = 0;
    698 	struct timespec ts;
    699 	int gotblk=0;
    700 
    701 	if (!(ip->i_flag & IN_ALLMOD))
    702 		return(0);
    703 
    704 	/* Allocate a new inode block if necessary. */
    705 	if ((ip->i_number != LFS_IFILE_INUM || sp->idp==NULL) && sp->ibp == NULL) {
    706 		/* Allocate a new segment if necessary. */
    707 		if (sp->seg_bytes_left < fs->lfs_bsize ||
    708 		    sp->sum_bytes_left < sizeof(ufs_daddr_t))
    709 			(void) lfs_writeseg(fs, sp);
    710 
    711 		/* Get next inode block. */
    712 		daddr = fs->lfs_offset;
    713 		fs->lfs_offset += fsbtodb(fs, 1);
    714 		sp->ibp = *sp->cbpp++ =
    715 			getblk(VTOI(fs->lfs_ivnode)->i_devvp, daddr, fs->lfs_bsize, 0, 0);
    716 		gotblk++;
    717 
    718 		/* Zero out inode numbers */
    719 		for (i = 0; i < INOPB(fs); ++i)
    720 			((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
    721 
    722 		++sp->start_bpp;
    723 		fs->lfs_avail -= fsbtodb(fs, 1);
    724 		/* Set remaining space counters. */
    725 		sp->seg_bytes_left -= fs->lfs_bsize;
    726 		sp->sum_bytes_left -= sizeof(ufs_daddr_t);
    727 		ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
    728 			sp->ninodes / INOPB(fs) - 1;
    729 		((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
    730 	}
    731 
    732 	/* Update the inode times and copy the inode onto the inode page. */
    733 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    734 	LFS_ITIMES(ip, &ts, &ts, &ts);
    735 
    736 	/*
    737 	 * If this is the Ifile, and we've already written the Ifile in this
    738 	 * partial segment, just overwrite it (it's not on disk yet) and
    739 	 * continue.
    740 	 *
    741 	 * XXX we know that the bp that we get the second time around has
    742 	 * already been gathered.
    743 	 */
    744 	if(ip->i_number == LFS_IFILE_INUM && sp->idp) {
    745 		*(sp->idp) = ip->i_din.ffs_din;
    746 		return 0;
    747 	}
    748 
    749 	bp = sp->ibp;
    750 	cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
    751 	*cdp = ip->i_din.ffs_din;
    752 
    753 	/*
    754 	 * If we are cleaning, ensure that we don't write UNWRITTEN disk
    755 	 * addresses to disk.
    756 	 */
    757 	if (ip->i_lfs_effnblks != ip->i_ffs_blocks) {
    758 #ifdef DEBUG_LFS
    759 		printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
    760 		       ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks);
    761 #endif
    762 		for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
    763 		     daddrp++) {
    764 			if (*daddrp == UNWRITTEN) {
    765 #ifdef DEBUG_LFS
    766 				printf("lfs_writeinode: wiping UNWRITTEN\n");
    767 #endif
    768 				*daddrp = 0;
    769 			}
    770 		}
    771 	}
    772 
    773 	if(ip->i_flag & IN_CLEANING)
    774 		LFS_CLR_UINO(ip, IN_CLEANING);
    775 	else {
    776 		/* XXX IN_ALLMOD */
    777 		LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
    778 			     IN_UPDATE);
    779 		if (ip->i_lfs_effnblks == ip->i_ffs_blocks)
    780 			LFS_CLR_UINO(ip, IN_MODIFIED);
    781 	}
    782 
    783 	if(ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
    784 		sp->idp = ((struct dinode *)bp->b_data) +
    785 			(sp->ninodes % INOPB(fs));
    786 	if(gotblk) {
    787 		bp->b_flags |= B_LOCKED;
    788 		brelse(bp);
    789 	}
    790 
    791 	/* Increment inode count in segment summary block. */
    792 	++((SEGSUM *)(sp->segsum))->ss_ninos;
    793 
    794 	/* If this page is full, set flag to allocate a new page. */
    795 	if (++sp->ninodes % INOPB(fs) == 0)
    796 		sp->ibp = NULL;
    797 
    798 	/*
    799 	 * If updating the ifile, update the super-block.  Update the disk
    800 	 * address and access times for this inode in the ifile.
    801 	 */
    802 	ino = ip->i_number;
    803 	if (ino == LFS_IFILE_INUM) {
    804 		daddr = fs->lfs_idaddr;
    805 		fs->lfs_idaddr = bp->b_blkno;
    806 	} else {
    807 		LFS_IENTRY(ifp, fs, ino, ibp);
    808 		daddr = ifp->if_daddr;
    809 		ifp->if_daddr = bp->b_blkno;
    810 #ifdef LFS_DEBUG_NEXTFREE
    811 		if(ino > 3 && ifp->if_nextfree) {
    812 			vprint("lfs_writeinode",ITOV(ip));
    813 			printf("lfs_writeinode: updating free ino %d\n",
    814 				ip->i_number);
    815 		}
    816 #endif
    817 		error = VOP_BWRITE(ibp); /* Ifile */
    818 	}
    819 
    820 	/*
    821 	 * Account the inode: it no longer belongs to its former segment,
    822 	 * though it will not belong to the new segment until that segment
    823 	 * is actually written.
    824 	 */
    825 #ifdef DEBUG
    826 	/*
    827 	 * The inode's last address should not be in the current partial
    828 	 * segment, except under exceptional circumstances (lfs_writevnodes
    829 	 * had to start over, and in the meantime more blocks were written
    830 	 * to a vnode).  Although the previous inode won't be accounted in
    831 	 * su_nbytes until lfs_writeseg, this shouldn't be a problem as we
    832 	 * have more data blocks in the current partial segment.
    833 	 */
    834 	if (daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)
    835 		printf("lfs_writeinode: last inode addr in current pseg "
    836 		       "(ino %d daddr 0x%x)\n", ino, daddr);
    837 #endif
    838 	if (daddr != LFS_UNUSED_DADDR) {
    839 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
    840 #ifdef DIAGNOSTIC
    841 		if (sup->su_nbytes < DINODE_SIZE) {
    842 			printf("lfs_writeinode: negative bytes "
    843 			       "(segment %d short by %d)\n",
    844 			       datosn(fs, daddr),
    845 			       (int)DINODE_SIZE - sup->su_nbytes);
    846 			panic("lfs_writeinode: negative bytes");
    847 			sup->su_nbytes = DINODE_SIZE;
    848 		}
    849 #endif
    850 		sup->su_nbytes -= DINODE_SIZE;
    851 		redo_ifile =
    852 			(ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
    853 		error = VOP_BWRITE(bp); /* Ifile */
    854 	}
    855 	return (redo_ifile);
    856 }
    857 
    858 int
    859 lfs_gatherblock(sp, bp, sptr)
    860 	struct segment *sp;
    861 	struct buf *bp;
    862 	int *sptr;
    863 {
    864 	struct lfs *fs;
    865 	int version;
    866 
    867 	/*
    868 	 * If full, finish this segment.  We may be doing I/O, so
    869 	 * release and reacquire the splbio().
    870 	 */
    871 #ifdef DIAGNOSTIC
    872 	if (sp->vp == NULL)
    873 		panic ("lfs_gatherblock: Null vp in segment");
    874 #endif
    875 	fs = sp->fs;
    876 	if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
    877 	    sp->seg_bytes_left < bp->b_bcount) {
    878 		if (sptr)
    879 			splx(*sptr);
    880 		lfs_updatemeta(sp);
    881 
    882 		version = sp->fip->fi_version;
    883 		(void) lfs_writeseg(fs, sp);
    884 
    885 		sp->fip->fi_version = version;
    886 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
    887 		/* Add the current file to the segment summary. */
    888 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    889 		sp->sum_bytes_left -=
    890 			sizeof(struct finfo) - sizeof(ufs_daddr_t);
    891 
    892 		if (sptr)
    893 			*sptr = splbio();
    894 		return(1);
    895 	}
    896 
    897 #ifdef DEBUG
    898 	if(bp->b_flags & B_GATHERED) {
    899 		printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
    900 		       sp->fip->fi_ino, bp->b_lblkno);
    901 		return(0);
    902 	}
    903 #endif
    904 	/* Insert into the buffer list, update the FINFO block. */
    905 	bp->b_flags |= B_GATHERED;
    906 	*sp->cbpp++ = bp;
    907 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
    908 
    909 	sp->sum_bytes_left -= sizeof(ufs_daddr_t);
    910 	sp->seg_bytes_left -= bp->b_bcount;
    911 	return(0);
    912 }
    913 
    914 int
    915 lfs_gather(fs, sp, vp, match)
    916 	struct lfs *fs;
    917 	struct segment *sp;
    918 	struct vnode *vp;
    919 	int (*match) __P((struct lfs *, struct buf *));
    920 {
    921 	struct buf *bp;
    922 	int s, count=0;
    923 
    924 	sp->vp = vp;
    925 	s = splbio();
    926 
    927 #ifndef LFS_NO_BACKBUF_HACK
    928 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
    929 #else /* LFS_NO_BACKBUF_HACK */
    930 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
    931 # define	BUF_OFFSET	(((void *)&bp->b_vnbufs.le_next) - (void *)bp)
    932 # define	BACK_BUF(BP)	((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
    933 # define	BEG_OF_LIST	((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
    934 /* Find last buffer. */
    935 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
    936 	    bp = bp->b_vnbufs.le_next);
    937 	for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
    938 #endif /* LFS_NO_BACKBUF_HACK */
    939 		if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
    940 			continue;
    941 		if(vp->v_type == VBLK) {
    942 			/* For block devices, just write the blocks. */
    943 			/* XXX Do we really need to even do this? */
    944 #ifdef DEBUG_LFS
    945 			if(count==0)
    946 				printf("BLK(");
    947 			printf(".");
    948 #endif
    949 			/* Get the block before bwrite, so we don't corrupt the free list */
    950 			bp->b_flags |= B_BUSY;
    951 			bremfree(bp);
    952 			bwrite(bp);
    953 		} else {
    954 #ifdef DIAGNOSTIC
    955 			if ((bp->b_flags & (B_CALL|B_INVAL))==B_INVAL) {
    956 				printf("lfs_gather: lbn %d is B_INVAL\n",
    957 					bp->b_lblkno);
    958 				VOP_PRINT(bp->b_vp);
    959 			}
    960 			if (!(bp->b_flags & B_DELWRI))
    961 				panic("lfs_gather: bp not B_DELWRI");
    962 			if (!(bp->b_flags & B_LOCKED)) {
    963 				printf("lfs_gather: lbn %d blk %d"
    964 				       " not B_LOCKED\n", bp->b_lblkno,
    965 				       bp->b_blkno);
    966 				VOP_PRINT(bp->b_vp);
    967 				panic("lfs_gather: bp not B_LOCKED");
    968 			}
    969 #endif
    970 			if (lfs_gatherblock(sp, bp, &s)) {
    971 				goto loop;
    972 			}
    973 		}
    974 		count++;
    975 	}
    976 	splx(s);
    977 #ifdef DEBUG_LFS
    978 	if(vp->v_type == VBLK && count)
    979 		printf(")\n");
    980 #endif
    981 	lfs_updatemeta(sp);
    982 	sp->vp = NULL;
    983 	return count;
    984 }
    985 
    986 /*
    987  * Update the metadata that points to the blocks listed in the FINFO
    988  * array.
    989  */
    990 void
    991 lfs_updatemeta(sp)
    992 	struct segment *sp;
    993 {
    994 	SEGUSE *sup;
    995 	struct buf *bp;
    996 	struct lfs *fs;
    997 	struct vnode *vp;
    998 	struct indir a[NIADDR + 2], *ap;
    999 	struct inode *ip;
   1000 	ufs_daddr_t daddr, lbn, off;
   1001 	daddr_t ooff;
   1002 	int error, i, nblocks, num;
   1003 	int bb;
   1004 
   1005 	vp = sp->vp;
   1006 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
   1007 	if (nblocks < 0)
   1008 		panic("This is a bad thing\n");
   1009 	if (vp == NULL || nblocks == 0)
   1010 		return;
   1011 
   1012 	/* Sort the blocks. */
   1013 	/*
   1014 	 * XXX KS - We have to sort even if the blocks come from the
   1015 	 * cleaner, because there might be other pending blocks on the
   1016 	 * same inode...and if we don't sort, and there are fragments
   1017 	 * present, blocks may be written in the wrong place.
   1018 	 */
   1019 	/* if (!(sp->seg_flags & SEGM_CLEAN)) */
   1020 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
   1021 
   1022 	/*
   1023 	 * Record the length of the last block in case it's a fragment.
   1024 	 * If there are indirect blocks present, they sort last.  An
   1025 	 * indirect block will be lfs_bsize and its presence indicates
   1026 	 * that you cannot have fragments.
   1027 	 */
   1028 	sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
   1029 
   1030 	/*
   1031 	 * Assign disk addresses, and update references to the logical
   1032 	 * block and the segment usage information.
   1033 	 */
   1034 	fs = sp->fs;
   1035 	for (i = nblocks; i--; ++sp->start_bpp) {
   1036 		lbn = *sp->start_lbp++;
   1037 
   1038 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
   1039 		if((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) {
   1040 			printf("lfs_updatemeta: ino %d blk %d"
   1041 			       " has same lbn and daddr\n",
   1042 			       VTOI(vp)->i_number, off);
   1043 		}
   1044 		bb = fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
   1045 		fs->lfs_offset += bb;
   1046 		error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
   1047 		if (error)
   1048 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
   1049 		ip = VTOI(vp);
   1050 		switch (num) {
   1051 		case 0:
   1052 			ooff = ip->i_ffs_db[lbn];
   1053 #ifdef DEBUG
   1054 			if (ooff == 0) {
   1055 				printf("lfs_updatemeta[1]: warning: writing "
   1056 				       "ino %d lbn %d at 0x%x, was 0x0\n",
   1057 				       ip->i_number, lbn, off);
   1058 			}
   1059 #endif
   1060 			if (ooff == UNWRITTEN)
   1061 				ip->i_ffs_blocks += bb;
   1062 			ip->i_ffs_db[lbn] = off;
   1063 			break;
   1064 		case 1:
   1065 			ooff = ip->i_ffs_ib[a[0].in_off];
   1066 #ifdef DEBUG
   1067 			if (ooff == 0) {
   1068 				printf("lfs_updatemeta[2]: warning: writing "
   1069 				       "ino %d lbn %d at 0x%x, was 0x0\n",
   1070 				       ip->i_number, lbn, off);
   1071 			}
   1072 #endif
   1073 			if (ooff == UNWRITTEN)
   1074 				ip->i_ffs_blocks += bb;
   1075 			ip->i_ffs_ib[a[0].in_off] = off;
   1076 			break;
   1077 		default:
   1078 			ap = &a[num - 1];
   1079 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
   1080 				panic("lfs_updatemeta: bread bno %d",
   1081 				      ap->in_lbn);
   1082 
   1083 			ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off];
   1084 #if DEBUG
   1085 			if (ooff == 0) {
   1086 				printf("lfs_updatemeta[3]: warning: writing "
   1087 				       "ino %d lbn %d at 0x%x, was 0x0\n",
   1088 				       ip->i_number, lbn, off);
   1089 			}
   1090 #endif
   1091 			if (ooff == UNWRITTEN)
   1092 				ip->i_ffs_blocks += bb;
   1093 			((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
   1094 			(void) VOP_BWRITE(bp);
   1095 		}
   1096 #ifdef DEBUG
   1097 		if (daddr >= fs->lfs_lastpseg && daddr <= off) {
   1098 			printf("lfs_updatemeta: ino %d, lbn %d, addr = %x "
   1099 			       "in same pseg\n", VTOI(sp->vp)->i_number,
   1100 			       (*sp->start_bpp)->b_lblkno, daddr);
   1101 		}
   1102 #endif
   1103 		/* Update segment usage information. */
   1104 		if (daddr > 0) {
   1105 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
   1106 #ifdef DIAGNOSTIC
   1107 			if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
   1108 				/* XXX -- Change to a panic. */
   1109 				printf("lfs_updatemeta: negative bytes "
   1110 				       "(segment %d short by %ld)\n",
   1111 				       datosn(fs, daddr),
   1112 				       (*sp->start_bpp)->b_bcount -
   1113 				       sup->su_nbytes);
   1114 				printf("lfs_updatemeta: ino %d, lbn %d, "
   1115 				       "addr = %x\n", VTOI(sp->vp)->i_number,
   1116 				       (*sp->start_bpp)->b_lblkno, daddr);
   1117 				panic("lfs_updatemeta: negative bytes");
   1118 				sup->su_nbytes = (*sp->start_bpp)->b_bcount;
   1119 			}
   1120 #endif
   1121 			sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
   1122 			error = VOP_BWRITE(bp); /* Ifile */
   1123 		}
   1124 	}
   1125 }
   1126 
   1127 /*
   1128  * Start a new segment.
   1129  */
   1130 int
   1131 lfs_initseg(fs)
   1132 	struct lfs *fs;
   1133 {
   1134 	struct segment *sp;
   1135 	SEGUSE *sup;
   1136 	SEGSUM *ssp;
   1137 	struct buf *bp;
   1138 	int repeat;
   1139 
   1140 	sp = fs->lfs_sp;
   1141 
   1142 	repeat = 0;
   1143 	/* Advance to the next segment. */
   1144 	if (!LFS_PARTIAL_FITS(fs)) {
   1145 		/* lfs_avail eats the remaining space */
   1146 		fs->lfs_avail -= fs->lfs_dbpseg - (fs->lfs_offset -
   1147 						   fs->lfs_curseg);
   1148 		/* Wake up any cleaning procs waiting on this file system. */
   1149 		wakeup(&lfs_allclean_wakeup);
   1150 		wakeup(&fs->lfs_nextseg);
   1151 		lfs_newseg(fs);
   1152 		repeat = 1;
   1153 		fs->lfs_offset = fs->lfs_curseg;
   1154 		sp->seg_number = datosn(fs, fs->lfs_curseg);
   1155 		sp->seg_bytes_left = dbtob(fs->lfs_dbpseg);
   1156 		/*
   1157 		 * If the segment contains a superblock, update the offset
   1158 		 * and summary address to skip over it.
   1159 		 */
   1160 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1161 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
   1162 			fs->lfs_offset += btodb(LFS_SBPAD);
   1163 			sp->seg_bytes_left -= LFS_SBPAD;
   1164 		}
   1165 		brelse(bp);
   1166 	} else {
   1167 		sp->seg_number = datosn(fs, fs->lfs_curseg);
   1168 		sp->seg_bytes_left = dbtob(fs->lfs_dbpseg -
   1169 				      (fs->lfs_offset - fs->lfs_curseg));
   1170 	}
   1171 	fs->lfs_lastpseg = fs->lfs_offset;
   1172 
   1173 	sp->fs = fs;
   1174 	sp->ibp = NULL;
   1175 	sp->idp = NULL;
   1176 	sp->ninodes = 0;
   1177 
   1178 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
   1179 	sp->cbpp = sp->bpp;
   1180 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
   1181 			       fs->lfs_offset, LFS_SUMMARY_SIZE);
   1182 	sp->segsum = (*sp->cbpp)->b_data;
   1183 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
   1184 	sp->start_bpp = ++sp->cbpp;
   1185 	fs->lfs_offset += btodb(LFS_SUMMARY_SIZE);
   1186 
   1187 	/* Set point to SEGSUM, initialize it. */
   1188 	ssp = sp->segsum;
   1189 	ssp->ss_next = fs->lfs_nextseg;
   1190 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
   1191 	ssp->ss_magic = SS_MAGIC;
   1192 
   1193 	/* Set pointer to first FINFO, initialize it. */
   1194 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
   1195 	sp->fip->fi_nblocks = 0;
   1196 	sp->start_lbp = &sp->fip->fi_blocks[0];
   1197 	sp->fip->fi_lastlength = 0;
   1198 
   1199 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
   1200 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
   1201 
   1202 	return(repeat);
   1203 }
   1204 
   1205 /*
   1206  * Return the next segment to write.
   1207  */
   1208 void
   1209 lfs_newseg(fs)
   1210 	struct lfs *fs;
   1211 {
   1212 	CLEANERINFO *cip;
   1213 	SEGUSE *sup;
   1214 	struct buf *bp;
   1215 	int curseg, isdirty, sn;
   1216 
   1217 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
   1218 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1219 	sup->su_nbytes = 0;
   1220 	sup->su_nsums = 0;
   1221 	sup->su_ninos = 0;
   1222 	(void) VOP_BWRITE(bp); /* Ifile */
   1223 
   1224 	LFS_CLEANERINFO(cip, fs, bp);
   1225 	--cip->clean;
   1226 	++cip->dirty;
   1227 	fs->lfs_nclean = cip->clean;
   1228 	(void) VOP_BWRITE(bp); /* Ifile */
   1229 
   1230 	fs->lfs_lastseg = fs->lfs_curseg;
   1231 	fs->lfs_curseg = fs->lfs_nextseg;
   1232 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
   1233 		sn = (sn + 1) % fs->lfs_nseg;
   1234 		if (sn == curseg)
   1235 			panic("lfs_nextseg: no clean segments");
   1236 		LFS_SEGENTRY(sup, fs, sn, bp);
   1237 		isdirty = sup->su_flags & SEGUSE_DIRTY;
   1238 		brelse(bp);
   1239 		if (!isdirty)
   1240 			break;
   1241 	}
   1242 
   1243 	++fs->lfs_nactive;
   1244 	fs->lfs_nextseg = sntoda(fs, sn);
   1245 	if(lfs_dostats) {
   1246 		++lfs_stats.segsused;
   1247 	}
   1248 }
   1249 
   1250 int
   1251 lfs_writeseg(fs, sp)
   1252 	struct lfs *fs;
   1253 	struct segment *sp;
   1254 {
   1255 	extern int locked_queue_count;
   1256 	extern long locked_queue_bytes;
   1257 	struct buf **bpp, *bp, *cbp, *newbp;
   1258 	SEGUSE *sup;
   1259 	SEGSUM *ssp;
   1260 	dev_t i_dev;
   1261 	u_long *datap, *dp;
   1262 	int do_again, i, nblocks, s;
   1263 #ifdef LFS_TRACK_IOS
   1264 	int j;
   1265 #endif
   1266 	int (*strategy)__P((void *));
   1267 	struct vop_strategy_args vop_strategy_a;
   1268 	u_short ninos;
   1269 	struct vnode *devvp;
   1270 	char *p;
   1271 	struct vnode *vn;
   1272 	struct inode *ip;
   1273 	daddr_t *daddrp;
   1274 	int changed;
   1275 #if defined(DEBUG) && defined(LFS_PROPELLER)
   1276 	static int propeller;
   1277 	char propstring[4] = "-\\|/";
   1278 
   1279 	printf("%c\b",propstring[propeller++]);
   1280 	if(propeller==4)
   1281 		propeller = 0;
   1282 #endif
   1283 
   1284 	/*
   1285 	 * If there are no buffers other than the segment summary to write
   1286 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
   1287 	 * even if there aren't any buffers, you need to write the superblock.
   1288 	 */
   1289 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
   1290 		return (0);
   1291 
   1292 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1293 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
   1294 
   1295 	/* Update the segment usage information. */
   1296 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1297 
   1298 	/* Loop through all blocks, except the segment summary. */
   1299 	for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
   1300 		if((*bpp)->b_vp != devvp)
   1301 			sup->su_nbytes += (*bpp)->b_bcount;
   1302 	}
   1303 
   1304 	ssp = (SEGSUM *)sp->segsum;
   1305 
   1306 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
   1307 	sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
   1308 	/* sup->su_nbytes += LFS_SUMMARY_SIZE; */
   1309 	sup->su_lastmod = time.tv_sec;
   1310 	sup->su_ninos += ninos;
   1311 	++sup->su_nsums;
   1312 	fs->lfs_dmeta += (btodb(LFS_SUMMARY_SIZE) + fsbtodb(fs, ninos));
   1313 	fs->lfs_avail -= btodb(LFS_SUMMARY_SIZE);
   1314 
   1315 	do_again = !(bp->b_flags & B_GATHERED);
   1316 	(void)VOP_BWRITE(bp); /* Ifile */
   1317 	/*
   1318 	 * Mark blocks B_BUSY, to prevent then from being changed between
   1319 	 * the checksum computation and the actual write.
   1320 	 *
   1321 	 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
   1322 	 * there are any, replace them with copies that have UNASSIGNED
   1323 	 * instead.
   1324 	 */
   1325 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
   1326 		++bpp;
   1327 		if((*bpp)->b_flags & B_CALL)
   1328 			continue;
   1329 		bp = *bpp;
   1330 	    again:
   1331 		s = splbio();
   1332 		if(bp->b_flags & B_BUSY) {
   1333 #ifdef DEBUG
   1334 			printf("lfs_writeseg: avoiding potential data "
   1335 			       "summary corruption for ino %d, lbn %d\n",
   1336 			       VTOI(bp->b_vp)->i_number, bp->b_lblkno);
   1337 #endif
   1338 			bp->b_flags |= B_WANTED;
   1339 			tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
   1340 			splx(s);
   1341 			goto again;
   1342 		}
   1343 		bp->b_flags |= B_BUSY;
   1344 		splx(s);
   1345 		/* Check and replace indirect block UNWRITTEN bogosity */
   1346 		if(bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
   1347 		   VTOI(bp->b_vp)->i_ffs_blocks !=
   1348 		   VTOI(bp->b_vp)->i_lfs_effnblks) {
   1349 #ifdef DEBUG_LFS
   1350 			printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
   1351 			       VTOI(bp->b_vp)->i_number,
   1352 			       VTOI(bp->b_vp)->i_lfs_effnblks,
   1353 			       VTOI(bp->b_vp)->i_ffs_blocks);
   1354 #endif
   1355 			/* Make a copy we'll make changes to */
   1356 			newbp = lfs_newbuf(bp->b_vp, bp->b_lblkno,
   1357 					   bp->b_bcount);
   1358 			newbp->b_blkno = bp->b_blkno;
   1359 			memcpy(newbp->b_data, bp->b_data,
   1360 			       newbp->b_bcount);
   1361 			*bpp = newbp;
   1362 
   1363 			changed = 0;
   1364 			for (daddrp = (daddr_t *)(newbp->b_data);
   1365 			     daddrp < (daddr_t *)(newbp->b_data +
   1366 						  newbp->b_bcount); daddrp++) {
   1367 				if (*daddrp == UNWRITTEN) {
   1368 					++changed;
   1369 #ifdef DEBUG_LFS
   1370 					printf("lfs_writeseg: replacing UNWRITTEN\n");
   1371 #endif
   1372 					*daddrp = 0;
   1373 				}
   1374 			}
   1375 			/*
   1376 			 * Get rid of the old buffer.  Don't mark it clean,
   1377 			 * though, if it still has dirty data on it.
   1378 			 */
   1379 			if (changed) {
   1380 				bp->b_flags &= ~(B_ERROR | B_GATHERED);
   1381 				if (bp->b_flags & B_CALL)
   1382 					lfs_freebuf(bp);
   1383 				else {
   1384 					/* Still on free list, leave it there */
   1385 					s = splbio();
   1386 					bp->b_flags &= ~B_BUSY;
   1387 					if (bp->b_flags & B_WANTED)
   1388 						wakeup(bp);
   1389 				 	splx(s);
   1390 				}
   1391 			} else {
   1392 				bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
   1393 						 B_LOCKED | B_GATHERED);
   1394 				if (bp->b_flags & B_CALL)
   1395 					lfs_freebuf(bp);
   1396 				else {
   1397 					bremfree(bp);
   1398 					bp->b_flags |= B_DONE;
   1399 					reassignbuf(bp, bp->b_vp);
   1400 					brelse(bp);
   1401 				}
   1402 			}
   1403 
   1404 		}
   1405 	}
   1406 	/*
   1407 	 * Compute checksum across data and then across summary; the first
   1408 	 * block (the summary block) is skipped.  Set the create time here
   1409 	 * so that it's guaranteed to be later than the inode mod times.
   1410 	 *
   1411 	 * XXX
   1412 	 * Fix this to do it inline, instead of malloc/copy.
   1413 	 */
   1414 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
   1415 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
   1416 		if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1417 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
   1418 				panic("lfs_writeseg: copyin failed [1]: "
   1419 				      "ino %d blk %d",
   1420 				      VTOI((*bpp)->b_vp)->i_number,
   1421 				      (*bpp)->b_lblkno);
   1422 		} else
   1423 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
   1424 	}
   1425 	ssp->ss_create = time.tv_sec;
   1426 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
   1427 	ssp->ss_sumsum =
   1428 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
   1429 	free(datap, M_SEGMENT);
   1430 
   1431 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + btodb(LFS_SUMMARY_SIZE));
   1432 
   1433 	strategy = devvp->v_op[VOFFSET(vop_strategy)];
   1434 
   1435 	/*
   1436 	 * When we simply write the blocks we lose a rotation for every block
   1437 	 * written.  To avoid this problem, we allocate memory in chunks, copy
   1438 	 * the buffers into the chunk and write the chunk.  CHUNKSIZE is the
   1439 	 * largest size I/O devices can handle.
   1440 	 * When the data is copied to the chunk, turn off the B_LOCKED bit
   1441 	 * and brelse the buffer (which will move them to the LRU list).  Add
   1442 	 * the B_CALL flag to the buffer header so we can count I/O's for the
   1443 	 * checkpoints and so we can release the allocated memory.
   1444 	 *
   1445 	 * XXX
   1446 	 * This should be removed if the new virtual memory system allows us to
   1447 	 * easily make the buffers contiguous in kernel memory and if that's
   1448 	 * fast enough.
   1449 	 */
   1450 
   1451 #define CHUNKSIZE MAXPHYS
   1452 
   1453 	if(devvp==NULL)
   1454 		panic("devvp is NULL");
   1455 	for (bpp = sp->bpp,i = nblocks; i;) {
   1456 		cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE);
   1457 		cbp->b_dev = i_dev;
   1458 		cbp->b_flags |= B_ASYNC | B_BUSY;
   1459 		cbp->b_bcount = 0;
   1460 
   1461 #ifdef DIAGNOSTIC
   1462 		if(datosn(fs, (*bpp)->b_blkno + btodb((*bpp)->b_bcount) - 1) !=
   1463 		   datosn(fs, cbp->b_blkno)) {
   1464 			panic("lfs_writeseg: Segment overwrite");
   1465 		}
   1466 #endif
   1467 
   1468 		s = splbio();
   1469 		if(fs->lfs_iocount >= LFS_THROTTLE) {
   1470 			tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
   1471 		}
   1472 		++fs->lfs_iocount;
   1473 #ifdef LFS_TRACK_IOS
   1474 		for(j=0;j<LFS_THROTTLE;j++) {
   1475 			if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) {
   1476 				fs->lfs_pending[j] = cbp->b_blkno;
   1477 				break;
   1478 			}
   1479 		}
   1480 #endif /* LFS_TRACK_IOS */
   1481 		for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
   1482 			bp = *bpp;
   1483 
   1484 			if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
   1485 				break;
   1486 
   1487 			/*
   1488 			 * Fake buffers from the cleaner are marked as B_INVAL.
   1489 			 * We need to copy the data from user space rather than
   1490 			 * from the buffer indicated.
   1491 			 * XXX == what do I do on an error?
   1492 			 */
   1493 			if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1494 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
   1495 					panic("lfs_writeseg: copyin failed [2]");
   1496 			} else
   1497 				bcopy(bp->b_data, p, bp->b_bcount);
   1498 			p += bp->b_bcount;
   1499 			cbp->b_bcount += bp->b_bcount;
   1500 			if (bp->b_flags & B_LOCKED) {
   1501 				--locked_queue_count;
   1502 				locked_queue_bytes -= bp->b_bufsize;
   1503 			}
   1504 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
   1505 					 B_LOCKED | B_GATHERED);
   1506 			vn = bp->b_vp;
   1507 			if (bp->b_flags & B_CALL) {
   1508 				/* if B_CALL, it was created with newbuf */
   1509 				lfs_freebuf(bp);
   1510 			} else {
   1511 				bremfree(bp);
   1512 				bp->b_flags |= B_DONE;
   1513 				if(vn)
   1514 					reassignbuf(bp, vn);
   1515 				brelse(bp);
   1516 			}
   1517 			if(bp->b_flags & B_NEEDCOMMIT) { /* XXX */
   1518 				bp->b_flags &= ~B_NEEDCOMMIT;
   1519 				wakeup(bp);
   1520 			}
   1521 
   1522 			bpp++;
   1523 
   1524 			/*
   1525 			 * If this is the last block for this vnode, but
   1526 			 * there are other blocks on its dirty list,
   1527 			 * set IN_MODIFIED/IN_CLEANING depending on what
   1528 			 * sort of block.  Only do this for our mount point,
   1529 			 * not for, e.g., inode blocks that are attached to
   1530 			 * the devvp.
   1531 			 */
   1532 			if(i>1 && vn && *bpp && (*bpp)->b_vp != vn
   1533 			   && (*bpp)->b_vp && (bp=vn->v_dirtyblkhd.lh_first)!=NULL &&
   1534 			   vn->v_mount == fs->lfs_ivnode->v_mount)
   1535 			{
   1536 				ip = VTOI(vn);
   1537 #ifdef DEBUG_LFS
   1538 				printf("lfs_writeseg: marking ino %d\n",ip->i_number);
   1539 #endif
   1540 				if(bp->b_flags & B_CALL)
   1541 					LFS_SET_UINO(ip, IN_CLEANING);
   1542 				else
   1543 					LFS_SET_UINO(ip, IN_MODIFIED);
   1544 			}
   1545 			/* if(vn->v_dirtyblkhd.lh_first == NULL) */
   1546 				wakeup(vn);
   1547 		}
   1548 		++cbp->b_vp->v_numoutput;
   1549 		splx(s);
   1550 		/*
   1551 		 * XXXX This is a gross and disgusting hack.  Since these
   1552 		 * buffers are physically addressed, they hang off the
   1553 		 * device vnode (devvp).  As a result, they have no way
   1554 		 * of getting to the LFS superblock or lfs structure to
   1555 		 * keep track of the number of I/O's pending.  So, I am
   1556 		 * going to stuff the fs into the saveaddr field of
   1557 		 * the buffer (yuk).
   1558 		 */
   1559 		cbp->b_saveaddr = (caddr_t)fs;
   1560 		vop_strategy_a.a_desc = VDESC(vop_strategy);
   1561 		vop_strategy_a.a_bp = cbp;
   1562 		(strategy)(&vop_strategy_a);
   1563 	}
   1564 	/*
   1565 	 * XXX
   1566 	 * Vinvalbuf can move locked buffers off the locked queue
   1567 	 * and we have no way of knowing about this.  So, after
   1568 	 * doing a big write, we recalculate how many buffers are
   1569 	 * really still left on the locked queue.
   1570 	 */
   1571 	s = splbio();
   1572 	lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
   1573 	splx(s);
   1574 	wakeup(&locked_queue_count);
   1575 	if(lfs_dostats) {
   1576 		++lfs_stats.psegwrites;
   1577 		lfs_stats.blocktot += nblocks - 1;
   1578 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
   1579 			++lfs_stats.psyncwrites;
   1580 		if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
   1581 			++lfs_stats.pcleanwrites;
   1582 			lfs_stats.cleanblocks += nblocks - 1;
   1583 		}
   1584 	}
   1585 	return (lfs_initseg(fs) || do_again);
   1586 }
   1587 
   1588 void
   1589 lfs_writesuper(fs, daddr)
   1590 	struct lfs *fs;
   1591 	daddr_t daddr;
   1592 {
   1593 	struct buf *bp;
   1594 	dev_t i_dev;
   1595 	int (*strategy) __P((void *));
   1596 	int s;
   1597 	struct vop_strategy_args vop_strategy_a;
   1598 
   1599 #ifdef LFS_CANNOT_ROLLFW
   1600 	/*
   1601 	 * If we can write one superblock while another is in
   1602 	 * progress, we risk not having a complete checkpoint if we crash.
   1603 	 * So, block here if a superblock write is in progress.
   1604 	 */
   1605 	s = splbio();
   1606 	while(fs->lfs_sbactive) {
   1607 		tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
   1608 	}
   1609 	fs->lfs_sbactive = daddr;
   1610 	splx(s);
   1611 #endif
   1612 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1613 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
   1614 
   1615 	/* Set timestamp of this version of the superblock */
   1616 	fs->lfs_tstamp = time.tv_sec;
   1617 
   1618 	/* Checksum the superblock and copy it into a buffer. */
   1619 	fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
   1620 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD);
   1621 	*(struct dlfs *)bp->b_data = fs->lfs_dlfs;
   1622 
   1623 	bp->b_dev = i_dev;
   1624 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
   1625 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
   1626 	bp->b_iodone = lfs_supercallback;
   1627 	/* XXX KS - same nasty hack as above */
   1628 	bp->b_saveaddr = (caddr_t)fs;
   1629 
   1630 	vop_strategy_a.a_desc = VDESC(vop_strategy);
   1631 	vop_strategy_a.a_bp = bp;
   1632 	s = splbio();
   1633 	++bp->b_vp->v_numoutput;
   1634 	++fs->lfs_iocount;
   1635 	splx(s);
   1636 	(strategy)(&vop_strategy_a);
   1637 }
   1638 
   1639 /*
   1640  * Logical block number match routines used when traversing the dirty block
   1641  * chain.
   1642  */
   1643 int
   1644 lfs_match_fake(fs, bp)
   1645 	struct lfs *fs;
   1646 	struct buf *bp;
   1647 {
   1648 	return (bp->b_flags & B_CALL);
   1649 }
   1650 
   1651 int
   1652 lfs_match_data(fs, bp)
   1653 	struct lfs *fs;
   1654 	struct buf *bp;
   1655 {
   1656 	return (bp->b_lblkno >= 0);
   1657 }
   1658 
   1659 int
   1660 lfs_match_indir(fs, bp)
   1661 	struct lfs *fs;
   1662 	struct buf *bp;
   1663 {
   1664 	int lbn;
   1665 
   1666 	lbn = bp->b_lblkno;
   1667 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
   1668 }
   1669 
   1670 int
   1671 lfs_match_dindir(fs, bp)
   1672 	struct lfs *fs;
   1673 	struct buf *bp;
   1674 {
   1675 	int lbn;
   1676 
   1677 	lbn = bp->b_lblkno;
   1678 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
   1679 }
   1680 
   1681 int
   1682 lfs_match_tindir(fs, bp)
   1683 	struct lfs *fs;
   1684 	struct buf *bp;
   1685 {
   1686 	int lbn;
   1687 
   1688 	lbn = bp->b_lblkno;
   1689 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
   1690 }
   1691 
   1692 /*
   1693  * XXX - The only buffers that are going to hit these functions are the
   1694  * segment write blocks, or the segment summaries, or the superblocks.
   1695  *
   1696  * All of the above are created by lfs_newbuf, and so do not need to be
   1697  * released via brelse.
   1698  */
   1699 void
   1700 lfs_callback(bp)
   1701 	struct buf *bp;
   1702 {
   1703 	struct lfs *fs;
   1704 #ifdef LFS_TRACK_IOS
   1705 	int j;
   1706 #endif
   1707 
   1708 	fs = (struct lfs *)bp->b_saveaddr;
   1709 #ifdef DIAGNOSTIC
   1710 	if (fs->lfs_iocount == 0)
   1711 		panic("lfs_callback: zero iocount\n");
   1712 #endif
   1713 	if (--fs->lfs_iocount < LFS_THROTTLE)
   1714 		wakeup(&fs->lfs_iocount);
   1715 #ifdef LFS_TRACK_IOS
   1716 	for(j=0;j<LFS_THROTTLE;j++) {
   1717 		if(fs->lfs_pending[j]==bp->b_blkno) {
   1718 			fs->lfs_pending[j] = LFS_UNUSED_DADDR;
   1719 			wakeup(&(fs->lfs_pending[j]));
   1720 			break;
   1721 		}
   1722 	}
   1723 #endif /* LFS_TRACK_IOS */
   1724 
   1725 	lfs_freebuf(bp);
   1726 }
   1727 
   1728 void
   1729 lfs_supercallback(bp)
   1730 	struct buf *bp;
   1731 {
   1732 	struct lfs *fs;
   1733 
   1734 	fs = (struct lfs *)bp->b_saveaddr;
   1735 #ifdef LFS_CANNOT_ROLLFW
   1736 	fs->lfs_sbactive = 0;
   1737 	wakeup(&fs->lfs_sbactive);
   1738 #endif
   1739 	if (--fs->lfs_iocount < LFS_THROTTLE)
   1740 		wakeup(&fs->lfs_iocount);
   1741 	lfs_freebuf(bp);
   1742 }
   1743 
   1744 /*
   1745  * Shellsort (diminishing increment sort) from Data Structures and
   1746  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
   1747  * see also Knuth Vol. 3, page 84.  The increments are selected from
   1748  * formula (8), page 95.  Roughly O(N^3/2).
   1749  */
   1750 /*
   1751  * This is our own private copy of shellsort because we want to sort
   1752  * two parallel arrays (the array of buffer pointers and the array of
   1753  * logical block numbers) simultaneously.  Note that we cast the array
   1754  * of logical block numbers to a unsigned in this routine so that the
   1755  * negative block numbers (meta data blocks) sort AFTER the data blocks.
   1756  */
   1757 
   1758 void
   1759 lfs_shellsort(bp_array, lb_array, nmemb)
   1760 	struct buf **bp_array;
   1761 	ufs_daddr_t *lb_array;
   1762 	int nmemb;
   1763 {
   1764 	static int __rsshell_increments[] = { 4, 1, 0 };
   1765 	int incr, *incrp, t1, t2;
   1766 	struct buf *bp_temp;
   1767 	u_long lb_temp;
   1768 
   1769 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
   1770 		for (t1 = incr; t1 < nmemb; ++t1)
   1771 			for (t2 = t1 - incr; t2 >= 0;)
   1772 				if (lb_array[t2] > lb_array[t2 + incr]) {
   1773 					lb_temp = lb_array[t2];
   1774 					lb_array[t2] = lb_array[t2 + incr];
   1775 					lb_array[t2 + incr] = lb_temp;
   1776 					bp_temp = bp_array[t2];
   1777 					bp_array[t2] = bp_array[t2 + incr];
   1778 					bp_array[t2 + incr] = bp_temp;
   1779 					t2 -= incr;
   1780 				} else
   1781 					break;
   1782 }
   1783 
   1784 /*
   1785  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
   1786  */
   1787 int
   1788 lfs_vref(vp)
   1789 	struct vnode *vp;
   1790 {
   1791 	/*
   1792 	 * If we return 1 here during a flush, we risk vinvalbuf() not
   1793 	 * being able to flush all of the pages from this vnode, which
   1794 	 * will cause it to panic.  So, return 0 if a flush is in progress.
   1795 	 */
   1796 	if (vp->v_flag & VXLOCK) {
   1797 		if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1798 			return 0;
   1799 		}
   1800 		return(1);
   1801 	}
   1802 	return (vget(vp, 0));
   1803 }
   1804 
   1805 /*
   1806  * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
   1807  * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
   1808  */
   1809 void
   1810 lfs_vunref(vp)
   1811 	struct vnode *vp;
   1812 {
   1813 	/*
   1814 	 * Analogous to lfs_vref, if the node is flushing, fake it.
   1815 	 */
   1816 	if((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1817 		return;
   1818 	}
   1819 
   1820 	simple_lock(&vp->v_interlock);
   1821 #ifdef DIAGNOSTIC
   1822 	if(vp->v_usecount<=0) {
   1823 		printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
   1824 		printf("lfs_vunref: flags are 0x%lx\n", vp->v_flag);
   1825 		printf("lfs_vunref: usecount = %ld\n", vp->v_usecount);
   1826 		panic("lfs_vunref: v_usecount<0");
   1827 	}
   1828 #endif
   1829 	vp->v_usecount--;
   1830 	if (vp->v_usecount > 0) {
   1831 		simple_unlock(&vp->v_interlock);
   1832 		return;
   1833 	}
   1834 	/*
   1835 	 * insert at tail of LRU list
   1836 	 */
   1837 	simple_lock(&vnode_free_list_slock);
   1838 	if (vp->v_holdcnt > 0)
   1839 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
   1840 	else
   1841 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
   1842 	simple_unlock(&vnode_free_list_slock);
   1843 	simple_unlock(&vp->v_interlock);
   1844 }
   1845 
   1846 /*
   1847  * We use this when we have vnodes that were loaded in solely for cleaning.
   1848  * There is no reason to believe that these vnodes will be referenced again
   1849  * soon, since the cleaning process is unrelated to normal filesystem
   1850  * activity.  Putting cleaned vnodes at the tail of the list has the effect
   1851  * of flushing the vnode LRU.  So, put vnodes that were loaded only for
   1852  * cleaning at the head of the list, instead.
   1853  */
   1854 void
   1855 lfs_vunref_head(vp)
   1856 	struct vnode *vp;
   1857 {
   1858 	simple_lock(&vp->v_interlock);
   1859 #ifdef DIAGNOSTIC
   1860 	if(vp->v_usecount==0) {
   1861 		panic("lfs_vunref: v_usecount<0");
   1862 	}
   1863 #endif
   1864 	vp->v_usecount--;
   1865 	if (vp->v_usecount > 0) {
   1866 		simple_unlock(&vp->v_interlock);
   1867 		return;
   1868 	}
   1869 	/*
   1870 	 * insert at head of LRU list
   1871 	 */
   1872 	simple_lock(&vnode_free_list_slock);
   1873 	TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
   1874 	simple_unlock(&vnode_free_list_slock);
   1875 	simple_unlock(&vp->v_interlock);
   1876 }
   1877 
   1878