pass2.c revision 1.26 1 /* $NetBSD: pass2.c,v 1.26 1999/11/15 19:18:25 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.26 1999/11/15 19:18:25 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 struct dinode *dp;
70 struct inoinfo **inpp, *inp;
71 struct inoinfo **inpend;
72 struct inodesc curino;
73 struct dinode dino;
74 char pathbuf[MAXPATHLEN + 1];
75
76 switch (statemap[ROOTINO]) {
77
78 case USTATE:
79 pfatal("ROOT INODE UNALLOCATED");
80 if (reply("ALLOCATE") == 0) {
81 markclean = 0;
82 ckfini();
83 exit(EEXIT);
84 }
85 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
86 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
87 break;
88
89 case DCLEAR:
90 pfatal("DUPS/BAD IN ROOT INODE");
91 if (reply("REALLOCATE")) {
92 freeino(ROOTINO);
93 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
94 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
95 break;
96 }
97 markclean = 0;
98 if (reply("CONTINUE") == 0) {
99 ckfini();
100 exit(EEXIT);
101 }
102 break;
103
104 case FSTATE:
105 case FCLEAR:
106 pfatal("ROOT INODE NOT DIRECTORY");
107 if (reply("REALLOCATE")) {
108 freeino(ROOTINO);
109 if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
110 errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
111 break;
112 }
113 if (reply("FIX") == 0) {
114 markclean = 0;
115 ckfini();
116 exit(EEXIT);
117 }
118 dp = ginode(ROOTINO);
119 dp->di_mode = iswap16((iswap16(dp->di_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", statemap[ROOTINO]);
128 }
129 if (newinofmt) {
130 statemap[WINO] = FSTATE;
131 typemap[WINO] = DT_WHT;
132 }
133 /*
134 * Sort the directory list into disk block order.
135 */
136 qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
137 /*
138 * Check the integrity of each directory.
139 */
140 memset(&curino, 0, sizeof(struct inodesc));
141 curino.id_type = DATA;
142 curino.id_func = pass2check;
143 inpend = &inpsort[inplast];
144 for (inpp = inpsort; inpp < inpend; inpp++) {
145 inp = *inpp;
146 if (inp->i_isize == 0)
147 continue;
148 if (inp->i_isize < MINDIRSIZE) {
149 direrror(inp->i_number, "DIRECTORY TOO SHORT");
150 inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
151 if (reply("FIX") == 1) {
152 dp = ginode(inp->i_number);
153 dp->di_size = iswap64(inp->i_isize);
154 inodirty();
155 } else
156 markclean = 0;
157 } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
158 getpathname(pathbuf, inp->i_number, inp->i_number);
159 if (usedsoftdep)
160 pfatal("%s %s: LENGTH %d NOT MULTIPLE OF %d",
161 "DIRECTORY", pathbuf, inp->i_isize,
162 DIRBLKSIZ);
163 else
164 pwarn("%s %s: LENGTH %d NOT MULTIPLE OF %d",
165 "DIRECTORY", pathbuf, inp->i_isize,
166 DIRBLKSIZ);
167 if (preen)
168 printf(" (ADJUSTED)\n");
169 inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
170 if (preen || reply("ADJUST") == 1) {
171 dp = ginode(inp->i_number);
172 dp->di_size = iswap64(inp->i_isize);
173 inodirty();
174 } else
175 markclean = 0;
176 }
177 memset(&dino, 0, DINODE_SIZE);
178 dino.di_mode = iswap16(IFDIR);
179 dino.di_size = iswap64(inp->i_isize);
180 memmove(&dino.di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks);
181 curino.id_number = inp->i_number;
182 curino.id_parent = inp->i_parent;
183 (void)ckinode(&dino, &curino);
184 }
185
186 /* byte swapping in direcoties entries, if needed, have been done.
187 * Now rescan dirs for pass2check()
188 */
189 if (do_dirswap) {
190 do_dirswap = 0;
191 for (inpp = inpsort; inpp < inpend; inpp++) {
192 inp = *inpp;
193 if (inp->i_isize == 0)
194 continue;
195 memset(&dino, 0, DINODE_SIZE);
196 dino.di_mode = iswap16(IFDIR);
197 dino.di_size = iswap64(inp->i_isize);
198 memmove(&dino.di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks);
199 curino.id_number = inp->i_number;
200 curino.id_parent = inp->i_parent;
201 (void)ckinode(&dino, &curino);
202 }
203 }
204
205 /*
206 * Now that the parents of all directories have been found,
207 * make another pass to verify the value of `..'
208 */
209 for (inpp = inpsort; inpp < inpend; inpp++) {
210 inp = *inpp;
211 if (inp->i_parent == 0 || inp->i_isize == 0)
212 continue;
213 if (inp->i_dotdot == inp->i_parent ||
214 inp->i_dotdot == (ino_t)-1)
215 continue;
216 if (inp->i_dotdot == 0) {
217 inp->i_dotdot = inp->i_parent;
218 fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
219 if (reply("FIX") == 0) {
220 markclean = 0;
221 continue;
222 }
223 (void)makeentry(inp->i_number, inp->i_parent, "..");
224 lncntp[inp->i_parent]--;
225 continue;
226 }
227 fileerror(inp->i_parent, inp->i_number,
228 "BAD INODE NUMBER FOR '..'");
229 if (reply("FIX") == 0) {
230 markclean = 0;
231 continue;
232 }
233 lncntp[inp->i_dotdot]++;
234 lncntp[inp->i_parent]--;
235 inp->i_dotdot = inp->i_parent;
236 (void)changeino(inp->i_number, "..", inp->i_parent);
237 }
238 /*
239 * Mark all the directories that can be found from the root.
240 */
241 propagate();
242 }
243
244 static int
245 pass2check(idesc)
246 struct inodesc *idesc;
247 {
248 struct direct *dirp = idesc->id_dirp;
249 struct inoinfo *inp;
250 int n, entrysize, ret = 0;
251 struct dinode *dp;
252 char *errmsg;
253 struct direct proto;
254 char namebuf[MAXPATHLEN + 1];
255 char pathbuf[MAXPATHLEN + 1];
256
257 /*
258 * If converting, set directory entry type.
259 */
260 if (doinglevel2 && iswap32(dirp->d_ino) > 0 && iswap32(dirp->d_ino) < maxino) {
261 dirp->d_type = typemap[iswap32(dirp->d_ino)];
262 ret |= ALTERED;
263 }
264 /*
265 * check for "."
266 */
267 if (idesc->id_entryno != 0)
268 goto chk1;
269 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
270 if (iswap32(dirp->d_ino) != idesc->id_number) {
271 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
272 dirp->d_ino = iswap32(idesc->id_number);
273 if (reply("FIX") == 1)
274 ret |= ALTERED;
275 else
276 markclean = 0;
277 }
278 if (newinofmt && dirp->d_type != DT_DIR) {
279 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
280 dirp->d_type = DT_DIR;
281 if (reply("FIX") == 1)
282 ret |= ALTERED;
283 else
284 markclean = 0;
285 }
286 goto chk1;
287 }
288 direrror(idesc->id_number, "MISSING '.'");
289 proto.d_ino = iswap32(idesc->id_number);
290 if (newinofmt)
291 proto.d_type = DT_DIR;
292 else
293 proto.d_type = 0;
294 proto.d_namlen = 1;
295 (void)strcpy(proto.d_name, ".");
296 # if BYTE_ORDER == LITTLE_ENDIAN
297 if (!newinofmt && !needswap) {
298 # else
299 if (!newinofmt && needswap) {
300 # endif
301 u_char tmp;
302
303 tmp = proto.d_type;
304 proto.d_type = proto.d_namlen;
305 proto.d_namlen = tmp;
306 }
307 entrysize = DIRSIZ(0, &proto, 0);
308 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
309 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
310 dirp->d_name);
311 markclean = 0;
312 } else if (iswap16(dirp->d_reclen) < entrysize) {
313 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
314 markclean = 0;
315 } else if (iswap16(dirp->d_reclen) < 2 * entrysize) {
316 proto.d_reclen = dirp->d_reclen;
317 memmove(dirp, &proto, (size_t)entrysize);
318 if (reply("FIX") == 1)
319 ret |= ALTERED;
320 else
321 markclean = 0;
322 } else {
323 n = iswap16(dirp->d_reclen) - entrysize;
324 proto.d_reclen = iswap16(entrysize);
325 memmove(dirp, &proto, (size_t)entrysize);
326 idesc->id_entryno++;
327 lncntp[iswap32(dirp->d_ino)]--;
328 dirp = (struct direct *)((char *)(dirp) + entrysize);
329 memset(dirp, 0, (size_t)n);
330 dirp->d_reclen = iswap16(n);
331 if (reply("FIX") == 1)
332 ret |= ALTERED;
333 else
334 markclean = 0;
335 }
336 chk1:
337 if (idesc->id_entryno > 1)
338 goto chk2;
339 inp = getinoinfo(idesc->id_number);
340 proto.d_ino = iswap32(inp->i_parent);
341 if (newinofmt)
342 proto.d_type = DT_DIR;
343 else
344 proto.d_type = 0;
345 proto.d_namlen = 2;
346 (void)strcpy(proto.d_name, "..");
347 # if BYTE_ORDER == LITTLE_ENDIAN
348 if (!newinofmt && !needswap) {
349 # else
350 if (!newinofmt && needswap) {
351 # endif
352 u_char tmp;
353
354 tmp = proto.d_type;
355 proto.d_type = proto.d_namlen;
356 proto.d_namlen = tmp;
357 }
358 entrysize = DIRSIZ(0, &proto, 0);
359 if (idesc->id_entryno == 0) {
360 n = DIRSIZ(0, dirp, 0);
361 if (iswap16(dirp->d_reclen) < n + entrysize)
362 goto chk2;
363 proto.d_reclen = iswap16(iswap16(dirp->d_reclen) - n);
364 dirp->d_reclen = iswap16(n);
365 idesc->id_entryno++;
366 lncntp[iswap32(dirp->d_ino)]--;
367 dirp = (struct direct *)((char *)(dirp) + n);
368 memset(dirp, 0, (size_t)iswap16(proto.d_reclen));
369 dirp->d_reclen = proto.d_reclen;
370 }
371 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
372 inp->i_dotdot = iswap32(dirp->d_ino);
373 if (newinofmt && dirp->d_type != DT_DIR) {
374 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
375 dirp->d_type = DT_DIR;
376 if (reply("FIX") == 1)
377 ret |= ALTERED;
378 else
379 markclean = 0;
380 }
381 goto chk2;
382 }
383 if (iswap32(dirp->d_ino) != 0 && strcmp(dirp->d_name, ".") != 0) {
384 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
385 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
386 dirp->d_name);
387 inp->i_dotdot = (ino_t)-1;
388 markclean = 0;
389 } else if (iswap16(dirp->d_reclen) < entrysize) {
390 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
391 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
392 inp->i_dotdot = (ino_t)-1;
393 markclean = 0;
394 } else if (inp->i_parent != 0) {
395 /*
396 * We know the parent, so fix now.
397 */
398 inp->i_dotdot = inp->i_parent;
399 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
400 proto.d_reclen = dirp->d_reclen;
401 memmove(dirp, &proto, (size_t)entrysize);
402 if (reply("FIX") == 1)
403 ret |= ALTERED;
404 else
405 markclean = 0;
406 }
407 idesc->id_entryno++;
408 if (dirp->d_ino != 0)
409 lncntp[iswap32(dirp->d_ino)]--;
410 return (ret|KEEPON);
411 chk2:
412 if (dirp->d_ino == 0)
413 return (ret|KEEPON);
414 if (dirp->d_namlen <= 2 &&
415 dirp->d_name[0] == '.' &&
416 idesc->id_entryno >= 2) {
417 if (dirp->d_namlen == 1) {
418 direrror(idesc->id_number, "EXTRA '.' ENTRY");
419 dirp->d_ino = 0;
420 if (reply("FIX") == 1)
421 ret |= ALTERED;
422 else
423 markclean = 0;
424 return (KEEPON | ret);
425 }
426 if (dirp->d_name[1] == '.') {
427 direrror(idesc->id_number, "EXTRA '..' ENTRY");
428 dirp->d_ino = 0;
429 if (reply("FIX") == 1)
430 ret |= ALTERED;
431 else
432 markclean = 0;
433 return (KEEPON | ret);
434 }
435 }
436 idesc->id_entryno++;
437 n = 0;
438 if (iswap32(dirp->d_ino) > maxino) {
439 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
440 n = reply("REMOVE");
441 if (n == 0)
442 markclean = 0;
443 } else if (newinofmt &&
444 ((iswap32(dirp->d_ino) == WINO && dirp->d_type != DT_WHT) ||
445 (iswap32(dirp->d_ino) != WINO && dirp->d_type == DT_WHT))) {
446 fileerror(idesc->id_number, iswap32(dirp->d_ino), "BAD WHITEOUT ENTRY");
447 dirp->d_ino = iswap32(WINO);
448 dirp->d_type = DT_WHT;
449 if (reply("FIX") == 1)
450 ret |= ALTERED;
451 else
452 markclean = 0;
453 } else {
454 again:
455 switch (statemap[iswap32(dirp->d_ino)]) {
456 case USTATE:
457 if (idesc->id_entryno <= 2)
458 break;
459 fileerror(idesc->id_number, iswap32(dirp->d_ino), "UNALLOCATED");
460 n = reply("REMOVE");
461 if (n == 0)
462 markclean = 0;
463 break;
464
465 case DCLEAR:
466 case FCLEAR:
467 if (idesc->id_entryno <= 2)
468 break;
469 if (statemap[iswap32(dirp->d_ino)] == FCLEAR)
470 errmsg = "DUP/BAD";
471 else if (!preen && !usedsoftdep)
472 errmsg = "ZERO LENGTH DIRECTORY";
473 else {
474 n = 1;
475 break;
476 }
477 fileerror(idesc->id_number, iswap32(dirp->d_ino), errmsg);
478 if ((n = reply("REMOVE")) == 1)
479 break;
480 dp = ginode(iswap32(dirp->d_ino));
481 statemap[iswap32(dirp->d_ino)] =
482 (iswap16(dp->di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
483 lncntp[iswap32(dirp->d_ino)] = iswap16(dp->di_nlink);
484 goto again;
485
486 case DSTATE:
487 case DFOUND:
488 inp = getinoinfo(iswap32(dirp->d_ino));
489 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
490 getpathname(pathbuf, idesc->id_number,
491 idesc->id_number);
492 getpathname(namebuf, iswap32(dirp->d_ino),
493 iswap32(dirp->d_ino));
494 pwarn("%s %s %s\n", pathbuf,
495 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
496 namebuf);
497 if (preen)
498 printf(" (IGNORED)\n");
499 else if ((n = reply("REMOVE")) == 1)
500 break;
501 }
502 if (idesc->id_entryno > 2)
503 inp->i_parent = idesc->id_number;
504 /* fall through */
505
506 case FSTATE:
507 if (newinofmt && dirp->d_type != typemap[iswap32(dirp->d_ino)]) {
508 fileerror(idesc->id_number, iswap32(dirp->d_ino),
509 "BAD TYPE VALUE");
510 dirp->d_type = typemap[iswap32(dirp->d_ino)];
511 if (reply("FIX") == 1)
512 ret |= ALTERED;
513 else
514 markclean = 0;
515 }
516 lncntp[iswap32(dirp->d_ino)]--;
517 break;
518
519 default:
520 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
521 statemap[iswap32(dirp->d_ino)], iswap32(dirp->d_ino));
522 }
523 }
524 if (n == 0)
525 return (ret|KEEPON);
526 dirp->d_ino = 0;
527 return (ret|KEEPON|ALTERED);
528 }
529
530 /*
531 * Routine to sort disk blocks.
532 */
533 static int
534 blksort(arg1, arg2)
535 const void *arg1, *arg2;
536 {
537
538 return ((*(struct inoinfo **)arg1)->i_blks[0] -
539 (*(struct inoinfo **)arg2)->i_blks[0]);
540 }
541