2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 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>
38 * following taken from linux/blkpg.h because they aren't
39 * anywhere else and it isn't safe to #include linux/ * stuff.
42 #define BLKPG _IO(0x12,105)
44 /* The argument structure */
45 struct blkpg_ioctl_arg
{
52 /* The subfunctions (for the op field) */
53 #define BLKPG_ADD_PARTITION 1
54 #define BLKPG_DEL_PARTITION 2
56 /* Sizes of name fields. Unused at present. */
57 #define BLKPG_DEVNAMELTH 64
58 #define BLKPG_VOLNAMELTH 64
60 /* The data structure for ADD_PARTITION and DEL_PARTITION */
61 struct blkpg_partition
{
62 long long start
; /* starting offset in bytes */
63 long long length
; /* length in bytes */
64 int pno
; /* partition number */
65 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
66 to be used in kernel messages */
67 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
72 /* Force a compilation error if condition is true */
73 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
75 /* Force a compilation error if condition is true, but also produce a
76 result (of value 0 and type size_t), so the expression can be used
77 e.g. in a structure initializer (or where-ever else comma expressions
79 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
82 * Parse a 128 bit uuid in 4 integers
83 * format is 32 hexx nibbles with options :.<space> separator
84 * If not exactly 32 hex digits are found, return 0
87 int parse_uuid(char *str
, int uuid
[4])
89 int hit
= 0; /* number of Hex digIT */
92 for (i
= 0; i
< 4; i
++)
95 while ((c
= *str
++) != 0) {
97 if (c
>= '0' && c
<= '9')
99 else if (c
>= 'a' && c
<= 'f')
101 else if (c
>= 'A' && c
<= 'F')
103 else if (strchr(":. -", c
))
119 * Get the md version number.
120 * We use the RAID_VERSION ioctl if it is supported
121 * If not, but we have a block device with major '9', we assume
124 * Return version number as 24 but number - assume version parts
128 int md_get_version(int fd
)
133 if (fstat(fd
, &stb
)<0)
135 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
138 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
139 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
142 if (major(stb
.st_rdev
) == MD_MAJOR
)
147 int get_linux_version()
151 int a
= 0, b
= 0,c
= 0;
156 a
= strtoul(cp
, &cp
, 10);
158 b
= strtoul(cp
+1, &cp
, 10);
160 c
= strtoul(cp
+1, &cp
, 10);
162 return (a
*1000000)+(b
*1000)+c
;
166 int mdadm_version(char *version
)
174 cp
= strchr(version
, '-');
175 if (!cp
|| *(cp
+1) != ' ' || *(cp
+2) != 'v')
178 a
= strtoul(cp
, &cp
, 10);
181 b
= strtoul(cp
+1, &cp
, 10);
183 c
= strtoul(cp
+1, &cp
, 10);
186 if (*cp
!= ' ' && *cp
!= '-')
188 return (a
*1000000)+(b
*1000)+c
;
191 unsigned long long parse_size(char *size
)
193 /* parse 'size' which should be a number optionally
194 * followed by 'K', 'M', or 'G'.
195 * Without a suffix, K is assumed.
196 * Number returned is in sectors (half-K)
197 * 0 returned on error.
200 long long s
= strtoll(size
, &c
, 10);
214 s
*= 1024 * 1024 * 2;
224 int parse_layout_10(char *layout
)
228 /* Parse the layout string for raid10 */
229 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
230 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
231 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
235 if (layout
[0] == 'n')
237 else if (layout
[0] == 'o')
238 rv
= 0x10000 + (copies
<<8) + 1;
240 rv
= 1 + (copies
<<8);
244 int parse_layout_faulty(char *layout
)
246 /* Parse the layout string for 'faulty' */
247 int ln
= strcspn(layout
, "0123456789");
248 char *m
= xstrdup(layout
);
251 mode
= map_name(faultylayout
, m
);
255 return mode
| (atoi(layout
+ln
)<< ModeShift
);
258 long parse_num(char *num
)
260 /* Either return a valid number, or -1 */
262 long rv
= strtol(num
, &c
, 10);
263 if (rv
< 0 || *c
|| !num
[0])
270 void remove_partitions(int fd
)
272 /* remove partitions from this block devices.
273 * This is used for components added to an array
275 #ifdef BLKPG_DEL_PARTITION
276 struct blkpg_ioctl_arg a
;
277 struct blkpg_partition p
;
279 a
.op
= BLKPG_DEL_PARTITION
;
281 a
.datalen
= sizeof(p
);
283 memset(a
.data
, 0, a
.datalen
);
284 for (p
.pno
= 0; p
.pno
< 16; p
.pno
++)
285 ioctl(fd
, BLKPG
, &a
);
289 int test_partition(int fd
)
291 /* Check if fd is a whole-disk or a partition.
292 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
293 * will return ENXIO on an invalid partition number.
295 struct blkpg_ioctl_arg a
;
296 struct blkpg_partition p
;
297 a
.op
= BLKPG_DEL_PARTITION
;
299 a
.datalen
= sizeof(p
);
301 memset(a
.data
, 0, a
.datalen
);
303 if (ioctl(fd
, BLKPG
, &a
) == 0)
304 /* Very unlikely, but not a partition */
307 /* not a partition */
313 int test_partition_from_id(dev_t id
)
318 sprintf(buf
, "%d:%d", major(id
), minor(id
));
319 fd
= dev_open(buf
, O_RDONLY
);
322 rv
= test_partition(fd
);
327 int enough(int level
, int raid_disks
, int layout
, int clean
, char *avail
)
333 for (i
= 0; i
< raid_disks
; i
++)
334 avail_disks
+= !!avail
[i
];
338 /* This is the tricky one - we need to check
339 * which actual disks are present.
341 copies
= (layout
&255)* ((layout
>>8) & 255);
344 /* there must be one of the 'copies' form 'first' */
351 this = (this+1) % raid_disks
;
355 first
= (first
+(layout
&255)) % raid_disks
;
356 } while (first
!= 0);
359 case LEVEL_MULTIPATH
:
360 return avail_disks
>= 1;
363 return avail_disks
== raid_disks
;
365 return avail_disks
>= 1;
369 return avail_disks
>= raid_disks
-1;
371 return avail_disks
>= raid_disks
;
374 return avail_disks
>= raid_disks
-2;
376 return avail_disks
>= raid_disks
;
382 int enough_fd(int fd
)
384 struct mdu_array_info_s array
;
385 struct mdu_disk_info_s disk
;
390 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0 ||
391 array
.raid_disks
<= 0)
393 avail
= xcalloc(array
.raid_disks
, 1);
394 for (i
= 0; i
< MAX_DISKS
&& array
.nr_disks
> 0; i
++) {
396 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
398 if (disk
.major
== 0 && disk
.minor
== 0)
402 if (! (disk
.state
& (1<<MD_DISK_SYNC
)))
404 if (disk
.raid_disk
< 0 || disk
.raid_disk
>= array
.raid_disks
)
407 avail
[disk
.raid_disk
] = 1;
409 /* This is used on an active array, so assume it is clean */
410 rv
= enough(array
.level
, array
.raid_disks
, array
.layout
,
416 const int uuid_zero
[4] = { 0, 0, 0, 0 };
418 int same_uuid(int a
[4], int b
[4], int swapuuid
)
421 /* parse uuids are hostendian.
422 * uuid's from some superblocks are big-ending
423 * if there is a difference, we need to swap..
425 unsigned char *ac
= (unsigned char *)a
;
426 unsigned char *bc
= (unsigned char *)b
;
428 for (i
= 0; i
< 16; i
+= 4) {
429 if (ac
[i
+0] != bc
[i
+3] ||
430 ac
[i
+1] != bc
[i
+2] ||
431 ac
[i
+2] != bc
[i
+1] ||
446 void copy_uuid(void *a
, int b
[4], int swapuuid
)
449 /* parse uuids are hostendian.
450 * uuid's from some superblocks are big-ending
451 * if there is a difference, we need to swap..
453 unsigned char *ac
= (unsigned char *)a
;
454 unsigned char *bc
= (unsigned char *)b
;
456 for (i
= 0; i
< 16; i
+= 4) {
466 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
473 copy_uuid(uuid
, id
, swap
);
474 for (i
= 0; i
< 4; i
++) {
477 for (j
= 3; j
>= 0; j
--) {
478 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
486 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
488 // dirty hack to work around an issue with super1 superblocks...
489 // super1 superblocks need swapuuid set in order for assembly to
490 // work, but can't have it set if we want this printout to match
491 // all the other uuid printouts in super1.c, so we force swapuuid
492 // to 1 to make our printout match the rest of super1
493 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
497 int check_ext2(int fd
, char *name
)
500 * Check for an ext2fs file system.
501 * Superblock is always 1K at 1K offset
503 * s_magic is le16 at 56 == 0xEF53
504 * report mtime - le32 at 44
506 * logblksize - le32 at 24
508 unsigned char sb
[1024];
511 if (lseek(fd
, 1024,0)!= 1024)
513 if (read(fd
, sb
, 1024)!= 1024)
515 if (sb
[56] != 0x53 || sb
[57] != 0xef)
518 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
519 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
520 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
521 pr_err("%s appears to contain an ext2fs file system\n",
523 fprintf(stderr
," size=%dK mtime=%s",
524 size
*(1<<bsize
), ctime(&mtime
));
528 int check_reiser(int fd
, char *name
)
531 * superblock is at 64K
533 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
536 unsigned char sb
[1024];
538 if (lseek(fd
, 64*1024, 0) != 64*1024)
540 if (read(fd
, sb
, 1024) != 1024)
542 if (strncmp((char*)sb
+52, "ReIsErFs",8) != 0 &&
543 strncmp((char*)sb
+52, "ReIsEr2Fs",9) != 0)
545 pr_err("%s appears to contain a reiserfs file system\n",name
);
546 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
547 fprintf(stderr
, " size = %luK\n", size
*4);
552 int check_raid(int fd
, char *name
)
557 struct supertype
*st
= guess_super(fd
);
560 st
->ignore_hw_compat
= 1;
561 st
->ss
->load_super(st
, fd
, name
);
562 /* Looks like a raid array .. */
563 pr_err("%s appears to be part of a raid array:\n",
565 st
->ss
->getinfo_super(st
, &info
, NULL
);
566 st
->ss
->free_super(st
);
567 crtime
= info
.array
.ctime
;
568 level
= map_num(pers
, info
.array
.level
);
569 if (!level
) level
= "-unknown-";
570 fprintf(stderr
, " level=%s devices=%d ctime=%s",
571 level
, info
.array
.raid_disks
, ctime(&crtime
));
579 for (i
= 0; i
< 5; i
++) {
581 fprintf(stderr
, "%s%s", mesg
, add
);
583 if (fgets(buf
, 100, stdin
)==NULL
)
585 if (buf
[0]=='y' || buf
[0]=='Y')
587 if (buf
[0]=='n' || buf
[0]=='N')
591 pr_err("assuming 'no'\n");
594 #endif /* MDASSEMBLE */
596 int is_standard(char *dev
, int *nump
)
598 /* tests if dev is a "standard" md dev name.
599 * i.e if the last component is "/dNN" or "/mdNN",
600 * where NN is a string of digits
601 * Returns 1 if a partitionable standard,
602 * -1 if non-partitonable,
603 * 0 if not a standard name.
605 char *d
= strrchr(dev
, '/');
610 if (strncmp(d
, "/d",2) == 0)
611 d
+= 2, type
= 1; /* /dev/md/dN{pM} */
612 else if (strncmp(d
, "/md_d", 5) == 0)
613 d
+= 5, type
= 1; /* /dev/md_dN{pM} */
614 else if (strncmp(d
, "/md", 3) == 0)
615 d
+= 3, type
= -1; /* /dev/mdN */
616 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3) == 0)
617 d
+= 1, type
= -1; /* /dev/md/N */
627 if (nump
) *nump
= num
;
632 unsigned long calc_csum(void *super
, int bytes
)
634 unsigned long long newcsum
= 0;
637 unsigned int *superc
= (unsigned int*) super
;
639 for(i
= 0; i
< bytes
/4; i
++)
640 newcsum
+= superc
[i
];
641 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
643 /* The in-kernel checksum calculation is always 16bit on
644 * the alpha, though it is 32 bit on i386...
645 * I wonder what it is elsewhere... (it uses an API in
646 * a way that it shouldn't).
648 csum
= (csum
& 0xffff) + (csum
>> 16);
649 csum
= (csum
& 0xffff) + (csum
>> 16);
655 char *human_size(long long bytes
)
659 /* We convert bytes to either centi-M{ega,ibi}bytes or
660 * centi-G{igi,ibi}bytes, with appropriate rounding,
661 * and then print 1/100th of those as a decimal.
662 * We allow upto 2048Megabytes before converting to
663 * gigabytes, as that shows more precision and isn't
664 * too large a number.
665 * Terabytes are not yet handled.
668 if (bytes
< 5000*1024)
670 else if (bytes
< 2*1024LL*1024LL*1024LL) {
671 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
672 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
673 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
674 cMiB
/100 , cMiB
% 100,
677 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
678 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
679 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
680 cGiB
/100 , cGiB
% 100,
686 char *human_size_brief(long long bytes
, int prefix
)
690 /* We convert bytes to either centi-M{ega,ibi}bytes or
691 * centi-G{igi,ibi}bytes, with appropriate rounding,
692 * and then print 1/100th of those as a decimal.
693 * We allow upto 2048Megabytes before converting to
694 * gigabytes, as that shows more precision and isn't
695 * too large a number.
696 * Terabytes are not yet handled.
698 * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
699 * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
702 if (bytes
< 5000*1024)
704 else if (prefix
== IEC
) {
705 if (bytes
< 2*1024LL*1024LL*1024LL) {
706 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
707 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
708 cMiB
/100 , cMiB
% 100);
710 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
711 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
712 cGiB
/100 , cGiB
% 100);
715 else if (prefix
== JEDEC
) {
716 if (bytes
< 2*1024LL*1024LL*1024LL) {
717 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
718 snprintf(buf
, sizeof(buf
), "%ld.%02ldMB",
721 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
722 snprintf(buf
, sizeof(buf
), "%ld.%02ldGB",
723 cGB
/100 , cGB
% 100);
732 void print_r10_layout(int layout
)
734 int near
= layout
& 255;
735 int far
= (layout
>> 8) & 255;
736 int offset
= (layout
&0x10000);
740 printf("%s near=%d", sep
, near
);
744 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
746 printf("NO REDUNDANCY");
750 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
751 int chunksize
, unsigned long long devsize
)
755 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
756 return get_data_disks(level
, layout
, raid_disks
) * devsize
;
759 int get_data_disks(int level
, int layout
, int raid_disks
)
763 case 0: data_disks
= raid_disks
;
765 case 1: data_disks
= 1;
768 case 5: data_disks
= raid_disks
- 1;
770 case 6: data_disks
= raid_disks
- 2;
772 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
779 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
780 char *get_md_name(int dev
)
782 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
783 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
784 static char devname
[50];
790 int mdp
= get_mdp_major();
791 if (mdp
< 0) return NULL
;
792 rdev
= makedev(mdp
, (-1-dev
)<<6);
793 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
794 if (stat(devname
, &stb
) == 0
795 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
796 && (stb
.st_rdev
== rdev
))
799 rdev
= makedev(MD_MAJOR
, dev
);
800 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
801 if (stat(devname
, &stb
) == 0
802 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
803 && (stb
.st_rdev
== rdev
))
806 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
807 if (stat(devname
, &stb
) == 0
808 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
809 && (stb
.st_rdev
== rdev
))
812 dn
= map_dev(major(rdev
), minor(rdev
), 0);
815 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
816 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
820 if (stat(devname
, &stb
) == 0
821 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
822 && (stb
.st_rdev
== rdev
))
828 void put_md_name(char *name
)
830 if (strncmp(name
, "/dev/.tmp.md", 12) == 0)
834 int find_free_devnum(int use_partitions
)
837 for (devnum
= 127; devnum
!= 128;
838 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
843 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
844 if (mddev_busy(_devnum
))
846 sprintf(nbuf
, "%s%d", use_partitions
?"mdp":"md", devnum
);
847 if (!conf_name_is_free(nbuf
))
849 /* make sure it is new to /dev too, at least as a
851 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
852 if (dn
&& ! is_standard(dn
, NULL
))
858 return use_partitions
? (-1-devnum
) : devnum
;
860 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
862 int dev_open(char *dev
, int flags
)
864 /* like 'open', but if 'dev' matches %d:%d, create a temp
865 * block device and open that
876 major
= strtoul(dev
, &e
, 0);
877 if (e
> dev
&& *e
== ':' && e
[1] &&
878 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
880 char *path
= map_dev(major
, minor
, 0);
882 fd
= open(path
, flags
);
884 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
885 (int)getpid(), major
, minor
);
886 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
887 fd
= open(devname
, flags
);
892 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
893 (int)getpid(), major
, minor
);
894 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
895 fd
= open(devname
, flags
);
900 fd
= open(dev
, flags
);
904 int open_dev_flags(int devnum
, int flags
)
908 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
909 return dev_open(buf
, flags
);
912 int open_dev(int devnum
)
914 return open_dev_flags(devnum
, O_RDONLY
);
917 int open_dev_excl(int devnum
)
923 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
924 for (i
= 0 ; i
< 25 ; i
++) {
925 int fd
= dev_open(buf
, flags
|O_EXCL
);
928 if (errno
== EACCES
&& flags
== O_RDWR
) {
939 int same_dev(char *one
, char *two
)
941 struct stat st1
, st2
;
942 if (stat(one
, &st1
) != 0)
944 if (stat(two
, &st2
) != 0)
946 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
948 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
950 return st1
.st_rdev
== st2
.st_rdev
;
953 void wait_for(char *dev
, int fd
)
956 struct stat stb_want
;
958 if (fstat(fd
, &stb_want
) != 0 ||
959 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
962 for (i
= 0 ; i
< 25 ; i
++) {
964 if (stat(dev
, &stb
) == 0 &&
965 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
966 (stb
.st_rdev
== stb_want
.st_rdev
))
971 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
974 struct superswitch
*superlist
[] =
977 &super_ddf
, &super_imsm
,
981 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
983 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
985 mdu_array_info_t array
;
988 struct supertype
*st
= NULL
;
993 char *subarray
= NULL
;
994 int container
= NoMdDev
;
996 sra
= sysfs_read(fd
, 0, GET_VERSION
);
999 vers
= sra
->array
.major_version
;
1000 minor
= sra
->array
.minor_version
;
1001 verstr
= sra
->text_version
;
1003 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1004 array
.major_version
= array
.minor_version
= 0;
1005 vers
= array
.major_version
;
1006 minor
= array
.minor_version
;
1011 sprintf(version
, "%d.%d", vers
, minor
);
1014 if (minor
== -2 && is_subarray(verstr
)) {
1015 char *dev
= verstr
+1;
1017 subarray
= strchr(dev
, '/');
1020 subarray
= xstrdup(subarray
);
1022 container
= devname2devnum(dev
);
1025 sra
= sysfs_read(-1, container
, GET_VERSION
);
1026 if (sra
&& sra
->text_version
[0])
1027 verstr
= sra
->text_version
;
1029 verstr
= "-no-metadata-";
1032 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1033 st
= superlist
[i
]->match_metadata_desc(verstr
);
1040 *subarrayp
= subarray
;
1041 st
->container_dev
= container
;
1042 st
->devnum
= fd2devnum(fd
);
1048 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1050 int dev_size_from_id(dev_t id
, unsigned long long *size
)
1055 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1056 fd
= dev_open(buf
, O_RDONLY
);
1059 if (get_dev_size(fd
, NULL
, size
)) {
1067 struct supertype
*dup_super(struct supertype
*orig
)
1069 struct supertype
*st
;
1073 st
= xcalloc(1, sizeof(*st
));
1075 st
->max_devs
= orig
->max_devs
;
1076 st
->minor_version
= orig
->minor_version
;
1082 struct supertype
*guess_super_type(int fd
, enum guess_types guess_type
)
1084 /* try each load_super to find the best match,
1085 * and return the best superswitch
1087 struct superswitch
*ss
;
1088 struct supertype
*st
;
1089 time_t besttime
= 0;
1093 st
= xcalloc(1, sizeof(*st
));
1094 st
->container_dev
= NoMdDev
;
1096 for (i
= 0 ; superlist
[i
]; i
++) {
1099 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1101 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1103 memset(st
, 0, sizeof(*st
));
1104 st
->ignore_hw_compat
= 1;
1105 rv
= ss
->load_super(st
, fd
, NULL
);
1108 st
->ss
->getinfo_super(st
, &info
, NULL
);
1109 if (bestsuper
== -1 ||
1110 besttime
< info
.array
.ctime
) {
1112 besttime
= info
.array
.ctime
;
1117 if (bestsuper
!= -1) {
1119 memset(st
, 0, sizeof(*st
));
1120 st
->ignore_hw_compat
= 1;
1121 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1123 superlist
[bestsuper
]->free_super(st
);
1124 st
->ignore_hw_compat
= 0;
1132 /* Return size of device in bytes */
1133 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1135 unsigned long long ldsize
;
1138 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1139 ldsize
= (unsigned long long)st
.st_size
;
1142 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1145 unsigned long dsize
;
1146 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1151 pr_err("Cannot get size of %s: %s\b",
1152 dname
, strerror(errno
));
1160 /* Return true if this can only be a container, not a member device.
1161 * i.e. is and md device and size is zero
1163 int must_be_container(int fd
)
1165 unsigned long long size
;
1166 if (md_get_version(fd
) < 0)
1168 if (get_dev_size(fd
, NULL
, &size
) == 0)
1175 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1176 * Returns: 1 if successful
1177 * -1 for unknown partition type
1178 * 0 for other errors
1180 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1183 unsigned char empty_gpt_entry
[16]= {0};
1184 struct GPT_part_entry
*part
;
1186 unsigned long long curr_part_end
;
1187 unsigned all_partitions
, entry_size
;
1192 BUILD_BUG_ON(sizeof(gpt
) != 512);
1193 /* skip protective MBR */
1194 lseek(fd
, 512, SEEK_SET
);
1195 /* read GPT header */
1196 if (read(fd
, &gpt
, 512) != 512)
1199 /* get the number of partition entries and the entry size */
1200 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1201 entry_size
= __le32_to_cpu(gpt
.part_size
);
1203 /* Check GPT signature*/
1204 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1208 if (all_partitions
> 1024 ||
1209 entry_size
> sizeof(buf
))
1212 part
= (struct GPT_part_entry
*)buf
;
1214 for (part_nr
= 0; part_nr
< all_partitions
; part_nr
++) {
1215 /* read partition entry */
1216 if (read(fd
, buf
, entry_size
) != (ssize_t
)entry_size
)
1219 /* is this valid partition? */
1220 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1221 /* check the last lba for the current partition */
1222 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1223 if (curr_part_end
> *endofpart
)
1224 *endofpart
= curr_part_end
;
1231 /* Sets endofpart parameter to the last block used by the last partition on the device.
1232 * Returns: 1 if successful
1233 * -1 for unknown partition type
1234 * 0 for other errors
1236 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1238 struct MBR boot_sect
;
1239 struct MBR_part_record
*part
;
1240 unsigned long long curr_part_end
;
1246 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1249 if (read(fd
, &boot_sect
, 512) != 512)
1252 /* check MBP signature */
1253 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1255 /* found the correct signature */
1256 part
= boot_sect
.parts
;
1258 for (part_nr
= 0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1259 /* check for GPT type */
1260 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1261 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1264 /* check the last used lba for the current partition */
1265 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1266 __le32_to_cpu(part
->blocks_num
);
1267 if (curr_part_end
> *endofpart
)
1268 *endofpart
= curr_part_end
;
1273 /* Unknown partition table */
1280 int check_partitions(int fd
, char *dname
, unsigned long long freesize
,
1281 unsigned long long size
)
1284 * Check where the last partition ends
1286 unsigned long long endofpart
;
1289 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1290 /* There appears to be a partition table here */
1291 if (freesize
== 0) {
1292 /* partitions will not be visible in new device */
1293 pr_err("partition table exists on %s but will be lost or\n"
1294 " meaningless after creating array\n",
1297 } else if (endofpart
> freesize
) {
1298 /* last partition overlaps metadata */
1299 pr_err("metadata will over-write last partition on %s.\n",
1302 } else if (size
&& endofpart
> size
) {
1303 /* partitions will be truncated in new device */
1304 pr_err("array size is too small to cover all partitions on %s.\n",
1312 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1316 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1317 for (d
= 0 ; d
< MAX_DISKS
; d
++) {
1318 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0 &&
1319 (disk
->major
|| disk
->minor
))
1324 int open_container(int fd
)
1326 /* 'fd' is a block device. Find out if it is in use
1327 * by a container, and return an open fd on that container.
1338 if (fstat(fd
, &st
) != 0)
1340 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1341 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1342 e
= path
+ strlen(path
);
1344 dir
= opendir(path
);
1347 while ((de
= readdir(dir
))) {
1350 if (de
->d_name
[0] == '.')
1352 sprintf(e
, "/%s/dev", de
->d_name
);
1353 dfd
= open(path
, O_RDONLY
);
1356 n
= read(dfd
, buf
, sizeof(buf
));
1358 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1361 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1363 sprintf(buf
, "%d:%d", major
, minor
);
1364 dfd
= dev_open(buf
, O_RDONLY
);
1374 struct superswitch
*version_to_superswitch(char *vers
)
1378 for (i
= 0; superlist
[i
]; i
++) {
1379 struct superswitch
*ss
= superlist
[i
];
1381 if (strcmp(vers
, ss
->name
) == 0)
1388 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1390 if (mdstat
->metadata_version
== NULL
||
1391 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1392 !is_subarray(mdstat
->metadata_version
+9) ||
1393 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
1394 mdstat
->metadata_version
[10+strlen(container
)] != '/')
1400 int is_subarray_active(char *subarray
, char *container
)
1402 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1403 struct mdstat_ent
*ent
;
1405 for (ent
= mdstat
; ent
; ent
= ent
->next
)
1406 if (is_container_member(ent
, container
))
1407 if (strcmp(to_subarray(ent
, container
), subarray
) == 0)
1410 free_mdstat(mdstat
);
1415 /* open_subarray - opens a subarray in a container
1416 * @dev: container device name
1417 * @st: empty supertype
1418 * @quiet: block reporting errors flag
1420 * On success returns an fd to a container and fills in *st
1422 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1425 struct mdinfo
*info
;
1428 fd
= open(dev
, O_RDWR
|O_EXCL
);
1431 pr_err("Couldn't open %s, aborting\n",
1436 st
->devnum
= fd2devnum(fd
);
1437 if (st
->devnum
== NoMdDev
) {
1439 pr_err("Failed to determine device number for %s\n",
1444 mdi
= sysfs_read(fd
, st
->devnum
, GET_VERSION
|GET_LEVEL
);
1447 pr_err("Failed to read sysfs for %s\n",
1452 if (mdi
->array
.level
!= UnSet
) {
1454 pr_err("%s is not a container\n", dev
);
1458 st
->ss
= version_to_superswitch(mdi
->text_version
);
1461 pr_err("Operation not supported for %s metadata\n",
1466 st
->devname
= devnum2devname(st
->devnum
);
1469 pr_err("Failed to allocate device name\n");
1473 if (!st
->ss
->load_container
) {
1475 pr_err("%s is not a container\n", dev
);
1479 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1481 pr_err("Failed to load metadata for %s\n",
1486 info
= st
->ss
->container_content(st
, subarray
);
1489 pr_err("Failed to find subarray-%s in %s\n",
1499 st
->ss
->free_super(st
);
1515 int add_disk(int mdfd
, struct supertype
*st
,
1516 struct mdinfo
*sra
, struct mdinfo
*info
)
1518 /* Add a device to an array, in one of 2 ways. */
1521 if (st
->ss
->external
) {
1522 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1523 info
->recovery_start
= MaxSector
;
1525 info
->recovery_start
= 0;
1526 rv
= sysfs_add_disk(sra
, info
, 0);
1529 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1533 sd2
= xmalloc(sizeof(*sd2
));
1535 sd2
->next
= sra
->devs
;
1541 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1545 int remove_disk(int mdfd
, struct supertype
*st
,
1546 struct mdinfo
*sra
, struct mdinfo
*info
)
1549 /* Remove the disk given by 'info' from the array */
1551 if (st
->ss
->external
)
1552 rv
= sysfs_set_str(sra
, info
, "slot", "none");
1555 rv
= ioctl(mdfd
, HOT_REMOVE_DISK
, makedev(info
->disk
.major
,
1560 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1562 /* Initialise kernel's knowledge of array.
1563 * This varies between externally managed arrays
1566 int vers
= md_get_version(mdfd
);
1570 if (st
->ss
->external
)
1571 rv
= sysfs_set_array(info
, vers
);
1574 if ((vers
% 100) >= 1) { /* can use different versions */
1575 mdu_array_info_t inf
;
1576 memset(&inf
, 0, sizeof(inf
));
1577 inf
.major_version
= info
->array
.major_version
;
1578 inf
.minor_version
= info
->array
.minor_version
;
1579 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1581 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1585 unsigned long long min_recovery_start(struct mdinfo
*array
)
1587 /* find the minimum recovery_start in an array for metadata
1588 * formats that only record per-array recovery progress instead
1591 unsigned long long recovery_start
= MaxSector
;
1594 for (d
= array
->devs
; d
; d
= d
->next
)
1595 recovery_start
= min(recovery_start
, d
->recovery_start
);
1597 return recovery_start
;
1600 int mdmon_pid(int devnum
)
1606 char *devname
= devnum2devname(devnum
);
1608 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devname
);
1611 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1615 n
= read(fd
, pid
, 9);
1622 int mdmon_running(int devnum
)
1624 int pid
= mdmon_pid(devnum
);
1627 if (kill(pid
, 0) == 0)
1632 int start_mdmon(int devnum
)
1646 if (check_env("MDADM_NO_MDMON"))
1649 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
)-1);
1653 sl
= strrchr(pathbuf
, '/');
1658 strcpy(sl
, "mdmon");
1664 /* FIXME yuk. CLOSE_EXEC?? */
1666 for (i
= 3; skipped
< 20; i
++)
1672 for (i
= 0; paths
[i
]; i
++)
1675 execl(paths
[i
], "mdmon", "--offroot",
1676 devnum2devname(devnum
),
1679 execl(paths
[i
], "mdmon",
1680 devnum2devname(devnum
),
1685 case -1: pr_err("cannot run mdmon. "
1686 "Array remains readonly\n");
1688 default: /* parent - good */
1689 pid
= wait(&status
);
1690 if (pid
< 0 || status
!= 0)
1696 int check_env(char *name
)
1698 char *val
= getenv(name
);
1700 if (val
&& atoi(val
) == 1)
1706 __u32
random32(void)
1709 int rfd
= open("/dev/urandom", O_RDONLY
);
1710 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1718 int flush_metadata_updates(struct supertype
*st
)
1722 st
->update_tail
= NULL
;
1726 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1730 while (st
->updates
) {
1731 struct metadata_update
*mu
= st
->updates
;
1732 st
->updates
= mu
->next
;
1734 send_message(sfd
, mu
, 0);
1742 st
->update_tail
= NULL
;
1746 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1749 struct metadata_update
*mu
= xmalloc(sizeof(*mu
));
1754 mu
->space_list
= NULL
;
1756 *st
->update_tail
= mu
;
1757 st
->update_tail
= &mu
->next
;
1759 #endif /* MDASSEMBLE */
1762 /* tinyc doesn't optimize this check in ioctl.h out ... */
1763 unsigned int __invalid_size_argument_for_IOC
= 0;
1766 int experimental(void)
1768 if (check_env("MDADM_EXPERIMENTAL"))
1771 pr_err("To use this feature MDADM_EXPERIMENTAL"
1772 " environment variable has to be defined.\n");
1777 /* Pick all spares matching given criteria from a container
1778 * if min_size == 0 do not check size
1779 * if domlist == NULL do not check domains
1780 * if spare_group given add it to domains of each spare
1781 * metadata allows to test domains using metadata of destination array */
1782 struct mdinfo
*container_choose_spares(struct supertype
*st
,
1783 unsigned long long min_size
,
1784 struct domainlist
*domlist
,
1786 const char *metadata
, int get_one
)
1788 struct mdinfo
*d
, **dp
, *disks
= NULL
;
1790 /* get list of all disks in container */
1791 if (st
->ss
->getinfo_super_disks
)
1792 disks
= st
->ss
->getinfo_super_disks(st
);
1796 /* find spare devices on the list */
1798 disks
->array
.spare_disks
= 0;
1802 if (d
->disk
.state
== 0) {
1803 /* check if size is acceptable */
1804 unsigned long long dev_size
;
1805 dev_t dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
1808 (dev_size_from_id(dev
, &dev_size
) &&
1809 dev_size
>= min_size
))
1811 /* check if domain matches */
1812 if (found
&& domlist
) {
1813 struct dev_policy
*pol
= devnum_policy(dev
);
1815 pol_add(&pol
, pol_domain
,
1817 if (domain_test(domlist
, pol
, metadata
) != 1)
1819 dev_policy_free(pol
);
1824 disks
->array
.spare_disks
++;
1838 /* Checks if paths point to the same device
1839 * Returns 0 if they do.
1840 * Returns 1 if they don't.
1841 * Returns -1 if something went wrong,
1842 * e.g. paths are empty or the files
1843 * they point to don't exist */
1844 int compare_paths (char* path1
, char* path2
)
1846 struct stat st1
,st2
;
1848 if (path1
== NULL
|| path2
== NULL
)
1850 if (stat(path1
,&st1
) != 0)
1852 if (stat(path2
,&st2
) != 0)
1854 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
))