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