]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super0.c
Assemble/super0: allow non-in-sync devices to be assembled without complaint.
[thirdparty/mdadm.git] / super0.c
index 1e06375e80324bfe97301a2360e49d342fcbe6f8..9f78b9452dec9e04792760500310769a0b277258 100644 (file)
--- a/super0.c
+++ b/super0.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *    Author: Neil Brown
- *    Email: <neilb@cse.unsw.edu.au>
- *    Paper: Neil Brown
- *           School of Computer Science and Engineering
- *           The University of New South Wales
- *           Sydney, 2052
- *           Australia
+ *    Email: <neilb@suse.de>
  */
 
 #define HAVE_STDINT_H 1
@@ -53,7 +48,7 @@ static unsigned long calc_sb0_csum(mdp_super_t *super)
 }
 
 
-void super0_swap_endian(struct mdp_superblock_s *sb)
+static 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
@@ -90,10 +85,11 @@ static void examine_super0(struct supertype *st, char *homehost)
        mdp_super_t *sb = st->sb;
        time_t atime;
        int d;
+       int delta_extra = 0;
        char *c;
 
        printf("          Magic : %08x\n", sb->md_magic);
-       printf("        Version : %02d.%02d.%02d\n", sb->major_version, sb->minor_version,
+       printf("        Version : %d.%02d.%02d\n", sb->major_version, sb->minor_version,
               sb->patch_version);
        if (sb->minor_version >= 90) {
                printf("           UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
@@ -140,10 +136,9 @@ static void examine_super0(struct supertype *st, char *homehost)
                printf("  Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
                if (sb->delta_disks) {
                        printf("  Delta Devices : %d", sb->delta_disks);
-                       if (sb->delta_disks)
-                               printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
-                       else
-                               printf(" (%d->%d)\n", sb->raid_disks, sb->raid_disks+sb->delta_disks);
+                       printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
+                       if (((int)sb->delta_disks) < 0)
+                               delta_extra = - sb->delta_disks;
                }
                if (sb->new_level != sb->level) {
                        c = map_num(pers, sb->new_level);
@@ -154,6 +149,10 @@ static void examine_super0(struct supertype *st, char *homehost)
                                c = map_num(r5layout, sb->new_layout);
                                printf("     New Layout : %s\n", c?c:"-unknown-");
                        }
+                       if (sb->level == 6) {
+                               c = map_num(r6layout, sb->new_layout);
+                               printf("     New Layout : %s\n", c?c:"-unknown-");
+                       }
                        if (sb->level == 10) {
                                printf("     New Layout : near=%d, %s=%d\n",
                                       sb->new_layout&255,
@@ -187,11 +186,14 @@ static void examine_super0(struct supertype *st, char *homehost)
                c = map_num(r5layout, sb->layout);
                printf("         Layout : %s\n", c?c:"-unknown-");
        }
+       if (sb->level == 6) {
+               c = map_num(r6layout, sb->layout);
+               printf("         Layout : %s\n", c?c:"-unknown-");
+       }
        if (sb->level == 10) {
-               printf("         Layout : near=%d, %s=%d\n",
-                      sb->layout&255,
-                      (sb->layout&0x10000)?"offset":"far",
-                      (sb->layout>>8)&255);
+               printf("         Layout :");
+               print_r10_layout(sb->layout);
+               printf("\n");
        }
        switch(sb->level) {
        case 0:
@@ -208,7 +210,7 @@ static void examine_super0(struct supertype *st, char *homehost)
        }
        printf("\n");
        printf("      Number   Major   Minor   RaidDevice State\n");
-       for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) {
+       for (d= -1; d<(signed int)(sb->raid_disks+delta_extra + sb->spare_disks); d++) {
                mdp_disk_t *dp;
                char *dv;
                char nb[5];
@@ -233,7 +235,7 @@ static void examine_super0(struct supertype *st, char *homehost)
        }
 }
 
-static void brief_examine_super0(struct supertype *st)
+static void brief_examine_super0(struct supertype *st, int verbose)
 {
        mdp_super_t *sb = st->sb;
        char *c=map_num(pers, sb->level);
@@ -241,14 +243,18 @@ static void brief_examine_super0(struct supertype *st)
 
        sprintf(devname, "/dev/md%d", sb->md_minor);
 
-       printf("ARRAY %s level=%s num-devices=%d UUID=",
-              devname,
-              c?c:"-unknown-", sb->raid_disks);
+       if (verbose) {
+               printf("ARRAY %s level=%s num-devices=%d",
+                      devname,
+                      c?c:"-unknown-", sb->raid_disks);
+       } else
+               printf("ARRAY %s", devname);
+
        if (sb->minor_version >= 90)
-               printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
+               printf(" UUID=%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
                       sb->set_uuid2, sb->set_uuid3);
        else
-               printf("%08x", sb->set_uuid0);
+               printf(" UUID=%08x", sb->set_uuid0);
        printf("\n");
 }
 
@@ -301,27 +307,19 @@ static void brief_detail_super0(struct supertype *st)
        else
                printf("%08x", sb->set_uuid0);
 }
-
-static void export_detail_super0(struct supertype *st)
-{
-       mdp_super_t *sb = st->sb;
-       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(struct supertype *st, char *homehost)
 {
        mdp_super_t *sb = st->sb;
        char buf[20];
-       char *hash = sha1_buffer(homehost,
-                                strlen(homehost),
-                                buf);
+       char *hash;
+
+       if (!homehost)
+               return 0;
+       hash = sha1_buffer(homehost,
+                          strlen(homehost),
+                          buf);
 
        return (memcmp(&sb->set_uuid2, hash, 8)==0);
 }
@@ -370,6 +368,7 @@ static void getinfo_super0(struct supertype *st, struct mdinfo *info)
        info->data_offset = 0;
 
        sprintf(info->text_version, "0.%d", sb->minor_version);
+       info->safe_mode_delay = 200;
 
        uuid_from_super0(st, info->uuid);
 
@@ -380,6 +379,8 @@ static void getinfo_super0(struct supertype *st, struct mdinfo *info)
                info->delta_disks = sb->delta_disks;
                info->new_layout = sb->new_layout;
                info->new_chunk = sb->new_chunk;
+               if (info->delta_disks < 0)
+                       info->array.raid_disks -= info->delta_disks;
        } else
                info->reshape_active = 0;
 
@@ -477,7 +478,14 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
        if (strcmp(update, "assemble")==0) {
                int d = info->disk.number;
                int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
-               if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY))
+               int mask = (1<<MD_DISK_WRITEMOSTLY);
+               int add = 0;
+               if (sb->minor_version >= 91)
+                       /* During reshape we don't insist on everything
+                        * being marked 'sync'
+                        */
+                       add = (1<<MD_DISK_SYNC);
+               if (((sb->disks[d].state & ~mask) | add)
                    != info->disk.state) {
                        sb->disks[d].state = info->disk.state | wonly;
                        rv = 1;
@@ -554,8 +562,14 @@ static int init_super0(struct supertype *st, mdu_array_info_t *info,
                       unsigned long long size, char *ignored_name, char *homehost,
                       int *uuid)
 {
-       mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
+       mdp_super_t *sb;
        int spares;
+
+       if (posix_memalign((void**)&sb, 4096,
+                          MD_SB_BYTES + ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
+               fprintf(stderr, Name ": %s could not allocate superblock\n", __func__);
+               return 0;
+       }
        memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
 
        st->sb = sb;
@@ -631,8 +645,10 @@ struct devinfo {
        mdu_disk_info_t disk;
        struct devinfo *next;
 };
+
+#ifndef MDASSEMBLE
 /* Add a device to the superblock being created */
-static void add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
+static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
                          int fd, char *devname)
 {
        mdp_super_t *sb = st->sb;
@@ -657,7 +673,10 @@ static void add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
        di->disk = *dinfo;
        di->next = NULL;
        *dip = di;
+
+       return 0;
 }
+#endif
 
 static int store_super0(struct supertype *st, int fd)
 {
@@ -684,7 +703,8 @@ 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, sizeof(*bm)) != sizeof(*bm))
+                       if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) != 
+                           ROUND_UP(sizeof(*bm),4096))
                            return 5;
        }
 
@@ -744,7 +764,13 @@ static int compare_super0(struct supertype *st, struct supertype *tst)
        if (second->md_magic != MD_SB_MAGIC)
                return 1;
        if (!first) {
-               first = malloc(MD_SB_BYTES + sizeof(struct bitmap_super_s));
+               if (posix_memalign((void**)&first, 4096,
+                            MD_SB_BYTES + 
+                            ROUND_UP(sizeof(struct bitmap_super_s), 4096)) != 0) {
+                       fprintf(stderr, Name
+                               ": %s could not allocate superblock\n", __func__);
+                       return 1;
+               }
                memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
                st->sb = first;
                return 0;
@@ -813,7 +839,13 @@ static int load_super0(struct supertype *st, int fd, char *devname)
                return 1;
        }
 
-       super = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
+       if (posix_memalign((void**)&super, 4096,
+                          MD_SB_BYTES +
+                          ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
+               fprintf(stderr, Name
+                       ": %s could not allocate superblock\n", __func__);
+               return 1;
+       }
 
        if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
                if (devname)
@@ -857,8 +889,8 @@ static int load_super0(struct supertype *st, int fd, char *devname)
         * valid.  If it doesn't clear the bit.  An --assemble --force
         * should get that written out.
         */
-       if (read(fd, super+1, sizeof(struct bitmap_super_s))
-           != sizeof(struct bitmap_super_s))
+       if (read(fd, super+1, ROUND_UP(sizeof(struct bitmap_super_s),4096))
+           != ROUND_UP(sizeof(struct bitmap_super_s),4096))
                goto no_bitmap;
 
        uuid_from_super0(st, uuid);
@@ -885,6 +917,9 @@ static struct supertype *match_metadata_desc0(char *arg)
        st->minor_version = 90;
        st->max_devs = MD_SB_DISKS;
        st->sb = NULL;
+       /* we sometimes get 00.90 */
+       while (arg[0] == '0' && arg[1] == '0')
+               arg++;
        if (strcmp(arg, "0") == 0 ||
            strcmp(arg, "0.90") == 0 ||
            strcmp(arg, "default") == 0 ||
@@ -957,7 +992,7 @@ static int add_internal_bitmap0(struct supertype *st, int *chunkp,
 }
 
 
-void locate_bitmap0(struct supertype *st, int fd)
+static void locate_bitmap0(struct supertype *st, int fd)
 {
        unsigned long long dsize;
        unsigned long long offset;
@@ -977,7 +1012,7 @@ void locate_bitmap0(struct supertype *st, int fd)
        lseek64(fd, offset, 0);
 }
 
-int write_bitmap0(struct supertype *st, int fd)
+static int write_bitmap0(struct supertype *st, int fd)
 {
        unsigned long long dsize;
        unsigned long long offset;
@@ -986,7 +1021,8 @@ int write_bitmap0(struct supertype *st, int fd)
        int rv = 0;
 
        int towrite, n;
-       char buf[4096];
+       char abuf[4096+4096];
+       char *buf = (char*)(((long)(abuf+4096))&~4095L);
 
        if (!get_dev_size(fd, NULL, &dsize))
                return 1;
@@ -1002,21 +1038,19 @@ int write_bitmap0(struct supertype *st, int fd)
        if (lseek64(fd, offset + 4096, 0)< 0LL)
                return 3;
 
-
-       if (write(fd, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)) !=
-           sizeof(bitmap_super_t))
-               return -2;
-       towrite = 64*1024 - MD_SB_BYTES - sizeof(bitmap_super_t);
-       memset(buf, 0xff, sizeof(buf));
+       memset(buf, 0xff, 4096);
+       memcpy(buf,  ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t));
+       towrite = 64*1024;
        while (towrite > 0) {
                n = towrite;
-               if (n > sizeof(buf))
-                       n = sizeof(buf);
+               if (n > 4096)
+                       n = 4096;
                n = write(fd, buf, n);
                if (n > 0)
                        towrite -= n;
                else
                        break;
+               memset(buf, 0xff, 4096);
        }
        fsync(fd);
        if (towrite)
@@ -1032,10 +1066,12 @@ static void free_super0(struct supertype *st)
        st->sb = NULL;
 }
 
+#ifndef MDASSEMBLE
 static int validate_geometry0(struct supertype *st, int level,
                              int layout, int raiddisks,
                              int chunk, unsigned long long size,
-                             char *subdev, unsigned long long *freesize)
+                             char *subdev, unsigned long long *freesize,
+                             int verbose)
 {
        unsigned long long ldsize;
        int fd;
@@ -1044,17 +1080,19 @@ static int validate_geometry0(struct supertype *st, int level,
                return 0;
        if (raiddisks > MD_SB_DISKS)
                return 0;
-       if (size > (0x7fffffffULL<<10))
+       if (size > (0x7fffffffULL<<9))
                return 0;
        if (!subdev)
                return 1;
 
        fd = open(subdev, O_RDONLY|O_EXCL, 0);
        if (fd < 0) {
-               fprintf(stderr, Name ": Cannot open %s: %s\n",
-                       subdev, strerror(errno));
+               if (verbose)
+                       fprintf(stderr, Name ": super0.90 cannot open %s: %s\n",
+                               subdev, strerror(errno));
                return 0;
        }
+
        if (!get_dev_size(fd, subdev, &ldsize)) {
                close(fd);
                return 0;
@@ -1063,11 +1101,12 @@ static int validate_geometry0(struct supertype *st, int level,
 
        if (ldsize < MD_RESERVED_SECTORS * 512)
                return 0;
-       if (size > (0x7fffffffULL<<10))
+       if (size > (0x7fffffffULL<<9))
                return 0;
        *freesize = MD_NEW_SIZE_SECTORS(ldsize >> 9);
        return 1;
 }
+#endif /* MDASSEMBLE */
 
 struct superswitch super0 = {
 #ifndef MDASSEMBLE
@@ -1076,15 +1115,15 @@ struct superswitch super0 = {
        .export_examine_super = export_examine_super0,
        .detail_super = detail_super0,
        .brief_detail_super = brief_detail_super0,
-       .export_detail_super = export_detail_super0,
        .write_init_super = write_init_super0,
+       .validate_geometry = validate_geometry0,
+       .add_to_super = add_to_super0,
 #endif
        .match_home = match_home0,
        .uuid_from_super = uuid_from_super0,
        .getinfo_super = getinfo_super0,
        .update_super = update_super0,
        .init_super = init_super0,
-       .add_to_super = add_to_super0,
        .store_super = store_super0,
        .compare_super = compare_super0,
        .load_super = load_super0,
@@ -1094,5 +1133,5 @@ struct superswitch super0 = {
        .locate_bitmap = locate_bitmap0,
        .write_bitmap = write_bitmap0,
        .free_super = free_super0,
-       .validate_geometry = validate_geometry0,
+       .name = "0.90",
 };