Skip to content

Commit 0ed3359

Browse files
committed
super: use locking helpers
Replace the open-coded {down,up}_{read,write}() calls with simple wrappers. Follow-up patches will benefit from this as well. Reviewed-by: Jan Kara <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent e127b9b commit 0ed3359

File tree

1 file changed

+78
-48
lines changed

1 file changed

+78
-48
lines changed

fs/super.c

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ static char *sb_writers_name[SB_FREEZE_LEVELS] = {
5050
"sb_internal",
5151
};
5252

53+
static inline void super_lock(struct super_block *sb, bool excl)
54+
{
55+
if (excl)
56+
down_write(&sb->s_umount);
57+
else
58+
down_read(&sb->s_umount);
59+
}
60+
61+
static inline void super_unlock(struct super_block *sb, bool excl)
62+
{
63+
if (excl)
64+
up_write(&sb->s_umount);
65+
else
66+
up_read(&sb->s_umount);
67+
}
68+
69+
static inline void super_lock_excl(struct super_block *sb)
70+
{
71+
super_lock(sb, true);
72+
}
73+
74+
static inline void super_lock_shared(struct super_block *sb)
75+
{
76+
super_lock(sb, false);
77+
}
78+
79+
static inline void super_unlock_excl(struct super_block *sb)
80+
{
81+
super_unlock(sb, true);
82+
}
83+
84+
static inline void super_unlock_shared(struct super_block *sb)
85+
{
86+
super_unlock(sb, false);
87+
}
88+
5389
/*
5490
* One thing we have to be careful of with a per-sb shrinker is that we don't
5591
* drop the last active reference to the superblock from within the shrinker.
@@ -110,7 +146,7 @@ static unsigned long super_cache_scan(struct shrinker *shrink,
110146
freed += sb->s_op->free_cached_objects(sb, sc);
111147
}
112148

113-
up_read(&sb->s_umount);
149+
super_unlock_shared(sb);
114150
return freed;
115151
}
116152

@@ -176,7 +212,7 @@ static void destroy_unused_super(struct super_block *s)
176212
{
177213
if (!s)
178214
return;
179-
up_write(&s->s_umount);
215+
super_unlock_excl(s);
180216
list_lru_destroy(&s->s_dentry_lru);
181217
list_lru_destroy(&s->s_inode_lru);
182218
security_sb_free(s);
@@ -340,7 +376,7 @@ void deactivate_locked_super(struct super_block *s)
340376
put_filesystem(fs);
341377
put_super(s);
342378
} else {
343-
up_write(&s->s_umount);
379+
super_unlock_excl(s);
344380
}
345381
}
346382

@@ -357,7 +393,7 @@ EXPORT_SYMBOL(deactivate_locked_super);
357393
void deactivate_super(struct super_block *s)
358394
{
359395
if (!atomic_add_unless(&s->s_active, -1, 1)) {
360-
down_write(&s->s_umount);
396+
super_lock_excl(s);
361397
deactivate_locked_super(s);
362398
}
363399
}
@@ -381,12 +417,12 @@ static int grab_super(struct super_block *s) __releases(sb_lock)
381417
{
382418
s->s_count++;
383419
spin_unlock(&sb_lock);
384-
down_write(&s->s_umount);
420+
super_lock_excl(s);
385421
if ((s->s_flags & SB_BORN) && atomic_inc_not_zero(&s->s_active)) {
386422
put_super(s);
387423
return 1;
388424
}
389-
up_write(&s->s_umount);
425+
super_unlock_excl(s);
390426
put_super(s);
391427
return 0;
392428
}
@@ -414,7 +450,7 @@ bool trylock_super(struct super_block *sb)
414450
if (!hlist_unhashed(&sb->s_instances) &&
415451
sb->s_root && (sb->s_flags & SB_BORN))
416452
return true;
417-
up_read(&sb->s_umount);
453+
super_unlock_shared(sb);
418454
}
419455

420456
return false;
@@ -439,13 +475,13 @@ bool trylock_super(struct super_block *sb)
439475
void retire_super(struct super_block *sb)
440476
{
441477
WARN_ON(!sb->s_bdev);
442-
down_write(&sb->s_umount);
478+
super_lock_excl(sb);
443479
if (sb->s_iflags & SB_I_PERSB_BDI) {
444480
bdi_unregister(sb->s_bdi);
445481
sb->s_iflags &= ~SB_I_PERSB_BDI;
446482
}
447483
sb->s_iflags |= SB_I_RETIRED;
448-
up_write(&sb->s_umount);
484+
super_unlock_excl(sb);
449485
}
450486
EXPORT_SYMBOL(retire_super);
451487

@@ -521,7 +557,7 @@ void generic_shutdown_super(struct super_block *sb)
521557
/* should be initialized for __put_super_and_need_restart() */
522558
hlist_del_init(&sb->s_instances);
523559
spin_unlock(&sb_lock);
524-
up_write(&sb->s_umount);
560+
super_unlock_excl(sb);
525561
if (sb->s_bdi != &noop_backing_dev_info) {
526562
if (sb->s_iflags & SB_I_PERSB_BDI)
527563
bdi_unregister(sb->s_bdi);
@@ -685,15 +721,15 @@ EXPORT_SYMBOL(sget);
685721

686722
void drop_super(struct super_block *sb)
687723
{
688-
up_read(&sb->s_umount);
724+
super_unlock_shared(sb);
689725
put_super(sb);
690726
}
691727

692728
EXPORT_SYMBOL(drop_super);
693729

694730
void drop_super_exclusive(struct super_block *sb)
695731
{
696-
up_write(&sb->s_umount);
732+
super_unlock_excl(sb);
697733
put_super(sb);
698734
}
699735
EXPORT_SYMBOL(drop_super_exclusive);
@@ -739,10 +775,10 @@ void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
739775
sb->s_count++;
740776
spin_unlock(&sb_lock);
741777

742-
down_read(&sb->s_umount);
778+
super_lock_shared(sb);
743779
if (sb->s_root && (sb->s_flags & SB_BORN))
744780
f(sb, arg);
745-
up_read(&sb->s_umount);
781+
super_unlock_shared(sb);
746782

747783
spin_lock(&sb_lock);
748784
if (p)
@@ -773,10 +809,10 @@ void iterate_supers_type(struct file_system_type *type,
773809
sb->s_count++;
774810
spin_unlock(&sb_lock);
775811

776-
down_read(&sb->s_umount);
812+
super_lock_shared(sb);
777813
if (sb->s_root && (sb->s_flags & SB_BORN))
778814
f(sb, arg);
779-
up_read(&sb->s_umount);
815+
super_unlock_shared(sb);
780816

781817
spin_lock(&sb_lock);
782818
if (p)
@@ -813,7 +849,7 @@ struct super_block *get_active_super(struct block_device *bdev)
813849
if (sb->s_bdev == bdev) {
814850
if (!grab_super(sb))
815851
goto restart;
816-
up_write(&sb->s_umount);
852+
super_unlock_excl(sb);
817853
return sb;
818854
}
819855
}
@@ -833,17 +869,11 @@ struct super_block *user_get_super(dev_t dev, bool excl)
833869
if (sb->s_dev == dev) {
834870
sb->s_count++;
835871
spin_unlock(&sb_lock);
836-
if (excl)
837-
down_write(&sb->s_umount);
838-
else
839-
down_read(&sb->s_umount);
872+
super_lock(sb, excl);
840873
/* still alive? */
841874
if (sb->s_root && (sb->s_flags & SB_BORN))
842875
return sb;
843-
if (excl)
844-
up_write(&sb->s_umount);
845-
else
846-
up_read(&sb->s_umount);
876+
super_unlock(sb, excl);
847877
/* nope, got unmounted */
848878
spin_lock(&sb_lock);
849879
__put_super(sb);
@@ -889,9 +919,9 @@ int reconfigure_super(struct fs_context *fc)
889919

890920
if (remount_ro) {
891921
if (!hlist_empty(&sb->s_pins)) {
892-
up_write(&sb->s_umount);
922+
super_unlock_excl(sb);
893923
group_pin_kill(&sb->s_pins);
894-
down_write(&sb->s_umount);
924+
super_lock_excl(sb);
895925
if (!sb->s_root)
896926
return 0;
897927
if (sb->s_writers.frozen != SB_UNFROZEN)
@@ -954,7 +984,7 @@ int reconfigure_super(struct fs_context *fc)
954984

955985
static void do_emergency_remount_callback(struct super_block *sb)
956986
{
957-
down_write(&sb->s_umount);
987+
super_lock_excl(sb);
958988
if (sb->s_root && sb->s_bdev && (sb->s_flags & SB_BORN) &&
959989
!sb_rdonly(sb)) {
960990
struct fs_context *fc;
@@ -967,7 +997,7 @@ static void do_emergency_remount_callback(struct super_block *sb)
967997
put_fs_context(fc);
968998
}
969999
}
970-
up_write(&sb->s_umount);
1000+
super_unlock_excl(sb);
9711001
}
9721002

9731003
static void do_emergency_remount(struct work_struct *work)
@@ -990,12 +1020,12 @@ void emergency_remount(void)
9901020

9911021
static void do_thaw_all_callback(struct super_block *sb)
9921022
{
993-
down_write(&sb->s_umount);
1023+
super_lock_excl(sb);
9941024
if (sb->s_root && sb->s_flags & SB_BORN) {
9951025
emergency_thaw_bdev(sb);
9961026
thaw_super_locked(sb);
9971027
} else {
998-
up_write(&sb->s_umount);
1028+
super_unlock_excl(sb);
9991029
}
10001030
}
10011031

@@ -1182,10 +1212,10 @@ EXPORT_SYMBOL(get_tree_keyed);
11821212
*/
11831213
static bool lock_active_super(struct super_block *sb)
11841214
{
1185-
down_read(&sb->s_umount);
1215+
super_lock_shared(sb);
11861216
if (!sb->s_root ||
11871217
(sb->s_flags & (SB_ACTIVE | SB_BORN)) != (SB_ACTIVE | SB_BORN)) {
1188-
up_read(&sb->s_umount);
1218+
super_unlock_shared(sb);
11891219
return false;
11901220
}
11911221
return true;
@@ -1208,7 +1238,7 @@ static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
12081238
if (sb->s_op->shutdown)
12091239
sb->s_op->shutdown(sb);
12101240

1211-
up_read(&sb->s_umount);
1241+
super_unlock_shared(sb);
12121242
}
12131243

12141244
static void fs_bdev_sync(struct block_device *bdev)
@@ -1220,7 +1250,7 @@ static void fs_bdev_sync(struct block_device *bdev)
12201250
if (!lock_active_super(sb))
12211251
return;
12221252
sync_filesystem(sb);
1223-
up_read(&sb->s_umount);
1253+
super_unlock_shared(sb);
12241254
}
12251255

12261256
const struct blk_holder_ops fs_holder_ops = {
@@ -1342,9 +1372,9 @@ int get_tree_bdev(struct fs_context *fc,
13421372
* bdev_mark_dead()). It is safe because we have active sb
13431373
* reference and SB_BORN is not set yet.
13441374
*/
1345-
up_write(&s->s_umount);
1375+
super_unlock_excl(s);
13461376
error = setup_bdev_super(s, fc->sb_flags, fc);
1347-
down_write(&s->s_umount);
1377+
super_lock_excl(s);
13481378
if (!error)
13491379
error = fill_super(s, fc);
13501380
if (error) {
@@ -1394,9 +1424,9 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,
13941424
* bdev_mark_dead()). It is safe because we have active sb
13951425
* reference and SB_BORN is not set yet.
13961426
*/
1397-
up_write(&s->s_umount);
1427+
super_unlock_excl(s);
13981428
error = setup_bdev_super(s, flags, NULL);
1399-
down_write(&s->s_umount);
1429+
super_lock_excl(s);
14001430
if (!error)
14011431
error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
14021432
if (error) {
@@ -1685,29 +1715,29 @@ int freeze_super(struct super_block *sb)
16851715
int ret;
16861716

16871717
atomic_inc(&sb->s_active);
1688-
down_write(&sb->s_umount);
1718+
super_lock_excl(sb);
16891719
if (sb->s_writers.frozen != SB_UNFROZEN) {
16901720
deactivate_locked_super(sb);
16911721
return -EBUSY;
16921722
}
16931723

16941724
if (!(sb->s_flags & SB_BORN)) {
1695-
up_write(&sb->s_umount);
1725+
super_unlock_excl(sb);
16961726
return 0; /* sic - it's "nothing to do" */
16971727
}
16981728

16991729
if (sb_rdonly(sb)) {
17001730
/* Nothing to do really... */
17011731
sb->s_writers.frozen = SB_FREEZE_COMPLETE;
1702-
up_write(&sb->s_umount);
1732+
super_unlock_excl(sb);
17031733
return 0;
17041734
}
17051735

17061736
sb->s_writers.frozen = SB_FREEZE_WRITE;
17071737
/* Release s_umount to preserve sb_start_write -> s_umount ordering */
1708-
up_write(&sb->s_umount);
1738+
super_unlock_excl(sb);
17091739
sb_wait_write(sb, SB_FREEZE_WRITE);
1710-
down_write(&sb->s_umount);
1740+
super_lock_excl(sb);
17111741

17121742
/* Now we go and block page faults... */
17131743
sb->s_writers.frozen = SB_FREEZE_PAGEFAULT;
@@ -1743,7 +1773,7 @@ int freeze_super(struct super_block *sb)
17431773
*/
17441774
sb->s_writers.frozen = SB_FREEZE_COMPLETE;
17451775
lockdep_sb_freeze_release(sb);
1746-
up_write(&sb->s_umount);
1776+
super_unlock_excl(sb);
17471777
return 0;
17481778
}
17491779
EXPORT_SYMBOL(freeze_super);
@@ -1753,7 +1783,7 @@ static int thaw_super_locked(struct super_block *sb)
17531783
int error;
17541784

17551785
if (sb->s_writers.frozen != SB_FREEZE_COMPLETE) {
1756-
up_write(&sb->s_umount);
1786+
super_unlock_excl(sb);
17571787
return -EINVAL;
17581788
}
17591789

@@ -1770,7 +1800,7 @@ static int thaw_super_locked(struct super_block *sb)
17701800
printk(KERN_ERR
17711801
"VFS:Filesystem thaw failed\n");
17721802
lockdep_sb_freeze_release(sb);
1773-
up_write(&sb->s_umount);
1803+
super_unlock_excl(sb);
17741804
return error;
17751805
}
17761806
}
@@ -1790,7 +1820,7 @@ static int thaw_super_locked(struct super_block *sb)
17901820
*/
17911821
int thaw_super(struct super_block *sb)
17921822
{
1793-
down_write(&sb->s_umount);
1823+
super_lock_excl(sb);
17941824
return thaw_super_locked(sb);
17951825
}
17961826
EXPORT_SYMBOL(thaw_super);

0 commit comments

Comments
 (0)