2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
32 #include <sys/socket.h>
33 #include <sys/utsname.h>
41 * following taken from linux/blkpg.h because they aren't
42 * anywhere else and it isn't safe to #include linux/ * stuff.
45 #define BLKPG _IO(0x12,105)
47 /* The argument structure */
48 struct blkpg_ioctl_arg
{
55 /* The subfunctions (for the op field) */
56 #define BLKPG_ADD_PARTITION 1
57 #define BLKPG_DEL_PARTITION 2
59 /* Sizes of name fields. Unused at present. */
60 #define BLKPG_DEVNAMELTH 64
61 #define BLKPG_VOLNAMELTH 64
63 /* The data structure for ADD_PARTITION and DEL_PARTITION */
64 struct blkpg_partition
{
65 long long start
; /* starting offset in bytes */
66 long long length
; /* length in bytes */
67 int pno
; /* partition number */
68 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
69 to be used in kernel messages */
70 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
74 * Parse a 128 bit uuid in 4 integers
75 * format is 32 hexx nibbles with options :.<space> separator
76 * If not exactly 32 hex digits are found, return 0
79 int parse_uuid(char *str
, int uuid
[4])
81 int hit
= 0; /* number of Hex digIT */
84 for (i
=0; i
<4; i
++) uuid
[i
]=0;
90 else if (c
>='a' && c
<= 'f')
92 else if (c
>='A' && c
<= 'F')
94 else if (strchr(":. -", c
))
111 * Get the md version number.
112 * We use the RAID_VERSION ioctl if it is supported
113 * If not, but we have a block device with major '9', we assume
116 * Return version number as 24 but number - assume version parts
120 int md_get_version(int fd
)
125 if (fstat(fd
, &stb
)<0)
127 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
130 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
131 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
134 if (major(stb
.st_rdev
) == MD_MAJOR
)
139 int get_linux_version()
148 a
= strtoul(cp
, &cp
, 10);
149 if (*cp
!= '.') return -1;
150 b
= strtoul(cp
+1, &cp
, 10);
151 if (*cp
!= '.') return -1;
152 c
= strtoul(cp
+1, NULL
, 10);
154 return (a
*1000000)+(b
*1000)+c
;
157 void remove_partitions(int fd
)
159 /* remove partitions from this block devices.
160 * This is used for components added to an array
162 #ifdef BLKPG_DEL_PARTITION
163 struct blkpg_ioctl_arg a
;
164 struct blkpg_partition p
;
166 a
.op
= BLKPG_DEL_PARTITION
;
168 a
.datalen
= sizeof(p
);
170 memset(a
.data
, 0, a
.datalen
);
171 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
172 ioctl(fd
, BLKPG
, &a
);
176 int enough(int level
, int raid_disks
, int layout
, int clean
,
177 char *avail
, int avail_disks
)
182 /* This is the tricky one - we need to check
183 * which actual disks are present.
185 copies
= (layout
&255)* ((layout
>>8) & 255);
188 /* there must be one of the 'copies' form 'first' */
194 first
= (first
+1) % raid_disks
;
199 } while (first
!= 0);
203 return avail_disks
>= 1;
206 return avail_disks
== raid_disks
;
208 return avail_disks
>= 1;
212 return avail_disks
>= raid_disks
-1;
214 return avail_disks
>= raid_disks
;
217 return avail_disks
>= raid_disks
-2;
219 return avail_disks
>= raid_disks
;
225 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
226 int same_uuid(int a
[4], int b
[4], int swapuuid
)
228 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
229 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
233 /* parse uuids are hostendian.
234 * uuid's from some superblocks are big-ending
235 * if there is a difference, we need to swap..
237 unsigned char *ac
= (unsigned char *)a
;
238 unsigned char *bc
= (unsigned char *)b
;
240 for (i
=0; i
<16; i
+= 4) {
241 if (ac
[i
+0] != bc
[i
+3] ||
242 ac
[i
+1] != bc
[i
+2] ||
243 ac
[i
+2] != bc
[i
+1] ||
257 void copy_uuid(void *a
, int b
[4], int swapuuid
)
260 /* parse uuids are hostendian.
261 * uuid's from some superblocks are big-ending
262 * if there is a difference, we need to swap..
264 unsigned char *ac
= (unsigned char *)a
;
265 unsigned char *bc
= (unsigned char *)b
;
267 for (i
=0; i
<16; i
+= 4) {
277 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
285 copy_uuid(uuid
, info
->uuid
, st
->ss
->swapuuid
);
286 for (i
= 0; i
< 4; i
++) {
290 for (j
= 3; j
>= 0; j
--) {
291 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
299 int check_ext2(int fd
, char *name
)
302 * Check for an ext2fs file system.
303 * Superblock is always 1K at 1K offset
305 * s_magic is le16 at 56 == 0xEF53
306 * report mtime - le32 at 44
308 * logblksize - le32 at 24
310 unsigned char sb
[1024];
313 if (lseek(fd
, 1024,0)!= 1024)
315 if (read(fd
, sb
, 1024)!= 1024)
317 if (sb
[56] != 0x53 || sb
[57] != 0xef)
320 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
321 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
322 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
323 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
325 fprintf(stderr
," size=%dK mtime=%s",
326 size
*(1<<bsize
), ctime(&mtime
));
330 int check_reiser(int fd
, char *name
)
333 * superblock is at 64K
335 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
338 unsigned char sb
[1024];
340 if (lseek(fd
, 64*1024, 0) != 64*1024)
342 if (read(fd
, sb
, 1024) != 1024)
344 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
345 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
347 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
348 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
349 fprintf(stderr
, " size = %luK\n", size
*4);
354 int check_raid(int fd
, char *name
)
359 struct supertype
*st
= guess_super(fd
);
362 st
->ss
->load_super(st
, fd
, name
);
363 /* Looks like a raid array .. */
364 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
366 st
->ss
->getinfo_super(st
, &info
);
367 st
->ss
->free_super(st
);
368 crtime
= info
.array
.ctime
;
369 level
= map_num(pers
, info
.array
.level
);
370 if (!level
) level
= "-unknown-";
371 fprintf(stderr
, " level=%s devices=%d ctime=%s",
372 level
, info
.array
.raid_disks
, ctime(&crtime
));
380 for (i
=0; i
<5; i
++) {
382 fprintf(stderr
, "%s%s", mesg
, add
);
384 if (fgets(buf
, 100, stdin
)==NULL
)
386 if (buf
[0]=='y' || buf
[0]=='Y')
388 if (buf
[0]=='n' || buf
[0]=='N')
392 fprintf(stderr
, Name
": assuming 'no'\n");
395 #endif /* MDASSEMBLE */
397 char *map_num(mapping_t
*map
, int num
)
407 int map_name(mapping_t
*map
, char *name
)
410 if (strcmp(map
->name
, name
)==0)
418 int is_standard(char *dev
, int *nump
)
420 /* tests if dev is a "standard" md dev name.
421 * i.e if the last component is "/dNN" or "/mdNN",
422 * where NN is a string of digits
423 * Returns 1 if a partitionable standard,
424 * -1 if non-partitonable,
425 * 0 if not a standard name.
427 char *d
= strrchr(dev
, '/');
432 if (strncmp(d
, "/d",2)==0)
433 d
+= 2, type
=1; /* /dev/md/dN{pM} */
434 else if (strncmp(d
, "/md_d", 5)==0)
435 d
+= 5, type
=1; /* /dev/md_dNpM */
436 else if (strncmp(d
, "/md", 3)==0)
437 d
+= 3, type
=-1; /* /dev/mdN */
438 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
439 d
+= 1, type
=-1; /* /dev/md/N */
449 if (nump
) *nump
= num
;
456 * convert a major/minor pair for a block device into a name in /dev, if possible.
457 * On the first call, walk /dev collecting name.
458 * Put them in a simple linked listfor now.
465 int devlist_ready
= 0;
467 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
470 if (S_ISLNK(stb
->st_mode
)) {
475 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
476 char *n
= strdup(name
);
477 struct devmap
*dm
= malloc(sizeof(*dm
));
478 if (strncmp(n
, "/dev/./", 7)==0)
481 dm
->major
= major(stb
->st_rdev
);
482 dm
->minor
= minor(stb
->st_rdev
);
493 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
495 return add_dev(name
, stb
, flag
, NULL
);
497 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
499 return ftw(path
, add_dev_1
, nopenfd
);
502 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
506 #endif /* HAVE_FTW */
507 #endif /* HAVE_NFTW */
510 * Find a block device with the right major/minor number.
511 * If we find multiple names, choose the shortest.
512 * If we find a non-standard name, it is probably there
513 * deliberately so prefer it over a standard name.
514 * This applies only to names for MD devices.
516 char *map_dev(int major
, int minor
, int create
)
519 char *std
= NULL
, *nonstd
=NULL
;
522 if (major
== 0 && minor
== 0)
526 if (!devlist_ready
) {
530 struct devmap
*d
= devlist
;
535 if (lstat(dev
, &stb
)==0 &&
536 S_ISLNK(stb
.st_mode
))
538 nftw(dev
, add_dev
, 10, FTW_PHYS
);
543 for (p
=devlist
; p
; p
=p
->next
)
544 if (p
->major
== major
&&
546 if (is_standard(p
->name
, NULL
)) {
548 strlen(p
->name
) < strlen(std
))
551 if (nonstd
== NULL
||
552 strlen(p
->name
) < strlen(nonstd
))
556 if (!std
&& !nonstd
&& !did_check
) {
560 if (create
&& !std
&& !nonstd
) {
562 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
566 return nonstd
? nonstd
: std
;
569 unsigned long calc_csum(void *super
, int bytes
)
571 unsigned long long newcsum
= 0;
574 unsigned int *superc
= (unsigned int*) super
;
576 for(i
=0; i
<bytes
/4; i
++)
578 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
580 /* The in-kernel checksum calculation is always 16bit on
581 * the alpha, though it is 32 bit on i386...
582 * I wonder what it is elsewhere... (it uses and API in
583 * a way that it shouldn't).
585 csum
= (csum
& 0xffff) + (csum
>> 16);
586 csum
= (csum
& 0xffff) + (csum
>> 16);
592 char *human_size(long long bytes
)
596 /* We convert bytes to either centi-M{ega,ibi}bytes or
597 * centi-G{igi,ibi}bytes, with appropriate rounding,
598 * and then print 1/100th of those as a decimal.
599 * We allow upto 2048Megabytes before converting to
600 * gigabytes, as that shows more precision and isn't
601 * too large a number.
602 * Terrabytes are not yet handled.
605 if (bytes
< 5000*1024)
607 else if (bytes
< 2*1024LL*1024LL*1024LL) {
608 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
609 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
610 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
611 cMiB
/100 , cMiB
% 100,
614 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
615 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
616 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
617 cGiB
/100 , cGiB
% 100,
623 char *human_size_brief(long long bytes
)
627 if (bytes
< 5000*1024)
628 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
629 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
631 else if (bytes
< 2*1024LL*1024LL*1024LL)
632 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
634 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
637 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
639 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
645 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
646 int chunksize
, unsigned long long devsize
)
650 case 0: data_disks
= raid_disks
; break;
651 case 1: data_disks
= 1; break;
653 case 5: data_disks
= raid_disks
- 1; break;
654 case 6: data_disks
= raid_disks
- 2; break;
655 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
658 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
659 return data_disks
* devsize
;
662 int get_mdp_major(void)
664 static int mdp_major
= -1;
668 int have_devices
= 0;
673 fl
= fopen("/proc/devices", "r");
676 while ((w
= conf_word(fl
, 1))) {
677 if (have_block
&& strcmp(w
, "devices:")==0)
679 have_block
= (strcmp(w
, "Block")==0);
682 if (have_devices
&& strcmp(w
, "mdp")==0)
683 mdp_major
= last_num
;
690 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
691 char *get_md_name(int dev
)
693 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
694 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
695 static char devname
[50];
701 int mdp
= get_mdp_major();
702 if (mdp
< 0) return NULL
;
703 rdev
= makedev(mdp
, (-1-dev
)<<6);
704 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
705 if (stat(devname
, &stb
) == 0
706 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
707 && (stb
.st_rdev
== rdev
))
710 rdev
= makedev(MD_MAJOR
, dev
);
711 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
712 if (stat(devname
, &stb
) == 0
713 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
714 && (stb
.st_rdev
== rdev
))
717 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
718 if (stat(devname
, &stb
) == 0
719 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
720 && (stb
.st_rdev
== rdev
))
723 dn
= map_dev(major(rdev
), minor(rdev
), 0);
726 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
727 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
731 if (stat(devname
, &stb
) == 0
732 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
733 && (stb
.st_rdev
== rdev
))
739 void put_md_name(char *name
)
741 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
745 int find_free_devnum(int use_partitions
)
748 for (devnum
= 127; devnum
!= 128;
749 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
753 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
754 if (mddev_busy(_devnum
))
756 /* make sure it is new to /dev too, at least as a
758 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
759 if (dn
&& ! is_standard(dn
, NULL
))
765 return use_partitions
? (-1-devnum
) : devnum
;
767 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
769 int dev_open(char *dev
, int flags
)
771 /* like 'open', but if 'dev' matches %d:%d, create a temp
772 * block device and open that
782 major
= strtoul(dev
, &e
, 0);
783 if (e
> dev
&& *e
== ':' && e
[1] &&
784 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
786 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
787 (int)getpid(), major
, minor
);
788 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
789 fd
= open(devname
, flags
|O_DIRECT
);
793 fd
= open(dev
, flags
|O_DIRECT
);
797 int open_dev_excl(int devnum
)
802 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
803 for (i
=0 ; i
<25 ; i
++) {
804 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
814 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
816 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
818 struct supertype
*super_by_fd(int fd
)
820 mdu_array_info_t array
;
823 struct supertype
*st
= NULL
;
828 char *subarray
= NULL
;
830 sra
= sysfs_read(fd
, 0, GET_VERSION
);
833 vers
= sra
->array
.major_version
;
834 minor
= sra
->array
.minor_version
;
835 verstr
= sra
->text_version
;
837 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
838 array
.major_version
= array
.minor_version
= 0;
839 vers
= array
.major_version
;
840 minor
= array
.minor_version
;
845 sprintf(version
, "%d.%d", vers
, minor
);
848 if (minor
== -2 && is_subarray(verstr
)) {
849 char *dev
= verstr
+1;
850 subarray
= strchr(dev
, '/');
854 devnum
= devname2devnum(dev
);
855 subarray
= strdup(subarray
);
858 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
859 verstr
= sra
->text_version
? : "-no-metadata-";
862 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
863 st
= superlist
[i
]->match_metadata_desc(verstr
);
870 strncpy(st
->subarray
, subarray
, 32);
871 st
->subarray
[31] = 0;
878 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
881 struct supertype
*dup_super(struct supertype
*orig
)
883 struct supertype
*st
;
887 st
= malloc(sizeof(*st
));
890 memset(st
, 0, sizeof(*st
));
892 st
->max_devs
= orig
->max_devs
;
893 st
->minor_version
= orig
->minor_version
;
894 strcpy(st
->subarray
, orig
->subarray
);
900 struct supertype
*guess_super(int fd
)
902 /* try each load_super to find the best match,
903 * and return the best superswitch
905 struct superswitch
*ss
;
906 struct supertype
*st
;
907 unsigned long besttime
= 0;
911 st
= malloc(sizeof(*st
));
912 for (i
=0 ; superlist
[i
]; i
++) {
915 memset(st
, 0, sizeof(*st
));
916 rv
= ss
->load_super(st
, fd
, NULL
);
919 st
->ss
->getinfo_super(st
, &info
);
920 if (bestsuper
== -1 ||
921 besttime
< info
.array
.ctime
) {
923 besttime
= info
.array
.ctime
;
928 if (bestsuper
!= -1) {
930 memset(st
, 0, sizeof(*st
));
931 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
933 superlist
[bestsuper
]->free_super(st
);
941 /* Return size of device in bytes */
942 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
944 unsigned long long ldsize
;
947 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
948 ldsize
= (unsigned long long)st
.st_size
;
951 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
955 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
960 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
961 dname
, strerror(errno
));
969 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
972 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
973 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
974 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
978 int open_container(int fd
)
980 /* 'fd' is a block device. Find out if it is in use
981 * by a container, and return an open fd on that container.
992 if (fstat(fd
, &st
) != 0)
994 sprintf(path
, "/sys/dev/block/%d:%d/holders",
995 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
996 e
= path
+ strlen(path
);
1001 while ((de
= readdir(dir
))) {
1004 if (de
->d_name
[0] == '.')
1006 sprintf(e
, "/%s/dev", de
->d_name
);
1007 dfd
= open(path
, O_RDONLY
);
1010 n
= read(dfd
, buf
, sizeof(buf
));
1012 if (n
<= 0 || n
>= sizeof(buf
))
1015 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1017 sprintf(buf
, "%d:%d", major
, minor
);
1018 dfd
= dev_open(buf
, O_RDONLY
);
1028 int add_disk(int mdfd
, struct supertype
*st
,
1029 struct mdinfo
*sra
, struct mdinfo
*info
)
1031 /* Add a device to an array, in one of 2 ways. */
1034 if (st
->ss
->external
) {
1035 rv
= sysfs_add_disk(sra
, info
);
1038 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1042 sd2
= malloc(sizeof(*sd2
));
1044 sd2
->next
= sra
->devs
;
1050 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1054 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1056 /* Initialise kernel's knowledge of array.
1057 * This varies between externally managed arrays
1060 int vers
= md_get_version(mdfd
);
1064 if (st
->ss
->external
)
1065 rv
= sysfs_set_array(info
, vers
);
1068 if ((vers
% 100) >= 1) { /* can use different versions */
1069 mdu_array_info_t inf
;
1070 memset(&inf
, 0, sizeof(inf
));
1071 inf
.major_version
= info
->array
.major_version
;
1072 inf
.minor_version
= info
->array
.minor_version
;
1073 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1075 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1079 char *devnum2devname(int num
)
1083 sprintf(name
, "md%d", num
);
1085 sprintf(name
, "md_d%d", -1-num
);
1086 return strdup(name
);
1089 int devname2devnum(char *name
)
1093 if (strncmp(name
, "md_d", 4)==0)
1094 num
= -1-strtoul(name
+4, &ep
, 10);
1096 num
= strtoul(name
+2, &ep
, 10);
1100 int stat2devnum(struct stat
*st
)
1102 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1103 if (major(st
->st_rdev
) == MD_MAJOR
)
1104 return minor(st
->st_rdev
);
1106 return -1- (minor(st
->st_rdev
)>>6);
1112 int fd2devnum(int fd
)
1115 if (fstat(fd
, &stb
) == 0)
1116 return stat2devnum(&stb
);
1120 int mdmon_running(int devnum
)
1126 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1127 fd
= open(path
, O_RDONLY
, 0);
1131 n
= read(fd
, pid
, 9);
1135 if (kill(atoi(pid
), 0) == 0)
1140 int signal_mdmon(int devnum
)
1146 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1147 fd
= open(path
, O_RDONLY
, 0);
1151 n
= read(fd
, pid
, 9);
1155 if (kill(atoi(pid
), SIGUSR1
) == 0)
1160 int start_mdmon(int devnum
)
1177 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1181 sl
= strrchr(pathbuf
, '/');
1186 strcpy(sl
, "mdmon");
1192 /* FIXME yuk. CLOSE_EXEC?? */
1193 for (i
=3; i
< 100; i
++)
1195 for (i
=0; paths
[i
]; i
++)
1197 execl(paths
[i
], "mdmon",
1198 map_dev(dev2major(devnum
),
1202 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1203 "Array remains readonly\n");
1205 default: /* parent - good */
1206 pid
= wait(&status
);
1207 if (pid
< 0 || status
!= 0)
1213 int env_no_mdmon(void)
1215 char *val
= getenv("MDADM_NO_MDMON");
1217 if (val
&& atoi(val
) == 1)
1224 int flush_metadata_updates(struct supertype
*st
)
1228 st
->update_tail
= NULL
;
1232 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1236 while (st
->updates
) {
1237 struct metadata_update
*mu
= st
->updates
;
1238 st
->updates
= mu
->next
;
1240 send_message(sfd
, mu
, 0);
1248 st
->update_tail
= NULL
;
1252 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1255 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1261 *st
->update_tail
= mu
;
1262 st
->update_tail
= &mu
->next
;
1264 #endif /* MDASSEMBLE */
1267 /* tinyc doesn't optimize this check in ioctl.h out ... */
1268 unsigned int __invalid_size_argument_for_IOC
= 0;