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