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
40 static unsigned long calc_sb0_csum(mdp_super_t
*super
)
42 unsigned long csum
= super
->sb_csum
;
43 unsigned long newcsum
;
45 newcsum
= calc_csum(super
, MD_SB_BYTES
);
46 super
->sb_csum
= csum
;
51 static void super0_swap_endian(struct mdp_superblock_s
*sb
)
53 /* as super0 superblocks are host-endian, it is sometimes
54 * useful to be able to swap the endianness
55 * as (almost) everything is u32's we byte-swap every 4byte
57 * We then also have to swap the events_hi and events_lo
59 char *sbc
= (char *)sb
;
63 for (i
=0; i
< MD_SB_BYTES
; i
+=4) {
72 sb
->events_hi
= sb
->events_lo
;
75 t32
= sb
->cp_events_hi
;
76 sb
->cp_events_hi
= sb
->cp_events_lo
;
77 sb
->cp_events_lo
= t32
;
83 static void examine_super0(struct supertype
*st
, char *homehost
)
85 mdp_super_t
*sb
= st
->sb
;
91 printf(" Magic : %08x\n", sb
->md_magic
);
92 printf(" Version : %d.%02d.%02d\n", sb
->major_version
, sb
->minor_version
,
94 if (sb
->minor_version
>= 90) {
95 printf(" UUID : %08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
96 sb
->set_uuid2
, sb
->set_uuid3
);
99 void *hash
= sha1_buffer(homehost
,
102 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
103 printf(" (local to host %s)", homehost
);
107 printf(" UUID : %08x\n", sb
->set_uuid0
);
109 if (sb
->not_persistent
)
110 printf(" Eedk : not persistent\n");
113 printf(" Creation Time : %.24s\n", ctime(&atime
));
114 c
=map_num(pers
, sb
->level
);
115 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
116 if ((int)sb
->level
> 0) {
117 int ddsks
= 0, ddsks_denom
= 1;
118 printf(" Used Dev Size : %d%s\n", sb
->size
,
119 human_size((long long)sb
->size
<<10));
121 case 1: ddsks
=1;break;
123 case 5: ddsks
= sb
->raid_disks
-1; break;
124 case 6: ddsks
= sb
->raid_disks
-2; break;
125 case 10: ddsks
= sb
->raid_disks
;
126 ddsks_denom
= (sb
->layout
&255) * ((sb
->layout
>>8)&255);
129 long long asize
= sb
->size
;
130 asize
= (asize
<< 10) * ddsks
/ ddsks_denom
;
131 printf(" Array Size : %llu%s\n",
132 asize
>> 10, human_size(asize
));
135 printf(" Raid Devices : %d\n", sb
->raid_disks
);
136 printf(" Total Devices : %d\n", sb
->nr_disks
);
137 printf("Preferred Minor : %d\n", sb
->md_minor
);
139 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
140 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb
->reshape_position
/2, human_size((long long)sb
->reshape_position
<<9));
141 if (sb
->delta_disks
) {
142 printf(" Delta Devices : %d", sb
->delta_disks
);
143 printf(" (%d->%d)\n", sb
->raid_disks
-sb
->delta_disks
, sb
->raid_disks
);
144 if (((int)sb
->delta_disks
) < 0)
145 delta_extra
= - sb
->delta_disks
;
147 if (sb
->new_level
!= sb
->level
) {
148 c
= map_num(pers
, sb
->new_level
);
149 printf(" New Level : %s\n", c
?c
:"-unknown-");
151 if (sb
->new_layout
!= sb
->layout
) {
152 if (sb
->level
== 5) {
153 c
= map_num(r5layout
, sb
->new_layout
);
154 printf(" New Layout : %s\n", c
?c
:"-unknown-");
156 if (sb
->level
== 6) {
157 c
= map_num(r6layout
, sb
->new_layout
);
158 printf(" New Layout : %s\n", c
?c
:"-unknown-");
160 if (sb
->level
== 10) {
161 printf(" New Layout : near=%d, %s=%d\n",
163 (sb
->new_layout
&0x10000)?"offset":"far",
164 (sb
->new_layout
>>8)&255);
167 if (sb
->new_chunk
!= sb
->chunk_size
)
168 printf(" New Chunksize : %d\n", sb
->new_chunk
);
172 printf(" Update Time : %.24s\n", ctime(&atime
));
173 printf(" State : %s\n",
174 (sb
->state
&(1<<MD_SB_CLEAN
))?"clean":"active");
175 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
))
176 printf("Internal Bitmap : present\n");
177 printf(" Active Devices : %d\n", sb
->active_disks
);
178 printf("Working Devices : %d\n", sb
->working_disks
);
179 printf(" Failed Devices : %d\n", sb
->failed_disks
);
180 printf(" Spare Devices : %d\n", sb
->spare_disks
);
181 if (calc_sb0_csum(sb
) == sb
->sb_csum
)
182 printf(" Checksum : %x - correct\n", sb
->sb_csum
);
184 printf(" Checksum : %x - expected %lx\n", sb
->sb_csum
, calc_sb0_csum(sb
));
185 printf(" Events : %llu\n",
186 ((unsigned long long)sb
->events_hi
<< 32)
189 if (sb
->level
== 5) {
190 c
= map_num(r5layout
, sb
->layout
);
191 printf(" Layout : %s\n", c
?c
:"-unknown-");
193 if (sb
->level
== 6) {
194 c
= map_num(r6layout
, sb
->layout
);
195 printf(" Layout : %s\n", c
?c
:"-unknown-");
197 if (sb
->level
== 10) {
199 print_r10_layout(sb
->layout
);
208 printf(" Chunk Size : %dK\n", sb
->chunk_size
/1024);
211 printf(" Rounding : %dK\n", sb
->chunk_size
/1024);
216 printf(" Number Major Minor RaidDevice State\n");
217 for (d
= -1; d
<(signed int)(sb
->raid_disks
+delta_extra
+ sb
->spare_disks
); d
++) {
222 if (d
>=0) dp
= &sb
->disks
[d
];
223 else dp
= &sb
->this_disk
;
224 snprintf(nb
, sizeof(nb
), "%4d", d
);
225 printf("%4s %5d %5d %5d %5d ", d
< 0 ? "this" : nb
,
226 dp
->number
, dp
->major
, dp
->minor
, dp
->raid_disk
);
227 wonly
= dp
->state
& (1<<MD_DISK_WRITEMOSTLY
);
228 dp
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
229 if (dp
->state
& (1<<MD_DISK_FAULTY
)) printf(" faulty");
230 if (dp
->state
& (1<<MD_DISK_ACTIVE
)) printf(" active");
231 if (dp
->state
& (1<<MD_DISK_SYNC
)) printf(" sync");
232 if (dp
->state
& (1<<MD_DISK_REMOVED
)) printf(" removed");
233 if (wonly
) printf(" write-mostly");
234 if (dp
->state
== 0) printf(" spare");
235 if ((dv
=map_dev(dp
->major
, dp
->minor
, 0)))
238 if (d
== -1) printf("\n");
242 static void brief_examine_super0(struct supertype
*st
, int verbose
)
244 mdp_super_t
*sb
= st
->sb
;
245 char *c
=map_num(pers
, sb
->level
);
248 sprintf(devname
, "/dev/md%d", sb
->md_minor
);
251 printf("ARRAY %s level=%s num-devices=%d",
253 c
?c
:"-unknown-", sb
->raid_disks
);
255 printf("ARRAY %s", devname
);
257 if (sb
->minor_version
>= 90)
258 printf(" UUID=%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
259 sb
->set_uuid2
, sb
->set_uuid3
);
261 printf(" UUID=%08x", sb
->set_uuid0
);
265 static void export_examine_super0(struct supertype
*st
)
267 mdp_super_t
*sb
= st
->sb
;
269 printf("MD_LEVEL=%s\n", map_num(pers
, sb
->level
));
270 printf("MD_DEVICES=%d\n", sb
->raid_disks
);
271 if (sb
->minor_version
>= 90)
272 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
273 sb
->set_uuid0
, sb
->set_uuid1
,
274 sb
->set_uuid2
, sb
->set_uuid3
);
276 printf("MD_UUID=%08x\n", sb
->set_uuid0
);
277 printf("MD_UPDATE_TIME=%llu\n",
278 __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
);
279 printf("MD_EVENTS=%llu\n",
280 ((unsigned long long)sb
->events_hi
<< 32)
284 static void detail_super0(struct supertype
*st
, char *homehost
)
286 mdp_super_t
*sb
= st
->sb
;
288 if (sb
->minor_version
>= 90)
289 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
290 sb
->set_uuid2
, sb
->set_uuid3
);
292 printf("%08x", sb
->set_uuid0
);
295 void *hash
= sha1_buffer(homehost
,
298 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
299 printf(" (local to host %s)", homehost
);
301 printf("\n Events : %d.%d\n\n", sb
->events_hi
, sb
->events_lo
);
304 static void brief_detail_super0(struct supertype
*st
)
306 mdp_super_t
*sb
= st
->sb
;
308 if (sb
->minor_version
>= 90)
309 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
310 sb
->set_uuid2
, sb
->set_uuid3
);
312 printf("%08x", sb
->set_uuid0
);
316 static int match_home0(struct supertype
*st
, char *homehost
)
318 mdp_super_t
*sb
= st
->sb
;
324 hash
= sha1_buffer(homehost
,
328 return (memcmp(&sb
->set_uuid2
, hash
, 8)==0);
331 static void uuid_from_super0(struct supertype
*st
, int uuid
[4])
333 mdp_super_t
*super
= st
->sb
;
334 uuid
[0] = super
->set_uuid0
;
335 if (super
->minor_version
>= 90) {
336 uuid
[1] = super
->set_uuid1
;
337 uuid
[2] = super
->set_uuid2
;
338 uuid
[3] = super
->set_uuid3
;
346 static void getinfo_super0(struct supertype
*st
, struct mdinfo
*info
, char *map
)
348 mdp_super_t
*sb
= st
->sb
;
351 int map_disks
= info
->array
.raid_disks
;
353 memset(info
, 0, sizeof(*info
));
354 info
->array
.major_version
= sb
->major_version
;
355 info
->array
.minor_version
= sb
->minor_version
;
356 info
->array
.patch_version
= sb
->patch_version
;
357 info
->array
.raid_disks
= sb
->raid_disks
;
358 info
->array
.level
= sb
->level
;
359 info
->array
.layout
= sb
->layout
;
360 info
->array
.md_minor
= sb
->md_minor
;
361 info
->array
.ctime
= sb
->ctime
;
362 info
->array
.utime
= sb
->utime
;
363 info
->array
.chunk_size
= sb
->chunk_size
;
364 info
->array
.state
= sb
->state
;
365 info
->component_size
= sb
->size
*2;
367 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
))
368 info
->bitmap_offset
= 8;
370 info
->disk
.state
= sb
->this_disk
.state
;
371 info
->disk
.major
= sb
->this_disk
.major
;
372 info
->disk
.minor
= sb
->this_disk
.minor
;
373 info
->disk
.raid_disk
= sb
->this_disk
.raid_disk
;
374 info
->disk
.number
= sb
->this_disk
.number
;
376 info
->events
= md_event(sb
);
377 info
->data_offset
= 0;
379 sprintf(info
->text_version
, "0.%d", sb
->minor_version
);
380 info
->safe_mode_delay
= 200;
382 uuid_from_super0(st
, info
->uuid
);
384 info
->recovery_start
= MaxSector
;
385 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
386 info
->reshape_active
= 1;
387 info
->reshape_progress
= sb
->reshape_position
;
388 info
->new_level
= sb
->new_level
;
389 info
->delta_disks
= sb
->delta_disks
;
390 info
->new_layout
= sb
->new_layout
;
391 info
->new_chunk
= sb
->new_chunk
;
392 if (info
->delta_disks
< 0)
393 info
->array
.raid_disks
-= info
->delta_disks
;
395 info
->reshape_active
= 0;
397 info
->recovery_blocked
= info
->reshape_active
;
399 sprintf(info
->name
, "%d", sb
->md_minor
);
400 /* work_disks is calculated rather than read directly */
401 for (i
=0; i
< MD_SB_DISKS
; i
++)
402 if ((sb
->disks
[i
].state
& (1<<MD_DISK_SYNC
)) &&
403 (sb
->disks
[i
].raid_disk
< (unsigned)info
->array
.raid_disks
) &&
404 (sb
->disks
[i
].state
& (1<<MD_DISK_ACTIVE
)) &&
405 !(sb
->disks
[i
].state
& (1<<MD_DISK_FAULTY
))) {
407 if (map
&& i
< map_disks
)
409 } else if (map
&& i
< map_disks
)
411 info
->array
.working_disks
= working
;
414 static struct mdinfo
*container_content0(struct supertype
*st
, char *subarray
)
421 info
= xmalloc(sizeof(*info
));
422 getinfo_super0(st
, info
, NULL
);
426 static int update_super0(struct supertype
*st
, struct mdinfo
*info
,
428 char *devname
, int verbose
,
429 int uuid_set
, char *homehost
)
431 /* NOTE: for 'assemble' and 'force' we need to return non-zero
432 * if any change was made. For others, the return value is
437 mdp_super_t
*sb
= st
->sb
;
438 if (strcmp(update
, "sparc2.2")==0 ) {
439 /* 2.2 sparc put the events in the wrong place
440 * So we copy the tail of the superblock
441 * up 4 bytes before continuing
443 __u32
*sb32
= (__u32
*)sb
;
444 memcpy(sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7,
445 sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7+1,
446 (MD_SB_WORDS
- (MD_SB_GENERIC_CONSTANT_WORDS
+7+1))*4);
448 pr_err("adjusting superblock of %s for 2.2/sparc compatability.\n",
450 } else if (strcmp(update
, "super-minor") ==0) {
451 sb
->md_minor
= info
->array
.md_minor
;
453 pr_err("updating superblock of %s with minor number %d\n",
454 devname
, info
->array
.md_minor
);
455 } else if (strcmp(update
, "summaries") == 0) {
457 /* set nr_disks, active_disks, working_disks,
458 * failed_disks, spare_disks based on disks[]
459 * array in superblock.
460 * Also make sure extra slots aren't 'failed'
462 sb
->nr_disks
= sb
->active_disks
=
463 sb
->working_disks
= sb
->failed_disks
=
465 for (i
=0; i
< MD_SB_DISKS
; i
++)
466 if (sb
->disks
[i
].major
||
467 sb
->disks
[i
].minor
) {
468 int state
= sb
->disks
[i
].state
;
469 if (state
& (1<<MD_DISK_REMOVED
))
472 if (state
& (1<<MD_DISK_ACTIVE
))
474 if (state
& (1<<MD_DISK_FAULTY
))
480 } else if (i
>= sb
->raid_disks
&& sb
->disks
[i
].number
== 0)
481 sb
->disks
[i
].state
= 0;
482 } else if (strcmp(update
, "force-one")==0) {
483 /* Not enough devices for a working array, so
484 * bring this one up-to-date.
486 __u32 ehi
= sb
->events_hi
, elo
= sb
->events_lo
;
487 sb
->events_hi
= (info
->events
>>32) & 0xFFFFFFFF;
488 sb
->events_lo
= (info
->events
) & 0xFFFFFFFF;
489 if (sb
->events_hi
!= ehi
||
490 sb
->events_lo
!= elo
)
492 } else if (strcmp(update
, "force-array")==0) {
493 /* degraded array and 'force' requested, so
494 * maybe need to mark it 'clean'
496 if ((sb
->level
== 5 || sb
->level
== 4 || sb
->level
== 6) &&
497 (sb
->state
& (1 << MD_SB_CLEAN
)) == 0) {
498 /* need to force clean */
499 sb
->state
|= (1 << MD_SB_CLEAN
);
502 } else if (strcmp(update
, "assemble")==0) {
503 int d
= info
->disk
.number
;
504 int wonly
= sb
->disks
[d
].state
& (1<<MD_DISK_WRITEMOSTLY
);
505 int mask
= (1<<MD_DISK_WRITEMOSTLY
);
507 if (sb
->minor_version
>= 91)
508 /* During reshape we don't insist on everything
509 * being marked 'sync'
511 add
= (1<<MD_DISK_SYNC
);
512 if (((sb
->disks
[d
].state
& ~mask
) | add
)
513 != (unsigned)info
->disk
.state
) {
514 sb
->disks
[d
].state
= info
->disk
.state
| wonly
;
517 if (info
->reshape_active
&&
518 sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0 &&
519 info
->delta_disks
>= 0 &&
520 info
->reshape_progress
< sb
->reshape_position
) {
521 sb
->reshape_position
= info
->reshape_progress
;
524 if (info
->reshape_active
&&
525 sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0 &&
526 info
->delta_disks
< 0 &&
527 info
->reshape_progress
> sb
->reshape_position
) {
528 sb
->reshape_position
= info
->reshape_progress
;
531 } else if (strcmp(update
, "linear-grow-new") == 0) {
532 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
533 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
534 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
535 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
536 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
537 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
538 sb
->this_disk
= sb
->disks
[info
->disk
.number
];
539 } else if (strcmp(update
, "linear-grow-update") == 0) {
540 sb
->raid_disks
= info
->array
.raid_disks
;
541 sb
->nr_disks
= info
->array
.nr_disks
;
542 sb
->active_disks
= info
->array
.active_disks
;
543 sb
->working_disks
= info
->array
.working_disks
;
544 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
545 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
546 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
547 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
548 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
549 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
550 } else if (strcmp(update
, "resync") == 0) {
551 /* make sure resync happens */
552 sb
->state
&= ~(1<<MD_SB_CLEAN
);
554 } else if (strcmp(update
, "homehost") == 0 &&
558 info
->uuid
[0] = sb
->set_uuid0
;
559 info
->uuid
[1] = sb
->set_uuid1
;
560 } else if (strcmp(update
, "uuid") == 0) {
561 if (!uuid_set
&& homehost
) {
563 char *hash
= sha1_buffer(homehost
,
566 memcpy(info
->uuid
+2, hash
, 8);
568 sb
->set_uuid0
= info
->uuid
[0];
569 sb
->set_uuid1
= info
->uuid
[1];
570 sb
->set_uuid2
= info
->uuid
[2];
571 sb
->set_uuid3
= info
->uuid
[3];
572 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
573 struct bitmap_super_s
*bm
;
574 bm
= (struct bitmap_super_s
*)(sb
+1);
575 uuid_from_super0(st
, uuid
);
576 memcpy(bm
->uuid
, uuid
, 16);
578 } else if (strcmp(update
, "no-bitmap") == 0) {
579 sb
->state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
580 } else if (strcmp(update
, "_reshape_progress")==0)
581 sb
->reshape_position
= info
->reshape_progress
;
582 else if (strcmp(update
, "writemostly")==0)
583 sb
->state
|= (1<<MD_DISK_WRITEMOSTLY
);
584 else if (strcmp(update
, "readwrite")==0)
585 sb
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
589 sb
->sb_csum
= calc_sb0_csum(sb
);
594 * For verion-0 superblock, the homehost is 'stored' in the
595 * uuid. 8 bytes for a hash of the host leaving 8 bytes
596 * of random material.
597 * We use the first 8 bytes (64bits) of the sha1 of the
602 static int init_super0(struct supertype
*st
, mdu_array_info_t
*info
,
603 unsigned long long size
, char *ignored_name
, char *homehost
,
609 if (posix_memalign((void**)&sb
, 4096,
610 MD_SB_BYTES
+ ROUND_UP(sizeof(bitmap_super_t
), 4096)) != 0) {
611 pr_err("%s could not allocate superblock\n", __func__
);
614 memset(sb
, 0, MD_SB_BYTES
+ sizeof(bitmap_super_t
));
618 /* zeroing the superblock */
622 spares
= info
->working_disks
- info
->active_disks
;
623 if (info
->raid_disks
+ spares
> MD_SB_DISKS
) {
624 pr_err("too many devices requested: %d+%d > %d\n",
625 info
->raid_disks
, spares
, MD_SB_DISKS
);
629 sb
->md_magic
= MD_SB_MAGIC
;
630 sb
->major_version
= 0;
631 sb
->minor_version
= 90;
632 sb
->patch_version
= 0;
633 sb
->gvalid_words
= 0; /* ignored */
635 sb
->level
= info
->level
;
636 if (size
!= (unsigned long long)info
->size
)
638 sb
->size
= info
->size
;
639 sb
->nr_disks
= info
->nr_disks
;
640 sb
->raid_disks
= info
->raid_disks
;
641 sb
->md_minor
= info
->md_minor
;
642 sb
->not_persistent
= 0;
644 sb
->set_uuid0
= uuid
[0];
645 sb
->set_uuid1
= uuid
[1];
646 sb
->set_uuid2
= uuid
[2];
647 sb
->set_uuid3
= uuid
[3];
649 int rfd
= open("/dev/urandom", O_RDONLY
);
650 if (rfd
< 0 || read(rfd
, &sb
->set_uuid0
, 4) != 4)
651 sb
->set_uuid0
= random();
652 if (rfd
< 0 || read(rfd
, &sb
->set_uuid1
, 12) != 12) {
653 sb
->set_uuid1
= random();
654 sb
->set_uuid2
= random();
655 sb
->set_uuid3
= random();
662 char *hash
= sha1_buffer(homehost
,
665 memcpy(&sb
->set_uuid2
, hash
, 8);
668 sb
->utime
= sb
->ctime
;
669 sb
->state
= info
->state
;
670 sb
->active_disks
= info
->active_disks
;
671 sb
->working_disks
= info
->working_disks
;
672 sb
->failed_disks
= info
->failed_disks
;
673 sb
->spare_disks
= info
->spare_disks
;
677 sb
->layout
= info
->layout
;
678 sb
->chunk_size
= info
->chunk_size
;
686 mdu_disk_info_t disk
;
687 struct devinfo
*next
;
691 /* Add a device to the superblock being created */
692 static int add_to_super0(struct supertype
*st
, mdu_disk_info_t
*dinfo
,
693 int fd
, char *devname
)
695 mdp_super_t
*sb
= st
->sb
;
696 mdp_disk_t
*dk
= &sb
->disks
[dinfo
->number
];
697 struct devinfo
*di
, **dip
;
699 dk
->number
= dinfo
->number
;
700 dk
->major
= dinfo
->major
;
701 dk
->minor
= dinfo
->minor
;
702 dk
->raid_disk
= dinfo
->raid_disk
;
703 dk
->state
= dinfo
->state
;
704 /* In case our source disk was writemostly, don't copy that bit */
705 dk
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
707 sb
->this_disk
= sb
->disks
[dinfo
->number
];
708 sb
->sb_csum
= calc_sb0_csum(sb
);
710 dip
= (struct devinfo
**)&st
->info
;
713 di
= xmalloc(sizeof(struct devinfo
));
715 di
->devname
= devname
;
724 static int store_super0(struct supertype
*st
, int fd
)
726 unsigned long long dsize
;
727 unsigned long long offset
;
728 mdp_super_t
*super
= st
->sb
;
730 if (!get_dev_size(fd
, NULL
, &dsize
))
733 if (dsize
< MD_RESERVED_SECTORS
*512)
736 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
740 if (lseek64(fd
, offset
, 0)< 0LL)
743 if (write(fd
, super
, sizeof(*super
)) != sizeof(*super
))
746 if (super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
747 struct bitmap_super_s
* bm
= (struct bitmap_super_s
*)(super
+1);
748 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
)
749 if (write(fd
, bm
, ROUND_UP(sizeof(*bm
),4096)) !=
750 ROUND_UP(sizeof(*bm
),4096))
759 static int write_init_super0(struct supertype
*st
)
761 mdp_super_t
*sb
= st
->sb
;
765 for (di
= st
->info
; di
&& ! rv
; di
= di
->next
) {
767 if (di
->disk
.state
== 1)
771 while (Kill(di
->devname
, NULL
, 0, -1, 1) == 0)
774 sb
->disks
[di
->disk
.number
].state
&= ~(1<<MD_DISK_FAULTY
);
776 sb
->this_disk
= sb
->disks
[di
->disk
.number
];
777 sb
->sb_csum
= calc_sb0_csum(sb
);
778 rv
= store_super0(st
, di
->fd
);
780 if (rv
== 0 && (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)))
781 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
784 pr_err("failed to write superblock to %s\n",
791 static int compare_super0(struct supertype
*st
, struct supertype
*tst
)
795 * 0 same, or first was empty, and second was copied
796 * 1 second had wrong number
800 mdp_super_t
*first
= st
->sb
;
801 mdp_super_t
*second
= tst
->sb
;
802 int uuid1
[4], uuid2
[4];
804 if (second
->md_magic
!= MD_SB_MAGIC
)
807 if (posix_memalign((void**)&first
, 4096,
809 ROUND_UP(sizeof(struct bitmap_super_s
), 4096)) != 0) {
810 pr_err("%s could not allocate superblock\n", __func__
);
813 memcpy(first
, second
, MD_SB_BYTES
+ sizeof(struct bitmap_super_s
));
818 uuid_from_super0(st
, uuid1
);
819 uuid_from_super0(tst
, uuid2
);
820 if (!same_uuid(uuid1
, uuid2
, 0))
822 if (first
->major_version
!= second
->major_version
||
823 first
->minor_version
!= second
->minor_version
||
824 first
->patch_version
!= second
->patch_version
||
825 first
->gvalid_words
!= second
->gvalid_words
||
826 first
->ctime
!= second
->ctime
||
827 first
->level
!= second
->level
||
828 first
->size
!= second
->size
||
829 first
->raid_disks
!= second
->raid_disks
)
836 static void free_super0(struct supertype
*st
);
838 static int load_super0(struct supertype
*st
, int fd
, char *devname
)
840 /* try to read in the superblock
843 * 1 on cannot get superblock
844 * 2 on superblock meaningless
846 unsigned long long dsize
;
847 unsigned long long offset
;
850 struct bitmap_super_s
*bsb
;
854 if (!get_dev_size(fd
, devname
, &dsize
))
857 if (dsize
< MD_RESERVED_SECTORS
*512) {
859 pr_err("%s is too small for md: size is %llu sectors.\n",
864 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
868 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
870 if (lseek64(fd
, offset
, 0)< 0LL) {
872 pr_err("Cannot seek to superblock on %s: %s\n",
873 devname
, strerror(errno
));
877 if (posix_memalign((void**)&super
, 4096,
879 ROUND_UP(sizeof(bitmap_super_t
), 4096)) != 0) {
880 pr_err("%s could not allocate superblock\n", __func__
);
884 if (read(fd
, super
, sizeof(*super
)) != MD_SB_BYTES
) {
886 pr_err("Cannot read superblock on %s\n",
892 if (st
->ss
&& st
->minor_version
== 9)
893 super0_swap_endian(super
);
895 if (super
->md_magic
!= MD_SB_MAGIC
) {
897 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
898 devname
, MD_SB_MAGIC
, super
->md_magic
);
903 if (super
->major_version
!= 0) {
905 pr_err("Cannot interpret superblock on %s - version is %d\n",
906 devname
, super
->major_version
);
912 if (st
->ss
== NULL
) {
914 st
->minor_version
= super
->minor_version
;
915 st
->max_devs
= MD_SB_DISKS
;
919 /* Now check on the bitmap superblock */
920 if ((super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) == 0)
922 /* Read the bitmap superblock and make sure it looks
923 * valid. If it doesn't clear the bit. An --assemble --force
924 * should get that written out.
926 if (read(fd
, super
+1, ROUND_UP(sizeof(struct bitmap_super_s
),4096))
927 != ROUND_UP(sizeof(struct bitmap_super_s
),4096))
930 uuid_from_super0(st
, uuid
);
931 bsb
= (struct bitmap_super_s
*)(super
+1);
932 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
933 memcmp(bsb
->uuid
, uuid
, 16) != 0)
938 super
->state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
943 static struct supertype
*match_metadata_desc0(char *arg
)
945 struct supertype
*st
= xcalloc(1, sizeof(*st
));
947 st
->container_dev
= NoMdDev
;
950 st
->minor_version
= 90;
951 st
->max_devs
= MD_SB_DISKS
;
953 /* we sometimes get 00.90 */
954 while (arg
[0] == '0' && arg
[1] == '0')
956 if (strcmp(arg
, "0") == 0 ||
957 #ifdef DEFAULT_OLD_METADATA /* ifndef in super1.c */
958 strcmp(arg
, "default") == 0 ||
959 #endif /* DEFAULT_OLD_METADATA */
960 strcmp(arg
, "0.90") == 0 ||
961 strcmp(arg
, "") == 0 /* no metadata - i.e. non_persistent */
965 st
->minor_version
= 91; /* reshape in progress */
966 if (strcmp(arg
, "0.91") == 0) /* For dup_super support */
969 st
->minor_version
= 9; /* flag for 'byte-swapped' */
970 if (strcmp(arg
, "0.swap")==0 ||
971 strcmp(arg
, "0.9") == 0) /* For dup_super support */
978 static __u64
avail_size0(struct supertype
*st
, __u64 devsize
)
980 if (devsize
< MD_RESERVED_SECTORS
)
982 return MD_NEW_SIZE_SECTORS(devsize
);
985 static int add_internal_bitmap0(struct supertype
*st
, int *chunkp
,
986 int delay
, int write_behind
,
987 unsigned long long size
, int may_change
,
991 * The bitmap comes immediately after the superblock and must be 60K in size
992 * at most. The default size is between 30K and 60K
994 * size is in sectors, chunk is in bytes !!!
996 unsigned long long bits
;
997 unsigned long long max_bits
= (60*1024 - sizeof(bitmap_super_t
))*8;
998 unsigned long long min_chunk
;
1000 mdp_super_t
*sb
= st
->sb
;
1001 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + MD_SB_BYTES
);
1005 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
1006 bits
= (size
* 512) / min_chunk
+ 1;
1007 while (bits
> max_bits
) {
1011 if (chunk
== UnSet
) {
1012 /* A chunk size less than a few Megabytes gives poor
1013 * performance without increasing resync noticeably
1016 if (chunk
< 64*1024*1024)
1017 chunk
= 64*1024*1024;
1018 } else if ((unsigned long long)chunk
< min_chunk
)
1019 return 0; /* chunk size too small */
1021 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
1023 memset(bms
, 0, sizeof(*bms
));
1024 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
1025 bms
->version
= __cpu_to_le32(major
);
1026 uuid_from_super0(st
, uuid
);
1027 memcpy(bms
->uuid
, uuid
, 16);
1028 bms
->chunksize
= __cpu_to_le32(chunk
);
1029 bms
->daemon_sleep
= __cpu_to_le32(delay
);
1030 bms
->sync_size
= __cpu_to_le64(size
);
1031 bms
->write_behind
= __cpu_to_le32(write_behind
);
1037 static void locate_bitmap0(struct supertype
*st
, int fd
)
1039 unsigned long long dsize
;
1040 unsigned long long offset
;
1042 if (!get_dev_size(fd
, NULL
, &dsize
))
1045 if (dsize
< MD_RESERVED_SECTORS
*512)
1048 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
1052 offset
+= MD_SB_BYTES
;
1054 lseek64(fd
, offset
, 0);
1057 static int write_bitmap0(struct supertype
*st
, int fd
)
1059 unsigned long long dsize
;
1060 unsigned long long offset
;
1061 mdp_super_t
*sb
= st
->sb
;
1068 if (!get_dev_size(fd
, NULL
, &dsize
))
1071 if (dsize
< MD_RESERVED_SECTORS
*512)
1074 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
1078 if (lseek64(fd
, offset
+ 4096, 0)< 0LL)
1081 if (posix_memalign(&buf
, 4096, 4096))
1084 memset(buf
, 0xff, 4096);
1085 memcpy(buf
, ((char*)sb
)+MD_SB_BYTES
, sizeof(bitmap_super_t
));
1087 while (towrite
> 0) {
1091 n
= write(fd
, buf
, n
);
1096 memset(buf
, 0xff, 4096);
1106 static void free_super0(struct supertype
*st
)
1111 struct devinfo
*di
= st
->info
;
1112 st
->info
= di
->next
;
1121 static int validate_geometry0(struct supertype
*st
, int level
,
1122 int layout
, int raiddisks
,
1123 int *chunk
, unsigned long long size
,
1124 char *subdev
, unsigned long long *freesize
,
1127 unsigned long long ldsize
;
1129 unsigned int tbmax
= 4;
1131 /* prior to linux 3.1, a but limits usable device size to 2TB.
1132 * It was introduced in 2.6.29, but we won't worry about that detail
1134 if (get_linux_version() < 3001000)
1137 if (level
== LEVEL_CONTAINER
) {
1139 pr_err("0.90 metadata does not support containers\n");
1142 if (raiddisks
> MD_SB_DISKS
) {
1144 pr_err("0.90 metadata supports at most %d devices per array\n",
1148 if (size
>= tbmax
* 2ULL*1024*1024*1024) {
1150 pr_err("0.90 metadata supports at most "
1151 "%d terabytes per device\n", tbmax
);
1154 if (chunk
&& *chunk
== UnSet
)
1155 *chunk
= DEFAULT_CHUNK
;
1160 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
1163 pr_err("super0.90 cannot open %s: %s\n",
1164 subdev
, strerror(errno
));
1168 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
1174 if (ldsize
< MD_RESERVED_SECTORS
* 512)
1176 *freesize
= MD_NEW_SIZE_SECTORS(ldsize
>> 9);
1179 #endif /* MDASSEMBLE */
1181 struct superswitch super0
= {
1183 .examine_super
= examine_super0
,
1184 .brief_examine_super
= brief_examine_super0
,
1185 .export_examine_super
= export_examine_super0
,
1186 .detail_super
= detail_super0
,
1187 .brief_detail_super
= brief_detail_super0
,
1188 .write_init_super
= write_init_super0
,
1189 .validate_geometry
= validate_geometry0
,
1190 .add_to_super
= add_to_super0
,
1192 .match_home
= match_home0
,
1193 .uuid_from_super
= uuid_from_super0
,
1194 .getinfo_super
= getinfo_super0
,
1195 .container_content
= container_content0
,
1196 .update_super
= update_super0
,
1197 .init_super
= init_super0
,
1198 .store_super
= store_super0
,
1199 .compare_super
= compare_super0
,
1200 .load_super
= load_super0
,
1201 .match_metadata_desc
= match_metadata_desc0
,
1202 .avail_size
= avail_size0
,
1203 .add_internal_bitmap
= add_internal_bitmap0
,
1204 .locate_bitmap
= locate_bitmap0
,
1205 .write_bitmap
= write_bitmap0
,
1206 .free_super
= free_super0
,