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