]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
6bd5ceca05c997b3cc0076d52e31da3834204045
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2004 Neil Brown <neilb@cse.unsw.edu.au>
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.
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.
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
22 * Email: <neilb@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
32 #include "asm/byteorder.h"
34 * The version-1 superblock :
35 * All numeric fields are little-endian.
37 * total size: 256 bytes plus 2 per device.
38 * 1K allows 384 devices.
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 */
47 __u8 set_uuid
[16]; /* user-space generated. */
48 char set_name
[32]; /* set and interpreted by user-space */
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 */
55 __u32 chunksize
; /* in 512byte sectors */
57 __u8 pad1
[128-96]; /* set to 0 when written */
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 */
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 */
77 /* device state information. Indexed by dev_number.
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.
83 __u16 dev_roles
[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
87 #define offsetof(t,f) ((int)&(((t*)0)->f))
89 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
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
;
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");
104 disk_csum
= sb
->sb_csum
;
107 for (i
=0; size
>=4; size
-= 4 )
108 newcsum
+= __le32_to_cpu(*isuper
++);
111 newcsum
+= __le16_to_cpu(*(unsigned short*) isuper
);
113 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
114 sb
->sb_csum
= disk_csum
;
119 static void examine_super1(void *sbv
)
121 struct mdp_superblock_1
*sb
= sbv
;
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(":");
136 printf(" Name : %.32s\n", sb
->set_name
);
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
));
144 printf(" Device Size : %llu%s\n", (unsigned long long)sb
->data_size
, human_size(sb
->data_size
<<9));
146 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->data_offset
));
147 if (sb
->super_offset
)
148 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->super_offset
));
149 printf(" State : %s\n", (__le64_to_cpu(sb
->resync_offset
)+1)? "active":"clean");
150 printf(" Device UUID : ");
151 for (i
=0; i
<16; i
++) {
152 printf("%02x", sb
->set_uuid
[i
]);
153 if ((i
&3)==0 && i
!= 0) printf(":");
157 atime
= __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
;
158 printf(" Update Time : %.24s\n", ctime(&atime
));
160 if (calc_sb_1_csum(sb
) == sb
->sb_csum
)
161 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb
->sb_csum
));
163 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb
->sb_csum
),
164 __le32_to_cpu(calc_sb_1_csum(sb
)));
165 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb
->events
));
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-");
171 switch(__le32_to_cpu(sb
->level
)) {
175 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb
->chunksize
/2));
178 printf(" Rounding : %dK\n", __le32_to_cpu(sb
->chunksize
/2));
183 printf(" Array State : ");
184 for (d
=0; d
<__le32_to_cpu(sb
->raid_disks
); d
++) {
188 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
189 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
191 if (i
== __le32_to_cpu(sb
->dev_number
))
196 if (cnt
> 1) printf("?");
197 else if (cnt
== 1 && me
) printf("U");
198 else if (cnt
== 1) printf("u");
202 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
203 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
207 if (faulty
) printf(" %d failed", faulty
);
212 static void brief_examine_super1(void *sbv
)
214 struct mdp_superblock_1
*sb
= sbv
;
217 char *c
=map_num(pers
, __le32_to_cpu(sb
->level
));
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(":");
228 static void detail_super1(void *sbv
)
230 struct mdp_superblock_1
*sb
= sbv
;
234 for (i
=0; i
<16; i
++) {
235 printf("%02x", sb
->set_uuid
[i
]);
236 if ((i
&3)==0 && i
!= 0) printf(":");
238 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb
->events
));
241 static void brief_detail_super1(void *sbv
)
243 struct mdp_superblock_1
*sb
= sbv
;
247 for (i
=0; i
<16; i
++) {
248 printf("%02x", sb
->set_uuid
[i
]);
249 if ((i
&3)==0 && i
!= 0) printf(":");
255 static void uuid_from_super1(int uuid
[4], void * sbv
)
257 struct mdp_superblock_1
*super
= sbv
;
258 char *cuuid
= (char*)uuid
;
261 cuuid
[i
] = super
->set_uuid
[i
];
264 static void getinfo_super1(struct mdinfo
*info
, void *sbv
)
266 struct mdp_superblock_1
*sb
= sbv
;
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
);
279 info
->disk
.major
= 0;
280 info
->disk
.minor
= 0;
281 info
->disk
.number
= __le32_to_cpu(sb
->dev_number
);
282 if (__le32_to_cpu(sb
->dev_number
) >= __le32_to_cpu(sb
->max_dev
) ||
283 __le32_to_cpu(sb
->max_dev
) > 512)
286 role
= __le16_to_cpu(sb
->dev_roles
[__le32_to_cpu(sb
->dev_number
)]);
288 info
->disk
.raid_disk
= -1;
291 info
->disk
.state
= 2; /* spare: ACTIVE, not sync, not faulty */
294 info
->disk
.state
= 1; /* faulty */
297 info
->disk
.state
= 6; /* active and in sync */
298 info
->disk
.raid_disk
= role
;
300 info
->events
= __le64_to_cpu(sb
->events
);
302 memcpy(info
->uuid
, sb
->set_uuid
, 16);
304 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
305 role
= __le16_to_cpu(sb
->dev_roles
[i
]);
306 if (/*role == 0xFFFF || */role
< info
->array
.raid_disks
)
310 info
->array
.working_disks
= working
;
313 static int update_super1(struct mdinfo
*info
, void *sbv
, char *update
, char *devname
, int verbose
)
316 struct mdp_superblock_1
*sb
= sbv
;
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;
326 if (strcmp(update
, "assemble")==0) {
327 int d
= info
->disk
.number
;
329 if (info
->disk
.state
== 6)
330 want
= __cpu_to_le32(info
->disk
.raid_disk
);
333 if (sb
->dev_roles
[d
] != want
) {
334 sb
->dev_roles
[d
] = want
;
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
];
350 if (strcmp(update
, "grow") == 0) {
351 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
354 if (strcmp(update
, "resync") == 0) {
355 /* make sure resync happens */
356 sb
->resync_offset
= ~0ULL;
359 sb
->sb_csum
= calc_sb_1_csum(sb
);
364 static __u64
event_super1(void *sbv
)
366 struct mdp_superblock_1
*sb
= sbv
;
367 return __le64_to_cpu(sb
->events
);
370 static int init_super1(struct supertype
*st
, void **sbp
, mdu_array_info_t
*info
)
372 struct mdp_superblock_1
*sb
= malloc(1024);
376 if (info
->major_version
== -1)
377 /* zeroing superblock */
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);
388 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
389 sb
->major_version
= __cpu_to_le32(1);
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();
399 sb
->ctime
= __cpu_to_le64((unsigned long long)time(0));
400 sb
->level
= __cpu_to_le32(info
->level
);
401 sb
->layout
= __cpu_to_le32(info
->layout
);
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
);
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);
411 sb
->utime
= sb
->ctime
;
412 sb
->events
= __cpu_to_le64(1);
413 if (info
->state
& (1<<MD_SB_CLEAN
))
414 sb
->resync_offset
= ~0ULL;
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
));
421 memset(sb
->dev_roles
, 0xff, 1024 - sizeof(struct mdp_superblock_1
));
427 /* Add a device to the superblock being created */
428 static void add_to_super1(void *sbv
, mdu_disk_info_t
*dk
)
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
);
434 else if ((dk
->state
& ~2) == 0) /* active or idle -> spare */
440 static int store_super1(struct supertype
*st
, int fd
, void *sbv
)
442 struct mdp_superblock_1
*sb
= sbv
;
443 unsigned long long sb_offset
;
447 if (ioctl(fd
, BLKGETSIZE
, &size
))
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.
462 switch(st
->minor_version
) {
466 sb_offset
&= ~(4*2-1);
480 if (sb_offset
!= __le64_to_cpu(sb
->super_offset
) &&
481 0 != __le64_to_cpu(sb
->super_offset
)
483 fprintf(stderr
, Name
": internal error - sb_offset is wrong\n");
487 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL)
490 sbsize
= sizeof(*sb
) + 2 * __le32_to_cpu(sb
->max_dev
);
492 if (write(fd
, sb
, sbsize
) != sbsize
)
499 static int load_super1(struct supertype
*st
, int fd
, void **sbp
, char *devname
);
501 static int write_init_super1(struct supertype
*st
, void *sbv
, mdu_disk_info_t
*dinfo
, char *devname
)
503 struct mdp_superblock_1
*sb
= sbv
;
504 struct mdp_superblock_1
*refsb
= NULL
;
505 int fd
= open(devname
, O_RDWR
| O_EXCL
);
514 fprintf(stderr
, Name
": Failed to open %s to write superblock\n",
519 sb
->dev_number
= __cpu_to_le32(dinfo
->number
);
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();
528 if (rfd
>= 0) close(rfd
);
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
;
541 if (ioctl(fd
, BLKGETSIZE
, &size
)) {
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.
560 switch(st
->minor_version
) {
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
;
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);
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);
585 sb
->sb_csum
= calc_sb_1_csum(sb
);
586 rv
= store_super1(st
, fd
, sb
);
588 fprintf(stderr
, Name
": failed to write superblock to %s\n", devname
);
593 static int compare_super1(void **firstp
, void *secondv
)
597 * 0 same, or first was empty, and second was copied
598 * 1 second had wrong number
602 struct mdp_superblock_1
*first
= *firstp
;
603 struct mdp_superblock_1
*second
= secondv
;
605 if (second
->magic
!= __cpu_to_le32(MD_SB_MAGIC
))
607 if (second
->major_version
!= __cpu_to_le32(1))
611 first
= malloc(1024);
612 memcpy(first
, second
, 1024);
616 if (memcmp(first
->set_uuid
, second
->set_uuid
, 16)!= 0)
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
)
629 static int load_super1(struct supertype
*st
, int fd
, void **sbp
, char *devname
)
632 unsigned long long sb_offset
;
633 struct mdp_superblock_1
*super
;
637 if (st
->ss
== NULL
) {
640 /* guess... choose latest ctime */
642 for (st
->minor_version
= 0; st
->minor_version
<= 2 ; st
->minor_version
++) {
643 switch(load_super1(st
, fd
, sbp
, devname
)) {
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
);
653 case 1: st
->ss
= NULL
; return 1; /*bad device */
654 case 2: break; /* bad, try next */
657 if (bestvers
!= -1) {
659 st
->minor_version
= bestvers
;
662 rv
= load_super1(st
, fd
, sbp
, devname
);
663 if (rv
) st
->ss
= NULL
;
669 if (ioctl(fd
, BLKGETSIZE
, &size
)) {
671 fprintf(stderr
, Name
": cannot find device size for %s: %s\n",
672 devname
, strerror(errno
));
678 fprintf(stderr
, Name
": %s is too small for md: size is %lu sectors.\n",
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.
691 switch(st
->minor_version
) {
695 sb_offset
&= ~(4*2-1);
707 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
710 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL) {
712 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
713 devname
, strerror(errno
));
717 super
= malloc(1024);
719 if (read(fd
, super
, 1024) != 1024) {
721 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
727 if (__le32_to_cpu(super
->magic
) != MD_SB_MAGIC
) {
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
));
735 if (__le32_to_cpu(super
->major_version
) != 1) {
737 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
738 devname
, __le32_to_cpu(super
->major_version
));
742 if (__le64_to_cpu(super
->super_offset
) != sb_offset
) {
744 fprintf(stderr
, Name
": No superblock found on %s (super_offset is wrong)\n",
754 static struct supertype
*match_metadata_desc1(char *arg
)
756 struct supertype
*st
= malloc(sizeof(*st
));
761 if (strcmp(arg
, "1") == 0 ||
762 strcmp(arg
, "1.0") == 0) {
763 st
->minor_version
= 0;
766 if (strcmp(arg
, "1.1") == 0) {
767 st
->minor_version
= 1;
770 if (strcmp(arg
, "1.2") == 0) {
771 st
->minor_version
= 2;
779 static __u64
avail_size1(__u64 devsize
)
784 return (devsize
- 8*2 ) & ~(4*2-1);
787 struct superswitch super1
= {
789 .examine_super
= examine_super1
,
790 .brief_examine_super
= brief_examine_super1
,
791 .detail_super
= detail_super1
,
792 .brief_detail_super
= brief_detail_super1
,
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
,