xref: /openbsd/sbin/fsck_ext2fs/pass4.c (revision cecf84d4)
1 /*	$OpenBSD: pass4.c,v 1.10 2015/01/16 06:39:57 deraadt Exp $	*/
2 /*	$NetBSD: pass4.c,v 1.2 1997/09/14 14:27:29 lukem Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1980, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>	/* isset clrbit */
35 #include <sys/time.h>
36 #include <ufs/ext2fs/ext2fs_dinode.h>
37 #include <ufs/ext2fs/ext2fs.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "fsutil.h"
42 #include "fsck.h"
43 #include "extern.h"
44 
45 void
46 pass4(void)
47 {
48 	ino_t inumber;
49 	struct zlncnt *zlnp;
50 	struct ext2fs_dinode *dp;
51 	struct inodesc idesc;
52 	int n;
53 
54 	memset(&idesc, 0, sizeof(struct inodesc));
55 	idesc.id_type = ADDR;
56 	idesc.id_func = pass4check;
57 	for (inumber = EXT2_ROOTINO; inumber <= lastino; inumber++) {
58 		if (inumber < EXT2_FIRSTINO && inumber > EXT2_ROOTINO)
59 			continue;
60 		idesc.id_number = inumber;
61 		switch (statemap[inumber]) {
62 
63 		case FSTATE:
64 		case DFOUND:
65 			n = lncntp[inumber];
66 			if (n)
67 				adjust(&idesc, (short)n);
68 			else {
69 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
70 					if (zlnp->zlncnt == inumber) {
71 						zlnp->zlncnt = zlnhead->zlncnt;
72 						zlnp = zlnhead;
73 						zlnhead = zlnhead->next;
74 						free((char *)zlnp);
75 						clri(&idesc, "UNREF", 1);
76 						break;
77 					}
78 			}
79 			break;
80 
81 		case DSTATE:
82 			clri(&idesc, "UNREF", 1);
83 			break;
84 
85 		case DCLEAR:
86 			dp = ginode(inumber);
87 			if (inosize(dp) == 0) {
88 				clri(&idesc, "ZERO LENGTH", 1);
89 				break;
90 			}
91 			/* fall through */
92 		case FCLEAR:
93 			clri(&idesc, "BAD/DUP", 1);
94 			break;
95 
96 		case USTATE:
97 			break;
98 
99 		default:
100 			errexit("BAD STATE %d FOR INODE I=%llu\n",
101 			    statemap[inumber], (unsigned long long)inumber);
102 		}
103 	}
104 }
105 
106 int
107 pass4check(struct inodesc *idesc)
108 {
109 	struct dups *dlp;
110 	int nfrags, res = KEEPON;
111 	daddr32_t blkno = idesc->id_blkno;
112 
113 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
114 		if (chkrange(blkno, 1)) {
115 			res = SKIP;
116 		} else if (testbmap(blkno)) {
117 			for (dlp = duplist; dlp; dlp = dlp->next) {
118 				if (dlp->dup != blkno)
119 					continue;
120 				dlp->dup = duplist->dup;
121 				dlp = duplist;
122 				duplist = duplist->next;
123 				free((char *)dlp);
124 				break;
125 			}
126 			if (dlp == 0) {
127 				clrbmap(blkno);
128 				n_blks--;
129 			}
130 		}
131 	}
132 	return (res);
133 }
134