]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdopen.c
imsm: support for OROMs shared by multiple HBAs
[thirdparty/mdadm.git] / mdopen.c
index 856b5c0404fc1017e4333cefd7f1941ed8631215..2c9d745545f404176a569455c08fc17e23e83f67 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
@@ -126,7 +126,7 @@ void make_parts(char *dev, int cnt)
  *
  * If udev is configured, we create a temporary device, open it, and
  * unlink it.
- * If not, we create the /dev/mdXX device, and is name is usable,
+ * If not, we create the /dev/mdXX device, and if name is usable,
  * /dev/md/name
  * In any case we return /dev/md/name or (if that isn't available)
  * /dev/mdXX in 'chosen'.
@@ -260,25 +260,6 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                }
        }
 
-       if (num < 0) {
-               /* need to choose a free number. */
-               char *_devnm = find_free_devnm(use_mdp);
-               if (devnm == NULL) {
-                       pr_err("No avail md devices - aborting\n");
-                       return -1;
-               }
-               strcpy(devnm, _devnm);
-       } else {
-               sprintf(devnm, "%s%d", use_mdp?"md_d":"md", num);
-               if (mddev_busy(devnm)) {
-                       pr_err("%s is already in use.\n",
-                               dev);
-                       return -1;
-               }
-       }
-
-       sprintf(devname, "/dev/%s", devnm);
-
        if (cname[0] == 0 && name) {
                /* Need to find a name if we can
                 * We don't completely trust 'name'.  Truncate to
@@ -323,6 +304,40 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
                }
        }
 
+       devnm[0] = 0;
+       if (num < 0 && cname && ci->names) {
+               int fd;
+               int n = -1;
+               sprintf(devnm, "md_%s", cname);
+               fd = open("/sys/module/md_mod/parameters/new_array", O_WRONLY);
+               if (fd >= 0) {
+                       n = write(fd, devnm, strlen(devnm));
+                       close(fd);
+               }
+               if (n < 0)
+                       devnm[0] = 0;
+       }
+       if (devnm[0])
+               ;
+       else if (num < 0) {
+               /* need to choose a free number. */
+               char *_devnm = find_free_devnm(use_mdp);
+               if (devnm == NULL) {
+                       pr_err("No avail md devices - aborting\n");
+                       return -1;
+               }
+               strcpy(devnm, _devnm);
+       } else {
+               sprintf(devnm, "%s%d", use_mdp?"md_d":"md", num);
+               if (mddev_busy(devnm)) {
+                       pr_err("%s is already in use.\n",
+                               dev);
+                       return -1;
+               }
+       }
+
+       sprintf(devname, "/dev/%s", devnm);
+
        if (dev && dev[0] == '/')
                strcpy(chosen, dev);
        else if (cname[0] == 0)
@@ -423,3 +438,36 @@ int open_mddev(char *dev, int report_errors)
        }
        return mdfd;
 }
+
+char *find_free_devnm(int use_partitions)
+{
+       static char devnm[32];
+       int devnum;
+       for (devnum = 127; devnum != 128;
+            devnum = devnum ? devnum-1 : (1<<20)-1) {
+
+               if (use_partitions)
+                       sprintf(devnm, "md_d%d", devnum);
+               else
+                       sprintf(devnm, "md%d", devnum);
+               if (mddev_busy(devnm))
+                       continue;
+               if (!conf_name_is_free(devnm))
+                       continue;
+               if (!use_udev()) {
+                       /* make sure it is new to /dev too, at least as a
+                        * non-standard */
+                       int devid = devnm2devid(devnm);
+                       if (devid) {
+                               char *dn = map_dev(major(devid),
+                                                  minor(devid), 0);
+                               if (dn && ! is_standard(dn, NULL))
+                                       continue;
+                       }
+               }
+               break;
+       }
+       if (devnum == 128)
+               return NULL;
+       return devnm;
+}