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