xref: /openbsd-src/sbin/fsck_ext2fs/pass4.c (revision b9fc9a728fce9c4289b7e9a992665e28d5629a54)
1*b9fc9a72Sderaadt /*	$OpenBSD: pass4.c,v 1.10 2015/01/16 06:39:57 deraadt Exp $	*/
20190393fSart /*	$NetBSD: pass4.c,v 1.2 1997/09/14 14:27:29 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 
34*b9fc9a72Sderaadt #include <sys/param.h>	/* isset clrbit */
358c424e8eSdownsj #include <sys/time.h>
368c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dinode.h>
378c424e8eSdownsj #include <ufs/ext2fs/ext2fs.h>
388c424e8eSdownsj #include <stdlib.h>
398c424e8eSdownsj #include <string.h>
408c424e8eSdownsj 
418c424e8eSdownsj #include "fsutil.h"
428c424e8eSdownsj #include "fsck.h"
438c424e8eSdownsj #include "extern.h"
448c424e8eSdownsj 
458c424e8eSdownsj void
pass4(void)468809fabbSderaadt pass4(void)
478c424e8eSdownsj {
480190393fSart 	ino_t inumber;
490190393fSart 	struct zlncnt *zlnp;
508c424e8eSdownsj 	struct ext2fs_dinode *dp;
518c424e8eSdownsj 	struct inodesc idesc;
528c424e8eSdownsj 	int n;
538c424e8eSdownsj 
548c424e8eSdownsj 	memset(&idesc, 0, sizeof(struct inodesc));
558c424e8eSdownsj 	idesc.id_type = ADDR;
568c424e8eSdownsj 	idesc.id_func = pass4check;
578c424e8eSdownsj 	for (inumber = EXT2_ROOTINO; inumber <= lastino; inumber++) {
588c424e8eSdownsj 		if (inumber < EXT2_FIRSTINO && inumber > EXT2_ROOTINO)
598c424e8eSdownsj 			continue;
608c424e8eSdownsj 		idesc.id_number = inumber;
618c424e8eSdownsj 		switch (statemap[inumber]) {
628c424e8eSdownsj 
638c424e8eSdownsj 		case FSTATE:
648c424e8eSdownsj 		case DFOUND:
658c424e8eSdownsj 			n = lncntp[inumber];
668c424e8eSdownsj 			if (n)
678c424e8eSdownsj 				adjust(&idesc, (short)n);
688c424e8eSdownsj 			else {
698c424e8eSdownsj 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
708c424e8eSdownsj 					if (zlnp->zlncnt == inumber) {
718c424e8eSdownsj 						zlnp->zlncnt = zlnhead->zlncnt;
728c424e8eSdownsj 						zlnp = zlnhead;
738c424e8eSdownsj 						zlnhead = zlnhead->next;
748c424e8eSdownsj 						free((char *)zlnp);
758c424e8eSdownsj 						clri(&idesc, "UNREF", 1);
768c424e8eSdownsj 						break;
778c424e8eSdownsj 					}
788c424e8eSdownsj 			}
798c424e8eSdownsj 			break;
808c424e8eSdownsj 
818c424e8eSdownsj 		case DSTATE:
828c424e8eSdownsj 			clri(&idesc, "UNREF", 1);
838c424e8eSdownsj 			break;
848c424e8eSdownsj 
858c424e8eSdownsj 		case DCLEAR:
868c424e8eSdownsj 			dp = ginode(inumber);
87935730fbSniallo 			if (inosize(dp) == 0) {
888c424e8eSdownsj 				clri(&idesc, "ZERO LENGTH", 1);
898c424e8eSdownsj 				break;
908c424e8eSdownsj 			}
918c424e8eSdownsj 			/* fall through */
928c424e8eSdownsj 		case FCLEAR:
938c424e8eSdownsj 			clri(&idesc, "BAD/DUP", 1);
948c424e8eSdownsj 			break;
958c424e8eSdownsj 
968c424e8eSdownsj 		case USTATE:
978c424e8eSdownsj 			break;
988c424e8eSdownsj 
998c424e8eSdownsj 		default:
1003b92bd08Sderaadt 			errexit("BAD STATE %d FOR INODE I=%llu\n",
1013b92bd08Sderaadt 			    statemap[inumber], (unsigned long long)inumber);
1028c424e8eSdownsj 		}
1038c424e8eSdownsj 	}
1048c424e8eSdownsj }
1058c424e8eSdownsj 
1068c424e8eSdownsj int
pass4check(struct inodesc * idesc)1078809fabbSderaadt pass4check(struct inodesc *idesc)
1088c424e8eSdownsj {
1090190393fSart 	struct dups *dlp;
1108c424e8eSdownsj 	int nfrags, res = KEEPON;
111b6d2e2d5Sderaadt 	daddr32_t blkno = idesc->id_blkno;
1128c424e8eSdownsj 
1138c424e8eSdownsj 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1148c424e8eSdownsj 		if (chkrange(blkno, 1)) {
1158c424e8eSdownsj 			res = SKIP;
1168c424e8eSdownsj 		} else if (testbmap(blkno)) {
1178c424e8eSdownsj 			for (dlp = duplist; dlp; dlp = dlp->next) {
1188c424e8eSdownsj 				if (dlp->dup != blkno)
1198c424e8eSdownsj 					continue;
1208c424e8eSdownsj 				dlp->dup = duplist->dup;
1218c424e8eSdownsj 				dlp = duplist;
1228c424e8eSdownsj 				duplist = duplist->next;
1238c424e8eSdownsj 				free((char *)dlp);
1248c424e8eSdownsj 				break;
1258c424e8eSdownsj 			}
1268c424e8eSdownsj 			if (dlp == 0) {
1278c424e8eSdownsj 				clrbmap(blkno);
1288c424e8eSdownsj 				n_blks--;
1298c424e8eSdownsj 			}
1308c424e8eSdownsj 		}
1318c424e8eSdownsj 	}
1328c424e8eSdownsj 	return (res);
1338c424e8eSdownsj }
134