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[] = "@(#)dir.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/time.h> 42 #include <sys/types.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 <string.h> 51 52 #include "fsck.h" 53 54 static struct dirtemplate emptydir = { 55 0, DIRBLKSIZ, DT_UNKNOWN, 0, "", 56 0, 0, DT_UNKNOWN, 0, "" 57 }; 58 static struct dirtemplate dirhead = { 59 0, 12, DT_DIR, 1, ".", 60 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." 61 }; 62 63 static int chgino(struct inodesc *); 64 static int dircheck(struct inodesc *, struct bufarea *, struct direct *); 65 static int expanddir(struct inode *ip, char *name); 66 static struct direct *fsck_readdir(struct inodesc *); 67 static struct bufarea *getdirblk(ufs2_daddr_t blkno, long size); 68 static int lftempname(char *bufp, ino_t ino); 69 static int mkentry(struct inodesc *); 70 71 /* 72 * Propagate connected state through the tree. 73 */ 74 void 75 propagate(void) 76 { 77 struct inoinfo **inpp, *inp; 78 struct inoinfo **inpend; 79 long change; 80 81 inpend = &inpsort[inplast]; 82 do { 83 change = 0; 84 for (inpp = inpsort; inpp < inpend; inpp++) { 85 inp = *inpp; 86 if (inp->i_parent == 0) 87 continue; 88 if (inoinfo(inp->i_parent)->ino_state == DFOUND && 89 INO_IS_DUNFOUND(inp->i_number)) { 90 inoinfo(inp->i_number)->ino_state = DFOUND; 91 change++; 92 } 93 } 94 } while (change > 0); 95 } 96 97 /* 98 * Scan each entry in a directory block. 99 */ 100 int 101 dirscan(struct inodesc *idesc) 102 { 103 struct direct *dp; 104 struct bufarea *bp; 105 u_int dsize, n; 106 long blksiz; 107 char dbuf[DIRBLKSIZ]; 108 109 if (idesc->id_type != DATA) 110 errx(EEXIT, "wrong type to dirscan %d", idesc->id_type); 111 if (idesc->id_entryno == 0 && 112 (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0) 113 idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ); 114 blksiz = idesc->id_numfrags * sblock.fs_fsize; 115 if (chkrange(idesc->id_blkno, idesc->id_numfrags)) { 116 idesc->id_filesize -= blksiz; 117 return (SKIP); 118 } 119 idesc->id_loc = 0; 120 for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 121 dsize = dp->d_reclen; 122 if (dsize > sizeof(dbuf)) 123 dsize = sizeof(dbuf); 124 memmove(dbuf, dp, (size_t)dsize); 125 idesc->id_dirp = (struct direct *)dbuf; 126 if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 127 bp = getdirblk(idesc->id_blkno, blksiz); 128 if (bp->b_errs != 0) 129 return (STOP); 130 memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf, 131 (size_t)dsize); 132 dirty(bp); 133 sbdirty(); 134 } 135 if (n & STOP) 136 return (n); 137 } 138 return (idesc->id_filesize > 0 ? KEEPON : STOP); 139 } 140 141 /* 142 * Get and verify the next entry in a directory. 143 * We also verify that if there is another entry in the block that it is 144 * valid, so if it is not valid it can be subsumed into the current entry. 145 */ 146 static struct direct * 147 fsck_readdir(struct inodesc *idesc) 148 { 149 struct direct *dp, *ndp; 150 struct bufarea *bp; 151 long size, blksiz, subsume_ndp; 152 153 subsume_ndp = 0; 154 blksiz = idesc->id_numfrags * sblock.fs_fsize; 155 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 156 return (NULL); 157 bp = getdirblk(idesc->id_blkno, blksiz); 158 if (bp->b_errs != 0) 159 return (NULL); 160 dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 161 /* 162 * Only need to check current entry if it is the first in the 163 * the block, as later entries will have been checked in the 164 * previous call to this function. 165 */ 166 if (idesc->id_loc % DIRBLKSIZ != 0 || dircheck(idesc, bp, dp) != 0) { 167 /* 168 * Current entry is good, update to point at next. 169 */ 170 idesc->id_loc += dp->d_reclen; 171 idesc->id_filesize -= dp->d_reclen; 172 /* 173 * If at end of directory block, just return this entry. 174 */ 175 if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz || 176 idesc->id_loc % DIRBLKSIZ == 0) 177 return (dp); 178 /* 179 * If the next entry good, return this entry. 180 */ 181 ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 182 if (dircheck(idesc, bp, ndp) != 0) 183 return (dp); 184 /* 185 * The next entry is bad, so subsume it and the remainder 186 * of this directory block into this entry. 187 */ 188 subsume_ndp = 1; 189 } 190 /* 191 * Current or next entry is bad. Zap current entry or 192 * subsume next entry into current entry as appropriate. 193 */ 194 size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 195 idesc->id_loc += size; 196 idesc->id_filesize -= size; 197 if (idesc->id_fix == IGNORE) 198 return (NULL); 199 if (subsume_ndp) { 200 memset(ndp, 0, size); 201 dp->d_reclen += size; 202 } else { 203 memset(dp, 0, size); 204 dp->d_reclen = size; 205 } 206 if (dofix(idesc, "DIRECTORY CORRUPTED")) 207 dirty(bp); 208 return (dp); 209 } 210 211 /* 212 * Verify that a directory entry is valid. 213 * This is a superset of the checks made in the kernel. 214 * Also optionally clears padding and unused directory space. 215 * 216 * Returns 0 if the entry is bad, 1 if the entry is good. 217 */ 218 static int 219 dircheck(struct inodesc *idesc, struct bufarea *bp, struct direct *dp) 220 { 221 size_t size; 222 char *cp; 223 u_int8_t namlen; 224 int spaceleft, modified, unused; 225 226 spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 227 size = DIRSIZ(0, dp); 228 if (dp->d_reclen == 0 || 229 dp->d_reclen > spaceleft || 230 dp->d_reclen < size || 231 idesc->id_filesize < size || 232 (dp->d_reclen & (DIR_ROUNDUP - 1)) != 0) 233 goto bad; 234 modified = 0; 235 if (dp->d_ino == 0) { 236 if (!zflag || fswritefd < 0) 237 return (1); 238 /* 239 * Special case of an unused directory entry. Normally only 240 * occurs at the beginning of a directory block when the block 241 * contains no entries. Other than the first entry in a 242 * directory block, the kernel coalesces unused space with 243 * the previous entry by extending its d_reclen. However, 244 * when cleaning up a directory, fsck may set d_ino to zero 245 * in the middle of a directory block. If we're clearing out 246 * directory cruft (-z flag), then make sure that all directory 247 * space in entries with d_ino == 0 gets fully cleared. 248 */ 249 if (dp->d_type != 0) { 250 dp->d_type = 0; 251 modified = 1; 252 } 253 if (dp->d_namlen != 0) { 254 dp->d_namlen = 0; 255 modified = 1; 256 } 257 unused = dp->d_reclen - __offsetof(struct direct, d_name); 258 for (cp = dp->d_name; unused > 0; unused--, cp++) { 259 if (*cp != '\0') { 260 *cp = '\0'; 261 modified = 1; 262 } 263 } 264 if (modified) 265 dirty(bp); 266 return (1); 267 } 268 /* 269 * The d_type field should not be tested here. A bad type is an error 270 * in the entry itself but is not a corruption of the directory 271 * structure itself. So blowing away all the remaining entries in the 272 * directory block is inappropriate. Rather the type error should be 273 * checked in pass1 and fixed there. 274 * 275 * The name validation should also be done in pass1 although the 276 * check to see if the name is longer than fits in the space 277 * allocated for it (i.e., the *cp != '\0' fails after exiting the 278 * loop below) then it really is a structural error that requires 279 * the stronger action taken here. 280 */ 281 namlen = dp->d_namlen; 282 if (namlen == 0 || dp->d_type > 15) 283 goto bad; 284 for (cp = dp->d_name, size = 0; size < namlen; size++) { 285 if (*cp == '\0' || *cp++ == '/') 286 goto bad; 287 } 288 if (*cp != '\0') 289 goto bad; 290 if (zflag && fswritefd >= 0) { 291 /* 292 * Clear unused directory entry space, including the d_name 293 * padding. 294 */ 295 /* First figure the number of pad bytes. */ 296 unused = roundup2(namlen + 1, DIR_ROUNDUP) - (namlen + 1); 297 298 /* Add in the free space to the end of the record. */ 299 unused += dp->d_reclen - DIRSIZ(0, dp); 300 301 /* 302 * Now clear out the unused space, keeping track if we actually 303 * changed anything. 304 */ 305 for (cp = &dp->d_name[namlen + 1]; unused > 0; unused--, cp++) { 306 if (*cp != '\0') { 307 *cp = '\0'; 308 modified = 1; 309 } 310 } 311 312 if (modified) 313 dirty(bp); 314 } 315 return (1); 316 317 bad: 318 if (debug) 319 printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n", 320 dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type, 321 dp->d_name); 322 return (0); 323 } 324 325 void 326 direrror(ino_t ino, const char *errmesg) 327 { 328 329 fileerror(ino, ino, errmesg); 330 } 331 332 void 333 fileerror(ino_t cwd, ino_t ino, const char *errmesg) 334 { 335 struct inode ip; 336 union dinode *dp; 337 char pathbuf[MAXPATHLEN + 1]; 338 339 pwarn("%s ", errmesg); 340 if (ino < UFS_ROOTINO || ino > maxino) { 341 pfatal("out-of-range inode number %ju", (uintmax_t)ino); 342 return; 343 } 344 ginode(ino, &ip); 345 dp = ip.i_dp; 346 prtinode(&ip); 347 printf("\n"); 348 getpathname(pathbuf, cwd, ino); 349 if (ftypeok(dp)) 350 pfatal("%s=%s\n", 351 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE", 352 pathbuf); 353 else 354 pfatal("NAME=%s\n", pathbuf); 355 irelse(&ip); 356 } 357 358 void 359 adjust(struct inodesc *idesc, int lcnt) 360 { 361 struct inode ip; 362 union dinode *dp; 363 int saveresolved; 364 365 ginode(idesc->id_number, &ip); 366 dp = ip.i_dp; 367 if (DIP(dp, di_nlink) == lcnt) { 368 /* 369 * If we have not hit any unresolved problems, are running 370 * in preen mode, and are on a file system using soft updates, 371 * then just toss any partially allocated files. 372 */ 373 if (resolved && (preen || bkgrdflag) && usedsoftdep) { 374 clri(idesc, "UNREF", 1); 375 irelse(&ip); 376 return; 377 } else { 378 /* 379 * The file system can be marked clean even if 380 * a file is not linked up, but is cleared. 381 * Hence, resolved should not be cleared when 382 * linkup is answered no, but clri is answered yes. 383 */ 384 saveresolved = resolved; 385 if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) { 386 resolved = saveresolved; 387 clri(idesc, "UNREF", 0); 388 irelse(&ip); 389 return; 390 } 391 /* 392 * Account for the new reference created by linkup(). 393 */ 394 lcnt--; 395 } 396 } 397 if (lcnt != 0) { 398 pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 399 ((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE")); 400 prtinode(&ip); 401 printf(" COUNT %d SHOULD BE %d", 402 DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt); 403 if (preen || usedsoftdep) { 404 if (lcnt < 0) { 405 printf("\n"); 406 pfatal("LINK COUNT INCREASING"); 407 } 408 if (preen) 409 printf(" (ADJUSTED)\n"); 410 } 411 if (preen || reply("ADJUST") == 1) { 412 if (bkgrdflag == 0) { 413 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt); 414 inodirty(&ip); 415 } else { 416 cmd.value = idesc->id_number; 417 cmd.size = -lcnt; 418 if (debug) 419 printf("adjrefcnt ino %ld amt %lld\n", 420 (long)cmd.value, 421 (long long)cmd.size); 422 if (sysctl(adjrefcnt, MIBSIZE, 0, 0, 423 &cmd, sizeof cmd) == -1) 424 rwerror("ADJUST INODE", cmd.value); 425 } 426 } 427 } 428 irelse(&ip); 429 } 430 431 static int 432 mkentry(struct inodesc *idesc) 433 { 434 struct direct *dirp = idesc->id_dirp; 435 struct direct newent; 436 int newlen, oldlen; 437 438 newent.d_namlen = strlen(idesc->id_name); 439 newlen = DIRSIZ(0, &newent); 440 if (dirp->d_ino != 0) 441 oldlen = DIRSIZ(0, dirp); 442 else 443 oldlen = 0; 444 if (dirp->d_reclen - oldlen < newlen) 445 return (KEEPON); 446 newent.d_reclen = dirp->d_reclen - oldlen; 447 dirp->d_reclen = oldlen; 448 dirp = (struct direct *)(((char *)dirp) + oldlen); 449 dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 450 dirp->d_reclen = newent.d_reclen; 451 dirp->d_type = inoinfo(idesc->id_parent)->ino_type; 452 dirp->d_namlen = newent.d_namlen; 453 memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1); 454 return (ALTERED|STOP); 455 } 456 457 static int 458 chgino(struct inodesc *idesc) 459 { 460 struct direct *dirp = idesc->id_dirp; 461 462 if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1)) 463 return (KEEPON); 464 dirp->d_ino = idesc->id_parent; 465 dirp->d_type = inoinfo(idesc->id_parent)->ino_type; 466 return (ALTERED|STOP); 467 } 468 469 int 470 linkup(ino_t orphan, ino_t parentdir, char *name) 471 { 472 struct inode ip; 473 union dinode *dp; 474 int lostdir; 475 ino_t oldlfdir; 476 struct inoinfo *inp; 477 struct inodesc idesc; 478 char tempname[BUFSIZ]; 479 480 memset(&idesc, 0, sizeof(struct inodesc)); 481 ginode(orphan, &ip); 482 dp = ip.i_dp; 483 lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR; 484 pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 485 prtinode(&ip); 486 printf("\n"); 487 if (preen && DIP(dp, di_size) == 0) { 488 irelse(&ip); 489 return (0); 490 } 491 irelse(&ip); 492 if (cursnapshot != 0) { 493 pfatal("FILE LINKUP IN SNAPSHOT"); 494 return (0); 495 } 496 if (preen) 497 printf(" (RECONNECTED)\n"); 498 else if (reply("RECONNECT") == 0) 499 return (0); 500 if (lfdir == 0) { 501 ginode(UFS_ROOTINO, &ip); 502 idesc.id_name = strdup(lfname); 503 idesc.id_type = DATA; 504 idesc.id_func = findino; 505 idesc.id_number = UFS_ROOTINO; 506 if ((ckinode(ip.i_dp, &idesc) & FOUND) != 0) { 507 lfdir = idesc.id_parent; 508 } else { 509 pwarn("NO lost+found DIRECTORY"); 510 if (preen || reply("CREATE")) { 511 lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode); 512 if (lfdir != 0) { 513 if (makeentry(UFS_ROOTINO, lfdir, 514 lfname) != 0) { 515 numdirs++; 516 if (preen) 517 printf(" (CREATED)\n"); 518 } else { 519 freedirino(lfdir, UFS_ROOTINO); 520 lfdir = 0; 521 if (preen) 522 printf("\n"); 523 } 524 } 525 } 526 } 527 irelse(&ip); 528 free(idesc.id_name); 529 if (lfdir == 0) { 530 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 531 printf("\n\n"); 532 return (0); 533 } 534 } 535 ginode(lfdir, &ip); 536 dp = ip.i_dp; 537 if ((DIP(dp, di_mode) & IFMT) != IFDIR) { 538 pfatal("lost+found IS NOT A DIRECTORY"); 539 if (reply("REALLOCATE") == 0) { 540 irelse(&ip); 541 return (0); 542 } 543 oldlfdir = lfdir; 544 if ((lfdir = allocdir(UFS_ROOTINO, (ino_t)0, lfmode)) == 0) { 545 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 546 irelse(&ip); 547 return (0); 548 } 549 if ((changeino(UFS_ROOTINO, lfname, lfdir) & ALTERED) == 0) { 550 pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 551 irelse(&ip); 552 return (0); 553 } 554 idesc.id_type = inoinfo(oldlfdir)->ino_idtype; 555 idesc.id_func = freeblock; 556 idesc.id_number = oldlfdir; 557 adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1); 558 inoinfo(oldlfdir)->ino_linkcnt = 0; 559 inodirty(&ip); 560 irelse(&ip); 561 ginode(lfdir, &ip); 562 dp = ip.i_dp; 563 } 564 if (inoinfo(lfdir)->ino_state != DFOUND) { 565 pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 566 irelse(&ip); 567 return (0); 568 } 569 (void)lftempname(tempname, orphan); 570 if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) { 571 pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 572 printf("\n\n"); 573 irelse(&ip); 574 return (0); 575 } 576 inoinfo(orphan)->ino_linkcnt--; 577 if (lostdir) { 578 if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 && 579 parentdir != (ino_t)-1) 580 (void)makeentry(orphan, lfdir, ".."); 581 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1); 582 inodirty(&ip); 583 inoinfo(lfdir)->ino_linkcnt++; 584 pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan); 585 inp = getinoinfo(parentdir); 586 if (parentdir != (ino_t)-1 && inp != NULL) { 587 printf("PARENT WAS I=%lu\n", (u_long)parentdir); 588 /* 589 * If the parent directory did not have to 590 * be replaced then because of the ordering 591 * guarantees, has had the link count incremented 592 * for the child, but no entry was made. This 593 * fixes the parent link count so that fsck does 594 * not need to be rerun. 595 */ 596 if ((inp->i_flags & INFO_NEW) != 0) 597 inoinfo(parentdir)->ino_linkcnt++; 598 } 599 if (preen == 0) 600 printf("\n"); 601 } 602 irelse(&ip); 603 return (1); 604 } 605 606 /* 607 * fix an entry in a directory. 608 */ 609 int 610 changeino(ino_t dir, const char *name, ino_t newnum) 611 { 612 struct inodesc idesc; 613 struct inode ip; 614 int error; 615 616 memset(&idesc, 0, sizeof(struct inodesc)); 617 idesc.id_type = DATA; 618 idesc.id_func = chgino; 619 idesc.id_number = dir; 620 idesc.id_fix = DONTKNOW; 621 idesc.id_name = strdup(name); 622 idesc.id_parent = newnum; /* new value for name */ 623 ginode(dir, &ip); 624 error = ckinode(ip.i_dp, &idesc); 625 free(idesc.id_name); 626 irelse(&ip); 627 return (error); 628 } 629 630 /* 631 * make an entry in a directory 632 */ 633 int 634 makeentry(ino_t parent, ino_t ino, const char *name) 635 { 636 struct inode ip; 637 union dinode *dp; 638 struct inodesc idesc; 639 int retval; 640 char pathbuf[MAXPATHLEN + 1]; 641 642 if (parent < UFS_ROOTINO || parent >= maxino || 643 ino < UFS_ROOTINO || ino >= maxino) 644 return (0); 645 memset(&idesc, 0, sizeof(struct inodesc)); 646 idesc.id_type = DATA; 647 idesc.id_func = mkentry; 648 idesc.id_number = parent; 649 idesc.id_parent = ino; /* this is the inode to enter */ 650 idesc.id_fix = DONTKNOW; 651 idesc.id_name = strdup(name); 652 ginode(parent, &ip); 653 dp = ip.i_dp; 654 if (DIP(dp, di_size) % DIRBLKSIZ) { 655 DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ)); 656 inodirty(&ip); 657 } 658 if ((ckinode(dp, &idesc) & ALTERED) != 0) { 659 irelse(&ip); 660 free(idesc.id_name); 661 return (1); 662 } 663 getpathname(pathbuf, parent, parent); 664 if (expanddir(&ip, pathbuf) == 0) { 665 irelse(&ip); 666 free(idesc.id_name); 667 return (0); 668 } 669 retval = ckinode(dp, &idesc) & ALTERED; 670 irelse(&ip); 671 free(idesc.id_name); 672 return (retval); 673 } 674 675 /* 676 * Attempt to expand the size of a directory 677 */ 678 static int 679 expanddir(struct inode *ip, char *name) 680 { 681 ufs2_daddr_t lastlbn, oldblk, newblk, indirblk; 682 size_t filesize, lastlbnsize; 683 struct bufarea *bp, *nbp; 684 struct inodesc idesc; 685 union dinode *dp; 686 long cg, indiralloced; 687 char *cp; 688 689 nbp = NULL; 690 indiralloced = newblk = indirblk = 0; 691 memset(&idesc, 0, sizeof(struct inodesc)); 692 idesc.id_type = ADDR; 693 pwarn("NO SPACE LEFT IN %s", name); 694 if (!preen && reply("EXPAND") == 0) 695 return (0); 696 cg = ino_to_cg(&sblock, ip->i_number); 697 dp = ip->i_dp; 698 filesize = DIP(dp, di_size); 699 lastlbn = lblkno(&sblock, filesize); 700 /* 701 * We only expand lost+found to a single indirect block. 702 */ 703 if ((DIP(dp, di_mode) & IFMT) != IFDIR || filesize == 0 || 704 lastlbn >= UFS_NDADDR + NINDIR(&sblock)) 705 goto bad; 706 /* 707 * If last block is a fragment, expand it to a full size block. 708 */ 709 lastlbnsize = sblksize(&sblock, filesize, lastlbn); 710 if (lastlbnsize > 0 && lastlbnsize < sblock.fs_bsize) { 711 oldblk = DIP(dp, di_db[lastlbn]); 712 bp = getdirblk(oldblk, lastlbnsize); 713 if (bp->b_errs) 714 goto bad; 715 newblk = allocblk(cg, sblock.fs_frag, std_checkblkavail); 716 if (newblk == 0) 717 goto bad; 718 nbp = getdatablk(newblk, sblock.fs_bsize, BT_DIRDATA); 719 if (nbp->b_errs) 720 goto bad; 721 DIP_SET(dp, di_db[lastlbn], newblk); 722 DIP_SET(dp, di_size, filesize + sblock.fs_bsize - lastlbnsize); 723 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + 724 btodb(sblock.fs_bsize - lastlbnsize)); 725 inodirty(ip); 726 memmove(nbp->b_un.b_buf, bp->b_un.b_buf, lastlbnsize); 727 memset(&nbp->b_un.b_buf[lastlbnsize], 0, 728 sblock.fs_bsize - lastlbnsize); 729 for (cp = &nbp->b_un.b_buf[lastlbnsize]; 730 cp < &nbp->b_un.b_buf[sblock.fs_bsize]; 731 cp += DIRBLKSIZ) 732 memmove(cp, &emptydir, sizeof emptydir); 733 dirty(nbp); 734 brelse(nbp); 735 binval(bp); 736 idesc.id_blkno = oldblk; 737 idesc.id_numfrags = numfrags(&sblock, lastlbnsize); 738 (void)freeblock(&idesc); 739 if (preen) 740 printf(" (EXPANDED)\n"); 741 return (1); 742 } 743 if ((newblk = allocblk(cg, sblock.fs_frag, std_checkblkavail)) == 0) 744 goto bad; 745 bp = getdirblk(newblk, sblock.fs_bsize); 746 if (bp->b_errs) 747 goto bad; 748 memset(bp->b_un.b_buf, 0, sblock.fs_bsize); 749 for (cp = bp->b_un.b_buf; 750 cp < &bp->b_un.b_buf[sblock.fs_bsize]; 751 cp += DIRBLKSIZ) 752 memmove(cp, &emptydir, sizeof emptydir); 753 dirty(bp); 754 if (lastlbn < UFS_NDADDR) { 755 DIP_SET(dp, di_db[lastlbn], newblk); 756 } else { 757 /* 758 * Allocate indirect block if needed. 759 */ 760 if ((indirblk = DIP(dp, di_ib[0])) == 0) { 761 indirblk = allocblk(cg, sblock.fs_frag, 762 std_checkblkavail); 763 if (indirblk == 0) { 764 binval(bp); 765 goto bad; 766 } 767 indiralloced = 1; 768 } 769 nbp = getdatablk(indirblk, sblock.fs_bsize, BT_LEVEL1); 770 if (nbp->b_errs) 771 goto bad; 772 if (indiralloced) { 773 memset(nbp->b_un.b_buf, 0, sblock.fs_bsize); 774 DIP_SET(dp, di_ib[0], indirblk); 775 DIP_SET(dp, di_blocks, 776 DIP(dp, di_blocks) + btodb(sblock.fs_bsize)); 777 } 778 IBLK_SET(nbp, lastlbn - UFS_NDADDR, newblk); 779 dirty(nbp); 780 brelse(nbp); 781 } 782 DIP_SET(dp, di_size, filesize + sblock.fs_bsize); 783 DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize)); 784 inodirty(ip); 785 if (preen) 786 printf(" (EXPANDED)\n"); 787 return (1); 788 bad: 789 pfatal(" (EXPANSION FAILED)\n"); 790 if (nbp != NULL) { 791 binval(bp); 792 brelse(nbp); 793 } 794 if (newblk != 0) { 795 idesc.id_blkno = newblk; 796 idesc.id_numfrags = sblock.fs_frag; 797 (void)freeblock(&idesc); 798 } 799 if (indiralloced) { 800 idesc.id_blkno = indirblk; 801 idesc.id_numfrags = sblock.fs_frag; 802 (void)freeblock(&idesc); 803 } 804 return (0); 805 } 806 807 /* 808 * allocate a new directory 809 */ 810 ino_t 811 allocdir(ino_t parent, ino_t request, int mode) 812 { 813 ino_t ino; 814 char *cp; 815 struct inode ip; 816 union dinode *dp; 817 struct bufarea *bp; 818 struct inoinfo *inp; 819 struct dirtemplate *dirp; 820 821 ino = allocino(request, IFDIR|mode); 822 if (ino == 0) 823 return (0); 824 dirp = &dirhead; 825 dirp->dot_ino = ino; 826 dirp->dotdot_ino = parent; 827 ginode(ino, &ip); 828 dp = ip.i_dp; 829 bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize); 830 if (bp->b_errs) { 831 freeino(ino); 832 irelse(&ip); 833 return (0); 834 } 835 memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate)); 836 for (cp = &bp->b_un.b_buf[DIRBLKSIZ]; 837 cp < &bp->b_un.b_buf[sblock.fs_fsize]; 838 cp += DIRBLKSIZ) 839 memmove(cp, &emptydir, sizeof emptydir); 840 dirty(bp); 841 DIP_SET(dp, di_nlink, 2); 842 inodirty(&ip); 843 if (ino == UFS_ROOTINO) { 844 inp = cacheino(dp, ino); 845 inp->i_parent = parent; 846 inp->i_dotdot = parent; 847 inp->i_flags |= INFO_NEW; 848 inoinfo(ino)->ino_type = DT_DIR; 849 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink); 850 irelse(&ip); 851 return(ino); 852 } 853 if (!INO_IS_DVALID(parent)) { 854 freeino(ino); 855 irelse(&ip); 856 return (0); 857 } 858 inp = cacheino(dp, ino); 859 inp->i_parent = parent; 860 inp->i_dotdot = parent; 861 inp->i_flags |= INFO_NEW; 862 inoinfo(ino)->ino_type = DT_DIR; 863 inoinfo(ino)->ino_state = inoinfo(parent)->ino_state; 864 if (inoinfo(ino)->ino_state == DSTATE) { 865 inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink); 866 inoinfo(parent)->ino_linkcnt++; 867 } 868 irelse(&ip); 869 ginode(parent, &ip); 870 dp = ip.i_dp; 871 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1); 872 inodirty(&ip); 873 irelse(&ip); 874 return (ino); 875 } 876 877 /* 878 * free a directory inode 879 */ 880 void 881 freedirino(ino_t ino, ino_t parent) 882 { 883 struct inode ip; 884 union dinode *dp; 885 886 if (ino != parent) { 887 ginode(parent, &ip); 888 dp = ip.i_dp; 889 DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1); 890 inodirty(&ip); 891 irelse(&ip); 892 } 893 removecachedino(ino); 894 freeino(ino); 895 } 896 897 /* 898 * generate a temporary name for the lost+found directory. 899 */ 900 static int 901 lftempname(char *bufp, ino_t ino) 902 { 903 ino_t in; 904 char *cp; 905 int namlen; 906 907 cp = bufp + 2; 908 for (in = maxino; in > 0; in /= 10) 909 cp++; 910 *--cp = 0; 911 namlen = cp - bufp; 912 in = ino; 913 while (cp > bufp) { 914 *--cp = (in % 10) + '0'; 915 in /= 10; 916 } 917 *cp = '#'; 918 return (namlen); 919 } 920 921 /* 922 * Get a directory block. 923 * Insure that it is held until another is requested. 924 */ 925 static struct bufarea * 926 getdirblk(ufs2_daddr_t blkno, long size) 927 { 928 929 if (pdirbp != NULL && pdirbp->b_errs == 0) 930 brelse(pdirbp); 931 pdirbp = getdatablk(blkno, size, BT_DIRDATA); 932 return (pdirbp); 933 } 934