diff mbox series

[3/5] btrfs: free qgroup pertrans rsv on trans abort

Message ID 07934597eaee1e2204c204bfd34bc628708e3739.1701464169.git.boris@bur.io (mailing list archive)
State New, archived
Headers show
Series btrfs: qgroups rsv fixes | expand

Commit Message

Boris Burkov Dec. 1, 2023, 9 p.m. UTC
If we abort a transaction, we never run the code that frees the pertrans
qgroup reservation. This results in warnings on unmount as that
reservation has been leaked. The leak isn't a huge issue since the fs is
read-only, but it's better to clean it up when we know we can/should. Do
it during the cleanup_transaction step of aborting.

Signed-off-by: Boris Burkov <boris@bur.io>
---
 fs/btrfs/disk-io.c | 28 ++++++++++++++++++++++++++++
 fs/btrfs/qgroup.c  |  5 +++--
 2 files changed, 31 insertions(+), 2 deletions(-)

Comments

Qu Wenruo Dec. 4, 2023, 9:08 p.m. UTC | #1
On 2023/12/2 07:30, Boris Burkov wrote:
> If we abort a transaction, we never run the code that frees the pertrans
> qgroup reservation. This results in warnings on unmount as that
> reservation has been leaked. The leak isn't a huge issue since the fs is
> read-only, but it's better to clean it up when we know we can/should. Do
> it during the cleanup_transaction step of aborting.
>
> Signed-off-by: Boris Burkov <boris@bur.io>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu
> ---
>   fs/btrfs/disk-io.c | 28 ++++++++++++++++++++++++++++
>   fs/btrfs/qgroup.c  |  5 +++--
>   2 files changed, 31 insertions(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index 9317606017e2..a1f440cd6d45 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -4775,6 +4775,32 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
>   	}
>   }
>
> +static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
> +{
> +	struct btrfs_root *gang[8];
> +	int i;
> +	int ret;
> +
> +	spin_lock(&fs_info->fs_roots_radix_lock);
> +	while (1) {
> +		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
> +						 (void **)gang, 0,
> +						 ARRAY_SIZE(gang),
> +						 0); // BTRFS_ROOT_TRANS_TAG
> +		if (ret == 0)
> +			break;
> +		for (i = 0; i < ret; i++) {
> +			struct btrfs_root *root = gang[i];
> +
> +			btrfs_qgroup_free_meta_all_pertrans(root);
> +			radix_tree_tag_clear(&fs_info->fs_roots_radix,
> +					(unsigned long)root->root_key.objectid,
> +					0); // BTRFS_ROOT_TRANS_TAG
> +		}
> +	}
> +	spin_unlock(&fs_info->fs_roots_radix_lock);
> +}
> +
>   void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
>   				   struct btrfs_fs_info *fs_info)
>   {
> @@ -4803,6 +4829,8 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
>   				     EXTENT_DIRTY);
>   	btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
>
> +	btrfs_free_all_qgroup_pertrans(fs_info);
> +
>   	cur_trans->state =TRANS_STATE_COMPLETED;
>   	wake_up(&cur_trans->commit_wait);
>   }
> diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
> index a953c16c7eb8..daec90342dad 100644
> --- a/fs/btrfs/qgroup.c
> +++ b/fs/btrfs/qgroup.c
> @@ -4337,8 +4337,9 @@ static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
>
>   		qgroup_rsv_release(fs_info, qgroup, num_bytes,
>   				BTRFS_QGROUP_RSV_META_PREALLOC);
> -		qgroup_rsv_add(fs_info, qgroup, num_bytes,
> -				BTRFS_QGROUP_RSV_META_PERTRANS);
> +		if (!sb_rdonly(fs_info->sb))
> +			qgroup_rsv_add(fs_info, qgroup, num_bytes,
> +				       BTRFS_QGROUP_RSV_META_PERTRANS);
>
>   		list_for_each_entry(glist, &qgroup->groups, next_group)
>   			qgroup_iterator_add(&qgroup_list, glist->group);
David Sterba Dec. 5, 2023, 2:27 p.m. UTC | #2
On Fri, Dec 01, 2023 at 01:00:11PM -0800, Boris Burkov wrote:
> If we abort a transaction, we never run the code that frees the pertrans
> qgroup reservation. This results in warnings on unmount as that
> reservation has been leaked. The leak isn't a huge issue since the fs is
> read-only, but it's better to clean it up when we know we can/should. Do
> it during the cleanup_transaction step of aborting.
> 
> Signed-off-by: Boris Burkov <boris@bur.io>
> ---
>  fs/btrfs/disk-io.c | 28 ++++++++++++++++++++++++++++
>  fs/btrfs/qgroup.c  |  5 +++--
>  2 files changed, 31 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index 9317606017e2..a1f440cd6d45 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -4775,6 +4775,32 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
>  	}
>  }
>  
> +static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
> +{
> +	struct btrfs_root *gang[8];
> +	int i;
> +	int ret;
> +
> +	spin_lock(&fs_info->fs_roots_radix_lock);
> +	while (1) {
> +		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
> +						 (void **)gang, 0,
> +						 ARRAY_SIZE(gang),
> +						 0); // BTRFS_ROOT_TRANS_TAG

What does the comment mean?
Boris Burkov Dec. 5, 2023, 7:45 p.m. UTC | #3
On Tue, Dec 05, 2023 at 03:27:32PM +0100, David Sterba wrote:
> On Fri, Dec 01, 2023 at 01:00:11PM -0800, Boris Burkov wrote:
> > If we abort a transaction, we never run the code that frees the pertrans
> > qgroup reservation. This results in warnings on unmount as that
> > reservation has been leaked. The leak isn't a huge issue since the fs is
> > read-only, but it's better to clean it up when we know we can/should. Do
> > it during the cleanup_transaction step of aborting.
> > 
> > Signed-off-by: Boris Burkov <boris@bur.io>
> > ---
> >  fs/btrfs/disk-io.c | 28 ++++++++++++++++++++++++++++
> >  fs/btrfs/qgroup.c  |  5 +++--
> >  2 files changed, 31 insertions(+), 2 deletions(-)
> > 
> > diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> > index 9317606017e2..a1f440cd6d45 100644
> > --- a/fs/btrfs/disk-io.c
> > +++ b/fs/btrfs/disk-io.c
> > @@ -4775,6 +4775,32 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
> >  	}
> >  }
> >  
> > +static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
> > +{
> > +	struct btrfs_root *gang[8];
> > +	int i;
> > +	int ret;
> > +
> > +	spin_lock(&fs_info->fs_roots_radix_lock);
> > +	while (1) {
> > +		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
> > +						 (void **)gang, 0,
> > +						 ARRAY_SIZE(gang),
> > +						 0); // BTRFS_ROOT_TRANS_TAG
> 
> What does the comment mean?

Oops, I forgot about this. BTRFS_ROOT_TRANS_TAG is a #define in
transaction.c, so it wasn't visible here in disk-io.c. I should move it
to some header they both include. Based on the other stuff in there,
btrfs/fs.h looks reasonable?
David Sterba Dec. 5, 2023, 10:39 p.m. UTC | #4
On Tue, Dec 05, 2023 at 11:45:52AM -0800, Boris Burkov wrote:
> On Tue, Dec 05, 2023 at 03:27:32PM +0100, David Sterba wrote:
> > On Fri, Dec 01, 2023 at 01:00:11PM -0800, Boris Burkov wrote:
> > > If we abort a transaction, we never run the code that frees the pertrans
> > > qgroup reservation. This results in warnings on unmount as that
> > > reservation has been leaked. The leak isn't a huge issue since the fs is
> > > read-only, but it's better to clean it up when we know we can/should. Do
> > > it during the cleanup_transaction step of aborting.
> > > 
> > > Signed-off-by: Boris Burkov <boris@bur.io>
> > > ---
> > >  fs/btrfs/disk-io.c | 28 ++++++++++++++++++++++++++++
> > >  fs/btrfs/qgroup.c  |  5 +++--
> > >  2 files changed, 31 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> > > index 9317606017e2..a1f440cd6d45 100644
> > > --- a/fs/btrfs/disk-io.c
> > > +++ b/fs/btrfs/disk-io.c
> > > @@ -4775,6 +4775,32 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
> > >  	}
> > >  }
> > >  
> > > +static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
> > > +{
> > > +	struct btrfs_root *gang[8];
> > > +	int i;
> > > +	int ret;
> > > +
> > > +	spin_lock(&fs_info->fs_roots_radix_lock);
> > > +	while (1) {
> > > +		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
> > > +						 (void **)gang, 0,
> > > +						 ARRAY_SIZE(gang),
> > > +						 0); // BTRFS_ROOT_TRANS_TAG
> > 
> > What does the comment mean?
> 
> Oops, I forgot about this. BTRFS_ROOT_TRANS_TAG is a #define in
> transaction.c, so it wasn't visible here in disk-io.c. I should move it
> to some header they both include. Based on the other stuff in there,
> btrfs/fs.h looks reasonable?

Maybe transaction.h
diff mbox series

Patch

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 9317606017e2..a1f440cd6d45 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -4775,6 +4775,32 @@  void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
 	}
 }
 
+static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
+{
+	struct btrfs_root *gang[8];
+	int i;
+	int ret;
+
+	spin_lock(&fs_info->fs_roots_radix_lock);
+	while (1) {
+		ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
+						 (void **)gang, 0,
+						 ARRAY_SIZE(gang),
+						 0); // BTRFS_ROOT_TRANS_TAG
+		if (ret == 0)
+			break;
+		for (i = 0; i < ret; i++) {
+			struct btrfs_root *root = gang[i];
+
+			btrfs_qgroup_free_meta_all_pertrans(root);
+			radix_tree_tag_clear(&fs_info->fs_roots_radix,
+					(unsigned long)root->root_key.objectid,
+					0); // BTRFS_ROOT_TRANS_TAG
+		}
+	}
+	spin_unlock(&fs_info->fs_roots_radix_lock);
+}
+
 void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
 				   struct btrfs_fs_info *fs_info)
 {
@@ -4803,6 +4829,8 @@  void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
 				     EXTENT_DIRTY);
 	btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
 
+	btrfs_free_all_qgroup_pertrans(fs_info);
+
 	cur_trans->state =TRANS_STATE_COMPLETED;
 	wake_up(&cur_trans->commit_wait);
 }
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index a953c16c7eb8..daec90342dad 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -4337,8 +4337,9 @@  static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
 
 		qgroup_rsv_release(fs_info, qgroup, num_bytes,
 				BTRFS_QGROUP_RSV_META_PREALLOC);
-		qgroup_rsv_add(fs_info, qgroup, num_bytes,
-				BTRFS_QGROUP_RSV_META_PERTRANS);
+		if (!sb_rdonly(fs_info->sb))
+			qgroup_rsv_add(fs_info, qgroup, num_bytes,
+				       BTRFS_QGROUP_RSV_META_PERTRANS);
 
 		list_for_each_entry(glist, &qgroup->groups, next_group)
 			qgroup_iterator_add(&qgroup_list, glist->group);