]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
Remove scattered checks for malloc success.
[thirdparty/mdadm.git] / super-intel.c
index 7803a2e08db10af1718bf4753289bda3e230a9b8..2f04302d5e29de622e763844c086ab418c4b7242 100644 (file)
@@ -295,7 +295,7 @@ struct md_list {
        struct md_list *next;
 };
 
-#define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
+#define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
 
 static __u8 migr_type(struct imsm_dev *dev)
 {
@@ -405,6 +405,7 @@ struct extent {
 enum imsm_reshape_type {
        CH_TAKEOVER,
        CH_MIGRATION,
+       CH_ARRAY_SIZE,
 };
 
 /* definition of messages passed to imsm_process_update */
@@ -418,6 +419,7 @@ enum imsm_update_type {
        update_reshape_migration,
        update_takeover,
        update_general_migration_checkpoint,
+       update_size_change,
 };
 
 struct imsm_update_activate_spare {
@@ -470,6 +472,12 @@ struct imsm_update_reshape_migration {
        int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
 };
 
+struct imsm_update_size_change {
+       enum imsm_update_type type;
+       int subdev;
+       long long new_size;
+};
+
 struct imsm_update_general_migration_checkpoint {
        enum imsm_update_type type;
        __u32 curr_migr_unit;
@@ -517,14 +525,14 @@ const char *get_sys_dev_type(enum sys_dev_type type)
 
 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
 {
-       struct intel_hba *result = malloc(sizeof(*result));
-       if (result) {
-               result->type = device->type;
-               result->path = strdup(device->path);
-               result->next = NULL;
-               if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
-                       result->pci_id++;
-       }
+       struct intel_hba *result = xmalloc(sizeof(*result));
+
+       result->type = device->type;
+       result->path = xstrdup(device->path);
+       result->next = NULL;
+       if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
+               result->pci_id++;
+
        return result;
 }
 
@@ -617,10 +625,7 @@ static struct supertype *match_metadata_desc_imsm(char *arg)
                )
                return NULL;
 
-       st = malloc(sizeof(*st));
-       if (!st)
-               return NULL;
-       memset(st, 0, sizeof(*st));
+       st = xcalloc(1, sizeof(*st));
        st->container_dev = NoMdDev;
        st->ss = &super_imsm;
        st->max_devs = IMSM_MAX_DEVICES;
@@ -910,6 +915,7 @@ static unsigned long long blocks_per_member(struct imsm_map *map)
        return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
 }
 
+#ifndef MDASSEMBLE
 static unsigned long long num_data_stripes(struct imsm_map *map)
 {
        if (map == NULL)
@@ -921,6 +927,7 @@ static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
 {
        split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
 }
+#endif
 
 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
 {
@@ -954,9 +961,7 @@ static struct extent *get_extents(struct intel_super *super, struct dl *dl)
        else
                reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
 
-       rv = malloc(sizeof(struct extent) * (memberships + 1));
-       if (!rv)
-               return NULL;
+       rv = xcalloc(sizeof(struct extent), (memberships + 1));
        e = rv;
 
        for (i = 0; i < super->anchor->num_raid_devs; i++) {
@@ -1331,7 +1336,7 @@ static int imsm_check_attributes(__u32 attributes)
 
        not_supported &= attributes;
        if (not_supported) {
-               fprintf(stderr, Name "(IMSM): Unsupported attributes : %x\n",
+               pr_err("(IMSM): Unsupported attributes : %x\n",
                        (unsigned)__le32_to_cpu(not_supported));
                if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
                        dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
@@ -1563,7 +1568,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
 
        if (port_count > (int)sizeof(port_mask) * 8) {
                if (verbose)
-                       fprintf(stderr, Name ": port_count %d out of range\n", port_count);
+                       pr_err("port_count %d out of range\n", port_count);
                return 2;
        }
 
@@ -1596,14 +1601,14 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                /* 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");
+                               pr_err("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",
+                               pr_err("failed to read device type for %s\n",
                                        path);
                        err = 2;
                        free(device);
@@ -1656,7 +1661,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                c = strchr(&path[hba_len], '/');
                if (!c) {
                        if (verbose)
-                               fprintf(stderr, Name ": %s - invalid path name\n", path + hba_len);
+                               pr_err("%s - invalid path name\n", path + hba_len);
                        err = 2;
                        break;
                }
@@ -1666,7 +1671,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                else {
                        if (verbose) {
                                *c = '/'; /* repair the full string */
-                               fprintf(stderr, Name ": failed to determine port number for %s\n",
+                               pr_err("failed to determine port number for %s\n",
                                        path);
                        }
                        err = 2;
@@ -1715,7 +1720,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
 static void print_found_intel_controllers(struct sys_dev *elem)
 {
        for (; elem; elem = elem->next) {
-               fprintf(stderr, Name ": found Intel(R) ");
+               pr_err("found Intel(R) ");
                if (elem->type == SYS_DEV_SATA)
                        fprintf(stderr, "SATA ");
                else if (elem->type == SYS_DEV_SAS)
@@ -1783,12 +1788,13 @@ static void print_imsm_capability(const struct imsm_orom *orom)
               imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
               imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
               imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
-       printf("      Max Disks : %d\n", orom->tds);
-       printf("    Max Volumes : %d\n", orom->vpa);
        printf("    2TB volumes :%s supported\n",
               (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
        printf("      2TB disks :%s supported\n",
               (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
+       printf("      Max Disks : %d\n", orom->tds);
+       printf("    Max Volumes : %d per array, %d per controller\n",
+              orom->vpa, orom->vphba);
        return;
 }
 
@@ -1831,7 +1837,7 @@ static int detail_platform_imsm(int verbose, int enumerate_only)
        list = find_intel_devices();
        if (!list) {
                if (verbose)
-                       fprintf(stderr, Name ": no active Intel(R) RAID "
+                       pr_err("no active Intel(R) RAID "
                                "controller found.\n");
                free_sys_dev(&list);
                return 2;
@@ -1841,7 +1847,7 @@ static int detail_platform_imsm(int verbose, int enumerate_only)
        for (hba = list; hba; hba = hba->next) {
                orom = find_imsm_capability(hba->type);
                if (!orom)
-                       fprintf(stderr, Name ": imsm capabilities not found for controller: %s (type %s)\n",
+                       pr_err("imsm capabilities not found for controller: %s (type %s)\n",
                                hba->path, get_sys_dev_type(hba->type));
                else
                        print_imsm_capability(orom);
@@ -1855,7 +1861,7 @@ static int detail_platform_imsm(int verbose, int enumerate_only)
                        host_base = ahci_get_port_count(hba->path, &port_count);
                        if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
                                if (verbose)
-                                       fprintf(stderr, Name ": failed to enumerate "
+                                       pr_err("failed to enumerate "
                                                "ports on SATA controller at %s.", hba->pci_id);
                                result |= 2;
                        }
@@ -2031,9 +2037,11 @@ static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map)
 
        switch (get_imsm_raid_level(map)) {
        case 0:
+               return map->num_members;
+               break;
        case 1:
        case 10:
-               return map->num_members;
+               return map->num_members/2;
        case 5:
                return map->num_members - 1;
        default:
@@ -2170,16 +2178,14 @@ static int read_imsm_migr_rec(int fd, struct intel_super *super)
 
        get_dev_size(fd, NULL, &dsize);
        if (lseek64(fd, dsize - MIGR_REC_POSITION, SEEK_SET) < 0) {
-               fprintf(stderr,
-                       Name ": Cannot seek to anchor block: %s\n",
-                       strerror(errno));
+               pr_err("Cannot seek to anchor block: %s\n",
+                      strerror(errno));
                goto out;
        }
        if (read(fd, super->migr_rec_buf, MIGR_REC_BUF_SIZE) !=
                                                            MIGR_REC_BUF_SIZE) {
-               fprintf(stderr,
-                       Name ": Cannot read migr record block: %s\n",
-                       strerror(errno));
+               pr_err("Cannot read migr record block: %s\n",
+                      strerror(errno));
                goto out;
        }
        ret_val = 0;
@@ -2306,7 +2312,7 @@ static int imsm_create_metadata_checkpoint_update(
        update_memory_size =
                sizeof(struct imsm_update_general_migration_checkpoint);
 
-       *u = calloc(1, update_memory_size);
+       *u = xcalloc(1, update_memory_size);
        if (*u == NULL) {
                dprintf("error: cannot get memory for "
                        "imsm_create_metadata_checkpoint_update update\n");
@@ -2376,16 +2382,14 @@ static int write_imsm_migr_rec(struct supertype *st)
                        continue;
                get_dev_size(fd, NULL, &dsize);
                if (lseek64(fd, dsize - MIGR_REC_POSITION, SEEK_SET) < 0) {
-                       fprintf(stderr,
-                               Name ": Cannot seek to anchor block: %s\n",
-                               strerror(errno));
+                       pr_err("Cannot seek to anchor block: %s\n",
+                              strerror(errno));
                        goto out;
                }
                if (write(fd, super->migr_rec_buf, MIGR_REC_BUF_SIZE) !=
                                                            MIGR_REC_BUF_SIZE) {
-                       fprintf(stderr,
-                               Name ": Cannot write migr record block: %s\n",
-                               strerror(errno));
+                       pr_err("Cannot write migr record block: %s\n",
+                              strerror(errno));
                        goto out;
                }
                close(fd);
@@ -2443,6 +2447,32 @@ int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
        }
        return rv;
 }
+static unsigned long long imsm_component_size_aligment_check(int level,
+                                             int chunk_size,
+                                             unsigned long long component_size)
+{
+       unsigned int component_size_alligment;
+
+       /* check component size aligment
+       */
+       component_size_alligment = component_size % (chunk_size/512);
+
+       dprintf("imsm_component_size_aligment_check(Level: %i, "
+               "chunk_size = %i, component_size = %llu), "
+               "component_size_alligment = %u\n",
+               level, chunk_size, component_size,
+               component_size_alligment);
+
+       if (component_size_alligment && (level != 1) && (level != UnSet)) {
+               dprintf("imsm: reported component size alligned from %llu ",
+                       component_size);
+               component_size -= component_size_alligment;
+               dprintf("to %llu (%i).\n",
+                       component_size, component_size_alligment);
+       }
+
+       return component_size;
+}
 
 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
 {
@@ -2454,7 +2484,6 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
        struct imsm_map *map_to_analyse = map;
        struct dl *dl;
        char *devname;
-       unsigned int component_size_alligment;
        int map_disks = info->array.raid_disks;
 
        memset(info, 0, sizeof(*info));
@@ -2537,19 +2566,10 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
        info->data_offset         = pba_of_lba0(map_to_analyse);
        info->component_size      = blocks_per_member(map_to_analyse);
 
-       /* check component size aligment
-        */
-       component_size_alligment =
-               info->component_size % (info->array.chunk_size/512);
-
-       if (component_size_alligment &&
-           (info->array.level != 1) && (info->array.level != UnSet)) {
-               dprintf("imsm: reported component size alligned from %llu ",
-                       info->component_size);
-               info->component_size -= component_size_alligment;
-               dprintf("to %llu (%i).\n",
-                       info->component_size, component_size_alligment);
-       }
+       info->component_size = imsm_component_size_aligment_check(
+                                                       info->array.level,
+                                                       info->array.chunk_size,
+                                                       info->component_size);
 
        memset(info->uuid, 0, sizeof(info->uuid));
        info->recovery_start = MaxSector;
@@ -2820,23 +2840,11 @@ struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
        if (!super || !super->disks)
                return NULL;
        dl = super->disks;
-       mddev = malloc(sizeof(*mddev));
-       if (!mddev) {
-               fprintf(stderr, Name ": Failed to allocate memory.\n");
-               return NULL;
-       }
-       memset(mddev, 0, sizeof(*mddev));
+       mddev = xcalloc(1, sizeof(*mddev));
        while (dl) {
                struct mdinfo *tmp;
                disk = &dl->disk;
-               tmp = malloc(sizeof(*tmp));
-               if (!tmp) {
-                       fprintf(stderr, Name ": Failed to allocate memory.\n");
-                       if (mddev)
-                               sysfs_free(mddev);
-                       return NULL;
-               }
-               memset(tmp, 0, sizeof(*tmp));
+               tmp = xcalloc(1, sizeof(*tmp));
                if (mddev->devs)
                        tmp->next = mddev->devs;
                mddev->devs = tmp;
@@ -3038,14 +3046,8 @@ static int compare_super_imsm(struct supertype *st, struct supertype *tst)
                 * fails here we don't associate the spare
                 */
                for (i = 0; i < sec->anchor->num_raid_devs; i++) {
-                       dv = malloc(sizeof(*dv));
-                       if (!dv)
-                               break;
-                       dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
-                       if (!dev) {
-                               free(dv);
-                               break;
-                       }
+                       dv = xmalloc(sizeof(*dv));
+                       dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
                        dv->dev = dev;
                        dv->index = i;
                        dv->next = first->devlist;
@@ -3120,18 +3122,16 @@ static int imsm_read_serial(int fd, char *devname,
 
        if (rv != 0) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Failed to retrieve serial for %s\n",
-                               devname);
+                       pr_err("Failed to retrieve serial for %s\n",
+                              devname);
                return rv;
        }
 
        rsp_len = scsi_serial[3];
        if (!rsp_len) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Failed to retrieve serial for %s\n",
-                               devname);
+                       pr_err("Failed to retrieve serial for %s\n",
+                              devname);
                return 2;
        }
        rsp_buf = (char *) &scsi_serial[4];
@@ -3220,14 +3220,7 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
        if (rv != 0)
                return 2;
 
-       dl = calloc(1, sizeof(*dl));
-       if (!dl) {
-               if (devname)
-                       fprintf(stderr,
-                               Name ": failed to allocate disk buffer for %s\n",
-                               devname);
-               return 2;
-       }
+       dl = xcalloc(1, sizeof(*dl));
 
        fstat(fd, &stb);
        dl->major = major(stb.st_rdev);
@@ -3241,9 +3234,9 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
        dl->e = NULL;
        fd2devname(fd, name);
        if (devname)
-               dl->devname = strdup(devname);
+               dl->devname = xstrdup(devname);
        else
-               dl->devname = strdup(name);
+               dl->devname = xstrdup(name);
 
        /* look up this disk's index in the current anchor */
        disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
@@ -3370,18 +3363,12 @@ static int parse_raid_devices(struct intel_super *super)
                if (len_migr > len)
                        space_needed += len_migr - len;
 
-               dv = malloc(sizeof(*dv));
-               if (!dv)
-                       return 1;
+               dv = xmalloc(sizeof(*dv));
                if (max_len < len_migr)
                        max_len = len_migr;
                if (max_len > len_migr)
                        space_needed += max_len - len_migr;
-               dev_new = malloc(max_len);
-               if (!dev_new) {
-                       free(dv);
-                       return 1;
-               }
+               dev_new = xmalloc(max_len);
                imsm_copy_dev(dev_new, dev_iter);
                dv->dev = dev_new;
                dv->index = i;
@@ -3476,40 +3463,35 @@ static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
        get_dev_size(fd, NULL, &dsize);
        if (dsize < 1024) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": %s: device to small for imsm\n",
-                               devname);
+                       pr_err("%s: device to small for imsm\n",
+                              devname);
                return 1;
        }
 
        if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
                if (devname)
-                       fprintf(stderr, Name
-                               ": Cannot seek to anchor block on %s: %s\n",
-                               devname, strerror(errno));
+                       pr_err("Cannot seek to anchor block on %s: %s\n",
+                              devname, strerror(errno));
                return 1;
        }
 
        if (posix_memalign((void**)&anchor, 512, 512) != 0) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Failed to allocate imsm anchor buffer"
-                               " on %s\n", devname);
+                       pr_err("Failed to allocate imsm anchor buffer"
+                              " on %s\n", devname);
                return 1;
        }
        if (read(fd, anchor, 512) != 512) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Cannot read anchor block on %s: %s\n",
-                               devname, strerror(errno));
+                       pr_err("Cannot read anchor block on %s: %s\n",
+                              devname, strerror(errno));
                free(anchor);
                return 1;
        }
 
        if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": no IMSM anchor on %s\n", devname);
+                       pr_err("no IMSM anchor on %s\n", devname);
                free(anchor);
                return 2;
        }
@@ -3522,9 +3504,8 @@ static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
        super->len = ROUND_UP(anchor->mpb_size, 512);
        if (posix_memalign(&super->buf, 512, super->len) != 0) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": unable to allocate %zu byte mpb buffer\n",
-                               super->len);
+                       pr_err("unable to allocate %zu byte mpb buffer\n",
+                              super->len);
                free(anchor);
                return 2;
        }
@@ -3534,8 +3515,7 @@ static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
        free(anchor);
 
        if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) {
-               fprintf(stderr, Name
-                       ": %s could not allocate migr_rec buffer\n", __func__);
+               pr_err("%s could not allocate migr_rec buffer\n", __func__);
                free(super->buf);
                return 2;
        }
@@ -3545,11 +3525,10 @@ static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
                check_sum = __gen_imsm_checksum(super->anchor);
                if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
                        if (devname)
-                               fprintf(stderr,
-                                       Name ": IMSM checksum %x != %x on %s\n",
-                                       check_sum,
-                                       __le32_to_cpu(super->anchor->check_sum),
-                                       devname);
+                               pr_err("IMSM checksum %x != %x on %s\n",
+                                      check_sum,
+                                      __le32_to_cpu(super->anchor->check_sum),
+                                      devname);
                        return 2;
                }
 
@@ -3559,27 +3538,24 @@ static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
        /* read the extended mpb */
        if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Cannot seek to extended mpb on %s: %s\n",
-                               devname, strerror(errno));
+                       pr_err("Cannot seek to extended mpb on %s: %s\n",
+                              devname, strerror(errno));
                return 1;
        }
 
        if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Cannot read extended mpb on %s: %s\n",
-                               devname, strerror(errno));
+                       pr_err("Cannot read extended mpb on %s: %s\n",
+                              devname, strerror(errno));
                return 2;
        }
 
        check_sum = __gen_imsm_checksum(super->anchor);
        if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": IMSM checksum %x != %x on %s\n",
-                               check_sum, __le32_to_cpu(super->anchor->check_sum),
-                               devname);
+                       pr_err("IMSM checksum %x != %x on %s\n",
+                              check_sum, __le32_to_cpu(super->anchor->check_sum),
+                              devname);
                return 3;
        }
 
@@ -3718,13 +3694,10 @@ static void free_super_imsm(struct supertype *st)
 
 static struct intel_super *alloc_super(void)
 {
-       struct intel_super *super = malloc(sizeof(*super));
+       struct intel_super *super = xcalloc(1, sizeof(*super));
 
-       if (super) {
-               memset(super, 0, sizeof(*super));
-               super->current_vol = -1;
-               super->create_offset = ~((unsigned long long) 0);
-       }
+       super->current_vol = -1;
+       super->create_offset = ~((unsigned long long) 0);
        return super;
 }
 
@@ -3744,9 +3717,8 @@ static int find_intel_hba_capability(int fd, struct intel_super *super, char *de
        hba_name = find_disk_attached_hba(fd, NULL);
        if (!hba_name) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": %s is not attached to Intel(R) RAID controller.\n",
-                               devname);
+                       pr_err("%s is not attached to Intel(R) RAID controller.\n",
+                              devname);
                return 1;
        }
        rv = attach_hba_to_super(super, hba_name);
@@ -3754,7 +3726,7 @@ static int find_intel_hba_capability(int fd, struct intel_super *super, char *de
                if (devname) {
                        struct intel_hba *hba = super->hba;
 
-                       fprintf(stderr, Name ": %s is attached to Intel(R) %s RAID "
+                       pr_err("%s is attached to Intel(R) %s RAID "
                                "controller (%s),\n"
                                "    but the container is assigned to Intel(R) "
                                "%s RAID controller (",
@@ -3801,13 +3773,11 @@ static int find_missing(struct intel_super *super)
                if (dl)
                        continue;
 
-               dl = malloc(sizeof(*dl));
-               if (!dl)
-                       return 1;
+               dl = xmalloc(sizeof(*dl));
                dl->major = 0;
                dl->minor = 0;
                dl->fd = -1;
-               dl->devname = strdup("missing");
+               dl->devname = xstrdup("missing");
                dl->index = i;
                serialcpy(dl->serial, disk->serial);
                dl->disk = *disk;
@@ -3922,9 +3892,7 @@ static int __prep_thunderdome(struct intel_super **table, int tbl_size,
                            is_failed(&idisk->disk))
                                idisk->disk.status &= ~(SPARE_DISK);
                } else {
-                       idisk = calloc(1, sizeof(*idisk));
-                       if (!idisk)
-                               return -1;
+                       idisk = xcalloc(1, sizeof(*idisk));
                        idisk->owner = IMSM_UNKNOWN_OWNER;
                        idisk->disk = *disk;
                        idisk->next = *disk_list;
@@ -4079,6 +4047,8 @@ imsm_thunderdome(struct intel_super **super_list, int len)
                if (s == champion)
                        continue;
 
+               mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
+
                for (i = 0; i < mpb->num_disks; i++) {
                        struct imsm_disk *disk;
 
@@ -4188,7 +4158,7 @@ static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
 
        /* Check migration compatibility */
        if ((err == 0) && (check_mpb_migr_compatibility(super) != 0)) {
-               fprintf(stderr, Name ": Unsupported migration detected");
+               pr_err("Unsupported migration detected");
                if (devname)
                        fprintf(stderr, " on %s\n", devname);
                else
@@ -4241,7 +4211,7 @@ get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list
                        int lmax = 0;
                        int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
                        if (fd < 0) {
-                               fprintf(stderr, Name ": cannot open device %s: %s\n",
+                               pr_err("cannot open device %s: %s\n",
                                        tmpdev->devname, strerror(errno));
                                err = 8;
                                goto error;
@@ -4322,7 +4292,7 @@ static int get_super_block(struct intel_super **super_list, int devnum, char *de
        } else {
                if (s)
                        free(s);
-               if (dfd)
+               if (dfd >= 0)
                        close(dfd);
        }
        if ((dfd >= 0) && (!keep_fd))
@@ -4382,12 +4352,6 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
        free_super_imsm(st);
 
        super = alloc_super();
-       if (!super) {
-               fprintf(stderr,
-                       Name ": malloc of %zu failed.\n",
-                       sizeof(*super));
-               return 1;
-       }
        /* Load hba and capabilities if they exist.
         * But do not preclude loading metadata in case capabilities or hba are
         * non-compliant and ignore_hw_compat is set.
@@ -4396,8 +4360,7 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
        /* no orom/efi or non-intel hba of the disk */
        if ((rv != 0) && (st->ignore_hw_compat == 0)) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": No OROM/EFI properties for %s\n", devname);
+                       pr_err("No OROM/EFI properties for %s\n", devname);
                free_imsm(super);
                return 2;
        }
@@ -4405,9 +4368,8 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
 
        if (rv) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Failed to load all information "
-                               "sections on %s\n", devname);
+                       pr_err("Failed to load all information "
+                              "sections on %s\n", devname);
                free_imsm(super);
                return rv;
        }
@@ -4423,8 +4385,7 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
        if (load_imsm_migr_rec(super, NULL) == 0) {
                /* Check for unsupported migration features */
                if (check_mpb_migr_compatibility(super) != 0) {
-                       fprintf(stderr,
-                               Name ": Unsupported migration detected");
+                       pr_err("Unsupported migration detected");
                        if (devname)
                                fprintf(stderr, " on %s\n", devname);
                        else
@@ -4518,7 +4479,7 @@ static int check_name(struct intel_super *super, char *name, int quiet)
        }
 
        if (reason && !quiet)
-               fprintf(stderr, Name ": imsm volume name %s\n", reason);
+               pr_err("imsm volume name %s\n", reason);
 
        return !reason;
 }
@@ -4543,7 +4504,7 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
        unsigned long long num_data_stripes;
 
        if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
-               fprintf(stderr, Name": This imsm-container already has the "
+               pr_err("This imsm-container already has the "
                        "maximum of %d volumes\n", super->orom->vpa);
                return 0;
        }
@@ -4556,14 +4517,13 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
                size_t size_round = ROUND_UP(size_new, 512);
 
                if (posix_memalign(&mpb_new, 512, size_round) != 0) {
-                       fprintf(stderr, Name": could not allocate new mpb\n");
+                       pr_err("could not allocate new mpb\n");
                        return 0;
                }
                if (posix_memalign(&super->migr_rec_buf, 512,
                                   MIGR_REC_BUF_SIZE) != 0) {
-                       fprintf(stderr, Name
-                               ": %s could not allocate migr_rec buffer\n",
-                               __func__);
+                       pr_err("%s could not allocate migr_rec buffer\n",
+                              __func__);
                        free(super->buf);
                        free(super);
                        free(mpb_new);
@@ -4607,25 +4567,15 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
                for (d = super->missing; d; d = d->next)
                        missing++;
                if (info->failed_disks > missing) {
-                       fprintf(stderr, Name": unable to add 'missing' disk to container\n");
+                       pr_err("unable to add 'missing' disk to container\n");
                        return 0;
                }
        }
 
        if (!check_name(super, name, 0))
                return 0;
-       dv = malloc(sizeof(*dv));
-       if (!dv) {
-               fprintf(stderr, Name ": failed to allocate device list entry\n");
-               return 0;
-       }
-       dev = calloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
-       if (!dev) {
-               free(dv);
-               fprintf(stderr, Name": could not allocate raid device\n");
-               return 0;
-       }
-
+       dv = xmalloc(sizeof(*dv));
+       dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
        strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
        array_blocks = calc_array_size(info->level, info->raid_disks,
                                               info->layout, info->chunk_size,
@@ -4656,7 +4606,7 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
        if (info->level == 1 && info->raid_disks > 2) {
                free(dev);
                free(dv);
-               fprintf(stderr, Name": imsm does not support more than 2 disks"
+               pr_err("imsm does not support more than 2 disks"
                                "in a raid1 volume\n");
                return 0;
        }
@@ -4723,13 +4673,11 @@ static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
                super = NULL;
        }
        if (!super) {
-               fprintf(stderr, Name
-                       ": %s could not allocate superblock\n", __func__);
+               pr_err("%s could not allocate superblock\n", __func__);
                return 0;
        }
        if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) {
-               fprintf(stderr, Name
-                       ": %s could not allocate migr_rec buffer\n", __func__);
+               pr_err("%s could not allocate migr_rec buffer\n", __func__);
                free(super->buf);
                free(super);
                return 0;
@@ -4770,7 +4718,7 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
        map = get_imsm_map(dev, MAP_0);
 
        if (! (dk->state & (1<<MD_DISK_SYNC))) {
-               fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
+               pr_err("%s: Cannot add spare devices to IMSM volume\n",
                        devname);
                return 1;
        }
@@ -4790,7 +4738,7 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
        }
 
        if (!dl) {
-               fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
+               pr_err("%s is not a member of the same container\n", devname);
                return 1;
        }
 
@@ -4803,7 +4751,7 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
        slot = get_imsm_disk_slot(map, dl->index);
        if (slot >= 0 &&
            (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
-               fprintf(stderr, Name ": %s has been included in this array twice\n",
+               pr_err("%s has been included in this array twice\n",
                        devname);
                return 1;
        }
@@ -4859,7 +4807,7 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
 
                _disk = __get_imsm_disk(mpb, dl->index);
                if (!_dev || !_disk) {
-                       fprintf(stderr, Name ": BUG mpb setup error\n");
+                       pr_err("BUG mpb setup error\n");
                        return 1;
                }
                *_dev = *dev;
@@ -4932,23 +4880,16 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
                return add_to_super_imsm_volume(st, dk, fd, devname);
 
        fstat(fd, &stb);
-       dd = malloc(sizeof(*dd));
-       if (!dd) {
-               fprintf(stderr,
-                       Name ": malloc failed %s:%d.\n", __func__, __LINE__);
-               return 1;
-       }
-       memset(dd, 0, sizeof(*dd));
+       dd = xcalloc(sizeof(*dd), 1);
        dd->major = major(stb.st_rdev);
        dd->minor = minor(stb.st_rdev);
-       dd->devname = devname ? strdup(devname) : NULL;
+       dd->devname = devname ? xstrdup(devname) : NULL;
        dd->fd = fd;
        dd->e = NULL;
        dd->action = DISK_ADD;
        rv = imsm_read_serial(fd, devname, dd->serial);
        if (rv) {
-               fprintf(stderr,
-                       Name ": failed to retrieve scsi serial, aborting\n");
+               pr_err("failed to retrieve scsi serial, aborting\n");
                free(dd);
                abort();
        }
@@ -4990,18 +4931,11 @@ static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
         * is prepared.
         */
        if (!st->update_tail) {
-               fprintf(stderr,
-                       Name ": %s shall be used in mdmon context only"
-                       "(line %d).\n", __func__, __LINE__);
-               return 1;
-       }
-       dd = malloc(sizeof(*dd));
-       if (!dd) {
-               fprintf(stderr,
-                       Name ": malloc failed %s:%d.\n", __func__, __LINE__);
+               pr_err("%s shall be used in mdmon context only"
+                      "(line %d).\n", __func__, __LINE__);
                return 1;
        }
-       memset(dd, 0, sizeof(*dd));
+       dd = xcalloc(1, sizeof(*dd));
        dd->major = dk->major;
        dd->minor = dk->minor;
        dd->fd = -1;
@@ -5048,6 +4982,9 @@ static int write_super_imsm_spares(struct intel_super *super, int doclose)
                        continue;
 
                spare->disk[0] = d->disk;
+               if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
+                       spare->attributes |= MPB_ATTRIB_2TB_DISK;
+
                sum = __gen_imsm_checksum(spare);
                spare->family_num = __cpu_to_le32(sum);
                spare->orig_family_num = 0;
@@ -5179,13 +5116,7 @@ static int create_array(struct supertype *st, int dev_idx)
 
        len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
              sizeof(*inf) * map->num_members;
-       u = malloc(len);
-       if (!u) {
-               fprintf(stderr, "%s: failed to allocate update buffer\n",
-                       __func__);
-               return 1;
-       }
-
+       u = xmalloc(len);
        u->type = update_create_array;
        u->dev_idx = dev_idx;
        imsm_copy_dev(&u->dev, dev);
@@ -5211,13 +5142,7 @@ static int mgmt_disk(struct supertype *st)
                return 0;
 
        len = sizeof(*u);
-       u = malloc(len);
-       if (!u) {
-               fprintf(stderr, "%s: failed to allocate update buffer\n",
-                       __func__);
-               return 1;
-       }
-
+       u = xmalloc(len);
        u->type = update_add_remove_disk;
        append_metadata_update(st, u, len);
 
@@ -5296,7 +5221,7 @@ static int validate_geometry_imsm_container(struct supertype *st, int level,
        fd = open(dev, O_RDONLY|O_EXCL, 0);
        if (fd < 0) {
                if (verbose)
-                       fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
+                       pr_err("imsm: Cannot open %s: %s\n",
                                dev, strerror(errno));
                return 0;
        }
@@ -5309,14 +5234,6 @@ static int validate_geometry_imsm_container(struct supertype *st, int level,
         * note that there is no fd for the disks in array.
         */
        super = alloc_super();
-       if (!super) {
-               fprintf(stderr,
-                       Name ": malloc of %zu failed.\n",
-                       sizeof(*super));
-               close(fd);
-               return 0;
-       }
-
        rv = find_intel_hba_capability(fd, super, verbose ? dev : NULL);
        if (rv != 0) {
 #if DEBUG
@@ -5331,14 +5248,22 @@ static int validate_geometry_imsm_container(struct supertype *st, int level,
                return 0;
        }
        close(fd);
-       if (super->orom && raiddisks > super->orom->tds) {
-               if (verbose)
-                       fprintf(stderr, Name ": %d exceeds maximum number of"
-                               " platform supported disks: %d\n",
-                               raiddisks, super->orom->tds);
-
-               free_imsm(super);
-               return 0;
+       if (super->orom) {
+               if (raiddisks > super->orom->tds) {
+                       if (verbose)
+                               pr_err("%d exceeds maximum number of"
+                                       " platform supported disks: %d\n",
+                                       raiddisks, super->orom->tds);
+                       free_imsm(super);
+                       return 0;
+               }
+               if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
+                   (ldsize >> 9) >> 32 > 0) {
+                       if (verbose)
+                               pr_err("%s exceeds maximum platform supported size\n", dev);
+                       free_imsm(super);
+                       return 0;
+               }
        }
 
        *freesize = avail_size_imsm(st, ldsize >> 9);
@@ -5380,7 +5305,7 @@ static unsigned long long merge_extents(struct intel_super *super, int sum_exten
         * 'maxsize' given the "all disks in an array must share a common start
         * offset" constraint
         */
-       struct extent *e = calloc(sum_extents, sizeof(*e));
+       struct extent *e = xcalloc(sum_extents, sizeof(*e));
        struct dl *dl;
        int i, j;
        int start_extent;
@@ -5389,9 +5314,6 @@ static unsigned long long merge_extents(struct intel_super *super, int sum_exten
        unsigned long long maxsize;
        unsigned long reserve;
 
-       if (!e)
-               return 0;
-
        /* coalesce and sort all extents. also, check to see if we need to
         * reserve space between member arrays
         */
@@ -5498,17 +5420,15 @@ active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
                        struct dev_member *dev = memb->members;
                        int fd = -1;
                        while(dev && (fd < 0)) {
-                               char *path = malloc(strlen(dev->name) + strlen("/dev/") + 1);
-                               if (path) {
-                                       num = sprintf(path, "%s%s", "/dev/", dev->name);
-                                       if (num > 0)
-                                               fd = open(path, O_RDONLY, 0);
-                                       if ((num <= 0) || (fd < 0)) {
-                                               pr_vrb(": Cannot open %s: %s\n",
-                                                      dev->name, strerror(errno));
-                                       }
-                                       free(path);
+                               char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
+                               num = sprintf(path, "%s%s", "/dev/", dev->name);
+                               if (num > 0)
+                                       fd = open(path, O_RDONLY, 0);
+                               if ((num <= 0) || (fd < 0)) {
+                                       pr_vrb(": Cannot open %s: %s\n",
+                                              dev->name, strerror(errno));
                                }
+                               free(path);
                                dev = dev->next;
                        }
                        found = 0;
@@ -5523,20 +5443,13 @@ active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
                                        }
                                }
                                if (*devlist && (found < dpa)) {
-                                 dv = calloc(1, sizeof(*dv));
-                                       if (dv == NULL)
-                                               fprintf(stderr, Name ": calloc failed\n");
-                                       else {
-                                               dv->devname = malloc(strlen(memb->dev) + strlen("/dev/") + 1);
-                                               if (dv->devname != NULL) {
-                                                       sprintf(dv->devname, "%s%s", "/dev/", memb->dev);
-                                                       dv->found = found;
-                                                       dv->used = 0;
-                                                       dv->next = *devlist;
-                                                       *devlist = dv;
-                                               } else
-                                                       free(dv);
-                                       }
+                                       dv = xcalloc(1, sizeof(*dv));
+                                       dv->devname = xmalloc(strlen(memb->dev) + strlen("/dev/") + 1);
+                                       sprintf(dv->devname, "%s%s", "/dev/", memb->dev);
+                                       dv->found = found;
+                                       dv->used = 0;
+                                       dv->next = *devlist;
+                                       *devlist = dv;
                                }
                        }
                        if (fd >= 0)
@@ -5556,17 +5469,8 @@ get_loop_devices(void)
        struct md_list *dv = NULL;
 
        for(i = 0; i < 12; i++) {
-               dv = calloc(1, sizeof(*dv));
-               if (dv == NULL) {
-                       fprintf(stderr, Name ": calloc failed\n");
-                       break;
-               }
-               dv->devname = malloc(40);
-               if (dv->devname == NULL) {
-                       fprintf(stderr, Name ": malloc failed\n");
-                       free(dv);
-                       break;
-               }
+               dv = xcalloc(1, sizeof(*dv));
+               dv->devname = xmalloc(40);
                sprintf(dv->devname, "/dev/loop%d", i);
                dv->next = devlist;
                devlist = dv;
@@ -5614,25 +5518,14 @@ get_devices(const char *hba_path)
                        fd2devname(fd, buf);
                        close(fd);
                } else {
-                       fprintf(stderr, Name ": cannot open device: %s\n",
+                       pr_err("cannot open device: %s\n",
                                ent->d_name);
                        continue;
                }
 
 
-               dv = calloc(1, sizeof(*dv));
-               if (dv == NULL) {
-                       fprintf(stderr, Name ": malloc failed\n");
-                       err = 1;
-                       break;
-               }
-               dv->devname = strdup(buf);
-               if (dv->devname == NULL) {
-                       fprintf(stderr, Name ": malloc failed\n");
-                       err = 1;
-                       free(dv);
-                       break;
-               }
+               dv = xcalloc(1, sizeof(*dv));
+               dv->devname = xstrdup(buf);
                dv->next = devlist;
                devlist = dv;
        }
@@ -5644,6 +5537,7 @@ get_devices(const char *hba_path)
                        free(dv);
                }
        }
+       closedir(dir);
        return devlist;
 }
 
@@ -5929,7 +5823,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
        mpb = super->anchor;
 
        if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
-               fprintf(stderr, Name ": RAID gemetry validation failed. "
+               pr_err("RAID gemetry validation failed. "
                        "Cannot proceed with the action(s).\n");
                return 0;
        }
@@ -5971,7 +5865,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
                }
                if (dcnt < raiddisks) {
                        if (verbose)
-                               fprintf(stderr, Name ": imsm: Not enough "
+                               pr_err("imsm: Not enough "
                                        "devices with space for this array "
                                        "(%d < %d)\n",
                                        dcnt, raiddisks);
@@ -5992,7 +5886,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
        }
        if (!dl) {
                if (verbose)
-                       fprintf(stderr, Name ": %s is not in the "
+                       pr_err("%s is not in the "
                                "same imsm set\n", dev);
                return 0;
        } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
@@ -6001,14 +5895,14 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
                 * 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"
+               pr_err("%s is a spare and a volume"
                        " is already defined for this container\n", dev);
-               fprintf(stderr, Name ": The option-rom requires all member"
+               pr_err("The option-rom requires all member"
                        " disks to be a member of all volumes\n");
                return 0;
        } else if (super->orom && mpb->num_raid_devs > 0 &&
                   mpb->num_disks != raiddisks) {
-               fprintf(stderr, Name ": The option-rom requires all member"
+               pr_err("The option-rom requires all member"
                        " disks to be a member of all volumes\n");
                return 0;
        }
@@ -6031,13 +5925,13 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
                dl->extent_cnt = i;
        } else {
                if (verbose)
-                       fprintf(stderr, Name ": unable to determine free space for: %s\n",
+                       pr_err("unable to determine free space for: %s\n",
                                dev);
                return 0;
        }
        if (maxsize < size) {
                if (verbose)
-                       fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
+                       pr_err("%s not enough space (%llu < %llu)\n",
                                dev, maxsize, size);
                return 0;
        }
@@ -6052,7 +5946,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
 
        if (!check_env("IMSM_NO_PLATFORM") &&
            mpb->num_raid_devs > 0 && size && size != maxsize) {
-               fprintf(stderr, Name ": attempting to create a second "
+               pr_err("attempting to create a second "
                        "volume with size less then remaining space. "
                        "Aborting...\n");
                return 0;
@@ -6061,10 +5955,10 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
        if (maxsize < size || maxsize == 0) {
                if (verbose) {
                        if (maxsize == 0)
-                               fprintf(stderr, Name ": no free space"
+                               pr_err("no free space"
                                                " left on device. Aborting...\n");
                        else
-                               fprintf(stderr, Name ": not enough space"
+                               pr_err("not enough space"
                                                " to create volume of given size"
                                                " (%llu < %llu). Aborting...\n",
                                                maxsize, size);
@@ -6078,7 +5972,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
                int count = count_volumes(super->hba->path,
                                      super->orom->dpa, verbose);
                if (super->orom->vphba <= count) {
-                       pr_vrb(": platform does not support more then %d raid volumes.\n",
+                       pr_vrb(": platform does not support more than %d raid volumes.\n",
                               super->orom->vphba);
                        return 0;
                }
@@ -6086,7 +5980,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
        return 1;
 }
 
-static int reserve_space(struct supertype *st, int raiddisks,
+static int imsm_get_free_size(struct supertype *st, int raiddisks,
                         unsigned long long size, int chunk,
                         unsigned long long *freesize)
 {
@@ -6138,7 +6032,7 @@ static int reserve_space(struct supertype *st, int raiddisks,
            (super->orom && used && used != raiddisks) ||
            maxsize < minsize ||
            maxsize == 0) {
-               fprintf(stderr, Name ": not enough devices with space to create array.\n");
+               pr_err("not enough devices with space to create array.\n");
                return 0; /* No enough free spaces large enough */
        }
 
@@ -6152,7 +6046,7 @@ static int reserve_space(struct supertype *st, int raiddisks,
        }
        if (!check_env("IMSM_NO_PLATFORM") &&
            mpb->num_raid_devs > 0 && size && size != maxsize) {
-               fprintf(stderr, Name ": attempting to create a second "
+               pr_err("attempting to create a second "
                        "volume with size less then remaining space. "
                        "Aborting...\n");
                return 0;
@@ -6164,9 +6058,32 @@ static int reserve_space(struct supertype *st, int raiddisks,
 
        *freesize = size;
 
+       dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
+
        return 1;
 }
 
+static int reserve_space(struct supertype *st, int raiddisks,
+                        unsigned long long size, int chunk,
+                        unsigned long long *freesize)
+{
+       struct intel_super *super = st->sb;
+       struct dl *dl;
+       int cnt;
+       int rv = 0;
+
+       rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize);
+       if (rv) {
+               cnt = 0;
+               for (dl = super->disks; dl; dl = dl->next)
+                       if (dl->e)
+                               dl->raiddisk = cnt++;
+               rv = 1;
+       }
+
+       return rv;
+}
+
 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
                                  int raiddisks, int *chunk, unsigned long long size,
                                  char *dev, unsigned long long *freesize,
@@ -6212,7 +6129,7 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,
                                                      super->orom->dpa, verbose);
                                if (super->orom->vphba <= count) {
                                        pr_vrb(": platform does not support more"
-                                              "then %d raid volumes.\n",
+                                              " than %d raid volumes.\n",
                                               super->orom->vphba);
                                        return 0;
                                }
@@ -6234,15 +6151,14 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,
        fd = open(dev, O_RDONLY|O_EXCL, 0);
        if (fd >= 0) {
                if (verbose)
-                       fprintf(stderr,
-                               Name ": Cannot create this array on device %s\n",
-                               dev);
+                       pr_err("Cannot create this array on device %s\n",
+                              dev);
                close(fd);
                return 0;
        }
        if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
                if (verbose)
-                       fprintf(stderr, Name ": Cannot open %s: %s\n",
+                       pr_err("Cannot open %s: %s\n",
                                dev, strerror(errno));
                return 0;
        }
@@ -6251,7 +6167,7 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,
        close(fd);
        if (cfd < 0) {
                if (verbose)
-                       fprintf(stderr, Name ": Cannot use %s: It is busy\n",
+                       pr_err("Cannot use %s: It is busy\n",
                                dev);
                return 0;
        }
@@ -6279,7 +6195,7 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,
        }
 
        if (verbose)
-               fprintf(stderr, Name ": failed container membership check\n");
+               pr_err("failed container membership check\n");
 
        close(cfd);
        return 0;
@@ -6327,19 +6243,16 @@ static int kill_subarray_imsm(struct supertype *st)
                        continue;
                sprintf(subarray, "%u", i);
                if (is_subarray_active(subarray, st->devname)) {
-                       fprintf(stderr,
-                               Name ": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
-                               current_vol, i);
+                       pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
+                              current_vol, i);
 
                        return 2;
                }
        }
 
        if (st->update_tail) {
-               struct imsm_update_kill_array *u = malloc(sizeof(*u));
+               struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
 
-               if (!u)
-                       return 2;
                u->type = update_kill_array;
                u->dev_idx = current_vol;
                append_metadata_update(st, u, sizeof(*u));
@@ -6386,8 +6299,7 @@ static int update_subarray_imsm(struct supertype *st, char *subarray,
                int vol;
 
                if (is_subarray_active(subarray, st->devname)) {
-                       fprintf(stderr,
-                               Name ": Unable to update name of active subarray\n");
+                       pr_err("Unable to update name of active subarray\n");
                        return 2;
                }
 
@@ -6399,10 +6311,8 @@ static int update_subarray_imsm(struct supertype *st, char *subarray,
                        return 2;
 
                if (st->update_tail) {
-                       struct imsm_update_rename_array *u = malloc(sizeof(*u));
+                       struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
 
-                       if (!u)
-                               return 2;
                        u->type = update_rename_array;
                        u->dev_idx = vol;
                        snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
@@ -6536,14 +6446,14 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
        /* do not assemble arrays when not all attributes are supported */
        if (imsm_check_attributes(mpb->attributes) == 0) {
                sb_errors = 1;
-               fprintf(stderr, Name ": Unsupported attributes in IMSM metadata."
+               pr_err("Unsupported attributes in IMSM metadata."
                        "Arrays activation is blocked.\n");
        }
 
        /* check for bad blocks */
        if (imsm_bbm_log_size(super->anchor)) {
-               fprintf(stderr, Name ": BBM log found in IMSM metadata."
-                       "Arrays activation is blocked.\n");
+               pr_err("BBM log found in IMSM metadata."
+                      "Arrays activation is blocked.\n");
                sb_errors = 1;
        }
 
@@ -6578,7 +6488,7 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                 */
                if (dev->vol.migr_state &&
                    (migr_type(dev) == MIGR_STATE_CHANGE)) {
-                       fprintf(stderr, Name ": cannot assemble volume '%.16s':"
+                       pr_err("cannot assemble volume '%.16s':"
                                " unsupported migration in progress\n",
                                dev->volume);
                        continue;
@@ -6587,12 +6497,7 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                 * OROM/EFI
                 */
 
-               this = malloc(sizeof(*this));
-               if (!this) {
-                       fprintf(stderr, Name ": failed to allocate %zu bytes\n",
-                               sizeof(*this));
-                       break;
-               }
+               this = xmalloc(sizeof(*this));
 
                super->current_vol = i;
                getinfo_super_imsm_volume(st, this, NULL);
@@ -6606,7 +6511,7 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                                                 map->num_members, /* raid disks */
                                                 &chunk, join_u32(dev->size_low, dev->size_high),
                                                 1 /* verbose */)) {
-                       fprintf(stderr, Name ": IMSM RAID geometry validation"
+                       pr_err("IMSM RAID geometry validation"
                                " failed.  Array %s activation is blocked.\n",
                                dev->volume);
                        this->array.state |=
@@ -6656,21 +6561,7 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                        if (skip)
                                continue;
 
-                       info_d = calloc(1, sizeof(*info_d));
-                       if (!info_d) {
-                               fprintf(stderr, Name ": failed to allocate disk"
-                                       " for volume %.16s\n", dev->volume);
-                               info_d = this->devs;
-                               while (info_d) {
-                                       struct mdinfo *d = info_d->next;
-
-                                       free(info_d);
-                                       info_d = d;
-                               }
-                               free(this);
-                               this = rest;
-                               break;
-                       }
+                       info_d = xcalloc(1, sizeof(*info_d));
                        info_d->next = this->devs;
                        this->devs = info_d;
 
@@ -6959,7 +6850,8 @@ static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
        super->updates_pending++;
 }
 
-static unsigned long long imsm_set_array_size(struct imsm_dev *dev)
+static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
+                                             long long new_size)
 {
        int used_disks = imsm_num_data_members(dev, MAP_0);
        unsigned long long array_blocks;
@@ -6978,8 +6870,17 @@ static unsigned long long imsm_set_array_size(struct imsm_dev *dev)
 
        /* set array size in metadata
         */
-       map = get_imsm_map(dev, MAP_0);
-       array_blocks = blocks_per_member(map) * used_disks;
+       if (new_size <= 0) {
+               /* OLCE size change is caused by added disks
+                */
+               map = get_imsm_map(dev, MAP_0);
+               array_blocks = blocks_per_member(map) * used_disks;
+       } else {
+               /* Online Volume Size Change
+                * Using  available free space
+                */
+               array_blocks = new_size;
+       }
 
        /* round array size down to closest MB
         */
@@ -7036,7 +6937,7 @@ static void imsm_progress_container_reshape(struct intel_super *super)
                memcpy(map2, map, copy_map_size);
                map2->num_members = prev_num_members;
 
-               imsm_set_array_size(dev);
+               imsm_set_array_size(dev, -1);
                super->clean_migration_record_by_mdmon = 1;
                super->updates_pending++;
        }
@@ -7200,6 +7101,8 @@ static void imsm_set_disk(struct active_array *a, int n, int state)
        struct imsm_dev *dev = get_imsm_dev(super, inst);
        struct imsm_map *map = get_imsm_map(dev, MAP_0);
        struct imsm_disk *disk;
+       struct mdinfo *mdi;
+       int recovery_not_finished = 0;
        int failed;
        __u32 ord;
        __u8 map_state;
@@ -7240,6 +7143,21 @@ static void imsm_set_disk(struct active_array *a, int n, int state)
                dprintf("normal: ");
                if (is_rebuilding(dev)) {
                        dprintf("while rebuilding");
+                       /* check if recovery is really finished */
+                       for (mdi = a->info.devs; mdi ; mdi = mdi->next)
+                               if (mdi->recovery_start != MaxSector) {
+                                       recovery_not_finished = 1;
+                                       break;
+                               }
+                       if (recovery_not_finished) {
+                               dprintf("\nimsm: Rebuild has not finished yet, "
+                                               "state not changed");
+                               if (a->last_checkpoint < mdi->recovery_start) {
+                                       a->last_checkpoint = mdi->recovery_start;
+                                       super->updates_pending++;
+                               }
+                               break;
+                       }
                        end_migration(dev, super, map_state);
                        map = get_imsm_map(dev, MAP_0);
                        map->failed_disk_num = ~0;
@@ -7640,10 +7558,7 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
                        continue;
  
                /* found a usable disk with enough space */
-               di = malloc(sizeof(*di));
-               if (!di)
-                       continue;
-               memset(di, 0, sizeof(*di));
+               di = xcalloc(1, sizeof(*di));
 
                /* dl->index will be -1 in the case we are activating a
                 * pristine spare.  imsm_process_update() will create a
@@ -7681,24 +7596,9 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
         * Create a metadata_update record to update the
         * disk_ord_tbl for the array
         */
-       mu = malloc(sizeof(*mu));
-       if (mu) {
-               mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
-               if (mu->buf == NULL) {
-                       free(mu);
-                       mu = NULL;
-               }
-       }
-       if (!mu) {
-               while (rv) {
-                       struct mdinfo *n = rv->next;
-
-                       free(rv);
-                       rv = n;
-               }
-               return NULL;
-       }
-
+       mu = xmalloc(sizeof(*mu));
+       mu->buf = xcalloc(num_spares, 
+                         sizeof(struct imsm_update_activate_spare));
        mu->space = NULL;
        mu->space_list = NULL;
        mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
@@ -7926,7 +7826,7 @@ skip_disk_add:
                        *tofree = *space_list;
                        /* calculate new size
                         */
-                       imsm_set_array_size(new_dev);
+                       imsm_set_array_size(new_dev, -1);
 
                        ret_val = 1;
                }
@@ -7941,6 +7841,44 @@ error_disk_add:
        return ret_val;
 }
 
+static int apply_size_change_update(struct imsm_update_size_change *u,
+               struct intel_super *super)
+{
+       struct intel_dev *id;
+       int ret_val = 0;
+
+       dprintf("apply_size_change_update()\n");
+       if ((u->subdev < 0) ||
+           (u->subdev > 1)) {
+               dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
+               return ret_val;
+       }
+
+       for (id = super->devlist ; id; id = id->next) {
+               if (id->index == (unsigned)u->subdev) {
+                       struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
+                       struct imsm_map *map = get_imsm_map(dev, MAP_0);
+                       int used_disks = imsm_num_data_members(dev, MAP_0);
+                       unsigned long long blocks_per_member;
+
+                       /* calculate new size
+                        */
+                       blocks_per_member = u->new_size / used_disks;
+                       dprintf("imsm: apply_size_change_update(size: %llu, "
+                               "blocks per member: %llu)\n",
+                               u->new_size, blocks_per_member);
+                       set_blocks_per_member(map, blocks_per_member);
+                       imsm_set_array_size(dev, u->new_size);
+
+                       ret_val = 1;
+                       break;
+               }
+       }
+
+       return ret_val;
+}
+
+
 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
                                       struct intel_super *super,
                                       struct active_array *active_array)
@@ -8140,7 +8078,7 @@ static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
                        newmap = get_imsm_map(newdev, MAP_1);
                        memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
 
-                       imsm_set_array_size(newdev);
+                       imsm_set_array_size(newdev, -1);
                }
 
                sp = (void **)id->dev;
@@ -8348,6 +8286,12 @@ static void imsm_process_update(struct supertype *st,
                        super->updates_pending++;
                break;
        }
+       case update_size_change: {
+               struct imsm_update_size_change *u = (void *)update->buf;
+               if (apply_size_change_update(u, super))
+                       super->updates_pending++;
+               break;
+       }
        case update_activate_spare: {
                struct imsm_update_activate_spare *u = (void *) update->buf; 
                if (apply_update_activate_spare(u, super, st->arrays))
@@ -8584,15 +8528,10 @@ static void imsm_prepare_update(struct supertype *st,
                        int num_members = map->num_members;
                        void *space;
                        int size, i;
-                       int err = 0;
                        /* allocate memory for added disks */
                        for (i = 0; i < num_members; i++) {
                                size = sizeof(struct dl);
-                               space = malloc(size);
-                               if (!space) {
-                                       err++;
-                                       break;
-                               }
+                               space = xmalloc(size);
                                *tail = space;
                                tail = space;
                                *tail = NULL;
@@ -8600,24 +8539,11 @@ static void imsm_prepare_update(struct supertype *st,
                        /* allocate memory for new device */
                        size = sizeof_imsm_dev(super->devlist->dev, 0) +
                                (num_members * sizeof(__u32));
-                       space = malloc(size);
-                       if (!space)
-                               err++;
-                       else {
-                               *tail = space;
-                               tail = space;
-                               *tail = NULL;
-                       }
-                       if (!err) {
-                               len = disks_to_mpb_size(num_members * 2);
-                       } else {
-                               /* if allocation didn't success, free buffer */
-                               while (update->space_list) {
-                                       void **sp = update->space_list;
-                                       update->space_list = *sp;
-                                       free(sp);
-                               }
-                       }
+                       space = xmalloc(size);
+                       *tail = space;
+                       tail = space;
+                       *tail = NULL;
+                       len = disks_to_mpb_size(num_members * 2);
                }
 
                break;
@@ -8643,9 +8569,7 @@ static void imsm_prepare_update(struct supertype *st,
                        if (u->new_raid_disks > u->old_raid_disks)
                                size += sizeof(__u32)*2*
                                        (u->new_raid_disks - u->old_raid_disks);
-                       s = malloc(size);
-                       if (!s)
-                               break;
+                       s = xmalloc(size);
                        *space_tail = s;
                        space_tail = s;
                        *space_tail = NULL;
@@ -8679,9 +8603,7 @@ static void imsm_prepare_update(struct supertype *st,
                                if (u->new_raid_disks > u->old_raid_disks)
                                        size += sizeof(__u32)*2*
                                        (u->new_raid_disks - u->old_raid_disks);
-                               s = malloc(size);
-                               if (!s)
-                                       break;
+                               s = xmalloc(size);
                                *space_tail = s;
                                space_tail = s;
                                *space_tail = NULL;
@@ -8694,12 +8616,7 @@ static void imsm_prepare_update(struct supertype *st,
                /* add space for disk in update
                 */
                size = sizeof(struct dl);
-               s = malloc(size);
-               if (!s) {
-                       free(update->space_list);
-                       update->space_list = NULL;
-                       break;
-               }
+               s = xmalloc(size);
                *space_tail = s;
                space_tail = s;
                *space_tail = NULL;
@@ -8742,6 +8659,9 @@ static void imsm_prepare_update(struct supertype *st,
                dprintf("New anchor length is %llu\n", (unsigned long long)len);
                break;
        }
+       case update_size_change: {
+               break;
+       }
        case update_create_array: {
                struct imsm_update_create_array *u = (void *) update->buf;
                struct intel_dev *dv;
@@ -8755,16 +8675,9 @@ static void imsm_prepare_update(struct supertype *st,
                inf = get_disk_info(u);
                len = sizeof_imsm_dev(dev, 1);
                /* 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;
-                       }
-               }
+               dv = xmalloc(sizeof(*dv));
+               dv->dev = xmalloc(len);
+               update->space = dv;
 
                /* count how many spares will be converted to members */
                for (i = 0; i < map->num_members; i++) {
@@ -9096,16 +9009,12 @@ int save_backup_imsm(struct supertype *st,
        unsigned long long start;
        int data_disks = imsm_num_data_members(dev, MAP_0);
 
-       targets = malloc(new_disks * sizeof(int));
-       if (!targets)
-               goto abort;
+       targets = xmalloc(new_disks * sizeof(int));
 
        for (i = 0; i < new_disks; i++)
                targets[i] = -1;
 
-       target_offsets = malloc(new_disks * sizeof(unsigned long long));
-       if (!target_offsets)
-               goto abort;
+       target_offsets = xcalloc(new_disks, sizeof(unsigned long long));
 
        start = info->reshape_progress * 512;
        for (i = 0; i < new_disks; i++) {
@@ -9136,7 +9045,7 @@ int save_backup_imsm(struct supertype *st,
                            start,
                            length,
                            buf) != 0) {
-               fprintf(stderr, Name ": Error restoring stripes\n");
+               pr_err("Error restoring stripes\n");
                goto abort;
        }
 
@@ -9267,13 +9176,10 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
        unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
        if (posix_memalign((void **)&buf, 512, unit_len) != 0)
                goto abort;
-       targets = malloc(new_disks * sizeof(int));
-       if (!targets)
-               goto abort;
+       targets = xcalloc(new_disks, sizeof(int));
 
        if (open_backup_targets(info, new_disks, targets, super, id->dev)) {
-               fprintf(stderr,
-                       Name ": Cannot open some devices belonging to array.\n");
+               pr_err("Cannot open some devices belonging to array.\n");
                goto abort;
        }
 
@@ -9283,30 +9189,26 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
                        continue;
                }
                if (lseek64(targets[i], read_offset, SEEK_SET) < 0) {
-                       fprintf(stderr,
-                               Name ": Cannot seek to block: %s\n",
-                               strerror(errno));
+                       pr_err("Cannot seek to block: %s\n",
+                              strerror(errno));
                        skipped_disks++;
                        continue;
                }
                if ((unsigned)read(targets[i], buf, unit_len) != unit_len) {
-                       fprintf(stderr,
-                               Name ": Cannot read copy area block: %s\n",
-                               strerror(errno));
+                       pr_err("Cannot read copy area block: %s\n",
+                              strerror(errno));
                        skipped_disks++;
                        continue;
                }
                if (lseek64(targets[i], write_offset, SEEK_SET) < 0) {
-                       fprintf(stderr,
-                               Name ": Cannot seek to block: %s\n",
-                               strerror(errno));
+                       pr_err("Cannot seek to block: %s\n",
+                              strerror(errno));
                        skipped_disks++;
                        continue;
                }
                if ((unsigned)write(targets[i], buf, unit_len) != unit_len) {
-                       fprintf(stderr,
-                               Name ": Cannot restore block: %s\n",
-                               strerror(errno));
+                       pr_err("Cannot restore block: %s\n",
+                              strerror(errno));
                        skipped_disks++;
                        continue;
                }
@@ -9316,9 +9218,8 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
                                                         new_disks,
                                                         super,
                                                         id->dev)) {
-               fprintf(stderr,
-                       Name ": Cannot restore data from backup."
-                       " Too many failed disks\n");
+               pr_err("Cannot restore data from backup."
+                      " Too many failed disks\n");
                goto abort;
        }
 
@@ -9391,7 +9292,8 @@ static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor
 
 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
                                                struct geo_params *geo,
-                                               int *old_raid_disks)
+                                               int *old_raid_disks,
+                                               int direction)
 {
        /* currently we only support increasing the number of devices
         * for a container.  This increases the number of device for each
@@ -9415,6 +9317,12 @@ static int imsm_reshape_is_allowed_on_container(struct supertype *st,
                return ret_val;
        }
 
+       if (direction == ROLLBACK_METADATA_CHANGES) {
+               dprintf("imsm: Metadata changes rollback is not supported for "
+                       "container operation.\n");
+               return ret_val;
+       }
+
        info = container_content_imsm(st, NULL);
        for (member = info; member; member = member->next) {
                int result;
@@ -9540,12 +9448,7 @@ static int imsm_create_metadata_update_for_reshape(
        /* now add space for spare disks that we need to add. */
        update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
 
-       u = calloc(1, update_memory_size);
-       if (u == NULL) {
-               dprintf("error: "
-                       "cannot get memory for imsm_update_reshape update\n");
-               return 0;
-       }
+       u = xcalloc(1, update_memory_size);
        u->type = update_reshape_container_disks;
        u->old_raid_disks = old_raid_disks;
        u->new_raid_disks = geo->raid_disks;
@@ -9556,7 +9459,7 @@ static int imsm_create_metadata_update_for_reshape(
 
        if (spares == NULL
            || delta_disks > spares->array.spare_disks) {
-               fprintf(stderr, Name ": imsm: ERROR: Cannot get spare devices "
+               pr_err("imsm: ERROR: Cannot get spare devices "
                        "for %s.\n", geo->dev_name);
                i = -1;
                goto abort;
@@ -9599,6 +9502,38 @@ abort:
        return 0;
 }
 
+
+/******************************************************************************
+ * function: imsm_create_metadata_update_for_size_change()
+ *           Creates update for IMSM array for array size change.
+ *
+ ******************************************************************************/
+static int imsm_create_metadata_update_for_size_change(
+                               struct supertype *st,
+                               struct geo_params *geo,
+                               struct imsm_update_size_change **updatep)
+{
+       struct intel_super *super = st->sb;
+       int update_memory_size = 0;
+       struct imsm_update_size_change *u = NULL;
+
+       dprintf("imsm_create_metadata_update_for_size_change(enter)"
+               " New size = %llu\n", geo->size);
+
+       /* size of all update data without anchor */
+       update_memory_size = sizeof(struct imsm_update_size_change);
+
+       u = xcalloc(1, update_memory_size);
+       u->type = update_size_change;
+       u->subdev = super->current_vol;
+       u->new_size = geo->size;
+
+       dprintf("imsm: reshape update preparation : OK\n");
+       *updatep = u;
+
+       return update_memory_size;
+}
+
 /******************************************************************************
  * function: imsm_create_metadata_update_for_migration()
  *           Creates update for IMSM array.
@@ -9621,12 +9556,7 @@ static int imsm_create_metadata_update_for_migration(
        /* size of all update data without anchor */
        update_memory_size = sizeof(struct imsm_update_reshape_migration);
 
-       u = calloc(1, update_memory_size);
-       if (u == NULL) {
-               dprintf("error: cannot get memory for "
-                       "imsm_create_metadata_update_for_migration\n");
-               return 0;
-       }
+       u = xcalloc(1, update_memory_size);
        u->type = update_reshape_migration;
        u->subdev = super->current_vol;
        u->new_level = geo->level;
@@ -9698,11 +9628,13 @@ static void imsm_update_metadata_locally(struct supertype *st,
 * Function:    imsm_analyze_change
 * Description: Function analyze change for single volume
 *              and validate if transition is supported
-* Parameters:  Geometry parameters, supertype structure
+* Parameters:  Geometry parameters, supertype structure,
+*              metadata change direction (apply/rollback)
 * Returns:     Operation type code on success, -1 if fail
 ****************************************************************************/
 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
-                                          struct geo_params *geo)
+                                          struct geo_params *geo,
+                                          int direction)
 {
        struct mdinfo info;
        int change = -1;
@@ -9712,6 +9644,13 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
        int devNumChange = 0;
        /* imsm compatible layout value for array geometry verification */
        int imsm_layout = -1;
+       int data_disks;
+       struct imsm_dev *dev;
+       struct intel_super *super;
+       long long current_size;
+       unsigned long long free_size;
+       long long max_size;
+       int rv;
 
        getinfo_super_imsm_volume(st, &info, NULL);
        if ((geo->level != info.array.level) &&
@@ -9722,10 +9661,9 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                        if (geo->level == 5) {
                                change = CH_MIGRATION;
                                if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
-                                       fprintf(stderr,
-                                       Name " Error. Requested Layout "
-                                       "not supported (left-asymmetric layout "
-                                       "is supported only)!\n");
+                                       pr_err("Error. Requested Layout "
+                                              "not supported (left-asymmetric layout "
+                                              "is supported only)!\n");
                                        change = -1;
                                        goto analyse_change_exit;
                                }
@@ -9750,10 +9688,9 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                        break;
                }
                if (change == -1) {
-                       fprintf(stderr,
-                               Name " Error. Level Migration from %d to %d "
-                               "not supported!\n",
-                               info.array.level, geo->level);
+                       pr_err("Error. Level Migration from %d to %d "
+                              "not supported!\n",
+                              info.array.level, geo->level);
                        goto analyse_change_exit;
                }
        } else
@@ -9773,10 +9710,9 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                        geo->layout = 0;
                        geo->level = 5;
                } else {
-                       fprintf(stderr,
-                               Name " Error. Layout Migration from %d to %d "
-                               "not supported!\n",
-                               info.array.layout, geo->layout);
+                       pr_err("Error. Layout Migration from %d to %d "
+                              "not supported!\n",
+                              info.array.layout, geo->layout);
                        change = -1;
                        goto analyse_change_exit;
                }
@@ -9793,6 +9729,95 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                geo->chunksize = info.array.chunk_size;
 
        chunk = geo->chunksize / 1024;
+
+       super = st->sb;
+       dev = get_imsm_dev(super, super->current_vol);
+       data_disks = imsm_num_data_members(dev , MAP_0);
+       /* compute current size per disk member
+        */
+       current_size = info.custom_array_size / data_disks;
+
+       if (geo->size > 0) {
+               /* align component size
+                */
+               geo->size = imsm_component_size_aligment_check(
+                                   get_imsm_raid_level(dev->vol.map),
+                                   chunk * 1024,
+                                   geo->size * 2);
+       }
+
+       if ((current_size != geo->size) && (geo->size >= 0)) {
+               if (change != -1) {
+                       pr_err("Error. Size change should be the only "
+                               "one at a time.\n");
+                       change = -1;
+                       goto analyse_change_exit;
+               }
+               if ((super->current_vol + 1) != super->anchor->num_raid_devs) {
+                       pr_err("Error. The last volume in container "
+                              "can be expanded only (%i/%i).\n",
+                              super->current_vol, st->devnum);
+                       goto analyse_change_exit;
+               }
+               /* check the maximum available size
+                */
+               rv =  imsm_get_free_size(st, dev->vol.map->num_members,
+                                        0, chunk, &free_size);
+               if (rv == 0)
+                       /* Cannot find maximum available space
+                        */
+                       max_size = 0;
+               else {
+                       max_size = free_size + current_size;
+                       /* align component size
+                        */
+                       max_size = imsm_component_size_aligment_check(
+                                       get_imsm_raid_level(dev->vol.map),
+                                       chunk * 1024,
+                                       max_size);
+               }
+               if (geo->size == 0) {
+                       /* requested size change to the maximum available size
+                        */
+                       if (max_size == 0) {
+                               pr_err("Error. Cannot find "
+                                       "maximum available space.\n");
+                               change = -1;
+                               goto analyse_change_exit;
+                       } else
+                               geo->size = max_size;
+               }
+
+               if ((direction == ROLLBACK_METADATA_CHANGES)) {
+                       /* accept size for rollback only
+                       */
+               } else {
+                       /* round size due to metadata compatibility
+                       */
+                       geo->size = (geo->size >> SECT_PER_MB_SHIFT)
+                                   << SECT_PER_MB_SHIFT;
+                       dprintf("Prepare update for size change to %llu\n",
+                               geo->size );
+                       if (current_size >= geo->size) {
+                               pr_err("Error. Size expansion is "
+                                      "supported only (current size is %llu, "
+                                      "requested size /rounded/ is %llu).\n",
+                                      current_size, geo->size);
+                               goto analyse_change_exit;
+                       }
+                       if (max_size && geo->size > max_size) {
+                               pr_err("Error. Requested size is larger "
+                                      "than maximum available size (maximum "
+                                      "available size is %llu, "
+                                      "requested size /rounded/ is %llu).\n",
+                                      max_size, geo->size);
+                               goto analyse_change_exit;
+                       }
+               }
+               geo->size *= data_disks;
+               geo->raid_disks = dev->vol.map->num_members;
+               change = CH_ARRAY_SIZE;
+       }
        if (!validate_geometry_imsm(st,
                                    geo->level,
                                    imsm_layout,
@@ -9807,17 +9832,21 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                struct imsm_super *mpb = super->anchor;
 
                if (mpb->num_raid_devs > 1) {
-                       fprintf(stderr,
-                               Name " Error. Cannot perform operation on %s"
-                               "- for this operation it MUST be single "
-                               "array in container\n",
-                               geo->dev_name);
+                       pr_err("Error. Cannot perform operation on %s"
+                              "- for this operation it MUST be single "
+                              "array in container\n",
+                              geo->dev_name);
                        change = -1;
                }
        }
 
 analyse_change_exit:
-
+       if ((direction == ROLLBACK_METADATA_CHANGES) &&
+            ((change == CH_MIGRATION) || (change == CH_TAKEOVER))) {
+               dprintf("imsm: Metadata changes rollback is not supported for "
+                       "migration and takeover operations.\n");
+               change = -1;
+       }
        return change;
 }
 
@@ -9826,9 +9855,7 @@ int imsm_takeover(struct supertype *st, struct geo_params *geo)
        struct intel_super *super = st->sb;
        struct imsm_update_takeover *u;
 
-       u = malloc(sizeof(struct imsm_update_takeover));
-       if (u == NULL)
-               return 1;
+       u = xmalloc(sizeof(struct imsm_update_takeover));
 
        u->type = update_takeover;
        u->subarray = super->current_vol;
@@ -9857,7 +9884,7 @@ int imsm_takeover(struct supertype *st, struct geo_params *geo)
 static int imsm_reshape_super(struct supertype *st, long long size, int level,
                              int layout, int chunksize, int raid_disks,
                              int delta_disks, char *backup, char *dev,
-                             int verbose)
+                             int direction, int verbose)
 {
        int ret_val = 1;
        struct geo_params geo;
@@ -9888,7 +9915,7 @@ static int imsm_reshape_super(struct supertype *st, long long size, int level,
                int old_raid_disks = 0;
 
                if (imsm_reshape_is_allowed_on_container(
-                           st, &geo, &old_raid_disks)) {
+                           st, &geo, &old_raid_disks, direction)) {
                        struct imsm_update_reshape *u = NULL;
                        int len;
 
@@ -9910,7 +9937,7 @@ static int imsm_reshape_super(struct supertype *st, long long size, int level,
                                free(u);
 
                } else {
-                       fprintf(stderr, Name ": (imsm) Operation "
+                       pr_err("(imsm) Operation "
                                "is not allowed on this container\n");
                }
        } else {
@@ -9932,12 +9959,12 @@ static int imsm_reshape_super(struct supertype *st, long long size, int level,
                        dev = dev->next;
                }
                if (dev == NULL) {
-                       fprintf(stderr, Name " Cannot find %s (%i) subarray\n",
+                       pr_err("Cannot find %s (%i) subarray\n",
                                geo.dev_name, geo.dev_id);
                        goto exit_imsm_reshape_super;
                }
                super->current_vol = dev->index;
-               change = imsm_analyze_change(st, &geo);
+               change = imsm_analyze_change(st, &geo, direction);
                switch (change) {
                case CH_TAKEOVER:
                        ret_val = imsm_takeover(st, &geo);
@@ -9962,6 +9989,26 @@ static int imsm_reshape_super(struct supertype *st, long long size, int level,
                                free(u);
                }
                break;
+               case CH_ARRAY_SIZE: {
+                       struct imsm_update_size_change *u = NULL;
+                       int len =
+                               imsm_create_metadata_update_for_size_change(
+                                       st, &geo, &u);
+                       if (len < 1) {
+                               dprintf("imsm: "
+                                       "Cannot prepare update\n");
+                               break;
+                       }
+                       ret_val = 0;
+                       /* update metadata locally */
+                       imsm_update_metadata_locally(st, u, len);
+                       /* and possibly remotely */
+                       if (st->update_tail)
+                               append_metadata_update(st, u, len);
+                       else
+                               free(u);
+               }
+               break;
                default:
                        ret_val = 1;
                }
@@ -10059,8 +10106,10 @@ int check_degradation_change(struct mdinfo *info,
                             int degraded)
 {
        unsigned long long new_degraded;
-       sysfs_get_ll(info, NULL, "degraded", &new_degraded);
-       if (new_degraded != (unsigned long long)degraded) {
+       int rv;
+
+       rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
+       if ((rv == -1) || (new_degraded != (unsigned long long)degraded)) {
                /* check each device to ensure it is still working */
                struct mdinfo *sd;
                new_degraded = 0;
@@ -10150,7 +10199,7 @@ static int imsm_manage_reshape(
        }
        /* Only one volume can migrate at the same time */
        if (migr_vol_qan != 1) {
-               fprintf(stderr, Name " : %s", migr_vol_qan ?
+               pr_err(": %s", migr_vol_qan ?
                        "Number of migrating volumes greater than 1\n" :
                        "There is no volume during migrationg\n");
                goto abort;