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