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