pass2.c revision 1.26 1 /* $NetBSD: pass2.c,v 1.26 2015/08/12 18:28:00 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 dirp->d_ino = idesc->id_number;
233 if (reply("FIX") == 1)
234 ret |= ALTERED;
235 }
236 if (dirp->d_type != LFS_DT_DIR) {
237 direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
238 dirp->d_type = LFS_DT_DIR;
239 if (reply("FIX") == 1)
240 ret |= ALTERED;
241 }
242 goto chk1;
243 }
244 direrror(idesc->id_number, "MISSING '.'");
245 proto.d_ino = idesc->id_number;
246 proto.d_type = LFS_DT_DIR;
247 proto.d_namlen = 1;
248 (void) strlcpy(proto.d_name, ".", sizeof(proto.d_name));
249 entrysize = LFS_DIRSIZ(0, &proto, 0);
250 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
251 pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
252 dirp->d_name);
253 } else if (dirp->d_reclen < entrysize) {
254 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
255 } else if (dirp->d_reclen < 2 * entrysize) {
256 proto.d_reclen = dirp->d_reclen;
257 memcpy(dirp, &proto, (size_t) entrysize);
258 if (reply("FIX") == 1)
259 ret |= ALTERED;
260 } else {
261 n = dirp->d_reclen - entrysize;
262 proto.d_reclen = entrysize;
263 memcpy(dirp, &proto, (size_t) entrysize);
264 idesc->id_entryno++;
265 lncntp[dirp->d_ino]--;
266 dirp = (struct lfs_direct *) ((char *) (dirp) + entrysize);
267 memset(dirp, 0, (size_t) n);
268 dirp->d_reclen = n;
269 if (reply("FIX") == 1)
270 ret |= ALTERED;
271 }
272 chk1:
273 if (idesc->id_entryno > 1)
274 goto chk2;
275 inp = getinoinfo(idesc->id_number);
276 proto.d_ino = inp->i_parent;
277 proto.d_type = LFS_DT_DIR;
278 proto.d_namlen = 2;
279 (void) strlcpy(proto.d_name, "..", sizeof(proto.d_name));
280 entrysize = LFS_DIRSIZ(0, &proto, 0);
281 if (idesc->id_entryno == 0) {
282 n = LFS_DIRSIZ(0, dirp, 0);
283 if (dirp->d_reclen < n + entrysize)
284 goto chk2;
285 proto.d_reclen = dirp->d_reclen - n;
286 dirp->d_reclen = n;
287 idesc->id_entryno++;
288 lncntp[dirp->d_ino]--;
289 dirp = (struct lfs_direct *) ((char *) (dirp) + n);
290 memset(dirp, 0, (size_t) proto.d_reclen);
291 dirp->d_reclen = proto.d_reclen;
292 }
293 if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
294 inp->i_dotdot = dirp->d_ino;
295 if (dirp->d_type != LFS_DT_DIR) {
296 direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
297 dirp->d_type = LFS_DT_DIR;
298 if (reply("FIX") == 1)
299 ret |= ALTERED;
300 }
301 goto chk2;
302 }
303 if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
304 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
305 pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
306 dirp->d_name);
307 inp->i_dotdot = (ino_t) - 1;
308 } else if (dirp->d_reclen < entrysize) {
309 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
310 pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
311 inp->i_dotdot = (ino_t) - 1;
312 } else if (inp->i_parent != 0) {
313 /*
314 * We know the parent, so fix now.
315 */
316 inp->i_dotdot = inp->i_parent;
317 fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
318 proto.d_reclen = dirp->d_reclen;
319 memcpy(dirp, &proto, (size_t) entrysize);
320 if (reply("FIX") == 1)
321 ret |= ALTERED;
322 }
323 idesc->id_entryno++;
324 if (dirp->d_ino != 0)
325 lncntp[dirp->d_ino]--;
326 return (ret | KEEPON);
327 chk2:
328 if (dirp->d_ino == 0)
329 return (ret | KEEPON);
330 if (dirp->d_namlen <= 2 &&
331 dirp->d_name[0] == '.' &&
332 idesc->id_entryno >= 2) {
333 if (dirp->d_namlen == 1) {
334 direrror(idesc->id_number, "EXTRA '.' ENTRY");
335 dirp->d_ino = 0;
336 if (reply("FIX") == 1)
337 ret |= ALTERED;
338 return (KEEPON | ret);
339 }
340 if (dirp->d_name[1] == '.') {
341 direrror(idesc->id_number, "EXTRA '..' ENTRY");
342 dirp->d_ino = 0;
343 if (reply("FIX") == 1)
344 ret |= ALTERED;
345 return (KEEPON | ret);
346 }
347 }
348 idesc->id_entryno++;
349 n = 0;
350 if (dirp->d_ino >= maxino) {
351 fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
352 n = reply("REMOVE");
353 } else if (dirp->d_ino == LFS_IFILE_INUM &&
354 idesc->id_number == ULFS_ROOTINO) {
355 if (dirp->d_type != LFS_DT_REG) {
356 fileerror(idesc->id_number, dirp->d_ino,
357 "BAD TYPE FOR IFILE");
358 dirp->d_type = LFS_DT_REG;
359 if (reply("FIX") == 1)
360 ret |= ALTERED;
361 }
362 } else if (((dirp->d_ino == ULFS_WINO && (dirp->d_type != LFS_DT_WHT)) ||
363 (dirp->d_ino != ULFS_WINO && dirp->d_type == LFS_DT_WHT))) {
364 fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
365 dirp->d_ino = ULFS_WINO;
366 dirp->d_type = LFS_DT_WHT;
367 if (reply("FIX") == 1)
368 ret |= ALTERED;
369 } else {
370 again:
371 switch (statemap[dirp->d_ino]) {
372 case USTATE:
373 if (idesc->id_entryno <= 2)
374 break;
375 fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
376 n = reply("REMOVE");
377 break;
378
379 case DCLEAR:
380 case FCLEAR:
381 if (idesc->id_entryno <= 2)
382 break;
383 if (statemap[dirp->d_ino] == FCLEAR)
384 errmsg = "DUP/BAD";
385 else if (!preen)
386 errmsg = "ZERO LENGTH DIRECTORY";
387 else {
388 n = 1;
389 break;
390 }
391 fileerror(idesc->id_number, dirp->d_ino, errmsg);
392 if ((n = reply("REMOVE")) == 1)
393 break;
394 dp = ginode(dirp->d_ino);
395 statemap[dirp->d_ino] =
396 (lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR ? DSTATE : FSTATE;
397 lncntp[dirp->d_ino] = lfs_dino_getnlink(fs, dp);
398 goto again;
399
400 case DSTATE:
401 case DFOUND:
402 inp = getinoinfo(dirp->d_ino);
403 if (inp->i_parent != 0 && idesc->id_entryno > 2) {
404 getpathname(pathbuf, sizeof(pathbuf),
405 idesc->id_number, idesc->id_number);
406 getpathname(namebuf, sizeof(namebuf),
407 dirp->d_ino, dirp->d_ino);
408 pwarn("%s %s %s\n", pathbuf,
409 "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
410 namebuf);
411 if (preen)
412 printf(" (IGNORED)\n");
413 else if ((n = reply("REMOVE")) == 1)
414 break;
415 }
416 if (idesc->id_entryno > 2)
417 inp->i_parent = idesc->id_number;
418 /* fall through */
419
420 case FSTATE:
421 if (dirp->d_type != typemap[dirp->d_ino]) {
422 fileerror(idesc->id_number, dirp->d_ino,
423 "BAD TYPE VALUE");
424 if (debug)
425 pwarn("dir has %d, typemap has %d\n",
426 dirp->d_type, typemap[dirp->d_ino]);
427 dirp->d_type = typemap[dirp->d_ino];
428 if (reply("FIX") == 1)
429 ret |= ALTERED;
430 }
431 lncntp[dirp->d_ino]--;
432 break;
433
434 default:
435 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
436 statemap[dirp->d_ino], dirp->d_ino);
437 }
438 }
439 if (n == 0)
440 return (ret | KEEPON);
441 dirp->d_ino = 0;
442 return (ret | KEEPON | ALTERED);
443 }
444 /*
445 * Routine to sort disk blocks.
446 */
447 static int
448 blksort(const void *inpp1, const void *inpp2)
449 {
450 return ((*(const struct inoinfo *const *) inpp1)->i_blks[0] -
451 (*(const struct inoinfo *const *) inpp2)->i_blks[0]);
452 }
453