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