xref: /original-bsd/sbin/fsck/pass2.c (revision 602360b2)
1 /*
2  * Copyright (c) 1980, 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)pass2.c	5.21 (Berkeley) 07/12/92";
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 	dino.di_mode = IFDIR;
91 	dp = &dino;
92 	inpend = &inpsort[inplast];
93 	for (inpp = inpsort; inpp < inpend; inpp++) {
94 		inp = *inpp;
95 		if (inp->i_isize == 0)
96 			continue;
97 		if (inp->i_isize < MINDIRSIZE) {
98 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
99 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
100 			if (reply("FIX") == 1) {
101 				dp = ginode(inp->i_number);
102 				dp->di_size = inp->i_isize;
103 				inodirty();
104 				dp = &dino;
105 			}
106 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
107 			getpathname(pathbuf, inp->i_number, inp->i_number);
108 			pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
109 				pathbuf, inp->i_isize, DIRBLKSIZ);
110 			if (preen)
111 				printf(" (ADJUSTED)\n");
112 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
113 			if (preen || reply("ADJUST") == 1) {
114 				dp = ginode(inp->i_number);
115 				dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
116 				inodirty();
117 				dp = &dino;
118 			}
119 		}
120 		dp->di_size = inp->i_isize;
121 		bcopy((char *)&inp->i_blks[0], (char *)&dp->di_db[0],
122 			(size_t)inp->i_numblks);
123 		curino.id_number = inp->i_number;
124 		curino.id_parent = inp->i_parent;
125 		(void)ckinode(dp, &curino);
126 	}
127 	/*
128 	 * Now that the parents of all directories have been found,
129 	 * make another pass to verify the value of `..'
130 	 */
131 	for (inpp = inpsort; inpp < inpend; inpp++) {
132 		inp = *inpp;
133 		if (inp->i_parent == 0 || inp->i_isize == 0)
134 			continue;
135 		if (statemap[inp->i_parent] == DFOUND &&
136 		    statemap[inp->i_number] == DSTATE)
137 			statemap[inp->i_number] = DFOUND;
138 		if (inp->i_dotdot == inp->i_parent ||
139 		    inp->i_dotdot == (ino_t)-1)
140 			continue;
141 		if (inp->i_dotdot == 0) {
142 			inp->i_dotdot = inp->i_parent;
143 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
144 			if (reply("FIX") == 0)
145 				continue;
146 			(void)makeentry(inp->i_number, inp->i_parent, "..");
147 			lncntp[inp->i_parent]--;
148 			continue;
149 		}
150 		fileerror(inp->i_parent, inp->i_number,
151 		    "BAD INODE NUMBER FOR '..'");
152 		if (reply("FIX") == 0)
153 			continue;
154 		lncntp[inp->i_dotdot]++;
155 		lncntp[inp->i_parent]--;
156 		inp->i_dotdot = inp->i_parent;
157 		(void)changeino(inp->i_number, "..", inp->i_parent);
158 	}
159 	/*
160 	 * Mark all the directories that can be found from the root.
161 	 */
162 	propagate();
163 }
164 
165 pass2check(idesc)
166 	struct inodesc *idesc;
167 {
168 	register struct direct *dirp = idesc->id_dirp;
169 	register struct inoinfo *inp;
170 	int n, entrysize, ret = 0;
171 	struct dinode *dp;
172 	char *errmsg;
173 	struct direct proto;
174 	char namebuf[MAXPATHLEN + 1];
175 	char pathbuf[MAXPATHLEN + 1];
176 
177 	/*
178 	 * If converting, set directory entry type.
179 	 */
180 	if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
181 		dirp->d_type = typemap[dirp->d_ino];
182 		ret |= ALTERED;
183 	}
184 	/*
185 	 * check for "."
186 	 */
187 	if (idesc->id_entryno != 0)
188 		goto chk1;
189 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
190 		if (dirp->d_ino != idesc->id_number) {
191 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
192 			dirp->d_ino = idesc->id_number;
193 			if (reply("FIX") == 1)
194 				ret |= ALTERED;
195 		}
196 		if (newinofmt && dirp->d_type != DT_DIR) {
197 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
198 			dirp->d_type = DT_DIR;
199 			if (reply("FIX") == 1)
200 				ret |= ALTERED;
201 		}
202 		goto chk1;
203 	}
204 	direrror(idesc->id_number, "MISSING '.'");
205 	proto.d_ino = idesc->id_number;
206 	if (newinofmt)
207 		proto.d_type = DT_DIR;
208 	else
209 		proto.d_type = 0;
210 	proto.d_namlen = 1;
211 	(void)strcpy(proto.d_name, ".");
212 	entrysize = DIRSIZ(0, &proto);
213 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
214 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
215 			dirp->d_name);
216 	} else if (dirp->d_reclen < entrysize) {
217 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
218 	} else if (dirp->d_reclen < 2 * entrysize) {
219 		proto.d_reclen = dirp->d_reclen;
220 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
221 		if (reply("FIX") == 1)
222 			ret |= ALTERED;
223 	} else {
224 		n = dirp->d_reclen - entrysize;
225 		proto.d_reclen = entrysize;
226 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
227 		idesc->id_entryno++;
228 		lncntp[dirp->d_ino]--;
229 		dirp = (struct direct *)((char *)(dirp) + entrysize);
230 		bzero((char *)dirp, (size_t)n);
231 		dirp->d_reclen = n;
232 		if (reply("FIX") == 1)
233 			ret |= ALTERED;
234 	}
235 chk1:
236 	if (idesc->id_entryno > 1)
237 		goto chk2;
238 	inp = getinoinfo(idesc->id_number);
239 	proto.d_ino = inp->i_parent;
240 	if (newinofmt)
241 		proto.d_type = DT_DIR;
242 	else
243 		proto.d_type = 0;
244 	proto.d_namlen = 2;
245 	(void)strcpy(proto.d_name, "..");
246 	entrysize = DIRSIZ(0, &proto);
247 	if (idesc->id_entryno == 0) {
248 		n = DIRSIZ(0, dirp);
249 		if (dirp->d_reclen < n + entrysize)
250 			goto chk2;
251 		proto.d_reclen = dirp->d_reclen - n;
252 		dirp->d_reclen = n;
253 		idesc->id_entryno++;
254 		lncntp[dirp->d_ino]--;
255 		dirp = (struct direct *)((char *)(dirp) + n);
256 		bzero((char *)dirp, (size_t)proto.d_reclen);
257 		dirp->d_reclen = proto.d_reclen;
258 	}
259 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
260 		inp->i_dotdot = dirp->d_ino;
261 		if (newinofmt && dirp->d_type != DT_DIR) {
262 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
263 			dirp->d_type = DT_DIR;
264 			if (reply("FIX") == 1)
265 				ret |= ALTERED;
266 		}
267 		goto chk2;
268 	}
269 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
270 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
271 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
272 			dirp->d_name);
273 		inp->i_dotdot = (ino_t)-1;
274 	} else if (dirp->d_reclen < entrysize) {
275 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
276 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
277 		inp->i_dotdot = (ino_t)-1;
278 	} else if (inp->i_parent != 0) {
279 		/*
280 		 * We know the parent, so fix now.
281 		 */
282 		inp->i_dotdot = inp->i_parent;
283 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
284 		proto.d_reclen = dirp->d_reclen;
285 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
286 		if (reply("FIX") == 1)
287 			ret |= ALTERED;
288 	}
289 	idesc->id_entryno++;
290 	if (dirp->d_ino != 0)
291 		lncntp[dirp->d_ino]--;
292 	return (ret|KEEPON);
293 chk2:
294 	if (dirp->d_ino == 0)
295 		return (ret|KEEPON);
296 	if (dirp->d_namlen <= 2 &&
297 	    dirp->d_name[0] == '.' &&
298 	    idesc->id_entryno >= 2) {
299 		if (dirp->d_namlen == 1) {
300 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
301 			dirp->d_ino = 0;
302 			if (reply("FIX") == 1)
303 				ret |= ALTERED;
304 			return (KEEPON | ret);
305 		}
306 		if (dirp->d_name[1] == '.') {
307 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
308 			dirp->d_ino = 0;
309 			if (reply("FIX") == 1)
310 				ret |= ALTERED;
311 			return (KEEPON | ret);
312 		}
313 	}
314 	idesc->id_entryno++;
315 	n = 0;
316 	if (dirp->d_ino > maxino) {
317 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
318 		n = reply("REMOVE");
319 	} else {
320 again:
321 		switch (statemap[dirp->d_ino]) {
322 		case USTATE:
323 			if (idesc->id_entryno <= 2)
324 				break;
325 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
326 			n = reply("REMOVE");
327 			break;
328 
329 		case DCLEAR:
330 		case FCLEAR:
331 			if (idesc->id_entryno <= 2)
332 				break;
333 			if (statemap[dirp->d_ino] == DCLEAR)
334 				errmsg = "ZERO LENGTH DIRECTORY";
335 			else
336 				errmsg = "DUP/BAD";
337 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
338 			if ((n = reply("REMOVE")) == 1)
339 				break;
340 			dp = ginode(dirp->d_ino);
341 			statemap[dirp->d_ino] =
342 			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
343 			lncntp[dirp->d_ino] = dp->di_nlink;
344 			goto again;
345 
346 		case DSTATE:
347 			if (statemap[idesc->id_number] == DFOUND)
348 				statemap[dirp->d_ino] = DFOUND;
349 			/* fall through */
350 
351 		case DFOUND:
352 			inp = getinoinfo(dirp->d_ino);
353 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
354 				getpathname(pathbuf, idesc->id_number,
355 				    idesc->id_number);
356 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
357 				pwarn("%s %s %s\n", pathbuf,
358 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
359 				    namebuf);
360 				if (preen)
361 					printf(" (IGNORED)\n");
362 				else if ((n = reply("REMOVE")) == 1)
363 					break;
364 			}
365 			if (idesc->id_entryno > 2)
366 				inp->i_parent = idesc->id_number;
367 			/* fall through */
368 
369 		case FSTATE:
370 			if (newinofmt && dirp->d_type != typemap[dirp->d_ino]) {
371 				fileerror(idesc->id_number, dirp->d_ino,
372 				    "BAD TYPE VALUE");
373 				dirp->d_type = typemap[dirp->d_ino];
374 				if (reply("FIX") == 1)
375 					ret |= ALTERED;
376 			}
377 			lncntp[dirp->d_ino]--;
378 			break;
379 
380 		default:
381 			errexit("BAD STATE %d FOR INODE I=%d",
382 			    statemap[dirp->d_ino], dirp->d_ino);
383 		}
384 	}
385 	if (n == 0)
386 		return (ret|KEEPON);
387 	dirp->d_ino = 0;
388 	return (ret|KEEPON|ALTERED);
389 }
390 
391 /*
392  * Routine to sort disk blocks.
393  */
394 blksort(inpp1, inpp2)
395 	struct inoinfo **inpp1, **inpp2;
396 {
397 
398 	return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]);
399 }
400