Skip to contentSkip to conversation

Slightly how START_SYNC and WAIT_SYNC work

PATCH In review David Sterba 3 participants 8 messages Jun 3, 2021 – Jun 4, 2021 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

cover Slightly how START_SYNC and WAIT_SYNC work

The async transaction commit ioctl has a subtle semantics that used to work for ceph. We need more straightforward semantics in progs (eg. when waiting for commit after subvolume deletion) and otherwise the async commit does a few annoying things.

Long explanation is in patch 3. I hope it works, but somebody please double check. It's a minor change in the commit logic, but merely removing some waiting, no other changes in state transitions.

David Sterba (4):
btrfs: sink wait_for_unblock parameter to async commit
btrfs: inline wait_current_trans_commit_start in its caller
btrfs: replace async commit by pending actions
btrfs: remove fs_info::transaction_blocked_wait

 fs/btrfs/ctree.h       |   1 -
 fs/btrfs/disk-io.c     |   5 +-
 fs/btrfs/ioctl.c       |  12 ++---
 fs/btrfs/super.c       |   1 -
 fs/btrfs/transaction.c | 103 +----------------------------------------
 fs/btrfs/transaction.h |   2 -
 6 files changed, 9 insertions(+), 115 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/4 btrfs: sink wait_for_unblock parameter to async commit

There's only one caller left btrfs_ioctl_start_sync that passes 0, so we can remove the switch in btrfs_commit_transaction_async.

A cleanup 9babda9f33fd ("btrfs: Remove async_transid from btrfs_mksubvol/create_subvol/create_snapshot") removed calls that passed 1, so this is a followup.

As this removes last call of wait_current_trans_commit_start_and_unblock, remove the function as well.

---
 fs/btrfs/ioctl.c       |  2 +-
 fs/btrfs/transaction.c | 24 ++----------------------
 fs/btrfs/transaction.h |  3 +--
 3 files changed, 4 insertions(+), 25 deletions(-)
fs/btrfs/ioctl.c +1 -1
index 2bdaf2018197..f83eb4a225cc 100644
@@ -3643,7 +3643,7 @@ static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
 		goto out;
 	}
 	transid = trans->transid;
-	ret = btrfs_commit_transaction_async(trans, 0);
+	ret = btrfs_commit_transaction_async(trans);
 	if (ret) {
 		btrfs_end_transaction(trans);
 		return ret;
fs/btrfs/transaction.c +2 -22
index 22951621363f..30347e660027 100644
@@ -1882,19 +1882,6 @@ static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
 		   TRANS_ABORTED(trans));
 }
 
-/*
- * wait for the current transaction to start and then become unblocked.
- * caller holds ref.
- */
-static void wait_current_trans_commit_start_and_unblock(
-					struct btrfs_fs_info *fs_info,
-					struct btrfs_transaction *trans)
-{
-	wait_event(fs_info->transaction_wait,
-		   trans->state >= TRANS_STATE_UNBLOCKED ||
-		   TRANS_ABORTED(trans));
-}
-
 /*
  * commit transactions asynchronously. once btrfs_commit_transaction_async
  * returns, any subsequent transaction will not be allowed to join.
@@ -1922,8 +1909,7 @@ static void do_async_commit(struct work_struct *work)
 	kfree(ac);
 }
 
-int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
-				   int wait_for_unblock)
+int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans)
 {
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_async_commit *ac;
@@ -1955,13 +1941,7 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
 		__sb_writers_release(fs_info->sb, SB_FREEZE_FS);
 
 	schedule_work(&ac->work);
-
-	/* wait for transaction to start and unblock */
-	if (wait_for_unblock)
-		wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
-	else
-		wait_current_trans_commit_start(fs_info, cur_trans);
-
+	wait_current_trans_commit_start(fs_info, cur_trans);
 	if (current->journal_info == trans)
 		current->journal_info = NULL;
 
fs/btrfs/transaction.h +1 -2
index c49e2266b28b..0702e8d9b30e 100644
@@ -226,8 +226,7 @@ void btrfs_add_dead_root(struct btrfs_root *root);
 int btrfs_defrag_root(struct btrfs_root *root);
 int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
-int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
-				   int wait_for_unblock);
+int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
 bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
 void btrfs_throttle(struct btrfs_fs_info *fs_info);
Signature · 1 line
2.31.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] btrfs: sink wait_for_unblock parameter to async commit

Quoted 11 lines · David Sterba

There's only one caller left btrfs_ioctl_start_sync that passes 0, so we can remove the switch in btrfs_commit_transaction_async.

A cleanup 9babda9f33fd ("btrfs: Remove async_transid from btrfs_mksubvol/create_subvol/create_snapshot") removed calls that passed 1, so this is a followup.

As this removes last call of wait_current_trans_commit_start_and_unblock, remove the function as well.

Thanks, Anand

Quoted 83 lines
---
  fs/btrfs/ioctl.c       |  2 +-
  fs/btrfs/transaction.c | 24 ++----------------------
  fs/btrfs/transaction.h |  3 +--
  3 files changed, 4 insertions(+), 25 deletions(-)
fs/btrfs/ioctl.c +1 -1
index 2bdaf2018197..f83eb4a225cc 100644
@@ -3643,7 +3643,7 @@ static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  		goto out;
  	}
  	transid = trans->transid;
-	ret = btrfs_commit_transaction_async(trans, 0);
+	ret = btrfs_commit_transaction_async(trans);
  	if (ret) {
  		btrfs_end_transaction(trans);
  		return ret;
fs/btrfs/transaction.c +2 -22
index 22951621363f..30347e660027 100644
@@ -1882,19 +1882,6 @@ static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
  		   TRANS_ABORTED(trans));
  }
 
-/*
- * wait for the current transaction to start and then become unblocked.
- * caller holds ref.
- */
-static void wait_current_trans_commit_start_and_unblock(
-					struct btrfs_fs_info *fs_info,
-					struct btrfs_transaction *trans)
-{
-	wait_event(fs_info->transaction_wait,
-		   trans->state >= TRANS_STATE_UNBLOCKED ||
-		   TRANS_ABORTED(trans));
-}
-
  /*
   * commit transactions asynchronously. once btrfs_commit_transaction_async
   * returns, any subsequent transaction will not be allowed to join.
@@ -1922,8 +1909,7 @@ static void do_async_commit(struct work_struct *work)
  	kfree(ac);
  }
 
-int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
-				   int wait_for_unblock)
+int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans)
  {
  	struct btrfs_fs_info *fs_info = trans->fs_info;
  	struct btrfs_async_commit *ac;
@@ -1955,13 +1941,7 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  		__sb_writers_release(fs_info->sb, SB_FREEZE_FS);
 
  	schedule_work(&ac->work);
-
-	/* wait for transaction to start and unblock */
-	if (wait_for_unblock)
-		wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
-	else
-		wait_current_trans_commit_start(fs_info, cur_trans);
-
+	wait_current_trans_commit_start(fs_info, cur_trans);
  	if (current->journal_info == trans)
  		current->journal_info = NULL;
 
fs/btrfs/transaction.h +1 -2
index c49e2266b28b..0702e8d9b30e 100644
@@ -226,8 +226,7 @@ void btrfs_add_dead_root(struct btrfs_root *root);
  int btrfs_defrag_root(struct btrfs_root *root);
  int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
  int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
-int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
-				   int wait_for_unblock);
+int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
  int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
  bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
  void btrfs_throttle(struct btrfs_fs_info *fs_info);
author maintainer
#3 lore raw

PATCH 2/4 btrfs: inline wait_current_trans_commit_start in its caller

In reply to David Sterba

Function wait_current_trans_commit_start is now fairly trivial so it can be inlined in its only caller.

---
 fs/btrfs/transaction.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
fs/btrfs/transaction.c +7 -13
index 30347e660027..73df8b81496e 100644
@@ -1870,18 +1870,6 @@ int btrfs_transaction_blocked(struct btrfs_fs_info *info)
 	return ret;
 }
 
-/*
- * wait for the current transaction commit to start and block subsequent
- * transaction joins
- */
-static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
-					    struct btrfs_transaction *trans)
-{
-	wait_event(fs_info->transaction_blocked_wait,
-		   trans->state >= TRANS_STATE_COMMIT_START ||
-		   TRANS_ABORTED(trans));
-}
-
 /*
  * commit transactions asynchronously. once btrfs_commit_transaction_async
  * returns, any subsequent transaction will not be allowed to join.
@@ -1941,7 +1929,13 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans)
 		__sb_writers_release(fs_info->sb, SB_FREEZE_FS);
 
 	schedule_work(&ac->work);
-	wait_current_trans_commit_start(fs_info, cur_trans);
+	/*
+	 * Wait for the current transaction commit to start and block
+	 * subsequent transaction joins
+	 */
+	wait_event(fs_info->transaction_blocked_wait,
+		   cur_trans->state >= TRANS_STATE_COMMIT_START ||
+		   TRANS_ABORTED(cur_trans));
 	if (current->journal_info == trans)
 		current->journal_info = NULL;
 
Signature · 1 line
2.31.1
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] btrfs: inline wait_current_trans_commit_start in its caller

Quoted 4 lines · David Sterba

Function wait_current_trans_commit_start is now fairly trivial so it can be inlined in its only caller.

Thanks, Anand

Quoted 43 lines
---
  fs/btrfs/transaction.c | 20 +++++++-------------
  1 file changed, 7 insertions(+), 13 deletions(-)
fs/btrfs/transaction.c +7 -13
index 30347e660027..73df8b81496e 100644
@@ -1870,18 +1870,6 @@ int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  	return ret;
  }
 
-/*
- * wait for the current transaction commit to start and block subsequent
- * transaction joins
- */
-static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
-					    struct btrfs_transaction *trans)
-{
-	wait_event(fs_info->transaction_blocked_wait,
-		   trans->state >= TRANS_STATE_COMMIT_START ||
-		   TRANS_ABORTED(trans));
-}
-
  /*
   * commit transactions asynchronously. once btrfs_commit_transaction_async
   * returns, any subsequent transaction will not be allowed to join.
@@ -1941,7 +1929,13 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans)
  		__sb_writers_release(fs_info->sb, SB_FREEZE_FS);
 
  	schedule_work(&ac->work);
-	wait_current_trans_commit_start(fs_info, cur_trans);
+	/*
+	 * Wait for the current transaction commit to start and block
+	 * subsequent transaction joins
+	 */
+	wait_event(fs_info->transaction_blocked_wait,
+		   cur_trans->state >= TRANS_STATE_COMMIT_START ||
+		   TRANS_ABORTED(cur_trans));
  	if (current->journal_info == trans)
  		current->journal_info = NULL;
 
author maintainer
#5 lore raw

PATCH 3/4 btrfs: replace async commit by pending actions

In reply to David Sterba

Note: this is a semantic change of what the async transaction ioctl does. This used to be utilized by ceph, together with the now removed user transaction ioctls. Last external code that can be found using this ioctl is a test in ceph testsuite. As btrfs and ceph have taken independent routes, we don't need to keep the code around.

The btrfs-progs use the start and wait transaction ioctls in a simpler way, that does not require to block new transaction on the start. The new semantics is to eventually get transaction id of the running transaction if there is anything pending for a commit. The wait for transaction ioctl will block until it's finished.

If there's nothing to be done since start, wait will return immediately. This effectively implements "wait for current transaction, if any".

Any action started before the start, like deleting a subvolume, may take long time to finish. In case it's fast enough to finish before the start, the following wait will also finish immediately. A long running operation will catch the transaction and block at wait call until it's done. Both cases are expected and shall be the new semantics.

This is implemented by the pending action infrastructure, that allows to asynchronously and safely request a commit by just setting a bit and waking the transaction kthread.

This is different, because originally start would block, but now it returns immediately. However, the new behaviour still corresponds with the documentation of libbtrfsutil so this should not cause any surprises.

This also simplifies a few transaction commit things:

- another quirky freezing protection removed
- fs_info::transaction_blocked_wait is now never populated, so it's a no-op and can be simplified as well
- one less current->journal_info usage, that's been problematic in some cases

---
 fs/btrfs/ioctl.c       | 12 +++----
 fs/btrfs/transaction.c | 74 ------------------------------------------
 fs/btrfs/transaction.h |  1 -
 3 files changed, 5 insertions(+), 82 deletions(-)
fs/btrfs/ioctl.c +5 -7
index f83eb4a225cc..445bb19d8a57 100644
@@ -3629,9 +3629,9 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
 					    void __user *argp)
 {
+	struct btrfs_fs_info *fs_info = root->fs_info;
 	struct btrfs_trans_handle *trans;
 	u64 transid;
-	int ret;
 
 	trans = btrfs_attach_transaction_barrier(root);
 	if (IS_ERR(trans)) {
@@ -3639,15 +3639,13 @@ static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
 			return PTR_ERR(trans);
 
 		/* No running transaction, don't bother */
-		transid = root->fs_info->last_trans_committed;
+		transid = fs_info->last_trans_committed;
 		goto out;
 	}
 	transid = trans->transid;
-	ret = btrfs_commit_transaction_async(trans);
-	if (ret) {
-		btrfs_end_transaction(trans);
-		return ret;
-	}
+	/* Trigger commit via the pending action */
+	btrfs_set_pending(fs_info, COMMIT);
+	wake_up_process(fs_info->transaction_kthread);
 out:
 	if (argp)
 		if (copy_to_user(argp, &transid, sizeof(transid)))
fs/btrfs/transaction.c +0 -74

80 lines not on this page · raw message

fs/btrfs/transaction.h +0 -1
index 0702e8d9b30e..0bdb804fddcb 100644
@@ -226,7 +226,6 @@ void btrfs_add_dead_root(struct btrfs_root *root);
 int btrfs_defrag_root(struct btrfs_root *root);
 int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root);
 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
-int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
 bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
 void btrfs_throttle(struct btrfs_fs_info *fs_info);
Signature · 1 line
2.31.1
author maintainer
#6 lore raw

PATCH 4/4 btrfs: remove fs_info::transaction_blocked_wait

In reply to David Sterba

Previous commit removed last use of transaction_blocked_wait. There were still the waking call sites, that are now without use as there's nothing populating the wait list.

As wake_up has a memory barrier semantics, it's directly replaced by smp_mb. The transaction state TRANS_STATE_COMMIT_START is now perhaps trivial, but there's more code relying on that, it's left in place to keep the behaviour as close as possible to the original code.

---
 fs/btrfs/ctree.h       | 1 -
 fs/btrfs/disk-io.c     | 5 ++---
 fs/btrfs/super.c       | 1 -
 fs/btrfs/transaction.c | 3 ++-
 4 files changed, 4 insertions(+), 6 deletions(-)
fs/btrfs/ctree.h +0 -1
index c601f6733576..384c00c982ab 100644
@@ -683,7 +683,6 @@ struct btrfs_fs_info {
 	struct btrfs_transaction *running_transaction;
 	wait_queue_head_t transaction_throttle;
 	wait_queue_head_t transaction_wait;
-	wait_queue_head_t transaction_blocked_wait;
 	wait_queue_head_t async_submit_wait;
 
 	/*
fs/btrfs/disk-io.c +2 -3
index d1d5091a8385..bad7df788458 100644
@@ -2986,7 +2986,6 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 
 	init_waitqueue_head(&fs_info->transaction_throttle);
 	init_waitqueue_head(&fs_info->transaction_wait);
-	init_waitqueue_head(&fs_info->transaction_blocked_wait);
 	init_waitqueue_head(&fs_info->async_submit_wait);
 	init_waitqueue_head(&fs_info->delayed_iputs_wait);
 
@@ -4918,8 +4917,8 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
 	btrfs_destroy_delayed_refs(cur_trans, fs_info);
 
 	cur_trans->state = TRANS_STATE_COMMIT_START;
-	wake_up(&fs_info->transaction_blocked_wait);
-
+	/* Serialize state change, but there are no waiters */
+	smp_mb();
 	cur_trans->state = TRANS_STATE_UNBLOCKED;
 	wake_up(&fs_info->transaction_wait);
 
fs/btrfs/super.c +0 -1
index bc613218c8c5..8d87da2b2377 100644
@@ -302,7 +302,6 @@ void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
 	WRITE_ONCE(trans->transaction->aborted, errno);
 	/* Wake up anybody who may be waiting on this transaction */
 	wake_up(&fs_info->transaction_wait);
-	wake_up(&fs_info->transaction_blocked_wait);
 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
 }
 /*
fs/btrfs/transaction.c +2 -1
index 5f3c95c876c8..1fd04170b0bf 100644
@@ -2057,7 +2057,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	}
 
 	cur_trans->state = TRANS_STATE_COMMIT_START;
-	wake_up(&fs_info->transaction_blocked_wait);
+	/* Serialize state change, but there are no waiters */
+	smp_mb();
 
 	if (cur_trans->list.prev != &fs_info->trans_list) {
 		enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
Signature · 1 line
2.31.1
#7 lore raw
In reply to David Sterba
Quoted 9 lines · David Sterba

The async transaction commit ioctl has a subtle semantics that used to work for ceph. We need more straightforward semantics in progs (eg. when waiting for commit after subvolume deletion) and otherwise the async commit does a few annoying things.

Long explanation is in patch 3. I hope it works, but somebody please double check. It's a minor change in the commit logic, but merely removing some waiting, no other changes in state transitions.

Quoted 5 lines

David Sterba (4):
btrfs: sink wait_for_unblock parameter to async commit
btrfs: inline wait_current_trans_commit_start in its caller
btrfs: replace async commit by pending actions
btrfs: remove fs_info::transaction_blocked_wait

btrfs/011 hangs so no dice. I maybe pick the two first patches as cleanups, and the rest will go to some future dev cycle.