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