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 static void printError (int errcode
, char *cmd_name
)
20 fprintf(stderr
, "%s: error #%d: %s\n", cmd_name
, errcode
,\
23 #define FABORT(err, fname) \
25 printError(-(err), fname);\
29 /* Fetch current disc superblock.
30 * If the superblock was not read, it is read from disk.
31 * No changes to the superblock are monitored. It is assumed the superblock is
34 * argument sb is the address of a pointer to a SOSuperBlock
36 * The address to the superblock is placed in (*sb).
38 void fetch_superblock(SOSuperBlock
**sb
)
40 assert((*sb
) != NULL
);
42 static SOSuperBlock localsb
;
43 static bool localsb_valid
= 0;
46 if (localsb_valid
== false) {
47 fret
= soReadRawBlock(0, &localsb
);
48 if (fret
< 0) FABORT(fret
, "fetch_superblock");
55 /* Inode control table */
58 static bool ictable_valid
= false;
59 static uint32_t ictable_size
= 0;
61 static void ictable_free(void)
63 assert(ictable_valid
== true);
66 ictable_valid
= false;
69 static void ictable_create(void)
71 assert (ictable_valid
== false);
75 fetch_superblock(&sb
);
77 ictable
= malloc(sizeof(ic_t
)*(sb
->itotal
));
78 if (ictable
== NULL
) FABORT(errno
, "ictable_init");
80 ictable_size
= sb
->itotal
;
81 for (r
= 0; r
< ictable_size
; ++r
) {
90 void ictable_set(uint32_t index
, ic_t value
)
92 if (ictable_valid
== false) ictable_create();
94 assert(ictable_valid
== true);
95 assert(index
< ictable_size
);
97 ictable
[index
] = value
;
100 void ictable_get(uint32_t index
, ic_t
*value
)
102 assert(value
!= NULL
);
104 if (ictable_valid
== false) ictable_create();
106 assert(ictable_valid
== true);
107 assert(index
< ictable_size
);
109 (*value
) = ictable
[index
];
112 /* Cluster control table */
113 static cc_t
*cctable
;
114 static bool cctable_valid
= false;
115 static uint32_t cctable_size
= 0;
117 static void cctable_free(void)
119 assert(cctable_valid
== true);
122 cctable_valid
= false;
125 static void cctable_create(void)
127 assert (cctable_valid
== false);
131 fetch_superblock(&sb
);
133 cctable
= malloc(sizeof(cc_t
)*(sb
->dzone_size
));
134 if (cctable
== NULL
) FABORT(errno
, "cctable_init");
136 ictable_size
= sb
->dzone_size
;
137 for (r
= 0; r
< cctable_size
; ++r
) {
141 cctable_valid
= true;
143 atexit(cctable_free
);
146 void cctable_set(uint32_t index
, cc_t value
)
148 if (cctable_valid
== false) cctable_create();
150 assert(cctable_valid
== true);
151 assert(index
< cctable_size
);
153 cctable
[index
] = value
;
156 void cctable_get(uint32_t index
, cc_t
*value
)
158 assert(value
!= NULL
);
160 if (cctable_valid
== false) cctable_create();
162 assert(cctable_valid
== true);
163 assert(index
< cctable_size
);
165 (*value
) = cctable
[index
];