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