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