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