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