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 #define MAX_SB_SIZE 4096
93 /* bitmap super size is 256, but we round up to a sector for alignment */
94 #define BM_SUPER_SIZE 512
95 #define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
96 #define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
97 + sizeof(struct misc_dev_info))
99 struct misc_dev_info
{
103 /* feature_map bits */
104 #define MD_FEATURE_BITMAP_OFFSET 1
105 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
108 #define MD_FEATURE_RESHAPE_ACTIVE 4
110 #define MD_FEATURE_ALL (1|2|4)
113 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
115 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
117 unsigned int disk_csum
, csum
;
118 unsigned long long newcsum
;
119 int size
= sizeof(*sb
) + __le32_to_cpu(sb
->max_dev
)*2;
120 unsigned int *isuper
= (unsigned int*)sb
;
122 /* make sure I can count... */
123 if (offsetof(struct mdp_superblock_1
,data_offset
) != 128 ||
124 offsetof(struct mdp_superblock_1
, utime
) != 192 ||
125 sizeof(struct mdp_superblock_1
) != 256) {
126 fprintf(stderr
, "WARNING - superblock isn't sized correctly\n");
129 disk_csum
= sb
->sb_csum
;
132 for (; size
>=4; size
-= 4 ) {
133 newcsum
+= __le32_to_cpu(*isuper
);
138 newcsum
+= __le16_to_cpu(*(unsigned short*) isuper
);
140 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
141 sb
->sb_csum
= disk_csum
;
142 return __cpu_to_le32(csum
);
146 * Information related to file descriptor used for aligned reads/writes.
147 * Cache the block size.
154 static void init_afd(struct align_fd
*afd
, int fd
)
158 if (ioctl(afd
->fd
, BLKSSZGET
, &afd
->blk_sz
) != 0)
162 static char abuf
[4096+4096];
163 static int aread(struct align_fd
*afd
, void *buf
, int len
)
166 * On devices with a 4K sector size, we need to read
167 * the full sector and copy relevant bits into
176 if (!bsize
|| bsize
> 4096 || len
> 4096) {
178 fprintf(stderr
, "WARNING - aread() called with "
179 "invalid block size\n");
182 b
= ROUND_UP_PTR((char *)abuf
, 4096);
184 for (iosize
= 0; iosize
< len
; iosize
+= bsize
)
186 n
= read(afd
->fd
, b
, iosize
);
189 lseek(afd
->fd
, len
- n
, 1);
196 static int awrite(struct align_fd
*afd
, void *buf
, int len
)
199 * On devices with a 4K sector size, we need to write
200 * the full sector. We pre-read if the sector is larger
202 * The address must be sector-aligned.
209 if (!bsize
|| bsize
> 4096 || len
> 4096) {
211 fprintf(stderr
, "WARNING - awrite() called with "
212 "invalid block size\n");
215 b
= ROUND_UP_PTR((char *)abuf
, 4096);
217 for (iosize
= 0; iosize
< len
; iosize
+= bsize
)
221 n
= read(afd
->fd
, b
, iosize
);
224 lseek(afd
->fd
, -n
, 1);
228 n
= write(afd
->fd
, b
, iosize
);
231 lseek(afd
->fd
, len
- n
, 1);
236 static void examine_super1(struct supertype
*st
, char *homehost
)
238 struct mdp_superblock_1
*sb
= st
->sb
;
245 int l
= homehost
? strlen(homehost
) : 0;
247 unsigned long long sb_offset
;
249 printf(" Magic : %08x\n", __le32_to_cpu(sb
->magic
));
250 printf(" Version : 1");
251 sb_offset
= __le64_to_cpu(sb
->super_offset
);
254 else if (sb_offset
<= 8)
258 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb
->feature_map
));
259 printf(" Array UUID : ");
260 for (i
=0; i
<16; i
++) {
261 if ((i
&3)==0 && i
!= 0) printf(":");
262 printf("%02x", sb
->set_uuid
[i
]);
265 printf(" Name : %.32s", sb
->set_name
);
266 if (l
> 0 && l
< 32 &&
267 sb
->set_name
[l
] == ':' &&
268 strncmp(sb
->set_name
, homehost
, l
) == 0)
269 printf(" (local to host %s)", homehost
);
271 atime
= __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
;
272 printf(" Creation Time : %.24s\n", ctime(&atime
));
273 c
=map_num(pers
, __le32_to_cpu(sb
->level
));
274 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
275 printf(" Raid Devices : %d\n", __le32_to_cpu(sb
->raid_disks
));
277 printf(" Avail Dev Size : %llu%s\n",
278 (unsigned long long)__le64_to_cpu(sb
->data_size
),
279 human_size(__le64_to_cpu(sb
->data_size
)<<9));
280 if (__le32_to_cpu(sb
->level
) > 0) {
281 int ddsks
= 0, ddsks_denom
= 1;
282 switch(__le32_to_cpu(sb
->level
)) {
283 case 1: ddsks
=1;break;
285 case 5: ddsks
= __le32_to_cpu(sb
->raid_disks
)-1; break;
286 case 6: ddsks
= __le32_to_cpu(sb
->raid_disks
)-2; break;
288 layout
= __le32_to_cpu(sb
->layout
);
289 ddsks
= __le32_to_cpu(sb
->raid_disks
);
290 ddsks_denom
= (layout
&255) * ((layout
>>8)&255);
293 long long asize
= __le64_to_cpu(sb
->size
);
294 asize
= (asize
<< 9) * ddsks
/ ddsks_denom
;
295 printf(" Array Size : %llu%s\n",
296 asize
>> 10, human_size(asize
));
298 if (sb
->size
!= sb
->data_size
)
299 printf(" Used Dev Size : %llu%s\n",
300 (unsigned long long)__le64_to_cpu(sb
->size
),
301 human_size(__le64_to_cpu(sb
->size
)<<9));
304 printf(" Data Offset : %llu sectors\n",
305 (unsigned long long)__le64_to_cpu(sb
->data_offset
));
306 printf(" Super Offset : %llu sectors\n",
307 (unsigned long long)__le64_to_cpu(sb
->super_offset
));
308 if (__le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
)
309 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->recovery_offset
));
310 printf(" State : %s\n", (__le64_to_cpu(sb
->resync_offset
)+1)? "active":"clean");
311 printf(" Device UUID : ");
312 for (i
=0; i
<16; i
++) {
313 if ((i
&3)==0 && i
!= 0) printf(":");
314 printf("%02x", sb
->device_uuid
[i
]);
318 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
319 printf("Internal Bitmap : %ld sectors from superblock\n",
320 (long)(int32_t)__le32_to_cpu(sb
->bitmap_offset
));
322 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
323 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb
->reshape_position
)/2,
324 human_size(__le64_to_cpu(sb
->reshape_position
)<<9));
325 if (__le32_to_cpu(sb
->delta_disks
)) {
326 printf(" Delta Devices : %d", __le32_to_cpu(sb
->delta_disks
));
327 printf(" (%d->%d)\n",
328 __le32_to_cpu(sb
->raid_disks
)-__le32_to_cpu(sb
->delta_disks
),
329 __le32_to_cpu(sb
->raid_disks
));
330 if ((int)__le32_to_cpu(sb
->delta_disks
) < 0)
331 delta_extra
= -__le32_to_cpu(sb
->delta_disks
);
333 if (__le32_to_cpu(sb
->new_level
) != __le32_to_cpu(sb
->level
)) {
334 c
= map_num(pers
, __le32_to_cpu(sb
->new_level
));
335 printf(" New Level : %s\n", c
?c
:"-unknown-");
337 if (__le32_to_cpu(sb
->new_layout
) != __le32_to_cpu(sb
->layout
)) {
338 if (__le32_to_cpu(sb
->level
) == 5) {
339 c
= map_num(r5layout
, __le32_to_cpu(sb
->new_layout
));
340 printf(" New Layout : %s\n", c
?c
:"-unknown-");
342 if (__le32_to_cpu(sb
->level
) == 6) {
343 c
= map_num(r6layout
, __le32_to_cpu(sb
->new_layout
));
344 printf(" New Layout : %s\n", c
?c
:"-unknown-");
346 if (__le32_to_cpu(sb
->level
) == 10) {
347 printf(" New Layout :");
348 print_r10_layout(__le32_to_cpu(sb
->new_layout
));
352 if (__le32_to_cpu(sb
->new_chunk
) != __le32_to_cpu(sb
->chunksize
))
353 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb
->new_chunk
)/2);
358 if (sb
->devflags
& WriteMostly1
)
359 printf(" write-mostly");
363 atime
= __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
;
364 printf(" Update Time : %.24s\n", ctime(&atime
));
366 if (calc_sb_1_csum(sb
) == sb
->sb_csum
)
367 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb
->sb_csum
));
369 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb
->sb_csum
),
370 __le32_to_cpu(calc_sb_1_csum(sb
)));
371 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb
->events
));
373 if (__le32_to_cpu(sb
->level
) == 5) {
374 c
= map_num(r5layout
, __le32_to_cpu(sb
->layout
));
375 printf(" Layout : %s\n", c
?c
:"-unknown-");
377 if (__le32_to_cpu(sb
->level
) == 6) {
378 c
= map_num(r6layout
, __le32_to_cpu(sb
->layout
));
379 printf(" Layout : %s\n", c
?c
:"-unknown-");
381 if (__le32_to_cpu(sb
->level
) == 10) {
382 int lo
= __le32_to_cpu(sb
->layout
);
384 print_r10_layout(lo
);
387 switch(__le32_to_cpu(sb
->level
)) {
393 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
396 printf(" Rounding : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
402 /* This turns out to just be confusing */
403 printf(" Array Slot : %d (", __le32_to_cpu(sb
->dev_number
));
404 for (i
= __le32_to_cpu(sb
->max_dev
); i
> 0 ; i
--)
405 if (__le16_to_cpu(sb
->dev_roles
[i
-1]) != 0xffff)
407 for (d
=0; d
< i
; d
++) {
408 int role
= __le16_to_cpu(sb
->dev_roles
[d
]);
410 if (role
== 0xffff) printf("empty");
411 else if(role
== 0xfffe) printf("failed");
412 else printf("%d", role
);
416 printf(" Device Role : ");
417 d
= __le32_to_cpu(sb
->dev_number
);
418 if (d
< __le32_to_cpu(sb
->max_dev
))
419 role
= __le16_to_cpu(sb
->dev_roles
[d
]);
425 printf("Active device %d\n", role
);
427 printf(" Array State : ");
428 for (d
=0; d
<__le32_to_cpu(sb
->raid_disks
) + delta_extra
; d
++) {
431 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
432 unsigned int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
436 if (cnt
> 1) printf("?");
437 else if (cnt
== 1) printf("A");
441 /* This is confusing too */
443 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
444 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
448 if (faulty
) printf(" %d failed", faulty
);
450 printf(" ('A' == active, '.' == missing)");
455 static void brief_examine_super1(struct supertype
*st
, int verbose
)
457 struct mdp_superblock_1
*sb
= st
->sb
;
459 unsigned long long sb_offset
;
461 char *c
=map_num(pers
, __le32_to_cpu(sb
->level
));
463 nm
= strchr(sb
->set_name
, ':');
466 else if (sb
->set_name
[0])
471 printf("ARRAY%s%s", nm
? " /dev/md/":"", nm
);
473 printf(" level=%s", c
);
474 sb_offset
= __le64_to_cpu(sb
->super_offset
);
476 printf(" metadata=1.1 ");
477 else if (sb_offset
<= 8)
478 printf(" metadata=1.2 ");
480 printf(" metadata=1.0 ");
482 printf("num-devices=%d ", __le32_to_cpu(sb
->raid_disks
));
484 for (i
=0; i
<16; i
++) {
485 if ((i
&3)==0 && i
!= 0) printf(":");
486 printf("%02x", sb
->set_uuid
[i
]);
489 printf(" name=%.32s", sb
->set_name
);
493 static void export_examine_super1(struct supertype
*st
)
495 struct mdp_superblock_1
*sb
= st
->sb
;
499 printf("MD_LEVEL=%s\n", map_num(pers
, __le32_to_cpu(sb
->level
)));
500 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb
->raid_disks
));
502 if (sb
->set_name
[i
] == '\n' ||
503 sb
->set_name
[i
] == '\0') {
508 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
510 for (i
=0; i
<16; i
++) {
511 if ((i
&3)==0 && i
!= 0) printf(":");
512 printf("%02x", sb
->set_uuid
[i
]);
515 printf("MD_UPDATE_TIME=%llu\n",
516 __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
);
517 printf("MD_DEV_UUID=");
518 for (i
=0; i
<16; i
++) {
519 if ((i
&3)==0 && i
!= 0) printf(":");
520 printf("%02x", sb
->device_uuid
[i
]);
523 printf("MD_EVENTS=%llu\n",
524 (unsigned long long)__le64_to_cpu(sb
->events
));
527 static void detail_super1(struct supertype
*st
, char *homehost
)
529 struct mdp_superblock_1
*sb
= st
->sb
;
531 int l
= homehost
? strlen(homehost
) : 0;
533 printf(" Name : %.32s", sb
->set_name
);
534 if (l
> 0 && l
< 32 &&
535 sb
->set_name
[l
] == ':' &&
536 strncmp(sb
->set_name
, homehost
, l
) == 0)
537 printf(" (local to host %s)", homehost
);
538 printf("\n UUID : ");
539 for (i
=0; i
<16; i
++) {
540 if ((i
&3)==0 && i
!= 0) printf(":");
541 printf("%02x", sb
->set_uuid
[i
]);
543 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb
->events
));
546 static void brief_detail_super1(struct supertype
*st
)
548 struct mdp_superblock_1
*sb
= st
->sb
;
552 printf(" name=%.32s", sb
->set_name
);
554 for (i
=0; i
<16; i
++) {
555 if ((i
&3)==0 && i
!= 0) printf(":");
556 printf("%02x", sb
->set_uuid
[i
]);
560 static void export_detail_super1(struct supertype
*st
)
562 struct mdp_superblock_1
*sb
= st
->sb
;
567 if (sb
->set_name
[i
] == '\n' ||
568 sb
->set_name
[i
] == '\0') {
573 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
578 static int match_home1(struct supertype
*st
, char *homehost
)
580 struct mdp_superblock_1
*sb
= st
->sb
;
581 int l
= homehost
? strlen(homehost
) : 0;
583 return (l
> 0 && l
< 32 &&
584 sb
->set_name
[l
] == ':' &&
585 strncmp(sb
->set_name
, homehost
, l
) == 0);
588 static void uuid_from_super1(struct supertype
*st
, int uuid
[4])
590 struct mdp_superblock_1
*super
= st
->sb
;
591 char *cuuid
= (char*)uuid
;
594 cuuid
[i
] = super
->set_uuid
[i
];
597 static void getinfo_super1(struct supertype
*st
, struct mdinfo
*info
, char *map
)
599 struct mdp_superblock_1
*sb
= st
->sb
;
603 unsigned int map_disks
= info
->array
.raid_disks
;
605 memset(info
, 0, sizeof(*info
));
606 info
->array
.major_version
= 1;
607 info
->array
.minor_version
= st
->minor_version
;
608 info
->array
.patch_version
= 0;
609 info
->array
.raid_disks
= __le32_to_cpu(sb
->raid_disks
);
610 info
->array
.level
= __le32_to_cpu(sb
->level
);
611 info
->array
.layout
= __le32_to_cpu(sb
->layout
);
612 info
->array
.md_minor
= -1;
613 info
->array
.ctime
= __le64_to_cpu(sb
->ctime
);
614 info
->array
.utime
= __le64_to_cpu(sb
->utime
);
615 info
->array
.chunk_size
= __le32_to_cpu(sb
->chunksize
)*512;
617 (__le64_to_cpu(sb
->resync_offset
) == MaxSector
)
620 info
->data_offset
= __le64_to_cpu(sb
->data_offset
);
621 info
->component_size
= __le64_to_cpu(sb
->size
);
622 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET
))
623 info
->bitmap_offset
= (int32_t)__le32_to_cpu(sb
->bitmap_offset
);
625 info
->disk
.major
= 0;
626 info
->disk
.minor
= 0;
627 info
->disk
.number
= __le32_to_cpu(sb
->dev_number
);
628 if (__le32_to_cpu(sb
->dev_number
) >= __le32_to_cpu(sb
->max_dev
) ||
629 __le32_to_cpu(sb
->dev_number
) >= MAX_DEVS
)
632 role
= __le16_to_cpu(sb
->dev_roles
[__le32_to_cpu(sb
->dev_number
)]);
634 info
->disk
.raid_disk
= -1;
637 info
->disk
.state
= 0; /* spare: not active, not sync, not faulty */
640 info
->disk
.state
= 1; /* faulty */
643 info
->disk
.state
= 6; /* active and in sync */
644 info
->disk
.raid_disk
= role
;
646 if (sb
->devflags
& WriteMostly1
)
647 info
->disk
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
648 info
->events
= __le64_to_cpu(sb
->events
);
649 sprintf(info
->text_version
, "1.%d", st
->minor_version
);
650 info
->safe_mode_delay
= 200;
652 memcpy(info
->uuid
, sb
->set_uuid
, 16);
654 strncpy(info
->name
, sb
->set_name
, 32);
657 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET
))
658 info
->recovery_start
= __le32_to_cpu(sb
->recovery_offset
);
660 info
->recovery_start
= MaxSector
;
662 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
663 info
->reshape_active
= 1;
664 info
->reshape_progress
= __le64_to_cpu(sb
->reshape_position
);
665 info
->new_level
= __le32_to_cpu(sb
->new_level
);
666 info
->delta_disks
= __le32_to_cpu(sb
->delta_disks
);
667 info
->new_layout
= __le32_to_cpu(sb
->new_layout
);
668 info
->new_chunk
= __le32_to_cpu(sb
->new_chunk
)<<9;
669 if (info
->delta_disks
< 0)
670 info
->array
.raid_disks
-= info
->delta_disks
;
672 info
->reshape_active
= 0;
674 info
->recovery_blocked
= info
->reshape_active
;
677 for (i
=0; i
<map_disks
; i
++)
679 for (i
= 0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
680 role
= __le16_to_cpu(sb
->dev_roles
[i
]);
681 if (/*role == 0xFFFF || */role
< (unsigned) info
->array
.raid_disks
) {
683 if (map
&& role
< map_disks
)
688 info
->array
.working_disks
= working
;
691 static struct mdinfo
*container_content1(struct supertype
*st
, char *subarray
)
698 info
= xmalloc(sizeof(*info
));
699 getinfo_super1(st
, info
, NULL
);
703 static int update_super1(struct supertype
*st
, struct mdinfo
*info
,
705 char *devname
, int verbose
,
706 int uuid_set
, char *homehost
)
708 /* NOTE: for 'assemble' and 'force' we need to return non-zero
709 * if any change was made. For others, the return value is
713 struct mdp_superblock_1
*sb
= st
->sb
;
715 if (strcmp(update
, "force-one")==0) {
716 /* Not enough devices for a working array,
717 * so bring this one up-to-date
719 if (sb
->events
!= __cpu_to_le64(info
->events
))
721 sb
->events
= __cpu_to_le64(info
->events
);
722 } else if (strcmp(update
, "force-array")==0) {
723 /* Degraded array and 'force' requests to
724 * maybe need to mark it 'clean'.
726 switch(__le32_to_cpu(sb
->level
)) {
727 case 5: case 4: case 6:
728 /* need to force clean */
729 if (sb
->resync_offset
!= MaxSector
)
731 sb
->resync_offset
= MaxSector
;
733 } else if (strcmp(update
, "assemble")==0) {
734 int d
= info
->disk
.number
;
736 if (info
->disk
.state
== 6)
737 want
= info
->disk
.raid_disk
;
740 if (sb
->dev_roles
[d
] != __cpu_to_le16(want
)) {
741 sb
->dev_roles
[d
] = __cpu_to_le16(want
);
744 if (info
->reshape_active
&&
745 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
746 info
->delta_disks
>= 0 &&
747 info
->reshape_progress
< __le64_to_cpu(sb
->reshape_position
)) {
748 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
751 if (info
->reshape_active
&&
752 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
753 info
->delta_disks
< 0 &&
754 info
->reshape_progress
> __le64_to_cpu(sb
->reshape_position
)) {
755 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
758 } else if (strcmp(update
, "linear-grow-new") == 0) {
761 unsigned int max
= __le32_to_cpu(sb
->max_dev
);
763 for (i
=0 ; i
< max
; i
++)
764 if (__le16_to_cpu(sb
->dev_roles
[i
]) >= 0xfffe)
766 sb
->dev_number
= __cpu_to_le32(i
);
767 info
->disk
.number
= i
;
768 if (max
>= __le32_to_cpu(sb
->max_dev
))
769 sb
->max_dev
= __cpu_to_le32(max
+1);
771 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
772 read(rfd
, sb
->device_uuid
, 16) != 16) {
773 __u32 r
[4] = {random(), random(), random(), random()};
774 memcpy(sb
->device_uuid
, r
, 16);
780 __cpu_to_le16(info
->disk
.raid_disk
);
782 fd
= open(devname
, O_RDONLY
);
784 unsigned long long ds
;
785 get_dev_size(fd
, devname
, &ds
);
788 if (__le64_to_cpu(sb
->super_offset
) <
789 __le64_to_cpu(sb
->data_offset
)) {
790 sb
->data_size
= __cpu_to_le64(
791 ds
- __le64_to_cpu(sb
->data_offset
));
794 ds
&= ~(unsigned long long)(4*2-1);
795 sb
->super_offset
= __cpu_to_le64(ds
);
796 sb
->data_size
= __cpu_to_le64(
797 ds
- __le64_to_cpu(sb
->data_offset
));
800 } else if (strcmp(update
, "linear-grow-update") == 0) {
801 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
802 sb
->dev_roles
[info
->disk
.number
] =
803 __cpu_to_le16(info
->disk
.raid_disk
);
804 } else if (strcmp(update
, "resync") == 0) {
805 /* make sure resync happens */
806 sb
->resync_offset
= 0ULL;
807 } else if (strcmp(update
, "uuid") == 0) {
808 copy_uuid(sb
->set_uuid
, info
->uuid
, super1
.swapuuid
);
810 if (__le32_to_cpu(sb
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
811 struct bitmap_super_s
*bm
;
812 bm
= (struct bitmap_super_s
*)(st
->sb
+MAX_SB_SIZE
);
813 memcpy(bm
->uuid
, sb
->set_uuid
, 16);
815 } else if (strcmp(update
, "no-bitmap") == 0) {
816 sb
->feature_map
&= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
817 } else if (strcmp(update
, "homehost") == 0 &&
821 c
= strchr(sb
->set_name
, ':');
823 strncpy(info
->name
, c
+1, 31 - (c
-sb
->set_name
));
825 strncpy(info
->name
, sb
->set_name
, 32);
827 } else if (strcmp(update
, "name") == 0) {
828 if (info
->name
[0] == 0)
829 sprintf(info
->name
, "%d", info
->array
.md_minor
);
830 memset(sb
->set_name
, 0, sizeof(sb
->set_name
));
832 strchr(info
->name
, ':') == NULL
&&
833 strlen(homehost
)+1+strlen(info
->name
) < 32) {
834 strcpy(sb
->set_name
, homehost
);
835 strcat(sb
->set_name
, ":");
836 strcat(sb
->set_name
, info
->name
);
838 strcpy(sb
->set_name
, info
->name
);
839 } else if (strcmp(update
, "devicesize") == 0 &&
840 __le64_to_cpu(sb
->super_offset
) <
841 __le64_to_cpu(sb
->data_offset
)) {
842 /* set data_size to device size less data_offset */
843 struct misc_dev_info
*misc
= (struct misc_dev_info
*)
844 (st
->sb
+ MAX_SB_SIZE
+ BM_SUPER_SIZE
);
845 printf("Size was %llu\n", (unsigned long long)
846 __le64_to_cpu(sb
->data_size
));
847 sb
->data_size
= __cpu_to_le64(
848 misc
->device_size
- __le64_to_cpu(sb
->data_offset
));
849 printf("Size is %llu\n", (unsigned long long)
850 __le64_to_cpu(sb
->data_size
));
851 } else if (strcmp(update
, "_reshape_progress")==0)
852 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
853 else if (strcmp(update
, "writemostly")==0)
854 sb
->devflags
|= WriteMostly1
;
855 else if (strcmp(update
, "readwrite")==0)
856 sb
->devflags
&= ~WriteMostly1
;
860 sb
->sb_csum
= calc_sb_1_csum(sb
);
864 static int init_super1(struct supertype
*st
, mdu_array_info_t
*info
,
865 unsigned long long size
, char *name
, char *homehost
, int *uuid
)
867 struct mdp_superblock_1
*sb
;
873 if (posix_memalign((void**)&sb
, 4096, SUPER1_SIZE
) != 0) {
874 pr_err("%s could not allocate superblock\n", __func__
);
877 memset(sb
, 0, SUPER1_SIZE
);
881 /* zeroing superblock */
885 spares
= info
->working_disks
- info
->active_disks
;
886 if (info
->raid_disks
+ spares
> MAX_DEVS
) {
887 pr_err("too many devices requested: %d+%d > %d\n",
888 info
->raid_disks
, spares
, MAX_DEVS
);
892 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
893 sb
->major_version
= __cpu_to_le32(1);
898 copy_uuid(sb
->set_uuid
, uuid
, super1
.swapuuid
);
900 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
901 read(rfd
, sb
->set_uuid
, 16) != 16) {
902 __u32 r
[4] = {random(), random(), random(), random()};
903 memcpy(sb
->set_uuid
, r
, 16);
905 if (rfd
>= 0) close(rfd
);
908 if (name
== NULL
|| *name
== 0) {
909 sprintf(defname
, "%d", info
->md_minor
);
913 strchr(name
, ':')== NULL
&&
914 strlen(homehost
)+1+strlen(name
) < 32) {
915 strcpy(sb
->set_name
, homehost
);
916 strcat(sb
->set_name
, ":");
917 strcat(sb
->set_name
, name
);
919 strcpy(sb
->set_name
, name
);
921 sb
->ctime
= __cpu_to_le64((unsigned long long)time(0));
922 sb
->level
= __cpu_to_le32(info
->level
);
923 sb
->layout
= __cpu_to_le32(info
->layout
);
924 sb
->size
= __cpu_to_le64(size
*2ULL);
925 sb
->chunksize
= __cpu_to_le32(info
->chunk_size
>>9);
926 sb
->raid_disks
= __cpu_to_le32(info
->raid_disks
);
928 sb
->data_offset
= __cpu_to_le64(0);
929 sb
->data_size
= __cpu_to_le64(0);
930 sb
->super_offset
= __cpu_to_le64(0);
931 sb
->recovery_offset
= __cpu_to_le64(0);
933 sb
->utime
= sb
->ctime
;
934 sb
->events
= __cpu_to_le64(1);
935 if (info
->state
& (1<<MD_SB_CLEAN
))
936 sb
->resync_offset
= MaxSector
;
938 sb
->resync_offset
= 0;
939 sbsize
= sizeof(struct mdp_superblock_1
) + 2 * (info
->raid_disks
+ spares
);
940 sbsize
= ROUND_UP(sbsize
, 512);
941 sb
->max_dev
= __cpu_to_le32((sbsize
- sizeof(struct mdp_superblock_1
)) / 2);
943 memset(sb
->dev_roles
, 0xff, MAX_SB_SIZE
- sizeof(struct mdp_superblock_1
));
951 mdu_disk_info_t disk
;
952 struct devinfo
*next
;
955 /* Add a device to the superblock being created */
956 static int add_to_super1(struct supertype
*st
, mdu_disk_info_t
*dk
,
957 int fd
, char *devname
)
959 struct mdp_superblock_1
*sb
= st
->sb
;
960 __u16
*rp
= sb
->dev_roles
+ dk
->number
;
961 struct devinfo
*di
, **dip
;
963 if ((dk
->state
& 6) == 6) /* active, sync */
964 *rp
= __cpu_to_le16(dk
->raid_disk
);
965 else if ((dk
->state
& ~2) == 0) /* active or idle -> spare */
970 if (dk
->number
>= (int)__le32_to_cpu(sb
->max_dev
) &&
971 __le32_to_cpu(sb
->max_dev
) < MAX_DEVS
)
972 sb
->max_dev
= __cpu_to_le32(dk
->number
+1);
974 sb
->dev_number
= __cpu_to_le32(dk
->number
);
975 sb
->devflags
= 0; /* don't copy another disks flags */
976 sb
->sb_csum
= calc_sb_1_csum(sb
);
978 dip
= (struct devinfo
**)&st
->info
;
981 di
= xmalloc(sizeof(struct devinfo
));
983 di
->devname
= devname
;
992 static void locate_bitmap1(struct supertype
*st
, int fd
);
994 static int store_super1(struct supertype
*st
, int fd
)
996 struct mdp_superblock_1
*sb
= st
->sb
;
997 unsigned long long sb_offset
;
1000 unsigned long long dsize
;
1002 if (!get_dev_size(fd
, NULL
, &dsize
))
1013 * Calculate the position of the superblock.
1014 * It is always aligned to a 4K boundary and
1015 * depending on minor_version, it can be:
1016 * 0: At least 8K, but less than 12K, from end of device
1017 * 1: At start of device
1018 * 2: 4K from start of device.
1020 switch(st
->minor_version
) {
1024 sb_offset
&= ~(4*2-1);
1038 if (sb_offset
!= __le64_to_cpu(sb
->super_offset
) &&
1039 0 != __le64_to_cpu(sb
->super_offset
)
1041 pr_err("internal error - sb_offset is wrong\n");
1045 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL)
1048 sbsize
= ROUND_UP(sizeof(*sb
) + 2 * __le32_to_cpu(sb
->max_dev
), 512);
1050 if (awrite(&afd
, sb
, sbsize
) != sbsize
)
1053 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
1054 struct bitmap_super_s
*bm
= (struct bitmap_super_s
*)
1055 (((char*)sb
)+MAX_SB_SIZE
);
1056 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
) {
1057 locate_bitmap1(st
, fd
);
1058 if (awrite(&afd
, bm
, sizeof(*bm
)) != sizeof(*bm
))
1066 static int load_super1(struct supertype
*st
, int fd
, char *devname
);
1068 static unsigned long choose_bm_space(unsigned long devsize
)
1070 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1071 * if bigger than 200Gig, save 128k
1072 * NOTE: result must be multiple of 4K else bad things happen
1073 * on 4K-sector devices.
1075 if (devsize
< 64*2) return 0;
1076 if (devsize
- 64*2 >= 200*1024*1024*2)
1078 if (devsize
- 4*2 > 8*1024*1024*2)
1083 static void free_super1(struct supertype
*st
);
1086 static int write_init_super1(struct supertype
*st
)
1088 struct mdp_superblock_1
*sb
= st
->sb
;
1089 struct supertype
*refst
;
1092 unsigned long long bm_space
;
1093 unsigned long long reserved
;
1095 unsigned long long dsize
, array_size
;
1096 unsigned long long sb_offset
, headroom
;
1098 for (di
= st
->info
; di
; di
= di
->next
) {
1099 if (di
->disk
.state
== 1)
1104 while (Kill(di
->devname
, NULL
, 0, -1, 1) == 0)
1107 sb
->dev_number
= __cpu_to_le32(di
->disk
.number
);
1108 if (di
->disk
.state
& (1<<MD_DISK_WRITEMOSTLY
))
1109 sb
->devflags
|= WriteMostly1
;
1111 sb
->devflags
&= ~WriteMostly1
;
1113 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
1114 read(rfd
, sb
->device_uuid
, 16) != 16) {
1115 __u32 r
[4] = {random(), random(), random(), random()};
1116 memcpy(sb
->device_uuid
, r
, 16);
1123 refst
= dup_super(st
);
1124 if (load_super1(refst
, di
->fd
, NULL
)==0) {
1125 struct mdp_superblock_1
*refsb
= refst
->sb
;
1127 memcpy(sb
->device_uuid
, refsb
->device_uuid
, 16);
1128 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16)==0) {
1129 /* same array, so preserve events and
1131 sb
->events
= refsb
->events
;
1132 /* bugs in 2.6.17 and earlier mean the
1133 * dev_number chosen in Manage must be preserved
1135 if (get_linux_version() >= 2006018)
1136 sb
->dev_number
= refsb
->dev_number
;
1142 if (!get_dev_size(di
->fd
, NULL
, &dsize
)) {
1156 * Calculate the position of the superblock.
1157 * It is always aligned to a 4K boundary and
1158 * depending on minor_version, it can be:
1159 * 0: At least 8K, but less than 12K, from end of device
1160 * 1: At start of device
1161 * 2: 4K from start of device.
1162 * Depending on the array size, we might leave extra space
1165 array_size
= __le64_to_cpu(sb
->size
);
1166 /* work out how much space we left for a bitmap */
1167 bm_space
= choose_bm_space(array_size
);
1169 /* We try to leave 0.1% at the start for reshape
1170 * operations, but limit this to 128Meg (0.1% of 10Gig)
1171 * which is plenty for efficient reshapes
1173 headroom
= 128 * 1024 * 2;
1174 while (headroom
<< 10 > array_size
)
1177 switch(st
->minor_version
) {
1181 sb_offset
&= ~(4*2-1);
1182 sb
->super_offset
= __cpu_to_le64(sb_offset
);
1183 sb
->data_offset
= __cpu_to_le64(0);
1184 if (sb_offset
< array_size
+ bm_space
)
1185 bm_space
= sb_offset
- array_size
;
1186 sb
->data_size
= __cpu_to_le64(sb_offset
- bm_space
);
1189 sb
->super_offset
= __cpu_to_le64(0);
1190 reserved
= bm_space
+ 4*2;
1191 if (reserved
< headroom
)
1192 reserved
= headroom
;
1193 if (reserved
+ array_size
> dsize
)
1194 reserved
= dsize
- array_size
;
1195 /* Try for multiple of 1Meg so it is nicely aligned */
1196 #define ONE_MEG (2*1024)
1197 if (reserved
> ONE_MEG
)
1198 reserved
= (reserved
/ONE_MEG
) * ONE_MEG
;
1200 /* force 4K alignment */
1203 sb
->data_offset
= __cpu_to_le64(reserved
);
1204 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1208 sb
->super_offset
= __cpu_to_le64(4*2);
1209 if (4*2 + 4*2 + bm_space
+ array_size
1211 bm_space
= dsize
- array_size
1214 reserved
= bm_space
+ 4*2 + 4*2;
1215 if (reserved
< headroom
)
1216 reserved
= headroom
;
1217 if (reserved
+ array_size
> dsize
)
1218 reserved
= dsize
- array_size
;
1219 /* Try for multiple of 1Meg so it is nicely aligned */
1220 #define ONE_MEG (2*1024)
1221 if (reserved
> ONE_MEG
)
1222 reserved
= (reserved
/ONE_MEG
) * ONE_MEG
;
1224 /* force 4K alignment */
1227 sb
->data_offset
= __cpu_to_le64(reserved
);
1228 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1231 pr_err("Failed to write invalid "
1232 "metadata format 1.%i to %s\n",
1233 st
->minor_version
, di
->devname
);
1238 sb
->sb_csum
= calc_sb_1_csum(sb
);
1239 rv
= store_super1(st
, di
->fd
);
1240 if (rv
== 0 && (__le32_to_cpu(sb
->feature_map
) & 1))
1241 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
1249 pr_err("Failed to write metadata to %s\n",
1256 static int compare_super1(struct supertype
*st
, struct supertype
*tst
)
1260 * 0 same, or first was empty, and second was copied
1261 * 1 second had wrong number
1263 * 3 wrong other info
1265 struct mdp_superblock_1
*first
= st
->sb
;
1266 struct mdp_superblock_1
*second
= tst
->sb
;
1268 if (second
->magic
!= __cpu_to_le32(MD_SB_MAGIC
))
1270 if (second
->major_version
!= __cpu_to_le32(1))
1274 if (posix_memalign((void**)&first
, 4096, SUPER1_SIZE
) != 0) {
1275 pr_err("%s could not allocate superblock\n", __func__
);
1278 memcpy(first
, second
, SUPER1_SIZE
);
1282 if (memcmp(first
->set_uuid
, second
->set_uuid
, 16)!= 0)
1285 if (first
->ctime
!= second
->ctime
||
1286 first
->level
!= second
->level
||
1287 first
->layout
!= second
->layout
||
1288 first
->size
!= second
->size
||
1289 first
->chunksize
!= second
->chunksize
||
1290 first
->raid_disks
!= second
->raid_disks
)
1295 static int load_super1(struct supertype
*st
, int fd
, char *devname
)
1297 unsigned long long dsize
;
1298 unsigned long long sb_offset
;
1299 struct mdp_superblock_1
*super
;
1301 struct bitmap_super_s
*bsb
;
1302 struct misc_dev_info
*misc
;
1303 struct align_fd afd
;
1309 if (st
->ss
== NULL
|| st
->minor_version
== -1) {
1311 struct supertype tst
;
1312 __u64 bestctime
= 0;
1313 /* guess... choose latest ctime */
1314 memset(&tst
, 0, sizeof(tst
));
1316 for (tst
.minor_version
= 0; tst
.minor_version
<= 2 ; tst
.minor_version
++) {
1317 switch(load_super1(&tst
, fd
, devname
)) {
1318 case 0: super
= tst
.sb
;
1319 if (bestvers
== -1 ||
1320 bestctime
< __le64_to_cpu(super
->ctime
)) {
1321 bestvers
= tst
.minor_version
;
1322 bestctime
= __le64_to_cpu(super
->ctime
);
1327 case 1: return 1; /*bad device */
1328 case 2: break; /* bad, try next */
1331 if (bestvers
!= -1) {
1333 tst
.minor_version
= bestvers
;
1335 tst
.max_devs
= MAX_DEVS
;
1336 rv
= load_super1(&tst
, fd
, devname
);
1343 if (!get_dev_size(fd
, devname
, &dsize
))
1349 pr_err("%s is too small for md: size is %llu sectors.\n",
1355 * Calculate the position of the superblock.
1356 * It is always aligned to a 4K boundary and
1357 * depending on minor_version, it can be:
1358 * 0: At least 8K, but less than 12K, from end of device
1359 * 1: At start of device
1360 * 2: 4K from start of device.
1362 switch(st
->minor_version
) {
1366 sb_offset
&= ~(4*2-1);
1378 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
1381 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL) {
1383 pr_err("Cannot seek to superblock on %s: %s\n",
1384 devname
, strerror(errno
));
1388 if (posix_memalign((void**)&super
, 4096, SUPER1_SIZE
) != 0) {
1389 pr_err("%s could not allocate superblock\n",
1394 if (aread(&afd
, super
, MAX_SB_SIZE
) != MAX_SB_SIZE
) {
1396 pr_err("Cannot read superblock on %s\n",
1402 if (__le32_to_cpu(super
->magic
) != MD_SB_MAGIC
) {
1404 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
1405 devname
, MD_SB_MAGIC
, __le32_to_cpu(super
->magic
));
1410 if (__le32_to_cpu(super
->major_version
) != 1) {
1412 pr_err("Cannot interpret superblock on %s - version is %d\n",
1413 devname
, __le32_to_cpu(super
->major_version
));
1417 if (__le64_to_cpu(super
->super_offset
) != sb_offset
) {
1419 pr_err("No superblock found on %s (super_offset is wrong)\n",
1426 bsb
= (struct bitmap_super_s
*)(((char*)super
)+MAX_SB_SIZE
);
1428 misc
= (struct misc_dev_info
*) (((char*)super
)+MAX_SB_SIZE
+BM_SUPER_SIZE
);
1429 misc
->device_size
= dsize
;
1431 /* Now check on the bitmap superblock */
1432 if ((__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) == 0)
1434 /* Read the bitmap superblock and make sure it looks
1435 * valid. If it doesn't clear the bit. An --assemble --force
1436 * should get that written out.
1438 locate_bitmap1(st
, fd
);
1439 if (aread(&afd
, bsb
, 512) != 512)
1442 uuid_from_super1(st
, uuid
);
1443 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
1444 memcmp(bsb
->uuid
, uuid
, 16) != 0)
1449 super
->feature_map
= __cpu_to_le32(__le32_to_cpu(super
->feature_map
)
1450 & ~MD_FEATURE_BITMAP_OFFSET
);
1455 static struct supertype
*match_metadata_desc1(char *arg
)
1457 struct supertype
*st
= xcalloc(1, sizeof(*st
));
1459 st
->container_dev
= NoMdDev
;
1461 st
->max_devs
= MAX_DEVS
;
1463 /* leading zeros can be safely ignored. --detail generates them. */
1466 if (strcmp(arg
, "1.0") == 0 ||
1467 strcmp(arg
, "1.00") == 0) {
1468 st
->minor_version
= 0;
1471 if (strcmp(arg
, "1.1") == 0 ||
1472 strcmp(arg
, "1.01") == 0
1474 st
->minor_version
= 1;
1477 if (strcmp(arg
, "1.2") == 0 ||
1478 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1479 strcmp(arg
, "default") == 0 ||
1480 #endif /* DEFAULT_OLD_METADATA */
1481 strcmp(arg
, "1.02") == 0) {
1482 st
->minor_version
= 2;
1485 if (strcmp(arg
, "1") == 0 ||
1486 strcmp(arg
, "default") == 0) {
1487 st
->minor_version
= -1;
1495 /* find available size on device with this devsize, using
1496 * superblock type st, and reserving 'reserve' sectors for
1499 static __u64
avail_size1(struct supertype
*st
, __u64 devsize
)
1501 struct mdp_superblock_1
*super
= st
->sb
;
1506 /* creating: allow suitable space for bitmap */
1507 devsize
-= choose_bm_space(devsize
);
1509 else if (__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
1510 /* hot-add. allow for actual size of bitmap */
1511 struct bitmap_super_s
*bsb
;
1512 bsb
= (struct bitmap_super_s
*)(((char*)super
)+MAX_SB_SIZE
);
1513 devsize
-= bitmap_sectors(bsb
);
1517 if (st
->minor_version
< 0)
1518 /* not specified, so time to set default */
1519 st
->minor_version
= 2;
1520 if (super
== NULL
&& st
->minor_version
> 0) {
1521 /* haven't committed to a size yet, so allow some
1522 * slack for space for reshape.
1523 * Limit slack to 128M, but aim for about 0.1%
1525 unsigned long long headroom
= 128*1024*2;
1526 while ((headroom
<< 10) > devsize
)
1528 devsize
-= headroom
;
1530 switch(st
->minor_version
) {
1533 return ((devsize
- 8*2 ) & ~(4*2-1));
1535 /* at start, 4K for superblock and possible bitmap */
1536 return devsize
- 4*2;
1538 /* 4k from start, 4K for superblock and possible bitmap */
1539 return devsize
- (4+4)*2;
1545 add_internal_bitmap1(struct supertype
*st
,
1546 int *chunkp
, int delay
, int write_behind
,
1547 unsigned long long size
,
1548 int may_change
, int major
)
1551 * If not may_change, then this is a 'Grow' without sysfs support for
1552 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
1553 * If may_change, then this is create or a Grow with sysfs syupport,
1554 * and we can put the bitmap wherever we like.
1556 * size is in sectors, chunk is in bytes !!!
1559 unsigned long long bits
;
1560 unsigned long long max_bits
;
1561 unsigned long long min_chunk
;
1563 unsigned long long chunk
= *chunkp
;
1566 struct mdp_superblock_1
*sb
= st
->sb
;
1567 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + MAX_SB_SIZE
);
1570 if (__le64_to_cpu(sb
->data_size
) == 0)
1571 /* Must be creating the array, else data_size would be non-zero */
1573 switch(st
->minor_version
) {
1575 /* either 3K after the superblock (when hot-add),
1576 * or some amount of space before.
1579 /* We are creating array, so we *know* how much room has
1583 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1585 room
= __le64_to_cpu(sb
->super_offset
)
1586 - __le64_to_cpu(sb
->data_offset
)
1587 - __le64_to_cpu(sb
->data_size
);
1589 if (!may_change
|| (room
< 3*2 &&
1590 __le32_to_cpu(sb
->max_dev
) <= 384)) {
1594 offset
= 0; /* means movable offset */
1599 case 2: /* between superblock and data */
1602 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1604 room
= __le64_to_cpu(sb
->data_offset
)
1605 - __le64_to_cpu(sb
->super_offset
);
1607 room
-= 2; /* Leave 1K for superblock */
1610 room
-= 4*2; /* leave 4K for superblock */
1619 if (chunk
== UnSet
&& room
> 128*2)
1620 /* Limit to 128K of bitmap when chunk size not requested */
1623 max_bits
= (room
* 512 - sizeof(bitmap_super_t
)) * 8;
1625 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
1626 bits
= (size
*512)/min_chunk
+1;
1627 while (bits
> max_bits
) {
1631 if (chunk
== UnSet
) {
1632 /* For practical purpose, 64Meg is a good
1633 * default chunk size for internal bitmaps.
1636 if (chunk
< 64*1024*1024)
1637 chunk
= 64*1024*1024;
1638 } else if (chunk
< min_chunk
)
1639 return 0; /* chunk size too small */
1640 if (chunk
== 0) /* rounding problem */
1644 /* start bitmap on a 4K boundary with enough space for
1647 bits
= (size
*512) / chunk
+ 1;
1648 room
= ((bits
+7)/8 + sizeof(bitmap_super_t
) +4095)/4096;
1649 room
*= 8; /* convert 4K blocks to sectors */
1653 sb
->bitmap_offset
= (int32_t)__cpu_to_le32(offset
);
1655 sb
->feature_map
= __cpu_to_le32(__le32_to_cpu(sb
->feature_map
)
1656 | MD_FEATURE_BITMAP_OFFSET
);
1657 memset(bms
, 0, sizeof(*bms
));
1658 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
1659 bms
->version
= __cpu_to_le32(major
);
1660 uuid_from_super1(st
, uuid
);
1661 memcpy(bms
->uuid
, uuid
, 16);
1662 bms
->chunksize
= __cpu_to_le32(chunk
);
1663 bms
->daemon_sleep
= __cpu_to_le32(delay
);
1664 bms
->sync_size
= __cpu_to_le64(size
);
1665 bms
->write_behind
= __cpu_to_le32(write_behind
);
1671 static void locate_bitmap1(struct supertype
*st
, int fd
)
1673 unsigned long long offset
;
1674 struct mdp_superblock_1
*sb
;
1678 if (st
->ss
->load_super(st
, fd
, NULL
))
1679 return; /* no error I hope... */
1684 offset
= __le64_to_cpu(sb
->super_offset
);
1685 offset
+= (int32_t) __le32_to_cpu(sb
->bitmap_offset
);
1688 lseek64(fd
, offset
<<9, 0);
1691 static int write_bitmap1(struct supertype
*st
, int fd
)
1693 struct mdp_superblock_1
*sb
= st
->sb
;
1694 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
)+MAX_SB_SIZE
);
1698 struct align_fd afd
;
1702 locate_bitmap1(st
, fd
);
1704 if (posix_memalign(&buf
, 4096, 4096))
1707 memset(buf
, 0xff, 4096);
1708 memcpy(buf
, (char *)bms
, sizeof(bitmap_super_t
));
1710 towrite
= __le64_to_cpu(bms
->sync_size
) / (__le32_to_cpu(bms
->chunksize
)>>9);
1711 towrite
= (towrite
+7) >> 3; /* bits to bytes */
1712 towrite
+= sizeof(bitmap_super_t
);
1713 towrite
= ROUND_UP(towrite
, 512);
1714 while (towrite
> 0) {
1718 n
= awrite(&afd
, buf
, n
);
1723 memset(buf
, 0xff, 4096);
1733 static void free_super1(struct supertype
*st
)
1738 struct devinfo
*di
= st
->info
;
1739 st
->info
= di
->next
;
1748 static int validate_geometry1(struct supertype
*st
, int level
,
1749 int layout
, int raiddisks
,
1750 int *chunk
, unsigned long long size
,
1751 char *subdev
, unsigned long long *freesize
,
1754 unsigned long long ldsize
;
1757 if (level
== LEVEL_CONTAINER
) {
1759 pr_err("1.x metadata does not support containers\n");
1762 if (chunk
&& *chunk
== UnSet
)
1763 *chunk
= DEFAULT_CHUNK
;
1768 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
1771 pr_err("super1.x cannot open %s: %s\n",
1772 subdev
, strerror(errno
));
1776 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
1782 *freesize
= avail_size1(st
, ldsize
>> 9);
1785 #endif /* MDASSEMBLE */
1787 struct superswitch super1
= {
1789 .examine_super
= examine_super1
,
1790 .brief_examine_super
= brief_examine_super1
,
1791 .export_examine_super
= export_examine_super1
,
1792 .detail_super
= detail_super1
,
1793 .brief_detail_super
= brief_detail_super1
,
1794 .export_detail_super
= export_detail_super1
,
1795 .write_init_super
= write_init_super1
,
1796 .validate_geometry
= validate_geometry1
,
1797 .add_to_super
= add_to_super1
,
1799 .match_home
= match_home1
,
1800 .uuid_from_super
= uuid_from_super1
,
1801 .getinfo_super
= getinfo_super1
,
1802 .container_content
= container_content1
,
1803 .update_super
= update_super1
,
1804 .init_super
= init_super1
,
1805 .store_super
= store_super1
,
1806 .compare_super
= compare_super1
,
1807 .load_super
= load_super1
,
1808 .match_metadata_desc
= match_metadata_desc1
,
1809 .avail_size
= avail_size1
,
1810 .add_internal_bitmap
= add_internal_bitmap1
,
1811 .locate_bitmap
= locate_bitmap1
,
1812 .write_bitmap
= write_bitmap1
,
1813 .free_super
= free_super1
,
1814 #if __BYTE_ORDER == BIG_ENDIAN