]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2004 Neil Brown <neilb@cse.unsw.edu.au>
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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
32 #include "asm/byteorder.h"
34 * The version-1 superblock :
35 * All numeric fields are little-endian.
37 * total size: 256 bytes plus 2 per device.
38 * 1K allows 384 devices.
40 struct mdp_superblock_1
{
41 /* constant array information - 128 bytes */
42 __u32 magic
; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
43 __u32 major_version
; /* 1 */
44 __u32 feature_map
; /* 0 for now */
45 __u32 pad0
; /* always set to 0 when writing */
47 __u8 set_uuid
[16]; /* user-space generated. */
48 char set_name
[32]; /* set and interpreted by user-space */
50 __u64 ctime
; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
51 __u32 level
; /* -4 (multipath), -1 (linear), 0,1,4,5 */
52 __u32 layout
; /* only for raid5 currently */
53 __u64 size
; /* used size of component devices, in 512byte sectors */
55 __u32 chunksize
; /* in 512byte sectors */
57 __u32 bitmap_offset
; /* sectors after start of superblock that bitmap starts
58 * NOTE: signed, so bitmap can be before superblock
59 * only meaningful of feature_map[0] is set.
61 __u8 pad1
[128-100]; /* 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 devs[max_dev] */
80 __u32 max_dev
; /* size of devs[] 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 */
93 #define offsetof(t,f) ((int)&(((t*)0)->f))
95 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
97 unsigned int disk_csum
, csum
;
98 unsigned long long newcsum
;
99 int size
= sizeof(*sb
) + __le32_to_cpu(sb
->max_dev
)*2;
100 unsigned int *isuper
= (unsigned int*)sb
;
103 /* make sure I can count... */
104 if (offsetof(struct mdp_superblock_1
,data_offset
) != 128 ||
105 offsetof(struct mdp_superblock_1
, utime
) != 192 ||
106 sizeof(struct mdp_superblock_1
) != 256) {
107 fprintf(stderr
, "WARNING - superblock isn't sized correctly\n");
110 disk_csum
= sb
->sb_csum
;
113 for (i
=0; size
>=4; size
-= 4 )
114 newcsum
+= __le32_to_cpu(*isuper
++);
117 newcsum
+= __le16_to_cpu(*(unsigned short*) isuper
);
119 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
120 sb
->sb_csum
= disk_csum
;
125 static void examine_super1(void *sbv
)
127 struct mdp_superblock_1
*sb
= sbv
;
134 printf(" Magic : %08x\n", __le32_to_cpu(sb
->magic
));
135 printf(" Version : %02d.%02d\n", 1, __le32_to_cpu(sb
->feature_map
));
136 printf(" Array UUID : ");
137 for (i
=0; i
<16; i
++) {
138 if ((i
&3)==0 && i
!= 0) printf(":");
139 printf("%02x", sb
->set_uuid
[i
]);
142 printf(" Name : %.32s\n", sb
->set_name
);
144 atime
= __le64_to_cpu(sb
->ctime
) & 0xFFFFFFFFFFULL
;
145 printf(" Creation Time : %.24s\n", ctime(&atime
));
146 c
=map_num(pers
, __le32_to_cpu(sb
->level
));
147 printf(" Raid Level : %s\n", c
?c
:"-unknown-");
148 printf(" Raid Devices : %d\n", __le32_to_cpu(sb
->raid_disks
));
150 printf(" Device Size : %llu%s\n", (unsigned long long)sb
->data_size
, human_size(sb
->data_size
<<9));
152 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->data_offset
));
153 if (sb
->super_offset
)
154 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb
->super_offset
));
155 printf(" State : %s\n", (__le64_to_cpu(sb
->resync_offset
)+1)? "active":"clean");
156 printf(" Device UUID : ");
157 for (i
=0; i
<16; i
++) {
158 if ((i
&3)==0 && i
!= 0) printf(":");
159 printf("%02x", sb
->device_uuid
[i
]);
164 if (sb
->devflags
& WriteMostly1
)
165 printf(" write-mostly");
169 atime
= __le64_to_cpu(sb
->utime
) & 0xFFFFFFFFFFULL
;
170 printf(" Update Time : %.24s\n", ctime(&atime
));
172 if (calc_sb_1_csum(sb
) == sb
->sb_csum
)
173 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb
->sb_csum
));
175 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb
->sb_csum
),
176 __le32_to_cpu(calc_sb_1_csum(sb
)));
177 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb
->events
));
179 if (__le32_to_cpu(sb
->level
) == 5) {
180 c
= map_num(r5layout
, __le32_to_cpu(sb
->layout
));
181 printf(" Layout : %s\n", c
?c
:"-unknown-");
183 switch(__le32_to_cpu(sb
->level
)) {
187 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb
->chunksize
/2));
190 printf(" Rounding : %dK\n", __le32_to_cpu(sb
->chunksize
/2));
195 printf(" Array State : ");
196 for (d
=0; d
<__le32_to_cpu(sb
->raid_disks
); d
++) {
200 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
201 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
203 if (i
== __le32_to_cpu(sb
->dev_number
))
208 if (cnt
> 1) printf("?");
209 else if (cnt
== 1 && me
) printf("U");
210 else if (cnt
== 1) printf("u");
214 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
215 int role
= __le16_to_cpu(sb
->dev_roles
[i
]);
219 if (faulty
) printf(" %d failed", faulty
);
224 static void brief_examine_super1(void *sbv
)
226 struct mdp_superblock_1
*sb
= sbv
;
229 char *c
=map_num(pers
, __le32_to_cpu(sb
->level
));
231 printf("ARRAY /dev/?? level=%s metadata=1 num-devices=%d UUID=",
232 c
?c
:"-unknown-", sb
->raid_disks
);
233 for (i
=0; i
<16; i
++) {
234 printf("%02x", sb
->set_uuid
[i
]);
235 if ((i
&3)==0 && i
!= 0) printf(":");
238 printf(" name=%.32s", sb
->set_name
);
242 static void detail_super1(void *sbv
)
244 struct mdp_superblock_1
*sb
= sbv
;
247 printf(" Name : %.32s\n", sb
->set_name
);
249 for (i
=0; i
<16; i
++) {
250 if ((i
&3)==0 && i
!= 0) printf(":");
251 printf("%02x", sb
->set_uuid
[i
]);
253 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb
->events
));
256 static void brief_detail_super1(void *sbv
)
258 struct mdp_superblock_1
*sb
= sbv
;
262 printf(" name=%.32s", sb
->set_name
);
264 for (i
=0; i
<16; i
++) {
265 if ((i
&3)==0 && i
!= 0) printf(":");
266 printf("%02x", sb
->set_uuid
[i
]);
272 static void uuid_from_super1(int uuid
[4], void * sbv
)
274 struct mdp_superblock_1
*super
= sbv
;
275 char *cuuid
= (char*)uuid
;
278 cuuid
[i
] = super
->set_uuid
[i
];
281 static void getinfo_super1(struct mdinfo
*info
, mddev_ident_t ident
, void *sbv
)
283 struct mdp_superblock_1
*sb
= sbv
;
288 info
->array
.major_version
= 1;
289 info
->array
.minor_version
= __le32_to_cpu(sb
->feature_map
);
290 info
->array
.patch_version
= 0;
291 info
->array
.raid_disks
= __le32_to_cpu(sb
->raid_disks
);
292 info
->array
.level
= __le32_to_cpu(sb
->level
);
293 info
->array
.md_minor
= -1;
294 info
->array
.ctime
= __le64_to_cpu(sb
->ctime
);
296 info
->disk
.major
= 0;
297 info
->disk
.minor
= 0;
298 info
->disk
.number
= __le32_to_cpu(sb
->dev_number
);
299 if (__le32_to_cpu(sb
->dev_number
) >= __le32_to_cpu(sb
->max_dev
) ||
300 __le32_to_cpu(sb
->max_dev
) > 512)
303 role
= __le16_to_cpu(sb
->dev_roles
[__le32_to_cpu(sb
->dev_number
)]);
305 info
->disk
.raid_disk
= -1;
308 info
->disk
.state
= 2; /* spare: ACTIVE, not sync, not faulty */
311 info
->disk
.state
= 1; /* faulty */
314 info
->disk
.state
= 6; /* active and in sync */
315 info
->disk
.raid_disk
= role
;
317 info
->events
= __le64_to_cpu(sb
->events
);
319 memcpy(info
->uuid
, sb
->set_uuid
, 16);
321 strncpy(ident
->name
, sb
->set_name
, 32);
324 for (i
=0; i
< __le32_to_cpu(sb
->max_dev
); i
++) {
325 role
= __le16_to_cpu(sb
->dev_roles
[i
]);
326 if (/*role == 0xFFFF || */role
< info
->array
.raid_disks
)
330 info
->array
.working_disks
= working
;
333 static int update_super1(struct mdinfo
*info
, void *sbv
, char *update
, char *devname
, int verbose
)
336 struct mdp_superblock_1
*sb
= sbv
;
338 if (strcmp(update
, "force")==0) {
339 sb
->events
= __cpu_to_le32(info
->events
);
340 switch(__le32_to_cpu(sb
->level
)) {
341 case 5: case 4: case 6:
342 /* need to force clean */
343 sb
->resync_offset
= ~0ULL;
346 if (strcmp(update
, "assemble")==0) {
347 int d
= info
->disk
.number
;
349 if (info
->disk
.state
== 6)
350 want
= __cpu_to_le32(info
->disk
.raid_disk
);
353 if (sb
->dev_roles
[d
] != want
) {
354 sb
->dev_roles
[d
] = want
;
359 if (strcmp(update
, "newdev") == 0) {
360 int d
= info
->disk
.number
;
361 memset(&sb
->disks
[d
], 0, sizeof(sb
->disks
[d
]));
362 sb
->disks
[d
].number
= d
;
363 sb
->disks
[d
].major
= info
->disk
.major
;
364 sb
->disks
[d
].minor
= info
->disk
.minor
;
365 sb
->disks
[d
].raid_disk
= info
->disk
.raid_disk
;
366 sb
->disks
[d
].state
= info
->disk
.state
;
367 sb
->this_disk
= sb
->disks
[d
];
370 if (strcmp(update
, "grow") == 0) {
371 sb
->raid_disks
= __cpu_to_le32(info
->array
.raid_disks
);
374 if (strcmp(update
, "resync") == 0) {
375 /* make sure resync happens */
376 sb
->resync_offset
= ~0ULL;
379 sb
->sb_csum
= calc_sb_1_csum(sb
);
384 static __u64
event_super1(void *sbv
)
386 struct mdp_superblock_1
*sb
= sbv
;
387 return __le64_to_cpu(sb
->events
);
390 static int init_super1(struct supertype
*st
, void **sbp
, mdu_array_info_t
*info
, char *name
)
392 struct mdp_superblock_1
*sb
= malloc(1024 + sizeof(bitmap_super_t
));
397 if (info
->major_version
== -1)
398 /* zeroing superblock */
401 spares
= info
->working_disks
- info
->active_disks
;
402 if (info
->raid_disks
+ spares
> 384) {
403 fprintf(stderr
, Name
": too many devices requested: %d+%d > %d\n",
404 info
->raid_disks
, spares
, 384);
409 sb
->magic
= __cpu_to_le32(MD_SB_MAGIC
);
410 sb
->major_version
= __cpu_to_le32(1);
414 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
415 read(rfd
, sb
->set_uuid
, 16) != 16) {
416 *(__u32
*)(sb
->set_uuid
) = random();
417 *(__u32
*)(sb
->set_uuid
+4) = random();
418 *(__u32
*)(sb
->set_uuid
+8) = random();
419 *(__u32
*)(sb
->set_uuid
+12) = random();
421 if (rfd
>= 0) close(rfd
);
423 memset(sb
->set_name
, 0, 32);
424 strcpy(sb
->set_name
, name
);
426 sb
->ctime
= __cpu_to_le64((unsigned long long)time(0));
427 sb
->level
= __cpu_to_le32(info
->level
);
428 sb
->layout
= __cpu_to_le32(info
->layout
);
429 sb
->size
= __cpu_to_le64(info
->size
*2ULL);
430 sb
->chunksize
= __cpu_to_le32(info
->chunk_size
>>9);
431 sb
->raid_disks
= __cpu_to_le32(info
->raid_disks
);
433 sb
->data_offset
= __cpu_to_le64(0);
434 sb
->data_size
= __cpu_to_le64(0);
435 sb
->super_offset
= __cpu_to_le64(0);
436 sb
->recovery_offset
= __cpu_to_le64(0);
438 sb
->utime
= sb
->ctime
;
439 sb
->events
= __cpu_to_le64(1);
440 if (info
->state
& (1<<MD_SB_CLEAN
))
441 sb
->resync_offset
= ~0ULL;
443 sb
->resync_offset
= 0;
444 sb
->max_dev
= __cpu_to_le32((1024- sizeof(struct mdp_superblock_1
))/
445 sizeof(sb
->dev_roles
[0]));
446 memset(sb
->pad3
, 0, sizeof(sb
->pad3
));
448 memset(sb
->dev_roles
, 0xff, 1024 - sizeof(struct mdp_superblock_1
));
454 /* Add a device to the superblock being created */
455 static void add_to_super1(void *sbv
, mdu_disk_info_t
*dk
)
457 struct mdp_superblock_1
*sb
= sbv
;
458 __u16
*rp
= sb
->dev_roles
+ dk
->number
;
459 if ((dk
->state
& 6) == 6) /* active, sync */
460 *rp
= __cpu_to_le16(dk
->raid_disk
);
461 else if ((dk
->state
& ~2) == 0) /* active or idle -> spare */
467 static int store_super1(struct supertype
*st
, int fd
, void *sbv
)
469 struct mdp_superblock_1
*sb
= sbv
;
470 unsigned long long sb_offset
;
474 if (ioctl(fd
, BLKGETSIZE
, &size
))
482 * Calculate the position of the superblock.
483 * It is always aligned to a 4K boundary and
484 * depending on minor_version, it can be:
485 * 0: At least 8K, but less than 12K, from end of device
486 * 1: At start of device
487 * 2: 4K from start of device.
489 switch(st
->minor_version
) {
493 sb_offset
&= ~(4*2-1);
507 if (sb_offset
!= __le64_to_cpu(sb
->super_offset
) &&
508 0 != __le64_to_cpu(sb
->super_offset
)
510 fprintf(stderr
, Name
": internal error - sb_offset is wrong\n");
514 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL)
517 sbsize
= sizeof(*sb
) + 2 * __le32_to_cpu(sb
->max_dev
);
519 if (write(fd
, sb
, sbsize
) != sbsize
)
526 static int load_super1(struct supertype
*st
, int fd
, void **sbp
, char *devname
);
528 static int write_init_super1(struct supertype
*st
, void *sbv
,
529 mdu_disk_info_t
*dinfo
, char *devname
,
532 struct mdp_superblock_1
*sb
= sbv
;
533 struct mdp_superblock_1
*refsb
= NULL
;
534 int fd
= open(devname
, O_RDWR
| O_EXCL
);
543 fprintf(stderr
, Name
": Failed to open %s to write superblock\n",
548 sb
->dev_number
= __cpu_to_le32(dinfo
->number
);
549 if (dinfo
->state
& (1<<MD_DISK_WRITEMOSTLY
))
550 sb
->devflags
|= WriteMostly1
;
552 if ((rfd
= open("/dev/urandom", O_RDONLY
)) < 0 ||
553 read(rfd
, sb
->device_uuid
, 16) != 16) {
554 *(__u32
*)(sb
->device_uuid
) = random();
555 *(__u32
*)(sb
->device_uuid
+4) = random();
556 *(__u32
*)(sb
->device_uuid
+8) = random();
557 *(__u32
*)(sb
->device_uuid
+12) = random();
559 if (rfd
>= 0) close(rfd
);
562 if (load_super1(st
, fd
, (void**)&refsb
, NULL
)==0) {
563 memcpy(sb
->device_uuid
, refsb
->device_uuid
, 16);
564 if (memcmp(sb
->set_uuid
, refsb
->set_uuid
, 16)==0) {
565 /* same array, so preserve events and dev_number */
566 sb
->events
= refsb
->events
;
567 sb
->dev_number
= refsb
->dev_number
;
572 if (ioctl(fd
, BLKGETSIZE
, &size
)) {
584 * Calculate the position of the superblock.
585 * It is always aligned to a 4K boundary and
586 * depending on minor_version, it can be:
587 * 0: At least 8K, but less than 12K, from end of device
588 * 1: At start of device
589 * 2: 4K from start of device.
591 switch(st
->minor_version
) {
595 sb_offset
&= ~(4*2-1);
596 sb
->super_offset
= __cpu_to_le64(sb_offset
);
597 sb
->data_offset
= __cpu_to_le64(0);
598 sb
->data_size
= __cpu_to_le64(sb_offset
- reserve
);
601 sb
->super_offset
= __cpu_to_le64(0);
602 sb
->data_offset
= __cpu_to_le64(2 + reserve
);
603 sb
->data_size
= __cpu_to_le64(size
- 2 - reserve
);
607 sb
->super_offset
= __cpu_to_le64(sb_offset
);
608 sb
->data_offset
= __cpu_to_le64(sb_offset
+2 + reserve
);
609 sb
->data_size
= __cpu_to_le64(size
- 4*2 - 2 - reserve
);
616 sb
->sb_csum
= calc_sb_1_csum(sb
);
617 rv
= store_super1(st
, fd
, sb
);
619 fprintf(stderr
, Name
": failed to write superblock to %s\n", devname
);
621 if (__le32_to_cpu(sb
->feature_map
) & 1) {
622 /* write the bitmap */
626 st
->ss
->locate_bitmap(st
, fd
);
627 write(fd
, ((char*)sb
)+1024, sizeof(bitmap_super_t
));
629 memset(buf
, 0xff, sizeof(buf
));
630 while (towrite
> 0) {
634 n
= write(fd
, buf
, n
);
648 static int compare_super1(void **firstp
, void *secondv
)
652 * 0 same, or first was empty, and second was copied
653 * 1 second had wrong number
657 struct mdp_superblock_1
*first
= *firstp
;
658 struct mdp_superblock_1
*second
= secondv
;
660 if (second
->magic
!= __cpu_to_le32(MD_SB_MAGIC
))
662 if (second
->major_version
!= __cpu_to_le32(1))
666 first
= malloc(1024);
667 memcpy(first
, second
, 1024);
671 if (memcmp(first
->set_uuid
, second
->set_uuid
, 16)!= 0)
674 if (first
->ctime
!= second
->ctime
||
675 first
->level
!= second
->level
||
676 first
->layout
!= second
->layout
||
677 first
->size
!= second
->size
||
678 first
->chunksize
!= second
->chunksize
||
679 first
->raid_disks
!= second
->raid_disks
)
684 static int load_super1(struct supertype
*st
, int fd
, void **sbp
, char *devname
)
687 unsigned long long sb_offset
;
688 struct mdp_superblock_1
*super
;
692 if (st
->ss
== NULL
) {
695 /* guess... choose latest ctime */
697 for (st
->minor_version
= 0; st
->minor_version
<= 2 ; st
->minor_version
++) {
698 switch(load_super1(st
, fd
, sbp
, devname
)) {
699 case 0: super
= *sbp
;
700 if (bestvers
== -1 ||
701 bestctime
< __le64_to_cpu(super
->ctime
)) {
702 bestvers
= st
->minor_version
;
703 bestctime
= __le64_to_cpu(super
->ctime
);
708 case 1: st
->ss
= NULL
; return 1; /*bad device */
709 case 2: break; /* bad, try next */
712 if (bestvers
!= -1) {
714 st
->minor_version
= bestvers
;
717 rv
= load_super1(st
, fd
, sbp
, devname
);
718 if (rv
) st
->ss
= NULL
;
724 if (ioctl(fd
, BLKGETSIZE
, &size
)) {
726 fprintf(stderr
, Name
": cannot find device size for %s: %s\n",
727 devname
, strerror(errno
));
733 fprintf(stderr
, Name
": %s is too small for md: size is %lu sectors.\n",
739 * Calculate the position of the superblock.
740 * It is always aligned to a 4K boundary and
741 * depeding on minor_version, it can be:
742 * 0: At least 8K, but less than 12K, from end of device
743 * 1: At start of device
744 * 2: 4K from start of device.
746 switch(st
->minor_version
) {
750 sb_offset
&= ~(4*2-1);
762 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
765 if (lseek64(fd
, sb_offset
<< 9, 0)< 0LL) {
767 fprintf(stderr
, Name
": Cannot seek to superblock on %s: %s\n",
768 devname
, strerror(errno
));
772 super
= malloc(1024 + sizeof(bitmap_super_t
));
774 if (read(fd
, super
, 1024) != 1024) {
776 fprintf(stderr
, Name
": Cannot read superblock on %s\n",
782 if (__le32_to_cpu(super
->magic
) != MD_SB_MAGIC
) {
784 fprintf(stderr
, Name
": No super block found on %s (Expected magic %08x, got %08x)\n",
785 devname
, MD_SB_MAGIC
, __le32_to_cpu(super
->magic
));
790 if (__le32_to_cpu(super
->major_version
) != 1) {
792 fprintf(stderr
, Name
": Cannot interpret superblock on %s - version is %d\n",
793 devname
, __le32_to_cpu(super
->major_version
));
797 if (__le64_to_cpu(super
->super_offset
) != sb_offset
) {
799 fprintf(stderr
, Name
": No superblock found on %s (super_offset is wrong)\n",
809 static struct supertype
*match_metadata_desc1(char *arg
)
811 struct supertype
*st
= malloc(sizeof(*st
));
816 if (strcmp(arg
, "1") == 0 ||
817 strcmp(arg
, "1.0") == 0) {
818 st
->minor_version
= 0;
821 if (strcmp(arg
, "1.1") == 0) {
822 st
->minor_version
= 1;
825 if (strcmp(arg
, "1.2") == 0) {
826 st
->minor_version
= 2;
834 /* find available size on device with this devsize, using
835 * superblock type st, and reserving 'reserve' sectors for
838 static __u64
avail_size1(struct supertype
*st
, __u64 devsize
, int reserve
)
843 switch(st
->minor_version
) {
845 /* at end, with reserve before it */
846 return ((devsize
- 8*2 ) & ~(4*2-1)) - reserve
;
848 /* at start, 1K for superblock */
849 return devsize
- 2 - reserve
;
851 /* 4k from start, 1K for superblock */
852 return devsize
- (4+1)*2 - reserve
;
857 static int add_internal_bitmap1(struct supertype
*st
, void *sbv
, int chunk
, int delay
, int write_behind
, unsigned long long size
)
860 * The bitmap comes immediately before of after the superblock and must be 62K in size
861 * at most. The default size is between 31K and 62K
863 * size is in K, chunk is in bytes !!!
866 unsigned long long bits
= size
;
867 unsigned long long max_bits
= 62*1024*8;
868 unsigned long long min_chunk
;
869 struct mdp_superblock_1
*sb
= sbv
;
870 bitmap_super_t
*bms
= (bitmap_super_t
*)(((char*)sb
) + 1024);
873 min_chunk
= 4096; /* sub-page chunks don't work yet.. */
874 while (bits
> max_bits
) {
880 else if (chunk
< min_chunk
)
881 return 0; /* chunk size too small */
883 if (st
->minor_version
== 0)
884 sb
->bitmap_offset
= __cpu_to_le32(-64*2);
886 sb
->bitmap_offset
= __cpu_to_le32(2);
888 sb
->feature_map
= __cpu_to_le32(__le32_to_cpu(sb
->feature_map
) | 1);
889 memset(bms
, sizeof(*bms
), 0);
890 bms
->magic
= __cpu_to_le32(BITMAP_MAGIC
);
891 bms
->version
= __cpu_to_le32(BITMAP_MAJOR
);
892 uuid_from_super1((int*)bms
->uuid
, sb
);
893 bms
->chunksize
= __cpu_to_le32(chunk
);
894 bms
->daemon_sleep
= __cpu_to_le32(delay
);
895 bms
->sync_size
= __cpu_to_le64(size
);
896 bms
->write_behind
= __cpu_to_le32(write_behind
);
904 void locate_bitmap1(struct supertype
*st
, int fd
)
906 unsigned long long dsize
;
908 unsigned long long offset
;
910 switch(st
->minor_version
){
913 if (ioctl(fd
, BLKGETSIZE64
, &dsize
) != 0)
916 if (ioctl(fd
, BLKGETSIZE
, &size
))
919 dsize
= ((unsigned long long)size
)<<9;
922 offset
= (dsize
- 8192) & ~4095ULL;
933 lseek64(fd
, offset
, 0);
936 int write_bitmap1(struct supertype
*st
, int fd
, void *sbv
)
938 struct mdp_superblock_1
*sb
= sbv
;
945 locate_bitmap1(st
, fd
);
947 write(fd
, ((char*)sb
)+1024, sizeof(bitmap_super_t
));
948 towrite
= 62*1024 - sizeof(bitmap_super_t
);
949 memset(buf
, 0xff, sizeof(buf
));
950 while (towrite
> 0) {
954 n
= write(fd
, buf
, n
);
967 struct superswitch super1
= {
969 .examine_super
= examine_super1
,
970 .brief_examine_super
= brief_examine_super1
,
971 .detail_super
= detail_super1
,
972 .brief_detail_super
= brief_detail_super1
,
974 .uuid_from_super
= uuid_from_super1
,
975 .getinfo_super
= getinfo_super1
,
976 .update_super
= update_super1
,
977 .event_super
= event_super1
,
978 .init_super
= init_super1
,
979 .add_to_super
= add_to_super1
,
980 .store_super
= store_super1
,
981 .write_init_super
= write_init_super1
,
982 .compare_super
= compare_super1
,
983 .load_super
= load_super1
,
984 .match_metadata_desc
= match_metadata_desc1
,
985 .avail_size
= avail_size1
,
986 .add_internal_bitmap
= add_internal_bitmap1
,
987 .locate_bitmap
= locate_bitmap1
,
988 .write_bitmap
= write_bitmap1
,
990 #if __BYTE_ORDER == BIG_ENDIAN