pass1.c revision 1.19 1 /* $NetBSD: pass1.c,v 1.19 1997/09/16 16:45:09 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: pass1.c,v 1.19 1997/09/16 16:45:09 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ffs/fs.h>
51
52 #include <err.h>
53 #include <string.h>
54
55 #include "fsck.h"
56 #include "extern.h"
57 #include "fsutil.h"
58
59 static ufs_daddr_t badblk;
60 static ufs_daddr_t dupblk;
61 static void checkinode __P((ino_t, struct inodesc *));
62
63 void
64 pass1()
65 {
66 ino_t inumber;
67 int c, i, cgd;
68 struct inodesc idesc;
69
70 /*
71 * Set file system reserved blocks in used block map.
72 */
73 for (c = 0; c < sblock.fs_ncg; c++) {
74 cgd = cgdmin(&sblock, c);
75 if (c == 0)
76 i = cgbase(&sblock, c);
77 else
78 i = cgsblock(&sblock, c);
79 for (; i < cgd; i++)
80 setbmap(i);
81 }
82 i = sblock.fs_csaddr;
83 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
84 for (; i < cgd; i++)
85 setbmap(i);
86 /*
87 * Find all allocated blocks.
88 */
89 memset(&idesc, 0, sizeof(struct inodesc));
90 idesc.id_type = ADDR;
91 idesc.id_func = pass1check;
92 inumber = 0;
93 n_files = n_blks = 0;
94 resetinodebuf();
95 for (c = 0; c < sblock.fs_ncg; c++) {
96 for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
97 if (inumber < ROOTINO)
98 continue;
99 checkinode(inumber, &idesc);
100 }
101 }
102 freeinodebuf();
103 }
104
105 static void
106 checkinode(inumber, idesc)
107 ino_t inumber;
108 struct inodesc *idesc;
109 {
110 struct dinode *dp;
111 struct zlncnt *zlnp;
112 int ndb, j;
113 mode_t mode;
114 char symbuf[MAXSYMLINKLEN];
115
116 dp = getnextinode(inumber);
117 mode = dp->di_mode & IFMT;
118 if (mode == 0) {
119 if (memcmp(dp->di_db, zino.di_db,
120 NDADDR * sizeof(ufs_daddr_t)) ||
121 memcmp(dp->di_ib, zino.di_ib,
122 NIADDR * sizeof(ufs_daddr_t)) ||
123 dp->di_mode || dp->di_size) {
124 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
125 if (reply("CLEAR") == 1) {
126 dp = ginode(inumber);
127 clearinode(dp);
128 inodirty();
129 }
130 }
131 statemap[inumber] = USTATE;
132 return;
133 }
134 lastino = inumber;
135 if (/* dp->di_size < 0 || */
136 dp->di_size + sblock.fs_bsize - 1 < dp->di_size ||
137 (mode == IFDIR && dp->di_size > MAXDIRSIZE)) {
138 if (debug)
139 printf("bad size %qu:",(unsigned long long)dp->di_size);
140 goto unknown;
141 }
142 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
143 dp = ginode(inumber);
144 dp->di_size = sblock.fs_fsize;
145 dp->di_mode = IFREG|0600;
146 inodirty();
147 }
148 ndb = howmany(dp->di_size, sblock.fs_bsize);
149 if (ndb < 0) {
150 if (debug)
151 printf("bad size %qu ndb %d:",
152 (unsigned long long)dp->di_size, ndb);
153 goto unknown;
154 }
155 if (mode == IFBLK || mode == IFCHR)
156 ndb++;
157 if (mode == IFLNK) {
158 /*
159 * Note that the old fastlink format always had di_blocks set
160 * to 0. Other than that we no longer use the `spare' field
161 * (which is now the extended uid) for sanity checking, the
162 * new format is the same as the old. We simply ignore the
163 * conversion altogether. - mycroft, 19MAY1994
164 */
165 if (doinglevel2 &&
166 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
167 dp->di_blocks != 0) {
168 if (bread(fsreadfd, symbuf,
169 fsbtodb(&sblock, dp->di_db[0]),
170 (long)secsize) != 0)
171 errx(EEXIT, "cannot read symlink");
172 if (debug) {
173 symbuf[dp->di_size] = 0;
174 printf("convert symlink %u(%s) of size %qd\n",
175 inumber, symbuf,
176 (unsigned long long)dp->di_size);
177 }
178 dp = ginode(inumber);
179 memmove(dp->di_shortlink, symbuf, (long)dp->di_size);
180 dp->di_blocks = 0;
181 inodirty();
182 }
183 /*
184 * Fake ndb value so direct/indirect block checks below
185 * will detect any garbage after symlink string.
186 */
187 if (dp->di_size < sblock.fs_maxsymlinklen ||
188 (sblock.fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
189 ndb = howmany(dp->di_size, sizeof(daddr_t));
190 if (ndb > NDADDR) {
191 j = ndb - NDADDR;
192 for (ndb = 1; j > 1; j--)
193 ndb *= NINDIR(&sblock);
194 ndb += NDADDR;
195 }
196 }
197 }
198 for (j = ndb; j < NDADDR; j++)
199 if (dp->di_db[j] != 0) {
200 if (debug)
201 printf("bad direct addr: %d\n", dp->di_db[j]);
202 goto unknown;
203 }
204 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
205 ndb /= NINDIR(&sblock);
206 for (; j < NIADDR; j++)
207 if (dp->di_ib[j] != 0) {
208 if (debug)
209 printf("bad indirect addr: %d\n",
210 dp->di_ib[j]);
211 goto unknown;
212 }
213 if (ftypeok(dp) == 0)
214 goto unknown;
215 n_files++;
216 lncntp[inumber] = dp->di_nlink;
217 if (dp->di_nlink <= 0) {
218 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
219 if (zlnp == NULL) {
220 pfatal("LINK COUNT TABLE OVERFLOW");
221 if (reply("CONTINUE") == 0)
222 exit(EEXIT);
223 } else {
224 zlnp->zlncnt = inumber;
225 zlnp->next = zlnhead;
226 zlnhead = zlnp;
227 }
228 }
229 if (mode == IFDIR) {
230 if (dp->di_size == 0)
231 statemap[inumber] = DCLEAR;
232 else
233 statemap[inumber] = DSTATE;
234 cacheino(dp, inumber);
235 } else
236 statemap[inumber] = FSTATE;
237 typemap[inumber] = IFTODT(mode);
238 if (doinglevel2 &&
239 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
240 dp = ginode(inumber);
241 dp->di_uid = dp->di_ouid;
242 dp->di_ouid = -1;
243 dp->di_gid = dp->di_ogid;
244 dp->di_ogid = -1;
245 inodirty();
246 }
247 badblk = dupblk = 0;
248 idesc->id_number = inumber;
249 (void)ckinode(dp, idesc);
250 idesc->id_entryno *= btodb(sblock.fs_fsize);
251 if (dp->di_blocks != idesc->id_entryno) {
252 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
253 inumber, dp->di_blocks, idesc->id_entryno);
254 if (preen)
255 printf(" (CORRECTED)\n");
256 else if (reply("CORRECT") == 0)
257 return;
258 dp = ginode(inumber);
259 dp->di_blocks = idesc->id_entryno;
260 inodirty();
261 }
262 return;
263 unknown:
264 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
265 statemap[inumber] = FCLEAR;
266 if (reply("CLEAR") == 1) {
267 statemap[inumber] = USTATE;
268 dp = ginode(inumber);
269 clearinode(dp);
270 inodirty();
271 }
272 }
273
274 int
275 pass1check(idesc)
276 struct inodesc *idesc;
277 {
278 int res = KEEPON;
279 int anyout, nfrags;
280 ufs_daddr_t blkno = idesc->id_blkno;
281 struct dups *dlp;
282 struct dups *new;
283
284 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
285 blkerror(idesc->id_number, "BAD", blkno);
286 if (badblk++ >= MAXBAD) {
287 pwarn("EXCESSIVE BAD BLKS I=%u",
288 idesc->id_number);
289 if (preen)
290 printf(" (SKIPPING)\n");
291 else if (reply("CONTINUE") == 0)
292 exit(EEXIT);
293 return (STOP);
294 }
295 }
296 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
297 if (anyout && chkrange(blkno, 1)) {
298 res = SKIP;
299 } else if (!testbmap(blkno)) {
300 n_blks++;
301 setbmap(blkno);
302 } else {
303 blkerror(idesc->id_number, "DUP", blkno);
304 if (dupblk++ >= MAXDUP) {
305 pwarn("EXCESSIVE DUP BLKS I=%u",
306 idesc->id_number);
307 if (preen)
308 printf(" (SKIPPING)\n");
309 else if (reply("CONTINUE") == 0)
310 exit(EEXIT);
311 return (STOP);
312 }
313 new = (struct dups *)malloc(sizeof(struct dups));
314 if (new == NULL) {
315 pfatal("DUP TABLE OVERFLOW.");
316 if (reply("CONTINUE") == 0)
317 exit(EEXIT);
318 return (STOP);
319 }
320 new->dup = blkno;
321 if (muldup == 0) {
322 duplist = muldup = new;
323 new->next = 0;
324 } else {
325 new->next = muldup->next;
326 muldup->next = new;
327 }
328 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
329 if (dlp->dup == blkno)
330 break;
331 if (dlp == muldup && dlp->dup != blkno)
332 muldup = new;
333 }
334 /*
335 * count the number of blocks found in id_entryno
336 */
337 idesc->id_entryno++;
338 }
339 return (res);
340 }
341