2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)pass5.c 8.9 (Berkeley) 4/28/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/sysctl.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ffs/fs.h>
50 static void check_maps(u_char *, u_char *, int, int, const char *, int *, int, int);
55 int c, i, j, blk, frags, basesize, mapsize;
56 int inomapsize, blkmapsize;
57 struct fs *fs = &sblock;
58 struct cg *cg = &cgrp;
59 ufs2_daddr_t d, dbase, dmax;
60 int excessdirs, rewritecg = 0;
62 struct csum_total cstotal;
63 struct inodesc idesc[3];
65 struct cg *newcg = (struct cg *)buf;
67 inoinfo(WINO)->ino_state = USTATE;
68 memset(newcg, 0, (size_t)fs->fs_cgsize);
69 newcg->cg_niblk = fs->fs_ipg;
71 if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
73 pwarn("DELETING CLUSTERING MAPS\n");
74 if (preen || reply("DELETE CLUSTERING MAPS")) {
75 fs->fs_contigsumsize = 0;
80 if (fs->fs_maxcontig > 1) {
83 if (fs->fs_contigsumsize < 1) {
85 } else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
86 fs->fs_contigsumsize < FS_MAXCONTIG) {
90 i = fs->fs_contigsumsize;
91 fs->fs_contigsumsize =
92 MIN(fs->fs_maxcontig, FS_MAXCONTIG);
93 if (CGSIZE(fs) > (u_int)fs->fs_bsize) {
94 pwarn("CANNOT %s CLUSTER MAPS\n", doit);
95 fs->fs_contigsumsize = i;
97 reply("CREATE CLUSTER MAPS")) {
99 pwarn("%sING CLUSTER MAPS\n",
102 fragroundup(fs, CGSIZE(fs));
109 basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
110 if (sblock.fs_magic == FS_UFS2_MAGIC) {
111 newcg->cg_iusedoff = basesize;
114 * We reserve the space for the old rotation summary
115 * tables for the benefit of old kernels, but do not
116 * maintain them in modern kernels. In time, they can
119 newcg->cg_old_btotoff = basesize;
120 newcg->cg_old_boff = newcg->cg_old_btotoff +
121 fs->fs_old_cpg * sizeof(int32_t);
122 newcg->cg_iusedoff = newcg->cg_old_boff +
123 fs->fs_old_cpg * fs->fs_old_nrpos * sizeof(u_int16_t);
124 memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
126 inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
127 newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
128 blkmapsize = howmany(fs->fs_fpg, CHAR_BIT);
129 newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
130 if (fs->fs_contigsumsize > 0) {
131 newcg->cg_clustersumoff = newcg->cg_nextfreeoff -
133 newcg->cg_clustersumoff =
134 roundup(newcg->cg_clustersumoff, sizeof(u_int32_t));
135 newcg->cg_clusteroff = newcg->cg_clustersumoff +
136 (fs->fs_contigsumsize + 1) * sizeof(u_int32_t);
137 newcg->cg_nextfreeoff = newcg->cg_clusteroff +
138 howmany(fragstoblks(fs, fs->fs_fpg), CHAR_BIT);
140 newcg->cg_magic = CG_MAGIC;
141 mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
142 memset(&idesc[0], 0, sizeof idesc);
143 for (i = 0; i < 3; i++)
144 idesc[i].id_type = ADDR;
145 memset(&cstotal, 0, sizeof(struct csum_total));
146 dmax = blknum(fs, fs->fs_size + fs->fs_frag - 1);
147 for (d = fs->fs_size; d < dmax; d++)
149 for (c = 0; c < fs->fs_ncg; c++) {
151 printf("%s: phase 5: cyl group %d of %d (%d%%)\n",
152 cdevname, c, sblock.fs_ncg,
153 c * 100 / sblock.fs_ncg);
157 setproctitle("%s p5 %d%%", cdevname,
158 c * 100 / sblock.fs_ncg);
161 getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
162 if (!cg_chkmagic(cg))
163 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
164 newcg->cg_time = cg->cg_time;
165 newcg->cg_old_time = cg->cg_old_time;
167 dbase = cgbase(fs, c);
168 dmax = dbase + fs->fs_fpg;
169 if (dmax > fs->fs_size)
171 newcg->cg_ndblk = dmax - dbase;
172 if (fs->fs_magic == FS_UFS1_MAGIC) {
173 if (c == fs->fs_ncg - 1)
174 newcg->cg_old_ncyl = howmany(newcg->cg_ndblk,
175 fs->fs_fpg / fs->fs_old_cpg);
177 newcg->cg_old_ncyl = fs->fs_old_cpg;
178 newcg->cg_old_niblk = fs->fs_ipg;
181 if (fs->fs_contigsumsize > 0)
182 newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
183 newcg->cg_cs.cs_ndir = 0;
184 newcg->cg_cs.cs_nffree = 0;
185 newcg->cg_cs.cs_nbfree = 0;
186 newcg->cg_cs.cs_nifree = fs->fs_ipg;
187 if (cg->cg_rotor >= 0 && cg->cg_rotor < newcg->cg_ndblk)
188 newcg->cg_rotor = cg->cg_rotor;
191 if (cg->cg_frotor >= 0 && cg->cg_frotor < newcg->cg_ndblk)
192 newcg->cg_frotor = cg->cg_frotor;
194 newcg->cg_frotor = 0;
195 if (cg->cg_irotor >= 0 && cg->cg_irotor < fs->fs_ipg)
196 newcg->cg_irotor = cg->cg_irotor;
198 newcg->cg_irotor = 0;
199 if (fs->fs_magic == FS_UFS1_MAGIC) {
200 newcg->cg_initediblk = 0;
202 if ((unsigned)cg->cg_initediblk > fs->fs_ipg)
203 newcg->cg_initediblk = fs->fs_ipg;
205 newcg->cg_initediblk = cg->cg_initediblk;
207 memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
208 memset(cg_inosused(newcg), 0, (size_t)(mapsize));
210 for (i = 0; i < inostathead[c].il_numalloced; j++, i++) {
211 switch (inoinfo(j)->ino_state) {
219 newcg->cg_cs.cs_ndir++;
224 newcg->cg_cs.cs_nifree--;
225 setbit(cg_inosused(newcg), i);
229 if (j < (int)ROOTINO)
231 errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
232 inoinfo(j)->ino_state, j);
236 for (i = 0; i < (int)ROOTINO; i++) {
237 setbit(cg_inosused(newcg), i);
238 newcg->cg_cs.cs_nifree--;
240 for (i = 0, d = dbase;
242 d += fs->fs_frag, i += fs->fs_frag) {
244 for (j = 0; j < fs->fs_frag; j++) {
247 setbit(cg_blksfree(newcg), i + j);
250 if (frags == fs->fs_frag) {
251 newcg->cg_cs.cs_nbfree++;
252 if (fs->fs_contigsumsize > 0)
253 setbit(cg_clustersfree(newcg),
255 } else if (frags > 0) {
256 newcg->cg_cs.cs_nffree += frags;
257 blk = blkmap(fs, cg_blksfree(newcg), i);
258 ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
261 if (fs->fs_contigsumsize > 0) {
262 int32_t *sump = cg_clustersum(newcg);
263 u_char *mapp = cg_clustersfree(newcg);
268 for (i = 0; i < newcg->cg_nclusterblks; i++) {
269 if ((map & bit) != 0) {
271 } else if (run != 0) {
272 if (run > fs->fs_contigsumsize)
273 run = fs->fs_contigsumsize;
277 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
285 if (run > fs->fs_contigsumsize)
286 run = fs->fs_contigsumsize;
290 cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
291 cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
292 cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
293 cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
294 cs = &fs->fs_cs(fs, c);
295 if (cursnapshot == 0 &&
296 memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
297 dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
298 memmove(cs, &newcg->cg_cs, sizeof *cs);
302 memmove(cg, newcg, (size_t)fs->fs_cgsize);
306 if (cursnapshot == 0 &&
307 memcmp(newcg, cg, basesize) != 0 &&
308 dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
309 memmove(cg, newcg, (size_t)basesize);
312 if (bkgrdflag != 0 || usedsoftdep || debug) {
313 excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
314 if (excessdirs < 0) {
315 pfatal("LOST %d DIRECTORIES\n", -excessdirs);
319 check_maps(cg_inosused(newcg), cg_inosused(cg),
320 inomapsize, cg->cg_cgx * fs->fs_ipg, "DIR",
321 freedirs, 0, excessdirs);
322 check_maps(cg_inosused(newcg), cg_inosused(cg),
323 inomapsize, cg->cg_cgx * fs->fs_ipg, "FILE",
324 freefiles, excessdirs, fs->fs_ipg);
325 check_maps(cg_blksfree(cg), cg_blksfree(newcg),
326 blkmapsize, cg->cg_cgx * fs->fs_fpg, "FRAG",
327 freeblks, 0, fs->fs_fpg);
329 if (cursnapshot == 0 &&
330 memcmp(cg_inosused(newcg), cg_inosused(cg), mapsize) != 0 &&
331 dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
332 memmove(cg_inosused(cg), cg_inosused(newcg),
337 if (cursnapshot == 0 &&
338 memcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal) != 0
339 && dofix(&idesc[0], "SUMMARY BLK COUNT(S) WRONG IN SUPERBLK")) {
340 memmove(&fs->fs_cstotal, &cstotal, sizeof cstotal);
349 u_char *map1, /* map of claimed allocations */
350 u_char *map2, /* map of determined allocations */
351 int mapsize, /* size of above two maps */
352 int startvalue, /* resource value for first element in map */
353 const char *name, /* name of resource found in maps */
354 int *opcode, /* sysctl opcode to free resource */
355 int skip, /* number of entries to skip before starting to free */
356 int limit) /* limit on number of entries to free */
360 long i, j, k, l, m, n, size;
361 int astart, aend, ustart, uend;
362 void (*msg)(const char *fmt, ...);
368 astart = ustart = aend = uend = -1;
369 for (i = 0; i < mapsize; i++) {
374 for (m = 0, l = 1; m < CHAR_BIT; m++, l <<= 1) {
375 if ((j & l) == (k & l))
377 n = startvalue + i * CHAR_BIT + m;
388 (*msg)("ALLOCATED %s %d MARKED FREE\n",
391 (*msg)("%s %sS %d-%d MARKED FREE\n",
392 "ALLOCATED", name, astart, aend);
403 size = uend - ustart + 1;
416 if (debug && size == 1)
417 pwarn("%s %s %d MARKED USED\n",
418 "UNALLOCATED", name, ustart);
420 pwarn("%s %sS %d-%ld MARKED USED\n",
421 "UNALLOCATED", name, ustart,
423 if (bkgrdflag != 0) {
426 if (sysctl(opcode, MIBSIZE, 0, 0,
427 &cmd, sizeof cmd) == -1) {
428 snprintf(buf, BUFSIZE,
430 rwerror(buf, cmd.value);
442 (*msg)("ALLOCATED %s %d MARKED FREE\n", name, astart);
444 (*msg)("ALLOCATED %sS %d-%d MARKED FREE\n",
448 size = uend - ustart + 1;
459 pwarn("UNALLOCATED %s %d MARKED USED\n",
462 pwarn("UNALLOCATED %sS %d-%ld MARKED USED\n",
463 name, ustart, ustart + size - 1);
465 if (bkgrdflag != 0) {
468 if (sysctl(opcode, MIBSIZE, 0, 0, &cmd,
470 snprintf(buf, BUFSIZE, "FREE %s", name);
471 rwerror(buf, cmd.value);