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