pass5.c revision 1.1 1 1.1 perseant /* $Id: pass5.c,v 1.1 1999/03/18 02:02:19 perseant Exp $ */
2 1.1 perseant
3 1.1 perseant /*
4 1.1 perseant * Copyright (c) 1980, 1986, 1993
5 1.1 perseant * The Regents of the University of California. All rights reserved.
6 1.1 perseant *
7 1.1 perseant * Redistribution and use in source and binary forms, with or without
8 1.1 perseant * modification, are permitted provided that the following conditions
9 1.1 perseant * are met:
10 1.1 perseant * 1. Redistributions of source code must retain the above copyright
11 1.1 perseant * notice, this list of conditions and the following disclaimer.
12 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer in the
14 1.1 perseant * documentation and/or other materials provided with the distribution.
15 1.1 perseant * 3. All advertising materials mentioning features or use of this software
16 1.1 perseant * must display the following acknowledgement:
17 1.1 perseant * This product includes software developed by the University of
18 1.1 perseant * California, Berkeley and its contributors.
19 1.1 perseant * 4. Neither the name of the University nor the names of its contributors
20 1.1 perseant * may be used to endorse or promote products derived from this software
21 1.1 perseant * without specific prior written permission.
22 1.1 perseant *
23 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 perseant * SUCH DAMAGE.
34 1.1 perseant */
35 1.1 perseant
36 1.1 perseant #ifndef lint
37 1.1 perseant #if 0
38 1.1 perseant static char sccsid[] = "@(#)pass5.c 8.6 (Berkeley) 11/30/94";
39 1.1 perseant #else
40 1.1 perseant static char rcsid[] = "$Id: pass5.c,v 1.1 1999/03/18 02:02:19 perseant Exp $";
41 1.1 perseant #endif
42 1.1 perseant #endif /* not lint */
43 1.1 perseant
44 1.1 perseant #include <sys/param.h>
45 1.1 perseant #include <sys/time.h>
46 1.1 perseant #include <ufs/ufs/dinode.h>
47 1.1 perseant #include <ufs/ffs/fs.h>
48 1.1 perseant #include <ufs/ffs/ffs_extern.h>
49 1.1 perseant #include <string.h>
50 1.1 perseant
51 1.1 perseant #include "fsutil.h"
52 1.1 perseant #include "fsck.h"
53 1.1 perseant #include "extern.h"
54 1.1 perseant
55 1.1 perseant void
56 1.1 perseant pass5()
57 1.1 perseant {
58 1.1 perseant int c, blk, frags, basesize, sumsize, mapsize, savednrpos;
59 1.1 perseant register struct fs *fs = &sblock;
60 1.1 perseant register struct cg *cg = &cgrp;
61 1.1 perseant daddr_t dbase, dmax;
62 1.1 perseant register daddr_t d;
63 1.1 perseant register long i, j;
64 1.1 perseant struct csum *cs;
65 1.1 perseant struct csum cstotal;
66 1.1 perseant struct inodesc idesc[3];
67 1.1 perseant char buf[MAXBSIZE];
68 1.1 perseant register struct cg *newcg = (struct cg *)buf;
69 1.1 perseant struct ocg *ocg = (struct ocg *)buf;
70 1.1 perseant
71 1.1 perseant statemap[WINO] = USTATE;
72 1.1 perseant memset(newcg, 0, (size_t)fs->fs_cgsize);
73 1.1 perseant newcg->cg_niblk = fs->fs_ipg;
74 1.1 perseant if (cvtlevel >= 3) {
75 1.1 perseant if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
76 1.1 perseant if (preen)
77 1.1 perseant pwarn("DELETING CLUSTERING MAPS\n");
78 1.1 perseant if (preen || reply("DELETE CLUSTERING MAPS")) {
79 1.1 perseant fs->fs_contigsumsize = 0;
80 1.1 perseant doinglevel1 = 1;
81 1.1 perseant sbdirty();
82 1.1 perseant }
83 1.1 perseant }
84 1.1 perseant if (fs->fs_maxcontig > 1) {
85 1.1 perseant char *doit = 0;
86 1.1 perseant
87 1.1 perseant if (fs->fs_contigsumsize < 1) {
88 1.1 perseant doit = "CREAT";
89 1.1 perseant } else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
90 1.1 perseant fs->fs_contigsumsize < FS_MAXCONTIG) {
91 1.1 perseant doit = "EXPAND";
92 1.1 perseant }
93 1.1 perseant if (doit) {
94 1.1 perseant i = fs->fs_contigsumsize;
95 1.1 perseant fs->fs_contigsumsize =
96 1.1 perseant MIN(fs->fs_maxcontig, FS_MAXCONTIG);
97 1.1 perseant if (CGSIZE(fs) > fs->fs_bsize) {
98 1.1 perseant pwarn("CANNOT %s CLUSTER MAPS\n", doit);
99 1.1 perseant fs->fs_contigsumsize = i;
100 1.1 perseant } else if (preen ||
101 1.1 perseant reply("CREATE CLUSTER MAPS")) {
102 1.1 perseant if (preen)
103 1.1 perseant pwarn("%sING CLUSTER MAPS\n",
104 1.1 perseant doit);
105 1.1 perseant fs->fs_cgsize =
106 1.1 perseant fragroundup(fs, CGSIZE(fs));
107 1.1 perseant doinglevel1 = 1;
108 1.1 perseant sbdirty();
109 1.1 perseant }
110 1.1 perseant }
111 1.1 perseant }
112 1.1 perseant }
113 1.1 perseant switch ((int)fs->fs_postblformat) {
114 1.1 perseant
115 1.1 perseant case FS_42POSTBLFMT:
116 1.1 perseant basesize = (char *)(&ocg->cg_btot[0]) -
117 1.1 perseant (char *)(&ocg->cg_firstfield);
118 1.1 perseant sumsize = &ocg->cg_iused[0] - (u_int8_t *)(&ocg->cg_btot[0]);
119 1.1 perseant mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] -
120 1.1 perseant (u_char *)&ocg->cg_iused[0];
121 1.1 perseant ocg->cg_magic = CG_MAGIC;
122 1.1 perseant savednrpos = fs->fs_nrpos;
123 1.1 perseant fs->fs_nrpos = 8;
124 1.1 perseant break;
125 1.1 perseant
126 1.1 perseant case FS_DYNAMICPOSTBLFMT:
127 1.1 perseant newcg->cg_btotoff =
128 1.1 perseant &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
129 1.1 perseant newcg->cg_boff =
130 1.1 perseant newcg->cg_btotoff + fs->fs_cpg * sizeof(int32_t);
131 1.1 perseant newcg->cg_iusedoff = newcg->cg_boff +
132 1.1 perseant fs->fs_cpg * fs->fs_nrpos * sizeof(int16_t);
133 1.1 perseant newcg->cg_freeoff =
134 1.1 perseant newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY);
135 1.1 perseant if (fs->fs_contigsumsize <= 0) {
136 1.1 perseant newcg->cg_nextfreeoff = newcg->cg_freeoff +
137 1.1 perseant howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), NBBY);
138 1.1 perseant } else {
139 1.1 perseant newcg->cg_clustersumoff = newcg->cg_freeoff +
140 1.1 perseant howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), NBBY) -
141 1.1 perseant sizeof(int32_t);
142 1.1 perseant newcg->cg_clustersumoff =
143 1.1 perseant roundup(newcg->cg_clustersumoff, sizeof(int32_t));
144 1.1 perseant newcg->cg_clusteroff = newcg->cg_clustersumoff +
145 1.1 perseant (fs->fs_contigsumsize + 1) * sizeof(int32_t);
146 1.1 perseant newcg->cg_nextfreeoff = newcg->cg_clusteroff +
147 1.1 perseant howmany(fs->fs_cpg * fs->fs_spc / NSPB(fs), NBBY);
148 1.1 perseant }
149 1.1 perseant newcg->cg_magic = CG_MAGIC;
150 1.1 perseant basesize = &newcg->cg_space[0] -
151 1.1 perseant (u_char *)(&newcg->cg_firstfield);
152 1.1 perseant sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
153 1.1 perseant mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
154 1.1 perseant break;
155 1.1 perseant
156 1.1 perseant default:
157 1.1 perseant errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n",
158 1.1 perseant fs->fs_postblformat);
159 1.1 perseant }
160 1.1 perseant memset(&idesc[0], 0, sizeof idesc);
161 1.1 perseant for (i = 0; i < 3; i++) {
162 1.1 perseant idesc[i].id_type = ADDR;
163 1.1 perseant if (doinglevel2)
164 1.1 perseant idesc[i].id_fix = FIX;
165 1.1 perseant }
166 1.1 perseant memset(&cstotal, 0, sizeof(struct csum));
167 1.1 perseant j = blknum(fs, fs->fs_size + fs->fs_frag - 1);
168 1.1 perseant for (i = fs->fs_size; i < j; i++)
169 1.1 perseant setbmap(i);
170 1.1 perseant for (c = 0; c < fs->fs_ncg; c++) {
171 1.1 perseant getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
172 1.1 perseant if (!cg_chkmagic(cg))
173 1.1 perseant pfatal("CG %d: BAD MAGIC NUMBER\n", c);
174 1.1 perseant dbase = cgbase(fs, c);
175 1.1 perseant dmax = dbase + fs->fs_fpg;
176 1.1 perseant if (dmax > fs->fs_size)
177 1.1 perseant dmax = fs->fs_size;
178 1.1 perseant newcg->cg_time = cg->cg_time;
179 1.1 perseant newcg->cg_cgx = c;
180 1.1 perseant if (c == fs->fs_ncg - 1)
181 1.1 perseant newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg;
182 1.1 perseant else
183 1.1 perseant newcg->cg_ncyl = fs->fs_cpg;
184 1.1 perseant newcg->cg_ndblk = dmax - dbase;
185 1.1 perseant if (fs->fs_contigsumsize > 0)
186 1.1 perseant newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
187 1.1 perseant newcg->cg_cs.cs_ndir = 0;
188 1.1 perseant newcg->cg_cs.cs_nffree = 0;
189 1.1 perseant newcg->cg_cs.cs_nbfree = 0;
190 1.1 perseant newcg->cg_cs.cs_nifree = fs->fs_ipg;
191 1.1 perseant if (cg->cg_rotor < newcg->cg_ndblk)
192 1.1 perseant newcg->cg_rotor = cg->cg_rotor;
193 1.1 perseant else
194 1.1 perseant newcg->cg_rotor = 0;
195 1.1 perseant if (cg->cg_frotor < newcg->cg_ndblk)
196 1.1 perseant newcg->cg_frotor = cg->cg_frotor;
197 1.1 perseant else
198 1.1 perseant newcg->cg_frotor = 0;
199 1.1 perseant if (cg->cg_irotor < newcg->cg_niblk)
200 1.1 perseant newcg->cg_irotor = cg->cg_irotor;
201 1.1 perseant else
202 1.1 perseant newcg->cg_irotor = 0;
203 1.1 perseant memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
204 1.1 perseant memset(&cg_blktot(newcg)[0], 0,
205 1.1 perseant (size_t)(sumsize + mapsize));
206 1.1 perseant if (fs->fs_postblformat == FS_42POSTBLFMT)
207 1.1 perseant ocg->cg_magic = CG_MAGIC;
208 1.1 perseant j = fs->fs_ipg * c;
209 1.1 perseant for (i = 0; i < fs->fs_ipg; j++, i++) {
210 1.1 perseant switch (statemap[j]) {
211 1.1 perseant
212 1.1 perseant case USTATE:
213 1.1 perseant break;
214 1.1 perseant
215 1.1 perseant case DSTATE:
216 1.1 perseant case DCLEAR:
217 1.1 perseant case DFOUND:
218 1.1 perseant newcg->cg_cs.cs_ndir++;
219 1.1 perseant /* fall through */
220 1.1 perseant
221 1.1 perseant case FSTATE:
222 1.1 perseant case FCLEAR:
223 1.1 perseant newcg->cg_cs.cs_nifree--;
224 1.1 perseant setbit(cg_inosused(newcg), i);
225 1.1 perseant break;
226 1.1 perseant
227 1.1 perseant default:
228 1.1 perseant if (j < ROOTINO)
229 1.1 perseant break;
230 1.1 perseant errexit("BAD STATE %d FOR INODE I=%ld",
231 1.1 perseant statemap[j], j);
232 1.1 perseant }
233 1.1 perseant }
234 1.1 perseant if (c == 0)
235 1.1 perseant for (i = 0; i < ROOTINO; i++) {
236 1.1 perseant setbit(cg_inosused(newcg), i);
237 1.1 perseant newcg->cg_cs.cs_nifree--;
238 1.1 perseant }
239 1.1 perseant for (i = 0, d = dbase;
240 1.1 perseant d < dmax;
241 1.1 perseant d += fs->fs_frag, i += fs->fs_frag) {
242 1.1 perseant frags = 0;
243 1.1 perseant for (j = 0; j < fs->fs_frag; j++) {
244 1.1 perseant if (testbmap(d + j))
245 1.1 perseant continue;
246 1.1 perseant setbit(cg_blksfree(newcg), i + j);
247 1.1 perseant frags++;
248 1.1 perseant }
249 1.1 perseant if (frags == fs->fs_frag) {
250 1.1 perseant newcg->cg_cs.cs_nbfree++;
251 1.1 perseant j = cbtocylno(fs, i);
252 1.1 perseant cg_blktot(newcg)[j]++;
253 1.1 perseant cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
254 1.1 perseant if (fs->fs_contigsumsize > 0)
255 1.1 perseant setbit(cg_clustersfree(newcg),
256 1.1 perseant i / fs->fs_frag);
257 1.1 perseant } else if (frags > 0) {
258 1.1 perseant newcg->cg_cs.cs_nffree += frags;
259 1.1 perseant blk = blkmap(fs, cg_blksfree(newcg), i);
260 1.1 perseant ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
261 1.1 perseant }
262 1.1 perseant }
263 1.1 perseant if (fs->fs_contigsumsize > 0) {
264 1.1 perseant int32_t *sump = cg_clustersum(newcg);
265 1.1 perseant u_char *mapp = cg_clustersfree(newcg);
266 1.1 perseant int map = *mapp++;
267 1.1 perseant int bit = 1;
268 1.1 perseant int run = 0;
269 1.1 perseant
270 1.1 perseant for (i = 0; i < newcg->cg_nclusterblks; i++) {
271 1.1 perseant if ((map & bit) != 0) {
272 1.1 perseant run++;
273 1.1 perseant } else if (run != 0) {
274 1.1 perseant if (run > fs->fs_contigsumsize)
275 1.1 perseant run = fs->fs_contigsumsize;
276 1.1 perseant sump[run]++;
277 1.1 perseant run = 0;
278 1.1 perseant }
279 1.1 perseant if ((i & (NBBY - 1)) != (NBBY - 1)) {
280 1.1 perseant bit <<= 1;
281 1.1 perseant } else {
282 1.1 perseant map = *mapp++;
283 1.1 perseant bit = 1;
284 1.1 perseant }
285 1.1 perseant }
286 1.1 perseant if (run != 0) {
287 1.1 perseant if (run > fs->fs_contigsumsize)
288 1.1 perseant run = fs->fs_contigsumsize;
289 1.1 perseant sump[run]++;
290 1.1 perseant }
291 1.1 perseant }
292 1.1 perseant cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
293 1.1 perseant cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
294 1.1 perseant cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
295 1.1 perseant cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
296 1.1 perseant cs = &fs->fs_cs(fs, c);
297 1.1 perseant if (memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
298 1.1 perseant dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
299 1.1 perseant memcpy(cs, &newcg->cg_cs, sizeof *cs);
300 1.1 perseant sbdirty();
301 1.1 perseant }
302 1.1 perseant if (doinglevel1) {
303 1.1 perseant memcpy(cg, newcg, (size_t)fs->fs_cgsize);
304 1.1 perseant cgdirty();
305 1.1 perseant continue;
306 1.1 perseant }
307 1.1 perseant if (memcmp(cg_inosused(newcg),
308 1.1 perseant cg_inosused(cg), mapsize) != 0 &&
309 1.1 perseant dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
310 1.1 perseant memcpy(cg_inosused(cg), cg_inosused(newcg),
311 1.1 perseant (size_t)mapsize);
312 1.1 perseant cgdirty();
313 1.1 perseant }
314 1.1 perseant if ((memcmp(newcg, cg, basesize) != 0 ||
315 1.1 perseant memcmp(&cg_blktot(newcg)[0],
316 1.1 perseant &cg_blktot(cg)[0], sumsize) != 0) &&
317 1.1 perseant dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
318 1.1 perseant memcpy(cg, newcg, (size_t)basesize);
319 1.1 perseant memcpy(&cg_blktot(cg)[0],
320 1.1 perseant &cg_blktot(newcg)[0], (size_t)sumsize);
321 1.1 perseant cgdirty();
322 1.1 perseant }
323 1.1 perseant }
324 1.1 perseant if (fs->fs_postblformat == FS_42POSTBLFMT)
325 1.1 perseant fs->fs_nrpos = savednrpos;
326 1.1 perseant if (memcmp(&cstotal, &fs->fs_cstotal, sizeof *cs) != 0
327 1.1 perseant && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
328 1.1 perseant memcpy(&fs->fs_cstotal, &cstotal, sizeof *cs);
329 1.1 perseant fs->fs_ronly = 0;
330 1.1 perseant fs->fs_fmod = 0;
331 1.1 perseant sbdirty();
332 1.1 perseant }
333 1.1 perseant }
334