]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
util: Introduce md_array_active() helper
authorJes Sorensen <jsorensen@fb.com>
Thu, 13 Apr 2017 17:30:17 +0000 (13:30 -0400)
committerJes Sorensen <jsorensen@fb.com>
Thu, 20 Apr 2017 04:12:34 +0000 (00:12 -0400)
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>
Query.c
mdadm.h
util.c

diff --git a/Query.c b/Query.c
index b761c470b268702d6191f93a2507588c8b68f126..4dec9f5104f81882326b2d499eba4bba79cdbb4f 100644 (file)
--- 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 a3799733a4d9f11d65160b49c366caac79602bb4..f6e97fdb7ca9d4ff9f942d0021af3bd6151f74d2 100644 (file)
--- 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 a695c45bf867b38977ac7ae1797b323ee1dbb0b1..3adc675a87547d47c02d19c5e1c4d931ecd43e50 100644 (file)
--- 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.