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