pass1.c revision 1.1.1.3 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[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
43 #include <ufs/ffs/fs.h>
44
45 #include <err.h>
46 #include <string.h>
47
48 #include "fsck.h"
49
50 static ufs_daddr_t badblk;
51 static ufs_daddr_t dupblk;
52 static void checkinode __P((ino_t inumber, struct inodesc *));
53
54 void
55 pass1()
56 {
57 ino_t inumber;
58 int c, i, cgd;
59 struct inodesc idesc;
60
61 /*
62 * Set file system reserved blocks in used block map.
63 */
64 for (c = 0; c < sblock.fs_ncg; c++) {
65 cgd = cgdmin(&sblock, c);
66 if (c == 0) {
67 i = cgbase(&sblock, c);
68 cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
69 } else
70 i = cgsblock(&sblock, c);
71 for (; i < cgd; i++)
72 setbmap(i);
73 }
74 /*
75 * Find all allocated blocks.
76 */
77 memset(&idesc, 0, sizeof(struct inodesc));
78 idesc.id_type = ADDR;
79 idesc.id_func = pass1check;
80 inumber = 0;
81 n_files = n_blks = 0;
82 resetinodebuf();
83 for (c = 0; c < sblock.fs_ncg; c++) {
84 for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
85 if (inumber < ROOTINO)
86 continue;
87 checkinode(inumber, &idesc);
88 }
89 }
90 freeinodebuf();
91 }
92
93 static void
94 checkinode(inumber, idesc)
95 ino_t inumber;
96 register struct inodesc *idesc;
97 {
98 register struct dinode *dp;
99 struct zlncnt *zlnp;
100 int ndb, j;
101 mode_t mode;
102 char *symbuf;
103
104 dp = getnextinode(inumber);
105 mode = dp->di_mode & IFMT;
106 if (mode == 0) {
107 if (memcmp(dp->di_db, zino.di_db,
108 NDADDR * sizeof(ufs_daddr_t)) ||
109 memcmp(dp->di_ib, zino.di_ib,
110 NIADDR * sizeof(ufs_daddr_t)) ||
111 dp->di_mode || dp->di_size) {
112 pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
113 if (reply("CLEAR") == 1) {
114 dp = ginode(inumber);
115 clearinode(dp);
116 inodirty();
117 }
118 }
119 statemap[inumber] = USTATE;
120 return;
121 }
122 lastino = inumber;
123 if (/* dp->di_size < 0 || */
124 dp->di_size + sblock.fs_bsize - 1 < dp->di_size ||
125 (mode == IFDIR && dp->di_size > MAXDIRSIZE)) {
126 if (debug)
127 printf("bad size %qu:", dp->di_size);
128 goto unknown;
129 }
130 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
131 dp = ginode(inumber);
132 dp->di_size = sblock.fs_fsize;
133 dp->di_mode = IFREG|0600;
134 inodirty();
135 }
136 ndb = howmany(dp->di_size, sblock.fs_bsize);
137 if (ndb < 0) {
138 if (debug)
139 printf("bad size %qu ndb %d:",
140 dp->di_size, ndb);
141 goto unknown;
142 }
143 if (mode == IFBLK || mode == IFCHR)
144 ndb++;
145 if (mode == IFLNK) {
146 if (doinglevel2 &&
147 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
148 dp->di_blocks != 0) {
149 symbuf = alloca(secsize);
150 if (bread(fsreadfd, symbuf,
151 fsbtodb(&sblock, dp->di_db[0]),
152 (long)secsize) != 0)
153 errx(EEXIT, "cannot read symlink");
154 if (debug) {
155 symbuf[dp->di_size] = 0;
156 printf("convert symlink %d(%s) of size %d\n",
157 inumber, symbuf, (long)dp->di_size);
158 }
159 dp = ginode(inumber);
160 memmove(dp->di_shortlink, symbuf, (long)dp->di_size);
161 dp->di_blocks = 0;
162 inodirty();
163 }
164 /*
165 * Fake ndb value so direct/indirect block checks below
166 * will detect any garbage after symlink string.
167 */
168 if (dp->di_size < sblock.fs_maxsymlinklen) {
169 ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
170 if (ndb > NDADDR) {
171 j = ndb - NDADDR;
172 for (ndb = 1; j > 1; j--)
173 ndb *= NINDIR(&sblock);
174 ndb += NDADDR;
175 }
176 }
177 }
178 for (j = ndb; j < NDADDR; j++)
179 if (dp->di_db[j] != 0) {
180 if (debug)
181 printf("bad direct addr: %ld\n", dp->di_db[j]);
182 goto unknown;
183 }
184 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
185 ndb /= NINDIR(&sblock);
186 for (; j < NIADDR; j++)
187 if (dp->di_ib[j] != 0) {
188 if (debug)
189 printf("bad indirect addr: %ld\n",
190 dp->di_ib[j]);
191 goto unknown;
192 }
193 if (ftypeok(dp) == 0)
194 goto unknown;
195 n_files++;
196 lncntp[inumber] = dp->di_nlink;
197 if (dp->di_nlink <= 0) {
198 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
199 if (zlnp == NULL) {
200 pfatal("LINK COUNT TABLE OVERFLOW");
201 if (reply("CONTINUE") == 0)
202 exit(EEXIT);
203 } else {
204 zlnp->zlncnt = inumber;
205 zlnp->next = zlnhead;
206 zlnhead = zlnp;
207 }
208 }
209 if (mode == IFDIR) {
210 if (dp->di_size == 0)
211 statemap[inumber] = DCLEAR;
212 else
213 statemap[inumber] = DSTATE;
214 cacheino(dp, inumber);
215 } else
216 statemap[inumber] = FSTATE;
217 typemap[inumber] = IFTODT(mode);
218 if (doinglevel2 &&
219 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
220 dp = ginode(inumber);
221 dp->di_uid = dp->di_ouid;
222 dp->di_ouid = -1;
223 dp->di_gid = dp->di_ogid;
224 dp->di_ogid = -1;
225 inodirty();
226 }
227 badblk = dupblk = 0;
228 idesc->id_number = inumber;
229 (void)ckinode(dp, idesc);
230 idesc->id_entryno *= btodb(sblock.fs_fsize);
231 if (dp->di_blocks != idesc->id_entryno) {
232 pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
233 inumber, dp->di_blocks, idesc->id_entryno);
234 if (preen)
235 printf(" (CORRECTED)\n");
236 else if (reply("CORRECT") == 0)
237 return;
238 dp = ginode(inumber);
239 dp->di_blocks = idesc->id_entryno;
240 inodirty();
241 }
242 return;
243 unknown:
244 pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
245 statemap[inumber] = FCLEAR;
246 if (reply("CLEAR") == 1) {
247 statemap[inumber] = USTATE;
248 dp = ginode(inumber);
249 clearinode(dp);
250 inodirty();
251 }
252 }
253
254 int
255 pass1check(idesc)
256 register struct inodesc *idesc;
257 {
258 int res = KEEPON;
259 int anyout, nfrags;
260 ufs_daddr_t blkno = idesc->id_blkno;
261 register struct dups *dlp;
262 struct dups *new;
263
264 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
265 blkerror(idesc->id_number, "BAD", blkno);
266 if (badblk++ >= MAXBAD) {
267 pwarn("EXCESSIVE BAD BLKS I=%lu",
268 idesc->id_number);
269 if (preen)
270 printf(" (SKIPPING)\n");
271 else if (reply("CONTINUE") == 0)
272 exit(EEXIT);
273 return (STOP);
274 }
275 }
276 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
277 if (anyout && chkrange(blkno, 1)) {
278 res = SKIP;
279 } else if (!testbmap(blkno)) {
280 n_blks++;
281 setbmap(blkno);
282 } else {
283 blkerror(idesc->id_number, "DUP", blkno);
284 if (dupblk++ >= MAXDUP) {
285 pwarn("EXCESSIVE DUP BLKS I=%lu",
286 idesc->id_number);
287 if (preen)
288 printf(" (SKIPPING)\n");
289 else if (reply("CONTINUE") == 0)
290 exit(EEXIT);
291 return (STOP);
292 }
293 new = (struct dups *)malloc(sizeof(struct dups));
294 if (new == NULL) {
295 pfatal("DUP TABLE OVERFLOW.");
296 if (reply("CONTINUE") == 0)
297 exit(EEXIT);
298 return (STOP);
299 }
300 new->dup = blkno;
301 if (muldup == 0) {
302 duplist = muldup = new;
303 new->next = 0;
304 } else {
305 new->next = muldup->next;
306 muldup->next = new;
307 }
308 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
309 if (dlp->dup == blkno)
310 break;
311 if (dlp == muldup && dlp->dup != blkno)
312 muldup = new;
313 }
314 /*
315 * count the number of blocks found in id_entryno
316 */
317 idesc->id_entryno++;
318 }
319 return (res);
320 }
321