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