]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
sysfs: avoid possible data corruption in sys_load.
[thirdparty/mdadm.git] / super-intel.c
index b977a41a95ce522641fd4aab3aabc18ca73ab724..b05243b173cb787ceb0d53381de186fb0ba7d91b 100644 (file)
@@ -147,6 +147,19 @@ struct extent {
        unsigned long long start, size;
 };
 
+/* definition of messages passed to imsm_process_update */
+enum imsm_update_type {
+       update_activate_spare,
+};
+
+struct imsm_update_activate_spare {
+       enum imsm_update_type type;
+       int disk_idx;
+       int slot;
+       int array;
+       struct imsm_update_activate_spare *next;
+};
+
 static struct supertype *match_metadata_desc_imsm(char *arg)
 {
        struct supertype *st;
@@ -1022,6 +1035,24 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
        return 0;
 }
 
+static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
+{
+       if (info->level == 1)
+               return 128;
+       return info->chunk_size >> 9;
+}
+
+static __u32 info_to_num_data_stripes(mdu_array_info_t *info)
+{
+       __u32 num_stripes;
+
+       num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
+       if (info->level == 1)
+               num_stripes /= 2;
+
+       return num_stripes;
+}
+
 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
                                  unsigned long long size, char *name,
                                  char *homehost, int *uuid)
@@ -1037,7 +1068,6 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
        int idx = mpb->num_raid_devs;
        int i;
        unsigned long long array_blocks;
-       unsigned long long sz;
        __u32 offset = 0;
        size_t size_old, size_new;
 
@@ -1090,16 +1120,22 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
        }
        map = &vol->map[0];
        map->pba_of_lba0 = __cpu_to_le32(offset);
-       sz = info->size * 2;
-       map->blocks_per_member = __cpu_to_le32(sz);
-       map->blocks_per_strip = __cpu_to_le16(info->chunk_size >> 9);
-       map->num_data_stripes = __cpu_to_le32(sz / (info->chunk_size >> 9));
+       map->blocks_per_member = __cpu_to_le32(info->size * 2);
+       map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
+       map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info));
        map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
                                       IMSM_T_STATE_NORMAL;
+
+       if (info->level == 1 && info->raid_disks > 2) {
+               fprintf(stderr, Name": imsm does not support more than 2 disks"
+                               "in a raid1 volume\n");
+               return 0;
+       }
        if (info->level == 10)
                map->raid_level = 1;
        else
                map->raid_level = info->level;
+
        map->num_members = info->raid_disks;
        for (i = 0; i < map->num_members; i++) {
                /* initialized in add_to_super */
@@ -1365,9 +1401,11 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
 
        if (!dev) {
                /* General test:  make sure there is space for
-                * 'raiddisks' device extents of size 'size'.
+                * 'raiddisks' device extents of size 'size' at a given
+                * offset
                 */
                unsigned long long minsize = size*2 /* convert to blocks */;
+               unsigned long long start_offset = ~0ULL;
                int dcnt = 0;
                if (minsize == 0)
                        minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
@@ -1383,6 +1421,13 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
                                esize = e[i].start - pos;
                                if (esize >= minsize)
                                        found = 1;
+                               if (found && start_offset == ~0ULL) {
+                                       start_offset = pos;
+                                       break;
+                               } else if (found && pos != start_offset) {
+                                       found = 0;
+                                       break;
+                               }
                                pos = e[i].start + e[i].size;
                                i++;
                        } while (e[i-1].size);
@@ -1627,6 +1672,15 @@ static struct mdinfo *container_content_imsm(struct supertype *st)
 static int imsm_open_new(struct supertype *c, struct active_array *a,
                         char *inst)
 {
+       struct intel_super *super = c->sb;
+       struct imsm_super *mpb = super->mpb;
+       
+       if (atoi(inst) + 1 > mpb->num_raid_devs) {
+               fprintf(stderr, "%s: subarry index %d, out of range\n",
+                       __func__, atoi(inst));
+               return -ENODEV;
+       }
+
        dprintf("imsm: open_new %s\n", inst);
        a->info.container_member = atoi(inst);
        return 0;
@@ -1813,8 +1867,6 @@ static int store_imsm_mpb(int fd, struct intel_super *super)
        if (write(fd, super->buf, 512) != 512)
                return 1;
 
-       fsync(fd);
-
        return 0;
 }
 
@@ -1830,6 +1882,273 @@ static void imsm_sync_metadata(struct supertype *container)
        super->updates_pending = 0;
 }
 
+static struct mdinfo *imsm_activate_spare(struct active_array *a,
+                                         struct metadata_update **updates)
+{
+       /**
+        * Take a device that is marked spare in the metadata and use it to
+        * replace a failed/vacant slot in an array.  There may be a case where
+        * a device is failed in one array but active in a second.
+        * imsm_process_update catches this case and does not clear the SPARE_DISK
+        * flag, allowing the second array to start using the device on failure.
+        * SPARE_DISK is cleared when all arrays are using a device.
+        *
+        * FIXME: is this a valid use of SPARE_DISK?
+        */
+
+       struct intel_super *super = a->container->sb;
+       struct imsm_super *mpb = super->mpb;
+       int inst = a->info.container_member;
+       struct imsm_dev *dev = get_imsm_dev(mpb, inst);
+       struct imsm_map *map = dev->vol.map;
+       int failed = a->info.array.raid_disks;
+       struct mdinfo *rv = NULL;
+       struct mdinfo *d;
+       struct mdinfo *di;
+       struct metadata_update *mu;
+       struct dl *dl;
+       struct imsm_update_activate_spare *u;
+       int num_spares = 0;
+       int i;
+
+       for (d = a->info.devs ; d ; d = d->next) {
+               if ((d->curr_state & DS_FAULTY) &&
+                       d->state_fd >= 0)
+                       /* wait for Removal to happen */
+                       return NULL;
+               if (d->state_fd >= 0)
+                       failed--;
+       }
+
+       dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
+               inst, failed, a->info.array.raid_disks, a->info.array.level);
+       if (imsm_check_degraded(mpb, inst, failed) != IMSM_T_STATE_DEGRADED)
+               return NULL;
+
+       /* For each slot, if it is not working, find a spare */
+       dl = super->disks;
+       for (i = 0; i < a->info.array.raid_disks; i++) {
+               for (d = a->info.devs ; d ; d = d->next)
+                       if (d->disk.raid_disk == i)
+                               break;
+               dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
+               if (d && (d->state_fd >= 0))
+                       continue;
+
+               /* OK, this device needs recovery.  Find a spare */
+               for ( ; dl ; dl = dl->next) {
+                       unsigned long long esize;
+                       unsigned long long pos;
+                       struct mdinfo *d2;
+                       struct extent *ex;
+                       struct imsm_disk *disk;
+                       int j;
+                       int found;
+
+                       /* If in this array, skip */
+                       for (d2 = a->info.devs ; d2 ; d2 = d2->next)
+                               if (d2->disk.major == dl->major &&
+                                   d2->disk.minor == dl->minor) {
+                                       dprintf("%x:%x already in array\n", dl->major, dl->minor);
+                                       break;
+                               }
+                       if (d2)
+                               continue;
+
+                       /* is this unused device marked as a spare? */
+                       disk = get_imsm_disk(mpb, dl->index);
+                       if (!(__le32_to_cpu(disk->status) & SPARE_DISK))
+                               continue;
+
+                       /* We are allowed to use this device - is there space?
+                        * We need a->info.component_size sectors */
+                       ex = get_extents(super, dl);
+                       if (!ex) {
+                               dprintf("cannot get extents\n");
+                               continue;
+                       }
+                       found = 0;
+                       j = 0;
+                       pos = 0;
+                       esize = 0;
+
+                       do {
+                               esize = ex[j].start - pos;
+                               if (esize >= a->info.component_size &&
+                                   pos == __le32_to_cpu(map->pba_of_lba0)) {
+                                       found = 1;
+                                       break;
+                               }
+                               pos = ex[i].start + ex[i].size;
+                               i++;
+                       } while (ex[i-1].size);
+
+                       free(ex);
+                       if (!found) {
+                               dprintf("%x:%x does not have %llu at %d\n",
+                                       dl->major, dl->minor,
+                                       a->info.component_size,
+                                       __le32_to_cpu(map->pba_of_lba0));
+                               /* No room */
+                               continue;
+                       }
+
+                       /* Cool, we have a device with some space at pos */
+                       di = malloc(sizeof(*di));
+                       memset(di, 0, sizeof(*di));
+                       di->disk.number = dl->index;
+                       di->disk.raid_disk = i;
+                       di->disk.major = dl->major;
+                       di->disk.minor = dl->minor;
+                       di->disk.state = 0;
+                       di->data_offset = pos;
+                       di->component_size = a->info.component_size;
+                       di->container_member = inst;
+                       di->next = rv;
+                       rv = di;
+                       num_spares++;
+                       dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
+                               i, pos);
+
+                       break;
+               }
+       }
+
+       if (!rv)
+               /* No spares found */
+               return rv;
+       /* Now 'rv' has a list of devices to return.
+        * Create a metadata_update record to update the
+        * disk_ord_tbl for the array
+        */
+       mu = malloc(sizeof(*mu));
+       mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
+       mu->space = NULL;
+       mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
+       mu->next = *updates;
+       u = (struct imsm_update_activate_spare *) mu->buf;
+
+       for (di = rv ; di ; di = di->next) {
+               u->type = update_activate_spare;
+               u->disk_idx = di->disk.number; 
+               u->slot = di->disk.raid_disk;
+               u->array = inst;
+               u->next = u + 1;
+               u++;
+       }
+       (u-1)->next = NULL;
+       *updates = mu;
+
+       return rv;
+}
+
+static int weight(unsigned int field)
+{
+       int weight;
+
+       for (weight = 0; field; weight++)
+               field &= field - 1;
+
+       return weight;
+}
+
+static void imsm_process_update(struct supertype *st,
+                               struct metadata_update *update)
+{
+       /**
+        * crack open the metadata_update envelope to find the update record
+        * update can be one of:
+        *      update_activate_spare - a spare device has replaced a failed
+        *      device in an array, update the disk_ord_tbl.  If this disk is
+        *      present in all member arrays then also clear the SPARE_DISK
+        *      flag
+        */
+       struct intel_super *super = st->sb;
+       struct imsm_super *mpb = super->mpb;
+       enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
+
+       switch (type) {
+       case update_activate_spare: {
+               struct imsm_update_activate_spare *u = (void *) update->buf; 
+               struct imsm_dev *dev = get_imsm_dev(mpb, u->array);
+               struct imsm_map *map = &dev->vol.map[0];
+               struct active_array *a;
+               struct imsm_disk *disk;
+               __u32 status;
+               struct dl *dl;
+               struct mdinfo *d;
+               unsigned int members;
+               unsigned int found;
+               int victim;
+               int i;
+
+               for (dl = super->disks; dl; dl = dl->next)
+                       if (dl->index == u->disk_idx)
+                               break;
+
+               if (!dl) {
+                       fprintf(stderr, "error: imsm_activate_spare passed "
+                               "an unknown disk_idx: %d\n", u->disk_idx);
+                       return;
+               }
+
+               super->updates_pending++;
+
+               victim = get_imsm_disk_idx(map, u->slot);
+               map->disk_ord_tbl[u->slot] = __cpu_to_le32(u->disk_idx);
+               disk = get_imsm_disk(mpb, u->disk_idx);
+               status = __le32_to_cpu(disk->status);
+               status |= CONFIGURED_DISK;
+               disk->status = __cpu_to_le32(status);
+
+               /* map unique/live arrays using the spare */
+               members = 0;
+               found = 0;
+               for (a = st->arrays; a; a = a->next) {
+                       int inst = a->info.container_member;
+
+                       dev = get_imsm_dev(mpb, inst);
+                       map = &dev->vol.map[0];
+                       if (map->raid_level > 0)
+                               members |= 1 << inst;
+                       for (d = a->info.devs; d; d = d->next)
+                               if (d->disk.major == dl->major &&
+                                   d->disk.minor == dl->minor)
+                                       found |= 1 << inst;
+               }
+
+               /* until all arrays that can absorb this disk have absorbed
+                * this disk it can still be considered a spare
+                */
+               if (weight(found) >= weight(members)) {
+                       status = __le32_to_cpu(disk->status);
+                       status &= ~SPARE_DISK;
+                       disk->status = __cpu_to_le32(status);
+               }
+
+               /* count arrays using the victim in the metadata */
+               found = 0;
+               for (a = st->arrays; a ; a = a->next) {
+                       dev = get_imsm_dev(mpb, a->info.container_member);
+                       map = &dev->vol.map[0];
+                       for (i = 0; i < map->num_members; i++)
+                               if (victim == get_imsm_disk_idx(map, i))
+                                       found++;
+               }
+
+               /* clear some flags if the victim is no longer being
+                * utilized anywhere
+                */
+               disk = get_imsm_disk(mpb, victim);
+               if (!found) {
+                       status = __le32_to_cpu(disk->status);
+                       status &= ~(CONFIGURED_DISK | USABLE_DISK);
+                       disk->status = __cpu_to_le32(status);
+               }
+       }
+       }
+}
+
 struct superswitch super_imsm = {
 #ifndef        MDASSEMBLE
        .examine_super  = examine_super_imsm,
@@ -1864,4 +2183,6 @@ struct superswitch super_imsm = {
        .set_array_state= imsm_set_array_state,
        .set_disk       = imsm_set_disk,
        .sync_metadata  = imsm_sync_metadata,
+       .activate_spare = imsm_activate_spare,
+       .process_update = imsm_process_update,
 };