1 // SPDX-License-Identifier: GPL-2.0
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:
19 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
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
24 #include <linux/export.h>
25 #include <linux/slab.h>
26 #include <linux/blkdev.h>
27 #include <linux/mount.h>
28 #include <linux/security.h>
29 #include <linux/writeback.h> /* for the emergency remount stuff */
30 #include <linux/idr.h>
31 #include <linux/mutex.h>
32 #include <linux/backing-dev.h>
33 #include <linux/rculist_bl.h>
34 #include <linux/cleancache.h>
35 #include <linux/fscrypt.h>
36 #include <linux/fsnotify.h>
37 #include <linux/lockdep.h>
38 #include <linux/user_namespace.h>
39 #include <linux/fs_context.h>
40 #include <uapi/linux/mount.h>
41 #include "internal.h"
42
43 static int thaw_super_locked(struct super_block *sb);
44
45 static LIST_HEAD(super_blocks);
46 static DEFINE_SPINLOCK(sb_lock);
47
48 static char *sb_writers_name[SB_FREEZE_LEVELS] = {
49 "sb_writers",
50 "sb_pagefaults",
51 "sb_internal",
52 };
53
54 /*
55 * One thing we have to be careful of with a per-sb shrinker is that we don't
56 * drop the last active reference to the superblock from within the shrinker.
57 * If that happens we could trigger unregistering the shrinker from within the
58 * shrinker path and that leads to deadlock on the shrinker_rwsem. Hence we
59 * take a passive reference to the superblock to avoid this from occurring.
60 */
super_cache_scan(struct shrinker * shrink,struct shrink_control * sc)61 static unsigned long super_cache_scan(struct shrinker *shrink,
62 struct shrink_control *sc)
63 {
64 struct super_block *sb;
65 long fs_objects = 0;
66 long total_objects;
67 long freed = 0;
68 long dentries;
69 long inodes;
70
71 sb = container_of(shrink, struct super_block, s_shrink);
72
73 /*
74 * Deadlock avoidance. We may hold various FS locks, and we don't want
75 * to recurse into the FS that called us in clear_inode() and friends..
76 */
77 if (!(sc->gfp_mask & __GFP_FS))
78 return SHRINK_STOP;
79
80 if (!trylock_super(sb))
81 return SHRINK_STOP;
82
83 if (sb->s_op->nr_cached_objects)
84 fs_objects = sb->s_op->nr_cached_objects(sb, sc);
85
86 inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
87 dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc);
88 total_objects = dentries + inodes + fs_objects + 1;
89 if (!total_objects)
90 total_objects = 1;
91
92 /* proportion the scan between the caches */
93 dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
94 inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
95 fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
96
97 /*
98 * prune the dcache first as the icache is pinned by it, then
99 * prune the icache, followed by the filesystem specific caches
100 *
101 * Ensure that we always scan at least one object - memcg kmem
102 * accounting uses this to fully empty the caches.
103 */
104 sc->nr_to_scan = dentries + 1;
105 freed = prune_dcache_sb(sb, sc);
106 sc->nr_to_scan = inodes + 1;
107 freed += prune_icache_sb(sb, sc);
108
109 if (fs_objects) {
110 sc->nr_to_scan = fs_objects + 1;
111 freed += sb->s_op->free_cached_objects(sb, sc);
112 }
113
114 up_read(&sb->s_umount);
115 return freed;
116 }
117
super_cache_count(struct shrinker * shrink,struct shrink_control * sc)118 static unsigned long super_cache_count(struct shrinker *shrink,
119 struct shrink_control *sc)
120 {
121 struct super_block *sb;
122 long total_objects = 0;
123
124 sb = container_of(shrink, struct super_block, s_shrink);
125
126 /*
127 * We don't call trylock_super() here as it is a scalability bottleneck,
128 * so we're exposed to partial setup state. The shrinker rwsem does not
129 * protect filesystem operations backing list_lru_shrink_count() or
130 * s_op->nr_cached_objects(). Counts can change between
131 * super_cache_count and super_cache_scan, so we really don't need locks
132 * here.
133 *
134 * However, if we are currently mounting the superblock, the underlying
135 * filesystem might be in a state of partial construction and hence it
136 * is dangerous to access it. trylock_super() uses a SB_BORN check to
137 * avoid this situation, so do the same here. The memory barrier is
138 * matched with the one in mount_fs() as we don't hold locks here.
139 */
140 if (!(sb->s_flags & SB_BORN))
141 return 0;
142 smp_rmb();
143
144 if (sb->s_op && sb->s_op->nr_cached_objects)
145 total_objects = sb->s_op->nr_cached_objects(sb, sc);
146
147 total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
148 total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
149
150 if (!total_objects)
151 return SHRINK_EMPTY;
152
153 total_objects = vfs_pressure_ratio(total_objects);
154 return total_objects;
155 }
156
destroy_super_work(struct work_struct * work)157 static void destroy_super_work(struct work_struct *work)
158 {
159 struct super_block *s = container_of(work, struct super_block,
160 destroy_work);
161 int i;
162
163 for (i = 0; i < SB_FREEZE_LEVELS; i++)
164 percpu_free_rwsem(&s->s_writers.rw_sem[i]);
165 kfree(s);
166 }
167
destroy_super_rcu(struct rcu_head * head)168 static void destroy_super_rcu(struct rcu_head *head)
169 {
170 struct super_block *s = container_of(head, struct super_block, rcu);
171 INIT_WORK(&s->destroy_work, destroy_super_work);
172 schedule_work(&s->destroy_work);
173 }
174
175 /* Free a superblock that has never been seen by anyone */
destroy_unused_super(struct super_block * s)176 static void destroy_unused_super(struct super_block *s)
177 {
178 if (!s)
179 return;
180 up_write(&s->s_umount);
181 list_lru_destroy(&s->s_dentry_lru);
182 list_lru_destroy(&s->s_inode_lru);
183 security_sb_free(s);
184 put_user_ns(s->s_user_ns);
185 kfree(s->s_subtype);
186 free_prealloced_shrinker(&s->s_shrink);
187 /* no delays needed */
188 destroy_super_work(&s->destroy_work);
189 }
190
191 /**
192 * alloc_super - create new superblock
193 * @type: filesystem type superblock should belong to
194 * @flags: the mount flags
195 * @user_ns: User namespace for the super_block
196 *
197 * Allocates and initializes a new &struct super_block. alloc_super()
198 * returns a pointer new superblock or %NULL if allocation had failed.
199 */
alloc_super(struct file_system_type * type,int flags,struct user_namespace * user_ns)200 static struct super_block *alloc_super(struct file_system_type *type, int flags,
201 struct user_namespace *user_ns)
202 {
203 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
204 static const struct super_operations default_op;
205 int i;
206
207 if (!s)
208 return NULL;
209
210 INIT_LIST_HEAD(&s->s_mounts);
211 s->s_user_ns = get_user_ns(user_ns);
212 init_rwsem(&s->s_umount);
213 lockdep_set_class(&s->s_umount, &type->s_umount_key);
214 /*
215 * sget() can have s_umount recursion.
216 *
217 * When it cannot find a suitable sb, it allocates a new
218 * one (this one), and tries again to find a suitable old
219 * one.
220 *
221 * In case that succeeds, it will acquire the s_umount
222 * lock of the old one. Since these are clearly distrinct
223 * locks, and this object isn't exposed yet, there's no
224 * risk of deadlocks.
225 *
226 * Annotate this by putting this lock in a different
227 * subclass.
228 */
229 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
230
231 if (security_sb_alloc(s))
232 goto fail;
233
234 for (i = 0; i < SB_FREEZE_LEVELS; i++) {
235 if (__percpu_init_rwsem(&s->s_writers.rw_sem[i],
236 sb_writers_name[i],
237 &type->s_writers_key[i]))
238 goto fail;
239 }
240 init_waitqueue_head(&s->s_writers.wait_unfrozen);
241 s->s_bdi = &noop_backing_dev_info;
242 s->s_flags = flags;
243 if (s->s_user_ns != &init_user_ns)
244 s->s_iflags |= SB_I_NODEV;
245 INIT_HLIST_NODE(&s->s_instances);
246 INIT_HLIST_BL_HEAD(&s->s_roots);
247 mutex_init(&s->s_sync_lock);
248 INIT_LIST_HEAD(&s->s_inodes);
249 spin_lock_init(&s->s_inode_list_lock);
250 INIT_LIST_HEAD(&s->s_inodes_wb);
251 spin_lock_init(&s->s_inode_wblist_lock);
252
253 s->s_count = 1;
254 atomic_set(&s->s_active, 1);
255 mutex_init(&s->s_vfs_rename_mutex);
256 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
257 init_rwsem(&s->s_dquot.dqio_sem);
258 s->s_maxbytes = MAX_NON_LFS;
259 s->s_op = &default_op;
260 s->s_time_gran = 1000000000;
261 s->s_time_min = TIME64_MIN;
262 s->s_time_max = TIME64_MAX;
263 s->cleancache_poolid = CLEANCACHE_NO_POOL;
264
265 s->s_shrink.seeks = DEFAULT_SEEKS;
266 s->s_shrink.scan_objects = super_cache_scan;
267 s->s_shrink.count_objects = super_cache_count;
268 s->s_shrink.batch = 1024;
269 s->s_shrink.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE;
270 if (prealloc_shrinker(&s->s_shrink))
271 goto fail;
272 if (list_lru_init_memcg(&s->s_dentry_lru, &s->s_shrink))
273 goto fail;
274 if (list_lru_init_memcg(&s->s_inode_lru, &s->s_shrink))
275 goto fail;
276 return s;
277
278 fail:
279 destroy_unused_super(s);
280 return NULL;
281 }
282
283 /* Superblock refcounting */
284
285 /*
286 * Drop a superblock's refcount. The caller must hold sb_lock.
287 */
__put_super(struct super_block * s)288 static void __put_super(struct super_block *s)
289 {
290 if (!--s->s_count) {
291 list_del_init(&s->s_list);
292 WARN_ON(s->s_dentry_lru.node);
293 WARN_ON(s->s_inode_lru.node);
294 WARN_ON(!list_empty(&s->s_mounts));
295 security_sb_free(s);
296 put_user_ns(s->s_user_ns);
297 kfree(s->s_subtype);
298 call_rcu(&s->rcu, destroy_super_rcu);
299 }
300 }
301
302 /**
303 * put_super - drop a temporary reference to superblock
304 * @sb: superblock in question
305 *
306 * Drops a temporary reference, frees superblock if there's no
307 * references left.
308 */
put_super(struct super_block * sb)309 void put_super(struct super_block *sb)
310 {
311 spin_lock(&sb_lock);
312 __put_super(sb);
313 spin_unlock(&sb_lock);
314 }
315
316
317 /**
318 * deactivate_locked_super - drop an active reference to superblock
319 * @s: superblock to deactivate
320 *
321 * Drops an active reference to superblock, converting it into a temporary
322 * one if there is no other active references left. In that case we
323 * tell fs driver to shut it down and drop the temporary reference we
324 * had just acquired.
325 *
326 * Caller holds exclusive lock on superblock; that lock is released.
327 */
deactivate_locked_super(struct super_block * s)328 void deactivate_locked_super(struct super_block *s)
329 {
330 struct file_system_type *fs = s->s_type;
331 if (atomic_dec_and_test(&s->s_active)) {
332 cleancache_invalidate_fs(s);
333 unregister_shrinker(&s->s_shrink);
334 fs->kill_sb(s);
335
336 /*
337 * Since list_lru_destroy() may sleep, we cannot call it from
338 * put_super(), where we hold the sb_lock. Therefore we destroy
339 * the lru lists right now.
340 */
341 list_lru_destroy(&s->s_dentry_lru);
342 list_lru_destroy(&s->s_inode_lru);
343
344 put_filesystem(fs);
345 put_super(s);
346 } else {
347 up_write(&s->s_umount);
348 }
349 }
350
351 EXPORT_SYMBOL(deactivate_locked_super);
352
353 /**
354 * deactivate_super - drop an active reference to superblock
355 * @s: superblock to deactivate
356 *
357 * Variant of deactivate_locked_super(), except that superblock is *not*
358 * locked by caller. If we are going to drop the final active reference,
359 * lock will be acquired prior to that.
360 */
deactivate_super(struct super_block * s)361 void deactivate_super(struct super_block *s)
362 {
363 if (!atomic_add_unless(&s->s_active, -1, 1)) {
364 down_write(&s->s_umount);
365 deactivate_locked_super(s);
366 }
367 }
368
369 EXPORT_SYMBOL(deactivate_super);
370
371 /**
372 * grab_super - acquire an active reference
373 * @s: reference we are trying to make active
374 *
375 * Tries to acquire an active reference. grab_super() is used when we
376 * had just found a superblock in super_blocks or fs_type->fs_supers
377 * and want to turn it into a full-blown active reference. grab_super()
378 * is called with sb_lock held and drops it. Returns 1 in case of
379 * success, 0 if we had failed (superblock contents was already dead or
380 * dying when grab_super() had been called). Note that this is only
381 * called for superblocks not in rundown mode (== ones still on ->fs_supers
382 * of their type), so increment of ->s_count is OK here.
383 */
grab_super(struct super_block * s)384 static int grab_super(struct super_block *s) __releases(sb_lock)
385 {
386 s->s_count++;
387 spin_unlock(&sb_lock);
388 down_write(&s->s_umount);
389 if ((s->s_flags & SB_BORN) && atomic_inc_not_zero(&s->s_active)) {
390 put_super(s);
391 return 1;
392 }
393 up_write(&s->s_umount);
394 put_super(s);
395 return 0;
396 }
397
398 /*
399 * trylock_super - try to grab ->s_umount shared
400 * @sb: reference we are trying to grab
401 *
402 * Try to prevent fs shutdown. This is used in places where we
403 * cannot take an active reference but we need to ensure that the
404 * filesystem is not shut down while we are working on it. It returns
405 * false if we cannot acquire s_umount or if we lose the race and
406 * filesystem already got into shutdown, and returns true with the s_umount
407 * lock held in read mode in case of success. On successful return,
408 * the caller must drop the s_umount lock when done.
409 *
410 * Note that unlike get_super() et.al. this one does *not* bump ->s_count.
411 * The reason why it's safe is that we are OK with doing trylock instead
412 * of down_read(). There's a couple of places that are OK with that, but
413 * it's very much not a general-purpose interface.
414 */
trylock_super(struct super_block * sb)415 bool trylock_super(struct super_block *sb)
416 {
417 if (down_read_trylock(&sb->s_umount)) {
418 if (!hlist_unhashed(&sb->s_instances) &&
419 sb->s_root && (sb->s_flags & SB_BORN))
420 return true;
421 up_read(&sb->s_umount);
422 }
423
424 return false;
425 }
426
427 /**
428 * generic_shutdown_super - common helper for ->kill_sb()
429 * @sb: superblock to kill
430 *
431 * generic_shutdown_super() does all fs-independent work on superblock
432 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
433 * that need destruction out of superblock, call generic_shutdown_super()
434 * and release aforementioned objects. Note: dentries and inodes _are_
435 * taken care of and do not need specific handling.
436 *
437 * Upon calling this function, the filesystem may no longer alter or
438 * rearrange the set of dentries belonging to this super_block, nor may it
439 * change the attachments of dentries to inodes.
440 */
generic_shutdown_super(struct super_block * sb)441 void generic_shutdown_super(struct super_block *sb)
442 {
443 const struct super_operations *sop = sb->s_op;
444
445 if (sb->s_root) {
446 shrink_dcache_for_umount(sb);
447 sync_filesystem(sb);
448 sb->s_flags &= ~SB_ACTIVE;
449
450 cgroup_writeback_umount();
451
452 /* Evict all inodes with zero refcount. */
453 evict_inodes(sb);
454
455 /*
456 * Clean up and evict any inodes that still have references due
457 * to fsnotify or the security policy.
458 */
459 fsnotify_sb_delete(sb);
460 security_sb_delete(sb);
461
462 /*
463 * Now that all potentially-encrypted inodes have been evicted,
464 * the fscrypt keyring can be destroyed.
465 */
466 fscrypt_destroy_keyring(sb);
467
468 if (sb->s_dio_done_wq) {
469 destroy_workqueue(sb->s_dio_done_wq);
470 sb->s_dio_done_wq = NULL;
471 }
472
473 if (sop->put_super)
474 sop->put_super(sb);
475
476 if (!list_empty(&sb->s_inodes)) {
477 printk("VFS: Busy inodes after unmount of %s. "
478 "Self-destruct in 5 seconds. Have a nice day...\n",
479 sb->s_id);
480 }
481 }
482 spin_lock(&sb_lock);
483 /* should be initialized for __put_super_and_need_restart() */
484 hlist_del_init(&sb->s_instances);
485 spin_unlock(&sb_lock);
486 up_write(&sb->s_umount);
487 if (sb->s_bdi != &noop_backing_dev_info) {
488 bdi_put(sb->s_bdi);
489 sb->s_bdi = &noop_backing_dev_info;
490 }
491 }
492
493 EXPORT_SYMBOL(generic_shutdown_super);
494
mount_capable(struct fs_context * fc)495 bool mount_capable(struct fs_context *fc)
496 {
497 if (!(fc->fs_type->fs_flags & FS_USERNS_MOUNT))
498 return capable(CAP_SYS_ADMIN);
499 else
500 return ns_capable(fc->user_ns, CAP_SYS_ADMIN);
501 }
502
503 /**
504 * sget_fc - Find or create a superblock
505 * @fc: Filesystem context.
506 * @test: Comparison callback
507 * @set: Setup callback
508 *
509 * Find or create a superblock using the parameters stored in the filesystem
510 * context and the two callback functions.
511 *
512 * If an extant superblock is matched, then that will be returned with an
513 * elevated reference count that the caller must transfer or discard.
514 *
515 * If no match is made, a new superblock will be allocated and basic
516 * initialisation will be performed (s_type, s_fs_info and s_id will be set and
517 * the set() callback will be invoked), the superblock will be published and it
518 * will be returned in a partially constructed state with SB_BORN and SB_ACTIVE
519 * as yet unset.
520 */
sget_fc(struct fs_context * fc,int (* test)(struct super_block *,struct fs_context *),int (* set)(struct super_block *,struct fs_context *))521 struct super_block *sget_fc(struct fs_context *fc,
522 int (*test)(struct super_block *, struct fs_context *),
523 int (*set)(struct super_block *, struct fs_context *))
524 {
525 struct super_block *s = NULL;
526 struct super_block *old;
527 struct user_namespace *user_ns = fc->global ? &init_user_ns : fc->user_ns;
528 int err;
529
530 retry:
531 spin_lock(&sb_lock);
532 if (test) {
533 hlist_for_each_entry(old, &fc->fs_type->fs_supers, s_instances) {
534 if (test(old, fc))
535 goto share_extant_sb;
536 }
537 }
538 if (!s) {
539 spin_unlock(&sb_lock);
540 s = alloc_super(fc->fs_type, fc->sb_flags, user_ns);
541 if (!s)
542 return ERR_PTR(-ENOMEM);
543 goto retry;
544 }
545
546 s->s_fs_info = fc->s_fs_info;
547 err = set(s, fc);
548 if (err) {
549 s->s_fs_info = NULL;
550 spin_unlock(&sb_lock);
551 destroy_unused_super(s);
552 return ERR_PTR(err);
553 }
554 fc->s_fs_info = NULL;
555 s->s_type = fc->fs_type;
556 s->s_iflags |= fc->s_iflags;
557 strlcpy(s->s_id, s->s_type->name, sizeof(s->s_id));
558 list_add_tail(&s->s_list, &super_blocks);
559 hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
560 spin_unlock(&sb_lock);
561 get_filesystem(s->s_type);
562 register_shrinker_prepared(&s->s_shrink);
563 return s;
564
565 share_extant_sb:
566 if (user_ns != old->s_user_ns) {
567 spin_unlock(&sb_lock);
568 destroy_unused_super(s);
569 return ERR_PTR(-EBUSY);
570 }
571 if (!grab_super(old))
572 goto retry;
573 destroy_unused_super(s);
574 return old;
575 }
576 EXPORT_SYMBOL(sget_fc);
577
578 /**
579 * sget - find or create a superblock
580 * @type: filesystem type superblock should belong to
581 * @test: comparison callback
582 * @set: setup callback
583 * @flags: mount flags
584 * @data: argument to each of them
585 */
sget(struct file_system_type * type,int (* test)(struct super_block *,void *),int (* set)(struct super_block *,void *),int flags,void * data)586 struct super_block *sget(struct file_system_type *type,
587 int (*test)(struct super_block *,void *),
588 int (*set)(struct super_block *,void *),
589 int flags,
590 void *data)
591 {
592 struct user_namespace *user_ns = current_user_ns();
593 struct super_block *s = NULL;
594 struct super_block *old;
595 int err;
596
597 /* We don't yet pass the user namespace of the parent
598 * mount through to here so always use &init_user_ns
599 * until that changes.
600 */
601 if (flags & SB_SUBMOUNT)
602 user_ns = &init_user_ns;
603
604 retry:
605 spin_lock(&sb_lock);
606 if (test) {
607 hlist_for_each_entry(old, &type->fs_supers, s_instances) {
608 if (!test(old, data))
609 continue;
610 if (user_ns != old->s_user_ns) {
611 spin_unlock(&sb_lock);
612 destroy_unused_super(s);
613 return ERR_PTR(-EBUSY);
614 }
615 if (!grab_super(old))
616 goto retry;
617 destroy_unused_super(s);
618 return old;
619 }
620 }
621 if (!s) {
622 spin_unlock(&sb_lock);
623 s = alloc_super(type, (flags & ~SB_SUBMOUNT), user_ns);
624 if (!s)
625 return ERR_PTR(-ENOMEM);
626 goto retry;
627 }
628
629 err = set(s, data);
630 if (err) {
631 spin_unlock(&sb_lock);
632 destroy_unused_super(s);
633 return ERR_PTR(err);
634 }
635 s->s_type = type;
636 strlcpy(s->s_id, type->name, sizeof(s->s_id));
637 list_add_tail(&s->s_list, &super_blocks);
638 hlist_add_head(&s->s_instances, &type->fs_supers);
639 spin_unlock(&sb_lock);
640 get_filesystem(type);
641 register_shrinker_prepared(&s->s_shrink);
642 return s;
643 }
644 EXPORT_SYMBOL(sget);
645
drop_super(struct super_block * sb)646 void drop_super(struct super_block *sb)
647 {
648 up_read(&sb->s_umount);
649 put_super(sb);
650 }
651
652 EXPORT_SYMBOL(drop_super);
653
drop_super_exclusive(struct super_block * sb)654 void drop_super_exclusive(struct super_block *sb)
655 {
656 up_write(&sb->s_umount);
657 put_super(sb);
658 }
659 EXPORT_SYMBOL(drop_super_exclusive);
660
__iterate_supers(void (* f)(struct super_block *))661 static void __iterate_supers(void (*f)(struct super_block *))
662 {
663 struct super_block *sb, *p = NULL;
664
665 spin_lock(&sb_lock);
666 list_for_each_entry(sb, &super_blocks, s_list) {
667 if (hlist_unhashed(&sb->s_instances))
668 continue;
669 sb->s_count++;
670 spin_unlock(&sb_lock);
671
672 f(sb);
673
674 spin_lock(&sb_lock);
675 if (p)
676 __put_super(p);
677 p = sb;
678 }
679 if (p)
680 __put_super(p);
681 spin_unlock(&sb_lock);
682 }
683 /**
684 * iterate_supers - call function for all active superblocks
685 * @f: function to call
686 * @arg: argument to pass to it
687 *
688 * Scans the superblock list and calls given function, passing it
689 * locked superblock and given argument.
690 */
iterate_supers(void (* f)(struct super_block *,void *),void * arg)691 void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
692 {
693 struct super_block *sb, *p = NULL;
694
695 spin_lock(&sb_lock);
696 list_for_each_entry(sb, &super_blocks, s_list) {
697 if (hlist_unhashed(&sb->s_instances))
698 continue;
699 sb->s_count++;
700 spin_unlock(&sb_lock);
701
702 down_read(&sb->s_umount);
703 if (sb->s_root && (sb->s_flags & SB_BORN))
704 f(sb, arg);
705 up_read(&sb->s_umount);
706
707 spin_lock(&sb_lock);
708 if (p)
709 __put_super(p);
710 p = sb;
711 }
712 if (p)
713 __put_super(p);
714 spin_unlock(&sb_lock);
715 }
716
717 /**
718 * iterate_supers_type - call function for superblocks of given type
719 * @type: fs type
720 * @f: function to call
721 * @arg: argument to pass to it
722 *
723 * Scans the superblock list and calls given function, passing it
724 * locked superblock and given argument.
725 */
iterate_supers_type(struct file_system_type * type,void (* f)(struct super_block *,void *),void * arg)726 void iterate_supers_type(struct file_system_type *type,
727 void (*f)(struct super_block *, void *), void *arg)
728 {
729 struct super_block *sb, *p = NULL;
730
731 spin_lock(&sb_lock);
732 hlist_for_each_entry(sb, &type->fs_supers, s_instances) {
733 sb->s_count++;
734 spin_unlock(&sb_lock);
735
736 down_read(&sb->s_umount);
737 if (sb->s_root && (sb->s_flags & SB_BORN))
738 f(sb, arg);
739 up_read(&sb->s_umount);
740
741 spin_lock(&sb_lock);
742 if (p)
743 __put_super(p);
744 p = sb;
745 }
746 if (p)
747 __put_super(p);
748 spin_unlock(&sb_lock);
749 }
750
751 EXPORT_SYMBOL(iterate_supers_type);
752
753 /**
754 * get_super - get the superblock of a device
755 * @bdev: device to get the superblock for
756 *
757 * Scans the superblock list and finds the superblock of the file system
758 * mounted on the device given. %NULL is returned if no match is found.
759 */
get_super(struct block_device * bdev)760 struct super_block *get_super(struct block_device *bdev)
761 {
762 struct super_block *sb;
763
764 if (!bdev)
765 return NULL;
766
767 spin_lock(&sb_lock);
768 rescan:
769 list_for_each_entry(sb, &super_blocks, s_list) {
770 if (hlist_unhashed(&sb->s_instances))
771 continue;
772 if (sb->s_bdev == bdev) {
773 sb->s_count++;
774 spin_unlock(&sb_lock);
775 down_read(&sb->s_umount);
776 /* still alive? */
777 if (sb->s_root && (sb->s_flags & SB_BORN))
778 return sb;
779 up_read(&sb->s_umount);
780 /* nope, got unmounted */
781 spin_lock(&sb_lock);
782 __put_super(sb);
783 goto rescan;
784 }
785 }
786 spin_unlock(&sb_lock);
787 return NULL;
788 }
789
790 /**
791 * get_active_super - get an active reference to the superblock of a device
792 * @bdev: device to get the superblock for
793 *
794 * Scans the superblock list and finds the superblock of the file system
795 * mounted on the device given. Returns the superblock with an active
796 * reference or %NULL if none was found.
797 */
get_active_super(struct block_device * bdev)798 struct super_block *get_active_super(struct block_device *bdev)
799 {
800 struct super_block *sb;
801
802 if (!bdev)
803 return NULL;
804
805 restart:
806 spin_lock(&sb_lock);
807 list_for_each_entry(sb, &super_blocks, s_list) {
808 if (hlist_unhashed(&sb->s_instances))
809 continue;
810 if (sb->s_bdev == bdev) {
811 if (!grab_super(sb))
812 goto restart;
813 up_write(&sb->s_umount);
814 return sb;
815 }
816 }
817 spin_unlock(&sb_lock);
818 return NULL;
819 }
820
user_get_super(dev_t dev,bool excl)821 struct super_block *user_get_super(dev_t dev, bool excl)
822 {
823 struct super_block *sb;
824
825 spin_lock(&sb_lock);
826 rescan:
827 list_for_each_entry(sb, &super_blocks, s_list) {
828 if (hlist_unhashed(&sb->s_instances))
829 continue;
830 if (sb->s_dev == dev) {
831 sb->s_count++;
832 spin_unlock(&sb_lock);
833 if (excl)
834 down_write(&sb->s_umount);
835 else
836 down_read(&sb->s_umount);
837 /* still alive? */
838 if (sb->s_root && (sb->s_flags & SB_BORN))
839 return sb;
840 if (excl)
841 up_write(&sb->s_umount);
842 else
843 up_read(&sb->s_umount);
844 /* nope, got unmounted */
845 spin_lock(&sb_lock);
846 __put_super(sb);
847 goto rescan;
848 }
849 }
850 spin_unlock(&sb_lock);
851 return NULL;
852 }
853
854 /**
855 * reconfigure_super - asks filesystem to change superblock parameters
856 * @fc: The superblock and configuration
857 *
858 * Alters the configuration parameters of a live superblock.
859 */
reconfigure_super(struct fs_context * fc)860 int reconfigure_super(struct fs_context *fc)
861 {
862 struct super_block *sb = fc->root->d_sb;
863 int retval;
864 bool remount_ro = false;
865 bool remount_rw = false;
866 bool force = fc->sb_flags & SB_FORCE;
867
868 if (fc->sb_flags_mask & ~MS_RMT_MASK)
869 return -EINVAL;
870 if (sb->s_writers.frozen != SB_UNFROZEN)
871 return -EBUSY;
872
873 retval = security_sb_remount(sb, fc->security);
874 if (retval)
875 return retval;
876
877 if (fc->sb_flags_mask & SB_RDONLY) {
878 #ifdef CONFIG_BLOCK
879 if (!(fc->sb_flags & SB_RDONLY) && sb->s_bdev &&
880 bdev_read_only(sb->s_bdev))
881 return -EACCES;
882 #endif
883 remount_rw = !(fc->sb_flags & SB_RDONLY) && sb_rdonly(sb);
884 remount_ro = (fc->sb_flags & SB_RDONLY) && !sb_rdonly(sb);
885 }
886
887 if (remount_ro) {
888 if (!hlist_empty(&sb->s_pins)) {
889 up_write(&sb->s_umount);
890 group_pin_kill(&sb->s_pins);
891 down_write(&sb->s_umount);
892 if (!sb->s_root)
893 return 0;
894 if (sb->s_writers.frozen != SB_UNFROZEN)
895 return -EBUSY;
896 remount_ro = !sb_rdonly(sb);
897 }
898 }
899 shrink_dcache_sb(sb);
900
901 /* If we are reconfiguring to RDONLY and current sb is read/write,
902 * make sure there are no files open for writing.
903 */
904 if (remount_ro) {
905 if (force) {
906 sb->s_readonly_remount = 1;
907 smp_wmb();
908 } else {
909 retval = sb_prepare_remount_readonly(sb);
910 if (retval)
911 return retval;
912 }
913 } else if (remount_rw) {
914 /*
915 * We set s_readonly_remount here to protect filesystem's
916 * reconfigure code from writes from userspace until
917 * reconfigure finishes.
918 */
919 sb->s_readonly_remount = 1;
920 smp_wmb();
921 }
922
923 if (fc->ops->reconfigure) {
924 retval = fc->ops->reconfigure(fc);
925 if (retval) {
926 if (!force)
927 goto cancel_readonly;
928 /* If forced remount, go ahead despite any errors */
929 WARN(1, "forced remount of a %s fs returned %i\n",
930 sb->s_type->name, retval);
931 }
932 }
933
934 WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
935 (fc->sb_flags & fc->sb_flags_mask)));
936 /* Needs to be ordered wrt mnt_is_readonly() */
937 smp_wmb();
938 sb->s_readonly_remount = 0;
939
940 /*
941 * Some filesystems modify their metadata via some other path than the
942 * bdev buffer cache (eg. use a private mapping, or directories in
943 * pagecache, etc). Also file data modifications go via their own
944 * mappings. So If we try to mount readonly then copy the filesystem
945 * from bdev, we could get stale data, so invalidate it to give a best
946 * effort at coherency.
947 */
948 if (remount_ro && sb->s_bdev)
949 invalidate_bdev(sb->s_bdev);
950 return 0;
951
952 cancel_readonly:
953 sb->s_readonly_remount = 0;
954 return retval;
955 }
956
do_emergency_remount_callback(struct super_block * sb)957 static void do_emergency_remount_callback(struct super_block *sb)
958 {
959 down_write(&sb->s_umount);
960 if (sb->s_root && sb->s_bdev && (sb->s_flags & SB_BORN) &&
961 !sb_rdonly(sb)) {
962 struct fs_context *fc;
963
964 fc = fs_context_for_reconfigure(sb->s_root,
965 SB_RDONLY | SB_FORCE, SB_RDONLY);
966 if (!IS_ERR(fc)) {
967 if (parse_monolithic_mount_data(fc, NULL) == 0)
968 (void)reconfigure_super(fc);
969 put_fs_context(fc);
970 }
971 }
972 up_write(&sb->s_umount);
973 }
974
do_emergency_remount(struct work_struct * work)975 static void do_emergency_remount(struct work_struct *work)
976 {
977 __iterate_supers(do_emergency_remount_callback);
978 kfree(work);
979 printk("Emergency Remount complete\n");
980 }
981
emergency_remount(void)982 void emergency_remount(void)
983 {
984 struct work_struct *work;
985
986 work = kmalloc(sizeof(*work), GFP_ATOMIC);
987 if (work) {
988 INIT_WORK(work, do_emergency_remount);
989 schedule_work(work);
990 }
991 }
992
do_thaw_all_callback(struct super_block * sb)993 static void do_thaw_all_callback(struct super_block *sb)
994 {
995 down_write(&sb->s_umount);
996 if (sb->s_root && sb->s_flags & SB_BORN) {
997 emergency_thaw_bdev(sb);
998 thaw_super_locked(sb);
999 } else {
1000 up_write(&sb->s_umount);
1001 }
1002 }
1003
do_thaw_all(struct work_struct * work)1004 static void do_thaw_all(struct work_struct *work)
1005 {
1006 __iterate_supers(do_thaw_all_callback);
1007 kfree(work);
1008 printk(KERN_WARNING "Emergency Thaw complete\n");
1009 }
1010
1011 /**
1012 * emergency_thaw_all -- forcibly thaw every frozen filesystem
1013 *
1014 * Used for emergency unfreeze of all filesystems via SysRq
1015 */
emergency_thaw_all(void)1016 void emergency_thaw_all(void)
1017 {
1018 struct work_struct *work;
1019
1020 work = kmalloc(sizeof(*work), GFP_ATOMIC);
1021 if (work) {
1022 INIT_WORK(work, do_thaw_all);
1023 schedule_work(work);
1024 }
1025 }
1026
1027 static DEFINE_IDA(unnamed_dev_ida);
1028
1029 /**
1030 * get_anon_bdev - Allocate a block device for filesystems which don't have one.
1031 * @p: Pointer to a dev_t.
1032 *
1033 * Filesystems which don't use real block devices can call this function
1034 * to allocate a virtual block device.
1035 *
1036 * Context: Any context. Frequently called while holding sb_lock.
1037 * Return: 0 on success, -EMFILE if there are no anonymous bdevs left
1038 * or -ENOMEM if memory allocation failed.
1039 */
get_anon_bdev(dev_t * p)1040 int get_anon_bdev(dev_t *p)
1041 {
1042 int dev;
1043
1044 /*
1045 * Many userspace utilities consider an FSID of 0 invalid.
1046 * Always return at least 1 from get_anon_bdev.
1047 */
1048 dev = ida_alloc_range(&unnamed_dev_ida, 1, (1 << MINORBITS) - 1,
1049 GFP_ATOMIC);
1050 if (dev == -ENOSPC)
1051 dev = -EMFILE;
1052 if (dev < 0)
1053 return dev;
1054
1055 *p = MKDEV(0, dev);
1056 return 0;
1057 }
1058 EXPORT_SYMBOL(get_anon_bdev);
1059
free_anon_bdev(dev_t dev)1060 void free_anon_bdev(dev_t dev)
1061 {
1062 ida_free(&unnamed_dev_ida, MINOR(dev));
1063 }
1064 EXPORT_SYMBOL(free_anon_bdev);
1065
set_anon_super(struct super_block * s,void * data)1066 int set_anon_super(struct super_block *s, void *data)
1067 {
1068 return get_anon_bdev(&s->s_dev);
1069 }
1070 EXPORT_SYMBOL(set_anon_super);
1071
kill_anon_super(struct super_block * sb)1072 void kill_anon_super(struct super_block *sb)
1073 {
1074 dev_t dev = sb->s_dev;
1075 generic_shutdown_super(sb);
1076 free_anon_bdev(dev);
1077 }
1078 EXPORT_SYMBOL(kill_anon_super);
1079
kill_litter_super(struct super_block * sb)1080 void kill_litter_super(struct super_block *sb)
1081 {
1082 if (sb->s_root)
1083 d_genocide(sb->s_root);
1084 kill_anon_super(sb);
1085 }
1086 EXPORT_SYMBOL(kill_litter_super);
1087
set_anon_super_fc(struct super_block * sb,struct fs_context * fc)1088 int set_anon_super_fc(struct super_block *sb, struct fs_context *fc)
1089 {
1090 return set_anon_super(sb, NULL);
1091 }
1092 EXPORT_SYMBOL(set_anon_super_fc);
1093
test_keyed_super(struct super_block * sb,struct fs_context * fc)1094 static int test_keyed_super(struct super_block *sb, struct fs_context *fc)
1095 {
1096 return sb->s_fs_info == fc->s_fs_info;
1097 }
1098
test_single_super(struct super_block * s,struct fs_context * fc)1099 static int test_single_super(struct super_block *s, struct fs_context *fc)
1100 {
1101 return 1;
1102 }
1103
1104 /**
1105 * vfs_get_super - Get a superblock with a search key set in s_fs_info.
1106 * @fc: The filesystem context holding the parameters
1107 * @keying: How to distinguish superblocks
1108 * @fill_super: Helper to initialise a new superblock
1109 *
1110 * Search for a superblock and create a new one if not found. The search
1111 * criterion is controlled by @keying. If the search fails, a new superblock
1112 * is created and @fill_super() is called to initialise it.
1113 *
1114 * @keying can take one of a number of values:
1115 *
1116 * (1) vfs_get_single_super - Only one superblock of this type may exist on the
1117 * system. This is typically used for special system filesystems.
1118 *
1119 * (2) vfs_get_keyed_super - Multiple superblocks may exist, but they must have
1120 * distinct keys (where the key is in s_fs_info). Searching for the same
1121 * key again will turn up the superblock for that key.
1122 *
1123 * (3) vfs_get_independent_super - Multiple superblocks may exist and are
1124 * unkeyed. Each call will get a new superblock.
1125 *
1126 * A permissions check is made by sget_fc() unless we're getting a superblock
1127 * for a kernel-internal mount or a submount.
1128 */
vfs_get_super(struct fs_context * fc,enum vfs_get_super_keying keying,int (* fill_super)(struct super_block * sb,struct fs_context * fc))1129 int vfs_get_super(struct fs_context *fc,
1130 enum vfs_get_super_keying keying,
1131 int (*fill_super)(struct super_block *sb,
1132 struct fs_context *fc))
1133 {
1134 int (*test)(struct super_block *, struct fs_context *);
1135 struct super_block *sb;
1136 int err;
1137
1138 switch (keying) {
1139 case vfs_get_single_super:
1140 case vfs_get_single_reconf_super:
1141 test = test_single_super;
1142 break;
1143 case vfs_get_keyed_super:
1144 test = test_keyed_super;
1145 break;
1146 case vfs_get_independent_super:
1147 test = NULL;
1148 break;
1149 default:
1150 BUG();
1151 }
1152
1153 sb = sget_fc(fc, test, set_anon_super_fc);
1154 if (IS_ERR(sb))
1155 return PTR_ERR(sb);
1156
1157 if (!sb->s_root) {
1158 err = fill_super(sb, fc);
1159 if (err)
1160 goto error;
1161
1162 sb->s_flags |= SB_ACTIVE;
1163 fc->root = dget(sb->s_root);
1164 } else {
1165 fc->root = dget(sb->s_root);
1166 if (keying == vfs_get_single_reconf_super) {
1167 err = reconfigure_super(fc);
1168 if (err < 0) {
1169 dput(fc->root);
1170 fc->root = NULL;
1171 goto error;
1172 }
1173 }
1174 }
1175
1176 return 0;
1177
1178 error:
1179 deactivate_locked_super(sb);
1180 return err;
1181 }
1182 EXPORT_SYMBOL(vfs_get_super);
1183
get_tree_nodev(struct fs_context * fc,int (* fill_super)(struct super_block * sb,struct fs_context * fc))1184 int get_tree_nodev(struct fs_context *fc,
1185 int (*fill_super)(struct super_block *sb,
1186 struct fs_context *fc))
1187 {
1188 return vfs_get_super(fc, vfs_get_independent_super, fill_super);
1189 }
1190 EXPORT_SYMBOL(get_tree_nodev);
1191
get_tree_single(struct fs_context * fc,int (* fill_super)(struct super_block * sb,struct fs_context * fc))1192 int get_tree_single(struct fs_context *fc,
1193 int (*fill_super)(struct super_block *sb,
1194 struct fs_context *fc))
1195 {
1196 return vfs_get_super(fc, vfs_get_single_super, fill_super);
1197 }
1198 EXPORT_SYMBOL(get_tree_single);
1199
get_tree_single_reconf(struct fs_context * fc,int (* fill_super)(struct super_block * sb,struct fs_context * fc))1200 int get_tree_single_reconf(struct fs_context *fc,
1201 int (*fill_super)(struct super_block *sb,
1202 struct fs_context *fc))
1203 {
1204 return vfs_get_super(fc, vfs_get_single_reconf_super, fill_super);
1205 }
1206 EXPORT_SYMBOL(get_tree_single_reconf);
1207
get_tree_keyed(struct fs_context * fc,int (* fill_super)(struct super_block * sb,struct fs_context * fc),void * key)1208 int get_tree_keyed(struct fs_context *fc,
1209 int (*fill_super)(struct super_block *sb,
1210 struct fs_context *fc),
1211 void *key)
1212 {
1213 fc->s_fs_info = key;
1214 return vfs_get_super(fc, vfs_get_keyed_super, fill_super);
1215 }
1216 EXPORT_SYMBOL(get_tree_keyed);
1217
1218 #ifdef CONFIG_BLOCK
1219
set_bdev_super(struct super_block * s,void * data)1220 static int set_bdev_super(struct super_block *s, void *data)
1221 {
1222 s->s_bdev = data;
1223 s->s_dev = s->s_bdev->bd_dev;
1224 s->s_bdi = bdi_get(s->s_bdev->bd_disk->bdi);
1225
1226 if (blk_queue_stable_writes(s->s_bdev->bd_disk->queue))
1227 s->s_iflags |= SB_I_STABLE_WRITES;
1228 return 0;
1229 }
1230
set_bdev_super_fc(struct super_block * s,struct fs_context * fc)1231 static int set_bdev_super_fc(struct super_block *s, struct fs_context *fc)
1232 {
1233 return set_bdev_super(s, fc->sget_key);
1234 }
1235
test_bdev_super_fc(struct super_block * s,struct fs_context * fc)1236 static int test_bdev_super_fc(struct super_block *s, struct fs_context *fc)
1237 {
1238 return s->s_bdev == fc->sget_key;
1239 }
1240
1241 /**
1242 * get_tree_bdev - Get a superblock based on a single block device
1243 * @fc: The filesystem context holding the parameters
1244 * @fill_super: Helper to initialise a new superblock
1245 */
get_tree_bdev(struct fs_context * fc,int (* fill_super)(struct super_block *,struct fs_context *))1246 int get_tree_bdev(struct fs_context *fc,
1247 int (*fill_super)(struct super_block *,
1248 struct fs_context *))
1249 {
1250 struct block_device *bdev;
1251 struct super_block *s;
1252 fmode_t mode = FMODE_READ | FMODE_EXCL;
1253 int error = 0;
1254
1255 if (!(fc->sb_flags & SB_RDONLY))
1256 mode |= FMODE_WRITE;
1257
1258 if (!fc->source)
1259 return invalf(fc, "No source specified");
1260
1261 bdev = blkdev_get_by_path(fc->source, mode, fc->fs_type);
1262 if (IS_ERR(bdev)) {
1263 errorf(fc, "%s: Can't open blockdev", fc->source);
1264 return PTR_ERR(bdev);
1265 }
1266
1267 /* Once the superblock is inserted into the list by sget_fc(), s_umount
1268 * will protect the lockfs code from trying to start a snapshot while
1269 * we are mounting
1270 */
1271 mutex_lock(&bdev->bd_fsfreeze_mutex);
1272 if (bdev->bd_fsfreeze_count > 0) {
1273 mutex_unlock(&bdev->bd_fsfreeze_mutex);
1274 warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev);
1275 blkdev_put(bdev, mode);
1276 return -EBUSY;
1277 }
1278
1279 fc->sb_flags |= SB_NOSEC;
1280 fc->sget_key = bdev;
1281 s = sget_fc(fc, test_bdev_super_fc, set_bdev_super_fc);
1282 mutex_unlock(&bdev->bd_fsfreeze_mutex);
1283 if (IS_ERR(s)) {
1284 blkdev_put(bdev, mode);
1285 return PTR_ERR(s);
1286 }
1287
1288 if (s->s_root) {
1289 /* Don't summarily change the RO/RW state. */
1290 if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
1291 warnf(fc, "%pg: Can't mount, would change RO state", bdev);
1292 deactivate_locked_super(s);
1293 blkdev_put(bdev, mode);
1294 return -EBUSY;
1295 }
1296
1297 /*
1298 * s_umount nests inside open_mutex during
1299 * __invalidate_device(). blkdev_put() acquires
1300 * open_mutex and can't be called under s_umount. Drop
1301 * s_umount temporarily. This is safe as we're
1302 * holding an active reference.
1303 */
1304 up_write(&s->s_umount);
1305 blkdev_put(bdev, mode);
1306 down_write(&s->s_umount);
1307 } else {
1308 s->s_mode = mode;
1309 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1310 sb_set_blocksize(s, block_size(bdev));
1311 error = fill_super(s, fc);
1312 if (error) {
1313 deactivate_locked_super(s);
1314 return error;
1315 }
1316
1317 s->s_flags |= SB_ACTIVE;
1318 bdev->bd_super = s;
1319 }
1320
1321 BUG_ON(fc->root);
1322 fc->root = dget(s->s_root);
1323 return 0;
1324 }
1325 EXPORT_SYMBOL(get_tree_bdev);
1326
test_bdev_super(struct super_block * s,void * data)1327 static int test_bdev_super(struct super_block *s, void *data)
1328 {
1329 return (void *)s->s_bdev == data;
1330 }
1331
mount_bdev(struct file_system_type * fs_type,int flags,const char * dev_name,void * data,int (* fill_super)(struct super_block *,void *,int))1332 struct dentry *mount_bdev(struct file_system_type *fs_type,
1333 int flags, const char *dev_name, void *data,
1334 int (*fill_super)(struct super_block *, void *, int))
1335 {
1336 struct block_device *bdev;
1337 struct super_block *s;
1338 fmode_t mode = FMODE_READ | FMODE_EXCL;
1339 int error = 0;
1340
1341 if (!(flags & SB_RDONLY))
1342 mode |= FMODE_WRITE;
1343
1344 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1345 if (IS_ERR(bdev))
1346 return ERR_CAST(bdev);
1347
1348 /*
1349 * once the super is inserted into the list by sget, s_umount
1350 * will protect the lockfs code from trying to start a snapshot
1351 * while we are mounting
1352 */
1353 mutex_lock(&bdev->bd_fsfreeze_mutex);
1354 if (bdev->bd_fsfreeze_count > 0) {
1355 mutex_unlock(&bdev->bd_fsfreeze_mutex);
1356 error = -EBUSY;
1357 goto error_bdev;
1358 }
1359 s = sget(fs_type, test_bdev_super, set_bdev_super, flags | SB_NOSEC,
1360 bdev);
1361 mutex_unlock(&bdev->bd_fsfreeze_mutex);
1362 if (IS_ERR(s))
1363 goto error_s;
1364
1365 if (s->s_root) {
1366 if ((flags ^ s->s_flags) & SB_RDONLY) {
1367 deactivate_locked_super(s);
1368 error = -EBUSY;
1369 goto error_bdev;
1370 }
1371
1372 /*
1373 * s_umount nests inside open_mutex during
1374 * __invalidate_device(). blkdev_put() acquires
1375 * open_mutex and can't be called under s_umount. Drop
1376 * s_umount temporarily. This is safe as we're
1377 * holding an active reference.
1378 */
1379 up_write(&s->s_umount);
1380 blkdev_put(bdev, mode);
1381 down_write(&s->s_umount);
1382 } else {
1383 s->s_mode = mode;
1384 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1385 sb_set_blocksize(s, block_size(bdev));
1386 error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1387 if (error) {
1388 deactivate_locked_super(s);
1389 goto error;
1390 }
1391
1392 s->s_flags |= SB_ACTIVE;
1393 bdev->bd_super = s;
1394 }
1395
1396 return dget(s->s_root);
1397
1398 error_s:
1399 error = PTR_ERR(s);
1400 error_bdev:
1401 blkdev_put(bdev, mode);
1402 error:
1403 return ERR_PTR(error);
1404 }
1405 EXPORT_SYMBOL_NS(mount_bdev, ANDROID_GKI_VFS_EXPORT_ONLY);
1406
kill_block_super(struct super_block * sb)1407 void kill_block_super(struct super_block *sb)
1408 {
1409 struct block_device *bdev = sb->s_bdev;
1410 fmode_t mode = sb->s_mode;
1411
1412 bdev->bd_super = NULL;
1413 generic_shutdown_super(sb);
1414 sync_blockdev(bdev);
1415 WARN_ON_ONCE(!(mode & FMODE_EXCL));
1416 blkdev_put(bdev, mode | FMODE_EXCL);
1417 }
1418
1419 EXPORT_SYMBOL_NS(kill_block_super, ANDROID_GKI_VFS_EXPORT_ONLY);
1420 #endif
1421
mount_nodev(struct file_system_type * fs_type,int flags,void * data,int (* fill_super)(struct super_block *,void *,int))1422 struct dentry *mount_nodev(struct file_system_type *fs_type,
1423 int flags, void *data,
1424 int (*fill_super)(struct super_block *, void *, int))
1425 {
1426 int error;
1427 struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
1428
1429 if (IS_ERR(s))
1430 return ERR_CAST(s);
1431
1432 error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1433 if (error) {
1434 deactivate_locked_super(s);
1435 return ERR_PTR(error);
1436 }
1437 s->s_flags |= SB_ACTIVE;
1438 return dget(s->s_root);
1439 }
1440 EXPORT_SYMBOL(mount_nodev);
1441
reconfigure_single(struct super_block * s,int flags,void * data)1442 int reconfigure_single(struct super_block *s,
1443 int flags, void *data)
1444 {
1445 struct fs_context *fc;
1446 int ret;
1447
1448 /* The caller really need to be passing fc down into mount_single(),
1449 * then a chunk of this can be removed. [Bollocks -- AV]
1450 * Better yet, reconfiguration shouldn't happen, but rather the second
1451 * mount should be rejected if the parameters are not compatible.
1452 */
1453 fc = fs_context_for_reconfigure(s->s_root, flags, MS_RMT_MASK);
1454 if (IS_ERR(fc))
1455 return PTR_ERR(fc);
1456
1457 ret = parse_monolithic_mount_data(fc, data);
1458 if (ret < 0)
1459 goto out;
1460
1461 ret = reconfigure_super(fc);
1462 out:
1463 put_fs_context(fc);
1464 return ret;
1465 }
1466
compare_single(struct super_block * s,void * p)1467 static int compare_single(struct super_block *s, void *p)
1468 {
1469 return 1;
1470 }
1471
mount_single(struct file_system_type * fs_type,int flags,void * data,int (* fill_super)(struct super_block *,void *,int))1472 struct dentry *mount_single(struct file_system_type *fs_type,
1473 int flags, void *data,
1474 int (*fill_super)(struct super_block *, void *, int))
1475 {
1476 struct super_block *s;
1477 int error;
1478
1479 s = sget(fs_type, compare_single, set_anon_super, flags, NULL);
1480 if (IS_ERR(s))
1481 return ERR_CAST(s);
1482 if (!s->s_root) {
1483 error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1484 if (!error)
1485 s->s_flags |= SB_ACTIVE;
1486 } else {
1487 error = reconfigure_single(s, flags, data);
1488 }
1489 if (unlikely(error)) {
1490 deactivate_locked_super(s);
1491 return ERR_PTR(error);
1492 }
1493 return dget(s->s_root);
1494 }
1495 EXPORT_SYMBOL(mount_single);
1496
1497 /**
1498 * vfs_get_tree - Get the mountable root
1499 * @fc: The superblock configuration context.
1500 *
1501 * The filesystem is invoked to get or create a superblock which can then later
1502 * be used for mounting. The filesystem places a pointer to the root to be
1503 * used for mounting in @fc->root.
1504 */
vfs_get_tree(struct fs_context * fc)1505 int vfs_get_tree(struct fs_context *fc)
1506 {
1507 struct super_block *sb;
1508 int error;
1509
1510 if (fc->root)
1511 return -EBUSY;
1512
1513 /* Get the mountable root in fc->root, with a ref on the root and a ref
1514 * on the superblock.
1515 */
1516 error = fc->ops->get_tree(fc);
1517 if (error < 0)
1518 return error;
1519
1520 if (!fc->root) {
1521 pr_err("Filesystem %s get_tree() didn't set fc->root\n",
1522 fc->fs_type->name);
1523 /* We don't know what the locking state of the superblock is -
1524 * if there is a superblock.
1525 */
1526 BUG();
1527 }
1528
1529 sb = fc->root->d_sb;
1530 WARN_ON(!sb->s_bdi);
1531
1532 /*
1533 * Write barrier is for super_cache_count(). We place it before setting
1534 * SB_BORN as the data dependency between the two functions is the
1535 * superblock structure contents that we just set up, not the SB_BORN
1536 * flag.
1537 */
1538 smp_wmb();
1539 sb->s_flags |= SB_BORN;
1540
1541 error = security_sb_set_mnt_opts(sb, fc->security, 0, NULL);
1542 if (unlikely(error)) {
1543 fc_drop_locked(fc);
1544 return error;
1545 }
1546
1547 /*
1548 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1549 * but s_maxbytes was an unsigned long long for many releases. Throw
1550 * this warning for a little while to try and catch filesystems that
1551 * violate this rule.
1552 */
1553 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1554 "negative value (%lld)\n", fc->fs_type->name, sb->s_maxbytes);
1555
1556 return 0;
1557 }
1558 EXPORT_SYMBOL(vfs_get_tree);
1559
1560 /*
1561 * Setup private BDI for given superblock. It gets automatically cleaned up
1562 * in generic_shutdown_super().
1563 */
super_setup_bdi_name(struct super_block * sb,char * fmt,...)1564 int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1565 {
1566 struct backing_dev_info *bdi;
1567 int err;
1568 va_list args;
1569
1570 bdi = bdi_alloc(NUMA_NO_NODE);
1571 if (!bdi)
1572 return -ENOMEM;
1573
1574 va_start(args, fmt);
1575 err = bdi_register_va(bdi, fmt, args);
1576 va_end(args);
1577 if (err) {
1578 bdi_put(bdi);
1579 return err;
1580 }
1581 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1582 sb->s_bdi = bdi;
1583
1584 return 0;
1585 }
1586 EXPORT_SYMBOL(super_setup_bdi_name);
1587
1588 /*
1589 * Setup private BDI for given superblock. I gets automatically cleaned up
1590 * in generic_shutdown_super().
1591 */
super_setup_bdi(struct super_block * sb)1592 int super_setup_bdi(struct super_block *sb)
1593 {
1594 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
1595
1596 return super_setup_bdi_name(sb, "%.28s-%ld", sb->s_type->name,
1597 atomic_long_inc_return(&bdi_seq));
1598 }
1599 EXPORT_SYMBOL(super_setup_bdi);
1600
1601 /**
1602 * sb_wait_write - wait until all writers to given file system finish
1603 * @sb: the super for which we wait
1604 * @level: type of writers we wait for (normal vs page fault)
1605 *
1606 * This function waits until there are no writers of given type to given file
1607 * system.
1608 */
sb_wait_write(struct super_block * sb,int level)1609 static void sb_wait_write(struct super_block *sb, int level)
1610 {
1611 percpu_down_write(sb->s_writers.rw_sem + level-1);
1612 }
1613
1614 /*
1615 * We are going to return to userspace and forget about these locks, the
1616 * ownership goes to the caller of thaw_super() which does unlock().
1617 */
lockdep_sb_freeze_release(struct super_block * sb)1618 static void lockdep_sb_freeze_release(struct super_block *sb)
1619 {
1620 int level;
1621
1622 for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--)
1623 percpu_rwsem_release(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
1624 }
1625
1626 /*
1627 * Tell lockdep we are holding these locks before we call ->unfreeze_fs(sb).
1628 */
lockdep_sb_freeze_acquire(struct super_block * sb)1629 static void lockdep_sb_freeze_acquire(struct super_block *sb)
1630 {
1631 int level;
1632
1633 for (level = 0; level < SB_FREEZE_LEVELS; ++level)
1634 percpu_rwsem_acquire(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
1635 }
1636
sb_freeze_unlock(struct super_block * sb,int level)1637 static void sb_freeze_unlock(struct super_block *sb, int level)
1638 {
1639 for (level--; level >= 0; level--)
1640 percpu_up_write(sb->s_writers.rw_sem + level);
1641 }
1642
1643 /**
1644 * freeze_super - lock the filesystem and force it into a consistent state
1645 * @sb: the super to lock
1646 *
1647 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1648 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1649 * -EBUSY.
1650 *
1651 * During this function, sb->s_writers.frozen goes through these values:
1652 *
1653 * SB_UNFROZEN: File system is normal, all writes progress as usual.
1654 *
1655 * SB_FREEZE_WRITE: The file system is in the process of being frozen. New
1656 * writes should be blocked, though page faults are still allowed. We wait for
1657 * all writes to complete and then proceed to the next stage.
1658 *
1659 * SB_FREEZE_PAGEFAULT: Freezing continues. Now also page faults are blocked
1660 * but internal fs threads can still modify the filesystem (although they
1661 * should not dirty new pages or inodes), writeback can run etc. After waiting
1662 * for all running page faults we sync the filesystem which will clean all
1663 * dirty pages and inodes (no new dirty pages or inodes can be created when
1664 * sync is running).
1665 *
1666 * SB_FREEZE_FS: The file system is frozen. Now all internal sources of fs
1667 * modification are blocked (e.g. XFS preallocation truncation on inode
1668 * reclaim). This is usually implemented by blocking new transactions for
1669 * filesystems that have them and need this additional guard. After all
1670 * internal writers are finished we call ->freeze_fs() to finish filesystem
1671 * freezing. Then we transition to SB_FREEZE_COMPLETE state. This state is
1672 * mostly auxiliary for filesystems to verify they do not modify frozen fs.
1673 *
1674 * sb->s_writers.frozen is protected by sb->s_umount.
1675 */
freeze_super(struct super_block * sb)1676 int freeze_super(struct super_block *sb)
1677 {
1678 int ret;
1679
1680 atomic_inc(&sb->s_active);
1681 down_write(&sb->s_umount);
1682 if (sb->s_writers.frozen != SB_UNFROZEN) {
1683 deactivate_locked_super(sb);
1684 return -EBUSY;
1685 }
1686
1687 if (!(sb->s_flags & SB_BORN)) {
1688 up_write(&sb->s_umount);
1689 return 0; /* sic - it's "nothing to do" */
1690 }
1691
1692 if (sb_rdonly(sb)) {
1693 /* Nothing to do really... */
1694 sb->s_writers.frozen = SB_FREEZE_COMPLETE;
1695 up_write(&sb->s_umount);
1696 return 0;
1697 }
1698
1699 sb->s_writers.frozen = SB_FREEZE_WRITE;
1700 /* Release s_umount to preserve sb_start_write -> s_umount ordering */
1701 up_write(&sb->s_umount);
1702 sb_wait_write(sb, SB_FREEZE_WRITE);
1703 down_write(&sb->s_umount);
1704
1705 /* Now we go and block page faults... */
1706 sb->s_writers.frozen = SB_FREEZE_PAGEFAULT;
1707 sb_wait_write(sb, SB_FREEZE_PAGEFAULT);
1708
1709 /* All writers are done so after syncing there won't be dirty data */
1710 ret = sync_filesystem(sb);
1711 if (ret) {
1712 sb->s_writers.frozen = SB_UNFROZEN;
1713 sb_freeze_unlock(sb, SB_FREEZE_PAGEFAULT);
1714 wake_up(&sb->s_writers.wait_unfrozen);
1715 deactivate_locked_super(sb);
1716 return ret;
1717 }
1718
1719 /* Now wait for internal filesystem counter */
1720 sb->s_writers.frozen = SB_FREEZE_FS;
1721 sb_wait_write(sb, SB_FREEZE_FS);
1722
1723 if (sb->s_op->freeze_fs) {
1724 ret = sb->s_op->freeze_fs(sb);
1725 if (ret) {
1726 printk(KERN_ERR
1727 "VFS:Filesystem freeze failed\n");
1728 sb->s_writers.frozen = SB_UNFROZEN;
1729 sb_freeze_unlock(sb, SB_FREEZE_FS);
1730 wake_up(&sb->s_writers.wait_unfrozen);
1731 deactivate_locked_super(sb);
1732 return ret;
1733 }
1734 }
1735 /*
1736 * For debugging purposes so that fs can warn if it sees write activity
1737 * when frozen is set to SB_FREEZE_COMPLETE, and for thaw_super().
1738 */
1739 sb->s_writers.frozen = SB_FREEZE_COMPLETE;
1740 lockdep_sb_freeze_release(sb);
1741 up_write(&sb->s_umount);
1742 return 0;
1743 }
1744 EXPORT_SYMBOL(freeze_super);
1745
thaw_super_locked(struct super_block * sb)1746 static int thaw_super_locked(struct super_block *sb)
1747 {
1748 int error;
1749
1750 if (sb->s_writers.frozen != SB_FREEZE_COMPLETE) {
1751 up_write(&sb->s_umount);
1752 return -EINVAL;
1753 }
1754
1755 if (sb_rdonly(sb)) {
1756 sb->s_writers.frozen = SB_UNFROZEN;
1757 goto out;
1758 }
1759
1760 lockdep_sb_freeze_acquire(sb);
1761
1762 if (sb->s_op->unfreeze_fs) {
1763 error = sb->s_op->unfreeze_fs(sb);
1764 if (error) {
1765 printk(KERN_ERR
1766 "VFS:Filesystem thaw failed\n");
1767 lockdep_sb_freeze_release(sb);
1768 up_write(&sb->s_umount);
1769 return error;
1770 }
1771 }
1772
1773 sb->s_writers.frozen = SB_UNFROZEN;
1774 sb_freeze_unlock(sb, SB_FREEZE_FS);
1775 out:
1776 wake_up(&sb->s_writers.wait_unfrozen);
1777 deactivate_locked_super(sb);
1778 return 0;
1779 }
1780
1781 /**
1782 * thaw_super -- unlock filesystem
1783 * @sb: the super to thaw
1784 *
1785 * Unlocks the filesystem and marks it writeable again after freeze_super().
1786 */
thaw_super(struct super_block * sb)1787 int thaw_super(struct super_block *sb)
1788 {
1789 down_write(&sb->s_umount);
1790 return thaw_super_locked(sb);
1791 }
1792 EXPORT_SYMBOL(thaw_super);
1793