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