]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Monitor.c
util: Introduce md_get_disk_info()
[thirdparty/mdadm.git] / Monitor.c
index 5cb24fab8f2a4b83baa31ab2be05f9cb0b737a62..2c0f717ce505c4572f21b9ca4d709cf9efba14e0 100644 (file)
--- a/Monitor.c
+++ b/Monitor.c
@@ -33,7 +33,7 @@
 struct state {
        char *devname;
        char devnm[32]; /* to sync with mdstat info */
-       long utime;
+       unsigned int utime;
        int err;
        char *spare_group;
        int active, working, failed, spare, raid;
@@ -213,6 +213,8 @@ int Monitor(struct mddev_dev *devlist,
                if (mdstat)
                        free_mdstat(mdstat);
                mdstat = mdstat_read(oneshot?0:1, 0);
+               if (!mdstat)
+                       mdstat_close();
 
                for (st=statelist; st; st=st->next)
                        if (check_array(st, mdstat, c->test, &info,
@@ -310,15 +312,11 @@ static int check_one_sharer(int scan)
                rv = stat(dir, &buf);
                if (rv != -1) {
                        if (scan) {
-                               pr_err("Only one "
-                                       "autorebuild process allowed"
-                                       " in scan mode, aborting\n");
+                               pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
                                fclose(fp);
                                return 1;
                        } else {
-                               pr_err("Warning: One"
-                                       " autorebuild process already"
-                                       " running.\n");
+                               pr_err("Warning: One autorebuild process already running.\n");
                        }
                }
                fclose(fp);
@@ -326,14 +324,11 @@ static int check_one_sharer(int scan)
        if (scan) {
                if (mkdir(MDMON_DIR, S_IRWXU) < 0 &&
                    errno != EEXIST) {
-                       pr_err("Can't create "
-                               "autorebuild.pid file\n");
+                       pr_err("Can't create autorebuild.pid file\n");
                } else {
                        fp = fopen(path, "w");
                        if (!fp)
-                               pr_err("Cannot create"
-                                       " autorebuild.pid"
-                                       "file\n");
+                               pr_err("Cannot create autorebuild.pidfile\n");
                        else {
                                pid = getpid();
                                fprintf(fp, "%d\n", pid);
@@ -381,24 +376,21 @@ static void alert(char *event, char *dev, char *disc, struct alert_info *info)
                        if (info->mailfrom)
                                fprintf(mp, "From: %s\n", info->mailfrom);
                        else
-                               fprintf(mp, "From: " Name " monitoring <root>\n");
+                               fprintf(mp, "From: %s monitoring <root>\n", Name);
                        fprintf(mp, "To: %s\n", info->mailaddr);
                        fprintf(mp, "Subject: %s event on %s:%s\n\n",
                                event, dev, hname);
 
                        fprintf(mp,
-                               "This is an automatically generated"
-                               " mail message from " Name "\n");
+                               "This is an automatically generated mail message from %s\n", Name);
                        fprintf(mp, "running on %s\n\n", hname);
 
                        fprintf(mp,
-                               "A %s event had been detected on"
-                               " md device %s.\n\n", event, dev);
+                               "A %s event had been detected on md device %s.\n\n", event, dev);
 
                        if (disc && disc[0] != ' ')
                                fprintf(mp,
-                                       "It could be related to"
-                                       " component device %s.\n\n", disc);
+                                       "It could be related to component device %s.\n\n", disc);
                        if (disc && disc[0] == ' ')
                                fprintf(mp, "Extra information:%s.\n\n", disc);
 
@@ -409,8 +401,7 @@ static void alert(char *event, char *dev, char *disc, struct alert_info *info)
                                char buf[8192];
                                int n;
                                fprintf(mp,
-                                       "\nP.S. The /proc/mdstat file"
-                                       " currently contains the following:\n\n");
+                                       "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
                                while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
                                        n=fwrite(buf, 1, n, mp);
                                fclose(mdstat);
@@ -437,10 +428,13 @@ static void alert(char *event, char *dev, char *disc, struct alert_info *info)
                else
                        priority = LOG_INFO;
 
-               if (disc)
+               if (disc && disc[0] != ' ')
                        syslog(priority,
-                              "%s event detected on md device %s,"
-                              " component device %s", event, dev, disc);
+                              "%s event detected on md device %s, component device %s", event, dev, disc);
+               else if (disc)
+                       syslog(priority,
+                              "%s event detected on md device %s: %s",
+                              event, dev, disc);
                else
                        syslog(priority,
                               "%s event detected on md device %s",
@@ -460,7 +454,7 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
        mdu_array_info_t array;
        struct mdstat_ent *mse = NULL, *mse2;
        char *dev = st->devname;
-       int fd;
+       int fd = -1;
        int i;
        int remaining_disks;
        int last_disk;
@@ -468,6 +462,33 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
 
        if (test)
                alert("TestMessage", dev, NULL, ainfo);
+       if (st->devnm[0])
+               fd = open("/sys/block", O_RDONLY|O_DIRECTORY);
+       if (fd >= 0) {
+               /* Don't open the device unless it is present and
+                * active in sysfs.
+                */
+               char buf[10];
+               close(fd);
+               fd = sysfs_open(st->devnm, NULL, "array_state");
+               if (fd < 0 ||
+                   read(fd, buf, 10) < 5 ||
+                   strncmp(buf,"clear",5) == 0 ||
+                   strncmp(buf,"inact",5) == 0) {
+                       if (fd >= 0)
+                               close(fd);
+                       fd = sysfs_open(st->devnm, NULL, "level");
+                       if (fd < 0 || read(fd, buf, 10) != 0) {
+                               if (fd >= 0)
+                                       close(fd);
+                               if (!st->err)
+                                       alert("DeviceDisappeared", dev, NULL, ainfo);
+                               st->err++;
+                               return 0;
+                       }
+               }
+               close(fd);
+       }
        fd = open(dev, O_RDONLY);
        if (fd < 0) {
                if (!st->err)
@@ -476,7 +497,7 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
                return 0;
        }
        fcntl(fd, F_SETFD, FD_CLOEXEC);
-       if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
+       if (md_get_array_info(fd, &array) < 0) {
                if (!st->err)
                        alert("DeviceDisappeared", dev, NULL, ainfo);
                st->err++;
@@ -488,7 +509,7 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
         */
        if (array.level == 0 || array.level == -1) {
                if (!st->err && !st->from_config)
-                       alert("DeviceDisappeared", dev, "Wrong-Level", ainfo);
+                       alert("DeviceDisappeared", dev, " Wrong-Level", ainfo);
                st->err++;
                close(fd);
                return 0;
@@ -578,7 +599,7 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
                } else
                        alert("RebuildFinished", dev, NULL, ainfo);
                if (sra)
-                       free(sra);
+                       sysfs_free(sra);
        }
        st->percent = mse->percent;
 
@@ -587,7 +608,7 @@ static int check_array(struct state *st, struct mdstat_ent *mdstat,
             i++) {
                mdu_disk_info_t disc;
                disc.number = i;
-               if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
+               if (md_get_disk_info(fd, &disc) >= 0) {
                        info[i].state = disc.state;
                        info[i].major = disc.major;
                        info[i].minor = disc.minor;
@@ -668,6 +689,7 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
 {
        struct mdstat_ent *mse;
        int new_found = 0;
+       char *name;
 
        for (mse=mdstat; mse; mse=mse->next)
                if (mse->devnm[0] &&
@@ -678,11 +700,19 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
                        struct state *st = xcalloc(1, sizeof *st);
                        mdu_array_info_t array;
                        int fd;
-                       st->devname = xstrdup(get_md_name(mse->devnm));
+
+                       name = get_md_name(mse->devnm);
+                       if (!name) {
+                               free(st);
+                               continue;
+                       }
+
+                       st->devname = xstrdup(name);
                        if ((fd = open(st->devname, O_RDONLY)) < 0 ||
-                           ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
+                           md_get_array_info(fd, &array) < 0) {
                                /* no such array */
-                               if (fd >=0) close(fd);
+                               if (fd >= 0)
+                                       close(fd);
                                put_md_name(st->devname);
                                free(st->devname);
                                if (st->metadata) {
@@ -965,14 +995,21 @@ int Wait(char *dev)
 {
        struct stat stb;
        char devnm[32];
+       char *tmp;
        int rv = 1;
+       int frozen_remaining = 3;
 
        if (stat(dev, &stb) != 0) {
                pr_err("Cannot find %s: %s\n", dev,
                        strerror(errno));
                return 2;
        }
-       strcpy(devnm, stat2devnm(&stb));
+       tmp = stat2devnm(&stb);
+       if (!tmp) {
+               pr_err("%s is not a block device.\n", dev);
+               return 2;
+       }
+       strcpy(devnm, tmp);
 
        while(1) {
                struct mdstat_ent *ms = mdstat_read(1, 0);
@@ -982,7 +1019,7 @@ int Wait(char *dev)
                        if (strcmp(e->devnm, devnm) == 0)
                                break;
 
-               if (e->percent == RESYNC_NONE) {
+               if (e && e->percent == RESYNC_NONE) {
                        /* We could be in the brief pause before something
                         * starts. /proc/mdstat doesn't show that, but
                         * sync_action does.
@@ -992,8 +1029,15 @@ int Wait(char *dev)
                        sysfs_init(&mdi, -1, devnm);
                        if (sysfs_get_str(&mdi, NULL, "sync_action",
                                          buf, 20) > 0 &&
-                           strcmp(buf,"idle\n") != 0)
+                           strcmp(buf,"idle\n") != 0) {
                                e->percent = RESYNC_UNKNOWN;
+                               if (strcmp(buf, "frozen\n") == 0) {
+                                       if (frozen_remaining == 0)
+                                               e->percent = RESYNC_NONE;
+                                       else
+                                               frozen_remaining -= 1;
+                               }
+                       }
                }
                if (!e || e->percent == RESYNC_NONE) {
                        if (e && e->metadata_version &&
@@ -1035,8 +1079,7 @@ int WaitClean(char *dev, int sock, int verbose)
        mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
        if (!mdi) {
                if (verbose)
-                       pr_err("Failed to read sysfs attributes for "
-                              "%s\n", dev);
+                       pr_err("Failed to read sysfs attributes for %s\n", dev);
                close(fd);
                return 0;
        }