]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super0.c
Add 'supertype' arg to almost all metadata methods.
[thirdparty/mdadm.git] / super0.c
index 600aae028cafc047d2dfde724e3c050d1ed7b2b4..b81037ed67a08a5dcc0359ea93adcaa3ed1c9c3d 100644 (file)
--- a/super0.c
+++ b/super0.c
@@ -38,7 +38,7 @@
  *   - initialising a new superblock
  *   - printing the superblock for --examine
  *   - printing part of the superblock for --detail
- * .. other stuff 
+ * .. other stuff
  */
 
 
@@ -56,7 +56,7 @@ static unsigned long calc_sb0_csum(mdp_super_t *super)
 void super0_swap_endian(struct mdp_superblock_s *sb)
 {
        /* as super0 superblocks are host-endian, it is sometimes
-        * useful to be able to swap the endianness 
+        * useful to be able to swap the endianness
         * as (almost) everything is u32's we byte-swap every 4byte
         * number.
         * We then also have to swap the events_hi and events_lo
@@ -85,7 +85,7 @@ void super0_swap_endian(struct mdp_superblock_s *sb)
 
 #ifndef MDASSEMBLE
 
-static void examine_super0(void *sbv, char *homehost)
+static void examine_super0(struct supertype *st, void *sbv, char *homehost)
 {
        mdp_super_t *sb = sbv;
        time_t atime;
@@ -110,13 +110,17 @@ static void examine_super0(void *sbv, char *homehost)
        } else
                printf("           UUID : %08x\n", sb->set_uuid0);
 
+       if (sb->not_persistent)
+               printf("           Eedk : not persistent\n");
+
        atime = sb->ctime;
        printf("  Creation Time : %.24s\n", ctime(&atime));
        c=map_num(pers, sb->level);
        printf("     Raid Level : %s\n", c?c:"-unknown-");
        if ((int)sb->level >= 0) {
                int ddsks=0;
-               printf("    Device Size : %d%s\n", sb->size, human_size((long long)sb->size<<10));
+               printf("  Used Dev Size : %d%s\n", sb->size,
+                      human_size((long long)sb->size<<10));
                switch(sb->level) {
                case 1: ddsks=1;break;
                case 4:
@@ -227,7 +231,7 @@ static void examine_super0(void *sbv, char *homehost)
        }
 }
 
-static void brief_examine_super0(void *sbv)
+static void brief_examine_super0(struct supertype *st, void *sbv)
 {
        mdp_super_t *sb = sbv;
        char *c=map_num(pers, sb->level);
@@ -246,7 +250,7 @@ static void brief_examine_super0(void *sbv)
        printf("\n");
 }
 
-static void detail_super0(void *sbv, char *homehost)
+static void detail_super0(struct supertype *st, void *sbv, char *homehost)
 {
        mdp_super_t *sb = sbv;
        printf("           UUID : ");
@@ -266,7 +270,7 @@ static void detail_super0(void *sbv, char *homehost)
        printf("\n         Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
 }
 
-static void brief_detail_super0(void *sbv)
+static void brief_detail_super0(struct supertype *st, void *sbv)
 {
        mdp_super_t *sb = sbv;
        printf(" UUID=");
@@ -276,9 +280,21 @@ static void brief_detail_super0(void *sbv)
        else
                printf("%08x", sb->set_uuid0);
 }
+
+static void export_super0(struct supertype *st, void *sbv)
+{
+       mdp_super_t *sb = sbv;
+       printf("MD_UUID=");
+       if (sb->minor_version >= 90)
+               printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
+                      sb->set_uuid2, sb->set_uuid3);
+       else
+               printf("%08x", sb->set_uuid0);
+       printf("\n");
+}
 #endif
 
-static int match_home0(void *sbv, char *homehost)
+static int match_home0(struct supertype *st, void *sbv, char *homehost)
 {
        mdp_super_t *sb = sbv;
        char buf[20];
@@ -289,7 +305,7 @@ static int match_home0(void *sbv, char *homehost)
        return (memcmp(&sb->set_uuid2, hash, 8)==0);
 }
 
-static void uuid_from_super0(int uuid[4], void * sbv)
+static void uuid_from_super0(struct supertype *st, int uuid[4], void * sbv)
 {
        mdp_super_t *super = sbv;
        uuid[0] = super->set_uuid0;
@@ -304,7 +320,7 @@ static void uuid_from_super0(int uuid[4], void * sbv)
        }
 }
 
-static void getinfo_super0(struct mdinfo *info, void *sbv)
+static void getinfo_super0(struct supertype *st, struct mdinfo *info, void *sbv)
 {
        mdp_super_t *sb = sbv;
        int working = 0;
@@ -320,6 +336,7 @@ static void getinfo_super0(struct mdinfo *info, void *sbv)
        info->array.ctime = sb->ctime;
        info->array.utime = sb->utime;
        info->array.chunk_size = sb->chunk_size;
+       info->array.state = sb->state;
        info->component_size = sb->size*2;
 
        info->disk.state = sb->this_disk.state;
@@ -331,7 +348,7 @@ static void getinfo_super0(struct mdinfo *info, void *sbv)
        info->events = md_event(sb);
        info->data_offset = 0;
 
-       uuid_from_super0(info->uuid, sbv);
+       uuid_from_super0(st, info->uuid, sbv);
 
        if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
                info->reshape_active = 1;
@@ -355,7 +372,8 @@ static void getinfo_super0(struct mdinfo *info, void *sbv)
 }
 
 
-static int update_super0(struct mdinfo *info, void *sbv, char *update,
+static int update_super0(struct supertype *st, struct mdinfo *info,
+                        void *sbv, char *update,
                         char *devname, int verbose,
                         int uuid_set, char *homehost)
 {
@@ -386,14 +404,14 @@ static int update_super0(struct mdinfo *info, void *sbv, char *update,
        if (strcmp(update, "summaries") == 0) {
                int i;
                /* set nr_disks, active_disks, working_disks,
-                * failed_disks, spare_disks based on disks[] 
+                * failed_disks, spare_disks based on disks[]
                 * array in superblock.
                 * Also make sure extra slots aren't 'failed'
                 */
                sb->nr_disks = sb->active_disks =
                        sb->working_disks = sb->failed_disks =
                        sb->spare_disks = 0;
-               for (i=0; i < MD_SB_DISKS ; i++) 
+               for (i=0; i < MD_SB_DISKS ; i++)
                        if (sb->disks[i].major ||
                            sb->disks[i].minor) {
                                int state = sb->disks[i].state;
@@ -442,7 +460,16 @@ static int update_super0(struct mdinfo *info, void *sbv, char *update,
                        rv = 1;
                }
        }
-       if (strcmp(update, "grow") == 0) {
+       if (strcmp(update, "linear-grow-new") == 0) {
+               memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
+               sb->disks[info->disk.number].number = info->disk.number;
+               sb->disks[info->disk.number].major = info->disk.major;
+               sb->disks[info->disk.number].minor = info->disk.minor;
+               sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
+               sb->disks[info->disk.number].state = info->disk.state;
+               sb->this_disk = sb->disks[info->disk.number];
+       }
+       if (strcmp(update, "linear-grow-update") == 0) {
                sb->raid_disks = info->array.raid_disks;
                sb->nr_disks = info->array.nr_disks;
                sb->active_disks = info->array.active_disks;
@@ -453,8 +480,6 @@ static int update_super0(struct mdinfo *info, void *sbv, char *update,
                sb->disks[info->disk.number].minor = info->disk.minor;
                sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
                sb->disks[info->disk.number].state = info->disk.state;
-               if (sb->this_disk.number == info->disk.number)
-                       sb->this_disk = sb->disks[info->disk.number];
        }
        if (strcmp(update, "resync") == 0) {
                /* make sure resync happens */
@@ -483,7 +508,7 @@ static int update_super0(struct mdinfo *info, void *sbv, char *update,
                if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
                        struct bitmap_super_s *bm;
                        bm = (struct bitmap_super_s*)(sb+1);
-                       uuid_from_super0((int*)bm->uuid, sbv);
+                       uuid_from_super0(st, (int*)bm->uuid, sbv);
                }
        }
        if (strcmp(update, "_reshape_progress")==0)
@@ -503,11 +528,11 @@ static int update_super0(struct mdinfo *info, void *sbv, char *update,
 
 
 static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
-                      unsigned long long size, char *ignored_name, char *homehost)
+                      unsigned long long size, char *ignored_name, char *homehost,
+                      int *uuid)
 {
        mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
        int spares;
-       int rfd;
        memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
 
        if (info->major_version == -1) {
@@ -523,14 +548,11 @@ static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
                return 0;
        }
 
-       rfd = open("/dev/urandom", O_RDONLY);
        sb->md_magic = MD_SB_MAGIC;
        sb->major_version = 0;
        sb->minor_version = 90;
        sb->patch_version = 0;
        sb->gvalid_words = 0; /* ignored */
-       if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
-               sb->set_uuid0 = random();
        sb->ctime = time(0);
        sb->level = info->level;
        if (size != info->size)
@@ -540,13 +562,23 @@ static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
        sb->raid_disks = info->raid_disks;
        sb->md_minor = info->md_minor;
        sb->not_persistent = 0;
-       if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
-               sb->set_uuid1 = random();
-               sb->set_uuid2 = random();
-               sb->set_uuid3 = random();
+       if (uuid) {
+               sb->set_uuid0 = uuid[0];
+               sb->set_uuid1 = uuid[1];
+               sb->set_uuid2 = uuid[2];
+               sb->set_uuid3 = uuid[3];
+       } else {
+               int rfd = open("/dev/urandom", O_RDONLY);
+               if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
+                       sb->set_uuid0 = random();
+               if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
+                       sb->set_uuid1 = random();
+                       sb->set_uuid2 = random();
+                       sb->set_uuid3 = random();
+               }
+               if (rfd >= 0)
+                       close(rfd);
        }
-       if (rfd >= 0)
-               close(rfd);
        if (homehost) {
                char buf[20];
                char *hash = sha1_buffer(homehost,
@@ -572,7 +604,7 @@ static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
 }
 
 /* Add a device to the superblock being created */
-static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo)
+static void add_to_super0(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo)
 {
        mdp_super_t *sb = sbv;
        mdp_disk_t *dk = &sb->disks[dinfo->number];
@@ -586,22 +618,14 @@ static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo)
 
 static int store_super0(struct supertype *st, int fd, void *sbv)
 {
-       unsigned long size;
        unsigned long long dsize;
        unsigned long long offset;
        mdp_super_t *super = sbv;
-    
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
-       {
-               if (ioctl(fd, BLKGETSIZE, &size))
-                       return 1;
-               else
-                       dsize = ((unsigned long long)size)<<9;
-       }
 
-       if (dsize < MD_RESERVED_SECTORS*2*512)
+       if (!get_dev_size(fd, NULL, &dsize))
+               return 1;
+
+       if (dsize < MD_RESERVED_SECTORS*512)
                return 2;
 
        offset = MD_NEW_SIZE_SECTORS(dsize>>9);
@@ -673,8 +697,8 @@ static int compare_super0(void **firstp, void *secondv)
                return 0;
        }
 
-       uuid_from_super0(uuid1, first);
-       uuid_from_super0(uuid2, second);
+       uuid_from_super0(NULL, uuid1, first);
+       uuid_from_super0(NULL, uuid2, second);
        if (!same_uuid(uuid1, uuid2, 0))
                return 2;
        if (first->major_version != second->major_version ||
@@ -699,30 +723,20 @@ static int load_super0(struct supertype *st, int fd, void **sbp, char *devname)
         *  1 on cannot get superblock
         *  2 on superblock meaningless
         */
-       unsigned long size;
        unsigned long long dsize;
        unsigned long long offset;
        mdp_super_t *super;
        int uuid[4];
        struct bitmap_super_s *bsb;
-    
-#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;
-               } else
-                       dsize = size << 9;
-       }
 
-       if (dsize < MD_RESERVED_SECTORS*2) {
+       if (!get_dev_size(fd, devname, &dsize))
+               return 1;
+
+       if (dsize < MD_RESERVED_SECTORS*512) {
                if (devname)
-                       fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n",
-                               devname, size);
+                       fprintf(stderr, Name
+                           ": %s is too small for md: size is %llu sectors.\n",
+                               devname, dsize);
                return 1;
        }
 
@@ -785,7 +799,7 @@ static int load_super0(struct supertype *st, int fd, void **sbp, char *devname)
            != sizeof(struct bitmap_super_s))
                goto no_bitmap;
 
-       uuid_from_super0(uuid, super);
+       uuid_from_super0(st, uuid, super);
        bsb = (struct bitmap_super_s *)(super+1);
        if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
            memcmp(bsb->uuid, uuid, 16) != 0)
@@ -822,12 +836,15 @@ static struct supertype *match_metadata_desc0(char *arg)
 
 static __u64 avail_size0(struct supertype *st, __u64 devsize)
 {
-       if (devsize < MD_RESERVED_SECTORS*2)
+       if (devsize < MD_RESERVED_SECTORS)
                return 0ULL;
        return MD_NEW_SIZE_SECTORS(devsize);
 }
 
-static int add_internal_bitmap0(struct supertype *st, void *sbv, int chunk, int delay, int write_behind, unsigned long long size, int may_change, int major)
+static int add_internal_bitmap0(struct supertype *st, void *sbv, int *chunkp,
+                               int delay, int write_behind,
+                               unsigned long long size, int may_change,
+                               int major)
 {
        /*
         * The bitmap comes immediately after the superblock and must be 60K in size
@@ -838,6 +855,7 @@ static int add_internal_bitmap0(struct supertype *st, void *sbv, int chunk, int
        unsigned long long bits;
        unsigned long long max_bits = 60*1024*8;
        unsigned long long min_chunk;
+       int chunk = *chunkp;
        mdp_super_t *sb = sbv;
        bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
 
@@ -858,12 +876,12 @@ static int add_internal_bitmap0(struct supertype *st, void *sbv, int chunk, int
        memset(bms, 0, sizeof(*bms));
        bms->magic = __cpu_to_le32(BITMAP_MAGIC);
        bms->version = __cpu_to_le32(major);
-       uuid_from_super0((int*)bms->uuid, sb);
+       uuid_from_super0(st, (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);
-
+       *chunkp = chunk;
        return 1;
 }
 
@@ -871,19 +889,12 @@ static int add_internal_bitmap0(struct supertype *st, void *sbv, int chunk, int
 void locate_bitmap0(struct supertype *st, int fd, void *sbv)
 {
        unsigned long long dsize;
-       unsigned long size;
        unsigned long long offset;
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
-       {
-               if (ioctl(fd, BLKGETSIZE, &size))
-                       return;
-               else
-                       dsize = ((unsigned long long)size)<<9;
-       }
 
-       if (dsize < MD_RESERVED_SECTORS*2)
+       if (!get_dev_size(fd, NULL, &dsize))
+               return;
+
+       if (dsize < MD_RESERVED_SECTORS*512)
                return;
 
        offset = MD_NEW_SIZE_SECTORS(dsize>>9);
@@ -897,28 +908,21 @@ void locate_bitmap0(struct supertype *st, int fd, void *sbv)
 
 int write_bitmap0(struct supertype *st, int fd, void *sbv)
 {
-       unsigned long size;
        unsigned long long dsize;
        unsigned long long offset;
        mdp_super_t *sb = sbv;
-    
+
        int rv = 0;
 
        int towrite, n;
        char buf[4096];
 
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
-       {
-               if (ioctl(fd, BLKGETSIZE, &size))
-                       return 1;
-               else
-                       dsize = ((unsigned long long)size)<<9;
-       }
+       if (!get_dev_size(fd, NULL, &dsize))
+               return 1;
 
-       if (dsize < MD_RESERVED_SECTORS*2)
-       return -1;
+
+       if (dsize < MD_RESERVED_SECTORS*512)
+               return -1;
 
        offset = MD_NEW_SIZE_SECTORS(dsize>>9);
 
@@ -935,7 +939,7 @@ int write_bitmap0(struct supertype *st, int fd, void *sbv)
        memset(buf, 0xff, sizeof(buf));
        while (towrite > 0) {
                n = towrite;
-               if (n > sizeof(buf)) 
+               if (n > sizeof(buf))
                        n = sizeof(buf);
                n = write(fd, buf, n);
                if (n > 0)
@@ -950,12 +954,19 @@ int write_bitmap0(struct supertype *st, int fd, void *sbv)
        return rv;
 }
 
+static void free_super0(struct supertype *st, void *super)
+{
+       if (super)
+               free(super);
+}
+
 struct superswitch super0 = {
 #ifndef MDASSEMBLE
        .examine_super = examine_super0,
        .brief_examine_super = brief_examine_super0,
        .detail_super = detail_super0,
        .brief_detail_super = brief_detail_super0,
+       .export_super = export_super0,
 #endif
        .match_home = match_home0,
        .uuid_from_super = uuid_from_super0,
@@ -972,6 +983,7 @@ struct superswitch super0 = {
        .add_internal_bitmap = add_internal_bitmap0,
        .locate_bitmap = locate_bitmap0,
        .write_bitmap = write_bitmap0,
+       .free_super = free_super0,
        .major = 0,
        .swapuuid = 0,
 };