Skip to content

Commit 32ed505

Browse files
committed
ext4: Replace lock/unlock_super() with an explicit lock for resizing
Use a separate lock to protect s_groups_count and the other block group descriptors which get changed via an on-line resize operation, so we can stop overloading the use of lock_super(). Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 3b9d4ed commit 32ed505

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

‎fs/ext4/ext4_sb.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct ext4_sb_info {
7272
struct journal_s *s_journal;
7373
struct list_head s_orphan;
7474
struct mutex s_orphan_lock;
75+
struct mutex s_resize_lock;
7576
unsigned long s_commit_interval;
7677
u32 s_max_batch_time;
7778
u32 s_min_batch_time;

‎fs/ext4/resize.c‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static int setup_new_group_blocks(struct super_block *sb,
193193
if (IS_ERR(handle))
194194
return PTR_ERR(handle);
195195

196-
lock_super(sb);
196+
mutex_lock(&sbi->s_resize_lock);
197197
if (input->group != sbi->s_groups_count) {
198198
err = -EBUSY;
199199
goto exit_journal;
@@ -302,7 +302,7 @@ static int setup_new_group_blocks(struct super_block *sb,
302302
brelse(bh);
303303

304304
exit_journal:
305-
unlock_super(sb);
305+
mutex_unlock(&sbi->s_resize_lock);
306306
if ((err2 = ext4_journal_stop(handle)) && !err)
307307
err = err2;
308308

@@ -643,11 +643,12 @@ static int reserve_backup_gdb(handle_t *handle, struct inode *inode,
643643
* important part is that the new block and inode counts are in the backup
644644
* superblocks, and the location of the new group metadata in the GDT backups.
645645
*
646-
* We do not need lock_super() for this, because these blocks are not
647-
* otherwise touched by the filesystem code when it is mounted. We don't
648-
* need to worry about last changing from sbi->s_groups_count, because the
649-
* worst that can happen is that we do not copy the full number of backups
650-
* at this time. The resize which changed s_groups_count will backup again.
646+
* We do not need take the s_resize_lock for this, because these
647+
* blocks are not otherwise touched by the filesystem code when it is
648+
* mounted. We don't need to worry about last changing from
649+
* sbi->s_groups_count, because the worst that can happen is that we
650+
* do not copy the full number of backups at this time. The resize
651+
* which changed s_groups_count will backup again.
651652
*/
652653
static void update_backups(struct super_block *sb,
653654
int blk_off, char *data, int size)
@@ -809,7 +810,7 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
809810
goto exit_put;
810811
}
811812

812-
lock_super(sb);
813+
mutex_lock(&sbi->s_resize_lock);
813814
if (input->group != sbi->s_groups_count) {
814815
ext4_warning(sb, __func__,
815816
"multiple resizers run on filesystem!");
@@ -840,7 +841,7 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
840841
/*
841842
* OK, now we've set up the new group. Time to make it active.
842843
*
843-
* Current kernels don't lock all allocations via lock_super(),
844+
* We do not lock all allocations via s_resize_lock
844845
* so we have to be safe wrt. concurrent accesses the group
845846
* data. So we need to be careful to set all of the relevant
846847
* group descriptor data etc. *before* we enable the group.
@@ -900,12 +901,12 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
900901
*
901902
* The precise rules we use are:
902903
*
903-
* * Writers of s_groups_count *must* hold lock_super
904+
* * Writers of s_groups_count *must* hold s_resize_lock
904905
* AND
905906
* * Writers must perform a smp_wmb() after updating all dependent
906907
* data and before modifying the groups count
907908
*
908-
* * Readers must hold lock_super() over the access
909+
* * Readers must hold s_resize_lock over the access
909910
* OR
910911
* * Readers must perform an smp_rmb() after reading the groups count
911912
* and before reading any dependent data.
@@ -948,7 +949,7 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
948949
sb->s_dirt = 1;
949950

950951
exit_journal:
951-
unlock_super(sb);
952+
mutex_unlock(&sbi->s_resize_lock);
952953
if ((err2 = ext4_journal_stop(handle)) && !err)
953954
err = err2;
954955
if (!err) {
@@ -986,7 +987,7 @@ int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
986987

987988
/* We don't need to worry about locking wrt other resizers just
988989
* yet: we're going to revalidate es->s_blocks_count after
989-
* taking lock_super() below. */
990+
* taking the s_resize_lock below. */
990991
o_blocks_count = ext4_blocks_count(es);
991992
o_groups_count = EXT4_SB(sb)->s_groups_count;
992993

@@ -1056,11 +1057,11 @@ int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
10561057
goto exit_put;
10571058
}
10581059

1059-
lock_super(sb);
1060+
mutex_lock(&EXT4_SB(sb)->s_resize_lock);
10601061
if (o_blocks_count != ext4_blocks_count(es)) {
10611062
ext4_warning(sb, __func__,
10621063
"multiple resizers run on filesystem!");
1063-
unlock_super(sb);
1064+
mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
10641065
ext4_journal_stop(handle);
10651066
err = -EBUSY;
10661067
goto exit_put;
@@ -1070,14 +1071,14 @@ int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
10701071
EXT4_SB(sb)->s_sbh))) {
10711072
ext4_warning(sb, __func__,
10721073
"error %d on journal write access", err);
1073-
unlock_super(sb);
1074+
mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
10741075
ext4_journal_stop(handle);
10751076
goto exit_put;
10761077
}
10771078
ext4_blocks_count_set(es, o_blocks_count + add);
10781079
ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
10791080
sb->s_dirt = 1;
1080-
unlock_super(sb);
1081+
mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
10811082
ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
10821083
o_blocks_count + add);
10831084
/* We add the blocks to the bitmap and set the group need init bit */

‎fs/ext4/super.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
26462646
#endif
26472647
INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
26482648
mutex_init(&sbi->s_orphan_lock);
2649+
mutex_init(&sbi->s_resize_lock);
26492650

26502651
sb->s_root = NULL;
26512652

0 commit comments

Comments
 (0)