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.
1646 if (devsize < 64*2) return 0;
1647 if (devsize - 64*2 >= 200*1024*1024*2)
1649 if (devsize - 4*2 > 8*1024*1024*2)
1654 static void free_super1(struct supertype *st);
1656 #define META_BLOCK_SIZE 4096
1657 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
1660 static int write_empty_r5l_meta_block(struct supertype *st, int fd)
1662 struct r5l_meta_block *mb;
1663 struct mdp_superblock_1 *sb = st->sb;
1664 struct align_fd afd;
1669 if (posix_memalign((void**)&mb, 4096, META_BLOCK_SIZE) != 0) {
1670 pr_err("Could not allocate memory for the meta block.\n");
1674 memset(mb, 0, META_BLOCK_SIZE);
1676 mb->magic = __cpu_to_le32(R5LOG_MAGIC);
1677 mb->version = R5LOG_VERSION;
1678 mb->meta_size = __cpu_to_le32(sizeof(struct r5l_meta_block));
1679 mb->seq = __cpu_to_le64(random32());
1680 mb->position = __cpu_to_le64(0);
1682 crc = crc32c_le(0xffffffff, sb->set_uuid, sizeof(sb->set_uuid));
1683 crc = crc32c_le(crc, (void *)mb, META_BLOCK_SIZE);
1686 if (lseek64(fd, (sb->data_offset) * 512, 0) < 0LL) {
1687 pr_err("cannot seek to offset of the meta block\n");
1691 if (awrite(&afd, mb, META_BLOCK_SIZE) != META_BLOCK_SIZE) {
1692 pr_err("failed to store write the meta block \n");
1705 static int write_init_super1(struct supertype *st)
1707 struct mdp_superblock_1 *sb = st->sb;
1708 struct supertype *refst;
1711 unsigned long long bm_space;
1713 unsigned long long dsize, array_size;
1714 unsigned long long sb_offset;
1715 unsigned long long data_offset;
1717 for (di = st->info; di; di = di->next) {
1718 if (di->disk.state & (1 << MD_DISK_JOURNAL))
1719 sb->feature_map |= MD_FEATURE_JOURNAL;
1722 for (di = st->info; di; di = di->next) {
1723 if (di->disk.state & (1 << MD_DISK_FAULTY))
1728 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
1731 sb->dev_number = __cpu_to_le32(di->disk.number);
1732 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1733 sb->devflags |= WriteMostly1;
1735 sb->devflags &= ~WriteMostly1;
1737 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1738 read(rfd, sb->device_uuid, 16) != 16) {
1739 __u32 r[4] = {random(), random(), random(), random()};
1740 memcpy(sb->device_uuid, r, 16);
1745 if (!(di->disk.state & (1<<MD_DISK_JOURNAL)))
1748 refst = dup_super(st);
1749 if (load_super1(refst, di->fd, NULL)==0) {
1750 struct mdp_superblock_1 *refsb = refst->sb;
1752 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1753 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1754 /* same array, so preserve events and
1756 sb->events = refsb->events;
1757 /* bugs in 2.6.17 and earlier mean the
1758 * dev_number chosen in Manage must be preserved
1760 if (get_linux_version() >= 2006018)
1761 sb->dev_number = refsb->dev_number;
1767 if (!get_dev_size(di->fd, NULL, &dsize)) {
1780 * Calculate the position of the superblock.
1781 * It is always aligned to a 4K boundary and
1782 * depending on minor_version, it can be:
1783 * 0: At least 8K, but less than 12K, from end of device
1784 * 1: At start of device
1785 * 2: 4K from start of device.
1786 * data_offset has already been set.
1788 array_size = __le64_to_cpu(sb->size);
1789 /* work out how much space we left for a bitmap,
1790 * Add 8 sectors for bad block log */
1791 bm_space = choose_bm_space(array_size) + 8;
1793 data_offset = di->data_offset;
1794 if (data_offset == INVALID_SECTORS)
1795 data_offset = st->data_offset;
1796 switch(st->minor_version) {
1798 if (data_offset == INVALID_SECTORS)
1802 sb_offset &= ~(4*2-1);
1803 sb->data_offset = __cpu_to_le64(data_offset);
1804 sb->super_offset = __cpu_to_le64(sb_offset);
1805 if (sb_offset < array_size + bm_space)
1806 bm_space = sb_offset - array_size;
1807 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1808 if (bm_space >= 8) {
1809 sb->bblog_size = __cpu_to_le16(8);
1810 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1814 sb->super_offset = __cpu_to_le64(0);
1815 if (data_offset == INVALID_SECTORS)
1818 sb->data_offset = __cpu_to_le64(data_offset);
1819 sb->data_size = __cpu_to_le64(dsize - data_offset);
1820 if (data_offset >= 8 + 32*2 + 8) {
1821 sb->bblog_size = __cpu_to_le16(8);
1822 sb->bblog_offset = __cpu_to_le32(8 + 32*2);
1823 } else if (data_offset >= 16) {
1824 sb->bblog_size = __cpu_to_le16(8);
1825 sb->bblog_offset = __cpu_to_le32(data_offset-8);
1830 sb->super_offset = __cpu_to_le64(sb_offset);
1831 if (data_offset == INVALID_SECTORS)
1834 sb->data_offset = __cpu_to_le64(data_offset);
1835 sb->data_size = __cpu_to_le64(dsize - data_offset);
1836 if (data_offset >= 16 + 32*2 + 8) {
1837 sb->bblog_size = __cpu_to_le16(8);
1838 sb->bblog_offset = __cpu_to_le32(8 + 32*2);
1839 } else if (data_offset >= 16+16) {
1840 sb->bblog_size = __cpu_to_le16(8);
1841 /* '8' sectors for the bblog, and another '8'
1842 * because we want offset from superblock, not
1845 sb->bblog_offset = __cpu_to_le32(data_offset-8-8);
1849 pr_err("Failed to write invalid metadata format 1.%i to %s\n",
1850 st->minor_version, di->devname);
1854 /* Disable badblock log on clusters, or when explicitly requested */
1855 if (st->nodes > 0 || conf_get_create_info()->bblist == 0) {
1857 sb->bblog_offset = 0;
1860 sb->sb_csum = calc_sb_1_csum(sb);
1861 rv = store_super1(st, di->fd);
1863 if (rv == 0 && (di->disk.state & (1 << MD_DISK_JOURNAL))) {
1864 rv = write_empty_r5l_meta_block(st, di->fd);
1869 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1870 rv = st->ss->write_bitmap(st, di->fd, NodeNumUpdate);
1878 pr_err("Failed to write metadata to %s\n",
1885 static int compare_super1(struct supertype *st, struct supertype *tst)
1889 * 0 same, or first was empty, and second was copied
1890 * 1 second had wrong number
1892 * 3 wrong other info
1894 struct mdp_superblock_1 *first = st->sb;
1895 struct mdp_superblock_1 *second = tst->sb;
1897 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1899 if (second->major_version != __cpu_to_le32(1))
1903 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1904 pr_err("could not allocate superblock\n");
1907 memcpy(first, second, SUPER1_SIZE);
1911 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1914 if (first->ctime != second->ctime ||
1915 first->level != second->level ||
1916 first->layout != second->layout ||
1917 first->size != second->size ||
1918 first->chunksize != second->chunksize ||
1919 first->raid_disks != second->raid_disks)
1924 static int load_super1(struct supertype *st, int fd, char *devname)
1926 unsigned long long dsize;
1927 unsigned long long sb_offset;
1928 struct mdp_superblock_1 *super;
1930 struct bitmap_super_s *bsb;
1931 struct misc_dev_info *misc;
1932 struct align_fd afd;
1938 if (st->ss == NULL || st->minor_version == -1) {
1940 struct supertype tst;
1941 __u64 bestctime = 0;
1942 /* guess... choose latest ctime */
1943 memset(&tst, 0, sizeof(tst));
1945 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1946 switch(load_super1(&tst, fd, devname)) {
1947 case 0: super = tst.sb;
1948 if (bestvers == -1 ||
1949 bestctime < __le64_to_cpu(super->ctime)) {
1950 bestvers = tst.minor_version;
1951 bestctime = __le64_to_cpu(super->ctime);
1956 case 1: return 1; /*bad device */
1957 case 2: break; /* bad, try next */
1960 if (bestvers != -1) {
1962 tst.minor_version = bestvers;
1964 tst.max_devs = MAX_DEVS;
1965 rv = load_super1(&tst, fd, devname);
1972 if (!get_dev_size(fd, devname, &dsize))
1978 pr_err("%s is too small for md: size is %llu sectors.\n",
1984 * Calculate the position of the superblock.
1985 * It is always aligned to a 4K boundary and
1986 * depending on minor_version, it can be:
1987 * 0: At least 8K, but less than 12K, from end of device
1988 * 1: At start of device
1989 * 2: 4K from start of device.
1991 switch(st->minor_version) {
1995 sb_offset &= ~(4*2-1);
2007 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
2009 pr_err("Cannot seek to superblock on %s: %s\n",
2010 devname, strerror(errno));
2014 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
2015 pr_err("could not allocate superblock\n");
2019 memset(super, 0, SUPER1_SIZE);
2021 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
2023 pr_err("Cannot read superblock on %s\n",
2029 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
2031 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
2032 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
2037 if (__le32_to_cpu(super->major_version) != 1) {
2039 pr_err("Cannot interpret superblock on %s - version is %d\n",
2040 devname, __le32_to_cpu(super->major_version));
2044 if (__le64_to_cpu(super->super_offset) != sb_offset) {
2046 pr_err("No superblock found on %s (super_offset is wrong)\n",
2053 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
2055 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
2056 misc->device_size = dsize;
2057 if (st->data_offset == INVALID_SECTORS)
2058 st->data_offset = __le64_to_cpu(super->data_offset);
2060 /* Now check on the bitmap superblock */
2061 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
2063 /* Read the bitmap superblock and make sure it looks
2064 * valid. If it doesn't clear the bit. An --assemble --force
2065 * should get that written out.
2067 locate_bitmap1(st, fd, 0);
2068 if (aread(&afd, bsb, 512) != 512)
2071 uuid_from_super1(st, uuid);
2072 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
2073 memcmp(bsb->uuid, uuid, 16) != 0)
2078 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
2079 & ~MD_FEATURE_BITMAP_OFFSET);
2083 static struct supertype *match_metadata_desc1(char *arg)
2085 struct supertype *st = xcalloc(1, sizeof(*st));
2087 st->container_devnm[0] = 0;
2089 st->max_devs = MAX_DEVS;
2091 st->data_offset = INVALID_SECTORS;
2092 /* leading zeros can be safely ignored. --detail generates them. */
2095 if (strcmp(arg, "1.0") == 0 ||
2096 strcmp(arg, "1.00") == 0) {
2097 st->minor_version = 0;
2100 if (strcmp(arg, "1.1") == 0 ||
2101 strcmp(arg, "1.01") == 0
2103 st->minor_version = 1;
2106 if (strcmp(arg, "1.2") == 0 ||
2107 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
2108 strcmp(arg, "default") == 0 ||
2109 #endif /* DEFAULT_OLD_METADATA */
2110 strcmp(arg, "1.02") == 0) {
2111 st->minor_version = 2;
2114 if (strcmp(arg, "1") == 0 ||
2115 strcmp(arg, "default") == 0) {
2116 st->minor_version = -1;
2124 /* find available size on device with this devsize, using
2125 * superblock type st, and reserving 'reserve' sectors for
2128 static __u64 avail_size1(struct supertype *st, __u64 devsize,
2129 unsigned long long data_offset)
2131 struct mdp_superblock_1 *super = st->sb;
2138 if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
2139 /* hot-add. allow for actual size of bitmap */
2140 struct bitmap_super_s *bsb;
2141 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
2142 bmspace = bitmap_sectors(bsb);
2145 /* Allow space for bad block log */
2146 if (super->bblog_size)
2147 bbspace = __le16_to_cpu(super->bblog_size);
2149 if (st->minor_version < 0)
2150 /* not specified, so time to set default */
2151 st->minor_version = 2;
2153 if (data_offset == INVALID_SECTORS)
2154 data_offset = st->data_offset;
2156 if (data_offset != INVALID_SECTORS)
2157 switch(st->minor_version) {
2159 return devsize - data_offset - 8*2 - bbspace;
2162 return devsize - data_offset;
2169 switch(st->minor_version) {
2172 return ((devsize - 8*2 - bbspace ) & ~(4*2-1));
2174 /* at start, 4K for superblock and possible bitmap */
2175 return devsize - 4*2 - bbspace;
2177 /* 4k from start, 4K for superblock and possible bitmap */
2178 return devsize - (4+4)*2 - bbspace;
2184 add_internal_bitmap1(struct supertype *st,
2185 int *chunkp, int delay, int write_behind,
2186 unsigned long long size,
2187 int may_change, int major)
2190 * If not may_change, then this is a 'Grow' without sysfs support for
2191 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
2192 * If may_change, then this is create or a Grow with sysfs syupport,
2193 * and we can put the bitmap wherever we like.
2195 * size is in sectors, chunk is in bytes !!!
2198 unsigned long long bits;
2199 unsigned long long max_bits;
2200 unsigned long long min_chunk;
2202 long bbl_offset, bbl_size;
2203 unsigned long long chunk = *chunkp;
2207 struct mdp_superblock_1 *sb = st->sb;
2208 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
2211 if (__le64_to_cpu(sb->data_size) == 0)
2212 /* Must be creating the array, else data_size would be non-zero */
2214 switch(st->minor_version) {
2216 /* either 3K after the superblock (when hot-add),
2217 * or some amount of space before.
2220 /* We are creating array, so we *know* how much room has
2225 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
2227 room = __le64_to_cpu(sb->super_offset)
2228 - __le64_to_cpu(sb->data_offset)
2229 - __le64_to_cpu(sb->data_size);
2230 bbl_size = __le16_to_cpu(sb->bblog_size);
2233 bbl_offset = (__s32)__le32_to_cpu(sb->bblog_offset);
2234 if (bbl_size < -bbl_offset)
2235 bbl_size = -bbl_offset;
2237 if (!may_change || (room < 3*2 &&
2238 __le32_to_cpu(sb->max_dev) <= 384)) {
2243 offset = 0; /* means movable offset */
2248 case 2: /* between superblock and data */
2252 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
2254 room = __le64_to_cpu(sb->data_offset)
2255 - __le64_to_cpu(sb->super_offset);
2256 bbl_size = __le16_to_cpu(sb->bblog_size);
2258 room = __le32_to_cpu(sb->bblog_offset) + bbl_size;
2263 room -= 2; /* Leave 1K for superblock */
2267 room -= 4*2; /* leave 4K for superblock */
2277 if (chunk == UnSet && room > 128*2)
2278 /* Limit to 128K of bitmap when chunk size not requested */
2282 /* No room for a bitmap */
2285 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
2287 min_chunk = 4096; /* sub-page chunks don't work yet.. */
2288 bits = (size*512)/min_chunk +1;
2289 while (bits > max_bits) {
2293 if (chunk == UnSet) {
2294 /* For practical purpose, 64Meg is a good
2295 * default chunk size for internal bitmaps.
2298 if (chunk < 64*1024*1024)
2299 chunk = 64*1024*1024;
2300 } else if (chunk < min_chunk)
2301 return 0; /* chunk size too small */
2302 if (chunk == 0) /* rounding problem */
2306 /* start bitmap on a 4K boundary with enough space for
2309 bits = (size*512) / chunk + 1;
2310 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
2311 room *= 8; /* convert 4K blocks to sectors */
2312 offset = -room - bbl_size;
2315 sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
2317 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2318 | MD_FEATURE_BITMAP_OFFSET);
2319 memset(bms, 0, sizeof(*bms));
2320 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
2321 bms->version = __cpu_to_le32(major);
2322 uuid_from_super1(st, uuid);
2323 memcpy(bms->uuid, uuid, 16);
2324 bms->chunksize = __cpu_to_le32(chunk);
2325 bms->daemon_sleep = __cpu_to_le32(delay);
2326 bms->sync_size = __cpu_to_le64(size);
2327 bms->write_behind = __cpu_to_le32(write_behind);
2328 bms->nodes = __cpu_to_le32(st->nodes);
2330 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2331 | MD_FEATURE_BITMAP_VERSIONED);
2332 if (st->cluster_name) {
2333 len = sizeof(bms->cluster_name);
2334 strncpy((char *)bms->cluster_name, st->cluster_name, len);
2335 bms->cluster_name[len - 1] = '\0';
2342 static int locate_bitmap1(struct supertype *st, int fd, int node_num)
2344 unsigned long long offset;
2345 struct mdp_superblock_1 *sb;
2350 if (st->ss->load_super(st, fd, NULL))
2351 return -1; /* no error I hope... */
2356 if ((__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
2360 offset = __le64_to_cpu(sb->super_offset);
2361 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
2364 lseek64(fd, offset<<9, 0);
2368 static int write_bitmap1(struct supertype *st, int fd, enum bitmap_update update)
2370 struct mdp_superblock_1 *sb = st->sb;
2371 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
2374 int towrite, n, len;
2375 struct align_fd afd;
2377 unsigned long long total_bm_space, bm_space_per_node;
2381 /* update cluster name */
2382 if (st->cluster_name) {
2383 len = sizeof(bms->cluster_name);
2384 memset((char *)bms->cluster_name, 0, len);
2385 strncpy((char *)bms->cluster_name,
2386 st->cluster_name, len);
2387 bms->cluster_name[len - 1] = '\0';
2391 /* cluster md only supports superblock 1.2 now */
2392 if (st->minor_version != 2 && bms->version == BITMAP_MAJOR_CLUSTERED) {
2393 pr_err("Warning: cluster md only works with superblock 1.2\n");
2397 if (bms->version == BITMAP_MAJOR_CLUSTERED && st->nodes <= 1) {
2398 pr_err("Warning: cluster-md at least needs two nodes\n");
2402 /* Each node has an independent bitmap, it is necessary to calculate the
2403 * space is enough or not, first get how many bytes for the total bitmap */
2404 bm_space_per_node = calc_bitmap_size(bms, 4096);
2406 total_bm_space = 512 * (__le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset));
2407 total_bm_space = total_bm_space - 4096; /* leave another 4k for superblock */
2409 if (bm_space_per_node * st->nodes > total_bm_space) {
2410 pr_err("Warning: The max num of nodes can't exceed %llu\n",
2411 total_bm_space / bm_space_per_node);
2415 bms->nodes = __cpu_to_le32(st->nodes);
2424 locate_bitmap1(st, fd, 0);
2426 if (posix_memalign(&buf, 4096, 4096))
2430 /* Only the bitmap[0] should resync
2431 * whole device on initial assembly
2434 memset(buf, 0x00, 4096);
2436 memset(buf, 0xff, 4096);
2437 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
2439 towrite = calc_bitmap_size(bms, 4096);
2440 while (towrite > 0) {
2444 n = awrite(&afd, buf, n);
2450 memset(buf, 0x00, 4096);
2452 memset(buf, 0xff, 4096);
2459 } while (++i < __le32_to_cpu(bms->nodes));
2465 static void free_super1(struct supertype *st)
2471 struct devinfo *di = st->info;
2472 st->info = di->next;
2481 static int validate_geometry1(struct supertype *st, int level,
2482 int layout, int raiddisks,
2483 int *chunk, unsigned long long size,
2484 unsigned long long data_offset,
2485 char *subdev, unsigned long long *freesize,
2488 unsigned long long ldsize, devsize;
2490 unsigned long long headroom;
2493 if (level == LEVEL_CONTAINER) {
2495 pr_err("1.x metadata does not support containers\n");
2498 if (*chunk == UnSet)
2499 *chunk = DEFAULT_CHUNK;
2504 if (st->minor_version < 0)
2505 /* not specified, so time to set default */
2506 st->minor_version = 2;
2508 fd = open(subdev, O_RDONLY|O_EXCL, 0);
2511 pr_err("super1.x cannot open %s: %s\n",
2512 subdev, strerror(errno));
2516 if (!get_dev_size(fd, subdev, &ldsize)) {
2522 devsize = ldsize >> 9;
2528 /* creating: allow suitable space for bitmap */
2529 bmspace = choose_bm_space(devsize);
2531 if (data_offset == INVALID_SECTORS)
2532 data_offset = st->data_offset;
2533 if (data_offset == INVALID_SECTORS)
2534 switch (st->minor_version) {
2540 /* Choose data offset appropriate for this device
2541 * and use as default for whole array.
2542 * The data_offset must allow for bitmap space
2543 * and base metadata, should allow for some headroom
2544 * for reshape, and should be rounded to multiple
2546 * Headroom is limited to 128M, but aim for about 0.1%
2548 headroom = 128*1024*2;
2549 while ((headroom << 10) > devsize &&
2551 headroom / 2 >= ((unsigned)(*chunk)*2)*2))
2553 data_offset = 12*2 + bmspace + headroom;
2554 #define ONE_MEG (2*1024)
2555 if (data_offset > ONE_MEG)
2556 data_offset = (data_offset / ONE_MEG) * ONE_MEG;
2559 if (st->data_offset == INVALID_SECTORS)
2560 st->data_offset = data_offset;
2561 switch(st->minor_version) {
2562 case 0: /* metadata at end. Round down and subtract space to reserve */
2563 devsize = (devsize & ~(4ULL*2-1));
2564 /* space for metadata, bblog, bitmap */
2565 devsize -= 8*2 + 8 + bmspace;
2569 devsize -= data_offset;
2572 *freesize = devsize;
2575 #endif /* MDASSEMBLE */
2577 void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0)
2579 /* Create a v1.0 superblock based on 'info'*/
2581 struct mdp_superblock_1 *sb;
2584 unsigned long long offset;
2586 if (posix_memalign(&ret, 4096, 1024) != 0)
2589 memset(ret, 0, 1024);
2590 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
2591 sb->major_version = __cpu_to_le32(1);
2593 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
2594 sprintf(sb->set_name, "%d", sb0->md_minor);
2595 sb->ctime = __cpu_to_le32(info->array.ctime+1);
2596 sb->level = __cpu_to_le32(info->array.level);
2597 sb->layout = __cpu_to_le32(info->array.layout);
2598 sb->size = __cpu_to_le64(info->component_size);
2599 sb->chunksize = __cpu_to_le32(info->array.chunk_size/512);
2600 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
2601 if (info->array.level > 0)
2602 sb->data_size = sb->size;
2604 sb->data_size = st->ss->avail_size(st, st->devsize/512, 0);
2605 sb->resync_offset = MaxSector;
2606 sb->max_dev = __cpu_to_le32(MD_SB_DISKS);
2607 sb->dev_number = __cpu_to_le32(info->disk.number);
2608 sb->utime = __cpu_to_le64(info->array.utime);
2610 offset = st->devsize/512 - 8*2;
2612 sb->super_offset = __cpu_to_le64(offset);
2613 //*(__u64*)(st->other + 128 + 8 + 8) = __cpu_to_le64(offset);
2615 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
2616 read(rfd, sb->device_uuid, 16) != 16) {
2617 __u32 r[4] = {random(), random(), random(), random()};
2618 memcpy(sb->device_uuid, r, 16);
2623 for (i = 0; i < MD_SB_DISKS; i++) {
2624 int state = sb0->disks[i].state;
2625 sb->dev_roles[i] = MD_DISK_ROLE_SPARE;
2626 if ((state & (1<<MD_DISK_SYNC)) &&
2627 !(state & (1<<MD_DISK_FAULTY)))
2628 sb->dev_roles[i] = __cpu_to_le16(sb0->disks[i].raid_disk);
2630 sb->sb_csum = calc_sb_1_csum(sb);
2634 struct superswitch super1 = {
2636 .examine_super = examine_super1,
2637 .brief_examine_super = brief_examine_super1,
2638 .export_examine_super = export_examine_super1,
2639 .detail_super = detail_super1,
2640 .brief_detail_super = brief_detail_super1,
2641 .export_detail_super = export_detail_super1,
2642 .write_init_super = write_init_super1,
2643 .validate_geometry = validate_geometry1,
2644 .add_to_super = add_to_super1,
2645 .examine_badblocks = examine_badblocks_super1,
2646 .copy_metadata = copy_metadata1,
2648 .match_home = match_home1,
2649 .uuid_from_super = uuid_from_super1,
2650 .getinfo_super = getinfo_super1,
2651 .container_content = container_content1,
2652 .update_super = update_super1,
2653 .init_super = init_super1,
2654 .store_super = store_super1,
2655 .compare_super = compare_super1,
2656 .load_super = load_super1,
2657 .match_metadata_desc = match_metadata_desc1,
2658 .avail_size = avail_size1,
2659 .add_internal_bitmap = add_internal_bitmap1,
2660 .locate_bitmap = locate_bitmap1,
2661 .write_bitmap = write_bitmap1,
2662 .free_super = free_super1,
2663 #if __BYTE_ORDER == BIG_ENDIAN