xref: /openbsd/sbin/fsck_ffs/pass1.c (revision 404b540a)
1 /*	$OpenBSD: pass1.c,v 1.31 2009/05/29 07:57:43 otto Exp $	*/
2 /*	$NetBSD: pass1.c,v 1.16 1996/09/27 22:45:15 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)pass1.c	8.1 (Berkeley) 6/5/93";
36 #else
37 static const char rcsid[] = "$OpenBSD: pass1.c,v 1.31 2009/05/29 07:57:43 otto Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ffs/fs.h>
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "fsck.h"
53 #include "extern.h"
54 #include "fsutil.h"
55 
56 static daddr64_t badblk;
57 static daddr64_t dupblk;
58 static void checkinode(ino_t, struct inodesc *);
59 
60 static ino_t info_inumber;
61 
62 static int
63 pass1_info(char *buf, size_t buflen)
64 {
65 	return (snprintf(buf, buflen, "phase 1, inode %d/%d",
66 	    info_inumber, sblock.fs_ipg * sblock.fs_ncg) > 0);
67 }
68 
69 void
70 pass1(void)
71 {
72 	struct inodesc idesc;
73 	ino_t inumber, inosused;
74 	int c, i, cgd;
75 
76 	/*
77 	 * Set file system reserved blocks in used block map.
78 	 */
79 	for (c = 0; c < sblock.fs_ncg; c++) {
80 		cgd = cgdmin(&sblock, c);
81 		if (c == 0)
82 			i = cgbase(&sblock, c);
83 		else
84 			i = cgsblock(&sblock, c);
85 		for (; i < cgd; i++)
86 			setbmap(i);
87 	}
88 	i = sblock.fs_csaddr;
89 	cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
90 	for (; i < cgd; i++)
91 		setbmap(i);
92 	/*
93 	 * Find all allocated blocks.
94 	 */
95 	memset(&idesc, 0, sizeof(struct inodesc));
96 	idesc.id_type = ADDR;
97 	idesc.id_func = pass1check;
98 	n_files = n_blks = 0;
99 	info_inumber = 0;
100 	info_fn = pass1_info;
101 	for (c = 0; c < sblock.fs_ncg; c++) {
102 		inumber = c * sblock.fs_ipg;
103 		setinodebuf(inumber);
104 		getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
105 		if (sblock.fs_magic == FS_UFS2_MAGIC) {
106 			inosused = cgrp.cg_initediblk;
107 			if (inosused > sblock.fs_ipg)
108 				inosused = sblock.fs_ipg;
109 		} else
110 			inosused = sblock.fs_ipg;
111 		cginosused[c] = inosused;
112 		for (i = 0; i < inosused; i++, inumber++) {
113 			info_inumber = inumber;
114 			if (inumber < ROOTINO)
115 				continue;
116 			checkinode(inumber, &idesc);
117 		}
118 	}
119 	info_fn = NULL;
120 	freeinodebuf();
121 }
122 
123 static void
124 checkinode(ino_t inumber, struct inodesc *idesc)
125 {
126 	union dinode *dp;
127 	off_t kernmaxfilesize;
128 	struct zlncnt *zlnp;
129 	int ndb, j;
130 	mode_t mode;
131 	char *symbuf;
132 	u_int64_t lndb;
133 
134 	dp = getnextinode(inumber);
135 	mode = DIP(dp, di_mode) & IFMT;
136 	if (mode == 0) {
137 		if ((sblock.fs_magic == FS_UFS1_MAGIC &&
138 		     (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
139 			NDADDR * sizeof(int32_t)) ||
140 		      memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
141 			NIADDR * sizeof(int32_t)) ||
142 		      dp->dp1.di_mode || dp->dp1.di_size)) ||
143 		    (sblock.fs_magic == FS_UFS2_MAGIC &&
144 		     (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
145 			NDADDR * sizeof(daddr64_t)) ||
146 		      memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
147 			NIADDR * sizeof(daddr64_t)) ||
148 		      dp->dp2.di_mode || dp->dp2.di_size))) {
149 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
150 			if (reply("CLEAR") == 1) {
151 				dp = ginode(inumber);
152 				clearinode(dp);
153 				inodirty();
154 			}
155 		}
156 		SET_ISTATE(inumber, USTATE);
157 		return;
158 	}
159 	lastino = inumber;
160 	/* This should match the file size limit in ffs_mountfs(). */
161 	kernmaxfilesize = FS_KERNMAXFILESIZE(getpagesize(), &sblock);
162 	if (DIP(dp, di_size) > kernmaxfilesize ||
163 	    DIP(dp, di_size) > sblock.fs_maxfilesize ||
164 	    (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
165 		if (debug)
166 			printf("bad size %llu:",
167 			    (unsigned long long)DIP(dp, di_size));
168 		goto unknown;
169 	}
170 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
171 		dp = ginode(inumber);
172 		DIP_SET(dp, di_size, sblock.fs_fsize);
173 		DIP_SET(dp, di_mode, IFREG|0600);
174 		inodirty();
175 	}
176 	lndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
177 	ndb = lndb > (u_int64_t)INT_MAX ? -1 : (int)lndb;
178 	if (ndb < 0) {
179 		if (debug)
180 			printf("bad size %llu ndb %d:",
181 			    (unsigned long long)DIP(dp, di_size), ndb);
182 		goto unknown;
183 	}
184 	if (mode == IFBLK || mode == IFCHR)
185 		ndb++;
186 	if (mode == IFLNK) {
187 		/*
188 		 * Note that the old fastlink format always had di_blocks set
189 		 * to 0.  Other than that we no longer use the `spare' field
190 		 * (which is now the extended uid) for sanity checking, the
191 		 * new format is the same as the old.  We simply ignore the
192 		 * conversion altogether.  - mycroft, 19MAY1994
193 		 */
194 		if (sblock.fs_magic == FS_UFS1_MAGIC && doinglevel2 &&
195 		    DIP(dp, di_size) > 0 &&
196 		    DIP(dp, di_size) < MAXSYMLINKLEN_UFS1 &&
197 		    DIP(dp, di_blocks) != 0) {
198 			symbuf = alloca(secsize);
199 			if (bread(fsreadfd, symbuf,
200 			    fsbtodb(&sblock, DIP(dp, di_db[0])),
201 			    (long)secsize) != 0)
202 				errexit("cannot read symlink\n");
203 			if (debug) {
204 				symbuf[DIP(dp, di_size)] = 0;
205 				printf("convert symlink %d(%s) of size %llu\n",
206 					inumber, symbuf,
207 					(unsigned long long)DIP(dp, di_size));
208 			}
209 			dp = ginode(inumber);
210 			memcpy(dp->dp1.di_shortlink, symbuf,
211 			    (long)DIP(dp, di_size));
212 			DIP_SET(dp, di_blocks, 0);
213 			inodirty();
214 		}
215 		/*
216 		 * Fake ndb value so direct/indirect block checks below
217 		 * will detect any garbage after symlink string.
218 		 */
219 		if (DIP(dp, di_size) < sblock.fs_maxsymlinklen ||
220 		    (sblock.fs_maxsymlinklen == 0 && DIP(dp, di_blocks) == 0)) {
221 			if (sblock.fs_magic == FS_UFS1_MAGIC)
222 				ndb = howmany(DIP(dp, di_size),
223 				    sizeof(int32_t));
224 			else
225 				ndb = howmany(DIP(dp, di_size),
226 				    sizeof(int64_t));
227 			if (ndb > NDADDR) {
228 				j = ndb - NDADDR;
229 				for (ndb = 1; j > 1; j--)
230 					ndb *= NINDIR(&sblock);
231 				ndb += NDADDR;
232 			}
233 		}
234 	}
235 	for (j = ndb; j < NDADDR; j++)
236 		if (DIP(dp, di_db[j]) != 0) {
237 			if (debug)
238 				printf("bad direct addr: %ld\n",
239 				    (long)DIP(dp, di_db[j]));
240 			goto unknown;
241 		}
242 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
243 		ndb /= NINDIR(&sblock);
244 	for (; j < NIADDR; j++)
245 		if (DIP(dp, di_ib[j]) != 0) {
246 			if (debug)
247 				printf("bad indirect addr: %ld\n",
248 				    (long)DIP(dp, di_ib[j]));
249 			goto unknown;
250 		}
251 	if (ftypeok(dp) == 0)
252 		goto unknown;
253 	n_files++;
254 	lncntp[inumber] = DIP(dp, di_nlink);
255 	if (DIP(dp, di_nlink) <= 0) {
256 		zlnp =  malloc(sizeof *zlnp);
257 		if (zlnp == NULL) {
258 			pfatal("LINK COUNT TABLE OVERFLOW");
259 			if (reply("CONTINUE") == 0) {
260 				ckfini(0);
261 				errexit("%s", "");
262 			}
263 		} else {
264 			zlnp->zlncnt = inumber;
265 			zlnp->next = zlnhead;
266 			zlnhead = zlnp;
267 		}
268 	}
269 	if (mode == IFDIR) {
270 		if (DIP(dp, di_size) == 0)
271 			SET_ISTATE(inumber, DCLEAR);
272 		else
273 			SET_ISTATE(inumber, DSTATE);
274 		cacheino(dp, inumber);
275 	} else
276 		SET_ISTATE(inumber, FSTATE);
277 	SET_ITYPE(inumber, IFTODT(mode));
278 	if (sblock.fs_magic == FS_UFS1_MAGIC && doinglevel2 &&
279 	   (dp->dp1.di_ouid != (u_short)-1 ||
280 	    dp->dp1.di_ogid != (u_short)-1)) {
281 		dp = ginode(inumber);
282 		DIP_SET(dp, di_uid, dp->dp1.di_ouid);
283 		dp->dp1.di_ouid = -1;
284 		DIP_SET(dp, di_gid, dp->dp1.di_ogid);
285 		dp->dp1.di_ogid = -1;
286 		inodirty();
287 	}
288 	badblk = dupblk = 0;
289 	idesc->id_number = inumber;
290 	(void)ckinode(dp, idesc);
291 	idesc->id_entryno *= btodb(sblock.fs_fsize);
292 	if (DIP(dp, di_blocks) != idesc->id_entryno) {
293 		pwarn("INCORRECT BLOCK COUNT I=%u (%ld should be %d)",
294 		    inumber, (long)DIP(dp, di_blocks), idesc->id_entryno);
295 		if (preen)
296 			printf(" (CORRECTED)\n");
297 		else if (reply("CORRECT") == 0)
298 			return;
299 		dp = ginode(inumber);
300 		DIP_SET(dp, di_blocks, idesc->id_entryno);
301 		inodirty();
302 	}
303 	return;
304 unknown:
305 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
306 	SET_ISTATE(inumber, FCLEAR);
307 	if (reply("CLEAR") == 1) {
308 		SET_ISTATE(inumber, USTATE);
309 		dp = ginode(inumber);
310 		clearinode(dp);
311 		inodirty();
312 	}
313 }
314 
315 int
316 pass1check(struct inodesc *idesc)
317 {
318 	int res = KEEPON;
319 	int anyout, nfrags;
320 	daddr64_t blkno = idesc->id_blkno;
321 	struct dups *dlp;
322 	struct dups *new;
323 
324 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
325 		blkerror(idesc->id_number, "BAD", blkno);
326 		if (badblk++ >= MAXBAD) {
327 			pwarn("EXCESSIVE BAD BLKS I=%u",
328 				idesc->id_number);
329 			if (preen)
330 				printf(" (SKIPPING)\n");
331 			else if (reply("CONTINUE") == 0) {
332 				ckfini(0);
333 				errexit("%s", "");
334 			}
335 			return (STOP);
336 		}
337 	}
338 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
339 		if (anyout && chkrange(blkno, 1)) {
340 			res = SKIP;
341 		} else if (!testbmap(blkno)) {
342 			n_blks++;
343 			setbmap(blkno);
344 		} else {
345 			blkerror(idesc->id_number, "DUP", blkno);
346 			if (dupblk++ >= MAXDUP) {
347 				pwarn("EXCESSIVE DUP BLKS I=%u",
348 					idesc->id_number);
349 				if (preen)
350 					printf(" (SKIPPING)\n");
351 				else if (reply("CONTINUE") == 0) {
352 					ckfini(0);
353 					errexit("%s", "");
354 				}
355 				return (STOP);
356 			}
357 			new = malloc(sizeof(struct dups));
358 			if (new == NULL) {
359 				pfatal("DUP TABLE OVERFLOW.");
360 				if (reply("CONTINUE") == 0) {
361 					ckfini(0);
362 					errexit("%s", "");
363 				}
364 				return (STOP);
365 			}
366 			new->dup = blkno;
367 			if (muldup == 0) {
368 				duplist = muldup = new;
369 				new->next = 0;
370 			} else {
371 				new->next = muldup->next;
372 				muldup->next = new;
373 			}
374 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
375 				if (dlp->dup == blkno)
376 					break;
377 			if (dlp == muldup && dlp->dup != blkno)
378 				muldup = new;
379 		}
380 		/*
381 		 * count the number of blocks found in id_entryno
382 		 */
383 		idesc->id_entryno++;
384 	}
385 	return (res);
386 }
387