1 /* $NetBSD: inode.c,v 1.35 2006/09/01 19:52:48 perseant Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Konrad E. Schroder <perseant@hhhh.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1980, 1986, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 */ 67 68 #include <sys/types.h> 69 #include <sys/param.h> 70 #include <sys/time.h> 71 #include <sys/buf.h> 72 #include <sys/mount.h> 73 74 #include <ufs/ufs/inode.h> 75 #include <ufs/ufs/dir.h> 76 #define vnode uvnode 77 #include <ufs/lfs/lfs.h> 78 #undef vnode 79 80 #include <err.h> 81 #ifndef SMALL 82 #include <pwd.h> 83 #endif 84 #include <stdio.h> 85 #include <stdlib.h> 86 #include <string.h> 87 88 #include "bufcache.h" 89 #include "vnode.h" 90 #include "lfs_user.h" 91 92 #include "fsck.h" 93 #include "fsutil.h" 94 #include "extern.h" 95 96 extern SEGUSE *seg_table; 97 extern ufs_daddr_t *din_table; 98 99 static int iblock(struct inodesc *, long, u_int64_t); 100 int blksreqd(struct lfs *, int); 101 int lfs_maxino(void); 102 103 /* 104 * Get a dinode of a given inum. 105 * XXX combine this function with vget. 106 */ 107 struct ufs1_dinode * 108 ginode(ino_t ino) 109 { 110 struct uvnode *vp; 111 struct ubuf *bp; 112 IFILE *ifp; 113 114 vp = vget(fs, ino); 115 if (vp == NULL) 116 return NULL; 117 118 if (din_table[ino] == 0x0) { 119 LFS_IENTRY(ifp, fs, ino, bp); 120 din_table[ino] = ifp->if_daddr; 121 seg_table[dtosn(fs, ifp->if_daddr)].su_nbytes += DINODE1_SIZE; 122 brelse(bp); 123 } 124 return (VTOI(vp)->i_din.ffs1_din); 125 } 126 127 /* 128 * Check validity of held blocks in an inode, recursing through all blocks. 129 */ 130 int 131 ckinode(struct ufs1_dinode *dp, struct inodesc *idesc) 132 { 133 ufs_daddr_t *ap, lbn; 134 long ret, n, ndb, offset; 135 struct ufs1_dinode dino; 136 u_int64_t remsize, sizepb; 137 mode_t mode; 138 char pathbuf[MAXPATHLEN + 1]; 139 struct uvnode *vp, *thisvp; 140 141 if (idesc->id_fix != IGNORE) 142 idesc->id_fix = DONTKNOW; 143 idesc->id_entryno = 0; 144 idesc->id_filesize = dp->di_size; 145 mode = dp->di_mode & IFMT; 146 if (mode == IFBLK || mode == IFCHR || 147 (mode == IFLNK && (dp->di_size < fs->lfs_maxsymlinklen || 148 (fs->lfs_maxsymlinklen == 0 && 149 dp->di_blocks == 0)))) 150 return (KEEPON); 151 dino = *dp; 152 ndb = howmany(dino.di_size, fs->lfs_bsize); 153 154 thisvp = vget(fs, idesc->id_number); 155 for (lbn = 0; lbn < NDADDR; lbn++) { 156 ap = dino.di_db + lbn; 157 if (thisvp) 158 idesc->id_numfrags = 159 numfrags(fs, VTOI(thisvp)->i_lfs_fragsize[lbn]); 160 else { 161 if (--ndb == 0 && (offset = blkoff(fs, dino.di_size)) != 0) { 162 idesc->id_numfrags = 163 numfrags(fs, fragroundup(fs, offset)); 164 } else 165 idesc->id_numfrags = fs->lfs_frag; 166 } 167 if (*ap == 0) { 168 if (idesc->id_type == DATA && ndb >= 0) { 169 /* An empty block in a directory XXX */ 170 getpathname(pathbuf, sizeof(pathbuf), 171 idesc->id_number, idesc->id_number); 172 pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [1]", 173 pathbuf, (long long)idesc->id_number); 174 if (reply("ADJUST LENGTH") == 1) { 175 vp = vget(fs, idesc->id_number); 176 dp = VTOD(vp); 177 dp->di_size = (ap - &dino.di_db[0]) * 178 fs->lfs_bsize; 179 printf( 180 "YOU MUST RERUN FSCK AFTERWARDS\n"); 181 rerun = 1; 182 inodirty(VTOI(vp)); 183 } else 184 break; 185 } 186 continue; 187 } 188 idesc->id_blkno = *ap; 189 idesc->id_lblkno = ap - &dino.di_db[0]; 190 if (idesc->id_type == ADDR) { 191 ret = (*idesc->id_func) (idesc); 192 } else 193 ret = dirscan(idesc); 194 if (ret & STOP) 195 return (ret); 196 } 197 idesc->id_numfrags = fs->lfs_frag; 198 remsize = dino.di_size - fs->lfs_bsize * NDADDR; 199 sizepb = fs->lfs_bsize; 200 for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) { 201 if (*ap) { 202 idesc->id_blkno = *ap; 203 ret = iblock(idesc, n, remsize); 204 if (ret & STOP) 205 return (ret); 206 } else { 207 if (idesc->id_type == DATA && remsize > 0) { 208 /* An empty block in a directory XXX */ 209 getpathname(pathbuf, sizeof(pathbuf), 210 idesc->id_number, idesc->id_number); 211 pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [2]", 212 pathbuf, (long long)idesc->id_number); 213 if (reply("ADJUST LENGTH") == 1) { 214 vp = vget(fs, idesc->id_number); 215 dp = VTOD(vp); 216 dp->di_size -= remsize; 217 remsize = 0; 218 printf( 219 "YOU MUST RERUN FSCK AFTERWARDS\n"); 220 rerun = 1; 221 inodirty(VTOI(vp)); 222 break; 223 } else 224 break; 225 } 226 } 227 sizepb *= NINDIR(fs); 228 remsize -= sizepb; 229 } 230 return (KEEPON); 231 } 232 233 static int 234 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize) 235 { 236 ufs_daddr_t *ap, *aplim; 237 struct ubuf *bp; 238 int i, n, (*func) (struct inodesc *), nif; 239 u_int64_t sizepb; 240 char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ]; 241 struct uvnode *devvp, *vp; 242 int diddirty = 0; 243 244 if (idesc->id_type == ADDR) { 245 func = idesc->id_func; 246 n = (*func) (idesc); 247 if ((n & KEEPON) == 0) 248 return (n); 249 } else 250 func = dirscan; 251 if (chkrange(idesc->id_blkno, fragstofsb(fs, idesc->id_numfrags))) 252 return (SKIP); 253 254 devvp = fs->lfs_devvp; 255 bread(devvp, fsbtodb(fs, idesc->id_blkno), fs->lfs_bsize, NOCRED, &bp); 256 ilevel--; 257 for (sizepb = fs->lfs_bsize, i = 0; i < ilevel; i++) 258 sizepb *= NINDIR(fs); 259 if (isize > sizepb * NINDIR(fs)) 260 nif = NINDIR(fs); 261 else 262 nif = howmany(isize, sizepb); 263 if (idesc->id_func == pass1check && nif < NINDIR(fs)) { 264 aplim = ((ufs_daddr_t *) bp->b_data) + NINDIR(fs); 265 for (ap = ((ufs_daddr_t *) bp->b_data) + nif; ap < aplim; ap++) { 266 if (*ap == 0) 267 continue; 268 (void) sprintf(buf, "PARTIALLY TRUNCATED INODE I=%llu", 269 (unsigned long long)idesc->id_number); 270 if (dofix(idesc, buf)) { 271 *ap = 0; 272 ++diddirty; 273 } 274 } 275 } 276 aplim = ((ufs_daddr_t *) bp->b_data) + nif; 277 for (ap = ((ufs_daddr_t *) bp->b_data); ap < aplim; ap++) { 278 if (*ap) { 279 idesc->id_blkno = *ap; 280 if (ilevel == 0) { 281 /* 282 * dirscan needs lblkno. 283 */ 284 idesc->id_lblkno++; 285 n = (*func) (idesc); 286 } else { 287 n = iblock(idesc, ilevel, isize); 288 } 289 if (n & STOP) { 290 if (diddirty) 291 VOP_BWRITE(bp); 292 else 293 brelse(bp); 294 return (n); 295 } 296 } else { 297 if (idesc->id_type == DATA && isize > 0) { 298 /* An empty block in a directory XXX */ 299 getpathname(pathbuf, sizeof(pathbuf), 300 idesc->id_number, idesc->id_number); 301 pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [3]", 302 pathbuf, (long long)idesc->id_number); 303 if (reply("ADJUST LENGTH") == 1) { 304 vp = vget(fs, idesc->id_number); 305 VTOI(vp)->i_ffs1_size -= isize; 306 isize = 0; 307 printf( 308 "YOU MUST RERUN FSCK AFTERWARDS\n"); 309 rerun = 1; 310 inodirty(VTOI(vp)); 311 if (diddirty) 312 VOP_BWRITE(bp); 313 else 314 brelse(bp); 315 return (STOP); 316 } 317 } 318 } 319 isize -= sizepb; 320 } 321 if (diddirty) 322 VOP_BWRITE(bp); 323 else 324 brelse(bp); 325 return (KEEPON); 326 } 327 328 /* 329 * Check that a block in a legal block number. 330 * Return 0 if in range, 1 if out of range. 331 */ 332 int 333 chkrange(daddr_t blk, int cnt) 334 { 335 if (blk < sntod(fs, 0)) { 336 return (1); 337 } 338 if (blk > maxfsblock) { 339 return (1); 340 } 341 if (blk + cnt < sntod(fs, 0)) { 342 return (1); 343 } 344 if (blk + cnt > maxfsblock) { 345 return (1); 346 } 347 return (0); 348 } 349 350 /* 351 * Routines to maintain information about directory inodes. 352 * This is built during the first pass and used during the 353 * second and third passes. 354 * 355 * Enter inodes into the cache. 356 */ 357 void 358 cacheino(struct ufs1_dinode * dp, ino_t inumber) 359 { 360 struct inoinfo *inp; 361 struct inoinfo **inpp, **ninpsort; 362 unsigned int blks; 363 364 blks = howmany(dp->di_size, fs->lfs_bsize); 365 if (blks > NDADDR) 366 blks = NDADDR + NIADDR; 367 inp = (struct inoinfo *) 368 malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs_daddr_t)); 369 if (inp == NULL) 370 return; 371 inpp = &inphead[inumber % numdirs]; 372 inp->i_nexthash = *inpp; 373 *inpp = inp; 374 inp->i_child = inp->i_sibling = inp->i_parentp = 0; 375 if (inumber == ROOTINO) 376 inp->i_parent = ROOTINO; 377 else 378 inp->i_parent = (ino_t) 0; 379 inp->i_dotdot = (ino_t) 0; 380 inp->i_number = inumber; 381 inp->i_isize = dp->di_size; 382 383 inp->i_numblks = blks * sizeof(ufs_daddr_t); 384 memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t) inp->i_numblks); 385 if (inplast == listmax) { 386 ninpsort = (struct inoinfo **) realloc((char *) inpsort, 387 (unsigned) (listmax + 100) * sizeof(struct inoinfo *)); 388 if (ninpsort == NULL) 389 err(8, "cannot increase directory list\n"); 390 inpsort = ninpsort; 391 listmax += 100; 392 } 393 inpsort[inplast++] = inp; 394 } 395 396 /* 397 * Look up an inode cache structure. 398 */ 399 struct inoinfo * 400 getinoinfo(ino_t inumber) 401 { 402 struct inoinfo *inp; 403 404 for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) { 405 if (inp->i_number != inumber) 406 continue; 407 return (inp); 408 } 409 err(8, "cannot find inode %llu\n", (unsigned long long)inumber); 410 return ((struct inoinfo *) 0); 411 } 412 413 /* 414 * Clean up all the inode cache structure. 415 */ 416 void 417 inocleanup(void) 418 { 419 struct inoinfo **inpp; 420 421 if (inphead == NULL) 422 return; 423 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) 424 free((char *) (*inpp)); 425 free((char *) inphead); 426 free((char *) inpsort); 427 inphead = inpsort = NULL; 428 } 429 430 void 431 inodirty(struct inode *ip) 432 { 433 ip->i_flag |= IN_MODIFIED; 434 } 435 436 void 437 clri(struct inodesc * idesc, const char *type, int flag) 438 { 439 struct uvnode *vp; 440 441 vp = vget(fs, idesc->id_number); 442 if (flag & 0x1) { 443 pwarn("%s %s", type, 444 (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ? "DIR" : "FILE"); 445 pinode(idesc->id_number); 446 } 447 if ((flag & 0x2) || preen || reply("CLEAR") == 1) { 448 if (preen && flag != 2) 449 printf(" (CLEARED)\n"); 450 n_files--; 451 (void) ckinode(VTOD(vp), idesc); 452 clearinode(idesc->id_number); 453 statemap[idesc->id_number] = USTATE; 454 vnode_destroy(vp); 455 return; 456 } 457 return; 458 } 459 460 void 461 clearinode(ino_t inumber) 462 { 463 struct ubuf *bp; 464 IFILE *ifp; 465 daddr_t daddr; 466 467 /* Send cleared inode to the free list */ 468 469 LFS_IENTRY(ifp, fs, inumber, bp); 470 daddr = ifp->if_daddr; 471 if (daddr == LFS_UNUSED_DADDR) { 472 brelse(bp); 473 return; 474 } 475 ifp->if_daddr = LFS_UNUSED_DADDR; 476 ifp->if_nextfree = fs->lfs_freehd; 477 fs->lfs_freehd = inumber; 478 sbdirty(); 479 VOP_BWRITE(bp); 480 481 /* 482 * update segment usage. 483 */ 484 if (daddr != LFS_UNUSED_DADDR) { 485 SEGUSE *sup; 486 u_int32_t oldsn = dtosn(fs, daddr); 487 488 seg_table[oldsn].su_nbytes -= DINODE1_SIZE; 489 LFS_SEGENTRY(sup, fs, oldsn, bp); 490 sup->su_nbytes -= DINODE1_SIZE; 491 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */ 492 } 493 } 494 495 int 496 findname(struct inodesc * idesc) 497 { 498 struct direct *dirp = idesc->id_dirp; 499 size_t len; 500 char *buf; 501 502 if (dirp->d_ino != idesc->id_parent) 503 return (KEEPON); 504 if ((len = dirp->d_namlen + 1) > MAXPATHLEN) { 505 /* Truncate it but don't overflow the buffer */ 506 len = MAXPATHLEN; 507 } 508 /* this is namebuf with utils.h */ 509 buf = __UNCONST(idesc->id_name); 510 (void)memcpy(buf, dirp->d_name, len); 511 return (STOP | FOUND); 512 } 513 514 int 515 findino(struct inodesc * idesc) 516 { 517 struct direct *dirp = idesc->id_dirp; 518 519 if (dirp->d_ino == 0) 520 return (KEEPON); 521 if (strcmp(dirp->d_name, idesc->id_name) == 0 && 522 dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) { 523 idesc->id_parent = dirp->d_ino; 524 return (STOP | FOUND); 525 } 526 return (KEEPON); 527 } 528 529 void 530 pinode(ino_t ino) 531 { 532 struct ufs1_dinode *dp; 533 char *p; 534 struct passwd *pw; 535 time_t t; 536 537 printf(" I=%llu ", (unsigned long long)ino); 538 if (ino < ROOTINO || ino >= maxino) 539 return; 540 dp = ginode(ino); 541 if (dp) { 542 printf(" OWNER="); 543 #ifndef SMALL 544 if ((pw = getpwuid((int) dp->di_uid)) != 0) 545 printf("%s ", pw->pw_name); 546 else 547 #endif 548 printf("%u ", (unsigned) dp->di_uid); 549 printf("MODE=%o\n", dp->di_mode); 550 if (preen) 551 printf("%s: ", cdevname()); 552 printf("SIZE=%llu ", (unsigned long long) dp->di_size); 553 t = dp->di_mtime; 554 p = ctime(&t); 555 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]); 556 } 557 } 558 559 void 560 blkerror(ino_t ino, const char *type, daddr_t blk) 561 { 562 563 pfatal("%lld %s I=%llu", (long long) blk, type, 564 (unsigned long long)ino); 565 printf("\n"); 566 if (exitonfail) 567 exit(1); 568 switch (statemap[ino]) { 569 570 case FSTATE: 571 statemap[ino] = FCLEAR; 572 return; 573 574 case DSTATE: 575 statemap[ino] = DCLEAR; 576 return; 577 578 case FCLEAR: 579 case DCLEAR: 580 return; 581 582 default: 583 err(8, "BAD STATE %d TO BLKERR\n", statemap[ino]); 584 /* NOTREACHED */ 585 } 586 } 587 588 /* 589 * allocate an unused inode 590 */ 591 ino_t 592 allocino(ino_t request, int type) 593 { 594 ino_t ino; 595 struct ufs1_dinode *dp; 596 time_t t; 597 struct uvnode *vp; 598 struct ubuf *bp; 599 600 if (request == 0) 601 request = ROOTINO; 602 else if (statemap[request] != USTATE) 603 return (0); 604 for (ino = request; ino < maxino; ino++) 605 if (statemap[ino] == USTATE) 606 break; 607 if (ino == maxino) 608 extend_ifile(fs); 609 610 switch (type & IFMT) { 611 case IFDIR: 612 statemap[ino] = DSTATE; 613 break; 614 case IFREG: 615 case IFLNK: 616 statemap[ino] = FSTATE; 617 break; 618 default: 619 return (0); 620 } 621 vp = lfs_valloc(fs, ino); 622 if (vp == NULL) 623 return (0); 624 dp = (VTOI(vp)->i_din.ffs1_din); 625 bp = getblk(vp, 0, fs->lfs_fsize); 626 VOP_BWRITE(bp); 627 dp->di_mode = type; 628 (void) time(&t); 629 dp->di_atime = t; 630 dp->di_mtime = dp->di_ctime = dp->di_atime; 631 dp->di_size = fs->lfs_fsize; 632 dp->di_blocks = btofsb(fs, fs->lfs_fsize); 633 n_files++; 634 inodirty(VTOI(vp)); 635 typemap[ino] = IFTODT(type); 636 return (ino); 637 } 638 639 /* 640 * deallocate an inode 641 */ 642 void 643 freeino(ino_t ino) 644 { 645 struct inodesc idesc; 646 struct uvnode *vp; 647 648 memset(&idesc, 0, sizeof(struct inodesc)); 649 idesc.id_type = ADDR; 650 idesc.id_func = pass4check; 651 idesc.id_number = ino; 652 vp = vget(fs, ino); 653 (void) ckinode(VTOD(vp), &idesc); 654 clearinode(ino); 655 statemap[ino] = USTATE; 656 vnode_destroy(vp); 657 658 n_files--; 659 } 660