xref: /netbsd-src/sys/ufs/lfs/lfs_segment.c (revision 4b71a66d0f279143147d63ebfcfd8a59499a3684)
1 /*	$NetBSD: lfs_segment.c,v 1.212 2008/05/16 09:22:00 hannken 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.212 2008/05/16 09:22:00 hannken 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, fs->lfs_cleansz + n,
686 			    fs->lfs_bsize, NOCRED, B_MODIFY, &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,
1479 			B_MODIFY, &bp))
1480 			    panic("lfs_updatemeta: bread bno %" PRId64,
1481 				  ap->in_lbn);
1482 
1483 		    /* XXX ondisk32 */
1484 		    ooff = ((int32_t *)bp->b_data)[ap->in_off];
1485 		    DEBUG_OOFF(num);
1486 		    if (ooff == UNWRITTEN)
1487 			    ip->i_ffs1_blocks += bb;
1488 		    /* XXX ondisk32 */
1489 		    ((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
1490 		    (void) VOP_BWRITE(bp);
1491 	}
1492 
1493 	KASSERT(ooff == 0 || ooff == UNWRITTEN || ooff == daddr);
1494 
1495 	/* Update hiblk when extending the file */
1496 	if (lbn > ip->i_lfs_hiblk)
1497 		ip->i_lfs_hiblk = lbn;
1498 
1499 	/*
1500 	 * Though we'd rather it couldn't, this *can* happen right now
1501 	 * if cleaning blocks and regular blocks coexist.
1502 	 */
1503 	/* KASSERT(daddr < fs->lfs_lastpseg || daddr > ndaddr); */
1504 
1505 	/*
1506 	 * Update segment usage information, based on old size
1507 	 * and location.
1508 	 */
1509 	if (daddr > 0) {
1510 		u_int32_t oldsn = dtosn(fs, daddr);
1511 #ifdef DIAGNOSTIC
1512 		int ndupino;
1513 
1514 		if (sp && sp->seg_number == oldsn) {
1515 			ndupino = sp->ndupino;
1516 		} else {
1517 			ndupino = 0;
1518 		}
1519 #endif
1520 		KASSERT(oldsn < fs->lfs_nseg);
1521 		if (lbn >= 0 && lbn < NDADDR)
1522 			osize = ip->i_lfs_fragsize[lbn];
1523 		else
1524 			osize = fs->lfs_bsize;
1525 		LFS_SEGENTRY(sup, fs, oldsn, bp);
1526 #ifdef DIAGNOSTIC
1527 		if (sup->su_nbytes + sizeof (struct ufs1_dinode) * ndupino
1528 		    < osize) {
1529 			printf("lfs_updatemeta: negative bytes "
1530 			       "(segment %" PRIu32 " short by %" PRId64
1531 			       ")\n", dtosn(fs, daddr),
1532 			       (int64_t)osize -
1533 			       (sizeof (struct ufs1_dinode) * ndupino +
1534 				sup->su_nbytes));
1535 			printf("lfs_updatemeta: ino %llu, lbn %" PRId64
1536 			       ", addr = 0x%" PRIx64 "\n",
1537 			       (unsigned long long)ip->i_number, lbn, daddr);
1538 			printf("lfs_updatemeta: ndupino=%d\n", ndupino);
1539 			panic("lfs_updatemeta: negative bytes");
1540 			sup->su_nbytes = osize -
1541 			    sizeof (struct ufs1_dinode) * ndupino;
1542 		}
1543 #endif
1544 		DLOG((DLOG_SU, "seg %" PRIu32 " -= %d for ino %d lbn %" PRId64
1545 		      " db 0x%" PRIx64 "\n",
1546 		      dtosn(fs, daddr), osize,
1547 		      ip->i_number, lbn, daddr));
1548 		sup->su_nbytes -= osize;
1549 		if (!(bp->b_flags & B_GATHERED)) {
1550 			mutex_enter(&lfs_lock);
1551 			fs->lfs_flags |= LFS_IFDIRTY;
1552 			mutex_exit(&lfs_lock);
1553 		}
1554 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
1555 	}
1556 	/*
1557 	 * Now that this block has a new address, and its old
1558 	 * segment no longer owns it, we can forget about its
1559 	 * old size.
1560 	 */
1561 	if (lbn >= 0 && lbn < NDADDR)
1562 		ip->i_lfs_fragsize[lbn] = size;
1563 }
1564 
1565 /*
1566  * Update the metadata that points to the blocks listed in the FINFO
1567  * array.
1568  */
1569 void
1570 lfs_updatemeta(struct segment *sp)
1571 {
1572 	struct buf *sbp;
1573 	struct lfs *fs;
1574 	struct vnode *vp;
1575 	daddr_t lbn;
1576 	int i, nblocks, num;
1577 	int bb;
1578 	int bytesleft, size;
1579 
1580 	ASSERT_SEGLOCK(sp->fs);
1581 	vp = sp->vp;
1582 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1583 	KASSERT(nblocks >= 0);
1584 	KASSERT(vp != NULL);
1585 	if (nblocks == 0)
1586 		return;
1587 
1588 	/*
1589 	 * This count may be high due to oversize blocks from lfs_gop_write.
1590 	 * Correct for this. (XXX we should be able to keep track of these.)
1591 	 */
1592 	fs = sp->fs;
1593 	for (i = 0; i < nblocks; i++) {
1594 		if (sp->start_bpp[i] == NULL) {
1595 			DLOG((DLOG_SEG, "lfs_updatemeta: nblocks = %d, not %d\n", i, nblocks));
1596 			nblocks = i;
1597 			break;
1598 		}
1599 		num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
1600 		KASSERT(sp->start_bpp[i]->b_lblkno >= 0 || num == 1);
1601 		nblocks -= num - 1;
1602 	}
1603 
1604 	KASSERT(vp->v_type == VREG ||
1605 	   nblocks == &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp);
1606 	KASSERT(nblocks == sp->cbpp - sp->start_bpp);
1607 
1608 	/*
1609 	 * Sort the blocks.
1610 	 *
1611 	 * We have to sort even if the blocks come from the
1612 	 * cleaner, because there might be other pending blocks on the
1613 	 * same inode...and if we don't sort, and there are fragments
1614 	 * present, blocks may be written in the wrong place.
1615 	 */
1616 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
1617 
1618 	/*
1619 	 * Record the length of the last block in case it's a fragment.
1620 	 * If there are indirect blocks present, they sort last.  An
1621 	 * indirect block will be lfs_bsize and its presence indicates
1622 	 * that you cannot have fragments.
1623 	 *
1624 	 * XXX This last is a lie.  A cleaned fragment can coexist with
1625 	 * XXX a later indirect block.	This will continue to be
1626 	 * XXX true until lfs_markv is fixed to do everything with
1627 	 * XXX fake blocks (including fake inodes and fake indirect blocks).
1628 	 */
1629 	sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
1630 		fs->lfs_bmask) + 1;
1631 
1632 	/*
1633 	 * Assign disk addresses, and update references to the logical
1634 	 * block and the segment usage information.
1635 	 */
1636 	for (i = nblocks; i--; ++sp->start_bpp) {
1637 		sbp = *sp->start_bpp;
1638 		lbn = *sp->start_lbp;
1639 		KASSERT(sbp->b_lblkno == lbn);
1640 
1641 		sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1642 
1643 		/*
1644 		 * If we write a frag in the wrong place, the cleaner won't
1645 		 * be able to correctly identify its size later, and the
1646 		 * segment will be uncleanable.	 (Even worse, it will assume
1647 		 * that the indirect block that actually ends the list
1648 		 * is of a smaller size!)
1649 		 */
1650 		if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
1651 			panic("lfs_updatemeta: fragment is not last block");
1652 
1653 		/*
1654 		 * For each subblock in this possibly oversized block,
1655 		 * update its address on disk.
1656 		 */
1657 		KASSERT(lbn >= 0 || sbp->b_bcount == fs->lfs_bsize);
1658 		KASSERT(vp == sbp->b_vp);
1659 		for (bytesleft = sbp->b_bcount; bytesleft > 0;
1660 		     bytesleft -= fs->lfs_bsize) {
1661 			size = MIN(bytesleft, fs->lfs_bsize);
1662 			bb = fragstofsb(fs, numfrags(fs, size));
1663 			lbn = *sp->start_lbp++;
1664 			lfs_update_single(fs, sp, sp->vp, lbn, fs->lfs_offset,
1665 			    size);
1666 			fs->lfs_offset += bb;
1667 		}
1668 
1669 	}
1670 
1671 	/* This inode has been modified */
1672 	LFS_SET_UINO(VTOI(vp), IN_MODIFIED);
1673 }
1674 
1675 /*
1676  * Move lfs_offset to a segment earlier than sn.
1677  */
1678 int
1679 lfs_rewind(struct lfs *fs, int newsn)
1680 {
1681 	int sn, osn, isdirty;
1682 	struct buf *bp;
1683 	SEGUSE *sup;
1684 
1685 	ASSERT_SEGLOCK(fs);
1686 
1687 	osn = dtosn(fs, fs->lfs_offset);
1688 	if (osn < newsn)
1689 		return 0;
1690 
1691 	/* lfs_avail eats the remaining space in this segment */
1692 	fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset - fs->lfs_curseg);
1693 
1694 	/* Find a low-numbered segment */
1695 	for (sn = 0; sn < fs->lfs_nseg; ++sn) {
1696 		LFS_SEGENTRY(sup, fs, sn, bp);
1697 		isdirty = sup->su_flags & SEGUSE_DIRTY;
1698 		brelse(bp, 0);
1699 
1700 		if (!isdirty)
1701 			break;
1702 	}
1703 	if (sn == fs->lfs_nseg)
1704 		panic("lfs_rewind: no clean segments");
1705 	if (newsn >= 0 && sn >= newsn)
1706 		return ENOENT;
1707 	fs->lfs_nextseg = sn;
1708 	lfs_newseg(fs);
1709 	fs->lfs_offset = fs->lfs_curseg;
1710 
1711 	return 0;
1712 }
1713 
1714 /*
1715  * Start a new partial segment.
1716  *
1717  * Return 1 when we entered to a new segment.
1718  * Otherwise, return 0.
1719  */
1720 int
1721 lfs_initseg(struct lfs *fs)
1722 {
1723 	struct segment *sp = fs->lfs_sp;
1724 	SEGSUM *ssp;
1725 	struct buf *sbp;	/* buffer for SEGSUM */
1726 	int repeat = 0;		/* return value */
1727 
1728 	ASSERT_SEGLOCK(fs);
1729 	/* Advance to the next segment. */
1730 	if (!LFS_PARTIAL_FITS(fs)) {
1731 		SEGUSE *sup;
1732 		struct buf *bp;
1733 
1734 		/* lfs_avail eats the remaining space */
1735 		fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1736 						   fs->lfs_curseg);
1737 		/* Wake up any cleaning procs waiting on this file system. */
1738 		lfs_wakeup_cleaner(fs);
1739 		lfs_newseg(fs);
1740 		repeat = 1;
1741 		fs->lfs_offset = fs->lfs_curseg;
1742 
1743 		sp->seg_number = dtosn(fs, fs->lfs_curseg);
1744 		sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1745 
1746 		/*
1747 		 * If the segment contains a superblock, update the offset
1748 		 * and summary address to skip over it.
1749 		 */
1750 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1751 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1752 			fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1753 			sp->seg_bytes_left -= LFS_SBPAD;
1754 		}
1755 		brelse(bp, 0);
1756 		/* Segment zero could also contain the labelpad */
1757 		if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1758 		    fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1759 			fs->lfs_offset +=
1760 			    btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1761 			sp->seg_bytes_left -=
1762 			    LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1763 		}
1764 	} else {
1765 		sp->seg_number = dtosn(fs, fs->lfs_curseg);
1766 		sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1767 				      (fs->lfs_offset - fs->lfs_curseg));
1768 	}
1769 	fs->lfs_lastpseg = fs->lfs_offset;
1770 
1771 	/* Record first address of this partial segment */
1772 	if (sp->seg_flags & SEGM_CLEAN) {
1773 		fs->lfs_cleanint[fs->lfs_cleanind] = fs->lfs_offset;
1774 		if (++fs->lfs_cleanind >= LFS_MAX_CLEANIND) {
1775 			/* "1" is the artificial inc in lfs_seglock */
1776 			mutex_enter(&lfs_lock);
1777 			while (fs->lfs_iocount > 1) {
1778 				mtsleep(&fs->lfs_iocount, PRIBIO + 1,
1779 				    "lfs_initseg", 0, &lfs_lock);
1780 			}
1781 			mutex_exit(&lfs_lock);
1782 			fs->lfs_cleanind = 0;
1783 		}
1784 	}
1785 
1786 	sp->fs = fs;
1787 	sp->ibp = NULL;
1788 	sp->idp = NULL;
1789 	sp->ninodes = 0;
1790 	sp->ndupino = 0;
1791 
1792 	sp->cbpp = sp->bpp;
1793 
1794 	/* Get a new buffer for SEGSUM */
1795 	sbp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1796 	    fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize, LFS_NB_SUMMARY);
1797 
1798 	/* ... and enter it into the buffer list. */
1799 	*sp->cbpp = sbp;
1800 	sp->cbpp++;
1801 	fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1802 
1803 	sp->start_bpp = sp->cbpp;
1804 
1805 	/* Set point to SEGSUM, initialize it. */
1806 	ssp = sp->segsum = sbp->b_data;
1807 	memset(ssp, 0, fs->lfs_sumsize);
1808 	ssp->ss_next = fs->lfs_nextseg;
1809 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
1810 	ssp->ss_magic = SS_MAGIC;
1811 
1812 	/* Set pointer to first FINFO, initialize it. */
1813 	sp->fip = (struct finfo *)((char *)sp->segsum + SEGSUM_SIZE(fs));
1814 	sp->fip->fi_nblocks = 0;
1815 	sp->start_lbp = &sp->fip->fi_blocks[0];
1816 	sp->fip->fi_lastlength = 0;
1817 
1818 	sp->seg_bytes_left -= fs->lfs_sumsize;
1819 	sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1820 
1821 	return (repeat);
1822 }
1823 
1824 /*
1825  * Remove SEGUSE_INVAL from all segments.
1826  */
1827 void
1828 lfs_unset_inval_all(struct lfs *fs)
1829 {
1830 	SEGUSE *sup;
1831 	struct buf *bp;
1832 	int i;
1833 
1834 	for (i = 0; i < fs->lfs_nseg; i++) {
1835 		LFS_SEGENTRY(sup, fs, i, bp);
1836 		if (sup->su_flags & SEGUSE_INVAL) {
1837 			sup->su_flags &= ~SEGUSE_INVAL;
1838 			LFS_WRITESEGENTRY(sup, fs, i, bp);
1839 		} else
1840 			brelse(bp, 0);
1841 	}
1842 }
1843 
1844 /*
1845  * Return the next segment to write.
1846  */
1847 void
1848 lfs_newseg(struct lfs *fs)
1849 {
1850 	CLEANERINFO *cip;
1851 	SEGUSE *sup;
1852 	struct buf *bp;
1853 	int curseg, isdirty, sn, skip_inval;
1854 
1855 	ASSERT_SEGLOCK(fs);
1856 
1857 	/* Honor LFCNWRAPSTOP */
1858 	mutex_enter(&lfs_lock);
1859 	while (fs->lfs_nextseg < fs->lfs_curseg && fs->lfs_nowrap) {
1860 		if (fs->lfs_wrappass) {
1861 			log(LOG_NOTICE, "%s: wrappass=%d\n",
1862 				fs->lfs_fsmnt, fs->lfs_wrappass);
1863 			fs->lfs_wrappass = 0;
1864 			break;
1865 		}
1866 		fs->lfs_wrapstatus = LFS_WRAP_WAITING;
1867 		wakeup(&fs->lfs_nowrap);
1868 		log(LOG_NOTICE, "%s: waiting at log wrap\n", fs->lfs_fsmnt);
1869 		mtsleep(&fs->lfs_wrappass, PVFS, "newseg", 10 * hz,
1870 			&lfs_lock);
1871 	}
1872 	fs->lfs_wrapstatus = LFS_WRAP_GOING;
1873 	mutex_exit(&lfs_lock);
1874 
1875 	LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1876 	DLOG((DLOG_SU, "lfs_newseg: seg %d := 0 in newseg\n",
1877 	      dtosn(fs, fs->lfs_nextseg)));
1878 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1879 	sup->su_nbytes = 0;
1880 	sup->su_nsums = 0;
1881 	sup->su_ninos = 0;
1882 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1883 
1884 	LFS_CLEANERINFO(cip, fs, bp);
1885 	--cip->clean;
1886 	++cip->dirty;
1887 	fs->lfs_nclean = cip->clean;
1888 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1889 
1890 	fs->lfs_lastseg = fs->lfs_curseg;
1891 	fs->lfs_curseg = fs->lfs_nextseg;
1892 	skip_inval = 1;
1893 	for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1894 		sn = (sn + 1) % fs->lfs_nseg;
1895 
1896 		if (sn == curseg) {
1897 			if (skip_inval)
1898 				skip_inval = 0;
1899 			else
1900 				panic("lfs_nextseg: no clean segments");
1901 		}
1902 		LFS_SEGENTRY(sup, fs, sn, bp);
1903 		isdirty = sup->su_flags & (SEGUSE_DIRTY | (skip_inval ? SEGUSE_INVAL : 0));
1904 		/* Check SEGUSE_EMPTY as we go along */
1905 		if (isdirty && sup->su_nbytes == 0 &&
1906 		    !(sup->su_flags & SEGUSE_EMPTY))
1907 			LFS_WRITESEGENTRY(sup, fs, sn, bp);
1908 		else
1909 			brelse(bp, 0);
1910 
1911 		if (!isdirty)
1912 			break;
1913 	}
1914 	if (skip_inval == 0)
1915 		lfs_unset_inval_all(fs);
1916 
1917 	++fs->lfs_nactive;
1918 	fs->lfs_nextseg = sntod(fs, sn);
1919 	if (lfs_dostats) {
1920 		++lfs_stats.segsused;
1921 	}
1922 }
1923 
1924 static struct buf *
1925 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr,
1926     int n)
1927 {
1928 	struct lfs_cluster *cl;
1929 	struct buf **bpp, *bp;
1930 
1931 	ASSERT_SEGLOCK(fs);
1932 	cl = (struct lfs_cluster *)pool_get(&fs->lfs_clpool, PR_WAITOK);
1933 	bpp = (struct buf **)pool_get(&fs->lfs_bpppool, PR_WAITOK);
1934 	memset(cl, 0, sizeof(*cl));
1935 	cl->fs = fs;
1936 	cl->bpp = bpp;
1937 	cl->bufcount = 0;
1938 	cl->bufsize = 0;
1939 
1940 	/* If this segment is being written synchronously, note that */
1941 	if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
1942 		cl->flags |= LFS_CL_SYNC;
1943 		cl->seg = fs->lfs_sp;
1944 		++cl->seg->seg_iocount;
1945 	}
1946 
1947 	/* Get an empty buffer header, or maybe one with something on it */
1948 	bp = getiobuf(vp, true);
1949 	bp->b_dev = NODEV;
1950 	bp->b_blkno = bp->b_lblkno = addr;
1951 	bp->b_iodone = lfs_cluster_callback;
1952 	bp->b_private = cl;
1953 
1954 	return bp;
1955 }
1956 
1957 int
1958 lfs_writeseg(struct lfs *fs, struct segment *sp)
1959 {
1960 	struct buf **bpp, *bp, *cbp, *newbp, *unbusybp;
1961 	SEGUSE *sup;
1962 	SEGSUM *ssp;
1963 	int i;
1964 	int do_again, nblocks, byteoffset;
1965 	size_t el_size;
1966 	struct lfs_cluster *cl;
1967 	u_short ninos;
1968 	struct vnode *devvp;
1969 	char *p = NULL;
1970 	struct vnode *vp;
1971 	int32_t *daddrp;	/* XXX ondisk32 */
1972 	int changed;
1973 	u_int32_t sum;
1974 #ifdef DEBUG
1975 	FINFO *fip;
1976 	int findex;
1977 #endif
1978 
1979 	ASSERT_SEGLOCK(fs);
1980 
1981 	ssp = (SEGSUM *)sp->segsum;
1982 
1983 	/*
1984 	 * If there are no buffers other than the segment summary to write,
1985 	 * don't do anything.  If we are the end of a dirop sequence, however,
1986 	 * write the empty segment summary anyway, to help out the
1987 	 * roll-forward agent.
1988 	 */
1989 	if ((nblocks = sp->cbpp - sp->bpp) == 1) {
1990 		if ((ssp->ss_flags & (SS_DIROP | SS_CONT)) != SS_DIROP)
1991 			return 0;
1992 	}
1993 
1994 	/* Note if partial segment is being written by the cleaner */
1995 	if (sp->seg_flags & SEGM_CLEAN)
1996 		ssp->ss_flags |= SS_CLEAN;
1997 
1998 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1999 
2000 	/* Update the segment usage information. */
2001 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
2002 
2003 	/* Loop through all blocks, except the segment summary. */
2004 	for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
2005 		if ((*bpp)->b_vp != devvp) {
2006 			sup->su_nbytes += (*bpp)->b_bcount;
2007 			DLOG((DLOG_SU, "seg %" PRIu32 " += %ld for ino %d"
2008 			      " lbn %" PRId64 " db 0x%" PRIx64 "\n",
2009 			      sp->seg_number, (*bpp)->b_bcount,
2010 			      VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno,
2011 			      (*bpp)->b_blkno));
2012 		}
2013 	}
2014 
2015 #ifdef DEBUG
2016 	/* Check for zero-length and zero-version FINFO entries. */
2017 	fip = (struct finfo *)((char *)ssp + SEGSUM_SIZE(fs));
2018 	for (findex = 0; findex < ssp->ss_nfinfo; findex++) {
2019 		KDASSERT(fip->fi_nblocks > 0);
2020 		KDASSERT(fip->fi_version > 0);
2021 		fip = (FINFO *)((char *)fip + FINFOSIZE +
2022 			sizeof(int32_t) * fip->fi_nblocks);
2023 	}
2024 #endif /* DEBUG */
2025 
2026 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
2027 	DLOG((DLOG_SU, "seg %d += %d for %d inodes\n",
2028 	      sp->seg_number, ssp->ss_ninos * sizeof (struct ufs1_dinode),
2029 	      ssp->ss_ninos));
2030 	sup->su_nbytes += ssp->ss_ninos * sizeof (struct ufs1_dinode);
2031 	/* sup->su_nbytes += fs->lfs_sumsize; */
2032 	if (fs->lfs_version == 1)
2033 		sup->su_olastmod = time_second;
2034 	else
2035 		sup->su_lastmod = time_second;
2036 	sup->su_ninos += ninos;
2037 	++sup->su_nsums;
2038 	fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
2039 
2040 	do_again = !(bp->b_flags & B_GATHERED);
2041 	LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
2042 
2043 	/*
2044 	 * Mark blocks B_BUSY, to prevent then from being changed between
2045 	 * the checksum computation and the actual write.
2046 	 *
2047 	 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
2048 	 * there are any, replace them with copies that have UNASSIGNED
2049 	 * instead.
2050 	 */
2051 	mutex_enter(&bufcache_lock);
2052 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
2053 		++bpp;
2054 		bp = *bpp;
2055 		if (bp->b_iodone != NULL) {	 /* UBC or malloced buffer */
2056 			bp->b_cflags |= BC_BUSY;
2057 			continue;
2058 		}
2059 
2060 		while (bp->b_cflags & BC_BUSY) {
2061 			DLOG((DLOG_SEG, "lfs_writeseg: avoiding potential"
2062 			      " data summary corruption for ino %d, lbn %"
2063 			      PRId64 "\n",
2064 			      VTOI(bp->b_vp)->i_number, bp->b_lblkno));
2065 			bp->b_cflags |= BC_WANTED;
2066 			cv_wait(&bp->b_busy, &bufcache_lock);
2067 		}
2068 		bp->b_cflags |= BC_BUSY;
2069 		mutex_exit(&bufcache_lock);
2070 		unbusybp = NULL;
2071 
2072 		/*
2073 		 * Check and replace indirect block UNWRITTEN bogosity.
2074 		 * XXX See comment in lfs_writefile.
2075 		 */
2076 		if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
2077 		   VTOI(bp->b_vp)->i_ffs1_blocks !=
2078 		   VTOI(bp->b_vp)->i_lfs_effnblks) {
2079 			DLOG((DLOG_VNODE, "lfs_writeseg: cleansing ino %d (%d != %d)\n",
2080 			      VTOI(bp->b_vp)->i_number,
2081 			      VTOI(bp->b_vp)->i_lfs_effnblks,
2082 			      VTOI(bp->b_vp)->i_ffs1_blocks));
2083 			/* Make a copy we'll make changes to */
2084 			newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
2085 					   bp->b_bcount, LFS_NB_IBLOCK);
2086 			newbp->b_blkno = bp->b_blkno;
2087 			memcpy(newbp->b_data, bp->b_data,
2088 			       newbp->b_bcount);
2089 
2090 			changed = 0;
2091 			/* XXX ondisk32 */
2092 			for (daddrp = (int32_t *)(newbp->b_data);
2093 			     daddrp < (int32_t *)((char *)newbp->b_data +
2094 						  newbp->b_bcount); daddrp++) {
2095 				if (*daddrp == UNWRITTEN) {
2096 					++changed;
2097 					*daddrp = 0;
2098 				}
2099 			}
2100 			/*
2101 			 * Get rid of the old buffer.  Don't mark it clean,
2102 			 * though, if it still has dirty data on it.
2103 			 */
2104 			if (changed) {
2105 				DLOG((DLOG_SEG, "lfs_writeseg: replacing UNWRITTEN(%d):"
2106 				      " bp = %p newbp = %p\n", changed, bp,
2107 				      newbp));
2108 				*bpp = newbp;
2109 				bp->b_flags &= ~B_GATHERED;
2110 				bp->b_error = 0;
2111 				if (bp->b_iodone != NULL) {
2112 					DLOG((DLOG_SEG, "lfs_writeseg: "
2113 					      "indir bp should not be B_CALL\n"));
2114 					biodone(bp);
2115 					bp = NULL;
2116 				} else {
2117 					/* Still on free list, leave it there */
2118 					unbusybp = bp;
2119 					/*
2120 					 * We have to re-decrement lfs_avail
2121 					 * since this block is going to come
2122 					 * back around to us in the next
2123 					 * segment.
2124 					 */
2125 					fs->lfs_avail -=
2126 					    btofsb(fs, bp->b_bcount);
2127 				}
2128 			} else {
2129 				lfs_freebuf(fs, newbp);
2130 			}
2131 		}
2132 		mutex_enter(&bufcache_lock);
2133 		if (unbusybp != NULL) {
2134 			unbusybp->b_cflags &= ~BC_BUSY;
2135 			if (unbusybp->b_cflags & BC_WANTED)
2136 				cv_broadcast(&bp->b_busy);
2137 		}
2138 	}
2139 	mutex_exit(&bufcache_lock);
2140 
2141 	/*
2142 	 * Compute checksum across data and then across summary; the first
2143 	 * block (the summary block) is skipped.  Set the create time here
2144 	 * so that it's guaranteed to be later than the inode mod times.
2145 	 */
2146 	sum = 0;
2147 	if (fs->lfs_version == 1)
2148 		el_size = sizeof(u_long);
2149 	else
2150 		el_size = sizeof(u_int32_t);
2151 	for (bpp = sp->bpp, i = nblocks - 1; i--; ) {
2152 		++bpp;
2153 		/* Loop through gop_write cluster blocks */
2154 		for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
2155 		     byteoffset += fs->lfs_bsize) {
2156 #ifdef LFS_USE_B_INVAL
2157 			if ((*bpp)->b_cflags & BC_INVAL) != 0 &&
2158 			    (*bpp)->b_iodone != NULL) {
2159 				if (copyin((void *)(*bpp)->b_saveaddr +
2160 					   byteoffset, dp, el_size)) {
2161 					panic("lfs_writeseg: copyin failed [1]:"
2162 						" ino %d blk %" PRId64,
2163 						VTOI((*bpp)->b_vp)->i_number,
2164 						(*bpp)->b_lblkno);
2165 				}
2166 			} else
2167 #endif /* LFS_USE_B_INVAL */
2168 			{
2169 				sum = lfs_cksum_part((char *)
2170 				    (*bpp)->b_data + byteoffset, el_size, sum);
2171 			}
2172 		}
2173 	}
2174 	if (fs->lfs_version == 1)
2175 		ssp->ss_ocreate = time_second;
2176 	else {
2177 		ssp->ss_create = time_second;
2178 		ssp->ss_serial = ++fs->lfs_serial;
2179 		ssp->ss_ident  = fs->lfs_ident;
2180 	}
2181 	ssp->ss_datasum = lfs_cksum_fold(sum);
2182 	ssp->ss_sumsum = cksum(&ssp->ss_datasum,
2183 	    fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
2184 
2185 	mutex_enter(&lfs_lock);
2186 	fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
2187 			  btofsb(fs, fs->lfs_sumsize));
2188 	fs->lfs_dmeta += (btofsb(fs, ninos * fs->lfs_ibsize) +
2189 			  btofsb(fs, fs->lfs_sumsize));
2190 	mutex_exit(&lfs_lock);
2191 
2192 	/*
2193 	 * When we simply write the blocks we lose a rotation for every block
2194 	 * written.  To avoid this problem, we cluster the buffers into a
2195 	 * chunk and write the chunk.  MAXPHYS is the largest size I/O
2196 	 * devices can handle, use that for the size of the chunks.
2197 	 *
2198 	 * Blocks that are already clusters (from GOP_WRITE), however, we
2199 	 * don't bother to copy into other clusters.
2200 	 */
2201 
2202 #define CHUNKSIZE MAXPHYS
2203 
2204 	if (devvp == NULL)
2205 		panic("devvp is NULL");
2206 	for (bpp = sp->bpp, i = nblocks; i;) {
2207 		cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
2208 		cl = cbp->b_private;
2209 
2210 		cbp->b_flags |= B_ASYNC;
2211 		cbp->b_cflags |= BC_BUSY;
2212 		cbp->b_bcount = 0;
2213 
2214 #if defined(DEBUG) && defined(DIAGNOSTIC)
2215 		if (bpp - sp->bpp > (fs->lfs_sumsize - SEGSUM_SIZE(fs))
2216 		    / sizeof(int32_t)) {
2217 			panic("lfs_writeseg: real bpp overwrite");
2218 		}
2219 		if (bpp - sp->bpp > segsize(fs) / fs->lfs_fsize) {
2220 			panic("lfs_writeseg: theoretical bpp overwrite");
2221 		}
2222 #endif
2223 
2224 		/*
2225 		 * Construct the cluster.
2226 		 */
2227 		mutex_enter(&lfs_lock);
2228 		++fs->lfs_iocount;
2229 		mutex_exit(&lfs_lock);
2230 		while (i && cbp->b_bcount < CHUNKSIZE) {
2231 			bp = *bpp;
2232 
2233 			if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
2234 				break;
2235 			if (cbp->b_bcount > 0 && !(cl->flags & LFS_CL_MALLOC))
2236 				break;
2237 
2238 			/* Clusters from GOP_WRITE are expedited */
2239 			if (bp->b_bcount > fs->lfs_bsize) {
2240 				if (cbp->b_bcount > 0)
2241 					/* Put in its own buffer */
2242 					break;
2243 				else {
2244 					cbp->b_data = bp->b_data;
2245 				}
2246 			} else if (cbp->b_bcount == 0) {
2247 				p = cbp->b_data = lfs_malloc(fs, CHUNKSIZE,
2248 							     LFS_NB_CLUSTER);
2249 				cl->flags |= LFS_CL_MALLOC;
2250 			}
2251 #ifdef DIAGNOSTIC
2252 			if (dtosn(fs, dbtofsb(fs, bp->b_blkno +
2253 					      btodb(bp->b_bcount - 1))) !=
2254 			    sp->seg_number) {
2255 				printf("blk size %d daddr %" PRIx64
2256 				    " not in seg %d\n",
2257 				    bp->b_bcount, bp->b_blkno,
2258 				    sp->seg_number);
2259 				panic("segment overwrite");
2260 			}
2261 #endif
2262 
2263 #ifdef LFS_USE_B_INVAL
2264 			/*
2265 			 * Fake buffers from the cleaner are marked as B_INVAL.
2266 			 * We need to copy the data from user space rather than
2267 			 * from the buffer indicated.
2268 			 * XXX == what do I do on an error?
2269 			 */
2270 			if ((bp->b_cflags & BC_INVAL) != 0 &&
2271 			    bp->b_iodone != NULL) {
2272 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
2273 					panic("lfs_writeseg: "
2274 					    "copyin failed [2]");
2275 			} else
2276 #endif /* LFS_USE_B_INVAL */
2277 			if (cl->flags & LFS_CL_MALLOC) {
2278 				/* copy data into our cluster. */
2279 				memcpy(p, bp->b_data, bp->b_bcount);
2280 				p += bp->b_bcount;
2281 			}
2282 
2283 			cbp->b_bcount += bp->b_bcount;
2284 			cl->bufsize += bp->b_bcount;
2285 
2286 			bp->b_flags &= ~B_READ;
2287 			bp->b_error = 0;
2288 			cl->bpp[cl->bufcount++] = bp;
2289 
2290 			vp = bp->b_vp;
2291 			mutex_enter(&bufcache_lock);
2292 			mutex_enter(&vp->v_interlock);
2293 			bp->b_oflags &= ~(BO_DELWRI | BO_DONE);
2294 			reassignbuf(bp, vp);
2295 			vp->v_numoutput++;
2296 			mutex_exit(&vp->v_interlock);
2297 			mutex_exit(&bufcache_lock);
2298 
2299 			bpp++;
2300 			i--;
2301 		}
2302 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
2303 			BIO_SETPRIO(cbp, BPRIO_TIMECRITICAL);
2304 		else
2305 			BIO_SETPRIO(cbp, BPRIO_TIMELIMITED);
2306 		mutex_enter(&devvp->v_interlock);
2307 		devvp->v_numoutput++;
2308 		mutex_exit(&devvp->v_interlock);
2309 		VOP_STRATEGY(devvp, cbp);
2310 		curlwp->l_ru.ru_oublock++;
2311 	}
2312 
2313 	if (lfs_dostats) {
2314 		++lfs_stats.psegwrites;
2315 		lfs_stats.blocktot += nblocks - 1;
2316 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
2317 			++lfs_stats.psyncwrites;
2318 		if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
2319 			++lfs_stats.pcleanwrites;
2320 			lfs_stats.cleanblocks += nblocks - 1;
2321 		}
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 	struct vnode *devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2332 	int s;
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 	mutex_enter(&lfs_lock);
2344 	s = splbio();
2345 	while (fs->lfs_sbactive) {
2346 		mtsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0,
2347 			&lfs_lock);
2348 	}
2349 	fs->lfs_sbactive = daddr;
2350 	splx(s);
2351 	mutex_exit(&lfs_lock);
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((char *)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_cflags |= BC_BUSY;
2367 	bp->b_flags = (bp->b_flags & ~B_READ) | B_ASYNC;
2368 	bp->b_oflags &= ~(BO_DONE | BO_DELWRI);
2369 	bp->b_error = 0;
2370 	bp->b_iodone = lfs_supercallback;
2371 
2372 	if (fs->lfs_sp != NULL && fs->lfs_sp->seg_flags & SEGM_SYNC)
2373 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
2374 	else
2375 		BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
2376 	curlwp->l_ru.ru_oublock++;
2377 
2378 	mutex_enter(&devvp->v_interlock);
2379 	devvp->v_numoutput++;
2380 	mutex_exit(&devvp->v_interlock);
2381 
2382 	mutex_enter(&lfs_lock);
2383 	++fs->lfs_iocount;
2384 	mutex_exit(&lfs_lock);
2385 	VOP_STRATEGY(devvp, bp);
2386 }
2387 
2388 /*
2389  * Logical block number match routines used when traversing the dirty block
2390  * chain.
2391  */
2392 int
2393 lfs_match_fake(struct lfs *fs, struct buf *bp)
2394 {
2395 
2396 	ASSERT_SEGLOCK(fs);
2397 	return LFS_IS_MALLOC_BUF(bp);
2398 }
2399 
2400 #if 0
2401 int
2402 lfs_match_real(struct lfs *fs, struct buf *bp)
2403 {
2404 
2405 	ASSERT_SEGLOCK(fs);
2406 	return (lfs_match_data(fs, bp) && !lfs_match_fake(fs, bp));
2407 }
2408 #endif
2409 
2410 int
2411 lfs_match_data(struct lfs *fs, struct buf *bp)
2412 {
2413 
2414 	ASSERT_SEGLOCK(fs);
2415 	return (bp->b_lblkno >= 0);
2416 }
2417 
2418 int
2419 lfs_match_indir(struct lfs *fs, struct buf *bp)
2420 {
2421 	daddr_t lbn;
2422 
2423 	ASSERT_SEGLOCK(fs);
2424 	lbn = bp->b_lblkno;
2425 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
2426 }
2427 
2428 int
2429 lfs_match_dindir(struct lfs *fs, struct buf *bp)
2430 {
2431 	daddr_t lbn;
2432 
2433 	ASSERT_SEGLOCK(fs);
2434 	lbn = bp->b_lblkno;
2435 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
2436 }
2437 
2438 int
2439 lfs_match_tindir(struct lfs *fs, struct buf *bp)
2440 {
2441 	daddr_t lbn;
2442 
2443 	ASSERT_SEGLOCK(fs);
2444 	lbn = bp->b_lblkno;
2445 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
2446 }
2447 
2448 static void
2449 lfs_free_aiodone(struct buf *bp)
2450 {
2451 	struct lfs *fs;
2452 
2453 	KERNEL_LOCK(1, curlwp);
2454 	fs = bp->b_private;
2455 	ASSERT_NO_SEGLOCK(fs);
2456 	lfs_freebuf(fs, bp);
2457 	KERNEL_UNLOCK_LAST(curlwp);
2458 }
2459 
2460 static void
2461 lfs_super_aiodone(struct buf *bp)
2462 {
2463 	struct lfs *fs;
2464 
2465 	KERNEL_LOCK(1, curlwp);
2466 	fs = bp->b_private;
2467 	ASSERT_NO_SEGLOCK(fs);
2468 	mutex_enter(&lfs_lock);
2469 	fs->lfs_sbactive = 0;
2470 	if (--fs->lfs_iocount <= 1)
2471 		wakeup(&fs->lfs_iocount);
2472 	wakeup(&fs->lfs_sbactive);
2473 	mutex_exit(&lfs_lock);
2474 	lfs_freebuf(fs, bp);
2475 	KERNEL_UNLOCK_LAST(curlwp);
2476 }
2477 
2478 static void
2479 lfs_cluster_aiodone(struct buf *bp)
2480 {
2481 	struct lfs_cluster *cl;
2482 	struct lfs *fs;
2483 	struct buf *tbp, *fbp;
2484 	struct vnode *vp, *devvp, *ovp;
2485 	struct inode *ip;
2486 	int error;
2487 
2488 	KERNEL_LOCK(1, curlwp);
2489 
2490 	error = bp->b_error;
2491 	cl = bp->b_private;
2492 	fs = cl->fs;
2493 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2494 	ASSERT_NO_SEGLOCK(fs);
2495 
2496 	/* Put the pages back, and release the buffer */
2497 	while (cl->bufcount--) {
2498 		tbp = cl->bpp[cl->bufcount];
2499 		KASSERT(tbp->b_cflags & BC_BUSY);
2500 		if (error) {
2501 			tbp->b_error = error;
2502 		}
2503 
2504 		/*
2505 		 * We're done with tbp.	 If it has not been re-dirtied since
2506 		 * the cluster was written, free it.  Otherwise, keep it on
2507 		 * the locked list to be written again.
2508 		 */
2509 		vp = tbp->b_vp;
2510 
2511 		tbp->b_flags &= ~B_GATHERED;
2512 
2513 		LFS_BCLEAN_LOG(fs, tbp);
2514 
2515 		mutex_enter(&bufcache_lock);
2516 		if (tbp->b_iodone == NULL) {
2517 			KASSERT(tbp->b_flags & B_LOCKED);
2518 			bremfree(tbp);
2519 			if (vp) {
2520 				mutex_enter(&vp->v_interlock);
2521 				reassignbuf(tbp, vp);
2522 				mutex_exit(&vp->v_interlock);
2523 			}
2524 			tbp->b_flags |= B_ASYNC; /* for biodone */
2525 		}
2526 
2527 		if (((tbp->b_flags | tbp->b_oflags) &
2528 		    (B_LOCKED | BO_DELWRI)) == B_LOCKED)
2529 			LFS_UNLOCK_BUF(tbp);
2530 
2531 		if (tbp->b_oflags & BO_DONE) {
2532 			DLOG((DLOG_SEG, "blk %d biodone already (flags %lx)\n",
2533 				cl->bufcount, (long)tbp->b_flags));
2534 		}
2535 
2536 		if (tbp->b_iodone != NULL && !LFS_IS_MALLOC_BUF(tbp)) {
2537 			/*
2538 			 * A buffer from the page daemon.
2539 			 * We use the same iodone as it does,
2540 			 * so we must manually disassociate its
2541 			 * buffers from the vp.
2542 			 */
2543 			if ((ovp = tbp->b_vp) != NULL) {
2544 				/* This is just silly */
2545 				mutex_enter(&ovp->v_interlock);
2546 				brelvp(tbp);
2547 				mutex_exit(&ovp->v_interlock);
2548 				tbp->b_vp = vp;
2549 				tbp->b_objlock = &vp->v_interlock;
2550 			}
2551 			/* Put it back the way it was */
2552 			tbp->b_flags |= B_ASYNC;
2553 			/* Master buffers have BC_AGE */
2554 			if (tbp->b_private == tbp)
2555 				tbp->b_flags |= BC_AGE;
2556 		}
2557 		mutex_exit(&bufcache_lock);
2558 
2559 		biodone(tbp);
2560 
2561 		/*
2562 		 * If this is the last block for this vnode, but
2563 		 * there are other blocks on its dirty list,
2564 		 * set IN_MODIFIED/IN_CLEANING depending on what
2565 		 * sort of block.  Only do this for our mount point,
2566 		 * not for, e.g., inode blocks that are attached to
2567 		 * the devvp.
2568 		 * XXX KS - Shouldn't we set *both* if both types
2569 		 * of blocks are present (traverse the dirty list?)
2570 		 */
2571 		mutex_enter(&lfs_lock);
2572 		mutex_enter(&vp->v_interlock);
2573 		if (vp != devvp && vp->v_numoutput == 0 &&
2574 		    (fbp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL) {
2575 			ip = VTOI(vp);
2576 			DLOG((DLOG_SEG, "lfs_cluster_aiodone: mark ino %d\n",
2577 			       ip->i_number));
2578 			if (LFS_IS_MALLOC_BUF(fbp))
2579 				LFS_SET_UINO(ip, IN_CLEANING);
2580 			else
2581 				LFS_SET_UINO(ip, IN_MODIFIED);
2582 		}
2583 		cv_broadcast(&vp->v_cv);
2584 		mutex_exit(&vp->v_interlock);
2585 		mutex_exit(&lfs_lock);
2586 	}
2587 
2588 	/* Fix up the cluster buffer, and release it */
2589 	if (cl->flags & LFS_CL_MALLOC)
2590 		lfs_free(fs, bp->b_data, LFS_NB_CLUSTER);
2591 	putiobuf(bp);
2592 
2593 	/* Note i/o done */
2594 	if (cl->flags & LFS_CL_SYNC) {
2595 		if (--cl->seg->seg_iocount == 0)
2596 			wakeup(&cl->seg->seg_iocount);
2597 	}
2598 	mutex_enter(&lfs_lock);
2599 #ifdef DIAGNOSTIC
2600 	if (fs->lfs_iocount == 0)
2601 		panic("lfs_cluster_aiodone: zero iocount");
2602 #endif
2603 	if (--fs->lfs_iocount <= 1)
2604 		wakeup(&fs->lfs_iocount);
2605 	mutex_exit(&lfs_lock);
2606 
2607 	KERNEL_UNLOCK_LAST(curlwp);
2608 
2609 	pool_put(&fs->lfs_bpppool, cl->bpp);
2610 	cl->bpp = NULL;
2611 	pool_put(&fs->lfs_clpool, cl);
2612 }
2613 
2614 static void
2615 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
2616 {
2617 	/* reset b_iodone for when this is a single-buf i/o. */
2618 	bp->b_iodone = aiodone;
2619 
2620 	workqueue_enqueue(uvm.aiodone_queue, &bp->b_work, NULL);
2621 }
2622 
2623 static void
2624 lfs_cluster_callback(struct buf *bp)
2625 {
2626 
2627 	lfs_generic_callback(bp, lfs_cluster_aiodone);
2628 }
2629 
2630 void
2631 lfs_supercallback(struct buf *bp)
2632 {
2633 
2634 	lfs_generic_callback(bp, lfs_super_aiodone);
2635 }
2636 
2637 /*
2638  * The only buffers that are going to hit these functions are the
2639  * segment write blocks, or the segment summaries, or the superblocks.
2640  *
2641  * All of the above are created by lfs_newbuf, and so do not need to be
2642  * released via brelse.
2643  */
2644 void
2645 lfs_callback(struct buf *bp)
2646 {
2647 
2648 	lfs_generic_callback(bp, lfs_free_aiodone);
2649 }
2650 
2651 /*
2652  * Shellsort (diminishing increment sort) from Data Structures and
2653  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
2654  * see also Knuth Vol. 3, page 84.  The increments are selected from
2655  * formula (8), page 95.  Roughly O(N^3/2).
2656  */
2657 /*
2658  * This is our own private copy of shellsort because we want to sort
2659  * two parallel arrays (the array of buffer pointers and the array of
2660  * logical block numbers) simultaneously.  Note that we cast the array
2661  * of logical block numbers to a unsigned in this routine so that the
2662  * negative block numbers (meta data blocks) sort AFTER the data blocks.
2663  */
2664 
2665 void
2666 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb, int size)
2667 {
2668 	static int __rsshell_increments[] = { 4, 1, 0 };
2669 	int incr, *incrp, t1, t2;
2670 	struct buf *bp_temp;
2671 
2672 #ifdef DEBUG
2673 	incr = 0;
2674 	for (t1 = 0; t1 < nmemb; t1++) {
2675 		for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2676 			if (lb_array[incr++] != bp_array[t1]->b_lblkno + t2) {
2677 				/* dump before panic */
2678 				printf("lfs_shellsort: nmemb=%d, size=%d\n",
2679 				    nmemb, size);
2680 				incr = 0;
2681 				for (t1 = 0; t1 < nmemb; t1++) {
2682 					const struct buf *bp = bp_array[t1];
2683 
2684 					printf("bp[%d]: lbn=%" PRIu64 ", size=%"
2685 					    PRIu64 "\n", t1,
2686 					    (uint64_t)bp->b_bcount,
2687 					    (uint64_t)bp->b_lblkno);
2688 					printf("lbns:");
2689 					for (t2 = 0; t2 * size < bp->b_bcount;
2690 					    t2++) {
2691 						printf(" %" PRId32,
2692 						    lb_array[incr++]);
2693 					}
2694 					printf("\n");
2695 				}
2696 				panic("lfs_shellsort: inconsistent input");
2697 			}
2698 		}
2699 	}
2700 #endif
2701 
2702 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
2703 		for (t1 = incr; t1 < nmemb; ++t1)
2704 			for (t2 = t1 - incr; t2 >= 0;)
2705 				if ((u_int32_t)bp_array[t2]->b_lblkno >
2706 				    (u_int32_t)bp_array[t2 + incr]->b_lblkno) {
2707 					bp_temp = bp_array[t2];
2708 					bp_array[t2] = bp_array[t2 + incr];
2709 					bp_array[t2 + incr] = bp_temp;
2710 					t2 -= incr;
2711 				} else
2712 					break;
2713 
2714 	/* Reform the list of logical blocks */
2715 	incr = 0;
2716 	for (t1 = 0; t1 < nmemb; t1++) {
2717 		for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2718 			lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
2719 		}
2720 	}
2721 }
2722 
2723 /*
2724  * Call vget with LK_NOWAIT.  If we are the one who holds VI_XLOCK,
2725  * however, we must press on.  Just fake success in that case.
2726  */
2727 int
2728 lfs_vref(struct vnode *vp)
2729 {
2730 	int error;
2731 	struct lfs *fs;
2732 
2733 	KASSERT(mutex_owned(&vp->v_interlock));
2734 
2735 	fs = VTOI(vp)->i_lfs;
2736 
2737 	ASSERT_MAYBE_SEGLOCK(fs);
2738 
2739 	/*
2740 	 * If we return 1 here during a flush, we risk vinvalbuf() not
2741 	 * being able to flush all of the pages from this vnode, which
2742 	 * will cause it to panic.  So, return 0 if a flush is in progress.
2743 	 */
2744 	error = vget(vp, LK_NOWAIT | LK_INTERLOCK);
2745 	if (error == EBUSY && IS_FLUSHING(VTOI(vp)->i_lfs, vp)) {
2746 		++fs->lfs_flushvp_fakevref;
2747 		return 0;
2748 	}
2749 	return error;
2750 }
2751 
2752 /*
2753  * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
2754  * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
2755  */
2756 void
2757 lfs_vunref(struct vnode *vp)
2758 {
2759 	struct lfs *fs;
2760 
2761 	fs = VTOI(vp)->i_lfs;
2762 	ASSERT_MAYBE_SEGLOCK(fs);
2763 
2764 	/*
2765 	 * Analogous to lfs_vref, if the node is flushing, fake it.
2766 	 */
2767 	if (IS_FLUSHING(fs, vp) && fs->lfs_flushvp_fakevref) {
2768 		--fs->lfs_flushvp_fakevref;
2769 		return;
2770 	}
2771 
2772 	/* does not call inactive */
2773 	mutex_enter(&vp->v_interlock);
2774 	vrelel(vp, VRELEL_NOINACTIVE);
2775 }
2776 
2777 /*
2778  * We use this when we have vnodes that were loaded in solely for cleaning.
2779  * There is no reason to believe that these vnodes will be referenced again
2780  * soon, since the cleaning process is unrelated to normal filesystem
2781  * activity.  Putting cleaned vnodes at the tail of the list has the effect
2782  * of flushing the vnode LRU.  So, put vnodes that were loaded only for
2783  * cleaning at the head of the list, instead.
2784  */
2785 void
2786 lfs_vunref_head(struct vnode *vp)
2787 {
2788 
2789 	ASSERT_SEGLOCK(VTOI(vp)->i_lfs);
2790 
2791 	/* does not call inactive, inserts non-held vnode at head of freelist */
2792 	mutex_enter(&vp->v_interlock);
2793 	vrelel(vp, VRELEL_NOINACTIVE | VRELEL_ONHEAD);
2794 }
2795 
2796 
2797 /*
2798  * Set up an FINFO entry for a new file.  The fip pointer is assumed to
2799  * point at uninitialized space.
2800  */
2801 void
2802 lfs_acquire_finfo(struct lfs *fs, ino_t ino, int vers)
2803 {
2804 	struct segment *sp = fs->lfs_sp;
2805 
2806 	KASSERT(vers > 0);
2807 
2808 	if (sp->seg_bytes_left < fs->lfs_bsize ||
2809 	    sp->sum_bytes_left < sizeof(struct finfo))
2810 		(void) lfs_writeseg(fs, fs->lfs_sp);
2811 
2812 	sp->sum_bytes_left -= FINFOSIZE;
2813 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
2814 	sp->fip->fi_nblocks = 0;
2815 	sp->fip->fi_ino = ino;
2816 	sp->fip->fi_version = vers;
2817 }
2818 
2819 /*
2820  * Release the FINFO entry, either clearing out an unused entry or
2821  * advancing us to the next available entry.
2822  */
2823 void
2824 lfs_release_finfo(struct lfs *fs)
2825 {
2826 	struct segment *sp = fs->lfs_sp;
2827 
2828 	if (sp->fip->fi_nblocks != 0) {
2829 		sp->fip = (FINFO*)((char *)sp->fip + FINFOSIZE +
2830 			sizeof(int32_t) * sp->fip->fi_nblocks);
2831 		sp->start_lbp = &sp->fip->fi_blocks[0];
2832 	} else {
2833 		sp->sum_bytes_left += FINFOSIZE;
2834 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
2835 	}
2836 }
2837