xref: /freebsd/sbin/fsck_ffs/pass2.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
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 #if 0
35 #ifndef lint
36 static const char sccsid[] = "@(#)pass2.c	8.9 (Berkeley) 4/28/95";
37 #endif /* not lint */
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
47 
48 #include <err.h>
49 #include <stdint.h>
50 #include <string.h>
51 
52 #include "fsck.h"
53 
54 #define MINDIRSIZE	(sizeof (struct dirtemplate))
55 
56 static int blksort(const void *, const void *);
57 static int pass2check(struct inodesc *);
58 
59 void
60 pass2(void)
61 {
62 	union dinode *dp;
63 	struct inoinfo **inpp, *inp;
64 	struct inoinfo **inpend;
65 	struct inodesc curino;
66 	union dinode dino;
67 	int i;
68 	char pathbuf[MAXPATHLEN + 1];
69 
70 	switch (inoinfo(ROOTINO)->ino_state) {
71 
72 	case USTATE:
73 		pfatal("ROOT INODE UNALLOCATED");
74 		if (reply("ALLOCATE") == 0) {
75 			ckfini(0);
76 			exit(EEXIT);
77 		}
78 		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
79 			errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
80 		break;
81 
82 	case DCLEAR:
83 		pfatal("DUPS/BAD IN ROOT INODE");
84 		if (reply("REALLOCATE")) {
85 			freeino(ROOTINO);
86 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
87 				errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
88 			break;
89 		}
90 		if (reply("CONTINUE") == 0) {
91 			ckfini(0);
92 			exit(EEXIT);
93 		}
94 		break;
95 
96 	case FSTATE:
97 	case FCLEAR:
98 		pfatal("ROOT INODE NOT DIRECTORY");
99 		if (reply("REALLOCATE")) {
100 			freeino(ROOTINO);
101 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
102 				errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
103 			break;
104 		}
105 		if (reply("FIX") == 0) {
106 			ckfini(0);
107 			exit(EEXIT);
108 		}
109 		dp = ginode(ROOTINO);
110 		DIP(dp, di_mode) &= ~IFMT;
111 		DIP(dp, di_mode) |= IFDIR;
112 		inodirty();
113 		break;
114 
115 	case DSTATE:
116 		break;
117 
118 	default:
119 		errx(EEXIT, "BAD STATE %d FOR ROOT INODE",
120 		    inoinfo(ROOTINO)->ino_state);
121 	}
122 	inoinfo(ROOTINO)->ino_state = DFOUND;
123 	inoinfo(WINO)->ino_state = FSTATE;
124 	inoinfo(WINO)->ino_type = DT_WHT;
125 	/*
126 	 * Sort the directory list into disk block order.
127 	 */
128 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
129 	/*
130 	 * Check the integrity of each directory.
131 	 */
132 	memset(&curino, 0, sizeof(struct inodesc));
133 	curino.id_type = DATA;
134 	curino.id_func = pass2check;
135 	inpend = &inpsort[inplast];
136 	for (inpp = inpsort; inpp < inpend; inpp++) {
137 		if (got_siginfo) {
138 			printf("%s: phase 2: dir %td of %d (%d%%)\n", cdevname,
139 			    inpp - inpsort, (int)inplast,
140 			    (int)((inpp - inpsort) * 100 / inplast));
141 			got_siginfo = 0;
142 		}
143 		if (got_sigalarm) {
144 			setproctitle("%s p2 %d%%", cdevname,
145 			    (int)((inpp - inpsort) * 100 / inplast));
146 			got_sigalarm = 0;
147 		}
148 		inp = *inpp;
149 		if (inp->i_isize == 0)
150 			continue;
151 		if (inp->i_isize < MINDIRSIZE) {
152 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
153 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
154 			if (reply("FIX") == 1) {
155 				dp = ginode(inp->i_number);
156 				DIP(dp, di_size) = inp->i_isize;
157 				inodirty();
158 			}
159 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
160 			getpathname(pathbuf, inp->i_number, inp->i_number);
161 			if (usedsoftdep)
162 				pfatal("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
163 					"DIRECTORY", pathbuf,
164 					(intmax_t)inp->i_isize, DIRBLKSIZ);
165 			else
166 				pwarn("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
167 					"DIRECTORY", pathbuf,
168 					(intmax_t)inp->i_isize, DIRBLKSIZ);
169 			if (preen)
170 				printf(" (ADJUSTED)\n");
171 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
172 			if (preen || reply("ADJUST") == 1) {
173 				dp = ginode(inp->i_number);
174 				DIP(dp, di_size) =
175 				    roundup(inp->i_isize, DIRBLKSIZ);
176 				inodirty();
177 			}
178 		}
179 		dp = &dino;
180 		memset(dp, 0, sizeof(struct ufs2_dinode));
181 		DIP(dp, di_mode) = IFDIR;
182 		DIP(dp, di_size) = inp->i_isize;
183 		for (i = 0;
184 		     i < (inp->i_numblks<NDADDR ? inp->i_numblks : NDADDR);
185 		     i++)
186 			DIP(dp, di_db[i]) = inp->i_blks[i];
187 		if (inp->i_numblks > NDADDR)
188 			for (i = 0; i < NIADDR; i++)
189 				DIP(dp, di_ib[i]) = inp->i_blks[NDADDR + i];
190 		curino.id_number = inp->i_number;
191 		curino.id_parent = inp->i_parent;
192 		(void)ckinode(dp, &curino);
193 	}
194 	/*
195 	 * Now that the parents of all directories have been found,
196 	 * make another pass to verify the value of `..'
197 	 */
198 	for (inpp = inpsort; inpp < inpend; inpp++) {
199 		inp = *inpp;
200 		if (inp->i_parent == 0 || inp->i_isize == 0)
201 			continue;
202 		if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
203 		    inoinfo(inp->i_number)->ino_state == DSTATE)
204 			inoinfo(inp->i_number)->ino_state = DFOUND;
205 		if (inp->i_dotdot == inp->i_parent ||
206 		    inp->i_dotdot == (ino_t)-1)
207 			continue;
208 		if (inp->i_dotdot == 0) {
209 			inp->i_dotdot = inp->i_parent;
210 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
211 			if (reply("FIX") == 0)
212 				continue;
213 			(void)makeentry(inp->i_number, inp->i_parent, "..");
214 			inoinfo(inp->i_parent)->ino_linkcnt--;
215 			continue;
216 		}
217 		fileerror(inp->i_parent, inp->i_number,
218 		    "BAD INODE NUMBER FOR '..'");
219 		if (reply("FIX") == 0)
220 			continue;
221 		inoinfo(inp->i_dotdot)->ino_linkcnt++;
222 		inoinfo(inp->i_parent)->ino_linkcnt--;
223 		inp->i_dotdot = inp->i_parent;
224 		(void)changeino(inp->i_number, "..", inp->i_parent);
225 	}
226 	/*
227 	 * Mark all the directories that can be found from the root.
228 	 */
229 	propagate();
230 }
231 
232 static int
233 pass2check(struct inodesc *idesc)
234 {
235 	struct direct *dirp = idesc->id_dirp;
236 	struct inoinfo *inp;
237 	int n, entrysize, ret = 0;
238 	union dinode *dp;
239 	const char *errmsg;
240 	struct direct proto;
241 	char namebuf[MAXPATHLEN + 1];
242 	char pathbuf[MAXPATHLEN + 1];
243 
244 	/*
245 	 * check for "."
246 	 */
247 	if (idesc->id_entryno != 0)
248 		goto chk1;
249 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
250 		if (dirp->d_ino != idesc->id_number) {
251 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
252 			dirp->d_ino = idesc->id_number;
253 			if (reply("FIX") == 1)
254 				ret |= ALTERED;
255 		}
256 		if (dirp->d_type != DT_DIR) {
257 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
258 			dirp->d_type = DT_DIR;
259 			if (reply("FIX") == 1)
260 				ret |= ALTERED;
261 		}
262 		goto chk1;
263 	}
264 	direrror(idesc->id_number, "MISSING '.'");
265 	proto.d_ino = idesc->id_number;
266 	proto.d_type = DT_DIR;
267 	proto.d_namlen = 1;
268 	(void)strcpy(proto.d_name, ".");
269 	entrysize = DIRSIZ(0, &proto);
270 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
271 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
272 			dirp->d_name);
273 	} else if (dirp->d_reclen < entrysize) {
274 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
275 	} else if (dirp->d_reclen < 2 * entrysize) {
276 		proto.d_reclen = dirp->d_reclen;
277 		memmove(dirp, &proto, (size_t)entrysize);
278 		if (reply("FIX") == 1)
279 			ret |= ALTERED;
280 	} else {
281 		n = dirp->d_reclen - entrysize;
282 		proto.d_reclen = entrysize;
283 		memmove(dirp, &proto, (size_t)entrysize);
284 		idesc->id_entryno++;
285 		inoinfo(dirp->d_ino)->ino_linkcnt--;
286 		dirp = (struct direct *)((char *)(dirp) + entrysize);
287 		memset(dirp, 0, (size_t)n);
288 		dirp->d_reclen = n;
289 		if (reply("FIX") == 1)
290 			ret |= ALTERED;
291 	}
292 chk1:
293 	if (idesc->id_entryno > 1)
294 		goto chk2;
295 	inp = getinoinfo(idesc->id_number);
296 	proto.d_ino = inp->i_parent;
297 	proto.d_type = DT_DIR;
298 	proto.d_namlen = 2;
299 	(void)strcpy(proto.d_name, "..");
300 	entrysize = DIRSIZ(0, &proto);
301 	if (idesc->id_entryno == 0) {
302 		n = DIRSIZ(0, dirp);
303 		if (dirp->d_reclen < n + entrysize)
304 			goto chk2;
305 		proto.d_reclen = dirp->d_reclen - n;
306 		dirp->d_reclen = n;
307 		idesc->id_entryno++;
308 		inoinfo(dirp->d_ino)->ino_linkcnt--;
309 		dirp = (struct direct *)((char *)(dirp) + n);
310 		memset(dirp, 0, (size_t)proto.d_reclen);
311 		dirp->d_reclen = proto.d_reclen;
312 	}
313 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
314 		inp->i_dotdot = dirp->d_ino;
315 		if (dirp->d_type != DT_DIR) {
316 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
317 			dirp->d_type = DT_DIR;
318 			if (reply("FIX") == 1)
319 				ret |= ALTERED;
320 		}
321 		goto chk2;
322 	}
323 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
324 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
325 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
326 			dirp->d_name);
327 		inp->i_dotdot = (ino_t)-1;
328 	} else if (dirp->d_reclen < entrysize) {
329 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
330 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
331 		inp->i_dotdot = (ino_t)-1;
332 	} else if (inp->i_parent != 0) {
333 		/*
334 		 * We know the parent, so fix now.
335 		 */
336 		inp->i_dotdot = inp->i_parent;
337 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
338 		proto.d_reclen = dirp->d_reclen;
339 		memmove(dirp, &proto, (size_t)entrysize);
340 		if (reply("FIX") == 1)
341 			ret |= ALTERED;
342 	}
343 	idesc->id_entryno++;
344 	if (dirp->d_ino != 0)
345 		inoinfo(dirp->d_ino)->ino_linkcnt--;
346 	return (ret|KEEPON);
347 chk2:
348 	if (dirp->d_ino == 0)
349 		return (ret|KEEPON);
350 	if (dirp->d_namlen <= 2 &&
351 	    dirp->d_name[0] == '.' &&
352 	    idesc->id_entryno >= 2) {
353 		if (dirp->d_namlen == 1) {
354 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
355 			dirp->d_ino = 0;
356 			if (reply("FIX") == 1)
357 				ret |= ALTERED;
358 			return (KEEPON | ret);
359 		}
360 		if (dirp->d_name[1] == '.') {
361 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
362 			dirp->d_ino = 0;
363 			if (reply("FIX") == 1)
364 				ret |= ALTERED;
365 			return (KEEPON | ret);
366 		}
367 	}
368 	idesc->id_entryno++;
369 	n = 0;
370 	if (dirp->d_ino > maxino) {
371 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
372 		n = reply("REMOVE");
373 	} else if (((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
374 		    (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
375 		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
376 		dirp->d_ino = WINO;
377 		dirp->d_type = DT_WHT;
378 		if (reply("FIX") == 1)
379 			ret |= ALTERED;
380 	} else {
381 again:
382 		switch (inoinfo(dirp->d_ino)->ino_state) {
383 		case USTATE:
384 			if (idesc->id_entryno <= 2)
385 				break;
386 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
387 			n = reply("REMOVE");
388 			break;
389 
390 		case DCLEAR:
391 		case FCLEAR:
392 			if (idesc->id_entryno <= 2)
393 				break;
394 			if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
395 				errmsg = "DUP/BAD";
396 			else if (!preen && !usedsoftdep)
397 				errmsg = "ZERO LENGTH DIRECTORY";
398 			else {
399 				n = 1;
400 				break;
401 			}
402 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
403 			if ((n = reply("REMOVE")) == 1)
404 				break;
405 			dp = ginode(dirp->d_ino);
406 			inoinfo(dirp->d_ino)->ino_state =
407 			   (DIP(dp, di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
408 			inoinfo(dirp->d_ino)->ino_linkcnt = DIP(dp, di_nlink);
409 			goto again;
410 
411 		case DSTATE:
412 			if (inoinfo(idesc->id_number)->ino_state == DFOUND)
413 				inoinfo(dirp->d_ino)->ino_state = DFOUND;
414 			/* FALLTHROUGH */
415 
416 		case DFOUND:
417 			inp = getinoinfo(dirp->d_ino);
418 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
419 				getpathname(pathbuf, idesc->id_number,
420 				    idesc->id_number);
421 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
422 				pwarn("%s%s%s %s %s\n", pathbuf,
423 				    (strcmp(pathbuf, "/") == 0 ? "" : "/"),
424 				    dirp->d_name,
425 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
426 				    namebuf);
427 				if (cursnapshot != 0)
428 					break;
429 				if (preen) {
430 					printf(" (REMOVED)\n");
431 					n = 1;
432 					break;
433 				}
434 				if ((n = reply("REMOVE")) == 1)
435 					break;
436 			}
437 			if (idesc->id_entryno > 2)
438 				inp->i_parent = idesc->id_number;
439 			/* FALLTHROUGH */
440 
441 		case FSTATE:
442 			if (dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
443 				fileerror(idesc->id_number, dirp->d_ino,
444 				    "BAD TYPE VALUE");
445 				dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
446 				if (reply("FIX") == 1)
447 					ret |= ALTERED;
448 			}
449 			inoinfo(dirp->d_ino)->ino_linkcnt--;
450 			break;
451 
452 		default:
453 			errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
454 			    inoinfo(dirp->d_ino)->ino_state, dirp->d_ino);
455 		}
456 	}
457 	if (n == 0)
458 		return (ret|KEEPON);
459 	dirp->d_ino = 0;
460 	return (ret|KEEPON|ALTERED);
461 }
462 
463 /*
464  * Routine to sort disk blocks.
465  */
466 static int
467 blksort(const void *arg1, const void *arg2)
468 {
469 
470 	return ((*(struct inoinfo * const *)arg1)->i_blks[0] -
471 		(*(struct inoinfo * const *)arg2)->i_blks[0]);
472 }
473