1 /* $NetBSD: lfs.c,v 1.30 2009/02/22 20:28:05 ad 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 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 /* 31 * Copyright (c) 1989, 1991, 1993 32 * The Regents of the University of California. All rights reserved. 33 * (c) UNIX System Laboratories, Inc. 34 * All or some portions of this file are derived from material licensed 35 * to the University of California by American Telephone and Telegraph 36 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 37 * the permission of UNIX System Laboratories, Inc. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95 64 */ 65 66 67 #include <sys/types.h> 68 #include <sys/param.h> 69 #include <sys/time.h> 70 #include <sys/buf.h> 71 #include <sys/mount.h> 72 73 #include <ufs/ufs/inode.h> 74 #include <ufs/ufs/ufsmount.h> 75 #define vnode uvnode 76 #include <ufs/lfs/lfs.h> 77 #undef vnode 78 79 #include <assert.h> 80 #include <err.h> 81 #include <errno.h> 82 #include <stdarg.h> 83 #include <stdio.h> 84 #include <stdlib.h> 85 #include <string.h> 86 #include <unistd.h> 87 #include <util.h> 88 89 #include "bufcache.h" 90 #include "vnode.h" 91 #include "lfs_user.h" 92 #include "segwrite.h" 93 94 #define panic call_panic 95 96 extern u_int32_t cksum(void *, size_t); 97 extern u_int32_t lfs_sb_cksum(struct dlfs *); 98 extern void pwarn(const char *, ...); 99 100 extern struct uvnodelst vnodelist; 101 extern struct uvnodelst getvnodelist[VNODE_HASH_MAX]; 102 extern int nvnodes; 103 104 static int 105 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **); 106 107 int fsdirty = 0; 108 void (*panic_func)(int, const char *, va_list) = my_vpanic; 109 110 /* 111 * LFS buffer and uvnode operations 112 */ 113 114 int 115 lfs_vop_strategy(struct ubuf * bp) 116 { 117 int count; 118 119 if (bp->b_flags & B_READ) { 120 count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 121 dbtob(bp->b_blkno)); 122 if (count == bp->b_bcount) 123 bp->b_flags |= B_DONE; 124 } else { 125 count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 126 dbtob(bp->b_blkno)); 127 if (count == 0) { 128 perror("pwrite"); 129 return -1; 130 } 131 bp->b_flags &= ~B_DELWRI; 132 reassignbuf(bp, bp->b_vp); 133 } 134 return 0; 135 } 136 137 int 138 lfs_vop_bwrite(struct ubuf * bp) 139 { 140 struct lfs *fs; 141 142 fs = bp->b_vp->v_fs; 143 if (!(bp->b_flags & B_DELWRI)) { 144 fs->lfs_avail -= btofsb(fs, bp->b_bcount); 145 } 146 bp->b_flags |= B_DELWRI | B_LOCKED; 147 reassignbuf(bp, bp->b_vp); 148 brelse(bp, 0); 149 return 0; 150 } 151 152 /* 153 * ufs_bmaparray does the bmap conversion, and if requested returns the 154 * array of logical blocks which must be traversed to get to a block. 155 * Each entry contains the offset into that block that gets you to the 156 * next block and the disk address of the block (if it is assigned). 157 */ 158 int 159 ufs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump) 160 { 161 struct inode *ip; 162 struct ubuf *bp; 163 struct indir a[NIADDR + 1], *xap; 164 daddr_t daddr; 165 daddr_t metalbn; 166 int error, num; 167 168 ip = VTOI(vp); 169 170 if (bn >= 0 && bn < NDADDR) { 171 if (nump != NULL) 172 *nump = 0; 173 *bnp = fsbtodb(fs, ip->i_ffs1_db[bn]); 174 if (*bnp == 0) 175 *bnp = -1; 176 return (0); 177 } 178 xap = ap == NULL ? a : ap; 179 if (!nump) 180 nump = # 181 if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0) 182 return (error); 183 184 num = *nump; 185 186 /* Get disk address out of indirect block array */ 187 daddr = ip->i_ffs1_ib[xap->in_off]; 188 189 for (bp = NULL, ++xap; --num; ++xap) { 190 /* Exit the loop if there is no disk address assigned yet and 191 * the indirect block isn't in the cache, or if we were 192 * looking for an indirect block and we've found it. */ 193 194 metalbn = xap->in_lbn; 195 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn) 196 break; 197 /* 198 * If we get here, we've either got the block in the cache 199 * or we have a disk address for it, go fetch it. 200 */ 201 if (bp) 202 brelse(bp, 0); 203 204 xap->in_exists = 1; 205 bp = getblk(vp, metalbn, fs->lfs_bsize); 206 207 if (!(bp->b_flags & (B_DONE | B_DELWRI))) { 208 bp->b_blkno = fsbtodb(fs, daddr); 209 bp->b_flags |= B_READ; 210 VOP_STRATEGY(bp); 211 } 212 daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off]; 213 } 214 if (bp) 215 brelse(bp, 0); 216 217 daddr = fsbtodb(fs, (ufs_daddr_t) daddr); 218 *bnp = daddr == 0 ? -1 : daddr; 219 return (0); 220 } 221 222 /* 223 * Create an array of logical block number/offset pairs which represent the 224 * path of indirect blocks required to access a data block. The first "pair" 225 * contains the logical block number of the appropriate single, double or 226 * triple indirect block and the offset into the inode indirect block array. 227 * Note, the logical block number of the inode single/double/triple indirect 228 * block appears twice in the array, once with the offset into the i_ffs1_ib and 229 * once with the offset into the page itself. 230 */ 231 int 232 ufs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump) 233 { 234 daddr_t metalbn, realbn; 235 int64_t blockcnt; 236 int lbc; 237 int i, numlevels, off; 238 int lognindir, indir; 239 240 metalbn = 0; /* XXXGCC -Wuninitialized [sh3] */ 241 242 if (nump) 243 *nump = 0; 244 numlevels = 0; 245 realbn = bn; 246 if (bn < 0) 247 bn = -bn; 248 249 lognindir = -1; 250 for (indir = fs->lfs_nindir; indir; indir >>= 1) 251 ++lognindir; 252 253 /* Determine the number of levels of indirection. After this loop is 254 * done, blockcnt indicates the number of data blocks possible at the 255 * given level of indirection, and NIADDR - i is the number of levels 256 * of indirection needed to locate the requested block. */ 257 258 bn -= NDADDR; 259 for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) { 260 if (i == 0) 261 return (EFBIG); 262 263 lbc += lognindir; 264 blockcnt = (int64_t) 1 << lbc; 265 266 if (bn < blockcnt) 267 break; 268 } 269 270 /* Calculate the address of the first meta-block. */ 271 metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + NIADDR - i); 272 273 /* At each iteration, off is the offset into the bap array which is an 274 * array of disk addresses at the current level of indirection. The 275 * logical block number and the offset in that block are stored into 276 * the argument array. */ 277 ap->in_lbn = metalbn; 278 ap->in_off = off = NIADDR - i; 279 ap->in_exists = 0; 280 ap++; 281 for (++numlevels; i <= NIADDR; i++) { 282 /* If searching for a meta-data block, quit when found. */ 283 if (metalbn == realbn) 284 break; 285 286 lbc -= lognindir; 287 blockcnt = (int64_t) 1 << lbc; 288 off = (bn >> lbc) & (fs->lfs_nindir - 1); 289 290 ++numlevels; 291 ap->in_lbn = metalbn; 292 ap->in_off = off; 293 ap->in_exists = 0; 294 ++ap; 295 296 metalbn -= -1 + (off << lbc); 297 } 298 if (nump) 299 *nump = numlevels; 300 return (0); 301 } 302 303 int 304 lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp) 305 { 306 return ufs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL); 307 } 308 309 /* Search a block for a specific dinode. */ 310 struct ufs1_dinode * 311 lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp) 312 { 313 struct ufs1_dinode *dip = (struct ufs1_dinode *) bp->b_data; 314 struct ufs1_dinode *ldip, *fin; 315 316 fin = dip + INOPB(fs); 317 318 /* 319 * Read the inode block backwards, since later versions of the 320 * inode will supercede earlier ones. Though it is unlikely, it is 321 * possible that the same inode will appear in the same inode block. 322 */ 323 for (ldip = fin - 1; ldip >= dip; --ldip) 324 if (ldip->di_inumber == ino) 325 return (ldip); 326 return NULL; 327 } 328 329 /* 330 * lfs_raw_vget makes us a new vnode from the inode at the given disk address. 331 * XXX it currently loses atime information. 332 */ 333 struct uvnode * 334 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr) 335 { 336 struct uvnode *vp; 337 struct inode *ip; 338 struct ufs1_dinode *dip; 339 struct ubuf *bp; 340 int i, hash; 341 342 vp = ecalloc(1, sizeof(*vp)); 343 vp->v_fd = fd; 344 vp->v_fs = fs; 345 vp->v_usecount = 0; 346 vp->v_strategy_op = lfs_vop_strategy; 347 vp->v_bwrite_op = lfs_vop_bwrite; 348 vp->v_bmap_op = lfs_vop_bmap; 349 LIST_INIT(&vp->v_cleanblkhd); 350 LIST_INIT(&vp->v_dirtyblkhd); 351 352 ip = ecalloc(1, sizeof(*ip)); 353 354 ip->i_din.ffs1_din = ecalloc(1, sizeof(*ip->i_din.ffs1_din)); 355 356 /* Initialize the inode -- from lfs_vcreate. */ 357 ip->inode_ext.lfs = ecalloc(1, sizeof(*ip->inode_ext.lfs)); 358 vp->v_data = ip; 359 /* ip->i_vnode = vp; */ 360 ip->i_number = ino; 361 ip->i_lockf = 0; 362 ip->i_diroff = 0; 363 ip->i_lfs_effnblks = 0; 364 ip->i_flag = 0; 365 366 /* Load inode block and find inode */ 367 if (daddr > 0) { 368 bread(fs->lfs_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, 369 NULL, 0, &bp); 370 bp->b_flags |= B_AGE; 371 dip = lfs_ifind(fs, ino, bp); 372 if (dip == NULL) { 373 brelse(bp, 0); 374 free(ip); 375 free(vp); 376 return NULL; 377 } 378 memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip)); 379 brelse(bp, 0); 380 } 381 ip->i_number = ino; 382 /* ip->i_devvp = fs->lfs_devvp; */ 383 ip->i_lfs = fs; 384 385 ip->i_lfs_effnblks = ip->i_ffs1_blocks; 386 ip->i_lfs_osize = ip->i_ffs1_size; 387 #if 0 388 if (fs->lfs_version > 1) { 389 ip->i_ffs1_atime = ts.tv_sec; 390 ip->i_ffs1_atimensec = ts.tv_nsec; 391 } 392 #endif 393 394 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize)); 395 for (i = 0; i < NDADDR; i++) 396 if (ip->i_ffs1_db[i] != 0) 397 ip->i_lfs_fragsize[i] = blksize(fs, ip, i); 398 399 ++nvnodes; 400 hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1); 401 LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes); 402 LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes); 403 404 return vp; 405 } 406 407 static struct uvnode * 408 lfs_vget(void *vfs, ino_t ino) 409 { 410 struct lfs *fs = (struct lfs *)vfs; 411 ufs_daddr_t daddr; 412 struct ubuf *bp; 413 IFILE *ifp; 414 415 LFS_IENTRY(ifp, fs, ino, bp); 416 daddr = ifp->if_daddr; 417 brelse(bp, 0); 418 if (daddr <= 0 || dtosn(fs, daddr) >= fs->lfs_nseg) 419 return NULL; 420 return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr); 421 } 422 423 /* Check superblock magic number and checksum */ 424 static int 425 check_sb(struct lfs *fs) 426 { 427 u_int32_t checksum; 428 429 if (fs->lfs_magic != LFS_MAGIC) { 430 printf("Superblock magic number (0x%lx) does not match " 431 "expected 0x%lx\n", (unsigned long) fs->lfs_magic, 432 (unsigned long) LFS_MAGIC); 433 return 1; 434 } 435 /* checksum */ 436 checksum = lfs_sb_cksum(&(fs->lfs_dlfs)); 437 if (fs->lfs_cksum != checksum) { 438 printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n", 439 (unsigned long) fs->lfs_cksum, (unsigned long) checksum); 440 return 1; 441 } 442 return 0; 443 } 444 445 /* Initialize LFS library; load superblocks and choose which to use. */ 446 struct lfs * 447 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug) 448 { 449 struct uvnode *devvp; 450 struct ubuf *bp; 451 int tryalt; 452 struct lfs *fs, *altfs; 453 int error; 454 455 vfs_init(); 456 457 devvp = ecalloc(1, sizeof(*devvp)); 458 devvp->v_fs = NULL; 459 devvp->v_fd = devfd; 460 devvp->v_strategy_op = raw_vop_strategy; 461 devvp->v_bwrite_op = raw_vop_bwrite; 462 devvp->v_bmap_op = raw_vop_bmap; 463 LIST_INIT(&devvp->v_cleanblkhd); 464 LIST_INIT(&devvp->v_dirtyblkhd); 465 466 tryalt = 0; 467 if (dummy_read) { 468 if (sblkno == 0) 469 sblkno = btodb(LFS_LABELPAD); 470 fs = ecalloc(1, sizeof(*fs)); 471 fs->lfs_devvp = devvp; 472 } else { 473 if (sblkno == 0) { 474 sblkno = btodb(LFS_LABELPAD); 475 tryalt = 1; 476 } else if (debug) { 477 printf("No -b flag given, not attempting to verify checkpoint\n"); 478 } 479 error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, 0, &bp); 480 fs = ecalloc(1, sizeof(*fs)); 481 fs->lfs_dlfs = *((struct dlfs *) bp->b_data); 482 fs->lfs_devvp = devvp; 483 bp->b_flags |= B_INVAL; 484 brelse(bp, 0); 485 486 if (tryalt) { 487 error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]), 488 LFS_SBPAD, NOCRED, 0, &bp); 489 altfs = ecalloc(1, sizeof(*altfs)); 490 altfs->lfs_dlfs = *((struct dlfs *) bp->b_data); 491 altfs->lfs_devvp = devvp; 492 bp->b_flags |= B_INVAL; 493 brelse(bp, 0); 494 495 if (check_sb(fs) || fs->lfs_idaddr <= 0) { 496 if (debug) 497 printf("Primary superblock is no good, using first alternate\n"); 498 free(fs); 499 fs = altfs; 500 } else { 501 /* If both superblocks check out, try verification */ 502 if (check_sb(altfs)) { 503 if (debug) 504 printf("First alternate superblock is no good, using primary\n"); 505 free(altfs); 506 } else { 507 if (lfs_verify(fs, altfs, devvp, debug) == fs) { 508 free(altfs); 509 } else { 510 free(fs); 511 fs = altfs; 512 } 513 } 514 } 515 } 516 if (check_sb(fs)) { 517 free(fs); 518 return NULL; 519 } 520 } 521 522 /* Compatibility */ 523 if (fs->lfs_version < 2) { 524 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE; 525 fs->lfs_ibsize = fs->lfs_bsize; 526 fs->lfs_start = fs->lfs_sboffs[0]; 527 fs->lfs_tstamp = fs->lfs_otstamp; 528 fs->lfs_fsbtodb = 0; 529 } 530 531 if (!dummy_read) { 532 fs->lfs_suflags = emalloc(2 * sizeof(u_int32_t *)); 533 fs->lfs_suflags[0] = emalloc(fs->lfs_nseg * sizeof(u_int32_t)); 534 fs->lfs_suflags[1] = emalloc(fs->lfs_nseg * sizeof(u_int32_t)); 535 } 536 537 if (idaddr == 0) 538 idaddr = fs->lfs_idaddr; 539 else 540 fs->lfs_idaddr = idaddr; 541 /* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */ 542 fs->lfs_ivnode = lfs_raw_vget(fs, 543 (dummy_read ? LFS_IFILE_INUM : fs->lfs_ifile), devvp->v_fd, 544 idaddr); 545 if (fs->lfs_ivnode == NULL) 546 return NULL; 547 548 register_vget((void *)fs, lfs_vget); 549 550 return fs; 551 } 552 553 /* 554 * Check partial segment validity between fs->lfs_offset and the given goal. 555 * 556 * If goal == 0, just keep on going until the segments stop making sense, 557 * and return the address of the last valid partial segment. 558 * 559 * If goal != 0, return the address of the first partial segment that failed, 560 * or "goal" if we reached it without failure (the partial segment *at* goal 561 * need not be valid). 562 */ 563 ufs_daddr_t 564 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug) 565 { 566 ufs_daddr_t daddr, odaddr; 567 SEGSUM *sp; 568 int i, bc, hitclean; 569 struct ubuf *bp; 570 ufs_daddr_t nodirop_daddr; 571 u_int64_t serial; 572 573 bc = 0; 574 hitclean = 0; 575 odaddr = -1; 576 daddr = osb->lfs_offset; 577 nodirop_daddr = daddr; 578 serial = osb->lfs_serial; 579 while (daddr != goal) { 580 /* 581 * Don't mistakenly read a superblock, if there is one here. 582 */ 583 if (sntod(osb, dtosn(osb, daddr)) == daddr) { 584 if (daddr == osb->lfs_start) 585 daddr += btofsb(osb, LFS_LABELPAD); 586 for (i = 0; i < LFS_MAXNUMSB; i++) { 587 if (osb->lfs_sboffs[i] < daddr) 588 break; 589 if (osb->lfs_sboffs[i] == daddr) 590 daddr += btofsb(osb, LFS_SBPAD); 591 } 592 } 593 594 /* Read in summary block */ 595 bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, 596 NULL, 0, &bp); 597 sp = (SEGSUM *)bp->b_data; 598 599 /* 600 * Check for a valid segment summary belonging to our fs. 601 */ 602 if (sp->ss_magic != SS_MAGIC || 603 sp->ss_ident != osb->lfs_ident || 604 sp->ss_serial < serial || /* XXX strengthen this */ 605 sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize - 606 sizeof(sp->ss_sumsum))) { 607 brelse(bp, 0); 608 if (debug) { 609 if (sp->ss_magic != SS_MAGIC) 610 pwarn("pseg at 0x%x: " 611 "wrong magic number\n", 612 (int)daddr); 613 else if (sp->ss_ident != osb->lfs_ident) 614 pwarn("pseg at 0x%x: " 615 "expected ident %llx, got %llx\n", 616 (int)daddr, 617 (long long)sp->ss_ident, 618 (long long)osb->lfs_ident); 619 else if (sp->ss_serial >= serial) 620 pwarn("pseg at 0x%x: " 621 "serial %d < %d\n", (int)daddr, 622 (int)sp->ss_serial, (int)serial); 623 else 624 pwarn("pseg at 0x%x: " 625 "summary checksum wrong\n", 626 (int)daddr); 627 } 628 break; 629 } 630 if (debug && sp->ss_serial != serial) 631 pwarn("warning, serial=%d ss_serial=%d\n", 632 (int)serial, (int)sp->ss_serial); 633 ++serial; 634 bc = check_summary(osb, sp, daddr, debug, devvp, NULL); 635 if (bc == 0) { 636 brelse(bp, 0); 637 break; 638 } 639 if (debug) 640 pwarn("summary good: 0x%x/%d\n", (int)daddr, 641 (int)sp->ss_serial); 642 assert (bc > 0); 643 odaddr = daddr; 644 daddr += btofsb(osb, osb->lfs_sumsize + bc); 645 if (dtosn(osb, odaddr) != dtosn(osb, daddr) || 646 dtosn(osb, daddr) != dtosn(osb, daddr + 647 btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize) - 1)) { 648 daddr = sp->ss_next; 649 } 650 651 /* 652 * Check for the beginning and ending of a sequence of 653 * dirops. Writes from the cleaner never involve new 654 * information, and are always checkpoints; so don't try 655 * to roll forward through them. Likewise, psegs written 656 * by a previous roll-forward attempt are not interesting. 657 */ 658 if (sp->ss_flags & (SS_CLEAN | SS_RFW)) 659 hitclean = 1; 660 if (hitclean == 0 && (sp->ss_flags & SS_CONT) == 0) 661 nodirop_daddr = daddr; 662 663 brelse(bp, 0); 664 } 665 666 if (goal == 0) 667 return nodirop_daddr; 668 else 669 return daddr; 670 } 671 672 /* Use try_verify to check whether the newer superblock is valid. */ 673 struct lfs * 674 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug) 675 { 676 ufs_daddr_t daddr; 677 struct lfs *osb, *nsb; 678 679 /* 680 * Verify the checkpoint of the newer superblock, 681 * if the timestamp/serial number of the two superblocks is 682 * different. 683 */ 684 685 osb = NULL; 686 if (debug) 687 pwarn("sb0 %lld, sb1 %lld", 688 (long long) sb0->lfs_serial, 689 (long long) sb1->lfs_serial); 690 691 if ((sb0->lfs_version == 1 && 692 sb0->lfs_otstamp != sb1->lfs_otstamp) || 693 (sb0->lfs_version > 1 && 694 sb0->lfs_serial != sb1->lfs_serial)) { 695 if (sb0->lfs_version == 1) { 696 if (sb0->lfs_otstamp > sb1->lfs_otstamp) { 697 osb = sb1; 698 nsb = sb0; 699 } else { 700 osb = sb0; 701 nsb = sb1; 702 } 703 } else { 704 if (sb0->lfs_serial > sb1->lfs_serial) { 705 osb = sb1; 706 nsb = sb0; 707 } else { 708 osb = sb0; 709 nsb = sb1; 710 } 711 } 712 if (debug) { 713 printf("Attempting to verify newer checkpoint..."); 714 fflush(stdout); 715 } 716 daddr = try_verify(osb, devvp, nsb->lfs_offset, debug); 717 718 if (debug) 719 printf("done.\n"); 720 if (daddr == nsb->lfs_offset) { 721 pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n", 722 (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp); 723 sbdirty(); 724 } else { 725 pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp); 726 } 727 return (daddr == nsb->lfs_offset ? nsb : osb); 728 } 729 /* Nothing to check */ 730 return osb; 731 } 732 733 /* Verify a partial-segment summary; return the number of bytes on disk. */ 734 int 735 check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug, 736 struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *))) 737 { 738 FINFO *fp; 739 int bc; /* Bytes in partial segment */ 740 int nblocks; 741 ufs_daddr_t seg_addr, daddr; 742 ufs_daddr_t *dp, *idp; 743 struct ubuf *bp; 744 int i, j, k, datac, len; 745 long sn; 746 u_int32_t *datap; 747 u_int32_t ccksum; 748 749 sn = dtosn(fs, pseg_addr); 750 seg_addr = sntod(fs, sn); 751 752 /* We've already checked the sumsum, just do the data bounds and sum */ 753 754 /* Count the blocks. */ 755 nblocks = howmany(sp->ss_ninos, INOPB(fs)); 756 bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift); 757 assert(bc >= 0); 758 759 fp = (FINFO *) (sp + 1); 760 for (i = 0; i < sp->ss_nfinfo; i++) { 761 nblocks += fp->fi_nblocks; 762 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1) 763 << fs->lfs_bshift); 764 assert(bc >= 0); 765 fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks); 766 if (((char *)fp) - (char *)sp > fs->lfs_sumsize) 767 return 0; 768 } 769 datap = emalloc(nblocks * sizeof(*datap)); 770 datac = 0; 771 772 dp = (ufs_daddr_t *) sp; 773 dp += fs->lfs_sumsize / sizeof(ufs_daddr_t); 774 dp--; 775 776 idp = dp; 777 daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize); 778 fp = (FINFO *) (sp + 1); 779 for (i = 0, j = 0; 780 i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) { 781 if (i >= sp->ss_nfinfo && *idp != daddr) { 782 pwarn("Not enough inode blocks in pseg at 0x%" PRIx32 783 ": found %d, wanted %d\n", 784 pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs))); 785 if (debug) 786 pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp, 787 daddr); 788 break; 789 } 790 while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) { 791 bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, 792 NOCRED, 0, &bp); 793 datap[datac++] = ((u_int32_t *) (bp->b_data))[0]; 794 brelse(bp, 0); 795 796 ++j; 797 daddr += btofsb(fs, fs->lfs_ibsize); 798 --idp; 799 } 800 if (i < sp->ss_nfinfo) { 801 if (func) 802 func(daddr, fp); 803 for (k = 0; k < fp->fi_nblocks; k++) { 804 len = (k == fp->fi_nblocks - 1 ? 805 fp->fi_lastlength 806 : fs->lfs_bsize); 807 bread(devvp, fsbtodb(fs, daddr), len, 808 NOCRED, 0, &bp); 809 datap[datac++] = ((u_int32_t *) (bp->b_data))[0]; 810 brelse(bp, 0); 811 daddr += btofsb(fs, len); 812 } 813 fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks); 814 } 815 } 816 817 if (datac != nblocks) { 818 pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n", 819 (long long) pseg_addr, nblocks, datac); 820 } 821 ccksum = cksum(datap, nblocks * sizeof(u_int32_t)); 822 /* Check the data checksum */ 823 if (ccksum != sp->ss_datasum) { 824 pwarn("Partial segment at 0x%" PRIx32 " data checksum" 825 " mismatch: given 0x%x, computed 0x%x\n", 826 pseg_addr, sp->ss_datasum, ccksum); 827 free(datap); 828 return 0; 829 } 830 free(datap); 831 assert(bc >= 0); 832 return bc; 833 } 834 835 /* print message and exit */ 836 void 837 my_vpanic(int fatal, const char *fmt, va_list ap) 838 { 839 (void) vprintf(fmt, ap); 840 exit(8); 841 } 842 843 void 844 call_panic(const char *fmt, ...) 845 { 846 va_list ap; 847 848 va_start(ap, fmt); 849 panic_func(1, fmt, ap); 850 va_end(ap); 851 } 852 853 /* Allocate a new inode. */ 854 struct uvnode * 855 lfs_valloc(struct lfs *fs, ino_t ino) 856 { 857 struct ubuf *bp, *cbp; 858 struct ifile *ifp; 859 ino_t new_ino; 860 int error; 861 int new_gen; 862 CLEANERINFO *cip; 863 864 /* Get the head of the freelist. */ 865 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino); 866 867 /* 868 * Remove the inode from the free list and write the new start 869 * of the free list into the superblock. 870 */ 871 LFS_IENTRY(ifp, fs, new_ino, bp); 872 if (ifp->if_daddr != LFS_UNUSED_DADDR) 873 panic("lfs_valloc: inuse inode %d on the free list", new_ino); 874 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree); 875 876 new_gen = ifp->if_version; /* version was updated by vfree */ 877 brelse(bp, 0); 878 879 /* Extend IFILE so that the next lfs_valloc will succeed. */ 880 if (fs->lfs_freehd == LFS_UNUSED_INUM) { 881 if ((error = extend_ifile(fs)) != 0) { 882 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino); 883 return NULL; 884 } 885 } 886 887 /* Set superblock modified bit and increment file count. */ 888 sbdirty(); 889 ++fs->lfs_nfiles; 890 891 return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0); 892 } 893 894 #ifdef IN_FSCK_LFS 895 void reset_maxino(ino_t); 896 #endif 897 898 /* 899 * Add a new block to the Ifile, to accommodate future file creations. 900 */ 901 int 902 extend_ifile(struct lfs *fs) 903 { 904 struct uvnode *vp; 905 struct inode *ip; 906 IFILE *ifp; 907 IFILE_V1 *ifp_v1; 908 struct ubuf *bp, *cbp; 909 daddr_t i, blkno, max; 910 ino_t oldlast; 911 CLEANERINFO *cip; 912 913 vp = fs->lfs_ivnode; 914 ip = VTOI(vp); 915 blkno = lblkno(fs, ip->i_ffs1_size); 916 917 lfs_balloc(vp, ip->i_ffs1_size, fs->lfs_bsize, &bp); 918 ip->i_ffs1_size += fs->lfs_bsize; 919 ip->i_flag |= IN_MODIFIED; 920 921 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 922 fs->lfs_ifpb; 923 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast); 924 LFS_PUT_HEADFREE(fs, cip, cbp, i); 925 max = i + fs->lfs_ifpb; 926 fs->lfs_bfree -= btofsb(fs, fs->lfs_bsize); 927 928 if (fs->lfs_version == 1) { 929 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) { 930 ifp_v1->if_version = 1; 931 ifp_v1->if_daddr = LFS_UNUSED_DADDR; 932 ifp_v1->if_nextfree = ++i; 933 } 934 ifp_v1--; 935 ifp_v1->if_nextfree = oldlast; 936 } else { 937 for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) { 938 ifp->if_version = 1; 939 ifp->if_daddr = LFS_UNUSED_DADDR; 940 ifp->if_nextfree = ++i; 941 } 942 ifp--; 943 ifp->if_nextfree = oldlast; 944 } 945 LFS_PUT_TAILFREE(fs, cip, cbp, max - 1); 946 947 LFS_BWRITE_LOG(bp); 948 949 #ifdef IN_FSCK_LFS 950 reset_maxino(((ip->i_ffs1_size >> fs->lfs_bshift) - fs->lfs_segtabsz - 951 fs->lfs_cleansz) * fs->lfs_ifpb); 952 #endif 953 return 0; 954 } 955 956 /* 957 * Allocate a block, and to inode and filesystem block accounting for it 958 * and for any indirect blocks the may need to be created in order for 959 * this block to be created. 960 * 961 * Blocks which have never been accounted for (i.e., which "do not exist") 962 * have disk address 0, which is translated by ufs_bmap to the special value 963 * UNASSIGNED == -1, as in the historical UFS. 964 * 965 * Blocks which have been accounted for but which have not yet been written 966 * to disk are given the new special disk address UNWRITTEN == -2, so that 967 * they can be differentiated from completely new blocks. 968 */ 969 int 970 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp) 971 { 972 int offset; 973 daddr_t daddr, idaddr; 974 struct ubuf *ibp, *bp; 975 struct inode *ip; 976 struct lfs *fs; 977 struct indir indirs[NIADDR+2], *idp; 978 daddr_t lbn, lastblock; 979 int bb, bcount; 980 int error, frags, i, nsize, osize, num; 981 982 ip = VTOI(vp); 983 fs = ip->i_lfs; 984 offset = blkoff(fs, startoffset); 985 lbn = lblkno(fs, startoffset); 986 987 /* 988 * Three cases: it's a block beyond the end of file, it's a block in 989 * the file that may or may not have been assigned a disk address or 990 * we're writing an entire block. 991 * 992 * Note, if the daddr is UNWRITTEN, the block already exists in 993 * the cache (it was read or written earlier). If so, make sure 994 * we don't count it as a new block or zero out its contents. If 995 * it did not, make sure we allocate any necessary indirect 996 * blocks. 997 * 998 * If we are writing a block beyond the end of the file, we need to 999 * check if the old last block was a fragment. If it was, we need 1000 * to rewrite it. 1001 */ 1002 1003 if (bpp) 1004 *bpp = NULL; 1005 1006 /* Check for block beyond end of file and fragment extension needed. */ 1007 lastblock = lblkno(fs, ip->i_ffs1_size); 1008 if (lastblock < NDADDR && lastblock < lbn) { 1009 osize = blksize(fs, ip, lastblock); 1010 if (osize < fs->lfs_bsize && osize > 0) { 1011 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize, 1012 lastblock, 1013 (bpp ? &bp : NULL)))) 1014 return (error); 1015 ip->i_ffs1_size = ip->i_ffs1_size = 1016 (lastblock + 1) * fs->lfs_bsize; 1017 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1018 if (bpp) 1019 (void) VOP_BWRITE(bp); 1020 } 1021 } 1022 1023 /* 1024 * If the block we are writing is a direct block, it's the last 1025 * block in the file, and offset + iosize is less than a full 1026 * block, we can write one or more fragments. There are two cases: 1027 * the block is brand new and we should allocate it the correct 1028 * size or it already exists and contains some fragments and 1029 * may need to extend it. 1030 */ 1031 if (lbn < NDADDR && lblkno(fs, ip->i_ffs1_size) <= lbn) { 1032 osize = blksize(fs, ip, lbn); 1033 nsize = fragroundup(fs, offset + iosize); 1034 if (lblktosize(fs, lbn) >= ip->i_ffs1_size) { 1035 /* Brand new block or fragment */ 1036 frags = numfrags(fs, nsize); 1037 bb = fragstofsb(fs, frags); 1038 if (bpp) { 1039 *bpp = bp = getblk(vp, lbn, nsize); 1040 bp->b_blkno = UNWRITTEN; 1041 } 1042 ip->i_lfs_effnblks += bb; 1043 fs->lfs_bfree -= bb; 1044 ip->i_ffs1_db[lbn] = UNWRITTEN; 1045 } else { 1046 if (nsize <= osize) { 1047 /* No need to extend */ 1048 if (bpp && (error = bread(vp, lbn, osize, 1049 NOCRED, 0, &bp))) 1050 return error; 1051 } else { 1052 /* Extend existing block */ 1053 if ((error = 1054 lfs_fragextend(vp, osize, nsize, lbn, 1055 (bpp ? &bp : NULL)))) 1056 return error; 1057 } 1058 if (bpp) 1059 *bpp = bp; 1060 } 1061 return 0; 1062 } 1063 1064 error = ufs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num); 1065 if (error) 1066 return (error); 1067 1068 daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */ 1069 1070 /* 1071 * Do byte accounting all at once, so we can gracefully fail *before* 1072 * we start assigning blocks. 1073 */ 1074 bb = fsbtodb(fs, 1); /* bb = VFSTOUFS(vp->v_mount)->um_seqinc; */ 1075 bcount = 0; 1076 if (daddr == UNASSIGNED) { 1077 bcount = bb; 1078 } 1079 for (i = 1; i < num; ++i) { 1080 if (!indirs[i].in_exists) { 1081 bcount += bb; 1082 } 1083 } 1084 fs->lfs_bfree -= bcount; 1085 ip->i_lfs_effnblks += bcount; 1086 1087 if (daddr == UNASSIGNED) { 1088 if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) { 1089 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN; 1090 } 1091 1092 /* 1093 * Create new indirect blocks if necessary 1094 */ 1095 if (num > 1) { 1096 idaddr = ip->i_ffs1_ib[indirs[0].in_off]; 1097 for (i = 1; i < num; ++i) { 1098 ibp = getblk(vp, indirs[i].in_lbn, 1099 fs->lfs_bsize); 1100 if (!indirs[i].in_exists) { 1101 memset(ibp->b_data, 0, ibp->b_bufsize); 1102 ibp->b_blkno = UNWRITTEN; 1103 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) { 1104 ibp->b_blkno = fsbtodb(fs, idaddr); 1105 ibp->b_flags |= B_READ; 1106 VOP_STRATEGY(ibp); 1107 } 1108 /* 1109 * This block exists, but the next one may not. 1110 * If that is the case mark it UNWRITTEN to 1111 * keep the accounting straight. 1112 */ 1113 /* XXX ondisk32 */ 1114 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0) 1115 ((int32_t *)ibp->b_data)[indirs[i].in_off] = 1116 UNWRITTEN; 1117 /* XXX ondisk32 */ 1118 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off]; 1119 if ((error = VOP_BWRITE(ibp))) 1120 return error; 1121 } 1122 } 1123 } 1124 1125 1126 /* 1127 * Get the existing block from the cache, if requested. 1128 */ 1129 frags = fsbtofrags(fs, bb); 1130 if (bpp) 1131 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn)); 1132 1133 /* 1134 * The block we are writing may be a brand new block 1135 * in which case we need to do accounting. 1136 * 1137 * We can tell a truly new block because ufs_bmaparray will say 1138 * it is UNASSIGNED. Once we allocate it we will assign it the 1139 * disk address UNWRITTEN. 1140 */ 1141 if (daddr == UNASSIGNED) { 1142 if (bpp) { 1143 /* Note the new address */ 1144 bp->b_blkno = UNWRITTEN; 1145 } 1146 1147 switch (num) { 1148 case 0: 1149 ip->i_ffs1_db[lbn] = UNWRITTEN; 1150 break; 1151 case 1: 1152 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN; 1153 break; 1154 default: 1155 idp = &indirs[num - 1]; 1156 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED, 1157 0, &ibp)) 1158 panic("lfs_balloc: bread bno %lld", 1159 (long long)idp->in_lbn); 1160 /* XXX ondisk32 */ 1161 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN; 1162 VOP_BWRITE(ibp); 1163 } 1164 } else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) { 1165 /* 1166 * Not a brand new block, also not in the cache; 1167 * read it in from disk. 1168 */ 1169 if (iosize == fs->lfs_bsize) 1170 /* Optimization: I/O is unnecessary. */ 1171 bp->b_blkno = daddr; 1172 else { 1173 /* 1174 * We need to read the block to preserve the 1175 * existing bytes. 1176 */ 1177 bp->b_blkno = daddr; 1178 bp->b_flags |= B_READ; 1179 VOP_STRATEGY(bp); 1180 return 0; 1181 } 1182 } 1183 1184 return (0); 1185 } 1186 1187 int 1188 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn, 1189 struct ubuf **bpp) 1190 { 1191 struct inode *ip; 1192 struct lfs *fs; 1193 long bb; 1194 int error; 1195 size_t obufsize; 1196 1197 ip = VTOI(vp); 1198 fs = ip->i_lfs; 1199 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize)); 1200 error = 0; 1201 1202 /* 1203 * If we are not asked to actually return the block, all we need 1204 * to do is allocate space for it. UBC will handle dirtying the 1205 * appropriate things and making sure it all goes to disk. 1206 * Don't bother to read in that case. 1207 */ 1208 if (bpp && (error = bread(vp, lbn, osize, NOCRED, 0, bpp))) { 1209 brelse(*bpp, 0); 1210 goto out; 1211 } 1212 1213 fs->lfs_bfree -= bb; 1214 ip->i_lfs_effnblks += bb; 1215 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1216 1217 if (bpp) { 1218 obufsize = (*bpp)->b_bufsize; 1219 (*bpp)->b_data = erealloc((*bpp)->b_data, nsize); 1220 (void)memset((*bpp)->b_data + osize, 0, nsize - osize); 1221 } 1222 1223 out: 1224 return (error); 1225 } 1226