Some messages altered.
[fsck.sofs09.git] / fsck_helper.c
blob1a3d63a227bb8e9c1351909da0cf01c3f2acf2ec
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 static void ictable_free(void)
58 assert(ictable_valid == true);
60 free(ictable);
61 ictable_valid = false;
64 static 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;
82 atexit(ictable_free);
85 void ictable_set(uint32_t index, ic_t value)
87 if (ictable_valid == false) ictable_create();
89 assert(ictable_valid == true);
90 assert(index < ictable_size);
92 ictable[index] = value;
95 void ictable_get(uint32_t index, ic_t *value)
97 assert(value != NULL);
99 if (ictable_valid == false) ictable_create();
101 assert(ictable_valid == true);
102 assert(index < ictable_size);
104 (*value) = ictable[index];
107 void ictable_print(void)
109 assert(ictable_valid == true);
111 int ncols;
112 int r;
113 ic_t icstat;
115 ncols = 100;
117 printf("\nInode control table contents (\e[42mfree\e[m \e[44mbusy\e[m "
118 "\e[41munknown\e[m)\n");
119 for (r = 0; r < ictable_size; ++r) {
120 ictable_get(r, &icstat);
121 switch (icstat) {
122 case idle:
123 printf("\e[42m ");
124 break;
125 case busy:
126 printf("\e[44m ");
127 break;
128 case bah:
129 printf("\e[41m ");
130 break;
131 default:
132 printf("\e[m\nBUM!\n");
134 if (((r+1)%ncols) == 0) printf("\e[m\n");
136 printf("\e[m\n");
139 /* Cluster control table */
140 static cc_t *cctable;
141 static bool cctable_valid = false;
142 static uint32_t cctable_size = 0;
144 static void cctable_free(void)
146 assert(cctable_valid == true);
148 free(cctable);
149 cctable_valid = false;
152 static void cctable_create(void)
154 assert (cctable_valid == false);
156 int r;
157 SOSuperBlock *sb;
158 fetch_superblock(&sb);
160 cctable = malloc(sizeof(cc_t)*(sb->dzone_size));
161 if (cctable == NULL) FABORT(errno, "cctable_init");
163 cctable_size = sb->dzone_size;
164 for (r = 0; r < cctable_size; ++r) {
165 cctable[r] = bah;
168 cctable_valid = true;
170 atexit(cctable_free);
173 void cctable_set(uint32_t index, cc_t value)
175 if (cctable_valid == false) cctable_create();
177 assert(cctable_valid == true);
178 assert(index < cctable_size);
180 cctable[index] = value;
183 void cctable_get(uint32_t index, cc_t *value)
185 assert(value != NULL);
187 if (cctable_valid == false) cctable_create();
189 assert(cctable_valid == true);
190 assert(index < cctable_size);
192 (*value) = cctable[index];
195 void cctable_print(void)
197 assert(cctable_valid == true);
199 int ncols;
200 int r;
201 cc_t ccstat;
203 ncols = 100;
205 printf("\nCluster control table contents (\e[42mfree\e[m \e[44mbusy\e[m "
206 "\e[41munknown\e[m)\n");
207 for (r = 0; r < cctable_size; ++r) {
208 cctable_get(r, &ccstat);
209 switch (ccstat) {
210 case idle:
211 printf("\e[42m ");
212 break;
213 case busy:
214 printf("\e[44m ");
215 break;
216 case bah:
217 printf("\e[41m ");
218 break;
220 if (((r+1)%ncols) == 0) printf("\e[m\n");
222 printf("\e[m\n");
225 /* inode refcount table */
227 static uint32_t *irtable;
228 static bool irtable_valid = false;
229 static uint32_t irtable_size = 0;
231 static void irtable_free(void)
233 assert(irtable_valid == true);
235 free(irtable);
236 irtable_valid = false;
239 static void irtable_create(void)
241 assert (irtable_valid == false);
243 int r;
244 SOSuperBlock *sb;
245 fetch_superblock(&sb);
247 irtable = malloc(sizeof(uint32_t)*(sb->itotal));
248 if (irtable == NULL) FABORT(errno, "irtable_init");
250 irtable_size = sb->itotal;
251 for (r = 0; r < irtable_size; ++r) {
252 irtable[r] = 0;
255 irtable_valid = true;
257 atexit(irtable_free);
260 void irtable_inc(uint32_t index)
262 if (irtable_valid == false) irtable_create();
264 assert(irtable_valid == true);
265 assert(index < irtable_size);
267 irtable[index]++;
270 void irtable_get(uint32_t index, uint32_t *value)
272 assert(value != NULL);
274 if (irtable_valid == false) irtable_create();
276 assert(irtable_valid == true);
277 assert(index < irtable_size);
279 (*value) = irtable[index];