2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
30 #define HAVE_STDINT_H 1
34 * All handling for the 0.90.0 version superblock is in
37 * - finding, loading, and writing the superblock.
38 * - initialising a new superblock
39 * - printing the superblock for --examine
40 * - printing part of the superblock for --detail
45 static unsigned long calc_sb0_csum(mdp_super_t
*super
)
47 unsigned long csum
= super
->sb_csum
;
48 unsigned long newcsum
;
50 newcsum
= calc_csum(super
, MD_SB_BYTES
);
51 super
->sb_csum
= csum
;
56 void super0_swap_endian(struct mdp_superblock_s
*sb
)
58 /* as super0 superblocks are host-endian, it is sometimes
59 * useful to be able to swap the endianness
60 * as (almost) everything is u32's we byte-swap every 4byte
62 * We then also have to swap the events_hi and events_lo
64 char *sbc
= (char *)sb
;
68 for (i
=0; i
< MD_SB_BYTES
; i
+=4) {
77 sb
->events_hi
= sb
->events_lo
;
80 t32
= sb
->cp_events_hi
;
81 sb
->cp_events_hi
= sb
->cp_events_lo
;
82 sb
->cp_events_lo
= t32
;
88 static void examine_super0(struct supertype
*st
, char *homehost
)
90 mdp_super_t
*sb
= st
->sb
;
95 printf(" Magic : %08x\n", sb
->md_magic
);
96 printf(" Version : %02d.%02d.%02d\n", sb
->major_version
, sb
->minor_version
,
98 if (sb
->minor_version
>= 90) {
99 printf(" UUID : %08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
100 sb
->set_uuid2
, sb
->set_uuid3
);
103 void *hash
= sha1_buffer(homehost
,
106 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
107 printf(" (local to host %s)", homehost
);
111 printf(" UUID : %08x\n", sb
->set_uuid0
);
113 if (sb
->not_persistent
)
114 printf(" Eedk : not persistent\n");
117 printf(" Creation Time : %.24s\n", ctime(&atime
));
118 c
=map_num(pers
, sb
->level
);
119 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
120 if ((int)sb
->level
>= 0) {
122 printf(" Used Dev Size : %d%s\n", sb
->size
,
123 human_size((long long)sb
->size
<<10));
125 case 1: ddsks
=1;break;
127 case 5: ddsks
= sb
->raid_disks
-1; break;
128 case 6: ddsks
= sb
->raid_disks
-2; break;
129 case 10: ddsks
= sb
->raid_disks
/ (sb
->layout
&255) / ((sb
->layout
>>8)&255);
132 printf(" Array Size : %llu%s\n", (unsigned long long)ddsks
* sb
->size
,
133 human_size(ddsks
*(long long)sb
->size
<<10));
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
);
144 printf(" (%d->%d)\n", sb
->raid_disks
-sb
->delta_disks
, sb
->raid_disks
);
146 printf(" (%d->%d)\n", sb
->raid_disks
, sb
->raid_disks
+sb
->delta_disks
);
148 if (sb
->new_level
!= sb
->level
) {
149 c
= map_num(pers
, sb
->new_level
);
150 printf(" New Level : %s\n", c
?c
:"-unknown-");
152 if (sb
->new_layout
!= sb
->layout
) {
153 if (sb
->level
== 5) {
154 c
= map_num(r5layout
, sb
->new_layout
);
155 printf(" New Layout : %s\n", c
?c
:"-unknown-");
157 if (sb
->level
== 10) {
158 printf(" New Layout : near=%d, %s=%d\n",
160 (sb
->new_layout
&0x10000)?"offset":"far",
161 (sb
->new_layout
>>8)&255);
164 if (sb
->new_chunk
!= sb
->chunk_size
)
165 printf(" New Chunksize : %d\n", sb
->new_chunk
);
169 printf(" Update Time : %.24s\n", ctime(&atime
));
170 printf(" State : %s\n",
171 (sb
->state
&(1<<MD_SB_CLEAN
))?"clean":"active");
172 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
))
173 printf("Internal Bitmap : present\n");
174 printf(" Active Devices : %d\n", sb
->active_disks
);
175 printf("Working Devices : %d\n", sb
->working_disks
);
176 printf(" Failed Devices : %d\n", sb
->failed_disks
);
177 printf(" Spare Devices : %d\n", sb
->spare_disks
);
178 if (calc_sb0_csum(sb
) == sb
->sb_csum
)
179 printf(" Checksum : %x - correct\n", sb
->sb_csum
);
181 printf(" Checksum : %x - expected %lx\n", sb
->sb_csum
, calc_sb0_csum(sb
));
182 printf(" Events : %llu\n",
183 ((unsigned long long)sb
->events_hi
<< 32)
186 if (sb
->level
== 5) {
187 c
= map_num(r5layout
, sb
->layout
);
188 printf(" Layout : %s\n", c
?c
:"-unknown-");
190 if (sb
->level
== 10) {
191 printf(" Layout : near=%d, %s=%d\n",
193 (sb
->layout
&0x10000)?"offset":"far",
194 (sb
->layout
>>8)&255);
202 printf(" Chunk Size : %dK\n", sb
->chunk_size
/1024);
205 printf(" Rounding : %dK\n", sb
->chunk_size
/1024);
210 printf(" Number Major Minor RaidDevice State\n");
211 for (d
= -1; d
<(signed int)(sb
->raid_disks
+sb
->spare_disks
); d
++) {
216 if (d
>=0) dp
= &sb
->disks
[d
];
217 else dp
= &sb
->this_disk
;
218 snprintf(nb
, sizeof(nb
), "%4d", d
);
219 printf("%4s %5d %5d %5d %5d ", d
< 0 ? "this" : nb
,
220 dp
->number
, dp
->major
, dp
->minor
, dp
->raid_disk
);
221 wonly
= dp
->state
& (1<<MD_DISK_WRITEMOSTLY
);
222 dp
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
223 if (dp
->state
& (1<<MD_DISK_FAULTY
)) printf(" faulty");
224 if (dp
->state
& (1<<MD_DISK_ACTIVE
)) printf(" active");
225 if (dp
->state
& (1<<MD_DISK_SYNC
)) printf(" sync");
226 if (dp
->state
& (1<<MD_DISK_REMOVED
)) printf(" removed");
227 if (wonly
) printf(" write-mostly");
228 if (dp
->state
== 0) printf(" spare");
229 if ((dv
=map_dev(dp
->major
, dp
->minor
, 0)))
232 if (d
== -1) printf("\n");
236 static void brief_examine_super0(struct supertype
*st
)
238 mdp_super_t
*sb
= st
->sb
;
239 char *c
=map_num(pers
, sb
->level
);
242 sprintf(devname
, "/dev/md%d", sb
->md_minor
);
244 printf("ARRAY %s level=%s num-devices=%d UUID=",
246 c
?c
:"-unknown-", sb
->raid_disks
);
247 if (sb
->minor_version
>= 90)
248 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
249 sb
->set_uuid2
, sb
->set_uuid3
);
251 printf("%08x", sb
->set_uuid0
);
255 static void detail_super0(struct supertype
*st
, char *homehost
)
257 mdp_super_t
*sb
= st
->sb
;
259 if (sb
->minor_version
>= 90)
260 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
261 sb
->set_uuid2
, sb
->set_uuid3
);
263 printf("%08x", sb
->set_uuid0
);
266 void *hash
= sha1_buffer(homehost
,
269 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
270 printf(" (local to host %s)", homehost
);
272 printf("\n Events : %d.%d\n\n", sb
->events_hi
, sb
->events_lo
);
275 static void brief_detail_super0(struct supertype
*st
)
277 mdp_super_t
*sb
= st
->sb
;
279 if (sb
->minor_version
>= 90)
280 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
281 sb
->set_uuid2
, sb
->set_uuid3
);
283 printf("%08x", sb
->set_uuid0
);
286 static void export_super0(struct supertype
*st
)
288 mdp_super_t
*sb
= st
->sb
;
290 if (sb
->minor_version
>= 90)
291 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
292 sb
->set_uuid2
, sb
->set_uuid3
);
294 printf("%08x", sb
->set_uuid0
);
299 static int match_home0(struct supertype
*st
, char *homehost
)
301 mdp_super_t
*sb
= st
->sb
;
303 char *hash
= sha1_buffer(homehost
,
307 return (memcmp(&sb
->set_uuid2
, hash
, 8)==0);
310 static void uuid_from_super0(struct supertype
*st
, int uuid
[4])
312 mdp_super_t
*super
= st
->sb
;
313 uuid
[0] = super
->set_uuid0
;
314 if (super
->minor_version
>= 90) {
315 uuid
[1] = super
->set_uuid1
;
316 uuid
[2] = super
->set_uuid2
;
317 uuid
[3] = super
->set_uuid3
;
325 static void getinfo_super0(struct supertype
*st
, struct mdinfo
*info
)
327 mdp_super_t
*sb
= st
->sb
;
331 info
->array
.major_version
= sb
->major_version
;
332 info
->array
.minor_version
= sb
->minor_version
;
333 info
->array
.patch_version
= sb
->patch_version
;
334 info
->array
.raid_disks
= sb
->raid_disks
;
335 info
->array
.level
= sb
->level
;
336 info
->array
.layout
= sb
->layout
;
337 info
->array
.md_minor
= sb
->md_minor
;
338 info
->array
.ctime
= sb
->ctime
;
339 info
->array
.utime
= sb
->utime
;
340 info
->array
.chunk_size
= sb
->chunk_size
;
341 info
->array
.state
= sb
->state
;
342 info
->component_size
= sb
->size
*2;
344 info
->disk
.state
= sb
->this_disk
.state
;
345 info
->disk
.major
= sb
->this_disk
.major
;
346 info
->disk
.minor
= sb
->this_disk
.minor
;
347 info
->disk
.raid_disk
= sb
->this_disk
.raid_disk
;
348 info
->disk
.number
= sb
->this_disk
.number
;
350 info
->events
= md_event(sb
);
351 info
->data_offset
= 0;
353 uuid_from_super0(st
, info
->uuid
);
355 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
356 info
->reshape_active
= 1;
357 info
->reshape_progress
= sb
->reshape_position
;
358 info
->new_level
= sb
->new_level
;
359 info
->delta_disks
= sb
->delta_disks
;
360 info
->new_layout
= sb
->new_layout
;
361 info
->new_chunk
= sb
->new_chunk
;
363 info
->reshape_active
= 0;
365 sprintf(info
->name
, "%d", sb
->md_minor
);
366 /* work_disks is calculated rather than read directly */
367 for (i
=0; i
< MD_SB_DISKS
; i
++)
368 if ((sb
->disks
[i
].state
& (1<<MD_DISK_SYNC
)) &&
369 (sb
->disks
[i
].raid_disk
< info
->array
.raid_disks
) &&
370 (sb
->disks
[i
].state
& (1<<MD_DISK_ACTIVE
)) &&
371 !(sb
->disks
[i
].state
& (1<<MD_DISK_FAULTY
)))
373 info
->array
.working_disks
= working
;
377 static int update_super0(struct supertype
*st
, struct mdinfo
*info
,
379 char *devname
, int verbose
,
380 int uuid_set
, char *homehost
)
382 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
383 * For others, the return value is ignored.
386 mdp_super_t
*sb
= st
->sb
;
387 if (strcmp(update
, "sparc2.2")==0 ) {
388 /* 2.2 sparc put the events in the wrong place
389 * So we copy the tail of the superblock
390 * up 4 bytes before continuing
392 __u32
*sb32
= (__u32
*)sb
;
393 memcpy(sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7,
394 sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7+1,
395 (MD_SB_WORDS
- (MD_SB_GENERIC_CONSTANT_WORDS
+7+1))*4);
397 fprintf (stderr
, Name
": adjusting superblock of %s for 2.2/sparc compatability.\n",
400 if (strcmp(update
, "super-minor") ==0) {
401 sb
->md_minor
= info
->array
.md_minor
;
403 fprintf(stderr
, Name
": updating superblock of %s with minor number %d\n",
404 devname
, info
->array
.md_minor
);
406 if (strcmp(update
, "summaries") == 0) {
408 /* set nr_disks, active_disks, working_disks,
409 * failed_disks, spare_disks based on disks[]
410 * array in superblock.
411 * Also make sure extra slots aren't 'failed'
413 sb
->nr_disks
= sb
->active_disks
=
414 sb
->working_disks
= sb
->failed_disks
=
416 for (i
=0; i
< MD_SB_DISKS
; i
++)
417 if (sb
->disks
[i
].major
||
418 sb
->disks
[i
].minor
) {
419 int state
= sb
->disks
[i
].state
;
420 if (state
& (1<<MD_DISK_REMOVED
))
423 if (state
& (1<<MD_DISK_ACTIVE
))
425 if (state
& (1<<MD_DISK_FAULTY
))
431 } else if (i
>= sb
->raid_disks
&& sb
->disks
[i
].number
== 0)
432 sb
->disks
[i
].state
= 0;
434 if (strcmp(update
, "force-one")==0) {
435 /* Not enough devices for a working array, so
436 * bring this one up-to-date.
438 __u32 ehi
= sb
->events_hi
, elo
= sb
->events_lo
;
439 sb
->events_hi
= (info
->events
>>32) & 0xFFFFFFFF;
440 sb
->events_lo
= (info
->events
) & 0xFFFFFFFF;
441 if (sb
->events_hi
!= ehi
||
442 sb
->events_lo
!= elo
)
445 if (strcmp(update
, "force-array")==0) {
446 /* degraded array and 'force' requested, so
447 * maybe need to mark it 'clean'
449 if ((sb
->level
== 5 || sb
->level
== 4 || sb
->level
== 6) &&
450 (sb
->state
& (1 << MD_SB_CLEAN
)) == 0) {
451 /* need to force clean */
452 sb
->state
|= (1 << MD_SB_CLEAN
);
456 if (strcmp(update
, "assemble")==0) {
457 int d
= info
->disk
.number
;
458 int wonly
= sb
->disks
[d
].state
& (1<<MD_DISK_WRITEMOSTLY
);
459 if ((sb
->disks
[d
].state
& ~(1<<MD_DISK_WRITEMOSTLY
))
460 != info
->disk
.state
) {
461 sb
->disks
[d
].state
= info
->disk
.state
| wonly
;
465 if (strcmp(update
, "linear-grow-new") == 0) {
466 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
467 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
468 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
469 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
470 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
471 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
472 sb
->this_disk
= sb
->disks
[info
->disk
.number
];
474 if (strcmp(update
, "linear-grow-update") == 0) {
475 sb
->raid_disks
= info
->array
.raid_disks
;
476 sb
->nr_disks
= info
->array
.nr_disks
;
477 sb
->active_disks
= info
->array
.active_disks
;
478 sb
->working_disks
= info
->array
.working_disks
;
479 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
480 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
481 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
482 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
483 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
484 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
486 if (strcmp(update
, "resync") == 0) {
487 /* make sure resync happens */
488 sb
->state
&= ~(1<<MD_SB_CLEAN
);
491 if (strcmp(update
, "homehost") == 0 &&
495 info
->uuid
[0] = sb
->set_uuid0
;
496 info
->uuid
[1] = sb
->set_uuid1
;
498 if (strcmp(update
, "uuid") == 0) {
499 if (!uuid_set
&& homehost
) {
501 char *hash
= sha1_buffer(homehost
,
504 memcpy(info
->uuid
+2, hash
, 8);
506 sb
->set_uuid0
= info
->uuid
[0];
507 sb
->set_uuid1
= info
->uuid
[1];
508 sb
->set_uuid2
= info
->uuid
[2];
509 sb
->set_uuid3
= info
->uuid
[3];
510 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
511 struct bitmap_super_s
*bm
;
512 bm
= (struct bitmap_super_s
*)(sb
+1);
513 uuid_from_super0(st
, (int*)bm
->uuid
);
516 if (strcmp(update
, "_reshape_progress")==0)
517 sb
->reshape_position
= info
->reshape_progress
;
519 sb
->sb_csum
= calc_sb0_csum(sb
);
524 * For verion-0 superblock, the homehost is 'stored' in the
525 * uuid. 8 bytes for a hash of the host leaving 8 bytes
526 * of random material.
527 * We use the first 8 bytes (64bits) of the sha1 of the
532 static int init_super0(struct supertype
*st
, mdu_array_info_t
*info
,
533 unsigned long long size
, char *ignored_name
, char *homehost
,
536 mdp_super_t
*sb
= malloc(MD_SB_BYTES
+ sizeof(bitmap_super_t
));
538 memset(sb
, 0, MD_SB_BYTES
+ sizeof(bitmap_super_t
));
541 if (info
->major_version
== -1) {
542 /* zeroing the superblock */
546 spares
= info
->working_disks
- info
->active_disks
;
547 if (info
->raid_disks
+ spares
> MD_SB_DISKS
) {
548 fprintf(stderr
, Name
": too many devices requested: %d+%d > %d\n",
549 info
->raid_disks
, spares
, MD_SB_DISKS
);
553 sb
->md_magic
= MD_SB_MAGIC
;
554 sb
->major_version
= 0;
555 sb
->minor_version
= 90;
556 sb
->patch_version
= 0;
557 sb
->gvalid_words
= 0; /* ignored */
559 sb
->level
= info
->level
;
560 if (size
!= info
->size
)
562 sb
->size
= info
->size
;
563 sb
->nr_disks
= info
->nr_disks
;
564 sb
->raid_disks
= info
->raid_disks
;
565 sb
->md_minor
= info
->md_minor
;
566 sb
->not_persistent
= 0;
568 sb
->set_uuid0
= uuid
[0];
569 sb
->set_uuid1
= uuid
[1];
570 sb
->set_uuid2
= uuid
[2];
571 sb
->set_uuid3
= uuid
[3];
573 int rfd
= open("/dev/urandom", O_RDONLY
);
574 if (rfd
< 0 || read(rfd
, &sb
->set_uuid0
, 4) != 4)
575 sb
->set_uuid0
= random();
576 if (rfd
< 0 || read(rfd
, &sb
->set_uuid1
, 12) != 12) {
577 sb
->set_uuid1
= random();
578 sb
->set_uuid2
= random();
579 sb
->set_uuid3
= random();
586 char *hash
= sha1_buffer(homehost
,
589 memcpy(&sb
->set_uuid2
, hash
, 8);
592 sb
->utime
= sb
->ctime
;
593 sb
->state
= info
->state
;
594 sb
->active_disks
= info
->active_disks
;
595 sb
->working_disks
= info
->working_disks
;
596 sb
->failed_disks
= info
->failed_disks
;
597 sb
->spare_disks
= info
->spare_disks
;
601 sb
->layout
= info
->layout
;
602 sb
->chunk_size
= info
->chunk_size
;
607 /* Add a device to the superblock being created */
608 static void add_to_super0(struct supertype
*st
, mdu_disk_info_t
*dinfo
)
610 mdp_super_t
*sb
= st
->sb
;
611 mdp_disk_t
*dk
= &sb
->disks
[dinfo
->number
];
613 dk
->number
= dinfo
->number
;
614 dk
->major
= dinfo
->major
;
615 dk
->minor
= dinfo
->minor
;
616 dk
->raid_disk
= dinfo
->raid_disk
;
617 dk
->state
= dinfo
->state
;
620 static int store_super0(struct supertype
*st
, int fd
)
622 unsigned long long dsize
;
623 unsigned long long offset
;
624 mdp_super_t
*super
= st
->sb
;
626 if (!get_dev_size(fd
, NULL
, &dsize
))
629 if (dsize
< MD_RESERVED_SECTORS
*512)
632 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
636 if (lseek64(fd
, offset
, 0)< 0LL)
639 if (write(fd
, super
, sizeof(*super
)) != sizeof(*super
))
642 if (super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
643 struct bitmap_super_s
* bm
= (struct bitmap_super_s
*)(super
+1);
644 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
)
645 if (write(fd
, bm
, sizeof(*bm
)) != sizeof(*bm
))
653 static int write_init_super0(struct supertype
*st
,
654 mdu_disk_info_t
*dinfo
, char *devname
)
656 mdp_super_t
*sb
= st
->sb
;
657 int fd
= open(devname
, O_RDWR
|O_EXCL
);
661 fprintf(stderr
, Name
": Failed to open %s to write superblock\n", devname
);
665 sb
->disks
[dinfo
->number
].state
&= ~(1<<MD_DISK_FAULTY
);
667 sb
->this_disk
= sb
->disks
[dinfo
->number
];
668 sb
->sb_csum
= calc_sb0_csum(sb
);
669 rv
= store_super0(st
, fd
);
671 if (rv
== 0 && (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)))
672 rv
= st
->ss
->write_bitmap(st
, fd
);
676 fprintf(stderr
, Name
": failed to write superblock to %s\n", devname
);
680 static int compare_super0(struct supertype
*st
, struct supertype
*tst
)
684 * 0 same, or first was empty, and second was copied
685 * 1 second had wrong number
689 mdp_super_t
*first
= st
->sb
;
690 mdp_super_t
*second
= tst
->sb
;
691 int uuid1
[4], uuid2
[4];
693 if (second
->md_magic
!= MD_SB_MAGIC
)
696 first
= malloc(MD_SB_BYTES
+ sizeof(struct bitmap_super_s
));
697 memcpy(first
, second
, MD_SB_BYTES
+ sizeof(struct bitmap_super_s
));
702 uuid_from_super0(st
, uuid1
);
703 uuid_from_super0(tst
, uuid2
);
704 if (!same_uuid(uuid1
, uuid2
, 0))
706 if (first
->major_version
!= second
->major_version
||
707 first
->minor_version
!= second
->minor_version
||
708 first
->patch_version
!= second
->patch_version
||
709 first
->gvalid_words
!= second
->gvalid_words
||
710 first
->ctime
!= second
->ctime
||
711 first
->level
!= second
->level
||
712 first
->size
!= second
->size
||
713 first
->raid_disks
!= second
->raid_disks
)
720 static void free_super0(struct supertype
*st
);
722 static int load_super0(struct supertype
*st
, int fd
, char *devname
)
724 /* try to read in the superblock
727 * 1 on cannot get superblock
728 * 2 on superblock meaningless
730 unsigned long long dsize
;
731 unsigned long long offset
;
734 struct bitmap_super_s
*bsb
;
738 if (!get_dev_size(fd
, devname
, &dsize
))
741 if (dsize
< MD_RESERVED_SECTORS
*512) {
744 ": %s is too small for md: size is %llu sectors.\n",
749 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
753 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
755 if (lseek64(fd
, offset
, 0)< 0LL) {
757 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
758 devname
, strerror(errno
));
762 super
= malloc(MD_SB_BYTES
+ sizeof(bitmap_super_t
));
764 if (read(fd
, super
, sizeof(*super
)) != MD_SB_BYTES
) {
766 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
772 if (st
->ss
&& st
->minor_version
== 9)
773 super0_swap_endian(super
);
775 if (super
->md_magic
!= MD_SB_MAGIC
) {
777 fprintf(stderr
, Name
": No super block found on %s (Expected magic %08x, got %08x)\n",
778 devname
, MD_SB_MAGIC
, super
->md_magic
);
783 if (super
->major_version
!= 0) {
785 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
786 devname
, super
->major_version
);
792 if (st
->ss
== NULL
) {
794 st
->minor_version
= 90;
795 st
->max_devs
= MD_SB_DISKS
;
798 /* Now check on the bitmap superblock */
799 if ((super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) == 0)
801 /* Read the bitmap superblock and make sure it looks
802 * valid. If it doesn't clear the bit. An --assemble --force
803 * should get that written out.
805 if (read(fd
, super
+1, sizeof(struct bitmap_super_s
))
806 != sizeof(struct bitmap_super_s
))
809 uuid_from_super0(st
, uuid
);
810 bsb
= (struct bitmap_super_s
*)(super
+1);
811 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
812 memcmp(bsb
->uuid
, uuid
, 16) != 0)
817 super
->state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
822 static struct supertype
*match_metadata_desc0(char *arg
)
824 struct supertype
*st
= malloc(sizeof(*st
));
828 st
->minor_version
= 90;
829 st
->max_devs
= MD_SB_DISKS
;
831 if (strcmp(arg
, "0") == 0 ||
832 strcmp(arg
, "0.90") == 0 ||
833 strcmp(arg
, "default") == 0
837 st
->minor_version
= 9; /* flag for 'byte-swapped' */
838 if (strcmp(arg
, "0.swap")==0)
845 static __u64
avail_size0(struct supertype
*st
, __u64 devsize
)
847 if (devsize
< MD_RESERVED_SECTORS
)
849 return MD_NEW_SIZE_SECTORS(devsize
);
852 static int add_internal_bitmap0(struct supertype
*st
, int *chunkp
,
853 int delay
, int write_behind
,
854 unsigned long long size
, int may_change
,
858 * The bitmap comes immediately after the superblock and must be 60K in size
859 * at most. The default size is between 30K and 60K
861 * size is in sectors, chunk is in bytes !!!
863 unsigned long long bits
;
864 unsigned long long max_bits
= 60*1024*8;
865 unsigned long long min_chunk
;
867 mdp_super_t
*sb
= st
->sb
;
868 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + MD_SB_BYTES
);
871 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
872 bits
= (size
* 512) / min_chunk
+ 1;
873 while (bits
> max_bits
) {
879 else if (chunk
< min_chunk
)
880 return 0; /* chunk size too small */
882 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
884 memset(bms
, 0, sizeof(*bms
));
885 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
886 bms
->version
= __cpu_to_le32(major
);
887 uuid_from_super0(st
, (int*)bms
->uuid
);
888 bms
->chunksize
= __cpu_to_le32(chunk
);
889 bms
->daemon_sleep
= __cpu_to_le32(delay
);
890 bms
->sync_size
= __cpu_to_le64(size
);
891 bms
->write_behind
= __cpu_to_le32(write_behind
);
897 void locate_bitmap0(struct supertype
*st
, int fd
)
899 unsigned long long dsize
;
900 unsigned long long offset
;
902 if (!get_dev_size(fd
, NULL
, &dsize
))
905 if (dsize
< MD_RESERVED_SECTORS
*512)
908 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
912 offset
+= MD_SB_BYTES
;
914 lseek64(fd
, offset
, 0);
917 int write_bitmap0(struct supertype
*st
, int fd
)
919 unsigned long long dsize
;
920 unsigned long long offset
;
921 mdp_super_t
*sb
= st
->sb
;
928 if (!get_dev_size(fd
, NULL
, &dsize
))
932 if (dsize
< MD_RESERVED_SECTORS
*512)
935 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
939 if (lseek64(fd
, offset
+ 4096, 0)< 0LL)
943 if (write(fd
, ((char*)sb
)+MD_SB_BYTES
, sizeof(bitmap_super_t
)) !=
944 sizeof(bitmap_super_t
))
946 towrite
= 64*1024 - MD_SB_BYTES
- sizeof(bitmap_super_t
);
947 memset(buf
, 0xff, sizeof(buf
));
948 while (towrite
> 0) {
952 n
= write(fd
, buf
, n
);
965 static void free_super0(struct supertype
*st
)
972 struct superswitch super0
= {
974 .examine_super
= examine_super0
,
975 .brief_examine_super
= brief_examine_super0
,
976 .detail_super
= detail_super0
,
977 .brief_detail_super
= brief_detail_super0
,
978 .export_super
= export_super0
,
980 .match_home
= match_home0
,
981 .uuid_from_super
= uuid_from_super0
,
982 .getinfo_super
= getinfo_super0
,
983 .update_super
= update_super0
,
984 .init_super
= init_super0
,
985 .add_to_super
= add_to_super0
,
986 .store_super
= store_super0
,
987 .write_init_super
= write_init_super0
,
988 .compare_super
= compare_super0
,
989 .load_super
= load_super0
,
990 .match_metadata_desc
= match_metadata_desc0
,
991 .avail_size
= avail_size0
,
992 .add_internal_bitmap
= add_internal_bitmap0
,
993 .locate_bitmap
= locate_bitmap0
,
994 .write_bitmap
= write_bitmap0
,
995 .free_super
= free_super0
,