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