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