Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
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, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
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 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static char sccsid[] = "@(#)pass5.c 5.13 (Berkeley) 7/20/90"; | |
4c30d378 | 36 | static char rcsid[] = "$Header: /b/source/CVS/src/sbin/fsck/pass5.c,v 1.3 1993/03/23 00:28:09 cgd Exp $"; |
15637ed4 RG |
37 | #endif /* not lint */ |
38 | ||
39 | #include <sys/param.h> | |
40 | #include <ufs/dinode.h> | |
41 | #include <ufs/fs.h> | |
42 | #include <string.h> | |
43 | #include "fsck.h" | |
44 | ||
45 | pass5() | |
46 | { | |
47 | int c, blk, frags, basesize, sumsize, mapsize, savednrpos; | |
48 | register struct fs *fs = &sblock; | |
49 | register struct cg *cg = &cgrp; | |
50 | daddr_t dbase, dmax; | |
51 | register daddr_t d; | |
52 | register long i, j; | |
53 | struct csum *cs; | |
54 | time_t now; | |
55 | struct csum cstotal; | |
56 | struct inodesc idesc[3]; | |
57 | char buf[MAXBSIZE]; | |
58 | register struct cg *newcg = (struct cg *)buf; | |
59 | struct ocg *ocg = (struct ocg *)buf; | |
60 | ||
61 | bzero((char *)newcg, (size_t)fs->fs_cgsize); | |
62 | newcg->cg_niblk = fs->fs_ipg; | |
63 | switch ((int)fs->fs_postblformat) { | |
64 | ||
65 | case FS_42POSTBLFMT: | |
66 | basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link); | |
67 | sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]); | |
68 | mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] - | |
69 | (u_char *)&ocg->cg_iused[0]; | |
70 | ocg->cg_magic = CG_MAGIC; | |
71 | savednrpos = fs->fs_nrpos; | |
72 | fs->fs_nrpos = 8; | |
73 | break; | |
74 | ||
75 | case FS_DYNAMICPOSTBLFMT: | |
76 | newcg->cg_btotoff = | |
77 | &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); | |
78 | newcg->cg_boff = | |
79 | newcg->cg_btotoff + fs->fs_cpg * sizeof(long); | |
80 | newcg->cg_iusedoff = newcg->cg_boff + | |
81 | fs->fs_cpg * fs->fs_nrpos * sizeof(short); | |
82 | newcg->cg_freeoff = | |
83 | newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY); | |
84 | newcg->cg_nextfreeoff = newcg->cg_freeoff + | |
85 | howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), | |
86 | NBBY); | |
87 | newcg->cg_magic = CG_MAGIC; | |
88 | basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); | |
89 | sumsize = newcg->cg_iusedoff - newcg->cg_btotoff; | |
90 | mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff; | |
91 | break; | |
92 | ||
93 | default: | |
94 | errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n", | |
95 | fs->fs_postblformat); | |
96 | } | |
97 | bzero((char *)&idesc[0], sizeof idesc); | |
98 | for (i = 0; i < 3; i++) | |
99 | idesc[i].id_type = ADDR; | |
100 | bzero((char *)&cstotal, sizeof(struct csum)); | |
101 | (void)time(&now); | |
102 | j = blknum(fs, fs->fs_size + fs->fs_frag - 1); | |
103 | for (i = fs->fs_size; i < j; i++) | |
104 | setbmap(i); | |
105 | for (c = 0; c < fs->fs_ncg; c++) { | |
106 | getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize); | |
107 | if (!cg_chkmagic(cg)) | |
108 | pfatal("CG %d: BAD MAGIC NUMBER\n", c); | |
109 | dbase = cgbase(fs, c); | |
110 | dmax = dbase + fs->fs_fpg; | |
111 | if (dmax > fs->fs_size) | |
112 | dmax = fs->fs_size; | |
113 | if (now > cg->cg_time) | |
114 | newcg->cg_time = cg->cg_time; | |
115 | else | |
116 | newcg->cg_time = now; | |
117 | newcg->cg_cgx = c; | |
118 | if (c == fs->fs_ncg - 1) | |
119 | newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg; | |
120 | else | |
121 | newcg->cg_ncyl = fs->fs_cpg; | |
122 | newcg->cg_ndblk = dmax - dbase; | |
123 | newcg->cg_cs.cs_ndir = 0; | |
124 | newcg->cg_cs.cs_nffree = 0; | |
125 | newcg->cg_cs.cs_nbfree = 0; | |
126 | newcg->cg_cs.cs_nifree = fs->fs_ipg; | |
127 | if (cg->cg_rotor < newcg->cg_ndblk) | |
128 | newcg->cg_rotor = cg->cg_rotor; | |
129 | else | |
130 | newcg->cg_rotor = 0; | |
131 | if (cg->cg_frotor < newcg->cg_ndblk) | |
132 | newcg->cg_frotor = cg->cg_frotor; | |
133 | else | |
134 | newcg->cg_frotor = 0; | |
135 | if (cg->cg_irotor < newcg->cg_niblk) | |
136 | newcg->cg_irotor = cg->cg_irotor; | |
137 | else | |
138 | newcg->cg_irotor = 0; | |
139 | bzero((char *)&newcg->cg_frsum[0], sizeof newcg->cg_frsum); | |
140 | bzero((char *)&cg_blktot(newcg)[0], | |
141 | (size_t)(sumsize + mapsize)); | |
142 | if (fs->fs_postblformat == FS_42POSTBLFMT) | |
143 | ocg->cg_magic = CG_MAGIC; | |
144 | j = fs->fs_ipg * c; | |
145 | for (i = 0; i < fs->fs_ipg; j++, i++) { | |
146 | switch (statemap[j]) { | |
147 | ||
148 | case USTATE: | |
149 | break; | |
150 | ||
151 | case DSTATE: | |
152 | case DCLEAR: | |
153 | case DFOUND: | |
154 | newcg->cg_cs.cs_ndir++; | |
155 | /* fall through */ | |
156 | ||
157 | case FSTATE: | |
158 | case FCLEAR: | |
159 | newcg->cg_cs.cs_nifree--; | |
160 | setbit(cg_inosused(newcg), i); | |
161 | break; | |
162 | ||
163 | default: | |
164 | if (j < ROOTINO) | |
165 | break; | |
166 | errexit("BAD STATE %d FOR INODE I=%d", | |
167 | statemap[j], j); | |
168 | } | |
169 | } | |
170 | if (c == 0) | |
171 | for (i = 0; i < ROOTINO; i++) { | |
172 | setbit(cg_inosused(newcg), i); | |
173 | newcg->cg_cs.cs_nifree--; | |
174 | } | |
175 | for (i = 0, d = dbase; | |
176 | d < dmax; | |
177 | d += fs->fs_frag, i += fs->fs_frag) { | |
178 | frags = 0; | |
179 | for (j = 0; j < fs->fs_frag; j++) { | |
180 | if (testbmap(d + j)) | |
181 | continue; | |
182 | setbit(cg_blksfree(newcg), i + j); | |
183 | frags++; | |
184 | } | |
185 | if (frags == fs->fs_frag) { | |
186 | newcg->cg_cs.cs_nbfree++; | |
187 | j = cbtocylno(fs, i); | |
188 | cg_blktot(newcg)[j]++; | |
189 | cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++; | |
190 | } else if (frags > 0) { | |
191 | newcg->cg_cs.cs_nffree += frags; | |
192 | blk = blkmap(fs, cg_blksfree(newcg), i); | |
193 | fragacct(fs, blk, newcg->cg_frsum, 1); | |
194 | } | |
195 | } | |
196 | cstotal.cs_nffree += newcg->cg_cs.cs_nffree; | |
197 | cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree; | |
198 | cstotal.cs_nifree += newcg->cg_cs.cs_nifree; | |
199 | cstotal.cs_ndir += newcg->cg_cs.cs_ndir; | |
200 | cs = &fs->fs_cs(fs, c); | |
201 | if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 && | |
202 | dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { | |
203 | bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs); | |
204 | sbdirty(); | |
205 | } | |
206 | if (cvtflag) { | |
207 | bcopy((char *)newcg, (char *)cg, (size_t)fs->fs_cgsize); | |
208 | cgdirty(); | |
209 | continue; | |
210 | } | |
211 | if (bcmp(cg_inosused(newcg), | |
212 | cg_inosused(cg), mapsize) != 0 && | |
213 | dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) { | |
214 | bcopy(cg_inosused(newcg), cg_inosused(cg), | |
215 | (size_t)mapsize); | |
216 | cgdirty(); | |
217 | } | |
218 | if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 || | |
219 | bcmp((char *)&cg_blktot(newcg)[0], | |
220 | (char *)&cg_blktot(cg)[0], sumsize) != 0) && | |
221 | dofix(&idesc[2], "SUMMARY INFORMATION BAD")) { | |
222 | bcopy((char *)newcg, (char *)cg, (size_t)basesize); | |
223 | bcopy((char *)&cg_blktot(newcg)[0], | |
224 | (char *)&cg_blktot(cg)[0], (size_t)sumsize); | |
225 | cgdirty(); | |
226 | } | |
227 | } | |
228 | if (fs->fs_postblformat == FS_42POSTBLFMT) | |
229 | fs->fs_nrpos = savednrpos; | |
230 | if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0 | |
231 | && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { | |
232 | bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs); | |
233 | fs->fs_ronly = 0; | |
234 | fs->fs_fmod = 0; | |
235 | sbdirty(); | |
236 | } | |
237 | } |