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' */
350 first
= (first
+1) % raid_disks
;
355 } while (first
!= 0);
358 case LEVEL_MULTIPATH
:
359 return avail_disks
>= 1;
362 return avail_disks
== raid_disks
;
364 return avail_disks
>= 1;
368 return avail_disks
>= raid_disks
-1;
370 return avail_disks
>= raid_disks
;
373 return avail_disks
>= raid_disks
-2;
375 return avail_disks
>= raid_disks
;
381 int enough_fd(int fd
)
383 struct mdu_array_info_s array
;
384 struct mdu_disk_info_s disk
;
389 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0 ||
390 array
.raid_disks
<= 0)
392 avail
= xcalloc(array
.raid_disks
, 1);
393 for (i
= 0; i
< MAX_DISKS
&& array
.nr_disks
> 0; i
++) {
395 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
397 if (disk
.major
== 0 && disk
.minor
== 0)
401 if (! (disk
.state
& (1<<MD_DISK_SYNC
)))
403 if (disk
.raid_disk
< 0 || disk
.raid_disk
>= array
.raid_disks
)
406 avail
[disk
.raid_disk
] = 1;
408 /* This is used on an active array, so assume it is clean */
409 rv
= enough(array
.level
, array
.raid_disks
, array
.layout
,
415 const int uuid_zero
[4] = { 0, 0, 0, 0 };
417 int same_uuid(int a
[4], int b
[4], int swapuuid
)
420 /* parse uuids are hostendian.
421 * uuid's from some superblocks are big-ending
422 * if there is a difference, we need to swap..
424 unsigned char *ac
= (unsigned char *)a
;
425 unsigned char *bc
= (unsigned char *)b
;
427 for (i
= 0; i
< 16; i
+= 4) {
428 if (ac
[i
+0] != bc
[i
+3] ||
429 ac
[i
+1] != bc
[i
+2] ||
430 ac
[i
+2] != bc
[i
+1] ||
445 void copy_uuid(void *a
, int b
[4], int swapuuid
)
448 /* parse uuids are hostendian.
449 * uuid's from some superblocks are big-ending
450 * if there is a difference, we need to swap..
452 unsigned char *ac
= (unsigned char *)a
;
453 unsigned char *bc
= (unsigned char *)b
;
455 for (i
= 0; i
< 16; i
+= 4) {
465 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
472 copy_uuid(uuid
, id
, swap
);
473 for (i
= 0; i
< 4; i
++) {
476 for (j
= 3; j
>= 0; j
--) {
477 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
485 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
487 // dirty hack to work around an issue with super1 superblocks...
488 // super1 superblocks need swapuuid set in order for assembly to
489 // work, but can't have it set if we want this printout to match
490 // all the other uuid printouts in super1.c, so we force swapuuid
491 // to 1 to make our printout match the rest of super1
492 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
496 int check_ext2(int fd
, char *name
)
499 * Check for an ext2fs file system.
500 * Superblock is always 1K at 1K offset
502 * s_magic is le16 at 56 == 0xEF53
503 * report mtime - le32 at 44
505 * logblksize - le32 at 24
507 unsigned char sb
[1024];
510 if (lseek(fd
, 1024,0)!= 1024)
512 if (read(fd
, sb
, 1024)!= 1024)
514 if (sb
[56] != 0x53 || sb
[57] != 0xef)
517 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
518 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
519 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
520 pr_err("%s appears to contain an ext2fs file system\n",
522 fprintf(stderr
," size=%dK mtime=%s",
523 size
*(1<<bsize
), ctime(&mtime
));
527 int check_reiser(int fd
, char *name
)
530 * superblock is at 64K
532 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
535 unsigned char sb
[1024];
537 if (lseek(fd
, 64*1024, 0) != 64*1024)
539 if (read(fd
, sb
, 1024) != 1024)
541 if (strncmp((char*)sb
+52, "ReIsErFs",8) != 0 &&
542 strncmp((char*)sb
+52, "ReIsEr2Fs",9) != 0)
544 pr_err("%s appears to contain a reiserfs file system\n",name
);
545 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
546 fprintf(stderr
, " size = %luK\n", size
*4);
551 int check_raid(int fd
, char *name
)
556 struct supertype
*st
= guess_super(fd
);
559 st
->ignore_hw_compat
= 1;
560 st
->ss
->load_super(st
, fd
, name
);
561 /* Looks like a raid array .. */
562 pr_err("%s appears to be part of a raid array:\n",
564 st
->ss
->getinfo_super(st
, &info
, NULL
);
565 st
->ss
->free_super(st
);
566 crtime
= info
.array
.ctime
;
567 level
= map_num(pers
, info
.array
.level
);
568 if (!level
) level
= "-unknown-";
569 fprintf(stderr
, " level=%s devices=%d ctime=%s",
570 level
, info
.array
.raid_disks
, ctime(&crtime
));
578 for (i
= 0; i
< 5; i
++) {
580 fprintf(stderr
, "%s%s", mesg
, add
);
582 if (fgets(buf
, 100, stdin
)==NULL
)
584 if (buf
[0]=='y' || buf
[0]=='Y')
586 if (buf
[0]=='n' || buf
[0]=='N')
590 pr_err("assuming 'no'\n");
593 #endif /* MDASSEMBLE */
595 int is_standard(char *dev
, int *nump
)
597 /* tests if dev is a "standard" md dev name.
598 * i.e if the last component is "/dNN" or "/mdNN",
599 * where NN is a string of digits
600 * Returns 1 if a partitionable standard,
601 * -1 if non-partitonable,
602 * 0 if not a standard name.
604 char *d
= strrchr(dev
, '/');
609 if (strncmp(d
, "/d",2) == 0)
610 d
+= 2, type
= 1; /* /dev/md/dN{pM} */
611 else if (strncmp(d
, "/md_d", 5) == 0)
612 d
+= 5, type
= 1; /* /dev/md_dN{pM} */
613 else if (strncmp(d
, "/md", 3) == 0)
614 d
+= 3, type
= -1; /* /dev/mdN */
615 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3) == 0)
616 d
+= 1, type
= -1; /* /dev/md/N */
626 if (nump
) *nump
= num
;
631 unsigned long calc_csum(void *super
, int bytes
)
633 unsigned long long newcsum
= 0;
636 unsigned int *superc
= (unsigned int*) super
;
638 for(i
= 0; i
< bytes
/4; i
++)
639 newcsum
+= superc
[i
];
640 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
642 /* The in-kernel checksum calculation is always 16bit on
643 * the alpha, though it is 32 bit on i386...
644 * I wonder what it is elsewhere... (it uses an API in
645 * a way that it shouldn't).
647 csum
= (csum
& 0xffff) + (csum
>> 16);
648 csum
= (csum
& 0xffff) + (csum
>> 16);
654 char *human_size(long long bytes
)
658 /* We convert bytes to either centi-M{ega,ibi}bytes or
659 * centi-G{igi,ibi}bytes, with appropriate rounding,
660 * and then print 1/100th of those as a decimal.
661 * We allow upto 2048Megabytes before converting to
662 * gigabytes, as that shows more precision and isn't
663 * too large a number.
664 * Terabytes are not yet handled.
667 if (bytes
< 5000*1024)
669 else if (bytes
< 2*1024LL*1024LL*1024LL) {
670 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
671 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
672 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
673 cMiB
/100 , cMiB
% 100,
676 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
677 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
678 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
679 cGiB
/100 , cGiB
% 100,
685 char *human_size_brief(long long bytes
)
689 /* We convert bytes to either centi-M{ega,ibi}bytes or
690 * centi-G{igi,ibi}bytes, with appropriate rounding,
691 * and then print 1/100th of those as a decimal.
692 * We allow upto 2048Megabytes before converting to
693 * gigabytes, as that shows more precision and isn't
694 * too large a number.
695 * Terabytes are not yet handled.
698 if (bytes
< 5000*1024)
700 else if (bytes
< 2*1024LL*1024LL*1024LL) {
701 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
702 snprintf(buf
, sizeof(buf
), " (%ld.%02ldMiB)",
703 cMiB
/100 , cMiB
% 100);
705 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
706 snprintf(buf
, sizeof(buf
), " (%ld.%02ldGiB)",
707 cGiB
/100 , cGiB
% 100);
713 void print_r10_layout(int layout
)
715 int near
= layout
& 255;
716 int far
= (layout
>> 8) & 255;
717 int offset
= (layout
&0x10000);
721 printf("%s near=%d", sep
, near
);
725 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
727 printf("NO REDUNDANCY");
731 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
732 int chunksize
, unsigned long long devsize
)
736 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
737 return get_data_disks(level
, layout
, raid_disks
) * devsize
;
740 int get_data_disks(int level
, int layout
, int raid_disks
)
744 case 0: data_disks
= raid_disks
;
746 case 1: data_disks
= 1;
749 case 5: data_disks
= raid_disks
- 1;
751 case 6: data_disks
= raid_disks
- 2;
753 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
760 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
761 char *get_md_name(int dev
)
763 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
764 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
765 static char devname
[50];
771 int mdp
= get_mdp_major();
772 if (mdp
< 0) return NULL
;
773 rdev
= makedev(mdp
, (-1-dev
)<<6);
774 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
775 if (stat(devname
, &stb
) == 0
776 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
777 && (stb
.st_rdev
== rdev
))
780 rdev
= makedev(MD_MAJOR
, dev
);
781 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
782 if (stat(devname
, &stb
) == 0
783 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
784 && (stb
.st_rdev
== rdev
))
787 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
788 if (stat(devname
, &stb
) == 0
789 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
790 && (stb
.st_rdev
== rdev
))
793 dn
= map_dev(major(rdev
), minor(rdev
), 0);
796 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
797 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
801 if (stat(devname
, &stb
) == 0
802 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
803 && (stb
.st_rdev
== rdev
))
809 void put_md_name(char *name
)
811 if (strncmp(name
, "/dev/.tmp.md", 12) == 0)
815 int find_free_devnum(int use_partitions
)
818 for (devnum
= 127; devnum
!= 128;
819 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
824 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
825 if (mddev_busy(_devnum
))
827 sprintf(nbuf
, "%s%d", use_partitions
?"mdp":"md", devnum
);
828 if (!conf_name_is_free(nbuf
))
830 /* make sure it is new to /dev too, at least as a
832 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
833 if (dn
&& ! is_standard(dn
, NULL
))
839 return use_partitions
? (-1-devnum
) : devnum
;
841 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
843 int dev_open(char *dev
, int flags
)
845 /* like 'open', but if 'dev' matches %d:%d, create a temp
846 * block device and open that
857 major
= strtoul(dev
, &e
, 0);
858 if (e
> dev
&& *e
== ':' && e
[1] &&
859 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
861 char *path
= map_dev(major
, minor
, 0);
863 fd
= open(path
, flags
);
865 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
866 (int)getpid(), major
, minor
);
867 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
868 fd
= open(devname
, flags
);
873 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
874 (int)getpid(), major
, minor
);
875 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
876 fd
= open(devname
, flags
);
881 fd
= open(dev
, flags
);
885 int open_dev_flags(int devnum
, int flags
)
889 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
890 return dev_open(buf
, flags
);
893 int open_dev(int devnum
)
895 return open_dev_flags(devnum
, O_RDONLY
);
898 int open_dev_excl(int devnum
)
904 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
905 for (i
= 0 ; i
< 25 ; i
++) {
906 int fd
= dev_open(buf
, flags
|O_EXCL
);
909 if (errno
== EACCES
&& flags
== O_RDWR
) {
920 int same_dev(char *one
, char *two
)
922 struct stat st1
, st2
;
923 if (stat(one
, &st1
) != 0)
925 if (stat(two
, &st2
) != 0)
927 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
929 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
931 return st1
.st_rdev
== st2
.st_rdev
;
934 void wait_for(char *dev
, int fd
)
937 struct stat stb_want
;
939 if (fstat(fd
, &stb_want
) != 0 ||
940 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
943 for (i
= 0 ; i
< 25 ; i
++) {
945 if (stat(dev
, &stb
) == 0 &&
946 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
947 (stb
.st_rdev
== stb_want
.st_rdev
))
952 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
955 struct superswitch
*superlist
[] =
958 &super_ddf
, &super_imsm
,
962 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
964 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
966 mdu_array_info_t array
;
969 struct supertype
*st
= NULL
;
974 char *subarray
= NULL
;
975 int container
= NoMdDev
;
977 sra
= sysfs_read(fd
, 0, GET_VERSION
);
980 vers
= sra
->array
.major_version
;
981 minor
= sra
->array
.minor_version
;
982 verstr
= sra
->text_version
;
984 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
985 array
.major_version
= array
.minor_version
= 0;
986 vers
= array
.major_version
;
987 minor
= array
.minor_version
;
992 sprintf(version
, "%d.%d", vers
, minor
);
995 if (minor
== -2 && is_subarray(verstr
)) {
996 char *dev
= verstr
+1;
998 subarray
= strchr(dev
, '/');
1001 subarray
= xstrdup(subarray
);
1003 container
= devname2devnum(dev
);
1006 sra
= sysfs_read(-1, container
, GET_VERSION
);
1007 if (sra
&& sra
->text_version
[0])
1008 verstr
= sra
->text_version
;
1010 verstr
= "-no-metadata-";
1013 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1014 st
= superlist
[i
]->match_metadata_desc(verstr
);
1021 *subarrayp
= subarray
;
1022 st
->container_dev
= container
;
1023 st
->devnum
= fd2devnum(fd
);
1029 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1031 int dev_size_from_id(dev_t id
, unsigned long long *size
)
1036 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1037 fd
= dev_open(buf
, O_RDONLY
);
1040 if (get_dev_size(fd
, NULL
, size
)) {
1048 struct supertype
*dup_super(struct supertype
*orig
)
1050 struct supertype
*st
;
1054 st
= xcalloc(1, sizeof(*st
));
1056 st
->max_devs
= orig
->max_devs
;
1057 st
->minor_version
= orig
->minor_version
;
1063 struct supertype
*guess_super_type(int fd
, enum guess_types guess_type
)
1065 /* try each load_super to find the best match,
1066 * and return the best superswitch
1068 struct superswitch
*ss
;
1069 struct supertype
*st
;
1070 time_t besttime
= 0;
1074 st
= xcalloc(1, sizeof(*st
));
1075 st
->container_dev
= NoMdDev
;
1077 for (i
= 0 ; superlist
[i
]; i
++) {
1080 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1082 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1084 memset(st
, 0, sizeof(*st
));
1085 st
->ignore_hw_compat
= 1;
1086 rv
= ss
->load_super(st
, fd
, NULL
);
1089 st
->ss
->getinfo_super(st
, &info
, NULL
);
1090 if (bestsuper
== -1 ||
1091 besttime
< info
.array
.ctime
) {
1093 besttime
= info
.array
.ctime
;
1098 if (bestsuper
!= -1) {
1100 memset(st
, 0, sizeof(*st
));
1101 st
->ignore_hw_compat
= 1;
1102 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1104 superlist
[bestsuper
]->free_super(st
);
1105 st
->ignore_hw_compat
= 0;
1113 /* Return size of device in bytes */
1114 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1116 unsigned long long ldsize
;
1119 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1120 ldsize
= (unsigned long long)st
.st_size
;
1123 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1126 unsigned long dsize
;
1127 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1132 pr_err("Cannot get size of %s: %s\b",
1133 dname
, strerror(errno
));
1141 /* Return true if this can only be a container, not a member device.
1142 * i.e. is and md device and size is zero
1144 int must_be_container(int fd
)
1146 unsigned long long size
;
1147 if (md_get_version(fd
) < 0)
1149 if (get_dev_size(fd
, NULL
, &size
) == 0)
1156 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1157 * Returns: 1 if successful
1158 * -1 for unknown partition type
1159 * 0 for other errors
1161 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1164 unsigned char empty_gpt_entry
[16]= {0};
1165 struct GPT_part_entry
*part
;
1167 unsigned long long curr_part_end
;
1168 unsigned all_partitions
, entry_size
;
1173 BUILD_BUG_ON(sizeof(gpt
) != 512);
1174 /* skip protective MBR */
1175 lseek(fd
, 512, SEEK_SET
);
1176 /* read GPT header */
1177 if (read(fd
, &gpt
, 512) != 512)
1180 /* get the number of partition entries and the entry size */
1181 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1182 entry_size
= __le32_to_cpu(gpt
.part_size
);
1184 /* Check GPT signature*/
1185 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1189 if (all_partitions
> 1024 ||
1190 entry_size
> sizeof(buf
))
1193 part
= (struct GPT_part_entry
*)buf
;
1195 for (part_nr
= 0; part_nr
< all_partitions
; part_nr
++) {
1196 /* read partition entry */
1197 if (read(fd
, buf
, entry_size
) != (ssize_t
)entry_size
)
1200 /* is this valid partition? */
1201 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1202 /* check the last lba for the current partition */
1203 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1204 if (curr_part_end
> *endofpart
)
1205 *endofpart
= curr_part_end
;
1212 /* Sets endofpart parameter to the last block used by the last partition on the device.
1213 * Returns: 1 if successful
1214 * -1 for unknown partition type
1215 * 0 for other errors
1217 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1219 struct MBR boot_sect
;
1220 struct MBR_part_record
*part
;
1221 unsigned long long curr_part_end
;
1227 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1230 if (read(fd
, &boot_sect
, 512) != 512)
1233 /* check MBP signature */
1234 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1236 /* found the correct signature */
1237 part
= boot_sect
.parts
;
1239 for (part_nr
= 0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1240 /* check for GPT type */
1241 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1242 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1245 /* check the last used lba for the current partition */
1246 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1247 __le32_to_cpu(part
->blocks_num
);
1248 if (curr_part_end
> *endofpart
)
1249 *endofpart
= curr_part_end
;
1254 /* Unknown partition table */
1261 int check_partitions(int fd
, char *dname
, unsigned long long freesize
,
1262 unsigned long long size
)
1265 * Check where the last partition ends
1267 unsigned long long endofpart
;
1270 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1271 /* There appears to be a partition table here */
1272 if (freesize
== 0) {
1273 /* partitions will not be visible in new device */
1274 pr_err("partition table exists on %s but will be lost or\n"
1275 " meaningless after creating array\n",
1278 } else if (endofpart
> freesize
) {
1279 /* last partition overlaps metadata */
1280 pr_err("metadata will over-write last partition on %s.\n",
1283 } else if (size
&& endofpart
> size
) {
1284 /* partitions will be truncated in new device */
1285 pr_err("array size is too small to cover all partitions on %s.\n",
1293 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1297 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1298 for (d
= 0 ; d
< MAX_DISKS
; d
++) {
1299 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0 &&
1300 (disk
->major
|| disk
->minor
))
1305 int open_container(int fd
)
1307 /* 'fd' is a block device. Find out if it is in use
1308 * by a container, and return an open fd on that container.
1319 if (fstat(fd
, &st
) != 0)
1321 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1322 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1323 e
= path
+ strlen(path
);
1325 dir
= opendir(path
);
1328 while ((de
= readdir(dir
))) {
1331 if (de
->d_name
[0] == '.')
1333 sprintf(e
, "/%s/dev", de
->d_name
);
1334 dfd
= open(path
, O_RDONLY
);
1337 n
= read(dfd
, buf
, sizeof(buf
));
1339 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1342 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1344 sprintf(buf
, "%d:%d", major
, minor
);
1345 dfd
= dev_open(buf
, O_RDONLY
);
1355 struct superswitch
*version_to_superswitch(char *vers
)
1359 for (i
= 0; superlist
[i
]; i
++) {
1360 struct superswitch
*ss
= superlist
[i
];
1362 if (strcmp(vers
, ss
->name
) == 0)
1369 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1371 if (mdstat
->metadata_version
== NULL
||
1372 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1373 !is_subarray(mdstat
->metadata_version
+9) ||
1374 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
1375 mdstat
->metadata_version
[10+strlen(container
)] != '/')
1381 int is_subarray_active(char *subarray
, char *container
)
1383 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1384 struct mdstat_ent
*ent
;
1386 for (ent
= mdstat
; ent
; ent
= ent
->next
)
1387 if (is_container_member(ent
, container
))
1388 if (strcmp(to_subarray(ent
, container
), subarray
) == 0)
1391 free_mdstat(mdstat
);
1396 /* open_subarray - opens a subarray in a container
1397 * @dev: container device name
1398 * @st: empty supertype
1399 * @quiet: block reporting errors flag
1401 * On success returns an fd to a container and fills in *st
1403 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1406 struct mdinfo
*info
;
1409 fd
= open(dev
, O_RDWR
|O_EXCL
);
1412 pr_err("Couldn't open %s, aborting\n",
1417 st
->devnum
= fd2devnum(fd
);
1418 if (st
->devnum
== NoMdDev
) {
1420 pr_err("Failed to determine device number for %s\n",
1425 mdi
= sysfs_read(fd
, st
->devnum
, GET_VERSION
|GET_LEVEL
);
1428 pr_err("Failed to read sysfs for %s\n",
1433 if (mdi
->array
.level
!= UnSet
) {
1435 pr_err("%s is not a container\n", dev
);
1439 st
->ss
= version_to_superswitch(mdi
->text_version
);
1442 pr_err("Operation not supported for %s metadata\n",
1447 st
->devname
= devnum2devname(st
->devnum
);
1450 pr_err("Failed to allocate device name\n");
1454 if (!st
->ss
->load_container
) {
1456 pr_err("%s is not a container\n", dev
);
1460 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1462 pr_err("Failed to load metadata for %s\n",
1467 info
= st
->ss
->container_content(st
, subarray
);
1470 pr_err("Failed to find subarray-%s in %s\n",
1480 st
->ss
->free_super(st
);
1496 int add_disk(int mdfd
, struct supertype
*st
,
1497 struct mdinfo
*sra
, struct mdinfo
*info
)
1499 /* Add a device to an array, in one of 2 ways. */
1502 if (st
->ss
->external
) {
1503 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1504 info
->recovery_start
= MaxSector
;
1506 info
->recovery_start
= 0;
1507 rv
= sysfs_add_disk(sra
, info
, 0);
1510 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1514 sd2
= xmalloc(sizeof(*sd2
));
1516 sd2
->next
= sra
->devs
;
1522 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1526 int remove_disk(int mdfd
, struct supertype
*st
,
1527 struct mdinfo
*sra
, struct mdinfo
*info
)
1530 /* Remove the disk given by 'info' from the array */
1532 if (st
->ss
->external
)
1533 rv
= sysfs_set_str(sra
, info
, "slot", "none");
1536 rv
= ioctl(mdfd
, HOT_REMOVE_DISK
, makedev(info
->disk
.major
,
1541 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1543 /* Initialise kernel's knowledge of array.
1544 * This varies between externally managed arrays
1547 int vers
= md_get_version(mdfd
);
1551 if (st
->ss
->external
)
1552 rv
= sysfs_set_array(info
, vers
);
1555 if ((vers
% 100) >= 1) { /* can use different versions */
1556 mdu_array_info_t inf
;
1557 memset(&inf
, 0, sizeof(inf
));
1558 inf
.major_version
= info
->array
.major_version
;
1559 inf
.minor_version
= info
->array
.minor_version
;
1560 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1562 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1566 unsigned long long min_recovery_start(struct mdinfo
*array
)
1568 /* find the minimum recovery_start in an array for metadata
1569 * formats that only record per-array recovery progress instead
1572 unsigned long long recovery_start
= MaxSector
;
1575 for (d
= array
->devs
; d
; d
= d
->next
)
1576 recovery_start
= min(recovery_start
, d
->recovery_start
);
1578 return recovery_start
;
1581 int mdmon_pid(int devnum
)
1587 char *devname
= devnum2devname(devnum
);
1589 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devname
);
1592 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1596 n
= read(fd
, pid
, 9);
1603 int mdmon_running(int devnum
)
1605 int pid
= mdmon_pid(devnum
);
1608 if (kill(pid
, 0) == 0)
1613 int start_mdmon(int devnum
)
1627 if (check_env("MDADM_NO_MDMON"))
1630 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
)-1);
1634 sl
= strrchr(pathbuf
, '/');
1639 strcpy(sl
, "mdmon");
1645 /* FIXME yuk. CLOSE_EXEC?? */
1647 for (i
= 3; skipped
< 20; i
++)
1653 for (i
= 0; paths
[i
]; i
++)
1656 execl(paths
[i
], "mdmon", "--offroot",
1657 devnum2devname(devnum
),
1660 execl(paths
[i
], "mdmon",
1661 devnum2devname(devnum
),
1666 case -1: pr_err("cannot run mdmon. "
1667 "Array remains readonly\n");
1669 default: /* parent - good */
1670 pid
= wait(&status
);
1671 if (pid
< 0 || status
!= 0)
1677 int check_env(char *name
)
1679 char *val
= getenv(name
);
1681 if (val
&& atoi(val
) == 1)
1687 __u32
random32(void)
1690 int rfd
= open("/dev/urandom", O_RDONLY
);
1691 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1699 int flush_metadata_updates(struct supertype
*st
)
1703 st
->update_tail
= NULL
;
1707 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1711 while (st
->updates
) {
1712 struct metadata_update
*mu
= st
->updates
;
1713 st
->updates
= mu
->next
;
1715 send_message(sfd
, mu
, 0);
1723 st
->update_tail
= NULL
;
1727 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1730 struct metadata_update
*mu
= xmalloc(sizeof(*mu
));
1735 mu
->space_list
= NULL
;
1737 *st
->update_tail
= mu
;
1738 st
->update_tail
= &mu
->next
;
1740 #endif /* MDASSEMBLE */
1743 /* tinyc doesn't optimize this check in ioctl.h out ... */
1744 unsigned int __invalid_size_argument_for_IOC
= 0;
1747 int experimental(void)
1749 if (check_env("MDADM_EXPERIMENTAL"))
1752 pr_err("To use this feature MDADM_EXPERIMENTAL"
1753 " environment variable has to be defined.\n");
1758 /* Pick all spares matching given criteria from a container
1759 * if min_size == 0 do not check size
1760 * if domlist == NULL do not check domains
1761 * if spare_group given add it to domains of each spare
1762 * metadata allows to test domains using metadata of destination array */
1763 struct mdinfo
*container_choose_spares(struct supertype
*st
,
1764 unsigned long long min_size
,
1765 struct domainlist
*domlist
,
1767 const char *metadata
, int get_one
)
1769 struct mdinfo
*d
, **dp
, *disks
= NULL
;
1771 /* get list of all disks in container */
1772 if (st
->ss
->getinfo_super_disks
)
1773 disks
= st
->ss
->getinfo_super_disks(st
);
1777 /* find spare devices on the list */
1779 disks
->array
.spare_disks
= 0;
1783 if (d
->disk
.state
== 0) {
1784 /* check if size is acceptable */
1785 unsigned long long dev_size
;
1786 dev_t dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
1789 (dev_size_from_id(dev
, &dev_size
) &&
1790 dev_size
>= min_size
))
1792 /* check if domain matches */
1793 if (found
&& domlist
) {
1794 struct dev_policy
*pol
= devnum_policy(dev
);
1796 pol_add(&pol
, pol_domain
,
1798 if (domain_test(domlist
, pol
, metadata
) != 1)
1800 dev_policy_free(pol
);
1805 disks
->array
.spare_disks
++;