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