Skip to content

Commit 73fd0db

Browse files
committed
Merge patch series "fs: introduce super write guard"
Christian Brauner <brauner@kernel.org> says: I'm in the process of adding a few more guards for vfs constructs. I've chosen the easy case of super_start_write() and super_end_write() and converted eligible callers. I think long-term we can move a lot of the manual placement to completely rely on guards - where sensible. * patches from https://patch.msgid.link/20251104-work-guards-v1-0-5108ac78a171@kernel.org: xfs: use super write guard in xfs_file_ioctl() open: use super write guard in do_ftruncate() btrfs: use super write guard in relocating_repair_kthread() ext4: use super write guard in write_mmp_block() btrfs: use super write guard in sb_start_write() btrfs: use super write guard btrfs_run_defrag_inode() btrfs: use super write guard in btrfs_reclaim_bgs_work() fs: add super_write_guard Link: https://patch.msgid.link/20251104-work-guards-v1-0-5108ac78a171@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 5b8ed52 + ab5f296 commit 73fd0db

7 files changed

Lines changed: 22 additions & 32 deletions

File tree

fs/btrfs/block-group.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,20 +1850,17 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
18501850
if (!btrfs_should_reclaim(fs_info))
18511851
return;
18521852

1853-
sb_start_write(fs_info->sb);
1853+
guard(super_write)(fs_info->sb);
18541854

1855-
if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
1856-
sb_end_write(fs_info->sb);
1855+
if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
18571856
return;
1858-
}
18591857

18601858
/*
18611859
* Long running balances can keep us blocked here for eternity, so
18621860
* simply skip reclaim if we're unable to get the mutex.
18631861
*/
18641862
if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
18651863
btrfs_exclop_finish(fs_info);
1866-
sb_end_write(fs_info->sb);
18671864
return;
18681865
}
18691866

@@ -1947,7 +1944,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
19471944
/*
19481945
* Get out fast, in case we're read-only or unmounting the
19491946
* filesystem. It is OK to drop block groups from the list even
1950-
* for the read-only case. As we did sb_start_write(),
1947+
* for the read-only case. As we did take the super write lock,
19511948
* "mount -o remount,ro" won't happen and read-only filesystem
19521949
* means it is forced read-only due to a fatal error. So, it
19531950
* never gets back to read-write to let us reclaim again.
@@ -2030,7 +2027,6 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
20302027
list_splice_tail(&retry_list, &fs_info->reclaim_bgs);
20312028
spin_unlock(&fs_info->unused_bgs_lock);
20322029
btrfs_exclop_finish(fs_info);
2033-
sb_end_write(fs_info->sb);
20342030
}
20352031

20362032
void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info)

fs/btrfs/defrag.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,9 @@ static int btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
254254
range.extent_thresh = defrag->extent_thresh;
255255
file_ra_state_init(ra, inode->vfs_inode.i_mapping);
256256

257-
sb_start_write(fs_info->sb);
258-
ret = btrfs_defrag_file(inode, ra, &range, defrag->transid,
259-
BTRFS_DEFRAG_BATCH);
260-
sb_end_write(fs_info->sb);
257+
scoped_guard(super_write, fs_info->sb)
258+
ret = btrfs_defrag_file(inode, ra, &range,
259+
defrag->transid, BTRFS_DEFRAG_BATCH);
261260
iput(&inode->vfs_inode);
262261

263262
if (ret < 0)

fs/btrfs/volumes.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4660,12 +4660,12 @@ static int balance_kthread(void *data)
46604660
struct btrfs_fs_info *fs_info = data;
46614661
int ret = 0;
46624662

4663-
sb_start_write(fs_info->sb);
4663+
guard(super_write)(fs_info->sb);
4664+
46644665
mutex_lock(&fs_info->balance_mutex);
46654666
if (fs_info->balance_ctl)
46664667
ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
46674668
mutex_unlock(&fs_info->balance_mutex);
4668-
sb_end_write(fs_info->sb);
46694669

46704670
return ret;
46714671
}
@@ -8177,12 +8177,12 @@ static int relocating_repair_kthread(void *data)
81778177
target = cache->start;
81788178
btrfs_put_block_group(cache);
81798179

8180-
sb_start_write(fs_info->sb);
8180+
guard(super_write)(fs_info->sb);
8181+
81818182
if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
81828183
btrfs_info(fs_info,
81838184
"zoned: skip relocating block group %llu to repair: EBUSY",
81848185
target);
8185-
sb_end_write(fs_info->sb);
81868186
return -EBUSY;
81878187
}
81888188

@@ -8210,7 +8210,6 @@ static int relocating_repair_kthread(void *data)
82108210
btrfs_put_block_group(cache);
82118211
mutex_unlock(&fs_info->reclaim_bgs_lock);
82128212
btrfs_exclop_finish(fs_info);
8213-
sb_end_write(fs_info->sb);
82148213

82158214
return ret;
82168215
}

fs/ext4/mmp.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,12 @@ static int write_mmp_block_thawed(struct super_block *sb,
5757

5858
static int write_mmp_block(struct super_block *sb, struct buffer_head *bh)
5959
{
60-
int err;
61-
6260
/*
6361
* We protect against freezing so that we don't create dirty buffers
6462
* on frozen filesystem.
6563
*/
66-
sb_start_write(sb);
67-
err = write_mmp_block_thawed(sb, bh);
68-
sb_end_write(sb);
69-
return err;
64+
scoped_guard(super_write, sb)
65+
return write_mmp_block_thawed(sb, bh);
7066
}
7167

7268
/*

fs/open.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,9 @@ int do_ftruncate(struct file *file, loff_t length, int small)
191191
if (error)
192192
return error;
193193

194-
sb_start_write(inode->i_sb);
195-
error = do_truncate(file_mnt_idmap(file), dentry, length,
196-
ATTR_MTIME | ATTR_CTIME, file);
197-
sb_end_write(inode->i_sb);
198-
199-
return error;
194+
scoped_guard(super_write, inode->i_sb)
195+
return do_truncate(file_mnt_idmap(file), dentry, length,
196+
ATTR_MTIME | ATTR_CTIME, file);
200197
}
201198

202199
int do_sys_ftruncate(unsigned int fd, loff_t length, int small)

fs/xfs/xfs_ioctl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,10 +1408,8 @@ xfs_file_ioctl(
14081408

14091409
trace_xfs_ioc_free_eofblocks(mp, &icw, _RET_IP_);
14101410

1411-
sb_start_write(mp->m_super);
1412-
error = xfs_blockgc_free_space(mp, &icw);
1413-
sb_end_write(mp->m_super);
1414-
return error;
1411+
guard(super_write)(mp->m_super);
1412+
return xfs_blockgc_free_space(mp, &icw);
14151413
}
14161414

14171415
case XFS_IOC_EXCHANGE_RANGE:

include/linux/fs/super.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ static inline void sb_start_write(struct super_block *sb)
125125
__sb_start_write(sb, SB_FREEZE_WRITE);
126126
}
127127

128+
DEFINE_GUARD(super_write,
129+
struct super_block *,
130+
sb_start_write(_T),
131+
sb_end_write(_T))
132+
128133
static inline bool sb_start_write_trylock(struct super_block *sb)
129134
{
130135
return __sb_start_write_trylock(sb, SB_FREEZE_WRITE);

0 commit comments

Comments
 (0)