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 * INVALID_SECTORS returned on error.
200 long long s
= strtoll(size
, &c
, 10);
214 s
*= 1024 * 1024 * 2;
216 case 's': /* sectors */
227 int parse_layout_10(char *layout
)
231 /* Parse the layout string for raid10 */
232 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
233 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
234 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
238 if (layout
[0] == 'n')
240 else if (layout
[0] == 'o')
241 rv
= 0x10000 + (copies
<<8) + 1;
243 rv
= 1 + (copies
<<8);
247 int parse_layout_faulty(char *layout
)
249 /* Parse the layout string for 'faulty' */
250 int ln
= strcspn(layout
, "0123456789");
251 char *m
= xstrdup(layout
);
254 mode
= map_name(faultylayout
, m
);
258 return mode
| (atoi(layout
+ln
)<< ModeShift
);
261 long parse_num(char *num
)
263 /* Either return a valid number, or -1 */
265 long rv
= strtol(num
, &c
, 10);
266 if (rv
< 0 || *c
|| !num
[0])
273 void remove_partitions(int fd
)
275 /* remove partitions from this block devices.
276 * This is used for components added to an array
278 #ifdef BLKPG_DEL_PARTITION
279 struct blkpg_ioctl_arg a
;
280 struct blkpg_partition p
;
282 a
.op
= BLKPG_DEL_PARTITION
;
284 a
.datalen
= sizeof(p
);
286 memset(a
.data
, 0, a
.datalen
);
287 for (p
.pno
= 0; p
.pno
< 16; p
.pno
++)
288 ioctl(fd
, BLKPG
, &a
);
292 int test_partition(int fd
)
294 /* Check if fd is a whole-disk or a partition.
295 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
296 * will return ENXIO on an invalid partition number.
298 struct blkpg_ioctl_arg a
;
299 struct blkpg_partition p
;
300 a
.op
= BLKPG_DEL_PARTITION
;
302 a
.datalen
= sizeof(p
);
304 memset(a
.data
, 0, a
.datalen
);
306 if (ioctl(fd
, BLKPG
, &a
) == 0)
307 /* Very unlikely, but not a partition */
310 /* not a partition */
316 int test_partition_from_id(dev_t id
)
321 sprintf(buf
, "%d:%d", major(id
), minor(id
));
322 fd
= dev_open(buf
, O_RDONLY
);
325 rv
= test_partition(fd
);
330 int enough(int level
, int raid_disks
, int layout
, int clean
, char *avail
)
336 for (i
= 0; i
< raid_disks
; i
++)
337 avail_disks
+= !!avail
[i
];
341 /* This is the tricky one - we need to check
342 * which actual disks are present.
344 copies
= (layout
&255)* ((layout
>>8) & 255);
347 /* there must be one of the 'copies' form 'first' */
354 this = (this+1) % raid_disks
;
358 first
= (first
+(layout
&255)) % raid_disks
;
359 } while (first
!= 0);
362 case LEVEL_MULTIPATH
:
363 return avail_disks
>= 1;
366 return avail_disks
== raid_disks
;
368 return avail_disks
>= 1;
372 return avail_disks
>= raid_disks
-1;
374 return avail_disks
>= raid_disks
;
377 return avail_disks
>= raid_disks
-2;
379 return avail_disks
>= raid_disks
;
385 int enough_fd(int fd
)
387 struct mdu_array_info_s array
;
388 struct mdu_disk_info_s disk
;
393 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0 ||
394 array
.raid_disks
<= 0)
396 avail
= xcalloc(array
.raid_disks
, 1);
397 for (i
= 0; i
< MAX_DISKS
&& array
.nr_disks
> 0; i
++) {
399 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
401 if (disk
.major
== 0 && disk
.minor
== 0)
405 if (! (disk
.state
& (1<<MD_DISK_SYNC
)))
407 if (disk
.raid_disk
< 0 || disk
.raid_disk
>= array
.raid_disks
)
410 avail
[disk
.raid_disk
] = 1;
412 /* This is used on an active array, so assume it is clean */
413 rv
= enough(array
.level
, array
.raid_disks
, array
.layout
,
419 const int uuid_zero
[4] = { 0, 0, 0, 0 };
421 int same_uuid(int a
[4], int b
[4], int swapuuid
)
424 /* parse uuids are hostendian.
425 * uuid's from some superblocks are big-ending
426 * if there is a difference, we need to swap..
428 unsigned char *ac
= (unsigned char *)a
;
429 unsigned char *bc
= (unsigned char *)b
;
431 for (i
= 0; i
< 16; i
+= 4) {
432 if (ac
[i
+0] != bc
[i
+3] ||
433 ac
[i
+1] != bc
[i
+2] ||
434 ac
[i
+2] != bc
[i
+1] ||
449 void copy_uuid(void *a
, int b
[4], int swapuuid
)
452 /* parse uuids are hostendian.
453 * uuid's from some superblocks are big-ending
454 * if there is a difference, we need to swap..
456 unsigned char *ac
= (unsigned char *)a
;
457 unsigned char *bc
= (unsigned char *)b
;
459 for (i
= 0; i
< 16; i
+= 4) {
469 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
476 copy_uuid(uuid
, id
, swap
);
477 for (i
= 0; i
< 4; i
++) {
480 for (j
= 3; j
>= 0; j
--) {
481 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
489 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
491 // dirty hack to work around an issue with super1 superblocks...
492 // super1 superblocks need swapuuid set in order for assembly to
493 // work, but can't have it set if we want this printout to match
494 // all the other uuid printouts in super1.c, so we force swapuuid
495 // to 1 to make our printout match the rest of super1
496 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
500 int check_ext2(int fd
, char *name
)
503 * Check for an ext2fs file system.
504 * Superblock is always 1K at 1K offset
506 * s_magic is le16 at 56 == 0xEF53
507 * report mtime - le32 at 44
509 * logblksize - le32 at 24
511 unsigned char sb
[1024];
514 if (lseek(fd
, 1024,0)!= 1024)
516 if (read(fd
, sb
, 1024)!= 1024)
518 if (sb
[56] != 0x53 || sb
[57] != 0xef)
521 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
522 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
523 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
524 pr_err("%s appears to contain an ext2fs file system\n",
526 fprintf(stderr
," size=%dK mtime=%s",
527 size
*(1<<bsize
), ctime(&mtime
));
531 int check_reiser(int fd
, char *name
)
534 * superblock is at 64K
536 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
539 unsigned char sb
[1024];
541 if (lseek(fd
, 64*1024, 0) != 64*1024)
543 if (read(fd
, sb
, 1024) != 1024)
545 if (strncmp((char*)sb
+52, "ReIsErFs",8) != 0 &&
546 strncmp((char*)sb
+52, "ReIsEr2Fs",9) != 0)
548 pr_err("%s appears to contain a reiserfs file system\n",name
);
549 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
550 fprintf(stderr
, " size = %luK\n", size
*4);
555 int check_raid(int fd
, char *name
)
560 struct supertype
*st
= guess_super(fd
);
564 st
->ss
->load_super(st
, fd
, name
);
565 /* Looks like a raid array .. */
566 pr_err("%s appears to be part of a raid array:\n",
568 st
->ss
->getinfo_super(st
, &info
, NULL
);
569 st
->ss
->free_super(st
);
570 crtime
= info
.array
.ctime
;
571 level
= map_num(pers
, info
.array
.level
);
572 if (!level
) level
= "-unknown-";
573 fprintf(stderr
, " level=%s devices=%d ctime=%s",
574 level
, info
.array
.raid_disks
, ctime(&crtime
));
582 for (i
= 0; i
< 5; i
++) {
584 fprintf(stderr
, "%s%s", mesg
, add
);
586 if (fgets(buf
, 100, stdin
)==NULL
)
588 if (buf
[0]=='y' || buf
[0]=='Y')
590 if (buf
[0]=='n' || buf
[0]=='N')
594 pr_err("assuming 'no'\n");
597 #endif /* MDASSEMBLE */
599 int is_standard(char *dev
, int *nump
)
601 /* tests if dev is a "standard" md dev name.
602 * i.e if the last component is "/dNN" or "/mdNN",
603 * where NN is a string of digits
604 * Returns 1 if a partitionable standard,
605 * -1 if non-partitonable,
606 * 0 if not a standard name.
608 char *d
= strrchr(dev
, '/');
613 if (strncmp(d
, "/d",2) == 0)
614 d
+= 2, type
= 1; /* /dev/md/dN{pM} */
615 else if (strncmp(d
, "/md_d", 5) == 0)
616 d
+= 5, type
= 1; /* /dev/md_dN{pM} */
617 else if (strncmp(d
, "/md", 3) == 0)
618 d
+= 3, type
= -1; /* /dev/mdN */
619 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3) == 0)
620 d
+= 1, type
= -1; /* /dev/md/N */
630 if (nump
) *nump
= num
;
635 unsigned long calc_csum(void *super
, int bytes
)
637 unsigned long long newcsum
= 0;
640 unsigned int *superc
= (unsigned int*) super
;
642 for(i
= 0; i
< bytes
/4; i
++)
643 newcsum
+= superc
[i
];
644 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
646 /* The in-kernel checksum calculation is always 16bit on
647 * the alpha, though it is 32 bit on i386...
648 * I wonder what it is elsewhere... (it uses an API in
649 * a way that it shouldn't).
651 csum
= (csum
& 0xffff) + (csum
>> 16);
652 csum
= (csum
& 0xffff) + (csum
>> 16);
658 char *human_size(long long bytes
)
662 /* We convert bytes to either centi-M{ega,ibi}bytes or
663 * centi-G{igi,ibi}bytes, with appropriate rounding,
664 * and then print 1/100th of those as a decimal.
665 * We allow upto 2048Megabytes before converting to
666 * gigabytes, as that shows more precision and isn't
667 * too large a number.
668 * Terabytes are not yet handled.
671 if (bytes
< 5000*1024)
673 else if (bytes
< 2*1024LL*1024LL*1024LL) {
674 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
675 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
676 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
677 cMiB
/100 , cMiB
% 100,
680 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
681 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
682 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
683 cGiB
/100 , cGiB
% 100,
689 char *human_size_brief(long long bytes
, int prefix
)
693 /* We convert bytes to either centi-M{ega,ibi}bytes or
694 * centi-G{igi,ibi}bytes, with appropriate rounding,
695 * and then print 1/100th of those as a decimal.
696 * We allow upto 2048Megabytes before converting to
697 * gigabytes, as that shows more precision and isn't
698 * too large a number.
699 * Terabytes are not yet handled.
701 * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
702 * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
705 if (bytes
< 5000*1024)
707 else if (prefix
== IEC
) {
708 if (bytes
< 2*1024LL*1024LL*1024LL) {
709 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
710 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
711 cMiB
/100 , cMiB
% 100);
713 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
714 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
715 cGiB
/100 , cGiB
% 100);
718 else if (prefix
== JEDEC
) {
719 if (bytes
< 2*1024LL*1024LL*1024LL) {
720 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
721 snprintf(buf
, sizeof(buf
), "%ld.%02ldMB",
724 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
725 snprintf(buf
, sizeof(buf
), "%ld.%02ldGB",
726 cGB
/100 , cGB
% 100);
735 void print_r10_layout(int layout
)
737 int near
= layout
& 255;
738 int far
= (layout
>> 8) & 255;
739 int offset
= (layout
&0x10000);
743 printf("%s near=%d", sep
, near
);
747 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
749 printf("NO REDUNDANCY");
753 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
754 int chunksize
, unsigned long long devsize
)
758 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
759 return get_data_disks(level
, layout
, raid_disks
) * devsize
;
762 int get_data_disks(int level
, int layout
, int raid_disks
)
766 case 0: data_disks
= raid_disks
;
768 case 1: data_disks
= 1;
771 case 5: data_disks
= raid_disks
- 1;
773 case 6: data_disks
= raid_disks
- 2;
775 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
782 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
783 char *get_md_name(int dev
)
785 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
786 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
787 static char devname
[50];
793 int mdp
= get_mdp_major();
794 if (mdp
< 0) return NULL
;
795 rdev
= makedev(mdp
, (-1-dev
)<<6);
796 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
797 if (stat(devname
, &stb
) == 0
798 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
799 && (stb
.st_rdev
== rdev
))
802 rdev
= makedev(MD_MAJOR
, dev
);
803 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
804 if (stat(devname
, &stb
) == 0
805 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
806 && (stb
.st_rdev
== rdev
))
809 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
810 if (stat(devname
, &stb
) == 0
811 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
812 && (stb
.st_rdev
== rdev
))
815 dn
= map_dev(major(rdev
), minor(rdev
), 0);
818 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
819 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
823 if (stat(devname
, &stb
) == 0
824 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
825 && (stb
.st_rdev
== rdev
))
831 void put_md_name(char *name
)
833 if (strncmp(name
, "/dev/.tmp.md", 12) == 0)
837 int find_free_devnum(int use_partitions
)
840 for (devnum
= 127; devnum
!= 128;
841 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
845 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
846 if (mddev_busy(_devnum
))
848 sprintf(nbuf
, "%s%d", use_partitions
?"mdp":"md", devnum
);
849 if (!conf_name_is_free(nbuf
))
852 /* make sure it is new to /dev too, at least as a
854 char *dn
= map_dev(dev2major(_devnum
),
855 dev2minor(_devnum
), 0);
856 if (dn
&& ! is_standard(dn
, NULL
))
863 return use_partitions
? (-1-devnum
) : devnum
;
865 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
867 int dev_open(char *dev
, int flags
)
869 /* like 'open', but if 'dev' matches %d:%d, create a temp
870 * block device and open that
881 major
= strtoul(dev
, &e
, 0);
882 if (e
> dev
&& *e
== ':' && e
[1] &&
883 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
885 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
886 (int)getpid(), major
, minor
);
887 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
888 fd
= open(devname
, flags
);
892 /* Try /tmp as /dev appear to be read-only */
893 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
894 (int)getpid(), major
, minor
);
895 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
896 fd
= open(devname
, flags
);
901 fd
= open(dev
, flags
);
905 int open_dev_flags(int devnum
, int flags
)
909 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
910 return dev_open(buf
, flags
);
913 int open_dev(int devnum
)
915 return open_dev_flags(devnum
, O_RDONLY
);
918 int open_dev_excl(int devnum
)
924 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
925 for (i
= 0 ; i
< 25 ; i
++) {
926 int fd
= dev_open(buf
, flags
|O_EXCL
);
929 if (errno
== EACCES
&& flags
== O_RDWR
) {
940 int same_dev(char *one
, char *two
)
942 struct stat st1
, st2
;
943 if (stat(one
, &st1
) != 0)
945 if (stat(two
, &st2
) != 0)
947 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
949 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
951 return st1
.st_rdev
== st2
.st_rdev
;
954 void wait_for(char *dev
, int fd
)
957 struct stat stb_want
;
959 if (fstat(fd
, &stb_want
) != 0 ||
960 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
963 for (i
= 0 ; i
< 25 ; i
++) {
965 if (stat(dev
, &stb
) == 0 &&
966 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
967 (stb
.st_rdev
== stb_want
.st_rdev
))
972 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
975 struct superswitch
*superlist
[] =
978 &super_ddf
, &super_imsm
,
982 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
984 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
986 mdu_array_info_t array
;
989 struct supertype
*st
= NULL
;
994 char *subarray
= NULL
;
995 int container
= NoMdDev
;
997 sra
= sysfs_read(fd
, 0, GET_VERSION
);
1000 vers
= sra
->array
.major_version
;
1001 minor
= sra
->array
.minor_version
;
1002 verstr
= sra
->text_version
;
1004 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1005 array
.major_version
= array
.minor_version
= 0;
1006 vers
= array
.major_version
;
1007 minor
= array
.minor_version
;
1012 sprintf(version
, "%d.%d", vers
, minor
);
1015 if (minor
== -2 && is_subarray(verstr
)) {
1016 char *dev
= verstr
+1;
1018 subarray
= strchr(dev
, '/');
1021 subarray
= xstrdup(subarray
);
1023 container
= devname2devnum(dev
);
1026 sra
= sysfs_read(-1, container
, GET_VERSION
);
1027 if (sra
&& sra
->text_version
[0])
1028 verstr
= sra
->text_version
;
1030 verstr
= "-no-metadata-";
1033 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1034 st
= superlist
[i
]->match_metadata_desc(verstr
);
1041 *subarrayp
= subarray
;
1042 st
->container_dev
= container
;
1043 st
->devnum
= fd2devnum(fd
);
1049 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1051 int dev_size_from_id(dev_t id
, unsigned long long *size
)
1056 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1057 fd
= dev_open(buf
, O_RDONLY
);
1060 if (get_dev_size(fd
, NULL
, size
)) {
1068 struct supertype
*dup_super(struct supertype
*orig
)
1070 struct supertype
*st
;
1074 st
= xcalloc(1, sizeof(*st
));
1076 st
->max_devs
= orig
->max_devs
;
1077 st
->minor_version
= orig
->minor_version
;
1078 st
->ignore_hw_compat
= orig
->ignore_hw_compat
;
1084 struct supertype
*guess_super_type(int fd
, enum guess_types guess_type
)
1086 /* try each load_super to find the best match,
1087 * and return the best superswitch
1089 struct superswitch
*ss
;
1090 struct supertype
*st
;
1091 time_t besttime
= 0;
1095 st
= xcalloc(1, sizeof(*st
));
1096 st
->container_dev
= NoMdDev
;
1098 for (i
= 0 ; superlist
[i
]; i
++) {
1101 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1103 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1105 memset(st
, 0, sizeof(*st
));
1106 st
->ignore_hw_compat
= 1;
1107 rv
= ss
->load_super(st
, fd
, NULL
);
1110 st
->ss
->getinfo_super(st
, &info
, NULL
);
1111 if (bestsuper
== -1 ||
1112 besttime
< info
.array
.ctime
) {
1114 besttime
= info
.array
.ctime
;
1119 if (bestsuper
!= -1) {
1121 memset(st
, 0, sizeof(*st
));
1122 st
->ignore_hw_compat
= 1;
1123 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1125 superlist
[bestsuper
]->free_super(st
);
1133 /* Return size of device in bytes */
1134 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1136 unsigned long long ldsize
;
1139 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1140 ldsize
= (unsigned long long)st
.st_size
;
1143 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1146 unsigned long dsize
;
1147 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1152 pr_err("Cannot get size of %s: %s\b",
1153 dname
, strerror(errno
));
1161 /* Return true if this can only be a container, not a member device.
1162 * i.e. is and md device and size is zero
1164 int must_be_container(int fd
)
1166 unsigned long long size
;
1167 if (md_get_version(fd
) < 0)
1169 if (get_dev_size(fd
, NULL
, &size
) == 0)
1176 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1177 * Returns: 1 if successful
1178 * -1 for unknown partition type
1179 * 0 for other errors
1181 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1184 unsigned char empty_gpt_entry
[16]= {0};
1185 struct GPT_part_entry
*part
;
1187 unsigned long long curr_part_end
;
1188 unsigned all_partitions
, entry_size
;
1193 BUILD_BUG_ON(sizeof(gpt
) != 512);
1194 /* skip protective MBR */
1195 lseek(fd
, 512, SEEK_SET
);
1196 /* read GPT header */
1197 if (read(fd
, &gpt
, 512) != 512)
1200 /* get the number of partition entries and the entry size */
1201 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1202 entry_size
= __le32_to_cpu(gpt
.part_size
);
1204 /* Check GPT signature*/
1205 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1209 if (all_partitions
> 1024 ||
1210 entry_size
> sizeof(buf
))
1213 part
= (struct GPT_part_entry
*)buf
;
1215 for (part_nr
= 0; part_nr
< all_partitions
; part_nr
++) {
1216 /* read partition entry */
1217 if (read(fd
, buf
, entry_size
) != (ssize_t
)entry_size
)
1220 /* is this valid partition? */
1221 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1222 /* check the last lba for the current partition */
1223 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1224 if (curr_part_end
> *endofpart
)
1225 *endofpart
= curr_part_end
;
1232 /* Sets endofpart parameter to the last block used by the last partition on the device.
1233 * Returns: 1 if successful
1234 * -1 for unknown partition type
1235 * 0 for other errors
1237 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1239 struct MBR boot_sect
;
1240 struct MBR_part_record
*part
;
1241 unsigned long long curr_part_end
;
1247 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1250 if (read(fd
, &boot_sect
, 512) != 512)
1253 /* check MBP signature */
1254 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1256 /* found the correct signature */
1257 part
= boot_sect
.parts
;
1259 for (part_nr
= 0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1260 /* check for GPT type */
1261 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1262 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1265 /* check the last used lba for the current partition */
1266 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1267 __le32_to_cpu(part
->blocks_num
);
1268 if (curr_part_end
> *endofpart
)
1269 *endofpart
= curr_part_end
;
1274 /* Unknown partition table */
1281 int check_partitions(int fd
, char *dname
, unsigned long long freesize
,
1282 unsigned long long size
)
1285 * Check where the last partition ends
1287 unsigned long long endofpart
;
1290 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1291 /* There appears to be a partition table here */
1292 if (freesize
== 0) {
1293 /* partitions will not be visible in new device */
1294 pr_err("partition table exists on %s but will be lost or\n"
1295 " meaningless after creating array\n",
1298 } else if (endofpart
> freesize
) {
1299 /* last partition overlaps metadata */
1300 pr_err("metadata will over-write last partition on %s.\n",
1303 } else if (size
&& endofpart
> size
) {
1304 /* partitions will be truncated in new device */
1305 pr_err("array size is too small to cover all partitions on %s.\n",
1313 int open_container(int fd
)
1315 /* 'fd' is a block device. Find out if it is in use
1316 * by a container, and return an open fd on that container.
1327 if (fstat(fd
, &st
) != 0)
1329 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1330 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1331 e
= path
+ strlen(path
);
1333 dir
= opendir(path
);
1336 while ((de
= readdir(dir
))) {
1339 if (de
->d_name
[0] == '.')
1341 /* Need to make sure it is a container and not a volume */
1342 sprintf(e
, "/%s/md/metadata_version", de
->d_name
);
1343 dfd
= open(path
, O_RDONLY
);
1346 n
= read(dfd
, buf
, sizeof(buf
));
1348 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1351 if (strncmp(buf
, "external", 8) != 0 ||
1355 sprintf(e
, "/%s/dev", de
->d_name
);
1356 dfd
= open(path
, O_RDONLY
);
1359 n
= read(dfd
, buf
, sizeof(buf
));
1361 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1364 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1366 sprintf(buf
, "%d:%d", major
, minor
);
1367 dfd
= dev_open(buf
, O_RDONLY
);
1377 struct superswitch
*version_to_superswitch(char *vers
)
1381 for (i
= 0; superlist
[i
]; i
++) {
1382 struct superswitch
*ss
= superlist
[i
];
1384 if (strcmp(vers
, ss
->name
) == 0)
1391 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1393 if (mdstat
->metadata_version
== NULL
||
1394 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1395 !is_subarray(mdstat
->metadata_version
+9) ||
1396 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
1397 mdstat
->metadata_version
[10+strlen(container
)] != '/')
1403 int is_subarray_active(char *subarray
, char *container
)
1405 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1406 struct mdstat_ent
*ent
;
1408 for (ent
= mdstat
; ent
; ent
= ent
->next
)
1409 if (is_container_member(ent
, container
))
1410 if (strcmp(to_subarray(ent
, container
), subarray
) == 0)
1413 free_mdstat(mdstat
);
1418 /* open_subarray - opens a subarray in a container
1419 * @dev: container device name
1420 * @st: empty supertype
1421 * @quiet: block reporting errors flag
1423 * On success returns an fd to a container and fills in *st
1425 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1428 struct mdinfo
*info
;
1431 fd
= open(dev
, O_RDWR
|O_EXCL
);
1434 pr_err("Couldn't open %s, aborting\n",
1439 st
->devnum
= fd2devnum(fd
);
1440 if (st
->devnum
== NoMdDev
) {
1442 pr_err("Failed to determine device number for %s\n",
1447 mdi
= sysfs_read(fd
, st
->devnum
, GET_VERSION
|GET_LEVEL
);
1450 pr_err("Failed to read sysfs for %s\n",
1455 if (mdi
->array
.level
!= UnSet
) {
1457 pr_err("%s is not a container\n", dev
);
1461 st
->ss
= version_to_superswitch(mdi
->text_version
);
1464 pr_err("Operation not supported for %s metadata\n",
1469 st
->devname
= devnum2devname(st
->devnum
);
1472 pr_err("Failed to allocate device name\n");
1476 if (!st
->ss
->load_container
) {
1478 pr_err("%s is not a container\n", dev
);
1482 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1484 pr_err("Failed to load metadata for %s\n",
1489 info
= st
->ss
->container_content(st
, subarray
);
1492 pr_err("Failed to find subarray-%s in %s\n",
1502 st
->ss
->free_super(st
);
1518 int add_disk(int mdfd
, struct supertype
*st
,
1519 struct mdinfo
*sra
, struct mdinfo
*info
)
1521 /* Add a device to an array, in one of 2 ways. */
1524 if (st
->ss
->external
) {
1525 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1526 info
->recovery_start
= MaxSector
;
1528 info
->recovery_start
= 0;
1529 rv
= sysfs_add_disk(sra
, info
, 0);
1532 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1536 sd2
= xmalloc(sizeof(*sd2
));
1538 sd2
->next
= sra
->devs
;
1544 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1548 int remove_disk(int mdfd
, struct supertype
*st
,
1549 struct mdinfo
*sra
, struct mdinfo
*info
)
1552 /* Remove the disk given by 'info' from the array */
1554 if (st
->ss
->external
)
1555 rv
= sysfs_set_str(sra
, info
, "slot", "none");
1558 rv
= ioctl(mdfd
, HOT_REMOVE_DISK
, makedev(info
->disk
.major
,
1563 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1565 /* Initialise kernel's knowledge of array.
1566 * This varies between externally managed arrays
1569 int vers
= md_get_version(mdfd
);
1573 if (st
->ss
->external
)
1574 rv
= sysfs_set_array(info
, vers
);
1577 if ((vers
% 100) >= 1) { /* can use different versions */
1578 mdu_array_info_t inf
;
1579 memset(&inf
, 0, sizeof(inf
));
1580 inf
.major_version
= info
->array
.major_version
;
1581 inf
.minor_version
= info
->array
.minor_version
;
1582 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1584 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1588 unsigned long long min_recovery_start(struct mdinfo
*array
)
1590 /* find the minimum recovery_start in an array for metadata
1591 * formats that only record per-array recovery progress instead
1594 unsigned long long recovery_start
= MaxSector
;
1597 for (d
= array
->devs
; d
; d
= d
->next
)
1598 recovery_start
= min(recovery_start
, d
->recovery_start
);
1600 return recovery_start
;
1603 int mdmon_pid(int devnum
)
1609 char *devname
= devnum2devname(devnum
);
1611 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devname
);
1614 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1618 n
= read(fd
, pid
, 9);
1625 int mdmon_running(int devnum
)
1627 int pid
= mdmon_pid(devnum
);
1630 if (kill(pid
, 0) == 0)
1635 int start_mdmon(int devnum
)
1649 if (check_env("MDADM_NO_MDMON"))
1652 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
)-1);
1656 sl
= strrchr(pathbuf
, '/');
1661 strcpy(sl
, "mdmon");
1667 /* FIXME yuk. CLOSE_EXEC?? */
1669 for (i
= 3; skipped
< 20; i
++)
1675 for (i
= 0; paths
[i
]; i
++)
1678 execl(paths
[i
], "mdmon", "--offroot",
1679 devnum2devname(devnum
),
1682 execl(paths
[i
], "mdmon",
1683 devnum2devname(devnum
),
1688 case -1: pr_err("cannot run mdmon. "
1689 "Array remains readonly\n");
1691 default: /* parent - good */
1692 pid
= wait(&status
);
1693 if (pid
< 0 || status
!= 0)
1699 int check_env(char *name
)
1701 char *val
= getenv(name
);
1703 if (val
&& atoi(val
) == 1)
1709 __u32
random32(void)
1712 int rfd
= open("/dev/urandom", O_RDONLY
);
1713 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1721 int flush_metadata_updates(struct supertype
*st
)
1725 st
->update_tail
= NULL
;
1729 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1733 while (st
->updates
) {
1734 struct metadata_update
*mu
= st
->updates
;
1735 st
->updates
= mu
->next
;
1737 send_message(sfd
, mu
, 0);
1745 st
->update_tail
= NULL
;
1749 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1752 struct metadata_update
*mu
= xmalloc(sizeof(*mu
));
1757 mu
->space_list
= NULL
;
1759 *st
->update_tail
= mu
;
1760 st
->update_tail
= &mu
->next
;
1762 #endif /* MDASSEMBLE */
1765 /* tinyc doesn't optimize this check in ioctl.h out ... */
1766 unsigned int __invalid_size_argument_for_IOC
= 0;
1769 int experimental(void)
1771 if (check_env("MDADM_EXPERIMENTAL"))
1774 pr_err("To use this feature MDADM_EXPERIMENTAL"
1775 " environment variable has to be defined.\n");
1780 /* Pick all spares matching given criteria from a container
1781 * if min_size == 0 do not check size
1782 * if domlist == NULL do not check domains
1783 * if spare_group given add it to domains of each spare
1784 * metadata allows to test domains using metadata of destination array */
1785 struct mdinfo
*container_choose_spares(struct supertype
*st
,
1786 unsigned long long min_size
,
1787 struct domainlist
*domlist
,
1789 const char *metadata
, int get_one
)
1791 struct mdinfo
*d
, **dp
, *disks
= NULL
;
1793 /* get list of all disks in container */
1794 if (st
->ss
->getinfo_super_disks
)
1795 disks
= st
->ss
->getinfo_super_disks(st
);
1799 /* find spare devices on the list */
1801 disks
->array
.spare_disks
= 0;
1805 if (d
->disk
.state
== 0) {
1806 /* check if size is acceptable */
1807 unsigned long long dev_size
;
1808 dev_t dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
1811 (dev_size_from_id(dev
, &dev_size
) &&
1812 dev_size
>= min_size
))
1814 /* check if domain matches */
1815 if (found
&& domlist
) {
1816 struct dev_policy
*pol
= devnum_policy(dev
);
1818 pol_add(&pol
, pol_domain
,
1820 if (domain_test(domlist
, pol
, metadata
) != 1)
1822 dev_policy_free(pol
);
1827 disks
->array
.spare_disks
++;
1841 /* Checks if paths point to the same device
1842 * Returns 0 if they do.
1843 * Returns 1 if they don't.
1844 * Returns -1 if something went wrong,
1845 * e.g. paths are empty or the files
1846 * they point to don't exist */
1847 int compare_paths (char* path1
, char* path2
)
1849 struct stat st1
,st2
;
1851 if (path1
== NULL
|| path2
== NULL
)
1853 if (stat(path1
,&st1
) != 0)
1855 if (stat(path2
,&st2
) != 0)
1857 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
))