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