]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
sysfs: Avoid if and return on the same line
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
old mode 100644 (file)
new mode 100755 (executable)
index f2cf46a..628f0e7
--- a/Grow.c
+++ b/Grow.c
@@ -297,6 +297,16 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        "  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.
+        */
+       if (!s->bitmap_file)
+               return 1;
+
+       if (strcmp(s->bitmap_file, "clustered") == 0)
+               major = BITMAP_MAJOR_CLUSTERED;
+
        if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
                if (errno == ENOMEM)
                        pr_err("Memory allocation failure.\n");
@@ -305,8 +315,8 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                return 1;
        }
        if (bmf.pathname[0]) {
-               if (strcmp(s->bitmap_file,"none")==0) {
-                       if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
+               if (strcmp(s->bitmap_file,"none") == 0) {
+                       if (ioctl(fd, SET_BITMAP_FILE, -1) != 0) {
                                pr_err("failed to remove bitmap %s\n",
                                        bmf.pathname);
                                return 1;
@@ -321,17 +331,19 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                pr_err("cannot get array status for %s\n", devname);
                return 1;
        }
-       if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
+       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) {
-                               pr_err("failed to remove internal bitmap.\n");
+                       array.state &= ~(1 << MD_SB_BITMAP_PRESENT);
+                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                               if (array.state & (1 << MD_SB_CLUSTERED))
+                                       pr_err("failed to remove clustered bitmap.\n");
+                               else
+                                       pr_err("failed to remove internal bitmap.\n");
                                return 1;
                        }
                        return 0;
                }
-               pr_err("Internal bitmap already present on %s\n",
-                       devname);
+               pr_err("bitmap already present on %s\n", devname);
                return 1;
        }
 
@@ -347,7 +359,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
        bitmapsize = array.size;
        bitmapsize <<= 1;
        if (get_dev_size(fd, NULL, &array_size) &&
-           array_size > (0x7fffffffULL<<9)) {
+           array_size > (0x7fffffffULL << 9)) {
                /* Array is big enough that we cannot trust array.size
                 * try other approaches
                 */
@@ -359,7 +371,9 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
        }
 
        if (array.level == 10) {
-               int ncopies = (array.layout&255)*((array.layout>>8)&255);
+               int ncopies;
+
+               ncopies = (array.layout & 255) * ((array.layout >> 8) & 255);
                bitmapsize = bitmapsize * array.raid_disks / ncopies;
        }
 
@@ -375,7 +389,8 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                free(st);
                return 1;
        }
-       if (strcmp(s->bitmap_file, "internal") == 0) {
+       if (strcmp(s->bitmap_file, "internal") == 0 ||
+           strcmp(s->bitmap_file, "clustered") == 0) {
                int rv;
                int d;
                int offset_setable = 0;
@@ -384,41 +399,47 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        pr_err("Internal bitmaps not supported with %s metadata\n", st->ss->name);
                        return 1;
                }
+               st->nodes = c->nodes;
+               st->cluster_name = c->homecluster;
                mdi = sysfs_read(fd, NULL, GET_BITMAP_LOCATION);
                if (mdi)
                        offset_setable = 1;
-               for (d=0; d< st->max_devs; d++) {
+               for (d = 0; d < st->max_devs; d++) {
                        mdu_disk_info_t disk;
                        char *dv;
+                       int fd2;
+
                        disk.number = d;
                        if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
                                continue;
-                       if (disk.major == 0 &&
-                           disk.minor == 0)
+                       if (disk.major == 0 && disk.minor == 0)
                                continue;
-                       if ((disk.state & (1<<MD_DISK_SYNC))==0)
+                       if ((disk.state & (1 << MD_DISK_SYNC)) == 0)
                                continue;
                        dv = map_dev(disk.major, disk.minor, 1);
-                       if (dv) {
-                               int fd2 = dev_open(dv, O_RDWR);
-                               if (fd2 < 0)
-                                       continue;
-                               if (st->ss->load_super(st, fd2, NULL)==0) {
-                                       if (st->ss->add_internal_bitmap(
-                                                   st,
-                                                   &s->bitmap_chunk, c->delay, s->write_behind,
-                                                   bitmapsize, offset_setable,
-                                                   major)
-                                               )
-                                               st->ss->write_bitmap(st, fd2);
-                                       else {
-                                               pr_err("failed to create internal bitmap - chunksize problem.\n");
-                                               close(fd2);
-                                               return 1;
-                                       }
+                       if (!dv)
+                               continue;
+                       fd2 = dev_open(dv, O_RDWR);
+                       if (fd2 < 0)
+                               continue;
+                       rv = st->ss->load_super(st, fd2, NULL);
+                       if (!rv) {
+                               rv = st->ss->add_internal_bitmap(
+                                       st, &s->bitmap_chunk, c->delay,
+                                       s->write_behind, bitmapsize,
+                                       offset_setable, major);
+                               if (!rv) {
+                                       st->ss->write_bitmap(st, fd2,
+                                                            NodeNumUpdate);
+                               } else {
+                                       pr_err("failed to create internal bitmap - chunksize problem.\n");
                                }
-                               close(fd2);
+                       } else {
+                               pr_err("failed to load super-block.\n");
                        }
+                       close(fd2);
+                       if (rv)
+                               return 1;
                }
                if (offset_setable) {
                        st->ss->getinfo_super(st, mdi, NULL);
@@ -426,7 +447,9 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
                                                  mdi->bitmap_offset);
                } else {
-                       array.state |= (1<<MD_SB_BITMAP_PRESENT);
+                       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);
                }
                if (rv < 0) {
@@ -449,8 +472,8 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        disk.number = d;
                        if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
                                continue;
-                       if ((disk.major==0 && disk.minor==0) ||
-                           (disk.state & (1<<MD_DISK_REMOVED)))
+                       if ((disk.major==0 && disk.minor == 0) ||
+                           (disk.state & (1 << MD_DISK_REMOVED)))
                                continue;
                        dv = map_dev(disk.major, disk.minor, 1);
                        if (!dv)
@@ -469,14 +492,14 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        pr_err("cannot find UUID for array!\n");
                        return 1;
                }
-               if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
-                                c->delay, s->write_behind, bitmapsize, major)) {
+               if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid,
+                                s->bitmap_chunk, c->delay, s->write_behind,
+                                bitmapsize, major)) {
                        return 1;
                }
                bitmap_fd = open(s->bitmap_file, O_RDWR);
                if (bitmap_fd < 0) {
-                       pr_err("weird: %s cannot be opened\n",
-                               s->bitmap_file);
+                       pr_err("weird: %s cannot be opened\n", s->bitmap_file);
                        return 1;
                }
                if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
@@ -612,10 +635,8 @@ static void unfreeze(struct supertype *st)
 
                if (sra &&
                    sysfs_get_str(sra, NULL, "sync_action", buf, 20) > 0
-                   && strcmp(buf, "frozen\n") == 0) {
-                       printf("unfreeze\n");
+                   && strcmp(buf, "frozen\n") == 0)
                        sysfs_set_str(sra, NULL, "sync_action", "idle");
-               }
                sysfs_free(sra);
        }
 }
@@ -732,6 +753,14 @@ int start_reshape(struct mdinfo *sra, int already_running,
 void abort_reshape(struct mdinfo *sra)
 {
        sysfs_set_str(sra, NULL, "sync_action", "idle");
+       /*
+        * Prior to kernel commit: 23ddff3792f6 ("md: allow suspend_lo and
+        * suspend_hi to decrease as well as increase.")
+        * you could only increase suspend_{lo,hi} unless the region they
+        * covered was empty.  So to reset to 0, you need to push suspend_lo
+        * up past suspend_hi first.  So to maximize the chance of mdadm
+        * working on all kernels, we want to keep doing that.
+        */
        sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
        sysfs_set_num(sra, NULL, "suspend_hi", 0);
        sysfs_set_num(sra, NULL, "suspend_lo", 0);
@@ -850,7 +879,8 @@ int reshape_prepare_fdlist(char *devname,
        for (sd = sra->devs; sd; sd = sd->next) {
                if (sd->disk.state & (1<<MD_DISK_FAULTY))
                        continue;
-               if (sd->disk.state & (1<<MD_DISK_SYNC)) {
+               if (sd->disk.state & (1<<MD_DISK_SYNC) &&
+                   sd->disk.raid_disk < raid_disks) {
                        char *dn = map_dev(sd->disk.major,
                                           sd->disk.minor, 1);
                        fdlist[sd->disk.raid_disk]
@@ -1053,6 +1083,9 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
                        re->level = 1;
                        return NULL;
                }
+               if (info->array.raid_disks != 2 &&
+                   info->new_level == 5)
+                       return "Can only convert a 2-device array to RAID5";
                if (info->array.raid_disks == 2 &&
                    info->new_level == 5) {
 
@@ -1581,6 +1614,15 @@ int Grow_reshape(char *devname, int fd,
                pr_err("Cannot increase raid-disks on this array beyond %d\n", st->max_devs);
                return 1;
        }
+       if (s->level == 0 &&
+           (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) {
+                        pr_err("failed to remove internal bitmap.\n");
+                        return 1;
+                }
+        }
 
        /* in the external case we need to check that the requested reshape is
         * supported, and perform an initial check that the container holds the
@@ -1776,8 +1818,8 @@ int Grow_reshape(char *devname, int fd,
                                                   "raid5");
                                if (!rv) {
                                        raid0_takeover = 1;
-                                       /* get array parametes after takeover
-                                        * to chane one parameter at time only
+                                       /* get array parameters after takeover
+                                        * to change one parameter at time only
                                         */
                                        rv = ioctl(fd, GET_ARRAY_INFO, &array);
                                }
@@ -2037,6 +2079,10 @@ size_change_error:
                 * number of devices (On-Line Capacity Expansion) must be
                 * performed at the level of the container
                 */
+               if (fd > 0) {
+                       close(fd);
+                       fd = -1;
+               }
                rv = reshape_container(container, devname, -1, st, &info,
                                       c->force, c->backup_file, c->verbose, 0, 0, 0);
                frozen = 0;
@@ -2539,7 +2585,7 @@ static void update_cache_size(char *container, struct mdinfo *sra,
        /* make sure there is room for 'blocks' with a bit to spare */
        if (cache < 16 + blocks / disks)
                cache = 16 + blocks / disks;
-       cache /= (4096/512); /* Covert from sectors to pages */
+       cache /= (4096/512); /* Convert from sectors to pages */
 
        if (sra->cache_size < cache)
                subarray_set_num(container, sra, "stripe_cache_size",
@@ -2969,9 +3015,11 @@ static int reshape_array(char *container, int fd, char *devname,
         * array.  Now that the array has been changed to the right
         * level and frozen, we can safely add them.
         */
-       if (devlist)
-               Manage_subdevs(devname, fd, devlist, verbose,
-                              0,NULL, 0);
+       if (devlist) {
+               if (Manage_subdevs(devname, fd, devlist, verbose,
+                                  0, NULL, 0))
+                       goto release;
+       }
 
        if (reshape.backup_blocks == 0 && data_offset != INVALID_SECTORS)
                reshape.backup_blocks = reshape.before.data_disks * info->array.chunk_size/512;
@@ -3184,7 +3232,7 @@ started:
        d = reshape_prepare_fdlist(devname, sra, odisks,
                                   nrdisks, blocks, backup_file,
                                   fdlist, offsets);
-       if (d < 0) {
+       if (d < odisks) {
                goto release;
        }
        if ((st->ss->manage_reshape == NULL) ||
@@ -3196,7 +3244,7 @@ started:
                                       devname);
                                pr_err(" Please provide one with \"--backup=...\"\n");
                                goto release;
-                       } else if (sra->array.spare_disks == 0) {
+                       } else if (d == odisks) {
                                pr_err("%s: Cannot grow - need a spare or backup-file to backup critical section\n", devname);
                                goto release;
                        }
@@ -3485,7 +3533,7 @@ int reshape_container(char *container, char *devname,
                int fd;
                struct mdstat_ent *mdstat;
                char *adev;
-               int devid;
+               dev_t devid;
 
                sysfs_free(cc);
 
@@ -3858,27 +3906,30 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
        }
        /* Some kernels reset 'sync_completed' to zero,
         * we need to have real point we are in md.
-        * But only if array is actually still reshaping,
-        * not stopped.
+        * So in that case, read 'reshape_position' from sysfs.
         */
        if (completed == 0) {
+               unsigned long long reshapep;
                char action[20];
                if (sysfs_get_str(info, NULL, "sync_action",
                                  action, 20) > 0 &&
-                   strncmp(action, "idle", 4) == 0)
-                       completed = max_progress;
-       }
-
-       /* some kernels can give an incorrectly high 'completed' number */
-       completed /= (info->new_chunk/512);
-       completed *= (info->new_chunk/512);
-       /* Convert 'completed' back in to a 'progress' number */
-       completed *= reshape->after.data_disks;
-       if (!advancing) {
-               completed = info->component_size * reshape->after.data_disks
-                       - completed;
+                   strncmp(action, "idle", 4) == 0 &&
+                   sysfs_get_ll(info, NULL,
+                                "reshape_position", &reshapep) == 0)
+                       *reshape_completed = reshapep;
+       } else {
+               /* some kernels can give an incorrectly high
+                * 'completed' number, so round down */
+               completed /= (info->new_chunk/512);
+               completed *= (info->new_chunk/512);
+               /* Convert 'completed' back in to a 'progress' number */
+               completed *= reshape->after.data_disks;
+               if (!advancing)
+                       completed = (info->component_size
+                                    * reshape->after.data_disks
+                                    - completed);
+               *reshape_completed = completed;
        }
-       *reshape_completed = completed;
 
        close(fd);
 
@@ -3898,7 +3949,6 @@ check_progress:
         * it was just a device failure that leaves us degraded but
         * functioning.
         */
-       strcpy(buf, "hi");
        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
@@ -4495,8 +4545,8 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                 * sometimes they aren't... So allow considerable flexability in matching, and allow
                 * this test to be overridden by an environment variable.
                 */
-               if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
-                   info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
+               if(time_after(info->array.utime, (unsigned int)__le64_to_cpu(bsb.mtime) + 2*60*60) ||
+                  time_before(info->array.utime, (unsigned int)__le64_to_cpu(bsb.mtime) - 10*60)) {
                        if (check_env("MDADM_GROW_ALLOW_OLD")) {
                                pr_err("accepting backup with timestamp %lu for array with timestamp %lu\n",
                                        (unsigned long)__le64_to_cpu(bsb.mtime),
@@ -4724,7 +4774,7 @@ int Grow_continue_command(char *devname, int fd,
        struct mdinfo *cc = NULL;
        struct mdstat_ent *mdstat = NULL;
        int cfd = -1;
-       int fd2 = -1;
+       int fd2;
 
        dprintf("Grow continue from command line called for %s\n",
                devname);
@@ -4738,6 +4788,7 @@ int Grow_continue_command(char *devname, int fd,
        dprintf("Grow continue is run for ");
        if (st->ss->external == 0) {
                int d;
+               int cnt = 5;
                dprintf_cont("native array (%s)\n", devname);
                if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
                        pr_err("%s is not an active md array - aborting\n", devname);
@@ -4749,38 +4800,42 @@ int Grow_continue_command(char *devname, int fd,
                 * FIXME we should really get what we need from
                 * sysfs
                 */
-               for (d = 0; d < MAX_DISKS; d++) {
-                       mdu_disk_info_t disk;
-                       char *dv;
-                       int err;
-                       disk.number = d;
-                       if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
-                               continue;
-                       if (disk.major == 0 && disk.minor == 0)
-                               continue;
-                       if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
-                               continue;
-                       dv = map_dev(disk.major, disk.minor, 1);
-                       if (!dv)
-                               continue;
-                       fd2 = dev_open(dv, O_RDONLY);
-                       if (fd2 < 0)
-                               continue;
-                       err = st->ss->load_super(st, fd2, NULL);
-                       close(fd2);
-                       /* invalidate fd2 to avoid possible double close() */
-                       fd2 = -1;
-                       if (err)
-                               continue;
-                       break;
-               }
-               if (d == MAX_DISKS) {
-                       pr_err("Unable to load metadata for %s\n",
-                              devname);
-                       ret_val = 1;
-                       goto Grow_continue_command_exit;
-               }
-               st->ss->getinfo_super(st, content, NULL);
+               do {
+                       for (d = 0; d < MAX_DISKS; d++) {
+                               mdu_disk_info_t disk;
+                               char *dv;
+                               int err;
+                               disk.number = d;
+                               if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
+                                       continue;
+                               if (disk.major == 0 && disk.minor == 0)
+                                       continue;
+                               if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
+                                       continue;
+                               dv = map_dev(disk.major, disk.minor, 1);
+                               if (!dv)
+                                       continue;
+                               fd2 = dev_open(dv, O_RDONLY);
+                               if (fd2 < 0)
+                                       continue;
+                               err = st->ss->load_super(st, fd2, NULL);
+                               close(fd2);
+                               if (err)
+                                       continue;
+                               break;
+                       }
+                       if (d == MAX_DISKS) {
+                               pr_err("Unable to load metadata for %s\n",
+                                      devname);
+                               ret_val = 1;
+                               goto Grow_continue_command_exit;
+                       }
+                       st->ss->getinfo_super(st, content, NULL);
+                       if (!content->reshape_active)
+                               sleep(3);
+                       else
+                               break;
+               } while (cnt-- > 0);
        } else {
                char *container;
 
@@ -4865,6 +4920,8 @@ int Grow_continue_command(char *devname, int fd,
 
                sysfs_init(content, fd2, mdstat->devnm);
 
+               close(fd2);
+
                /* start mdmon in case it is not running
                 */
                if (!mdmon_running(container))
@@ -4893,8 +4950,6 @@ int Grow_continue_command(char *devname, int fd,
        ret_val = Grow_continue(fd, st, content, backup_file, 1, 0);
 
 Grow_continue_command_exit:
-       if (fd2 > -1)
-               close(fd2);
        if (cfd > -1)
                close(cfd);
        st->ss->free_super(st);