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 * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114589Sobrien#if 0
311558Srgrimes#ifndef lint
3223675Speterstatic const char sccsid[] = "@(#)pass3.c	8.2 (Berkeley) 4/27/95";
33114589Sobrien#endif /* not lint */
3441477Sjulian#endif
35114589Sobrien#include <sys/cdefs.h>
36114589Sobrien__FBSDID("$FreeBSD$");
371558Srgrimes
381558Srgrimes#include <sys/param.h>
3923675Speter
401558Srgrimes#include <ufs/ufs/dinode.h>
4141474Sjulian#include <ufs/ufs/dir.h>
4241474Sjulian#include <ufs/ffs/fs.h>
4323675Speter
4441474Sjulian#include <string.h>
4541474Sjulian
461558Srgrimes#include "fsck.h"
471558Srgrimes
487585Sbdevoid
4992839Simppass3(void)
501558Srgrimes{
5141474Sjulian	struct inoinfo *inp;
5241474Sjulian	int loopcnt, inpindex, state;
531558Srgrimes	ino_t orphan;
5441474Sjulian	struct inodesc idesc;
5541474Sjulian	char namebuf[MAXNAMLEN+1];
561558Srgrimes
5741474Sjulian	for (inpindex = inplast - 1; inpindex >= 0; inpindex--) {
5870050Siedowse		if (got_siginfo) {
5970050Siedowse			printf("%s: phase 3: dir %d of %d (%d%%)\n", cdevname,
6086514Siedowse			    (int)(inplast - inpindex - 1), (int)inplast,
6186514Siedowse			    (int)((inplast - inpindex - 1) * 100 / inplast));
6270050Siedowse			got_siginfo = 0;
6370050Siedowse		}
64126345Sscottl		if (got_sigalarm) {
65126345Sscottl			setproctitle("%s p3 %d%%", cdevname,
66126345Sscottl			    (int)((inplast - inpindex - 1) * 100 / inplast));
67126345Sscottl			got_sigalarm = 0;
68126345Sscottl		}
6941474Sjulian		inp = inpsort[inpindex];
7041474Sjulian		state = inoinfo(inp->i_number)->ino_state;
711558Srgrimes		if (inp->i_number == ROOTINO ||
72136281Struckman		    (inp->i_parent != 0 && !S_IS_DUNFOUND(state)))
731558Srgrimes			continue;
7441474Sjulian		if (state == DCLEAR)
751558Srgrimes			continue;
7641474Sjulian		/*
7741474Sjulian		 * If we are running with soft updates and we come
7841474Sjulian		 * across unreferenced directories, we just leave
7941474Sjulian		 * them in DSTATE which will cause them to be pitched
8041474Sjulian		 * in pass 4.
8141474Sjulian		 */
8274556Smckusick		if ((preen || bkgrdflag) &&
83136281Struckman		    resolved && usedsoftdep && S_IS_DUNFOUND(state)) {
8441474Sjulian			if (inp->i_dotdot >= ROOTINO)
8541474Sjulian				inoinfo(inp->i_dotdot)->ino_linkcnt++;
8641474Sjulian			continue;
8741474Sjulian		}
881558Srgrimes		for (loopcnt = 0; ; loopcnt++) {
891558Srgrimes			orphan = inp->i_number;
901558Srgrimes			if (inp->i_parent == 0 ||
91136281Struckman			    !INO_IS_DUNFOUND(inp->i_parent) ||
9241474Sjulian			    loopcnt > countdirs)
931558Srgrimes				break;
941558Srgrimes			inp = getinoinfo(inp->i_parent);
951558Srgrimes		}
9641474Sjulian		if (loopcnt <= countdirs) {
9741474Sjulian			if (linkup(orphan, inp->i_dotdot, NULL)) {
9841474Sjulian				inp->i_parent = inp->i_dotdot = lfdir;
9941474Sjulian				inoinfo(lfdir)->ino_linkcnt--;
10041474Sjulian			}
10141474Sjulian			inoinfo(orphan)->ino_state = DFOUND;
10241474Sjulian			propagate();
10341474Sjulian			continue;
10441474Sjulian		}
10586514Siedowse		pfatal("ORPHANED DIRECTORY LOOP DETECTED I=%lu",
10686514Siedowse		    (u_long)orphan);
10741474Sjulian		if (reply("RECONNECT") == 0)
10841474Sjulian			continue;
10941474Sjulian		memset(&idesc, 0, sizeof(struct inodesc));
11041474Sjulian		idesc.id_type = DATA;
11141474Sjulian		idesc.id_number = inp->i_parent;
11241474Sjulian		idesc.id_parent = orphan;
11341474Sjulian		idesc.id_func = findname;
11441474Sjulian		idesc.id_name = namebuf;
11541474Sjulian		if ((ckinode(ginode(inp->i_parent), &idesc) & FOUND) == 0)
11641474Sjulian			pfatal("COULD NOT FIND NAME IN PARENT DIRECTORY");
11741474Sjulian		if (linkup(orphan, inp->i_parent, namebuf)) {
11841474Sjulian			idesc.id_func = clearentry;
11941474Sjulian			if (ckinode(ginode(inp->i_parent), &idesc) & FOUND)
12041474Sjulian				inoinfo(orphan)->ino_linkcnt++;
12141474Sjulian			inp->i_parent = inp->i_dotdot = lfdir;
12241474Sjulian			inoinfo(lfdir)->ino_linkcnt--;
12341474Sjulian		}
12441474Sjulian		inoinfo(orphan)->ino_state = DFOUND;
1251558Srgrimes		propagate();
1261558Srgrimes	}
1271558Srgrimes}
128