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) {
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
/ (sb
->layout
&255) / ((sb
->layout
>>8)&255);
128 printf(" Array Size : %llu%s\n", (unsigned long long)ddsks
* sb
->size
,
129 human_size(ddsks
*(long long)sb
->size
<<10));
131 printf(" Raid Devices : %d\n", sb
->raid_disks
);
132 printf(" Total Devices : %d\n", sb
->nr_disks
);
133 printf("Preferred Minor : %d\n", sb
->md_minor
);
135 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
136 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb
->reshape_position
/2, human_size((long long)sb
->reshape_position
<<9));
137 if (sb
->delta_disks
) {
138 printf(" Delta Devices : %d", sb
->delta_disks
);
139 printf(" (%d->%d)\n", sb
->raid_disks
-sb
->delta_disks
, sb
->raid_disks
);
140 if (((int)sb
->delta_disks
) < 0)
141 delta_extra
= - sb
->delta_disks
;
143 if (sb
->new_level
!= sb
->level
) {
144 c
= map_num(pers
, sb
->new_level
);
145 printf(" New Level : %s\n", c
?c
:"-unknown-");
147 if (sb
->new_layout
!= sb
->layout
) {
148 if (sb
->level
== 5) {
149 c
= map_num(r5layout
, sb
->new_layout
);
150 printf(" New Layout : %s\n", c
?c
:"-unknown-");
152 if (sb
->level
== 6) {
153 c
= map_num(r6layout
, sb
->new_layout
);
154 printf(" New Layout : %s\n", c
?c
:"-unknown-");
156 if (sb
->level
== 10) {
157 printf(" New Layout : near=%d, %s=%d\n",
159 (sb
->new_layout
&0x10000)?"offset":"far",
160 (sb
->new_layout
>>8)&255);
163 if (sb
->new_chunk
!= sb
->chunk_size
)
164 printf(" New Chunksize : %d\n", sb
->new_chunk
);
168 printf(" Update Time : %.24s\n", ctime(&atime
));
169 printf(" State : %s\n",
170 (sb
->state
&(1<<MD_SB_CLEAN
))?"clean":"active");
171 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
))
172 printf("Internal Bitmap : present\n");
173 printf(" Active Devices : %d\n", sb
->active_disks
);
174 printf("Working Devices : %d\n", sb
->working_disks
);
175 printf(" Failed Devices : %d\n", sb
->failed_disks
);
176 printf(" Spare Devices : %d\n", sb
->spare_disks
);
177 if (calc_sb0_csum(sb
) == sb
->sb_csum
)
178 printf(" Checksum : %x - correct\n", sb
->sb_csum
);
180 printf(" Checksum : %x - expected %lx\n", sb
->sb_csum
, calc_sb0_csum(sb
));
181 printf(" Events : %llu\n",
182 ((unsigned long long)sb
->events_hi
<< 32)
185 if (sb
->level
== 5) {
186 c
= map_num(r5layout
, sb
->layout
);
187 printf(" Layout : %s\n", c
?c
:"-unknown-");
189 if (sb
->level
== 6) {
190 c
= map_num(r6layout
, sb
->layout
);
191 printf(" Layout : %s\n", c
?c
:"-unknown-");
193 if (sb
->level
== 10) {
195 print_r10_layout(sb
->layout
);
204 printf(" Chunk Size : %dK\n", sb
->chunk_size
/1024);
207 printf(" Rounding : %dK\n", sb
->chunk_size
/1024);
212 printf(" Number Major Minor RaidDevice State\n");
213 for (d
= -1; d
<(signed int)(sb
->raid_disks
+delta_extra
+ sb
->spare_disks
); d
++) {
218 if (d
>=0) dp
= &sb
->disks
[d
];
219 else dp
= &sb
->this_disk
;
220 snprintf(nb
, sizeof(nb
), "%4d", d
);
221 printf("%4s %5d %5d %5d %5d ", d
< 0 ? "this" : nb
,
222 dp
->number
, dp
->major
, dp
->minor
, dp
->raid_disk
);
223 wonly
= dp
->state
& (1<<MD_DISK_WRITEMOSTLY
);
224 dp
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
225 if (dp
->state
& (1<<MD_DISK_FAULTY
)) printf(" faulty");
226 if (dp
->state
& (1<<MD_DISK_ACTIVE
)) printf(" active");
227 if (dp
->state
& (1<<MD_DISK_SYNC
)) printf(" sync");
228 if (dp
->state
& (1<<MD_DISK_REMOVED
)) printf(" removed");
229 if (wonly
) printf(" write-mostly");
230 if (dp
->state
== 0) printf(" spare");
231 if ((dv
=map_dev(dp
->major
, dp
->minor
, 0)))
234 if (d
== -1) printf("\n");
238 static void brief_examine_super0(struct supertype
*st
, int verbose
)
240 mdp_super_t
*sb
= st
->sb
;
241 char *c
=map_num(pers
, sb
->level
);
244 sprintf(devname
, "/dev/md%d", sb
->md_minor
);
247 printf("ARRAY %s level=%s num-devices=%d",
249 c
?c
:"-unknown-", sb
->raid_disks
);
251 printf("ARRAY %s", devname
);
253 if (sb
->minor_version
>= 90)
254 printf(" UUID=%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
255 sb
->set_uuid2
, sb
->set_uuid3
);
257 printf(" UUID=%08x", sb
->set_uuid0
);
261 static void export_examine_super0(struct supertype
*st
)
263 mdp_super_t
*sb
= st
->sb
;
265 printf("MD_LEVEL=%s\n", map_num(pers
, sb
->level
));
266 printf("MD_DEVICES=%d\n", sb
->raid_disks
);
267 if (sb
->minor_version
>= 90)
268 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
269 sb
->set_uuid0
, sb
->set_uuid1
,
270 sb
->set_uuid2
, sb
->set_uuid3
);
272 printf("MD_UUID=%08x\n", sb
->set_uuid0
);
273 printf("MD_UPDATE_TIME=%llu\n",
274 __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
);
275 printf("MD_EVENTS=%llu\n",
276 ((unsigned long long)sb
->events_hi
<< 32)
280 static void detail_super0(struct supertype
*st
, char *homehost
)
282 mdp_super_t
*sb
= st
->sb
;
284 if (sb
->minor_version
>= 90)
285 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
286 sb
->set_uuid2
, sb
->set_uuid3
);
288 printf("%08x", sb
->set_uuid0
);
291 void *hash
= sha1_buffer(homehost
,
294 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
295 printf(" (local to host %s)", homehost
);
297 printf("\n Events : %d.%d\n\n", sb
->events_hi
, sb
->events_lo
);
300 static void brief_detail_super0(struct supertype
*st
)
302 mdp_super_t
*sb
= st
->sb
;
304 if (sb
->minor_version
>= 90)
305 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
306 sb
->set_uuid2
, sb
->set_uuid3
);
308 printf("%08x", sb
->set_uuid0
);
312 static int match_home0(struct supertype
*st
, char *homehost
)
314 mdp_super_t
*sb
= st
->sb
;
320 hash
= sha1_buffer(homehost
,
324 return (memcmp(&sb
->set_uuid2
, hash
, 8)==0);
327 static void uuid_from_super0(struct supertype
*st
, int uuid
[4])
329 mdp_super_t
*super
= st
->sb
;
330 uuid
[0] = super
->set_uuid0
;
331 if (super
->minor_version
>= 90) {
332 uuid
[1] = super
->set_uuid1
;
333 uuid
[2] = super
->set_uuid2
;
334 uuid
[3] = super
->set_uuid3
;
342 static void getinfo_super0(struct supertype
*st
, struct mdinfo
*info
)
344 mdp_super_t
*sb
= st
->sb
;
348 info
->array
.major_version
= sb
->major_version
;
349 info
->array
.minor_version
= sb
->minor_version
;
350 info
->array
.patch_version
= sb
->patch_version
;
351 info
->array
.raid_disks
= sb
->raid_disks
;
352 info
->array
.level
= sb
->level
;
353 info
->array
.layout
= sb
->layout
;
354 info
->array
.md_minor
= sb
->md_minor
;
355 info
->array
.ctime
= sb
->ctime
;
356 info
->array
.utime
= sb
->utime
;
357 info
->array
.chunk_size
= sb
->chunk_size
;
358 info
->array
.state
= sb
->state
;
359 info
->component_size
= sb
->size
*2;
361 info
->disk
.state
= sb
->this_disk
.state
;
362 info
->disk
.major
= sb
->this_disk
.major
;
363 info
->disk
.minor
= sb
->this_disk
.minor
;
364 info
->disk
.raid_disk
= sb
->this_disk
.raid_disk
;
365 info
->disk
.number
= sb
->this_disk
.number
;
367 info
->events
= md_event(sb
);
368 info
->data_offset
= 0;
370 sprintf(info
->text_version
, "0.%d", sb
->minor_version
);
371 info
->safe_mode_delay
= 200;
373 uuid_from_super0(st
, info
->uuid
);
375 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
376 info
->reshape_active
= 1;
377 info
->reshape_progress
= sb
->reshape_position
;
378 info
->new_level
= sb
->new_level
;
379 info
->delta_disks
= sb
->delta_disks
;
380 info
->new_layout
= sb
->new_layout
;
381 info
->new_chunk
= sb
->new_chunk
;
382 if (info
->delta_disks
< 0)
383 info
->array
.raid_disks
-= info
->delta_disks
;
385 info
->reshape_active
= 0;
387 sprintf(info
->name
, "%d", sb
->md_minor
);
388 /* work_disks is calculated rather than read directly */
389 for (i
=0; i
< MD_SB_DISKS
; i
++)
390 if ((sb
->disks
[i
].state
& (1<<MD_DISK_SYNC
)) &&
391 (sb
->disks
[i
].raid_disk
< info
->array
.raid_disks
) &&
392 (sb
->disks
[i
].state
& (1<<MD_DISK_ACTIVE
)) &&
393 !(sb
->disks
[i
].state
& (1<<MD_DISK_FAULTY
)))
395 info
->array
.working_disks
= working
;
399 static int update_super0(struct supertype
*st
, struct mdinfo
*info
,
401 char *devname
, int verbose
,
402 int uuid_set
, char *homehost
)
404 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
405 * For others, the return value is ignored.
408 mdp_super_t
*sb
= st
->sb
;
409 if (strcmp(update
, "sparc2.2")==0 ) {
410 /* 2.2 sparc put the events in the wrong place
411 * So we copy the tail of the superblock
412 * up 4 bytes before continuing
414 __u32
*sb32
= (__u32
*)sb
;
415 memcpy(sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7,
416 sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7+1,
417 (MD_SB_WORDS
- (MD_SB_GENERIC_CONSTANT_WORDS
+7+1))*4);
419 fprintf (stderr
, Name
": adjusting superblock of %s for 2.2/sparc compatability.\n",
422 if (strcmp(update
, "super-minor") ==0) {
423 sb
->md_minor
= info
->array
.md_minor
;
425 fprintf(stderr
, Name
": updating superblock of %s with minor number %d\n",
426 devname
, info
->array
.md_minor
);
428 if (strcmp(update
, "summaries") == 0) {
430 /* set nr_disks, active_disks, working_disks,
431 * failed_disks, spare_disks based on disks[]
432 * array in superblock.
433 * Also make sure extra slots aren't 'failed'
435 sb
->nr_disks
= sb
->active_disks
=
436 sb
->working_disks
= sb
->failed_disks
=
438 for (i
=0; i
< MD_SB_DISKS
; i
++)
439 if (sb
->disks
[i
].major
||
440 sb
->disks
[i
].minor
) {
441 int state
= sb
->disks
[i
].state
;
442 if (state
& (1<<MD_DISK_REMOVED
))
445 if (state
& (1<<MD_DISK_ACTIVE
))
447 if (state
& (1<<MD_DISK_FAULTY
))
453 } else if (i
>= sb
->raid_disks
&& sb
->disks
[i
].number
== 0)
454 sb
->disks
[i
].state
= 0;
456 if (strcmp(update
, "force-one")==0) {
457 /* Not enough devices for a working array, so
458 * bring this one up-to-date.
460 __u32 ehi
= sb
->events_hi
, elo
= sb
->events_lo
;
461 sb
->events_hi
= (info
->events
>>32) & 0xFFFFFFFF;
462 sb
->events_lo
= (info
->events
) & 0xFFFFFFFF;
463 if (sb
->events_hi
!= ehi
||
464 sb
->events_lo
!= elo
)
467 if (strcmp(update
, "force-array")==0) {
468 /* degraded array and 'force' requested, so
469 * maybe need to mark it 'clean'
471 if ((sb
->level
== 5 || sb
->level
== 4 || sb
->level
== 6) &&
472 (sb
->state
& (1 << MD_SB_CLEAN
)) == 0) {
473 /* need to force clean */
474 sb
->state
|= (1 << MD_SB_CLEAN
);
478 if (strcmp(update
, "assemble")==0) {
479 int d
= info
->disk
.number
;
480 int wonly
= sb
->disks
[d
].state
& (1<<MD_DISK_WRITEMOSTLY
);
481 if ((sb
->disks
[d
].state
& ~(1<<MD_DISK_WRITEMOSTLY
))
482 != info
->disk
.state
) {
483 sb
->disks
[d
].state
= info
->disk
.state
| wonly
;
487 if (strcmp(update
, "linear-grow-new") == 0) {
488 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
489 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
490 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
491 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
492 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
493 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
494 sb
->this_disk
= sb
->disks
[info
->disk
.number
];
496 if (strcmp(update
, "linear-grow-update") == 0) {
497 sb
->raid_disks
= info
->array
.raid_disks
;
498 sb
->nr_disks
= info
->array
.nr_disks
;
499 sb
->active_disks
= info
->array
.active_disks
;
500 sb
->working_disks
= info
->array
.working_disks
;
501 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
502 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
503 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
504 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
505 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
506 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
508 if (strcmp(update
, "resync") == 0) {
509 /* make sure resync happens */
510 sb
->state
&= ~(1<<MD_SB_CLEAN
);
513 if (strcmp(update
, "homehost") == 0 &&
517 info
->uuid
[0] = sb
->set_uuid0
;
518 info
->uuid
[1] = sb
->set_uuid1
;
520 if (strcmp(update
, "uuid") == 0) {
521 if (!uuid_set
&& homehost
) {
523 char *hash
= sha1_buffer(homehost
,
526 memcpy(info
->uuid
+2, hash
, 8);
528 sb
->set_uuid0
= info
->uuid
[0];
529 sb
->set_uuid1
= info
->uuid
[1];
530 sb
->set_uuid2
= info
->uuid
[2];
531 sb
->set_uuid3
= info
->uuid
[3];
532 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
533 struct bitmap_super_s
*bm
;
534 bm
= (struct bitmap_super_s
*)(sb
+1);
535 uuid_from_super0(st
, (int*)bm
->uuid
);
538 if (strcmp(update
, "_reshape_progress")==0)
539 sb
->reshape_position
= info
->reshape_progress
;
541 sb
->sb_csum
= calc_sb0_csum(sb
);
546 * For verion-0 superblock, the homehost is 'stored' in the
547 * uuid. 8 bytes for a hash of the host leaving 8 bytes
548 * of random material.
549 * We use the first 8 bytes (64bits) of the sha1 of the
554 static int init_super0(struct supertype
*st
, mdu_array_info_t
*info
,
555 unsigned long long size
, char *ignored_name
, char *homehost
,
561 if (posix_memalign((void**)&sb
, 4096,
562 MD_SB_BYTES
+ ROUND_UP(sizeof(bitmap_super_t
), 4096)) != 0) {
563 fprintf(stderr
, Name
": %s could not allocate superblock\n", __func__
);
566 memset(sb
, 0, MD_SB_BYTES
+ sizeof(bitmap_super_t
));
570 /* zeroing the superblock */
574 spares
= info
->working_disks
- info
->active_disks
;
575 if (info
->raid_disks
+ spares
> MD_SB_DISKS
) {
576 fprintf(stderr
, Name
": too many devices requested: %d+%d > %d\n",
577 info
->raid_disks
, spares
, MD_SB_DISKS
);
581 sb
->md_magic
= MD_SB_MAGIC
;
582 sb
->major_version
= 0;
583 sb
->minor_version
= 90;
584 sb
->patch_version
= 0;
585 sb
->gvalid_words
= 0; /* ignored */
587 sb
->level
= info
->level
;
588 if (size
!= info
->size
)
590 sb
->size
= info
->size
;
591 sb
->nr_disks
= info
->nr_disks
;
592 sb
->raid_disks
= info
->raid_disks
;
593 sb
->md_minor
= info
->md_minor
;
594 sb
->not_persistent
= 0;
596 sb
->set_uuid0
= uuid
[0];
597 sb
->set_uuid1
= uuid
[1];
598 sb
->set_uuid2
= uuid
[2];
599 sb
->set_uuid3
= uuid
[3];
601 int rfd
= open("/dev/urandom", O_RDONLY
);
602 if (rfd
< 0 || read(rfd
, &sb
->set_uuid0
, 4) != 4)
603 sb
->set_uuid0
= random();
604 if (rfd
< 0 || read(rfd
, &sb
->set_uuid1
, 12) != 12) {
605 sb
->set_uuid1
= random();
606 sb
->set_uuid2
= random();
607 sb
->set_uuid3
= random();
614 char *hash
= sha1_buffer(homehost
,
617 memcpy(&sb
->set_uuid2
, hash
, 8);
620 sb
->utime
= sb
->ctime
;
621 sb
->state
= info
->state
;
622 sb
->active_disks
= info
->active_disks
;
623 sb
->working_disks
= info
->working_disks
;
624 sb
->failed_disks
= info
->failed_disks
;
625 sb
->spare_disks
= info
->spare_disks
;
629 sb
->layout
= info
->layout
;
630 sb
->chunk_size
= info
->chunk_size
;
638 mdu_disk_info_t disk
;
639 struct devinfo
*next
;
643 /* Add a device to the superblock being created */
644 static int add_to_super0(struct supertype
*st
, mdu_disk_info_t
*dinfo
,
645 int fd
, char *devname
)
647 mdp_super_t
*sb
= st
->sb
;
648 mdp_disk_t
*dk
= &sb
->disks
[dinfo
->number
];
649 struct devinfo
*di
, **dip
;
651 dk
->number
= dinfo
->number
;
652 dk
->major
= dinfo
->major
;
653 dk
->minor
= dinfo
->minor
;
654 dk
->raid_disk
= dinfo
->raid_disk
;
655 dk
->state
= dinfo
->state
;
657 sb
->this_disk
= sb
->disks
[dinfo
->number
];
658 sb
->sb_csum
= calc_sb0_csum(sb
);
660 dip
= (struct devinfo
**)&st
->info
;
663 di
= malloc(sizeof(struct devinfo
));
665 di
->devname
= devname
;
674 static int store_super0(struct supertype
*st
, int fd
)
676 unsigned long long dsize
;
677 unsigned long long offset
;
678 mdp_super_t
*super
= st
->sb
;
680 if (!get_dev_size(fd
, NULL
, &dsize
))
683 if (dsize
< MD_RESERVED_SECTORS
*512)
686 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
690 if (lseek64(fd
, offset
, 0)< 0LL)
693 if (write(fd
, super
, sizeof(*super
)) != sizeof(*super
))
696 if (super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
697 struct bitmap_super_s
* bm
= (struct bitmap_super_s
*)(super
+1);
698 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
)
699 if (write(fd
, bm
, ROUND_UP(sizeof(*bm
),4096)) !=
700 ROUND_UP(sizeof(*bm
),4096))
709 static int write_init_super0(struct supertype
*st
)
711 mdp_super_t
*sb
= st
->sb
;
715 for (di
= st
->info
; di
&& ! rv
; di
= di
->next
) {
717 if (di
->disk
.state
== 1)
721 Kill(di
->devname
, 0, 1, 1);
722 Kill(di
->devname
, 0, 1, 1);
724 sb
->disks
[di
->disk
.number
].state
&= ~(1<<MD_DISK_FAULTY
);
726 sb
->this_disk
= sb
->disks
[di
->disk
.number
];
727 sb
->sb_csum
= calc_sb0_csum(sb
);
728 rv
= store_super0(st
, di
->fd
);
730 if (rv
== 0 && (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)))
731 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
735 Name
": failed to write superblock to %s\n",
744 static int compare_super0(struct supertype
*st
, struct supertype
*tst
)
748 * 0 same, or first was empty, and second was copied
749 * 1 second had wrong number
753 mdp_super_t
*first
= st
->sb
;
754 mdp_super_t
*second
= tst
->sb
;
755 int uuid1
[4], uuid2
[4];
757 if (second
->md_magic
!= MD_SB_MAGIC
)
760 if (posix_memalign((void**)&first
, 4096,
762 ROUND_UP(sizeof(struct bitmap_super_s
), 4096)) != 0) {
764 ": %s could not allocate superblock\n", __func__
);
767 memcpy(first
, second
, MD_SB_BYTES
+ sizeof(struct bitmap_super_s
));
772 uuid_from_super0(st
, uuid1
);
773 uuid_from_super0(tst
, uuid2
);
774 if (!same_uuid(uuid1
, uuid2
, 0))
776 if (first
->major_version
!= second
->major_version
||
777 first
->minor_version
!= second
->minor_version
||
778 first
->patch_version
!= second
->patch_version
||
779 first
->gvalid_words
!= second
->gvalid_words
||
780 first
->ctime
!= second
->ctime
||
781 first
->level
!= second
->level
||
782 first
->size
!= second
->size
||
783 first
->raid_disks
!= second
->raid_disks
)
790 static void free_super0(struct supertype
*st
);
792 static int load_super0(struct supertype
*st
, int fd
, char *devname
)
794 /* try to read in the superblock
797 * 1 on cannot get superblock
798 * 2 on superblock meaningless
800 unsigned long long dsize
;
801 unsigned long long offset
;
804 struct bitmap_super_s
*bsb
;
811 if (!get_dev_size(fd
, devname
, &dsize
))
814 if (dsize
< MD_RESERVED_SECTORS
*512) {
817 ": %s is too small for md: size is %llu sectors.\n",
822 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
826 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
828 if (lseek64(fd
, offset
, 0)< 0LL) {
830 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
831 devname
, strerror(errno
));
835 if (posix_memalign((void**)&super
, 4096,
837 ROUND_UP(sizeof(bitmap_super_t
), 4096)) != 0) {
839 ": %s could not allocate superblock\n", __func__
);
843 if (read(fd
, super
, sizeof(*super
)) != MD_SB_BYTES
) {
845 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
851 if (st
->ss
&& st
->minor_version
== 9)
852 super0_swap_endian(super
);
854 if (super
->md_magic
!= MD_SB_MAGIC
) {
856 fprintf(stderr
, Name
": No super block found on %s (Expected magic %08x, got %08x)\n",
857 devname
, MD_SB_MAGIC
, super
->md_magic
);
862 if (super
->major_version
!= 0) {
864 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
865 devname
, super
->major_version
);
871 if (st
->ss
== NULL
) {
873 st
->minor_version
= super
->minor_version
;
874 st
->max_devs
= MD_SB_DISKS
;
878 /* Now check on the bitmap superblock */
879 if ((super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) == 0)
881 /* Read the bitmap superblock and make sure it looks
882 * valid. If it doesn't clear the bit. An --assemble --force
883 * should get that written out.
885 if (read(fd
, super
+1, ROUND_UP(sizeof(struct bitmap_super_s
),4096))
886 != ROUND_UP(sizeof(struct bitmap_super_s
),4096))
889 uuid_from_super0(st
, uuid
);
890 bsb
= (struct bitmap_super_s
*)(super
+1);
891 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
892 memcmp(bsb
->uuid
, uuid
, 16) != 0)
897 super
->state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
902 static struct supertype
*match_metadata_desc0(char *arg
)
904 struct supertype
*st
= malloc(sizeof(*st
));
907 memset(st
, 0, sizeof(*st
));
910 st
->minor_version
= 90;
911 st
->max_devs
= MD_SB_DISKS
;
913 /* we sometimes get 00.90 */
914 while (arg
[0] == '0' && arg
[1] == '0')
916 if (strcmp(arg
, "0") == 0 ||
917 strcmp(arg
, "0.90") == 0 ||
918 strcmp(arg
, "default") == 0 ||
919 strcmp(arg
, "") == 0 /* no metadata */
923 st
->minor_version
= 91; /* reshape in progress */
924 if (strcmp(arg
, "0.91") == 0) /* For dup_super support */
927 st
->minor_version
= 9; /* flag for 'byte-swapped' */
928 if (strcmp(arg
, "0.swap")==0 ||
929 strcmp(arg
, "0.9") == 0) /* For dup_super support */
936 static __u64
avail_size0(struct supertype
*st
, __u64 devsize
)
938 if (devsize
< MD_RESERVED_SECTORS
)
940 return MD_NEW_SIZE_SECTORS(devsize
);
943 static int add_internal_bitmap0(struct supertype
*st
, int *chunkp
,
944 int delay
, int write_behind
,
945 unsigned long long size
, int may_change
,
949 * The bitmap comes immediately after the superblock and must be 60K in size
950 * at most. The default size is between 30K and 60K
952 * size is in sectors, chunk is in bytes !!!
954 unsigned long long bits
;
955 unsigned long long max_bits
= 60*1024*8;
956 unsigned long long min_chunk
;
958 mdp_super_t
*sb
= st
->sb
;
959 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + MD_SB_BYTES
);
962 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
963 bits
= (size
* 512) / min_chunk
+ 1;
964 while (bits
> max_bits
) {
970 else if (chunk
< min_chunk
)
971 return 0; /* chunk size too small */
973 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
975 memset(bms
, 0, sizeof(*bms
));
976 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
977 bms
->version
= __cpu_to_le32(major
);
978 uuid_from_super0(st
, (int*)bms
->uuid
);
979 bms
->chunksize
= __cpu_to_le32(chunk
);
980 bms
->daemon_sleep
= __cpu_to_le32(delay
);
981 bms
->sync_size
= __cpu_to_le64(size
);
982 bms
->write_behind
= __cpu_to_le32(write_behind
);
988 static void locate_bitmap0(struct supertype
*st
, int fd
)
990 unsigned long long dsize
;
991 unsigned long long offset
;
993 if (!get_dev_size(fd
, NULL
, &dsize
))
996 if (dsize
< MD_RESERVED_SECTORS
*512)
999 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
1003 offset
+= MD_SB_BYTES
;
1005 lseek64(fd
, offset
, 0);
1008 static int write_bitmap0(struct supertype
*st
, int fd
)
1010 unsigned long long dsize
;
1011 unsigned long long offset
;
1012 mdp_super_t
*sb
= st
->sb
;
1017 char abuf
[4096+4096];
1018 char *buf
= (char*)(((long)(abuf
+4096))&~4095L);
1020 if (!get_dev_size(fd
, NULL
, &dsize
))
1024 if (dsize
< MD_RESERVED_SECTORS
*512)
1027 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
1031 if (lseek64(fd
, offset
+ 4096, 0)< 0LL)
1034 memset(buf
, 0xff, 4096);
1035 memcpy(buf
, ((char*)sb
)+MD_SB_BYTES
, sizeof(bitmap_super_t
));
1037 while (towrite
> 0) {
1041 n
= write(fd
, buf
, n
);
1046 memset(buf
, 0xff, 4096);
1055 static void free_super0(struct supertype
*st
)
1063 static int validate_geometry0(struct supertype
*st
, int level
,
1064 int layout
, int raiddisks
,
1065 int chunk
, unsigned long long size
,
1066 char *subdev
, unsigned long long *freesize
,
1069 unsigned long long ldsize
;
1072 if (level
== LEVEL_CONTAINER
)
1074 if (raiddisks
> MD_SB_DISKS
)
1076 if (size
> (0x7fffffffULL
<<9))
1081 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
1084 fprintf(stderr
, Name
": super0.90 cannot open %s: %s\n",
1085 subdev
, strerror(errno
));
1089 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
1095 if (ldsize
< MD_RESERVED_SECTORS
* 512)
1097 if (size
> (0x7fffffffULL
<<9))
1099 *freesize
= MD_NEW_SIZE_SECTORS(ldsize
>> 9);
1102 #endif /* MDASSEMBLE */
1104 struct superswitch super0
= {
1106 .examine_super
= examine_super0
,
1107 .brief_examine_super
= brief_examine_super0
,
1108 .export_examine_super
= export_examine_super0
,
1109 .detail_super
= detail_super0
,
1110 .brief_detail_super
= brief_detail_super0
,
1111 .write_init_super
= write_init_super0
,
1112 .validate_geometry
= validate_geometry0
,
1113 .add_to_super
= add_to_super0
,
1115 .match_home
= match_home0
,
1116 .uuid_from_super
= uuid_from_super0
,
1117 .getinfo_super
= getinfo_super0
,
1118 .update_super
= update_super0
,
1119 .init_super
= init_super0
,
1120 .store_super
= store_super0
,
1121 .compare_super
= compare_super0
,
1122 .load_super
= load_super0
,
1123 .match_metadata_desc
= match_metadata_desc0
,
1124 .avail_size
= avail_size0
,
1125 .add_internal_bitmap
= add_internal_bitmap0
,
1126 .locate_bitmap
= locate_bitmap0
,
1127 .write_bitmap
= write_bitmap0
,
1128 .free_super
= free_super0
,