/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1980, 1986, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/param.h>3233#include <ufs/ufs/dinode.h>34#include <ufs/ufs/dir.h>35#include <ufs/ffs/fs.h>3637#include <string.h>3839#include "fsck.h"4041void42pass3(void)43{44struct inoinfo *inp;45int loopcnt, inpindex, state;46ino_t orphan;47struct inode ip;48struct inodesc idesc;49char namebuf[UFS_MAXNAMLEN+1];5051for (inpindex = inplast - 1; inpindex >= 0; inpindex--) {52if (got_siginfo) {53printf("%s: phase 3: dir %d of %d (%d%%)\n", cdevname,54(int)(inplast - inpindex - 1), (int)inplast,55(int)((inplast - inpindex - 1) * 100 / inplast));56got_siginfo = 0;57}58if (got_sigalarm) {59setproctitle("%s p3 %d%%", cdevname,60(int)((inplast - inpindex - 1) * 100 / inplast));61got_sigalarm = 0;62}63inp = inpsort[inpindex];64state = inoinfo(inp->i_number)->ino_state;65if (inp->i_number == UFS_ROOTINO ||66(inp->i_parent != 0 && !S_IS_DUNFOUND(state)))67continue;68if (state == DCLEAR || state == DZLINK)69continue;70/*71* If we are running with soft updates and we come72* across unreferenced directories, we just leave73* them in DSTATE which will cause them to be pitched74* in pass 4.75*/76if ((preen || bkgrdflag) &&77resolved && usedsoftdep && S_IS_DUNFOUND(state)) {78if (inp->i_dotdot >= UFS_ROOTINO)79inoinfo(inp->i_dotdot)->ino_linkcnt++;80continue;81}82for (loopcnt = 0; ; loopcnt++) {83orphan = inp->i_number;84if (inp->i_parent == 0 ||85!INO_IS_DUNFOUND(inp->i_parent) ||86loopcnt > countdirs)87break;88inp = getinoinfo(inp->i_parent);89}90if (loopcnt <= countdirs) {91if (linkup(orphan, inp->i_dotdot, NULL)) {92inp->i_parent = inp->i_dotdot = lfdir;93inoinfo(lfdir)->ino_linkcnt--;94inoinfo(orphan)->ino_state = DFOUND;95check_dirdepth(inp);96propagate();97}98continue;99}100pfatal("ORPHANED DIRECTORY LOOP DETECTED I=%lu",101(u_long)orphan);102if (reply("RECONNECT") == 0)103continue;104memset(&idesc, 0, sizeof(struct inodesc));105idesc.id_type = DATA;106idesc.id_number = inp->i_parent;107idesc.id_parent = orphan;108idesc.id_func = findname;109idesc.id_name = namebuf;110ginode(inp->i_parent, &ip);111if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0)112pfatal("COULD NOT FIND NAME IN PARENT DIRECTORY");113if (linkup(orphan, inp->i_parent, namebuf)) {114idesc.id_func = clearentry;115if (ckinode(ip.i_dp, &idesc) & FOUND)116inoinfo(orphan)->ino_linkcnt++;117inp->i_parent = inp->i_dotdot = lfdir;118inoinfo(lfdir)->ino_linkcnt--;119}120irelse(&ip);121inoinfo(orphan)->ino_state = DFOUND;122check_dirdepth(inp);123propagate();124}125}126127128