btrfs: Simplify snapshot exclusion code
PATCH 1/2 btrfs: Simplify snapshot exclusion code
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.
--- fs/btrfs/extent-tree.c | 20 +++++--------------- fs/btrfs/ioctl.c | 9 ++------- 2 files changed, 7 insertions(+), 22 deletions(-)
fs/btrfs/extent-tree.c +5 -15
@@ -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)
fs/btrfs/ioctl.c +2 -7
@@ -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);
Signature · 1 line
2.17.1
PATCH 2/2 btrfs: Remove dead code
BTRFS no longer relies on btrfs_subvolume_writers for snapshot exclusion. Just remove any code allocating/freeing it and the structure definition itself.
--- fs/btrfs/ctree.h | 6 ------ fs/btrfs/disk-io.c | 10 ---------- 2 files changed, 16 deletions(-)
fs/btrfs/ctree.h +0 -6
@@ -1169,11 +1169,6 @@ static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)return sb->s_fs_info; } -struct btrfs_subvolume_writers { -struct percpu_counter counter; -wait_queue_head_t wait; -}; -/* * The state of btrfs root */
@@ -1339,7 +1334,6 @@ struct btrfs_root {* manipulation with the read-only status via SUBVOL_SETFLAGS */ int send_in_progress; -struct btrfs_subvolume_writers *subv_writers;atomic_t will_be_snapshotted;atomic_t snapshot_force_cow;
fs/btrfs/disk-io.c +0 -10
@@ -1492,7 +1492,6 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,int btrfs_init_fs_root (struct btrfs_root *root) {int ret; -struct btrfs_subvolume_writers *writers; root->free_ino_ctl =kzalloc (sizeof (*root->free_ino_ctl),GFP_NOFS ); root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
@@ -1502,13 +1501,6 @@ int btrfs_init_fs_root(struct btrfs_root *root) goto fail; } - writers =btrfs_alloc_subvolume_writers (); -if (IS_ERR (writers)) { - ret =PTR_ERR (writers); - goto fail; - } - root->subv_writers = writers; -btrfs_init_free_ino_ctl (root);spin_lock_init (&root->ino_cache_lock);init_waitqueue_head (&root->ino_cache_wait);
@@ -3870,8 +3862,6 @@ void btrfs_free_fs_root(struct btrfs_root *root)WARN_ON (!RB_EMPTY_ROOT (&root->inode_tree));if (root->anon_dev)free_anon_bdev (root->anon_dev); -if (root->subv_writers) -btrfs_free_subvolume_writers (root->subv_writers);free_extent_buffer (root->node);free_extent_buffer (root->commit_root);kfree (root->free_ino_ctl);
Signature · 1 line
2.17.1
PATCH v2 1/1 btrfs: Remove dead code
BTRFS no longer relies on btrfs_subvolume_writers for snapshot exclusion. Just remove any code allocating/freeing it and the structure definition itself.
---
Changes in v2:
* Remove definition of btrfs_alloc_subvolume_writers.
fs/btrfs/ctree.h | 6 ------ fs/btrfs/disk-io.c | 29 ----------------------------- 2 files changed, 35 deletions(-)
fs/btrfs/ctree.h +0 -6
@@ -1169,11 +1169,6 @@ static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)return sb->s_fs_info; } -struct btrfs_subvolume_writers { -struct percpu_counter counter; -wait_queue_head_t wait; -}; -/* * The state of btrfs root */
@@ -1339,7 +1334,6 @@ struct btrfs_root {* manipulation with the read-only status via SUBVOL_SETFLAGS */ int send_in_progress; -struct btrfs_subvolume_writers *subv_writers;atomic_t will_be_snapshotted;atomic_t snapshot_force_cow;
fs/btrfs/disk-io.c +0 -29
@@ -1130,25 +1130,6 @@ void btrfs_clean_tree_block(struct extent_buffer *buf) } } -static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers (void ) -{ -struct btrfs_subvolume_writers *writers; -int ret; - - writers =kmalloc (sizeof (*writers),GFP_NOFS ); -if (!writers) -return ERR_PTR (-ENOMEM ); - - ret =percpu_counter_init (&writers->counter,0 ,GFP_NOFS ); -if (ret <0 ) { -kfree (writers); -return ERR_PTR (ret); - } - -init_waitqueue_head (&writers->wait); -return writers; -} -static void btrfs_free_subvolume_writers (struct btrfs_subvolume_writers *writers) {
@@ -1492,7 +1473,6 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,int btrfs_init_fs_root (struct btrfs_root *root) {int ret; -struct btrfs_subvolume_writers *writers; root->free_ino_ctl =kzalloc (sizeof (*root->free_ino_ctl),GFP_NOFS ); root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
@@ -1502,13 +1482,6 @@ int btrfs_init_fs_root(struct btrfs_root *root) goto fail; } - writers =btrfs_alloc_subvolume_writers (); -if (IS_ERR (writers)) { - ret =PTR_ERR (writers); - goto fail; - } - root->subv_writers = writers; -btrfs_init_free_ino_ctl (root);spin_lock_init (&root->ino_cache_lock);init_waitqueue_head (&root->ino_cache_wait);
@@ -3870,8 +3843,6 @@ void btrfs_free_fs_root(struct btrfs_root *root)WARN_ON (!RB_EMPTY_ROOT (&root->inode_tree));if (root->anon_dev)free_anon_bdev (root->anon_dev); -if (root->subv_writers) -btrfs_free_subvolume_writers (root->subv_writers);free_extent_buffer (root->node);free_extent_buffer (root->commit_root);kfree (root->free_ino_ctl);
Signature · 1 line
2.17.1
PATCH v3 1/1 btrfs: Remove dead code
BTRFS no longer relies on btrfs_subvolume_writers for snapshot exclusion. Just remove any code allocating/freeing it and the structure definition itself.
---
Changes in v3:
* Removed -btrfs_free_subvolume_writers. This is really the final piece of the puzzle....
Changes in v2:
* Remove definition of btrfs_alloc_subvolume_writers.
fs/btrfs/ctree.h | 6 ------ fs/btrfs/disk-io.c | 36 ------------------------------------ 2 files changed, 42 deletions(-)
fs/btrfs/ctree.h +0 -6
@@ -1169,11 +1169,6 @@ static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)return sb->s_fs_info; } -struct btrfs_subvolume_writers { -struct percpu_counter counter; -wait_queue_head_t wait; -}; -/* * The state of btrfs root */
@@ -1339,7 +1334,6 @@ struct btrfs_root {* manipulation with the read-only status via SUBVOL_SETFLAGS */ int send_in_progress; -struct btrfs_subvolume_writers *subv_writers;atomic_t will_be_snapshotted;atomic_t snapshot_force_cow;
fs/btrfs/disk-io.c +0 -36
@@ -1130,32 +1130,6 @@ void btrfs_clean_tree_block(struct extent_buffer *buf) } } -static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers (void ) -{ -struct btrfs_subvolume_writers *writers; -int ret; - - writers =kmalloc (sizeof (*writers),GFP_NOFS ); -if (!writers) -return ERR_PTR (-ENOMEM ); - - ret =percpu_counter_init (&writers->counter,0 ,GFP_NOFS ); -if (ret <0 ) { -kfree (writers); -return ERR_PTR (ret); - } - -init_waitqueue_head (&writers->wait); -return writers; -} - -static void -btrfs_free_subvolume_writers (struct btrfs_subvolume_writers *writers) -{ -percpu_counter_destroy (&writers->counter); -kfree (writers); -} -static void __setup_root (struct btrfs_root *root,struct btrfs_fs_info *fs_info,u64 objectid) {
@@ -1492,7 +1466,6 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,int btrfs_init_fs_root (struct btrfs_root *root) {int ret; -struct btrfs_subvolume_writers *writers; root->free_ino_ctl =kzalloc (sizeof (*root->free_ino_ctl),GFP_NOFS ); root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
@@ -1502,13 +1475,6 @@ int btrfs_init_fs_root(struct btrfs_root *root) goto fail; } - writers =btrfs_alloc_subvolume_writers (); -if (IS_ERR (writers)) { - ret =PTR_ERR (writers); - goto fail; - } - root->subv_writers = writers; -btrfs_init_free_ino_ctl (root);spin_lock_init (&root->ino_cache_lock);init_waitqueue_head (&root->ino_cache_wait);
@@ -3870,8 +3836,6 @@ void btrfs_free_fs_root(struct btrfs_root *root)WARN_ON (!RB_EMPTY_ROOT (&root->inode_tree));if (root->anon_dev)free_anon_bdev (root->anon_dev); -if (root->subv_writers) -btrfs_free_subvolume_writers (root->subv_writers);free_extent_buffer (root->node);free_extent_buffer (root->commit_root);kfree (root->free_ino_ctl);
Signature · 1 line
2.17.1
Quoted 9 lines · Nikolay Borisov
BTRFS sports a mechanism to provide exclusion when a snapshot is about to be created. This is implemented via
btrfs_start_write_no_snapshottinget al. Currently the implementation of that mechanism is some perverse amalgamation of a percpu variable, an explicit waitqueue, anatomic_tvariable and an implicit wait bit on saidatomic_tviawait_var_eventfamily of API. And for good measure there is a memory barrier thrown in the mix...
Quoted 5 lines
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_tused as a boolean flag.
Nop, can't work as a boolean, see below.
Quoted 40 lines
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_snapshottingwill get a ret val of 0 that should be handled accordingly.
btrfs_wait_for_snapshot_creationOTOH 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 <[email protected]>--- fs/btrfs/extent-tree.c | 20 +++++--------------- fs/btrfs/ioctl.c | 9 ++------- 2 files changed, 7 insertions(+), 22 deletions(-)fs/btrfs/extent-tree.c +3
-8@@ -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.
fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) · line 11345
Quoted 10 lines
fs/btrfs/extent-tree.c +2
-7-*/ -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 ); }
So if two writes call btrfs_start_write_no_snapshotting(), we end up with root->will_be_snapshotted == 1.
One task calls btrfs_end_write_no_snapshotting(), it decrements it to 1 - we wake up the snapshot creation task while there's still one nodatacow writer - this is incorrect.
Now the second task calls btrfs_end_write_no_snapshotting(), sees root->will_be_snapshotted == 0, assertion failure.
Quoted 7 lines
fs/btrfs/extent-tree.c +0
-0void btrfs_wait_for_snapshot_creation (struct btrfs_root *root)fs/btrfs/ioctl.c +0
-0
fs/btrfs/ioctl.c static int create_snapshot(struct btrfs_root *root, struct inode *dir, · line 797
Quoted 9 lines
fs/btrfs/ioctl.c +1
-5@@ -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);
This naming is also confusing now. The task that creates a snapshot is calling btrfs_wait_for_snapshot_creation(), waiting for itself?
fs/btrfs/ioctl.c static int create_snapshot(struct btrfs_root *root, struct inode *dir, · line 877
Quoted 10 lines
fs/btrfs/ioctl.c +1
-2ret =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);
Also confusing. We are not ending a write operation, we are ending snapshot creation.
Thanks.
Quoted 6 lines
fs/btrfs/ioctl.c +0
-1free_pending:kfree (pending_snapshot->root_item);btrfs_free_path (pending_snapshot->path); --2.17.1
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”
Quoted 73 lines · Filipe Manana
Quoted 9 lines · Nikolay Borisov
BTRFS sports a mechanism to provide exclusion when a snapshot is about to be created. This is implemented via
btrfs_start_write_no_snapshottinget al. Currently the implementation of that mechanism is some perverse amalgamation of a percpu variable, an explicit waitqueue, anatomic_tvariable and an implicit wait bit on saidatomic_tviawait_var_eventfamily of API. And for good measure there is a memory barrier thrown in the mix...Quoted 5 lines
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_tused as a boolean flag.Nop, can't work as a boolean, see below.
Quoted 40 lines
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_snapshottingwill get a ret val of 0 that should be handled accordingly.
btrfs_wait_for_snapshot_creationOTOH 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 <[email protected]>--- fs/btrfs/extent-tree.c | 20 +++++--------------- fs/btrfs/ioctl.c | 9 ++------- 2 files changed, 7 insertions(+), 22 deletions(-)fs/btrfs/extent-tree.c +3
-8@@ -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. fs/btrfs/extent-tree.c int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) · line 11345
Quoted 10 lines
fs/btrfs/extent-tree.c +2
-7-*/ -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 ); }So if two writes call
btrfs_start_write_no_snapshotting(), we end up withroot->will_be_snapshotted== 1.
Quoted 5 lines
One task calls
btrfs_end_write_no_snapshotting(), it decrements it to 1 - we wake up the snapshot creation task while there's still one nodatacow writer - this is incorrect.
Now the second task callsbtrfs_end_write_no_snapshotting(), seesroot->will_be_snapshotted== 0, assertion failure.
Actually take that out, I ignored the return value of atomic_add_unless().
So this change does not allow for concurrent no snapshot writers anymore, multiple tasks calling btrfs_start_write_no_snapshotting(), only one succeeds and all the others fail,
so that's a regression from what we currently have.
The rest of the confusing names still applies.
Quoted 49 lines
Quoted 8 lines
fs/btrfs/extent-tree.c +0
-0void btrfs_wait_for_snapshot_creation (struct btrfs_root *root)fs/btrfs/ioctl.c +0
-0fs/btrfs/ioctl.c static int create_snapshot(struct btrfs_root *root, struct inode *dir, · line 797
Quoted 9 lines
fs/btrfs/ioctl.c +1
-5@@ -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);This naming is also confusing now. The task that creates a snapshot is calling
btrfs_wait_for_snapshot_creation(), waiting for itself?fs/btrfs/ioctl.c static int create_snapshot(struct btrfs_root *root, struct inode *dir, · line 877
Quoted 10 lines
fs/btrfs/ioctl.c +1
-2ret =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);Also confusing. We are not ending a write operation, we are ending snapshot creation.
Thanks.
Quoted 6 lines
fs/btrfs/ioctl.c +0
-1free_pending:kfree (pending_snapshot->root_item);btrfs_free_path (pending_snapshot->path); --2.17.1
--
Filipe David Manana,“Whether you think you can, or you think you can't — you're right.”
Signature · 3 lines
Filipe David Manana, “Whether you think you can, or you think you can't — you're right.”