lfs.c revision 1.5
1/* $NetBSD: lfs.c,v 1.5 2003/07/12 11:57:59 yamt Exp $ */ 2/*- 3 * Copyright (c) 2003 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Konrad E. Schroder <perseant@hhhh.org>. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the NetBSD 20 * Foundation, Inc. and its contributors. 21 * 4. Neither the name of The NetBSD Foundation nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37/* 38 * Copyright (c) 1989, 1991, 1993 39 * The Regents of the University of California. All rights reserved. 40 * (c) UNIX System Laboratories, Inc. 41 * All or some portions of this file are derived from material licensed 42 * to the University of California by American Telephone and Telegraph 43 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 44 * the permission of UNIX System Laboratories, Inc. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. All advertising materials mentioning features or use of this software 55 * must display the following acknowledgement: 56 * This product includes software developed by the University of 57 * California, Berkeley and its contributors. 58 * 4. Neither the name of the University nor the names of its contributors 59 * may be used to endorse or promote products derived from this software 60 * without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95 75 */ 76 77 78#include <sys/types.h> 79#include <sys/param.h> 80#include <sys/time.h> 81#include <sys/buf.h> 82#include <sys/mount.h> 83 84#include <ufs/ufs/inode.h> 85#include <ufs/ufs/ufsmount.h> 86#define vnode uvnode 87#include <ufs/lfs/lfs.h> 88#undef vnode 89 90#include <assert.h> 91#include <err.h> 92#include <errno.h> 93#include <stdarg.h> 94#include <stdio.h> 95#include <stdlib.h> 96#include <string.h> 97#include <unistd.h> 98 99#include "bufcache.h" 100#include "vnode.h" 101#include "lfs.h" 102#include "segwrite.h" 103 104#define panic call_panic 105 106extern u_int32_t cksum(void *, size_t); 107extern u_int32_t lfs_sb_cksum(struct dlfs *); 108 109extern struct uvnodelst vnodelist; 110extern struct uvnodelst getvnodelist; 111extern int nvnodes; 112 113int fsdirty = 0; 114void (*panic_func)(int, const char *, va_list) = my_vpanic; 115 116/* 117 * LFS buffer and uvnode operations 118 */ 119 120int 121lfs_vop_strategy(struct ubuf * bp) 122{ 123 int count; 124 125 if (bp->b_flags & B_READ) { 126 count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 127 dbtob(bp->b_blkno)); 128 if (count == bp->b_bcount) 129 bp->b_flags |= B_DONE; 130 } else { 131 count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 132 dbtob(bp->b_blkno)); 133 if (count == 0) { 134 perror("pwrite"); 135 return -1; 136 } 137 bp->b_flags &= ~B_DELWRI; 138 reassignbuf(bp, bp->b_vp); 139 } 140 return 0; 141} 142 143int 144lfs_vop_bwrite(struct ubuf * bp) 145{ 146 struct lfs *fs; 147 148 fs = bp->b_vp->v_fs; 149 if (!(bp->b_flags & B_DELWRI)) { 150 fs->lfs_avail -= btofsb(fs, bp->b_bcount); 151 } 152 bp->b_flags |= B_DELWRI | B_LOCKED; 153 reassignbuf(bp, bp->b_vp); 154 brelse(bp); 155 return 0; 156} 157 158/* 159 * ufs_bmaparray does the bmap conversion, and if requested returns the 160 * array of logical blocks which must be traversed to get to a block. 161 * Each entry contains the offset into that block that gets you to the 162 * next block and the disk address of the block (if it is assigned). 163 */ 164int 165ufs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump) 166{ 167 struct inode *ip; 168 struct ubuf *bp; 169 struct indir a[NIADDR + 1], *xap; 170 daddr_t daddr; 171 daddr_t metalbn; 172 int error, num; 173 174 ip = VTOI(vp); 175 176 if (bn >= 0 && bn < NDADDR) { 177 if (nump != NULL) 178 *nump = 0; 179 *bnp = fsbtodb(fs, ip->i_ffs1_db[bn]); 180 if (*bnp == 0) 181 *bnp = -1; 182 return (0); 183 } 184 xap = ap == NULL ? a : ap; 185 if (!nump) 186 nump = # 187 if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0) 188 return (error); 189 190 num = *nump; 191 192 /* Get disk address out of indirect block array */ 193 daddr = ip->i_ffs1_ib[xap->in_off]; 194 195 for (bp = NULL, ++xap; --num; ++xap) { 196 /* Exit the loop if there is no disk address assigned yet and 197 * the indirect block isn't in the cache, or if we were 198 * looking for an indirect block and we've found it. */ 199 200 metalbn = xap->in_lbn; 201 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn) 202 break; 203 /* 204 * If we get here, we've either got the block in the cache 205 * or we have a disk address for it, go fetch it. 206 */ 207 if (bp) 208 brelse(bp); 209 210 xap->in_exists = 1; 211 bp = getblk(vp, metalbn, fs->lfs_bsize); 212 213 if (!(bp->b_flags & (B_DONE | B_DELWRI))) { 214 bp->b_blkno = fsbtodb(fs, daddr); 215 bp->b_flags |= B_READ; 216 VOP_STRATEGY(bp); 217 } 218 daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off]; 219 } 220 if (bp) 221 brelse(bp); 222 223 daddr = fsbtodb(fs, (ufs_daddr_t) daddr); 224 *bnp = daddr == 0 ? -1 : daddr; 225 return (0); 226} 227 228/* 229 * Create an array of logical block number/offset pairs which represent the 230 * path of indirect blocks required to access a data block. The first "pair" 231 * contains the logical block number of the appropriate single, double or 232 * triple indirect block and the offset into the inode indirect block array. 233 * Note, the logical block number of the inode single/double/triple indirect 234 * block appears twice in the array, once with the offset into the i_ffs1_ib and 235 * once with the offset into the page itself. 236 */ 237int 238ufs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump) 239{ 240 daddr_t metalbn, realbn; 241 int64_t blockcnt; 242 int lbc; 243 int i, numlevels, off; 244 int lognindir, indir; 245 246 if (nump) 247 *nump = 0; 248 numlevels = 0; 249 realbn = bn; 250 if (bn < 0) 251 bn = -bn; 252 253 lognindir = -1; 254 for (indir = fs->lfs_nindir; indir; indir >>= 1) 255 ++lognindir; 256 257 /* Determine the number of levels of indirection. After this loop is 258 * done, blockcnt indicates the number of data blocks possible at the 259 * given level of indirection, and NIADDR - i is the number of levels 260 * of indirection needed to locate the requested block. */ 261 262 bn -= NDADDR; 263 for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) { 264 if (i == 0) 265 return (EFBIG); 266 267 lbc += lognindir; 268 blockcnt = (int64_t) 1 << lbc; 269 270 if (bn < blockcnt) 271 break; 272 } 273 274 /* Calculate the address of the first meta-block. */ 275 if (realbn >= 0) 276 metalbn = -(realbn - bn + NIADDR - i); 277 else 278 metalbn = -(-realbn - bn + NIADDR - i); 279 280 /* At each iteration, off is the offset into the bap array which is an 281 * array of disk addresses at the current level of indirection. The 282 * logical block number and the offset in that block are stored into 283 * the argument array. */ 284 ap->in_lbn = metalbn; 285 ap->in_off = off = NIADDR - i; 286 ap->in_exists = 0; 287 ap++; 288 for (++numlevels; i <= NIADDR; i++) { 289 /* If searching for a meta-data block, quit when found. */ 290 if (metalbn == realbn) 291 break; 292 293 lbc -= lognindir; 294 blockcnt = (int64_t) 1 << lbc; 295 off = (bn >> lbc) & (fs->lfs_nindir - 1); 296 297 ++numlevels; 298 ap->in_lbn = metalbn; 299 ap->in_off = off; 300 ap->in_exists = 0; 301 ++ap; 302 303 metalbn -= -1 + (off << lbc); 304 } 305 if (nump) 306 *nump = numlevels; 307 return (0); 308} 309 310int 311lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp) 312{ 313 return ufs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL); 314} 315 316/* Search a block for a specific dinode. */ 317struct ufs1_dinode * 318lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp) 319{ 320 struct ufs1_dinode *dip = (struct ufs1_dinode *) bp->b_data; 321 struct ufs1_dinode *ldip, *fin; 322 323 fin = dip + INOPB(fs); 324 325 /* 326 * Read the inode block backwards, since later versions of the 327 * inode will supercede earlier ones. Though it is unlikely, it is 328 * possible that the same inode will appear in the same inode block. 329 */ 330 for (ldip = fin - 1; ldip >= dip; --ldip) 331 if (ldip->di_inumber == ino) 332 return (ldip); 333 return NULL; 334} 335 336/* 337 * lfs_raw_vget makes us a new vnode from the inode at the given disk address. 338 * XXX it currently loses atime information. 339 */ 340struct uvnode * 341lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr) 342{ 343 struct uvnode *vp; 344 struct inode *ip; 345 struct ufs1_dinode *dip; 346 struct ubuf *bp; 347 int i; 348 349 vp = (struct uvnode *) malloc(sizeof(*vp)); 350 memset(vp, 0, sizeof(*vp)); 351 vp->v_fd = fd; 352 vp->v_fs = fs; 353 vp->v_usecount = 0; 354 vp->v_strategy_op = lfs_vop_strategy; 355 vp->v_bwrite_op = lfs_vop_bwrite; 356 vp->v_bmap_op = lfs_vop_bmap; 357 LIST_INIT(&vp->v_cleanblkhd); 358 LIST_INIT(&vp->v_dirtyblkhd); 359 360 ++nvnodes; 361 LIST_INSERT_HEAD(&getvnodelist, vp, v_getvnodes); 362 LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes); 363 364 vp->v_data = ip = (struct inode *) malloc(sizeof(*ip)); 365 memset(ip, 0, sizeof(*ip)); 366 367 ip->i_din.ffs1_din = (struct ufs1_dinode *) 368 malloc(sizeof(struct ufs1_dinode)); 369 memset(ip->i_din.ffs1_din, 0, sizeof (struct ufs1_dinode)); 370 371 /* Initialize the inode -- from lfs_vcreate. */ 372 ip->inode_ext.lfs = malloc(sizeof(struct lfs_inode_ext)); 373 memset(ip->inode_ext.lfs, 0, sizeof(struct lfs_inode_ext)); 374 vp->v_data = ip; 375 /* ip->i_vnode = vp; */ 376 ip->i_number = ino; 377 ip->i_lockf = 0; 378 ip->i_diroff = 0; 379 ip->i_lfs_effnblks = 0; 380 ip->i_flag = 0; 381 382 /* Load inode block and find inode */ 383 bread(fs->lfs_unlockvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp); 384 bp->b_flags |= B_AGE; 385 dip = lfs_ifind(fs, ino, bp); 386 if (dip == NULL) { 387 brelse(bp); 388 free(vp); 389 return NULL; 390 } 391 memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip)); 392 brelse(bp); 393 ip->i_number = ino; 394 /* ip->i_devvp = fs->lfs_unlockvp; */ 395 ip->i_lfs = fs; 396 397 ip->i_ffs_effnlink = ip->i_ffs1_nlink; 398 ip->i_lfs_effnblks = ip->i_ffs1_blocks; 399 ip->i_lfs_osize = ip->i_ffs1_size; 400#if 0 401 if (fs->lfs_version > 1) { 402 ip->i_ffs1_atime = ts.tv_sec; 403 ip->i_ffs1_atimensec = ts.tv_nsec; 404 } 405#endif 406 407 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize)); 408 for (i = 0; i < NDADDR; i++) 409 if (ip->i_ffs1_db[i] != 0) 410 ip->i_lfs_fragsize[i] = blksize(fs, ip, i); 411 412 return vp; 413} 414 415static struct uvnode * 416lfs_vget(void *vfs, ino_t ino) 417{ 418 struct lfs *fs = (struct lfs *)vfs; 419 ufs_daddr_t daddr; 420 struct ubuf *bp; 421 IFILE *ifp; 422 423 LFS_IENTRY(ifp, fs, ino, bp); 424 daddr = ifp->if_daddr; 425 brelse(bp); 426 if (daddr == 0) 427 return NULL; 428 return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr); 429} 430 431/* Check superblock magic number and checksum */ 432static int 433check_sb(struct lfs *fs) 434{ 435 u_int32_t checksum; 436 437 if (fs->lfs_magic != LFS_MAGIC) { 438 printf("Superblock magic number (0x%lx) does not match " 439 "expected 0x%lx\n", (unsigned long) fs->lfs_magic, 440 (unsigned long) LFS_MAGIC); 441 return 1; 442 } 443 /* checksum */ 444 checksum = lfs_sb_cksum(&(fs->lfs_dlfs)); 445 if (fs->lfs_cksum != checksum) { 446 printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n", 447 (unsigned long) fs->lfs_cksum, (unsigned long) checksum); 448 return 1; 449 } 450 return 0; 451} 452 453/* Initialize LFS library; load superblocks and choose which to use. */ 454struct lfs * 455lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int debug) 456{ 457 struct uvnode *devvp; 458 struct ubuf *bp; 459 int tryalt; 460 struct lfs *fs, *altfs; 461 int error; 462 463 vfs_init(); 464 465 devvp = (struct uvnode *) malloc(sizeof(*devvp)); 466 memset(devvp, 0, sizeof(*devvp)); 467 devvp->v_fs = NULL; 468 devvp->v_fd = devfd; 469 devvp->v_strategy_op = raw_vop_strategy; 470 devvp->v_bwrite_op = raw_vop_bwrite; 471 devvp->v_bmap_op = raw_vop_bmap; 472 LIST_INIT(&devvp->v_cleanblkhd); 473 LIST_INIT(&devvp->v_dirtyblkhd); 474 475 tryalt = 0; 476 if (sblkno == 0) { 477 sblkno = btodb(LFS_LABELPAD); 478 tryalt = 1; 479 } else if (debug) { 480 printf("No -b flag given, not attempting to verify checkpoint\n"); 481 } 482 error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp); 483 fs = (struct lfs *) malloc(sizeof(*fs)); 484 memset(fs, 0, sizeof(*fs)); 485 fs->lfs_dlfs = *((struct dlfs *) bp->b_data); 486 fs->lfs_unlockvp = devvp; 487 bp->b_flags |= B_INVAL; 488 brelse(bp); 489 490 if (tryalt) { 491 error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]), 492 LFS_SBPAD, NOCRED, &bp); 493 altfs = (struct lfs *) malloc(sizeof(*altfs)); 494 memset(altfs, 0, sizeof(*altfs)); 495 altfs->lfs_dlfs = *((struct dlfs *) bp->b_data); 496 altfs->lfs_unlockvp = devvp; 497 bp->b_flags |= B_INVAL; 498 brelse(bp); 499 500 if (check_sb(fs)) { 501 if (debug) 502 printf("Primary superblock is no good, using first alternate\n"); 503 free(fs); 504 fs = altfs; 505 } else { 506 /* If both superblocks check out, try verification */ 507 if (check_sb(altfs)) { 508 if (debug) 509 printf("First alternate superblock is no good, using primary\n"); 510 free(altfs); 511 } else { 512 if (lfs_verify(fs, altfs, devvp, debug) == fs) { 513 free(altfs); 514 } else { 515 free(fs); 516 fs = altfs; 517 } 518 } 519 } 520 } 521 if (check_sb(fs)) { 522 free(fs); 523 return NULL; 524 } 525 /* Compatibility */ 526 if (fs->lfs_version < 2) { 527 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE; 528 fs->lfs_ibsize = fs->lfs_bsize; 529 fs->lfs_start = fs->lfs_sboffs[0]; 530 fs->lfs_tstamp = fs->lfs_otstamp; 531 fs->lfs_fsbtodb = 0; 532 } 533 fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *)); 534 fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t)); 535 fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t)); 536 537 if (idaddr == 0) 538 idaddr = fs->lfs_idaddr; 539 fs->lfs_ivnode = lfs_raw_vget(fs, fs->lfs_ifile, devvp->v_fd, idaddr); 540 541 register_vget((void *)fs, lfs_vget); 542 543 return fs; 544} 545 546/* 547 * Check partial segment validity between fs->lfs_offset and the given goal. 548 * If goal == 0, just keep on going until the segments stop making sense. 549 * Return the address of the first partial segment that failed. 550 */ 551ufs_daddr_t 552try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug) 553{ 554 ufs_daddr_t daddr, odaddr; 555 SEGSUM *sp; 556 int bc, flag; 557 struct ubuf *bp; 558 ufs_daddr_t nodirop_daddr; 559 u_int64_t serial; 560 561 daddr = osb->lfs_offset; 562 nodirop_daddr = daddr; 563 serial = osb->lfs_serial; 564 while (daddr != goal) { 565 flag = 0; 566oncemore: 567 /* Read in summary block */ 568 bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp); 569 sp = (SEGSUM *)bp->b_data; 570 571 /* 572 * Could be a superblock instead of a segment summary. 573 * XXX should use gseguse, but right now we need to do more 574 * setup before we can...fix this 575 */ 576 if (sp->ss_magic != SS_MAGIC || 577 sp->ss_ident != osb->lfs_ident || 578 sp->ss_serial < serial || 579 sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize - 580 sizeof(sp->ss_sumsum))) { 581 brelse(bp); 582 if (flag == 0) { 583 flag = 1; 584 daddr += btofsb(osb, LFS_SBPAD); 585 goto oncemore; 586 } 587 break; 588 } 589 ++serial; 590 bc = check_summary(osb, sp, daddr, debug, devvp, NULL); 591 if (bc == 0) { 592 brelse(bp); 593 break; 594 } 595 assert (bc > 0); 596 odaddr = daddr; 597 daddr += btofsb(osb, osb->lfs_sumsize + bc); 598 if (dtosn(osb, odaddr) != dtosn(osb, daddr) || 599 dtosn(osb, daddr) != dtosn(osb, daddr + 600 btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) { 601 daddr = sp->ss_next; 602 } 603 if (!(sp->ss_flags & SS_CONT)) 604 nodirop_daddr = daddr; 605 brelse(bp); 606 } 607 608 if (goal == 0) 609 return nodirop_daddr; 610 else 611 return daddr; 612} 613 614/* Use try_verify to check whether the newer superblock is valid. */ 615struct lfs * 616lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug) 617{ 618 ufs_daddr_t daddr; 619 struct lfs *osb, *nsb; 620 621 /* 622 * Verify the checkpoint of the newer superblock, 623 * if the timestamp/serial number of the two superblocks is 624 * different. 625 */ 626 627 if (debug) 628 printf("sb0 %lld, sb1 %lld\n", (long long) sb0->lfs_serial, 629 (long long) sb1->lfs_serial); 630 631 if ((sb0->lfs_version == 1 && 632 sb0->lfs_otstamp != sb1->lfs_otstamp) || 633 (sb0->lfs_version > 1 && 634 sb0->lfs_serial != sb1->lfs_serial)) { 635 if (sb0->lfs_version == 1) { 636 if (sb0->lfs_otstamp > sb1->lfs_otstamp) { 637 osb = sb1; 638 nsb = sb0; 639 } else { 640 osb = sb0; 641 nsb = sb1; 642 } 643 } else { 644 if (sb0->lfs_serial > sb1->lfs_serial) { 645 osb = sb1; 646 nsb = sb0; 647 } else { 648 osb = sb0; 649 nsb = sb1; 650 } 651 } 652 if (debug) { 653 printf("Attempting to verify newer checkpoint..."); 654 fflush(stdout); 655 } 656 daddr = try_verify(osb, devvp, nsb->lfs_offset, debug); 657 658 if (debug) 659 printf("done.\n"); 660 if (daddr == nsb->lfs_offset) { 661 warnx("** Newer checkpoint verified, recovered %lld seconds of data\n", 662 (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp); 663 sbdirty(); 664 } else { 665 warnx("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp); 666 } 667 return (daddr == nsb->lfs_offset ? nsb : osb); 668 } 669 /* Nothing to check */ 670 return osb; 671} 672 673/* Verify a partial-segment summary; return the number of bytes on disk. */ 674int 675check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug, 676 struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *))) 677{ 678 FINFO *fp; 679 int bc; /* Bytes in partial segment */ 680 int nblocks; 681 ufs_daddr_t seg_addr, daddr; 682 ufs_daddr_t *dp, *idp; 683 struct ubuf *bp; 684 int i, j, k, datac, len; 685 long sn; 686 u_int32_t *datap; 687 u_int32_t ccksum; 688 689 sn = dtosn(fs, pseg_addr); 690 seg_addr = sntod(fs, sn); 691 692 /* We've already checked the sumsum, just do the data bounds and sum */ 693 694 /* Count the blocks. */ 695 nblocks = howmany(sp->ss_ninos, INOPB(fs)); 696 bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift); 697 assert(bc >= 0); 698 699 fp = (FINFO *) (sp + 1); 700 for (i = 0; i < sp->ss_nfinfo; i++) { 701 nblocks += fp->fi_nblocks; 702 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1) 703 << fs->lfs_bshift); 704 assert(bc >= 0); 705 fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks); 706 } 707 datap = (u_int32_t *) malloc(nblocks * sizeof(*datap)); 708 datac = 0; 709 710 dp = (ufs_daddr_t *) sp; 711 dp += fs->lfs_sumsize / sizeof(ufs_daddr_t); 712 dp--; 713 714 idp = dp; 715 daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize); 716 fp = (FINFO *) (sp + 1); 717 for (i = 0, j = 0; 718 i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) { 719 if (i >= sp->ss_nfinfo && *idp != daddr) { 720 warnx("Not enough inode blocks in pseg at 0x%" PRIx32 721 ": found %d, wanted %d\n", 722 pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs))); 723 if (debug) 724 warnx("*idp=%x, daddr=%" PRIx32 "\n", *idp, 725 daddr); 726 break; 727 } 728 while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) { 729 bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp); 730 datap[datac++] = ((u_int32_t *) (bp->b_data))[0]; 731 brelse(bp); 732 733 ++j; 734 daddr += btofsb(fs, fs->lfs_ibsize); 735 --idp; 736 } 737 if (i < sp->ss_nfinfo) { 738 if (func) 739 func(daddr, fp); 740 for (k = 0; k < fp->fi_nblocks; k++) { 741 len = (k == fp->fi_nblocks - 1 ? 742 fp->fi_lastlength 743 : fs->lfs_bsize); 744 bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp); 745 datap[datac++] = ((u_int32_t *) (bp->b_data))[0]; 746 brelse(bp); 747 daddr += btofsb(fs, len); 748 } 749 fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks); 750 } 751 } 752 753 if (datac != nblocks) { 754 warnx("Partial segment at 0x%llx expected %d blocks counted %d\n", 755 (long long) pseg_addr, nblocks, datac); 756 } 757 ccksum = cksum(datap, nblocks * sizeof(u_int32_t)); 758 /* Check the data checksum */ 759 if (ccksum != sp->ss_datasum) { 760 warnx("Partial segment at 0x%" PRIx32 " data checksum" 761 " mismatch: given 0x%x, computed 0x%x\n", 762 pseg_addr, sp->ss_datasum, ccksum); 763 free(datap); 764 return 0; 765 } 766 free(datap); 767 assert(bc >= 0); 768 return bc; 769} 770 771/* print message and exit */ 772void 773my_vpanic(int fatal, const char *fmt, va_list ap) 774{ 775 (void) vprintf(fmt, ap); 776 exit(8); 777} 778 779void 780call_panic(const char *fmt, ...) 781{ 782 va_list ap; 783 784 va_start(ap, fmt); 785 panic_func(1, fmt, ap); 786 va_end(ap); 787} 788