2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
27 * The version-1 superblock :
28 * All numeric fields are little-endian.
30 * total size: 256 bytes plus 2 per device.
31 * 1K allows 384 devices.
33 struct mdp_superblock_1
{
34 /* constant array information - 128 bytes */
35 __u32 magic
; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
36 __u32 major_version
; /* 1 */
37 __u32 feature_map
; /* 0 for now */
38 __u32 pad0
; /* always set to 0 when writing */
40 __u8 set_uuid
[16]; /* user-space generated. */
41 char set_name
[32]; /* set and interpreted by user-space */
43 __u64 ctime
; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
44 __u32 level
; /* -4 (multipath), -1 (linear), 0,1,4,5 */
45 __u32 layout
; /* only for raid5 currently */
46 __u64 size
; /* used size of component devices, in 512byte sectors */
48 __u32 chunksize
; /* in 512byte sectors */
50 __u32 bitmap_offset
; /* sectors after start of superblock that bitmap starts
51 * NOTE: signed, so bitmap can be before superblock
52 * only meaningful of feature_map[0] is set.
55 /* These are only valid with feature bit '4' */
56 __u32 new_level
; /* new level we are reshaping to */
57 __u64 reshape_position
; /* next address in array-space for reshape */
58 __u32 delta_disks
; /* change in number of raid_disks */
59 __u32 new_layout
; /* new layout */
60 __u32 new_chunk
; /* new chunk size (bytes) */
61 __u8 pad1
[128-124]; /* set to 0 when written */
63 /* constant this-device information - 64 bytes */
64 __u64 data_offset
; /* sector start of data, often 0 */
65 __u64 data_size
; /* sectors in this device that can be used for data */
66 __u64 super_offset
; /* sector start of this superblock */
67 __u64 recovery_offset
;/* sectors before this offset (from data_offset) have been recovered */
68 __u32 dev_number
; /* permanent identifier of this device - not role in raid */
69 __u32 cnt_corrected_read
; /* number of read errors that were corrected by re-writing */
70 __u8 device_uuid
[16]; /* user-space setable, ignored by kernel */
71 __u8 devflags
; /* per-device flags. Only one defined...*/
72 #define WriteMostly1 1 /* mask for writemostly flag in above */
73 __u8 pad2
[64-57]; /* set to 0 when writing */
75 /* array state information - 64 bytes */
76 __u64 utime
; /* 40 bits second, 24 btes microseconds */
77 __u64 events
; /* incremented when superblock updated */
78 __u64 resync_offset
; /* data before this offset (from data_offset) known to be in sync */
79 __u32 sb_csum
; /* checksum upto dev_roles[max_dev] */
80 __u32 max_dev
; /* size of dev_roles[] array to consider */
81 __u8 pad3
[64-32]; /* set to 0 when writing */
83 /* device state information. Indexed by dev_number.
85 * Note there are no per-device state flags. State information is rolled
86 * into the 'roles' value. If a device is spare or faulty, then it doesn't
87 * have a meaningful role.
89 __u16 dev_roles
[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
92 struct misc_dev_info
{
96 /* feature_map bits */
97 #define MD_FEATURE_BITMAP_OFFSET 1
98 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
101 #define MD_FEATURE_RESHAPE_ACTIVE 4
103 #define MD_FEATURE_ALL (1|2|4)
106 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
108 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
110 unsigned int disk_csum
, csum
;
111 unsigned long long newcsum
;
112 int size
= sizeof(*sb
) + __le32_to_cpu(sb
->max_dev
)*2;
113 unsigned int *isuper
= (unsigned int*)sb
;
116 /* make sure I can count... */
117 if (offsetof(struct mdp_superblock_1
,data_offset
) != 128 ||
118 offsetof(struct mdp_superblock_1
, utime
) != 192 ||
119 sizeof(struct mdp_superblock_1
) != 256) {
120 fprintf(stderr
, "WARNING - superblock isn't sized correctly\n");
123 disk_csum
= sb
->sb_csum
;
126 for (i
=0; size
>=4; size
-= 4 ) {
127 newcsum
+= __le32_to_cpu(*isuper
);
132 newcsum
+= __le16_to_cpu(*(unsigned short*) isuper
);
134 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
135 sb
->sb_csum
= disk_csum
;
136 return __cpu_to_le32(csum
);
139 static char abuf
[4096+4096];
140 static int aread(int fd
, void *buf
, int len
)
143 * On devices with a 4K sector size, we need to read
144 * the full sector and copy relevant bits into
150 if (ioctl(fd
, BLKSSZGET
, &bsize
) != 0 ||
152 return read(fd
, buf
, len
);
155 b
= (char*)(((long)(abuf
+4096))&~4095UL);
157 n
= read(fd
, b
, bsize
);
160 lseek(fd
, len
- n
, 1);
167 static int awrite(int fd
, void *buf
, int len
)
170 * On devices with a 4K sector size, we need to write
171 * the full sector. We pre-read if the sector is larger
173 * The address must be sector-aligned.
178 if (ioctl(fd
, BLKSSZGET
, &bsize
) != 0 ||
180 return write(fd
, buf
, len
);
183 b
= (char*)(((long)(abuf
+4096))&~4095UL);
185 n
= read(fd
, b
, bsize
);
190 n
= write(fd
, b
, bsize
);
193 lseek(fd
, len
- n
, 1);
198 static void examine_super1(struct supertype
*st
, char *homehost
)
200 struct mdp_superblock_1
*sb
= st
->sb
;
207 int l
= homehost
? strlen(homehost
) : 0;
209 unsigned long long sb_offset
;
211 printf(" Magic : %08x\n", __le32_to_cpu(sb
->magic
));
212 printf(" Version : 1");
213 sb_offset
= __le64_to_cpu(sb
->super_offset
);
216 else if (sb_offset
<= 8)
220 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb
->feature_map
));
221 printf(" Array UUID : ");
222 for (i
=0; i
<16; i
++) {
223 if ((i
&3)==0 && i
!= 0) printf(":");
224 printf("%02x", sb
->set_uuid
[i
]);
227 printf(" Name : %.32s", sb
->set_name
);
228 if (l
> 0 && l
< 32 &&
229 sb
->set_name
[l
] == ':' &&
230 strncmp(sb
->set_name
, homehost
, l
) == 0)
231 printf(" (local to host %s)", homehost
);
233 atime
= __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
;
234 printf(" Creation Time : %.24s\n", ctime(&atime
));
235 c
=map_num(pers
, __le32_to_cpu(sb
->level
));
236 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
237 printf(" Raid Devices : %d\n", __le32_to_cpu(sb
->raid_disks
));
239 printf(" Avail Dev Size : %llu%s\n",
240 (unsigned long long)__le64_to_cpu(sb
->data_size
),
241 human_size(__le64_to_cpu(sb
->data_size
)<<9));
242 if (__le32_to_cpu(sb
->level
) > 0) {
244 switch(__le32_to_cpu(sb
->level
)) {
245 case 1: ddsks
=1;break;
247 case 5: ddsks
= __le32_to_cpu(sb
->raid_disks
)-1; break;
248 case 6: ddsks
= __le32_to_cpu(sb
->raid_disks
)-2; break;
250 layout
= __le32_to_cpu(sb
->layout
);
251 ddsks
= __le32_to_cpu(sb
->raid_disks
)
252 / (layout
&255) / ((layout
>>8)&255);
255 printf(" Array Size : %llu%s\n",
256 ddsks
*(unsigned long long)__le64_to_cpu(sb
->size
),
257 human_size(ddsks
*__le64_to_cpu(sb
->size
)<<9));
258 if (sb
->size
!= sb
->data_size
)
259 printf(" Used Dev Size : %llu%s\n",
260 (unsigned long long)__le64_to_cpu(sb
->size
),
261 human_size(__le64_to_cpu(sb
->size
)<<9));
264 printf(" Data Offset : %llu sectors\n",
265 (unsigned long long)__le64_to_cpu(sb
->data_offset
));
266 printf(" Super Offset : %llu sectors\n",
267 (unsigned long long)__le64_to_cpu(sb
->super_offset
));
268 if (__le32_to_cpu(sb
->feature_map
) & MD_FEATURE_RECOVERY_OFFSET
)
269 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->recovery_offset
));
270 printf(" State : %s\n", (__le64_to_cpu(sb
->resync_offset
)+1)? "active":"clean");
271 printf(" Device UUID : ");
272 for (i
=0; i
<16; i
++) {
273 if ((i
&3)==0 && i
!= 0) printf(":");
274 printf("%02x", sb
->device_uuid
[i
]);
278 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
279 printf("Internal Bitmap : %ld sectors from superblock\n",
280 (long)(int32_t)__le32_to_cpu(sb
->bitmap_offset
));
282 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
283 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb
->reshape_position
)/2,
284 human_size(__le64_to_cpu(sb
->reshape_position
)<<9));
285 if (__le32_to_cpu(sb
->delta_disks
)) {
286 printf(" Delta Devices : %d", __le32_to_cpu(sb
->delta_disks
));
287 printf(" (%d->%d)\n",
288 __le32_to_cpu(sb
->raid_disks
)-__le32_to_cpu(sb
->delta_disks
),
289 __le32_to_cpu(sb
->raid_disks
));
290 if ((int)__le32_to_cpu(sb
->delta_disks
) < 0)
291 delta_extra
= -__le32_to_cpu(sb
->delta_disks
);
293 if (__le32_to_cpu(sb
->new_level
) != __le32_to_cpu(sb
->level
)) {
294 c
= map_num(pers
, __le32_to_cpu(sb
->new_level
));
295 printf(" New Level : %s\n", c
?c
:"-unknown-");
297 if (__le32_to_cpu(sb
->new_layout
) != __le32_to_cpu(sb
->layout
)) {
298 if (__le32_to_cpu(sb
->level
) == 5) {
299 c
= map_num(r5layout
, __le32_to_cpu(sb
->new_layout
));
300 printf(" New Layout : %s\n", c
?c
:"-unknown-");
302 if (__le32_to_cpu(sb
->level
) == 6) {
303 c
= map_num(r6layout
, __le32_to_cpu(sb
->new_layout
));
304 printf(" New Layout : %s\n", c
?c
:"-unknown-");
306 if (__le32_to_cpu(sb
->level
) == 10) {
307 printf(" New Layout :");
308 print_r10_layout(__le32_to_cpu(sb
->new_layout
));
312 if (__le32_to_cpu(sb
->new_chunk
) != __le32_to_cpu(sb
->chunksize
))
313 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb
->new_chunk
)/2);
318 if (sb
->devflags
& WriteMostly1
)
319 printf(" write-mostly");
323 atime
= __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
;
324 printf(" Update Time : %.24s\n", ctime(&atime
));
326 if (calc_sb_1_csum(sb
) == sb
->sb_csum
)
327 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb
->sb_csum
));
329 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb
->sb_csum
),
330 __le32_to_cpu(calc_sb_1_csum(sb
)));
331 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb
->events
));
333 if (__le32_to_cpu(sb
->level
) == 5) {
334 c
= map_num(r5layout
, __le32_to_cpu(sb
->layout
));
335 printf(" Layout : %s\n", c
?c
:"-unknown-");
337 if (__le32_to_cpu(sb
->level
) == 6) {
338 c
= map_num(r6layout
, __le32_to_cpu(sb
->layout
));
339 printf(" Layout : %s\n", c
?c
:"-unknown-");
341 if (__le32_to_cpu(sb
->level
) == 10) {
342 int lo
= __le32_to_cpu(sb
->layout
);
344 print_r10_layout(lo
);
347 switch(__le32_to_cpu(sb
->level
)) {
353 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
356 printf(" Rounding : %dK\n", __le32_to_cpu(sb
->chunksize
)/2);
362 /* This turns out to just be confusing */
363 printf(" Array Slot : %d (", __le32_to_cpu(sb
->dev_number
));
364 for (i
= __le32_to_cpu(sb
->max_dev
); i
> 0 ; i
--)
365 if (__le16_to_cpu(sb
->dev_roles
[i
-1]) != 0xffff)
367 for (d
=0; d
< i
; d
++) {
368 int role
= __le16_to_cpu(sb
->dev_roles
[d
]);
370 if (role
== 0xffff) printf("empty");
371 else if(role
== 0xfffe) printf("failed");
372 else printf("%d", role
);
376 printf(" Device Role : ");
377 d
= __le32_to_cpu(sb
->dev_number
);
378 if (d
< __le32_to_cpu(sb
->max_dev
))
379 role
= __le16_to_cpu(sb
->dev_roles
[d
]);
385 printf("Active device %d\n", role
);
387 printf(" Array State : ");
388 for (d
=0; d
<__le32_to_cpu(sb
->raid_disks
) + delta_extra
; d
++) {
392 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
393 unsigned int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
395 if (i
== __le32_to_cpu(sb
->dev_number
))
400 if (cnt
> 1) printf("?");
401 else if (cnt
== 1) printf("A");
405 /* This is confusing too */
407 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
408 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
412 if (faulty
) printf(" %d failed", faulty
);
414 printf(" ('A' == active, '.' == missing)");
419 static void brief_examine_super1(struct supertype
*st
, int verbose
)
421 struct mdp_superblock_1
*sb
= st
->sb
;
423 unsigned long long sb_offset
;
425 char *c
=map_num(pers
, __le32_to_cpu(sb
->level
));
427 nm
= strchr(sb
->set_name
, ':');
430 else if (sb
->set_name
[0])
435 printf("ARRAY%s%s", nm
? " /dev/md/":"", nm
);
437 printf(" level=%s", c
);
438 sb_offset
= __le64_to_cpu(sb
->super_offset
);
440 printf(" metadata=1.1 ");
441 else if (sb_offset
<= 8)
442 printf(" metadata=1.2 ");
444 printf(" metadata=1.0 ");
446 printf("num-devices=%d ", __le32_to_cpu(sb
->raid_disks
));
448 for (i
=0; i
<16; i
++) {
449 if ((i
&3)==0 && i
!= 0) printf(":");
450 printf("%02x", sb
->set_uuid
[i
]);
453 printf(" name=%.32s", sb
->set_name
);
457 static void export_examine_super1(struct supertype
*st
)
459 struct mdp_superblock_1
*sb
= st
->sb
;
463 printf("MD_LEVEL=%s\n", map_num(pers
, __le32_to_cpu(sb
->level
)));
464 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb
->raid_disks
));
466 if (sb
->set_name
[i
] == '\n' ||
467 sb
->set_name
[i
] == '\0') {
472 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
474 for (i
=0; i
<16; i
++) {
475 if ((i
&3)==0 && i
!= 0) printf(":");
476 printf("%02x", sb
->set_uuid
[i
]);
479 printf("MD_UPDATE_TIME=%llu\n",
480 __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
);
481 printf("MD_DEV_UUID=");
482 for (i
=0; i
<16; i
++) {
483 if ((i
&3)==0 && i
!= 0) printf(":");
484 printf("%02x", sb
->device_uuid
[i
]);
487 printf("MD_EVENTS=%llu\n",
488 (unsigned long long)__le64_to_cpu(sb
->events
));
491 static void detail_super1(struct supertype
*st
, char *homehost
)
493 struct mdp_superblock_1
*sb
= st
->sb
;
495 int l
= homehost
? strlen(homehost
) : 0;
497 printf(" Name : %.32s", sb
->set_name
);
498 if (l
> 0 && l
< 32 &&
499 sb
->set_name
[l
] == ':' &&
500 strncmp(sb
->set_name
, homehost
, l
) == 0)
501 printf(" (local to host %s)", homehost
);
502 printf("\n UUID : ");
503 for (i
=0; i
<16; i
++) {
504 if ((i
&3)==0 && i
!= 0) printf(":");
505 printf("%02x", sb
->set_uuid
[i
]);
507 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb
->events
));
510 static void brief_detail_super1(struct supertype
*st
)
512 struct mdp_superblock_1
*sb
= st
->sb
;
516 printf(" name=%.32s", sb
->set_name
);
518 for (i
=0; i
<16; i
++) {
519 if ((i
&3)==0 && i
!= 0) printf(":");
520 printf("%02x", sb
->set_uuid
[i
]);
524 static void export_detail_super1(struct supertype
*st
)
526 struct mdp_superblock_1
*sb
= st
->sb
;
531 if (sb
->set_name
[i
] == '\n' ||
532 sb
->set_name
[i
] == '\0') {
537 printf("MD_NAME=%.*s\n", len
, sb
->set_name
);
542 static int match_home1(struct supertype
*st
, char *homehost
)
544 struct mdp_superblock_1
*sb
= st
->sb
;
545 int l
= homehost
? strlen(homehost
) : 0;
547 return (l
> 0 && l
< 32 &&
548 sb
->set_name
[l
] == ':' &&
549 strncmp(sb
->set_name
, homehost
, l
) == 0);
552 static void uuid_from_super1(struct supertype
*st
, int uuid
[4])
554 struct mdp_superblock_1
*super
= st
->sb
;
555 char *cuuid
= (char*)uuid
;
558 cuuid
[i
] = super
->set_uuid
[i
];
561 static void getinfo_super1(struct supertype
*st
, struct mdinfo
*info
, char *map
)
563 struct mdp_superblock_1
*sb
= st
->sb
;
567 unsigned int map_disks
= info
->array
.raid_disks
;
569 info
->array
.major_version
= 1;
570 info
->array
.minor_version
= st
->minor_version
;
571 info
->array
.patch_version
= 0;
572 info
->array
.raid_disks
= __le32_to_cpu(sb
->raid_disks
);
573 info
->array
.level
= __le32_to_cpu(sb
->level
);
574 info
->array
.layout
= __le32_to_cpu(sb
->layout
);
575 info
->array
.md_minor
= -1;
576 info
->array
.ctime
= __le64_to_cpu(sb
->ctime
);
577 info
->array
.utime
= __le64_to_cpu(sb
->utime
);
578 info
->array
.chunk_size
= __le32_to_cpu(sb
->chunksize
)*512;
580 (__le64_to_cpu(sb
->resync_offset
) >= __le64_to_cpu(sb
->size
))
583 info
->data_offset
= __le64_to_cpu(sb
->data_offset
);
584 info
->component_size
= __le64_to_cpu(sb
->size
);
586 info
->disk
.major
= 0;
587 info
->disk
.minor
= 0;
588 info
->disk
.number
= __le32_to_cpu(sb
->dev_number
);
589 if (__le32_to_cpu(sb
->dev_number
) >= __le32_to_cpu(sb
->max_dev
) ||
590 __le32_to_cpu(sb
->max_dev
) > 512)
593 role
= __le16_to_cpu(sb
->dev_roles
[__le32_to_cpu(sb
->dev_number
)]);
595 info
->disk
.raid_disk
= -1;
598 info
->disk
.state
= 0; /* spare: not active, not sync, not faulty */
601 info
->disk
.state
= 1; /* faulty */
604 info
->disk
.state
= 6; /* active and in sync */
605 info
->disk
.raid_disk
= role
;
607 info
->events
= __le64_to_cpu(sb
->events
);
608 sprintf(info
->text_version
, "1.%d", st
->minor_version
);
609 info
->safe_mode_delay
= 200;
611 memcpy(info
->uuid
, sb
->set_uuid
, 16);
613 strncpy(info
->name
, sb
->set_name
, 32);
616 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET
))
617 info
->recovery_start
= __le32_to_cpu(sb
->recovery_offset
);
619 info
->recovery_start
= MaxSector
;
621 if (sb
->feature_map
& __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE
)) {
622 info
->reshape_active
= 1;
623 info
->reshape_progress
= __le64_to_cpu(sb
->reshape_position
);
624 info
->new_level
= __le32_to_cpu(sb
->new_level
);
625 info
->delta_disks
= __le32_to_cpu(sb
->delta_disks
);
626 info
->new_layout
= __le32_to_cpu(sb
->new_layout
);
627 info
->new_chunk
= __le32_to_cpu(sb
->new_chunk
)<<9;
628 if (info
->delta_disks
< 0)
629 info
->array
.raid_disks
-= info
->delta_disks
;
631 info
->reshape_active
= 0;
634 for (i
=0; i
<map_disks
; i
++)
636 for (i
= 0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
637 role
= __le16_to_cpu(sb
->dev_roles
[i
]);
638 if (/*role == 0xFFFF || */role
< (unsigned) info
->array
.raid_disks
) {
640 if (map
&& role
< map_disks
)
645 info
->array
.working_disks
= working
;
648 static struct mdinfo
*container_content1(struct supertype
*st
, char *subarray
)
655 info
= malloc(sizeof(*info
));
656 getinfo_super1(st
, info
, NULL
);
660 static int update_super1(struct supertype
*st
, struct mdinfo
*info
,
662 char *devname
, int verbose
,
663 int uuid_set
, char *homehost
)
665 /* NOTE: for 'assemble' and 'force' we need to return non-zero
666 * if any change was made. For others, the return value is
670 struct mdp_superblock_1
*sb
= st
->sb
;
672 if (strcmp(update
, "force-one")==0) {
673 /* Not enough devices for a working array,
674 * so bring this one up-to-date
676 if (sb
->events
!= __cpu_to_le64(info
->events
))
678 sb
->events
= __cpu_to_le64(info
->events
);
679 } else if (strcmp(update
, "force-array")==0) {
680 /* Degraded array and 'force' requests to
681 * maybe need to mark it 'clean'.
683 switch(__le32_to_cpu(sb
->level
)) {
684 case 5: case 4: case 6:
685 /* need to force clean */
686 if (sb
->resync_offset
!= MaxSector
)
688 sb
->resync_offset
= MaxSector
;
690 } else if (strcmp(update
, "assemble")==0) {
691 int d
= info
->disk
.number
;
693 if (info
->disk
.state
== 6)
694 want
= info
->disk
.raid_disk
;
697 if (sb
->dev_roles
[d
] != __cpu_to_le16(want
)) {
698 sb
->dev_roles
[d
] = __cpu_to_le16(want
);
701 } else if (strcmp(update
, "linear-grow-new") == 0) {
704 unsigned int max
= __le32_to_cpu(sb
->max_dev
);
706 for (i
=0 ; i
< max
; i
++)
707 if (__le16_to_cpu(sb
->dev_roles
[i
]) >= 0xfffe)
709 sb
->dev_number
= __cpu_to_le32(i
);
710 info
->disk
.number
= i
;
711 if (max
>= __le32_to_cpu(sb
->max_dev
))
712 sb
->max_dev
= __cpu_to_le32(max
+1);
714 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
715 read(rfd
, sb
->device_uuid
, 16) != 16) {
716 __u32 r
[4] = {random(), random(), random(), random()};
717 memcpy(sb
->device_uuid
, r
, 16);
723 __cpu_to_le16(info
->disk
.raid_disk
);
725 fd
= open(devname
, O_RDONLY
);
727 unsigned long long ds
;
728 get_dev_size(fd
, devname
, &ds
);
731 if (__le64_to_cpu(sb
->super_offset
) <
732 __le64_to_cpu(sb
->data_offset
)) {
733 sb
->data_size
= __cpu_to_le64(
734 ds
- __le64_to_cpu(sb
->data_offset
));
737 ds
&= ~(unsigned long long)(4*2-1);
738 sb
->super_offset
= __cpu_to_le64(ds
);
739 sb
->data_size
= __cpu_to_le64(
740 ds
- __le64_to_cpu(sb
->data_offset
));
743 } else if (strcmp(update
, "linear-grow-update") == 0) {
744 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
745 sb
->dev_roles
[info
->disk
.number
] =
746 __cpu_to_le16(info
->disk
.raid_disk
);
747 } else if (strcmp(update
, "resync") == 0) {
748 /* make sure resync happens */
749 sb
->resync_offset
= 0ULL;
750 } else if (strcmp(update
, "uuid") == 0) {
751 copy_uuid(sb
->set_uuid
, info
->uuid
, super1
.swapuuid
);
753 if (__le32_to_cpu(sb
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
754 struct bitmap_super_s
*bm
;
755 bm
= (struct bitmap_super_s
*)(st
->sb
+1024);
756 memcpy(bm
->uuid
, sb
->set_uuid
, 16);
758 } else if (strcmp(update
, "no-bitmap") == 0) {
759 sb
->feature_map
&= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
);
760 } else if (strcmp(update
, "homehost") == 0 &&
764 c
= strchr(sb
->set_name
, ':');
766 strncpy(info
->name
, c
+1, 31 - (c
-sb
->set_name
));
768 strncpy(info
->name
, sb
->set_name
, 32);
770 } else if (strcmp(update
, "name") == 0) {
771 if (info
->name
[0] == 0)
772 sprintf(info
->name
, "%d", info
->array
.md_minor
);
773 memset(sb
->set_name
, 0, sizeof(sb
->set_name
));
775 strchr(info
->name
, ':') == NULL
&&
776 strlen(homehost
)+1+strlen(info
->name
) < 32) {
777 strcpy(sb
->set_name
, homehost
);
778 strcat(sb
->set_name
, ":");
779 strcat(sb
->set_name
, info
->name
);
781 strcpy(sb
->set_name
, info
->name
);
782 } else if (strcmp(update
, "devicesize") == 0 &&
783 __le64_to_cpu(sb
->super_offset
) <
784 __le64_to_cpu(sb
->data_offset
)) {
785 /* set data_size to device size less data_offset */
786 struct misc_dev_info
*misc
= (struct misc_dev_info
*)
787 (st
->sb
+ 1024 + 512);
788 printf("Size was %llu\n", (unsigned long long)
789 __le64_to_cpu(sb
->data_size
));
790 sb
->data_size
= __cpu_to_le64(
791 misc
->device_size
- __le64_to_cpu(sb
->data_offset
));
792 printf("Size is %llu\n", (unsigned long long)
793 __le64_to_cpu(sb
->data_size
));
794 } else if (strcmp(update
, "_reshape_progress")==0)
795 sb
->reshape_position
= __cpu_to_le64(info
->reshape_progress
);
799 sb
->sb_csum
= calc_sb_1_csum(sb
);
803 static int init_super1(struct supertype
*st
, mdu_array_info_t
*info
,
804 unsigned long long size
, char *name
, char *homehost
, int *uuid
)
806 struct mdp_superblock_1
*sb
;
811 if (posix_memalign((void**)&sb
, 512, (1024 + 512 +
812 sizeof(struct misc_dev_info
))) != 0) {
814 ": %s could not allocate superblock\n", __func__
);
821 /* zeroing superblock */
825 spares
= info
->working_disks
- info
->active_disks
;
826 if (info
->raid_disks
+ spares
> 384) {
827 fprintf(stderr
, Name
": too many devices requested: %d+%d > %d\n",
828 info
->raid_disks
, spares
, 384);
832 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
833 sb
->major_version
= __cpu_to_le32(1);
838 copy_uuid(sb
->set_uuid
, uuid
, super1
.swapuuid
);
840 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
841 read(rfd
, sb
->set_uuid
, 16) != 16) {
842 __u32 r
[4] = {random(), random(), random(), random()};
843 memcpy(sb
->set_uuid
, r
, 16);
845 if (rfd
>= 0) close(rfd
);
848 if (name
== NULL
|| *name
== 0) {
849 sprintf(defname
, "%d", info
->md_minor
);
852 memset(sb
->set_name
, 0, 32);
854 strchr(name
, ':')== NULL
&&
855 strlen(homehost
)+1+strlen(name
) < 32) {
856 strcpy(sb
->set_name
, homehost
);
857 strcat(sb
->set_name
, ":");
858 strcat(sb
->set_name
, name
);
860 strcpy(sb
->set_name
, name
);
862 sb
->ctime
= __cpu_to_le64((unsigned long long)time(0));
863 sb
->level
= __cpu_to_le32(info
->level
);
864 sb
->layout
= __cpu_to_le32(info
->layout
);
865 sb
->size
= __cpu_to_le64(size
*2ULL);
866 sb
->chunksize
= __cpu_to_le32(info
->chunk_size
>>9);
867 sb
->raid_disks
= __cpu_to_le32(info
->raid_disks
);
869 sb
->data_offset
= __cpu_to_le64(0);
870 sb
->data_size
= __cpu_to_le64(0);
871 sb
->super_offset
= __cpu_to_le64(0);
872 sb
->recovery_offset
= __cpu_to_le64(0);
874 sb
->utime
= sb
->ctime
;
875 sb
->events
= __cpu_to_le64(1);
876 if (info
->state
& (1<<MD_SB_CLEAN
))
877 sb
->resync_offset
= MaxSector
;
879 sb
->resync_offset
= 0;
880 sb
->max_dev
= __cpu_to_le32((1024- sizeof(struct mdp_superblock_1
))/
881 sizeof(sb
->dev_roles
[0]));
882 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
884 memset(sb
->dev_roles
, 0xff, 1024 - sizeof(struct mdp_superblock_1
));
892 mdu_disk_info_t disk
;
893 struct devinfo
*next
;
896 /* Add a device to the superblock being created */
897 static int add_to_super1(struct supertype
*st
, mdu_disk_info_t
*dk
,
898 int fd
, char *devname
)
900 struct mdp_superblock_1
*sb
= st
->sb
;
901 __u16
*rp
= sb
->dev_roles
+ dk
->number
;
902 struct devinfo
*di
, **dip
;
904 if ((dk
->state
& 6) == 6) /* active, sync */
905 *rp
= __cpu_to_le16(dk
->raid_disk
);
906 else if ((dk
->state
& ~2) == 0) /* active or idle -> spare */
911 if (dk
->number
>= (int)__le32_to_cpu(sb
->max_dev
) &&
912 __le32_to_cpu(sb
->max_dev
) < 384)
913 sb
->max_dev
= __cpu_to_le32(dk
->number
+1);
915 sb
->dev_number
= __cpu_to_le32(dk
->number
);
916 sb
->sb_csum
= calc_sb_1_csum(sb
);
918 dip
= (struct devinfo
**)&st
->info
;
921 di
= malloc(sizeof(struct devinfo
));
923 di
->devname
= devname
;
932 static void locate_bitmap1(struct supertype
*st
, int fd
);
934 static int store_super1(struct supertype
*st
, int fd
)
936 struct mdp_superblock_1
*sb
= st
->sb
;
937 unsigned long long sb_offset
;
939 unsigned long long dsize
;
941 if (!get_dev_size(fd
, NULL
, &dsize
))
950 * Calculate the position of the superblock.
951 * It is always aligned to a 4K boundary and
952 * depending on minor_version, it can be:
953 * 0: At least 8K, but less than 12K, from end of device
954 * 1: At start of device
955 * 2: 4K from start of device.
957 switch(st
->minor_version
) {
961 sb_offset
&= ~(4*2-1);
975 if (sb_offset
!= __le64_to_cpu(sb
->super_offset
) &&
976 0 != __le64_to_cpu(sb
->super_offset
)
978 fprintf(stderr
, Name
": internal error - sb_offset is wrong\n");
982 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL)
985 sbsize
= sizeof(*sb
) + 2 * __le32_to_cpu(sb
->max_dev
);
986 sbsize
= (sbsize
+511)&(~511UL);
988 if (awrite(fd
, sb
, sbsize
) != sbsize
)
991 if (sb
->feature_map
& __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET
)) {
992 struct bitmap_super_s
*bm
= (struct bitmap_super_s
*)
994 if (__le32_to_cpu(bm
->magic
) == BITMAP_MAGIC
) {
995 locate_bitmap1(st
, fd
);
996 if (awrite(fd
, bm
, sizeof(*bm
)) !=
1005 static int load_super1(struct supertype
*st
, int fd
, char *devname
);
1007 static unsigned long choose_bm_space(unsigned long devsize
)
1009 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1010 * if bigger than 200Gig, save 128k
1011 * NOTE: result must be multiple of 4K else bad things happen
1012 * on 4K-sector devices.
1014 if (devsize
< 64*2) return 0;
1015 if (devsize
- 64*2 >= 200*1024*1024*2)
1017 if (devsize
- 4*2 > 8*1024*1024*2)
1023 static int write_init_super1(struct supertype
*st
)
1025 struct mdp_superblock_1
*sb
= st
->sb
;
1026 struct supertype refst
;
1029 unsigned long long bm_space
;
1030 unsigned long long reserved
;
1032 unsigned long long dsize
, array_size
;
1033 unsigned long long sb_offset
;
1035 for (di
= st
->info
; di
&& ! rv
; di
= di
->next
) {
1036 if (di
->disk
.state
== 1)
1041 while (Kill(di
->devname
, NULL
, 0, 1, 1) == 0)
1044 sb
->dev_number
= __cpu_to_le32(di
->disk
.number
);
1045 if (di
->disk
.state
& (1<<MD_DISK_WRITEMOSTLY
))
1046 sb
->devflags
|= __cpu_to_le32(WriteMostly1
);
1048 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
1049 read(rfd
, sb
->device_uuid
, 16) != 16) {
1050 __u32 r
[4] = {random(), random(), random(), random()};
1051 memcpy(sb
->device_uuid
, r
, 16);
1060 if (load_super1(&refst
, di
->fd
, NULL
)==0) {
1061 struct mdp_superblock_1
*refsb
= refst
.sb
;
1063 memcpy(sb
->device_uuid
, refsb
->device_uuid
, 16);
1064 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16)==0) {
1065 /* same array, so preserve events and
1067 sb
->events
= refsb
->events
;
1068 /* bugs in 2.6.17 and earlier mean the
1069 * dev_number chosen in Manage must be preserved
1071 if (get_linux_version() >= 2006018)
1072 sb
->dev_number
= refsb
->dev_number
;
1077 if (!get_dev_size(di
->fd
, NULL
, &dsize
))
1088 * Calculate the position of the superblock.
1089 * It is always aligned to a 4K boundary and
1090 * depending on minor_version, it can be:
1091 * 0: At least 8K, but less than 12K, from end of device
1092 * 1: At start of device
1093 * 2: 4K from start of device.
1094 * Depending on the array size, we might leave extra space
1097 array_size
= __le64_to_cpu(sb
->size
);
1098 /* work out how much space we left for a bitmap */
1099 bm_space
= choose_bm_space(array_size
);
1101 switch(st
->minor_version
) {
1105 sb_offset
&= ~(4*2-1);
1106 sb
->super_offset
= __cpu_to_le64(sb_offset
);
1107 sb
->data_offset
= __cpu_to_le64(0);
1108 if (sb_offset
< array_size
+ bm_space
)
1109 bm_space
= sb_offset
- array_size
;
1110 sb
->data_size
= __cpu_to_le64(sb_offset
- bm_space
);
1113 sb
->super_offset
= __cpu_to_le64(0);
1114 reserved
= bm_space
+ 4*2;
1115 /* Try for multiple of 1Meg so it is nicely aligned */
1116 #define ONE_MEG (2*1024)
1117 reserved
= ((reserved
+ ONE_MEG
-1)/ONE_MEG
) * ONE_MEG
;
1118 if (reserved
+ __le64_to_cpu(sb
->size
) > dsize
)
1119 reserved
= dsize
- __le64_to_cpu(sb
->size
);
1120 /* force 4K alignment */
1123 sb
->data_offset
= __cpu_to_le64(reserved
);
1124 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1128 sb
->super_offset
= __cpu_to_le64(4*2);
1129 if (4*2 + 4*2 + bm_space
+ __le64_to_cpu(sb
->size
)
1131 bm_space
= dsize
- __le64_to_cpu(sb
->size
)
1134 reserved
= bm_space
+ 4*2 + 4*2;
1135 /* Try for multiple of 1Meg so it is nicely aligned */
1136 #define ONE_MEG (2*1024)
1137 reserved
= ((reserved
+ ONE_MEG
-1)/ONE_MEG
) * ONE_MEG
;
1138 if (reserved
+ __le64_to_cpu(sb
->size
) > dsize
)
1139 reserved
= dsize
- __le64_to_cpu(sb
->size
);
1140 /* force 4K alignment */
1143 sb
->data_offset
= __cpu_to_le64(reserved
);
1144 sb
->data_size
= __cpu_to_le64(dsize
- reserved
);
1151 sb
->sb_csum
= calc_sb_1_csum(sb
);
1152 rv
= store_super1(st
, di
->fd
);
1155 Name
": failed to write superblock to %s\n",
1158 if (rv
== 0 && (__le32_to_cpu(sb
->feature_map
) & 1))
1159 rv
= st
->ss
->write_bitmap(st
, di
->fd
);
1167 static int compare_super1(struct supertype
*st
, struct supertype
*tst
)
1171 * 0 same, or first was empty, and second was copied
1172 * 1 second had wrong number
1174 * 3 wrong other info
1176 struct mdp_superblock_1
*first
= st
->sb
;
1177 struct mdp_superblock_1
*second
= tst
->sb
;
1179 if (second
->magic
!= __cpu_to_le32(MD_SB_MAGIC
))
1181 if (second
->major_version
!= __cpu_to_le32(1))
1185 if (posix_memalign((void**)&first
, 512,
1187 sizeof(struct misc_dev_info
)) != 0) {
1188 fprintf(stderr
, Name
1189 ": %s could not allocate superblock\n", __func__
);
1192 memcpy(first
, second
, 1024 + 512 +
1193 sizeof(struct misc_dev_info
));
1197 if (memcmp(first
->set_uuid
, second
->set_uuid
, 16)!= 0)
1200 if (first
->ctime
!= second
->ctime
||
1201 first
->level
!= second
->level
||
1202 first
->layout
!= second
->layout
||
1203 first
->size
!= second
->size
||
1204 first
->chunksize
!= second
->chunksize
||
1205 first
->raid_disks
!= second
->raid_disks
)
1210 static void free_super1(struct supertype
*st
);
1212 static int load_super1(struct supertype
*st
, int fd
, char *devname
)
1214 unsigned long long dsize
;
1215 unsigned long long sb_offset
;
1216 struct mdp_superblock_1
*super
;
1218 struct bitmap_super_s
*bsb
;
1219 struct misc_dev_info
*misc
;
1223 if (st
->ss
== NULL
|| st
->minor_version
== -1) {
1225 struct supertype tst
;
1226 __u64 bestctime
= 0;
1227 /* guess... choose latest ctime */
1228 memset(&tst
, 0, sizeof(tst
));
1230 for (tst
.minor_version
= 0; tst
.minor_version
<= 2 ; tst
.minor_version
++) {
1231 switch(load_super1(&tst
, fd
, devname
)) {
1232 case 0: super
= tst
.sb
;
1233 if (bestvers
== -1 ||
1234 bestctime
< __le64_to_cpu(super
->ctime
)) {
1235 bestvers
= tst
.minor_version
;
1236 bestctime
= __le64_to_cpu(super
->ctime
);
1241 case 1: return 1; /*bad device */
1242 case 2: break; /* bad, try next */
1245 if (bestvers
!= -1) {
1247 tst
.minor_version
= bestvers
;
1250 rv
= load_super1(&tst
, fd
, devname
);
1257 if (!get_dev_size(fd
, devname
, &dsize
))
1263 fprintf(stderr
, Name
": %s is too small for md: size is %llu sectors.\n",
1269 * Calculate the position of the superblock.
1270 * It is always aligned to a 4K boundary and
1271 * depending on minor_version, it can be:
1272 * 0: At least 8K, but less than 12K, from end of device
1273 * 1: At start of device
1274 * 2: 4K from start of device.
1276 switch(st
->minor_version
) {
1280 sb_offset
&= ~(4*2-1);
1292 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
1295 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL) {
1297 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
1298 devname
, strerror(errno
));
1302 if (posix_memalign((void**)&super
, 512,
1304 sizeof(struct misc_dev_info
)) != 0) {
1305 fprintf(stderr
, Name
": %s could not allocate superblock\n",
1310 if (aread(fd
, super
, 1024) != 1024) {
1312 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
1318 if (__le32_to_cpu(super
->magic
) != MD_SB_MAGIC
) {
1320 fprintf(stderr
, Name
": No super block found on %s (Expected magic %08x, got %08x)\n",
1321 devname
, MD_SB_MAGIC
, __le32_to_cpu(super
->magic
));
1326 if (__le32_to_cpu(super
->major_version
) != 1) {
1328 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
1329 devname
, __le32_to_cpu(super
->major_version
));
1333 if (__le64_to_cpu(super
->super_offset
) != sb_offset
) {
1335 fprintf(stderr
, Name
": No superblock found on %s (super_offset is wrong)\n",
1342 bsb
= (struct bitmap_super_s
*)(((char*)super
)+1024);
1344 misc
= (struct misc_dev_info
*) (((char*)super
)+1024+512);
1345 misc
->device_size
= dsize
;
1347 /* Now check on the bitmap superblock */
1348 if ((__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) == 0)
1350 /* Read the bitmap superblock and make sure it looks
1351 * valid. If it doesn't clear the bit. An --assemble --force
1352 * should get that written out.
1354 locate_bitmap1(st
, fd
);
1355 if (aread(fd
, ((char*)super
)+1024, 512)
1359 uuid_from_super1(st
, uuid
);
1360 if (__le32_to_cpu(bsb
->magic
) != BITMAP_MAGIC
||
1361 memcmp(bsb
->uuid
, uuid
, 16) != 0)
1366 super
->feature_map
= __cpu_to_le32(__le32_to_cpu(super
->feature_map
) & ~1);
1371 static struct supertype
*match_metadata_desc1(char *arg
)
1373 struct supertype
*st
= malloc(sizeof(*st
));
1376 memset(st
, 0, sizeof(*st
));
1377 st
->container_dev
= NoMdDev
;
1381 /* leading zeros can be safely ignored. --detail generates them. */
1384 if (strcmp(arg
, "1.0") == 0 ||
1385 strcmp(arg
, "1.00") == 0) {
1386 st
->minor_version
= 0;
1389 if (strcmp(arg
, "1.1") == 0 ||
1390 strcmp(arg
, "1.01") == 0
1392 st
->minor_version
= 1;
1395 if (strcmp(arg
, "1.2") == 0 ||
1396 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1397 strcmp(arg
, "default") == 0 ||
1398 #endif /* DEFAULT_OLD_METADATA */
1399 strcmp(arg
, "1.02") == 0) {
1400 st
->minor_version
= 2;
1403 if (strcmp(arg
, "1") == 0 ||
1404 strcmp(arg
, "default") == 0) {
1405 st
->minor_version
= -1;
1413 /* find available size on device with this devsize, using
1414 * superblock type st, and reserving 'reserve' sectors for
1417 static __u64
avail_size1(struct supertype
*st
, __u64 devsize
)
1419 struct mdp_superblock_1
*super
= st
->sb
;
1424 /* creating: allow suitable space for bitmap */
1425 devsize
-= choose_bm_space(devsize
);
1427 else if (__le32_to_cpu(super
->feature_map
)&MD_FEATURE_BITMAP_OFFSET
) {
1428 /* hot-add. allow for actual size of bitmap */
1429 struct bitmap_super_s
*bsb
;
1430 bsb
= (struct bitmap_super_s
*)(((char*)super
)+1024);
1431 devsize
-= bitmap_sectors(bsb
);
1435 if (st
->minor_version
< 0)
1436 /* not specified, so time to set default */
1437 st
->minor_version
= 2;
1438 if (super
== NULL
&& st
->minor_version
> 0) {
1439 /* haven't committed to a size yet, so allow some
1440 * slack for alignment of data_offset.
1441 * We haven't access to device details so allow
1442 * 1 Meg if bigger than 1Gig
1444 if (devsize
> 1024*1024*2)
1447 switch(st
->minor_version
) {
1450 return ((devsize
- 8*2 ) & ~(4*2-1));
1452 /* at start, 4K for superblock and possible bitmap */
1453 return devsize
- 4*2;
1455 /* 4k from start, 4K for superblock and possible bitmap */
1456 return devsize
- (4+4)*2;
1462 add_internal_bitmap1(struct supertype
*st
,
1463 int *chunkp
, int delay
, int write_behind
,
1464 unsigned long long size
,
1465 int may_change
, int major
)
1468 * If not may_change, then this is a 'Grow', and the bitmap
1469 * must fit after the superblock.
1470 * If may_change, then this is create, and we can put the bitmap
1471 * before the superblock if we like, or may move the start.
1472 * If !may_change, the bitmap MUST live at offset of 1K, until
1473 * we get a sysfs interface.
1475 * size is in sectors, chunk is in bytes !!!
1478 unsigned long long bits
;
1479 unsigned long long max_bits
;
1480 unsigned long long min_chunk
;
1482 unsigned long long chunk
= *chunkp
;
1484 struct mdp_superblock_1
*sb
= st
->sb
;
1485 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + 1024);
1487 switch(st
->minor_version
) {
1489 /* either 3K after the superblock (when hot-add),
1490 * or some amount of space before.
1493 /* We are creating array, so we *know* how much room has
1497 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1499 room
= __le64_to_cpu(sb
->super_offset
)
1500 - __le64_to_cpu(sb
->data_offset
)
1501 - __le64_to_cpu(sb
->data_size
);
1502 /* remove '1 ||' when we can set offset via sysfs */
1503 if (1 || (room
< 3*2 &&
1504 __le32_to_cpu(sb
->max_dev
) <= 384)) {
1508 offset
= 0; /* means movable offset */
1513 case 2: /* between superblock and data */
1516 room
= choose_bm_space(__le64_to_cpu(sb
->size
));
1518 room
= __le64_to_cpu(sb
->data_offset
)
1519 - __le64_to_cpu(sb
->super_offset
);
1520 if (1 || __le32_to_cpu(sb
->max_dev
) <= 384) {
1533 if (chunk
== UnSet
&& room
> 128*2)
1534 /* Limit to 128K of bitmap when chunk size not requested */
1537 max_bits
= (room
* 512 - sizeof(bitmap_super_t
)) * 8;
1539 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
1540 bits
= (size
*512)/min_chunk
+1;
1541 while (bits
> max_bits
) {
1545 if (chunk
== UnSet
) {
1546 /* For practical purpose, 64Meg is a good
1547 * default chunk size for internal bitmaps.
1550 if (chunk
< 64*1024*1024)
1551 chunk
= 64*1024*1024;
1552 } else if (chunk
< min_chunk
)
1553 return 0; /* chunk size too small */
1554 if (chunk
== 0) /* rounding problem */
1558 /* start bitmap on a 4K boundary with enough space for
1561 bits
= (size
*512) / chunk
+ 1;
1562 room
= ((bits
+7)/8 + sizeof(bitmap_super_t
) +4095)/4096;
1563 room
*= 8; /* convert 4K blocks to sectors */
1567 sb
->bitmap_offset
= __cpu_to_le32(offset
);
1569 sb
->feature_map
= __cpu_to_le32(__le32_to_cpu(sb
->feature_map
) | 1);
1570 memset(bms
, 0, sizeof(*bms
));
1571 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
1572 bms
->version
= __cpu_to_le32(major
);
1573 uuid_from_super1(st
, (int*)bms
->uuid
);
1574 bms
->chunksize
= __cpu_to_le32(chunk
);
1575 bms
->daemon_sleep
= __cpu_to_le32(delay
);
1576 bms
->sync_size
= __cpu_to_le64(size
);
1577 bms
->write_behind
= __cpu_to_le32(write_behind
);
1584 static void locate_bitmap1(struct supertype
*st
, int fd
)
1586 unsigned long long offset
;
1587 struct mdp_superblock_1
*sb
;
1591 if (st
->ss
->load_super(st
, fd
, NULL
))
1592 return; /* no error I hope... */
1597 offset
= __le64_to_cpu(sb
->super_offset
);
1598 offset
+= (int32_t) __le32_to_cpu(sb
->bitmap_offset
);
1601 lseek64(fd
, offset
<<9, 0);
1604 static int write_bitmap1(struct supertype
*st
, int fd
)
1606 struct mdp_superblock_1
*sb
= st
->sb
;
1607 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
)+1024);
1611 char *buf
= (char*)(((long)(abuf
+4096))&~4095UL);
1613 locate_bitmap1(st
, fd
);
1615 memset(buf
, 0xff, 4096);
1616 memcpy(buf
, ((char*)sb
)+1024, sizeof(bitmap_super_t
));
1618 towrite
= __le64_to_cpu(bms
->sync_size
) / (__le32_to_cpu(bms
->chunksize
)>>9);
1619 towrite
= (towrite
+7) >> 3; /* bits to bytes */
1620 towrite
+= sizeof(bitmap_super_t
);
1621 towrite
= ROUND_UP(towrite
, 512);
1622 while (towrite
> 0) {
1626 n
= write(fd
, buf
, n
);
1631 memset(buf
, 0xff, 4096);
1640 static void free_super1(struct supertype
*st
)
1648 static int validate_geometry1(struct supertype
*st
, int level
,
1649 int layout
, int raiddisks
,
1650 int chunk
, unsigned long long size
,
1651 char *subdev
, unsigned long long *freesize
,
1654 unsigned long long ldsize
;
1657 if (level
== LEVEL_CONTAINER
) {
1659 fprintf(stderr
, Name
": 1.x metadata does not support containers\n");
1665 fd
= open(subdev
, O_RDONLY
|O_EXCL
, 0);
1668 fprintf(stderr
, Name
": super1.x cannot open %s: %s\n",
1669 subdev
, strerror(errno
));
1673 if (!get_dev_size(fd
, subdev
, &ldsize
)) {
1679 *freesize
= avail_size1(st
, ldsize
>> 9);
1682 #endif /* MDASSEMBLE */
1684 struct superswitch super1
= {
1686 .examine_super
= examine_super1
,
1687 .brief_examine_super
= brief_examine_super1
,
1688 .export_examine_super
= export_examine_super1
,
1689 .detail_super
= detail_super1
,
1690 .brief_detail_super
= brief_detail_super1
,
1691 .export_detail_super
= export_detail_super1
,
1692 .write_init_super
= write_init_super1
,
1693 .validate_geometry
= validate_geometry1
,
1694 .add_to_super
= add_to_super1
,
1696 .match_home
= match_home1
,
1697 .uuid_from_super
= uuid_from_super1
,
1698 .getinfo_super
= getinfo_super1
,
1699 .container_content
= container_content1
,
1700 .update_super
= update_super1
,
1701 .init_super
= init_super1
,
1702 .store_super
= store_super1
,
1703 .compare_super
= compare_super1
,
1704 .load_super
= load_super1
,
1705 .match_metadata_desc
= match_metadata_desc1
,
1706 .avail_size
= avail_size1
,
1707 .add_internal_bitmap
= add_internal_bitmap1
,
1708 .locate_bitmap
= locate_bitmap1
,
1709 .write_bitmap
= write_bitmap1
,
1710 .free_super
= free_super1
,
1711 #if __BYTE_ORDER == BIG_ENDIAN