1 /* 2 * Copyright (c) 1980, 1986 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)pass2.c 5.20 (Berkeley) 07/02/92"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/time.h> 14 #include <ufs/ufs/dinode.h> 15 #include <ufs/ufs/dir.h> 16 #include <ufs/ffs/fs.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include "fsck.h" 20 21 #define MINDIRSIZE (sizeof (struct dirtemplate)) 22 23 int pass2check(), blksort(); 24 25 pass2() 26 { 27 register struct dinode *dp; 28 register struct inoinfo **inpp, *inp; 29 struct inoinfo **inpend; 30 struct inodesc curino; 31 struct dinode dino; 32 char pathbuf[MAXPATHLEN + 1]; 33 34 switch (statemap[ROOTINO]) { 35 36 case USTATE: 37 pfatal("ROOT INODE UNALLOCATED"); 38 if (reply("ALLOCATE") == 0) 39 errexit(""); 40 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) 41 errexit("CANNOT ALLOCATE ROOT INODE\n"); 42 break; 43 44 case DCLEAR: 45 pfatal("DUPS/BAD IN ROOT INODE"); 46 if (reply("REALLOCATE")) { 47 freeino(ROOTINO); 48 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) 49 errexit("CANNOT ALLOCATE ROOT INODE\n"); 50 break; 51 } 52 if (reply("CONTINUE") == 0) 53 errexit(""); 54 break; 55 56 case FSTATE: 57 case FCLEAR: 58 pfatal("ROOT INODE NOT DIRECTORY"); 59 if (reply("REALLOCATE")) { 60 freeino(ROOTINO); 61 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) 62 errexit("CANNOT ALLOCATE ROOT INODE\n"); 63 break; 64 } 65 if (reply("FIX") == 0) 66 errexit(""); 67 dp = ginode(ROOTINO); 68 dp->di_mode &= ~IFMT; 69 dp->di_mode |= IFDIR; 70 inodirty(); 71 break; 72 73 case DSTATE: 74 break; 75 76 default: 77 errexit("BAD STATE %d FOR ROOT INODE", statemap[ROOTINO]); 78 } 79 statemap[ROOTINO] = DFOUND; 80 /* 81 * Sort the directory list into disk block order. 82 */ 83 qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort); 84 /* 85 * Check the integrity of each directory. 86 */ 87 bzero((char *)&curino, sizeof(struct inodesc)); 88 curino.id_type = DATA; 89 curino.id_func = pass2check; 90 dino.di_mode = IFDIR; 91 dp = &dino; 92 inpend = &inpsort[inplast]; 93 for (inpp = inpsort; inpp < inpend; inpp++) { 94 inp = *inpp; 95 if (inp->i_isize == 0) 96 continue; 97 if (inp->i_isize < MINDIRSIZE) { 98 direrror(inp->i_number, "DIRECTORY TOO SHORT"); 99 inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ); 100 if (reply("FIX") == 1) { 101 dp = ginode(inp->i_number); 102 dp->di_size = inp->i_isize; 103 inodirty(); 104 dp = &dino; 105 } 106 } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) { 107 getpathname(pathbuf, inp->i_number, inp->i_number); 108 pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d", 109 pathbuf, inp->i_isize, DIRBLKSIZ); 110 if (preen) 111 printf(" (ADJUSTED)\n"); 112 inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ); 113 if (preen || reply("ADJUST") == 1) { 114 dp = ginode(inp->i_number); 115 dp->di_size = roundup(inp->i_isize, DIRBLKSIZ); 116 inodirty(); 117 dp = &dino; 118 } 119 } 120 dp->di_size = inp->i_isize; 121 bcopy((char *)&inp->i_blks[0], (char *)&dp->di_db[0], 122 (size_t)inp->i_numblks); 123 curino.id_number = inp->i_number; 124 curino.id_parent = inp->i_parent; 125 (void)ckinode(dp, &curino); 126 } 127 /* 128 * Now that the parents of all directories have been found, 129 * make another pass to verify the value of `..' 130 */ 131 for (inpp = inpsort; inpp < inpend; inpp++) { 132 inp = *inpp; 133 if (inp->i_parent == 0 || inp->i_isize == 0) 134 continue; 135 if (statemap[inp->i_parent] == DFOUND && 136 statemap[inp->i_number] == DSTATE) 137 statemap[inp->i_number] = DFOUND; 138 if (inp->i_dotdot == inp->i_parent || 139 inp->i_dotdot == (ino_t)-1) 140 continue; 141 if (inp->i_dotdot == 0) { 142 inp->i_dotdot = inp->i_parent; 143 fileerror(inp->i_parent, inp->i_number, "MISSING '..'"); 144 if (reply("FIX") == 0) 145 continue; 146 (void)makeentry(inp->i_number, inp->i_parent, ".."); 147 lncntp[inp->i_parent]--; 148 continue; 149 } 150 fileerror(inp->i_parent, inp->i_number, 151 "BAD INODE NUMBER FOR '..'"); 152 if (reply("FIX") == 0) 153 continue; 154 lncntp[inp->i_dotdot]++; 155 lncntp[inp->i_parent]--; 156 inp->i_dotdot = inp->i_parent; 157 (void)changeino(inp->i_number, "..", inp->i_parent); 158 } 159 /* 160 * Mark all the directories that can be found from the root. 161 */ 162 propagate(); 163 } 164 165 pass2check(idesc) 166 struct inodesc *idesc; 167 { 168 register struct direct *dirp = idesc->id_dirp; 169 register struct inoinfo *inp; 170 int n, entrysize, ret = 0; 171 struct dinode *dp; 172 char *errmsg; 173 struct direct proto; 174 char namebuf[MAXPATHLEN + 1]; 175 char pathbuf[MAXPATHLEN + 1]; 176 177 /* 178 * If converting, set directory entry type. 179 */ 180 if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) { 181 dirp->d_type = typemap[dirp->d_ino]; 182 ret |= ALTERED; 183 } 184 /* 185 * check for "." 186 */ 187 if (idesc->id_entryno != 0) 188 goto chk1; 189 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) { 190 if (dirp->d_ino != idesc->id_number) { 191 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'"); 192 dirp->d_ino = idesc->id_number; 193 if (reply("FIX") == 1) 194 ret |= ALTERED; 195 } 196 if (newinofmt && dirp->d_type != DT_DIR) { 197 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'"); 198 dirp->d_type = DT_DIR; 199 if (reply("FIX") == 1) 200 ret |= ALTERED; 201 } 202 goto chk1; 203 } 204 direrror(idesc->id_number, "MISSING '.'"); 205 proto.d_ino = idesc->id_number; 206 if (newinofmt) 207 proto.d_type = DT_DIR; 208 proto.d_namlen = 1; 209 (void)strcpy(proto.d_name, "."); 210 entrysize = DIRSIZ(0, &proto); 211 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) { 212 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n", 213 dirp->d_name); 214 } else if (dirp->d_reclen < entrysize) { 215 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n"); 216 } else if (dirp->d_reclen < 2 * entrysize) { 217 proto.d_reclen = dirp->d_reclen; 218 bcopy((char *)&proto, (char *)dirp, (size_t)entrysize); 219 if (reply("FIX") == 1) 220 ret |= ALTERED; 221 } else { 222 n = dirp->d_reclen - entrysize; 223 proto.d_reclen = entrysize; 224 bcopy((char *)&proto, (char *)dirp, (size_t)entrysize); 225 idesc->id_entryno++; 226 lncntp[dirp->d_ino]--; 227 dirp = (struct direct *)((char *)(dirp) + entrysize); 228 bzero((char *)dirp, (size_t)n); 229 dirp->d_reclen = n; 230 if (reply("FIX") == 1) 231 ret |= ALTERED; 232 } 233 chk1: 234 if (idesc->id_entryno > 1) 235 goto chk2; 236 inp = getinoinfo(idesc->id_number); 237 proto.d_ino = inp->i_parent; 238 if (newinofmt) 239 proto.d_type = DT_DIR; 240 proto.d_namlen = 2; 241 (void)strcpy(proto.d_name, ".."); 242 entrysize = DIRSIZ(0, &proto); 243 if (idesc->id_entryno == 0) { 244 n = DIRSIZ(0, dirp); 245 if (dirp->d_reclen < n + entrysize) 246 goto chk2; 247 proto.d_reclen = dirp->d_reclen - n; 248 dirp->d_reclen = n; 249 idesc->id_entryno++; 250 lncntp[dirp->d_ino]--; 251 dirp = (struct direct *)((char *)(dirp) + n); 252 bzero((char *)dirp, (size_t)proto.d_reclen); 253 dirp->d_reclen = proto.d_reclen; 254 } 255 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) { 256 inp->i_dotdot = dirp->d_ino; 257 if (newinofmt && dirp->d_type != DT_DIR) { 258 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'"); 259 dirp->d_type = DT_DIR; 260 if (reply("FIX") == 1) 261 ret |= ALTERED; 262 } 263 goto chk2; 264 } 265 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) { 266 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); 267 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n", 268 dirp->d_name); 269 inp->i_dotdot = (ino_t)-1; 270 } else if (dirp->d_reclen < entrysize) { 271 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); 272 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n"); 273 inp->i_dotdot = (ino_t)-1; 274 } else if (inp->i_parent != 0) { 275 /* 276 * We know the parent, so fix now. 277 */ 278 inp->i_dotdot = inp->i_parent; 279 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); 280 proto.d_reclen = dirp->d_reclen; 281 bcopy((char *)&proto, (char *)dirp, (size_t)entrysize); 282 if (reply("FIX") == 1) 283 ret |= ALTERED; 284 } 285 idesc->id_entryno++; 286 if (dirp->d_ino != 0) 287 lncntp[dirp->d_ino]--; 288 return (ret|KEEPON); 289 chk2: 290 if (dirp->d_ino == 0) 291 return (ret|KEEPON); 292 if (dirp->d_namlen <= 2 && 293 dirp->d_name[0] == '.' && 294 idesc->id_entryno >= 2) { 295 if (dirp->d_namlen == 1) { 296 direrror(idesc->id_number, "EXTRA '.' ENTRY"); 297 dirp->d_ino = 0; 298 if (reply("FIX") == 1) 299 ret |= ALTERED; 300 return (KEEPON | ret); 301 } 302 if (dirp->d_name[1] == '.') { 303 direrror(idesc->id_number, "EXTRA '..' ENTRY"); 304 dirp->d_ino = 0; 305 if (reply("FIX") == 1) 306 ret |= ALTERED; 307 return (KEEPON | ret); 308 } 309 } 310 idesc->id_entryno++; 311 n = 0; 312 if (dirp->d_ino > maxino) { 313 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE"); 314 n = reply("REMOVE"); 315 } else { 316 again: 317 switch (statemap[dirp->d_ino]) { 318 case USTATE: 319 if (idesc->id_entryno <= 2) 320 break; 321 fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED"); 322 n = reply("REMOVE"); 323 break; 324 325 case DCLEAR: 326 case FCLEAR: 327 if (idesc->id_entryno <= 2) 328 break; 329 if (statemap[dirp->d_ino] == DCLEAR) 330 errmsg = "ZERO LENGTH DIRECTORY"; 331 else 332 errmsg = "DUP/BAD"; 333 fileerror(idesc->id_number, dirp->d_ino, errmsg); 334 if ((n = reply("REMOVE")) == 1) 335 break; 336 dp = ginode(dirp->d_ino); 337 statemap[dirp->d_ino] = 338 (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE; 339 lncntp[dirp->d_ino] = dp->di_nlink; 340 goto again; 341 342 case DSTATE: 343 if (statemap[idesc->id_number] == DFOUND) 344 statemap[dirp->d_ino] = DFOUND; 345 /* fall through */ 346 347 case DFOUND: 348 inp = getinoinfo(dirp->d_ino); 349 if (inp->i_parent != 0 && idesc->id_entryno > 2) { 350 getpathname(pathbuf, idesc->id_number, 351 idesc->id_number); 352 getpathname(namebuf, dirp->d_ino, dirp->d_ino); 353 pwarn("%s %s %s\n", pathbuf, 354 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY", 355 namebuf); 356 if (preen) 357 printf(" (IGNORED)\n"); 358 else if ((n = reply("REMOVE")) == 1) 359 break; 360 } 361 if (idesc->id_entryno > 2) 362 inp->i_parent = idesc->id_number; 363 /* fall through */ 364 365 case FSTATE: 366 if (newinofmt && dirp->d_type != typemap[dirp->d_ino]) { 367 fileerror(idesc->id_number, dirp->d_ino, 368 "BAD TYPE VALUE"); 369 dirp->d_type = typemap[dirp->d_ino]; 370 if (reply("FIX") == 1) 371 ret |= ALTERED; 372 } 373 lncntp[dirp->d_ino]--; 374 break; 375 376 default: 377 errexit("BAD STATE %d FOR INODE I=%d", 378 statemap[dirp->d_ino], dirp->d_ino); 379 } 380 } 381 if (n == 0) 382 return (ret|KEEPON); 383 dirp->d_ino = 0; 384 return (ret|KEEPON|ALTERED); 385 } 386 387 /* 388 * Routine to sort disk blocks. 389 */ 390 blksort(inpp1, inpp2) 391 struct inoinfo **inpp1, **inpp2; 392 { 393 394 return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]); 395 } 396