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