2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
25 #define HAVE_STDINT_H 1
29 * All handling for the 0.90.0 version superblock is in
32 * - finding, loading, and writing the superblock.
33 * - initialising a new superblock
34 * - printing the superblock for --examine
35 * - printing part of the superblock for --detail
39 static unsigned long calc_sb0_csum(mdp_super_t
*super
)
41 unsigned long csum
= super
->sb_csum
;
42 unsigned long newcsum
;
44 newcsum
= calc_csum(super
, MD_SB_BYTES
);
45 super
->sb_csum
= csum
;
49 static void super0_swap_endian(struct mdp_superblock_s
*sb
)
51 /* as super0 superblocks are host-endian, it is sometimes
52 * useful to be able to swap the endianness
53 * as (almost) everything is u32's we byte-swap every 4byte
55 * We then also have to swap the events_hi and events_lo
57 char *sbc
= (char *)sb
;
61 for (i
=0; i
< MD_SB_BYTES
; i
+=4) {
70 sb
->events_hi
= sb
->events_lo
;
73 t32
= sb
->cp_events_hi
;
74 sb
->cp_events_hi
= sb
->cp_events_lo
;
75 sb
->cp_events_lo
= t32
;
81 static void examine_super0(struct supertype
*st
, char *homehost
)
83 mdp_super_t
*sb
= st
->sb
;
89 printf(" Magic : %08x\n", sb
->md_magic
);
90 printf(" Version : %d.%02d.%02d\n", sb
->major_version
, sb
->minor_version
,
92 if (sb
->minor_version
>= 90) {
93 printf(" UUID : %08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
94 sb
->set_uuid2
, sb
->set_uuid3
);
97 void *hash
= sha1_buffer(homehost
,
100 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
101 printf(" (local to host %s)", homehost
);
105 printf(" UUID : %08x\n", sb
->set_uuid0
);
107 if (sb
->not_persistent
)
108 printf(" Eedk : not persistent\n");
111 printf(" Creation Time : %.24s\n", ctime(&atime
));
112 c
=map_num(pers
, sb
->level
);
113 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
114 if ((int)sb
->level
> 0) {
115 int ddsks
= 0, ddsks_denom
= 1;
116 printf(" Used Dev Size : %d%s\n", sb
->size
,
117 human_size((long long)sb
->size
<<10));
119 case 1: ddsks
=1;break;
121 case 5: ddsks
= sb
->raid_disks
-1; break;
122 case 6: ddsks
= sb
->raid_disks
-2; break;
123 case 10: ddsks
= sb
->raid_disks
;
124 ddsks_denom
= (sb
->layout
&255) * ((sb
->layout
>>8)&255);
127 long long asize
= sb
->size
;
128 asize
= (asize
<< 10) * ddsks
/ ddsks_denom
;
129 printf(" Array Size : %llu%s\n",
130 asize
>> 10, human_size(asize
));
133 printf(" Raid Devices : %d\n", sb
->raid_disks
);
134 printf(" Total Devices : %d\n", sb
->nr_disks
);
135 printf("Preferred Minor : %d\n", sb
->md_minor
);
137 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
138 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb
->reshape_position
/2, human_size((long long)sb
->reshape_position
<<9));
139 if (sb
->delta_disks
) {
140 printf(" Delta Devices : %d", sb
->delta_disks
);
141 printf(" (%d->%d)\n", sb
->raid_disks
-sb
->delta_disks
, sb
->raid_disks
);
142 if (((int)sb
->delta_disks
) < 0)
143 delta_extra
= - sb
->delta_disks
;
145 if (sb
->new_level
!= sb
->level
) {
146 c
= map_num(pers
, sb
->new_level
);
147 printf(" New Level : %s\n", c
?c
:"-unknown-");
149 if (sb
->new_layout
!= sb
->layout
) {
150 if (sb
->level
== 5) {
151 c
= map_num(r5layout
, sb
->new_layout
);
152 printf(" New Layout : %s\n", c
?c
:"-unknown-");
154 if (sb
->level
== 6) {
155 c
= map_num(r6layout
, sb
->new_layout
);
156 printf(" New Layout : %s\n", c
?c
:"-unknown-");
158 if (sb
->level
== 10) {
159 printf(" New Layout : near=%d, %s=%d\n",
161 (sb
->new_layout
&0x10000)?"offset":"far",
162 (sb
->new_layout
>>8)&255);
165 if (sb
->new_chunk
!= sb
->chunk_size
)
166 printf(" New Chunksize : %d\n", sb
->new_chunk
);
170 printf(" Update Time : %.24s\n", ctime(&atime
));
171 printf(" State : %s\n",
172 (sb
->state
&(1<<MD_SB_CLEAN
))?"clean":"active");
173 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
))
174 printf("Internal Bitmap : present\n");
175 printf(" Active Devices : %d\n", sb
->active_disks
);
176 printf("Working Devices : %d\n", sb
->working_disks
);
177 printf(" Failed Devices : %d\n", sb
->failed_disks
);
178 printf(" Spare Devices : %d\n", sb
->spare_disks
);
179 if (calc_sb0_csum(sb
) == sb
->sb_csum
)
180 printf(" Checksum : %x - correct\n", sb
->sb_csum
);
182 printf(" Checksum : %x - expected %lx\n", sb
->sb_csum
, calc_sb0_csum(sb
));
183 printf(" Events : %llu\n",
184 ((unsigned long long)sb
->events_hi
<< 32)
187 if (sb
->level
== 5) {
188 c
= map_num(r5layout
, sb
->layout
);
189 printf(" Layout : %s\n", c
?c
:"-unknown-");
191 if (sb
->level
== 6) {
192 c
= map_num(r6layout
, sb
->layout
);
193 printf(" Layout : %s\n", c
?c
:"-unknown-");
195 if (sb
->level
== 10) {
197 print_r10_layout(sb
->layout
);
206 printf(" Chunk Size : %dK\n", sb
->chunk_size
/1024);
209 printf(" Rounding : %dK\n", sb
->chunk_size
/1024);
214 printf(" Number Major Minor RaidDevice State\n");
215 for (d
= -1; d
<(signed int)(sb
->raid_disks
+delta_extra
+ sb
->spare_disks
); d
++) {
220 if (d
>=0) dp
= &sb
->disks
[d
];
221 else dp
= &sb
->this_disk
;
222 snprintf(nb
, sizeof(nb
), "%4d", d
);
223 printf("%4s %5d %5d %5d %5d ", d
< 0 ? "this" : nb
,
224 dp
->number
, dp
->major
, dp
->minor
, dp
->raid_disk
);
225 wonly
= dp
->state
& (1<<MD_DISK_WRITEMOSTLY
);
226 dp
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
227 if (dp
->state
& (1<<MD_DISK_FAULTY
)) printf(" faulty");
228 if (dp
->state
& (1<<MD_DISK_ACTIVE
)) printf(" active");
229 if (dp
->state
& (1<<MD_DISK_SYNC
)) printf(" sync");
230 if (dp
->state
& (1<<MD_DISK_REMOVED
)) printf(" removed");
231 if (wonly
) printf(" write-mostly");
232 if (dp
->state
== 0) printf(" spare");
233 if ((dv
=map_dev(dp
->major
, dp
->minor
, 0)))
236 if (d
== -1) printf("\n");
240 static void brief_examine_super0(struct supertype
*st
, int verbose
)
242 mdp_super_t
*sb
= st
->sb
;
243 char *c
=map_num(pers
, sb
->level
);
246 sprintf(devname
, "/dev/md%d", sb
->md_minor
);
249 printf("ARRAY %s level=%s num-devices=%d",
251 c
?c
:"-unknown-", sb
->raid_disks
);
253 printf("ARRAY %s", devname
);
255 if (sb
->minor_version
>= 90)
256 printf(" UUID=%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
257 sb
->set_uuid2
, sb
->set_uuid3
);
259 printf(" UUID=%08x", sb
->set_uuid0
);
263 static void export_examine_super0(struct supertype
*st
)
265 mdp_super_t
*sb
= st
->sb
;
267 printf("MD_LEVEL=%s\n", map_num(pers
, sb
->level
));
268 printf("MD_DEVICES=%d\n", sb
->raid_disks
);
269 if (sb
->minor_version
>= 90)
270 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
271 sb
->set_uuid0
, sb
->set_uuid1
,
272 sb
->set_uuid2
, sb
->set_uuid3
);
274 printf("MD_UUID=%08x\n", sb
->set_uuid0
);
275 printf("MD_UPDATE_TIME=%llu\n",
276 __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
);
277 printf("MD_EVENTS=%llu\n",
278 ((unsigned long long)sb
->events_hi
<< 32)
282 static int copy_metadata0(struct supertype
*st
, int from
, int to
)
284 /* Read 64K from the appropriate offset of 'from'
285 * and if it looks a little like a 0.90 superblock,
286 * write it to the same offset of 'to'
289 unsigned long long dsize
, offset
;
290 const int bufsize
= 64*1024;
293 if (posix_memalign(&buf
, 4096, bufsize
) != 0)
296 if (!get_dev_size(from
, NULL
, &dsize
))
299 if (dsize
< MD_RESERVED_SECTORS
*512)
302 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
306 if (lseek64(from
, offset
, 0) < 0LL)
308 if (read(from
, buf
, bufsize
) != bufsize
)
311 if (lseek64(to
, offset
, 0) < 0LL)
314 if (super
->md_magic
!= MD_SB_MAGIC
||
315 super
->major_version
!= 0 ||
316 calc_sb0_csum(super
) != super
->sb_csum
)
318 if (write(to
, buf
, bufsize
) != bufsize
)
327 static void detail_super0(struct supertype
*st
, char *homehost
)
329 mdp_super_t
*sb
= st
->sb
;
331 if (sb
->minor_version
>= 90)
332 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
333 sb
->set_uuid2
, sb
->set_uuid3
);
335 printf("%08x", sb
->set_uuid0
);
338 void *hash
= sha1_buffer(homehost
,
341 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
342 printf(" (local to host %s)", homehost
);
344 printf("\n Events : %d.%d\n\n", sb
->events_hi
, sb
->events_lo
);
347 static void brief_detail_super0(struct supertype
*st
)
349 mdp_super_t
*sb
= st
->sb
;
351 if (sb
->minor_version
>= 90)
352 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
353 sb
->set_uuid2
, sb
->set_uuid3
);
355 printf("%08x", sb
->set_uuid0
);
359 static int match_home0(struct supertype
*st
, char *homehost
)
361 mdp_super_t
*sb
= st
->sb
;
367 hash
= sha1_buffer(homehost
,
371 return (memcmp(&sb
->set_uuid2
, hash
, 8)==0);
374 static void uuid_from_super0(struct supertype
*st
, int uuid
[4])
376 mdp_super_t
*super
= st
->sb
;
377 uuid
[0] = super
->set_uuid0
;
378 if (super
->minor_version
>= 90) {
379 uuid
[1] = super
->set_uuid1
;
380 uuid
[2] = super
->set_uuid2
;
381 uuid
[3] = super
->set_uuid3
;
389 static void getinfo_super0(struct supertype
*st
, struct mdinfo
*info
, char *map
)
391 mdp_super_t
*sb
= st
->sb
;
394 int map_disks
= info
->array
.raid_disks
;
396 memset(info
, 0, sizeof(*info
));
397 info
->array
.major_version
= sb
->major_version
;
398 info
->array
.minor_version
= sb
->minor_version
;
399 info
->array
.patch_version
= sb
->patch_version
;
400 info
->array
.raid_disks
= sb
->raid_disks
;
401 info
->array
.level
= sb
->level
;
402 info
->array
.layout
= sb
->layout
;
403 info
->array
.md_minor
= sb
->md_minor
;
404 info
->array
.ctime
= sb
->ctime
;
405 info
->array
.utime
= sb
->utime
;
406 info
->array
.chunk_size
= sb
->chunk_size
;
407 info
->array
.state
= sb
->state
;
408 info
->component_size
= sb
->size
*2;
410 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
))
411 info
->bitmap_offset
= 8;
413 info
->disk
.state
= sb
->this_disk
.state
;
414 info
->disk
.major
= sb
->this_disk
.major
;
415 info
->disk
.minor
= sb
->this_disk
.minor
;
416 info
->disk
.raid_disk
= sb
->this_disk
.raid_disk
;
417 info
->disk
.number
= sb
->this_disk
.number
;
419 info
->events
= md_event(sb
);
420 info
->data_offset
= 0;
422 sprintf(info
->text_version
, "0.%d", sb
->minor_version
);
423 info
->safe_mode_delay
= 200;
425 uuid_from_super0(st
, info
->uuid
);
427 info
->recovery_start
= MaxSector
;
428 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
429 info
->reshape_active
= 1;
430 info
->reshape_progress
= sb
->reshape_position
;
431 info
->new_level
= sb
->new_level
;
432 info
->delta_disks
= sb
->delta_disks
;
433 info
->new_layout
= sb
->new_layout
;
434 info
->new_chunk
= sb
->new_chunk
;
435 if (info
->delta_disks
< 0)
436 info
->array
.raid_disks
-= info
->delta_disks
;
438 info
->reshape_active
= 0;
440 info
->recovery_blocked
= info
->reshape_active
;
442 sprintf(info
->name
, "%d", sb
->md_minor
);
443 /* work_disks is calculated rather than read directly */
444 for (i
=0; i
< MD_SB_DISKS
; i
++)
445 if ((sb
->disks
[i
].state
& (1<<MD_DISK_SYNC
)) &&
446 (sb
->disks
[i
].raid_disk
< (unsigned)info
->array
.raid_disks
) &&
447 (sb
->disks
[i
].state
& (1<<MD_DISK_ACTIVE
)) &&
448 !(sb
->disks
[i
].state
& (1<<MD_DISK_FAULTY
))) {
450 if (map
&& i
< map_disks
)
452 } else if (map
&& i
< map_disks
)
454 info
->array
.working_disks
= working
;
457 static struct mdinfo
*container_content0(struct supertype
*st
, char *subarray
)
464 info
= xmalloc(sizeof(*info
));
465 getinfo_super0(st
, info
, NULL
);
469 static int update_super0(struct supertype
*st
, struct mdinfo
*info
,
471 char *devname
, int verbose
,
472 int uuid_set
, char *homehost
)
474 /* NOTE: for 'assemble' and 'force' we need to return non-zero
475 * if any change was made. For others, the return value is
480 mdp_super_t
*sb
= st
->sb
;
482 if (strcmp(update
, "homehost") == 0 &&
484 /* note that 'homehost' is special as it is really
489 info
->uuid
[0] = sb
->set_uuid0
;
490 info
->uuid
[1] = sb
->set_uuid1
;
493 if (strcmp(update
, "sparc2.2")==0 ) {
494 /* 2.2 sparc put the events in the wrong place
495 * So we copy the tail of the superblock
496 * up 4 bytes before continuing
498 __u32
*sb32
= (__u32
*)sb
;
499 memcpy(sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7,
500 sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7+1,
501 (MD_SB_WORDS
- (MD_SB_GENERIC_CONSTANT_WORDS
+7+1))*4);
503 pr_err("adjusting superblock of %s for 2.2/sparc compatibility.\n",
505 } else if (strcmp(update
, "super-minor") ==0) {
506 sb
->md_minor
= info
->array
.md_minor
;
508 pr_err("updating superblock of %s with minor number %d\n",
509 devname
, info
->array
.md_minor
);
510 } else if (strcmp(update
, "summaries") == 0) {
512 /* set nr_disks, active_disks, working_disks,
513 * failed_disks, spare_disks based on disks[]
514 * array in superblock.
515 * Also make sure extra slots aren't 'failed'
517 sb
->nr_disks
= sb
->active_disks
=
518 sb
->working_disks
= sb
->failed_disks
=
520 for (i
=0; i
< MD_SB_DISKS
; i
++)
521 if (sb
->disks
[i
].major
||
522 sb
->disks
[i
].minor
) {
523 int state
= sb
->disks
[i
].state
;
524 if (state
& (1<<MD_DISK_REMOVED
))
527 if (state
& (1<<MD_DISK_ACTIVE
))
529 if (state
& (1<<MD_DISK_FAULTY
))
535 } else if (i
>= sb
->raid_disks
&& sb
->disks
[i
].number
== 0)
536 sb
->disks
[i
].state
= 0;
537 } else if (strcmp(update
, "force-one")==0) {
538 /* Not enough devices for a working array, so
539 * bring this one up-to-date.
541 __u32 ehi
= sb
->events_hi
, elo
= sb
->events_lo
;
542 sb
->events_hi
= (info
->events
>>32) & 0xFFFFFFFF;
543 sb
->events_lo
= (info
->events
) & 0xFFFFFFFF;
544 if (sb
->events_hi
!= ehi
||
545 sb
->events_lo
!= elo
)
547 } else if (strcmp(update
, "force-array")==0) {
548 /* degraded array and 'force' requested, so
549 * maybe need to mark it 'clean'
551 if ((sb
->level
== 5 || sb
->level
== 4 || sb
->level
== 6) &&
552 (sb
->state
& (1 << MD_SB_CLEAN
)) == 0) {
553 /* need to force clean */
554 sb
->state
|= (1 << MD_SB_CLEAN
);
557 } else if (strcmp(update
, "assemble")==0) {
558 int d
= info
->disk
.number
;
559 int wonly
= sb
->disks
[d
].state
& (1<<MD_DISK_WRITEMOSTLY
);
560 int mask
= (1<<MD_DISK_WRITEMOSTLY
);
562 if (sb
->minor_version
>= 91)
563 /* During reshape we don't insist on everything
564 * being marked 'sync'
566 add
= (1<<MD_DISK_SYNC
);
567 if (((sb
->disks
[d
].state
& ~mask
) | add
)
568 != (unsigned)info
->disk
.state
) {
569 sb
->disks
[d
].state
= info
->disk
.state
| wonly
;
572 if (info
->reshape_active
&&
573 sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0 &&
574 info
->delta_disks
>= 0 &&
575 info
->reshape_progress
< sb
->reshape_position
) {
576 sb
->reshape_position
= info
->reshape_progress
;
579 if (info
->reshape_active
&&
580 sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0 &&
581 info
->delta_disks
< 0 &&
582 info
->reshape_progress
> sb
->reshape_position
) {
583 sb
->reshape_position
= info
->reshape_progress
;
586 } else if (strcmp(update
, "linear-grow-new") == 0) {
587 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
588 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
589 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
590 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
591 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
592 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
593 sb
->this_disk
= sb
->disks
[info
->disk
.number
];
594 } else if (strcmp(update
, "linear-grow-update") == 0) {
595 sb
->raid_disks
= info
->array
.raid_disks
;
596 sb
->nr_disks
= info
->array
.nr_disks
;
597 sb
->active_disks
= info
->array
.active_disks
;
598 sb
->working_disks
= info
->array
.working_disks
;
599 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
600 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
601 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
602 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
603 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
604 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
605 } else if (strcmp(update
, "resync") == 0) {
606 /* make sure resync happens */
607 sb
->state
&= ~(1<<MD_SB_CLEAN
);
609 } else if (strcmp(update
, "uuid") == 0) {
610 if (!uuid_set
&& homehost
) {
612 char *hash
= sha1_buffer(homehost
,
615 memcpy(info
->uuid
+2, hash
, 8);
617 sb
->set_uuid0
= info
->uuid
[0];
618 sb
->set_uuid1
= info
->uuid
[1];
619 sb
->set_uuid2
= info
->uuid
[2];
620 sb
->set_uuid3
= info
->uuid
[3];
621 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
622 struct bitmap_super_s
*bm
;
623 bm
= (struct bitmap_super_s
*)(sb
+1);
624 uuid_from_super0(st
, uuid
);
625 memcpy(bm
->uuid
, uuid
, 16);
627 } else if (strcmp(update
, "metadata") == 0) {
628 /* Create some v1.0 metadata to match ours but make the
629 * ctime bigger. Also update info->array.*_version.
630 * We need to arrange that store_super writes out
632 * Not permitted for unclean array, or array with
635 if (info
->bitmap_offset
) {
636 pr_err("Cannot update metadata when bitmap is present\n");
638 } else if (info
->array
.state
!= 1) {
639 pr_err("Cannot update metadata on unclean array\n");
642 info
->array
.major_version
= 1;
643 info
->array
.minor_version
= 0;
644 uuid_from_super0(st
, info
->uuid
);
645 st
->other
= super1_make_v0(st
, info
, st
->sb
);
647 } else if (strcmp(update
, "revert-reshape") == 0) {
649 if (sb
->minor_version
<= 90)
650 pr_err("No active reshape to revert on %s\n",
652 else if (sb
->delta_disks
== 0)
653 pr_err("%s: Can on revert reshape which changes number of devices\n",
658 sb
->raid_disks
-= sb
->delta_disks
;
659 sb
->delta_disks
= -sb
->delta_disks
;
661 tmp
= sb
->new_layout
;
662 sb
->new_layout
= sb
->layout
;
666 sb
->new_chunk
= sb
->chunk_size
;
667 sb
->chunk_size
= tmp
;
669 } else if (strcmp(update
, "no-bitmap") == 0) {
670 sb
->state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
671 } else if (strcmp(update
, "_reshape_progress")==0)
672 sb
->reshape_position
= info
->reshape_progress
;
673 else if (strcmp(update
, "writemostly")==0)
674 sb
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
675 else if (strcmp(update
, "readwrite")==0)
676 sb
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
680 sb
->sb_csum
= calc_sb0_csum(sb
);
685 * For verion-0 superblock, the homehost is 'stored' in the
686 * uuid. 8 bytes for a hash of the host leaving 8 bytes
687 * of random material.
688 * We use the first 8 bytes (64bits) of the sha1 of the
692 static int init_super0(struct supertype
*st
, mdu_array_info_t
*info
,
693 unsigned long long size
, char *ignored_name
, char *homehost
,
694 int *uuid
, unsigned long long data_offset
)
699 if (data_offset
!= INVALID_SECTORS
) {
700 pr_err("data-offset not support for 0.90\n");
704 if (posix_memalign((void**)&sb
, 4096,
705 MD_SB_BYTES
+ ROUND_UP(sizeof(bitmap_super_t
), 4096)) != 0) {
706 pr_err("%s could not allocate superblock\n", __func__
);
709 memset(sb
, 0, MD_SB_BYTES
+ sizeof(bitmap_super_t
));
713 /* zeroing the superblock */
717 spares
= info
->working_disks
- info
->active_disks
;
718 if (info
->raid_disks
+ spares
> MD_SB_DISKS
) {
719 pr_err("too many devices requested: %d+%d > %d\n",
720 info
->raid_disks
, spares
, MD_SB_DISKS
);
724 sb
->md_magic
= MD_SB_MAGIC
;
725 sb
->major_version
= 0;
726 sb
->minor_version
= 90;
727 sb
->patch_version
= 0;
728 sb
->gvalid_words
= 0; /* ignored */
730 sb
->level
= info
->level
;
732 if (size
!= (unsigned long long)sb
->size
)
734 sb
->nr_disks
= info
->nr_disks
;
735 sb
->raid_disks
= info
->raid_disks
;
736 sb
->md_minor
= info
->md_minor
;
737 sb
->not_persistent
= 0;
739 sb
->set_uuid0
= uuid
[0];
740 sb
->set_uuid1
= uuid
[1];
741 sb
->set_uuid2
= uuid
[2];
742 sb
->set_uuid3
= uuid
[3];
744 int rfd
= open("/dev/urandom", O_RDONLY
);
745 if (rfd
< 0 || read(rfd
, &sb
->set_uuid0
, 4) != 4)
746 sb
->set_uuid0
= random();
747 if (rfd
< 0 || read(rfd
, &sb
->set_uuid1
, 12) != 12) {
748 sb
->set_uuid1
= random();
749 sb
->set_uuid2
= random();
750 sb
->set_uuid3
= random();
755 if (homehost
&& !uuid
) {
757 char *hash
= sha1_buffer(homehost
,
760 memcpy(&sb
->set_uuid2
, hash
, 8);
763 sb
->utime
= sb
->ctime
;
764 sb
->state
= info
->state
;
765 sb
->active_disks
= info
->active_disks
;
766 sb
->working_disks
= info
->working_disks
;
767 sb
->failed_disks
= info
->failed_disks
;
768 sb
->spare_disks
= info
->spare_disks
;
772 sb
->layout
= info
->layout
;
773 sb
->chunk_size
= info
->chunk_size
;
781 mdu_disk_info_t disk
;
782 struct devinfo
*next
;
786 /* Add a device to the superblock being created */
787 static int add_to_super0(struct supertype
*st
, mdu_disk_info_t
*dinfo
,
788 int fd
, char *devname
, unsigned long long data_offset
)
790 mdp_super_t
*sb
= st
->sb
;
791 mdp_disk_t
*dk
= &sb
->disks
[dinfo
->number
];
792 struct devinfo
*di
, **dip
;
794 dk
->number
= dinfo
->number
;
795 dk
->major
= dinfo
->major
;
796 dk
->minor
= dinfo
->minor
;
797 dk
->raid_disk
= dinfo
->raid_disk
;
798 dk
->state
= dinfo
->state
;
799 /* In case our source disk was writemostly, don't copy that bit */
800 dk
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
802 sb
->this_disk
= sb
->disks
[dinfo
->number
];
803 sb
->sb_csum
= calc_sb0_csum(sb
);
805 dip
= (struct devinfo
**)&st
->info
;
808 di
= xmalloc(sizeof(struct devinfo
));
810 di
->devname
= devname
;
819 static int store_super0(struct supertype
*st
, int fd
)
821 unsigned long long dsize
;
822 unsigned long long offset
;
823 mdp_super_t
*super
= st
->sb
;
825 if (!get_dev_size(fd
, NULL
, &dsize
))
828 if (dsize
< MD_RESERVED_SECTORS
*512)
832 /* Writing out v1.0 metadata for --update=metadata */
835 offset
= dsize
/512 - 8*2;
838 if (lseek64(fd
, offset
, 0)< 0LL)
840 else if (write(fd
, st
->other
, 1024) != 1024)
849 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
853 if (lseek64(fd
, offset
, 0)< 0LL)
856 if (write(fd
, super
, sizeof(*super
)) != sizeof(*super
))
859 if (super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
860 struct bitmap_super_s
* bm
= (struct bitmap_super_s
*)(super
+1);
861 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
)
862 if (write(fd
, bm
, ROUND_UP(sizeof(*bm
),4096)) !=
863 ROUND_UP(sizeof(*bm
),4096))
872 static int write_init_super0(struct supertype
*st
)
874 mdp_super_t
*sb
= st
->sb
;
878 for (di
= st
->info
; di
&& ! rv
; di
= di
->next
) {
880 if (di
->disk
.state
== 1)
884 while (Kill(di
->devname
, NULL
, 0, -1, 1) == 0)
887 sb
->disks
[di
->disk
.number
].state
&= ~(1<<MD_DISK_FAULTY
);
889 sb
->this_disk
= sb
->disks
[di
->disk
.number
];
890 sb
->sb_csum
= calc_sb0_csum(sb
);
891 rv
= store_super0(st
, di
->fd
);
893 if (rv
== 0 && (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)))
894 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
897 pr_err("failed to write superblock to %s\n",
904 static int compare_super0(struct supertype
*st
, struct supertype
*tst
)
908 * 0 same, or first was empty, and second was copied
909 * 1 second had wrong number
913 mdp_super_t
*first
= st
->sb
;
914 mdp_super_t
*second
= tst
->sb
;
915 int uuid1
[4], uuid2
[4];
917 if (second
->md_magic
!= MD_SB_MAGIC
)
920 if (posix_memalign((void**)&first
, 4096,
922 ROUND_UP(sizeof(struct bitmap_super_s
), 4096)) != 0) {
923 pr_err("%s could not allocate superblock\n", __func__
);
926 memcpy(first
, second
, MD_SB_BYTES
+ sizeof(struct bitmap_super_s
));
931 uuid_from_super0(st
, uuid1
);
932 uuid_from_super0(tst
, uuid2
);
933 if (!same_uuid(uuid1
, uuid2
, 0))
935 if (first
->major_version
!= second
->major_version
||
936 first
->minor_version
!= second
->minor_version
||
937 first
->patch_version
!= second
->patch_version
||
938 first
->gvalid_words
!= second
->gvalid_words
||
939 first
->ctime
!= second
->ctime
||
940 first
->level
!= second
->level
||
941 first
->size
!= second
->size
||
942 first
->raid_disks
!= second
->raid_disks
)
948 static void free_super0(struct supertype
*st
);
950 static int load_super0(struct supertype
*st
, int fd
, char *devname
)
952 /* try to read in the superblock
955 * 1 on cannot get superblock
956 * 2 on superblock meaningless
958 unsigned long long dsize
;
959 unsigned long long offset
;
962 struct bitmap_super_s
*bsb
;
966 if (!get_dev_size(fd
, devname
, &dsize
))
969 if (dsize
< MD_RESERVED_SECTORS
*512) {
971 pr_err("%s is too small for md: size is %llu sectors.\n",
977 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
981 if (lseek64(fd
, offset
, 0)< 0LL) {
983 pr_err("Cannot seek to superblock on %s: %s\n",
984 devname
, strerror(errno
));
988 if (posix_memalign((void**)&super
, 4096,
990 ROUND_UP(sizeof(bitmap_super_t
), 4096)) != 0) {
991 pr_err("%s could not allocate superblock\n", __func__
);
995 if (read(fd
, super
, sizeof(*super
)) != MD_SB_BYTES
) {
997 pr_err("Cannot read superblock on %s\n",
1003 if (st
->ss
&& st
->minor_version
== 9)
1004 super0_swap_endian(super
);
1006 if (super
->md_magic
!= MD_SB_MAGIC
) {
1008 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
1009 devname
, MD_SB_MAGIC
, super
->md_magic
);
1014 if (super
->major_version
!= 0) {
1016 pr_err("Cannot interpret superblock on %s - version is %d\n",
1017 devname
, super
->major_version
);
1023 if (st
->ss
== NULL
) {
1025 st
->minor_version
= super
->minor_version
;
1026 st
->max_devs
= MD_SB_DISKS
;
1030 /* Now check on the bitmap superblock */
1031 if ((super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) == 0)
1033 /* Read the bitmap superblock and make sure it looks
1034 * valid. If it doesn't clear the bit. An --assemble --force
1035 * should get that written out.
1037 if (read(fd
, super
+1, ROUND_UP(sizeof(struct bitmap_super_s
),4096))
1038 != ROUND_UP(sizeof(struct bitmap_super_s
),4096))
1041 uuid_from_super0(st
, uuid
);
1042 bsb
= (struct bitmap_super_s
*)(super
+1);
1043 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
1044 memcmp(bsb
->uuid
, uuid
, 16) != 0)
1049 super
->state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
1054 static struct supertype
*match_metadata_desc0(char *arg
)
1056 struct supertype
*st
= xcalloc(1, sizeof(*st
));
1058 st
->container_devnm
[0] = 0;
1061 st
->minor_version
= 90;
1062 st
->max_devs
= MD_SB_DISKS
;
1064 /* we sometimes get 00.90 */
1065 while (arg
[0] == '0' && arg
[1] == '0')
1067 if (strcmp(arg
, "0") == 0 ||
1068 #ifdef DEFAULT_OLD_METADATA /* ifndef in super1.c */
1069 strcmp(arg
, "default") == 0 ||
1070 #endif /* DEFAULT_OLD_METADATA */
1071 strcmp(arg
, "0.90") == 0 ||
1072 strcmp(arg
, "") == 0 /* no metadata - i.e. non_persistent */
1076 st
->minor_version
= 91; /* reshape in progress */
1077 if (strcmp(arg
, "0.91") == 0) /* For dup_super support */
1080 st
->minor_version
= 9; /* flag for 'byte-swapped' */
1081 if (strcmp(arg
, "0.swap")==0 ||
1082 strcmp(arg
, "0.9") == 0) /* For dup_super support */
1089 static __u64
avail_size0(struct supertype
*st
, __u64 devsize
,
1090 unsigned long long data_offset
)
1092 if (data_offset
!= 0 && data_offset
!= INVALID_SECTORS
)
1094 if (devsize
< MD_RESERVED_SECTORS
)
1096 return MD_NEW_SIZE_SECTORS(devsize
);
1099 static int add_internal_bitmap0(struct supertype
*st
, int *chunkp
,
1100 int delay
, int write_behind
,
1101 unsigned long long size
, int may_change
,
1105 * The bitmap comes immediately after the superblock and must be 60K in size
1106 * at most. The default size is between 30K and 60K
1108 * size is in sectors, chunk is in bytes !!!
1110 unsigned long long bits
;
1111 unsigned long long max_bits
= (60*1024 - sizeof(bitmap_super_t
))*8;
1112 unsigned long long min_chunk
;
1113 int chunk
= *chunkp
;
1114 mdp_super_t
*sb
= st
->sb
;
1115 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + MD_SB_BYTES
);
1118 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
1119 bits
= (size
* 512) / min_chunk
+ 1;
1120 while (bits
> max_bits
) {
1124 if (chunk
== UnSet
) {
1125 /* A chunk size less than a few Megabytes gives poor
1126 * performance without increasing resync noticeably
1129 if (chunk
< 64*1024*1024)
1130 chunk
= 64*1024*1024;
1131 } else if ((unsigned long long)chunk
< min_chunk
)
1132 return 0; /* chunk size too small */
1134 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1136 memset(bms
, 0, sizeof(*bms
));
1137 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
1138 bms
->version
= __cpu_to_le32(major
);
1139 uuid_from_super0(st
, uuid
);
1140 memcpy(bms
->uuid
, uuid
, 16);
1141 bms
->chunksize
= __cpu_to_le32(chunk
);
1142 bms
->daemon_sleep
= __cpu_to_le32(delay
);
1143 bms
->sync_size
= __cpu_to_le64(size
);
1144 bms
->write_behind
= __cpu_to_le32(write_behind
);
1149 static void locate_bitmap0(struct supertype
*st
, int fd
)
1151 unsigned long long dsize
;
1152 unsigned long long offset
;
1154 if (!get_dev_size(fd
, NULL
, &dsize
))
1157 if (dsize
< MD_RESERVED_SECTORS
*512)
1160 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
1164 offset
+= MD_SB_BYTES
;
1166 lseek64(fd
, offset
, 0);
1169 static int write_bitmap0(struct supertype
*st
, int fd
)
1171 unsigned long long dsize
;
1172 unsigned long long offset
;
1173 mdp_super_t
*sb
= st
->sb
;
1180 if (!get_dev_size(fd
, NULL
, &dsize
))
1183 if (dsize
< MD_RESERVED_SECTORS
*512)
1186 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
1190 if (lseek64(fd
, offset
+ 4096, 0)< 0LL)
1193 if (posix_memalign(&buf
, 4096, 4096))
1196 memset(buf
, 0xff, 4096);
1197 memcpy(buf
, ((char*)sb
)+MD_SB_BYTES
, sizeof(bitmap_super_t
));
1199 while (towrite
> 0) {
1203 n
= write(fd
, buf
, n
);
1208 memset(buf
, 0xff, 4096);
1218 static void free_super0(struct supertype
*st
)
1223 struct devinfo
*di
= st
->info
;
1224 st
->info
= di
->next
;
1233 static int validate_geometry0(struct supertype
*st
, int level
,
1234 int layout
, int raiddisks
,
1235 int *chunk
, unsigned long long size
,
1236 unsigned long long data_offset
,
1237 char *subdev
, unsigned long long *freesize
,
1240 unsigned long long ldsize
;
1242 unsigned int tbmax
= 4;
1244 /* prior to linux 3.1, a but limits usable device size to 2TB.
1245 * It was introduced in 2.6.29, but we won't worry about that detail
1247 if (get_linux_version() < 3001000)
1250 if (level
== LEVEL_CONTAINER
) {
1252 pr_err("0.90 metadata does not support containers\n");
1255 if (raiddisks
> MD_SB_DISKS
) {
1257 pr_err("0.90 metadata supports at most %d devices per array\n",
1261 if (size
>= tbmax
* 2ULL*1024*1024*1024) {
1263 pr_err("0.90 metadata supports at most "
1264 "%d terabytes per device\n", tbmax
);
1267 if (chunk
&& *chunk
== UnSet
)
1268 *chunk
= DEFAULT_CHUNK
;
1273 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
1276 pr_err("super0.90 cannot open %s: %s\n",
1277 subdev
, strerror(errno
));
1281 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
1287 if (ldsize
< MD_RESERVED_SECTORS
* 512)
1289 *freesize
= MD_NEW_SIZE_SECTORS(ldsize
>> 9);
1292 #endif /* MDASSEMBLE */
1294 struct superswitch super0
= {
1296 .examine_super
= examine_super0
,
1297 .brief_examine_super
= brief_examine_super0
,
1298 .export_examine_super
= export_examine_super0
,
1299 .detail_super
= detail_super0
,
1300 .brief_detail_super
= brief_detail_super0
,
1301 .write_init_super
= write_init_super0
,
1302 .validate_geometry
= validate_geometry0
,
1303 .add_to_super
= add_to_super0
,
1304 .copy_metadata
= copy_metadata0
,
1306 .match_home
= match_home0
,
1307 .uuid_from_super
= uuid_from_super0
,
1308 .getinfo_super
= getinfo_super0
,
1309 .container_content
= container_content0
,
1310 .update_super
= update_super0
,
1311 .init_super
= init_super0
,
1312 .store_super
= store_super0
,
1313 .compare_super
= compare_super0
,
1314 .load_super
= load_super0
,
1315 .match_metadata_desc
= match_metadata_desc0
,
1316 .avail_size
= avail_size0
,
1317 .add_internal_bitmap
= add_internal_bitmap0
,
1318 .locate_bitmap
= locate_bitmap0
,
1319 .write_bitmap
= write_bitmap0
,
1320 .free_super
= free_super0
,