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 - 1); 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 if (bpp != NULL) 268 *bpp = NULL; 269 /* 270 * Handle extattr blocks first. 271 */ 272 if (lbn < 0 && lbn >= -UFS_NXADDR) { 273 lbn = -1 - lbn; 274 if (lbn > lblkno(&sblock, dp->dp2.di_extsize - 1)) 275 return (0); 276 *frags = numfrags(&sblock, 277 sblksize(&sblock, dp->dp2.di_extsize, lbn)); 278 return (dp->dp2.di_extb[lbn]); 279 } 280 /* 281 * Now direct and indirect. 282 */ 283 if (DIP(dp, di_mode) == IFLNK && 284 DIP(dp, di_size) < sblock.fs_maxsymlinklen) 285 return (0); 286 if (lbn >= 0 && lbn < UFS_NDADDR) { 287 *frags = numfrags(&sblock, 288 sblksize(&sblock, DIP(dp, di_size), lbn)); 289 return (DIP(dp, di_db[lbn])); 290 } 291 *frags = sblock.fs_frag; 292 293 for (i = 0, tmpval = NINDIR(&sblock), cur = UFS_NDADDR; i < UFS_NIADDR; 294 i++, tmpval *= NINDIR(&sblock), cur = next) { 295 next = cur + tmpval; 296 if (lbn == -cur - i) 297 return (DIP(dp, di_ib[i])); 298 /* 299 * Determine whether the lbn in question is within this tree. 300 */ 301 if (lbn < 0 && -lbn >= next) 302 continue; 303 if (lbn > 0 && lbn >= next) 304 continue; 305 if (DIP(dp, di_ib[i]) == 0) 306 return (0); 307 return (indir_blkatoff(DIP(dp, di_ib[i]), ino, -cur - i, lbn, 308 bpp)); 309 } 310 pfatal("lbn %jd not in ino %ju\n", lbn, (uintmax_t)ino); 311 return (0); 312 } 313 314 /* 315 * Fetch an indirect block to find the block at a given lbn. The lbn 316 * may be negative to fetch a specific indirect block pointer or positive 317 * to fetch a specific block. 318 */ 319 static ufs2_daddr_t 320 indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn, 321 struct bufarea **bpp) 322 { 323 struct bufarea *bp; 324 ufs_lbn_t lbnadd; 325 ufs_lbn_t base; 326 int i, level; 327 328 level = lbn_level(cur); 329 if (level == -1) 330 pfatal("Invalid indir lbn %jd in ino %ju\n", 331 lbn, (uintmax_t)ino); 332 if (level == 0 && lbn < 0) 333 pfatal("Invalid lbn %jd in ino %ju\n", 334 lbn, (uintmax_t)ino); 335 lbnadd = 1; 336 base = -(cur + level); 337 for (i = level; i > 0; i--) 338 lbnadd *= NINDIR(&sblock); 339 if (lbn > 0) 340 i = (lbn - base) / lbnadd; 341 else 342 i = (-lbn - base) / lbnadd; 343 if (i < 0 || i >= NINDIR(&sblock)) { 344 pfatal("Invalid indirect index %d produced by lbn %jd " 345 "in ino %ju\n", i, lbn, (uintmax_t)ino); 346 return (0); 347 } 348 if (level == 0) 349 cur = base + (i * lbnadd); 350 else 351 cur = -(base + (i * lbnadd)) - (level - 1); 352 bp = getdatablk(blk, sblock.fs_bsize, BT_LEVEL1 + level); 353 if (bp->b_errs != 0) 354 return (0); 355 blk = IBLK(bp, i); 356 bp->b_index = i; 357 if (cur == lbn || blk == 0) { 358 if (bpp != NULL) 359 *bpp = bp; 360 else 361 brelse(bp); 362 return (blk); 363 } 364 brelse(bp); 365 if (level == 0) 366 pfatal("Invalid lbn %jd at level 0 for ino %ju\n", lbn, 367 (uintmax_t)ino); 368 return (indir_blkatoff(blk, ino, cur, lbn, bpp)); 369 } 370 371 /* 372 * Check that a block in a legal block number. 373 * Return 0 if in range, 1 if out of range. 374 */ 375 int 376 chkrange(ufs2_daddr_t blk, int cnt) 377 { 378 int c; 379 380 if (cnt <= 0 || blk <= 0 || blk > maxfsblock || 381 cnt - 1 > maxfsblock - blk) 382 return (1); 383 if (cnt > sblock.fs_frag || 384 fragnum(&sblock, blk) + cnt > sblock.fs_frag) { 385 if (debug) 386 printf("bad size: blk %ld, offset %i, size %d\n", 387 (long)blk, (int)fragnum(&sblock, blk), cnt); 388 return (1); 389 } 390 c = dtog(&sblock, blk); 391 if (blk < cgdmin(&sblock, c)) { 392 if ((blk + cnt) > cgsblock(&sblock, c)) { 393 if (debug) { 394 printf("blk %ld < cgdmin %ld;", 395 (long)blk, (long)cgdmin(&sblock, c)); 396 printf(" blk + cnt %ld > cgsbase %ld\n", 397 (long)(blk + cnt), 398 (long)cgsblock(&sblock, c)); 399 } 400 return (1); 401 } 402 } else { 403 if ((blk + cnt) > cgbase(&sblock, c+1)) { 404 if (debug) { 405 printf("blk %ld >= cgdmin %ld;", 406 (long)blk, (long)cgdmin(&sblock, c)); 407 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n", 408 (long)(blk + cnt), (long)sblock.fs_fpg); 409 } 410 return (1); 411 } 412 } 413 return (0); 414 } 415 416 /* 417 * General purpose interface for reading inodes. 418 */ 419 void 420 ginode(ino_t inumber, struct inode *ip) 421 { 422 ufs2_daddr_t iblk; 423 424 if (inumber < UFS_ROOTINO || inumber > maxino) 425 errx(EEXIT, "bad inode number %ju to ginode", 426 (uintmax_t)inumber); 427 ip->i_number = inumber; 428 if (icachebp != NULL && 429 inumber >= icachebp->b_index && 430 inumber < icachebp->b_index + INOPB(&sblock)) { 431 /* take an additional reference for the returned inode */ 432 icachebp->b_refcnt++; 433 } else { 434 iblk = ino_to_fsba(&sblock, inumber); 435 /* release our cache-hold reference on old icachebp */ 436 if (icachebp != NULL) 437 brelse(icachebp); 438 icachebp = getdatablk(iblk, sblock.fs_bsize, BT_INODES); 439 if (icachebp->b_errs != 0) { 440 icachebp = NULL; 441 ip->i_bp = NULL; 442 ip->i_dp = &zino; 443 return; 444 } 445 /* take a cache-hold reference on new icachebp */ 446 icachebp->b_refcnt++; 447 icachebp->b_index = rounddown(inumber, INOPB(&sblock)); 448 } 449 ip->i_bp = icachebp; 450 if (sblock.fs_magic == FS_UFS1_MAGIC) { 451 ip->i_dp = (union dinode *) 452 &icachebp->b_un.b_dinode1[inumber % INOPB(&sblock)]; 453 return; 454 } 455 ip->i_dp = (union dinode *) 456 &icachebp->b_un.b_dinode2[inumber % INOPB(&sblock)]; 457 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)ip->i_dp)) { 458 pwarn("INODE CHECK-HASH FAILED"); 459 prtinode(ip); 460 if (preen || reply("FIX") != 0) { 461 if (preen) 462 printf(" (FIXED)\n"); 463 ffs_update_dinode_ckhash(&sblock, 464 (struct ufs2_dinode *)ip->i_dp); 465 inodirty(ip); 466 } 467 } 468 } 469 470 /* 471 * Release a held inode. 472 */ 473 void 474 irelse(struct inode *ip) 475 { 476 477 /* Check for failed inode read */ 478 if (ip->i_bp == NULL) 479 return; 480 if (ip->i_bp->b_refcnt <= 0) 481 pfatal("irelse: releasing unreferenced ino %ju\n", 482 (uintmax_t) ip->i_number); 483 brelse(ip->i_bp); 484 } 485 486 /* 487 * Special purpose version of ginode used to optimize first pass 488 * over all the inodes in numerical order. 489 */ 490 static ino_t nextino, lastinum, lastvalidinum; 491 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 492 static struct bufarea inobuf; 493 494 union dinode * 495 getnextinode(ino_t inumber, int rebuildcg) 496 { 497 int j; 498 long size; 499 mode_t mode; 500 ufs2_daddr_t ndb, blk; 501 union dinode *dp; 502 struct inode ip; 503 static caddr_t nextinop; 504 505 if (inumber != nextino++ || inumber > lastvalidinum) 506 errx(EEXIT, "bad inode number %ju to nextinode", 507 (uintmax_t)inumber); 508 if (inumber >= lastinum) { 509 readcount++; 510 blk = ino_to_fsba(&sblock, lastinum); 511 if (readcount % readpercg == 0) { 512 size = partialsize; 513 lastinum += partialcnt; 514 } else { 515 size = inobufsize; 516 lastinum += fullcnt; 517 } 518 /* 519 * Flush old contents in case they have been updated. 520 * If getblk encounters an error, it will already have zeroed 521 * out the buffer, so we do not need to do so here. 522 */ 523 flush(fswritefd, &inobuf); 524 getblk(&inobuf, blk, size); 525 nextinop = inobuf.b_un.b_buf; 526 } 527 dp = (union dinode *)nextinop; 528 if (sblock.fs_magic == FS_UFS1_MAGIC) 529 nextinop += sizeof(struct ufs1_dinode); 530 else 531 nextinop += sizeof(struct ufs2_dinode); 532 if ((ckhashadd & CK_INODE) != 0) { 533 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp); 534 dirty(&inobuf); 535 } 536 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) { 537 pwarn("INODE CHECK-HASH FAILED"); 538 ip.i_bp = NULL; 539 ip.i_dp = dp; 540 ip.i_number = inumber; 541 prtinode(&ip); 542 if (preen || reply("FIX") != 0) { 543 if (preen) 544 printf(" (FIXED)\n"); 545 ffs_update_dinode_ckhash(&sblock, 546 (struct ufs2_dinode *)dp); 547 dirty(&inobuf); 548 } 549 } 550 if (rebuildcg && (char *)dp == inobuf.b_un.b_buf) { 551 /* 552 * Try to determine if we have reached the end of the 553 * allocated inodes. 554 */ 555 mode = DIP(dp, di_mode) & IFMT; 556 if (mode == 0) { 557 if (memcmp(dp->dp2.di_db, zino.dp2.di_db, 558 UFS_NDADDR * sizeof(ufs2_daddr_t)) || 559 memcmp(dp->dp2.di_ib, zino.dp2.di_ib, 560 UFS_NIADDR * sizeof(ufs2_daddr_t)) || 561 dp->dp2.di_mode || dp->dp2.di_size) 562 return (NULL); 563 return (dp); 564 } 565 if (!ftypeok(dp)) 566 return (NULL); 567 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize); 568 if (ndb < 0) 569 return (NULL); 570 if (mode == IFBLK || mode == IFCHR) 571 ndb++; 572 if (mode == IFLNK) { 573 /* 574 * Fake ndb value so direct/indirect block checks below 575 * will detect any garbage after symlink string. 576 */ 577 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) { 578 ndb = howmany(DIP(dp, di_size), 579 sizeof(ufs2_daddr_t)); 580 if (ndb > UFS_NDADDR) { 581 j = ndb - UFS_NDADDR; 582 for (ndb = 1; j > 1; j--) 583 ndb *= NINDIR(&sblock); 584 ndb += UFS_NDADDR; 585 } 586 } 587 } 588 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++) 589 if (DIP(dp, di_db[j]) != 0) 590 return (NULL); 591 for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++) 592 ndb /= NINDIR(&sblock); 593 for (; j < UFS_NIADDR; j++) 594 if (DIP(dp, di_ib[j]) != 0) 595 return (NULL); 596 } 597 return (dp); 598 } 599 600 void 601 setinodebuf(int cg, ino_t inosused) 602 { 603 ino_t inum; 604 605 inum = cg * sblock.fs_ipg; 606 lastvalidinum = inum + inosused - 1; 607 nextino = inum; 608 lastinum = inum; 609 readcount = 0; 610 /* Flush old contents in case they have been updated */ 611 flush(fswritefd, &inobuf); 612 inobuf.b_bno = 0; 613 if (inobuf.b_un.b_buf == NULL) { 614 inobufsize = blkroundup(&sblock, 615 MAX(INOBUFSIZE, sblock.fs_bsize)); 616 initbarea(&inobuf, BT_INODES); 617 if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL) 618 errx(EEXIT, "cannot allocate space for inode buffer"); 619 } 620 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ? 621 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 622 readpercg = inosused / fullcnt; 623 partialcnt = inosused % fullcnt; 624 partialsize = fragroundup(&sblock, 625 partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ? 626 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode))); 627 if (partialcnt != 0) { 628 readpercg++; 629 } else { 630 partialcnt = fullcnt; 631 partialsize = inobufsize; 632 } 633 } 634 635 int 636 freeblock(struct inodesc *idesc) 637 { 638 struct dups *dlp; 639 ufs2_daddr_t blkno; 640 long nfrags, res; 641 642 res = KEEPON; 643 blkno = idesc->id_blkno; 644 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 645 if (chkrange(blkno, 1)) { 646 res = SKIP; 647 } else if (testbmap(blkno)) { 648 for (dlp = duplist; dlp; dlp = dlp->next) { 649 if (dlp->dup != blkno) 650 continue; 651 dlp->dup = duplist->dup; 652 dlp = duplist; 653 duplist = duplist->next; 654 free((char *)dlp); 655 break; 656 } 657 if (dlp == NULL) { 658 clrbmap(blkno); 659 n_blks--; 660 } 661 } 662 } 663 return (res); 664 } 665 666 void 667 freeinodebuf(void) 668 { 669 670 /* 671 * Flush old contents in case they have been updated. 672 */ 673 flush(fswritefd, &inobuf); 674 if (inobuf.b_un.b_buf != NULL) 675 free((char *)inobuf.b_un.b_buf); 676 inobuf.b_un.b_buf = NULL; 677 } 678 679 /* 680 * Routines to maintain information about directory inodes. 681 * This is built during the first pass and used during the 682 * second and third passes. 683 * 684 * Enter inodes into the cache. 685 */ 686 void 687 cacheino(union dinode *dp, ino_t inumber) 688 { 689 struct inoinfo *inp, **inpp; 690 int i, blks; 691 692 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR) 693 blks = UFS_NDADDR + UFS_NIADDR; 694 else if (DIP(dp, di_size) > 0) 695 blks = howmany(DIP(dp, di_size), sblock.fs_bsize); 696 else 697 blks = 1; 698 inp = (struct inoinfo *) 699 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t)); 700 if (inp == NULL) 701 errx(EEXIT, "cannot increase directory list"); 702 inpp = &inphead[inumber % dirhash]; 703 inp->i_nexthash = *inpp; 704 *inpp = inp; 705 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0; 706 inp->i_dotdot = (ino_t)0; 707 inp->i_number = inumber; 708 inp->i_isize = DIP(dp, di_size); 709 inp->i_numblks = blks; 710 for (i = 0; i < MIN(blks, UFS_NDADDR); i++) 711 inp->i_blks[i] = DIP(dp, di_db[i]); 712 if (blks > UFS_NDADDR) 713 for (i = 0; i < UFS_NIADDR; i++) 714 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]); 715 if (inplast == listmax) { 716 listmax += 100; 717 inpsort = (struct inoinfo **)reallocarray((char *)inpsort, 718 listmax, sizeof(struct inoinfo *)); 719 if (inpsort == NULL) 720 errx(EEXIT, "cannot increase directory list"); 721 } 722 inpsort[inplast++] = inp; 723 } 724 725 /* 726 * Look up an inode cache structure. 727 */ 728 struct inoinfo * 729 getinoinfo(ino_t inumber) 730 { 731 struct inoinfo *inp; 732 733 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) { 734 if (inp->i_number != inumber) 735 continue; 736 return (inp); 737 } 738 errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber); 739 return ((struct inoinfo *)0); 740 } 741 742 /* 743 * Clean up all the inode cache structure. 744 */ 745 void 746 inocleanup(void) 747 { 748 struct inoinfo **inpp; 749 750 if (inphead == NULL) 751 return; 752 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) 753 free((char *)(*inpp)); 754 free((char *)inphead); 755 free((char *)inpsort); 756 inphead = inpsort = NULL; 757 } 758 759 void 760 inodirty(struct inode *ip) 761 { 762 763 if (sblock.fs_magic == FS_UFS2_MAGIC) 764 ffs_update_dinode_ckhash(&sblock, 765 (struct ufs2_dinode *)ip->i_dp); 766 dirty(ip->i_bp); 767 } 768 769 void 770 clri(struct inodesc *idesc, const char *type, int flag) 771 { 772 union dinode *dp; 773 struct inode ip; 774 775 ginode(idesc->id_number, &ip); 776 dp = ip.i_dp; 777 if (flag == 1) { 778 pwarn("%s %s", type, 779 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"); 780 prtinode(&ip); 781 printf("\n"); 782 } 783 if (preen || reply("CLEAR") == 1) { 784 if (preen) 785 printf(" (CLEARED)\n"); 786 n_files--; 787 if (bkgrdflag == 0) { 788 (void)ckinode(dp, idesc); 789 inoinfo(idesc->id_number)->ino_state = USTATE; 790 clearinode(dp); 791 inodirty(&ip); 792 } else { 793 cmd.value = idesc->id_number; 794 cmd.size = -DIP(dp, di_nlink); 795 if (debug) 796 printf("adjrefcnt ino %ld amt %lld\n", 797 (long)cmd.value, (long long)cmd.size); 798 if (sysctl(adjrefcnt, MIBSIZE, 0, 0, 799 &cmd, sizeof cmd) == -1) 800 rwerror("ADJUST INODE", cmd.value); 801 } 802 } 803 irelse(&ip); 804 } 805 806 int 807 findname(struct inodesc *idesc) 808 { 809 struct direct *dirp = idesc->id_dirp; 810 811 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) { 812 idesc->id_entryno++; 813 return (KEEPON); 814 } 815 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1); 816 return (STOP|FOUND); 817 } 818 819 int 820 findino(struct inodesc *idesc) 821 { 822 struct direct *dirp = idesc->id_dirp; 823 824 if (dirp->d_ino == 0) 825 return (KEEPON); 826 if (strcmp(dirp->d_name, idesc->id_name) == 0 && 827 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) { 828 idesc->id_parent = dirp->d_ino; 829 return (STOP|FOUND); 830 } 831 return (KEEPON); 832 } 833 834 int 835 clearentry(struct inodesc *idesc) 836 { 837 struct direct *dirp = idesc->id_dirp; 838 839 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) { 840 idesc->id_entryno++; 841 return (KEEPON); 842 } 843 dirp->d_ino = 0; 844 return (STOP|FOUND|ALTERED); 845 } 846 847 void 848 prtinode(struct inode *ip) 849 { 850 char *p; 851 union dinode *dp; 852 struct passwd *pw; 853 time_t t; 854 855 dp = ip->i_dp; 856 printf(" I=%lu ", (u_long)ip->i_number); 857 if (ip->i_number < UFS_ROOTINO || ip->i_number > maxino) 858 return; 859 printf(" OWNER="); 860 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL) 861 printf("%s ", pw->pw_name); 862 else 863 printf("%u ", (unsigned)DIP(dp, di_uid)); 864 printf("MODE=%o\n", DIP(dp, di_mode)); 865 if (preen) 866 printf("%s: ", cdevname); 867 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size)); 868 t = DIP(dp, di_mtime); 869 p = ctime(&t); 870 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]); 871 } 872 873 void 874 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk) 875 { 876 877 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino); 878 printf("\n"); 879 switch (inoinfo(ino)->ino_state) { 880 881 case FSTATE: 882 case FZLINK: 883 inoinfo(ino)->ino_state = FCLEAR; 884 return; 885 886 case DSTATE: 887 case DZLINK: 888 inoinfo(ino)->ino_state = DCLEAR; 889 return; 890 891 case FCLEAR: 892 case DCLEAR: 893 return; 894 895 default: 896 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state); 897 /* NOTREACHED */ 898 } 899 } 900 901 /* 902 * allocate an unused inode 903 */ 904 ino_t 905 allocino(ino_t request, int type) 906 { 907 ino_t ino; 908 struct inode ip; 909 union dinode *dp; 910 struct bufarea *cgbp; 911 struct cg *cgp; 912 int cg, anyino; 913 914 anyino = 0; 915 if (request == 0) { 916 request = UFS_ROOTINO; 917 anyino = 1; 918 } else if (inoinfo(request)->ino_state != USTATE) 919 return (0); 920 retry: 921 for (ino = request; ino < maxino; ino++) 922 if (inoinfo(ino)->ino_state == USTATE) 923 break; 924 if (ino >= maxino) 925 return (0); 926 cg = ino_to_cg(&sblock, ino); 927 cgbp = cglookup(cg); 928 cgp = cgbp->b_un.b_cg; 929 if (!check_cgmagic(cg, cgbp, 0)) { 930 if (anyino == 0) 931 return (0); 932 request = (cg + 1) * sblock.fs_ipg; 933 goto retry; 934 } 935 setbit(cg_inosused(cgp), ino % sblock.fs_ipg); 936 cgp->cg_cs.cs_nifree--; 937 switch (type & IFMT) { 938 case IFDIR: 939 inoinfo(ino)->ino_state = DSTATE; 940 cgp->cg_cs.cs_ndir++; 941 break; 942 case IFREG: 943 case IFLNK: 944 inoinfo(ino)->ino_state = FSTATE; 945 break; 946 default: 947 return (0); 948 } 949 cgdirty(cgbp); 950 ginode(ino, &ip); 951 dp = ip.i_dp; 952 DIP_SET(dp, di_db[0], allocblk((long)1)); 953 if (DIP(dp, di_db[0]) == 0) { 954 inoinfo(ino)->ino_state = USTATE; 955 irelse(&ip); 956 return (0); 957 } 958 DIP_SET(dp, di_mode, type); 959 DIP_SET(dp, di_flags, 0); 960 DIP_SET(dp, di_atime, time(NULL)); 961 DIP_SET(dp, di_ctime, DIP(dp, di_atime)); 962 DIP_SET(dp, di_mtime, DIP(dp, di_ctime)); 963 DIP_SET(dp, di_mtimensec, 0); 964 DIP_SET(dp, di_ctimensec, 0); 965 DIP_SET(dp, di_atimensec, 0); 966 DIP_SET(dp, di_size, sblock.fs_fsize); 967 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize)); 968 n_files++; 969 inodirty(&ip); 970 irelse(&ip); 971 inoinfo(ino)->ino_type = IFTODT(type); 972 return (ino); 973 } 974 975 /* 976 * deallocate an inode 977 */ 978 void 979 freeino(ino_t ino) 980 { 981 struct inodesc idesc; 982 union dinode *dp; 983 struct inode ip; 984 985 memset(&idesc, 0, sizeof(struct inodesc)); 986 idesc.id_type = inoinfo(ino)->ino_idtype; 987 idesc.id_func = freeblock; 988 idesc.id_number = ino; 989 ginode(ino, &ip); 990 dp = ip.i_dp; 991 (void)ckinode(dp, &idesc); 992 clearinode(dp); 993 inodirty(&ip); 994 irelse(&ip); 995 inoinfo(ino)->ino_state = USTATE; 996 n_files--; 997 } 998