1 /* $NetBSD: inode.c,v 1.38 2003/04/08 14:59:52 fvdl Exp $ */ 2 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95"; 40 #else 41 __RCSID("$NetBSD: inode.c,v 1.38 2003/04/08 14:59:52 fvdl Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/time.h> 47 48 #include <ufs/ufs/dinode.h> 49 #include <ufs/ufs/dir.h> 50 #include <ufs/ffs/fs.h> 51 #include <ufs/ffs/ffs_extern.h> 52 #include <ufs/ufs/ufs_bswap.h> 53 54 #ifndef SMALL 55 #include <err.h> 56 #include <pwd.h> 57 #endif 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <time.h> 62 63 #include "fsck.h" 64 #include "fsutil.h" 65 #include "extern.h" 66 67 static ino_t startinum; 68 69 static int iblock __P((struct inodesc *, long, u_int64_t)); 70 static void swap_dinode1(union dinode *, int); 71 static void swap_dinode2(union dinode *, int); 72 73 int 74 ckinode(dp, idesc) 75 union dinode *dp; 76 struct inodesc *idesc; 77 { 78 int ret, offset, i; 79 union dinode dino; 80 u_int64_t sizepb; 81 int64_t remsize; 82 daddr_t ndb; 83 mode_t mode; 84 char pathbuf[MAXPATHLEN + 1]; 85 86 if (idesc->id_fix != IGNORE) 87 idesc->id_fix = DONTKNOW; 88 idesc->id_entryno = 0; 89 idesc->id_filesize = iswap64(DIP(dp, size)); 90 mode = iswap16(DIP(dp, mode)) & IFMT; 91 if (mode == IFBLK || mode == IFCHR || (mode == IFLNK && 92 (idesc->id_filesize < sblock->fs_maxsymlinklen || 93 (isappleufs && (idesc->id_filesize < APPLEUFS_MAXSYMLINKLEN)) || 94 (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0)))) 95 return (KEEPON); 96 if (is_ufs2) 97 dino.dp2 = dp->dp2; 98 else 99 dino.dp1 = dp->dp1; 100 ndb = howmany(iswap64(DIP(&dino, size)), sblock->fs_bsize); 101 for (i = 0; i < NDADDR; i++) { 102 if (--ndb == 0 && 103 (offset = blkoff(sblock, iswap64(DIP(&dino, size)))) != 0) 104 idesc->id_numfrags = 105 numfrags(sblock, fragroundup(sblock, offset)); 106 else 107 idesc->id_numfrags = sblock->fs_frag; 108 if (DIP(&dino, db[i]) == 0) { 109 if (idesc->id_type == DATA && ndb >= 0) { 110 /* An empty block in a directory XXX */ 111 markclean = 0; 112 getpathname(pathbuf, idesc->id_number, 113 idesc->id_number); 114 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS I", 115 pathbuf); 116 abort(); 117 if (reply("ADJUST LENGTH") == 1) { 118 dp = ginode(idesc->id_number); 119 DIP(dp, size) = iswap64(i * 120 sblock->fs_bsize); 121 printf( 122 "YOU MUST RERUN FSCK AFTERWARDS\n"); 123 rerun = 1; 124 inodirty(); 125 } 126 } 127 continue; 128 } 129 if (is_ufs2) 130 idesc->id_blkno = iswap64(dino.dp2.di_db[i]); 131 else 132 idesc->id_blkno = iswap32(dino.dp1.di_db[i]); 133 if (idesc->id_type == ADDR) 134 ret = (*idesc->id_func)(idesc); 135 else 136 ret = dirscan(idesc); 137 if (ret & STOP) 138 return (ret); 139 } 140 idesc->id_numfrags = sblock->fs_frag; 141 remsize = iswap64(DIP(&dino, size)) - sblock->fs_bsize * NDADDR; 142 sizepb = sblock->fs_bsize; 143 for (i = 0; i < NIADDR; i++) { 144 if (DIP(&dino, ib[i])) { 145 if (is_ufs2) 146 idesc->id_blkno = iswap64(dino.dp2.di_ib[i]); 147 else 148 idesc->id_blkno = iswap32(dino.dp1.di_ib[i]); 149 ret = iblock(idesc, i + 1, remsize); 150 if (ret & STOP) 151 return (ret); 152 } else { 153 if (idesc->id_type == DATA && remsize > 0) { 154 /* An empty block in a directory XXX */ 155 markclean = 0; 156 getpathname(pathbuf, idesc->id_number, 157 idesc->id_number); 158 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 159 pathbuf); 160 if (reply("ADJUST LENGTH") == 1) { 161 dp = ginode(idesc->id_number); 162 DIP(dp, size) = 163 iswap64(iswap64(DIP(dp, size)) 164 - remsize); 165 remsize = 0; 166 printf( 167 "YOU MUST RERUN FSCK AFTERWARDS\n"); 168 rerun = 1; 169 inodirty(); 170 break; 171 } 172 } 173 } 174 sizepb *= NINDIR(sblock); 175 remsize -= sizepb; 176 } 177 return (KEEPON); 178 } 179 180 static int 181 iblock(idesc, ilevel, isize) 182 struct inodesc *idesc; 183 long ilevel; 184 u_int64_t isize; 185 { 186 struct bufarea *bp; 187 int i, n, (*func) __P((struct inodesc *)), nif; 188 u_int64_t sizepb; 189 char buf[BUFSIZ]; 190 char pathbuf[MAXPATHLEN + 1]; 191 union dinode *dp; 192 193 if (idesc->id_type == ADDR) { 194 func = idesc->id_func; 195 if (((n = (*func)(idesc)) & KEEPON) == 0) 196 return (n); 197 } else 198 func = dirscan; 199 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) 200 return (SKIP); 201 bp = getdatablk(idesc->id_blkno, sblock->fs_bsize); 202 ilevel--; 203 for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++) 204 sizepb *= NINDIR(sblock); 205 if (howmany(isize, sizepb) > NINDIR(sblock)) 206 nif = NINDIR(sblock); 207 else 208 nif = howmany(isize, sizepb); 209 if (do_blkswap) { /* swap byte order of the whole blk */ 210 if (is_ufs2) { 211 for (i = 0; i < nif; i++) 212 bp->b_un.b_indir2[i] = 213 bswap64(bp->b_un.b_indir2[i]); 214 } else { 215 for (i = 0; i < nif; i++) 216 bp->b_un.b_indir1[i] = 217 bswap32(bp->b_un.b_indir1[i]); 218 } 219 dirty(bp); 220 flush(fswritefd, bp); 221 } 222 if (idesc->id_func == pass1check && nif < NINDIR(sblock)) { 223 for (i = nif; i < NINDIR(sblock); i++) { 224 if (IBLK(bp, i) == 0) 225 continue; 226 (void)snprintf(buf, sizeof(buf), 227 "PARTIALLY TRUNCATED INODE I=%u", idesc->id_number); 228 if (dofix(idesc, buf)) { 229 IBLK(bp, i) = 0; 230 dirty(bp); 231 } else 232 markclean= 0; 233 } 234 flush(fswritefd, bp); 235 } 236 for (i = 0; i < nif; i++) { 237 if (IBLK(bp, i)) { 238 if (is_ufs2) 239 idesc->id_blkno = iswap64(bp->b_un.b_indir2[i]); 240 else 241 idesc->id_blkno = iswap32(bp->b_un.b_indir1[i]); 242 if (ilevel == 0) 243 n = (*func)(idesc); 244 else 245 n = iblock(idesc, ilevel, isize); 246 if (n & STOP) { 247 bp->b_flags &= ~B_INUSE; 248 return (n); 249 } 250 } else { 251 if (idesc->id_type == DATA && isize > 0) { 252 /* An empty block in a directory XXX */ 253 markclean= 0; 254 getpathname(pathbuf, idesc->id_number, 255 idesc->id_number); 256 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS", 257 pathbuf); 258 if (reply("ADJUST LENGTH") == 1) { 259 dp = ginode(idesc->id_number); 260 DIP(dp, size) = 261 iswap64(iswap64(DIP(dp, size)) 262 - isize); 263 isize = 0; 264 printf( 265 "YOU MUST RERUN FSCK AFTERWARDS\n"); 266 rerun = 1; 267 inodirty(); 268 bp->b_flags &= ~B_INUSE; 269 return(STOP); 270 } 271 } 272 } 273 isize -= sizepb; 274 } 275 bp->b_flags &= ~B_INUSE; 276 return (KEEPON); 277 } 278 279 /* 280 * Check that a block in a legal block number. 281 * Return 0 if in range, 1 if out of range. 282 */ 283 int 284 chkrange(blk, cnt) 285 daddr_t blk; 286 int cnt; 287 { 288 int c; 289 290 if ((unsigned)(blk + cnt) > maxfsblock) 291 return (1); 292 if (cnt > sblock->fs_frag || 293 fragnum(sblock, blk) + cnt > sblock->fs_frag) { 294 if (debug) 295 printf("bad size: blk %lld, offset %d, size %d\n", 296 (long long)blk, (int)fragnum(sblock, blk), cnt); 297 } 298 c = dtog(sblock, blk); 299 if (blk < cgdmin(sblock, c)) { 300 if ((blk + cnt) > cgsblock(sblock, c)) { 301 if (debug) { 302 printf("blk %lld < cgdmin %lld;", 303 (long long)blk, 304 (long long)cgdmin(sblock, c)); 305 printf(" blk + cnt %lld > cgsbase %lld\n", 306 (long long)(blk + cnt), 307 (long long)cgsblock(sblock, c)); 308 } 309 return (1); 310 } 311 } else { 312 if ((blk + cnt) > cgbase(sblock, c+1)) { 313 if (debug) { 314 printf("blk %lld >= cgdmin %lld;", 315 (long long)blk, 316 (long long)cgdmin(sblock, c)); 317 printf(" blk + cnt %lld > sblock->fs_fpg %d\n", 318 (long long)(blk+cnt), sblock->fs_fpg); 319 } 320 return (1); 321 } 322 } 323 return (0); 324 } 325 326 /* 327 * General purpose interface for reading inodes. 328 */ 329 union dinode * 330 ginode(inumber) 331 ino_t inumber; 332 { 333 daddr_t iblk; 334 int blkoff; 335 336 if (inumber < ROOTINO || inumber > maxino) 337 errx(EEXIT, "bad inode number %d to ginode", inumber); 338 if (startinum == 0 || 339 inumber < startinum || inumber >= startinum + INOPB(sblock)) { 340 iblk = ino_to_fsba(sblock, inumber); 341 if (pbp != 0) 342 pbp->b_flags &= ~B_INUSE; 343 pbp = getdatablk(iblk, sblock->fs_bsize); 344 startinum = (inumber / INOPB(sblock)) * INOPB(sblock); 345 } 346 if (is_ufs2) { 347 blkoff = (inumber % INOPB(sblock)) * DINODE2_SIZE; 348 return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff)); 349 } 350 blkoff = (inumber % INOPB(sblock)) * DINODE1_SIZE; 351 return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff)); 352 } 353 354 static void 355 swap_dinode1(union dinode *dp, int n) 356 { 357 int i, j; 358 struct ufs1_dinode *dp1; 359 360 dp1 = (struct ufs1_dinode *)&dp->dp1; 361 for (i = 0; i < n; i++, dp1++) { 362 ffs_dinode1_swap(dp1, dp1); 363 if ((iswap16(dp1->di_mode) & IFMT) != IFLNK || 364 (isappleufs && (iswap64(dp1->di_size) > 365 APPLEUFS_MAXSYMLINKLEN)) || 366 iswap64(dp1->di_size) > sblock->fs_maxsymlinklen) { 367 for (j = 0; j < (NDADDR + NIADDR); j++) 368 dp1->di_db[j] = bswap32(dp1->di_db[j]); 369 } 370 } 371 } 372 373 static void 374 swap_dinode2(union dinode *dp, int n) 375 { 376 int i, j; 377 struct ufs2_dinode *dp2; 378 379 dp2 = (struct ufs2_dinode *)&dp->dp2; 380 for (i = 0; i < n; i++, dp2++) { 381 ffs_dinode2_swap(dp2, dp2); 382 if ((iswap16(dp2->di_mode) & IFMT) != IFLNK) { 383 for (j = 0; j < (NDADDR + NIADDR + NXADDR); j++) 384 dp2->di_extb[j] = bswap64(dp2->di_extb[j]); 385 } 386 } 387 } 388 389 /* 390 * Special purpose version of ginode used to optimize first pass 391 * over all the inodes in numerical order. 392 */ 393 ino_t nextino, lastinum, lastvalidinum; 394 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 395 union dinode *inodebuf; 396 397 union dinode * 398 getnextinode(inumber) 399 ino_t inumber; 400 { 401 long size; 402 daddr_t dblk; 403 static union dinode *dp; 404 union dinode *ret; 405 406 if (inumber != nextino++ || inumber > lastvalidinum) { 407 abort(); 408 errx(EEXIT, "bad inode number %d to nextinode", inumber); 409 } 410 411 if (inumber >= lastinum) { 412 readcnt++; 413 dblk = fsbtodb(sblock, ino_to_fsba(sblock, lastinum)); 414 if (readcnt % readpercg == 0) { 415 size = partialsize; 416 lastinum += partialcnt; 417 } else { 418 size = inobufsize; 419 lastinum += fullcnt; 420 } 421 (void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size); 422 if (doswap) { 423 if (is_ufs2) 424 swap_dinode2(inodebuf, lastinum - inumber); 425 else 426 swap_dinode1(inodebuf, lastinum - inumber); 427 bwrite(fswritefd, (char *)inodebuf, dblk, size); 428 } 429 dp = (union dinode *)inodebuf; 430 } 431 ret = dp; 432 dp = (union dinode *) 433 ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE)); 434 return ret; 435 } 436 437 void 438 setinodebuf(inum) 439 ino_t inum; 440 { 441 442 if (inum % sblock->fs_ipg != 0) 443 errx(EEXIT, "bad inode number %d to setinodebuf", inum); 444 445 lastvalidinum = inum + sblock->fs_ipg - 1; 446 startinum = 0; 447 nextino = inum; 448 lastinum = inum; 449 readcnt = 0; 450 if (inodebuf != NULL) 451 return; 452 inobufsize = blkroundup(sblock, INOBUFSIZE); 453 fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE); 454 readpercg = sblock->fs_ipg / fullcnt; 455 partialcnt = sblock->fs_ipg % fullcnt; 456 partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE); 457 if (partialcnt != 0) { 458 readpercg++; 459 } else { 460 partialcnt = fullcnt; 461 partialsize = inobufsize; 462 } 463 if (inodebuf == NULL && 464 (inodebuf = malloc((unsigned)inobufsize)) == NULL) 465 errx(EEXIT, "Cannot allocate space for inode buffer"); 466 } 467 468 void 469 freeinodebuf() 470 { 471 472 if (inodebuf != NULL) 473 free((char *)inodebuf); 474 inodebuf = NULL; 475 } 476 477 /* 478 * Routines to maintain information about directory inodes. 479 * This is built during the first pass and used during the 480 * second and third passes. 481 * 482 * Enter inodes into the cache. 483 */ 484 void 485 cacheino(dp, inumber) 486 union dinode *dp; 487 ino_t inumber; 488 { 489 struct inoinfo *inp; 490 struct inoinfo **inpp; 491 unsigned int blks; 492 int i; 493 int64_t size; 494 495 size = iswap64(DIP(dp, size)); 496 blks = howmany(size, sblock->fs_bsize); 497 if (blks > NDADDR) 498 blks = NDADDR + NIADDR; 499 500 inp = (struct inoinfo *) malloc(sizeof(*inp) + (blks - 1) 501 * sizeof (int64_t)); 502 if (inp == NULL) 503 return; 504 inpp = &inphead[inumber % dirhash]; 505 inp->i_nexthash = *inpp; 506 *inpp = inp; 507 inp->i_child = inp->i_sibling = inp->i_parentp = 0; 508 if (inumber == ROOTINO) 509 inp->i_parent = ROOTINO; 510 else 511 inp->i_parent = (ino_t)0; 512 inp->i_dotdot = (ino_t)0; 513 inp->i_number = inumber; 514 inp->i_isize = size; 515 inp->i_numblks = blks; 516 for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++) 517 inp->i_blks[i] = DIP(dp, db[i]); 518 if (blks > NDADDR) 519 for (i = 0; i < NIADDR; i++) 520 inp->i_blks[NDADDR + i] = DIP(dp, ib[i]); 521 if (inplast == listmax) { 522 listmax += 100; 523 inpsort = (struct inoinfo **)realloc((char *)inpsort, 524 (unsigned)listmax * sizeof(struct inoinfo *)); 525 if (inpsort == NULL) 526 errx(EEXIT, "cannot increase directory list"); 527 } 528 inpsort[inplast++] = inp; 529 } 530 531 /* 532 * Look up an inode cache structure. 533 */ 534 struct inoinfo * 535 getinoinfo(inumber) 536 ino_t inumber; 537 { 538 struct inoinfo *inp; 539 540 for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) { 541 if (inp->i_number != inumber) 542 continue; 543 return (inp); 544 } 545 errx(EEXIT, "cannot find inode %d", inumber); 546 return ((struct inoinfo *)0); 547 } 548 549 /* 550 * Clean up all the inode cache structure. 551 */ 552 void 553 inocleanup() 554 { 555 struct inoinfo **inpp; 556 557 if (inphead == NULL) 558 return; 559 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) 560 free((char *)(*inpp)); 561 free((char *)inphead); 562 free((char *)inpsort); 563 inphead = inpsort = NULL; 564 } 565 566 void 567 inodirty() 568 { 569 570 dirty(pbp); 571 } 572 573 void 574 clri(idesc, type, flag) 575 struct inodesc *idesc; 576 char *type; 577 int flag; 578 { 579 union dinode *dp; 580 581 dp = ginode(idesc->id_number); 582 if (flag == 1) { 583 pwarn("%s %s", type, 584 (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? "DIR" : "FILE"); 585 pinode(idesc->id_number); 586 } 587 if (preen || reply("CLEAR") == 1) { 588 if (preen) 589 printf(" (CLEARED)\n"); 590 n_files--; 591 (void)ckinode(dp, idesc); 592 clearinode(dp); 593 inoinfo(idesc->id_number)->ino_state = USTATE; 594 inodirty(); 595 } else 596 markclean= 0; 597 } 598 599 int 600 findname(idesc) 601 struct inodesc *idesc; 602 { 603 struct direct *dirp = idesc->id_dirp; 604 605 if (iswap32(dirp->d_ino) != idesc->id_parent || idesc->id_entryno < 2) { 606 idesc->id_entryno++; 607 return (KEEPON); 608 } 609 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1); 610 return (STOP|FOUND); 611 } 612 613 int 614 findino(idesc) 615 struct inodesc *idesc; 616 { 617 struct direct *dirp = idesc->id_dirp; 618 619 if (dirp->d_ino == 0) 620 return (KEEPON); 621 if (strcmp(dirp->d_name, idesc->id_name) == 0 && 622 iswap32(dirp->d_ino) >= ROOTINO && iswap32(dirp->d_ino) <= maxino) { 623 idesc->id_parent = iswap32(dirp->d_ino); 624 return (STOP|FOUND); 625 } 626 return (KEEPON); 627 } 628 629 int 630 clearentry(idesc) 631 struct inodesc *idesc; 632 { 633 struct direct *dirp = idesc->id_dirp; 634 635 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) { 636 idesc->id_entryno++; 637 return (KEEPON); 638 } 639 dirp->d_ino = 0; 640 return (STOP|FOUND|ALTERED); 641 } 642 643 void 644 pinode(ino) 645 ino_t ino; 646 { 647 union dinode *dp; 648 char *p; 649 struct passwd *pw; 650 time_t t; 651 652 printf(" I=%u ", ino); 653 if (ino < ROOTINO || ino > maxino) 654 return; 655 dp = ginode(ino); 656 printf(" OWNER="); 657 #ifndef SMALL 658 if ((pw = getpwuid((int)iswap32(DIP(dp, uid)))) != 0) 659 printf("%s ", pw->pw_name); 660 else 661 #endif 662 printf("%u ", (unsigned)iswap32(DIP(dp, uid))); 663 printf("MODE=%o\n", iswap16(DIP(dp, mode))); 664 if (preen) 665 printf("%s: ", cdevname()); 666 printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size))); 667 t = iswap32(DIP(dp, mtime)); 668 p = ctime(&t); 669 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]); 670 } 671 672 void 673 blkerror(ino, type, blk) 674 ino_t ino; 675 char *type; 676 daddr_t blk; 677 { 678 struct inostat *info; 679 680 pfatal("%lld %s I=%u", (long long)blk, type, ino); 681 printf("\n"); 682 info = inoinfo(ino); 683 switch (info->ino_state) { 684 685 case FSTATE: 686 info->ino_state = FCLEAR; 687 return; 688 689 case DSTATE: 690 info->ino_state = DCLEAR; 691 return; 692 693 case FCLEAR: 694 case DCLEAR: 695 return; 696 697 default: 698 errx(EEXIT, "BAD STATE %d TO BLKERR", info->ino_state); 699 /* NOTREACHED */ 700 } 701 } 702 703 /* 704 * allocate an unused inode 705 */ 706 ino_t 707 allocino(request, type) 708 ino_t request; 709 int type; 710 { 711 ino_t ino; 712 union dinode *dp; 713 struct ufs1_dinode *dp1; 714 struct ufs2_dinode *dp2; 715 time_t t; 716 struct cg *cgp = cgrp; 717 int cg; 718 struct inostat *info; 719 720 if (request == 0) 721 request = ROOTINO; 722 else if (inoinfo(request)->ino_state != USTATE) 723 return (0); 724 for (ino = request; ino < maxino; ino++) { 725 info = inoinfo(ino); 726 if (info->ino_state == USTATE) 727 break; 728 } 729 if (ino == maxino) 730 return (0); 731 cg = ino_to_cg(sblock, ino); 732 getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize); 733 memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize); 734 if ((doswap && !needswap) || (!doswap && needswap)) 735 ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock); 736 if (!cg_chkmagic(cgp, 0)) 737 pfatal("CG %d: ALLOCINO: BAD MAGIC NUMBER\n", cg); 738 if (doswap) 739 cgdirty(); 740 setbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg); 741 cgp->cg_cs.cs_nifree--; 742 switch (type & IFMT) { 743 case IFDIR: 744 info->ino_state = DSTATE; 745 cgp->cg_cs.cs_ndir++; 746 break; 747 case IFREG: 748 case IFLNK: 749 info->ino_state = FSTATE; 750 break; 751 default: 752 return (0); 753 } 754 cgdirty(); 755 dp = ginode(ino); 756 if (is_ufs2) { 757 dp2 = &dp->dp2; 758 dp2->di_db[0] = iswap64(allocblk(1)); 759 if (dp2->di_db[0] == 0) { 760 info->ino_state = USTATE; 761 return (0); 762 } 763 dp2->di_mode = iswap16(type); 764 dp2->di_flags = 0; 765 (void)time(&t); 766 dp2->di_atime = iswap64(t); 767 dp2->di_mtime = dp2->di_ctime = dp2->di_atime; 768 dp2->di_size = iswap64(sblock->fs_fsize); 769 dp2->di_blocks = iswap64(btodb(sblock->fs_fsize)); 770 } else { 771 dp1 = &dp->dp1; 772 dp1->di_db[0] = iswap32(allocblk(1)); 773 if (dp1->di_db[0] == 0) { 774 info->ino_state = USTATE; 775 return (0); 776 } 777 dp1->di_mode = iswap16(type); 778 dp1->di_flags = 0; 779 (void)time(&t); 780 dp1->di_atime = iswap32(t); 781 dp1->di_mtime = dp1->di_ctime = dp1->di_atime; 782 dp1->di_size = iswap64(sblock->fs_fsize); 783 dp1->di_blocks = iswap32(btodb(sblock->fs_fsize)); 784 } 785 n_files++; 786 inodirty(); 787 if (newinofmt) 788 info->ino_type = IFTODT(type); 789 return (ino); 790 } 791 792 /* 793 * deallocate an inode 794 */ 795 void 796 freeino(ino) 797 ino_t ino; 798 { 799 struct inodesc idesc; 800 union dinode *dp; 801 802 memset(&idesc, 0, sizeof(struct inodesc)); 803 idesc.id_type = ADDR; 804 idesc.id_func = pass4check; 805 idesc.id_number = ino; 806 dp = ginode(ino); 807 (void)ckinode(dp, &idesc); 808 clearinode(dp); 809 inodirty(); 810 inoinfo(ino)->ino_state = USTATE; 811 n_files--; 812 } 813