]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
sysfs: Make sysfs_init() return an error code
authorJes Sorensen <Jes.Sorensen@gmail.com>
Thu, 30 Mar 2017 20:52:37 +0000 (16:52 -0400)
committerJes Sorensen <Jes.Sorensen@gmail.com>
Thu, 30 Mar 2017 20:52:37 +0000 (16:52 -0400)
Rather than have the caller inspect the returned content, return an
error code from sysfs_init(). In addition make all callers actually
check it.

Signed-off-by: Jes Sorensen <Jes.Sorensen@gmail.com>
Assemble.c
Create.c
Grow.c
Incremental.c
Manage.c
Monitor.c
mdadm.c
mdadm.h
sysfs.c

index 6a6a56bfb8b9311197a96c0f7e5c98ba3bb7301e..672cd12c1dd294e319c590851c6cec07b9b06d09 100644 (file)
@@ -1670,7 +1670,12 @@ try_again:
        }
        st->ss->getinfo_super(st, content, NULL);
 #ifndef MDASSEMBLE
        }
        st->ss->getinfo_super(st, content, NULL);
 #ifndef MDASSEMBLE
-       sysfs_init(content, mdfd, NULL);
+       if (sysfs_init(content, mdfd, NULL)) {
+               pr_err("Unable to initialize sysfs\n");
+               close(mdfd);
+               free(devices);
+               return 1;
+       }
 #endif
        /* after reload context, store journal_clean in context */
        content->journal_clean = journal_clean;
 #endif
        /* after reload context, store journal_clean in context */
        content->journal_clean = journal_clean;
@@ -1885,7 +1890,10 @@ int assemble_container_content(struct supertype *st, int mdfd,
        char *avail;
        int err;
 
        char *avail;
        int err;
 
-       sysfs_init(content, mdfd, NULL);
+       if (sysfs_init(content, mdfd, NULL)) {
+               pr_err("Unable to initialize sysfs\n");
+               return 1;
+       }
 
        sra = sysfs_read(mdfd, NULL, GET_VERSION|GET_DEVS);
        if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
 
        sra = sysfs_read(mdfd, NULL, GET_VERSION|GET_DEVS);
        if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0) {
index 0e0778ff5327054e7b86be98279ce3903ed871d9..32987af3e5c7921c1e72a339251f2dce69d8e940 100644 (file)
--- a/Create.c
+++ b/Create.c
@@ -737,7 +737,10 @@ int Create(struct supertype *st, char *mddev,
 
        total_slots = info.array.nr_disks;
        st->ss->getinfo_super(st, &info, NULL);
 
        total_slots = info.array.nr_disks;
        st->ss->getinfo_super(st, &info, NULL);
-       sysfs_init(&info, mdfd, NULL);
+       if (sysfs_init(&info, mdfd, NULL)) {
+               pr_err("unable to initialize sysfs\n");
+               goto abort_locked;
+       }
 
        if (did_default && c->verbose >= 0) {
                if (is_subarray(info.text_version)) {
 
        if (did_default && c->verbose >= 0) {
                if (is_subarray(info.text_version)) {
@@ -794,7 +797,10 @@ int Create(struct supertype *st, char *mddev,
                s->bitmap_file = NULL;
        }
 
                s->bitmap_file = NULL;
        }
 
-       sysfs_init(&info, mdfd, NULL);
+       if (sysfs_init(&info, mdfd, NULL)) {
+               pr_err("unable to initialize sysfs\n");
+               goto abort_locked;
+       }
 
        if (st->ss->external && st->container_devnm[0]) {
                /* member */
 
        if (st->ss->external && st->container_devnm[0]) {
                /* member */
diff --git a/Grow.c b/Grow.c
index 0c16d5b25deb0badbc20da4945cec1e058d23900..78a34746f9f397f7a4900fcc45b084b5c088e519 100755 (executable)
--- a/Grow.c
+++ b/Grow.c
@@ -455,7 +455,10 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                }
                if (offset_setable) {
                        st->ss->getinfo_super(st, mdi, NULL);
                }
                if (offset_setable) {
                        st->ss->getinfo_super(st, mdi, NULL);
-                       sysfs_init(mdi, fd, NULL);
+                       if (sysfs_init(mdi, fd, NULL)) {
+                               pr_err("failed to intialize sysfs.\n");
+                               free(mdi);
+                       }
                        rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
                                                  mdi->bitmap_offset);
                        free(mdi);
                        rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
                                                  mdi->bitmap_offset);
                        free(mdi);
@@ -2149,7 +2152,11 @@ size_change_error:
 
        memset(&info, 0, sizeof(info));
        info.array = array;
 
        memset(&info, 0, sizeof(info));
        info.array = array;
-       sysfs_init(&info, fd, NULL);
+       if (sysfs_init(&info, fd, NULL)) {
+               pr_err("failed to intialize sysfs.\n");
+               rv = 1;
+               goto release;
+       }
        strcpy(info.text_version, sra->text_version);
        info.component_size = s->size*2;
        info.new_level = s->level;
        strcpy(info.text_version, sra->text_version);
        info.component_size = s->size*2;
        info.new_level = s->level;
@@ -2870,7 +2877,11 @@ static int impose_level(int fd, int level, char *devname, int verbose)
        char *c;
        struct mdu_array_info_s array;
        struct mdinfo info;
        char *c;
        struct mdu_array_info_s array;
        struct mdinfo info;
-       sysfs_init(&info, fd, NULL);
+
+       if (sysfs_init(&info, fd, NULL)) {
+               pr_err("failed to intialize sysfs.\n");
+               return  1;
+       }
 
        md_get_array_info(fd, &array);
        if (level == 0 &&
 
        md_get_array_info(fd, &array);
        if (level == 0 &&
@@ -3178,7 +3189,12 @@ static int reshape_array(char *container, int fd, char *devname,
                struct mdinfo *d;
 
                if (info2) {
                struct mdinfo *d;
 
                if (info2) {
-                       sysfs_init(info2, fd, st->devnm);
+                       if (sysfs_init(info2, fd, st->devnm)) {
+                               pr_err("unable to initialize sysfs for %s",
+                                      st->devnm);
+                               free(info2);
+                               goto release;
+                       }
                        /* When increasing number of devices, we need to set
                         * new raid_disks before adding these, or they might
                         * be rejected.
                        /* When increasing number of devices, we need to set
                         * new raid_disks before adding these, or they might
                         * be rejected.
@@ -3777,7 +3793,12 @@ int reshape_container(char *container, char *devname,
                }
                strcpy(last_devnm, mdstat->devnm);
 
                }
                strcpy(last_devnm, mdstat->devnm);
 
-               sysfs_init(content, fd, mdstat->devnm);
+               if (sysfs_init(content, fd, mdstat->devnm)) {
+                       pr_err("Unable to initialize sysfs for %s\n",
+                              mdstat->devnm);
+                       rv = 1;
+                       break;
+               }
 
                if (mdmon_running(container))
                        flush_mdmon(container);
 
                if (mdmon_running(container))
                        flush_mdmon(container);
@@ -5110,7 +5131,13 @@ int Grow_continue_command(char *devname, int fd,
                        goto Grow_continue_command_exit;
                }
 
                        goto Grow_continue_command_exit;
                }
 
-               sysfs_init(content, fd2, mdstat->devnm);
+               if (sysfs_init(content, fd2, mdstat->devnm)) {
+                       pr_err("Unable to initialize sysfs for %s, Grow cannot continue",
+                              mdstat->devnm);
+                       ret_val = 1;
+                       close(fd2);
+                       goto Grow_continue_command_exit;
+               }
 
                close(fd2);
 
 
                close(fd2);
 
index 802e5255d3389c74f00aef55a00c549ee31db926..28f1f7734956277c371b3d60915c1ff113b754db 100644 (file)
@@ -326,7 +326,12 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
                if (mdfd < 0)
                        goto out_unlock;
 
                if (mdfd < 0)
                        goto out_unlock;
 
-               sysfs_init(&info, mdfd, NULL);
+               if (sysfs_init(&info, mdfd, NULL)) {
+                       pr_err("unable to initialize sysfs for %s\n",
+                              chosen_name);
+                       rv = 2;
+                       goto out_unlock;
+               }
 
                if (set_array_info(mdfd, st, &info) != 0) {
                        pr_err("failed to set array info for %s: %s\n",
 
                if (set_array_info(mdfd, st, &info) != 0) {
                        pr_err("failed to set array info for %s: %s\n",
@@ -1734,7 +1739,10 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
                        pr_err("%s does not appear to be a component of any array\n", devname);
                return 1;
        }
                        pr_err("%s does not appear to be a component of any array\n", devname);
                return 1;
        }
-       sysfs_init(&mdi, -1, ent->devnm);
+       if (sysfs_init(&mdi, -1, ent->devnm)) {
+               pr_err("unable to initialize sysfs for: %s\n", devname);
+               return 1;
+       }
        mdfd = open_dev_excl(ent->devnm);
        if (mdfd > 0) {
                close(mdfd);
        mdfd = open_dev_excl(ent->devnm);
        if (mdfd > 0) {
                close(mdfd);
index 0ffb6c68dec2334047dc34a1040a88b90b269f96..618c98b4dd216c281266784c4114d06ffbb07c7f 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -1382,12 +1382,15 @@ int Manage_subdevs(char *devname, int fd,
        int busy = 0;
        int raid_slot = -1;
 
        int busy = 0;
        int raid_slot = -1;
 
+       if (sysfs_init(&info, fd, NULL)) {
+               pr_err("sysfs not availabile for %s\n", devname);
+               goto abort;
+       }
+
        if (md_get_array_info(fd, &array)) {
                pr_err("Cannot get array info for %s\n", devname);
                goto abort;
        }
        if (md_get_array_info(fd, &array)) {
                pr_err("Cannot get array info for %s\n", devname);
                goto abort;
        }
-       sysfs_init(&info, fd, NULL);
-
        /* array.size is only 32 bits and may be truncated.
         * So read from sysfs if possible, and record number of sectors
         */
        /* array.size is only 32 bits and may be truncated.
         * So read from sysfs if possible, and record number of sectors
         */
index 2c0f717ce505c4572f21b9ca4d709cf9efba14e0..036a561c6f99684ec809c4da0fb0804296eba01f 100644 (file)
--- a/Monitor.c
+++ b/Monitor.c
@@ -1026,7 +1026,9 @@ int Wait(char *dev)
                         */
                        struct mdinfo mdi;
                        char buf[21];
                         */
                        struct mdinfo mdi;
                        char buf[21];
-                       sysfs_init(&mdi, -1, devnm);
+
+                       if (sysfs_init(&mdi, -1, devnm))
+                               return 2;
                        if (sysfs_get_str(&mdi, NULL, "sync_action",
                                          buf, 20) > 0 &&
                            strcmp(buf,"idle\n") != 0) {
                        if (sysfs_get_str(&mdi, NULL, "sync_action",
                                          buf, 20) > 0 &&
                            strcmp(buf,"idle\n") != 0) {
diff --git a/mdadm.c b/mdadm.c
index d6b54378a6bed8a5d4748c1299246b3a77af9d37..3fe17fc0e15873402ad1ffef0e0656248f54accd 100644 (file)
--- a/mdadm.c
+++ b/mdadm.c
@@ -1631,7 +1631,10 @@ int main(int argc, char *argv[])
                                rv = 1;
                                break;
                        }
                                rv = 1;
                                break;
                        }
-                       sysfs_init(&sra, mdfd, NULL);
+                       if (sysfs_init(&sra, mdfd, NULL)) {
+                               rv = 1;
+                               break;
+                       }
                        if (array_size == MAX_SIZE)
                                err = sysfs_set_str(&sra, NULL, "array_size", "default");
                        else
                        if (array_size == MAX_SIZE)
                                err = sysfs_set_str(&sra, NULL, "array_size", "default");
                        else
@@ -1998,13 +2001,15 @@ int SetAction(char *dev, char *action)
 {
        int fd = open(dev, O_RDONLY);
        struct mdinfo mdi;
 {
        int fd = open(dev, O_RDONLY);
        struct mdinfo mdi;
+       int retval;
+
        if (fd < 0) {
                pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
                return 1;
        }
        if (fd < 0) {
                pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
                return 1;
        }
-       sysfs_init(&mdi, fd, NULL);
+       retval = sysfs_init(&mdi, fd, NULL);
        close(fd);
        close(fd);
-       if (!mdi.sys_name[0]) {
+       if (retval) {
                pr_err("%s is no an md array\n", dev);
                return 1;
        }
                pr_err("%s is no an md array\n", dev);
                return 1;
        }
diff --git a/mdadm.h b/mdadm.h
index 084bc970692eba5ab22212f06849aeb3f7cb56f3..612bd863eef315b13490a2d7183aa1d2e755f1a6 100644 (file)
--- a/mdadm.h
+++ b/mdadm.h
@@ -639,7 +639,7 @@ enum sysfs_read_flags {
  * else use devnm.
  */
 extern int sysfs_open(char *devnm, char *devname, char *attr);
  * else use devnm.
  */
 extern int sysfs_open(char *devnm, char *devname, char *attr);
-extern void sysfs_init(struct mdinfo *mdi, int fd, char *devnm);
+extern int sysfs_init(struct mdinfo *mdi, int fd, char *devnm);
 extern void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid);
 extern void sysfs_free(struct mdinfo *sra);
 extern struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options);
 extern void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid);
 extern void sysfs_free(struct mdinfo *sra);
 extern struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options);
diff --git a/sysfs.c b/sysfs.c
index 93ec3de826c1c491c9f92a6255667683b025c66d..51deb2376f3cd85c87ba66ec8a27ae01347f5751 100644 (file)
--- a/sysfs.c
+++ b/sysfs.c
@@ -84,25 +84,30 @@ void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid)
                 sizeof(mdi->sys_name), "dev-%s", devid2kname(devid));
 }
 
                 sizeof(mdi->sys_name), "dev-%s", devid2kname(devid));
 }
 
-void sysfs_init(struct mdinfo *mdi, int fd, char *devnm)
+int sysfs_init(struct mdinfo *mdi, int fd, char *devnm)
 {
        struct stat stb;
        char fname[MAX_SYSFS_PATH_LEN];
 {
        struct stat stb;
        char fname[MAX_SYSFS_PATH_LEN];
+       int retval = -ENODEV;
 
        mdi->sys_name[0] = 0;
        if (fd >= 0)
                devnm = fd2devnm(fd);
 
        if (devnm == NULL)
 
        mdi->sys_name[0] = 0;
        if (fd >= 0)
                devnm = fd2devnm(fd);
 
        if (devnm == NULL)
-               return;
+               goto out;
 
        snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm);
 
        if (stat(fname, &stb))
 
        snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm);
 
        if (stat(fname, &stb))
-               return;
+               goto out;
        if (!S_ISDIR(stb.st_mode))
        if (!S_ISDIR(stb.st_mode))
-               return;
+               goto out;
        strcpy(mdi->sys_name, devnm);
        strcpy(mdi->sys_name, devnm);
+
+       retval = 0;
+out:
+       return retval;
 }
 
 struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
 }
 
 struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
@@ -117,8 +122,7 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
        struct dirent *de;
 
        sra = xcalloc(1, sizeof(*sra));
        struct dirent *de;
 
        sra = xcalloc(1, sizeof(*sra));
-       sysfs_init(sra, fd, devnm);
-       if (sra->sys_name[0] == 0) {
+       if (sysfs_init(sra, fd, devnm)) {
                free(sra);
                return NULL;
        }
                free(sra);
                return NULL;
        }