+ wakeup_monitor();
+}
+
+struct metadata_update *update_queue = NULL;
+struct metadata_update *update_queue_handled = NULL;
+struct metadata_update *update_queue_pending = NULL;
+
+static void free_updates(struct metadata_update **update)
+{
+ while (*update) {
+ struct metadata_update *this = *update;
+
+ *update = this->next;
+ free(this->buf);
+ free(this->space);
+ free(this);
+ }
+}
+
+void check_update_queue(struct supertype *container)
+{
+ free_updates(&update_queue_handled);
+
+ if (update_queue == NULL &&
+ update_queue_pending) {
+ update_queue = update_queue_pending;
+ update_queue_pending = NULL;
+ wakeup_monitor();
+ }
+}
+
+static void queue_metadata_update(struct metadata_update *mu)
+{
+ struct metadata_update **qp;
+
+ qp = &update_queue_pending;
+ while (*qp)
+ qp = & ((*qp)->next);
+ *qp = mu;
+}
+
+static void add_disk_to_container(struct supertype *st, struct mdinfo *sd)
+{
+ int dfd;
+ char nm[20];
+ struct supertype *st2;
+ struct metadata_update *update = NULL;
+ struct mdinfo info;
+ mdu_disk_info_t dk = {
+ .number = -1,
+ .major = sd->disk.major,
+ .minor = sd->disk.minor,
+ .raid_disk = -1,
+ .state = 0,
+ };
+
+ dprintf("%s: add %d:%d to container\n",
+ __func__, sd->disk.major, sd->disk.minor);
+
+ sd->next = st->devs;
+ st->devs = sd;
+
+ sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
+ dfd = dev_open(nm, O_RDWR);
+ if (dfd < 0)
+ return;
+
+ /* Check the metadata and see if it is already part of this
+ * array
+ */
+ st2 = dup_super(st);
+ if (st2->ss->load_super(st2, dfd, NULL) == 0) {
+ st2->ss->getinfo_super(st, &info);
+ if (st->ss->compare_super(st, st2) == 0 &&
+ info.disk.raid_disk >= 0) {
+ /* Looks like a good member of array.
+ * Just accept it.
+ * mdadm will incorporate any parts into
+ * active arrays.
+ */
+ st2->ss->free_super(st2);
+ return;
+ }
+ }
+ st2->ss->free_super(st2);
+
+ st->update_tail = &update;
+ st->ss->add_to_super(st, &dk, dfd, NULL);
+ st->ss->write_init_super(st);
+ queue_metadata_update(update);
+ st->update_tail = NULL;