1207141Sjeff/*- 2207141Sjeff * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 3207141Sjeff * All rights reserved. 4207141Sjeff * 5207141Sjeff * Redistribution and use in source and binary forms, with or without 6207141Sjeff * modification, are permitted provided that the following conditions 7207141Sjeff * are met: 8207141Sjeff * 1. Redistributions of source code must retain the above copyright 9207141Sjeff * notice, this list of conditions and the following disclaimer. 10207141Sjeff * 2. Redistributions in binary form must reproduce the above copyright 11207141Sjeff * notice, this list of conditions and the following disclaimer in the 12207141Sjeff * documentation and/or other materials provided with the distribution. 13207141Sjeff * 14207141Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15207141Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16207141Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17207141Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18207141Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19207141Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20207141Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21207141Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22207141Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23207141Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24207141Sjeff * SUCH DAMAGE. 25207141Sjeff */ 26207141Sjeff 27207141Sjeff#include <sys/cdefs.h> 28207141Sjeff__FBSDID("$FreeBSD$"); 29207141Sjeff 30207141Sjeff#include <sys/param.h> 31218604Skib#include <sys/disk.h> 32207141Sjeff#include <sys/disklabel.h> 33207141Sjeff#include <sys/mount.h> 34207141Sjeff#include <sys/stat.h> 35207141Sjeff 36207141Sjeff#include <ufs/ufs/ufsmount.h> 37207141Sjeff#include <ufs/ufs/dinode.h> 38207141Sjeff#include <ufs/ufs/dir.h> 39207141Sjeff#include <ufs/ffs/fs.h> 40207141Sjeff 41217769Smckusick#include <assert.h> 42217769Smckusick#include <err.h> 43209408Sdelphij#include <setjmp.h> 44209408Sdelphij#include <stdarg.h> 45207141Sjeff#include <stdio.h> 46207141Sjeff#include <stdlib.h> 47207141Sjeff#include <stdint.h> 48207141Sjeff#include <libufs.h> 49207141Sjeff#include <string.h> 50207141Sjeff#include <strings.h> 51209408Sdelphij#include <sysexits.h> 52217769Smckusick#include <time.h> 53207141Sjeff 54207141Sjeff#include "fsck.h" 55207141Sjeff 56207141Sjeff#define DOTDOT_OFFSET DIRECTSIZ(1) 57207141Sjeff#define SUJ_HASHSIZE 2048 58207141Sjeff#define SUJ_HASHMASK (SUJ_HASHSIZE - 1) 59207141Sjeff#define SUJ_HASH(x) ((x * 2654435761) & SUJ_HASHMASK) 60207141Sjeff 61207141Sjeffstruct suj_seg { 62207141Sjeff TAILQ_ENTRY(suj_seg) ss_next; 63207141Sjeff struct jsegrec ss_rec; 64207141Sjeff uint8_t *ss_blk; 65207141Sjeff}; 66207141Sjeff 67207141Sjeffstruct suj_rec { 68207141Sjeff TAILQ_ENTRY(suj_rec) sr_next; 69207141Sjeff union jrec *sr_rec; 70207141Sjeff}; 71207141SjeffTAILQ_HEAD(srechd, suj_rec); 72207141Sjeff 73207141Sjeffstruct suj_ino { 74207141Sjeff LIST_ENTRY(suj_ino) si_next; 75207141Sjeff struct srechd si_recs; 76207141Sjeff struct srechd si_newrecs; 77207141Sjeff struct srechd si_movs; 78207141Sjeff struct jtrncrec *si_trunc; 79207141Sjeff ino_t si_ino; 80207141Sjeff char si_skipparent; 81207141Sjeff char si_hasrecs; 82207141Sjeff char si_blkadj; 83207141Sjeff char si_linkadj; 84207141Sjeff int si_mode; 85207141Sjeff nlink_t si_nlinkadj; 86207141Sjeff nlink_t si_nlink; 87207141Sjeff nlink_t si_dotlinks; 88207141Sjeff}; 89207141SjeffLIST_HEAD(inohd, suj_ino); 90207141Sjeff 91207141Sjeffstruct suj_blk { 92207141Sjeff LIST_ENTRY(suj_blk) sb_next; 93207141Sjeff struct srechd sb_recs; 94207141Sjeff ufs2_daddr_t sb_blk; 95207141Sjeff}; 96207141SjeffLIST_HEAD(blkhd, suj_blk); 97207141Sjeff 98207141Sjeffstruct data_blk { 99207141Sjeff LIST_ENTRY(data_blk) db_next; 100207141Sjeff uint8_t *db_buf; 101207141Sjeff ufs2_daddr_t db_blk; 102207141Sjeff int db_size; 103207141Sjeff int db_dirty; 104207141Sjeff}; 105207141Sjeff 106207141Sjeffstruct ino_blk { 107207141Sjeff LIST_ENTRY(ino_blk) ib_next; 108207141Sjeff uint8_t *ib_buf; 109207141Sjeff int ib_dirty; 110207141Sjeff ufs2_daddr_t ib_blk; 111207141Sjeff}; 112207141SjeffLIST_HEAD(iblkhd, ino_blk); 113207141Sjeff 114207141Sjeffstruct suj_cg { 115207141Sjeff LIST_ENTRY(suj_cg) sc_next; 116207141Sjeff struct blkhd sc_blkhash[SUJ_HASHSIZE]; 117207141Sjeff struct inohd sc_inohash[SUJ_HASHSIZE]; 118207141Sjeff struct iblkhd sc_iblkhash[SUJ_HASHSIZE]; 119207141Sjeff struct ino_blk *sc_lastiblk; 120207141Sjeff struct suj_ino *sc_lastino; 121207141Sjeff struct suj_blk *sc_lastblk; 122207141Sjeff uint8_t *sc_cgbuf; 123207141Sjeff struct cg *sc_cgp; 124207141Sjeff int sc_dirty; 125207141Sjeff int sc_cgx; 126207141Sjeff}; 127207141Sjeff 128207141SjeffLIST_HEAD(cghd, suj_cg) cghash[SUJ_HASHSIZE]; 129207141SjeffLIST_HEAD(dblkhd, data_blk) dbhash[SUJ_HASHSIZE]; 130207141Sjeffstruct suj_cg *lastcg; 131207141Sjeffstruct data_blk *lastblk; 132207141Sjeff 133207141SjeffTAILQ_HEAD(seghd, suj_seg) allsegs; 134207141Sjeffuint64_t oldseq; 135207141Sjeffstatic struct uufsd *disk = NULL; 136207141Sjeffstatic struct fs *fs = NULL; 137207141Sjeffino_t sujino; 138207141Sjeff 139207141Sjeff/* 140207141Sjeff * Summary statistics. 141207141Sjeff */ 142207141Sjeffuint64_t freefrags; 143207141Sjeffuint64_t freeblocks; 144207141Sjeffuint64_t freeinos; 145207141Sjeffuint64_t freedir; 146207141Sjeffuint64_t jbytes; 147207141Sjeffuint64_t jrecs; 148207141Sjeff 149209408Sdelphijstatic jmp_buf jmpbuf; 150209408Sdelphij 151207141Sjefftypedef void (*ino_visitor)(ino_t, ufs_lbn_t, ufs2_daddr_t, int); 152209408Sdelphijstatic void err_suj(const char *, ...) __dead2; 153207141Sjeffstatic void ino_trunc(ino_t, off_t); 154207141Sjeffstatic void ino_decr(ino_t); 155207141Sjeffstatic void ino_adjust(struct suj_ino *); 156207141Sjeffstatic void ino_build(struct suj_ino *); 157207141Sjeffstatic int blk_isfree(ufs2_daddr_t); 158207141Sjeff 159207141Sjeffstatic void * 160207141Sjefferrmalloc(size_t n) 161207141Sjeff{ 162207141Sjeff void *a; 163207141Sjeff 164249788Smckusick a = Malloc(n); 165207141Sjeff if (a == NULL) 166209408Sdelphij err(EX_OSERR, "malloc(%zu)", n); 167207141Sjeff return (a); 168207141Sjeff} 169207141Sjeff 170207141Sjeff/* 171209408Sdelphij * When hit a fatal error in journalling check, print out 172209408Sdelphij * the error and then offer to fallback to normal fsck. 173209408Sdelphij */ 174209408Sdelphijstatic void 175209408Sdelphijerr_suj(const char * restrict fmt, ...) 176209408Sdelphij{ 177209408Sdelphij va_list ap; 178209408Sdelphij 179209408Sdelphij if (preen) 180209408Sdelphij (void)fprintf(stdout, "%s: ", cdevname); 181209408Sdelphij 182209408Sdelphij va_start(ap, fmt); 183209408Sdelphij (void)vfprintf(stdout, fmt, ap); 184209408Sdelphij va_end(ap); 185209408Sdelphij 186209408Sdelphij longjmp(jmpbuf, -1); 187209408Sdelphij} 188209408Sdelphij 189209408Sdelphij/* 190207141Sjeff * Open the given provider, load superblock. 191207141Sjeff */ 192207141Sjeffstatic void 193207141Sjeffopendisk(const char *devnam) 194207141Sjeff{ 195207141Sjeff if (disk != NULL) 196207141Sjeff return; 197249788Smckusick disk = Malloc(sizeof(*disk)); 198207141Sjeff if (disk == NULL) 199209408Sdelphij err(EX_OSERR, "malloc(%zu)", sizeof(*disk)); 200207141Sjeff if (ufs_disk_fillout(disk, devnam) == -1) { 201209408Sdelphij err(EX_OSERR, "ufs_disk_fillout(%s) failed: %s", devnam, 202207141Sjeff disk->d_error); 203207141Sjeff } 204207141Sjeff fs = &disk->d_fs; 205218604Skib if (real_dev_bsize == 0 && ioctl(disk->d_fd, DIOCGSECTORSIZE, 206218604Skib &real_dev_bsize) == -1) 207218604Skib real_dev_bsize = secsize; 208218604Skib if (debug) 209229256Skib printf("dev_bsize %u\n", real_dev_bsize); 210207141Sjeff} 211207141Sjeff 212207141Sjeff/* 213207141Sjeff * Mark file system as clean, write the super-block back, close the disk. 214207141Sjeff */ 215207141Sjeffstatic void 216207141Sjeffclosedisk(const char *devnam) 217207141Sjeff{ 218207141Sjeff struct csum *cgsum; 219207141Sjeff int i; 220207141Sjeff 221207141Sjeff /* 222207141Sjeff * Recompute the fs summary info from correct cs summaries. 223207141Sjeff */ 224207141Sjeff bzero(&fs->fs_cstotal, sizeof(struct csum_total)); 225207141Sjeff for (i = 0; i < fs->fs_ncg; i++) { 226207141Sjeff cgsum = &fs->fs_cs(fs, i); 227207141Sjeff fs->fs_cstotal.cs_nffree += cgsum->cs_nffree; 228207141Sjeff fs->fs_cstotal.cs_nbfree += cgsum->cs_nbfree; 229207141Sjeff fs->fs_cstotal.cs_nifree += cgsum->cs_nifree; 230207141Sjeff fs->fs_cstotal.cs_ndir += cgsum->cs_ndir; 231207141Sjeff } 232207141Sjeff fs->fs_pendinginodes = 0; 233207141Sjeff fs->fs_pendingblocks = 0; 234207141Sjeff fs->fs_clean = 1; 235207141Sjeff fs->fs_time = time(NULL); 236207141Sjeff fs->fs_mtime = time(NULL); 237207141Sjeff if (sbwrite(disk, 0) == -1) 238209408Sdelphij err(EX_OSERR, "sbwrite(%s)", devnam); 239207141Sjeff if (ufs_disk_close(disk) == -1) 240209408Sdelphij err(EX_OSERR, "ufs_disk_close(%s)", devnam); 241207141Sjeff free(disk); 242207141Sjeff disk = NULL; 243207141Sjeff fs = NULL; 244207141Sjeff} 245207141Sjeff 246207141Sjeff/* 247207141Sjeff * Lookup a cg by number in the hash so we can keep track of which cgs 248207141Sjeff * need stats rebuilt. 249207141Sjeff */ 250207141Sjeffstatic struct suj_cg * 251207141Sjeffcg_lookup(int cgx) 252207141Sjeff{ 253207141Sjeff struct cghd *hd; 254207141Sjeff struct suj_cg *sc; 255207141Sjeff 256209408Sdelphij if (cgx < 0 || cgx >= fs->fs_ncg) 257209408Sdelphij err_suj("Bad cg number %d\n", cgx); 258207141Sjeff if (lastcg && lastcg->sc_cgx == cgx) 259207141Sjeff return (lastcg); 260207141Sjeff hd = &cghash[SUJ_HASH(cgx)]; 261207141Sjeff LIST_FOREACH(sc, hd, sc_next) 262207141Sjeff if (sc->sc_cgx == cgx) { 263207141Sjeff lastcg = sc; 264207141Sjeff return (sc); 265207141Sjeff } 266207141Sjeff sc = errmalloc(sizeof(*sc)); 267207141Sjeff bzero(sc, sizeof(*sc)); 268207141Sjeff sc->sc_cgbuf = errmalloc(fs->fs_bsize); 269207141Sjeff sc->sc_cgp = (struct cg *)sc->sc_cgbuf; 270207141Sjeff sc->sc_cgx = cgx; 271207141Sjeff LIST_INSERT_HEAD(hd, sc, sc_next); 272207141Sjeff if (bread(disk, fsbtodb(fs, cgtod(fs, sc->sc_cgx)), sc->sc_cgbuf, 273207141Sjeff fs->fs_bsize) == -1) 274209408Sdelphij err_suj("Unable to read cylinder group %d\n", sc->sc_cgx); 275207141Sjeff 276207141Sjeff return (sc); 277207141Sjeff} 278207141Sjeff 279207141Sjeff/* 280207141Sjeff * Lookup an inode number in the hash and allocate a suj_ino if it does 281207141Sjeff * not exist. 282207141Sjeff */ 283207141Sjeffstatic struct suj_ino * 284207141Sjeffino_lookup(ino_t ino, int creat) 285207141Sjeff{ 286207141Sjeff struct suj_ino *sino; 287207141Sjeff struct inohd *hd; 288207141Sjeff struct suj_cg *sc; 289207141Sjeff 290207141Sjeff sc = cg_lookup(ino_to_cg(fs, ino)); 291207141Sjeff if (sc->sc_lastino && sc->sc_lastino->si_ino == ino) 292207141Sjeff return (sc->sc_lastino); 293207141Sjeff hd = &sc->sc_inohash[SUJ_HASH(ino)]; 294207141Sjeff LIST_FOREACH(sino, hd, si_next) 295207141Sjeff if (sino->si_ino == ino) 296207141Sjeff return (sino); 297207141Sjeff if (creat == 0) 298207141Sjeff return (NULL); 299207141Sjeff sino = errmalloc(sizeof(*sino)); 300207141Sjeff bzero(sino, sizeof(*sino)); 301207141Sjeff sino->si_ino = ino; 302207141Sjeff TAILQ_INIT(&sino->si_recs); 303207141Sjeff TAILQ_INIT(&sino->si_newrecs); 304207141Sjeff TAILQ_INIT(&sino->si_movs); 305207141Sjeff LIST_INSERT_HEAD(hd, sino, si_next); 306207141Sjeff 307207141Sjeff return (sino); 308207141Sjeff} 309207141Sjeff 310207141Sjeff/* 311207141Sjeff * Lookup a block number in the hash and allocate a suj_blk if it does 312207141Sjeff * not exist. 313207141Sjeff */ 314207141Sjeffstatic struct suj_blk * 315207141Sjeffblk_lookup(ufs2_daddr_t blk, int creat) 316207141Sjeff{ 317207141Sjeff struct suj_blk *sblk; 318207141Sjeff struct suj_cg *sc; 319207141Sjeff struct blkhd *hd; 320207141Sjeff 321207141Sjeff sc = cg_lookup(dtog(fs, blk)); 322207141Sjeff if (sc->sc_lastblk && sc->sc_lastblk->sb_blk == blk) 323207141Sjeff return (sc->sc_lastblk); 324207141Sjeff hd = &sc->sc_blkhash[SUJ_HASH(fragstoblks(fs, blk))]; 325207141Sjeff LIST_FOREACH(sblk, hd, sb_next) 326207141Sjeff if (sblk->sb_blk == blk) 327207141Sjeff return (sblk); 328207141Sjeff if (creat == 0) 329207141Sjeff return (NULL); 330207141Sjeff sblk = errmalloc(sizeof(*sblk)); 331207141Sjeff bzero(sblk, sizeof(*sblk)); 332207141Sjeff sblk->sb_blk = blk; 333207141Sjeff TAILQ_INIT(&sblk->sb_recs); 334207141Sjeff LIST_INSERT_HEAD(hd, sblk, sb_next); 335207141Sjeff 336207141Sjeff return (sblk); 337207141Sjeff} 338207141Sjeff 339207141Sjeffstatic struct data_blk * 340207141Sjeffdblk_lookup(ufs2_daddr_t blk) 341207141Sjeff{ 342207141Sjeff struct data_blk *dblk; 343207141Sjeff struct dblkhd *hd; 344207141Sjeff 345207141Sjeff hd = &dbhash[SUJ_HASH(fragstoblks(fs, blk))]; 346207141Sjeff if (lastblk && lastblk->db_blk == blk) 347207141Sjeff return (lastblk); 348207141Sjeff LIST_FOREACH(dblk, hd, db_next) 349207141Sjeff if (dblk->db_blk == blk) 350207141Sjeff return (dblk); 351207141Sjeff /* 352207141Sjeff * The inode block wasn't located, allocate a new one. 353207141Sjeff */ 354207141Sjeff dblk = errmalloc(sizeof(*dblk)); 355207141Sjeff bzero(dblk, sizeof(*dblk)); 356207141Sjeff LIST_INSERT_HEAD(hd, dblk, db_next); 357207141Sjeff dblk->db_blk = blk; 358207141Sjeff return (dblk); 359207141Sjeff} 360207141Sjeff 361207141Sjeffstatic uint8_t * 362207141Sjeffdblk_read(ufs2_daddr_t blk, int size) 363207141Sjeff{ 364207141Sjeff struct data_blk *dblk; 365207141Sjeff 366207141Sjeff dblk = dblk_lookup(blk); 367207141Sjeff /* 368207141Sjeff * I doubt size mismatches can happen in practice but it is trivial 369207141Sjeff * to handle. 370207141Sjeff */ 371207141Sjeff if (size != dblk->db_size) { 372207141Sjeff if (dblk->db_buf) 373207141Sjeff free(dblk->db_buf); 374207141Sjeff dblk->db_buf = errmalloc(size); 375207141Sjeff dblk->db_size = size; 376207141Sjeff if (bread(disk, fsbtodb(fs, blk), dblk->db_buf, size) == -1) 377209408Sdelphij err_suj("Failed to read data block %jd\n", blk); 378207141Sjeff } 379207141Sjeff return (dblk->db_buf); 380207141Sjeff} 381207141Sjeff 382207141Sjeffstatic void 383207141Sjeffdblk_dirty(ufs2_daddr_t blk) 384207141Sjeff{ 385207141Sjeff struct data_blk *dblk; 386207141Sjeff 387207141Sjeff dblk = dblk_lookup(blk); 388207141Sjeff dblk->db_dirty = 1; 389207141Sjeff} 390207141Sjeff 391207141Sjeffstatic void 392207141Sjeffdblk_write(void) 393207141Sjeff{ 394207141Sjeff struct data_blk *dblk; 395207141Sjeff int i; 396207141Sjeff 397207141Sjeff for (i = 0; i < SUJ_HASHSIZE; i++) { 398207141Sjeff LIST_FOREACH(dblk, &dbhash[i], db_next) { 399207141Sjeff if (dblk->db_dirty == 0 || dblk->db_size == 0) 400207141Sjeff continue; 401207141Sjeff if (bwrite(disk, fsbtodb(fs, dblk->db_blk), 402207141Sjeff dblk->db_buf, dblk->db_size) == -1) 403209408Sdelphij err_suj("Unable to write block %jd\n", 404207141Sjeff dblk->db_blk); 405207141Sjeff } 406207141Sjeff } 407207141Sjeff} 408207141Sjeff 409207141Sjeffstatic union dinode * 410207141Sjeffino_read(ino_t ino) 411207141Sjeff{ 412207141Sjeff struct ino_blk *iblk; 413207141Sjeff struct iblkhd *hd; 414207141Sjeff struct suj_cg *sc; 415207141Sjeff ufs2_daddr_t blk; 416207141Sjeff int off; 417207141Sjeff 418207141Sjeff blk = ino_to_fsba(fs, ino); 419207141Sjeff sc = cg_lookup(ino_to_cg(fs, ino)); 420207141Sjeff iblk = sc->sc_lastiblk; 421207141Sjeff if (iblk && iblk->ib_blk == blk) 422207141Sjeff goto found; 423207141Sjeff hd = &sc->sc_iblkhash[SUJ_HASH(fragstoblks(fs, blk))]; 424207141Sjeff LIST_FOREACH(iblk, hd, ib_next) 425207141Sjeff if (iblk->ib_blk == blk) 426207141Sjeff goto found; 427207141Sjeff /* 428207141Sjeff * The inode block wasn't located, allocate a new one. 429207141Sjeff */ 430207141Sjeff iblk = errmalloc(sizeof(*iblk)); 431207141Sjeff bzero(iblk, sizeof(*iblk)); 432207141Sjeff iblk->ib_buf = errmalloc(fs->fs_bsize); 433207141Sjeff iblk->ib_blk = blk; 434207141Sjeff LIST_INSERT_HEAD(hd, iblk, ib_next); 435207141Sjeff if (bread(disk, fsbtodb(fs, blk), iblk->ib_buf, fs->fs_bsize) == -1) 436209408Sdelphij err_suj("Failed to read inode block %jd\n", blk); 437207141Sjefffound: 438207141Sjeff sc->sc_lastiblk = iblk; 439207141Sjeff off = ino_to_fsbo(fs, ino); 440207141Sjeff if (fs->fs_magic == FS_UFS1_MAGIC) 441207141Sjeff return (union dinode *)&((struct ufs1_dinode *)iblk->ib_buf)[off]; 442207141Sjeff else 443207141Sjeff return (union dinode *)&((struct ufs2_dinode *)iblk->ib_buf)[off]; 444207141Sjeff} 445207141Sjeff 446207141Sjeffstatic void 447207141Sjeffino_dirty(ino_t ino) 448207141Sjeff{ 449207141Sjeff struct ino_blk *iblk; 450207141Sjeff struct iblkhd *hd; 451207141Sjeff struct suj_cg *sc; 452207141Sjeff ufs2_daddr_t blk; 453207141Sjeff 454207141Sjeff blk = ino_to_fsba(fs, ino); 455207141Sjeff sc = cg_lookup(ino_to_cg(fs, ino)); 456207141Sjeff iblk = sc->sc_lastiblk; 457207141Sjeff if (iblk && iblk->ib_blk == blk) { 458207141Sjeff iblk->ib_dirty = 1; 459207141Sjeff return; 460207141Sjeff } 461207141Sjeff hd = &sc->sc_iblkhash[SUJ_HASH(fragstoblks(fs, blk))]; 462207141Sjeff LIST_FOREACH(iblk, hd, ib_next) { 463207141Sjeff if (iblk->ib_blk == blk) { 464207141Sjeff iblk->ib_dirty = 1; 465207141Sjeff return; 466207141Sjeff } 467207141Sjeff } 468207141Sjeff ino_read(ino); 469207141Sjeff ino_dirty(ino); 470207141Sjeff} 471207141Sjeff 472207141Sjeffstatic void 473207141Sjeffiblk_write(struct ino_blk *iblk) 474207141Sjeff{ 475207141Sjeff 476207141Sjeff if (iblk->ib_dirty == 0) 477207141Sjeff return; 478207141Sjeff if (bwrite(disk, fsbtodb(fs, iblk->ib_blk), iblk->ib_buf, 479207141Sjeff fs->fs_bsize) == -1) 480209408Sdelphij err_suj("Failed to write inode block %jd\n", iblk->ib_blk); 481207141Sjeff} 482207141Sjeff 483207141Sjeffstatic int 484207141Sjeffblk_overlaps(struct jblkrec *brec, ufs2_daddr_t start, int frags) 485207141Sjeff{ 486207141Sjeff ufs2_daddr_t bstart; 487207141Sjeff ufs2_daddr_t bend; 488207141Sjeff ufs2_daddr_t end; 489207141Sjeff 490207141Sjeff end = start + frags; 491207141Sjeff bstart = brec->jb_blkno + brec->jb_oldfrags; 492207141Sjeff bend = bstart + brec->jb_frags; 493207141Sjeff if (start < bend && end > bstart) 494207141Sjeff return (1); 495207141Sjeff return (0); 496207141Sjeff} 497207141Sjeff 498207141Sjeffstatic int 499207141Sjeffblk_equals(struct jblkrec *brec, ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t start, 500207141Sjeff int frags) 501207141Sjeff{ 502207141Sjeff 503207141Sjeff if (brec->jb_ino != ino || brec->jb_lbn != lbn) 504207141Sjeff return (0); 505207141Sjeff if (brec->jb_blkno + brec->jb_oldfrags != start) 506207141Sjeff return (0); 507245778Sscottl if (brec->jb_frags < frags) 508207141Sjeff return (0); 509207141Sjeff return (1); 510207141Sjeff} 511207141Sjeff 512207141Sjeffstatic void 513207141Sjeffblk_setmask(struct jblkrec *brec, int *mask) 514207141Sjeff{ 515207141Sjeff int i; 516207141Sjeff 517207141Sjeff for (i = brec->jb_oldfrags; i < brec->jb_oldfrags + brec->jb_frags; i++) 518207141Sjeff *mask |= 1 << i; 519207141Sjeff} 520207141Sjeff 521207141Sjeff/* 522207141Sjeff * Determine whether a given block has been reallocated to a new location. 523207141Sjeff * Returns a mask of overlapping bits if any frags have been reused or 524207141Sjeff * zero if the block has not been re-used and the contents can be trusted. 525221110Sdes * 526207141Sjeff * This is used to ensure that an orphaned pointer due to truncate is safe 527207141Sjeff * to be freed. The mask value can be used to free partial blocks. 528207141Sjeff */ 529207141Sjeffstatic int 530207141Sjeffblk_freemask(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags) 531207141Sjeff{ 532207141Sjeff struct suj_blk *sblk; 533207141Sjeff struct suj_rec *srec; 534207141Sjeff struct jblkrec *brec; 535207141Sjeff int mask; 536207141Sjeff int off; 537207141Sjeff 538207141Sjeff /* 539207141Sjeff * To be certain we're not freeing a reallocated block we lookup 540207141Sjeff * this block in the blk hash and see if there is an allocation 541207141Sjeff * journal record that overlaps with any fragments in the block 542207141Sjeff * we're concerned with. If any fragments have ben reallocated 543207141Sjeff * the block has already been freed and re-used for another purpose. 544207141Sjeff */ 545207141Sjeff mask = 0; 546207141Sjeff sblk = blk_lookup(blknum(fs, blk), 0); 547207141Sjeff if (sblk == NULL) 548207141Sjeff return (0); 549207141Sjeff off = blk - sblk->sb_blk; 550207141Sjeff TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) { 551207141Sjeff brec = (struct jblkrec *)srec->sr_rec; 552207141Sjeff /* 553207141Sjeff * If the block overlaps but does not match 554207141Sjeff * exactly this record refers to the current 555207141Sjeff * location. 556221110Sdes */ 557207141Sjeff if (blk_overlaps(brec, blk, frags) == 0) 558207141Sjeff continue; 559207141Sjeff if (blk_equals(brec, ino, lbn, blk, frags) == 1) 560207141Sjeff mask = 0; 561207141Sjeff else 562207141Sjeff blk_setmask(brec, &mask); 563207141Sjeff } 564207141Sjeff if (debug) 565207141Sjeff printf("blk_freemask: blk %jd sblk %jd off %d mask 0x%X\n", 566207141Sjeff blk, sblk->sb_blk, off, mask); 567207141Sjeff return (mask >> off); 568207141Sjeff} 569207141Sjeff 570207141Sjeff/* 571207141Sjeff * Determine whether it is safe to follow an indirect. It is not safe 572207141Sjeff * if any part of the indirect has been reallocated or the last journal 573207141Sjeff * entry was an allocation. Just allocated indirects may not have valid 574207141Sjeff * pointers yet and all of their children will have their own records. 575207141Sjeff * It is also not safe to follow an indirect if the cg bitmap has been 576207141Sjeff * cleared as a new allocation may write to the block prior to the journal 577207141Sjeff * being written. 578221110Sdes * 579207141Sjeff * Returns 1 if it's safe to follow the indirect and 0 otherwise. 580207141Sjeff */ 581207141Sjeffstatic int 582207141Sjeffblk_isindir(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn) 583207141Sjeff{ 584207141Sjeff struct suj_blk *sblk; 585207141Sjeff struct jblkrec *brec; 586207141Sjeff 587207141Sjeff sblk = blk_lookup(blk, 0); 588207141Sjeff if (sblk == NULL) 589207141Sjeff return (1); 590207141Sjeff if (TAILQ_EMPTY(&sblk->sb_recs)) 591207141Sjeff return (1); 592207141Sjeff brec = (struct jblkrec *)TAILQ_LAST(&sblk->sb_recs, srechd)->sr_rec; 593207141Sjeff if (blk_equals(brec, ino, lbn, blk, fs->fs_frag)) 594207141Sjeff if (brec->jb_op == JOP_FREEBLK) 595207141Sjeff return (!blk_isfree(blk)); 596207141Sjeff return (0); 597207141Sjeff} 598207141Sjeff 599207141Sjeff/* 600207141Sjeff * Clear an inode from the cg bitmap. If the inode was already clear return 601207141Sjeff * 0 so the caller knows it does not have to check the inode contents. 602207141Sjeff */ 603207141Sjeffstatic int 604207141Sjeffino_free(ino_t ino, int mode) 605207141Sjeff{ 606207141Sjeff struct suj_cg *sc; 607207141Sjeff uint8_t *inosused; 608207141Sjeff struct cg *cgp; 609207141Sjeff int cg; 610207141Sjeff 611207141Sjeff cg = ino_to_cg(fs, ino); 612207141Sjeff ino = ino % fs->fs_ipg; 613207141Sjeff sc = cg_lookup(cg); 614207141Sjeff cgp = sc->sc_cgp; 615207141Sjeff inosused = cg_inosused(cgp); 616207141Sjeff /* 617207141Sjeff * The bitmap may never have made it to the disk so we have to 618207141Sjeff * conditionally clear. We can avoid writing the cg in this case. 619207141Sjeff */ 620207141Sjeff if (isclr(inosused, ino)) 621207141Sjeff return (0); 622207141Sjeff freeinos++; 623207141Sjeff clrbit(inosused, ino); 624207141Sjeff if (ino < cgp->cg_irotor) 625207141Sjeff cgp->cg_irotor = ino; 626207141Sjeff cgp->cg_cs.cs_nifree++; 627207141Sjeff if ((mode & IFMT) == IFDIR) { 628207141Sjeff freedir++; 629207141Sjeff cgp->cg_cs.cs_ndir--; 630207141Sjeff } 631207141Sjeff sc->sc_dirty = 1; 632207141Sjeff 633207141Sjeff return (1); 634207141Sjeff} 635207141Sjeff 636207141Sjeff/* 637207141Sjeff * Free 'frags' frags starting at filesystem block 'bno' skipping any frags 638207141Sjeff * set in the mask. 639207141Sjeff */ 640207141Sjeffstatic void 641207141Sjeffblk_free(ufs2_daddr_t bno, int mask, int frags) 642207141Sjeff{ 643207141Sjeff ufs1_daddr_t fragno, cgbno; 644207141Sjeff struct suj_cg *sc; 645207141Sjeff struct cg *cgp; 646207141Sjeff int i, cg; 647207141Sjeff uint8_t *blksfree; 648207141Sjeff 649207141Sjeff if (debug) 650245778Sscottl printf("Freeing %d frags at blk %jd mask 0x%x\n", 651245778Sscottl frags, bno, mask); 652207141Sjeff cg = dtog(fs, bno); 653207141Sjeff sc = cg_lookup(cg); 654207141Sjeff cgp = sc->sc_cgp; 655207141Sjeff cgbno = dtogd(fs, bno); 656207141Sjeff blksfree = cg_blksfree(cgp); 657207141Sjeff 658207141Sjeff /* 659207141Sjeff * If it's not allocated we only wrote the journal entry 660207141Sjeff * and never the bitmaps. Here we unconditionally clear and 661207141Sjeff * resolve the cg summary later. 662207141Sjeff */ 663207141Sjeff if (frags == fs->fs_frag && mask == 0) { 664207141Sjeff fragno = fragstoblks(fs, cgbno); 665207141Sjeff ffs_setblock(fs, blksfree, fragno); 666207141Sjeff freeblocks++; 667207141Sjeff } else { 668207141Sjeff /* 669207141Sjeff * deallocate the fragment 670207141Sjeff */ 671207141Sjeff for (i = 0; i < frags; i++) 672207141Sjeff if ((mask & (1 << i)) == 0 && isclr(blksfree, cgbno +i)) { 673207141Sjeff freefrags++; 674207141Sjeff setbit(blksfree, cgbno + i); 675207141Sjeff } 676207141Sjeff } 677207141Sjeff sc->sc_dirty = 1; 678207141Sjeff} 679207141Sjeff 680207141Sjeff/* 681207141Sjeff * Returns 1 if the whole block starting at 'bno' is marked free and 0 682207141Sjeff * otherwise. 683207141Sjeff */ 684207141Sjeffstatic int 685207141Sjeffblk_isfree(ufs2_daddr_t bno) 686207141Sjeff{ 687207141Sjeff struct suj_cg *sc; 688207141Sjeff 689207141Sjeff sc = cg_lookup(dtog(fs, bno)); 690207141Sjeff return ffs_isblock(fs, cg_blksfree(sc->sc_cgp), dtogd(fs, bno)); 691207141Sjeff} 692207141Sjeff 693207141Sjeff/* 694207141Sjeff * Fetch an indirect block to find the block at a given lbn. The lbn 695207141Sjeff * may be negative to fetch a specific indirect block pointer or positive 696207141Sjeff * to fetch a specific block. 697207141Sjeff */ 698207141Sjeffstatic ufs2_daddr_t 699207141Sjeffindir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn) 700207141Sjeff{ 701207141Sjeff ufs2_daddr_t *bap2; 702207141Sjeff ufs2_daddr_t *bap1; 703207141Sjeff ufs_lbn_t lbnadd; 704207141Sjeff ufs_lbn_t base; 705207141Sjeff int level; 706207141Sjeff int i; 707207141Sjeff 708207141Sjeff if (blk == 0) 709207141Sjeff return (0); 710207141Sjeff level = lbn_level(cur); 711207141Sjeff if (level == -1) 712209408Sdelphij err_suj("Invalid indir lbn %jd\n", lbn); 713207141Sjeff if (level == 0 && lbn < 0) 714209408Sdelphij err_suj("Invalid lbn %jd\n", lbn); 715207141Sjeff bap2 = (void *)dblk_read(blk, fs->fs_bsize); 716207141Sjeff bap1 = (void *)bap2; 717207141Sjeff lbnadd = 1; 718207141Sjeff base = -(cur + level); 719207141Sjeff for (i = level; i > 0; i--) 720207141Sjeff lbnadd *= NINDIR(fs); 721221110Sdes if (lbn > 0) 722207141Sjeff i = (lbn - base) / lbnadd; 723207141Sjeff else 724207141Sjeff i = (-lbn - base) / lbnadd; 725207141Sjeff if (i < 0 || i >= NINDIR(fs)) 726209408Sdelphij err_suj("Invalid indirect index %d produced by lbn %jd\n", 727207141Sjeff i, lbn); 728207141Sjeff if (level == 0) 729207141Sjeff cur = base + (i * lbnadd); 730207141Sjeff else 731207141Sjeff cur = -(base + (i * lbnadd)) - (level - 1); 732207141Sjeff if (fs->fs_magic == FS_UFS1_MAGIC) 733207141Sjeff blk = bap1[i]; 734207141Sjeff else 735207141Sjeff blk = bap2[i]; 736207141Sjeff if (cur == lbn) 737207141Sjeff return (blk); 738209408Sdelphij if (level == 0) 739209408Sdelphij err_suj("Invalid lbn %jd at level 0\n", lbn); 740207141Sjeff return indir_blkatoff(blk, ino, cur, lbn); 741207141Sjeff} 742207141Sjeff 743207141Sjeff/* 744207141Sjeff * Finds the disk block address at the specified lbn within the inode 745207141Sjeff * specified by ip. This follows the whole tree and honors di_size and 746207141Sjeff * di_extsize so it is a true test of reachability. The lbn may be 747207141Sjeff * negative if an extattr or indirect block is requested. 748207141Sjeff */ 749207141Sjeffstatic ufs2_daddr_t 750207141Sjeffino_blkatoff(union dinode *ip, ino_t ino, ufs_lbn_t lbn, int *frags) 751207141Sjeff{ 752207141Sjeff ufs_lbn_t tmpval; 753207141Sjeff ufs_lbn_t cur; 754207141Sjeff ufs_lbn_t next; 755207141Sjeff int i; 756207141Sjeff 757207141Sjeff /* 758207141Sjeff * Handle extattr blocks first. 759207141Sjeff */ 760207141Sjeff if (lbn < 0 && lbn >= -NXADDR) { 761207141Sjeff lbn = -1 - lbn; 762207141Sjeff if (lbn > lblkno(fs, ip->dp2.di_extsize - 1)) 763207141Sjeff return (0); 764207141Sjeff *frags = numfrags(fs, sblksize(fs, ip->dp2.di_extsize, lbn)); 765207141Sjeff return (ip->dp2.di_extb[lbn]); 766207141Sjeff } 767207141Sjeff /* 768207141Sjeff * Now direct and indirect. 769207141Sjeff */ 770207141Sjeff if (DIP(ip, di_mode) == IFLNK && 771207141Sjeff DIP(ip, di_size) < fs->fs_maxsymlinklen) 772207141Sjeff return (0); 773207141Sjeff if (lbn >= 0 && lbn < NDADDR) { 774207141Sjeff *frags = numfrags(fs, sblksize(fs, DIP(ip, di_size), lbn)); 775207141Sjeff return (DIP(ip, di_db[lbn])); 776207141Sjeff } 777207141Sjeff *frags = fs->fs_frag; 778207141Sjeff 779207141Sjeff for (i = 0, tmpval = NINDIR(fs), cur = NDADDR; i < NIADDR; i++, 780207141Sjeff tmpval *= NINDIR(fs), cur = next) { 781207141Sjeff next = cur + tmpval; 782207141Sjeff if (lbn == -cur - i) 783207141Sjeff return (DIP(ip, di_ib[i])); 784207141Sjeff /* 785207141Sjeff * Determine whether the lbn in question is within this tree. 786207141Sjeff */ 787207141Sjeff if (lbn < 0 && -lbn >= next) 788207141Sjeff continue; 789207141Sjeff if (lbn > 0 && lbn >= next) 790207141Sjeff continue; 791207141Sjeff return indir_blkatoff(DIP(ip, di_ib[i]), ino, -cur - i, lbn); 792207141Sjeff } 793209408Sdelphij err_suj("lbn %jd not in ino\n", lbn); 794209408Sdelphij /* NOTREACHED */ 795207141Sjeff} 796207141Sjeff 797207141Sjeff/* 798207141Sjeff * Determine whether a block exists at a particular lbn in an inode. 799207141Sjeff * Returns 1 if found, 0 if not. lbn may be negative for indirects 800207141Sjeff * or ext blocks. 801207141Sjeff */ 802207141Sjeffstatic int 803207141Sjeffblk_isat(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int *frags) 804207141Sjeff{ 805207141Sjeff union dinode *ip; 806207141Sjeff ufs2_daddr_t nblk; 807207141Sjeff 808207141Sjeff ip = ino_read(ino); 809207141Sjeff 810207141Sjeff if (DIP(ip, di_nlink) == 0 || DIP(ip, di_mode) == 0) 811207141Sjeff return (0); 812207141Sjeff nblk = ino_blkatoff(ip, ino, lbn, frags); 813207141Sjeff 814207141Sjeff return (nblk == blk); 815207141Sjeff} 816207141Sjeff 817207141Sjeff/* 818209716Sjeff * Clear the directory entry at diroff that should point to child. Minimal 819209716Sjeff * checking is done and it is assumed that this path was verified with isat. 820209716Sjeff */ 821209716Sjeffstatic void 822209716Sjeffino_clrat(ino_t parent, off_t diroff, ino_t child) 823209716Sjeff{ 824209716Sjeff union dinode *dip; 825209716Sjeff struct direct *dp; 826209716Sjeff ufs2_daddr_t blk; 827209716Sjeff uint8_t *block; 828209716Sjeff ufs_lbn_t lbn; 829209716Sjeff int blksize; 830209716Sjeff int frags; 831209716Sjeff int doff; 832209716Sjeff 833209716Sjeff if (debug) 834209716Sjeff printf("Clearing inode %d from parent %d at offset %jd\n", 835209716Sjeff child, parent, diroff); 836209716Sjeff 837209716Sjeff lbn = lblkno(fs, diroff); 838209716Sjeff doff = blkoff(fs, diroff); 839209716Sjeff dip = ino_read(parent); 840209716Sjeff blk = ino_blkatoff(dip, parent, lbn, &frags); 841209716Sjeff blksize = sblksize(fs, DIP(dip, di_size), lbn); 842209716Sjeff block = dblk_read(blk, blksize); 843209716Sjeff dp = (struct direct *)&block[doff]; 844209716Sjeff if (dp->d_ino != child) 845209716Sjeff errx(1, "Inode %d does not exist in %d at %jd", 846209716Sjeff child, parent, diroff); 847209716Sjeff dp->d_ino = 0; 848209716Sjeff dblk_dirty(blk); 849209716Sjeff /* 850209716Sjeff * The actual .. reference count will already have been removed 851209716Sjeff * from the parent by the .. remref record. 852209716Sjeff */ 853209716Sjeff} 854209716Sjeff 855209716Sjeff/* 856207141Sjeff * Determines whether a pointer to an inode exists within a directory 857207141Sjeff * at a specified offset. Returns the mode of the found entry. 858207141Sjeff */ 859207141Sjeffstatic int 860207141Sjeffino_isat(ino_t parent, off_t diroff, ino_t child, int *mode, int *isdot) 861207141Sjeff{ 862207141Sjeff union dinode *dip; 863207141Sjeff struct direct *dp; 864207141Sjeff ufs2_daddr_t blk; 865207141Sjeff uint8_t *block; 866207141Sjeff ufs_lbn_t lbn; 867207141Sjeff int blksize; 868207141Sjeff int frags; 869207141Sjeff int dpoff; 870207141Sjeff int doff; 871207141Sjeff 872207141Sjeff *isdot = 0; 873207141Sjeff dip = ino_read(parent); 874207141Sjeff *mode = DIP(dip, di_mode); 875207141Sjeff if ((*mode & IFMT) != IFDIR) { 876207141Sjeff if (debug) { 877207141Sjeff /* 878207141Sjeff * This can happen if the parent inode 879207141Sjeff * was reallocated. 880207141Sjeff */ 881207141Sjeff if (*mode != 0) 882207141Sjeff printf("Directory %d has bad mode %o\n", 883207141Sjeff parent, *mode); 884207141Sjeff else 885240961Szont printf("Directory %d has zero mode\n", parent); 886207141Sjeff } 887207141Sjeff return (0); 888207141Sjeff } 889207141Sjeff lbn = lblkno(fs, diroff); 890207141Sjeff doff = blkoff(fs, diroff); 891207141Sjeff blksize = sblksize(fs, DIP(dip, di_size), lbn); 892207141Sjeff if (diroff + DIRECTSIZ(1) > DIP(dip, di_size) || doff >= blksize) { 893207141Sjeff if (debug) 894207141Sjeff printf("ino %d absent from %d due to offset %jd" 895207141Sjeff " exceeding size %jd\n", 896207141Sjeff child, parent, diroff, DIP(dip, di_size)); 897207141Sjeff return (0); 898207141Sjeff } 899207141Sjeff blk = ino_blkatoff(dip, parent, lbn, &frags); 900207141Sjeff if (blk <= 0) { 901207141Sjeff if (debug) 902207141Sjeff printf("Sparse directory %d", parent); 903207141Sjeff return (0); 904207141Sjeff } 905207141Sjeff block = dblk_read(blk, blksize); 906207141Sjeff /* 907207141Sjeff * Walk through the records from the start of the block to be 908207141Sjeff * certain we hit a valid record and not some junk in the middle 909207141Sjeff * of a file name. Stop when we reach or pass the expected offset. 910207141Sjeff */ 911207141Sjeff dpoff = (doff / DIRBLKSIZ) * DIRBLKSIZ; 912207141Sjeff do { 913207141Sjeff dp = (struct direct *)&block[dpoff]; 914207141Sjeff if (dpoff == doff) 915207141Sjeff break; 916207141Sjeff if (dp->d_reclen == 0) 917207141Sjeff break; 918207141Sjeff dpoff += dp->d_reclen; 919207141Sjeff } while (dpoff <= doff); 920207141Sjeff if (dpoff > fs->fs_bsize) 921209408Sdelphij err_suj("Corrupt directory block in dir ino %d\n", parent); 922207141Sjeff /* Not found. */ 923207141Sjeff if (dpoff != doff) { 924207141Sjeff if (debug) 925207141Sjeff printf("ino %d not found in %d, lbn %jd, dpoff %d\n", 926207141Sjeff child, parent, lbn, dpoff); 927207141Sjeff return (0); 928207141Sjeff } 929207141Sjeff /* 930207141Sjeff * We found the item in question. Record the mode and whether it's 931207141Sjeff * a . or .. link for the caller. 932207141Sjeff */ 933207141Sjeff if (dp->d_ino == child) { 934207141Sjeff if (child == parent) 935207141Sjeff *isdot = 1; 936207141Sjeff else if (dp->d_namlen == 2 && 937207141Sjeff dp->d_name[0] == '.' && dp->d_name[1] == '.') 938207141Sjeff *isdot = 1; 939207141Sjeff *mode = DTTOIF(dp->d_type); 940207141Sjeff return (1); 941207141Sjeff } 942207141Sjeff if (debug) 943207141Sjeff printf("ino %d doesn't match dirent ino %d in parent %d\n", 944207141Sjeff child, dp->d_ino, parent); 945207141Sjeff return (0); 946207141Sjeff} 947207141Sjeff 948207141Sjeff#define VISIT_INDIR 0x0001 949207141Sjeff#define VISIT_EXT 0x0002 950207141Sjeff#define VISIT_ROOT 0x0004 /* Operation came via root & valid pointers. */ 951207141Sjeff 952207141Sjeff/* 953207141Sjeff * Read an indirect level which may or may not be linked into an inode. 954207141Sjeff */ 955207141Sjeffstatic void 956207141Sjeffindir_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, uint64_t *frags, 957207141Sjeff ino_visitor visitor, int flags) 958207141Sjeff{ 959207141Sjeff ufs2_daddr_t *bap2; 960207141Sjeff ufs1_daddr_t *bap1; 961207141Sjeff ufs_lbn_t lbnadd; 962207141Sjeff ufs2_daddr_t nblk; 963207141Sjeff ufs_lbn_t nlbn; 964207141Sjeff int level; 965207141Sjeff int i; 966207141Sjeff 967207141Sjeff /* 968207141Sjeff * Don't visit indirect blocks with contents we can't trust. This 969207141Sjeff * should only happen when indir_visit() is called to complete a 970207141Sjeff * truncate that never finished and not when a pointer is found via 971207141Sjeff * an inode. 972207141Sjeff */ 973207141Sjeff if (blk == 0) 974207141Sjeff return; 975207141Sjeff level = lbn_level(lbn); 976207141Sjeff if (level == -1) 977209408Sdelphij err_suj("Invalid level for lbn %jd\n", lbn); 978207141Sjeff if ((flags & VISIT_ROOT) == 0 && blk_isindir(blk, ino, lbn) == 0) { 979207141Sjeff if (debug) 980207141Sjeff printf("blk %jd ino %d lbn %jd(%d) is not indir.\n", 981207141Sjeff blk, ino, lbn, level); 982207141Sjeff goto out; 983207141Sjeff } 984207141Sjeff lbnadd = 1; 985207141Sjeff for (i = level; i > 0; i--) 986207141Sjeff lbnadd *= NINDIR(fs); 987207141Sjeff bap1 = (void *)dblk_read(blk, fs->fs_bsize); 988207141Sjeff bap2 = (void *)bap1; 989207141Sjeff for (i = 0; i < NINDIR(fs); i++) { 990207141Sjeff if (fs->fs_magic == FS_UFS1_MAGIC) 991207141Sjeff nblk = *bap1++; 992207141Sjeff else 993207141Sjeff nblk = *bap2++; 994207141Sjeff if (nblk == 0) 995207141Sjeff continue; 996207141Sjeff if (level == 0) { 997207141Sjeff nlbn = -lbn + i * lbnadd; 998207141Sjeff (*frags) += fs->fs_frag; 999207141Sjeff visitor(ino, nlbn, nblk, fs->fs_frag); 1000207141Sjeff } else { 1001207141Sjeff nlbn = (lbn + 1) - (i * lbnadd); 1002207141Sjeff indir_visit(ino, nlbn, nblk, frags, visitor, flags); 1003207141Sjeff } 1004207141Sjeff } 1005207141Sjeffout: 1006207141Sjeff if (flags & VISIT_INDIR) { 1007207141Sjeff (*frags) += fs->fs_frag; 1008207141Sjeff visitor(ino, lbn, blk, fs->fs_frag); 1009207141Sjeff } 1010207141Sjeff} 1011207141Sjeff 1012207141Sjeff/* 1013207141Sjeff * Visit each block in an inode as specified by 'flags' and call a 1014207141Sjeff * callback function. The callback may inspect or free blocks. The 1015207141Sjeff * count of frags found according to the size in the file is returned. 1016207141Sjeff * This is not valid for sparse files but may be used to determine 1017207141Sjeff * the correct di_blocks for a file. 1018207141Sjeff */ 1019207141Sjeffstatic uint64_t 1020207141Sjeffino_visit(union dinode *ip, ino_t ino, ino_visitor visitor, int flags) 1021207141Sjeff{ 1022207141Sjeff ufs_lbn_t nextlbn; 1023207141Sjeff ufs_lbn_t tmpval; 1024207141Sjeff ufs_lbn_t lbn; 1025207141Sjeff uint64_t size; 1026207141Sjeff uint64_t fragcnt; 1027207141Sjeff int mode; 1028207141Sjeff int frags; 1029207141Sjeff int i; 1030207141Sjeff 1031207141Sjeff size = DIP(ip, di_size); 1032207141Sjeff mode = DIP(ip, di_mode) & IFMT; 1033207141Sjeff fragcnt = 0; 1034207141Sjeff if ((flags & VISIT_EXT) && 1035207141Sjeff fs->fs_magic == FS_UFS2_MAGIC && ip->dp2.di_extsize) { 1036207141Sjeff for (i = 0; i < NXADDR; i++) { 1037207141Sjeff if (ip->dp2.di_extb[i] == 0) 1038207141Sjeff continue; 1039207141Sjeff frags = sblksize(fs, ip->dp2.di_extsize, i); 1040207141Sjeff frags = numfrags(fs, frags); 1041207141Sjeff fragcnt += frags; 1042207141Sjeff visitor(ino, -1 - i, ip->dp2.di_extb[i], frags); 1043207141Sjeff } 1044207141Sjeff } 1045207141Sjeff /* Skip datablocks for short links and devices. */ 1046207141Sjeff if (mode == IFBLK || mode == IFCHR || 1047207141Sjeff (mode == IFLNK && size < fs->fs_maxsymlinklen)) 1048207141Sjeff return (fragcnt); 1049207141Sjeff for (i = 0; i < NDADDR; i++) { 1050207141Sjeff if (DIP(ip, di_db[i]) == 0) 1051207141Sjeff continue; 1052207141Sjeff frags = sblksize(fs, size, i); 1053207141Sjeff frags = numfrags(fs, frags); 1054207141Sjeff fragcnt += frags; 1055207141Sjeff visitor(ino, i, DIP(ip, di_db[i]), frags); 1056207141Sjeff } 1057207141Sjeff /* 1058207141Sjeff * We know the following indirects are real as we're following 1059207141Sjeff * real pointers to them. 1060207141Sjeff */ 1061207141Sjeff flags |= VISIT_ROOT; 1062207141Sjeff for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; i++, 1063207141Sjeff lbn = nextlbn) { 1064207141Sjeff nextlbn = lbn + tmpval; 1065207141Sjeff tmpval *= NINDIR(fs); 1066207141Sjeff if (DIP(ip, di_ib[i]) == 0) 1067207141Sjeff continue; 1068207141Sjeff indir_visit(ino, -lbn - i, DIP(ip, di_ib[i]), &fragcnt, visitor, 1069207141Sjeff flags); 1070207141Sjeff } 1071207141Sjeff return (fragcnt); 1072207141Sjeff} 1073207141Sjeff 1074207141Sjeff/* 1075207141Sjeff * Null visitor function used when we just want to count blocks and 1076207141Sjeff * record the lbn. 1077207141Sjeff */ 1078207141Sjeffufs_lbn_t visitlbn; 1079207141Sjeffstatic void 1080207141Sjeffnull_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 1081207141Sjeff{ 1082207141Sjeff if (lbn > 0) 1083207141Sjeff visitlbn = lbn; 1084207141Sjeff} 1085207141Sjeff 1086207141Sjeff/* 1087207141Sjeff * Recalculate di_blocks when we discover that a block allocation or 1088207141Sjeff * free was not successfully completed. The kernel does not roll this back 1089207141Sjeff * because it would be too expensive to compute which indirects were 1090207141Sjeff * reachable at the time the inode was written. 1091207141Sjeff */ 1092207141Sjeffstatic void 1093207141Sjeffino_adjblks(struct suj_ino *sino) 1094207141Sjeff{ 1095207141Sjeff union dinode *ip; 1096207141Sjeff uint64_t blocks; 1097207141Sjeff uint64_t frags; 1098207141Sjeff off_t isize; 1099207141Sjeff off_t size; 1100207141Sjeff ino_t ino; 1101207141Sjeff 1102207141Sjeff ino = sino->si_ino; 1103207141Sjeff ip = ino_read(ino); 1104207141Sjeff /* No need to adjust zero'd inodes. */ 1105207141Sjeff if (DIP(ip, di_mode) == 0) 1106207141Sjeff return; 1107207141Sjeff /* 1108207141Sjeff * Visit all blocks and count them as well as recording the last 1109207141Sjeff * valid lbn in the file. If the file size doesn't agree with the 1110207141Sjeff * last lbn we need to truncate to fix it. Otherwise just adjust 1111207141Sjeff * the blocks count. 1112207141Sjeff */ 1113207141Sjeff visitlbn = 0; 1114207141Sjeff frags = ino_visit(ip, ino, null_visit, VISIT_INDIR | VISIT_EXT); 1115207141Sjeff blocks = fsbtodb(fs, frags); 1116207141Sjeff /* 1117207141Sjeff * We assume the size and direct block list is kept coherent by 1118207141Sjeff * softdep. For files that have extended into indirects we truncate 1119207141Sjeff * to the size in the inode or the maximum size permitted by 1120207141Sjeff * populated indirects. 1121207141Sjeff */ 1122207141Sjeff if (visitlbn >= NDADDR) { 1123207141Sjeff isize = DIP(ip, di_size); 1124207141Sjeff size = lblktosize(fs, visitlbn + 1); 1125207141Sjeff if (isize > size) 1126207141Sjeff isize = size; 1127207141Sjeff /* Always truncate to free any unpopulated indirects. */ 1128207141Sjeff ino_trunc(sino->si_ino, isize); 1129207141Sjeff return; 1130207141Sjeff } 1131207141Sjeff if (blocks == DIP(ip, di_blocks)) 1132207141Sjeff return; 1133207141Sjeff if (debug) 1134207141Sjeff printf("ino %d adjusting block count from %jd to %jd\n", 1135207141Sjeff ino, DIP(ip, di_blocks), blocks); 1136207141Sjeff DIP_SET(ip, di_blocks, blocks); 1137207141Sjeff ino_dirty(ino); 1138207141Sjeff} 1139207141Sjeff 1140207141Sjeffstatic void 1141207141Sjeffblk_free_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 1142207141Sjeff{ 1143207141Sjeff 1144245778Sscottl blk_free(blk, blk_freemask(blk, ino, lbn, frags), frags); 1145207141Sjeff} 1146207141Sjeff 1147207141Sjeff/* 1148207141Sjeff * Free a block or tree of blocks that was previously rooted in ino at 1149207141Sjeff * the given lbn. If the lbn is an indirect all children are freed 1150207141Sjeff * recursively. 1151207141Sjeff */ 1152207141Sjeffstatic void 1153207141Sjeffblk_free_lbn(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags, int follow) 1154207141Sjeff{ 1155207141Sjeff uint64_t resid; 1156207141Sjeff int mask; 1157207141Sjeff 1158207141Sjeff mask = blk_freemask(blk, ino, lbn, frags); 1159207141Sjeff resid = 0; 1160207141Sjeff if (lbn <= -NDADDR && follow && mask == 0) 1161207141Sjeff indir_visit(ino, lbn, blk, &resid, blk_free_visit, VISIT_INDIR); 1162207141Sjeff else 1163207141Sjeff blk_free(blk, mask, frags); 1164207141Sjeff} 1165207141Sjeff 1166207141Sjeffstatic void 1167207141Sjeffino_setskip(struct suj_ino *sino, ino_t parent) 1168207141Sjeff{ 1169207141Sjeff int isdot; 1170207141Sjeff int mode; 1171207141Sjeff 1172207141Sjeff if (ino_isat(sino->si_ino, DOTDOT_OFFSET, parent, &mode, &isdot)) 1173207141Sjeff sino->si_skipparent = 1; 1174207141Sjeff} 1175207141Sjeff 1176209716Sjeffstatic void 1177209716Sjeffino_remref(ino_t parent, ino_t child, uint64_t diroff, int isdotdot) 1178209716Sjeff{ 1179209716Sjeff struct suj_ino *sino; 1180209716Sjeff struct suj_rec *srec; 1181209716Sjeff struct jrefrec *rrec; 1182209716Sjeff 1183209716Sjeff /* 1184209716Sjeff * Lookup this inode to see if we have a record for it. 1185209716Sjeff */ 1186209716Sjeff sino = ino_lookup(child, 0); 1187209716Sjeff /* 1188209716Sjeff * Tell any child directories we've already removed their 1189209716Sjeff * parent link cnt. Don't try to adjust our link down again. 1190209716Sjeff */ 1191209716Sjeff if (sino != NULL && isdotdot == 0) 1192209716Sjeff ino_setskip(sino, parent); 1193209716Sjeff /* 1194209716Sjeff * No valid record for this inode. Just drop the on-disk 1195209716Sjeff * link by one. 1196209716Sjeff */ 1197209716Sjeff if (sino == NULL || sino->si_hasrecs == 0) { 1198209716Sjeff ino_decr(child); 1199209716Sjeff return; 1200209716Sjeff } 1201209716Sjeff /* 1202209716Sjeff * Use ino_adjust() if ino_check() has already processed this 1203209716Sjeff * child. If we lose the last non-dot reference to a 1204209716Sjeff * directory it will be discarded. 1205209716Sjeff */ 1206209716Sjeff if (sino->si_linkadj) { 1207209716Sjeff sino->si_nlink--; 1208209716Sjeff if (isdotdot) 1209209716Sjeff sino->si_dotlinks--; 1210209716Sjeff ino_adjust(sino); 1211209716Sjeff return; 1212209716Sjeff } 1213209716Sjeff /* 1214209716Sjeff * If we haven't yet processed this inode we need to make 1215209716Sjeff * sure we will successfully discover the lost path. If not 1216209716Sjeff * use nlinkadj to remember. 1217209716Sjeff */ 1218209716Sjeff TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 1219209716Sjeff rrec = (struct jrefrec *)srec->sr_rec; 1220209716Sjeff if (rrec->jr_parent == parent && 1221209716Sjeff rrec->jr_diroff == diroff) 1222209716Sjeff return; 1223209716Sjeff } 1224209716Sjeff sino->si_nlinkadj++; 1225209716Sjeff} 1226209716Sjeff 1227207141Sjeff/* 1228207141Sjeff * Free the children of a directory when the directory is discarded. 1229207141Sjeff */ 1230207141Sjeffstatic void 1231207141Sjeffino_free_children(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 1232207141Sjeff{ 1233207141Sjeff struct suj_ino *sino; 1234207141Sjeff struct direct *dp; 1235207141Sjeff off_t diroff; 1236207141Sjeff uint8_t *block; 1237207141Sjeff int skipparent; 1238209716Sjeff int isdotdot; 1239207141Sjeff int dpoff; 1240207141Sjeff int size; 1241207141Sjeff 1242207141Sjeff sino = ino_lookup(ino, 0); 1243207141Sjeff if (sino) 1244207141Sjeff skipparent = sino->si_skipparent; 1245207141Sjeff else 1246207141Sjeff skipparent = 0; 1247207141Sjeff size = lfragtosize(fs, frags); 1248207141Sjeff block = dblk_read(blk, size); 1249207141Sjeff dp = (struct direct *)&block[0]; 1250207141Sjeff for (dpoff = 0; dpoff < size && dp->d_reclen; dpoff += dp->d_reclen) { 1251207141Sjeff dp = (struct direct *)&block[dpoff]; 1252207141Sjeff if (dp->d_ino == 0 || dp->d_ino == WINO) 1253207141Sjeff continue; 1254207141Sjeff if (dp->d_namlen == 1 && dp->d_name[0] == '.') 1255207141Sjeff continue; 1256209716Sjeff isdotdot = dp->d_namlen == 2 && dp->d_name[0] == '.' && 1257207141Sjeff dp->d_name[1] == '.'; 1258209716Sjeff if (isdotdot && skipparent == 1) 1259207141Sjeff continue; 1260207141Sjeff if (debug) 1261207141Sjeff printf("Directory %d removing ino %d name %s\n", 1262207141Sjeff ino, dp->d_ino, dp->d_name); 1263207141Sjeff diroff = lblktosize(fs, lbn) + dpoff; 1264209716Sjeff ino_remref(ino, dp->d_ino, diroff, isdotdot); 1265207141Sjeff } 1266207141Sjeff} 1267207141Sjeff 1268207141Sjeff/* 1269207141Sjeff * Reclaim an inode, freeing all blocks and decrementing all children's 1270207141Sjeff * link counts. Free the inode back to the cg. 1271207141Sjeff */ 1272207141Sjeffstatic void 1273207141Sjeffino_reclaim(union dinode *ip, ino_t ino, int mode) 1274207141Sjeff{ 1275207141Sjeff uint32_t gen; 1276207141Sjeff 1277207141Sjeff if (ino == ROOTINO) 1278209408Sdelphij err_suj("Attempting to free ROOTINO\n"); 1279207141Sjeff if (debug) 1280207141Sjeff printf("Truncating and freeing ino %d, nlink %d, mode %o\n", 1281207141Sjeff ino, DIP(ip, di_nlink), DIP(ip, di_mode)); 1282207141Sjeff 1283207141Sjeff /* We are freeing an inode or directory. */ 1284207141Sjeff if ((DIP(ip, di_mode) & IFMT) == IFDIR) 1285207141Sjeff ino_visit(ip, ino, ino_free_children, 0); 1286207141Sjeff DIP_SET(ip, di_nlink, 0); 1287207141Sjeff ino_visit(ip, ino, blk_free_visit, VISIT_EXT | VISIT_INDIR); 1288207141Sjeff /* Here we have to clear the inode and release any blocks it holds. */ 1289207141Sjeff gen = DIP(ip, di_gen); 1290207141Sjeff if (fs->fs_magic == FS_UFS1_MAGIC) 1291207141Sjeff bzero(ip, sizeof(struct ufs1_dinode)); 1292207141Sjeff else 1293207141Sjeff bzero(ip, sizeof(struct ufs2_dinode)); 1294207141Sjeff DIP_SET(ip, di_gen, gen); 1295207141Sjeff ino_dirty(ino); 1296207141Sjeff ino_free(ino, mode); 1297207141Sjeff return; 1298207141Sjeff} 1299207141Sjeff 1300207141Sjeff/* 1301207141Sjeff * Adjust an inode's link count down by one when a directory goes away. 1302207141Sjeff */ 1303207141Sjeffstatic void 1304207141Sjeffino_decr(ino_t ino) 1305207141Sjeff{ 1306207141Sjeff union dinode *ip; 1307207141Sjeff int reqlink; 1308207141Sjeff int nlink; 1309207141Sjeff int mode; 1310207141Sjeff 1311207141Sjeff ip = ino_read(ino); 1312207141Sjeff nlink = DIP(ip, di_nlink); 1313207141Sjeff mode = DIP(ip, di_mode); 1314207141Sjeff if (nlink < 1) 1315209408Sdelphij err_suj("Inode %d link count %d invalid\n", ino, nlink); 1316207141Sjeff if (mode == 0) 1317209408Sdelphij err_suj("Inode %d has a link of %d with 0 mode\n", ino, nlink); 1318207141Sjeff nlink--; 1319207141Sjeff if ((mode & IFMT) == IFDIR) 1320207141Sjeff reqlink = 2; 1321207141Sjeff else 1322207141Sjeff reqlink = 1; 1323207141Sjeff if (nlink < reqlink) { 1324207141Sjeff if (debug) 1325207141Sjeff printf("ino %d not enough links to live %d < %d\n", 1326207141Sjeff ino, nlink, reqlink); 1327207141Sjeff ino_reclaim(ip, ino, mode); 1328207141Sjeff return; 1329207141Sjeff } 1330207141Sjeff DIP_SET(ip, di_nlink, nlink); 1331207141Sjeff ino_dirty(ino); 1332207141Sjeff} 1333207141Sjeff 1334207141Sjeff/* 1335207141Sjeff * Adjust the inode link count to 'nlink'. If the count reaches zero 1336207141Sjeff * free it. 1337207141Sjeff */ 1338207141Sjeffstatic void 1339207141Sjeffino_adjust(struct suj_ino *sino) 1340207141Sjeff{ 1341207141Sjeff struct jrefrec *rrec; 1342207141Sjeff struct suj_rec *srec; 1343207141Sjeff struct suj_ino *stmp; 1344207141Sjeff union dinode *ip; 1345207141Sjeff nlink_t nlink; 1346209716Sjeff int recmode; 1347207141Sjeff int reqlink; 1348209716Sjeff int isdot; 1349207141Sjeff int mode; 1350207141Sjeff ino_t ino; 1351207141Sjeff 1352207141Sjeff nlink = sino->si_nlink; 1353207141Sjeff ino = sino->si_ino; 1354209716Sjeff mode = sino->si_mode & IFMT; 1355207141Sjeff /* 1356209716Sjeff * If it's a directory with no dot links, it was truncated before 1357209716Sjeff * the name was cleared. We need to clear the dirent that 1358209716Sjeff * points at it. 1359209716Sjeff */ 1360209716Sjeff if (mode == IFDIR && nlink == 1 && sino->si_dotlinks == 0) { 1361209716Sjeff sino->si_nlink = nlink = 0; 1362209716Sjeff TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 1363209716Sjeff rrec = (struct jrefrec *)srec->sr_rec; 1364209716Sjeff if (ino_isat(rrec->jr_parent, rrec->jr_diroff, ino, 1365209716Sjeff &recmode, &isdot) == 0) 1366209716Sjeff continue; 1367209716Sjeff ino_clrat(rrec->jr_parent, rrec->jr_diroff, ino); 1368209716Sjeff break; 1369209716Sjeff } 1370209716Sjeff if (srec == NULL) 1371209716Sjeff errx(1, "Directory %d name not found", ino); 1372209716Sjeff } 1373209716Sjeff /* 1374207141Sjeff * If it's a directory with no real names pointing to it go ahead 1375207141Sjeff * and truncate it. This will free any children. 1376207141Sjeff */ 1377209716Sjeff if (mode == IFDIR && nlink - sino->si_dotlinks == 0) { 1378207141Sjeff sino->si_nlink = nlink = 0; 1379207141Sjeff /* 1380207141Sjeff * Mark any .. links so they know not to free this inode 1381207141Sjeff * when they are removed. 1382207141Sjeff */ 1383207141Sjeff TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 1384207141Sjeff rrec = (struct jrefrec *)srec->sr_rec; 1385207141Sjeff if (rrec->jr_diroff == DOTDOT_OFFSET) { 1386207141Sjeff stmp = ino_lookup(rrec->jr_parent, 0); 1387207141Sjeff if (stmp) 1388207141Sjeff ino_setskip(stmp, ino); 1389207141Sjeff } 1390207141Sjeff } 1391207141Sjeff } 1392207141Sjeff ip = ino_read(ino); 1393207141Sjeff mode = DIP(ip, di_mode) & IFMT; 1394207141Sjeff if (nlink > LINK_MAX) 1395209408Sdelphij err_suj( 1396209408Sdelphij "ino %d nlink manipulation error, new link %d, old link %d\n", 1397207141Sjeff ino, nlink, DIP(ip, di_nlink)); 1398207141Sjeff if (debug) 1399207141Sjeff printf("Adjusting ino %d, nlink %d, old link %d lastmode %o\n", 1400207141Sjeff ino, nlink, DIP(ip, di_nlink), sino->si_mode); 1401207141Sjeff if (mode == 0) { 1402207141Sjeff if (debug) 1403207141Sjeff printf("ino %d, zero inode freeing bitmap\n", ino); 1404207141Sjeff ino_free(ino, sino->si_mode); 1405207141Sjeff return; 1406207141Sjeff } 1407207141Sjeff /* XXX Should be an assert? */ 1408207141Sjeff if (mode != sino->si_mode && debug) 1409207141Sjeff printf("ino %d, mode %o != %o\n", ino, mode, sino->si_mode); 1410207141Sjeff if ((mode & IFMT) == IFDIR) 1411207141Sjeff reqlink = 2; 1412207141Sjeff else 1413207141Sjeff reqlink = 1; 1414207141Sjeff /* If the inode doesn't have enough links to live, free it. */ 1415207141Sjeff if (nlink < reqlink) { 1416207141Sjeff if (debug) 1417207141Sjeff printf("ino %d not enough links to live %d < %d\n", 1418207141Sjeff ino, nlink, reqlink); 1419207141Sjeff ino_reclaim(ip, ino, mode); 1420207141Sjeff return; 1421207141Sjeff } 1422207141Sjeff /* If required write the updated link count. */ 1423207141Sjeff if (DIP(ip, di_nlink) == nlink) { 1424207141Sjeff if (debug) 1425207141Sjeff printf("ino %d, link matches, skipping.\n", ino); 1426207141Sjeff return; 1427207141Sjeff } 1428207141Sjeff DIP_SET(ip, di_nlink, nlink); 1429207141Sjeff ino_dirty(ino); 1430207141Sjeff} 1431207141Sjeff 1432207141Sjeff/* 1433207141Sjeff * Truncate some or all blocks in an indirect, freeing any that are required 1434207141Sjeff * and zeroing the indirect. 1435207141Sjeff */ 1436207141Sjeffstatic void 1437207141Sjeffindir_trunc(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, ufs_lbn_t lastlbn) 1438207141Sjeff{ 1439207141Sjeff ufs2_daddr_t *bap2; 1440207141Sjeff ufs1_daddr_t *bap1; 1441207141Sjeff ufs_lbn_t lbnadd; 1442207141Sjeff ufs2_daddr_t nblk; 1443207141Sjeff ufs_lbn_t next; 1444207141Sjeff ufs_lbn_t nlbn; 1445207141Sjeff int dirty; 1446207141Sjeff int level; 1447207141Sjeff int i; 1448207141Sjeff 1449207141Sjeff if (blk == 0) 1450207141Sjeff return; 1451207141Sjeff dirty = 0; 1452207141Sjeff level = lbn_level(lbn); 1453207141Sjeff if (level == -1) 1454209408Sdelphij err_suj("Invalid level for lbn %jd\n", lbn); 1455207141Sjeff lbnadd = 1; 1456207141Sjeff for (i = level; i > 0; i--) 1457207141Sjeff lbnadd *= NINDIR(fs); 1458207141Sjeff bap1 = (void *)dblk_read(blk, fs->fs_bsize); 1459207141Sjeff bap2 = (void *)bap1; 1460207141Sjeff for (i = 0; i < NINDIR(fs); i++) { 1461207141Sjeff if (fs->fs_magic == FS_UFS1_MAGIC) 1462207141Sjeff nblk = *bap1++; 1463207141Sjeff else 1464207141Sjeff nblk = *bap2++; 1465207141Sjeff if (nblk == 0) 1466207141Sjeff continue; 1467207141Sjeff if (level != 0) { 1468207141Sjeff nlbn = (lbn + 1) - (i * lbnadd); 1469207141Sjeff /* 1470207141Sjeff * Calculate the lbn of the next indirect to 1471207141Sjeff * determine if any of this indirect must be 1472207141Sjeff * reclaimed. 1473207141Sjeff */ 1474207141Sjeff next = -(lbn + level) + ((i+1) * lbnadd); 1475207141Sjeff if (next <= lastlbn) 1476207141Sjeff continue; 1477207141Sjeff indir_trunc(ino, nlbn, nblk, lastlbn); 1478207141Sjeff /* If all of this indirect was reclaimed, free it. */ 1479207141Sjeff nlbn = next - lbnadd; 1480207141Sjeff if (nlbn < lastlbn) 1481207141Sjeff continue; 1482207141Sjeff } else { 1483207141Sjeff nlbn = -lbn + i * lbnadd; 1484207141Sjeff if (nlbn < lastlbn) 1485207141Sjeff continue; 1486207141Sjeff } 1487207141Sjeff dirty = 1; 1488207141Sjeff blk_free(nblk, 0, fs->fs_frag); 1489207141Sjeff if (fs->fs_magic == FS_UFS1_MAGIC) 1490207141Sjeff *(bap1 - 1) = 0; 1491207141Sjeff else 1492207141Sjeff *(bap2 - 1) = 0; 1493207141Sjeff } 1494207141Sjeff if (dirty) 1495207141Sjeff dblk_dirty(blk); 1496207141Sjeff} 1497207141Sjeff 1498207141Sjeff/* 1499207141Sjeff * Truncate an inode to the minimum of the given size or the last populated 1500207141Sjeff * block after any over size have been discarded. The kernel would allocate 1501207141Sjeff * the last block in the file but fsck does not and neither do we. This 1502207141Sjeff * code never extends files, only shrinks them. 1503207141Sjeff */ 1504207141Sjeffstatic void 1505207141Sjeffino_trunc(ino_t ino, off_t size) 1506207141Sjeff{ 1507207141Sjeff union dinode *ip; 1508207141Sjeff ufs2_daddr_t bn; 1509207141Sjeff uint64_t totalfrags; 1510207141Sjeff ufs_lbn_t nextlbn; 1511207141Sjeff ufs_lbn_t lastlbn; 1512207141Sjeff ufs_lbn_t tmpval; 1513207141Sjeff ufs_lbn_t lbn; 1514207141Sjeff ufs_lbn_t i; 1515207141Sjeff int frags; 1516207141Sjeff off_t cursize; 1517207141Sjeff off_t off; 1518207141Sjeff int mode; 1519207141Sjeff 1520207141Sjeff ip = ino_read(ino); 1521207141Sjeff mode = DIP(ip, di_mode) & IFMT; 1522207141Sjeff cursize = DIP(ip, di_size); 1523207141Sjeff if (debug) 1524207141Sjeff printf("Truncating ino %d, mode %o to size %jd from size %jd\n", 1525207141Sjeff ino, mode, size, cursize); 1526207141Sjeff 1527207141Sjeff /* Skip datablocks for short links and devices. */ 1528207141Sjeff if (mode == 0 || mode == IFBLK || mode == IFCHR || 1529207141Sjeff (mode == IFLNK && cursize < fs->fs_maxsymlinklen)) 1530207141Sjeff return; 1531207141Sjeff /* Don't extend. */ 1532207141Sjeff if (size > cursize) 1533207141Sjeff size = cursize; 1534207141Sjeff lastlbn = lblkno(fs, blkroundup(fs, size)); 1535207141Sjeff for (i = lastlbn; i < NDADDR; i++) { 1536207141Sjeff if (DIP(ip, di_db[i]) == 0) 1537207141Sjeff continue; 1538207141Sjeff frags = sblksize(fs, cursize, i); 1539207141Sjeff frags = numfrags(fs, frags); 1540207141Sjeff blk_free(DIP(ip, di_db[i]), 0, frags); 1541207141Sjeff DIP_SET(ip, di_db[i], 0); 1542207141Sjeff } 1543207141Sjeff /* 1544207141Sjeff * Follow indirect blocks, freeing anything required. 1545207141Sjeff */ 1546207141Sjeff for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; i++, 1547207141Sjeff lbn = nextlbn) { 1548207141Sjeff nextlbn = lbn + tmpval; 1549207141Sjeff tmpval *= NINDIR(fs); 1550207141Sjeff /* If we're not freeing any in this indirect range skip it. */ 1551207141Sjeff if (lastlbn >= nextlbn) 1552207141Sjeff continue; 1553207141Sjeff if (DIP(ip, di_ib[i]) == 0) 1554207141Sjeff continue; 1555207141Sjeff indir_trunc(ino, -lbn - i, DIP(ip, di_ib[i]), lastlbn); 1556207141Sjeff /* If we freed everything in this indirect free the indir. */ 1557207141Sjeff if (lastlbn > lbn) 1558207141Sjeff continue; 1559207141Sjeff blk_free(DIP(ip, di_ib[i]), 0, frags); 1560207141Sjeff DIP_SET(ip, di_ib[i], 0); 1561207141Sjeff } 1562207141Sjeff ino_dirty(ino); 1563207141Sjeff /* 1564207141Sjeff * Now that we've freed any whole blocks that exceed the desired 1565207141Sjeff * truncation size, figure out how many blocks remain and what the 1566207141Sjeff * last populated lbn is. We will set the size to this last lbn 1567207141Sjeff * rather than worrying about allocating the final lbn as the kernel 1568207141Sjeff * would've done. This is consistent with normal fsck behavior. 1569221110Sdes */ 1570207141Sjeff visitlbn = 0; 1571207141Sjeff totalfrags = ino_visit(ip, ino, null_visit, VISIT_INDIR | VISIT_EXT); 1572207141Sjeff if (size > lblktosize(fs, visitlbn + 1)) 1573207141Sjeff size = lblktosize(fs, visitlbn + 1); 1574207141Sjeff /* 1575207141Sjeff * If we're truncating direct blocks we have to adjust frags 1576207141Sjeff * accordingly. 1577207141Sjeff */ 1578207141Sjeff if (visitlbn < NDADDR && totalfrags) { 1579207141Sjeff long oldspace, newspace; 1580207141Sjeff 1581207141Sjeff bn = DIP(ip, di_db[visitlbn]); 1582207141Sjeff if (bn == 0) 1583209408Sdelphij err_suj("Bad blk at ino %d lbn %jd\n", ino, visitlbn); 1584207141Sjeff oldspace = sblksize(fs, cursize, visitlbn); 1585207141Sjeff newspace = sblksize(fs, size, visitlbn); 1586207141Sjeff if (oldspace != newspace) { 1587207141Sjeff bn += numfrags(fs, newspace); 1588207141Sjeff frags = numfrags(fs, oldspace - newspace); 1589207141Sjeff blk_free(bn, 0, frags); 1590207141Sjeff totalfrags -= frags; 1591207141Sjeff } 1592207141Sjeff } 1593207141Sjeff DIP_SET(ip, di_blocks, fsbtodb(fs, totalfrags)); 1594207141Sjeff DIP_SET(ip, di_size, size); 1595207141Sjeff /* 1596207141Sjeff * If we've truncated into the middle of a block or frag we have 1597207141Sjeff * to zero it here. Otherwise the file could extend into 1598207141Sjeff * uninitialized space later. 1599207141Sjeff */ 1600207141Sjeff off = blkoff(fs, size); 1601222958Sjeff if (off && DIP(ip, di_mode) != IFDIR) { 1602207141Sjeff uint8_t *buf; 1603207141Sjeff long clrsize; 1604207141Sjeff 1605207141Sjeff bn = ino_blkatoff(ip, ino, visitlbn, &frags); 1606207141Sjeff if (bn == 0) 1607209408Sdelphij err_suj("Block missing from ino %d at lbn %jd\n", 1608207141Sjeff ino, visitlbn); 1609207141Sjeff clrsize = frags * fs->fs_fsize; 1610207141Sjeff buf = dblk_read(bn, clrsize); 1611207141Sjeff clrsize -= off; 1612207141Sjeff buf += off; 1613207141Sjeff bzero(buf, clrsize); 1614207141Sjeff dblk_dirty(bn); 1615207141Sjeff } 1616207141Sjeff return; 1617207141Sjeff} 1618207141Sjeff 1619207141Sjeff/* 1620207141Sjeff * Process records available for one inode and determine whether the 1621207141Sjeff * link count is correct or needs adjusting. 1622207141Sjeff */ 1623207141Sjeffstatic void 1624207141Sjeffino_check(struct suj_ino *sino) 1625207141Sjeff{ 1626207141Sjeff struct suj_rec *srec; 1627207141Sjeff struct jrefrec *rrec; 1628207141Sjeff nlink_t dotlinks; 1629207141Sjeff int newlinks; 1630207141Sjeff int removes; 1631207141Sjeff int nlink; 1632207141Sjeff ino_t ino; 1633207141Sjeff int isdot; 1634207141Sjeff int isat; 1635207141Sjeff int mode; 1636207141Sjeff 1637207141Sjeff if (sino->si_hasrecs == 0) 1638207141Sjeff return; 1639207141Sjeff ino = sino->si_ino; 1640207141Sjeff rrec = (struct jrefrec *)TAILQ_FIRST(&sino->si_recs)->sr_rec; 1641207141Sjeff nlink = rrec->jr_nlink; 1642207141Sjeff newlinks = 0; 1643207141Sjeff dotlinks = 0; 1644207141Sjeff removes = sino->si_nlinkadj; 1645207141Sjeff TAILQ_FOREACH(srec, &sino->si_recs, sr_next) { 1646207141Sjeff rrec = (struct jrefrec *)srec->sr_rec; 1647221110Sdes isat = ino_isat(rrec->jr_parent, rrec->jr_diroff, 1648207141Sjeff rrec->jr_ino, &mode, &isdot); 1649207141Sjeff if (isat && (mode & IFMT) != (rrec->jr_mode & IFMT)) 1650209408Sdelphij err_suj("Inode mode/directory type mismatch %o != %o\n", 1651207141Sjeff mode, rrec->jr_mode); 1652207141Sjeff if (debug) 1653207141Sjeff printf("jrefrec: op %d ino %d, nlink %d, parent %d, " 1654207141Sjeff "diroff %jd, mode %o, isat %d, isdot %d\n", 1655207141Sjeff rrec->jr_op, rrec->jr_ino, rrec->jr_nlink, 1656207141Sjeff rrec->jr_parent, rrec->jr_diroff, rrec->jr_mode, 1657207141Sjeff isat, isdot); 1658207141Sjeff mode = rrec->jr_mode & IFMT; 1659207141Sjeff if (rrec->jr_op == JOP_REMREF) 1660207141Sjeff removes++; 1661207141Sjeff newlinks += isat; 1662207141Sjeff if (isdot) 1663207141Sjeff dotlinks += isat; 1664207141Sjeff } 1665207141Sjeff /* 1666207141Sjeff * The number of links that remain are the starting link count 1667207141Sjeff * subtracted by the total number of removes with the total 1668207141Sjeff * links discovered back in. An incomplete remove thus 1669207141Sjeff * makes no change to the link count but an add increases 1670207141Sjeff * by one. 1671207141Sjeff */ 1672207141Sjeff if (debug) 1673207141Sjeff printf("ino %d nlink %d newlinks %d removes %d dotlinks %d\n", 1674207141Sjeff ino, nlink, newlinks, removes, dotlinks); 1675207141Sjeff nlink += newlinks; 1676207141Sjeff nlink -= removes; 1677207141Sjeff sino->si_linkadj = 1; 1678207141Sjeff sino->si_nlink = nlink; 1679207141Sjeff sino->si_dotlinks = dotlinks; 1680207141Sjeff sino->si_mode = mode; 1681207141Sjeff ino_adjust(sino); 1682207141Sjeff} 1683207141Sjeff 1684207141Sjeff/* 1685207141Sjeff * Process records available for one block and determine whether it is 1686207141Sjeff * still allocated and whether the owning inode needs to be updated or 1687207141Sjeff * a free completed. 1688207141Sjeff */ 1689207141Sjeffstatic void 1690207141Sjeffblk_check(struct suj_blk *sblk) 1691207141Sjeff{ 1692207141Sjeff struct suj_rec *srec; 1693207141Sjeff struct jblkrec *brec; 1694207141Sjeff struct suj_ino *sino; 1695207141Sjeff ufs2_daddr_t blk; 1696207141Sjeff int mask; 1697207141Sjeff int frags; 1698207141Sjeff int isat; 1699207141Sjeff 1700207141Sjeff /* 1701207141Sjeff * Each suj_blk actually contains records for any fragments in that 1702207141Sjeff * block. As a result we must evaluate each record individually. 1703207141Sjeff */ 1704207141Sjeff sino = NULL; 1705207141Sjeff TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) { 1706207141Sjeff brec = (struct jblkrec *)srec->sr_rec; 1707207141Sjeff frags = brec->jb_frags; 1708207141Sjeff blk = brec->jb_blkno + brec->jb_oldfrags; 1709207141Sjeff isat = blk_isat(brec->jb_ino, brec->jb_lbn, blk, &frags); 1710207141Sjeff if (sino == NULL || sino->si_ino != brec->jb_ino) { 1711207141Sjeff sino = ino_lookup(brec->jb_ino, 1); 1712207141Sjeff sino->si_blkadj = 1; 1713207141Sjeff } 1714207141Sjeff if (debug) 1715207141Sjeff printf("op %d blk %jd ino %d lbn %jd frags %d isat %d (%d)\n", 1716207141Sjeff brec->jb_op, blk, brec->jb_ino, brec->jb_lbn, 1717207141Sjeff brec->jb_frags, isat, frags); 1718207141Sjeff /* 1719207141Sjeff * If we found the block at this address we still have to 1720207141Sjeff * determine if we need to free the tail end that was 1721207141Sjeff * added by adding contiguous fragments from the same block. 1722207141Sjeff */ 1723207141Sjeff if (isat == 1) { 1724207141Sjeff if (frags == brec->jb_frags) 1725207141Sjeff continue; 1726207141Sjeff mask = blk_freemask(blk, brec->jb_ino, brec->jb_lbn, 1727207141Sjeff brec->jb_frags); 1728207141Sjeff mask >>= frags; 1729207141Sjeff blk += frags; 1730207141Sjeff frags = brec->jb_frags - frags; 1731207141Sjeff blk_free(blk, mask, frags); 1732207141Sjeff continue; 1733207141Sjeff } 1734207141Sjeff /* 1735207141Sjeff * The block wasn't found, attempt to free it. It won't be 1736207141Sjeff * freed if it was actually reallocated. If this was an 1737207141Sjeff * allocation we don't want to follow indirects as they 1738207141Sjeff * may not be written yet. Any children of the indirect will 1739207141Sjeff * have their own records. If it's a free we need to 1740207141Sjeff * recursively free children. 1741207141Sjeff */ 1742207141Sjeff blk_free_lbn(blk, brec->jb_ino, brec->jb_lbn, brec->jb_frags, 1743207141Sjeff brec->jb_op == JOP_FREEBLK); 1744207141Sjeff } 1745207141Sjeff} 1746207141Sjeff 1747207141Sjeff/* 1748207141Sjeff * Walk the list of inode records for this cg and resolve moved and duplicate 1749207141Sjeff * inode references now that we have a complete picture. 1750207141Sjeff */ 1751207141Sjeffstatic void 1752207141Sjeffcg_build(struct suj_cg *sc) 1753207141Sjeff{ 1754207141Sjeff struct suj_ino *sino; 1755207141Sjeff int i; 1756207141Sjeff 1757207141Sjeff for (i = 0; i < SUJ_HASHSIZE; i++) 1758207141Sjeff LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) 1759207141Sjeff ino_build(sino); 1760207141Sjeff} 1761207141Sjeff 1762207141Sjeff/* 1763207141Sjeff * Handle inodes requiring truncation. This must be done prior to 1764207141Sjeff * looking up any inodes in directories. 1765207141Sjeff */ 1766207141Sjeffstatic void 1767207141Sjeffcg_trunc(struct suj_cg *sc) 1768207141Sjeff{ 1769207141Sjeff struct suj_ino *sino; 1770207141Sjeff int i; 1771207141Sjeff 1772222958Sjeff for (i = 0; i < SUJ_HASHSIZE; i++) { 1773222958Sjeff LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) { 1774207141Sjeff if (sino->si_trunc) { 1775207141Sjeff ino_trunc(sino->si_ino, 1776207141Sjeff sino->si_trunc->jt_size); 1777222958Sjeff sino->si_blkadj = 0; 1778207141Sjeff sino->si_trunc = NULL; 1779207141Sjeff } 1780222958Sjeff if (sino->si_blkadj) 1781222958Sjeff ino_adjblks(sino); 1782222958Sjeff } 1783222958Sjeff } 1784207141Sjeff} 1785207141Sjeff 1786237265Skibstatic void 1787237265Skibcg_adj_blk(struct suj_cg *sc) 1788237265Skib{ 1789237265Skib struct suj_ino *sino; 1790237265Skib int i; 1791237265Skib 1792237265Skib for (i = 0; i < SUJ_HASHSIZE; i++) { 1793237265Skib LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) { 1794237265Skib if (sino->si_blkadj) 1795237265Skib ino_adjblks(sino); 1796237265Skib } 1797237265Skib } 1798237265Skib} 1799237265Skib 1800207141Sjeff/* 1801207141Sjeff * Free any partially allocated blocks and then resolve inode block 1802207141Sjeff * counts. 1803207141Sjeff */ 1804207141Sjeffstatic void 1805207141Sjeffcg_check_blk(struct suj_cg *sc) 1806207141Sjeff{ 1807207141Sjeff struct suj_blk *sblk; 1808207141Sjeff int i; 1809207141Sjeff 1810207141Sjeff 1811207141Sjeff for (i = 0; i < SUJ_HASHSIZE; i++) 1812207141Sjeff LIST_FOREACH(sblk, &sc->sc_blkhash[i], sb_next) 1813207141Sjeff blk_check(sblk); 1814207141Sjeff} 1815207141Sjeff 1816207141Sjeff/* 1817207141Sjeff * Walk the list of inode records for this cg, recovering any 1818207141Sjeff * changes which were not complete at the time of crash. 1819207141Sjeff */ 1820207141Sjeffstatic void 1821207141Sjeffcg_check_ino(struct suj_cg *sc) 1822207141Sjeff{ 1823207141Sjeff struct suj_ino *sino; 1824207141Sjeff int i; 1825207141Sjeff 1826207141Sjeff for (i = 0; i < SUJ_HASHSIZE; i++) 1827207141Sjeff LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) 1828207141Sjeff ino_check(sino); 1829207141Sjeff} 1830207141Sjeff 1831207141Sjeff/* 1832207141Sjeff * Write a potentially dirty cg. Recalculate the summary information and 1833207141Sjeff * update the superblock summary. 1834207141Sjeff */ 1835207141Sjeffstatic void 1836207141Sjeffcg_write(struct suj_cg *sc) 1837207141Sjeff{ 1838207141Sjeff ufs1_daddr_t fragno, cgbno, maxbno; 1839207141Sjeff u_int8_t *blksfree; 1840207141Sjeff struct cg *cgp; 1841207141Sjeff int blk; 1842207141Sjeff int i; 1843207141Sjeff 1844207141Sjeff if (sc->sc_dirty == 0) 1845207141Sjeff return; 1846207141Sjeff /* 1847207141Sjeff * Fix the frag and cluster summary. 1848207141Sjeff */ 1849207141Sjeff cgp = sc->sc_cgp; 1850207141Sjeff cgp->cg_cs.cs_nbfree = 0; 1851207141Sjeff cgp->cg_cs.cs_nffree = 0; 1852207141Sjeff bzero(&cgp->cg_frsum, sizeof(cgp->cg_frsum)); 1853207141Sjeff maxbno = fragstoblks(fs, fs->fs_fpg); 1854207141Sjeff if (fs->fs_contigsumsize > 0) { 1855207141Sjeff for (i = 1; i <= fs->fs_contigsumsize; i++) 1856207141Sjeff cg_clustersum(cgp)[i] = 0; 1857207141Sjeff bzero(cg_clustersfree(cgp), howmany(maxbno, CHAR_BIT)); 1858207141Sjeff } 1859207141Sjeff blksfree = cg_blksfree(cgp); 1860207141Sjeff for (cgbno = 0; cgbno < maxbno; cgbno++) { 1861207141Sjeff if (ffs_isfreeblock(fs, blksfree, cgbno)) 1862207141Sjeff continue; 1863207141Sjeff if (ffs_isblock(fs, blksfree, cgbno)) { 1864207141Sjeff ffs_clusteracct(fs, cgp, cgbno, 1); 1865207141Sjeff cgp->cg_cs.cs_nbfree++; 1866207141Sjeff continue; 1867207141Sjeff } 1868207141Sjeff fragno = blkstofrags(fs, cgbno); 1869207141Sjeff blk = blkmap(fs, blksfree, fragno); 1870207141Sjeff ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 1871207141Sjeff for (i = 0; i < fs->fs_frag; i++) 1872207141Sjeff if (isset(blksfree, fragno + i)) 1873207141Sjeff cgp->cg_cs.cs_nffree++; 1874207141Sjeff } 1875207141Sjeff /* 1876207141Sjeff * Update the superblock cg summary from our now correct values 1877207141Sjeff * before writing the block. 1878207141Sjeff */ 1879207141Sjeff fs->fs_cs(fs, sc->sc_cgx) = cgp->cg_cs; 1880207141Sjeff if (bwrite(disk, fsbtodb(fs, cgtod(fs, sc->sc_cgx)), sc->sc_cgbuf, 1881207141Sjeff fs->fs_bsize) == -1) 1882209408Sdelphij err_suj("Unable to write cylinder group %d\n", sc->sc_cgx); 1883207141Sjeff} 1884207141Sjeff 1885207141Sjeff/* 1886207141Sjeff * Write out any modified inodes. 1887207141Sjeff */ 1888207141Sjeffstatic void 1889207141Sjeffcg_write_inos(struct suj_cg *sc) 1890207141Sjeff{ 1891207141Sjeff struct ino_blk *iblk; 1892207141Sjeff int i; 1893207141Sjeff 1894207141Sjeff for (i = 0; i < SUJ_HASHSIZE; i++) 1895207141Sjeff LIST_FOREACH(iblk, &sc->sc_iblkhash[i], ib_next) 1896207141Sjeff if (iblk->ib_dirty) 1897207141Sjeff iblk_write(iblk); 1898207141Sjeff} 1899207141Sjeff 1900207141Sjeffstatic void 1901207141Sjeffcg_apply(void (*apply)(struct suj_cg *)) 1902207141Sjeff{ 1903207141Sjeff struct suj_cg *scg; 1904207141Sjeff int i; 1905207141Sjeff 1906207141Sjeff for (i = 0; i < SUJ_HASHSIZE; i++) 1907207141Sjeff LIST_FOREACH(scg, &cghash[i], sc_next) 1908207141Sjeff apply(scg); 1909207141Sjeff} 1910207141Sjeff 1911207141Sjeff/* 1912207141Sjeff * Process the unlinked but referenced file list. Freeing all inodes. 1913207141Sjeff */ 1914207141Sjeffstatic void 1915207141Sjeffino_unlinked(void) 1916207141Sjeff{ 1917207141Sjeff union dinode *ip; 1918207141Sjeff uint16_t mode; 1919207141Sjeff ino_t inon; 1920207141Sjeff ino_t ino; 1921207141Sjeff 1922207141Sjeff ino = fs->fs_sujfree; 1923207141Sjeff fs->fs_sujfree = 0; 1924207141Sjeff while (ino != 0) { 1925207141Sjeff ip = ino_read(ino); 1926207141Sjeff mode = DIP(ip, di_mode) & IFMT; 1927207141Sjeff inon = DIP(ip, di_freelink); 1928207141Sjeff DIP_SET(ip, di_freelink, 0); 1929207141Sjeff /* 1930207141Sjeff * XXX Should this be an errx? 1931207141Sjeff */ 1932207141Sjeff if (DIP(ip, di_nlink) == 0) { 1933207141Sjeff if (debug) 1934207141Sjeff printf("Freeing unlinked ino %d mode %o\n", 1935207141Sjeff ino, mode); 1936207141Sjeff ino_reclaim(ip, ino, mode); 1937207141Sjeff } else if (debug) 1938207141Sjeff printf("Skipping ino %d mode %o with link %d\n", 1939207141Sjeff ino, mode, DIP(ip, di_nlink)); 1940207141Sjeff ino = inon; 1941207141Sjeff } 1942207141Sjeff} 1943207141Sjeff 1944207141Sjeff/* 1945207141Sjeff * Append a new record to the list of records requiring processing. 1946207141Sjeff */ 1947207141Sjeffstatic void 1948207141Sjeffino_append(union jrec *rec) 1949207141Sjeff{ 1950207141Sjeff struct jrefrec *refrec; 1951207141Sjeff struct jmvrec *mvrec; 1952207141Sjeff struct suj_ino *sino; 1953207141Sjeff struct suj_rec *srec; 1954207141Sjeff 1955207141Sjeff mvrec = &rec->rec_jmvrec; 1956207141Sjeff refrec = &rec->rec_jrefrec; 1957207141Sjeff if (debug && mvrec->jm_op == JOP_MVREF) 1958221110Sdes printf("ino move: ino %d, parent %d, diroff %jd, oldoff %jd\n", 1959207141Sjeff mvrec->jm_ino, mvrec->jm_parent, mvrec->jm_newoff, 1960207141Sjeff mvrec->jm_oldoff); 1961207141Sjeff else if (debug && 1962207141Sjeff (refrec->jr_op == JOP_ADDREF || refrec->jr_op == JOP_REMREF)) 1963207141Sjeff printf("ino ref: op %d, ino %d, nlink %d, " 1964221110Sdes "parent %d, diroff %jd\n", 1965207141Sjeff refrec->jr_op, refrec->jr_ino, refrec->jr_nlink, 1966207141Sjeff refrec->jr_parent, refrec->jr_diroff); 1967207141Sjeff sino = ino_lookup(((struct jrefrec *)rec)->jr_ino, 1); 1968207141Sjeff sino->si_hasrecs = 1; 1969207141Sjeff srec = errmalloc(sizeof(*srec)); 1970207141Sjeff srec->sr_rec = rec; 1971207141Sjeff TAILQ_INSERT_TAIL(&sino->si_newrecs, srec, sr_next); 1972207141Sjeff} 1973207141Sjeff 1974207141Sjeff/* 1975207141Sjeff * Add a reference adjustment to the sino list and eliminate dups. The 1976207141Sjeff * primary loop in ino_build_ref() checks for dups but new ones may be 1977207141Sjeff * created as a result of offset adjustments. 1978207141Sjeff */ 1979207141Sjeffstatic void 1980207141Sjeffino_add_ref(struct suj_ino *sino, struct suj_rec *srec) 1981207141Sjeff{ 1982207141Sjeff struct jrefrec *refrec; 1983207141Sjeff struct suj_rec *srn; 1984207141Sjeff struct jrefrec *rrn; 1985207141Sjeff 1986207141Sjeff refrec = (struct jrefrec *)srec->sr_rec; 1987207141Sjeff /* 1988207141Sjeff * We walk backwards so that the oldest link count is preserved. If 1989207141Sjeff * an add record conflicts with a remove keep the remove. Redundant 1990207141Sjeff * removes are eliminated in ino_build_ref. Otherwise we keep the 1991207141Sjeff * oldest record at a given location. 1992207141Sjeff */ 1993207141Sjeff for (srn = TAILQ_LAST(&sino->si_recs, srechd); srn; 1994207141Sjeff srn = TAILQ_PREV(srn, srechd, sr_next)) { 1995207141Sjeff rrn = (struct jrefrec *)srn->sr_rec; 1996207141Sjeff if (rrn->jr_parent != refrec->jr_parent || 1997207141Sjeff rrn->jr_diroff != refrec->jr_diroff) 1998207141Sjeff continue; 1999207141Sjeff if (rrn->jr_op == JOP_REMREF || refrec->jr_op == JOP_ADDREF) { 2000207141Sjeff rrn->jr_mode = refrec->jr_mode; 2001207141Sjeff return; 2002207141Sjeff } 2003207141Sjeff /* 2004207141Sjeff * Adding a remove. 2005207141Sjeff * 2006207141Sjeff * Replace the record in place with the old nlink in case 2007207141Sjeff * we replace the head of the list. Abandon srec as a dup. 2008207141Sjeff */ 2009207141Sjeff refrec->jr_nlink = rrn->jr_nlink; 2010207141Sjeff srn->sr_rec = srec->sr_rec; 2011207141Sjeff return; 2012207141Sjeff } 2013207141Sjeff TAILQ_INSERT_TAIL(&sino->si_recs, srec, sr_next); 2014207141Sjeff} 2015207141Sjeff 2016207141Sjeff/* 2017207141Sjeff * Create a duplicate of a reference at a previous location. 2018207141Sjeff */ 2019207141Sjeffstatic void 2020207141Sjeffino_dup_ref(struct suj_ino *sino, struct jrefrec *refrec, off_t diroff) 2021207141Sjeff{ 2022207141Sjeff struct jrefrec *rrn; 2023207141Sjeff struct suj_rec *srn; 2024207141Sjeff 2025207141Sjeff rrn = errmalloc(sizeof(*refrec)); 2026207141Sjeff *rrn = *refrec; 2027207141Sjeff rrn->jr_op = JOP_ADDREF; 2028207141Sjeff rrn->jr_diroff = diroff; 2029207141Sjeff srn = errmalloc(sizeof(*srn)); 2030207141Sjeff srn->sr_rec = (union jrec *)rrn; 2031207141Sjeff ino_add_ref(sino, srn); 2032207141Sjeff} 2033207141Sjeff 2034207141Sjeff/* 2035207141Sjeff * Add a reference to the list at all known locations. We follow the offset 2036207141Sjeff * changes for a single instance and create duplicate add refs at each so 2037207141Sjeff * that we can tolerate any version of the directory block. Eliminate 2038207141Sjeff * removes which collide with adds that are seen in the journal. They should 2039207141Sjeff * not adjust the link count down. 2040207141Sjeff */ 2041207141Sjeffstatic void 2042207141Sjeffino_build_ref(struct suj_ino *sino, struct suj_rec *srec) 2043207141Sjeff{ 2044207141Sjeff struct jrefrec *refrec; 2045207141Sjeff struct jmvrec *mvrec; 2046207141Sjeff struct suj_rec *srp; 2047207141Sjeff struct suj_rec *srn; 2048207141Sjeff struct jrefrec *rrn; 2049207141Sjeff off_t diroff; 2050207141Sjeff 2051207141Sjeff refrec = (struct jrefrec *)srec->sr_rec; 2052207141Sjeff /* 2053207141Sjeff * Search for a mvrec that matches this offset. Whether it's an add 2054207141Sjeff * or a remove we can delete the mvref after creating a dup record in 2055207141Sjeff * the old location. 2056207141Sjeff */ 2057207141Sjeff if (!TAILQ_EMPTY(&sino->si_movs)) { 2058207141Sjeff diroff = refrec->jr_diroff; 2059207141Sjeff for (srn = TAILQ_LAST(&sino->si_movs, srechd); srn; srn = srp) { 2060207141Sjeff srp = TAILQ_PREV(srn, srechd, sr_next); 2061207141Sjeff mvrec = (struct jmvrec *)srn->sr_rec; 2062207141Sjeff if (mvrec->jm_parent != refrec->jr_parent || 2063207141Sjeff mvrec->jm_newoff != diroff) 2064207141Sjeff continue; 2065207141Sjeff diroff = mvrec->jm_oldoff; 2066207141Sjeff TAILQ_REMOVE(&sino->si_movs, srn, sr_next); 2067209408Sdelphij free(srn); 2068207141Sjeff ino_dup_ref(sino, refrec, diroff); 2069207141Sjeff } 2070207141Sjeff } 2071207141Sjeff /* 2072207141Sjeff * If a remove wasn't eliminated by an earlier add just append it to 2073207141Sjeff * the list. 2074207141Sjeff */ 2075207141Sjeff if (refrec->jr_op == JOP_REMREF) { 2076207141Sjeff ino_add_ref(sino, srec); 2077207141Sjeff return; 2078207141Sjeff } 2079207141Sjeff /* 2080207141Sjeff * Walk the list of records waiting to be added to the list. We 2081207141Sjeff * must check for moves that apply to our current offset and remove 2082207141Sjeff * them from the list. Remove any duplicates to eliminate removes 2083207141Sjeff * with corresponding adds. 2084207141Sjeff */ 2085207141Sjeff TAILQ_FOREACH_SAFE(srn, &sino->si_newrecs, sr_next, srp) { 2086207141Sjeff switch (srn->sr_rec->rec_jrefrec.jr_op) { 2087207141Sjeff case JOP_ADDREF: 2088207141Sjeff /* 2089207141Sjeff * This should actually be an error we should 2090207141Sjeff * have a remove for every add journaled. 2091207141Sjeff */ 2092207141Sjeff rrn = (struct jrefrec *)srn->sr_rec; 2093207141Sjeff if (rrn->jr_parent != refrec->jr_parent || 2094207141Sjeff rrn->jr_diroff != refrec->jr_diroff) 2095207141Sjeff break; 2096207141Sjeff TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next); 2097207141Sjeff break; 2098207141Sjeff case JOP_REMREF: 2099207141Sjeff /* 2100207141Sjeff * Once we remove the current iteration of the 2101207141Sjeff * record at this address we're done. 2102207141Sjeff */ 2103207141Sjeff rrn = (struct jrefrec *)srn->sr_rec; 2104207141Sjeff if (rrn->jr_parent != refrec->jr_parent || 2105207141Sjeff rrn->jr_diroff != refrec->jr_diroff) 2106207141Sjeff break; 2107207141Sjeff TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next); 2108207141Sjeff ino_add_ref(sino, srec); 2109207141Sjeff return; 2110207141Sjeff case JOP_MVREF: 2111207141Sjeff /* 2112207141Sjeff * Update our diroff based on any moves that match 2113207141Sjeff * and remove the move. 2114207141Sjeff */ 2115207141Sjeff mvrec = (struct jmvrec *)srn->sr_rec; 2116207141Sjeff if (mvrec->jm_parent != refrec->jr_parent || 2117207141Sjeff mvrec->jm_oldoff != refrec->jr_diroff) 2118207141Sjeff break; 2119207141Sjeff ino_dup_ref(sino, refrec, mvrec->jm_oldoff); 2120207141Sjeff refrec->jr_diroff = mvrec->jm_newoff; 2121207141Sjeff TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next); 2122207141Sjeff break; 2123207141Sjeff default: 2124209408Sdelphij err_suj("ino_build_ref: Unknown op %d\n", 2125207141Sjeff srn->sr_rec->rec_jrefrec.jr_op); 2126207141Sjeff } 2127207141Sjeff } 2128207141Sjeff ino_add_ref(sino, srec); 2129207141Sjeff} 2130207141Sjeff 2131207141Sjeff/* 2132207141Sjeff * Walk the list of new records and add them in-order resolving any 2133207141Sjeff * dups and adjusted offsets. 2134207141Sjeff */ 2135207141Sjeffstatic void 2136207141Sjeffino_build(struct suj_ino *sino) 2137207141Sjeff{ 2138207141Sjeff struct suj_rec *srec; 2139207141Sjeff 2140207141Sjeff while ((srec = TAILQ_FIRST(&sino->si_newrecs)) != NULL) { 2141207141Sjeff TAILQ_REMOVE(&sino->si_newrecs, srec, sr_next); 2142207141Sjeff switch (srec->sr_rec->rec_jrefrec.jr_op) { 2143207141Sjeff case JOP_ADDREF: 2144207141Sjeff case JOP_REMREF: 2145207141Sjeff ino_build_ref(sino, srec); 2146207141Sjeff break; 2147207141Sjeff case JOP_MVREF: 2148207141Sjeff /* 2149207141Sjeff * Add this mvrec to the queue of pending mvs. 2150207141Sjeff */ 2151207141Sjeff TAILQ_INSERT_TAIL(&sino->si_movs, srec, sr_next); 2152207141Sjeff break; 2153207141Sjeff default: 2154209408Sdelphij err_suj("ino_build: Unknown op %d\n", 2155207141Sjeff srec->sr_rec->rec_jrefrec.jr_op); 2156207141Sjeff } 2157207141Sjeff } 2158207141Sjeff if (TAILQ_EMPTY(&sino->si_recs)) 2159207141Sjeff sino->si_hasrecs = 0; 2160207141Sjeff} 2161207141Sjeff 2162207141Sjeff/* 2163207141Sjeff * Modify journal records so they refer to the base block number 2164207141Sjeff * and a start and end frag range. This is to facilitate the discovery 2165207141Sjeff * of overlapping fragment allocations. 2166207141Sjeff */ 2167207141Sjeffstatic void 2168207141Sjeffblk_build(struct jblkrec *blkrec) 2169207141Sjeff{ 2170207141Sjeff struct suj_rec *srec; 2171207141Sjeff struct suj_blk *sblk; 2172207141Sjeff struct jblkrec *blkrn; 2173207141Sjeff ufs2_daddr_t blk; 2174207141Sjeff int frag; 2175207141Sjeff 2176207141Sjeff if (debug) 2177207141Sjeff printf("blk_build: op %d blkno %jd frags %d oldfrags %d " 2178207141Sjeff "ino %d lbn %jd\n", 2179207141Sjeff blkrec->jb_op, blkrec->jb_blkno, blkrec->jb_frags, 2180207141Sjeff blkrec->jb_oldfrags, blkrec->jb_ino, blkrec->jb_lbn); 2181207141Sjeff 2182207141Sjeff blk = blknum(fs, blkrec->jb_blkno); 2183207141Sjeff frag = fragnum(fs, blkrec->jb_blkno); 2184207141Sjeff sblk = blk_lookup(blk, 1); 2185207141Sjeff /* 2186207141Sjeff * Rewrite the record using oldfrags to indicate the offset into 2187207141Sjeff * the block. Leave jb_frags as the actual allocated count. 2188207141Sjeff */ 2189207141Sjeff blkrec->jb_blkno -= frag; 2190207141Sjeff blkrec->jb_oldfrags = frag; 2191207141Sjeff if (blkrec->jb_oldfrags + blkrec->jb_frags > fs->fs_frag) 2192209408Sdelphij err_suj("Invalid fragment count %d oldfrags %d\n", 2193207141Sjeff blkrec->jb_frags, frag); 2194207141Sjeff /* 2195207141Sjeff * Detect dups. If we detect a dup we always discard the oldest 2196207141Sjeff * record as it is superseded by the new record. This speeds up 2197207141Sjeff * later stages but also eliminates free records which are used 2198207141Sjeff * to indicate that the contents of indirects can be trusted. 2199207141Sjeff */ 2200207141Sjeff TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) { 2201207141Sjeff blkrn = (struct jblkrec *)srec->sr_rec; 2202207141Sjeff if (blkrn->jb_ino != blkrec->jb_ino || 2203207141Sjeff blkrn->jb_lbn != blkrec->jb_lbn || 2204207141Sjeff blkrn->jb_blkno != blkrec->jb_blkno || 2205207141Sjeff blkrn->jb_frags != blkrec->jb_frags || 2206207141Sjeff blkrn->jb_oldfrags != blkrec->jb_oldfrags) 2207207141Sjeff continue; 2208207141Sjeff if (debug) 2209207141Sjeff printf("Removed dup.\n"); 2210207141Sjeff /* Discard the free which is a dup with an alloc. */ 2211207141Sjeff if (blkrec->jb_op == JOP_FREEBLK) 2212207141Sjeff return; 2213207141Sjeff TAILQ_REMOVE(&sblk->sb_recs, srec, sr_next); 2214207141Sjeff free(srec); 2215207141Sjeff break; 2216207141Sjeff } 2217207141Sjeff srec = errmalloc(sizeof(*srec)); 2218207141Sjeff srec->sr_rec = (union jrec *)blkrec; 2219207141Sjeff TAILQ_INSERT_TAIL(&sblk->sb_recs, srec, sr_next); 2220207141Sjeff} 2221207141Sjeff 2222207141Sjeffstatic void 2223207141Sjeffino_build_trunc(struct jtrncrec *rec) 2224207141Sjeff{ 2225207141Sjeff struct suj_ino *sino; 2226207141Sjeff 2227207141Sjeff if (debug) 2228222958Sjeff printf("ino_build_trunc: op %d ino %d, size %jd\n", 2229222958Sjeff rec->jt_op, rec->jt_ino, rec->jt_size); 2230207141Sjeff sino = ino_lookup(rec->jt_ino, 1); 2231222958Sjeff if (rec->jt_op == JOP_SYNC) { 2232222958Sjeff sino->si_trunc = NULL; 2233222958Sjeff return; 2234222958Sjeff } 2235222958Sjeff if (sino->si_trunc == NULL || sino->si_trunc->jt_size > rec->jt_size) 2236222958Sjeff sino->si_trunc = rec; 2237207141Sjeff} 2238207141Sjeff 2239207141Sjeff/* 2240207141Sjeff * Build up tables of the operations we need to recover. 2241207141Sjeff */ 2242207141Sjeffstatic void 2243207141Sjeffsuj_build(void) 2244207141Sjeff{ 2245207141Sjeff struct suj_seg *seg; 2246207141Sjeff union jrec *rec; 2247207141Sjeff int off; 2248207141Sjeff int i; 2249207141Sjeff 2250207141Sjeff TAILQ_FOREACH(seg, &allsegs, ss_next) { 2251207141Sjeff if (debug) 2252207141Sjeff printf("seg %jd has %d records, oldseq %jd.\n", 2253207141Sjeff seg->ss_rec.jsr_seq, seg->ss_rec.jsr_cnt, 2254207141Sjeff seg->ss_rec.jsr_oldest); 2255207141Sjeff off = 0; 2256207141Sjeff rec = (union jrec *)seg->ss_blk; 2257207141Sjeff for (i = 0; i < seg->ss_rec.jsr_cnt; off += JREC_SIZE, rec++) { 2258207141Sjeff /* skip the segrec. */ 2259218604Skib if ((off % real_dev_bsize) == 0) 2260207141Sjeff continue; 2261207141Sjeff switch (rec->rec_jrefrec.jr_op) { 2262207141Sjeff case JOP_ADDREF: 2263207141Sjeff case JOP_REMREF: 2264207141Sjeff case JOP_MVREF: 2265207141Sjeff ino_append(rec); 2266207141Sjeff break; 2267207141Sjeff case JOP_NEWBLK: 2268207141Sjeff case JOP_FREEBLK: 2269207141Sjeff blk_build((struct jblkrec *)rec); 2270207141Sjeff break; 2271207141Sjeff case JOP_TRUNC: 2272223689Sjeff case JOP_SYNC: 2273207141Sjeff ino_build_trunc((struct jtrncrec *)rec); 2274207141Sjeff break; 2275207141Sjeff default: 2276209408Sdelphij err_suj("Unknown journal operation %d (%d)\n", 2277207141Sjeff rec->rec_jrefrec.jr_op, off); 2278207141Sjeff } 2279207141Sjeff i++; 2280207141Sjeff } 2281207141Sjeff } 2282207141Sjeff} 2283207141Sjeff 2284207141Sjeff/* 2285207141Sjeff * Prune the journal segments to those we care about based on the 2286207141Sjeff * oldest sequence in the newest segment. Order the segment list 2287207141Sjeff * based on sequence number. 2288207141Sjeff */ 2289207141Sjeffstatic void 2290207141Sjeffsuj_prune(void) 2291207141Sjeff{ 2292207141Sjeff struct suj_seg *seg; 2293207141Sjeff struct suj_seg *segn; 2294207141Sjeff uint64_t newseq; 2295207141Sjeff int discard; 2296207141Sjeff 2297207141Sjeff if (debug) 2298207141Sjeff printf("Pruning up to %jd\n", oldseq); 2299207141Sjeff /* First free the expired segments. */ 2300207141Sjeff TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) { 2301207141Sjeff if (seg->ss_rec.jsr_seq >= oldseq) 2302207141Sjeff continue; 2303207141Sjeff TAILQ_REMOVE(&allsegs, seg, ss_next); 2304207141Sjeff free(seg->ss_blk); 2305207141Sjeff free(seg); 2306207141Sjeff } 2307207141Sjeff /* Next ensure that segments are ordered properly. */ 2308207141Sjeff seg = TAILQ_FIRST(&allsegs); 2309207141Sjeff if (seg == NULL) { 2310207141Sjeff if (debug) 2311207141Sjeff printf("Empty journal\n"); 2312207141Sjeff return; 2313207141Sjeff } 2314207141Sjeff newseq = seg->ss_rec.jsr_seq; 2315207141Sjeff for (;;) { 2316207141Sjeff seg = TAILQ_LAST(&allsegs, seghd); 2317207141Sjeff if (seg->ss_rec.jsr_seq >= newseq) 2318207141Sjeff break; 2319207141Sjeff TAILQ_REMOVE(&allsegs, seg, ss_next); 2320207141Sjeff TAILQ_INSERT_HEAD(&allsegs, seg, ss_next); 2321207141Sjeff newseq = seg->ss_rec.jsr_seq; 2322221110Sdes 2323207141Sjeff } 2324209408Sdelphij if (newseq != oldseq) { 2325245778Sscottl TAILQ_FOREACH(seg, &allsegs, ss_next) { 2326245778Sscottl printf("%jd, ", seg->ss_rec.jsr_seq); 2327245778Sscottl } 2328245778Sscottl printf("\n"); 2329209408Sdelphij err_suj("Journal file sequence mismatch %jd != %jd\n", 2330207141Sjeff newseq, oldseq); 2331209408Sdelphij } 2332207141Sjeff /* 2333207141Sjeff * The kernel may asynchronously write segments which can create 2334207141Sjeff * gaps in the sequence space. Throw away any segments after the 2335207141Sjeff * gap as the kernel guarantees only those that are contiguously 2336207141Sjeff * reachable are marked as completed. 2337207141Sjeff */ 2338207141Sjeff discard = 0; 2339207141Sjeff TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) { 2340207141Sjeff if (!discard && newseq++ == seg->ss_rec.jsr_seq) { 2341207141Sjeff jrecs += seg->ss_rec.jsr_cnt; 2342218604Skib jbytes += seg->ss_rec.jsr_blocks * real_dev_bsize; 2343207141Sjeff continue; 2344207141Sjeff } 2345207141Sjeff discard = 1; 2346207141Sjeff if (debug) 2347207141Sjeff printf("Journal order mismatch %jd != %jd pruning\n", 2348207141Sjeff newseq-1, seg->ss_rec.jsr_seq); 2349207141Sjeff TAILQ_REMOVE(&allsegs, seg, ss_next); 2350207141Sjeff free(seg->ss_blk); 2351207141Sjeff free(seg); 2352207141Sjeff } 2353207141Sjeff if (debug) 2354207141Sjeff printf("Processing journal segments from %jd to %jd\n", 2355207141Sjeff oldseq, newseq-1); 2356207141Sjeff} 2357207141Sjeff 2358207141Sjeff/* 2359207141Sjeff * Verify the journal inode before attempting to read records. 2360207141Sjeff */ 2361207141Sjeffstatic int 2362207141Sjeffsuj_verifyino(union dinode *ip) 2363207141Sjeff{ 2364207141Sjeff 2365207141Sjeff if (DIP(ip, di_nlink) != 1) { 2366207141Sjeff printf("Invalid link count %d for journal inode %d\n", 2367207141Sjeff DIP(ip, di_nlink), sujino); 2368207141Sjeff return (-1); 2369207141Sjeff } 2370207141Sjeff 2371207141Sjeff if ((DIP(ip, di_flags) & (SF_IMMUTABLE | SF_NOUNLINK)) != 2372207141Sjeff (SF_IMMUTABLE | SF_NOUNLINK)) { 2373207141Sjeff printf("Invalid flags 0x%X for journal inode %d\n", 2374207141Sjeff DIP(ip, di_flags), sujino); 2375207141Sjeff return (-1); 2376207141Sjeff } 2377207141Sjeff 2378207141Sjeff if (DIP(ip, di_mode) != (IFREG | IREAD)) { 2379207141Sjeff printf("Invalid mode %o for journal inode %d\n", 2380207141Sjeff DIP(ip, di_mode), sujino); 2381207141Sjeff return (-1); 2382207141Sjeff } 2383207141Sjeff 2384239559Skib if (DIP(ip, di_size) < SUJ_MIN) { 2385207141Sjeff printf("Invalid size %jd for journal inode %d\n", 2386207141Sjeff DIP(ip, di_size), sujino); 2387207141Sjeff return (-1); 2388207141Sjeff } 2389207141Sjeff 2390207141Sjeff if (DIP(ip, di_modrev) != fs->fs_mtime) { 2391207141Sjeff printf("Journal timestamp does not match fs mount time\n"); 2392207141Sjeff return (-1); 2393207141Sjeff } 2394207141Sjeff 2395207141Sjeff return (0); 2396207141Sjeff} 2397207141Sjeff 2398207141Sjeffstruct jblocks { 2399207141Sjeff struct jextent *jb_extent; /* Extent array. */ 2400207141Sjeff int jb_avail; /* Available extents. */ 2401207141Sjeff int jb_used; /* Last used extent. */ 2402207141Sjeff int jb_head; /* Allocator head. */ 2403207141Sjeff int jb_off; /* Allocator extent offset. */ 2404207141Sjeff}; 2405207141Sjeffstruct jextent { 2406207141Sjeff ufs2_daddr_t je_daddr; /* Disk block address. */ 2407207141Sjeff int je_blocks; /* Disk block count. */ 2408207141Sjeff}; 2409207141Sjeff 2410207141Sjeffstruct jblocks *suj_jblocks; 2411207141Sjeff 2412207141Sjeffstatic struct jblocks * 2413207141Sjeffjblocks_create(void) 2414207141Sjeff{ 2415207141Sjeff struct jblocks *jblocks; 2416207141Sjeff int size; 2417207141Sjeff 2418207141Sjeff jblocks = errmalloc(sizeof(*jblocks)); 2419207141Sjeff jblocks->jb_avail = 10; 2420207141Sjeff jblocks->jb_used = 0; 2421207141Sjeff jblocks->jb_head = 0; 2422207141Sjeff jblocks->jb_off = 0; 2423207141Sjeff size = sizeof(struct jextent) * jblocks->jb_avail; 2424207141Sjeff jblocks->jb_extent = errmalloc(size); 2425207141Sjeff bzero(jblocks->jb_extent, size); 2426207141Sjeff 2427207141Sjeff return (jblocks); 2428207141Sjeff} 2429207141Sjeff 2430207141Sjeff/* 2431207141Sjeff * Return the next available disk block and the amount of contiguous 2432207141Sjeff * free space it contains. 2433207141Sjeff */ 2434207141Sjeffstatic ufs2_daddr_t 2435207141Sjeffjblocks_next(struct jblocks *jblocks, int bytes, int *actual) 2436207141Sjeff{ 2437207141Sjeff struct jextent *jext; 2438207141Sjeff ufs2_daddr_t daddr; 2439207141Sjeff int freecnt; 2440207141Sjeff int blocks; 2441207141Sjeff 2442218604Skib blocks = bytes / disk->d_bsize; 2443207141Sjeff jext = &jblocks->jb_extent[jblocks->jb_head]; 2444207141Sjeff freecnt = jext->je_blocks - jblocks->jb_off; 2445207141Sjeff if (freecnt == 0) { 2446207141Sjeff jblocks->jb_off = 0; 2447207141Sjeff if (++jblocks->jb_head > jblocks->jb_used) 2448207141Sjeff return (0); 2449207141Sjeff jext = &jblocks->jb_extent[jblocks->jb_head]; 2450207141Sjeff freecnt = jext->je_blocks; 2451207141Sjeff } 2452207141Sjeff if (freecnt > blocks) 2453207141Sjeff freecnt = blocks; 2454218604Skib *actual = freecnt * disk->d_bsize; 2455207141Sjeff daddr = jext->je_daddr + jblocks->jb_off; 2456207141Sjeff 2457207141Sjeff return (daddr); 2458207141Sjeff} 2459207141Sjeff 2460207141Sjeff/* 2461207141Sjeff * Advance the allocation head by a specified number of bytes, consuming 2462207141Sjeff * one journal segment. 2463207141Sjeff */ 2464207141Sjeffstatic void 2465207141Sjeffjblocks_advance(struct jblocks *jblocks, int bytes) 2466207141Sjeff{ 2467207141Sjeff 2468218604Skib jblocks->jb_off += bytes / disk->d_bsize; 2469207141Sjeff} 2470207141Sjeff 2471207141Sjeffstatic void 2472207141Sjeffjblocks_destroy(struct jblocks *jblocks) 2473207141Sjeff{ 2474207141Sjeff 2475207141Sjeff free(jblocks->jb_extent); 2476207141Sjeff free(jblocks); 2477207141Sjeff} 2478207141Sjeff 2479207141Sjeffstatic void 2480207141Sjeffjblocks_add(struct jblocks *jblocks, ufs2_daddr_t daddr, int blocks) 2481207141Sjeff{ 2482207141Sjeff struct jextent *jext; 2483207141Sjeff int size; 2484207141Sjeff 2485207141Sjeff jext = &jblocks->jb_extent[jblocks->jb_used]; 2486207141Sjeff /* Adding the first block. */ 2487207141Sjeff if (jext->je_daddr == 0) { 2488207141Sjeff jext->je_daddr = daddr; 2489207141Sjeff jext->je_blocks = blocks; 2490207141Sjeff return; 2491207141Sjeff } 2492207141Sjeff /* Extending the last extent. */ 2493207141Sjeff if (jext->je_daddr + jext->je_blocks == daddr) { 2494207141Sjeff jext->je_blocks += blocks; 2495207141Sjeff return; 2496207141Sjeff } 2497207141Sjeff /* Adding a new extent. */ 2498207141Sjeff if (++jblocks->jb_used == jblocks->jb_avail) { 2499207141Sjeff jblocks->jb_avail *= 2; 2500207141Sjeff size = sizeof(struct jextent) * jblocks->jb_avail; 2501207141Sjeff jext = errmalloc(size); 2502207141Sjeff bzero(jext, size); 2503207141Sjeff bcopy(jblocks->jb_extent, jext, 2504207141Sjeff sizeof(struct jextent) * jblocks->jb_used); 2505207141Sjeff free(jblocks->jb_extent); 2506207141Sjeff jblocks->jb_extent = jext; 2507207141Sjeff } 2508207141Sjeff jext = &jblocks->jb_extent[jblocks->jb_used]; 2509207141Sjeff jext->je_daddr = daddr; 2510207141Sjeff jext->je_blocks = blocks; 2511207141Sjeff 2512207141Sjeff return; 2513207141Sjeff} 2514207141Sjeff 2515207141Sjeff/* 2516207141Sjeff * Add a file block from the journal to the extent map. We can't read 2517207141Sjeff * each file block individually because the kernel treats it as a circular 2518207141Sjeff * buffer and segments may span mutliple contiguous blocks. 2519207141Sjeff */ 2520207141Sjeffstatic void 2521207141Sjeffsuj_add_block(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 2522207141Sjeff{ 2523207141Sjeff 2524207141Sjeff jblocks_add(suj_jblocks, fsbtodb(fs, blk), fsbtodb(fs, frags)); 2525207141Sjeff} 2526207141Sjeff 2527207141Sjeffstatic void 2528207141Sjeffsuj_read(void) 2529207141Sjeff{ 2530207141Sjeff uint8_t block[1 * 1024 * 1024]; 2531207141Sjeff struct suj_seg *seg; 2532207141Sjeff struct jsegrec *recn; 2533207141Sjeff struct jsegrec *rec; 2534207141Sjeff ufs2_daddr_t blk; 2535207141Sjeff int readsize; 2536207141Sjeff int blocks; 2537207141Sjeff int recsize; 2538207141Sjeff int size; 2539207141Sjeff int i; 2540207141Sjeff 2541207141Sjeff /* 2542207141Sjeff * Read records until we exhaust the journal space. If we find 2543207141Sjeff * an invalid record we start searching for a valid segment header 2544207141Sjeff * at the next block. This is because we don't have a head/tail 2545207141Sjeff * pointer and must recover the information indirectly. At the gap 2546207141Sjeff * between the head and tail we won't necessarily have a valid 2547207141Sjeff * segment. 2548207141Sjeff */ 2549207141Sjeffrestart: 2550207141Sjeff for (;;) { 2551207141Sjeff size = sizeof(block); 2552207141Sjeff blk = jblocks_next(suj_jblocks, size, &readsize); 2553207141Sjeff if (blk == 0) 2554207141Sjeff return; 2555207141Sjeff size = readsize; 2556207141Sjeff /* 2557207141Sjeff * Read 1MB at a time and scan for records within this block. 2558207141Sjeff */ 2559209408Sdelphij if (bread(disk, blk, &block, size) == -1) { 2560209408Sdelphij err_suj("Error reading journal block %jd\n", 2561207141Sjeff (intmax_t)blk); 2562209408Sdelphij } 2563207141Sjeff for (rec = (void *)block; size; size -= recsize, 2564207141Sjeff rec = (struct jsegrec *)((uintptr_t)rec + recsize)) { 2565218604Skib recsize = real_dev_bsize; 2566207141Sjeff if (rec->jsr_time != fs->fs_mtime) { 2567207141Sjeff if (debug) 2568207141Sjeff printf("Rec time %jd != fs mtime %jd\n", 2569207141Sjeff rec->jsr_time, fs->fs_mtime); 2570207141Sjeff jblocks_advance(suj_jblocks, recsize); 2571207141Sjeff continue; 2572207141Sjeff } 2573207141Sjeff if (rec->jsr_cnt == 0) { 2574207141Sjeff if (debug) 2575207141Sjeff printf("Found illegal count %d\n", 2576207141Sjeff rec->jsr_cnt); 2577207141Sjeff jblocks_advance(suj_jblocks, recsize); 2578207141Sjeff continue; 2579207141Sjeff } 2580207141Sjeff blocks = rec->jsr_blocks; 2581218604Skib recsize = blocks * real_dev_bsize; 2582207141Sjeff if (recsize > size) { 2583207141Sjeff /* 2584207141Sjeff * We may just have run out of buffer, restart 2585207141Sjeff * the loop to re-read from this spot. 2586207141Sjeff */ 2587207141Sjeff if (size < fs->fs_bsize && 2588207141Sjeff size != readsize && 2589207141Sjeff recsize <= fs->fs_bsize) 2590207141Sjeff goto restart; 2591207141Sjeff if (debug) 2592207141Sjeff printf("Found invalid segsize %d > %d\n", 2593207141Sjeff recsize, size); 2594218604Skib recsize = real_dev_bsize; 2595207141Sjeff jblocks_advance(suj_jblocks, recsize); 2596207141Sjeff continue; 2597207141Sjeff } 2598207141Sjeff /* 2599207141Sjeff * Verify that all blocks in the segment are present. 2600207141Sjeff */ 2601207141Sjeff for (i = 1; i < blocks; i++) { 2602218604Skib recn = (void *)((uintptr_t)rec) + i * 2603218604Skib real_dev_bsize; 2604207141Sjeff if (recn->jsr_seq == rec->jsr_seq && 2605221110Sdes recn->jsr_time == rec->jsr_time) 2606207141Sjeff continue; 2607207141Sjeff if (debug) 2608207141Sjeff printf("Incomplete record %jd (%d)\n", 2609207141Sjeff rec->jsr_seq, i); 2610218604Skib recsize = i * real_dev_bsize; 2611207141Sjeff jblocks_advance(suj_jblocks, recsize); 2612207141Sjeff goto restart; 2613207141Sjeff } 2614207141Sjeff seg = errmalloc(sizeof(*seg)); 2615207141Sjeff seg->ss_blk = errmalloc(recsize); 2616207141Sjeff seg->ss_rec = *rec; 2617207141Sjeff bcopy((void *)rec, seg->ss_blk, recsize); 2618207141Sjeff if (rec->jsr_oldest > oldseq) 2619207141Sjeff oldseq = rec->jsr_oldest; 2620207141Sjeff TAILQ_INSERT_TAIL(&allsegs, seg, ss_next); 2621207141Sjeff jblocks_advance(suj_jblocks, recsize); 2622207141Sjeff } 2623207141Sjeff } 2624207141Sjeff} 2625207141Sjeff 2626207141Sjeff/* 2627207141Sjeff * Search a directory block for the SUJ_FILE. 2628207141Sjeff */ 2629207141Sjeffstatic void 2630207141Sjeffsuj_find(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags) 2631207141Sjeff{ 2632207141Sjeff char block[MAXBSIZE]; 2633207141Sjeff struct direct *dp; 2634207141Sjeff int bytes; 2635207141Sjeff int off; 2636207141Sjeff 2637207141Sjeff if (sujino) 2638207141Sjeff return; 2639207141Sjeff bytes = lfragtosize(fs, frags); 2640207141Sjeff if (bread(disk, fsbtodb(fs, blk), block, bytes) <= 0) 2641209408Sdelphij err_suj("Failed to read ROOTINO directory block %jd\n", blk); 2642207141Sjeff for (off = 0; off < bytes; off += dp->d_reclen) { 2643207141Sjeff dp = (struct direct *)&block[off]; 2644207141Sjeff if (dp->d_reclen == 0) 2645207141Sjeff break; 2646207141Sjeff if (dp->d_ino == 0) 2647207141Sjeff continue; 2648207141Sjeff if (dp->d_namlen != strlen(SUJ_FILE)) 2649207141Sjeff continue; 2650207141Sjeff if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0) 2651207141Sjeff continue; 2652207141Sjeff sujino = dp->d_ino; 2653207141Sjeff return; 2654207141Sjeff } 2655207141Sjeff} 2656207141Sjeff 2657207141Sjeff/* 2658207141Sjeff * Orchestrate the verification of a filesystem via the softupdates journal. 2659207141Sjeff */ 2660207141Sjeffint 2661207141Sjeffsuj_check(const char *filesys) 2662207141Sjeff{ 2663207141Sjeff union dinode *jip; 2664207141Sjeff union dinode *ip; 2665207141Sjeff uint64_t blocks; 2666209408Sdelphij int retval; 2667209408Sdelphij struct suj_seg *seg; 2668209408Sdelphij struct suj_seg *segn; 2669207141Sjeff 2670207141Sjeff opendisk(filesys); 2671207141Sjeff TAILQ_INIT(&allsegs); 2672209408Sdelphij 2673207141Sjeff /* 2674209408Sdelphij * Set an exit point when SUJ check failed 2675209408Sdelphij */ 2676209408Sdelphij retval = setjmp(jmpbuf); 2677209408Sdelphij if (retval != 0) { 2678209408Sdelphij pwarn("UNEXPECTED SU+J INCONSISTENCY\n"); 2679209408Sdelphij TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) { 2680209408Sdelphij TAILQ_REMOVE(&allsegs, seg, ss_next); 2681209408Sdelphij free(seg->ss_blk); 2682209408Sdelphij free(seg); 2683209408Sdelphij } 2684209408Sdelphij if (reply("FALLBACK TO FULL FSCK") == 0) { 2685209408Sdelphij ckfini(0); 2686209408Sdelphij exit(EEXIT); 2687209408Sdelphij } else 2688209408Sdelphij return (-1); 2689209408Sdelphij } 2690209408Sdelphij 2691209408Sdelphij /* 2692207141Sjeff * Find the journal inode. 2693207141Sjeff */ 2694207141Sjeff ip = ino_read(ROOTINO); 2695207141Sjeff sujino = 0; 2696207141Sjeff ino_visit(ip, ROOTINO, suj_find, 0); 2697209408Sdelphij if (sujino == 0) { 2698209408Sdelphij printf("Journal inode removed. Use tunefs to re-create.\n"); 2699209408Sdelphij sblock.fs_flags &= ~FS_SUJ; 2700209408Sdelphij sblock.fs_sujfree = 0; 2701209408Sdelphij return (-1); 2702209408Sdelphij } 2703207141Sjeff /* 2704207141Sjeff * Fetch the journal inode and verify it. 2705207141Sjeff */ 2706207141Sjeff jip = ino_read(sujino); 2707207141Sjeff printf("** SU+J Recovering %s\n", filesys); 2708207141Sjeff if (suj_verifyino(jip) != 0) 2709207141Sjeff return (-1); 2710207141Sjeff /* 2711207141Sjeff * Build a list of journal blocks in jblocks before parsing the 2712207141Sjeff * available journal blocks in with suj_read(). 2713207141Sjeff */ 2714207141Sjeff printf("** Reading %jd byte journal from inode %d.\n", 2715207141Sjeff DIP(jip, di_size), sujino); 2716207141Sjeff suj_jblocks = jblocks_create(); 2717207141Sjeff blocks = ino_visit(jip, sujino, suj_add_block, 0); 2718209408Sdelphij if (blocks != numfrags(fs, DIP(jip, di_size))) { 2719209408Sdelphij printf("Sparse journal inode %d.\n", sujino); 2720209408Sdelphij return (-1); 2721209408Sdelphij } 2722207141Sjeff suj_read(); 2723207141Sjeff jblocks_destroy(suj_jblocks); 2724207141Sjeff suj_jblocks = NULL; 2725207141Sjeff if (preen || reply("RECOVER")) { 2726207141Sjeff printf("** Building recovery table.\n"); 2727207141Sjeff suj_prune(); 2728207141Sjeff suj_build(); 2729207141Sjeff cg_apply(cg_build); 2730207141Sjeff printf("** Resolving unreferenced inode list.\n"); 2731207141Sjeff ino_unlinked(); 2732207141Sjeff printf("** Processing journal entries.\n"); 2733207141Sjeff cg_apply(cg_trunc); 2734207141Sjeff cg_apply(cg_check_blk); 2735237265Skib cg_apply(cg_adj_blk); 2736207141Sjeff cg_apply(cg_check_ino); 2737207141Sjeff } 2738209408Sdelphij if (preen == 0 && (jrecs > 0 || jbytes > 0) && reply("WRITE CHANGES") == 0) 2739207141Sjeff return (0); 2740207141Sjeff /* 2741207141Sjeff * To remain idempotent with partial truncations the free bitmaps 2742207141Sjeff * must be written followed by indirect blocks and lastly inode 2743207141Sjeff * blocks. This preserves access to the modified pointers until 2744207141Sjeff * they are freed. 2745207141Sjeff */ 2746207141Sjeff cg_apply(cg_write); 2747207141Sjeff dblk_write(); 2748207141Sjeff cg_apply(cg_write_inos); 2749207141Sjeff /* Write back superblock. */ 2750207141Sjeff closedisk(filesys); 2751209408Sdelphij if (jrecs > 0 || jbytes > 0) { 2752209408Sdelphij printf("** %jd journal records in %jd bytes for %.2f%% utilization\n", 2753209408Sdelphij jrecs, jbytes, ((float)jrecs / (float)(jbytes / JREC_SIZE)) * 100); 2754209408Sdelphij printf("** Freed %jd inodes (%jd dirs) %jd blocks, and %jd frags.\n", 2755209408Sdelphij freeinos, freedir, freeblocks, freefrags); 2756209408Sdelphij } 2757207141Sjeff 2758207141Sjeff return (0); 2759207141Sjeff} 2760