]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super1.c
Enable support for v.large raid1.
[thirdparty/mdadm.git] / super1.c
index 3c24f3481553dc1b953e9da860064f34cf9f17cd..190ec60fe09bdbd8f17325d05672595f00ddd419 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -54,7 +54,11 @@ struct mdp_superblock_1 {
 
        __u32   chunksize;      /* in 512byte sectors */
        __u32   raid_disks;
-       __u8    pad1[128-96];   /* set to 0 when written */
+       __u32   bitmap_offset;  /* sectors after start of superblock that bitmap starts
+                                * NOTE: signed, so bitmap can be before superblock
+                                * only meaningful of feature_map[0] is set.
+                                */
+       __u8    pad1[128-100];  /* set to 0 when written */
 
        /* constant this-device information - 64 bytes */
        __u64   data_offset;    /* sector start of data, often 0 */
@@ -64,7 +68,9 @@ struct mdp_superblock_1 {
        __u32   dev_number;     /* permanent identifier of this  device - not role in raid */
        __u32   cnt_corrected_read; /* number of read errors that were corrected by re-writing */
        __u8    device_uuid[16]; /* user-space setable, ignored by kernel */
-       __u8    pad2[64-56];    /* set to 0 when writing */
+        __u8    devflags;        /* per-device flags.  Only one defined...*/
+#define WriteMostly1    1        /* mask for writemostly flag in above */
+       __u8    pad2[64-57];    /* set to 0 when writing */
 
        /* array state information - 64 bytes */
        __u64   utime;          /* 40 bits second, 24 btes microseconds */
@@ -83,6 +89,14 @@ struct mdp_superblock_1 {
        __u16   dev_roles[0];   /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
 };
 
+/* feature_map bits */
+#define MD_FEATURE_BITMAP_OFFSET       1
+#define        MD_FEATURE_RECOVERY_OFFSET      2 /* recovery_offset is present and
+                                          * must be honoured
+                                          */
+
+#define        MD_FEATURE_ALL                  (1|2)
+
 #ifndef offsetof
 #define offsetof(t,f) ((int)&(((t*)0)->f))
 #endif
@@ -129,8 +143,8 @@ static void examine_super1(void *sbv)
        printf("        Version : %02d.%02d\n", 1, __le32_to_cpu(sb->feature_map));
        printf("     Array UUID : ");
        for (i=0; i<16; i++) {
-               printf("%02x", sb->set_uuid[i]);
                if ((i&3)==0 && i != 0) printf(":");
+               printf("%02x", sb->set_uuid[i]);
        }
        printf("\n");
        printf("           Name : %.32s\n", sb->set_name);
@@ -142,17 +156,27 @@ static void examine_super1(void *sbv)
        printf("   Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
        printf("\n");
        printf("    Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
+       if (sb->size != sb->data_size)
+               printf("      Used Size : %llu%s\n", (unsigned long long)sb->size, human_size(sb->size<<9));
        if (sb->data_offset)
                printf("    Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->data_offset));
        if (sb->super_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)
+               printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
        printf("          State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
        printf("    Device UUID : ");
        for (i=0; i<16; i++) {
-               printf("%02x", sb->set_uuid[i]);
                if ((i&3)==0 && i != 0) printf(":");
+               printf("%02x", sb->device_uuid[i]);
        }
        printf("\n");
+       if (sb->devflags) {
+               printf("      Flags :");
+               if (sb->devflags & WriteMostly1)
+                       printf(" write-mostly");
+               printf("\n");
+       }
 
        atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
        printf("    Update Time : %.24s\n", ctime(&atime));
@@ -168,10 +192,17 @@ static void examine_super1(void *sbv)
                c = map_num(r5layout, __le32_to_cpu(sb->layout));
                printf("         Layout : %s\n", c?c:"-unknown-");
        }
+       if (__le32_to_cpu(sb->level) == 10) {
+               int lo = __le32_to_cpu(sb->layout);
+               printf("         Layout : near=%d, far=%d\n",
+                      lo&255, (lo>>8)&255);
+       }
        switch(__le32_to_cpu(sb->level)) {
        case 0:
        case 4:
        case 5:
+       case 6:
+       case 10:
                printf("     Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize/2));
                break;
        case -1:
@@ -222,6 +253,8 @@ static void brief_examine_super1(void *sbv)
                printf("%02x", sb->set_uuid[i]);
                if ((i&3)==0 && i != 0) printf(":");
        }
+       if (sb->set_name[0])
+               printf(" name=%.32s", sb->set_name);
        printf("\n");
 }
 
@@ -230,10 +263,11 @@ static void detail_super1(void *sbv)
        struct mdp_superblock_1 *sb = sbv;
        int i;
 
+       printf("           Name : %.32s\n", sb->set_name);
        printf("           UUID : ");
        for (i=0; i<16; i++) {
-               printf("%02x", sb->set_uuid[i]);
                if ((i&3)==0 && i != 0) printf(":");
+               printf("%02x", sb->set_uuid[i]);
        }
        printf("\n         Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
 }
@@ -243,10 +277,12 @@ static void brief_detail_super1(void *sbv)
        struct mdp_superblock_1 *sb = sbv;
        int i;
 
+       if (sb->set_name[0])
+               printf(" name=%.32s", sb->set_name);
        printf(" UUID=");
        for (i=0; i<16; i++) {
-               printf("%02x", sb->set_uuid[i]);
                if ((i&3)==0 && i != 0) printf(":");
+               printf("%02x", sb->set_uuid[i]);
        }
 }
 
@@ -261,7 +297,7 @@ static void uuid_from_super1(int uuid[4], void * sbv)
                cuuid[i] = super->set_uuid[i];
 }
 
-static void getinfo_super1(struct mdinfo *info, void *sbv)
+static void getinfo_super1(struct mdinfo *info, mddev_ident_t ident, void *sbv)
 {
        struct mdp_superblock_1 *sb = sbv;
        int working = 0;
@@ -273,6 +309,7 @@ static void getinfo_super1(struct mdinfo *info, void *sbv)
        info->array.patch_version = 0;
        info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
        info->array.level = __le32_to_cpu(sb->level);
+       info->array.layout = __le32_to_cpu(sb->layout);
        info->array.md_minor = -1;
        info->array.ctime = __le64_to_cpu(sb->ctime);
 
@@ -301,6 +338,9 @@ static void getinfo_super1(struct mdinfo *info, void *sbv)
 
        memcpy(info->uuid, sb->set_uuid, 16);
 
+       strncpy(ident->name, sb->set_name, 32);
+       ident->name[32] = 0;
+
        for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
                role = __le16_to_cpu(sb->dev_roles[i]);
                if (/*role == 0xFFFF || */role < info->array.raid_disks)
@@ -316,7 +356,7 @@ static int update_super1(struct mdinfo *info, void *sbv, char *update, char *dev
        struct mdp_superblock_1 *sb = sbv;
 
        if (strcmp(update, "force")==0) {
-               sb->events = __cpu_to_le32(info->events);
+               sb->events = __cpu_to_le64(info->events);
                switch(__le32_to_cpu(sb->level)) {
                case 5: case 4: case 6:
                        /* need to force clean */
@@ -355,6 +395,8 @@ static int update_super1(struct mdinfo *info, void *sbv, char *update, char *dev
                /* make sure resync happens */
                sb->resync_offset = ~0ULL;
        }
+       if (strcmp(update, "uuid") == 0)
+               memcpy(sb->set_uuid, info->uuid, 16);
 
        sb->sb_csum = calc_sb_1_csum(sb);
        return rv;
@@ -367,10 +409,11 @@ static __u64 event_super1(void *sbv)
        return __le64_to_cpu(sb->events);
 }
 
-static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info)
+static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info, unsigned long long size, char *name)
 {
-       struct mdp_superblock_1 *sb = malloc(1024);
+       struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
        int spares;
+       int rfd;
        memset(sb, 0, 1024);
 
        if (info->major_version == -1)
@@ -389,17 +432,23 @@ static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info)
        sb->major_version = __cpu_to_le32(1);
        sb->feature_map = 0;
        sb->pad0 = 0;
-       *(__u32*)(sb->set_uuid) = random();
-       *(__u32*)(sb->set_uuid+4) = random();
-       *(__u32*)(sb->set_uuid+8) = random();
-       *(__u32*)(sb->set_uuid+12) = random();
 
-       /* FIXME name */
+       if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
+           read(rfd, sb->set_uuid, 16) != 16) {
+               *(__u32*)(sb->set_uuid) = random();
+               *(__u32*)(sb->set_uuid+4) = random();
+               *(__u32*)(sb->set_uuid+8) = random();
+               *(__u32*)(sb->set_uuid+12) = random();
+       }
+       if (rfd >= 0) close(rfd);
+
+       memset(sb->set_name, 0, 32);
+       strcpy(sb->set_name, name);
 
        sb->ctime = __cpu_to_le64((unsigned long long)time(0));
        sb->level = __cpu_to_le32(info->level);
        sb->layout = __cpu_to_le32(info->layout);
-       sb->size = __cpu_to_le64(info->size*2ULL);
+       sb->size = __cpu_to_le64(size*2ULL);
        sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
        sb->raid_disks = __cpu_to_le32(info->raid_disks);
 
@@ -414,7 +463,7 @@ static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info)
                sb->resync_offset = ~0ULL;
        else
                sb->resync_offset = 0;
-       sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/ 
+       sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
                                    sizeof(sb->dev_roles[0]));
        memset(sb->pad3, 0, sizeof(sb->pad3));
 
@@ -429,11 +478,11 @@ static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
 {
        struct mdp_superblock_1 *sb = sbv;
        __u16 *rp = sb->dev_roles + dk->number;
-       if (dk->state == 6) /* active, sync */
+       if ((dk->state & 6) == 6) /* active, sync */
                *rp = __cpu_to_le16(dk->raid_disk);
        else if ((dk->state & ~2) == 0) /* active or idle -> spare */
                *rp = 0xffff;
-       else 
+       else
                *rp = 0xfffe;
 }
 
@@ -442,13 +491,21 @@ static int store_super1(struct supertype *st, int fd, void *sbv)
        struct mdp_superblock_1 *sb = sbv;
        unsigned long long sb_offset;
        int sbsize;
-       long size;
-
-       if (ioctl(fd, BLKGETSIZE, &size))
-               return 1;
+       unsigned long size;
+       unsigned long long dsize;
 
+#ifdef BLKGETSIZE64
+       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
+#endif
+       {
+               if (ioctl(fd, BLKGETSIZE, &size))
+                       return 1;
+               else
+                       dsize = (unsigned long long)size;
+       } else
+               dsize >>= 9;
 
-       if (size < 24)
+       if (dsize < 24)
                return 2;
 
        /*
@@ -461,7 +518,7 @@ static int store_super1(struct supertype *st, int fd, void *sbv)
         */
        switch(st->minor_version) {
        case 0:
-               sb_offset = size;
+               sb_offset = dsize;
                sb_offset -= 8*2;
                sb_offset &= ~(4*2-1);
                break;
@@ -476,7 +533,7 @@ static int store_super1(struct supertype *st, int fd, void *sbv)
        }
 
 
-    
+
        if (sb_offset != __le64_to_cpu(sb->super_offset) &&
            0 != __le64_to_cpu(sb->super_offset)
                ) {
@@ -498,15 +555,17 @@ static int store_super1(struct supertype *st, int fd, void *sbv)
 
 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
 
-static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname)
+static int write_init_super1(struct supertype *st, void *sbv,
+                            mdu_disk_info_t *dinfo, char *devname)
 {
        struct mdp_superblock_1 *sb = sbv;
-       struct mdp_superblock_1 *refsb = NULL;
+       void *refsbv = NULL;
        int fd = open(devname, O_RDWR | O_EXCL);
        int rfd;
        int rv;
 
-       long size;
+       unsigned long size;
+       unsigned long long dsize;
        long long sb_offset;
 
 
@@ -517,6 +576,8 @@ static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *d
        }
 
        sb->dev_number = __cpu_to_le32(dinfo->number);
+       if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
+               sb->devflags |= WriteMostly1;
 
        if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
            read(rfd, sb->device_uuid, 16) != 16) {
@@ -528,7 +589,9 @@ static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *d
        if (rfd >= 0) close(rfd);
        sb->events = 0;
 
-       if (load_super1(st, fd, (void**)&refsb, NULL)==0) {
+       if (load_super1(st, fd, &refsbv, NULL)==0) {
+               struct mdp_superblock_1 *refsb = refsbv;
+
                memcpy(sb->device_uuid, refsb->device_uuid, 16);
                if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
                        /* same array, so preserve events and dev_number */
@@ -537,13 +600,19 @@ static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *d
                }
                free(refsb);
        }
-    
-       if (ioctl(fd, BLKGETSIZE, &size)) {
-               close(fd);
-               return 1;
-       }
 
-       if (size < 24) {
+#ifdef BLKGETSIZE64
+       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
+#endif
+       {
+               if (ioctl(fd, BLKGETSIZE, &size))
+                       return 1;
+               else
+                       dsize = size;
+       } else
+               dsize >>= 9;
+
+       if (dsize < 24) {
                close(fd);
                return 2;
        }
@@ -559,23 +628,23 @@ static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *d
         */
        switch(st->minor_version) {
        case 0:
-               sb_offset = size;
+               sb_offset = dsize;
                sb_offset -= 8*2;
                sb_offset &= ~(4*2-1);
                sb->super_offset = __cpu_to_le64(sb_offset);
                sb->data_offset = __cpu_to_le64(0);
-               sb->data_size = sb->super_offset;
+               sb->data_size = __cpu_to_le64(sb_offset);
                break;
        case 1:
                sb->super_offset = __cpu_to_le64(0);
-               sb->data_offset = __cpu_to_le64(2);
-               sb->data_size = __cpu_to_le64(size - 2);
+               sb->data_offset = __cpu_to_le64(4*2); /* leave 4k for super and bitmap */
+               sb->data_size = __cpu_to_le64(dsize - 4*2);
                break;
        case 2:
                sb_offset = 4*2;
                sb->super_offset = __cpu_to_le64(sb_offset);
-               sb->data_offset = __cpu_to_le64(sb_offset+2);
-               sb->data_size = __cpu_to_le64(size - 4*2 - 2);
+               sb->data_offset = __cpu_to_le64(sb_offset+4*2);
+               sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2);
                break;
        default:
                return -EINVAL;
@@ -586,6 +655,9 @@ static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *d
        rv = store_super1(st, fd, sb);
        if (rv)
                fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
+
+       if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
+               rv = st->ss->write_bitmap(st, fd, sbv);
        close(fd);
        return rv;
 }
@@ -629,6 +701,7 @@ static int compare_super1(void **firstp, void *secondv)
 static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
 {
        unsigned long size;
+       unsigned long long dsize;
        unsigned long long sb_offset;
        struct mdp_superblock_1 *super;
 
@@ -666,17 +739,24 @@ static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
                st->ss = NULL;
                return 2;
        }
-       if (ioctl(fd, BLKGETSIZE, &size)) {
-               if (devname) 
-                       fprintf(stderr, Name ": cannot find device size for %s: %s\n",
-                               devname, strerror(errno));
-               return 1;
-       }
+#ifdef BLKGETSIZE64
+       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
+#endif
+       {
+               if (ioctl(fd, BLKGETSIZE, &size)) {
+                       if (devname)
+                               fprintf(stderr, Name ": cannot find device size for %s: %s\n",
+                                       devname, strerror(errno));
+                       return 1;
+               }
+               dsize = size;
+       } else
+               dsize >>= 9;
 
-       if (size < 24) {
+       if (dsize < 24) {
                if (devname)
-                       fprintf(stderr, Name ": %s is too small for md: size is %lu sectors.\n",
-                               devname, size);
+                       fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
+                               devname, dsize);
                return 1;
        }
 
@@ -690,7 +770,7 @@ static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
         */
        switch(st->minor_version) {
        case 0:
-               sb_offset = size;
+               sb_offset = dsize;
                sb_offset -= 8*2;
                sb_offset &= ~(4*2-1);
                break;
@@ -714,7 +794,7 @@ static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
                return 1;
        }
 
-       super = malloc(1024);
+       super = malloc(1024 + sizeof(bitmap_super_t));
 
        if (read(fd, super, 1024) != 1024) {
                if (devname)
@@ -776,12 +856,130 @@ static struct supertype *match_metadata_desc1(char *arg)
        return NULL;
 }
 
-static __u64 avail_size1(__u64 devsize)
+/* find available size on device with this devsize, using
+ * superblock type st, and reserving 'reserve' sectors for
+ * a possible bitmap
+ */
+static __u64 avail_size1(struct supertype *st, __u64 devsize)
 {
        if (devsize < 24)
                return 0;
 
-       return (devsize - 8*2 ) & ~(4*2-1);
+       switch(st->minor_version) {
+       case 0:
+               /* at end */
+               return ((devsize - 8*2 ) & ~(4*2-1));
+       case 1:
+               /* at start, 4K for superblock and possible bitmap */
+               return devsize - 4*2;
+       case 2:
+               /* 4k from start, 4K for superblock and possible bitmap */
+               return devsize - (4+4)*2;
+       }
+       return 0;
+}
+
+static int
+add_internal_bitmap1(struct supertype *st, void *sbv,
+                    int chunk, int delay, int write_behind, unsigned long long size, int may_change, int major)
+{
+       /*
+        * If not may_change, then this is a 'Grow', and the bitmap
+        * must fit after the superblock.
+        * If may_change, then this is create, and we can put the bitmap
+        * before the superblock if we like, or may move the start.
+        * For now, just squeeze the bitmap into 3k and don't change anything.
+        *
+        * size is in sectors,  chunk is in bytes !!!
+        */
+
+       unsigned long long bits;
+       unsigned long long max_bits = (3*512 - sizeof(bitmap_super_t)) * 8;
+       unsigned long long min_chunk;
+       struct mdp_superblock_1 *sb = sbv;
+       bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
+
+       if (st->minor_version && !may_change &&
+           __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
+               return 0; /* doesn't fit */
+
+
+
+       min_chunk = 4096; /* sub-page chunks don't work yet.. */
+       bits = (size*512)/min_chunk +1;
+       while (bits > max_bits) {
+               min_chunk *= 2;
+               bits = (bits+1)/2;
+       }
+       if (chunk == UnSet)
+               chunk = min_chunk;
+       else if (chunk < min_chunk)
+               return 0; /* chunk size too small */
+
+       sb->bitmap_offset = __cpu_to_le32(2);
+
+       sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
+       memset(bms, 0, sizeof(*bms));
+       bms->magic = __cpu_to_le32(BITMAP_MAGIC);
+       bms->version = __cpu_to_le32(major);
+       uuid_from_super1((int*)bms->uuid, sb);
+       bms->chunksize = __cpu_to_le32(chunk);
+       bms->daemon_sleep = __cpu_to_le32(delay);
+       bms->sync_size = __cpu_to_le64(size);
+       bms->write_behind = __cpu_to_le32(write_behind);
+
+       return 1;
+}
+
+
+void locate_bitmap1(struct supertype *st, int fd, void *sbv)
+{
+       unsigned long long offset;
+       struct mdp_superblock_1 *sb;
+
+       if (!sbv)
+               if (st->ss->load_super(st, fd, sbv, NULL))
+                       return; /* no error I hope... */
+
+       sb = sbv;
+
+       offset = __le64_to_cpu(sb->super_offset);
+       offset += (long) __le32_to_cpu(sb->bitmap_offset);
+       if (!sbv)
+               free(sb);
+       lseek64(fd, offset<<9, 0);
+}
+
+int write_bitmap1(struct supertype *st, int fd, void *sbv)
+{
+       struct mdp_superblock_1 *sb = sbv;
+       bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
+       int rv = 0;
+
+       int towrite, n;
+       char buf[4096];
+
+       locate_bitmap1(st, fd, sbv);
+
+       write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t));
+       towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
+       towrite = (towrite+7) >> 3; /* bits to bytes */
+       memset(buf, 0xff, sizeof(buf));
+       while (towrite > 0) {
+               n = towrite;
+               if (n > sizeof(buf))
+                       n = sizeof(buf);
+               n = write(fd, buf, n);
+               if (n > 0)
+                       towrite -= n;
+               else
+                       break;
+       }
+       fsync(fd);
+       if (towrite)
+               rv = -2;
+
+       return rv;
 }
 
 struct superswitch super1 = {
@@ -803,6 +1001,9 @@ struct superswitch super1 = {
        .load_super = load_super1,
        .match_metadata_desc = match_metadata_desc1,
        .avail_size = avail_size1,
+       .add_internal_bitmap = add_internal_bitmap1,
+       .locate_bitmap = locate_bitmap1,
+       .write_bitmap = write_bitmap1,
        .major = 1,
 #if __BYTE_ORDER == BIG_ENDIAN
        .swapuuid = 0,