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