ext4: simplify device handling
[linux-2.6-block.git] / fs / super.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/super.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * super.c contains code to handle: - mount structures
8 * - super-block tables
9 * - filesystem drivers list
10 * - mount system call
11 * - umount system call
12 * - ustat system call
13 *
14 * GK 2/5/95 - Changed to support mounting the root fs via NFS
15 *
16 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
17 * Added change_root: Werner Almesberger & Hans Lermen, Feb '96
18 * Added options to /proc/mounts:
96de0e25 19 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
1da177e4
LT
20 * Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
21 * Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
22 */
23
630d9c47 24#include <linux/export.h>
1da177e4 25#include <linux/slab.h>
1da177e4 26#include <linux/blkdev.h>
1da177e4
LT
27#include <linux/mount.h>
28#include <linux/security.h>
1da177e4
LT
29#include <linux/writeback.h> /* for the emergency remount stuff */
30#include <linux/idr.h>
353ab6e9 31#include <linux/mutex.h>
5477d0fa 32#include <linux/backing-dev.h>
ceb5bdc2 33#include <linux/rculist_bl.h>
22d94f49 34#include <linux/fscrypt.h>
40401530 35#include <linux/fsnotify.h>
5accdf82 36#include <linux/lockdep.h>
6e4eab57 37#include <linux/user_namespace.h>
9bc61ab1 38#include <linux/fs_context.h>
e262e32d 39#include <uapi/linux/mount.h>
6d59e7f5 40#include "internal.h"
1da177e4 41
880b9577 42static int thaw_super_locked(struct super_block *sb, enum freeze_holder who);
1da177e4 43
15d0f5ea
AV
44static LIST_HEAD(super_blocks);
45static DEFINE_SPINLOCK(sb_lock);
1da177e4 46
5accdf82
JK
47static char *sb_writers_name[SB_FREEZE_LEVELS] = {
48 "sb_writers",
49 "sb_pagefaults",
50 "sb_internal",
51};
52
5e874914 53static inline void __super_lock(struct super_block *sb, bool excl)
0ed33598
CB
54{
55 if (excl)
56 down_write(&sb->s_umount);
57 else
58 down_read(&sb->s_umount);
59}
60
61static 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
5e874914 69static inline void __super_lock_excl(struct super_block *sb)
0ed33598 70{
5e874914 71 __super_lock(sb, true);
0ed33598
CB
72}
73
74static inline void super_unlock_excl(struct super_block *sb)
75{
76 super_unlock(sb, true);
77}
78
79static inline void super_unlock_shared(struct super_block *sb)
80{
81 super_unlock(sb, false);
82}
83
5e874914
CB
84static inline bool wait_born(struct super_block *sb)
85{
86 unsigned int flags;
87
88 /*
89 * Pairs with smp_store_release() in super_wake() and ensures
90 * that we see SB_BORN or SB_DYING after we're woken.
91 */
92 flags = smp_load_acquire(&sb->s_flags);
93 return flags & (SB_BORN | SB_DYING);
94}
95
96/**
97 * super_lock - wait for superblock to become ready and lock it
98 * @sb: superblock to wait for
99 * @excl: whether exclusive access is required
100 *
101 * If the superblock has neither passed through vfs_get_tree() or
102 * generic_shutdown_super() yet wait for it to happen. Either superblock
103 * creation will succeed and SB_BORN is set by vfs_get_tree() or we're
104 * woken and we'll see SB_DYING.
105 *
106 * The caller must have acquired a temporary reference on @sb->s_count.
107 *
f0cd9880
CB
108 * Return: The function returns true if SB_BORN was set and with
109 * s_umount held. The function returns false if SB_DYING was
110 * set and without s_umount held.
5e874914
CB
111 */
112static __must_check bool super_lock(struct super_block *sb, bool excl)
113{
114
115 lockdep_assert_not_held(&sb->s_umount);
116
117relock:
118 __super_lock(sb, excl);
119
120 /*
121 * Has gone through generic_shutdown_super() in the meantime.
122 * @sb->s_root is NULL and @sb->s_active is 0. No one needs to
123 * grab a reference to this. Tell them so.
124 */
f0cd9880
CB
125 if (sb->s_flags & SB_DYING) {
126 super_unlock(sb, excl);
5e874914 127 return false;
f0cd9880 128 }
5e874914
CB
129
130 /* Has called ->get_tree() successfully. */
131 if (sb->s_flags & SB_BORN)
132 return true;
133
134 super_unlock(sb, excl);
135
136 /* wait until the superblock is ready or dying */
137 wait_var_event(&sb->s_flags, wait_born(sb));
138
139 /*
140 * Neither SB_BORN nor SB_DYING are ever unset so we never loop.
141 * Just reacquire @sb->s_umount for the caller.
142 */
143 goto relock;
144}
145
f0cd9880 146/* wait and try to acquire read-side of @sb->s_umount */
5e874914
CB
147static inline bool super_lock_shared(struct super_block *sb)
148{
149 return super_lock(sb, false);
150}
151
f0cd9880 152/* wait and try to acquire write-side of @sb->s_umount */
5e874914
CB
153static inline bool super_lock_excl(struct super_block *sb)
154{
155 return super_lock(sb, true);
156}
157
158/* wake waiters */
2c18a63b 159#define SUPER_WAKE_FLAGS (SB_BORN | SB_DYING | SB_DEAD)
5e874914
CB
160static void super_wake(struct super_block *sb, unsigned int flag)
161{
162 WARN_ON_ONCE((flag & ~SUPER_WAKE_FLAGS));
163 WARN_ON_ONCE(hweight32(flag & SUPER_WAKE_FLAGS) > 1);
164
165 /*
166 * Pairs with smp_load_acquire() in super_lock() to make sure
167 * all initializations in the superblock are seen by the user
168 * seeing SB_BORN sent.
169 */
170 smp_store_release(&sb->s_flags, sb->s_flags | flag);
171 /*
172 * Pairs with the barrier in prepare_to_wait_event() to make sure
173 * ___wait_var_event() either sees SB_BORN set or
174 * waitqueue_active() check in wake_up_var() sees the waiter.
175 */
176 smp_mb();
177 wake_up_var(&sb->s_flags);
178}
179
b0d40c92
DC
180/*
181 * One thing we have to be careful of with a per-sb shrinker is that we don't
182 * drop the last active reference to the superblock from within the shrinker.
183 * If that happens we could trigger unregistering the shrinker from within the
8a0e8bb1 184 * shrinker path and that leads to deadlock on the shrinker_mutex. Hence we
b0d40c92
DC
185 * take a passive reference to the superblock to avoid this from occurring.
186 */
0a234c6d
DC
187static unsigned long super_cache_scan(struct shrinker *shrink,
188 struct shrink_control *sc)
b0d40c92
DC
189{
190 struct super_block *sb;
0a234c6d
DC
191 long fs_objects = 0;
192 long total_objects;
193 long freed = 0;
194 long dentries;
195 long inodes;
b0d40c92 196
1720f5dd 197 sb = shrink->private_data;
b0d40c92
DC
198
199 /*
200 * Deadlock avoidance. We may hold various FS locks, and we don't want
201 * to recurse into the FS that called us in clear_inode() and friends..
202 */
0a234c6d
DC
203 if (!(sc->gfp_mask & __GFP_FS))
204 return SHRINK_STOP;
b0d40c92 205
d8ce82ef 206 if (!super_trylock_shared(sb))
0a234c6d 207 return SHRINK_STOP;
b0d40c92 208
d0407903 209 if (sb->s_op->nr_cached_objects)
4101b624 210 fs_objects = sb->s_op->nr_cached_objects(sb, sc);
0e1fdafd 211
503c358c
VD
212 inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
213 dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc);
f6041567 214 total_objects = dentries + inodes + fs_objects + 1;
475d0db7
TH
215 if (!total_objects)
216 total_objects = 1;
0e1fdafd 217
0a234c6d 218 /* proportion the scan between the caches */
f6041567 219 dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
bc3b14cb 220 inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
503c358c 221 fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
b0d40c92 222
0a234c6d
DC
223 /*
224 * prune the dcache first as the icache is pinned by it, then
225 * prune the icache, followed by the filesystem specific caches
49e7e7ff
VD
226 *
227 * Ensure that we always scan at least one object - memcg kmem
228 * accounting uses this to fully empty the caches.
0a234c6d 229 */
49e7e7ff 230 sc->nr_to_scan = dentries + 1;
503c358c 231 freed = prune_dcache_sb(sb, sc);
49e7e7ff 232 sc->nr_to_scan = inodes + 1;
503c358c 233 freed += prune_icache_sb(sb, sc);
0a234c6d
DC
234
235 if (fs_objects) {
49e7e7ff 236 sc->nr_to_scan = fs_objects + 1;
4101b624 237 freed += sb->s_op->free_cached_objects(sb, sc);
b0d40c92
DC
238 }
239
0ed33598 240 super_unlock_shared(sb);
0a234c6d
DC
241 return freed;
242}
243
244static unsigned long super_cache_count(struct shrinker *shrink,
245 struct shrink_control *sc)
246{
247 struct super_block *sb;
248 long total_objects = 0;
249
1720f5dd 250 sb = shrink->private_data;
0a234c6d 251
d23da150 252 /*
d8ce82ef
CB
253 * We don't call super_trylock_shared() here as it is a scalability
254 * bottleneck, so we're exposed to partial setup state. The shrinker
255 * rwsem does not protect filesystem operations backing
256 * list_lru_shrink_count() or s_op->nr_cached_objects(). Counts can
257 * change between super_cache_count and super_cache_scan, so we really
258 * don't need locks here.
79f546a6
DC
259 *
260 * However, if we are currently mounting the superblock, the underlying
261 * filesystem might be in a state of partial construction and hence it
d8ce82ef
CB
262 * is dangerous to access it. super_trylock_shared() uses a SB_BORN check
263 * to avoid this situation, so do the same here. The memory barrier is
79f546a6 264 * matched with the one in mount_fs() as we don't hold locks here.
d23da150 265 */
79f546a6
DC
266 if (!(sb->s_flags & SB_BORN))
267 return 0;
268 smp_rmb();
269
0a234c6d 270 if (sb->s_op && sb->s_op->nr_cached_objects)
4101b624 271 total_objects = sb->s_op->nr_cached_objects(sb, sc);
0a234c6d 272
503c358c
VD
273 total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
274 total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
0a234c6d 275
9b996468
KT
276 if (!total_objects)
277 return SHRINK_EMPTY;
278
55f841ce 279 total_objects = vfs_pressure_ratio(total_objects);
0e1fdafd 280 return total_objects;
b0d40c92
DC
281}
282
853b39a7
ON
283static void destroy_super_work(struct work_struct *work)
284{
285 struct super_block *s = container_of(work, struct super_block,
286 destroy_work);
287 int i;
288
289 for (i = 0; i < SB_FREEZE_LEVELS; i++)
8129ed29 290 percpu_free_rwsem(&s->s_writers.rw_sem[i]);
853b39a7
ON
291 kfree(s);
292}
293
294static void destroy_super_rcu(struct rcu_head *head)
295{
296 struct super_block *s = container_of(head, struct super_block, rcu);
297 INIT_WORK(&s->destroy_work, destroy_super_work);
298 schedule_work(&s->destroy_work);
299}
300
0200894d
AV
301/* Free a superblock that has never been seen by anyone */
302static void destroy_unused_super(struct super_block *s)
5accdf82 303{
0200894d
AV
304 if (!s)
305 return;
0ed33598 306 super_unlock_excl(s);
7eb5e882
AV
307 list_lru_destroy(&s->s_dentry_lru);
308 list_lru_destroy(&s->s_inode_lru);
7eb5e882 309 security_sb_free(s);
6e4eab57 310 put_user_ns(s->s_user_ns);
7eb5e882 311 kfree(s->s_subtype);
1720f5dd 312 shrinker_free(s->s_shrink);
0200894d
AV
313 /* no delays needed */
314 destroy_super_work(&s->destroy_work);
5accdf82
JK
315}
316
1da177e4
LT
317/**
318 * alloc_super - create new superblock
fe2bbc48 319 * @type: filesystem type superblock should belong to
9249e17f 320 * @flags: the mount flags
6e4eab57 321 * @user_ns: User namespace for the super_block
1da177e4
LT
322 *
323 * Allocates and initializes a new &struct super_block. alloc_super()
324 * returns a pointer new superblock or %NULL if allocation had failed.
325 */
6e4eab57
EB
326static struct super_block *alloc_super(struct file_system_type *type, int flags,
327 struct user_namespace *user_ns)
1da177e4 328{
11b0b5ab 329 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
b87221de 330 static const struct super_operations default_op;
7eb5e882
AV
331 int i;
332
333 if (!s)
334 return NULL;
1da177e4 335
b5bd856a 336 INIT_LIST_HEAD(&s->s_mounts);
6e4eab57 337 s->s_user_ns = get_user_ns(user_ns);
ca0168e8
AV
338 init_rwsem(&s->s_umount);
339 lockdep_set_class(&s->s_umount, &type->s_umount_key);
340 /*
341 * sget() can have s_umount recursion.
342 *
343 * When it cannot find a suitable sb, it allocates a new
344 * one (this one), and tries again to find a suitable old
345 * one.
346 *
347 * In case that succeeds, it will acquire the s_umount
348 * lock of the old one. Since these are clearly distrinct
349 * locks, and this object isn't exposed yet, there's no
350 * risk of deadlocks.
351 *
352 * Annotate this by putting this lock in a different
353 * subclass.
354 */
355 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
b5bd856a 356
7eb5e882
AV
357 if (security_sb_alloc(s))
358 goto fail;
7b7a8665 359
7eb5e882 360 for (i = 0; i < SB_FREEZE_LEVELS; i++) {
8129ed29
ON
361 if (__percpu_init_rwsem(&s->s_writers.rw_sem[i],
362 sb_writers_name[i],
363 &type->s_writers_key[i]))
7eb5e882 364 goto fail;
1da177e4 365 }
df0ce26c 366 s->s_bdi = &noop_backing_dev_info;
7eb5e882 367 s->s_flags = flags;
cc50a07a 368 if (s->s_user_ns != &init_user_ns)
67690f93 369 s->s_iflags |= SB_I_NODEV;
7eb5e882 370 INIT_HLIST_NODE(&s->s_instances);
f1ee6162 371 INIT_HLIST_BL_HEAD(&s->s_roots);
e97fedb9 372 mutex_init(&s->s_sync_lock);
7eb5e882 373 INIT_LIST_HEAD(&s->s_inodes);
74278da9 374 spin_lock_init(&s->s_inode_list_lock);
6c60d2b5
DC
375 INIT_LIST_HEAD(&s->s_inodes_wb);
376 spin_lock_init(&s->s_inode_wblist_lock);
7eb5e882 377
7eb5e882
AV
378 s->s_count = 1;
379 atomic_set(&s->s_active, 1);
380 mutex_init(&s->s_vfs_rename_mutex);
381 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
bc8230ee 382 init_rwsem(&s->s_dquot.dqio_sem);
7eb5e882
AV
383 s->s_maxbytes = MAX_NON_LFS;
384 s->s_op = &default_op;
385 s->s_time_gran = 1000000000;
188d20bc
DD
386 s->s_time_min = TIME64_MIN;
387 s->s_time_max = TIME64_MAX;
7eb5e882 388
1720f5dd
QZ
389 s->s_shrink = shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
390 "sb-%s", type->name);
391 if (!s->s_shrink)
8e04944f 392 goto fail;
1720f5dd
QZ
393
394 s->s_shrink->scan_objects = super_cache_scan;
395 s->s_shrink->count_objects = super_cache_count;
396 s->s_shrink->batch = 1024;
397 s->s_shrink->private_data = s;
398
399 if (list_lru_init_memcg(&s->s_dentry_lru, s->s_shrink))
2b3648a6 400 goto fail;
1720f5dd 401 if (list_lru_init_memcg(&s->s_inode_lru, s->s_shrink))
2b3648a6 402 goto fail;
1da177e4 403 return s;
5ca302c8 404
7eb5e882 405fail:
0200894d 406 destroy_unused_super(s);
7eb5e882 407 return NULL;
1da177e4
LT
408}
409
410/* Superblock refcounting */
411
412/*
35cf7ba0 413 * Drop a superblock's refcount. The caller must hold sb_lock.
1da177e4 414 */
c645b930 415static void __put_super(struct super_block *s)
1da177e4 416{
c645b930
AV
417 if (!--s->s_count) {
418 list_del_init(&s->s_list);
419 WARN_ON(s->s_dentry_lru.node);
420 WARN_ON(s->s_inode_lru.node);
421 WARN_ON(!list_empty(&s->s_mounts));
422 security_sb_free(s);
423 put_user_ns(s->s_user_ns);
424 kfree(s->s_subtype);
425 call_rcu(&s->rcu, destroy_super_rcu);
1da177e4 426 }
1da177e4
LT
427}
428
429/**
430 * put_super - drop a temporary reference to superblock
431 * @sb: superblock in question
432 *
433 * Drops a temporary reference, frees superblock if there's no
434 * references left.
435 */
60b49885 436void put_super(struct super_block *sb)
1da177e4
LT
437{
438 spin_lock(&sb_lock);
439 __put_super(sb);
440 spin_unlock(&sb_lock);
441}
442
dc3216b1
CB
443static void kill_super_notify(struct super_block *sb)
444{
445 lockdep_assert_not_held(&sb->s_umount);
446
447 /* already notified earlier */
448 if (sb->s_flags & SB_DEAD)
449 return;
450
451 /*
452 * Remove it from @fs_supers so it isn't found by new
453 * sget{_fc}() walkers anymore. Any concurrent mounter still
454 * managing to grab a temporary reference is guaranteed to
455 * already see SB_DYING and will wait until we notify them about
456 * SB_DEAD.
457 */
458 spin_lock(&sb_lock);
459 hlist_del_init(&sb->s_instances);
460 spin_unlock(&sb_lock);
461
462 /*
463 * Let concurrent mounts know that this thing is really dead.
464 * We don't need @sb->s_umount here as every concurrent caller
465 * will see SB_DYING and either discard the superblock or wait
466 * for SB_DEAD.
467 */
468 super_wake(sb, SB_DEAD);
469}
1da177e4
LT
470
471/**
1712ac8f 472 * deactivate_locked_super - drop an active reference to superblock
1da177e4
LT
473 * @s: superblock to deactivate
474 *
bd7ced98 475 * Drops an active reference to superblock, converting it into a temporary
1712ac8f 476 * one if there is no other active references left. In that case we
1da177e4
LT
477 * tell fs driver to shut it down and drop the temporary reference we
478 * had just acquired.
1712ac8f
AV
479 *
480 * Caller holds exclusive lock on superblock; that lock is released.
1da177e4 481 */
1712ac8f 482void deactivate_locked_super(struct super_block *s)
1da177e4
LT
483{
484 struct file_system_type *fs = s->s_type;
b20bd1a5 485 if (atomic_dec_and_test(&s->s_active)) {
1720f5dd 486 shrinker_free(s->s_shrink);
28f2cd4f 487 fs->kill_sb(s);
f5e1dd34 488
dc3216b1
CB
489 kill_super_notify(s);
490
c0a5b560
VD
491 /*
492 * Since list_lru_destroy() may sleep, we cannot call it from
493 * put_super(), where we hold the sb_lock. Therefore we destroy
494 * the lru lists right now.
495 */
496 list_lru_destroy(&s->s_dentry_lru);
497 list_lru_destroy(&s->s_inode_lru);
498
1da177e4
LT
499 put_filesystem(fs);
500 put_super(s);
1712ac8f 501 } else {
0ed33598 502 super_unlock_excl(s);
1da177e4
LT
503 }
504}
505
1712ac8f 506EXPORT_SYMBOL(deactivate_locked_super);
1da177e4 507
74dbbdd7 508/**
1712ac8f 509 * deactivate_super - drop an active reference to superblock
74dbbdd7
AV
510 * @s: superblock to deactivate
511 *
1712ac8f
AV
512 * Variant of deactivate_locked_super(), except that superblock is *not*
513 * locked by caller. If we are going to drop the final active reference,
514 * lock will be acquired prior to that.
74dbbdd7 515 */
1712ac8f 516void deactivate_super(struct super_block *s)
74dbbdd7 517{
cc23402c 518 if (!atomic_add_unless(&s->s_active, -1, 1)) {
5e874914 519 __super_lock_excl(s);
1712ac8f 520 deactivate_locked_super(s);
74dbbdd7
AV
521 }
522}
523
1712ac8f 524EXPORT_SYMBOL(deactivate_super);
74dbbdd7 525
2c18a63b
CB
526static inline bool wait_dead(struct super_block *sb)
527{
528 unsigned int flags;
529
530 /*
531 * Pairs with memory barrier in super_wake() and ensures
532 * that we see SB_DEAD after we're woken.
533 */
534 flags = smp_load_acquire(&sb->s_flags);
535 return flags & SB_DEAD;
536}
537
538/**
97cbed04 539 * grab_super - acquire an active reference to a superblock
2c18a63b
CB
540 * @sb: superblock to acquire
541 *
542 * Acquire a temporary reference on a superblock and try to trade it for
543 * an active reference. This is used in sget{_fc}() to wait for a
544 * superblock to either become SB_BORN or for it to pass through
545 * sb->kill() and be marked as SB_DEAD.
546 *
547 * Return: This returns true if an active reference could be acquired,
548 * false if not.
549 */
97cbed04 550static bool grab_super(struct super_block *sb)
2c18a63b 551{
97cbed04
CB
552 bool locked;
553
2c18a63b 554 sb->s_count++;
97cbed04
CB
555 spin_unlock(&sb_lock);
556 locked = super_lock_excl(sb);
557 if (locked) {
558 if (atomic_inc_not_zero(&sb->s_active)) {
559 put_super(sb);
560 return true;
561 }
562 super_unlock_excl(sb);
2c18a63b
CB
563 }
564 wait_var_event(&sb->s_flags, wait_dead(sb));
345a5c4a 565 put_super(sb);
2c18a63b
CB
566 return false;
567}
568
12ad3ab6 569/*
d8ce82ef 570 * super_trylock_shared - try to grab ->s_umount shared
331cbdee 571 * @sb: reference we are trying to grab
12ad3ab6 572 *
eb6ef3df 573 * Try to prevent fs shutdown. This is used in places where we
12ad3ab6 574 * cannot take an active reference but we need to ensure that the
eb6ef3df
KK
575 * filesystem is not shut down while we are working on it. It returns
576 * false if we cannot acquire s_umount or if we lose the race and
577 * filesystem already got into shutdown, and returns true with the s_umount
578 * lock held in read mode in case of success. On successful return,
579 * the caller must drop the s_umount lock when done.
580 *
581 * Note that unlike get_super() et.al. this one does *not* bump ->s_count.
582 * The reason why it's safe is that we are OK with doing trylock instead
583 * of down_read(). There's a couple of places that are OK with that, but
584 * it's very much not a general-purpose interface.
12ad3ab6 585 */
d8ce82ef 586bool super_trylock_shared(struct super_block *sb)
12ad3ab6 587{
12ad3ab6 588 if (down_read_trylock(&sb->s_umount)) {
5e874914
CB
589 if (!(sb->s_flags & SB_DYING) && sb->s_root &&
590 (sb->s_flags & SB_BORN))
12ad3ab6 591 return true;
0ed33598 592 super_unlock_shared(sb);
12ad3ab6
DC
593 }
594
12ad3ab6
DC
595 return false;
596}
597
04b94071
DL
598/**
599 * retire_super - prevents superblock from being reused
600 * @sb: superblock to retire
601 *
602 * The function marks superblock to be ignored in superblock test, which
603 * prevents it from being reused for any new mounts. If the superblock has
604 * a private bdi, it also unregisters it, but doesn't reduce the refcount
605 * of the superblock to prevent potential races. The refcount is reduced
606 * by generic_shutdown_super(). The function can not be called
607 * concurrently with generic_shutdown_super(). It is safe to call the
608 * function multiple times, subsequent calls have no effect.
609 *
610 * The marker will affect the re-use only for block-device-based
611 * superblocks. Other superblocks will still get marked if this function
612 * is used, but that will not affect their reusability.
613 */
614void retire_super(struct super_block *sb)
615{
616 WARN_ON(!sb->s_bdev);
5e874914 617 __super_lock_excl(sb);
04b94071
DL
618 if (sb->s_iflags & SB_I_PERSB_BDI) {
619 bdi_unregister(sb->s_bdi);
620 sb->s_iflags &= ~SB_I_PERSB_BDI;
621 }
622 sb->s_iflags |= SB_I_RETIRED;
0ed33598 623 super_unlock_excl(sb);
04b94071
DL
624}
625EXPORT_SYMBOL(retire_super);
626
1da177e4
LT
627/**
628 * generic_shutdown_super - common helper for ->kill_sb()
629 * @sb: superblock to kill
630 *
631 * generic_shutdown_super() does all fs-independent work on superblock
632 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
633 * that need destruction out of superblock, call generic_shutdown_super()
634 * and release aforementioned objects. Note: dentries and inodes _are_
635 * taken care of and do not need specific handling.
c636ebdb
DH
636 *
637 * Upon calling this function, the filesystem may no longer alter or
638 * rearrange the set of dentries belonging to this super_block, nor may it
639 * change the attachments of dentries to inodes.
1da177e4
LT
640 */
641void generic_shutdown_super(struct super_block *sb)
642{
ee9b6d61 643 const struct super_operations *sop = sb->s_op;
1da177e4 644
c636ebdb
DH
645 if (sb->s_root) {
646 shrink_dcache_for_umount(sb);
60b0680f 647 sync_filesystem(sb);
e462ec50 648 sb->s_flags &= ~SB_ACTIVE;
efaee192 649
a1a0e23e 650 cgroup_writeback_umount();
63997e98 651
ccb820dc 652 /* Evict all inodes with zero refcount. */
63997e98 653 evict_inodes(sb);
ccb820dc
EB
654
655 /*
656 * Clean up and evict any inodes that still have references due
657 * to fsnotify or the security policy.
658 */
1edc8eb2 659 fsnotify_sb_delete(sb);
83e804f0 660 security_sb_delete(sb);
1da177e4 661
ccb820dc
EB
662 /*
663 * Now that all potentially-encrypted inodes have been evicted,
664 * the fscrypt keyring can be destroyed.
665 */
666 fscrypt_destroy_keyring(sb);
667
7b7a8665
CH
668 if (sb->s_dio_done_wq) {
669 destroy_workqueue(sb->s_dio_done_wq);
670 sb->s_dio_done_wq = NULL;
671 }
672
1da177e4
LT
673 if (sop->put_super)
674 sop->put_super(sb);
675
47d58691
JH
676 if (CHECK_DATA_CORRUPTION(!list_empty(&sb->s_inodes),
677 "VFS: Busy inodes after unmount of %s (%s)",
678 sb->s_id, sb->s_type->name)) {
679 /*
680 * Adding a proper bailout path here would be hard, but
681 * we can at least make it more likely that a later
682 * iput_final() or such crashes cleanly.
683 */
684 struct inode *inode;
685
686 spin_lock(&sb->s_inode_list_lock);
687 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
688 inode->i_op = VFS_PTR_POISON;
689 inode->i_sb = VFS_PTR_POISON;
690 inode->i_mapping = VFS_PTR_POISON;
691 }
692 spin_unlock(&sb->s_inode_list_lock);
1da177e4 693 }
1da177e4 694 }
5e874914
CB
695 /*
696 * Broadcast to everyone that grabbed a temporary reference to this
697 * superblock before we removed it from @fs_supers that the superblock
698 * is dying. Every walker of @fs_supers outside of sget{_fc}() will now
699 * discard this superblock and treat it as dead.
2c18a63b
CB
700 *
701 * We leave the superblock on @fs_supers so it can be found by
702 * sget{_fc}() until we passed sb->kill_sb().
5e874914
CB
703 */
704 super_wake(sb, SB_DYING);
0ed33598 705 super_unlock_excl(sb);
c1844d53 706 if (sb->s_bdi != &noop_backing_dev_info) {
0b3ea092
CH
707 if (sb->s_iflags & SB_I_PERSB_BDI)
708 bdi_unregister(sb->s_bdi);
fca39346
JK
709 bdi_put(sb->s_bdi);
710 sb->s_bdi = &noop_backing_dev_info;
fca39346 711 }
1da177e4
LT
712}
713
714EXPORT_SYMBOL(generic_shutdown_super);
715
20284ab7 716bool mount_capable(struct fs_context *fc)
0ce0cf12 717{
20284ab7 718 if (!(fc->fs_type->fs_flags & FS_USERNS_MOUNT))
0ce0cf12
AV
719 return capable(CAP_SYS_ADMIN);
720 else
c2c44ec2 721 return ns_capable(fc->user_ns, CAP_SYS_ADMIN);
0ce0cf12
AV
722}
723
cb50b348
AV
724/**
725 * sget_fc - Find or create a superblock
726 * @fc: Filesystem context.
727 * @test: Comparison callback
728 * @set: Setup callback
729 *
22ed7ecd 730 * Create a new superblock or find an existing one.
cb50b348 731 *
22ed7ecd
CB
732 * The @test callback is used to find a matching existing superblock.
733 * Whether or not the requested parameters in @fc are taken into account
734 * is specific to the @test callback that is used. They may even be
735 * completely ignored.
736 *
737 * If an extant superblock is matched, it will be returned unless:
738 *
739 * (1) the namespace the filesystem context @fc and the extant
740 * superblock's namespace differ
741 *
742 * (2) the filesystem context @fc has requested that reusing an extant
743 * superblock is not allowed
744 *
745 * In both cases EBUSY will be returned.
cb50b348
AV
746 *
747 * If no match is made, a new superblock will be allocated and basic
22ed7ecd
CB
748 * initialisation will be performed (s_type, s_fs_info and s_id will be
749 * set and the @set callback will be invoked), the superblock will be
750 * published and it will be returned in a partially constructed state
751 * with SB_BORN and SB_ACTIVE as yet unset.
752 *
753 * Return: On success, an extant or newly created superblock is
754 * returned. On failure an error pointer is returned.
cb50b348
AV
755 */
756struct super_block *sget_fc(struct fs_context *fc,
757 int (*test)(struct super_block *, struct fs_context *),
758 int (*set)(struct super_block *, struct fs_context *))
759{
760 struct super_block *s = NULL;
761 struct super_block *old;
762 struct user_namespace *user_ns = fc->global ? &init_user_ns : fc->user_ns;
763 int err;
764
cb50b348
AV
765retry:
766 spin_lock(&sb_lock);
767 if (test) {
768 hlist_for_each_entry(old, &fc->fs_type->fs_supers, s_instances) {
769 if (test(old, fc))
770 goto share_extant_sb;
771 }
772 }
773 if (!s) {
774 spin_unlock(&sb_lock);
775 s = alloc_super(fc->fs_type, fc->sb_flags, user_ns);
776 if (!s)
777 return ERR_PTR(-ENOMEM);
778 goto retry;
779 }
780
781 s->s_fs_info = fc->s_fs_info;
782 err = set(s, fc);
783 if (err) {
784 s->s_fs_info = NULL;
785 spin_unlock(&sb_lock);
786 destroy_unused_super(s);
787 return ERR_PTR(err);
788 }
789 fc->s_fs_info = NULL;
790 s->s_type = fc->fs_type;
c80fa7c8 791 s->s_iflags |= fc->s_iflags;
c642256b 792 strscpy(s->s_id, s->s_type->name, sizeof(s->s_id));
5e874914
CB
793 /*
794 * Make the superblock visible on @super_blocks and @fs_supers.
795 * It's in a nascent state and users should wait on SB_BORN or
796 * SB_DYING to be set.
797 */
cb50b348
AV
798 list_add_tail(&s->s_list, &super_blocks);
799 hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
800 spin_unlock(&sb_lock);
801 get_filesystem(s->s_type);
1720f5dd 802 shrinker_register(s->s_shrink);
cb50b348
AV
803 return s;
804
805share_extant_sb:
22ed7ecd 806 if (user_ns != old->s_user_ns || fc->exclusive) {
cb50b348
AV
807 spin_unlock(&sb_lock);
808 destroy_unused_super(s);
22ed7ecd
CB
809 if (fc->exclusive)
810 warnfc(fc, "reusing existing filesystem not allowed");
811 else
812 warnfc(fc, "reusing existing filesystem in another namespace not allowed");
cb50b348
AV
813 return ERR_PTR(-EBUSY);
814 }
97cbed04 815 if (!grab_super(old))
cb50b348
AV
816 goto retry;
817 destroy_unused_super(s);
818 return old;
819}
820EXPORT_SYMBOL(sget_fc);
821
1da177e4 822/**
023d066a
DH
823 * sget - find or create a superblock
824 * @type: filesystem type superblock should belong to
825 * @test: comparison callback
826 * @set: setup callback
827 * @flags: mount flags
828 * @data: argument to each of them
1da177e4 829 */
023d066a 830struct super_block *sget(struct file_system_type *type,
1da177e4
LT
831 int (*test)(struct super_block *,void *),
832 int (*set)(struct super_block *,void *),
023d066a 833 int flags,
1da177e4
LT
834 void *data)
835{
023d066a 836 struct user_namespace *user_ns = current_user_ns();
1da177e4 837 struct super_block *s = NULL;
d4730127 838 struct super_block *old;
1da177e4
LT
839 int err;
840
023d066a
DH
841 /* We don't yet pass the user namespace of the parent
842 * mount through to here so always use &init_user_ns
843 * until that changes.
844 */
845 if (flags & SB_SUBMOUNT)
846 user_ns = &init_user_ns;
847
1da177e4
LT
848retry:
849 spin_lock(&sb_lock);
d4730127 850 if (test) {
b67bfe0d 851 hlist_for_each_entry(old, &type->fs_supers, s_instances) {
d4730127
MK
852 if (!test(old, data))
853 continue;
6e4eab57
EB
854 if (user_ns != old->s_user_ns) {
855 spin_unlock(&sb_lock);
0200894d 856 destroy_unused_super(s);
6e4eab57
EB
857 return ERR_PTR(-EBUSY);
858 }
97cbed04 859 if (!grab_super(old))
d4730127 860 goto retry;
0200894d 861 destroy_unused_super(s);
d4730127
MK
862 return old;
863 }
1da177e4
LT
864 }
865 if (!s) {
866 spin_unlock(&sb_lock);
e462ec50 867 s = alloc_super(type, (flags & ~SB_SUBMOUNT), user_ns);
1da177e4
LT
868 if (!s)
869 return ERR_PTR(-ENOMEM);
870 goto retry;
871 }
dd111b31 872
1da177e4
LT
873 err = set(s, data);
874 if (err) {
875 spin_unlock(&sb_lock);
0200894d 876 destroy_unused_super(s);
1da177e4
LT
877 return ERR_PTR(err);
878 }
879 s->s_type = type;
c642256b 880 strscpy(s->s_id, type->name, sizeof(s->s_id));
1da177e4 881 list_add_tail(&s->s_list, &super_blocks);
a5166169 882 hlist_add_head(&s->s_instances, &type->fs_supers);
1da177e4
LT
883 spin_unlock(&sb_lock);
884 get_filesystem(type);
1720f5dd 885 shrinker_register(s->s_shrink);
1da177e4
LT
886 return s;
887}
1da177e4
LT
888EXPORT_SYMBOL(sget);
889
890void drop_super(struct super_block *sb)
891{
0ed33598 892 super_unlock_shared(sb);
1da177e4
LT
893 put_super(sb);
894}
895
896EXPORT_SYMBOL(drop_super);
897
ba6379f7
JK
898void drop_super_exclusive(struct super_block *sb)
899{
0ed33598 900 super_unlock_excl(sb);
ba6379f7
JK
901 put_super(sb);
902}
903EXPORT_SYMBOL(drop_super_exclusive);
904
fa7c1d50
MG
905static void __iterate_supers(void (*f)(struct super_block *))
906{
907 struct super_block *sb, *p = NULL;
908
909 spin_lock(&sb_lock);
910 list_for_each_entry(sb, &super_blocks, s_list) {
5e874914
CB
911 /* Pairs with memory marrier in super_wake(). */
912 if (smp_load_acquire(&sb->s_flags) & SB_DYING)
fa7c1d50
MG
913 continue;
914 sb->s_count++;
915 spin_unlock(&sb_lock);
916
917 f(sb);
918
919 spin_lock(&sb_lock);
920 if (p)
921 __put_super(p);
922 p = sb;
923 }
924 if (p)
925 __put_super(p);
926 spin_unlock(&sb_lock);
927}
01a05b33
AV
928/**
929 * iterate_supers - call function for all active superblocks
930 * @f: function to call
931 * @arg: argument to pass to it
932 *
933 * Scans the superblock list and calls given function, passing it
934 * locked superblock and given argument.
935 */
936void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
937{
dca33252 938 struct super_block *sb, *p = NULL;
01a05b33
AV
939
940 spin_lock(&sb_lock);
dca33252 941 list_for_each_entry(sb, &super_blocks, s_list) {
f0cd9880 942 bool locked;
5e874914 943
01a05b33
AV
944 sb->s_count++;
945 spin_unlock(&sb_lock);
946
f0cd9880
CB
947 locked = super_lock_shared(sb);
948 if (locked) {
949 if (sb->s_root)
950 f(sb, arg);
951 super_unlock_shared(sb);
952 }
01a05b33
AV
953
954 spin_lock(&sb_lock);
dca33252
AV
955 if (p)
956 __put_super(p);
957 p = sb;
01a05b33 958 }
dca33252
AV
959 if (p)
960 __put_super(p);
01a05b33
AV
961 spin_unlock(&sb_lock);
962}
963
43e15cdb
AV
964/**
965 * iterate_supers_type - call function for superblocks of given type
966 * @type: fs type
967 * @f: function to call
968 * @arg: argument to pass to it
969 *
970 * Scans the superblock list and calls given function, passing it
971 * locked superblock and given argument.
972 */
973void iterate_supers_type(struct file_system_type *type,
974 void (*f)(struct super_block *, void *), void *arg)
975{
976 struct super_block *sb, *p = NULL;
977
978 spin_lock(&sb_lock);
b67bfe0d 979 hlist_for_each_entry(sb, &type->fs_supers, s_instances) {
f0cd9880 980 bool locked;
5e874914 981
43e15cdb
AV
982 sb->s_count++;
983 spin_unlock(&sb_lock);
984
f0cd9880
CB
985 locked = super_lock_shared(sb);
986 if (locked) {
987 if (sb->s_root)
988 f(sb, arg);
989 super_unlock_shared(sb);
990 }
43e15cdb
AV
991
992 spin_lock(&sb_lock);
993 if (p)
994 __put_super(p);
995 p = sb;
996 }
997 if (p)
998 __put_super(p);
999 spin_unlock(&sb_lock);
1000}
1001
1002EXPORT_SYMBOL(iterate_supers_type);
1003
4e7b5671 1004struct super_block *user_get_super(dev_t dev, bool excl)
1da177e4 1005{
618f0636 1006 struct super_block *sb;
1da177e4 1007
1da177e4 1008 spin_lock(&sb_lock);
618f0636
KK
1009 list_for_each_entry(sb, &super_blocks, s_list) {
1010 if (sb->s_dev == dev) {
f0cd9880 1011 bool locked;
5e874914 1012
618f0636 1013 sb->s_count++;
1da177e4 1014 spin_unlock(&sb_lock);
df40c01a 1015 /* still alive? */
f0cd9880
CB
1016 locked = super_lock(sb, excl);
1017 if (locked) {
1018 if (sb->s_root)
1019 return sb;
1020 super_unlock(sb, excl);
1021 }
df40c01a 1022 /* nope, got unmounted */
618f0636 1023 spin_lock(&sb_lock);
df40c01a 1024 __put_super(sb);
5e874914 1025 break;
1da177e4
LT
1026 }
1027 }
1028 spin_unlock(&sb_lock);
1029 return NULL;
1030}
1031
1da177e4 1032/**
8d0347f6
DH
1033 * reconfigure_super - asks filesystem to change superblock parameters
1034 * @fc: The superblock and configuration
1da177e4 1035 *
8d0347f6 1036 * Alters the configuration parameters of a live superblock.
1da177e4 1037 */
8d0347f6 1038int reconfigure_super(struct fs_context *fc)
1da177e4 1039{
8d0347f6 1040 struct super_block *sb = fc->root->d_sb;
1da177e4 1041 int retval;
8d0347f6 1042 bool remount_ro = false;
c541dce8 1043 bool remount_rw = false;
8d0347f6 1044 bool force = fc->sb_flags & SB_FORCE;
4504230a 1045
8d0347f6
DH
1046 if (fc->sb_flags_mask & ~MS_RMT_MASK)
1047 return -EINVAL;
5accdf82 1048 if (sb->s_writers.frozen != SB_UNFROZEN)
4504230a
CH
1049 return -EBUSY;
1050
8d0347f6
DH
1051 retval = security_sb_remount(sb, fc->security);
1052 if (retval)
1053 return retval;
1054
1055 if (fc->sb_flags_mask & SB_RDONLY) {
9361401e 1056#ifdef CONFIG_BLOCK
6f0d9689
CH
1057 if (!(fc->sb_flags & SB_RDONLY) && sb->s_bdev &&
1058 bdev_read_only(sb->s_bdev))
8d0347f6 1059 return -EACCES;
9361401e 1060#endif
c541dce8 1061 remount_rw = !(fc->sb_flags & SB_RDONLY) && sb_rdonly(sb);
8d0347f6
DH
1062 remount_ro = (fc->sb_flags & SB_RDONLY) && !sb_rdonly(sb);
1063 }
d208bbdd 1064
0aec09d0 1065 if (remount_ro) {
fdab684d 1066 if (!hlist_empty(&sb->s_pins)) {
0ed33598 1067 super_unlock_excl(sb);
fdab684d 1068 group_pin_kill(&sb->s_pins);
5e874914 1069 __super_lock_excl(sb);
0aec09d0
AV
1070 if (!sb->s_root)
1071 return 0;
1072 if (sb->s_writers.frozen != SB_UNFROZEN)
1073 return -EBUSY;
8d0347f6 1074 remount_ro = !sb_rdonly(sb);
0aec09d0
AV
1075 }
1076 }
1077 shrink_dcache_sb(sb);
1078
8d0347f6
DH
1079 /* If we are reconfiguring to RDONLY and current sb is read/write,
1080 * make sure there are no files open for writing.
1081 */
d208bbdd 1082 if (remount_ro) {
4ed5e82f 1083 if (force) {
d7439fb1 1084 sb_start_ro_state_change(sb);
4ed5e82f
MS
1085 } else {
1086 retval = sb_prepare_remount_readonly(sb);
1087 if (retval)
1088 return retval;
4ed5e82f 1089 }
c541dce8
JK
1090 } else if (remount_rw) {
1091 /*
d7439fb1
JK
1092 * Protect filesystem's reconfigure code from writes from
1093 * userspace until reconfigure finishes.
c541dce8 1094 */
d7439fb1 1095 sb_start_ro_state_change(sb);
1da177e4
LT
1096 }
1097
f3a09c92
AV
1098 if (fc->ops->reconfigure) {
1099 retval = fc->ops->reconfigure(fc);
1100 if (retval) {
1101 if (!force)
1102 goto cancel_readonly;
1103 /* If forced remount, go ahead despite any errors */
1104 WARN(1, "forced remount of a %s fs returned %i\n",
1105 sb->s_type->name, retval);
1106 }
1da177e4 1107 }
8d0347f6
DH
1108
1109 WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
1110 (fc->sb_flags & fc->sb_flags_mask)));
d7439fb1 1111 sb_end_ro_state_change(sb);
c79d967d 1112
d208bbdd
NP
1113 /*
1114 * Some filesystems modify their metadata via some other path than the
1115 * bdev buffer cache (eg. use a private mapping, or directories in
1116 * pagecache, etc). Also file data modifications go via their own
1117 * mappings. So If we try to mount readonly then copy the filesystem
1118 * from bdev, we could get stale data, so invalidate it to give a best
1119 * effort at coherency.
1120 */
1121 if (remount_ro && sb->s_bdev)
1122 invalidate_bdev(sb->s_bdev);
1da177e4 1123 return 0;
4ed5e82f
MS
1124
1125cancel_readonly:
d7439fb1 1126 sb_end_ro_state_change(sb);
4ed5e82f 1127 return retval;
1da177e4
LT
1128}
1129
fa7c1d50 1130static void do_emergency_remount_callback(struct super_block *sb)
1da177e4 1131{
f0cd9880 1132 bool locked = super_lock_excl(sb);
5e874914 1133
f0cd9880 1134 if (locked && sb->s_root && sb->s_bdev && !sb_rdonly(sb)) {
8d0347f6
DH
1135 struct fs_context *fc;
1136
1137 fc = fs_context_for_reconfigure(sb->s_root,
1138 SB_RDONLY | SB_FORCE, SB_RDONLY);
1139 if (!IS_ERR(fc)) {
1140 if (parse_monolithic_mount_data(fc, NULL) == 0)
1141 (void)reconfigure_super(fc);
1142 put_fs_context(fc);
1143 }
1da177e4 1144 }
f0cd9880
CB
1145 if (locked)
1146 super_unlock_excl(sb);
fa7c1d50
MG
1147}
1148
1149static void do_emergency_remount(struct work_struct *work)
1150{
1151 __iterate_supers(do_emergency_remount_callback);
a2a9537a 1152 kfree(work);
1da177e4
LT
1153 printk("Emergency Remount complete\n");
1154}
1155
1156void emergency_remount(void)
1157{
a2a9537a
JA
1158 struct work_struct *work;
1159
1160 work = kmalloc(sizeof(*work), GFP_ATOMIC);
1161 if (work) {
1162 INIT_WORK(work, do_emergency_remount);
1163 schedule_work(work);
1164 }
1da177e4
LT
1165}
1166
08fdc8a0
MG
1167static void do_thaw_all_callback(struct super_block *sb)
1168{
f0cd9880 1169 bool locked = super_lock_excl(sb);
5e874914 1170
f0cd9880 1171 if (locked && sb->s_root) {
4a8b719f 1172 if (IS_ENABLED(CONFIG_BLOCK))
982c3b30 1173 while (sb->s_bdev && !bdev_thaw(sb->s_bdev))
4a8b719f 1174 pr_warn("Emergency Thaw on %pg\n", sb->s_bdev);
880b9577 1175 thaw_super_locked(sb, FREEZE_HOLDER_USERSPACE);
f0cd9880 1176 return;
08fdc8a0 1177 }
f0cd9880
CB
1178 if (locked)
1179 super_unlock_excl(sb);
08fdc8a0
MG
1180}
1181
1182static void do_thaw_all(struct work_struct *work)
1183{
1184 __iterate_supers(do_thaw_all_callback);
1185 kfree(work);
1186 printk(KERN_WARNING "Emergency Thaw complete\n");
1187}
1188
1189/**
1190 * emergency_thaw_all -- forcibly thaw every frozen filesystem
1191 *
1192 * Used for emergency unfreeze of all filesystems via SysRq
1193 */
1194void emergency_thaw_all(void)
1195{
1196 struct work_struct *work;
1197
1198 work = kmalloc(sizeof(*work), GFP_ATOMIC);
1199 if (work) {
1200 INIT_WORK(work, do_thaw_all);
1201 schedule_work(work);
1202 }
1203}
1204
ad76cbc6 1205static DEFINE_IDA(unnamed_dev_ida);
1da177e4 1206
5a66847e
MW
1207/**
1208 * get_anon_bdev - Allocate a block device for filesystems which don't have one.
1209 * @p: Pointer to a dev_t.
1210 *
1211 * Filesystems which don't use real block devices can call this function
1212 * to allocate a virtual block device.
1213 *
1214 * Context: Any context. Frequently called while holding sb_lock.
1215 * Return: 0 on success, -EMFILE if there are no anonymous bdevs left
1216 * or -ENOMEM if memory allocation failed.
1217 */
0ee5dc67 1218int get_anon_bdev(dev_t *p)
1da177e4
LT
1219{
1220 int dev;
5a66847e
MW
1221
1222 /*
1223 * Many userspace utilities consider an FSID of 0 invalid.
1224 * Always return at least 1 from get_anon_bdev.
1225 */
1226 dev = ida_alloc_range(&unnamed_dev_ida, 1, (1 << MINORBITS) - 1,
1227 GFP_ATOMIC);
1228 if (dev == -ENOSPC)
1229 dev = -EMFILE;
1230 if (dev < 0)
1231 return dev;
1232
1233 *p = MKDEV(0, dev);
1da177e4
LT
1234 return 0;
1235}
0ee5dc67 1236EXPORT_SYMBOL(get_anon_bdev);
1da177e4 1237
0ee5dc67 1238void free_anon_bdev(dev_t dev)
1da177e4 1239{
5a66847e 1240 ida_free(&unnamed_dev_ida, MINOR(dev));
1da177e4 1241}
0ee5dc67
AV
1242EXPORT_SYMBOL(free_anon_bdev);
1243
1244int set_anon_super(struct super_block *s, void *data)
1245{
df0ce26c 1246 return get_anon_bdev(&s->s_dev);
0ee5dc67 1247}
0ee5dc67
AV
1248EXPORT_SYMBOL(set_anon_super);
1249
1250void kill_anon_super(struct super_block *sb)
1251{
1252 dev_t dev = sb->s_dev;
1253 generic_shutdown_super(sb);
dc3216b1 1254 kill_super_notify(sb);
0ee5dc67
AV
1255 free_anon_bdev(dev);
1256}
1da177e4
LT
1257EXPORT_SYMBOL(kill_anon_super);
1258
1da177e4
LT
1259void kill_litter_super(struct super_block *sb)
1260{
1261 if (sb->s_root)
1262 d_genocide(sb->s_root);
1263 kill_anon_super(sb);
1264}
1da177e4
LT
1265EXPORT_SYMBOL(kill_litter_super);
1266
cb50b348
AV
1267int set_anon_super_fc(struct super_block *sb, struct fs_context *fc)
1268{
1269 return set_anon_super(sb, NULL);
1270}
1271EXPORT_SYMBOL(set_anon_super_fc);
1272
1273static int test_keyed_super(struct super_block *sb, struct fs_context *fc)
1274{
1275 return sb->s_fs_info == fc->s_fs_info;
1276}
1277
1278static int test_single_super(struct super_block *s, struct fs_context *fc)
1279{
1280 return 1;
1281}
1282
e062abae 1283static int vfs_get_super(struct fs_context *fc,
cda2ed05
CH
1284 int (*test)(struct super_block *, struct fs_context *),
1285 int (*fill_super)(struct super_block *sb,
1286 struct fs_context *fc))
cb50b348 1287{
cb50b348 1288 struct super_block *sb;
43ce4c1f 1289 int err;
cb50b348 1290
cb50b348
AV
1291 sb = sget_fc(fc, test, set_anon_super_fc);
1292 if (IS_ERR(sb))
1293 return PTR_ERR(sb);
1294
1295 if (!sb->s_root) {
43ce4c1f
DH
1296 err = fill_super(sb, fc);
1297 if (err)
1298 goto error;
cb50b348
AV
1299
1300 sb->s_flags |= SB_ACTIVE;
1301 }
1302
e062abae 1303 fc->root = dget(sb->s_root);
cb50b348 1304 return 0;
43ce4c1f
DH
1305
1306error:
1307 deactivate_locked_super(sb);
1308 return err;
cb50b348 1309}
cb50b348 1310
2ac295d4
AV
1311int get_tree_nodev(struct fs_context *fc,
1312 int (*fill_super)(struct super_block *sb,
1313 struct fs_context *fc))
1314{
e062abae 1315 return vfs_get_super(fc, NULL, fill_super);
2ac295d4
AV
1316}
1317EXPORT_SYMBOL(get_tree_nodev);
1318
c23a0bba
AV
1319int get_tree_single(struct fs_context *fc,
1320 int (*fill_super)(struct super_block *sb,
1321 struct fs_context *fc))
1322{
e062abae 1323 return vfs_get_super(fc, test_single_super, fill_super);
c23a0bba
AV
1324}
1325EXPORT_SYMBOL(get_tree_single);
1326
533770cc
AV
1327int get_tree_keyed(struct fs_context *fc,
1328 int (*fill_super)(struct super_block *sb,
1329 struct fs_context *fc),
1330 void *key)
1331{
1332 fc->s_fs_info = key;
e062abae 1333 return vfs_get_super(fc, test_keyed_super, fill_super);
533770cc
AV
1334}
1335EXPORT_SYMBOL(get_tree_keyed);
1336
69881be3
CB
1337static int set_bdev_super(struct super_block *s, void *data)
1338{
1339 s->s_dev = *(dev_t *)data;
1340 return 0;
1341}
1342
1343static int super_s_dev_set(struct super_block *s, struct fs_context *fc)
1344{
1345 return set_bdev_super(s, fc->sget_key);
1346}
1347
1348static int super_s_dev_test(struct super_block *s, struct fs_context *fc)
1349{
1350 return !(s->s_iflags & SB_I_RETIRED) &&
1351 s->s_dev == *(dev_t *)fc->sget_key;
1352}
1353
1354/**
1355 * sget_dev - Find or create a superblock by device number
1356 * @fc: Filesystem context.
1357 * @dev: device number
1358 *
1359 * Find or create a superblock using the provided device number that
1360 * will be stored in fc->sget_key.
1361 *
1362 * If an extant superblock is matched, then that will be returned with
1363 * an elevated reference count that the caller must transfer or discard.
1364 *
1365 * If no match is made, a new superblock will be allocated and basic
1366 * initialisation will be performed (s_type, s_fs_info, s_id, s_dev will
1367 * be set). The superblock will be published and it will be returned in
1368 * a partially constructed state with SB_BORN and SB_ACTIVE as yet
1369 * unset.
1370 *
1371 * Return: an existing or newly created superblock on success, an error
1372 * pointer on failure.
1373 */
1374struct super_block *sget_dev(struct fs_context *fc, dev_t dev)
1375{
1376 fc->sget_key = &dev;
1377 return sget_fc(fc, super_s_dev_test, super_s_dev_set);
1378}
1379EXPORT_SYMBOL(sget_dev);
1380
9361401e 1381#ifdef CONFIG_BLOCK
9c09a7cf 1382/*
fd146410
JK
1383 * Lock the superblock that is holder of the bdev. Returns the superblock
1384 * pointer if we successfully locked the superblock and it is alive. Otherwise
1385 * we return NULL and just unlock bdev->bd_holder_lock.
9c09a7cf 1386 *
fd146410 1387 * The function must be called with bdev->bd_holder_lock and releases it.
9c09a7cf 1388 */
49ef8832 1389static struct super_block *bdev_super_lock(struct block_device *bdev, bool excl)
fd146410 1390 __releases(&bdev->bd_holder_lock)
87efb390 1391{
fd146410 1392 struct super_block *sb = bdev->bd_holder;
f0cd9880 1393 bool locked;
fd146410
JK
1394
1395 lockdep_assert_held(&bdev->bd_holder_lock);
1396 lockdep_assert_not_held(&sb->s_umount);
3b224e1d 1397 lockdep_assert_not_held(&bdev->bd_disk->open_mutex);
fd146410
JK
1398
1399 /* Make sure sb doesn't go away from under us */
1400 spin_lock(&sb_lock);
1401 sb->s_count++;
1402 spin_unlock(&sb_lock);
49ef8832 1403
fd146410 1404 mutex_unlock(&bdev->bd_holder_lock);
87efb390 1405
49ef8832
CB
1406 locked = super_lock(sb, excl);
1407
fd146410 1408 /*
49ef8832
CB
1409 * If the superblock wasn't already SB_DYING then we hold
1410 * s_umount and can safely drop our temporary reference.
1411 */
fd146410 1412 put_super(sb);
49ef8832
CB
1413
1414 if (!locked)
1415 return NULL;
1416
1417 if (!sb->s_root || !(sb->s_flags & SB_ACTIVE)) {
1418 super_unlock(sb, excl);
1419 return NULL;
1420 }
1421
fd146410 1422 return sb;
9c09a7cf
CH
1423}
1424
d8530de5 1425static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
87efb390 1426{
fd146410 1427 struct super_block *sb;
9c09a7cf 1428
49ef8832 1429 sb = bdev_super_lock(bdev, false);
fd146410 1430 if (!sb)
87efb390
CH
1431 return;
1432
d8530de5
CH
1433 if (!surprise)
1434 sync_filesystem(sb);
1435 shrink_dcache_sb(sb);
e127b9bc 1436 invalidate_inodes(sb);
87efb390
CH
1437 if (sb->s_op->shutdown)
1438 sb->s_op->shutdown(sb);
9c09a7cf 1439
0ed33598 1440 super_unlock_shared(sb);
87efb390
CH
1441}
1442
2142b88c
CH
1443static void fs_bdev_sync(struct block_device *bdev)
1444{
fd146410 1445 struct super_block *sb;
2142b88c 1446
49ef8832 1447 sb = bdev_super_lock(bdev, false);
fd146410 1448 if (!sb)
2142b88c 1449 return;
49ef8832 1450
2142b88c 1451 sync_filesystem(sb);
0ed33598 1452 super_unlock_shared(sb);
2142b88c
CH
1453}
1454
49ef8832
CB
1455static struct super_block *get_bdev_super(struct block_device *bdev)
1456{
1457 bool active = false;
1458 struct super_block *sb;
1459
1460 sb = bdev_super_lock(bdev, true);
1461 if (sb) {
1462 active = atomic_inc_not_zero(&sb->s_active);
1463 super_unlock_excl(sb);
1464 }
1465 if (!active)
1466 return NULL;
1467 return sb;
1468}
1469
1470static int fs_bdev_freeze(struct block_device *bdev)
1471{
1472 struct super_block *sb;
1473 int error = 0;
1474
1475 lockdep_assert_held(&bdev->bd_fsfreeze_mutex);
1476
1477 sb = get_bdev_super(bdev);
1478 if (!sb)
1479 return -EINVAL;
1480
1481 if (sb->s_op->freeze_super)
1482 error = sb->s_op->freeze_super(sb, FREEZE_HOLDER_USERSPACE);
1483 else
1484 error = freeze_super(sb, FREEZE_HOLDER_USERSPACE);
1485 if (!error)
1486 error = sync_blockdev(bdev);
1487 deactivate_super(sb);
1488 return error;
1489}
1490
1491static int fs_bdev_thaw(struct block_device *bdev)
1492{
1493 struct super_block *sb;
1494 int error;
1495
1496 lockdep_assert_held(&bdev->bd_fsfreeze_mutex);
1497
1498 sb = get_bdev_super(bdev);
1499 if (WARN_ON_ONCE(!sb))
1500 return -EINVAL;
1501
1502 if (sb->s_op->thaw_super)
1503 error = sb->s_op->thaw_super(sb, FREEZE_HOLDER_USERSPACE);
1504 else
1505 error = thaw_super(sb, FREEZE_HOLDER_USERSPACE);
1506 deactivate_super(sb);
1507 return error;
1508}
1509
7ecd0b6f 1510const struct blk_holder_ops fs_holder_ops = {
d8530de5 1511 .mark_dead = fs_bdev_mark_dead,
2142b88c 1512 .sync = fs_bdev_sync,
49ef8832
CB
1513 .freeze = fs_bdev_freeze,
1514 .thaw = fs_bdev_thaw,
87efb390 1515};
7ecd0b6f 1516EXPORT_SYMBOL_GPL(fs_holder_ops);
fe62c3a4 1517
cf6da236 1518int setup_bdev_super(struct super_block *sb, int sb_flags,
aca740ce
JK
1519 struct fs_context *fc)
1520{
1521 blk_mode_t mode = sb_open_mode(sb_flags);
f4a48bc3 1522 struct bdev_handle *bdev_handle;
aca740ce
JK
1523 struct block_device *bdev;
1524
f4a48bc3
JK
1525 bdev_handle = bdev_open_by_dev(sb->s_dev, mode, sb, &fs_holder_ops);
1526 if (IS_ERR(bdev_handle)) {
aca740ce
JK
1527 if (fc)
1528 errorf(fc, "%s: Can't open blockdev", fc->source);
f4a48bc3 1529 return PTR_ERR(bdev_handle);
aca740ce 1530 }
f4a48bc3 1531 bdev = bdev_handle->bdev;
aca740ce
JK
1532
1533 /*
1534 * This really should be in blkdev_get_by_dev, but right now can't due
1535 * to legacy issues that require us to allow opening a block device node
1536 * writable from userspace even for a read-only block device.
1537 */
1538 if ((mode & BLK_OPEN_WRITE) && bdev_read_only(bdev)) {
f4a48bc3 1539 bdev_release(bdev_handle);
aca740ce
JK
1540 return -EACCES;
1541 }
1542
1543 /*
49ef8832
CB
1544 * It is enough to check bdev was not frozen before we set
1545 * s_bdev as freezing will wait until SB_BORN is set.
aca740ce 1546 */
49ef8832 1547 if (atomic_read(&bdev->bd_fsfreeze_count) > 0) {
aca740ce
JK
1548 if (fc)
1549 warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev);
f4a48bc3 1550 bdev_release(bdev_handle);
aca740ce
JK
1551 return -EBUSY;
1552 }
1553 spin_lock(&sb_lock);
f4a48bc3 1554 sb->s_bdev_handle = bdev_handle;
aca740ce
JK
1555 sb->s_bdev = bdev;
1556 sb->s_bdi = bdi_get(bdev->bd_disk->bdi);
1557 if (bdev_stable_writes(bdev))
1558 sb->s_iflags |= SB_I_STABLE_WRITES;
1559 spin_unlock(&sb_lock);
aca740ce
JK
1560
1561 snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
1720f5dd 1562 shrinker_debugfs_rename(sb->s_shrink, "sb-%s:%s", sb->s_type->name,
aca740ce
JK
1563 sb->s_id);
1564 sb_set_blocksize(sb, block_size(bdev));
1565 return 0;
fe62c3a4 1566}
cf6da236 1567EXPORT_SYMBOL_GPL(setup_bdev_super);
fe62c3a4
DH
1568
1569/**
1570 * get_tree_bdev - Get a superblock based on a single block device
1571 * @fc: The filesystem context holding the parameters
1572 * @fill_super: Helper to initialise a new superblock
1573 */
1574int get_tree_bdev(struct fs_context *fc,
1575 int (*fill_super)(struct super_block *,
1576 struct fs_context *))
1577{
fe62c3a4 1578 struct super_block *s;
fe62c3a4 1579 int error = 0;
aca740ce 1580 dev_t dev;
fe62c3a4 1581
fe62c3a4
DH
1582 if (!fc->source)
1583 return invalf(fc, "No source specified");
1584
aca740ce
JK
1585 error = lookup_bdev(fc->source, &dev);
1586 if (error) {
1587 errorf(fc, "%s: Can't lookup blockdev", fc->source);
1588 return error;
fe62c3a4
DH
1589 }
1590
1591 fc->sb_flags |= SB_NOSEC;
69881be3 1592 s = sget_dev(fc, dev);
aca740ce 1593 if (IS_ERR(s))
fe62c3a4
DH
1594 return PTR_ERR(s);
1595
1596 if (s->s_root) {
1597 /* Don't summarily change the RO/RW state. */
1598 if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
aca740ce 1599 warnf(fc, "%pg: Can't mount, would change RO state", s->s_bdev);
fe62c3a4 1600 deactivate_locked_super(s);
fe62c3a4
DH
1601 return -EBUSY;
1602 }
aca740ce 1603 } else {
aca740ce 1604 error = setup_bdev_super(s, fc->sb_flags, fc);
aca740ce
JK
1605 if (!error)
1606 error = fill_super(s, fc);
fe62c3a4
DH
1607 if (error) {
1608 deactivate_locked_super(s);
1609 return error;
1610 }
fe62c3a4 1611 s->s_flags |= SB_ACTIVE;
fe62c3a4
DH
1612 }
1613
1614 BUG_ON(fc->root);
1615 fc->root = dget(s->s_root);
1616 return 0;
1617}
1618EXPORT_SYMBOL(get_tree_bdev);
1619
1da177e4
LT
1620static int test_bdev_super(struct super_block *s, void *data)
1621{
aca740ce 1622 return !(s->s_iflags & SB_I_RETIRED) && s->s_dev == *(dev_t *)data;
1da177e4
LT
1623}
1624
152a0836 1625struct dentry *mount_bdev(struct file_system_type *fs_type,
1da177e4 1626 int flags, const char *dev_name, void *data,
152a0836 1627 int (*fill_super)(struct super_block *, void *, int))
1da177e4 1628{
1da177e4 1629 struct super_block *s;
aca740ce
JK
1630 int error;
1631 dev_t dev;
1da177e4 1632
aca740ce
JK
1633 error = lookup_bdev(dev_name, &dev);
1634 if (error)
1635 return ERR_PTR(error);
1da177e4 1636
aca740ce
JK
1637 flags |= SB_NOSEC;
1638 s = sget(fs_type, test_bdev_super, set_bdev_super, flags, &dev);
1da177e4 1639 if (IS_ERR(s))
aca740ce 1640 return ERR_CAST(s);
1da177e4
LT
1641
1642 if (s->s_root) {
e462ec50 1643 if ((flags ^ s->s_flags) & SB_RDONLY) {
74dbbdd7 1644 deactivate_locked_super(s);
aca740ce 1645 return ERR_PTR(-EBUSY);
1da177e4 1646 }
aca740ce 1647 } else {
aca740ce 1648 error = setup_bdev_super(s, flags, NULL);
aca740ce
JK
1649 if (!error)
1650 error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1da177e4 1651 if (error) {
74dbbdd7 1652 deactivate_locked_super(s);
aca740ce 1653 return ERR_PTR(error);
fa675765 1654 }
454e2398 1655
e462ec50 1656 s->s_flags |= SB_ACTIVE;
1da177e4
LT
1657 }
1658
152a0836 1659 return dget(s->s_root);
152a0836
AV
1660}
1661EXPORT_SYMBOL(mount_bdev);
1662
1da177e4
LT
1663void kill_block_super(struct super_block *sb)
1664{
1665 struct block_device *bdev = sb->s_bdev;
1666
1da177e4 1667 generic_shutdown_super(sb);
aca740ce
JK
1668 if (bdev) {
1669 sync_blockdev(bdev);
f4a48bc3 1670 bdev_release(sb->s_bdev_handle);
aca740ce 1671 }
1da177e4
LT
1672}
1673
1674EXPORT_SYMBOL(kill_block_super);
9361401e 1675#endif
1da177e4 1676
3c26ff6e 1677struct dentry *mount_nodev(struct file_system_type *fs_type,
1da177e4 1678 int flags, void *data,
3c26ff6e 1679 int (*fill_super)(struct super_block *, void *, int))
1da177e4
LT
1680{
1681 int error;
9249e17f 1682 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
1da177e4
LT
1683
1684 if (IS_ERR(s))
3c26ff6e 1685 return ERR_CAST(s);
1da177e4 1686
e462ec50 1687 error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1da177e4 1688 if (error) {
74dbbdd7 1689 deactivate_locked_super(s);
3c26ff6e 1690 return ERR_PTR(error);
1da177e4 1691 }
e462ec50 1692 s->s_flags |= SB_ACTIVE;
3c26ff6e 1693 return dget(s->s_root);
1da177e4 1694}
3c26ff6e
AV
1695EXPORT_SYMBOL(mount_nodev);
1696
a6097180
N
1697int reconfigure_single(struct super_block *s,
1698 int flags, void *data)
8d0347f6
DH
1699{
1700 struct fs_context *fc;
1701 int ret;
1702
1703 /* The caller really need to be passing fc down into mount_single(),
1704 * then a chunk of this can be removed. [Bollocks -- AV]
1705 * Better yet, reconfiguration shouldn't happen, but rather the second
1706 * mount should be rejected if the parameters are not compatible.
1707 */
1708 fc = fs_context_for_reconfigure(s->s_root, flags, MS_RMT_MASK);
1709 if (IS_ERR(fc))
1710 return PTR_ERR(fc);
1711
1712 ret = parse_monolithic_mount_data(fc, data);
1713 if (ret < 0)
1714 goto out;
1715
1716 ret = reconfigure_super(fc);
1717out:
1718 put_fs_context(fc);
1719 return ret;
1720}
1721
1da177e4
LT
1722static int compare_single(struct super_block *s, void *p)
1723{
1724 return 1;
1725}
1726
fc14f2fe 1727struct dentry *mount_single(struct file_system_type *fs_type,
1da177e4 1728 int flags, void *data,
fc14f2fe 1729 int (*fill_super)(struct super_block *, void *, int))
1da177e4
LT
1730{
1731 struct super_block *s;
1732 int error;
1733
9249e17f 1734 s = sget(fs_type, compare_single, set_anon_super, flags, NULL);
1da177e4 1735 if (IS_ERR(s))
fc14f2fe 1736 return ERR_CAST(s);
1da177e4 1737 if (!s->s_root) {
e462ec50 1738 error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
8d0347f6
DH
1739 if (!error)
1740 s->s_flags |= SB_ACTIVE;
9329d1be 1741 } else {
8d0347f6
DH
1742 error = reconfigure_single(s, flags, data);
1743 }
1744 if (unlikely(error)) {
1745 deactivate_locked_super(s);
1746 return ERR_PTR(error);
1da177e4 1747 }
fc14f2fe
AV
1748 return dget(s->s_root);
1749}
1750EXPORT_SYMBOL(mount_single);
1751
9bc61ab1
DH
1752/**
1753 * vfs_get_tree - Get the mountable root
1754 * @fc: The superblock configuration context.
1755 *
1756 * The filesystem is invoked to get or create a superblock which can then later
1757 * be used for mounting. The filesystem places a pointer to the root to be
1758 * used for mounting in @fc->root.
1759 */
1760int vfs_get_tree(struct fs_context *fc)
1da177e4 1761{
9d412a43 1762 struct super_block *sb;
9bc61ab1 1763 int error;
8089352a 1764
f3a09c92
AV
1765 if (fc->root)
1766 return -EBUSY;
1767
1768 /* Get the mountable root in fc->root, with a ref on the root and a ref
1769 * on the superblock.
1770 */
1771 error = fc->ops->get_tree(fc);
9bc61ab1
DH
1772 if (error < 0)
1773 return error;
1da177e4 1774
f3a09c92
AV
1775 if (!fc->root) {
1776 pr_err("Filesystem %s get_tree() didn't set fc->root\n",
1777 fc->fs_type->name);
1778 /* We don't know what the locking state of the superblock is -
1779 * if there is a superblock.
1780 */
1781 BUG();
1782 }
1783
9bc61ab1 1784 sb = fc->root->d_sb;
9d412a43 1785 WARN_ON(!sb->s_bdi);
79f546a6
DC
1786
1787 /*
5e874914
CB
1788 * super_wake() contains a memory barrier which also care of
1789 * ordering for super_cache_count(). We place it before setting
1790 * SB_BORN as the data dependency between the two functions is
1791 * the superblock structure contents that we just set up, not
1792 * the SB_BORN flag.
79f546a6 1793 */
5e874914 1794 super_wake(sb, SB_BORN);
454e2398 1795
9bc61ab1 1796 error = security_sb_set_mnt_opts(sb, fc->security, 0, NULL);
c9ce29ed
AV
1797 if (unlikely(error)) {
1798 fc_drop_locked(fc);
1799 return error;
a10d7c22
AV
1800 }
1801
42cb56ae
JL
1802 /*
1803 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1804 * but s_maxbytes was an unsigned long long for many releases. Throw
1805 * this warning for a little while to try and catch filesystems that
4358b567 1806 * violate this rule.
42cb56ae 1807 */
9d412a43 1808 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
9bc61ab1 1809 "negative value (%lld)\n", fc->fs_type->name, sb->s_maxbytes);
42cb56ae 1810
9bc61ab1 1811 return 0;
1da177e4 1812}
9bc61ab1 1813EXPORT_SYMBOL(vfs_get_tree);
1da177e4 1814
fca39346
JK
1815/*
1816 * Setup private BDI for given superblock. It gets automatically cleaned up
1817 * in generic_shutdown_super().
1818 */
1819int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1820{
1821 struct backing_dev_info *bdi;
1822 int err;
1823 va_list args;
1824
aef33c2f 1825 bdi = bdi_alloc(NUMA_NO_NODE);
fca39346
JK
1826 if (!bdi)
1827 return -ENOMEM;
1828
fca39346 1829 va_start(args, fmt);
7c4cc300 1830 err = bdi_register_va(bdi, fmt, args);
fca39346
JK
1831 va_end(args);
1832 if (err) {
1833 bdi_put(bdi);
1834 return err;
1835 }
1836 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1837 sb->s_bdi = bdi;
0b3ea092 1838 sb->s_iflags |= SB_I_PERSB_BDI;
fca39346
JK
1839
1840 return 0;
1841}
1842EXPORT_SYMBOL(super_setup_bdi_name);
1843
1844/*
1845 * Setup private BDI for given superblock. I gets automatically cleaned up
1846 * in generic_shutdown_super().
1847 */
1848int super_setup_bdi(struct super_block *sb)
1849{
1850 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
1851
1852 return super_setup_bdi_name(sb, "%.28s-%ld", sb->s_type->name,
1853 atomic_long_inc_return(&bdi_seq));
1854}
1855EXPORT_SYMBOL(super_setup_bdi);
1856
5accdf82
JK
1857/**
1858 * sb_wait_write - wait until all writers to given file system finish
1859 * @sb: the super for which we wait
1860 * @level: type of writers we wait for (normal vs page fault)
1861 *
1862 * This function waits until there are no writers of given type to given file
8129ed29 1863 * system.
5accdf82
JK
1864 */
1865static void sb_wait_write(struct super_block *sb, int level)
1866{
8129ed29 1867 percpu_down_write(sb->s_writers.rw_sem + level-1);
8129ed29 1868}
5accdf82 1869
f1a96220
ON
1870/*
1871 * We are going to return to userspace and forget about these locks, the
1872 * ownership goes to the caller of thaw_super() which does unlock().
1873 */
1874static void lockdep_sb_freeze_release(struct super_block *sb)
1875{
1876 int level;
1877
1878 for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--)
1879 percpu_rwsem_release(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
1880}
1881
1882/*
1883 * Tell lockdep we are holding these locks before we call ->unfreeze_fs(sb).
1884 */
1885static void lockdep_sb_freeze_acquire(struct super_block *sb)
8129ed29
ON
1886{
1887 int level;
5accdf82 1888
8129ed29
ON
1889 for (level = 0; level < SB_FREEZE_LEVELS; ++level)
1890 percpu_rwsem_acquire(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
f1a96220
ON
1891}
1892
2719c716 1893static void sb_freeze_unlock(struct super_block *sb, int level)
f1a96220 1894{
2719c716 1895 for (level--; level >= 0; level--)
8129ed29 1896 percpu_up_write(sb->s_writers.rw_sem + level);
5accdf82
JK
1897}
1898
59ba4fdd
DW
1899static int wait_for_partially_frozen(struct super_block *sb)
1900{
1901 int ret = 0;
1902
1903 do {
1904 unsigned short old = sb->s_writers.frozen;
1905
1906 up_write(&sb->s_umount);
1907 ret = wait_var_event_killable(&sb->s_writers.frozen,
1908 sb->s_writers.frozen != old);
1909 down_write(&sb->s_umount);
1910 } while (ret == 0 &&
1911 sb->s_writers.frozen != SB_UNFROZEN &&
1912 sb->s_writers.frozen != SB_FREEZE_COMPLETE);
1913
1914 return ret;
1915}
1916
18e9e510 1917/**
7000d3c4
RD
1918 * freeze_super - lock the filesystem and force it into a consistent state
1919 * @sb: the super to lock
880b9577 1920 * @who: context that wants to freeze
18e9e510
JB
1921 *
1922 * Syncs the super to make sure the filesystem is consistent and calls the fs's
880b9577 1923 * freeze_fs. Subsequent calls to this without first thawing the fs may return
18e9e510 1924 * -EBUSY.
5accdf82 1925 *
880b9577
DW
1926 * @who should be:
1927 * * %FREEZE_HOLDER_USERSPACE if userspace wants to freeze the fs;
1928 * * %FREEZE_HOLDER_KERNEL if the kernel wants to freeze the fs.
1929 *
1930 * The @who argument distinguishes between the kernel and userspace trying to
1931 * freeze the filesystem. Although there cannot be multiple kernel freezes or
1932 * multiple userspace freezes in effect at any given time, the kernel and
1933 * userspace can both hold a filesystem frozen. The filesystem remains frozen
1934 * until there are no kernel or userspace freezes in effect.
1935 *
5accdf82
JK
1936 * During this function, sb->s_writers.frozen goes through these values:
1937 *
1938 * SB_UNFROZEN: File system is normal, all writes progress as usual.
1939 *
1940 * SB_FREEZE_WRITE: The file system is in the process of being frozen. New
1941 * writes should be blocked, though page faults are still allowed. We wait for
1942 * all writes to complete and then proceed to the next stage.
1943 *
1944 * SB_FREEZE_PAGEFAULT: Freezing continues. Now also page faults are blocked
1945 * but internal fs threads can still modify the filesystem (although they
1946 * should not dirty new pages or inodes), writeback can run etc. After waiting
1947 * for all running page faults we sync the filesystem which will clean all
1948 * dirty pages and inodes (no new dirty pages or inodes can be created when
1949 * sync is running).
1950 *
1951 * SB_FREEZE_FS: The file system is frozen. Now all internal sources of fs
1952 * modification are blocked (e.g. XFS preallocation truncation on inode
1953 * reclaim). This is usually implemented by blocking new transactions for
1954 * filesystems that have them and need this additional guard. After all
1955 * internal writers are finished we call ->freeze_fs() to finish filesystem
1956 * freezing. Then we transition to SB_FREEZE_COMPLETE state. This state is
1957 * mostly auxiliary for filesystems to verify they do not modify frozen fs.
1958 *
1959 * sb->s_writers.frozen is protected by sb->s_umount.
18e9e510 1960 */
880b9577 1961int freeze_super(struct super_block *sb, enum freeze_holder who)
18e9e510
JB
1962{
1963 int ret;
1964
f0cd9880
CB
1965 if (!super_lock_excl(sb)) {
1966 WARN_ON_ONCE("Dying superblock while freezing!");
1967 return -EINVAL;
1968 }
18e9e510 1969 atomic_inc(&sb->s_active);
051178c3 1970
59ba4fdd 1971retry:
880b9577
DW
1972 if (sb->s_writers.frozen == SB_FREEZE_COMPLETE) {
1973 if (sb->s_writers.freeze_holders & who) {
1974 deactivate_locked_super(sb);
1975 return -EBUSY;
1976 }
1977
1978 WARN_ON(sb->s_writers.freeze_holders == 0);
1979
1980 /*
1981 * Someone else already holds this type of freeze; share the
1982 * freeze and assign the active ref to the freeze.
1983 */
1984 sb->s_writers.freeze_holders |= who;
3fb5a656 1985 super_unlock_excl(sb);
880b9577
DW
1986 return 0;
1987 }
1988
5accdf82 1989 if (sb->s_writers.frozen != SB_UNFROZEN) {
59ba4fdd
DW
1990 ret = wait_for_partially_frozen(sb);
1991 if (ret) {
1992 deactivate_locked_super(sb);
1993 return ret;
1994 }
1995
1996 goto retry;
18e9e510
JB
1997 }
1998
e462ec50 1999 if (!(sb->s_flags & SB_BORN)) {
0ed33598 2000 super_unlock_excl(sb);
dabe0dc1
AV
2001 return 0; /* sic - it's "nothing to do" */
2002 }
2003
bc98a42c 2004 if (sb_rdonly(sb)) {
5accdf82 2005 /* Nothing to do really... */
880b9577 2006 sb->s_writers.freeze_holders |= who;
5accdf82 2007 sb->s_writers.frozen = SB_FREEZE_COMPLETE;
59ba4fdd 2008 wake_up_var(&sb->s_writers.frozen);
0ed33598 2009 super_unlock_excl(sb);
18e9e510
JB
2010 return 0;
2011 }
2012
5accdf82 2013 sb->s_writers.frozen = SB_FREEZE_WRITE;
5accdf82 2014 /* Release s_umount to preserve sb_start_write -> s_umount ordering */
0ed33598 2015 super_unlock_excl(sb);
5accdf82 2016 sb_wait_write(sb, SB_FREEZE_WRITE);
f0cd9880
CB
2017 if (!super_lock_excl(sb)) {
2018 WARN_ON_ONCE("Dying superblock while freezing!");
2019 return -EINVAL;
2020 }
5accdf82
JK
2021
2022 /* Now we go and block page faults... */
5accdf82 2023 sb->s_writers.frozen = SB_FREEZE_PAGEFAULT;
5accdf82
JK
2024 sb_wait_write(sb, SB_FREEZE_PAGEFAULT);
2025
2026 /* All writers are done so after syncing there won't be dirty data */
2719c716
DW
2027 ret = sync_filesystem(sb);
2028 if (ret) {
2029 sb->s_writers.frozen = SB_UNFROZEN;
2030 sb_freeze_unlock(sb, SB_FREEZE_PAGEFAULT);
59ba4fdd 2031 wake_up_var(&sb->s_writers.frozen);
2719c716
DW
2032 deactivate_locked_super(sb);
2033 return ret;
2034 }
18e9e510 2035
5accdf82
JK
2036 /* Now wait for internal filesystem counter */
2037 sb->s_writers.frozen = SB_FREEZE_FS;
5accdf82 2038 sb_wait_write(sb, SB_FREEZE_FS);
18e9e510 2039
18e9e510
JB
2040 if (sb->s_op->freeze_fs) {
2041 ret = sb->s_op->freeze_fs(sb);
2042 if (ret) {
2043 printk(KERN_ERR
2044 "VFS:Filesystem freeze failed\n");
5accdf82 2045 sb->s_writers.frozen = SB_UNFROZEN;
2719c716 2046 sb_freeze_unlock(sb, SB_FREEZE_FS);
59ba4fdd 2047 wake_up_var(&sb->s_writers.frozen);
18e9e510
JB
2048 deactivate_locked_super(sb);
2049 return ret;
2050 }
2051 }
5accdf82 2052 /*
89f39af1
ON
2053 * For debugging purposes so that fs can warn if it sees write activity
2054 * when frozen is set to SB_FREEZE_COMPLETE, and for thaw_super().
5accdf82 2055 */
880b9577 2056 sb->s_writers.freeze_holders |= who;
5accdf82 2057 sb->s_writers.frozen = SB_FREEZE_COMPLETE;
59ba4fdd 2058 wake_up_var(&sb->s_writers.frozen);
f1a96220 2059 lockdep_sb_freeze_release(sb);
0ed33598 2060 super_unlock_excl(sb);
18e9e510
JB
2061 return 0;
2062}
2063EXPORT_SYMBOL(freeze_super);
2064
880b9577
DW
2065/*
2066 * Undoes the effect of a freeze_super_locked call. If the filesystem is
2067 * frozen both by userspace and the kernel, a thaw call from either source
2068 * removes that state without releasing the other state or unlocking the
2069 * filesystem.
2070 */
2071static int thaw_super_locked(struct super_block *sb, enum freeze_holder who)
18e9e510
JB
2072{
2073 int error;
2074
880b9577
DW
2075 if (sb->s_writers.frozen == SB_FREEZE_COMPLETE) {
2076 if (!(sb->s_writers.freeze_holders & who)) {
3fb5a656 2077 super_unlock_excl(sb);
880b9577
DW
2078 return -EINVAL;
2079 }
2080
2081 /*
2082 * Freeze is shared with someone else. Release our hold and
2083 * drop the active ref that freeze_super assigned to the
2084 * freezer.
2085 */
2086 if (sb->s_writers.freeze_holders & ~who) {
2087 sb->s_writers.freeze_holders &= ~who;
2088 deactivate_locked_super(sb);
2089 return 0;
2090 }
2091 } else {
0ed33598 2092 super_unlock_excl(sb);
18e9e510
JB
2093 return -EINVAL;
2094 }
2095
bc98a42c 2096 if (sb_rdonly(sb)) {
880b9577 2097 sb->s_writers.freeze_holders &= ~who;
8129ed29 2098 sb->s_writers.frozen = SB_UNFROZEN;
59ba4fdd 2099 wake_up_var(&sb->s_writers.frozen);
18e9e510 2100 goto out;
8129ed29 2101 }
18e9e510 2102
f1a96220
ON
2103 lockdep_sb_freeze_acquire(sb);
2104
18e9e510
JB
2105 if (sb->s_op->unfreeze_fs) {
2106 error = sb->s_op->unfreeze_fs(sb);
2107 if (error) {
3fb5a656 2108 printk(KERN_ERR "VFS:Filesystem thaw failed\n");
f1a96220 2109 lockdep_sb_freeze_release(sb);
0ed33598 2110 super_unlock_excl(sb);
18e9e510
JB
2111 return error;
2112 }
2113 }
2114
880b9577 2115 sb->s_writers.freeze_holders &= ~who;
5accdf82 2116 sb->s_writers.frozen = SB_UNFROZEN;
59ba4fdd 2117 wake_up_var(&sb->s_writers.frozen);
2719c716 2118 sb_freeze_unlock(sb, SB_FREEZE_FS);
8129ed29 2119out:
18e9e510 2120 deactivate_locked_super(sb);
18e9e510
JB
2121 return 0;
2122}
08fdc8a0 2123
961f3c89
MCC
2124/**
2125 * thaw_super -- unlock filesystem
2126 * @sb: the super to thaw
880b9577
DW
2127 * @who: context that wants to freeze
2128 *
2129 * Unlocks the filesystem and marks it writeable again after freeze_super()
2130 * if there are no remaining freezes on the filesystem.
961f3c89 2131 *
880b9577
DW
2132 * @who should be:
2133 * * %FREEZE_HOLDER_USERSPACE if userspace wants to thaw the fs;
2134 * * %FREEZE_HOLDER_KERNEL if the kernel wants to thaw the fs.
961f3c89 2135 */
880b9577 2136int thaw_super(struct super_block *sb, enum freeze_holder who)
08fdc8a0 2137{
f0cd9880
CB
2138 if (!super_lock_excl(sb)) {
2139 WARN_ON_ONCE("Dying superblock while thawing!");
2140 return -EINVAL;
2141 }
880b9577 2142 return thaw_super_locked(sb, who);
08fdc8a0 2143}
18e9e510 2144EXPORT_SYMBOL(thaw_super);
439bc39b
CH
2145
2146/*
2147 * Create workqueue for deferred direct IO completions. We allocate the
2148 * workqueue when it's first needed. This avoids creating workqueue for
2149 * filesystems that don't need it and also allows us to create the workqueue
2150 * late enough so the we can include s_id in the name of the workqueue.
2151 */
2152int sb_init_dio_done_wq(struct super_block *sb)
2153{
2154 struct workqueue_struct *old;
2155 struct workqueue_struct *wq = alloc_workqueue("dio/%s",
2156 WQ_MEM_RECLAIM, 0,
2157 sb->s_id);
2158 if (!wq)
2159 return -ENOMEM;
2160 /*
2161 * This has to be atomic as more DIOs can race to create the workqueue
2162 */
2163 old = cmpxchg(&sb->s_dio_done_wq, NULL, wq);
2164 /* Someone created workqueue before us? Free ours... */
2165 if (old)
2166 destroy_workqueue(wq);
2167 return 0;
2168}
389a4a4a 2169EXPORT_SYMBOL_GPL(sb_init_dio_done_wq);