xref: /freebsd/sbin/fsck_ffs/pass1.c (revision 71fe318b852b8dfb3e799cb12ef184750f7f8eac)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. 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 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)pass1.c	8.6 (Berkeley) 4/28/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/sysctl.h>
45 
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ufs/dir.h>
48 #include <ufs/ffs/fs.h>
49 
50 #include <err.h>
51 #include <limits.h>
52 #include <stdint.h>
53 #include <string.h>
54 
55 #include "fsck.h"
56 
57 static ufs2_daddr_t badblk;
58 static ufs2_daddr_t dupblk;
59 static ino_t lastino;		/* last inode in use */
60 
61 static void checkinode(ino_t inumber, struct inodesc *);
62 
63 void
64 pass1(void)
65 {
66 	struct inostat *info;
67 	struct inodesc idesc;
68 	ino_t inumber, inosused;
69 	ufs2_daddr_t i, cgd;
70 	u_int8_t *cp;
71 	int c;
72 
73 	/*
74 	 * Set file system reserved blocks in used block map.
75 	 */
76 	for (c = 0; c < sblock.fs_ncg; c++) {
77 		cgd = cgdmin(&sblock, c);
78 		if (c == 0) {
79 			i = cgbase(&sblock, c);
80 		} else
81 			i = cgsblock(&sblock, c);
82 		for (; i < cgd; i++)
83 			setbmap(i);
84 	}
85 	i = sblock.fs_csaddr;
86 	cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
87 	for (; i < cgd; i++)
88 		setbmap(i);
89 
90 	/*
91 	 * Find all allocated blocks.
92 	 */
93 	memset(&idesc, 0, sizeof(struct inodesc));
94 	idesc.id_func = pass1check;
95 	n_files = n_blks = 0;
96 	for (c = 0; c < sblock.fs_ncg; c++) {
97 		inumber = c * sblock.fs_ipg;
98 		setinodebuf(inumber);
99 		getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
100 		if (sblock.fs_magic == FS_UFS2_MAGIC)
101 			inosused = cgrp.cg_initediblk;
102 		else
103 			inosused = sblock.fs_ipg;
104 		if (got_siginfo) {
105 			printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
106 			    cdevname, c, sblock.fs_ncg,
107 			    c * 100 / sblock.fs_ncg);
108 			got_siginfo = 0;
109 		}
110 		/*
111 		 * If we are using soft updates, then we can trust the
112 		 * cylinder group inode allocation maps to tell us which
113 		 * inodes are allocated. We will scan the used inode map
114 		 * to find the inodes that are really in use, and then
115 		 * read only those inodes in from disk.
116 		 */
117 		if (preen && usedsoftdep) {
118 			if (!cg_chkmagic(&cgrp))
119 				pfatal("CG %d: BAD MAGIC NUMBER\n", c);
120 			cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
121 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
122 				if (*cp == 0)
123 					continue;
124 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
125 					if (*cp & i)
126 						break;
127 					inosused--;
128 				}
129 				break;
130 			}
131 			if (inosused < 0)
132 				inosused = 0;
133 		}
134 		/*
135 		 * Allocate inoinfo structures for the allocated inodes.
136 		 */
137 		inostathead[c].il_numalloced = inosused;
138 		if (inosused == 0) {
139 			inostathead[c].il_stat = 0;
140 			continue;
141 		}
142 		info = calloc((unsigned)inosused, sizeof(struct inostat));
143 		if (info == NULL)
144 			pfatal("cannot alloc %u bytes for inoinfo\n",
145 			    (unsigned)(sizeof(struct inostat) * inosused));
146 		inostathead[c].il_stat = info;
147 		/*
148 		 * Scan the allocated inodes.
149 		 */
150 		for (i = 0; i < inosused; i++, inumber++) {
151 			if (inumber < ROOTINO) {
152 				(void)getnextinode(inumber);
153 				continue;
154 			}
155 			checkinode(inumber, &idesc);
156 		}
157 		lastino += 1;
158 		if (inosused < sblock.fs_ipg || inumber == lastino)
159 			continue;
160 		/*
161 		 * If we were not able to determine in advance which inodes
162 		 * were in use, then reduce the size of the inoinfo structure
163 		 * to the size necessary to describe the inodes that we
164 		 * really found.
165 		 */
166 		if (lastino < (c * sblock.fs_ipg))
167 			inosused = 0;
168 		else
169 			inosused = lastino - (c * sblock.fs_ipg);
170 		inostathead[c].il_numalloced = inosused;
171 		if (inosused == 0) {
172 			free(inostathead[c].il_stat);
173 			inostathead[c].il_stat = 0;
174 			continue;
175 		}
176 		info = calloc((unsigned)inosused, sizeof(struct inostat));
177 		if (info == NULL)
178 			pfatal("cannot alloc %u bytes for inoinfo\n",
179 			    (unsigned)(sizeof(struct inostat) * inosused));
180 		memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
181 		free(inostathead[c].il_stat);
182 		inostathead[c].il_stat = info;
183 	}
184 	freeinodebuf();
185 }
186 
187 static void
188 checkinode(ino_t inumber, struct inodesc *idesc)
189 {
190 	union dinode *dp;
191 	struct zlncnt *zlnp;
192 	off_t kernmaxfilesize;
193 	ufs2_daddr_t ndb;
194 	mode_t mode;
195 	int j, ret, offset;
196 
197 	dp = getnextinode(inumber);
198 	mode = DIP(dp, di_mode) & IFMT;
199 	if (mode == 0) {
200 		if ((sblock.fs_magic == FS_UFS1_MAGIC &&
201 		     (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
202 			NDADDR * sizeof(ufs1_daddr_t)) ||
203 		      memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
204 			NIADDR * sizeof(ufs1_daddr_t)) ||
205 		      dp->dp1.di_mode || dp->dp1.di_size)) ||
206 		    (sblock.fs_magic == FS_UFS2_MAGIC &&
207 		     (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
208 			NDADDR * sizeof(ufs2_daddr_t)) ||
209 		      memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
210 			NIADDR * sizeof(ufs2_daddr_t)) ||
211 		      dp->dp2.di_mode || dp->dp2.di_size))) {
212 			pfatal("PARTIALLY ALLOCATED INODE I=%lu",
213 			    (u_long)inumber);
214 			if (reply("CLEAR") == 1) {
215 				dp = ginode(inumber);
216 				clearinode(dp);
217 				inodirty();
218 			}
219 		}
220 		inoinfo(inumber)->ino_state = USTATE;
221 		return;
222 	}
223 	lastino = inumber;
224 	/* This should match the file size limit in ffs_mountfs(). */
225 	if (sblock.fs_magic == FS_UFS1_MAGIC)
226 		kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
227 	else
228 		kernmaxfilesize = sblock.fs_maxfilesize;
229 	if (DIP(dp, di_size) > kernmaxfilesize ||
230 	    DIP(dp, di_size) > sblock.fs_maxfilesize ||
231 	    (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
232 		if (debug)
233 			printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
234 		goto unknown;
235 	}
236 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
237 		dp = ginode(inumber);
238 		DIP(dp, di_size) = sblock.fs_fsize;
239 		DIP(dp, di_mode) = IFREG|0600;
240 		inodirty();
241 	}
242 	if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
243 	     mode == IFSOCK) && DIP(dp, di_size) != 0) {
244 		if (debug)
245 			printf("bad special-file size %ju:",
246 			    (uintmax_t)DIP(dp, di_size));
247 		goto unknown;
248 	}
249 	if ((mode == IFBLK || mode == IFCHR) &&
250 	    (dev_t)DIP(dp, di_rdev) == NODEV) {
251 		if (debug)
252 			printf("bad special-file rdev NODEV:");
253 		goto unknown;
254 	}
255 	ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
256 	if (ndb < 0) {
257 		if (debug)
258 			printf("bad size %ju ndb %ju:",
259 				(uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
260 		goto unknown;
261 	}
262 	if (mode == IFBLK || mode == IFCHR)
263 		ndb++;
264 	if (mode == IFLNK) {
265 		/*
266 		 * Fake ndb value so direct/indirect block checks below
267 		 * will detect any garbage after symlink string.
268 		 */
269 		if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
270 			if (sblock.fs_magic == FS_UFS1_MAGIC)
271 				ndb = howmany(DIP(dp, di_size),
272 				    sizeof(ufs1_daddr_t));
273 			else
274 				ndb = howmany(DIP(dp, di_size),
275 				    sizeof(ufs2_daddr_t));
276 			if (ndb > NDADDR) {
277 				j = ndb - NDADDR;
278 				for (ndb = 1; j > 1; j--)
279 					ndb *= NINDIR(&sblock);
280 				ndb += NDADDR;
281 			}
282 		}
283 	}
284 	for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
285 		if (DIP(dp, di_db[j]) != 0) {
286 			if (debug)
287 				printf("bad direct addr[%d]: %ju\n", j,
288 				    (uintmax_t)DIP(dp, di_db[j]));
289 			goto unknown;
290 		}
291 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
292 		ndb /= NINDIR(&sblock);
293 	for (; j < NIADDR; j++)
294 		if (DIP(dp, di_ib[j]) != 0) {
295 			if (debug)
296 				printf("bad indirect addr: %ju\n",
297 				    (uintmax_t)DIP(dp, di_ib[j]));
298 			goto unknown;
299 		}
300 	if (ftypeok(dp) == 0)
301 		goto unknown;
302 	n_files++;
303 	inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
304 	if (DIP(dp, di_nlink) <= 0) {
305 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
306 		if (zlnp == NULL) {
307 			pfatal("LINK COUNT TABLE OVERFLOW");
308 			if (reply("CONTINUE") == 0) {
309 				ckfini(0);
310 				exit(EEXIT);
311 			}
312 		} else {
313 			zlnp->zlncnt = inumber;
314 			zlnp->next = zlnhead;
315 			zlnhead = zlnp;
316 		}
317 	}
318 	if (mode == IFDIR) {
319 		if (DIP(dp, di_size) == 0)
320 			inoinfo(inumber)->ino_state = DCLEAR;
321 		else
322 			inoinfo(inumber)->ino_state = DSTATE;
323 		cacheino(dp, inumber);
324 		countdirs++;
325 	} else
326 		inoinfo(inumber)->ino_state = FSTATE;
327 	inoinfo(inumber)->ino_type = IFTODT(mode);
328 	badblk = dupblk = 0;
329 	idesc->id_number = inumber;
330 	if (DIP(dp, di_flags) & SF_SNAPSHOT)
331 		idesc->id_type = SNAP;
332 	else
333 		idesc->id_type = ADDR;
334 	(void)ckinode(dp, idesc);
335 	if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
336 		idesc->id_type = ADDR;
337 		ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
338 		for (j = 0; j < NXADDR; j++) {
339 			if (--ndb == 0 &&
340 			    (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
341 				idesc->id_numfrags = numfrags(&sblock,
342 				    fragroundup(&sblock, offset));
343 			else
344 				idesc->id_numfrags = sblock.fs_frag;
345 			if (dp->dp2.di_extb[j] == 0)
346 				continue;
347 			idesc->id_blkno = dp->dp2.di_extb[j];
348 			ret = (*idesc->id_func)(idesc);
349 			if (ret & STOP)
350 				break;
351 		}
352 	}
353 	if (sblock.fs_magic == FS_UFS2_MAGIC)
354 		eascan(idesc, &dp->dp2);
355 	idesc->id_entryno *= btodb(sblock.fs_fsize);
356 	if (DIP(dp, di_blocks) != idesc->id_entryno) {
357 		pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
358 		    (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
359 		    (uintmax_t)idesc->id_entryno);
360 		if (preen)
361 			printf(" (CORRECTED)\n");
362 		else if (reply("CORRECT") == 0)
363 			return;
364 		if (bkgrdflag == 0) {
365 			dp = ginode(inumber);
366 			DIP(dp, di_blocks) = idesc->id_entryno;
367 			inodirty();
368 		} else {
369 			cmd.value = idesc->id_number;
370 			cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
371 			if (debug)
372 				printf("adjblkcnt ino %ju amount %lld\n",
373 				    (uintmax_t)cmd.value, (long long)cmd.size);
374 			if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
375 			    &cmd, sizeof cmd) == -1)
376 				rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
377 		}
378 	}
379 	return;
380 unknown:
381 	pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
382 	inoinfo(inumber)->ino_state = FCLEAR;
383 	if (reply("CLEAR") == 1) {
384 		inoinfo(inumber)->ino_state = USTATE;
385 		dp = ginode(inumber);
386 		clearinode(dp);
387 		inodirty();
388 	}
389 }
390 
391 int
392 pass1check(struct inodesc *idesc)
393 {
394 	int res = KEEPON;
395 	int anyout, nfrags;
396 	ufs2_daddr_t blkno = idesc->id_blkno;
397 	struct dups *dlp;
398 	struct dups *new;
399 
400 	if (idesc->id_type == SNAP) {
401 		if (blkno == BLK_NOCOPY)
402 			return (KEEPON);
403 		if (idesc->id_number == cursnapshot) {
404 			if (blkno == blkstofrags(&sblock, idesc->id_lbn))
405 				return (KEEPON);
406 			if (blkno == BLK_SNAP) {
407 				blkno = blkstofrags(&sblock, idesc->id_lbn);
408 				idesc->id_entryno -= idesc->id_numfrags;
409 			}
410 		} else {
411 			if (blkno == BLK_SNAP)
412 				return (KEEPON);
413 		}
414 	}
415 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
416 		blkerror(idesc->id_number, "BAD", blkno);
417 		if (badblk++ >= MAXBAD) {
418 			pwarn("EXCESSIVE BAD BLKS I=%lu",
419 			    (u_long)idesc->id_number);
420 			if (preen)
421 				printf(" (SKIPPING)\n");
422 			else if (reply("CONTINUE") == 0) {
423 				ckfini(0);
424 				exit(EEXIT);
425 			}
426 			return (STOP);
427 		}
428 	}
429 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
430 		if (anyout && chkrange(blkno, 1)) {
431 			res = SKIP;
432 		} else if (!testbmap(blkno)) {
433 			n_blks++;
434 			setbmap(blkno);
435 		} else {
436 			blkerror(idesc->id_number, "DUP", blkno);
437 			if (dupblk++ >= MAXDUP) {
438 				pwarn("EXCESSIVE DUP BLKS I=%lu",
439 					(u_long)idesc->id_number);
440 				if (preen)
441 					printf(" (SKIPPING)\n");
442 				else if (reply("CONTINUE") == 0) {
443 					ckfini(0);
444 					exit(EEXIT);
445 				}
446 				return (STOP);
447 			}
448 			new = (struct dups *)malloc(sizeof(struct dups));
449 			if (new == NULL) {
450 				pfatal("DUP TABLE OVERFLOW.");
451 				if (reply("CONTINUE") == 0) {
452 					ckfini(0);
453 					exit(EEXIT);
454 				}
455 				return (STOP);
456 			}
457 			new->dup = blkno;
458 			if (muldup == 0) {
459 				duplist = muldup = new;
460 				new->next = 0;
461 			} else {
462 				new->next = muldup->next;
463 				muldup->next = new;
464 			}
465 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
466 				if (dlp->dup == blkno)
467 					break;
468 			if (dlp == muldup && dlp->dup != blkno)
469 				muldup = new;
470 		}
471 		/*
472 		 * count the number of blocks found in id_entryno
473 		 */
474 		idesc->id_entryno++;
475 	}
476 	return (res);
477 }
478