pass2.c revision 92839
16527Sache/* 26527Sache * Copyright (c) 1980, 1986, 1993 36527Sache * The Regents of the University of California. All rights reserved. 46527Sache * 56527Sache * Redistribution and use in source and binary forms, with or without 66527Sache * modification, are permitted provided that the following conditions 76527Sache * are met: 86527Sache * 1. Redistributions of source code must retain the above copyright 96527Sache * notice, this list of conditions and the following disclaimer. 106527Sache * 2. Redistributions in binary form must reproduce the above copyright 116527Sache * notice, this list of conditions and the following disclaimer in the 126527Sache * documentation and/or other materials provided with the distribution. 136527Sache * 3. All advertising materials mentioning features or use of this software 146527Sache * must display the following acknowledgement: 156527Sache * This product includes software developed by the University of 166527Sache * California, Berkeley and its contributors. 176527Sache * 4. Neither the name of the University nor the names of its contributors 186527Sache * may be used to endorse or promote products derived from this software 196527Sache * without specific prior written permission. 206527Sache * 216527Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 226527Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 236527Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 246527Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 256527Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2650477Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2726959Scharnier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2869860Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 296527Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 306527Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 316527Sache * SUCH DAMAGE. 326527Sache */ 336527Sache 346527Sache#ifndef lint 3568963Sru#if 0 3618950Sachestatic const char sccsid[] = "@(#)pass2.c 8.9 (Berkeley) 4/28/95"; 3718950Sache#endif 3818950Sachestatic const char rcsid[] = 396527Sache "$FreeBSD: head/sbin/fsck_ffs/pass2.c 92839 2002-03-20 22:57:10Z imp $"; 4095124Scharnier#endif /* not lint */ 4195124Scharnier 4295124Scharnier#include <sys/param.h> 436527Sache 446527Sache#include <ufs/ufs/dinode.h> 456527Sache#include <ufs/ufs/dir.h> 466527Sache#include <ufs/ffs/fs.h> 4757670Ssheldonh 4857670Ssheldonh#include <err.h> 496527Sache#include <string.h> 50119848Scharnier 516527Sache#include "fsck.h" 52119848Scharnier 536527Sache#define MINDIRSIZE (sizeof (struct dirtemplate)) 5457670Ssheldonh 5557670Ssheldonhstatic int blksort(const void *, const void *); 566527Sachestatic int pass2check(struct inodesc *); 576527Sache 586527Sachevoid 596527Sachepass2(void) 606527Sache{ 61119848Scharnier struct dinode *dp; 626527Sache struct inoinfo **inpp, *inp; 63119848Scharnier struct inoinfo **inpend; 646527Sache struct inodesc curino; 656527Sache struct dinode dino; 6695124Scharnier char pathbuf[MAXPATHLEN + 1]; 6795124Scharnier 6895124Scharnier switch (inoinfo(ROOTINO)->ino_state) { 696527Sache 706527Sache case USTATE: 716527Sache pfatal("ROOT INODE UNALLOCATED"); 726527Sache if (reply("ALLOCATE") == 0) { 736527Sache ckfini(0); 74119848Scharnier exit(EEXIT); 75119848Scharnier } 7673233Sru if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) 77119848Scharnier errx(EEXIT, "CANNOT ALLOCATE ROOT INODE"); 7818950Sache break; 7918950Sache 8073233Sru case DCLEAR: 81119848Scharnier pfatal("DUPS/BAD IN ROOT INODE"); 8218950Sache if (reply("REALLOCATE")) { 8318950Sache freeino(ROOTINO); 8418950Sache if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) 8518950Sache errx(EEXIT, "CANNOT ALLOCATE ROOT INODE"); 866527Sache break; 876527Sache } 886527Sache if (reply("CONTINUE") == 0) { 896527Sache ckfini(0); 906527Sache exit(EEXIT); 9118950Sache } 9218950Sache break; 9318950Sache 9418950Sache case FSTATE: 9518950Sache case FCLEAR: 9618950Sache pfatal("ROOT INODE NOT DIRECTORY"); 9718950Sache if (reply("REALLOCATE")) { 9857670Ssheldonh freeino(ROOTINO); 9957670Ssheldonh if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) 1006527Sache errx(EEXIT, "CANNOT ALLOCATE ROOT INODE"); 10118950Sache break; 1026527Sache } 10318950Sache if (reply("FIX") == 0) { 10457670Ssheldonh ckfini(0); 10557670Ssheldonh exit(EEXIT); 1066527Sache } 1076527Sache dp = ginode(ROOTINO); 10818950Sache dp->di_mode &= ~IFMT; 109129426Sru dp->di_mode |= IFDIR; 11018950Sache inodirty(); 11157670Ssheldonh break; 11257670Ssheldonh 1136527Sache case DSTATE: 1146527Sache break; 115129426Sru 11618950Sache default: 117119848Scharnier errx(EEXIT, "BAD STATE %d FOR ROOT INODE", 1186527Sache inoinfo(ROOTINO)->ino_state); 1196527Sache } 1206527Sache inoinfo(ROOTINO)->ino_state = DFOUND; 1216527Sache if (newinofmt) { 1226527Sache inoinfo(WINO)->ino_state = FSTATE; 1236527Sache inoinfo(WINO)->ino_type = DT_WHT; 12457670Ssheldonh } 12557670Ssheldonh /* 1266527Sache * Sort the directory list into disk block order. 127129426Sru */ 128129426Sru qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort); 12918950Sache /* 1306527Sache * Check the integrity of each directory. 131119848Scharnier */ 132119848Scharnier memset(&curino, 0, sizeof(struct inodesc)); 133119848Scharnier curino.id_type = DATA; 134119848Scharnier curino.id_func = pass2check; 13568575Sru dp = &dino; 1366527Sache inpend = &inpsort[inplast]; 137119848Scharnier for (inpp = inpsort; inpp < inpend; inpp++) { 138119848Scharnier if (got_siginfo) { 139119848Scharnier printf("%s: phase 2: dir %d of %d (%d%%)\n", cdevname, 140119848Scharnier inpp - inpsort, (int)inplast, 14118950Sache (int)((inpp - inpsort) * 100 / inplast)); 14218950Sache got_siginfo = 0; 14318950Sache } 144119848Scharnier inp = *inpp; 145119848Scharnier if (inp->i_isize == 0) 146119848Scharnier continue; 14718950Sache if (inp->i_isize < MINDIRSIZE) { 1486527Sache direrror(inp->i_number, "DIRECTORY TOO SHORT"); 1496527Sache inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ); 1506527Sache if (reply("FIX") == 1) { 15143967Sache dp = ginode(inp->i_number); 1526527Sache dp->di_size = inp->i_isize; 15343967Sache inodirty(); 15443967Sache dp = &dino; 15543967Sache } 15643967Sache } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) { 15718950Sache getpathname(pathbuf, inp->i_number, inp->i_number); 15818950Sache if (usedsoftdep) 15918950Sache pfatal("%s %s: LENGTH %d NOT MULTIPLE OF %d", 1606527Sache "DIRECTORY", pathbuf, inp->i_isize, 161129426Sru DIRBLKSIZ); 16218950Sache else 163119848Scharnier pwarn("%s %s: LENGTH %d NOT MULTIPLE OF %d", 16418955Sache "DIRECTORY", pathbuf, inp->i_isize, 16557670Ssheldonh DIRBLKSIZ); 16657670Ssheldonh if (preen) 16718950Sache printf(" (ADJUSTED)\n"); 1686527Sache inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ); 16918950Sache if (preen || reply("ADJUST") == 1) { 1706527Sache dp = ginode(inp->i_number); 1716527Sache dp->di_size = roundup(inp->i_isize, DIRBLKSIZ); 1726527Sache inodirty(); 17318955Sache dp = &dino; 1746527Sache } 1756527Sache } 17618950Sache memset(&dino, 0, sizeof(struct dinode)); 17718950Sache dino.di_mode = IFDIR; 17818950Sache dp->di_size = inp->i_isize; 1796527Sache memmove(&dp->di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks); 1806527Sache curino.id_number = inp->i_number; 181119848Scharnier curino.id_parent = inp->i_parent; 18218950Sache (void)ckinode(dp, &curino); 183102299Sache } 1846527Sache /* 1856527Sache * Now that the parents of all directories have been found, 186119848Scharnier * make another pass to verify the value of `..' 18718950Sache */ 188102299Sache for (inpp = inpsort; inpp < inpend; inpp++) { 1896527Sache inp = *inpp; 1906527Sache if (inp->i_parent == 0 || inp->i_isize == 0) 191119848Scharnier continue; 19218950Sache if (inoinfo(inp->i_parent)->ino_state == DFOUND && 19318950Sache inoinfo(inp->i_number)->ino_state == DSTATE) 19418950Sache inoinfo(inp->i_number)->ino_state = DFOUND; 19518950Sache if (inp->i_dotdot == inp->i_parent || 196102299Sache inp->i_dotdot == (ino_t)-1) 1976527Sache continue; 198102299Sache if (inp->i_dotdot == 0) { 1996527Sache inp->i_dotdot = inp->i_parent; 20018950Sache fileerror(inp->i_parent, inp->i_number, "MISSING '..'"); 20118950Sache if (reply("FIX") == 0) 20218950Sache continue; 20318950Sache (void)makeentry(inp->i_number, inp->i_parent, ".."); 20418950Sache inoinfo(inp->i_parent)->ino_linkcnt--; 20518950Sache continue; 20618950Sache } 20718950Sache fileerror(inp->i_parent, inp->i_number, 20818950Sache "BAD INODE NUMBER FOR '..'"); 20918950Sache if (reply("FIX") == 0) 21018950Sache continue; 21118950Sache inoinfo(inp->i_dotdot)->ino_linkcnt++; 21218950Sache inoinfo(inp->i_parent)->ino_linkcnt--; 21318950Sache inp->i_dotdot = inp->i_parent; 21418950Sache (void)changeino(inp->i_number, "..", inp->i_parent); 21518950Sache } 21618950Sache /* 217119848Scharnier * Mark all the directories that can be found from the root. 21818955Sache */ 219102299Sache propagate(); 220102299Sache} 221102313Sache 222102313Sachestatic int 223102299Sachepass2check(struct inodesc *idesc) 22418955Sache{ 225119848Scharnier struct direct *dirp = idesc->id_dirp; 22618955Sache struct inoinfo *inp; 22718955Sache int n, entrysize, ret = 0; 22818955Sache struct dinode *dp; 22918955Sache char *errmsg; 23018955Sache struct direct proto; 23118955Sache char namebuf[MAXPATHLEN + 1]; 23218955Sache char pathbuf[MAXPATHLEN + 1]; 23318955Sache 23418955Sache /* 23518955Sache * If converting, set directory entry type. 23618955Sache */ 23718955Sache if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) { 23818955Sache dirp->d_type = inoinfo(dirp->d_ino)->ino_type; 23918955Sache ret |= ALTERED; 24018955Sache } 24118955Sache /* 24218955Sache * check for "." 24318955Sache */ 2446527Sache if (idesc->id_entryno != 0) 2456527Sache goto chk1; 24618950Sache if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) { 2476527Sache if (dirp->d_ino != idesc->id_number) { 24857670Ssheldonh direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'"); 24957670Ssheldonh dirp->d_ino = idesc->id_number; 25018955Sache if (reply("FIX") == 1) 251140420Sru ret |= ALTERED; 252140420Sru } 253140420Sru if (newinofmt && dirp->d_type != DT_DIR) { 254140420Sru direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'"); 255140420Sru dirp->d_type = DT_DIR; 256140420Sru if (reply("FIX") == 1) 257140420Sru ret |= ALTERED; 258140368Sru } 25995124Scharnier goto chk1; 26095124Scharnier } 26195124Scharnier direrror(idesc->id_number, "MISSING '.'"); 26268854Sru proto.d_ino = idesc->id_number; 26368854Sru if (newinofmt) 26468854Sru proto.d_type = DT_DIR; 26568854Sru else 2666527Sache proto.d_type = 0; 26768854Sru proto.d_namlen = 1; 2686527Sache (void)strcpy(proto.d_name, "."); 2696527Sache# if BYTE_ORDER == LITTLE_ENDIAN 27021550Smpp if (!newinofmt) { 2716527Sache u_char tmp; 2726527Sache 273 tmp = proto.d_type; 274 proto.d_type = proto.d_namlen; 275 proto.d_namlen = tmp; 276 } 277# endif 278 entrysize = DIRSIZ(0, &proto); 279 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) { 280 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n", 281 dirp->d_name); 282 } else if (dirp->d_reclen < entrysize) { 283 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n"); 284 } else if (dirp->d_reclen < 2 * entrysize) { 285 proto.d_reclen = dirp->d_reclen; 286 memmove(dirp, &proto, (size_t)entrysize); 287 if (reply("FIX") == 1) 288 ret |= ALTERED; 289 } else { 290 n = dirp->d_reclen - entrysize; 291 proto.d_reclen = entrysize; 292 memmove(dirp, &proto, (size_t)entrysize); 293 idesc->id_entryno++; 294 inoinfo(dirp->d_ino)->ino_linkcnt--; 295 dirp = (struct direct *)((char *)(dirp) + entrysize); 296 memset(dirp, 0, (size_t)n); 297 dirp->d_reclen = n; 298 if (reply("FIX") == 1) 299 ret |= ALTERED; 300 } 301chk1: 302 if (idesc->id_entryno > 1) 303 goto chk2; 304 inp = getinoinfo(idesc->id_number); 305 proto.d_ino = inp->i_parent; 306 if (newinofmt) 307 proto.d_type = DT_DIR; 308 else 309 proto.d_type = 0; 310 proto.d_namlen = 2; 311 (void)strcpy(proto.d_name, ".."); 312# if BYTE_ORDER == LITTLE_ENDIAN 313 if (!newinofmt) { 314 u_char tmp; 315 316 tmp = proto.d_type; 317 proto.d_type = proto.d_namlen; 318 proto.d_namlen = tmp; 319 } 320# endif 321 entrysize = DIRSIZ(0, &proto); 322 if (idesc->id_entryno == 0) { 323 n = DIRSIZ(0, dirp); 324 if (dirp->d_reclen < n + entrysize) 325 goto chk2; 326 proto.d_reclen = dirp->d_reclen - n; 327 dirp->d_reclen = n; 328 idesc->id_entryno++; 329 inoinfo(dirp->d_ino)->ino_linkcnt--; 330 dirp = (struct direct *)((char *)(dirp) + n); 331 memset(dirp, 0, (size_t)proto.d_reclen); 332 dirp->d_reclen = proto.d_reclen; 333 } 334 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) { 335 inp->i_dotdot = dirp->d_ino; 336 if (newinofmt && dirp->d_type != DT_DIR) { 337 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'"); 338 dirp->d_type = DT_DIR; 339 if (reply("FIX") == 1) 340 ret |= ALTERED; 341 } 342 goto chk2; 343 } 344 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) { 345 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); 346 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n", 347 dirp->d_name); 348 inp->i_dotdot = (ino_t)-1; 349 } else if (dirp->d_reclen < entrysize) { 350 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); 351 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n"); 352 inp->i_dotdot = (ino_t)-1; 353 } else if (inp->i_parent != 0) { 354 /* 355 * We know the parent, so fix now. 356 */ 357 inp->i_dotdot = inp->i_parent; 358 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); 359 proto.d_reclen = dirp->d_reclen; 360 memmove(dirp, &proto, (size_t)entrysize); 361 if (reply("FIX") == 1) 362 ret |= ALTERED; 363 } 364 idesc->id_entryno++; 365 if (dirp->d_ino != 0) 366 inoinfo(dirp->d_ino)->ino_linkcnt--; 367 return (ret|KEEPON); 368chk2: 369 if (dirp->d_ino == 0) 370 return (ret|KEEPON); 371 if (dirp->d_namlen <= 2 && 372 dirp->d_name[0] == '.' && 373 idesc->id_entryno >= 2) { 374 if (dirp->d_namlen == 1) { 375 direrror(idesc->id_number, "EXTRA '.' ENTRY"); 376 dirp->d_ino = 0; 377 if (reply("FIX") == 1) 378 ret |= ALTERED; 379 return (KEEPON | ret); 380 } 381 if (dirp->d_name[1] == '.') { 382 direrror(idesc->id_number, "EXTRA '..' ENTRY"); 383 dirp->d_ino = 0; 384 if (reply("FIX") == 1) 385 ret |= ALTERED; 386 return (KEEPON | ret); 387 } 388 } 389 idesc->id_entryno++; 390 n = 0; 391 if (dirp->d_ino > maxino) { 392 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE"); 393 n = reply("REMOVE"); 394 } else if (newinofmt && 395 ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) || 396 (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) { 397 fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY"); 398 dirp->d_ino = WINO; 399 dirp->d_type = DT_WHT; 400 if (reply("FIX") == 1) 401 ret |= ALTERED; 402 } else { 403again: 404 switch (inoinfo(dirp->d_ino)->ino_state) { 405 case USTATE: 406 if (idesc->id_entryno <= 2) 407 break; 408 fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED"); 409 n = reply("REMOVE"); 410 break; 411 412 case DCLEAR: 413 case FCLEAR: 414 if (idesc->id_entryno <= 2) 415 break; 416 if (inoinfo(dirp->d_ino)->ino_state == FCLEAR) 417 errmsg = "DUP/BAD"; 418 else if (!preen && !usedsoftdep) 419 errmsg = "ZERO LENGTH DIRECTORY"; 420 else { 421 n = 1; 422 break; 423 } 424 fileerror(idesc->id_number, dirp->d_ino, errmsg); 425 if ((n = reply("REMOVE")) == 1) 426 break; 427 dp = ginode(dirp->d_ino); 428 inoinfo(dirp->d_ino)->ino_state = 429 (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE; 430 inoinfo(dirp->d_ino)->ino_linkcnt = dp->di_nlink; 431 goto again; 432 433 case DSTATE: 434 if (inoinfo(idesc->id_number)->ino_state == DFOUND) 435 inoinfo(dirp->d_ino)->ino_state = DFOUND; 436 /* fall through */ 437 438 case DFOUND: 439 inp = getinoinfo(dirp->d_ino); 440 if (inp->i_parent != 0 && idesc->id_entryno > 2) { 441 getpathname(pathbuf, idesc->id_number, 442 idesc->id_number); 443 getpathname(namebuf, dirp->d_ino, dirp->d_ino); 444 pwarn("%s%s%s %s %s\n", pathbuf, 445 (strcmp(pathbuf, "/") == 0 ? "" : "/"), 446 dirp->d_name, 447 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY", 448 namebuf); 449 if (cursnapshot != 0) 450 break; 451 if (preen) { 452 printf(" (REMOVED)\n"); 453 n = 1; 454 break; 455 } 456 if ((n = reply("REMOVE")) == 1) 457 break; 458 } 459 if (idesc->id_entryno > 2) 460 inp->i_parent = idesc->id_number; 461 /* fall through */ 462 463 case FSTATE: 464 if (newinofmt && 465 dirp->d_type != inoinfo(dirp->d_ino)->ino_type) { 466 fileerror(idesc->id_number, dirp->d_ino, 467 "BAD TYPE VALUE"); 468 dirp->d_type = inoinfo(dirp->d_ino)->ino_type; 469 if (reply("FIX") == 1) 470 ret |= ALTERED; 471 } 472 inoinfo(dirp->d_ino)->ino_linkcnt--; 473 break; 474 475 default: 476 errx(EEXIT, "BAD STATE %d FOR INODE I=%d", 477 inoinfo(dirp->d_ino)->ino_state, dirp->d_ino); 478 } 479 } 480 if (n == 0) 481 return (ret|KEEPON); 482 dirp->d_ino = 0; 483 return (ret|KEEPON|ALTERED); 484} 485 486/* 487 * Routine to sort disk blocks. 488 */ 489static int 490blksort(const void *arg1, const void *arg2) 491{ 492 493 return ((*(struct inoinfo **)arg1)->i_blks[0] - 494 (*(struct inoinfo **)arg2)->i_blks[0]); 495} 496