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