pass2.c revision 1.28 1 /* $NetBSD: pass2.c,v 1.28 2015/09/15 14:58:05 dholland 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/mount.h>
36 #include <sys/buf.h>
37
38 #include <ufs/lfs/lfs.h>
39 #include <ufs/lfs/lfs_accessors.h>
40 #include <ufs/lfs/lfs_inode.h>
41
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "bufcache.h"
48 #include "vnode.h"
49 #include "lfs_user.h"
50
51 #include "fsck.h"
52 #include "fsutil.h"
53 #include "extern.h"
54
55 #define MINDIRSIZE (sizeof (struct lfs_dirtemplate))
56
57 static int pass2check(struct inodesc *);
58 static int blksort(const void *, const void *);
59
60 void
61 pass2(void)
62 {
63 union lfs_dinode *dp;
64 struct uvnode *vp;
65 struct inoinfo **inpp, *inp;
66 struct inoinfo **inpend;
67 struct inodesc curino;
68 union lfs_dinode dino;
69 char pathbuf[MAXPATHLEN + 1];
70 uint16_t mode;
71 unsigned ii;
72
73 switch (statemap[ULFS_ROOTINO]) {
74
75 case USTATE:
76 pfatal("ROOT INODE UNALLOCATED");
77 if (reply("ALLOCATE") == 0)
78 err(EEXIT, "%s", "");
79 if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
80 err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
81 break;
82
83 case DCLEAR:
84 pfatal("DUPS/BAD IN ROOT INODE");
85 if (reply("REALLOCATE")) {
86 freeino(ULFS_ROOTINO);
87 if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
88 err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
89 break;
90 }
91 if (reply("CONTINUE") == 0)
92 err(EEXIT, "%s", "");
93 break;
94
95 case FSTATE:
96 case FCLEAR:
97 pfatal("ROOT INODE NOT DIRECTORY");
98 if (reply("REALLOCATE")) {
99 freeino(ULFS_ROOTINO);
100 if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
101 err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
102 break;
103 }
104 if (reply("FIX") == 0)
105 errx(EEXIT, "%s", "");
106 vp = vget(fs, ULFS_ROOTINO);
107 dp = VTOD(vp);
108 mode = lfs_dino_getmode(fs, dp);
109 mode &= ~LFS_IFMT;
110 mode |= LFS_IFDIR;
111 lfs_dino_setmode(fs, dp, mode);
112 inodirty(VTOI(vp));
113 break;
114
115 case DSTATE:
116 break;
117
118 default:
119 errx(EEXIT, "BAD STATE %d FOR ROOT INODE", statemap[ULFS_ROOTINO]);
120 }
121 statemap[ULFS_WINO] = FSTATE;
122 typemap[ULFS_WINO] = LFS_DT_WHT;
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, LFS_DIRBLKSIZ);
141 if (reply("FIX") == 1) {
142 vp = vget(fs, inp->i_number);
143 dp = VTOD(vp);
144 lfs_dino_setsize(fs, dp, inp->i_isize);
145 inodirty(VTOI(vp));
146 }
147 } else if ((inp->i_isize & (LFS_DIRBLKSIZ - 1)) != 0) {
148 getpathname(pathbuf, sizeof(pathbuf), inp->i_number,
149 inp->i_number);
150 pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
151 pathbuf, (unsigned long) inp->i_isize, LFS_DIRBLKSIZ);
152 if (preen)
153 printf(" (ADJUSTED)\n");
154 inp->i_isize = roundup(inp->i_isize, LFS_DIRBLKSIZ);
155 if (preen || reply("ADJUST") == 1) {
156 vp = vget(fs, inp->i_number);
157 dp = VTOD(vp);
158 lfs_dino_setsize(fs, dp, inp->i_isize);
159 inodirty(VTOI(vp));
160 }
161 }
162 memset(&dino, 0, sizeof(dino));
163 lfs_dino_setmode(fs, &dino, LFS_IFDIR);
164 lfs_dino_setsize(fs, &dino, inp->i_isize);
165 for (ii = 0; ii < inp->i_numblks / sizeof(inp->i_blks[0]) &&
166 ii < ULFS_NDADDR; ii++) {
167 lfs_dino_setdb(fs, &dino, ii, inp->i_blks[ii]);
168 }
169 for (; ii < inp->i_numblks / sizeof(inp->i_blks[0]); ii++) {
170 lfs_dino_setib(fs, &dino, ii - ULFS_NDADDR,
171 inp->i_blks[ii]);
172 }
173 curino.id_number = inp->i_number;
174 curino.id_parent = inp->i_parent;
175 (void) ckinode(&dino, &curino);
176 }
177 /*
178 * Now that the parents of all directories have been found,
179 * make another pass to verify the value of `..'
180 */
181 for (inpp = inpsort; inpp < inpend; inpp++) {
182 inp = *inpp;
183 if (inp->i_parent == 0 || inp->i_isize == 0)
184 continue;
185 if (inp->i_dotdot == inp->i_parent ||
186 inp->i_dotdot == (ino_t) - 1)
187 continue;
188 if (inp->i_dotdot == 0) {
189 inp->i_dotdot = inp->i_parent;
190 fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
191 if (reply("FIX") == 0)
192 continue;
193 (void) makeentry(inp->i_number, inp->i_parent, "..");
194 lncntp[inp->i_parent]--;
195 continue;
196 }
197 fileerror(inp->i_parent, inp->i_number,
198 "BAD INODE NUMBER FOR '..'");
199 if (reply("FIX") == 0)
200 continue;
201 lncntp[inp->i_dotdot]++;
202 lncntp[inp->i_parent]--;
203 inp->i_dotdot = inp->i_parent;
204 (void) changeino(inp->i_number, "..", inp->i_parent);
205 }
206 /*
207 * Mark all the directories that can be found from the root.
208 */
209 propagate();
210 }
211
212 static int
213 pass2check(struct inodesc * idesc)
214 {
215 struct lfs_direct *dirp = idesc->id_dirp;
216 struct inoinfo *inp;
217 int n, entrysize, ret = 0;
218 union lfs_dinode *dp;
219 const char *errmsg;
220 struct lfs_direct proto;
221 char namebuf[MAXPATHLEN + 1];
222 char pathbuf[MAXPATHLEN + 1];
223
224 /*
225 * check for "."
226 */
227 if (idesc->id_entryno != 0)
228 goto chk1;
229 if (lfs_dir_getino(fs, dirp) != 0 && strcmp(dirp->d_name, ".") == 0) {
230 if (lfs_dir_getino(fs, dirp) != idesc->id_number) {
231 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
232 if (reply("FIX") == 1) {
233 lfs_dir_setino(fs, dirp, idesc->id_number);
234 ret |= ALTERED;
235 }
236 }
237 if (lfs_dir_gettype(fs, dirp) != LFS_DT_DIR) {
238 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
239 if (reply("FIX") == 1) {
240 lfs_dir_settype(fs, dirp, LFS_DT_DIR);
241 ret |= ALTERED;
242 }
243 }
244 goto chk1;
245 }
246 direrror(idesc->id_number, "MISSING '.'");
247 lfs_dir_setino(fs, &proto, idesc->id_number);
248 lfs_dir_settype(fs, &proto, LFS_DT_DIR);
249 lfs_dir_setnamlen(fs, &proto, 1);
250 (void) strlcpy(proto.d_name, ".", sizeof(proto.d_name));
251 entrysize = LFS_DIRSIZ(fs, &proto);
252 if (lfs_dir_getino(fs, dirp) != 0 && strcmp(dirp->d_name, "..") != 0) {
253 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
254 dirp->d_name);
255 } else if (lfs_dir_getreclen(fs, dirp) < entrysize) {
256 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
257 } else if (lfs_dir_getreclen(fs, dirp) < 2 * entrysize) {
258 lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp));
259 memcpy(dirp, &proto, (size_t) entrysize);
260 if (reply("FIX") == 1)
261 ret |= ALTERED;
262 } else {
263 n = lfs_dir_getreclen(fs, dirp) - entrysize;
264 lfs_dir_setreclen(fs, &proto, entrysize);
265 memcpy(dirp, &proto, (size_t) entrysize);
266 idesc->id_entryno++;
267 lncntp[lfs_dir_getino(fs, dirp)]--;
268 dirp = (struct lfs_direct *) ((char *) (dirp) + entrysize);
269 memset(dirp, 0, (size_t) n);
270 lfs_dir_setreclen(fs, dirp, n);
271 if (reply("FIX") == 1)
272 ret |= ALTERED;
273 }
274 chk1:
275 if (idesc->id_entryno > 1)
276 goto chk2;
277 inp = getinoinfo(idesc->id_number);
278 lfs_dir_setino(fs, &proto, inp->i_parent);
279 lfs_dir_settype(fs, &proto, LFS_DT_DIR);
280 lfs_dir_setnamlen(fs, &proto, 2);
281 (void) strlcpy(proto.d_name, "..", sizeof(proto.d_name));
282 entrysize = LFS_DIRSIZ(fs, &proto);
283 if (idesc->id_entryno == 0) {
284 n = LFS_DIRSIZ(fs, dirp);
285 if (lfs_dir_getreclen(fs, dirp) < n + entrysize)
286 goto chk2;
287 lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp) - n);
288 lfs_dir_setreclen(fs, dirp, n);
289 idesc->id_entryno++;
290 lncntp[lfs_dir_getino(fs, dirp)]--;
291 dirp = (struct lfs_direct *) ((char *) (dirp) + n);
292 memset(dirp, 0, lfs_dir_getreclen(fs, &proto));
293 lfs_dir_setreclen(fs, dirp, lfs_dir_getreclen(fs, &proto));
294 }
295 if (lfs_dir_getino(fs, dirp) != 0 && strcmp(dirp->d_name, "..") == 0) {
296 inp->i_dotdot = lfs_dir_getino(fs, dirp);
297 if (lfs_dir_gettype(fs, dirp) != LFS_DT_DIR) {
298 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
299 lfs_dir_settype(fs, dirp, LFS_DT_DIR);
300 if (reply("FIX") == 1)
301 ret |= ALTERED;
302 }
303 goto chk2;
304 }
305 if (lfs_dir_getino(fs, dirp) != 0 && strcmp(dirp->d_name, ".") != 0) {
306 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
307 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
308 dirp->d_name);
309 inp->i_dotdot = (ino_t) - 1;
310 } else if (lfs_dir_getreclen(fs, dirp) < entrysize) {
311 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
312 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
313 inp->i_dotdot = (ino_t) - 1;
314 } else if (inp->i_parent != 0) {
315 /*
316 * We know the parent, so fix now.
317 */
318 inp->i_dotdot = inp->i_parent;
319 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
320 lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp));
321 memcpy(dirp, &proto, (size_t) entrysize);
322 if (reply("FIX") == 1)
323 ret |= ALTERED;
324 }
325 idesc->id_entryno++;
326 if (lfs_dir_getino(fs, dirp) != 0)
327 lncntp[lfs_dir_getino(fs, dirp)]--;
328 return (ret | KEEPON);
329 chk2:
330 if (lfs_dir_getino(fs, dirp) == 0)
331 return (ret | KEEPON);
332 if (lfs_dir_getnamlen(fs, dirp) <= 2 &&
333 dirp->d_name[0] == '.' &&
334 idesc->id_entryno >= 2) {
335 if (lfs_dir_getnamlen(fs, dirp) == 1) {
336 direrror(idesc->id_number, "EXTRA '.' ENTRY");
337 if (reply("FIX") == 1) {
338 lfs_dir_setino(fs, dirp, 0);
339 ret |= ALTERED;
340 }
341 return (KEEPON | ret);
342 }
343 if (dirp->d_name[1] == '.') {
344 direrror(idesc->id_number, "EXTRA '..' ENTRY");
345 if (reply("FIX") == 1) {
346 lfs_dir_setino(fs, dirp, 0);
347 ret |= ALTERED;
348 }
349 return (KEEPON | ret);
350 }
351 }
352 idesc->id_entryno++;
353 n = 0;
354 if (lfs_dir_getino(fs, dirp) >= maxino) {
355 fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), "I OUT OF RANGE");
356 n = reply("REMOVE");
357 } else if (lfs_dir_getino(fs, dirp) == LFS_IFILE_INUM &&
358 idesc->id_number == ULFS_ROOTINO) {
359 if (lfs_dir_gettype(fs, dirp) != LFS_DT_REG) {
360 fileerror(idesc->id_number, lfs_dir_getino(fs, dirp),
361 "BAD TYPE FOR IFILE");
362 if (reply("FIX") == 1) {
363 lfs_dir_settype(fs, dirp, LFS_DT_REG);
364 ret |= ALTERED;
365 }
366 }
367 } else if (((lfs_dir_getino(fs, dirp) == ULFS_WINO && lfs_dir_gettype(fs, dirp) != LFS_DT_WHT) ||
368 (lfs_dir_getino(fs, dirp) != ULFS_WINO && lfs_dir_gettype(fs, dirp) == LFS_DT_WHT))) {
369 fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), "BAD WHITEOUT ENTRY");
370 if (reply("FIX") == 1) {
371 lfs_dir_setino(fs, dirp, ULFS_WINO);
372 lfs_dir_settype(fs, dirp, LFS_DT_WHT);
373 ret |= ALTERED;
374 }
375 } else {
376 again:
377 switch (statemap[lfs_dir_getino(fs, dirp)]) {
378 case USTATE:
379 if (idesc->id_entryno <= 2)
380 break;
381 fileerror(idesc->id_number, lfs_dir_getino(fs, dirp),
382 "UNALLOCATED");
383 n = reply("REMOVE");
384 break;
385
386 case DCLEAR:
387 case FCLEAR:
388 if (idesc->id_entryno <= 2)
389 break;
390 if (statemap[lfs_dir_getino(fs, dirp)] == FCLEAR)
391 errmsg = "DUP/BAD";
392 else if (!preen)
393 errmsg = "ZERO LENGTH DIRECTORY";
394 else {
395 n = 1;
396 break;
397 }
398 fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), errmsg);
399 if ((n = reply("REMOVE")) == 1)
400 break;
401 dp = ginode(lfs_dir_getino(fs, dirp));
402 statemap[lfs_dir_getino(fs, dirp)] =
403 (lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR ? DSTATE : FSTATE;
404 lncntp[lfs_dir_getino(fs, dirp)] = lfs_dino_getnlink(fs, dp);
405 goto again;
406
407 case DSTATE:
408 case DFOUND:
409 inp = getinoinfo(lfs_dir_getino(fs, dirp));
410 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
411 getpathname(pathbuf, sizeof(pathbuf),
412 idesc->id_number, idesc->id_number);
413 getpathname(namebuf, sizeof(namebuf),
414 lfs_dir_getino(fs, dirp),
415 lfs_dir_getino(fs, dirp));
416 pwarn("%s %s %s\n", pathbuf,
417 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
418 namebuf);
419 if (preen)
420 printf(" (IGNORED)\n");
421 else if ((n = reply("REMOVE")) == 1)
422 break;
423 }
424 if (idesc->id_entryno > 2)
425 inp->i_parent = idesc->id_number;
426 /* fall through */
427
428 case FSTATE:
429 if (lfs_dir_gettype(fs, dirp) != typemap[lfs_dir_getino(fs, dirp)]) {
430 fileerror(idesc->id_number,
431 lfs_dir_getino(fs, dirp),
432 "BAD TYPE VALUE");
433 if (debug)
434 pwarn("dir has %d, typemap has %d\n",
435 lfs_dir_gettype(fs, dirp), typemap[lfs_dir_getino(fs, dirp)]);
436 lfs_dir_settype(fs, dirp, typemap[lfs_dir_getino(fs, dirp)]);
437 if (reply("FIX") == 1)
438 ret |= ALTERED;
439 }
440 lncntp[lfs_dir_getino(fs, dirp)]--;
441 break;
442
443 default:
444 errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
445 statemap[lfs_dir_getino(fs, dirp)],
446 (uintmax_t)lfs_dir_getino(fs, dirp));
447 }
448 }
449 if (n == 0)
450 return (ret | KEEPON);
451 lfs_dir_setino(fs, dirp, 0);
452 return (ret | KEEPON | ALTERED);
453 }
454 /*
455 * Routine to sort disk blocks.
456 */
457 static int
458 blksort(const void *inpp1, const void *inpp2)
459 {
460 return ((*(const struct inoinfo *const *) inpp1)->i_blks[0] -
461 (*(const struct inoinfo *const *) inpp2)->i_blks[0]);
462 }
463