From: Jes Sorensen Date: Thu, 13 Apr 2017 17:30:17 +0000 (-0400) Subject: util: Introduce md_array_active() helper X-Git-Tag: mdadm-4.1-rc1~183 X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fmdadm.git;a=commitdiff_plain;h=3ab8f4bf33d906cb1084f7b4036556bfb4bb73ec;hp=5e4ca8bb82e98400c9258cb3d7e4d030576f21df util: Introduce md_array_active() helper Rather than querying md_get_array_info() to determine whether an array is valid, do the work in md_array_active() using sysfs, and fall back on md_get_array_info() if sysfs fails. Signed-off-by: Jes Sorensen --- diff --git a/Query.c b/Query.c index b761c470..4dec9f51 100644 --- a/Query.c +++ b/Query.c @@ -38,7 +38,6 @@ int Query(char *dev) int level, raid_disks, spare_disks; struct mdinfo info; struct mdinfo *sra; - mdu_array_info_t array; struct supertype *st = NULL; unsigned long long larray_size; struct stat stb; @@ -65,6 +64,8 @@ int Query(char *dev) raid_disks = sra->array.raid_disks; spare_disks = sra->array.spare_disks; } else { + mdu_array_info_t array; + if (md_get_array_info(fd, &array) < 0) { ioctlerr = errno; } else { @@ -111,7 +112,7 @@ int Query(char *dev) disc.number = info.disk.number; activity = "undetected"; if (mddev && (fd = open(mddev, O_RDONLY))>=0) { - if (md_get_array_info(fd, &array) >= 0) { + if (md_array_active(fd)) { if (md_get_disk_info(fd, &disc) >= 0 && makedev((unsigned)disc.major,(unsigned)disc.minor) == stb.st_rdev) activity = "active"; diff --git a/mdadm.h b/mdadm.h index a3799733..f6e97fdb 100644 --- a/mdadm.h +++ b/mdadm.h @@ -1415,6 +1415,7 @@ extern int Dump_metadata(char *dev, char *dir, struct context *c, extern int Restore_metadata(char *dev, char *dir, struct context *c, struct supertype *st, int only); +int md_array_active(int fd); int md_get_array_info(int fd, struct mdu_array_info_s *array); int md_set_array_info(int fd, struct mdu_array_info_s *array); int md_get_disk_info(int fd, struct mdu_disk_info_s *disk); diff --git a/util.c b/util.c index a695c45b..3adc675a 100644 --- a/util.c +++ b/util.c @@ -200,6 +200,33 @@ out: return ret; } +int md_array_active(int fd) +{ + struct mdinfo *sra; + struct mdu_array_info_s array; + int ret; + + sra = sysfs_read(fd, NULL, GET_ARRAY_STATE); + if (sra) { + if (sra->array_state != ARRAY_CLEAR && + sra->array_state != ARRAY_INACTIVE && + sra->array_state != ARRAY_UNKNOWN_STATE) + ret = 0; + else + ret = -ENODEV; + + free(sra); + } else { + /* + * GET_ARRAY_INFO doesn't provide access to the proper state + * information, so fallback to a basic check for raid_disks != 0 + */ + ret = ioctl(fd, GET_ARRAY_INFO, &array); + } + + return !ret; +} + /* * Get array info from the kernel. Longer term we want to deprecate the * ioctl and get it from sysfs.