xref: /openbsd/sbin/fsck_ext2fs/pass1.c (revision 404b540a)
1 /*	$OpenBSD: pass1.c,v 1.11 2005/04/30 13:56:16 niallo Exp $	*/
2 /*	$NetBSD: pass1.c,v 1.9 2000/01/31 11:40:12 bouyer 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>
35 #include <sys/time.h>
36 #include <ufs/ext2fs/ext2fs_dinode.h>
37 #include <ufs/ext2fs/ext2fs_dir.h>
38 #include <ufs/ext2fs/ext2fs.h>
39 
40 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 
47 #include "fsck.h"
48 #include "extern.h"
49 #include "fsutil.h"
50 
51 static daddr_t badblk;
52 static daddr_t dupblk;
53 static void checkinode(ino_t, struct inodesc *);
54 
55 void
56 pass1(void)
57 {
58 	ino_t inumber;
59 	int c, i;
60 	daddr_t dbase;
61 	struct inodesc idesc;
62 
63 	/*
64 	 * Set file system reserved blocks in used block map.
65 	 */
66 	for (c = 0; c < sblock.e2fs_ncg; c++) {
67 		dbase = c * sblock.e2fs.e2fs_bpg +
68 		    sblock.e2fs.e2fs_first_dblock;
69 		/* Mark the blocks used for the inode table */
70 		if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) {
71 			for (i = 0; i < sblock.e2fs_itpg; i++)
72 				setbmap(
73 				    fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables)
74 				    + i);
75 		}
76 		/* Mark the blocks used for the block bitmap */
77 		if (fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase)
78 			setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap));
79 		/* Mark the blocks used for the inode bitmap */
80 		if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase)
81 			setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap));
82 
83 		if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
84 		    (sblock.e2fs.e2fs_features_rocompat &
85 			EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
86 		    cg_has_sb(c)) {
87 			/* Mark copuy of SB and descriptors */
88 			setbmap(dbase);
89 			for (i = 1; i <= sblock.e2fs_ngdb; i++)
90 				setbmap(dbase+i);
91 		}
92 
93 
94 		if (c == 0) {
95 			for(i = 0; i < dbase; i++)
96 				setbmap(i);
97 		}
98 	}
99 
100 	/*
101 	 * Find all allocated blocks.
102 	 */
103 	memset(&idesc, 0, sizeof(struct inodesc));
104 	idesc.id_type = ADDR;
105 	idesc.id_func = pass1check;
106 	inumber = 1;
107 	n_files = n_blks = 0;
108 	resetinodebuf();
109 	for (c = 0; c < sblock.e2fs_ncg; c++) {
110 		for (i = 0;
111 			i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
112 			i++, inumber++) {
113 			if (inumber < EXT2_ROOTINO) /* XXX */
114 				continue;
115 			checkinode(inumber, &idesc);
116 		}
117 	}
118 	freeinodebuf();
119 }
120 
121 static void
122 checkinode(ino_t inumber, struct inodesc *idesc)
123 {
124 	struct ext2fs_dinode *dp;
125 	struct zlncnt *zlnp;
126 	int ndb, j;
127 	mode_t mode;
128 
129 	dp = getnextinode(inumber);
130 	if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
131 		return;
132 
133 	mode = fs2h16(dp->e2di_mode) & IFMT;
134 	if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
135 		if (mode == 0 && (
136 			memcmp(dp->e2di_blocks, zino.e2di_blocks,
137 			(NDADDR + NIADDR) * sizeof(u_int32_t)) ||
138 		    dp->e2di_mode || inosize(dp))) {
139 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
140 			if (reply("CLEAR") == 1) {
141 				dp = ginode(inumber);
142 				clearinode(dp);
143 				inodirty();
144 			}
145 		}
146 #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
147 		if (dp->e2di_dtime == 0) {
148 			pwarn("DELETED INODE I=%u HAS A NULL DTIME", inumber);
149 			if (preen) {
150 				printf(" (CORRECTED)\n");
151 			}
152 			if (preen || reply("CORRECT")) {
153 				time_t t;
154 				time(&t);
155 				dp->e2di_dtime = h2fs32(t);
156 				dp = ginode(inumber);
157 				inodirty();
158 			}
159 		}
160 #endif
161 		statemap[inumber] = USTATE;
162 		return;
163 	}
164 	lastino = inumber;
165 	if (dp->e2di_dtime != 0) {
166 		time_t t = fs2h32(dp->e2di_dtime);
167 		char *p = ctime(&t);
168 		pwarn("INODE I=%u HAS DTIME=%12.12s %4.4s", inumber, &p[4], &p[20]);
169 		if (preen) {
170 			printf(" (CORRECTED)\n");
171 		}
172 		if (preen || reply("CORRECT")) {
173 			dp = ginode(inumber);
174 			dp->e2di_dtime = 0;
175 			inodirty();
176 		}
177 	}
178 	if (inosize(dp) + sblock.e2fs_bsize - 1 < inosize(dp)) {
179 		if (debug)
180 			printf("bad size %llu:", (unsigned long long)inosize(dp));
181 		goto unknown;
182 	}
183 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
184 		dp = ginode(inumber);
185 		dp->e2di_mode = h2fs16(IFREG|0600);
186 		inossize(dp, sblock.e2fs_bsize);
187 		inodirty();
188 	}
189 	ndb = howmany(inosize(dp), sblock.e2fs_bsize);
190 	if (ndb < 0) {
191 		if (debug)
192 			printf("bad size %llu ndb %d:",
193 			    (unsigned long long)inosize(dp), ndb);
194 		goto unknown;
195 	}
196 	if (mode == IFBLK || mode == IFCHR)
197 		ndb++;
198 	if (mode == IFLNK) {
199 		/*
200 		 * Fake ndb value so direct/indirect block checks below
201 		 * will detect any garbage after symlink string.
202 		 */
203 		if (inosize(dp) < EXT2_MAXSYMLINKLEN ||
204 		    (EXT2_MAXSYMLINKLEN == 0 && dp->e2di_blocks == 0)) {
205 			ndb = howmany(inosize(dp), sizeof(u_int32_t));
206 			if (ndb > NDADDR) {
207 				j = ndb - NDADDR;
208 				for (ndb = 1; j > 1; j--)
209 					ndb *= NINDIR(&sblock);
210 				ndb += NDADDR;
211 			}
212 		}
213 	}
214 	/* Linux puts things in blocks for FIFO, so skip this check */
215 	if (mode != IFIFO) {
216 		for (j = ndb; j < NDADDR; j++)
217 			if (dp->e2di_blocks[j] != 0) {
218 				if (debug)
219 					printf("bad direct addr: %d\n",
220 					    fs2h32(dp->e2di_blocks[j]));
221 				goto unknown;
222 			}
223 		for (j = 0, ndb -= NDADDR; ndb > 0; j++)
224 			ndb /= NINDIR(&sblock);
225 		for (; j < NIADDR; j++) {
226 			if (dp->e2di_blocks[j+NDADDR] != 0) {
227 				if (debug)
228 					printf("bad indirect addr: %d\n",
229 					    fs2h32(dp->e2di_blocks[j+NDADDR]));
230 				goto unknown;
231 			}
232 		}
233 	}
234 	if (ftypeok(dp) == 0)
235 		goto unknown;
236 	n_files++;
237 	lncntp[inumber] = fs2h16(dp->e2di_nlink);
238 	if (dp->e2di_nlink == 0) {
239 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
240 		if (zlnp == NULL) {
241 			pfatal("LINK COUNT TABLE OVERFLOW");
242 			if (reply("CONTINUE") == 0)
243 				errexit("%s\n", "");
244 		} else {
245 			zlnp->zlncnt = inumber;
246 			zlnp->next = zlnhead;
247 			zlnhead = zlnp;
248 		}
249 	}
250 	if (mode == IFDIR) {
251 		if (inosize(dp) == 0)
252 			statemap[inumber] = DCLEAR;
253 		else
254 			statemap[inumber] = DSTATE;
255 		cacheino(dp, inumber);
256 	} else {
257 		statemap[inumber] = FSTATE;
258 	}
259 	typemap[inumber] = E2IFTODT(mode);
260 	badblk = dupblk = 0;
261 	idesc->id_number = inumber;
262 	(void)ckinode(dp, idesc);
263 	idesc->id_entryno *= btodb(sblock.e2fs_bsize);
264 	if (fs2h32(dp->e2di_nblock) != idesc->id_entryno) {
265 		pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
266 		    inumber, fs2h32(dp->e2di_nblock), idesc->id_entryno);
267 		if (preen)
268 			printf(" (CORRECTED)\n");
269 		else if (reply("CORRECT") == 0)
270 			return;
271 		dp = ginode(inumber);
272 		dp->e2di_nblock = h2fs32(idesc->id_entryno);
273 		inodirty();
274 	}
275 	return;
276 unknown:
277 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
278 	statemap[inumber] = FCLEAR;
279 	if (reply("CLEAR") == 1) {
280 		statemap[inumber] = USTATE;
281 		dp = ginode(inumber);
282 		clearinode(dp);
283 		inodirty();
284 	}
285 }
286 
287 int
288 pass1check(struct inodesc *idesc)
289 {
290 	int res = KEEPON;
291 	int anyout, nfrags;
292 	daddr_t blkno = idesc->id_blkno;
293 	struct dups *dlp;
294 	struct dups *new;
295 
296 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
297 		blkerror(idesc->id_number, "BAD", blkno);
298 		if (badblk++ >= MAXBAD) {
299 			pwarn("EXCESSIVE BAD BLKS I=%u",
300 				idesc->id_number);
301 			if (preen)
302 				printf(" (SKIPPING)\n");
303 			else if (reply("CONTINUE") == 0)
304 				errexit("%s\n", "");
305 			return (STOP);
306 		}
307 	}
308 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
309 		if (anyout && chkrange(blkno, 1)) {
310 			res = SKIP;
311 		} else if (!testbmap(blkno)) {
312 			n_blks++;
313 			setbmap(blkno);
314 		} else {
315 			blkerror(idesc->id_number, "DUP", blkno);
316 			if (dupblk++ >= MAXDUP) {
317 				pwarn("EXCESSIVE DUP BLKS I=%u",
318 					idesc->id_number);
319 				if (preen)
320 					printf(" (SKIPPING)\n");
321 				else if (reply("CONTINUE") == 0)
322 					errexit("%s\n", "");
323 				return (STOP);
324 			}
325 			new = (struct dups *)malloc(sizeof(struct dups));
326 			if (new == NULL) {
327 				pfatal("DUP TABLE OVERFLOW.");
328 				if (reply("CONTINUE") == 0)
329 					errexit("%s\n", "");
330 				return (STOP);
331 			}
332 			new->dup = blkno;
333 			if (muldup == 0) {
334 				duplist = muldup = new;
335 				new->next = 0;
336 			} else {
337 				new->next = muldup->next;
338 				muldup->next = new;
339 			}
340 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
341 				if (dlp->dup == blkno)
342 					break;
343 			if (dlp == muldup && dlp->dup != blkno)
344 				muldup = new;
345 		}
346 		/*
347 		 * count the number of blocks found in id_entryno
348 		 */
349 		idesc->id_entryno++;
350 	}
351 	return (res);
352 }
353