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