]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdopen.c
Release 3.2.6 - stability release
[thirdparty/mdadm.git] / mdopen.c
index b4d7bdc0ee263324030fc222d74685247cf7aec2..ad479423a660291b067259be606b8b49c174185f 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *    Author: Neil Brown
- *    Email: <neilb@cse.unsw.edu.au>
- *    Paper: Neil Brown
- *           School of Computer Science and Engineering
- *           The University of New South Wales
- *           Sydney, 2052
- *           Australia
+ *    Email: <neilb@suse.de>
  */
 
 #include "mdadm.h"
@@ -43,12 +38,12 @@ void make_parts(char *dev, int cnt)
         * else that of dev
         */
        struct stat stb;
-       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 major_num;
+       int minor_num;
+       int odig;
        int i;
        int nlen = strlen(dev) + 20;
-       char *name = malloc(nlen);
+       char *name;
        int dig = isdigit(dev[strlen(dev)-1]);
        char orig[1024];
        char sym[1024];
@@ -57,22 +52,27 @@ void make_parts(char *dev, int cnt)
        if (cnt==0) cnt=4;
        if (lstat(dev, &stb)!= 0)
                return;
-       if (S_ISLNK(stb.st_mode)) {
+
+       if (S_ISBLK(stb.st_mode)) {
+               major_num = major(stb.st_rdev);
+               minor_num = minor(stb.st_rdev);
+               odig = -1;
+       } else if (S_ISLNK(stb.st_mode)) {
                int len = readlink(dev, orig, sizeof(orig));
                if (len < 0 || len > 1000)
                        return;
                orig[len] = 0;
                odig = isdigit(orig[len-1]);
-       } else if (S_ISBLK(stb.st_mode)) {
-               major_num = major(stb.st_rdev);
-               minor_num = minor(stb.st_rdev);
+               major_num = -1;
+               minor_num = -1;
        } else
-                  return;
+               return;
+       name = malloc(nlen);
        for (i=1; i <= cnt ; i++) {
                struct stat stb2;
                snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
                if (stat(name, &stb2)==0) {
-                       if (!S_ISBLK(stb2.st_mode))
+                       if (!S_ISBLK(stb2.st_mode) || !S_ISBLK(stb.st_mode))
                                continue;
                        if (stb2.st_rdev == makedev(major_num, minor_num+i))
                                continue;
@@ -97,6 +97,7 @@ void make_parts(char *dev, int cnt)
                if (err == 0 && stat(name, &stb2) == 0)
                        add_dev(name, &stb2, 0, NULL);
        }
+       free(name);
 }
 
 
@@ -161,7 +162,6 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
 
 
        if (dev) {
-               
                if (strncmp(dev, "/dev/md/", 8) == 0) {
                        strcpy(cname, dev+8);
                } else if (strncmp(dev, "/dev/", 5) == 0) {
@@ -210,7 +210,10 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                        char *ep;
                        if (cname[0] == 'd')
                                sp++;
-                       num = strtoul(sp, &ep, 10);
+                       if (isdigit(sp[0]))
+                               num = strtoul(sp, &ep, 10);
+                       else
+                               ep = sp;
                        if (ep == sp || *ep || num < 0)
                                num = -1;
                        else if (cname[0] == 'd')
@@ -238,11 +241,19 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                        use_mdp = 0;
        }
        if (num < 0 && trustworthy == LOCAL && name) {
-               /* if name is numeric, use that for num
+               /* if name is numeric, possibly prefixed by 
+                * 'md' or '/dev/md', use that for num
                 * if it is not already in use */
                char *ep;
-               num = strtoul(name, &ep, 10);
-               if (ep == name || *ep)
+               char *n2 = name;
+               if (strncmp(n2, "/dev/", 5) == 0)
+                       n2 += 5;
+               if (strncmp(n2, "md", 2) == 0)
+                       n2 += 2;
+               if (*n2 == '/')
+                       n2++;
+               num = strtoul(n2, &ep, 10);
+               if (ep == n2 || *ep)
                        num = -1;
                else if (mddev_busy(use_mdp ? (-1-num) : num))
                        num = -1;
@@ -281,8 +292,17 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                int cnlen;
                strncpy(cname, name, 200);
                cname[200] = 0;
-               while ((cp = strchr(cname, '/')) != NULL)
-                       *cp = '-';
+               for (cp = cname; *cp ; cp++)
+                       switch (*cp) {
+                       case '/':
+                               *cp = '-';
+                               break;
+                       case ' ':
+                       case '\t':
+                               *cp = '_';
+                               break;
+                       }
+
                if (trustworthy == LOCAL ||
                    (trustworthy == FOREIGN && strchr(cname, ':') != NULL)) {
                        /* Only need suffix if there is a conflict */
@@ -291,7 +311,7 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                }
                cnlen = strlen(cname);
                while (conflict) {
-                       if (trustworthy == METADATA)
+                       if (trustworthy == METADATA && !isdigit(cname[cnlen-1]))
                                sprintf(cname+cnlen, "%d", unum);
                        else
                                /* add _%d to FOREIGN array that don't 
@@ -303,14 +323,17 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                                conflict = 0;
                }
        }
-       if (cname[0] == 0)
+
+       if (dev && dev[0] == '/')
+               strcpy(chosen, dev);
+       else if (cname[0] == 0)
                strcpy(chosen, devname);
 
        /* We have a device number and name.
         * If we cannot detect udev, we need to make
         * devices and links ourselves.
         */
-       if (stat("/dev/.udev", &stb) != 0 ||
+       if ((stat("/dev/.udev", &stb) != 0 && stat("/run/udev", &stb) != 0) ||
            check_env("MDADM_NO_UDEV")) {
                /* Make sure 'devname' exists and 'chosen' is a symlink to it */
                if (lstat(devname, &stb) == 0) {
@@ -352,8 +375,12 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
 
                        if (lstat(chosen, &stb) == 0) {
                                char buf[300];
+                               ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1);
+                               if (link_len >= 0)
+                                       buf[link_len] = '\0';
+
                                if ((stb.st_mode & S_IFMT) != S_IFLNK ||
-                                   readlink(chosen, buf, 300) <0 ||
+                                   link_len < 0 ||
                                    strcmp(buf, devname) != 0) {
                                        fprintf(stderr, Name ": %s exists - ignoring\n",
                                                chosen);
@@ -382,6 +409,8 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
 int open_mddev(char *dev, int report_errors)
 {
        int mdfd = open(dev, O_RDWR);
+       if (mdfd < 0 && errno == EACCES)
+               mdfd = open(dev, O_RDONLY);
        if (mdfd < 0) {
                if (report_errors)
                        fprintf(stderr, Name ": error opening %s: %s\n",