]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super1.c
clear hi bits if not used after loading metadata from disk
[thirdparty/mdadm.git] / super1.c
index f165f59201342a60fa6589ddb1d7f76db55fb5d6..20f4c866ee9e77309810794d7df6219fc7e2dea5 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -142,8 +142,25 @@ static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
        return __cpu_to_le32(csum);
 }
 
+/*
+ * Information related to file descriptor used for aligned reads/writes.
+ * Cache the block size.
+ */
+struct align_fd {
+       int fd;
+       int blk_sz;
+};
+
+static void init_afd(struct align_fd *afd, int fd)
+{
+       afd->fd = fd;
+
+       if (ioctl(afd->fd, BLKSSZGET, &afd->blk_sz) != 0)
+               afd->blk_sz = 512;
+}
+
 static char abuf[4096+4096];
-static int aread(int fd, void *buf, int len)
+static int aread(struct align_fd *afd, void *buf, int len)
 {
        /* aligned read.
         * On devices with a 4K sector size, we need to read
@@ -153,26 +170,30 @@ static int aread(int fd, void *buf, int len)
        int bsize, iosize;
        char *b;
        int n;
-       if (ioctl(fd, BLKSSZGET, &bsize) != 0)
-               bsize = 512;
 
-       if (bsize > 4096 || len > 4096)
+       bsize = afd->blk_sz;
+
+       if (!bsize || bsize > 4096 || len > 4096) {
+               if (!bsize)
+                       fprintf(stderr, "WARNING - aread() called with "
+                               "invalid block size\n");
                return -1;
-       b = (char*)(((long)(abuf+4096))&~4095UL);
+       }
+       b = ROUND_UP_PTR((char *)abuf, 4096);
 
        for (iosize = 0; iosize < len; iosize += bsize)
                ;
-       n = read(fd, b, iosize);
+       n = read(afd->fd, b, iosize);
        if (n <= 0)
                return n;
-       lseek(fd, len - n, 1);
+       lseek(afd->fd, len - n, 1);
        if (n > len)
                n = len;
        memcpy(buf, b, n);
        return n;
 }
 
-static int awrite(int fd, void *buf, int len)
+static int awrite(struct align_fd *afd, void *buf, int len)
 {
        /* aligned write.
         * On devices with a 4K sector size, we need to write
@@ -183,27 +204,31 @@ static int awrite(int fd, void *buf, int len)
        int bsize, iosize;
        char *b;
        int n;
-       if (ioctl(fd, BLKSSZGET, &bsize) != 0)
-               bsize = 512;
-       if (bsize > 4096 || len > 4096)
+
+       bsize = afd->blk_sz;
+       if (!bsize || bsize > 4096 || len > 4096) {
+               if (!bsize)
+                       fprintf(stderr, "WARNING - awrite() called with "
+                               "invalid block size\n");
                return -1;
-       b = (char*)(((long)(abuf+4096))&~4095UL);
+       }
+       b = ROUND_UP_PTR((char *)abuf, 4096);
 
        for (iosize = 0; iosize < len ; iosize += bsize)
                ;
 
        if (len != iosize) {
-               n = read(fd, b, iosize);
+               n = read(afd->fd, b, iosize);
                if (n <= 0)
                        return n;
-               lseek(fd, -n, 1);
+               lseek(afd->fd, -n, 1);
        }
 
        memcpy(b, buf, len);
-       n = write(fd, b, iosize);
+       n = write(afd->fd, b, iosize);
        if (n <= 0)
                return n;
-       lseek(fd, len - n, 1);
+       lseek(afd->fd, len - n, 1);
        return len;
 }
 
@@ -253,7 +278,7 @@ static void examine_super1(struct supertype *st, char *homehost)
               (unsigned long long)__le64_to_cpu(sb->data_size),
               human_size(__le64_to_cpu(sb->data_size)<<9));
        if (__le32_to_cpu(sb->level) > 0) {
-               int ddsks=0;
+               int ddsks = 0, ddsks_denom = 1;
                switch(__le32_to_cpu(sb->level)) {
                case 1: ddsks=1;break;
                case 4:
@@ -261,13 +286,15 @@ static void examine_super1(struct supertype *st, char *homehost)
                case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
                case 10:
                        layout = __le32_to_cpu(sb->layout);
-                       ddsks = __le32_to_cpu(sb->raid_disks)
-                                / (layout&255) / ((layout>>8)&255);
+                       ddsks = __le32_to_cpu(sb->raid_disks);
+                       ddsks_denom = (layout&255) * ((layout>>8)&255);
                }
-               if (ddsks)
+               if (ddsks) {
+                       long long asize = __le64_to_cpu(sb->size);
+                       asize = (asize << 9) * ddsks / ddsks_denom;
                        printf("     Array Size : %llu%s\n",
-                              ddsks*(unsigned long long)__le64_to_cpu(sb->size),
-                              human_size(ddsks*__le64_to_cpu(sb->size)<<9));
+                              asize >> 10,  human_size(asize));
+               }
                if (sb->size != sb->data_size)
                        printf("  Used Dev Size : %llu%s\n",
                               (unsigned long long)__le64_to_cpu(sb->size),
@@ -843,12 +870,12 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info,
        char defname[10];
        int sbsize;
 
-       if (posix_memalign((void**)&sb, 512, SUPER1_SIZE) != 0) {
+       if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
                fprintf(stderr, Name
                        ": %s could not allocate superblock\n", __func__);
                return 0;
        }
-       memset(sb, 0, MAX_SB_SIZE);
+       memset(sb, 0, SUPER1_SIZE);
 
        st->sb = sb;
        if (info == NULL) {
@@ -883,7 +910,6 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info,
                sprintf(defname, "%d", info->md_minor);
                name = defname;
        }
-       memset(sb->set_name, 0, 32);
        if (homehost &&
            strchr(name, ':')== NULL &&
            strlen(homehost)+1+strlen(name) < 32) {
@@ -970,6 +996,7 @@ static int store_super1(struct supertype *st, int fd)
 {
        struct mdp_superblock_1 *sb = st->sb;
        unsigned long long sb_offset;
+       struct align_fd afd;
        int sbsize;
        unsigned long long dsize;
 
@@ -981,6 +1008,8 @@ static int store_super1(struct supertype *st, int fd)
        if (dsize < 24)
                return 2;
 
+       init_afd(&afd, fd);
+
        /*
         * Calculate the position of the superblock.
         * It is always aligned to a 4K boundary and
@@ -1017,10 +1046,9 @@ static int store_super1(struct supertype *st, int fd)
        if (lseek64(fd, sb_offset << 9, 0)< 0LL)
                return 3;
 
-       sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
-       sbsize = (sbsize+511)&(~511UL);
+       sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
 
-       if (awrite(fd, sb, sbsize) != sbsize)
+       if (awrite(&afd, sb, sbsize) != sbsize)
                return 4;
 
        if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
@@ -1028,9 +1056,8 @@ static int store_super1(struct supertype *st, int fd)
                        (((char*)sb)+MAX_SB_SIZE);
                if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
                        locate_bitmap1(st, fd);
-                       if (awrite(fd, bm, sizeof(*bm)) !=
-                           sizeof(*bm))
-                           return 5;
+                       if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
+                               return 5;
                }
        }
        fsync(fd);
@@ -1228,7 +1255,7 @@ static int compare_super1(struct supertype *st, struct supertype *tst)
                return 1;
 
        if (!first) {
-               if (posix_memalign((void**)&first, 512, SUPER1_SIZE) != 0) {
+               if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
                        fprintf(stderr, Name
                                ": %s could not allocate superblock\n", __func__);
                        return 1;
@@ -1258,9 +1285,12 @@ static int load_super1(struct supertype *st, int fd, char *devname)
        int uuid[4];
        struct bitmap_super_s *bsb;
        struct misc_dev_info *misc;
+       struct align_fd afd;
 
        free_super1(st);
 
+       init_afd(&afd, fd);
+
        if (st->ss == NULL || st->minor_version == -1) {
                int bestvers = -1;
                struct supertype tst;
@@ -1340,13 +1370,13 @@ static int load_super1(struct supertype *st, int fd, char *devname)
                return 1;
        }
 
-       if (posix_memalign((void**)&super, 512, SUPER1_SIZE) != 0) {
+       if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
                fprintf(stderr, Name ": %s could not allocate superblock\n",
                        __func__);
                return 1;
        }
 
-       if (aread(fd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
+       if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
                if (devname)
                        fprintf(stderr, Name ": Cannot read superblock on %s\n",
                                devname);
@@ -1391,7 +1421,7 @@ static int load_super1(struct supertype *st, int fd, char *devname)
         * should get that written out.
         */
        locate_bitmap1(st, fd);
-       if (aread(fd, bsb, 512) != 512)
+       if (aread(&afd, bsb, 512) != 512)
                goto no_bitmap;
 
        uuid_from_super1(st, uuid);
@@ -1409,10 +1439,10 @@ static int load_super1(struct supertype *st, int fd, char *devname)
 
 static struct supertype *match_metadata_desc1(char *arg)
 {
-       struct supertype *st = malloc(sizeof(*st));
-       if (!st) return st;
+       struct supertype *st = calloc(1, sizeof(*st));
+       if (!st)
+               return st;
 
-       memset(st, 0, sizeof(*st));
        st->container_dev = NoMdDev;
        st->ss = &super1;
        st->max_devs = MAX_DEVS;
@@ -1651,6 +1681,9 @@ static int write_bitmap1(struct supertype *st, int fd)
        int rv = 0;
        void *buf;
        int towrite, n;
+       struct align_fd afd;
+
+       init_afd(&afd, fd);
 
        locate_bitmap1(st, fd);
 
@@ -1668,7 +1701,7 @@ static int write_bitmap1(struct supertype *st, int fd)
                n = towrite;
                if (n > 4096)
                        n = 4096;
-               n = awrite(fd, buf, n);
+               n = awrite(&afd, buf, n);
                if (n > 0)
                        towrite -= n;
                else