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