]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
mdadm: Fixup more broken logical operator formatting
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
old mode 100755 (executable)
new mode 100644 (file)
index 1c90902..db3f18b
--- a/Grow.c
+++ b/Grow.c
@@ -109,7 +109,7 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
         */
        struct mdinfo info;
 
-       struct stat stb;
+       dev_t rdev;
        int nfd, fd2;
        int d, nd;
        struct supertype *st = NULL;
@@ -145,9 +145,7 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
                free(st);
                return 1;
        }
-       fstat(nfd, &stb);
-       if ((stb.st_mode & S_IFMT) != S_IFBLK) {
-               pr_err("%s is not a block device!\n", newdev);
+       if (!fstat_is_blkdev(nfd, newdev, &rdev)) {
                close(nfd);
                free(st);
                return 1;
@@ -198,8 +196,8 @@ int Grow_Add_device(char *devname, int fd, char *newdev)
         */
 
        info.disk.number = d;
-       info.disk.major = major(stb.st_rdev);
-       info.disk.minor = minor(stb.st_rdev);
+       info.disk.major = major(rdev);
+       info.disk.minor = minor(rdev);
        info.disk.raid_disk = d;
        info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
        st->ss->update_super(st, &info, "linear-grow-new", newdev,
@@ -288,16 +286,9 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
        struct supertype *st;
        char *subarray = NULL;
        int major = BITMAP_MAJOR_HI;
-       int vers = md_get_version(fd);
        unsigned long long bitmapsize, array_size;
        struct mdinfo *mdi;
 
-       if (vers < 9003) {
-               major = BITMAP_MAJOR_HOSTENDIAN;
-               pr_err("Warning - bitmaps created on this kernel are not portable\n"
-                       "  between different architectures.  Consider upgrading the Linux kernel.\n");
-       }
-
        /*
         * We only ever get called if s->bitmap_file is != NULL, so this check
         * is just here to quiet down static code checkers.
@@ -335,7 +326,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
        if (array.state & (1 << MD_SB_BITMAP_PRESENT)) {
                if (strcmp(s->bitmap_file, "none")==0) {
                        array.state &= ~(1 << MD_SB_BITMAP_PRESENT);
-                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                       if (md_set_array_info(fd, &array) != 0) {
                                if (array.state & (1 << MD_SB_CLUSTERED))
                                        pr_err("failed to remove clustered bitmap.\n");
                                else
@@ -455,7 +446,10 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                }
                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);
@@ -463,7 +457,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        if (strcmp(s->bitmap_file, "clustered") == 0)
                                array.state |= (1 << MD_SB_CLUSTERED);
                        array.state |= (1 << MD_SB_BITMAP_PRESENT);
-                       rv = ioctl(fd, SET_ARRAY_INFO, &array);
+                       rv = md_set_array_info(fd, &array);
                }
                if (rv < 0) {
                        if (errno == EBUSY)
@@ -819,8 +813,8 @@ static void unfreeze(struct supertype *st)
                char buf[20];
 
                if (sra &&
-                   sysfs_get_str(sra, NULL, "sync_action", buf, 20) > 0
-                   && strcmp(buf, "frozen\n") == 0)
+                   sysfs_get_str(sra, NULL, "sync_action", buf, 20) > 0 &&
+                   strcmp(buf, "frozen\n") == 0)
                        sysfs_set_str(sra, NULL, "sync_action", "idle");
                sysfs_free(sra);
        }
@@ -1823,7 +1817,7 @@ int Grow_reshape(char *devname, int fd,
            (array.state & (1<<MD_SB_BITMAP_PRESENT)) &&
            !(array.state & (1<<MD_SB_CLUSTERED))) {
                 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
-                if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
+                if (md_set_array_info(fd, &array)!= 0) {
                         pr_err("failed to remove internal bitmap.\n");
                         return 1;
                 }
@@ -1834,7 +1828,7 @@ int Grow_reshape(char *devname, int fd,
         * pre-requisite spare devices (mdmon owns final validation)
         */
        if (st->ss->external) {
-               int rv;
+               int retval;
 
                if (subarray) {
                        container = st->container_devnm;
@@ -1852,9 +1846,9 @@ int Grow_reshape(char *devname, int fd,
                        return 1;
                }
 
-               rv = st->ss->load_container(st, cfd, NULL);
+               retval = st->ss->load_container(st, cfd, NULL);
 
-               if (rv) {
+               if (retval) {
                        pr_err("Cannot read superblock for %s\n",
                                devname);
                        free(subarray);
@@ -1982,15 +1976,7 @@ int Grow_reshape(char *devname, int fd,
                 * understands '0' to mean 'max'.
                 */
                min_csize = 0;
-               rv = 0;
                for (mdi = sra->devs; mdi; mdi = mdi->next) {
-                       if (sysfs_set_num(sra, mdi, "size",
-                                         s->size == MAX_SIZE ? 0 : s->size) < 0) {
-                               /* Probably kernel refusing to let us
-                                * reduce the size - not an error.
-                                */
-                               break;
-                       }
                        if (array.not_persistent == 0 &&
                            array.major_version == 0 &&
                            get_linux_version() < 3001000) {
@@ -2005,10 +1991,6 @@ int Grow_reshape(char *devname, int fd,
                                }
                        }
                }
-               if (rv) {
-                       pr_err("Cannot set size on array members.\n");
-                       goto size_change_error;
-               }
                if (min_csize && s->size > min_csize) {
                        pr_err("Cannot safely make this array use more than 2TB per device on this kernel.\n");
                        rv = 1;
@@ -2056,7 +2038,7 @@ int Grow_reshape(char *devname, int fd,
                        else
                                rv = -1;
                } else {
-                       rv = ioctl(fd, SET_ARRAY_INFO, &array);
+                       rv = md_set_array_info(fd, &array);
 
                        /* manage array size when it is managed externally
                         */
@@ -2161,7 +2143,11 @@ size_change_error:
 
        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;
@@ -2272,7 +2258,7 @@ size_change_error:
                                goto release;
                        }
                        array.layout = info.new_layout;
-                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                       if (md_set_array_info(fd, &array) != 0) {
                                pr_err("failed to set new layout\n");
                                rv = 1;
                        } else if (c->verbose >= 0)
@@ -2836,8 +2822,7 @@ static int impose_reshape(struct mdinfo *sra,
            st->ss->external == 0) {
                /* use SET_ARRAY_INFO but only if reshape hasn't started */
                array.raid_disks = reshape->after.data_disks + reshape->parity;
-               if (!restart &&
-                   ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+               if (!restart && md_set_array_info(fd, &array) != 0) {
                        int err = errno;
 
                        pr_err("Cannot set device shape for %s: %s\n",
@@ -2883,7 +2868,11 @@ static int impose_level(int fd, int level, char *devname, int verbose)
        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 &&
@@ -2913,8 +2902,8 @@ static int impose_level(int fd, int level, char *devname, int verbose)
                        if (disk.major == 0 && disk.minor == 0)
                                continue;
                        found++;
-                       if ((disk.state & (1 << MD_DISK_ACTIVE))
-                           && disk.raid_disk < data_disks)
+                       if ((disk.state & (1 << MD_DISK_ACTIVE)) &&
+                           disk.raid_disk < data_disks)
                                /* keep this */
                                continue;
                        ioctl(fd, HOT_REMOVE_DISK,
@@ -2932,8 +2921,8 @@ static int impose_level(int fd, int level, char *devname, int verbose)
                        if (disk.major == 0 && disk.minor == 0)
                                continue;
                        found++;
-                       if ((disk.state & (1 << MD_DISK_ACTIVE))
-                           && disk.raid_disk < data_disks)
+                       if ((disk.state & (1 << MD_DISK_ACTIVE)) &&
+                           disk.raid_disk < data_disks)
                                /* keep this */
                                continue;
                        ioctl(fd, SET_DISK_FAULTY,
@@ -3191,7 +3180,12 @@ static int reshape_array(char *container, int fd, char *devname,
                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.
@@ -3239,7 +3233,7 @@ static int reshape_array(char *container, int fd, char *devname,
                if (info->new_layout != UnSet &&
                    info->new_layout != array.layout) {
                        array.layout = info->new_layout;
-                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                       if (md_set_array_info(fd, &array) != 0) {
                                pr_err("failed to set new layout\n");
                                goto release;
                        } else if (verbose >= 0)
@@ -3250,7 +3244,7 @@ static int reshape_array(char *container, int fd, char *devname,
                    info->delta_disks != 0 &&
                    array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
                        array.raid_disks += info->delta_disks;
-                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                       if (md_set_array_info(fd, &array) != 0) {
                                pr_err("failed to set raid disks\n");
                                goto release;
                        } else if (verbose >= 0) {
@@ -3603,9 +3597,8 @@ started:
        }
 
        if (!st->ss->external &&
-           !(reshape.before.data_disks != reshape.after.data_disks
-             && info->custom_array_size) &&
-           info->new_level == reshape.level &&
+           !(reshape.before.data_disks != reshape.after.data_disks &&
+             info->custom_array_size) && info->new_level == reshape.level &&
            !forked) {
                /* no need to wait for the reshape to finish as
                 * there is nothing more to do.
@@ -3790,7 +3783,12 @@ int reshape_container(char *container, char *devname,
                }
                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);
@@ -3997,8 +3995,8 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
         * a backup.
         */
        if (advancing) {
-               if ((need_backup > info->reshape_progress
-                    || info->array.major_version < 0) &&
+               if ((need_backup > info->reshape_progress ||
+                    info->array.major_version < 0) &&
                    *suspend_point < info->reshape_progress + target) {
                        if (need_backup < *suspend_point + 2 * target)
                                *suspend_point = need_backup;
@@ -4093,14 +4091,14 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
                 * before setting 'sync_action' to 'idle'.
                 * So we need these extra tests.
                 */
-               if (completed == 0 && advancing
-                   && strncmp(action, "idle", 4) == 0
-                   && info->reshape_progress > 0)
+               if (completed == 0 && advancing &&
+                   strncmp(action, "idle", 4) == 0 &&
+                   info->reshape_progress > 0)
                        break;
-               if (completed == 0 && !advancing
-                   && strncmp(action, "idle", 4) == 0
-                   && info->reshape_progress < (info->component_size
-                                                * reshape->after.data_disks))
+               if (completed == 0 && !advancing &&
+                   strncmp(action, "idle", 4) == 0 &&
+                   info->reshape_progress < (info->component_size
+                                             * reshape->after.data_disks))
                        break;
                sysfs_wait(fd, NULL);
                if (sysfs_fd_get_ll(fd, &completed) < 0)
@@ -4151,8 +4149,9 @@ check_progress:
         * it was just a device failure that leaves us degraded but
         * functioning.
         */
-       if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
-           || strncmp(buf, "none", 4) != 0) {
+       if (sysfs_get_str(info, NULL, "reshape_position", buf,
+                         sizeof(buf)) < 0 ||
+           strncmp(buf, "none", 4) != 0) {
                /* The abort might only be temporary.  Wait up to 10
                 * seconds for fd to contain a valid number again.
                 */
@@ -4184,9 +4183,10 @@ check_progress:
                /* Maybe racing with array shutdown - check state */
                if (fd >= 0)
                        close(fd);
-               if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
-                   || strncmp(buf, "inactive", 8) == 0
-                   || strncmp(buf, "clear",5) == 0)
+               if (sysfs_get_str(info, NULL, "array_state", buf,
+                                 sizeof(buf)) < 0 ||
+                   strncmp(buf, "inactive", 8) == 0 ||
+                   strncmp(buf, "clear",5) == 0)
                        return -2; /* abort */
                return -1; /* complete */
        }
@@ -4788,8 +4788,7 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                                /* reshape_progress is increasing */
                                if ((__le64_to_cpu(bsb.arraystart)
                                     + __le64_to_cpu(bsb.length)
-                                    < info->reshape_progress)
-                                   &&
+                                    < info->reshape_progress) &&
                                    (__le64_to_cpu(bsb.arraystart2)
                                     + __le64_to_cpu(bsb.length2)
                                     < info->reshape_progress))
@@ -5001,6 +5000,7 @@ int Grow_continue_command(char *devname, int fd,
                        goto Grow_continue_command_exit;
                }
                content = &array;
+               sysfs_init(content, fd, NULL);
                /* Need to load a superblock.
                 * FIXME we should really get what we need from
                 * sysfs
@@ -5073,7 +5073,7 @@ int Grow_continue_command(char *devname, int fd,
 
                cc = st->ss->container_content(st, subarray);
                for (content = cc; content ; content = content->next) {
-                       char *array;
+                       char *array_name;
                        int allow_reshape = 1;
 
                        if (content->reshape_active == 0)
@@ -5098,8 +5098,8 @@ int Grow_continue_command(char *devname, int fd,
                                goto Grow_continue_command_exit;
                        }
 
-                       array = strchr(content->text_version+1, '/')+1;
-                       mdstat = mdstat_by_subdev(array, container);
+                       array_name = strchr(content->text_version+1, '/')+1;
+                       mdstat = mdstat_by_subdev(array_name, container);
                        if (!mdstat)
                                continue;
                        if (mdstat->active == 0) {
@@ -5123,7 +5123,13 @@ int Grow_continue_command(char *devname, int fd,
                        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);