xref: /netbsd-src/sbin/fsck_lfs/pass1.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /* $NetBSD: pass1.c,v 1.23 2005/09/13 04:14:17 christos Exp $	 */
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/mount.h>
35 #include <sys/buf.h>
36 
37 #include <ufs/ufs/inode.h>
38 #include <ufs/ufs/dir.h>
39 #define vnode uvnode
40 #include <ufs/lfs/lfs.h>
41 #undef vnode
42 
43 #include <err.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include <signal.h>
49 
50 #include "bufcache.h"
51 #include "vnode.h"
52 #include "lfs_user.h"
53 
54 #include "fsck.h"
55 #include "extern.h"
56 #include "fsutil.h"
57 
58 SEGUSE *seg_table;
59 extern ufs_daddr_t *din_table;
60 
61 ufs_daddr_t badblk;
62 static ufs_daddr_t dupblk;
63 static int i_d_cmp(const void *, const void *);
64 
65 struct ino_daddr {
66 	ino_t ino;
67 	ufs_daddr_t daddr;
68 };
69 
70 static int
71 i_d_cmp(const void *va, const void *vb)
72 {
73 	const struct ino_daddr *a, *b;
74 
75 	a = *((const struct ino_daddr *const *) va);
76 	b = *((const struct ino_daddr *const *) vb);
77 
78 	if (a->daddr == b->daddr) {
79 		return (a->ino - b->ino);
80 	}
81 	if (a->daddr > b->daddr) {
82 		return 1;
83 	}
84 	return -1;
85 }
86 
87 void
88 pass1(void)
89 {
90 	ino_t inumber;
91 	int i;
92 	struct inodesc idesc;
93 	struct ufs1_dinode *tinode;
94 	struct ifile *ifp;
95 	struct ubuf *bp;
96 	struct ino_daddr **dins;
97 
98 	/*
99 	 * Find all allocated blocks, initialize numdirs.
100 	 */
101 	memset(&idesc, 0, sizeof(struct inodesc));
102 	idesc.id_type = ADDR;
103 	idesc.id_func = pass1check;
104 	idesc.id_lblkno = 0;
105 	inumber = 0;
106 	n_files = n_blks = 0;
107 
108 	if (debug)
109 		printf("creating sorted inode address table...\n");
110 	/* Sort by daddr */
111 	dins = (struct ino_daddr **) malloc(maxino * sizeof(*dins));
112 	for (i = 0; i < maxino; i++) {
113 		dins[i] = malloc(sizeof(**dins));
114 		dins[i]->ino = i;
115 		if (i == fs->lfs_ifile)
116 			dins[i]->daddr = fs->lfs_idaddr;
117 		else {
118 			LFS_IENTRY(ifp, fs, i, bp);
119 			dins[i]->daddr = ifp->if_daddr;
120 			brelse(bp);
121 		}
122 	}
123 	qsort(dins, maxino, sizeof(*dins), i_d_cmp);
124 
125 	/* find a value for numdirs, fill in din_table */
126 	if (debug)
127 		printf("counting dirs...\n");
128 	numdirs = 0;
129 	for (i = 0; i < maxino; i++) {
130 		inumber = dins[i]->ino;
131 		if (inumber == 0 || dins[i]->daddr == 0)
132 			continue;
133 		tinode = ginode(inumber);
134 		if (tinode && (tinode->di_mode & IFMT) == IFDIR)
135 			numdirs++;
136 	}
137 
138 	/* from setup.c */
139 	inplast = 0;
140 	listmax = numdirs + 10;
141 	inpsort = (struct inoinfo **) calloc((unsigned) listmax,
142 	    sizeof(struct inoinfo *));
143 	inphead = (struct inoinfo **) calloc((unsigned) numdirs,
144 	    sizeof(struct inoinfo *));
145 	if (inpsort == NULL || inphead == NULL) {
146 		printf("cannot alloc %lu bytes for inphead\n",
147 		    (unsigned long) numdirs * sizeof(struct inoinfo *));
148 		exit(1);
149 	}
150 	if (debug)
151 		printf("counting blocks...\n");
152 
153 	for (i = 0; i < maxino; i++) {
154 		inumber = dins[i]->ino;
155 		if (inumber == 0 || dins[i]->daddr == 0) {
156 			statemap[inumber] = USTATE;
157 			continue;
158 		}
159 		if (dins[i]->daddr != LFS_UNUSED_DADDR) {
160 			checkinode(inumber, &idesc);
161 		} else {
162 			statemap[inumber] = USTATE;
163 		}
164 		free(dins[i]);
165 	}
166 	free(dins);
167 }
168 
169 void
170 checkinode(ino_t inumber, struct inodesc * idesc)
171 {
172 	struct ufs1_dinode *dp;
173 	struct uvnode  *vp;
174 	struct zlncnt *zlnp;
175 	int ndb, j;
176 	mode_t mode;
177 
178 	vp = vget(fs, inumber);
179 	if (vp)
180 		dp = VTOD(vp);
181 	else
182 		dp = NULL;
183 
184 	if (dp == NULL) {
185 		/* pwarn("Could not find inode %ld\n",(long)inumber); */
186 		statemap[inumber] = USTATE;
187 		return;
188 	}
189 	mode = dp->di_mode & IFMT;
190 
191 	/* XXX - LFS doesn't have this particular problem (?) */
192 	if (mode == 0) {
193 		if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(ufs_daddr_t)) ||
194 		    memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(ufs_daddr_t)) ||
195 		    dp->di_mode || dp->di_size) {
196 			pwarn("mode=o%o, ifmt=o%o\n", dp->di_mode, mode);
197 			pfatal("PARTIALLY ALLOCATED INODE I=%llu",
198 			    (unsigned long long)inumber);
199 			if (reply("CLEAR") == 1) {
200 				vp = vget(fs, inumber);
201 				clearinode(inumber);
202 				vnode_destroy(vp);
203 			}
204 		}
205 		statemap[inumber] = USTATE;
206 		return;
207 	}
208 	lastino = inumber;
209 	if (dp->di_size < 0 || dp->di_size + fs->lfs_bsize - 1 < dp->di_size) {
210 		if (debug)
211 			printf("bad size %llu:",
212 			    (unsigned long long) dp->di_size);
213 		goto unknown;
214 	}
215 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
216 		vp = vget(fs, inumber);
217 		dp = VTOD(vp);
218 		dp->di_size = fs->lfs_fsize;
219 		dp->di_mode = IFREG | 0600;
220 		inodirty(VTOI(vp));
221 	}
222 	ndb = howmany(dp->di_size, fs->lfs_bsize);
223 	if (ndb < 0) {
224 		if (debug)
225 			printf("bad size %llu ndb %d:",
226 			    (unsigned long long) dp->di_size, ndb);
227 		goto unknown;
228 	}
229 	if (mode == IFBLK || mode == IFCHR)
230 		ndb++;
231 	if (mode == IFLNK) {
232 		/*
233 		 * Fake ndb value so direct/indirect block checks below
234 		 * will detect any garbage after symlink string.
235 		 */
236 		if (dp->di_size < fs->lfs_maxsymlinklen ||
237 		    (fs->lfs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
238 			ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
239 			if (ndb > NDADDR) {
240 				j = ndb - NDADDR;
241 				for (ndb = 1; j > 1; j--)
242 					ndb *= NINDIR(fs);
243 				ndb += NDADDR;
244 			}
245 		}
246 	}
247 	for (j = ndb; j < NDADDR; j++)
248 		if (dp->di_db[j] != 0) {
249 			if (debug)
250 				printf("bad direct addr: %d\n", dp->di_db[j]);
251 			goto unknown;
252 		}
253 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
254 		ndb /= NINDIR(fs);
255 	for (; j < NIADDR; j++)
256 		if (dp->di_ib[j] != 0) {
257 			if (debug)
258 				printf("bad indirect addr: %d\n",
259 				    dp->di_ib[j]);
260 			/* goto unknown; */
261 		}
262 	if (ftypeok(dp) == 0)
263 		goto unknown;
264 	n_files++;
265 	lncntp[inumber] = dp->di_nlink;
266 	if (dp->di_nlink <= 0) {
267 		zlnp = (struct zlncnt *) malloc(sizeof *zlnp);
268 		if (zlnp == NULL) {
269 			pfatal("LINK COUNT TABLE OVERFLOW");
270 			if (reply("CONTINUE") == 0)
271 				err(8, "%s", "");
272 		} else {
273 			zlnp->zlncnt = inumber;
274 			zlnp->next = zlnhead;
275 			zlnhead = zlnp;
276 		}
277 	}
278 	if (mode == IFDIR) {
279 		if (dp->di_size == 0)
280 			statemap[inumber] = DCLEAR;
281 		else
282 			statemap[inumber] = DSTATE;
283 		cacheino(dp, inumber);
284 	} else
285 		statemap[inumber] = FSTATE;
286 	typemap[inumber] = IFTODT(mode);
287 	badblk = dupblk = 0;
288 	idesc->id_number = inumber;
289 	(void) ckinode(VTOD(vp), idesc);
290 	if (dp->di_blocks != idesc->id_entryno) {
291 		pwarn("INCORRECT BLOCK COUNT I=%llu (%d should be %d)",
292 		    (unsigned long long)inumber, dp->di_blocks,
293 		    idesc->id_entryno);
294 		if (preen)
295 			printf(" (CORRECTED)\n");
296 		else if (reply("CORRECT") == 0)
297 			return;
298 		VTOI(vp)->i_ffs1_blocks = idesc->id_entryno;
299 		inodirty(VTOI(vp));
300 	}
301 	return;
302 unknown:
303 	pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber);
304 	statemap[inumber] = FCLEAR;
305 	if (reply("CLEAR") == 1) {
306 		statemap[inumber] = USTATE;
307 		vp = vget(fs, inumber);
308 		clearinode(inumber);
309 		vnode_destroy(vp);
310 	}
311 }
312 
313 int
314 pass1check(struct inodesc *idesc)
315 {
316 	int res = KEEPON;
317 	int anyout, ndblks;
318 	daddr_t blkno = idesc->id_blkno;
319 	struct dups *dlp;
320 	struct dups *new;
321 
322 	if ((anyout = chkrange(blkno, fragstofsb(fs, idesc->id_numfrags))) != 0) {
323 		blkerror(idesc->id_number, "BAD", blkno);
324 		if (badblk++ >= MAXBAD) {
325 			pwarn("EXCESSIVE BAD BLKS I=%llu",
326 			    (unsigned long long)idesc->id_number);
327 			if (preen)
328 				printf(" (SKIPPING)\n");
329 			else if (reply("CONTINUE") == 0)
330 				err(8, "%s", "");
331 			return (STOP);
332 		}
333 	} else if (!testbmap(blkno)) {
334 		seg_table[dtosn(fs, blkno)].su_nbytes += idesc->id_numfrags * fs->lfs_fsize;
335 	}
336 	for (ndblks = fragstofsb(fs, idesc->id_numfrags); ndblks > 0; blkno++, ndblks--) {
337 		if (anyout && chkrange(blkno, 1)) {
338 			res = SKIP;
339 		} else if (!testbmap(blkno)) {
340 			n_blks++;
341 #ifndef VERBOSE_BLOCKMAP
342 			setbmap(blkno);
343 #else
344 			setbmap(blkno, idesc->id_number);
345 #endif
346 		} else {
347 			blkerror(idesc->id_number, "DUP", blkno);
348 #ifdef VERBOSE_BLOCKMAP
349 			pwarn("(lbn %d: Holder is %d)\n", idesc->id_lblkno,
350 			    testbmap(blkno));
351 #endif
352 			if (dupblk++ >= MAXDUP) {
353 				pwarn("EXCESSIVE DUP BLKS I=%llu",
354 				    (unsigned long long)idesc->id_number);
355 				if (preen)
356 					printf(" (SKIPPING)\n");
357 				else if (reply("CONTINUE") == 0)
358 					err(8, "%s", "");
359 				return (STOP);
360 			}
361 			new = (struct dups *) malloc(sizeof(struct dups));
362 			if (new == NULL) {
363 				pfatal("DUP TABLE OVERFLOW.");
364 				if (reply("CONTINUE") == 0)
365 					err(8, "%s", "");
366 				return (STOP);
367 			}
368 			new->dup = blkno;
369 			if (muldup == 0) {
370 				duplist = muldup = new;
371 				new->next = 0;
372 			} else {
373 				new->next = muldup->next;
374 				muldup->next = new;
375 			}
376 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
377 				if (dlp->dup == blkno)
378 					break;
379 			if (dlp == muldup && dlp->dup != blkno)
380 				muldup = new;
381 		}
382 		/*
383 		 * count the number of blocks found in id_entryno
384 		 */
385 		idesc->id_entryno++;
386 	}
387 	return (res);
388 }
389