Commit | Line | Data |
---|---|---|
76797561 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
62ae4dc6 | 7 | #ifndef lint |
484ac932 | 8 | static char sccsid[] = "@(#)pass5.c 5.5 (Berkeley) %G%"; |
76797561 | 9 | #endif not lint |
62ae4dc6 KM |
10 | |
11 | #include <sys/param.h> | |
12 | #include <sys/inode.h> | |
13 | #include <sys/fs.h> | |
14 | #include "fsck.h" | |
15 | ||
16 | pass5() | |
17 | { | |
af29c275 | 18 | int c, blk, frags, basesize, sumsize, mapsize, savednrpos; |
484ac932 KM |
19 | register struct fs *fs = &sblock; |
20 | register struct cg *cg = &cgrp; | |
af29c275 KM |
21 | daddr_t dbase, dmax; |
22 | register daddr_t d; | |
993a756c KM |
23 | register long i, j; |
24 | struct csum *cs; | |
25 | time_t now; | |
26 | struct csum cstotal; | |
27 | struct inodesc idesc; | |
28 | char buf[MAXBSIZE]; | |
29 | register struct cg *newcg = (struct cg *)buf; | |
af29c275 | 30 | struct ocg *ocg = (struct ocg *)buf; |
62ae4dc6 | 31 | |
484ac932 KM |
32 | bzero((char *)newcg, fs->fs_cgsize); |
33 | newcg->cg_niblk = fs->fs_ipg; | |
34 | switch (fs->fs_postblformat) { | |
af29c275 KM |
35 | |
36 | case FS_42POSTBLFMT: | |
37 | basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link); | |
38 | sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]); | |
484ac932 | 39 | mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] - |
af29c275 KM |
40 | (u_char *)&ocg->cg_iused[0]; |
41 | ocg->cg_magic = CG_MAGIC; | |
484ac932 KM |
42 | savednrpos = fs->fs_nrpos; |
43 | fs->fs_nrpos = 8; | |
af29c275 KM |
44 | break; |
45 | ||
46 | case FS_DYNAMICPOSTBLFMT: | |
47 | newcg->cg_btotoff = | |
48 | &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); | |
49 | newcg->cg_boff = | |
484ac932 | 50 | newcg->cg_btotoff + fs->fs_cpg * sizeof(long); |
af29c275 | 51 | newcg->cg_iusedoff = newcg->cg_boff + |
484ac932 | 52 | fs->fs_cpg * fs->fs_nrpos * sizeof(short); |
af29c275 | 53 | newcg->cg_freeoff = |
484ac932 | 54 | newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY); |
af29c275 | 55 | newcg->cg_nextfreeoff = newcg->cg_freeoff + |
484ac932 | 56 | howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), |
af29c275 KM |
57 | NBBY); |
58 | newcg->cg_magic = CG_MAGIC; | |
59 | basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); | |
60 | sumsize = newcg->cg_iusedoff - newcg->cg_btotoff; | |
61 | mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff; | |
62 | break; | |
63 | ||
64 | default: | |
65 | errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n", | |
484ac932 | 66 | fs->fs_postblformat); |
af29c275 | 67 | } |
993a756c KM |
68 | bzero((char *)&idesc, sizeof(struct inodesc)); |
69 | idesc.id_type = ADDR; | |
70 | bzero((char *)&cstotal, sizeof(struct csum)); | |
993a756c | 71 | (void)time(&now); |
484ac932 | 72 | for (i = fs->fs_size; i < fragroundup(fs, fs->fs_size); i++) |
af29c275 | 73 | setbmap(i); |
484ac932 KM |
74 | for (c = 0; c < fs->fs_ncg; c++) { |
75 | getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize); | |
76 | if (!cg_chkmagic(cg)) | |
62ae4dc6 | 77 | pfatal("CG %d: BAD MAGIC NUMBER\n", c); |
484ac932 KM |
78 | dbase = cgbase(fs, c); |
79 | dmax = dbase + fs->fs_fpg; | |
80 | if (dmax > fs->fs_size) | |
81 | dmax = fs->fs_size; | |
82 | if (now > cg->cg_time) | |
83 | newcg->cg_time = cg->cg_time; | |
993a756c KM |
84 | else |
85 | newcg->cg_time = now; | |
86 | newcg->cg_cgx = c; | |
484ac932 KM |
87 | if (c == fs->fs_ncg - 1) |
88 | newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg; | |
993a756c | 89 | else |
484ac932 | 90 | newcg->cg_ncyl = fs->fs_cpg; |
993a756c KM |
91 | newcg->cg_ndblk = dmax - dbase; |
92 | newcg->cg_cs.cs_ndir = 0; | |
93 | newcg->cg_cs.cs_nffree = 0; | |
94 | newcg->cg_cs.cs_nbfree = 0; | |
484ac932 KM |
95 | newcg->cg_cs.cs_nifree = fs->fs_ipg; |
96 | if (cg->cg_rotor < newcg->cg_ndblk) | |
97 | newcg->cg_rotor = cg->cg_rotor; | |
993a756c KM |
98 | else |
99 | newcg->cg_rotor = 0; | |
484ac932 KM |
100 | if (cg->cg_frotor < newcg->cg_ndblk) |
101 | newcg->cg_frotor = cg->cg_frotor; | |
993a756c KM |
102 | else |
103 | newcg->cg_frotor = 0; | |
484ac932 KM |
104 | if (cg->cg_irotor < newcg->cg_niblk) |
105 | newcg->cg_irotor = cg->cg_irotor; | |
993a756c KM |
106 | else |
107 | newcg->cg_irotor = 0; | |
af29c275 KM |
108 | bzero((char *)&newcg->cg_frsum[0], sizeof newcg->cg_frsum); |
109 | bzero((char *)&cg_blktot(newcg)[0], sumsize + mapsize); | |
484ac932 | 110 | if (fs->fs_postblformat == FS_42POSTBLFMT) |
af29c275 | 111 | ocg->cg_magic = CG_MAGIC; |
484ac932 KM |
112 | j = fs->fs_ipg * c; |
113 | for (i = 0; i < fs->fs_ipg; j++, i++) { | |
993a756c KM |
114 | switch (statemap[j]) { |
115 | ||
116 | case USTATE: | |
117 | break; | |
118 | ||
119 | case DSTATE: | |
120 | case DCLEAR: | |
121 | case DFOUND: | |
122 | newcg->cg_cs.cs_ndir++; | |
123 | /* fall through */ | |
124 | ||
125 | case FSTATE: | |
126 | case FCLEAR: | |
127 | newcg->cg_cs.cs_nifree--; | |
af29c275 | 128 | setbit(cg_inosused(newcg), i); |
993a756c | 129 | break; |
ea4448dc KM |
130 | |
131 | default: | |
132 | if (j < ROOTINO) | |
133 | break; | |
134 | errexit("BAD STATE %d FOR INODE I=%d", | |
135 | statemap[j], j); | |
993a756c | 136 | } |
62ae4dc6 | 137 | } |
993a756c KM |
138 | if (c == 0) |
139 | for (i = 0; i < ROOTINO; i++) { | |
af29c275 | 140 | setbit(cg_inosused(newcg), i); |
993a756c KM |
141 | newcg->cg_cs.cs_nifree--; |
142 | } | |
143 | for (i = 0, d = dbase; | |
af29c275 | 144 | d < dmax; |
484ac932 | 145 | d += fs->fs_frag, i += fs->fs_frag) { |
993a756c | 146 | frags = 0; |
484ac932 | 147 | for (j = 0; j < fs->fs_frag; j++) { |
993a756c KM |
148 | if (getbmap(d + j)) |
149 | continue; | |
af29c275 | 150 | setbit(cg_blksfree(newcg), i + j); |
993a756c KM |
151 | frags++; |
152 | } | |
484ac932 | 153 | if (frags == fs->fs_frag) { |
993a756c | 154 | newcg->cg_cs.cs_nbfree++; |
484ac932 | 155 | j = cbtocylno(fs, i); |
af29c275 | 156 | cg_blktot(newcg)[j]++; |
484ac932 | 157 | cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++; |
993a756c KM |
158 | } else if (frags > 0) { |
159 | newcg->cg_cs.cs_nffree += frags; | |
484ac932 KM |
160 | blk = blkmap(fs, cg_blksfree(newcg), i); |
161 | fragacct(fs, blk, newcg->cg_frsum, 1); | |
62ae4dc6 | 162 | } |
62ae4dc6 | 163 | } |
993a756c KM |
164 | cstotal.cs_nffree += newcg->cg_cs.cs_nffree; |
165 | cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree; | |
166 | cstotal.cs_nifree += newcg->cg_cs.cs_nifree; | |
167 | cstotal.cs_ndir += newcg->cg_cs.cs_ndir; | |
484ac932 | 168 | cs = &fs->fs_cs(fs, c); |
993a756c KM |
169 | if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 && |
170 | dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { | |
171 | bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs); | |
172 | sbdirty(); | |
62ae4dc6 | 173 | } |
af29c275 | 174 | if (cvtflag) { |
484ac932 | 175 | bcopy((char *)newcg, (char *)cg, fs->fs_cgsize); |
af29c275 KM |
176 | cgdirty(); |
177 | continue; | |
178 | } | |
179 | if (bcmp(cg_inosused(newcg), | |
484ac932 | 180 | cg_inosused(cg), mapsize) != 0 && |
af29c275 | 181 | dofix(&idesc, "BLK(S) MISSING IN BIT MAPS")) { |
484ac932 | 182 | bcopy(cg_inosused(newcg), cg_inosused(cg), mapsize); |
af29c275 KM |
183 | cgdirty(); |
184 | } | |
484ac932 | 185 | if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 || |
af29c275 | 186 | bcmp((char *)&cg_blktot(newcg)[0], |
484ac932 | 187 | (char *)&cg_blktot(cg)[0], sumsize) != 0) && |
af29c275 | 188 | dofix(&idesc, "SUMMARY INFORMATION BAD")) { |
484ac932 | 189 | bcopy((char *)newcg, (char *)cg, basesize); |
af29c275 | 190 | bcopy((char *)&cg_blktot(newcg)[0], |
484ac932 | 191 | (char *)&cg_blktot(cg)[0], sumsize); |
af29c275 KM |
192 | cgdirty(); |
193 | } | |
62ae4dc6 | 194 | } |
484ac932 KM |
195 | if (fs->fs_postblformat == FS_42POSTBLFMT) |
196 | fs->fs_nrpos = savednrpos; | |
197 | if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0 | |
993a756c | 198 | && dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { |
484ac932 KM |
199 | bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs); |
200 | fs->fs_ronly = 0; | |
201 | fs->fs_fmod = 0; | |
993a756c KM |
202 | sbdirty(); |
203 | } | |
62ae4dc6 | 204 | } |