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