]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - managemon.c
imsm: store imsm_disk entries in the super->disks list
[thirdparty/mdadm.git] / managemon.c
index 9f3520e8ea8e475e7a535071550fd01943a69aca..7a1c06336a179fe6e3eeacaf534157a6ff56a263 100644 (file)
@@ -85,6 +85,7 @@
 #endif
 #include       "mdadm.h"
 #include       "mdmon.h"
+#include       <sys/syscall.h>
 #include       <sys/socket.h>
 #include       <signal.h>
 
@@ -143,16 +144,11 @@ static struct active_array *duplicate_aa(struct active_array *aa)
        return newa;
 }
 
-static void write_wakeup(struct supertype *c)
+static void wakeup_monitor(void)
 {
-       static struct md_generic_cmd cmd = { .action = md_action_ping_monitor };
-       int err;
-
-       active_cmd = &cmd;
-
-       /* send the monitor thread a pointer to the ping action */
-       write(c->mgr_pipe[1], &err, 1);
-       read(c->mon_pipe[0], &err, 1);
+       /* tgkill(getpid(), mon_tid, SIGUSR1); */
+       int pid = getpid();
+       syscall(SYS_tgkill, pid, mon_tid, SIGUSR1);
 }
 
 static void remove_old(void)
@@ -163,6 +159,7 @@ static void remove_old(void)
                if (pending_discard == discard_this)
                        pending_discard = NULL;
                discard_this = NULL;
+               wakeup_monitor();
        }
 }
 
@@ -178,7 +175,6 @@ static void replace_array(struct supertype *container,
         */
        remove_old();
        while (pending_discard) {
-               write_wakeup(container);
                while (discard_this == NULL)
                        sleep(1);
                remove_old();
@@ -187,7 +183,7 @@ static void replace_array(struct supertype *container,
        new->replaces = old;
        new->next = container->arrays;
        container->arrays = new;
-       write_wakeup(container);
+       wakeup_monitor();
 }
 
 struct metadata_update *update_queue = NULL;
@@ -199,14 +195,16 @@ void check_update_queue(struct supertype *container)
        while (update_queue_handled) {
                struct metadata_update *this = update_queue_handled;
                update_queue_handled = this->next;
-//             free(this->buf);
+               free(this->buf);
+               if (this->space)
+                       free(this->space);
                free(this);
        }
        if (update_queue == NULL &&
            update_queue_pending) {
                update_queue = update_queue_pending;
                update_queue_pending = NULL;
-               write_wakeup(container);
+               wakeup_monitor();
        }
 }
 
@@ -291,7 +289,7 @@ static void manage_member(struct mdstat_ent *mdstat,
                         * and open files for each newdev */
                        for (d = newdev; d ; d = d->next) {
                                struct mdinfo *newd;
-                               if (sysfs_add_disk(&newa->info, d))
+                               if (sysfs_add_disk(&newa->info, d) < 0)
                                        continue;
                                newd = newa->info.devs;
                                newd->state_fd = sysfs_open(a->devnum,
@@ -303,11 +301,32 @@ static void manage_member(struct mdstat_ent *mdstat,
                        }
                        queue_metadata_update(updates);
                        replace_array(a->container, a, newa);
-                       sysfs_set_str(&a->info, NULL, "sync_action", "repair");
+                       sysfs_set_str(&a->info, NULL, "sync_action", "recover");
                }
        }
 }
 
+static int aa_ready(struct active_array *aa)
+{
+       struct mdinfo *d;
+       int level = aa->info.array.level;
+
+       for (d = aa->info.devs; d; d = d->next)
+               if (d->state_fd < 0)
+                       return 0;
+
+       if (aa->info.state_fd < 0)
+               return 0;
+
+       if (level > 0 && (aa->action_fd < 0 || aa->resync_start_fd < 0))
+               return 0;
+
+       if (!aa->container)
+               return 0;
+
+       return 1;
+}
+
 static void manage_new(struct mdstat_ent *mdstat,
                       struct supertype *container,
                       struct active_array *victim)
@@ -323,8 +342,23 @@ static void manage_new(struct mdstat_ent *mdstat,
        char *inst;
        int i;
 
+       /* check if array is ready to be monitored */
+       if (!mdstat->active)
+               return;
+
+       mdi = sysfs_read(-1, mdstat->devnum,
+                        GET_LEVEL|GET_CHUNK|GET_DISKS|GET_COMPONENT|
+                        GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
+
        new = malloc(sizeof(*new));
 
+       if (!new || !mdi) {
+               if (mdi)
+                       sysfs_free(mdi);
+               if (new)
+                       free(new);
+               return;
+       }
        memset(new, 0, sizeof(*new));
 
        new->devnum = mdstat->devnum;
@@ -337,19 +371,8 @@ static void manage_new(struct mdstat_ent *mdstat,
 
        inst = &mdstat->metadata_version[10+strlen(container->devname)+1];
 
-       mdi = sysfs_read(-1, new->devnum,
-                        GET_LEVEL|GET_CHUNK|GET_DISKS|
-                        GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
-       if (!mdi) {
-               /* Eeek. Cannot monitor this array.
-                * Mark it to be ignored by setting container to NULL
-                */
-               new->container = NULL;
-               replace_array(container, victim, new);
-               return;
-       }
-
        new->info.array = mdi->array;
+       new->info.component_size = mdi->component_size;
 
        for (i = 0; i < new->info.array.raid_disks; i++) {
                struct mdinfo *newd = malloc(sizeof(*newd));
@@ -368,32 +391,34 @@ static void manage_new(struct mdstat_ent *mdstat,
                        newd->prev_state = read_dev_state(newd->state_fd);
                        newd->curr_state = newd->prev_state;
                } else {
-                       newd->state_fd = -1;
-                       newd->disk.raid_disk = i;
-                       newd->prev_state = DS_REMOVE;
-                       newd->curr_state = DS_REMOVE;
+                       /* we cannot properly monitor without all raid_disks */
+                       new->container = NULL;
+                       break;
                }
                sprintf(newd->sys_name, "rd%d", i);
                newd->next = new->info.devs;
                new->info.devs = newd;
        }
+
        new->action_fd = sysfs_open(new->devnum, NULL, "sync_action");
        new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state");
        new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start");
-       new->resync_start = 0;
+       get_resync_start(new);
        dprintf("%s: inst: %d action: %d state: %d\n", __func__, atoi(inst),
                new->action_fd, new->info.state_fd);
 
        sysfs_free(mdi);
-       // finds and compares.
-       if (container->ss->open_new(container, new, inst) < 0) {
-               // FIXME close all those files
+
+       /* if everything checks out tell the metadata handler we want to
+        * manage this instance
+        */
+       if (!aa_ready(new) || container->ss->open_new(container, new, inst) < 0) {
+               fprintf(stderr, "mdmon: failed to monitor %s\n",
+                       mdstat->metadata_version);
                new->container = NULL;
+               free_aa(new);
+       } else
                replace_array(container, victim, new);
-               return;
-       }
-       replace_array(container, victim, new);
-       return;
 }
 
 void manage(struct mdstat_ent *mdstat, struct supertype *container)
@@ -430,32 +455,39 @@ void manage(struct mdstat_ent *mdstat, struct supertype *container)
        }
 }
 
-static int handle_message(struct supertype *container, struct md_message *msg)
+static void handle_message(struct supertype *container, struct metadata_update *msg)
 {
-       int err;
-       struct md_generic_cmd *cmd = msg->buf;
+       /* queue this metadata update through to the monitor */
 
-       if (!cmd)
-               return 0;
+       struct metadata_update *mu;
 
-       switch (cmd->action) {
-       case md_action_remove_device:
-
-               /* forward to the monitor */
-               active_cmd = cmd;
-               write(container->mgr_pipe[1], &err, 1);
-               read(container->mon_pipe[0], &err, 1);
-               return err;
-
-       default:
-               return -1;
+       if (msg->len == 0) {
+               int cnt = monitor_loop_cnt;
+               if (cnt & 1)
+                       cnt += 2; /* wait until next pselect */
+               else
+                       cnt += 3; /* wait for 2 pselects */
+               wakeup_monitor();
+               wait_update_handled();
+               while (monitor_loop_cnt - cnt < 0)
+                       usleep(10 * 1000);
+       } else {
+               mu = malloc(sizeof(*mu));
+               mu->len = msg->len;
+               mu->buf = msg->buf;
+               msg->buf = NULL;
+               mu->space = NULL;
+               mu->next = NULL;
+               if (container->ss->prepare_update)
+                       container->ss->prepare_update(container, mu);
+               queue_metadata_update(mu);
        }
 }
 
 void read_sock(struct supertype *container)
 {
        int fd;
-       struct md_message msg;
+       struct metadata_update msg;
        int terminate = 0;
        long fl;
        int tmo = 3; /* 3 second timeout before hanging up the socket */
@@ -469,49 +501,32 @@ void read_sock(struct supertype *container)
        fcntl(fd, F_SETFL, fl);
 
        do {
-               int err;
-
                msg.buf = NULL;
 
                /* read and validate the message */
                if (receive_message(fd, &msg, tmo) == 0) {
-                       err = handle_message(container, &msg);
-                       if (!err)
-                               ack(fd, msg.seq, tmo);
-                       else
-                               nack(fd, err, tmo);
-               } else {
+                       handle_message(container, &msg);
+                       if (ack(fd, tmo) < 0)
+                               terminate = 1;
+               } else
                        terminate = 1;
-                       nack(fd, -1, tmo);
-               }
 
-               if (msg.buf)
-                       free(msg.buf);
        } while (!terminate);
 
        close(fd);
 }
 
-static int woke = 0;
-void wake_me(int sig)
-{
-       woke = 1;
-}
-
 int exit_now = 0;
 int manager_ready = 0;
 void do_manager(struct supertype *container)
 {
        struct mdstat_ent *mdstat;
-       sigset_t block, orig;
+       sigset_t set;
 
-       sigemptyset(&block);
-       sigaddset(&block, SIGUSR1);
-
-       signal(SIGUSR1, wake_me);
+       sigprocmask(SIG_UNBLOCK, NULL, &set);
+       sigdelset(&set, SIGUSR1);
 
        do {
-               woke = 0;
 
                if (exit_now)
                        exit(0);
@@ -529,9 +544,7 @@ void do_manager(struct supertype *container)
                check_update_queue(container);
 
                manager_ready = 1;
-               sigprocmask(SIG_SETMASK, &block, &orig);
-               if (woke == 0)
-                       mdstat_wait_fd(container->sock, &orig);
-               sigprocmask(SIG_SETMASK, &orig, NULL);
+
+               mdstat_wait_fd(container->sock, &set);
        } while(1);
 }