]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
fix load_super/free_super mismatch in util.c
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index f3b7a3970224ffba7f2201287dcdb3a18b03dfbc..dbe46404c89ae1d00d649b6ecb02292709e5b879 100644 (file)
--- a/util.c
+++ b/util.c
 #include       "md_p.h"
 #include       <sys/utsname.h>
 #include       <ctype.h>
-#include       <linux/blkpg.h>
+
+/*
+ * following taken from linux/blkpg.h because they aren't
+ * anywhere else and it isn't safe to #include linux/ * stuff.
+ */
+
+#define BLKPG      _IO(0x12,105)
+
+/* The argument structure */
+struct blkpg_ioctl_arg {
+        int op;
+        int flags;
+        int datalen;
+        void *data;
+};
+
+/* The subfunctions (for the op field) */
+#define BLKPG_ADD_PARTITION    1
+#define BLKPG_DEL_PARTITION    2
+
+/* Sizes of name fields. Unused at present. */
+#define BLKPG_DEVNAMELTH       64
+#define BLKPG_VOLNAMELTH       64
+
+/* The data structure for ADD_PARTITION and DEL_PARTITION */
+struct blkpg_partition {
+       long long start;                /* starting offset in bytes */
+       long long length;               /* length in bytes */
+       int pno;                        /* partition number */
+       char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2,
+                                          to be used in kernel messages */
+       char volname[BLKPG_VOLNAMELTH]; /* volume label */
+};
 
 /*
  * Parse a 128 bit uuid in 4 integers
  */
 int parse_uuid(char *str, int uuid[4])
 {
-    int hit = 0; /* number of Hex digIT */
-    int i;
-    char c;
-    for (i=0; i<4; i++) uuid[i]=0;
-
-    while ((c= *str++)) {
-       int n;
-       if (c>='0' && c<='9')
-           n = c-'0';
-       else if (c>='a' && c <= 'f')
-           n = 10 + c - 'a';
-       else if (c>='A' && c <= 'F')
-           n = 10 + c - 'A';
-       else if (strchr(":. -", c))
-           continue;
-       else return 0;
-
-       if (hit<32) {
-           uuid[hit/8] <<= 4;
-           uuid[hit/8] += n;
+       int hit = 0; /* number of Hex digIT */
+       int i;
+       char c;
+       for (i=0; i<4; i++) uuid[i]=0;
+
+       while ((c= *str++)) {
+               int n;
+               if (c>='0' && c<='9')
+                       n = c-'0';
+               else if (c>='a' && c <= 'f')
+                       n = 10 + c - 'a';
+               else if (c>='A' && c <= 'F')
+                       n = 10 + c - 'A';
+               else if (strchr(":. -", c))
+                       continue;
+               else return 0;
+
+               if (hit<32) {
+                       uuid[hit/8] <<= 4;
+                       uuid[hit/8] += n;
+               }
+               hit++;
        }
-       hit++;
-    }
-    if (hit == 32)
-       return 1;
-    return 0;
-    
+       if (hit == 32)
+               return 1;
+       return 0;
 }
 
 
@@ -100,7 +131,6 @@ int md_get_version(int fd)
     return -1;
 }
 
-    
 int get_linux_version()
 {
        struct utsname name;
@@ -138,7 +168,7 @@ void remove_partitions(int fd)
 #endif
 }
 
-int enough(int level, int raid_disks, int layout,
+int enough(int level, int raid_disks, int layout, int clean,
           char *avail, int avail_disks)
 {
        int copies, first;
@@ -147,7 +177,7 @@ int enough(int level, int raid_disks, int layout,
                /* This is the tricky one - we need to check
                 * which actual disks are present.
                 */
-               copies = (layout&255)* (layout>>8);
+               copies = (layout&255)* ((layout>>8) & 255);
                first=0;
                do {
                        /* there must be one of the 'copies' form 'first' */
@@ -173,9 +203,15 @@ int enough(int level, int raid_disks, int layout,
                return avail_disks >= 1;
        case 4:
        case 5:
-               return avail_disks >= raid_disks-1;
+               if (clean)
+                       return avail_disks >= raid_disks-1;
+               else
+                       return avail_disks >= raid_disks;
        case 6:
-               return avail_disks >= raid_disks-2;
+               if (clean)
+                       return avail_disks >= raid_disks-2;
+               else
+                       return avail_disks >= raid_disks;
        default:
                return 0;
        }
@@ -186,7 +222,7 @@ int same_uuid(int a[4], int b[4], int swapuuid)
        if (swapuuid) {
                /* parse uuids are hostendian.
                 * uuid's from some superblocks are big-ending
-                * if there is a difference, we need to swap.. 
+                * if there is a difference, we need to swap..
                 */
                unsigned char *ac = (unsigned char *)a;
                unsigned char *bc = (unsigned char *)b;
@@ -208,7 +244,27 @@ int same_uuid(int a[4], int b[4], int swapuuid)
                return 0;
        }
 }
+void copy_uuid(void *a, int b[4], int swapuuid)
+{
+       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) {
+                       ac[i+0] = bc[i+3];
+                       ac[i+1] = bc[i+2];
+                       ac[i+2] = bc[i+1];
+                       ac[i+3] = bc[i+0];
+               }
+       } else
+               memcpy(a, b, 16);
+}
 
+#ifndef MDASSEMBLE
 int check_ext2(int fd, char *name)
 {
        /*
@@ -260,25 +316,24 @@ int check_reiser(int fd, char *name)
        fprintf(stderr, Name ": %s appears to contain a reiserfs file system\n",name);
        size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
        fprintf(stderr, "    size = %luK\n", size*4);
-               
+
        return 1;
 }
 
 int check_raid(int fd, char *name)
 {
-       void *super;
        struct mdinfo info;
        time_t crtime;
        char *level;
        struct supertype *st = guess_super(fd);
 
        if (!st) return 0;
-       st->ss->load_super(st, fd, &super, name);
+       st->ss->load_super(st, fd, 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);
-       free(super);
+       st->ss->getinfo_super(st, &info);
+       st->ss->free_super(st);
        crtime = info.array.ctime;
        level = map_num(pers, info.array.level);
        if (!level) level = "-unknown-";
@@ -306,6 +361,7 @@ int ask(char *mesg)
        fprintf(stderr, Name ": assuming 'no'\n");
        return 0;
 }
+#endif /* MDASSEMBLE */
 
 char *map_num(mapping_t *map, int num)
 {
@@ -332,7 +388,7 @@ int is_standard(char *dev, int *nump)
 {
        /* tests if dev is a "standard" md dev name.
         * i.e if the last component is "/dNN" or "/mdNN",
-        * where NN is a string of digits 
+        * where NN is a string of digits
         */
        char *d = strrchr(dev, '/');
        int type=0;
@@ -409,10 +465,6 @@ int nftw(const char *path, int (*han)(const char *name, const struct stat *stb,
        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;
@@ -491,7 +543,7 @@ unsigned long calc_csum(void *super, int bytes)
                newcsum+= superc[i];
        csum = (newcsum& 0xffffffff) + (newcsum>>32);
 #ifdef __alpha__
-/* The in-kernel checksum calculation is always 16bit on 
+/* The in-kernel checksum calculation is always 16bit on
  * the alpha, though it is 32 bit on i386...
  * I wonder what it is elsewhere... (it uses and API in
  * a way that it shouldn't).
@@ -502,6 +554,7 @@ unsigned long calc_csum(void *super, int bytes)
        return csum;
 }
 
+#ifndef MDASSEMBLE
 char *human_size(long long bytes)
 {
        static char buf[30];
@@ -536,7 +589,6 @@ char *human_size(long long bytes)
 char *human_size_brief(long long bytes)
 {
        static char buf[30];
-       
 
        if (bytes < 5000*1024)
                snprintf(buf, sizeof(buf), "%ld.%02ldKiB",
@@ -554,7 +606,9 @@ char *human_size_brief(long long bytes)
                        );
        return buf;
 }
+#endif
 
+#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
 int get_mdp_major(void)
 {
 static int mdp_major = -1;
@@ -638,6 +692,7 @@ void put_md_name(char *name)
        if (strncmp(name, "/dev/.tmp.md", 12)==0)
                unlink(name);
 }
+#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
 
 int dev_open(char *dev, int flags)
 {
@@ -668,23 +723,67 @@ int dev_open(char *dev, int flags)
 
 struct superswitch *superlist[] = { &super0, &super1, NULL };
 
-struct supertype *super_by_version(int vers, int minor)
+struct supertype *super_by_fd(int fd)
 {
-       struct supertype *st = malloc(sizeof(*st));
-       if (!st) return st;
-       if (vers == 0) {
-               st->ss = &super0;
-               st->max_devs = MD_SB_DISKS;
+       mdu_array_info_t array;
+       int vers;
+       int minor;
+       struct supertype *st = NULL;
+       struct mdinfo *sra;
+       char *verstr;
+       char version[20];
+       int i;
+
+       sra = sysfs_read(fd, 0, GET_VERSION);
+
+       if (sra) {
+               vers = sra->array.major_version;
+               minor = sra->array.minor_version;
+               verstr = sra->text_version;
+       } else {
+               if (ioctl(fd, GET_ARRAY_INFO, &array))
+                       array.major_version = array.minor_version = 0;
+               vers = array.major_version;
+               minor = array.minor_version;
+               verstr = "";
        }
 
-       if (vers == 1) {
-               st->ss = &super1;
-               st->max_devs = 384;
+       if (vers != -1) {
+               sprintf(version, "%d.%d", vers, minor);
+               verstr = version;
        }
-       st->minor_version = minor;
+       for (i = 0; st == NULL && superlist[i] ; i++)
+               st = superlist[i]->match_metadata_desc(verstr);
+
+       if (sra)
+               sysfs_free(sra);
+       st->sb = NULL;
        return st;
 }
 
+struct supertype *dup_super(struct supertype *st)
+{
+       struct supertype *stnew = NULL;
+       char *verstr = NULL;
+       char version[20];
+       int i;
+
+       if (!st)
+               return st;
+
+       if (st->minor_version == -1)
+               sprintf(version, "%d", st->ss->major);
+       else
+               sprintf(version, "%d.%d", st->ss->major, st->minor_version);
+       verstr = version;
+
+       for (i = 0; stnew == NULL && superlist[i] ; i++)
+               stnew = superlist[i]->match_metadata_desc(verstr);
+
+       stnew->sb = NULL;
+       return stnew;
+}
+
 struct supertype *guess_super(int fd)
 {
        /* try each load_super to find the best match,
@@ -694,8 +793,6 @@ struct supertype *guess_super(int fd)
        struct supertype *st;
        unsigned long besttime = 0;
        int bestsuper = -1;
-       
-       void *sbp = NULL;
        int i;
 
        st = malloc(sizeof(*st));
@@ -704,24 +801,24 @@ struct supertype *guess_super(int fd)
                int rv;
                ss = superlist[i];
                st->ss = NULL;
-               rv = ss->load_super(st, fd, &sbp, NULL);
+               rv = ss->load_super(st, fd, NULL);
                if (rv == 0) {
                        struct mdinfo info;
-                       ss->getinfo_super(&info, sbp);
+                       st->ss->getinfo_super(st, &info);
                        if (bestsuper == -1 ||
                            besttime < info.array.ctime) {
                                bestsuper = i;
                                besttime = info.array.ctime;
                        }
-                       free(sbp);
+                       ss->free_super(st);
                }
        }
        if (bestsuper != -1) {
                int rv;
                st->ss = NULL;
-               rv = superlist[bestsuper]->load_super(st, fd, &sbp, NULL);
+               rv = superlist[bestsuper]->load_super(st, fd, NULL);
                if (rv == 0) {
-                       free(sbp);
+                       superlist[bestsuper]->free_super(st);
                        return st;
                }
        }
@@ -729,7 +826,42 @@ struct supertype *guess_super(int fd)
        return NULL;
 }
 
+/* Return size of device in bytes */
+int get_dev_size(int fd, char *dname, unsigned long long *sizep)
+{
+       unsigned long long ldsize;
+       struct stat st;
 
+       if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
+               ldsize = (unsigned long long)st.st_size;
+       else
+#ifdef BLKGETSIZE64
+       if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
+#endif
+       {
+               unsigned long dsize;
+               if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
+                       ldsize = dsize;
+                       ldsize <<= 9;
+               } else {
+                       if (dname)
+                               fprintf(stderr, Name ": Cannot get size of %s: %s\b",
+                                       dname, strerror(errno));
+                       return 0;
+               }
+       }
+       *sizep = ldsize;
+       return 1;
+}
+
+void get_one_disk(int mdfd, mdu_array_info_t *ainf, mdu_disk_info_t *disk)
+{
+       int d;
+       ioctl(mdfd, GET_ARRAY_INFO, ainf);
+       for (d = 0 ; d < ainf->raid_disks + ainf->nr_disks ; d++)
+               if (ioctl(mdfd, GET_DISK_INFO, disk) == 0)
+                       return;
+}
 #ifdef __TINYC__
 /* tinyc doesn't optimize this check in ioctl.h out ... */
 unsigned int __invalid_size_argument_for_IOC = 0;