]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Release!!
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 386ec2ae4ee2e98f244ffd77bf3e4d523626279a..db4fbf38520e499067ce9fa5f44d77b5b3655001 100644 (file)
--- a/util.c
+++ b/util.c
@@ -140,14 +140,32 @@ int enough(int level, int raid_disks, int avail_disks)
        }
 }
 
-int same_uuid(int a[4], int b[4])
+int same_uuid(int a[4], int b[4], int swapuuid)
 {
-    if (a[0]==b[0] &&
-       a[1]==b[1] &&
-       a[2]==b[2] &&
-       a[3]==b[3])
-       return 1;
-    return 0;
+       if (swapuuid) {
+               /* parse uuids are hostendian.
+                * uuid's from some superblocks are big-ending
+                * if there is a difference, we need to swap.. 
+                */
+               unsigned char *ac = (unsigned char *)a;
+               unsigned char *bc = (unsigned char *)b;
+               int i;
+               for (i=0; i<16; i+= 4) {
+                       if (ac[i+0] != bc[i+3] ||
+                           ac[i+1] != bc[i+2] ||
+                           ac[i+2] != bc[i+1] ||
+                           ac[i+3] != bc[i+0])
+                               return 0;
+               }
+               return 1;
+       } else {
+               if (a[0]==b[0] &&
+                   a[1]==b[1] &&
+                   a[2]==b[2] &&
+                   a[3]==b[3])
+                       return 1;
+               return 0;
+       }
 }
 
 int check_ext2(int fd, char *name)
@@ -209,7 +227,9 @@ int check_raid(int fd, char *name)
 {
        void *super;
        struct mdinfo info;
+       struct mddev_ident_s ident;
        time_t crtime;
+       char *level;
        struct supertype *st = guess_super(fd);
 
        if (!st) return 0;
@@ -217,11 +237,13 @@ int check_raid(int fd, char *name)
        /* Looks like a raid array .. */
        fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
                name);
-       st->ss->getinfo_super(&info, super);
+       st->ss->getinfo_super(&info, &ident, super);
        free(super);
        crtime = info.array.ctime;
-       fprintf(stderr, "    level=%d devices=%d ctime=%s",
-               info.array.level, info.array.raid_disks, ctime(&crtime));
+       level = map_num(pers, info.array.level);
+       if (!level) level = "-unknown-";
+       fprintf(stderr, "    level=%s devices=%d ctime=%s",
+               level, info.array.raid_disks, ctime(&crtime));
        return 1;
 }
 
@@ -313,6 +335,9 @@ struct devmap {
 int devlist_ready = 0;
 
 #ifdef UCLIBC
+int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
+{
+}
 char *map_dev(int major, int minor)
 {
 #if 0
@@ -322,15 +347,19 @@ char *map_dev(int major, int minor)
        return NULL;
 }
 #else
-#define  __USE_XOPEN_EXTENDED
-#include <ftw.h>
 
+#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)
+{
+       ftw(path, add_dev_1, nopenfd);
+}
+#endif
 
-#ifndef __dietlibc__
 int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s)
-#else
-int add_dev(const char *name, const struct stat *stb, int flag)
-#endif
 {
     if ((stb->st_mode&S_IFMT)== S_IFBLK) {
        char *n = strdup(name);
@@ -348,30 +377,34 @@ int add_dev(const char *name, const struct stat *stb, int flag)
 
 /*
  * Find a block device with the right major/minor number.
- * Avoid /dev/mdNN and /dev/md/dNN if possible
+ * If we find multiple names, choose the shortest.
+ * If we find a non-standard name, it is probably there
+ * deliberately so prefer it over a standard name.
+ * This applies only to names for MD devices.
  */
 char *map_dev(int major, int minor)
 {
        struct devmap *p;
-       char *std = NULL;
+       char *std = NULL, *nonstd=NULL;
        if (!devlist_ready) {
-#ifndef __dietlibc__
                nftw("/dev", add_dev, 10, FTW_PHYS);
-#else
-               ftw("/dev", add_dev, 10);
-#endif
                devlist_ready=1;
        }
 
        for (p=devlist; p; p=p->next)
                if (p->major == major &&
                    p->minor == minor) {
-                       if (is_standard(p->name, NULL))
-                               std = p->name;
-                       else
-                               return p->name;
+                       if (is_standard(p->name, NULL)) {
+                               if (std == NULL ||
+                                   strlen(p->name) < strlen(std))
+                                       std = p->name;
+                       } else {
+                               if (nonstd == NULL ||
+                                   strlen(p->name) < strlen(nonstd))
+                                       nonstd = p->name;
+                       }
                }
-       return std;
+       return nonstd ? nonstd : std;
 }
 
 #endif
@@ -416,13 +449,13 @@ char *human_size(long long bytes)
        else if (bytes < 2*1024LL*1024LL*1024LL) {
                long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
                long cMB  = (bytes / ( 1000000LL / 200LL ) +1) /2;
-               sprintf(buf, " (%ld.%02ld MiB %ld.%02ld MB)",
+               snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
                        cMiB/100 , cMiB % 100,
                        cMB/100, cMB % 100);
        } else {
                long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
                long cGB  = (bytes / (1000000000LL/200LL ) +1) /2;
-               sprintf(buf, " (%ld.%02ld GiB %ld.%02ld GB)",
+               snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
                        cGiB/100 , cGiB % 100,
                        cGB/100, cGB % 100);
        }
@@ -435,16 +468,16 @@ char *human_size_brief(long long bytes)
        
 
        if (bytes < 5000*1024)
-               sprintf(buf, "%ld.%02ldKiB",
+               snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
                        (long)(bytes>>10), (long)(((bytes&1023)*100+512)/1024)
                        );
        else if (bytes < 2*1024LL*1024LL*1024LL)
-               sprintf(buf, "%ld.%02ldMiB",
+               snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
                        (long)(bytes>>20),
                        (long)((bytes&0xfffff)+0x100000/200)/(0x100000/100)
                        );
        else
-               sprintf(buf, "%ld.%02ldGiB",
+               snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
                        (long)(bytes>>30),
                        (long)(((bytes>>10)&0xfffff)+0x100000/200)/(0x100000/100)
                        );
@@ -494,20 +527,20 @@ char *get_md_name(int dev)
                int mdp =  get_mdp_major();
                if (mdp < 0) return NULL;
                rdev = makedev(mdp, (-1-dev)<<6);
-               sprintf(devname, "/dev/md/d%d", -1-dev);
+               snprintf(devname, sizeof(devname), "/dev/md/d%d", -1-dev);
                if (stat(devname, &stb) == 0
                    && (S_IFMT&stb.st_mode) == S_IFBLK
                    && (stb.st_rdev == rdev))
                        return devname;
        } else {
                rdev = makedev(MD_MAJOR, dev);
-               sprintf(devname, "/dev/md%d", dev);
+               snprintf(devname, sizeof(devname), "/dev/md%d", dev);
                if (stat(devname, &stb) == 0
                    && (S_IFMT&stb.st_mode) == S_IFBLK
                    && (stb.st_rdev == rdev))
                        return devname;
 
-               sprintf(devname, "/dev/md/%d", dev);
+               snprintf(devname, sizeof(devname), "/dev/md/%d", dev);
                if (stat(devname, &stb) == 0
                    && (S_IFMT&stb.st_mode) == S_IFBLK
                    && (stb.st_rdev == rdev))
@@ -516,7 +549,7 @@ char *get_md_name(int dev)
        dn = map_dev(major(rdev), minor(rdev));
        if (dn)
                return dn;
-       sprintf(devname, "/dev/.tmp.md%d", dev);
+       snprintf(devname, sizeof(devname), "/dev/.tmp.md%d", dev);
        if (mknod(devname, S_IFBLK | 0600, rdev) == -1)
                if (errno != EEXIST)
                        return NULL;
@@ -537,7 +570,6 @@ void put_md_name(char *name)
 
 
 
-
 struct superswitch *superlist[] = { &super0, &super1, NULL };
 
 struct supertype *super_by_version(int vers, int minor)
@@ -575,21 +607,23 @@ struct supertype *guess_super(int fd)
        for (i=0 ; superlist[i]; i++) {
                int rv;
                ss = superlist[i];
+               st->ss = NULL;
                rv = ss->load_super(st, fd, &sbp, NULL);
                if (rv == 0) {
                        struct mdinfo info;
-                       ss->getinfo_super(&info, sbp);
+                       struct mddev_ident_s ident;
+                       ss->getinfo_super(&info, &ident, sbp);
                        if (bestsuper == -1 ||
                            besttime < info.array.ctime) {
                                bestsuper = i;
                                besttime = info.array.ctime;
                        }
-                       st->ss = NULL;
                        free(sbp);
                }
        }
        if (bestsuper != -1) {
                int rv;
+               st->ss = NULL;
                rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL);
                if (rv == 0) {
                        free(sbp);
@@ -599,3 +633,9 @@ struct supertype *guess_super(int fd)
        free(st);
        return NULL;
 }
+
+#ifdef __TINYC__
+/* tinyc doesn't optimize this check in ioctl.h out ... */
+unsigned int __invalid_size_argument_for_IOC = 0;
+#endif
+