pass2.c revision 70050
178344Sobrien/*
278344Sobrien * Copyright (c) 1980, 1986, 1993
398184Sgordon *	The Regents of the University of California.  All rights reserved.
478344Sobrien *
578344Sobrien * Redistribution and use in source and binary forms, with or without
678344Sobrien * modification, are permitted provided that the following conditions
7208307Sdougb * are met:
8215824Sdougb * 1. Redistributions of source code must retain the above copyright
978344Sobrien *    notice, this list of conditions and the following disclaimer.
1078344Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1178344Sobrien *    notice, this list of conditions and the following disclaimer in the
1278344Sobrien *    documentation and/or other materials provided with the distribution.
1378344Sobrien * 3. All advertising materials mentioning features or use of this software
14215824Sdougb *    must display the following acknowledgement:
1578344Sobrien *	This product includes software developed by the University of
1678344Sobrien *	California, Berkeley and its contributors.
1778344Sobrien * 4. Neither the name of the University nor the names of its contributors
18176873Smtm *    may be used to endorse or promote products derived from this software
19176873Smtm *    without specific prior written permission.
20124618Smtm *
21124618Smtm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22124618Smtm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23124618Smtm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24124618Smtm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25124618Smtm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26124618Smtm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27124618Smtm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28124618Smtm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2978344Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30124618Smtm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31197947Sdougb * SUCH DAMAGE.
32124618Smtm */
33124618Smtm
34124618Smtm#ifndef lint
35124618Smtm#if 0
36124618Smtmstatic const char sccsid[] = "@(#)pass2.c	8.9 (Berkeley) 4/28/95";
37124618Smtm#endif
38124618Smtmstatic const char rcsid[] =
39176873Smtm  "$FreeBSD: head/sbin/fsck_ffs/pass2.c 70050 2000-12-15 14:23:55Z iedowse $";
40197947Sdougb#endif /* not lint */
4198184Sgordon
42176873Smtm#include <sys/param.h>
43124618Smtm
4498184Sgordon#include <ufs/ufs/dinode.h>
45124618Smtm#include <ufs/ufs/dir.h>
46124618Smtm
47124618Smtm#include <err.h>
48169668Smtm#include <string.h>
4998184Sgordon
5098184Sgordon#include "fsck.h"
5178344Sobrien
5278344Sobrien#define MINDIRSIZE	(sizeof (struct dirtemplate))
5378344Sobrien
5478344Sobrienstatic int blksort __P((const void *, const void *));
55static int pass2check __P((struct inodesc *));
56
57void
58pass2()
59{
60	register struct dinode *dp;
61	register struct inoinfo **inpp, *inp;
62	struct inoinfo **inpend;
63	struct inodesc curino;
64	struct dinode dino;
65	char pathbuf[MAXPATHLEN + 1];
66
67	switch (inoinfo(ROOTINO)->ino_state) {
68
69	case USTATE:
70		pfatal("ROOT INODE UNALLOCATED");
71		if (reply("ALLOCATE") == 0) {
72			ckfini(0);
73			exit(EEXIT);
74		}
75		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
76			errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
77		break;
78
79	case DCLEAR:
80		pfatal("DUPS/BAD IN ROOT INODE");
81		if (reply("REALLOCATE")) {
82			freeino(ROOTINO);
83			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
84				errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
85			break;
86		}
87		if (reply("CONTINUE") == 0) {
88			ckfini(0);
89			exit(EEXIT);
90		}
91		break;
92
93	case FSTATE:
94	case FCLEAR:
95		pfatal("ROOT INODE NOT DIRECTORY");
96		if (reply("REALLOCATE")) {
97			freeino(ROOTINO);
98			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
99				errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
100			break;
101		}
102		if (reply("FIX") == 0) {
103			ckfini(0);
104			exit(EEXIT);
105		}
106		dp = ginode(ROOTINO);
107		dp->di_mode &= ~IFMT;
108		dp->di_mode |= IFDIR;
109		inodirty();
110		break;
111
112	case DSTATE:
113		break;
114
115	default:
116		errx(EEXIT, "BAD STATE %d FOR ROOT INODE",
117		    inoinfo(ROOTINO)->ino_state);
118	}
119	inoinfo(ROOTINO)->ino_state = DFOUND;
120	if (newinofmt) {
121		inoinfo(WINO)->ino_state = FSTATE;
122		inoinfo(WINO)->ino_type = DT_WHT;
123	}
124	/*
125	 * Sort the directory list into disk block order.
126	 */
127	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
128	/*
129	 * Check the integrity of each directory.
130	 */
131	memset(&curino, 0, sizeof(struct inodesc));
132	curino.id_type = DATA;
133	curino.id_func = pass2check;
134	dp = &dino;
135	inpend = &inpsort[inplast];
136	for (inpp = inpsort; inpp < inpend; inpp++) {
137		if (got_siginfo) {
138			printf("%s: phase 2: dir %d of %d (%d%%)\n", cdevname,
139			    inpp - inpsort, inplast, (inpp - inpsort) * 100 /
140			    inplast);
141			got_siginfo = 0;
142		}
143		inp = *inpp;
144		if (inp->i_isize == 0)
145			continue;
146		if (inp->i_isize < MINDIRSIZE) {
147			direrror(inp->i_number, "DIRECTORY TOO SHORT");
148			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
149			if (reply("FIX") == 1) {
150				dp = ginode(inp->i_number);
151				dp->di_size = inp->i_isize;
152				inodirty();
153				dp = &dino;
154			}
155		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
156			getpathname(pathbuf, inp->i_number, inp->i_number);
157			if (usedsoftdep)
158				pfatal("%s %s: LENGTH %d NOT MULTIPLE OF %d",
159					"DIRECTORY", pathbuf, inp->i_isize,
160					DIRBLKSIZ);
161			else
162				pwarn("%s %s: LENGTH %d NOT MULTIPLE OF %d",
163					"DIRECTORY", pathbuf, inp->i_isize,
164					DIRBLKSIZ);
165			if (preen)
166				printf(" (ADJUSTED)\n");
167			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
168			if (preen || reply("ADJUST") == 1) {
169				dp = ginode(inp->i_number);
170				dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
171				inodirty();
172				dp = &dino;
173			}
174		}
175		memset(&dino, 0, sizeof(struct dinode));
176		dino.di_mode = IFDIR;
177		dp->di_size = inp->i_isize;
178		memmove(&dp->di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks);
179		curino.id_number = inp->i_number;
180		curino.id_parent = inp->i_parent;
181		(void)ckinode(dp, &curino);
182	}
183	/*
184	 * Now that the parents of all directories have been found,
185	 * make another pass to verify the value of `..'
186	 */
187	for (inpp = inpsort; inpp < inpend; inpp++) {
188		inp = *inpp;
189		if (inp->i_parent == 0 || inp->i_isize == 0)
190			continue;
191		if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
192		    inoinfo(inp->i_number)->ino_state == DSTATE)
193			inoinfo(inp->i_number)->ino_state = DFOUND;
194		if (inp->i_dotdot == inp->i_parent ||
195		    inp->i_dotdot == (ino_t)-1)
196			continue;
197		if (inp->i_dotdot == 0) {
198			inp->i_dotdot = inp->i_parent;
199			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
200			if (reply("FIX") == 0)
201				continue;
202			(void)makeentry(inp->i_number, inp->i_parent, "..");
203			inoinfo(inp->i_parent)->ino_linkcnt--;
204			continue;
205		}
206		fileerror(inp->i_parent, inp->i_number,
207		    "BAD INODE NUMBER FOR '..'");
208		if (reply("FIX") == 0)
209			continue;
210		inoinfo(inp->i_dotdot)->ino_linkcnt++;
211		inoinfo(inp->i_parent)->ino_linkcnt--;
212		inp->i_dotdot = inp->i_parent;
213		(void)changeino(inp->i_number, "..", inp->i_parent);
214	}
215	/*
216	 * Mark all the directories that can be found from the root.
217	 */
218	propagate();
219}
220
221static int
222pass2check(idesc)
223	struct inodesc *idesc;
224{
225	register struct direct *dirp = idesc->id_dirp;
226	register struct inoinfo *inp;
227	int n, entrysize, ret = 0;
228	struct dinode *dp;
229	char *errmsg;
230	struct direct proto;
231	char namebuf[MAXPATHLEN + 1];
232	char pathbuf[MAXPATHLEN + 1];
233
234	/*
235	 * If converting, set directory entry type.
236	 */
237	if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
238		dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
239		ret |= ALTERED;
240	}
241	/*
242	 * check for "."
243	 */
244	if (idesc->id_entryno != 0)
245		goto chk1;
246	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
247		if (dirp->d_ino != idesc->id_number) {
248			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
249			dirp->d_ino = idesc->id_number;
250			if (reply("FIX") == 1)
251				ret |= ALTERED;
252		}
253		if (newinofmt && dirp->d_type != DT_DIR) {
254			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
255			dirp->d_type = DT_DIR;
256			if (reply("FIX") == 1)
257				ret |= ALTERED;
258		}
259		goto chk1;
260	}
261	direrror(idesc->id_number, "MISSING '.'");
262	proto.d_ino = idesc->id_number;
263	if (newinofmt)
264		proto.d_type = DT_DIR;
265	else
266		proto.d_type = 0;
267	proto.d_namlen = 1;
268	(void)strcpy(proto.d_name, ".");
269#	if BYTE_ORDER == LITTLE_ENDIAN
270		if (!newinofmt) {
271			u_char tmp;
272
273			tmp = proto.d_type;
274			proto.d_type = proto.d_namlen;
275			proto.d_namlen = tmp;
276		}
277#	endif
278	entrysize = DIRSIZ(0, &proto);
279	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
280		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
281			dirp->d_name);
282	} else if (dirp->d_reclen < entrysize) {
283		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
284	} else if (dirp->d_reclen < 2 * entrysize) {
285		proto.d_reclen = dirp->d_reclen;
286		memmove(dirp, &proto, (size_t)entrysize);
287		if (reply("FIX") == 1)
288			ret |= ALTERED;
289	} else {
290		n = dirp->d_reclen - entrysize;
291		proto.d_reclen = entrysize;
292		memmove(dirp, &proto, (size_t)entrysize);
293		idesc->id_entryno++;
294		inoinfo(dirp->d_ino)->ino_linkcnt--;
295		dirp = (struct direct *)((char *)(dirp) + entrysize);
296		memset(dirp, 0, (size_t)n);
297		dirp->d_reclen = n;
298		if (reply("FIX") == 1)
299			ret |= ALTERED;
300	}
301chk1:
302	if (idesc->id_entryno > 1)
303		goto chk2;
304	inp = getinoinfo(idesc->id_number);
305	proto.d_ino = inp->i_parent;
306	if (newinofmt)
307		proto.d_type = DT_DIR;
308	else
309		proto.d_type = 0;
310	proto.d_namlen = 2;
311	(void)strcpy(proto.d_name, "..");
312#	if BYTE_ORDER == LITTLE_ENDIAN
313		if (!newinofmt) {
314			u_char tmp;
315
316			tmp = proto.d_type;
317			proto.d_type = proto.d_namlen;
318			proto.d_namlen = tmp;
319		}
320#	endif
321	entrysize = DIRSIZ(0, &proto);
322	if (idesc->id_entryno == 0) {
323		n = DIRSIZ(0, dirp);
324		if (dirp->d_reclen < n + entrysize)
325			goto chk2;
326		proto.d_reclen = dirp->d_reclen - n;
327		dirp->d_reclen = n;
328		idesc->id_entryno++;
329		inoinfo(dirp->d_ino)->ino_linkcnt--;
330		dirp = (struct direct *)((char *)(dirp) + n);
331		memset(dirp, 0, (size_t)proto.d_reclen);
332		dirp->d_reclen = proto.d_reclen;
333	}
334	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
335		inp->i_dotdot = dirp->d_ino;
336		if (newinofmt && dirp->d_type != DT_DIR) {
337			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
338			dirp->d_type = DT_DIR;
339			if (reply("FIX") == 1)
340				ret |= ALTERED;
341		}
342		goto chk2;
343	}
344	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
345		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
346		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
347			dirp->d_name);
348		inp->i_dotdot = (ino_t)-1;
349	} else if (dirp->d_reclen < entrysize) {
350		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
351		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
352		inp->i_dotdot = (ino_t)-1;
353	} else if (inp->i_parent != 0) {
354		/*
355		 * We know the parent, so fix now.
356		 */
357		inp->i_dotdot = inp->i_parent;
358		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
359		proto.d_reclen = dirp->d_reclen;
360		memmove(dirp, &proto, (size_t)entrysize);
361		if (reply("FIX") == 1)
362			ret |= ALTERED;
363	}
364	idesc->id_entryno++;
365	if (dirp->d_ino != 0)
366		inoinfo(dirp->d_ino)->ino_linkcnt--;
367	return (ret|KEEPON);
368chk2:
369	if (dirp->d_ino == 0)
370		return (ret|KEEPON);
371	if (dirp->d_namlen <= 2 &&
372	    dirp->d_name[0] == '.' &&
373	    idesc->id_entryno >= 2) {
374		if (dirp->d_namlen == 1) {
375			direrror(idesc->id_number, "EXTRA '.' ENTRY");
376			dirp->d_ino = 0;
377			if (reply("FIX") == 1)
378				ret |= ALTERED;
379			return (KEEPON | ret);
380		}
381		if (dirp->d_name[1] == '.') {
382			direrror(idesc->id_number, "EXTRA '..' ENTRY");
383			dirp->d_ino = 0;
384			if (reply("FIX") == 1)
385				ret |= ALTERED;
386			return (KEEPON | ret);
387		}
388	}
389	idesc->id_entryno++;
390	n = 0;
391	if (dirp->d_ino > maxino) {
392		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
393		n = reply("REMOVE");
394	} else if (newinofmt &&
395		   ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
396		    (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
397		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
398		dirp->d_ino = WINO;
399		dirp->d_type = DT_WHT;
400		if (reply("FIX") == 1)
401			ret |= ALTERED;
402	} else {
403again:
404		switch (inoinfo(dirp->d_ino)->ino_state) {
405		case USTATE:
406			if (idesc->id_entryno <= 2)
407				break;
408			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
409			n = reply("REMOVE");
410			break;
411
412		case DCLEAR:
413		case FCLEAR:
414			if (idesc->id_entryno <= 2)
415				break;
416			if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
417				errmsg = "DUP/BAD";
418			else if (!preen && !usedsoftdep)
419				errmsg = "ZERO LENGTH DIRECTORY";
420			else {
421				n = 1;
422				break;
423			}
424			fileerror(idesc->id_number, dirp->d_ino, errmsg);
425			if ((n = reply("REMOVE")) == 1)
426				break;
427			dp = ginode(dirp->d_ino);
428			inoinfo(dirp->d_ino)->ino_state =
429			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
430			inoinfo(dirp->d_ino)->ino_linkcnt = dp->di_nlink;
431			goto again;
432
433		case DSTATE:
434			if (inoinfo(idesc->id_number)->ino_state == DFOUND)
435				inoinfo(dirp->d_ino)->ino_state = DFOUND;
436			/* fall through */
437
438		case DFOUND:
439			inp = getinoinfo(dirp->d_ino);
440			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
441				getpathname(pathbuf, idesc->id_number,
442				    idesc->id_number);
443				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
444				pwarn("%s %s %s\n", pathbuf,
445				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
446				    namebuf);
447				if (preen) {
448					printf(" (REMOVED)\n");
449					n = 1;
450					break;
451				}
452				if ((n = reply("REMOVE")) == 1)
453					break;
454			}
455			if (idesc->id_entryno > 2)
456				inp->i_parent = idesc->id_number;
457			/* fall through */
458
459		case FSTATE:
460			if (newinofmt &&
461			    dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
462				fileerror(idesc->id_number, dirp->d_ino,
463				    "BAD TYPE VALUE");
464				dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
465				if (reply("FIX") == 1)
466					ret |= ALTERED;
467			}
468			inoinfo(dirp->d_ino)->ino_linkcnt--;
469			break;
470
471		default:
472			errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
473			    inoinfo(dirp->d_ino)->ino_state, dirp->d_ino);
474		}
475	}
476	if (n == 0)
477		return (ret|KEEPON);
478	dirp->d_ino = 0;
479	return (ret|KEEPON|ALTERED);
480}
481
482/*
483 * Routine to sort disk blocks.
484 */
485static int
486blksort(arg1, arg2)
487	const void *arg1, *arg2;
488{
489
490	return ((*(struct inoinfo **)arg1)->i_blks[0] -
491		(*(struct inoinfo **)arg2)->i_blks[0]);
492}
493