]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Util: get device size from id
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 66c7f0b9d6ddfa5f26ccc3bc77d13a481c72fbac..1ecce3b8fd73d598051955bd8c6343b65bf29a9b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1086,14 +1086,30 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
                sysfs_free(sra);
        if (st) {
                st->sb = NULL;
-               st->subarray[0] = 0;
-               *subarrayp = subarray;
+               if (subarrayp)
+                       *subarrayp = subarray;
        } else
                free(subarray);
        return st;
 }
 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
 
+int dev_size_from_id(unsigned int id, unsigned long long *size)
+{
+       char buf[20];
+       int fd;
+
+       sprintf(buf, "%d:%d", major(id), minor(id));
+       fd = dev_open(buf, O_RDONLY);
+       if (fd < 0)
+               return 0;
+       if (get_dev_size(fd, NULL, size)) {
+               close(fd);
+               return 1;
+       }
+       close(fd);
+       return 0;
+}
 
 struct supertype *dup_super(struct supertype *orig)
 {
@@ -1108,7 +1124,6 @@ struct supertype *dup_super(struct supertype *orig)
        st->ss = orig->ss;
        st->max_devs = orig->max_devs;
        st->minor_version = orig->minor_version;
-       strcpy(st->subarray, orig->subarray);
        st->sb = NULL;
        st->info = NULL;
        return st;
@@ -1126,6 +1141,9 @@ struct supertype *guess_super_type(int fd, enum guess_types guess_type)
        int i;
 
        st = malloc(sizeof(*st));
+       memset(st, 0, sizeof(*st));
+       st->container_dev = NoMdDev;
+
        for (i=0 ; superlist[i]; i++) {
                int rv;
                ss = superlist[i];
@@ -1187,6 +1205,20 @@ int get_dev_size(int fd, char *dname, unsigned long long *sizep)
        return 1;
 }
 
+/* Return true if this can only be a container, not a member device.
+ * i.e. is and md device and size is zero
+ */
+int must_be_container(int fd)
+{
+       unsigned long long size;
+       if (md_get_version(fd) < 0)
+               return 0;
+       if (get_dev_size(fd, NULL, &size) == 0)
+               return 1;
+       if (size == 0)
+               return 1;
+       return 0;
+}
 
 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
  * Returns: 1 if successful
@@ -1446,6 +1478,7 @@ int is_container_active(char *container)
 int open_subarray(char *dev, char *subarray, struct supertype *st, int quiet)
 {
        struct mdinfo *mdi;
+       struct mdinfo *info;
        int fd, err = 1;
 
        fd = open(dev, O_RDWR|O_EXCL);
@@ -1495,20 +1528,27 @@ int open_subarray(char *dev, char *subarray, struct supertype *st, int quiet)
                goto free_sysfs;
        }
 
-       strncpy(st->subarray, subarray, sizeof(st->subarray)-1);
+       if (!st->ss->load_container) {
+               if (!quiet)
+                       fprintf(stderr, Name ": %s is not a container\n", dev);
+               goto free_name;
+       }
 
-       if (st->ss->load_super(st, fd, NULL)) {
+       if (st->ss->load_container(st, fd, NULL)) {
                if (!quiet)
-                       fprintf(stderr, Name ": Failed to find subarray-%s in %s\n",
-                               st->subarray, dev);
+                       fprintf(stderr, Name ": Failed to load metadata for %s\n",
+                               dev);
                goto free_name;
        }
 
-       if (!st->loaded_container) {
+       info = st->ss->container_content(st, subarray);
+       if (!info) {
                if (!quiet)
-                       fprintf(stderr, Name ": %s is not a container\n", dev);
+                       fprintf(stderr, Name ": Failed to find subarray-%s in %s\n",
+                               subarray, dev);
                goto free_super;
        }
+       free(info);
 
        err = 0;