Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Nikolay Borisov <nborisov@suse.com>
To: linux-btrfs@vger.kernel.org
Cc: Nikolay Borisov <nborisov@suse.com>
Subject: [PATCH 1/2] btrfs: Simplify snapshot exclusion code
Date: Tue, 23 Apr 2019 14:42:06 +0300	[thread overview]
Message-ID: <20190423114207.7899-1-nborisov@suse.com> (raw)

BTRFS sports a mechanism to provide exclusion when a snapshot is about
to be created. This is implemented via btrfs_start_write_no_snapshotting
et al. Currently the implementation of that mechanism is some perverse
amalgamation of a percpu variable, an explicit waitqueue, an atomic_t
variable and an implicit wait bit on said atomic_t via wait_var_event
family of API. And for good measure there is a memory barrier thrown in
the mix...

Astute reader should have concluded by now that it's bordering on
impossible to prove whether this scheme works. What's worse - all of
this is required to achieve something really simple - ensure certain
operations cannot run during snapshot creation. Let's simplify this by
relying on a single atomic_t used as a boolean flag. This commit changes
only the implementation and not the semantics of the existing mechanism.

Now, if the atomic is 1 (snapshot is in progress) callers of
btrfs_start_write_no_snapshotting will get a ret val of 0 that should be
handled accordingly.

btrfs_wait_for_snapshot_creation OTOH will block until snapshotting is
in progress and return when current snapshot in progress is finished and
will acquire the right to create a snapshot.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
 fs/btrfs/extent-tree.c | 20 +++++---------------
 fs/btrfs/ioctl.c       |  9 ++-------
 2 files changed, 7 insertions(+), 22 deletions(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 8f2b7b29c3fd..d9e2e35700fd 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -11333,25 +11333,15 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
  */
 void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
 {
-	percpu_counter_dec(&root->subv_writers->counter);
-	cond_wake_up(&root->subv_writers->wait);
+	ASSERT(atomic_read(&root->will_be_snapshotted) == 1);
+	if (atomic_dec_and_test(&root->will_be_snapshotted))
+		wake_up_var(&root->will_be_snapshotted);
 }
 
 int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
 {
-	if (atomic_read(&root->will_be_snapshotted))
-		return 0;
-
-	percpu_counter_inc(&root->subv_writers->counter);
-	/*
-	 * Make sure counter is updated before we check for snapshot creation.
-	 */
-	smp_mb();
-	if (atomic_read(&root->will_be_snapshotted)) {
-		btrfs_end_write_no_snapshotting(root);
-		return 0;
-	}
-	return 1;
+	ASSERT(atomic_read(&root->will_be_snapshotted) >= 0);
+	return atomic_add_unless(&root->will_be_snapshotted, 1, 1);
 }
 
 void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 8774d4be7c97..f9f66c8a5dad 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -794,11 +794,7 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
 	 * possible. This is to avoid later writeback (running dealloc) to
 	 * fallback to COW mode and unexpectedly fail with ENOSPC.
 	 */
-	atomic_inc(&root->will_be_snapshotted);
-	smp_mb__after_atomic();
-	/* wait for no snapshot writes */
-	wait_event(root->subv_writers->wait,
-		   percpu_counter_sum(&root->subv_writers->counter) == 0);
+	btrfs_wait_for_snapshot_creation(root);
 
 	ret = btrfs_start_delalloc_snapshot(root);
 	if (ret)
@@ -878,8 +874,7 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
 dec_and_free:
 	if (snapshot_force_cow)
 		atomic_dec(&root->snapshot_force_cow);
-	if (atomic_dec_and_test(&root->will_be_snapshotted))
-		wake_up_var(&root->will_be_snapshotted);
+	btrfs_end_write_no_snapshotting(root);
 free_pending:
 	kfree(pending_snapshot->root_item);
 	btrfs_free_path(pending_snapshot->path);
-- 
2.17.1


             reply	other threads:[~2019-04-23 11:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-23 11:42 Nikolay Borisov [this message]
2019-04-23 11:42 ` [PATCH 2/2] btrfs: Remove dead code Nikolay Borisov
2019-04-23 13:58   ` [PATCH v2] " Nikolay Borisov
2019-04-23 14:49     ` [PATCH v3] " Nikolay Borisov
2019-04-24  9:49 ` [PATCH 1/2] btrfs: Simplify snapshot exclusion code Filipe Manana
2019-04-24 10:19   ` Filipe Manana

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=20190423114207.7899-1-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