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