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