1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if 0 33 #ifndef lint 34 static const char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95"; 35 #endif /* not lint */ 36 #endif 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/stdint.h> 42 #include <sys/sysctl.h> 43 44 #include <ufs/ufs/dinode.h> 45 #include <ufs/ufs/dir.h> 46 #include <ufs/ffs/fs.h> 47 48 #include <err.h> 49 #include <pwd.h> 50 #include <string.h> 51 #include <time.h> 52 #include <libufs.h> 53 54 #include "fsck.h" 55 56 struct bufarea *icachebp; /* inode cache buffer */ 57 58 static int iblock(struct inodesc *, off_t isize, int type); 59 static ufs2_daddr_t indir_blkatoff(ufs2_daddr_t, ino_t, ufs_lbn_t, ufs_lbn_t, 60 struct bufarea **); 61 62 int 63 ckinode(union dinode *dp, struct inodesc *idesc) 64 { 65 off_t remsize, sizepb; 66 int i, offset, ret; 67 struct inode ip; 68 union dinode dino; 69 ufs2_daddr_t ndb; 70 mode_t mode; 71 char pathbuf[MAXPATHLEN + 1]; 72 73 if (idesc->id_fix != IGNORE) 74 idesc->id_fix = DONTKNOW; 75 idesc->id_dp = dp; 76 idesc->id_lbn = -1; 77 idesc->id_lballoc = -1; 78 idesc->id_level = 0; 79 idesc->id_entryno = 0; 80 idesc->id_filesize = DIP(dp, di_size); 81 mode = DIP(dp, di_mode) & IFMT; 82 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK && 83 DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen)) 84 return (KEEPON); 85 if (sblock.fs_magic == FS_UFS1_MAGIC) 86 dino.dp1 = dp->dp1; 87 else 88 dino.dp2 = dp->dp2; 89 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize); 90 for (i = 0; i < UFS_NDADDR; i++) { 91 idesc->id_lbn++; 92 if (--ndb == 0 && 93 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0) 94 idesc->id_numfrags = 95 numfrags(&sblock, fragroundup(&sblock, offset)); 96 else 97 idesc->id_numfrags = sblock.fs_frag; 98 if (DIP(&dino, di_db[i]) == 0) { 99 if (idesc->id_type == DATA && ndb >= 0) { 100 /* An empty block in a directory XXX */ 101 getpathname(pathbuf, idesc->id_number, 102 idesc->id_number); 103 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 104 pathbuf); 105 if (reply("ADJUST LENGTH") == 1) { 106 ginode(idesc->id_number, &ip); 107 DIP_SET(ip.i_dp, di_size, 108 i * sblock.fs_bsize); 109 printf( 110 "YOU MUST RERUN FSCK AFTERWARDS\n"); 111 rerun = 1; 112 inodirty(&ip); 113 irelse(&ip); 114 } 115 } 116 continue; 117 } 118 idesc->id_blkno = DIP(&dino, di_db[i]); 119 if (idesc->id_type != DATA) 120 ret = (*idesc->id_func)(idesc); 121 else 122 ret = dirscan(idesc); 123 if (ret & STOP) 124 return (ret); 125 } 126 idesc->id_numfrags = sblock.fs_frag; 127 remsize = DIP(&dino, di_size) - sblock.fs_bsize * UFS_NDADDR; 128 sizepb = sblock.fs_bsize; 129 for (i = 0; i < UFS_NIADDR; i++) { 130 sizepb *= NINDIR(&sblock); 131 idesc->id_level = i + 1; 132 if (DIP(&dino, di_ib[i])) { 133 idesc->id_blkno = DIP(&dino, di_ib[i]); 134 ret = iblock(idesc, remsize, BT_LEVEL1 + i); 135 if (ret & STOP) 136 return (ret); 137 } else if (remsize > 0) { 138 idesc->id_lbn += sizepb / sblock.fs_bsize; 139 if (idesc->id_type == DATA) { 140 /* An empty block in a directory XXX */ 141 getpathname(pathbuf, idesc->id_number, 142 idesc->id_number); 143 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 144 pathbuf); 145 if (reply("ADJUST LENGTH") == 1) { 146 ginode(idesc->id_number, &ip); 147 DIP_SET(ip.i_dp, di_size, 148 DIP(ip.i_dp, di_size) - remsize); 149 remsize = 0; 150 printf( 151 "YOU MUST RERUN FSCK AFTERWARDS\n"); 152 rerun = 1; 153 inodirty(&ip); 154 irelse(&ip); 155 break; 156 } 157 } 158 } 159 remsize -= sizepb; 160 } 161 return (KEEPON); 162 } 163 164 static int 165 iblock(struct inodesc *idesc, off_t isize, int type) 166 { 167 struct inode ip; 168 struct bufarea *bp; 169 int i, n, (*func)(struct inodesc *), nif; 170 off_t sizepb; 171 char buf[BUFSIZ]; 172 char pathbuf[MAXPATHLEN + 1]; 173 174 if (idesc->id_type != DATA) { 175 func = idesc->id_func; 176 if (((n = (*func)(idesc)) & KEEPON) == 0) 177 return (n); 178 } else 179 func = dirscan; 180 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type); 181 if (bp->b_errs != 0) { 182 brelse(bp); 183 return (SKIP); 184 } 185 idesc->id_bp = bp; 186 idesc->id_level--; 187 for (sizepb = sblock.fs_bsize, i = 0; i < idesc->id_level; i++) 188 sizepb *= NINDIR(&sblock); 189 if (howmany(isize, sizepb) > NINDIR(&sblock)) 190 nif = NINDIR(&sblock); 191 else 192 nif = howmany(isize, sizepb); 193 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) { 194 for (i = nif; i < NINDIR(&sblock); i++) { 195 if (IBLK(bp, i) == 0) 196 continue; 197 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu", 198 (u_long)idesc->id_number); 199 if (preen) { 200 pfatal("%s", buf); 201 } else if (dofix(idesc, buf)) { 202 IBLK_SET(bp, i, 0); 203 dirty(bp); 204 } 205 } 206 flush(fswritefd, bp); 207 } 208 for (i = 0; i < nif; i++) { 209 if (IBLK(bp, i)) { 210 idesc->id_blkno = IBLK(bp, i); 211 bp->b_index = i; 212 if (idesc->id_level == 0) { 213 idesc->id_lbn++; 214 n = (*func)(idesc); 215 } else { 216 n = iblock(idesc, isize, type); 217 idesc->id_level++; 218 } 219 if (n & STOP) { 220 brelse(bp); 221 return (n); 222 } 223 } else { 224 idesc->id_lbn += sizepb / sblock.fs_bsize; 225 if (idesc->id_type == DATA && isize > 0) { 226 /* An empty block in a directory XXX */ 227 getpathname(pathbuf, idesc->id_number, 228 idesc->id_number); 229 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 230 pathbuf); 231 if (reply("ADJUST LENGTH") == 1) { 232 ginode(idesc->id_number, &ip); 233 DIP_SET(ip.i_dp, di_size, 234 DIP(ip.i_dp, di_size) - isize); 235 isize = 0; 236 printf( 237 "YOU MUST RERUN FSCK AFTERWARDS\n"); 238 rerun = 1; 239 inodirty(&ip); 240 brelse(bp); 241 return(STOP); 242 } 243 } 244 } 245 isize -= sizepb; 246 } 247 brelse(bp); 248 return (KEEPON); 249 } 250 251 /* 252 * Finds the disk block address at the specified lbn within the inode 253 * specified by dp. This follows the whole tree and honors di_size and 254 * di_extsize so it is a true test of reachability. The lbn may be 255 * negative if an extattr or indirect block is requested. 256 */ 257 ufs2_daddr_t 258 ino_blkatoff(union dinode *dp, ino_t ino, ufs_lbn_t lbn, int *frags, 259 struct bufarea **bpp) 260 { 261 ufs_lbn_t tmpval; 262 ufs_lbn_t cur; 263 ufs_lbn_t next; 264 int i; 265 266 *frags = 0; 267 /* 268 * Handle extattr blocks first. 269 */ 270 if (lbn < 0 && lbn >= -UFS_NXADDR) { 271 lbn = -1 - lbn; 272 if (lbn > lblkno(&sblock, dp->dp2.di_extsize - 1)) 273 return (0); 274 *frags = numfrags(&sblock, 275 sblksize(&sblock, dp->dp2.di_extsize, lbn)); 276 return (dp->dp2.di_extb[lbn]); 277 } 278 /* 279 * Now direct and indirect. 280 */ 281 if (DIP(dp, di_mode) == IFLNK && 282 DIP(dp, di_size) < sblock.fs_maxsymlinklen) 283 return (0); 284 if (lbn >= 0 && lbn < UFS_NDADDR) { 285 *frags = numfrags(&sblock, 286 sblksize(&sblock, DIP(dp, di_size), lbn)); 287 return (DIP(dp, di_db[lbn])); 288 } 289 *frags = sblock.fs_frag; 290 291 for (i = 0, tmpval = NINDIR(&sblock), cur = UFS_NDADDR; i < UFS_NIADDR; 292 i++, tmpval *= NINDIR(&sblock), cur = next) { 293 next = cur + tmpval; 294 if (lbn == -cur - i) 295 return (DIP(dp, di_ib[i])); 296 /* 297 * Determine whether the lbn in question is within this tree. 298 */ 299 if (lbn < 0 && -lbn >= next) 300 continue; 301 if (lbn > 0 && lbn >= next) 302 continue; 303 return (indir_blkatoff(DIP(dp, di_ib[i]), ino, -cur - i, lbn, 304 bpp)); 305 } 306 pfatal("lbn %jd not in ino %ju\n", lbn, (uintmax_t)ino); 307 return (0); 308 } 309 310 /* 311 * Fetch an indirect block to find the block at a given lbn. The lbn 312 * may be negative to fetch a specific indirect block pointer or positive 313 * to fetch a specific block. 314 */ 315 static ufs2_daddr_t 316 indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn, 317 struct bufarea **bpp) 318 { 319 struct bufarea *bp; 320 ufs_lbn_t lbnadd; 321 ufs_lbn_t base; 322 int i, level; 323 324 if (blk == 0) 325 return (0); 326 level = lbn_level(cur); 327 if (level == -1) 328 pfatal("Invalid indir lbn %jd in ino %ju\n", 329 lbn, (uintmax_t)ino); 330 if (level == 0 && lbn < 0) 331 pfatal("Invalid lbn %jd in ino %ju\n", 332 lbn, (uintmax_t)ino); 333 lbnadd = 1; 334 base = -(cur + level); 335 for (i = level; i > 0; i--) 336 lbnadd *= NINDIR(&sblock); 337 if (lbn > 0) 338 i = (lbn - base) / lbnadd; 339 else 340 i = (-lbn - base) / lbnadd; 341 if (i < 0 || i >= NINDIR(&sblock)) { 342 pfatal("Invalid indirect index %d produced by lbn %jd " 343 "in ino %ju\n", i, lbn, (uintmax_t)ino); 344 return (0); 345 } 346 if (level == 0) 347 cur = base + (i * lbnadd); 348 else 349 cur = -(base + (i * lbnadd)) - (level - 1); 350 bp = getdatablk(blk, sblock.fs_bsize, BT_LEVEL1 + level); 351 if (bp->b_errs != 0) 352 return (0); 353 blk = IBLK(bp, i); 354 bp->b_index = i; 355 if (bpp != NULL) 356 *bpp = bp; 357 else 358 brelse(bp); 359 if (cur == lbn) 360 return (blk); 361 if (level == 0) 362 pfatal("Invalid lbn %jd at level 0 for ino %ju\n", lbn, 363 (uintmax_t)ino); 364 return (indir_blkatoff(blk, ino, cur, lbn, bpp)); 365 } 366 367 /* 368 * Check that a block in a legal block number. 369 * Return 0 if in range, 1 if out of range. 370 */ 371 int 372 chkrange(ufs2_daddr_t blk, int cnt) 373 { 374 int c; 375 376 if (cnt <= 0 || blk <= 0 || blk > maxfsblock || 377 cnt - 1 > maxfsblock - blk) 378 return (1); 379 if (cnt > sblock.fs_frag || 380 fragnum(&sblock, blk) + cnt > sblock.fs_frag) { 381 if (debug) 382 printf("bad size: blk %ld, offset %i, size %d\n", 383 (long)blk, (int)fragnum(&sblock, blk), cnt); 384 return (1); 385 } 386 c = dtog(&sblock, blk); 387 if (blk < cgdmin(&sblock, c)) { 388 if ((blk + cnt) > cgsblock(&sblock, c)) { 389 if (debug) { 390 printf("blk %ld < cgdmin %ld;", 391 (long)blk, (long)cgdmin(&sblock, c)); 392 printf(" blk + cnt %ld > cgsbase %ld\n", 393 (long)(blk + cnt), 394 (long)cgsblock(&sblock, c)); 395 } 396 return (1); 397 } 398 } else { 399 if ((blk + cnt) > cgbase(&sblock, c+1)) { 400 if (debug) { 401 printf("blk %ld >= cgdmin %ld;", 402 (long)blk, (long)cgdmin(&sblock, c)); 403 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n", 404 (long)(blk + cnt), (long)sblock.fs_fpg); 405 } 406 return (1); 407 } 408 } 409 return (0); 410 } 411 412 /* 413 * General purpose interface for reading inodes. 414 */ 415 void 416 ginode(ino_t inumber, struct inode *ip) 417 { 418 ufs2_daddr_t iblk; 419 static ino_t startinum = -1; 420 421 if (inumber < UFS_ROOTINO || inumber > maxino) 422 errx(EEXIT, "bad inode number %ju to ginode", 423 (uintmax_t)inumber); 424 ip->i_number = inumber; 425 if (startinum != -1 && 426 inumber >= startinum && inumber < startinum + INOPB(&sblock)) { 427 /* take an additional reference for the returned inode */ 428 icachebp->b_refcnt++; 429 } else { 430 iblk = ino_to_fsba(&sblock, inumber); 431 /* release our cache-hold reference on old icachebp */ 432 if (icachebp != NULL) 433 brelse(icachebp); 434 icachebp = getdatablk(iblk, sblock.fs_bsize, BT_INODES); 435 if (icachebp->b_errs != 0) { 436 ip->i_bp = NULL; 437 ip->i_dp = &zino; 438 return; 439 } 440 startinum = rounddown(inumber, INOPB(&sblock)); 441 /* take a cache-hold reference on new icachebp */ 442 icachebp->b_refcnt++; 443 icachebp->b_index = startinum; 444 } 445 ip->i_bp = icachebp; 446 if (sblock.fs_magic == FS_UFS1_MAGIC) { 447 ip->i_dp = (union dinode *) 448 &icachebp->b_un.b_dinode1[inumber % INOPB(&sblock)]; 449 return; 450 } 451 ip->i_dp = (union dinode *) 452 &icachebp->b_un.b_dinode2[inumber % INOPB(&sblock)]; 453 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)ip->i_dp)) { 454 pwarn("INODE CHECK-HASH FAILED"); 455 prtinode(ip); 456 if (preen || reply("FIX") != 0) { 457 if (preen) 458 printf(" (FIXED)\n"); 459 ffs_update_dinode_ckhash(&sblock, 460 (struct ufs2_dinode *)ip->i_dp); 461 inodirty(ip); 462 } 463 } 464 } 465 466 /* 467 * Release a held inode. 468 */ 469 void 470 irelse(struct inode *ip) 471 { 472 473 if (ip->i_bp->b_refcnt <= 0) 474 pfatal("irelse: releasing unreferenced ino %ju\n", 475 (uintmax_t) ip->i_number); 476 brelse(ip->i_bp); 477 } 478 479 /* 480 * Special purpose version of ginode used to optimize first pass 481 * over all the inodes in numerical order. 482 */ 483 static ino_t nextino, lastinum, lastvalidinum; 484 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 485 static struct bufarea inobuf; 486 487 union dinode * 488 getnextinode(ino_t inumber, int rebuildcg) 489 { 490 int j; 491 long size; 492 mode_t mode; 493 ufs2_daddr_t ndb, blk; 494 union dinode *dp; 495 struct inode ip; 496 static caddr_t nextinop; 497 498 if (inumber != nextino++ || inumber > lastvalidinum) 499 errx(EEXIT, "bad inode number %ju to nextinode", 500 (uintmax_t)inumber); 501 if (inumber >= lastinum) { 502 readcount++; 503 blk = ino_to_fsba(&sblock, lastinum); 504 if (readcount % readpercg == 0) { 505 size = partialsize; 506 lastinum += partialcnt; 507 } else { 508 size = inobufsize; 509 lastinum += fullcnt; 510 } 511 /* 512 * Flush old contents in case they have been updated. 513 * If getblk encounters an error, it will already have zeroed 514 * out the buffer, so we do not need to do so here. 515 */ 516 flush(fswritefd, &inobuf); 517 getblk(&inobuf, blk, size); 518 nextinop = inobuf.b_un.b_buf; 519 } 520 dp = (union dinode *)nextinop; 521 if (sblock.fs_magic == FS_UFS1_MAGIC) 522 nextinop += sizeof(struct ufs1_dinode); 523 else 524 nextinop += sizeof(struct ufs2_dinode); 525 if ((ckhashadd & CK_INODE) != 0) { 526 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp); 527 dirty(&inobuf); 528 } 529 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) { 530 pwarn("INODE CHECK-HASH FAILED"); 531 ip.i_bp = NULL; 532 ip.i_dp = dp; 533 ip.i_number = inumber; 534 prtinode(&ip); 535 if (preen || reply("FIX") != 0) { 536 if (preen) 537 printf(" (FIXED)\n"); 538 ffs_update_dinode_ckhash(&sblock, 539 (struct ufs2_dinode *)dp); 540 dirty(&inobuf); 541 } 542 } 543 if (rebuildcg && (char *)dp == inobuf.b_un.b_buf) { 544 /* 545 * Try to determine if we have reached the end of the 546 * allocated inodes. 547 */ 548 mode = DIP(dp, di_mode) & IFMT; 549 if (mode == 0) { 550 if (memcmp(dp->dp2.di_db, zino.dp2.di_db, 551 UFS_NDADDR * sizeof(ufs2_daddr_t)) || 552 memcmp(dp->dp2.di_ib, zino.dp2.di_ib, 553 UFS_NIADDR * sizeof(ufs2_daddr_t)) || 554 dp->dp2.di_mode || dp->dp2.di_size) 555 return (NULL); 556 return (dp); 557 } 558 if (!ftypeok(dp)) 559 return (NULL); 560 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize); 561 if (ndb < 0) 562 return (NULL); 563 if (mode == IFBLK || mode == IFCHR) 564 ndb++; 565 if (mode == IFLNK) { 566 /* 567 * Fake ndb value so direct/indirect block checks below 568 * will detect any garbage after symlink string. 569 */ 570 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) { 571 ndb = howmany(DIP(dp, di_size), 572 sizeof(ufs2_daddr_t)); 573 if (ndb > UFS_NDADDR) { 574 j = ndb - UFS_NDADDR; 575 for (ndb = 1; j > 1; j--) 576 ndb *= NINDIR(&sblock); 577 ndb += UFS_NDADDR; 578 } 579 } 580 } 581 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++) 582 if (DIP(dp, di_db[j]) != 0) 583 return (NULL); 584 for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++) 585 ndb /= NINDIR(&sblock); 586 for (; j < UFS_NIADDR; j++) 587 if (DIP(dp, di_ib[j]) != 0) 588 return (NULL); 589 } 590 return (dp); 591 } 592 593 void 594 setinodebuf(int cg, ino_t inosused) 595 { 596 ino_t inum; 597 598 inum = cg * sblock.fs_ipg; 599 lastvalidinum = inum + inosused - 1; 600 nextino = inum; 601 lastinum = inum; 602 readcount = 0; 603 if (inobuf.b_un.b_buf == NULL) { 604 inobufsize = blkroundup(&sblock, 605 MAX(INOBUFSIZE, sblock.fs_bsize)); 606 initbarea(&inobuf, BT_INODES); 607 if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL) 608 errx(EEXIT, "cannot allocate space for inode buffer"); 609 } 610 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ? 611 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 612 readpercg = inosused / fullcnt; 613 partialcnt = inosused % fullcnt; 614 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ? 615 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 616 if (partialcnt != 0) { 617 readpercg++; 618 } else { 619 partialcnt = fullcnt; 620 partialsize = inobufsize; 621 } 622 } 623 624 int 625 freeblock(struct inodesc *idesc) 626 { 627 struct dups *dlp; 628 ufs2_daddr_t blkno; 629 long nfrags, res; 630 631 res = KEEPON; 632 blkno = idesc->id_blkno; 633 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 634 if (chkrange(blkno, 1)) { 635 res = SKIP; 636 } else if (testbmap(blkno)) { 637 for (dlp = duplist; dlp; dlp = dlp->next) { 638 if (dlp->dup != blkno) 639 continue; 640 dlp->dup = duplist->dup; 641 dlp = duplist; 642 duplist = duplist->next; 643 free((char *)dlp); 644 break; 645 } 646 if (dlp == NULL) { 647 clrbmap(blkno); 648 n_blks--; 649 } 650 } 651 } 652 return (res); 653 } 654 655 void 656 freeinodebuf(void) 657 { 658 659 /* 660 * Flush old contents in case they have been updated. 661 */ 662 flush(fswritefd, &inobuf); 663 if (inobuf.b_un.b_buf != NULL) 664 free((char *)inobuf.b_un.b_buf); 665 inobuf.b_un.b_buf = NULL; 666 } 667 668 /* 669 * Routines to maintain information about directory inodes. 670 * This is built during the first pass and used during the 671 * second and third passes. 672 * 673 * Enter inodes into the cache. 674 */ 675 void 676 cacheino(union dinode *dp, ino_t inumber) 677 { 678 struct inoinfo *inp, **inpp; 679 int i, blks; 680 681 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR) 682 blks = UFS_NDADDR + UFS_NIADDR; 683 else if (DIP(dp, di_size) > 0) 684 blks = howmany(DIP(dp, di_size), sblock.fs_bsize); 685 else 686 blks = 1; 687 inp = (struct inoinfo *) 688 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t)); 689 if (inp == NULL) 690 errx(EEXIT, "cannot increase directory list"); 691 inpp = &inphead[inumber % dirhash]; 692 inp->i_nexthash = *inpp; 693 *inpp = inp; 694 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0; 695 inp->i_dotdot = (ino_t)0; 696 inp->i_number = inumber; 697 inp->i_isize = DIP(dp, di_size); 698 inp->i_numblks = blks; 699 for (i = 0; i < MIN(blks, UFS_NDADDR); i++) 700 inp->i_blks[i] = DIP(dp, di_db[i]); 701 if (blks > UFS_NDADDR) 702 for (i = 0; i < UFS_NIADDR; i++) 703 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]); 704 if (inplast == listmax) { 705 listmax += 100; 706 inpsort = (struct inoinfo **)reallocarray((char *)inpsort, 707 listmax, sizeof(struct inoinfo *)); 708 if (inpsort == NULL) 709 errx(EEXIT, "cannot increase directory list"); 710 } 711 inpsort[inplast++] = inp; 712 } 713 714 /* 715 * Look up an inode cache structure. 716 */ 717 struct inoinfo * 718 getinoinfo(ino_t inumber) 719 { 720 struct inoinfo *inp; 721 722 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) { 723 if (inp->i_number != inumber) 724 continue; 725 return (inp); 726 } 727 errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber); 728 return ((struct inoinfo *)0); 729 } 730 731 /* 732 * Clean up all the inode cache structure. 733 */ 734 void 735 inocleanup(void) 736 { 737 struct inoinfo **inpp; 738 739 if (inphead == NULL) 740 return; 741 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) 742 free((char *)(*inpp)); 743 free((char *)inphead); 744 free((char *)inpsort); 745 inphead = inpsort = NULL; 746 } 747 748 void 749 inodirty(struct inode *ip) 750 { 751 752 if (sblock.fs_magic == FS_UFS2_MAGIC) 753 ffs_update_dinode_ckhash(&sblock, 754 (struct ufs2_dinode *)ip->i_dp); 755 dirty(ip->i_bp); 756 } 757 758 void 759 clri(struct inodesc *idesc, const char *type, int flag) 760 { 761 union dinode *dp; 762 struct inode ip; 763 764 ginode(idesc->id_number, &ip); 765 dp = ip.i_dp; 766 if (flag == 1) { 767 pwarn("%s %s", type, 768 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"); 769 prtinode(&ip); 770 printf("\n"); 771 } 772 if (preen || reply("CLEAR") == 1) { 773 if (preen) 774 printf(" (CLEARED)\n"); 775 n_files--; 776 if (bkgrdflag == 0) { 777 (void)ckinode(dp, idesc); 778 inoinfo(idesc->id_number)->ino_state = USTATE; 779 clearinode(dp); 780 inodirty(&ip); 781 } else { 782 cmd.value = idesc->id_number; 783 cmd.size = -DIP(dp, di_nlink); 784 if (debug) 785 printf("adjrefcnt ino %ld amt %lld\n", 786 (long)cmd.value, (long long)cmd.size); 787 if (sysctl(adjrefcnt, MIBSIZE, 0, 0, 788 &cmd, sizeof cmd) == -1) 789 rwerror("ADJUST INODE", cmd.value); 790 } 791 } 792 irelse(&ip); 793 } 794 795 int 796 findname(struct inodesc *idesc) 797 { 798 struct direct *dirp = idesc->id_dirp; 799 800 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) { 801 idesc->id_entryno++; 802 return (KEEPON); 803 } 804 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1); 805 return (STOP|FOUND); 806 } 807 808 int 809 findino(struct inodesc *idesc) 810 { 811 struct direct *dirp = idesc->id_dirp; 812 813 if (dirp->d_ino == 0) 814 return (KEEPON); 815 if (strcmp(dirp->d_name, idesc->id_name) == 0 && 816 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) { 817 idesc->id_parent = dirp->d_ino; 818 return (STOP|FOUND); 819 } 820 return (KEEPON); 821 } 822 823 int 824 clearentry(struct inodesc *idesc) 825 { 826 struct direct *dirp = idesc->id_dirp; 827 828 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) { 829 idesc->id_entryno++; 830 return (KEEPON); 831 } 832 dirp->d_ino = 0; 833 return (STOP|FOUND|ALTERED); 834 } 835 836 void 837 prtinode(struct inode *ip) 838 { 839 char *p; 840 union dinode *dp; 841 struct passwd *pw; 842 time_t t; 843 844 dp = ip->i_dp; 845 printf(" I=%lu ", (u_long)ip->i_number); 846 if (ip->i_number < UFS_ROOTINO || ip->i_number > maxino) 847 return; 848 printf(" OWNER="); 849 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL) 850 printf("%s ", pw->pw_name); 851 else 852 printf("%u ", (unsigned)DIP(dp, di_uid)); 853 printf("MODE=%o\n", DIP(dp, di_mode)); 854 if (preen) 855 printf("%s: ", cdevname); 856 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size)); 857 t = DIP(dp, di_mtime); 858 p = ctime(&t); 859 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]); 860 } 861 862 void 863 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk) 864 { 865 866 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino); 867 printf("\n"); 868 switch (inoinfo(ino)->ino_state) { 869 870 case FSTATE: 871 case FZLINK: 872 inoinfo(ino)->ino_state = FCLEAR; 873 return; 874 875 case DSTATE: 876 case DZLINK: 877 inoinfo(ino)->ino_state = DCLEAR; 878 return; 879 880 case FCLEAR: 881 case DCLEAR: 882 return; 883 884 default: 885 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state); 886 /* NOTREACHED */ 887 } 888 } 889 890 /* 891 * allocate an unused inode 892 */ 893 ino_t 894 allocino(ino_t request, int type) 895 { 896 ino_t ino; 897 struct inode ip; 898 union dinode *dp; 899 struct bufarea *cgbp; 900 struct cg *cgp; 901 int cg; 902 903 if (request == 0) 904 request = UFS_ROOTINO; 905 else if (inoinfo(request)->ino_state != USTATE) 906 return (0); 907 for (ino = request; ino < maxino; ino++) 908 if (inoinfo(ino)->ino_state == USTATE) 909 break; 910 if (ino == maxino) 911 return (0); 912 cg = ino_to_cg(&sblock, ino); 913 cgbp = cglookup(cg); 914 cgp = cgbp->b_un.b_cg; 915 if (!check_cgmagic(cg, cgbp, 0)) 916 return (0); 917 setbit(cg_inosused(cgp), ino % sblock.fs_ipg); 918 cgp->cg_cs.cs_nifree--; 919 switch (type & IFMT) { 920 case IFDIR: 921 inoinfo(ino)->ino_state = DSTATE; 922 cgp->cg_cs.cs_ndir++; 923 break; 924 case IFREG: 925 case IFLNK: 926 inoinfo(ino)->ino_state = FSTATE; 927 break; 928 default: 929 return (0); 930 } 931 cgdirty(cgbp); 932 ginode(ino, &ip); 933 dp = ip.i_dp; 934 DIP_SET(dp, di_db[0], allocblk((long)1)); 935 if (DIP(dp, di_db[0]) == 0) { 936 inoinfo(ino)->ino_state = USTATE; 937 irelse(&ip); 938 return (0); 939 } 940 DIP_SET(dp, di_mode, type); 941 DIP_SET(dp, di_flags, 0); 942 DIP_SET(dp, di_atime, time(NULL)); 943 DIP_SET(dp, di_ctime, DIP(dp, di_atime)); 944 DIP_SET(dp, di_mtime, DIP(dp, di_ctime)); 945 DIP_SET(dp, di_mtimensec, 0); 946 DIP_SET(dp, di_ctimensec, 0); 947 DIP_SET(dp, di_atimensec, 0); 948 DIP_SET(dp, di_size, sblock.fs_fsize); 949 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize)); 950 n_files++; 951 inodirty(&ip); 952 irelse(&ip); 953 inoinfo(ino)->ino_type = IFTODT(type); 954 return (ino); 955 } 956 957 /* 958 * deallocate an inode 959 */ 960 void 961 freeino(ino_t ino) 962 { 963 struct inodesc idesc; 964 union dinode *dp; 965 struct inode ip; 966 967 memset(&idesc, 0, sizeof(struct inodesc)); 968 idesc.id_type = inoinfo(ino)->ino_idtype; 969 idesc.id_func = freeblock; 970 idesc.id_number = ino; 971 ginode(ino, &ip); 972 dp = ip.i_dp; 973 (void)ckinode(dp, &idesc); 974 clearinode(dp); 975 inodirty(&ip); 976 irelse(&ip); 977 inoinfo(ino)->ino_state = USTATE; 978 n_files--; 979 } 980