Skip to contentSkip to conversation

Btrfs: fix OOPS of empty filesystem after balance

PATCH In review liubo 3 participants 5 messages Mar 7, 2011 – Apr 1, 2011 Btrfs file system

lore
Share & tools
lore

liubo

author
#0 lore raw

PATCH 1/2 Btrfs: fix OOPS of empty filesystem after balance

btrfs will remove unused block groups after balance.
When a empty filesystem is balanced, the block group with tag "DATA" may be dropped, and after umount and mount again, it will not find "DATA" space_info and lead to OOPS.
So we initial the necessary space_infos(DATA, SYSTEM, METADATA) to avoid OOPS.

---
 fs/btrfs/ctree.h       |    1 +
 fs/btrfs/disk-io.c     |    6 ++++++
 fs/btrfs/extent-tree.c |   23 +++++++++++++++++++++++
 3 files changed, 30 insertions(+), 0 deletions(-)
fs/btrfs/ctree.h +1 -0
index 28188a7..49c50e5 100644
@@ -2221,6 +2221,7 @@ int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
 			       u64 num_bytes);
 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
 			    struct btrfs_root *root, u64 type);
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
 
 /* ctree.c */
 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
fs/btrfs/disk-io.c +6 -0
index 3e1ea3e..8bcdc62 100644
@@ -1967,6 +1967,12 @@ struct btrfs_root *open_ctree(struct super_block *sb,
 	fs_info->metadata_alloc_profile = (u64)-1;
 	fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
 
+	ret = btrfs_init_space_info(fs_info);
+	if (ret) {
+		printk(KERN_ERR "Failed to initial space info: %d\n", ret);
+		goto fail_block_groups;
+	}
+
 	ret = btrfs_read_block_groups(extent_root);
 	if (ret) {
 		printk(KERN_ERR "Failed to read block groups: %d\n", ret);
fs/btrfs/extent-tree.c +23 -0
index 100e409..08525ee 100644
@@ -8714,6 +8714,29 @@ out:
 	return ret;
 }
 
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
+{
+	struct btrfs_space_info *space_info;
+	int ret;
+
+	ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
+								 &space_info);
+	if (ret)
+		return ret;
+
+	ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
+								 &space_info);
+	if (ret)
+		return ret;
+
+	ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
+								 &space_info);
+	if (ret)
+		return ret;
+
+	return ret;
+}
+
 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
 {
 	return unpin_extent_range(root, start, end);
Signature · 1 line
1.6.5.2
author
#1 lore raw
Quoted 6 lines · liubo

btrfs will remove unused block groups after balance.
When a empty filesystem is balanced, the block group with tag "DATA" may be dropped, and after umount and mount again, it will not find "DATA" space_info and lead to OOPS.
So we initial the necessary space_infos(DATA, SYSTEM, METADATA) to avoid OOPS.

Hi, Chirs,

These two fixes are for critical problems(one OOPS and one memory leak), so would you please take some time to review them and check if they are ready for the next git pull?

Seems that you have been a lot busy these days. ;)

thanks,
liubo

Quoted 71 lines
---
 fs/btrfs/ctree.h       |    1 +
 fs/btrfs/disk-io.c     |    6 ++++++
 fs/btrfs/extent-tree.c |   23 +++++++++++++++++++++++
 3 files changed, 30 insertions(+), 0 deletions(-)
fs/btrfs/ctree.h +1 -0
index 28188a7..49c50e5 100644
@@ -2221,6 +2221,7 @@ int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
 			       u64 num_bytes);
 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
 			    struct btrfs_root *root, u64 type);
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
 
 /* ctree.c */
 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
fs/btrfs/disk-io.c +6 -0
index 3e1ea3e..8bcdc62 100644
@@ -1967,6 +1967,12 @@ struct btrfs_root *open_ctree(struct super_block *sb,
 	fs_info->metadata_alloc_profile = (u64)-1;
 	fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
 
+	ret = btrfs_init_space_info(fs_info);
+	if (ret) {
+		printk(KERN_ERR "Failed to initial space info: %d\n", ret);
+		goto fail_block_groups;
+	}
+
 	ret = btrfs_read_block_groups(extent_root);
 	if (ret) {
 		printk(KERN_ERR "Failed to read block groups: %d\n", ret);
fs/btrfs/extent-tree.c +23 -0
index 100e409..08525ee 100644
@@ -8714,6 +8714,29 @@ out:
 	return ret;
 }
 
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
+{
+	struct btrfs_space_info *space_info;
+	int ret;
+
+	ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
+								 &space_info);
+	if (ret)
+		return ret;
+
+	ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
+								 &space_info);
+	if (ret)
+		return ret;
+
+	ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
+								 &space_info);
+	if (ret)
+		return ret;
+
+	return ret;
+}
+
 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
 {
 	return unpin_extent_range(root, start, end);
#2 lore raw

Excerpts from liubo's message of 2011-03-10 03:50:27 -0500:

Quoted 8 lines · liubo
Quoted 6 lines

btrfs will remove unused block groups after balance.
When a empty filesystem is balanced, the block group with tag "DATA" may be dropped, and after umount and mount again, it will not find "DATA" space_info and lead to OOPS.
So we initial the necessary space_infos(DATA, SYSTEM, METADATA) to avoid OOPS.

Quoted 7 lines

Hi, Chirs,

These two fixes are for critical problems(one OOPS and one memory leak), so would you please take some time to review them and check if they are ready for the next git pull?

Seems that you have been a lot busy these days. ;)

Hi Liubo,

I'm looking at both of these. There are no more rc's for 2.6.38, only the final release, so the bar is very high for a commit that goes in.

-chris

Quoted 76 lines

thanks,
liubo

Quoted 71 lines
---
 fs/btrfs/ctree.h       |    1 +
 fs/btrfs/disk-io.c     |    6 ++++++
 fs/btrfs/extent-tree.c |   23 +++++++++++++++++++++++
 3 files changed, 30 insertions(+), 0 deletions(-)
fs/btrfs/ctree.h +1 -0
index 28188a7..49c50e5 100644
@@ -2221,6 +2221,7 @@ int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
                    u64 num_bytes);
 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
                 struct btrfs_root *root, u64 type);
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
 
 /* ctree.c */
 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
fs/btrfs/disk-io.c +6 -0
index 3e1ea3e..8bcdc62 100644
@@ -1967,6 +1967,12 @@ struct btrfs_root *open_ctree(struct super_block *sb,
     fs_info->metadata_alloc_profile = (u64)-1;
     fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
 
+    ret = btrfs_init_space_info(fs_info);
+    if (ret) {
+        printk(KERN_ERR "Failed to initial space info: %d\n", ret);
+        goto fail_block_groups;
+    }
+
     ret = btrfs_read_block_groups(extent_root);
     if (ret) {
         printk(KERN_ERR "Failed to read block groups: %d\n", ret);
fs/btrfs/extent-tree.c +23 -0
index 100e409..08525ee 100644
@@ -8714,6 +8714,29 @@ out:
     return ret;
 }
 
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
+{
+    struct btrfs_space_info *space_info;
+    int ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    return ret;
+}
+
 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
 {
     return unpin_extent_range(root, start, end);
#3 lore raw

Am 10.03.2011 13:28, schrieb Chris Mason:

Quoted 8 lines

Excerpts from liubo's message of 2011-03-10 03:50:27 -0500:

Quoted 7 lines · liubo
Quoted 6 lines

btrfs will remove unused block groups after balance.
When a empty filesystem is balanced, the block group with tag "DATA" may be dropped, and after umount and mount again, it will not find "DATA" space_info and lead to OOPS.
So we initial the necessary space_infos(DATA, SYSTEM, METADATA) to avoid OOPS.

this patch breaks mixed block groups. If the space_infos get added upfront, later on all mixed block groups will be added to the data space_info, leaving the metadata space_info completely empty. No mixed space_info will ever get created.
As a fix it might be enough to call btrfs_init_space_info after btrfs_read_block_groups, not before, but I haven't tested it.

This was the cause of the BUG reported by Sergei Trofimovich in the thread "v2.6.38-6555-ga44f99c: null pointer dereference on -ENOSPC".

-Arne

Quoted 92 lines
Quoted 8 lines

Hi, Chirs,

These two fixes are for critical problems(one OOPS and one memory leak), so would you please take some time to review them and check if they are ready for the next git pull?

Seems that you have been a lot busy these days. ;)

Hi Liubo,

I'm looking at both of these. There are no more rc's for 2.6.38, only the final release, so the bar is very high for a commit that goes in.

-chris

Quoted 76 lines

thanks,
liubo

Quoted 71 lines
---
 fs/btrfs/ctree.h       |    1 +
 fs/btrfs/disk-io.c     |    6 ++++++
 fs/btrfs/extent-tree.c |   23 +++++++++++++++++++++++
 3 files changed, 30 insertions(+), 0 deletions(-)
fs/btrfs/ctree.h +1 -0
index 28188a7..49c50e5 100644
@@ -2221,6 +2221,7 @@ int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
                    u64 num_bytes);
 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
                 struct btrfs_root *root, u64 type);
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
 
 /* ctree.c */
 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
fs/btrfs/disk-io.c +6 -0
index 3e1ea3e..8bcdc62 100644
@@ -1967,6 +1967,12 @@ struct btrfs_root *open_ctree(struct super_block *sb,
     fs_info->metadata_alloc_profile = (u64)-1;
     fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
 
+    ret = btrfs_init_space_info(fs_info);
+    if (ret) {
+        printk(KERN_ERR "Failed to initial space info: %d\n", ret);
+        goto fail_block_groups;
+    }
+
     ret = btrfs_read_block_groups(extent_root);
     if (ret) {
         printk(KERN_ERR "Failed to read block groups: %d\n", ret);
fs/btrfs/extent-tree.c +23 -0
index 100e409..08525ee 100644
@@ -8714,6 +8714,29 @@ out:
     return ret;
 }
 
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
+{
+    struct btrfs_space_info *space_info;
+    int ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    return ret;
+}
+
 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
 {
     return unpin_extent_range(root, start, end);
author
#4 lore raw
Quoted 10 lines · Arne Jansen

Am 10.03.2011 13:28, schrieb Chris Mason:

Quoted 8 lines

Excerpts from liubo's message of 2011-03-10 03:50:27 -0500:

Quoted 7 lines · liubo
Quoted 6 lines

btrfs will remove unused block groups after balance.
When a empty filesystem is balanced, the block group with tag "DATA" may be dropped, and after umount and mount again, it will not find "DATA" space_info and lead to OOPS.
So we initial the necessary space_infos(DATA, SYSTEM, METADATA) to avoid OOPS.

Quoted 4 lines

this patch breaks mixed block groups. If the space_infos get added upfront, later on all mixed block groups will be added to the data space_info, leaving the metadata space_info completely empty. No mixed space_info will ever get created.

Hi, Arne,

Sorry for the late reply.

Quoted 3 lines

As a fix it might be enough to call btrfs_init_space_info after btrfs_read_block_groups, not before, but I haven't tested it.

Seems impossible, the original bug just occurs in btrfs_read_block_groups()...

Quoted 3 lines

This was the cause of the BUG reported by Sergei Trofimovich in the thread "v2.6.38-6555-ga44f99c: null pointer dereference on -ENOSPC".

Thanks for pointing this out.
Anyway, will dig it more.

thanks,
liubo

Quoted 91 lines

-Arne

Quoted 88 lines
Quoted 7 lines

Hi, Chirs,

These two fixes are for critical problems(one OOPS and one memory leak), so would you please take some time to review them and check if they are ready for the next git pull?

Seems that you have been a lot busy these days. ;)

Hi Liubo,

I'm looking at both of these. There are no more rc's for 2.6.38, only the final release, so the bar is very high for a commit that goes in.

-chris

Quoted 74 lines

thanks,
liubo

Quoted 71 lines
---
 fs/btrfs/ctree.h       |    1 +
 fs/btrfs/disk-io.c     |    6 ++++++
 fs/btrfs/extent-tree.c |   23 +++++++++++++++++++++++
 3 files changed, 30 insertions(+), 0 deletions(-)
fs/btrfs/ctree.h +1 -0
index 28188a7..49c50e5 100644
@@ -2221,6 +2221,7 @@ int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
                    u64 num_bytes);
 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
                 struct btrfs_root *root, u64 type);
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
 
 /* ctree.c */
 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
fs/btrfs/disk-io.c +6 -0
index 3e1ea3e..8bcdc62 100644
@@ -1967,6 +1967,12 @@ struct btrfs_root *open_ctree(struct super_block *sb,
     fs_info->metadata_alloc_profile = (u64)-1;
     fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
 
+    ret = btrfs_init_space_info(fs_info);
+    if (ret) {
+        printk(KERN_ERR "Failed to initial space info: %d\n", ret);
+        goto fail_block_groups;
+    }
+
     ret = btrfs_read_block_groups(extent_root);
     if (ret) {
         printk(KERN_ERR "Failed to read block groups: %d\n", ret);
fs/btrfs/extent-tree.c +23 -0
index 100e409..08525ee 100644
@@ -8714,6 +8714,29 @@ out:
     return ret;
 }
 
+int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
+{
+    struct btrfs_space_info *space_info;
+    int ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
+                                 &space_info);
+    if (ret)
+        return ret;
+
+    return ret;
+}
+
 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
 {
     return unpin_extent_range(root, start, end);