]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super0.c
Don't break long strings onto multiple lines.
[thirdparty/mdadm.git] / super0.c
index ecb6b38202c26c9db6fbbd0307aba352bc95782a..deb59994e9707a5aa2d74073c98a7f674a767757 100644 (file)
--- a/super0.c
+++ b/super0.c
@@ -36,7 +36,6 @@
  * .. other stuff
  */
 
-
 static unsigned long calc_sb0_csum(mdp_super_t *super)
 {
        unsigned long csum = super->sb_csum;
@@ -47,7 +46,6 @@ static unsigned long calc_sb0_csum(mdp_super_t *super)
        return newcsum;
 }
 
-
 static void super0_swap_endian(struct mdp_superblock_s *sb)
 {
        /* as super0 superblocks are host-endian, it is sometimes
@@ -281,6 +279,51 @@ static void export_examine_super0(struct supertype *st)
               + sb->events_lo);
 }
 
+static int copy_metadata0(struct supertype *st, int from, int to)
+{
+       /* Read 64K from the appropriate offset of 'from'
+        * and if it looks a little like a 0.90 superblock,
+        * write it to the same offset of 'to'
+        */
+       void *buf;
+       unsigned long long dsize, offset;
+       const int bufsize = 64*1024;
+       mdp_super_t *super;
+
+       if (posix_memalign(&buf, 4096, bufsize) != 0)
+               return 1;
+
+       if (!get_dev_size(from, NULL, &dsize))
+               goto err;
+
+       if (dsize < MD_RESERVED_SECTORS*512)
+               goto err;
+
+       offset = MD_NEW_SIZE_SECTORS(dsize>>9);
+
+       offset *= 512;
+
+       if (lseek64(from, offset, 0) < 0LL)
+               goto err;
+       if (read(from, buf, bufsize) != bufsize)
+               goto err;
+
+       if (lseek64(to, offset, 0) < 0LL)
+               goto err;
+       super = buf;
+       if (super->md_magic != MD_SB_MAGIC ||
+           super->major_version != 0 ||
+           calc_sb0_csum(super) != super->sb_csum)
+               goto err;
+       if (write(to, buf, bufsize) != bufsize)
+               goto err;
+       free(buf);
+       return 0;
+err:
+       free(buf);
+       return 1;
+}
+
 static void detail_super0(struct supertype *st, char *homehost)
 {
        mdp_super_t *sb = st->sb;
@@ -435,6 +478,18 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
        int rv = 0;
        int uuid[4];
        mdp_super_t *sb = st->sb;
+
+       if (strcmp(update, "homehost") == 0 &&
+           homehost) {
+               /* note that 'homehost' is special as it is really
+                * a "uuid" update.
+                */
+               uuid_set = 0;
+               update = "uuid";
+               info->uuid[0] = sb->set_uuid0;
+               info->uuid[1] = sb->set_uuid1;
+       }
+
        if (strcmp(update, "sparc2.2")==0 ) {
                /* 2.2 sparc put the events in the wrong place
                 * So we copy the tail of the superblock
@@ -551,12 +606,6 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
                /* make sure resync happens */
                sb->state &= ~(1<<MD_SB_CLEAN);
                sb->recovery_cp = 0;
-       } else if (strcmp(update, "homehost") == 0 &&
-                  homehost) {
-               uuid_set = 0;
-               update = "uuid";
-               info->uuid[0] = sb->set_uuid0;
-               info->uuid[1] = sb->set_uuid1;
        } else if (strcmp(update, "uuid") == 0) {
                if (!uuid_set && homehost) {
                        char buf[20];
@@ -575,6 +624,58 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
                        uuid_from_super0(st, uuid);
                        memcpy(bm->uuid, uuid, 16);
                }
+       } else if (strcmp(update, "metadata") == 0) {
+               /* Create some v1.0 metadata to match ours but make the
+                * ctime bigger.  Also update info->array.*_version.
+                * We need to arrange that store_super writes out
+                * the v1.0 metadata.
+                * Not permitted for unclean array, or array with
+                * bitmap.
+                */
+               if (info->bitmap_offset) {
+                       pr_err("Cannot update metadata when bitmap is present\n");
+                       rv = -2;
+               } else if (info->array.state != 1) {
+                       pr_err("Cannot update metadata on unclean array\n");
+                       rv = -2;
+               } else {
+                       info->array.major_version = 1;
+                       info->array.minor_version = 0;
+                       uuid_from_super0(st, info->uuid);
+                       st->other = super1_make_v0(st, info, st->sb);
+               }
+       } else if (strcmp(update, "revert-reshape") == 0) {
+               rv = -2;
+               if (sb->minor_version <= 90)
+                       pr_err("No active reshape to revert on %s\n",
+                              devname);
+               else if (sb->delta_disks == 0)
+                       pr_err("%s: Can only revert reshape which changes number of devices\n",
+                              devname);
+               else {
+                       int tmp;
+                       int parity = sb->level == 6 ? 2 : 1;
+                       rv = 0;
+
+                       if (sb->level >= 4 && sb->level <= 6 &&
+                           sb->reshape_position % (
+                                   sb->new_chunk/512 *
+                                   (sb->raid_disks - sb->delta_disks - parity))) {
+                               pr_err("Reshape position is not suitably aligned.\n");
+                               pr_err("Try normal assembly and stop again\n");
+                               return -2;
+                       }
+                       sb->raid_disks -= sb->delta_disks;
+                       sb->delta_disks = -sb->delta_disks;
+
+                       tmp = sb->new_layout;
+                       sb->new_layout = sb->layout;
+                       sb->layout = tmp;
+
+                       tmp = sb->new_chunk;
+                       sb->new_chunk = sb->chunk_size;
+                       sb->chunk_size = tmp;
+               }
        } else if (strcmp(update, "no-bitmap") == 0) {
                sb->state &= ~(1<<MD_SB_BITMAP_PRESENT);
        } else if (strcmp(update, "_reshape_progress")==0)
@@ -598,7 +699,6 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
  * host name
  */
 
-
 static int init_super0(struct supertype *st, mdu_array_info_t *info,
                       unsigned long long size, char *ignored_name, char *homehost,
                       int *uuid, unsigned long long data_offset)
@@ -607,13 +707,13 @@ static int init_super0(struct supertype *st, mdu_array_info_t *info,
        int spares;
 
        if (data_offset != INVALID_SECTORS) {
-               fprintf(stderr, Name ": data-offset not support for 0.90\n");
+               pr_err("data-offset not support for 0.90\n");
                return 0;
        }
 
        if (posix_memalign((void**)&sb, 4096,
                           MD_SB_BYTES + ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
-               pr_err("%s could not allocate superblock\n", __func__);
+               pr_err("could not allocate superblock\n");
                return 0;
        }
        memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
@@ -705,9 +805,8 @@ static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
        dk->major = dinfo->major;
        dk->minor = dinfo->minor;
        dk->raid_disk = dinfo->raid_disk;
-       dk->state = dinfo->state;
-       /* In case our source disk was writemostly, don't copy that bit */
-       dk->state &= ~(1<<MD_DISK_WRITEMOSTLY);
+       dk->state = dinfo->state & ((1<<MD_DISK_ACTIVE) |
+                                   (1<<MD_DISK_SYNC));
 
        sb->this_disk = sb->disks[dinfo->number];
        sb->sb_csum = calc_sb0_csum(sb);
@@ -738,6 +837,24 @@ static int store_super0(struct supertype *st, int fd)
        if (dsize < MD_RESERVED_SECTORS*512)
                return 2;
 
+       if (st->other) {
+               /* Writing out v1.0 metadata for --update=metadata */
+               int ret = 0;
+
+               offset = dsize/512 - 8*2;
+               offset &= ~(4*2-1);
+               offset *= 512;
+               if (lseek64(fd, offset, 0)< 0LL)
+                       ret = 3;
+               else if (write(fd, st->other, 1024) != 1024)
+                       ret = 4;
+               else
+                       fsync(fd);
+               free(st->other);
+               st->other = NULL;
+               return ret;
+       }
+
        offset = MD_NEW_SIZE_SECTORS(dsize>>9);
 
        offset *= 512;
@@ -751,7 +868,7 @@ static int store_super0(struct supertype *st, int fd)
        if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
                struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
                if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
-                       if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) != 
+                       if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) !=
                            ROUND_UP(sizeof(*bm),4096))
                            return 5;
        }
@@ -769,7 +886,7 @@ static int write_init_super0(struct supertype *st)
 
        for (di = st->info ; di && ! rv ; di = di->next) {
 
-               if (di->disk.state == 1)
+               if (di->disk.state & (1 << MD_DISK_FAULTY))
                        continue;
                if (di->fd == -1)
                        continue;
@@ -810,9 +927,9 @@ static int compare_super0(struct supertype *st, struct supertype *tst)
                return 1;
        if (!first) {
                if (posix_memalign((void**)&first, 4096,
-                            MD_SB_BYTES + 
+                            MD_SB_BYTES +
                             ROUND_UP(sizeof(struct bitmap_super_s), 4096)) != 0) {
-                       pr_err("%s could not allocate superblock\n", __func__);
+                       pr_err("could not allocate superblock\n");
                        return 1;
                }
                memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
@@ -837,7 +954,6 @@ static int compare_super0(struct supertype *st, struct supertype *tst)
        return 0;
 }
 
-
 static void free_super0(struct supertype *st);
 
 static int load_super0(struct supertype *st, int fd, char *devname)
@@ -865,13 +981,12 @@ static int load_super0(struct supertype *st, int fd, char *devname)
                               devname, dsize);
                return 1;
        }
+       st->devsize = dsize;
 
        offset = MD_NEW_SIZE_SECTORS(dsize>>9);
 
        offset *= 512;
 
-       ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
-
        if (lseek64(fd, offset, 0)< 0LL) {
                if (devname)
                        pr_err("Cannot seek to superblock on %s: %s\n",
@@ -882,7 +997,7 @@ static int load_super0(struct supertype *st, int fd, char *devname)
        if (posix_memalign((void**)&super, 4096,
                           MD_SB_BYTES +
                           ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
-               pr_err("%s could not allocate superblock\n", __func__);
+               pr_err("could not allocate superblock\n");
                return 1;
        }
 
@@ -949,7 +1064,7 @@ static struct supertype *match_metadata_desc0(char *arg)
 {
        struct supertype *st = xcalloc(1, sizeof(*st));
 
-       st->container_dev = NoMdDev;
+       st->container_devnm[0] = 0;
        st->ss = &super0;
        st->info = NULL;
        st->minor_version = 90;
@@ -1009,7 +1124,6 @@ static int add_internal_bitmap0(struct supertype *st, int *chunkp,
        bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
        int uuid[4];
 
-
        min_chunk = 4096; /* sub-page chunks don't work yet.. */
        bits = (size * 512) / min_chunk + 1;
        while (bits > max_bits) {
@@ -1041,7 +1155,6 @@ static int add_internal_bitmap0(struct supertype *st, int *chunkp,
        return 1;
 }
 
-
 static void locate_bitmap0(struct supertype *st, int fd)
 {
        unsigned long long dsize;
@@ -1156,11 +1269,10 @@ static int validate_geometry0(struct supertype *st, int level,
        }
        if (size >= tbmax * 2ULL*1024*1024*1024) {
                if (verbose)
-                       pr_err("0.90 metadata supports at most "
-                              "%d terabytes per device\n", tbmax);
+                       pr_err("0.90 metadata supports at most %d terabytes per device\n", tbmax);
                return 0;
        }
-       if (chunk && *chunk == UnSet)
+       if (*chunk == UnSet)
                *chunk = DEFAULT_CHUNK;
 
        if (!subdev)
@@ -1197,6 +1309,7 @@ struct superswitch super0 = {
        .write_init_super = write_init_super0,
        .validate_geometry = validate_geometry0,
        .add_to_super = add_to_super0,
+       .copy_metadata = copy_metadata0,
 #endif
        .match_home = match_home0,
        .uuid_from_super = uuid_from_super0,