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 __u32 new_offset
; /* signed number to add to data_offset in new
62 * layout. 0 == no-change. This can be
63 * different on each device in the array.
66 /* constant this-device information - 64 bytes */
67 __u64 data_offset
; /* sector start of data, often 0 */
68 __u64 data_size
; /* sectors in this device that can be used for data */
69 __u64 super_offset
; /* sector start of this superblock */
70 __u64 recovery_offset
;/* sectors before this offset (from data_offset) have been recovered */
71 __u32 dev_number
; /* permanent identifier of this device - not role in raid */
72 __u32 cnt_corrected_read
; /* number of read errors that were corrected by re-writing */
73 __u8 device_uuid
[16]; /* user-space setable, ignored by kernel */
74 __u8 devflags
; /* per-device flags. Only one defined...*/
75 #define WriteMostly1 1 /* mask for writemostly flag in above */
76 /* bad block log. If there are any bad blocks the feature flag is set.
77 * if offset and size are non-zero, that space is reserved and available.
79 __u8 bblog_shift
; /* shift from sectors to block size for badblocklist */
80 __u16 bblog_size
; /* number of sectors reserved for badblocklist */
81 __u32 bblog_offset
; /* sector offset from superblock to bblog, signed */
83 /* array state information - 64 bytes */
84 __u64 utime
; /* 40 bits second, 24 btes microseconds */
85 __u64 events
; /* incremented when superblock updated */
86 __u64 resync_offset
; /* data before this offset (from data_offset) known to be in sync */
87 __u32 sb_csum
; /* checksum upto dev_roles[max_dev] */
88 __u32 max_dev
; /* size of dev_roles[] array to consider */
89 __u8 pad3
[64-32]; /* set to 0 when writing */
91 /* device state information. Indexed by dev_number.
93 * Note there are no per-device state flags. State information is rolled
94 * into the 'roles' value. If a device is spare or faulty, then it doesn't
95 * have a meaningful role.
97 __u16 dev_roles
[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
100 #define MAX_SB_SIZE 4096
101 /* bitmap super size is 256, but we round up to a sector for alignment */
102 #define BM_SUPER_SIZE 512
103 #define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
104 #define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
105 + sizeof(struct misc_dev_info))
107 struct misc_dev_info
{
111 /* feature_map bits */
112 #define MD_FEATURE_BITMAP_OFFSET 1
113 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
116 #define MD_FEATURE_RESHAPE_ACTIVE 4
117 #define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
118 #define MD_FEATURE_REPLACEMENT 16 /* This device is replacing an
119 * active device with same 'role'.
120 * 'recovery_offset' is also set.
122 #define MD_FEATURE_RESHAPE_BACKWARDS 32 /* Reshape doesn't change number
123 * of devices, but is going
126 #define MD_FEATURE_NEW_OFFSET 64 /* new_offset must be honoured */
127 #define MD_FEATURE_ALL (MD_FEATURE_BITMAP_OFFSET \
128 |MD_FEATURE_RECOVERY_OFFSET \
129 |MD_FEATURE_RESHAPE_ACTIVE \
130 |MD_FEATURE_BAD_BLOCKS \
131 |MD_FEATURE_REPLACEMENT \
132 |MD_FEATURE_RESHAPE_BACKWARDS \
133 |MD_FEATURE_NEW_OFFSET \
137 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
139 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
141 unsigned int disk_csum
, csum
;
142 unsigned long long newcsum
;
143 int size
= sizeof(*sb
) + __le32_to_cpu(sb
->max_dev
)*2;
144 unsigned int *isuper
= (unsigned int*)sb
;
146 /* make sure I can count... */
147 if (offsetof(struct mdp_superblock_1
,data_offset
) != 128 ||
148 offsetof(struct mdp_superblock_1
, utime
) != 192 ||
149 sizeof(struct mdp_superblock_1
) != 256) {
150 fprintf(stderr
, "WARNING - superblock isn't sized correctly\n");
153 disk_csum
= sb
->sb_csum
;
156 for (; size
>=4; size
-= 4 ) {
157 newcsum
+= __le32_to_cpu(*isuper
);
162 newcsum
+= __le16_to_cpu(*(unsigned short*) isuper
);
164 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
165 sb
->sb_csum
= disk_csum
;
166 return __cpu_to_le32(csum
);
170 * Information related to file descriptor used for aligned reads/writes.
171 * Cache the block size.
178 static void init_afd(struct align_fd
*afd
, int fd
)
182 if (ioctl(afd
->fd
, BLKSSZGET
, &afd
->blk_sz
) != 0)
186 static char abuf
[4096+4096];
187 static int aread(struct align_fd
*afd
, void *buf
, int len
)
190 * On devices with a 4K sector size, we need to read
191 * the full sector and copy relevant bits into
200 if (!bsize
|| bsize
> 4096 || len
> 4096) {
202 fprintf(stderr
, "WARNING - aread() called with "
203 "invalid block size\n");
206 b
= ROUND_UP_PTR((char *)abuf
, 4096);
208 for (iosize
= 0; iosize
< len
; iosize
+= bsize
)
210 n
= read(afd
->fd
, b
, iosize
);
213 lseek(afd
->fd
, len
- n
, 1);
220 static int awrite(struct align_fd
*afd
, void *buf
, int len
)
223 * On devices with a 4K sector size, we need to write
224 * the full sector. We pre-read if the sector is larger
226 * The address must be sector-aligned.
233 if (!bsize
|| bsize
> 4096 || len
> 4096) {
235 fprintf(stderr
, "WARNING - awrite() called with "
236 "invalid block size\n");
239 b
= ROUND_UP_PTR((char *)abuf
, 4096);
241 for (iosize
= 0; iosize
< len
; iosize
+= bsize
)
245 n
= read(afd
->fd
, b
, iosize
);
248 lseek(afd
->fd
, -n
, 1);
252 n
= write(afd
->fd
, b
, iosize
);
255 lseek(afd
->fd
, len
- n
, 1);
260 static void examine_super1(struct supertype
*st
, char *homehost
)
262 struct mdp_superblock_1
*sb
= st
->sb
;
269 int l
= homehost
? strlen(homehost
) : 0;
271 unsigned long long sb_offset
;
274 printf(" Magic : %08x\n", __le32_to_cpu(sb
->magic
));
275 printf(" Version : 1");
276 sb_offset
= __le64_to_cpu(sb
->super_offset
);
279 else if (sb_offset
<= 8)
283 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb
->feature_map
));
284 printf(" Array UUID : ");
285 for (i
=0; i
<16; i
++) {
286 if ((i
&3)==0 && i
!= 0) printf(":");
287 printf("%02x", sb
->set_uuid
[i
]);
290 printf(" Name : %.32s", sb
->set_name
);
291 if (l
> 0 && l
< 32 &&
292 sb
->set_name
[l
] == ':' &&
293 strncmp(sb
->set_name
, homehost
, l
) == 0)
294 printf(" (local to host %s)", homehost
);
296 atime
= __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
;
297 printf(" Creation Time : %.24s\n", ctime(&atime
));
298 c
=map_num(pers
, __le32_to_cpu(sb
->level
));
299 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
300 printf(" Raid Devices : %d\n", __le32_to_cpu(sb
->raid_disks
));
302 printf(" Avail Dev Size : %llu%s\n",
303 (unsigned long long)__le64_to_cpu(sb
->data_size
),
304 human_size(__le64_to_cpu(sb
->data_size
)<<9));
305 if (__le32_to_cpu(sb
->level
) > 0) {
306 int ddsks
= 0, ddsks_denom
= 1;
307 switch(__le32_to_cpu(sb
->level
)) {
308 case 1: ddsks
=1;break;
310 case 5: ddsks
= __le32_to_cpu(sb
->raid_disks
)-1; break;
311 case 6: ddsks
= __le32_to_cpu(sb
->raid_disks
)-2; break;
313 layout
= __le32_to_cpu(sb
->layout
);
314 ddsks
= __le32_to_cpu(sb
->raid_disks
);
315 ddsks_denom
= (layout
&255) * ((layout
>>8)&255);
318 long long asize
= __le64_to_cpu(sb
->size
);
319 asize
= (asize
<< 9) * ddsks
/ ddsks_denom
;
320 printf(" Array Size : %llu%s\n",
321 asize
>> 10, human_size(asize
));
323 if (sb
->size
!= sb
->data_size
)
324 printf(" Used Dev Size : %llu%s\n",
325 (unsigned long long)__le64_to_cpu(sb
->size
),
326 human_size(__le64_to_cpu(sb
->size
)<<9));
329 printf(" Data Offset : %llu sectors\n",
330 (unsigned long long)__le64_to_cpu(sb
->data_offset
));
331 if (sb
->new_offset
&&
332 (__le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
)) {
333 unsigned long long offset
= __le64_to_cpu(sb
->data_offset
);
334 offset
+= (signed)(int32_t)__le32_to_cpu(sb
->new_offset
);
335 printf(" New Offset : %llu sectors\n", offset
);
337 printf(" Super Offset : %llu sectors\n",
338 (unsigned long long)__le64_to_cpu(sb
->super_offset
));
339 if (__le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
)
340 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->recovery_offset
));
342 st
->ss
->getinfo_super(st
, &info
, NULL
);
343 if (info
.space_after
!= 1 &&
344 !(__le32_to_cpu(sb
->feature_map
) & MD_FEATURE_NEW_OFFSET
))
345 printf(" Unused Space : before=%llu sectors, after=%llu sectors\n",
346 info
.space_before
, info
.space_after
);
348 printf(" State : %s\n", (__le64_to_cpu(sb
->resync_offset
)+1)? "active":"clean");
349 printf(" Device UUID : ");
350 for (i
=0; i
<16; i
++) {
351 if ((i
&3)==0 && i
!= 0) printf(":");
352 printf("%02x", sb
->device_uuid
[i
]);
356 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
357 printf("Internal Bitmap : %ld sectors from superblock\n",
358 (long)(int32_t)__le32_to_cpu(sb
->bitmap_offset
));
360 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
)) {
361 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb
->reshape_position
)/2,
362 human_size(__le64_to_cpu(sb
->reshape_position
)<<9));
363 if (__le32_to_cpu(sb
->delta_disks
)) {
364 printf(" Delta Devices : %d", __le32_to_cpu(sb
->delta_disks
));
365 printf(" (%d->%d)\n",
366 __le32_to_cpu(sb
->raid_disks
)-__le32_to_cpu(sb
->delta_disks
),
367 __le32_to_cpu(sb
->raid_disks
));
368 if ((int)__le32_to_cpu(sb
->delta_disks
) < 0)
369 delta_extra
= -__le32_to_cpu(sb
->delta_disks
);
371 if (__le32_to_cpu(sb
->new_level
) != __le32_to_cpu(sb
->level
)) {
372 c
= map_num(pers
, __le32_to_cpu(sb
->new_level
));
373 printf(" New Level : %s\n", c
?c
:"-unknown-");
375 if (__le32_to_cpu(sb
->new_layout
) != __le32_to_cpu(sb
->layout
)) {
376 if (__le32_to_cpu(sb
->level
) == 5) {
377 c
= map_num(r5layout
, __le32_to_cpu(sb
->new_layout
));
378 printf(" New Layout : %s\n", c
?c
:"-unknown-");
380 if (__le32_to_cpu(sb
->level
) == 6) {
381 c
= map_num(r6layout
, __le32_to_cpu(sb
->new_layout
));
382 printf(" New Layout : %s\n", c
?c
:"-unknown-");
384 if (__le32_to_cpu(sb
->level
) == 10) {
385 printf(" New Layout :");
386 print_r10_layout(__le32_to_cpu(sb
->new_layout
));
390 if (__le32_to_cpu(sb
->new_chunk
) != __le32_to_cpu(sb
->chunksize
))
391 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb
->new_chunk
)/2);
396 if (sb
->devflags
& WriteMostly1
)
397 printf(" write-mostly");
401 atime
= __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
;
402 printf(" Update Time : %.24s\n", ctime(&atime
));
404 if (sb
->bblog_size
&& sb
->bblog_offset
) {
405 printf(" Bad Block Log : %d entries available at offset %ld sectors",
406 __le16_to_cpu(sb
->bblog_size
)*512/8,
407 (long)(int32_t)__le32_to_cpu(sb
->bblog_offset
));
408 if (sb
->feature_map
&
409 __cpu_to_le32(MD_FEATURE_BAD_BLOCKS
))
410 printf(" - bad blocks present.");
414 if (calc_sb_1_csum(sb
) == sb
->sb_csum
)
415 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb
->sb_csum
));
417 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb
->sb_csum
),
418 __le32_to_cpu(calc_sb_1_csum(sb
)));
419 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb
->events
));
421 if (__le32_to_cpu(sb
->level
) == 5) {
422 c
= map_num(r5layout
, __le32_to_cpu(sb
->layout
));
423 printf(" Layout : %s\n", c
?c
:"-unknown-");
425 if (__le32_to_cpu(sb
->level
) == 6) {
426 c
= map_num(r6layout
, __le32_to_cpu(sb
->layout
));
427 printf(" Layout : %s\n", c
?c
:"-unknown-");
429 if (__le32_to_cpu(sb
->level
) == 10) {
430 int lo
= __le32_to_cpu(sb
->layout
);
432 print_r10_layout(lo
);
435 switch(__le32_to_cpu(sb
->level
)) {
441 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
444 printf(" Rounding : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
450 /* This turns out to just be confusing */
451 printf(" Array Slot : %d (", __le32_to_cpu(sb
->dev_number
));
452 for (i
= __le32_to_cpu(sb
->max_dev
); i
> 0 ; i
--)
453 if (__le16_to_cpu(sb
->dev_roles
[i
-1]) != 0xffff)
455 for (d
=0; d
< i
; d
++) {
456 int role
= __le16_to_cpu(sb
->dev_roles
[d
]);
458 if (role
== 0xffff) printf("empty");
459 else if(role
== 0xfffe) printf("failed");
460 else printf("%d", role
);
464 printf(" Device Role : ");
465 d
= __le32_to_cpu(sb
->dev_number
);
466 if (d
< __le32_to_cpu(sb
->max_dev
))
467 role
= __le16_to_cpu(sb
->dev_roles
[d
]);
472 else if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_REPLACEMENT
))
473 printf("Replacement device %d\n", role
);
475 printf("Active device %d\n", role
);
477 printf(" Array State : ");
478 for (d
=0; d
<__le32_to_cpu(sb
->raid_disks
) + delta_extra
; d
++) {
481 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
482 unsigned int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
496 /* This is confusing too */
498 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
499 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
503 if (faulty
) printf(" %d failed", faulty
);
505 printf(" ('A' == active, '.' == missing, 'R' == replacing)");
509 static void brief_examine_super1(struct supertype
*st
, int verbose
)
511 struct mdp_superblock_1
*sb
= st
->sb
;
513 unsigned long long sb_offset
;
515 char *c
=map_num(pers
, __le32_to_cpu(sb
->level
));
517 nm
= strchr(sb
->set_name
, ':');
520 else if (sb
->set_name
[0])
532 printf(" level=%s", c
);
533 sb_offset
= __le64_to_cpu(sb
->super_offset
);
535 printf(" metadata=1.1 ");
536 else if (sb_offset
<= 8)
537 printf(" metadata=1.2 ");
539 printf(" metadata=1.0 ");
541 printf("num-devices=%d ", __le32_to_cpu(sb
->raid_disks
));
543 for (i
=0; i
<16; i
++) {
544 if ((i
&3)==0 && i
!= 0) printf(":");
545 printf("%02x", sb
->set_uuid
[i
]);
547 if (sb
->set_name
[0]) {
549 print_quoted(sb
->set_name
);
554 static void export_examine_super1(struct supertype
*st
)
556 struct mdp_superblock_1
*sb
= st
->sb
;
561 printf("MD_LEVEL=%s\n", map_num(pers
, __le32_to_cpu(sb
->level
)));
562 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb
->raid_disks
));
564 if (sb
->set_name
[i
] == '\n' ||
565 sb
->set_name
[i
] == '\0') {
570 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
571 if (__le32_to_cpu(sb
->level
) > 0) {
572 int ddsks
= 0, ddsks_denom
= 1;
573 switch(__le32_to_cpu(sb
->level
)) {
574 case 1: ddsks
=1;break;
576 case 5: ddsks
= __le32_to_cpu(sb
->raid_disks
)-1; break;
577 case 6: ddsks
= __le32_to_cpu(sb
->raid_disks
)-2; break;
579 layout
= __le32_to_cpu(sb
->layout
);
580 ddsks
= __le32_to_cpu(sb
->raid_disks
);
581 ddsks_denom
= (layout
&255) * ((layout
>>8)&255);
584 long long asize
= __le64_to_cpu(sb
->size
);
585 asize
= (asize
<< 9) * ddsks
/ ddsks_denom
;
586 printf("MD_ARRAY_SIZE=%s\n",human_size_brief(asize
,JEDEC
));
590 for (i
=0; i
<16; i
++) {
591 if ((i
&3)==0 && i
!= 0) printf(":");
592 printf("%02x", sb
->set_uuid
[i
]);
595 printf("MD_UPDATE_TIME=%llu\n",
596 __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
);
597 printf("MD_DEV_UUID=");
598 for (i
=0; i
<16; i
++) {
599 if ((i
&3)==0 && i
!= 0) printf(":");
600 printf("%02x", sb
->device_uuid
[i
]);
603 printf("MD_EVENTS=%llu\n",
604 (unsigned long long)__le64_to_cpu(sb
->events
));
607 static int copy_metadata1(struct supertype
*st
, int from
, int to
)
609 /* Read superblock. If it looks good, write it out.
610 * Then if a bitmap is present, copy that.
611 * And if a bad-block-list is present, copy that too.
614 unsigned long long dsize
, sb_offset
;
615 const int bufsize
= 4*1024;
616 struct mdp_superblock_1 super
, *sb
;
618 if (posix_memalign(&buf
, 4096, bufsize
) != 0)
621 if (!get_dev_size(from
, NULL
, &dsize
))
627 switch(st
->minor_version
) {
631 sb_offset
&= ~(4*2-1);
643 if (lseek64(from
, sb_offset
<< 9, 0) < 0LL)
645 if (read(from
, buf
, bufsize
) != bufsize
)
649 super
= *sb
; // save most of sb for when we reuse buf
651 if (__le32_to_cpu(super
.magic
) != MD_SB_MAGIC
||
652 __le32_to_cpu(super
.major_version
) != 1 ||
653 __le64_to_cpu(super
.super_offset
) != sb_offset
||
654 calc_sb_1_csum(sb
) != super
.sb_csum
)
657 if (lseek64(to
, sb_offset
<< 9, 0) < 0LL)
659 if (write(to
, buf
, bufsize
) != bufsize
)
662 if (super
.feature_map
& __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET
)) {
663 unsigned long long bitmap_offset
= sb_offset
;
664 int bytes
= 4096; // just an estimate.
666 struct align_fd afrom
, ato
;
668 init_afd(&afrom
, from
);
671 bitmap_offset
+= (int32_t)__le32_to_cpu(super
.bitmap_offset
);
673 if (lseek64(from
, bitmap_offset
<<9, 0) < 0)
675 if (lseek64(to
, bitmap_offset
<<9, 0) < 0)
678 for (written
= 0; written
< bytes
; ) {
679 int n
= bytes
- written
;
682 if (aread(&afrom
, buf
, n
) != n
)
685 /* have the header, can calculate
686 * correct bitmap bytes */
690 bits
= __le64_to_cpu(bms
->sync_size
) / (__le32_to_cpu(bms
->chunksize
)>>9);
691 bytes
= (bits
+7) >> 3;
692 bytes
+= sizeof(bitmap_super_t
);
693 bytes
= ROUND_UP(bytes
, 512);
697 if (awrite(&ato
, buf
, n
) != n
)
703 if (super
.bblog_size
!= 0 &&
704 __le32_to_cpu(super
.bblog_size
) <= 100 &&
705 super
.bblog_offset
!= 0 &&
706 (super
.feature_map
& __le32_to_cpu(MD_FEATURE_BAD_BLOCKS
))) {
707 /* There is a bad block log */
708 unsigned long long bb_offset
= sb_offset
;
709 int bytes
= __le32_to_cpu(super
.bblog_size
) * 512;
711 struct align_fd afrom
, ato
;
713 init_afd(&afrom
, from
);
716 bb_offset
+= (int32_t)__le32_to_cpu(super
.bblog_offset
);
718 if (lseek64(from
, bb_offset
<<9, 0) < 0)
720 if (lseek64(to
, bb_offset
<<9, 0) < 0)
723 for (written
= 0; written
< bytes
; ) {
724 int n
= bytes
- written
;
727 if (aread(&afrom
, buf
, n
) != n
)
730 if (awrite(&ato
, buf
, n
) != n
)
744 static void detail_super1(struct supertype
*st
, char *homehost
)
746 struct mdp_superblock_1
*sb
= st
->sb
;
748 int l
= homehost
? strlen(homehost
) : 0;
750 printf(" Name : %.32s", sb
->set_name
);
751 if (l
> 0 && l
< 32 &&
752 sb
->set_name
[l
] == ':' &&
753 strncmp(sb
->set_name
, homehost
, l
) == 0)
754 printf(" (local to host %s)", homehost
);
755 printf("\n UUID : ");
756 for (i
=0; i
<16; i
++) {
757 if ((i
&3)==0 && i
!= 0) printf(":");
758 printf("%02x", sb
->set_uuid
[i
]);
760 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb
->events
));
763 static void brief_detail_super1(struct supertype
*st
)
765 struct mdp_superblock_1
*sb
= st
->sb
;
768 if (sb
->set_name
[0]) {
770 print_quoted(sb
->set_name
);
773 for (i
=0; i
<16; i
++) {
774 if ((i
&3)==0 && i
!= 0) printf(":");
775 printf("%02x", sb
->set_uuid
[i
]);
779 static void export_detail_super1(struct supertype
*st
)
781 struct mdp_superblock_1
*sb
= st
->sb
;
786 if (sb
->set_name
[i
] == '\n' ||
787 sb
->set_name
[i
] == '\0') {
792 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
795 static int examine_badblocks_super1(struct supertype
*st
, int fd
, char *devname
)
797 struct mdp_superblock_1
*sb
= st
->sb
;
798 unsigned long long offset
;
803 if (!sb
->bblog_size
|| __le32_to_cpu(sb
->bblog_size
) > 100
804 || !sb
->bblog_offset
){
805 printf("No bad-blocks list configured on %s\n", devname
);
808 if ((sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BAD_BLOCKS
))
810 printf("Bad-blocks list is empty in %s\n", devname
);
814 size
= __le32_to_cpu(sb
->bblog_size
)* 512;
815 if (posix_memalign((void**)&bbl
, 4096, size
) != 0) {
816 pr_err("%s could not allocate badblocks list\n", __func__
);
819 offset
= __le64_to_cpu(sb
->super_offset
) +
820 (int)__le32_to_cpu(sb
->bblog_offset
);
822 if (lseek64(fd
, offset
, 0) < 0) {
823 pr_err("Cannot seek to bad-blocks list\n");
826 if (read(fd
, bbl
, size
) != size
) {
827 pr_err("Cannot read bad-blocks list\n");
830 /* 64bits per entry. 10 bits is block-count, 54 bits is block
831 * offset. Blocks are sectors unless bblog->shift makes them bigger
834 printf("Bad-blocks on %s:\n", devname
);
835 for (i
= 0; i
< size
/8; i
++, bbp
++) {
836 __u64 bb
= __le64_to_cpu(*bbp
);
837 int count
= bb
& 0x3ff;
838 unsigned long long sector
= bb
>> 10;
843 sector
<<= sb
->bblog_shift
;
844 count
<<= sb
->bblog_shift
;
846 printf("%20llu for %d sectors\n", sector
, count
);
853 static int match_home1(struct supertype
*st
, char *homehost
)
855 struct mdp_superblock_1
*sb
= st
->sb
;
856 int l
= homehost
? strlen(homehost
) : 0;
858 return (l
> 0 && l
< 32 &&
859 sb
->set_name
[l
] == ':' &&
860 strncmp(sb
->set_name
, homehost
, l
) == 0);
863 static void uuid_from_super1(struct supertype
*st
, int uuid
[4])
865 struct mdp_superblock_1
*super
= st
->sb
;
866 char *cuuid
= (char*)uuid
;
869 cuuid
[i
] = super
->set_uuid
[i
];
872 static void getinfo_super1(struct supertype
*st
, struct mdinfo
*info
, char *map
)
874 struct mdp_superblock_1
*sb
= st
->sb
;
875 struct bitmap_super_s
*bsb
= (void*)(((char*)sb
)+MAX_SB_SIZE
);
876 struct misc_dev_info
*misc
= (void*)(((char*)sb
)+MAX_SB_SIZE
+BM_SUPER_SIZE
);
880 unsigned int map_disks
= info
->array
.raid_disks
;
881 unsigned long long super_offset
;
882 unsigned long long data_size
;
884 memset(info
, 0, sizeof(*info
));
885 info
->array
.major_version
= 1;
886 info
->array
.minor_version
= st
->minor_version
;
887 info
->array
.patch_version
= 0;
888 info
->array
.raid_disks
= __le32_to_cpu(sb
->raid_disks
);
889 info
->array
.level
= __le32_to_cpu(sb
->level
);
890 info
->array
.layout
= __le32_to_cpu(sb
->layout
);
891 info
->array
.md_minor
= -1;
892 info
->array
.ctime
= __le64_to_cpu(sb
->ctime
);
893 info
->array
.utime
= __le64_to_cpu(sb
->utime
);
894 info
->array
.chunk_size
= __le32_to_cpu(sb
->chunksize
)*512;
896 (__le64_to_cpu(sb
->resync_offset
) == MaxSector
)
899 info
->data_offset
= __le64_to_cpu(sb
->data_offset
);
900 info
->component_size
= __le64_to_cpu(sb
->size
);
901 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET
))
902 info
->bitmap_offset
= (int32_t)__le32_to_cpu(sb
->bitmap_offset
);
904 info
->disk
.major
= 0;
905 info
->disk
.minor
= 0;
906 info
->disk
.number
= __le32_to_cpu(sb
->dev_number
);
907 if (__le32_to_cpu(sb
->dev_number
) >= __le32_to_cpu(sb
->max_dev
) ||
908 __le32_to_cpu(sb
->dev_number
) >= MAX_DEVS
)
911 role
= __le16_to_cpu(sb
->dev_roles
[__le32_to_cpu(sb
->dev_number
)]);
913 super_offset
= __le64_to_cpu(sb
->super_offset
);
914 data_size
= __le64_to_cpu(sb
->size
);
915 if (info
->data_offset
< super_offset
) {
916 unsigned long long end
;
917 info
->space_before
= info
->data_offset
;
920 if (sb
->bblog_offset
&& sb
->bblog_size
) {
921 unsigned long long bboffset
= super_offset
;
922 bboffset
+= (int32_t)__le32_to_cpu(sb
->bblog_offset
);
927 if (super_offset
+ info
->bitmap_offset
< end
)
928 end
= super_offset
+ info
->bitmap_offset
;
930 if (info
->data_offset
+ data_size
< end
)
931 info
->space_after
= end
- data_size
- info
->data_offset
;
933 info
->space_after
= 0;
935 unsigned long long earliest
;
936 earliest
= super_offset
+ (32+4)*2; /* match kernel */
937 if (info
->bitmap_offset
> 0) {
938 unsigned long long bmend
= info
->bitmap_offset
;
939 unsigned long long size
= __le64_to_cpu(bsb
->sync_size
);
940 size
/= __le32_to_cpu(bsb
->chunksize
) >> 9;
941 size
= (size
+ 7) >> 3;
942 size
+= sizeof(bitmap_super_t
);
943 size
= ROUND_UP(size
, 4096);
946 if (bmend
> earliest
)
949 if (sb
->bblog_offset
&& sb
->bblog_size
) {
950 unsigned long long bbend
= super_offset
;
951 bbend
+= (int32_t)__le32_to_cpu(sb
->bblog_offset
);
952 bbend
+= __le32_to_cpu(sb
->bblog_size
);
953 if (bbend
> earliest
)
956 if (earliest
< info
->data_offset
)
957 info
->space_before
= info
->data_offset
- earliest
;
959 info
->space_before
= 0;
960 info
->space_after
= misc
->device_size
- data_size
- info
->data_offset
;
962 if (info
->space_before
== 0 && info
->space_after
== 0) {
963 /* It will look like we don't support data_offset changes,
964 * be we do - it's just that there is no room.
965 * A change that reduced the number of devices should
966 * still be allowed, so set the otherwise useless value of '1'
968 info
->space_after
= 1;
971 info
->disk
.raid_disk
= -1;
974 info
->disk
.state
= 0; /* spare: not active, not sync, not faulty */
977 info
->disk
.state
= 1; /* faulty */
980 info
->disk
.state
= 6; /* active and in sync */
981 info
->disk
.raid_disk
= role
;
983 if (sb
->devflags
& WriteMostly1
)
984 info
->disk
.state
|= (1 << MD_DISK_WRITEMOSTLY
);
985 info
->events
= __le64_to_cpu(sb
->events
);
986 sprintf(info
->text_version
, "1.%d", st
->minor_version
);
987 info
->safe_mode_delay
= 200;
989 memcpy(info
->uuid
, sb
->set_uuid
, 16);
991 strncpy(info
->name
, sb
->set_name
, 32);
994 if ((__le32_to_cpu(sb
->feature_map
)&MD_FEATURE_REPLACEMENT
)) {
995 info
->disk
.state
&= ~(1 << MD_DISK_SYNC
);
996 info
->disk
.state
|= 1 << MD_DISK_REPLACEMENT
;
999 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET
))
1000 info
->recovery_start
= __le32_to_cpu(sb
->recovery_offset
);
1002 info
->recovery_start
= MaxSector
;
1004 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
1005 info
->reshape_active
= 1;
1006 if ((sb
->feature_map
& __le32_to_cpu(MD_FEATURE_NEW_OFFSET
)) &&
1007 sb
->new_offset
!= 0)
1008 info
->reshape_active
|= RESHAPE_NO_BACKUP
;
1009 info
->reshape_progress
= __le64_to_cpu(sb
->reshape_position
);
1010 info
->new_level
= __le32_to_cpu(sb
->new_level
);
1011 info
->delta_disks
= __le32_to_cpu(sb
->delta_disks
);
1012 info
->new_layout
= __le32_to_cpu(sb
->new_layout
);
1013 info
->new_chunk
= __le32_to_cpu(sb
->new_chunk
)<<9;
1014 if (info
->delta_disks
< 0)
1015 info
->array
.raid_disks
-= info
->delta_disks
;
1017 info
->reshape_active
= 0;
1019 info
->recovery_blocked
= info
->reshape_active
;
1022 for (i
=0; i
<map_disks
; i
++)
1024 for (i
= 0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
1025 role
= __le16_to_cpu(sb
->dev_roles
[i
]);
1026 if (/*role == 0xFFFF || */role
< (unsigned) info
->array
.raid_disks
) {
1028 if (map
&& role
< map_disks
)
1033 info
->array
.working_disks
= working
;
1036 static struct mdinfo
*container_content1(struct supertype
*st
, char *subarray
)
1038 struct mdinfo
*info
;
1043 info
= xmalloc(sizeof(*info
));
1044 getinfo_super1(st
, info
, NULL
);
1048 static int update_super1(struct supertype
*st
, struct mdinfo
*info
,
1050 char *devname
, int verbose
,
1051 int uuid_set
, char *homehost
)
1053 /* NOTE: for 'assemble' and 'force' we need to return non-zero
1054 * if any change was made. For others, the return value is
1058 struct mdp_superblock_1
*sb
= st
->sb
;
1060 if (strcmp(update
, "homehost") == 0 &&
1062 /* Note that 'homehost' is special as it is really
1067 c
= strchr(sb
->set_name
, ':');
1069 strncpy(info
->name
, c
+1, 31 - (c
-sb
->set_name
));
1071 strncpy(info
->name
, sb
->set_name
, 32);
1075 if (strcmp(update
, "force-one")==0) {
1076 /* Not enough devices for a working array,
1077 * so bring this one up-to-date
1079 if (sb
->events
!= __cpu_to_le64(info
->events
))
1081 sb
->events
= __cpu_to_le64(info
->events
);
1082 } else if (strcmp(update
, "force-array")==0) {
1083 /* Degraded array and 'force' requests to
1084 * maybe need to mark it 'clean'.
1086 switch(__le32_to_cpu(sb
->level
)) {
1087 case 5: case 4: case 6:
1088 /* need to force clean */
1089 if (sb
->resync_offset
!= MaxSector
)
1091 sb
->resync_offset
= MaxSector
;
1093 } else if (strcmp(update
, "assemble")==0) {
1094 int d
= info
->disk
.number
;
1096 if (info
->disk
.state
& (1<<MD_DISK_ACTIVE
))
1097 want
= info
->disk
.raid_disk
;
1100 if (sb
->dev_roles
[d
] != __cpu_to_le16(want
)) {
1101 sb
->dev_roles
[d
] = __cpu_to_le16(want
);
1104 if (info
->reshape_active
&&
1105 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
1106 info
->delta_disks
>= 0 &&
1107 info
->reshape_progress
< __le64_to_cpu(sb
->reshape_position
)) {
1108 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
1111 if (info
->reshape_active
&&
1112 sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
) &&
1113 info
->delta_disks
< 0 &&
1114 info
->reshape_progress
> __le64_to_cpu(sb
->reshape_position
)) {
1115 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
1118 } else if (strcmp(update
, "linear-grow-new") == 0) {
1121 unsigned int max
= __le32_to_cpu(sb
->max_dev
);
1123 for (i
=0 ; i
< max
; i
++)
1124 if (__le16_to_cpu(sb
->dev_roles
[i
]) >= 0xfffe)
1126 sb
->dev_number
= __cpu_to_le32(i
);
1127 info
->disk
.number
= i
;
1128 if (max
>= __le32_to_cpu(sb
->max_dev
))
1129 sb
->max_dev
= __cpu_to_le32(max
+1);
1131 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
1132 read(rfd
, sb
->device_uuid
, 16) != 16) {
1133 __u32 r
[4] = {random(), random(), random(), random()};
1134 memcpy(sb
->device_uuid
, r
, 16);
1140 __cpu_to_le16(info
->disk
.raid_disk
);
1142 fd
= open(devname
, O_RDONLY
);
1144 unsigned long long ds
;
1145 get_dev_size(fd
, devname
, &ds
);
1148 if (__le64_to_cpu(sb
->super_offset
) <
1149 __le64_to_cpu(sb
->data_offset
)) {
1150 sb
->data_size
= __cpu_to_le64(
1151 ds
- __le64_to_cpu(sb
->data_offset
));
1154 ds
&= ~(unsigned long long)(4*2-1);
1155 sb
->super_offset
= __cpu_to_le64(ds
);
1156 sb
->data_size
= __cpu_to_le64(
1157 ds
- __le64_to_cpu(sb
->data_offset
));
1160 } else if (strcmp(update
, "linear-grow-update") == 0) {
1161 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
1162 sb
->dev_roles
[info
->disk
.number
] =
1163 __cpu_to_le16(info
->disk
.raid_disk
);
1164 } else if (strcmp(update
, "resync") == 0) {
1165 /* make sure resync happens */
1166 sb
->resync_offset
= 0ULL;
1167 } else if (strcmp(update
, "uuid") == 0) {
1168 copy_uuid(sb
->set_uuid
, info
->uuid
, super1
.swapuuid
);
1170 if (__le32_to_cpu(sb
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
1171 struct bitmap_super_s
*bm
;
1172 bm
= (struct bitmap_super_s
*)(st
->sb
+MAX_SB_SIZE
);
1173 memcpy(bm
->uuid
, sb
->set_uuid
, 16);
1175 } else if (strcmp(update
, "no-bitmap") == 0) {
1176 sb
->feature_map
&= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
1177 } else if (strcmp(update
, "bbl") == 0) {
1178 /* only possible if there is room after the bitmap, or if
1179 * there is no bitmap
1181 unsigned long long sb_offset
= __le64_to_cpu(sb
->super_offset
);
1182 unsigned long long data_offset
= __le64_to_cpu(sb
->data_offset
);
1183 long bitmap_offset
= (long)(int32_t)__le32_to_cpu(sb
->bitmap_offset
);
1184 long bm_sectors
= 0;
1188 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
1189 struct bitmap_super_s
*bsb
;
1190 bsb
= (struct bitmap_super_s
*)(((char*)sb
)+MAX_SB_SIZE
);
1191 bm_sectors
= bitmap_sectors(bsb
);
1194 if (sb_offset
< data_offset
) {
1195 /* 1.1 or 1.2. Put bbl after bitmap leaving at least 32K
1198 bb_offset
= sb_offset
+ 8;
1199 if (bm_sectors
&& bitmap_offset
> 0)
1200 bb_offset
= bitmap_offset
+ bm_sectors
;
1201 while (bb_offset
< (long)sb_offset
+ 8 + 32*2
1202 && bb_offset
+ 8+8 <= (long)data_offset
)
1203 /* too close to bitmap, and room to grow */
1205 if (bb_offset
+ 8 <= (long)data_offset
) {
1206 sb
->bblog_size
= __cpu_to_le16(8);
1207 sb
->bblog_offset
= __cpu_to_le32(bb_offset
);
1210 /* 1.0 - Put bbl just before super block */
1211 if (bm_sectors
&& bitmap_offset
< 0)
1212 space
= -bitmap_offset
- bm_sectors
;
1214 space
= sb_offset
- data_offset
-
1215 __le64_to_cpu(sb
->data_size
);
1217 sb
->bblog_size
= __cpu_to_le16(8);
1218 sb
->bblog_offset
= __cpu_to_le32((unsigned)-8);
1221 } else if (strcmp(update
, "no-bbl") == 0) {
1222 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BAD_BLOCKS
))
1223 pr_err("Cannot remove active bbl from %s\n",devname
);
1226 sb
->bblog_shift
= 0;
1227 sb
->bblog_offset
= 0;
1229 } else if (strcmp(update
, "name") == 0) {
1230 if (info
->name
[0] == 0)
1231 sprintf(info
->name
, "%d", info
->array
.md_minor
);
1232 memset(sb
->set_name
, 0, sizeof(sb
->set_name
));
1234 strchr(info
->name
, ':') == NULL
&&
1235 strlen(homehost
)+1+strlen(info
->name
) < 32) {
1236 strcpy(sb
->set_name
, homehost
);
1237 strcat(sb
->set_name
, ":");
1238 strcat(sb
->set_name
, info
->name
);
1240 strcpy(sb
->set_name
, info
->name
);
1241 } else if (strcmp(update
, "devicesize") == 0 &&
1242 __le64_to_cpu(sb
->super_offset
) <
1243 __le64_to_cpu(sb
->data_offset
)) {
1244 /* set data_size to device size less data_offset */
1245 struct misc_dev_info
*misc
= (struct misc_dev_info
*)
1246 (st
->sb
+ MAX_SB_SIZE
+ BM_SUPER_SIZE
);
1247 printf("Size was %llu\n", (unsigned long long)
1248 __le64_to_cpu(sb
->data_size
));
1249 sb
->data_size
= __cpu_to_le64(
1250 misc
->device_size
- __le64_to_cpu(sb
->data_offset
));
1251 printf("Size is %llu\n", (unsigned long long)
1252 __le64_to_cpu(sb
->data_size
));
1253 } else if (strcmp(update
, "revert-reshape") == 0) {
1255 if (!(sb
->feature_map
& __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE
)))
1256 pr_err("No active reshape to revert on %s\n",
1261 sb
->raid_disks
= __cpu_to_le32(__le32_to_cpu(sb
->raid_disks
) +
1262 __le32_to_cpu(sb
->delta_disks
));
1263 if (sb
->delta_disks
== 0)
1264 sb
->feature_map
^= __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS
);
1266 sb
->delta_disks
= __cpu_to_le32(-__le32_to_cpu(sb
->delta_disks
));
1268 temp
= sb
->new_layout
;
1269 sb
->new_layout
= sb
->layout
;
1272 temp
= sb
->new_chunk
;
1273 sb
->new_chunk
= sb
->chunksize
;
1274 sb
->chunksize
= temp
;
1276 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_NEW_OFFSET
)) {
1277 sb
->data_offset
= __cpu_to_le64(__le64_to_cpu(sb
->data_offset
) +
1278 (long)(int32_t)__le32_to_cpu(sb
->new_offset
));
1279 sb
->new_offset
= __cpu_to_le32(-(int32_t)__le32_to_cpu(sb
->new_offset
));
1282 } else if (strcmp(update
, "_reshape_progress")==0)
1283 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
1284 else if (strcmp(update
, "writemostly")==0)
1285 sb
->devflags
|= WriteMostly1
;
1286 else if (strcmp(update
, "readwrite")==0)
1287 sb
->devflags
&= ~WriteMostly1
;
1291 sb
->sb_csum
= calc_sb_1_csum(sb
);
1295 static int init_super1(struct supertype
*st
, mdu_array_info_t
*info
,
1296 unsigned long long size
, char *name
, char *homehost
,
1297 int *uuid
, unsigned long long data_offset
)
1299 struct mdp_superblock_1
*sb
;
1305 if (posix_memalign((void**)&sb
, 4096, SUPER1_SIZE
) != 0) {
1306 pr_err("%s could not allocate superblock\n", __func__
);
1309 memset(sb
, 0, SUPER1_SIZE
);
1313 /* zeroing superblock */
1317 spares
= info
->working_disks
- info
->active_disks
;
1318 if (info
->raid_disks
+ spares
> MAX_DEVS
) {
1319 pr_err("too many devices requested: %d+%d > %d\n",
1320 info
->raid_disks
, spares
, MAX_DEVS
);
1324 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
1325 sb
->major_version
= __cpu_to_le32(1);
1326 sb
->feature_map
= 0;
1330 copy_uuid(sb
->set_uuid
, uuid
, super1
.swapuuid
);
1332 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
1333 read(rfd
, sb
->set_uuid
, 16) != 16) {
1334 __u32 r
[4] = {random(), random(), random(), random()};
1335 memcpy(sb
->set_uuid
, r
, 16);
1337 if (rfd
>= 0) close(rfd
);
1340 if (name
== NULL
|| *name
== 0) {
1341 sprintf(defname
, "%d", info
->md_minor
);
1345 strchr(name
, ':')== NULL
&&
1346 strlen(homehost
)+1+strlen(name
) < 32) {
1347 strcpy(sb
->set_name
, homehost
);
1348 strcat(sb
->set_name
, ":");
1349 strcat(sb
->set_name
, name
);
1351 strcpy(sb
->set_name
, name
);
1353 sb
->ctime
= __cpu_to_le64((unsigned long long)time(0));
1354 sb
->level
= __cpu_to_le32(info
->level
);
1355 sb
->layout
= __cpu_to_le32(info
->layout
);
1356 sb
->size
= __cpu_to_le64(size
*2ULL);
1357 sb
->chunksize
= __cpu_to_le32(info
->chunk_size
>>9);
1358 sb
->raid_disks
= __cpu_to_le32(info
->raid_disks
);
1360 sb
->data_offset
= __cpu_to_le64(data_offset
);
1361 sb
->data_size
= __cpu_to_le64(0);
1362 sb
->super_offset
= __cpu_to_le64(0);
1363 sb
->recovery_offset
= __cpu_to_le64(0);
1365 sb
->utime
= sb
->ctime
;
1366 sb
->events
= __cpu_to_le64(1);
1367 if (info
->state
& (1<<MD_SB_CLEAN
))
1368 sb
->resync_offset
= MaxSector
;
1370 sb
->resync_offset
= 0;
1371 sbsize
= sizeof(struct mdp_superblock_1
) + 2 * (info
->raid_disks
+ spares
);
1372 sbsize
= ROUND_UP(sbsize
, 512);
1373 sb
->max_dev
= __cpu_to_le32((sbsize
- sizeof(struct mdp_superblock_1
)) / 2);
1375 memset(sb
->dev_roles
, 0xff, MAX_SB_SIZE
- sizeof(struct mdp_superblock_1
));
1383 long long data_offset
;
1384 mdu_disk_info_t disk
;
1385 struct devinfo
*next
;
1388 /* Add a device to the superblock being created */
1389 static int add_to_super1(struct supertype
*st
, mdu_disk_info_t
*dk
,
1390 int fd
, char *devname
, unsigned long long data_offset
)
1392 struct mdp_superblock_1
*sb
= st
->sb
;
1393 __u16
*rp
= sb
->dev_roles
+ dk
->number
;
1394 struct devinfo
*di
, **dip
;
1396 if ((dk
->state
& 6) == 6) /* active, sync */
1397 *rp
= __cpu_to_le16(dk
->raid_disk
);
1398 else if ((dk
->state
& ~2) == 0) /* active or idle -> spare */
1403 if (dk
->number
>= (int)__le32_to_cpu(sb
->max_dev
) &&
1404 __le32_to_cpu(sb
->max_dev
) < MAX_DEVS
)
1405 sb
->max_dev
= __cpu_to_le32(dk
->number
+1);
1407 sb
->dev_number
= __cpu_to_le32(dk
->number
);
1408 sb
->devflags
= 0; /* don't copy another disks flags */
1409 sb
->sb_csum
= calc_sb_1_csum(sb
);
1411 dip
= (struct devinfo
**)&st
->info
;
1413 dip
= &(*dip
)->next
;
1414 di
= xmalloc(sizeof(struct devinfo
));
1416 di
->devname
= devname
;
1418 di
->data_offset
= data_offset
;
1426 static void locate_bitmap1(struct supertype
*st
, int fd
);
1428 static int store_super1(struct supertype
*st
, int fd
)
1430 struct mdp_superblock_1
*sb
= st
->sb
;
1431 unsigned long long sb_offset
;
1432 struct align_fd afd
;
1434 unsigned long long dsize
;
1436 if (!get_dev_size(fd
, NULL
, &dsize
))
1447 * Calculate the position of the superblock.
1448 * It is always aligned to a 4K boundary and
1449 * depending on minor_version, it can be:
1450 * 0: At least 8K, but less than 12K, from end of device
1451 * 1: At start of device
1452 * 2: 4K from start of device.
1454 switch(st
->minor_version
) {
1458 sb_offset
&= ~(4*2-1);
1470 if (sb_offset
!= __le64_to_cpu(sb
->super_offset
) &&
1471 0 != __le64_to_cpu(sb
->super_offset
)
1473 pr_err("internal error - sb_offset is wrong\n");
1477 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL)
1480 sbsize
= ROUND_UP(sizeof(*sb
) + 2 * __le32_to_cpu(sb
->max_dev
), 512);
1482 if (awrite(&afd
, sb
, sbsize
) != sbsize
)
1485 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
1486 struct bitmap_super_s
*bm
= (struct bitmap_super_s
*)
1487 (((char*)sb
)+MAX_SB_SIZE
);
1488 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
) {
1489 locate_bitmap1(st
, fd
);
1490 if (awrite(&afd
, bm
, sizeof(*bm
)) != sizeof(*bm
))
1498 static int load_super1(struct supertype
*st
, int fd
, char *devname
);
1500 static unsigned long choose_bm_space(unsigned long devsize
)
1502 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1503 * if bigger than 200Gig, save 128k
1504 * NOTE: result must be multiple of 4K else bad things happen
1505 * on 4K-sector devices.
1507 if (devsize
< 64*2) return 0;
1508 if (devsize
- 64*2 >= 200*1024*1024*2)
1510 if (devsize
- 4*2 > 8*1024*1024*2)
1515 static void free_super1(struct supertype
*st
);
1518 static int write_init_super1(struct supertype
*st
)
1520 struct mdp_superblock_1
*sb
= st
->sb
;
1521 struct supertype
*refst
;
1524 unsigned long long bm_space
;
1525 unsigned long long reserved
;
1527 unsigned long long dsize
, array_size
;
1528 unsigned long long sb_offset
, headroom
;
1529 unsigned long long data_offset
;
1531 for (di
= st
->info
; di
; di
= di
->next
) {
1532 if (di
->disk
.state
== 1)
1537 while (Kill(di
->devname
, NULL
, 0, -1, 1) == 0)
1540 sb
->dev_number
= __cpu_to_le32(di
->disk
.number
);
1541 if (di
->disk
.state
& (1<<MD_DISK_WRITEMOSTLY
))
1542 sb
->devflags
|= WriteMostly1
;
1544 sb
->devflags
&= ~WriteMostly1
;
1546 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
1547 read(rfd
, sb
->device_uuid
, 16) != 16) {
1548 __u32 r
[4] = {random(), random(), random(), random()};
1549 memcpy(sb
->device_uuid
, r
, 16);
1556 refst
= dup_super(st
);
1557 if (load_super1(refst
, di
->fd
, NULL
)==0) {
1558 struct mdp_superblock_1
*refsb
= refst
->sb
;
1560 memcpy(sb
->device_uuid
, refsb
->device_uuid
, 16);
1561 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16)==0) {
1562 /* same array, so preserve events and
1564 sb
->events
= refsb
->events
;
1565 /* bugs in 2.6.17 and earlier mean the
1566 * dev_number chosen in Manage must be preserved
1568 if (get_linux_version() >= 2006018)
1569 sb
->dev_number
= refsb
->dev_number
;
1575 if (!get_dev_size(di
->fd
, NULL
, &dsize
)) {
1588 * Calculate the position of the superblock.
1589 * It is always aligned to a 4K boundary and
1590 * depending on minor_version, it can be:
1591 * 0: At least 8K, but less than 12K, from end of device
1592 * 1: At start of device
1593 * 2: 4K from start of device.
1594 * Depending on the array size, we might leave extra space
1596 * Also leave 4K for bad-block log.
1598 array_size
= __le64_to_cpu(sb
->size
);
1599 /* work out how much space we left for a bitmap,
1600 * Add 8 sectors for bad block log */
1601 bm_space
= choose_bm_space(array_size
) + 8;
1603 /* We try to leave 0.1% at the start for reshape
1604 * operations, but limit this to 128Meg (0.1% of 10Gig)
1605 * which is plenty for efficient reshapes
1606 * However we make it at least 2 chunks as one chunk
1607 * is minimum needed for reshape.
1609 headroom
= 128 * 1024 * 2;
1610 while (headroom
<< 10 > array_size
&&
1611 headroom
/2 >= __le32_to_cpu(sb
->chunksize
) * 2)
1614 data_offset
= di
->data_offset
;
1615 switch(st
->minor_version
) {
1619 sb_offset
&= ~(4*2-1);
1620 sb
->super_offset
= __cpu_to_le64(sb_offset
);
1621 if (data_offset
== INVALID_SECTORS
)
1622 sb
->data_offset
= 0;
1623 if (sb_offset
< array_size
+ bm_space
)
1624 bm_space
= sb_offset
- array_size
;
1625 sb
->data_size
= __cpu_to_le64(sb_offset
- bm_space
);
1626 if (bm_space
>= 8) {
1627 sb
->bblog_size
= __cpu_to_le16(8);
1628 sb
->bblog_offset
= __cpu_to_le32((unsigned)-8);
1632 sb
->super_offset
= __cpu_to_le64(0);
1633 if (data_offset
== INVALID_SECTORS
) {
1634 reserved
= bm_space
+ 4*2;
1635 if (reserved
< headroom
)
1636 reserved
= headroom
;
1637 if (reserved
+ array_size
> dsize
)
1638 reserved
= dsize
- array_size
;
1639 /* Try for multiple of 1Meg so it is nicely aligned */
1640 #define ONE_MEG (2*1024)
1641 if (reserved
> ONE_MEG
)
1642 reserved
= (reserved
/ONE_MEG
) * ONE_MEG
;
1644 /* force 4K alignment */
1648 reserved
= data_offset
;
1650 sb
->data_offset
= __cpu_to_le64(reserved
);
1651 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1652 if (reserved
>= 8 + 32*2 + 8) {
1653 sb
->bblog_size
= __cpu_to_le16(8);
1654 sb
->bblog_offset
= __cpu_to_le32(8 + 32*2);
1655 } else if (reserved
>= 16) {
1656 sb
->bblog_size
= __cpu_to_le16(8);
1657 sb
->bblog_offset
= __cpu_to_le32(reserved
-8);
1662 sb
->super_offset
= __cpu_to_le64(4*2);
1663 if (data_offset
== INVALID_SECTORS
) {
1664 if (4*2 + 4*2 + bm_space
+ array_size
1666 bm_space
= dsize
- array_size
1669 reserved
= bm_space
+ 4*2 + 4*2;
1670 if (reserved
< headroom
)
1671 reserved
= headroom
;
1672 if (reserved
+ array_size
> dsize
)
1673 reserved
= dsize
- array_size
;
1674 /* Try for multiple of 1Meg so it is nicely aligned */
1675 #define ONE_MEG (2*1024)
1676 if (reserved
> ONE_MEG
)
1677 reserved
= (reserved
/ONE_MEG
) * ONE_MEG
;
1679 /* force 4K alignment */
1683 reserved
= data_offset
;
1685 sb
->data_offset
= __cpu_to_le64(reserved
);
1686 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1687 if (reserved
>= 16 + 32*2 + 8) {
1688 sb
->bblog_size
= __cpu_to_le16(8);
1689 sb
->bblog_offset
= __cpu_to_le32(8 + 32*2);
1690 } else if (reserved
>= 16+16) {
1691 sb
->bblog_size
= __cpu_to_le16(8);
1692 /* '8' sectors for the bblog, and another '8'
1693 * because we want offset from superblock, not
1696 sb
->bblog_offset
= __cpu_to_le32(reserved
-8-8);
1700 pr_err("Failed to write invalid "
1701 "metadata format 1.%i to %s\n",
1702 st
->minor_version
, di
->devname
);
1707 sb
->sb_csum
= calc_sb_1_csum(sb
);
1708 rv
= store_super1(st
, di
->fd
);
1709 if (rv
== 0 && (__le32_to_cpu(sb
->feature_map
) & 1))
1710 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
1718 pr_err("Failed to write metadata to %s\n",
1725 static int compare_super1(struct supertype
*st
, struct supertype
*tst
)
1729 * 0 same, or first was empty, and second was copied
1730 * 1 second had wrong number
1732 * 3 wrong other info
1734 struct mdp_superblock_1
*first
= st
->sb
;
1735 struct mdp_superblock_1
*second
= tst
->sb
;
1737 if (second
->magic
!= __cpu_to_le32(MD_SB_MAGIC
))
1739 if (second
->major_version
!= __cpu_to_le32(1))
1743 if (posix_memalign((void**)&first
, 4096, SUPER1_SIZE
) != 0) {
1744 pr_err("%s could not allocate superblock\n", __func__
);
1747 memcpy(first
, second
, SUPER1_SIZE
);
1751 if (memcmp(first
->set_uuid
, second
->set_uuid
, 16)!= 0)
1754 if (first
->ctime
!= second
->ctime
||
1755 first
->level
!= second
->level
||
1756 first
->layout
!= second
->layout
||
1757 first
->size
!= second
->size
||
1758 first
->chunksize
!= second
->chunksize
||
1759 first
->raid_disks
!= second
->raid_disks
)
1764 static int load_super1(struct supertype
*st
, int fd
, char *devname
)
1766 unsigned long long dsize
;
1767 unsigned long long sb_offset
;
1768 struct mdp_superblock_1
*super
;
1770 struct bitmap_super_s
*bsb
;
1771 struct misc_dev_info
*misc
;
1772 struct align_fd afd
;
1778 if (st
->ss
== NULL
|| st
->minor_version
== -1) {
1780 struct supertype tst
;
1781 __u64 bestctime
= 0;
1782 /* guess... choose latest ctime */
1783 memset(&tst
, 0, sizeof(tst
));
1785 for (tst
.minor_version
= 0; tst
.minor_version
<= 2 ; tst
.minor_version
++) {
1786 switch(load_super1(&tst
, fd
, devname
)) {
1787 case 0: super
= tst
.sb
;
1788 if (bestvers
== -1 ||
1789 bestctime
< __le64_to_cpu(super
->ctime
)) {
1790 bestvers
= tst
.minor_version
;
1791 bestctime
= __le64_to_cpu(super
->ctime
);
1796 case 1: return 1; /*bad device */
1797 case 2: break; /* bad, try next */
1800 if (bestvers
!= -1) {
1802 tst
.minor_version
= bestvers
;
1804 tst
.max_devs
= MAX_DEVS
;
1805 rv
= load_super1(&tst
, fd
, devname
);
1812 if (!get_dev_size(fd
, devname
, &dsize
))
1818 pr_err("%s is too small for md: size is %llu sectors.\n",
1824 * Calculate the position of the superblock.
1825 * It is always aligned to a 4K boundary and
1826 * depending on minor_version, it can be:
1827 * 0: At least 8K, but less than 12K, from end of device
1828 * 1: At start of device
1829 * 2: 4K from start of device.
1831 switch(st
->minor_version
) {
1835 sb_offset
&= ~(4*2-1);
1847 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL) {
1849 pr_err("Cannot seek to superblock on %s: %s\n",
1850 devname
, strerror(errno
));
1854 if (posix_memalign((void**)&super
, 4096, SUPER1_SIZE
) != 0) {
1855 pr_err("%s could not allocate superblock\n",
1860 if (aread(&afd
, super
, MAX_SB_SIZE
) != MAX_SB_SIZE
) {
1862 pr_err("Cannot read superblock on %s\n",
1868 if (__le32_to_cpu(super
->magic
) != MD_SB_MAGIC
) {
1870 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
1871 devname
, MD_SB_MAGIC
, __le32_to_cpu(super
->magic
));
1876 if (__le32_to_cpu(super
->major_version
) != 1) {
1878 pr_err("Cannot interpret superblock on %s - version is %d\n",
1879 devname
, __le32_to_cpu(super
->major_version
));
1883 if (__le64_to_cpu(super
->super_offset
) != sb_offset
) {
1885 pr_err("No superblock found on %s (super_offset is wrong)\n",
1892 bsb
= (struct bitmap_super_s
*)(((char*)super
)+MAX_SB_SIZE
);
1894 misc
= (struct misc_dev_info
*) (((char*)super
)+MAX_SB_SIZE
+BM_SUPER_SIZE
);
1895 misc
->device_size
= dsize
;
1897 /* Now check on the bitmap superblock */
1898 if ((__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) == 0)
1900 /* Read the bitmap superblock and make sure it looks
1901 * valid. If it doesn't clear the bit. An --assemble --force
1902 * should get that written out.
1904 locate_bitmap1(st
, fd
);
1905 if (aread(&afd
, bsb
, 512) != 512)
1908 uuid_from_super1(st
, uuid
);
1909 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
1910 memcmp(bsb
->uuid
, uuid
, 16) != 0)
1915 super
->feature_map
= __cpu_to_le32(__le32_to_cpu(super
->feature_map
)
1916 & ~MD_FEATURE_BITMAP_OFFSET
);
1920 static struct supertype
*match_metadata_desc1(char *arg
)
1922 struct supertype
*st
= xcalloc(1, sizeof(*st
));
1924 st
->container_devnm
[0] = 0;
1926 st
->max_devs
= MAX_DEVS
;
1928 /* leading zeros can be safely ignored. --detail generates them. */
1931 if (strcmp(arg
, "1.0") == 0 ||
1932 strcmp(arg
, "1.00") == 0) {
1933 st
->minor_version
= 0;
1936 if (strcmp(arg
, "1.1") == 0 ||
1937 strcmp(arg
, "1.01") == 0
1939 st
->minor_version
= 1;
1942 if (strcmp(arg
, "1.2") == 0 ||
1943 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1944 strcmp(arg
, "default") == 0 ||
1945 #endif /* DEFAULT_OLD_METADATA */
1946 strcmp(arg
, "1.02") == 0) {
1947 st
->minor_version
= 2;
1950 if (strcmp(arg
, "1") == 0 ||
1951 strcmp(arg
, "default") == 0) {
1952 st
->minor_version
= -1;
1960 /* find available size on device with this devsize, using
1961 * superblock type st, and reserving 'reserve' sectors for
1964 static __u64
_avail_size1(struct supertype
*st
, __u64 devsize
,
1965 unsigned long long data_offset
, int chunksize
)
1967 struct mdp_superblock_1
*super
= st
->sb
;
1973 /* creating: allow suitable space for bitmap */
1974 bmspace
= choose_bm_space(devsize
);
1976 else if (__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
1977 /* hot-add. allow for actual size of bitmap */
1978 struct bitmap_super_s
*bsb
;
1979 bsb
= (struct bitmap_super_s
*)(((char*)super
)+MAX_SB_SIZE
);
1980 bmspace
= bitmap_sectors(bsb
);
1983 /* Allow space for bad block log */
1984 if (super
&& super
->bblog_size
)
1985 devsize
-= __le16_to_cpu(super
->bblog_size
);
1989 if (st
->minor_version
< 0)
1990 /* not specified, so time to set default */
1991 st
->minor_version
= 2;
1993 if (data_offset
!= INVALID_SECTORS
)
1994 switch(st
->minor_version
) {
1996 return devsize
- data_offset
- 8*2;
1999 return devsize
- data_offset
;
2006 if (super
== NULL
&& st
->minor_version
> 0) {
2007 /* haven't committed to a size yet, so allow some
2008 * slack for space for reshape.
2009 * Limit slack to 128M, but aim for about 0.1%
2011 unsigned long long headroom
= 128*1024*2;
2012 while ((headroom
<< 10) > devsize
&&
2014 headroom
/ 2 >= ((unsigned)chunksize
*2)*2))
2016 devsize
-= headroom
;
2018 switch(st
->minor_version
) {
2021 return ((devsize
- 8*2 ) & ~(4*2-1));
2023 /* at start, 4K for superblock and possible bitmap */
2024 return devsize
- 4*2;
2026 /* 4k from start, 4K for superblock and possible bitmap */
2027 return devsize
- (4+4)*2;
2031 static __u64
avail_size1(struct supertype
*st
, __u64 devsize
,
2032 unsigned long long data_offset
)
2034 return _avail_size1(st
, devsize
, data_offset
, 0);
2038 add_internal_bitmap1(struct supertype
*st
,
2039 int *chunkp
, int delay
, int write_behind
,
2040 unsigned long long size
,
2041 int may_change
, int major
)
2044 * If not may_change, then this is a 'Grow' without sysfs support for
2045 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
2046 * If may_change, then this is create or a Grow with sysfs syupport,
2047 * and we can put the bitmap wherever we like.
2049 * size is in sectors, chunk is in bytes !!!
2052 unsigned long long bits
;
2053 unsigned long long max_bits
;
2054 unsigned long long min_chunk
;
2056 long bbl_offset
, bbl_size
;
2057 unsigned long long chunk
= *chunkp
;
2060 struct mdp_superblock_1
*sb
= st
->sb
;
2061 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + MAX_SB_SIZE
);
2064 if (__le64_to_cpu(sb
->data_size
) == 0)
2065 /* Must be creating the array, else data_size would be non-zero */
2067 switch(st
->minor_version
) {
2069 /* either 3K after the superblock (when hot-add),
2070 * or some amount of space before.
2073 /* We are creating array, so we *know* how much room has
2077 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
2080 room
= __le64_to_cpu(sb
->super_offset
)
2081 - __le64_to_cpu(sb
->data_offset
)
2082 - __le64_to_cpu(sb
->data_size
);
2083 bbl_size
= __le16_to_cpu(sb
->bblog_size
);
2086 bbl_offset
= (__s32
)__le32_to_cpu(sb
->bblog_offset
);
2087 if (bbl_size
< -bbl_offset
)
2088 bbl_size
= -bbl_offset
;
2090 if (!may_change
|| (room
< 3*2 &&
2091 __le32_to_cpu(sb
->max_dev
) <= 384)) {
2096 offset
= 0; /* means movable offset */
2101 case 2: /* between superblock and data */
2104 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
2107 room
= __le64_to_cpu(sb
->data_offset
)
2108 - __le64_to_cpu(sb
->super_offset
);
2109 bbl_size
= __le16_to_cpu(sb
->bblog_size
);
2111 room
= __le32_to_cpu(sb
->bblog_offset
) + bbl_size
;
2116 room
-= 2; /* Leave 1K for superblock */
2120 room
-= 4*2; /* leave 4K for superblock */
2130 if (chunk
== UnSet
&& room
> 128*2)
2131 /* Limit to 128K of bitmap when chunk size not requested */
2134 max_bits
= (room
* 512 - sizeof(bitmap_super_t
)) * 8;
2136 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
2137 bits
= (size
*512)/min_chunk
+1;
2138 while (bits
> max_bits
) {
2142 if (chunk
== UnSet
) {
2143 /* For practical purpose, 64Meg is a good
2144 * default chunk size for internal bitmaps.
2147 if (chunk
< 64*1024*1024)
2148 chunk
= 64*1024*1024;
2149 } else if (chunk
< min_chunk
)
2150 return 0; /* chunk size too small */
2151 if (chunk
== 0) /* rounding problem */
2155 /* start bitmap on a 4K boundary with enough space for
2158 bits
= (size
*512) / chunk
+ 1;
2159 room
= ((bits
+7)/8 + sizeof(bitmap_super_t
) +4095)/4096;
2160 room
*= 8; /* convert 4K blocks to sectors */
2161 offset
= -room
- bbl_size
;
2164 sb
->bitmap_offset
= (int32_t)__cpu_to_le32(offset
);
2166 sb
->feature_map
= __cpu_to_le32(__le32_to_cpu(sb
->feature_map
)
2167 | MD_FEATURE_BITMAP_OFFSET
);
2168 memset(bms
, 0, sizeof(*bms
));
2169 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
2170 bms
->version
= __cpu_to_le32(major
);
2171 uuid_from_super1(st
, uuid
);
2172 memcpy(bms
->uuid
, uuid
, 16);
2173 bms
->chunksize
= __cpu_to_le32(chunk
);
2174 bms
->daemon_sleep
= __cpu_to_le32(delay
);
2175 bms
->sync_size
= __cpu_to_le64(size
);
2176 bms
->write_behind
= __cpu_to_le32(write_behind
);
2182 static void locate_bitmap1(struct supertype
*st
, int fd
)
2184 unsigned long long offset
;
2185 struct mdp_superblock_1
*sb
;
2189 if (st
->ss
->load_super(st
, fd
, NULL
))
2190 return; /* no error I hope... */
2195 offset
= __le64_to_cpu(sb
->super_offset
);
2196 offset
+= (int32_t) __le32_to_cpu(sb
->bitmap_offset
);
2199 lseek64(fd
, offset
<<9, 0);
2202 static int write_bitmap1(struct supertype
*st
, int fd
)
2204 struct mdp_superblock_1
*sb
= st
->sb
;
2205 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
)+MAX_SB_SIZE
);
2209 struct align_fd afd
;
2213 locate_bitmap1(st
, fd
);
2215 if (posix_memalign(&buf
, 4096, 4096))
2218 memset(buf
, 0xff, 4096);
2219 memcpy(buf
, (char *)bms
, sizeof(bitmap_super_t
));
2221 towrite
= __le64_to_cpu(bms
->sync_size
) / (__le32_to_cpu(bms
->chunksize
)>>9);
2222 towrite
= (towrite
+7) >> 3; /* bits to bytes */
2223 towrite
+= sizeof(bitmap_super_t
);
2224 towrite
= ROUND_UP(towrite
, 512);
2225 while (towrite
> 0) {
2229 n
= awrite(&afd
, buf
, n
);
2234 memset(buf
, 0xff, 4096);
2244 static void free_super1(struct supertype
*st
)
2249 struct devinfo
*di
= st
->info
;
2250 st
->info
= di
->next
;
2259 static int validate_geometry1(struct supertype
*st
, int level
,
2260 int layout
, int raiddisks
,
2261 int *chunk
, unsigned long long size
,
2262 unsigned long long data_offset
,
2263 char *subdev
, unsigned long long *freesize
,
2266 unsigned long long ldsize
;
2269 if (level
== LEVEL_CONTAINER
) {
2271 pr_err("1.x metadata does not support containers\n");
2274 if (chunk
&& *chunk
== UnSet
)
2275 *chunk
= DEFAULT_CHUNK
;
2280 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
2283 pr_err("super1.x cannot open %s: %s\n",
2284 subdev
, strerror(errno
));
2288 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
2294 *freesize
= _avail_size1(st
, ldsize
>> 9, data_offset
, *chunk
);
2297 #endif /* MDASSEMBLE */
2299 void *super1_make_v0(struct supertype
*st
, struct mdinfo
*info
, mdp_super_t
*sb0
)
2301 /* Create a v1.0 superblock based on 'info'*/
2303 struct mdp_superblock_1
*sb
;
2306 unsigned long long offset
;
2308 if (posix_memalign(&ret
, 4096, 1024) != 0)
2311 memset(ret
, 0, 1024);
2312 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
2313 sb
->major_version
= __cpu_to_le32(1);
2315 copy_uuid(sb
->set_uuid
, info
->uuid
, super1
.swapuuid
);
2316 sprintf(sb
->set_name
, "%d", sb0
->md_minor
);
2317 sb
->ctime
= __cpu_to_le32(info
->array
.ctime
+1);
2318 sb
->level
= __cpu_to_le32(info
->array
.level
);
2319 sb
->layout
= __cpu_to_le32(info
->array
.layout
);
2320 sb
->size
= __cpu_to_le64(info
->component_size
);
2321 sb
->chunksize
= __cpu_to_le32(info
->array
.chunk_size
/512);
2322 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
2323 sb
->data_size
= sb
->size
;
2324 sb
->resync_offset
= MaxSector
;
2325 sb
->max_dev
= __cpu_to_le32(MD_SB_DISKS
);
2326 sb
->dev_number
= __cpu_to_le32(info
->disk
.number
);
2327 sb
->utime
= __cpu_to_le64(info
->array
.utime
);
2329 offset
= st
->devsize
/512 - 8*2;
2331 sb
->super_offset
= __cpu_to_le64(offset
);
2332 //*(__u64*)(st->other + 128 + 8 + 8) = __cpu_to_le64(offset);
2334 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
2335 read(rfd
, sb
->device_uuid
, 16) != 16) {
2336 __u32 r
[4] = {random(), random(), random(), random()};
2337 memcpy(sb
->device_uuid
, r
, 16);
2342 for (i
= 0; i
< MD_SB_DISKS
; i
++) {
2343 int state
= sb0
->disks
[i
].state
;
2344 sb
->dev_roles
[i
] = 0xFFFF;
2345 if ((state
& (1<<MD_DISK_SYNC
)) &&
2346 !(state
& (1<<MD_DISK_FAULTY
)))
2347 sb
->dev_roles
[i
] = __cpu_to_le16(sb0
->disks
[i
].raid_disk
);
2349 sb
->sb_csum
= calc_sb_1_csum(sb
);
2353 struct superswitch super1
= {
2355 .examine_super
= examine_super1
,
2356 .brief_examine_super
= brief_examine_super1
,
2357 .export_examine_super
= export_examine_super1
,
2358 .detail_super
= detail_super1
,
2359 .brief_detail_super
= brief_detail_super1
,
2360 .export_detail_super
= export_detail_super1
,
2361 .write_init_super
= write_init_super1
,
2362 .validate_geometry
= validate_geometry1
,
2363 .add_to_super
= add_to_super1
,
2364 .examine_badblocks
= examine_badblocks_super1
,
2365 .copy_metadata
= copy_metadata1
,
2367 .match_home
= match_home1
,
2368 .uuid_from_super
= uuid_from_super1
,
2369 .getinfo_super
= getinfo_super1
,
2370 .container_content
= container_content1
,
2371 .update_super
= update_super1
,
2372 .init_super
= init_super1
,
2373 .store_super
= store_super1
,
2374 .compare_super
= compare_super1
,
2375 .load_super
= load_super1
,
2376 .match_metadata_desc
= match_metadata_desc1
,
2377 .avail_size
= avail_size1
,
2378 .add_internal_bitmap
= add_internal_bitmap1
,
2379 .locate_bitmap
= locate_bitmap1
,
2380 .write_bitmap
= write_bitmap1
,
2381 .free_super
= free_super1
,
2382 #if __BYTE_ORDER == BIG_ENDIAN