2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2016 Neil Brown <neilb@suse.com>
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>
28 * The version-1 superblock :
29 * All numeric fields are little-endian.
31 * total size: 256 bytes plus 2 per device.
32 * 1K allows 384 devices.
34 struct mdp_superblock_1 {
35 /* constant array information - 128 bytes */
36 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
37 __u32 major_version; /* 1 */
38 __u32 feature_map; /* 0 for now */
39 __u32 pad0; /* always set to 0 when writing */
41 __u8 set_uuid[16]; /* user-space generated. */
42 char set_name[32]; /* set and interpreted by user-space */
44 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
45 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
46 __u32 layout; /* only for raid5 currently */
47 __u64 size; /* used size of component devices, in 512byte sectors */
49 __u32 chunksize; /* in 512byte sectors */
51 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
52 * NOTE: signed, so bitmap can be before superblock
53 * only meaningful of feature_map[0] is set.
56 /* These are only valid with feature bit '4' */
57 __u32 new_level; /* new level we are reshaping to */
58 __u64 reshape_position; /* next address in array-space for reshape */
59 __u32 delta_disks; /* change in number of raid_disks */
60 __u32 new_layout; /* new layout */
61 __u32 new_chunk; /* new chunk size (sectors) */
62 __u32 new_offset; /* signed number to add to data_offset in new
63 * layout. 0 == no-change. This can be
64 * different on each device in the array.
67 /* constant this-device information - 64 bytes */
68 __u64 data_offset; /* sector start of data, often 0 */
69 __u64 data_size; /* sectors in this device that can be used for data */
70 __u64 super_offset; /* sector start of this superblock */
72 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
73 __u64 journal_tail;/* journal tail of journal device (from data_offset) */
75 __u32 dev_number; /* permanent identifier of this device - not role in raid */
76 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
77 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
78 __u8 devflags; /* per-device flags. Only one defined...*/
79 #define WriteMostly1 1 /* mask for writemostly flag in above */
80 /* bad block log. If there are any bad blocks the feature flag is set.
81 * if offset and size are non-zero, that space is reserved and available.
83 __u8 bblog_shift; /* shift from sectors to block size for badblocklist */
84 __u16 bblog_size; /* number of sectors reserved for badblocklist */
85 __u32 bblog_offset; /* sector offset from superblock to bblog, signed */
87 /* array state information - 64 bytes */
88 __u64 utime; /* 40 bits second, 24 btes microseconds */
89 __u64 events; /* incremented when superblock updated */
90 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
91 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
92 __u32 max_dev; /* size of dev_roles[] array to consider */
93 __u8 pad3[64-32]; /* set to 0 when writing */
95 /* device state information. Indexed by dev_number.
97 * Note there are no per-device state flags. State information is rolled
98 * into the 'roles' value. If a device is spare or faulty, then it doesn't
99 * have a meaningful role.
101 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
104 #define MAX_SB_SIZE 4096
105 /* bitmap super size is 256, but we round up to a sector for alignment */
106 #define BM_SUPER_SIZE 512
107 #define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
108 #define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
109 + sizeof(struct misc_dev_info))
111 struct misc_dev_info {
115 /* feature_map bits */
116 #define MD_FEATURE_BITMAP_OFFSET 1
117 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
120 #define MD_FEATURE_RESHAPE_ACTIVE 4
121 #define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
122 #define MD_FEATURE_REPLACEMENT 16 /* This device is replacing an
123 * active device with same 'role'.
124 * 'recovery_offset' is also set.
126 #define MD_FEATURE_RESHAPE_BACKWARDS 32 /* Reshape doesn't change number
127 * of devices, but is going
130 #define MD_FEATURE_NEW_OFFSET 64 /* new_offset must be honoured */
131 #define MD_FEATURE_BITMAP_VERSIONED 256 /* bitmap version number checked properly */
132 #define MD_FEATURE_JOURNAL 512 /* support write journal */
133 #define MD_FEATURE_ALL (MD_FEATURE_BITMAP_OFFSET \
134 |MD_FEATURE_RECOVERY_OFFSET \
135 |MD_FEATURE_RESHAPE_ACTIVE \
136 |MD_FEATURE_BAD_BLOCKS \
137 |MD_FEATURE_REPLACEMENT \
138 |MD_FEATURE_RESHAPE_BACKWARDS \
139 |MD_FEATURE_NEW_OFFSET \
140 |MD_FEATURE_BITMAP_VERSIONED \
141 |MD_FEATURE_JOURNAL \
145 static int role_from_sb(struct mdp_superblock_1 *sb)
150 d = __le32_to_cpu(sb->dev_number);
151 if (d < __le32_to_cpu(sb->max_dev))
152 role = __le16_to_cpu(sb->dev_roles[d]);
154 role = MD_DISK_ROLE_SPARE;
159 /* return how many bytes are needed for bitmap, for cluster-md each node
160 * should have it's own bitmap */
161 static unsigned int calc_bitmap_size(bitmap_super_t *bms, unsigned int boundary)
163 unsigned long long bits, bytes;
165 bits = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
166 bytes = (bits+7) >> 3;
167 bytes += sizeof(bitmap_super_t);
168 bytes = ROUND_UP(bytes, boundary);
173 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
175 unsigned int disk_csum, csum;
176 unsigned long long newcsum;
177 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
178 unsigned int *isuper = (unsigned int*)sb;
180 /* make sure I can count... */
181 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
182 offsetof(struct mdp_superblock_1, utime) != 192 ||
183 sizeof(struct mdp_superblock_1) != 256) {
184 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
187 disk_csum = sb->sb_csum;
190 for (; size>=4; size -= 4 ) {
191 newcsum += __le32_to_cpu(*isuper);
196 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
198 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
199 sb->sb_csum = disk_csum;
200 return __cpu_to_le32(csum);
204 * Information related to file descriptor used for aligned reads/writes.
205 * Cache the block size.
212 static void init_afd(struct align_fd *afd, int fd)
216 if (ioctl(afd->fd, BLKSSZGET, &afd->blk_sz) != 0)
220 static char abuf[4096+4096];
221 static int aread(struct align_fd *afd, void *buf, int len)
224 * On devices with a 4K sector size, we need to read
225 * the full sector and copy relevant bits into
234 if (!bsize || bsize > 4096 || len > 4096) {
236 fprintf(stderr, "WARNING - aread() called with invalid block size\n");
239 b = ROUND_UP_PTR((char *)abuf, 4096);
241 for (iosize = 0; iosize < len; iosize += bsize)
243 n = read(afd->fd, b, iosize);
246 lseek(afd->fd, len - n, 1);
253 static int awrite(struct align_fd *afd, void *buf, int len)
256 * On devices with a 4K sector size, we need to write
257 * the full sector. We pre-read if the sector is larger
259 * The address must be sector-aligned.
266 if (!bsize || bsize > 4096 || len > 4096) {
268 fprintf(stderr, "WARNING - awrite() called with invalid block size\n");
271 b = ROUND_UP_PTR((char *)abuf, 4096);
273 for (iosize = 0; iosize < len ; iosize += bsize)
277 n = read(afd->fd, b, iosize);
280 lseek(afd->fd, -n, 1);
284 n = write(afd->fd, b, iosize);
287 lseek(afd->fd, len - n, 1);
292 static void examine_super1(struct supertype *st, char *homehost)
294 struct mdp_superblock_1 *sb = st->sb;
295 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
302 int l = homehost ? strlen(homehost) : 0;
304 unsigned long long sb_offset;
307 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
308 printf(" Version : 1");
309 sb_offset = __le64_to_cpu(sb->super_offset);
312 else if (sb_offset <= 8)
316 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
317 printf(" Array UUID : ");
318 for (i=0; i<16; i++) {
319 if ((i&3)==0 && i != 0) printf(":");
320 printf("%02x", sb->set_uuid[i]);
323 printf(" Name : %.32s", sb->set_name);
324 if (l > 0 && l < 32 &&
325 sb->set_name[l] == ':' &&
326 strncmp(sb->set_name, homehost, l) == 0)
327 printf(" (local to host %s)", homehost);
329 if (bms->nodes > 0 && (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
330 printf(" Cluster Name : %-64s\n", bms->cluster_name);
331 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
332 printf(" Creation Time : %.24s\n", ctime(&atime));
333 c=map_num(pers, __le32_to_cpu(sb->level));
334 printf(" Raid Level : %s\n", c?c:"-unknown-");
335 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
337 printf(" Avail Dev Size : %llu%s\n",
338 (unsigned long long)__le64_to_cpu(sb->data_size),
339 human_size(__le64_to_cpu(sb->data_size)<<9));
340 if (__le32_to_cpu(sb->level) > 0) {
341 int ddsks = 0, ddsks_denom = 1;
342 switch(__le32_to_cpu(sb->level)) {
343 case 1: ddsks=1;break;
345 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
346 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
348 layout = __le32_to_cpu(sb->layout);
349 ddsks = __le32_to_cpu(sb->raid_disks);
350 ddsks_denom = (layout&255) * ((layout>>8)&255);
353 long long asize = __le64_to_cpu(sb->size);
354 asize = (asize << 9) * ddsks / ddsks_denom;
355 printf(" Array Size : %llu%s\n",
356 asize >> 10, human_size(asize));
358 if (sb->size != sb->data_size)
359 printf(" Used Dev Size : %llu%s\n",
360 (unsigned long long)__le64_to_cpu(sb->size),
361 human_size(__le64_to_cpu(sb->size)<<9));
364 printf(" Data Offset : %llu sectors\n",
365 (unsigned long long)__le64_to_cpu(sb->data_offset));
366 if (sb->new_offset &&
367 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) {
368 unsigned long long offset = __le64_to_cpu(sb->data_offset);
369 offset += (signed)(int32_t)__le32_to_cpu(sb->new_offset);
370 printf(" New Offset : %llu sectors\n", offset);
372 printf(" Super Offset : %llu sectors\n",
373 (unsigned long long)__le64_to_cpu(sb->super_offset));
374 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
375 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
377 st->ss->getinfo_super(st, &info, NULL);
378 if (info.space_after != 1 &&
379 !(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
380 printf(" Unused Space : before=%llu sectors, after=%llu sectors\n",
381 info.space_before, info.space_after);
383 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
384 printf(" Device UUID : ");
385 for (i=0; i<16; i++) {
386 if ((i&3)==0 && i != 0) printf(":");
387 printf("%02x", sb->device_uuid[i]);
391 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
392 printf("Internal Bitmap : %ld sectors from superblock\n",
393 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
395 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)) {
396 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
397 human_size(__le64_to_cpu(sb->reshape_position)<<9));
398 if (__le32_to_cpu(sb->delta_disks)) {
399 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
400 printf(" (%d->%d)\n",
401 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
402 __le32_to_cpu(sb->raid_disks));
403 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
404 delta_extra = -__le32_to_cpu(sb->delta_disks);
406 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
407 c = map_num(pers, __le32_to_cpu(sb->new_level));
408 printf(" New Level : %s\n", c?c:"-unknown-");
410 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
411 if (__le32_to_cpu(sb->level) == 5) {
412 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
413 printf(" New Layout : %s\n", c?c:"-unknown-");
415 if (__le32_to_cpu(sb->level) == 6) {
416 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
417 printf(" New Layout : %s\n", c?c:"-unknown-");
419 if (__le32_to_cpu(sb->level) == 10) {
420 printf(" New Layout :");
421 print_r10_layout(__le32_to_cpu(sb->new_layout));
425 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
426 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
431 if (sb->devflags & WriteMostly1)
432 printf(" write-mostly");
436 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
437 printf(" Update Time : %.24s\n", ctime(&atime));
439 if (sb->bblog_size && sb->bblog_offset) {
440 printf(" Bad Block Log : %d entries available at offset %ld sectors",
441 __le16_to_cpu(sb->bblog_size)*512/8,
442 (long)(int32_t)__le32_to_cpu(sb->bblog_offset));
443 if (sb->feature_map &
444 __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
445 printf(" - bad blocks present.");
449 if (calc_sb_1_csum(sb) == sb->sb_csum)
450 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
452 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
453 __le32_to_cpu(calc_sb_1_csum(sb)));
454 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
456 if (__le32_to_cpu(sb->level) == 5) {
457 c = map_num(r5layout, __le32_to_cpu(sb->layout));
458 printf(" Layout : %s\n", c?c:"-unknown-");
460 if (__le32_to_cpu(sb->level) == 6) {
461 c = map_num(r6layout, __le32_to_cpu(sb->layout));
462 printf(" Layout : %s\n", c?c:"-unknown-");
464 if (__le32_to_cpu(sb->level) == 10) {
465 int lo = __le32_to_cpu(sb->layout);
467 print_r10_layout(lo);
470 switch(__le32_to_cpu(sb->level)) {
476 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
479 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
485 /* This turns out to just be confusing */
486 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
487 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
488 if (__le16_to_cpu(sb->dev_roles[i-1]) != MD_DISK_ROLE_SPARE)
490 for (d=0; d < i; d++) {
491 int role = __le16_to_cpu(sb->dev_roles[d]);
493 if (role == MD_DISK_ROLE_SPARE) printf("empty");
494 else if(role == MD_DISK_ROLE_FAULTY) printf("failed");
495 else printf("%d", role);
499 printf(" Device Role : ");
500 role = role_from_sb(sb);
501 if (role >= MD_DISK_ROLE_FAULTY)
503 else if (role == MD_DISK_ROLE_JOURNAL)
505 else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_REPLACEMENT))
506 printf("Replacement device %d\n", role);
508 printf("Active device %d\n", role);
510 printf(" Array State : ");
511 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
514 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
515 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
529 /* This is confusing too */
531 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
532 int role = __le16_to_cpu(sb->dev_roles[i]);
533 if (role == MD_DISK_ROLE_FAULTY)
536 if (faulty) printf(" %d failed", faulty);
538 printf(" ('A' == active, '.' == missing, 'R' == replacing)");
542 static void brief_examine_super1(struct supertype *st, int verbose)
544 struct mdp_superblock_1 *sb = st->sb;
546 unsigned long long sb_offset;
548 char *c=map_num(pers, __le32_to_cpu(sb->level));
550 nm = strchr(sb->set_name, ':');
553 else if (sb->set_name[0])
565 printf(" level=%s", c);
566 sb_offset = __le64_to_cpu(sb->super_offset);
568 printf(" metadata=1.1 ");
569 else if (sb_offset <= 8)
570 printf(" metadata=1.2 ");
572 printf(" metadata=1.0 ");
574 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
576 for (i=0; i<16; i++) {
577 if ((i&3)==0 && i != 0) printf(":");
578 printf("%02x", sb->set_uuid[i]);
580 if (sb->set_name[0]) {
582 print_quoted(sb->set_name);
587 static void export_examine_super1(struct supertype *st)
589 struct mdp_superblock_1 *sb = st->sb;
594 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
595 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
597 if (sb->set_name[i] == '\n' ||
598 sb->set_name[i] == '\0') {
603 printf("MD_NAME=%.*s\n", len, sb->set_name);
604 if (__le32_to_cpu(sb->level) > 0) {
605 int ddsks = 0, ddsks_denom = 1;
606 switch(__le32_to_cpu(sb->level)) {
607 case 1: ddsks=1;break;
609 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
610 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
612 layout = __le32_to_cpu(sb->layout);
613 ddsks = __le32_to_cpu(sb->raid_disks);
614 ddsks_denom = (layout&255) * ((layout>>8)&255);
617 long long asize = __le64_to_cpu(sb->size);
618 asize = (asize << 9) * ddsks / ddsks_denom;
619 printf("MD_ARRAY_SIZE=%s\n",human_size_brief(asize,JEDEC));
623 for (i=0; i<16; i++) {
624 if ((i&3)==0 && i != 0) printf(":");
625 printf("%02x", sb->set_uuid[i]);
628 printf("MD_UPDATE_TIME=%llu\n",
629 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
630 printf("MD_DEV_UUID=");
631 for (i=0; i<16; i++) {
632 if ((i&3)==0 && i != 0) printf(":");
633 printf("%02x", sb->device_uuid[i]);
636 printf("MD_EVENTS=%llu\n",
637 (unsigned long long)__le64_to_cpu(sb->events));
640 static int copy_metadata1(struct supertype *st, int from, int to)
642 /* Read superblock. If it looks good, write it out.
643 * Then if a bitmap is present, copy that.
644 * And if a bad-block-list is present, copy that too.
647 unsigned long long dsize, sb_offset;
648 const int bufsize = 4*1024;
649 struct mdp_superblock_1 super, *sb;
651 if (posix_memalign(&buf, 4096, bufsize) != 0)
654 if (!get_dev_size(from, NULL, &dsize))
660 switch(st->minor_version) {
664 sb_offset &= ~(4*2-1);
676 if (lseek64(from, sb_offset << 9, 0) < 0LL)
678 if (read(from, buf, bufsize) != bufsize)
682 super = *sb; // save most of sb for when we reuse buf
684 if (__le32_to_cpu(super.magic) != MD_SB_MAGIC ||
685 __le32_to_cpu(super.major_version) != 1 ||
686 __le64_to_cpu(super.super_offset) != sb_offset ||
687 calc_sb_1_csum(sb) != super.sb_csum)
690 if (lseek64(to, sb_offset << 9, 0) < 0LL)
692 if (write(to, buf, bufsize) != bufsize)
695 if (super.feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
696 unsigned long long bitmap_offset = sb_offset;
697 int bytes = 4096; // just an estimate.
699 struct align_fd afrom, ato;
701 init_afd(&afrom, from);
704 bitmap_offset += (int32_t)__le32_to_cpu(super.bitmap_offset);
706 if (lseek64(from, bitmap_offset<<9, 0) < 0)
708 if (lseek64(to, bitmap_offset<<9, 0) < 0)
711 for (written = 0; written < bytes ; ) {
712 int n = bytes - written;
715 if (aread(&afrom, buf, n) != n)
718 /* have the header, can calculate
719 * correct bitmap bytes */
722 bytes = calc_bitmap_size(bms, 512);
726 if (awrite(&ato, buf, n) != n)
732 if (super.bblog_size != 0 &&
733 __le16_to_cpu(super.bblog_size) <= 100 &&
734 super.bblog_offset != 0 &&
735 (super.feature_map & __le32_to_cpu(MD_FEATURE_BAD_BLOCKS))) {
736 /* There is a bad block log */
737 unsigned long long bb_offset = sb_offset;
738 int bytes = __le16_to_cpu(super.bblog_size) * 512;
740 struct align_fd afrom, ato;
742 init_afd(&afrom, from);
745 bb_offset += (int32_t)__le32_to_cpu(super.bblog_offset);
747 if (lseek64(from, bb_offset<<9, 0) < 0)
749 if (lseek64(to, bb_offset<<9, 0) < 0)
752 for (written = 0; written < bytes ; ) {
753 int n = bytes - written;
756 if (aread(&afrom, buf, n) != n)
759 if (awrite(&ato, buf, n) != n)
773 static void detail_super1(struct supertype *st, char *homehost)
775 struct mdp_superblock_1 *sb = st->sb;
776 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
778 int l = homehost ? strlen(homehost) : 0;
780 printf(" Name : %.32s", sb->set_name);
781 if (l > 0 && l < 32 &&
782 sb->set_name[l] == ':' &&
783 strncmp(sb->set_name, homehost, l) == 0)
784 printf(" (local to host %s)", homehost);
785 if (bms->nodes > 0 && (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
786 printf("\n Cluster Name : %-64s", bms->cluster_name);
787 printf("\n UUID : ");
788 for (i=0; i<16; i++) {
789 if ((i&3)==0 && i != 0) printf(":");
790 printf("%02x", sb->set_uuid[i]);
792 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
795 static void brief_detail_super1(struct supertype *st)
797 struct mdp_superblock_1 *sb = st->sb;
800 if (sb->set_name[0]) {
802 print_quoted(sb->set_name);
805 for (i=0; i<16; i++) {
806 if ((i&3)==0 && i != 0) printf(":");
807 printf("%02x", sb->set_uuid[i]);
811 static void export_detail_super1(struct supertype *st)
813 struct mdp_superblock_1 *sb = st->sb;
818 if (sb->set_name[i] == '\n' ||
819 sb->set_name[i] == '\0') {
824 printf("MD_NAME=%.*s\n", len, sb->set_name);
827 static int examine_badblocks_super1(struct supertype *st, int fd, char *devname)
829 struct mdp_superblock_1 *sb = st->sb;
830 unsigned long long offset;
835 if (!sb->bblog_size || __le16_to_cpu(sb->bblog_size) > 100
836 || !sb->bblog_offset){
837 printf("No bad-blocks list configured on %s\n", devname);
840 if ((sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
842 printf("Bad-blocks list is empty in %s\n", devname);
846 size = __le16_to_cpu(sb->bblog_size)* 512;
847 if (posix_memalign((void**)&bbl, 4096, size) != 0) {
848 pr_err("could not allocate badblocks list\n");
851 offset = __le64_to_cpu(sb->super_offset) +
852 (int)__le32_to_cpu(sb->bblog_offset);
854 if (lseek64(fd, offset, 0) < 0) {
855 pr_err("Cannot seek to bad-blocks list\n");
858 if (read(fd, bbl, size) != size) {
859 pr_err("Cannot read bad-blocks list\n");
862 /* 64bits per entry. 10 bits is block-count, 54 bits is block
863 * offset. Blocks are sectors unless bblog->shift makes them bigger
866 printf("Bad-blocks on %s:\n", devname);
867 for (i = 0; i < size/8; i++, bbp++) {
868 __u64 bb = __le64_to_cpu(*bbp);
869 int count = bb & 0x3ff;
870 unsigned long long sector = bb >> 10;
875 sector <<= sb->bblog_shift;
876 count <<= sb->bblog_shift;
878 printf("%20llu for %d sectors\n", sector, count);
885 static int match_home1(struct supertype *st, char *homehost)
887 struct mdp_superblock_1 *sb = st->sb;
888 int l = homehost ? strlen(homehost) : 0;
890 return (l > 0 && l < 32 &&
891 sb->set_name[l] == ':' &&
892 strncmp(sb->set_name, homehost, l) == 0);
895 static void uuid_from_super1(struct supertype *st, int uuid[4])
897 struct mdp_superblock_1 *super = st->sb;
898 char *cuuid = (char*)uuid;
901 cuuid[i] = super->set_uuid[i];
904 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
906 struct mdp_superblock_1 *sb = st->sb;
907 struct bitmap_super_s *bsb = (void*)(((char*)sb)+MAX_SB_SIZE);
908 struct misc_dev_info *misc = (void*)(((char*)sb)+MAX_SB_SIZE+BM_SUPER_SIZE);
912 unsigned int map_disks = info->array.raid_disks;
913 unsigned long long super_offset;
914 unsigned long long data_size;
916 memset(info, 0, sizeof(*info));
917 info->array.major_version = 1;
918 info->array.minor_version = st->minor_version;
919 info->array.patch_version = 0;
920 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
921 info->array.level = __le32_to_cpu(sb->level);
922 info->array.layout = __le32_to_cpu(sb->layout);
923 info->array.md_minor = -1;
924 info->array.ctime = __le64_to_cpu(sb->ctime);
925 info->array.utime = __le64_to_cpu(sb->utime);
926 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
928 (__le64_to_cpu(sb->resync_offset) == MaxSector)
930 if (__le32_to_cpu(bsb->nodes) > 1)
931 info->array.state |= (1 << MD_SB_CLUSTERED);
933 info->data_offset = __le64_to_cpu(sb->data_offset);
934 info->component_size = __le64_to_cpu(sb->size);
935 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET))
936 info->bitmap_offset = (int32_t)__le32_to_cpu(sb->bitmap_offset);
938 info->disk.major = 0;
939 info->disk.minor = 0;
940 info->disk.number = __le32_to_cpu(sb->dev_number);
941 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
942 __le32_to_cpu(sb->dev_number) >= MAX_DEVS)
943 role = MD_DISK_ROLE_FAULTY;
945 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
947 super_offset = __le64_to_cpu(sb->super_offset);
948 if (info->array.level <= 0)
949 data_size = __le64_to_cpu(sb->data_size);
951 data_size = __le64_to_cpu(sb->size);
952 if (info->data_offset < super_offset) {
953 unsigned long long end;
954 info->space_before = info->data_offset;
957 if (sb->bblog_offset && sb->bblog_size) {
958 unsigned long long bboffset = super_offset;
959 bboffset += (int32_t)__le32_to_cpu(sb->bblog_offset);
964 if (super_offset + info->bitmap_offset < end)
965 end = super_offset + info->bitmap_offset;
967 if (info->data_offset + data_size < end)
968 info->space_after = end - data_size - info->data_offset;
970 info->space_after = 0;
972 unsigned long long earliest;
973 earliest = super_offset + (32+4)*2; /* match kernel */
974 if (info->bitmap_offset > 0) {
975 unsigned long long bmend = info->bitmap_offset;
976 unsigned long long size = __le64_to_cpu(bsb->sync_size);
977 size /= __le32_to_cpu(bsb->chunksize) >> 9;
978 size = (size + 7) >> 3;
979 size += sizeof(bitmap_super_t);
980 size = ROUND_UP(size, 4096);
983 if (bmend > earliest)
986 if (sb->bblog_offset && sb->bblog_size) {
987 unsigned long long bbend = super_offset;
988 bbend += (int32_t)__le32_to_cpu(sb->bblog_offset);
989 bbend += __le16_to_cpu(sb->bblog_size);
990 if (bbend > earliest)
993 if (earliest < info->data_offset)
994 info->space_before = info->data_offset - earliest;
996 info->space_before = 0;
997 info->space_after = misc->device_size - data_size - info->data_offset;
999 if (info->space_before == 0 && info->space_after == 0) {
1000 /* It will look like we don't support data_offset changes,
1001 * be we do - it's just that there is no room.
1002 * A change that reduced the number of devices should
1003 * still be allowed, so set the otherwise useless value of '1'
1005 info->space_after = 1;
1008 info->disk.raid_disk = -1;
1010 case MD_DISK_ROLE_SPARE:
1011 info->disk.state = 0; /* spare: not active, not sync, not faulty */
1013 case MD_DISK_ROLE_FAULTY:
1014 info->disk.state = 1; /* faulty */
1016 case MD_DISK_ROLE_JOURNAL:
1017 info->disk.state = (1 << MD_DISK_JOURNAL);
1018 info->disk.raid_disk = role;
1019 info->space_after = (misc->device_size - info->data_offset) % 8; /* journal uses all 4kB blocks*/
1022 info->disk.state = 6; /* active and in sync */
1023 info->disk.raid_disk = role;
1025 if (sb->devflags & WriteMostly1)
1026 info->disk.state |= (1 << MD_DISK_WRITEMOSTLY);
1027 info->events = __le64_to_cpu(sb->events);
1028 sprintf(info->text_version, "1.%d", st->minor_version);
1029 info->safe_mode_delay = 200;
1031 memcpy(info->uuid, sb->set_uuid, 16);
1033 strncpy(info->name, sb->set_name, 32);
1036 if ((__le32_to_cpu(sb->feature_map)&MD_FEATURE_REPLACEMENT)) {
1037 info->disk.state &= ~(1 << MD_DISK_SYNC);
1038 info->disk.state |= 1 << MD_DISK_REPLACEMENT;
1041 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
1042 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
1044 info->recovery_start = MaxSector;
1046 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1047 info->reshape_active = 1;
1048 if ((sb->feature_map & __le32_to_cpu(MD_FEATURE_NEW_OFFSET)) &&
1049 sb->new_offset != 0)
1050 info->reshape_active |= RESHAPE_NO_BACKUP;
1051 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
1052 info->new_level = __le32_to_cpu(sb->new_level);
1053 info->delta_disks = __le32_to_cpu(sb->delta_disks);
1054 info->new_layout = __le32_to_cpu(sb->new_layout);
1055 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
1056 if (info->delta_disks < 0)
1057 info->array.raid_disks -= info->delta_disks;
1059 info->reshape_active = 0;
1061 info->recovery_blocked = info->reshape_active;
1064 for (i=0; i<map_disks; i++)
1066 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
1067 role = __le16_to_cpu(sb->dev_roles[i]);
1068 if (/*role == MD_DISK_ROLE_SPARE || */role < (unsigned) info->array.raid_disks) {
1070 if (map && role < map_disks)
1075 info->array.working_disks = working;
1076 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_JOURNAL))
1077 info->journal_device_required = 1;
1078 info->journal_clean = 0;
1081 static struct mdinfo *container_content1(struct supertype *st, char *subarray)
1083 struct mdinfo *info;
1088 info = xmalloc(sizeof(*info));
1089 getinfo_super1(st, info, NULL);
1093 static int update_super1(struct supertype *st, struct mdinfo *info,
1095 char *devname, int verbose,
1096 int uuid_set, char *homehost)
1098 /* NOTE: for 'assemble' and 'force' we need to return non-zero
1099 * if any change was made. For others, the return value is
1104 struct mdp_superblock_1 *sb = st->sb;
1105 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1107 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1108 rv = cluster_get_dlmlock(&lockid);
1110 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
1111 cluster_release_dlmlock(lockid);
1116 if (strcmp(update, "homehost") == 0 &&
1118 /* Note that 'homehost' is special as it is really
1123 c = strchr(sb->set_name, ':');
1125 strncpy(info->name, c+1, 31 - (c-sb->set_name));
1127 strncpy(info->name, sb->set_name, 32);
1131 if (strcmp(update, "force-one")==0) {
1132 /* Not enough devices for a working array,
1133 * so bring this one up-to-date
1135 if (sb->events != __cpu_to_le64(info->events))
1137 sb->events = __cpu_to_le64(info->events);
1138 } else if (strcmp(update, "force-array")==0) {
1139 /* Degraded array and 'force' requests to
1140 * maybe need to mark it 'clean'.
1142 switch(__le32_to_cpu(sb->level)) {
1143 case 5: case 4: case 6:
1144 /* need to force clean */
1145 if (sb->resync_offset != MaxSector)
1147 sb->resync_offset = MaxSector;
1149 } else if (strcmp(update, "assemble")==0) {
1150 int d = info->disk.number;
1152 if (info->disk.state & (1<<MD_DISK_ACTIVE))
1153 want = info->disk.raid_disk;
1154 else if (info->disk.state & (1<<MD_DISK_JOURNAL))
1155 want = MD_DISK_ROLE_JOURNAL;
1157 want = MD_DISK_ROLE_SPARE;
1158 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
1159 sb->dev_roles[d] = __cpu_to_le16(want);
1162 if (info->reshape_active &&
1163 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1164 info->delta_disks >= 0 &&
1165 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
1166 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1169 if (info->reshape_active &&
1170 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1171 info->delta_disks < 0 &&
1172 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
1173 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1176 } else if (strcmp(update, "linear-grow-new") == 0) {
1179 unsigned int max = __le32_to_cpu(sb->max_dev);
1181 for (i=0 ; i < max ; i++)
1182 if (__le16_to_cpu(sb->dev_roles[i]) >= MD_DISK_ROLE_FAULTY)
1184 sb->dev_number = __cpu_to_le32(i);
1185 info->disk.number = i;
1186 if (max >= __le32_to_cpu(sb->max_dev))
1187 sb->max_dev = __cpu_to_le32(max+1);
1189 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1190 read(rfd, sb->device_uuid, 16) != 16) {
1191 __u32 r[4] = {random(), random(), random(), random()};
1192 memcpy(sb->device_uuid, r, 16);
1198 __cpu_to_le16(info->disk.raid_disk);
1200 fd = open(devname, O_RDONLY);
1202 unsigned long long ds;
1203 get_dev_size(fd, devname, &ds);
1206 if (__le64_to_cpu(sb->super_offset) <
1207 __le64_to_cpu(sb->data_offset)) {
1208 sb->data_size = __cpu_to_le64(
1209 ds - __le64_to_cpu(sb->data_offset));
1212 ds &= ~(unsigned long long)(4*2-1);
1213 sb->super_offset = __cpu_to_le64(ds);
1214 sb->data_size = __cpu_to_le64(
1215 ds - __le64_to_cpu(sb->data_offset));
1218 } else if (strcmp(update, "linear-grow-update") == 0) {
1219 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
1220 sb->dev_roles[info->disk.number] =
1221 __cpu_to_le16(info->disk.raid_disk);
1222 } else if (strcmp(update, "resync") == 0) {
1223 /* make sure resync happens */
1224 sb->resync_offset = 0ULL;
1225 } else if (strcmp(update, "uuid") == 0) {
1226 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
1228 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1229 struct bitmap_super_s *bm;
1230 bm = (struct bitmap_super_s*)(st->sb+MAX_SB_SIZE);
1231 memcpy(bm->uuid, sb->set_uuid, 16);
1233 } else if (strcmp(update, "no-bitmap") == 0) {
1234 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1235 } else if (strcmp(update, "bbl") == 0) {
1236 /* only possible if there is room after the bitmap, or if
1237 * there is no bitmap
1239 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
1240 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
1241 long bitmap_offset = (long)(int32_t)__le32_to_cpu(sb->bitmap_offset);
1242 long bm_sectors = 0;
1246 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1247 struct bitmap_super_s *bsb;
1248 bsb = (struct bitmap_super_s *)(((char*)sb)+MAX_SB_SIZE);
1249 bm_sectors = bitmap_sectors(bsb);
1252 if (sb_offset < data_offset) {
1253 /* 1.1 or 1.2. Put bbl after bitmap leaving at least 32K
1256 bb_offset = sb_offset + 8;
1257 if (bm_sectors && bitmap_offset > 0)
1258 bb_offset = bitmap_offset + bm_sectors;
1259 while (bb_offset < (long)sb_offset + 8 + 32*2
1260 && bb_offset + 8+8 <= (long)data_offset)
1261 /* too close to bitmap, and room to grow */
1263 if (bb_offset + 8 <= (long)data_offset) {
1264 sb->bblog_size = __cpu_to_le16(8);
1265 sb->bblog_offset = __cpu_to_le32(bb_offset);
1268 /* 1.0 - Put bbl just before super block */
1269 if (bm_sectors && bitmap_offset < 0)
1270 space = -bitmap_offset - bm_sectors;
1272 space = sb_offset - data_offset -
1273 __le64_to_cpu(sb->data_size);
1275 sb->bblog_size = __cpu_to_le16(8);
1276 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1279 } else if (strcmp(update, "no-bbl") == 0) {
1280 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
1281 pr_err("Cannot remove active bbl from %s\n",devname);
1284 sb->bblog_shift = 0;
1285 sb->bblog_offset = 0;
1287 } else if (strcmp(update, "force-no-bbl") == 0) {
1288 sb->feature_map &= ~ __cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1290 sb->bblog_shift = 0;
1291 sb->bblog_offset = 0;
1292 } else if (strcmp(update, "name") == 0) {
1293 if (info->name[0] == 0)
1294 sprintf(info->name, "%d", info->array.md_minor);
1295 memset(sb->set_name, 0, sizeof(sb->set_name));
1297 strchr(info->name, ':') == NULL &&
1298 strlen(homehost)+1+strlen(info->name) < 32) {
1299 strcpy(sb->set_name, homehost);
1300 strcat(sb->set_name, ":");
1301 strcat(sb->set_name, info->name);
1303 strcpy(sb->set_name, info->name);
1304 } else if (strcmp(update, "devicesize") == 0 &&
1305 __le64_to_cpu(sb->super_offset) <
1306 __le64_to_cpu(sb->data_offset)) {
1307 /* set data_size to device size less data_offset */
1308 struct misc_dev_info *misc = (struct misc_dev_info*)
1309 (st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
1310 sb->data_size = __cpu_to_le64(
1311 misc->device_size - __le64_to_cpu(sb->data_offset));
1312 } else if (strncmp(update, "revert-reshape", 14) == 0) {
1314 if (!(sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)))
1315 pr_err("No active reshape to revert on %s\n",
1319 unsigned long long reshape_sectors;
1322 /* If the reshape hasn't started, just stop it.
1323 * It is conceivable that a stripe was modified but
1324 * the metadata not updated. In that case the backup
1325 * should have been used to get passed the critical stage.
1326 * If that couldn't happen, the "-nobackup" version
1329 if (strcmp(update, "revert-reshape-nobackup") == 0 &&
1330 sb->reshape_position == 0 &&
1331 (__le32_to_cpu(sb->delta_disks) > 0 ||
1332 (__le32_to_cpu(sb->delta_disks) == 0 &&
1333 !(sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS))))) {
1334 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1335 sb->raid_disks = __cpu_to_le32(__le32_to_cpu(sb->raid_disks) -
1336 __le32_to_cpu(sb->delta_disks));
1337 sb->delta_disks = 0;
1340 /* reshape_position is a little messy.
1341 * Its value must be a multiple of the larger
1342 * chunk size, and of the "after" data disks.
1343 * So when reverting we need to change it to
1344 * be a multiple of the new "after" data disks,
1345 * which is the old "before".
1346 * If it isn't already a multiple of 'before',
1347 * the only thing we could do would be
1348 * copy some block around on the disks, which
1349 * is easy to get wrong.
1350 * So we reject a revert-reshape unless the
1351 * alignment is good.
1353 if (__le32_to_cpu(sb->level) >= 4 &&
1354 __le32_to_cpu(sb->level) <= 6) {
1355 reshape_sectors = __le64_to_cpu(sb->reshape_position);
1356 reshape_chunk = __le32_to_cpu(sb->new_chunk);
1357 reshape_chunk *= __le32_to_cpu(sb->raid_disks) - __le32_to_cpu(sb->delta_disks) -
1358 (__le32_to_cpu(sb->level)==6 ? 2 : 1);
1359 if (reshape_sectors % reshape_chunk) {
1360 pr_err("Reshape position is not suitably aligned.\n");
1361 pr_err("Try normal assembly and stop again\n");
1365 sb->raid_disks = __cpu_to_le32(__le32_to_cpu(sb->raid_disks) -
1366 __le32_to_cpu(sb->delta_disks));
1367 if (sb->delta_disks == 0)
1368 sb->feature_map ^= __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1370 sb->delta_disks = __cpu_to_le32(-__le32_to_cpu(sb->delta_disks));
1372 temp = sb->new_layout;
1373 sb->new_layout = sb->layout;
1376 temp = sb->new_chunk;
1377 sb->new_chunk = sb->chunksize;
1378 sb->chunksize = temp;
1380 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_NEW_OFFSET)) {
1381 long offset_delta = (int32_t)__le32_to_cpu(sb->new_offset);
1382 sb->data_offset = __cpu_to_le64(__le64_to_cpu(sb->data_offset) + offset_delta);
1383 sb->new_offset = __cpu_to_le32(-offset_delta);
1384 sb->data_size = __cpu_to_le64(__le64_to_cpu(sb->data_size) - offset_delta);
1388 } else if (strcmp(update, "_reshape_progress")==0)
1389 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1390 else if (strcmp(update, "writemostly")==0)
1391 sb->devflags |= WriteMostly1;
1392 else if (strcmp(update, "readwrite")==0)
1393 sb->devflags &= ~WriteMostly1;
1397 sb->sb_csum = calc_sb_1_csum(sb);
1398 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1399 cluster_release_dlmlock(lockid);
1404 static int init_super1(struct supertype *st, mdu_array_info_t *info,
1405 unsigned long long size, char *name, char *homehost,
1406 int *uuid, unsigned long long data_offset)
1408 struct mdp_superblock_1 *sb;
1414 if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
1415 pr_err("could not allocate superblock\n");
1418 memset(sb, 0, SUPER1_SIZE);
1422 /* zeroing superblock */
1426 spares = info->working_disks - info->active_disks;
1427 if (info->raid_disks + spares > MAX_DEVS) {
1428 pr_err("too many devices requested: %d+%d > %d\n",
1429 info->raid_disks , spares, MAX_DEVS);
1433 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
1434 sb->major_version = __cpu_to_le32(1);
1435 sb->feature_map = 0;
1439 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
1441 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1442 read(rfd, sb->set_uuid, 16) != 16) {
1443 __u32 r[4] = {random(), random(), random(), random()};
1444 memcpy(sb->set_uuid, r, 16);
1446 if (rfd >= 0) close(rfd);
1449 if (name == NULL || *name == 0) {
1450 sprintf(defname, "%d", info->md_minor);
1454 strchr(name, ':')== NULL &&
1455 strlen(homehost)+1+strlen(name) < 32) {
1456 strcpy(sb->set_name, homehost);
1457 strcat(sb->set_name, ":");
1458 strcat(sb->set_name, name);
1460 strcpy(sb->set_name, name);
1462 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
1463 sb->level = __cpu_to_le32(info->level);
1464 sb->layout = __cpu_to_le32(info->layout);
1465 sb->size = __cpu_to_le64(size*2ULL);
1466 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
1467 sb->raid_disks = __cpu_to_le32(info->raid_disks);
1469 sb->data_offset = __cpu_to_le64(data_offset);
1470 sb->data_size = __cpu_to_le64(0);
1471 sb->super_offset = __cpu_to_le64(0);
1472 sb->recovery_offset = __cpu_to_le64(0);
1474 sb->utime = sb->ctime;
1475 sb->events = __cpu_to_le64(1);
1476 if (info->state & (1<<MD_SB_CLEAN))
1477 sb->resync_offset = MaxSector;
1479 sb->resync_offset = 0;
1480 sbsize = sizeof(struct mdp_superblock_1) + 2 * (info->raid_disks + spares);
1481 sbsize = ROUND_UP(sbsize, 512);
1482 sb->max_dev = __cpu_to_le32((sbsize - sizeof(struct mdp_superblock_1)) / 2);
1484 memset(sb->dev_roles, 0xff, MAX_SB_SIZE - sizeof(struct mdp_superblock_1));
1492 long long data_offset;
1493 mdu_disk_info_t disk;
1494 struct devinfo *next;
1497 /* Add a device to the superblock being created */
1498 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
1499 int fd, char *devname, unsigned long long data_offset)
1501 struct mdp_superblock_1 *sb = st->sb;
1502 __u16 *rp = sb->dev_roles + dk->number;
1503 struct devinfo *di, **dip;
1504 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1507 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1508 rv = cluster_get_dlmlock(&lockid);
1510 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
1511 cluster_release_dlmlock(lockid);
1516 if ((dk->state & 6) == 6) /* active, sync */
1517 *rp = __cpu_to_le16(dk->raid_disk);
1518 else if (dk->state & (1<<MD_DISK_JOURNAL))
1519 *rp = MD_DISK_ROLE_JOURNAL;
1520 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
1521 *rp = MD_DISK_ROLE_SPARE;
1523 *rp = MD_DISK_ROLE_FAULTY;
1525 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
1526 __le32_to_cpu(sb->max_dev) < MAX_DEVS)
1527 sb->max_dev = __cpu_to_le32(dk->number+1);
1529 sb->dev_number = __cpu_to_le32(dk->number);
1530 sb->devflags = 0; /* don't copy another disks flags */
1531 sb->sb_csum = calc_sb_1_csum(sb);
1533 dip = (struct devinfo **)&st->info;
1535 dip = &(*dip)->next;
1536 di = xmalloc(sizeof(struct devinfo));
1538 di->devname = devname;
1540 di->data_offset = data_offset;
1544 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1545 cluster_release_dlmlock(lockid);
1551 static int locate_bitmap1(struct supertype *st, int fd, int node_num);
1553 static int store_super1(struct supertype *st, int fd)
1555 struct mdp_superblock_1 *sb = st->sb;
1556 unsigned long long sb_offset;
1557 struct align_fd afd;
1559 unsigned long long dsize;
1560 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1563 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1564 rv = cluster_get_dlmlock(&lockid);
1566 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
1567 cluster_release_dlmlock(lockid);
1572 if (!get_dev_size(fd, NULL, &dsize))
1583 * Calculate the position of the superblock.
1584 * It is always aligned to a 4K boundary and
1585 * depending on minor_version, it can be:
1586 * 0: At least 8K, but less than 12K, from end of device
1587 * 1: At start of device
1588 * 2: 4K from start of device.
1590 switch(st->minor_version) {
1594 sb_offset &= ~(4*2-1);
1606 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1607 0 != __le64_to_cpu(sb->super_offset)
1609 pr_err("internal error - sb_offset is wrong\n");
1613 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
1616 sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
1618 if (awrite(&afd, sb, sbsize) != sbsize)
1621 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1622 struct bitmap_super_s *bm = (struct bitmap_super_s*)
1623 (((char*)sb)+MAX_SB_SIZE);
1624 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
1625 locate_bitmap1(st, fd, 0);
1626 if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
1631 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1632 cluster_release_dlmlock(lockid);
1637 static int load_super1(struct supertype *st, int fd, char *devname);
1639 static unsigned long choose_bm_space(unsigned long devsize)
1641 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1642 * if bigger than 200Gig, save 128k
1643 * NOTE: result must be multiple of 4K else bad things happen
1644 * on 4K-sector devices.
1648 if (devsize - 64*2 >= 200*1024*1024*2)
1650 if (devsize - 4*2 > 8*1024*1024*2)
1655 static void free_super1(struct supertype *st);
1657 #define META_BLOCK_SIZE 4096
1658 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
1661 static int write_empty_r5l_meta_block(struct supertype *st, int fd)
1663 struct r5l_meta_block *mb;
1664 struct mdp_superblock_1 *sb = st->sb;
1665 struct align_fd afd;
1670 if (posix_memalign((void**)&mb, 4096, META_BLOCK_SIZE) != 0) {
1671 pr_err("Could not allocate memory for the meta block.\n");
1675 memset(mb, 0, META_BLOCK_SIZE);
1677 mb->magic = __cpu_to_le32(R5LOG_MAGIC);
1678 mb->version = R5LOG_VERSION;
1679 mb->meta_size = __cpu_to_le32(sizeof(struct r5l_meta_block));
1680 mb->seq = __cpu_to_le64(random32());
1681 mb->position = __cpu_to_le64(0);
1683 crc = crc32c_le(0xffffffff, sb->set_uuid, sizeof(sb->set_uuid));
1684 crc = crc32c_le(crc, (void *)mb, META_BLOCK_SIZE);
1687 if (lseek64(fd, (sb->data_offset) * 512, 0) < 0LL) {
1688 pr_err("cannot seek to offset of the meta block\n");
1692 if (awrite(&afd, mb, META_BLOCK_SIZE) != META_BLOCK_SIZE) {
1693 pr_err("failed to store write the meta block \n");
1706 static int write_init_super1(struct supertype *st)
1708 struct mdp_superblock_1 *sb = st->sb;
1709 struct supertype *refst;
1712 unsigned long long bm_space;
1714 unsigned long long dsize, array_size;
1715 unsigned long long sb_offset;
1716 unsigned long long data_offset;
1718 for (di = st->info; di; di = di->next) {
1719 if (di->disk.state & (1 << MD_DISK_JOURNAL))
1720 sb->feature_map |= MD_FEATURE_JOURNAL;
1723 for (di = st->info; di; di = di->next) {
1724 if (di->disk.state & (1 << MD_DISK_FAULTY))
1729 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
1732 sb->dev_number = __cpu_to_le32(di->disk.number);
1733 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1734 sb->devflags |= WriteMostly1;
1736 sb->devflags &= ~WriteMostly1;
1738 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1739 read(rfd, sb->device_uuid, 16) != 16) {
1740 __u32 r[4] = {random(), random(), random(), random()};
1741 memcpy(sb->device_uuid, r, 16);
1746 if (!(di->disk.state & (1<<MD_DISK_JOURNAL)))
1749 refst = dup_super(st);
1750 if (load_super1(refst, di->fd, NULL)==0) {
1751 struct mdp_superblock_1 *refsb = refst->sb;
1753 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1754 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1755 /* same array, so preserve events and
1757 sb->events = refsb->events;
1758 /* bugs in 2.6.17 and earlier mean the
1759 * dev_number chosen in Manage must be preserved
1761 if (get_linux_version() >= 2006018)
1762 sb->dev_number = refsb->dev_number;
1768 if (!get_dev_size(di->fd, NULL, &dsize)) {
1781 * Calculate the position of the superblock.
1782 * It is always aligned to a 4K boundary and
1783 * depending on minor_version, it can be:
1784 * 0: At least 8K, but less than 12K, from end of device
1785 * 1: At start of device
1786 * 2: 4K from start of device.
1787 * data_offset has already been set.
1789 array_size = __le64_to_cpu(sb->size);
1790 /* work out how much space we left for a bitmap,
1791 * Add 8 sectors for bad block log */
1792 bm_space = choose_bm_space(array_size) + 8;
1794 data_offset = di->data_offset;
1795 if (data_offset == INVALID_SECTORS)
1796 data_offset = st->data_offset;
1797 switch(st->minor_version) {
1799 if (data_offset == INVALID_SECTORS)
1803 sb_offset &= ~(4*2-1);
1804 sb->data_offset = __cpu_to_le64(data_offset);
1805 sb->super_offset = __cpu_to_le64(sb_offset);
1806 if (sb_offset < array_size + bm_space)
1807 bm_space = sb_offset - array_size;
1808 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1809 if (bm_space >= 8) {
1810 sb->bblog_size = __cpu_to_le16(8);
1811 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1815 sb->super_offset = __cpu_to_le64(0);
1816 if (data_offset == INVALID_SECTORS)
1819 sb->data_offset = __cpu_to_le64(data_offset);
1820 sb->data_size = __cpu_to_le64(dsize - data_offset);
1821 if (data_offset >= 8 + 32*2 + 8) {
1822 sb->bblog_size = __cpu_to_le16(8);
1823 sb->bblog_offset = __cpu_to_le32(8 + 32*2);
1824 } else if (data_offset >= 16) {
1825 sb->bblog_size = __cpu_to_le16(8);
1826 sb->bblog_offset = __cpu_to_le32(data_offset-8);
1831 sb->super_offset = __cpu_to_le64(sb_offset);
1832 if (data_offset == INVALID_SECTORS)
1835 sb->data_offset = __cpu_to_le64(data_offset);
1836 sb->data_size = __cpu_to_le64(dsize - data_offset);
1837 if (data_offset >= 16 + 32*2 + 8) {
1838 sb->bblog_size = __cpu_to_le16(8);
1839 sb->bblog_offset = __cpu_to_le32(8 + 32*2);
1840 } else if (data_offset >= 16+16) {
1841 sb->bblog_size = __cpu_to_le16(8);
1842 /* '8' sectors for the bblog, and another '8'
1843 * because we want offset from superblock, not
1846 sb->bblog_offset = __cpu_to_le32(data_offset-8-8);
1850 pr_err("Failed to write invalid metadata format 1.%i to %s\n",
1851 st->minor_version, di->devname);
1855 /* Disable badblock log on clusters, or when explicitly requested */
1856 if (st->nodes > 0 || conf_get_create_info()->bblist == 0) {
1858 sb->bblog_offset = 0;
1861 sb->sb_csum = calc_sb_1_csum(sb);
1862 rv = store_super1(st, di->fd);
1864 if (rv == 0 && (di->disk.state & (1 << MD_DISK_JOURNAL))) {
1865 rv = write_empty_r5l_meta_block(st, di->fd);
1870 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1871 rv = st->ss->write_bitmap(st, di->fd, NodeNumUpdate);
1879 pr_err("Failed to write metadata to %s\n",
1886 static int compare_super1(struct supertype *st, struct supertype *tst)
1890 * 0 same, or first was empty, and second was copied
1891 * 1 second had wrong number
1893 * 3 wrong other info
1895 struct mdp_superblock_1 *first = st->sb;
1896 struct mdp_superblock_1 *second = tst->sb;
1898 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1900 if (second->major_version != __cpu_to_le32(1))
1904 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1905 pr_err("could not allocate superblock\n");
1908 memcpy(first, second, SUPER1_SIZE);
1912 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1915 if (first->ctime != second->ctime ||
1916 first->level != second->level ||
1917 first->layout != second->layout ||
1918 first->size != second->size ||
1919 first->chunksize != second->chunksize ||
1920 first->raid_disks != second->raid_disks)
1925 static int load_super1(struct supertype *st, int fd, char *devname)
1927 unsigned long long dsize;
1928 unsigned long long sb_offset;
1929 struct mdp_superblock_1 *super;
1931 struct bitmap_super_s *bsb;
1932 struct misc_dev_info *misc;
1933 struct align_fd afd;
1939 if (st->ss == NULL || st->minor_version == -1) {
1941 struct supertype tst;
1942 __u64 bestctime = 0;
1943 /* guess... choose latest ctime */
1944 memset(&tst, 0, sizeof(tst));
1946 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1947 switch(load_super1(&tst, fd, devname)) {
1948 case 0: super = tst.sb;
1949 if (bestvers == -1 ||
1950 bestctime < __le64_to_cpu(super->ctime)) {
1951 bestvers = tst.minor_version;
1952 bestctime = __le64_to_cpu(super->ctime);
1957 case 1: return 1; /*bad device */
1958 case 2: break; /* bad, try next */
1961 if (bestvers != -1) {
1963 tst.minor_version = bestvers;
1965 tst.max_devs = MAX_DEVS;
1966 rv = load_super1(&tst, fd, devname);
1973 if (!get_dev_size(fd, devname, &dsize))
1979 pr_err("%s is too small for md: size is %llu sectors.\n",
1985 * Calculate the position of the superblock.
1986 * It is always aligned to a 4K boundary and
1987 * depending on minor_version, it can be:
1988 * 0: At least 8K, but less than 12K, from end of device
1989 * 1: At start of device
1990 * 2: 4K from start of device.
1992 switch(st->minor_version) {
1996 sb_offset &= ~(4*2-1);
2008 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
2010 pr_err("Cannot seek to superblock on %s: %s\n",
2011 devname, strerror(errno));
2015 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
2016 pr_err("could not allocate superblock\n");
2020 memset(super, 0, SUPER1_SIZE);
2022 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
2024 pr_err("Cannot read superblock on %s\n",
2030 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
2032 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
2033 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
2038 if (__le32_to_cpu(super->major_version) != 1) {
2040 pr_err("Cannot interpret superblock on %s - version is %d\n",
2041 devname, __le32_to_cpu(super->major_version));
2045 if (__le64_to_cpu(super->super_offset) != sb_offset) {
2047 pr_err("No superblock found on %s (super_offset is wrong)\n",
2054 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
2056 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
2057 misc->device_size = dsize;
2058 if (st->data_offset == INVALID_SECTORS)
2059 st->data_offset = __le64_to_cpu(super->data_offset);
2061 /* Now check on the bitmap superblock */
2062 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
2064 /* Read the bitmap superblock and make sure it looks
2065 * valid. If it doesn't clear the bit. An --assemble --force
2066 * should get that written out.
2068 locate_bitmap1(st, fd, 0);
2069 if (aread(&afd, bsb, 512) != 512)
2072 uuid_from_super1(st, uuid);
2073 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
2074 memcmp(bsb->uuid, uuid, 16) != 0)
2079 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
2080 & ~MD_FEATURE_BITMAP_OFFSET);
2084 static struct supertype *match_metadata_desc1(char *arg)
2086 struct supertype *st = xcalloc(1, sizeof(*st));
2088 st->container_devnm[0] = 0;
2090 st->max_devs = MAX_DEVS;
2092 st->data_offset = INVALID_SECTORS;
2093 /* leading zeros can be safely ignored. --detail generates them. */
2096 if (strcmp(arg, "1.0") == 0 ||
2097 strcmp(arg, "1.00") == 0) {
2098 st->minor_version = 0;
2101 if (strcmp(arg, "1.1") == 0 ||
2102 strcmp(arg, "1.01") == 0
2104 st->minor_version = 1;
2107 if (strcmp(arg, "1.2") == 0 ||
2108 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
2109 strcmp(arg, "default") == 0 ||
2110 #endif /* DEFAULT_OLD_METADATA */
2111 strcmp(arg, "1.02") == 0) {
2112 st->minor_version = 2;
2115 if (strcmp(arg, "1") == 0 ||
2116 strcmp(arg, "default") == 0) {
2117 st->minor_version = -1;
2125 /* find available size on device with this devsize, using
2126 * superblock type st, and reserving 'reserve' sectors for
2129 static __u64 avail_size1(struct supertype *st, __u64 devsize,
2130 unsigned long long data_offset)
2132 struct mdp_superblock_1 *super = st->sb;
2139 if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
2140 /* hot-add. allow for actual size of bitmap */
2141 struct bitmap_super_s *bsb;
2142 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
2143 bmspace = bitmap_sectors(bsb);
2146 /* Allow space for bad block log */
2147 if (super->bblog_size)
2148 bbspace = __le16_to_cpu(super->bblog_size);
2150 if (st->minor_version < 0)
2151 /* not specified, so time to set default */
2152 st->minor_version = 2;
2154 if (data_offset == INVALID_SECTORS)
2155 data_offset = st->data_offset;
2157 if (data_offset != INVALID_SECTORS)
2158 switch(st->minor_version) {
2160 return devsize - data_offset - 8*2 - bbspace;
2163 return devsize - data_offset;
2170 switch(st->minor_version) {
2173 return ((devsize - 8*2 - bbspace ) & ~(4*2-1));
2175 /* at start, 4K for superblock and possible bitmap */
2176 return devsize - 4*2 - bbspace;
2178 /* 4k from start, 4K for superblock and possible bitmap */
2179 return devsize - (4+4)*2 - bbspace;
2185 add_internal_bitmap1(struct supertype *st,
2186 int *chunkp, int delay, int write_behind,
2187 unsigned long long size,
2188 int may_change, int major)
2191 * If not may_change, then this is a 'Grow' without sysfs support for
2192 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
2193 * If may_change, then this is create or a Grow with sysfs syupport,
2194 * and we can put the bitmap wherever we like.
2196 * size is in sectors, chunk is in bytes !!!
2199 unsigned long long bits;
2200 unsigned long long max_bits;
2201 unsigned long long min_chunk;
2203 long bbl_offset, bbl_size;
2204 unsigned long long chunk = *chunkp;
2208 struct mdp_superblock_1 *sb = st->sb;
2209 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
2212 if (__le64_to_cpu(sb->data_size) == 0)
2213 /* Must be creating the array, else data_size would be non-zero */
2215 switch(st->minor_version) {
2217 /* either 3K after the superblock (when hot-add),
2218 * or some amount of space before.
2221 /* We are creating array, so we *know* how much room has
2226 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
2228 room = __le64_to_cpu(sb->super_offset)
2229 - __le64_to_cpu(sb->data_offset)
2230 - __le64_to_cpu(sb->data_size);
2231 bbl_size = __le16_to_cpu(sb->bblog_size);
2234 bbl_offset = (__s32)__le32_to_cpu(sb->bblog_offset);
2235 if (bbl_size < -bbl_offset)
2236 bbl_size = -bbl_offset;
2238 if (!may_change || (room < 3*2 &&
2239 __le32_to_cpu(sb->max_dev) <= 384)) {
2244 offset = 0; /* means movable offset */
2249 case 2: /* between superblock and data */
2253 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
2255 room = __le64_to_cpu(sb->data_offset)
2256 - __le64_to_cpu(sb->super_offset);
2257 bbl_size = __le16_to_cpu(sb->bblog_size);
2259 room = __le32_to_cpu(sb->bblog_offset) + bbl_size;
2264 room -= 2; /* Leave 1K for superblock */
2268 room -= 4*2; /* leave 4K for superblock */
2278 if (chunk == UnSet && room > 128*2)
2279 /* Limit to 128K of bitmap when chunk size not requested */
2283 /* No room for a bitmap */
2286 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
2288 min_chunk = 4096; /* sub-page chunks don't work yet.. */
2289 bits = (size*512)/min_chunk +1;
2290 while (bits > max_bits) {
2294 if (chunk == UnSet) {
2295 /* For practical purpose, 64Meg is a good
2296 * default chunk size for internal bitmaps.
2299 if (chunk < 64*1024*1024)
2300 chunk = 64*1024*1024;
2301 } else if (chunk < min_chunk)
2302 return -EINVAL; /* chunk size too small */
2303 if (chunk == 0) /* rounding problem */
2307 /* start bitmap on a 4K boundary with enough space for
2310 bits = (size*512) / chunk + 1;
2311 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
2312 room *= 8; /* convert 4K blocks to sectors */
2313 offset = -room - bbl_size;
2316 sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
2318 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2319 | MD_FEATURE_BITMAP_OFFSET);
2320 memset(bms, 0, sizeof(*bms));
2321 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
2322 bms->version = __cpu_to_le32(major);
2323 uuid_from_super1(st, uuid);
2324 memcpy(bms->uuid, uuid, 16);
2325 bms->chunksize = __cpu_to_le32(chunk);
2326 bms->daemon_sleep = __cpu_to_le32(delay);
2327 bms->sync_size = __cpu_to_le64(size);
2328 bms->write_behind = __cpu_to_le32(write_behind);
2329 bms->nodes = __cpu_to_le32(st->nodes);
2331 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2332 | MD_FEATURE_BITMAP_VERSIONED);
2333 if (st->cluster_name) {
2334 len = sizeof(bms->cluster_name);
2335 strncpy((char *)bms->cluster_name, st->cluster_name, len);
2336 bms->cluster_name[len - 1] = '\0';
2343 static int locate_bitmap1(struct supertype *st, int fd, int node_num)
2345 unsigned long long offset;
2346 struct mdp_superblock_1 *sb;
2351 if (st->ss->load_super(st, fd, NULL))
2352 return -1; /* no error I hope... */
2357 if ((__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
2361 offset = __le64_to_cpu(sb->super_offset);
2362 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
2365 lseek64(fd, offset<<9, 0);
2369 static int write_bitmap1(struct supertype *st, int fd, enum bitmap_update update)
2371 struct mdp_superblock_1 *sb = st->sb;
2372 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
2375 int towrite, n, len;
2376 struct align_fd afd;
2378 unsigned long long total_bm_space, bm_space_per_node;
2382 /* update cluster name */
2383 if (st->cluster_name) {
2384 len = sizeof(bms->cluster_name);
2385 memset((char *)bms->cluster_name, 0, len);
2386 strncpy((char *)bms->cluster_name,
2387 st->cluster_name, len);
2388 bms->cluster_name[len - 1] = '\0';
2392 /* cluster md only supports superblock 1.2 now */
2393 if (st->minor_version != 2 && bms->version == BITMAP_MAJOR_CLUSTERED) {
2394 pr_err("Warning: cluster md only works with superblock 1.2\n");
2398 if (bms->version == BITMAP_MAJOR_CLUSTERED) {
2399 if (st->nodes == 1) {
2400 /* the parameter for nodes is not valid */
2401 pr_err("Warning: cluster-md at least needs two nodes\n");
2403 } else if (st->nodes == 0)
2404 /* --nodes is not specified */
2406 else if (__cpu_to_le32(st->nodes) < bms->nodes) {
2407 /* Since the nodes num is not increased, no need to check the space
2408 * is enough or not, just update bms->nodes */
2409 bms->nodes = __cpu_to_le32(st->nodes);
2413 /* no need to change bms->nodes for other bitmap types */
2415 pr_err("Warning: --nodes option is only suitable for clustered bitmap\n");
2419 /* Each node has an independent bitmap, it is necessary to calculate the
2420 * space is enough or not, first get how many bytes for the total bitmap */
2421 bm_space_per_node = calc_bitmap_size(bms, 4096);
2423 total_bm_space = 512 * (__le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset));
2424 total_bm_space = total_bm_space - 4096; /* leave another 4k for superblock */
2426 if (bm_space_per_node * st->nodes > total_bm_space) {
2427 pr_err("Warning: The max num of nodes can't exceed %llu\n",
2428 total_bm_space / bm_space_per_node);
2432 bms->nodes = __cpu_to_le32(st->nodes);
2441 locate_bitmap1(st, fd, 0);
2443 if (posix_memalign(&buf, 4096, 4096))
2447 /* Only the bitmap[0] should resync
2448 * whole device on initial assembly
2451 memset(buf, 0x00, 4096);
2453 memset(buf, 0xff, 4096);
2454 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
2456 towrite = calc_bitmap_size(bms, 4096);
2457 while (towrite > 0) {
2461 n = awrite(&afd, buf, n);
2467 memset(buf, 0x00, 4096);
2469 memset(buf, 0xff, 4096);
2476 } while (++i < __le32_to_cpu(bms->nodes));
2482 static void free_super1(struct supertype *st)
2488 struct devinfo *di = st->info;
2489 st->info = di->next;
2498 static int validate_geometry1(struct supertype *st, int level,
2499 int layout, int raiddisks,
2500 int *chunk, unsigned long long size,
2501 unsigned long long data_offset,
2502 char *subdev, unsigned long long *freesize,
2505 unsigned long long ldsize, devsize;
2507 unsigned long long headroom;
2510 if (level == LEVEL_CONTAINER) {
2512 pr_err("1.x metadata does not support containers\n");
2515 if (*chunk == UnSet)
2516 *chunk = DEFAULT_CHUNK;
2521 if (st->minor_version < 0)
2522 /* not specified, so time to set default */
2523 st->minor_version = 2;
2525 fd = open(subdev, O_RDONLY|O_EXCL, 0);
2528 pr_err("super1.x cannot open %s: %s\n",
2529 subdev, strerror(errno));
2533 if (!get_dev_size(fd, subdev, &ldsize)) {
2539 devsize = ldsize >> 9;
2545 /* creating: allow suitable space for bitmap */
2546 bmspace = choose_bm_space(devsize);
2548 if (data_offset == INVALID_SECTORS)
2549 data_offset = st->data_offset;
2550 if (data_offset == INVALID_SECTORS)
2551 switch (st->minor_version) {
2557 /* Choose data offset appropriate for this device
2558 * and use as default for whole array.
2559 * The data_offset must allow for bitmap space
2560 * and base metadata, should allow for some headroom
2561 * for reshape, and should be rounded to multiple
2563 * Headroom is limited to 128M, but aim for about 0.1%
2565 headroom = 128*1024*2;
2566 while ((headroom << 10) > devsize &&
2568 headroom / 2 >= ((unsigned)(*chunk)*2)*2))
2570 data_offset = 12*2 + bmspace + headroom;
2571 #define ONE_MEG (2*1024)
2572 if (data_offset > ONE_MEG)
2573 data_offset = (data_offset / ONE_MEG) * ONE_MEG;
2576 if (st->data_offset == INVALID_SECTORS)
2577 st->data_offset = data_offset;
2578 switch(st->minor_version) {
2579 case 0: /* metadata at end. Round down and subtract space to reserve */
2580 devsize = (devsize & ~(4ULL*2-1));
2581 /* space for metadata, bblog, bitmap */
2582 devsize -= 8*2 + 8 + bmspace;
2586 devsize -= data_offset;
2589 *freesize = devsize;
2592 #endif /* MDASSEMBLE */
2594 void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0)
2596 /* Create a v1.0 superblock based on 'info'*/
2598 struct mdp_superblock_1 *sb;
2601 unsigned long long offset;
2603 if (posix_memalign(&ret, 4096, 1024) != 0)
2606 memset(ret, 0, 1024);
2607 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
2608 sb->major_version = __cpu_to_le32(1);
2610 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
2611 sprintf(sb->set_name, "%d", sb0->md_minor);
2612 sb->ctime = __cpu_to_le32(info->array.ctime+1);
2613 sb->level = __cpu_to_le32(info->array.level);
2614 sb->layout = __cpu_to_le32(info->array.layout);
2615 sb->size = __cpu_to_le64(info->component_size);
2616 sb->chunksize = __cpu_to_le32(info->array.chunk_size/512);
2617 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
2618 if (info->array.level > 0)
2619 sb->data_size = sb->size;
2621 sb->data_size = st->ss->avail_size(st, st->devsize/512, 0);
2622 sb->resync_offset = MaxSector;
2623 sb->max_dev = __cpu_to_le32(MD_SB_DISKS);
2624 sb->dev_number = __cpu_to_le32(info->disk.number);
2625 sb->utime = __cpu_to_le64(info->array.utime);
2627 offset = st->devsize/512 - 8*2;
2629 sb->super_offset = __cpu_to_le64(offset);
2630 //*(__u64*)(st->other + 128 + 8 + 8) = __cpu_to_le64(offset);
2632 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
2633 read(rfd, sb->device_uuid, 16) != 16) {
2634 __u32 r[4] = {random(), random(), random(), random()};
2635 memcpy(sb->device_uuid, r, 16);
2640 for (i = 0; i < MD_SB_DISKS; i++) {
2641 int state = sb0->disks[i].state;
2642 sb->dev_roles[i] = MD_DISK_ROLE_SPARE;
2643 if ((state & (1<<MD_DISK_SYNC)) &&
2644 !(state & (1<<MD_DISK_FAULTY)))
2645 sb->dev_roles[i] = __cpu_to_le16(sb0->disks[i].raid_disk);
2647 sb->sb_csum = calc_sb_1_csum(sb);
2651 struct superswitch super1 = {
2653 .examine_super = examine_super1,
2654 .brief_examine_super = brief_examine_super1,
2655 .export_examine_super = export_examine_super1,
2656 .detail_super = detail_super1,
2657 .brief_detail_super = brief_detail_super1,
2658 .export_detail_super = export_detail_super1,
2659 .write_init_super = write_init_super1,
2660 .validate_geometry = validate_geometry1,
2661 .add_to_super = add_to_super1,
2662 .examine_badblocks = examine_badblocks_super1,
2663 .copy_metadata = copy_metadata1,
2665 .match_home = match_home1,
2666 .uuid_from_super = uuid_from_super1,
2667 .getinfo_super = getinfo_super1,
2668 .container_content = container_content1,
2669 .update_super = update_super1,
2670 .init_super = init_super1,
2671 .store_super = store_super1,
2672 .compare_super = compare_super1,
2673 .load_super = load_super1,
2674 .match_metadata_desc = match_metadata_desc1,
2675 .avail_size = avail_size1,
2676 .add_internal_bitmap = add_internal_bitmap1,
2677 .locate_bitmap = locate_bitmap1,
2678 .write_bitmap = write_bitmap1,
2679 .free_super = free_super1,
2680 #if __BYTE_ORDER == BIG_ENDIAN