1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/disk.h> 32 #include <sys/disklabel.h> 33 #include <sys/mount.h> 34 #include <sys/stat.h> 35 36 #include <ufs/ufs/extattr.h> 37 #include <ufs/ufs/quota.h> 38 #include <ufs/ufs/ufsmount.h> 39 #include <ufs/ufs/dinode.h> 40 #include <ufs/ufs/dir.h> 41 #include <ufs/ffs/fs.h> 42 43 #include <assert.h> 44 #include <err.h> 45 #include <setjmp.h> 46 #include <stdarg.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <stdint.h> 50 #include <libufs.h> 51 #include <string.h> 52 #include <strings.h> 53 #include <sysexits.h> 54 #include <time.h> 55 56 #include "fsck.h" 57 58 #define DOTDOT_OFFSET DIRECTSIZ(1) 59 60 struct suj_seg { 61 TAILQ_ENTRY(suj_seg) ss_next; 62 struct jsegrec ss_rec; 63 uint8_t *ss_blk; 64 }; 65 66 struct suj_rec { 67 TAILQ_ENTRY(suj_rec) sr_next; 68 union jrec *sr_rec; 69 }; 70 TAILQ_HEAD(srechd, suj_rec); 71 72 struct suj_ino { 73 LIST_ENTRY(suj_ino) si_next; 74 struct srechd si_recs; 75 struct srechd si_newrecs; 76 struct srechd si_movs; 77 struct jtrncrec *si_trunc; 78 ino_t si_ino; 79 char si_skipparent; 80 char si_hasrecs; 81 char si_blkadj; 82 char si_linkadj; 83 int si_mode; 84 nlink_t si_nlinkadj; 85 nlink_t si_nlink; 86 nlink_t si_dotlinks; 87 }; 88 LIST_HEAD(inohd, suj_ino); 89 90 struct suj_blk { 91 LIST_ENTRY(suj_blk) sb_next; 92 struct srechd sb_recs; 93 ufs2_daddr_t sb_blk; 94 }; 95 LIST_HEAD(blkhd, suj_blk); 96 97 struct suj_cg { 98 LIST_ENTRY(suj_cg) sc_next; 99 struct blkhd sc_blkhash[HASHSIZE]; 100 struct inohd sc_inohash[HASHSIZE]; 101 struct ino_blk *sc_lastiblk; 102 struct suj_ino *sc_lastino; 103 struct suj_blk *sc_lastblk; 104 struct bufarea *sc_cgbp; 105 struct cg *sc_cgp; 106 int sc_cgx; 107 }; 108 109 static LIST_HEAD(cghd, suj_cg) cghash[HASHSIZE]; 110 static struct suj_cg *lastcg; 111 112 static TAILQ_HEAD(seghd, suj_seg) allsegs; 113 static uint64_t oldseq; 114 static struct fs *fs = NULL; 115 static ino_t sujino; 116 static char *joptype[JOP_NUMJOPTYPES] = JOP_NAMES; 117 118 /* 119 * Summary statistics. 120 */ 121 static uint64_t freefrags; 122 static uint64_t freeblocks; 123 static uint64_t freeinos; 124 static uint64_t freedir; 125 static uint64_t jbytes; 126 static uint64_t jrecs; 127 128 static jmp_buf jmpbuf; 129 130 typedef void (*ino_visitor)(ino_t, ufs_lbn_t, ufs2_daddr_t, int); 131 static void err_suj(const char *, ...) __dead2; 132 static void ino_trunc(ino_t, off_t); 133 static void ino_decr(ino_t); 134 static void ino_adjust(struct suj_ino *); 135 static void ino_build(struct suj_ino *); 136 static int blk_isfree(ufs2_daddr_t); 137 static void initsuj(void); 138 139 static void * 140 errmalloc(size_t n) 141 { 142 void *a; 143 144 a = Malloc(n); 145 if (a == NULL) 146 err(EX_OSERR, "malloc(%zu)", n); 147 return (a); 148 } 149 150 /* 151 * When hit a fatal error in journalling check, print out 152 * the error and then offer to fallback to normal fsck. 153 */ 154 static void 155 err_suj(const char * restrict fmt, ...) 156 { 157 va_list ap; 158 159 if (preen) 160 (void)fprintf(stdout, "%s: ", cdevname); 161 162 va_start(ap, fmt); 163 (void)vfprintf(stdout, fmt, ap); 164 va_end(ap); 165 166 longjmp(jmpbuf, -1); 167 } 168 169 /* 170 * Lookup a cg by number in the hash so we can keep track of which cgs 171 * need stats rebuilt. 172 */ 173 static struct suj_cg * 174 cg_lookup(int cgx) 175 { 176 struct cghd *hd; 177 struct suj_cg *sc; 178 struct bufarea *cgbp; 179 180 if (cgx < 0 || cgx >= fs->fs_ncg) 181 err_suj("Bad cg number %d\n", cgx); 182 if (lastcg && lastcg->sc_cgx == cgx) 183 return (lastcg); 184 cgbp = cglookup(cgx); 185 if (!check_cgmagic(cgx, cgbp)) 186 err_suj("UNABLE TO REBUILD CYLINDER GROUP %d", cgx); 187 hd = &cghash[HASH(cgx)]; 188 LIST_FOREACH(sc, hd, sc_next) 189 if (sc->sc_cgx == cgx) { 190 sc->sc_cgbp = cgbp; 191 sc->sc_cgp = sc->sc_cgbp->b_un.b_cg; 192 lastcg = sc; 193 return (sc); 194 } 195 sc = errmalloc(sizeof(*sc)); 196 bzero(sc, sizeof(*sc)); 197 sc->sc_cgbp = cgbp; 198 sc->sc_cgp = sc->sc_cgbp->b_un.b_cg; 199 sc->sc_cgx = cgx; 200 LIST_INSERT_HEAD(hd, sc, sc_next); 201 return (sc); 202 } 203 204 /* 205 * Lookup an inode number in the hash and allocate a suj_ino if it does 206 * not exist. 207 */ 208 static struct suj_ino * 209 ino_lookup(ino_t ino, int creat) 210 { 211 struct suj_ino *sino; 212 struct inohd *hd; 213 struct suj_cg *sc; 214 215 sc = cg_lookup(ino_to_cg(fs, ino)); 216 if (sc->sc_lastino && sc->sc_lastino->si_ino == ino) 217 return (sc->sc_lastino); 218 hd = &sc->sc_inohash[HASH(ino)]; 219 LIST_FOREACH(sino, hd, si_next) 220 if (sino->si_ino == ino) 221 return (sino); 222 if (creat == 0) 223 return (NULL); 224 sino = errmalloc(sizeof(*sino)); 225 bzero(sino, sizeof(*sino)); 226 sino->si_ino = ino; 227 TAILQ_INIT(&sino->si_recs); 228 TAILQ_INIT(&sino->si_newrecs); 229 TAILQ_INIT(&sino->si_movs); 230 LIST_INSERT_HEAD(hd, sino, si_next); 231 232 return (sino); 233 } 234 235 /* 236 * Lookup a block number in the hash and allocate a suj_blk if it does 237 * not exist. 238 */ 239 static struct suj_blk * 240 blk_lookup(ufs2_daddr_t blk, int creat) 241 { 242 struct suj_blk *sblk; 243 struct suj_cg *sc; 244 struct blkhd *hd; 245 246 sc = cg_lookup(dtog(fs, blk)); 247 if (sc->sc_lastblk && sc->sc_lastblk->sb_blk == blk) 248 return (sc->sc_lastblk); 249 hd = &sc->sc_blkhash[HASH(fragstoblks(fs, blk))]; 250 LIST_FOREACH(sblk, hd, sb_next) 251 if (sblk->sb_blk == blk) 252 return (sblk); 253 if (creat == 0) 254 return (NULL); 255 sblk = errmalloc(sizeof(*sblk)); 256 bzero(sblk, sizeof(*sblk)); 257 sblk->sb_blk = blk; 258 TAILQ_INIT(&sblk->sb_recs); 259 LIST_INSERT_HEAD(hd, sblk, sb_next); 260 261 return (sblk); 262 } 263 264 static int 265 blk_overlaps(struct jblkrec *brec, ufs2_daddr_t start, int frags) 266 { 267 ufs2_daddr_t bstart; 268 ufs2_daddr_t bend; 269 ufs2_daddr_t end; 270 271 end = start + frags; 272 bstart = brec->jb_blkno + brec->jb_oldfrags; 273 bend = bstart + brec->jb_frags; 274 if (start < bend && end > bstart) 275 return (1); 276 return (0); 277 } 278 279 static int 280 blk_equals(struct jblkrec *brec, ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t start, 281 int frags) 282 { 283 284 if (brec->jb_ino != ino || brec->jb_lbn != lbn) 285 return (0); 286 if (brec->jb_blkno + brec->jb_oldfrags != start) 287 return (0); 288 if (brec->jb_frags < frags) 289 return (0); 290 return (1); 291 } 292 293 static void 294 blk_setmask(struct jblkrec *brec, int *mask) 295 { 296 int i; 297 298 for (i = brec->jb_oldfrags; i < brec->jb_oldfrags + brec->jb_frags; i++) 299 *mask |= 1 << i; 300 } 301 302 /* 303 * Determine whether a given block has been reallocated to a new location. 304 * Returns a mask of overlapping bits if any frags have been reused or 305 * zero if the block has not been re-used and the contents can be trusted. 306 * 307 * This is used to ensure that an orphaned pointer due to truncate is safe 308 * to be freed. The mask value can be used to free partial blocks. 309 */ 310 static int 311 blk_freemask(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags) 312 { 313 struct suj_blk *sblk; 314 struct suj_rec *srec; 315 struct jblkrec *brec; 316 int mask; 317 int off; 318 319 /* 320 * To be certain we're not freeing a reallocated block we lookup 321 * this block in the blk hash and see if there is an allocation 322 * journal record that overlaps with any fragments in the block 323 * we're concerned with. If any fragments have been reallocated 324 * the block has already been freed and re-used for another purpose. 325 */ 326 mask = 0; 327 sblk = blk_lookup(blknum(fs, blk), 0); 328 if (sblk == NULL) 329 return (0); 330 off = blk - sblk->sb_blk; 331 TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) { 332 brec = (struct jblkrec *)srec->sr_rec; 333 /* 334 * If the block overlaps but does not match 335 * exactly this record refers to the current 336 * location. 337 */ 338 if (blk_overlaps(brec, blk, frags) == 0) 339 continue; 340 if (blk_equals(brec, ino, lbn, blk, frags) == 1) 341 mask = 0; 342 else 343 blk_setmask(brec, &mask); 344 } 345 if (debug) 346 printf("blk_freemask: blk %jd sblk %jd off %d mask 0x%X\n", 347 blk, sblk->sb_blk, off, mask); 348 return (mask >> off); 349 } 350 351 /* 352 * Determine whether it is safe to follow an indirect. It is not safe 353 * if any part of the indirect has been reallocated or the last journal 354 * entry was an allocation. Just allocated indirects may not have valid 355 * pointers yet and all of their children will have their own records. 356 * It is also not safe to follow an indirect if the cg bitmap has been 357 * cleared as a new allocation may write to the block prior to the journal 358 * being written. 359 * 360 * Returns 1 if it's safe to follow the indirect and 0 otherwise. 361 */ 362 static int 363 blk_isindir(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn) 364 { 365 struct suj_blk *sblk; 366 struct jblkrec *brec; 367 368 sblk = blk_lookup(blk, 0); 369 if (sblk == NULL) 370 return (1); 371 if (TAILQ_EMPTY(&sblk->sb_recs)) 372 return (1); 373 brec = (struct jblkrec *)TAILQ_LAST(&sblk->sb_recs, srechd)->sr_rec; 374 if (blk_equals(brec, ino, lbn, blk, fs->fs_frag)) 375 if (brec->jb_op == JOP_FREEBLK) 376 return (!blk_isfree(blk)); 377 return (0); 378 } 379 380 /* 381 * Check to see if the requested block is available. 382 * We can just check in the cylinder-group maps as 383 * they will only have usable blocks in them. 384 */ 385 ufs2_daddr_t 386 suj_checkblkavail(ufs2_daddr_t blkno, long frags) 387 { 388 struct bufarea *cgbp; 389 struct cg *cgp; 390 ufs2_daddr_t j, k, baseblk; 391 long cg; 392 393 if ((u_int64_t)blkno > sblock.fs_size) 394 return (0); 395 cg = dtog(&sblock, blkno); 396 cgbp = cglookup(cg); 397 cgp = cgbp->b_un.b_cg; 398 if (!check_cgmagic(cg, cgbp)) 399 return (-((cg + 1) * sblock.fs_fpg - sblock.fs_frag)); 400 baseblk = dtogd(&sblock, blkno); 401 for (j = 0; j <= sblock.fs_frag - frags; j++) { 402 if (!isset(cg_blksfree(cgp), baseblk + j)) 403 continue; 404 for (k = 1; k < frags; k++) 405 if (!isset(cg_blksfree(cgp), baseblk + j + k)) 406 break; 407 if (k < frags) { 408 j += k; 409 continue; 410 } 411 for (k = 0; k < frags; k++) 412 clrbit(cg_blksfree(cgp), baseblk + j + k); 413 n_blks += frags; 414 if (frags == sblock.fs_frag) 415 cgp->cg_cs.cs_nbfree--; 416 else 417 cgp->cg_cs.cs_nffree -= frags; 418 cgdirty(cgbp); 419 return ((cg * sblock.fs_fpg) + baseblk + j); 420 } 421 return (0); 422 } 423 424 /* 425 * Clear an inode from the cg bitmap. If the inode was already clear return 426 * 0 so the caller knows it does not have to check the inode contents. 427 */ 428 static int 429 ino_free(ino_t ino, int mode) 430 { 431 struct suj_cg *sc; 432 uint8_t *inosused; 433 struct cg *cgp; 434 int cg; 435 436 cg = ino_to_cg(fs, ino); 437 ino = ino % fs->fs_ipg; 438 sc = cg_lookup(cg); 439 cgp = sc->sc_cgp; 440 inosused = cg_inosused(cgp); 441 /* 442 * The bitmap may never have made it to the disk so we have to 443 * conditionally clear. We can avoid writing the cg in this case. 444 */ 445 if (isclr(inosused, ino)) 446 return (0); 447 freeinos++; 448 clrbit(inosused, ino); 449 if (ino < cgp->cg_irotor) 450 cgp->cg_irotor = ino; 451 cgp->cg_cs.cs_nifree++; 452 if ((mode & IFMT) == IFDIR) { 453 freedir++; 454 cgp->cg_cs.cs_ndir--; 455 } 456 cgdirty(sc->sc_cgbp); 457 458 return (1); 459 } 460 461 /* 462 * Free 'frags' frags starting at filesystem block 'bno' skipping any frags 463 * set in the mask. 464 */ 465 static void 466 blk_free(ino_t ino, ufs2_daddr_t bno, int mask, int frags) 467 { 468 ufs1_daddr_t fragno, cgbno; 469 struct suj_cg *sc; 470 struct cg *cgp; 471 int i, cg; 472 uint8_t *blksfree; 473 474 if (debug) 475 printf("Freeing %d frags at blk %jd mask 0x%x\n", 476 frags, bno, mask); 477 /* 478 * Check to see if the block needs to be claimed by a snapshot. 479 * If wanted, the snapshot references it. Otherwise we free it. 480 */ 481 if (snapblkfree(fs, bno, lfragtosize(fs, frags), ino, 482 suj_checkblkavail)) 483 return; 484 cg = dtog(fs, bno); 485 sc = cg_lookup(cg); 486 cgp = sc->sc_cgp; 487 cgbno = dtogd(fs, bno); 488 blksfree = cg_blksfree(cgp); 489 490 /* 491 * If it's not allocated we only wrote the journal entry 492 * and never the bitmaps. Here we unconditionally clear and 493 * resolve the cg summary later. 494 */ 495 if (frags == fs->fs_frag && mask == 0) { 496 fragno = fragstoblks(fs, cgbno); 497 ffs_setblock(fs, blksfree, fragno); 498 freeblocks++; 499 } else { 500 /* 501 * deallocate the fragment 502 */ 503 for (i = 0; i < frags; i++) 504 if ((mask & (1 << i)) == 0 && 505 isclr(blksfree, cgbno +i)) { 506 freefrags++; 507 setbit(blksfree, cgbno + i); 508 } 509 } 510 cgdirty(sc->sc_cgbp); 511 } 512 513 /* 514 * Returns 1 if the whole block starting at 'bno' is marked free and 0 515 * otherwise. 516 */ 517 static int 518 blk_isfree(ufs2_daddr_t bno) 519 { 520 struct suj_cg *sc; 521 522 sc = cg_lookup(dtog(fs, bno)); 523 return ffs_isblock(fs, cg_blksfree(sc->sc_cgp), dtogd(fs, bno)); 524 } 525 526 /* 527 * Determine whether a block exists at a particular lbn in an inode. 528 * Returns 1 if found, 0 if not. lbn may be negative for indirects 529 * or ext blocks. 530 */ 531 static int 532 blk_isat(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int *frags) 533 { 534 struct inode ip; 535 union dinode *dp; 536 ufs2_daddr_t nblk; 537 538 ginode(ino, &ip); 539 dp = ip.i_dp; 540 if (DIP(dp, di_nlink) == 0 || DIP(dp, di_mode) == 0) { 541 irelse(&ip); 542 return (0); 543 } 544 nblk = ino_blkatoff(dp, ino, lbn, frags, NULL); 545 irelse(&ip); 546 return (nblk == blk); 547 } 548 549 /* 550 * Clear the directory entry at diroff that should point to child. Minimal 551 * checking is done and it is assumed that this path was verified with isat. 552 */ 553 static void 554 ino_clrat(ino_t parent, off_t diroff, ino_t child) 555 { 556 union dinode *dip; 557 struct direct *dp; 558 struct inode ip; 559 ufs2_daddr_t blk; 560 struct bufarea *bp; 561 ufs_lbn_t lbn; 562 int blksize; 563 int frags; 564 int doff; 565 566 if (debug) 567 printf("Clearing inode %ju from parent %ju at offset %jd\n", 568 (uintmax_t)child, (uintmax_t)parent, diroff); 569 570 lbn = lblkno(fs, diroff); 571 doff = blkoff(fs, diroff); 572 ginode(parent, &ip); 573 dip = ip.i_dp; 574 blk = ino_blkatoff(dip, parent, lbn, &frags, NULL); 575 blksize = sblksize(fs, DIP(dip, di_size), lbn); 576 irelse(&ip); 577 bp = getdatablk(blk, blksize, BT_DIRDATA); 578 if (bp->b_errs != 0) 579 err_suj("ino_clrat: UNRECOVERABLE I/O ERROR"); 580 dp = (struct direct *)&bp->b_un.b_buf[doff]; 581 if (dp->d_ino != child) 582 errx(1, "Inode %ju does not exist in %ju at %jd", 583 (uintmax_t)child, (uintmax_t)parent, diroff); 584 dp->d_ino = 0; 585 dirty(bp); 586 brelse(bp); 587 /* 588 * The actual .. reference count will already have been removed 589 * from the parent by the .. remref record. 590 */ 591 } 592 593 /* 594 * Determines whether a pointer to an inode exists within a directory 595 * at a specified offset. Returns the mode of the found entry. 596 */ 597 static int 598 ino_isat(ino_t parent, off_t diroff, ino_t child, int *mode, int *isdot) 599 { 600 struct inode ip; 601 union dinode *dip; 602 struct bufarea *bp; 603 struct direct *dp; 604 ufs2_daddr_t blk; 605 ufs_lbn_t lbn; 606 int blksize; 607 int frags; 608 int dpoff; 609 int doff; 610 611 *isdot = 0; 612 ginode(parent, &ip); 613 dip = ip.i_dp; 614 *mode = DIP(dip, di_mode); 615 if ((*mode & IFMT) != IFDIR) { 616 if (debug) { 617 /* 618 * This can happen if the parent inode 619 * was reallocated. 620 */ 621 if (*mode != 0) 622 printf("Directory %ju has bad mode %o\n", 623 (uintmax_t)parent, *mode); 624 else 625 printf("Directory %ju has zero mode\n", 626 (uintmax_t)parent); 627 } 628 irelse(&ip); 629 return (0); 630 } 631 lbn = lblkno(fs, diroff); 632 doff = blkoff(fs, diroff); 633 blksize = sblksize(fs, DIP(dip, di_size), lbn); 634 if (diroff + DIRECTSIZ(1) > DIP(dip, di_size) || doff >= blksize) { 635 if (debug) 636 printf("ino %ju absent from %ju due to offset %jd" 637 " exceeding size %jd\n", 638 (uintmax_t)child, (uintmax_t)parent, diroff, 639 DIP(dip, di_size)); 640 irelse(&ip); 641 return (0); 642 } 643 blk = ino_blkatoff(dip, parent, lbn, &frags, NULL); 644 irelse(&ip); 645 if (blk <= 0) { 646 if (debug) 647 printf("Sparse directory %ju", (uintmax_t)parent); 648 return (0); 649 } 650 bp = getdatablk(blk, blksize, BT_DIRDATA); 651 if (bp->b_errs != 0) 652 err_suj("ino_isat: UNRECOVERABLE I/O ERROR"); 653 /* 654 * Walk through the records from the start of the block to be 655 * certain we hit a valid record and not some junk in the middle 656 * of a file name. Stop when we reach or pass the expected offset. 657 */ 658 dpoff = rounddown(doff, DIRBLKSIZ); 659 do { 660 dp = (struct direct *)&bp->b_un.b_buf[dpoff]; 661 if (dpoff == doff) 662 break; 663 if (dp->d_reclen == 0) 664 break; 665 dpoff += dp->d_reclen; 666 } while (dpoff <= doff); 667 if (dpoff > fs->fs_bsize) 668 err_suj("Corrupt directory block in dir ino %ju\n", 669 (uintmax_t)parent); 670 /* Not found. */ 671 if (dpoff != doff) { 672 if (debug) 673 printf("ino %ju not found in %ju, lbn %jd, dpoff %d\n", 674 (uintmax_t)child, (uintmax_t)parent, lbn, dpoff); 675 brelse(bp); 676 return (0); 677 } 678 /* 679 * We found the item in question. Record the mode and whether it's 680 * a . or .. link for the caller. 681 */ 682 if (dp->d_ino == child) { 683 if (child == parent) 684 *isdot = 1; 685 else if (dp->d_namlen == 2 && 686 dp->d_name[0] == '.' && dp->d_name[1] == '.') 687 *isdot = 1; 688 *mode = DTTOIF(dp->d_type); 689 brelse(bp); 690 return (1); 691 } 692 if (debug) 693 printf("ino %ju doesn't match dirent ino %ju in parent %ju\n", 694 (uintmax_t)child, (uintmax_t)dp->d_ino, (uintmax_t)parent); 695 brelse(bp); 696 return (0); 697 } 698 699 #define VISIT_INDIR 0x0001 700 #define VISIT_EXT 0x0002 701 #define VISIT_ROOT 0x0004 /* Operation came via root & valid pointers. */ 702 703 /* 704 * Read an indirect level which may or may not be linked into an inode. 705 */ 706 static void 707 indir_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, uint64_t *frags, 708 ino_visitor visitor, int flags) 709 { 710 struct bufarea *bp; 711 ufs_lbn_t lbnadd; 712 ufs2_daddr_t nblk; 713 ufs_lbn_t nlbn; 714 int level; 715 int i; 716 717 /* 718 * Don't visit indirect blocks with contents we can't trust. This 719 * should only happen when indir_visit() is called to complete a 720 * truncate that never finished and not when a pointer is found via 721 * an inode. 722 */ 723 if (blk == 0) 724 return; 725 level = lbn_level(lbn); 726 if (level == -1) 727 err_suj("Invalid level for lbn %jd\n", lbn); 728 if ((flags & VISIT_ROOT) == 0 && blk_isindir(blk, ino, lbn) == 0) { 729 if (debug) 730 printf("blk %jd ino %ju lbn %jd(%d) is not indir.\n", 731 blk, (uintmax_t)ino, lbn, level); 732 goto out; 733 } 734 lbnadd = 1; 735 for (i = level; i > 0; i--) 736 lbnadd *= NINDIR(fs); 737 bp = getdatablk(blk, fs->fs_bsize, BT_LEVEL1 + level); 738 if (bp->b_errs != 0) 739 err_suj("indir_visit: UNRECOVERABLE I/O ERROR\n"); 740 for (i = 0; i < NINDIR(fs); i++) { 741 if ((nblk = IBLK(bp, i)) == 0) 742 continue; 743 if (level == 0) { 744 nlbn = -lbn + i * lbnadd; 745 (*frags) += fs->fs_frag; 746 visitor(ino, nlbn, nblk, fs->fs_frag); 747 } else { 748 nlbn = (lbn + 1) - (i * lbnadd); 749 indir_visit(ino, nlbn, nblk, frags, visitor, flags); 750 } 751 } 752 brelse(bp); 753 out: 754 if (flags & VISIT_INDIR) { 755 (*frags) += fs->fs_frag; 756 visitor(ino, lbn, blk, fs->fs_frag); 757 } 758 } 759 760 /* 761 * Visit each block in an inode as specified by 'flags' and call a 762 * callback function. The callback may inspect or free blocks. The 763 * count of frags found according to the size in the file is returned. 764 * This is not valid for sparse files but may be used to determine 765 * the correct di_blocks for a file. 766 */ 767 static uint64_t 768 ino_visit(union dinode *dp, ino_t ino, ino_visitor visitor, int flags) 769 { 770 ufs_lbn_t nextlbn; 771 ufs_lbn_t tmpval; 772 ufs_lbn_t lbn; 773 uint64_t size; 774 uint64_t fragcnt; 775 int mode; 776 int frags; 777 int i; 778 779 size = DIP(dp, di_size); 780 mode = DIP(dp, di_mode) & IFMT; 781 fragcnt = 0; 782 if ((flags & VISIT_EXT) && 783 fs->fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize) { 784 for (i = 0; i < UFS_NXADDR; i++) { 785 if (dp->dp2.di_extb[i] == 0) 786 continue; 787 frags = sblksize(fs, dp->dp2.di_extsize, i); 788 frags = numfrags(fs, frags); 789 fragcnt += frags; 790 visitor(ino, -1 - i, dp->dp2.di_extb[i], frags); 791 } 792 } 793 /* Skip datablocks for short links and devices. */ 794 if (mode == IFBLK || mode == IFCHR || 795 (mode == IFLNK && size < fs->fs_maxsymlinklen)) 796 return (fragcnt); 797 for (i = 0; i < UFS_NDADDR; i++) { 798 if (DIP(dp, di_db[i]) == 0) 799 continue; 800 frags = sblksize(fs, size, i); 801 frags = numfrags(fs, frags); 802 fragcnt += frags; 803 visitor(ino, i, DIP(dp, di_db[i]), frags); 804 } 805 /* 806 * We know the following indirects are real as we're following 807 * real pointers to them. 808 */ 809 flags |= VISIT_ROOT; 810 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; i < UFS_NIADDR; i++, 811 lbn = nextlbn) { 812 nextlbn = lbn + tmpval; 813 tmpval *= NINDIR(fs); 814 if (DIP(dp, di_ib[i]) == 0) 815 continue; 816 indir_visit(ino, -lbn - i, DIP(dp, di_ib[i]), &fragcnt, visitor, 817 flags); 818 } 819 return (fragcnt); 820 } 821 822 /* 823 * Null visitor function used when we just want to count blocks and 824 * record the lbn. 825 */ 826 ufs_lbn_t visitlbn; 827 static void 828 null_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 829 { 830 if (lbn > 0) 831 visitlbn = lbn; 832 } 833 834 /* 835 * Recalculate di_blocks when we discover that a block allocation or 836 * free was not successfully completed. The kernel does not roll this back 837 * because it would be too expensive to compute which indirects were 838 * reachable at the time the inode was written. 839 */ 840 static void 841 ino_adjblks(struct suj_ino *sino) 842 { 843 struct inode ip; 844 union dinode *dp; 845 uint64_t blocks; 846 uint64_t frags; 847 off_t isize; 848 off_t size; 849 ino_t ino; 850 851 ino = sino->si_ino; 852 ginode(ino, &ip); 853 dp = ip.i_dp; 854 /* No need to adjust zero'd inodes. */ 855 if (DIP(dp, di_mode) == 0) { 856 irelse(&ip); 857 return; 858 } 859 /* 860 * Visit all blocks and count them as well as recording the last 861 * valid lbn in the file. If the file size doesn't agree with the 862 * last lbn we need to truncate to fix it. Otherwise just adjust 863 * the blocks count. 864 */ 865 visitlbn = 0; 866 frags = ino_visit(dp, ino, null_visit, VISIT_INDIR | VISIT_EXT); 867 blocks = fsbtodb(fs, frags); 868 /* 869 * We assume the size and direct block list is kept coherent by 870 * softdep. For files that have extended into indirects we truncate 871 * to the size in the inode or the maximum size permitted by 872 * populated indirects. 873 */ 874 if (visitlbn >= UFS_NDADDR) { 875 isize = DIP(dp, di_size); 876 size = lblktosize(fs, visitlbn + 1); 877 if (isize > size) 878 isize = size; 879 /* Always truncate to free any unpopulated indirects. */ 880 ino_trunc(ino, isize); 881 irelse(&ip); 882 return; 883 } 884 if (blocks == DIP(dp, di_blocks)) { 885 irelse(&ip); 886 return; 887 } 888 if (debug) 889 printf("ino %ju adjusting block count from %jd to %jd\n", 890 (uintmax_t)ino, DIP(dp, di_blocks), blocks); 891 DIP_SET(dp, di_blocks, blocks); 892 inodirty(&ip); 893 irelse(&ip); 894 } 895 896 static void 897 blk_free_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 898 { 899 900 blk_free(ino, blk, blk_freemask(blk, ino, lbn, frags), frags); 901 } 902 903 /* 904 * Free a block or tree of blocks that was previously rooted in ino at 905 * the given lbn. If the lbn is an indirect all children are freed 906 * recursively. 907 */ 908 static void 909 blk_free_lbn(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags, int follow) 910 { 911 uint64_t resid; 912 int mask; 913 914 mask = blk_freemask(blk, ino, lbn, frags); 915 resid = 0; 916 if (lbn <= -UFS_NDADDR && follow && mask == 0) 917 indir_visit(ino, lbn, blk, &resid, blk_free_visit, VISIT_INDIR); 918 else 919 blk_free(ino, blk, mask, frags); 920 } 921 922 static void 923 ino_setskip(struct suj_ino *sino, ino_t parent) 924 { 925 int isdot; 926 int mode; 927 928 if (ino_isat(sino->si_ino, DOTDOT_OFFSET, parent, &mode, &isdot)) 929 sino->si_skipparent = 1; 930 } 931 932 static void 933 ino_remref(ino_t parent, ino_t child, uint64_t diroff, int isdotdot) 934 { 935 struct suj_ino *sino; 936 struct suj_rec *srec; 937 struct jrefrec *rrec; 938 939 /* 940 * Lookup this inode to see if we have a record for it. 941 */ 942 sino = ino_lookup(child, 0); 943 /* 944 * Tell any child directories we've already removed their 945 * parent link cnt. Don't try to adjust our link down again. 946 */ 947 if (sino != NULL && isdotdot == 0) 948 ino_setskip(sino, parent); 949 /* 950 * No valid record for this inode. Just drop the on-disk 951 * link by one. 952 */ 953 if (sino == NULL || sino->si_hasrecs == 0) { 954 ino_decr(child); 955 return; 956 } 957 /* 958 * Use ino_adjust() if ino_check() has already processed this 959 * child. If we lose the last non-dot reference to a 960 * directory it will be discarded. 961 */ 962 if (sino->si_linkadj) { 963 if (sino->si_nlink == 0) 964 err_suj("ino_remref: ino %ld mode 0%o about to go " 965 "negative\n", sino->si_ino, sino->si_mode); 966 sino->si_nlink--; 967 if (isdotdot) 968 sino->si_dotlinks--; 969 ino_adjust(sino); 970 return; 971 } 972 /* 973 * If we haven't yet processed this inode we need to make 974 * sure we will successfully discover the lost path. If not 975 * use nlinkadj to remember. 976 */ 977 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 978 rrec = (struct jrefrec *)srec->sr_rec; 979 if (rrec->jr_parent == parent && 980 rrec->jr_diroff == diroff) 981 return; 982 } 983 sino->si_nlinkadj++; 984 } 985 986 /* 987 * Free the children of a directory when the directory is discarded. 988 */ 989 static void 990 ino_free_children(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 991 { 992 struct suj_ino *sino; 993 struct bufarea *bp; 994 struct direct *dp; 995 off_t diroff; 996 int skipparent; 997 int isdotdot; 998 int dpoff; 999 int size; 1000 1001 sino = ino_lookup(ino, 0); 1002 if (sino) 1003 skipparent = sino->si_skipparent; 1004 else 1005 skipparent = 0; 1006 size = lfragtosize(fs, frags); 1007 bp = getdatablk(blk, size, BT_DIRDATA); 1008 if (bp->b_errs != 0) 1009 err_suj("ino_free_children: UNRECOVERABLE I/O ERROR"); 1010 dp = (struct direct *)&bp->b_un.b_buf[0]; 1011 for (dpoff = 0; dpoff < size && dp->d_reclen; dpoff += dp->d_reclen) { 1012 dp = (struct direct *)&bp->b_un.b_buf[dpoff]; 1013 if (dp->d_ino == 0 || dp->d_ino == UFS_WINO) 1014 continue; 1015 if (dp->d_namlen == 1 && dp->d_name[0] == '.') 1016 continue; 1017 isdotdot = dp->d_namlen == 2 && dp->d_name[0] == '.' && 1018 dp->d_name[1] == '.'; 1019 if (isdotdot && skipparent == 1) 1020 continue; 1021 if (debug) 1022 printf("Directory %ju removing ino %ju name %s\n", 1023 (uintmax_t)ino, (uintmax_t)dp->d_ino, dp->d_name); 1024 diroff = lblktosize(fs, lbn) + dpoff; 1025 ino_remref(ino, dp->d_ino, diroff, isdotdot); 1026 } 1027 brelse(bp); 1028 } 1029 1030 /* 1031 * Reclaim an inode, freeing all blocks and decrementing all children's 1032 * link counts. Free the inode back to the cg. 1033 */ 1034 static void 1035 ino_reclaim(struct inode *ip, ino_t ino, int mode) 1036 { 1037 union dinode *dp; 1038 uint32_t gen; 1039 1040 dp = ip->i_dp; 1041 if (ino == UFS_ROOTINO) 1042 err_suj("Attempting to free UFS_ROOTINO\n"); 1043 if (debug) 1044 printf("Truncating and freeing ino %ju, nlink %d, mode %o\n", 1045 (uintmax_t)ino, DIP(dp, di_nlink), DIP(dp, di_mode)); 1046 1047 /* We are freeing an inode or directory. */ 1048 if ((DIP(dp, di_mode) & IFMT) == IFDIR) 1049 ino_visit(dp, ino, ino_free_children, 0); 1050 DIP_SET(dp, di_nlink, 0); 1051 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) 1052 snapremove(ino); 1053 ino_visit(dp, ino, blk_free_visit, VISIT_EXT | VISIT_INDIR); 1054 /* Here we have to clear the inode and release any blocks it holds. */ 1055 gen = DIP(dp, di_gen); 1056 if (fs->fs_magic == FS_UFS1_MAGIC) 1057 bzero(dp, sizeof(struct ufs1_dinode)); 1058 else 1059 bzero(dp, sizeof(struct ufs2_dinode)); 1060 DIP_SET(dp, di_gen, gen); 1061 inodirty(ip); 1062 ino_free(ino, mode); 1063 return; 1064 } 1065 1066 /* 1067 * Adjust an inode's link count down by one when a directory goes away. 1068 */ 1069 static void 1070 ino_decr(ino_t ino) 1071 { 1072 struct inode ip; 1073 union dinode *dp; 1074 int reqlink; 1075 int nlink; 1076 int mode; 1077 1078 ginode(ino, &ip); 1079 dp = ip.i_dp; 1080 nlink = DIP(dp, di_nlink); 1081 mode = DIP(dp, di_mode); 1082 if (nlink < 1) 1083 err_suj("Inode %d link count %d invalid\n", ino, nlink); 1084 if (mode == 0) 1085 err_suj("Inode %d has a link of %d with 0 mode\n", ino, nlink); 1086 nlink--; 1087 if ((mode & IFMT) == IFDIR) 1088 reqlink = 2; 1089 else 1090 reqlink = 1; 1091 if (nlink < reqlink) { 1092 if (debug) 1093 printf("ino %ju not enough links to live %d < %d\n", 1094 (uintmax_t)ino, nlink, reqlink); 1095 ino_reclaim(&ip, ino, mode); 1096 irelse(&ip); 1097 return; 1098 } 1099 DIP_SET(dp, di_nlink, nlink); 1100 inodirty(&ip); 1101 irelse(&ip); 1102 } 1103 1104 /* 1105 * Adjust the inode link count to 'nlink'. If the count reaches zero 1106 * free it. 1107 */ 1108 static void 1109 ino_adjust(struct suj_ino *sino) 1110 { 1111 struct jrefrec *rrec; 1112 struct suj_rec *srec; 1113 struct suj_ino *stmp; 1114 union dinode *dp; 1115 struct inode ip; 1116 nlink_t nlink; 1117 nlink_t reqlink; 1118 int recmode; 1119 int isdot; 1120 int mode; 1121 ino_t ino; 1122 1123 nlink = sino->si_nlink; 1124 ino = sino->si_ino; 1125 mode = sino->si_mode & IFMT; 1126 /* 1127 * If it's a directory with no dot links, it was truncated before 1128 * the name was cleared. We need to clear the dirent that 1129 * points at it. 1130 */ 1131 if (mode == IFDIR && nlink == 1 && sino->si_dotlinks == 0) { 1132 sino->si_nlink = nlink = 0; 1133 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 1134 rrec = (struct jrefrec *)srec->sr_rec; 1135 if (ino_isat(rrec->jr_parent, rrec->jr_diroff, ino, 1136 &recmode, &isdot) == 0) 1137 continue; 1138 ino_clrat(rrec->jr_parent, rrec->jr_diroff, ino); 1139 break; 1140 } 1141 if (srec == NULL) 1142 errx(1, "Directory %ju name not found", (uintmax_t)ino); 1143 } 1144 /* 1145 * If it's a directory with no real names pointing to it go ahead 1146 * and truncate it. This will free any children. 1147 */ 1148 if (mode == IFDIR && nlink - sino->si_dotlinks == 0) { 1149 sino->si_nlink = nlink = 0; 1150 /* 1151 * Mark any .. links so they know not to free this inode 1152 * when they are removed. 1153 */ 1154 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 1155 rrec = (struct jrefrec *)srec->sr_rec; 1156 if (rrec->jr_diroff == DOTDOT_OFFSET) { 1157 stmp = ino_lookup(rrec->jr_parent, 0); 1158 if (stmp) 1159 ino_setskip(stmp, ino); 1160 } 1161 } 1162 } 1163 ginode(ino, &ip); 1164 dp = ip.i_dp; 1165 mode = DIP(dp, di_mode) & IFMT; 1166 if (nlink > UFS_LINK_MAX) 1167 err_suj("ino %ju nlink manipulation error, new %ju, old %d\n", 1168 (uintmax_t)ino, (uintmax_t)nlink, DIP(dp, di_nlink)); 1169 if (debug) 1170 printf("Adjusting ino %ju, nlink %ju, old link %d lastmode %o\n", 1171 (uintmax_t)ino, (uintmax_t)nlink, DIP(dp, di_nlink), 1172 sino->si_mode); 1173 if (mode == 0) { 1174 if (debug) 1175 printf("ino %ju, zero inode freeing bitmap\n", 1176 (uintmax_t)ino); 1177 ino_free(ino, sino->si_mode); 1178 irelse(&ip); 1179 return; 1180 } 1181 /* XXX Should be an assert? */ 1182 if (mode != sino->si_mode && debug) 1183 printf("ino %ju, mode %o != %o\n", 1184 (uintmax_t)ino, mode, sino->si_mode); 1185 if ((mode & IFMT) == IFDIR) 1186 reqlink = 2; 1187 else 1188 reqlink = 1; 1189 /* If the inode doesn't have enough links to live, free it. */ 1190 if (nlink < reqlink) { 1191 if (debug) 1192 printf("ino %ju not enough links to live %ju < %ju\n", 1193 (uintmax_t)ino, (uintmax_t)nlink, 1194 (uintmax_t)reqlink); 1195 ino_reclaim(&ip, ino, mode); 1196 irelse(&ip); 1197 return; 1198 } 1199 /* If required write the updated link count. */ 1200 if (DIP(dp, di_nlink) == nlink) { 1201 if (debug) 1202 printf("ino %ju, link matches, skipping.\n", 1203 (uintmax_t)ino); 1204 irelse(&ip); 1205 return; 1206 } 1207 DIP_SET(dp, di_nlink, nlink); 1208 inodirty(&ip); 1209 irelse(&ip); 1210 } 1211 1212 /* 1213 * Truncate some or all blocks in an indirect, freeing any that are required 1214 * and zeroing the indirect. 1215 */ 1216 static void 1217 indir_trunc(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, ufs_lbn_t lastlbn, 1218 union dinode *dp) 1219 { 1220 struct bufarea *bp; 1221 ufs_lbn_t lbnadd; 1222 ufs2_daddr_t nblk; 1223 ufs_lbn_t next; 1224 ufs_lbn_t nlbn; 1225 int isdirty; 1226 int level; 1227 int i; 1228 1229 if (blk == 0) 1230 return; 1231 isdirty = 0; 1232 level = lbn_level(lbn); 1233 if (level == -1) 1234 err_suj("Invalid level for lbn %jd\n", lbn); 1235 lbnadd = 1; 1236 for (i = level; i > 0; i--) 1237 lbnadd *= NINDIR(fs); 1238 bp = getdatablk(blk, fs->fs_bsize, BT_LEVEL1 + level); 1239 if (bp->b_errs != 0) 1240 err_suj("indir_trunc: UNRECOVERABLE I/O ERROR"); 1241 for (i = 0; i < NINDIR(fs); i++) { 1242 if ((nblk = IBLK(bp, i)) == 0) 1243 continue; 1244 if (level != 0) { 1245 nlbn = (lbn + 1) - (i * lbnadd); 1246 /* 1247 * Calculate the lbn of the next indirect to 1248 * determine if any of this indirect must be 1249 * reclaimed. 1250 */ 1251 next = -(lbn + level) + ((i+1) * lbnadd); 1252 if (next <= lastlbn) 1253 continue; 1254 indir_trunc(ino, nlbn, nblk, lastlbn, dp); 1255 /* If all of this indirect was reclaimed, free it. */ 1256 nlbn = next - lbnadd; 1257 if (nlbn < lastlbn) 1258 continue; 1259 } else { 1260 nlbn = -lbn + i * lbnadd; 1261 if (nlbn < lastlbn) 1262 continue; 1263 } 1264 isdirty = 1; 1265 blk_free(ino, nblk, 0, fs->fs_frag); 1266 IBLK_SET(bp, i, 0); 1267 } 1268 if (isdirty) 1269 dirty(bp); 1270 brelse(bp); 1271 } 1272 1273 /* 1274 * Truncate an inode to the minimum of the given size or the last populated 1275 * block after any over size have been discarded. The kernel would allocate 1276 * the last block in the file but fsck does not and neither do we. This 1277 * code never extends files, only shrinks them. 1278 */ 1279 static void 1280 ino_trunc(ino_t ino, off_t size) 1281 { 1282 struct inode ip; 1283 union dinode *dp; 1284 struct bufarea *bp; 1285 ufs2_daddr_t bn; 1286 uint64_t totalfrags; 1287 ufs_lbn_t nextlbn; 1288 ufs_lbn_t lastlbn; 1289 ufs_lbn_t tmpval; 1290 ufs_lbn_t lbn; 1291 ufs_lbn_t i; 1292 int blksize, frags; 1293 off_t cursize; 1294 off_t off; 1295 int mode; 1296 1297 ginode(ino, &ip); 1298 dp = ip.i_dp; 1299 mode = DIP(dp, di_mode) & IFMT; 1300 cursize = DIP(dp, di_size); 1301 /* If no size change, nothing to do */ 1302 if (size == cursize) { 1303 irelse(&ip); 1304 return; 1305 } 1306 if (debug) 1307 printf("Truncating ino %ju, mode %o to size %jd from " 1308 "size %jd\n", (uintmax_t)ino, mode, size, cursize); 1309 1310 /* Skip datablocks for short links and devices. */ 1311 if (mode == 0 || mode == IFBLK || mode == IFCHR || 1312 (mode == IFLNK && cursize < fs->fs_maxsymlinklen)) { 1313 irelse(&ip); 1314 return; 1315 } 1316 /* Don't extend. */ 1317 if (size > cursize) { 1318 irelse(&ip); 1319 return; 1320 } 1321 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) { 1322 if (size > 0) 1323 err_suj("Partial truncation of ino %ju snapshot file\n", 1324 (uintmax_t)ino); 1325 snapremove(ino); 1326 } 1327 lastlbn = lblkno(fs, blkroundup(fs, size)); 1328 for (i = lastlbn; i < UFS_NDADDR; i++) { 1329 if ((bn = DIP(dp, di_db[i])) == 0) 1330 continue; 1331 blksize = sblksize(fs, cursize, i); 1332 blk_free(ino, bn, 0, numfrags(fs, blksize)); 1333 DIP_SET(dp, di_db[i], 0); 1334 } 1335 /* 1336 * Follow indirect blocks, freeing anything required. 1337 */ 1338 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; i < UFS_NIADDR; i++, 1339 lbn = nextlbn) { 1340 nextlbn = lbn + tmpval; 1341 tmpval *= NINDIR(fs); 1342 /* If we're not freeing any in this indirect range skip it. */ 1343 if (lastlbn >= nextlbn) 1344 continue; 1345 if ((bn = DIP(dp, di_ib[i])) == 0) 1346 continue; 1347 indir_trunc(ino, -lbn - i, bn, lastlbn, dp); 1348 /* If we freed everything in this indirect free the indir. */ 1349 if (lastlbn > lbn) 1350 continue; 1351 blk_free(ino, bn, 0, fs->fs_frag); 1352 DIP_SET(dp, di_ib[i], 0); 1353 } 1354 /* 1355 * Now that we've freed any whole blocks that exceed the desired 1356 * truncation size, figure out how many blocks remain and what the 1357 * last populated lbn is. We will set the size to this last lbn 1358 * rather than worrying about allocating the final lbn as the kernel 1359 * would've done. This is consistent with normal fsck behavior. 1360 */ 1361 visitlbn = 0; 1362 totalfrags = ino_visit(dp, ino, null_visit, VISIT_INDIR | VISIT_EXT); 1363 if (size > lblktosize(fs, visitlbn + 1)) 1364 size = lblktosize(fs, visitlbn + 1); 1365 /* 1366 * If we're truncating direct blocks we have to adjust frags 1367 * accordingly. 1368 */ 1369 if (visitlbn < UFS_NDADDR && totalfrags) { 1370 long oldspace, newspace; 1371 1372 bn = DIP(dp, di_db[visitlbn]); 1373 if (bn == 0) 1374 err_suj("Bad blk at ino %ju lbn %jd\n", 1375 (uintmax_t)ino, visitlbn); 1376 oldspace = sblksize(fs, cursize, visitlbn); 1377 newspace = sblksize(fs, size, visitlbn); 1378 if (oldspace != newspace) { 1379 bn += numfrags(fs, newspace); 1380 frags = numfrags(fs, oldspace - newspace); 1381 blk_free(ino, bn, 0, frags); 1382 totalfrags -= frags; 1383 } 1384 } 1385 DIP_SET(dp, di_blocks, fsbtodb(fs, totalfrags)); 1386 DIP_SET(dp, di_size, size); 1387 inodirty(&ip); 1388 /* 1389 * If we've truncated into the middle of a block or frag we have 1390 * to zero it here. Otherwise the file could extend into 1391 * uninitialized space later. 1392 */ 1393 off = blkoff(fs, size); 1394 if (off && DIP(dp, di_mode) != IFDIR) { 1395 long clrsize; 1396 1397 bn = ino_blkatoff(dp, ino, visitlbn, &frags, NULL); 1398 if (bn == 0) 1399 err_suj("Block missing from ino %ju at lbn %jd\n", 1400 (uintmax_t)ino, visitlbn); 1401 clrsize = frags * fs->fs_fsize; 1402 bp = getdatablk(bn, clrsize, BT_DATA); 1403 if (bp->b_errs != 0) 1404 err_suj("ino_trunc: UNRECOVERABLE I/O ERROR"); 1405 clrsize -= off; 1406 bzero(&bp->b_un.b_buf[off], clrsize); 1407 dirty(bp); 1408 brelse(bp); 1409 } 1410 irelse(&ip); 1411 return; 1412 } 1413 1414 /* 1415 * Process records available for one inode and determine whether the 1416 * link count is correct or needs adjusting. 1417 */ 1418 static void 1419 ino_check(struct suj_ino *sino) 1420 { 1421 struct suj_rec *srec; 1422 struct jrefrec *rrec; 1423 nlink_t dotlinks; 1424 nlink_t newlinks; 1425 nlink_t removes; 1426 nlink_t nlink; 1427 ino_t ino; 1428 int isdot; 1429 int isat; 1430 int mode; 1431 1432 if (sino->si_hasrecs == 0) 1433 return; 1434 ino = sino->si_ino; 1435 rrec = (struct jrefrec *)TAILQ_FIRST(&sino->si_recs)->sr_rec; 1436 nlink = rrec->jr_nlink; 1437 newlinks = 0; 1438 dotlinks = 0; 1439 removes = sino->si_nlinkadj; 1440 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 1441 rrec = (struct jrefrec *)srec->sr_rec; 1442 isat = ino_isat(rrec->jr_parent, rrec->jr_diroff, 1443 rrec->jr_ino, &mode, &isdot); 1444 if (isat && (mode & IFMT) != (rrec->jr_mode & IFMT)) 1445 err_suj("Inode mode/directory type mismatch %o != %o\n", 1446 mode, rrec->jr_mode); 1447 if (debug) 1448 printf("jrefrec: op %s ino %ju, nlink %ju, parent %ju, " 1449 "diroff %jd, mode %o, isat %d, isdot %d\n", 1450 JOP_OPTYPE(rrec->jr_op), (uintmax_t)rrec->jr_ino, 1451 (uintmax_t)rrec->jr_nlink, 1452 (uintmax_t)rrec->jr_parent, 1453 (uintmax_t)rrec->jr_diroff, 1454 rrec->jr_mode, isat, isdot); 1455 mode = rrec->jr_mode & IFMT; 1456 if (rrec->jr_op == JOP_REMREF) 1457 removes++; 1458 newlinks += isat; 1459 if (isdot) 1460 dotlinks += isat; 1461 } 1462 /* 1463 * The number of links that remain are the starting link count 1464 * subtracted by the total number of removes with the total 1465 * links discovered back in. An incomplete remove thus 1466 * makes no change to the link count but an add increases 1467 * by one. 1468 */ 1469 if (debug) 1470 printf( 1471 "ino %ju nlink %ju newlinks %ju removes %ju dotlinks %ju\n", 1472 (uintmax_t)ino, (uintmax_t)nlink, (uintmax_t)newlinks, 1473 (uintmax_t)removes, (uintmax_t)dotlinks); 1474 nlink += newlinks; 1475 nlink -= removes; 1476 sino->si_linkadj = 1; 1477 sino->si_nlink = nlink; 1478 sino->si_dotlinks = dotlinks; 1479 sino->si_mode = mode; 1480 ino_adjust(sino); 1481 } 1482 1483 /* 1484 * Process records available for one block and determine whether it is 1485 * still allocated and whether the owning inode needs to be updated or 1486 * a free completed. 1487 */ 1488 static void 1489 blk_check(struct suj_blk *sblk) 1490 { 1491 struct suj_rec *srec; 1492 struct jblkrec *brec; 1493 struct suj_ino *sino; 1494 ufs2_daddr_t blk; 1495 int mask; 1496 int frags; 1497 int isat; 1498 1499 /* 1500 * Each suj_blk actually contains records for any fragments in that 1501 * block. As a result we must evaluate each record individually. 1502 */ 1503 sino = NULL; 1504 TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) { 1505 brec = (struct jblkrec *)srec->sr_rec; 1506 frags = brec->jb_frags; 1507 blk = brec->jb_blkno + brec->jb_oldfrags; 1508 isat = blk_isat(brec->jb_ino, brec->jb_lbn, blk, &frags); 1509 if (sino == NULL || sino->si_ino != brec->jb_ino) { 1510 sino = ino_lookup(brec->jb_ino, 1); 1511 sino->si_blkadj = 1; 1512 } 1513 if (debug) 1514 printf("op %s blk %jd ino %ju lbn %jd frags %d isat %d " 1515 "(%d)\n", JOP_OPTYPE(brec->jb_op), blk, 1516 (uintmax_t)brec->jb_ino, brec->jb_lbn, 1517 brec->jb_frags, isat, frags); 1518 /* 1519 * If we found the block at this address we still have to 1520 * determine if we need to free the tail end that was 1521 * added by adding contiguous fragments from the same block. 1522 */ 1523 if (isat == 1) { 1524 if (frags == brec->jb_frags) 1525 continue; 1526 mask = blk_freemask(blk, brec->jb_ino, brec->jb_lbn, 1527 brec->jb_frags); 1528 mask >>= frags; 1529 blk += frags; 1530 frags = brec->jb_frags - frags; 1531 blk_free(brec->jb_ino, blk, mask, frags); 1532 continue; 1533 } 1534 /* 1535 * The block wasn't found, attempt to free it. It won't be 1536 * freed if it was actually reallocated. If this was an 1537 * allocation we don't want to follow indirects as they 1538 * may not be written yet. Any children of the indirect will 1539 * have their own records. If it's a free we need to 1540 * recursively free children. 1541 */ 1542 blk_free_lbn(blk, brec->jb_ino, brec->jb_lbn, brec->jb_frags, 1543 brec->jb_op == JOP_FREEBLK); 1544 } 1545 } 1546 1547 /* 1548 * Walk the list of inode records for this cg and resolve moved and duplicate 1549 * inode references now that we have a complete picture. 1550 */ 1551 static void 1552 cg_build(struct suj_cg *sc) 1553 { 1554 struct suj_ino *sino; 1555 int i; 1556 1557 for (i = 0; i < HASHSIZE; i++) 1558 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) 1559 ino_build(sino); 1560 } 1561 1562 /* 1563 * Handle inodes requiring truncation. This must be done prior to 1564 * looking up any inodes in directories. 1565 */ 1566 static void 1567 cg_trunc(struct suj_cg *sc) 1568 { 1569 struct suj_ino *sino; 1570 int i; 1571 1572 for (i = 0; i < HASHSIZE; i++) { 1573 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) { 1574 if (sino->si_trunc) { 1575 ino_trunc(sino->si_ino, 1576 sino->si_trunc->jt_size); 1577 sino->si_blkadj = 0; 1578 sino->si_trunc = NULL; 1579 } 1580 if (sino->si_blkadj) 1581 ino_adjblks(sino); 1582 } 1583 } 1584 } 1585 1586 static void 1587 cg_adj_blk(struct suj_cg *sc) 1588 { 1589 struct suj_ino *sino; 1590 int i; 1591 1592 for (i = 0; i < HASHSIZE; i++) { 1593 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) { 1594 if (sino->si_blkadj) 1595 ino_adjblks(sino); 1596 } 1597 } 1598 } 1599 1600 /* 1601 * Free any partially allocated blocks and then resolve inode block 1602 * counts. 1603 */ 1604 static void 1605 cg_check_blk(struct suj_cg *sc) 1606 { 1607 struct suj_blk *sblk; 1608 int i; 1609 1610 1611 for (i = 0; i < HASHSIZE; i++) 1612 LIST_FOREACH(sblk, &sc->sc_blkhash[i], sb_next) 1613 blk_check(sblk); 1614 } 1615 1616 /* 1617 * Walk the list of inode records for this cg, recovering any 1618 * changes which were not complete at the time of crash. 1619 */ 1620 static void 1621 cg_check_ino(struct suj_cg *sc) 1622 { 1623 struct suj_ino *sino; 1624 int i; 1625 1626 for (i = 0; i < HASHSIZE; i++) 1627 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) 1628 ino_check(sino); 1629 } 1630 1631 static void 1632 cg_apply(void (*apply)(struct suj_cg *)) 1633 { 1634 struct suj_cg *scg; 1635 int i; 1636 1637 for (i = 0; i < HASHSIZE; i++) 1638 LIST_FOREACH(scg, &cghash[i], sc_next) 1639 apply(scg); 1640 } 1641 1642 /* 1643 * Process the unlinked but referenced file list. Freeing all inodes. 1644 */ 1645 static void 1646 ino_unlinked(void) 1647 { 1648 struct inode ip; 1649 union dinode *dp; 1650 uint16_t mode; 1651 ino_t inon; 1652 ino_t ino; 1653 1654 ino = fs->fs_sujfree; 1655 fs->fs_sujfree = 0; 1656 while (ino != 0) { 1657 ginode(ino, &ip); 1658 dp = ip.i_dp; 1659 mode = DIP(dp, di_mode) & IFMT; 1660 inon = DIP(dp, di_freelink); 1661 DIP_SET(dp, di_freelink, 0); 1662 inodirty(&ip); 1663 /* 1664 * XXX Should this be an errx? 1665 */ 1666 if (DIP(dp, di_nlink) == 0) { 1667 if (debug) 1668 printf("Freeing unlinked ino %ju mode %o\n", 1669 (uintmax_t)ino, mode); 1670 ino_reclaim(&ip, ino, mode); 1671 } else if (debug) 1672 printf("Skipping ino %ju mode %o with link %d\n", 1673 (uintmax_t)ino, mode, DIP(dp, di_nlink)); 1674 ino = inon; 1675 irelse(&ip); 1676 } 1677 } 1678 1679 /* 1680 * Append a new record to the list of records requiring processing. 1681 */ 1682 static void 1683 ino_append(union jrec *rec) 1684 { 1685 struct jrefrec *refrec; 1686 struct jmvrec *mvrec; 1687 struct suj_ino *sino; 1688 struct suj_rec *srec; 1689 1690 mvrec = &rec->rec_jmvrec; 1691 refrec = &rec->rec_jrefrec; 1692 if (debug && mvrec->jm_op == JOP_MVREF) 1693 printf("ino move: ino %ju, parent %ju, " 1694 "diroff %jd, oldoff %jd\n", 1695 (uintmax_t)mvrec->jm_ino, (uintmax_t)mvrec->jm_parent, 1696 (uintmax_t)mvrec->jm_newoff, (uintmax_t)mvrec->jm_oldoff); 1697 else if (debug && 1698 (refrec->jr_op == JOP_ADDREF || refrec->jr_op == JOP_REMREF)) 1699 printf("ino ref: op %s, ino %ju, nlink %ju, " 1700 "parent %ju, diroff %jd\n", 1701 JOP_OPTYPE(refrec->jr_op), (uintmax_t)refrec->jr_ino, 1702 (uintmax_t)refrec->jr_nlink, 1703 (uintmax_t)refrec->jr_parent, (uintmax_t)refrec->jr_diroff); 1704 sino = ino_lookup(((struct jrefrec *)rec)->jr_ino, 1); 1705 sino->si_hasrecs = 1; 1706 srec = errmalloc(sizeof(*srec)); 1707 srec->sr_rec = rec; 1708 TAILQ_INSERT_TAIL(&sino->si_newrecs, srec, sr_next); 1709 } 1710 1711 /* 1712 * Add a reference adjustment to the sino list and eliminate dups. The 1713 * primary loop in ino_build_ref() checks for dups but new ones may be 1714 * created as a result of offset adjustments. 1715 */ 1716 static void 1717 ino_add_ref(struct suj_ino *sino, struct suj_rec *srec) 1718 { 1719 struct jrefrec *refrec; 1720 struct suj_rec *srn; 1721 struct jrefrec *rrn; 1722 1723 refrec = (struct jrefrec *)srec->sr_rec; 1724 /* 1725 * We walk backwards so that the oldest link count is preserved. If 1726 * an add record conflicts with a remove keep the remove. Redundant 1727 * removes are eliminated in ino_build_ref. Otherwise we keep the 1728 * oldest record at a given location. 1729 */ 1730 for (srn = TAILQ_LAST(&sino->si_recs, srechd); srn; 1731 srn = TAILQ_PREV(srn, srechd, sr_next)) { 1732 rrn = (struct jrefrec *)srn->sr_rec; 1733 if (rrn->jr_parent != refrec->jr_parent || 1734 rrn->jr_diroff != refrec->jr_diroff) 1735 continue; 1736 if (rrn->jr_op == JOP_REMREF || refrec->jr_op == JOP_ADDREF) { 1737 rrn->jr_mode = refrec->jr_mode; 1738 return; 1739 } 1740 /* 1741 * Adding a remove. 1742 * 1743 * Replace the record in place with the old nlink in case 1744 * we replace the head of the list. Abandon srec as a dup. 1745 */ 1746 refrec->jr_nlink = rrn->jr_nlink; 1747 srn->sr_rec = srec->sr_rec; 1748 return; 1749 } 1750 TAILQ_INSERT_TAIL(&sino->si_recs, srec, sr_next); 1751 } 1752 1753 /* 1754 * Create a duplicate of a reference at a previous location. 1755 */ 1756 static void 1757 ino_dup_ref(struct suj_ino *sino, struct jrefrec *refrec, off_t diroff) 1758 { 1759 struct jrefrec *rrn; 1760 struct suj_rec *srn; 1761 1762 rrn = errmalloc(sizeof(*refrec)); 1763 *rrn = *refrec; 1764 rrn->jr_op = JOP_ADDREF; 1765 rrn->jr_diroff = diroff; 1766 srn = errmalloc(sizeof(*srn)); 1767 srn->sr_rec = (union jrec *)rrn; 1768 ino_add_ref(sino, srn); 1769 } 1770 1771 /* 1772 * Add a reference to the list at all known locations. We follow the offset 1773 * changes for a single instance and create duplicate add refs at each so 1774 * that we can tolerate any version of the directory block. Eliminate 1775 * removes which collide with adds that are seen in the journal. They should 1776 * not adjust the link count down. 1777 */ 1778 static void 1779 ino_build_ref(struct suj_ino *sino, struct suj_rec *srec) 1780 { 1781 struct jrefrec *refrec; 1782 struct jmvrec *mvrec; 1783 struct suj_rec *srp; 1784 struct suj_rec *srn; 1785 struct jrefrec *rrn; 1786 off_t diroff; 1787 1788 refrec = (struct jrefrec *)srec->sr_rec; 1789 /* 1790 * Search for a mvrec that matches this offset. Whether it's an add 1791 * or a remove we can delete the mvref after creating a dup record in 1792 * the old location. 1793 */ 1794 if (!TAILQ_EMPTY(&sino->si_movs)) { 1795 diroff = refrec->jr_diroff; 1796 for (srn = TAILQ_LAST(&sino->si_movs, srechd); srn; srn = srp) { 1797 srp = TAILQ_PREV(srn, srechd, sr_next); 1798 mvrec = (struct jmvrec *)srn->sr_rec; 1799 if (mvrec->jm_parent != refrec->jr_parent || 1800 mvrec->jm_newoff != diroff) 1801 continue; 1802 diroff = mvrec->jm_oldoff; 1803 TAILQ_REMOVE(&sino->si_movs, srn, sr_next); 1804 free(srn); 1805 ino_dup_ref(sino, refrec, diroff); 1806 } 1807 } 1808 /* 1809 * If a remove wasn't eliminated by an earlier add just append it to 1810 * the list. 1811 */ 1812 if (refrec->jr_op == JOP_REMREF) { 1813 ino_add_ref(sino, srec); 1814 return; 1815 } 1816 /* 1817 * Walk the list of records waiting to be added to the list. We 1818 * must check for moves that apply to our current offset and remove 1819 * them from the list. Remove any duplicates to eliminate removes 1820 * with corresponding adds. 1821 */ 1822 TAILQ_FOREACH_SAFE(srn, &sino->si_newrecs, sr_next, srp) { 1823 switch (srn->sr_rec->rec_jrefrec.jr_op) { 1824 case JOP_ADDREF: 1825 /* 1826 * This should actually be an error we should 1827 * have a remove for every add journaled. 1828 */ 1829 rrn = (struct jrefrec *)srn->sr_rec; 1830 if (rrn->jr_parent != refrec->jr_parent || 1831 rrn->jr_diroff != refrec->jr_diroff) 1832 break; 1833 TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next); 1834 break; 1835 case JOP_REMREF: 1836 /* 1837 * Once we remove the current iteration of the 1838 * record at this address we're done. 1839 */ 1840 rrn = (struct jrefrec *)srn->sr_rec; 1841 if (rrn->jr_parent != refrec->jr_parent || 1842 rrn->jr_diroff != refrec->jr_diroff) 1843 break; 1844 TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next); 1845 ino_add_ref(sino, srec); 1846 return; 1847 case JOP_MVREF: 1848 /* 1849 * Update our diroff based on any moves that match 1850 * and remove the move. 1851 */ 1852 mvrec = (struct jmvrec *)srn->sr_rec; 1853 if (mvrec->jm_parent != refrec->jr_parent || 1854 mvrec->jm_oldoff != refrec->jr_diroff) 1855 break; 1856 ino_dup_ref(sino, refrec, mvrec->jm_oldoff); 1857 refrec->jr_diroff = mvrec->jm_newoff; 1858 TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next); 1859 break; 1860 default: 1861 err_suj("ino_build_ref: Unknown op %s\n", 1862 JOP_OPTYPE(srn->sr_rec->rec_jrefrec.jr_op)); 1863 } 1864 } 1865 ino_add_ref(sino, srec); 1866 } 1867 1868 /* 1869 * Walk the list of new records and add them in-order resolving any 1870 * dups and adjusted offsets. 1871 */ 1872 static void 1873 ino_build(struct suj_ino *sino) 1874 { 1875 struct suj_rec *srec; 1876 1877 while ((srec = TAILQ_FIRST(&sino->si_newrecs)) != NULL) { 1878 TAILQ_REMOVE(&sino->si_newrecs, srec, sr_next); 1879 switch (srec->sr_rec->rec_jrefrec.jr_op) { 1880 case JOP_ADDREF: 1881 case JOP_REMREF: 1882 ino_build_ref(sino, srec); 1883 break; 1884 case JOP_MVREF: 1885 /* 1886 * Add this mvrec to the queue of pending mvs. 1887 */ 1888 TAILQ_INSERT_TAIL(&sino->si_movs, srec, sr_next); 1889 break; 1890 default: 1891 err_suj("ino_build: Unknown op %s\n", 1892 JOP_OPTYPE(srec->sr_rec->rec_jrefrec.jr_op)); 1893 } 1894 } 1895 if (TAILQ_EMPTY(&sino->si_recs)) 1896 sino->si_hasrecs = 0; 1897 } 1898 1899 /* 1900 * Modify journal records so they refer to the base block number 1901 * and a start and end frag range. This is to facilitate the discovery 1902 * of overlapping fragment allocations. 1903 */ 1904 static void 1905 blk_build(struct jblkrec *blkrec) 1906 { 1907 struct suj_rec *srec; 1908 struct suj_blk *sblk; 1909 struct jblkrec *blkrn; 1910 ufs2_daddr_t blk; 1911 int frag; 1912 1913 if (debug) 1914 printf("blk_build: op %s blkno %jd frags %d oldfrags %d " 1915 "ino %ju lbn %jd\n", 1916 JOP_OPTYPE(blkrec->jb_op), (uintmax_t)blkrec->jb_blkno, 1917 blkrec->jb_frags, blkrec->jb_oldfrags, 1918 (uintmax_t)blkrec->jb_ino, (uintmax_t)blkrec->jb_lbn); 1919 1920 blk = blknum(fs, blkrec->jb_blkno); 1921 frag = fragnum(fs, blkrec->jb_blkno); 1922 if (blkrec->jb_blkno < 0 || blk + fs->fs_frag - frag > fs->fs_size) 1923 err_suj("Out-of-bounds journal block number %jd\n", 1924 blkrec->jb_blkno); 1925 sblk = blk_lookup(blk, 1); 1926 /* 1927 * Rewrite the record using oldfrags to indicate the offset into 1928 * the block. Leave jb_frags as the actual allocated count. 1929 */ 1930 blkrec->jb_blkno -= frag; 1931 blkrec->jb_oldfrags = frag; 1932 if (blkrec->jb_oldfrags + blkrec->jb_frags > fs->fs_frag) 1933 err_suj("Invalid fragment count %d oldfrags %d\n", 1934 blkrec->jb_frags, frag); 1935 /* 1936 * Detect dups. If we detect a dup we always discard the oldest 1937 * record as it is superseded by the new record. This speeds up 1938 * later stages but also eliminates free records which are used 1939 * to indicate that the contents of indirects can be trusted. 1940 */ 1941 TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) { 1942 blkrn = (struct jblkrec *)srec->sr_rec; 1943 if (blkrn->jb_ino != blkrec->jb_ino || 1944 blkrn->jb_lbn != blkrec->jb_lbn || 1945 blkrn->jb_blkno != blkrec->jb_blkno || 1946 blkrn->jb_frags != blkrec->jb_frags || 1947 blkrn->jb_oldfrags != blkrec->jb_oldfrags) 1948 continue; 1949 if (debug) 1950 printf("Removed dup.\n"); 1951 /* Discard the free which is a dup with an alloc. */ 1952 if (blkrec->jb_op == JOP_FREEBLK) 1953 return; 1954 TAILQ_REMOVE(&sblk->sb_recs, srec, sr_next); 1955 free(srec); 1956 break; 1957 } 1958 srec = errmalloc(sizeof(*srec)); 1959 srec->sr_rec = (union jrec *)blkrec; 1960 TAILQ_INSERT_TAIL(&sblk->sb_recs, srec, sr_next); 1961 } 1962 1963 static void 1964 ino_build_trunc(struct jtrncrec *rec) 1965 { 1966 struct suj_ino *sino; 1967 1968 if (debug) 1969 printf("ino_build_trunc: op %d ino %ju, size %jd\n", 1970 rec->jt_op, (uintmax_t)rec->jt_ino, 1971 (uintmax_t)rec->jt_size); 1972 if (chkfilesize(IFREG, rec->jt_size) == 0) 1973 err_suj("ino_build: truncation size too large %ju\n", 1974 (intmax_t)rec->jt_size); 1975 sino = ino_lookup(rec->jt_ino, 1); 1976 if (rec->jt_op == JOP_SYNC) { 1977 sino->si_trunc = NULL; 1978 return; 1979 } 1980 if (sino->si_trunc == NULL || sino->si_trunc->jt_size > rec->jt_size) 1981 sino->si_trunc = rec; 1982 } 1983 1984 /* 1985 * Build up tables of the operations we need to recover. 1986 */ 1987 static void 1988 suj_build(void) 1989 { 1990 struct suj_seg *seg; 1991 union jrec *rec; 1992 int off; 1993 int i; 1994 1995 TAILQ_FOREACH(seg, &allsegs, ss_next) { 1996 if (debug) 1997 printf("seg %jd has %d records, oldseq %jd.\n", 1998 seg->ss_rec.jsr_seq, seg->ss_rec.jsr_cnt, 1999 seg->ss_rec.jsr_oldest); 2000 off = 0; 2001 rec = (union jrec *)seg->ss_blk; 2002 for (i = 0; i < seg->ss_rec.jsr_cnt; off += JREC_SIZE, rec++) { 2003 /* skip the segrec. */ 2004 if ((off % real_dev_bsize) == 0) 2005 continue; 2006 switch (rec->rec_jrefrec.jr_op) { 2007 case JOP_ADDREF: 2008 case JOP_REMREF: 2009 case JOP_MVREF: 2010 ino_append(rec); 2011 break; 2012 case JOP_NEWBLK: 2013 case JOP_FREEBLK: 2014 blk_build((struct jblkrec *)rec); 2015 break; 2016 case JOP_TRUNC: 2017 case JOP_SYNC: 2018 ino_build_trunc((struct jtrncrec *)rec); 2019 break; 2020 default: 2021 err_suj("Unknown journal operation %s at %d\n", 2022 JOP_OPTYPE(rec->rec_jrefrec.jr_op), off); 2023 } 2024 i++; 2025 } 2026 } 2027 } 2028 2029 /* 2030 * Prune the journal segments to those we care about based on the 2031 * oldest sequence in the newest segment. Order the segment list 2032 * based on sequence number. 2033 */ 2034 static void 2035 suj_prune(void) 2036 { 2037 struct suj_seg *seg; 2038 struct suj_seg *segn; 2039 uint64_t newseq; 2040 int discard; 2041 2042 if (debug) 2043 printf("Pruning up to %jd\n", oldseq); 2044 /* First free the expired segments. */ 2045 TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) { 2046 if (seg->ss_rec.jsr_seq >= oldseq) 2047 continue; 2048 TAILQ_REMOVE(&allsegs, seg, ss_next); 2049 free(seg->ss_blk); 2050 free(seg); 2051 } 2052 /* Next ensure that segments are ordered properly. */ 2053 seg = TAILQ_FIRST(&allsegs); 2054 if (seg == NULL) { 2055 if (debug) 2056 printf("Empty journal\n"); 2057 return; 2058 } 2059 newseq = seg->ss_rec.jsr_seq; 2060 for (;;) { 2061 seg = TAILQ_LAST(&allsegs, seghd); 2062 if (seg->ss_rec.jsr_seq >= newseq) 2063 break; 2064 TAILQ_REMOVE(&allsegs, seg, ss_next); 2065 TAILQ_INSERT_HEAD(&allsegs, seg, ss_next); 2066 newseq = seg->ss_rec.jsr_seq; 2067 2068 } 2069 if (newseq != oldseq) { 2070 TAILQ_FOREACH(seg, &allsegs, ss_next) { 2071 printf("%jd, ", seg->ss_rec.jsr_seq); 2072 } 2073 printf("\n"); 2074 err_suj("Journal file sequence mismatch %jd != %jd\n", 2075 newseq, oldseq); 2076 } 2077 /* 2078 * The kernel may asynchronously write segments which can create 2079 * gaps in the sequence space. Throw away any segments after the 2080 * gap as the kernel guarantees only those that are contiguously 2081 * reachable are marked as completed. 2082 */ 2083 discard = 0; 2084 TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) { 2085 if (!discard && newseq++ == seg->ss_rec.jsr_seq) { 2086 jrecs += seg->ss_rec.jsr_cnt; 2087 jbytes += seg->ss_rec.jsr_blocks * real_dev_bsize; 2088 continue; 2089 } 2090 discard = 1; 2091 if (debug) 2092 printf("Journal order mismatch %jd != %jd pruning\n", 2093 newseq-1, seg->ss_rec.jsr_seq); 2094 TAILQ_REMOVE(&allsegs, seg, ss_next); 2095 free(seg->ss_blk); 2096 free(seg); 2097 } 2098 if (debug) 2099 printf("Processing journal segments from %jd to %jd\n", 2100 oldseq, newseq-1); 2101 } 2102 2103 /* 2104 * Verify the journal inode before attempting to read records. 2105 */ 2106 static int 2107 suj_verifyino(union dinode *dp) 2108 { 2109 2110 if (DIP(dp, di_nlink) != 1) { 2111 printf("Invalid link count %d for journal inode %ju\n", 2112 DIP(dp, di_nlink), (uintmax_t)sujino); 2113 return (-1); 2114 } 2115 2116 if ((DIP(dp, di_flags) & (SF_IMMUTABLE | SF_NOUNLINK)) != 2117 (SF_IMMUTABLE | SF_NOUNLINK)) { 2118 printf("Invalid flags 0x%X for journal inode %ju\n", 2119 DIP(dp, di_flags), (uintmax_t)sujino); 2120 return (-1); 2121 } 2122 2123 if (DIP(dp, di_mode) != (IFREG | IREAD)) { 2124 printf("Invalid mode %o for journal inode %ju\n", 2125 DIP(dp, di_mode), (uintmax_t)sujino); 2126 return (-1); 2127 } 2128 2129 if (DIP(dp, di_size) < SUJ_MIN) { 2130 printf("Invalid size %jd for journal inode %ju\n", 2131 DIP(dp, di_size), (uintmax_t)sujino); 2132 return (-1); 2133 } 2134 2135 if (DIP(dp, di_modrev) != fs->fs_mtime) { 2136 if (!bkgrdcheck || debug) 2137 printf("Journal timestamp does not match " 2138 "fs mount time\n"); 2139 return (-1); 2140 } 2141 2142 return (0); 2143 } 2144 2145 struct jblocks { 2146 struct jextent *jb_extent; /* Extent array. */ 2147 int jb_avail; /* Available extents. */ 2148 int jb_used; /* Last used extent. */ 2149 int jb_head; /* Allocator head. */ 2150 int jb_off; /* Allocator extent offset. */ 2151 }; 2152 struct jextent { 2153 ufs2_daddr_t je_daddr; /* Disk block address. */ 2154 int je_blocks; /* Disk block count. */ 2155 }; 2156 2157 static struct jblocks *suj_jblocks; 2158 2159 static struct jblocks * 2160 jblocks_create(void) 2161 { 2162 struct jblocks *jblocks; 2163 int size; 2164 2165 jblocks = errmalloc(sizeof(*jblocks)); 2166 jblocks->jb_avail = 10; 2167 jblocks->jb_used = 0; 2168 jblocks->jb_head = 0; 2169 jblocks->jb_off = 0; 2170 size = sizeof(struct jextent) * jblocks->jb_avail; 2171 jblocks->jb_extent = errmalloc(size); 2172 bzero(jblocks->jb_extent, size); 2173 2174 return (jblocks); 2175 } 2176 2177 /* 2178 * Return the next available disk block and the amount of contiguous 2179 * free space it contains. 2180 */ 2181 static ufs2_daddr_t 2182 jblocks_next(struct jblocks *jblocks, int bytes, int *actual) 2183 { 2184 struct jextent *jext; 2185 ufs2_daddr_t daddr; 2186 int freecnt; 2187 int blocks; 2188 2189 blocks = btodb(bytes); 2190 jext = &jblocks->jb_extent[jblocks->jb_head]; 2191 freecnt = jext->je_blocks - jblocks->jb_off; 2192 if (freecnt == 0) { 2193 jblocks->jb_off = 0; 2194 if (++jblocks->jb_head > jblocks->jb_used) 2195 return (0); 2196 jext = &jblocks->jb_extent[jblocks->jb_head]; 2197 freecnt = jext->je_blocks; 2198 } 2199 if (freecnt > blocks) 2200 freecnt = blocks; 2201 *actual = dbtob(freecnt); 2202 daddr = jext->je_daddr + jblocks->jb_off; 2203 2204 return (daddr); 2205 } 2206 2207 /* 2208 * Advance the allocation head by a specified number of bytes, consuming 2209 * one journal segment. 2210 */ 2211 static void 2212 jblocks_advance(struct jblocks *jblocks, int bytes) 2213 { 2214 2215 jblocks->jb_off += btodb(bytes); 2216 } 2217 2218 static void 2219 jblocks_destroy(struct jblocks *jblocks) 2220 { 2221 2222 free(jblocks->jb_extent); 2223 free(jblocks); 2224 } 2225 2226 static void 2227 jblocks_add(struct jblocks *jblocks, ufs2_daddr_t daddr, int blocks) 2228 { 2229 struct jextent *jext; 2230 int size; 2231 2232 jext = &jblocks->jb_extent[jblocks->jb_used]; 2233 /* Adding the first block. */ 2234 if (jext->je_daddr == 0) { 2235 jext->je_daddr = daddr; 2236 jext->je_blocks = blocks; 2237 return; 2238 } 2239 /* Extending the last extent. */ 2240 if (jext->je_daddr + jext->je_blocks == daddr) { 2241 jext->je_blocks += blocks; 2242 return; 2243 } 2244 /* Adding a new extent. */ 2245 if (++jblocks->jb_used == jblocks->jb_avail) { 2246 jblocks->jb_avail *= 2; 2247 size = sizeof(struct jextent) * jblocks->jb_avail; 2248 jext = errmalloc(size); 2249 bzero(jext, size); 2250 bcopy(jblocks->jb_extent, jext, 2251 sizeof(struct jextent) * jblocks->jb_used); 2252 free(jblocks->jb_extent); 2253 jblocks->jb_extent = jext; 2254 } 2255 jext = &jblocks->jb_extent[jblocks->jb_used]; 2256 jext->je_daddr = daddr; 2257 jext->je_blocks = blocks; 2258 2259 return; 2260 } 2261 2262 /* 2263 * Add a file block from the journal to the extent map. We can't read 2264 * each file block individually because the kernel treats it as a circular 2265 * buffer and segments may span multiple contiguous blocks. 2266 */ 2267 static void 2268 suj_add_block(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 2269 { 2270 2271 jblocks_add(suj_jblocks, fsbtodb(fs, blk), fsbtodb(fs, frags)); 2272 } 2273 2274 static void 2275 suj_read(void) 2276 { 2277 uint8_t block[1 * 1024 * 1024]; 2278 struct suj_seg *seg; 2279 struct jsegrec *recn; 2280 struct jsegrec *rec; 2281 ufs2_daddr_t blk; 2282 int readsize; 2283 int blocks; 2284 int recsize; 2285 int size; 2286 int i; 2287 2288 /* 2289 * Read records until we exhaust the journal space. If we find 2290 * an invalid record we start searching for a valid segment header 2291 * at the next block. This is because we don't have a head/tail 2292 * pointer and must recover the information indirectly. At the gap 2293 * between the head and tail we won't necessarily have a valid 2294 * segment. 2295 */ 2296 restart: 2297 for (;;) { 2298 size = sizeof(block); 2299 blk = jblocks_next(suj_jblocks, size, &readsize); 2300 if (blk == 0) 2301 return; 2302 size = readsize; 2303 /* 2304 * Read 1MB at a time and scan for records within this block. 2305 */ 2306 if (pread(fsreadfd, &block, size, dbtob(blk)) != size) { 2307 err_suj("Error reading journal block %jd\n", 2308 (intmax_t)blk); 2309 } 2310 for (rec = (void *)block; size; size -= recsize, 2311 rec = (struct jsegrec *)((uintptr_t)rec + recsize)) { 2312 recsize = real_dev_bsize; 2313 if (rec->jsr_time != fs->fs_mtime) { 2314 #ifdef notdef 2315 if (debug) 2316 printf("Rec time %jd != fs mtime %jd\n", 2317 rec->jsr_time, fs->fs_mtime); 2318 #endif 2319 jblocks_advance(suj_jblocks, recsize); 2320 continue; 2321 } 2322 if (rec->jsr_cnt == 0) { 2323 if (debug) 2324 printf("Found illegal count %d\n", 2325 rec->jsr_cnt); 2326 jblocks_advance(suj_jblocks, recsize); 2327 continue; 2328 } 2329 blocks = rec->jsr_blocks; 2330 recsize = blocks * real_dev_bsize; 2331 if (recsize > size) { 2332 /* 2333 * We may just have run out of buffer, restart 2334 * the loop to re-read from this spot. 2335 */ 2336 if (size < fs->fs_bsize && 2337 size != readsize && 2338 recsize <= fs->fs_bsize) 2339 goto restart; 2340 if (debug) 2341 printf("Found invalid segsize " 2342 "%d > %d\n", recsize, size); 2343 recsize = real_dev_bsize; 2344 jblocks_advance(suj_jblocks, recsize); 2345 continue; 2346 } 2347 /* 2348 * Verify that all blocks in the segment are present. 2349 */ 2350 for (i = 1; i < blocks; i++) { 2351 recn = (void *)((uintptr_t)rec) + i * 2352 real_dev_bsize; 2353 if (recn->jsr_seq == rec->jsr_seq && 2354 recn->jsr_time == rec->jsr_time) 2355 continue; 2356 if (debug) 2357 printf("Incomplete record %jd (%d)\n", 2358 rec->jsr_seq, i); 2359 recsize = i * real_dev_bsize; 2360 jblocks_advance(suj_jblocks, recsize); 2361 goto restart; 2362 } 2363 seg = errmalloc(sizeof(*seg)); 2364 seg->ss_blk = errmalloc(recsize); 2365 seg->ss_rec = *rec; 2366 bcopy((void *)rec, seg->ss_blk, recsize); 2367 if (rec->jsr_oldest > oldseq) 2368 oldseq = rec->jsr_oldest; 2369 TAILQ_INSERT_TAIL(&allsegs, seg, ss_next); 2370 jblocks_advance(suj_jblocks, recsize); 2371 } 2372 } 2373 } 2374 2375 /* 2376 * Orchestrate the verification of a filesystem via the softupdates journal. 2377 */ 2378 int 2379 suj_check(const char *filesys) 2380 { 2381 struct inodesc idesc; 2382 struct csum *cgsum; 2383 union dinode *dp, *jip; 2384 struct inode ip; 2385 uint64_t blocks; 2386 int i, retval; 2387 struct suj_seg *seg; 2388 struct suj_seg *segn; 2389 2390 initsuj(); 2391 fs = &sblock; 2392 if (real_dev_bsize == 0 && ioctl(fsreadfd, DIOCGSECTORSIZE, 2393 &real_dev_bsize) == -1) 2394 real_dev_bsize = secsize; 2395 if (debug) 2396 printf("dev_bsize %u\n", real_dev_bsize); 2397 2398 /* 2399 * Set an exit point when SUJ check failed 2400 */ 2401 retval = setjmp(jmpbuf); 2402 if (retval != 0) { 2403 pwarn("UNEXPECTED SU+J INCONSISTENCY\n"); 2404 TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) { 2405 TAILQ_REMOVE(&allsegs, seg, ss_next); 2406 free(seg->ss_blk); 2407 free(seg); 2408 } 2409 if (reply("FALLBACK TO FULL FSCK") == 0) { 2410 ckfini(0); 2411 exit(EEXIT); 2412 } else 2413 return (-1); 2414 } 2415 2416 /* 2417 * Search the root directory for the SUJ_FILE. 2418 */ 2419 idesc.id_type = DATA; 2420 idesc.id_fix = IGNORE; 2421 idesc.id_number = UFS_ROOTINO; 2422 idesc.id_func = findino; 2423 idesc.id_name = SUJ_FILE; 2424 ginode(UFS_ROOTINO, &ip); 2425 dp = ip.i_dp; 2426 if ((DIP(dp, di_mode) & IFMT) != IFDIR) { 2427 irelse(&ip); 2428 err_suj("root inode is not a directory\n"); 2429 } 2430 if (DIP(dp, di_size) < 0 || DIP(dp, di_size) > MAXDIRSIZE) { 2431 irelse(&ip); 2432 err_suj("negative or oversized root directory %jd\n", 2433 (uintmax_t)DIP(dp, di_size)); 2434 } 2435 if ((ckinode(dp, &idesc) & FOUND) == FOUND) { 2436 sujino = idesc.id_parent; 2437 irelse(&ip); 2438 } else { 2439 if (!bkgrdcheck || debug) 2440 printf("Journal inode removed. " 2441 "Use tunefs to re-create.\n"); 2442 sblock.fs_flags &= ~FS_SUJ; 2443 sblock.fs_sujfree = 0; 2444 irelse(&ip); 2445 return (-1); 2446 } 2447 /* 2448 * Fetch the journal inode and verify it. 2449 */ 2450 ginode(sujino, &ip); 2451 jip = ip.i_dp; 2452 if (!bkgrdcheck || debug) 2453 printf("** SU+J Recovering %s\n", filesys); 2454 if (suj_verifyino(jip) != 0 || (!preen && !reply("USE JOURNAL"))) { 2455 irelse(&ip); 2456 return (-1); 2457 } 2458 /* 2459 * Build a list of journal blocks in jblocks before parsing the 2460 * available journal blocks in with suj_read(). 2461 */ 2462 if (!bkgrdcheck || debug) 2463 printf("** Reading %jd byte journal from inode %ju.\n", 2464 DIP(jip, di_size), (uintmax_t)sujino); 2465 suj_jblocks = jblocks_create(); 2466 blocks = ino_visit(jip, sujino, suj_add_block, 0); 2467 if (blocks != numfrags(fs, DIP(jip, di_size))) { 2468 if (!bkgrdcheck || debug) 2469 printf("Sparse journal inode %ju.\n", 2470 (uintmax_t)sujino); 2471 irelse(&ip); 2472 return (-1); 2473 } 2474 /* If journal is valid then do journal check rather than background */ 2475 if (bkgrdcheck) { 2476 irelse(&ip); 2477 return (0); 2478 } 2479 irelse(&ip); 2480 suj_read(); 2481 jblocks_destroy(suj_jblocks); 2482 suj_jblocks = NULL; 2483 if (preen || reply("RECOVER")) { 2484 printf("** Building recovery table.\n"); 2485 suj_prune(); 2486 suj_build(); 2487 cg_apply(cg_build); 2488 printf("** Resolving unreferenced inode list.\n"); 2489 ino_unlinked(); 2490 printf("** Processing journal entries.\n"); 2491 cg_apply(cg_trunc); 2492 cg_apply(cg_check_blk); 2493 cg_apply(cg_adj_blk); 2494 cg_apply(cg_check_ino); 2495 } 2496 if (preen == 0 && (jrecs > 0 || jbytes > 0) && 2497 reply("WRITE CHANGES") == 0) 2498 return (0); 2499 /* 2500 * Check block counts of snapshot inodes and 2501 * make copies of any needed snapshot blocks. 2502 */ 2503 for (i = 0; i < snapcnt; i++) 2504 check_blkcnt(&snaplist[i]); 2505 snapflush(suj_checkblkavail); 2506 /* 2507 * Recompute the fs summary info from correct cs summaries. 2508 */ 2509 bzero(&fs->fs_cstotal, sizeof(struct csum_total)); 2510 for (i = 0; i < fs->fs_ncg; i++) { 2511 cgsum = &fs->fs_cs(fs, i); 2512 fs->fs_cstotal.cs_nffree += cgsum->cs_nffree; 2513 fs->fs_cstotal.cs_nbfree += cgsum->cs_nbfree; 2514 fs->fs_cstotal.cs_nifree += cgsum->cs_nifree; 2515 fs->fs_cstotal.cs_ndir += cgsum->cs_ndir; 2516 } 2517 fs->fs_pendinginodes = 0; 2518 fs->fs_pendingblocks = 0; 2519 fs->fs_clean = 1; 2520 fs->fs_time = time(NULL); 2521 fs->fs_mtime = time(NULL); 2522 sbdirty(); 2523 ckfini(1); 2524 if (jrecs > 0 || jbytes > 0) { 2525 printf("** %jd journal records in %jd bytes for %.2f%% " 2526 "utilization\n", jrecs, jbytes, 2527 ((float)jrecs / (float)(jbytes / JREC_SIZE)) * 100); 2528 printf("** Freed %jd inodes (%jd dirs) %jd blocks, and %jd " 2529 "frags.\n", freeinos, freedir, freeblocks, freefrags); 2530 } 2531 2532 return (0); 2533 } 2534 2535 static void 2536 initsuj(void) 2537 { 2538 int i; 2539 2540 for (i = 0; i < HASHSIZE; i++) 2541 LIST_INIT(&cghash[i]); 2542 lastcg = NULL; 2543 TAILQ_INIT(&allsegs); 2544 oldseq = 0; 2545 fs = NULL; 2546 sujino = 0; 2547 freefrags = 0; 2548 freeblocks = 0; 2549 freeinos = 0; 2550 freedir = 0; 2551 jbytes = 0; 2552 jrecs = 0; 2553 suj_jblocks = NULL; 2554 } 2555