]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
util: Introduce md_array_active() helper
[thirdparty/mdadm.git] / util.c
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.