]> 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 5b994d6ae726b64d4a46046a6c8bac8c8e070f1d..f3b7a3970224ffba7f2201287dcdb3a18b03dfbc 100644 (file)
--- a/util.c
+++ b/util.c
@@ -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,32 +374,6 @@ 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)
-{
-       return 0;
-}
-char *map_dev(int major, int minor, int create)
-{
-#if 0
-       fprintf(stderr, "Warning - fail to map %d,%d to a device name\n",
-               major, minor);
-#endif
-       return NULL;
-}
-#else
-
-#ifdef __dietlibc__
-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)
-{
-       return ftw(path, add_dev_1, nopenfd);
-}
-#endif
-
 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
 {
        struct stat st;
@@ -404,6 +398,28 @@ int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
        return 0;
 }
 
+#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)
+{
+       return ftw(path, add_dev_1, nopenfd);
+}
+#else
+int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
+{
+       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.
  * If we find multiple names, choose the shortest.
@@ -416,10 +432,20 @@ 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) {
                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/.";
@@ -454,8 +480,6 @@ char *map_dev(int major, int minor, int create)
        return nonstd ? nonstd : std;
 }
 
-#endif
-
 unsigned long calc_csum(void *super, int bytes)
 {
        unsigned long long newcsum = 0;
@@ -623,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) {