]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
Make '-x nnn' spares work for type-1 superblocks.
[thirdparty/mdadm.git] / super1.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2004 Neil Brown <neilb@cse.unsw.edu.au>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdadm.h"
31
32 #include "asm/byteorder.h"
33 /*
34 * The version-1 superblock :
35 * All numeric fields are little-endian.
36 *
37 * total size: 256 bytes plus 2 per device.
38 * 1K allows 384 devices.
39 */
40 struct mdp_superblock_1 {
41 /* constant array information - 128 bytes */
42 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
43 __u32 major_version; /* 1 */
44 __u32 feature_map; /* 0 for now */
45 __u32 pad0; /* always set to 0 when writing */
46
47 __u8 set_uuid[16]; /* user-space generated. */
48 char set_name[32]; /* set and interpreted by user-space */
49
50 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
51 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
52 __u32 layout; /* only for raid5 currently */
53 __u64 size; /* used size of component devices, in 512byte sectors */
54
55 __u32 chunksize; /* in 512byte sectors */
56 __u32 raid_disks;
57 __u8 pad1[128-96]; /* set to 0 when written */
58
59 /* constant this-device information - 64 bytes */
60 __u64 data_offset; /* sector start of data, often 0 */
61 __u64 data_size; /* sectors in this device that can be used for data */
62 __u64 super_offset; /* sector start of this superblock */
63 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
64 __u32 dev_number; /* permanent identifier of this device - not role in raid */
65 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
66 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
67 __u8 pad2[64-56]; /* set to 0 when writing */
68
69 /* array state information - 64 bytes */
70 __u64 utime; /* 40 bits second, 24 btes microseconds */
71 __u64 events; /* incremented when superblock updated */
72 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
73 __u32 sb_csum; /* checksum upto devs[max_dev] */
74 __u32 max_dev; /* size of devs[] array to consider */
75 __u8 pad3[64-32]; /* set to 0 when writing */
76
77 /* device state information. Indexed by dev_number.
78 * 2 bytes per device
79 * Note there are no per-device state flags. State information is rolled
80 * into the 'roles' value. If a device is spare or faulty, then it doesn't
81 * have a meaningful role.
82 */
83 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
84 };
85
86 #ifndef offsetof
87 #define offsetof(t,f) ((int)&(((t*)0)->f))
88 #endif
89 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
90 {
91 unsigned int disk_csum, csum;
92 unsigned long long newcsum;
93 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
94 unsigned int *isuper = (unsigned int*)sb;
95 int i;
96
97 /* make sure I can count... */
98 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
99 offsetof(struct mdp_superblock_1, utime) != 192 ||
100 sizeof(struct mdp_superblock_1) != 256) {
101 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
102 }
103
104 disk_csum = sb->sb_csum;
105 sb->sb_csum = 0;
106 newcsum = 0;
107 for (i=0; size>=4; size -= 4 )
108 newcsum += __le32_to_cpu(*isuper++);
109
110 if (size == 2)
111 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
112
113 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
114 sb->sb_csum = disk_csum;
115 return csum;
116 }
117
118
119 static void examine_super1(void *sbv)
120 {
121 struct mdp_superblock_1 *sb = sbv;
122 time_t atime;
123 int d;
124 int spares, faulty;
125 int i;
126 char *c;
127
128 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
129 printf(" Version : %02d.%02d\n", 1, __le32_to_cpu(sb->feature_map));
130 printf(" Array UUID : ");
131 for (i=0; i<16; i++) {
132 printf("%02x", sb->set_uuid[i]);
133 if ((i&3)==0 && i != 0) printf(":");
134 }
135 printf("\n");
136 printf(" Name : %.32s\n", sb->set_name);
137
138 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
139 printf(" Creation Time : %.24s\n", ctime(&atime));
140 c=map_num(pers, __le32_to_cpu(sb->level));
141 printf(" Raid Level : %s\n", c?c:"-unknown-");
142 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
143 printf("\n");
144 printf(" Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
145 if (sb->data_offset)
146 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->data_offset));
147 if (sb->super_offset)
148 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->super_offset));
149 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
150 printf(" Device UUID : ");
151 for (i=0; i<16; i++) {
152 printf("%02x", sb->set_uuid[i]);
153 if ((i&3)==0 && i != 0) printf(":");
154 }
155 printf("\n");
156
157 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
158 printf(" Update Time : %.24s\n", ctime(&atime));
159
160 if (calc_sb_1_csum(sb) == sb->sb_csum)
161 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
162 else
163 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
164 __le32_to_cpu(calc_sb_1_csum(sb)));
165 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
166 printf("\n");
167 if (__le32_to_cpu(sb->level) == 5) {
168 c = map_num(r5layout, __le32_to_cpu(sb->layout));
169 printf(" Layout : %s\n", c?c:"-unknown-");
170 }
171 switch(__le32_to_cpu(sb->level)) {
172 case 0:
173 case 4:
174 case 5:
175 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize/2));
176 break;
177 case -1:
178 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize/2));
179 break;
180 default: break;
181 }
182 printf("\n");
183 printf(" Array State : ");
184 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
185 int cnt = 0;
186 int me = 0;
187 int i;
188 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
189 int role = __le16_to_cpu(sb->dev_roles[i]);
190 if (role == d) {
191 if (i == __le32_to_cpu(sb->dev_number))
192 me = 1;
193 cnt++;
194 }
195 }
196 if (cnt > 1) printf("?");
197 else if (cnt == 1 && me) printf("U");
198 else if (cnt == 1) printf("u");
199 else printf ("_");
200 }
201 spares = faulty = 0;
202 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
203 int role = __le16_to_cpu(sb->dev_roles[i]);
204 switch (role) {
205 case 0xFFFF: spares++; break;
206 case 0xFFFE: faulty++;
207 }
208 }
209 if (spares) printf(" %d spares", spares);
210 if (faulty) printf(" %d failed", faulty);
211 printf("\n");
212 }
213
214
215 static void brief_examine_super1(void *sbv)
216 {
217 struct mdp_superblock_1 *sb = sbv;
218 int i;
219
220 char *c=map_num(pers, __le32_to_cpu(sb->level));
221
222 printf("ARRAY /dev/?? level=%s metadata=1 num-devices=%d UUID=",
223 c?c:"-unknown-", sb->raid_disks);
224 for (i=0; i<16; i++) {
225 printf("%02x", sb->set_uuid[i]);
226 if ((i&3)==0 && i != 0) printf(":");
227 }
228 printf("\n");
229 }
230
231 static void detail_super1(void *sbv)
232 {
233 struct mdp_superblock_1 *sb = sbv;
234 int i;
235
236 printf(" UUID : ");
237 for (i=0; i<16; i++) {
238 printf("%02x", sb->set_uuid[i]);
239 if ((i&3)==0 && i != 0) printf(":");
240 }
241 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
242 }
243
244 static void brief_detail_super1(void *sbv)
245 {
246 struct mdp_superblock_1 *sb = sbv;
247 int i;
248
249 printf(" UUID=");
250 for (i=0; i<16; i++) {
251 printf("%02x", sb->set_uuid[i]);
252 if ((i&3)==0 && i != 0) printf(":");
253 }
254 }
255
256 static void uuid_from_super1(int uuid[4], void * sbv)
257 {
258 struct mdp_superblock_1 *super = sbv;
259 char *cuuid = (char*)uuid;
260 int i;
261 for (i=0; i<16; i++)
262 cuuid[i] = super->set_uuid[i];
263 }
264
265 static void getinfo_super1(struct mdinfo *info, void *sbv)
266 {
267 struct mdp_superblock_1 *sb = sbv;
268 int working = 0;
269 int i;
270 int role;
271
272 info->array.major_version = 1;
273 info->array.minor_version = __le32_to_cpu(sb->feature_map);
274 info->array.patch_version = 0;
275 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
276 info->array.level = __le32_to_cpu(sb->level);
277 info->array.md_minor = -1;
278 info->array.ctime = __le64_to_cpu(sb->ctime);
279
280 info->disk.major = 0;
281 info->disk.minor = 0;
282
283 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
284 __le32_to_cpu(sb->max_dev) > 512)
285 role = 0xfffe;
286 else
287 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
288
289 info->disk.raid_disk = -1;
290 switch(role) {
291 case 0xFFFF:
292 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
293 break;
294 case 0xFFFE:
295 info->disk.state = 1; /* faulty */
296 break;
297 default:
298 info->disk.state = 6; /* active and in sync */
299 info->disk.raid_disk = role;
300 }
301 info->events = __le64_to_cpu(sb->events);
302
303 memcpy(info->uuid, sb->set_uuid, 16);
304
305 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
306 role = __le16_to_cpu(sb->dev_roles[i]);
307 if (role == 0xFFFF || role < info->array.raid_disks)
308 working++;
309 }
310
311 info->array.working_disks = working;
312 }
313
314 static int update_super1(struct mdinfo *info, void *sbv, char *update, char *devname, int verbose)
315 {
316 int rv = 0;
317 struct mdp_superblock_1 *sb = sbv;
318
319 if (strcmp(update, "force")==0) {
320 sb->events = __cpu_to_le32(info->events);
321 switch(__le32_to_cpu(sb->level)) {
322 case 5: case 4: case 6:
323 /* need to force clean */
324 sb->resync_offset = ~0ULL;
325 }
326 }
327 if (strcmp(update, "assemble")==0) {
328 int d = info->disk.number;
329 int want;
330 if (info->disk.state == 6)
331 want = __cpu_to_le32(info->disk.raid_disk);
332 else
333 want = 0xFFFF;
334 if (sb->dev_roles[d] != want) {
335 sb->dev_roles[d] = want;
336 rv = 1;
337 }
338 }
339 #if 0
340 if (strcmp(update, "newdev") == 0) {
341 int d = info->disk.number;
342 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
343 sb->disks[d].number = d;
344 sb->disks[d].major = info->disk.major;
345 sb->disks[d].minor = info->disk.minor;
346 sb->disks[d].raid_disk = info->disk.raid_disk;
347 sb->disks[d].state = info->disk.state;
348 sb->this_disk = sb->disks[d];
349 }
350 #endif
351 if (strcmp(update, "grow") == 0) {
352 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
353 /* FIXME */
354 }
355 if (strcmp(update, "resync") == 0) {
356 /* make sure resync happens */
357 sb->resync_offset = ~0ULL;
358 }
359
360 sb->sb_csum = calc_sb_1_csum(sb);
361 return rv;
362 }
363
364
365 static __u64 event_super1(void *sbv)
366 {
367 struct mdp_superblock_1 *sb = sbv;
368 return __le64_to_cpu(sb->events);
369 }
370
371 static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info)
372 {
373 struct mdp_superblock_1 *sb = malloc(1024);
374 int spares;
375 memset(sb, 0, 1024);
376
377 if (info->major_version == -1)
378 /* zeroing superblock */
379 return 0;
380
381 spares = info->working_disks - info->active_disks;
382 if (info->raid_disks + spares > 384) {
383 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
384 info->raid_disks , spares, 384);
385 return 0;
386 }
387
388
389 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
390 sb->major_version = __cpu_to_le32(1);
391 sb->feature_map = 0;
392 sb->pad0 = 0;
393 *(__u32*)(sb->set_uuid) = random();
394 *(__u32*)(sb->set_uuid+4) = random();
395 *(__u32*)(sb->set_uuid+8) = random();
396 *(__u32*)(sb->set_uuid+12) = random();
397
398 /* FIXME name */
399
400 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
401 sb->level = __cpu_to_le32(info->level);
402 sb->layout = __cpu_to_le32(info->layout);
403 sb->size = __cpu_to_le64(info->size*2ULL);
404 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
405 sb->raid_disks = __cpu_to_le32(info->raid_disks);
406
407 sb->data_offset = __cpu_to_le64(0);
408 sb->data_size = __cpu_to_le64(0);
409 sb->super_offset = __cpu_to_le64(0);
410 sb->recovery_offset = __cpu_to_le64(0);
411
412 sb->utime = sb->ctime;
413 sb->events = __cpu_to_le64(1);
414 if (info->state & (1<<MD_SB_CLEAN))
415 sb->resync_offset = ~0ULL;
416 else
417 sb->resync_offset = 0;
418 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
419 sizeof(sb->dev_roles[0]));
420 memset(sb->pad3, 0, sizeof(sb->pad3));
421
422 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
423
424 *sbp = sb;
425 return 1;
426 }
427
428 /* Add a device to the superblock being created */
429 static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
430 {
431 struct mdp_superblock_1 *sb = sbv;
432 __u16 *rp = sb->dev_roles + dk->number;
433 if (dk->state == 6) /* active, sync */
434 *rp = __cpu_to_le16(dk->raid_disk);
435 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
436 *rp = 0xffff;
437 else
438 *rp = 0xfffe;
439 }
440
441 static int store_super1(struct supertype *st, int fd, void *sbv)
442 {
443 struct mdp_superblock_1 *sb = sbv;
444 long long sb_offset;
445 int sbsize;
446 long size;
447
448 if (ioctl(fd, BLKGETSIZE, &size))
449 return 1;
450
451
452 if (size < 24)
453 return 2;
454
455 /*
456 * Calculate the position of the superblock.
457 * It is always aligned to a 4K boundary and
458 * depending on minor_version, it can be:
459 * 0: At least 8K, but less than 12K, from end of device
460 * 1: At start of device
461 * 2: 4K from start of device.
462 */
463 switch(st->minor_version) {
464 case 0:
465 sb_offset = size;
466 sb_offset -= 8*2;
467 sb_offset &= ~(4*2-1);
468 break;
469 case 1:
470 sb->super_offset = __cpu_to_le64(0);
471 break;
472 case 2:
473 sb_offset = 4*2;
474 break;
475 default:
476 return -EINVAL;
477 }
478
479
480
481 if (sb_offset != (__le64_to_cpu(sb->super_offset) << 9 ) &&
482 0 != (__le64_to_cpu(sb->super_offset) << 9 )
483 ) {
484 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
485 abort();
486 }
487
488 if (lseek64(fd, sb_offset, 0)< 0LL)
489 return 3;
490
491 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
492
493 if (write(fd, sb, sbsize) != sbsize)
494 return 4;
495
496 fsync(fd);
497 return 0;
498 }
499
500 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
501
502 static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
503 {
504 struct mdp_superblock_1 *sb = sbv;
505 struct mdp_superblock_1 *refsb = NULL;
506 int fd = open(devname, O_RDWR | O_EXCL);
507 int rfd;
508 int rv;
509
510 long size;
511 long long sb_offset;
512
513
514 if (fd < 0) {
515 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
516 devname);
517 return -1;
518 }
519
520 sb->dev_number = __cpu_to_le32(dinfo->number);
521
522 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
523 read(rfd, sb->device_uuid, 16) != 16) {
524 *(__u32*)(sb->device_uuid) = random();
525 *(__u32*)(sb->device_uuid+4) = random();
526 *(__u32*)(sb->device_uuid+8) = random();
527 *(__u32*)(sb->device_uuid+12) = random();
528 }
529 if (rfd >= 0) close(rfd);
530 sb->events = 0;
531
532 if (load_super1(st, fd, (void**)&refsb, NULL)==0) {
533 memcpy(sb->device_uuid, refsb->device_uuid, 16);
534 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
535 /* same array, so preserve events and dev_number */
536 sb->events = refsb->events;
537 sb->dev_number = refsb->dev_number;
538 }
539 free(refsb);
540 }
541
542 if (ioctl(fd, BLKGETSIZE, &size)) {
543 close(fd);
544 return 1;
545 }
546
547 if (size < 24) {
548 close(fd);
549 return 2;
550 }
551
552
553 /*
554 * Calculate the position of the superblock.
555 * It is always aligned to a 4K boundary and
556 * depending on minor_version, it can be:
557 * 0: At least 8K, but less than 12K, from end of device
558 * 1: At start of device
559 * 2: 4K from start of device.
560 */
561 switch(st->minor_version) {
562 case 0:
563 sb_offset = size;
564 sb_offset -= 8*2;
565 sb_offset &= ~(4*2-1);
566 sb->super_offset = __cpu_to_le64(sb_offset);
567 sb->data_offset = __cpu_to_le64(0);
568 sb->data_size = sb->super_offset;
569 break;
570 case 1:
571 sb->super_offset = __cpu_to_le64(0);
572 sb->data_offset = __cpu_to_le64(2);
573 sb->data_size = __cpu_to_le64(size - 2);
574 break;
575 case 2:
576 sb_offset = 4*2;
577 sb->super_offset = __cpu_to_le64(sb_offset);
578 sb->data_offset = __cpu_to_le64(sb_offset+2);
579 sb->data_size = __cpu_to_le64(size - 4*2 - 2);
580 break;
581 default:
582 return -EINVAL;
583 }
584
585
586 sb->sb_csum = calc_sb_1_csum(sb);
587 rv = store_super1(st, fd, sb);
588 if (rv)
589 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
590 close(fd);
591 return rv;
592 }
593
594 static int compare_super1(void **firstp, void *secondv)
595 {
596 /*
597 * return:
598 * 0 same, or first was empty, and second was copied
599 * 1 second had wrong number
600 * 2 wrong uuid
601 * 3 wrong other info
602 */
603 struct mdp_superblock_1 *first = *firstp;
604 struct mdp_superblock_1 *second = secondv;
605
606 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
607 return 1;
608 if (second->major_version != __cpu_to_le32(1))
609 return 1;
610
611 if (!first) {
612 first = malloc(1024);
613 memcpy(first, second, 1024);
614 *firstp = first;
615 return 0;
616 }
617 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
618 return 2;
619
620 if (first->ctime != second->ctime ||
621 first->level != second->level ||
622 first->layout != second->layout ||
623 first->size != second->size ||
624 first->chunksize != second->chunksize ||
625 first->raid_disks != second->raid_disks)
626 return 3;
627 return 0;
628 }
629
630 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
631 {
632 unsigned long size;
633 unsigned long long sb_offset;
634 struct mdp_superblock_1 *super;
635
636
637
638 if (st->ss == NULL) {
639 int bestvers = -1;
640 __u64 bestctime = 0;
641 /* guess... choose latest ctime */
642 st->ss = &super1;
643 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
644 switch(load_super1(st, fd, sbp, devname)) {
645 case 0: super = *sbp;
646 if (bestvers == -1 ||
647 bestctime < __le64_to_cpu(super->ctime)) {
648 bestvers = st->minor_version;
649 bestctime = __le64_to_cpu(super->ctime);
650 }
651 free(super);
652 *sbp = NULL;
653 break;
654 case 1: st->ss = NULL; return 1; /*bad device */
655 case 2: break; /* bad, try next */
656 }
657 }
658 if (bestvers != -1) {
659 int rv;
660 st->minor_version = bestvers;
661 st->ss = &super1;
662 st->max_devs = 384;
663 rv = load_super1(st, fd, sbp, devname);
664 if (rv) st->ss = NULL;
665 return rv;
666 }
667 st->ss = NULL;
668 return 2;
669 }
670 if (ioctl(fd, BLKGETSIZE, &size)) {
671 if (devname)
672 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
673 devname, strerror(errno));
674 return 1;
675 }
676
677 if (size < 24) {
678 if (devname)
679 fprintf(stderr, Name ": %s is too small for md: size is %lu sectors.\n",
680 devname, size);
681 return 1;
682 }
683
684 /*
685 * Calculate the position of the superblock.
686 * It is always aligned to a 4K boundary and
687 * depeding on minor_version, it can be:
688 * 0: At least 8K, but less than 12K, from end of device
689 * 1: At start of device
690 * 2: 4K from start of device.
691 */
692 switch(st->minor_version) {
693 case 0:
694 sb_offset = size;
695 sb_offset -= 8*2;
696 sb_offset &= ~(4*2-1);
697 break;
698 case 1:
699 sb_offset = 0;
700 break;
701 case 2:
702 sb_offset = 4*2;
703 break;
704 default:
705 return -EINVAL;
706 }
707
708 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
709
710
711 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
712 if (devname)
713 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
714 devname, strerror(errno));
715 return 1;
716 }
717
718 super = malloc(1024);
719
720 if (read(fd, super, 1024) != 1024) {
721 if (devname)
722 fprintf(stderr, Name ": Cannot read superblock on %s\n",
723 devname);
724 free(super);
725 return 1;
726 }
727
728 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
729 if (devname)
730 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
731 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
732 free(super);
733 return 2;
734 }
735
736 if (__le32_to_cpu(super->major_version) != 1) {
737 if (devname)
738 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
739 devname, __le32_to_cpu(super->major_version));
740 free(super);
741 return 2;
742 }
743 if (__le64_to_cpu(super->super_offset) != sb_offset) {
744 if (devname)
745 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
746 devname);
747 free(super);
748 return 2;
749 }
750 *sbp = super;
751 return 0;
752 }
753
754
755 static struct supertype *match_metadata_desc1(char *arg)
756 {
757 struct supertype *st = malloc(sizeof(*st));
758 if (!st) return st;
759
760 st->ss = &super1;
761 st->max_devs = 384;
762 if (strcmp(arg, "1") == 0 ||
763 strcmp(arg, "1.0") == 0) {
764 st->minor_version = 0;
765 return st;
766 }
767 if (strcmp(arg, "1.1") == 0) {
768 st->minor_version = 1;
769 return st;
770 }
771 if (strcmp(arg, "1.2") == 0) {
772 st->minor_version = 2;
773 return st;
774 }
775
776 free(st);
777 return NULL;
778 }
779
780 static __u64 avail_size1(__u64 devsize)
781 {
782 if (devsize < 24)
783 return 0;
784
785 return (devsize - 8*2 ) & ~(4*2-1);
786 }
787
788 struct superswitch super1 = {
789 .examine_super = examine_super1,
790 .brief_examine_super = brief_examine_super1,
791 .detail_super = detail_super1,
792 .brief_detail_super = brief_detail_super1,
793 .uuid_from_super = uuid_from_super1,
794 .getinfo_super = getinfo_super1,
795 .update_super = update_super1,
796 .event_super = event_super1,
797 .init_super = init_super1,
798 .add_to_super = add_to_super1,
799 .store_super = store_super1,
800 .write_init_super = write_init_super1,
801 .compare_super = compare_super1,
802 .load_super = load_super1,
803 .match_metadata_desc = match_metadata_desc1,
804 .avail_size = avail_size1,
805 .major = 1,
806 };