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