xref: /openbsd-src/sbin/fsck_ext2fs/pass1.c (revision a7b9eedcb4c1ab985f28fc923822c24bc6417908)
1 /*	$OpenBSD: pass1.c,v 1.19 2024/05/09 08:35:40 florian 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>	/* setbit btodb isset */
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 daddr32_t badblk;
52 static daddr32_t dupblk;
53 static void checkinode(ino_t, struct inodesc *);
54 
55 void
pass1(void)56 pass1(void)
57 {
58 	ino_t inumber;
59 	int c, i;
60 	daddr32_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 (letoh32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) {
71 			for (i = 0; i < sblock.e2fs_itpg; i++)
72 				setbmap(
73 				    letoh32(sblock.e2fs_gd[c].ext2bgd_i_tables)
74 				    + i);
75 		}
76 		/* Mark the blocks used for the block bitmap */
77 		if (letoh32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase)
78 			setbmap(letoh32(sblock.e2fs_gd[c].ext2bgd_b_bitmap));
79 		/* Mark the blocks used for the inode bitmap */
80 		if (letoh32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase)
81 			setbmap(letoh32(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_SPARSE_SUPER) == 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
checkinode(ino_t inumber,struct inodesc * idesc)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 = letoh16(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=%llu",
140 			    (unsigned long long)inumber);
141 			if (reply("CLEAR") == 1) {
142 				dp = ginode(inumber);
143 				clearinode(dp);
144 				inodirty();
145 			}
146 		}
147 #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
148 		if (dp->e2di_dtime == 0) {
149 			pwarn("DELETED INODE I=%llu HAS A NULL DTIME",
150 			    (unsigned long long)inumber);
151 			if (preen) {
152 				printf(" (CORRECTED)\n");
153 			}
154 			if (preen || reply("CORRECT")) {
155 				time_t t;
156 				time(&t);
157 				dp->e2di_dtime = htole32(t);
158 				dp = ginode(inumber);
159 				inodirty();
160 			}
161 		}
162 #endif
163 		statemap[inumber] = USTATE;
164 		return;
165 	}
166 	lastino = inumber;
167 	if (dp->e2di_dtime != 0) {
168 		time_t t = letoh32(dp->e2di_dtime);
169 		char *p = ctime(&t);
170 		if (p)
171 			pwarn("INODE I=%llu HAS DTIME=%12.12s %4.4s",
172 			    (unsigned long long)inumber, &p[4], &p[20]);
173 		else
174 			pwarn("INODE I=%llu HAS DTIME=%lld",
175 			    (unsigned long long)inumber, t);
176 		if (preen) {
177 			printf(" (CORRECTED)\n");
178 		}
179 		if (preen || reply("CORRECT")) {
180 			dp = ginode(inumber);
181 			dp->e2di_dtime = 0;
182 			inodirty();
183 		}
184 	}
185 	if (inosize(dp) + sblock.e2fs_bsize - 1 < inosize(dp)) {
186 		if (debug)
187 			printf("bad size %llu:", (unsigned long long)inosize(dp));
188 		goto unknown;
189 	}
190 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
191 		dp = ginode(inumber);
192 		dp->e2di_mode = htole16(IFREG|0600);
193 		inossize(dp, sblock.e2fs_bsize);
194 		inodirty();
195 	}
196 	ndb = howmany(inosize(dp), sblock.e2fs_bsize);
197 	if (ndb < 0) {
198 		if (debug)
199 			printf("bad size %llu ndb %d:",
200 			    (unsigned long long)inosize(dp), ndb);
201 		goto unknown;
202 	}
203 	if (mode == IFBLK || mode == IFCHR)
204 		ndb++;
205 	if (mode == IFLNK) {
206 		/*
207 		 * Fake ndb value so direct/indirect block checks below
208 		 * will detect any garbage after symlink string.
209 		 */
210 		if (inosize(dp) < EXT2_MAXSYMLINKLEN ||
211 		    (EXT2_MAXSYMLINKLEN == 0 && htole32(dp->e2di_nblock) == 0)) {
212 			ndb = howmany(inosize(dp), sizeof(u_int32_t));
213 			if (ndb > NDADDR) {
214 				j = ndb - NDADDR;
215 				for (ndb = 1; j > 1; j--)
216 					ndb *= NINDIR(&sblock);
217 				ndb += NDADDR;
218 			}
219 		}
220 	}
221 	/* Linux puts things in blocks for FIFO, so skip this check */
222 	if (mode != IFIFO) {
223 		for (j = ndb; j < NDADDR; j++)
224 			if (dp->e2di_blocks[j] != 0) {
225 				if (debug)
226 					printf("bad direct addr: %d\n",
227 					    letoh32(dp->e2di_blocks[j]));
228 				goto unknown;
229 			}
230 		for (j = 0, ndb -= NDADDR; ndb > 0; j++)
231 			ndb /= NINDIR(&sblock);
232 		for (; j < NIADDR; j++) {
233 			if (dp->e2di_blocks[j+NDADDR] != 0) {
234 				if (debug)
235 					printf("bad indirect addr: %d\n",
236 					    letoh32(dp->e2di_blocks[j+NDADDR]));
237 				goto unknown;
238 			}
239 		}
240 	}
241 	if (ftypeok(dp) == 0)
242 		goto unknown;
243 	n_files++;
244 	lncntp[inumber] = letoh16(dp->e2di_nlink);
245 	if (dp->e2di_nlink == 0) {
246 		zlnp = malloc(sizeof *zlnp);
247 		if (zlnp == NULL) {
248 			pfatal("LINK COUNT TABLE OVERFLOW");
249 			if (reply("CONTINUE") == 0)
250 				errexit("%s\n", "");
251 		} else {
252 			zlnp->zlncnt = inumber;
253 			zlnp->next = zlnhead;
254 			zlnhead = zlnp;
255 		}
256 	}
257 	if (mode == IFDIR) {
258 		if (inosize(dp) == 0)
259 			statemap[inumber] = DCLEAR;
260 		else
261 			statemap[inumber] = DSTATE;
262 		cacheino(dp, inumber);
263 	} else {
264 		statemap[inumber] = FSTATE;
265 	}
266 	typemap[inumber] = E2IFTODT(mode);
267 	badblk = dupblk = 0;
268 	idesc->id_number = inumber;
269 	(void)ckinode(dp, idesc);
270 	idesc->id_entryno *= btodb(sblock.e2fs_bsize);
271 	if (letoh32(dp->e2di_nblock) != idesc->id_entryno) {
272 		pwarn("INCORRECT BLOCK COUNT I=%llu (%d should be %d)",
273 		    (unsigned long long)inumber,
274 		    letoh32(dp->e2di_nblock), idesc->id_entryno);
275 		if (preen)
276 			printf(" (CORRECTED)\n");
277 		else if (reply("CORRECT") == 0)
278 			return;
279 		dp = ginode(inumber);
280 		dp->e2di_nblock = htole32(idesc->id_entryno);
281 		inodirty();
282 	}
283 	return;
284 unknown:
285 	pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber);
286 	statemap[inumber] = FCLEAR;
287 	if (reply("CLEAR") == 1) {
288 		statemap[inumber] = USTATE;
289 		dp = ginode(inumber);
290 		clearinode(dp);
291 		inodirty();
292 	}
293 }
294 
295 int
pass1check(struct inodesc * idesc)296 pass1check(struct inodesc *idesc)
297 {
298 	int res = KEEPON;
299 	int anyout, nfrags;
300 	daddr32_t blkno = idesc->id_blkno;
301 	struct dups *dlp;
302 	struct dups *new;
303 
304 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
305 		blkerror(idesc->id_number, "BAD", blkno);
306 		if (badblk++ >= MAXBAD) {
307 			pwarn("EXCESSIVE BAD BLKS I=%llu",
308 			    (unsigned long long)idesc->id_number);
309 			if (preen)
310 				printf(" (SKIPPING)\n");
311 			else if (reply("CONTINUE") == 0)
312 				errexit("%s\n", "");
313 			return (STOP);
314 		}
315 	}
316 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
317 		if (anyout && chkrange(blkno, 1)) {
318 			res = SKIP;
319 		} else if (!testbmap(blkno)) {
320 			n_blks++;
321 			setbmap(blkno);
322 		} else {
323 			blkerror(idesc->id_number, "DUP", blkno);
324 			if (dupblk++ >= MAXDUP) {
325 				pwarn("EXCESSIVE DUP BLKS I=%lluu",
326 				    (unsigned long long)idesc->id_number);
327 				if (preen)
328 					printf(" (SKIPPING)\n");
329 				else if (reply("CONTINUE") == 0)
330 					errexit("%s\n", "");
331 				return (STOP);
332 			}
333 			new = malloc(sizeof(struct dups));
334 			if (new == NULL) {
335 				pfatal("DUP TABLE OVERFLOW.");
336 				if (reply("CONTINUE") == 0)
337 					errexit("%s\n", "");
338 				return (STOP);
339 			}
340 			new->dup = blkno;
341 			if (muldup == 0) {
342 				duplist = muldup = new;
343 				new->next = 0;
344 			} else {
345 				new->next = muldup->next;
346 				muldup->next = new;
347 			}
348 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
349 				if (dlp->dup == blkno)
350 					break;
351 			if (dlp == muldup && dlp->dup != blkno)
352 				muldup = new;
353 		}
354 		/*
355 		 * count the number of blocks found in id_entryno
356 		 */
357 		idesc->id_entryno++;
358 	}
359 	return (res);
360 }
361