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