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>
27 * The version-1 superblock :
28 * All numeric fields are little-endian.
30 * total size: 256 bytes plus 2 per device.
31 * 1K allows 384 devices.
33 struct mdp_superblock_1
{
34 /* constant array information - 128 bytes */
35 __u32 magic
; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
36 __u32 major_version
; /* 1 */
37 __u32 feature_map
; /* 0 for now */
38 __u32 pad0
; /* always set to 0 when writing */
40 __u8 set_uuid
[16]; /* user-space generated. */
41 char set_name
[32]; /* set and interpreted by user-space */
43 __u64 ctime
; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
44 __u32 level
; /* -4 (multipath), -1 (linear), 0,1,4,5 */
45 __u32 layout
; /* only for raid5 currently */
46 __u64 size
; /* used size of component devices, in 512byte sectors */
48 __u32 chunksize
; /* in 512byte sectors */
50 __u32 bitmap_offset
; /* sectors after start of superblock that bitmap starts
51 * NOTE: signed, so bitmap can be before superblock
52 * only meaningful of feature_map[0] is set.
55 /* These are only valid with feature bit '4' */
56 __u32 new_level
; /* new level we are reshaping to */
57 __u64 reshape_position
; /* next address in array-space for reshape */
58 __u32 delta_disks
; /* change in number of raid_disks */
59 __u32 new_layout
; /* new layout */
60 __u32 new_chunk
; /* new chunk size (bytes) */
61 __u8 pad1
[128-124]; /* set to 0 when written */
63 /* constant this-device information - 64 bytes */
64 __u64 data_offset
; /* sector start of data, often 0 */
65 __u64 data_size
; /* sectors in this device that can be used for data */
66 __u64 super_offset
; /* sector start of this superblock */
67 __u64 recovery_offset
;/* sectors before this offset (from data_offset) have been recovered */
68 __u32 dev_number
; /* permanent identifier of this device - not role in raid */
69 __u32 cnt_corrected_read
; /* number of read errors that were corrected by re-writing */
70 __u8 device_uuid
[16]; /* user-space setable, ignored by kernel */
71 __u8 devflags
; /* per-device flags. Only one defined...*/
72 #define WriteMostly1 1 /* mask for writemostly flag in above */
73 __u8 pad2
[64-57]; /* set to 0 when writing */
75 /* array state information - 64 bytes */
76 __u64 utime
; /* 40 bits second, 24 btes microseconds */
77 __u64 events
; /* incremented when superblock updated */
78 __u64 resync_offset
; /* data before this offset (from data_offset) known to be in sync */
79 __u32 sb_csum
; /* checksum upto dev_roles[max_dev] */
80 __u32 max_dev
; /* size of dev_roles[] array to consider */
81 __u8 pad3
[64-32]; /* set to 0 when writing */
83 /* device state information. Indexed by dev_number.
85 * Note there are no per-device state flags. State information is rolled
86 * into the 'roles' value. If a device is spare or faulty, then it doesn't
87 * have a meaningful role.
89 __u16 dev_roles
[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
92 struct misc_dev_info
{
96 /* feature_map bits */
97 #define MD_FEATURE_BITMAP_OFFSET 1
98 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
101 #define MD_FEATURE_RESHAPE_ACTIVE 4
103 #define MD_FEATURE_ALL (1|2|4)
106 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
108 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
110 unsigned int disk_csum
, csum
;
111 unsigned long long newcsum
;
112 int size
= sizeof(*sb
) + __le32_to_cpu(sb
->max_dev
)*2;
113 unsigned int *isuper
= (unsigned int*)sb
;
115 /* make sure I can count... */
116 if (offsetof(struct mdp_superblock_1
,data_offset
) != 128 ||
117 offsetof(struct mdp_superblock_1
, utime
) != 192 ||
118 sizeof(struct mdp_superblock_1
) != 256) {
119 fprintf(stderr
, "WARNING - superblock isn't sized correctly\n");
122 disk_csum
= sb
->sb_csum
;
125 for (; size
>=4; size
-= 4 ) {
126 newcsum
+= __le32_to_cpu(*isuper
);
131 newcsum
+= __le16_to_cpu(*(unsigned short*) isuper
);
133 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
134 sb
->sb_csum
= disk_csum
;
135 return __cpu_to_le32(csum
);
138 static char abuf
[4096+4096];
139 static int aread(int fd
, void *buf
, int len
)
142 * On devices with a 4K sector size, we need to read
143 * the full sector and copy relevant bits into
149 if (ioctl(fd
, BLKSSZGET
, &bsize
) != 0)
152 if (bsize
> 4096 || len
> 4096)
154 b
= (char*)(((long)(abuf
+4096))&~4095UL);
156 for (iosize
= 0; iosize
< len
; iosize
+= bsize
)
158 n
= read(fd
, b
, iosize
);
161 lseek(fd
, len
- n
, 1);
168 static int awrite(int fd
, void *buf
, int len
)
171 * On devices with a 4K sector size, we need to write
172 * the full sector. We pre-read if the sector is larger
174 * The address must be sector-aligned.
179 if (ioctl(fd
, BLKSSZGET
, &bsize
) != 0)
181 if (bsize
> 4096 || len
> 4096)
183 b
= (char*)(((long)(abuf
+4096))&~4095UL);
185 for (iosize
= 0; iosize
< len
; iosize
+= bsize
)
189 n
= read(fd
, b
, iosize
);
196 n
= write(fd
, b
, iosize
);
199 lseek(fd
, len
- n
, 1);
204 static void examine_super1(struct supertype
*st
, char *homehost
)
206 struct mdp_superblock_1
*sb
= st
->sb
;
213 int l
= homehost
? strlen(homehost
) : 0;
215 unsigned long long sb_offset
;
217 printf(" Magic : %08x\n", __le32_to_cpu(sb
->magic
));
218 printf(" Version : 1");
219 sb_offset
= __le64_to_cpu(sb
->super_offset
);
222 else if (sb_offset
<= 8)
226 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb
->feature_map
));
227 printf(" Array UUID : ");
228 for (i
=0; i
<16; i
++) {
229 if ((i
&3)==0 && i
!= 0) printf(":");
230 printf("%02x", sb
->set_uuid
[i
]);
233 printf(" Name : %.32s", sb
->set_name
);
234 if (l
> 0 && l
< 32 &&
235 sb
->set_name
[l
] == ':' &&
236 strncmp(sb
->set_name
, homehost
, l
) == 0)
237 printf(" (local to host %s)", homehost
);
239 atime
= __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
;
240 printf(" Creation Time : %.24s\n", ctime(&atime
));
241 c
=map_num(pers
, __le32_to_cpu(sb
->level
));
242 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
243 printf(" Raid Devices : %d\n", __le32_to_cpu(sb
->raid_disks
));
245 printf(" Avail Dev Size : %llu%s\n",
246 (unsigned long long)__le64_to_cpu(sb
->data_size
),
247 human_size(__le64_to_cpu(sb
->data_size
)<<9));
248 if (__le32_to_cpu(sb
->level
) > 0) {
250 switch(__le32_to_cpu(sb
->level
)) {
251 case 1: ddsks
=1;break;
253 case 5: ddsks
= __le32_to_cpu(sb
->raid_disks
)-1; break;
254 case 6: ddsks
= __le32_to_cpu(sb
->raid_disks
)-2; break;
256 layout
= __le32_to_cpu(sb
->layout
);
257 ddsks
= __le32_to_cpu(sb
->raid_disks
)
258 / (layout
&255) / ((layout
>>8)&255);
261 printf(" Array Size : %llu%s\n",
262 ddsks
*(unsigned long long)__le64_to_cpu(sb
->size
),
263 human_size(ddsks
*__le64_to_cpu(sb
->size
)<<9));
264 if (sb
->size
!= sb
->data_size
)
265 printf(" Used Dev Size : %llu%s\n",
266 (unsigned long long)__le64_to_cpu(sb
->size
),
267 human_size(__le64_to_cpu(sb
->size
)<<9));
270 printf(" Data Offset : %llu sectors\n",
271 (unsigned long long)__le64_to_cpu(sb
->data_offset
));
272 printf(" Super Offset : %llu sectors\n",
273 (unsigned long long)__le64_to_cpu(sb
->super_offset
));
274 if (__le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
)
275 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->recovery_offset
));
276 printf(" State : %s\n", (__le64_to_cpu(sb
->resync_offset
)+1)? "active":"clean");
277 printf(" Device UUID : ");
278 for (i
=0; i
<16; i
++) {
279 if ((i
&3)==0 && i
!= 0) printf(":");
280 printf("%02x", sb
->device_uuid
[i
]);
284 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
285 printf("Internal Bitmap : %ld sectors from superblock\n",
286 (long)(int32_t)__le32_to_cpu(sb
->bitmap_offset
));
288 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
289 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb
->reshape_position
)/2,
290 human_size(__le64_to_cpu(sb
->reshape_position
)<<9));
291 if (__le32_to_cpu(sb
->delta_disks
)) {
292 printf(" Delta Devices : %d", __le32_to_cpu(sb
->delta_disks
));
293 printf(" (%d->%d)\n",
294 __le32_to_cpu(sb
->raid_disks
)-__le32_to_cpu(sb
->delta_disks
),
295 __le32_to_cpu(sb
->raid_disks
));
296 if ((int)__le32_to_cpu(sb
->delta_disks
) < 0)
297 delta_extra
= -__le32_to_cpu(sb
->delta_disks
);
299 if (__le32_to_cpu(sb
->new_level
) != __le32_to_cpu(sb
->level
)) {
300 c
= map_num(pers
, __le32_to_cpu(sb
->new_level
));
301 printf(" New Level : %s\n", c
?c
:"-unknown-");
303 if (__le32_to_cpu(sb
->new_layout
) != __le32_to_cpu(sb
->layout
)) {
304 if (__le32_to_cpu(sb
->level
) == 5) {
305 c
= map_num(r5layout
, __le32_to_cpu(sb
->new_layout
));
306 printf(" New Layout : %s\n", c
?c
:"-unknown-");
308 if (__le32_to_cpu(sb
->level
) == 6) {
309 c
= map_num(r6layout
, __le32_to_cpu(sb
->new_layout
));
310 printf(" New Layout : %s\n", c
?c
:"-unknown-");
312 if (__le32_to_cpu(sb
->level
) == 10) {
313 printf(" New Layout :");
314 print_r10_layout(__le32_to_cpu(sb
->new_layout
));
318 if (__le32_to_cpu(sb
->new_chunk
) != __le32_to_cpu(sb
->chunksize
))
319 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb
->new_chunk
)/2);
324 if (sb
->devflags
& WriteMostly1
)
325 printf(" write-mostly");
329 atime
= __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
;
330 printf(" Update Time : %.24s\n", ctime(&atime
));
332 if (calc_sb_1_csum(sb
) == sb
->sb_csum
)
333 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb
->sb_csum
));
335 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb
->sb_csum
),
336 __le32_to_cpu(calc_sb_1_csum(sb
)));
337 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb
->events
));
339 if (__le32_to_cpu(sb
->level
) == 5) {
340 c
= map_num(r5layout
, __le32_to_cpu(sb
->layout
));
341 printf(" Layout : %s\n", c
?c
:"-unknown-");
343 if (__le32_to_cpu(sb
->level
) == 6) {
344 c
= map_num(r6layout
, __le32_to_cpu(sb
->layout
));
345 printf(" Layout : %s\n", c
?c
:"-unknown-");
347 if (__le32_to_cpu(sb
->level
) == 10) {
348 int lo
= __le32_to_cpu(sb
->layout
);
350 print_r10_layout(lo
);
353 switch(__le32_to_cpu(sb
->level
)) {
359 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
362 printf(" Rounding : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
368 /* This turns out to just be confusing */
369 printf(" Array Slot : %d (", __le32_to_cpu(sb
->dev_number
));
370 for (i
= __le32_to_cpu(sb
->max_dev
); i
> 0 ; i
--)
371 if (__le16_to_cpu(sb
->dev_roles
[i
-1]) != 0xffff)
373 for (d
=0; d
< i
; d
++) {
374 int role
= __le16_to_cpu(sb
->dev_roles
[d
]);
376 if (role
== 0xffff) printf("empty");
377 else if(role
== 0xfffe) printf("failed");
378 else printf("%d", role
);
382 printf(" Device Role : ");
383 d
= __le32_to_cpu(sb
->dev_number
);
384 if (d
< __le32_to_cpu(sb
->max_dev
))
385 role
= __le16_to_cpu(sb
->dev_roles
[d
]);
391 printf("Active device %d\n", role
);
393 printf(" Array State : ");
394 for (d
=0; d
<__le32_to_cpu(sb
->raid_disks
) + delta_extra
; d
++) {
397 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
398 unsigned int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
402 if (cnt
> 1) printf("?");
403 else if (cnt
== 1) printf("A");
407 /* This is confusing too */
409 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
410 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
414 if (faulty
) printf(" %d failed", faulty
);
416 printf(" ('A' == active, '.' == missing)");
421 static void brief_examine_super1(struct supertype
*st
, int verbose
)
423 struct mdp_superblock_1
*sb
= st
->sb
;
425 unsigned long long sb_offset
;
427 char *c
=map_num(pers
, __le32_to_cpu(sb
->level
));
429 nm
= strchr(sb
->set_name
, ':');
432 else if (sb
->set_name
[0])
437 printf("ARRAY%s%s", nm
? " /dev/md/":"", nm
);
439 printf(" level=%s", c
);
440 sb_offset
= __le64_to_cpu(sb
->super_offset
);
442 printf(" metadata=1.1 ");
443 else if (sb_offset
<= 8)
444 printf(" metadata=1.2 ");
446 printf(" metadata=1.0 ");
448 printf("num-devices=%d ", __le32_to_cpu(sb
->raid_disks
));
450 for (i
=0; i
<16; i
++) {
451 if ((i
&3)==0 && i
!= 0) printf(":");
452 printf("%02x", sb
->set_uuid
[i
]);
455 printf(" name=%.32s", sb
->set_name
);
459 static void export_examine_super1(struct supertype
*st
)
461 struct mdp_superblock_1
*sb
= st
->sb
;
465 printf("MD_LEVEL=%s\n", map_num(pers
, __le32_to_cpu(sb
->level
)));
466 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb
->raid_disks
));
468 if (sb
->set_name
[i
] == '\n' ||
469 sb
->set_name
[i
] == '\0') {
474 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
476 for (i
=0; i
<16; i
++) {
477 if ((i
&3)==0 && i
!= 0) printf(":");
478 printf("%02x", sb
->set_uuid
[i
]);
481 printf("MD_UPDATE_TIME=%llu\n",
482 __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
);
483 printf("MD_DEV_UUID=");
484 for (i
=0; i
<16; i
++) {
485 if ((i
&3)==0 && i
!= 0) printf(":");
486 printf("%02x", sb
->device_uuid
[i
]);
489 printf("MD_EVENTS=%llu\n",
490 (unsigned long long)__le64_to_cpu(sb
->events
));
493 static void detail_super1(struct supertype
*st
, char *homehost
)
495 struct mdp_superblock_1
*sb
= st
->sb
;
497 int l
= homehost
? strlen(homehost
) : 0;
499 printf(" Name : %.32s", sb
->set_name
);
500 if (l
> 0 && l
< 32 &&
501 sb
->set_name
[l
] == ':' &&
502 strncmp(sb
->set_name
, homehost
, l
) == 0)
503 printf(" (local to host %s)", homehost
);
504 printf("\n UUID : ");
505 for (i
=0; i
<16; i
++) {
506 if ((i
&3)==0 && i
!= 0) printf(":");
507 printf("%02x", sb
->set_uuid
[i
]);
509 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb
->events
));
512 static void brief_detail_super1(struct supertype
*st
)
514 struct mdp_superblock_1
*sb
= st
->sb
;
518 printf(" name=%.32s", sb
->set_name
);
520 for (i
=0; i
<16; i
++) {
521 if ((i
&3)==0 && i
!= 0) printf(":");
522 printf("%02x", sb
->set_uuid
[i
]);
526 static void export_detail_super1(struct supertype
*st
)
528 struct mdp_superblock_1
*sb
= st
->sb
;
533 if (sb
->set_name
[i
] == '\n' ||
534 sb
->set_name
[i
] == '\0') {
539 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
544 static int match_home1(struct supertype
*st
, char *homehost
)
546 struct mdp_superblock_1
*sb
= st
->sb
;
547 int l
= homehost
? strlen(homehost
) : 0;
549 return (l
> 0 && l
< 32 &&
550 sb
->set_name
[l
] == ':' &&
551 strncmp(sb
->set_name
, homehost
, l
) == 0);
554 static void uuid_from_super1(struct supertype
*st
, int uuid
[4])
556 struct mdp_superblock_1
*super
= st
->sb
;
557 char *cuuid
= (char*)uuid
;
560 cuuid
[i
] = super
->set_uuid
[i
];
563 static void getinfo_super1(struct supertype
*st
, struct mdinfo
*info
, char *map
)
565 struct mdp_superblock_1
*sb
= st
->sb
;
569 unsigned int map_disks
= info
->array
.raid_disks
;
571 memset(info
, 0, sizeof(*info
));
572 info
->array
.major_version
= 1;
573 info
->array
.minor_version
= st
->minor_version
;
574 info
->array
.patch_version
= 0;
575 info
->array
.raid_disks
= __le32_to_cpu(sb
->raid_disks
);
576 info
->array
.level
= __le32_to_cpu(sb
->level
);
577 info
->array
.layout
= __le32_to_cpu(sb
->layout
);
578 info
->array
.md_minor
= -1;
579 info
->array
.ctime
= __le64_to_cpu(sb
->ctime
);
580 info
->array
.utime
= __le64_to_cpu(sb
->utime
);
581 info
->array
.chunk_size
= __le32_to_cpu(sb
->chunksize
)*512;
583 (__le64_to_cpu(sb
->resync_offset
) == MaxSector
)
586 info
->data_offset
= __le64_to_cpu(sb
->data_offset
);
587 info
->component_size
= __le64_to_cpu(sb
->size
);
588 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET
))
589 info
->bitmap_offset
= __le32_to_cpu(sb
->bitmap_offset
);
591 info
->disk
.major
= 0;
592 info
->disk
.minor
= 0;
593 info
->disk
.number
= __le32_to_cpu(sb
->dev_number
);
594 if (__le32_to_cpu(sb
->dev_number
) >= __le32_to_cpu(sb
->max_dev
) ||
595 __le32_to_cpu(sb
->max_dev
) > 512)
598 role
= __le16_to_cpu(sb
->dev_roles
[__le32_to_cpu(sb
->dev_number
)]);
600 info
->disk
.raid_disk
= -1;
603 info
->disk
.state
= 0; /* spare: not active, not sync, not faulty */
606 info
->disk
.state
= 1; /* faulty */
609 info
->disk
.state
= 6; /* active and in sync */
610 info
->disk
.raid_disk
= role
;
612 if (sb
->devflags
& WriteMostly1
)
613 info
->disk
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
614 info
->events
= __le64_to_cpu(sb
->events
);
615 sprintf(info
->text_version
, "1.%d", st
->minor_version
);
616 info
->safe_mode_delay
= 200;
618 memcpy(info
->uuid
, sb
->set_uuid
, 16);
620 strncpy(info
->name
, sb
->set_name
, 32);
623 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET
))
624 info
->recovery_start
= __le32_to_cpu(sb
->recovery_offset
);
626 info
->recovery_start
= MaxSector
;
628 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
629 info
->reshape_active
= 1;
630 info
->reshape_progress
= __le64_to_cpu(sb
->reshape_position
);
631 info
->new_level
= __le32_to_cpu(sb
->new_level
);
632 info
->delta_disks
= __le32_to_cpu(sb
->delta_disks
);
633 info
->new_layout
= __le32_to_cpu(sb
->new_layout
);
634 info
->new_chunk
= __le32_to_cpu(sb
->new_chunk
)<<9;
635 if (info
->delta_disks
< 0)
636 info
->array
.raid_disks
-= info
->delta_disks
;
638 info
->reshape_active
= 0;
640 info
->recovery_blocked
= info
->reshape_active
;
643 for (i
=0; i
<map_disks
; i
++)
645 for (i
= 0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
646 role
= __le16_to_cpu(sb
->dev_roles
[i
]);
647 if (/*role == 0xFFFF || */role
< (unsigned) info
->array
.raid_disks
) {
649 if (map
&& role
< map_disks
)
654 info
->array
.working_disks
= working
;
657 static struct mdinfo
*container_content1(struct supertype
*st
, char *subarray
)
664 info
= malloc(sizeof(*info
));
665 getinfo_super1(st
, info
, NULL
);
669 static int update_super1(struct supertype
*st
, struct mdinfo
*info
,
671 char *devname
, int verbose
,
672 int uuid_set
, char *homehost
)
674 /* NOTE: for 'assemble' and 'force' we need to return non-zero
675 * if any change was made. For others, the return value is
679 struct mdp_superblock_1
*sb
= st
->sb
;
681 if (strcmp(update
, "force-one")==0) {
682 /* Not enough devices for a working array,
683 * so bring this one up-to-date
685 if (sb
->events
!= __cpu_to_le64(info
->events
))
687 sb
->events
= __cpu_to_le64(info
->events
);
688 } else if (strcmp(update
, "force-array")==0) {
689 /* Degraded array and 'force' requests to
690 * maybe need to mark it 'clean'.
692 switch(__le32_to_cpu(sb
->level
)) {
693 case 5: case 4: case 6:
694 /* need to force clean */
695 if (sb
->resync_offset
!= MaxSector
)
697 sb
->resync_offset
= MaxSector
;
699 } else if (strcmp(update
, "assemble")==0) {
700 int d
= info
->disk
.number
;
702 if (info
->disk
.state
== 6)
703 want
= info
->disk
.raid_disk
;
706 if (sb
->dev_roles
[d
] != __cpu_to_le16(want
)) {
707 sb
->dev_roles
[d
] = __cpu_to_le16(want
);
710 if (info
->reshape_active
&&
711 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
712 info
->delta_disks
>= 0 &&
713 info
->reshape_progress
< __le64_to_cpu(sb
->reshape_position
)) {
714 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
717 if (info
->reshape_active
&&
718 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
719 info
->delta_disks
< 0 &&
720 info
->reshape_progress
> __le64_to_cpu(sb
->reshape_position
)) {
721 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
724 } else if (strcmp(update
, "linear-grow-new") == 0) {
727 unsigned int max
= __le32_to_cpu(sb
->max_dev
);
729 for (i
=0 ; i
< max
; i
++)
730 if (__le16_to_cpu(sb
->dev_roles
[i
]) >= 0xfffe)
732 sb
->dev_number
= __cpu_to_le32(i
);
733 info
->disk
.number
= i
;
734 if (max
>= __le32_to_cpu(sb
->max_dev
))
735 sb
->max_dev
= __cpu_to_le32(max
+1);
737 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
738 read(rfd
, sb
->device_uuid
, 16) != 16) {
739 __u32 r
[4] = {random(), random(), random(), random()};
740 memcpy(sb
->device_uuid
, r
, 16);
746 __cpu_to_le16(info
->disk
.raid_disk
);
748 fd
= open(devname
, O_RDONLY
);
750 unsigned long long ds
;
751 get_dev_size(fd
, devname
, &ds
);
754 if (__le64_to_cpu(sb
->super_offset
) <
755 __le64_to_cpu(sb
->data_offset
)) {
756 sb
->data_size
= __cpu_to_le64(
757 ds
- __le64_to_cpu(sb
->data_offset
));
760 ds
&= ~(unsigned long long)(4*2-1);
761 sb
->super_offset
= __cpu_to_le64(ds
);
762 sb
->data_size
= __cpu_to_le64(
763 ds
- __le64_to_cpu(sb
->data_offset
));
766 } else if (strcmp(update
, "linear-grow-update") == 0) {
767 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
768 sb
->dev_roles
[info
->disk
.number
] =
769 __cpu_to_le16(info
->disk
.raid_disk
);
770 } else if (strcmp(update
, "resync") == 0) {
771 /* make sure resync happens */
772 sb
->resync_offset
= 0ULL;
773 } else if (strcmp(update
, "uuid") == 0) {
774 copy_uuid(sb
->set_uuid
, info
->uuid
, super1
.swapuuid
);
776 if (__le32_to_cpu(sb
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
777 struct bitmap_super_s
*bm
;
778 bm
= (struct bitmap_super_s
*)(st
->sb
+1024);
779 memcpy(bm
->uuid
, sb
->set_uuid
, 16);
781 } else if (strcmp(update
, "no-bitmap") == 0) {
782 sb
->feature_map
&= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
783 } else if (strcmp(update
, "homehost") == 0 &&
787 c
= strchr(sb
->set_name
, ':');
789 strncpy(info
->name
, c
+1, 31 - (c
-sb
->set_name
));
791 strncpy(info
->name
, sb
->set_name
, 32);
793 } else if (strcmp(update
, "name") == 0) {
794 if (info
->name
[0] == 0)
795 sprintf(info
->name
, "%d", info
->array
.md_minor
);
796 memset(sb
->set_name
, 0, sizeof(sb
->set_name
));
798 strchr(info
->name
, ':') == NULL
&&
799 strlen(homehost
)+1+strlen(info
->name
) < 32) {
800 strcpy(sb
->set_name
, homehost
);
801 strcat(sb
->set_name
, ":");
802 strcat(sb
->set_name
, info
->name
);
804 strcpy(sb
->set_name
, info
->name
);
805 } else if (strcmp(update
, "devicesize") == 0 &&
806 __le64_to_cpu(sb
->super_offset
) <
807 __le64_to_cpu(sb
->data_offset
)) {
808 /* set data_size to device size less data_offset */
809 struct misc_dev_info
*misc
= (struct misc_dev_info
*)
810 (st
->sb
+ 1024 + 512);
811 printf("Size was %llu\n", (unsigned long long)
812 __le64_to_cpu(sb
->data_size
));
813 sb
->data_size
= __cpu_to_le64(
814 misc
->device_size
- __le64_to_cpu(sb
->data_offset
));
815 printf("Size is %llu\n", (unsigned long long)
816 __le64_to_cpu(sb
->data_size
));
817 } else if (strcmp(update
, "_reshape_progress")==0)
818 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
819 else if (strcmp(update
, "writemostly")==0)
820 sb
->devflags
|= WriteMostly1
;
821 else if (strcmp(update
, "readwrite")==0)
822 sb
->devflags
&= ~WriteMostly1
;
826 sb
->sb_csum
= calc_sb_1_csum(sb
);
830 static int init_super1(struct supertype
*st
, mdu_array_info_t
*info
,
831 unsigned long long size
, char *name
, char *homehost
, int *uuid
)
833 struct mdp_superblock_1
*sb
;
838 if (posix_memalign((void**)&sb
, 512, (1024 + 512 +
839 sizeof(struct misc_dev_info
))) != 0) {
841 ": %s could not allocate superblock\n", __func__
);
848 /* zeroing superblock */
852 spares
= info
->working_disks
- info
->active_disks
;
853 if (info
->raid_disks
+ spares
> 384) {
854 fprintf(stderr
, Name
": too many devices requested: %d+%d > %d\n",
855 info
->raid_disks
, spares
, 384);
859 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
860 sb
->major_version
= __cpu_to_le32(1);
865 copy_uuid(sb
->set_uuid
, uuid
, super1
.swapuuid
);
867 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
868 read(rfd
, sb
->set_uuid
, 16) != 16) {
869 __u32 r
[4] = {random(), random(), random(), random()};
870 memcpy(sb
->set_uuid
, r
, 16);
872 if (rfd
>= 0) close(rfd
);
875 if (name
== NULL
|| *name
== 0) {
876 sprintf(defname
, "%d", info
->md_minor
);
879 memset(sb
->set_name
, 0, 32);
881 strchr(name
, ':')== NULL
&&
882 strlen(homehost
)+1+strlen(name
) < 32) {
883 strcpy(sb
->set_name
, homehost
);
884 strcat(sb
->set_name
, ":");
885 strcat(sb
->set_name
, name
);
887 strcpy(sb
->set_name
, name
);
889 sb
->ctime
= __cpu_to_le64((unsigned long long)time(0));
890 sb
->level
= __cpu_to_le32(info
->level
);
891 sb
->layout
= __cpu_to_le32(info
->layout
);
892 sb
->size
= __cpu_to_le64(size
*2ULL);
893 sb
->chunksize
= __cpu_to_le32(info
->chunk_size
>>9);
894 sb
->raid_disks
= __cpu_to_le32(info
->raid_disks
);
896 sb
->data_offset
= __cpu_to_le64(0);
897 sb
->data_size
= __cpu_to_le64(0);
898 sb
->super_offset
= __cpu_to_le64(0);
899 sb
->recovery_offset
= __cpu_to_le64(0);
901 sb
->utime
= sb
->ctime
;
902 sb
->events
= __cpu_to_le64(1);
903 if (info
->state
& (1<<MD_SB_CLEAN
))
904 sb
->resync_offset
= MaxSector
;
906 sb
->resync_offset
= 0;
907 sb
->max_dev
= __cpu_to_le32((1024- sizeof(struct mdp_superblock_1
))/
908 sizeof(sb
->dev_roles
[0]));
909 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
911 memset(sb
->dev_roles
, 0xff, 1024 - sizeof(struct mdp_superblock_1
));
919 mdu_disk_info_t disk
;
920 struct devinfo
*next
;
923 /* Add a device to the superblock being created */
924 static int add_to_super1(struct supertype
*st
, mdu_disk_info_t
*dk
,
925 int fd
, char *devname
)
927 struct mdp_superblock_1
*sb
= st
->sb
;
928 __u16
*rp
= sb
->dev_roles
+ dk
->number
;
929 struct devinfo
*di
, **dip
;
931 if ((dk
->state
& 6) == 6) /* active, sync */
932 *rp
= __cpu_to_le16(dk
->raid_disk
);
933 else if ((dk
->state
& ~2) == 0) /* active or idle -> spare */
938 if (dk
->number
>= (int)__le32_to_cpu(sb
->max_dev
) &&
939 __le32_to_cpu(sb
->max_dev
) < 384)
940 sb
->max_dev
= __cpu_to_le32(dk
->number
+1);
942 sb
->dev_number
= __cpu_to_le32(dk
->number
);
943 sb
->devflags
= 0; /* don't copy another disks flags */
944 sb
->sb_csum
= calc_sb_1_csum(sb
);
946 dip
= (struct devinfo
**)&st
->info
;
949 di
= malloc(sizeof(struct devinfo
));
951 di
->devname
= devname
;
960 static void locate_bitmap1(struct supertype
*st
, int fd
);
962 static int store_super1(struct supertype
*st
, int fd
)
964 struct mdp_superblock_1
*sb
= st
->sb
;
965 unsigned long long sb_offset
;
967 unsigned long long dsize
;
969 if (!get_dev_size(fd
, NULL
, &dsize
))
978 * Calculate the position of the superblock.
979 * It is always aligned to a 4K boundary and
980 * depending on minor_version, it can be:
981 * 0: At least 8K, but less than 12K, from end of device
982 * 1: At start of device
983 * 2: 4K from start of device.
985 switch(st
->minor_version
) {
989 sb_offset
&= ~(4*2-1);
1003 if (sb_offset
!= __le64_to_cpu(sb
->super_offset
) &&
1004 0 != __le64_to_cpu(sb
->super_offset
)
1006 fprintf(stderr
, Name
": internal error - sb_offset is wrong\n");
1010 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL)
1013 sbsize
= sizeof(*sb
) + 2 * __le32_to_cpu(sb
->max_dev
);
1014 sbsize
= (sbsize
+511)&(~511UL);
1016 if (awrite(fd
, sb
, sbsize
) != sbsize
)
1019 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
1020 struct bitmap_super_s
*bm
= (struct bitmap_super_s
*)
1022 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
) {
1023 locate_bitmap1(st
, fd
);
1024 if (awrite(fd
, bm
, sizeof(*bm
)) !=
1033 static int load_super1(struct supertype
*st
, int fd
, char *devname
);
1035 static unsigned long choose_bm_space(unsigned long devsize
)
1037 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1038 * if bigger than 200Gig, save 128k
1039 * NOTE: result must be multiple of 4K else bad things happen
1040 * on 4K-sector devices.
1042 if (devsize
< 64*2) return 0;
1043 if (devsize
- 64*2 >= 200*1024*1024*2)
1045 if (devsize
- 4*2 > 8*1024*1024*2)
1050 static void free_super1(struct supertype
*st
);
1053 static int write_init_super1(struct supertype
*st
)
1055 struct mdp_superblock_1
*sb
= st
->sb
;
1056 struct supertype
*refst
;
1059 unsigned long long bm_space
;
1060 unsigned long long reserved
;
1062 unsigned long long dsize
, array_size
;
1063 unsigned long long sb_offset
;
1065 for (di
= st
->info
; di
&& ! rv
; di
= di
->next
) {
1066 if (di
->disk
.state
== 1)
1071 while (Kill(di
->devname
, NULL
, 0, 1, 1) == 0)
1074 sb
->dev_number
= __cpu_to_le32(di
->disk
.number
);
1075 if (di
->disk
.state
& (1<<MD_DISK_WRITEMOSTLY
))
1076 sb
->devflags
|= WriteMostly1
;
1078 sb
->devflags
&= ~WriteMostly1
;
1080 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
1081 read(rfd
, sb
->device_uuid
, 16) != 16) {
1082 __u32 r
[4] = {random(), random(), random(), random()};
1083 memcpy(sb
->device_uuid
, r
, 16);
1090 refst
= dup_super(st
);
1091 if (load_super1(refst
, di
->fd
, NULL
)==0) {
1092 struct mdp_superblock_1
*refsb
= refst
->sb
;
1094 memcpy(sb
->device_uuid
, refsb
->device_uuid
, 16);
1095 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16)==0) {
1096 /* same array, so preserve events and
1098 sb
->events
= refsb
->events
;
1099 /* bugs in 2.6.17 and earlier mean the
1100 * dev_number chosen in Manage must be preserved
1102 if (get_linux_version() >= 2006018)
1103 sb
->dev_number
= refsb
->dev_number
;
1109 if (!get_dev_size(di
->fd
, NULL
, &dsize
)) {
1123 * Calculate the position of the superblock.
1124 * It is always aligned to a 4K boundary and
1125 * depending on minor_version, it can be:
1126 * 0: At least 8K, but less than 12K, from end of device
1127 * 1: At start of device
1128 * 2: 4K from start of device.
1129 * Depending on the array size, we might leave extra space
1132 array_size
= __le64_to_cpu(sb
->size
);
1133 /* work out how much space we left for a bitmap */
1134 bm_space
= choose_bm_space(array_size
);
1136 switch(st
->minor_version
) {
1140 sb_offset
&= ~(4*2-1);
1141 sb
->super_offset
= __cpu_to_le64(sb_offset
);
1142 sb
->data_offset
= __cpu_to_le64(0);
1143 if (sb_offset
< array_size
+ bm_space
)
1144 bm_space
= sb_offset
- array_size
;
1145 sb
->data_size
= __cpu_to_le64(sb_offset
- bm_space
);
1148 sb
->super_offset
= __cpu_to_le64(0);
1149 reserved
= bm_space
+ 4*2;
1150 /* Try for multiple of 1Meg so it is nicely aligned */
1151 #define ONE_MEG (2*1024)
1152 reserved
= ((reserved
+ ONE_MEG
-1)/ONE_MEG
) * ONE_MEG
;
1153 if (reserved
+ __le64_to_cpu(sb
->size
) > dsize
)
1154 reserved
= dsize
- __le64_to_cpu(sb
->size
);
1155 /* force 4K alignment */
1158 sb
->data_offset
= __cpu_to_le64(reserved
);
1159 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1163 sb
->super_offset
= __cpu_to_le64(4*2);
1164 if (4*2 + 4*2 + bm_space
+ __le64_to_cpu(sb
->size
)
1166 bm_space
= dsize
- __le64_to_cpu(sb
->size
)
1169 reserved
= bm_space
+ 4*2 + 4*2;
1170 /* Try for multiple of 1Meg so it is nicely aligned */
1171 #define ONE_MEG (2*1024)
1172 reserved
= ((reserved
+ ONE_MEG
-1)/ONE_MEG
) * ONE_MEG
;
1173 if (reserved
+ __le64_to_cpu(sb
->size
) > dsize
)
1174 reserved
= dsize
- __le64_to_cpu(sb
->size
);
1175 /* force 4K alignment */
1178 sb
->data_offset
= __cpu_to_le64(reserved
);
1179 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1182 fprintf(stderr
, Name
": Failed to write invalid "
1183 "metadata format 1.%i to %s\n",
1184 st
->minor_version
, di
->devname
);
1190 sb
->sb_csum
= calc_sb_1_csum(sb
);
1191 rv
= store_super1(st
, di
->fd
);
1192 if (rv
== 0 && (__le32_to_cpu(sb
->feature_map
) & 1))
1193 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
1199 fprintf(stderr
, Name
": Failed to write metadata to %s\n",
1206 static int compare_super1(struct supertype
*st
, struct supertype
*tst
)
1210 * 0 same, or first was empty, and second was copied
1211 * 1 second had wrong number
1213 * 3 wrong other info
1215 struct mdp_superblock_1
*first
= st
->sb
;
1216 struct mdp_superblock_1
*second
= tst
->sb
;
1218 if (second
->magic
!= __cpu_to_le32(MD_SB_MAGIC
))
1220 if (second
->major_version
!= __cpu_to_le32(1))
1224 if (posix_memalign((void**)&first
, 512,
1226 sizeof(struct misc_dev_info
)) != 0) {
1227 fprintf(stderr
, Name
1228 ": %s could not allocate superblock\n", __func__
);
1231 memcpy(first
, second
, 1024 + 512 +
1232 sizeof(struct misc_dev_info
));
1236 if (memcmp(first
->set_uuid
, second
->set_uuid
, 16)!= 0)
1239 if (first
->ctime
!= second
->ctime
||
1240 first
->level
!= second
->level
||
1241 first
->layout
!= second
->layout
||
1242 first
->size
!= second
->size
||
1243 first
->chunksize
!= second
->chunksize
||
1244 first
->raid_disks
!= second
->raid_disks
)
1249 static int load_super1(struct supertype
*st
, int fd
, char *devname
)
1251 unsigned long long dsize
;
1252 unsigned long long sb_offset
;
1253 struct mdp_superblock_1
*super
;
1255 struct bitmap_super_s
*bsb
;
1256 struct misc_dev_info
*misc
;
1260 if (st
->ss
== NULL
|| st
->minor_version
== -1) {
1262 struct supertype tst
;
1263 __u64 bestctime
= 0;
1264 /* guess... choose latest ctime */
1265 memset(&tst
, 0, sizeof(tst
));
1267 for (tst
.minor_version
= 0; tst
.minor_version
<= 2 ; tst
.minor_version
++) {
1268 switch(load_super1(&tst
, fd
, devname
)) {
1269 case 0: super
= tst
.sb
;
1270 if (bestvers
== -1 ||
1271 bestctime
< __le64_to_cpu(super
->ctime
)) {
1272 bestvers
= tst
.minor_version
;
1273 bestctime
= __le64_to_cpu(super
->ctime
);
1278 case 1: return 1; /*bad device */
1279 case 2: break; /* bad, try next */
1282 if (bestvers
!= -1) {
1284 tst
.minor_version
= bestvers
;
1287 rv
= load_super1(&tst
, fd
, devname
);
1294 if (!get_dev_size(fd
, devname
, &dsize
))
1300 fprintf(stderr
, Name
": %s is too small for md: size is %llu sectors.\n",
1306 * Calculate the position of the superblock.
1307 * It is always aligned to a 4K boundary and
1308 * depending on minor_version, it can be:
1309 * 0: At least 8K, but less than 12K, from end of device
1310 * 1: At start of device
1311 * 2: 4K from start of device.
1313 switch(st
->minor_version
) {
1317 sb_offset
&= ~(4*2-1);
1329 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
1332 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL) {
1334 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
1335 devname
, strerror(errno
));
1339 if (posix_memalign((void**)&super
, 512,
1341 sizeof(struct misc_dev_info
)) != 0) {
1342 fprintf(stderr
, Name
": %s could not allocate superblock\n",
1347 if (aread(fd
, super
, 1024) != 1024) {
1349 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
1355 if (__le32_to_cpu(super
->magic
) != MD_SB_MAGIC
) {
1357 fprintf(stderr
, Name
": No super block found on %s (Expected magic %08x, got %08x)\n",
1358 devname
, MD_SB_MAGIC
, __le32_to_cpu(super
->magic
));
1363 if (__le32_to_cpu(super
->major_version
) != 1) {
1365 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
1366 devname
, __le32_to_cpu(super
->major_version
));
1370 if (__le64_to_cpu(super
->super_offset
) != sb_offset
) {
1372 fprintf(stderr
, Name
": No superblock found on %s (super_offset is wrong)\n",
1379 bsb
= (struct bitmap_super_s
*)(((char*)super
)+1024);
1381 misc
= (struct misc_dev_info
*) (((char*)super
)+1024+512);
1382 misc
->device_size
= dsize
;
1384 /* Now check on the bitmap superblock */
1385 if ((__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) == 0)
1387 /* Read the bitmap superblock and make sure it looks
1388 * valid. If it doesn't clear the bit. An --assemble --force
1389 * should get that written out.
1391 locate_bitmap1(st
, fd
);
1392 if (aread(fd
, ((char*)super
)+1024, 512)
1396 uuid_from_super1(st
, uuid
);
1397 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
1398 memcmp(bsb
->uuid
, uuid
, 16) != 0)
1403 super
->feature_map
= __cpu_to_le32(__le32_to_cpu(super
->feature_map
)
1404 & ~MD_FEATURE_BITMAP_OFFSET
);
1409 static struct supertype
*match_metadata_desc1(char *arg
)
1411 struct supertype
*st
= malloc(sizeof(*st
));
1414 memset(st
, 0, sizeof(*st
));
1415 st
->container_dev
= NoMdDev
;
1419 /* leading zeros can be safely ignored. --detail generates them. */
1422 if (strcmp(arg
, "1.0") == 0 ||
1423 strcmp(arg
, "1.00") == 0) {
1424 st
->minor_version
= 0;
1427 if (strcmp(arg
, "1.1") == 0 ||
1428 strcmp(arg
, "1.01") == 0
1430 st
->minor_version
= 1;
1433 if (strcmp(arg
, "1.2") == 0 ||
1434 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1435 strcmp(arg
, "default") == 0 ||
1436 #endif /* DEFAULT_OLD_METADATA */
1437 strcmp(arg
, "1.02") == 0) {
1438 st
->minor_version
= 2;
1441 if (strcmp(arg
, "1") == 0 ||
1442 strcmp(arg
, "default") == 0) {
1443 st
->minor_version
= -1;
1451 /* find available size on device with this devsize, using
1452 * superblock type st, and reserving 'reserve' sectors for
1455 static __u64
avail_size1(struct supertype
*st
, __u64 devsize
)
1457 struct mdp_superblock_1
*super
= st
->sb
;
1462 /* creating: allow suitable space for bitmap */
1463 devsize
-= choose_bm_space(devsize
);
1465 else if (__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
1466 /* hot-add. allow for actual size of bitmap */
1467 struct bitmap_super_s
*bsb
;
1468 bsb
= (struct bitmap_super_s
*)(((char*)super
)+1024);
1469 devsize
-= bitmap_sectors(bsb
);
1473 if (st
->minor_version
< 0)
1474 /* not specified, so time to set default */
1475 st
->minor_version
= 2;
1476 if (super
== NULL
&& st
->minor_version
> 0) {
1477 /* haven't committed to a size yet, so allow some
1478 * slack for alignment of data_offset.
1479 * We haven't access to device details so allow
1480 * 1 Meg if bigger than 1Gig
1482 if (devsize
> 1024*1024*2)
1485 switch(st
->minor_version
) {
1488 return ((devsize
- 8*2 ) & ~(4*2-1));
1490 /* at start, 4K for superblock and possible bitmap */
1491 return devsize
- 4*2;
1493 /* 4k from start, 4K for superblock and possible bitmap */
1494 return devsize
- (4+4)*2;
1500 add_internal_bitmap1(struct supertype
*st
,
1501 int *chunkp
, int delay
, int write_behind
,
1502 unsigned long long size
,
1503 int may_change
, int major
)
1506 * If not may_change, then this is a 'Grow' without sysfs support for
1507 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
1508 * If may_change, then this is create or a Grow with sysfs syupport,
1509 * and we can put the bitmap wherever we like.
1511 * size is in sectors, chunk is in bytes !!!
1514 unsigned long long bits
;
1515 unsigned long long max_bits
;
1516 unsigned long long min_chunk
;
1518 unsigned long long chunk
= *chunkp
;
1521 struct mdp_superblock_1
*sb
= st
->sb
;
1522 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + 1024);
1525 if (__le64_to_cpu(sb
->data_size
) == 0)
1526 /* Must be creating the array, else data_size would be non-zero */
1528 switch(st
->minor_version
) {
1530 /* either 3K after the superblock (when hot-add),
1531 * or some amount of space before.
1534 /* We are creating array, so we *know* how much room has
1538 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1540 room
= __le64_to_cpu(sb
->super_offset
)
1541 - __le64_to_cpu(sb
->data_offset
)
1542 - __le64_to_cpu(sb
->data_size
);
1544 if (!may_change
|| (room
< 3*2 &&
1545 __le32_to_cpu(sb
->max_dev
) <= 384)) {
1549 offset
= 0; /* means movable offset */
1554 case 2: /* between superblock and data */
1557 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1559 room
= __le64_to_cpu(sb
->data_offset
)
1560 - __le64_to_cpu(sb
->super_offset
);
1562 room
-= 2; /* Leave 1K for superblock */
1565 room
-= 4*2; /* leave 4K for superblock */
1574 if (chunk
== UnSet
&& room
> 128*2)
1575 /* Limit to 128K of bitmap when chunk size not requested */
1578 max_bits
= (room
* 512 - sizeof(bitmap_super_t
)) * 8;
1580 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
1581 bits
= (size
*512)/min_chunk
+1;
1582 while (bits
> max_bits
) {
1586 if (chunk
== UnSet
) {
1587 /* For practical purpose, 64Meg is a good
1588 * default chunk size for internal bitmaps.
1591 if (chunk
< 64*1024*1024)
1592 chunk
= 64*1024*1024;
1593 } else if (chunk
< min_chunk
)
1594 return 0; /* chunk size too small */
1595 if (chunk
== 0) /* rounding problem */
1599 /* start bitmap on a 4K boundary with enough space for
1602 bits
= (size
*512) / chunk
+ 1;
1603 room
= ((bits
+7)/8 + sizeof(bitmap_super_t
) +4095)/4096;
1604 room
*= 8; /* convert 4K blocks to sectors */
1608 sb
->bitmap_offset
= __cpu_to_le32(offset
);
1610 sb
->feature_map
= __cpu_to_le32(__le32_to_cpu(sb
->feature_map
)
1611 | MD_FEATURE_BITMAP_OFFSET
);
1612 memset(bms
, 0, sizeof(*bms
));
1613 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
1614 bms
->version
= __cpu_to_le32(major
);
1615 uuid_from_super1(st
, uuid
);
1616 memcpy(bms
->uuid
, uuid
, 16);
1617 bms
->chunksize
= __cpu_to_le32(chunk
);
1618 bms
->daemon_sleep
= __cpu_to_le32(delay
);
1619 bms
->sync_size
= __cpu_to_le64(size
);
1620 bms
->write_behind
= __cpu_to_le32(write_behind
);
1626 static void locate_bitmap1(struct supertype
*st
, int fd
)
1628 unsigned long long offset
;
1629 struct mdp_superblock_1
*sb
;
1633 if (st
->ss
->load_super(st
, fd
, NULL
))
1634 return; /* no error I hope... */
1639 offset
= __le64_to_cpu(sb
->super_offset
);
1640 offset
+= (int32_t) __le32_to_cpu(sb
->bitmap_offset
);
1643 lseek64(fd
, offset
<<9, 0);
1646 static int write_bitmap1(struct supertype
*st
, int fd
)
1648 struct mdp_superblock_1
*sb
= st
->sb
;
1649 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
)+1024);
1654 locate_bitmap1(st
, fd
);
1656 if (posix_memalign(&buf
, 4096, 4096))
1659 memset(buf
, 0xff, 4096);
1660 memcpy(buf
, ((char*)sb
)+1024, sizeof(bitmap_super_t
));
1662 towrite
= __le64_to_cpu(bms
->sync_size
) / (__le32_to_cpu(bms
->chunksize
)>>9);
1663 towrite
= (towrite
+7) >> 3; /* bits to bytes */
1664 towrite
+= sizeof(bitmap_super_t
);
1665 towrite
= ROUND_UP(towrite
, 512);
1666 while (towrite
> 0) {
1670 n
= awrite(fd
, buf
, n
);
1675 memset(buf
, 0xff, 4096);
1685 static void free_super1(struct supertype
*st
)
1690 struct devinfo
*di
= st
->info
;
1691 st
->info
= di
->next
;
1700 static int validate_geometry1(struct supertype
*st
, int level
,
1701 int layout
, int raiddisks
,
1702 int *chunk
, unsigned long long size
,
1703 char *subdev
, unsigned long long *freesize
,
1706 unsigned long long ldsize
;
1709 if (level
== LEVEL_CONTAINER
) {
1711 fprintf(stderr
, Name
": 1.x metadata does not support containers\n");
1714 if (chunk
&& *chunk
== UnSet
)
1715 *chunk
= DEFAULT_CHUNK
;
1720 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
1723 fprintf(stderr
, Name
": super1.x cannot open %s: %s\n",
1724 subdev
, strerror(errno
));
1728 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
1734 *freesize
= avail_size1(st
, ldsize
>> 9);
1737 #endif /* MDASSEMBLE */
1739 struct superswitch super1
= {
1741 .examine_super
= examine_super1
,
1742 .brief_examine_super
= brief_examine_super1
,
1743 .export_examine_super
= export_examine_super1
,
1744 .detail_super
= detail_super1
,
1745 .brief_detail_super
= brief_detail_super1
,
1746 .export_detail_super
= export_detail_super1
,
1747 .write_init_super
= write_init_super1
,
1748 .validate_geometry
= validate_geometry1
,
1749 .add_to_super
= add_to_super1
,
1751 .match_home
= match_home1
,
1752 .uuid_from_super
= uuid_from_super1
,
1753 .getinfo_super
= getinfo_super1
,
1754 .container_content
= container_content1
,
1755 .update_super
= update_super1
,
1756 .init_super
= init_super1
,
1757 .store_super
= store_super1
,
1758 .compare_super
= compare_super1
,
1759 .load_super
= load_super1
,
1760 .match_metadata_desc
= match_metadata_desc1
,
1761 .avail_size
= avail_size1
,
1762 .add_internal_bitmap
= add_internal_bitmap1
,
1763 .locate_bitmap
= locate_bitmap1
,
1764 .write_bitmap
= write_bitmap1
,
1765 .free_super
= free_super1
,
1766 #if __BYTE_ORDER == BIG_ENDIAN