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