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