pass2.c revision 1.35 1 /* $NetBSD: pass2.c,v 1.35 2003/07/13 08:16:15 itojun 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.35 2003/07/13 08:16:15 itojun 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, sizeof(pathbuf), inp->i_number,
171 inp->i_number);
172 if (usedsoftdep)
173 pfatal("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
174 "DIRECTORY", pathbuf,
175 (long long)inp->i_isize, dirblksiz);
176 else
177 pwarn("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
178 "DIRECTORY", pathbuf,
179 (long long)inp->i_isize, dirblksiz);
180 if (preen)
181 printf(" (ADJUSTED)\n");
182 inp->i_isize = roundup(inp->i_isize, dirblksiz);
183 if (preen || reply("ADJUST") == 1) {
184 dp = ginode(inp->i_number);
185 DIP(dp, size) = iswap64(inp->i_isize);
186 inodirty();
187 } else
188 markclean = 0;
189 }
190 memset(&dino, 0, sizeof dino);
191 dp = &dino;
192 if (!is_ufs2) {
193 dp->dp1.di_mode = iswap16(IFDIR);
194 dp->dp1.di_size = iswap64(inp->i_isize);
195 maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
196 NDADDR;
197 for (i = 0; i < maxblk; i++)
198 dp->dp1.di_db[i] = inp->i_blks[i];
199 if (inp->i_numblks > NDADDR) {
200 for (i = 0; i < NIADDR; i++)
201 dp->dp1.di_ib[i] =
202 inp->i_blks[NDADDR + i];
203 }
204 } else {
205 dp->dp2.di_mode = iswap16(IFDIR);
206 dp->dp2.di_size = iswap64(inp->i_isize);
207 maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
208 NDADDR;
209 for (i = 0; i < maxblk; i++)
210 dp->dp2.di_db[i] = inp->i_blks[i];
211 if (inp->i_numblks > NDADDR) {
212 for (i = 0; i < NIADDR; i++)
213 dp->dp2.di_ib[i] =
214 inp->i_blks[NDADDR + i];
215 }
216 }
217 curino.id_number = inp->i_number;
218 curino.id_parent = inp->i_parent;
219 (void)ckinode(&dino, &curino);
220 }
221
222 /*
223 * Byte swapping in directory entries, if needed, has been done.
224 * Now rescan dirs for pass2check()
225 */
226 if (do_dirswap) {
227 do_dirswap = 0;
228 for (inpp = inpsort; inpp < inpend; inpp++) {
229 inp = *inpp;
230 if (inp->i_isize == 0)
231 continue;
232 memset(&dino, 0, sizeof dino);
233 if (!is_ufs2) {
234 dino.dp1.di_mode = iswap16(IFDIR);
235 dino.dp1.di_size = iswap64(inp->i_isize);
236 for (i = 0; i < inp->i_numblks; i++)
237 dino.dp1.di_db[i] = inp->i_blks[i];
238 } else {
239 dino.dp2.di_mode = iswap16(IFDIR);
240 dino.dp2.di_size = iswap64(inp->i_isize);
241 for (i = 0; i < inp->i_numblks; i++)
242 dino.dp2.di_db[i] = inp->i_blks[i];
243 }
244 curino.id_number = inp->i_number;
245 curino.id_parent = inp->i_parent;
246 (void)ckinode(&dino, &curino);
247 }
248 }
249
250 /*
251 * Now that the parents of all directories have been found,
252 * make another pass to verify the value of `..'
253 */
254 for (inpp = inpsort; inpp < inpend; inpp++) {
255 inp = *inpp;
256 if (inp->i_parent == 0 || inp->i_isize == 0)
257 continue;
258 if (inp->i_dotdot == inp->i_parent ||
259 inp->i_dotdot == (ino_t)-1)
260 continue;
261 info = inoinfo(inp->i_parent);
262 if (inp->i_dotdot == 0) {
263 inp->i_dotdot = inp->i_parent;
264 fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
265 if (reply("FIX") == 0) {
266 markclean = 0;
267 continue;
268 }
269 (void)makeentry(inp->i_number, inp->i_parent, "..");
270 info->ino_linkcnt--;
271 continue;
272 }
273 fileerror(inp->i_parent, inp->i_number,
274 "BAD INODE NUMBER FOR '..'");
275 if (reply("FIX") == 0) {
276 markclean = 0;
277 continue;
278 }
279 inoinfo(inp->i_dotdot)->ino_linkcnt++;
280 info->ino_linkcnt--;
281 inp->i_dotdot = inp->i_parent;
282 (void)changeino(inp->i_number, "..", inp->i_parent);
283 }
284 /*
285 * Create a list of children for each directory.
286 */
287 inpend = &inpsort[inplast];
288 for (inpp = inpsort; inpp < inpend; inpp++) {
289 inp = *inpp;
290 info = inoinfo(inp->i_number);
291 inp->i_child = inp->i_sibling = inp->i_parentp = 0;
292 if (info->ino_state == DFOUND)
293 info->ino_state = DSTATE;
294 }
295 for (inpp = inpsort; inpp < inpend; inpp++) {
296 inp = *inpp;
297 if (inp->i_parent == 0 ||
298 inp->i_number == ROOTINO)
299 continue;
300 pinp = getinoinfo(inp->i_parent);
301 inp->i_parentp = pinp;
302 inp->i_sibling = pinp->i_child;
303 pinp->i_child = inp;
304 }
305 /*
306 * Mark all the directories that can be found from the root.
307 */
308 propagate(ROOTINO);
309 }
310
311 static int
312 pass2check(idesc)
313 struct inodesc *idesc;
314 {
315 struct direct *dirp = idesc->id_dirp;
316 struct inoinfo *inp;
317 struct inostat *info;
318 int n, entrysize, ret = 0;
319 union dinode *dp;
320 char *errmsg;
321 struct direct proto;
322 char namebuf[MAXPATHLEN + 1];
323 char pathbuf[MAXPATHLEN + 1];
324
325 /*
326 * If converting, set directory entry type.
327 */
328 if (!is_ufs2 && doinglevel2 && iswap32(dirp->d_ino) > 0 &&
329 iswap32(dirp->d_ino) < maxino) {
330 dirp->d_type = inoinfo(iswap32(dirp->d_ino))->ino_type;
331 ret |= ALTERED;
332 }
333 /*
334 * check for "."
335 */
336 if (idesc->id_entryno != 0)
337 goto chk1;
338 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
339 if (iswap32(dirp->d_ino) != idesc->id_number) {
340 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
341 dirp->d_ino = iswap32(idesc->id_number);
342 if (reply("FIX") == 1)
343 ret |= ALTERED;
344 else
345 markclean = 0;
346 }
347 if (newinofmt && dirp->d_type != DT_DIR) {
348 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
349 dirp->d_type = DT_DIR;
350 if (reply("FIX") == 1)
351 ret |= ALTERED;
352 else
353 markclean = 0;
354 }
355 goto chk1;
356 }
357 direrror(idesc->id_number, "MISSING '.'");
358 proto.d_ino = iswap32(idesc->id_number);
359 if (newinofmt)
360 proto.d_type = DT_DIR;
361 else
362 proto.d_type = 0;
363 proto.d_namlen = 1;
364 (void)strlcpy(proto.d_name, ".", sizeof(proto.d_name));
365 # if BYTE_ORDER == LITTLE_ENDIAN
366 if (!newinofmt && !needswap) {
367 # else
368 if (!newinofmt && needswap) {
369 # endif
370 u_char tmp;
371
372 tmp = proto.d_type;
373 proto.d_type = proto.d_namlen;
374 proto.d_namlen = tmp;
375 }
376 entrysize = DIRSIZ(0, &proto, 0);
377 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
378 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
379 dirp->d_name);
380 markclean = 0;
381 } else if (iswap16(dirp->d_reclen) < entrysize) {
382 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
383 markclean = 0;
384 } else if (iswap16(dirp->d_reclen) < 2 * entrysize) {
385 proto.d_reclen = dirp->d_reclen;
386 memmove(dirp, &proto, (size_t)entrysize);
387 if (reply("FIX") == 1)
388 ret |= ALTERED;
389 else
390 markclean = 0;
391 } else {
392 n = iswap16(dirp->d_reclen) - entrysize;
393 proto.d_reclen = iswap16(entrysize);
394 memmove(dirp, &proto, (size_t)entrysize);
395 idesc->id_entryno++;
396 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
397 dirp = (struct direct *)((char *)(dirp) + entrysize);
398 memset(dirp, 0, (size_t)n);
399 dirp->d_reclen = iswap16(n);
400 if (reply("FIX") == 1)
401 ret |= ALTERED;
402 else
403 markclean = 0;
404 }
405 chk1:
406 if (idesc->id_entryno > 1)
407 goto chk2;
408 inp = getinoinfo(idesc->id_number);
409 proto.d_ino = iswap32(inp->i_parent);
410 if (newinofmt)
411 proto.d_type = DT_DIR;
412 else
413 proto.d_type = 0;
414 proto.d_namlen = 2;
415 (void)strlcpy(proto.d_name, "..", sizeof(proto.d_name));
416 #if BYTE_ORDER == LITTLE_ENDIAN
417 if (!newinofmt && !needswap) {
418 #else
419 if (!newinofmt && needswap) {
420 #endif
421 u_char tmp;
422
423 tmp = proto.d_type;
424 proto.d_type = proto.d_namlen;
425 proto.d_namlen = tmp;
426 }
427 entrysize = DIRSIZ(0, &proto, 0);
428 if (idesc->id_entryno == 0) {
429 n = DIRSIZ(0, dirp, 0);
430 if (iswap16(dirp->d_reclen) < n + entrysize)
431 goto chk2;
432 proto.d_reclen = iswap16(iswap16(dirp->d_reclen) - n);
433 dirp->d_reclen = iswap16(n);
434 idesc->id_entryno++;
435 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
436 dirp = (struct direct *)((char *)(dirp) + n);
437 memset(dirp, 0, (size_t)iswap16(proto.d_reclen));
438 dirp->d_reclen = proto.d_reclen;
439 }
440 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
441 inp->i_dotdot = iswap32(dirp->d_ino);
442 if (newinofmt && dirp->d_type != DT_DIR) {
443 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
444 dirp->d_type = DT_DIR;
445 if (reply("FIX") == 1)
446 ret |= ALTERED;
447 else
448 markclean = 0;
449 }
450 goto chk2;
451 }
452 if (iswap32(dirp->d_ino) != 0 && strcmp(dirp->d_name, ".") != 0) {
453 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
454 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
455 dirp->d_name);
456 inp->i_dotdot = (ino_t)-1;
457 markclean = 0;
458 } else if (iswap16(dirp->d_reclen) < entrysize) {
459 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
460 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
461 inp->i_dotdot = (ino_t)-1;
462 markclean = 0;
463 } else if (inp->i_parent != 0) {
464 /*
465 * We know the parent, so fix now.
466 */
467 inp->i_dotdot = inp->i_parent;
468 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
469 proto.d_reclen = dirp->d_reclen;
470 memmove(dirp, &proto, (size_t)entrysize);
471 if (reply("FIX") == 1)
472 ret |= ALTERED;
473 else
474 markclean = 0;
475 }
476 idesc->id_entryno++;
477 if (dirp->d_ino != 0)
478 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
479 return (ret|KEEPON);
480 chk2:
481 if (dirp->d_ino == 0)
482 return (ret|KEEPON);
483 if (dirp->d_namlen <= 2 &&
484 dirp->d_name[0] == '.' &&
485 idesc->id_entryno >= 2) {
486 if (dirp->d_namlen == 1) {
487 direrror(idesc->id_number, "EXTRA '.' ENTRY");
488 dirp->d_ino = 0;
489 if (reply("FIX") == 1)
490 ret |= ALTERED;
491 else
492 markclean = 0;
493 return (KEEPON | ret);
494 }
495 if (dirp->d_name[1] == '.') {
496 direrror(idesc->id_number, "EXTRA '..' ENTRY");
497 dirp->d_ino = 0;
498 if (reply("FIX") == 1)
499 ret |= ALTERED;
500 else
501 markclean = 0;
502 return (KEEPON | ret);
503 }
504 }
505 idesc->id_entryno++;
506 n = 0;
507 if (iswap32(dirp->d_ino) > maxino) {
508 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
509 n = reply("REMOVE");
510 if (n == 0)
511 markclean = 0;
512 } else if (newinofmt &&
513 ((iswap32(dirp->d_ino) == WINO && dirp->d_type != DT_WHT) ||
514 (iswap32(dirp->d_ino) != WINO && dirp->d_type == DT_WHT))) {
515 fileerror(idesc->id_number, iswap32(dirp->d_ino), "BAD WHITEOUT ENTRY");
516 dirp->d_ino = iswap32(WINO);
517 dirp->d_type = DT_WHT;
518 if (reply("FIX") == 1)
519 ret |= ALTERED;
520 else
521 markclean = 0;
522 } else {
523 again:
524 info = inoinfo(iswap32(dirp->d_ino));
525 switch (info->ino_state) {
526 case USTATE:
527 if (idesc->id_entryno <= 2)
528 break;
529 fileerror(idesc->id_number, iswap32(dirp->d_ino), "UNALLOCATED");
530 n = reply("REMOVE");
531 if (n == 0)
532 markclean = 0;
533 break;
534
535 case DCLEAR:
536 case FCLEAR:
537 if (idesc->id_entryno <= 2)
538 break;
539 if (info->ino_state == FCLEAR)
540 errmsg = "DUP/BAD";
541 else if (!preen && !usedsoftdep)
542 errmsg = "ZERO LENGTH DIRECTORY";
543 else {
544 n = 1;
545 break;
546 }
547 fileerror(idesc->id_number, iswap32(dirp->d_ino), errmsg);
548 if ((n = reply("REMOVE")) == 1)
549 break;
550 dp = ginode(iswap32(dirp->d_ino));
551 info->ino_state =
552 (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? DSTATE : FSTATE;
553 info->ino_linkcnt = iswap16(DIP(dp, nlink));
554 goto again;
555
556 case DSTATE:
557 case DFOUND:
558 inp = getinoinfo(iswap32(dirp->d_ino));
559 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
560 getpathname(pathbuf, sizeof(pathbuf),
561 idesc->id_number, idesc->id_number);
562 getpathname(namebuf, sizeof(namebuf),
563 iswap32(dirp->d_ino), iswap32(dirp->d_ino));
564 pwarn("%s %s %s\n", pathbuf,
565 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
566 namebuf);
567 if (preen)
568 printf(" (IGNORED)\n");
569 else if ((n = reply("REMOVE")) == 1)
570 break;
571 }
572 if (idesc->id_entryno > 2)
573 inp->i_parent = idesc->id_number;
574 /* fall through */
575
576 case FSTATE:
577 if (newinofmt && dirp->d_type != info->ino_type) {
578 fileerror(idesc->id_number, iswap32(dirp->d_ino),
579 "BAD TYPE VALUE");
580 dirp->d_type = info->ino_type;
581 if (reply("FIX") == 1)
582 ret |= ALTERED;
583 else
584 markclean = 0;
585 }
586 info->ino_linkcnt--;
587 break;
588
589 default:
590 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
591 info->ino_state, iswap32(dirp->d_ino));
592 }
593 }
594 if (n == 0)
595 return (ret|KEEPON);
596 dirp->d_ino = 0;
597 return (ret|KEEPON|ALTERED);
598 }
599
600 /*
601 * Routine to sort disk blocks.
602 */
603 static int
604 blksort(arg1, arg2)
605 const void *arg1, *arg2;
606 {
607
608 return ((*(struct inoinfo **)arg1)->i_blks[0] -
609 (*(struct inoinfo **)arg2)->i_blks[0]);
610 }
611