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 int same_uuid(int a
[4], int b
[4], int swapuuid
)
228 /* parse uuids are hostendian.
229 * uuid's from some superblocks are big-ending
230 * if there is a difference, we need to swap..
232 unsigned char *ac
= (unsigned char *)a
;
233 unsigned char *bc
= (unsigned char *)b
;
235 for (i
=0; i
<16; i
+= 4) {
236 if (ac
[i
+0] != bc
[i
+3] ||
237 ac
[i
+1] != bc
[i
+2] ||
238 ac
[i
+2] != bc
[i
+1] ||
252 void copy_uuid(void *a
, int b
[4], int swapuuid
)
255 /* parse uuids are hostendian.
256 * uuid's from some superblocks are big-ending
257 * if there is a difference, we need to swap..
259 unsigned char *ac
= (unsigned char *)a
;
260 unsigned char *bc
= (unsigned char *)b
;
262 for (i
=0; i
<16; i
+= 4) {
272 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
)
279 copy_uuid(uuid
, info
->uuid
, st
->ss
->swapuuid
);
280 for (i
=0; i
<16; i
++) {
285 sprintf(c
,"%02x", (unsigned char)uuid
[i
]);
292 int check_ext2(int fd
, char *name
)
295 * Check for an ext2fs file system.
296 * Superblock is always 1K at 1K offset
298 * s_magic is le16 at 56 == 0xEF53
299 * report mtime - le32 at 44
301 * logblksize - le32 at 24
303 unsigned char sb
[1024];
306 if (lseek(fd
, 1024,0)!= 1024)
308 if (read(fd
, sb
, 1024)!= 1024)
310 if (sb
[56] != 0x53 || sb
[57] != 0xef)
313 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
314 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
315 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
316 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
318 fprintf(stderr
," size=%dK mtime=%s",
319 size
*(1<<bsize
), ctime(&mtime
));
323 int check_reiser(int fd
, char *name
)
326 * superblock is at 64K
328 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
331 unsigned char sb
[1024];
333 if (lseek(fd
, 64*1024, 0) != 64*1024)
335 if (read(fd
, sb
, 1024) != 1024)
337 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
338 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
340 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
341 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
342 fprintf(stderr
, " size = %luK\n", size
*4);
347 int check_raid(int fd
, char *name
)
352 struct supertype
*st
= guess_super(fd
);
355 st
->ss
->load_super(st
, fd
, name
);
356 /* Looks like a raid array .. */
357 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
359 st
->ss
->getinfo_super(st
, &info
);
360 st
->ss
->free_super(st
);
361 crtime
= info
.array
.ctime
;
362 level
= map_num(pers
, info
.array
.level
);
363 if (!level
) level
= "-unknown-";
364 fprintf(stderr
, " level=%s devices=%d ctime=%s",
365 level
, info
.array
.raid_disks
, ctime(&crtime
));
373 for (i
=0; i
<5; i
++) {
375 fprintf(stderr
, "%s%s", mesg
, add
);
377 if (fgets(buf
, 100, stdin
)==NULL
)
379 if (buf
[0]=='y' || buf
[0]=='Y')
381 if (buf
[0]=='n' || buf
[0]=='N')
385 fprintf(stderr
, Name
": assuming 'no'\n");
388 #endif /* MDASSEMBLE */
390 char *map_num(mapping_t
*map
, int num
)
400 int map_name(mapping_t
*map
, char *name
)
403 if (strcmp(map
->name
, name
)==0)
411 int is_standard(char *dev
, int *nump
)
413 /* tests if dev is a "standard" md dev name.
414 * i.e if the last component is "/dNN" or "/mdNN",
415 * where NN is a string of digits
416 * Returns 1 if a partitionable standard,
417 * -1 if non-partitonable,
418 * 0 if not a standard name.
420 char *d
= strrchr(dev
, '/');
425 if (strncmp(d
, "/d",2)==0)
426 d
+= 2, type
=1; /* /dev/md/dN{pM} */
427 else if (strncmp(d
, "/md_d", 5)==0)
428 d
+= 5, type
=1; /* /dev/md_dNpM */
429 else if (strncmp(d
, "/md", 3)==0)
430 d
+= 3, type
=-1; /* /dev/mdN */
431 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
432 d
+= 1, type
=-1; /* /dev/md/N */
442 if (nump
) *nump
= num
;
449 * convert a major/minor pair for a block device into a name in /dev, if possible.
450 * On the first call, walk /dev collecting name.
451 * Put them in a simple linked listfor now.
458 int devlist_ready
= 0;
460 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
463 if (S_ISLNK(stb
->st_mode
)) {
468 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
469 char *n
= strdup(name
);
470 struct devmap
*dm
= malloc(sizeof(*dm
));
471 if (strncmp(n
, "/dev/./", 7)==0)
474 dm
->major
= major(stb
->st_rdev
);
475 dm
->minor
= minor(stb
->st_rdev
);
486 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
488 return add_dev(name
, stb
, flag
, NULL
);
490 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
492 return ftw(path
, add_dev_1
, nopenfd
);
495 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
499 #endif /* HAVE_FTW */
500 #endif /* HAVE_NFTW */
503 * Find a block device with the right major/minor number.
504 * If we find multiple names, choose the shortest.
505 * If we find a non-standard name, it is probably there
506 * deliberately so prefer it over a standard name.
507 * This applies only to names for MD devices.
509 char *map_dev(int major
, int minor
, int create
)
512 char *std
= NULL
, *nonstd
=NULL
;
515 if (major
== 0 && minor
== 0)
519 if (!devlist_ready
) {
523 struct devmap
*d
= devlist
;
528 if (lstat(dev
, &stb
)==0 &&
529 S_ISLNK(stb
.st_mode
))
531 nftw(dev
, add_dev
, 10, FTW_PHYS
);
536 for (p
=devlist
; p
; p
=p
->next
)
537 if (p
->major
== major
&&
539 if (is_standard(p
->name
, NULL
)) {
541 strlen(p
->name
) < strlen(std
))
544 if (nonstd
== NULL
||
545 strlen(p
->name
) < strlen(nonstd
))
549 if (!std
&& !nonstd
&& !did_check
) {
553 if (create
&& !std
&& !nonstd
) {
555 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
559 return nonstd
? nonstd
: std
;
562 unsigned long calc_csum(void *super
, int bytes
)
564 unsigned long long newcsum
= 0;
567 unsigned int *superc
= (unsigned int*) super
;
569 for(i
=0; i
<bytes
/4; i
++)
571 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
573 /* The in-kernel checksum calculation is always 16bit on
574 * the alpha, though it is 32 bit on i386...
575 * I wonder what it is elsewhere... (it uses and API in
576 * a way that it shouldn't).
578 csum
= (csum
& 0xffff) + (csum
>> 16);
579 csum
= (csum
& 0xffff) + (csum
>> 16);
585 char *human_size(long long bytes
)
589 /* We convert bytes to either centi-M{ega,ibi}bytes or
590 * centi-G{igi,ibi}bytes, with appropriate rounding,
591 * and then print 1/100th of those as a decimal.
592 * We allow upto 2048Megabytes before converting to
593 * gigabytes, as that shows more precision and isn't
594 * too large a number.
595 * Terrabytes are not yet handled.
598 if (bytes
< 5000*1024)
600 else if (bytes
< 2*1024LL*1024LL*1024LL) {
601 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
602 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
603 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
604 cMiB
/100 , cMiB
% 100,
607 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
608 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
609 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
610 cGiB
/100 , cGiB
% 100,
616 char *human_size_brief(long long bytes
)
620 if (bytes
< 5000*1024)
621 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
622 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
624 else if (bytes
< 2*1024LL*1024LL*1024LL)
625 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
627 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
630 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
632 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
638 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
639 int chunksize
, unsigned long long devsize
)
643 case 0: data_disks
= raid_disks
; break;
644 case 1: data_disks
= 1; break;
646 case 5: data_disks
= raid_disks
- 1; break;
647 case 6: data_disks
= raid_disks
- 2; break;
648 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
651 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
652 return data_disks
* devsize
;
655 int get_mdp_major(void)
657 static int mdp_major
= -1;
661 int have_devices
= 0;
666 fl
= fopen("/proc/devices", "r");
669 while ((w
= conf_word(fl
, 1))) {
670 if (have_block
&& strcmp(w
, "devices:")==0)
672 have_block
= (strcmp(w
, "Block")==0);
675 if (have_devices
&& strcmp(w
, "mdp")==0)
676 mdp_major
= last_num
;
683 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
684 char *get_md_name(int dev
)
686 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
687 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
688 static char devname
[50];
694 int mdp
= get_mdp_major();
695 if (mdp
< 0) return NULL
;
696 rdev
= makedev(mdp
, (-1-dev
)<<6);
697 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
698 if (stat(devname
, &stb
) == 0
699 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
700 && (stb
.st_rdev
== rdev
))
703 rdev
= makedev(MD_MAJOR
, dev
);
704 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
705 if (stat(devname
, &stb
) == 0
706 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
707 && (stb
.st_rdev
== rdev
))
710 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
711 if (stat(devname
, &stb
) == 0
712 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
713 && (stb
.st_rdev
== rdev
))
716 dn
= map_dev(major(rdev
), minor(rdev
), 0);
719 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
720 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
724 if (stat(devname
, &stb
) == 0
725 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
726 && (stb
.st_rdev
== rdev
))
732 void put_md_name(char *name
)
734 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
738 int find_free_devnum(int use_partitions
)
741 for (devnum
= 127; devnum
!= 128;
742 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
746 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
747 if (mddev_busy(_devnum
))
749 /* make sure it is new to /dev too, at least as a
751 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
752 if (dn
&& ! is_standard(dn
, NULL
))
758 return use_partitions
? (-1-devnum
) : devnum
;
760 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
762 int dev_open(char *dev
, int flags
)
764 /* like 'open', but if 'dev' matches %d:%d, create a temp
765 * block device and open that
775 major
= strtoul(dev
, &e
, 0);
776 if (e
> dev
&& *e
== ':' && e
[1] &&
777 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
779 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
780 (int)getpid(), major
, minor
);
781 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
782 fd
= open(devname
, flags
|O_DIRECT
);
786 fd
= open(dev
, flags
|O_DIRECT
);
790 int open_dev_excl(int devnum
)
795 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
796 for (i
=0 ; i
<25 ; i
++) {
797 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
807 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
809 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
811 struct supertype
*super_by_fd(int fd
)
813 mdu_array_info_t array
;
816 struct supertype
*st
= NULL
;
821 char *subarray
= NULL
;
823 sra
= sysfs_read(fd
, 0, GET_VERSION
);
826 vers
= sra
->array
.major_version
;
827 minor
= sra
->array
.minor_version
;
828 verstr
= sra
->text_version
;
830 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
831 array
.major_version
= array
.minor_version
= 0;
832 vers
= array
.major_version
;
833 minor
= array
.minor_version
;
838 sprintf(version
, "%d.%d", vers
, minor
);
841 if (minor
== -2 && is_subarray(verstr
)) {
842 char *dev
= verstr
+1;
843 subarray
= strchr(dev
, '/');
847 devnum
= devname2devnum(dev
);
848 subarray
= strdup(subarray
);
851 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
852 verstr
= sra
->text_version
? : "-no-metadata-";
855 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
856 st
= superlist
[i
]->match_metadata_desc(verstr
);
863 strncpy(st
->subarray
, subarray
, 32);
864 st
->subarray
[31] = 0;
871 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
874 struct supertype
*dup_super(struct supertype
*orig
)
876 struct supertype
*st
;
880 st
= malloc(sizeof(*st
));
883 memset(st
, 0, sizeof(*st
));
885 st
->max_devs
= orig
->max_devs
;
886 st
->minor_version
= orig
->minor_version
;
887 strcpy(st
->subarray
, orig
->subarray
);
893 struct supertype
*guess_super(int fd
)
895 /* try each load_super to find the best match,
896 * and return the best superswitch
898 struct superswitch
*ss
;
899 struct supertype
*st
;
900 unsigned long besttime
= 0;
904 st
= malloc(sizeof(*st
));
905 for (i
=0 ; superlist
[i
]; i
++) {
908 memset(st
, 0, sizeof(*st
));
909 rv
= ss
->load_super(st
, fd
, NULL
);
912 st
->ss
->getinfo_super(st
, &info
);
913 if (bestsuper
== -1 ||
914 besttime
< info
.array
.ctime
) {
916 besttime
= info
.array
.ctime
;
921 if (bestsuper
!= -1) {
923 memset(st
, 0, sizeof(*st
));
924 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
926 superlist
[bestsuper
]->free_super(st
);
934 /* Return size of device in bytes */
935 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
937 unsigned long long ldsize
;
940 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
941 ldsize
= (unsigned long long)st
.st_size
;
944 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
948 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
953 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
954 dname
, strerror(errno
));
962 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
965 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
966 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
967 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
971 int open_container(int fd
)
973 /* 'fd' is a block device. Find out if it is in use
974 * by a container, and return an open fd on that container.
985 if (fstat(fd
, &st
) != 0)
987 sprintf(path
, "/sys/dev/block/%d:%d/holders",
988 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
989 e
= path
+ strlen(path
);
994 while ((de
= readdir(dir
))) {
997 if (de
->d_name
[0] == '.')
999 sprintf(e
, "/%s/dev", de
->d_name
);
1000 dfd
= open(path
, O_RDONLY
);
1003 n
= read(dfd
, buf
, sizeof(buf
));
1005 if (n
<= 0 || n
>= sizeof(buf
))
1008 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1010 sprintf(buf
, "%d:%d", major
, minor
);
1011 dfd
= dev_open(buf
, O_RDONLY
);
1021 int add_disk(int mdfd
, struct supertype
*st
,
1022 struct mdinfo
*sra
, struct mdinfo
*info
)
1024 /* Add a device to an array, in one of 2 ways. */
1027 if (st
->ss
->external
) {
1028 rv
= sysfs_add_disk(sra
, info
);
1031 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1035 sd2
= malloc(sizeof(*sd2
));
1037 sd2
->next
= sra
->devs
;
1043 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1047 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1049 /* Initialise kernel's knowledge of array.
1050 * This varies between externally managed arrays
1053 int vers
= md_get_version(mdfd
);
1057 if (st
->ss
->external
)
1058 rv
= sysfs_set_array(info
, vers
);
1061 if ((vers
% 100) >= 1) { /* can use different versions */
1062 mdu_array_info_t inf
;
1063 memset(&inf
, 0, sizeof(inf
));
1064 inf
.major_version
= info
->array
.major_version
;
1065 inf
.minor_version
= info
->array
.minor_version
;
1066 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1068 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1072 char *devnum2devname(int num
)
1076 sprintf(name
, "md%d", num
);
1078 sprintf(name
, "md_d%d", -1-num
);
1079 return strdup(name
);
1082 int devname2devnum(char *name
)
1086 if (strncmp(name
, "md_d", 4)==0)
1087 num
= -1-strtoul(name
+4, &ep
, 10);
1089 num
= strtoul(name
+2, &ep
, 10);
1093 int stat2devnum(struct stat
*st
)
1095 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1096 if (major(st
->st_rdev
) == MD_MAJOR
)
1097 return minor(st
->st_rdev
);
1099 return -1- (minor(st
->st_rdev
)>>6);
1105 int fd2devnum(int fd
)
1108 if (fstat(fd
, &stb
) == 0)
1109 return stat2devnum(&stb
);
1113 int mdmon_running(int devnum
)
1119 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1120 fd
= open(path
, O_RDONLY
, 0);
1124 n
= read(fd
, pid
, 9);
1128 if (kill(atoi(pid
), 0) == 0)
1133 int signal_mdmon(int devnum
)
1139 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1140 fd
= open(path
, O_RDONLY
, 0);
1144 n
= read(fd
, pid
, 9);
1148 if (kill(atoi(pid
), SIGUSR1
) == 0)
1153 int start_mdmon(int devnum
)
1170 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1174 sl
= strrchr(pathbuf
, '/');
1179 strcpy(sl
, "mdmon");
1185 /* FIXME yuk. CLOSE_EXEC?? */
1186 for (i
=3; i
< 100; i
++)
1188 for (i
=0; paths
[i
]; i
++)
1190 execl(paths
[i
], "mdmon",
1191 map_dev(dev2major(devnum
),
1195 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1196 "Array remains readonly\n");
1198 default: /* parent - good */
1199 pid
= wait(&status
);
1200 if (pid
< 0 || status
!= 0)
1206 int env_no_mdmon(void)
1208 char *val
= getenv("MDADM_NO_MDMON");
1210 if (val
&& atoi(val
) == 1)
1217 int flush_metadata_updates(struct supertype
*st
)
1221 st
->update_tail
= NULL
;
1225 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1229 while (st
->updates
) {
1230 struct metadata_update
*mu
= st
->updates
;
1231 st
->updates
= mu
->next
;
1233 send_message(sfd
, mu
, 0);
1241 st
->update_tail
= NULL
;
1245 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1248 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1254 *st
->update_tail
= mu
;
1255 st
->update_tail
= &mu
->next
;
1257 #endif /* MDASSEMBLE */
1260 /* tinyc doesn't optimize this check in ioctl.h out ... */
1261 unsigned int __invalid_size_argument_for_IOC
= 0;