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) {
273 int check_ext2(int fd
, char *name
)
276 * Check for an ext2fs file system.
277 * Superblock is always 1K at 1K offset
279 * s_magic is le16 at 56 == 0xEF53
280 * report mtime - le32 at 44
282 * logblksize - le32 at 24
284 unsigned char sb
[1024];
287 if (lseek(fd
, 1024,0)!= 1024)
289 if (read(fd
, sb
, 1024)!= 1024)
291 if (sb
[56] != 0x53 || sb
[57] != 0xef)
294 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
295 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
296 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
297 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
299 fprintf(stderr
," size=%dK mtime=%s",
300 size
*(1<<bsize
), ctime(&mtime
));
304 int check_reiser(int fd
, char *name
)
307 * superblock is at 64K
309 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
312 unsigned char sb
[1024];
314 if (lseek(fd
, 64*1024, 0) != 64*1024)
316 if (read(fd
, sb
, 1024) != 1024)
318 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
319 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
321 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
322 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
323 fprintf(stderr
, " size = %luK\n", size
*4);
328 int check_raid(int fd
, char *name
)
333 struct supertype
*st
= guess_super(fd
);
336 st
->ss
->load_super(st
, fd
, name
);
337 /* Looks like a raid array .. */
338 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
340 st
->ss
->getinfo_super(st
, &info
);
341 st
->ss
->free_super(st
);
342 crtime
= info
.array
.ctime
;
343 level
= map_num(pers
, info
.array
.level
);
344 if (!level
) level
= "-unknown-";
345 fprintf(stderr
, " level=%s devices=%d ctime=%s",
346 level
, info
.array
.raid_disks
, ctime(&crtime
));
354 for (i
=0; i
<5; i
++) {
356 fprintf(stderr
, "%s%s", mesg
, add
);
358 if (fgets(buf
, 100, stdin
)==NULL
)
360 if (buf
[0]=='y' || buf
[0]=='Y')
362 if (buf
[0]=='n' || buf
[0]=='N')
366 fprintf(stderr
, Name
": assuming 'no'\n");
369 #endif /* MDASSEMBLE */
371 char *map_num(mapping_t
*map
, int num
)
381 int map_name(mapping_t
*map
, char *name
)
384 if (strcmp(map
->name
, name
)==0)
392 int is_standard(char *dev
, int *nump
)
394 /* tests if dev is a "standard" md dev name.
395 * i.e if the last component is "/dNN" or "/mdNN",
396 * where NN is a string of digits
397 * Returns 1 if a partitionable standard,
398 * -1 if non-partitonable,
399 * 0 if not a standard name.
401 char *d
= strrchr(dev
, '/');
406 if (strncmp(d
, "/d",2)==0)
407 d
+= 2, type
=1; /* /dev/md/dN{pM} */
408 else if (strncmp(d
, "/md_d", 5)==0)
409 d
+= 5, type
=1; /* /dev/md_dNpM */
410 else if (strncmp(d
, "/md", 3)==0)
411 d
+= 3, type
=-1; /* /dev/mdN */
412 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
413 d
+= 1, type
=-1; /* /dev/md/N */
423 if (nump
) *nump
= num
;
430 * convert a major/minor pair for a block device into a name in /dev, if possible.
431 * On the first call, walk /dev collecting name.
432 * Put them in a simple linked listfor now.
439 int devlist_ready
= 0;
441 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
444 if (S_ISLNK(stb
->st_mode
)) {
449 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
450 char *n
= strdup(name
);
451 struct devmap
*dm
= malloc(sizeof(*dm
));
452 if (strncmp(n
, "/dev/./", 7)==0)
455 dm
->major
= major(stb
->st_rdev
);
456 dm
->minor
= minor(stb
->st_rdev
);
467 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
469 return add_dev(name
, stb
, flag
, NULL
);
471 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
473 return ftw(path
, add_dev_1
, nopenfd
);
476 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
480 #endif /* HAVE_FTW */
481 #endif /* HAVE_NFTW */
484 * Find a block device with the right major/minor number.
485 * If we find multiple names, choose the shortest.
486 * If we find a non-standard name, it is probably there
487 * deliberately so prefer it over a standard name.
488 * This applies only to names for MD devices.
490 char *map_dev(int major
, int minor
, int create
)
493 char *std
= NULL
, *nonstd
=NULL
;
496 if (major
== 0 && minor
== 0)
500 if (!devlist_ready
) {
504 struct devmap
*d
= devlist
;
509 if (lstat(dev
, &stb
)==0 &&
510 S_ISLNK(stb
.st_mode
))
512 nftw(dev
, add_dev
, 10, FTW_PHYS
);
517 for (p
=devlist
; p
; p
=p
->next
)
518 if (p
->major
== major
&&
520 if (is_standard(p
->name
, NULL
)) {
522 strlen(p
->name
) < strlen(std
))
525 if (nonstd
== NULL
||
526 strlen(p
->name
) < strlen(nonstd
))
530 if (!std
&& !nonstd
&& !did_check
) {
534 if (create
&& !std
&& !nonstd
) {
536 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
540 return nonstd
? nonstd
: std
;
543 unsigned long calc_csum(void *super
, int bytes
)
545 unsigned long long newcsum
= 0;
548 unsigned int *superc
= (unsigned int*) super
;
550 for(i
=0; i
<bytes
/4; i
++)
552 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
554 /* The in-kernel checksum calculation is always 16bit on
555 * the alpha, though it is 32 bit on i386...
556 * I wonder what it is elsewhere... (it uses and API in
557 * a way that it shouldn't).
559 csum
= (csum
& 0xffff) + (csum
>> 16);
560 csum
= (csum
& 0xffff) + (csum
>> 16);
566 char *human_size(long long bytes
)
570 /* We convert bytes to either centi-M{ega,ibi}bytes or
571 * centi-G{igi,ibi}bytes, with appropriate rounding,
572 * and then print 1/100th of those as a decimal.
573 * We allow upto 2048Megabytes before converting to
574 * gigabytes, as that shows more precision and isn't
575 * too large a number.
576 * Terrabytes are not yet handled.
579 if (bytes
< 5000*1024)
581 else if (bytes
< 2*1024LL*1024LL*1024LL) {
582 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
583 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
584 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
585 cMiB
/100 , cMiB
% 100,
588 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
589 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
590 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
591 cGiB
/100 , cGiB
% 100,
597 char *human_size_brief(long long bytes
)
601 if (bytes
< 5000*1024)
602 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
603 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
605 else if (bytes
< 2*1024LL*1024LL*1024LL)
606 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
608 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
611 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
613 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
619 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
620 int chunksize
, unsigned long long devsize
)
624 case 0: data_disks
= raid_disks
; break;
625 case 1: data_disks
= 1; break;
627 case 5: data_disks
= raid_disks
- 1; break;
628 case 6: data_disks
= raid_disks
- 2; break;
629 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
632 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
633 return data_disks
* devsize
;
636 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
637 int get_mdp_major(void)
639 static int mdp_major
= -1;
643 int have_devices
= 0;
648 fl
= fopen("/proc/devices", "r");
651 while ((w
= conf_word(fl
, 1))) {
652 if (have_block
&& strcmp(w
, "devices:")==0)
654 have_block
= (strcmp(w
, "Block")==0);
657 if (have_devices
&& strcmp(w
, "mdp")==0)
658 mdp_major
= last_num
;
667 char *get_md_name(int dev
)
669 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
670 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
671 static char devname
[50];
677 int mdp
= get_mdp_major();
678 if (mdp
< 0) return NULL
;
679 rdev
= makedev(mdp
, (-1-dev
)<<6);
680 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
681 if (stat(devname
, &stb
) == 0
682 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
683 && (stb
.st_rdev
== rdev
))
686 rdev
= makedev(MD_MAJOR
, dev
);
687 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
688 if (stat(devname
, &stb
) == 0
689 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
690 && (stb
.st_rdev
== rdev
))
693 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
694 if (stat(devname
, &stb
) == 0
695 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
696 && (stb
.st_rdev
== rdev
))
699 dn
= map_dev(major(rdev
), minor(rdev
), 0);
702 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
703 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
707 if (stat(devname
, &stb
) == 0
708 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
709 && (stb
.st_rdev
== rdev
))
715 void put_md_name(char *name
)
717 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
721 int find_free_devnum(int use_partitions
)
724 for (devnum
= 127; devnum
!= 128;
725 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
729 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
730 if (mddev_busy(_devnum
))
732 /* make sure it is new to /dev too, at least as a
734 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
735 if (dn
&& ! is_standard(dn
, NULL
))
741 return use_partitions
? (-1-devnum
) : devnum
;
743 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
745 int dev_open(char *dev
, int flags
)
747 /* like 'open', but if 'dev' matches %d:%d, create a temp
748 * block device and open that
758 major
= strtoul(dev
, &e
, 0);
759 if (e
> dev
&& *e
== ':' && e
[1] &&
760 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
762 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
763 (int)getpid(), major
, minor
);
764 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
765 fd
= open(devname
, flags
|O_DIRECT
);
769 fd
= open(dev
, flags
|O_DIRECT
);
773 int open_dev_excl(int devnum
)
778 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
779 for (i
=0 ; i
<25 ; i
++) {
780 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
790 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
792 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
794 struct supertype
*super_by_fd(int fd
)
796 mdu_array_info_t array
;
799 struct supertype
*st
= NULL
;
804 char *subarray
= NULL
;
806 sra
= sysfs_read(fd
, 0, GET_VERSION
);
809 vers
= sra
->array
.major_version
;
810 minor
= sra
->array
.minor_version
;
811 verstr
= sra
->text_version
;
813 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
814 array
.major_version
= array
.minor_version
= 0;
815 vers
= array
.major_version
;
816 minor
= array
.minor_version
;
821 sprintf(version
, "%d.%d", vers
, minor
);
824 if (minor
== -2 && is_subarray(verstr
)) {
825 char *dev
= verstr
+1;
826 subarray
= strchr(dev
, '/');
830 devnum
= devname2devnum(dev
);
831 subarray
= strdup(subarray
);
834 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
835 verstr
= sra
->text_version
? : "-no-metadata-";
838 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
839 st
= superlist
[i
]->match_metadata_desc(verstr
);
846 strncpy(st
->subarray
, subarray
, 32);
847 st
->subarray
[31] = 0;
854 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
857 struct supertype
*dup_super(struct supertype
*orig
)
859 struct supertype
*st
;
863 st
= malloc(sizeof(*st
));
866 memset(st
, 0, sizeof(*st
));
868 st
->max_devs
= orig
->max_devs
;
869 st
->minor_version
= orig
->minor_version
;
870 strcpy(st
->subarray
, orig
->subarray
);
876 struct supertype
*guess_super(int fd
)
878 /* try each load_super to find the best match,
879 * and return the best superswitch
881 struct superswitch
*ss
;
882 struct supertype
*st
;
883 unsigned long besttime
= 0;
887 st
= malloc(sizeof(*st
));
888 for (i
=0 ; superlist
[i
]; i
++) {
891 memset(st
, 0, sizeof(*st
));
892 rv
= ss
->load_super(st
, fd
, NULL
);
895 st
->ss
->getinfo_super(st
, &info
);
896 if (bestsuper
== -1 ||
897 besttime
< info
.array
.ctime
) {
899 besttime
= info
.array
.ctime
;
904 if (bestsuper
!= -1) {
906 memset(st
, 0, sizeof(*st
));
907 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
909 superlist
[bestsuper
]->free_super(st
);
917 /* Return size of device in bytes */
918 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
920 unsigned long long ldsize
;
923 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
924 ldsize
= (unsigned long long)st
.st_size
;
927 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
931 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
936 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
937 dname
, strerror(errno
));
945 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
948 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
949 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
950 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
954 int open_container(int fd
)
956 /* 'fd' is a block device. Find out if it is in use
957 * by a container, and return an open fd on that container.
968 if (fstat(fd
, &st
) != 0)
970 sprintf(path
, "/sys/dev/block/%d:%d/holders",
971 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
972 e
= path
+ strlen(path
);
977 while ((de
= readdir(dir
))) {
980 if (de
->d_name
[0] == '.')
982 sprintf(e
, "/%s/dev", de
->d_name
);
983 dfd
= open(path
, O_RDONLY
);
986 n
= read(dfd
, buf
, sizeof(buf
));
988 if (n
<= 0 || n
>= sizeof(buf
))
991 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
993 sprintf(buf
, "%d:%d", major
, minor
);
994 dfd
= dev_open(buf
, O_RDONLY
);
1004 char *devnum2devname(int num
)
1008 sprintf(name
, "md%d", num
);
1010 sprintf(name
, "md_d%d", -1-num
);
1011 return strdup(name
);
1014 int devname2devnum(char *name
)
1018 if (strncmp(name
, "md_d", 4)==0)
1019 num
= -1-strtoul(name
+4, &ep
, 10);
1021 num
= strtoul(name
+2, &ep
, 10);
1025 int fd2devnum(int fd
)
1028 if (fstat(fd
, &stb
) == 0 &&
1029 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
1030 if (major(stb
.st_rdev
) == MD_MAJOR
)
1031 return minor(stb
.st_rdev
);
1033 return -1- (minor(stb
.st_rdev
)>>6);
1038 int mdmon_running(int devnum
)
1044 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1045 fd
= open(path
, O_RDONLY
, 0);
1049 n
= read(fd
, pid
, 9);
1053 if (kill(atoi(pid
), 0) == 0)
1058 int signal_mdmon(int devnum
)
1064 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1065 fd
= open(path
, O_RDONLY
, 0);
1069 n
= read(fd
, pid
, 9);
1073 if (kill(atoi(pid
), SIGUSR1
) == 0)
1078 int start_mdmon(int devnum
)
1095 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1099 sl
= strrchr(pathbuf
, '/');
1104 strcpy(sl
, "mdmon");
1110 /* FIXME yuk. CLOSE_EXEC?? */
1111 for (i
=3; i
< 100; i
++)
1113 for (i
=0; paths
[i
]; i
++)
1115 execl(paths
[i
], "mdmon",
1116 map_dev(dev2major(devnum
),
1120 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1121 "Array remains readonly\n");
1123 default: /* parent - good */
1124 pid
= wait(&status
);
1125 if (pid
< 0 || status
!= 0)
1131 int env_no_mdmon(void)
1133 char *val
= getenv("MDADM_NO_MDMON");
1135 if (val
&& atoi(val
) == 1)
1142 int flush_metadata_updates(struct supertype
*st
)
1146 st
->update_tail
= NULL
;
1150 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1154 while (st
->updates
) {
1155 struct metadata_update
*mu
= st
->updates
;
1156 st
->updates
= mu
->next
;
1158 send_message(sfd
, mu
, 0);
1166 st
->update_tail
= NULL
;
1170 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1173 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1179 *st
->update_tail
= mu
;
1180 st
->update_tail
= &mu
->next
;
1185 /* tinyc doesn't optimize this check in ioctl.h out ... */
1186 unsigned int __invalid_size_argument_for_IOC
= 0;