+ /* This update arrived at managemon.
+ * We are about to pass it to monitor.
+ * If a malloc is needed, do it here.
+ */
+ struct ddf_super *ddf = st->sb;
+ __u32 *magic = (__u32*)update->buf;
+ if (*magic == DDF_VD_CONF_MAGIC)
+ if (posix_memalign(&update->space, 512,
+ offsetof(struct vcl, conf)
+ + ddf->conf_rec_len * 512) != 0)
+ update->space = NULL;
+}
+
+/*
+ * Check if the array 'a' is degraded but not failed.
+ * If it is, find as many spares as are available and needed and
+ * arrange for their inclusion.
+ * We only choose devices which are not already in the array,
+ * and prefer those with a spare-assignment to this array.
+ * otherwise we choose global spares - assuming always that
+ * there is enough room.
+ * For each spare that we assign, we return an 'mdinfo' which
+ * describes the position for the device in the array.
+ * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
+ * the new phys_refnum and lba_offset values.
+ *
+ * Only worry about BVDs at the moment.
+ */
+static struct mdinfo *ddf_activate_spare(struct active_array *a,
+ struct metadata_update **updates)
+{
+ int working = 0;
+ struct mdinfo *d;
+ struct ddf_super *ddf = a->container->sb;
+ int global_ok = 0;
+ struct mdinfo *rv = NULL;
+ struct mdinfo *di;
+ struct metadata_update *mu;
+ struct dl *dl;
+ int i;
+ struct vd_config *vc;
+ __u64 *lba;
+
+ 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)
+ working ++;
+ }
+
+ dprintf("ddf_activate: working=%d (%d) level=%d\n", working, a->info.array.raid_disks,
+ a->info.array.level);
+ if (working == a->info.array.raid_disks)
+ return NULL; /* array not degraded */
+ switch (a->info.array.level) {
+ case 1:
+ if (working == 0)
+ return NULL; /* failed */
+ break;
+ case 4:
+ case 5:
+ if (working < a->info.array.raid_disks - 1)
+ return NULL; /* failed */
+ break;
+ case 6:
+ if (working < a->info.array.raid_disks - 2)
+ return NULL; /* failed */
+ break;
+ default: /* concat or stripe */
+ return NULL; /* failed */
+ }
+
+ /* For each slot, if it is not working, find a spare */
+ dl = ddf->dlist;
+ 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 */
+ again:
+ for ( ; dl ; dl = dl->next) {
+ unsigned long long esize;
+ unsigned long long pos;
+ struct mdinfo *d2;
+ int is_global = 0;
+ int is_dedicated = 0;
+ struct extent *ex;
+ int j;
+ /* 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;
+ if (ddf->phys->entries[dl->pdnum].type &
+ __cpu_to_be16(DDF_Spare)) {
+ /* Check spare assign record */
+ if (dl->spare) {
+ if (dl->spare->type & DDF_spare_dedicated) {
+ /* check spare_ents for guid */
+ for (j = 0 ;
+ j < __be16_to_cpu(dl->spare->populated);
+ j++) {
+ if (memcmp(dl->spare->spare_ents[j].guid,
+ ddf->virt->entries[a->info.container_member].guid,
+ DDF_GUID_LEN) == 0)
+ is_dedicated = 1;
+ }
+ } else
+ is_global = 1;
+ }
+ } else if (ddf->phys->entries[dl->pdnum].type &
+ __cpu_to_be16(DDF_Global_Spare)) {
+ is_global = 1;
+ }
+ if ( ! (is_dedicated ||
+ (is_global && global_ok))) {
+ dprintf("%x:%x not suitable: %d %d\n", dl->major, dl->minor,
+ is_dedicated, is_global);
+ continue;
+ }
+
+ /* We are allowed to use this device - is there space?
+ * We need a->info.component_size sectors */
+ ex = get_extents(ddf, dl);
+ if (!ex) {
+ dprintf("cannot get extents\n");
+ continue;
+ }
+ j = 0; pos = 0;
+ esize = 0;
+
+ do {
+ esize = ex[j].start - pos;
+ if (esize >= a->info.component_size)
+ break;
+ pos = ex[i].start + ex[i].size;
+ i++;
+ } while (ex[i-1].size);
+
+ free(ex);
+ if (esize < a->info.component_size) {
+ dprintf("%x:%x has no room: %llu %llu\n", dl->major, dl->minor,
+ esize, a->info.component_size);
+ /* No room */
+ continue;
+ }
+
+ /* Cool, we have a device with some space at pos */
+ di = malloc(sizeof(*di));
+ if (!di)
+ continue;
+ memset(di, 0, sizeof(*di));
+ di->disk.number = i;
+ 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 = dl->pdnum;
+ di->next = rv;
+ rv = di;
+ dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
+ i, pos);
+
+ break;
+ }
+ if (!dl && ! global_ok) {
+ /* not enough dedicated spares, try global */
+ global_ok = 1;
+ dl = ddf->dlist;
+ goto again;
+ }
+ }
+
+ if (!rv)
+ /* No spares found */
+ return rv;
+ /* Now 'rv' has a list of devices to return.
+ * Create a metadata_update record to update the
+ * phys_refnum and lba_offset values
+ */
+ mu = malloc(sizeof(*mu));
+ if (mu && posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) {
+ free(mu);
+ mu = NULL;
+ }
+ if (!mu) {
+ while (rv) {
+ struct mdinfo *n = rv->next;
+
+ free(rv);
+ rv = n;
+ }
+ return NULL;
+ }
+
+ mu->buf = malloc(ddf->conf_rec_len * 512);
+ mu->len = ddf->conf_rec_len;
+ mu->next = *updates;
+ vc = find_vdcr(ddf, a->info.container_member);
+ memcpy(mu->buf, vc, ddf->conf_rec_len * 512);
+
+ vc = (struct vd_config*)mu->buf;
+ lba = (__u64*)&vc->phys_refnum[ddf->mppe];
+ for (di = rv ; di ; di = di->next) {
+ vc->phys_refnum[di->disk.raid_disk] =
+ ddf->phys->entries[dl->pdnum].refnum;
+ lba[di->disk.raid_disk] = di->data_offset;
+ }
+ *updates = mu;
+ return rv;
+}
+#endif /* MDASSEMBLE */
+
+static int ddf_level_to_layout(int level)
+{
+ switch(level) {
+ case 0:
+ case 1:
+ return 0;
+ case 5:
+ return ALGORITHM_LEFT_SYMMETRIC;
+ case 6:
+ return ALGORITHM_ROTATING_N_CONTINUE;
+ case 10:
+ return 0x102;
+ default:
+ return UnSet;
+ }