]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - bitmap.c
mdmon: fix wrong array state when disk fails during mdmon startup
[thirdparty/mdadm.git] / bitmap.c
index 5ad7401bed0735d2763e697dc508396cba303c03..e38cb9650390235d481aeba3b3fa38b724e37910 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,54 +179,51 @@ 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;
        struct stat stb;
        struct supertype *st = *stp;
 
-       if (stat(filename, &stb) < 0) {
-               pr_err("failed to find file %s: %s\n",
-                       filename, strerror(errno));
+       fd = open(filename, O_RDONLY|O_DIRECT);
+       if (fd < 0) {
+               pr_err("failed to open bitmap file %s: %s\n",
+                      filename, strerror(errno));
                return -1;
        }
-       if ((S_IFMT & stb.st_mode) == S_IFBLK) {
-               fd = open(filename, O_RDONLY|O_DIRECT);
-               if (fd < 0) {
-                       pr_err("failed to open bitmap file %s: %s\n",
-                               filename, strerror(errno));
-                       return -1;
-               }
+
+       if (fstat(fd, &stb) < 0) {
+               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) st = guess_super(fd);
+               if (!st)
+                       st = guess_super(fd);
                if (!st) {
                        /* just look at device... */
                        lseek(fd, 0, 0);
                } else if (!st->ss->locate_bitmap) {
                        pr_err("No bitmap possible with %s metadata\n",
                                st->ss->name);
+                       close(fd);
                        return -1;
                } else {
                        if (st->ss->locate_bitmap(st, fd, node_num)) {
                                pr_err("%s doesn't have bitmap\n", filename);
+                               close(fd);
                                fd = -1;
                        }
                }
-
                *stp = st;
-       } else {
-               fd = open(filename, O_RDONLY|O_DIRECT);
-               if (fd < 0) {
-                       pr_err("failed to open bitmap file %s: %s\n",
-                               filename, strerror(errno));
-                       return -1;
-               }
        }
 
        return fd;
 }
 
-__u32 swapl(__u32 l)
+static __u32 swapl(__u32 l)
 {
        char *c = (char*)&l;
        char t= c[0];
@@ -275,7 +257,7 @@ 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");
@@ -351,7 +333,17 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
                        st = NULL;
                        free(info);
                        fd = bitmap_file_open(filename, &st, i);
+                       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;
+                       }
                        sb = &info->sb;
                        if (sb->magic != BITMAP_MAGIC)
                                pr_err("invalid bitmap magic 0x%x, the bitmap file appears to be corrupted\n", sb->magic);