Support fixing of byte-swapped superblocks.
[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"
f277ce36 31#include <endian.h>
82d9eba6
NB
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
c7654afc 118#ifndef MDASSEMBLE
82d9eba6
NB
119static void examine_super1(void *sbv)
120{
121 struct mdp_superblock_1 *sb = sbv;
122 time_t atime;
123 int d;
6fbba4c9 124 int faulty;
82d9eba6
NB
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 }
6fbba4c9 201 faulty = 0;
82d9eba6
NB
202 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
203 int role = __le16_to_cpu(sb->dev_roles[i]);
6fbba4c9
NB
204 if (role == 0xFFFE)
205 faulty++;
82d9eba6 206 }
82d9eba6
NB
207 if (faulty) printf(" %d failed", faulty);
208 printf("\n");
209}
210
211
212static void brief_examine_super1(void *sbv)
213{
214 struct mdp_superblock_1 *sb = sbv;
215 int i;
216
217 char *c=map_num(pers, __le32_to_cpu(sb->level));
218
219 printf("ARRAY /dev/?? level=%s metadata=1 num-devices=%d UUID=",
220 c?c:"-unknown-", sb->raid_disks);
221 for (i=0; i<16; i++) {
222 printf("%02x", sb->set_uuid[i]);
223 if ((i&3)==0 && i != 0) printf(":");
224 }
225 printf("\n");
226}
227
228static void detail_super1(void *sbv)
229{
230 struct mdp_superblock_1 *sb = sbv;
231 int i;
232
233 printf(" UUID : ");
234 for (i=0; i<16; i++) {
235 printf("%02x", sb->set_uuid[i]);
236 if ((i&3)==0 && i != 0) printf(":");
237 }
570c0542 238 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
239}
240
241static void brief_detail_super1(void *sbv)
242{
243 struct mdp_superblock_1 *sb = sbv;
244 int i;
245
246 printf(" UUID=");
247 for (i=0; i<16; i++) {
248 printf("%02x", sb->set_uuid[i]);
249 if ((i&3)==0 && i != 0) printf(":");
250 }
251}
252
c7654afc
NB
253#endif
254
82d9eba6
NB
255static void uuid_from_super1(int uuid[4], void * sbv)
256{
257 struct mdp_superblock_1 *super = sbv;
258 char *cuuid = (char*)uuid;
259 int i;
260 for (i=0; i<16; i++)
261 cuuid[i] = super->set_uuid[i];
262}
263
264static void getinfo_super1(struct mdinfo *info, void *sbv)
265{
266 struct mdp_superblock_1 *sb = sbv;
267 int working = 0;
268 int i;
269 int role;
270
271 info->array.major_version = 1;
272 info->array.minor_version = __le32_to_cpu(sb->feature_map);
273 info->array.patch_version = 0;
274 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
275 info->array.level = __le32_to_cpu(sb->level);
276 info->array.md_minor = -1;
277 info->array.ctime = __le64_to_cpu(sb->ctime);
278
279 info->disk.major = 0;
280 info->disk.minor = 0;
fbf8a0b7 281 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
282 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
283 __le32_to_cpu(sb->max_dev) > 512)
284 role = 0xfffe;
285 else
286 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
287
288 info->disk.raid_disk = -1;
289 switch(role) {
290 case 0xFFFF:
291 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
292 break;
293 case 0xFFFE:
294 info->disk.state = 1; /* faulty */
295 break;
296 default:
297 info->disk.state = 6; /* active and in sync */
298 info->disk.raid_disk = role;
299 }
300 info->events = __le64_to_cpu(sb->events);
301
302 memcpy(info->uuid, sb->set_uuid, 16);
303
304 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
305 role = __le16_to_cpu(sb->dev_roles[i]);
fbf8a0b7 306 if (/*role == 0xFFFF || */role < info->array.raid_disks)
82d9eba6
NB
307 working++;
308 }
309
310 info->array.working_disks = working;
311}
312
313static int update_super1(struct mdinfo *info, void *sbv, char *update, char *devname, int verbose)
314{
315 int rv = 0;
316 struct mdp_superblock_1 *sb = sbv;
317
318 if (strcmp(update, "force")==0) {
319 sb->events = __cpu_to_le32(info->events);
320 switch(__le32_to_cpu(sb->level)) {
321 case 5: case 4: case 6:
322 /* need to force clean */
323 sb->resync_offset = ~0ULL;
324 }
325 }
326 if (strcmp(update, "assemble")==0) {
327 int d = info->disk.number;
328 int want;
329 if (info->disk.state == 6)
330 want = __cpu_to_le32(info->disk.raid_disk);
331 else
332 want = 0xFFFF;
333 if (sb->dev_roles[d] != want) {
334 sb->dev_roles[d] = want;
335 rv = 1;
336 }
337 }
338#if 0
339 if (strcmp(update, "newdev") == 0) {
340 int d = info->disk.number;
341 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
342 sb->disks[d].number = d;
343 sb->disks[d].major = info->disk.major;
344 sb->disks[d].minor = info->disk.minor;
345 sb->disks[d].raid_disk = info->disk.raid_disk;
346 sb->disks[d].state = info->disk.state;
347 sb->this_disk = sb->disks[d];
348 }
349#endif
350 if (strcmp(update, "grow") == 0) {
351 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
352 /* FIXME */
353 }
354 if (strcmp(update, "resync") == 0) {
355 /* make sure resync happens */
356 sb->resync_offset = ~0ULL;
357 }
358
359 sb->sb_csum = calc_sb_1_csum(sb);
360 return rv;
361}
362
363
364static __u64 event_super1(void *sbv)
365{
366 struct mdp_superblock_1 *sb = sbv;
367 return __le64_to_cpu(sb->events);
368}
369
96395475 370static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info)
82d9eba6
NB
371{
372 struct mdp_superblock_1 *sb = malloc(1024);
373 int spares;
374 memset(sb, 0, 1024);
375
376 if (info->major_version == -1)
377 /* zeroing superblock */
378 return 0;
379
380 spares = info->working_disks - info->active_disks;
381 if (info->raid_disks + spares > 384) {
382 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
383 info->raid_disks , spares, 384);
384 return 0;
385 }
386
387
388 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
389 sb->major_version = __cpu_to_le32(1);
390 sb->feature_map = 0;
391 sb->pad0 = 0;
392 *(__u32*)(sb->set_uuid) = random();
393 *(__u32*)(sb->set_uuid+4) = random();
394 *(__u32*)(sb->set_uuid+8) = random();
395 *(__u32*)(sb->set_uuid+12) = random();
396
397 /* FIXME name */
398
399 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
400 sb->level = __cpu_to_le32(info->level);
ea329559 401 sb->layout = __cpu_to_le32(info->layout);
82d9eba6
NB
402 sb->size = __cpu_to_le64(info->size*2ULL);
403 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
404 sb->raid_disks = __cpu_to_le32(info->raid_disks);
405
406 sb->data_offset = __cpu_to_le64(0);
407 sb->data_size = __cpu_to_le64(0);
408 sb->super_offset = __cpu_to_le64(0);
409 sb->recovery_offset = __cpu_to_le64(0);
410
411 sb->utime = sb->ctime;
412 sb->events = __cpu_to_le64(1);
34321279 413 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
414 sb->resync_offset = ~0ULL;
415 else
416 sb->resync_offset = 0;
417 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
418 sizeof(sb->dev_roles[0]));
419 memset(sb->pad3, 0, sizeof(sb->pad3));
420
421 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
422
423 *sbp = sb;
424 return 1;
425}
426
427/* Add a device to the superblock being created */
428static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
429{
430 struct mdp_superblock_1 *sb = sbv;
431 __u16 *rp = sb->dev_roles + dk->number;
432 if (dk->state == 6) /* active, sync */
433 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 434 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6
NB
435 *rp = 0xffff;
436 else
437 *rp = 0xfffe;
438}
439
96395475 440static int store_super1(struct supertype *st, int fd, void *sbv)
82d9eba6
NB
441{
442 struct mdp_superblock_1 *sb = sbv;
6fbba4c9 443 unsigned long long sb_offset;
82d9eba6 444 int sbsize;
96395475
NB
445 long size;
446
447 if (ioctl(fd, BLKGETSIZE, &size))
448 return 1;
449
450
451 if (size < 24)
452 return 2;
453
454 /*
455 * Calculate the position of the superblock.
456 * It is always aligned to a 4K boundary and
457 * depending on minor_version, it can be:
458 * 0: At least 8K, but less than 12K, from end of device
459 * 1: At start of device
460 * 2: 4K from start of device.
461 */
462 switch(st->minor_version) {
463 case 0:
464 sb_offset = size;
465 sb_offset -= 8*2;
466 sb_offset &= ~(4*2-1);
467 break;
468 case 1:
6fbba4c9 469 sb_offset = 0;
96395475
NB
470 break;
471 case 2:
472 sb_offset = 4*2;
473 break;
474 default:
475 return -EINVAL;
476 }
477
82d9eba6
NB
478
479
6fbba4c9
NB
480 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
481 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
482 ) {
483 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
484 abort();
485 }
82d9eba6 486
6fbba4c9 487 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
488 return 3;
489
490 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
491
492 if (write(fd, sb, sbsize) != sbsize)
493 return 4;
494
570c0542 495 fsync(fd);
82d9eba6
NB
496 return 0;
497}
498
892debc8
NB
499static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
500
82d9eba6
NB
501static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
502{
503 struct mdp_superblock_1 *sb = sbv;
892debc8
NB
504 struct mdp_superblock_1 *refsb = NULL;
505 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 506 int rfd;
82d9eba6
NB
507 int rv;
508
509 long size;
510 long long sb_offset;
511
512
513 if (fd < 0) {
514 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
515 devname);
516 return -1;
517 }
518
519 sb->dev_number = __cpu_to_le32(dinfo->number);
892debc8 520
dfe47e00
NB
521 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
522 read(rfd, sb->device_uuid, 16) != 16) {
523 *(__u32*)(sb->device_uuid) = random();
524 *(__u32*)(sb->device_uuid+4) = random();
525 *(__u32*)(sb->device_uuid+8) = random();
526 *(__u32*)(sb->device_uuid+12) = random();
527 }
528 if (rfd >= 0) close(rfd);
892debc8
NB
529 sb->events = 0;
530
531 if (load_super1(st, fd, (void**)&refsb, NULL)==0) {
532 memcpy(sb->device_uuid, refsb->device_uuid, 16);
533 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
534 /* same array, so preserve events and dev_number */
535 sb->events = refsb->events;
536 sb->dev_number = refsb->dev_number;
537 }
538 free(refsb);
539 }
82d9eba6 540
024ce7fe
NB
541 if (ioctl(fd, BLKGETSIZE, &size)) {
542 close(fd);
82d9eba6 543 return 1;
024ce7fe 544 }
82d9eba6 545
024ce7fe
NB
546 if (size < 24) {
547 close(fd);
82d9eba6 548 return 2;
024ce7fe 549 }
82d9eba6
NB
550
551
552 /*
553 * Calculate the position of the superblock.
554 * It is always aligned to a 4K boundary and
555 * depending on minor_version, it can be:
556 * 0: At least 8K, but less than 12K, from end of device
557 * 1: At start of device
558 * 2: 4K from start of device.
559 */
560 switch(st->minor_version) {
561 case 0:
562 sb_offset = size;
563 sb_offset -= 8*2;
564 sb_offset &= ~(4*2-1);
565 sb->super_offset = __cpu_to_le64(sb_offset);
566 sb->data_offset = __cpu_to_le64(0);
567 sb->data_size = sb->super_offset;
568 break;
569 case 1:
570 sb->super_offset = __cpu_to_le64(0);
571 sb->data_offset = __cpu_to_le64(2);
572 sb->data_size = __cpu_to_le64(size - 2);
573 break;
574 case 2:
575 sb_offset = 4*2;
576 sb->super_offset = __cpu_to_le64(sb_offset);
577 sb->data_offset = __cpu_to_le64(sb_offset+2);
578 sb->data_size = __cpu_to_le64(size - 4*2 - 2);
579 break;
580 default:
581 return -EINVAL;
582 }
583
584
585 sb->sb_csum = calc_sb_1_csum(sb);
96395475 586 rv = store_super1(st, fd, sb);
82d9eba6
NB
587 if (rv)
588 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
024ce7fe 589 close(fd);
82d9eba6
NB
590 return rv;
591}
592
593static int compare_super1(void **firstp, void *secondv)
594{
595 /*
596 * return:
597 * 0 same, or first was empty, and second was copied
598 * 1 second had wrong number
599 * 2 wrong uuid
600 * 3 wrong other info
601 */
602 struct mdp_superblock_1 *first = *firstp;
603 struct mdp_superblock_1 *second = secondv;
604
605 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
606 return 1;
607 if (second->major_version != __cpu_to_le32(1))
608 return 1;
609
610 if (!first) {
611 first = malloc(1024);
612 memcpy(first, second, 1024);
613 *firstp = first;
614 return 0;
615 }
616 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
617 return 2;
618
619 if (first->ctime != second->ctime ||
620 first->level != second->level ||
621 first->layout != second->layout ||
622 first->size != second->size ||
623 first->chunksize != second->chunksize ||
624 first->raid_disks != second->raid_disks)
625 return 3;
626 return 0;
627}
628
629static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
630{
631 unsigned long size;
632 unsigned long long sb_offset;
633 struct mdp_superblock_1 *super;
634
635
636
637 if (st->ss == NULL) {
570c0542
NB
638 int bestvers = -1;
639 __u64 bestctime = 0;
640 /* guess... choose latest ctime */
82d9eba6
NB
641 st->ss = &super1;
642 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
643 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
644 case 0: super = *sbp;
645 if (bestvers == -1 ||
646 bestctime < __le64_to_cpu(super->ctime)) {
647 bestvers = st->minor_version;
648 bestctime = __le64_to_cpu(super->ctime);
649 }
650 free(super);
651 *sbp = NULL;
652 break;
82d9eba6
NB
653 case 1: st->ss = NULL; return 1; /*bad device */
654 case 2: break; /* bad, try next */
655 }
656 }
570c0542
NB
657 if (bestvers != -1) {
658 int rv;
659 st->minor_version = bestvers;
660 st->ss = &super1;
ea329559 661 st->max_devs = 384;
570c0542
NB
662 rv = load_super1(st, fd, sbp, devname);
663 if (rv) st->ss = NULL;
664 return rv;
665 }
82d9eba6
NB
666 st->ss = NULL;
667 return 2;
668 }
669 if (ioctl(fd, BLKGETSIZE, &size)) {
670 if (devname)
671 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
672 devname, strerror(errno));
673 return 1;
674 }
675
676 if (size < 24) {
677 if (devname)
678 fprintf(stderr, Name ": %s is too small for md: size is %lu sectors.\n",
679 devname, size);
680 return 1;
681 }
682
683 /*
684 * Calculate the position of the superblock.
685 * It is always aligned to a 4K boundary and
686 * depeding on minor_version, it can be:
687 * 0: At least 8K, but less than 12K, from end of device
688 * 1: At start of device
689 * 2: 4K from start of device.
690 */
691 switch(st->minor_version) {
692 case 0:
693 sb_offset = size;
694 sb_offset -= 8*2;
695 sb_offset &= ~(4*2-1);
696 break;
697 case 1:
698 sb_offset = 0;
699 break;
700 case 2:
701 sb_offset = 4*2;
702 break;
703 default:
704 return -EINVAL;
705 }
706
707 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
708
709
710 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
711 if (devname)
712 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
713 devname, strerror(errno));
714 return 1;
715 }
716
717 super = malloc(1024);
718
719 if (read(fd, super, 1024) != 1024) {
720 if (devname)
721 fprintf(stderr, Name ": Cannot read superblock on %s\n",
722 devname);
723 free(super);
724 return 1;
725 }
726
727 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
728 if (devname)
729 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
730 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
731 free(super);
732 return 2;
733 }
734
735 if (__le32_to_cpu(super->major_version) != 1) {
736 if (devname)
737 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
738 devname, __le32_to_cpu(super->major_version));
739 free(super);
740 return 2;
741 }
742 if (__le64_to_cpu(super->super_offset) != sb_offset) {
743 if (devname)
744 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
745 devname);
746 free(super);
747 return 2;
748 }
749 *sbp = super;
750 return 0;
751}
752
753
754static struct supertype *match_metadata_desc1(char *arg)
755{
756 struct supertype *st = malloc(sizeof(*st));
757 if (!st) return st;
758
759 st->ss = &super1;
ea329559 760 st->max_devs = 384;
82d9eba6
NB
761 if (strcmp(arg, "1") == 0 ||
762 strcmp(arg, "1.0") == 0) {
763 st->minor_version = 0;
764 return st;
765 }
766 if (strcmp(arg, "1.1") == 0) {
767 st->minor_version = 1;
768 return st;
769 }
770 if (strcmp(arg, "1.2") == 0) {
771 st->minor_version = 2;
772 return st;
773 }
774
775 free(st);
776 return NULL;
777}
778
779static __u64 avail_size1(__u64 devsize)
780{
781 if (devsize < 24)
782 return 0;
783
784 return (devsize - 8*2 ) & ~(4*2-1);
785}
786
787struct superswitch super1 = {
c7654afc 788#ifndef MDASSEMBLE
82d9eba6
NB
789 .examine_super = examine_super1,
790 .brief_examine_super = brief_examine_super1,
791 .detail_super = detail_super1,
792 .brief_detail_super = brief_detail_super1,
c7654afc 793#endif
82d9eba6
NB
794 .uuid_from_super = uuid_from_super1,
795 .getinfo_super = getinfo_super1,
796 .update_super = update_super1,
797 .event_super = event_super1,
798 .init_super = init_super1,
799 .add_to_super = add_to_super1,
800 .store_super = store_super1,
801 .write_init_super = write_init_super1,
802 .compare_super = compare_super1,
803 .load_super = load_super1,
804 .match_metadata_desc = match_metadata_desc1,
805 .avail_size = avail_size1,
806 .major = 1,
f277ce36
NB
807#if __BYTE_ORDER == BIG_ENDIAN
808 .swapuuid = 0,
809#else
810 .swapuuid = 1,
811#endif
82d9eba6 812};