1 /* This module implements functions that allow a controlled access to vital
4 * No function from this module returns if failure occurs.
5 * When something wrong occurs, the function aborts the program, immediately.
15 #include "fsck_helper.h"
18 void printError (int errcode
, char *cmd_name
)
20 fprintf(stderr
, "%s: error #%d: %s\n", cmd_name
, 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
29 * argument sb is the address of a pointer to a SOSuperBlock
31 * The address to the superblock is placed in (*sb).
33 void fetch_superblock(SOSuperBlock
**sb
)
35 assert(&(*sb
) != NULL
);
37 static SOSuperBlock localsb
;
38 static bool localsb_valid
= 0;
41 if (localsb_valid
== false) {
42 fret
= soReadRawBlock(0, &localsb
);
43 if (fret
< 0) FABORT(fret
, "fetch_superblock");
50 /* Inode control table */
53 static bool ictable_valid
= false;
54 static uint32_t ictable_size
= 0;
56 static void ictable_free(void)
58 assert(ictable_valid
== true);
61 ictable_valid
= false;
64 static void ictable_create(void)
66 assert (ictable_valid
== false);
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
) {
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);
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
);
132 printf("\e[m\nBUM!\n");
134 if (((r
+1)%ncols
) == 0) 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);
149 cctable_valid
= false;
152 static void cctable_create(void)
154 assert (cctable_valid
== false);
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
) {
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);
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
);
220 if (((r
+1)%ncols
) == 0) 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);
236 irtable_valid
= false;
239 static void irtable_create(void)
241 assert (irtable_valid
== false);
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
) {
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
);
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
];