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 <jsorensen@fb.com>
This commit is contained in:
Jes Sorensen 2017-04-13 13:30:17 -04:00
parent 5e4ca8bb82
commit 3ab8f4bf33
3 changed files with 31 additions and 2 deletions

View File

@ -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";

View File

@ -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);

27
util.c
View File

@ -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.