Head cache cnt and idx were being checked the wrong way.
[fsck.sofs09.git] / fsck_helper.c
blobd69ff628de999428c23dabed1d337e0977dc09cf
1 /* This module implements functions that allow a controlled access to vital
2 * information of fsck.
4 * No function from this module returns if failure occurs.
5 * When something wrong occurs, the function aborts the program, immediately.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <assert.h>
15 #include "fsck_helper.h"
16 #include "sofs09.h"
18 void printError (int errcode, char *cmd_name)
20 fprintf(stderr, "%s: error #%d: %s\n", cmd_name, errcode,\
21 strerror(errcode));
24 /* Fetch current disc superblock.
25 * If the superblock was not read, it is read from disk.
26 * No changes to the superblock are monitored. It is assumed the superblock is
27 * used as read-only.
29 * argument sb is the address of a pointer to a SOSuperBlock
31 * The address to the superblock is placed in (*sb).
32 */
33 void fetch_superblock(SOSuperBlock **sb)
35 assert(&(*sb) != NULL);
37 static SOSuperBlock localsb;
38 static bool localsb_valid = 0;
39 int fret;
41 if (localsb_valid == false) {
42 fret = soReadRawBlock(0, &localsb);
43 if (fret < 0) FABORT(fret, "fetch_superblock");
44 localsb_valid = true;
47 (*sb) = &localsb;
50 /* Inode control table */
52 static ic_t *ictable;
53 static bool ictable_valid = false;
54 static uint32_t ictable_size = 0;
56 void ictable_free(void)
58 assert(ictable_valid == true);
60 free(ictable);
61 ictable_valid = false;
64 void ictable_create(void)
66 assert (ictable_valid == false);
68 int r;
69 SOSuperBlock *sb;
70 fetch_superblock(&sb);
72 ictable = malloc(sizeof(ic_t)*(sb->itotal));
73 if (ictable == NULL) FABORT(errno, "ictable_init");
75 ictable_size = sb->itotal;
76 for (r = 0; r < ictable_size; ++r) {
77 ictable[r] = bah;
80 ictable_valid = true;
83 void ictable_set(uint32_t index, ic_t value)
85 assert(ictable_valid == true);
86 assert(index < ictable_size);
88 ictable[index] = value;
91 void ictable_get(uint32_t index, ic_t *value)
93 assert(value != NULL);
94 assert(ictable_valid == true);
95 assert(index < ictable_size);
97 (*value) = ictable[index];
100 void ictable_print(void)
102 assert(ictable_valid == true);
104 int ncols;
105 int r;
106 ic_t icstat;
108 ncols = 100;
110 printf("\nInode control table contents (\e[42mfree\e[m \e[44mbusy\e[m "
111 "\e[41munknown\e[m)\n");
112 for (r = 0; r < ictable_size; ++r) {
113 ictable_get(r, &icstat);
114 switch (icstat) {
115 case idle:
116 printf("\e[42m ");
117 break;
118 case busy:
119 printf("\e[44m ");
120 break;
121 case bah:
122 printf("\e[41m ");
123 break;
124 default:
125 printf("\e[m\nBUM!\n");
127 if (((r+1)%ncols) == 0) printf("\e[m\n");
129 printf("\e[m\n");
132 /* Cluster control table */
133 static cc_t *cctable;
134 static bool cctable_valid = false;
135 static uint32_t cctable_size = 0;
137 void cctable_free(void)
139 assert(cctable_valid == true);
141 free(cctable);
142 cctable_valid = false;
145 void cctable_create(void)
147 assert (cctable_valid == false);
149 int r;
150 SOSuperBlock *sb;
151 fetch_superblock(&sb);
153 cctable = malloc(sizeof(cc_t)*(sb->dzone_size));
154 if (cctable == NULL) FABORT(errno, "cctable_init");
156 cctable_size = sb->dzone_size;
157 for (r = 0; r < cctable_size; ++r) {
158 cctable[r] = bah;
161 cctable_valid = true;
164 void cctable_set(uint32_t index, cc_t value)
166 assert(cctable_valid == true);
167 assert(index < cctable_size);
169 cctable[index] = value;
172 void cctable_get(uint32_t index, cc_t *value)
174 assert(value != NULL);
175 assert(cctable_valid == true);
176 assert(index < cctable_size);
178 (*value) = cctable[index];
181 void cctable_print(void)
183 assert(cctable_valid == true);
185 int ncols;
186 int r;
187 cc_t ccstat;
189 ncols = 100;
191 printf("\nCluster control table contents (\e[42mfree\e[m \e[44mbusy\e[m "
192 "\e[41munknown\e[m)\n");
193 for (r = 0; r < cctable_size; ++r) {
194 cctable_get(r, &ccstat);
195 switch (ccstat) {
196 case idle:
197 printf("\e[42m ");
198 break;
199 case busy:
200 printf("\e[44m ");
201 break;
202 case bah:
203 printf("\e[41m ");
204 break;
206 if (((r+1)%ncols) == 0) printf("\e[m\n");
208 printf("\e[m\n");
211 /* inode refcount table */
213 static uint32_t *irtable;
214 static bool irtable_valid = false;
215 static uint32_t irtable_size = 0;
217 void irtable_free(void)
219 assert(irtable_valid == true);
221 free(irtable);
222 irtable_valid = false;
225 void irtable_create(void)
227 assert (irtable_valid == false);
229 int r;
230 SOSuperBlock *sb;
231 fetch_superblock(&sb);
233 irtable = malloc(sizeof(uint32_t)*(sb->itotal));
234 if (irtable == NULL) FABORT(errno, "irtable_init");
236 irtable_size = sb->itotal;
237 for (r = 0; r < irtable_size; ++r) {
238 irtable[r] = 0;
241 irtable_valid = true;
244 void irtable_inc(uint32_t index)
246 assert(irtable_valid == true);
247 assert(index < irtable_size);
249 irtable[index]++;
252 void irtable_get(uint32_t index, uint32_t *value)
254 assert(value != NULL);
255 assert(irtable_valid == true);
256 assert(index < irtable_size);
258 (*value) = irtable[index];