Skip to contentSkip to conversation

mdadm/bitmap: locate bitmap calcuate bitmap position wrongly

PATCH Applied Xiao Ni 3 participants 7 messages Oct 28, 2020 – Feb 26, 2021 MD RAID

lore
Share & tools
lore

Xiao Ni

author
#0 lore raw

PATCH 1/1 mdadm/bitmap: locate bitmap calcuate bitmap position wrongly

Now it only adds bitmap offset based on cluster nodes. It's not right. It needs to add per node bitmap space to find next node bitmap position.

---
 super1.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
super1.c +9 -3
index 8b0d6ff..b5b379b 100644
@@ -2582,8 +2582,9 @@ add_internal_bitmap1(struct supertype *st,
 
 static int locate_bitmap1(struct supertype *st, int fd, int node_num)
 {
-	unsigned long long offset;
+	unsigned long long offset, bm_sectors_per_node;
 	struct mdp_superblock_1 *sb;
+	bitmap_super_t *bms;
 	int mustfree = 0;
 	int ret;
 
@@ -2598,8 +2599,13 @@ static int locate_bitmap1(struct supertype *st, int fd, int node_num)
 		ret = 0;
 	else
 		ret = -1;
-	offset = __le64_to_cpu(sb->super_offset);
-	offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
+
+	offset = __le64_to_cpu(sb->super_offset) + __le32_to_cpu(sb->bitmap_offset);
+	if (node_num) {
+		bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
+		bm_sectors_per_node = calc_bitmap_size(bms, 4096) >> 9;
+		offset += bm_sectors_per_node * node_num;
+	}
 	if (mustfree)
 		free(sb);
 	lseek64(fd, offset<<9, 0);
Signature · 1 line
2.7.5
#1 lore raw

Hello Xiao,

My review comment:
You code only work in modern system. the boundary is 4k not 512, because using hardcode 4k to call calc_bitmap_size

In current cluster env, if bitmap area beyond 4K size (or 512 in very old system), locate_bitmap1 will return wrong address.

Please refer write_bitmap1() to saparate 512 & 4096 case.

Quoted 40 lines · Xiao Ni

Now it only adds bitmap offset based on cluster nodes. It's not right. It needs to add per node bitmap space to find next node bitmap position.

---
  super1.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
super1.c +9 -3
index 8b0d6ff..b5b379b 100644
@@ -2582,8 +2582,9 @@ add_internal_bitmap1(struct supertype *st,
 
  static int locate_bitmap1(struct supertype *st, int fd, int node_num)
  {
-	unsigned long long offset;
+	unsigned long long offset, bm_sectors_per_node;
  	struct mdp_superblock_1 *sb;
+	bitmap_super_t *bms;
  	int mustfree = 0;
  	int ret;
 
@@ -2598,8 +2599,13 @@ static int locate_bitmap1(struct supertype *st, int fd, int node_num)
  		ret = 0;
  	else
  		ret = -1;
-	offset = __le64_to_cpu(sb->super_offset);
-	offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
+
+	offset = __le64_to_cpu(sb->super_offset) + __le32_to_cpu(sb->bitmap_offset);
+	if (node_num) {
+		bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
+		bm_sectors_per_node = calc_bitmap_size(bms, 4096) >> 9;
+		offset += bm_sectors_per_node * node_num;
+	}
  	if (mustfree)
  		free(sb);
  	lseek64(fd, offset<<9, 0);
author
#2 lore raw

Hi Heming

The cluster raid is only supported by super 1.2, so we don't need to consider the old system when it's a cluster raid.

Regards
Xiao

Quoted 51 lines · On 10/28/2020 08:29 PM, [email protected] wrote:

Hello Xiao,

My review comment:
You code only work in modern system. the boundary is 4k not 512, because using hardcode 4k to call calc_bitmap_size

In current cluster env, if bitmap area beyond 4K size (or 512 in very old system), locate_bitmap1 will return wrong address.

Please refer write_bitmap1() to saparate 512 & 4096 case.

Quoted 40 lines · Xiao Ni

Now it only adds bitmap offset based on cluster nodes. It's not right. It needs to add per node bitmap space to find next node bitmap position.

---
   super1.c | 12 +++++++++---
   1 file changed, 9 insertions(+), 3 deletions(-)
super1.c +9 -3
index 8b0d6ff..b5b379b 100644
@@ -2582,8 +2582,9 @@ add_internal_bitmap1(struct supertype *st,
 
   static int locate_bitmap1(struct supertype *st, int fd, int node_num)
   {
-	unsigned long long offset;
+	unsigned long long offset, bm_sectors_per_node;
   	struct mdp_superblock_1 *sb;
+	bitmap_super_t *bms;
   	int mustfree = 0;
   	int ret;
 
@@ -2598,8 +2599,13 @@ static int locate_bitmap1(struct supertype *st, int fd, int node_num)
   		ret = 0;
   	else
   		ret = -1;
-	offset = __le64_to_cpu(sb->super_offset);
-	offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
+
+	offset = __le64_to_cpu(sb->super_offset) + __le32_to_cpu(sb->bitmap_offset);
+	if (node_num) {
+		bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
+		bm_sectors_per_node = calc_bitmap_size(bms, 4096) >> 9;
+		offset += bm_sectors_per_node * node_num;
+	}
   	if (mustfree)
   		free(sb);
   	lseek64(fd, offset<<9, 0);
#3 lore raw

You are right. Only cluster case can trigger your patch code. Hardcode 4096 is correct.

Quoted 59 lines · Xiao Ni

Hi Heming

The cluster raid is only supported by super 1.2, so we don't need to consider the old system when it's a cluster raid.

Regards
Xiao

Quoted 49 lines · On 10/28/2020 08:29 PM, [email protected] wrote:

Hello Xiao,

My review comment:
You code only work in modern system. the boundary is 4k not 512, because using hardcode 4k to call calc_bitmap_size

In current cluster env, if bitmap area beyond 4K size (or 512 in very old system), locate_bitmap1 will return wrong address.

Please refer write_bitmap1() to saparate 512 & 4096 case.

Quoted 38 lines · Xiao Ni

Now it only adds bitmap offset based on cluster nodes. It's not right. It needs to add per node bitmap space to find next node bitmap position.

---
   super1.c | 12 +++++++++---

   1 file changed, 9 insertions(+), 3 deletions(-)
super1.c +2 -1
index 8b0d6ff..b5b379b 100644
@@ -2582,8 +2582,9 @@ add_internal_bitmap1(struct supertype *st,
    static int locate_bitmap1(struct supertype *st, int fd, int node_num)
    {
-    unsigned long long offset;
+    unsigned long long offset, bm_sectors_per_node;
        struct mdp_superblock_1 *sb;
+    bitmap_super_t *bms;
        int mustfree = 0;
        int ret;
@@ -2598,8 +2599,13 @@ static int locate_bitmap1(struct supertype *st, int fd, int node_num)
            ret = 0;

       else
           ret = -1;
-    offset = __le64_to_cpu(sb->super_offset);
-    offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1); +
+    offset = __le64_to_cpu(sb->super_offset) + __le32_to_cpu(sb->bitmap_offset); +    if (node_num) {
+        bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
+        bm_sectors_per_node = calc_bitmap_size(bms, 4096) >> 9;
+        offset += bm_sectors_per_node * node_num;
+    }
       if (mustfree)
           free(sb);
       lseek64(fd, offset<<9, 0);

#4 lore raw
Quoted 2 lines · On 10/30/20 2:44 AM, [email protected] wrote:

You are right. Only cluster case can trigger your patch code. Hardcode 4096 is correct.

Just catching up, was the conclusion the original patch was correct?

Thanks,
Jes

Quoted 61 lines · Xiao Ni
Quoted 59 lines

Hi Heming

The cluster raid is only supported by super 1.2, so we don't need to consider the old system when it's a cluster raid.

Regards
Xiao

Quoted 49 lines · On 10/28/2020 08:29 PM, [email protected] wrote:

Hello Xiao,

My review comment:
You code only work in modern system. the boundary is 4k not 512, because using hardcode 4k to call calc_bitmap_size

In current cluster env, if bitmap area beyond 4K size (or 512 in very old system), locate_bitmap1 will return wrong address.

Please refer write_bitmap1() to saparate 512 & 4096 case.

Quoted 38 lines · Xiao Ni

Now it only adds bitmap offset based on cluster nodes. It's not right. It needs to add per node bitmap space to find next node bitmap position.

---
   super1.c | 12 +++++++++---

   1 file changed, 9 insertions(+), 3 deletions(-)
super1.c +2 -1
index 8b0d6ff..b5b379b 100644
@@ -2582,8 +2582,9 @@ add_internal_bitmap1(struct supertype *st,
    static int locate_bitmap1(struct supertype *st, int fd, int node_num)
    {
-    unsigned long long offset;
+    unsigned long long offset, bm_sectors_per_node;
        struct mdp_superblock_1 *sb;
+    bitmap_super_t *bms;
        int mustfree = 0;
        int ret;
@@ -2598,8 +2599,13 @@ static int locate_bitmap1(struct supertype *st, int fd, int node_num)
            ret = 0;

       else
           ret = -1;
-    offset = __le64_to_cpu(sb->super_offset);
-    offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1); +
+    offset = __le64_to_cpu(sb->super_offset) + __le32_to_cpu(sb->bitmap_offset); +    if (node_num) {
+        bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
+        bm_sectors_per_node = calc_bitmap_size(bms, 4096) >> 9;
+        offset += bm_sectors_per_node * node_num;
+    }
       if (mustfree)
           free(sb);
       lseek64(fd, offset<<9, 0);

#5 lore raw
Quoted 9 lines · Jes Sorensen
Quoted 2 lines · On 10/30/20 2:44 AM, [email protected] wrote:

You are right. Only cluster case can trigger your patch code. Hardcode 4096 is correct.

Just catching up, was the conclusion the original patch was correct?

Thanks,
Jes

Xiao's patch (original patch) is correct.

Thanks.

Quoted 64 lines
Quoted 62 lines · Xiao Ni
Quoted 59 lines

Hi Heming

The cluster raid is only supported by super 1.2, so we don't need to consider the old system when it's a cluster raid.

Regards
Xiao

Quoted 49 lines · On 10/28/2020 08:29 PM, [email protected] wrote:

Hello Xiao,

My review comment:
You code only work in modern system. the boundary is 4k not 512, because using hardcode 4k to call calc_bitmap_size

In current cluster env, if bitmap area beyond 4K size (or 512 in very old system), locate_bitmap1 will return wrong address.

Please refer write_bitmap1() to saparate 512 & 4096 case.

Quoted 38 lines · Xiao Ni

Now it only adds bitmap offset based on cluster nodes. It's not right. It needs to add per node bitmap space to find next node bitmap position.

---
    super1.c | 12 +++++++++---
    1 file changed, 9 insertions(+), 3 deletions(-)
super1.c +9 -3
index 8b0d6ff..b5b379b 100644
@@ -2582,8 +2582,9 @@ add_internal_bitmap1(struct supertype *st,
    static int locate_bitmap1(struct supertype *st, int fd, int node_num)
    {
-    unsigned long long offset;
+    unsigned long long offset, bm_sectors_per_node;
        struct mdp_superblock_1 *sb;
+    bitmap_super_t *bms;
        int mustfree = 0;
        int ret;
@@ -2598,8 +2599,13 @@ static int locate_bitmap1(struct supertype *st, int fd, int node_num)
            ret = 0;
        else
            ret = -1;
-    offset = __le64_to_cpu(sb->super_offset);
-    offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
+
+    offset = __le64_to_cpu(sb->super_offset) + __le32_to_cpu(sb->bitmap_offset);
+    if (node_num) {
+        bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
+        bm_sectors_per_node = calc_bitmap_size(bms, 4096) >> 9;
+        offset += bm_sectors_per_node * node_num;
+    }
        if (mustfree)
            free(sb);
        lseek64(fd, offset<<9, 0);
Applied
#6 lore raw
Quoted 5 lines · On 11/25/20 8:43 PM, [email protected] wrote:
Quoted 4 lines · Jes Sorensen
Quoted 2 lines · On 10/30/20 2:44 AM, [email protected] wrote:

You are right. Only cluster case can trigger your patch code. Hardcode 4096 is correct.

Quoted 8 lines
Quoted 5 lines

Just catching up, was the conclusion the original patch was correct?

Thanks,
Jes

Xiao's patch (original patch) is correct.

Thanks.

Applied!

Thanks,
Jes

Quoted 65 lines
Quoted 64 lines
Quoted 62 lines · Xiao Ni
Quoted 59 lines

Hi Heming

The cluster raid is only supported by super 1.2, so we don't need to consider the old system when it's a cluster raid.

Regards
Xiao

Quoted 49 lines · On 10/28/2020 08:29 PM, [email protected] wrote:

Hello Xiao,

My review comment:
You code only work in modern system. the boundary is 4k not 512, because using hardcode 4k to call calc_bitmap_size

In current cluster env, if bitmap area beyond 4K size (or 512 in very old system), locate_bitmap1 will return wrong address.

Please refer write_bitmap1() to saparate 512 & 4096 case.

Quoted 38 lines · Xiao Ni

Now it only adds bitmap offset based on cluster nodes. It's not right. It needs to add per node bitmap space to find next node bitmap position.

---
    super1.c | 12 +++++++++---
    1 file changed, 9 insertions(+), 3 deletions(-)
super1.c +9 -3
index 8b0d6ff..b5b379b 100644
@@ -2582,8 +2582,9 @@ add_internal_bitmap1(struct supertype *st,
    static int locate_bitmap1(struct supertype *st, int fd, int node_num)
    {
-    unsigned long long offset;
+    unsigned long long offset, bm_sectors_per_node;
        struct mdp_superblock_1 *sb;
+    bitmap_super_t *bms;
        int mustfree = 0;
        int ret;
@@ -2598,8 +2599,13 @@ static int locate_bitmap1(struct supertype *st, int fd, int node_num)
            ret = 0;
        else
            ret = -1;
-    offset = __le64_to_cpu(sb->super_offset);
-    offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
+
+    offset = __le64_to_cpu(sb->super_offset) + __le32_to_cpu(sb->bitmap_offset);
+    if (node_num) {
+        bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
+        bm_sectors_per_node = calc_bitmap_size(bms, 4096) >> 9;
+        offset += bm_sectors_per_node * node_num;
+    }
        if (mustfree)
            free(sb);
        lseek64(fd, offset<<9, 0);