1 /* $OpenBSD: inode.c,v 1.32 2008/12/29 18:06:10 otto Exp $ */ 2 /* $NetBSD: inode.c,v 1.23 1996/10/11 20:15:47 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1986, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)inode.c 8.5 (Berkeley) 2/8/95"; 36 #else 37 static const char rcsid[] = "$OpenBSD: inode.c,v 1.32 2008/12/29 18:06:10 otto Exp $"; 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/time.h> 43 #include <ufs/ufs/dinode.h> 44 #include <ufs/ufs/dir.h> 45 #include <ufs/ffs/fs.h> 46 #ifndef SMALL 47 #include <pwd.h> 48 #endif 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include "fsck.h" 55 #include "fsutil.h" 56 #include "extern.h" 57 58 static ino_t startinum; 59 60 static int iblock(struct inodesc *, long, off_t); 61 62 int 63 ckinode(union dinode *dp, struct inodesc *idesc) 64 { 65 long ret, ndb, offset; 66 union dinode dino; 67 off_t sizepb, remsize; 68 mode_t mode; 69 int i; 70 char pathbuf[MAXPATHLEN + 1]; 71 72 if (idesc->id_fix != IGNORE) 73 idesc->id_fix = DONTKNOW; 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) < sblock.fs_maxsymlinklen || 79 (sblock.fs_maxsymlinklen == 0 && DIP(dp, di_blocks) == 0)))) 80 return (KEEPON); 81 if (sblock.fs_magic == FS_UFS1_MAGIC) 82 dino.dp1 = dp->dp1; 83 else 84 dino.dp2 = dp->dp2; 85 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize); 86 for (i = 0; i < NDADDR; i++) { 87 if (--ndb == 0 && (offset = blkoff(&sblock, 88 DIP(&dino, di_size))) != 0) 89 idesc->id_numfrags = 90 numfrags(&sblock, fragroundup(&sblock, offset)); 91 else 92 idesc->id_numfrags = sblock.fs_frag; 93 if (DIP(&dino, di_db[i]) == 0) { 94 if (idesc->id_type == DATA && ndb >= 0) { 95 /* An empty block in a directory XXX */ 96 getpathname(pathbuf, sizeof pathbuf, 97 idesc->id_number, idesc->id_number); 98 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 99 pathbuf); 100 if (reply("ADJUST LENGTH") == 1) { 101 dp = ginode(idesc->id_number); 102 DIP_SET(dp, di_size, 103 i * sblock.fs_bsize); 104 printf( 105 "YOU MUST RERUN FSCK AFTERWARDS\n"); 106 rerun = 1; 107 inodirty(); 108 } 109 } 110 continue; 111 } 112 idesc->id_blkno = DIP(&dino, di_db[i]); 113 if (idesc->id_type == ADDR) 114 ret = (*idesc->id_func)(idesc); 115 else 116 ret = dirscan(idesc); 117 if (ret & STOP) 118 return (ret); 119 } 120 idesc->id_numfrags = sblock.fs_frag; 121 remsize = DIP(&dino, di_size) - sblock.fs_bsize * NDADDR; 122 sizepb = sblock.fs_bsize; 123 for (i = 0; i < NIADDR; i++) { 124 if (DIP(&dino, di_ib[i])) { 125 idesc->id_blkno = DIP(&dino, di_ib[i]); 126 ret = iblock(idesc, i + 1, remsize); 127 if (ret & STOP) 128 return (ret); 129 } else { 130 if (idesc->id_type == DATA && remsize > 0) { 131 /* An empty block in a directory XXX */ 132 getpathname(pathbuf, sizeof pathbuf, 133 idesc->id_number, idesc->id_number); 134 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 135 pathbuf); 136 if (reply("ADJUST LENGTH") == 1) { 137 dp = ginode(idesc->id_number); 138 DIP_SET(dp, di_size, 139 DIP(dp, di_size) - remsize); 140 remsize = 0; 141 printf( 142 "YOU MUST RERUN FSCK AFTERWARDS\n"); 143 rerun = 1; 144 inodirty(); 145 break; 146 } 147 } 148 } 149 sizepb *= NINDIR(&sblock); 150 remsize -= sizepb; 151 } 152 return (KEEPON); 153 } 154 155 static int 156 iblock(struct inodesc *idesc, long ilevel, off_t isize) 157 { 158 struct bufarea *bp; 159 int i, n, (*func)(struct inodesc *), nif; 160 off_t sizepb; 161 char buf[BUFSIZ]; 162 char pathbuf[MAXPATHLEN + 1]; 163 union dinode *dp; 164 165 if (idesc->id_type == ADDR) { 166 func = idesc->id_func; 167 if (((n = (*func)(idesc)) & KEEPON) == 0) 168 return (n); 169 } else 170 func = dirscan; 171 if (isize < 0 || chkrange(idesc->id_blkno, idesc->id_numfrags)) 172 return (SKIP); 173 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize); 174 ilevel--; 175 for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++) 176 sizepb *= NINDIR(&sblock); 177 if (howmany(isize, sizepb) > NINDIR(&sblock)) 178 nif = NINDIR(&sblock); 179 else 180 nif = howmany(isize, sizepb); 181 if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) { 182 for (i = nif; i < NINDIR(&sblock); i++) { 183 if (IBLK(bp, i) == 0) 184 continue; 185 (void)snprintf(buf, sizeof buf, 186 "PARTIALLY TRUNCATED INODE I=%u", 187 idesc->id_number); 188 if (preen) 189 pfatal("%s", buf); 190 else if (dofix(idesc, buf)) { 191 IBLK_SET(bp, i, 0); 192 dirty(bp); 193 } 194 } 195 flush(fswritefd, bp); 196 } 197 for (i = 0; i < nif; i++) { 198 if (IBLK(bp, i)) { 199 idesc->id_blkno = IBLK(bp, i); 200 if (ilevel == 0) 201 n = (*func)(idesc); 202 else 203 n = iblock(idesc, ilevel, isize); 204 if (n & STOP) { 205 bp->b_flags &= ~B_INUSE; 206 return (n); 207 } 208 } else { 209 if (idesc->id_type == DATA && isize > 0) { 210 /* An empty block in a directory XXX */ 211 getpathname(pathbuf, sizeof pathbuf, 212 idesc->id_number, idesc->id_number); 213 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 214 pathbuf); 215 if (reply("ADJUST LENGTH") == 1) { 216 dp = ginode(idesc->id_number); 217 DIP_SET(dp, di_size, 218 DIP(dp, di_size) - isize); 219 isize = 0; 220 printf( 221 "YOU MUST RERUN FSCK AFTERWARDS\n"); 222 rerun = 1; 223 inodirty(); 224 bp->b_flags &= ~B_INUSE; 225 return(STOP); 226 } 227 } 228 } 229 isize -= sizepb; 230 } 231 bp->b_flags &= ~B_INUSE; 232 return (KEEPON); 233 } 234 235 /* 236 * Check that a block in a legal block number. 237 * Return 0 if in range, 1 if out of range. 238 */ 239 int 240 chkrange(daddr64_t blk, int cnt) 241 { 242 int c; 243 244 if (cnt <= 0 || blk <= 0 || blk > maxfsblock || 245 cnt - 1 > maxfsblock - blk) 246 return (1); 247 if (cnt > sblock.fs_frag || 248 fragnum(&sblock, blk) + cnt > sblock.fs_frag) { 249 if (debug) 250 printf("bad size: blk %lld, offset %lld, size %d\n", 251 blk, fragnum(&sblock, blk), cnt); 252 return (1); 253 } 254 c = dtog(&sblock, blk); 255 if (blk < cgdmin(&sblock, c)) { 256 if ((blk + cnt) > cgsblock(&sblock, c)) { 257 if (debug) { 258 printf("blk %lld < cgdmin %lld;", 259 blk, cgdmin(&sblock, c)); 260 printf(" blk + cnt %lld > cgsbase %lld\n", 261 blk + cnt, cgsblock(&sblock, c)); 262 } 263 return (1); 264 } 265 } else { 266 if ((blk + cnt) > cgbase(&sblock, c+1)) { 267 if (debug) { 268 printf("blk %lld >= cgdmin %lld;", 269 blk, cgdmin(&sblock, c)); 270 printf(" blk + cnt %lld > sblock.fs_fpg %d\n", 271 blk+cnt, sblock.fs_fpg); 272 } 273 return (1); 274 } 275 } 276 return (0); 277 } 278 279 /* 280 * General purpose interface for reading inodes. 281 */ 282 union dinode * 283 ginode(ino_t inumber) 284 { 285 daddr64_t iblk; 286 287 if (inumber < ROOTINO || inumber > maxino) 288 errexit("bad inode number %d to ginode\n", inumber); 289 if (startinum == 0 || 290 inumber < startinum || inumber >= startinum + INOPB(&sblock)) { 291 iblk = ino_to_fsba(&sblock, inumber); 292 if (pbp != 0) 293 pbp->b_flags &= ~B_INUSE; 294 pbp = getdatablk(iblk, sblock.fs_bsize); 295 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock); 296 } 297 if (sblock.fs_magic == FS_UFS1_MAGIC) 298 return ((union dinode *) 299 &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]); 300 return ((union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)]); 301 } 302 303 /* 304 * Special purpose version of ginode used to optimize first pass 305 * over all the inodes in numerical order. 306 */ 307 ino_t nextino, lastinum; 308 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 309 static caddr_t inodebuf; 310 311 union dinode * 312 getnextinode(ino_t inumber) 313 { 314 long size; 315 daddr64_t dblk; 316 union dinode *dp; 317 static caddr_t nextinop; 318 319 if (inumber != nextino++ || inumber > maxino) 320 errexit("bad inode number %d to nextinode\n", inumber); 321 if (inumber >= lastinum) { 322 readcnt++; 323 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum)); 324 if (readcnt % readpercg == 0) { 325 size = partialsize; 326 lastinum += partialcnt; 327 } else { 328 size = inobufsize; 329 lastinum += fullcnt; 330 } 331 (void)bread(fsreadfd, inodebuf, dblk, size); 332 nextinop = inodebuf; 333 } 334 dp = (union dinode *)nextinop; 335 if (sblock.fs_magic == FS_UFS1_MAGIC) 336 nextinop += sizeof(struct ufs1_dinode); 337 else 338 nextinop += sizeof(struct ufs2_dinode); 339 return (dp); 340 } 341 342 void 343 setinodebuf(ino_t inum) 344 { 345 346 startinum = 0; 347 nextino = inum; 348 lastinum = inum; 349 readcnt = 0; 350 if (inodebuf != NULL) 351 return; 352 inobufsize = blkroundup(&sblock, INOBUFSIZE); 353 if (sblock.fs_magic == FS_UFS1_MAGIC) 354 fullcnt = inobufsize / sizeof(struct ufs1_dinode); 355 else 356 fullcnt = inobufsize / sizeof(struct ufs2_dinode); 357 readpercg = sblock.fs_ipg / fullcnt; 358 partialcnt = sblock.fs_ipg % fullcnt; 359 if (sblock.fs_magic == FS_UFS1_MAGIC) 360 partialsize = partialcnt * sizeof(struct ufs1_dinode); 361 else 362 partialsize = partialcnt * sizeof(struct ufs2_dinode); 363 if (partialcnt != 0) { 364 readpercg++; 365 } else { 366 partialcnt = fullcnt; 367 partialsize = inobufsize; 368 } 369 if (inodebuf == NULL && 370 (inodebuf = malloc((unsigned)inobufsize)) == NULL) 371 errexit("Cannot allocate space for inode buffer\n"); 372 while (nextino < ROOTINO) 373 (void)getnextinode(nextino); 374 } 375 376 void 377 freeinodebuf(void) 378 { 379 380 if (inodebuf != NULL) 381 free(inodebuf); 382 inodebuf = NULL; 383 } 384 385 /* 386 * Routines to maintain information about directory inodes. 387 * This is built during the first pass and used during the 388 * second and third passes. 389 * 390 * Enter inodes into the cache. 391 */ 392 void 393 cacheino(union dinode *dp, ino_t inumber) 394 { 395 struct inoinfo *inp; 396 struct inoinfo **inpp, **newinpsort; 397 unsigned int blks; 398 long newlistmax; 399 int i; 400 401 blks = howmany(DIP(dp, di_size), sblock.fs_bsize); 402 if (blks > NDADDR) 403 blks = NDADDR + NIADDR; 404 inp = malloc(sizeof(*inp) + (blks ? blks - 1 : 0) * sizeof(daddr64_t)); 405 if (inp == NULL) 406 errexit("cannot allocate memory for inode cache\n"); 407 inpp = &inphead[inumber % numdirs]; 408 inp->i_nexthash = *inpp; 409 *inpp = inp; 410 inp->i_child = inp->i_sibling = inp->i_parentp = 0; 411 if (inumber == ROOTINO) 412 inp->i_parent = ROOTINO; 413 else 414 inp->i_parent = 0; 415 inp->i_dotdot = 0; 416 inp->i_number = inumber; 417 inp->i_isize = DIP(dp, di_size); 418 inp->i_numblks = blks; 419 for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++) 420 inp->i_blks[i] = DIP(dp, di_db[i]); 421 if (blks > NDADDR) 422 for (i = 0; i < NIADDR; i++) 423 inp->i_blks[NDADDR + i] = DIP(dp, di_ib[i]); 424 if (inplast == listmax) { 425 newlistmax = listmax + 100; 426 newinpsort = realloc(inpsort, 427 (unsigned)newlistmax * sizeof(struct inoinfo *)); 428 if (newinpsort == NULL) 429 errexit("cannot increase directory list"); 430 inpsort = newinpsort; 431 listmax = newlistmax; 432 } 433 inpsort[inplast++] = inp; 434 } 435 436 /* 437 * Look up an inode cache structure. 438 */ 439 struct inoinfo * 440 getinoinfo(ino_t inumber) 441 { 442 struct inoinfo *inp; 443 444 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) { 445 if (inp->i_number != inumber) 446 continue; 447 return (inp); 448 } 449 errexit("cannot find inode %d\n", inumber); 450 return (NULL); 451 } 452 453 /* 454 * Clean up all the inode cache structure. 455 */ 456 void 457 inocleanup(void) 458 { 459 struct inoinfo **inpp; 460 461 if (inphead == NULL) 462 return; 463 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) 464 free(*inpp); 465 free(inphead); 466 free(inpsort); 467 inphead = inpsort = NULL; 468 } 469 470 void 471 inodirty(void) 472 { 473 dirty(pbp); 474 } 475 476 void 477 clri(struct inodesc *idesc, char *type, int flag) 478 { 479 union dinode *dp; 480 481 dp = ginode(idesc->id_number); 482 if (flag == 1) { 483 pwarn("%s %s", type, 484 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"); 485 pinode(idesc->id_number); 486 } 487 if (preen || reply("CLEAR") == 1) { 488 if (preen) 489 printf(" (CLEARED)\n"); 490 n_files--; 491 (void)ckinode(dp, idesc); 492 clearinode(dp); 493 SET_ISTATE(idesc->id_number, USTATE); 494 inodirty(); 495 } 496 } 497 498 int 499 findname(struct inodesc *idesc) 500 { 501 struct direct *dirp = idesc->id_dirp; 502 503 if (dirp->d_ino != idesc->id_parent) 504 return (KEEPON); 505 memcpy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1); 506 return (STOP|FOUND); 507 } 508 509 int 510 findino(struct inodesc *idesc) 511 { 512 struct direct *dirp = idesc->id_dirp; 513 514 if (dirp->d_ino == 0) 515 return (KEEPON); 516 if (strcmp(dirp->d_name, idesc->id_name) == 0 && 517 dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) { 518 idesc->id_parent = dirp->d_ino; 519 return (STOP|FOUND); 520 } 521 return (KEEPON); 522 } 523 524 void 525 pinode(ino_t ino) 526 { 527 union dinode *dp; 528 char *p; 529 struct passwd *pw; 530 time_t t; 531 532 printf(" I=%u ", ino); 533 if (ino < ROOTINO || ino > maxino) 534 return; 535 dp = ginode(ino); 536 printf(" OWNER="); 537 #ifndef SMALL 538 if ((pw = getpwuid(DIP(dp, di_uid))) != 0) 539 printf("%s ", pw->pw_name); 540 else 541 #endif 542 printf("%u ", (unsigned)DIP(dp, di_uid)); 543 printf("MODE=%o\n", DIP(dp, di_mode)); 544 if (preen) 545 printf("%s: ", cdevname()); 546 printf("SIZE=%llu ", (unsigned long long)DIP(dp, di_size)); 547 t = DIP(dp, di_mtime); 548 p = ctime(&t); 549 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]); 550 } 551 552 void 553 blkerror(ino_t ino, char *type, daddr64_t blk) 554 { 555 556 pfatal("%lld %s I=%u", blk, type, ino); 557 printf("\n"); 558 switch (GET_ISTATE(ino)) { 559 560 case FSTATE: 561 SET_ISTATE(ino, FCLEAR); 562 return; 563 564 case DSTATE: 565 SET_ISTATE(ino, DCLEAR); 566 return; 567 568 case FCLEAR: 569 case DCLEAR: 570 return; 571 572 default: 573 errexit("BAD STATE %d TO BLKERR\n", GET_ISTATE(ino)); 574 /* NOTREACHED */ 575 } 576 } 577 578 /* 579 * allocate an unused inode 580 */ 581 ino_t 582 allocino(ino_t request, int type) 583 { 584 ino_t ino; 585 union dinode *dp; 586 struct cg *cgp = &cgrp; 587 int cg; 588 time_t t; 589 590 if (request == 0) 591 request = ROOTINO; 592 else if (GET_ISTATE(request) != USTATE) 593 return (0); 594 for (ino = request; ino < maxino; ino++) 595 if (GET_ISTATE(ino) == USTATE) 596 break; 597 if (ino == maxino) 598 return (0); 599 cg = ino_to_cg(&sblock, ino); 600 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize); 601 if (!cg_chkmagic(cgp)) 602 pfatal("CG %d: BAD MAGIC NUMBER\n", cg); 603 setbit(cg_inosused(cgp), ino % sblock.fs_ipg); 604 cgp->cg_cs.cs_nifree--; 605 606 switch (type & IFMT) { 607 case IFDIR: 608 SET_ISTATE(ino, DSTATE); 609 cgp->cg_cs.cs_ndir++; 610 break; 611 case IFREG: 612 case IFLNK: 613 SET_ISTATE(ino, FSTATE); 614 break; 615 default: 616 return (0); 617 } 618 cgdirty(); 619 dp = ginode(ino); 620 DIP_SET(dp, di_db[0], allocblk(1)); 621 if (DIP(dp, di_db[0]) == 0) { 622 SET_ISTATE(ino, USTATE); 623 return (0); 624 } 625 DIP_SET(dp, di_mode, type); 626 DIP_SET(dp, di_uid, geteuid()); 627 DIP_SET(dp, di_gid, getegid()); 628 DIP_SET(dp, di_flags, 0); 629 (void)time(&t); 630 DIP_SET(dp, di_atime, t); 631 DIP_SET(dp, di_atimensec, 0); 632 DIP_SET(dp, di_mtime, t); 633 DIP_SET(dp, di_mtimensec, 0); 634 DIP_SET(dp, di_ctime, t); 635 DIP_SET(dp, di_ctimensec, 0); 636 DIP_SET(dp, di_size, sblock.fs_fsize); 637 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize)); 638 n_files++; 639 inodirty(); 640 if (newinofmt) 641 SET_ITYPE(ino, IFTODT(type)); 642 return (ino); 643 } 644 645 /* 646 * deallocate an inode 647 */ 648 void 649 freeino(ino_t ino) 650 { 651 struct inodesc idesc; 652 union dinode *dp; 653 654 memset(&idesc, 0, sizeof(struct inodesc)); 655 idesc.id_type = ADDR; 656 idesc.id_func = pass4check; 657 idesc.id_number = ino; 658 dp = ginode(ino); 659 (void)ckinode(dp, &idesc); 660 clearinode(dp); 661 inodirty(); 662 SET_ISTATE(ino, USTATE); 663 n_files--; 664 } 665