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