lfs_segment.c revision 1.158.2.13 1 /* $NetBSD: lfs_segment.c,v 1.158.2.13 2006/08/10 12:16:46 tron Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_segment.c,v 1.158.2.13 2006/08/10 12:16:46 tron 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/syslog.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 ((flags & SEGM_CLEAN) == 0 && \
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 TIMEVAL_TO_TIMESPEC(&time, &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 struct timespec ts;
921 int gotblk = 0;
922
923 ASSERT_SEGLOCK(fs);
924 if (!(ip->i_flag & IN_ALLMOD))
925 return (0);
926
927 /* Allocate a new inode block if necessary. */
928 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
929 sp->ibp == NULL) {
930 /* Allocate a new segment if necessary. */
931 if (sp->seg_bytes_left < fs->lfs_ibsize ||
932 sp->sum_bytes_left < sizeof(int32_t))
933 (void) lfs_writeseg(fs, sp);
934
935 /* Get next inode block. */
936 daddr = fs->lfs_offset;
937 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
938 sp->ibp = *sp->cbpp++ =
939 getblk(VTOI(fs->lfs_ivnode)->i_devvp,
940 fsbtodb(fs, daddr), fs->lfs_ibsize, 0, 0);
941 gotblk++;
942
943 /* Zero out inode numbers */
944 for (i = 0; i < INOPB(fs); ++i)
945 ((struct ufs1_dinode *)sp->ibp->b_data)[i].di_inumber =
946 0;
947
948 ++sp->start_bpp;
949 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
950 /* Set remaining space counters. */
951 sp->seg_bytes_left -= fs->lfs_ibsize;
952 sp->sum_bytes_left -= sizeof(int32_t);
953 ndx = fs->lfs_sumsize / sizeof(int32_t) -
954 sp->ninodes / INOPB(fs) - 1;
955 ((int32_t *)(sp->segsum))[ndx] = daddr;
956 }
957
958 /* Update the inode times and copy the inode onto the inode page. */
959 TIMEVAL_TO_TIMESPEC(&time, &ts);
960 /* XXX kludge --- don't redirty the ifile just to put times on it */
961 if (ip->i_number != LFS_IFILE_INUM)
962 LFS_ITIMES(ip, &ts, &ts, &ts);
963
964 /*
965 * If this is the Ifile, and we've already written the Ifile in this
966 * partial segment, just overwrite it (it's not on disk yet) and
967 * continue.
968 *
969 * XXX we know that the bp that we get the second time around has
970 * already been gathered.
971 */
972 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
973 *(sp->idp) = *ip->i_din.ffs1_din;
974 ip->i_lfs_osize = ip->i_size;
975 return 0;
976 }
977
978 bp = sp->ibp;
979 cdp = ((struct ufs1_dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
980 *cdp = *ip->i_din.ffs1_din;
981
982 /* We can finish the segment accounting for truncations now */
983 lfs_finalize_ino_seguse(fs, ip);
984
985 /*
986 * If we are cleaning, ensure that we don't write UNWRITTEN disk
987 * addresses to disk; possibly change the on-disk record of
988 * the inode size, either by reverting to the previous size
989 * (in the case of cleaning) or by verifying the inode's block
990 * holdings (in the case of files being allocated as they are being
991 * written).
992 * XXX By not writing UNWRITTEN blocks, we are making the lfs_avail
993 * XXX count on disk wrong by the same amount. We should be
994 * XXX able to "borrow" from lfs_avail and return it after the
995 * XXX Ifile is written. See also in lfs_writeseg.
996 */
997
998 /* Check file size based on highest allocated block */
999 if (((ip->i_ffs1_mode & IFMT) == IFREG ||
1000 (ip->i_ffs1_mode & IFMT) == IFDIR) &&
1001 ip->i_size > ((ip->i_lfs_hiblk + 1) << fs->lfs_bshift)) {
1002 cdp->di_size = (ip->i_lfs_hiblk + 1) << fs->lfs_bshift;
1003 DLOG((DLOG_SEG, "lfs_writeinode: ino %d size %" PRId64 " -> %"
1004 PRId64 "\n", (int)ip->i_number, ip->i_size, cdp->di_size));
1005 }
1006 if (ip->i_lfs_effnblks != ip->i_ffs1_blocks) {
1007 if (ip->i_flags & IN_CLEANING)
1008 cdp->di_size = ip->i_lfs_osize;
1009 DLOG((DLOG_SEG, "lfs_writeinode: cleansing ino %d eff %d != nblk %d)"
1010 " at %x\n", ip->i_number, ip->i_lfs_effnblks,
1011 ip->i_ffs1_blocks, fs->lfs_offset));
1012 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
1013 daddrp++) {
1014 if (*daddrp == UNWRITTEN) {
1015 DLOG((DLOG_SEG, "lfs_writeinode: wiping UNWRITTEN\n"));
1016 *daddrp = 0;
1017 }
1018 }
1019 } else {
1020 /* If all blocks are going to disk, update "size on disk" */
1021 ip->i_lfs_osize = ip->i_size;
1022 }
1023
1024 #ifdef DIAGNOSTIC
1025 /*
1026 * Check dinode held blocks against dinode size.
1027 * This should be identical to the check in lfs_vget().
1028 */
1029 for (i = (cdp->di_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
1030 i < NDADDR; i++) {
1031 KASSERT(i >= 0);
1032 if ((cdp->di_mode & IFMT) == IFLNK)
1033 continue;
1034 if (((cdp->di_mode & IFMT) == IFBLK ||
1035 (cdp->di_mode & IFMT) == IFCHR) && i == 0)
1036 continue;
1037 if (cdp->di_db[i] != 0) {
1038 # ifdef DEBUG
1039 lfs_dump_dinode(cdp);
1040 # endif
1041 panic("writing inconsistent inode");
1042 }
1043 }
1044 #endif /* DIAGNOSTIC */
1045
1046 if (ip->i_flag & IN_CLEANING)
1047 LFS_CLR_UINO(ip, IN_CLEANING);
1048 else {
1049 /* XXX IN_ALLMOD */
1050 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
1051 IN_UPDATE | IN_MODIFY);
1052 if (ip->i_lfs_effnblks == ip->i_ffs1_blocks)
1053 LFS_CLR_UINO(ip, IN_MODIFIED);
1054 else
1055 DLOG((DLOG_VNODE, "lfs_writeinode: ino %d: real blks=%d, "
1056 "eff=%d\n", ip->i_number, ip->i_ffs1_blocks,
1057 ip->i_lfs_effnblks));
1058 }
1059
1060 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
1061 sp->idp = ((struct ufs1_dinode *)bp->b_data) +
1062 (sp->ninodes % INOPB(fs));
1063 if (gotblk) {
1064 LFS_LOCK_BUF(bp);
1065 brelse(bp);
1066 }
1067
1068 /* Increment inode count in segment summary block. */
1069 ++((SEGSUM *)(sp->segsum))->ss_ninos;
1070
1071 /* If this page is full, set flag to allocate a new page. */
1072 if (++sp->ninodes % INOPB(fs) == 0)
1073 sp->ibp = NULL;
1074
1075 /*
1076 * If updating the ifile, update the super-block. Update the disk
1077 * address and access times for this inode in the ifile.
1078 */
1079 ino = ip->i_number;
1080 if (ino == LFS_IFILE_INUM) {
1081 daddr = fs->lfs_idaddr;
1082 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
1083 } else {
1084 LFS_IENTRY(ifp, fs, ino, ibp);
1085 daddr = ifp->if_daddr;
1086 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
1087 error = LFS_BWRITE_LOG(ibp); /* Ifile */
1088 }
1089
1090 /*
1091 * The inode's last address should not be in the current partial
1092 * segment, except under exceptional circumstances (lfs_writevnodes
1093 * had to start over, and in the meantime more blocks were written
1094 * to a vnode). Both inodes will be accounted to this segment
1095 * in lfs_writeseg so we need to subtract the earlier version
1096 * here anyway. The segment count can temporarily dip below
1097 * zero here; keep track of how many duplicates we have in
1098 * "dupino" so we don't panic below.
1099 */
1100 if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno)) {
1101 ++sp->ndupino;
1102 DLOG((DLOG_SEG, "lfs_writeinode: last inode addr in current pseg "
1103 "(ino %d daddr 0x%llx) ndupino=%d\n", ino,
1104 (long long)daddr, sp->ndupino));
1105 }
1106 /*
1107 * Account the inode: it no longer belongs to its former segment,
1108 * though it will not belong to the new segment until that segment
1109 * is actually written.
1110 */
1111 if (daddr != LFS_UNUSED_DADDR) {
1112 u_int32_t oldsn = dtosn(fs, daddr);
1113 #ifdef DIAGNOSTIC
1114 int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0;
1115 #endif
1116 LFS_SEGENTRY(sup, fs, oldsn, bp);
1117 #ifdef DIAGNOSTIC
1118 if (sup->su_nbytes +
1119 sizeof (struct ufs1_dinode) * ndupino
1120 < sizeof (struct ufs1_dinode)) {
1121 printf("lfs_writeinode: negative bytes "
1122 "(segment %" PRIu32 " short by %d, "
1123 "oldsn=%" PRIu32 ", cursn=%" PRIu32
1124 ", daddr=%" PRId64 ", su_nbytes=%u, "
1125 "ndupino=%d)\n",
1126 dtosn(fs, daddr),
1127 (int)sizeof (struct ufs1_dinode) *
1128 (1 - sp->ndupino) - sup->su_nbytes,
1129 oldsn, sp->seg_number, daddr,
1130 (unsigned int)sup->su_nbytes,
1131 sp->ndupino);
1132 panic("lfs_writeinode: negative bytes");
1133 sup->su_nbytes = sizeof (struct ufs1_dinode);
1134 }
1135 #endif
1136 DLOG((DLOG_SU, "seg %d -= %d for ino %d inode\n",
1137 dtosn(fs, daddr), sizeof (struct ufs1_dinode), ino));
1138 sup->su_nbytes -= sizeof (struct ufs1_dinode);
1139 redo_ifile =
1140 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
1141 if (redo_ifile) {
1142 simple_lock(&fs->lfs_interlock);
1143 fs->lfs_flags |= LFS_IFDIRTY;
1144 simple_unlock(&fs->lfs_interlock);
1145 }
1146 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
1147 }
1148 return (redo_ifile);
1149 }
1150
1151 int
1152 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
1153 {
1154 struct lfs *fs;
1155 int vers;
1156 int j, blksinblk;
1157
1158 ASSERT_SEGLOCK(sp->fs);
1159 /*
1160 * If full, finish this segment. We may be doing I/O, so
1161 * release and reacquire the splbio().
1162 */
1163 #ifdef DIAGNOSTIC
1164 if (sp->vp == NULL)
1165 panic ("lfs_gatherblock: Null vp in segment");
1166 #endif
1167 fs = sp->fs;
1168 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
1169 if (sp->sum_bytes_left < sizeof(int32_t) * blksinblk ||
1170 sp->seg_bytes_left < bp->b_bcount) {
1171 if (sptr)
1172 splx(*sptr);
1173 lfs_updatemeta(sp);
1174
1175 vers = sp->fip->fi_version;
1176 (void) lfs_writeseg(fs, sp);
1177
1178 /* Add the current file to the segment summary. */
1179 lfs_acquire_finfo(fs, VTOI(sp->vp)->i_number, vers);
1180
1181 if (sptr)
1182 *sptr = splbio();
1183 return (1);
1184 }
1185
1186 if (bp->b_flags & B_GATHERED) {
1187 DLOG((DLOG_SEG, "lfs_gatherblock: already gathered! Ino %d,"
1188 " lbn %" PRId64 "\n",
1189 sp->fip->fi_ino, bp->b_lblkno));
1190 return (0);
1191 }
1192
1193 /* Insert into the buffer list, update the FINFO block. */
1194 bp->b_flags |= B_GATHERED;
1195
1196 *sp->cbpp++ = bp;
1197 for (j = 0; j < blksinblk; j++) {
1198 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
1199 /* This block's accounting moves from lfs_favail to lfs_avail */
1200 lfs_deregister_block(sp->vp, bp->b_lblkno + j);
1201 }
1202
1203 sp->sum_bytes_left -= sizeof(int32_t) * blksinblk;
1204 sp->seg_bytes_left -= bp->b_bcount;
1205 return (0);
1206 }
1207
1208 int
1209 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp,
1210 int (*match)(struct lfs *, struct buf *))
1211 {
1212 struct buf *bp, *nbp;
1213 int s, count = 0;
1214
1215 ASSERT_SEGLOCK(fs);
1216 if (vp->v_type == VBLK)
1217 return 0;
1218 KASSERT(sp->vp == NULL);
1219 sp->vp = vp;
1220 s = splbio();
1221
1222 #ifndef LFS_NO_BACKBUF_HACK
1223 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
1224 # define BUF_OFFSET \
1225 (((caddr_t)&LIST_NEXT(bp, b_vnbufs)) - (caddr_t)bp)
1226 # define BACK_BUF(BP) \
1227 ((struct buf *)(((caddr_t)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
1228 # define BEG_OF_LIST \
1229 ((struct buf *)(((caddr_t)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
1230
1231 loop:
1232 /* Find last buffer. */
1233 for (bp = LIST_FIRST(&vp->v_dirtyblkhd);
1234 bp && LIST_NEXT(bp, b_vnbufs) != NULL;
1235 bp = LIST_NEXT(bp, b_vnbufs))
1236 /* nothing */;
1237 for (; bp && bp != BEG_OF_LIST; bp = nbp) {
1238 nbp = BACK_BUF(bp);
1239 #else /* LFS_NO_BACKBUF_HACK */
1240 loop:
1241 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1242 nbp = LIST_NEXT(bp, b_vnbufs);
1243 #endif /* LFS_NO_BACKBUF_HACK */
1244 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) {
1245 #ifdef DEBUG
1246 if (vp == fs->lfs_ivnode &&
1247 (bp->b_flags & (B_BUSY|B_GATHERED)) == B_BUSY)
1248 DLOG((DLOG_SEG, "lfs_gather: ifile lbn %"
1249 PRId64 " busy (%x)",
1250 bp->b_lblkno, bp->b_flags));
1251 #endif
1252 continue;
1253 }
1254 #ifdef DIAGNOSTIC
1255 # ifdef LFS_USE_B_INVAL
1256 if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
1257 DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64
1258 " is B_INVAL\n", bp->b_lblkno));
1259 VOP_PRINT(bp->b_vp);
1260 }
1261 # endif /* LFS_USE_B_INVAL */
1262 if (!(bp->b_flags & B_DELWRI))
1263 panic("lfs_gather: bp not B_DELWRI");
1264 if (!(bp->b_flags & B_LOCKED)) {
1265 DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64
1266 " blk %" PRId64 " not B_LOCKED\n",
1267 bp->b_lblkno,
1268 dbtofsb(fs, bp->b_blkno)));
1269 VOP_PRINT(bp->b_vp);
1270 panic("lfs_gather: bp not B_LOCKED");
1271 }
1272 #endif
1273 if (lfs_gatherblock(sp, bp, &s)) {
1274 goto loop;
1275 }
1276 count++;
1277 }
1278 splx(s);
1279 lfs_updatemeta(sp);
1280 KASSERT(sp->vp == vp);
1281 sp->vp = NULL;
1282 return count;
1283 }
1284
1285 #if DEBUG
1286 # define DEBUG_OOFF(n) do { \
1287 if (ooff == 0) { \
1288 DLOG((DLOG_SEG, "lfs_updatemeta[%d]: warning: writing " \
1289 "ino %d lbn %" PRId64 " at 0x%" PRIx32 \
1290 ", was 0x0 (or %" PRId64 ")\n", \
1291 (n), ip->i_number, lbn, ndaddr, daddr)); \
1292 } \
1293 } while (0)
1294 #else
1295 # define DEBUG_OOFF(n)
1296 #endif
1297
1298 /*
1299 * Change the given block's address to ndaddr, finding its previous
1300 * location using ufs_bmaparray().
1301 *
1302 * Account for this change in the segment table.
1303 *
1304 * called with sp == NULL by roll-forwarding code.
1305 */
1306 void
1307 lfs_update_single(struct lfs *fs, struct segment *sp, struct vnode *vp,
1308 daddr_t lbn, int32_t ndaddr, int size)
1309 {
1310 SEGUSE *sup;
1311 struct buf *bp;
1312 struct indir a[NIADDR + 2], *ap;
1313 struct inode *ip;
1314 daddr_t daddr, ooff;
1315 int num, error;
1316 int bb, osize, obb;
1317
1318 ASSERT_SEGLOCK(fs);
1319 KASSERT(sp == NULL || sp->vp == vp);
1320 ip = VTOI(vp);
1321
1322 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL, NULL);
1323 if (error)
1324 panic("lfs_updatemeta: ufs_bmaparray returned %d", error);
1325
1326 daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
1327 KASSERT(daddr <= LFS_MAX_DADDR);
1328 if (daddr > 0)
1329 daddr = dbtofsb(fs, daddr);
1330
1331 bb = fragstofsb(fs, numfrags(fs, size));
1332 switch (num) {
1333 case 0:
1334 ooff = ip->i_ffs1_db[lbn];
1335 DEBUG_OOFF(0);
1336 if (ooff == UNWRITTEN)
1337 ip->i_ffs1_blocks += bb;
1338 else {
1339 /* possible fragment truncation or extension */
1340 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
1341 ip->i_ffs1_blocks += (bb - obb);
1342 }
1343 ip->i_ffs1_db[lbn] = ndaddr;
1344 break;
1345 case 1:
1346 ooff = ip->i_ffs1_ib[a[0].in_off];
1347 DEBUG_OOFF(1);
1348 if (ooff == UNWRITTEN)
1349 ip->i_ffs1_blocks += bb;
1350 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
1351 break;
1352 default:
1353 ap = &a[num - 1];
1354 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1355 panic("lfs_updatemeta: bread bno %" PRId64,
1356 ap->in_lbn);
1357
1358 /* XXX ondisk32 */
1359 ooff = ((int32_t *)bp->b_data)[ap->in_off];
1360 DEBUG_OOFF(num);
1361 if (ooff == UNWRITTEN)
1362 ip->i_ffs1_blocks += bb;
1363 /* XXX ondisk32 */
1364 ((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
1365 (void) VOP_BWRITE(bp);
1366 }
1367
1368 KASSERT(ooff == 0 || ooff == UNWRITTEN || ooff == daddr);
1369
1370 /* Update hiblk when extending the file */
1371 if (lbn > ip->i_lfs_hiblk)
1372 ip->i_lfs_hiblk = lbn;
1373
1374 /*
1375 * Though we'd rather it couldn't, this *can* happen right now
1376 * if cleaning blocks and regular blocks coexist.
1377 */
1378 /* KASSERT(daddr < fs->lfs_lastpseg || daddr > ndaddr); */
1379
1380 /*
1381 * Update segment usage information, based on old size
1382 * and location.
1383 */
1384 if (daddr > 0) {
1385 u_int32_t oldsn = dtosn(fs, daddr);
1386 #ifdef DIAGNOSTIC
1387 int ndupino;
1388
1389 if (sp && sp->seg_number == oldsn) {
1390 ndupino = sp->ndupino;
1391 } else {
1392 ndupino = 0;
1393 }
1394 #endif
1395 KASSERT(oldsn >= 0 && oldsn < fs->lfs_nseg);
1396 if (lbn >= 0 && lbn < NDADDR)
1397 osize = ip->i_lfs_fragsize[lbn];
1398 else
1399 osize = fs->lfs_bsize;
1400 LFS_SEGENTRY(sup, fs, oldsn, bp);
1401 #ifdef DIAGNOSTIC
1402 if (sup->su_nbytes + sizeof (struct ufs1_dinode) * ndupino
1403 < osize) {
1404 printf("lfs_updatemeta: negative bytes "
1405 "(segment %" PRIu32 " short by %" PRId64
1406 ")\n", dtosn(fs, daddr),
1407 (int64_t)osize -
1408 (sizeof (struct ufs1_dinode) * ndupino +
1409 sup->su_nbytes));
1410 printf("lfs_updatemeta: ino %llu, lbn %" PRId64
1411 ", addr = 0x%" PRIx64 "\n",
1412 (unsigned long long)ip->i_number, lbn, daddr);
1413 printf("lfs_updatemeta: ndupino=%d\n", ndupino);
1414 panic("lfs_updatemeta: negative bytes");
1415 sup->su_nbytes = osize -
1416 sizeof (struct ufs1_dinode) * ndupino;
1417 }
1418 #endif
1419 DLOG((DLOG_SU, "seg %" PRIu32 " -= %d for ino %d lbn %" PRId64
1420 " db 0x%" PRIx64 "\n",
1421 dtosn(fs, daddr), osize,
1422 ip->i_number, lbn, daddr));
1423 sup->su_nbytes -= osize;
1424 if (!(bp->b_flags & B_GATHERED)) {
1425 simple_lock(&fs->lfs_interlock);
1426 fs->lfs_flags |= LFS_IFDIRTY;
1427 simple_unlock(&fs->lfs_interlock);
1428 }
1429 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
1430 }
1431 /*
1432 * Now that this block has a new address, and its old
1433 * segment no longer owns it, we can forget about its
1434 * old size.
1435 */
1436 if (lbn >= 0 && lbn < NDADDR)
1437 ip->i_lfs_fragsize[lbn] = size;
1438 }
1439
1440 /*
1441 * Update the metadata that points to the blocks listed in the FINFO
1442 * array.
1443 */
1444 void
1445 lfs_updatemeta(struct segment *sp)
1446 {
1447 struct buf *sbp;
1448 struct lfs *fs;
1449 struct vnode *vp;
1450 daddr_t lbn;
1451 int i, nblocks, num;
1452 int bb;
1453 int bytesleft, size;
1454
1455 ASSERT_SEGLOCK(sp->fs);
1456 vp = sp->vp;
1457 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1458 KASSERT(nblocks >= 0);
1459 KASSERT(vp != NULL);
1460 if (nblocks == 0)
1461 return;
1462
1463 /*
1464 * This count may be high due to oversize blocks from lfs_gop_write.
1465 * Correct for this. (XXX we should be able to keep track of these.)
1466 */
1467 fs = sp->fs;
1468 for (i = 0; i < nblocks; i++) {
1469 if (sp->start_bpp[i] == NULL) {
1470 DLOG((DLOG_SEG, "lfs_updatemeta: nblocks = %d, not %d\n", i, nblocks));
1471 nblocks = i;
1472 break;
1473 }
1474 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
1475 KASSERT(sp->start_bpp[i]->b_lblkno >= 0 || num == 1);
1476 nblocks -= num - 1;
1477 }
1478
1479 KASSERT(vp->v_type == VREG ||
1480 nblocks == &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp);
1481 KASSERT(nblocks == sp->cbpp - sp->start_bpp);
1482
1483 /*
1484 * Sort the blocks.
1485 *
1486 * We have to sort even if the blocks come from the
1487 * cleaner, because there might be other pending blocks on the
1488 * same inode...and if we don't sort, and there are fragments
1489 * present, blocks may be written in the wrong place.
1490 */
1491 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
1492
1493 /*
1494 * Record the length of the last block in case it's a fragment.
1495 * If there are indirect blocks present, they sort last. An
1496 * indirect block will be lfs_bsize and its presence indicates
1497 * that you cannot have fragments.
1498 *
1499 * XXX This last is a lie. A cleaned fragment can coexist with
1500 * XXX a later indirect block. This will continue to be
1501 * XXX true until lfs_markv is fixed to do everything with
1502 * XXX fake blocks (including fake inodes and fake indirect blocks).
1503 */
1504 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
1505 fs->lfs_bmask) + 1;
1506
1507 /*
1508 * Assign disk addresses, and update references to the logical
1509 * block and the segment usage information.
1510 */
1511 for (i = nblocks; i--; ++sp->start_bpp) {
1512 sbp = *sp->start_bpp;
1513 lbn = *sp->start_lbp;
1514 KASSERT(sbp->b_lblkno == lbn);
1515
1516 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1517
1518 /*
1519 * If we write a frag in the wrong place, the cleaner won't
1520 * be able to correctly identify its size later, and the
1521 * segment will be uncleanable. (Even worse, it will assume
1522 * that the indirect block that actually ends the list
1523 * is of a smaller size!)
1524 */
1525 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
1526 panic("lfs_updatemeta: fragment is not last block");
1527
1528 /*
1529 * For each subblock in this possibly oversized block,
1530 * update its address on disk.
1531 */
1532 KASSERT(lbn >= 0 || sbp->b_bcount == fs->lfs_bsize);
1533 KASSERT(vp == sbp->b_vp);
1534 for (bytesleft = sbp->b_bcount; bytesleft > 0;
1535 bytesleft -= fs->lfs_bsize) {
1536 size = MIN(bytesleft, fs->lfs_bsize);
1537 bb = fragstofsb(fs, numfrags(fs, size));
1538 lbn = *sp->start_lbp++;
1539 lfs_update_single(fs, sp, sp->vp, lbn, fs->lfs_offset,
1540 size);
1541 fs->lfs_offset += bb;
1542 }
1543
1544 }
1545 }
1546
1547 /*
1548 * Move lfs_offset to a segment earlier than sn.
1549 */
1550 int
1551 lfs_rewind(struct lfs *fs, int newsn)
1552 {
1553 int sn, osn, isdirty;
1554 struct buf *bp;
1555 SEGUSE *sup;
1556
1557 ASSERT_SEGLOCK(fs);
1558
1559 osn = dtosn(fs, fs->lfs_offset);
1560 if (osn < newsn)
1561 return 0;
1562
1563 /* lfs_avail eats the remaining space in this segment */
1564 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset - fs->lfs_curseg);
1565
1566 /* Find a low-numbered segment */
1567 for (sn = 0; sn < fs->lfs_nseg; ++sn) {
1568 LFS_SEGENTRY(sup, fs, sn, bp);
1569 isdirty = sup->su_flags & SEGUSE_DIRTY;
1570 brelse(bp);
1571
1572 if (!isdirty)
1573 break;
1574 }
1575 if (sn == fs->lfs_nseg)
1576 panic("lfs_rewind: no clean segments");
1577 if (newsn >= 0 && sn >= newsn)
1578 return ENOENT;
1579 fs->lfs_nextseg = sn;
1580 lfs_newseg(fs);
1581 fs->lfs_offset = fs->lfs_curseg;
1582
1583 return 0;
1584 }
1585
1586 /*
1587 * Start a new partial segment.
1588 *
1589 * Return 1 when we entered to a new segment.
1590 * Otherwise, return 0.
1591 */
1592 int
1593 lfs_initseg(struct lfs *fs)
1594 {
1595 struct segment *sp = fs->lfs_sp;
1596 SEGSUM *ssp;
1597 struct buf *sbp; /* buffer for SEGSUM */
1598 int repeat = 0; /* return value */
1599
1600 ASSERT_SEGLOCK(fs);
1601 /* Advance to the next segment. */
1602 if (!LFS_PARTIAL_FITS(fs)) {
1603 SEGUSE *sup;
1604 struct buf *bp;
1605
1606 /* lfs_avail eats the remaining space */
1607 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1608 fs->lfs_curseg);
1609 /* Wake up any cleaning procs waiting on this file system. */
1610 lfs_wakeup_cleaner(fs);
1611 lfs_newseg(fs);
1612 repeat = 1;
1613 fs->lfs_offset = fs->lfs_curseg;
1614
1615 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1616 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1617
1618 /*
1619 * If the segment contains a superblock, update the offset
1620 * and summary address to skip over it.
1621 */
1622 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1623 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1624 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1625 sp->seg_bytes_left -= LFS_SBPAD;
1626 }
1627 brelse(bp);
1628 /* Segment zero could also contain the labelpad */
1629 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1630 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1631 fs->lfs_offset +=
1632 btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1633 sp->seg_bytes_left -=
1634 LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1635 }
1636 } else {
1637 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1638 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1639 (fs->lfs_offset - fs->lfs_curseg));
1640 }
1641 fs->lfs_lastpseg = fs->lfs_offset;
1642
1643 /* Record first address of this partial segment */
1644 if (sp->seg_flags & SEGM_CLEAN) {
1645 fs->lfs_cleanint[fs->lfs_cleanind] = fs->lfs_offset;
1646 if (++fs->lfs_cleanind >= LFS_MAX_CLEANIND) {
1647 /* "1" is the artificial inc in lfs_seglock */
1648 simple_lock(&fs->lfs_interlock);
1649 while (fs->lfs_iocount > 1) {
1650 ltsleep(&fs->lfs_iocount, PRIBIO + 1,
1651 "lfs_initseg", 0, &fs->lfs_interlock);
1652 }
1653 simple_unlock(&fs->lfs_interlock);
1654 fs->lfs_cleanind = 0;
1655 }
1656 }
1657
1658 sp->fs = fs;
1659 sp->ibp = NULL;
1660 sp->idp = NULL;
1661 sp->ninodes = 0;
1662 sp->ndupino = 0;
1663
1664 sp->cbpp = sp->bpp;
1665
1666 /* Get a new buffer for SEGSUM */
1667 sbp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1668 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize, LFS_NB_SUMMARY);
1669
1670 /* ... and enter it into the buffer list. */
1671 *sp->cbpp = sbp;
1672 sp->cbpp++;
1673 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1674
1675 sp->start_bpp = sp->cbpp;
1676
1677 /* Set point to SEGSUM, initialize it. */
1678 ssp = sp->segsum = sbp->b_data;
1679 memset(ssp, 0, fs->lfs_sumsize);
1680 ssp->ss_next = fs->lfs_nextseg;
1681 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1682 ssp->ss_magic = SS_MAGIC;
1683
1684 /* Set pointer to first FINFO, initialize it. */
1685 sp->fip = (struct finfo *)((caddr_t)sp->segsum + SEGSUM_SIZE(fs));
1686 sp->fip->fi_nblocks = 0;
1687 sp->start_lbp = &sp->fip->fi_blocks[0];
1688 sp->fip->fi_lastlength = 0;
1689
1690 sp->seg_bytes_left -= fs->lfs_sumsize;
1691 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1692
1693 return (repeat);
1694 }
1695
1696 /*
1697 * Remove SEGUSE_INVAL from all segments.
1698 */
1699 void
1700 lfs_unset_inval_all(struct lfs *fs)
1701 {
1702 SEGUSE *sup;
1703 struct buf *bp;
1704 int i;
1705
1706 for (i = 0; i < fs->lfs_nseg; i++) {
1707 LFS_SEGENTRY(sup, fs, i, bp);
1708 if (sup->su_flags & SEGUSE_INVAL) {
1709 sup->su_flags &= ~SEGUSE_INVAL;
1710 VOP_BWRITE(bp);
1711 } else
1712 brelse(bp);
1713 }
1714 }
1715
1716 /*
1717 * Return the next segment to write.
1718 */
1719 void
1720 lfs_newseg(struct lfs *fs)
1721 {
1722 CLEANERINFO *cip;
1723 SEGUSE *sup;
1724 struct buf *bp;
1725 int curseg, isdirty, sn, skip_inval;
1726
1727 ASSERT_SEGLOCK(fs);
1728
1729 /* Honor LFCNWRAPSTOP */
1730 simple_lock(&fs->lfs_interlock);
1731 while (fs->lfs_nowrap && fs->lfs_nextseg < fs->lfs_curseg) {
1732 log(LOG_NOTICE, "%s: waiting on log wrap\n", fs->lfs_fsmnt);
1733 wakeup(&fs->lfs_nowrap);
1734 ltsleep(&fs->lfs_nowrap, PVFS, "newseg", 0,
1735 &fs->lfs_interlock);
1736 }
1737 simple_unlock(&fs->lfs_interlock);
1738
1739 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1740 DLOG((DLOG_SU, "lfs_newseg: seg %d := 0 in newseg\n",
1741 dtosn(fs, fs->lfs_nextseg)));
1742 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1743 sup->su_nbytes = 0;
1744 sup->su_nsums = 0;
1745 sup->su_ninos = 0;
1746 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1747
1748 LFS_CLEANERINFO(cip, fs, bp);
1749 --cip->clean;
1750 ++cip->dirty;
1751 fs->lfs_nclean = cip->clean;
1752 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1753
1754 fs->lfs_lastseg = fs->lfs_curseg;
1755 fs->lfs_curseg = fs->lfs_nextseg;
1756 skip_inval = 1;
1757 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1758 sn = (sn + 1) % fs->lfs_nseg;
1759
1760 if (sn == curseg) {
1761 if (skip_inval)
1762 skip_inval = 0;
1763 else
1764 panic("lfs_nextseg: no clean segments");
1765 }
1766 LFS_SEGENTRY(sup, fs, sn, bp);
1767 isdirty = sup->su_flags & (SEGUSE_DIRTY | (skip_inval ? SEGUSE_INVAL : 0));
1768 /* Check SEGUSE_EMPTY as we go along */
1769 if (isdirty && sup->su_nbytes == 0 &&
1770 !(sup->su_flags & SEGUSE_EMPTY))
1771 LFS_WRITESEGENTRY(sup, fs, sn, bp);
1772 else
1773 brelse(bp);
1774
1775 if (!isdirty)
1776 break;
1777 }
1778 if (skip_inval == 0)
1779 lfs_unset_inval_all(fs);
1780
1781 ++fs->lfs_nactive;
1782 fs->lfs_nextseg = sntod(fs, sn);
1783 if (lfs_dostats) {
1784 ++lfs_stats.segsused;
1785 }
1786 }
1787
1788 static struct buf *
1789 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr, int n)
1790 {
1791 struct lfs_cluster *cl;
1792 struct buf **bpp, *bp;
1793 int s;
1794
1795 ASSERT_SEGLOCK(fs);
1796 cl = (struct lfs_cluster *)pool_get(&fs->lfs_clpool, PR_WAITOK);
1797 bpp = (struct buf **)pool_get(&fs->lfs_bpppool, PR_WAITOK);
1798 memset(cl, 0, sizeof(*cl));
1799 cl->fs = fs;
1800 cl->bpp = bpp;
1801 cl->bufcount = 0;
1802 cl->bufsize = 0;
1803
1804 /* If this segment is being written synchronously, note that */
1805 if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
1806 cl->flags |= LFS_CL_SYNC;
1807 cl->seg = fs->lfs_sp;
1808 ++cl->seg->seg_iocount;
1809 }
1810
1811 /* Get an empty buffer header, or maybe one with something on it */
1812 s = splbio();
1813 bp = pool_get(&bufpool, PR_WAITOK); /* XXX should use lfs_malloc? */
1814 splx(s);
1815 memset(bp, 0, sizeof(*bp));
1816 BUF_INIT(bp);
1817
1818 bp->b_flags = B_BUSY | B_CALL;
1819 bp->b_dev = NODEV;
1820 bp->b_blkno = bp->b_lblkno = addr;
1821 bp->b_iodone = lfs_cluster_callback;
1822 bp->b_private = cl;
1823 bp->b_vp = vp;
1824
1825 return bp;
1826 }
1827
1828 int
1829 lfs_writeseg(struct lfs *fs, struct segment *sp)
1830 {
1831 struct buf **bpp, *bp, *cbp, *newbp;
1832 SEGUSE *sup;
1833 SEGSUM *ssp;
1834 int i, s;
1835 int do_again, nblocks, byteoffset;
1836 size_t el_size;
1837 struct lfs_cluster *cl;
1838 u_short ninos;
1839 struct vnode *devvp;
1840 char *p = NULL;
1841 struct vnode *vp;
1842 int32_t *daddrp; /* XXX ondisk32 */
1843 int changed;
1844 u_int32_t sum;
1845 #ifdef DEBUG
1846 FINFO *fip;
1847 int findex;
1848 #endif
1849
1850 ASSERT_SEGLOCK(fs);
1851 /*
1852 * If there are no buffers other than the segment summary to write
1853 * and it is not a checkpoint, don't do anything. On a checkpoint,
1854 * even if there aren't any buffers, you need to write the superblock.
1855 */
1856 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1857 return (0);
1858
1859 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1860
1861 /* Update the segment usage information. */
1862 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1863
1864 /* Loop through all blocks, except the segment summary. */
1865 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1866 if ((*bpp)->b_vp != devvp) {
1867 sup->su_nbytes += (*bpp)->b_bcount;
1868 DLOG((DLOG_SU, "seg %" PRIu32 " += %ld for ino %d"
1869 " lbn %" PRId64 " db 0x%" PRIx64 "\n",
1870 sp->seg_number, (*bpp)->b_bcount,
1871 VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno,
1872 (*bpp)->b_blkno));
1873 }
1874 }
1875
1876 ssp = (SEGSUM *)sp->segsum;
1877
1878 #ifdef DEBUG
1879 /* Check for zero-length and zero-version FINFO entries. */
1880 fip = (struct finfo *)((caddr_t)ssp + SEGSUM_SIZE(fs));
1881 for (findex = 0; findex < ssp->ss_nfinfo; findex++) {
1882 KDASSERT(fip->fi_nblocks > 0);
1883 KDASSERT(fip->fi_version > 0);
1884 fip = (FINFO *)((caddr_t)fip + FINFOSIZE +
1885 sizeof(int32_t) * fip->fi_nblocks);
1886 }
1887 #endif /* DEBUG */
1888
1889 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1890 DLOG((DLOG_SU, "seg %d += %d for %d inodes\n",
1891 sp->seg_number, ssp->ss_ninos * sizeof (struct ufs1_dinode),
1892 ssp->ss_ninos));
1893 sup->su_nbytes += ssp->ss_ninos * sizeof (struct ufs1_dinode);
1894 /* sup->su_nbytes += fs->lfs_sumsize; */
1895 if (fs->lfs_version == 1)
1896 sup->su_olastmod = time.tv_sec;
1897 else
1898 sup->su_lastmod = time.tv_sec;
1899 sup->su_ninos += ninos;
1900 ++sup->su_nsums;
1901 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
1902
1903 do_again = !(bp->b_flags & B_GATHERED);
1904 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
1905
1906 /*
1907 * Mark blocks B_BUSY, to prevent then from being changed between
1908 * the checksum computation and the actual write.
1909 *
1910 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1911 * there are any, replace them with copies that have UNASSIGNED
1912 * instead.
1913 */
1914 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1915 ++bpp;
1916 bp = *bpp;
1917 if (bp->b_flags & B_CALL) { /* UBC or malloced buffer */
1918 bp->b_flags |= B_BUSY;
1919 continue;
1920 }
1921
1922 simple_lock(&bp->b_interlock);
1923 s = splbio();
1924 while (bp->b_flags & B_BUSY) {
1925 DLOG((DLOG_SEG, "lfs_writeseg: avoiding potential"
1926 " data summary corruption for ino %d, lbn %"
1927 PRId64 "\n",
1928 VTOI(bp->b_vp)->i_number, bp->b_lblkno));
1929 bp->b_flags |= B_WANTED;
1930 ltsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0,
1931 &bp->b_interlock);
1932 splx(s);
1933 s = splbio();
1934 }
1935 bp->b_flags |= B_BUSY;
1936 splx(s);
1937 simple_unlock(&bp->b_interlock);
1938
1939 /*
1940 * Check and replace indirect block UNWRITTEN bogosity.
1941 * XXX See comment in lfs_writefile.
1942 */
1943 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1944 VTOI(bp->b_vp)->i_ffs1_blocks !=
1945 VTOI(bp->b_vp)->i_lfs_effnblks) {
1946 DLOG((DLOG_VNODE, "lfs_writeseg: cleansing ino %d (%d != %d)\n",
1947 VTOI(bp->b_vp)->i_number,
1948 VTOI(bp->b_vp)->i_lfs_effnblks,
1949 VTOI(bp->b_vp)->i_ffs1_blocks));
1950 /* Make a copy we'll make changes to */
1951 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
1952 bp->b_bcount, LFS_NB_IBLOCK);
1953 newbp->b_blkno = bp->b_blkno;
1954 memcpy(newbp->b_data, bp->b_data,
1955 newbp->b_bcount);
1956
1957 changed = 0;
1958 /* XXX ondisk32 */
1959 for (daddrp = (int32_t *)(newbp->b_data);
1960 daddrp < (int32_t *)(newbp->b_data +
1961 newbp->b_bcount); daddrp++) {
1962 if (*daddrp == UNWRITTEN) {
1963 ++changed;
1964 *daddrp = 0;
1965 }
1966 }
1967 /*
1968 * Get rid of the old buffer. Don't mark it clean,
1969 * though, if it still has dirty data on it.
1970 */
1971 if (changed) {
1972 DLOG((DLOG_SEG, "lfs_writeseg: replacing UNWRITTEN(%d):"
1973 " bp = %p newbp = %p\n", changed, bp,
1974 newbp));
1975 *bpp = newbp;
1976 bp->b_flags &= ~(B_ERROR | B_GATHERED);
1977 if (bp->b_flags & B_CALL) {
1978 DLOG((DLOG_SEG, "lfs_writeseg: "
1979 "indir bp should not be B_CALL\n"));
1980 s = splbio();
1981 biodone(bp);
1982 splx(s);
1983 bp = NULL;
1984 } else {
1985 /* Still on free list, leave it there */
1986 s = splbio();
1987 bp->b_flags &= ~B_BUSY;
1988 if (bp->b_flags & B_WANTED)
1989 wakeup(bp);
1990 splx(s);
1991 /*
1992 * We have to re-decrement lfs_avail
1993 * since this block is going to come
1994 * back around to us in the next
1995 * segment.
1996 */
1997 fs->lfs_avail -=
1998 btofsb(fs, bp->b_bcount);
1999 }
2000 } else {
2001 lfs_freebuf(fs, newbp);
2002 }
2003 }
2004 }
2005 /*
2006 * Compute checksum across data and then across summary; the first
2007 * block (the summary block) is skipped. Set the create time here
2008 * so that it's guaranteed to be later than the inode mod times.
2009 */
2010 sum = 0;
2011 if (fs->lfs_version == 1)
2012 el_size = sizeof(u_long);
2013 else
2014 el_size = sizeof(u_int32_t);
2015 for (bpp = sp->bpp, i = nblocks - 1; i--; ) {
2016 ++bpp;
2017 /* Loop through gop_write cluster blocks */
2018 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
2019 byteoffset += fs->lfs_bsize) {
2020 #ifdef LFS_USE_B_INVAL
2021 if (((*bpp)->b_flags & (B_CALL | B_INVAL)) ==
2022 (B_CALL | B_INVAL)) {
2023 if (copyin((caddr_t)(*bpp)->b_saveaddr +
2024 byteoffset, dp, el_size)) {
2025 panic("lfs_writeseg: copyin failed [1]:"
2026 " ino %d blk %" PRId64,
2027 VTOI((*bpp)->b_vp)->i_number,
2028 (*bpp)->b_lblkno);
2029 }
2030 } else
2031 #endif /* LFS_USE_B_INVAL */
2032 {
2033 sum = lfs_cksum_part(
2034 (*bpp)->b_data + byteoffset, el_size, sum);
2035 }
2036 }
2037 }
2038 if (fs->lfs_version == 1)
2039 ssp->ss_ocreate = time.tv_sec;
2040 else {
2041 ssp->ss_create = time.tv_sec;
2042 ssp->ss_serial = ++fs->lfs_serial;
2043 ssp->ss_ident = fs->lfs_ident;
2044 }
2045 ssp->ss_datasum = lfs_cksum_fold(sum);
2046 ssp->ss_sumsum = cksum(&ssp->ss_datasum,
2047 fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
2048
2049 simple_lock(&fs->lfs_interlock);
2050 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
2051 btofsb(fs, fs->lfs_sumsize));
2052 fs->lfs_dmeta += (btofsb(fs, ninos * fs->lfs_ibsize) +
2053 btofsb(fs, fs->lfs_sumsize));
2054 simple_unlock(&fs->lfs_interlock);
2055
2056 /*
2057 * When we simply write the blocks we lose a rotation for every block
2058 * written. To avoid this problem, we cluster the buffers into a
2059 * chunk and write the chunk. MAXPHYS is the largest size I/O
2060 * devices can handle, use that for the size of the chunks.
2061 *
2062 * Blocks that are already clusters (from GOP_WRITE), however, we
2063 * don't bother to copy into other clusters.
2064 */
2065
2066 #define CHUNKSIZE MAXPHYS
2067
2068 if (devvp == NULL)
2069 panic("devvp is NULL");
2070 for (bpp = sp->bpp, i = nblocks; i;) {
2071 cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
2072 cl = cbp->b_private;
2073
2074 cbp->b_flags |= B_ASYNC | B_BUSY;
2075 cbp->b_bcount = 0;
2076
2077 #if defined(DEBUG) && defined(DIAGNOSTIC)
2078 if (bpp - sp->bpp > (fs->lfs_sumsize - SEGSUM_SIZE(fs))
2079 / sizeof(int32_t)) {
2080 panic("lfs_writeseg: real bpp overwrite");
2081 }
2082 if (bpp - sp->bpp > segsize(fs) / fs->lfs_fsize) {
2083 panic("lfs_writeseg: theoretical bpp overwrite");
2084 }
2085 #endif
2086
2087 /*
2088 * Construct the cluster.
2089 */
2090 simple_lock(&fs->lfs_interlock);
2091 ++fs->lfs_iocount;
2092 simple_unlock(&fs->lfs_interlock);
2093 while (i && cbp->b_bcount < CHUNKSIZE) {
2094 bp = *bpp;
2095
2096 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
2097 break;
2098 if (cbp->b_bcount > 0 && !(cl->flags & LFS_CL_MALLOC))
2099 break;
2100
2101 /* Clusters from GOP_WRITE are expedited */
2102 if (bp->b_bcount > fs->lfs_bsize) {
2103 if (cbp->b_bcount > 0)
2104 /* Put in its own buffer */
2105 break;
2106 else {
2107 cbp->b_data = bp->b_data;
2108 }
2109 } else if (cbp->b_bcount == 0) {
2110 p = cbp->b_data = lfs_malloc(fs, CHUNKSIZE,
2111 LFS_NB_CLUSTER);
2112 cl->flags |= LFS_CL_MALLOC;
2113 }
2114 #ifdef DIAGNOSTIC
2115 if (dtosn(fs, dbtofsb(fs, bp->b_blkno +
2116 btodb(bp->b_bcount - 1))) !=
2117 sp->seg_number) {
2118 printf("blk size %d daddr %" PRIx64
2119 " not in seg %d\n",
2120 bp->b_bcount, bp->b_blkno,
2121 sp->seg_number);
2122 panic("segment overwrite");
2123 }
2124 #endif
2125
2126 #ifdef LFS_USE_B_INVAL
2127 /*
2128 * Fake buffers from the cleaner are marked as B_INVAL.
2129 * We need to copy the data from user space rather than
2130 * from the buffer indicated.
2131 * XXX == what do I do on an error?
2132 */
2133 if ((bp->b_flags & (B_CALL|B_INVAL)) ==
2134 (B_CALL|B_INVAL)) {
2135 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
2136 panic("lfs_writeseg: "
2137 "copyin failed [2]");
2138 } else
2139 #endif /* LFS_USE_B_INVAL */
2140 if (cl->flags & LFS_CL_MALLOC) {
2141 /* copy data into our cluster. */
2142 memcpy(p, bp->b_data, bp->b_bcount);
2143 p += bp->b_bcount;
2144 }
2145
2146 cbp->b_bcount += bp->b_bcount;
2147 cl->bufsize += bp->b_bcount;
2148
2149 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | B_DONE);
2150 cl->bpp[cl->bufcount++] = bp;
2151 vp = bp->b_vp;
2152 s = splbio();
2153 reassignbuf(bp, vp);
2154 V_INCR_NUMOUTPUT(vp);
2155 splx(s);
2156
2157 bpp++;
2158 i--;
2159 }
2160 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
2161 BIO_SETPRIO(cbp, BPRIO_TIMECRITICAL);
2162 else
2163 BIO_SETPRIO(cbp, BPRIO_TIMELIMITED);
2164 s = splbio();
2165 V_INCR_NUMOUTPUT(devvp);
2166 splx(s);
2167 VOP_STRATEGY(devvp, cbp);
2168 curproc->p_stats->p_ru.ru_oublock++;
2169 }
2170
2171 if (lfs_dostats) {
2172 ++lfs_stats.psegwrites;
2173 lfs_stats.blocktot += nblocks - 1;
2174 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
2175 ++lfs_stats.psyncwrites;
2176 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
2177 ++lfs_stats.pcleanwrites;
2178 lfs_stats.cleanblocks += nblocks - 1;
2179 }
2180 }
2181 return (lfs_initseg(fs) || do_again);
2182 }
2183
2184 void
2185 lfs_writesuper(struct lfs *fs, daddr_t daddr)
2186 {
2187 struct buf *bp;
2188 int s;
2189 struct vnode *devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2190
2191 ASSERT_MAYBE_SEGLOCK(fs);
2192 #ifdef DIAGNOSTIC
2193 KASSERT(fs->lfs_magic == LFS_MAGIC);
2194 #endif
2195 /*
2196 * If we can write one superblock while another is in
2197 * progress, we risk not having a complete checkpoint if we crash.
2198 * So, block here if a superblock write is in progress.
2199 */
2200 simple_lock(&fs->lfs_interlock);
2201 s = splbio();
2202 while (fs->lfs_sbactive) {
2203 ltsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0,
2204 &fs->lfs_interlock);
2205 }
2206 fs->lfs_sbactive = daddr;
2207 splx(s);
2208 simple_unlock(&fs->lfs_interlock);
2209
2210 /* Set timestamp of this version of the superblock */
2211 if (fs->lfs_version == 1)
2212 fs->lfs_otstamp = time.tv_sec;
2213 fs->lfs_tstamp = time.tv_sec;
2214
2215 /* Checksum the superblock and copy it into a buffer. */
2216 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
2217 bp = lfs_newbuf(fs, devvp,
2218 fsbtodb(fs, daddr), LFS_SBPAD, LFS_NB_SBLOCK);
2219 memset(bp->b_data + sizeof(struct dlfs), 0,
2220 LFS_SBPAD - sizeof(struct dlfs));
2221 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
2222
2223 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
2224 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
2225 bp->b_iodone = lfs_supercallback;
2226
2227 if (fs->lfs_sp != NULL && fs->lfs_sp->seg_flags & SEGM_SYNC)
2228 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
2229 else
2230 BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
2231 curproc->p_stats->p_ru.ru_oublock++;
2232 s = splbio();
2233 V_INCR_NUMOUTPUT(bp->b_vp);
2234 splx(s);
2235 simple_lock(&fs->lfs_interlock);
2236 ++fs->lfs_iocount;
2237 simple_unlock(&fs->lfs_interlock);
2238 VOP_STRATEGY(devvp, bp);
2239 }
2240
2241 /*
2242 * Logical block number match routines used when traversing the dirty block
2243 * chain.
2244 */
2245 int
2246 lfs_match_fake(struct lfs *fs, struct buf *bp)
2247 {
2248
2249 ASSERT_SEGLOCK(fs);
2250 return LFS_IS_MALLOC_BUF(bp);
2251 }
2252
2253 #if 0
2254 int
2255 lfs_match_real(struct lfs *fs, struct buf *bp)
2256 {
2257
2258 ASSERT_SEGLOCK(fs);
2259 return (lfs_match_data(fs, bp) && !lfs_match_fake(fs, bp));
2260 }
2261 #endif
2262
2263 int
2264 lfs_match_data(struct lfs *fs, struct buf *bp)
2265 {
2266
2267 ASSERT_SEGLOCK(fs);
2268 return (bp->b_lblkno >= 0);
2269 }
2270
2271 int
2272 lfs_match_indir(struct lfs *fs, struct buf *bp)
2273 {
2274 daddr_t lbn;
2275
2276 ASSERT_SEGLOCK(fs);
2277 lbn = bp->b_lblkno;
2278 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
2279 }
2280
2281 int
2282 lfs_match_dindir(struct lfs *fs, struct buf *bp)
2283 {
2284 daddr_t lbn;
2285
2286 ASSERT_SEGLOCK(fs);
2287 lbn = bp->b_lblkno;
2288 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
2289 }
2290
2291 int
2292 lfs_match_tindir(struct lfs *fs, struct buf *bp)
2293 {
2294 daddr_t lbn;
2295
2296 ASSERT_SEGLOCK(fs);
2297 lbn = bp->b_lblkno;
2298 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
2299 }
2300
2301 /*
2302 * XXX - The only buffers that are going to hit these functions are the
2303 * segment write blocks, or the segment summaries, or the superblocks.
2304 *
2305 * All of the above are created by lfs_newbuf, and so do not need to be
2306 * released via brelse.
2307 */
2308 void
2309 lfs_callback(struct buf *bp)
2310 {
2311 struct lfs *fs;
2312
2313 fs = bp->b_private;
2314 ASSERT_NO_SEGLOCK(fs);
2315 lfs_freebuf(fs, bp);
2316 }
2317
2318 static void
2319 lfs_super_aiodone(struct buf *bp)
2320 {
2321 struct lfs *fs;
2322
2323 fs = bp->b_private;
2324 ASSERT_NO_SEGLOCK(fs);
2325 simple_lock(&fs->lfs_interlock);
2326 fs->lfs_sbactive = 0;
2327 if (--fs->lfs_iocount <= 1)
2328 wakeup(&fs->lfs_iocount);
2329 simple_unlock(&fs->lfs_interlock);
2330 wakeup(&fs->lfs_sbactive);
2331 lfs_freebuf(fs, bp);
2332 }
2333
2334 static void
2335 lfs_cluster_aiodone(struct buf *bp)
2336 {
2337 struct lfs_cluster *cl;
2338 struct lfs *fs;
2339 struct buf *tbp, *fbp;
2340 struct vnode *vp, *devvp;
2341 struct inode *ip;
2342 int s, error=0;
2343
2344 if (bp->b_flags & B_ERROR)
2345 error = bp->b_error;
2346
2347 cl = bp->b_private;
2348 fs = cl->fs;
2349 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2350 ASSERT_NO_SEGLOCK(fs);
2351
2352 /* Put the pages back, and release the buffer */
2353 while (cl->bufcount--) {
2354 tbp = cl->bpp[cl->bufcount];
2355 KASSERT(tbp->b_flags & B_BUSY);
2356 if (error) {
2357 tbp->b_flags |= B_ERROR;
2358 tbp->b_error = error;
2359 }
2360
2361 /*
2362 * We're done with tbp. If it has not been re-dirtied since
2363 * the cluster was written, free it. Otherwise, keep it on
2364 * the locked list to be written again.
2365 */
2366 vp = tbp->b_vp;
2367
2368 tbp->b_flags &= ~B_GATHERED;
2369
2370 LFS_BCLEAN_LOG(fs, tbp);
2371
2372 if (!(tbp->b_flags & B_CALL)) {
2373 KASSERT(tbp->b_flags & B_LOCKED);
2374 s = splbio();
2375 simple_lock(&bqueue_slock);
2376 bremfree(tbp);
2377 simple_unlock(&bqueue_slock);
2378 if (vp)
2379 reassignbuf(tbp, vp);
2380 splx(s);
2381 tbp->b_flags |= B_ASYNC; /* for biodone */
2382 }
2383
2384 if ((tbp->b_flags & (B_LOCKED | B_DELWRI)) == B_LOCKED)
2385 LFS_UNLOCK_BUF(tbp);
2386
2387 if (tbp->b_flags & B_DONE) {
2388 DLOG((DLOG_SEG, "blk %d biodone already (flags %lx)\n",
2389 cl->bufcount, (long)tbp->b_flags));
2390 }
2391
2392 if ((tbp->b_flags & B_CALL) && !LFS_IS_MALLOC_BUF(tbp)) {
2393 /*
2394 * A buffer from the page daemon.
2395 * We use the same iodone as it does,
2396 * so we must manually disassociate its
2397 * buffers from the vp.
2398 */
2399 if (tbp->b_vp) {
2400 /* This is just silly */
2401 s = splbio();
2402 brelvp(tbp);
2403 tbp->b_vp = vp;
2404 splx(s);
2405 }
2406 /* Put it back the way it was */
2407 tbp->b_flags |= B_ASYNC;
2408 /* Master buffers have B_AGE */
2409 if (tbp->b_private == tbp)
2410 tbp->b_flags |= B_AGE;
2411 }
2412 s = splbio();
2413 biodone(tbp);
2414
2415 /*
2416 * If this is the last block for this vnode, but
2417 * there are other blocks on its dirty list,
2418 * set IN_MODIFIED/IN_CLEANING depending on what
2419 * sort of block. Only do this for our mount point,
2420 * not for, e.g., inode blocks that are attached to
2421 * the devvp.
2422 * XXX KS - Shouldn't we set *both* if both types
2423 * of blocks are present (traverse the dirty list?)
2424 */
2425 simple_lock(&global_v_numoutput_slock);
2426 if (vp != devvp && vp->v_numoutput == 0 &&
2427 (fbp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL) {
2428 ip = VTOI(vp);
2429 DLOG((DLOG_SEG, "lfs_cluster_aiodone: mark ino %d\n",
2430 ip->i_number));
2431 if (LFS_IS_MALLOC_BUF(fbp))
2432 LFS_SET_UINO(ip, IN_CLEANING);
2433 else
2434 LFS_SET_UINO(ip, IN_MODIFIED);
2435 }
2436 simple_unlock(&global_v_numoutput_slock);
2437 splx(s);
2438 wakeup(vp);
2439 }
2440
2441 /* Fix up the cluster buffer, and release it */
2442 if (cl->flags & LFS_CL_MALLOC)
2443 lfs_free(fs, bp->b_data, LFS_NB_CLUSTER);
2444 s = splbio();
2445 pool_put(&bufpool, bp); /* XXX should use lfs_free? */
2446 splx(s);
2447
2448 /* Note i/o done */
2449 if (cl->flags & LFS_CL_SYNC) {
2450 if (--cl->seg->seg_iocount == 0)
2451 wakeup(&cl->seg->seg_iocount);
2452 }
2453 simple_lock(&fs->lfs_interlock);
2454 #ifdef DIAGNOSTIC
2455 if (fs->lfs_iocount == 0)
2456 panic("lfs_cluster_aiodone: zero iocount");
2457 #endif
2458 if (--fs->lfs_iocount <= 1)
2459 wakeup(&fs->lfs_iocount);
2460 simple_unlock(&fs->lfs_interlock);
2461
2462 pool_put(&fs->lfs_bpppool, cl->bpp);
2463 cl->bpp = NULL;
2464 pool_put(&fs->lfs_clpool, cl);
2465 }
2466
2467 static void
2468 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
2469 {
2470 /* reset b_iodone for when this is a single-buf i/o. */
2471 bp->b_iodone = aiodone;
2472
2473 simple_lock(&uvm.aiodoned_lock); /* locks uvm.aio_done */
2474 TAILQ_INSERT_TAIL(&uvm.aio_done, bp, b_freelist);
2475 wakeup(&uvm.aiodoned);
2476 simple_unlock(&uvm.aiodoned_lock);
2477 }
2478
2479 static void
2480 lfs_cluster_callback(struct buf *bp)
2481 {
2482
2483 lfs_generic_callback(bp, lfs_cluster_aiodone);
2484 }
2485
2486 void
2487 lfs_supercallback(struct buf *bp)
2488 {
2489
2490 lfs_generic_callback(bp, lfs_super_aiodone);
2491 }
2492
2493 /*
2494 * Shellsort (diminishing increment sort) from Data Structures and
2495 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
2496 * see also Knuth Vol. 3, page 84. The increments are selected from
2497 * formula (8), page 95. Roughly O(N^3/2).
2498 */
2499 /*
2500 * This is our own private copy of shellsort because we want to sort
2501 * two parallel arrays (the array of buffer pointers and the array of
2502 * logical block numbers) simultaneously. Note that we cast the array
2503 * of logical block numbers to a unsigned in this routine so that the
2504 * negative block numbers (meta data blocks) sort AFTER the data blocks.
2505 */
2506
2507 void
2508 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb, int size)
2509 {
2510 static int __rsshell_increments[] = { 4, 1, 0 };
2511 int incr, *incrp, t1, t2;
2512 struct buf *bp_temp;
2513
2514 #ifdef DEBUG
2515 incr = 0;
2516 for (t1 = 0; t1 < nmemb; t1++) {
2517 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2518 if (lb_array[incr++] != bp_array[t1]->b_lblkno + t2) {
2519 /* dump before panic */
2520 printf("lfs_shellsort: nmemb=%d, size=%d\n",
2521 nmemb, size);
2522 incr = 0;
2523 for (t1 = 0; t1 < nmemb; t1++) {
2524 const struct buf *bp = bp_array[t1];
2525
2526 printf("bp[%d]: lbn=%" PRIu64 ", size=%"
2527 PRIu64 "\n", t1,
2528 (uint64_t)bp->b_bcount,
2529 (uint64_t)bp->b_lblkno);
2530 printf("lbns:");
2531 for (t2 = 0; t2 * size < bp->b_bcount;
2532 t2++) {
2533 printf(" %" PRId32,
2534 lb_array[incr++]);
2535 }
2536 printf("\n");
2537 }
2538 panic("lfs_shellsort: inconsistent input");
2539 }
2540 }
2541 }
2542 #endif
2543
2544 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
2545 for (t1 = incr; t1 < nmemb; ++t1)
2546 for (t2 = t1 - incr; t2 >= 0;)
2547 if ((u_int32_t)bp_array[t2]->b_lblkno >
2548 (u_int32_t)bp_array[t2 + incr]->b_lblkno) {
2549 bp_temp = bp_array[t2];
2550 bp_array[t2] = bp_array[t2 + incr];
2551 bp_array[t2 + incr] = bp_temp;
2552 t2 -= incr;
2553 } else
2554 break;
2555
2556 /* Reform the list of logical blocks */
2557 incr = 0;
2558 for (t1 = 0; t1 < nmemb; t1++) {
2559 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2560 lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
2561 }
2562 }
2563 }
2564
2565 /*
2566 * Call vget with LK_NOWAIT. If we are the one who holds VXLOCK/VFREEING,
2567 * however, we must press on. Just fake success in that case.
2568 */
2569 int
2570 lfs_vref(struct vnode *vp)
2571 {
2572 int error;
2573 struct lfs *fs;
2574
2575 fs = VTOI(vp)->i_lfs;
2576
2577 ASSERT_MAYBE_SEGLOCK(fs);
2578
2579 /*
2580 * If we return 1 here during a flush, we risk vinvalbuf() not
2581 * being able to flush all of the pages from this vnode, which
2582 * will cause it to panic. So, return 0 if a flush is in progress.
2583 */
2584 error = vget(vp, LK_NOWAIT);
2585 if (error == EBUSY && IS_FLUSHING(VTOI(vp)->i_lfs, vp)) {
2586 ++fs->lfs_flushvp_fakevref;
2587 return 0;
2588 }
2589 return error;
2590 }
2591
2592 /*
2593 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
2594 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
2595 */
2596 void
2597 lfs_vunref(struct vnode *vp)
2598 {
2599 struct lfs *fs;
2600
2601 fs = VTOI(vp)->i_lfs;
2602 ASSERT_MAYBE_SEGLOCK(fs);
2603
2604 /*
2605 * Analogous to lfs_vref, if the node is flushing, fake it.
2606 */
2607 if (IS_FLUSHING(fs, vp) && fs->lfs_flushvp_fakevref) {
2608 --fs->lfs_flushvp_fakevref;
2609 return;
2610 }
2611
2612 simple_lock(&vp->v_interlock);
2613 #ifdef DIAGNOSTIC
2614 if (vp->v_usecount <= 0) {
2615 printf("lfs_vunref: inum is %llu\n", (unsigned long long)
2616 VTOI(vp)->i_number);
2617 printf("lfs_vunref: flags are 0x%lx\n", (u_long)vp->v_flag);
2618 printf("lfs_vunref: usecount = %ld\n", (long)vp->v_usecount);
2619 panic("lfs_vunref: v_usecount < 0");
2620 }
2621 #endif
2622 vp->v_usecount--;
2623 if (vp->v_usecount > 0) {
2624 simple_unlock(&vp->v_interlock);
2625 return;
2626 }
2627 /*
2628 * insert at tail of LRU list
2629 */
2630 simple_lock(&vnode_free_list_slock);
2631 if (vp->v_holdcnt > 0)
2632 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2633 else
2634 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2635 simple_unlock(&vnode_free_list_slock);
2636 simple_unlock(&vp->v_interlock);
2637 }
2638
2639 /*
2640 * We use this when we have vnodes that were loaded in solely for cleaning.
2641 * There is no reason to believe that these vnodes will be referenced again
2642 * soon, since the cleaning process is unrelated to normal filesystem
2643 * activity. Putting cleaned vnodes at the tail of the list has the effect
2644 * of flushing the vnode LRU. So, put vnodes that were loaded only for
2645 * cleaning at the head of the list, instead.
2646 */
2647 void
2648 lfs_vunref_head(struct vnode *vp)
2649 {
2650
2651 ASSERT_SEGLOCK(VTOI(vp)->i_lfs);
2652 simple_lock(&vp->v_interlock);
2653 #ifdef DIAGNOSTIC
2654 if (vp->v_usecount == 0) {
2655 panic("lfs_vunref: v_usecount<0");
2656 }
2657 #endif
2658 vp->v_usecount--;
2659 if (vp->v_usecount > 0) {
2660 simple_unlock(&vp->v_interlock);
2661 return;
2662 }
2663 /*
2664 * insert at head of LRU list
2665 */
2666 simple_lock(&vnode_free_list_slock);
2667 if (vp->v_holdcnt > 0)
2668 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2669 else
2670 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2671 simple_unlock(&vnode_free_list_slock);
2672 simple_unlock(&vp->v_interlock);
2673 }
2674
2675
2676 /*
2677 * Set up an FINFO entry for a new file. The fip pointer is assumed to
2678 * point at uninitialized space.
2679 */
2680 void
2681 lfs_acquire_finfo(struct lfs *fs, ino_t ino, int vers)
2682 {
2683 struct segment *sp = fs->lfs_sp;
2684
2685 KASSERT(vers > 0);
2686
2687 if (sp->seg_bytes_left < fs->lfs_bsize ||
2688 sp->sum_bytes_left < sizeof(struct finfo))
2689 (void) lfs_writeseg(fs, fs->lfs_sp);
2690
2691 sp->sum_bytes_left -= FINFOSIZE;
2692 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
2693 sp->fip->fi_nblocks = 0;
2694 sp->fip->fi_ino = ino;
2695 sp->fip->fi_version = vers;
2696 }
2697
2698 /*
2699 * Release the FINFO entry, either clearing out an unused entry or
2700 * advancing us to the next available entry.
2701 */
2702 void
2703 lfs_release_finfo(struct lfs *fs)
2704 {
2705 struct segment *sp = fs->lfs_sp;
2706
2707 if (sp->fip->fi_nblocks != 0) {
2708 sp->fip = (FINFO*)((caddr_t)sp->fip + FINFOSIZE +
2709 sizeof(int32_t) * sp->fip->fi_nblocks);
2710 sp->start_lbp = &sp->fip->fi_blocks[0];
2711 } else {
2712 sp->sum_bytes_left += FINFOSIZE;
2713 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
2714 }
2715 }
2716