diff mbox series

[1/2] btrfs: Introduce btrfs_try_lock_balance

Message ID 20220503083637.1051023-2-nborisov@suse.com (mailing list archive)
State New, archived
Headers show
Series Refactor btrfs_ioctl_balance | expand

Commit Message

Nikolay Borisov May 3, 2022, 8:36 a.m. UTC
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(+)

Comments

kernel test robot May 3, 2022, 10:20 a.m. UTC | #1
Hi Nikolay,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on kdave/for-next]
[also build test WARNING on v5.18-rc5 next-20220503]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Nikolay-Borisov/Refactor-btrfs_ioctl_balance/20220503-163837
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next
config: mips-allyesconfig (https://download.01.org/0day-ci/archive/20220503/202205031820.eWTnmpgQ-lkp@intel.com/config)
compiler: mips-linux-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/d383145190e87f46bc73d86059724df2b3af9720
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Nikolay-Borisov/Refactor-btrfs_ioctl_balance/20220503-163837
        git checkout d383145190e87f46bc73d86059724df2b3af9720
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash fs/btrfs/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> fs/btrfs/ioctl.c:4350: warning: expecting prototype for Try to acquire fs_info:(). Prototype was for btrfs_try_lock_balance() instead


vim +4350 fs/btrfs/ioctl.c

  4336	
  4337	/**
  4338	 * Try to acquire fs_info::balance_Mutex as well as set BTRFS_EXLCOP_BALANCE as
  4339	 * required.
  4340	 *
  4341	 * @fs_info:       context of the filesystem
  4342	 * @excl_acquired: ptr to boolean value which is set to 'false' in case balance
  4343	 * is being resumed.
  4344	 *
  4345	 * Returns 0 on success in which case both fs_info::balance is acquired as well
  4346	 * as exclusive ops are blocked. In case of failure returns an error code.
  4347	 *
  4348	 */
  4349	static int btrfs_try_lock_balance(struct btrfs_fs_info *fs_info, bool *excl_acquired)
> 4350	{
  4351		int ret;
  4352		/*
  4353		 * mut. excl. ops lock is locked.  Three possibilities:
  4354		 *   (1) some other op is running
  4355		 *   (2) balance is running
  4356		 *   (3) balance is paused -- special case (think resume)
  4357		 */
  4358		while (1) {
  4359			if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
  4360				*excl_acquired = true;
  4361				mutex_lock(&fs_info->balance_mutex);
  4362				return 0;
  4363			}
  4364	
  4365			mutex_lock(&fs_info->balance_mutex);
  4366			if (fs_info->balance_ctl) {
  4367				/* this is either (2) or (3) */
  4368				if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  4369					/* this is (2) */
  4370					ret = -EINPROGRESS;
  4371					goto out_failure;
  4372	
  4373				} else {
  4374					mutex_unlock(&fs_info->balance_mutex);
  4375					/*
  4376					 * Lock released to allow other waiters to continue,
  4377					 * we'll reexamine the status again.
  4378					 */
  4379					mutex_lock(&fs_info->balance_mutex);
  4380	
  4381					if (fs_info->balance_ctl &&
  4382					    !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  4383						/* this is (3) */
  4384						*excl_acquired = false;
  4385						return 0;
  4386					}
  4387				}
  4388			} else {
  4389				/* this is (1) */
  4390				ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  4391				goto out_failure;
  4392			}
  4393	
  4394			mutex_unlock(&fs_info->balance_mutex);
  4395		}
  4396	
  4397	out_failure:
  4398		mutex_unlock(&fs_info->balance_mutex);
  4399		*excl_acquired = false;
  4400		return ret;
  4401	}
  4402
diff mbox series

Patch

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;