]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-ddf.c
handle Manage_subdevs() for 'external' arrays
[thirdparty/mdadm.git] / super-ddf.c
index 9d421c581dd18631aed6e1d25e7d1292c98fd34e..1031e22b6c2622fd20c72e8ba1ec73a6be43cc9f 100644 (file)
@@ -288,8 +288,10 @@ struct vd_config {
        __u8    sec_elmnt_count;
        __u8    sec_elmnt_seq;
        __u8    srl;
-       __u64   blocks;
-       __u64   array_blocks;
+       __u64   blocks;         /* blocks per component could be different
+                                * on different component devices...(only
+                                * for concat I hope) */
+       __u64   array_blocks;   /* blocks in array */
        __u8    pad1[8];
        __u32   spare_refs[8];
        __u8    cache_pol[8];
@@ -414,7 +416,7 @@ struct ddf_super {
 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
 #endif
 
-struct superswitch super_ddf_container, super_ddf_bvd;
+extern struct superswitch super_ddf_container, super_ddf_bvd;
 
 static int calc_crc(void *buf, int len)
 {
@@ -609,6 +611,7 @@ static int load_ddf_local(int fd, struct ddf_super *super,
        char *conf;
        int i;
        int conflen;
+       int mppe;
 
        /* First the local disk info */
        super->max_part = __be16_to_cpu(super->active->max_partitions);
@@ -620,6 +623,7 @@ static int load_ddf_local(int fd, struct ddf_super *super,
                     super->active->data_section_length,
                     0);
        dl->devname = devname ? strdup(devname) : NULL;
+
        fstat(fd, &stb);
        dl->major = major(stb.st_rdev);
        dl->minor = minor(stb.st_rdev);
@@ -667,8 +671,9 @@ static int load_ddf_local(int fd, struct ddf_super *super,
                        super->conflist = vcl;
                }
                memcpy(&vcl->conf, vd, conflen*512);
+               mppe = __be16_to_cpu(super->anchor.max_primary_element_entries);
                vcl->lba_offset = (__u64*)
-                       &vcl->conf.phys_refnum[super->max_part+1];
+                       &vcl->conf.phys_refnum[mppe];
                dl->vlist[i/conflen] = vcl;
        }
        free(conf);
@@ -1044,7 +1049,9 @@ static void examine_super_ddf(struct supertype *st, char *homehost)
 
        printf("          Magic : %08x\n", __be32_to_cpu(sb->anchor.magic));
        printf("        Version : %.8s\n", sb->anchor.revision);
-       printf("Controller GUID : "); print_guid(sb->anchor.guid, 1);
+       printf("Controller GUID : "); print_guid(sb->controller.guid, 0);
+       printf("\n");
+       printf(" Container GUID : "); print_guid(sb->anchor.guid, 1);
        printf("\n");
        printf("            Seq : %08x\n", __be32_to_cpu(sb->active->seq));
        printf("  Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC
@@ -1149,6 +1156,7 @@ static void uuid_from_super_ddf(struct supertype *st, int uuid[4])
 static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info)
 {
        struct ddf_super *ddf = st->sb;
+       int i;
 
        info->array.major_version = 1000;
        info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
@@ -1167,15 +1175,35 @@ static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info)
 
        info->disk.major = 0;
        info->disk.minor = 0;
-//     info->disk.number = __be32_to_cpu(ddf->disk.refnum);
+       info->disk.number = __be32_to_cpu(ddf->dlist->disk.refnum);
 //     info->disk.raid_disk = find refnum in the table and use index;
-//     info->disk.state = ???;
+       info->disk.raid_disk = -1;
+       for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes) ; i++)
+               if (ddf->phys->entries[i].refnum == ddf->dlist->disk.refnum) {
+                       info->disk.raid_disk = i;
+                       break;
+               }
+       info->disk.state = (1 << MD_DISK_SYNC);
+
+       info->reshape_active = 0;
 
 //     uuid_from_super_ddf(info->uuid, sbv);
 
 //     info->name[] ?? ;
 }
 
+static void getinfo_super_n_container(struct supertype *st, struct mdinfo *info)
+{
+       /* just need offset and size */
+       struct ddf_super *ddf = st->sb;
+       int n = info->disk.number;
+
+       info->data_offset = __be64_to_cpu(ddf->phys->entries[n].config_size);
+       info->component_size = 32*1024*1024 / 512;
+}
+
+static int rlq_to_layout(int rlq, int prl, int raiddisks);
+
 static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info)
 {
        struct ddf_super *ddf = st->sb;
@@ -1188,7 +1216,8 @@ static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info)
        info->array.patch_version = 0;
        info->array.raid_disks    = __be16_to_cpu(vd->prim_elmnt_count);
        info->array.level         = map_num1(ddf_level_num, vd->prl);
-       info->array.layout        = vd->rlq; /* FIXME should this be mapped */
+       info->array.layout        = rlq_to_layout(vd->rlq, vd->prl,
+                                                 info->array.raid_disks);
        info->array.md_minor      = -1;
        info->array.ctime         = DECADE + __be32_to_cpu(*(__u32*)(vd->guid+16));
        info->array.utime         = DECADE + __be32_to_cpu(vd->timestamp);
@@ -1208,6 +1237,18 @@ static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info)
 //     info->name[] ?? ;
 }
 
+static void getinfo_super_n_bvd(struct supertype *st, struct mdinfo *info)
+{
+       /* Find the particular details for info->disk.raid_disk.
+        * This includes data_offset, component_size,
+        */
+       struct ddf_super *ddf = st->sb;
+       __u64 *lba_offset = ddf->newconf->lba_offset;
+       struct vd_config *conf = &ddf->newconf->conf;
+       info->data_offset = __be64_to_cpu(lba_offset[info->disk.raid_disk]);
+       info->component_size = __be64_to_cpu(conf->blocks);
+}
+
 static int update_super_ddf(struct supertype *st, struct mdinfo *info,
                            char *update,
                            char *devname, int verbose,
@@ -1279,6 +1320,30 @@ static int update_super_ddf(struct supertype *st, struct mdinfo *info,
        return rv;
 }
 
+static void make_header_guid(char *guid)
+{
+       __u32 stamp;
+       int rfd;
+       /* Create a DDF Header of Virtual Disk GUID */
+
+       /* 24 bytes of fiction required.
+        * first 8 are a 'vendor-id'  - "Linux-MD"
+        * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
+        * Remaining 8 random number plus timestamp
+        */
+       memcpy(guid, T10, sizeof(T10));
+       stamp = __cpu_to_be32(0xdeadbeef);
+       memcpy(guid+8, &stamp, 4);
+       stamp = __cpu_to_be32(0);
+       memcpy(guid+12, &stamp, 4);
+       stamp = __cpu_to_be32(time(0) - DECADE);
+       memcpy(guid+16, &stamp, 4);
+       rfd = open("/dev/urandom", O_RDONLY);
+       if (rfd < 0 || read(rfd, &stamp, 4) != 4)
+               stamp = random();
+       memcpy(guid+20, &stamp, 4);
+       if (rfd >= 0) close(rfd);
+}
 static int init_super_ddf(struct supertype *st,
                          mdu_array_info_t *info,
                          unsigned long long size, char *name, char *homehost,
@@ -1310,8 +1375,6 @@ static int init_super_ddf(struct supertype *st,
        struct ddf_super *ddf;
        char hostname[17];
        int hostlen;
-       __u32 stamp;
-       int rfd;
        int max_phys_disks, max_virt_disks;
        unsigned long long sector;
        int clen;
@@ -1333,23 +1396,7 @@ static int init_super_ddf(struct supertype *st,
         */
 
        ddf->anchor.magic = DDF_HEADER_MAGIC;
-       /* 24 bytes of fiction required.
-        * first 8 are a 'vendor-id'  - "Linux-MD"
-        * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
-        * Remaining 16 are serial number.... maybe a hostname would do?
-        */
-       memcpy(ddf->anchor.guid, T10, sizeof(T10));
-       stamp = __cpu_to_be32(0xdeadbeef);
-       memcpy(ddf->anchor.guid+8, &stamp, 4);
-       stamp = __cpu_to_be32(0);
-       memcpy(ddf->anchor.guid+12, &stamp, 4);
-       stamp = __cpu_to_be32(time(0) - DECADE);
-       memcpy(ddf->anchor.guid+16, &stamp, 4);
-       rfd = open("/dev/urandom", O_RDONLY);
-       if (rfd < 0 || read(rfd, &stamp, 4) != 4)
-               stamp = random();
-       memcpy(ddf->anchor.guid+20, &stamp, 4);
-       if (rfd >= 0) close(rfd);
+       make_header_guid(ddf->anchor.guid);
 
        memcpy(ddf->anchor.revision, DDF_REVISION, 8);
        ddf->anchor.seq = __cpu_to_be32(1);
@@ -1479,10 +1526,246 @@ static int init_super_ddf(struct supertype *st,
        vd->max_vdes = __cpu_to_be16(max_virt_disks);
        memset(vd->pad, 0xff, 52);
 
+       for (i=0; i<max_virt_disks; i++)
+               memset(&vd->entries[i], 0xff, sizeof(struct virtual_entry));
+
        st->sb = ddf;
        return 1;
 }
 
+static int all_ff(char *guid)
+{
+       int i;
+       for (i = 0; i < DDF_GUID_LEN; i++)
+               if (guid[i] != (char)0xff)
+                       return 0;
+       return 1;
+}
+static int chunk_to_shift(int chunksize)
+{
+       return ffs(chunksize/512)-1;
+}
+
+static int level_to_prl(int level)
+{
+       switch (level) {
+       case LEVEL_LINEAR: return DDF_CONCAT;
+       case 0: return DDF_RAID0;
+       case 1: return DDF_RAID1;
+       case 4: return DDF_RAID4;
+       case 5: return DDF_RAID5;
+       case 6: return DDF_RAID6;
+       default: return -1;
+       }
+}
+static int layout_to_rlq(int level, int layout, int raiddisks)
+{
+       switch(level) {
+       case 0:
+               return DDF_RAID0_SIMPLE;
+       case 1:
+               switch(raiddisks) {
+               case 2: return DDF_RAID1_SIMPLE;
+               case 3: return DDF_RAID1_MULTI;
+               default: return -1;
+               }
+       case 4:
+               switch(layout) {
+               case 0: return DDF_RAID4_N;
+               }
+               break;
+       case 5:
+       case 6:
+               switch(layout) {
+               case ALGORITHM_LEFT_ASYMMETRIC:
+                       return DDF_RAID5_N_RESTART;
+               case ALGORITHM_RIGHT_ASYMMETRIC:
+                       return DDF_RAID5_0_RESTART;
+               case ALGORITHM_LEFT_SYMMETRIC:
+                       return DDF_RAID5_N_CONTINUE;
+               case ALGORITHM_RIGHT_SYMMETRIC:
+                       return -1; /* not mentioned in standard */
+               }
+       }
+       return -1;
+}
+
+static int rlq_to_layout(int rlq, int prl, int raiddisks)
+{
+       switch(prl) {
+       case DDF_RAID0:
+               return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
+       case DDF_RAID1:
+               return 0; /* hopefully rlq == SIMPLE or MULTI depending
+                            on raiddisks*/
+       case DDF_RAID4:
+               switch(rlq) {
+               case DDF_RAID4_N:
+                       return 0;
+               default:
+                       /* not supported */
+                       return -1; /* FIXME this isn't checked */
+               }
+       case DDF_RAID5:
+       case DDF_RAID6:
+               switch(rlq) {
+               case DDF_RAID5_N_RESTART:
+                       return ALGORITHM_LEFT_ASYMMETRIC;
+               case DDF_RAID5_0_RESTART:
+                       return ALGORITHM_RIGHT_ASYMMETRIC;
+               case DDF_RAID5_N_CONTINUE:
+                       return ALGORITHM_LEFT_SYMMETRIC;
+               default:
+                       return -1;
+               }
+       }
+       return -1;
+}
+
+static int init_super_ddf_bvd(struct supertype *st,
+                             mdu_array_info_t *info,
+                             unsigned long long size,
+                             char *name, char *homehost,
+                             int *uuid)
+{
+       /* We are creating a BVD inside a pre-existing container.
+        * so st->sb is already set.
+        * We need to create a new vd_config and a new virtual_entry
+        */
+       struct ddf_super *ddf = st->sb;
+       int venum;
+       struct virtual_entry *ve;
+       struct vcl *vcl;
+       struct vd_config *vc;
+       int mppe;
+       int conflen;
+
+       if (__be16_to_cpu(ddf->virt->populated_vdes)
+           >= __be16_to_cpu(ddf->virt->max_vdes)) {
+               fprintf(stderr, Name": This ddf already has the "
+                       "maximum of %d virtual devices\n",
+                       __be16_to_cpu(ddf->virt->max_vdes));
+               return 0;
+       }
+
+       for (venum = 0; venum < __be16_to_cpu(ddf->virt->max_vdes); venum++)
+               if (all_ff(ddf->virt->entries[venum].guid))
+                       break;
+       if (venum == __be16_to_cpu(ddf->virt->max_vdes)) {
+               fprintf(stderr, Name ": Cannot find spare slot for "
+                       "virtual disk - DDF is corrupt\n");
+               return 0;
+       }
+       ve = &ddf->virt->entries[venum];
+       st->container_member = venum;
+
+       /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
+        * timestamp, random number
+        */
+       make_header_guid(ve->guid);
+       ve->unit = __cpu_to_be16(info->md_minor);
+       ve->pad0 = 0xFFFF;
+       ve->guid_crc = crc32(0, (unsigned char*)ddf->anchor.guid, DDF_GUID_LEN);
+       ve->type = 0;
+       ve->state = 0;
+       ve->init_state = 0;
+       if (!(info->state & 1))
+               ve->init_state = DDF_state_inconsistent;
+       memset(ve->pad1, 0xff, 14);
+       memset(ve->name, ' ', 16);
+       if (name)
+               strncpy(ve->name, name, 16);
+       ddf->virt->populated_vdes =
+               __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1);
+
+       /* Now create a new vd_config */
+       conflen =  __be16_to_cpu(ddf->active->config_record_len);
+       vcl = malloc(offsetof(struct vcl, conf) + conflen * 512);
+       mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
+       vcl->lba_offset = (__u64*) &vcl->conf.phys_refnum[mppe];
+
+       vc = &vcl->conf;
+
+       vc->magic = DDF_VD_CONF_MAGIC;
+       memcpy(vc->guid, ve->guid, DDF_GUID_LEN);
+       vc->timestamp = __cpu_to_be32(time(0)-DECADE);
+       vc->seqnum = __cpu_to_be32(1);
+       memset(vc->pad0, 0xff, 24);
+       vc->prim_elmnt_count = __cpu_to_be16(info->raid_disks);
+       vc->chunk_shift = chunk_to_shift(info->chunk_size);
+       vc->prl = level_to_prl(info->level);
+       vc->rlq = layout_to_rlq(info->level, info->layout, info->raid_disks);
+       vc->sec_elmnt_count = 1;
+       vc->sec_elmnt_seq = 0;
+       vc->srl = 0;
+       vc->blocks = __cpu_to_be64(info->size * 2);
+       vc->array_blocks = __cpu_to_be64(
+               calc_array_size(info->level, info->raid_disks, info->layout,
+                               info->chunk_size, info->size*2));
+       memset(vc->pad1, 0xff, 8);
+       vc->spare_refs[0] = 0xffffffff;
+       vc->spare_refs[1] = 0xffffffff;
+       vc->spare_refs[2] = 0xffffffff;
+       vc->spare_refs[3] = 0xffffffff;
+       vc->spare_refs[4] = 0xffffffff;
+       vc->spare_refs[5] = 0xffffffff;
+       vc->spare_refs[6] = 0xffffffff;
+       vc->spare_refs[7] = 0xffffffff;
+       memset(vc->cache_pol, 0, 8);
+       vc->bg_rate = 0x80;
+       memset(vc->pad2, 0xff, 3);
+       memset(vc->pad3, 0xff, 52);
+       memset(vc->pad4, 0xff, 192);
+       memset(vc->v0, 0xff, 32);
+       memset(vc->v1, 0xff, 32);
+       memset(vc->v2, 0xff, 16);
+       memset(vc->v3, 0xff, 16);
+       memset(vc->vendor, 0xff, 32);
+
+       memset(vc->phys_refnum, 0xff, 4*mppe);
+       memset(vc->phys_refnum+mppe, 0x00, 8*mppe);
+
+       vcl->next = ddf->conflist;
+       ddf->conflist = vcl;
+       ddf->newconf = vcl;
+       return 1;
+}
+
+static void add_to_super_ddf_bvd(struct supertype *st,
+                                mdu_disk_info_t *dk, int fd, char *devname)
+{
+       /* fd and devname identify a device with-in the ddf container (st).
+        * dk identifies a location in the new BVD.
+        * We need to find suitable free space in that device and update
+        * the phys_refnum and lba_offset for the newly created vd_config.
+        * We might also want to update the type in the phys_disk
+        * section. FIXME
+        */
+       struct dl *dl;
+       struct ddf_super *ddf = st->sb;
+       struct vd_config *vc;
+       __u64 *lba_offset;
+       int mppe;
+
+       for (dl = ddf->dlist; dl ; dl = dl->next)
+               if (dl->major == dk->major &&
+                   dl->minor == dk->minor)
+                       break;
+       if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
+               return;
+
+       vc = &ddf->newconf->conf;
+       vc->phys_refnum[dk->raid_disk] = dl->disk.refnum;
+       mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
+       lba_offset = (__u64*)(vc->phys_refnum + mppe);
+       lba_offset[dk->raid_disk] = 0; /* FIXME */
+
+       dl->vlist[0] =ddf->newconf; /* FIXME */
+
+       dl->fd = fd;
+       dl->devname = devname;
+}
+
 /* add a device to a container, either while creating it or while
  * expanding a pre-existing container
  */
@@ -1517,7 +1800,7 @@ static void add_to_super_ddf(struct supertype *st,
        *(__u32*)(dd->disk.guid + 16) = random();
        *(__u32*)(dd->disk.guid + 20) = random();
 
-       dd->disk.refnum = random(); /* and hope for the best */
+       dd->disk.refnum = random(); /* and hope for the best FIXME check this is unique!!*/
        dd->disk.forced_ref = 1;
        dd->disk.forced_guid = 1;
        memset(dd->disk.vendor, ' ', 32);
@@ -1664,14 +1947,40 @@ int validate_geometry_ddf(struct supertype *st,
 
        if (level == LEVEL_CONTAINER) {
                st->ss = &super_ddf_container;
+               if (dev) {
+                       int rv =st->ss->validate_geometry(st, level, layout,
+                                                         raiddisks, chunk,
+                                                         size,
+                                                         NULL, freesize);
+                       if (rv)
+                               return rv;
+               }
                return st->ss->validate_geometry(st, level, layout, raiddisks,
                                                 chunk, size, dev, freesize);
        }
+
+       if (st->sb) {
+               /* creating in a given container */
+               st->ss = &super_ddf_bvd;
+               if (dev) {
+                       int rv =st->ss->validate_geometry(st, level, layout,
+                                                         raiddisks, chunk,
+                                                         size,
+                                                         NULL, freesize);
+                       if (rv)
+                               return rv;
+               }
+               return st->ss->validate_geometry(st, level, layout, raiddisks,
+                                                chunk, size, dev, freesize);
+       }
+       /* FIXME should exclude MULTIPATH, or more appropriately, allow
+        * only known levels.
+        */
        if (!dev)
                return 1;
 
        /* This device needs to be either a device in a 'ddf' container,
-        * or it needs to be a 'ddf-bvd' array.  Test the first first.
+        * or it needs to be a 'ddf-bvd' array.
         */
 
        fd = open(dev, O_RDONLY|O_EXCL, 0);
@@ -1714,7 +2023,9 @@ int validate_geometry_ddf(struct supertype *st,
                struct ddf_super *ddf;
                st->ss = &super_ddf_bvd;
                if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL, 1) == 0) {
-                       st->info = ddf;
+                       st->sb = ddf;
+                       st->container_dev = fd2devnum(cfd);
+                       st->container_member = 27; // FIXME
                        close(cfd);
                        return st->ss->validate_geometry(st, level, layout,
                                                         raiddisks, chunk, size,
@@ -1757,6 +2068,67 @@ int validate_geometry_ddf_container(struct supertype *st,
        return 1;
 }
 
+struct extent {
+       unsigned long long start, size;
+};
+int cmp_extent(const void *av, const void *bv)
+{
+       const struct extent *a = av;
+       const struct extent *b = bv;
+       if (a->start < b->start)
+               return -1;
+       if (a->start > b->start)
+               return 1;
+       return 0;
+}
+
+struct extent *get_extents(struct ddf_super *ddf, struct dl *dl)
+{
+       /* find a list of used extents on the give physical device
+        * (dnum) or the given ddf.
+        * Return a malloced array of 'struct extent'
+
+FIXME ignore DDF_Legacy devices?
+
+        */
+       struct extent *rv;
+       int n = 0;
+       int dnum;
+       int i, j;
+
+       for (dnum = 0; dnum < ddf->phys->used_pdes; dnum++)
+               if (memcmp(dl->disk.guid,
+                          ddf->phys->entries[dnum].guid,
+                          DDF_GUID_LEN) == 0)
+                       break;
+
+       if (dnum == ddf->phys->used_pdes)
+               return NULL;
+
+       rv = malloc(sizeof(struct extent) * (ddf->max_part + 2));
+       if (!rv)
+               return NULL;
+
+       for (i = 0; i < ddf->max_part+1; i++) {
+               struct vcl *v = dl->vlist[i];
+               if (v == NULL)
+                       continue;
+               for (j=0; j < v->conf.prim_elmnt_count; j++)
+                       if (v->conf.phys_refnum[j] == dl->disk.refnum) {
+                               /* This device plays role 'j' in  'v'. */
+                               rv[n].start = __be64_to_cpu(v->lba_offset[j]);
+                               rv[n].size = __be64_to_cpu(v->conf.blocks);
+                               n++;
+                               break;
+                       }
+       }
+       qsort(rv, n, sizeof(*rv), cmp_extent);
+
+       rv[n].start = __be64_to_cpu(ddf->phys->entries[dnum].config_size);
+       rv[n].size = 0;
+       return rv;
+}
+
 int validate_geometry_ddf_bvd(struct supertype *st,
                              int level, int layout, int raiddisks,
                              int chunk, unsigned long long size,
@@ -1765,6 +2137,10 @@ int validate_geometry_ddf_bvd(struct supertype *st,
        struct stat stb;
        struct ddf_super *ddf = st->sb;
        struct dl *dl;
+       unsigned long long pos = 0;
+       unsigned long long maxsize;
+       struct extent *e;
+       int i;
        /* ddf/bvd supports lots of things, but not containers */
        if (level == LEVEL_CONTAINER)
                return 0;
@@ -1772,6 +2148,41 @@ int validate_geometry_ddf_bvd(struct supertype *st,
        if (!ddf)
                return 0;
 
+       if (!dev) {
+               /* General test:  make sure there is space for
+                * 'raiddisks' device extents of size 'size'.
+                */
+               unsigned long long minsize = size;
+               int dcnt = 0;
+               if (minsize == 0)
+                       minsize = 8;
+               for (dl = ddf->dlist; dl ; dl = dl->next)
+               {
+                       int found = 0;
+
+                       i = 0;
+                       e = get_extents(ddf, dl);
+                       if (!e) continue;
+                       do {
+                               unsigned long long esize;
+                               esize = e[i].start - pos;
+                               if (esize >= minsize)
+                                       found = 1;
+                               pos = e[i].start + e[i].size;
+                               i++;
+                       } while (e[i-1].size);
+                       if (found)
+                               dcnt++;
+                       free(e);
+               }
+               if (dcnt < raiddisks) {
+                       fprintf(stderr, Name ": Not enough devices with space "
+                               "for this array (%d < %d)\n",
+                               dcnt, raiddisks);
+                       return 0;
+               }
+               return 1;
+       }
        /* This device must be a member of the set */
        if (stat(dev, &stb) < 0)
                return 0;
@@ -1782,6 +2193,23 @@ int validate_geometry_ddf_bvd(struct supertype *st,
                    dl->minor == minor(stb.st_rdev))
                        break;
        }
+       if (!dl) {
+               fprintf(stderr, Name ": %s is not in the same DDF set\n",
+                       dev);
+               return 0;
+       }
+       e = get_extents(ddf, dl);
+       maxsize = 0;
+       i = 0;
+       if (e) do {
+               unsigned long long esize;
+               esize = e[i].start - pos;
+               if (esize >= maxsize)
+                       maxsize = esize;
+               pos = e[i].start + e[i].size;
+               i++;
+       } while (e[i-1].size);
+       *freesize = maxsize;
        // FIXME here I am
 
        return 1;
@@ -1855,15 +2283,15 @@ static int load_super_ddf_all(struct supertype *st, int fd,
        /* Now we need the device-local bits */
        for (sd = sra->devs ; sd ; sd = sd->next) {
                sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
-               dfd = dev_open(nm, O_RDONLY);
+               dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
                if (!dfd)
                        return 2;
                seq = load_ddf_local(dfd, super, NULL, keep_fd);
-               close(dfd);
+               if (!keep_fd) close(dfd);
        }
        *sbp = super;
        if (st->ss == NULL) {
-               st->ss = &super_ddf;
+               st->ss = &super_ddf_container;
                st->minor_version = 0;
                st->max_devs = 512;
        }
@@ -1873,6 +2301,98 @@ static int load_super_ddf_all(struct supertype *st, int fd,
 
 
 
+static struct mdinfo *container_content_ddf(struct supertype *st)
+{
+       /* Given a container loaded by load_super_ddf_all,
+        * extract information about all the arrays into
+        * an mdinfo tree.
+        *
+        * For each vcl in conflist: create an mdinfo, fill it in,
+        *  then look for matching devices (phys_refnum) in dlist
+        *  and create appropriate device mdinfo.
+        */
+       struct ddf_super *ddf = st->sb;
+       struct mdinfo *rest = NULL;
+       struct vcl *vc;
+
+       for (vc = ddf->conflist ; vc ; vc=vc->next)
+       {
+               int mppe;
+               int i;
+               struct mdinfo *this;
+               this = malloc(sizeof(*this));
+               memset(this, 0, sizeof(*this));
+               this->next = rest;
+               rest = this;
+
+               this->array.major_version = 1000;
+               this->array.minor_version = 0;
+               this->array.patch_version = 0;
+               this->array.level = map_num1(ddf_level_num, vc->conf.prl);
+               this->array.raid_disks =
+                       __be16_to_cpu(vc->conf.prim_elmnt_count);
+               /* FIXME this should be mapped */
+               this->array.layout = vc->conf.rlq;
+               this->array.md_minor      = -1;
+               this->array.ctime         = DECADE +
+                       __be32_to_cpu(*(__u32*)(vc->conf.guid+16));
+               this->array.utime         = DECADE +
+                       __be32_to_cpu(vc->conf.timestamp);
+               this->array.chunk_size    = 512 << vc->conf.chunk_shift;
+
+               for (i=0; i < __be16_to_cpu(ddf->virt->populated_vdes); i++)
+                       if (memcmp(ddf->virt->entries[i].guid,
+                                  vc->conf.guid, DDF_GUID_LEN) == 0)
+                               break;
+               if (ddf->virt->entries[i].state & DDF_state_inconsistent)
+                       this->array.state = 0;
+               else
+                       this->array.state = 1;
+               memcpy(this->name, ddf->virt->entries[i].name, 32);
+               this->name[33]=0;
+
+               memset(this->uuid, 0, sizeof(this->uuid));
+               this->component_size = __be64_to_cpu(vc->conf.blocks);
+               this->array.size = this->component_size / 2;
+               this->container_member = i;
+
+               mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
+               for (i=0 ; i < mppe ; i++) {
+                       struct mdinfo *dev;
+                       struct dl *d;
+
+                       if (vc->conf.phys_refnum[i] == 0xFFFFFFFF)
+                               continue;
+
+                       this->array.working_disks++;
+
+                       for (d = ddf->dlist; d ; d=d->next)
+                               if (d->disk.refnum == vc->conf.phys_refnum[i])
+                                       break;
+                       if (d == NULL)
+                               break;
+
+                       dev = malloc(sizeof(*dev));
+                       memset(dev, 0, sizeof(*dev));
+                       dev->next = this->devs;
+                       this->devs = dev;
+
+                       dev->disk.number = __be32_to_cpu(d->disk.refnum);
+                       dev->disk.major = d->major;
+                       dev->disk.minor = d->minor;
+                       dev->disk.raid_disk = i;
+                       dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
+
+                       dev->events = __le32_to_cpu(ddf->primary.seq);
+                       dev->data_offset = vc->lba_offset[i];
+                       dev->component_size = __be64_to_cpu(vc->conf.blocks);
+                       if (d->devname)
+                               strcpy(dev->name, d->devname);
+               }
+       }
+       return rest;
+}
+
 static int init_zero_ddf(struct supertype *st,
                         mdu_array_info_t *info,
                         unsigned long long size, char *name,
@@ -1897,6 +2417,31 @@ static int store_zero_ddf(struct supertype *st, int fd)
        return 0;
 }
 
+static int compare_super_ddf(struct supertype *st, struct supertype *tst)
+{
+       /*
+        * return:
+        *  0 same, or first was empty, and second was copied
+        *  1 second had wrong number
+        *  2 wrong uuid
+        *  3 wrong other info
+        */
+       struct ddf_super *first = st->sb;
+       struct ddf_super *second = tst->sb;
+
+       if (!first) {
+               st->sb = tst->sb;
+               tst->sb = NULL;
+               return 0;
+       }
+
+       if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0)
+               return 2;
+
+       /* FIXME should I look at anything else? */
+       return 0;
+}
+
 struct superswitch super_ddf = {
 #ifndef        MDASSEMBLE
        .examine_super  = examine_super_ddf,
@@ -1912,11 +2457,14 @@ struct superswitch super_ddf = {
 
        .avail_size     = avail_size_ddf,
 
+       .compare_super  = compare_super_ddf,
+
        .load_super     = load_super_ddf,
        .init_super     = init_zero_ddf,
        .store_super    = store_zero_ddf,
        .free_super     = free_super_ddf,
        .match_metadata_desc = match_metadata_desc_ddf,
+       .getinfo_super_n  = getinfo_super_n_container,
 
 
        .major          = 1000,
@@ -1939,6 +2487,8 @@ struct superswitch super_ddf_container = {
 
        .free_super     = free_super_ddf,
 
+       .container_content = container_content_ddf,
+
        .major          = 1000,
        .swapuuid       = 0,
        .external       = 1,
@@ -1950,10 +2500,13 @@ struct superswitch super_ddf_bvd = {
 //     .detail_super   = detail_super_ddf_bvd,
 //     .brief_detail_super = brief_detail_super_ddf_bvd,
        .validate_geometry = validate_geometry_ddf_bvd,
+       .write_init_super = write_init_super_ddf,
 #endif
        .update_super   = update_super_ddf,
-       .init_super     = init_super_ddf,
+       .init_super     = init_super_ddf_bvd,
+       .add_to_super   = add_to_super_ddf_bvd,
        .getinfo_super  = getinfo_super_ddf_bvd,
+       .getinfo_super_n  = getinfo_super_n_bvd,
 
        .load_super     = load_super_ddf,
        .free_super     = free_super_ddf,
@@ -1962,7 +2515,7 @@ struct superswitch super_ddf_bvd = {
 
        .major          = 1001,
        .swapuuid       = 0,
-       .external       = 1,
+       .external       = 2,
        .text_version   = "ddf",
 };
 
@@ -1970,7 +2523,7 @@ struct superswitch super_ddf_svd = {
 #ifndef        MDASSEMBLE
 //     .detail_super   = detail_super_ddf_svd,
 //     .brief_detail_super = brief_detail_super_ddf_svd,
-       .validate_geometry = validate_geometry_ddf_bvd,
+       .validate_geometry = validate_geometry_ddf_svd,
 #endif
        .update_super   = update_super_ddf,
        .init_super     = init_super_ddf,
@@ -1979,9 +2532,8 @@ struct superswitch super_ddf_svd = {
        .free_super     = free_super_ddf,
        .match_metadata_desc = match_metadata_desc_ddf_svd,
 
-
        .major          = 1002,
        .swapuuid       = 0,
-       .external       = 1,
+       .external       = 2,
        .text_version   = "ddf",
 };