]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
util: md_array_valid(): Introduce md_array_valid() helper
authorJes Sorensen <jsorensen@fb.com>
Wed, 3 May 2017 18:25:57 +0000 (14:25 -0400)
committerJes Sorensen <jsorensen@fb.com>
Wed, 3 May 2017 20:15:16 +0000 (16:15 -0400)
Using md_get_array_info() to determine if an array is valid is broken
during creation, since the ioctl() returns -ENODEV if the device is
valid but not active.

Where did I leave my stash of brown paper bags?

Fixes: ("40b054e mdopen/open_mddev: Use md_get_array_info() to determine valid array")
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
mdadm.h
mdopen.c
util.c

diff --git a/mdadm.h b/mdadm.h
index 6a382a7c1b904431ef748626b47688289096d2a5..07ee9635e7959fc7a1615a3731ecb4dbdc7985db 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_valid(int fd);
 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);
index 099efa0aa2e5cfaf5512054cd568a5005b393161..c4f1c12c2dcd8a9ce574ef3d77147826e96adbd3 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -442,7 +442,6 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
  */
 int open_mddev(char *dev, int report_errors)
 {
-       struct mdu_array_info_s array;
        int mdfd = open(dev, O_RDONLY);
 
        if (mdfd < 0) {
@@ -452,7 +451,7 @@ int open_mddev(char *dev, int report_errors)
                return -1;
        }
 
-       if (md_get_array_info(mdfd, &array) != 0) {
+       if (md_array_valid(mdfd) == 0) {
                close(mdfd);
                if (report_errors)
                        pr_err("%s does not appear to be an md device\n", dev);
diff --git a/util.c b/util.c
index 21a63c9a638ca9e6bbb6d5a1b2ea4b5e6d3efaa5..c7585aca3e4a76e98cda5d288fc39e3323c30bca 100644 (file)
--- a/util.c
+++ b/util.c
@@ -200,6 +200,30 @@ out:
        return ret;
 }
 
+int md_array_valid(int fd)
+{
+       struct mdinfo *sra;
+       int ret;
+
+       sra = sysfs_read(fd, NULL, GET_ARRAY_STATE);
+       if (sra) {
+               if (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, RAID_VERSION);
+       }
+
+       return !ret;
+}
+
 int md_array_active(int fd)
 {
        struct mdinfo *sra;