btrfs: dev-replace: swich locking to rw semaphore
authorDavid Sterba <dsterba@suse.com>
Wed, 4 Apr 2018 23:29:24 +0000 (01:29 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 17 Dec 2018 13:51:44 +0000 (14:51 +0100)
This is the first part of removing the custom locking and waiting scheme
used for device replace. It was probably copied from extent buffer
locking, but there's nothing that would require more than is provided by
the common locking primitives.

The rw spinlock protects waiting tasks counter in case of incompatible
locks and the waitqueue. Same as rw semaphore.

This patch only switches the locking primitive, for better
bisectability.  There should be no functional change other than the
overhead of the locking and potential sleeping instead of spinning when
the lock is contended.

Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.h
fs/btrfs/dev-replace.c
fs/btrfs/disk-io.c

index 81cbbb24678e3142b1f9fd2ac896a90211815377..b4f97120aecdac72f9e1ee07db906ddb03254efe 100644 (file)
@@ -378,7 +378,7 @@ struct btrfs_dev_replace {
        struct btrfs_device *tgtdev;
 
        struct mutex lock_finishing_cancel_unmount;
-       rwlock_t lock;
+       struct rw_semaphore rwsem;
        atomic_t blocking_readers;
        wait_queue_head_t read_lock_wq;
 
index 3b272ff60fea750b57745b6b4705602d83b5bb97..316a292783060ce351dfd14dba31ced43ce7e089 100644 (file)
@@ -1002,12 +1002,12 @@ int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
 
 void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace)
 {
-       read_lock(&dev_replace->lock);
+       down_read(&dev_replace->rwsem);
 }
 
 void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace)
 {
-       read_unlock(&dev_replace->lock);
+       up_read(&dev_replace->rwsem);
 }
 
 void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
@@ -1015,16 +1015,16 @@ void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
 again:
        wait_event(dev_replace->read_lock_wq,
                   atomic_read(&dev_replace->blocking_readers) == 0);
-       write_lock(&dev_replace->lock);
+       down_write(&dev_replace->rwsem);
        if (atomic_read(&dev_replace->blocking_readers)) {
-               write_unlock(&dev_replace->lock);
+               up_write(&dev_replace->rwsem);
                goto again;
        }
 }
 
 void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace)
 {
-       write_unlock(&dev_replace->lock);
+       up_write(&dev_replace->rwsem);
 }
 
 /* inc blocking cnt and release read lock */
@@ -1033,7 +1033,7 @@ void btrfs_dev_replace_set_lock_blocking(
 {
        /* only set blocking for read lock */
        atomic_inc(&dev_replace->blocking_readers);
-       read_unlock(&dev_replace->lock);
+       up_read(&dev_replace->rwsem);
 }
 
 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
index 14d355d0cb7a439177884f0d88b50831b6a7a6a9..eca66ac52c7a8a76e39b8b7c5ed3074cac451b1a 100644 (file)
@@ -2141,7 +2141,7 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
 static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
 {
        mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
-       rwlock_init(&fs_info->dev_replace.lock);
+       init_rwsem(&fs_info->dev_replace.rwsem);
        atomic_set(&fs_info->dev_replace.blocking_readers, 0);
        init_waitqueue_head(&fs_info->dev_replace.replace_wait);
        init_waitqueue_head(&fs_info->dev_replace.read_lock_wq);