Skip to content

Commit 74dbbdd

Browse files
author
Al Viro
committed
New helper: deactivate_locked_super()
Does equivalent of up_write(&s->s_umount); deactivate_super(s); However, it does not does not unlock it until it's all over. As the result, it's safe to use to dispose of new superblock on ->get_sb() failure exits - nobody will see the sucker until it's all over. Equivalent using up_write/deactivate_super is safe for that purpose if superblock is either safe to use or has NULL ->s_root when we unlock. Normally filesystems take the required precautions, but a) we do have bugs in that area in some of them. b) up_write/deactivate_super sequence is extremely common, so the helper makes sense anyway. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 677c9b2 commit 74dbbdd

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

‎fs/super.c‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,34 @@ void deactivate_super(struct super_block *s)
207207

208208
EXPORT_SYMBOL(deactivate_super);
209209

210+
/**
211+
* deactivate_locked_super - drop an active reference to superblock
212+
* @s: superblock to deactivate
213+
*
214+
* Equivalent of up_write(&s->s_umount); deactivate_super(s);, except that
215+
* it does not unlock it until it's all over. As the result, it's safe to
216+
* use to dispose of new superblock on ->get_sb() failure exits - nobody
217+
* will see the sucker until it's all over. Equivalent using up_write +
218+
* deactivate_super is safe for that purpose only if superblock is either
219+
* safe to use or has NULL ->s_root when we unlock.
220+
*/
221+
void deactivate_locked_super(struct super_block *s)
222+
{
223+
struct file_system_type *fs = s->s_type;
224+
if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
225+
s->s_count -= S_BIAS-1;
226+
spin_unlock(&sb_lock);
227+
vfs_dq_off(s, 0);
228+
fs->kill_sb(s);
229+
put_filesystem(fs);
230+
put_super(s);
231+
} else {
232+
up_write(&s->s_umount);
233+
}
234+
}
235+
236+
EXPORT_SYMBOL(deactivate_locked_super);
237+
210238
/**
211239
* grab_super - acquire an active reference
212240
* @s: reference we are trying to make active
@@ -797,8 +825,7 @@ int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
797825
sb->s_flags = flags;
798826
err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
799827
if (err) {
800-
up_write(&sb->s_umount);
801-
deactivate_super(sb);
828+
deactivate_locked_super(sb);
802829
return err;
803830
}
804831

@@ -854,8 +881,7 @@ int get_sb_bdev(struct file_system_type *fs_type,
854881

855882
if (s->s_root) {
856883
if ((flags ^ s->s_flags) & MS_RDONLY) {
857-
up_write(&s->s_umount);
858-
deactivate_super(s);
884+
deactivate_locked_super(s);
859885
error = -EBUSY;
860886
goto error_bdev;
861887
}
@@ -870,8 +896,7 @@ int get_sb_bdev(struct file_system_type *fs_type,
870896
sb_set_blocksize(s, block_size(bdev));
871897
error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
872898
if (error) {
873-
up_write(&s->s_umount);
874-
deactivate_super(s);
899+
deactivate_locked_super(s);
875900
goto error;
876901
}
877902

@@ -921,8 +946,7 @@ int get_sb_nodev(struct file_system_type *fs_type,
921946

922947
error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
923948
if (error) {
924-
up_write(&s->s_umount);
925-
deactivate_super(s);
949+
deactivate_locked_super(s);
926950
return error;
927951
}
928952
s->s_flags |= MS_ACTIVE;
@@ -952,8 +976,7 @@ int get_sb_single(struct file_system_type *fs_type,
952976
s->s_flags = flags;
953977
error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
954978
if (error) {
955-
up_write(&s->s_umount);
956-
deactivate_super(s);
979+
deactivate_locked_super(s);
957980
return error;
958981
}
959982
s->s_flags |= MS_ACTIVE;
@@ -1006,8 +1029,7 @@ vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void
10061029
return mnt;
10071030
out_sb:
10081031
dput(mnt->mnt_root);
1009-
up_write(&mnt->mnt_sb->s_umount);
1010-
deactivate_super(mnt->mnt_sb);
1032+
deactivate_locked_super(mnt->mnt_sb);
10111033
out_free_secdata:
10121034
free_secdata(secdata);
10131035
out_mnt:

‎include/linux/fs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,7 @@ void kill_block_super(struct super_block *sb);
17751775
void kill_anon_super(struct super_block *sb);
17761776
void kill_litter_super(struct super_block *sb);
17771777
void deactivate_super(struct super_block *sb);
1778+
void deactivate_locked_super(struct super_block *sb);
17781779
int set_anon_super(struct super_block *s, void *data);
17791780
struct super_block *sget(struct file_system_type *type,
17801781
int (*test)(struct super_block *,void *),

0 commit comments

Comments
 (0)