lfs_rfw.c revision 1.26 1 /* $NetBSD: lfs_rfw.c,v 1.26 2015/08/12 18:25:52 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: lfs_rfw.c,v 1.26 2015/08/12 18:25:52 dholland Exp $");
34
35 #if defined(_KERNEL_OPT)
36 #include "opt_quota.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/proc.h>
43 #include <sys/kernel.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/kthread.h>
47 #include <sys/buf.h>
48 #include <sys/device.h>
49 #include <sys/mbuf.h>
50 #include <sys/file.h>
51 #include <sys/disklabel.h>
52 #include <sys/ioctl.h>
53 #include <sys/errno.h>
54 #include <sys/malloc.h>
55 #include <sys/pool.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
58 #include <uvm/uvm_extern.h>
59 #include <sys/sysctl.h>
60 #include <sys/conf.h>
61 #include <sys/kauth.h>
62
63 #include <miscfs/specfs/specdev.h>
64
65 #include <ufs/lfs/ulfs_quotacommon.h>
66 #include <ufs/lfs/ulfs_inode.h>
67 #include <ufs/lfs/ulfsmount.h>
68 #include <ufs/lfs/ulfs_extern.h>
69
70 #include <uvm/uvm.h>
71 #include <uvm/uvm_stat.h>
72 #include <uvm/uvm_pager.h>
73 #include <uvm/uvm_pdaemon.h>
74
75 #include <ufs/lfs/lfs.h>
76 #include <ufs/lfs/lfs_accessors.h>
77 #include <ufs/lfs/lfs_kernel.h>
78 #include <ufs/lfs/lfs_extern.h>
79
80 #include <miscfs/genfs/genfs.h>
81 #include <miscfs/genfs/genfs_node.h>
82
83 /*
84 * Roll-forward code.
85 */
86 static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
87 kauth_cred_t, int, int *, struct lwp *);
88
89 extern int lfs_do_rfw;
90
91 /*
92 * Allocate a particular inode with a particular version number, freeing
93 * any previous versions of this inode that may have gone before.
94 * Used by the roll-forward code.
95 *
96 * XXX this function does not have appropriate locking to be used on a live fs;
97 * XXX but something similar could probably be used for an "undelete" call.
98 *
99 * Called with the Ifile inode locked.
100 */
101 int
102 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
103 struct vnode **vpp)
104 {
105 struct vattr va;
106 struct vnode *vp;
107 struct inode *ip;
108 int error;
109
110 ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
111
112 /*
113 * First, just try a vget. If the version number is the one we want,
114 * we don't have to do anything else. If the version number is wrong,
115 * take appropriate action.
116 */
117 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
118 if (error == 0) {
119 DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
120
121 *vpp = vp;
122 ip = VTOI(vp);
123 if (ip->i_gen == vers)
124 return 0;
125 else if (ip->i_gen < vers) {
126 lfs_truncate(vp, (off_t)0, 0, NOCRED);
127 ip->i_gen = ip->i_ffs1_gen = vers;
128 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
129 return 0;
130 } else {
131 DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
132 ino, vers, ip->i_ffs1_gen));
133 vput(vp);
134 *vpp = NULLVP;
135 return EEXIST;
136 }
137 }
138
139 /* Not found, create as regular file. */
140 vattr_null(&va);
141 va.va_type = VREG;
142 va.va_mode = 0;
143 va.va_fileid = ino;
144 va.va_gen = vers;
145 error = vcache_new(fs->lfs_ivnode->v_mount, NULL, &va, NOCRED, &vp);
146 if (error)
147 return error;
148 error = vn_lock(vp, LK_EXCLUSIVE);
149 if (error) {
150 vrele(vp);
151 *vpp = NULLVP;
152 return error;
153 }
154 ip = VTOI(vp);
155 ip->i_nlink = ip->i_ffs1_nlink = 1;
156 *vpp = vp;
157 return 0;
158 }
159
160 /*
161 * Load the appropriate indirect block, and change the appropriate pointer.
162 * Mark the block dirty. Do segment and avail accounting.
163 */
164 static int
165 update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
166 daddr_t ndaddr, size_t size, struct lwp *l)
167 {
168 int error;
169 struct vnode *vp;
170 struct inode *ip;
171 #ifdef DEBUG
172 daddr_t odaddr;
173 struct indir a[ULFS_NIADDR];
174 int num;
175 int i;
176 #endif /* DEBUG */
177 struct buf *bp;
178 SEGUSE *sup;
179
180 KASSERT(lbn >= 0); /* no indirect blocks */
181
182 if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
183 DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
184 " returned %d\n", ino, error));
185 return error;
186 }
187
188 if ((error = lfs_balloc(vp, (lbn << lfs_sb_getbshift(fs)), size,
189 NOCRED, 0, &bp)) != 0) {
190 vput(vp);
191 return (error);
192 }
193 /* No need to write, the block is already on disk */
194 if (bp->b_oflags & BO_DELWRI) {
195 LFS_UNLOCK_BUF(bp);
196 lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
197 /* XXX should this wake up fs->lfs_availsleep? */
198 }
199 brelse(bp, BC_INVAL);
200
201 /*
202 * Extend the file, if it is not large enough already.
203 * XXX this is not exactly right, we don't know how much of the
204 * XXX last block is actually used. We hope that an inode will
205 * XXX appear later to give the correct size.
206 */
207 ip = VTOI(vp);
208 if (ip->i_size <= (lbn << lfs_sb_getbshift(fs))) {
209 u_int64_t newsize;
210
211 if (lbn < ULFS_NDADDR)
212 newsize = ip->i_ffs1_size = (lbn << lfs_sb_getbshift(fs)) +
213 (size - lfs_sb_getfsize(fs)) + 1;
214 else
215 newsize = ip->i_ffs1_size = (lbn << lfs_sb_getbshift(fs)) + 1;
216
217 if (ip->i_size < newsize) {
218 ip->i_size = newsize;
219 /*
220 * tell vm our new size for the case the inode won't
221 * appear later.
222 */
223 uvm_vnp_setsize(vp, newsize);
224 }
225 }
226
227 lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
228
229 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
230 sup->su_nbytes += size;
231 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
232
233 /* differences here should be due to UNWRITTEN indirect blocks. */
234 KASSERT((lfs_lblkno(fs, ip->i_size) > ULFS_NDADDR &&
235 ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
236 ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
237
238 #ifdef DEBUG
239 /* Now look again to make sure it worked */
240 ulfs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
241 for (i = num; i > 0; i--) {
242 if (!a[i].in_exists)
243 panic("update_meta: absent %d lv indirect block", i);
244 }
245 if (LFS_DBTOFSB(fs, odaddr) != ndaddr)
246 DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
247 PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
248 #endif /* DEBUG */
249 vput(vp);
250 return 0;
251 }
252
253 static int
254 update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
255 struct lwp *l)
256 {
257 struct vnode *devvp, *vp;
258 struct inode *ip;
259 struct ulfs1_dinode *dip;
260 struct buf *dbp, *ibp;
261 int error;
262 daddr_t daddr;
263 IFILE *ifp;
264 SEGUSE *sup;
265
266 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
267
268 /*
269 * Get the inode, update times and perms.
270 * DO NOT update disk blocks, we do that separately.
271 */
272 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getibsize(fs),
273 0, &dbp);
274 if (error) {
275 DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
276 return error;
277 }
278 dip = ((struct ulfs1_dinode *)(dbp->b_data)) + LFS_INOPB(fs);
279 while (--dip >= (struct ulfs1_dinode *)dbp->b_data) {
280 if (dip->di_inumber > LFS_IFILE_INUM) {
281 error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
282 l, &vp);
283 if (error) {
284 DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
285 " returned %d\n", error));
286 continue;
287 }
288 ip = VTOI(vp);
289 if (dip->di_size != ip->i_size)
290 lfs_truncate(vp, dip->di_size, 0, NOCRED);
291 /* Get mode, link count, size, and times */
292 memcpy(ip->i_din.ffs1_din, dip,
293 offsetof(struct ulfs1_dinode, di_db[0]));
294
295 /* Then the rest, except di_blocks */
296 ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
297 ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
298 ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
299 ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
300
301 ip->i_mode = ip->i_ffs1_mode;
302 ip->i_nlink = ip->i_ffs1_nlink;
303 ip->i_size = ip->i_ffs1_size;
304
305 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
306
307 /* Re-initialize to get type right */
308 ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
309 &vp);
310 vput(vp);
311
312 /* Record change in location */
313 LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
314 daddr = lfs_if_getdaddr(fs, ifp);
315 lfs_if_setdaddr(fs, ifp, LFS_DBTOFSB(fs, dbp->b_blkno));
316 error = LFS_BWRITE_LOG(ibp); /* Ifile */
317 /* And do segment accounting */
318 if (lfs_dtosn(fs, daddr) != lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno))) {
319 if (daddr > 0) {
320 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, daddr),
321 ibp);
322 sup->su_nbytes -= sizeof (struct ulfs1_dinode);
323 LFS_WRITESEGENTRY(sup, fs,
324 lfs_dtosn(fs, daddr),
325 ibp);
326 }
327 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
328 ibp);
329 sup->su_nbytes += sizeof (struct ulfs1_dinode);
330 LFS_WRITESEGENTRY(sup, fs,
331 lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
332 ibp);
333 }
334 }
335 }
336 brelse(dbp, BC_AGE);
337
338 return 0;
339 }
340
341 #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */
342 #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */
343
344 static daddr_t
345 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
346 kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
347 {
348 struct vnode *devvp;
349 struct buf *bp, *dbp;
350 int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
351 SEGSUM *ssp;
352 u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
353 daddr_t oldoffset;
354 int32_t *iaddr; /* XXX ondisk32 */
355 FINFO *fip;
356 SEGUSE *sup;
357 size_t size;
358
359 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
360 /*
361 * If the segment has a superblock and we're at the top
362 * of the segment, skip the superblock.
363 */
364 if (lfs_sntod(fs, lfs_dtosn(fs, offset)) == offset) {
365 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
366 if (sup->su_flags & SEGUSE_SUPERBLOCK)
367 offset += lfs_btofsb(fs, LFS_SBPAD);
368 brelse(bp, 0);
369 }
370
371 /* Read in the segment summary */
372 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getsumsize(fs),
373 0, &bp);
374 if (error)
375 return -1;
376
377 /* Check summary checksum */
378 ssp = (SEGSUM *)bp->b_data;
379 if (flags & CHECK_CKSUM) {
380 if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
381 lfs_sb_getsumsize(fs) -
382 sizeof(ssp->ss_sumsum))) {
383 DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
384 offset = -1;
385 goto err1;
386 }
387 if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
388 DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
389 offset = -1;
390 goto err1;
391 }
392 if (ssp->ss_create < lfs_sb_gettstamp(fs)) {
393 DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
394 offset = -1;
395 goto err1;
396 }
397 }
398 if (lfs_sb_getversion(fs) > 1) {
399 if (ssp->ss_serial != nextserial) {
400 DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
401 "\n", offset));
402 offset = -1;
403 goto err1;
404 }
405 if (ssp->ss_ident != lfs_sb_getident(fs)) {
406 DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
407 PRIx64 "\n", ssp->ss_ident, lfs_sb_getident(fs), offset));
408 offset = -1;
409 goto err1;
410 }
411 }
412 if (pseg_flags)
413 *pseg_flags = ssp->ss_flags;
414 oldoffset = offset;
415 offset += lfs_btofsb(fs, lfs_sb_getsumsize(fs));
416
417 ninos = howmany(ssp->ss_ninos, LFS_INOPB(fs));
418 /* XXX ondisk32 */
419 iaddr = (int32_t *)((char*)bp->b_data + lfs_sb_getsumsize(fs) - sizeof(int32_t));
420 if (flags & CHECK_CKSUM) {
421 /* Count blocks */
422 nblocks = 0;
423 fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
424 for (i = 0; i < ssp->ss_nfinfo; ++i) {
425 nblocks += fip->fi_nblocks;
426 if (fip->fi_nblocks <= 0)
427 break;
428 /* XXX ondisk32 */
429 fip = (FINFO *)(((char *)fip) + FINFOSIZE +
430 (fip->fi_nblocks * sizeof(int32_t)));
431 }
432 nblocks += ninos;
433 /* Create the sum array */
434 datap = dp = malloc(nblocks * sizeof(u_long),
435 M_SEGMENT, M_WAITOK);
436 }
437
438 /* Handle individual blocks */
439 fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
440 for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
441 /* Inode block? */
442 if (ninos && *iaddr == offset) {
443 if (flags & CHECK_CKSUM) {
444 /* Read in the head and add to the buffer */
445 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getbsize(fs),
446 0, &dbp);
447 if (error) {
448 offset = -1;
449 goto err2;
450 }
451 (*dp++) = ((u_long *)(dbp->b_data))[0];
452 brelse(dbp, BC_AGE);
453 }
454 if (flags & CHECK_UPDATE) {
455 if ((error = update_inoblk(fs, offset, cred, l))
456 != 0) {
457 offset = -1;
458 goto err2;
459 }
460 }
461 offset += lfs_btofsb(fs, lfs_sb_getibsize(fs));
462 --iaddr;
463 --ninos;
464 --i; /* compensate */
465 continue;
466 }
467 size = lfs_sb_getbsize(fs);
468 for (j = 0; j < fip->fi_nblocks; ++j) {
469 if (j == fip->fi_nblocks - 1)
470 size = fip->fi_lastlength;
471 if (flags & CHECK_CKSUM) {
472 error = bread(devvp, LFS_FSBTODB(fs, offset), size,
473 0, &dbp);
474 if (error) {
475 offset = -1;
476 goto err2;
477 }
478 (*dp++) = ((u_long *)(dbp->b_data))[0];
479 brelse(dbp, BC_AGE);
480 }
481 /* Account for and update any direct blocks */
482 if ((flags & CHECK_UPDATE) &&
483 fip->fi_ino > LFS_IFILE_INUM &&
484 fip->fi_blocks[j] >= 0) {
485 update_meta(fs, fip->fi_ino, fip->fi_version,
486 fip->fi_blocks[j], offset, size, l);
487 }
488 offset += lfs_btofsb(fs, size);
489 }
490 /* XXX ondisk32 */
491 fip = (FINFO *)(((char *)fip) + FINFOSIZE
492 + fip->fi_nblocks * sizeof(int32_t));
493 }
494 /* Checksum the array, compare */
495 if ((flags & CHECK_CKSUM) &&
496 ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
497 {
498 DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
499 " (wanted %x got %x)\n",
500 offset, ssp->ss_datasum, cksum(datap, nblocks *
501 sizeof(u_long))));
502 offset = -1;
503 goto err2;
504 }
505
506 /* If we're at the end of the segment, move to the next */
507 if (lfs_dtosn(fs, offset + lfs_btofsb(fs, lfs_sb_getsumsize(fs) + lfs_sb_getbsize(fs))) !=
508 lfs_dtosn(fs, offset)) {
509 if (lfs_dtosn(fs, offset) == lfs_dtosn(fs, ssp->ss_next)) {
510 offset = -1;
511 goto err2;
512 }
513 offset = ssp->ss_next;
514 DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
515 " -> segment %d\n", offset, lfs_dtosn(fs,offset)));
516 }
517
518 if (flags & CHECK_UPDATE) {
519 lfs_sb_subavail(fs, offset - oldoffset);
520 /* Don't clog the buffer queue */
521 mutex_enter(&lfs_lock);
522 if (locked_queue_count > LFS_MAX_BUFS ||
523 locked_queue_bytes > LFS_MAX_BYTES) {
524 lfs_flush(fs, SEGM_CKP, 0);
525 }
526 mutex_exit(&lfs_lock);
527 }
528
529 err2:
530 if (flags & CHECK_CKSUM)
531 free(datap, M_SEGMENT);
532 err1:
533 brelse(bp, BC_AGE);
534
535 /* XXX should we update the serial number even for bad psegs? */
536 if ((flags & CHECK_UPDATE) && offset > 0 && lfs_sb_getversion(fs) > 1)
537 lfs_sb_setserial(fs, nextserial);
538 return offset;
539 }
540
541 void
542 lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
543 {
544 int flags, dirty;
545 daddr_t offset, oldoffset, lastgoodpseg;
546 int sn, curseg, do_rollforward;
547 struct proc *p;
548 kauth_cred_t cred;
549 SEGUSE *sup;
550 struct buf *bp;
551
552 p = l ? l->l_proc : NULL;
553 cred = p ? p->p_cred : NOCRED;
554
555 /*
556 * Roll forward.
557 *
558 * We don't roll forward for v1 filesystems, because
559 * of the danger that the clock was turned back between the last
560 * checkpoint and crash. This would roll forward garbage.
561 *
562 * v2 filesystems don't have this problem because they use a
563 * monotonically increasing serial number instead of a timestamp.
564 */
565 do_rollforward = (!(lfs_sb_getpflags(fs) & LFS_PF_CLEAN) &&
566 lfs_do_rfw && lfs_sb_getversion(fs) > 1 && p != NULL);
567 if (do_rollforward) {
568 u_int64_t nextserial;
569 /*
570 * Phase I: Find the address of the last good partial
571 * segment that was written after the checkpoint. Mark
572 * the segments in question dirty, so they won't be
573 * reallocated.
574 */
575 lastgoodpseg = oldoffset = offset = lfs_sb_getoffset(fs);
576 flags = 0x0;
577 DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
578 PRIx64 "\n", offset));
579 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
580 if (!(sup->su_flags & SEGUSE_DIRTY))
581 lfs_sb_subnclean(fs, 1);
582 sup->su_flags |= SEGUSE_DIRTY;
583 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
584 nextserial = lfs_sb_getserial(fs) + 1;
585 while ((offset = check_segsum(fs, offset, nextserial,
586 cred, CHECK_CKSUM, &flags, l)) > 0) {
587 nextserial++;
588 if (lfs_sntod(fs, oldoffset) != lfs_sntod(fs, offset)) {
589 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
590 bp);
591 if (!(sup->su_flags & SEGUSE_DIRTY))
592 lfs_sb_subnclean(fs, 1);
593 sup->su_flags |= SEGUSE_DIRTY;
594 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
595 bp);
596 }
597
598 DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
599 PRIx64 "\n", offset));
600 if (flags & SS_DIROP) {
601 DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
602 PRIx64 "\n", oldoffset));
603 if (!(flags & SS_CONT)) {
604 DLOG((DLOG_RF, "lfs_mountfs: dirops end "
605 "at 0x%" PRIx64 "\n", oldoffset));
606 }
607 }
608 if (!(flags & SS_CONT))
609 lastgoodpseg = offset;
610 oldoffset = offset;
611 }
612 if (flags & SS_CONT) {
613 DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
614 "dirops discarded\n"));
615 }
616 DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
617 "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
618 oldoffset = lfs_sb_getoffset(fs);
619 if (lfs_sb_getoffset(fs) != lastgoodpseg) {
620 /* Don't overwrite what we're trying to preserve */
621 offset = lfs_sb_getoffset(fs);
622 lfs_sb_setoffset(fs, lastgoodpseg);
623 lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_dtosn(fs, lfs_sb_getoffset(fs))));
624 for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs));;) {
625 sn = (sn + 1) % lfs_sb_getnseg(fs);
626 if (sn == curseg)
627 panic("lfs_mountfs: no clean segments");
628 LFS_SEGENTRY(sup, fs, sn, bp);
629 dirty = (sup->su_flags & SEGUSE_DIRTY);
630 brelse(bp, 0);
631 if (!dirty)
632 break;
633 }
634 lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
635
636 /*
637 * Phase II: Roll forward from the first superblock.
638 */
639 while (offset != lastgoodpseg) {
640 DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
641 PRIx64 "\n", offset));
642 offset = check_segsum(fs, offset,
643 lfs_sb_getserial(fs) + 1, cred, CHECK_UPDATE,
644 NULL, l);
645 }
646
647 /*
648 * Finish: flush our changes to disk.
649 */
650 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
651 DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
652 "recovered %jd blocks\n",
653 (intmax_t)(lastgoodpseg - oldoffset)));
654 }
655 DLOG((DLOG_RF, "LFS roll forward complete\n"));
656 }
657 }
658