]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
Grow: analyse_change add notification about only 2-device can be convert from RAID1...
[thirdparty/mdadm.git] / Manage.c
index 0c7246dde5b50970bfa95046653ac252ca527c73..e3d46f3b968ec28bc2cfef44e026a624f5ca7084 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -119,8 +119,7 @@ int Manage_ro(char *devname, int fd, int readonly)
        }
 out:
 #ifndef MDASSEMBLE
-       if (mdi)
-               sysfs_free(mdi);
+       sysfs_free(mdi);
 #endif
        return rv;
 }
@@ -493,14 +492,17 @@ done:
                rv = 1;
                goto out;
        }
-       /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
-        * was stopped, so We'll do it here just to be sure.  Drop any
-        * partitions as well...
-        */
-       if (fd >= 0)
-               ioctl(fd, BLKRRPART, 0);
-       if (mdi)
-               sysfs_uevent(mdi, "change");
+
+       if (get_linux_version() < 2006028) {
+               /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
+                * was stopped, so We'll do it here just to be sure.  Drop any
+                * partitions as well...
+                */
+               if (fd >= 0)
+                       ioctl(fd, BLKRRPART, 0);
+               if (mdi)
+                       sysfs_uevent(mdi, "change");
+       }
 
        if (devnm[0] && use_udev()) {
                struct map_ent *mp = map_by_devnm(&map, devnm);
@@ -513,8 +515,7 @@ done:
        map_remove(&map, devnm);
        map_unlock(&map);
 out:
-       if (mdi)
-               sysfs_free(mdi);
+       sysfs_free(mdi);
 
        return rv;
 }
@@ -669,6 +670,15 @@ int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
                disc.number = mdi.disk.number;
                disc.raid_disk = mdi.disk.raid_disk;
                disc.state = mdi.disk.state;
+               if (array->state & (1 << MD_SB_CLUSTERED)) {
+                       /* extra flags are needed when adding to a cluster as
+                        * there are two cases to distinguish
+                        */
+                       if (dv->disposition == 'c')
+                               disc.state |= (1 << MD_DISK_CANDIDATE);
+                       else
+                               disc.state |= (1 << MD_DISK_CLUSTER_ADD);
+               }
                if (dv->writemostly == 1)
                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
                if (dv->writemostly == 2)
@@ -816,7 +826,8 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                }
 
                /* Make sure device is large enough */
-               if (tst->sb &&
+               if (dv->disposition != 'j' &&  /* skip size check for Journal */
+                   tst->sb &&
                    tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
                    array_size) {
                        if (dv->disposition == 'M')
@@ -869,10 +880,10 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                                        continue;
                                if (disc.major == 0 && disc.minor == 0)
                                        continue;
-                               found++;
                                if (!(disc.state & (1<<MD_DISK_SYNC)))
                                        continue;
                                avail[disc.raid_disk] = 1;
+                               found++;
                        }
                        array_failed = !enough(array->level, array->raid_disks,
                                               array->layout, 1, avail);
@@ -920,8 +931,35 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
        else
                disc.number = raid_slot;
        disc.state = 0;
+
+       /* only add journal to array that supports journaling */
+       if (dv->disposition == 'j') {
+               struct mdinfo mdi;
+               struct mdinfo *mdp;
+
+               mdp = sysfs_read(fd, NULL, GET_ARRAY_STATE);
+               if (!mdp) {
+                       pr_err("%s unable to read array state.\n", devname);
+                       return -1;
+               }
+
+               if (strncmp(mdp->sysfs_array_state, "readonly", 8) != 0) {
+                       pr_err("%s is not readonly, cannot add journal.\n", devname);
+                       return -1;
+               }
+
+               tst->ss->getinfo_super(tst, &mdi, NULL);
+               if (mdi.journal_device_required == 0) {
+                       pr_err("%s does not support journal device.\n", devname);
+                       return -1;
+               }
+               disc.raid_disk = 0;
+       }
+
        if (array->not_persistent==0) {
                int dfd;
+               if (dv->disposition == 'j')
+                       disc.state |= (1 << MD_DISK_JOURNAL) | (1 << MD_DISK_SYNC);
                if (dv->writemostly == 1)
                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
                dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
@@ -1032,10 +1070,20 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
        } else {
                tst->ss->free_super(tst);
                if (ioctl(fd, ADD_NEW_DISK, &disc)) {
-                       pr_err("add new device failed for %s as %d: %s\n",
-                              dv->devname, j, strerror(errno));
+                       if (dv->disposition == 'j')
+                               pr_err("Failed to hot add %s as journal, "
+                                      "please try restart %s.\n", dv->devname, devname);
+                       else
+                               pr_err("add new device failed for %s as %d: %s\n",
+                                      dv->devname, j, strerror(errno));
                        return -1;
                }
+               if (dv->disposition == 'j') {
+                       pr_err("Journal added successfully, making %s read-write\n", devname);
+                       if (Manage_ro(devname, fd, -1))
+                               pr_err("Failed to make %s read-write\n", devname);
+               }
+
        }
        if (verbose >= 0)
                pr_err("added %s\n", dv->devname);
@@ -1118,8 +1166,7 @@ int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
                                                    "state", "remove");
                        else
                                err = -1;
-                       if (sra)
-                               sysfs_free(sra);
+                       sysfs_free(sra);
                }
        }
        if (err) {
@@ -1268,6 +1315,7 @@ int Manage_subdevs(char *devname, int fd,
         *         try HOT_ADD_DISK
         *         If that fails EINVAL, try ADD_NEW_DISK
         *  'S' - add the device as a spare - don't try re-add
+        *  'j' - add the device as a journal device
         *  'A' - re-add the device
         *  'r' - remove the device: HOT_REMOVE_DISK
         *        device can be 'faulty' or 'detached' in which case all
@@ -1300,6 +1348,7 @@ int Manage_subdevs(char *devname, int fd,
        int sysfd = -1;
        int count = 0; /* number of actions taken */
        struct mdinfo info;
+       struct mdinfo devinfo;
        int frozen = 0;
        int busy = 0;
        int raid_slot = -1;
@@ -1336,7 +1385,7 @@ int Manage_subdevs(char *devname, int fd,
                        rv = parse_cluster_confirm_arg(dv->devname,
                                                       &dv->devname,
                                                       &raid_slot);
-                       if (!rv) {
+                       if (rv) {
                                pr_err("Could not get the devname of cluster\n");
                                goto abort;
                        }
@@ -1459,9 +1508,10 @@ int Manage_subdevs(char *devname, int fd,
                } else {
                        struct stat stb;
                        tfd = dev_open(dv->devname, O_RDONLY);
-                       if (tfd >= 0)
+                       if (tfd >= 0) {
                                fstat(tfd, &stb);
-                       else {
+                               close(tfd);
+                       } else {
                                int open_err = errno;
                                if (stat(dv->devname, &stb) != 0) {
                                        pr_err("Cannot find %s: %s\n",
@@ -1499,6 +1549,7 @@ int Manage_subdevs(char *devname, int fd,
                        goto abort;
                case 'a':
                case 'S': /* --add-spare */
+               case 'j': /* --add-journal */
                case 'A':
                case 'M': /* --re-add missing */
                case 'F': /* --re-add faulty  */
@@ -1508,6 +1559,18 @@ int Manage_subdevs(char *devname, int fd,
                                pr_err("Cannot add disks to a \'member\' array, perform this operation on the parent container\n");
                                goto abort;
                        }
+
+                       /* Let's first try to write re-add to sysfs */
+                       if (rdev != 0 &&
+                           (dv->disposition == 'A' || dv->disposition == 'F')) {
+                               sysfs_init_dev(&devinfo, rdev);
+                               if (sysfs_set_str(&info, &devinfo, "state", "re-add") == 0) {
+                                       pr_err("re-add %s to %s succeed\n",
+                                               dv->devname, info.sys_name);
+                                       break;
+                               }
+                       }
+
                        if (dv->disposition == 'F')
                                /* Need to remove first */
                                ioctl(fd, HOT_REMOVE_DISK, rdev);