xref: /dragonfly/sbin/fsck/pass1.c (revision d5f516c3)
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  * @(#)pass1.c	8.6 (Berkeley) 4/28/95
34  * $FreeBSD: src/sbin/fsck/pass1.c,v 1.16.2.5 2002/06/23 22:34:58 iedowse Exp $
35  * $DragonFly: src/sbin/fsck/pass1.c,v 1.5 2003/11/01 17:15:58 drhodus Exp $
36  */
37 
38 #include <sys/param.h>
39 
40 #include <vfs/ufs/dinode.h>
41 #include <vfs/ufs/dir.h>
42 #include <vfs/ufs/fs.h>
43 
44 #include <err.h>
45 #include <string.h>
46 
47 #include "fsck.h"
48 
49 static ufs_daddr_t badblk;
50 static ufs_daddr_t dupblk;
51 static ino_t lastino;		/* last inode in use */
52 
53 static void checkinode(ino_t inumber, struct inodesc *);
54 
55 void
56 pass1(void)
57 {
58 	u_int8_t *cp;
59 	ino_t inumber;
60 	int c, i, cgd, inosused;
61 	struct inostat *info;
62 	struct inodesc idesc;
63 
64 	/*
65 	 * Set file system reserved blocks in used block map.
66 	 */
67 	for (c = 0; c < sblock.fs_ncg; c++) {
68 		cgd = cgdmin(&sblock, c);
69 		if (c == 0) {
70 			i = cgbase(&sblock, c);
71 		} else
72 			i = cgsblock(&sblock, c);
73 		for (; i < cgd; i++)
74 			setbmap(i);
75 	}
76 	i = sblock.fs_csaddr;
77 	cgd = i+ howmany(sblock.fs_cssize, sblock.fs_fsize);
78 	for (; i < cgd; i++)
79 		setbmap(i);
80 	/*
81 	 * Find all allocated blocks.
82 	 */
83 	memset(&idesc, 0, sizeof(struct inodesc));
84 	idesc.id_type = ADDR;
85 	idesc.id_func = pass1check;
86 	n_files = n_blks = 0;
87 	for (c = 0; c < sblock.fs_ncg; c++) {
88 		inumber = c * sblock.fs_ipg;
89 		setinodebuf(inumber);
90 		inosused = sblock.fs_ipg;
91 		if (got_siginfo) {
92 			printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
93 			    cdevname, c, sblock.fs_ncg,
94 			    c * 100 / sblock.fs_ncg);
95 			got_siginfo = 0;
96 		}
97 		/*
98 		 * If we are using soft updates, then we can trust the
99 		 * cylinder group inode allocation maps to tell us which
100 		 * inodes are allocated. We will scan the used inode map
101 		 * to find the inodes that are really in use, and then
102 		 * read only those inodes in from disk.
103 		 */
104 		if (preen && usedsoftdep) {
105 			getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
106 			if (!cg_chkmagic(&cgrp))
107 				pfatal("CG %d: BAD MAGIC NUMBER\n", c);
108 			cp = &cg_inosused(&cgrp)[(sblock.fs_ipg - 1) / NBBY];
109 			for ( ; inosused > 0; inosused -= NBBY, cp--) {
110 				if (*cp == 0)
111 					continue;
112 				for (i = 1 << (NBBY - 1); i > 0; i >>= 1) {
113 					if (*cp & i)
114 						break;
115 					inosused--;
116 				}
117 				break;
118 			}
119 			if (inosused < 0)
120 				inosused = 0;
121 		}
122 		/*
123 		 * Allocate inoinfo structures for the allocated inodes.
124 		 */
125 		inostathead[c].il_numalloced = inosused;
126 		if (inosused == 0) {
127 			inostathead[c].il_stat = 0;
128 			continue;
129 		}
130 		info = calloc((unsigned)inosused, sizeof(struct inostat));
131 		if (info == NULL)
132 			pfatal("cannot alloc %u bytes for inoinfo\n",
133 			    (unsigned)(sizeof(struct inostat) * inosused));
134 		inostathead[c].il_stat = info;
135 		/*
136 		 * Scan the allocated inodes.
137 		 */
138 		for (i = 0; i < inosused; i++, inumber++) {
139 			if (inumber < ROOTINO) {
140 				(void)getnextinode(inumber);
141 				continue;
142 			}
143 			checkinode(inumber, &idesc);
144 		}
145 		lastino += 1;
146 		if (inosused < sblock.fs_ipg || inumber == lastino)
147 			continue;
148 		/*
149 		 * If we were not able to determine in advance which inodes
150 		 * were in use, then reduce the size of the inoinfo structure
151 		 * to the size necessary to describe the inodes that we
152 		 * really found.
153 		 */
154 		inosused = lastino - (c * sblock.fs_ipg);
155 		if (inosused < 0)
156 			inosused = 0;
157 		inostathead[c].il_numalloced = inosused;
158 		if (inosused == 0) {
159 			free(inostathead[c].il_stat);
160 			inostathead[c].il_stat = 0;
161 			continue;
162 		}
163 		info = calloc((unsigned)inosused, sizeof(struct inostat));
164 		if (info == NULL)
165 			pfatal("cannot alloc %u bytes for inoinfo\n",
166 			    (unsigned)(sizeof(struct inostat) * inosused));
167 		memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
168 		free(inostathead[c].il_stat);
169 		inostathead[c].il_stat = info;
170 	}
171 	freeinodebuf();
172 }
173 
174 static void
175 checkinode(inumber, idesc)
176 	ino_t inumber;
177 	register struct inodesc *idesc;
178 {
179 	register struct dinode *dp;
180 	struct zlncnt *zlnp;
181 	u_int64_t kernmaxfilesize;
182 	ufs_daddr_t ndb, j;
183 	mode_t mode;
184 	char *symbuf;
185 
186 	dp = getnextinode(inumber);
187 	mode = dp->di_mode & IFMT;
188 	if (mode == 0) {
189 		if (memcmp(dp->di_db, zino.di_db,
190 			NDADDR * sizeof(ufs_daddr_t)) ||
191 		    memcmp(dp->di_ib, zino.di_ib,
192 			NIADDR * sizeof(ufs_daddr_t)) ||
193 		    dp->di_mode || dp->di_size) {
194 			pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
195 			if (reply("CLEAR") == 1) {
196 				dp = ginode(inumber);
197 				clearinode(dp);
198 				inodirty();
199 			}
200 		}
201 		inoinfo(inumber)->ino_state = USTATE;
202 		return;
203 	}
204 	lastino = inumber;
205 	/* This should match the file size limit in ffs_mountfs(). */
206 	kernmaxfilesize = (u_int64_t)0x40000000 * sblock.fs_bsize - 1;
207 	if (kernmaxfilesize > (u_int64_t)0x80000000u * PAGE_SIZE - 1)
208 		kernmaxfilesize = (u_int64_t)0x80000000u * PAGE_SIZE - 1;
209 	if (dp->di_size > kernmaxfilesize ||
210 	    dp->di_size > sblock.fs_maxfilesize ||
211 	    (mode == IFDIR && dp->di_size > MAXDIRSIZE)) {
212 		if (debug)
213 			printf("bad size %qu:", dp->di_size);
214 		goto unknown;
215 	}
216 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
217 		dp = ginode(inumber);
218 		dp->di_size = sblock.fs_fsize;
219 		dp->di_mode = IFREG|0600;
220 		inodirty();
221 	}
222 	if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
223 	     mode == IFSOCK) && dp->di_size != 0) {
224 		if (debug)
225 			printf("bad special-file size %qu:", dp->di_size);
226 		goto unknown;
227 	}
228 	ndb = howmany(dp->di_size, sblock.fs_bsize);
229 	if (ndb < 0) {
230 		if (debug)
231 			printf("bad size %qu ndb %d:",
232 				dp->di_size, ndb);
233 		goto unknown;
234 	}
235 	if (mode == IFBLK || mode == IFCHR)
236 		ndb++;
237 	if (mode == IFLNK) {
238 		if (doinglevel2 &&
239 		    dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
240 		    dp->di_blocks != 0) {
241 			symbuf = alloca(secsize);
242 			if (bread(fsreadfd, symbuf,
243 			    fsbtodb(&sblock, dp->di_db[0]),
244 			    (long)secsize) != 0)
245 				errx(EEXIT, "cannot read symlink");
246 			if (debug) {
247 				symbuf[dp->di_size] = 0;
248 				printf("convert symlink %lu(%s) of size %ld\n",
249 				    (u_long)inumber, symbuf, (long)dp->di_size);
250 			}
251 			dp = ginode(inumber);
252 			memmove(dp->di_shortlink, symbuf, (long)dp->di_size);
253 			dp->di_blocks = 0;
254 			inodirty();
255 		}
256 		/*
257 		 * Fake ndb value so direct/indirect block checks below
258 		 * will detect any garbage after symlink string.
259 		 */
260 		if (dp->di_size < sblock.fs_maxsymlinklen) {
261 			ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
262 			if (ndb > NDADDR) {
263 				j = ndb - NDADDR;
264 				for (ndb = 1; j > 1; j--)
265 					ndb *= NINDIR(&sblock);
266 				ndb += NDADDR;
267 			}
268 		}
269 	}
270 	for (j = ndb; j < NDADDR; j++)
271 		if (dp->di_db[j] != 0) {
272 			if (debug)
273 				printf("bad direct addr: %ld\n",
274 				    (long)dp->di_db[j]);
275 			goto unknown;
276 		}
277 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
278 		ndb /= NINDIR(&sblock);
279 	for (; j < NIADDR; j++)
280 		if (dp->di_ib[j] != 0) {
281 			if (debug)
282 				printf("bad indirect addr: %ld\n",
283 				    (long)dp->di_ib[j]);
284 			goto unknown;
285 		}
286 	if (ftypeok(dp) == 0)
287 		goto unknown;
288 	n_files++;
289 	inoinfo(inumber)->ino_linkcnt = dp->di_nlink;
290 	if (dp->di_nlink <= 0) {
291 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
292 		if (zlnp == NULL) {
293 			pfatal("LINK COUNT TABLE OVERFLOW");
294 			if (reply("CONTINUE") == 0) {
295 				ckfini(0);
296 				exit(EEXIT);
297 			}
298 		} else {
299 			zlnp->zlncnt = inumber;
300 			zlnp->next = zlnhead;
301 			zlnhead = zlnp;
302 		}
303 	}
304 	if (mode == IFDIR) {
305 		if (dp->di_size == 0)
306 			inoinfo(inumber)->ino_state = DCLEAR;
307 		else
308 			inoinfo(inumber)->ino_state = DSTATE;
309 		cacheino(dp, inumber);
310 		countdirs++;
311 	} else
312 		inoinfo(inumber)->ino_state = FSTATE;
313 	inoinfo(inumber)->ino_type = IFTODT(mode);
314 	if (doinglevel2 &&
315 	    (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
316 		dp = ginode(inumber);
317 		dp->di_uid = dp->di_ouid;
318 		dp->di_ouid = -1;
319 		dp->di_gid = dp->di_ogid;
320 		dp->di_ogid = -1;
321 		inodirty();
322 	}
323 	badblk = dupblk = 0;
324 	idesc->id_number = inumber;
325 	(void)ckinode(dp, idesc);
326 	idesc->id_entryno *= btodb(sblock.fs_fsize);
327 	if (dp->di_blocks != idesc->id_entryno) {
328 		pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
329 		    inumber, dp->di_blocks, idesc->id_entryno);
330 		if (preen)
331 			printf(" (CORRECTED)\n");
332 		else if (reply("CORRECT") == 0)
333 			return;
334 		dp = ginode(inumber);
335 		dp->di_blocks = idesc->id_entryno;
336 		inodirty();
337 	}
338 	return;
339 unknown:
340 	pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
341 	inoinfo(inumber)->ino_state = FCLEAR;
342 	if (reply("CLEAR") == 1) {
343 		inoinfo(inumber)->ino_state = USTATE;
344 		dp = ginode(inumber);
345 		clearinode(dp);
346 		inodirty();
347 	}
348 }
349 
350 int
351 pass1check(register struct inodesc *idesc)
352 {
353 	int res = KEEPON;
354 	int anyout, nfrags;
355 	ufs_daddr_t blkno = idesc->id_blkno;
356 	register struct dups *dlp;
357 	struct dups *new;
358 
359 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
360 		blkerror(idesc->id_number, "BAD", blkno);
361 		if (badblk++ >= MAXBAD) {
362 			pwarn("EXCESSIVE BAD BLKS I=%lu",
363 				idesc->id_number);
364 			if (preen)
365 				printf(" (SKIPPING)\n");
366 			else if (reply("CONTINUE") == 0) {
367 				ckfini(0);
368 				exit(EEXIT);
369 			}
370 			return (STOP);
371 		}
372 	}
373 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
374 		if (anyout && chkrange(blkno, 1)) {
375 			res = SKIP;
376 		} else if (!testbmap(blkno)) {
377 			n_blks++;
378 			setbmap(blkno);
379 		} else {
380 			blkerror(idesc->id_number, "DUP", blkno);
381 			if (dupblk++ >= MAXDUP) {
382 				pwarn("EXCESSIVE DUP BLKS I=%lu",
383 					idesc->id_number);
384 				if (preen)
385 					printf(" (SKIPPING)\n");
386 				else if (reply("CONTINUE") == 0) {
387 					ckfini(0);
388 					exit(EEXIT);
389 				}
390 				return (STOP);
391 			}
392 			new = (struct dups *)malloc(sizeof(struct dups));
393 			if (new == NULL) {
394 				pfatal("DUP TABLE OVERFLOW.");
395 				if (reply("CONTINUE") == 0) {
396 					ckfini(0);
397 					exit(EEXIT);
398 				}
399 				return (STOP);
400 			}
401 			new->dup = blkno;
402 			if (muldup == 0) {
403 				duplist = muldup = new;
404 				new->next = 0;
405 			} else {
406 				new->next = muldup->next;
407 				muldup->next = new;
408 			}
409 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
410 				if (dlp->dup == blkno)
411 					break;
412 			if (dlp == muldup && dlp->dup != blkno)
413 				muldup = new;
414 		}
415 		/*
416 		 * count the number of blocks found in id_entryno
417 		 */
418 		idesc->id_entryno++;
419 	}
420 	return (res);
421 }
422