pass3.c revision 126345
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
34114589Sobrien#if 0
351558Srgrimes#ifndef lint
3623675Speterstatic const char sccsid[] = "@(#)pass3.c	8.2 (Berkeley) 4/27/95";
37114589Sobrien#endif /* not lint */
3841477Sjulian#endif
39114589Sobrien#include <sys/cdefs.h>
40114589Sobrien__FBSDID("$FreeBSD: head/sbin/fsck_ffs/pass3.c 126345 2004-02-28 07:50:42Z scottl $");
411558Srgrimes
421558Srgrimes#include <sys/param.h>
4323675Speter
441558Srgrimes#include <ufs/ufs/dinode.h>
4541474Sjulian#include <ufs/ufs/dir.h>
4641474Sjulian#include <ufs/ffs/fs.h>
4723675Speter
4841474Sjulian#include <string.h>
4941474Sjulian
501558Srgrimes#include "fsck.h"
511558Srgrimes
527585Sbdevoid
5392839Simppass3(void)
541558Srgrimes{
5541474Sjulian	struct inoinfo *inp;
5641474Sjulian	int loopcnt, inpindex, state;
571558Srgrimes	ino_t orphan;
5841474Sjulian	struct inodesc idesc;
5941474Sjulian	char namebuf[MAXNAMLEN+1];
601558Srgrimes
6141474Sjulian	for (inpindex = inplast - 1; inpindex >= 0; inpindex--) {
6270050Siedowse		if (got_siginfo) {
6370050Siedowse			printf("%s: phase 3: dir %d of %d (%d%%)\n", cdevname,
6486514Siedowse			    (int)(inplast - inpindex - 1), (int)inplast,
6586514Siedowse			    (int)((inplast - inpindex - 1) * 100 / inplast));
6670050Siedowse			got_siginfo = 0;
6770050Siedowse		}
68126345Sscottl		if (got_sigalarm) {
69126345Sscottl			setproctitle("%s p3 %d%%", cdevname,
70126345Sscottl			    (int)((inplast - inpindex - 1) * 100 / inplast));
71126345Sscottl			got_sigalarm = 0;
72126345Sscottl		}
7341474Sjulian		inp = inpsort[inpindex];
7441474Sjulian		state = inoinfo(inp->i_number)->ino_state;
751558Srgrimes		if (inp->i_number == ROOTINO ||
7641474Sjulian		    (inp->i_parent != 0 && state != DSTATE))
771558Srgrimes			continue;
7841474Sjulian		if (state == DCLEAR)
791558Srgrimes			continue;
8041474Sjulian		/*
8141474Sjulian		 * If we are running with soft updates and we come
8241474Sjulian		 * across unreferenced directories, we just leave
8341474Sjulian		 * them in DSTATE which will cause them to be pitched
8441474Sjulian		 * in pass 4.
8541474Sjulian		 */
8674556Smckusick		if ((preen || bkgrdflag) &&
8774556Smckusick		    resolved && usedsoftdep && state == DSTATE) {
8841474Sjulian			if (inp->i_dotdot >= ROOTINO)
8941474Sjulian				inoinfo(inp->i_dotdot)->ino_linkcnt++;
9041474Sjulian			continue;
9141474Sjulian		}
921558Srgrimes		for (loopcnt = 0; ; loopcnt++) {
931558Srgrimes			orphan = inp->i_number;
941558Srgrimes			if (inp->i_parent == 0 ||
9541474Sjulian			    inoinfo(inp->i_parent)->ino_state != DSTATE ||
9641474Sjulian			    loopcnt > countdirs)
971558Srgrimes				break;
981558Srgrimes			inp = getinoinfo(inp->i_parent);
991558Srgrimes		}
10041474Sjulian		if (loopcnt <= countdirs) {
10141474Sjulian			if (linkup(orphan, inp->i_dotdot, NULL)) {
10241474Sjulian				inp->i_parent = inp->i_dotdot = lfdir;
10341474Sjulian				inoinfo(lfdir)->ino_linkcnt--;
10441474Sjulian			}
10541474Sjulian			inoinfo(orphan)->ino_state = DFOUND;
10641474Sjulian			propagate();
10741474Sjulian			continue;
10841474Sjulian		}
10986514Siedowse		pfatal("ORPHANED DIRECTORY LOOP DETECTED I=%lu",
11086514Siedowse		    (u_long)orphan);
11141474Sjulian		if (reply("RECONNECT") == 0)
11241474Sjulian			continue;
11341474Sjulian		memset(&idesc, 0, sizeof(struct inodesc));
11441474Sjulian		idesc.id_type = DATA;
11541474Sjulian		idesc.id_number = inp->i_parent;
11641474Sjulian		idesc.id_parent = orphan;
11741474Sjulian		idesc.id_func = findname;
11841474Sjulian		idesc.id_name = namebuf;
11941474Sjulian		if ((ckinode(ginode(inp->i_parent), &idesc) & FOUND) == 0)
12041474Sjulian			pfatal("COULD NOT FIND NAME IN PARENT DIRECTORY");
12141474Sjulian		if (linkup(orphan, inp->i_parent, namebuf)) {
12241474Sjulian			idesc.id_func = clearentry;
12341474Sjulian			if (ckinode(ginode(inp->i_parent), &idesc) & FOUND)
12441474Sjulian				inoinfo(orphan)->ino_linkcnt++;
12541474Sjulian			inp->i_parent = inp->i_dotdot = lfdir;
12641474Sjulian			inoinfo(lfdir)->ino_linkcnt--;
12741474Sjulian		}
12841474Sjulian		inoinfo(orphan)->ino_state = DFOUND;
1291558Srgrimes		propagate();
1301558Srgrimes	}
1311558Srgrimes}
132