pass1.c revision 1.17 1 /* $NetBSD: pass1.c,v 1.17 2008/03/16 23:17:55 lukem 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 Manuel Bouyer.
46 * 4. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93";
65 #else
66 __RCSID("$NetBSD: pass1.c,v 1.17 2008/03/16 23:17:55 lukem Exp $");
67 #endif
68 #endif /* not lint */
69
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <ufs/ext2fs/ext2fs_dinode.h>
73 #include <ufs/ext2fs/ext2fs_dir.h>
74 #include <ufs/ext2fs/ext2fs.h>
75
76 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
77
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <time.h>
82
83 #include "fsck.h"
84 #include "extern.h"
85 #include "fsutil.h"
86 #include "exitvalues.h"
87
88 static daddr_t badblk;
89 static daddr_t dupblk;
90 static void checkinode(ino_t, struct inodesc *);
91
92 void
93 pass1(void)
94 {
95 ino_t inumber;
96 int c, i;
97 daddr_t dbase;
98 struct inodesc idesc;
99
100 /*
101 * Set file system reserved blocks in used block map.
102 */
103 for (c = 0; c < sblock.e2fs_ncg; c++) {
104 dbase = c * sblock.e2fs.e2fs_bpg +
105 sblock.e2fs.e2fs_first_dblock;
106 /* Mark the blocks used for the inode table */
107 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) {
108 for (i = 0; i < sblock.e2fs_itpg; i++)
109 setbmap(
110 fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables)
111 + i);
112 }
113 /* Mark the blocks used for the block bitmap */
114 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase)
115 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap));
116 /* Mark the blocks used for the inode bitmap */
117 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase)
118 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap));
119
120 if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
121 (sblock.e2fs.e2fs_features_rocompat &
122 EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
123 cg_has_sb(c)) {
124 /* Mark copuy of SB and descriptors */
125 setbmap(dbase);
126 for (i = 1; i <= sblock.e2fs_ngdb; i++)
127 setbmap(dbase+i);
128 }
129
130
131 if (c == 0) {
132 for(i = 0; i < dbase; i++)
133 setbmap(i);
134 }
135 }
136
137 /*
138 * Find all allocated blocks.
139 */
140 memset(&idesc, 0, sizeof(struct inodesc));
141 idesc.id_type = ADDR;
142 idesc.id_func = pass1check;
143 inumber = 1;
144 n_files = n_blks = 0;
145 resetinodebuf();
146 for (c = 0; c < sblock.e2fs_ncg; c++) {
147 for (i = 0;
148 i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
149 i++, inumber++) {
150 if (inumber < EXT2_ROOTINO) /* XXX */
151 continue;
152 checkinode(inumber, &idesc);
153 }
154 }
155 freeinodebuf();
156 }
157
158 static void
159 checkinode(ino_t inumber, struct inodesc *idesc)
160 {
161 struct ext2fs_dinode *dp;
162 struct zlncnt *zlnp;
163 int ndb, j;
164 mode_t mode;
165
166 dp = getnextinode(inumber);
167 if (inumber < EXT2_FIRSTINO &&
168 inumber != EXT2_ROOTINO &&
169 !(inumber == EXT2_RESIZEINO &&
170 (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
171 return;
172
173 mode = fs2h16(dp->e2di_mode) & IFMT;
174 if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
175 if (mode == 0 && (
176 memcmp(dp->e2di_blocks, zino.e2di_blocks,
177 (NDADDR + NIADDR) * sizeof(u_int32_t)) ||
178 dp->e2di_mode || inosize(dp))) {
179 pfatal("PARTIALLY ALLOCATED INODE I=%llu",
180 (unsigned long long)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=%llu HAS A NULL DTIME",
190 (unsigned long long)inumber);
191 if (preen) {
192 printf(" (CORRECTED)\n");
193 }
194 if (preen || reply("CORRECT")) {
195 time_t t;
196 time(&t);
197 dp->e2di_dtime = h2fs32(t);
198 dp = ginode(inumber);
199 inodirty();
200 }
201 }
202 #endif
203 statemap[inumber] = USTATE;
204 return;
205 }
206 lastino = inumber;
207 if (dp->e2di_dtime != 0) {
208 time_t t = fs2h32(dp->e2di_dtime);
209 char *p = ctime(&t);
210 pwarn("INODE I=%llu HAS DTIME=%12.12s %4.4s",
211 (unsigned long long)inumber, &p[4], &p[20]);
212 if (preen) {
213 printf(" (CORRECTED)\n");
214 }
215 if (preen || reply("CORRECT")) {
216 dp = ginode(inumber);
217 dp->e2di_dtime = 0;
218 inodirty();
219 }
220 }
221 if (inosize(dp) + sblock.e2fs_bsize - 1 < inosize(dp)) {
222 if (debug)
223 printf("bad size %llu:", (unsigned long long)inosize(dp));
224 goto unknown;
225 }
226 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
227 dp = ginode(inumber);
228 dp->e2di_mode = h2fs16(IFREG|0600);
229 inossize(dp, sblock.e2fs_bsize);
230 inodirty();
231 }
232 ndb = howmany(inosize(dp), sblock.e2fs_bsize);
233 if (ndb < 0) {
234 if (debug)
235 printf("bad size %llu ndb %d:",
236 (unsigned long long)inosize(dp), 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 (inosize(dp) < EXT2_MAXSYMLINKLEN ||
247 (EXT2_MAXSYMLINKLEN == 0 && dp->e2di_blocks == 0)) {
248 ndb = howmany(inosize(dp), 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 if (inumber >= EXT2_FIRSTINO || inumber == EXT2_ROOTINO) {
280 /* Don't count reserved inodes except root */
281 n_files++;
282 }
283 lncntp[inumber] = fs2h16(dp->e2di_nlink);
284 if (dp->e2di_nlink == 0) {
285 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
286 if (zlnp == NULL) {
287 pfatal("LINK COUNT TABLE OVERFLOW");
288 if (reply("CONTINUE") == 0)
289 exit(FSCK_EXIT_CHECK_FAILED);
290 } else {
291 zlnp->zlncnt = inumber;
292 zlnp->next = zlnhead;
293 zlnhead = zlnp;
294 }
295 }
296 if (mode == IFDIR) {
297 if (inosize(dp) == 0)
298 statemap[inumber] = DCLEAR;
299 else
300 statemap[inumber] = DSTATE;
301 cacheino(dp, inumber);
302 } else {
303 statemap[inumber] = FSTATE;
304 }
305 typemap[inumber] = E2IFTODT(mode);
306 badblk = dupblk = 0;
307 idesc->id_number = inumber;
308 (void)ckinode(dp, idesc);
309 idesc->id_entryno *= btodb(sblock.e2fs_bsize);
310 if (fs2h32(dp->e2di_nblock) != idesc->id_entryno) {
311 pwarn("INCORRECT BLOCK COUNT I=%llu (%d should be %d)",
312 (unsigned long long)inumber, fs2h32(dp->e2di_nblock),
313 idesc->id_entryno);
314 if (preen)
315 printf(" (CORRECTED)\n");
316 else if (reply("CORRECT") == 0)
317 return;
318 dp = ginode(inumber);
319 dp->e2di_nblock = h2fs32(idesc->id_entryno);
320 inodirty();
321 }
322 return;
323 unknown:
324 pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber);
325 statemap[inumber] = FCLEAR;
326 if (reply("CLEAR") == 1) {
327 statemap[inumber] = USTATE;
328 dp = ginode(inumber);
329 clearinode(dp);
330 inodirty();
331 }
332 }
333
334 int
335 pass1check(struct inodesc *idesc)
336 {
337 int res = KEEPON;
338 int anyout, nfrags;
339 daddr_t blkno = idesc->id_blkno;
340 struct dups *dlp;
341 struct dups *new;
342
343 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
344 blkerror(idesc->id_number, "BAD", blkno);
345 if (badblk++ >= MAXBAD) {
346 pwarn("EXCESSIVE BAD BLKS I=%llu",
347 (unsigned long long)idesc->id_number);
348 if (preen)
349 printf(" (SKIPPING)\n");
350 else if (reply("CONTINUE") == 0)
351 exit(FSCK_EXIT_CHECK_FAILED);
352 return (STOP);
353 }
354 }
355 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
356 if (anyout && chkrange(blkno, 1)) {
357 res = SKIP;
358 } else if (!testbmap(blkno)) {
359 n_blks++;
360 setbmap(blkno);
361 } else {
362 blkerror(idesc->id_number, "DUP", blkno);
363 if (dupblk++ >= MAXDUP) {
364 pwarn("EXCESSIVE DUP BLKS I=%llu",
365 (unsigned long long)idesc->id_number);
366 if (preen)
367 printf(" (SKIPPING)\n");
368 else if (reply("CONTINUE") == 0)
369 exit(FSCK_EXIT_CHECK_FAILED);
370 return (STOP);
371 }
372 new = (struct dups *)malloc(sizeof(struct dups));
373 if (new == NULL) {
374 pfatal("DUP TABLE OVERFLOW.");
375 if (reply("CONTINUE") == 0)
376 exit(FSCK_EXIT_CHECK_FAILED);
377 return (STOP);
378 }
379 new->dup = blkno;
380 if (muldup == 0) {
381 duplist = muldup = new;
382 new->next = 0;
383 } else {
384 new->next = muldup->next;
385 muldup->next = new;
386 }
387 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
388 if (dlp->dup == blkno)
389 break;
390 if (dlp == muldup && dlp->dup != blkno)
391 muldup = new;
392 }
393 /*
394 * count the number of blocks found in id_entryno
395 */
396 idesc->id_entryno++;
397 }
398 return (res);
399 }
400