pass2.c revision 1.38 1 /* $NetBSD: pass2.c,v 1.38 2005/01/13 15:22:35 christos 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. 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 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)pass2.c 8.9 (Berkeley) 4/28/95";
36 #else
37 __RCSID("$NetBSD: pass2.c,v 1.38 2005/01/13 15:22:35 christos Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
47
48 #include <err.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "fsck.h"
54 #include "fsutil.h"
55 #include "extern.h"
56
57 #define MINDIRSIZE (sizeof (struct dirtemplate))
58
59 static int blksort __P((const void *, const void *));
60 static int pass2check __P((struct inodesc *));
61
62 void
63 pass2()
64 {
65 union dinode *dp;
66 struct inoinfo **inpp, *inp, *pinp;
67 struct inoinfo **inpend;
68 struct inostat *rinfo, *info;
69 struct inodesc curino;
70 union dinode dino;
71 int i, maxblk;
72 char pathbuf[MAXPATHLEN + 1];
73
74 rinfo = inoinfo(ROOTINO);
75 switch (rinfo->ino_state) {
76
77 case USTATE:
78 pfatal("ROOT INODE UNALLOCATED");
79 if (reply("ALLOCATE") == 0) {
80 markclean = 0;
81 ckfini();
82 exit(EEXIT);
83 }
84 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
85 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
86 break;
87
88 case DCLEAR:
89 pfatal("DUPS/BAD IN ROOT INODE");
90 if (reply("REALLOCATE")) {
91 freeino(ROOTINO);
92 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
93 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
94 break;
95 }
96 markclean = 0;
97 if (reply("CONTINUE") == 0) {
98 ckfini();
99 exit(EEXIT);
100 }
101 break;
102
103 case FSTATE:
104 case FCLEAR:
105 pfatal("ROOT INODE NOT DIRECTORY");
106 if (reply("REALLOCATE")) {
107 freeino(ROOTINO);
108 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
109 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
110 break;
111 }
112 if (reply("FIX") == 0) {
113 markclean = 0;
114 ckfini();
115 exit(EEXIT);
116 }
117 dp = ginode(ROOTINO);
118 DIP(dp, mode) =
119 iswap16((iswap16(DIP(dp, mode)) & ~IFMT) | IFDIR);
120 inodirty();
121 break;
122
123 case DSTATE:
124 break;
125
126 default:
127 errx(EEXIT, "BAD STATE %d FOR ROOT INODE", rinfo->ino_state);
128 }
129 if (newinofmt) {
130 info = inoinfo(WINO);
131 info->ino_state = FSTATE;
132 info->ino_type = DT_WHT;
133 }
134 /*
135 * Sort the directory list into disk block order.
136 */
137 qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
138 /*
139 * Check the integrity of each directory.
140 */
141 memset(&curino, 0, sizeof(struct inodesc));
142 curino.id_type = DATA;
143 curino.id_func = pass2check;
144 inpend = &inpsort[inplast];
145 for (inpp = inpsort; inpp < inpend; inpp++) {
146 if (got_siginfo) {
147 fprintf(stderr,
148 "%s: phase 2: dir %ld of %d (%d%%)\n", cdevname(),
149 (long)(inpp - inpsort), (int)inplast,
150 (int)((inpp - inpsort) * 100 / inplast));
151 got_siginfo = 0;
152 }
153 #ifndef SMALL
154 progress_bar(cdevname(), preen ? NULL : "phase 2",
155 (inpp - inpsort), inplast);
156 #endif /* ! SMALL */
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 = 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_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 #ifndef SMALL
310 if (preen)
311 progress_add(inplast);
312 else
313 progress_done();
314 #endif /* ! SMALL */
315 }
316
317 static int
318 pass2check(idesc)
319 struct inodesc *idesc;
320 {
321 struct direct *dirp = idesc->id_dirp;
322 struct inoinfo *inp;
323 struct inostat *info;
324 int n, entrysize, ret = 0;
325 union dinode *dp;
326 char *errmsg;
327 struct direct proto;
328 char namebuf[MAXPATHLEN + 1];
329 char pathbuf[MAXPATHLEN + 1];
330
331 /*
332 * If converting, set directory entry type.
333 */
334 if (!is_ufs2 && doinglevel2 && iswap32(dirp->d_ino) > 0 &&
335 iswap32(dirp->d_ino) < maxino) {
336 dirp->d_type = inoinfo(iswap32(dirp->d_ino))->ino_type;
337 ret |= ALTERED;
338 }
339 /*
340 * check for "."
341 */
342 if (idesc->id_entryno != 0)
343 goto chk1;
344 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
345 if (iswap32(dirp->d_ino) != idesc->id_number) {
346 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
347 dirp->d_ino = iswap32(idesc->id_number);
348 if (reply("FIX") == 1)
349 ret |= ALTERED;
350 else
351 markclean = 0;
352 }
353 if (newinofmt && dirp->d_type != DT_DIR) {
354 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
355 dirp->d_type = DT_DIR;
356 if (reply("FIX") == 1)
357 ret |= ALTERED;
358 else
359 markclean = 0;
360 }
361 goto chk1;
362 }
363 direrror(idesc->id_number, "MISSING '.'");
364 proto.d_ino = iswap32(idesc->id_number);
365 if (newinofmt)
366 proto.d_type = DT_DIR;
367 else
368 proto.d_type = 0;
369 proto.d_namlen = 1;
370 (void)strlcpy(proto.d_name, ".", sizeof(proto.d_name));
371 # if BYTE_ORDER == LITTLE_ENDIAN
372 if (!newinofmt && !needswap) {
373 # else
374 if (!newinofmt && needswap) {
375 # endif
376 u_char tmp;
377
378 tmp = proto.d_type;
379 proto.d_type = proto.d_namlen;
380 proto.d_namlen = tmp;
381 }
382 entrysize = DIRSIZ(0, &proto, 0);
383 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
384 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
385 dirp->d_name);
386 markclean = 0;
387 } else if (iswap16(dirp->d_reclen) < entrysize) {
388 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
389 markclean = 0;
390 } else if (iswap16(dirp->d_reclen) < 2 * entrysize) {
391 proto.d_reclen = dirp->d_reclen;
392 memmove(dirp, &proto, (size_t)entrysize);
393 if (reply("FIX") == 1)
394 ret |= ALTERED;
395 else
396 markclean = 0;
397 } else {
398 n = iswap16(dirp->d_reclen) - entrysize;
399 proto.d_reclen = iswap16(entrysize);
400 memmove(dirp, &proto, (size_t)entrysize);
401 idesc->id_entryno++;
402 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
403 dirp = (struct direct *)((char *)(dirp) + entrysize);
404 memset(dirp, 0, (size_t)n);
405 dirp->d_reclen = iswap16(n);
406 if (reply("FIX") == 1)
407 ret |= ALTERED;
408 else
409 markclean = 0;
410 }
411 chk1:
412 if (idesc->id_entryno > 1)
413 goto chk2;
414 inp = getinoinfo(idesc->id_number);
415 proto.d_ino = iswap32(inp->i_parent);
416 if (newinofmt)
417 proto.d_type = DT_DIR;
418 else
419 proto.d_type = 0;
420 proto.d_namlen = 2;
421 (void)strlcpy(proto.d_name, "..", sizeof(proto.d_name));
422 #if BYTE_ORDER == LITTLE_ENDIAN
423 if (!newinofmt && !needswap) {
424 #else
425 if (!newinofmt && needswap) {
426 #endif
427 u_char tmp;
428
429 tmp = proto.d_type;
430 proto.d_type = proto.d_namlen;
431 proto.d_namlen = tmp;
432 }
433 entrysize = DIRSIZ(0, &proto, 0);
434 if (idesc->id_entryno == 0) {
435 n = DIRSIZ(0, dirp, 0);
436 if (iswap16(dirp->d_reclen) < n + entrysize)
437 goto chk2;
438 proto.d_reclen = iswap16(iswap16(dirp->d_reclen) - n);
439 dirp->d_reclen = iswap16(n);
440 idesc->id_entryno++;
441 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
442 dirp = (struct direct *)((char *)(dirp) + n);
443 memset(dirp, 0, (size_t)iswap16(proto.d_reclen));
444 dirp->d_reclen = proto.d_reclen;
445 }
446 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
447 inp->i_dotdot = iswap32(dirp->d_ino);
448 if (newinofmt && dirp->d_type != DT_DIR) {
449 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
450 dirp->d_type = DT_DIR;
451 if (reply("FIX") == 1)
452 ret |= ALTERED;
453 else
454 markclean = 0;
455 }
456 goto chk2;
457 }
458 if (iswap32(dirp->d_ino) != 0 && strcmp(dirp->d_name, ".") != 0) {
459 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
460 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
461 dirp->d_name);
462 inp->i_dotdot = (ino_t)-1;
463 markclean = 0;
464 } else if (iswap16(dirp->d_reclen) < entrysize) {
465 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
466 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
467 inp->i_dotdot = (ino_t)-1;
468 markclean = 0;
469 } else if (inp->i_parent != 0) {
470 /*
471 * We know the parent, so fix now.
472 */
473 inp->i_dotdot = inp->i_parent;
474 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
475 proto.d_reclen = dirp->d_reclen;
476 memmove(dirp, &proto, (size_t)entrysize);
477 if (reply("FIX") == 1)
478 ret |= ALTERED;
479 else
480 markclean = 0;
481 }
482 idesc->id_entryno++;
483 if (dirp->d_ino != 0)
484 inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
485 return (ret|KEEPON);
486 chk2:
487 if (dirp->d_ino == 0)
488 return (ret|KEEPON);
489 if (dirp->d_namlen <= 2 &&
490 dirp->d_name[0] == '.' &&
491 idesc->id_entryno >= 2) {
492 if (dirp->d_namlen == 1) {
493 direrror(idesc->id_number, "EXTRA '.' ENTRY");
494 dirp->d_ino = 0;
495 if (reply("FIX") == 1)
496 ret |= ALTERED;
497 else
498 markclean = 0;
499 return (KEEPON | ret);
500 }
501 if (dirp->d_name[1] == '.') {
502 direrror(idesc->id_number, "EXTRA '..' ENTRY");
503 dirp->d_ino = 0;
504 if (reply("FIX") == 1)
505 ret |= ALTERED;
506 else
507 markclean = 0;
508 return (KEEPON | ret);
509 }
510 }
511 idesc->id_entryno++;
512 n = 0;
513 if (iswap32(dirp->d_ino) > maxino) {
514 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
515 n = reply("REMOVE");
516 if (n == 0)
517 markclean = 0;
518 } else if (newinofmt &&
519 ((iswap32(dirp->d_ino) == WINO && dirp->d_type != DT_WHT) ||
520 (iswap32(dirp->d_ino) != WINO && dirp->d_type == DT_WHT))) {
521 fileerror(idesc->id_number, iswap32(dirp->d_ino), "BAD WHITEOUT ENTRY");
522 dirp->d_ino = iswap32(WINO);
523 dirp->d_type = DT_WHT;
524 if (reply("FIX") == 1)
525 ret |= ALTERED;
526 else
527 markclean = 0;
528 } else {
529 again:
530 info = inoinfo(iswap32(dirp->d_ino));
531 switch (info->ino_state) {
532 case USTATE:
533 if (idesc->id_entryno <= 2)
534 break;
535 fileerror(idesc->id_number, iswap32(dirp->d_ino), "UNALLOCATED");
536 n = reply("REMOVE");
537 if (n == 0)
538 markclean = 0;
539 break;
540
541 case DCLEAR:
542 case FCLEAR:
543 if (idesc->id_entryno <= 2)
544 break;
545 if (info->ino_state == FCLEAR)
546 errmsg = "DUP/BAD";
547 else if (!preen && !usedsoftdep)
548 errmsg = "ZERO LENGTH DIRECTORY";
549 else {
550 n = 1;
551 break;
552 }
553 fileerror(idesc->id_number, iswap32(dirp->d_ino), errmsg);
554 if ((n = reply("REMOVE")) == 1)
555 break;
556 dp = ginode(iswap32(dirp->d_ino));
557 info->ino_state =
558 (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? DSTATE : FSTATE;
559 info->ino_linkcnt = iswap16(DIP(dp, nlink));
560 goto again;
561
562 case DSTATE:
563 case DFOUND:
564 inp = getinoinfo(iswap32(dirp->d_ino));
565 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
566 getpathname(pathbuf, sizeof(pathbuf),
567 idesc->id_number, idesc->id_number);
568 getpathname(namebuf, sizeof(namebuf),
569 iswap32(dirp->d_ino), iswap32(dirp->d_ino));
570 pwarn("%s %s %s\n", pathbuf,
571 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
572 namebuf);
573 if (preen)
574 printf(" (IGNORED)\n");
575 else if ((n = reply("REMOVE")) == 1)
576 break;
577 }
578 if (idesc->id_entryno > 2)
579 inp->i_parent = idesc->id_number;
580 /* fall through */
581
582 case FSTATE:
583 if (newinofmt && dirp->d_type != info->ino_type) {
584 fileerror(idesc->id_number, iswap32(dirp->d_ino),
585 "BAD TYPE VALUE");
586 dirp->d_type = info->ino_type;
587 if (reply("FIX") == 1)
588 ret |= ALTERED;
589 else
590 markclean = 0;
591 }
592 info->ino_linkcnt--;
593 break;
594
595 default:
596 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
597 info->ino_state, iswap32(dirp->d_ino));
598 }
599 }
600 if (n == 0)
601 return (ret|KEEPON);
602 dirp->d_ino = 0;
603 return (ret|KEEPON|ALTERED);
604 }
605
606 /*
607 * Routine to sort disk blocks.
608 */
609 static int
610 blksort(arg1, arg2)
611 const void *arg1, *arg2;
612 {
613
614 return ((*(struct inoinfo **)arg1)->i_blks[0] -
615 (*(struct inoinfo **)arg2)->i_blks[0]);
616 }
617