From: Nikolay Borisov <nborisov@suse.com>
To: linux-btrfs@vger.kernel.org
Cc: Nikolay Borisov <nborisov@suse.com>
Subject: [PATCH 1/2] btrfs: Introduce btrfs_try_lock_balance
Date: Tue, 3 May 2022 11:36:36 +0300 [thread overview]
Message-ID: <20220503083637.1051023-2-nborisov@suse.com> (raw)
In-Reply-To: <20220503083637.1051023-1-nborisov@suse.com>
This function contains the factored out locking sequence
btrfs_ioctl_balance. Having this piece of code separate helps to
simplify btrfs_ioctl_balance which has turned into a boiling pile of
spaghetti. This will be used in the next patch to streamline the logic
in btrfs_ioctl_balance.
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
fs/btrfs/ioctl.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 9d8e46815ee4..8e0cc17d5f81 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -4334,6 +4334,72 @@ void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
spin_unlock(&fs_info->balance_lock);
}
+/**
+ * Try to acquire fs_info::balance_Mutex as well as set BTRFS_EXLCOP_BALANCE as
+ * required.
+ *
+ * @fs_info: context of the filesystem
+ * @excl_acquired: ptr to boolean value which is set to 'false' in case balance
+ * is being resumed.
+ *
+ * Returns 0 on success in which case both fs_info::balance is acquired as well
+ * as exclusive ops are blocked. In case of failure returns an error code.
+ *
+ */
+static int btrfs_try_lock_balance(struct btrfs_fs_info *fs_info, bool *excl_acquired)
+{
+ int ret;
+ /*
+ * mut. excl. ops lock is locked. Three possibilities:
+ * (1) some other op is running
+ * (2) balance is running
+ * (3) balance is paused -- special case (think resume)
+ */
+ while (1) {
+ if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
+ *excl_acquired = true;
+ mutex_lock(&fs_info->balance_mutex);
+ return 0;
+ }
+
+ mutex_lock(&fs_info->balance_mutex);
+ if (fs_info->balance_ctl) {
+ /* this is either (2) or (3) */
+ if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
+ /* this is (2) */
+ ret = -EINPROGRESS;
+ goto out_failure;
+
+ } else {
+ mutex_unlock(&fs_info->balance_mutex);
+ /*
+ * Lock released to allow other waiters to continue,
+ * we'll reexamine the status again.
+ */
+ mutex_lock(&fs_info->balance_mutex);
+
+ if (fs_info->balance_ctl &&
+ !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
+ /* this is (3) */
+ *excl_acquired = false;
+ return 0;
+ }
+ }
+ } else {
+ /* this is (1) */
+ ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
+ goto out_failure;
+ }
+
+ mutex_unlock(&fs_info->balance_mutex);
+ }
+
+out_failure:
+ mutex_unlock(&fs_info->balance_mutex);
+ *excl_acquired = false;
+ return ret;
+}
+
static long btrfs_ioctl_balance(struct file *file, void __user *arg)
{
struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
--
2.25.1
next prev parent reply other threads:[~2022-05-03 8:36 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-03 8:36 [PATCH 0/2] Refactor btrfs_ioctl_balance Nikolay Borisov
2022-05-03 8:36 ` Nikolay Borisov [this message]
2022-05-03 10:20 ` [PATCH 1/2] btrfs: Introduce btrfs_try_lock_balance kernel test robot
2022-05-03 8:36 ` [PATCH 2/2] btrfs: Use btrfs_try_lock_balance in btrfs_ioctl_balance Nikolay Borisov
2022-05-04 6:46 ` [kbuild] " Dan Carpenter
2022-05-04 8:44 ` Nikolay Borisov
2022-05-04 15:25 ` David Sterba
2022-05-05 7:08 ` [PATCH v2] " Nikolay Borisov
2022-05-09 19:51 ` David Sterba
2022-05-10 5:42 ` Nikolay Borisov
2022-05-27 15:58 ` David Sterba
2022-05-06 9:04 ` [btrfs] c696e46e6e: BUG:KASAN:double-free_or_invalid-free_in_btrfs_ioctl_balance kernel test robot
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=20220503083637.1051023-2-nborisov@suse.com \
--to=nborisov@suse.com \
--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