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