pass1.c revision 1.10 1 /* $NetBSD: pass1.c,v 1.10 2003/08/07 10:04:18 agc 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 /*
33 * Copyright (c) 1997 Manuel Bouyer.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 #include <sys/cdefs.h>
65 #ifndef lint
66 #if 0
67 static char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93";
68 #else
69 __RCSID("$NetBSD: pass1.c,v 1.10 2003/08/07 10:04:18 agc Exp $");
70 #endif
71 #endif /* not lint */
72
73 #include <sys/param.h>
74 #include <sys/time.h>
75 #include <ufs/ext2fs/ext2fs_dinode.h>
76 #include <ufs/ext2fs/ext2fs_dir.h>
77 #include <ufs/ext2fs/ext2fs.h>
78
79 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
80
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <time.h>
85
86 #include "fsck.h"
87 #include "extern.h"
88 #include "fsutil.h"
89
90 static daddr_t badblk;
91 static daddr_t dupblk;
92 static void checkinode __P((ino_t, struct inodesc *));
93
94 void
95 pass1()
96 {
97 ino_t inumber;
98 int c, i;
99 daddr_t dbase;
100 struct inodesc idesc;
101
102 /*
103 * Set file system reserved blocks in used block map.
104 */
105 for (c = 0; c < sblock.e2fs_ncg; c++) {
106 dbase = c * sblock.e2fs.e2fs_bpg +
107 sblock.e2fs.e2fs_first_dblock;
108 /* Mark the blocks used for the inode table */
109 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) {
110 for (i = 0; i < sblock.e2fs_itpg; i++)
111 setbmap(
112 fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables)
113 + i);
114 }
115 /* Mark the blocks used for the block bitmap */
116 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase)
117 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap));
118 /* Mark the blocks used for the inode bitmap */
119 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase)
120 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap));
121
122 if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
123 (sblock.e2fs.e2fs_features_rocompat &
124 EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
125 cg_has_sb(c)) {
126 /* Mark copuy of SB and descriptors */
127 setbmap(dbase);
128 for (i = 1; i <= sblock.e2fs_ngdb; i++)
129 setbmap(dbase+i);
130 }
131
132
133 if (c == 0) {
134 for(i = 0; i < dbase; i++)
135 setbmap(i);
136 }
137 }
138
139 /*
140 * Find all allocated blocks.
141 */
142 memset(&idesc, 0, sizeof(struct inodesc));
143 idesc.id_type = ADDR;
144 idesc.id_func = pass1check;
145 inumber = 1;
146 n_files = n_blks = 0;
147 resetinodebuf();
148 for (c = 0; c < sblock.e2fs_ncg; c++) {
149 for (i = 0;
150 i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
151 i++, inumber++) {
152 if (inumber < EXT2_ROOTINO) /* XXX */
153 continue;
154 checkinode(inumber, &idesc);
155 }
156 }
157 freeinodebuf();
158 }
159
160 static void
161 checkinode(inumber, idesc)
162 ino_t inumber;
163 struct inodesc *idesc;
164 {
165 struct ext2fs_dinode *dp;
166 struct zlncnt *zlnp;
167 int ndb, j;
168 mode_t mode;
169
170 dp = getnextinode(inumber);
171 if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
172 return;
173
174 mode = fs2h16(dp->e2di_mode) & IFMT;
175 if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
176 if (mode == 0 && (
177 memcmp(dp->e2di_blocks, zino.e2di_blocks,
178 (NDADDR + NIADDR) * sizeof(u_int32_t)) ||
179 dp->e2di_mode || dp->e2di_size)) {
180 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
181 if (reply("CLEAR") == 1) {
182 dp = ginode(inumber);
183 clearinode(dp);
184 inodirty();
185 }
186 }
187 #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
188 if (dp->e2di_dtime == 0) {
189 pwarn("DELETED INODE I=%u HAS A NULL DTIME", inumber);
190 if (preen) {
191 printf(" (CORRECTED)\n");
192 }
193 if (preen || reply("CORRECT")) {
194 time_t t;
195 time(&t);
196 dp->e2di_dtime = h2fs32(t);
197 dp = ginode(inumber);
198 inodirty();
199 }
200 }
201 #endif
202 statemap[inumber] = USTATE;
203 return;
204 }
205 lastino = inumber;
206 if (dp->e2di_dtime != 0) {
207 time_t t = fs2h32(dp->e2di_dtime);
208 char *p = ctime(&t);
209 pwarn("INODE I=%u HAS DTIME=%12.12s %4.4s", inumber, &p[4], &p[20]);
210 if (preen) {
211 printf(" (CORRECTED)\n");
212 }
213 if (preen || reply("CORRECT")) {
214 dp = ginode(inumber);
215 dp->e2di_dtime = 0;
216 inodirty();
217 }
218 }
219 if (/* dp->di_size < 0 || */
220 fs2h32(dp->e2di_size) + sblock.e2fs_bsize - 1 <
221 fs2h32(dp->e2di_size)) {
222 if (debug)
223 printf("bad size %lu:", (u_long)fs2h32(dp->e2di_size));
224 goto unknown;
225 }
226 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
227 dp = ginode(inumber);
228 dp->e2di_size = h2fs32(sblock.e2fs_bsize);
229 dp->e2di_mode = h2fs16(IFREG|0600);
230 inodirty();
231 }
232 ndb = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
233 if (ndb < 0) {
234 if (debug)
235 printf("bad size %lu ndb %d:",
236 (u_long)fs2h32(dp->e2di_size), ndb);
237 goto unknown;
238 }
239 if (mode == IFBLK || mode == IFCHR)
240 ndb++;
241 if (mode == IFLNK) {
242 /*
243 * Fake ndb value so direct/indirect block checks below
244 * will detect any garbage after symlink string.
245 */
246 if (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN ||
247 (EXT2_MAXSYMLINKLEN == 0 && dp->e2di_blocks == 0)) {
248 ndb = howmany(fs2h32(dp->e2di_size), sizeof(u_int32_t));
249 if (ndb > NDADDR) {
250 j = ndb - NDADDR;
251 for (ndb = 1; j > 1; j--)
252 ndb *= NINDIR(&sblock);
253 ndb += NDADDR;
254 }
255 }
256 }
257 /* Linux puts things in blocks for FIFO, so skip this check */
258 if (mode != IFIFO) {
259 for (j = ndb; j < NDADDR; j++)
260 if (dp->e2di_blocks[j] != 0) {
261 if (debug)
262 printf("bad direct addr: %d\n",
263 fs2h32(dp->e2di_blocks[j]));
264 goto unknown;
265 }
266 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
267 ndb /= NINDIR(&sblock);
268 for (; j < NIADDR; j++) {
269 if (dp->e2di_blocks[j+NDADDR] != 0) {
270 if (debug)
271 printf("bad indirect addr: %d\n",
272 fs2h32(dp->e2di_blocks[j+NDADDR]));
273 goto unknown;
274 }
275 }
276 }
277 if (ftypeok(dp) == 0)
278 goto unknown;
279 n_files++;
280 lncntp[inumber] = fs2h16(dp->e2di_nlink);
281 if (dp->e2di_nlink == 0) {
282 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
283 if (zlnp == NULL) {
284 pfatal("LINK COUNT TABLE OVERFLOW");
285 if (reply("CONTINUE") == 0)
286 errexit("%s\n", "");
287 } else {
288 zlnp->zlncnt = inumber;
289 zlnp->next = zlnhead;
290 zlnhead = zlnp;
291 }
292 }
293 if (mode == IFDIR) {
294 if (dp->e2di_size == 0)
295 statemap[inumber] = DCLEAR;
296 else
297 statemap[inumber] = DSTATE;
298 cacheino(dp, inumber);
299 } else {
300 statemap[inumber] = FSTATE;
301 }
302 typemap[inumber] = E2IFTODT(mode);
303 badblk = dupblk = 0;
304 idesc->id_number = inumber;
305 (void)ckinode(dp, idesc);
306 idesc->id_entryno *= btodb(sblock.e2fs_bsize);
307 if (fs2h32(dp->e2di_nblock) != idesc->id_entryno) {
308 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
309 inumber, fs2h32(dp->e2di_nblock), idesc->id_entryno);
310 if (preen)
311 printf(" (CORRECTED)\n");
312 else if (reply("CORRECT") == 0)
313 return;
314 dp = ginode(inumber);
315 dp->e2di_nblock = h2fs32(idesc->id_entryno);
316 inodirty();
317 }
318 return;
319 unknown:
320 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
321 statemap[inumber] = FCLEAR;
322 if (reply("CLEAR") == 1) {
323 statemap[inumber] = USTATE;
324 dp = ginode(inumber);
325 clearinode(dp);
326 inodirty();
327 }
328 }
329
330 int
331 pass1check(idesc)
332 struct inodesc *idesc;
333 {
334 int res = KEEPON;
335 int anyout, nfrags;
336 daddr_t blkno = idesc->id_blkno;
337 struct dups *dlp;
338 struct dups *new;
339
340 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
341 blkerror(idesc->id_number, "BAD", blkno);
342 if (badblk++ >= MAXBAD) {
343 pwarn("EXCESSIVE BAD BLKS I=%u",
344 idesc->id_number);
345 if (preen)
346 printf(" (SKIPPING)\n");
347 else if (reply("CONTINUE") == 0)
348 errexit("%s\n", "");
349 return (STOP);
350 }
351 }
352 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
353 if (anyout && chkrange(blkno, 1)) {
354 res = SKIP;
355 } else if (!testbmap(blkno)) {
356 n_blks++;
357 setbmap(blkno);
358 } else {
359 blkerror(idesc->id_number, "DUP", blkno);
360 if (dupblk++ >= MAXDUP) {
361 pwarn("EXCESSIVE DUP BLKS I=%u",
362 idesc->id_number);
363 if (preen)
364 printf(" (SKIPPING)\n");
365 else if (reply("CONTINUE") == 0)
366 errexit("%s\n", "");
367 return (STOP);
368 }
369 new = (struct dups *)malloc(sizeof(struct dups));
370 if (new == NULL) {
371 pfatal("DUP TABLE OVERFLOW.");
372 if (reply("CONTINUE") == 0)
373 errexit("%s\n", "");
374 return (STOP);
375 }
376 new->dup = blkno;
377 if (muldup == 0) {
378 duplist = muldup = new;
379 new->next = 0;
380 } else {
381 new->next = muldup->next;
382 muldup->next = new;
383 }
384 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
385 if (dlp->dup == blkno)
386 break;
387 if (dlp == muldup && dlp->dup != blkno)
388 muldup = new;
389 }
390 /*
391 * count the number of blocks found in id_entryno
392 */
393 idesc->id_entryno++;
394 }
395 return (res);
396 }
397