xref: /netbsd-src/sbin/fsck_ext2fs/pass4.c (revision 6d07b400dc878478f544ef48ff61b3295c40c6bb)
1*6d07b400Sbouyer /*	$NetBSD: pass4.c,v 1.10 2009/10/19 18:41:08 bouyer Exp $	*/
2bf07c871Sagc 
3bf07c871Sagc /*
4bf07c871Sagc  * Copyright (c) 1980, 1986, 1993
5bf07c871Sagc  *	The Regents of the University of California.  All rights reserved.
6bf07c871Sagc  *
7bf07c871Sagc  * Redistribution and use in source and binary forms, with or without
8bf07c871Sagc  * modification, are permitted provided that the following conditions
9bf07c871Sagc  * are met:
10bf07c871Sagc  * 1. Redistributions of source code must retain the above copyright
11bf07c871Sagc  *    notice, this list of conditions and the following disclaimer.
12bf07c871Sagc  * 2. Redistributions in binary form must reproduce the above copyright
13bf07c871Sagc  *    notice, this list of conditions and the following disclaimer in the
14bf07c871Sagc  *    documentation and/or other materials provided with the distribution.
15bf07c871Sagc  * 3. Neither the name of the University nor the names of its contributors
16bf07c871Sagc  *    may be used to endorse or promote products derived from this software
17bf07c871Sagc  *    without specific prior written permission.
18bf07c871Sagc  *
19bf07c871Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20bf07c871Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bf07c871Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bf07c871Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23bf07c871Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bf07c871Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bf07c871Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bf07c871Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bf07c871Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bf07c871Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bf07c871Sagc  * SUCH DAMAGE.
30bf07c871Sagc  */
318f7c2b37Sbouyer 
328f7c2b37Sbouyer /*
338f7c2b37Sbouyer  * Copyright (c) 1997 Manuel Bouyer.
348f7c2b37Sbouyer  *
358f7c2b37Sbouyer  * Redistribution and use in source and binary forms, with or without
368f7c2b37Sbouyer  * modification, are permitted provided that the following conditions
378f7c2b37Sbouyer  * are met:
388f7c2b37Sbouyer  * 1. Redistributions of source code must retain the above copyright
398f7c2b37Sbouyer  *    notice, this list of conditions and the following disclaimer.
408f7c2b37Sbouyer  * 2. Redistributions in binary form must reproduce the above copyright
418f7c2b37Sbouyer  *    notice, this list of conditions and the following disclaimer in the
428f7c2b37Sbouyer  *    documentation and/or other materials provided with the distribution.
438f7c2b37Sbouyer  *
442f853da9Sbouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
452f853da9Sbouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
462f853da9Sbouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
472f853da9Sbouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
482f853da9Sbouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
492f853da9Sbouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
502f853da9Sbouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
512f853da9Sbouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
522f853da9Sbouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
532f853da9Sbouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
548f7c2b37Sbouyer  */
558f7c2b37Sbouyer 
564b836889Slukem #include <sys/cdefs.h>
578f7c2b37Sbouyer #ifndef lint
588f7c2b37Sbouyer #if 0
598f7c2b37Sbouyer static char sccsid[] = "@(#)pass4.c	8.1 (Berkeley) 6/5/93";
608f7c2b37Sbouyer #else
61*6d07b400Sbouyer __RCSID("$NetBSD: pass4.c,v 1.10 2009/10/19 18:41:08 bouyer Exp $");
628f7c2b37Sbouyer #endif
638f7c2b37Sbouyer #endif /* not lint */
648f7c2b37Sbouyer 
658f7c2b37Sbouyer #include <sys/param.h>
668f7c2b37Sbouyer #include <sys/time.h>
678f7c2b37Sbouyer #include <ufs/ext2fs/ext2fs_dinode.h>
688f7c2b37Sbouyer #include <ufs/ext2fs/ext2fs.h>
698f7c2b37Sbouyer #include <stdlib.h>
708f7c2b37Sbouyer #include <string.h>
718f7c2b37Sbouyer 
728f7c2b37Sbouyer #include "fsutil.h"
738f7c2b37Sbouyer #include "fsck.h"
748f7c2b37Sbouyer #include "extern.h"
758f7c2b37Sbouyer 
768f7c2b37Sbouyer void
pass4(void)77ccde05f0Sxtraeme pass4(void)
788f7c2b37Sbouyer {
794b836889Slukem 	ino_t inumber;
804b836889Slukem 	struct zlncnt *zlnp;
818f7c2b37Sbouyer 	struct ext2fs_dinode *dp;
828f7c2b37Sbouyer 	struct inodesc idesc;
838f7c2b37Sbouyer 	int n;
848f7c2b37Sbouyer 
858f7c2b37Sbouyer 	memset(&idesc, 0, sizeof(struct inodesc));
868f7c2b37Sbouyer 	idesc.id_type = ADDR;
878f7c2b37Sbouyer 	idesc.id_func = pass4check;
888f7c2b37Sbouyer 	for (inumber = EXT2_ROOTINO; inumber <= lastino; inumber++) {
898f7c2b37Sbouyer 		if (inumber < EXT2_FIRSTINO && inumber > EXT2_ROOTINO)
908f7c2b37Sbouyer 			continue;
918f7c2b37Sbouyer 		idesc.id_number = inumber;
928f7c2b37Sbouyer 		switch (statemap[inumber]) {
938f7c2b37Sbouyer 
948f7c2b37Sbouyer 		case FSTATE:
958f7c2b37Sbouyer 		case DFOUND:
968f7c2b37Sbouyer 			n = lncntp[inumber];
978f7c2b37Sbouyer 			if (n)
988f7c2b37Sbouyer 				adjust(&idesc, (short)n);
998f7c2b37Sbouyer 			else {
1008f7c2b37Sbouyer 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
1018f7c2b37Sbouyer 					if (zlnp->zlncnt == inumber) {
1028f7c2b37Sbouyer 						zlnp->zlncnt = zlnhead->zlncnt;
1038f7c2b37Sbouyer 						zlnp = zlnhead;
1048f7c2b37Sbouyer 						zlnhead = zlnhead->next;
1058f7c2b37Sbouyer 						free((char *)zlnp);
1068f7c2b37Sbouyer 						clri(&idesc, "UNREF", 1);
1078f7c2b37Sbouyer 						break;
1088f7c2b37Sbouyer 					}
1098f7c2b37Sbouyer 			}
1108f7c2b37Sbouyer 			break;
1118f7c2b37Sbouyer 
1128f7c2b37Sbouyer 		case DSTATE:
1138f7c2b37Sbouyer 			clri(&idesc, "UNREF", 1);
1148f7c2b37Sbouyer 			break;
1158f7c2b37Sbouyer 
1168f7c2b37Sbouyer 		case DCLEAR:
1178f7c2b37Sbouyer 			dp = ginode(inumber);
118d53c382dSws 			if (inosize(dp) == 0) {
1198f7c2b37Sbouyer 				clri(&idesc, "ZERO LENGTH", 1);
1208f7c2b37Sbouyer 				break;
1218f7c2b37Sbouyer 			}
1228f7c2b37Sbouyer 			/* fall through */
1238f7c2b37Sbouyer 		case FCLEAR:
1248f7c2b37Sbouyer 			clri(&idesc, "BAD/DUP", 1);
1258f7c2b37Sbouyer 			break;
1268f7c2b37Sbouyer 
1278f7c2b37Sbouyer 		case USTATE:
1288f7c2b37Sbouyer 			break;
1298f7c2b37Sbouyer 
1308f7c2b37Sbouyer 		default:
131481ad7b0Slukem 			errexit("BAD STATE %d FOR INODE I=%llu",
132c4ee9f6dSchristos 			    statemap[inumber], (unsigned long long)inumber);
1338f7c2b37Sbouyer 		}
1348f7c2b37Sbouyer 	}
1358f7c2b37Sbouyer }
1368f7c2b37Sbouyer 
1378f7c2b37Sbouyer int
pass4check(struct inodesc * idesc)138ccde05f0Sxtraeme pass4check(struct inodesc *idesc)
1398f7c2b37Sbouyer {
1404b836889Slukem 	struct dups *dlp;
1418f7c2b37Sbouyer 	int nfrags, res = KEEPON;
1428f7c2b37Sbouyer 	daddr_t blkno = idesc->id_blkno;
1438f7c2b37Sbouyer 
1448f7c2b37Sbouyer 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1458f7c2b37Sbouyer 		if (chkrange(blkno, 1)) {
1468f7c2b37Sbouyer 			res = SKIP;
1478f7c2b37Sbouyer 		} else if (testbmap(blkno)) {
1488f7c2b37Sbouyer 			for (dlp = duplist; dlp; dlp = dlp->next) {
1498f7c2b37Sbouyer 				if (dlp->dup != blkno)
1508f7c2b37Sbouyer 					continue;
1518f7c2b37Sbouyer 				dlp->dup = duplist->dup;
1528f7c2b37Sbouyer 				dlp = duplist;
1538f7c2b37Sbouyer 				duplist = duplist->next;
1548f7c2b37Sbouyer 				free((char *)dlp);
1558f7c2b37Sbouyer 				break;
1568f7c2b37Sbouyer 			}
1578f7c2b37Sbouyer 			if (dlp == 0) {
1588f7c2b37Sbouyer 				clrbmap(blkno);
1598f7c2b37Sbouyer 				n_blks--;
1608f7c2b37Sbouyer 			}
1618f7c2b37Sbouyer 		}
1628f7c2b37Sbouyer 	}
1638f7c2b37Sbouyer 	return (res);
1648f7c2b37Sbouyer }
165