Home | History | Annotate | Line # | Download | only in lfs
lfs_segment.c revision 1.16
      1 /*	$NetBSD: lfs_segment.c,v 1.16 1999/03/25 21:39:18 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 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 <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/namei.h>
     78 #include <sys/kernel.h>
     79 #include <sys/resourcevar.h>
     80 #include <sys/file.h>
     81 #include <sys/stat.h>
     82 #include <sys/buf.h>
     83 #include <sys/proc.h>
     84 #include <sys/conf.h>
     85 #include <sys/vnode.h>
     86 #include <sys/malloc.h>
     87 #include <sys/mount.h>
     88 
     89 #include <miscfs/specfs/specdev.h>
     90 #include <miscfs/fifofs/fifo.h>
     91 
     92 #include <ufs/ufs/quota.h>
     93 #include <ufs/ufs/inode.h>
     94 #include <ufs/ufs/dir.h>
     95 #include <ufs/ufs/ufsmount.h>
     96 #include <ufs/ufs/ufs_extern.h>
     97 
     98 #include <ufs/lfs/lfs.h>
     99 #include <ufs/lfs/lfs_extern.h>
    100 
    101 extern int count_lock_queue __P((void));
    102 extern struct simplelock vnode_free_list_slock;		/* XXX */
    103 extern TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* 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 *, register 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 
    139 /* Statistics Counters */
    140 int lfs_dostats = 1;
    141 struct lfs_stats lfs_stats;
    142 
    143 /* op values to lfs_writevnodes */
    144 #define	VN_REG	        0
    145 #define	VN_DIROP	1
    146 #define	VN_EMPTY	2
    147 #define VN_CLEAN        3
    148 
    149 #define LFS_MAX_ACTIVE          10
    150 
    151 /*
    152  * XXX KS - Set modification time on the Ifile, so the cleaner can
    153  * read the fs mod time off of it.  We don't set IN_UPDATE here,
    154  * since we don't really need this to be flushed to disk (and in any
    155  * case that wouldn't happen to the Ifile until we checkpoint).
    156  */
    157 void
    158 lfs_imtime(fs)
    159 	struct lfs *fs;
    160 {
    161 	struct timespec ts;
    162 	struct inode *ip;
    163 
    164 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    165 	ip = VTOI(fs->lfs_ivnode);
    166 	ip->i_ffs_mtime = ts.tv_sec;
    167 	ip->i_ffs_mtimensec = ts.tv_nsec;
    168 }
    169 
    170 /*
    171  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
    172  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
    173  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
    174  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
    175  */
    176 
    177 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
    178 #define IS_FLUSHING(fs,vp)  ((fs)->lfs_flushvp == (vp))
    179 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
    180 
    181 int
    182 lfs_vflush(vp)
    183 	struct vnode *vp;
    184 {
    185 	struct inode *ip;
    186 	struct lfs *fs;
    187 	struct segment *sp;
    188 	int error;
    189 	struct buf *bp;
    190 
    191 	/* Protect against VXLOCK deadlock in vinvalbuf() */
    192 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
    193 	lfs_seglock(fs, SEGM_SYNC);
    194 	SET_FLUSHING(fs,vp);
    195 	if (fs->lfs_nactive > LFS_MAX_ACTIVE) {
    196 		error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP);
    197 		CLR_FLUSHING(fs,vp);
    198 		lfs_segunlock(fs);
    199 		return error;
    200 	}
    201 	sp = fs->lfs_sp;
    202 
    203 	ip = VTOI(vp);
    204 	if (vp->v_dirtyblkhd.lh_first == NULL) {
    205 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
    206 	}
    207 	else if(lfs_dostats) {
    208 		if(vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & (IN_MODIFIED|IN_UPDATE|IN_ACCESS|IN_CHANGE|IN_CLEANING)))
    209 			++lfs_stats.vflush_invoked;
    210 #ifdef DEBUG_LFS
    211 		printf("V");
    212 #endif
    213 	}
    214 
    215 	/* XXX KS - can this ever happen?  I think so.... */
    216 	if(ip->i_flag & IN_CLEANING) {
    217 #ifdef DEBUG_LFS
    218 		printf("C");
    219 #endif
    220 		ip->i_flag &= ~IN_CLEANING;
    221 		/*
    222 		 * XXX Copyin all of the fake buffers *now* to avoid
    223 		 * a later panic; and take off B_INVAL.
    224 		 */
    225 		for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=bp->b_vnbufs.le_next) {
    226 			if((bp->b_flags & (B_CALL|B_INVAL))==(B_CALL|B_INVAL)) {
    227 				bp->b_data = malloc(bp->b_bufsize, M_SEGMENT, M_WAITOK);
    228 				copyin(bp->b_saveaddr, bp->b_data, bp->b_bcount);
    229 				bp->b_flags &= ~B_INVAL;
    230 			}
    231 		}
    232 
    233 		if(ip->i_flag & IN_MODIFIED) {
    234 			fs->lfs_uinodes--;
    235 #ifdef DEBUG_LFS
    236 			if((int32_t)fs->lfs_uinodes<0) {
    237 				printf("U4");
    238 				fs->lfs_uinodes=0;
    239 			}
    240 #endif
    241 		} else
    242 			ip->i_flag |= IN_MODIFIED;
    243 	}
    244 
    245 	do {
    246 		do {
    247 			if (vp->v_dirtyblkhd.lh_first != NULL)
    248 				lfs_writefile(fs, sp, vp);
    249 		} while (lfs_writeinode(fs, sp, ip));
    250 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
    251 
    252 	if(lfs_dostats) {
    253 		++lfs_stats.nwrites;
    254 		if (sp->seg_flags & SEGM_SYNC)
    255 			++lfs_stats.nsync_writes;
    256 		if (sp->seg_flags & SEGM_CKP)
    257 			++lfs_stats.ncheckpoints;
    258 	}
    259 	lfs_segunlock(fs);
    260 
    261 	CLR_FLUSHING(fs,vp);
    262 	return (0);
    263 }
    264 
    265 #ifdef DEBUG_LFS_VERBOSE
    266 # 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)
    267 #else
    268 # define vndebug(vp,str)
    269 #endif
    270 
    271 int
    272 lfs_writevnodes(fs, mp, sp, op)
    273 	struct lfs *fs;
    274 	struct mount *mp;
    275 	struct segment *sp;
    276 	int op;
    277 {
    278 	struct inode *ip;
    279 	struct vnode *vp;
    280 	int inodes_written=0;
    281 
    282 #ifndef LFS_NO_BACKVP_HACK
    283 	/* BEGIN HACK */
    284 #define	VN_OFFSET	(((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
    285 #define	BACK_VP(VP)	((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
    286 #define	BEG_OF_VLIST	((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
    287 
    288 	/* Find last vnode. */
    289  loop:	for (vp = mp->mnt_vnodelist.lh_first;
    290 	     vp && vp->v_mntvnodes.le_next != NULL;
    291 	     vp = vp->v_mntvnodes.le_next);
    292 	for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
    293 #else
    294 	loop:
    295 	for (vp = mp->mnt_vnodelist.lh_first;
    296 	     vp != NULL;
    297 	     vp = vp->v_mntvnodes.le_next) {
    298 #endif
    299 		/*
    300 		 * If the vnode that we are about to sync is no longer
    301 		 * associated with this mount point, start over.
    302 		 */
    303 		if (vp->v_mount != mp)
    304 			goto loop;
    305 
    306 		ip = VTOI(vp);
    307 		if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
    308 		    (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
    309 			vndebug(vp,"dirop");
    310 			continue;
    311 		}
    312 
    313 		if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) {
    314 			vndebug(vp,"empty");
    315 			continue;
    316 		}
    317 
    318 		if (vp->v_type == VNON) {
    319 			continue;
    320 		}
    321 
    322 		if(op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
    323 		   && !(ip->i_flag & IN_CLEANING)) {
    324 			vndebug(vp,"cleaning");
    325 			continue;
    326 		}
    327 
    328 		if (lfs_vref(vp)) {
    329 			vndebug(vp,"vref");
    330 			continue;
    331 		}
    332 
    333 #if 0 /* XXX KS - if we skip the ifile, things could go badly for us. */
    334 		if(WRITEINPROG(vp)) {
    335 			lfs_vunref(vp);
    336 #ifdef DEBUG_LFS
    337 			ivndebug(vp,"writevnodes/writeinprog");
    338 #endif
    339 			continue;
    340 		}
    341 #endif
    342 		/*
    343 		 * Write the inode/file if dirty and it's not the
    344 		 * the IFILE.
    345 		 */
    346 		if ((ip->i_flag &
    347 		     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE | IN_CLEANING) ||
    348 		     vp->v_dirtyblkhd.lh_first != NULL))
    349 		{
    350 			if(ip->i_number != LFS_IFILE_INUM
    351 			   && vp->v_dirtyblkhd.lh_first != NULL)
    352 			{
    353 				lfs_writefile(fs, sp, vp);
    354 			}
    355 			if(vp->v_dirtyblkhd.lh_first != NULL) {
    356 				if(WRITEINPROG(vp)) {
    357 #ifdef DEBUG_LFS
    358 					ivndebug(vp,"writevnodes/write2");
    359 #endif
    360 				} else if(!(ip->i_flag & (IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE|IN_CLEANING))) {
    361 #ifdef DEBUG_LFS
    362 					printf("<%d>",ip->i_number);
    363 #endif
    364 					ip->i_flag |= IN_MODIFIED;
    365 					++fs->lfs_uinodes;
    366 				}
    367 			}
    368 			(void) lfs_writeinode(fs, sp, ip);
    369 			inodes_written++;
    370 		}
    371 
    372 		if(vp->v_flag & VDIROP) {
    373 			--fs->lfs_dirvcount;
    374 			vp->v_flag &= ~VDIROP;
    375 			wakeup(&fs->lfs_dirvcount);
    376 			lfs_vunref(vp);
    377 		}
    378 
    379 		lfs_vunref(vp);
    380 	}
    381 	return inodes_written;
    382 }
    383 
    384 /*
    385  * There is a distinct difference in the interpretation of SEGM_CLEAN,
    386  * depending on whether it is passed *directly* to lfs_segwrite (i.e., we
    387  * were called from lfs_markv), or whether it was just in the segment flags
    388  * (we were called indirectly through getnewvnode/lfs_vflush).  In the former
    389  * case, we only want to write vnodes where cleaning is in progress; but
    390  * in the latter case, we might want to write all empty vnodes, or possibly
    391  * all vnodes.
    392  */
    393 int
    394 lfs_segwrite(mp, flags)
    395 	struct mount *mp;
    396 	int flags;			/* Do a checkpoint. */
    397 {
    398 	struct buf *bp;
    399 	struct inode *ip;
    400 	struct lfs *fs;
    401 	struct segment *sp;
    402 	struct vnode *vp;
    403 	SEGUSE *segusep;
    404 	ufs_daddr_t ibno;
    405 	int do_ckp, error, i;
    406 	int writer_set = 0;
    407 	int need_unlock = 0;
    408 
    409 	fs = VFSTOUFS(mp)->um_lfs;
    410 
    411 	lfs_imtime(fs);
    412 
    413 	/*
    414 	 * If we are not the cleaner, and we have fewer than MIN_FREE_SEGS
    415 	 * clean segments, wait until cleaner writes.
    416 	 */
    417 	if(!(flags & SEGM_CLEAN)
    418 	   && (!fs->lfs_seglock || !(fs->lfs_sp->seg_flags & SEGM_CLEAN)))
    419 	{
    420 		do {
    421 			if (fs->lfs_nclean <= MIN_FREE_SEGS
    422 			    || fs->lfs_avail <= 0)
    423 			{
    424 				wakeup(&lfs_allclean_wakeup);
    425 				wakeup(&fs->lfs_nextseg);
    426 				error = tsleep(&fs->lfs_avail, PRIBIO + 1,
    427 					       "lfs_avail", 0);
    428 				if (error) {
    429 					return (error);
    430 				}
    431 			}
    432 		} while (fs->lfs_nclean <= MIN_FREE_SEGS || fs->lfs_avail <= 0);
    433 	}
    434 
    435 	/*
    436 	 * Allocate a segment structure and enough space to hold pointers to
    437 	 * the maximum possible number of buffers which can be described in a
    438 	 * single summary block.
    439 	 */
    440 	do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
    441 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
    442 	sp = fs->lfs_sp;
    443 
    444 	/*
    445 	 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
    446 	 * in which case we have to flush *all* buffers off of this vnode.
    447 	 */
    448 	if((sp->seg_flags & SEGM_CLEAN) && !(fs->lfs_flushvp))
    449 		lfs_writevnodes(fs, mp, sp, VN_CLEAN);
    450 	else {
    451 		lfs_writevnodes(fs, mp, sp, VN_REG);
    452 		/*
    453 		 * XXX KS - If we're cleaning, we can't wait for dirops,
    454 		 * because they might be waiting on us.  The downside of this
    455 		 * is that, if we write anything besides cleaning blocks
    456 		 * while cleaning, the checkpoint is not completely
    457 		 * consistent.
    458 		 */
    459 		if(!(sp->seg_flags & SEGM_CLEAN)) {
    460 			while(fs->lfs_dirops)
    461 				if((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
    462 						"lfs writer", 0)))
    463 				{
    464 					free(sp->bpp, M_SEGMENT);
    465 					free(sp, M_SEGMENT);
    466 					return (error);
    467 				}
    468 			fs->lfs_writer++;
    469 			writer_set=1;
    470 			lfs_writevnodes(fs, mp, sp, VN_DIROP);
    471 			((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
    472 		}
    473 	}
    474 
    475 	/*
    476 	 * If we are doing a checkpoint, mark everything since the
    477 	 * last checkpoint as no longer ACTIVE.
    478 	 */
    479 	if (do_ckp) {
    480 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
    481 		     --ibno >= fs->lfs_cleansz; ) {
    482 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
    483 
    484 				panic("lfs_segwrite: ifile read");
    485 			segusep = (SEGUSE *)bp->b_data;
    486 			for (i = fs->lfs_sepb; i--; segusep++)
    487 				segusep->su_flags &= ~SEGUSE_ACTIVE;
    488 
    489 			/* But the current segment is still ACTIVE */
    490 			if (fs->lfs_curseg/fs->lfs_sepb==(ibno-fs->lfs_cleansz))
    491 				((SEGUSE *)(bp->b_data))[fs->lfs_curseg%fs->lfs_sepb].su_flags |= SEGUSE_ACTIVE;
    492 			error = VOP_BWRITE(bp);
    493 		}
    494 	}
    495 
    496 	if (do_ckp || fs->lfs_doifile) {
    497 	redo:
    498 		vp = fs->lfs_ivnode;
    499 		/*
    500 		 * Depending on the circumstances of our calling, the ifile
    501 		 * inode might be locked.  If it is, and if it is locked by
    502 		 * us, we should VREF instead of vget here.
    503 		 */
    504 		need_unlock = 0;
    505 		if(VOP_ISLOCKED(vp)
    506 		   && VTOI(vp)->i_lock.lk_lockholder == curproc->p_pid) {
    507 			VREF(vp);
    508 		} else {
    509 			while (vget(vp, LK_EXCLUSIVE))
    510 				continue;
    511 			need_unlock = 1;
    512 		}
    513 		ip = VTOI(vp);
    514 		if (vp->v_dirtyblkhd.lh_first != NULL)
    515 			lfs_writefile(fs, sp, vp);
    516 		(void)lfs_writeinode(fs, sp, ip);
    517 
    518 		/* Only vput if we used vget() above. */
    519 		if(need_unlock)
    520 			vput(vp);
    521 		else
    522 			vrele(vp);
    523 
    524 		if (lfs_writeseg(fs, sp) && do_ckp)
    525 			goto redo;
    526 	} else {
    527 		(void) lfs_writeseg(fs, sp);
    528 	}
    529 
    530 	/*
    531 	 * If the I/O count is non-zero, sleep until it reaches zero.
    532 	 * At the moment, the user's process hangs around so we can
    533 	 * sleep.
    534 	 */
    535 	fs->lfs_doifile = 0;
    536 	if(writer_set && --fs->lfs_writer==0)
    537 		wakeup(&fs->lfs_dirops);
    538 
    539 	if(lfs_dostats) {
    540 		++lfs_stats.nwrites;
    541 		if (sp->seg_flags & SEGM_SYNC)
    542 			++lfs_stats.nsync_writes;
    543 		if (sp->seg_flags & SEGM_CKP)
    544 			++lfs_stats.ncheckpoints;
    545 	}
    546 	lfs_segunlock(fs);
    547 	return (0);
    548 }
    549 
    550 /*
    551  * Write the dirty blocks associated with a vnode.
    552  */
    553 void
    554 lfs_writefile(fs, sp, vp)
    555 	struct lfs *fs;
    556 	struct segment *sp;
    557 	struct vnode *vp;
    558 {
    559 	struct buf *bp;
    560 	struct finfo *fip;
    561 	IFILE *ifp;
    562 
    563 
    564 	if (sp->seg_bytes_left < fs->lfs_bsize ||
    565 	    sp->sum_bytes_left < sizeof(struct finfo))
    566 		(void) lfs_writeseg(fs, sp);
    567 
    568 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
    569 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    570 
    571 	if(vp->v_flag & VDIROP)
    572 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
    573 
    574 	fip = sp->fip;
    575 	fip->fi_nblocks = 0;
    576 	fip->fi_ino = VTOI(vp)->i_number;
    577 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
    578 	fip->fi_version = ifp->if_version;
    579 	brelse(bp);
    580 
    581 	/*
    582 	 * It may not be necessary to write the meta-data blocks at this point,
    583 	 * as the roll-forward recovery code should be able to reconstruct the
    584 	 * list.
    585 	 *
    586 	 * We have to write them anyway, though, under two conditions: (1) the
    587 	 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
    588 	 * checkpointing.
    589 	 */
    590 	if((sp->seg_flags & SEGM_CLEAN)
    591 	   && VTOI(vp)->i_number != LFS_IFILE_INUM
    592 	   && !IS_FLUSHING(fs,vp))
    593 	{
    594 		lfs_gather(fs, sp, vp, lfs_match_fake);
    595 	} else
    596 		lfs_gather(fs, sp, vp, lfs_match_data);
    597 
    598 	if(lfs_writeindir
    599 	   || IS_FLUSHING(fs,vp)
    600 	   || (sp->seg_flags & SEGM_CKP))
    601 	{
    602 		lfs_gather(fs, sp, vp, lfs_match_indir);
    603 		lfs_gather(fs, sp, vp, lfs_match_dindir);
    604 /* XXX KS - when is TRIPLE not true? */ /* #ifdef TRIPLE */
    605 		lfs_gather(fs, sp, vp, lfs_match_tindir);
    606 /* #endif */
    607 	}
    608 	fip = sp->fip;
    609 	if (fip->fi_nblocks != 0) {
    610 		sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
    611 				   sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
    612 		sp->start_lbp = &sp->fip->fi_blocks[0];
    613 	} else {
    614 		sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
    615 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
    616 	}
    617 }
    618 
    619 int
    620 lfs_writeinode(fs, sp, ip)
    621 	struct lfs *fs;
    622 	struct segment *sp;
    623 	struct inode *ip;
    624 {
    625 	struct buf *bp, *ibp;
    626 	IFILE *ifp;
    627 	SEGUSE *sup;
    628 	ufs_daddr_t daddr;
    629 	ino_t ino;
    630 	int error, i, ndx;
    631 	int redo_ifile = 0;
    632 	struct timespec ts;
    633 
    634 	if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE | IN_CLEANING)))
    635 		return(0);
    636 
    637 	/* Allocate a new inode block if necessary. */
    638 	if (sp->ibp == NULL) {
    639 		/* Allocate a new segment if necessary. */
    640 		if (sp->seg_bytes_left < fs->lfs_bsize ||
    641 		    sp->sum_bytes_left < sizeof(ufs_daddr_t))
    642 			(void) lfs_writeseg(fs, sp);
    643 
    644 		/* Get next inode block. */
    645 		daddr = fs->lfs_offset;
    646 		fs->lfs_offset += fsbtodb(fs, 1);
    647 		sp->ibp = *sp->cbpp++ =
    648 			lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
    649 				   fs->lfs_bsize);
    650 		/* Zero out inode numbers */
    651 		for (i = 0; i < INOPB(fs); ++i)
    652 			((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
    653 
    654 		++sp->start_bpp;
    655 		fs->lfs_avail -= fsbtodb(fs, 1);
    656 		/* Set remaining space counters. */
    657 		sp->seg_bytes_left -= fs->lfs_bsize;
    658 		sp->sum_bytes_left -= sizeof(ufs_daddr_t);
    659 		ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
    660 			sp->ninodes / INOPB(fs) - 1;
    661 		((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
    662 	}
    663 
    664 	/* Update the inode times and copy the inode onto the inode page. */
    665 	if (ip->i_flag & (IN_CLEANING|IN_MODIFIED))
    666 		--fs->lfs_uinodes;
    667 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    668 	LFS_ITIMES(ip, &ts, &ts, &ts);
    669 
    670 	if(ip->i_flag & IN_CLEANING)
    671 		ip->i_flag &= ~IN_CLEANING;
    672 	else
    673 		ip->i_flag &= ~(IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE);
    674 
    675 	bp = sp->ibp;
    676 	((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] =
    677 		ip->i_din.ffs_din;
    678 
    679 	/* Increment inode count in segment summary block. */
    680 	++((SEGSUM *)(sp->segsum))->ss_ninos;
    681 
    682 	/* If this page is full, set flag to allocate a new page. */
    683 	if (++sp->ninodes % INOPB(fs) == 0)
    684 		sp->ibp = NULL;
    685 
    686 	/*
    687 	 * If updating the ifile, update the super-block.  Update the disk
    688 	 * address and access times for this inode in the ifile.
    689 	 */
    690 	ino = ip->i_number;
    691 	if (ino == LFS_IFILE_INUM) {
    692 		daddr = fs->lfs_idaddr;
    693 		fs->lfs_idaddr = bp->b_blkno;
    694 	} else {
    695 		LFS_IENTRY(ifp, fs, ino, ibp);
    696 		daddr = ifp->if_daddr;
    697 		ifp->if_daddr = bp->b_blkno;
    698 		error = VOP_BWRITE(ibp);
    699 	}
    700 
    701 	/*
    702 	 * No need to update segment usage if there was no former inode address
    703 	 * or if the last inode address is in the current partial segment.
    704 	 */
    705 	if (daddr != LFS_UNUSED_DADDR &&
    706 	    !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
    707 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
    708 #ifdef DIAGNOSTIC
    709 		if (sup->su_nbytes < DINODE_SIZE) {
    710 			/* XXX -- Change to a panic. */
    711 			printf("lfs_writeinode: negative bytes (segment %d)\n",
    712 			       datosn(fs, daddr));
    713 			panic("negative bytes");
    714 		}
    715 #endif
    716 		sup->su_nbytes -= DINODE_SIZE;
    717 		redo_ifile =
    718 			(ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
    719 		error = VOP_BWRITE(bp);
    720 	}
    721 	return (redo_ifile);
    722 }
    723 
    724 int
    725 lfs_gatherblock(sp, bp, sptr)
    726 	struct segment *sp;
    727 	struct buf *bp;
    728 	int *sptr;
    729 {
    730 	struct lfs *fs;
    731 	int version;
    732 
    733 	/*
    734 	 * If full, finish this segment.  We may be doing I/O, so
    735 	 * release and reacquire the splbio().
    736 	 */
    737 #ifdef DIAGNOSTIC
    738 	if (sp->vp == NULL)
    739 		panic ("lfs_gatherblock: Null vp in segment");
    740 #endif
    741 	fs = sp->fs;
    742 	if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
    743 	    sp->seg_bytes_left < bp->b_bcount) {
    744 		if (sptr)
    745 			splx(*sptr);
    746 		lfs_updatemeta(sp);
    747 
    748 		version = sp->fip->fi_version;
    749 		(void) lfs_writeseg(fs, sp);
    750 
    751 		sp->fip->fi_version = version;
    752 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
    753 		/* Add the current file to the segment summary. */
    754 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
    755 		sp->sum_bytes_left -=
    756 			sizeof(struct finfo) - sizeof(ufs_daddr_t);
    757 
    758 		if (sptr)
    759 			*sptr = splbio();
    760 		return(1);
    761 	}
    762 
    763 #ifdef DEBUG
    764 	if(bp->b_flags & B_GATHERED) {
    765 		printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
    766 		       sp->fip->fi_ino, bp->b_lblkno);
    767 		return(0);
    768 	}
    769 #endif
    770 	/* Insert into the buffer list, update the FINFO block. */
    771 	bp->b_flags |= B_GATHERED;
    772 	*sp->cbpp++ = bp;
    773 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
    774 
    775 	sp->sum_bytes_left -= sizeof(ufs_daddr_t);
    776 	sp->seg_bytes_left -= bp->b_bcount;
    777 	return(0);
    778 }
    779 
    780 int
    781 lfs_gather(fs, sp, vp, match)
    782 	struct lfs *fs;
    783 	struct segment *sp;
    784 	struct vnode *vp;
    785 	int (*match) __P((struct lfs *, struct buf *));
    786 {
    787 	struct buf *bp;
    788 	int s, count=0;
    789 
    790 	sp->vp = vp;
    791 	s = splbio();
    792 
    793 #ifndef LFS_NO_BACKBUF_HACK
    794 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
    795 #else /* LFS_NO_BACKBUF_HACK */
    796 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
    797 # define	BUF_OFFSET	(((void *)&bp->b_vnbufs.le_next) - (void *)bp)
    798 # define	BACK_BUF(BP)	((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
    799 # define	BEG_OF_LIST	((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
    800 /* Find last buffer. */
    801 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
    802 	    bp = bp->b_vnbufs.le_next);
    803 	for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
    804 #endif /* LFS_NO_BACKBUF_HACK */
    805 		if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
    806 			continue;
    807 #ifdef DIAGNOSTIC
    808 		if (!(bp->b_flags & B_DELWRI))
    809 			panic("lfs_gather: bp not B_DELWRI");
    810 		if (!(bp->b_flags & B_LOCKED))
    811 			panic("lfs_gather: bp not B_LOCKED");
    812 #endif
    813 		count++;
    814 		if (lfs_gatherblock(sp, bp, &s)) {
    815 			goto loop;
    816 		}
    817 	}
    818 	splx(s);
    819 	lfs_updatemeta(sp);
    820 	sp->vp = NULL;
    821 	return count;
    822 }
    823 
    824 /*
    825  * Update the metadata that points to the blocks listed in the FINFO
    826  * array.
    827  */
    828 void
    829 lfs_updatemeta(sp)
    830 	struct segment *sp;
    831 {
    832 	SEGUSE *sup;
    833 	struct buf *bp;
    834 	struct lfs *fs;
    835 	struct vnode *vp;
    836 	struct indir a[NIADDR + 2], *ap;
    837 	struct inode *ip;
    838 	ufs_daddr_t daddr, lbn, off;
    839 	int error, i, nblocks, num;
    840 
    841 	vp = sp->vp;
    842 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
    843 	if (nblocks < 0)
    844 		panic("This is a bad thing\n");
    845 	if (vp == NULL || nblocks == 0)
    846 		return;
    847 
    848 	/* Sort the blocks. */
    849 	/*
    850 	 * XXX KS - We have to sort even if the blocks come from the
    851 	 * cleaner, because there might be other pending blocks on the
    852 	 * same inode...and if we don't sort, and there are fragments
    853 	 * present, blocks may be written in the wrong place.
    854 	 */
    855 	/* if (!(sp->seg_flags & SEGM_CLEAN)) */
    856 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
    857 
    858 	/*
    859 	 * Record the length of the last block in case it's a fragment.
    860 	 * If there are indirect blocks present, they sort last.  An
    861 	 * indirect block will be lfs_bsize and its presence indicates
    862 	 * that you cannot have fragments.
    863 	 */
    864 	sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
    865 
    866 	/*
    867 	 * Assign disk addresses, and update references to the logical
    868 	 * block and the segment usage information.
    869 	 */
    870 	fs = sp->fs;
    871 	for (i = nblocks; i--; ++sp->start_bpp) {
    872 		lbn = *sp->start_lbp++;
    873 
    874 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
    875 		fs->lfs_offset +=
    876 			fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
    877 
    878 		error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
    879 		if (error)
    880 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
    881 		ip = VTOI(vp);
    882 		switch (num) {
    883 		case 0:
    884 			ip->i_ffs_db[lbn] = off;
    885 			break;
    886 		case 1:
    887 			ip->i_ffs_ib[a[0].in_off] = off;
    888 			break;
    889 		default:
    890 			ap = &a[num - 1];
    891 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
    892 				panic("lfs_updatemeta: bread bno %d",
    893 				      ap->in_lbn);
    894 			/*
    895 			 * Bread may create a new (indirect) block which needs
    896 			 * to get counted for the inode.
    897 			 */
    898 			if (/* bp->b_blkno == -1 && */
    899 			    !(bp->b_flags & (B_DELWRI|B_DONE))) {
    900 				ip->i_ffs_blocks += fsbtodb(fs, 1);
    901 				fs->lfs_bfree -= fragstodb(fs, fs->lfs_frag);
    902 			}
    903 			((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
    904 			VOP_BWRITE(bp);
    905 		}
    906 		/* Update segment usage information. */
    907 		if (daddr != UNASSIGNED &&
    908 		    !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
    909 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
    910 #ifdef DIAGNOSTIC
    911 			if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
    912 				/* XXX -- Change to a panic. */
    913 				printf("lfs_updatemeta: negative bytes (segment %d)\n",
    914 				       datosn(fs, daddr));
    915 				printf("lfs_updatemeta: bp = 0x%p, addr = 0x%p\n",
    916 				       bp, bp->b_un.b_addr);
    917 				/* panic ("Negative Bytes"); */
    918 			}
    919 #endif
    920 			sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
    921 			error = VOP_BWRITE(bp);
    922 		}
    923 	}
    924 }
    925 
    926 /*
    927  * Start a new segment.
    928  */
    929 int
    930 lfs_initseg(fs)
    931 	struct lfs *fs;
    932 {
    933 	struct segment *sp;
    934 	SEGUSE *sup;
    935 	SEGSUM *ssp;
    936 	struct buf *bp;
    937 	int repeat;
    938 
    939 	sp = fs->lfs_sp;
    940 
    941 	repeat = 0;
    942 	/* Advance to the next segment. */
    943 	if (!LFS_PARTIAL_FITS(fs)) {
    944 		/* Wake up any cleaning procs waiting on this file system. */
    945 		wakeup(&lfs_allclean_wakeup);
    946 		wakeup(&fs->lfs_nextseg);
    947 		lfs_newseg(fs);
    948 		repeat = 1;
    949 		fs->lfs_offset = fs->lfs_curseg;
    950 		sp->seg_number = datosn(fs, fs->lfs_curseg);
    951 		sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
    952 		/*
    953 		 * If the segment contains a superblock, update the offset
    954 		 * and summary address to skip over it.
    955 		 */
    956 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
    957 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
    958 			fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
    959 			sp->seg_bytes_left -= LFS_SBPAD;
    960 		}
    961 		brelse(bp);
    962 	} else {
    963 		sp->seg_number = datosn(fs, fs->lfs_curseg);
    964 		sp->seg_bytes_left = (fs->lfs_dbpseg -
    965 				      (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
    966 	}
    967 	fs->lfs_lastpseg = fs->lfs_offset;
    968 
    969 	sp->fs = fs;
    970 	sp->ibp = NULL;
    971 	sp->ninodes = 0;
    972 
    973 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
    974 	sp->cbpp = sp->bpp;
    975 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
    976 			       fs->lfs_offset, LFS_SUMMARY_SIZE);
    977 	sp->segsum = (*sp->cbpp)->b_data;
    978 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
    979 	sp->start_bpp = ++sp->cbpp;
    980 	fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
    981 
    982 	/* Set point to SEGSUM, initialize it. */
    983 	ssp = sp->segsum;
    984 	ssp->ss_next = fs->lfs_nextseg;
    985 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
    986 	ssp->ss_magic = SS_MAGIC;
    987 
    988 	/* Set pointer to first FINFO, initialize it. */
    989 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
    990 	sp->fip->fi_nblocks = 0;
    991 	sp->start_lbp = &sp->fip->fi_blocks[0];
    992 	sp->fip->fi_lastlength = 0;
    993 
    994 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
    995 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
    996 
    997 	return(repeat);
    998 }
    999 
   1000 /*
   1001  * Return the next segment to write.
   1002  */
   1003 void
   1004 lfs_newseg(fs)
   1005 	struct lfs *fs;
   1006 {
   1007 	CLEANERINFO *cip;
   1008 	SEGUSE *sup;
   1009 	struct buf *bp;
   1010 	int curseg, isdirty, sn;
   1011 
   1012 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
   1013 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1014 	sup->su_nbytes = 0;
   1015 	sup->su_nsums = 0;
   1016 	sup->su_ninos = 0;
   1017 	(void) VOP_BWRITE(bp);
   1018 
   1019 	LFS_CLEANERINFO(cip, fs, bp);
   1020 	--cip->clean;
   1021 	++cip->dirty;
   1022 	fs->lfs_nclean = cip->clean;
   1023 	(void) VOP_BWRITE(bp);
   1024 
   1025 	fs->lfs_lastseg = fs->lfs_curseg;
   1026 	fs->lfs_curseg = fs->lfs_nextseg;
   1027 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
   1028 		sn = (sn + 1) % fs->lfs_nseg;
   1029 		if (sn == curseg)
   1030 			panic("lfs_nextseg: no clean segments");
   1031 		LFS_SEGENTRY(sup, fs, sn, bp);
   1032 		isdirty = sup->su_flags & SEGUSE_DIRTY;
   1033 		brelse(bp);
   1034 		if (!isdirty)
   1035 			break;
   1036 	}
   1037 
   1038 	++fs->lfs_nactive;
   1039 	fs->lfs_nextseg = sntoda(fs, sn);
   1040 	if(lfs_dostats) {
   1041 		++lfs_stats.segsused;
   1042 	}
   1043 }
   1044 
   1045 int
   1046 lfs_writeseg(fs, sp)
   1047 	struct lfs *fs;
   1048 	struct segment *sp;
   1049 {
   1050 	extern int locked_queue_count;
   1051 	extern long locked_queue_bytes;
   1052 	struct buf **bpp, *bp, *cbp;
   1053 	SEGUSE *sup;
   1054 	SEGSUM *ssp;
   1055 	dev_t i_dev;
   1056 	u_long *datap, *dp;
   1057 	int do_again, i, nblocks, s;
   1058 #ifdef LFS_TRACK_IOS
   1059 	int j;
   1060 #endif
   1061 	int (*strategy)__P((void *));
   1062 	struct vop_strategy_args vop_strategy_a;
   1063 	u_short ninos;
   1064 	struct vnode *devvp;
   1065 	char *p;
   1066 	struct vnode *vn;
   1067 #if defined(DEBUG) && defined(LFS_PROPELLER)
   1068 	static int propeller;
   1069 	char propstring[4] = "-\\|/";
   1070 
   1071 	printf("%c\b",propstring[propeller++]);
   1072 	if(propeller==4)
   1073 		propeller = 0;
   1074 #endif
   1075 
   1076 	/*
   1077 	 * If there are no buffers other than the segment summary to write
   1078 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
   1079 	 * even if there aren't any buffers, you need to write the superblock.
   1080 	 */
   1081 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
   1082 		return (0);
   1083 
   1084 #ifdef DEBUG_LFS
   1085 	lfs_check_bpp(fs,sp,__FILE__,__LINE__);
   1086 #endif
   1087 
   1088 	/* Update the segment usage information. */
   1089 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
   1090 
   1091 	/* Loop through all blocks, except the segment summary. */
   1092 	for (bpp = sp->bpp; ++bpp < sp->cbpp; )
   1093 		sup->su_nbytes += (*bpp)->b_bcount;
   1094 
   1095 	ssp = (SEGSUM *)sp->segsum;
   1096 
   1097 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
   1098 	/* sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE; */
   1099 	sup->su_nbytes += LFS_SUMMARY_SIZE;
   1100 	sup->su_lastmod = time.tv_sec;
   1101 	sup->su_ninos += ninos;
   1102 	++sup->su_nsums;
   1103 
   1104 	do_again = !(bp->b_flags & B_GATHERED);
   1105 	(void)VOP_BWRITE(bp);
   1106 	/*
   1107 	 * Compute checksum across data and then across summary; the first
   1108 	 * block (the summary block) is skipped.  Set the create time here
   1109 	 * so that it's guaranteed to be later than the inode mod times.
   1110 	 *
   1111 	 * XXX
   1112 	 * Fix this to do it inline, instead of malloc/copy.
   1113 	 */
   1114 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
   1115 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
   1116 		if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1117 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
   1118 				panic("lfs_writeseg: copyin failed [1]: ino %d blk %d", VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno);
   1119 		} else
   1120 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
   1121 	}
   1122 	ssp->ss_create = time.tv_sec;
   1123 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
   1124 	ssp->ss_sumsum =
   1125 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
   1126 	free(datap, M_SEGMENT);
   1127 #ifdef DIAGNOSTIC
   1128 	if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
   1129 		panic("lfs_writeseg: No diskspace for summary");
   1130 #endif
   1131 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
   1132 
   1133 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1134 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
   1135 	strategy = devvp->v_op[VOFFSET(vop_strategy)];
   1136 
   1137 	/*
   1138 	 * When we simply write the blocks we lose a rotation for every block
   1139 	 * written.  To avoid this problem, we allocate memory in chunks, copy
   1140 	 * the buffers into the chunk and write the chunk.  CHUNKSIZE is the
   1141 	 * largest size I/O devices can handle.
   1142 	 * When the data is copied to the chunk, turn off the the B_LOCKED bit
   1143 	 * and brelse the buffer (which will move them to the LRU list).  Add
   1144 	 * the B_CALL flag to the buffer header so we can count I/O's for the
   1145 	 * checkpoints and so we can release the allocated memory.
   1146 	 *
   1147 	 * XXX
   1148 	 * This should be removed if the new virtual memory system allows us to
   1149 	 * easily make the buffers contiguous in kernel memory and if that's
   1150 	 * fast enough.
   1151 	 */
   1152 
   1153 #define CHUNKSIZE MAXPHYS
   1154 
   1155 	if(devvp==NULL)
   1156 		panic("devvp is NULL");
   1157 	for (bpp = sp->bpp,i = nblocks; i;) {
   1158 		cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE);
   1159 		cbp->b_dev = i_dev;
   1160 		cbp->b_flags |= B_ASYNC | B_BUSY;
   1161 		cbp->b_bcount = 0;
   1162 
   1163 		if(fs->lfs_iocount >= LFS_THROTTLE) {
   1164 			tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
   1165 		}
   1166 		s = splbio();
   1167 		++fs->lfs_iocount;
   1168 #ifdef LFS_TRACK_IOS
   1169 		for(j=0;j<LFS_THROTTLE;j++) {
   1170 			if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) {
   1171 				fs->lfs_pending[j] = cbp->b_blkno;
   1172 				break;
   1173 			}
   1174 		}
   1175 #endif /* LFS_TRACK_IOS */
   1176 		for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
   1177 			bp = *bpp;
   1178 
   1179 			if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
   1180 				break;
   1181 
   1182 			/*
   1183 			 * Fake buffers from the cleaner are marked as B_INVAL.
   1184 			 * We need to copy the data from user space rather than
   1185 			 * from the buffer indicated.
   1186 			 * XXX == what do I do on an error?
   1187 			 */
   1188 			if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
   1189 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
   1190 					panic("lfs_writeseg: copyin failed [2]");
   1191 			} else
   1192 				bcopy(bp->b_data, p, bp->b_bcount);
   1193 			p += bp->b_bcount;
   1194 			cbp->b_bcount += bp->b_bcount;
   1195 			if (bp->b_flags & B_LOCKED) {
   1196 				--locked_queue_count;
   1197 				locked_queue_bytes -= bp->b_bufsize;
   1198 			}
   1199 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
   1200 					 B_LOCKED | B_GATHERED);
   1201 			vn = bp->b_vp;
   1202 			if (bp->b_flags & B_CALL) {
   1203 				/* if B_CALL, it was created with newbuf */
   1204 				lfs_freebuf(bp);
   1205 			} else {
   1206 				bremfree(bp);
   1207 				bp->b_flags |= B_DONE;
   1208 				if(vn)
   1209 					reassignbuf(bp, vn);
   1210 				brelse(bp);
   1211 			}
   1212 			if(bp->b_flags & B_NEEDCOMMIT) { /* XXX */
   1213 				bp->b_flags &= ~B_NEEDCOMMIT;
   1214 				wakeup(bp);
   1215 			}
   1216 			bpp++;
   1217 		}
   1218 		++cbp->b_vp->v_numoutput;
   1219 		splx(s);
   1220 		/*
   1221 		 * XXXX This is a gross and disgusting hack.  Since these
   1222 		 * buffers are physically addressed, they hang off the
   1223 		 * device vnode (devvp).  As a result, they have no way
   1224 		 * of getting to the LFS superblock or lfs structure to
   1225 		 * keep track of the number of I/O's pending.  So, I am
   1226 		 * going to stuff the fs into the saveaddr field of
   1227 		 * the buffer (yuk).
   1228 		 */
   1229 		cbp->b_saveaddr = (caddr_t)fs;
   1230 		vop_strategy_a.a_desc = VDESC(vop_strategy);
   1231 		vop_strategy_a.a_bp = cbp;
   1232 		(strategy)(&vop_strategy_a);
   1233 	}
   1234 	/*
   1235 	 * XXX
   1236 	 * Vinvalbuf can move locked buffers off the locked queue
   1237 	 * and we have no way of knowing about this.  So, after
   1238 	 * doing a big write, we recalculate how many buffers are
   1239 	 * really still left on the locked queue.
   1240 	 */
   1241 	lfs_countlocked(&locked_queue_count,&locked_queue_bytes);
   1242 	wakeup(&locked_queue_count);
   1243 	if(lfs_dostats) {
   1244 		++lfs_stats.psegwrites;
   1245 		lfs_stats.blocktot += nblocks - 1;
   1246 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
   1247 			++lfs_stats.psyncwrites;
   1248 		if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
   1249 			++lfs_stats.pcleanwrites;
   1250 			lfs_stats.cleanblocks += nblocks - 1;
   1251 		}
   1252 	}
   1253 	return (lfs_initseg(fs) || do_again);
   1254 }
   1255 
   1256 void
   1257 lfs_writesuper(fs, daddr)
   1258 	struct lfs *fs;
   1259 	daddr_t daddr;
   1260 {
   1261 	struct buf *bp;
   1262 	dev_t i_dev;
   1263 	int (*strategy) __P((void *));
   1264 	int s;
   1265 	struct vop_strategy_args vop_strategy_a;
   1266 
   1267 #ifdef LFS_CANNOT_ROLLFW
   1268 	/*
   1269 	 * If we can write one superblock while another is in
   1270 	 * progress, we risk not having a complete checkpoint if we crash.
   1271 	 * So, block here if a superblock write is in progress.
   1272 	 *
   1273 	 * XXX - should be a proper lock, not this hack
   1274 	 */
   1275 	while(fs->lfs_sbactive) {
   1276 		tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
   1277 	}
   1278 	fs->lfs_sbactive = daddr;
   1279 #endif
   1280 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
   1281 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
   1282 
   1283 	/* Set timestamp of this version of the superblock */
   1284 	fs->lfs_tstamp = time.tv_sec;
   1285 
   1286 	/* Checksum the superblock and copy it into a buffer. */
   1287 	fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
   1288 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD);
   1289 	*(struct dlfs *)bp->b_data = fs->lfs_dlfs;
   1290 
   1291 	bp->b_dev = i_dev;
   1292 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
   1293 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
   1294 	bp->b_iodone = lfs_supercallback;
   1295 	/* XXX KS - same nasty hack as above */
   1296 	bp->b_saveaddr = (caddr_t)fs;
   1297 
   1298 	vop_strategy_a.a_desc = VDESC(vop_strategy);
   1299 	vop_strategy_a.a_bp = bp;
   1300 	s = splbio();
   1301 	++bp->b_vp->v_numoutput;
   1302 	splx(s);
   1303 	(strategy)(&vop_strategy_a);
   1304 }
   1305 
   1306 /*
   1307  * Logical block number match routines used when traversing the dirty block
   1308  * chain.
   1309  */
   1310 int
   1311 lfs_match_fake(fs, bp)
   1312 	struct lfs *fs;
   1313 	struct buf *bp;
   1314 {
   1315 	return (bp->b_flags & (B_CALL|B_INVAL))==(B_CALL|B_INVAL);
   1316 }
   1317 
   1318 int
   1319 lfs_match_data(fs, bp)
   1320 	struct lfs *fs;
   1321 	struct buf *bp;
   1322 {
   1323 	return (bp->b_lblkno >= 0);
   1324 }
   1325 
   1326 int
   1327 lfs_match_indir(fs, bp)
   1328 	struct lfs *fs;
   1329 	struct buf *bp;
   1330 {
   1331 	int lbn;
   1332 
   1333 	lbn = bp->b_lblkno;
   1334 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
   1335 }
   1336 
   1337 int
   1338 lfs_match_dindir(fs, bp)
   1339 	struct lfs *fs;
   1340 	struct buf *bp;
   1341 {
   1342 	int lbn;
   1343 
   1344 	lbn = bp->b_lblkno;
   1345 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
   1346 }
   1347 
   1348 int
   1349 lfs_match_tindir(fs, bp)
   1350 	struct lfs *fs;
   1351 	struct buf *bp;
   1352 {
   1353 	int lbn;
   1354 
   1355 	lbn = bp->b_lblkno;
   1356 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
   1357 }
   1358 
   1359 /*
   1360  * XXX - The only buffers that are going to hit these functions are the
   1361  * segment write blocks, or the segment summaries, or the superblocks.
   1362  *
   1363  * All of the above are created by lfs_newbuf, and so do not need to be
   1364  * released via brelse.
   1365  */
   1366 void
   1367 lfs_callback(bp)
   1368 	struct buf *bp;
   1369 {
   1370 	struct lfs *fs;
   1371 #ifdef LFS_TRACK_IOS
   1372 	int j;
   1373 #endif
   1374 
   1375 	fs = (struct lfs *)bp->b_saveaddr;
   1376 #ifdef DIAGNOSTIC
   1377 	if (fs->lfs_iocount == 0)
   1378 		panic("lfs_callback: zero iocount\n");
   1379 #endif
   1380 	if (--fs->lfs_iocount < LFS_THROTTLE)
   1381 		wakeup(&fs->lfs_iocount);
   1382 #ifdef LFS_TRACK_IOS
   1383 	for(j=0;j<LFS_THROTTLE;j++) {
   1384 		if(fs->lfs_pending[j]==bp->b_blkno) {
   1385 			fs->lfs_pending[j] = LFS_UNUSED_DADDR;
   1386 			wakeup(&(fs->lfs_pending[j]));
   1387 			break;
   1388 		}
   1389 	}
   1390 #endif /* LFS_TRACK_IOS */
   1391 
   1392 	lfs_freebuf(bp);
   1393 }
   1394 
   1395 void
   1396 lfs_supercallback(bp)
   1397 	struct buf *bp;
   1398 {
   1399 #ifdef LFS_CANNOT_ROLLFW
   1400 	struct lfs *fs;
   1401 
   1402 	fs = (struct lfs *)bp->b_saveaddr;
   1403 	fs->lfs_sbactive=NULL;
   1404 	wakeup(&fs->lfs_sbactive);
   1405 #endif
   1406 	lfs_freebuf(bp);
   1407 }
   1408 
   1409 /*
   1410  * Shellsort (diminishing increment sort) from Data Structures and
   1411  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
   1412  * see also Knuth Vol. 3, page 84.  The increments are selected from
   1413  * formula (8), page 95.  Roughly O(N^3/2).
   1414  */
   1415 /*
   1416  * This is our own private copy of shellsort because we want to sort
   1417  * two parallel arrays (the array of buffer pointers and the array of
   1418  * logical block numbers) simultaneously.  Note that we cast the array
   1419  * of logical block numbers to a unsigned in this routine so that the
   1420  * negative block numbers (meta data blocks) sort AFTER the data blocks.
   1421  */
   1422 
   1423 void
   1424 lfs_shellsort(bp_array, lb_array, nmemb)
   1425 	struct buf **bp_array;
   1426 	ufs_daddr_t *lb_array;
   1427 	register int nmemb;
   1428 {
   1429 	static int __rsshell_increments[] = { 4, 1, 0 };
   1430 	register int incr, *incrp, t1, t2;
   1431 	struct buf *bp_temp;
   1432 	u_long lb_temp;
   1433 
   1434 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
   1435 		for (t1 = incr; t1 < nmemb; ++t1)
   1436 			for (t2 = t1 - incr; t2 >= 0;)
   1437 				if (lb_array[t2] > lb_array[t2 + incr]) {
   1438 					lb_temp = lb_array[t2];
   1439 					lb_array[t2] = lb_array[t2 + incr];
   1440 					lb_array[t2 + incr] = lb_temp;
   1441 					bp_temp = bp_array[t2];
   1442 					bp_array[t2] = bp_array[t2 + incr];
   1443 					bp_array[t2 + incr] = bp_temp;
   1444 					t2 -= incr;
   1445 				} else
   1446 					break;
   1447 }
   1448 
   1449 /*
   1450  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
   1451  */
   1452 int
   1453 lfs_vref(vp)
   1454 	register struct vnode *vp;
   1455 {
   1456 	/*
   1457 	 * If we return 1 here during a flush, we risk vinvalbuf() not
   1458 	 * being able to flush all of the pages from this vnode, which
   1459 	 * will cause it to panic.  So, return 0 if a flush is in progress.
   1460 	 */
   1461 	if (vp->v_flag & VXLOCK) {
   1462 		if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1463 			vp->v_usecount++;
   1464 			return 0;
   1465 		}
   1466 		return(1);
   1467 	}
   1468 	return (vget(vp, 0));
   1469 }
   1470 
   1471 /*
   1472  * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
   1473  * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
   1474  */
   1475 void
   1476 lfs_vunref(vp)
   1477 	register struct vnode *vp;
   1478 {
   1479 	simple_lock(&vp->v_interlock);
   1480 #ifdef DIAGNOSTIC
   1481 	if(vp->v_usecount==0) {
   1482 		panic("lfs_vunref: v_usecount<0");
   1483 	}
   1484 #endif
   1485 	vp->v_usecount--;
   1486 	if (vp->v_usecount > 0) {
   1487 		simple_unlock(&vp->v_interlock);
   1488 		return;
   1489 	}
   1490 	/*
   1491 	 * We also don't want to vrele() here during a flush, since
   1492 	 * that will be done again later, causing us serious problems.
   1493 	 */
   1494 	if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
   1495 		simple_unlock(&vp->v_interlock);
   1496 		return;
   1497 	}
   1498 
   1499 	/*
   1500 	 * insert at tail of LRU list
   1501 	 */
   1502 	simple_lock(&vnode_free_list_slock);
   1503 	TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
   1504 	simple_unlock(&vnode_free_list_slock);
   1505 	simple_unlock(&vp->v_interlock);
   1506 }
   1507 
   1508 /*
   1509  * We use this when we have vnodes that were loaded in solely for cleaning.
   1510  * There is no reason to believe that these vnodes will be referenced again
   1511  * soon, since the cleaning process is unrelated to normal filesystem
   1512  * activity.  Putting cleaned vnodes at the tail of the list has the effect
   1513  * of flushing the vnode LRU.  So, put vnodes that were loaded only for
   1514  * cleaning at the head of the list, instead.
   1515  */
   1516 void
   1517 lfs_vunref_head(vp)
   1518 	register struct vnode *vp;
   1519 {
   1520 	simple_lock(&vp->v_interlock);
   1521 #ifdef DIAGNOSTIC
   1522 	if(vp->v_usecount==0) {
   1523 		panic("lfs_vunref: v_usecount<0");
   1524 	}
   1525 #endif
   1526 	vp->v_usecount--;
   1527 	if (vp->v_usecount > 0) {
   1528 		simple_unlock(&vp->v_interlock);
   1529 		return;
   1530 	}
   1531 	/*
   1532 	 * insert at head of LRU list
   1533 	 */
   1534 	simple_lock(&vnode_free_list_slock);
   1535 	TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
   1536 	simple_unlock(&vnode_free_list_slock);
   1537 	simple_unlock(&vp->v_interlock);
   1538 }
   1539 
   1540