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 #include <sys/param.h> 33 #include <sys/stat.h> 34 #include <sys/stdint.h> 35 #include <sys/sysctl.h> 36 37 #include <ufs/ufs/dinode.h> 38 #include <ufs/ufs/dir.h> 39 #include <ufs/ffs/fs.h> 40 41 #include <err.h> 42 #include <pwd.h> 43 #include <string.h> 44 #include <time.h> 45 46 #include "fsck.h" 47 48 struct bufarea *icachebp; /* inode cache buffer */ 49 50 static int iblock(struct inodesc *, off_t isize, int type); 51 static ufs2_daddr_t indir_blkatoff(ufs2_daddr_t, ino_t, ufs_lbn_t, ufs_lbn_t, 52 struct bufarea **); 53 static int snapclean(struct inodesc *idesc); 54 static void chkcopyonwrite(struct fs *, ufs2_daddr_t, 55 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long)); 56 57 int 58 ckinode(union dinode *dp, struct inodesc *idesc) 59 { 60 off_t remsize, sizepb; 61 int i, offset, ret; 62 struct inode ip; 63 union dinode dino; 64 ufs2_daddr_t ndb; 65 mode_t mode; 66 char pathbuf[MAXPATHLEN + 1]; 67 68 if (idesc->id_fix != IGNORE) 69 idesc->id_fix = DONTKNOW; 70 idesc->id_dp = dp; 71 idesc->id_lbn = -1; 72 idesc->id_lballoc = -1; 73 idesc->id_level = 0; 74 idesc->id_entryno = 0; 75 idesc->id_filesize = DIP(dp, di_size); 76 mode = DIP(dp, di_mode) & IFMT; 77 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK && 78 DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen)) 79 return (KEEPON); 80 if (sblock.fs_magic == FS_UFS1_MAGIC) 81 dino.dp1 = dp->dp1; 82 else 83 dino.dp2 = dp->dp2; 84 if (DIP(&dino, di_size) < 0) { 85 pfatal("NEGATIVE INODE SIZE %jd\n", DIP(&dino, di_size)); 86 return (STOP); 87 } 88 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize); 89 for (i = 0; i < UFS_NDADDR; i++) { 90 idesc->id_lbn++; 91 if (--ndb == 0 && 92 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0) 93 idesc->id_numfrags = 94 numfrags(&sblock, fragroundup(&sblock, offset)); 95 else 96 idesc->id_numfrags = sblock.fs_frag; 97 if (DIP(&dino, di_db[i]) == 0) { 98 if (idesc->id_type == DATA && ndb >= 0) { 99 /* An empty block in a directory XXX */ 100 getpathname(pathbuf, idesc->id_number, 101 idesc->id_number); 102 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 103 pathbuf); 104 if (reply("ADJUST LENGTH") == 1) { 105 ginode(idesc->id_number, &ip); 106 DIP_SET(ip.i_dp, di_size, 107 i * sblock.fs_bsize); 108 printf( 109 "YOU MUST RERUN FSCK AFTERWARDS\n"); 110 rerun = 1; 111 inodirty(&ip); 112 irelse(&ip); 113 } 114 return (STOP); 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 > maxfsblock - blk) { 382 if (debug) 383 printf("out of range: blk %ld, offset %i, size %d\n", 384 (long)blk, (int)fragnum(&sblock, blk), cnt); 385 return (1); 386 } 387 if (cnt > sblock.fs_frag || 388 fragnum(&sblock, blk) + cnt > sblock.fs_frag) { 389 if (debug) 390 printf("bad size: blk %ld, offset %i, size %d\n", 391 (long)blk, (int)fragnum(&sblock, blk), cnt); 392 return (1); 393 } 394 c = dtog(&sblock, blk); 395 if (blk < cgdmin(&sblock, c)) { 396 if ((blk + cnt) > cgsblock(&sblock, c)) { 397 if (debug) { 398 printf("blk %ld < cgdmin %ld;", 399 (long)blk, (long)cgdmin(&sblock, c)); 400 printf(" blk + cnt %ld > cgsbase %ld\n", 401 (long)(blk + cnt), 402 (long)cgsblock(&sblock, c)); 403 } 404 return (1); 405 } 406 } else { 407 if ((blk + cnt) > cgbase(&sblock, c+1)) { 408 if (debug) { 409 printf("blk %ld >= cgdmin %ld;", 410 (long)blk, (long)cgdmin(&sblock, c)); 411 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n", 412 (long)(blk + cnt), (long)sblock.fs_fpg); 413 } 414 return (1); 415 } 416 } 417 return (0); 418 } 419 420 /* 421 * General purpose interface for reading inodes. 422 * 423 * firstinum and lastinum track contents of getnextino() cache (below). 424 */ 425 static ino_t firstinum, lastinum; 426 static struct bufarea inobuf; 427 428 void 429 ginode(ino_t inumber, struct inode *ip) 430 { 431 ufs2_daddr_t iblk; 432 union dinodep dpp; 433 struct ufs2_dinode *dp; 434 435 if (inumber < UFS_ROOTINO || inumber >= maxino) 436 errx(EEXIT, "bad inode number %ju to ginode", 437 (uintmax_t)inumber); 438 ip->i_number = inumber; 439 if (inumber >= firstinum && inumber < lastinum) { 440 /* contents in getnextino() cache */ 441 ip->i_bp = &inobuf; 442 inobuf.b_refcnt++; 443 inobuf.b_index = firstinum; 444 } else if (icachebp != NULL && 445 inumber >= icachebp->b_index && 446 inumber < icachebp->b_index + INOPB(&sblock)) { 447 /* take an additional reference for the returned inode */ 448 icachebp->b_refcnt++; 449 ip->i_bp = icachebp; 450 } else { 451 iblk = ino_to_fsba(&sblock, inumber); 452 /* release our cache-hold reference on old icachebp */ 453 if (icachebp != NULL) 454 brelse(icachebp); 455 icachebp = getdatablk(iblk, sblock.fs_bsize, BT_INODES); 456 if (icachebp->b_errs != 0) { 457 icachebp = NULL; 458 ip->i_bp = NULL; 459 ip->i_dp = &zino; 460 return; 461 } 462 /* take a cache-hold reference on new icachebp */ 463 icachebp->b_refcnt++; 464 icachebp->b_index = rounddown(inumber, INOPB(&sblock)); 465 ip->i_bp = icachebp; 466 } 467 if (sblock.fs_magic == FS_UFS1_MAGIC) { 468 ip->i_dp = (union dinode *) 469 &ip->i_bp->b_un.b_dinode1[inumber - ip->i_bp->b_index]; 470 dpp.dp1 = (struct ufs1_dinode *)ip->i_dp; 471 return; 472 } 473 ip->i_dp = (union dinode *) 474 &ip->i_bp->b_un.b_dinode2[inumber - ip->i_bp->b_index]; 475 dpp.dp2 = dp = (struct ufs2_dinode *)ip->i_dp; 476 /* Do not check hash of inodes being created */ 477 if (dp->di_mode != 0 && ffs_verify_dinode_ckhash(&sblock, dp)) { 478 pwarn("INODE CHECK-HASH FAILED"); 479 prtinode(ip); 480 if (preen || reply("FIX") != 0) { 481 if (preen) 482 printf(" (FIXED)\n"); 483 ffs_update_dinode_ckhash(&sblock, dp); 484 inodirty(ip); 485 } 486 } 487 } 488 489 /* 490 * Release a held inode. 491 */ 492 void 493 irelse(struct inode *ip) 494 { 495 496 /* Check for failed inode read */ 497 if (ip->i_bp == NULL) 498 return; 499 if (debug && sblock.fs_magic == FS_UFS2_MAGIC && 500 ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)ip->i_dp)) { 501 pwarn("irelse: releasing inode with bad check-hash"); 502 prtinode(ip); 503 } 504 if (ip->i_bp->b_refcnt <= 0) 505 pfatal("irelse: releasing unreferenced ino %ju\n", 506 (uintmax_t) ip->i_number); 507 brelse(ip->i_bp); 508 } 509 510 /* 511 * Special purpose version of ginode used to optimize first pass 512 * over all the inodes in numerical order. 513 */ 514 static ino_t nextinum, lastvalidinum; 515 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 516 517 union dinode * 518 getnextinode(ino_t inumber, int rebuiltcg) 519 { 520 int j; 521 long size; 522 mode_t mode; 523 ufs2_daddr_t ndb, blk; 524 union dinode *dp; 525 union dinodep dpp; 526 struct inode ip; 527 static caddr_t nextinop; 528 529 if (inumber != nextinum++ || inumber > lastvalidinum) 530 errx(EEXIT, "bad inode number %ju to nextinode", 531 (uintmax_t)inumber); 532 if (inumber >= lastinum) { 533 readcount++; 534 firstinum = lastinum; 535 blk = ino_to_fsba(&sblock, lastinum); 536 if (readcount % readpercg == 0) { 537 size = partialsize; 538 lastinum += partialcnt; 539 } else { 540 size = inobufsize; 541 lastinum += fullcnt; 542 } 543 /* 544 * Flush old contents in case they have been updated. 545 * If getblk encounters an error, it will already have zeroed 546 * out the buffer, so we do not need to do so here. 547 */ 548 if (inobuf.b_refcnt != 0) 549 pfatal("Non-zero getnextinode() ref count %d\n", 550 inobuf.b_refcnt); 551 flush(fswritefd, &inobuf); 552 getblk(&inobuf, blk, size); 553 nextinop = inobuf.b_un.b_buf; 554 } 555 dp = (union dinode *)nextinop; 556 if (sblock.fs_magic == FS_UFS1_MAGIC) { 557 nextinop += sizeof(struct ufs1_dinode); 558 dpp.dp1 = (struct ufs1_dinode *)dp; 559 } else { 560 nextinop += sizeof(struct ufs2_dinode); 561 dpp.dp2 = (struct ufs2_dinode *)dp; 562 } 563 if ((ckhashadd & CK_INODE) != 0) { 564 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp); 565 dirty(&inobuf); 566 } 567 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) { 568 pwarn("INODE CHECK-HASH FAILED"); 569 ip.i_bp = NULL; 570 ip.i_dp = dp; 571 ip.i_number = inumber; 572 prtinode(&ip); 573 if (preen || reply("FIX") != 0) { 574 if (preen) 575 printf(" (FIXED)\n"); 576 ffs_update_dinode_ckhash(&sblock, 577 (struct ufs2_dinode *)dp); 578 dirty(&inobuf); 579 } 580 } 581 if (rebuiltcg && (char *)dp == inobuf.b_un.b_buf) { 582 /* 583 * Try to determine if we have reached the end of the 584 * allocated inodes. 585 */ 586 mode = DIP(dp, di_mode) & IFMT; 587 if (mode == 0) { 588 if (memcmp(dp->dp2.di_db, zino.dp2.di_db, 589 UFS_NDADDR * sizeof(ufs2_daddr_t)) || 590 memcmp(dp->dp2.di_ib, zino.dp2.di_ib, 591 UFS_NIADDR * sizeof(ufs2_daddr_t)) || 592 dp->dp2.di_mode || dp->dp2.di_size) 593 return (NULL); 594 return (dp); 595 } 596 if (!ftypeok(dp)) 597 return (NULL); 598 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize); 599 if (ndb < 0) 600 return (NULL); 601 if (mode == IFBLK || mode == IFCHR) 602 ndb++; 603 if (mode == IFLNK) { 604 /* 605 * Fake ndb value so direct/indirect block checks below 606 * will detect any garbage after symlink string. 607 */ 608 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) { 609 ndb = howmany(DIP(dp, di_size), 610 sizeof(ufs2_daddr_t)); 611 if (ndb > UFS_NDADDR) { 612 j = ndb - UFS_NDADDR; 613 for (ndb = 1; j > 1; j--) 614 ndb *= NINDIR(&sblock); 615 ndb += UFS_NDADDR; 616 } 617 } 618 } 619 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++) 620 if (DIP(dp, di_db[j]) != 0) 621 return (NULL); 622 for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++) 623 ndb /= NINDIR(&sblock); 624 for (; j < UFS_NIADDR; j++) 625 if (DIP(dp, di_ib[j]) != 0) 626 return (NULL); 627 } 628 return (dp); 629 } 630 631 void 632 setinodebuf(int cg, ino_t inosused) 633 { 634 ino_t inum; 635 636 inum = cg * sblock.fs_ipg; 637 lastvalidinum = inum + inosused - 1; 638 nextinum = inum; 639 lastinum = inum; 640 readcount = 0; 641 /* Flush old contents in case they have been updated */ 642 flush(fswritefd, &inobuf); 643 inobuf.b_bno = 0; 644 if (inobuf.b_un.b_buf == NULL) { 645 inobufsize = blkroundup(&sblock, 646 MAX(INOBUFSIZE, sblock.fs_bsize)); 647 initbarea(&inobuf, BT_INODES); 648 if ((inobuf.b_un.b_buf = Balloc((unsigned)inobufsize)) == NULL) 649 errx(EEXIT, "cannot allocate space for inode buffer"); 650 } 651 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ? 652 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 653 readpercg = inosused / fullcnt; 654 partialcnt = inosused % fullcnt; 655 partialsize = fragroundup(&sblock, 656 partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ? 657 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode))); 658 if (partialcnt != 0) { 659 readpercg++; 660 } else { 661 partialcnt = fullcnt; 662 partialsize = inobufsize; 663 } 664 } 665 666 int 667 freeblock(struct inodesc *idesc) 668 { 669 struct dups *dlp; 670 struct bufarea *cgbp; 671 struct cg *cgp; 672 ufs2_daddr_t blkno; 673 long size, nfrags; 674 675 blkno = idesc->id_blkno; 676 if (idesc->id_type == SNAP) { 677 pfatal("clearing a snapshot dinode\n"); 678 return (STOP); 679 } 680 size = lfragtosize(&sblock, idesc->id_numfrags); 681 if (snapblkfree(&sblock, blkno, size, idesc->id_number, 682 std_checkblkavail)) 683 return (KEEPON); 684 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 685 if (chkrange(blkno, 1)) { 686 return (SKIP); 687 } else if (testbmap(blkno)) { 688 for (dlp = duplist; dlp; dlp = dlp->next) { 689 if (dlp->dup != blkno) 690 continue; 691 dlp->dup = duplist->dup; 692 dlp = duplist; 693 duplist = duplist->next; 694 free((char *)dlp); 695 break; 696 } 697 if (dlp == NULL) { 698 clrbmap(blkno); 699 n_blks--; 700 } 701 } 702 } 703 /* 704 * If all successfully returned, account for them. 705 */ 706 if (nfrags == 0) { 707 cgbp = cglookup(dtog(&sblock, idesc->id_blkno)); 708 cgp = cgbp->b_un.b_cg; 709 if (idesc->id_numfrags == sblock.fs_frag) 710 cgp->cg_cs.cs_nbfree++; 711 else 712 cgp->cg_cs.cs_nffree += idesc->id_numfrags; 713 cgdirty(cgbp); 714 } 715 return (KEEPON); 716 } 717 718 /* 719 * Prepare a snapshot file for being removed. 720 */ 721 void 722 snapremove(ino_t inum) 723 { 724 struct inodesc idesc; 725 struct inode ip; 726 int i; 727 728 for (i = 0; i < snapcnt; i++) 729 if (snaplist[i].i_number == inum) 730 break; 731 if (i == snapcnt) 732 ginode(inum, &ip); 733 else 734 ip = snaplist[i]; 735 if ((DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) == 0) { 736 printf("snapremove: inode %jd is not a snapshot\n", 737 (intmax_t)inum); 738 if (i == snapcnt) 739 irelse(&ip); 740 return; 741 } 742 if (debug) 743 printf("snapremove: remove %sactive snapshot %jd\n", 744 i == snapcnt ? "in" : "", (intmax_t)inum); 745 /* 746 * If on active snapshot list, remove it. 747 */ 748 if (i < snapcnt) { 749 for (i++; i < FSMAXSNAP; i++) { 750 if (sblock.fs_snapinum[i] == 0) 751 break; 752 snaplist[i - 1] = snaplist[i]; 753 sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i]; 754 } 755 sblock.fs_snapinum[i - 1] = 0; 756 bzero(&snaplist[i - 1], sizeof(struct inode)); 757 snapcnt--; 758 } 759 memset(&idesc, 0, sizeof(struct inodesc)); 760 idesc.id_type = SNAP; 761 idesc.id_func = snapclean; 762 idesc.id_number = inum; 763 (void)ckinode(ip.i_dp, &idesc); 764 DIP_SET(ip.i_dp, di_flags, DIP(ip.i_dp, di_flags) & ~SF_SNAPSHOT); 765 inodirty(&ip); 766 irelse(&ip); 767 } 768 769 static int 770 snapclean(struct inodesc *idesc) 771 { 772 ufs2_daddr_t blkno; 773 struct bufarea *bp; 774 union dinode *dp; 775 776 blkno = idesc->id_blkno; 777 if (blkno == 0) 778 return (KEEPON); 779 780 dp = idesc->id_dp; 781 if (blkno == BLK_NOCOPY || blkno == BLK_SNAP) { 782 if (idesc->id_lbn < UFS_NDADDR) { 783 DIP_SET(dp, di_db[idesc->id_lbn], 0); 784 } else { 785 bp = idesc->id_bp; 786 IBLK_SET(bp, bp->b_index, 0); 787 dirty(bp); 788 } 789 } 790 return (KEEPON); 791 } 792 793 /* 794 * Notification that a block is being freed. Return zero if the free 795 * should be allowed to proceed. Return non-zero if the snapshot file 796 * wants to claim the block. The block will be claimed if it is an 797 * uncopied part of one of the snapshots. It will be freed if it is 798 * either a BLK_NOCOPY or has already been copied in all of the snapshots. 799 * If a fragment is being freed, then all snapshots that care about 800 * it must make a copy since a snapshot file can only claim full sized 801 * blocks. Note that if more than one snapshot file maps the block, 802 * we can pick one at random to claim it. Since none of the snapshots 803 * can change, we are assurred that they will all see the same unmodified 804 * image. When deleting a snapshot file (see ino_trunc above), we 805 * must push any of these claimed blocks to one of the other snapshots 806 * that maps it. These claimed blocks are easily identified as they will 807 * have a block number equal to their logical block number within the 808 * snapshot. A copied block can never have this property because they 809 * must always have been allocated from a BLK_NOCOPY location. 810 */ 811 int 812 snapblkfree(struct fs *fs, ufs2_daddr_t bno, long size, ino_t inum, 813 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags)) 814 { 815 union dinode *dp; 816 struct inode ip; 817 struct bufarea *snapbp; 818 ufs_lbn_t lbn; 819 ufs2_daddr_t blkno, relblkno; 820 int i, frags, claimedblk, copydone; 821 822 /* If no snapshots, nothing to do */ 823 if (snapcnt == 0) 824 return (0); 825 if (debug) 826 printf("snapblkfree: in ino %jd free blkno %jd, size %jd\n", 827 (intmax_t)inum, (intmax_t)bno, (intmax_t)size); 828 relblkno = blknum(fs, bno); 829 lbn = fragstoblks(fs, relblkno); 830 /* Direct blocks are always pre-copied */ 831 if (lbn < UFS_NDADDR) 832 return (0); 833 copydone = 0; 834 claimedblk = 0; 835 for (i = 0; i < snapcnt; i++) { 836 /* 837 * Lookup block being freed. 838 */ 839 ip = snaplist[i]; 840 dp = ip.i_dp; 841 blkno = ino_blkatoff(dp, inum != 0 ? inum : ip.i_number, 842 lbn, &frags, &snapbp); 843 /* 844 * Check to see if block needs to be copied. 845 */ 846 if (blkno == 0) { 847 /* 848 * A block that we map is being freed. If it has not 849 * been claimed yet, we will claim or copy it (below). 850 */ 851 claimedblk = 1; 852 } else if (blkno == BLK_SNAP) { 853 /* 854 * No previous snapshot claimed the block, 855 * so it will be freed and become a BLK_NOCOPY 856 * (don't care) for us. 857 */ 858 if (claimedblk) 859 pfatal("snapblkfree: inconsistent block type"); 860 IBLK_SET(snapbp, snapbp->b_index, BLK_NOCOPY); 861 dirty(snapbp); 862 brelse(snapbp); 863 continue; 864 } else /* BLK_NOCOPY or default */ { 865 /* 866 * If the snapshot has already copied the block 867 * (default), or does not care about the block, 868 * it is not needed. 869 */ 870 brelse(snapbp); 871 continue; 872 } 873 /* 874 * If this is a full size block, we will just grab it 875 * and assign it to the snapshot inode. Otherwise we 876 * will proceed to copy it. See explanation for this 877 * routine as to why only a single snapshot needs to 878 * claim this block. 879 */ 880 if (size == fs->fs_bsize) { 881 if (debug) 882 printf("Grabonremove snapshot %ju lbn %jd " 883 "from inum %ju\n", (intmax_t)ip.i_number, 884 (intmax_t)lbn, (uintmax_t)inum); 885 IBLK_SET(snapbp, snapbp->b_index, relblkno); 886 dirty(snapbp); 887 brelse(snapbp); 888 DIP_SET(dp, di_blocks, 889 DIP(dp, di_blocks) + btodb(size)); 890 inodirty(&ip); 891 return (1); 892 } 893 894 /* First time through, read the contents of the old block. */ 895 if (copydone == 0) { 896 copydone = 1; 897 if (blread(fsreadfd, copybuf, fsbtodb(fs, relblkno), 898 fs->fs_bsize) != 0) { 899 pfatal("Could not read snapshot %ju block " 900 "%jd\n", (intmax_t)ip.i_number, 901 (intmax_t)relblkno); 902 continue; 903 } 904 } 905 /* 906 * This allocation will never require any additional 907 * allocations for the snapshot inode. 908 */ 909 blkno = allocblk(dtog(fs, relblkno), fs->fs_frag, 910 checkblkavail); 911 if (blkno == 0) { 912 pfatal("Could not allocate block for snapshot %ju\n", 913 (intmax_t)ip.i_number); 914 continue; 915 } 916 if (debug) 917 printf("Copyonremove: snapino %jd lbn %jd for inum %ju " 918 "size %ld new blkno %jd\n", (intmax_t)ip.i_number, 919 (intmax_t)lbn, (uintmax_t)inum, size, 920 (intmax_t)blkno); 921 blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize); 922 IBLK_SET(snapbp, snapbp->b_index, blkno); 923 dirty(snapbp); 924 brelse(snapbp); 925 DIP_SET(dp, di_blocks, 926 DIP(dp, di_blocks) + btodb(fs->fs_bsize)); 927 inodirty(&ip); 928 } 929 return (0); 930 } 931 932 /* 933 * Notification that a block is being written. Return if the block 934 * is part of a snapshot as snapshots never track other snapshots. 935 * The block will be copied in all of the snapshots that are tracking 936 * it and have not yet copied it. Some buffers may hold more than one 937 * block. Here we need to check each block in the buffer. 938 */ 939 void 940 copyonwrite(struct fs *fs, struct bufarea *bp, 941 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags)) 942 { 943 ufs2_daddr_t copyblkno; 944 long i, numblks; 945 946 /* If no snapshots, nothing to do. */ 947 if (snapcnt == 0) 948 return; 949 numblks = blkroundup(fs, bp->b_size) / fs->fs_bsize; 950 if (debug) 951 prtbuf(bp, "copyonwrite: checking %jd block%s in buffer", 952 (intmax_t)numblks, numblks > 1 ? "s" : ""); 953 copyblkno = blknum(fs, dbtofsb(fs, bp->b_bno)); 954 for (i = 0; i < numblks; i++) { 955 chkcopyonwrite(fs, copyblkno, checkblkavail); 956 copyblkno += fs->fs_frag; 957 } 958 } 959 960 static void 961 chkcopyonwrite(struct fs *fs, ufs2_daddr_t copyblkno, 962 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags)) 963 { 964 struct inode ip; 965 union dinode *dp; 966 struct bufarea *snapbp; 967 ufs2_daddr_t blkno; 968 int i, frags, copydone; 969 ufs_lbn_t lbn; 970 971 lbn = fragstoblks(fs, copyblkno); 972 /* Direct blocks are always pre-copied */ 973 if (lbn < UFS_NDADDR) 974 return; 975 copydone = 0; 976 for (i = 0; i < snapcnt; i++) { 977 /* 978 * Lookup block being freed. 979 */ 980 ip = snaplist[i]; 981 dp = ip.i_dp; 982 blkno = ino_blkatoff(dp, ip.i_number, lbn, &frags, &snapbp); 983 /* 984 * Check to see if block needs to be copied. 985 */ 986 if (blkno != 0) { 987 /* 988 * A block that we have already copied or don't track. 989 */ 990 brelse(snapbp); 991 continue; 992 } 993 /* First time through, read the contents of the old block. */ 994 if (copydone == 0) { 995 copydone = 1; 996 if (blread(fsreadfd, copybuf, fsbtodb(fs, copyblkno), 997 fs->fs_bsize) != 0) { 998 pfatal("Could not read snapshot %ju block " 999 "%jd\n", (intmax_t)ip.i_number, 1000 (intmax_t)copyblkno); 1001 continue; 1002 } 1003 } 1004 /* 1005 * This allocation will never require any additional 1006 * allocations for the snapshot inode. 1007 */ 1008 if ((blkno = allocblk(dtog(fs, copyblkno), fs->fs_frag, 1009 checkblkavail)) == 0) { 1010 pfatal("Could not allocate block for snapshot %ju\n", 1011 (intmax_t)ip.i_number); 1012 continue; 1013 } 1014 if (debug) 1015 prtbuf(snapbp, "Copyonwrite: snapino %jd lbn %jd using " 1016 "blkno %ju setting in buffer", 1017 (intmax_t)ip.i_number, (intmax_t)lbn, 1018 (intmax_t)blkno); 1019 blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize); 1020 IBLK_SET(snapbp, snapbp->b_index, blkno); 1021 dirty(snapbp); 1022 brelse(snapbp); 1023 DIP_SET(dp, di_blocks, 1024 DIP(dp, di_blocks) + btodb(fs->fs_bsize)); 1025 inodirty(&ip); 1026 } 1027 return; 1028 } 1029 1030 /* 1031 * Traverse an inode and check that its block count is correct 1032 * fixing it if necessary. 1033 */ 1034 void 1035 check_blkcnt(struct inode *ip) 1036 { 1037 struct inodesc idesc; 1038 union dinode *dp; 1039 ufs2_daddr_t ndb; 1040 int j, ret, offset; 1041 1042 dp = ip->i_dp; 1043 memset(&idesc, 0, sizeof(struct inodesc)); 1044 idesc.id_func = pass1check; 1045 idesc.id_number = ip->i_number; 1046 idesc.id_type = (DIP(dp, di_flags) & SF_SNAPSHOT) == 0 ? ADDR : SNAP; 1047 (void)ckinode(dp, &idesc); 1048 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) { 1049 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize); 1050 for (j = 0; j < UFS_NXADDR; j++) { 1051 if (--ndb == 0 && 1052 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0) 1053 idesc.id_numfrags = numfrags(&sblock, 1054 fragroundup(&sblock, offset)); 1055 else 1056 idesc.id_numfrags = sblock.fs_frag; 1057 if (dp->dp2.di_extb[j] == 0) 1058 continue; 1059 idesc.id_blkno = dp->dp2.di_extb[j]; 1060 ret = (*idesc.id_func)(&idesc); 1061 if (ret & STOP) 1062 break; 1063 } 1064 } 1065 idesc.id_entryno *= btodb(sblock.fs_fsize); 1066 if (DIP(dp, di_blocks) != idesc.id_entryno) { 1067 if (!(sujrecovery && preen)) { 1068 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)", 1069 (u_long)idesc.id_number, 1070 (uintmax_t)DIP(dp, di_blocks), 1071 (uintmax_t)idesc.id_entryno); 1072 if (preen) 1073 printf(" (CORRECTED)\n"); 1074 else if (reply("CORRECT") == 0) 1075 return; 1076 } 1077 if (bkgrdflag == 0) { 1078 DIP_SET(dp, di_blocks, idesc.id_entryno); 1079 inodirty(ip); 1080 } else { 1081 cmd.value = idesc.id_number; 1082 cmd.size = idesc.id_entryno - DIP(dp, di_blocks); 1083 if (debug) 1084 printf("adjblkcnt ino %ju amount %lld\n", 1085 (uintmax_t)cmd.value, (long long)cmd.size); 1086 if (sysctl(adjblkcnt, MIBSIZE, 0, 0, 1087 &cmd, sizeof cmd) == -1) 1088 rwerror("ADJUST INODE BLOCK COUNT", cmd.value); 1089 } 1090 } 1091 } 1092 1093 void 1094 freeinodebuf(void) 1095 { 1096 struct bufarea *bp; 1097 int i; 1098 1099 /* 1100 * Flush old contents in case they have been updated. 1101 */ 1102 flush(fswritefd, &inobuf); 1103 if (inobuf.b_un.b_buf != NULL) 1104 free((char *)inobuf.b_un.b_buf); 1105 inobuf.b_un.b_buf = NULL; 1106 firstinum = lastinum = 0; 1107 /* 1108 * Reload the snapshot inodes in case any of them changed. 1109 */ 1110 for (i = 0; i < snapcnt; i++) { 1111 bp = snaplist[i].i_bp; 1112 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, bp->b_bno, 1113 bp->b_size); 1114 } 1115 } 1116 1117 /* 1118 * Routines to maintain information about directory inodes. 1119 * This is built during the first pass and used during the 1120 * second and third passes. 1121 * 1122 * Enter inodes into the cache. 1123 */ 1124 struct inoinfo * 1125 cacheino(union dinode *dp, ino_t inumber) 1126 { 1127 struct inoinfo *inp; 1128 int i, blks; 1129 1130 if (getinoinfo(inumber) != NULL) 1131 pfatal("cacheino: duplicate entry for ino %jd\n", 1132 (intmax_t)inumber); 1133 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR) 1134 blks = UFS_NDADDR + UFS_NIADDR; 1135 else if (DIP(dp, di_size) > 0) 1136 blks = howmany(DIP(dp, di_size), sblock.fs_bsize); 1137 else 1138 blks = 1; 1139 inp = (struct inoinfo *) 1140 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t)); 1141 if (inp == NULL) 1142 errx(EEXIT, "cannot increase directory list"); 1143 SLIST_INSERT_HEAD(&inphash[inumber % dirhash], inp, i_hash); 1144 inp->i_flags = 0; 1145 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0; 1146 inp->i_dotdot = (ino_t)0; 1147 inp->i_number = inumber; 1148 inp->i_isize = DIP(dp, di_size); 1149 inp->i_depth = DIP(dp, di_dirdepth); 1150 inp->i_numblks = blks; 1151 for (i = 0; i < MIN(blks, UFS_NDADDR); i++) 1152 inp->i_blks[i] = DIP(dp, di_db[i]); 1153 if (blks > UFS_NDADDR) 1154 for (i = 0; i < UFS_NIADDR; i++) 1155 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]); 1156 if (inplast == listmax) { 1157 listmax += 100; 1158 inpsort = (struct inoinfo **)reallocarray((char *)inpsort, 1159 listmax, sizeof(struct inoinfo *)); 1160 if (inpsort == NULL) 1161 errx(EEXIT, "cannot increase directory list"); 1162 } 1163 inpsort[inplast++] = inp; 1164 return (inp); 1165 } 1166 1167 /* 1168 * Look up an inode cache structure. 1169 */ 1170 struct inoinfo * 1171 getinoinfo(ino_t inumber) 1172 { 1173 struct inoinfo *inp; 1174 1175 SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) { 1176 if (inp->i_number != inumber) 1177 continue; 1178 return (inp); 1179 } 1180 return (NULL); 1181 } 1182 1183 /* 1184 * Remove an entry from the inode cache and disk-order sorted list. 1185 * Return 0 on success and 1 on failure. 1186 */ 1187 int 1188 removecachedino(ino_t inumber) 1189 { 1190 struct inoinfo *inp, **inpp; 1191 char *listtype; 1192 1193 listtype = "hash"; 1194 SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) { 1195 if (inp->i_number != inumber) 1196 continue; 1197 SLIST_REMOVE(&inphash[inumber % dirhash], inp, inoinfo, i_hash); 1198 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) { 1199 if (*inpp != inp) 1200 continue; 1201 *inpp = inpsort[inplast - 1]; 1202 inplast--; 1203 free(inp); 1204 return (0); 1205 } 1206 listtype = "sort"; 1207 break; 1208 } 1209 pfatal("removecachedino: entry for ino %jd not found on %s list\n", 1210 (intmax_t)inumber, listtype); 1211 return (1); 1212 } 1213 1214 /* 1215 * Clean up all the inode cache structure. 1216 */ 1217 void 1218 inocleanup(void) 1219 { 1220 struct inoinfo **inpp; 1221 1222 if (inphash == NULL) 1223 return; 1224 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) 1225 free((char *)(*inpp)); 1226 free((char *)inphash); 1227 inphash = NULL; 1228 free((char *)inpsort); 1229 inpsort = NULL; 1230 } 1231 1232 void 1233 inodirty(struct inode *ip) 1234 { 1235 1236 if (sblock.fs_magic == FS_UFS2_MAGIC) 1237 ffs_update_dinode_ckhash(&sblock, 1238 (struct ufs2_dinode *)ip->i_dp); 1239 dirty(ip->i_bp); 1240 } 1241 1242 void 1243 clri(struct inodesc *idesc, const char *type, int flag) 1244 { 1245 union dinode *dp; 1246 struct inode ip; 1247 1248 ginode(idesc->id_number, &ip); 1249 dp = ip.i_dp; 1250 if (flag == 1) { 1251 pwarn("%s %s", type, 1252 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"); 1253 prtinode(&ip); 1254 printf("\n"); 1255 } 1256 if (preen || reply("CLEAR") == 1) { 1257 if (preen) 1258 printf(" (CLEARED)\n"); 1259 n_files--; 1260 if (bkgrdflag == 0) { 1261 if (idesc->id_type == SNAP) { 1262 snapremove(idesc->id_number); 1263 idesc->id_type = ADDR; 1264 } 1265 (void)ckinode(dp, idesc); 1266 inoinfo(idesc->id_number)->ino_state = USTATE; 1267 clearinode(dp); 1268 inodirty(&ip); 1269 } else { 1270 cmd.value = idesc->id_number; 1271 cmd.size = -DIP(dp, di_nlink); 1272 if (debug) 1273 printf("adjrefcnt ino %ld amt %lld\n", 1274 (long)cmd.value, (long long)cmd.size); 1275 if (sysctl(adjrefcnt, MIBSIZE, 0, 0, 1276 &cmd, sizeof cmd) == -1) 1277 rwerror("ADJUST INODE", cmd.value); 1278 } 1279 } 1280 irelse(&ip); 1281 } 1282 1283 int 1284 findname(struct inodesc *idesc) 1285 { 1286 struct direct *dirp = idesc->id_dirp; 1287 1288 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) { 1289 idesc->id_entryno++; 1290 return (KEEPON); 1291 } 1292 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1); 1293 return (STOP|FOUND); 1294 } 1295 1296 int 1297 findino(struct inodesc *idesc) 1298 { 1299 struct direct *dirp = idesc->id_dirp; 1300 1301 if (dirp->d_ino == 0) 1302 return (KEEPON); 1303 if (strcmp(dirp->d_name, idesc->id_name) == 0 && 1304 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino < maxino) { 1305 idesc->id_parent = dirp->d_ino; 1306 return (STOP|FOUND); 1307 } 1308 return (KEEPON); 1309 } 1310 1311 int 1312 clearentry(struct inodesc *idesc) 1313 { 1314 struct direct *dirp = idesc->id_dirp; 1315 1316 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) { 1317 idesc->id_entryno++; 1318 return (KEEPON); 1319 } 1320 dirp->d_ino = 0; 1321 return (STOP|FOUND|ALTERED); 1322 } 1323 1324 void 1325 prtinode(struct inode *ip) 1326 { 1327 char *p; 1328 union dinode *dp; 1329 struct passwd *pw; 1330 time_t t; 1331 1332 dp = ip->i_dp; 1333 printf(" I=%lu ", (u_long)ip->i_number); 1334 if (ip->i_number < UFS_ROOTINO || ip->i_number >= maxino) 1335 return; 1336 printf(" OWNER="); 1337 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL) 1338 printf("%s ", pw->pw_name); 1339 else 1340 printf("%u ", (unsigned)DIP(dp, di_uid)); 1341 printf("MODE=%o\n", DIP(dp, di_mode)); 1342 if (preen) 1343 printf("%s: ", cdevname); 1344 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size)); 1345 t = DIP(dp, di_mtime); 1346 if ((p = ctime(&t)) != NULL) 1347 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]); 1348 } 1349 1350 void 1351 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk) 1352 { 1353 1354 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino); 1355 printf("\n"); 1356 switch (inoinfo(ino)->ino_state) { 1357 1358 case FSTATE: 1359 case FZLINK: 1360 inoinfo(ino)->ino_state = FCLEAR; 1361 return; 1362 1363 case DSTATE: 1364 case DZLINK: 1365 inoinfo(ino)->ino_state = DCLEAR; 1366 return; 1367 1368 case FCLEAR: 1369 case DCLEAR: 1370 return; 1371 1372 default: 1373 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state); 1374 /* NOTREACHED */ 1375 } 1376 } 1377 1378 /* 1379 * allocate an unused inode 1380 */ 1381 ino_t 1382 allocino(ino_t request, int type) 1383 { 1384 ino_t ino; 1385 struct inode ip; 1386 union dinode *dp; 1387 struct bufarea *cgbp; 1388 struct cg *cgp; 1389 int cg, anyino; 1390 1391 anyino = 0; 1392 if (request == 0) { 1393 request = UFS_ROOTINO; 1394 anyino = 1; 1395 } else if (inoinfo(request)->ino_state != USTATE) 1396 return (0); 1397 retry: 1398 for (ino = request; ino < maxino; ino++) 1399 if (inoinfo(ino)->ino_state == USTATE) 1400 break; 1401 if (ino >= maxino) 1402 return (0); 1403 cg = ino_to_cg(&sblock, ino); 1404 cgbp = cglookup(cg); 1405 cgp = cgbp->b_un.b_cg; 1406 if (!check_cgmagic(cg, cgbp)) { 1407 if (anyino == 0) 1408 return (0); 1409 request = (cg + 1) * sblock.fs_ipg; 1410 goto retry; 1411 } 1412 setbit(cg_inosused(cgp), ino % sblock.fs_ipg); 1413 cgp->cg_cs.cs_nifree--; 1414 switch (type & IFMT) { 1415 case IFDIR: 1416 inoinfo(ino)->ino_state = DSTATE; 1417 cgp->cg_cs.cs_ndir++; 1418 break; 1419 case IFREG: 1420 case IFLNK: 1421 inoinfo(ino)->ino_state = FSTATE; 1422 break; 1423 default: 1424 return (0); 1425 } 1426 cgdirty(cgbp); 1427 ginode(ino, &ip); 1428 dp = ip.i_dp; 1429 memset(dp, 0, ((sblock.fs_magic == FS_UFS1_MAGIC) ? 1430 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode))); 1431 DIP_SET(dp, di_db[0], allocblk(ino_to_cg(&sblock, ino), (long)1, 1432 std_checkblkavail)); 1433 if (DIP(dp, di_db[0]) == 0) { 1434 inoinfo(ino)->ino_state = USTATE; 1435 inodirty(&ip); 1436 irelse(&ip); 1437 return (0); 1438 } 1439 DIP_SET(dp, di_mode, type); 1440 DIP_SET(dp, di_atime, time(NULL)); 1441 DIP_SET(dp, di_ctime, DIP(dp, di_atime)); 1442 DIP_SET(dp, di_mtime, DIP(dp, di_ctime)); 1443 DIP_SET(dp, di_size, sblock.fs_fsize); 1444 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize)); 1445 n_files++; 1446 inodirty(&ip); 1447 irelse(&ip); 1448 inoinfo(ino)->ino_type = IFTODT(type); 1449 return (ino); 1450 } 1451 1452 /* 1453 * deallocate an inode 1454 */ 1455 void 1456 freeino(ino_t ino) 1457 { 1458 struct inodesc idesc; 1459 union dinode *dp; 1460 struct inode ip; 1461 1462 memset(&idesc, 0, sizeof(struct inodesc)); 1463 idesc.id_type = ADDR; 1464 idesc.id_func = freeblock; 1465 idesc.id_number = ino; 1466 ginode(ino, &ip); 1467 dp = ip.i_dp; 1468 (void)ckinode(dp, &idesc); 1469 clearinode(dp); 1470 inodirty(&ip); 1471 irelse(&ip); 1472 inoinfo(ino)->ino_state = USTATE; 1473 n_files--; 1474 } 1475