Commit d7439fb1 authored by Jan Kara's avatar Jan Kara Committed by Christian Brauner

fs: Provide helpers for manipulating sb->s_readonly_remount

Provide helpers to set and clear sb->s_readonly_remount including
appropriate memory barriers. Also use this opportunity to document what
the barriers pair with and why they are needed.
Suggested-by: default avatarDave Chinner <david@fromorbit.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Reviewed-by: default avatarDave Chinner <dchinner@redhat.com>
Message-Id: <20230620112832.5158-1-jack@suse.cz>
Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent c541dce8
...@@ -120,6 +120,47 @@ void put_super(struct super_block *sb); ...@@ -120,6 +120,47 @@ void put_super(struct super_block *sb);
extern bool mount_capable(struct fs_context *); extern bool mount_capable(struct fs_context *);
int sb_init_dio_done_wq(struct super_block *sb); int sb_init_dio_done_wq(struct super_block *sb);
/*
* Prepare superblock for changing its read-only state (i.e., either remount
* read-write superblock read-only or vice versa). After this function returns
* mnt_is_readonly() will return true for any mount of the superblock if its
* caller is able to observe any changes done by the remount. This holds until
* sb_end_ro_state_change() is called.
*/
static inline void sb_start_ro_state_change(struct super_block *sb)
{
WRITE_ONCE(sb->s_readonly_remount, 1);
/*
* For RO->RW transition, the barrier pairs with the barrier in
* mnt_is_readonly() making sure if mnt_is_readonly() sees SB_RDONLY
* cleared, it will see s_readonly_remount set.
* For RW->RO transition, the barrier pairs with the barrier in
* __mnt_want_write() before the mnt_is_readonly() check. The barrier
* makes sure if __mnt_want_write() sees MNT_WRITE_HOLD already
* cleared, it will see s_readonly_remount set.
*/
smp_wmb();
}
/*
* Ends section changing read-only state of the superblock. After this function
* returns if mnt_is_readonly() returns false, the caller will be able to
* observe all the changes remount did to the superblock.
*/
static inline void sb_end_ro_state_change(struct super_block *sb)
{
/*
* This barrier provides release semantics that pairs with
* the smp_rmb() acquire semantics in mnt_is_readonly().
* This barrier pair ensure that when mnt_is_readonly() sees
* 0 for sb->s_readonly_remount, it will also see all the
* preceding flag changes that were made during the RO state
* change.
*/
smp_wmb();
WRITE_ONCE(sb->s_readonly_remount, 0);
}
/* /*
* open.c * open.c
*/ */
......
...@@ -309,9 +309,16 @@ static unsigned int mnt_get_writers(struct mount *mnt) ...@@ -309,9 +309,16 @@ static unsigned int mnt_get_writers(struct mount *mnt)
static int mnt_is_readonly(struct vfsmount *mnt) static int mnt_is_readonly(struct vfsmount *mnt)
{ {
if (mnt->mnt_sb->s_readonly_remount) if (READ_ONCE(mnt->mnt_sb->s_readonly_remount))
return 1; return 1;
/* Order wrt setting s_flags/s_readonly_remount in do_remount() */ /*
* The barrier pairs with the barrier in sb_start_ro_state_change()
* making sure if we don't see s_readonly_remount set yet, we also will
* not see any superblock / mount flag changes done by remount.
* It also pairs with the barrier in sb_end_ro_state_change()
* assuring that if we see s_readonly_remount already cleared, we will
* see the values of superblock / mount flags updated by remount.
*/
smp_rmb(); smp_rmb();
return __mnt_is_readonly(mnt); return __mnt_is_readonly(mnt);
} }
...@@ -364,9 +371,11 @@ int __mnt_want_write(struct vfsmount *m) ...@@ -364,9 +371,11 @@ int __mnt_want_write(struct vfsmount *m)
} }
} }
/* /*
* After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will * The barrier pairs with the barrier sb_start_ro_state_change() making
* be set to match its requirements. So we must not load that until * sure that if we see MNT_WRITE_HOLD cleared, we will also see
* MNT_WRITE_HOLD is cleared. * s_readonly_remount set (or even SB_RDONLY / MNT_READONLY flags) in
* mnt_is_readonly() and bail in case we are racing with remount
* read-only.
*/ */
smp_rmb(); smp_rmb();
if (mnt_is_readonly(m)) { if (mnt_is_readonly(m)) {
...@@ -588,10 +597,8 @@ int sb_prepare_remount_readonly(struct super_block *sb) ...@@ -588,10 +597,8 @@ int sb_prepare_remount_readonly(struct super_block *sb)
if (!err && atomic_long_read(&sb->s_remove_count)) if (!err && atomic_long_read(&sb->s_remove_count))
err = -EBUSY; err = -EBUSY;
if (!err) { if (!err)
sb->s_readonly_remount = 1; sb_start_ro_state_change(sb);
smp_wmb();
}
list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
......
...@@ -944,8 +944,7 @@ int reconfigure_super(struct fs_context *fc) ...@@ -944,8 +944,7 @@ int reconfigure_super(struct fs_context *fc)
*/ */
if (remount_ro) { if (remount_ro) {
if (force) { if (force) {
sb->s_readonly_remount = 1; sb_start_ro_state_change(sb);
smp_wmb();
} else { } else {
retval = sb_prepare_remount_readonly(sb); retval = sb_prepare_remount_readonly(sb);
if (retval) if (retval)
...@@ -953,12 +952,10 @@ int reconfigure_super(struct fs_context *fc) ...@@ -953,12 +952,10 @@ int reconfigure_super(struct fs_context *fc)
} }
} else if (remount_rw) { } else if (remount_rw) {
/* /*
* We set s_readonly_remount here to protect filesystem's * Protect filesystem's reconfigure code from writes from
* reconfigure code from writes from userspace until * userspace until reconfigure finishes.
* reconfigure finishes.
*/ */
sb->s_readonly_remount = 1; sb_start_ro_state_change(sb);
smp_wmb();
} }
if (fc->ops->reconfigure) { if (fc->ops->reconfigure) {
...@@ -974,9 +971,7 @@ int reconfigure_super(struct fs_context *fc) ...@@ -974,9 +971,7 @@ int reconfigure_super(struct fs_context *fc)
WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) | WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
(fc->sb_flags & fc->sb_flags_mask))); (fc->sb_flags & fc->sb_flags_mask)));
/* Needs to be ordered wrt mnt_is_readonly() */ sb_end_ro_state_change(sb);
smp_wmb();
sb->s_readonly_remount = 0;
/* /*
* Some filesystems modify their metadata via some other path than the * Some filesystems modify their metadata via some other path than the
...@@ -991,7 +986,7 @@ int reconfigure_super(struct fs_context *fc) ...@@ -991,7 +986,7 @@ int reconfigure_super(struct fs_context *fc)
return 0; return 0;
cancel_readonly: cancel_readonly:
sb->s_readonly_remount = 0; sb_end_ro_state_change(sb);
return retval; return retval;
} }
......
...@@ -1248,7 +1248,7 @@ struct super_block { ...@@ -1248,7 +1248,7 @@ struct super_block {
*/ */
atomic_long_t s_fsnotify_connectors; atomic_long_t s_fsnotify_connectors;
/* Being remounted read-only */ /* Read-only state of the superblock is being changed */
int s_readonly_remount; int s_readonly_remount;
/* per-sb errseq_t for reporting writeback errors via syncfs */ /* per-sb errseq_t for reporting writeback errors via syncfs */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment