linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: fdmanana@kernel.org, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 2/8] btrfs: preallocate inodes xarray entry to avoid transaction abort
Date: Thu, 9 May 2024 09:51:46 +0930	[thread overview]
Message-ID: <a521fb0a-c2f5-49cf-b1b8-9a1ae67ee824@gmx.com> (raw)
In-Reply-To: <b9b409c79af85d0fe336ebec2800c36399c8f515.1715169723.git.fdmanana@suse.com>



在 2024/5/8 21:47, fdmanana@kernel.org 写道:
> From: Filipe Manana <fdmanana@suse.com>
>
> When creating a new inode, at btrfs_create_new_inode(), one of the very
> last steps is to add the inode to the root's inodes xarray. This often
> requires allocating memory which may fail (even though xarrays have a
> dedicated kmem_cache which make it less likely to fail), and at that point
> we are forced to abort the current transaction (as some, but not all, of
> the inode metadata was added to its subvolume btree).
>
> To avoid a transaction abort, preallocate memory for the xarray early at
> btrfs_create_new_inode(), so that if we fail we don't need to abort the
> transaction and the insertion into the xarray is guaranteed to succeed.
>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

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

Thanks,
Qu
> ---
>   fs/btrfs/inode.c | 26 +++++++++++++++++++-------
>   1 file changed, 19 insertions(+), 7 deletions(-)
>
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 450fe1582f1d..85dbc19c2f6f 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -5493,7 +5493,7 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
>   	return err;
>   }
>
> -static int btrfs_add_inode_to_root(struct btrfs_inode *inode)
> +static int btrfs_add_inode_to_root(struct btrfs_inode *inode, bool prealloc)
>   {
>   	struct btrfs_root *root = inode->root;
>   	struct btrfs_inode *existing;
> @@ -5503,9 +5503,11 @@ static int btrfs_add_inode_to_root(struct btrfs_inode *inode)
>   	if (inode_unhashed(&inode->vfs_inode))
>   		return 0;
>
> -	ret = xa_reserve(&root->inodes, ino, GFP_NOFS);
> -	if (ret)
> -		return ret;
> +	if (prealloc) {
> +		ret = xa_reserve(&root->inodes, ino, GFP_NOFS);
> +		if (ret)
> +			return ret;
> +	}
>
>   	spin_lock(&root->inode_lock);
>   	existing = xa_store(&root->inodes, ino, inode, GFP_ATOMIC);
> @@ -5606,7 +5608,7 @@ struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
>
>   		ret = btrfs_read_locked_inode(inode, path);
>   		if (!ret) {
> -			ret = btrfs_add_inode_to_root(BTRFS_I(inode));
> +			ret = btrfs_add_inode_to_root(BTRFS_I(inode), true);
>   			if (ret) {
>   				iget_failed(inode);
>   				inode = ERR_PTR(ret);
> @@ -6237,6 +6239,7 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
>   	struct btrfs_item_batch batch;
>   	unsigned long ptr;
>   	int ret;
> +	bool xa_reserved = false;
>
>   	path = btrfs_alloc_path();
>   	if (!path)
> @@ -6251,6 +6254,11 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
>   		goto out;
>   	inode->i_ino = objectid;
>
> +	ret = xa_reserve(&root->inodes, objectid, GFP_NOFS);
> +	if (ret)
> +		goto out;
> +	xa_reserved = true;
> +
>   	if (args->orphan) {
>   		/*
>   		 * O_TMPFILE, set link count to 0, so that after this point, we
> @@ -6424,8 +6432,9 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
>   		}
>   	}
>
> -	ret = btrfs_add_inode_to_root(BTRFS_I(inode));
> -	if (ret) {
> +	ret = btrfs_add_inode_to_root(BTRFS_I(inode), false);
> +	if (WARN_ON(ret)) {
> +		/* Shouldn't happen, we used xa_reserve() before. */
>   		btrfs_abort_transaction(trans, ret);
>   		goto discard;
>   	}
> @@ -6456,6 +6465,9 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
>   	ihold(inode);
>   	discard_new_inode(inode);
>   out:
> +	if (xa_reserved)
> +		xa_release(&root->inodes, objectid);
> +
>   	btrfs_free_path(path);
>   	return ret;
>   }

  reply	other threads:[~2024-05-09  0:21 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-08 12:17 [PATCH 0/8] btrfs: inode management and memory consumption improvements fdmanana
2024-05-08 12:17 ` [PATCH 1/8] btrfs: use an xarray to track open inodes in a root fdmanana
2024-05-09  0:18   ` Qu Wenruo
2024-05-08 12:17 ` [PATCH 2/8] btrfs: preallocate inodes xarray entry to avoid transaction abort fdmanana
2024-05-09  0:21   ` Qu Wenruo [this message]
2024-05-08 12:17 ` [PATCH 3/8] btrfs: reduce nesting and deduplicate error handling at btrfs_iget_path() fdmanana
2024-05-09  0:23   ` Qu Wenruo
2024-05-08 12:17 ` [PATCH 4/8] btrfs: remove inode_lock from struct btrfs_root and use xarray locks fdmanana
2024-05-09  0:25   ` Qu Wenruo
2024-05-09  8:38     ` Filipe Manana
2024-05-09  8:42       ` Qu Wenruo
2024-05-08 12:17 ` [PATCH 5/8] btrfs: unify index_cnt and csum_bytes from struct btrfs_inode fdmanana
2024-05-09  0:30   ` Qu Wenruo
2024-05-09  8:39     ` Filipe Manana
2024-05-08 12:17 ` [PATCH 6/8] btrfs: don't allocate file extent tree for non regular files fdmanana
2024-05-09  0:39   ` Qu Wenruo
2024-05-09  8:41     ` Filipe Manana
2024-05-13 18:39       ` David Sterba
2024-05-08 12:17 ` [PATCH 7/8] btrfs: remove location key from struct btrfs_inode fdmanana
2024-05-08 12:17 ` [PATCH 8/8] btrfs: remove objectid from struct btrfs_inode on 64 bits platforms fdmanana
2024-05-09 17:56 ` [PATCH 0/8] btrfs: inode management and memory consumption improvements David Sterba
2024-05-10 11:04   ` Filipe Manana
2024-05-10 17:32 ` [PATCH v2 00/10] " fdmanana
2024-05-10 17:32   ` [PATCH v2 01/10] btrfs: use an xarray to track open inodes in a root fdmanana
2024-05-14 15:49     ` David Sterba
2024-05-10 17:32   ` [PATCH v2 02/10] btrfs: preallocate inodes xarray entry to avoid transaction abort fdmanana
2024-05-10 17:32   ` [PATCH v2 03/10] btrfs: reduce nesting and deduplicate error handling at btrfs_iget_path() fdmanana
2024-05-10 17:32   ` [PATCH v2 04/10] btrfs: remove inode_lock from struct btrfs_root and use xarray locks fdmanana
2024-05-10 17:32   ` [PATCH v2 05/10] btrfs: unify index_cnt and csum_bytes from struct btrfs_inode fdmanana
2024-05-10 17:32   ` [PATCH v2 06/10] btrfs: don't allocate file extent tree for non regular files fdmanana
2024-05-10 17:32   ` [PATCH v2 07/10] btrfs: remove location key from struct btrfs_inode fdmanana
2024-05-10 17:32   ` [PATCH v2 08/10] btrfs: remove objectid from struct btrfs_inode on 64 bits platforms fdmanana
2024-05-10 17:32   ` [PATCH v2 09/10] btrfs: rename rb_root member of extent_map_tree from map to root fdmanana
2024-05-10 17:32   ` [PATCH v2 10/10] btrfs: use a regular rb_root instead of cached rb_root for extent_map_tree fdmanana
2024-05-14 15:58     ` David Sterba
2024-05-14 16:08   ` [PATCH v2 00/10] btrfs: inode management and memory consumption improvements David Sterba
2024-05-15 18:28   ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a521fb0a-c2f5-49cf-b1b8-9a1ae67ee824@gmx.com \
    --to=quwenruo.btrfs@gmx.com \
    --cc=fdmanana@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).