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