lfs_rfw.c revision 1.30 1 /* $NetBSD: lfs_rfw.c,v 1.30 2015/08/19 20:33:29 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.30 2015/08/19 20:33:29 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 /*
254 * Copy some the fields of the dinode as needed by update_inoblk().
255 */
256 static void
257 update_inoblk_copy_dinode(struct lfs *fs,
258 union lfs_dinode *dstu, const union lfs_dinode *srcu)
259 {
260 if (fs->lfs_is64) {
261 struct lfs64_dinode *dst = &dstu->u_64;
262 const struct lfs64_dinode *src = &srcu->u_64;
263 unsigned i;
264
265 /*
266 * Copy everything but the block pointers and di_blocks.
267 * XXX what about di_extb?
268 */
269 dst->di_mode = src->di_mode;
270 dst->di_nlink = src->di_nlink;
271 dst->di_uid = src->di_uid;
272 dst->di_gid = src->di_gid;
273 dst->di_blksize = src->di_blksize;
274 dst->di_size = src->di_size;
275 dst->di_atime = src->di_atime;
276 dst->di_mtime = src->di_mtime;
277 dst->di_ctime = src->di_ctime;
278 dst->di_birthtime = src->di_birthtime;
279 dst->di_mtimensec = src->di_mtimensec;
280 dst->di_atimensec = src->di_atimensec;
281 dst->di_ctimensec = src->di_ctimensec;
282 dst->di_birthnsec = src->di_birthnsec;
283 dst->di_gen = src->di_gen;
284 dst->di_kernflags = src->di_kernflags;
285 dst->di_flags = src->di_flags;
286 dst->di_extsize = src->di_extsize;
287 dst->di_modrev = src->di_modrev;
288 dst->di_inumber = src->di_inumber;
289 for (i = 0; i < __arraycount(src->di_spare); i++) {
290 dst->di_spare[i] = src->di_spare[i];
291 }
292 } else {
293 struct lfs32_dinode *dst = &dstu->u_32;
294 const struct lfs32_dinode *src = &srcu->u_32;
295
296 /* Get mode, link count, size, and times */
297 memcpy(dst, src, offsetof(struct lfs32_dinode, di_db[0]));
298
299 /* Then the rest, except di_blocks */
300 dst->di_flags = src->di_flags;
301 dst->di_gen = src->di_gen;
302 dst->di_uid = src->di_uid;
303 dst->di_gid = src->di_gid;
304 dst->di_modrev = src->di_modrev;
305 }
306 }
307
308 static int
309 update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
310 struct lwp *l)
311 {
312 struct vnode *devvp, *vp;
313 struct inode *ip;
314 union lfs_dinode *dip;
315 struct buf *dbp, *ibp;
316 int error;
317 daddr_t daddr;
318 IFILE *ifp;
319 SEGUSE *sup;
320 unsigned i, num;
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, LFS_FSBTODB(fs, offset), lfs_sb_getibsize(fs),
329 0, &dbp);
330 if (error) {
331 DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
332 return error;
333 }
334 num = LFS_INOPB(fs);
335 for (i = num; i-- > 0; ) {
336 dip = DINO_IN_BLOCK(fs, dbp->b_data, i);
337 if (lfs_dino_getinumber(fs, dip) > LFS_IFILE_INUM) {
338 error = lfs_rf_valloc(fs, lfs_dino_getinumber(fs, dip),
339 lfs_dino_getgen(fs, dip),
340 l, &vp);
341 if (error) {
342 DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
343 " returned %d\n", error));
344 continue;
345 }
346 ip = VTOI(vp);
347 if (lfs_dino_getsize(fs, dip) != ip->i_size)
348 lfs_truncate(vp, lfs_dino_getsize(fs, dip), 0,
349 NOCRED);
350 update_inoblk_copy_dinode(fs, ip->i_din, dip);
351
352 ip->i_flags = lfs_dino_getflags(fs, dip);
353 ip->i_gen = lfs_dino_getgen(fs, dip);
354 ip->i_uid = lfs_dino_getuid(fs, dip);
355 ip->i_gid = lfs_dino_getgid(fs, dip);
356
357 ip->i_mode = lfs_dino_getmode(fs, dip);
358 ip->i_nlink = lfs_dino_getnlink(fs, dip);
359 ip->i_size = lfs_dino_getsize(fs, dip);
360
361 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
362
363 /* Re-initialize to get type right */
364 ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
365 &vp);
366 vput(vp);
367
368 /* Record change in location */
369 LFS_IENTRY(ifp, fs, lfs_dino_getinumber(fs, dip), ibp);
370 daddr = lfs_if_getdaddr(fs, ifp);
371 lfs_if_setdaddr(fs, ifp, LFS_DBTOFSB(fs, dbp->b_blkno));
372 error = LFS_BWRITE_LOG(ibp); /* Ifile */
373 /* And do segment accounting */
374 if (lfs_dtosn(fs, daddr) != lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno))) {
375 if (daddr > 0) {
376 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, daddr),
377 ibp);
378 sup->su_nbytes -= DINOSIZE(fs);
379 LFS_WRITESEGENTRY(sup, fs,
380 lfs_dtosn(fs, daddr),
381 ibp);
382 }
383 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
384 ibp);
385 sup->su_nbytes += DINOSIZE(fs);
386 LFS_WRITESEGENTRY(sup, fs,
387 lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
388 ibp);
389 }
390 }
391 }
392 brelse(dbp, BC_AGE);
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 uint32_t datasum, foundsum;
415
416 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
417 /*
418 * If the segment has a superblock and we're at the top
419 * of the segment, skip the superblock.
420 */
421 if (lfs_sntod(fs, lfs_dtosn(fs, offset)) == offset) {
422 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
423 if (sup->su_flags & SEGUSE_SUPERBLOCK)
424 offset += lfs_btofsb(fs, LFS_SBPAD);
425 brelse(bp, 0);
426 }
427
428 /* Read in the segment summary */
429 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getsumsize(fs),
430 0, &bp);
431 if (error)
432 return -1;
433
434 /* Check summary checksum */
435 ssp = (SEGSUM *)bp->b_data;
436 if (flags & CHECK_CKSUM) {
437 size_t sumstart;
438
439 sumstart = lfs_ss_getsumstart(fs);
440 if (lfs_ss_getsumsum(fs, ssp) !=
441 cksum((char *)ssp + sumstart,
442 lfs_sb_getsumsize(fs) - sumstart)) {
443 DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
444 offset = -1;
445 goto err1;
446 }
447 if (lfs_ss_getnfinfo(fs, ssp) == 0 &&
448 lfs_ss_getninos(fs, ssp) == 0) {
449 DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
450 offset = -1;
451 goto err1;
452 }
453 if (lfs_ss_getcreate(fs, ssp) < lfs_sb_gettstamp(fs)) {
454 DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
455 offset = -1;
456 goto err1;
457 }
458 }
459 if (lfs_sb_getversion(fs) > 1) {
460 if (lfs_ss_getserial(fs, ssp) != nextserial) {
461 DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
462 "\n", offset));
463 offset = -1;
464 goto err1;
465 }
466 if (lfs_ss_getident(fs, ssp) != lfs_sb_getident(fs)) {
467 DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
468 PRIx64 "\n", lfs_ss_getident(fs, ssp),
469 lfs_sb_getident(fs), offset));
470 offset = -1;
471 goto err1;
472 }
473 }
474 if (pseg_flags)
475 *pseg_flags = lfs_ss_getflags(fs, ssp);
476 oldoffset = offset;
477 offset += lfs_btofsb(fs, lfs_sb_getsumsize(fs));
478
479 ninos = howmany(lfs_ss_getninos(fs, ssp), LFS_INOPB(fs));
480 /* XXX ondisk32 */
481 iaddr = (int32_t *)((char*)bp->b_data + lfs_sb_getsumsize(fs) - sizeof(int32_t));
482 if (flags & CHECK_CKSUM) {
483 /* Count blocks */
484 nblocks = 0;
485 fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
486 for (i = 0; i < lfs_ss_getnfinfo(fs, ssp); ++i) {
487 nblocks += lfs_fi_getnblocks(fs, fip);
488 if (lfs_fi_getnblocks(fs, fip) <= 0)
489 break;
490 fip = NEXT_FINFO(fs, fip);
491 }
492 nblocks += ninos;
493 /* Create the sum array */
494 datap = dp = malloc(nblocks * sizeof(u_long),
495 M_SEGMENT, M_WAITOK);
496 }
497
498 /* Handle individual blocks */
499 fip = SEGSUM_FINFOBASE(fs, (SEGSUM *)bp->b_data);
500 for (i = 0; i < lfs_ss_getnfinfo(fs, ssp) || ninos; ++i) {
501 /* Inode block? */
502 if (ninos && *iaddr == offset) {
503 if (flags & CHECK_CKSUM) {
504 /* Read in the head and add to the buffer */
505 error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getbsize(fs),
506 0, &dbp);
507 if (error) {
508 offset = -1;
509 goto err2;
510 }
511 /* XXX this can't be right, on-disk u_long? */
512 (*dp++) = ((u_long *)(dbp->b_data))[0];
513 brelse(dbp, BC_AGE);
514 }
515 if (flags & CHECK_UPDATE) {
516 if ((error = update_inoblk(fs, offset, cred, l))
517 != 0) {
518 offset = -1;
519 goto err2;
520 }
521 }
522 offset += lfs_btofsb(fs, lfs_sb_getibsize(fs));
523 --iaddr;
524 --ninos;
525 --i; /* compensate */
526 continue;
527 }
528 size = lfs_sb_getbsize(fs);
529 for (j = 0; j < lfs_fi_getnblocks(fs, fip); ++j) {
530 if (j == lfs_fi_getnblocks(fs, fip) - 1)
531 size = lfs_fi_getlastlength(fs, fip);
532 if (flags & CHECK_CKSUM) {
533 error = bread(devvp, LFS_FSBTODB(fs, offset), size,
534 0, &dbp);
535 if (error) {
536 offset = -1;
537 goto err2;
538 }
539 (*dp++) = ((u_long *)(dbp->b_data))[0];
540 brelse(dbp, BC_AGE);
541 }
542 /* Account for and update any direct blocks */
543 if ((flags & CHECK_UPDATE) &&
544 lfs_fi_getino(fs, fip) > LFS_IFILE_INUM &&
545 lfs_fi_getblock(fs, fip, j) >= 0) {
546 update_meta(fs, lfs_fi_getino(fs, fip),
547 lfs_fi_getversion(fs, fip),
548 lfs_fi_getblock(fs, fip, j),
549 offset, size, l);
550 }
551 offset += lfs_btofsb(fs, size);
552 }
553 fip = NEXT_FINFO(fs, fip);
554 }
555 /* Checksum the array, compare */
556 datasum = lfs_ss_getdatasum(fs, ssp);
557 foundsum = cksum(datap, nblocks * sizeof(u_long));
558 if ((flags & CHECK_CKSUM) && datasum != foundsum) {
559 DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
560 " (wanted %x got %x)\n",
561 offset, datasum, foundsum));
562 offset = -1;
563 goto err2;
564 }
565
566 /* If we're at the end of the segment, move to the next */
567 if (lfs_dtosn(fs, offset + lfs_btofsb(fs, lfs_sb_getsumsize(fs) + lfs_sb_getbsize(fs))) !=
568 lfs_dtosn(fs, offset)) {
569 if (lfs_dtosn(fs, offset) == lfs_dtosn(fs, lfs_ss_getnext(fs, ssp))) {
570 offset = -1;
571 goto err2;
572 }
573 offset = lfs_ss_getnext(fs, ssp);
574 DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
575 " -> segment %d\n", offset, lfs_dtosn(fs,offset)));
576 }
577
578 if (flags & CHECK_UPDATE) {
579 lfs_sb_subavail(fs, offset - oldoffset);
580 /* Don't clog the buffer queue */
581 mutex_enter(&lfs_lock);
582 if (locked_queue_count > LFS_MAX_BUFS ||
583 locked_queue_bytes > LFS_MAX_BYTES) {
584 lfs_flush(fs, SEGM_CKP, 0);
585 }
586 mutex_exit(&lfs_lock);
587 }
588
589 err2:
590 if (flags & CHECK_CKSUM)
591 free(datap, M_SEGMENT);
592 err1:
593 brelse(bp, BC_AGE);
594
595 /* XXX should we update the serial number even for bad psegs? */
596 if ((flags & CHECK_UPDATE) && offset > 0 && lfs_sb_getversion(fs) > 1)
597 lfs_sb_setserial(fs, nextserial);
598 return offset;
599 }
600
601 void
602 lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
603 {
604 int flags, dirty;
605 daddr_t offset, oldoffset, lastgoodpseg;
606 int sn, curseg, do_rollforward;
607 struct proc *p;
608 kauth_cred_t cred;
609 SEGUSE *sup;
610 struct buf *bp;
611
612 p = l ? l->l_proc : NULL;
613 cred = p ? p->p_cred : NOCRED;
614
615 /*
616 * Roll forward.
617 *
618 * We don't roll forward for v1 filesystems, because
619 * of the danger that the clock was turned back between the last
620 * checkpoint and crash. This would roll forward garbage.
621 *
622 * v2 filesystems don't have this problem because they use a
623 * monotonically increasing serial number instead of a timestamp.
624 */
625 do_rollforward = (!(lfs_sb_getpflags(fs) & LFS_PF_CLEAN) &&
626 lfs_do_rfw && lfs_sb_getversion(fs) > 1 && p != NULL);
627 if (do_rollforward) {
628 u_int64_t nextserial;
629 /*
630 * Phase I: Find the address of the last good partial
631 * segment that was written after the checkpoint. Mark
632 * the segments in question dirty, so they won't be
633 * reallocated.
634 */
635 lastgoodpseg = oldoffset = offset = lfs_sb_getoffset(fs);
636 flags = 0x0;
637 DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
638 PRIx64 "\n", offset));
639 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
640 if (!(sup->su_flags & SEGUSE_DIRTY))
641 lfs_sb_subnclean(fs, 1);
642 sup->su_flags |= SEGUSE_DIRTY;
643 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
644 nextserial = lfs_sb_getserial(fs) + 1;
645 while ((offset = check_segsum(fs, offset, nextserial,
646 cred, CHECK_CKSUM, &flags, l)) > 0) {
647 nextserial++;
648 if (lfs_sntod(fs, oldoffset) != lfs_sntod(fs, offset)) {
649 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
650 bp);
651 if (!(sup->su_flags & SEGUSE_DIRTY))
652 lfs_sb_subnclean(fs, 1);
653 sup->su_flags |= SEGUSE_DIRTY;
654 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
655 bp);
656 }
657
658 DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
659 PRIx64 "\n", offset));
660 if (flags & SS_DIROP) {
661 DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
662 PRIx64 "\n", oldoffset));
663 if (!(flags & SS_CONT)) {
664 DLOG((DLOG_RF, "lfs_mountfs: dirops end "
665 "at 0x%" PRIx64 "\n", oldoffset));
666 }
667 }
668 if (!(flags & SS_CONT))
669 lastgoodpseg = offset;
670 oldoffset = offset;
671 }
672 if (flags & SS_CONT) {
673 DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
674 "dirops discarded\n"));
675 }
676 DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
677 "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
678 oldoffset = lfs_sb_getoffset(fs);
679 if (lfs_sb_getoffset(fs) != lastgoodpseg) {
680 /* Don't overwrite what we're trying to preserve */
681 offset = lfs_sb_getoffset(fs);
682 lfs_sb_setoffset(fs, lastgoodpseg);
683 lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_dtosn(fs, lfs_sb_getoffset(fs))));
684 for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs));;) {
685 sn = (sn + 1) % lfs_sb_getnseg(fs);
686 if (sn == curseg)
687 panic("lfs_mountfs: no clean segments");
688 LFS_SEGENTRY(sup, fs, sn, bp);
689 dirty = (sup->su_flags & SEGUSE_DIRTY);
690 brelse(bp, 0);
691 if (!dirty)
692 break;
693 }
694 lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
695
696 /*
697 * Phase II: Roll forward from the first superblock.
698 */
699 while (offset != lastgoodpseg) {
700 DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
701 PRIx64 "\n", offset));
702 offset = check_segsum(fs, offset,
703 lfs_sb_getserial(fs) + 1, cred, CHECK_UPDATE,
704 NULL, l);
705 }
706
707 /*
708 * Finish: flush our changes to disk.
709 */
710 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
711 DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
712 "recovered %jd blocks\n",
713 (intmax_t)(lastgoodpseg - oldoffset)));
714 }
715 DLOG((DLOG_RF, "LFS roll forward complete\n"));
716 }
717 }
718