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