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