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