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>
40 * following taken from linux/blkpg.h because they aren't
41 * anywhere else and it isn't safe to #include linux/ * stuff.
44 #define BLKPG _IO(0x12,105)
46 /* The argument structure */
47 struct blkpg_ioctl_arg
{
54 /* The subfunctions (for the op field) */
55 #define BLKPG_ADD_PARTITION 1
56 #define BLKPG_DEL_PARTITION 2
58 /* Sizes of name fields. Unused at present. */
59 #define BLKPG_DEVNAMELTH 64
60 #define BLKPG_VOLNAMELTH 64
62 /* The data structure for ADD_PARTITION and DEL_PARTITION */
63 struct blkpg_partition
{
64 long long start
; /* starting offset in bytes */
65 long long length
; /* length in bytes */
66 int pno
; /* partition number */
67 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
68 to be used in kernel messages */
69 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
73 * Parse a 128 bit uuid in 4 integers
74 * format is 32 hexx nibbles with options :.<space> separator
75 * If not exactly 32 hex digits are found, return 0
78 int parse_uuid(char *str
, int uuid
[4])
80 int hit
= 0; /* number of Hex digIT */
83 for (i
=0; i
<4; i
++) uuid
[i
]=0;
89 else if (c
>='a' && c
<= 'f')
91 else if (c
>='A' && c
<= 'F')
93 else if (strchr(":. -", c
))
110 * Get the md version number.
111 * We use the RAID_VERSION ioctl if it is supported
112 * If not, but we have a block device with major '9', we assume
115 * Return version number as 24 but number - assume version parts
119 int md_get_version(int fd
)
124 if (fstat(fd
, &stb
)<0)
126 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
129 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
130 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
133 if (major(stb
.st_rdev
) == MD_MAJOR
)
138 int get_linux_version()
147 a
= strtoul(cp
, &cp
, 10);
148 if (*cp
!= '.') return -1;
149 b
= strtoul(cp
+1, &cp
, 10);
150 if (*cp
!= '.') return -1;
151 c
= strtoul(cp
+1, NULL
, 10);
153 return (a
*1000000)+(b
*1000)+c
;
156 void remove_partitions(int fd
)
158 /* remove partitions from this block devices.
159 * This is used for components added to an array
161 #ifdef BLKPG_DEL_PARTITION
162 struct blkpg_ioctl_arg a
;
163 struct blkpg_partition p
;
165 a
.op
= BLKPG_DEL_PARTITION
;
167 a
.datalen
= sizeof(p
);
169 memset(a
.data
, 0, a
.datalen
);
170 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
171 ioctl(fd
, BLKPG
, &a
);
175 int enough(int level
, int raid_disks
, int layout
, int clean
,
176 char *avail
, int avail_disks
)
181 /* This is the tricky one - we need to check
182 * which actual disks are present.
184 copies
= (layout
&255)* ((layout
>>8) & 255);
187 /* there must be one of the 'copies' form 'first' */
193 first
= (first
+1) % raid_disks
;
198 } while (first
!= 0);
202 return avail_disks
>= 1;
205 return avail_disks
== raid_disks
;
207 return avail_disks
>= 1;
211 return avail_disks
>= raid_disks
-1;
213 return avail_disks
>= raid_disks
;
216 return avail_disks
>= raid_disks
-2;
218 return avail_disks
>= raid_disks
;
224 int same_uuid(int a
[4], int b
[4], int swapuuid
)
227 /* parse uuids are hostendian.
228 * uuid's from some superblocks are big-ending
229 * if there is a difference, we need to swap..
231 unsigned char *ac
= (unsigned char *)a
;
232 unsigned char *bc
= (unsigned char *)b
;
234 for (i
=0; i
<16; i
+= 4) {
235 if (ac
[i
+0] != bc
[i
+3] ||
236 ac
[i
+1] != bc
[i
+2] ||
237 ac
[i
+2] != bc
[i
+1] ||
251 void copy_uuid(void *a
, int b
[4], int swapuuid
)
254 /* parse uuids are hostendian.
255 * uuid's from some superblocks are big-ending
256 * if there is a difference, we need to swap..
258 unsigned char *ac
= (unsigned char *)a
;
259 unsigned char *bc
= (unsigned char *)b
;
261 for (i
=0; i
<16; i
+= 4) {
272 int check_ext2(int fd
, char *name
)
275 * Check for an ext2fs file system.
276 * Superblock is always 1K at 1K offset
278 * s_magic is le16 at 56 == 0xEF53
279 * report mtime - le32 at 44
281 * logblksize - le32 at 24
283 unsigned char sb
[1024];
286 if (lseek(fd
, 1024,0)!= 1024)
288 if (read(fd
, sb
, 1024)!= 1024)
290 if (sb
[56] != 0x53 || sb
[57] != 0xef)
293 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
294 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
295 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
296 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
298 fprintf(stderr
," size=%dK mtime=%s",
299 size
*(1<<bsize
), ctime(&mtime
));
303 int check_reiser(int fd
, char *name
)
306 * superblock is at 64K
308 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
311 unsigned char sb
[1024];
313 if (lseek(fd
, 64*1024, 0) != 64*1024)
315 if (read(fd
, sb
, 1024) != 1024)
317 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
318 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
320 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
321 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
322 fprintf(stderr
, " size = %luK\n", size
*4);
327 int check_raid(int fd
, char *name
)
332 struct supertype
*st
= guess_super(fd
);
335 st
->ss
->load_super(st
, fd
, name
);
336 /* Looks like a raid array .. */
337 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
339 st
->ss
->getinfo_super(st
, &info
);
340 st
->ss
->free_super(st
);
341 crtime
= info
.array
.ctime
;
342 level
= map_num(pers
, info
.array
.level
);
343 if (!level
) level
= "-unknown-";
344 fprintf(stderr
, " level=%s devices=%d ctime=%s",
345 level
, info
.array
.raid_disks
, ctime(&crtime
));
353 for (i
=0; i
<5; i
++) {
355 fprintf(stderr
, "%s%s", mesg
, add
);
357 if (fgets(buf
, 100, stdin
)==NULL
)
359 if (buf
[0]=='y' || buf
[0]=='Y')
361 if (buf
[0]=='n' || buf
[0]=='N')
365 fprintf(stderr
, Name
": assuming 'no'\n");
368 #endif /* MDASSEMBLE */
370 char *map_num(mapping_t
*map
, int num
)
380 int map_name(mapping_t
*map
, char *name
)
383 if (strcmp(map
->name
, name
)==0)
391 int is_standard(char *dev
, int *nump
)
393 /* tests if dev is a "standard" md dev name.
394 * i.e if the last component is "/dNN" or "/mdNN",
395 * where NN is a string of digits
396 * Returns 1 if a partitionable standard,
397 * -1 if non-partitonable,
398 * 0 if not a standard name.
400 char *d
= strrchr(dev
, '/');
405 if (strncmp(d
, "/d",2)==0)
406 d
+= 2, type
=1; /* /dev/md/dN{pM} */
407 else if (strncmp(d
, "/md_d", 5)==0)
408 d
+= 5, type
=1; /* /dev/md_dNpM */
409 else if (strncmp(d
, "/md", 3)==0)
410 d
+= 3, type
=-1; /* /dev/mdN */
411 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
412 d
+= 1, type
=-1; /* /dev/md/N */
422 if (nump
) *nump
= num
;
429 * convert a major/minor pair for a block device into a name in /dev, if possible.
430 * On the first call, walk /dev collecting name.
431 * Put them in a simple linked listfor now.
438 int devlist_ready
= 0;
440 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
443 if (S_ISLNK(stb
->st_mode
)) {
448 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
449 char *n
= strdup(name
);
450 struct devmap
*dm
= malloc(sizeof(*dm
));
451 if (strncmp(n
, "/dev/./", 7)==0)
454 dm
->major
= major(stb
->st_rdev
);
455 dm
->minor
= minor(stb
->st_rdev
);
466 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
468 return add_dev(name
, stb
, flag
, NULL
);
470 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
472 return ftw(path
, add_dev_1
, nopenfd
);
475 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
479 #endif /* HAVE_FTW */
480 #endif /* HAVE_NFTW */
483 * Find a block device with the right major/minor number.
484 * If we find multiple names, choose the shortest.
485 * If we find a non-standard name, it is probably there
486 * deliberately so prefer it over a standard name.
487 * This applies only to names for MD devices.
489 char *map_dev(int major
, int minor
, int create
)
492 char *std
= NULL
, *nonstd
=NULL
;
495 if (major
== 0 && minor
== 0)
499 if (!devlist_ready
) {
503 struct devmap
*d
= devlist
;
508 if (lstat(dev
, &stb
)==0 &&
509 S_ISLNK(stb
.st_mode
))
511 nftw(dev
, add_dev
, 10, FTW_PHYS
);
516 for (p
=devlist
; p
; p
=p
->next
)
517 if (p
->major
== major
&&
519 if (is_standard(p
->name
, NULL
)) {
521 strlen(p
->name
) < strlen(std
))
524 if (nonstd
== NULL
||
525 strlen(p
->name
) < strlen(nonstd
))
529 if (!std
&& !nonstd
&& !did_check
) {
533 if (create
&& !std
&& !nonstd
) {
535 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
539 return nonstd
? nonstd
: std
;
542 unsigned long calc_csum(void *super
, int bytes
)
544 unsigned long long newcsum
= 0;
547 unsigned int *superc
= (unsigned int*) super
;
549 for(i
=0; i
<bytes
/4; i
++)
551 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
553 /* The in-kernel checksum calculation is always 16bit on
554 * the alpha, though it is 32 bit on i386...
555 * I wonder what it is elsewhere... (it uses and API in
556 * a way that it shouldn't).
558 csum
= (csum
& 0xffff) + (csum
>> 16);
559 csum
= (csum
& 0xffff) + (csum
>> 16);
565 char *human_size(long long bytes
)
569 /* We convert bytes to either centi-M{ega,ibi}bytes or
570 * centi-G{igi,ibi}bytes, with appropriate rounding,
571 * and then print 1/100th of those as a decimal.
572 * We allow upto 2048Megabytes before converting to
573 * gigabytes, as that shows more precision and isn't
574 * too large a number.
575 * Terrabytes are not yet handled.
578 if (bytes
< 5000*1024)
580 else if (bytes
< 2*1024LL*1024LL*1024LL) {
581 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
582 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
583 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
584 cMiB
/100 , cMiB
% 100,
587 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
588 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
589 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
590 cGiB
/100 , cGiB
% 100,
596 char *human_size_brief(long long bytes
)
600 if (bytes
< 5000*1024)
601 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
602 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
604 else if (bytes
< 2*1024LL*1024LL*1024LL)
605 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
607 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
610 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
612 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
618 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
619 int chunksize
, unsigned long long devsize
)
623 case 0: data_disks
= raid_disks
; break;
624 case 1: data_disks
= 1; break;
626 case 5: data_disks
= raid_disks
- 1; break;
627 case 6: data_disks
= raid_disks
- 2; break;
628 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
631 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
632 return data_disks
* devsize
;
635 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
636 int get_mdp_major(void)
638 static int mdp_major
= -1;
642 int have_devices
= 0;
647 fl
= fopen("/proc/devices", "r");
650 while ((w
= conf_word(fl
, 1))) {
651 if (have_block
&& strcmp(w
, "devices:")==0)
653 have_block
= (strcmp(w
, "Block")==0);
656 if (have_devices
&& strcmp(w
, "mdp")==0)
657 mdp_major
= last_num
;
666 char *get_md_name(int dev
)
668 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
669 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
670 static char devname
[50];
676 int mdp
= get_mdp_major();
677 if (mdp
< 0) return NULL
;
678 rdev
= makedev(mdp
, (-1-dev
)<<6);
679 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
680 if (stat(devname
, &stb
) == 0
681 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
682 && (stb
.st_rdev
== rdev
))
685 rdev
= makedev(MD_MAJOR
, dev
);
686 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
687 if (stat(devname
, &stb
) == 0
688 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
689 && (stb
.st_rdev
== rdev
))
692 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
693 if (stat(devname
, &stb
) == 0
694 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
695 && (stb
.st_rdev
== rdev
))
698 dn
= map_dev(major(rdev
), minor(rdev
), 0);
701 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
702 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
706 if (stat(devname
, &stb
) == 0
707 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
708 && (stb
.st_rdev
== rdev
))
714 void put_md_name(char *name
)
716 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
720 int find_free_devnum(int use_partitions
)
723 for (devnum
= 127; devnum
!= 128;
724 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
728 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
729 if (mddev_busy(_devnum
))
731 /* make sure it is new to /dev too, at least as a
733 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
734 if (dn
&& ! is_standard(dn
, NULL
))
740 return use_partitions
? (-1-devnum
) : devnum
;
742 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
744 int dev_open(char *dev
, int flags
)
746 /* like 'open', but if 'dev' matches %d:%d, create a temp
747 * block device and open that
757 major
= strtoul(dev
, &e
, 0);
758 if (e
> dev
&& *e
== ':' && e
[1] &&
759 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
761 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
762 (int)getpid(), major
, minor
);
763 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
764 fd
= open(devname
, flags
|O_DIRECT
);
768 fd
= open(dev
, flags
|O_DIRECT
);
772 int open_dev_excl(int devnum
)
777 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
778 for (i
=0 ; i
<25 ; i
++) {
779 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
789 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
791 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
793 struct supertype
*super_by_fd(int fd
)
795 mdu_array_info_t array
;
798 struct supertype
*st
= NULL
;
803 char *subarray
= NULL
;
805 sra
= sysfs_read(fd
, 0, GET_VERSION
);
808 vers
= sra
->array
.major_version
;
809 minor
= sra
->array
.minor_version
;
810 verstr
= sra
->text_version
;
812 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
813 array
.major_version
= array
.minor_version
= 0;
814 vers
= array
.major_version
;
815 minor
= array
.minor_version
;
820 sprintf(version
, "%d.%d", vers
, minor
);
823 if (minor
== -2 && verstr
[0] == '/') {
824 char *dev
= verstr
+1;
825 subarray
= strchr(dev
, '/');
829 devnum
= devname2devnum(dev
);
830 subarray
= strdup(subarray
);
833 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
834 verstr
= sra
->text_version
? : "-no-metadata-";
837 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
838 st
= superlist
[i
]->match_metadata_desc(verstr
);
845 strncpy(st
->subarray
, subarray
, 32);
846 st
->subarray
[31] = 0;
853 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
856 struct supertype
*dup_super(struct supertype
*orig
)
858 struct supertype
*st
;
862 st
= malloc(sizeof(*st
));
865 memset(st
, 0, sizeof(*st
));
867 st
->max_devs
= orig
->max_devs
;
868 st
->minor_version
= orig
->minor_version
;
869 strcpy(st
->subarray
, orig
->subarray
);
875 struct supertype
*guess_super(int fd
)
877 /* try each load_super to find the best match,
878 * and return the best superswitch
880 struct superswitch
*ss
;
881 struct supertype
*st
;
882 unsigned long besttime
= 0;
886 st
= malloc(sizeof(*st
));
887 for (i
=0 ; superlist
[i
]; i
++) {
890 memset(st
, 0, sizeof(*st
));
891 rv
= ss
->load_super(st
, fd
, NULL
);
894 st
->ss
->getinfo_super(st
, &info
);
895 if (bestsuper
== -1 ||
896 besttime
< info
.array
.ctime
) {
898 besttime
= info
.array
.ctime
;
903 if (bestsuper
!= -1) {
905 memset(st
, 0, sizeof(*st
));
906 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
908 superlist
[bestsuper
]->free_super(st
);
916 /* Return size of device in bytes */
917 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
919 unsigned long long ldsize
;
922 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
923 ldsize
= (unsigned long long)st
.st_size
;
926 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
930 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
935 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
936 dname
, strerror(errno
));
944 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
947 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
948 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
949 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
953 int open_container(int fd
)
955 /* 'fd' is a block device. Find out if it is in use
956 * by a container, and return an open fd on that container.
967 if (fstat(fd
, &st
) != 0)
969 sprintf(path
, "/sys/dev/block/%d:%d/holders",
970 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
971 e
= path
+ strlen(path
);
976 while ((de
= readdir(dir
))) {
979 if (de
->d_name
[0] == '.')
981 sprintf(e
, "/%s/dev", de
->d_name
);
982 dfd
= open(path
, O_RDONLY
);
985 n
= read(dfd
, buf
, sizeof(buf
));
987 if (n
<= 0 || n
>= sizeof(buf
))
990 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
992 sprintf(buf
, "%d:%d", major
, minor
);
993 dfd
= dev_open(buf
, O_RDONLY
);
1003 char *devnum2devname(int num
)
1007 sprintf(name
, "md%d", num
);
1009 sprintf(name
, "md_d%d", -1-num
);
1010 return strdup(name
);
1013 int devname2devnum(char *name
)
1017 if (strncmp(name
, "md_d", 4)==0)
1018 num
= -1-strtoul(name
+4, &ep
, 10);
1020 num
= strtoul(name
+2, &ep
, 10);
1024 int fd2devnum(int fd
)
1027 if (fstat(fd
, &stb
) == 0 &&
1028 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
1029 if (major(stb
.st_rdev
) == MD_MAJOR
)
1030 return minor(stb
.st_rdev
);
1032 return -1- (minor(stb
.st_rdev
)>>6);
1037 int mdmon_running(int devnum
)
1043 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1044 fd
= open(path
, O_RDONLY
, 0);
1048 n
= read(fd
, pid
, 9);
1052 if (kill(atoi(pid
), 0) == 0)
1057 int signal_mdmon(int devnum
)
1063 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1064 fd
= open(path
, O_RDONLY
, 0);
1068 n
= read(fd
, pid
, 9);
1072 if (kill(atoi(pid
), SIGUSR1
) == 0)
1077 int start_mdmon(int devnum
)
1092 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1096 sl
= strrchr(pathbuf
, '/');
1101 strcpy(sl
, "mdmon");
1107 /* FIXME yuk. CLOSE_EXEC?? */
1108 for (i
=3; i
< 100; i
++)
1110 for (i
=0; paths
[i
]; i
++)
1112 execl(paths
[i
], "mdmon",
1113 map_dev(dev2major(devnum
),
1117 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1118 "Array remains readonly\n");
1120 default: ; /* parent - good */
1125 int env_no_mdmon(void)
1127 char *val
= getenv("MDADM_NO_MDMON");
1129 if (val
&& atoi(val
) == 1)
1136 int flush_metadata_updates(struct supertype
*st
)
1140 st
->update_tail
= NULL
;
1144 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1148 while (st
->updates
) {
1149 struct metadata_update
*mu
= st
->updates
;
1150 st
->updates
= mu
->next
;
1152 send_message(sfd
, mu
, 0);
1160 st
->update_tail
= NULL
;
1164 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1167 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1173 *st
->update_tail
= mu
;
1174 st
->update_tail
= &mu
->next
;
1179 /* tinyc doesn't optimize this check in ioctl.h out ... */
1180 unsigned int __invalid_size_argument_for_IOC
= 0;