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 #include <sys/socket.h>
28 #include <sys/utsname.h>
36 * following taken from linux/blkpg.h because they aren't
37 * anywhere else and it isn't safe to #include linux/ * stuff.
40 #define BLKPG _IO(0x12,105)
42 /* The argument structure */
43 struct blkpg_ioctl_arg
{
50 /* The subfunctions (for the op field) */
51 #define BLKPG_ADD_PARTITION 1
52 #define BLKPG_DEL_PARTITION 2
54 /* Sizes of name fields. Unused at present. */
55 #define BLKPG_DEVNAMELTH 64
56 #define BLKPG_VOLNAMELTH 64
58 /* The data structure for ADD_PARTITION and DEL_PARTITION */
59 struct blkpg_partition
{
60 long long start
; /* starting offset in bytes */
61 long long length
; /* length in bytes */
62 int pno
; /* partition number */
63 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
64 to be used in kernel messages */
65 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
70 /* Force a compilation error if condition is true */
71 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
73 /* Force a compilation error if condition is true, but also produce a
74 result (of value 0 and type size_t), so the expression can be used
75 e.g. in a structure initializer (or where-ever else comma expressions
77 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
80 * Parse a 128 bit uuid in 4 integers
81 * format is 32 hexx nibbles with options :.<space> separator
82 * If not exactly 32 hex digits are found, return 0
85 int parse_uuid(char *str
, int uuid
[4])
87 int hit
= 0; /* number of Hex digIT */
90 for (i
=0; i
<4; i
++) uuid
[i
]=0;
96 else if (c
>='a' && c
<= 'f')
98 else if (c
>='A' && c
<= 'F')
100 else if (strchr(":. -", c
))
117 * Get the md version number.
118 * We use the RAID_VERSION ioctl if it is supported
119 * If not, but we have a block device with major '9', we assume
122 * Return version number as 24 but number - assume version parts
126 int md_get_version(int fd
)
131 if (fstat(fd
, &stb
)<0)
133 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
136 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
137 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
140 if (major(stb
.st_rdev
) == MD_MAJOR
)
145 int get_linux_version()
154 a
= strtoul(cp
, &cp
, 10);
155 if (*cp
!= '.') return -1;
156 b
= strtoul(cp
+1, &cp
, 10);
157 if (*cp
!= '.') return -1;
158 c
= strtoul(cp
+1, NULL
, 10);
160 return (a
*1000000)+(b
*1000)+c
;
164 long long parse_size(char *size
)
166 /* parse 'size' which should be a number optionally
167 * followed by 'K', 'M', or 'G'.
168 * Without a suffix, K is assumed.
169 * Number returned is in sectors (half-K)
172 long long s
= strtoll(size
, &c
, 10);
186 s
*= 1024 * 1024 * 2;
195 int parse_layout_10(char *layout
)
199 /* Parse the layout string for raid10 */
200 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
201 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
202 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
206 if (layout
[0] == 'n')
208 else if (layout
[0] == 'o')
209 rv
= 0x10000 + (copies
<<8) + 1;
211 rv
= 1 + (copies
<<8);
215 int parse_layout_faulty(char *layout
)
217 /* Parse the layout string for 'faulty' */
218 int ln
= strcspn(layout
, "0123456789");
219 char *m
= strdup(layout
);
222 mode
= map_name(faultylayout
, m
);
226 return mode
| (atoi(layout
+ln
)<< ModeShift
);
230 void remove_partitions(int fd
)
232 /* remove partitions from this block devices.
233 * This is used for components added to an array
235 #ifdef BLKPG_DEL_PARTITION
236 struct blkpg_ioctl_arg a
;
237 struct blkpg_partition p
;
239 a
.op
= BLKPG_DEL_PARTITION
;
241 a
.datalen
= sizeof(p
);
243 memset(a
.data
, 0, a
.datalen
);
244 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
245 ioctl(fd
, BLKPG
, &a
);
249 int test_partition(int fd
)
251 /* Check if fd is a whole-disk or a partition.
252 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
253 * will return ENXIO on an invalid partition number.
255 struct blkpg_ioctl_arg a
;
256 struct blkpg_partition p
;
257 a
.op
= BLKPG_DEL_PARTITION
;
259 a
.datalen
= sizeof(p
);
261 memset(a
.data
, 0, a
.datalen
);
263 if (ioctl(fd
, BLKPG
, &a
) == 0)
264 /* Very unlikely, but not a partition */
267 /* not a partition */
274 int enough(int level
, int raid_disks
, int layout
, int clean
,
275 char *avail
, int avail_disks
)
280 /* This is the tricky one - we need to check
281 * which actual disks are present.
283 copies
= (layout
&255)* ((layout
>>8) & 255);
286 /* there must be one of the 'copies' form 'first' */
292 first
= (first
+1) % raid_disks
;
297 } while (first
!= 0);
300 case LEVEL_MULTIPATH
:
301 return avail_disks
>= 1;
304 return avail_disks
== raid_disks
;
306 return avail_disks
>= 1;
310 return avail_disks
>= raid_disks
-1;
312 return avail_disks
>= raid_disks
;
315 return avail_disks
>= raid_disks
-2;
317 return avail_disks
>= raid_disks
;
323 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
324 int same_uuid(int a
[4], int b
[4], int swapuuid
)
326 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
327 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
331 /* parse uuids are hostendian.
332 * uuid's from some superblocks are big-ending
333 * if there is a difference, we need to swap..
335 unsigned char *ac
= (unsigned char *)a
;
336 unsigned char *bc
= (unsigned char *)b
;
338 for (i
=0; i
<16; i
+= 4) {
339 if (ac
[i
+0] != bc
[i
+3] ||
340 ac
[i
+1] != bc
[i
+2] ||
341 ac
[i
+2] != bc
[i
+1] ||
355 void copy_uuid(void *a
, int b
[4], int swapuuid
)
358 /* parse uuids are hostendian.
359 * uuid's from some superblocks are big-ending
360 * if there is a difference, we need to swap..
362 unsigned char *ac
= (unsigned char *)a
;
363 unsigned char *bc
= (unsigned char *)b
;
365 for (i
=0; i
<16; i
+= 4) {
375 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
382 copy_uuid(uuid
, id
, swap
);
383 for (i
= 0; i
< 4; i
++) {
386 for (j
= 3; j
>= 0; j
--) {
387 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
395 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
397 // dirty hack to work around an issue with super1 superblocks...
398 // super1 superblocks need swapuuid set in order for assembly to
399 // work, but can't have it set if we want this printout to match
400 // all the other uuid printouts in super1.c, so we force swapuuid
401 // to 1 to make our printout match the rest of super1
402 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
406 int check_ext2(int fd
, char *name
)
409 * Check for an ext2fs file system.
410 * Superblock is always 1K at 1K offset
412 * s_magic is le16 at 56 == 0xEF53
413 * report mtime - le32 at 44
415 * logblksize - le32 at 24
417 unsigned char sb
[1024];
420 if (lseek(fd
, 1024,0)!= 1024)
422 if (read(fd
, sb
, 1024)!= 1024)
424 if (sb
[56] != 0x53 || sb
[57] != 0xef)
427 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
428 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
429 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
430 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
432 fprintf(stderr
," size=%dK mtime=%s",
433 size
*(1<<bsize
), ctime(&mtime
));
437 int check_reiser(int fd
, char *name
)
440 * superblock is at 64K
442 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
445 unsigned char sb
[1024];
447 if (lseek(fd
, 64*1024, 0) != 64*1024)
449 if (read(fd
, sb
, 1024) != 1024)
451 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
452 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
454 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
455 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
456 fprintf(stderr
, " size = %luK\n", size
*4);
461 int check_raid(int fd
, char *name
)
466 struct supertype
*st
= guess_super(fd
);
469 st
->ss
->load_super(st
, fd
, name
);
470 /* Looks like a raid array .. */
471 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
473 st
->ss
->getinfo_super(st
, &info
);
474 st
->ss
->free_super(st
);
475 crtime
= info
.array
.ctime
;
476 level
= map_num(pers
, info
.array
.level
);
477 if (!level
) level
= "-unknown-";
478 fprintf(stderr
, " level=%s devices=%d ctime=%s",
479 level
, info
.array
.raid_disks
, ctime(&crtime
));
487 for (i
=0; i
<5; i
++) {
489 fprintf(stderr
, "%s%s", mesg
, add
);
491 if (fgets(buf
, 100, stdin
)==NULL
)
493 if (buf
[0]=='y' || buf
[0]=='Y')
495 if (buf
[0]=='n' || buf
[0]=='N')
499 fprintf(stderr
, Name
": assuming 'no'\n");
502 #endif /* MDASSEMBLE */
504 char *map_num(mapping_t
*map
, int num
)
514 int map_name(mapping_t
*map
, char *name
)
517 if (strcmp(map
->name
, name
)==0)
525 int is_standard(char *dev
, int *nump
)
527 /* tests if dev is a "standard" md dev name.
528 * i.e if the last component is "/dNN" or "/mdNN",
529 * where NN is a string of digits
530 * Returns 1 if a partitionable standard,
531 * -1 if non-partitonable,
532 * 0 if not a standard name.
534 char *d
= strrchr(dev
, '/');
539 if (strncmp(d
, "/d",2)==0)
540 d
+= 2, type
=1; /* /dev/md/dN{pM} */
541 else if (strncmp(d
, "/md_d", 5)==0)
542 d
+= 5, type
=1; /* /dev/md_dN{pM} */
543 else if (strncmp(d
, "/md", 3)==0)
544 d
+= 3, type
=-1; /* /dev/mdN */
545 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
546 d
+= 1, type
=-1; /* /dev/md/N */
556 if (nump
) *nump
= num
;
563 * convert a major/minor pair for a block device into a name in /dev, if possible.
564 * On the first call, walk /dev collecting name.
565 * Put them in a simple linked listfor now.
572 int devlist_ready
= 0;
574 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
578 if (S_ISLNK(stb
->st_mode
)) {
579 if (stat(name
, &st
) != 0)
584 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
585 char *n
= strdup(name
);
586 struct devmap
*dm
= malloc(sizeof(*dm
));
587 if (strncmp(n
, "/dev/./", 7)==0)
590 dm
->major
= major(stb
->st_rdev
);
591 dm
->minor
= minor(stb
->st_rdev
);
602 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
604 return add_dev(name
, stb
, flag
, NULL
);
606 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
608 return ftw(path
, add_dev_1
, nopenfd
);
611 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
615 #endif /* HAVE_FTW */
616 #endif /* HAVE_NFTW */
619 * Find a block device with the right major/minor number.
620 * If we find multiple names, choose the shortest.
621 * If we find a name in /dev/md/, we prefer that.
622 * This applies only to names for MD devices.
624 char *map_dev(int major
, int minor
, int create
)
627 char *regular
= NULL
, *preferred
=NULL
;
630 if (major
== 0 && minor
== 0)
634 if (!devlist_ready
) {
638 struct devmap
*d
= devlist
;
643 if (lstat(dev
, &stb
)==0 &&
644 S_ISLNK(stb
.st_mode
))
646 nftw(dev
, add_dev
, 10, FTW_PHYS
);
651 for (p
=devlist
; p
; p
=p
->next
)
652 if (p
->major
== major
&&
654 if (strncmp(p
->name
, "/dev/md/",8) == 0) {
655 if (preferred
== NULL
||
656 strlen(p
->name
) < strlen(preferred
))
659 if (regular
== NULL
||
660 strlen(p
->name
) < strlen(regular
))
664 if (!regular
&& !preferred
&& !did_check
) {
668 if (create
&& !regular
&& !preferred
) {
670 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
674 return preferred
? preferred
: regular
;
677 unsigned long calc_csum(void *super
, int bytes
)
679 unsigned long long newcsum
= 0;
682 unsigned int *superc
= (unsigned int*) super
;
684 for(i
=0; i
<bytes
/4; i
++)
686 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
688 /* The in-kernel checksum calculation is always 16bit on
689 * the alpha, though it is 32 bit on i386...
690 * I wonder what it is elsewhere... (it uses and API in
691 * a way that it shouldn't).
693 csum
= (csum
& 0xffff) + (csum
>> 16);
694 csum
= (csum
& 0xffff) + (csum
>> 16);
700 char *human_size(long long bytes
)
704 /* We convert bytes to either centi-M{ega,ibi}bytes or
705 * centi-G{igi,ibi}bytes, with appropriate rounding,
706 * and then print 1/100th of those as a decimal.
707 * We allow upto 2048Megabytes before converting to
708 * gigabytes, as that shows more precision and isn't
709 * too large a number.
710 * Terrabytes are not yet handled.
713 if (bytes
< 5000*1024)
715 else if (bytes
< 2*1024LL*1024LL*1024LL) {
716 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
717 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
718 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
719 cMiB
/100 , cMiB
% 100,
722 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
723 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
724 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
725 cGiB
/100 , cGiB
% 100,
731 char *human_size_brief(long long bytes
)
735 if (bytes
< 5000*1024)
736 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
737 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
739 else if (bytes
< 2*1024LL*1024LL*1024LL)
740 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
742 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
745 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
747 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
752 void print_r10_layout(int layout
)
754 int near
= layout
& 255;
755 int far
= (layout
>> 8) & 255;
756 int offset
= (layout
&0x10000);
760 printf("%s near=%d", sep
, near
);
764 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
766 printf("NO REDUNDANCY");
770 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
771 int chunksize
, unsigned long long devsize
)
775 case 0: data_disks
= raid_disks
; break;
776 case 1: data_disks
= 1; break;
778 case 5: data_disks
= raid_disks
- 1; break;
779 case 6: data_disks
= raid_disks
- 2; break;
780 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
783 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
784 return data_disks
* devsize
;
787 int get_mdp_major(void)
789 static int mdp_major
= -1;
793 int have_devices
= 0;
798 fl
= fopen("/proc/devices", "r");
801 while ((w
= conf_word(fl
, 1))) {
802 if (have_block
&& strcmp(w
, "devices:")==0)
804 have_block
= (strcmp(w
, "Block")==0);
807 if (have_devices
&& strcmp(w
, "mdp")==0)
808 mdp_major
= last_num
;
815 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
816 char *get_md_name(int dev
)
818 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
819 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
820 static char devname
[50];
826 int mdp
= get_mdp_major();
827 if (mdp
< 0) return NULL
;
828 rdev
= makedev(mdp
, (-1-dev
)<<6);
829 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
830 if (stat(devname
, &stb
) == 0
831 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
832 && (stb
.st_rdev
== rdev
))
835 rdev
= makedev(MD_MAJOR
, dev
);
836 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
837 if (stat(devname
, &stb
) == 0
838 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
839 && (stb
.st_rdev
== rdev
))
842 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
843 if (stat(devname
, &stb
) == 0
844 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
845 && (stb
.st_rdev
== rdev
))
848 dn
= map_dev(major(rdev
), minor(rdev
), 0);
851 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
852 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
856 if (stat(devname
, &stb
) == 0
857 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
858 && (stb
.st_rdev
== rdev
))
864 void put_md_name(char *name
)
866 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
870 int find_free_devnum(int use_partitions
)
873 for (devnum
= 127; devnum
!= 128;
874 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
878 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
879 if (mddev_busy(_devnum
))
881 /* make sure it is new to /dev too, at least as a
883 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
884 if (dn
&& ! is_standard(dn
, NULL
))
890 return use_partitions
? (-1-devnum
) : devnum
;
892 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
894 int dev_open(char *dev
, int flags
)
896 /* like 'open', but if 'dev' matches %d:%d, create a temp
897 * block device and open that
908 major
= strtoul(dev
, &e
, 0);
909 if (e
> dev
&& *e
== ':' && e
[1] &&
910 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
912 char *path
= map_dev(major
, minor
, 0);
914 fd
= open(path
, flags
);
916 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
917 (int)getpid(), major
, minor
);
918 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
919 fd
= open(devname
, flags
);
924 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
925 (int)getpid(), major
, minor
);
926 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
927 fd
= open(devname
, flags
);
932 fd
= open(dev
, flags
);
936 int open_dev(int devnum
)
940 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
941 return dev_open(buf
, O_RDWR
);
944 int open_dev_excl(int devnum
)
949 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
950 for (i
=0 ; i
<25 ; i
++) {
951 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
961 int same_dev(char *one
, char *two
)
963 struct stat st1
, st2
;
964 if (stat(one
, &st1
) != 0)
966 if (stat(two
, &st2
) != 0)
968 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
970 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
972 return st1
.st_rdev
== st2
.st_rdev
;
975 void wait_for(char *dev
, int fd
)
978 struct stat stb_want
;
980 if (fstat(fd
, &stb_want
) != 0 ||
981 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
984 for (i
=0 ; i
<25 ; i
++) {
986 if (stat(dev
, &stb
) == 0 &&
987 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
988 (stb
.st_rdev
== stb_want
.st_rdev
))
993 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
996 struct superswitch
*superlist
[] =
999 &super_ddf
, &super_imsm
,
1003 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
1005 struct supertype
*super_by_fd(int fd
)
1007 mdu_array_info_t array
;
1010 struct supertype
*st
= NULL
;
1015 char *subarray
= NULL
;
1017 sra
= sysfs_read(fd
, 0, GET_VERSION
);
1020 vers
= sra
->array
.major_version
;
1021 minor
= sra
->array
.minor_version
;
1022 verstr
= sra
->text_version
;
1024 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1025 array
.major_version
= array
.minor_version
= 0;
1026 vers
= array
.major_version
;
1027 minor
= array
.minor_version
;
1032 sprintf(version
, "%d.%d", vers
, minor
);
1035 if (minor
== -2 && is_subarray(verstr
)) {
1036 char *dev
= verstr
+1;
1037 subarray
= strchr(dev
, '/');
1041 devnum
= devname2devnum(dev
);
1042 subarray
= strdup(subarray
);
1045 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
1046 if (sra
&& sra
->text_version
[0])
1047 verstr
= sra
->text_version
;
1049 verstr
= "-no-metadata-";
1052 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1053 st
= superlist
[i
]->match_metadata_desc(verstr
);
1060 strncpy(st
->subarray
, subarray
, 32);
1061 st
->subarray
[31] = 0;
1064 st
->subarray
[0] = 0;
1068 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1071 struct supertype
*dup_super(struct supertype
*orig
)
1073 struct supertype
*st
;
1077 st
= malloc(sizeof(*st
));
1080 memset(st
, 0, sizeof(*st
));
1082 st
->max_devs
= orig
->max_devs
;
1083 st
->minor_version
= orig
->minor_version
;
1084 strcpy(st
->subarray
, orig
->subarray
);
1090 struct supertype
*guess_super(int fd
)
1092 /* try each load_super to find the best match,
1093 * and return the best superswitch
1095 struct superswitch
*ss
;
1096 struct supertype
*st
;
1097 time_t besttime
= 0;
1101 st
= malloc(sizeof(*st
));
1102 for (i
=0 ; superlist
[i
]; i
++) {
1105 memset(st
, 0, sizeof(*st
));
1106 rv
= ss
->load_super(st
, fd
, NULL
);
1109 st
->ss
->getinfo_super(st
, &info
);
1110 if (bestsuper
== -1 ||
1111 besttime
< info
.array
.ctime
) {
1113 besttime
= info
.array
.ctime
;
1118 if (bestsuper
!= -1) {
1120 memset(st
, 0, sizeof(*st
));
1121 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1123 superlist
[bestsuper
]->free_super(st
);
1131 /* Return size of device in bytes */
1132 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1134 unsigned long long ldsize
;
1137 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1138 ldsize
= (unsigned long long)st
.st_size
;
1141 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1144 unsigned long dsize
;
1145 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1150 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
1151 dname
, strerror(errno
));
1160 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1161 * Returns: 1 if successful
1162 * -1 for unknown partition type
1163 * 0 for other errors
1165 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1168 unsigned char buf
[512];
1169 unsigned char empty_gpt_entry
[16]= {0};
1170 struct GPT_part_entry
*part
;
1171 unsigned long long curr_part_end
;
1172 unsigned all_partitions
, entry_size
;
1177 BUILD_BUG_ON(sizeof(gpt
) != 512);
1178 /* read GPT header */
1179 lseek(fd
, 512, SEEK_SET
);
1180 if (read(fd
, &gpt
, 512) != 512)
1183 /* get the number of partition entries and the entry size */
1184 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1185 entry_size
= __le32_to_cpu(gpt
.part_size
);
1187 /* Check GPT signature*/
1188 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1192 if (all_partitions
> 1024 ||
1196 /* read first GPT partition entries */
1197 if (read(fd
, buf
, 512) != 512)
1200 part
= (struct GPT_part_entry
*)buf
;
1202 for (part_nr
=0; part_nr
< all_partitions
; part_nr
++) {
1203 /* is this valid partition? */
1204 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1205 /* check the last lba for the current partition */
1206 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1207 if (curr_part_end
> *endofpart
)
1208 *endofpart
= curr_part_end
;
1211 part
= (struct GPT_part_entry
*)((unsigned char*)part
+ entry_size
);
1213 if ((unsigned char *)part
>= buf
+ 512) {
1214 if (read(fd
, buf
, 512) != 512)
1216 part
= (struct GPT_part_entry
*)buf
;
1222 /* Sets endofpart parameter to the last block used by the last partition on the device.
1223 * Returns: 1 if successful
1224 * -1 for unknown partition type
1225 * 0 for other errors
1227 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1229 struct MBR boot_sect
;
1230 struct MBR_part_record
*part
;
1231 unsigned long long curr_part_end
;
1237 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1240 if (read(fd
, &boot_sect
, 512) != 512)
1243 /* check MBP signature */
1244 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1246 /* found the correct signature */
1247 part
= boot_sect
.parts
;
1249 for (part_nr
=0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1250 /* check for GPT type */
1251 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1252 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1255 /* check the last used lba for the current partition */
1256 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1257 __le32_to_cpu(part
->blocks_num
);
1258 if (curr_part_end
> *endofpart
)
1259 *endofpart
= curr_part_end
;
1264 /* Unknown partition table */
1271 int check_partitions(int fd
, char *dname
, unsigned long long freesize
)
1274 * Check where the last partition ends
1276 unsigned long long endofpart
;
1279 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1280 /* There appears to be a partition table here */
1281 if (freesize
== 0) {
1282 /* partitions will not be visible in new device */
1284 Name
": partition table exists on %s but will be lost or\n"
1285 " meaningless after creating array\n",
1288 } else if (endofpart
> freesize
) {
1289 /* last partition overlaps metadata */
1291 Name
": metadata will over-write last partition on %s.\n",
1299 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1302 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1303 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
1304 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
1308 int open_container(int fd
)
1310 /* 'fd' is a block device. Find out if it is in use
1311 * by a container, and return an open fd on that container.
1322 if (fstat(fd
, &st
) != 0)
1324 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1325 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1326 e
= path
+ strlen(path
);
1328 dir
= opendir(path
);
1331 while ((de
= readdir(dir
))) {
1334 if (de
->d_name
[0] == '.')
1336 sprintf(e
, "/%s/dev", de
->d_name
);
1337 dfd
= open(path
, O_RDONLY
);
1340 n
= read(dfd
, buf
, sizeof(buf
));
1342 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1345 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1347 sprintf(buf
, "%d:%d", major
, minor
);
1348 dfd
= dev_open(buf
, O_RDONLY
);
1358 struct superswitch
*version_to_superswitch(char *vers
)
1362 for (i
= 0; superlist
[i
]; i
++) {
1363 struct superswitch
*ss
= superlist
[i
];
1365 if (strcmp(vers
, ss
->name
) == 0)
1372 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1374 if (mdstat
->metadata_version
== NULL
||
1375 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1376 !is_subarray(mdstat
->metadata_version
+9) ||
1377 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
1378 mdstat
->metadata_version
[10+strlen(container
)] != '/')
1384 int is_subarray_active(char *subarray
, char *container
)
1386 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1387 struct mdstat_ent
*ent
;
1389 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
1390 if (is_container_member(ent
, container
)) {
1391 char *inst
= &ent
->metadata_version
[10+strlen(container
)+1];
1393 if (!subarray
|| strcmp(inst
, subarray
) == 0)
1398 free_mdstat(mdstat
);
1403 int is_container_active(char *container
)
1405 return is_subarray_active(NULL
, container
);
1408 /* open_subarray - opens a subarray in a container
1409 * @dev: container device name
1410 * @st: supertype with only ->subarray set
1411 * @quiet: block reporting errors flag
1413 * On success returns an fd to a container and fills in *st
1415 int open_subarray(char *dev
, struct supertype
*st
, int quiet
)
1420 fd
= open(dev
, O_RDWR
|O_EXCL
);
1423 fprintf(stderr
, Name
": Couldn't open %s, aborting\n",
1428 st
->devnum
= fd2devnum(fd
);
1429 if (st
->devnum
== NoMdDev
) {
1432 Name
": Failed to determine device number for %s\n",
1437 mdi
= sysfs_read(fd
, st
->devnum
, GET_VERSION
|GET_LEVEL
);
1440 fprintf(stderr
, Name
": Failed to read sysfs for %s\n",
1445 if (mdi
->array
.level
!= UnSet
) {
1447 fprintf(stderr
, Name
": %s is not a container\n", dev
);
1451 st
->ss
= version_to_superswitch(mdi
->text_version
);
1455 Name
": Operation not supported for %s metadata\n",
1460 st
->devname
= devnum2devname(st
->devnum
);
1463 fprintf(stderr
, Name
": Failed to allocate device name\n");
1467 if (st
->ss
->load_super(st
, fd
, NULL
)) {
1469 fprintf(stderr
, Name
": Failed to find subarray-%s in %s\n",
1474 if (!st
->loaded_container
) {
1476 fprintf(stderr
, Name
": %s is not a container\n", dev
);
1484 st
->ss
->free_super(st
);
1500 int add_disk(int mdfd
, struct supertype
*st
,
1501 struct mdinfo
*sra
, struct mdinfo
*info
)
1503 /* Add a device to an array, in one of 2 ways. */
1506 if (st
->ss
->external
) {
1507 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1508 info
->recovery_start
= MaxSector
;
1510 info
->recovery_start
= 0;
1511 rv
= sysfs_add_disk(sra
, info
, 0);
1514 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1518 sd2
= malloc(sizeof(*sd2
));
1520 sd2
->next
= sra
->devs
;
1526 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1530 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1532 /* Initialise kernel's knowledge of array.
1533 * This varies between externally managed arrays
1536 int vers
= md_get_version(mdfd
);
1540 if (st
->ss
->external
)
1541 rv
= sysfs_set_array(info
, vers
);
1544 if ((vers
% 100) >= 1) { /* can use different versions */
1545 mdu_array_info_t inf
;
1546 memset(&inf
, 0, sizeof(inf
));
1547 inf
.major_version
= info
->array
.major_version
;
1548 inf
.minor_version
= info
->array
.minor_version
;
1549 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1551 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1555 unsigned long long min_recovery_start(struct mdinfo
*array
)
1557 /* find the minimum recovery_start in an array for metadata
1558 * formats that only record per-array recovery progress instead
1561 unsigned long long recovery_start
= MaxSector
;
1564 for (d
= array
->devs
; d
; d
= d
->next
)
1565 recovery_start
= min(recovery_start
, d
->recovery_start
);
1567 return recovery_start
;
1570 char *devnum2devname(int num
)
1574 sprintf(name
, "md%d", num
);
1576 sprintf(name
, "md_d%d", -1-num
);
1577 return strdup(name
);
1580 int devname2devnum(char *name
)
1584 if (strncmp(name
, "md_d", 4)==0)
1585 num
= -1-strtoul(name
+4, &ep
, 10);
1587 num
= strtoul(name
+2, &ep
, 10);
1591 int stat2devnum(struct stat
*st
)
1598 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1599 if (major(st
->st_rdev
) == MD_MAJOR
)
1600 return minor(st
->st_rdev
);
1601 else if (major(st
->st_rdev
) == (unsigned)get_mdp_major())
1602 return -1- (minor(st
->st_rdev
)>>MdpMinorShift
);
1604 /* must be an extended-minor partition. Look at the
1605 * /sys/dev/block/%d:%d link which must look like
1606 * ../../block/mdXXX/mdXXXpYY
1608 sprintf(path
, "/sys/dev/block/%d:%d", major(st
->st_rdev
),
1609 minor(st
->st_rdev
));
1610 n
= readlink(path
, link
, sizeof(link
)-1);
1614 cp
= strrchr(link
, '/');
1616 cp
= strchr(link
, '/');
1617 if (cp
&& strncmp(cp
, "/md", 3) == 0)
1618 return devname2devnum(cp
+1);
1624 int fd2devnum(int fd
)
1627 if (fstat(fd
, &stb
) == 0)
1628 return stat2devnum(&stb
);
1632 int mdmon_pid(int devnum
)
1638 char *devname
= devnum2devname(devnum
);
1640 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devname
);
1643 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1647 n
= read(fd
, pid
, 9);
1654 int mdmon_running(int devnum
)
1656 int pid
= mdmon_pid(devnum
);
1659 if (kill(pid
, 0) == 0)
1664 int start_mdmon(int devnum
)
1678 if (check_env("MDADM_NO_MDMON"))
1681 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1685 sl
= strrchr(pathbuf
, '/');
1690 strcpy(sl
, "mdmon");
1696 /* FIXME yuk. CLOSE_EXEC?? */
1697 for (i
=3; i
< 100; i
++)
1699 for (i
=0; paths
[i
]; i
++)
1701 execl(paths
[i
], "mdmon",
1702 devnum2devname(devnum
),
1705 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1706 "Array remains readonly\n");
1708 default: /* parent - good */
1709 pid
= wait(&status
);
1710 if (pid
< 0 || status
!= 0)
1716 int check_env(char *name
)
1718 char *val
= getenv(name
);
1720 if (val
&& atoi(val
) == 1)
1726 __u32
random32(void)
1729 int rfd
= open("/dev/urandom", O_RDONLY
);
1730 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1738 int flush_metadata_updates(struct supertype
*st
)
1742 st
->update_tail
= NULL
;
1746 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1750 while (st
->updates
) {
1751 struct metadata_update
*mu
= st
->updates
;
1752 st
->updates
= mu
->next
;
1754 send_message(sfd
, mu
, 0);
1762 st
->update_tail
= NULL
;
1766 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1769 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1775 *st
->update_tail
= mu
;
1776 st
->update_tail
= &mu
->next
;
1778 #endif /* MDASSEMBLE */
1781 /* tinyc doesn't optimize this check in ioctl.h out ... */
1782 unsigned int __invalid_size_argument_for_IOC
= 0;