]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super1.c
Allow data-offset to be specified per-device for create
[thirdparty/mdadm.git] / super1.c
index 01743169763638578b8dc17c5cdaa179641b1526..d6b69fbe93bc80f9d0c7cd617d2e9005e12a9a66 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -58,7 +58,10 @@ struct mdp_superblock_1 {
        __u32   delta_disks;    /* change in number of raid_disks               */
        __u32   new_layout;     /* new layout                                   */
        __u32   new_chunk;      /* new chunk size (bytes)                       */
-       __u8    pad1[128-124];  /* set to 0 when written */
+       __u32   new_offset;     /* signed number to add to data_offset in new
+                                * layout.  0 == no-change.  This can be
+                                * different on each device in the array.
+                                */
 
        /* constant this-device information - 64 bytes */
        __u64   data_offset;    /* sector start of data, often 0 */
@@ -112,8 +115,23 @@ struct misc_dev_info {
                                           */
 #define        MD_FEATURE_RESHAPE_ACTIVE       4
 #define        MD_FEATURE_BAD_BLOCKS           8 /* badblock list is not empty */
-
-#define        MD_FEATURE_ALL                  (1|2|4|8)
+#define        MD_FEATURE_REPLACEMENT          16 /* This device is replacing an
+                                           * active device with same 'role'.
+                                           * 'recovery_offset' is also set.
+                                           */
+#define        MD_FEATURE_RESHAPE_BACKWARDS    32 /* Reshape doesn't change number
+                                           * of devices, but is going
+                                           * backwards anyway.
+                                           */
+#define        MD_FEATURE_NEW_OFFSET           64 /* new_offset must be honoured */
+#define        MD_FEATURE_ALL                  (MD_FEATURE_BITMAP_OFFSET       \
+                                       |MD_FEATURE_RECOVERY_OFFSET     \
+                                       |MD_FEATURE_RESHAPE_ACTIVE      \
+                                       |MD_FEATURE_BAD_BLOCKS          \
+                                       |MD_FEATURE_REPLACEMENT         \
+                                       |MD_FEATURE_RESHAPE_BACKWARDS   \
+                                       |MD_FEATURE_NEW_OFFSET          \
+                                       )
 
 #ifndef offsetof
 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
@@ -309,6 +327,11 @@ static void examine_super1(struct supertype *st, char *homehost)
        if (sb->data_offset)
                printf("    Data Offset : %llu sectors\n",
                       (unsigned long long)__le64_to_cpu(sb->data_offset));
+       if (sb->new_offset) {
+               unsigned long long offset = __le64_to_cpu(sb->data_offset);
+               offset += (signed)(int32_t)__le32_to_cpu(sb->new_offset);
+               printf("     New Offset : %llu sectors\n", offset);
+       }
        printf("   Super Offset : %llu sectors\n",
               (unsigned long long)__le64_to_cpu(sb->super_offset));
        if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
@@ -642,10 +665,14 @@ static void uuid_from_super1(struct supertype *st, int uuid[4])
 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
 {
        struct mdp_superblock_1 *sb = st->sb;
+       struct bitmap_super_s *bsb = (void*)(((char*)sb)+MAX_SB_SIZE);
+       struct misc_dev_info *misc = (void*)(((char*)sb)+MAX_SB_SIZE+BM_SUPER_SIZE);
        int working = 0;
        unsigned int i;
        unsigned int role;
        unsigned int map_disks = info->array.raid_disks;
+       unsigned long long super_offset;
+       unsigned long long data_size;
 
        memset(info, 0, sizeof(*info));
        info->array.major_version = 1;
@@ -676,6 +703,39 @@ static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
        else
                role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
 
+       super_offset = __le64_to_cpu(sb->super_offset);
+       data_size = __le64_to_cpu(sb->size);
+       if (info->data_offset < super_offset) {
+               unsigned long long end;
+               info->space_before = info->data_offset;
+               end = super_offset;
+               if (info->bitmap_offset < 0)
+                       end += info->bitmap_offset;
+               if (info->data_offset + data_size < end)
+                       info->space_after = end - data_size - info->data_offset;
+               else
+                       info->space_after = 0;
+       } else {
+               info->space_before = (info->data_offset -
+                                     super_offset);
+               if (info->bitmap_offset > 0) {
+                       unsigned long long bmend = info->bitmap_offset;
+                       unsigned long long size = __le64_to_cpu(bsb->sync_size);
+                       size /= __le32_to_cpu(bsb->chunksize) >> 9;
+                       size = (size + 7) >> 3;
+                       size += sizeof(bitmap_super_t);
+                       size = ROUND_UP(size, 4096);
+                       size /= 512;
+                       size += bmend;
+                       if (size < info->space_before)
+                               info->space_before -= size;
+                       else
+                               info->space_before = 0;
+               } else
+                       info->space_before -= 8; /* superblock */
+               info->space_after = misc->device_size - data_size - info->data_offset;
+       }
+
        info->disk.raid_disk = -1;
        switch(role) {
        case 0xFFFF:
@@ -706,6 +766,8 @@ static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
 
        if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
                info->reshape_active = 1;
+               if (info->array.level == 10)
+                       info->reshape_active |= RESHAPE_NO_BACKUP;
                info->reshape_progress = __le64_to_cpu(sb->reshape_position);
                info->new_level = __le32_to_cpu(sb->new_level);
                info->delta_disks = __le32_to_cpu(sb->delta_disks);
@@ -1044,13 +1106,14 @@ static int init_super1(struct supertype *st, mdu_array_info_t *info,
 struct devinfo {
        int fd;
        char *devname;
+       long long data_offset;
        mdu_disk_info_t disk;
        struct devinfo *next;
 };
 #ifndef MDASSEMBLE
 /* Add a device to the superblock being created */
 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
-                         int fd, char *devname)
+                        int fd, char *devname, unsigned long long data_offset)
 {
        struct mdp_superblock_1 *sb = st->sb;
        __u16 *rp = sb->dev_roles + dk->number;
@@ -1078,6 +1141,7 @@ static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
        di->fd = fd;
        di->devname = devname;
        di->disk = *dk;
+       di->data_offset = data_offset;
        di->next = NULL;
        *dip = di;
 
@@ -1268,18 +1332,21 @@ static int write_init_super1(struct supertype *st)
                /* 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
+                * However we make it at least 2 chunks as one chunk
+                * is minimum needed for reshape.
                 */
                headroom = 128 * 1024 * 2;
-               while  (headroom << 10 > array_size)
+               while  (headroom << 10 > array_size &&
+                       headroom/2 >= __le32_to_cpu(sb->chunksize) * 2)
                        headroom >>= 1;
 
+               data_offset = di->data_offset;
                switch(st->minor_version) {
                case 0:
                        sb_offset = dsize;
                        sb_offset -= 8*2;
                        sb_offset &= ~(4*2-1);
                        sb->super_offset = __cpu_to_le64(sb_offset);
-                       data_offset = __le64_to_cpu(sb->data_offset);
                        if (data_offset == INVALID_SECTORS)
                                sb->data_offset = 0;
                        if (sb_offset < array_size + bm_space)
@@ -1292,7 +1359,6 @@ static int write_init_super1(struct supertype *st)
                        break;
                case 1:
                        sb->super_offset = __cpu_to_le64(0);
-                       data_offset = __le64_to_cpu(sb->data_offset);
                        if (data_offset == INVALID_SECTORS) {
                                reserved = bm_space + 4*2;
                                if (reserved < headroom)
@@ -1320,7 +1386,6 @@ static int write_init_super1(struct supertype *st)
                case 2:
                        sb_offset = 4*2;
                        sb->super_offset = __cpu_to_le64(4*2);
-                       data_offset = __le64_to_cpu(sb->data_offset);
                        if (data_offset == INVALID_SECTORS) {
                                if (4*2 + 4*2 + bm_space + array_size
                                    > dsize)
@@ -1623,8 +1688,8 @@ static struct supertype *match_metadata_desc1(char *arg)
  * superblock type st, and reserving 'reserve' sectors for
  * a possible bitmap
  */
-static __u64 avail_size1(struct supertype *st, __u64 devsize,
-                        unsigned long long data_offset)
+static __u64 _avail_size1(struct supertype *st, __u64 devsize,
+                         unsigned long long data_offset, int chunksize)
 {
        struct mdp_superblock_1 *super = st->sb;
        int bmspace = 0;
@@ -1672,7 +1737,9 @@ static __u64 avail_size1(struct supertype *st, __u64 devsize,
                 * Limit slack to 128M, but aim for about 0.1%
                 */
                unsigned long long headroom = 128*1024*2;
-               while ((headroom << 10) > devsize)
+               while ((headroom << 10) > devsize &&
+                      (chunksize == 0 ||
+                       headroom / 2 >= ((unsigned)chunksize*2)*2))
                        headroom >>= 1;
                devsize -= headroom;
        }
@@ -1689,6 +1756,11 @@ static __u64 avail_size1(struct supertype *st, __u64 devsize,
        }
        return 0;
 }
+static __u64 avail_size1(struct supertype *st, __u64 devsize,
+                        unsigned long long data_offset)
+{
+       return _avail_size1(st, devsize, data_offset, 0);
+}
 
 static int
 add_internal_bitmap1(struct supertype *st,
@@ -1948,7 +2020,7 @@ static int validate_geometry1(struct supertype *st, int level,
        }
        close(fd);
 
-       *freesize = avail_size1(st, ldsize >> 9, data_offset);
+       *freesize = _avail_size1(st, ldsize >> 9, data_offset, *chunk);
        return 1;
 }
 #endif /* MDASSEMBLE */