pass1.c revision 1.9 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.9 1994/06/29 11:01:35 ws 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 bzero((char *)&idesc, 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 (bcmp((char *)dp->di_db, (char *)zino.di_db,
105 NDADDR * sizeof(daddr_t)) ||
106 bcmp((char *)dp->di_ib, (char *)zino.di_ib,
107 NIADDR * sizeof(daddr_t)) ||
108 dp->di_mode || dp->di_size) {
109 pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
110 if (reply("CLEAR") == 1) {
111 dp = ginode(inumber);
112 clearinode(dp);
113 inodirty();
114 }
115 }
116 statemap[inumber] = USTATE;
117 return;
118 }
119 lastino = inumber;
120 if (/* dp->di_size < 0 || */
121 dp->di_size + sblock.fs_bsize - 1 < dp->di_size) {
122 if (debug)
123 printf("bad size %qu:", dp->di_size);
124 goto unknown;
125 }
126 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
127 dp = ginode(inumber);
128 dp->di_size = sblock.fs_fsize;
129 dp->di_mode = IFREG|0600;
130 inodirty();
131 }
132 ndb = howmany(dp->di_size, sblock.fs_bsize);
133 if (ndb < 0) {
134 if (debug)
135 printf("bad size %qu ndb %d:",
136 dp->di_size, ndb);
137 goto unknown;
138 }
139 if (mode == IFBLK || mode == IFCHR)
140 ndb++;
141 if (mode == IFLNK) {
142 /*
143 * Note that the old fastlink format always had di_blocks set
144 * to 0. Other than that we no longer use the `spare' field
145 * (which is now the extended uid) for sanity checking, the
146 * new format is the same as the old. We simply ignore the
147 * conversion altogether. - mycroft, 19MAY1994
148 */
149 if (doinglevel2 &&
150 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
151 dp->di_blocks != 0) {
152 symbuf = alloca(secsize);
153 if (bread(fsreadfd, symbuf,
154 fsbtodb(&sblock, dp->di_db[0]),
155 (long)secsize) != 0)
156 errexit("cannot read symlink");
157 if (debug) {
158 symbuf[dp->di_size] = 0;
159 printf("convert symlink %d(%s) of size %d\n",
160 inumber, symbuf, (long)dp->di_size);
161 }
162 dp = ginode(inumber);
163 bcopy(symbuf, (caddr_t)dp->di_shortlink,
164 (long)dp->di_size);
165 dp->di_blocks = 0;
166 inodirty();
167 }
168 /*
169 * Fake ndb value so direct/indirect block checks below
170 * will detect any garbage after symlink string.
171 */
172 if (dp->di_size < sblock.fs_maxsymlinklen ||
173 (sblock.fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
174 ndb = howmany(dp->di_size, sizeof(daddr_t));
175 if (ndb > NDADDR) {
176 j = ndb - NDADDR;
177 for (ndb = 1; j > 1; j--)
178 ndb *= NINDIR(&sblock);
179 ndb += NDADDR;
180 }
181 }
182 }
183 for (j = ndb; j < NDADDR; j++)
184 if (dp->di_db[j] != 0) {
185 if (debug)
186 printf("bad direct addr: %ld\n", dp->di_db[j]);
187 goto unknown;
188 }
189 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
190 ndb /= NINDIR(&sblock);
191 for (; j < NIADDR; j++)
192 if (dp->di_ib[j] != 0) {
193 if (debug)
194 printf("bad indirect addr: %ld\n",
195 dp->di_ib[j]);
196 goto unknown;
197 }
198 if (ftypeok(dp) == 0)
199 goto unknown;
200 n_files++;
201 lncntp[inumber] = dp->di_nlink;
202 if (dp->di_nlink <= 0) {
203 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
204 if (zlnp == NULL) {
205 pfatal("LINK COUNT TABLE OVERFLOW");
206 if (reply("CONTINUE") == 0)
207 errexit("");
208 } else {
209 zlnp->zlncnt = inumber;
210 zlnp->next = zlnhead;
211 zlnhead = zlnp;
212 }
213 }
214 if (mode == IFDIR) {
215 if (dp->di_size == 0)
216 statemap[inumber] = DCLEAR;
217 else
218 statemap[inumber] = DSTATE;
219 cacheino(dp, inumber);
220 } else
221 statemap[inumber] = FSTATE;
222 typemap[inumber] = IFTODT(mode);
223 if (doinglevel2 &&
224 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
225 dp = ginode(inumber);
226 dp->di_uid = dp->di_ouid;
227 dp->di_ouid = -1;
228 dp->di_gid = dp->di_ogid;
229 dp->di_ogid = -1;
230 inodirty();
231 }
232 badblk = dupblk = 0;
233 idesc->id_number = inumber;
234 (void)ckinode(dp, idesc);
235 idesc->id_entryno *= btodb(sblock.fs_fsize);
236 if (dp->di_blocks != idesc->id_entryno) {
237 pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
238 inumber, dp->di_blocks, idesc->id_entryno);
239 if (preen)
240 printf(" (CORRECTED)\n");
241 else if (reply("CORRECT") == 0)
242 return;
243 dp = ginode(inumber);
244 dp->di_blocks = idesc->id_entryno;
245 inodirty();
246 }
247 return;
248 unknown:
249 pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
250 statemap[inumber] = FCLEAR;
251 if (reply("CLEAR") == 1) {
252 statemap[inumber] = USTATE;
253 dp = ginode(inumber);
254 clearinode(dp);
255 inodirty();
256 }
257 }
258
259 pass1check(idesc)
260 register struct inodesc *idesc;
261 {
262 int res = KEEPON;
263 int anyout, nfrags;
264 daddr_t blkno = idesc->id_blkno;
265 register struct dups *dlp;
266 struct dups *new;
267
268 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
269 blkerror(idesc->id_number, "BAD", blkno);
270 if (badblk++ >= MAXBAD) {
271 pwarn("EXCESSIVE BAD BLKS I=%lu",
272 idesc->id_number);
273 if (preen)
274 printf(" (SKIPPING)\n");
275 else if (reply("CONTINUE") == 0)
276 errexit("");
277 return (STOP);
278 }
279 }
280 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
281 if (anyout && chkrange(blkno, 1)) {
282 res = SKIP;
283 } else if (!testbmap(blkno)) {
284 n_blks++;
285 setbmap(blkno);
286 } else {
287 blkerror(idesc->id_number, "DUP", blkno);
288 if (dupblk++ >= MAXDUP) {
289 pwarn("EXCESSIVE DUP BLKS I=%lu",
290 idesc->id_number);
291 if (preen)
292 printf(" (SKIPPING)\n");
293 else if (reply("CONTINUE") == 0)
294 errexit("");
295 return (STOP);
296 }
297 new = (struct dups *)malloc(sizeof(struct dups));
298 if (new == NULL) {
299 pfatal("DUP TABLE OVERFLOW.");
300 if (reply("CONTINUE") == 0)
301 errexit("");
302 return (STOP);
303 }
304 new->dup = blkno;
305 if (muldup == 0) {
306 duplist = muldup = new;
307 new->next = 0;
308 } else {
309 new->next = muldup->next;
310 muldup->next = new;
311 }
312 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
313 if (dlp->dup == blkno)
314 break;
315 if (dlp == muldup && dlp->dup != blkno)
316 muldup = new;
317 }
318 /*
319 * count the number of blocks found in id_entryno
320 */
321 idesc->id_entryno++;
322 }
323 return (res);
324 }
325