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