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(void *sbv
, char *homehost
)
90 mdp_super_t
*sb
= sbv
;
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
);
114 printf(" Creation Time : %.24s\n", ctime(&atime
));
115 c
=map_num(pers
, sb
->level
);
116 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
117 if ((int)sb
->level
>= 0) {
119 printf(" Device Size : %d%s\n", sb
->size
, 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
);
140 printf(" (%d->%d)\n", sb
->raid_disks
-sb
->delta_disks
, sb
->raid_disks
);
142 printf(" (%d->%d)\n", sb
->raid_disks
, sb
->raid_disks
+sb
->delta_disks
);
144 if (sb
->new_level
!= sb
->level
) {
145 c
= map_num(pers
, sb
->new_level
);
146 printf(" New Level : %s\n", c
?c
:"-unknown-");
148 if (sb
->new_layout
!= sb
->layout
) {
149 if (sb
->level
== 5) {
150 c
= map_num(r5layout
, sb
->new_layout
);
151 printf(" New Layout : %s\n", c
?c
:"-unknown-");
153 if (sb
->level
== 10) {
154 printf(" New Layout : near=%d, %s=%d\n",
156 (sb
->new_layout
&0x10000)?"offset":"far",
157 (sb
->new_layout
>>8)&255);
160 if (sb
->new_chunk
!= sb
->chunk_size
)
161 printf(" New Chunksize : %d\n", sb
->new_chunk
);
165 printf(" Update Time : %.24s\n", ctime(&atime
));
166 printf(" State : %s\n",
167 (sb
->state
&(1<<MD_SB_CLEAN
))?"clean":"active");
168 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
))
169 printf("Internal Bitmap : present\n");
170 printf(" Active Devices : %d\n", sb
->active_disks
);
171 printf("Working Devices : %d\n", sb
->working_disks
);
172 printf(" Failed Devices : %d\n", sb
->failed_disks
);
173 printf(" Spare Devices : %d\n", sb
->spare_disks
);
174 if (calc_sb0_csum(sb
) == sb
->sb_csum
)
175 printf(" Checksum : %x - correct\n", sb
->sb_csum
);
177 printf(" Checksum : %x - expected %lx\n", sb
->sb_csum
, calc_sb0_csum(sb
));
178 printf(" Events : %d.%d\n", sb
->events_hi
, sb
->events_lo
);
180 if (sb
->level
== 5) {
181 c
= map_num(r5layout
, sb
->layout
);
182 printf(" Layout : %s\n", c
?c
:"-unknown-");
184 if (sb
->level
== 10) {
185 printf(" Layout : near=%d, %s=%d\n",
187 (sb
->layout
&0x10000)?"offset":"far",
188 (sb
->layout
>>8)&255);
196 printf(" Chunk Size : %dK\n", sb
->chunk_size
/1024);
199 printf(" Rounding : %dK\n", sb
->chunk_size
/1024);
204 printf(" Number Major Minor RaidDevice State\n");
205 for (d
= -1; d
<(signed int)(sb
->raid_disks
+sb
->spare_disks
); d
++) {
210 if (d
>=0) dp
= &sb
->disks
[d
];
211 else dp
= &sb
->this_disk
;
212 snprintf(nb
, sizeof(nb
), "%4d", d
);
213 printf("%4s %5d %5d %5d %5d ", d
< 0 ? "this" : nb
,
214 dp
->number
, dp
->major
, dp
->minor
, dp
->raid_disk
);
215 wonly
= dp
->state
& (1<<MD_DISK_WRITEMOSTLY
);
216 dp
->state
&= ~(1<<MD_DISK_WRITEMOSTLY
);
217 if (dp
->state
& (1<<MD_DISK_FAULTY
)) printf(" faulty");
218 if (dp
->state
& (1<<MD_DISK_ACTIVE
)) printf(" active");
219 if (dp
->state
& (1<<MD_DISK_SYNC
)) printf(" sync");
220 if (dp
->state
& (1<<MD_DISK_REMOVED
)) printf(" removed");
221 if (wonly
) printf(" write-mostly");
222 if (dp
->state
== 0) printf(" spare");
223 if ((dv
=map_dev(dp
->major
, dp
->minor
, 0)))
226 if (d
== -1) printf("\n");
230 static void brief_examine_super0(void *sbv
)
232 mdp_super_t
*sb
= sbv
;
233 char *c
=map_num(pers
, sb
->level
);
236 sprintf(devname
, "/dev/md%d", sb
->md_minor
);
238 printf("ARRAY %s level=%s num-devices=%d UUID=",
240 c
?c
:"-unknown-", sb
->raid_disks
);
241 if (sb
->minor_version
>= 90)
242 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
243 sb
->set_uuid2
, sb
->set_uuid3
);
245 printf("%08x", sb
->set_uuid0
);
249 static void detail_super0(void *sbv
, char *homehost
)
251 mdp_super_t
*sb
= sbv
;
253 if (sb
->minor_version
>= 90)
254 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
255 sb
->set_uuid2
, sb
->set_uuid3
);
257 printf("%08x", sb
->set_uuid0
);
260 void *hash
= sha1_buffer(homehost
,
263 if (memcmp(&sb
->set_uuid2
, hash
, 8)==0)
264 printf(" (local to host %s)", homehost
);
266 printf("\n Events : %d.%d\n\n", sb
->events_hi
, sb
->events_lo
);
269 static void brief_detail_super0(void *sbv
)
271 mdp_super_t
*sb
= sbv
;
273 if (sb
->minor_version
>= 90)
274 printf("%08x:%08x:%08x:%08x", sb
->set_uuid0
, sb
->set_uuid1
,
275 sb
->set_uuid2
, sb
->set_uuid3
);
277 printf("%08x", sb
->set_uuid0
);
281 static int match_home0(void *sbv
, char *homehost
)
283 mdp_super_t
*sb
= sbv
;
285 char *hash
= sha1_buffer(homehost
,
289 return (memcmp(&sb
->set_uuid2
, hash
, 8)==0);
292 static void uuid_from_super0(int uuid
[4], void * sbv
)
294 mdp_super_t
*super
= sbv
;
295 uuid
[0] = super
->set_uuid0
;
296 if (super
->minor_version
>= 90) {
297 uuid
[1] = super
->set_uuid1
;
298 uuid
[2] = super
->set_uuid2
;
299 uuid
[3] = super
->set_uuid3
;
307 static void getinfo_super0(struct mdinfo
*info
, void *sbv
)
309 mdp_super_t
*sb
= sbv
;
313 info
->array
.major_version
= sb
->major_version
;
314 info
->array
.minor_version
= sb
->minor_version
;
315 info
->array
.patch_version
= sb
->patch_version
;
316 info
->array
.raid_disks
= sb
->raid_disks
;
317 info
->array
.level
= sb
->level
;
318 info
->array
.layout
= sb
->layout
;
319 info
->array
.md_minor
= sb
->md_minor
;
320 info
->array
.ctime
= sb
->ctime
;
321 info
->array
.utime
= sb
->utime
;
322 info
->array
.chunk_size
= sb
->chunk_size
;
323 info
->array
.state
= sb
->state
;
324 info
->component_size
= sb
->size
*2;
326 info
->disk
.state
= sb
->this_disk
.state
;
327 info
->disk
.major
= sb
->this_disk
.major
;
328 info
->disk
.minor
= sb
->this_disk
.minor
;
329 info
->disk
.raid_disk
= sb
->this_disk
.raid_disk
;
330 info
->disk
.number
= sb
->this_disk
.number
;
332 info
->events
= md_event(sb
);
333 info
->data_offset
= 0;
335 uuid_from_super0(info
->uuid
, sbv
);
337 if (sb
->minor_version
> 90 && (sb
->reshape_position
+1) != 0) {
338 info
->reshape_active
= 1;
339 info
->reshape_progress
= sb
->reshape_position
;
340 info
->new_level
= sb
->new_level
;
341 info
->delta_disks
= sb
->delta_disks
;
342 info
->new_layout
= sb
->new_layout
;
343 info
->new_chunk
= sb
->new_chunk
;
345 info
->reshape_active
= 0;
347 sprintf(info
->name
, "%d", sb
->md_minor
);
348 /* work_disks is calculated rather than read directly */
349 for (i
=0; i
< MD_SB_DISKS
; i
++)
350 if ((sb
->disks
[i
].state
& (1<<MD_DISK_SYNC
)) &&
351 (sb
->disks
[i
].raid_disk
< info
->array
.raid_disks
) &&
352 (sb
->disks
[i
].state
& (1<<MD_DISK_ACTIVE
)) &&
353 !(sb
->disks
[i
].state
& (1<<MD_DISK_FAULTY
)))
355 info
->array
.working_disks
= working
;
359 static int update_super0(struct mdinfo
*info
, void *sbv
, char *update
,
360 char *devname
, int verbose
,
361 int uuid_set
, char *homehost
)
363 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
364 * For others, the return value is ignored.
367 mdp_super_t
*sb
= sbv
;
368 if (strcmp(update
, "sparc2.2")==0 ) {
369 /* 2.2 sparc put the events in the wrong place
370 * So we copy the tail of the superblock
371 * up 4 bytes before continuing
373 __u32
*sb32
= (__u32
*)sb
;
374 memcpy(sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7,
375 sb32
+MD_SB_GENERIC_CONSTANT_WORDS
+7+1,
376 (MD_SB_WORDS
- (MD_SB_GENERIC_CONSTANT_WORDS
+7+1))*4);
378 fprintf (stderr
, Name
": adjusting superblock of %s for 2.2/sparc compatability.\n",
381 if (strcmp(update
, "super-minor") ==0) {
382 sb
->md_minor
= info
->array
.md_minor
;
384 fprintf(stderr
, Name
": updating superblock of %s with minor number %d\n",
385 devname
, info
->array
.md_minor
);
387 if (strcmp(update
, "summaries") == 0) {
389 /* set nr_disks, active_disks, working_disks,
390 * failed_disks, spare_disks based on disks[]
391 * array in superblock.
392 * Also make sure extra slots aren't 'failed'
394 sb
->nr_disks
= sb
->active_disks
=
395 sb
->working_disks
= sb
->failed_disks
=
397 for (i
=0; i
< MD_SB_DISKS
; i
++)
398 if (sb
->disks
[i
].major
||
399 sb
->disks
[i
].minor
) {
400 int state
= sb
->disks
[i
].state
;
401 if (state
& (1<<MD_DISK_REMOVED
))
404 if (state
& (1<<MD_DISK_ACTIVE
))
406 if (state
& (1<<MD_DISK_FAULTY
))
412 } else if (i
>= sb
->raid_disks
&& sb
->disks
[i
].number
== 0)
413 sb
->disks
[i
].state
= 0;
415 if (strcmp(update
, "force-one")==0) {
416 /* Not enough devices for a working array, so
417 * bring this one up-to-date.
419 __u32 ehi
= sb
->events_hi
, elo
= sb
->events_lo
;
420 sb
->events_hi
= (info
->events
>>32) & 0xFFFFFFFF;
421 sb
->events_lo
= (info
->events
) & 0xFFFFFFFF;
422 if (sb
->events_hi
!= ehi
||
423 sb
->events_lo
!= elo
)
426 if (strcmp(update
, "force-array")==0) {
427 /* degraded array and 'force' requested, so
428 * maybe need to mark it 'clean'
430 if ((sb
->level
== 5 || sb
->level
== 4 || sb
->level
== 6) &&
431 (sb
->state
& (1 << MD_SB_CLEAN
)) == 0) {
432 /* need to force clean */
433 sb
->state
|= (1 << MD_SB_CLEAN
);
437 if (strcmp(update
, "assemble")==0) {
438 int d
= info
->disk
.number
;
439 int wonly
= sb
->disks
[d
].state
& (1<<MD_DISK_WRITEMOSTLY
);
440 if ((sb
->disks
[d
].state
& ~(1<<MD_DISK_WRITEMOSTLY
))
441 != info
->disk
.state
) {
442 sb
->disks
[d
].state
= info
->disk
.state
| wonly
;
446 if (strcmp(update
, "grow") == 0) {
447 sb
->raid_disks
= info
->array
.raid_disks
;
448 sb
->nr_disks
= info
->array
.nr_disks
;
449 sb
->active_disks
= info
->array
.active_disks
;
450 sb
->working_disks
= info
->array
.working_disks
;
451 memset(&sb
->disks
[info
->disk
.number
], 0, sizeof(sb
->disks
[0]));
452 sb
->disks
[info
->disk
.number
].number
= info
->disk
.number
;
453 sb
->disks
[info
->disk
.number
].major
= info
->disk
.major
;
454 sb
->disks
[info
->disk
.number
].minor
= info
->disk
.minor
;
455 sb
->disks
[info
->disk
.number
].raid_disk
= info
->disk
.raid_disk
;
456 sb
->disks
[info
->disk
.number
].state
= info
->disk
.state
;
457 if (sb
->this_disk
.number
== info
->disk
.number
)
458 sb
->this_disk
= sb
->disks
[info
->disk
.number
];
460 if (strcmp(update
, "resync") == 0) {
461 /* make sure resync happens */
462 sb
->state
&= ~(1<<MD_SB_CLEAN
);
465 if (strcmp(update
, "homehost") == 0 &&
469 info
->uuid
[0] = sb
->set_uuid0
;
470 info
->uuid
[1] = sb
->set_uuid1
;
472 if (strcmp(update
, "uuid") == 0) {
473 if (!uuid_set
&& homehost
) {
475 char *hash
= sha1_buffer(homehost
,
478 memcpy(info
->uuid
+2, hash
, 8);
480 sb
->set_uuid0
= info
->uuid
[0];
481 sb
->set_uuid1
= info
->uuid
[1];
482 sb
->set_uuid2
= info
->uuid
[2];
483 sb
->set_uuid3
= info
->uuid
[3];
484 if (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
485 struct bitmap_super_s
*bm
;
486 bm
= (struct bitmap_super_s
*)(sb
+1);
487 uuid_from_super0((int*)bm
->uuid
, sbv
);
490 if (strcmp(update
, "_reshape_progress")==0)
491 sb
->reshape_position
= info
->reshape_progress
;
493 sb
->sb_csum
= calc_sb0_csum(sb
);
498 * For verion-0 superblock, the homehost is 'stored' in the
499 * uuid. 8 bytes for a hash of the host leaving 8 bytes
500 * of random material.
501 * We use the first 8 bytes (64bits) of the sha1 of the
506 static int init_super0(struct supertype
*st
, void **sbp
, mdu_array_info_t
*info
,
507 unsigned long long size
, char *ignored_name
, char *homehost
)
509 mdp_super_t
*sb
= malloc(MD_SB_BYTES
+ sizeof(bitmap_super_t
));
512 memset(sb
, 0, MD_SB_BYTES
+ sizeof(bitmap_super_t
));
514 if (info
->major_version
== -1) {
515 /* zeroing the superblock */
520 spares
= info
->working_disks
- info
->active_disks
;
521 if (info
->raid_disks
+ spares
> MD_SB_DISKS
) {
522 fprintf(stderr
, Name
": too many devices requested: %d+%d > %d\n",
523 info
->raid_disks
, spares
, MD_SB_DISKS
);
527 rfd
= open("/dev/urandom", O_RDONLY
);
528 sb
->md_magic
= MD_SB_MAGIC
;
529 sb
->major_version
= 0;
530 sb
->minor_version
= 90;
531 sb
->patch_version
= 0;
532 sb
->gvalid_words
= 0; /* ignored */
533 if (rfd
< 0 || read(rfd
, &sb
->set_uuid0
, 4) != 4)
534 sb
->set_uuid0
= random();
536 sb
->level
= info
->level
;
537 if (size
!= info
->size
)
539 sb
->size
= info
->size
;
540 sb
->nr_disks
= info
->nr_disks
;
541 sb
->raid_disks
= info
->raid_disks
;
542 sb
->md_minor
= info
->md_minor
;
543 sb
->not_persistent
= 0;
544 if (rfd
< 0 || read(rfd
, &sb
->set_uuid1
, 12) != 12) {
545 sb
->set_uuid1
= random();
546 sb
->set_uuid2
= random();
547 sb
->set_uuid3
= random();
553 char *hash
= sha1_buffer(homehost
,
556 memcpy(&sb
->set_uuid2
, hash
, 8);
559 sb
->utime
= sb
->ctime
;
560 sb
->state
= info
->state
;
561 sb
->active_disks
= info
->active_disks
;
562 sb
->working_disks
= info
->working_disks
;
563 sb
->failed_disks
= info
->failed_disks
;
564 sb
->spare_disks
= info
->spare_disks
;
568 sb
->layout
= info
->layout
;
569 sb
->chunk_size
= info
->chunk_size
;
575 /* Add a device to the superblock being created */
576 static void add_to_super0(void *sbv
, mdu_disk_info_t
*dinfo
)
578 mdp_super_t
*sb
= sbv
;
579 mdp_disk_t
*dk
= &sb
->disks
[dinfo
->number
];
581 dk
->number
= dinfo
->number
;
582 dk
->major
= dinfo
->major
;
583 dk
->minor
= dinfo
->minor
;
584 dk
->raid_disk
= dinfo
->raid_disk
;
585 dk
->state
= dinfo
->state
;
588 static int store_super0(struct supertype
*st
, int fd
, void *sbv
)
591 unsigned long long dsize
;
592 unsigned long long offset
;
593 mdp_super_t
*super
= sbv
;
596 if (ioctl(fd
, BLKGETSIZE64
, &dsize
) != 0)
599 if (ioctl(fd
, BLKGETSIZE
, &size
))
602 dsize
= ((unsigned long long)size
)<<9;
605 if (dsize
< MD_RESERVED_SECTORS
*2*512)
608 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
612 if (lseek64(fd
, offset
, 0)< 0LL)
615 if (write(fd
, super
, sizeof(*super
)) != sizeof(*super
))
618 if (super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) {
619 struct bitmap_super_s
* bm
= (struct bitmap_super_s
*)(super
+1);
620 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
)
621 if (write(fd
, bm
, sizeof(*bm
)) != sizeof(*bm
))
629 static int write_init_super0(struct supertype
*st
, void *sbv
, mdu_disk_info_t
*dinfo
, char *devname
)
631 mdp_super_t
*sb
= sbv
;
632 int fd
= open(devname
, O_RDWR
|O_EXCL
);
636 fprintf(stderr
, Name
": Failed to open %s to write superblock\n", devname
);
640 sb
->disks
[dinfo
->number
].state
&= ~(1<<MD_DISK_FAULTY
);
642 sb
->this_disk
= sb
->disks
[dinfo
->number
];
643 sb
->sb_csum
= calc_sb0_csum(sb
);
644 rv
= store_super0(st
, fd
, sb
);
646 if (rv
== 0 && (sb
->state
& (1<<MD_SB_BITMAP_PRESENT
)))
647 rv
= st
->ss
->write_bitmap(st
, fd
, sbv
);
651 fprintf(stderr
, Name
": failed to write superblock to %s\n", devname
);
655 static int compare_super0(void **firstp
, void *secondv
)
659 * 0 same, or first was empty, and second was copied
660 * 1 second had wrong number
664 mdp_super_t
*first
= *firstp
;
665 mdp_super_t
*second
= secondv
;
667 int uuid1
[4], uuid2
[4];
668 if (second
->md_magic
!= MD_SB_MAGIC
)
671 first
= malloc(MD_SB_BYTES
+ sizeof(struct bitmap_super_s
));
672 memcpy(first
, second
, MD_SB_BYTES
+ sizeof(struct bitmap_super_s
));
677 uuid_from_super0(uuid1
, first
);
678 uuid_from_super0(uuid2
, second
);
679 if (!same_uuid(uuid1
, uuid2
, 0))
681 if (first
->major_version
!= second
->major_version
||
682 first
->minor_version
!= second
->minor_version
||
683 first
->patch_version
!= second
->patch_version
||
684 first
->gvalid_words
!= second
->gvalid_words
||
685 first
->ctime
!= second
->ctime
||
686 first
->level
!= second
->level
||
687 first
->size
!= second
->size
||
688 first
->raid_disks
!= second
->raid_disks
)
695 static int load_super0(struct supertype
*st
, int fd
, void **sbp
, char *devname
)
697 /* try to read in the superblock
700 * 1 on cannot get superblock
701 * 2 on superblock meaningless
704 unsigned long long dsize
;
705 unsigned long long offset
;
708 struct bitmap_super_s
*bsb
;
711 if (ioctl(fd
, BLKGETSIZE64
, &dsize
) != 0)
714 if (ioctl(fd
, BLKGETSIZE
, &size
)) {
716 fprintf(stderr
, Name
": cannot find device size for %s: %s\n",
717 devname
, strerror(errno
));
723 if (dsize
< MD_RESERVED_SECTORS
*2) {
725 fprintf(stderr
, Name
": %s is too small for md: size is %ld sectors.\n",
730 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
734 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
736 if (lseek64(fd
, offset
, 0)< 0LL) {
738 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
739 devname
, strerror(errno
));
743 super
= malloc(MD_SB_BYTES
+ sizeof(bitmap_super_t
));
745 if (read(fd
, super
, sizeof(*super
)) != MD_SB_BYTES
) {
747 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
753 if (st
->ss
&& st
->minor_version
== 9)
754 super0_swap_endian(super
);
756 if (super
->md_magic
!= MD_SB_MAGIC
) {
758 fprintf(stderr
, Name
": No super block found on %s (Expected magic %08x, got %08x)\n",
759 devname
, MD_SB_MAGIC
, super
->md_magic
);
764 if (super
->major_version
!= 0) {
766 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
767 devname
, super
->major_version
);
772 if (st
->ss
== NULL
) {
774 st
->minor_version
= 90;
775 st
->max_devs
= MD_SB_DISKS
;
778 /* Now check on the bitmap superblock */
779 if ((super
->state
& (1<<MD_SB_BITMAP_PRESENT
)) == 0)
781 /* Read the bitmap superblock and make sure it looks
782 * valid. If it doesn't clear the bit. An --assemble --force
783 * should get that written out.
785 if (read(fd
, super
+1, sizeof(struct bitmap_super_s
))
786 != sizeof(struct bitmap_super_s
))
789 uuid_from_super0(uuid
, super
);
790 bsb
= (struct bitmap_super_s
*)(super
+1);
791 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
792 memcmp(bsb
->uuid
, uuid
, 16) != 0)
797 super
->state
&= ~(1<<MD_SB_BITMAP_PRESENT
);
802 static struct supertype
*match_metadata_desc0(char *arg
)
804 struct supertype
*st
= malloc(sizeof(*st
));
808 st
->minor_version
= 90;
809 st
->max_devs
= MD_SB_DISKS
;
810 if (strcmp(arg
, "0") == 0 ||
811 strcmp(arg
, "0.90") == 0 ||
812 strcmp(arg
, "default") == 0
816 st
->minor_version
= 9; /* flag for 'byte-swapped' */
817 if (strcmp(arg
, "0.swap")==0)
824 static __u64
avail_size0(struct supertype
*st
, __u64 devsize
)
826 if (devsize
< MD_RESERVED_SECTORS
*2)
828 return MD_NEW_SIZE_SECTORS(devsize
);
831 static int add_internal_bitmap0(struct supertype
*st
, void *sbv
, int *chunkp
,
832 int delay
, int write_behind
,
833 unsigned long long size
, int may_change
,
837 * The bitmap comes immediately after the superblock and must be 60K in size
838 * at most. The default size is between 30K and 60K
840 * size is in sectors, chunk is in bytes !!!
842 unsigned long long bits
;
843 unsigned long long max_bits
= 60*1024*8;
844 unsigned long long min_chunk
;
846 mdp_super_t
*sb
= sbv
;
847 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + MD_SB_BYTES
);
850 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
851 bits
= (size
* 512) / min_chunk
+ 1;
852 while (bits
> max_bits
) {
858 else if (chunk
< min_chunk
)
859 return 0; /* chunk size too small */
861 sb
->state
|= (1<<MD_SB_BITMAP_PRESENT
);
863 memset(bms
, 0, sizeof(*bms
));
864 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
865 bms
->version
= __cpu_to_le32(major
);
866 uuid_from_super0((int*)bms
->uuid
, sb
);
867 bms
->chunksize
= __cpu_to_le32(chunk
);
868 bms
->daemon_sleep
= __cpu_to_le32(delay
);
869 bms
->sync_size
= __cpu_to_le64(size
);
870 bms
->write_behind
= __cpu_to_le32(write_behind
);
876 void locate_bitmap0(struct supertype
*st
, int fd
, void *sbv
)
878 unsigned long long dsize
;
880 unsigned long long offset
;
882 if (ioctl(fd
, BLKGETSIZE64
, &dsize
) != 0)
885 if (ioctl(fd
, BLKGETSIZE
, &size
))
888 dsize
= ((unsigned long long)size
)<<9;
891 if (dsize
< MD_RESERVED_SECTORS
*2)
894 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
898 offset
+= MD_SB_BYTES
;
900 lseek64(fd
, offset
, 0);
903 int write_bitmap0(struct supertype
*st
, int fd
, void *sbv
)
906 unsigned long long dsize
;
907 unsigned long long offset
;
908 mdp_super_t
*sb
= sbv
;
916 if (ioctl(fd
, BLKGETSIZE64
, &dsize
) != 0)
919 if (ioctl(fd
, BLKGETSIZE
, &size
))
922 dsize
= ((unsigned long long)size
)<<9;
925 if (dsize
< MD_RESERVED_SECTORS
*2)
928 offset
= MD_NEW_SIZE_SECTORS(dsize
>>9);
932 if (lseek64(fd
, offset
+ 4096, 0)< 0LL)
936 if (write(fd
, ((char*)sb
)+MD_SB_BYTES
, sizeof(bitmap_super_t
)) !=
937 sizeof(bitmap_super_t
))
939 towrite
= 64*1024 - MD_SB_BYTES
- sizeof(bitmap_super_t
);
940 memset(buf
, 0xff, sizeof(buf
));
941 while (towrite
> 0) {
945 n
= write(fd
, buf
, n
);
958 struct superswitch super0
= {
960 .examine_super
= examine_super0
,
961 .brief_examine_super
= brief_examine_super0
,
962 .detail_super
= detail_super0
,
963 .brief_detail_super
= brief_detail_super0
,
965 .match_home
= match_home0
,
966 .uuid_from_super
= uuid_from_super0
,
967 .getinfo_super
= getinfo_super0
,
968 .update_super
= update_super0
,
969 .init_super
= init_super0
,
970 .add_to_super
= add_to_super0
,
971 .store_super
= store_super0
,
972 .write_init_super
= write_init_super0
,
973 .compare_super
= compare_super0
,
974 .load_super
= load_super0
,
975 .match_metadata_desc
= match_metadata_desc0
,
976 .avail_size
= avail_size0
,
977 .add_internal_bitmap
= add_internal_bitmap0
,
978 .locate_bitmap
= locate_bitmap0
,
979 .write_bitmap
= write_bitmap0
,