xref: /openbsd-src/sbin/fsck_ext2fs/pass2.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: pass2.c,v 1.4 2000/04/26 23:26:06 jasoni Exp $	*/
2 /*	$NetBSD: pass2.c,v 1.1 1997/06/11 11:21:53 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1980, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)pass2.c	8.6 (Berkeley) 10/27/94";
41 #else
42 #if 0
43 static char rcsid[] = "$NetBSD: pass2.c,v 1.1 1997/06/11 11:21:53 bouyer Exp $";
44 #else
45 static char rcsid[] = "$OpenBSD: pass2.c,v 1.4 2000/04/26 23:26:06 jasoni Exp $";
46 #endif
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <ufs/ext2fs/ext2fs_dinode.h>
53 #include <ufs/ext2fs/ext2fs_dir.h>
54 #include <ufs/ext2fs/ext2fs.h>
55 
56 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 
62 #include "fsck.h"
63 #include "fsutil.h"
64 #include "extern.h"
65 
66 #define MINDIRSIZE	(sizeof (struct ext2fs_dirtemplate))
67 
68 static int pass2check __P((struct inodesc *));
69 static int blksort __P((const void *, const void *));
70 
71 void
72 pass2()
73 {
74 	register struct ext2fs_dinode *dp;
75 	register struct inoinfo **inpp, *inp;
76 	struct inoinfo **inpend;
77 	struct inodesc curino;
78 	struct ext2fs_dinode dino;
79 	char pathbuf[MAXPATHLEN + 1];
80 
81 	switch (statemap[EXT2_ROOTINO]) {
82 
83 	case USTATE:
84 		pfatal("ROOT INODE UNALLOCATED");
85 		if (reply("ALLOCATE") == 0)
86 			errexit("%s\n", "");
87 		if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
88 			errexit("CANNOT ALLOCATE ROOT INODE\n");
89 		break;
90 
91 	case DCLEAR:
92 		pfatal("DUPS/BAD IN ROOT INODE");
93 		if (reply("REALLOCATE")) {
94 			freeino(EXT2_ROOTINO);
95 			if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
96 				errexit("CANNOT ALLOCATE ROOT INODE\n");
97 			break;
98 		}
99 		if (reply("CONTINUE") == 0)
100 			errexit("%s\n", "");
101 		break;
102 
103 	case FSTATE:
104 	case FCLEAR:
105 		pfatal("ROOT INODE NOT DIRECTORY");
106 		if (reply("REALLOCATE")) {
107 			freeino(EXT2_ROOTINO);
108 			if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
109 				errexit("CANNOT ALLOCATE ROOT INODE\n");
110 			break;
111 		}
112 		if (reply("FIX") == 0)
113 			errexit("%s\n", "");
114 		dp = ginode(EXT2_ROOTINO);
115 		dp->e2di_mode = h2fs16((fs2h16(dp->e2di_mode) & ~IFMT) | IFDIR);
116 		inodirty();
117 		break;
118 
119 	case DSTATE:
120 		break;
121 
122 	default:
123 		errexit("BAD STATE %d FOR ROOT INODE\n", statemap[EXT2_ROOTINO]);
124 	}
125 
126 	/*
127 	 * Sort the directory list into disk block order.
128 	 */
129 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
130 	/*
131 	 * Check the integrity of each directory.
132 	 */
133 	memset(&curino, 0, sizeof(struct inodesc));
134 	curino.id_type = DATA;
135 	curino.id_func = pass2check;
136 	inpend = &inpsort[inplast];
137 	for (inpp = inpsort; inpp < inpend; inpp++) {
138 		inp = *inpp;
139 		if (inp->i_isize == 0)
140 			continue;
141 		if (inp->i_isize < MINDIRSIZE) {
142 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
143 			inp->i_isize = roundup(MINDIRSIZE, sblock.e2fs_bsize);
144 			if (reply("FIX") == 1) {
145 				dp = ginode(inp->i_number);
146 				dp->e2di_size = h2fs32(inp->i_isize);
147 				inodirty();
148 			}
149 		} else if ((inp->i_isize & (sblock.e2fs_bsize - 1)) != 0) {
150 			getpathname(pathbuf, inp->i_number, inp->i_number);
151 			pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
152 			    pathbuf, (u_long)inp->i_isize, sblock.e2fs_bsize);
153 			if (preen)
154 				printf(" (ADJUSTED)\n");
155 			inp->i_isize = roundup(inp->i_isize, sblock.e2fs_bsize);
156 			if (preen || reply("ADJUST") == 1) {
157 				dp = ginode(inp->i_number);
158 				dp->e2di_size = h2fs32(inp->i_isize);
159 				inodirty();
160 			}
161 		}
162 		memset(&dino, 0, sizeof(struct ext2fs_dinode));
163 		dino.e2di_mode = h2fs16(IFDIR);
164 		dino.e2di_size = h2fs32(inp->i_isize);
165 		memcpy(&dino.e2di_blocks[0], &inp->i_blks[0], (size_t)inp->i_numblks);
166 		curino.id_number = inp->i_number;
167 		curino.id_parent = inp->i_parent;
168 		(void)ckinode(&dino, &curino);
169 	}
170 	/*
171 	 * Now that the parents of all directories have been found,
172 	 * make another pass to verify the value of `..'
173 	 */
174 	for (inpp = inpsort; inpp < inpend; inpp++) {
175 		inp = *inpp;
176 		if (inp->i_parent == 0 || inp->i_isize == 0)
177 			continue;
178 		if (inp->i_dotdot == inp->i_parent ||
179 		    inp->i_dotdot == (ino_t)-1)
180 			continue;
181 		if (inp->i_dotdot == 0) {
182 			inp->i_dotdot = inp->i_parent;
183 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
184 			if (reply("FIX") == 0)
185 				continue;
186 			(void)makeentry(inp->i_number, inp->i_parent, "..");
187 			lncntp[inp->i_parent]--;
188 			continue;
189 		}
190 		fileerror(inp->i_parent, inp->i_number,
191 		    "BAD INODE NUMBER FOR '..'");
192 		if (reply("FIX") == 0)
193 			continue;
194 		lncntp[inp->i_dotdot]++;
195 		lncntp[inp->i_parent]--;
196 		inp->i_dotdot = inp->i_parent;
197 		(void)changeino(inp->i_number, "..", inp->i_parent);
198 	}
199 	/*
200 	 * Mark all the directories that can be found from the root.
201 	 */
202 	propagate();
203 }
204 
205 static int
206 pass2check(idesc)
207 	struct inodesc *idesc;
208 {
209 	register struct ext2fs_direct *dirp = idesc->id_dirp;
210 	register struct inoinfo *inp;
211 	int n, entrysize, ret = 0;
212 	struct ext2fs_dinode *dp;
213 	char *errmsg;
214 	struct ext2fs_direct proto;
215 	char namebuf[MAXPATHLEN + 1];
216 	char pathbuf[MAXPATHLEN + 1];
217 
218 	/*
219 	 * check for "."
220 	 */
221 	if (idesc->id_entryno != 0)
222 		goto chk1;
223 	if (fs2h32(dirp->e2d_ino) != 0 && dirp->e2d_namlen == 1 &&
224 		dirp->e2d_name[0] == '.') {
225 		if (fs2h32(dirp->e2d_ino) != idesc->id_number) {
226 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
227 			dirp->e2d_ino = h2fs32(idesc->id_number);
228 			if (reply("FIX") == 1)
229 				ret |= ALTERED;
230 		}
231 		if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
232 		    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
233 		    && (dirp->e2d_type != EXT2_FT_DIR)) {
234 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
235 			dirp->e2d_type = EXT2_FT_DIR;
236 			if (reply("FIX") == 1)
237 				ret |= ALTERED;
238 		}
239 		goto chk1;
240 	}
241 	direrror(idesc->id_number, "MISSING '.'");
242 	proto.e2d_ino = h2fs32(idesc->id_number);
243 	proto.e2d_namlen = 1;
244 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
245 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
246 		proto.e2d_type = EXT2_FT_DIR;
247 	else
248 		proto.e2d_type = 0;
249 	(void)strcpy(proto.e2d_name, ".");
250 	entrysize = EXT2FS_DIRSIZ(proto.e2d_namlen);
251 	if (fs2h32(dirp->e2d_ino) != 0 && strcmp(dirp->e2d_name, "..") != 0) {
252 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
253 			dirp->e2d_name);
254 	} else if (fs2h16(dirp->e2d_reclen) < entrysize) {
255 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
256 	} else if (fs2h16(dirp->e2d_reclen) < 2 * entrysize) {
257 		proto.e2d_reclen = dirp->e2d_reclen;
258 		memcpy(dirp, &proto, (size_t)entrysize);
259 		if (reply("FIX") == 1)
260 			ret |= ALTERED;
261 	} else {
262 		n = fs2h16(dirp->e2d_reclen) - entrysize;
263 		proto.e2d_reclen = h2fs16(entrysize);
264 		memcpy(dirp, &proto, (size_t)entrysize);
265 		idesc->id_entryno++;
266 		lncntp[fs2h32(dirp->e2d_ino)]--;
267 		dirp = (struct ext2fs_direct *)((char *)(dirp) + entrysize);
268 		memset(dirp, 0, (size_t)n);
269 		dirp->e2d_reclen = h2fs16(n);
270 		if (reply("FIX") == 1)
271 			ret |= ALTERED;
272 	}
273 chk1:
274 	if (idesc->id_entryno > 1)
275 		goto chk2;
276 	inp = getinoinfo(idesc->id_number);
277 	proto.e2d_ino = h2fs32(inp->i_parent);
278 	proto.e2d_namlen = 2;
279 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
280 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
281 		proto.e2d_type = EXT2_FT_DIR;
282 	else
283 		proto.e2d_type = 0;
284 	(void)strcpy(proto.e2d_name, "..");
285 	entrysize = EXT2FS_DIRSIZ(2);
286 	if (idesc->id_entryno == 0) {
287 		n = EXT2FS_DIRSIZ(dirp->e2d_namlen);
288 		if (fs2h16(dirp->e2d_reclen) < n + entrysize)
289 			goto chk2;
290 		proto.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - n);
291 		dirp->e2d_reclen = h2fs16(n);
292 		idesc->id_entryno++;
293 		lncntp[fs2h32(dirp->e2d_ino)]--;
294 		dirp = (struct ext2fs_direct *)((char *)(dirp) + n);
295 		memset(dirp, 0, (size_t)fs2h16(proto.e2d_reclen));
296 		dirp->e2d_reclen = proto.e2d_reclen;
297 	}
298 	if (fs2h32(dirp->e2d_ino) != 0 &&
299 	    dirp->e2d_namlen == 2 &&
300 	    strncmp(dirp->e2d_name, "..", 2) == 0) {
301 		inp->i_dotdot = fs2h32(dirp->e2d_ino);
302 		if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
303 		    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
304 		    && dirp->e2d_type != EXT2_FT_DIR) {
305 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
306 			dirp->e2d_type = EXT2_FT_DIR;
307 			if (reply("FIX") == 1)
308 				ret |= ALTERED;
309 		}
310 		goto chk2;
311 	}
312 	if (fs2h32(dirp->e2d_ino) != 0 &&
313 		dirp->e2d_namlen == 1 &&
314 		strncmp(dirp->e2d_name, ".", 1) != 0) {
315 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
316 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
317 			dirp->e2d_name);
318 		inp->i_dotdot = (ino_t)-1;
319 	} else if (fs2h16(dirp->e2d_reclen) < entrysize) {
320 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
321 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
322 		inp->i_dotdot = (ino_t)-1;
323 	} else if (inp->i_parent != 0) {
324 		/*
325 		 * We know the parent, so fix now.
326 		 */
327 		inp->i_dotdot = inp->i_parent;
328 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
329 		proto.e2d_reclen = dirp->e2d_reclen;
330 		memcpy(dirp, &proto, (size_t)entrysize);
331 		if (reply("FIX") == 1)
332 			ret |= ALTERED;
333 	}
334 	idesc->id_entryno++;
335 	if (fs2h32(dirp->e2d_ino) != 0)
336 		lncntp[fs2h32(dirp->e2d_ino)]--;
337 	return (ret|KEEPON);
338 chk2:
339 	if (fs2h32(dirp->e2d_ino) == 0)
340 		return (ret|KEEPON);
341 	if (dirp->e2d_namlen <= 2 &&
342 	    dirp->e2d_name[0] == '.' &&
343 	    idesc->id_entryno >= 2) {
344 		if (dirp->e2d_namlen == 1) {
345 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
346 			dirp->e2d_ino = 0;
347 			if (reply("FIX") == 1)
348 				ret |= ALTERED;
349 			return (KEEPON | ret);
350 		}
351 		if (dirp->e2d_name[1] == '.') {
352 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
353 			dirp->e2d_ino = 0;
354 			if (reply("FIX") == 1)
355 				ret |= ALTERED;
356 			return (KEEPON | ret);
357 		}
358 	}
359 	idesc->id_entryno++;
360 	n = 0;
361 	if (fs2h32(dirp->e2d_ino) > maxino ||
362 		(fs2h32(dirp->e2d_ino) < EXT2_FIRSTINO &&
363 		 fs2h32(dirp->e2d_ino) != EXT2_ROOTINO)) {
364 		fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "I OUT OF RANGE");
365 		n = reply("REMOVE");
366 	} else {
367 again:
368 		switch (statemap[fs2h32(dirp->e2d_ino)]) {
369 		case USTATE:
370 			if (idesc->id_entryno <= 2)
371 				break;
372 			fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "UNALLOCATED");
373 			n = reply("REMOVE");
374 			break;
375 
376 		case DCLEAR:
377 		case FCLEAR:
378 			if (idesc->id_entryno <= 2)
379 				break;
380 			if (statemap[fs2h32(dirp->e2d_ino)] == FCLEAR)
381 				errmsg = "DUP/BAD";
382 			else if (!preen)
383 				errmsg = "ZERO LENGTH DIRECTORY";
384 			else {
385 				n = 1;
386 				break;
387 			}
388 			fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), errmsg);
389 			if ((n = reply("REMOVE")) == 1)
390 				break;
391 			dp = ginode(fs2h32(dirp->e2d_ino));
392 			statemap[fs2h32(dirp->e2d_ino)] =
393 			    (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
394 			lncntp[fs2h32(dirp->e2d_ino)] = fs2h16(dp->e2di_nlink);
395 			goto again;
396 
397 		case DSTATE:
398 		case DFOUND:
399 			inp = getinoinfo(fs2h32(dirp->e2d_ino));
400 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
401 				getpathname(pathbuf, idesc->id_number,
402 				    idesc->id_number);
403 				getpathname(namebuf, fs2h32(dirp->e2d_ino),
404 					fs2h32(dirp->e2d_ino));
405 				pwarn("%s %s %s\n", pathbuf,
406 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
407 				    namebuf);
408 				if (preen)
409 					printf(" (IGNORED)\n");
410 				else if ((n = reply("REMOVE")) == 1)
411 					break;
412 			}
413 			if (idesc->id_entryno > 2)
414 				inp->i_parent = idesc->id_number;
415 			/* fall through */
416 
417 		case FSTATE:
418 			if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
419 			    (sblock.e2fs.e2fs_features_incompat &
420 				EXT2F_INCOMPAT_FTYPE) &&
421 			    dirp->e2d_type !=
422 				inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)])) {
423 				dirp->e2d_type =
424 				    inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)]);
425 				fileerror(idesc->id_number,
426 				    fs2h32(dirp->e2d_ino),
427 				    "BAD TYPE VALUE");
428 				if (reply("FIX") == 1)
429 					ret |= ALTERED;
430 			}
431 			lncntp[fs2h32(dirp->e2d_ino)]--;
432 			break;
433 
434 		default:
435 			errexit("BAD STATE %d FOR INODE I=%d\n",
436 			    statemap[fs2h32(dirp->e2d_ino)], fs2h32(dirp->e2d_ino));
437 		}
438 	}
439 	if (n == 0)
440 		return (ret|KEEPON);
441 	dirp->e2d_ino = 0;
442 	return (ret|KEEPON|ALTERED);
443 }
444 
445 /*
446  * Routine to sort disk blocks.
447  */
448 static int
449 blksort(inpp1, inpp2)
450 	const void *inpp1, *inpp2;
451 {
452 	return ((* (struct inoinfo **) inpp1)->i_blks[0] -
453 		(* (struct inoinfo **) inpp2)->i_blks[0]);
454 }
455