]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdopen.c
More consistent honoring of --configfile
[thirdparty/mdadm.git] / mdopen.c
index 0656ce26dd3ee2f04f06a3ccd39f8226600a0c0c..3004d4ab1dd470ac016113400a3a70f7adaf8e70 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
+ * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
@@ -46,6 +46,7 @@ void make_parts(char *dev, int cnt)
        char *name = malloc(nlen);
        int dig = isdigit(dev[strlen(dev)-1]);
 
+       if (cnt==0) cnt=4;
        if (stat(dev, &stb)!= 0)
                return;
        if (!S_ISBLK(stb.st_mode))
@@ -64,9 +65,12 @@ void make_parts(char *dev, int cnt)
                } else {
                        stb2 = stb;
                }
-               mknod(name, S_IFBLK | 0600, makedev(major, minor+i));
-               chown(name, stb2.st_uid, stb2.st_gid);
-               chmod(name, stb2.st_mode & 07777);
+               if (mknod(name, S_IFBLK | 0600, makedev(major, minor+i)))
+                       perror("mknod");
+               if (chown(name, stb2.st_uid, stb2.st_gid))
+                       perror("chown");
+               if (chmod(name, stb2.st_mode & 07777))
+                       perror("chmod");
                stat(name, &stb2);
                add_dev(name, &stb2, 0, NULL);
        }
@@ -79,7 +83,7 @@ void make_parts(char *dev, int cnt)
  * If it exists and is not an md device, is not the right type (partitioned or not),
  * or is currently in-use, we remove the device, but remember the owner and mode.
  * If it now doesn't exist, we find a new md array and create the device.
- * Default ownership is user=0, group=0 perm=0600
+ * Default ownership/mode comes from config file.
  */
 int open_mddev(char *dev, int autof)
 {
@@ -90,24 +94,60 @@ int open_mddev(char *dev, int autof)
        int must_remove = 0;
        struct mdstat_ent *mdlist;
        int num;
+       struct createinfo *ci = conf_get_create_info();
+       int parts;
 
-       if (autof) {
+       if (autof == 0)
+               autof = ci->autof;
+
+       parts = autof >> 3;
+       autof &= 7;
+
+       if (autof && autof != 1) {
                /* autof is set, so we need to check that the name is ok,
                 * and possibly create one if not
                 */
-               if (autof == -2 && !is_standard(dev, NULL)) {
-                       fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
-                       return -1;
-               }
+               int std;
                stb.st_mode = 0;
                if (stat(dev, &stb)==0 && ! S_ISBLK(stb.st_mode)) {
                        fprintf(stderr, Name ": %s is not a block device.\n",
                                dev);
                        return -1;
                }
+               if (autof == 2 && stb.st_mode == 0 && !is_standard(dev, NULL)) {
+                       fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
+                       return -1;
+               }
                /* check major number is correct */
-               if (autof>0)
-                       major = get_mdp_major();
+               num = -1;
+               std = is_standard(dev, &num);
+               if (std>0) major = get_mdp_major();
+               switch(autof) {
+               case 2: /* only create is_standard names */
+                       if (!std && !stb.st_mode) {
+                               fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
+                               return -1;
+                       }
+                       break;
+               case 3: /* create md, reject std>0 */
+                       if (std > 0) {
+                               fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
+                               return -1;
+                       }
+                       break;
+               case 4: /* create mdp, reject std<0 */
+                       if (std < 0) {
+                               fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
+                               return -1;
+                       }
+                       break;
+               case 5: /* default to md if not standard */
+                       break;
+               case 6: /* default to mdp if not standard */
+                       if (std == 0) major = get_mdp_major();
+                       break;
+               }
+               /* major is final. num is -1 if not standard */
                if (stb.st_mode && major(stb.st_rdev) != major)
                        must_remove = 1;
                if (stb.st_mode && !must_remove) {
@@ -126,11 +166,13 @@ int open_mddev(char *dev, int autof)
                        }
                        if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) {
                                /* already active */
-                               must_remove = 1;
                                close(mdfd);
+                               fprintf(stderr, Name ": %s is already active.\n",
+                                       dev);
+                               return -1;
                        } else {
-                               if (autof > 0)
-                                       make_parts(dev, autof);
+                               if (major != MD_MAJOR && parts > 0)
+                                       make_parts(dev, parts);
                                return mdfd;
                        }
                }
@@ -140,40 +182,31 @@ int open_mddev(char *dev, int autof)
                 * easiest to read /proc/mdstat, and hunt through for
                 * an unused number 
                 */
-               switch(is_standard(dev, &num)) {
-               case -1: /* non partitioned */
-                       if (autof > 0) {
-                               fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
-                               return -1;
-                       }
-                       minor = num;
-                       num = -1-num;
-                       break;
-               case 1: /* partitioned */
-                       if (autof == -1) {
-                               fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
-                               return -1;
-                       }
-                       minor = num <<  MdpMinorShift;
-                       major = get_mdp_major();
-                       break;
-               case 0: /* not standard, pick an unused number */
+               if (num < 0) {
+                       /* need to pick an unused number */
                        mdlist = mdstat_read(0, 0);
-                       for (num= (autof>0)?-1:0 ; ; num+= (autof>2)?-1:1) {
+                       /* Choose a large number.  Start from 127 and search down,
+                        * but if nothing is found, start really big
+                        */
+                       for (num = 127 ; num != 128 ; num = num ? num-1 : (1<<22)-1) {
                                struct mdstat_ent *me;
+                               int devnum = num;
+                               if (major != MD_MAJOR)
+                                       devnum = -1-num;
+
                                for (me=mdlist; me; me=me->next)
-                                       if (me->devnum == num)
+                                       if (me->devnum == devnum)
                                                break;
                                if (!me) {
-                                       /* doesn't exist if mdstat.
+                                       /* doesn't exist in mdstat.
                                         * make sure it is new to /dev too
                                         */
                                        char *dn;
-                                       if (autof > 0) 
-                                               minor = (-1-num) << MdpMinorShift;
+                                       if (major != MD_MAJOR)
+                                               minor = num << MdpMinorShift;
                                        else
                                                minor = num;
-                                       dn = map_dev(major,minor);
+                                       dn = map_dev(major,minor, 0);
                                        if (dn==NULL || is_standard(dn, NULL)) {
                                                /* this number only used by a 'standard' name,
                                                 * so it is safe to use
@@ -182,9 +215,12 @@ int open_mddev(char *dev, int autof)
                                        }
                                }
                        }
-               }
+               } else if (major == MD_MAJOR)
+                       minor = num;
+               else
+                       minor = num << MdpMinorShift;
                /* major and minor have been chosen */
-               
+
                /* If it was a 'standard' name and it is in-use, then
                 * the device could already be correct
                 */
@@ -205,12 +241,20 @@ int open_mddev(char *dev, int autof)
                                return -1;
                        }
                        if (must_remove) {
-                               chown(dev, stb.st_uid, stb.st_gid);
-                               chmod(dev, stb.st_mode & 07777);
+                               if (chown(dev, stb.st_uid, stb.st_gid))
+                                       perror("chown");
+                               if (chmod(dev, stb.st_mode & 07777))
+                                       perror("chmod");
+                       } else {
+                               if (chown(dev, ci->uid, ci->gid))
+                                       perror("chown");
+                               if (chmod(dev, ci->mode))
+                                       perror("chmod");
                        }
                        stat(dev, &stb);
                        add_dev(dev, &stb, 0, NULL);
-                       make_parts(dev,autof);
+                       if (major != MD_MAJOR)
+                               make_parts(dev,parts);
                }
        }
        mdfd = open(dev, O_RDWR, 0);