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