]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Remove partitions from components of an md array
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index ff175009ec8624a2e50fe1d8699a1590cd6e18fb..f3b7a3970224ffba7f2201287dcdb3a18b03dfbc 100644 (file)
--- a/util.c
+++ b/util.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
@@ -31,6 +31,7 @@
 #include       "md_p.h"
 #include       <sys/utsname.h>
 #include       <ctype.h>
+#include       <linux/blkpg.h>
 
 /*
  * Parse a 128 bit uuid in 4 integers
@@ -118,6 +119,25 @@ int get_linux_version()
        return (a*1000000)+(b*1000)+c;
 }
 
+void remove_partitions(int fd)
+{
+       /* remove partitions from this block devices.
+        * This is used for components added to an array
+        */
+#ifdef BLKPG_DEL_PARTITION
+       struct blkpg_ioctl_arg a;
+       struct blkpg_partition p;
+
+       a.op = BLKPG_DEL_PARTITION;
+       a.data = (void*)&p;
+       a.datalen = sizeof(p);
+       a.flags = 0;
+       memset(a.data, 0, a.datalen);
+       for (p.pno=0; p.pno < 16; p.pno++)
+               ioctl(fd, BLKPG, &a);
+#endif
+}
+
 int enough(int level, int raid_disks, int layout,
           char *avail, int avail_disks)
 {
@@ -354,46 +374,51 @@ struct devmap {
 } *devlist = NULL;
 int devlist_ready = 0;
 
-#ifdef UCLIBC
 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
 {
+       struct stat st;
+       if (S_ISLNK(stb->st_mode)) {
+               stat(name, &st);
+               stb = &st;
+       }
+
+       if ((stb->st_mode&S_IFMT)== S_IFBLK) {
+               char *n = strdup(name);
+               struct devmap *dm = malloc(sizeof(*dm));
+               if (strncmp(n, "/dev/./", 7)==0)
+                       strcpy(n+4, name+6);
+               if (dm) {
+                       dm->major = major(stb->st_rdev);
+                       dm->minor = minor(stb->st_rdev);
+                       dm->name = n;
+                       dm->next = devlist;
+                       devlist = dm;
+               }
+       }
+       return 0;
 }
-char *map_dev(int major, int minor)
-{
-#if 0
-       fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
-               major, minor);
-#endif
-       return NULL;
-}
-#else
 
-#ifdef __dietlibc__
+#ifndef HAVE_NFTW
+#ifdef HAVE_FTW
 int add_dev_1(const char *name, const struct stat *stb, int flag)
 {
        return add_dev(name, stb, flag, NULL);
 }
 int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
 {
-       ftw(path, add_dev_1, nopenfd);
+       return ftw(path, add_dev_1, nopenfd);
 }
-#endif
-
+#else
 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
 {
-    if ((stb->st_mode&S_IFMT)== S_IFBLK) {
-       char *n = strdup(name);
-       struct devmap *dm = malloc(sizeof(*dm));
-       if (dm) {
-           dm->major = major(stb->st_rdev);
-           dm->minor = minor(stb->st_rdev);
-           dm->name = n;
-           dm->next = devlist;
-           devlist = dm;
-       }
-    }
-    return 0;
+       return 0;
 }
+int nftw(const char *path, int (*han)(const char *name, const struct stat *stb, int flag, struct FTW *s), int nopenfd, int flags)
+{
+       return 0;
+}
+#endif /* HAVE_FTW */
+#endif /* HAVE_NFTW */
 
 /*
  * Find a block device with the right major/minor number.
@@ -402,13 +427,31 @@ int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
  * deliberately so prefer it over a standard name.
  * This applies only to names for MD devices.
  */
-char *map_dev(int major, int minor)
+char *map_dev(int major, int minor, int create)
 {
        struct devmap *p;
        char *std = NULL, *nonstd=NULL;
+       int did_check = 0;
+
+       if (major == 0 && minor == 0)
+                       return NULL;
+
+ retry:
        if (!devlist_ready) {
-               nftw("/dev", add_dev, 10, FTW_PHYS);
+               char *dev = "/dev";
+               struct stat stb;
+               while(devlist) {
+                       struct devmap *d = devlist;
+                       devlist = d->next;
+                       free(d->name);
+                       free(d);
+               }
+               if (lstat(dev, &stb)==0 &&
+                   S_ISLNK(stb.st_mode))
+                       dev = "/dev/.";
+               nftw(dev, add_dev, 10, FTW_PHYS);
                devlist_ready=1;
+               did_check = 1;
        }
 
        for (p=devlist; p; p=p->next)
@@ -424,11 +467,19 @@ char *map_dev(int major, int minor)
                                        nonstd = p->name;
                        }
                }
+       if (!std && !nonstd && !did_check) {
+               devlist_ready = 0;
+               goto retry;
+       }
+       if (create && !std && !nonstd) {
+               static char buf[30];
+               snprintf(buf, sizeof(buf), "%d:%d", major, minor);
+               nonstd = buf;
+       }
+
        return nonstd ? nonstd : std;
 }
 
-#endif
-
 unsigned long calc_csum(void *super, int bytes)
 {
        unsigned long long newcsum = 0;
@@ -566,7 +617,7 @@ char *get_md_name(int dev)
                    && (stb.st_rdev == rdev))
                        return devname;
        }
-       dn = map_dev(major(rdev), minor(rdev));
+       dn = map_dev(major(rdev), minor(rdev), 0);
        if (dn)
                return dn;
        snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
@@ -596,8 +647,12 @@ int dev_open(char *dev, int flags)
        char *e;
        int fd = -1;
        char devname[32];
-       int major = strtoul(dev, &e, 0);
+       int major;
        int minor;
+
+       if (!dev) return -1;
+
+       major = strtoul(dev, &e, 0);
        if (e > dev && *e == ':' && e[1] &&
            (minor = strtoul(e+1, &e, 0)) >= 0 &&
            *e == 0) {