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