-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd.c
416 lines (358 loc) · 12.9 KB
/
md.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <fcntl.h>
#include <libudev.h>
#include "algsocket.h"
#include "md.h"
#include "saturnd.h"
#define ALGORITHM_LEFT_ASYMMETRIC 0 /* Rotating Parity N with Data Restart */
#define ALGORITHM_RIGHT_ASYMMETRIC 1 /* Rotating Parity 0 with Data Restart */
#define ALGORITHM_LEFT_SYMMETRIC 2 /* Rotating Parity N with Data Continuation */
#define ALGORITHM_RIGHT_SYMMETRIC 3 /* Rotating Parity 0 with Data Continuation */
#define ALGORITHM_PARITY_0 4 /* P or P,Q are initial devices */
#define ALGORITHM_PARITY_N 5 /* P or P,Q are final devices. */
#define ALGORITHM_ROTATING_ZERO_RESTART 8 /* DDF PRL=6 RLQ=1 */
#define ALGORITHM_ROTATING_N_RESTART 9 /* DDF PRL=6 RLQ=2 */
#define ALGORITHM_ROTATING_N_CONTINUE 10 /*DDF PRL=6 RLQ=3 */
#define ALGORITHM_LEFT_ASYMMETRIC_6 16
#define ALGORITHM_RIGHT_ASYMMETRIC_6 17
#define ALGORITHM_LEFT_SYMMETRIC_6 18
#define ALGORITHM_RIGHT_SYMMETRIC_6 19
#define ALGORITHM_PARITY_0_6 20
#define ALGORITHM_PARITY_N_6 ALGORITHM_PARITY_N
/*
struct stripe stripe;
getStripe(lsector, &stripe);
unsigned int cs_expected; // Expected checksum
unsigned int cs_data; // Checksum of data on disk
unsigned int cs_rdata; // Checksum of reconstructed data
*/
struct stripe {
uint64_t sector;
int dd_idx; // Disk containing sector
int pd_idx; // Parity disk P
//int qd_idx; // Parity disk Q (raid6)
};
int initmddev(dev_t devnum, struct mddev *mddev) {
if(mddev == NULL)
return -1;
char path[PATH_MAX];
struct udev_device *ud = udev_device_new_from_devnum(udevctx, 'b', devnum);
if(ud == NULL)
return -1;
const char *syspath = udev_device_get_syspath(ud);
strncpy(path, syspath, PATH_MAX);
strcpy(mddev->syspath, syspath);
strcpy(mddev->devnode, udev_device_get_devnode(ud));
udev_device_unref(ud);
int pathend = strlen(path);
// Get layout
FILE *f = fopen(strncat(path,"/md/layout", PATH_MAX), "r");
fscanf(f, "%d", &mddev->layout);
fclose(f);
path[pathend] = '\0';
// Get level
f = fopen(strncat(path, "/md/level", PATH_MAX), "r");
fscanf(f, "raid%d", &mddev->level);
fclose(f);
path[pathend] = '\0';
// Get number of RAID disks
f = fopen(strncat(path, "/md/raid_disks", PATH_MAX), "r");
fscanf(f, "%d", &mddev->raid_disks);
fclose(f);
path[pathend] = '\0';
// Get chunk size
f = fopen(strncat(path, "/md/chunk_size", PATH_MAX), "r");
fscanf(f, "%d", &mddev->chunk_size);
fclose(f);
path[pathend] = '\0';
mddev->disks = calloc(mddev->raid_disks, sizeof(char *));
mddev->offsets = calloc(mddev->raid_disks, sizeof(uint64_t));
char tmp[PATH_MAX];
strcpy(tmp, path);
for(int i=0; i<mddev->raid_disks; i++) {
int major = -1;
int minor = -1;
snprintf(path, PATH_MAX, "%s/md/rd%d/block/dev", tmp, i);
// Get major/minor number of raid disk
f = fopen(path, "r");
fscanf(f, "%d:%d", &major, &minor);
fclose(f);
path[pathend] = '\0';
snprintf(path, PATH_MAX, "%s/md/rd%d/offset", tmp, i);
f = fopen(path, "r");
fscanf(f, "%lu", &mddev->offsets[i]);
fclose(f);
path[pathend] = '\0';
// Get device node path from udev
ud = udev_device_new_from_devnum(udevctx, 'b', makedev(major, minor));
const char *diskpath = udev_device_get_devnode(ud);
// Store in array
mddev->disks[i] = malloc(strlen(diskpath));
strcpy(mddev->disks[i], diskpath);
udev_device_unref(ud);
}
return 0;
}
static int getStripe(struct mddev *mddev, uint64_t lsector, struct stripe *stripe) {
if(stripe == NULL || mddev == NULL)
return -1;
//int qd_idx = -1;
int pd_idx = -1, dd_idx = -1;
int layout = mddev->layout;
int level = mddev->level;
int raid_disks = mddev->raid_disks;
int max_degraded = level == 6 ? 2 : 1;
int data_disks = raid_disks - max_degraded;
// sector_div(a, b) makes a = a/b and returns a % b
int sectors_per_chunk = mddev->chunk_size >> 9;
// chunk_offset = sector_div(lsector, sectors_per_chunk)
unsigned int chunk_offset = lsector % sectors_per_chunk;
// chunk_number = lsector
uint64_t chunk_number = lsector/sectors_per_chunk;
// stripe_num = chunk_number
// dd_idx = sector_div(stripe_num, data_disks)
dd_idx = chunk_number % data_disks;
int stripe_num = chunk_number/data_disks;
// Select parity disk based on algorithm
switch(level) {
case 4:
pd_idx = data_disks;
break;
case 5:
switch(layout) {
case ALGORITHM_LEFT_ASYMMETRIC:
pd_idx = data_disks - stripe_num % raid_disks;
if(dd_idx >= pd_idx)
dd_idx++;
break;
case ALGORITHM_RIGHT_ASYMMETRIC:
pd_idx = stripe_num % raid_disks;
if (dd_idx >= pd_idx)
dd_idx++;
break;
case ALGORITHM_LEFT_SYMMETRIC:
pd_idx = data_disks - stripe_num % raid_disks;
dd_idx = (pd_idx + 1 + dd_idx) % raid_disks;
break;
case ALGORITHM_RIGHT_SYMMETRIC:
pd_idx = stripe_num % raid_disks;
dd_idx = (pd_idx + 1 + dd_idx) % raid_disks;
break;
case ALGORITHM_PARITY_0:
pd_idx = 0;
dd_idx++;
break;
case ALGORITHM_PARITY_N:
pd_idx = data_disks;
break;
default:
//BUG();
break;
}
break;
//case 6:
// switch (layout) {
// case ALGORITHM_LEFT_ASYMMETRIC:
// pd_idx = raid_disks - 1 - stripe_num % raid_disks;
// qd_idx = pd_idx + 1;
// if (pd_idx == raid_disks-1) {
// dd_idx++; /* Q D D D P */
// qd_idx = 0;
// } else if (dd_idx >= pd_idx)
// dd_idx += 2; /* D D P Q D */
// break;
// case ALGORITHM_RIGHT_ASYMMETRIC:
// pd_idx = stripe_num % raid_disks;
// qd_idx = pd_idx + 1;
// if (pd_idx == raid_disks-1) {
// dd_idx++; /* Q D D D P */
// qd_idx = 0;
// } else if (dd_idx >= pd_idx)
// dd_idx += 2; /* D D P Q D */
// break;
// case ALGORITHM_LEFT_SYMMETRIC:
// pd_idx = raid_disks - 1 - stripe_num % raid_disks;
// qd_idx = (pd_idx + 1) % raid_disks;
// dd_idx = (pd_idx + 2 + dd_idx) % raid_disks;
// break;
// case ALGORITHM_RIGHT_SYMMETRIC:
// pd_idx = stripe_num % raid_disks;
// qd_idx = (pd_idx + 1) % raid_disks;
// dd_idx = (pd_idx + 2 + dd_idx) % raid_disks;
// break;
// case ALGORITHM_PARITY_0:
// pd_idx = 0;
// qd_idx = 1;
// dd_idx += 2;
// break;
// case ALGORITHM_PARITY_N:
// pd_idx = data_disks;
// qd_idx = data_disks + 1;
// break;
// case ALGORITHM_ROTATING_ZERO_RESTART:
// /* Exactly the same as RIGHT_ASYMMETRIC, but or
// * of blocks for computing Q is different.
// */
// pd_idx = stripe_num % raid_disks;
// qd_idx = pd_idx + 1;
// if (pd_idx == raid_disks-1) {
// dd_idx++; /* Q D D D P */
// qd_idx = 0;
// } else if (dd_idx >= pd_idx)
// dd_idx += 2; /* D D P Q D */
// ddf_layout = 1;
// break;
// case ALGORITHM_ROTATING_N_RESTART:
// /* Same a left_asymmetric, by first stripe is
// * D D D P Q rather than
// * Q D D D P
// */
// stripe_num += 1;
// pd_idx = raid_disks - 1 - stripe_num % raid_disks;
// qd_idx = pd_idx + 1;
// if (pd_idx == raid_disks-1) {
// dd_idx++; /* Q D D D P */
// qd_idx = 0;
// } else if (dd_idx >= pd_idx)
// dd_idx += 2; /* D D P Q D */
// ddf_layout = 1;
// break;
// case ALGORITHM_ROTATING_N_CONTINUE:
// /* Same as left_symmetric but Q is before P */
// pd_idx = raid_disks - 1 - stripe_num % raid_disks;
// qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
// dd_idx = (pd_idx + 1 + dd_idx) % raid_disks;
// ddf_layout = 1;
// break;
// case ALGORITHM_LEFT_ASYMMETRIC_6:
// /* RAID5 left_asymmetric, with Q on last device */
// pd_idx = data_disks - stripe_num % (raid_disks-1);
// if (dd_idx >= pd_idx)
// dd_idx++;
// qd_idx = raid_disks - 1;
// break;
// case ALGORITHM_RIGHT_ASYMMETRIC_6:
// pd_idx =stripe_num % (raid_disks-1);
// if (dd_idx >= pd_idx)
// dd_idx++;
// qd_idx = raid_disks - 1;
// break;
// case ALGORITHM_LEFT_SYMMETRIC_6:
// pd_idx = data_disks - stripe_num % (raid_disks-1);
// dd_idx = (pd_idx + 1 + dd_idx) % (raid_disks-1);
// qd_idx = raid_disks - 1;
// break;
// case ALGORITHM_RIGHT_SYMMETRIC_6:
// pd_idx = stripe_num % (raid_disks-1);
// dd_idx = (pd_idx + 1 + dd_idx) % (raid_disks-1);
// qd_idx = raid_disks - 1;
// break;
// case ALGORITHM_PARITY_0_6:
// pd_idx = 0;
// dd_idx++;
// qd_idx = raid_disks - 1;
// break;
// default:
// //BUG();
// }
// break;
}
stripe->dd_idx = dd_idx;
stripe->pd_idx = pd_idx;
//stripe->qd_idx = qd_idx;
stripe->sector = (uint64_t)stripe_num * sectors_per_chunk + chunk_offset;
return 0;
}
static void xorData(int cnt, size_t len, char inputs[cnt][len], size_t pos,
char **out) {
if(cnt < 2)
*out = inputs[0];
for(int i=0; i<len; i++) {
char result = inputs[0][i];
for(int j=1; j<cnt; j++) {
result = result ^ inputs[j][i];
}
(*out)[i+pos] = result;
}
}
static void reconstructData(struct mddev *mddev, uint64_t start, size_t len,
char **out) {
size_t pos = 0;
int diskfds[mddev->raid_disks];
for(int i=0; i<mddev->raid_disks; i++) {
diskfds[i] = open(mddev->disks[i], O_RDONLY);
if(diskfds[i] < 0) {
printf("Error opening %s: %s\n", mddev->disks[i], strerror(errno));
}
}
while(pos < len) {
int wantToRead = len-pos;
struct stripe s;
// Calculate logical sector
uint64_t lsector = (start+pos)/512;
// Calculate offset into sector
uint64_t offset = (start+pos)-(lsector*512);
getStripe(mddev, lsector, &s);
int rc;
char diskdata[mddev->raid_disks-1][512];
for(int skip=0,i=0; i<mddev->raid_disks; i++) {
if(i == s.dd_idx) {
skip = 1;
continue;
}
// Calculate physical position of data
uint64_t diskpos = s.sector*512 + offset + mddev->offsets[i]*512;
int willRead = 512-offset;
if(willRead > wantToRead)
willRead = wantToRead;
rc = pread(diskfds[i], &diskdata[i-skip][0], willRead, diskpos);
if(rc != willRead) {
printf("Error reading disk %d: %s\n", i, strerror(errno));
return;
}
}
xorData(mddev->raid_disks-1, rc, diskdata, pos, out);
pos += rc;
}
for(int i=0; i<mddev->raid_disks; i++) {
close(diskfds[i]);
}
}
int mdrepair(struct mddev *mddev, uint64_t start, size_t len,
uint32_t csumActual, uint32_t csumExpected) {
printf("Actual checksum: %u\n", csumActual);
printf("Expected checksum: %u\n", csumExpected);
if(csumActual == csumExpected)
return 0;
char *reconstructedData = malloc(len);
reconstructData(mddev, start, len, &reconstructedData);
uint32_t csumReconstructed = alg_csumData(algsocket, reconstructedData, len);
printf("Reconstructed data checksum: %u\n", csumReconstructed);
if(csumReconstructed == csumExpected) {
int fd = open(mddev->devnode, O_WRONLY);
if(fd < 0) {
printf("Error opening %s for writing: %s\n", mddev->devnode,
strerror(errno));
return -1;
}
int rc = pwrite(fd, reconstructedData, len, start);
if(rc != len) {
printf("Error writing to %s: %s\n", mddev->devnode,
strerror(errno));
return -1;
}
close(fd);
sync();
} else {
return -1;
}
return 0;
}