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