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