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 ||
151 return read(fd
, buf
, len
);
154 b
= (char*)(((long)(abuf
+4096))&~4095UL);
156 n
= read(fd
, b
, bsize
);
159 lseek(fd
, len
- n
, 1);
166 static int awrite(int fd
, void *buf
, int len
)
169 * On devices with a 4K sector size, we need to write
170 * the full sector. We pre-read if the sector is larger
172 * The address must be sector-aligned.
177 if (ioctl(fd
, BLKSSZGET
, &bsize
) != 0 ||
179 return write(fd
, buf
, len
);
182 b
= (char*)(((long)(abuf
+4096))&~4095UL);
184 n
= read(fd
, b
, bsize
);
189 n
= write(fd
, b
, bsize
);
192 lseek(fd
, len
- n
, 1);
197 static void examine_super1(struct supertype
*st
, char *homehost
)
199 struct mdp_superblock_1
*sb
= st
->sb
;
206 int l
= homehost
? strlen(homehost
) : 0;
208 unsigned long long sb_offset
;
210 printf(" Magic : %08x\n", __le32_to_cpu(sb
->magic
));
211 printf(" Version : 1");
212 sb_offset
= __le64_to_cpu(sb
->super_offset
);
215 else if (sb_offset
<= 8)
219 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb
->feature_map
));
220 printf(" Array UUID : ");
221 for (i
=0; i
<16; i
++) {
222 if ((i
&3)==0 && i
!= 0) printf(":");
223 printf("%02x", sb
->set_uuid
[i
]);
226 printf(" Name : %.32s", sb
->set_name
);
227 if (l
> 0 && l
< 32 &&
228 sb
->set_name
[l
] == ':' &&
229 strncmp(sb
->set_name
, homehost
, l
) == 0)
230 printf(" (local to host %s)", homehost
);
232 atime
= __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
;
233 printf(" Creation Time : %.24s\n", ctime(&atime
));
234 c
=map_num(pers
, __le32_to_cpu(sb
->level
));
235 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
236 printf(" Raid Devices : %d\n", __le32_to_cpu(sb
->raid_disks
));
238 printf(" Avail Dev Size : %llu%s\n",
239 (unsigned long long)__le64_to_cpu(sb
->data_size
),
240 human_size(__le64_to_cpu(sb
->data_size
)<<9));
241 if (__le32_to_cpu(sb
->level
) > 0) {
243 switch(__le32_to_cpu(sb
->level
)) {
244 case 1: ddsks
=1;break;
246 case 5: ddsks
= __le32_to_cpu(sb
->raid_disks
)-1; break;
247 case 6: ddsks
= __le32_to_cpu(sb
->raid_disks
)-2; break;
249 layout
= __le32_to_cpu(sb
->layout
);
250 ddsks
= __le32_to_cpu(sb
->raid_disks
)
251 / (layout
&255) / ((layout
>>8)&255);
254 printf(" Array Size : %llu%s\n",
255 ddsks
*(unsigned long long)__le64_to_cpu(sb
->size
),
256 human_size(ddsks
*__le64_to_cpu(sb
->size
)<<9));
257 if (sb
->size
!= sb
->data_size
)
258 printf(" Used Dev Size : %llu%s\n",
259 (unsigned long long)__le64_to_cpu(sb
->size
),
260 human_size(__le64_to_cpu(sb
->size
)<<9));
263 printf(" Data Offset : %llu sectors\n",
264 (unsigned long long)__le64_to_cpu(sb
->data_offset
));
265 printf(" Super Offset : %llu sectors\n",
266 (unsigned long long)__le64_to_cpu(sb
->super_offset
));
267 if (__le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
)
268 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->recovery_offset
));
269 printf(" State : %s\n", (__le64_to_cpu(sb
->resync_offset
)+1)? "active":"clean");
270 printf(" Device UUID : ");
271 for (i
=0; i
<16; i
++) {
272 if ((i
&3)==0 && i
!= 0) printf(":");
273 printf("%02x", sb
->device_uuid
[i
]);
277 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
278 printf("Internal Bitmap : %ld sectors from superblock\n",
279 (long)(int32_t)__le32_to_cpu(sb
->bitmap_offset
));
281 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
282 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb
->reshape_position
)/2,
283 human_size(__le64_to_cpu(sb
->reshape_position
)<<9));
284 if (__le32_to_cpu(sb
->delta_disks
)) {
285 printf(" Delta Devices : %d", __le32_to_cpu(sb
->delta_disks
));
286 printf(" (%d->%d)\n",
287 __le32_to_cpu(sb
->raid_disks
)-__le32_to_cpu(sb
->delta_disks
),
288 __le32_to_cpu(sb
->raid_disks
));
289 if ((int)__le32_to_cpu(sb
->delta_disks
) < 0)
290 delta_extra
= -__le32_to_cpu(sb
->delta_disks
);
292 if (__le32_to_cpu(sb
->new_level
) != __le32_to_cpu(sb
->level
)) {
293 c
= map_num(pers
, __le32_to_cpu(sb
->new_level
));
294 printf(" New Level : %s\n", c
?c
:"-unknown-");
296 if (__le32_to_cpu(sb
->new_layout
) != __le32_to_cpu(sb
->layout
)) {
297 if (__le32_to_cpu(sb
->level
) == 5) {
298 c
= map_num(r5layout
, __le32_to_cpu(sb
->new_layout
));
299 printf(" New Layout : %s\n", c
?c
:"-unknown-");
301 if (__le32_to_cpu(sb
->level
) == 6) {
302 c
= map_num(r6layout
, __le32_to_cpu(sb
->new_layout
));
303 printf(" New Layout : %s\n", c
?c
:"-unknown-");
305 if (__le32_to_cpu(sb
->level
) == 10) {
306 printf(" New Layout :");
307 print_r10_layout(__le32_to_cpu(sb
->new_layout
));
311 if (__le32_to_cpu(sb
->new_chunk
) != __le32_to_cpu(sb
->chunksize
))
312 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb
->new_chunk
)/2);
317 if (sb
->devflags
& WriteMostly1
)
318 printf(" write-mostly");
322 atime
= __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
;
323 printf(" Update Time : %.24s\n", ctime(&atime
));
325 if (calc_sb_1_csum(sb
) == sb
->sb_csum
)
326 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb
->sb_csum
));
328 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb
->sb_csum
),
329 __le32_to_cpu(calc_sb_1_csum(sb
)));
330 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb
->events
));
332 if (__le32_to_cpu(sb
->level
) == 5) {
333 c
= map_num(r5layout
, __le32_to_cpu(sb
->layout
));
334 printf(" Layout : %s\n", c
?c
:"-unknown-");
336 if (__le32_to_cpu(sb
->level
) == 6) {
337 c
= map_num(r6layout
, __le32_to_cpu(sb
->layout
));
338 printf(" Layout : %s\n", c
?c
:"-unknown-");
340 if (__le32_to_cpu(sb
->level
) == 10) {
341 int lo
= __le32_to_cpu(sb
->layout
);
343 print_r10_layout(lo
);
346 switch(__le32_to_cpu(sb
->level
)) {
352 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
355 printf(" Rounding : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
361 /* This turns out to just be confusing */
362 printf(" Array Slot : %d (", __le32_to_cpu(sb
->dev_number
));
363 for (i
= __le32_to_cpu(sb
->max_dev
); i
> 0 ; i
--)
364 if (__le16_to_cpu(sb
->dev_roles
[i
-1]) != 0xffff)
366 for (d
=0; d
< i
; d
++) {
367 int role
= __le16_to_cpu(sb
->dev_roles
[d
]);
369 if (role
== 0xffff) printf("empty");
370 else if(role
== 0xfffe) printf("failed");
371 else printf("%d", role
);
375 printf(" Device Role : ");
376 d
= __le32_to_cpu(sb
->dev_number
);
377 if (d
< __le32_to_cpu(sb
->max_dev
))
378 role
= __le16_to_cpu(sb
->dev_roles
[d
]);
384 printf("Active device %d\n", role
);
386 printf(" Array State : ");
387 for (d
=0; d
<__le32_to_cpu(sb
->raid_disks
) + delta_extra
; d
++) {
390 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
391 unsigned int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
395 if (cnt
> 1) printf("?");
396 else if (cnt
== 1) printf("A");
400 /* This is confusing too */
402 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
403 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
407 if (faulty
) printf(" %d failed", faulty
);
409 printf(" ('A' == active, '.' == missing)");
414 static void brief_examine_super1(struct supertype
*st
, int verbose
)
416 struct mdp_superblock_1
*sb
= st
->sb
;
418 unsigned long long sb_offset
;
420 char *c
=map_num(pers
, __le32_to_cpu(sb
->level
));
422 nm
= strchr(sb
->set_name
, ':');
425 else if (sb
->set_name
[0])
430 printf("ARRAY%s%s", nm
? " /dev/md/":"", nm
);
432 printf(" level=%s", c
);
433 sb_offset
= __le64_to_cpu(sb
->super_offset
);
435 printf(" metadata=1.1 ");
436 else if (sb_offset
<= 8)
437 printf(" metadata=1.2 ");
439 printf(" metadata=1.0 ");
441 printf("num-devices=%d ", __le32_to_cpu(sb
->raid_disks
));
443 for (i
=0; i
<16; i
++) {
444 if ((i
&3)==0 && i
!= 0) printf(":");
445 printf("%02x", sb
->set_uuid
[i
]);
448 printf(" name=%.32s", sb
->set_name
);
452 static void export_examine_super1(struct supertype
*st
)
454 struct mdp_superblock_1
*sb
= st
->sb
;
458 printf("MD_LEVEL=%s\n", map_num(pers
, __le32_to_cpu(sb
->level
)));
459 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb
->raid_disks
));
461 if (sb
->set_name
[i
] == '\n' ||
462 sb
->set_name
[i
] == '\0') {
467 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
469 for (i
=0; i
<16; i
++) {
470 if ((i
&3)==0 && i
!= 0) printf(":");
471 printf("%02x", sb
->set_uuid
[i
]);
474 printf("MD_UPDATE_TIME=%llu\n",
475 __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
);
476 printf("MD_DEV_UUID=");
477 for (i
=0; i
<16; i
++) {
478 if ((i
&3)==0 && i
!= 0) printf(":");
479 printf("%02x", sb
->device_uuid
[i
]);
482 printf("MD_EVENTS=%llu\n",
483 (unsigned long long)__le64_to_cpu(sb
->events
));
486 static void detail_super1(struct supertype
*st
, char *homehost
)
488 struct mdp_superblock_1
*sb
= st
->sb
;
490 int l
= homehost
? strlen(homehost
) : 0;
492 printf(" Name : %.32s", sb
->set_name
);
493 if (l
> 0 && l
< 32 &&
494 sb
->set_name
[l
] == ':' &&
495 strncmp(sb
->set_name
, homehost
, l
) == 0)
496 printf(" (local to host %s)", homehost
);
497 printf("\n UUID : ");
498 for (i
=0; i
<16; i
++) {
499 if ((i
&3)==0 && i
!= 0) printf(":");
500 printf("%02x", sb
->set_uuid
[i
]);
502 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb
->events
));
505 static void brief_detail_super1(struct supertype
*st
)
507 struct mdp_superblock_1
*sb
= st
->sb
;
511 printf(" name=%.32s", sb
->set_name
);
513 for (i
=0; i
<16; i
++) {
514 if ((i
&3)==0 && i
!= 0) printf(":");
515 printf("%02x", sb
->set_uuid
[i
]);
519 static void export_detail_super1(struct supertype
*st
)
521 struct mdp_superblock_1
*sb
= st
->sb
;
526 if (sb
->set_name
[i
] == '\n' ||
527 sb
->set_name
[i
] == '\0') {
532 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
537 static int match_home1(struct supertype
*st
, char *homehost
)
539 struct mdp_superblock_1
*sb
= st
->sb
;
540 int l
= homehost
? strlen(homehost
) : 0;
542 return (l
> 0 && l
< 32 &&
543 sb
->set_name
[l
] == ':' &&
544 strncmp(sb
->set_name
, homehost
, l
) == 0);
547 static void uuid_from_super1(struct supertype
*st
, int uuid
[4])
549 struct mdp_superblock_1
*super
= st
->sb
;
550 char *cuuid
= (char*)uuid
;
553 cuuid
[i
] = super
->set_uuid
[i
];
556 static void getinfo_super1(struct supertype
*st
, struct mdinfo
*info
, char *map
)
558 struct mdp_superblock_1
*sb
= st
->sb
;
562 unsigned int map_disks
= info
->array
.raid_disks
;
564 memset(info
, 0, sizeof(*info
));
565 info
->array
.major_version
= 1;
566 info
->array
.minor_version
= st
->minor_version
;
567 info
->array
.patch_version
= 0;
568 info
->array
.raid_disks
= __le32_to_cpu(sb
->raid_disks
);
569 info
->array
.level
= __le32_to_cpu(sb
->level
);
570 info
->array
.layout
= __le32_to_cpu(sb
->layout
);
571 info
->array
.md_minor
= -1;
572 info
->array
.ctime
= __le64_to_cpu(sb
->ctime
);
573 info
->array
.utime
= __le64_to_cpu(sb
->utime
);
574 info
->array
.chunk_size
= __le32_to_cpu(sb
->chunksize
)*512;
576 (__le64_to_cpu(sb
->resync_offset
) >= __le64_to_cpu(sb
->size
))
579 info
->data_offset
= __le64_to_cpu(sb
->data_offset
);
580 info
->component_size
= __le64_to_cpu(sb
->size
);
582 info
->disk
.major
= 0;
583 info
->disk
.minor
= 0;
584 info
->disk
.number
= __le32_to_cpu(sb
->dev_number
);
585 if (__le32_to_cpu(sb
->dev_number
) >= __le32_to_cpu(sb
->max_dev
) ||
586 __le32_to_cpu(sb
->max_dev
) > 512)
589 role
= __le16_to_cpu(sb
->dev_roles
[__le32_to_cpu(sb
->dev_number
)]);
591 info
->disk
.raid_disk
= -1;
594 info
->disk
.state
= 0; /* spare: not active, not sync, not faulty */
597 info
->disk
.state
= 1; /* faulty */
600 info
->disk
.state
= 6; /* active and in sync */
601 info
->disk
.raid_disk
= role
;
603 info
->events
= __le64_to_cpu(sb
->events
);
604 sprintf(info
->text_version
, "1.%d", st
->minor_version
);
605 info
->safe_mode_delay
= 200;
607 memcpy(info
->uuid
, sb
->set_uuid
, 16);
609 strncpy(info
->name
, sb
->set_name
, 32);
612 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET
))
613 info
->recovery_start
= __le32_to_cpu(sb
->recovery_offset
);
615 info
->recovery_start
= MaxSector
;
617 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
618 info
->reshape_active
= 1;
619 info
->reshape_progress
= __le64_to_cpu(sb
->reshape_position
);
620 info
->new_level
= __le32_to_cpu(sb
->new_level
);
621 info
->delta_disks
= __le32_to_cpu(sb
->delta_disks
);
622 info
->new_layout
= __le32_to_cpu(sb
->new_layout
);
623 info
->new_chunk
= __le32_to_cpu(sb
->new_chunk
)<<9;
624 if (info
->delta_disks
< 0)
625 info
->array
.raid_disks
-= info
->delta_disks
;
627 info
->reshape_active
= 0;
629 info
->recovery_blocked
= info
->reshape_active
;
632 for (i
=0; i
<map_disks
; i
++)
634 for (i
= 0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
635 role
= __le16_to_cpu(sb
->dev_roles
[i
]);
636 if (/*role == 0xFFFF || */role
< (unsigned) info
->array
.raid_disks
) {
638 if (map
&& role
< map_disks
)
643 info
->array
.working_disks
= working
;
646 static struct mdinfo
*container_content1(struct supertype
*st
, char *subarray
)
653 info
= malloc(sizeof(*info
));
654 getinfo_super1(st
, info
, NULL
);
658 static int update_super1(struct supertype
*st
, struct mdinfo
*info
,
660 char *devname
, int verbose
,
661 int uuid_set
, char *homehost
)
663 /* NOTE: for 'assemble' and 'force' we need to return non-zero
664 * if any change was made. For others, the return value is
668 struct mdp_superblock_1
*sb
= st
->sb
;
670 if (strcmp(update
, "force-one")==0) {
671 /* Not enough devices for a working array,
672 * so bring this one up-to-date
674 if (sb
->events
!= __cpu_to_le64(info
->events
))
676 sb
->events
= __cpu_to_le64(info
->events
);
677 } else if (strcmp(update
, "force-array")==0) {
678 /* Degraded array and 'force' requests to
679 * maybe need to mark it 'clean'.
681 switch(__le32_to_cpu(sb
->level
)) {
682 case 5: case 4: case 6:
683 /* need to force clean */
684 if (sb
->resync_offset
!= MaxSector
)
686 sb
->resync_offset
= MaxSector
;
688 } else if (strcmp(update
, "assemble")==0) {
689 int d
= info
->disk
.number
;
691 if (info
->disk
.state
== 6)
692 want
= info
->disk
.raid_disk
;
695 if (sb
->dev_roles
[d
] != __cpu_to_le16(want
)) {
696 sb
->dev_roles
[d
] = __cpu_to_le16(want
);
699 if (info
->reshape_active
&&
700 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
701 info
->delta_disks
>= 0 &&
702 info
->reshape_progress
< __le64_to_cpu(sb
->reshape_position
)) {
703 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
706 if (info
->reshape_active
&&
707 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
708 info
->delta_disks
< 0 &&
709 info
->reshape_progress
> __le64_to_cpu(sb
->reshape_position
)) {
710 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
713 } else if (strcmp(update
, "linear-grow-new") == 0) {
716 unsigned int max
= __le32_to_cpu(sb
->max_dev
);
718 for (i
=0 ; i
< max
; i
++)
719 if (__le16_to_cpu(sb
->dev_roles
[i
]) >= 0xfffe)
721 sb
->dev_number
= __cpu_to_le32(i
);
722 info
->disk
.number
= i
;
723 if (max
>= __le32_to_cpu(sb
->max_dev
))
724 sb
->max_dev
= __cpu_to_le32(max
+1);
726 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
727 read(rfd
, sb
->device_uuid
, 16) != 16) {
728 __u32 r
[4] = {random(), random(), random(), random()};
729 memcpy(sb
->device_uuid
, r
, 16);
735 __cpu_to_le16(info
->disk
.raid_disk
);
737 fd
= open(devname
, O_RDONLY
);
739 unsigned long long ds
;
740 get_dev_size(fd
, devname
, &ds
);
743 if (__le64_to_cpu(sb
->super_offset
) <
744 __le64_to_cpu(sb
->data_offset
)) {
745 sb
->data_size
= __cpu_to_le64(
746 ds
- __le64_to_cpu(sb
->data_offset
));
749 ds
&= ~(unsigned long long)(4*2-1);
750 sb
->super_offset
= __cpu_to_le64(ds
);
751 sb
->data_size
= __cpu_to_le64(
752 ds
- __le64_to_cpu(sb
->data_offset
));
755 } else if (strcmp(update
, "linear-grow-update") == 0) {
756 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
757 sb
->dev_roles
[info
->disk
.number
] =
758 __cpu_to_le16(info
->disk
.raid_disk
);
759 } else if (strcmp(update
, "resync") == 0) {
760 /* make sure resync happens */
761 sb
->resync_offset
= 0ULL;
762 } else if (strcmp(update
, "uuid") == 0) {
763 copy_uuid(sb
->set_uuid
, info
->uuid
, super1
.swapuuid
);
765 if (__le32_to_cpu(sb
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
766 struct bitmap_super_s
*bm
;
767 bm
= (struct bitmap_super_s
*)(st
->sb
+1024);
768 memcpy(bm
->uuid
, sb
->set_uuid
, 16);
770 } else if (strcmp(update
, "no-bitmap") == 0) {
771 sb
->feature_map
&= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
772 } else if (strcmp(update
, "homehost") == 0 &&
776 c
= strchr(sb
->set_name
, ':');
778 strncpy(info
->name
, c
+1, 31 - (c
-sb
->set_name
));
780 strncpy(info
->name
, sb
->set_name
, 32);
782 } else if (strcmp(update
, "name") == 0) {
783 if (info
->name
[0] == 0)
784 sprintf(info
->name
, "%d", info
->array
.md_minor
);
785 memset(sb
->set_name
, 0, sizeof(sb
->set_name
));
787 strchr(info
->name
, ':') == NULL
&&
788 strlen(homehost
)+1+strlen(info
->name
) < 32) {
789 strcpy(sb
->set_name
, homehost
);
790 strcat(sb
->set_name
, ":");
791 strcat(sb
->set_name
, info
->name
);
793 strcpy(sb
->set_name
, info
->name
);
794 } else if (strcmp(update
, "devicesize") == 0 &&
795 __le64_to_cpu(sb
->super_offset
) <
796 __le64_to_cpu(sb
->data_offset
)) {
797 /* set data_size to device size less data_offset */
798 struct misc_dev_info
*misc
= (struct misc_dev_info
*)
799 (st
->sb
+ 1024 + 512);
800 printf("Size was %llu\n", (unsigned long long)
801 __le64_to_cpu(sb
->data_size
));
802 sb
->data_size
= __cpu_to_le64(
803 misc
->device_size
- __le64_to_cpu(sb
->data_offset
));
804 printf("Size is %llu\n", (unsigned long long)
805 __le64_to_cpu(sb
->data_size
));
806 } else if (strcmp(update
, "_reshape_progress")==0)
807 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
808 else if (strcmp(update
, "writemostly")==0)
809 sb
->devflags
|= WriteMostly1
;
810 else if (strcmp(update
, "readwrite")==0)
811 sb
->devflags
&= ~WriteMostly1
;
815 sb
->sb_csum
= calc_sb_1_csum(sb
);
819 static int init_super1(struct supertype
*st
, mdu_array_info_t
*info
,
820 unsigned long long size
, char *name
, char *homehost
, int *uuid
)
822 struct mdp_superblock_1
*sb
;
827 if (posix_memalign((void**)&sb
, 512, (1024 + 512 +
828 sizeof(struct misc_dev_info
))) != 0) {
830 ": %s could not allocate superblock\n", __func__
);
837 /* zeroing superblock */
841 spares
= info
->working_disks
- info
->active_disks
;
842 if (info
->raid_disks
+ spares
> 384) {
843 fprintf(stderr
, Name
": too many devices requested: %d+%d > %d\n",
844 info
->raid_disks
, spares
, 384);
848 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
849 sb
->major_version
= __cpu_to_le32(1);
854 copy_uuid(sb
->set_uuid
, uuid
, super1
.swapuuid
);
856 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
857 read(rfd
, sb
->set_uuid
, 16) != 16) {
858 __u32 r
[4] = {random(), random(), random(), random()};
859 memcpy(sb
->set_uuid
, r
, 16);
861 if (rfd
>= 0) close(rfd
);
864 if (name
== NULL
|| *name
== 0) {
865 sprintf(defname
, "%d", info
->md_minor
);
868 memset(sb
->set_name
, 0, 32);
870 strchr(name
, ':')== NULL
&&
871 strlen(homehost
)+1+strlen(name
) < 32) {
872 strcpy(sb
->set_name
, homehost
);
873 strcat(sb
->set_name
, ":");
874 strcat(sb
->set_name
, name
);
876 strcpy(sb
->set_name
, name
);
878 sb
->ctime
= __cpu_to_le64((unsigned long long)time(0));
879 sb
->level
= __cpu_to_le32(info
->level
);
880 sb
->layout
= __cpu_to_le32(info
->layout
);
881 sb
->size
= __cpu_to_le64(size
*2ULL);
882 sb
->chunksize
= __cpu_to_le32(info
->chunk_size
>>9);
883 sb
->raid_disks
= __cpu_to_le32(info
->raid_disks
);
885 sb
->data_offset
= __cpu_to_le64(0);
886 sb
->data_size
= __cpu_to_le64(0);
887 sb
->super_offset
= __cpu_to_le64(0);
888 sb
->recovery_offset
= __cpu_to_le64(0);
890 sb
->utime
= sb
->ctime
;
891 sb
->events
= __cpu_to_le64(1);
892 if (info
->state
& (1<<MD_SB_CLEAN
))
893 sb
->resync_offset
= MaxSector
;
895 sb
->resync_offset
= 0;
896 sb
->max_dev
= __cpu_to_le32((1024- sizeof(struct mdp_superblock_1
))/
897 sizeof(sb
->dev_roles
[0]));
898 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
900 memset(sb
->dev_roles
, 0xff, 1024 - sizeof(struct mdp_superblock_1
));
908 mdu_disk_info_t disk
;
909 struct devinfo
*next
;
912 /* Add a device to the superblock being created */
913 static int add_to_super1(struct supertype
*st
, mdu_disk_info_t
*dk
,
914 int fd
, char *devname
)
916 struct mdp_superblock_1
*sb
= st
->sb
;
917 __u16
*rp
= sb
->dev_roles
+ dk
->number
;
918 struct devinfo
*di
, **dip
;
920 if ((dk
->state
& 6) == 6) /* active, sync */
921 *rp
= __cpu_to_le16(dk
->raid_disk
);
922 else if ((dk
->state
& ~2) == 0) /* active or idle -> spare */
927 if (dk
->number
>= (int)__le32_to_cpu(sb
->max_dev
) &&
928 __le32_to_cpu(sb
->max_dev
) < 384)
929 sb
->max_dev
= __cpu_to_le32(dk
->number
+1);
931 sb
->dev_number
= __cpu_to_le32(dk
->number
);
932 sb
->devflags
= 0; /* don't copy another disks flags */
933 sb
->sb_csum
= calc_sb_1_csum(sb
);
935 dip
= (struct devinfo
**)&st
->info
;
938 di
= malloc(sizeof(struct devinfo
));
940 di
->devname
= devname
;
949 static void locate_bitmap1(struct supertype
*st
, int fd
);
951 static int store_super1(struct supertype
*st
, int fd
)
953 struct mdp_superblock_1
*sb
= st
->sb
;
954 unsigned long long sb_offset
;
956 unsigned long long dsize
;
958 if (!get_dev_size(fd
, NULL
, &dsize
))
967 * Calculate the position of the superblock.
968 * It is always aligned to a 4K boundary and
969 * depending on minor_version, it can be:
970 * 0: At least 8K, but less than 12K, from end of device
971 * 1: At start of device
972 * 2: 4K from start of device.
974 switch(st
->minor_version
) {
978 sb_offset
&= ~(4*2-1);
992 if (sb_offset
!= __le64_to_cpu(sb
->super_offset
) &&
993 0 != __le64_to_cpu(sb
->super_offset
)
995 fprintf(stderr
, Name
": internal error - sb_offset is wrong\n");
999 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL)
1002 sbsize
= sizeof(*sb
) + 2 * __le32_to_cpu(sb
->max_dev
);
1003 sbsize
= (sbsize
+511)&(~511UL);
1005 if (awrite(fd
, sb
, sbsize
) != sbsize
)
1008 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
1009 struct bitmap_super_s
*bm
= (struct bitmap_super_s
*)
1011 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
) {
1012 locate_bitmap1(st
, fd
);
1013 if (awrite(fd
, bm
, sizeof(*bm
)) !=
1022 static int load_super1(struct supertype
*st
, int fd
, char *devname
);
1024 static unsigned long choose_bm_space(unsigned long devsize
)
1026 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1027 * if bigger than 200Gig, save 128k
1028 * NOTE: result must be multiple of 4K else bad things happen
1029 * on 4K-sector devices.
1031 if (devsize
< 64*2) return 0;
1032 if (devsize
- 64*2 >= 200*1024*1024*2)
1034 if (devsize
- 4*2 > 8*1024*1024*2)
1039 static void free_super1(struct supertype
*st
);
1042 static int write_init_super1(struct supertype
*st
)
1044 struct mdp_superblock_1
*sb
= st
->sb
;
1045 struct supertype
*refst
;
1048 unsigned long long bm_space
;
1049 unsigned long long reserved
;
1051 unsigned long long dsize
, array_size
;
1052 unsigned long long sb_offset
;
1054 for (di
= st
->info
; di
&& ! rv
; di
= di
->next
) {
1055 if (di
->disk
.state
== 1)
1060 while (Kill(di
->devname
, NULL
, 0, 1, 1) == 0)
1063 sb
->dev_number
= __cpu_to_le32(di
->disk
.number
);
1064 if (di
->disk
.state
& (1<<MD_DISK_WRITEMOSTLY
))
1065 sb
->devflags
|= __cpu_to_le32(WriteMostly1
);
1067 sb
->devflags
&= ~(__cpu_to_le32(WriteMostly1
));
1069 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
1070 read(rfd
, sb
->device_uuid
, 16) != 16) {
1071 __u32 r
[4] = {random(), random(), random(), random()};
1072 memcpy(sb
->device_uuid
, r
, 16);
1079 refst
= dup_super(st
);
1080 if (load_super1(refst
, di
->fd
, NULL
)==0) {
1081 struct mdp_superblock_1
*refsb
= refst
->sb
;
1083 memcpy(sb
->device_uuid
, refsb
->device_uuid
, 16);
1084 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16)==0) {
1085 /* same array, so preserve events and
1087 sb
->events
= refsb
->events
;
1088 /* bugs in 2.6.17 and earlier mean the
1089 * dev_number chosen in Manage must be preserved
1091 if (get_linux_version() >= 2006018)
1092 sb
->dev_number
= refsb
->dev_number
;
1098 if (!get_dev_size(di
->fd
, NULL
, &dsize
))
1109 * Calculate the position of the superblock.
1110 * It is always aligned to a 4K boundary and
1111 * depending on minor_version, it can be:
1112 * 0: At least 8K, but less than 12K, from end of device
1113 * 1: At start of device
1114 * 2: 4K from start of device.
1115 * Depending on the array size, we might leave extra space
1118 array_size
= __le64_to_cpu(sb
->size
);
1119 /* work out how much space we left for a bitmap */
1120 bm_space
= choose_bm_space(array_size
);
1122 switch(st
->minor_version
) {
1126 sb_offset
&= ~(4*2-1);
1127 sb
->super_offset
= __cpu_to_le64(sb_offset
);
1128 sb
->data_offset
= __cpu_to_le64(0);
1129 if (sb_offset
< array_size
+ bm_space
)
1130 bm_space
= sb_offset
- array_size
;
1131 sb
->data_size
= __cpu_to_le64(sb_offset
- bm_space
);
1134 sb
->super_offset
= __cpu_to_le64(0);
1135 reserved
= bm_space
+ 4*2;
1136 /* Try for multiple of 1Meg so it is nicely aligned */
1137 #define ONE_MEG (2*1024)
1138 reserved
= ((reserved
+ ONE_MEG
-1)/ONE_MEG
) * ONE_MEG
;
1139 if (reserved
+ __le64_to_cpu(sb
->size
) > dsize
)
1140 reserved
= dsize
- __le64_to_cpu(sb
->size
);
1141 /* force 4K alignment */
1144 sb
->data_offset
= __cpu_to_le64(reserved
);
1145 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1149 sb
->super_offset
= __cpu_to_le64(4*2);
1150 if (4*2 + 4*2 + bm_space
+ __le64_to_cpu(sb
->size
)
1152 bm_space
= dsize
- __le64_to_cpu(sb
->size
)
1155 reserved
= bm_space
+ 4*2 + 4*2;
1156 /* Try for multiple of 1Meg so it is nicely aligned */
1157 #define ONE_MEG (2*1024)
1158 reserved
= ((reserved
+ ONE_MEG
-1)/ONE_MEG
) * ONE_MEG
;
1159 if (reserved
+ __le64_to_cpu(sb
->size
) > dsize
)
1160 reserved
= dsize
- __le64_to_cpu(sb
->size
);
1161 /* force 4K alignment */
1164 sb
->data_offset
= __cpu_to_le64(reserved
);
1165 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1172 sb
->sb_csum
= calc_sb_1_csum(sb
);
1173 rv
= store_super1(st
, di
->fd
);
1176 Name
": failed to write superblock to %s\n",
1179 if (rv
== 0 && (__le32_to_cpu(sb
->feature_map
) & 1))
1180 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
1188 static int compare_super1(struct supertype
*st
, struct supertype
*tst
)
1192 * 0 same, or first was empty, and second was copied
1193 * 1 second had wrong number
1195 * 3 wrong other info
1197 struct mdp_superblock_1
*first
= st
->sb
;
1198 struct mdp_superblock_1
*second
= tst
->sb
;
1200 if (second
->magic
!= __cpu_to_le32(MD_SB_MAGIC
))
1202 if (second
->major_version
!= __cpu_to_le32(1))
1206 if (posix_memalign((void**)&first
, 512,
1208 sizeof(struct misc_dev_info
)) != 0) {
1209 fprintf(stderr
, Name
1210 ": %s could not allocate superblock\n", __func__
);
1213 memcpy(first
, second
, 1024 + 512 +
1214 sizeof(struct misc_dev_info
));
1218 if (memcmp(first
->set_uuid
, second
->set_uuid
, 16)!= 0)
1221 if (first
->ctime
!= second
->ctime
||
1222 first
->level
!= second
->level
||
1223 first
->layout
!= second
->layout
||
1224 first
->size
!= second
->size
||
1225 first
->chunksize
!= second
->chunksize
||
1226 first
->raid_disks
!= second
->raid_disks
)
1231 static int load_super1(struct supertype
*st
, int fd
, char *devname
)
1233 unsigned long long dsize
;
1234 unsigned long long sb_offset
;
1235 struct mdp_superblock_1
*super
;
1237 struct bitmap_super_s
*bsb
;
1238 struct misc_dev_info
*misc
;
1242 if (st
->ss
== NULL
|| st
->minor_version
== -1) {
1244 struct supertype tst
;
1245 __u64 bestctime
= 0;
1246 /* guess... choose latest ctime */
1247 memset(&tst
, 0, sizeof(tst
));
1249 for (tst
.minor_version
= 0; tst
.minor_version
<= 2 ; tst
.minor_version
++) {
1250 switch(load_super1(&tst
, fd
, devname
)) {
1251 case 0: super
= tst
.sb
;
1252 if (bestvers
== -1 ||
1253 bestctime
< __le64_to_cpu(super
->ctime
)) {
1254 bestvers
= tst
.minor_version
;
1255 bestctime
= __le64_to_cpu(super
->ctime
);
1260 case 1: return 1; /*bad device */
1261 case 2: break; /* bad, try next */
1264 if (bestvers
!= -1) {
1266 tst
.minor_version
= bestvers
;
1269 rv
= load_super1(&tst
, fd
, devname
);
1276 if (!get_dev_size(fd
, devname
, &dsize
))
1282 fprintf(stderr
, Name
": %s is too small for md: size is %llu sectors.\n",
1288 * Calculate the position of the superblock.
1289 * It is always aligned to a 4K boundary and
1290 * depending on minor_version, it can be:
1291 * 0: At least 8K, but less than 12K, from end of device
1292 * 1: At start of device
1293 * 2: 4K from start of device.
1295 switch(st
->minor_version
) {
1299 sb_offset
&= ~(4*2-1);
1311 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
1314 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL) {
1316 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
1317 devname
, strerror(errno
));
1321 if (posix_memalign((void**)&super
, 512,
1323 sizeof(struct misc_dev_info
)) != 0) {
1324 fprintf(stderr
, Name
": %s could not allocate superblock\n",
1329 if (aread(fd
, super
, 1024) != 1024) {
1331 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
1337 if (__le32_to_cpu(super
->magic
) != MD_SB_MAGIC
) {
1339 fprintf(stderr
, Name
": No super block found on %s (Expected magic %08x, got %08x)\n",
1340 devname
, MD_SB_MAGIC
, __le32_to_cpu(super
->magic
));
1345 if (__le32_to_cpu(super
->major_version
) != 1) {
1347 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
1348 devname
, __le32_to_cpu(super
->major_version
));
1352 if (__le64_to_cpu(super
->super_offset
) != sb_offset
) {
1354 fprintf(stderr
, Name
": No superblock found on %s (super_offset is wrong)\n",
1361 bsb
= (struct bitmap_super_s
*)(((char*)super
)+1024);
1363 misc
= (struct misc_dev_info
*) (((char*)super
)+1024+512);
1364 misc
->device_size
= dsize
;
1366 /* Now check on the bitmap superblock */
1367 if ((__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) == 0)
1369 /* Read the bitmap superblock and make sure it looks
1370 * valid. If it doesn't clear the bit. An --assemble --force
1371 * should get that written out.
1373 locate_bitmap1(st
, fd
);
1374 if (aread(fd
, ((char*)super
)+1024, 512)
1378 uuid_from_super1(st
, uuid
);
1379 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
1380 memcmp(bsb
->uuid
, uuid
, 16) != 0)
1385 super
->feature_map
= __cpu_to_le32(__le32_to_cpu(super
->feature_map
) & ~1);
1390 static struct supertype
*match_metadata_desc1(char *arg
)
1392 struct supertype
*st
= malloc(sizeof(*st
));
1395 memset(st
, 0, sizeof(*st
));
1396 st
->container_dev
= NoMdDev
;
1400 /* leading zeros can be safely ignored. --detail generates them. */
1403 if (strcmp(arg
, "1.0") == 0 ||
1404 strcmp(arg
, "1.00") == 0) {
1405 st
->minor_version
= 0;
1408 if (strcmp(arg
, "1.1") == 0 ||
1409 strcmp(arg
, "1.01") == 0
1411 st
->minor_version
= 1;
1414 if (strcmp(arg
, "1.2") == 0 ||
1415 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1416 strcmp(arg
, "default") == 0 ||
1417 #endif /* DEFAULT_OLD_METADATA */
1418 strcmp(arg
, "1.02") == 0) {
1419 st
->minor_version
= 2;
1422 if (strcmp(arg
, "1") == 0 ||
1423 strcmp(arg
, "default") == 0) {
1424 st
->minor_version
= -1;
1432 /* find available size on device with this devsize, using
1433 * superblock type st, and reserving 'reserve' sectors for
1436 static __u64
avail_size1(struct supertype
*st
, __u64 devsize
)
1438 struct mdp_superblock_1
*super
= st
->sb
;
1443 /* creating: allow suitable space for bitmap */
1444 devsize
-= choose_bm_space(devsize
);
1446 else if (__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
1447 /* hot-add. allow for actual size of bitmap */
1448 struct bitmap_super_s
*bsb
;
1449 bsb
= (struct bitmap_super_s
*)(((char*)super
)+1024);
1450 devsize
-= bitmap_sectors(bsb
);
1454 if (st
->minor_version
< 0)
1455 /* not specified, so time to set default */
1456 st
->minor_version
= 2;
1457 if (super
== NULL
&& st
->minor_version
> 0) {
1458 /* haven't committed to a size yet, so allow some
1459 * slack for alignment of data_offset.
1460 * We haven't access to device details so allow
1461 * 1 Meg if bigger than 1Gig
1463 if (devsize
> 1024*1024*2)
1466 switch(st
->minor_version
) {
1469 return ((devsize
- 8*2 ) & ~(4*2-1));
1471 /* at start, 4K for superblock and possible bitmap */
1472 return devsize
- 4*2;
1474 /* 4k from start, 4K for superblock and possible bitmap */
1475 return devsize
- (4+4)*2;
1481 add_internal_bitmap1(struct supertype
*st
,
1482 int *chunkp
, int delay
, int write_behind
,
1483 unsigned long long size
,
1484 int may_change
, int major
)
1487 * If not may_change, then this is a 'Grow', and the bitmap
1488 * must fit after the superblock.
1489 * If may_change, then this is create, and we can put the bitmap
1490 * before the superblock if we like, or may move the start.
1491 * If !may_change, the bitmap MUST live at offset of 1K, until
1492 * we get a sysfs interface.
1494 * size is in sectors, chunk is in bytes !!!
1497 unsigned long long bits
;
1498 unsigned long long max_bits
;
1499 unsigned long long min_chunk
;
1501 unsigned long long chunk
= *chunkp
;
1503 struct mdp_superblock_1
*sb
= st
->sb
;
1504 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + 1024);
1507 switch(st
->minor_version
) {
1509 /* either 3K after the superblock (when hot-add),
1510 * or some amount of space before.
1513 /* We are creating array, so we *know* how much room has
1517 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1519 room
= __le64_to_cpu(sb
->super_offset
)
1520 - __le64_to_cpu(sb
->data_offset
)
1521 - __le64_to_cpu(sb
->data_size
);
1522 /* remove '1 ||' when we can set offset via sysfs */
1523 if (1 || (room
< 3*2 &&
1524 __le32_to_cpu(sb
->max_dev
) <= 384)) {
1528 offset
= 0; /* means movable offset */
1533 case 2: /* between superblock and data */
1536 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1538 room
= __le64_to_cpu(sb
->data_offset
)
1539 - __le64_to_cpu(sb
->super_offset
);
1540 if (1 || __le32_to_cpu(sb
->max_dev
) <= 384) {
1553 if (chunk
== UnSet
&& room
> 128*2)
1554 /* Limit to 128K of bitmap when chunk size not requested */
1557 max_bits
= (room
* 512 - sizeof(bitmap_super_t
)) * 8;
1559 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
1560 bits
= (size
*512)/min_chunk
+1;
1561 while (bits
> max_bits
) {
1565 if (chunk
== UnSet
) {
1566 /* For practical purpose, 64Meg is a good
1567 * default chunk size for internal bitmaps.
1570 if (chunk
< 64*1024*1024)
1571 chunk
= 64*1024*1024;
1572 } else if (chunk
< min_chunk
)
1573 return 0; /* chunk size too small */
1574 if (chunk
== 0) /* rounding problem */
1578 /* start bitmap on a 4K boundary with enough space for
1581 bits
= (size
*512) / chunk
+ 1;
1582 room
= ((bits
+7)/8 + sizeof(bitmap_super_t
) +4095)/4096;
1583 room
*= 8; /* convert 4K blocks to sectors */
1587 sb
->bitmap_offset
= __cpu_to_le32(offset
);
1589 sb
->feature_map
= __cpu_to_le32(__le32_to_cpu(sb
->feature_map
) | 1);
1590 memset(bms
, 0, sizeof(*bms
));
1591 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
1592 bms
->version
= __cpu_to_le32(major
);
1593 uuid_from_super1(st
, uuid
);
1594 memcpy(bms
->uuid
, uuid
, 16);
1595 bms
->chunksize
= __cpu_to_le32(chunk
);
1596 bms
->daemon_sleep
= __cpu_to_le32(delay
);
1597 bms
->sync_size
= __cpu_to_le64(size
);
1598 bms
->write_behind
= __cpu_to_le32(write_behind
);
1605 static void locate_bitmap1(struct supertype
*st
, int fd
)
1607 unsigned long long offset
;
1608 struct mdp_superblock_1
*sb
;
1612 if (st
->ss
->load_super(st
, fd
, NULL
))
1613 return; /* no error I hope... */
1618 offset
= __le64_to_cpu(sb
->super_offset
);
1619 offset
+= (int32_t) __le32_to_cpu(sb
->bitmap_offset
);
1622 lseek64(fd
, offset
<<9, 0);
1625 static int write_bitmap1(struct supertype
*st
, int fd
)
1627 struct mdp_superblock_1
*sb
= st
->sb
;
1628 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
)+1024);
1632 char *buf
= (char*)(((long)(abuf
+4096))&~4095UL);
1634 locate_bitmap1(st
, fd
);
1636 memset(buf
, 0xff, 4096);
1637 memcpy(buf
, ((char*)sb
)+1024, sizeof(bitmap_super_t
));
1639 towrite
= __le64_to_cpu(bms
->sync_size
) / (__le32_to_cpu(bms
->chunksize
)>>9);
1640 towrite
= (towrite
+7) >> 3; /* bits to bytes */
1641 towrite
+= sizeof(bitmap_super_t
);
1642 towrite
= ROUND_UP(towrite
, 512);
1643 while (towrite
> 0) {
1647 n
= write(fd
, buf
, n
);
1652 memset(buf
, 0xff, 4096);
1661 static void free_super1(struct supertype
*st
)
1666 struct devinfo
*di
= st
->info
;
1667 st
->info
= di
->next
;
1676 static int validate_geometry1(struct supertype
*st
, int level
,
1677 int layout
, int raiddisks
,
1678 int *chunk
, unsigned long long size
,
1679 char *subdev
, unsigned long long *freesize
,
1682 unsigned long long ldsize
;
1685 if (level
== LEVEL_CONTAINER
) {
1687 fprintf(stderr
, Name
": 1.x metadata does not support containers\n");
1690 if (chunk
&& *chunk
== UnSet
)
1691 *chunk
= DEFAULT_CHUNK
;
1696 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
1699 fprintf(stderr
, Name
": super1.x cannot open %s: %s\n",
1700 subdev
, strerror(errno
));
1704 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
1710 *freesize
= avail_size1(st
, ldsize
>> 9);
1713 #endif /* MDASSEMBLE */
1715 struct superswitch super1
= {
1717 .examine_super
= examine_super1
,
1718 .brief_examine_super
= brief_examine_super1
,
1719 .export_examine_super
= export_examine_super1
,
1720 .detail_super
= detail_super1
,
1721 .brief_detail_super
= brief_detail_super1
,
1722 .export_detail_super
= export_detail_super1
,
1723 .write_init_super
= write_init_super1
,
1724 .validate_geometry
= validate_geometry1
,
1725 .add_to_super
= add_to_super1
,
1727 .match_home
= match_home1
,
1728 .uuid_from_super
= uuid_from_super1
,
1729 .getinfo_super
= getinfo_super1
,
1730 .container_content
= container_content1
,
1731 .update_super
= update_super1
,
1732 .init_super
= init_super1
,
1733 .store_super
= store_super1
,
1734 .compare_super
= compare_super1
,
1735 .load_super
= load_super1
,
1736 .match_metadata_desc
= match_metadata_desc1
,
1737 .avail_size
= avail_size1
,
1738 .add_internal_bitmap
= add_internal_bitmap1
,
1739 .locate_bitmap
= locate_bitmap1
,
1740 .write_bitmap
= write_bitmap1
,
1741 .free_super
= free_super1
,
1742 #if __BYTE_ORDER == BIG_ENDIAN