lfs_rfw.c revision 1.2.4.2 1 /* $NetBSD: lfs_rfw.c,v 1.2.4.2 2006/08/11 15:47:37 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifdef LFS_KERNEL_RFW
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: lfs_rfw.c,v 1.2.4.2 2006/08/11 15:47:37 yamt Exp $");
43
44 #if defined(_KERNEL_OPT)
45 #include "opt_quota.h"
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/namei.h>
51 #include <sys/proc.h>
52 #include <sys/kernel.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/kthread.h>
56 #include <sys/buf.h>
57 #include <sys/device.h>
58 #include <sys/mbuf.h>
59 #include <sys/file.h>
60 #include <sys/disklabel.h>
61 #include <sys/ioctl.h>
62 #include <sys/errno.h>
63 #include <sys/malloc.h>
64 #include <sys/pool.h>
65 #include <sys/socket.h>
66 #include <sys/syslog.h>
67 #include <uvm/uvm_extern.h>
68 #include <sys/sysctl.h>
69 #include <sys/conf.h>
70 #include <sys/kauth.h>
71
72 #include <miscfs/specfs/specdev.h>
73
74 #include <ufs/ufs/quota.h>
75 #include <ufs/ufs/inode.h>
76 #include <ufs/ufs/ufsmount.h>
77 #include <ufs/ufs/ufs_extern.h>
78
79 #include <uvm/uvm.h>
80 #include <uvm/uvm_stat.h>
81 #include <uvm/uvm_pager.h>
82 #include <uvm/uvm_pdaemon.h>
83
84 #include <ufs/lfs/lfs.h>
85 #include <ufs/lfs/lfs_extern.h>
86
87 #include <miscfs/genfs/genfs.h>
88 #include <miscfs/genfs/genfs_node.h>
89
90 /*
91 * Roll-forward code.
92 */
93 static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
94 kauth_cred_t, int, int *, struct lwp *);
95
96 /*
97 * Allocate a particular inode with a particular version number, freeing
98 * any previous versions of this inode that may have gone before.
99 * Used by the roll-forward code.
100 *
101 * XXX this function does not have appropriate locking to be used on a live fs;
102 * XXX but something similar could probably be used for an "undelete" call.
103 *
104 * Called with the Ifile inode locked.
105 */
106 int
107 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
108 struct vnode **vpp)
109 {
110 IFILE *ifp;
111 struct buf *bp, *cbp;
112 struct vnode *vp;
113 struct inode *ip;
114 ino_t tino, oldnext;
115 int error;
116 CLEANERINFO *cip;
117
118 ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
119
120 /*
121 * First, just try a vget. If the version number is the one we want,
122 * we don't have to do anything else. If the version number is wrong,
123 * take appropriate action.
124 */
125 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
126 if (error == 0) {
127 DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
128
129 *vpp = vp;
130 ip = VTOI(vp);
131 if (ip->i_gen == vers)
132 return 0;
133 else if (ip->i_gen < vers) {
134 lfs_truncate(vp, (off_t)0, 0, NOCRED, l);
135 ip->i_gen = ip->i_ffs1_gen = vers;
136 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
137 return 0;
138 } else {
139 DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
140 ino, vers, ip->i_ffs1_gen));
141 vput(vp);
142 *vpp = NULLVP;
143 return EEXIST;
144 }
145 }
146
147 /*
148 * The inode is not in use. Find it on the free list.
149 */
150 /* If the Ifile is too short to contain this inum, extend it */
151 while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
152 fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
153 << fs->lfs_bshift) {
154 lfs_extend_ifile(fs, NOCRED);
155 }
156
157 LFS_IENTRY(ifp, fs, ino, bp);
158 oldnext = ifp->if_nextfree;
159 ifp->if_version = vers;
160 brelse(bp);
161
162 LFS_GET_HEADFREE(fs, cip, cbp, &ino);
163 if (ino) {
164 LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
165 } else {
166 tino = ino;
167 while (1) {
168 LFS_IENTRY(ifp, fs, tino, bp);
169 if (ifp->if_nextfree == ino ||
170 ifp->if_nextfree == LFS_UNUSED_INUM)
171 break;
172 tino = ifp->if_nextfree;
173 brelse(bp);
174 }
175 if (ifp->if_nextfree == LFS_UNUSED_INUM) {
176 brelse(bp);
177 return ENOENT;
178 }
179 ifp->if_nextfree = oldnext;
180 LFS_BWRITE_LOG(bp);
181 }
182
183 error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
184 if (error == 0) {
185 /*
186 * Make it VREG so we can put blocks on it. We will change
187 * this later if it turns out to be some other kind of file.
188 */
189 ip = VTOI(vp);
190 ip->i_mode = ip->i_ffs1_mode = IFREG;
191 ip->i_nlink = ip->i_ffs1_nlink = 1;
192 ip->i_ffs_effnlink = 1;
193 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
194 ip = VTOI(vp);
195
196 DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
197
198 /* The dirop-nature of this vnode is past */
199 lfs_unmark_vnode(vp);
200 (void)lfs_vunref(vp);
201 vp->v_flag &= ~VDIROP;
202 simple_lock(&fs->lfs_interlock);
203 simple_lock(&lfs_subsys_lock);
204 --lfs_dirvcount;
205 simple_unlock(&lfs_subsys_lock);
206 --fs->lfs_dirvcount;
207 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
208 wakeup(&lfs_dirvcount);
209 wakeup(&fs->lfs_dirvcount);
210 simple_unlock(&fs->lfs_interlock);
211 }
212 *vpp = vp;
213 return error;
214 }
215
216 /*
217 * Load the appropriate indirect block, and change the appropriate pointer.
218 * Mark the block dirty. Do segment and avail accounting.
219 */
220 static int
221 update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
222 daddr_t ndaddr, size_t size, struct lwp *l)
223 {
224 int error;
225 struct vnode *vp;
226 struct inode *ip;
227 #ifdef DEBUG
228 daddr_t odaddr;
229 struct indir a[NIADDR];
230 int num;
231 int i;
232 #endif /* DEBUG */
233 struct buf *bp;
234 SEGUSE *sup;
235
236 KASSERT(lbn >= 0); /* no indirect blocks */
237
238 if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
239 DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
240 " returned %d\n", ino, error));
241 return error;
242 }
243
244 if ((error = lfs_balloc(vp, (lbn << fs->lfs_bshift), size,
245 NOCRED, 0, &bp)) != 0) {
246 vput(vp);
247 return (error);
248 }
249 /* No need to write, the block is already on disk */
250 if (bp->b_flags & B_DELWRI) {
251 LFS_UNLOCK_BUF(bp);
252 fs->lfs_avail += btofsb(fs, bp->b_bcount);
253 }
254 bp->b_flags |= B_INVAL;
255 brelse(bp);
256
257 /*
258 * Extend the file, if it is not large enough already.
259 * XXX this is not exactly right, we don't know how much of the
260 * XXX last block is actually used. We hope that an inode will
261 * XXX appear later to give the correct size.
262 */
263 ip = VTOI(vp);
264 if (ip->i_size <= (lbn << fs->lfs_bshift)) {
265 u_int64_t newsize;
266
267 if (lbn < NDADDR)
268 newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) +
269 (size - fs->lfs_fsize) + 1;
270 else
271 newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) + 1;
272
273 if (ip->i_size < newsize) {
274 ip->i_size = newsize;
275 /*
276 * tell vm our new size for the case the inode won't
277 * appear later.
278 */
279 uvm_vnp_setsize(vp, newsize);
280 }
281 }
282
283 lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
284
285 LFS_SEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
286 sup->su_nbytes += size;
287 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
288
289 /* differences here should be due to UNWRITTEN indirect blocks. */
290 KASSERT((lblkno(fs, ip->i_size) > NDADDR &&
291 ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
292 ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
293
294 #ifdef DEBUG
295 /* Now look again to make sure it worked */
296 ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
297 for (i = num; i > 0; i--) {
298 if (!a[i].in_exists)
299 panic("update_meta: absent %d lv indirect block", i);
300 }
301 if (dbtofsb(fs, odaddr) != ndaddr)
302 DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
303 PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
304 #endif /* DEBUG */
305 vput(vp);
306 return 0;
307 }
308
309 static int
310 update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
311 struct lwp *l)
312 {
313 struct vnode *devvp, *vp;
314 struct inode *ip;
315 struct ufs1_dinode *dip;
316 struct buf *dbp, *ibp;
317 int error;
318 daddr_t daddr;
319 IFILE *ifp;
320 SEGUSE *sup;
321
322 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
323
324 /*
325 * Get the inode, update times and perms.
326 * DO NOT update disk blocks, we do that separately.
327 */
328 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_ibsize, cred, &dbp);
329 if (error) {
330 DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
331 return error;
332 }
333 dip = ((struct ufs1_dinode *)(dbp->b_data)) + INOPB(fs);
334 while (--dip >= (struct ufs1_dinode *)dbp->b_data) {
335 if (dip->di_inumber > LFS_IFILE_INUM) {
336 error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
337 l, &vp);
338 if (error) {
339 DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
340 " returned %d\n", error));
341 continue;
342 }
343 ip = VTOI(vp);
344 if (dip->di_size != ip->i_size)
345 lfs_truncate(vp, dip->di_size, 0, NOCRED, l);
346 /* Get mode, link count, size, and times */
347 memcpy(ip->i_din.ffs1_din, dip,
348 offsetof(struct ufs1_dinode, di_db[0]));
349
350 /* Then the rest, except di_blocks */
351 ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
352 ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
353 ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
354 ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
355
356 ip->i_mode = ip->i_ffs1_mode;
357 ip->i_nlink = ip->i_ffs_effnlink = ip->i_ffs1_nlink;
358 ip->i_size = ip->i_ffs1_size;
359
360 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
361
362 /* Re-initialize to get type right */
363 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
364 &vp);
365 vput(vp);
366
367 /* Record change in location */
368 LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
369 daddr = ifp->if_daddr;
370 ifp->if_daddr = dbtofsb(fs, dbp->b_blkno);
371 error = LFS_BWRITE_LOG(ibp); /* Ifile */
372 /* And do segment accounting */
373 if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) {
374 if (daddr > 0) {
375 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr),
376 ibp);
377 sup->su_nbytes -= sizeof (struct ufs1_dinode);
378 LFS_WRITESEGENTRY(sup, fs,
379 dtosn(fs, daddr),
380 ibp);
381 }
382 LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
383 ibp);
384 sup->su_nbytes += sizeof (struct ufs1_dinode);
385 LFS_WRITESEGENTRY(sup, fs,
386 dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
387 ibp);
388 }
389 }
390 }
391 dbp->b_flags |= B_AGE;
392 brelse(dbp);
393
394 return 0;
395 }
396
397 #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */
398 #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */
399
400 static daddr_t
401 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
402 kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
403 {
404 struct vnode *devvp;
405 struct buf *bp, *dbp;
406 int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
407 SEGSUM *ssp;
408 u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
409 daddr_t oldoffset;
410 int32_t *iaddr; /* XXX ondisk32 */
411 FINFO *fip;
412 SEGUSE *sup;
413 size_t size;
414
415 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
416 /*
417 * If the segment has a superblock and we're at the top
418 * of the segment, skip the superblock.
419 */
420 if (sntod(fs, dtosn(fs, offset)) == offset) {
421 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
422 if (sup->su_flags & SEGUSE_SUPERBLOCK)
423 offset += btofsb(fs, LFS_SBPAD);
424 brelse(bp);
425 }
426
427 /* Read in the segment summary */
428 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_sumsize, cred, &bp);
429 if (error)
430 return -1;
431
432 /* Check summary checksum */
433 ssp = (SEGSUM *)bp->b_data;
434 if (flags & CHECK_CKSUM) {
435 if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
436 fs->lfs_sumsize -
437 sizeof(ssp->ss_sumsum))) {
438 DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
439 offset = -1;
440 goto err1;
441 }
442 if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
443 DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
444 offset = -1;
445 goto err1;
446 }
447 if (ssp->ss_create < fs->lfs_tstamp) {
448 DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
449 offset = -1;
450 goto err1;
451 }
452 }
453 if (fs->lfs_version > 1) {
454 if (ssp->ss_serial != nextserial) {
455 DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
456 "\n", offset));
457 offset = -1;
458 goto err1;
459 }
460 if (ssp->ss_ident != fs->lfs_ident) {
461 DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
462 PRIx64 "\n", ssp->ss_ident, fs->lfs_ident, offset));
463 offset = -1;
464 goto err1;
465 }
466 }
467 if (pseg_flags)
468 *pseg_flags = ssp->ss_flags;
469 oldoffset = offset;
470 offset += btofsb(fs, fs->lfs_sumsize);
471
472 ninos = howmany(ssp->ss_ninos, INOPB(fs));
473 /* XXX ondisk32 */
474 iaddr = (int32_t *)(bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
475 if (flags & CHECK_CKSUM) {
476 /* Count blocks */
477 nblocks = 0;
478 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
479 for (i = 0; i < ssp->ss_nfinfo; ++i) {
480 nblocks += fip->fi_nblocks;
481 if (fip->fi_nblocks <= 0)
482 break;
483 /* XXX ondisk32 */
484 fip = (FINFO *)(((char *)fip) + FINFOSIZE +
485 (fip->fi_nblocks * sizeof(int32_t)));
486 }
487 nblocks += ninos;
488 /* Create the sum array */
489 datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
490 M_SEGMENT, M_WAITOK);
491 }
492
493 /* Handle individual blocks */
494 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
495 for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
496 /* Inode block? */
497 if (ninos && *iaddr == offset) {
498 if (flags & CHECK_CKSUM) {
499 /* Read in the head and add to the buffer */
500 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize,
501 cred, &dbp);
502 if (error) {
503 offset = -1;
504 goto err2;
505 }
506 (*dp++) = ((u_long *)(dbp->b_data))[0];
507 dbp->b_flags |= B_AGE;
508 brelse(dbp);
509 }
510 if (flags & CHECK_UPDATE) {
511 if ((error = update_inoblk(fs, offset, cred, l))
512 != 0) {
513 offset = -1;
514 goto err2;
515 }
516 }
517 offset += btofsb(fs, fs->lfs_ibsize);
518 --iaddr;
519 --ninos;
520 --i; /* compensate */
521 continue;
522 }
523 size = fs->lfs_bsize;
524 for (j = 0; j < fip->fi_nblocks; ++j) {
525 if (j == fip->fi_nblocks - 1)
526 size = fip->fi_lastlength;
527 if (flags & CHECK_CKSUM) {
528 error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp);
529 if (error) {
530 offset = -1;
531 goto err2;
532 }
533 (*dp++) = ((u_long *)(dbp->b_data))[0];
534 dbp->b_flags |= B_AGE;
535 brelse(dbp);
536 }
537 /* Account for and update any direct blocks */
538 if ((flags & CHECK_UPDATE) &&
539 fip->fi_ino > LFS_IFILE_INUM &&
540 fip->fi_blocks[j] >= 0) {
541 update_meta(fs, fip->fi_ino, fip->fi_version,
542 fip->fi_blocks[j], offset, size, l);
543 }
544 offset += btofsb(fs, size);
545 }
546 /* XXX ondisk32 */
547 fip = (FINFO *)(((char *)fip) + FINFOSIZE
548 + fip->fi_nblocks * sizeof(int32_t));
549 }
550 /* Checksum the array, compare */
551 if ((flags & CHECK_CKSUM) &&
552 ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
553 {
554 DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
555 " (wanted %x got %x)\n",
556 offset, ssp->ss_datasum, cksum(datap, nblocks *
557 sizeof(u_long))));
558 offset = -1;
559 goto err2;
560 }
561
562 /* If we're at the end of the segment, move to the next */
563 if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
564 dtosn(fs, offset)) {
565 if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) {
566 offset = -1;
567 goto err2;
568 }
569 offset = ssp->ss_next;
570 DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
571 " -> segment %d\n", offset, dtosn(fs,offset)));
572 }
573
574 if (flags & CHECK_UPDATE) {
575 fs->lfs_avail -= (offset - oldoffset);
576 /* Don't clog the buffer queue */
577 simple_lock(&lfs_subsys_lock);
578 if (locked_queue_count > LFS_MAX_BUFS ||
579 locked_queue_bytes > LFS_MAX_BYTES) {
580 lfs_flush(fs, SEGM_CKP, 0);
581 }
582 simple_unlock(&lfs_subsys_lock);
583 }
584
585 err2:
586 if (flags & CHECK_CKSUM)
587 free(datap, M_SEGMENT);
588 err1:
589 bp->b_flags |= B_AGE;
590 brelse(bp);
591
592 /* XXX should we update the serial number even for bad psegs? */
593 if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
594 fs->lfs_serial = nextserial;
595 return offset;
596 }
597
598 void
599 lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
600 {
601 int flags, dirty;
602 daddr_t offset, oldoffset, lastgoodpseg;
603 int sn, curseg, do_rollforward;
604
605 /*
606 * Roll forward.
607 *
608 * We don't roll forward for v1 filesystems, because
609 * of the danger that the clock was turned back between the last
610 * checkpoint and crash. This would roll forward garbage.
611 *
612 * v2 filesystems don't have this problem because they use a
613 * monotonically increasing serial number instead of a timestamp.
614 */
615 do_rollforward = (!(fs->lfs_pflags & LFS_PF_CLEAN) &&
616 lfs_do_rfw && fs->lfs_version > 1 && p != NULL);
617 if (do_rollforward) {
618 u_int64_t nextserial;
619 /*
620 * Phase I: Find the address of the last good partial
621 * segment that was written after the checkpoint. Mark
622 * the segments in question dirty, so they won't be
623 * reallocated.
624 */
625 lastgoodpseg = oldoffset = offset = fs->lfs_offset;
626 flags = 0x0;
627 DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
628 PRIx64 "\n", offset));
629 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
630 if (!(sup->su_flags & SEGUSE_DIRTY))
631 --fs->lfs_nclean;
632 sup->su_flags |= SEGUSE_DIRTY;
633 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, offset), bp);
634 nextserial = fs->lfs_serial + 1;
635 while ((offset = check_segsum(fs, offset, nextserial,
636 cred, CHECK_CKSUM, &flags, l)) > 0) {
637 nextserial++;
638 if (sntod(fs, oldoffset) != sntod(fs, offset)) {
639 LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset),
640 bp);
641 if (!(sup->su_flags & SEGUSE_DIRTY))
642 --fs->lfs_nclean;
643 sup->su_flags |= SEGUSE_DIRTY;
644 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, oldoffset),
645 bp);
646 }
647
648 DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
649 PRIx64 "\n", offset));
650 if (flags & SS_DIROP) {
651 DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
652 PRIx64 "\n", oldoffset));
653 if (!(flags & SS_CONT))
654 DLOG((DLOG_RF, "lfs_mountfs: dirops end "
655 "at 0x%" PRIx64 "\n", oldoffset));
656 }
657 if (!(flags & SS_CONT))
658 lastgoodpseg = offset;
659 oldoffset = offset;
660 }
661 if (flags & SS_CONT) {
662 DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
663 "dirops discarded\n"));
664 }
665 DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
666 "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
667 oldoffset = fs->lfs_offset;
668 if (fs->lfs_offset != lastgoodpseg) {
669 /* Don't overwrite what we're trying to preserve */
670 offset = fs->lfs_offset;
671 fs->lfs_offset = lastgoodpseg;
672 fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset));
673 for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) {
674 sn = (sn + 1) % fs->lfs_nseg;
675 if (sn == curseg)
676 panic("lfs_mountfs: no clean segments");
677 LFS_SEGENTRY(sup, fs, sn, bp);
678 dirty = (sup->su_flags & SEGUSE_DIRTY);
679 brelse(bp);
680 if (!dirty)
681 break;
682 }
683 fs->lfs_nextseg = sntod(fs, sn);
684
685 /*
686 * Phase II: Roll forward from the first superblock.
687 */
688 while (offset != lastgoodpseg) {
689 DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
690 PRIx64 "\n", offset));
691 offset = check_segsum(fs, offset,
692 fs->lfs_serial + 1, cred, CHECK_UPDATE,
693 NULL, l);
694 }
695
696 /*
697 * Finish: flush our changes to disk.
698 */
699 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
700 DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
701 "recovered %lld blocks\n",
702 (long long)(lastgoodpseg - oldoffset)));
703 }
704 DLOG((DLOG_RF, "LFS roll forward complete\n"));
705 }
706 }
707 #endif /* LFS_KERNEL_RFW */
708