Skip to content

Commit bb6b619

Browse files
adam900710kdave
authored andcommitted
btrfs: delay btrfs_open_devices() until super block is created
Currently btrfs always calls btrfs_open_devices() before creating the super block. It's fine for now because: - No blk_holder_ops is provided - btrfs_fs_type is used as a holder This means no matter who wins the device opening race, the holder will be the same thus not affecting the later sget_fc() race. And since no blk_holder_ops is provided, no bdev operation is depending on the holder. But this will no longer be true if we want to implement a proper blk_holder_ops using fs_holder_ops. This means we will need a proper super block as the bdev holder. To prepare for such change: - Add btrfs_fs_devices::holding member This will prevent btrfs_free_stale_devices() and btrfs_close_device() from deleting the fs_devices when there is another process trying to mount the fs. Along with the new member, here comes two helpers, btrfs_fs_devices_inc_holding() and btrfs_fs_devices_dec_holding(). This will allow us to hold an fs_devices without opening it. This is needed because we can not hold uuid_mutex while calling sget_fc(), this will reverse the lock sequence with s_umount, causing a lockdep warning. - Delay btrfs_open_devices() until a super block is returned This means we have to hold the initial fs_devices first, then unlock uuid_mutex, call sget_fc(), then re-lock uuid_mutex, and decrease the holding number. For new super block case, we continue to btrfs_open_devices() with uuid_mutex hold. For existing super block case, we can unlock uuid_mutex and continue. Although this means a more complex error handling path, as if we didn't call btrfs_open_devices() (either got an existing sb, or sget_fc() failed), we can not let btrfs_put_fs_info() to cleanup the fs_devices, as it can be freed at any time after we decrease the hold on fs_devices and unlock uuid_mutex. Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent a4a37b3 commit bb6b619

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

fs/btrfs/super.c

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,6 @@ static int btrfs_get_tree_super(struct fs_context *fc)
18411841
struct btrfs_fs_info *fs_info = fc->s_fs_info;
18421842
struct btrfs_fs_context *ctx = fc->fs_private;
18431843
struct btrfs_fs_devices *fs_devices = NULL;
1844-
struct block_device *bdev;
18451844
struct btrfs_device *device;
18461845
struct super_block *sb;
18471846
blk_mode_t mode = btrfs_open_mode(fc);
@@ -1860,23 +1859,38 @@ static int btrfs_get_tree_super(struct fs_context *fc)
18601859
mutex_unlock(&uuid_mutex);
18611860
return PTR_ERR(device);
18621861
}
1863-
18641862
fs_devices = device->fs_devices;
1863+
/*
1864+
* We can not hold uuid_mutex calling sget_fc(), it will lead to a
1865+
* locking order reversal with s_umount.
1866+
*
1867+
* So here we increase the holding number of fs_devices, this will ensure
1868+
* the fs_devices itself won't be freed.
1869+
*/
1870+
btrfs_fs_devices_inc_holding(fs_devices);
18651871
fs_info->fs_devices = fs_devices;
1866-
1867-
ret = btrfs_open_devices(fs_devices, mode, &btrfs_fs_type);
18681872
mutex_unlock(&uuid_mutex);
1869-
if (ret)
1870-
return ret;
18711873

1872-
if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0)
1873-
return -EACCES;
1874-
1875-
bdev = fs_devices->latest_dev->bdev;
18761874

18771875
sb = sget_fc(fc, btrfs_fc_test_super, set_anon_super_fc);
1878-
if (IS_ERR(sb))
1876+
if (IS_ERR(sb)) {
1877+
mutex_lock(&uuid_mutex);
1878+
btrfs_fs_devices_dec_holding(fs_devices);
1879+
/*
1880+
* Since the fs_devices is not opened, it can be freed at any
1881+
* time after unlocking uuid_mutex.
1882+
* We do not want to double free such dangling fs_devices through
1883+
* put_fs_context()->btrfs_free_fs_info().
1884+
* So here we reset fs_info->fs_devices to NULL, and let the
1885+
* regular fs_devices reclaim path to handle it.
1886+
*
1887+
* This applies to all later branches where no fs_devices is
1888+
* opened.
1889+
*/
1890+
fs_info->fs_devices = NULL;
1891+
mutex_unlock(&uuid_mutex);
18791892
return PTR_ERR(sb);
1893+
}
18801894

18811895
set_device_specific_options(fs_info);
18821896

@@ -1888,26 +1902,44 @@ static int btrfs_get_tree_super(struct fs_context *fc)
18881902
*
18891903
* fc->s_fs_info is not touched and will be later freed by
18901904
* put_fs_context() through btrfs_free_fs_context().
1891-
*
1892-
* And the fs_info->fs_devices will also be closed by
1893-
* btrfs_free_fs_context().
18941905
*/
18951906
ASSERT(fc->s_fs_info == fs_info);
18961907

1908+
mutex_lock(&uuid_mutex);
1909+
btrfs_fs_devices_dec_holding(fs_devices);
1910+
fs_info->fs_devices = NULL;
1911+
mutex_unlock(&uuid_mutex);
18971912
/*
18981913
* At this stage we may have RO flag mismatch between
18991914
* fc->sb_flags and sb->s_flags. Caller should detect such
19001915
* mismatch and reconfigure with sb->s_umount rwsem held if
19011916
* needed.
19021917
*/
19031918
} else {
1919+
struct block_device *bdev;
1920+
19041921
/*
19051922
* The first mount of the fs thus a new superblock, fc->s_fs_info
19061923
* should be NULL, and the owner ship of our fs_info and fs_devices is
19071924
* transferred to the super block.
19081925
*/
19091926
ASSERT(fc->s_fs_info == NULL);
19101927

1928+
mutex_lock(&uuid_mutex);
1929+
btrfs_fs_devices_dec_holding(fs_devices);
1930+
ret = btrfs_open_devices(fs_devices, mode, &btrfs_fs_type);
1931+
if (ret < 0)
1932+
fs_info->fs_devices = NULL;
1933+
mutex_unlock(&uuid_mutex);
1934+
if (ret < 0) {
1935+
deactivate_locked_super(sb);
1936+
return ret;
1937+
}
1938+
if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1939+
deactivate_locked_super(sb);
1940+
return -EACCES;
1941+
}
1942+
bdev = fs_devices->latest_dev->bdev;
19111943
snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
19121944
shrinker_debugfs_rename(sb->s_shrink, "sb-btrfs:%s", sb->s_id);
19131945
btrfs_sb(sb)->bdev_holder = &btrfs_fs_type;

fs/btrfs/volumes.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
413413
struct btrfs_device *device;
414414

415415
WARN_ON(fs_devices->opened);
416+
WARN_ON(fs_devices->holding);
416417
while (!list_empty(&fs_devices->devices)) {
417418
device = list_first_entry(&fs_devices->devices,
418419
struct btrfs_device, dev_list);
@@ -540,7 +541,7 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device
540541
continue;
541542
if (devt && devt != device->devt)
542543
continue;
543-
if (fs_devices->opened) {
544+
if (fs_devices->opened || fs_devices->holding) {
544545
if (devt)
545546
ret = -EBUSY;
546547
break;
@@ -1196,7 +1197,7 @@ void btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
11961197

11971198
mutex_lock(&uuid_mutex);
11981199
close_fs_devices(fs_devices);
1199-
if (!fs_devices->opened) {
1200+
if (!fs_devices->opened && !fs_devices->holding) {
12001201
list_splice_init(&fs_devices->seed_list, &list);
12011202

12021203
/*

fs/btrfs/volumes.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,17 @@ struct btrfs_fs_devices {
422422
/* Count fs-devices opened. */
423423
int opened;
424424

425+
/*
426+
* Counter of the processes that are holding this fs_devices but not
427+
* yet opened.
428+
* This is for mounting handling, as we can only open the fs_devices
429+
* after a super block is created.
430+
* But we can not hold uuid_mutex during sget_fc(), thus we have to
431+
* hold the fs_devices (meaning it can not be released) until a super
432+
* block is returned.
433+
*/
434+
int holding;
435+
425436
/* Set when we find or add a device that doesn't have the nonrot flag set. */
426437
bool rotating;
427438
/* Devices support TRIM/discard commands. */
@@ -854,6 +865,20 @@ static inline void btrfs_warn_unknown_chunk_allocation(enum btrfs_chunk_allocati
854865
WARN_ONCE(1, "unknown allocation policy %d, fallback to regular", pol);
855866
}
856867

868+
static inline void btrfs_fs_devices_inc_holding(struct btrfs_fs_devices *fs_devices)
869+
{
870+
lockdep_assert_held(&uuid_mutex);
871+
ASSERT(fs_devices->holding >= 0);
872+
fs_devices->holding++;
873+
}
874+
875+
static inline void btrfs_fs_devices_dec_holding(struct btrfs_fs_devices *fs_devices)
876+
{
877+
lockdep_assert_held(&uuid_mutex);
878+
ASSERT(fs_devices->holding > 0);
879+
fs_devices->holding--;
880+
}
881+
857882
void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
858883

859884
struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);

0 commit comments

Comments
 (0)