]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
tests/06name: adjust for homehost
[thirdparty/mdadm.git] / Manage.c
index 76447edacd2be231229cee95ff7d061d1af769dd..7855e21aa561b5320a08aa0ec5f23f8360aa236a 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -30,6 +30,7 @@
 #include "mdadm.h"
 #include "md_u.h"
 #include "md_p.h"
+#include <ctype.h>
 
 #define REGISTER_DEV           _IO (MD_MAJOR, 1)
 #define START_MD               _IO (MD_MAJOR, 2)
@@ -45,12 +46,15 @@ int Manage_ro(char *devname, int fd, int readonly)
         *
         */
        mdu_array_info_t array;
+#ifndef MDASSEMBLE
        struct mdinfo *mdi;
+#endif
 
        if (md_get_version(fd) < 9000) {
                fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
                return 1;
        }
+#ifndef MDASSEMBLE
        /* If this is an externally-manage array, we need to modify the
         * metadata_version so that mdmon doesn't undo our change.
         */
@@ -92,7 +96,7 @@ int Manage_ro(char *devname, int fd, int readonly)
                }
                return 0;
        }
-
+#endif
        if (ioctl(fd, GET_ARRAY_INFO, &array)) {
                fprintf(stderr, Name ": %s does not appear to be active.\n",
                        devname);
@@ -117,6 +121,52 @@ int Manage_ro(char *devname, int fd, int readonly)
 
 #ifndef MDASSEMBLE
 
+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.
+        */
+       char base[40];
+       char *path2;
+       char link[1024];
+       int n;
+       int part;
+       char *be;
+       char *pe;
+
+       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
+               path = NULL;
+       
+       for (part = 0; part < 16; part++) {
+               if (part) {
+                       sprintf(be, "p%d", part);
+                       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);
+               }
+       }
+}
+       
+
 int Manage_runstop(char *devname, int fd, int runstop, int quiet)
 {
        /* Run or stop the array. array must already be configured
@@ -160,9 +210,11 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
                struct map_ent *map = NULL;
                struct stat stb;
                struct mdinfo *mdi;
+               int devnum;
                /* If this is an mdmon managed array, just write 'inactive'
                 * to the array state and let mdmon clear up.
                 */
+               devnum = fd2devnum(fd);
                mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
                if (mdi &&
                    mdi->array.level > 0 &&
@@ -192,8 +244,6 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
                         */
                        ping_monitor(mdi->sys_name);
                }
-               if (mdi)
-                       sysfs_free(mdi);
 
                if (fd >= 0 && ioctl(fd, STOP_ARRAY, NULL)) {
                        if (quiet == 0) {
@@ -205,17 +255,28 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
                                                "process, mounted filesystem "
                                                "or active volume group?\n");
                        }
+                       if (mdi)
+                               sysfs_free(mdi);
                        return 1;
                }
+               /* 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.
+                */
+               if (mdi)
+                       sysfs_uevent(mdi, "change");
+
+               
+               if (devnum != NoMdDev &&
+                   (stat("/dev/.udev", &stb) != 0 ||
+                    check_env("MDADM_NO_UDEV"))) {
+                       struct map_ent *mp = map_by_devnum(&map, devnum);
+                       remove_devices(devnum, mp ? mp->path : NULL);
+               }
+
 
                if (quiet <= 0)
                        fprintf(stderr, Name ": stopped %s\n", devname);
-               if (fd >= 0 && fstat(fd, &stb) == 0) {
-                       int devnum;
-                       if (major(stb.st_rdev) == MD_MAJOR)
-                               devnum = minor(stb.st_rdev);
-                       else
-                               devnum = -1-(minor(stb.st_rdev)>>6);
+               if (devnum != NoMdDev) {
                        map_delete(&map, devnum);
                        map_write(map);
                        map_free(map);
@@ -443,13 +504,6 @@ int Manage_subdevs(char *devname, int fd,
                        }
 
                        if (array.not_persistent == 0 || tst->ss->external) {
-                               /* Make sure device is large enough */
-                               if (tst->ss->avail_size(tst, ldsize/512) <
-                                   array_size) {
-                                       fprintf(stderr, Name ": %s not large enough to join array\n",
-                                               dv->devname);
-                                       return 1;
-                               }
 
                                /* need to find a sample superblock to copy, and
                                 * a spare slot to use.
@@ -485,6 +539,15 @@ int Manage_subdevs(char *devname, int fd,
                                        fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
                                        return 1;
                                }
+
+                               /* Make sure device is large enough */
+                               if (tst->ss->avail_size(tst, ldsize/512) <
+                                   array_size) {
+                                       fprintf(stderr, Name ": %s not large enough to join array\n",
+                                               dv->devname);
+                                       return 1;
+                               }
+
                                /* Possibly this device was recently part of the array
                                 * and was temporarily removed, and is now being re-added.
                                 * If so, we can simply re-add it.
@@ -511,8 +574,10 @@ int Manage_subdevs(char *devname, int fd,
                                                disc.number = mdi.disk.number;
                                                disc.raid_disk = mdi.disk.raid_disk;
                                                disc.state = mdi.disk.state;
-                                               if (dv->writemostly)
+                                               if (dv->writemostly == 1)
                                                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
+                                               if (dv->writemostly == 2)
+                                                       disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
                                                if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
                                                        if (verbose >= 0)
                                                                fprintf(stderr, Name ": re-added %s\n", dv->devname);
@@ -551,7 +616,7 @@ int Manage_subdevs(char *devname, int fd,
                        disc.state = 0;
                        if (array.not_persistent==0 || tst->ss->external) {
                                int dfd;
-                               if (dv->writemostly)
+                               if (dv->writemostly == 1)
                                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
                                dfd = open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
                                tst->ss->add_to_super(tst, &disc, dfd,
@@ -591,7 +656,7 @@ int Manage_subdevs(char *devname, int fd,
                                                break;
                                        }
                        }
-                       if (dv->writemostly)
+                       if (dv->writemostly == 1)
                                disc.state |= (1 << MD_DISK_WRITEMOSTLY);
                        if (tst->ss->external) {
                                /* add a disk to an external metadata container
@@ -712,6 +777,23 @@ int Manage_subdevs(char *devname, int fd,
                                        close(lfd);
                                return 1;
                        }
+                       if (tst->ss->external) {
+                               /*
+                                * Before dropping our exclusive open we make an
+                                * attempt at preventing mdmon from seeing an
+                                * 'add' event before reconciling this 'remove'
+                                * event.
+                                */
+                               char *name = devnum2devname(fd2devnum(fd));
+
+                               if (!name) {
+                                       fprintf(stderr, Name ": unable to get container name\n");
+                                       return 1;
+                               }
+
+                               ping_manager(name);
+                               free(name);
+                       }
                        close(lfd);
                        if (verbose >= 0)
                                fprintf(stderr, Name ": hot removed %s\n",