]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
imsm: don't check raid1 chunk size
[thirdparty/mdadm.git] / super-intel.c
index 7066e8e607f46cd3ebc38b2f4a2624660fd86d41..caf9a996b3426b8f6a5b3951e0e47f335832a11b 100644 (file)
 #include "mdadm.h"
 #include "mdmon.h"
 #include "sha1.h"
+#include "platform-intel.h"
 #include <values.h>
 #include <scsi/sg.h>
 #include <ctype.h>
+#include <dirent.h>
 
 /* MPB == Metadata Parameter Block */
 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
@@ -201,6 +203,12 @@ static unsigned int mpb_sectors(struct imsm_super *mpb)
        return sector_count(__le32_to_cpu(mpb->mpb_size));
 }
 
+struct intel_dev {
+       struct imsm_dev *dev;
+       struct intel_dev *next;
+       int index;
+};
+
 /* internal representation of IMSM metadata */
 struct intel_super {
        union {
@@ -214,8 +222,7 @@ struct intel_super {
        int creating_imsm; /* flag to indicate container creation */
        int current_vol; /* index of raid device undergoing creation */
        __u32 create_offset; /* common start for 'current_vol' */
-       #define IMSM_MAX_RAID_DEVS 2
-       struct imsm_dev *dev_tbl[IMSM_MAX_RAID_DEVS];
+       struct intel_dev *devlist;
        struct dl {
                struct dl *next;
                int index;
@@ -230,6 +237,8 @@ struct intel_super {
        struct dl *add; /* list of disks to add while mdmon active */
        struct dl *missing; /* disks removed while we weren't looking */
        struct bbm_log *bbm_log;
+       const char *hba; /* device path of the raid controller for this metadata */
+       const struct imsm_orom *orom; /* platform firmware support */
 };
 
 struct extent {
@@ -405,9 +414,14 @@ static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
 
 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
 {
+       struct intel_dev *dv;
+
        if (index >= super->anchor->num_raid_devs)
                return NULL;
-       return super->dev_tbl[index];
+       for (dv = super->devlist; dv; dv = dv->next)
+               if (dv->index == index)
+                       return dv->dev;
+       return NULL;
 }
 
 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
@@ -741,6 +755,258 @@ static void brief_detail_super_imsm(struct supertype *st)
        fname_from_uuid(st, &info, nbuf,'-');
        printf(" UUID=%s", nbuf + 5);
 }
+
+static int imsm_read_serial(int fd, char *devname, __u8 *serial);
+static void fd2devname(int fd, char *name);
+
+static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
+{
+       /* dump an unsorted list of devices attached to ahci, as well as
+        * non-connected ports
+        */
+       int hba_len = strlen(hba_path) + 1;
+       struct dirent *ent;
+       DIR *dir;
+       char *path = NULL;
+       int err = 0;
+       unsigned long port_mask = (1 << port_count) - 1;
+
+       if (port_count > sizeof(port_mask) * 8) {
+               if (verbose)
+                       fprintf(stderr, Name ": port_count %d out of range\n", port_count);
+               return 2;
+       }
+
+       /* scroll through /sys/dev/block looking for devices attached to
+        * this hba
+        */
+       dir = opendir("/sys/dev/block");
+       for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
+               int fd;
+               char model[64];
+               char vendor[64];
+               char buf[1024];
+               int major, minor;
+               char *device;
+               char *c;
+               int port;
+               int type;
+
+               if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
+                       continue;
+               path = devt_to_devpath(makedev(major, minor));
+               if (!path)
+                       continue;
+               if (!path_attached_to_hba(path, hba_path)) {
+                       free(path);
+                       path = NULL;
+                       continue;
+               }
+
+               /* retrieve the scsi device type */
+               if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
+                       if (verbose)
+                               fprintf(stderr, Name ": failed to allocate 'device'\n");
+                       err = 2;
+                       break;
+               }
+               sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
+               if (load_sys(device, buf) != 0) {
+                       if (verbose)
+                               fprintf(stderr, Name ": failed to read device type for %s\n",
+                                       path);
+                       err = 2;
+                       free(device);
+                       break;
+               }
+               type = strtoul(buf, NULL, 10);
+
+               /* if it's not a disk print the vendor and model */
+               if (!(type == 0 || type == 7 || type == 14)) {
+                       vendor[0] = '\0';
+                       model[0] = '\0';
+                       sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
+                       if (load_sys(device, buf) == 0) {
+                               strncpy(vendor, buf, sizeof(vendor));
+                               vendor[sizeof(vendor) - 1] = '\0';
+                               c = (char *) &vendor[sizeof(vendor) - 1];
+                               while (isspace(*c) || *c == '\0')
+                                       *c-- = '\0';
+
+                       }
+                       sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
+                       if (load_sys(device, buf) == 0) {
+                               strncpy(model, buf, sizeof(model));
+                               model[sizeof(model) - 1] = '\0';
+                               c = (char *) &model[sizeof(model) - 1];
+                               while (isspace(*c) || *c == '\0')
+                                       *c-- = '\0';
+                       }
+
+                       if (vendor[0] && model[0])
+                               sprintf(buf, "%.64s %.64s", vendor, model);
+                       else
+                               switch (type) { /* numbers from hald/linux/device.c */
+                               case 1: sprintf(buf, "tape"); break;
+                               case 2: sprintf(buf, "printer"); break;
+                               case 3: sprintf(buf, "processor"); break;
+                               case 4:
+                               case 5: sprintf(buf, "cdrom"); break;
+                               case 6: sprintf(buf, "scanner"); break;
+                               case 8: sprintf(buf, "media_changer"); break;
+                               case 9: sprintf(buf, "comm"); break;
+                               case 12: sprintf(buf, "raid"); break;
+                               default: sprintf(buf, "unknown");
+                               }
+               } else
+                       buf[0] = '\0';
+               free(device);
+
+               /* chop device path to 'host%d' and calculate the port number */
+               c = strchr(&path[hba_len], '/');
+               *c = '\0';
+               if (sscanf(&path[hba_len], "host%d", &port) == 1)
+                       port -= host_base;
+               else {
+                       if (verbose) {
+                               *c = '/'; /* repair the full string */
+                               fprintf(stderr, Name ": failed to determine port number for %s\n",
+                                       path);
+                       }
+                       err = 2;
+                       break;
+               }
+
+               /* mark this port as used */
+               port_mask &= ~(1 << port);
+
+               /* print out the device information */
+               if (buf[0]) {
+                       printf("          Port%d : - non-disk device (%s) -\n", port, buf);
+                       continue;
+               }
+
+               fd = dev_open(ent->d_name, O_RDONLY);
+               if (fd < 0)
+                       printf("          Port%d : - disk info unavailable -\n", port);
+               else {
+                       fd2devname(fd, buf);
+                       printf("          Port%d : %s", port, buf);
+                       if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
+                               printf(" (%s)\n", buf);
+                       else
+                               printf("()\n");
+               }
+               close(fd);
+               free(path);
+               path = NULL;
+       }
+       if (path)
+               free(path);
+       if (dir)
+               closedir(dir);
+       if (err == 0) {
+               int i;
+
+               for (i = 0; i < port_count; i++)
+                       if (port_mask & (1 << i))
+                               printf("          Port%d : - no device attached -\n", i);
+       }
+
+       return err;
+}
+
+static int detail_platform_imsm(int verbose, int enumerate_only)
+{
+       /* There are two components to imsm platform support, the ahci SATA
+        * controller and the option-rom.  To find the SATA controller we
+        * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
+        * controller with the Intel vendor id is present.  This approach
+        * allows mdadm to leverage the kernel's ahci detection logic, with the
+        * caveat that if ahci.ko is not loaded mdadm will not be able to
+        * detect platform raid capabilities.  The option-rom resides in a
+        * platform "Adapter ROM".  We scan for its signature to retrieve the
+        * platform capabilities.  If raid support is disabled in the BIOS the
+        * option-rom capability structure will not be available.
+        */
+       const struct imsm_orom *orom;
+       struct sys_dev *list, *hba;
+       DIR *dir;
+       struct dirent *ent;
+       const char *hba_path;
+       int host_base = 0;
+       int port_count = 0;
+
+       if (enumerate_only) {
+               if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
+                       return 0;
+               return 2;
+       }
+
+       list = find_driver_devices("pci", "ahci");
+       for (hba = list; hba; hba = hba->next)
+               if (devpath_to_vendor(hba->path) == 0x8086)
+                       break;
+
+       if (!hba) {
+               if (verbose)
+                       fprintf(stderr, Name ": unable to find active ahci controller\n");
+               free_sys_dev(&list);
+               return 2;
+       } else if (verbose)
+               fprintf(stderr, Name ": found Intel SATA AHCI Controller\n");
+       hba_path = hba->path;
+       hba->path = NULL;
+       free_sys_dev(&list);
+
+       orom = find_imsm_orom();
+       if (!orom) {
+               if (verbose)
+                       fprintf(stderr, Name ": imsm option-rom not found\n");
+               return 2;
+       }
+
+       printf("       Platform : Intel(R) Matrix Storage Manager\n");
+       printf("        Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
+              orom->hotfix_ver, orom->build);
+       printf("    RAID Levels :%s%s%s%s%s\n",
+              imsm_orom_has_raid0(orom) ? " raid0" : "",
+              imsm_orom_has_raid1(orom) ? " raid1" : "",
+              imsm_orom_has_raid1e(orom) ? " raid1e" : "",
+              imsm_orom_has_raid10(orom) ? " raid10" : "",
+              imsm_orom_has_raid5(orom) ? " raid5" : "");
+       printf("      Max Disks : %d\n", orom->tds);
+       printf("    Max Volumes : %d\n", orom->vpa);
+       printf(" I/O Controller : %s\n", hba_path);
+
+       /* find the smallest scsi host number to determine a port number base */
+       dir = opendir(hba_path);
+       for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
+               int host;
+
+               if (sscanf(ent->d_name, "host%d", &host) != 1)
+                       continue;
+               if (port_count == 0)
+                       host_base = host;
+               else if (host < host_base)
+                       host_base = host;
+
+               if (host + 1 > port_count + host_base)
+                       port_count = host + 1 - host_base;
+
+       }
+       if (dir)
+               closedir(dir);
+
+       if (!port_count || imsm_enumerate_ports(hba_path, port_count,
+                                               host_base, verbose) != 0) {
+               if (verbose)
+                       fprintf(stderr, Name ": failed to enumerate ports\n");
+               return 2;
+       }
+
+       return 0;
+}
 #endif
 
 static int match_home_imsm(struct supertype *st, char *homehost)
@@ -841,7 +1107,7 @@ static int imsm_level_to_layout(int level)
        case 10:
                return 0x102;
        }
-       return -1;
+       return UnSet;
 }
 
 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
@@ -929,8 +1195,11 @@ static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
                info->component_size = reserved;
                s = disk->status;
                info->disk.state  = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
-               info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
                info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC);
+               if (s & FAILED_DISK || super->disks->index == -2) {
+                       info->disk.state |= 1 << MD_DISK_FAULTY;
+                       info->disk.raid_disk = -2;
+               }
        }
 
        /* only call uuid_from_super_imsm when this disk is part of a populated container,
@@ -1010,6 +1279,23 @@ static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
        return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
 }
 
+static void free_devlist(struct intel_super *super)
+{
+       struct intel_dev *dv;
+
+       while (super->devlist) {
+               dv = super->devlist->next;
+               free(super->devlist->dev);
+               free(super->devlist);
+               super->devlist = dv;
+       }
+}
+
+static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
+{
+       memcpy(dest, src, sizeof_imsm_dev(src, 0));
+}
+
 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
 {
        /*
@@ -1046,22 +1332,34 @@ static int compare_super_imsm(struct supertype *st, struct supertype *tst)
        if (first->anchor->num_raid_devs == 0 &&
            sec->anchor->num_raid_devs > 0) {
                int i;
+               struct intel_dev *dv;
+               struct imsm_dev *dev;
 
                /* we need to copy raid device info from sec if an allocation
                 * fails here we don't associate the spare
                 */
                for (i = 0; i < sec->anchor->num_raid_devs; i++) {
-                       first->dev_tbl[i] = malloc(sizeof(struct imsm_dev));
-                       if (!first->dev_tbl) {
-                               while (--i >= 0) {
-                                       free(first->dev_tbl[i]);
-                                       first->dev_tbl[i] = NULL;
-                               }
-                               fprintf(stderr, "imsm: failed to associate spare\n"); 
-                               return 3;
+                       dv = malloc(sizeof(*dv));
+                       if (!dv)
+                               break;
+                       dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
+                       if (!dev) {
+                               free(dv);
+                               break;
                        }
-                       *first->dev_tbl[i] = *sec->dev_tbl[i];
+                       dv->dev = dev;
+                       dv->index = i;
+                       dv->next = first->devlist;
+                       first->devlist = dv;
+               }
+               if (i <= sec->anchor->num_raid_devs) {
+                       /* allocation failure */
+                       free_devlist(first);
+                       fprintf(stderr, "imsm: failed to associate spare\n"); 
+                       return 3;
                }
+               for (i = 0; i < sec->anchor->num_raid_devs; i++)
+                       imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
 
                first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
                first->anchor->family_num = sec->anchor->family_num;
@@ -1126,6 +1424,13 @@ static int imsm_read_serial(int fd, char *devname,
 
        /* trim leading whitespace */
        rsp_len = scsi_serial[3];
+       if (!rsp_len) {
+               if (devname)
+                       fprintf(stderr,
+                               Name ": Failed to retrieve serial for %s\n",
+                               devname);
+               return 2;
+       }
        rsp_buf = (char *) &scsi_serial[4];
        c = rsp_buf;
        while (isspace(*c))
@@ -1253,11 +1558,6 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
        return 0;
 }
 
-static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
-{
-       memcpy(dest, src, sizeof_imsm_dev(src, 0));
-}
-
 #ifndef MDASSEMBLE
 /* When migrating map0 contains the 'destination' state while map1
  * contains the current state.  When not migrating map0 contains the
@@ -1307,17 +1607,26 @@ static int parse_raid_devices(struct intel_super *super)
 
        for (i = 0; i < super->anchor->num_raid_devs; i++) {
                struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
+               struct intel_dev *dv;
 
                len = sizeof_imsm_dev(dev_iter, 0);
                len_migr = sizeof_imsm_dev(dev_iter, 1);
                if (len_migr > len)
                        space_needed += len_migr - len;
                
+               dv = malloc(sizeof(*dv));
+               if (!dv)
+                       return 1;
                dev_new = malloc(len_migr);
-               if (!dev_new)
+               if (!dev_new) {
+                       free(dv);
                        return 1;
+               }
                imsm_copy_dev(dev_new, dev_iter);
-               super->dev_tbl[i] = dev_new;
+               dv->dev = dev_new;
+               dv->index = i;
+               dv->next = super->devlist;
+               super->devlist = dv;
        }
 
        /* ensure that super->buf is large enough when all raid devices
@@ -1492,19 +1801,17 @@ static void free_imsm_disks(struct intel_super *super)
 /* free all the pieces hanging off of a super pointer */
 static void __free_imsm(struct intel_super *super, int free_disks)
 {
-       int i;
-
        if (super->buf) {
                free(super->buf);
                super->buf = NULL;
        }
        if (free_disks)
                free_imsm_disks(super);
-       for (i = 0; i < IMSM_MAX_RAID_DEVS; i++)
-               if (super->dev_tbl[i]) {
-                       free(super->dev_tbl[i]);
-                       super->dev_tbl[i] = NULL;
-               }
+       free_devlist(super);
+       if (super->hba) {
+               free((void *) super->hba);
+               super->hba = NULL;
+       }
 }
 
 static void free_imsm(struct intel_super *super)
@@ -1533,6 +1840,22 @@ static struct intel_super *alloc_super(int creating_imsm)
                super->creating_imsm = creating_imsm;
                super->current_vol = -1;
                super->create_offset = ~((__u32 ) 0);
+               if (!check_env("IMSM_NO_PLATFORM"))
+                       super->orom = find_imsm_orom();
+               if (super->orom) {
+                       struct sys_dev *list, *ent;
+
+                       /* find the first intel ahci controller */
+                       list = find_driver_devices("pci", "ahci");
+                       for (ent = list; ent; ent = ent->next)
+                               if (devpath_to_vendor(ent->path) == 0x8086)
+                                       break;
+                       if (ent) {
+                               super->hba = ent->path;
+                               ent->path = NULL;
+                       }
+                       free_sys_dev(&list);
+               }
        }
 
        return super;
@@ -1608,7 +1931,7 @@ static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
        for (sd = sra->devs; sd; sd = sd->next) {
                sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
                dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
-               if (!dfd) {
+               if (dfd < 0) {
                        free_imsm(super);
                        return 2;
                }
@@ -1638,7 +1961,7 @@ static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
        /* load the most up to date anchor */
        sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
        dfd = dev_open(nm, O_RDONLY);
-       if (!dfd) {
+       if (dfd < 0) {
                free_imsm(super);
                return 1;
        }
@@ -1653,7 +1976,7 @@ static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
        for (sd = sra->devs ; sd ; sd = sd->next) {
                sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
                dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
-               if (!dfd) {
+               if (dfd < 0) {
                        free_imsm(super);
                        return 2;
                }
@@ -1750,7 +2073,10 @@ static __u32 info_to_num_data_stripes(mdu_array_info_t *info)
 
 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
 {
-       return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
+       if (info->level == 1)
+               return info->size * 2;
+       else
+               return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
 }
 
 static void imsm_update_version_info(struct intel_super *super)
@@ -1809,6 +2135,7 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
         */
        struct intel_super *super = st->sb;
        struct imsm_super *mpb = super->anchor;
+       struct intel_dev *dv;
        struct imsm_dev *dev;
        struct imsm_vol *vol;
        struct imsm_map *map;
@@ -1817,9 +2144,9 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
        unsigned long long array_blocks;
        size_t size_old, size_new;
 
-       if (mpb->num_raid_devs >= 2) {
+       if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
                fprintf(stderr, Name": This imsm-container already has the "
-                       "maximum of 2 volumes\n");
+                       "maximum of %d volumes\n", super->orom->vpa);
                return 0;
        }
 
@@ -1849,8 +2176,14 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
        if (super->current_vol == 0)
                mpb->num_disks = 0;
        sprintf(st->subarray, "%d", idx);
+       dv = malloc(sizeof(*dv));
+       if (!dv) {
+               fprintf(stderr, Name ": failed to allocate device list entry\n");
+               return 0;
+       }
        dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
        if (!dev) {
+               free(dv);
                fprintf(stderr, Name": could not allocate raid device\n");
                return 0;
        }
@@ -1897,7 +2230,11 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
                set_imsm_ord_tbl_ent(map, i, 0);
        }
        mpb->num_raid_devs++;
-       super->dev_tbl[super->current_vol] = dev;
+
+       dv->dev = dev;
+       dv->index = super->current_vol;
+       dv->next = super->devlist;
+       super->devlist = dv;
 
        imsm_update_version_info(super);
 
@@ -2014,6 +2351,16 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
        int rv;
        struct stat stb;
 
+       /* if we are on an RAID enabled platform check that the disk is
+        * attached to the raid controller
+        */
+       if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
+               fprintf(stderr,
+                       Name ": %s is not attached to the raid controller: %s\n",
+                       devname ? : "disk", super->hba);
+               return 1;
+       }
+
        if (super->current_vol >= 0)
                return add_to_super_imsm_volume(st, dk, fd, devname);
 
@@ -2131,7 +2478,7 @@ static int write_super_imsm(struct intel_super *super, int doclose)
        for (i = 0; i < mpb->num_raid_devs; i++) {
                struct imsm_dev *dev = __get_imsm_dev(mpb, i);
 
-               imsm_copy_dev(dev, super->dev_tbl[i]);
+               imsm_copy_dev(dev, get_imsm_dev(super, i));
                mpb_size += sizeof_imsm_dev(dev, 0);
        }
        mpb_size += __le32_to_cpu(mpb->bbm_log_size);
@@ -2282,12 +2629,25 @@ static int validate_geometry_imsm_container(struct supertype *st, int level,
 {
        int fd;
        unsigned long long ldsize;
+       const struct imsm_orom *orom;
 
        if (level != LEVEL_CONTAINER)
                return 0;
        if (!dev)
                return 1;
 
+       if (check_env("IMSM_NO_PLATFORM"))
+               orom = NULL;
+       else
+               orom = find_imsm_orom();
+       if (orom && raiddisks > orom->tds) {
+               if (verbose)
+                       fprintf(stderr, Name ": %d exceeds maximum number of"
+                               " platform supported disks: %d\n",
+                               raiddisks, orom->tds);
+               return 0;
+       }
+
        fd = open(dev, O_RDONLY|O_EXCL, 0);
        if (fd < 0) {
                if (verbose)
@@ -2408,6 +2768,29 @@ static unsigned long long merge_extents(struct intel_super *super, int sum_exten
        return maxsize - reserve;
 }
 
+static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
+{
+       if (level < 0 || level == 6 || level == 4)
+               return 0;
+
+       /* if we have an orom prevent invalid raid levels */
+       if (orom)
+               switch (level) {
+               case 0: return imsm_orom_has_raid0(orom);
+               case 1:
+                       if (raiddisks > 2)
+                               return imsm_orom_has_raid1e(orom);
+                       return imsm_orom_has_raid1(orom) && raiddisks == 2;
+               case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
+               case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
+               }
+       else
+               return 1; /* not on an Intel RAID platform so anything goes */
+
+       return 0;
+}
+
+#define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd 
  * FIX ME add ahci details
  */
@@ -2419,32 +2802,44 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
 {
        struct stat stb;
        struct intel_super *super = st->sb;
+       struct imsm_super *mpb = super->anchor;
        struct dl *dl;
        unsigned long long pos = 0;
        unsigned long long maxsize;
        struct extent *e;
        int i;
 
-       if (level == LEVEL_CONTAINER)
+       /* We must have the container info already read in. */
+       if (!super)
                return 0;
 
-       if (level == 1 && raiddisks > 2) {
-               if (verbose)
-                       fprintf(stderr, Name ": imsm does not support more "
-                               "than 2 in a raid1 configuration\n");
+       if (!is_raid_level_supported(super->orom, level, raiddisks)) {
+               pr_vrb(": platform does not support raid%d with %d disk%s\n",
+                       level, raiddisks, raiddisks > 1 ? "s" : "");
                return 0;
        }
-
-       /* We must have the container info already read in. */
-       if (!super)
+       if (super->orom && level != 1 &&
+           !imsm_orom_has_chunk(super->orom, chunk)) {
+               pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
                return 0;
+       }
+       if (layout != imsm_level_to_layout(level)) {
+               if (level == 5)
+                       pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
+               else if (level == 10)
+                       pr_vrb(": imsm raid 10 only supports the n2 layout\n");
+               else
+                       pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
+                               layout, level);
+               return 0;
+       }
 
        if (!dev) {
                /* General test:  make sure there is space for
                 * 'raiddisks' device extents of size 'size' at a given
                 * offset
                 */
-               unsigned long long minsize = size*2 /* convert to blocks */;
+               unsigned long long minsize = size;
                unsigned long long start_offset = ~0ULL;
                int dcnt = 0;
                if (minsize == 0)
@@ -2501,6 +2896,17 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
                        fprintf(stderr, Name ": %s is not in the "
                                "same imsm set\n", dev);
                return 0;
+       } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
+               /* If a volume is present then the current creation attempt
+                * cannot incorporate new spares because the orom may not
+                * understand this configuration (all member disks must be
+                * members of each array in the container).
+                */
+               fprintf(stderr, Name ": %s is a spare and a volume"
+                       " is already defined for this container\n", dev);
+               fprintf(stderr, Name ": The option-rom requires all member"
+                       " disks to be a member of all volumes\n");
+               return 0;
        }
 
        /* retrieve the largest free space block */
@@ -2574,6 +2980,14 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,
                                                        verbose);
        }
        
+       if (!dev) {
+               if (st->sb && freesize) {
+                       /* Should do auto-layout here */
+                       fprintf(stderr, Name ": IMSM does not support auto-layout yet\n");
+                       return 0;
+               }
+               return 1;
+       }
        if (st->sb) {
                /* creating in a given container */
                return validate_geometry_imsm_volume(st, level, layout,
@@ -3088,18 +3502,21 @@ static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_a
        return dl;
 }
 
-static struct dl *imsm_add_spare(struct intel_super *super, int slot, struct active_array *a)
+static struct dl *imsm_add_spare(struct intel_super *super, int slot,
+                                struct active_array *a, int activate_new)
 {
        struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
        int idx = get_imsm_disk_idx(dev, slot);
-       struct imsm_map *map = get_imsm_map(dev, 0);
+       struct imsm_super *mpb = super->anchor;
+       struct imsm_map *map;
        unsigned long long esize;
        unsigned long long pos;
        struct mdinfo *d;
        struct extent *ex;
-       int j;
+       int i, j;
        int found;
        __u32 array_start;
+       __u32 blocks;
        struct dl *dl;
 
        for (dl = super->disks; dl; dl = dl->next) {
@@ -3123,44 +3540,66 @@ static struct dl *imsm_add_spare(struct intel_super *super, int slot, struct act
                        continue;
                }
 
+               /* skip pure spares when we are looking for partially
+                * assimilated drives
+                */
+               if (dl->index == -1 && !activate_new)
+                       continue;
+
                /* Does this unused device have the requisite free space?
-                * We need a->info.component_size sectors
+                * It needs to be able to cover all member volumes
                 */
                ex = get_extents(super, dl);
                if (!ex) {
                        dprintf("cannot get extents\n");
                        continue;
                }
-               found = 0;
-               j = 0;
-               pos = 0;
-               array_start = __le32_to_cpu(map->pba_of_lba0);
+               for (i = 0; i < mpb->num_raid_devs; i++) {
+                       dev = get_imsm_dev(super, i);
+                       map = get_imsm_map(dev, 0);
 
-               do {
-                       /* check that we can start at pba_of_lba0 with
-                        * a->info.component_size of space
+                       /* check if this disk is already a member of
+                        * this array
                         */
-                       esize = ex[j].start - pos;
-                       if (array_start >= pos &&
-                           array_start + a->info.component_size < ex[j].start) {
-                               found = 1;
+                       for (j = 0; j < map->num_members; j++)
+                               if (get_imsm_disk_idx(dev, j) == dl->index)
+                                       break;
+                       if (j < map->num_members)
+                               continue;
+
+                       found = 0;
+                       j = 0;
+                       pos = 0;
+                       array_start = __le32_to_cpu(map->pba_of_lba0);
+                       blocks = __le32_to_cpu(map->blocks_per_member);
+
+                       do {
+                               /* check that we can start at pba_of_lba0 with
+                                * blocks_per_member of space
+                                */
+                               esize = ex[j].start - pos;
+                               if (array_start >= pos &&
+                                   array_start + blocks < ex[j].start) {
+                                       found = 1;
+                                       break;
+                               }
+                               pos = ex[j].start + ex[j].size;
+                               j++;
+                       } while (ex[j-1].size);
+
+                       if (!found)
                                break;
-                       }
-                       pos = ex[j].start + ex[j].size;
-                       j++;
-                           
-               } while (ex[j-1].size);
+               }
 
                free(ex);
-               if (!found) {
-                       dprintf("%x:%x does not have %llu at %d\n",
+               if (i < mpb->num_raid_devs) {
+                       dprintf("%x:%x does not have %u at %u\n",
                                dl->major, dl->minor,
-                               a->info.component_size,
-                               __le32_to_cpu(map->pba_of_lba0));
+                               blocks, array_start);
                        /* No room */
                        continue;
-               } else
-                       break;
+               }
+               return dl;
        }
 
        return dl;
@@ -3218,12 +3657,17 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
                        continue;
 
                /*
-                * OK, this device needs recovery.  Try to re-add the previous
-                * occupant of this slot, if this fails add a new spare
+                * OK, this device needs recovery.  Try to re-add the
+                * previous occupant of this slot, if this fails see if
+                * we can continue the assimilation of a spare that was
+                * partially assimilated, finally try to activate a new
+                * spare.
                 */
                dl = imsm_readd(super, i, a);
                if (!dl)
-                       dl = imsm_add_spare(super, i, a);
+                       dl = imsm_add_spare(super, i, a, 0);
+               if (!dl)
+                       dl = imsm_add_spare(super, i, a, 1);
                if (!dl)
                        continue;
  
@@ -3461,6 +3905,7 @@ static void imsm_process_update(struct supertype *st,
                 * (FIX ME) notice that its update did not take hold.
                 */
                struct imsm_update_create_array *u = (void *) update->buf;
+               struct intel_dev *dv;
                struct imsm_dev *dev;
                struct imsm_map *map, *new_map;
                unsigned long long start, end;
@@ -3473,14 +3918,14 @@ static void imsm_process_update(struct supertype *st,
                if (u->dev_idx < mpb->num_raid_devs) {
                        dprintf("%s: subarray %d already defined\n",
                                __func__, u->dev_idx);
-                       return;
+                       goto create_error;
                }
 
                /* check update is next in sequence */
                if (u->dev_idx != mpb->num_raid_devs) {
                        dprintf("%s: can not create array %d expected index %d\n",
                                __func__, u->dev_idx, mpb->num_raid_devs);
-                       return;
+                       goto create_error;
                }
 
                new_map = get_imsm_map(&u->dev, 0);
@@ -3505,14 +3950,14 @@ static void imsm_process_update(struct supertype *st,
 
                        if (disks_overlap(super, i, u)) {
                                dprintf("%s: arrays overlap\n", __func__);
-                               return;
+                               goto create_error;
                        }
                }
 
                /* check that prepare update was successful */
                if (!update->space) {
                        dprintf("%s: prepare update failed\n", __func__);
-                       return;
+                       goto create_error;
                }
 
                /* check that all disks are still active before committing
@@ -3524,7 +3969,7 @@ static void imsm_process_update(struct supertype *st,
                        dl = serial_to_dl(inf[i].serial, super);
                        if (!dl) {
                                dprintf("%s: disk disappeared\n", __func__);
-                               return;
+                               goto create_error;
                        }
                }
 
@@ -3542,14 +3987,26 @@ static void imsm_process_update(struct supertype *st,
                        set_imsm_ord_tbl_ent(new_map, i, dl->index);
                }
 
-               dev = update->space;
+               dv = update->space;
+               dev = dv->dev;
                update->space = NULL;
                imsm_copy_dev(dev, &u->dev);
-               super->dev_tbl[u->dev_idx] = dev;
+               dv->index = u->dev_idx;
+               dv->next = super->devlist;
+               super->devlist = dv;
                mpb->num_raid_devs++;
 
                imsm_update_version_info(super);
                break;
+ create_error:
+               /* mdmon knows how to release update->space, but not
+                * ((struct intel_dev *) update->space)->dev
+                */
+               if (update->space) {
+                       dv = update->space;
+                       free(dv->dev);
+               }
+               break;
        }
        case update_add_disk:
 
@@ -3597,6 +4054,7 @@ static void imsm_prepare_update(struct supertype *st,
        switch (type) {
        case update_create_array: {
                struct imsm_update_create_array *u = (void *) update->buf;
+               struct intel_dev *dv;
                struct imsm_dev *dev = &u->dev;
                struct imsm_map *map = get_imsm_map(dev, 0);
                struct dl *dl;
@@ -3606,8 +4064,17 @@ static void imsm_prepare_update(struct supertype *st,
 
                inf = get_disk_info(u);
                len = sizeof_imsm_dev(dev, 1);
-               /* allocate a new super->dev_tbl entry */
-               update->space = malloc(len);
+               /* allocate a new super->devlist entry */
+               dv = malloc(sizeof(*dv));
+               if (dv) {
+                       dv->dev = malloc(len);
+                       if (dv->dev)
+                               update->space = dv;
+                       else {
+                               free(dv);
+                               update->space = NULL;
+                       }
+               }
 
                /* count how many spares will be converted to members */
                for (i = 0; i < map->num_members; i++) {
@@ -3644,7 +4111,7 @@ static void imsm_prepare_update(struct supertype *st,
                        free(super->next_buf);
 
                super->next_len = buf_len;
-               if (posix_memalign(&super->next_buf, buf_len, 512) != 0)
+               if (posix_memalign(&super->next_buf, 512, buf_len) != 0)
                        super->next_buf = NULL;
        }
 }
@@ -3711,6 +4178,7 @@ struct superswitch super_imsm = {
        .write_init_super = write_init_super_imsm,
        .validate_geometry = validate_geometry_imsm,
        .add_to_super   = add_to_super_imsm,
+       .detail_platform = detail_platform_imsm,
 #endif
        .match_home     = match_home_imsm,
        .uuid_from_super= uuid_from_super_imsm,
@@ -3727,8 +4195,10 @@ struct superswitch super_imsm = {
        .free_super     = free_super_imsm,
        .match_metadata_desc = match_metadata_desc_imsm,
        .container_content = container_content_imsm,
+       .default_layout = imsm_level_to_layout,
 
        .external       = 1,
+       .name = "imsm",
 
 #ifndef MDASSEMBLE
 /* for mdmon */