]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - bitmap.c
mdadm/Detail: show correct state for clustered array
[thirdparty/mdadm.git] / bitmap.c
index e9dee738632dac71b1d5e36a6007208f26224c4c..9a7ffe3bb1bf209d2246ef539800272444ec63ce 100644 (file)
--- a/bitmap.c
+++ b/bitmap.c
@@ -47,13 +47,13 @@ mapping_t bitmap_states[] = {
        { NULL, -1 }
 };
 
-const char *bitmap_state(int state_num)
+static const char *bitmap_state(int state_num)
 {
        char *state = map_num(bitmap_states, state_num);
        return state ? state : "Unknown";
 }
 
-const char *human_chunksize(unsigned long bytes)
+static const char *human_chunksize(unsigned long bytes)
 {
        static char buf[16];
        char *suffixes[] = { "B", "KB", "MB", "GB", "TB", NULL };
@@ -95,7 +95,7 @@ static inline int count_dirty_bits_byte(char byte, int num_bits)
        return num;
 }
 
-int count_dirty_bits(char *buf, int num_bits)
+static int count_dirty_bits(char *buf, int num_bits)
 {
        int i, num = 0;
 
@@ -108,22 +108,7 @@ int count_dirty_bits(char *buf, int num_bits)
        return num;
 }
 
-/* calculate the size of the bitmap given the array size and bitmap chunksize */
-unsigned long long bitmap_bits(unsigned long long array_size,
-                               unsigned long chunksize)
-{
-       return (array_size * 512 + chunksize - 1) / chunksize;
-}
-
-unsigned long bitmap_sectors(struct bitmap_super_s *bsb)
-{
-       unsigned long long bits = bitmap_bits(__le64_to_cpu(bsb->sync_size),
-                                             __le32_to_cpu(bsb->chunksize));
-       int bits_per_sector = 8*512;
-       return (bits + bits_per_sector - 1) / bits_per_sector;
-}
-
-bitmap_info_t *bitmap_fd_read(int fd, int brief)
+static bitmap_info_t *bitmap_fd_read(int fd, int brief)
 {
        /* Note: fd might be open O_DIRECT, so we must be
         * careful to align reads properly
@@ -194,13 +179,15 @@ out:
        return info;
 }
 
-int bitmap_file_open(char *filename, struct supertype **stp, int node_num)
+static int
+bitmap_file_open(char *filename, struct supertype **stp, int node_num, int fd)
 {
-       int fd;
        struct stat stb;
        struct supertype *st = *stp;
 
-       fd = open(filename, O_RDONLY|O_DIRECT);
+       /* won't re-open filename when (fd >= 0) */
+       if (fd < 0)
+               fd = open(filename, O_RDONLY|O_DIRECT);
        if (fd < 0) {
                pr_err("failed to open bitmap file %s: %s\n",
                       filename, strerror(errno));
@@ -208,12 +195,10 @@ int bitmap_file_open(char *filename, struct supertype **stp, int node_num)
        }
 
        if (fstat(fd, &stb) < 0) {
-               pr_err("failed to determine bitmap file/device type: %s\n",
-                       strerror(errno));
+               pr_err("fstat failed for %s: %s\n", filename, strerror(errno));
                close(fd);
                return -1;
        }
-
        if ((stb.st_mode & S_IFMT) == S_IFBLK) {
                /* block device, so we are probably after an internal bitmap */
                if (!st)
@@ -233,14 +218,13 @@ int bitmap_file_open(char *filename, struct supertype **stp, int node_num)
                                fd = -1;
                        }
                }
-
                *stp = st;
        }
 
        return fd;
 }
 
-__u32 swapl(__u32 l)
+static __u32 swapl(__u32 l)
 {
        char *c = (char*)&l;
        char t= c[0];
@@ -266,7 +250,7 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
        int fd, i;
        __u32 uuid32[4];
 
-       fd = bitmap_file_open(filename, &st, 0);
+       fd = bitmap_file_open(filename, &st, 0, -1);
        if (fd < 0)
                return rv;
 
@@ -274,13 +258,12 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
        if (!info)
                return rv;
        sb = &info->sb;
-       if (sb->magic != BITMAP_MAGIC && md_get_version(fd) > 0) {
+       if (sb->magic != BITMAP_MAGIC) {
                pr_err("This is an md array.  To view a bitmap you need to examine\n");
                pr_err("a member device, not the array.\n");
                pr_err("Reporting bitmap that would be used if this array were used\n");
                pr_err("as a member of some other array\n");
        }
-       close(fd);
        printf("        Filename : %s\n", filename);
        printf("           Magic : %08x\n", sb->magic);
        if (sb->magic != BITMAP_MAGIC) {
@@ -349,15 +332,13 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
                for (i = 0; i < (int)sb->nodes; i++) {
                        st = NULL;
                        free(info);
-                       fd = bitmap_file_open(filename, &st, i);
+                       fd = bitmap_file_open(filename, &st, i, fd);
                        if (fd < 0) {
                                printf("   Unable to open bitmap file on node: %i\n", i);
-
                                continue;
                        }
                        info = bitmap_fd_read(fd, brief);
                        if (!info) {
-                               close(fd);
                                printf("   Unable to read bitmap on node: %i\n", i);
                                continue;
                        }
@@ -376,15 +357,74 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
                        printf("          Bitmap : %llu bits (chunks), %llu dirty (%2.1f%%)\n",
                               info->total_bits, info->dirty_bits,
                               100.0 * info->dirty_bits / (info->total_bits?:1));
-                        close(fd);
                }
        }
 
 free_info:
+       close(fd);
        free(info);
        return rv;
 }
 
+int IsBitmapDirty(char *filename)
+{
+       /*
+        * Read the bitmap file
+        * It will break reading bitmap action immediately when meeting any error.
+        *
+        * Return: 1(dirty), 0 (clean), -1(error)
+        */
+
+       int fd = -1, rv = 0, i;
+       struct supertype *st = NULL;
+       bitmap_info_t *info = NULL;
+       bitmap_super_t *sb = NULL;
+
+       fd = bitmap_file_open(filename, &st, 0, fd);
+       free(st);
+       if (fd < 0)
+               goto out;
+
+       info = bitmap_fd_read(fd, 0);
+       if (!info) {
+               close(fd);
+               goto out;
+       }
+
+       sb = &info->sb;
+       for (i = 0; i < (int)sb->nodes; i++) {
+               st = NULL;
+               free(info);
+               info = NULL;
+
+               fd = bitmap_file_open(filename, &st, i, fd);
+               free(st);
+               if (fd < 0)
+                       goto out;
+
+               info = bitmap_fd_read(fd, 0);
+               if (!info) {
+                       close(fd);
+                       goto out;
+               }
+
+               sb = &info->sb;
+               if (sb->magic != BITMAP_MAGIC) { /* invalid bitmap magic */
+                       free(info);
+                       close(fd);
+                       goto out;
+               }
+
+               if (info->dirty_bits)
+                       rv = 1;
+       }
+       close(fd);
+       free(info);
+       return rv;
+out:
+       return -1;
+}
+
 int CreateBitmap(char *filename, int force, char uuid[16],
                 unsigned long chunksize, unsigned long daemon_sleep,
                 unsigned long write_behind,