Home | History | Annotate | Line # | Download | only in lfs
lfs_segment.c revision 1.58
      1 /*	$NetBSD: lfs_segment.c,v 1.58 2000/09/09 04:49:55 perseant 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 	 * No need to update segment usage if there was no former inode
    822 	 * address or if the last inode address is in the current
    823 	 * partial segment.
    824 	 */
    825 #ifdef DEBUG
    826 	if (daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)
    827 		printf("lfs_writeinode: last inode addr in current pseg "
    828 		       "(ino %d daddr 0x%x)\n", ino, daddr);
    829 #endif
    830 	if (daddr != LFS_UNUSED_DADDR) {
    831 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
    832 #ifdef DIAGNOSTIC
    833 		if (sup->su_nbytes < DINODE_SIZE) {
    834 			printf("lfs_writeinode: negative bytes "
    835 			       "(segment %d short by %d)\n",
    836 			       datosn(fs, daddr),
    837 			       (int)DINODE_SIZE - sup->su_nbytes);
    838 			panic("lfs_writeinode: negative bytes");
    839 			sup->su_nbytes = DINODE_SIZE;
    840 		}
    841 #endif
    842 		sup->su_nbytes -= DINODE_SIZE;
    843 		redo_ifile =
    844 			(ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
    845 		error = VOP_BWRITE(bp); /* Ifile */
    846 	}
    847 	return (redo_ifile);
    848 }
    849 
    850 int
    851 lfs_gatherblock(sp, bp, sptr)
    852 	struct segment *sp;
    853 	struct buf *bp;
    854 	int *sptr;
    855 {
    856 	struct lfs *fs;
    857 	int version;
    858 
    859 	/*
    860 	 * If full, finish this segment.  We may be doing I/O, so
    861 	 * release and reacquire the splbio().
    862 	 */
    863 #ifdef DIAGNOSTIC
    864 	if (sp->vp == NULL)
    865 		panic ("lfs_gatherblock: Null vp in segment");
    866 #endif
    867 	fs = sp->fs;
    868 	if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
    869 	    sp->seg_bytes_left < bp->b_bcount) {
    870 		if (sptr)
    871 			splx(*sptr);
    872 		lfs_updatemeta(sp);
    873 
    874 		version = sp->fip->fi_version;
    875 		(void) lfs_writeseg(fs, sp);
    876 
    877 		sp->fip->fi_version = version;
    878 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
    879 		/* Add the current file to the segment summary. */
    880 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    881 		sp->sum_bytes_left -=
    882 			sizeof(struct finfo) - sizeof(ufs_daddr_t);
    883 
    884 		if (sptr)
    885 			*sptr = splbio();
    886 		return(1);
    887 	}
    888 
    889 #ifdef DEBUG
    890 	if(bp->b_flags & B_GATHERED) {
    891 		printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
    892 		       sp->fip->fi_ino, bp->b_lblkno);
    893 		return(0);
    894 	}
    895 #endif
    896 	/* Insert into the buffer list, update the FINFO block. */
    897 	bp->b_flags |= B_GATHERED;
    898 	*sp->cbpp++ = bp;
    899 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
    900 
    901 	sp->sum_bytes_left -= sizeof(ufs_daddr_t);
    902 	sp->seg_bytes_left -= bp->b_bcount;
    903 	return(0);
    904 }
    905 
    906 int
    907 lfs_gather(fs, sp, vp, match)
    908 	struct lfs *fs;
    909 	struct segment *sp;
    910 	struct vnode *vp;
    911 	int (*match) __P((struct lfs *, struct buf *));
    912 {
    913 	struct buf *bp;
    914 	int s, count=0;
    915 
    916 	sp->vp = vp;
    917 	s = splbio();
    918 
    919 #ifndef LFS_NO_BACKBUF_HACK
    920 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
    921 #else /* LFS_NO_BACKBUF_HACK */
    922 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
    923 # define	BUF_OFFSET	(((void *)&bp->b_vnbufs.le_next) - (void *)bp)
    924 # define	BACK_BUF(BP)	((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
    925 # define	BEG_OF_LIST	((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
    926 /* Find last buffer. */
    927 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
    928 	    bp = bp->b_vnbufs.le_next);
    929 	for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
    930 #endif /* LFS_NO_BACKBUF_HACK */
    931 		if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
    932 			continue;
    933 		if(vp->v_type == VBLK) {
    934 			/* For block devices, just write the blocks. */
    935 			/* XXX Do we really need to even do this? */
    936 #ifdef DEBUG_LFS
    937 			if(count==0)
    938 				printf("BLK(");
    939 			printf(".");
    940 #endif
    941 			/* Get the block before bwrite, so we don't corrupt the free list */
    942 			bp->b_flags |= B_BUSY;
    943 			bremfree(bp);
    944 			bwrite(bp);
    945 		} else {
    946 #ifdef DIAGNOSTIC
    947 			if ((bp->b_flags & (B_CALL|B_INVAL))==B_INVAL) {
    948 				printf("lfs_gather: lbn %d is B_INVAL\n",
    949 					bp->b_lblkno);
    950 				VOP_PRINT(bp->b_vp);
    951 			}
    952 			if (!(bp->b_flags & B_DELWRI))
    953 				panic("lfs_gather: bp not B_DELWRI");
    954 			if (!(bp->b_flags & B_LOCKED)) {
    955 				printf("lfs_gather: lbn %d blk %d"
    956 				       " not B_LOCKED\n", bp->b_lblkno,
    957 				       bp->b_blkno);
    958 				VOP_PRINT(bp->b_vp);
    959 				panic("lfs_gather: bp not B_LOCKED");
    960 			}
    961 #endif
    962 			if (lfs_gatherblock(sp, bp, &s)) {
    963 				goto loop;
    964 			}
    965 		}
    966 		count++;
    967 	}
    968 	splx(s);
    969 #ifdef DEBUG_LFS
    970 	if(vp->v_type == VBLK && count)
    971 		printf(")\n");
    972 #endif
    973 	lfs_updatemeta(sp);
    974 	sp->vp = NULL;
    975 	return count;
    976 }
    977 
    978 /*
    979  * Update the metadata that points to the blocks listed in the FINFO
    980  * array.
    981  */
    982 void
    983 lfs_updatemeta(sp)
    984 	struct segment *sp;
    985 {
    986 	SEGUSE *sup;
    987 	struct buf *bp;
    988 	struct lfs *fs;
    989 	struct vnode *vp;
    990 	struct indir a[NIADDR + 2], *ap;
    991 	struct inode *ip;
    992 	ufs_daddr_t daddr, lbn, off;
    993 	daddr_t ooff;
    994 	int error, i, nblocks, num;
    995 	int bb;
    996 
    997 	vp = sp->vp;
    998 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
    999 	if (nblocks < 0)
   1000 		panic("This is a bad thing\n");
   1001 	if (vp == NULL || nblocks == 0)
   1002 		return;
   1003 
   1004 	/* Sort the blocks. */
   1005 	/*
   1006 	 * XXX KS - We have to sort even if the blocks come from the
   1007 	 * cleaner, because there might be other pending blocks on the
   1008 	 * same inode...and if we don't sort, and there are fragments
   1009 	 * present, blocks may be written in the wrong place.
   1010 	 */
   1011 	/* if (!(sp->seg_flags & SEGM_CLEAN)) */
   1012 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
   1013 
   1014 	/*
   1015 	 * Record the length of the last block in case it's a fragment.
   1016 	 * If there are indirect blocks present, they sort last.  An
   1017 	 * indirect block will be lfs_bsize and its presence indicates
   1018 	 * that you cannot have fragments.
   1019 	 */
   1020 	sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
   1021 
   1022 	/*
   1023 	 * Assign disk addresses, and update references to the logical
   1024 	 * block and the segment usage information.
   1025 	 */
   1026 	fs = sp->fs;
   1027 	for (i = nblocks; i--; ++sp->start_bpp) {
   1028 		lbn = *sp->start_lbp++;
   1029 
   1030 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
   1031 		if((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) {
   1032 			printf("lfs_updatemeta: ino %d blk %d"
   1033 			       " has same lbn and daddr\n",
   1034 			       VTOI(vp)->i_number, off);
   1035 		}
   1036 		bb = fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
   1037 		fs->lfs_offset += bb;
   1038 		error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
   1039 		if (error)
   1040 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
   1041 		ip = VTOI(vp);
   1042 		switch (num) {
   1043 		case 0:
   1044 			ooff = ip->i_ffs_db[lbn];
   1045 #ifdef DEBUG
   1046 			if (ooff == 0) {
   1047 				printf("lfs_updatemeta[1]: warning: writing "
   1048 				       "ino %d lbn %d at 0x%x, was 0x0\n",
   1049 				       ip->i_number, lbn, off);
   1050 			}
   1051 #endif
   1052 			if (ooff == UNWRITTEN)
   1053 				ip->i_ffs_blocks += bb;
   1054 			ip->i_ffs_db[lbn] = off;
   1055 			break;
   1056 		case 1:
   1057 			ooff = ip->i_ffs_ib[a[0].in_off];
   1058 #ifdef DEBUG
   1059 			if (ooff == 0) {
   1060 				printf("lfs_updatemeta[2]: warning: writing "
   1061 				       "ino %d lbn %d at 0x%x, was 0x0\n",
   1062 				       ip->i_number, lbn, off);
   1063 			}
   1064 #endif
   1065 			if (ooff == UNWRITTEN)
   1066 				ip->i_ffs_blocks += bb;
   1067 			ip->i_ffs_ib[a[0].in_off] = off;
   1068 			break;
   1069 		default:
   1070 			ap = &a[num - 1];
   1071 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
   1072 				panic("lfs_updatemeta: bread bno %d",
   1073 				      ap->in_lbn);
   1074 
   1075 			ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off];
   1076 #if DEBUG
   1077 			if (ooff == 0) {
   1078 				printf("lfs_updatemeta[3]: warning: writing "
   1079 				       "ino %d lbn %d at 0x%x, was 0x0\n",
   1080 				       ip->i_number, lbn, off);
   1081 			}
   1082 #endif
   1083 			if (ooff == UNWRITTEN)
   1084 				ip->i_ffs_blocks += bb;
   1085 			((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
   1086 			(void) VOP_BWRITE(bp);
   1087 		}
   1088 #ifdef DEBUG
   1089 		if (daddr >= fs->lfs_lastpseg && daddr <= off) {
   1090 			printf("lfs_updatemeta: ino %d, lbn %d, addr = %x "
   1091 			       "in same pseg\n", VTOI(sp->vp)->i_number,
   1092 			       (*sp->start_bpp)->b_lblkno, daddr);
   1093 		}
   1094 #endif
   1095 		/* Update segment usage information. */
   1096 		if (daddr > 0) {
   1097 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
   1098 #ifdef DIAGNOSTIC
   1099 			if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
   1100 				/* XXX -- Change to a panic. */
   1101 				printf("lfs_updatemeta: negative bytes "
   1102 				       "(segment %d short by %ld)\n",
   1103 				       datosn(fs, daddr),
   1104 				       (*sp->start_bpp)->b_bcount -
   1105 				       sup->su_nbytes);
   1106 				printf("lfs_updatemeta: ino %d, lbn %d, "
   1107 				       "addr = %x\n", VTOI(sp->vp)->i_number,
   1108 				       (*sp->start_bpp)->b_lblkno, daddr);
   1109 				panic("lfs_updatemeta: negative bytes");
   1110 				sup->su_nbytes = (*sp->start_bpp)->b_bcount;
   1111 			}
   1112 #endif
   1113 			sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
   1114 			error = VOP_BWRITE(bp); /* Ifile */
   1115 		}
   1116 	}
   1117 }
   1118 
   1119 /*
   1120  * Start a new segment.
   1121  */
   1122 int
   1123 lfs_initseg(fs)
   1124 	struct lfs *fs;
   1125 {
   1126 	struct segment *sp;
   1127 	SEGUSE *sup;
   1128 	SEGSUM *ssp;
   1129 	struct buf *bp;
   1130 	int repeat;
   1131 
   1132 	sp = fs->lfs_sp;
   1133 
   1134 	repeat = 0;
   1135 	/* Advance to the next segment. */
   1136 	if (!LFS_PARTIAL_FITS(fs)) {
   1137 		/* lfs_avail eats the remaining space */
   1138 		fs->lfs_avail -= fs->lfs_dbpseg - (fs->lfs_offset -
   1139 						   fs->lfs_curseg);
   1140 		/* Wake up any cleaning procs waiting on this file system. */
   1141 		wakeup(&lfs_allclean_wakeup);
   1142 		wakeup(&fs->lfs_nextseg);
   1143 		lfs_newseg(fs);
   1144 		repeat = 1;
   1145 		fs->lfs_offset = fs->lfs_curseg;
   1146 		sp->seg_number = datosn(fs, fs->lfs_curseg);
   1147 		sp->seg_bytes_left = dbtob(fs->lfs_dbpseg);
   1148 		/*
   1149 		 * If the segment contains a superblock, update the offset
   1150 		 * and summary address to skip over it.
   1151 		 */
   1152 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1153 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
   1154 			fs->lfs_offset += btodb(LFS_SBPAD);
   1155 			sp->seg_bytes_left -= LFS_SBPAD;
   1156 		}
   1157 		brelse(bp);
   1158 	} else {
   1159 		sp->seg_number = datosn(fs, fs->lfs_curseg);
   1160 		sp->seg_bytes_left = dbtob(fs->lfs_dbpseg -
   1161 				      (fs->lfs_offset - fs->lfs_curseg));
   1162 	}
   1163 	fs->lfs_lastpseg = fs->lfs_offset;
   1164 
   1165 	sp->fs = fs;
   1166 	sp->ibp = NULL;
   1167 	sp->idp = NULL;
   1168 	sp->ninodes = 0;
   1169 
   1170 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
   1171 	sp->cbpp = sp->bpp;
   1172 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
   1173 			       fs->lfs_offset, LFS_SUMMARY_SIZE);
   1174 	sp->segsum = (*sp->cbpp)->b_data;
   1175 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
   1176 	sp->start_bpp = ++sp->cbpp;
   1177 	fs->lfs_offset += btodb(LFS_SUMMARY_SIZE);
   1178 
   1179 	/* Set point to SEGSUM, initialize it. */
   1180 	ssp = sp->segsum;
   1181 	ssp->ss_next = fs->lfs_nextseg;
   1182 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
   1183 	ssp->ss_magic = SS_MAGIC;
   1184 
   1185 	/* Set pointer to first FINFO, initialize it. */
   1186 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
   1187 	sp->fip->fi_nblocks = 0;
   1188 	sp->start_lbp = &sp->fip->fi_blocks[0];
   1189 	sp->fip->fi_lastlength = 0;
   1190 
   1191 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
   1192 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
   1193 
   1194 	return(repeat);
   1195 }
   1196 
   1197 /*
   1198  * Return the next segment to write.
   1199  */
   1200 void
   1201 lfs_newseg(fs)
   1202 	struct lfs *fs;
   1203 {
   1204 	CLEANERINFO *cip;
   1205 	SEGUSE *sup;
   1206 	struct buf *bp;
   1207 	int curseg, isdirty, sn;
   1208 
   1209 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
   1210 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1211 	sup->su_nbytes = 0;
   1212 	sup->su_nsums = 0;
   1213 	sup->su_ninos = 0;
   1214 	(void) VOP_BWRITE(bp); /* Ifile */
   1215 
   1216 	LFS_CLEANERINFO(cip, fs, bp);
   1217 	--cip->clean;
   1218 	++cip->dirty;
   1219 	fs->lfs_nclean = cip->clean;
   1220 	(void) VOP_BWRITE(bp); /* Ifile */
   1221 
   1222 	fs->lfs_lastseg = fs->lfs_curseg;
   1223 	fs->lfs_curseg = fs->lfs_nextseg;
   1224 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
   1225 		sn = (sn + 1) % fs->lfs_nseg;
   1226 		if (sn == curseg)
   1227 			panic("lfs_nextseg: no clean segments");
   1228 		LFS_SEGENTRY(sup, fs, sn, bp);
   1229 		isdirty = sup->su_flags & SEGUSE_DIRTY;
   1230 		brelse(bp);
   1231 		if (!isdirty)
   1232 			break;
   1233 	}
   1234 
   1235 	++fs->lfs_nactive;
   1236 	fs->lfs_nextseg = sntoda(fs, sn);
   1237 	if(lfs_dostats) {
   1238 		++lfs_stats.segsused;
   1239 	}
   1240 }
   1241 
   1242 int
   1243 lfs_writeseg(fs, sp)
   1244 	struct lfs *fs;
   1245 	struct segment *sp;
   1246 {
   1247 	extern int locked_queue_count;
   1248 	extern long locked_queue_bytes;
   1249 	struct buf **bpp, *bp, *cbp, *newbp;
   1250 	SEGUSE *sup;
   1251 	SEGSUM *ssp;
   1252 	dev_t i_dev;
   1253 	u_long *datap, *dp;
   1254 	int do_again, i, nblocks, s;
   1255 #ifdef LFS_TRACK_IOS
   1256 	int j;
   1257 #endif
   1258 	int (*strategy)__P((void *));
   1259 	struct vop_strategy_args vop_strategy_a;
   1260 	u_short ninos;
   1261 	struct vnode *devvp;
   1262 	char *p;
   1263 	struct vnode *vn;
   1264 	struct inode *ip;
   1265 	daddr_t *daddrp;
   1266 	int changed;
   1267 #if defined(DEBUG) && defined(LFS_PROPELLER)
   1268 	static int propeller;
   1269 	char propstring[4] = "-\\|/";
   1270 
   1271 	printf("%c\b",propstring[propeller++]);
   1272 	if(propeller==4)
   1273 		propeller = 0;
   1274 #endif
   1275 
   1276 	/*
   1277 	 * If there are no buffers other than the segment summary to write
   1278 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
   1279 	 * even if there aren't any buffers, you need to write the superblock.
   1280 	 */
   1281 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
   1282 		return (0);
   1283 
   1284 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1285 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
   1286 
   1287 	/* Update the segment usage information. */
   1288 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1289 
   1290 	/* Loop through all blocks, except the segment summary. */
   1291 	for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
   1292 		if((*bpp)->b_vp != devvp)
   1293 			sup->su_nbytes += (*bpp)->b_bcount;
   1294 	}
   1295 
   1296 	ssp = (SEGSUM *)sp->segsum;
   1297 
   1298 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
   1299 	sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
   1300 	/* sup->su_nbytes += LFS_SUMMARY_SIZE; */
   1301 	sup->su_lastmod = time.tv_sec;
   1302 	sup->su_ninos += ninos;
   1303 	++sup->su_nsums;
   1304 	fs->lfs_dmeta += (btodb(LFS_SUMMARY_SIZE) + fsbtodb(fs, ninos));
   1305 	fs->lfs_avail -= btodb(LFS_SUMMARY_SIZE);
   1306 
   1307 	do_again = !(bp->b_flags & B_GATHERED);
   1308 	(void)VOP_BWRITE(bp); /* Ifile */
   1309 	/*
   1310 	 * Mark blocks B_BUSY, to prevent then from being changed between
   1311 	 * the checksum computation and the actual write.
   1312 	 *
   1313 	 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
   1314 	 * there are any, replace them with copies that have UNASSIGNED
   1315 	 * instead.
   1316 	 */
   1317 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
   1318 		++bpp;
   1319 		if((*bpp)->b_flags & B_CALL)
   1320 			continue;
   1321 		bp = *bpp;
   1322 	    again:
   1323 		s = splbio();
   1324 		if(bp->b_flags & B_BUSY) {
   1325 #ifdef DEBUG
   1326 			printf("lfs_writeseg: avoiding potential data "
   1327 			       "summary corruption for ino %d, lbn %d\n",
   1328 			       VTOI(bp->b_vp)->i_number, bp->b_lblkno);
   1329 #endif
   1330 			bp->b_flags |= B_WANTED;
   1331 			tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
   1332 			splx(s);
   1333 			goto again;
   1334 		}
   1335 		bp->b_flags |= B_BUSY;
   1336 		splx(s);
   1337 		/* Check and replace indirect block UNWRITTEN bogosity */
   1338 		if(bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
   1339 		   VTOI(bp->b_vp)->i_ffs_blocks !=
   1340 		   VTOI(bp->b_vp)->i_lfs_effnblks) {
   1341 #ifdef DEBUG_LFS
   1342 			printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
   1343 			       VTOI(bp->b_vp)->i_number,
   1344 			       VTOI(bp->b_vp)->i_lfs_effnblks,
   1345 			       VTOI(bp->b_vp)->i_ffs_blocks);
   1346 #endif
   1347 			/* Make a copy we'll make changes to */
   1348 			newbp = lfs_newbuf(bp->b_vp, bp->b_lblkno,
   1349 					   bp->b_bcount);
   1350 			newbp->b_blkno = bp->b_blkno;
   1351 			memcpy(newbp->b_data, bp->b_data,
   1352 			       newbp->b_bcount);
   1353 			*bpp = newbp;
   1354 
   1355 			changed = 0;
   1356 			for (daddrp = (daddr_t *)(newbp->b_data);
   1357 			     daddrp < (daddr_t *)(newbp->b_data +
   1358 						  newbp->b_bcount); daddrp++) {
   1359 				if (*daddrp == UNWRITTEN) {
   1360 					++changed;
   1361 #ifdef DEBUG_LFS
   1362 					printf("lfs_writeseg: replacing UNWRITTEN\n");
   1363 #endif
   1364 					*daddrp = 0;
   1365 				}
   1366 			}
   1367 			/*
   1368 			 * Get rid of the old buffer.  Don't mark it clean,
   1369 			 * though, if it still has dirty data on it.
   1370 			 */
   1371 			if (changed) {
   1372 				bp->b_flags &= ~(B_ERROR | B_GATHERED);
   1373 				if (bp->b_flags & B_CALL)
   1374 					lfs_freebuf(bp);
   1375 				else {
   1376 					/* Still on free list, leave it there */
   1377 					s = splbio();
   1378 					bp->b_flags &= ~B_BUSY;
   1379 					if (bp->b_flags & B_WANTED)
   1380 						wakeup(bp);
   1381 				 	splx(s);
   1382 				}
   1383 			} else {
   1384 				bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
   1385 						 B_LOCKED | B_GATHERED);
   1386 				if (bp->b_flags & B_CALL)
   1387 					lfs_freebuf(bp);
   1388 				else {
   1389 					bremfree(bp);
   1390 					bp->b_flags |= B_DONE;
   1391 					reassignbuf(bp, bp->b_vp);
   1392 					brelse(bp);
   1393 				}
   1394 			}
   1395 
   1396 		}
   1397 	}
   1398 	/*
   1399 	 * Compute checksum across data and then across summary; the first
   1400 	 * block (the summary block) is skipped.  Set the create time here
   1401 	 * so that it's guaranteed to be later than the inode mod times.
   1402 	 *
   1403 	 * XXX
   1404 	 * Fix this to do it inline, instead of malloc/copy.
   1405 	 */
   1406 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
   1407 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
   1408 		if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1409 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
   1410 				panic("lfs_writeseg: copyin failed [1]: "
   1411 				      "ino %d blk %d",
   1412 				      VTOI((*bpp)->b_vp)->i_number,
   1413 				      (*bpp)->b_lblkno);
   1414 		} else
   1415 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
   1416 	}
   1417 	ssp->ss_create = time.tv_sec;
   1418 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
   1419 	ssp->ss_sumsum =
   1420 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
   1421 	free(datap, M_SEGMENT);
   1422 
   1423 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + btodb(LFS_SUMMARY_SIZE));
   1424 
   1425 	strategy = devvp->v_op[VOFFSET(vop_strategy)];
   1426 
   1427 	/*
   1428 	 * When we simply write the blocks we lose a rotation for every block
   1429 	 * written.  To avoid this problem, we allocate memory in chunks, copy
   1430 	 * the buffers into the chunk and write the chunk.  CHUNKSIZE is the
   1431 	 * largest size I/O devices can handle.
   1432 	 * When the data is copied to the chunk, turn off the B_LOCKED bit
   1433 	 * and brelse the buffer (which will move them to the LRU list).  Add
   1434 	 * the B_CALL flag to the buffer header so we can count I/O's for the
   1435 	 * checkpoints and so we can release the allocated memory.
   1436 	 *
   1437 	 * XXX
   1438 	 * This should be removed if the new virtual memory system allows us to
   1439 	 * easily make the buffers contiguous in kernel memory and if that's
   1440 	 * fast enough.
   1441 	 */
   1442 
   1443 #define CHUNKSIZE MAXPHYS
   1444 
   1445 	if(devvp==NULL)
   1446 		panic("devvp is NULL");
   1447 	for (bpp = sp->bpp,i = nblocks; i;) {
   1448 		cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE);
   1449 		cbp->b_dev = i_dev;
   1450 		cbp->b_flags |= B_ASYNC | B_BUSY;
   1451 		cbp->b_bcount = 0;
   1452 
   1453 #ifdef DIAGNOSTIC
   1454 		if(datosn(fs, (*bpp)->b_blkno + btodb(*bpp)->b_bcount - 1) !=
   1455 		   datosn(fs, cbp->b_blkno)) {
   1456 			panic("lfs_writeseg: Segment overwrite");
   1457 		}
   1458 #endif
   1459 
   1460 		s = splbio();
   1461 		if(fs->lfs_iocount >= LFS_THROTTLE) {
   1462 			tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
   1463 		}
   1464 		++fs->lfs_iocount;
   1465 #ifdef LFS_TRACK_IOS
   1466 		for(j=0;j<LFS_THROTTLE;j++) {
   1467 			if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) {
   1468 				fs->lfs_pending[j] = cbp->b_blkno;
   1469 				break;
   1470 			}
   1471 		}
   1472 #endif /* LFS_TRACK_IOS */
   1473 		for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
   1474 			bp = *bpp;
   1475 
   1476 			if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
   1477 				break;
   1478 
   1479 			/*
   1480 			 * Fake buffers from the cleaner are marked as B_INVAL.
   1481 			 * We need to copy the data from user space rather than
   1482 			 * from the buffer indicated.
   1483 			 * XXX == what do I do on an error?
   1484 			 */
   1485 			if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1486 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
   1487 					panic("lfs_writeseg: copyin failed [2]");
   1488 			} else
   1489 				bcopy(bp->b_data, p, bp->b_bcount);
   1490 			p += bp->b_bcount;
   1491 			cbp->b_bcount += bp->b_bcount;
   1492 			if (bp->b_flags & B_LOCKED) {
   1493 				--locked_queue_count;
   1494 				locked_queue_bytes -= bp->b_bufsize;
   1495 			}
   1496 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
   1497 					 B_LOCKED | B_GATHERED);
   1498 			vn = bp->b_vp;
   1499 			if (bp->b_flags & B_CALL) {
   1500 				/* if B_CALL, it was created with newbuf */
   1501 				lfs_freebuf(bp);
   1502 			} else {
   1503 				bremfree(bp);
   1504 				bp->b_flags |= B_DONE;
   1505 				if(vn)
   1506 					reassignbuf(bp, vn);
   1507 				brelse(bp);
   1508 			}
   1509 			if(bp->b_flags & B_NEEDCOMMIT) { /* XXX */
   1510 				bp->b_flags &= ~B_NEEDCOMMIT;
   1511 				wakeup(bp);
   1512 			}
   1513 
   1514 			bpp++;
   1515 
   1516 			/*
   1517 			 * If this is the last block for this vnode, but
   1518 			 * there are other blocks on its dirty list,
   1519 			 * set IN_MODIFIED/IN_CLEANING depending on what
   1520 			 * sort of block.  Only do this for our mount point,
   1521 			 * not for, e.g., inode blocks that are attached to
   1522 			 * the devvp.
   1523 			 */
   1524 			if(i>1 && vn && *bpp && (*bpp)->b_vp != vn
   1525 			   && (*bpp)->b_vp && (bp=vn->v_dirtyblkhd.lh_first)!=NULL &&
   1526 			   vn->v_mount == fs->lfs_ivnode->v_mount)
   1527 			{
   1528 				ip = VTOI(vn);
   1529 #ifdef DEBUG_LFS
   1530 				printf("lfs_writeseg: marking ino %d\n",ip->i_number);
   1531 #endif
   1532 				if(bp->b_flags & B_CALL)
   1533 					LFS_SET_UINO(ip, IN_CLEANING);
   1534 				else
   1535 					LFS_SET_UINO(ip, IN_MODIFIED);
   1536 			}
   1537 			/* if(vn->v_dirtyblkhd.lh_first == NULL) */
   1538 				wakeup(vn);
   1539 		}
   1540 		++cbp->b_vp->v_numoutput;
   1541 		splx(s);
   1542 		/*
   1543 		 * XXXX This is a gross and disgusting hack.  Since these
   1544 		 * buffers are physically addressed, they hang off the
   1545 		 * device vnode (devvp).  As a result, they have no way
   1546 		 * of getting to the LFS superblock or lfs structure to
   1547 		 * keep track of the number of I/O's pending.  So, I am
   1548 		 * going to stuff the fs into the saveaddr field of
   1549 		 * the buffer (yuk).
   1550 		 */
   1551 		cbp->b_saveaddr = (caddr_t)fs;
   1552 		vop_strategy_a.a_desc = VDESC(vop_strategy);
   1553 		vop_strategy_a.a_bp = cbp;
   1554 		(strategy)(&vop_strategy_a);
   1555 	}
   1556 	/*
   1557 	 * XXX
   1558 	 * Vinvalbuf can move locked buffers off the locked queue
   1559 	 * and we have no way of knowing about this.  So, after
   1560 	 * doing a big write, we recalculate how many buffers are
   1561 	 * really still left on the locked queue.
   1562 	 */
   1563 	s = splbio();
   1564 	lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
   1565 	splx(s);
   1566 	wakeup(&locked_queue_count);
   1567 	if(lfs_dostats) {
   1568 		++lfs_stats.psegwrites;
   1569 		lfs_stats.blocktot += nblocks - 1;
   1570 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
   1571 			++lfs_stats.psyncwrites;
   1572 		if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
   1573 			++lfs_stats.pcleanwrites;
   1574 			lfs_stats.cleanblocks += nblocks - 1;
   1575 		}
   1576 	}
   1577 	return (lfs_initseg(fs) || do_again);
   1578 }
   1579 
   1580 void
   1581 lfs_writesuper(fs, daddr)
   1582 	struct lfs *fs;
   1583 	daddr_t daddr;
   1584 {
   1585 	struct buf *bp;
   1586 	dev_t i_dev;
   1587 	int (*strategy) __P((void *));
   1588 	int s;
   1589 	struct vop_strategy_args vop_strategy_a;
   1590 
   1591 #ifdef LFS_CANNOT_ROLLFW
   1592 	/*
   1593 	 * If we can write one superblock while another is in
   1594 	 * progress, we risk not having a complete checkpoint if we crash.
   1595 	 * So, block here if a superblock write is in progress.
   1596 	 */
   1597 	s = splbio();
   1598 	while(fs->lfs_sbactive) {
   1599 		tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
   1600 	}
   1601 	fs->lfs_sbactive = daddr;
   1602 	splx(s);
   1603 #endif
   1604 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1605 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
   1606 
   1607 	/* Set timestamp of this version of the superblock */
   1608 	fs->lfs_tstamp = time.tv_sec;
   1609 
   1610 	/* Checksum the superblock and copy it into a buffer. */
   1611 	fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
   1612 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD);
   1613 	*(struct dlfs *)bp->b_data = fs->lfs_dlfs;
   1614 
   1615 	bp->b_dev = i_dev;
   1616 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
   1617 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
   1618 	bp->b_iodone = lfs_supercallback;
   1619 	/* XXX KS - same nasty hack as above */
   1620 	bp->b_saveaddr = (caddr_t)fs;
   1621 
   1622 	vop_strategy_a.a_desc = VDESC(vop_strategy);
   1623 	vop_strategy_a.a_bp = bp;
   1624 	s = splbio();
   1625 	++bp->b_vp->v_numoutput;
   1626 	++fs->lfs_iocount;
   1627 	splx(s);
   1628 	(strategy)(&vop_strategy_a);
   1629 }
   1630 
   1631 /*
   1632  * Logical block number match routines used when traversing the dirty block
   1633  * chain.
   1634  */
   1635 int
   1636 lfs_match_fake(fs, bp)
   1637 	struct lfs *fs;
   1638 	struct buf *bp;
   1639 {
   1640 	return (bp->b_flags & B_CALL);
   1641 }
   1642 
   1643 int
   1644 lfs_match_data(fs, bp)
   1645 	struct lfs *fs;
   1646 	struct buf *bp;
   1647 {
   1648 	return (bp->b_lblkno >= 0);
   1649 }
   1650 
   1651 int
   1652 lfs_match_indir(fs, bp)
   1653 	struct lfs *fs;
   1654 	struct buf *bp;
   1655 {
   1656 	int lbn;
   1657 
   1658 	lbn = bp->b_lblkno;
   1659 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
   1660 }
   1661 
   1662 int
   1663 lfs_match_dindir(fs, bp)
   1664 	struct lfs *fs;
   1665 	struct buf *bp;
   1666 {
   1667 	int lbn;
   1668 
   1669 	lbn = bp->b_lblkno;
   1670 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
   1671 }
   1672 
   1673 int
   1674 lfs_match_tindir(fs, bp)
   1675 	struct lfs *fs;
   1676 	struct buf *bp;
   1677 {
   1678 	int lbn;
   1679 
   1680 	lbn = bp->b_lblkno;
   1681 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
   1682 }
   1683 
   1684 /*
   1685  * XXX - The only buffers that are going to hit these functions are the
   1686  * segment write blocks, or the segment summaries, or the superblocks.
   1687  *
   1688  * All of the above are created by lfs_newbuf, and so do not need to be
   1689  * released via brelse.
   1690  */
   1691 void
   1692 lfs_callback(bp)
   1693 	struct buf *bp;
   1694 {
   1695 	struct lfs *fs;
   1696 #ifdef LFS_TRACK_IOS
   1697 	int j;
   1698 #endif
   1699 
   1700 	fs = (struct lfs *)bp->b_saveaddr;
   1701 #ifdef DIAGNOSTIC
   1702 	if (fs->lfs_iocount == 0)
   1703 		panic("lfs_callback: zero iocount\n");
   1704 #endif
   1705 	if (--fs->lfs_iocount < LFS_THROTTLE)
   1706 		wakeup(&fs->lfs_iocount);
   1707 #ifdef LFS_TRACK_IOS
   1708 	for(j=0;j<LFS_THROTTLE;j++) {
   1709 		if(fs->lfs_pending[j]==bp->b_blkno) {
   1710 			fs->lfs_pending[j] = LFS_UNUSED_DADDR;
   1711 			wakeup(&(fs->lfs_pending[j]));
   1712 			break;
   1713 		}
   1714 	}
   1715 #endif /* LFS_TRACK_IOS */
   1716 
   1717 	lfs_freebuf(bp);
   1718 }
   1719 
   1720 void
   1721 lfs_supercallback(bp)
   1722 	struct buf *bp;
   1723 {
   1724 	struct lfs *fs;
   1725 
   1726 	fs = (struct lfs *)bp->b_saveaddr;
   1727 #ifdef LFS_CANNOT_ROLLFW
   1728 	fs->lfs_sbactive = 0;
   1729 	wakeup(&fs->lfs_sbactive);
   1730 #endif
   1731 	if (--fs->lfs_iocount < LFS_THROTTLE)
   1732 		wakeup(&fs->lfs_iocount);
   1733 	lfs_freebuf(bp);
   1734 }
   1735 
   1736 /*
   1737  * Shellsort (diminishing increment sort) from Data Structures and
   1738  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
   1739  * see also Knuth Vol. 3, page 84.  The increments are selected from
   1740  * formula (8), page 95.  Roughly O(N^3/2).
   1741  */
   1742 /*
   1743  * This is our own private copy of shellsort because we want to sort
   1744  * two parallel arrays (the array of buffer pointers and the array of
   1745  * logical block numbers) simultaneously.  Note that we cast the array
   1746  * of logical block numbers to a unsigned in this routine so that the
   1747  * negative block numbers (meta data blocks) sort AFTER the data blocks.
   1748  */
   1749 
   1750 void
   1751 lfs_shellsort(bp_array, lb_array, nmemb)
   1752 	struct buf **bp_array;
   1753 	ufs_daddr_t *lb_array;
   1754 	int nmemb;
   1755 {
   1756 	static int __rsshell_increments[] = { 4, 1, 0 };
   1757 	int incr, *incrp, t1, t2;
   1758 	struct buf *bp_temp;
   1759 	u_long lb_temp;
   1760 
   1761 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
   1762 		for (t1 = incr; t1 < nmemb; ++t1)
   1763 			for (t2 = t1 - incr; t2 >= 0;)
   1764 				if (lb_array[t2] > lb_array[t2 + incr]) {
   1765 					lb_temp = lb_array[t2];
   1766 					lb_array[t2] = lb_array[t2 + incr];
   1767 					lb_array[t2 + incr] = lb_temp;
   1768 					bp_temp = bp_array[t2];
   1769 					bp_array[t2] = bp_array[t2 + incr];
   1770 					bp_array[t2 + incr] = bp_temp;
   1771 					t2 -= incr;
   1772 				} else
   1773 					break;
   1774 }
   1775 
   1776 /*
   1777  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
   1778  */
   1779 int
   1780 lfs_vref(vp)
   1781 	struct vnode *vp;
   1782 {
   1783 	/*
   1784 	 * If we return 1 here during a flush, we risk vinvalbuf() not
   1785 	 * being able to flush all of the pages from this vnode, which
   1786 	 * will cause it to panic.  So, return 0 if a flush is in progress.
   1787 	 */
   1788 	if (vp->v_flag & VXLOCK) {
   1789 		if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1790 			return 0;
   1791 		}
   1792 		return(1);
   1793 	}
   1794 	return (vget(vp, 0));
   1795 }
   1796 
   1797 /*
   1798  * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
   1799  * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
   1800  */
   1801 void
   1802 lfs_vunref(vp)
   1803 	struct vnode *vp;
   1804 {
   1805 	/*
   1806 	 * Analogous to lfs_vref, if the node is flushing, fake it.
   1807 	 */
   1808 	if((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1809 		return;
   1810 	}
   1811 
   1812 	simple_lock(&vp->v_interlock);
   1813 #ifdef DIAGNOSTIC
   1814 	if(vp->v_usecount<=0) {
   1815 		printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
   1816 		printf("lfs_vunref: flags are 0x%lx\n", vp->v_flag);
   1817 		printf("lfs_vunref: usecount = %ld\n", vp->v_usecount);
   1818 		panic("lfs_vunref: v_usecount<0");
   1819 	}
   1820 #endif
   1821 	vp->v_usecount--;
   1822 	if (vp->v_usecount > 0) {
   1823 		simple_unlock(&vp->v_interlock);
   1824 		return;
   1825 	}
   1826 	/*
   1827 	 * insert at tail of LRU list
   1828 	 */
   1829 	simple_lock(&vnode_free_list_slock);
   1830 	if (vp->v_holdcnt > 0)
   1831 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
   1832 	else
   1833 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
   1834 	simple_unlock(&vnode_free_list_slock);
   1835 	simple_unlock(&vp->v_interlock);
   1836 }
   1837 
   1838 /*
   1839  * We use this when we have vnodes that were loaded in solely for cleaning.
   1840  * There is no reason to believe that these vnodes will be referenced again
   1841  * soon, since the cleaning process is unrelated to normal filesystem
   1842  * activity.  Putting cleaned vnodes at the tail of the list has the effect
   1843  * of flushing the vnode LRU.  So, put vnodes that were loaded only for
   1844  * cleaning at the head of the list, instead.
   1845  */
   1846 void
   1847 lfs_vunref_head(vp)
   1848 	struct vnode *vp;
   1849 {
   1850 	simple_lock(&vp->v_interlock);
   1851 #ifdef DIAGNOSTIC
   1852 	if(vp->v_usecount==0) {
   1853 		panic("lfs_vunref: v_usecount<0");
   1854 	}
   1855 #endif
   1856 	vp->v_usecount--;
   1857 	if (vp->v_usecount > 0) {
   1858 		simple_unlock(&vp->v_interlock);
   1859 		return;
   1860 	}
   1861 	/*
   1862 	 * insert at head of LRU list
   1863 	 */
   1864 	simple_lock(&vnode_free_list_slock);
   1865 	TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
   1866 	simple_unlock(&vnode_free_list_slock);
   1867 	simple_unlock(&vp->v_interlock);
   1868 }
   1869 
   1870