Fix create bug which initialised superblocks wrongly.
[thirdparty/mdadm.git] / super1.c
CommitLineData
82d9eba6
NB
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 */
40struct 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
570c0542 86#ifndef offsetof
82d9eba6 87#define offsetof(t,f) ((int)&(((t*)0)->f))
570c0542 88#endif
82d9eba6
NB
89static 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
119static 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");
570c0542 144 printf(" Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
82d9eba6 145 if (sb->data_offset)
570c0542 146 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->data_offset));
82d9eba6 147 if (sb->super_offset)
570c0542 148 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->super_offset));
a3fd117c 149 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
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)));
570c0542 165 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
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
215static 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
231static 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 }
570c0542 241 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
242}
243
244static 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
256static 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
265static 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
314static 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
365static __u64 event_super1(void *sbv)
366{
367 struct mdp_superblock_1 *sb = sbv;
368 return __le64_to_cpu(sb->events);
369}
370
371static int init_super1(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->level);
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 & 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 */
429static 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) /* active -> spare */
436 *rp = 0xffff;
437 else
438 *rp = 0xfffe;
439}
440
441static int store_super1(int fd, void *sbv)
442{
443 struct mdp_superblock_1 *sb = sbv;
444 long long sb_offset;
445 int sbsize;
446
447
448 sb_offset = __le64_to_cpu(sb->super_offset) << 9;
449
450 if (lseek64(fd, sb_offset, 0)< 0LL)
451 return 3;
452
453 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
454
455 if (write(fd, sb, sbsize) != sbsize)
456 return 4;
457
570c0542 458 fsync(fd);
82d9eba6
NB
459 return 0;
460}
461
462static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
463{
464 struct mdp_superblock_1 *sb = sbv;
465 int fd = open(devname, O_RDWR, O_EXCL);
466 int rv;
467
468 long size;
469 long long sb_offset;
470
471
472 if (fd < 0) {
473 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
474 devname);
475 return -1;
476 }
477
478 sb->dev_number = __cpu_to_le32(dinfo->number);
479 *(__u32*)(sb->device_uuid) = random();
480 *(__u32*)(sb->device_uuid+4) = random();
481 *(__u32*)(sb->device_uuid+8) = random();
482 *(__u32*)(sb->device_uuid+12) = random();
483
484
024ce7fe
NB
485 if (ioctl(fd, BLKGETSIZE, &size)) {
486 close(fd);
82d9eba6 487 return 1;
024ce7fe 488 }
82d9eba6 489
024ce7fe
NB
490 if (size < 24) {
491 close(fd);
82d9eba6 492 return 2;
024ce7fe 493 }
82d9eba6
NB
494
495
496 /*
497 * Calculate the position of the superblock.
498 * It is always aligned to a 4K boundary and
499 * depending on minor_version, it can be:
500 * 0: At least 8K, but less than 12K, from end of device
501 * 1: At start of device
502 * 2: 4K from start of device.
503 */
504 switch(st->minor_version) {
505 case 0:
506 sb_offset = size;
507 sb_offset -= 8*2;
508 sb_offset &= ~(4*2-1);
509 sb->super_offset = __cpu_to_le64(sb_offset);
510 sb->data_offset = __cpu_to_le64(0);
511 sb->data_size = sb->super_offset;
512 break;
513 case 1:
514 sb->super_offset = __cpu_to_le64(0);
515 sb->data_offset = __cpu_to_le64(2);
516 sb->data_size = __cpu_to_le64(size - 2);
517 break;
518 case 2:
519 sb_offset = 4*2;
520 sb->super_offset = __cpu_to_le64(sb_offset);
521 sb->data_offset = __cpu_to_le64(sb_offset+2);
522 sb->data_size = __cpu_to_le64(size - 4*2 - 2);
523 break;
524 default:
525 return -EINVAL;
526 }
527
528
529 sb->sb_csum = calc_sb_1_csum(sb);
530 rv = store_super1(fd, sb);
531 if (rv)
532 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
024ce7fe 533 close(fd);
82d9eba6
NB
534 return rv;
535}
536
537static int compare_super1(void **firstp, void *secondv)
538{
539 /*
540 * return:
541 * 0 same, or first was empty, and second was copied
542 * 1 second had wrong number
543 * 2 wrong uuid
544 * 3 wrong other info
545 */
546 struct mdp_superblock_1 *first = *firstp;
547 struct mdp_superblock_1 *second = secondv;
548
549 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
550 return 1;
551 if (second->major_version != __cpu_to_le32(1))
552 return 1;
553
554 if (!first) {
555 first = malloc(1024);
556 memcpy(first, second, 1024);
557 *firstp = first;
558 return 0;
559 }
560 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
561 return 2;
562
563 if (first->ctime != second->ctime ||
564 first->level != second->level ||
565 first->layout != second->layout ||
566 first->size != second->size ||
567 first->chunksize != second->chunksize ||
568 first->raid_disks != second->raid_disks)
569 return 3;
570 return 0;
571}
572
573static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
574{
575 unsigned long size;
576 unsigned long long sb_offset;
577 struct mdp_superblock_1 *super;
578
579
580
581 if (st->ss == NULL) {
570c0542
NB
582 int bestvers = -1;
583 __u64 bestctime = 0;
584 /* guess... choose latest ctime */
82d9eba6
NB
585 st->ss = &super1;
586 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
587 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
588 case 0: super = *sbp;
589 if (bestvers == -1 ||
590 bestctime < __le64_to_cpu(super->ctime)) {
591 bestvers = st->minor_version;
592 bestctime = __le64_to_cpu(super->ctime);
593 }
594 free(super);
595 *sbp = NULL;
596 break;
82d9eba6
NB
597 case 1: st->ss = NULL; return 1; /*bad device */
598 case 2: break; /* bad, try next */
599 }
600 }
570c0542
NB
601 if (bestvers != -1) {
602 int rv;
603 st->minor_version = bestvers;
604 st->ss = &super1;
605 rv = load_super1(st, fd, sbp, devname);
606 if (rv) st->ss = NULL;
607 return rv;
608 }
82d9eba6
NB
609 st->ss = NULL;
610 return 2;
611 }
612 if (ioctl(fd, BLKGETSIZE, &size)) {
613 if (devname)
614 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
615 devname, strerror(errno));
616 return 1;
617 }
618
619 if (size < 24) {
620 if (devname)
621 fprintf(stderr, Name ": %s is too small for md: size is %lu sectors.\n",
622 devname, size);
623 return 1;
624 }
625
626 /*
627 * Calculate the position of the superblock.
628 * It is always aligned to a 4K boundary and
629 * depeding on minor_version, it can be:
630 * 0: At least 8K, but less than 12K, from end of device
631 * 1: At start of device
632 * 2: 4K from start of device.
633 */
634 switch(st->minor_version) {
635 case 0:
636 sb_offset = size;
637 sb_offset -= 8*2;
638 sb_offset &= ~(4*2-1);
639 break;
640 case 1:
641 sb_offset = 0;
642 break;
643 case 2:
644 sb_offset = 4*2;
645 break;
646 default:
647 return -EINVAL;
648 }
649
650 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
651
652
653 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
654 if (devname)
655 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
656 devname, strerror(errno));
657 return 1;
658 }
659
660 super = malloc(1024);
661
662 if (read(fd, super, 1024) != 1024) {
663 if (devname)
664 fprintf(stderr, Name ": Cannot read superblock on %s\n",
665 devname);
666 free(super);
667 return 1;
668 }
669
670 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
671 if (devname)
672 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
673 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
674 free(super);
675 return 2;
676 }
677
678 if (__le32_to_cpu(super->major_version) != 1) {
679 if (devname)
680 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
681 devname, __le32_to_cpu(super->major_version));
682 free(super);
683 return 2;
684 }
685 if (__le64_to_cpu(super->super_offset) != sb_offset) {
686 if (devname)
687 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
688 devname);
689 free(super);
690 return 2;
691 }
692 *sbp = super;
693 return 0;
694}
695
696
697static struct supertype *match_metadata_desc1(char *arg)
698{
699 struct supertype *st = malloc(sizeof(*st));
700 if (!st) return st;
701
702 st->ss = &super1;
703 if (strcmp(arg, "1") == 0 ||
704 strcmp(arg, "1.0") == 0) {
705 st->minor_version = 0;
706 return st;
707 }
708 if (strcmp(arg, "1.1") == 0) {
709 st->minor_version = 1;
710 return st;
711 }
712 if (strcmp(arg, "1.2") == 0) {
713 st->minor_version = 2;
714 return st;
715 }
716
717 free(st);
718 return NULL;
719}
720
721static __u64 avail_size1(__u64 devsize)
722{
723 if (devsize < 24)
724 return 0;
725
726 return (devsize - 8*2 ) & ~(4*2-1);
727}
728
729struct superswitch super1 = {
730 .examine_super = examine_super1,
731 .brief_examine_super = brief_examine_super1,
732 .detail_super = detail_super1,
733 .brief_detail_super = brief_detail_super1,
734 .uuid_from_super = uuid_from_super1,
735 .getinfo_super = getinfo_super1,
736 .update_super = update_super1,
737 .event_super = event_super1,
738 .init_super = init_super1,
739 .add_to_super = add_to_super1,
740 .store_super = store_super1,
741 .write_init_super = write_init_super1,
742 .compare_super = compare_super1,
743 .load_super = load_super1,
744 .match_metadata_desc = match_metadata_desc1,
745 .avail_size = avail_size1,
746 .major = 1,
747};