xref: /netbsd-src/sys/ufs/lfs/lfs_segment.c (revision bcc8ec9959e7b01e313d813067bfb43a3ad70551)
1 /*	$NetBSD: lfs_segment.c,v 1.67 2001/01/09 05:05:35 joff Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000 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. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed by the University of
53  *	California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)lfs_segment.c	8.10 (Berkeley) 6/10/95
71  */
72 
73 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str))
74 
75 #if defined(_KERNEL) && !defined(_LKM)
76 #include "opt_ddb.h"
77 #endif
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/namei.h>
82 #include <sys/kernel.h>
83 #include <sys/resourcevar.h>
84 #include <sys/file.h>
85 #include <sys/stat.h>
86 #include <sys/buf.h>
87 #include <sys/proc.h>
88 #include <sys/conf.h>
89 #include <sys/vnode.h>
90 #include <sys/malloc.h>
91 #include <sys/mount.h>
92 
93 #include <miscfs/specfs/specdev.h>
94 #include <miscfs/fifofs/fifo.h>
95 
96 #include <ufs/ufs/quota.h>
97 #include <ufs/ufs/inode.h>
98 #include <ufs/ufs/dir.h>
99 #include <ufs/ufs/ufsmount.h>
100 #include <ufs/ufs/ufs_extern.h>
101 
102 #include <ufs/lfs/lfs.h>
103 #include <ufs/lfs/lfs_extern.h>
104 
105 extern int count_lock_queue __P((void));
106 extern struct simplelock vnode_free_list_slock;		/* XXX */
107 
108 /*
109  * Determine if it's OK to start a partial in this segment, or if we need
110  * to go on to a new segment.
111  */
112 #define	LFS_PARTIAL_FITS(fs) \
113 	((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
114 	1 << (fs)->lfs_fsbtodb)
115 
116 void	 lfs_callback __P((struct buf *));
117 int	 lfs_gather __P((struct lfs *, struct segment *,
118 	     struct vnode *, int (*) __P((struct lfs *, struct buf *))));
119 int	 lfs_gatherblock __P((struct segment *, struct buf *, int *));
120 void	 lfs_iset __P((struct inode *, ufs_daddr_t, time_t));
121 int	 lfs_match_fake __P((struct lfs *, struct buf *));
122 int	 lfs_match_data __P((struct lfs *, struct buf *));
123 int	 lfs_match_dindir __P((struct lfs *, struct buf *));
124 int	 lfs_match_indir __P((struct lfs *, struct buf *));
125 int	 lfs_match_tindir __P((struct lfs *, struct buf *));
126 void	 lfs_newseg __P((struct lfs *));
127 void	 lfs_shellsort __P((struct buf **, ufs_daddr_t *, int));
128 void	 lfs_supercallback __P((struct buf *));
129 void	 lfs_updatemeta __P((struct segment *));
130 int	 lfs_vref __P((struct vnode *));
131 void	 lfs_vunref __P((struct vnode *));
132 void	 lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
133 int	 lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
134 int	 lfs_writeseg __P((struct lfs *, struct segment *));
135 void	 lfs_writesuper __P((struct lfs *, daddr_t));
136 int	 lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
137 	    struct segment *sp, int dirops));
138 
139 int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
140 int	lfs_writeindir = 1;             /* whether to flush indir on non-ckp */
141 int	lfs_clean_vnhead = 0;		/* Allow freeing to head of vn list */
142 int	lfs_dirvcount = 0;		/* # active dirops */
143 
144 /* Statistics Counters */
145 int lfs_dostats = 1;
146 struct lfs_stats lfs_stats;
147 
148 extern int locked_queue_count;
149 extern long locked_queue_bytes;
150 
151 /* op values to lfs_writevnodes */
152 #define	VN_REG	        0
153 #define	VN_DIROP	1
154 #define	VN_EMPTY	2
155 #define VN_CLEAN        3
156 
157 #define LFS_MAX_ACTIVE          10
158 
159 /*
160  * XXX KS - Set modification time on the Ifile, so the cleaner can
161  * read the fs mod time off of it.  We don't set IN_UPDATE here,
162  * since we don't really need this to be flushed to disk (and in any
163  * case that wouldn't happen to the Ifile until we checkpoint).
164  */
165 void
166 lfs_imtime(fs)
167 	struct lfs *fs;
168 {
169 	struct timespec ts;
170 	struct inode *ip;
171 
172 	TIMEVAL_TO_TIMESPEC(&time, &ts);
173 	ip = VTOI(fs->lfs_ivnode);
174 	ip->i_ffs_mtime = ts.tv_sec;
175 	ip->i_ffs_mtimensec = ts.tv_nsec;
176 }
177 
178 /*
179  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
180  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
181  * and getnewbuf().  They both mark the file system busy.  Lfs_vflush()
182  * explicitly marks the file system busy.  So lfs_segwrite is safe.  I think.
183  */
184 
185 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
186 #define IS_FLUSHING(fs,vp)  ((fs)->lfs_flushvp == (vp))
187 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
188 
189 int
190 lfs_vflush(vp)
191 	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, s;
198 
199 	ip = VTOI(vp);
200 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
201 
202 	if(ip->i_flag & IN_CLEANING) {
203 #ifdef DEBUG_LFS
204 		ivndebug(vp,"vflush/in_cleaning");
205 #endif
206 		LFS_CLR_UINO(ip, IN_CLEANING);
207 		LFS_SET_UINO(ip, IN_MODIFIED);
208 
209 		/*
210 		 * Toss any cleaning buffers that have real counterparts
211 		 * to avoid losing new data
212 		 */
213 		s = splbio();
214 		for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
215 			nbp = bp->b_vnbufs.le_next;
216 			if(bp->b_flags & B_CALL) {
217 				for(tbp=vp->v_dirtyblkhd.lh_first; tbp;
218 				    tbp=tnbp)
219 				{
220 					tnbp = tbp->b_vnbufs.le_next;
221 					if(tbp->b_vp == bp->b_vp
222 					   && tbp->b_lblkno == bp->b_lblkno
223 					   && tbp != bp)
224 					{
225 						fs->lfs_avail += btodb(bp->b_bcount);
226 						wakeup(&fs->lfs_avail);
227 						lfs_freebuf(bp);
228 					}
229 				}
230 			}
231 		}
232 		splx(s);
233 	}
234 
235 	/* If the node is being written, wait until that is done */
236 	if(WRITEINPROG(vp)) {
237 #ifdef DEBUG_LFS
238 		ivndebug(vp,"vflush/writeinprog");
239 #endif
240 		tsleep(vp, PRIBIO+1, "lfs_vw", 0);
241 	}
242 
243 	/* Protect against VXLOCK deadlock in vinvalbuf() */
244 	lfs_seglock(fs, SEGM_SYNC);
245 
246 	/* If we're supposed to flush a freed inode, just toss it */
247 	/* XXX - seglock, so these buffers can't be gathered, right? */
248 	if(ip->i_ffs_mode == 0) {
249 		printf("lfs_vflush: ino %d is freed, not flushing\n",
250 			ip->i_number);
251 		s = splbio();
252 		for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
253 			nbp = bp->b_vnbufs.le_next;
254 			if (bp->b_flags & B_DELWRI) { /* XXX always true? */
255 				fs->lfs_avail += btodb(bp->b_bcount);
256 				wakeup(&fs->lfs_avail);
257 			}
258 			/* Copied from lfs_writeseg */
259 			if (bp->b_flags & B_CALL) {
260 				/* if B_CALL, it was created with newbuf */
261 				lfs_freebuf(bp);
262 			} else {
263 				bremfree(bp);
264 				LFS_UNLOCK_BUF(bp);
265 				bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
266                                          B_GATHERED);
267 				bp->b_flags |= B_DONE;
268 				reassignbuf(bp, vp);
269 				brelse(bp);
270 			}
271 		}
272 		splx(s);
273 		LFS_CLR_UINO(ip, IN_CLEANING);
274 		LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
275 		ip->i_flag &= ~IN_ALLMOD;
276 		printf("lfs_vflush: done not flushing ino %d\n",
277 			ip->i_number);
278 		lfs_segunlock(fs);
279 		return 0;
280 	}
281 
282 	SET_FLUSHING(fs,vp);
283 	if (fs->lfs_nactive > LFS_MAX_ACTIVE) {
284 		error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP);
285 		CLR_FLUSHING(fs,vp);
286 		lfs_segunlock(fs);
287 		return error;
288 	}
289 	sp = fs->lfs_sp;
290 
291 	if (vp->v_dirtyblkhd.lh_first == NULL) {
292 		lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
293 	} else if((ip->i_flag & IN_CLEANING) &&
294 		  (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
295 #ifdef DEBUG_LFS
296 		ivndebug(vp,"vflush/clean");
297 #endif
298 		lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
299 	}
300 	else if(lfs_dostats) {
301 		if(vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & IN_ALLMOD))
302 			++lfs_stats.vflush_invoked;
303 #ifdef DEBUG_LFS
304 		ivndebug(vp,"vflush");
305 #endif
306 	}
307 
308 #ifdef DIAGNOSTIC
309 	/* XXX KS This actually can happen right now, though it shouldn't(?) */
310 	if(vp->v_flag & VDIROP) {
311 		printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
312 		/* panic("VDIROP being flushed...this can\'t happen"); */
313 	}
314 	if(vp->v_usecount<0) {
315 		printf("usecount=%d\n",vp->v_usecount);
316 		panic("lfs_vflush: usecount<0");
317 	}
318 #endif
319 
320 	do {
321 		do {
322 			if (vp->v_dirtyblkhd.lh_first != NULL)
323 				lfs_writefile(fs, sp, vp);
324 		} while (lfs_writeinode(fs, sp, ip));
325 	} while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
326 
327 	if(lfs_dostats) {
328 		++lfs_stats.nwrites;
329 		if (sp->seg_flags & SEGM_SYNC)
330 			++lfs_stats.nsync_writes;
331 		if (sp->seg_flags & SEGM_CKP)
332 			++lfs_stats.ncheckpoints;
333 	}
334 	lfs_segunlock(fs);
335 
336 	CLR_FLUSHING(fs,vp);
337 	return (0);
338 }
339 
340 #ifdef DEBUG_LFS_VERBOSE
341 # define vndebug(vp,str) if(VTOI(vp)->i_flag & IN_CLEANING) printf("not writing ino %d because %s (op %d)\n",VTOI(vp)->i_number,(str),op)
342 #else
343 # define vndebug(vp,str)
344 #endif
345 
346 int
347 lfs_writevnodes(fs, mp, sp, op)
348 	struct lfs *fs;
349 	struct mount *mp;
350 	struct segment *sp;
351 	int op;
352 {
353 	struct inode *ip;
354 	struct vnode *vp;
355 	int inodes_written=0, only_cleaning;
356 	int needs_unlock;
357 
358 #ifndef LFS_NO_BACKVP_HACK
359 	/* BEGIN HACK */
360 #define	VN_OFFSET	(((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
361 #define	BACK_VP(VP)	((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
362 #define	BEG_OF_VLIST	((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
363 
364 	/* Find last vnode. */
365  loop:	for (vp = mp->mnt_vnodelist.lh_first;
366 	     vp && vp->v_mntvnodes.le_next != NULL;
367 	     vp = vp->v_mntvnodes.le_next);
368 	for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
369 #else
370 	loop:
371 	for (vp = mp->mnt_vnodelist.lh_first;
372 	     vp != NULL;
373 	     vp = vp->v_mntvnodes.le_next) {
374 #endif
375 		/*
376 		 * If the vnode that we are about to sync is no longer
377 		 * associated with this mount point, start over.
378 		 */
379 		if (vp->v_mount != mp) {
380 			printf("lfs_writevnodes: starting over\n");
381 			goto loop;
382 		}
383 
384 		ip = VTOI(vp);
385 		if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
386 		    (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
387 			vndebug(vp,"dirop");
388 			continue;
389 		}
390 
391 		if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) {
392 			vndebug(vp,"empty");
393 			continue;
394 		}
395 
396 		if (vp->v_type == VNON) {
397 			continue;
398 		}
399 
400 		if(op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
401 		   && vp != fs->lfs_flushvp
402 		   && !(ip->i_flag & IN_CLEANING)) {
403 			vndebug(vp,"cleaning");
404 			continue;
405 		}
406 
407 		if (lfs_vref(vp)) {
408 			vndebug(vp,"vref");
409 			continue;
410 		}
411 
412 		needs_unlock = 0;
413 		if (VOP_ISLOCKED(vp)) {
414 			if (vp != fs->lfs_ivnode &&
415 			    vp->v_lock.lk_lockholder != curproc->p_pid) {
416 #ifdef DEBUG_LFS
417 				printf("lfs_writevnodes: not writing ino %d,"
418 				       " locked by pid %d\n",
419 				       VTOI(vp)->i_number,
420 				       vp->v_lock.lk_lockholder);
421 #endif
422 				lfs_vunref(vp);
423 				continue;
424 			}
425 		} else if (vp != fs->lfs_ivnode) {
426 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
427 			needs_unlock = 1;
428 		}
429 
430 		only_cleaning = 0;
431 		/*
432 		 * Write the inode/file if dirty and it's not the IFILE.
433 		 */
434 		if ((ip->i_flag & IN_ALLMOD) ||
435 		     (vp->v_dirtyblkhd.lh_first != NULL))
436 		{
437 			only_cleaning = ((ip->i_flag & IN_ALLMOD)==IN_CLEANING);
438 
439 			if(ip->i_number != LFS_IFILE_INUM
440 			   && vp->v_dirtyblkhd.lh_first != NULL)
441 			{
442 				lfs_writefile(fs, sp, vp);
443 			}
444 			if(vp->v_dirtyblkhd.lh_first != NULL) {
445 				if(WRITEINPROG(vp)) {
446 #ifdef DEBUG_LFS
447 					ivndebug(vp,"writevnodes/write2");
448 #endif
449 				} else if(!(ip->i_flag & IN_ALLMOD)) {
450 #ifdef DEBUG_LFS
451 					printf("<%d>",ip->i_number);
452 #endif
453 					LFS_SET_UINO(ip, IN_MODIFIED);
454 				}
455 			}
456 			(void) lfs_writeinode(fs, sp, ip);
457 			inodes_written++;
458 		}
459 
460 		if (needs_unlock)
461 			VOP_UNLOCK(vp, 0);
462 
463 		if (lfs_clean_vnhead && only_cleaning)
464 			lfs_vunref_head(vp);
465 		else
466 			lfs_vunref(vp);
467 	}
468 	return inodes_written;
469 }
470 
471 int
472 lfs_segwrite(mp, flags)
473 	struct mount *mp;
474 	int flags;			/* Do a checkpoint. */
475 {
476 	struct buf *bp;
477 	struct inode *ip;
478 	struct lfs *fs;
479 	struct segment *sp;
480 	struct vnode *vp;
481 	SEGUSE *segusep;
482 	ufs_daddr_t ibno;
483 	int do_ckp, did_ckp, error, i;
484 	int writer_set = 0;
485 	int dirty;
486 
487 	fs = VFSTOUFS(mp)->um_lfs;
488 
489 	if (fs->lfs_ronly)
490 		return EROFS;
491 
492 	lfs_imtime(fs);
493 
494 	/* printf("lfs_segwrite: ifile flags are 0x%lx\n",
495 	       (long)(VTOI(fs->lfs_ivnode)->i_flag)); */
496 
497 #if 0
498 	/*
499 	 * If we are not the cleaner, and there is no space available,
500 	 * wait until cleaner writes.
501 	 */
502 	if(!(flags & SEGM_CLEAN) && !(fs->lfs_seglock && fs->lfs_sp &&
503 				      (fs->lfs_sp->seg_flags & SEGM_CLEAN)))
504 	{
505 		while (fs->lfs_avail <= 0) {
506 			LFS_CLEANERINFO(cip, fs, bp);
507 			LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
508 
509 			wakeup(&lfs_allclean_wakeup);
510 			wakeup(&fs->lfs_nextseg);
511 			error = tsleep(&fs->lfs_avail, PRIBIO + 1, "lfs_av2",
512 				       0);
513 			if (error) {
514 				return (error);
515 			}
516 		}
517 	}
518 #endif
519 	/*
520 	 * Allocate a segment structure and enough space to hold pointers to
521 	 * the maximum possible number of buffers which can be described in a
522 	 * single summary block.
523 	 */
524 	do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
525 	lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
526 	sp = fs->lfs_sp;
527 
528 	/*
529 	 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
530 	 * in which case we have to flush *all* buffers off of this vnode.
531 	 * We don't care about other nodes, but write any non-dirop nodes
532 	 * anyway in anticipation of another getnewvnode().
533 	 *
534 	 * If we're cleaning we only write cleaning and ifile blocks, and
535 	 * no dirops, since otherwise we'd risk corruption in a crash.
536 	 */
537 	if(sp->seg_flags & SEGM_CLEAN)
538 		lfs_writevnodes(fs, mp, sp, VN_CLEAN);
539 	else {
540 		lfs_writevnodes(fs, mp, sp, VN_REG);
541 		if(!fs->lfs_dirops || !fs->lfs_flushvp) {
542 			while(fs->lfs_dirops)
543 				if((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
544 						"lfs writer", 0)))
545 				{
546 					free(sp->bpp, M_SEGMENT);
547 					free(sp, M_SEGMENT);
548 					return (error);
549 				}
550 			fs->lfs_writer++;
551 			writer_set=1;
552 			lfs_writevnodes(fs, mp, sp, VN_DIROP);
553 			((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
554 		}
555 	}
556 
557 	/*
558 	 * If we are doing a checkpoint, mark everything since the
559 	 * last checkpoint as no longer ACTIVE.
560 	 */
561 	if (do_ckp) {
562 		for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
563 		     --ibno >= fs->lfs_cleansz; ) {
564 			dirty = 0;
565 			if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
566 
567 				panic("lfs_segwrite: ifile read");
568 			segusep = (SEGUSE *)bp->b_data;
569 			for (i = fs->lfs_sepb; i--; segusep++) {
570 				if (segusep->su_flags & SEGUSE_ACTIVE) {
571 					segusep->su_flags &= ~SEGUSE_ACTIVE;
572 					++dirty;
573 				}
574 			}
575 
576 			/* But the current segment is still ACTIVE */
577 			segusep = (SEGUSE *)bp->b_data;
578 			if (datosn(fs, fs->lfs_curseg) / fs->lfs_sepb ==
579 			    (ibno-fs->lfs_cleansz)) {
580 				segusep[datosn(fs, fs->lfs_curseg) %
581 					fs->lfs_sepb].su_flags |= SEGUSE_ACTIVE;
582 				--dirty;
583 			}
584 			if (dirty)
585 				error = VOP_BWRITE(bp); /* Ifile */
586 			else
587 				brelse(bp);
588 		}
589 	}
590 
591 	did_ckp = 0;
592 	if (do_ckp || fs->lfs_doifile) {
593 		do {
594 			vp = fs->lfs_ivnode;
595 
596 			vget(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
597 
598 			ip = VTOI(vp);
599 			if (vp->v_dirtyblkhd.lh_first != NULL)
600 				lfs_writefile(fs, sp, vp);
601 			if (ip->i_flag & IN_ALLMOD)
602 				++did_ckp;
603 			(void) lfs_writeinode(fs, sp, ip);
604 
605 			vput(vp);
606 		} while (lfs_writeseg(fs, sp) && do_ckp);
607 
608 		/* The ifile should now be all clear */
609 		LFS_CLR_UINO(ip, IN_ALLMOD);
610 	} else {
611 		(void) lfs_writeseg(fs, sp);
612 	}
613 
614 	/*
615 	 * If the I/O count is non-zero, sleep until it reaches zero.
616 	 * At the moment, the user's process hangs around so we can
617 	 * sleep.
618 	 */
619 	fs->lfs_doifile = 0;
620 	if(writer_set && --fs->lfs_writer==0)
621 		wakeup(&fs->lfs_dirops);
622 
623 	/*
624 	 * If we didn't write the Ifile, we didn't really do anything.
625 	 * That means that (1) there is a checkpoint on disk and (2)
626 	 * nothing has changed since it was written.
627 	 *
628 	 * Take the flags off of the segment so that lfs_segunlock
629 	 * doesn't have to write the superblock either.
630 	 */
631 	if (did_ckp == 0) {
632 		sp->seg_flags &= ~(SEGM_SYNC|SEGM_CKP);
633 		/* if(do_ckp) printf("lfs_segwrite: no checkpoint\n"); */
634 	}
635 
636 	if(lfs_dostats) {
637 		++lfs_stats.nwrites;
638 		if (sp->seg_flags & SEGM_SYNC)
639 			++lfs_stats.nsync_writes;
640 		if (sp->seg_flags & SEGM_CKP)
641 			++lfs_stats.ncheckpoints;
642 	}
643 	lfs_segunlock(fs);
644 	return (0);
645 }
646 
647 /*
648  * Write the dirty blocks associated with a vnode.
649  */
650 void
651 lfs_writefile(fs, sp, vp)
652 	struct lfs *fs;
653 	struct segment *sp;
654 	struct vnode *vp;
655 {
656 	struct buf *bp;
657 	struct finfo *fip;
658 	IFILE *ifp;
659 
660 
661 	if (sp->seg_bytes_left < fs->lfs_bsize ||
662 	    sp->sum_bytes_left < sizeof(struct finfo))
663 		(void) lfs_writeseg(fs, sp);
664 
665 	sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
666 	++((SEGSUM *)(sp->segsum))->ss_nfinfo;
667 
668 	if(vp->v_flag & VDIROP)
669 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
670 
671 	fip = sp->fip;
672 	fip->fi_nblocks = 0;
673 	fip->fi_ino = VTOI(vp)->i_number;
674 	LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
675 	fip->fi_version = ifp->if_version;
676 	brelse(bp);
677 
678 	if(sp->seg_flags & SEGM_CLEAN)
679 	{
680 		lfs_gather(fs, sp, vp, lfs_match_fake);
681 		/*
682 		 * For a file being flushed, we need to write *all* blocks.
683 		 * This means writing the cleaning blocks first, and then
684 		 * immediately following with any non-cleaning blocks.
685 		 * The same is true of the Ifile since checkpoints assume
686 		 * that all valid Ifile blocks are written.
687 		 */
688 	   	if(IS_FLUSHING(fs,vp) || VTOI(vp)->i_number == LFS_IFILE_INUM)
689 			lfs_gather(fs, sp, vp, lfs_match_data);
690 	} else
691 		lfs_gather(fs, sp, vp, lfs_match_data);
692 
693 	/*
694 	 * It may not be necessary to write the meta-data blocks at this point,
695 	 * as the roll-forward recovery code should be able to reconstruct the
696 	 * list.
697 	 *
698 	 * We have to write them anyway, though, under two conditions: (1) the
699 	 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
700 	 * checkpointing.
701 	 */
702 	if(lfs_writeindir
703 	   || IS_FLUSHING(fs,vp)
704 	   || (sp->seg_flags & SEGM_CKP))
705 	{
706 		lfs_gather(fs, sp, vp, lfs_match_indir);
707 		lfs_gather(fs, sp, vp, lfs_match_dindir);
708 		lfs_gather(fs, sp, vp, lfs_match_tindir);
709 	}
710 	fip = sp->fip;
711 	if (fip->fi_nblocks != 0) {
712 		sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
713 				   sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
714 		sp->start_lbp = &sp->fip->fi_blocks[0];
715 	} else {
716 		sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
717 		--((SEGSUM *)(sp->segsum))->ss_nfinfo;
718 	}
719 }
720 
721 int
722 lfs_writeinode(fs, sp, ip)
723 	struct lfs *fs;
724 	struct segment *sp;
725 	struct inode *ip;
726 {
727 	struct buf *bp, *ibp;
728 	struct dinode *cdp;
729 	IFILE *ifp;
730 	SEGUSE *sup;
731 	ufs_daddr_t daddr;
732 	daddr_t *daddrp;
733 	ino_t ino;
734 	int error, i, ndx;
735 	int redo_ifile = 0;
736 	struct timespec ts;
737 	int gotblk=0;
738 
739 	if (!(ip->i_flag & IN_ALLMOD))
740 		return(0);
741 
742 	/* Allocate a new inode block if necessary. */
743 	if ((ip->i_number != LFS_IFILE_INUM || sp->idp==NULL) && sp->ibp == NULL) {
744 		/* Allocate a new segment if necessary. */
745 		if (sp->seg_bytes_left < fs->lfs_bsize ||
746 		    sp->sum_bytes_left < sizeof(ufs_daddr_t))
747 			(void) lfs_writeseg(fs, sp);
748 
749 		/* Get next inode block. */
750 		daddr = fs->lfs_offset;
751 		fs->lfs_offset += fsbtodb(fs, 1);
752 		sp->ibp = *sp->cbpp++ =
753 			getblk(VTOI(fs->lfs_ivnode)->i_devvp, daddr, fs->lfs_bsize, 0, 0);
754 		gotblk++;
755 
756 		/* Zero out inode numbers */
757 		for (i = 0; i < INOPB(fs); ++i)
758 			((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
759 
760 		++sp->start_bpp;
761 		fs->lfs_avail -= fsbtodb(fs, 1);
762 		/* Set remaining space counters. */
763 		sp->seg_bytes_left -= fs->lfs_bsize;
764 		sp->sum_bytes_left -= sizeof(ufs_daddr_t);
765 		ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
766 			sp->ninodes / INOPB(fs) - 1;
767 		((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
768 	}
769 
770 	/* Update the inode times and copy the inode onto the inode page. */
771 	TIMEVAL_TO_TIMESPEC(&time, &ts);
772 	LFS_ITIMES(ip, &ts, &ts, &ts);
773 
774 	/*
775 	 * If this is the Ifile, and we've already written the Ifile in this
776 	 * partial segment, just overwrite it (it's not on disk yet) and
777 	 * continue.
778 	 *
779 	 * XXX we know that the bp that we get the second time around has
780 	 * already been gathered.
781 	 */
782 	if(ip->i_number == LFS_IFILE_INUM && sp->idp) {
783 		*(sp->idp) = ip->i_din.ffs_din;
784 		return 0;
785 	}
786 
787 	bp = sp->ibp;
788 	cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
789 	*cdp = ip->i_din.ffs_din;
790 
791 	/*
792 	 * If we are cleaning, ensure that we don't write UNWRITTEN disk
793 	 * addresses to disk.
794 	 */
795 	if (ip->i_lfs_effnblks != ip->i_ffs_blocks) {
796 #ifdef DEBUG_LFS
797 		printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
798 		       ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks);
799 #endif
800 		for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
801 		     daddrp++) {
802 			if (*daddrp == UNWRITTEN) {
803 #ifdef DEBUG_LFS
804 				printf("lfs_writeinode: wiping UNWRITTEN\n");
805 #endif
806 				*daddrp = 0;
807 			}
808 		}
809 	}
810 
811 	if(ip->i_flag & IN_CLEANING)
812 		LFS_CLR_UINO(ip, IN_CLEANING);
813 	else {
814 		/* XXX IN_ALLMOD */
815 		LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
816 			     IN_UPDATE);
817 		if (ip->i_lfs_effnblks == ip->i_ffs_blocks)
818 			LFS_CLR_UINO(ip, IN_MODIFIED);
819 #ifdef DEBUG_LFS
820 		else
821 			printf("lfs_writeinode: ino %d: real blks=%d, "
822 			       "eff=%d\n", ip->i_number, ip->i_ffs_blocks,
823 			       ip->i_lfs_effnblks);
824 #endif
825 	}
826 
827 	if(ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
828 		sp->idp = ((struct dinode *)bp->b_data) +
829 			(sp->ninodes % INOPB(fs));
830 	if(gotblk) {
831 		LFS_LOCK_BUF(bp);
832 		brelse(bp);
833 	}
834 
835 	/* Increment inode count in segment summary block. */
836 	++((SEGSUM *)(sp->segsum))->ss_ninos;
837 
838 	/* If this page is full, set flag to allocate a new page. */
839 	if (++sp->ninodes % INOPB(fs) == 0)
840 		sp->ibp = NULL;
841 
842 	/*
843 	 * If updating the ifile, update the super-block.  Update the disk
844 	 * address and access times for this inode in the ifile.
845 	 */
846 	ino = ip->i_number;
847 	if (ino == LFS_IFILE_INUM) {
848 		daddr = fs->lfs_idaddr;
849 		fs->lfs_idaddr = bp->b_blkno;
850 	} else {
851 		LFS_IENTRY(ifp, fs, ino, ibp);
852 		daddr = ifp->if_daddr;
853 		ifp->if_daddr = bp->b_blkno;
854 #ifdef LFS_DEBUG_NEXTFREE
855 		if(ino > 3 && ifp->if_nextfree) {
856 			vprint("lfs_writeinode",ITOV(ip));
857 			printf("lfs_writeinode: updating free ino %d\n",
858 				ip->i_number);
859 		}
860 #endif
861 		error = VOP_BWRITE(ibp); /* Ifile */
862 	}
863 
864 	/*
865 	 * Account the inode: it no longer belongs to its former segment,
866 	 * though it will not belong to the new segment until that segment
867 	 * is actually written.
868 	 */
869 #ifdef DEBUG
870 	/*
871 	 * The inode's last address should not be in the current partial
872 	 * segment, except under exceptional circumstances (lfs_writevnodes
873 	 * had to start over, and in the meantime more blocks were written
874 	 * to a vnode).  Although the previous inode won't be accounted in
875 	 * su_nbytes until lfs_writeseg, this shouldn't be a problem as we
876 	 * have more data blocks in the current partial segment.
877 	 */
878 	if (daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)
879 		printf("lfs_writeinode: last inode addr in current pseg "
880 		       "(ino %d daddr 0x%x)\n", ino, daddr);
881 #endif
882 	if (daddr != LFS_UNUSED_DADDR) {
883 		LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
884 #ifdef DIAGNOSTIC
885 		if (sup->su_nbytes < DINODE_SIZE) {
886 			printf("lfs_writeinode: negative bytes "
887 			       "(segment %d short by %d)\n",
888 			       datosn(fs, daddr),
889 			       (int)DINODE_SIZE - sup->su_nbytes);
890 			panic("lfs_writeinode: negative bytes");
891 			sup->su_nbytes = DINODE_SIZE;
892 		}
893 #endif
894 		sup->su_nbytes -= DINODE_SIZE;
895 		redo_ifile =
896 			(ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
897 		error = VOP_BWRITE(bp); /* Ifile */
898 	}
899 	return (redo_ifile);
900 }
901 
902 int
903 lfs_gatherblock(sp, bp, sptr)
904 	struct segment *sp;
905 	struct buf *bp;
906 	int *sptr;
907 {
908 	struct lfs *fs;
909 	int version;
910 
911 	/*
912 	 * If full, finish this segment.  We may be doing I/O, so
913 	 * release and reacquire the splbio().
914 	 */
915 #ifdef DIAGNOSTIC
916 	if (sp->vp == NULL)
917 		panic ("lfs_gatherblock: Null vp in segment");
918 #endif
919 	fs = sp->fs;
920 	if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
921 	    sp->seg_bytes_left < bp->b_bcount) {
922 		if (sptr)
923 			splx(*sptr);
924 		lfs_updatemeta(sp);
925 
926 		version = sp->fip->fi_version;
927 		(void) lfs_writeseg(fs, sp);
928 
929 		sp->fip->fi_version = version;
930 		sp->fip->fi_ino = VTOI(sp->vp)->i_number;
931 		/* Add the current file to the segment summary. */
932 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
933 		sp->sum_bytes_left -=
934 			sizeof(struct finfo) - sizeof(ufs_daddr_t);
935 
936 		if (sptr)
937 			*sptr = splbio();
938 		return(1);
939 	}
940 
941 #ifdef DEBUG
942 	if(bp->b_flags & B_GATHERED) {
943 		printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
944 		       sp->fip->fi_ino, bp->b_lblkno);
945 		return(0);
946 	}
947 #endif
948 	/* Insert into the buffer list, update the FINFO block. */
949 	bp->b_flags |= B_GATHERED;
950 	*sp->cbpp++ = bp;
951 	sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
952 
953 	sp->sum_bytes_left -= sizeof(ufs_daddr_t);
954 	sp->seg_bytes_left -= bp->b_bcount;
955 	return(0);
956 }
957 
958 int
959 lfs_gather(fs, sp, vp, match)
960 	struct lfs *fs;
961 	struct segment *sp;
962 	struct vnode *vp;
963 	int (*match) __P((struct lfs *, struct buf *));
964 {
965 	struct buf *bp;
966 	int s, count=0;
967 
968 	sp->vp = vp;
969 	s = splbio();
970 
971 #ifndef LFS_NO_BACKBUF_HACK
972 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
973 #else /* LFS_NO_BACKBUF_HACK */
974 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
975 # define	BUF_OFFSET	(((void *)&bp->b_vnbufs.le_next) - (void *)bp)
976 # define	BACK_BUF(BP)	((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
977 # define	BEG_OF_LIST	((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
978 /* Find last buffer. */
979 loop:	for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
980 	    bp = bp->b_vnbufs.le_next);
981 	for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
982 #endif /* LFS_NO_BACKBUF_HACK */
983 		if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
984 			continue;
985 		if(vp->v_type == VBLK) {
986 			/* For block devices, just write the blocks. */
987 			/* XXX Do we really need to even do this? */
988 #ifdef DEBUG_LFS
989 			if(count==0)
990 				printf("BLK(");
991 			printf(".");
992 #endif
993 			/* Get the block before bwrite, so we don't corrupt the free list */
994 			bp->b_flags |= B_BUSY;
995 			bremfree(bp);
996 			bwrite(bp);
997 		} else {
998 #ifdef DIAGNOSTIC
999 			if ((bp->b_flags & (B_CALL|B_INVAL))==B_INVAL) {
1000 				printf("lfs_gather: lbn %d is B_INVAL\n",
1001 					bp->b_lblkno);
1002 				VOP_PRINT(bp->b_vp);
1003 			}
1004 			if (!(bp->b_flags & B_DELWRI))
1005 				panic("lfs_gather: bp not B_DELWRI");
1006 			if (!(bp->b_flags & B_LOCKED)) {
1007 				printf("lfs_gather: lbn %d blk %d"
1008 				       " not B_LOCKED\n", bp->b_lblkno,
1009 				       bp->b_blkno);
1010 				VOP_PRINT(bp->b_vp);
1011 				panic("lfs_gather: bp not B_LOCKED");
1012 			}
1013 #endif
1014 			if (lfs_gatherblock(sp, bp, &s)) {
1015 				goto loop;
1016 			}
1017 		}
1018 		count++;
1019 	}
1020 	splx(s);
1021 #ifdef DEBUG_LFS
1022 	if(vp->v_type == VBLK && count)
1023 		printf(")\n");
1024 #endif
1025 	lfs_updatemeta(sp);
1026 	sp->vp = NULL;
1027 	return count;
1028 }
1029 
1030 /*
1031  * Update the metadata that points to the blocks listed in the FINFO
1032  * array.
1033  */
1034 void
1035 lfs_updatemeta(sp)
1036 	struct segment *sp;
1037 {
1038 	SEGUSE *sup;
1039 	struct buf *bp;
1040 	struct lfs *fs;
1041 	struct vnode *vp;
1042 	struct indir a[NIADDR + 2], *ap;
1043 	struct inode *ip;
1044 	ufs_daddr_t daddr, lbn, off;
1045 	daddr_t ooff;
1046 	int error, i, nblocks, num;
1047 	int bb;
1048 
1049 	vp = sp->vp;
1050 	nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1051 	if (nblocks < 0)
1052 		panic("This is a bad thing\n");
1053 	if (vp == NULL || nblocks == 0)
1054 		return;
1055 
1056 	/* Sort the blocks. */
1057 	/*
1058 	 * XXX KS - We have to sort even if the blocks come from the
1059 	 * cleaner, because there might be other pending blocks on the
1060 	 * same inode...and if we don't sort, and there are fragments
1061 	 * present, blocks may be written in the wrong place.
1062 	 */
1063 	/* if (!(sp->seg_flags & SEGM_CLEAN)) */
1064 	lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
1065 
1066 	/*
1067 	 * Record the length of the last block in case it's a fragment.
1068 	 * If there are indirect blocks present, they sort last.  An
1069 	 * indirect block will be lfs_bsize and its presence indicates
1070 	 * that you cannot have fragments.
1071 	 */
1072 	sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
1073 
1074 	/*
1075 	 * Assign disk addresses, and update references to the logical
1076 	 * block and the segment usage information.
1077 	 */
1078 	fs = sp->fs;
1079 	for (i = nblocks; i--; ++sp->start_bpp) {
1080 		lbn = *sp->start_lbp++;
1081 
1082 		(*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
1083 		if((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) {
1084 			printf("lfs_updatemeta: ino %d blk %d"
1085 			       " has same lbn and daddr\n",
1086 			       VTOI(vp)->i_number, off);
1087 		}
1088 #ifdef DIAGNOSTIC
1089 		if((*sp->start_bpp)->b_bcount < fs->lfs_bsize && i != 0)
1090 			panic("lfs_updatemeta: fragment is not last block\n");
1091 #endif
1092 		bb = fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
1093 		fs->lfs_offset += bb;
1094 		error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
1095 		if (error)
1096 			panic("lfs_updatemeta: ufs_bmaparray %d", error);
1097 		ip = VTOI(vp);
1098 		switch (num) {
1099 		case 0:
1100 			ooff = ip->i_ffs_db[lbn];
1101 #ifdef DEBUG
1102 			if (ooff == 0) {
1103 				printf("lfs_updatemeta[1]: warning: writing "
1104 				       "ino %d lbn %d at 0x%x, was 0x0\n",
1105 				       ip->i_number, lbn, off);
1106 			}
1107 #endif
1108 			if (ooff == UNWRITTEN)
1109 				ip->i_ffs_blocks += bb;
1110 			ip->i_ffs_db[lbn] = off;
1111 			break;
1112 		case 1:
1113 			ooff = ip->i_ffs_ib[a[0].in_off];
1114 #ifdef DEBUG
1115 			if (ooff == 0) {
1116 				printf("lfs_updatemeta[2]: warning: writing "
1117 				       "ino %d lbn %d at 0x%x, was 0x0\n",
1118 				       ip->i_number, lbn, off);
1119 			}
1120 #endif
1121 			if (ooff == UNWRITTEN)
1122 				ip->i_ffs_blocks += bb;
1123 			ip->i_ffs_ib[a[0].in_off] = off;
1124 			break;
1125 		default:
1126 			ap = &a[num - 1];
1127 			if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1128 				panic("lfs_updatemeta: bread bno %d",
1129 				      ap->in_lbn);
1130 
1131 			ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off];
1132 #if DEBUG
1133 			if (ooff == 0) {
1134 				printf("lfs_updatemeta[3]: warning: writing "
1135 				       "ino %d lbn %d at 0x%x, was 0x0\n",
1136 				       ip->i_number, lbn, off);
1137 			}
1138 #endif
1139 			if (ooff == UNWRITTEN)
1140 				ip->i_ffs_blocks += bb;
1141 			((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
1142 			(void) VOP_BWRITE(bp);
1143 		}
1144 #ifdef DEBUG
1145 		if (daddr >= fs->lfs_lastpseg && daddr <= off) {
1146 			printf("lfs_updatemeta: ino %d, lbn %d, addr = %x "
1147 			       "in same pseg\n", VTOI(sp->vp)->i_number,
1148 			       (*sp->start_bpp)->b_lblkno, daddr);
1149 		}
1150 #endif
1151 		/* Update segment usage information. */
1152 		if (daddr > 0) {
1153 			LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
1154 #ifdef DIAGNOSTIC
1155 			if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
1156 				/* XXX -- Change to a panic. */
1157 				printf("lfs_updatemeta: negative bytes "
1158 				       "(segment %d short by %ld)\n",
1159 				       datosn(fs, daddr),
1160 				       (*sp->start_bpp)->b_bcount -
1161 				       sup->su_nbytes);
1162 				printf("lfs_updatemeta: ino %d, lbn %d, "
1163 				       "addr = %x\n", VTOI(sp->vp)->i_number,
1164 				       (*sp->start_bpp)->b_lblkno, daddr);
1165 				panic("lfs_updatemeta: negative bytes");
1166 				sup->su_nbytes = (*sp->start_bpp)->b_bcount;
1167 			}
1168 #endif
1169 			sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
1170 			error = VOP_BWRITE(bp); /* Ifile */
1171 		}
1172 	}
1173 }
1174 
1175 /*
1176  * Start a new segment.
1177  */
1178 int
1179 lfs_initseg(fs)
1180 	struct lfs *fs;
1181 {
1182 	struct segment *sp;
1183 	SEGUSE *sup;
1184 	SEGSUM *ssp;
1185 	struct buf *bp;
1186 	int repeat;
1187 
1188 	sp = fs->lfs_sp;
1189 
1190 	repeat = 0;
1191 	/* Advance to the next segment. */
1192 	if (!LFS_PARTIAL_FITS(fs)) {
1193 		/* lfs_avail eats the remaining space */
1194 		fs->lfs_avail -= fs->lfs_dbpseg - (fs->lfs_offset -
1195 						   fs->lfs_curseg);
1196 		/* Wake up any cleaning procs waiting on this file system. */
1197 		wakeup(&lfs_allclean_wakeup);
1198 		wakeup(&fs->lfs_nextseg);
1199 		lfs_newseg(fs);
1200 		repeat = 1;
1201 		fs->lfs_offset = fs->lfs_curseg;
1202 		sp->seg_number = datosn(fs, fs->lfs_curseg);
1203 		sp->seg_bytes_left = dbtob(fs->lfs_dbpseg);
1204 		/*
1205 		 * If the segment contains a superblock, update the offset
1206 		 * and summary address to skip over it.
1207 		 */
1208 		LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1209 		if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1210 			fs->lfs_offset += btodb(LFS_SBPAD);
1211 			sp->seg_bytes_left -= LFS_SBPAD;
1212 		}
1213 		brelse(bp);
1214 	} else {
1215 		sp->seg_number = datosn(fs, fs->lfs_curseg);
1216 		sp->seg_bytes_left = dbtob(fs->lfs_dbpseg -
1217 				      (fs->lfs_offset - fs->lfs_curseg));
1218 	}
1219 	fs->lfs_lastpseg = fs->lfs_offset;
1220 
1221 	sp->fs = fs;
1222 	sp->ibp = NULL;
1223 	sp->idp = NULL;
1224 	sp->ninodes = 0;
1225 
1226 	/* Get a new buffer for SEGSUM and enter it into the buffer list. */
1227 	sp->cbpp = sp->bpp;
1228 	*sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
1229 			       fs->lfs_offset, LFS_SUMMARY_SIZE);
1230 	sp->segsum = (*sp->cbpp)->b_data;
1231 	bzero(sp->segsum, LFS_SUMMARY_SIZE);
1232 	sp->start_bpp = ++sp->cbpp;
1233 	fs->lfs_offset += btodb(LFS_SUMMARY_SIZE);
1234 
1235 	/* Set point to SEGSUM, initialize it. */
1236 	ssp = sp->segsum;
1237 	ssp->ss_next = fs->lfs_nextseg;
1238 	ssp->ss_nfinfo = ssp->ss_ninos = 0;
1239 	ssp->ss_magic = SS_MAGIC;
1240 
1241 	/* Set pointer to first FINFO, initialize it. */
1242 	sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
1243 	sp->fip->fi_nblocks = 0;
1244 	sp->start_lbp = &sp->fip->fi_blocks[0];
1245 	sp->fip->fi_lastlength = 0;
1246 
1247 	sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
1248 	sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
1249 
1250 	return(repeat);
1251 }
1252 
1253 /*
1254  * Return the next segment to write.
1255  */
1256 void
1257 lfs_newseg(fs)
1258 	struct lfs *fs;
1259 {
1260 	CLEANERINFO *cip;
1261 	SEGUSE *sup;
1262 	struct buf *bp;
1263 	int curseg, isdirty, sn;
1264 
1265 	LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
1266 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1267 	sup->su_nbytes = 0;
1268 	sup->su_nsums = 0;
1269 	sup->su_ninos = 0;
1270 	(void) VOP_BWRITE(bp); /* Ifile */
1271 
1272 	LFS_CLEANERINFO(cip, fs, bp);
1273 	--cip->clean;
1274 	++cip->dirty;
1275 	fs->lfs_nclean = cip->clean;
1276 	LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1277 
1278 	fs->lfs_lastseg = fs->lfs_curseg;
1279 	fs->lfs_curseg = fs->lfs_nextseg;
1280 	for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
1281 		sn = (sn + 1) % fs->lfs_nseg;
1282 		if (sn == curseg)
1283 			panic("lfs_nextseg: no clean segments");
1284 		LFS_SEGENTRY(sup, fs, sn, bp);
1285 		isdirty = sup->su_flags & SEGUSE_DIRTY;
1286 		brelse(bp);
1287 		if (!isdirty)
1288 			break;
1289 	}
1290 
1291 	++fs->lfs_nactive;
1292 	fs->lfs_nextseg = sntoda(fs, sn);
1293 	if(lfs_dostats) {
1294 		++lfs_stats.segsused;
1295 	}
1296 }
1297 
1298 int
1299 lfs_writeseg(fs, sp)
1300 	struct lfs *fs;
1301 	struct segment *sp;
1302 {
1303 	struct buf **bpp, *bp, *cbp, *newbp;
1304 	SEGUSE *sup;
1305 	SEGSUM *ssp;
1306 	dev_t i_dev;
1307 	u_long *datap, *dp;
1308 	int do_again, i, nblocks, s;
1309 #ifdef LFS_TRACK_IOS
1310 	int j;
1311 #endif
1312 	int (*strategy)__P((void *));
1313 	struct vop_strategy_args vop_strategy_a;
1314 	u_short ninos;
1315 	struct vnode *devvp;
1316 	char *p;
1317 	struct vnode *vn;
1318 	struct inode *ip;
1319 	daddr_t *daddrp;
1320 	int changed;
1321 #if defined(DEBUG) && defined(LFS_PROPELLER)
1322 	static int propeller;
1323 	char propstring[4] = "-\\|/";
1324 
1325 	printf("%c\b",propstring[propeller++]);
1326 	if(propeller==4)
1327 		propeller = 0;
1328 #endif
1329 
1330 	/*
1331 	 * If there are no buffers other than the segment summary to write
1332 	 * and it is not a checkpoint, don't do anything.  On a checkpoint,
1333 	 * even if there aren't any buffers, you need to write the superblock.
1334 	 */
1335 	if ((nblocks = sp->cbpp - sp->bpp) == 1)
1336 		return (0);
1337 
1338 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1339 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1340 
1341 	/* Update the segment usage information. */
1342 	LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1343 
1344 	/* Loop through all blocks, except the segment summary. */
1345 	for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1346 		if((*bpp)->b_vp != devvp)
1347 			sup->su_nbytes += (*bpp)->b_bcount;
1348 	}
1349 
1350 	ssp = (SEGSUM *)sp->segsum;
1351 
1352 	ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1353 	sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
1354 	/* sup->su_nbytes += LFS_SUMMARY_SIZE; */
1355 	sup->su_lastmod = time.tv_sec;
1356 	sup->su_ninos += ninos;
1357 	++sup->su_nsums;
1358 	fs->lfs_dmeta += (btodb(LFS_SUMMARY_SIZE) + fsbtodb(fs, ninos));
1359 	fs->lfs_avail -= btodb(LFS_SUMMARY_SIZE);
1360 
1361 	do_again = !(bp->b_flags & B_GATHERED);
1362 	(void)VOP_BWRITE(bp); /* Ifile */
1363 	/*
1364 	 * Mark blocks B_BUSY, to prevent then from being changed between
1365 	 * the checksum computation and the actual write.
1366 	 *
1367 	 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1368 	 * there are any, replace them with copies that have UNASSIGNED
1369 	 * instead.
1370 	 */
1371 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1372 		++bpp;
1373 		if((*bpp)->b_flags & B_CALL)
1374 			continue;
1375 		bp = *bpp;
1376 	    again:
1377 		s = splbio();
1378 		if(bp->b_flags & B_BUSY) {
1379 #ifdef DEBUG
1380 			printf("lfs_writeseg: avoiding potential data "
1381 			       "summary corruption for ino %d, lbn %d\n",
1382 			       VTOI(bp->b_vp)->i_number, bp->b_lblkno);
1383 #endif
1384 			bp->b_flags |= B_WANTED;
1385 			tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
1386 			splx(s);
1387 			goto again;
1388 		}
1389 		bp->b_flags |= B_BUSY;
1390 		splx(s);
1391 		/* Check and replace indirect block UNWRITTEN bogosity */
1392 		if(bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1393 		   VTOI(bp->b_vp)->i_ffs_blocks !=
1394 		   VTOI(bp->b_vp)->i_lfs_effnblks) {
1395 #ifdef DEBUG_LFS
1396 			printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
1397 			       VTOI(bp->b_vp)->i_number,
1398 			       VTOI(bp->b_vp)->i_lfs_effnblks,
1399 			       VTOI(bp->b_vp)->i_ffs_blocks);
1400 #endif
1401 			/* Make a copy we'll make changes to */
1402 			newbp = lfs_newbuf(bp->b_vp, bp->b_lblkno,
1403 					   bp->b_bcount);
1404 			newbp->b_blkno = bp->b_blkno;
1405 			memcpy(newbp->b_data, bp->b_data,
1406 			       newbp->b_bcount);
1407 			*bpp = newbp;
1408 
1409 			changed = 0;
1410 			for (daddrp = (daddr_t *)(newbp->b_data);
1411 			     daddrp < (daddr_t *)(newbp->b_data +
1412 						  newbp->b_bcount); daddrp++) {
1413 				if (*daddrp == UNWRITTEN) {
1414 					++changed;
1415 #ifdef DEBUG_LFS
1416 					printf("lfs_writeseg: replacing UNWRITTEN\n");
1417 #endif
1418 					*daddrp = 0;
1419 				}
1420 			}
1421 			/*
1422 			 * Get rid of the old buffer.  Don't mark it clean,
1423 			 * though, if it still has dirty data on it.
1424 			 */
1425 			if (changed) {
1426 				bp->b_flags &= ~(B_ERROR | B_GATHERED);
1427 				if (bp->b_flags & B_CALL)
1428 					lfs_freebuf(bp);
1429 				else {
1430 					/* Still on free list, leave it there */
1431 					s = splbio();
1432 					bp->b_flags &= ~B_BUSY;
1433 					if (bp->b_flags & B_WANTED)
1434 						wakeup(bp);
1435 				 	splx(s);
1436 					/*
1437 					 * We have to re-decrement lfs_avail
1438 					 * since this block is going to come
1439 					 * back around to us in the next
1440 					 * segment.
1441 					 */
1442 					fs->lfs_avail -= btodb(bp->b_bcount);
1443 				}
1444 			} else {
1445 				bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1446 						 B_GATHERED);
1447 				LFS_UNLOCK_BUF(bp);
1448 				if (bp->b_flags & B_CALL)
1449 					lfs_freebuf(bp);
1450 				else {
1451 					bremfree(bp);
1452 					bp->b_flags |= B_DONE;
1453 					reassignbuf(bp, bp->b_vp);
1454 					brelse(bp);
1455 				}
1456 			}
1457 
1458 		}
1459 	}
1460 	/*
1461 	 * Compute checksum across data and then across summary; the first
1462 	 * block (the summary block) is skipped.  Set the create time here
1463 	 * so that it's guaranteed to be later than the inode mod times.
1464 	 *
1465 	 * XXX
1466 	 * Fix this to do it inline, instead of malloc/copy.
1467 	 */
1468 	datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
1469 	for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1470 		if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1471 			if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
1472 				panic("lfs_writeseg: copyin failed [1]: "
1473 				      "ino %d blk %d",
1474 				      VTOI((*bpp)->b_vp)->i_number,
1475 				      (*bpp)->b_lblkno);
1476 		} else
1477 			*dp++ = ((u_long *)(*bpp)->b_data)[0];
1478 	}
1479 	ssp->ss_create = time.tv_sec;
1480 	ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
1481 	ssp->ss_sumsum =
1482 	    cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
1483 	free(datap, M_SEGMENT);
1484 
1485 	fs->lfs_bfree -= (fsbtodb(fs, ninos) + btodb(LFS_SUMMARY_SIZE));
1486 
1487 	strategy = devvp->v_op[VOFFSET(vop_strategy)];
1488 
1489 	/*
1490 	 * When we simply write the blocks we lose a rotation for every block
1491 	 * written.  To avoid this problem, we allocate memory in chunks, copy
1492 	 * the buffers into the chunk and write the chunk.  CHUNKSIZE is the
1493 	 * largest size I/O devices can handle.
1494 	 * When the data is copied to the chunk, turn off the B_LOCKED bit
1495 	 * and brelse the buffer (which will move them to the LRU list).  Add
1496 	 * the B_CALL flag to the buffer header so we can count I/O's for the
1497 	 * checkpoints and so we can release the allocated memory.
1498 	 *
1499 	 * XXX
1500 	 * This should be removed if the new virtual memory system allows us to
1501 	 * easily make the buffers contiguous in kernel memory and if that's
1502 	 * fast enough.
1503 	 */
1504 
1505 #define CHUNKSIZE MAXPHYS
1506 
1507 	if(devvp==NULL)
1508 		panic("devvp is NULL");
1509 	for (bpp = sp->bpp,i = nblocks; i;) {
1510 		cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE);
1511 		cbp->b_dev = i_dev;
1512 		cbp->b_flags |= B_ASYNC | B_BUSY;
1513 		cbp->b_bcount = 0;
1514 
1515 #ifdef DIAGNOSTIC
1516 		if(datosn(fs, (*bpp)->b_blkno + btodb((*bpp)->b_bcount) - 1) !=
1517 		   datosn(fs, cbp->b_blkno)) {
1518 			panic("lfs_writeseg: Segment overwrite");
1519 		}
1520 #endif
1521 
1522 		s = splbio();
1523 		if(fs->lfs_iocount >= LFS_THROTTLE) {
1524 			tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
1525 		}
1526 		++fs->lfs_iocount;
1527 #ifdef LFS_TRACK_IOS
1528 		for(j=0;j<LFS_THROTTLE;j++) {
1529 			if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) {
1530 				fs->lfs_pending[j] = cbp->b_blkno;
1531 				break;
1532 			}
1533 		}
1534 #endif /* LFS_TRACK_IOS */
1535 		for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
1536 			bp = *bpp;
1537 
1538 			if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1539 				break;
1540 
1541 			/*
1542 			 * Fake buffers from the cleaner are marked as B_INVAL.
1543 			 * We need to copy the data from user space rather than
1544 			 * from the buffer indicated.
1545 			 * XXX == what do I do on an error?
1546 			 */
1547 			if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1548 				if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1549 					panic("lfs_writeseg: copyin failed [2]");
1550 			} else
1551 				bcopy(bp->b_data, p, bp->b_bcount);
1552 			p += bp->b_bcount;
1553 			cbp->b_bcount += bp->b_bcount;
1554 			LFS_UNLOCK_BUF(bp);
1555 			bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1556 					 B_GATHERED);
1557 			vn = bp->b_vp;
1558 			if (bp->b_flags & B_CALL) {
1559 				/* if B_CALL, it was created with newbuf */
1560 				lfs_freebuf(bp);
1561 			} else {
1562 				bremfree(bp);
1563 				bp->b_flags |= B_DONE;
1564 				if(vn)
1565 					reassignbuf(bp, vn);
1566 				brelse(bp);
1567 			}
1568 
1569 			bpp++;
1570 
1571 			/*
1572 			 * If this is the last block for this vnode, but
1573 			 * there are other blocks on its dirty list,
1574 			 * set IN_MODIFIED/IN_CLEANING depending on what
1575 			 * sort of block.  Only do this for our mount point,
1576 			 * not for, e.g., inode blocks that are attached to
1577 			 * the devvp.
1578 			 */
1579 			if(i>1 && vn && *bpp && (*bpp)->b_vp != vn
1580 			   && (*bpp)->b_vp && (bp=vn->v_dirtyblkhd.lh_first)!=NULL &&
1581 			   vn->v_mount == fs->lfs_ivnode->v_mount)
1582 			{
1583 				ip = VTOI(vn);
1584 #ifdef DEBUG_LFS
1585 				printf("lfs_writeseg: marking ino %d\n",ip->i_number);
1586 #endif
1587 				if(bp->b_flags & B_CALL)
1588 					LFS_SET_UINO(ip, IN_CLEANING);
1589 				else
1590 					LFS_SET_UINO(ip, IN_MODIFIED);
1591 			}
1592 			/* if(vn->v_dirtyblkhd.lh_first == NULL) */
1593 				wakeup(vn);
1594 		}
1595 		++cbp->b_vp->v_numoutput;
1596 		splx(s);
1597 		/*
1598 		 * XXXX This is a gross and disgusting hack.  Since these
1599 		 * buffers are physically addressed, they hang off the
1600 		 * device vnode (devvp).  As a result, they have no way
1601 		 * of getting to the LFS superblock or lfs structure to
1602 		 * keep track of the number of I/O's pending.  So, I am
1603 		 * going to stuff the fs into the saveaddr field of
1604 		 * the buffer (yuk).
1605 		 */
1606 		cbp->b_saveaddr = (caddr_t)fs;
1607 		vop_strategy_a.a_desc = VDESC(vop_strategy);
1608 		vop_strategy_a.a_bp = cbp;
1609 		(strategy)(&vop_strategy_a);
1610 	}
1611 #if 1 || defined(DEBUG)
1612 	/*
1613 	 * After doing a big write, we recalculate how many buffers are
1614 	 * really still left on the locked queue.
1615 	 */
1616 	s = splbio();
1617 	lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
1618 	splx(s);
1619 	wakeup(&locked_queue_count);
1620 #endif /* 1 || DEBUG */
1621 	if(lfs_dostats) {
1622 		++lfs_stats.psegwrites;
1623 		lfs_stats.blocktot += nblocks - 1;
1624 		if (fs->lfs_sp->seg_flags & SEGM_SYNC)
1625 			++lfs_stats.psyncwrites;
1626 		if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
1627 			++lfs_stats.pcleanwrites;
1628 			lfs_stats.cleanblocks += nblocks - 1;
1629 		}
1630 	}
1631 	return (lfs_initseg(fs) || do_again);
1632 }
1633 
1634 void
1635 lfs_writesuper(fs, daddr)
1636 	struct lfs *fs;
1637 	daddr_t daddr;
1638 {
1639 	struct buf *bp;
1640 	dev_t i_dev;
1641 	int (*strategy) __P((void *));
1642 	int s;
1643 	struct vop_strategy_args vop_strategy_a;
1644 
1645 #ifdef LFS_CANNOT_ROLLFW
1646 	/*
1647 	 * If we can write one superblock while another is in
1648 	 * progress, we risk not having a complete checkpoint if we crash.
1649 	 * So, block here if a superblock write is in progress.
1650 	 */
1651 	s = splbio();
1652 	while(fs->lfs_sbactive) {
1653 		tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
1654 	}
1655 	fs->lfs_sbactive = daddr;
1656 	splx(s);
1657 #endif
1658 	i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1659 	strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
1660 
1661 	/* Set timestamp of this version of the superblock */
1662 	fs->lfs_tstamp = time.tv_sec;
1663 
1664 	/* Checksum the superblock and copy it into a buffer. */
1665 	fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1666 	bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD);
1667 	*(struct dlfs *)bp->b_data = fs->lfs_dlfs;
1668 
1669 	bp->b_dev = i_dev;
1670 	bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
1671 	bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
1672 	bp->b_iodone = lfs_supercallback;
1673 	/* XXX KS - same nasty hack as above */
1674 	bp->b_saveaddr = (caddr_t)fs;
1675 
1676 	vop_strategy_a.a_desc = VDESC(vop_strategy);
1677 	vop_strategy_a.a_bp = bp;
1678 	s = splbio();
1679 	++bp->b_vp->v_numoutput;
1680 	++fs->lfs_iocount;
1681 	splx(s);
1682 	(strategy)(&vop_strategy_a);
1683 }
1684 
1685 /*
1686  * Logical block number match routines used when traversing the dirty block
1687  * chain.
1688  */
1689 int
1690 lfs_match_fake(fs, bp)
1691 	struct lfs *fs;
1692 	struct buf *bp;
1693 {
1694 	return (bp->b_flags & B_CALL);
1695 }
1696 
1697 int
1698 lfs_match_data(fs, bp)
1699 	struct lfs *fs;
1700 	struct buf *bp;
1701 {
1702 	return (bp->b_lblkno >= 0);
1703 }
1704 
1705 int
1706 lfs_match_indir(fs, bp)
1707 	struct lfs *fs;
1708 	struct buf *bp;
1709 {
1710 	int lbn;
1711 
1712 	lbn = bp->b_lblkno;
1713 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
1714 }
1715 
1716 int
1717 lfs_match_dindir(fs, bp)
1718 	struct lfs *fs;
1719 	struct buf *bp;
1720 {
1721 	int lbn;
1722 
1723 	lbn = bp->b_lblkno;
1724 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
1725 }
1726 
1727 int
1728 lfs_match_tindir(fs, bp)
1729 	struct lfs *fs;
1730 	struct buf *bp;
1731 {
1732 	int lbn;
1733 
1734 	lbn = bp->b_lblkno;
1735 	return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
1736 }
1737 
1738 /*
1739  * XXX - The only buffers that are going to hit these functions are the
1740  * segment write blocks, or the segment summaries, or the superblocks.
1741  *
1742  * All of the above are created by lfs_newbuf, and so do not need to be
1743  * released via brelse.
1744  */
1745 void
1746 lfs_callback(bp)
1747 	struct buf *bp;
1748 {
1749 	struct lfs *fs;
1750 #ifdef LFS_TRACK_IOS
1751 	int j;
1752 #endif
1753 
1754 	fs = (struct lfs *)bp->b_saveaddr;
1755 #ifdef DIAGNOSTIC
1756 	if (fs->lfs_iocount == 0)
1757 		panic("lfs_callback: zero iocount\n");
1758 #endif
1759 	if (--fs->lfs_iocount < LFS_THROTTLE)
1760 		wakeup(&fs->lfs_iocount);
1761 #ifdef LFS_TRACK_IOS
1762 	for(j=0;j<LFS_THROTTLE;j++) {
1763 		if(fs->lfs_pending[j]==bp->b_blkno) {
1764 			fs->lfs_pending[j] = LFS_UNUSED_DADDR;
1765 			wakeup(&(fs->lfs_pending[j]));
1766 			break;
1767 		}
1768 	}
1769 #endif /* LFS_TRACK_IOS */
1770 
1771 	lfs_freebuf(bp);
1772 }
1773 
1774 void
1775 lfs_supercallback(bp)
1776 	struct buf *bp;
1777 {
1778 	struct lfs *fs;
1779 
1780 	fs = (struct lfs *)bp->b_saveaddr;
1781 #ifdef LFS_CANNOT_ROLLFW
1782 	fs->lfs_sbactive = 0;
1783 	wakeup(&fs->lfs_sbactive);
1784 #endif
1785 	if (--fs->lfs_iocount < LFS_THROTTLE)
1786 		wakeup(&fs->lfs_iocount);
1787 	lfs_freebuf(bp);
1788 }
1789 
1790 /*
1791  * Shellsort (diminishing increment sort) from Data Structures and
1792  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
1793  * see also Knuth Vol. 3, page 84.  The increments are selected from
1794  * formula (8), page 95.  Roughly O(N^3/2).
1795  */
1796 /*
1797  * This is our own private copy of shellsort because we want to sort
1798  * two parallel arrays (the array of buffer pointers and the array of
1799  * logical block numbers) simultaneously.  Note that we cast the array
1800  * of logical block numbers to a unsigned in this routine so that the
1801  * negative block numbers (meta data blocks) sort AFTER the data blocks.
1802  */
1803 
1804 void
1805 lfs_shellsort(bp_array, lb_array, nmemb)
1806 	struct buf **bp_array;
1807 	ufs_daddr_t *lb_array;
1808 	int nmemb;
1809 {
1810 	static int __rsshell_increments[] = { 4, 1, 0 };
1811 	int incr, *incrp, t1, t2;
1812 	struct buf *bp_temp;
1813 	u_long lb_temp;
1814 
1815 	for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
1816 		for (t1 = incr; t1 < nmemb; ++t1)
1817 			for (t2 = t1 - incr; t2 >= 0;)
1818 				if (lb_array[t2] > lb_array[t2 + incr]) {
1819 					lb_temp = lb_array[t2];
1820 					lb_array[t2] = lb_array[t2 + incr];
1821 					lb_array[t2 + incr] = lb_temp;
1822 					bp_temp = bp_array[t2];
1823 					bp_array[t2] = bp_array[t2 + incr];
1824 					bp_array[t2 + incr] = bp_temp;
1825 					t2 -= incr;
1826 				} else
1827 					break;
1828 }
1829 
1830 /*
1831  * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
1832  */
1833 int
1834 lfs_vref(vp)
1835 	struct vnode *vp;
1836 {
1837 	/*
1838 	 * If we return 1 here during a flush, we risk vinvalbuf() not
1839 	 * being able to flush all of the pages from this vnode, which
1840 	 * will cause it to panic.  So, return 0 if a flush is in progress.
1841 	 */
1842 	if (vp->v_flag & VXLOCK) {
1843 		if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1844 			return 0;
1845 		}
1846 		return(1);
1847 	}
1848 	return (vget(vp, 0));
1849 }
1850 
1851 /*
1852  * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
1853  * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
1854  */
1855 void
1856 lfs_vunref(vp)
1857 	struct vnode *vp;
1858 {
1859 	/*
1860 	 * Analogous to lfs_vref, if the node is flushing, fake it.
1861 	 */
1862 	if((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1863 		return;
1864 	}
1865 
1866 	simple_lock(&vp->v_interlock);
1867 #ifdef DIAGNOSTIC
1868 	if(vp->v_usecount<=0) {
1869 		printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
1870 		printf("lfs_vunref: flags are 0x%x\n", vp->v_flag);
1871 		printf("lfs_vunref: usecount = %d\n", vp->v_usecount);
1872 		panic("lfs_vunref: v_usecount<0");
1873 	}
1874 #endif
1875 	vp->v_usecount--;
1876 	if (vp->v_usecount > 0) {
1877 		simple_unlock(&vp->v_interlock);
1878 		return;
1879 	}
1880 	/*
1881 	 * insert at tail of LRU list
1882 	 */
1883 	simple_lock(&vnode_free_list_slock);
1884 	if (vp->v_holdcnt > 0)
1885 		TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
1886 	else
1887 		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1888 	simple_unlock(&vnode_free_list_slock);
1889 	simple_unlock(&vp->v_interlock);
1890 }
1891 
1892 /*
1893  * We use this when we have vnodes that were loaded in solely for cleaning.
1894  * There is no reason to believe that these vnodes will be referenced again
1895  * soon, since the cleaning process is unrelated to normal filesystem
1896  * activity.  Putting cleaned vnodes at the tail of the list has the effect
1897  * of flushing the vnode LRU.  So, put vnodes that were loaded only for
1898  * cleaning at the head of the list, instead.
1899  */
1900 void
1901 lfs_vunref_head(vp)
1902 	struct vnode *vp;
1903 {
1904 	simple_lock(&vp->v_interlock);
1905 #ifdef DIAGNOSTIC
1906 	if(vp->v_usecount==0) {
1907 		panic("lfs_vunref: v_usecount<0");
1908 	}
1909 #endif
1910 	vp->v_usecount--;
1911 	if (vp->v_usecount > 0) {
1912 		simple_unlock(&vp->v_interlock);
1913 		return;
1914 	}
1915 	/*
1916 	 * insert at head of LRU list
1917 	 */
1918 	simple_lock(&vnode_free_list_slock);
1919 	TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1920 	simple_unlock(&vnode_free_list_slock);
1921 	simple_unlock(&vp->v_interlock);
1922 }
1923 
1924