pass1.c revision 1.7 1 /* $NetBSD: pass1.c,v 1.7 2000/01/26 16:21:32 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.7 2000/01/26 16:21:32 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 #include <ufs/ufs/dir.h> /* for IFTODT & friends */
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59
60 #include "fsck.h"
61 #include "extern.h"
62 #include "fsutil.h"
63
64 static daddr_t badblk;
65 static daddr_t dupblk;
66 static void checkinode __P((ino_t, struct inodesc *));
67
68 void
69 pass1()
70 {
71 ino_t inumber;
72 int c, i, cgd;
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 i = c * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock;
80 cgd = i + cgoverhead;
81
82 if (c == 0)
83 i = 0;
84 for (; i < cgd; i++)
85 setbmap(i);
86 }
87
88 /*
89 * Find all allocated blocks.
90 */
91 memset(&idesc, 0, sizeof(struct inodesc));
92 idesc.id_type = ADDR;
93 idesc.id_func = pass1check;
94 inumber = 1;
95 n_files = n_blks = 0;
96 resetinodebuf();
97 for (c = 0; c < sblock.e2fs_ncg; c++) {
98 for (i = 0;
99 i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
100 i++, inumber++) {
101 if (inumber < EXT2_ROOTINO) /* XXX */
102 continue;
103 checkinode(inumber, &idesc);
104 }
105 }
106 freeinodebuf();
107 }
108
109 static void
110 checkinode(inumber, idesc)
111 ino_t inumber;
112 struct inodesc *idesc;
113 {
114 struct ext2fs_dinode *dp;
115 struct zlncnt *zlnp;
116 int ndb, j;
117 mode_t mode;
118
119 dp = getnextinode(inumber);
120 if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
121 return;
122
123 mode = fs2h16(dp->e2di_mode) & IFMT;
124 if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
125 if (mode == 0 && (
126 memcmp(dp->e2di_blocks, zino.e2di_blocks,
127 (NDADDR + NIADDR) * sizeof(u_int32_t)) ||
128 dp->e2di_mode || dp->e2di_size)) {
129 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
130 if (reply("CLEAR") == 1) {
131 dp = ginode(inumber);
132 clearinode(dp);
133 inodirty();
134 }
135 }
136 #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
137 if (dp->e2di_dtime == 0) {
138 pwarn("DELETED INODE I=%u HAS A NULL DTIME", inumber);
139 if (preen) {
140 printf(" (CORRECTED)\n");
141 }
142 if (preen || reply("CORRECT")) {
143 time_t t;
144 time(&t);
145 dp->e2di_dtime = h2fs32(t);
146 dp = ginode(inumber);
147 inodirty();
148 }
149 }
150 #endif
151 statemap[inumber] = USTATE;
152 return;
153 }
154 lastino = inumber;
155 if (dp->e2di_dtime != 0) {
156 time_t t = fs2h32(dp->e2di_dtime);
157 char *p = ctime(&t);
158 pwarn("INODE I=%u HAS DTIME=%12.12s %4.4s", inumber, &p[4], &p[20]);
159 if (preen) {
160 printf(" (CORRECTED)\n");
161 }
162 if (preen || reply("CORRECT")) {
163 dp = ginode(inumber);
164 dp->e2di_dtime = 0;
165 inodirty();
166 }
167 }
168 if (/* dp->di_size < 0 || */
169 fs2h32(dp->e2di_size) + sblock.e2fs_bsize - 1 <
170 fs2h32(dp->e2di_size)) {
171 if (debug)
172 printf("bad size %lu:", (u_long)fs2h32(dp->e2di_size));
173 goto unknown;
174 }
175 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
176 dp = ginode(inumber);
177 dp->e2di_size = h2fs32(sblock.e2fs_bsize);
178 dp->e2di_mode = h2fs16(IFREG|0600);
179 inodirty();
180 }
181 ndb = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
182 if (ndb < 0) {
183 if (debug)
184 printf("bad size %lu ndb %d:",
185 (u_long)fs2h32(dp->e2di_size), ndb);
186 goto unknown;
187 }
188 if (mode == IFBLK || mode == IFCHR)
189 ndb++;
190 if (mode == IFLNK) {
191 /*
192 * Fake ndb value so direct/indirect block checks below
193 * will detect any garbage after symlink string.
194 */
195 if (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN ||
196 (EXT2_MAXSYMLINKLEN == 0 && dp->e2di_blocks == 0)) {
197 ndb = howmany(fs2h32(dp->e2di_size), sizeof(u_int32_t));
198 if (ndb > NDADDR) {
199 j = ndb - NDADDR;
200 for (ndb = 1; j > 1; j--)
201 ndb *= NINDIR(&sblock);
202 ndb += NDADDR;
203 }
204 }
205 }
206 /* Linux puts things in blocks for FIFO, so skip this check */
207 if (mode != IFIFO) {
208 for (j = ndb; j < NDADDR; j++)
209 if (dp->e2di_blocks[j] != 0) {
210 if (debug)
211 printf("bad direct addr: %d\n",
212 fs2h32(dp->e2di_blocks[j]));
213 goto unknown;
214 }
215 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
216 ndb /= NINDIR(&sblock);
217 for (; j < NIADDR; j++) {
218 if (dp->e2di_blocks[j+NDADDR] != 0) {
219 if (debug)
220 printf("bad indirect addr: %d\n",
221 fs2h32(dp->e2di_blocks[j+NDADDR]));
222 goto unknown;
223 }
224 }
225 }
226 if (ftypeok(dp) == 0)
227 goto unknown;
228 n_files++;
229 lncntp[inumber] = fs2h16(dp->e2di_nlink);
230 if (dp->e2di_nlink == 0) {
231 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
232 if (zlnp == NULL) {
233 pfatal("LINK COUNT TABLE OVERFLOW");
234 if (reply("CONTINUE") == 0)
235 errexit("%s\n", "");
236 } else {
237 zlnp->zlncnt = inumber;
238 zlnp->next = zlnhead;
239 zlnhead = zlnp;
240 }
241 }
242 if (mode == IFDIR) {
243 if (dp->e2di_size == 0)
244 statemap[inumber] = DCLEAR;
245 else
246 statemap[inumber] = DSTATE;
247 cacheino(dp, inumber);
248 } else {
249 statemap[inumber] = FSTATE;
250 }
251 typemap[inumber] = IFTODT(mode);
252 badblk = dupblk = 0;
253 idesc->id_number = inumber;
254 (void)ckinode(dp, idesc);
255 idesc->id_entryno *= btodb(sblock.e2fs_bsize);
256 if (fs2h32(dp->e2di_nblock) != idesc->id_entryno) {
257 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
258 inumber, fs2h32(dp->e2di_nblock), idesc->id_entryno);
259 if (preen)
260 printf(" (CORRECTED)\n");
261 else if (reply("CORRECT") == 0)
262 return;
263 dp = ginode(inumber);
264 dp->e2di_nblock = h2fs32(idesc->id_entryno);
265 inodirty();
266 }
267 return;
268 unknown:
269 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
270 statemap[inumber] = FCLEAR;
271 if (reply("CLEAR") == 1) {
272 statemap[inumber] = USTATE;
273 dp = ginode(inumber);
274 clearinode(dp);
275 inodirty();
276 }
277 }
278
279 int
280 pass1check(idesc)
281 struct inodesc *idesc;
282 {
283 int res = KEEPON;
284 int anyout, nfrags;
285 daddr_t blkno = idesc->id_blkno;
286 struct dups *dlp;
287 struct dups *new;
288
289 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
290 blkerror(idesc->id_number, "BAD", blkno);
291 if (badblk++ >= MAXBAD) {
292 pwarn("EXCESSIVE BAD BLKS I=%u",
293 idesc->id_number);
294 if (preen)
295 printf(" (SKIPPING)\n");
296 else if (reply("CONTINUE") == 0)
297 errexit("%s\n", "");
298 return (STOP);
299 }
300 }
301 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
302 if (anyout && chkrange(blkno, 1)) {
303 res = SKIP;
304 } else if (!testbmap(blkno)) {
305 n_blks++;
306 setbmap(blkno);
307 } else {
308 blkerror(idesc->id_number, "DUP", blkno);
309 if (dupblk++ >= MAXDUP) {
310 pwarn("EXCESSIVE DUP BLKS I=%u",
311 idesc->id_number);
312 if (preen)
313 printf(" (SKIPPING)\n");
314 else if (reply("CONTINUE") == 0)
315 errexit("%s\n", "");
316 return (STOP);
317 }
318 new = (struct dups *)malloc(sizeof(struct dups));
319 if (new == NULL) {
320 pfatal("DUP TABLE OVERFLOW.");
321 if (reply("CONTINUE") == 0)
322 errexit("%s\n", "");
323 return (STOP);
324 }
325 new->dup = blkno;
326 if (muldup == 0) {
327 duplist = muldup = new;
328 new->next = 0;
329 } else {
330 new->next = muldup->next;
331 muldup->next = new;
332 }
333 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
334 if (dlp->dup == blkno)
335 break;
336 if (dlp == muldup && dlp->dup != blkno)
337 muldup = new;
338 }
339 /*
340 * count the number of blocks found in id_entryno
341 */
342 idesc->id_entryno++;
343 }
344 return (res);
345 }
346