]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Monitor.c
Various compile fixes.
[thirdparty/mdadm.git] / Monitor.c
index 18462f2f597e0ba82789660105cbc6ca19dee480..d3795b1713d8e07226c4f83666ce01459e7452e9 100644 (file)
--- a/Monitor.c
+++ b/Monitor.c
@@ -291,7 +291,8 @@ static int check_one_sharer(int scan)
        struct stat buf;
        fp = fopen("/var/run/mdadm/autorebuild.pid", "r");
        if (fp) {
-               fscanf(fp, "%d", &pid);
+               if (fscanf(fp, "%d", &pid) != 1)
+                       pid = -1;
                sprintf(dir, "/proc/%d", pid);
                rv = stat(dir, &buf);
                if (rv != -1) {
@@ -702,24 +703,30 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
        return new_found;
 }
 
-unsigned long long min_spare_size_required(struct state *st)
+static int get_min_spare_size_required(struct state *st, unsigned long long *sizep)
 {
        int fd;
-       unsigned long long rv = 0;
 
        if (!st->metadata ||
-           !st->metadata->ss->min_acceptable_spare_size)
-               return rv;
+           !st->metadata->ss->min_acceptable_spare_size) {
+               *sizep = 0;
+               return 0;
+       }
 
        fd = open(st->devname, O_RDONLY);
        if (fd < 0)
-               return 0;
-       st->metadata->ss->load_super(st->metadata, fd, st->devname);
+               return 1;
+       if (st->metadata->ss->external)
+               st->metadata->ss->load_container(st->metadata, fd, st->devname);
+       else
+               st->metadata->ss->load_super(st->metadata, fd, st->devname);
        close(fd);
-       rv = st->metadata->ss->min_acceptable_spare_size(st->metadata);
+       if (!st->metadata->sb)
+               return 1;
+       *sizep = st->metadata->ss->min_acceptable_spare_size(st->metadata);
        st->metadata->ss->free_super(st->metadata);
 
-       return rv;
+       return 0;
 }
 
 static int check_donor(struct state *from, struct state *to)
@@ -759,6 +766,10 @@ static dev_t choose_spare(struct state *from, struct state *to,
                        struct dev_policy *pol;
                        unsigned long long dev_size;
 
+                       if (to->metadata->ss->external &&
+                           test_partition_from_id(from->devid[d]))
+                               continue;
+
                        if (min_size &&
                            dev_size_from_id(from->devid[d], &dev_size) &&
                            dev_size < min_size)
@@ -768,7 +779,7 @@ static dev_t choose_spare(struct state *from, struct state *to,
                        if (from->spare_group)
                                pol_add(&pol, pol_domain,
                                        from->spare_group, NULL);
-                       if (domain_test(domlist, pol, to->metadata->ss->name))
+                       if (domain_test(domlist, pol, to->metadata->ss->name) == 1)
                            dev = from->devid[d];
                        dev_policy_free(pol);
                }
@@ -778,7 +789,7 @@ static dev_t choose_spare(struct state *from, struct state *to,
 
 static dev_t container_choose_spare(struct state *from, struct state *to,
                                    struct domainlist *domlist,
-                                   unsigned long long min_size)
+                                   unsigned long long min_size, int active)
 {
        /* This is similar to choose_spare, but we cannot trust devstate,
         * so we need to read the metadata instead
@@ -801,6 +812,33 @@ static dev_t container_choose_spare(struct state *from, struct state *to,
        if (err)
                return 0;
        
+       if (from == to) {
+               /* We must check if number of active disks has not increased
+                * since ioctl in main loop. mdmon may have added spare
+                * to subarray. If so we do not need to look for more spares
+                * so return non zero value */
+               int active_cnt = 0;
+               struct mdinfo *dp;
+               list = st->ss->getinfo_super_disks(st);
+               if (!list) {
+                       st->ss->free_super(st);
+                       return 1;
+               }
+               dp = list->devs;
+               while (dp) {
+                       if (dp->disk.state & (1<<MD_DISK_SYNC) &&
+                           !(dp->disk.state & (1<<MD_DISK_FAULTY)))
+                               active_cnt++;
+                       dp = dp->next;
+               }
+               sysfs_free(list);
+               if (active < active_cnt) {
+                       /* Spare just activated.*/
+                       st->ss->free_super(st);
+                       return 1;
+               }
+       }
+
        /* We only need one spare so full list not needed */
        list = container_choose_spares(st, min_size, domlist, from->spare_group,
                                       to->metadata->ss->name, 1);
@@ -829,17 +867,23 @@ static void try_spare_migration(struct state *statelist, struct alert_info *info
                        struct state *to = st;
                        unsigned long long min_size;
 
+                       if (to->parent_dev != NoMdDev && !to->parent)
+                               /* subarray monitored without parent container
+                                * we can't move spares here */
+                               continue;
+                       
                        if (to->parent)
                                /* member of a container */
                                to = to->parent;
 
-                       min_size = min_spare_size_required(to);
+                       if (get_min_spare_size_required(to, &min_size))
+                               continue;
                        if (to->metadata->ss->external) {
                                /* We must make sure there is
                                 * no suitable spare in container already.
                                 * If there is we don't add more */
                                dev_t devid = container_choose_spare(
-                                       to, to, NULL, min_size);
+                                       to, to, NULL, min_size, st->active);
                                if (devid > 0)
                                        continue;
                        }
@@ -862,7 +906,7 @@ static void try_spare_migration(struct state *statelist, struct alert_info *info
                                        continue;
                                if (from->metadata->ss->external)
                                        devid = container_choose_spare(
-                                               from, to, domlist, min_size);
+                                               from, to, domlist, min_size, 0);
                                else
                                        devid = choose_spare(from, to, domlist,
                                                             min_size);