xref: /openbsd-src/sbin/fsck_ext2fs/pass3.c (revision b9fc9a728fce9c4289b7e9a992665e28d5629a54)
1*b9fc9a72Sderaadt /*	$OpenBSD: pass3.c,v 1.7 2015/01/16 06:39:57 deraadt Exp $	*/
20190393fSart /*	$NetBSD: pass3.c,v 1.2 1997/09/14 14:27:28 lukem Exp $	*/
38c424e8eSdownsj 
48c424e8eSdownsj /*
55ff4e0c8Sdownsj  * Copyright (c) 1997 Manuel Bouyer.
68c424e8eSdownsj  * Copyright (c) 1980, 1986, 1993
78c424e8eSdownsj  *	The Regents of the University of California.  All rights reserved.
88c424e8eSdownsj  *
98c424e8eSdownsj  * Redistribution and use in source and binary forms, with or without
108c424e8eSdownsj  * modification, are permitted provided that the following conditions
118c424e8eSdownsj  * are met:
128c424e8eSdownsj  * 1. Redistributions of source code must retain the above copyright
138c424e8eSdownsj  *    notice, this list of conditions and the following disclaimer.
148c424e8eSdownsj  * 2. Redistributions in binary form must reproduce the above copyright
158c424e8eSdownsj  *    notice, this list of conditions and the following disclaimer in the
168c424e8eSdownsj  *    documentation and/or other materials provided with the distribution.
171ef0d710Smillert  * 3. Neither the name of the University nor the names of its contributors
188c424e8eSdownsj  *    may be used to endorse or promote products derived from this software
198c424e8eSdownsj  *    without specific prior written permission.
208c424e8eSdownsj  *
218c424e8eSdownsj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228c424e8eSdownsj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238c424e8eSdownsj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248c424e8eSdownsj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258c424e8eSdownsj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268c424e8eSdownsj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278c424e8eSdownsj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288c424e8eSdownsj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298c424e8eSdownsj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308c424e8eSdownsj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318c424e8eSdownsj  * SUCH DAMAGE.
328c424e8eSdownsj  */
338c424e8eSdownsj 
348c424e8eSdownsj #include <sys/time.h>
358c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dinode.h>
368c424e8eSdownsj #include <ufs/ext2fs/ext2fs.h>
378c424e8eSdownsj #include "fsck.h"
388c424e8eSdownsj #include "extern.h"
398c424e8eSdownsj 
408c424e8eSdownsj void
pass3(void)418809fabbSderaadt pass3(void)
428c424e8eSdownsj {
430190393fSart 	struct inoinfo **inpp, *inp;
448c424e8eSdownsj 	ino_t orphan;
458c424e8eSdownsj 	int loopcnt;
468c424e8eSdownsj 
478c424e8eSdownsj 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
488c424e8eSdownsj 		inp = *inpp;
498c424e8eSdownsj 		if (inp->i_number == EXT2_ROOTINO ||
508c424e8eSdownsj 		    !(inp->i_parent == 0 || statemap[inp->i_number] == DSTATE))
518c424e8eSdownsj 			continue;
528c424e8eSdownsj 		if (statemap[inp->i_number] == DCLEAR)
538c424e8eSdownsj 			continue;
548c424e8eSdownsj 		for (loopcnt = 0; ; loopcnt++) {
558c424e8eSdownsj 			orphan = inp->i_number;
568c424e8eSdownsj 			if (inp->i_parent == 0 ||
578c424e8eSdownsj 			    statemap[inp->i_parent] != DSTATE ||
588c424e8eSdownsj 			    loopcnt > numdirs)
598c424e8eSdownsj 				break;
608c424e8eSdownsj 			inp = getinoinfo(inp->i_parent);
618c424e8eSdownsj 		}
628c424e8eSdownsj 		(void)linkup(orphan, inp->i_dotdot);
638c424e8eSdownsj 		inp->i_parent = inp->i_dotdot = lfdir;
648c424e8eSdownsj 		lncntp[lfdir]--;
658c424e8eSdownsj 		statemap[orphan] = DFOUND;
668c424e8eSdownsj 		propagate();
678c424e8eSdownsj 	}
688c424e8eSdownsj }
69