Commit | Line | Data |
---|---|---|
76797561 | 1 | /* |
fe32782c KM |
2 | * Copyright (c) 1980, 1986 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
76797561 DF |
16 | */ |
17 | ||
19052d90 | 18 | #ifndef lint |
c005a931 | 19 | static char sccsid[] = "@(#)pass1.c 5.13 (Berkeley) %G%"; |
fe32782c | 20 | #endif /* not lint */ |
19052d90 KM |
21 | |
22 | #include <sys/param.h> | |
72e5286b | 23 | #include <ufs/dinode.h> |
4d7f4685 | 24 | #include <ufs/fs.h> |
19052d90 KM |
25 | #include "fsck.h" |
26 | ||
993a756c KM |
27 | static daddr_t badblk; |
28 | static daddr_t dupblk; | |
29 | int pass1check(); | |
a0cb24a2 | 30 | struct dinode *getnextinode(); |
19052d90 KM |
31 | |
32 | pass1() | |
33 | { | |
993a756c | 34 | register int c, i, j; |
569ec282 | 35 | register struct dinode *dp; |
82dc9a9e | 36 | struct zlncnt *zlnp; |
569ec282 | 37 | int ndb, cgd; |
19052d90 KM |
38 | struct inodesc idesc; |
39 | ino_t inumber; | |
40 | ||
7718c0e6 KM |
41 | /* |
42 | * Set file system reserved blocks in used block map. | |
43 | */ | |
44 | for (c = 0; c < sblock.fs_ncg; c++) { | |
45 | cgd = cgdmin(&sblock, c); | |
46 | if (c == 0) { | |
47 | i = cgbase(&sblock, c); | |
48 | cgd += howmany(sblock.fs_cssize, sblock.fs_fsize); | |
49 | } else | |
50 | i = cgsblock(&sblock, c); | |
51 | for (; i < cgd; i++) | |
52 | setbmap(i); | |
53 | } | |
54 | /* | |
55 | * Find all allocated blocks. | |
56 | */ | |
19052d90 KM |
57 | bzero((char *)&idesc, sizeof(struct inodesc)); |
58 | idesc.id_type = ADDR; | |
59 | idesc.id_func = pass1check; | |
60 | inumber = 0; | |
993a756c | 61 | n_files = n_blks = 0; |
a0cb24a2 | 62 | resetinodebuf(); |
19052d90 | 63 | for (c = 0; c < sblock.fs_ncg; c++) { |
19052d90 | 64 | for (i = 0; i < sblock.fs_ipg; i++, inumber++) { |
993a756c | 65 | if (inumber < ROOTINO) |
19052d90 | 66 | continue; |
a0cb24a2 | 67 | dp = getnextinode(inumber); |
569ec282 | 68 | if ((dp->di_mode & IFMT) == 0) { |
993a756c KM |
69 | if (bcmp((char *)dp->di_db, (char *)zino.di_db, |
70 | NDADDR * sizeof(daddr_t)) || | |
71 | bcmp((char *)dp->di_ib, (char *)zino.di_ib, | |
72 | NIADDR * sizeof(daddr_t)) || | |
73 | dp->di_mode || dp->di_size) { | |
19052d90 KM |
74 | pfatal("PARTIALLY ALLOCATED INODE I=%u", |
75 | inumber); | |
76 | if (reply("CLEAR") == 1) { | |
0b4b70a9 | 77 | dp = ginode(inumber); |
569ec282 | 78 | clearinode(dp); |
19052d90 | 79 | inodirty(); |
19052d90 KM |
80 | } |
81 | } | |
993a756c KM |
82 | statemap[inumber] = USTATE; |
83 | continue; | |
84 | } | |
85 | lastino = inumber; | |
98061440 KM |
86 | if (dp->di_size < 0 || |
87 | dp->di_size + sblock.fs_bsize - 1 < 0) { | |
993a756c KM |
88 | if (debug) |
89 | printf("bad size %d:", dp->di_size); | |
90 | goto unknown; | |
91 | } | |
958be3d3 KM |
92 | if (!preen && (dp->di_mode & IFMT) == IFMT && |
93 | reply("HOLD BAD BLOCK") == 1) { | |
0b4b70a9 | 94 | dp = ginode(inumber); |
958be3d3 KM |
95 | dp->di_size = sblock.fs_fsize; |
96 | dp->di_mode = IFREG|0600; | |
97 | inodirty(); | |
98 | } | |
993a756c | 99 | ndb = howmany(dp->di_size, sblock.fs_bsize); |
dad05901 KM |
100 | if (ndb < 0) { |
101 | if (debug) | |
102 | printf("bad size %d ndb %d:", | |
103 | dp->di_size, ndb); | |
104 | goto unknown; | |
105 | } | |
569ec282 KM |
106 | if ((dp->di_mode & IFMT) == IFBLK || |
107 | (dp->di_mode & IFMT) == IFCHR) | |
993a756c KM |
108 | ndb++; |
109 | for (j = ndb; j < NDADDR; j++) | |
110 | if (dp->di_db[j] != 0) { | |
111 | if (debug) | |
112 | printf("bad direct addr: %d\n", | |
113 | dp->di_db[j]); | |
114 | goto unknown; | |
115 | } | |
116 | for (j = 0, ndb -= NDADDR; ndb > 0; j++) | |
117 | ndb /= NINDIR(&sblock); | |
118 | for (; j < NIADDR; j++) | |
119 | if (dp->di_ib[j] != 0) { | |
120 | if (debug) | |
121 | printf("bad indirect addr: %d\n", | |
122 | dp->di_ib[j]); | |
123 | goto unknown; | |
124 | } | |
958be3d3 | 125 | if (ftypeok(dp) == 0) |
993a756c KM |
126 | goto unknown; |
127 | n_files++; | |
128 | lncntp[inumber] = dp->di_nlink; | |
129 | if (dp->di_nlink <= 0) { | |
82dc9a9e KM |
130 | zlnp = (struct zlncnt *)malloc(sizeof *zlnp); |
131 | if (zlnp == NULL) { | |
993a756c KM |
132 | pfatal("LINK COUNT TABLE OVERFLOW"); |
133 | if (reply("CONTINUE") == 0) | |
134 | errexit(""); | |
82dc9a9e KM |
135 | } else { |
136 | zlnp->zlncnt = inumber; | |
137 | zlnp->next = zlnhead; | |
138 | zlnhead = zlnp; | |
993a756c KM |
139 | } |
140 | } | |
57117c0f | 141 | if ((dp->di_mode & IFMT) == IFDIR) { |
c005a931 KM |
142 | if (dp->di_size == 0) |
143 | statemap[inumber] = DCLEAR; | |
144 | else | |
145 | statemap[inumber] = DSTATE; | |
57117c0f KM |
146 | cacheino(dp, inumber); |
147 | } else | |
148 | statemap[inumber] = FSTATE; | |
569ec282 | 149 | badblk = dupblk = 0; |
993a756c | 150 | idesc.id_number = inumber; |
993a756c | 151 | (void)ckinode(dp, &idesc); |
f0ed004a KM |
152 | idesc.id_entryno *= btodb(sblock.fs_fsize); |
153 | if (dp->di_blocks != idesc.id_entryno) { | |
993a756c | 154 | pwarn("INCORRECT BLOCK COUNT I=%u (%ld should be %ld)", |
f0ed004a | 155 | inumber, dp->di_blocks, idesc.id_entryno); |
993a756c KM |
156 | if (preen) |
157 | printf(" (CORRECTED)\n"); | |
158 | else if (reply("CORRECT") == 0) | |
159 | continue; | |
0b4b70a9 | 160 | dp = ginode(inumber); |
f0ed004a | 161 | dp->di_blocks = idesc.id_entryno; |
993a756c KM |
162 | inodirty(); |
163 | } | |
164 | continue; | |
165 | unknown: | |
166 | pfatal("UNKNOWN FILE TYPE I=%u", inumber); | |
7b9b1d1f | 167 | statemap[inumber] = FCLEAR; |
993a756c | 168 | if (reply("CLEAR") == 1) { |
7b9b1d1f | 169 | statemap[inumber] = USTATE; |
0b4b70a9 | 170 | dp = ginode(inumber); |
569ec282 | 171 | clearinode(dp); |
993a756c | 172 | inodirty(); |
19052d90 KM |
173 | } |
174 | } | |
19052d90 | 175 | } |
a0cb24a2 | 176 | freeinodebuf(); |
19052d90 KM |
177 | } |
178 | ||
179 | pass1check(idesc) | |
180 | register struct inodesc *idesc; | |
181 | { | |
19052d90 KM |
182 | int res = KEEPON; |
183 | int anyout, nfrags; | |
184 | daddr_t blkno = idesc->id_blkno; | |
62e6c152 KM |
185 | register struct dups *dlp; |
186 | struct dups *new; | |
19052d90 | 187 | |
569ec282 KM |
188 | if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { |
189 | blkerror(idesc->id_number, "BAD", blkno); | |
7718c0e6 KM |
190 | if (++badblk >= MAXBAD) { |
191 | pwarn("EXCESSIVE BAD BLKS I=%u", | |
192 | idesc->id_number); | |
193 | if (preen) | |
194 | printf(" (SKIPPING)\n"); | |
195 | else if (reply("CONTINUE") == 0) | |
196 | errexit(""); | |
197 | return (STOP); | |
198 | } | |
199 | } | |
19052d90 | 200 | for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { |
569ec282 | 201 | if (anyout && chkrange(blkno, 1)) { |
19052d90 | 202 | res = SKIP; |
569ec282 | 203 | } else if (!testbmap(blkno)) { |
7718c0e6 KM |
204 | n_blks++; |
205 | setbmap(blkno); | |
206 | } else { | |
569ec282 | 207 | blkerror(idesc->id_number, "DUP", blkno); |
19052d90 KM |
208 | if (++dupblk >= MAXDUP) { |
209 | pwarn("EXCESSIVE DUP BLKS I=%u", | |
210 | idesc->id_number); | |
211 | if (preen) | |
212 | printf(" (SKIPPING)\n"); | |
213 | else if (reply("CONTINUE") == 0) | |
214 | errexit(""); | |
215 | return (STOP); | |
216 | } | |
62e6c152 KM |
217 | new = (struct dups *)malloc(sizeof(struct dups)); |
218 | if (new == NULL) { | |
19052d90 KM |
219 | pfatal("DUP TABLE OVERFLOW."); |
220 | if (reply("CONTINUE") == 0) | |
221 | errexit(""); | |
222 | return (STOP); | |
223 | } | |
62e6c152 KM |
224 | new->dup = blkno; |
225 | if (muldup == 0) { | |
226 | duplist = muldup = new; | |
227 | new->next = 0; | |
228 | } else { | |
229 | new->next = muldup->next; | |
230 | muldup->next = new; | |
19052d90 | 231 | } |
62e6c152 KM |
232 | for (dlp = duplist; dlp != muldup; dlp = dlp->next) |
233 | if (dlp->dup == blkno) | |
234 | break; | |
235 | if (dlp == muldup && dlp->dup != blkno) | |
236 | muldup = new; | |
19052d90 | 237 | } |
f0ed004a KM |
238 | /* |
239 | * count the number of blocks found in id_entryno | |
240 | */ | |
241 | idesc->id_entryno++; | |
19052d90 KM |
242 | } |
243 | return (res); | |
244 | } |