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