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