]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Show all bitmaps while examining bitmap
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 37c6e0d3bdef73021bd228ff398a8eae33a2acd8..9ec4aefde8c1fb21ccf4d5ec8fe272cbefaf0a72 100644 (file)
--- a/util.c
+++ b/util.c
 #include       <ctype.h>
 #include       <dirent.h>
 #include       <signal.h>
+#include       <dlfcn.h>
+#include       <stdint.h>
+#ifdef NO_COROSYNC
+ typedef uint64_t cmap_handle_t;
+ #define CS_OK 1
+#else
+ #include      <corosync/cmap.h>
+#endif
+
 
 /*
  * following taken from linux/blkpg.h because they aren't
@@ -368,6 +377,13 @@ int enough(int level, int raid_disks, int layout, int clean, char *avail)
        case 1:
                return avail_disks >= 1;
        case 4:
+               if (avail_disks == raid_disks - 1 &&
+                   !avail[raid_disks - 1])
+                       /* If just the parity device is missing, then we
+                        * have enough, even if not clean
+                        */
+                       return 1;
+               /* FALL THROUGH */
        case 5:
                if (clean)
                        return avail_disks >= raid_disks-1;
@@ -671,13 +687,13 @@ char *human_size(long long bytes)
        if (bytes < 5000*1024)
                buf[0] = 0;
        else if (bytes < 2*1024LL*1024LL*1024LL) {
-               long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
+               long cMiB = (bytes * 200LL / (1LL<<20) + 1) / 2;
                long cMB  = (bytes / ( 1000000LL / 200LL ) +1) /2;
                snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)",
                        cMiB/100 , cMiB % 100,
                        cMB/100, cMB % 100);
        } else {
-               long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
+               long cGiB = (bytes * 200LL / (1LL<<30) +1) / 2;
                long cGB  = (bytes / (1000000000LL/200LL ) +1) /2;
                snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)",
                        cGiB/100 , cGiB % 100,
@@ -706,11 +722,11 @@ char *human_size_brief(long long bytes, int prefix)
                buf[0] = 0;
        else if (prefix == IEC) {
                if (bytes < 2*1024LL*1024LL*1024LL) {
-                       long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2;
+                       long cMiB = (bytes * 200LL / (1LL<<20) +1) /2;
                        snprintf(buf, sizeof(buf), "%ld.%02ldMiB",
                                cMiB/100 , cMiB % 100);
                } else {
-                       long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2;
+                       long cGiB = (bytes * 200LL / (1LL<<30) +1) /2;
                        snprintf(buf, sizeof(buf), "%ld.%02ldGiB",
                                        cGiB/100 , cGiB % 100);
                }
@@ -990,7 +1006,7 @@ void wait_for(char *dev, int fd)
                        delay *= 2;
        }
        if (i == 25)
-               dprintf("%s: timeout waiting for %s\n", __func__, dev);
+               dprintf("timeout waiting for %s\n", dev);
 }
 
 struct superswitch *superlist[] =
@@ -1740,8 +1756,7 @@ int start_mdmon(char *devnm)
                        status = execl("/bin/systemctl", "systemctl", "start",
                                       pathbuf, NULL);
                        exit(1);
-               case -1: pr_err("cannot run mdmon. "
-                               "Array remains readonly\n");
+               case -1: pr_err("cannot run mdmon. Array remains readonly\n");
                        return -1;
                default: /* parent - good */
                        pid = wait(&status);
@@ -1766,14 +1781,12 @@ int start_mdmon(char *devnm)
                                      devnm, NULL);
                        }
                exit(1);
-       case -1: pr_err("cannot run mdmon. "
-                        "Array remains readonly\n");
+       case -1: pr_err("cannot run mdmon. Array remains readonly\n");
                return -1;
        default: /* parent - good */
                pid = wait(&status);
                if (pid < 0 || status != 0) {
-                       pr_err("failed to launch mdmon. "
-                              "Array remains readonly\n");
+                       pr_err("failed to launch mdmon. Array remains readonly\n");
                        return -1;
                }
        }
@@ -1845,8 +1858,7 @@ int experimental(void)
        if (check_env("MDADM_EXPERIMENTAL"))
                return 1;
        else {
-               pr_err("To use this feature MDADM_EXPERIMENTAL"
-                               " environment variable has to be defined.\n");
+               pr_err("To use this feature MDADM_EXPERIMENTAL environment variable has to be defined.\n");
                return 0;
        }
 }
@@ -1973,3 +1985,51 @@ void reopen_mddev(int mdfd)
        if (fd >= 0 && fd != mdfd)
                dup2(fd, mdfd);
 }
+
+int get_cluster_name(char **cluster_name)
+{
+        void *lib_handle = NULL;
+        int rv = -1;
+
+        cmap_handle_t handle;
+        static int (*initialize)(cmap_handle_t *handle);
+        static int (*get_string)(cmap_handle_t handle,
+                                const char *string,
+                                char **name);
+        static int (*finalize)(cmap_handle_t handle);
+
+
+        lib_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL);
+        if (!lib_handle)
+                return rv;
+
+        initialize = dlsym(lib_handle, "cmap_initialize");
+        if (!initialize)
+                goto out;
+
+        get_string = dlsym(lib_handle, "cmap_get_string");
+        if (!get_string)
+                goto out;
+
+        finalize = dlsym(lib_handle, "cmap_finalize");
+        if (!finalize)
+                goto out;
+
+        rv = initialize(&handle);
+        if (rv != CS_OK)
+                goto out;
+
+        rv = get_string(handle, "totem.cluster_name", cluster_name);
+        if (rv != CS_OK) {
+                free(*cluster_name);
+                rv = -1;
+                goto name_err;
+        }
+
+        rv = 0;
+name_err:
+        finalize(handle);
+out:
+        dlclose(lib_handle);
+        return rv;
+}