Skip to contentSkip to conversation

btrfs: free fs roots on failed mount

PATCH Superseded Josef Bacik 3 participants 12 messages Jul 22, 2020 – Jul 28, 2020 Btrfs file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 btrfs: free fs roots on failed mount

While testing a weird problem with -o degraded, I noticed I was getting leaked root errors

BTRFS warning (device loop0): writable mount is not allowed due to too many missing devices BTRFS error (device loop0): open_ctree failed
BTRFS error (device loop0): leaked root -9-0 refcount 1

This is the DATA_RELOC root, which gets read before the other fs roots, but is included in the fs roots radix tree, and thus gets freed by btrfs_free_fs_roots. Fix this by moving the call into fail_tree_roots:
in open_ctree. With this fix we no longer leak that root on mount failure.

---
 fs/btrfs/disk-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/disk-io.c +1 -1
index c850d7f44fbe..f1fdbdd44c02 100644
@@ -3421,7 +3421,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 fail_trans_kthread:
 	kthread_stop(fs_info->transaction_kthread);
 	btrfs_cleanup_transaction(fs_info);
-	btrfs_free_fs_roots(fs_info);
 fail_cleaner:
 	kthread_stop(fs_info->cleaner_kthread);
 
@@ -3441,6 +3440,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 	btrfs_put_block_group_cache(fs_info);
 
 fail_tree_roots:
+	btrfs_free_fs_roots(fs_info);
 	free_root_pointers(fs_info, true);
 	invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
 
Signature · 1 line
2.24.1
#1 lore raw

PATCH 2/2 btrfs: fix root leak printk to use %lld instead of %llu

I'm a actual human being so am incapable of converting u64 to s64 in my head, use %lld so we can see the negative number in order to identify which of our special roots we leaked.

---
 fs/btrfs/disk-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/disk-io.c +1 -1
index f1fdbdd44c02..cc4081a1c7f9 100644
@@ -1508,7 +1508,7 @@ void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
 	while (!list_empty(&fs_info->allocated_roots)) {
 		root = list_first_entry(&fs_info->allocated_roots,
 					struct btrfs_root, leak_list);
-		btrfs_err(fs_info, "leaked root %llu-%llu refcount %d",
+		btrfs_err(fs_info, "leaked root %lld-%llu refcount %d",
 			  root->root_key.objectid, root->root_key.offset,
 			  refcount_read(&root->refs));
 		while (refcount_read(&root->refs) > 1)
Signature · 1 line
2.24.1
#2 lore raw

Re: [PATCH 2/2] btrfs: fix root leak printk to use %lld instead of %llu

Quoted 14 lines · Josef Bacik

I'm a actual human being so am incapable of converting u64 to s64 in my head, use %lld so we can see the negative number in order to identify which of our special roots we leaked.

---
 fs/btrfs/disk-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/disk-io.c +0 -0
index f1fdbdd44c02..cc4081a1c7f9 100644

fs/btrfs/disk-io.c void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) · line 1511

Quoted 5 lines
fs/btrfs/disk-io.c +1 -1
@@ -1508,7 +1508,7 @@ void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
 	while (!list_empty(&fs_info->allocated_roots)) {
 		root = list_first_entry(&fs_info->allocated_roots,
 					struct btrfs_root, leak_list);
-		btrfs_err(fs_info, "leaked root %llu-%llu refcount %d",
+		btrfs_err(fs_info, "leaked root %lld-%llu refcount %d",

But this is wrong in another way, roots with high numbers will appear as negative numbers.

#3 lore raw

Re: [PATCH 2/2] btrfs: fix root leak printk to use %lld instead of %llu

Quoted 10 lines · David Sterba
Quoted 9 lines · Josef Bacik

I'm a actual human being so am incapable of converting u64 to s64 in my head, use %lld so we can see the negative number in order to identify which of our special roots we leaked.

---
 fs/btrfs/disk-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

fs/btrfs/disk-io.c void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) · line 1511

Quoted 10 lines
fs/btrfs/disk-io.c +1 -1
index f1fdbdd44c02..cc4081a1c7f9 100644
@@ -1508,7 +1508,7 @@ void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
 	while (!list_empty(&fs_info->allocated_roots)) {
 		root = list_first_entry(&fs_info->allocated_roots,
 					struct btrfs_root, leak_list);
-		btrfs_err(fs_info, "leaked root %llu-%llu refcount %d",
+		btrfs_err(fs_info, "leaked root %lld-%llu refcount %d",

But this is wrong in another way, roots with high numbers will appear as negative numbers.

Nope. We won't have that many roots.

In fact, for subvolumes, the highest id is only 2 ^ 48, an special limit introduced for qgroup.

So we won't have high enough subvolume ids to be negative, but only special trees.

Thanks,
Qu

#4 lore raw

Re: [PATCH 2/2] btrfs: fix root leak printk to use %lld instead of %llu

Quoted 28 lines · Qu Wenruo
Quoted 10 lines · David Sterba
Quoted 9 lines · Josef Bacik

I'm a actual human being so am incapable of converting u64 to s64 in my head, use %lld so we can see the negative number in order to identify which of our special roots we leaked.

---
 fs/btrfs/disk-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

fs/btrfs/disk-io.c void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) · line 1511

Quoted 10 lines
fs/btrfs/disk-io.c +1 -1
index f1fdbdd44c02..cc4081a1c7f9 100644
@@ -1508,7 +1508,7 @@ void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
 	while (!list_empty(&fs_info->allocated_roots)) {
 		root = list_first_entry(&fs_info->allocated_roots,
 					struct btrfs_root, leak_list);
-		btrfs_err(fs_info, "leaked root %llu-%llu refcount %d",
+		btrfs_err(fs_info, "leaked root %lld-%llu refcount %d",

But this is wrong in another way, roots with high numbers will appear as negative numbers.

Quoted 4 lines

Nope. We won't have that many roots.

In fact, for subvolumes, the highest id is only 2 ^ 48, an special limit introduced for qgroup.

It's not a hard limit and certainly can have subvolumes with numbers that high. That qgoups interpret the qgroup in some way is not a limitation on subvolumes. We'll have to start reusing the subvolume ids eventually, with qgroups we can on.

Also the negativer numbers start to appear with 2^32 so that's still below the percieved limit of 2^48.

Quoted 2 lines

So we won't have high enough subvolume ids to be negative, but only special trees.

For the internal trees we eg. have pretty-printer in progs so kernel can reuse that.

#5 lore raw

Re: [PATCH 2/2] btrfs: fix root leak printk to use %lld instead of %llu

Quoted 10 lines · David Sterba
Quoted 4 lines · Qu Wenruo

Nope. We won't have that many roots.

In fact, for subvolumes, the highest id is only 2 ^ 48, an special limit introduced for qgroup.

It's not a hard limit and certainly can have subvolumes with numbers that high. That qgoups interpret the qgroup in some way is not a limitation on subvolumes. We'll have to start reusing the subvolume ids eventually, with qgroups we can on.

Let me rephrase without the typos:

It's not a hard limit and we certainly can have subvolumes with numbers that high. That qgroups interpret the qgroup id in some way is not a limitation on subvolumes. We'll have to start reusing the subvolume ids eventually, when qgroups are turned on.

(The comment about 2^32/2^48 was incorrect.)

#6 lore raw

Re: [PATCH 2/2] btrfs: fix root leak printk to use %lld instead of %llu

In reply to David Sterba
Quoted 31 lines · David Sterba
Quoted 30 lines · Qu Wenruo
Quoted 10 lines · David Sterba
Quoted 9 lines · Josef Bacik

I'm a actual human being so am incapable of converting u64 to s64 in my head, use %lld so we can see the negative number in order to identify which of our special roots we leaked.

---
 fs/btrfs/disk-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

fs/btrfs/disk-io.c void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info) · line 1511

Quoted 10 lines
fs/btrfs/disk-io.c +1 -1
index f1fdbdd44c02..cc4081a1c7f9 100644
@@ -1508,7 +1508,7 @@ void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
 	while (!list_empty(&fs_info->allocated_roots)) {
 		root = list_first_entry(&fs_info->allocated_roots,
 					struct btrfs_root, leak_list);
-		btrfs_err(fs_info, "leaked root %llu-%llu refcount %d",
+		btrfs_err(fs_info, "leaked root %lld-%llu refcount %d",

But this is wrong in another way, roots with high numbers will appear as negative numbers.

Nope. We won't have that many roots.

Quoted 7 lines
Quoted 2 lines

In fact, for subvolumes, the highest id is only 2 ^ 48, an special limit introduced for qgroup.

It's not a hard limit and certainly can have subvolumes with numbers that high. That qgoups interpret the qgroup in some way is not a limitation on subvolumes. We'll have to start reusing the subvolume ids eventually, with qgroups we can on.

Strange...

I still remember that we put that limit as a hard limit for subvolume creation.
Did we change that behavior in recent releases?

Quoted 3 lines

Also the negativer numbers start to appear with 2^32 so that's still below the percieved limit of 2^48.

Nope, For signed 64bits, it's -2^63 ~ 2^63 - 1, not 2 ^ 32.

Quoted 7 lines
Quoted 3 lines

So we won't have high enough subvolume ids to be negative, but only special trees.

For the internal trees we eg. have pretty-printer in progs so kernel can reuse that.

That's true. Pretty tree name is much better for human to read.

Thanks,
Qu

#7 lore raw

Re: [PATCH 2/2] btrfs: fix root leak printk to use %lld instead of %llu

Quoted 2 lines · Qu Wenruo

I still remember that we put that limit as a hard limit for subvolume creation.

Ok, I found it, added by your patch e09fe2d2119800e6 in 2015. It's not documented anywhere, eg. manual page 5 states the limit is 2^64.

2^48 should be enough for everyone and we can't probably changed it right now as it's part of API and ABI. Oh well.

#8 lore raw
In reply to Josef Bacik
Quoted 31 lines · Josef Bacik

While testing a weird problem with -o degraded, I noticed I was getting leaked root errors

BTRFS warning (device loop0): writable mount is not allowed due to too many missing devices BTRFS error (device loop0): open_ctree failed
BTRFS error (device loop0): leaked root -9-0 refcount 1

This is the DATA_RELOC root, which gets read before the other fs roots, but is included in the fs roots radix tree, and thus gets freed by btrfs_free_fs_roots. Fix this by moving the call into fail_tree_roots:
in open_ctree. With this fix we no longer leak that root on mount failure.

---
 fs/btrfs/disk-io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/disk-io.c +0 -1
index c850d7f44fbe..f1fdbdd44c02 100644
@@ -3421,7 +3421,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 fail_trans_kthread:
 	kthread_stop(fs_info->transaction_kthread);
 	btrfs_cleanup_transaction(fs_info);
-	btrfs_free_fs_roots(fs_info);
 fail_cleaner:
 	kthread_stop(fs_info->cleaner_kthread);
 

fs/btrfs/disk-io.c int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · line 3444

Quoted 5 lines
fs/btrfs/disk-io.c +1 -0
@@ -3441,6 +3440,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
 	btrfs_put_block_group_cache(fs_info);
 
 fail_tree_roots:
+	btrfs_free_fs_roots(fs_info);
 	free_root_pointers(fs_info, true);

The data reloc tree is freed inside free_root_pointers, that it's also in the radix tree is for convenience so I'd rather fix it inside free_root_pointers and not reorder btrfs_free_fs_roots.

Quoted 4 lines
fs/btrfs/disk-io.c +0 -0
 	invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
 
Signature · 1 line
2.24.1
#9 lore raw
Quoted 34 lines · David Sterba
Quoted 33 lines · Josef Bacik

While testing a weird problem with -o degraded, I noticed I was getting leaked root errors

BTRFS warning (device loop0): writable mount is not allowed due to too many missing devices BTRFS error (device loop0): open_ctree failed
BTRFS error (device loop0): leaked root -9-0 refcount 1

This is the DATA_RELOC root, which gets read before the other fs roots, but is included in the fs roots radix tree, and thus gets freed by btrfs_free_fs_roots. Fix this by moving the call into fail_tree_roots:
in open_ctree. With this fix we no longer leak that root on mount failure.

---
  fs/btrfs/disk-io.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/disk-io.c +0 -1
index c850d7f44fbe..f1fdbdd44c02 100644
@@ -3421,7 +3421,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
  fail_trans_kthread:
  	kthread_stop(fs_info->transaction_kthread);
  	btrfs_cleanup_transaction(fs_info);
-	btrfs_free_fs_roots(fs_info);
  fail_cleaner:
  	kthread_stop(fs_info->cleaner_kthread);
 
@@ -3441,6 +3440,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
  	btrfs_put_block_group_cache(fs_info);
 
Quoted 8 lines

fs/btrfs/disk-io.c int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · line 3444

Quoted 3 lines
fs/btrfs/disk-io.c +1 -0
  fail_tree_roots:
+	btrfs_free_fs_roots(fs_info);
  	free_root_pointers(fs_info, true);

The data reloc tree is freed inside free_root_pointers, that it's also in the radix tree is for convenience so I'd rather fix it inside free_root_pointers and not reorder btrfs_free_fs_roots.

We can't do that, because free_root_pointers() is called to drop the extent buffers when we're looping through the backup roots. btrfs_free_fs_roots() is the correct thing to do here, it goes through anything that ended up in the radix tree. Thanks,

Josef

#10 lore raw
Quoted 11 lines · Josef Bacik
Quoted 10 lines

fs/btrfs/disk-io.c int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · line 3444

Quoted 6 lines
fs/btrfs/disk-io.c +1 -0
@@ -3441,6 +3440,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
  	btrfs_put_block_group_cache(fs_info);
 
  fail_tree_roots:
+	btrfs_free_fs_roots(fs_info);
  	free_root_pointers(fs_info, true);

The data reloc tree is freed inside free_root_pointers, that it's also in the radix tree is for convenience so I'd rather fix it inside free_root_pointers and not reorder btrfs_free_fs_roots.

Quoted 4 lines

We can't do that, because free_root_pointers() is called to drop the extent buffers when we're looping through the backup roots. btrfs_free_fs_roots() is the correct thing to do here, it goes through anything that ended up in the radix tree. Thanks,

I see, free_root_pointers is used elsewhere. I'm concerned about the reordeing because there are several functions that are now called after the roots are freed.

(before) btrfs_free_fs_roots(fs_info);

		kthread_stop(fs_info->cleaner_kthread);
		filemap_write_and_wait(fs_info->btree_inode->i_mapping);
		btrfs_sysfs_remove_mounted(fs_info);
		btrfs_sysfs_remove_fsid(fs_info->fs_devices);
		btrfs_put_block_group_cache(fs_info);

(after) btrfs_free_fs_roots(fs_info);

If something called by btrfs_free_fs_roots depends on structures removed/cleaned by the other functions, eg. btrfs_put_block_group_cache ti could be a problem.

I haven't spotted anything so far, the functions are called after failure still during mount, this is easier than shutdown sequence after a full mount.

#11 lore raw
Quoted 31 lines · David Sterba
Quoted 11 lines · Josef Bacik
Quoted 10 lines

fs/btrfs/disk-io.c int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · line 3444

Quoted 6 lines
fs/btrfs/disk-io.c +1 -0
@@ -3441,6 +3440,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
   	btrfs_put_block_group_cache(fs_info);
 
   fail_tree_roots:
+	btrfs_free_fs_roots(fs_info);
   	free_root_pointers(fs_info, true);

The data reloc tree is freed inside free_root_pointers, that it's also in the radix tree is for convenience so I'd rather fix it inside free_root_pointers and not reorder btrfs_free_fs_roots.

Quoted 4 lines

We can't do that, because free_root_pointers() is called to drop the extent buffers when we're looping through the backup roots. btrfs_free_fs_roots() is the correct thing to do here, it goes through anything that ended up in the radix tree. Thanks,

I see, free_root_pointers is used elsewhere. I'm concerned about the reordeing because there are several functions that are now called after the roots are freed.

(before) btrfs_free_fs_roots(fs_info);

		kthread_stop(fs_info->cleaner_kthread);
		filemap_write_and_wait(fs_info->btree_inode->i_mapping);
		btrfs_sysfs_remove_mounted(fs_info);
		btrfs_sysfs_remove_fsid(fs_info->fs_devices);
		btrfs_put_block_group_cache(fs_info);

(after) btrfs_free_fs_roots(fs_info);

Quoted 8 lines

If something called by btrfs_free_fs_roots depends on structures removed/cleaned by the other functions, eg. btrfs_put_block_group_cache ti could be a problem.

I haven't spotted anything so far, the functions are called after failure still during mount, this is easier than shutdown sequence after a full mount.

Yeah I'm always loathe to move this stuff around. I actually think we can end up with the case described in close_ctree if we get an error during log replay on mount. I'll rework this some more. Thanks,

Josef