]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
Allow test to detect 'resync=DELAYED' state
[thirdparty/mdadm.git] / super-intel.c
index dd3acebf6992565128d81ea42b643d3164088657..f578057d9842b297572bacd6ab11c8ef6cfc4cdc 100644 (file)
@@ -233,6 +233,13 @@ struct intel_dev {
        unsigned index;
 };
 
+struct intel_hba {
+       enum sys_dev_type type;
+       char *path;
+       char *pci_id;
+       struct intel_hba *next;
+};
+
 enum action {
        DISK_REMOVE = 1,
        DISK_ADD
@@ -268,7 +275,7 @@ struct intel_super {
                                      active */
        struct dl *missing; /* disks removed while we weren't looking */
        struct bbm_log *bbm_log;
-       const char *hba; /* device path of the raid controller for this metadata */
+       struct intel_hba *hba; /* device path of the raid controller for this metadata */
        const struct imsm_orom *orom; /* platform firmware support */
        struct intel_super *next; /* (temp) list for disambiguating family_num */
 };
@@ -362,6 +369,114 @@ struct imsm_update_add_remove_disk {
        enum imsm_update_type type;
 };
 
+
+static const char *_sys_dev_type[] = {
+       [SYS_DEV_UNKNOWN] = "Unknown",
+       [SYS_DEV_SAS] = "SAS",
+       [SYS_DEV_SATA] = "SATA"
+};
+
+const char *get_sys_dev_type(enum sys_dev_type type)
+{
+       if (type >= SYS_DEV_MAX)
+               type = SYS_DEV_UNKNOWN;
+
+       return _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++;
+       }
+       return result;
+}
+
+static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
+{
+       struct intel_hba *result=NULL;
+       for (result = hba; result; result = result->next) {
+               if (result->type == device->type && strcmp(result->path, device->path) == 0)
+                       break;
+       }
+       return result;
+}
+
+
+
+static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device,
+                              const char *devname)
+{
+       struct intel_hba *hba;
+
+       /* check if disk attached to Intel HBA */
+       hba = find_intel_hba(super->hba, device);
+       if (hba != NULL)
+               return 1;
+       /* Check if HBA is already attached to super */
+       if (super->hba == NULL) {
+               super->hba = alloc_intel_hba(device);
+               return 1;
+       }
+
+       hba = super->hba;
+       /* Intel metadata allows for all disks attached to the same type HBA.
+        * Do not sypport odf HBA types mixing
+        */
+       if (device->type != hba->type)
+               return 2;
+
+       while (hba->next)
+               hba = hba->next;
+
+       hba->next = alloc_intel_hba(device);
+       return 1;
+}
+
+static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
+{
+       struct sys_dev *list, *elem, *prev;
+       char *disk_path;
+
+       if ((list = find_intel_devices()) == NULL)
+               return 0;
+
+       if (fd < 0)
+               disk_path  = (char *) devname;
+       else
+               disk_path = diskfd_to_devpath(fd);
+
+       if (!disk_path) {
+               free_sys_dev(&list);
+               return 0;
+       }
+
+       for (prev = NULL, elem = list; elem; prev = elem, elem = elem->next) {
+               if (path_attached_to_hba(disk_path, elem->path)) {
+                       if (prev == NULL)
+                               list = list->next;
+                       else
+                               prev->next = elem->next;
+                       elem->next = NULL;
+                       if (disk_path != devname)
+                               free(disk_path);
+                       free_sys_dev(&list);
+                       return elem;
+               }
+       }
+       if (disk_path != devname)
+               free(disk_path);
+       free_sys_dev(&list);
+
+       return NULL;
+}
+
+
 static struct supertype *match_metadata_desc_imsm(char *arg)
 {
        struct supertype *st;
@@ -1018,10 +1133,10 @@ static void brief_detail_super_imsm(struct supertype *st)
 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
 static void fd2devname(int fd, char *name);
 
-static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
+static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
 {
-       /* dump an unsorted list of devices attached to ahci, as well as
-        * non-connected ports
+       /* dump an unsorted list of devices attached to AHCI Intel storage
+        * controller, as well as non-connected ports
         */
        int hba_len = strlen(hba_path) + 1;
        struct dirent *ent;
@@ -1181,6 +1296,51 @@ static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_b
        return err;
 }
 
+
+
+static void print_found_intel_controllers(struct sys_dev *elem)
+{
+       for (; elem; elem = elem->next) {
+               fprintf(stderr, Name ": found Intel(R) ");
+               if (elem->type == SYS_DEV_SATA)
+                       fprintf(stderr, "SATA ");
+               else if (elem->type == SYS_DEV_SAS)
+                       fprintf(stderr, "SAS ");
+               fprintf(stderr, "RAID controller");
+               if (elem->pci_id)
+                       fprintf(stderr, " at %s", elem->pci_id);
+               fprintf(stderr, ".\n");
+       }
+       fflush(stderr);
+}
+
+static int ahci_get_port_count(const char *hba_path, int *port_count)
+{
+       struct dirent *ent;
+       DIR *dir;
+       int host_base = -1;
+
+       *port_count = 0;
+       if ((dir = opendir(hba_path)) == NULL)
+               return -1;
+
+       for (ent = readdir(dir); ent; ent = readdir(dir)) {
+               int host;
+
+               if (sscanf(ent->d_name, "host%d", &host) != 1)
+                       continue;
+               if (*port_count == 0)
+                       host_base = host;
+               else if (host < host_base)
+                       host_base = host;
+
+               if (host + 1 > *port_count + host_base)
+                       *port_count = host + 1 - host_base;
+       }
+       closedir(dir);
+       return host_base;
+}
+
 static int detail_platform_imsm(int verbose, int enumerate_only)
 {
        /* There are two components to imsm platform support, the ahci SATA
@@ -1196,11 +1356,9 @@ static int detail_platform_imsm(int verbose, int enumerate_only)
         */
        const struct imsm_orom *orom;
        struct sys_dev *list, *hba;
-       DIR *dir;
-       struct dirent *ent;
-       const char *hba_path;
        int host_base = 0;
        int port_count = 0;
+       int result=0;
 
        if (enumerate_only) {
                if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
@@ -1208,24 +1366,19 @@ static int detail_platform_imsm(int verbose, int enumerate_only)
                return 2;
        }
 
-       list = find_driver_devices("pci", "ahci");
-       for (hba = list; hba; hba = hba->next)
-               if (devpath_to_vendor(hba->path) == 0x8086)
-                       break;
-
-       if (!hba) {
+       list = find_intel_devices();
+       if (!list) {
                if (verbose)
-                       fprintf(stderr, Name ": unable to find active ahci controller\n");
+                       fprintf(stderr, Name ": no active Intel(R) RAID "
+                               "controller found.\n");
                free_sys_dev(&list);
                return 2;
        } else if (verbose)
-               fprintf(stderr, Name ": found Intel SATA AHCI Controller\n");
-       hba_path = hba->path;
-       hba->path = NULL;
-       free_sys_dev(&list);
+               print_found_intel_controllers(list);
 
        orom = find_imsm_orom();
        if (!orom) {
+               free_sys_dev(&list);
                if (verbose)
                        fprintf(stderr, Name ": imsm option-rom not found\n");
                return 2;
@@ -1259,35 +1412,29 @@ static int detail_platform_imsm(int verbose, int enumerate_only)
               imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
        printf("      Max Disks : %d\n", orom->tds);
        printf("    Max Volumes : %d\n", orom->vpa);
-       printf(" I/O Controller : %s\n", hba_path);
-
-       /* find the smallest scsi host number to determine a port number base */
-       dir = opendir(hba_path);
-       for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
-               int host;
 
-               if (sscanf(ent->d_name, "host%d", &host) != 1)
-                       continue;
-               if (port_count == 0)
-                       host_base = host;
-               else if (host < host_base)
-                       host_base = host;
-
-               if (host + 1 > port_count + host_base)
-                       port_count = host + 1 - host_base;
-
-       }
-       if (dir)
-               closedir(dir);
-
-       if (!port_count || imsm_enumerate_ports(hba_path, port_count,
-                                               host_base, verbose) != 0) {
-               if (verbose)
-                       fprintf(stderr, Name ": failed to enumerate ports\n");
-               return 2;
+       for (hba = list; hba; hba = hba->next) {
+               printf(" I/O Controller : %s (%s)\n",
+                       hba->path, get_sys_dev_type(hba->type));
+
+               if (hba->type == SYS_DEV_SATA) {
+                       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 "
+                                               "ports on SATA controller at %s.", hba->pci_id);
+                               result |= 2;
+                       }
+               } else if (hba->type == SYS_DEV_SAS) {
+                               if (verbose)
+                                       fprintf(stderr, Name ": failed to enumerate "
+                                               "devices on SAS controller at %s.", hba->pci_id);
+                               result |= 2;
+               }
        }
 
-       return 0;
+       free_sys_dev(&list);
+       return result;
 }
 #endif
 
@@ -2540,6 +2687,11 @@ static void free_imsm_disks(struct intel_super *super)
                super->disks = d->next;
                __free_imsm_disk(d);
        }
+       while (super->disk_mgmt_list) {
+               d = super->disk_mgmt_list;
+               super->disk_mgmt_list = d->next;
+               __free_imsm_disk(d);
+       }
        while (super->missing) {
                d = super->missing;
                super->missing = d->next;
@@ -2551,6 +2703,8 @@ static void free_imsm_disks(struct intel_super *super)
 /* free all the pieces hanging off of a super pointer */
 static void __free_imsm(struct intel_super *super, int free_disks)
 {
+       struct intel_hba *elem, *next;
+
        if (super->buf) {
                free(super->buf);
                super->buf = NULL;
@@ -2558,10 +2712,15 @@ static void __free_imsm(struct intel_super *super, int free_disks)
        if (free_disks)
                free_imsm_disks(super);
        free_devlist(super);
-       if (super->hba) {
-               free((void *) super->hba);
-               super->hba = NULL;
+       elem = super->hba;
+       while (elem) {
+               if (elem->path)
+                       free((void *)elem->path);
+               next = elem->next;
+               free(elem);
+               elem = next;
        }
+       super->hba = NULL;
 }
 
 static void free_imsm(struct intel_super *super)
@@ -2591,20 +2750,6 @@ static struct intel_super *alloc_super(void)
                super->create_offset = ~((__u32 ) 0);
                if (!check_env("IMSM_NO_PLATFORM"))
                        super->orom = find_imsm_orom();
-               if (super->orom && !check_env("IMSM_TEST_OROM")) {
-                       struct sys_dev *list, *ent;
-
-                       /* find the first intel ahci controller */
-                       list = find_driver_devices("pci", "ahci");
-                       for (ent = list; ent; ent = ent->next)
-                               if (devpath_to_vendor(ent->path) == 0x8086)
-                                       break;
-                       if (ent) {
-                               super->hba = ent->path;
-                               ent->path = NULL;
-                       }
-                       free_sys_dev(&list);
-               }
        }
 
        return super;
@@ -3454,8 +3599,9 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
        return 0;
 }
 
+
 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
-                             int fd, char *devname)
+                            int fd, char *devname)
 {
        struct intel_super *super = st->sb;
        struct dl *dd;
@@ -3464,14 +3610,48 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
        int rv;
        struct stat stb;
 
-       /* if we are on an RAID enabled platform check that the disk is
-        * attached to the raid controller
+       /* If we are on an RAID enabled platform check that the disk is
+        * attached to the raid controller.
+        * We do not need to test disks attachment for container based additions,
+        * they shall be already tested when container was created/assembled.
         */
-       if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
-               fprintf(stderr,
-                       Name ": %s is not attached to the raid controller: %s\n",
-                       devname ? : "disk", super->hba);
-               return 1;
+       if ((fd != -1) && !check_env("IMSM_NO_PLATFORM")) {
+               struct sys_dev *hba_name;
+               struct intel_hba *hba;
+
+               hba_name = find_disk_attached_hba(fd, NULL);
+               if (!hba_name) {
+                       fprintf(stderr,
+                               Name ": %s is not attached to Intel(R) RAID controller.\n",
+                               devname ? : "disk");
+                       return 1;
+               }
+               rv = attach_hba_to_super(super, hba_name, devname);
+               switch (rv) {
+               case 2:
+                       fprintf(stderr, Name ": %s is attached to Intel(R) %s RAID "
+                               "controller (%s),\n    but the container is assigned to Intel(R) "
+                               "%s RAID controller (",
+                               devname,
+                               get_sys_dev_type(hba_name->type),
+                               hba_name->pci_id ? : "Err!",
+                               get_sys_dev_type(hba_name->type));
+
+                       hba = super->hba;
+                       while (hba) {
+                               fprintf(stderr, "%s", hba->pci_id ? : "Err!");
+                               if (hba->next)
+                                       fprintf(stderr, ", ");
+                               hba = hba->next;
+                       }
+
+                       fprintf(stderr, ").\n"
+                               "    Mixing devices attached to different controllers "
+                               "is not allowed.\n");
+                       free_sys_dev(&hba_name);
+                       return 1;
+               }
+               free_sys_dev(&hba_name);
        }
 
        if (super->current_vol >= 0)
@@ -5769,12 +5949,15 @@ update_reshape_exit:
 }
 
 static int apply_takeover_update(struct imsm_update_takeover *u,
-                                struct intel_super *super)
+                                struct intel_super *super,
+                                void ***space_list)
 {
        struct imsm_dev *dev = NULL;
+       struct intel_dev *dv;
+       struct imsm_dev *dev_new;
        struct imsm_map *map;
        struct dl *dm, *du;
-       struct intel_dev *dv;
+       int i;
 
        for (dv = super->devlist; dv; dv = dv->next)
                if (dv->index == (unsigned int)u->subarray) {
@@ -5788,6 +5971,10 @@ static int apply_takeover_update(struct imsm_update_takeover *u,
        map = get_imsm_map(dev, 0);
 
        if (u->direction == R10_TO_R0) {
+               /* Number of failed disks must be half of initial disk number */
+               if (imsm_count_failed(super, dev) != (map->num_members / 2))
+                       return 0;
+
                /* iterate through devices to mark removed disks as spare */
                for (dm = super->disks; dm; dm = dm->next) {
                        if (dm->disk.status & FAILED_DISK) {
@@ -5803,7 +5990,6 @@ static int apply_takeover_update(struct imsm_update_takeover *u,
                                dm->index = -1;
                        }
                }
-
                /* update map */
                map->num_members = map->num_members / 2;
                map->map_state = IMSM_T_STATE_NORMAL;
@@ -5812,10 +5998,59 @@ static int apply_takeover_update(struct imsm_update_takeover *u,
                map->failed_disk_num = -1;
        }
 
+       if (u->direction == R0_TO_R10) {
+               void **space;
+               /* update slots in current disk list */
+               for (dm = super->disks; dm; dm = dm->next) {
+                       if (dm->index >= 0)
+                               dm->index *= 2;
+               }
+               /* create new *missing* disks */
+               for (i = 0; i < map->num_members; i++) {
+                       space = *space_list;
+                       if (!space)
+                               continue;
+                       *space_list = *space;
+                       du = (void *)space;
+                       memcpy(du, super->disks, sizeof(*du));
+                       du->disk.status = FAILED_DISK;
+                       du->disk.scsi_id = 0;
+                       du->fd = -1;
+                       du->minor = 0;
+                       du->major = 0;
+                       du->index = (i * 2) + 1;
+                       sprintf((char *)du->disk.serial,
+                               " MISSING_%d", du->index);
+                       sprintf((char *)du->serial,
+                               "MISSING_%d", du->index);
+                       du->next = super->missing;
+                       super->missing = du;
+               }
+               /* create new dev and map */
+               space = *space_list;
+               if (!space)
+                       return 0;
+               *space_list = *space;
+               dev_new = (void *)space;
+               memcpy(dev_new, dev, sizeof(*dev));
+               /* update new map */
+               map = get_imsm_map(dev_new, 0);
+               map->failed_disk_num = map->num_members;
+               map->num_members = map->num_members * 2;
+               map->map_state = IMSM_T_STATE_NORMAL;
+               map->num_domains = 2;
+               map->raid_level = 1;
+               /* replace dev<->dev_new */
+               dv->dev = dev_new;
+       }
        /* update disk order table */
        for (du = super->disks; du; du = du->next)
                if (du->index >= 0)
                        set_imsm_ord_tbl_ent(map, du->index, du->index);
+       for (du = super->missing; du; du = du->next)
+               if (du->index >= 0)
+                       set_imsm_ord_tbl_ent(map, du->index,
+                                               du->index | IMSM_ORD_REBUILD);
 
        return 1;
 }
@@ -5864,7 +6099,7 @@ static void imsm_process_update(struct supertype *st,
        switch (type) {
        case update_takeover: {
                struct imsm_update_takeover *u = (void *)update->buf;
-               if (apply_takeover_update(u, super))
+               if (apply_takeover_update(u, super, &update->space_list))
                        super->updates_pending++;
                break;
        }
@@ -6189,6 +6424,53 @@ static void imsm_prepare_update(struct supertype *st,
        size_t len = 0;
 
        switch (type) {
+       case update_takeover: {
+               struct imsm_update_takeover *u = (void *)update->buf;
+               if (u->direction == R0_TO_R10) {
+                       void **tail = (void **)&update->space_list;
+                       struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
+                       struct imsm_map *map = get_imsm_map(dev, 0);
+                       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;
+                               }
+                               *tail = space;
+                               tail = space;
+                               *tail = NULL;
+                       }
+                       /* 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);
+                               }
+                       }
+               }
+
+               break;
+       }
        case update_reshape_container_disks: {
                /* Every raid device in the container is about to
                 * gain some more devices, and we will enter a
@@ -6345,34 +6627,33 @@ static char disk_by_path[] = "/dev/disk/by-path/";
 
 static const char *imsm_get_disk_controller_domain(const char *path)
 {
-       struct sys_dev *list, *hba = NULL;
        char disk_path[PATH_MAX];
-       int ahci = 0;
-       char *dpath = NULL;
-
-       list = find_driver_devices("pci", "ahci");
-       for (hba = list; hba; hba = hba->next)
-               if (devpath_to_vendor(hba->path) == 0x8086)
-                       break;
-
-       if (hba) {
-               struct stat st;
+       char *drv=NULL;
+       struct stat st;
 
-               strncpy(disk_path, disk_by_path, PATH_MAX - 1);
-               strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
-               if (stat(disk_path, &st) == 0) {
-                       dpath = devt_to_devpath(st.st_rdev);
-                       if (dpath)
-                               ahci = path_attached_to_hba(dpath, hba->path);
-               }
+       strncpy(disk_path, disk_by_path, PATH_MAX - 1);
+       strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
+       if (stat(disk_path, &st) == 0) {
+               struct sys_dev* hba;
+               char *path=NULL;
+
+               path = devt_to_devpath(st.st_rdev);
+               if (path == NULL)
+                       return "unknown";
+               hba = find_disk_attached_hba(-1, path);
+               if (hba && hba->type == SYS_DEV_SAS)
+                       drv = "isci";
+               else if (hba && hba->type == SYS_DEV_SATA)
+                       drv = "ahci";
+               else 
+                       drv = "unknown";
+               dprintf("path: %s hba: %s attached: %s\n",
+                       path, (hba) ? hba->path : "NULL", drv);
+               free(path);
+               if (hba)
+                       free_sys_dev(&hba);
        }
-       dprintf("path: %s(%s) hba: %s attached: %d\n",
-               path, dpath, (hba) ? hba->path : "NULL", ahci);
-       free_sys_dev(&list);
-       if (ahci)
-               return "ahci";
-       else
-               return NULL;
+       return drv;
 }
 
 static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor)
@@ -6424,7 +6705,7 @@ static int imsm_reshape_is_allowed_on_container(struct supertype *st,
                dprintf("imsm: checking device_num: %i\n",
                        member->container_member);
 
-               if (geo->raid_disks < member->array.raid_disks) {
+               if (geo->raid_disks <= member->array.raid_disks) {
                        /* we work on container for Online Capacity Expansion
                         * only so raid_disks has to grow
                         */
@@ -6640,6 +6921,12 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                                check_devs = 1;
                        }
                        break;
+               case 1:
+                       if (geo->level == 0) {
+                               change = CH_TAKEOVER;
+                               check_devs = 1;
+                       }
+                       break;
                case 5:
                        if (geo->level != 0)
                                change = CH_LEVEL_MIGRATION;