2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
27 * The version-1 superblock :
28 * All numeric fields are little-endian.
30 * total size: 256 bytes plus 2 per device.
31 * 1K allows 384 devices.
33 struct mdp_superblock_1 {
34 /* constant array information - 128 bytes */
35 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
36 __u32 major_version; /* 1 */
37 __u32 feature_map; /* 0 for now */
38 __u32 pad0; /* always set to 0 when writing */
40 __u8 set_uuid[16]; /* user-space generated. */
41 char set_name[32]; /* set and interpreted by user-space */
43 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
44 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
45 __u32 layout; /* only for raid5 currently */
46 __u64 size; /* used size of component devices, in 512byte sectors */
48 __u32 chunksize; /* in 512byte sectors */
50 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
51 * NOTE: signed, so bitmap can be before superblock
52 * only meaningful of feature_map[0] is set.
55 /* These are only valid with feature bit '4' */
56 __u32 new_level; /* new level we are reshaping to */
57 __u64 reshape_position; /* next address in array-space for reshape */
58 __u32 delta_disks; /* change in number of raid_disks */
59 __u32 new_layout; /* new layout */
60 __u32 new_chunk; /* new chunk size (bytes) */
61 __u8 pad1[128-124]; /* set to 0 when written */
63 /* constant this-device information - 64 bytes */
64 __u64 data_offset; /* sector start of data, often 0 */
65 __u64 data_size; /* sectors in this device that can be used for data */
66 __u64 super_offset; /* sector start of this superblock */
67 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
68 __u32 dev_number; /* permanent identifier of this device - not role in raid */
69 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
70 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
71 __u8 devflags; /* per-device flags. Only one defined...*/
72 #define WriteMostly1 1 /* mask for writemostly flag in above */
73 /* bad block log. If there are any bad blocks the feature flag is set.
74 * if offset and size are non-zero, that space is reserved and available.
76 __u8 bblog_shift; /* shift from sectors to block size for badblocklist */
77 __u16 bblog_size; /* number of sectors reserved for badblocklist */
78 __u32 bblog_offset; /* sector offset from superblock to bblog, signed */
80 /* array state information - 64 bytes */
81 __u64 utime; /* 40 bits second, 24 btes microseconds */
82 __u64 events; /* incremented when superblock updated */
83 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
84 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
85 __u32 max_dev; /* size of dev_roles[] array to consider */
86 __u8 pad3[64-32]; /* set to 0 when writing */
88 /* device state information. Indexed by dev_number.
90 * Note there are no per-device state flags. State information is rolled
91 * into the 'roles' value. If a device is spare or faulty, then it doesn't
92 * have a meaningful role.
94 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
97 struct misc_dev_info {
101 /* feature_map bits */
102 #define MD_FEATURE_BITMAP_OFFSET 1
103 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
106 #define MD_FEATURE_RESHAPE_ACTIVE 4
107 #define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
109 #define MD_FEATURE_ALL (1|2|4|8)
112 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
114 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
116 unsigned int disk_csum, csum;
117 unsigned long long newcsum;
118 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
119 unsigned int *isuper = (unsigned int*)sb;
121 /* make sure I can count... */
122 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
123 offsetof(struct mdp_superblock_1, utime) != 192 ||
124 sizeof(struct mdp_superblock_1) != 256) {
125 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
128 disk_csum = sb->sb_csum;
131 for (; size>=4; size -= 4 ) {
132 newcsum += __le32_to_cpu(*isuper);
137 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
139 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
140 sb->sb_csum = disk_csum;
141 return __cpu_to_le32(csum);
144 static char abuf[4096+4096];
145 static int aread(int fd, void *buf, int len)
148 * On devices with a 4K sector size, we need to read
149 * the full sector and copy relevant bits into
155 if (ioctl(fd, BLKSSZGET, &bsize) != 0 ||
157 return read(fd, buf, len);
160 b = (char*)(((long)(abuf+4096))&~4095UL);
162 n = read(fd, b, bsize);
165 lseek(fd, len - n, 1);
172 static int awrite(int fd, void *buf, int len)
175 * On devices with a 4K sector size, we need to write
176 * the full sector. We pre-read if the sector is larger
178 * The address must be sector-aligned.
183 if (ioctl(fd, BLKSSZGET, &bsize) != 0 ||
185 return write(fd, buf, len);
188 b = (char*)(((long)(abuf+4096))&~4095UL);
190 n = read(fd, b, bsize);
195 n = write(fd, b, bsize);
198 lseek(fd, len - n, 1);
203 static void examine_super1(struct supertype *st, char *homehost)
205 struct mdp_superblock_1 *sb = st->sb;
212 int l = homehost ? strlen(homehost) : 0;
214 unsigned long long sb_offset;
216 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
217 printf(" Version : 1");
218 sb_offset = __le64_to_cpu(sb->super_offset);
221 else if (sb_offset <= 8)
225 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
226 printf(" Array UUID : ");
227 for (i=0; i<16; i++) {
228 if ((i&3)==0 && i != 0) printf(":");
229 printf("%02x", sb->set_uuid[i]);
232 printf(" Name : %.32s", sb->set_name);
233 if (l > 0 && l < 32 &&
234 sb->set_name[l] == ':' &&
235 strncmp(sb->set_name, homehost, l) == 0)
236 printf(" (local to host %s)", homehost);
238 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
239 printf(" Creation Time : %.24s\n", ctime(&atime));
240 c=map_num(pers, __le32_to_cpu(sb->level));
241 printf(" Raid Level : %s\n", c?c:"-unknown-");
242 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
244 printf(" Avail Dev Size : %llu%s\n",
245 (unsigned long long)__le64_to_cpu(sb->data_size),
246 human_size(__le64_to_cpu(sb->data_size)<<9));
247 if (__le32_to_cpu(sb->level) > 0) {
249 switch(__le32_to_cpu(sb->level)) {
250 case 1: ddsks=1;break;
252 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
253 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
255 layout = __le32_to_cpu(sb->layout);
256 ddsks = __le32_to_cpu(sb->raid_disks)
257 / (layout&255) / ((layout>>8)&255);
260 printf(" Array Size : %llu%s\n",
261 ddsks*(unsigned long long)__le64_to_cpu(sb->size),
262 human_size(ddsks*__le64_to_cpu(sb->size)<<9));
263 if (sb->size != sb->data_size)
264 printf(" Used Dev Size : %llu%s\n",
265 (unsigned long long)__le64_to_cpu(sb->size),
266 human_size(__le64_to_cpu(sb->size)<<9));
269 printf(" Data Offset : %llu sectors\n",
270 (unsigned long long)__le64_to_cpu(sb->data_offset));
271 printf(" Super Offset : %llu sectors\n",
272 (unsigned long long)__le64_to_cpu(sb->super_offset));
273 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
274 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
275 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
276 printf(" Device UUID : ");
277 for (i=0; i<16; i++) {
278 if ((i&3)==0 && i != 0) printf(":");
279 printf("%02x", sb->device_uuid[i]);
283 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
284 printf("Internal Bitmap : %ld sectors from superblock\n",
285 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
287 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)) {
288 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
289 human_size(__le64_to_cpu(sb->reshape_position)<<9));
290 if (__le32_to_cpu(sb->delta_disks)) {
291 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
292 printf(" (%d->%d)\n",
293 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
294 __le32_to_cpu(sb->raid_disks));
295 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
296 delta_extra = -__le32_to_cpu(sb->delta_disks);
298 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
299 c = map_num(pers, __le32_to_cpu(sb->new_level));
300 printf(" New Level : %s\n", c?c:"-unknown-");
302 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
303 if (__le32_to_cpu(sb->level) == 5) {
304 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
305 printf(" New Layout : %s\n", c?c:"-unknown-");
307 if (__le32_to_cpu(sb->level) == 6) {
308 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
309 printf(" New Layout : %s\n", c?c:"-unknown-");
311 if (__le32_to_cpu(sb->level) == 10) {
312 printf(" New Layout :");
313 print_r10_layout(__le32_to_cpu(sb->new_layout));
317 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
318 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
323 if (sb->devflags & WriteMostly1)
324 printf(" write-mostly");
328 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
329 printf(" Update Time : %.24s\n", ctime(&atime));
331 if (sb->bblog_size && sb->bblog_offset) {
332 printf(" Bad Block Log : %d entries available at offset %ld sectors",
333 __le16_to_cpu(sb->bblog_size)*512/8,
334 (long)__le32_to_cpu(sb->bblog_offset));
335 if (sb->feature_map &
336 __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
337 printf(" - bad blocks present.");
342 if (calc_sb_1_csum(sb) == sb->sb_csum)
343 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
345 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
346 __le32_to_cpu(calc_sb_1_csum(sb)));
347 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
349 if (__le32_to_cpu(sb->level) == 5) {
350 c = map_num(r5layout, __le32_to_cpu(sb->layout));
351 printf(" Layout : %s\n", c?c:"-unknown-");
353 if (__le32_to_cpu(sb->level) == 6) {
354 c = map_num(r6layout, __le32_to_cpu(sb->layout));
355 printf(" Layout : %s\n", c?c:"-unknown-");
357 if (__le32_to_cpu(sb->level) == 10) {
358 int lo = __le32_to_cpu(sb->layout);
360 print_r10_layout(lo);
363 switch(__le32_to_cpu(sb->level)) {
369 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
372 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
378 /* This turns out to just be confusing */
379 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
380 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
381 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
383 for (d=0; d < i; d++) {
384 int role = __le16_to_cpu(sb->dev_roles[d]);
386 if (role == 0xffff) printf("empty");
387 else if(role == 0xfffe) printf("failed");
388 else printf("%d", role);
392 printf(" Device Role : ");
393 d = __le32_to_cpu(sb->dev_number);
394 if (d < __le32_to_cpu(sb->max_dev))
395 role = __le16_to_cpu(sb->dev_roles[d]);
401 printf("Active device %d\n", role);
403 printf(" Array State : ");
404 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
407 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
408 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
412 if (cnt > 1) printf("?");
413 else if (cnt == 1) printf("A");
417 /* This is confusing too */
419 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
420 int role = __le16_to_cpu(sb->dev_roles[i]);
424 if (faulty) printf(" %d failed", faulty);
426 printf(" ('A' == active, '.' == missing)");
431 static void brief_examine_super1(struct supertype *st, int verbose)
433 struct mdp_superblock_1 *sb = st->sb;
435 unsigned long long sb_offset;
437 char *c=map_num(pers, __le32_to_cpu(sb->level));
439 nm = strchr(sb->set_name, ':');
442 else if (sb->set_name[0])
447 printf("ARRAY%s%s", nm ? " /dev/md/":"", nm);
449 printf(" level=%s", c);
450 sb_offset = __le64_to_cpu(sb->super_offset);
452 printf(" metadata=1.1 ");
453 else if (sb_offset <= 8)
454 printf(" metadata=1.2 ");
456 printf(" metadata=1.0 ");
458 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
460 for (i=0; i<16; i++) {
461 if ((i&3)==0 && i != 0) printf(":");
462 printf("%02x", sb->set_uuid[i]);
465 printf(" name=%.32s", sb->set_name);
469 static void export_examine_super1(struct supertype *st)
471 struct mdp_superblock_1 *sb = st->sb;
475 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
476 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
478 if (sb->set_name[i] == '\n' ||
479 sb->set_name[i] == '\0') {
484 printf("MD_NAME=%.*s\n", len, sb->set_name);
486 for (i=0; i<16; i++) {
487 if ((i&3)==0 && i != 0) printf(":");
488 printf("%02x", sb->set_uuid[i]);
491 printf("MD_UPDATE_TIME=%llu\n",
492 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
493 printf("MD_DEV_UUID=");
494 for (i=0; i<16; i++) {
495 if ((i&3)==0 && i != 0) printf(":");
496 printf("%02x", sb->device_uuid[i]);
499 printf("MD_EVENTS=%llu\n",
500 (unsigned long long)__le64_to_cpu(sb->events));
503 static void detail_super1(struct supertype *st, char *homehost)
505 struct mdp_superblock_1 *sb = st->sb;
507 int l = homehost ? strlen(homehost) : 0;
509 printf(" Name : %.32s", sb->set_name);
510 if (l > 0 && l < 32 &&
511 sb->set_name[l] == ':' &&
512 strncmp(sb->set_name, homehost, l) == 0)
513 printf(" (local to host %s)", homehost);
514 printf("\n UUID : ");
515 for (i=0; i<16; i++) {
516 if ((i&3)==0 && i != 0) printf(":");
517 printf("%02x", sb->set_uuid[i]);
519 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
522 static void brief_detail_super1(struct supertype *st)
524 struct mdp_superblock_1 *sb = st->sb;
528 printf(" name=%.32s", sb->set_name);
530 for (i=0; i<16; i++) {
531 if ((i&3)==0 && i != 0) printf(":");
532 printf("%02x", sb->set_uuid[i]);
536 static void export_detail_super1(struct supertype *st)
538 struct mdp_superblock_1 *sb = st->sb;
543 if (sb->set_name[i] == '\n' ||
544 sb->set_name[i] == '\0') {
549 printf("MD_NAME=%.*s\n", len, sb->set_name);
554 static int match_home1(struct supertype *st, char *homehost)
556 struct mdp_superblock_1 *sb = st->sb;
557 int l = homehost ? strlen(homehost) : 0;
559 return (l > 0 && l < 32 &&
560 sb->set_name[l] == ':' &&
561 strncmp(sb->set_name, homehost, l) == 0);
564 static void uuid_from_super1(struct supertype *st, int uuid[4])
566 struct mdp_superblock_1 *super = st->sb;
567 char *cuuid = (char*)uuid;
570 cuuid[i] = super->set_uuid[i];
573 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
575 struct mdp_superblock_1 *sb = st->sb;
579 unsigned int map_disks = info->array.raid_disks;
581 memset(info, 0, sizeof(*info));
582 info->array.major_version = 1;
583 info->array.minor_version = st->minor_version;
584 info->array.patch_version = 0;
585 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
586 info->array.level = __le32_to_cpu(sb->level);
587 info->array.layout = __le32_to_cpu(sb->layout);
588 info->array.md_minor = -1;
589 info->array.ctime = __le64_to_cpu(sb->ctime);
590 info->array.utime = __le64_to_cpu(sb->utime);
591 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
593 (__le64_to_cpu(sb->resync_offset) >= __le64_to_cpu(sb->size))
596 info->data_offset = __le64_to_cpu(sb->data_offset);
597 info->component_size = __le64_to_cpu(sb->size);
599 info->disk.major = 0;
600 info->disk.minor = 0;
601 info->disk.number = __le32_to_cpu(sb->dev_number);
602 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
603 __le32_to_cpu(sb->max_dev) > 512)
606 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
608 info->disk.raid_disk = -1;
611 info->disk.state = 0; /* spare: not active, not sync, not faulty */
614 info->disk.state = 1; /* faulty */
617 info->disk.state = 6; /* active and in sync */
618 info->disk.raid_disk = role;
620 info->events = __le64_to_cpu(sb->events);
621 sprintf(info->text_version, "1.%d", st->minor_version);
622 info->safe_mode_delay = 200;
624 memcpy(info->uuid, sb->set_uuid, 16);
626 strncpy(info->name, sb->set_name, 32);
629 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
630 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
632 info->recovery_start = MaxSector;
634 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
635 info->reshape_active = 1;
636 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
637 info->new_level = __le32_to_cpu(sb->new_level);
638 info->delta_disks = __le32_to_cpu(sb->delta_disks);
639 info->new_layout = __le32_to_cpu(sb->new_layout);
640 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
641 if (info->delta_disks < 0)
642 info->array.raid_disks -= info->delta_disks;
644 info->reshape_active = 0;
647 for (i=0; i<map_disks; i++)
649 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
650 role = __le16_to_cpu(sb->dev_roles[i]);
651 if (/*role == 0xFFFF || */role < (unsigned) info->array.raid_disks) {
653 if (map && role < map_disks)
658 info->array.working_disks = working;
661 static struct mdinfo *container_content1(struct supertype *st, char *subarray)
668 info = malloc(sizeof(*info));
669 getinfo_super1(st, info, NULL);
673 static int update_super1(struct supertype *st, struct mdinfo *info,
675 char *devname, int verbose,
676 int uuid_set, char *homehost)
678 /* NOTE: for 'assemble' and 'force' we need to return non-zero
679 * if any change was made. For others, the return value is
683 struct mdp_superblock_1 *sb = st->sb;
685 if (strcmp(update, "force-one")==0) {
686 /* Not enough devices for a working array,
687 * so bring this one up-to-date
689 if (sb->events != __cpu_to_le64(info->events))
691 sb->events = __cpu_to_le64(info->events);
692 } else if (strcmp(update, "force-array")==0) {
693 /* Degraded array and 'force' requests to
694 * maybe need to mark it 'clean'.
696 switch(__le32_to_cpu(sb->level)) {
697 case 5: case 4: case 6:
698 /* need to force clean */
699 if (sb->resync_offset != MaxSector)
701 sb->resync_offset = MaxSector;
703 } else if (strcmp(update, "assemble")==0) {
704 int d = info->disk.number;
706 if (info->disk.state == 6)
707 want = info->disk.raid_disk;
710 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
711 sb->dev_roles[d] = __cpu_to_le16(want);
714 if (info->reshape_active &&
715 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
716 info->delta_disks >= 0 &&
717 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
718 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
721 if (info->reshape_active &&
722 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
723 info->delta_disks < 0 &&
724 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
725 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
728 } else if (strcmp(update, "linear-grow-new") == 0) {
731 unsigned int max = __le32_to_cpu(sb->max_dev);
733 for (i=0 ; i < max ; i++)
734 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
736 sb->dev_number = __cpu_to_le32(i);
737 info->disk.number = i;
738 if (max >= __le32_to_cpu(sb->max_dev))
739 sb->max_dev = __cpu_to_le32(max+1);
741 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
742 read(rfd, sb->device_uuid, 16) != 16) {
743 __u32 r[4] = {random(), random(), random(), random()};
744 memcpy(sb->device_uuid, r, 16);
750 __cpu_to_le16(info->disk.raid_disk);
752 fd = open(devname, O_RDONLY);
754 unsigned long long ds;
755 get_dev_size(fd, devname, &ds);
758 if (__le64_to_cpu(sb->super_offset) <
759 __le64_to_cpu(sb->data_offset)) {
760 sb->data_size = __cpu_to_le64(
761 ds - __le64_to_cpu(sb->data_offset));
764 ds &= ~(unsigned long long)(4*2-1);
765 sb->super_offset = __cpu_to_le64(ds);
766 sb->data_size = __cpu_to_le64(
767 ds - __le64_to_cpu(sb->data_offset));
770 } else if (strcmp(update, "linear-grow-update") == 0) {
771 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
772 sb->dev_roles[info->disk.number] =
773 __cpu_to_le16(info->disk.raid_disk);
774 } else if (strcmp(update, "resync") == 0) {
775 /* make sure resync happens */
776 sb->resync_offset = 0ULL;
777 } else if (strcmp(update, "uuid") == 0) {
778 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
780 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
781 struct bitmap_super_s *bm;
782 bm = (struct bitmap_super_s*)(st->sb+1024);
783 memcpy(bm->uuid, sb->set_uuid, 16);
785 } else if (strcmp(update, "no-bitmap") == 0) {
786 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
787 } else if (strcmp(update, "homehost") == 0 &&
791 c = strchr(sb->set_name, ':');
793 strncpy(info->name, c+1, 31 - (c-sb->set_name));
795 strncpy(info->name, sb->set_name, 32);
797 } else if (strcmp(update, "name") == 0) {
798 if (info->name[0] == 0)
799 sprintf(info->name, "%d", info->array.md_minor);
800 memset(sb->set_name, 0, sizeof(sb->set_name));
802 strchr(info->name, ':') == NULL &&
803 strlen(homehost)+1+strlen(info->name) < 32) {
804 strcpy(sb->set_name, homehost);
805 strcat(sb->set_name, ":");
806 strcat(sb->set_name, info->name);
808 strcpy(sb->set_name, info->name);
809 } else if (strcmp(update, "devicesize") == 0 &&
810 __le64_to_cpu(sb->super_offset) <
811 __le64_to_cpu(sb->data_offset)) {
812 /* set data_size to device size less data_offset */
813 struct misc_dev_info *misc = (struct misc_dev_info*)
814 (st->sb + 1024 + 512);
815 printf("Size was %llu\n", (unsigned long long)
816 __le64_to_cpu(sb->data_size));
817 sb->data_size = __cpu_to_le64(
818 misc->device_size - __le64_to_cpu(sb->data_offset));
819 printf("Size is %llu\n", (unsigned long long)
820 __le64_to_cpu(sb->data_size));
821 } else if (strcmp(update, "_reshape_progress")==0)
822 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
826 sb->sb_csum = calc_sb_1_csum(sb);
830 static int init_super1(struct supertype *st, mdu_array_info_t *info,
831 unsigned long long size, char *name, char *homehost, int *uuid)
833 struct mdp_superblock_1 *sb;
838 if (posix_memalign((void**)&sb, 512, (1024 + 512 +
839 sizeof(struct misc_dev_info))) != 0) {
841 ": %s could not allocate superblock\n", __func__);
848 /* zeroing superblock */
852 spares = info->working_disks - info->active_disks;
853 if (info->raid_disks + spares > 384) {
854 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
855 info->raid_disks , spares, 384);
859 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
860 sb->major_version = __cpu_to_le32(1);
865 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
867 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
868 read(rfd, sb->set_uuid, 16) != 16) {
869 __u32 r[4] = {random(), random(), random(), random()};
870 memcpy(sb->set_uuid, r, 16);
872 if (rfd >= 0) close(rfd);
875 if (name == NULL || *name == 0) {
876 sprintf(defname, "%d", info->md_minor);
879 memset(sb->set_name, 0, 32);
881 strchr(name, ':')== NULL &&
882 strlen(homehost)+1+strlen(name) < 32) {
883 strcpy(sb->set_name, homehost);
884 strcat(sb->set_name, ":");
885 strcat(sb->set_name, name);
887 strcpy(sb->set_name, name);
889 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
890 sb->level = __cpu_to_le32(info->level);
891 sb->layout = __cpu_to_le32(info->layout);
892 sb->size = __cpu_to_le64(size*2ULL);
893 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
894 sb->raid_disks = __cpu_to_le32(info->raid_disks);
896 sb->data_offset = __cpu_to_le64(0);
897 sb->data_size = __cpu_to_le64(0);
898 sb->super_offset = __cpu_to_le64(0);
899 sb->recovery_offset = __cpu_to_le64(0);
901 sb->utime = sb->ctime;
902 sb->events = __cpu_to_le64(1);
903 if (info->state & (1<<MD_SB_CLEAN))
904 sb->resync_offset = MaxSector;
906 sb->resync_offset = 0;
907 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
908 sizeof(sb->dev_roles[0]));
909 memset(sb->pad3, 0, sizeof(sb->pad3));
911 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
919 mdu_disk_info_t disk;
920 struct devinfo *next;
923 /* Add a device to the superblock being created */
924 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
925 int fd, char *devname)
927 struct mdp_superblock_1 *sb = st->sb;
928 __u16 *rp = sb->dev_roles + dk->number;
929 struct devinfo *di, **dip;
931 if ((dk->state & 6) == 6) /* active, sync */
932 *rp = __cpu_to_le16(dk->raid_disk);
933 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
938 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
939 __le32_to_cpu(sb->max_dev) < 384)
940 sb->max_dev = __cpu_to_le32(dk->number+1);
942 sb->dev_number = __cpu_to_le32(dk->number);
943 sb->sb_csum = calc_sb_1_csum(sb);
945 dip = (struct devinfo **)&st->info;
948 di = malloc(sizeof(struct devinfo));
950 di->devname = devname;
959 static void locate_bitmap1(struct supertype *st, int fd);
961 static int store_super1(struct supertype *st, int fd)
963 struct mdp_superblock_1 *sb = st->sb;
964 unsigned long long sb_offset;
966 unsigned long long dsize;
968 if (!get_dev_size(fd, NULL, &dsize))
977 * Calculate the position of the superblock.
978 * It is always aligned to a 4K boundary and
979 * depending on minor_version, it can be:
980 * 0: At least 8K, but less than 12K, from end of device
981 * 1: At start of device
982 * 2: 4K from start of device.
984 switch(st->minor_version) {
988 sb_offset &= ~(4*2-1);
1002 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1003 0 != __le64_to_cpu(sb->super_offset)
1005 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
1009 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
1012 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
1013 sbsize = (sbsize+511)&(~511UL);
1015 if (awrite(fd, sb, sbsize) != sbsize)
1018 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1019 struct bitmap_super_s *bm = (struct bitmap_super_s*)
1021 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
1022 locate_bitmap1(st, fd);
1023 if (awrite(fd, bm, sizeof(*bm)) !=
1032 static int load_super1(struct supertype *st, int fd, char *devname);
1034 static unsigned long choose_bm_space(unsigned long devsize)
1036 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1037 * if bigger than 200Gig, save 128k
1038 * NOTE: result must be multiple of 4K else bad things happen
1039 * on 4K-sector devices.
1041 if (devsize < 64*2) return 0;
1042 if (devsize - 64*2 >= 200*1024*1024*2)
1044 if (devsize - 4*2 > 8*1024*1024*2)
1049 static void free_super1(struct supertype *st);
1052 static int write_init_super1(struct supertype *st)
1054 struct mdp_superblock_1 *sb = st->sb;
1055 struct supertype *refst;
1058 unsigned long long bm_space;
1059 unsigned long long reserved;
1061 unsigned long long dsize, array_size;
1062 unsigned long long sb_offset;
1064 for (di = st->info; di && ! rv ; di = di->next) {
1065 if (di->disk.state == 1)
1070 while (Kill(di->devname, NULL, 0, 1, 1) == 0)
1073 sb->dev_number = __cpu_to_le32(di->disk.number);
1074 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1075 sb->devflags |= __cpu_to_le32(WriteMostly1);
1077 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1078 read(rfd, sb->device_uuid, 16) != 16) {
1079 __u32 r[4] = {random(), random(), random(), random()};
1080 memcpy(sb->device_uuid, r, 16);
1087 refst = dup_super(st);
1088 if (load_super1(refst, di->fd, NULL)==0) {
1089 struct mdp_superblock_1 *refsb = refst->sb;
1091 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1092 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1093 /* same array, so preserve events and
1095 sb->events = refsb->events;
1096 /* bugs in 2.6.17 and earlier mean the
1097 * dev_number chosen in Manage must be preserved
1099 if (get_linux_version() >= 2006018)
1100 sb->dev_number = refsb->dev_number;
1106 if (!get_dev_size(di->fd, NULL, &dsize))
1117 * Calculate the position of the superblock.
1118 * It is always aligned to a 4K boundary and
1119 * depending on minor_version, it can be:
1120 * 0: At least 8K, but less than 12K, from end of device
1121 * 1: At start of device
1122 * 2: 4K from start of device.
1123 * Depending on the array size, we might leave extra space
1125 * Also leave 4K for bad-block log.
1127 array_size = __le64_to_cpu(sb->size);
1128 /* work out how much space we left for a bitmap,
1129 * Add 8 sectors for bad block log */
1130 bm_space = choose_bm_space(array_size) + 8;
1132 switch(st->minor_version) {
1136 sb_offset &= ~(4*2-1);
1137 sb->super_offset = __cpu_to_le64(sb_offset);
1138 sb->data_offset = __cpu_to_le64(0);
1139 if (sb_offset < array_size + bm_space)
1140 bm_space = sb_offset - array_size;
1141 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1142 if (bm_space >= 8) {
1143 sb->bblog_size = __cpu_to_le16(8);
1144 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1148 sb->super_offset = __cpu_to_le64(0);
1149 reserved = bm_space + 4*2;
1150 /* Try for multiple of 1Meg so it is nicely aligned */
1151 #define ONE_MEG (2*1024)
1152 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1153 if (reserved + __le64_to_cpu(sb->size) > dsize)
1154 reserved = dsize - __le64_to_cpu(sb->size);
1155 /* force 4K alignment */
1158 sb->data_offset = __cpu_to_le64(reserved);
1159 sb->data_size = __cpu_to_le64(dsize - reserved);
1160 if (reserved >= 16) {
1161 sb->bblog_size = __cpu_to_le16(8);
1162 sb->bblog_offset = __cpu_to_le32(reserved-8);
1167 sb->super_offset = __cpu_to_le64(4*2);
1168 if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
1170 bm_space = dsize - __le64_to_cpu(sb->size)
1173 reserved = bm_space + 4*2 + 4*2;
1174 /* Try for multiple of 1Meg so it is nicely aligned */
1175 #define ONE_MEG (2*1024)
1176 reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
1177 if (reserved + __le64_to_cpu(sb->size) > dsize)
1178 reserved = dsize - __le64_to_cpu(sb->size);
1179 /* force 4K alignment */
1182 sb->data_offset = __cpu_to_le64(reserved);
1183 sb->data_size = __cpu_to_le64(dsize - reserved);
1184 if (reserved >= 16+16) {
1185 sb->bblog_size = __cpu_to_le16(8);
1186 sb->bblog_offset = __cpu_to_le32(reserved-8-8);
1194 sb->sb_csum = calc_sb_1_csum(sb);
1195 rv = store_super1(st, di->fd);
1198 Name ": failed to write superblock to %s\n",
1201 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1202 rv = st->ss->write_bitmap(st, di->fd);
1210 static int compare_super1(struct supertype *st, struct supertype *tst)
1214 * 0 same, or first was empty, and second was copied
1215 * 1 second had wrong number
1217 * 3 wrong other info
1219 struct mdp_superblock_1 *first = st->sb;
1220 struct mdp_superblock_1 *second = tst->sb;
1222 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1224 if (second->major_version != __cpu_to_le32(1))
1228 if (posix_memalign((void**)&first, 512,
1230 sizeof(struct misc_dev_info)) != 0) {
1231 fprintf(stderr, Name
1232 ": %s could not allocate superblock\n", __func__);
1235 memcpy(first, second, 1024 + 512 +
1236 sizeof(struct misc_dev_info));
1240 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1243 if (first->ctime != second->ctime ||
1244 first->level != second->level ||
1245 first->layout != second->layout ||
1246 first->size != second->size ||
1247 first->chunksize != second->chunksize ||
1248 first->raid_disks != second->raid_disks)
1253 static int load_super1(struct supertype *st, int fd, char *devname)
1255 unsigned long long dsize;
1256 unsigned long long sb_offset;
1257 struct mdp_superblock_1 *super;
1259 struct bitmap_super_s *bsb;
1260 struct misc_dev_info *misc;
1264 if (st->ss == NULL || st->minor_version == -1) {
1266 struct supertype tst;
1267 __u64 bestctime = 0;
1268 /* guess... choose latest ctime */
1269 memset(&tst, 0, sizeof(tst));
1271 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1272 switch(load_super1(&tst, fd, devname)) {
1273 case 0: super = tst.sb;
1274 if (bestvers == -1 ||
1275 bestctime < __le64_to_cpu(super->ctime)) {
1276 bestvers = tst.minor_version;
1277 bestctime = __le64_to_cpu(super->ctime);
1282 case 1: return 1; /*bad device */
1283 case 2: break; /* bad, try next */
1286 if (bestvers != -1) {
1288 tst.minor_version = bestvers;
1291 rv = load_super1(&tst, fd, devname);
1298 if (!get_dev_size(fd, devname, &dsize))
1304 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
1310 * Calculate the position of the superblock.
1311 * It is always aligned to a 4K boundary and
1312 * depending on minor_version, it can be:
1313 * 0: At least 8K, but less than 12K, from end of device
1314 * 1: At start of device
1315 * 2: 4K from start of device.
1317 switch(st->minor_version) {
1321 sb_offset &= ~(4*2-1);
1333 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1336 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1338 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
1339 devname, strerror(errno));
1343 if (posix_memalign((void**)&super, 512,
1345 sizeof(struct misc_dev_info)) != 0) {
1346 fprintf(stderr, Name ": %s could not allocate superblock\n",
1351 if (aread(fd, super, 1024) != 1024) {
1353 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1359 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1361 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1362 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1367 if (__le32_to_cpu(super->major_version) != 1) {
1369 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1370 devname, __le32_to_cpu(super->major_version));
1374 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1376 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1383 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1385 misc = (struct misc_dev_info*) (((char*)super)+1024+512);
1386 misc->device_size = dsize;
1388 /* Now check on the bitmap superblock */
1389 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1391 /* Read the bitmap superblock and make sure it looks
1392 * valid. If it doesn't clear the bit. An --assemble --force
1393 * should get that written out.
1395 locate_bitmap1(st, fd);
1396 if (aread(fd, ((char*)super)+1024, 512)
1400 uuid_from_super1(st, uuid);
1401 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1402 memcmp(bsb->uuid, uuid, 16) != 0)
1407 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
1412 static struct supertype *match_metadata_desc1(char *arg)
1414 struct supertype *st = malloc(sizeof(*st));
1417 memset(st, 0, sizeof(*st));
1418 st->container_dev = NoMdDev;
1422 /* leading zeros can be safely ignored. --detail generates them. */
1425 if (strcmp(arg, "1.0") == 0 ||
1426 strcmp(arg, "1.00") == 0) {
1427 st->minor_version = 0;
1430 if (strcmp(arg, "1.1") == 0 ||
1431 strcmp(arg, "1.01") == 0
1433 st->minor_version = 1;
1436 if (strcmp(arg, "1.2") == 0 ||
1437 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1438 strcmp(arg, "default") == 0 ||
1439 #endif /* DEFAULT_OLD_METADATA */
1440 strcmp(arg, "1.02") == 0) {
1441 st->minor_version = 2;
1444 if (strcmp(arg, "1") == 0 ||
1445 strcmp(arg, "default") == 0) {
1446 st->minor_version = -1;
1454 /* find available size on device with this devsize, using
1455 * superblock type st, and reserving 'reserve' sectors for
1458 static __u64 avail_size1(struct supertype *st, __u64 devsize)
1460 struct mdp_superblock_1 *super = st->sb;
1465 /* creating: allow suitable space for bitmap */
1466 devsize -= choose_bm_space(devsize);
1468 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1469 /* hot-add. allow for actual size of bitmap */
1470 struct bitmap_super_s *bsb;
1471 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1472 devsize -= bitmap_sectors(bsb);
1476 if (st->minor_version < 0)
1477 /* not specified, so time to set default */
1478 st->minor_version = 2;
1479 if (super == NULL && st->minor_version > 0) {
1480 /* haven't committed to a size yet, so allow some
1481 * slack for alignment of data_offset.
1482 * We haven't access to device details so allow
1483 * 1 Meg if bigger than 1Gig
1485 if (devsize > 1024*1024*2)
1488 switch(st->minor_version) {
1491 return ((devsize - 8*2 ) & ~(4*2-1));
1493 /* at start, 4K for superblock and possible bitmap */
1494 return devsize - 4*2;
1496 /* 4k from start, 4K for superblock and possible bitmap */
1497 return devsize - (4+4)*2;
1503 add_internal_bitmap1(struct supertype *st,
1504 int *chunkp, int delay, int write_behind,
1505 unsigned long long size,
1506 int may_change, int major)
1509 * If not may_change, then this is a 'Grow', and the bitmap
1510 * must fit after the superblock.
1511 * If may_change, then this is create, and we can put the bitmap
1512 * before the superblock if we like, or may move the start.
1513 * If !may_change, the bitmap MUST live at offset of 1K, until
1514 * we get a sysfs interface.
1516 * size is in sectors, chunk is in bytes !!!
1519 unsigned long long bits;
1520 unsigned long long max_bits;
1521 unsigned long long min_chunk;
1523 unsigned long long chunk = *chunkp;
1525 struct mdp_superblock_1 *sb = st->sb;
1526 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1529 switch(st->minor_version) {
1531 /* either 3K after the superblock (when hot-add),
1532 * or some amount of space before.
1535 /* We are creating array, so we *know* how much room has
1539 room = choose_bm_space(__le64_to_cpu(sb->size));
1541 room = __le64_to_cpu(sb->super_offset)
1542 - __le64_to_cpu(sb->data_offset)
1543 - __le64_to_cpu(sb->data_size);
1544 /* remove '1 ||' when we can set offset via sysfs */
1545 if (1 || (room < 3*2 &&
1546 __le32_to_cpu(sb->max_dev) <= 384)) {
1550 offset = 0; /* means movable offset */
1555 case 2: /* between superblock and data */
1558 room = choose_bm_space(__le64_to_cpu(sb->size));
1560 room = __le64_to_cpu(sb->data_offset)
1561 - __le64_to_cpu(sb->super_offset);
1562 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1575 if (chunk == UnSet && room > 128*2)
1576 /* Limit to 128K of bitmap when chunk size not requested */
1579 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1581 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1582 bits = (size*512)/min_chunk +1;
1583 while (bits > max_bits) {
1587 if (chunk == UnSet) {
1588 /* For practical purpose, 64Meg is a good
1589 * default chunk size for internal bitmaps.
1592 if (chunk < 64*1024*1024)
1593 chunk = 64*1024*1024;
1594 } else if (chunk < min_chunk)
1595 return 0; /* chunk size too small */
1596 if (chunk == 0) /* rounding problem */
1600 /* start bitmap on a 4K boundary with enough space for
1603 bits = (size*512) / chunk + 1;
1604 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1605 room *= 8; /* convert 4K blocks to sectors */
1609 sb->bitmap_offset = __cpu_to_le32(offset);
1611 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
1612 memset(bms, 0, sizeof(*bms));
1613 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1614 bms->version = __cpu_to_le32(major);
1615 uuid_from_super1(st, uuid);
1616 memcpy(bms->uuid, uuid, 16);
1617 bms->chunksize = __cpu_to_le32(chunk);
1618 bms->daemon_sleep = __cpu_to_le32(delay);
1619 bms->sync_size = __cpu_to_le64(size);
1620 bms->write_behind = __cpu_to_le32(write_behind);
1627 static void locate_bitmap1(struct supertype *st, int fd)
1629 unsigned long long offset;
1630 struct mdp_superblock_1 *sb;
1634 if (st->ss->load_super(st, fd, NULL))
1635 return; /* no error I hope... */
1640 offset = __le64_to_cpu(sb->super_offset);
1641 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1644 lseek64(fd, offset<<9, 0);
1647 static int write_bitmap1(struct supertype *st, int fd)
1649 struct mdp_superblock_1 *sb = st->sb;
1650 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
1654 char *buf = (char*)(((long)(abuf+4096))&~4095UL);
1656 locate_bitmap1(st, fd);
1658 memset(buf, 0xff, 4096);
1659 memcpy(buf, ((char*)sb)+1024, sizeof(bitmap_super_t));
1661 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1662 towrite = (towrite+7) >> 3; /* bits to bytes */
1663 towrite += sizeof(bitmap_super_t);
1664 towrite = ROUND_UP(towrite, 512);
1665 while (towrite > 0) {
1669 n = write(fd, buf, n);
1674 memset(buf, 0xff, 4096);
1683 static void free_super1(struct supertype *st)
1688 struct devinfo *di = st->info;
1689 st->info = di->next;
1698 static int validate_geometry1(struct supertype *st, int level,
1699 int layout, int raiddisks,
1700 int *chunk, unsigned long long size,
1701 char *subdev, unsigned long long *freesize,
1704 unsigned long long ldsize;
1707 if (level == LEVEL_CONTAINER) {
1709 fprintf(stderr, Name ": 1.x metadata does not support containers\n");
1712 if (chunk && *chunk == UnSet)
1713 *chunk = DEFAULT_CHUNK;
1718 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1721 fprintf(stderr, Name ": super1.x cannot open %s: %s\n",
1722 subdev, strerror(errno));
1726 if (!get_dev_size(fd, subdev, &ldsize)) {
1732 *freesize = avail_size1(st, ldsize >> 9);
1735 #endif /* MDASSEMBLE */
1737 struct superswitch super1 = {
1739 .examine_super = examine_super1,
1740 .brief_examine_super = brief_examine_super1,
1741 .export_examine_super = export_examine_super1,
1742 .detail_super = detail_super1,
1743 .brief_detail_super = brief_detail_super1,
1744 .export_detail_super = export_detail_super1,
1745 .write_init_super = write_init_super1,
1746 .validate_geometry = validate_geometry1,
1747 .add_to_super = add_to_super1,
1749 .match_home = match_home1,
1750 .uuid_from_super = uuid_from_super1,
1751 .getinfo_super = getinfo_super1,
1752 .container_content = container_content1,
1753 .update_super = update_super1,
1754 .init_super = init_super1,
1755 .store_super = store_super1,
1756 .compare_super = compare_super1,
1757 .load_super = load_super1,
1758 .match_metadata_desc = match_metadata_desc1,
1759 .avail_size = avail_size1,
1760 .add_internal_bitmap = add_internal_bitmap1,
1761 .locate_bitmap = locate_bitmap1,
1762 .write_bitmap = write_bitmap1,
1763 .free_super = free_super1,
1764 #if __BYTE_ORDER == BIG_ENDIAN