]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
super_by_fd: return subarray info explicitly.
[thirdparty/mdadm.git] / Manage.c
index f88d95e2f91a00762ec8e3e09f9cbf094885ee67..1ce4aa63521c7935cab8182c6d023110b12d49bd 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -118,9 +118,11 @@ int Manage_ro(char *devname, int fd, int readonly)
 
 static void remove_devices(int devnum, char *path)
 {
-       /* Remove all 'standard' devices for 'devnum', including
-        * partitions.  Also remove names at 'path' - possibly with
-        * partition suffixes - which link to those names.
+       /* 
+        * Remove names at 'path' - possibly with
+        * partition suffixes - which link to the 'standard'
+        * name for devnum.  These were probably created
+        * by mdadm when the array was assembled.
         */
        char base[40];
        char *path2;
@@ -130,36 +132,32 @@ static void remove_devices(int devnum, char *path)
        char *be;
        char *pe;
 
+       if (!path)
+               return;
+
        if (devnum >= 0)
                sprintf(base, "/dev/md%d", devnum);
        else
                sprintf(base, "/dev/md_d%d", -1-devnum);
        be = base + strlen(base);
-       if (path) {
-               path2 = malloc(strlen(path)+20);
-               strcpy(path2, path);
-               pe = path2 + strlen(path2);
-       } else
-               path2 = path = NULL;
+
+       path2 = malloc(strlen(path)+20);
+       strcpy(path2, path);
+       pe = path2 + strlen(path2);
        
        for (part = 0; part < 16; part++) {
                if (part) {
                        sprintf(be, "p%d", part);
-                       if (path) {
-                               if (isdigit(pe[-1]))
-                                       sprintf(pe, "p%d", part);
-                               else
-                                       sprintf(pe, "%d", part);
-                       }
-               }
-               /* FIXME test if really is md device ?? */
-               unlink(base);
-               if (path) {
-                       n = readlink(path2, link, sizeof(link));
-                       if (n && strlen(base) == n &&
-                           strncmp(link, base, n) == 0)
-                               unlink(path2);
+
+                       if (isdigit(pe[-1]))
+                               sprintf(pe, "p%d", part);
+                       else
+                               sprintf(pe, "%d", part);
                }
+               n = readlink(path2, link, sizeof(link));
+               if (n && (int)strlen(base) == n &&
+                   strncmp(link, base, n) == 0)
+                       unlink(path2);
        }
        free(path2);
 }
@@ -351,6 +349,7 @@ int Manage_subdevs(char *devname, int fd,
        int j, jnext = 0;
        int tfd = -1;
        struct supertype *st, *tst;
+       char *subarray = NULL;
        int duuid[4];
        int ouuid[4];
        int lfd = -1;
@@ -371,7 +370,7 @@ int Manage_subdevs(char *devname, int fd,
        if (array_size <= 0)
                array_size = array.size * 2;
 
-       tst = super_by_fd(fd);
+       tst = super_by_fd(fd, &subarray);
        if (!tst) {
                fprintf(stderr, Name ": unsupport array - version %d.%d\n",
                        array.major_version, array.minor_version);
@@ -385,6 +384,7 @@ int Manage_subdevs(char *devname, int fd,
                char *dnprintable = dv->devname;
                char *add_dev = dv->devname;
                int err;
+               int re_add_failed = 0;
 
                next = dv->next;
                jnext = 0;
@@ -398,7 +398,7 @@ int Manage_subdevs(char *devname, int fd,
                                return 1;
                        }
                        for (; j < array.raid_disks + array.nr_disks ; j++) {
-                               int dev;
+                               unsigned dev;
                                disc.number = j;
                                if (ioctl(fd, GET_DISK_INFO, &disc))
                                        continue;
@@ -430,7 +430,7 @@ int Manage_subdevs(char *devname, int fd,
                        }
                        for (; j < array.raid_disks + array.nr_disks; j++) {
                                int sfd;
-                               int dev;
+                               unsigned dev;
                                disc.number = j;
                                if (ioctl(fd, GET_DISK_INFO, &disc))
                                        continue;
@@ -480,6 +480,7 @@ int Manage_subdevs(char *devname, int fd,
                        if (stat(add_dev, &stb) < 0)
                                continue;
                } else if (strchr(dv->devname, '/') == NULL &&
+                          strchr(dv->devname, ':') == NULL &&
                           strlen(dv->devname) < 50) {
                        /* Assume this is a kernel-internal name like 'sda1' */
                        int found = 0;
@@ -548,7 +549,7 @@ int Manage_subdevs(char *devname, int fd,
                        return 1;
                case 'a':
                        /* add the device */
-                       if (tst->subarray[0]) {
+                       if (subarray) {
                                fprintf(stderr, Name ": Cannot add disks to a"
                                        " \'member\' array, perform this"
                                        " operation on the parent container\n");
@@ -663,14 +664,20 @@ int Manage_subdevs(char *devname, int fd,
                                    get_linux_version() <= 2006018)
                                        ;
                                else if (st->sb) {
+                                       struct mdinfo mdi;
+                                       st->ss->getinfo_super(st, &mdi, NULL);
                                        st->ss->uuid_from_super(st, ouuid);
-                                       if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
-                                               /* looks close enough for now.  Kernel
-                                                * will worry about whether a bitmap
-                                                * based reconstruction is possible.
+                                       if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
+                                           !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
+                                           memcmp(duuid, ouuid, sizeof(ouuid))==0) {
+                                               /* look like it is worth a try.  Need to
+                                                * make sure kernel will accept it though.
                                                 */
-                                               struct mdinfo mdi;
-                                               st->ss->getinfo_super(st, &mdi);
+                                               disc.number = mdi.disk.number;
+                                               if (ioctl(fd, GET_DISK_INFO, &disc) != 0
+                                                   || disc.major != 0 || disc.minor != 0
+                                                   || !enough_fd(fd))
+                                                       goto skip_re_add;
                                                disc.major = major(stb.st_rdev);
                                                disc.minor = minor(stb.st_rdev);
                                                disc.number = mdi.disk.number;
@@ -685,8 +692,7 @@ int Manage_subdevs(char *devname, int fd,
                                                tfd = -1;
                                                /* don't even try if disk is marked as faulty */
                                                errno = 0;
-                                               if ((disc.state & 1) == 0 &&
-                                                   ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
+                                               if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
                                                        if (verbose >= 0)
                                                                fprintf(stderr, Name ": re-added %s\n", add_dev);
                                                        count++;
@@ -699,7 +705,8 @@ int Manage_subdevs(char *devname, int fd,
                                                                continue;
                                                        return 1;
                                                }
-                                               /* fall back on normal-add */
+                                       skip_re_add:
+                                               re_add_failed = 1;
                                        }
                                }
                                if (add_dev != dv->devname) {
@@ -721,6 +728,17 @@ int Manage_subdevs(char *devname, int fd,
                                                dv->devname, devname);
                                        return 1;
                                }
+                               if (re_add_failed) {
+                                       fprintf(stderr, Name ": %s reports being an active member for %s, but a --re-add fails.\n",
+                                               dv->devname, devname);
+                                       fprintf(stderr, Name ": not performing --add as that would convert %s in to a spare.\n",
+                                               dv->devname);
+                                       fprintf(stderr, Name ": To make this a spare, use \"mdadm --zero-superblock %s\" first.\n",     
+                                               dv->devname);
+                                       if (tfd >= 0)
+                                               close(tfd);
+                                       return 1;
+                               }
                        } else {
                                /* non-persistent. Must ensure that new drive
                                 * is at least array.size big.
@@ -838,7 +856,7 @@ int Manage_subdevs(char *devname, int fd,
                                }
                                sra->array.level = LEVEL_CONTAINER;
                                /* Need to set data_offset and component_size */
-                               tst->ss->getinfo_super(tst, &new_mdi);
+                               tst->ss->getinfo_super(tst, &new_mdi, NULL);
                                new_mdi.disk.major = disc.major;
                                new_mdi.disk.minor = disc.minor;
                                new_mdi.recovery_start = 0;
@@ -862,7 +880,7 @@ int Manage_subdevs(char *devname, int fd,
 
                case 'r':
                        /* hot remove */
-                       if (tst->subarray[0]) {
+                       if (subarray) {
                                fprintf(stderr, Name ": Cannot remove disks from a"
                                        " \'member\' array, perform this"
                                        " operation on the parent container\n");
@@ -930,8 +948,8 @@ int Manage_subdevs(char *devname, int fd,
                                        if (sra)
                                                dv = sra->devs;
                                        for ( ; dv ; dv=dv->next)
-                                               if (dv->disk.major == major(stb.st_rdev) &&
-                                                   dv->disk.minor == minor(stb.st_rdev))
+                                               if (dv->disk.major == (int)major(stb.st_rdev) &&
+                                                   dv->disk.minor == (int)minor(stb.st_rdev))
                                                        break;
                                        if (dv)
                                                err = sysfs_set_str(sra, dv,
@@ -1020,16 +1038,8 @@ int Update_subarray(char *dev, char *subarray, char *update, mddev_ident_t ident
        int fd, rv = 2;
 
        memset(st, 0, sizeof(*st));
-       if (snprintf(st->subarray, sizeof(st->subarray), "%s", subarray) >=
-           sizeof(st->subarray)) {
-               if (!quiet)
-                       fprintf(stderr,
-                               Name ": Input overflow for subarray '%s' > %zu bytes\n",
-                               subarray, sizeof(st->subarray) - 1);
-               return 2;
-       }
 
-       fd = open_subarray(dev, st, quiet);
+       fd = open_subarray(dev, subarray, st, quiet);
        if (fd < 0)
                return 2;