]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdopen.c
imsm: turn off curr_migr_unit updates
[thirdparty/mdadm.git] / mdopen.c
index 4c0714d07c2c73f7f7bd1ff5a410e371d6ad7e36..5d478f5b5fdbcdb74f5572ff11788b6d6c521682 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -43,14 +43,16 @@ void make_parts(char *dev, int cnt)
         * else that of dev
         */
        struct stat stb;
-       int major_num, minor_num;
+       int major_num = major_num; /* quiet gcc -Os unitialized warning */
+       int minor_num = minor_num; /* quiet gcc -Os unitialized warning */
+       int odig = odig; /* quiet gcc -Os unitialized warning */
        int i;
        int nlen = strlen(dev) + 20;
        char *name = malloc(nlen);
        int dig = isdigit(dev[strlen(dev)-1]);
        char orig[1024];
        char sym[1024];
-       int odig;
+       int err;
 
        if (cnt==0) cnt=4;
        if (lstat(dev, &stb)!= 0)
@@ -86,12 +88,14 @@ void make_parts(char *dev, int cnt)
                                perror("chown");
                        if (chmod(name, stb2.st_mode & 07777))
                                perror("chmod");
+                       err = 0;
                } else {
-                       snprintf(sym, 10000, "%s%s%d", orig, odig?"p":"", i);
-                       symlink(sym, name);
+                       snprintf(sym, sizeof(sym), "%s%s%d", orig, odig?"p":"", i);
+                       err = symlink(sym, name);
                }
-               stat(name, &stb2);
-               add_dev(name, &stb2, 0, NULL);
+
+               if (err == 0 && stat(name, &stb2) == 0)
+                       add_dev(name, &stb2, 0, NULL);
        }
 }
 
@@ -180,25 +184,40 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                                use_mdp = 0;
                        else
                                use_mdp = 1;
+                       /* recreate name: /dev/md/0 or /dev/md/d0 */
+                       sprintf(cname, "%s%d", use_mdp?"d":"", num);
                } else
                        strcpy(cname, dev);
 
-               /* 'cname' must not contain a slash, may not start or end 
-                * with a digit, and may only be empty if num is present.
+               /* 'cname' must not contain a slash, and may not be
+                * empty.
                 */
-               if (strchr(cname, '/') != NULL ||
-                   isdigit(cname[0]) ||
-                   (cname[0] && isdigit(cname[strlen(cname)]))
-                       ) {
+               if (strchr(cname, '/') != NULL) {
                        fprintf(stderr, Name ": %s is an invalid name "
                                "for an md device.\n", dev);
                        return -1;
                }
-               if (cname[0] == 0 && num < 0) {
+               if (cname[0] == 0) {
                        fprintf(stderr, Name ": %s is an invalid name "
                                "for an md device (empty!).", dev);
                        return -1;
                }
+               if (num < 0) {
+                       /* If cname  is 'N' or 'dN', we get dev number
+                        * from there.
+                        */
+                       char *sp = cname;
+                       char *ep;
+                       if (cname[0] == 'd')
+                               sp++;
+                       num = strtoul(sp, &ep, 10);
+                       if (ep == sp || *ep || num < 0)
+                               num = -1;
+                       else if (cname[0] == 'd')
+                               use_mdp = 1;
+                       else
+                               use_mdp = 0;
+               }
        }
 
        /* Now determine device number */
@@ -219,11 +238,14 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                        use_mdp = 0;
        }
        if (num < 0 && trustworthy == LOCAL && name) {
-               /* if name is numeric, us that for num */
+               /* if name is numeric, use that for num
+                * if it is not already in use */
                char *ep;
                num = strtoul(name, &ep, 10);
                if (ep == name || *ep)
                        num = -1;
+               else if (mddev_busy(use_mdp ? (-1-num) : num))
+                       num = -1;
        }
 
        if (num < 0) {
@@ -253,26 +275,40 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                 * reasonable length and remove '/'
                 */
                char *cp;
+               struct map_ent *map = NULL;
+               int conflict = 1;
+               int unum = 0;
+               int cnlen;
                strncpy(cname, name, 200);
                cname[200] = 0;
                while ((cp = strchr(cname, '/')) != NULL)
                        *cp = '-';
-               if (trustworthy == METADATA)
-                       /* always add device number to metadata */
-                       sprintf(cname+strlen(cname), "%d", num);
-               else if (trustworthy == FOREIGN &&
-                        strchr(cname, ':') == NULL)
-                       /* add _%d to FOREIGN array that don't have
-                        * a 'host:' prefix
-                        */
-                       sprintf(cname+strlen(cname), "_%d", num<0?(-1-num):num);
+               if (trustworthy == LOCAL ||
+                   (trustworthy == FOREIGN && strchr(cname, ':') != NULL)) {
+                       /* Only need suffix if there is a conflict */
+                       if (map_by_name(&map, cname) == NULL)
+                               conflict = 0;
+               }
+               cnlen = strlen(cname);
+               while (conflict) {
+                       if (trustworthy == METADATA && !isdigit(cname[cnlen-1]))
+                               sprintf(cname+cnlen, "%d", unum);
+                       else
+                               /* add _%d to FOREIGN array that don't 
+                                * a 'host:' prefix
+                                */
+                               sprintf(cname+cnlen, "_%d", unum);
+                       unum++;
+                       if (map_by_name(&map, cname) == NULL)
+                               conflict = 0;
+               }
        }
        if (cname[0] == 0)
                strcpy(chosen, devname);
 
        /* We have a device number and name.
-        * If we can detect udev, just open the device and we
-        * are done.
+        * If we cannot detect udev, we need to make
+        * devices and links ourselves.
         */
        if (stat("/dev/.udev", &stb) != 0 ||
            check_env("MDADM_NO_UDEV")) {
@@ -323,8 +359,9 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                                                chosen);
                                        strcpy(chosen, devname);
                                }
-                       } else
-                               symlink(devname, chosen);
+                       } else if (symlink(devname, chosen) != 0)
+                               fprintf(stderr, Name ": failed to create %s: %s\n",
+                                       chosen, strerror(errno));
                        if (use_mdp && strcmp(chosen, devname) != 0)
                                make_parts(chosen, parts);
                }
@@ -360,83 +397,3 @@ int open_mddev(char *dev, int report_errors)
        }
        return mdfd;
 }
-
-
-int create_mddev_devnum(char *devname, int devnum, char *name,
-                     char *chosen_name, int parts)
-{
-       /* Open the md device with number 'devnum', possibly using 'devname',
-        * possibly constructing a name with 'name', but in any case, copying
-        * the name into 'chosen_name'
-        */
-       int major_num, minor_num;
-       struct stat stb;
-       int i;
-       struct createinfo *ci = conf_get_create_info();
-
-       if (devname)
-               strcpy(chosen_name, devname);
-       else if (name && *name && name[0] && strchr(name,'/') == NULL) {
-               char *n = strchr(name, ':');
-               if (n) n++; else n = name;
-               if (isdigit(*n) && devnum < 0)
-                       sprintf(chosen_name, "/dev/md/d%s", n);
-               else
-                       sprintf(chosen_name, "/dev/md/%s", n);
-       } else {
-               if (devnum >= 0)
-                       sprintf(chosen_name, "/dev/md%d", devnum);
-               else
-                       sprintf(chosen_name, "/dev/md/d%d", -1-devnum);
-       }
-       if (devnum >= 0) {
-               major_num = MD_MAJOR;
-               minor_num = devnum;
-       } else {
-               major_num = get_mdp_major();
-               minor_num = (-1-devnum) << 6;
-       }
-       if (stat(chosen_name, &stb) == 0) {
-               /* It already exists.  Check it is right. */
-               if ( ! S_ISBLK(stb.st_mode) ||
-                    stb.st_rdev != makedev(major_num, minor_num)) {
-                       errno = EEXIST;
-                       return -1;
-               }
-       } else {
-               /* special case: if --incremental is suggesting a name
-                * in /dev/md/, we make sure the directory exists.
-                */
-               if (strncmp(chosen_name, "/dev/md/", 8) == 0) {
-                       if (mkdir("/dev/md",0700)==0) {
-                               if (chown("/dev/md", ci->uid, ci->gid))
-                                       perror("chown /dev/md");
-                               if (chmod("/dev/md", ci->mode|
-                                                 ((ci->mode>>2) & 0111)))
-                                       perror("chmod /dev/md");
-                       }
-               }
-
-               if (mknod(chosen_name, S_IFBLK | 0600,
-                         makedev(major_num, minor_num)) != 0) {
-                       return -1;
-               }
-               /* FIXME chown/chmod ?? */
-       }
-
-       /* Simple locking to avoid --incr being called for the same
-        * array multiple times in parallel.
-        */
-       for (i = 0; i < 25 ; i++) {
-               int fd;
-
-               fd = open(chosen_name, O_RDWR|O_EXCL);
-               if (fd >= 0 || errno != EBUSY) {
-                       if (devnum < 0)
-                               make_parts(chosen_name, parts);
-                       return fd;
-               }
-               usleep(200000);
-       }
-       return -1;
-}