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