pass2.c revision 1.27 1 /* $NetBSD: pass2.c,v 1.27 2015/09/01 06:16:58 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 (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
230 if (dirp->d_ino != idesc->id_number) {
231 direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
232 if (reply("FIX") == 1) {
233 dirp->d_ino = 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 proto.d_ino = 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 (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
253 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
254 dirp->d_name);
255 } else if (dirp->d_reclen < entrysize) {
256 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
257 } else if (dirp->d_reclen < 2 * entrysize) {
258 proto.d_reclen = dirp->d_reclen;
259 memcpy(dirp, &proto, (size_t) entrysize);
260 if (reply("FIX") == 1)
261 ret |= ALTERED;
262 } else {
263 n = dirp->d_reclen - entrysize;
264 proto.d_reclen = entrysize;
265 memcpy(dirp, &proto, (size_t) entrysize);
266 idesc->id_entryno++;
267 lncntp[dirp->d_ino]--;
268 dirp = (struct lfs_direct *) ((char *) (dirp) + entrysize);
269 memset(dirp, 0, (size_t) n);
270 dirp->d_reclen = 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 proto.d_ino = 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 (dirp->d_reclen < n + entrysize)
286 goto chk2;
287 proto.d_reclen = dirp->d_reclen - n;
288 dirp->d_reclen = n;
289 idesc->id_entryno++;
290 lncntp[dirp->d_ino]--;
291 dirp = (struct lfs_direct *) ((char *) (dirp) + n);
292 memset(dirp, 0, (size_t) proto.d_reclen);
293 dirp->d_reclen = proto.d_reclen;
294 }
295 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
296 inp->i_dotdot = dirp->d_ino;
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 (dirp->d_ino != 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 (dirp->d_reclen < 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 proto.d_reclen = dirp->d_reclen;
321 memcpy(dirp, &proto, (size_t) entrysize);
322 if (reply("FIX") == 1)
323 ret |= ALTERED;
324 }
325 idesc->id_entryno++;
326 if (dirp->d_ino != 0)
327 lncntp[dirp->d_ino]--;
328 return (ret | KEEPON);
329 chk2:
330 if (dirp->d_ino == 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 dirp->d_ino = 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 dirp->d_ino = 0;
347 ret |= ALTERED;
348 }
349 return (KEEPON | ret);
350 }
351 }
352 idesc->id_entryno++;
353 n = 0;
354 if (dirp->d_ino >= maxino) {
355 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
356 n = reply("REMOVE");
357 } else if (dirp->d_ino == LFS_IFILE_INUM &&
358 idesc->id_number == ULFS_ROOTINO) {
359 if (lfs_dir_gettype(fs, dirp) != LFS_DT_REG) {
360 fileerror(idesc->id_number, dirp->d_ino,
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 (((dirp->d_ino == ULFS_WINO && lfs_dir_gettype(fs, dirp) != LFS_DT_WHT) ||
368 (dirp->d_ino != ULFS_WINO && lfs_dir_gettype(fs, dirp) == LFS_DT_WHT))) {
369 fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
370 dirp->d_ino = ULFS_WINO;
371 if (reply("FIX") == 1) {
372 lfs_dir_settype(fs, dirp, LFS_DT_WHT);
373 ret |= ALTERED;
374 }
375 } else {
376 again:
377 switch (statemap[dirp->d_ino]) {
378 case USTATE:
379 if (idesc->id_entryno <= 2)
380 break;
381 fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
382 n = reply("REMOVE");
383 break;
384
385 case DCLEAR:
386 case FCLEAR:
387 if (idesc->id_entryno <= 2)
388 break;
389 if (statemap[dirp->d_ino] == FCLEAR)
390 errmsg = "DUP/BAD";
391 else if (!preen)
392 errmsg = "ZERO LENGTH DIRECTORY";
393 else {
394 n = 1;
395 break;
396 }
397 fileerror(idesc->id_number, dirp->d_ino, errmsg);
398 if ((n = reply("REMOVE")) == 1)
399 break;
400 dp = ginode(dirp->d_ino);
401 statemap[dirp->d_ino] =
402 (lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR ? DSTATE : FSTATE;
403 lncntp[dirp->d_ino] = lfs_dino_getnlink(fs, dp);
404 goto again;
405
406 case DSTATE:
407 case DFOUND:
408 inp = getinoinfo(dirp->d_ino);
409 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
410 getpathname(pathbuf, sizeof(pathbuf),
411 idesc->id_number, idesc->id_number);
412 getpathname(namebuf, sizeof(namebuf),
413 dirp->d_ino, dirp->d_ino);
414 pwarn("%s %s %s\n", pathbuf,
415 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
416 namebuf);
417 if (preen)
418 printf(" (IGNORED)\n");
419 else if ((n = reply("REMOVE")) == 1)
420 break;
421 }
422 if (idesc->id_entryno > 2)
423 inp->i_parent = idesc->id_number;
424 /* fall through */
425
426 case FSTATE:
427 if (lfs_dir_gettype(fs, dirp) != typemap[dirp->d_ino]) {
428 fileerror(idesc->id_number, dirp->d_ino,
429 "BAD TYPE VALUE");
430 if (debug)
431 pwarn("dir has %d, typemap has %d\n",
432 lfs_dir_gettype(fs, dirp), typemap[dirp->d_ino]);
433 lfs_dir_settype(fs, dirp, typemap[dirp->d_ino]);
434 if (reply("FIX") == 1)
435 ret |= ALTERED;
436 }
437 lncntp[dirp->d_ino]--;
438 break;
439
440 default:
441 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
442 statemap[dirp->d_ino], dirp->d_ino);
443 }
444 }
445 if (n == 0)
446 return (ret | KEEPON);
447 dirp->d_ino = 0;
448 return (ret | KEEPON | ALTERED);
449 }
450 /*
451 * Routine to sort disk blocks.
452 */
453 static int
454 blksort(const void *inpp1, const void *inpp2)
455 {
456 return ((*(const struct inoinfo *const *) inpp1)->i_blks[0] -
457 (*(const struct inoinfo *const *) inpp2)->i_blks[0]);
458 }
459