xref: /original-bsd/sbin/fsck/pass2.c (revision cf2e4d33)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)pass2.c	8.4 (Berkeley) 08/14/94";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <ufs/ufs/dinode.h>
15 #include <ufs/ufs/dir.h>
16 #include <ufs/ffs/fs.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "fsck.h"
20 
21 #define MINDIRSIZE	(sizeof (struct dirtemplate))
22 
23 int	pass2check(), blksort();
24 
25 pass2()
26 {
27 	register struct dinode *dp;
28 	register struct inoinfo **inpp, *inp;
29 	struct inoinfo **inpend;
30 	struct inodesc curino;
31 	struct dinode dino;
32 	char pathbuf[MAXPATHLEN + 1];
33 
34 	switch (statemap[ROOTINO]) {
35 
36 	case USTATE:
37 		pfatal("ROOT INODE UNALLOCATED");
38 		if (reply("ALLOCATE") == 0)
39 			errexit("");
40 		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
41 			errexit("CANNOT ALLOCATE ROOT INODE\n");
42 		break;
43 
44 	case DCLEAR:
45 		pfatal("DUPS/BAD IN ROOT INODE");
46 		if (reply("REALLOCATE")) {
47 			freeino(ROOTINO);
48 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
49 				errexit("CANNOT ALLOCATE ROOT INODE\n");
50 			break;
51 		}
52 		if (reply("CONTINUE") == 0)
53 			errexit("");
54 		break;
55 
56 	case FSTATE:
57 	case FCLEAR:
58 		pfatal("ROOT INODE NOT DIRECTORY");
59 		if (reply("REALLOCATE")) {
60 			freeino(ROOTINO);
61 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
62 				errexit("CANNOT ALLOCATE ROOT INODE\n");
63 			break;
64 		}
65 		if (reply("FIX") == 0)
66 			errexit("");
67 		dp = ginode(ROOTINO);
68 		dp->di_mode &= ~IFMT;
69 		dp->di_mode |= IFDIR;
70 		inodirty();
71 		break;
72 
73 	case DSTATE:
74 		break;
75 
76 	default:
77 		errexit("BAD STATE %d FOR ROOT INODE", statemap[ROOTINO]);
78 	}
79 	statemap[ROOTINO] = DFOUND;
80 	/*
81 	 * Sort the directory list into disk block order.
82 	 */
83 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
84 	/*
85 	 * Check the integrity of each directory.
86 	 */
87 	bzero((char *)&curino, sizeof(struct inodesc));
88 	curino.id_type = DATA;
89 	curino.id_func = pass2check;
90 	dp = &dino;
91 	inpend = &inpsort[inplast];
92 	for (inpp = inpsort; inpp < inpend; inpp++) {
93 		inp = *inpp;
94 		if (inp->i_isize == 0)
95 			continue;
96 		if (inp->i_isize < MINDIRSIZE) {
97 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
98 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
99 			if (reply("FIX") == 1) {
100 				dp = ginode(inp->i_number);
101 				dp->di_size = inp->i_isize;
102 				inodirty();
103 				dp = &dino;
104 			}
105 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
106 			getpathname(pathbuf, inp->i_number, inp->i_number);
107 			pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
108 				pathbuf, inp->i_isize, DIRBLKSIZ);
109 			if (preen)
110 				printf(" (ADJUSTED)\n");
111 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
112 			if (preen || reply("ADJUST") == 1) {
113 				dp = ginode(inp->i_number);
114 				dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
115 				inodirty();
116 				dp = &dino;
117 			}
118 		}
119 		bzero((char *)&dino, sizeof(struct dinode));
120 		dino.di_mode = IFDIR;
121 		dp->di_size = inp->i_isize;
122 		bcopy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
123 			(size_t)inp->i_numblks);
124 		curino.id_number = inp->i_number;
125 		curino.id_parent = inp->i_parent;
126 		(void)ckinode(dp, &curino);
127 	}
128 	/*
129 	 * Now that the parents of all directories have been found,
130 	 * make another pass to verify the value of `..'
131 	 */
132 	for (inpp = inpsort; inpp < inpend; inpp++) {
133 		inp = *inpp;
134 		if (inp->i_parent == 0 || inp->i_isize == 0)
135 			continue;
136 		if (statemap[inp->i_parent] == DFOUND &&
137 		    statemap[inp->i_number] == DSTATE)
138 			statemap[inp->i_number] = DFOUND;
139 		if (inp->i_dotdot == inp->i_parent ||
140 		    inp->i_dotdot == (ino_t)-1)
141 			continue;
142 		if (inp->i_dotdot == 0) {
143 			inp->i_dotdot = inp->i_parent;
144 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
145 			if (reply("FIX") == 0)
146 				continue;
147 			(void)makeentry(inp->i_number, inp->i_parent, "..");
148 			lncntp[inp->i_parent]--;
149 			continue;
150 		}
151 		fileerror(inp->i_parent, inp->i_number,
152 		    "BAD INODE NUMBER FOR '..'");
153 		if (reply("FIX") == 0)
154 			continue;
155 		lncntp[inp->i_dotdot]++;
156 		lncntp[inp->i_parent]--;
157 		inp->i_dotdot = inp->i_parent;
158 		(void)changeino(inp->i_number, "..", inp->i_parent);
159 	}
160 	/*
161 	 * Mark all the directories that can be found from the root.
162 	 */
163 	propagate();
164 }
165 
166 pass2check(idesc)
167 	struct inodesc *idesc;
168 {
169 	register struct direct *dirp = idesc->id_dirp;
170 	register struct inoinfo *inp;
171 	int n, entrysize, ret = 0;
172 	struct dinode *dp;
173 	char *errmsg;
174 	struct direct proto;
175 	char namebuf[MAXPATHLEN + 1];
176 	char pathbuf[MAXPATHLEN + 1];
177 
178 	/*
179 	 * If converting, set directory entry type.
180 	 */
181 	if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
182 		dirp->d_type = typemap[dirp->d_ino];
183 		ret |= ALTERED;
184 	}
185 	/*
186 	 * check for "."
187 	 */
188 	if (idesc->id_entryno != 0)
189 		goto chk1;
190 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
191 		if (dirp->d_ino != idesc->id_number) {
192 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
193 			dirp->d_ino = idesc->id_number;
194 			if (reply("FIX") == 1)
195 				ret |= ALTERED;
196 		}
197 		if (newinofmt && dirp->d_type != DT_DIR) {
198 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
199 			dirp->d_type = DT_DIR;
200 			if (reply("FIX") == 1)
201 				ret |= ALTERED;
202 		}
203 		goto chk1;
204 	}
205 	direrror(idesc->id_number, "MISSING '.'");
206 	proto.d_ino = idesc->id_number;
207 	if (newinofmt)
208 		proto.d_type = DT_DIR;
209 	else
210 		proto.d_type = 0;
211 	proto.d_namlen = 1;
212 	(void)strcpy(proto.d_name, ".");
213 	entrysize = DIRSIZ(0, &proto);
214 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
215 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
216 			dirp->d_name);
217 	} else if (dirp->d_reclen < entrysize) {
218 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
219 	} else if (dirp->d_reclen < 2 * entrysize) {
220 		proto.d_reclen = dirp->d_reclen;
221 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
222 		if (reply("FIX") == 1)
223 			ret |= ALTERED;
224 	} else {
225 		n = dirp->d_reclen - entrysize;
226 		proto.d_reclen = entrysize;
227 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
228 		idesc->id_entryno++;
229 		lncntp[dirp->d_ino]--;
230 		dirp = (struct direct *)((char *)(dirp) + entrysize);
231 		bzero((char *)dirp, (size_t)n);
232 		dirp->d_reclen = n;
233 		if (reply("FIX") == 1)
234 			ret |= ALTERED;
235 	}
236 chk1:
237 	if (idesc->id_entryno > 1)
238 		goto chk2;
239 	inp = getinoinfo(idesc->id_number);
240 	proto.d_ino = inp->i_parent;
241 	if (newinofmt)
242 		proto.d_type = DT_DIR;
243 	else
244 		proto.d_type = 0;
245 	proto.d_namlen = 2;
246 	(void)strcpy(proto.d_name, "..");
247 	entrysize = DIRSIZ(0, &proto);
248 	if (idesc->id_entryno == 0) {
249 		n = DIRSIZ(0, dirp);
250 		if (dirp->d_reclen < n + entrysize)
251 			goto chk2;
252 		proto.d_reclen = dirp->d_reclen - n;
253 		dirp->d_reclen = n;
254 		idesc->id_entryno++;
255 		lncntp[dirp->d_ino]--;
256 		dirp = (struct direct *)((char *)(dirp) + n);
257 		bzero((char *)dirp, (size_t)proto.d_reclen);
258 		dirp->d_reclen = proto.d_reclen;
259 	}
260 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
261 		inp->i_dotdot = dirp->d_ino;
262 		if (newinofmt && dirp->d_type != DT_DIR) {
263 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
264 			dirp->d_type = DT_DIR;
265 			if (reply("FIX") == 1)
266 				ret |= ALTERED;
267 		}
268 		goto chk2;
269 	}
270 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
271 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
272 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
273 			dirp->d_name);
274 		inp->i_dotdot = (ino_t)-1;
275 	} else if (dirp->d_reclen < entrysize) {
276 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
277 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
278 		inp->i_dotdot = (ino_t)-1;
279 	} else if (inp->i_parent != 0) {
280 		/*
281 		 * We know the parent, so fix now.
282 		 */
283 		inp->i_dotdot = inp->i_parent;
284 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
285 		proto.d_reclen = dirp->d_reclen;
286 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
287 		if (reply("FIX") == 1)
288 			ret |= ALTERED;
289 	}
290 	idesc->id_entryno++;
291 	if (dirp->d_ino != 0)
292 		lncntp[dirp->d_ino]--;
293 	return (ret|KEEPON);
294 chk2:
295 	if (dirp->d_ino == 0)
296 		return (ret|KEEPON);
297 	if (dirp->d_namlen <= 2 &&
298 	    dirp->d_name[0] == '.' &&
299 	    idesc->id_entryno >= 2) {
300 		if (dirp->d_namlen == 1) {
301 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
302 			dirp->d_ino = 0;
303 			if (reply("FIX") == 1)
304 				ret |= ALTERED;
305 			return (KEEPON | ret);
306 		}
307 		if (dirp->d_name[1] == '.') {
308 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
309 			dirp->d_ino = 0;
310 			if (reply("FIX") == 1)
311 				ret |= ALTERED;
312 			return (KEEPON | ret);
313 		}
314 	}
315 	idesc->id_entryno++;
316 	n = 0;
317 	if (dirp->d_ino > maxino) {
318 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
319 		n = reply("REMOVE");
320 	} else if (newinofmt &&
321 		   ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
322 		    (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
323 		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
324 		dirp->d_ino = WINO;
325 		dirp->d_type = DT_WHT;
326 		if (reply("FIX") == 1)
327 			ret |= ALTERED;
328 	} else {
329 again:
330 		switch (statemap[dirp->d_ino]) {
331 		case USTATE:
332 			if (idesc->id_entryno <= 2)
333 				break;
334 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
335 			n = reply("REMOVE");
336 			break;
337 
338 		case DCLEAR:
339 		case FCLEAR:
340 			if (idesc->id_entryno <= 2)
341 				break;
342 			if (statemap[dirp->d_ino] == FCLEAR)
343 				errmsg = "DUP/BAD";
344 			else if (!preen)
345 				errmsg = "ZERO LENGTH DIRECTORY";
346 			else {
347 				n = 1;
348 				break;
349 			}
350 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
351 			if ((n = reply("REMOVE")) == 1)
352 				break;
353 			dp = ginode(dirp->d_ino);
354 			statemap[dirp->d_ino] =
355 			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
356 			lncntp[dirp->d_ino] = dp->di_nlink;
357 			goto again;
358 
359 		case DSTATE:
360 			if (statemap[idesc->id_number] == DFOUND)
361 				statemap[dirp->d_ino] = DFOUND;
362 			/* fall through */
363 
364 		case DFOUND:
365 			inp = getinoinfo(dirp->d_ino);
366 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
367 				getpathname(pathbuf, idesc->id_number,
368 				    idesc->id_number);
369 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
370 				pwarn("%s %s %s\n", pathbuf,
371 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
372 				    namebuf);
373 				if (preen)
374 					printf(" (IGNORED)\n");
375 				else if ((n = reply("REMOVE")) == 1)
376 					break;
377 			}
378 			if (idesc->id_entryno > 2)
379 				inp->i_parent = idesc->id_number;
380 			/* fall through */
381 
382 		case FSTATE:
383 			if (newinofmt && dirp->d_type != typemap[dirp->d_ino]) {
384 				fileerror(idesc->id_number, dirp->d_ino,
385 				    "BAD TYPE VALUE");
386 				dirp->d_type = typemap[dirp->d_ino];
387 				if (reply("FIX") == 1)
388 					ret |= ALTERED;
389 			}
390 			lncntp[dirp->d_ino]--;
391 			break;
392 
393 		default:
394 			errexit("BAD STATE %d FOR INODE I=%d",
395 			    statemap[dirp->d_ino], dirp->d_ino);
396 		}
397 	}
398 	if (n == 0)
399 		return (ret|KEEPON);
400 	dirp->d_ino = 0;
401 	return (ret|KEEPON|ALTERED);
402 }
403 
404 /*
405  * Routine to sort disk blocks.
406  */
407 blksort(inpp1, inpp2)
408 	struct inoinfo **inpp1, **inpp2;
409 {
410 
411 	return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]);
412 }
413