]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - lib.c
Show all bitmaps while examining bitmap
[thirdparty/mdadm.git] / lib.c
diff --git a/lib.c b/lib.c
index dc32c561faa9b9b63978981195ee35bc6ef684c3..6808f62d77ced0d6b044f48fa43a9b289f55b16b 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -23,6 +23,7 @@
  */
 
 #include       "mdadm.h"
+#include       "dlink.h"
 #include       <ctype.h>
 
 /* This fill contains various 'library' style function.  They
@@ -57,85 +58,96 @@ static int mdp_major = -1;
        return mdp_major;
 }
 
-
-void fmt_devname(char *name, int num)
-{
-       if (num >= 0)
-               sprintf(name, "md%d", num);
-       else
-               sprintf(name, "md_d%d", -1-num);
-}
-
-char *devnum2devname(int num)
+char *devid2kname(int devid)
 {
-       char name[100];
-       fmt_devname(name,num);
-       return xstrdup(name);
-}
+       char path[30];
+       char link[200];
+       static char devnm[32];
+       char *cp;
+       int n;
 
-int devname2devnum(char *name)
-{
-       char *ep;
-       int num;
-       if (strncmp(name, "md_d", 4)==0)
-               num = -1-strtoul(name+4, &ep, 10);
-       else
-               num = strtoul(name+2, &ep, 10);
-       return num;
+       /* Look at the
+        * /sys/dev/block/%d:%d link which must look like
+        * and take the last component.
+        */
+       sprintf(path, "/sys/dev/block/%d:%d", major(devid),
+               minor(devid));
+       n = readlink(path, link, sizeof(link)-1);
+       if (n > 0) {
+               link[n] = 0;
+               cp = strrchr(link, '/');
+               if (cp) {
+                       strcpy(devnm, cp+1);
+                       return devnm;
+               }
+       }
+       return NULL;
 }
 
-int stat2devnum(struct stat *st)
+char *devid2devnm(int devid)
 {
        char path[30];
        char link[200];
-       char *cp;
+       static char devnm[32];
+       char *cp, *ep;
        int n;
 
-       if ((S_IFMT & st->st_mode) == S_IFBLK) {
-               if (major(st->st_rdev) == MD_MAJOR)
-                       return minor(st->st_rdev);
-               else if (major(st->st_rdev) == (unsigned)get_mdp_major())
-                       return -1- (minor(st->st_rdev)>>MdpMinorShift);
-
-               /* must be an extended-minor partition. Look at the
-                * /sys/dev/block/%d:%d link which must look like
-                * ../../block/mdXXX/mdXXXpYY
-                */
-               sprintf(path, "/sys/dev/block/%d:%d", major(st->st_rdev),
-                       minor(st->st_rdev));
-               n = readlink(path, link, sizeof(link)-1);
-               if (n <= 0)
-                       return NoMdDev;
+       /* Might be an extended-minor partition or a
+        * named md device. Look at the
+        * /sys/dev/block/%d:%d link which must look like
+        *    ../../block/mdXXX/mdXXXpYY
+        * or
+        *    ...../block/md_FOO
+        */
+       sprintf(path, "/sys/dev/block/%d:%d", major(devid),
+               minor(devid));
+       n = readlink(path, link, sizeof(link)-1);
+       if (n > 0) {
                link[n] = 0;
-               cp = strrchr(link, '/');
-               if (cp) *cp = 0;
-               cp = strrchr(link, '/');
-               if (cp && strncmp(cp, "/md", 3) == 0)
-                       return devname2devnum(cp+1);
+               cp = strstr(link, "/block/");
+               if (cp) {
+                       cp += 7;
+                       ep = strchr(cp, '/');
+                       if (ep)
+                               *ep = 0;
+                       strcpy(devnm, cp);
+                       return devnm;
+               }
        }
-       return NoMdDev;
+       if (major(devid) == MD_MAJOR)
+               sprintf(devnm,"md%d", minor(devid));
+       else if (major(devid) == (unsigned)get_mdp_major())
+               sprintf(devnm,"md_d%d",
+                       (minor(devid)>>MdpMinorShift));
+       else
+               return NULL;
+       return devnm;
+}
 
+char *stat2devnm(struct stat *st)
+{
+       if ((S_IFMT & st->st_mode) != S_IFBLK)
+               return NULL;
+       return devid2devnm(st->st_rdev);
 }
 
-int fd2devnum(int fd)
+char *fd2devnm(int fd)
 {
        struct stat stb;
        if (fstat(fd, &stb) == 0)
-               return stat2devnum(&stb);
-       return NoMdDev;
+               return stat2devnm(&stb);
+       return NULL;
 }
 
-
-
 /*
  * convert a major/minor pair for a block device into a name in /dev, if possible.
  * On the first call, walk /dev collecting name.
  * Put them in a simple linked listfor now.
  */
 struct devmap {
-    int major, minor;
-    char *name;
-    struct devmap *next;
+       int major, minor;
+       char *name;
+       struct devmap *next;
 } *devlist = NULL;
 int devlist_ready = 0;
 
@@ -246,8 +258,6 @@ char *map_dev_preferred(int major, int minor, int create,
        return preferred ? preferred : regular;
 }
 
-
-
 /* conf_word gets one word from the conf file.
  * if "allow_key", then accept words at the start of a line,
  * otherwise stop when such a word is found.
@@ -390,3 +400,76 @@ void print_escape(char *str)
                }
        }
 }
+
+int check_env(char *name)
+{
+       char *val = getenv(name);
+
+       if (val && atoi(val) == 1)
+               return 1;
+
+       return 0;
+}
+
+int use_udev(void)
+{
+       static int use = -1;
+       struct stat stb;
+
+       if (use < 0) {
+               use = ((stat("/dev/.udev", &stb) == 0
+                       || stat("/run/udev", &stb) == 0)
+                      && check_env("MDADM_NO_UDEV") == 0);
+       }
+       return use;
+}
+
+unsigned long GCD(unsigned long a, unsigned long b)
+{
+       while (a != b) {
+               if (a < b)
+                       b -= a;
+               if (b < a)
+                       a -= b;
+       }
+       return a;
+}
+
+/*
+ * conf_line reads one logical line from the conffile or mdstat.
+ * It skips comments and continues until it finds a line that starts
+ * with a non blank/comment.  This character is pushed back for the next call
+ * A doubly linked list of words is returned.
+ * the first word will be a keyword.  Other words will have had quotes removed.
+ */
+
+char *conf_line(FILE *file)
+{
+       char *w;
+       char *list;
+
+       w = conf_word(file, 1);
+       if (w == NULL) return NULL;
+
+       list = dl_strdup(w);
+       free(w);
+       dl_init(list);
+
+       while ((w = conf_word(file,0))){
+               char *w2 = dl_strdup(w);
+               free(w);
+               dl_add(list, w2);
+       }
+/*    printf("got a line\n");*/
+       return list;
+}
+
+void free_line(char *line)
+{
+       char *w;
+       for (w=dl_next(line); w != line; w=dl_next(line)) {
+               dl_del(w);
+               dl_free(w);
+       }
+       dl_free(line);
+}