]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super1.c
Release 3.2.6 - stability release
[thirdparty/mdadm.git] / super1.c
index af0abe659413a9c9eba684e4f40ac032e5d4a4a1..63b5c177b28bdd4a58b4477b13eafd5de5965063 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -93,6 +93,8 @@ struct mdp_superblock_1 {
 /* bitmap super size is 256, but we round up to a sector for alignment */
 #define BM_SUPER_SIZE 512
 #define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
+#define SUPER1_SIZE    (MAX_SB_SIZE + BM_SUPER_SIZE \
+                        + sizeof(struct misc_dev_info))
 
 struct misc_dev_info {
        __u64 device_size;
@@ -140,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
@@ -151,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
@@ -181,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;
 }
 
@@ -251,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:
@@ -259,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),
@@ -439,7 +468,12 @@ static void brief_examine_super1(struct supertype *st, int verbose)
        else
                nm = NULL;
 
-       printf("ARRAY%s%s", nm ? " /dev/md/":"", nm);
+       printf("ARRAY ");
+       if (nm) {
+               printf("/dev/md/");
+               print_escape(nm);
+               putchar(' ');
+       }
        if (verbose && c)
                printf(" level=%s", c);
        sb_offset = __le64_to_cpu(sb->super_offset);
@@ -456,8 +490,10 @@ static void brief_examine_super1(struct supertype *st, int verbose)
                if ((i&3)==0 && i != 0) printf(":");
                printf("%02x", sb->set_uuid[i]);
        }
-       if (sb->set_name[0])
-               printf(" name=%.32s", sb->set_name);
+       if (sb->set_name[0]) {
+               printf(" name=");
+               print_quoted(sb->set_name);
+       }
        printf("\n");
 }
 
@@ -519,8 +555,10 @@ static void brief_detail_super1(struct supertype *st)
        struct mdp_superblock_1 *sb = st->sb;
        int i;
 
-       if (sb->set_name[0])
-               printf(" name=%.32s", sb->set_name);
+       if (sb->set_name[0]) {
+               printf(" name=");
+               print_quoted(sb->set_name);
+       }
        printf(" UUID=");
        for (i=0; i<16; i++) {
                if ((i&3)==0 && i != 0) printf(":");
@@ -591,7 +629,7 @@ static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
        info->data_offset = __le64_to_cpu(sb->data_offset);
        info->component_size = __le64_to_cpu(sb->size);
        if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET))
-               info->bitmap_offset = __le32_to_cpu(sb->bitmap_offset);
+               info->bitmap_offset = (int32_t)__le32_to_cpu(sb->bitmap_offset);
 
        info->disk.major = 0;
        info->disk.minor = 0;
@@ -841,13 +879,12 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info,
        char defname[10];
        int sbsize;
 
-       if (posix_memalign((void**)&sb, 512, (MAX_SB_SIZE + BM_SUPER_SIZE + 
-                          sizeof(struct misc_dev_info))) != 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) {
@@ -882,7 +919,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) {
@@ -969,6 +1005,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;
 
@@ -980,6 +1017,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
@@ -1016,10 +1055,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)) {
@@ -1027,9 +1065,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);
@@ -1066,9 +1103,9 @@ static int write_init_super1(struct supertype *st)
        unsigned long long reserved;
        struct devinfo *di;
        unsigned long long dsize, array_size;
-       unsigned long long sb_offset;
+       unsigned long long sb_offset, headroom;
 
-       for (di = st->info; di && ! rv ; di = di->next) {
+       for (di = st->info; di; di = di->next) {
                if (di->disk.state == 1)
                        continue;
                if (di->fd < 0)
@@ -1139,6 +1176,14 @@ static int write_init_super1(struct supertype *st)
                /* work out how much space we left for a bitmap */
                bm_space = choose_bm_space(array_size);
 
+               /* We try to leave 0.1% at the start for reshape
+                * operations, but limit this to 128Meg (0.1% of 10Gig)
+                * which is plenty for efficient reshapes
+                */
+               headroom = 128 * 1024 * 2;
+               while  (headroom << 10 > array_size)
+                       headroom >>= 1;
+
                switch(st->minor_version) {
                case 0:
                        sb_offset = dsize;
@@ -1153,11 +1198,15 @@ static int write_init_super1(struct supertype *st)
                case 1:
                        sb->super_offset = __cpu_to_le64(0);
                        reserved = bm_space + 4*2;
+                       if (reserved < headroom)
+                               reserved = headroom;
+                       if (reserved + array_size > dsize)
+                               reserved = dsize - array_size;
                        /* Try for multiple of 1Meg so it is nicely aligned */
                        #define ONE_MEG (2*1024)
-                       reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
-                       if (reserved + __le64_to_cpu(sb->size) > dsize)
-                               reserved = dsize - __le64_to_cpu(sb->size);
+                       if (reserved > ONE_MEG)
+                               reserved = (reserved/ONE_MEG) * ONE_MEG;
+
                        /* force 4K alignment */
                        reserved &= ~7ULL;
 
@@ -1167,17 +1216,21 @@ static int write_init_super1(struct supertype *st)
                case 2:
                        sb_offset = 4*2;
                        sb->super_offset = __cpu_to_le64(4*2);
-                       if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
+                       if (4*2 + 4*2 + bm_space + array_size
                            > dsize)
-                               bm_space = dsize - __le64_to_cpu(sb->size)
+                               bm_space = dsize - array_size
                                        - 4*2 - 4*2;
 
                        reserved = bm_space + 4*2 + 4*2;
+                       if (reserved < headroom)
+                               reserved = headroom;
+                       if (reserved + array_size > dsize)
+                               reserved = dsize - array_size;
                        /* Try for multiple of 1Meg so it is nicely aligned */
                        #define ONE_MEG (2*1024)
-                       reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
-                       if (reserved + __le64_to_cpu(sb->size) > dsize)
-                               reserved = dsize - __le64_to_cpu(sb->size);
+                       if (reserved > ONE_MEG)
+                               reserved = (reserved/ONE_MEG) * ONE_MEG;
+
                        /* force 4K alignment */
                        reserved &= ~7ULL;
 
@@ -1192,13 +1245,14 @@ static int write_init_super1(struct supertype *st)
                        goto out;
                }
 
-
                sb->sb_csum = calc_sb_1_csum(sb);
                rv = store_super1(st, di->fd);
                if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
                        rv = st->ss->write_bitmap(st, di->fd);
                close(di->fd);
                di->fd = -1;
+               if (rv)
+                       goto error_out;
        }
 error_out:
        if (rv)
@@ -1227,15 +1281,12 @@ static int compare_super1(struct supertype *st, struct supertype *tst)
                return 1;
 
        if (!first) {
-               if (posix_memalign((void**)&first, 512,
-                              MAX_SB_SIZE + BM_SUPER_SIZE +
-                              sizeof(struct misc_dev_info)) != 0) {
+               if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
                        fprintf(stderr, Name
                                ": %s could not allocate superblock\n", __func__);
                        return 1;
                }
-               memcpy(first, second, MAX_SB_SIZE + BM_SUPER_SIZE +
-                      sizeof(struct misc_dev_info));
+               memcpy(first, second, SUPER1_SIZE);
                st->sb = first;
                return 0;
        }
@@ -1260,9 +1311,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;
@@ -1342,15 +1396,13 @@ static int load_super1(struct supertype *st, int fd, char *devname)
                return 1;
        }
 
-       if (posix_memalign((void**)&super, 512,
-                          MAX_SB_SIZE + BM_SUPER_SIZE +
-                          sizeof(struct misc_dev_info)) != 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);
@@ -1395,8 +1447,7 @@ static int load_super1(struct supertype *st, int fd, char *devname)
         * should get that written out.
         */
        locate_bitmap1(st, fd);
-       if (aread(fd, ((char*)super)+MAX_SB_SIZE, 512)
-           != 512)
+       if (aread(&afd, bsb, 512) != 512)
                goto no_bitmap;
 
        uuid_from_super1(st, uuid);
@@ -1414,10 +1465,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;
@@ -1481,12 +1532,13 @@ static __u64 avail_size1(struct supertype *st, __u64 devsize)
                st->minor_version = 2;
        if (super == NULL && st->minor_version > 0) {
                /* haven't committed to a size yet, so allow some
-                * slack for alignment of data_offset.
-                * We haven't access to device details so allow
-                * 1 Meg if bigger than 1Gig
+                * slack for space for reshape.
+                * Limit slack to 128M, but aim for about 0.1%
                 */
-               if (devsize > 1024*1024*2)
-                       devsize -= 1024*2;
+               unsigned long long headroom = 128*1024*2;
+               while ((headroom << 10) > devsize)
+                       headroom >>= 1;
+               devsize -= headroom;
        }
        switch(st->minor_version) {
        case 0:
@@ -1611,7 +1663,7 @@ add_internal_bitmap1(struct supertype *st,
                offset = -room;
        }
 
-       sb->bitmap_offset = __cpu_to_le32(offset);
+       sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
 
        sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
                                        | MD_FEATURE_BITMAP_OFFSET);
@@ -1656,6 +1708,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);
 
@@ -1663,7 +1718,7 @@ static int write_bitmap1(struct supertype *st, int fd)
                return -ENOMEM;
 
        memset(buf, 0xff, 4096);
-       memcpy(buf, ((char*)sb)+MAX_SB_SIZE, sizeof(bitmap_super_t));
+       memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
 
        towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
        towrite = (towrite+7) >> 3; /* bits to bytes */
@@ -1673,7 +1728,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