xref: /original-bsd/sbin/fsck/pass2.c (revision 9a765c18)
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.17 (Berkeley) 12/28/90";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <ufs/dinode.h>
14 #include <ufs/fs.h>
15 #define KERNEL
16 #include <ufs/dir.h>
17 #undef KERNEL
18 #include <stdlib.h>
19 #include <string.h>
20 #include "fsck.h"
21 
22 #define MINDIRSIZE	(sizeof (struct dirtemplate))
23 
24 int	pass2check(), blksort();
25 
26 pass2()
27 {
28 	register struct dinode *dp;
29 	register struct inoinfo **inpp, *inp;
30 	struct inoinfo **inpend;
31 	struct inodesc curino;
32 	struct dinode dino;
33 	char pathbuf[MAXPATHLEN + 1];
34 
35 	switch (statemap[ROOTINO]) {
36 
37 	case USTATE:
38 		pfatal("ROOT INODE UNALLOCATED");
39 		if (reply("ALLOCATE") == 0)
40 			errexit("");
41 		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
42 			errexit("CANNOT ALLOCATE ROOT INODE\n");
43 		break;
44 
45 	case DCLEAR:
46 		pfatal("DUPS/BAD IN ROOT INODE");
47 		if (reply("REALLOCATE")) {
48 			freeino(ROOTINO);
49 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
50 				errexit("CANNOT ALLOCATE ROOT INODE\n");
51 			break;
52 		}
53 		if (reply("CONTINUE") == 0)
54 			errexit("");
55 		break;
56 
57 	case FSTATE:
58 	case FCLEAR:
59 		pfatal("ROOT INODE NOT DIRECTORY");
60 		if (reply("REALLOCATE")) {
61 			freeino(ROOTINO);
62 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
63 				errexit("CANNOT ALLOCATE ROOT INODE\n");
64 			break;
65 		}
66 		if (reply("FIX") == 0)
67 			errexit("");
68 		dp = ginode(ROOTINO);
69 		dp->di_mode &= ~IFMT;
70 		dp->di_mode |= IFDIR;
71 		inodirty();
72 		break;
73 
74 	case DSTATE:
75 		break;
76 
77 	default:
78 		errexit("BAD STATE %d FOR ROOT INODE", statemap[ROOTINO]);
79 	}
80 	statemap[ROOTINO] = DFOUND;
81 	/*
82 	 * Sort the directory list into disk block order.
83 	 */
84 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
85 	/*
86 	 * Check the integrity of each directory.
87 	 */
88 	bzero((char *)&curino, sizeof(struct inodesc));
89 	curino.id_type = DATA;
90 	curino.id_func = pass2check;
91 	dino.di_mode = IFDIR;
92 	dp = &dino;
93 	inpend = &inpsort[inplast];
94 	for (inpp = inpsort; inpp < inpend; inpp++) {
95 		inp = *inpp;
96 		if (inp->i_isize == 0)
97 			continue;
98 		if (inp->i_isize < MINDIRSIZE) {
99 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
100 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
101 			if (reply("FIX") == 1) {
102 				dp = ginode(inp->i_number);
103 				dp->di_size = inp->i_isize;
104 				inodirty();
105 				dp = &dino;
106 			}
107 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
108 			getpathname(pathbuf, inp->i_number, inp->i_number);
109 			pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
110 				pathbuf, inp->i_isize, DIRBLKSIZ);
111 			if (preen)
112 				printf(" (ADJUSTED)\n");
113 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
114 			if (preen || reply("ADJUST") == 1) {
115 				dp = ginode(inp->i_number);
116 				dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
117 				inodirty();
118 				dp = &dino;
119 			}
120 		}
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 	 * check for "."
180 	 */
181 	if (idesc->id_entryno != 0)
182 		goto chk1;
183 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
184 		if (dirp->d_ino != idesc->id_number) {
185 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
186 			dirp->d_ino = idesc->id_number;
187 			if (reply("FIX") == 1)
188 				ret |= ALTERED;
189 		}
190 		goto chk1;
191 	}
192 	direrror(idesc->id_number, "MISSING '.'");
193 	proto.d_ino = idesc->id_number;
194 	proto.d_namlen = 1;
195 	(void)strcpy(proto.d_name, ".");
196 	entrysize = DIRSIZ(&proto);
197 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
198 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
199 			dirp->d_name);
200 	} else if (dirp->d_reclen < entrysize) {
201 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
202 	} else if (dirp->d_reclen < 2 * entrysize) {
203 		proto.d_reclen = dirp->d_reclen;
204 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
205 		if (reply("FIX") == 1)
206 			ret |= ALTERED;
207 	} else {
208 		n = dirp->d_reclen - entrysize;
209 		proto.d_reclen = entrysize;
210 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
211 		idesc->id_entryno++;
212 		lncntp[dirp->d_ino]--;
213 		dirp = (struct direct *)((char *)(dirp) + entrysize);
214 		bzero((char *)dirp, (size_t)n);
215 		dirp->d_reclen = n;
216 		if (reply("FIX") == 1)
217 			ret |= ALTERED;
218 	}
219 chk1:
220 	if (idesc->id_entryno > 1)
221 		goto chk2;
222 	inp = getinoinfo(idesc->id_number);
223 	proto.d_ino = inp->i_parent;
224 	proto.d_namlen = 2;
225 	(void)strcpy(proto.d_name, "..");
226 	entrysize = DIRSIZ(&proto);
227 	if (idesc->id_entryno == 0) {
228 		n = DIRSIZ(dirp);
229 		if (dirp->d_reclen < n + entrysize)
230 			goto chk2;
231 		proto.d_reclen = dirp->d_reclen - n;
232 		dirp->d_reclen = n;
233 		idesc->id_entryno++;
234 		lncntp[dirp->d_ino]--;
235 		dirp = (struct direct *)((char *)(dirp) + n);
236 		bzero((char *)dirp, (size_t)proto.d_reclen);
237 		dirp->d_reclen = proto.d_reclen;
238 	}
239 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
240 		inp->i_dotdot = dirp->d_ino;
241 		goto chk2;
242 	}
243 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
244 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
245 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
246 			dirp->d_name);
247 		inp->i_dotdot = (ino_t)-1;
248 	} else if (dirp->d_reclen < entrysize) {
249 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
250 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
251 		inp->i_dotdot = (ino_t)-1;
252 	} else if (inp->i_parent != 0) {
253 		/*
254 		 * We know the parent, so fix now.
255 		 */
256 		inp->i_dotdot = inp->i_parent;
257 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
258 		proto.d_reclen = dirp->d_reclen;
259 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
260 		if (reply("FIX") == 1)
261 			ret |= ALTERED;
262 	}
263 	idesc->id_entryno++;
264 	if (dirp->d_ino != 0)
265 		lncntp[dirp->d_ino]--;
266 	return (ret|KEEPON);
267 chk2:
268 	if (dirp->d_ino == 0)
269 		return (ret|KEEPON);
270 	if (dirp->d_namlen <= 2 &&
271 	    dirp->d_name[0] == '.' &&
272 	    idesc->id_entryno >= 2) {
273 		if (dirp->d_namlen == 1) {
274 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
275 			dirp->d_ino = 0;
276 			if (reply("FIX") == 1)
277 				ret |= ALTERED;
278 			return (KEEPON | ret);
279 		}
280 		if (dirp->d_name[1] == '.') {
281 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
282 			dirp->d_ino = 0;
283 			if (reply("FIX") == 1)
284 				ret |= ALTERED;
285 			return (KEEPON | ret);
286 		}
287 	}
288 	idesc->id_entryno++;
289 	n = 0;
290 	if (dirp->d_ino > maxino) {
291 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
292 		n = reply("REMOVE");
293 	} else {
294 again:
295 		switch (statemap[dirp->d_ino]) {
296 		case USTATE:
297 			if (idesc->id_entryno <= 2)
298 				break;
299 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
300 			n = reply("REMOVE");
301 			break;
302 
303 		case DCLEAR:
304 		case FCLEAR:
305 			if (idesc->id_entryno <= 2)
306 				break;
307 			if (statemap[dirp->d_ino] == DCLEAR)
308 				errmsg = "ZERO LENGTH DIRECTORY";
309 			else
310 				errmsg = "DUP/BAD";
311 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
312 			if ((n = reply("REMOVE")) == 1)
313 				break;
314 			dp = ginode(dirp->d_ino);
315 			statemap[dirp->d_ino] =
316 			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
317 			lncntp[dirp->d_ino] = dp->di_nlink;
318 			goto again;
319 
320 		case DSTATE:
321 			if (statemap[idesc->id_number] == DFOUND)
322 				statemap[dirp->d_ino] = DFOUND;
323 			/* fall through */
324 
325 		case DFOUND:
326 			inp = getinoinfo(dirp->d_ino);
327 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
328 				getpathname(pathbuf, idesc->id_number,
329 				    idesc->id_number);
330 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
331 				pwarn("%s %s %s\n", pathbuf,
332 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
333 				    namebuf);
334 				if (preen)
335 					printf(" (IGNORED)\n");
336 				else if ((n = reply("REMOVE")) == 1)
337 					break;
338 			}
339 			if (idesc->id_entryno > 2)
340 				inp->i_parent = idesc->id_number;
341 			/* fall through */
342 
343 		case FSTATE:
344 			lncntp[dirp->d_ino]--;
345 			break;
346 
347 		default:
348 			errexit("BAD STATE %d FOR INODE I=%d",
349 			    statemap[dirp->d_ino], dirp->d_ino);
350 		}
351 	}
352 	if (n == 0)
353 		return (ret|KEEPON);
354 	dirp->d_ino = 0;
355 	return (ret|KEEPON|ALTERED);
356 }
357 
358 /*
359  * Routine to sort disk blocks.
360  */
361 blksort(inpp1, inpp2)
362 	struct inoinfo **inpp1, **inpp2;
363 {
364 
365 	return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]);
366 }
367