Return style changed.
[fsck.sofs09.git] / test_clusterfree.c
blob54972b82431418fcd9ca347e73bcaf0a32ec7ee2
1 #include <stdio.h>
3 #include "sofs09.h"
4 #include "fsck.h"
5 #include "fsck_helper.h"
7 static bool check_cluster(uint32_t cba)
9 uint32_t ci;
10 cc_t ccstat;
11 SOSuperBlock *sb;
13 fetch_superblock(&sb);
15 /* check if block address is inside data zone and if it is a valid
16 * cluster address (alignment) */
17 if (cba < (sb->dzone_start) ||
18 cba >= (sb->dzone_start) + (sb->dzone_size)*BLOCKS_PER_CLUSTER ||
19 ((cba-(sb->dzone_start))%BLOCKS_PER_CLUSTER) != 0)
21 printf("Member of free cluster list is not a valid cluster address, %u"
22 ".\n", cba);
23 return false;
26 /* check if cluster was already marked */
27 ci = (cba-(sb->dzone_start))/BLOCKS_PER_CLUSTER;
28 cctable_get(ci, &ccstat);
29 if (ccstat != bah) {
30 printf("Cluster number %u is used multiple times.\n", ci);
31 return false;
34 /* mark cluster */
35 cctable_set(ci, idle);
37 return true;
40 testresult_t test_clusterfree (void)
42 testresult_t ret;
43 uint32_t fret;
44 uint32_t freeclusters;
45 uint32_t pos;
46 uint32_t fcrefs[RPB];
47 uint32_t cba; /* cluster block address */
48 SOSuperBlock *sb;
49 uint32_t cacheidx, cachecnt;
50 uint32_t r;
52 fetch_superblock(&sb);
54 ret = nice;
55 freeclusters = 0;
57 /* check fctable limits */
58 if ((sb->fctable_head) >= (sb->fctable_size)*RPB) {
59 printf("fctable_head is out of bounds.\n");
60 return corrupt;
62 if ((sb->fctable_tail) >= (sb->fctable_size)*RPB) {
63 printf("fctable_tail is out of bounds.\n");
64 return corrupt;
66 pos = sb->fctable_head;
67 while (pos != sb->fctable_tail) {
68 fret = soReadRawBlock((sb->fctable_start)+(pos/RPB), fcrefs);
69 if (fret < 0) FABORT(fret, "sbfreeclusters");
71 cba = fcrefs[pos%RPB];
72 if (check_cluster(cba) == true) {
73 freeclusters++;
74 } else {
75 printf("It was entry number %u of fctable.\n", pos);
76 ret = corrupt;
78 pos = (pos+1)%((sb->fctable_size)*RPB);
81 /* check head cache */
82 cacheidx = sb->dzone_head.cache_idx;
83 cachecnt = sb->dzone_head.cache_cnt;
84 if (cacheidx + cachecnt > DZONE_CACHE_SIZE) {
85 printf("Head cache has incorrect information.\n");
86 return corrupt;
88 for (r = cacheidx; r < cacheidx + cachecnt, ++r) {
89 cba = (sb->dzone_head.cache)[r];
90 if (check_cluster(cba) == true) {
91 freeclusters++;
92 } else {
93 printf("It was entry number %u of dzone_head cache.\n", r);
94 ret = corrupt;
98 /* check tail cache */
99 cacheidx = 0;
100 cachecnt = sb->dzone_tail.cache_cnt;
101 if (cacheidx + cachecnt >= DZONE_CACHE_SIZE) {
102 printf("Tail cache has incorrect information.\n");
103 return corrupt;
105 for (r = cacheidx; r < cacheidx + cachecnt; ++r) {
106 cba = (sb->dzone_tail.cache)[r];
107 if (check_cluster(cba) == true) {
108 freeclusters++;
109 } else {
110 printf("It was entry number %u of dzone_tail cache.\n", r);
111 ret = corrupt;
115 if (freeclusters != sb->dzone_free) {
116 printf("donze_free is %u, should be %u.\n", sb->dzone_free,
117 freeclusters);
118 ret = corrupt;
121 return ret;