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