pass2.c revision 1.34 1 /* $NetBSD: pass2.c,v 1.34 2003/04/09 12:49:28 fvdl Exp $ */
2
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pass2.c 8.9 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: pass2.c,v 1.34 2003/04/09 12:49:28 fvdl Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ffs/fs.h>
51
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include "fsck.h"
58 #include "fsutil.h"
59 #include "extern.h"
60
61 #define MINDIRSIZE (sizeof (struct dirtemplate))
62
63 static int blksort __P((const void *, const void *));
64 static int pass2check __P((struct inodesc *));
65
66 void
67 pass2()
68 {
69 union dinode *dp;
70 struct inoinfo **inpp, *inp, *pinp;
71 struct inoinfo **inpend;
72 struct inostat *rinfo, *info;
73 struct inodesc curino;
74 union dinode dino;
75 int i, maxblk;
76 char pathbuf[MAXPATHLEN + 1];
77
78 rinfo = inoinfo(ROOTINO);
79 switch (rinfo->ino_state) {
80
81 case USTATE:
82 pfatal("ROOT INODE UNALLOCATED");
83 if (reply("ALLOCATE") == 0) {
84 markclean = 0;
85 ckfini();
86 exit(EEXIT);
87 }
88 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
89 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
90 break;
91
92 case DCLEAR:
93 pfatal("DUPS/BAD IN ROOT INODE");
94 if (reply("REALLOCATE")) {
95 freeino(ROOTINO);
96 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
97 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
98 break;
99 }
100 markclean = 0;
101 if (reply("CONTINUE") == 0) {
102 ckfini();
103 exit(EEXIT);
104 }
105 break;
106
107 case FSTATE:
108 case FCLEAR:
109 pfatal("ROOT INODE NOT DIRECTORY");
110 if (reply("REALLOCATE")) {
111 freeino(ROOTINO);
112 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
113 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
114 break;
115 }
116 if (reply("FIX") == 0) {
117 markclean = 0;
118 ckfini();
119 exit(EEXIT);
120 }
121 dp = ginode(ROOTINO);
122 DIP(dp, mode) =
123 iswap16((iswap16(DIP(dp, mode)) & ~IFMT) | IFDIR);
124 inodirty();
125 break;
126
127 case DSTATE:
128 break;
129
130 default:
131 errx(EEXIT, "BAD STATE %d FOR ROOT INODE", rinfo->ino_state);
132 }
133 if (newinofmt) {
134 info = inoinfo(WINO);
135 info->ino_state = FSTATE;
136 info->ino_type = DT_WHT;
137 }
138 /*
139 * Sort the directory list into disk block order.
140 */
141 qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
142 /*
143 * Check the integrity of each directory.
144 */
145 memset(&curino, 0, sizeof(struct inodesc));
146 curino.id_type = DATA;
147 curino.id_func = pass2check;
148 inpend = &inpsort[inplast];
149 for (inpp = inpsort; inpp < inpend; inpp++) {
150 if (got_siginfo) {
151 fprintf(stderr,
152 "%s: phase 2: dir %ld of %d (%d%%)\n", cdevname(),
153 (long)(inpp - inpsort), (int)inplast,
154 (int)((inpp - inpsort) * 100 / inplast));
155 got_siginfo = 0;
156 }
157 inp = *inpp;
158 if (inp->i_isize == 0)
159 continue;
160 if (inp->i_isize < MINDIRSIZE) {
161 direrror(inp->i_number, "DIRECTORY TOO SHORT");
162 inp->i_isize = roundup(MINDIRSIZE, dirblksiz);
163 if (reply("FIX") == 1) {
164 dp = ginode(inp->i_number);
165 DIP(dp, size) = iswap64(inp->i_isize);
166 inodirty();
167 } else
168 markclean = 0;
169 } else if ((inp->i_isize & (dirblksiz - 1)) != 0) {
170 getpathname(pathbuf, inp->i_number, inp->i_number);
171 if (usedsoftdep)
172 pfatal("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
173 "DIRECTORY", pathbuf,
174 (long long)inp->i_isize, dirblksiz);
175 else
176 pwarn("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
177 "DIRECTORY", pathbuf,
178 (long long)inp->i_isize, dirblksiz);
179 if (preen)
180 printf(" (ADJUSTED)\n");
181 inp->i_isize = roundup(inp->i_isize, dirblksiz);
182 if (preen || reply("ADJUST") == 1) {
183 dp = ginode(inp->i_number);
184 DIP(dp, size) = iswap64(inp->i_isize);
185 inodirty();
186 } else
187 markclean = 0;
188 }
189 memset(&dino, 0, sizeof dino);
190 dp = &dino;
191 if (!is_ufs2) {
192 dp->dp1.di_mode = iswap16(IFDIR);
193 dp->dp1.di_size = iswap64(inp->i_isize);
194 maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
195 NDADDR;
196 for (i = 0; i < maxblk; i++)
197 dp->dp1.di_db[i] = inp->i_blks[i];
198 if (inp->i_numblks > NDADDR) {
199 for (i = 0; i < NIADDR; i++)
200 dp->dp1.di_ib[i] =
201 inp->i_blks[NDADDR + i];
202 }
203 } else {
204 dp->dp2.di_mode = iswap16(IFDIR);
205 dp->dp2.di_size = iswap64(inp->i_isize);
206 maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
207 NDADDR;
208 for (i = 0; i < maxblk; i++)
209 dp->dp2.di_db[i] = inp->i_blks[i];
210 if (inp->i_numblks > NDADDR) {
211 for (i = 0; i < NIADDR; i++)
212 dp->dp2.di_ib[i] =
213 inp->i_blks[NDADDR + i];
214 }
215 }
216 curino.id_number = inp->i_number;
217 curino.id_parent = inp->i_parent;
218 (void)ckinode(&dino, &curino);
219 }
220
221 /*
222 * Byte swapping in directory entries, if needed, has been done.
223 * Now rescan dirs for pass2check()
224 */
225 if (do_dirswap) {
226 do_dirswap = 0;
227 for (inpp = inpsort; inpp < inpend; inpp++) {
228 inp = *inpp;
229 if (inp->i_isize == 0)
230 continue;
231 memset(&dino, 0, sizeof dino);
232 if (!is_ufs2) {
233 dino.dp1.di_mode = iswap16(IFDIR);
234 dino.dp1.di_size = iswap64(inp->i_isize);
235 for (i = 0; i < inp->i_numblks; i++)
236 dino.dp1.di_db[i] = inp->i_blks[i];
237 } else {
238 dino.dp2.di_mode = iswap16(IFDIR);
239 dino.dp2.di_size = iswap64(inp->i_isize);
240 for (i = 0; i < inp->i_numblks; i++)
241 dino.dp2.di_db[i] = inp->i_blks[i];
242 }
243 curino.id_number = inp->i_number;
244 curino.id_parent = inp->i_parent;
245 (void)ckinode(&dino, &curino);
246 }
247 }
248
249 /*
250 * Now that the parents of all directories have been found,
251 * make another pass to verify the value of `..'
252 */
253 for (inpp = inpsort; inpp < inpend; inpp++) {
254 inp = *inpp;
255 if (inp->i_parent == 0 || inp->i_isize == 0)
256 continue;
257 if (inp->i_dotdot == inp->i_parent ||
258 inp->i_dotdot == (ino_t)-1)
259 continue;
260 info = inoinfo(inp->i_parent);
261 if (inp->i_dotdot == 0) {
262 inp->i_dotdot = inp->i_parent;
263 fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
264 if (reply("FIX") == 0) {
265 markclean = 0;
266 continue;
267 }
268 (void)makeentry(inp->i_number, inp->i_parent, "..");
269 info->ino_linkcnt--;
270 continue;
271 }
272 fileerror(inp->i_parent, inp->i_number,
273 "BAD INODE NUMBER FOR '..'");
274 if (reply("FIX") == 0) {
275 markclean = 0;
276 continue;
277 }
278 inoinfo(inp->i_dotdot)->ino_linkcnt++;
279 info->ino_linkcnt--;
280 inp->i_dotdot = inp->i_parent;
281 (void)changeino(inp->i_number, "..", inp->i_parent);
282 }
283 /*
284 * Create a list of children for each directory.
285 */
286 inpend = &inpsort[inplast];
287 for (inpp = inpsort; inpp < inpend; inpp++) {
288 inp = *inpp;
289 info = inoinfo(inp->i_number);
290 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
291 if (info->ino_state == DFOUND)
292 info->ino_state = DSTATE;
293 }
294 for (inpp = inpsort; inpp < inpend; inpp++) {
295 inp = *inpp;
296 if (inp->i_parent == 0 ||
297 inp->i_number == ROOTINO)
298 continue;
299 pinp = getinoinfo(inp->i_parent);
300 inp->i_parentp = pinp;
301 inp->i_sibling = pinp->i_child;
302 pinp->i_child = inp;
303 }
304 /*
305 * Mark all the directories that can be found from the root.
306 */
307 propagate(ROOTINO);
308 }
309
310 static int
311 pass2check(idesc)
312 struct inodesc *idesc;
313 {
314 struct direct *dirp = idesc->id_dirp;
315 struct inoinfo *inp;
316 struct inostat *info;
317 int n, entrysize, ret = 0;
318 union dinode *dp;
319 char *errmsg;
320 struct direct proto;
321 char namebuf[MAXPATHLEN + 1];
322 char pathbuf[MAXPATHLEN + 1];
323
324 /*
325 * If converting, set directory entry type.
326 */
327 if (!is_ufs2 && doinglevel2 && iswap32(dirp->d_ino) > 0 &&
328 iswap32(dirp->d_ino) < maxino) {
329 dirp->d_type = inoinfo(iswap32(dirp->d_ino))->ino_type;
330 ret |= ALTERED;
331 }
332 /*
333 * check for "."
334 */
335 if (idesc->id_entryno != 0)
336 goto chk1;
337 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
338 if (iswap32(dirp->d_ino) != idesc->id_number) {
339 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
340 dirp->d_ino = iswap32(idesc->id_number);
341 if (reply("FIX") == 1)
342 ret |= ALTERED;
343 else
344 markclean = 0;
345 }
346 if (newinofmt && dirp->d_type != DT_DIR) {
347 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
348 dirp->d_type = DT_DIR;
349 if (reply("FIX") == 1)
350 ret |= ALTERED;
351 else
352 markclean = 0;
353 }
354 goto chk1;
355 }
356 direrror(idesc->id_number, "MISSING '.'");
357 proto.d_ino = iswap32(idesc->id_number);
358 if (newinofmt)
359 proto.d_type = DT_DIR;
360 else
361 proto.d_type = 0;
362 proto.d_namlen = 1;
363 (void)strcpy(proto.d_name, ".");
364 # if BYTE_ORDER == LITTLE_ENDIAN
365 if (!newinofmt && !needswap) {
366 # else
367 if (!newinofmt && needswap) {
368 # endif
369 u_char tmp;
370
371 tmp = proto.d_type;
372 proto.d_type = proto.d_namlen;
373 proto.d_namlen = tmp;
374 }
375 entrysize = DIRSIZ(0, &proto, 0);
376 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
377 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
378 dirp->d_name);
379 markclean = 0;
380 } else if (iswap16(dirp->d_reclen) < entrysize) {
381 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
382 markclean = 0;
383 } else if (iswap16(dirp->d_reclen) < 2 * entrysize) {
384 proto.d_reclen = dirp->d_reclen;
385 memmove(dirp, &proto, (size_t)entrysize);
386 if (reply("FIX") == 1)
387 ret |= ALTERED;
388 else
389 markclean = 0;
390 } else {
391 n = iswap16(dirp->d_reclen) - entrysize;
392 proto.d_reclen = iswap16(entrysize);
393 memmove(dirp, &proto, (size_t)entrysize);
394 idesc->id_entryno++;
395 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
396 dirp = (struct direct *)((char *)(dirp) + entrysize);
397 memset(dirp, 0, (size_t)n);
398 dirp->d_reclen = iswap16(n);
399 if (reply("FIX") == 1)
400 ret |= ALTERED;
401 else
402 markclean = 0;
403 }
404 chk1:
405 if (idesc->id_entryno > 1)
406 goto chk2;
407 inp = getinoinfo(idesc->id_number);
408 proto.d_ino = iswap32(inp->i_parent);
409 if (newinofmt)
410 proto.d_type = DT_DIR;
411 else
412 proto.d_type = 0;
413 proto.d_namlen = 2;
414 (void)strcpy(proto.d_name, "..");
415 # if BYTE_ORDER == LITTLE_ENDIAN
416 if (!newinofmt && !needswap) {
417 # else
418 if (!newinofmt && needswap) {
419 # endif
420 u_char tmp;
421
422 tmp = proto.d_type;
423 proto.d_type = proto.d_namlen;
424 proto.d_namlen = tmp;
425 }
426 entrysize = DIRSIZ(0, &proto, 0);
427 if (idesc->id_entryno == 0) {
428 n = DIRSIZ(0, dirp, 0);
429 if (iswap16(dirp->d_reclen) < n + entrysize)
430 goto chk2;
431 proto.d_reclen = iswap16(iswap16(dirp->d_reclen) - n);
432 dirp->d_reclen = iswap16(n);
433 idesc->id_entryno++;
434 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
435 dirp = (struct direct *)((char *)(dirp) + n);
436 memset(dirp, 0, (size_t)iswap16(proto.d_reclen));
437 dirp->d_reclen = proto.d_reclen;
438 }
439 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
440 inp->i_dotdot = iswap32(dirp->d_ino);
441 if (newinofmt && dirp->d_type != DT_DIR) {
442 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
443 dirp->d_type = DT_DIR;
444 if (reply("FIX") == 1)
445 ret |= ALTERED;
446 else
447 markclean = 0;
448 }
449 goto chk2;
450 }
451 if (iswap32(dirp->d_ino) != 0 && strcmp(dirp->d_name, ".") != 0) {
452 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
453 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
454 dirp->d_name);
455 inp->i_dotdot = (ino_t)-1;
456 markclean = 0;
457 } else if (iswap16(dirp->d_reclen) < entrysize) {
458 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
459 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
460 inp->i_dotdot = (ino_t)-1;
461 markclean = 0;
462 } else if (inp->i_parent != 0) {
463 /*
464 * We know the parent, so fix now.
465 */
466 inp->i_dotdot = inp->i_parent;
467 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
468 proto.d_reclen = dirp->d_reclen;
469 memmove(dirp, &proto, (size_t)entrysize);
470 if (reply("FIX") == 1)
471 ret |= ALTERED;
472 else
473 markclean = 0;
474 }
475 idesc->id_entryno++;
476 if (dirp->d_ino != 0)
477 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
478 return (ret|KEEPON);
479 chk2:
480 if (dirp->d_ino == 0)
481 return (ret|KEEPON);
482 if (dirp->d_namlen <= 2 &&
483 dirp->d_name[0] == '.' &&
484 idesc->id_entryno >= 2) {
485 if (dirp->d_namlen == 1) {
486 direrror(idesc->id_number, "EXTRA '.' ENTRY");
487 dirp->d_ino = 0;
488 if (reply("FIX") == 1)
489 ret |= ALTERED;
490 else
491 markclean = 0;
492 return (KEEPON | ret);
493 }
494 if (dirp->d_name[1] == '.') {
495 direrror(idesc->id_number, "EXTRA '..' ENTRY");
496 dirp->d_ino = 0;
497 if (reply("FIX") == 1)
498 ret |= ALTERED;
499 else
500 markclean = 0;
501 return (KEEPON | ret);
502 }
503 }
504 idesc->id_entryno++;
505 n = 0;
506 if (iswap32(dirp->d_ino) > maxino) {
507 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
508 n = reply("REMOVE");
509 if (n == 0)
510 markclean = 0;
511 } else if (newinofmt &&
512 ((iswap32(dirp->d_ino) == WINO && dirp->d_type != DT_WHT) ||
513 (iswap32(dirp->d_ino) != WINO && dirp->d_type == DT_WHT))) {
514 fileerror(idesc->id_number, iswap32(dirp->d_ino), "BAD WHITEOUT ENTRY");
515 dirp->d_ino = iswap32(WINO);
516 dirp->d_type = DT_WHT;
517 if (reply("FIX") == 1)
518 ret |= ALTERED;
519 else
520 markclean = 0;
521 } else {
522 again:
523 info = inoinfo(iswap32(dirp->d_ino));
524 switch (info->ino_state) {
525 case USTATE:
526 if (idesc->id_entryno <= 2)
527 break;
528 fileerror(idesc->id_number, iswap32(dirp->d_ino), "UNALLOCATED");
529 n = reply("REMOVE");
530 if (n == 0)
531 markclean = 0;
532 break;
533
534 case DCLEAR:
535 case FCLEAR:
536 if (idesc->id_entryno <= 2)
537 break;
538 if (info->ino_state == FCLEAR)
539 errmsg = "DUP/BAD";
540 else if (!preen && !usedsoftdep)
541 errmsg = "ZERO LENGTH DIRECTORY";
542 else {
543 n = 1;
544 break;
545 }
546 fileerror(idesc->id_number, iswap32(dirp->d_ino), errmsg);
547 if ((n = reply("REMOVE")) == 1)
548 break;
549 dp = ginode(iswap32(dirp->d_ino));
550 info->ino_state =
551 (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? DSTATE : FSTATE;
552 info->ino_linkcnt = iswap16(DIP(dp, nlink));
553 goto again;
554
555 case DSTATE:
556 case DFOUND:
557 inp = getinoinfo(iswap32(dirp->d_ino));
558 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
559 getpathname(pathbuf, idesc->id_number,
560 idesc->id_number);
561 getpathname(namebuf, iswap32(dirp->d_ino),
562 iswap32(dirp->d_ino));
563 pwarn("%s %s %s\n", pathbuf,
564 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
565 namebuf);
566 if (preen)
567 printf(" (IGNORED)\n");
568 else if ((n = reply("REMOVE")) == 1)
569 break;
570 }
571 if (idesc->id_entryno > 2)
572 inp->i_parent = idesc->id_number;
573 /* fall through */
574
575 case FSTATE:
576 if (newinofmt && dirp->d_type != info->ino_type) {
577 fileerror(idesc->id_number, iswap32(dirp->d_ino),
578 "BAD TYPE VALUE");
579 dirp->d_type = info->ino_type;
580 if (reply("FIX") == 1)
581 ret |= ALTERED;
582 else
583 markclean = 0;
584 }
585 info->ino_linkcnt--;
586 break;
587
588 default:
589 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
590 info->ino_state, iswap32(dirp->d_ino));
591 }
592 }
593 if (n == 0)
594 return (ret|KEEPON);
595 dirp->d_ino = 0;
596 return (ret|KEEPON|ALTERED);
597 }
598
599 /*
600 * Routine to sort disk blocks.
601 */
602 static int
603 blksort(arg1, arg2)
604 const void *arg1, *arg2;
605 {
606
607 return ((*(struct inoinfo **)arg1)->i_blks[0] -
608 (*(struct inoinfo **)arg2)->i_blks[0]);
609 }
610