-
Notifications
You must be signed in to change notification settings - Fork 0
/
libraid_user.c
194 lines (159 loc) · 3.69 KB
/
libraid_user.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DEVICES 100
#define MAXSTR 200
struct system
{
int num_raids;
struct raid *raids;
};
struct raid
{
int raid_id;
char raid_name[MAXSTR];
int stripe_size;
int bps; // blocks per sector
int num_devices;
struct device *devices[MAX_DEVICES];
int raidsize;
struct raid * next;
};
struct device
{
char *name;
int size;
};
int create_new_raid(struct system * s, char *raid_name, int num_devices, ...)
{
va_list list;
int i = 0;
struct raid *r;
struct device *disk;
// initialize one raid node
r = malloc(sizeof(struct raid));
if (r != NULL)
{
memset(r, 0, sizeof(struct raid));
}
r->next = NULL;
// set raid id
r->raid_id=s->num_raids;
// set raid name
strcpy(r->raid_name,raid_name);
// set number of devices
r->num_devices = num_devices;
// iterate over each device and set name
va_start(list, num_devices);
for(i=0; i<num_devices; i++)
{
// initialize disk
disk = malloc(sizeof(struct device));
memset(disk, 0, sizeof(struct device));
// set each name using va_arg(list, char *)
disk->name = va_arg(list, char *);
r->devices[i] = disk;
}
va_end(list);
// TODO : add raid node to linked list
if (s->raids == NULL){
// just add
s->raids = r;
} else {
// iterate to end and then add
while(s->raids->next != NULL)
s->raids = s->raids->next;
s->raids->next = r;
}
// s->raids[s->num_raids] = r;
// increment system.num_raids
s->num_raids += 1;
return EXIT_SUCCESS;
}
int delete_raid(struct system *s, int id)
{
struct raid *r = s->raids;
int i = 0;
// free all disks
while (s->raids->raid_id == id || s->raids != NULL)
{
r = s->raids;
s->raids = s->raids->next;
}
if (s->raids == NULL){
printf("Raid with ID %d does not exists.", id);
return 0;
}
// free all disks
for (i=0; i<s->raids->num_devices; i++)
{
free(s->raids->devices[i]);
}
// free raid
r->next = s->raids->next;
free(s->raids);
s->num_raids--;
return 0;
}
int initialize_system(struct system **sp)
{
// Do not let more than one system exists together
if (*sp != NULL)
{
return 0;
}
*sp = malloc(sizeof(struct system));
if(*sp != NULL)
{
memset(*sp, 0, sizeof(struct system));
}
return 0;
}
void print_raid_config(struct system *s)
{
int j=0;
printf("\n");
printf("Total Raids: %d\n", s->num_raids);
if (s->raids == NULL)
{
return;
}
while(s->raids != NULL)
{
printf("Raid ID: %d\n", s->raids->raid_id);
printf("Raid Name: %s\n", s->raids->raid_name);
for (j=0; j<s->raids->num_devices; j++)
{
printf("device: %s\n", s->raids->devices[j]->name);
}
s->raids = s->raids->next;
}
}
int main()
{
struct system * s;
char * rname,* lname;
char * d0;
char * d1;
int id = 1;
rname = malloc(sizeof(43));
lname = malloc(sizeof(43));
rname = "ankit_raid";
lname = "rana_raid";
d0 = malloc(sizeof(43));
d0 = "/dev/sdb";
d1 = malloc(sizeof(43));
d1 = "/dev/sdc";
// initialize system structure
initialize_system(&s);
// set raid config
create_new_raid(s, rname, 2, d0, d1);
create_new_raid(s, lname, 2, d0, d1);
//create_new_raid(s, rname, 0);
// view raid config
print_raid_config(s);
delete_raid(s,id);
//print_raid_config(s);
return 0;
}