super.c 35.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  linux/fs/super.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  super.c contains code to handle: - mount structures
 *                                   - super-block tables
 *                                   - filesystem drivers list
 *                                   - mount system call
 *                                   - umount system call
 *                                   - ustat system call
 *
 * GK 2/5/95  -  Changed to support mounting the root fs via NFS
 *
 *  Added kerneld support: Jacques Gelinas and Bjorn Ekwall
 *  Added change_root: Werner Almesberger & Hans Lermen, Feb '96
 *  Added options to /proc/mounts:
18
 *    Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
Linus Torvalds's avatar
Linus Torvalds committed
19 20 21 22
 *  Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
 *  Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
 */

23
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25 26 27 28 29
#include <linux/slab.h>
#include <linux/blkdev.h>
#include <linux/mount.h>
#include <linux/security.h>
#include <linux/writeback.h>		/* for the emergency remount stuff */
#include <linux/idr.h>
Ingo Molnar's avatar
Ingo Molnar committed
30
#include <linux/mutex.h>
31
#include <linux/backing-dev.h>
32
#include <linux/rculist_bl.h>
33
#include <linux/cleancache.h>
Al Viro's avatar
Al Viro committed
34
#include <linux/fsnotify.h>
35
#include <linux/lockdep.h>
36
#include "internal.h"
Linus Torvalds's avatar
Linus Torvalds committed
37 38


39 40
static LIST_HEAD(super_blocks);
static DEFINE_SPINLOCK(sb_lock);
Linus Torvalds's avatar
Linus Torvalds committed
41

42 43 44 45 46 47
static char *sb_writers_name[SB_FREEZE_LEVELS] = {
	"sb_writers",
	"sb_pagefaults",
	"sb_internal",
};

48 49 50 51 52 53 54
/*
 * One thing we have to be careful of with a per-sb shrinker is that we don't
 * drop the last active reference to the superblock from within the shrinker.
 * If that happens we could trigger unregistering the shrinker from within the
 * shrinker path and that leads to deadlock on the shrinker_rwsem. Hence we
 * take a passive reference to the superblock to avoid this from occurring.
 */
55 56
static unsigned long super_cache_scan(struct shrinker *shrink,
				      struct shrink_control *sc)
57 58
{
	struct super_block *sb;
59 60 61 62 63
	long	fs_objects = 0;
	long	total_objects;
	long	freed = 0;
	long	dentries;
	long	inodes;
64 65 66 67 68 69 70

	sb = container_of(shrink, struct super_block, s_shrink);

	/*
	 * Deadlock avoidance.  We may hold various FS locks, and we don't want
	 * to recurse into the FS that called us in clear_inode() and friends..
	 */
71 72
	if (!(sc->gfp_mask & __GFP_FS))
		return SHRINK_STOP;
73

74
	if (!trylock_super(sb))
75
		return SHRINK_STOP;
76

77
	if (sb->s_op->nr_cached_objects)
78
		fs_objects = sb->s_op->nr_cached_objects(sb, sc);
79

80 81
	inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
	dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc);
82
	total_objects = dentries + inodes + fs_objects + 1;
83 84
	if (!total_objects)
		total_objects = 1;
85

86
	/* proportion the scan between the caches */
87
	dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
88
	inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
89
	fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
90

91 92 93
	/*
	 * prune the dcache first as the icache is pinned by it, then
	 * prune the icache, followed by the filesystem specific caches
94 95 96
	 *
	 * Ensure that we always scan at least one object - memcg kmem
	 * accounting uses this to fully empty the caches.
97
	 */
98
	sc->nr_to_scan = dentries + 1;
99
	freed = prune_dcache_sb(sb, sc);
100
	sc->nr_to_scan = inodes + 1;
101
	freed += prune_icache_sb(sb, sc);
102 103

	if (fs_objects) {
104
		sc->nr_to_scan = fs_objects + 1;
105
		freed += sb->s_op->free_cached_objects(sb, sc);
106 107
	}

108
	up_read(&sb->s_umount);
109 110 111 112 113 114 115 116 117 118 119
	return freed;
}

static unsigned long super_cache_count(struct shrinker *shrink,
				       struct shrink_control *sc)
{
	struct super_block *sb;
	long	total_objects = 0;

	sb = container_of(shrink, struct super_block, s_shrink);

120
	/*
121 122 123 124 125 126 127 128 129 130 131 132
	 * We don't call trylock_super() here as it is a scalability bottleneck,
	 * so we're exposed to partial setup state. The shrinker rwsem does not
	 * protect filesystem operations backing list_lru_shrink_count() or
	 * s_op->nr_cached_objects(). Counts can change between
	 * super_cache_count and super_cache_scan, so we really don't need locks
	 * here.
	 *
	 * However, if we are currently mounting the superblock, the underlying
	 * filesystem might be in a state of partial construction and hence it
	 * is dangerous to access it.  trylock_super() uses a MS_BORN check to
	 * avoid this situation, so do the same here. The memory barrier is
	 * matched with the one in mount_fs() as we don't hold locks here.
133
	 */
134 135 136 137
	if (!(sb->s_flags & MS_BORN))
		return 0;
	smp_rmb();

138
	if (sb->s_op && sb->s_op->nr_cached_objects)
139
		total_objects = sb->s_op->nr_cached_objects(sb, sc);
140

141 142
	total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
	total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
143

144
	total_objects = vfs_pressure_ratio(total_objects);
145
	return total_objects;
146 147
}

148 149 150 151 152 153 154
static void destroy_super_work(struct work_struct *work)
{
	struct super_block *s = container_of(work, struct super_block,
							destroy_work);
	int i;

	for (i = 0; i < SB_FREEZE_LEVELS; i++)
155
		percpu_free_rwsem(&s->s_writers.rw_sem[i]);
156 157 158 159 160 161 162 163 164 165
	kfree(s);
}

static void destroy_super_rcu(struct rcu_head *head)
{
	struct super_block *s = container_of(head, struct super_block, rcu);
	INIT_WORK(&s->destroy_work, destroy_super_work);
	schedule_work(&s->destroy_work);
}

166 167 168 169 170 171 172
/**
 *	destroy_super	-	frees a superblock
 *	@s: superblock to free
 *
 *	Frees a superblock.
 */
static void destroy_super(struct super_block *s)
173
{
174 175 176 177 178 179
	list_lru_destroy(&s->s_dentry_lru);
	list_lru_destroy(&s->s_inode_lru);
	security_sb_free(s);
	WARN_ON(!list_empty(&s->s_mounts));
	kfree(s->s_subtype);
	kfree(s->s_options);
180
	call_rcu(&s->rcu, destroy_super_rcu);
181 182
}

Linus Torvalds's avatar
Linus Torvalds committed
183 184
/**
 *	alloc_super	-	create new superblock
185
 *	@type:	filesystem type superblock should belong to
186
 *	@flags: the mount flags
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190
 *
 *	Allocates and initializes a new &struct super_block.  alloc_super()
 *	returns a pointer new superblock or %NULL if allocation had failed.
 */
191
static struct super_block *alloc_super(struct file_system_type *type, int flags)
Linus Torvalds's avatar
Linus Torvalds committed
192
{
193
	struct super_block *s = kzalloc(sizeof(struct super_block),  GFP_USER);
194
	static const struct super_operations default_op;
195 196 197 198
	int i;

	if (!s)
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
199

200 201
	INIT_LIST_HEAD(&s->s_mounts);

202 203
	if (security_sb_alloc(s))
		goto fail;
204

205
	for (i = 0; i < SB_FREEZE_LEVELS; i++) {
206 207 208
		if (__percpu_init_rwsem(&s->s_writers.rw_sem[i],
					sb_writers_name[i],
					&type->s_writers_key[i]))
209
			goto fail;
Linus Torvalds's avatar
Linus Torvalds committed
210
	}
211
	init_waitqueue_head(&s->s_writers.wait_unfrozen);
212
	s->s_bdi = &noop_backing_dev_info;
213 214 215
	s->s_flags = flags;
	INIT_HLIST_NODE(&s->s_instances);
	INIT_HLIST_BL_HEAD(&s->s_anon);
216
	mutex_init(&s->s_sync_lock);
217
	INIT_LIST_HEAD(&s->s_inodes);
218
	spin_lock_init(&s->s_inode_list_lock);
219

220
	if (list_lru_init_memcg(&s->s_dentry_lru))
221
		goto fail;
222
	if (list_lru_init_memcg(&s->s_inode_lru))
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		goto fail;

	init_rwsem(&s->s_umount);
	lockdep_set_class(&s->s_umount, &type->s_umount_key);
	/*
	 * sget() can have s_umount recursion.
	 *
	 * When it cannot find a suitable sb, it allocates a new
	 * one (this one), and tries again to find a suitable old
	 * one.
	 *
	 * In case that succeeds, it will acquire the s_umount
	 * lock of the old one. Since these are clearly distrinct
	 * locks, and this object isn't exposed yet, there's no
	 * risk of deadlocks.
	 *
	 * Annotate this by putting this lock in a different
	 * subclass.
	 */
	down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
	s->s_count = 1;
	atomic_set(&s->s_active, 1);
	mutex_init(&s->s_vfs_rename_mutex);
	lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
	mutex_init(&s->s_dquot.dqio_mutex);
	mutex_init(&s->s_dquot.dqonoff_mutex);
	s->s_maxbytes = MAX_NON_LFS;
	s->s_op = &default_op;
	s->s_time_gran = 1000000000;
252
	s->cleancache_poolid = CLEANCACHE_NO_POOL;
253 254 255 256 257

	s->s_shrink.seeks = DEFAULT_SEEKS;
	s->s_shrink.scan_objects = super_cache_scan;
	s->s_shrink.count_objects = super_cache_count;
	s->s_shrink.batch = 1024;
258
	s->s_shrink.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE;
Linus Torvalds's avatar
Linus Torvalds committed
259
	return s;
260

261 262 263
fail:
	destroy_super(s);
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268
}

/* Superblock refcounting  */

/*
269
 * Drop a superblock's refcount.  The caller must hold sb_lock.
Linus Torvalds's avatar
Linus Torvalds committed
270
 */
Al Viro's avatar
Al Viro committed
271
static void __put_super(struct super_block *sb)
Linus Torvalds's avatar
Linus Torvalds committed
272 273
{
	if (!--sb->s_count) {
274
		list_del_init(&sb->s_list);
Linus Torvalds's avatar
Linus Torvalds committed
275 276 277 278 279 280 281 282 283 284 285
		destroy_super(sb);
	}
}

/**
 *	put_super	-	drop a temporary reference to superblock
 *	@sb: superblock in question
 *
 *	Drops a temporary reference, frees superblock if there's no
 *	references left.
 */
Al Viro's avatar
Al Viro committed
286
static void put_super(struct super_block *sb)
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289 290 291 292 293 294
{
	spin_lock(&sb_lock);
	__put_super(sb);
	spin_unlock(&sb_lock);
}


/**
295
 *	deactivate_locked_super	-	drop an active reference to superblock
Linus Torvalds's avatar
Linus Torvalds committed
296 297
 *	@s: superblock to deactivate
 *
298 299
 *	Drops an active reference to superblock, converting it into a temprory
 *	one if there is no other active references left.  In that case we
Linus Torvalds's avatar
Linus Torvalds committed
300 301
 *	tell fs driver to shut it down and drop the temporary reference we
 *	had just acquired.
302 303
 *
 *	Caller holds exclusive lock on superblock; that lock is released.
Linus Torvalds's avatar
Linus Torvalds committed
304
 */
305
void deactivate_locked_super(struct super_block *s)
Linus Torvalds's avatar
Linus Torvalds committed
306 307
{
	struct file_system_type *fs = s->s_type;
Al Viro's avatar
Al Viro committed
308
	if (atomic_dec_and_test(&s->s_active)) {
309
		cleancache_invalidate_fs(s);
310
		unregister_shrinker(&s->s_shrink);
311
		fs->kill_sb(s);
Glauber Costa's avatar
Glauber Costa committed
312

313 314 315 316 317 318 319 320
		/*
		 * Since list_lru_destroy() may sleep, we cannot call it from
		 * put_super(), where we hold the sb_lock. Therefore we destroy
		 * the lru lists right now.
		 */
		list_lru_destroy(&s->s_dentry_lru);
		list_lru_destroy(&s->s_inode_lru);

Linus Torvalds's avatar
Linus Torvalds committed
321 322
		put_filesystem(fs);
		put_super(s);
323 324
	} else {
		up_write(&s->s_umount);
Linus Torvalds's avatar
Linus Torvalds committed
325 326 327
	}
}

328
EXPORT_SYMBOL(deactivate_locked_super);
Linus Torvalds's avatar
Linus Torvalds committed
329

330
/**
331
 *	deactivate_super	-	drop an active reference to superblock
332 333
 *	@s: superblock to deactivate
 *
334 335 336
 *	Variant of deactivate_locked_super(), except that superblock is *not*
 *	locked by caller.  If we are going to drop the final active reference,
 *	lock will be acquired prior to that.
337
 */
338
void deactivate_super(struct super_block *s)
339
{
340 341 342
        if (!atomic_add_unless(&s->s_active, -1, 1)) {
		down_write(&s->s_umount);
		deactivate_locked_super(s);
343 344 345
	}
}

346
EXPORT_SYMBOL(deactivate_super);
347

Linus Torvalds's avatar
Linus Torvalds committed
348 349 350 351 352 353 354 355 356
/**
 *	grab_super - acquire an active reference
 *	@s: reference we are trying to make active
 *
 *	Tries to acquire an active reference.  grab_super() is used when we
 * 	had just found a superblock in super_blocks or fs_type->fs_supers
 *	and want to turn it into a full-blown active reference.  grab_super()
 *	is called with sb_lock held and drops it.  Returns 1 in case of
 *	success, 0 if we had failed (superblock contents was already dead or
Al Viro's avatar
Al Viro committed
357 358 359
 *	dying when grab_super() had been called).  Note that this is only
 *	called for superblocks not in rundown mode (== ones still on ->fs_supers
 *	of their type), so increment of ->s_count is OK here.
Linus Torvalds's avatar
Linus Torvalds committed
360
 */
361
static int grab_super(struct super_block *s) __releases(sb_lock)
Linus Torvalds's avatar
Linus Torvalds committed
362 363 364 365
{
	s->s_count++;
	spin_unlock(&sb_lock);
	down_write(&s->s_umount);
Al Viro's avatar
Al Viro committed
366 367 368 369
	if ((s->s_flags & MS_BORN) && atomic_inc_not_zero(&s->s_active)) {
		put_super(s);
		return 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
370 371 372 373 374
	up_write(&s->s_umount);
	put_super(s);
	return 0;
}

375
/*
376
 *	trylock_super - try to grab ->s_umount shared
377
 *	@sb: reference we are trying to grab
378
 *
379
 *	Try to prevent fs shutdown.  This is used in places where we
380
 *	cannot take an active reference but we need to ensure that the
381 382 383 384 385 386 387 388 389 390
 *	filesystem is not shut down while we are working on it. It returns
 *	false if we cannot acquire s_umount or if we lose the race and
 *	filesystem already got into shutdown, and returns true with the s_umount
 *	lock held in read mode in case of success. On successful return,
 *	the caller must drop the s_umount lock when done.
 *
 *	Note that unlike get_super() et.al. this one does *not* bump ->s_count.
 *	The reason why it's safe is that we are OK with doing trylock instead
 *	of down_read().  There's a couple of places that are OK with that, but
 *	it's very much not a general-purpose interface.
391
 */
392
bool trylock_super(struct super_block *sb)
393 394
{
	if (down_read_trylock(&sb->s_umount)) {
395 396
		if (!hlist_unhashed(&sb->s_instances) &&
		    sb->s_root && (sb->s_flags & MS_BORN))
397 398 399 400 401 402 403
			return true;
		up_read(&sb->s_umount);
	}

	return false;
}

Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407 408 409 410 411 412
/**
 *	generic_shutdown_super	-	common helper for ->kill_sb()
 *	@sb: superblock to kill
 *
 *	generic_shutdown_super() does all fs-independent work on superblock
 *	shutdown.  Typical ->kill_sb() should pick all fs-specific objects
 *	that need destruction out of superblock, call generic_shutdown_super()
 *	and release aforementioned objects.  Note: dentries and inodes _are_
 *	taken care of and do not need specific handling.
413 414 415 416
 *
 *	Upon calling this function, the filesystem may no longer alter or
 *	rearrange the set of dentries belonging to this super_block, nor may it
 *	change the attachments of dentries to inodes.
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419
 */
void generic_shutdown_super(struct super_block *sb)
{
420
	const struct super_operations *sop = sb->s_op;
Linus Torvalds's avatar
Linus Torvalds committed
421

422 423
	if (sb->s_root) {
		shrink_dcache_for_umount(sb);
424
		sync_filesystem(sb);
Linus Torvalds's avatar
Linus Torvalds committed
425
		sb->s_flags &= ~MS_ACTIVE;
426

427
		fsnotify_unmount_inodes(sb);
428
		cgroup_writeback_umount();
Al Viro's avatar
Al Viro committed
429 430

		evict_inodes(sb);
Linus Torvalds's avatar
Linus Torvalds committed
431

432 433 434 435 436
		if (sb->s_dio_done_wq) {
			destroy_workqueue(sb->s_dio_done_wq);
			sb->s_dio_done_wq = NULL;
		}

Linus Torvalds's avatar
Linus Torvalds committed
437 438 439
		if (sop->put_super)
			sop->put_super(sb);

Al Viro's avatar
Al Viro committed
440
		if (!list_empty(&sb->s_inodes)) {
441 442 443
			printk("VFS: Busy inodes after unmount of %s. "
			   "Self-destruct in 5 seconds.  Have a nice day...\n",
			   sb->s_id);
Linus Torvalds's avatar
Linus Torvalds committed
444 445 446 447
		}
	}
	spin_lock(&sb_lock);
	/* should be initialized for __put_super_and_need_restart() */
Al Viro's avatar
Al Viro committed
448
	hlist_del_init(&sb->s_instances);
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451 452 453 454 455 456 457 458 459
	spin_unlock(&sb_lock);
	up_write(&sb->s_umount);
}

EXPORT_SYMBOL(generic_shutdown_super);

/**
 *	sget	-	find or create a superblock
 *	@type:	filesystem type superblock should belong to
 *	@test:	comparison callback
 *	@set:	setup callback
460
 *	@flags:	mount flags
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465
 *	@data:	argument to each of them
 */
struct super_block *sget(struct file_system_type *type,
			int (*test)(struct super_block *,void *),
			int (*set)(struct super_block *,void *),
466
			int flags,
Linus Torvalds's avatar
Linus Torvalds committed
467 468 469
			void *data)
{
	struct super_block *s = NULL;
470
	struct super_block *old;
Linus Torvalds's avatar
Linus Torvalds committed
471 472 473 474
	int err;

retry:
	spin_lock(&sb_lock);
475
	if (test) {
476
		hlist_for_each_entry(old, &type->fs_supers, s_instances) {
477 478 479 480
			if (!test(old, data))
				continue;
			if (!grab_super(old))
				goto retry;
Li Zefan's avatar
Li Zefan committed
481 482
			if (s) {
				up_write(&s->s_umount);
483
				destroy_super(s);
Al Viro's avatar
Al Viro committed
484
				s = NULL;
Li Zefan's avatar
Li Zefan committed
485
			}
486 487
			return old;
		}
Linus Torvalds's avatar
Linus Torvalds committed
488 489 490
	}
	if (!s) {
		spin_unlock(&sb_lock);
491
		s = alloc_super(type, flags);
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494 495 496 497 498 499
		if (!s)
			return ERR_PTR(-ENOMEM);
		goto retry;
	}
		
	err = set(s, data);
	if (err) {
		spin_unlock(&sb_lock);
Li Zefan's avatar
Li Zefan committed
500
		up_write(&s->s_umount);
Linus Torvalds's avatar
Linus Torvalds committed
501 502 503 504 505 506
		destroy_super(s);
		return ERR_PTR(err);
	}
	s->s_type = type;
	strlcpy(s->s_id, type->name, sizeof(s->s_id));
	list_add_tail(&s->s_list, &super_blocks);
Al Viro's avatar
Al Viro committed
507
	hlist_add_head(&s->s_instances, &type->fs_supers);
Linus Torvalds's avatar
Linus Torvalds committed
508 509
	spin_unlock(&sb_lock);
	get_filesystem(type);
510 511 512 513 514
	err = register_shrinker(&s->s_shrink);
	if (err) {
		deactivate_locked_super(s);
		s = ERR_PTR(err);
	}
Linus Torvalds's avatar
Linus Torvalds committed
515 516 517 518 519 520 521 522 523 524 525 526 527
	return s;
}

EXPORT_SYMBOL(sget);

void drop_super(struct super_block *sb)
{
	up_read(&sb->s_umount);
	put_super(sb);
}

EXPORT_SYMBOL(drop_super);

Al Viro's avatar
Al Viro committed
528 529 530 531 532 533 534 535 536 537
/**
 *	iterate_supers - call function for all active superblocks
 *	@f: function to call
 *	@arg: argument to pass to it
 *
 *	Scans the superblock list and calls given function, passing it
 *	locked superblock and given argument.
 */
void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
{
538
	struct super_block *sb, *p = NULL;
Al Viro's avatar
Al Viro committed
539 540

	spin_lock(&sb_lock);
541
	list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro's avatar
Al Viro committed
542
		if (hlist_unhashed(&sb->s_instances))
Al Viro's avatar
Al Viro committed
543 544 545 546 547
			continue;
		sb->s_count++;
		spin_unlock(&sb_lock);

		down_read(&sb->s_umount);
Al Viro's avatar
Al Viro committed
548
		if (sb->s_root && (sb->s_flags & MS_BORN))
Al Viro's avatar
Al Viro committed
549 550 551 552
			f(sb, arg);
		up_read(&sb->s_umount);

		spin_lock(&sb_lock);
553 554 555
		if (p)
			__put_super(p);
		p = sb;
Al Viro's avatar
Al Viro committed
556
	}
557 558
	if (p)
		__put_super(p);
Al Viro's avatar
Al Viro committed
559 560 561
	spin_unlock(&sb_lock);
}

Al Viro's avatar
Al Viro committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
/**
 *	iterate_supers_type - call function for superblocks of given type
 *	@type: fs type
 *	@f: function to call
 *	@arg: argument to pass to it
 *
 *	Scans the superblock list and calls given function, passing it
 *	locked superblock and given argument.
 */
void iterate_supers_type(struct file_system_type *type,
	void (*f)(struct super_block *, void *), void *arg)
{
	struct super_block *sb, *p = NULL;

	spin_lock(&sb_lock);
577
	hlist_for_each_entry(sb, &type->fs_supers, s_instances) {
Al Viro's avatar
Al Viro committed
578 579 580 581
		sb->s_count++;
		spin_unlock(&sb_lock);

		down_read(&sb->s_umount);
Al Viro's avatar
Al Viro committed
582
		if (sb->s_root && (sb->s_flags & MS_BORN))
Al Viro's avatar
Al Viro committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
			f(sb, arg);
		up_read(&sb->s_umount);

		spin_lock(&sb_lock);
		if (p)
			__put_super(p);
		p = sb;
	}
	if (p)
		__put_super(p);
	spin_unlock(&sb_lock);
}

EXPORT_SYMBOL(iterate_supers_type);

Linus Torvalds's avatar
Linus Torvalds committed
598 599 600 601 602 603 604 605
/**
 *	get_super - get the superblock of a device
 *	@bdev: device to get the superblock for
 *	
 *	Scans the superblock list and finds the superblock of the file system
 *	mounted on the device given. %NULL is returned if no match is found.
 */

606
struct super_block *get_super(struct block_device *bdev)
Linus Torvalds's avatar
Linus Torvalds committed
607
{
608 609
	struct super_block *sb;

Linus Torvalds's avatar
Linus Torvalds committed
610 611
	if (!bdev)
		return NULL;
612

Linus Torvalds's avatar
Linus Torvalds committed
613
	spin_lock(&sb_lock);
614 615
rescan:
	list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro's avatar
Al Viro committed
616
		if (hlist_unhashed(&sb->s_instances))
617
			continue;
618 619
		if (sb->s_bdev == bdev) {
			sb->s_count++;
Linus Torvalds's avatar
Linus Torvalds committed
620
			spin_unlock(&sb_lock);
621
			down_read(&sb->s_umount);
622
			/* still alive? */
Al Viro's avatar
Al Viro committed
623
			if (sb->s_root && (sb->s_flags & MS_BORN))
624 625
				return sb;
			up_read(&sb->s_umount);
626
			/* nope, got unmounted */
627
			spin_lock(&sb_lock);
628 629
			__put_super(sb);
			goto rescan;
Linus Torvalds's avatar
Linus Torvalds committed
630 631 632 633 634 635 636
		}
	}
	spin_unlock(&sb_lock);
	return NULL;
}

EXPORT_SYMBOL(get_super);
637

638 639 640 641 642 643 644 645 646 647 648 649 650
/**
 *	get_super_thawed - get thawed superblock of a device
 *	@bdev: device to get the superblock for
 *
 *	Scans the superblock list and finds the superblock of the file system
 *	mounted on the device. The superblock is returned once it is thawed
 *	(or immediately if it was not frozen). %NULL is returned if no match
 *	is found.
 */
struct super_block *get_super_thawed(struct block_device *bdev)
{
	while (1) {
		struct super_block *s = get_super(bdev);
651
		if (!s || s->s_writers.frozen == SB_UNFROZEN)
652 653
			return s;
		up_read(&s->s_umount);
654 655
		wait_event(s->s_writers.wait_unfrozen,
			   s->s_writers.frozen == SB_UNFROZEN);
656 657 658 659 660
		put_super(s);
	}
}
EXPORT_SYMBOL(get_super_thawed);

661 662 663 664 665 666
/**
 * get_active_super - get an active reference to the superblock of a device
 * @bdev: device to get the superblock for
 *
 * Scans the superblock list and finds the superblock of the file system
 * mounted on the device given.  Returns the superblock with an active
667
 * reference or %NULL if none was found.
668 669 670 671 672 673 674 675
 */
struct super_block *get_active_super(struct block_device *bdev)
{
	struct super_block *sb;

	if (!bdev)
		return NULL;

676
restart:
677 678
	spin_lock(&sb_lock);
	list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro's avatar
Al Viro committed
679
		if (hlist_unhashed(&sb->s_instances))
680
			continue;
681
		if (sb->s_bdev == bdev) {
Al Viro's avatar
Al Viro committed
682
			if (!grab_super(sb))
683
				goto restart;
Al Viro's avatar
Al Viro committed
684 685
			up_write(&sb->s_umount);
			return sb;
686
		}
687 688 689 690
	}
	spin_unlock(&sb_lock);
	return NULL;
}
Linus Torvalds's avatar
Linus Torvalds committed
691
 
692
struct super_block *user_get_super(dev_t dev)
Linus Torvalds's avatar
Linus Torvalds committed
693
{
694
	struct super_block *sb;
Linus Torvalds's avatar
Linus Torvalds committed
695 696

	spin_lock(&sb_lock);
697 698
rescan:
	list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro's avatar
Al Viro committed
699
		if (hlist_unhashed(&sb->s_instances))
700
			continue;
701 702
		if (sb->s_dev ==  dev) {
			sb->s_count++;
Linus Torvalds's avatar
Linus Torvalds committed
703
			spin_unlock(&sb_lock);
704
			down_read(&sb->s_umount);
705
			/* still alive? */
Al Viro's avatar
Al Viro committed
706
			if (sb->s_root && (sb->s_flags & MS_BORN))
707 708
				return sb;
			up_read(&sb->s_umount);
709
			/* nope, got unmounted */
710
			spin_lock(&sb_lock);
711 712
			__put_super(sb);
			goto rescan;
Linus Torvalds's avatar
Linus Torvalds committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
		}
	}
	spin_unlock(&sb_lock);
	return NULL;
}

/**
 *	do_remount_sb - asks filesystem to change mount options.
 *	@sb:	superblock in question
 *	@flags:	numeric part of options
 *	@data:	the rest of options
 *      @force: whether or not to force the change
 *
 *	Alters the mount options of a mounted file system.
 */
int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
{
	int retval;
731
	int remount_ro;
732

733
	if (sb->s_writers.frozen != SB_UNFROZEN)
734 735
		return -EBUSY;

736
#ifdef CONFIG_BLOCK
Linus Torvalds's avatar
Linus Torvalds committed
737 738
	if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
		return -EACCES;
739
#endif
740

741 742
	remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);

743
	if (remount_ro) {
744
		if (!hlist_empty(&sb->s_pins)) {
745
			up_write(&sb->s_umount);
746
			group_pin_kill(&sb->s_pins);
747 748 749 750 751 752 753 754 755 756
			down_write(&sb->s_umount);
			if (!sb->s_root)
				return 0;
			if (sb->s_writers.frozen != SB_UNFROZEN)
				return -EBUSY;
			remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
		}
	}
	shrink_dcache_sb(sb);

Linus Torvalds's avatar
Linus Torvalds committed
757 758
	/* If we are remounting RDONLY and current sb is read/write,
	   make sure there are no rw files opened */
759
	if (remount_ro) {
760
		if (force) {
Al Viro's avatar
Al Viro committed
761 762
			sb->s_readonly_remount = 1;
			smp_wmb();
763 764 765 766 767
		} else {
			retval = sb_prepare_remount_readonly(sb);
			if (retval)
				return retval;
		}
Linus Torvalds's avatar
Linus Torvalds committed
768 769 770 771
	}

	if (sb->s_op->remount_fs) {
		retval = sb->s_op->remount_fs(sb, &flags, data);
772 773
		if (retval) {
			if (!force)
774
				goto cancel_readonly;
775 776 777 778
			/* If forced remount, go ahead despite any errors */
			WARN(1, "forced remount of a %s fs returned %i\n",
			     sb->s_type->name, retval);
		}
Linus Torvalds's avatar
Linus Torvalds committed
779 780
	}
	sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
781 782 783
	/* Needs to be ordered wrt mnt_is_readonly() */
	smp_wmb();
	sb->s_readonly_remount = 0;
784

785 786 787 788 789 790 791 792 793 794
	/*
	 * Some filesystems modify their metadata via some other path than the
	 * bdev buffer cache (eg. use a private mapping, or directories in
	 * pagecache, etc). Also file data modifications go via their own
	 * mappings. So If we try to mount readonly then copy the filesystem
	 * from bdev, we could get stale data, so invalidate it to give a best
	 * effort at coherency.
	 */
	if (remount_ro && sb->s_bdev)
		invalidate_bdev(sb->s_bdev);
Linus Torvalds's avatar
Linus Torvalds committed
795
	return 0;
796 797 798 799

cancel_readonly:
	sb->s_readonly_remount = 0;
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
800 801
}

802
static void do_emergency_remount(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
803
{
804
	struct super_block *sb, *p = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
805 806

	spin_lock(&sb_lock);
807
	list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro's avatar
Al Viro committed
808
		if (hlist_unhashed(&sb->s_instances))
809
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
810 811
		sb->s_count++;
		spin_unlock(&sb_lock);
812
		down_write(&sb->s_umount);
Al Viro's avatar
Al Viro committed
813 814
		if (sb->s_root && sb->s_bdev && (sb->s_flags & MS_BORN) &&
		    !(sb->s_flags & MS_RDONLY)) {
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818 819
			/*
			 * What lock protects sb->s_flags??
			 */
			do_remount_sb(sb, MS_RDONLY, NULL, 1);
		}
820
		up_write(&sb->s_umount);
Linus Torvalds's avatar
Linus Torvalds committed
821
		spin_lock(&sb_lock);
822 823 824
		if (p)
			__put_super(p);
		p = sb;
Linus Torvalds's avatar
Linus Torvalds committed
825
	}
826 827
	if (p)
		__put_super(p);
Linus Torvalds's avatar
Linus Torvalds committed
828
	spin_unlock(&sb_lock);
829
	kfree(work);
Linus Torvalds's avatar
Linus Torvalds committed
830 831 832 833 834
	printk("Emergency Remount complete\n");
}

void emergency_remount(void)
{
835 836 837 838 839 840 841
	struct work_struct *work;

	work = kmalloc(sizeof(*work), GFP_ATOMIC);
	if (work) {
		INIT_WORK(work, do_emergency_remount);
		schedule_work(work);
	}
Linus Torvalds's avatar
Linus Torvalds committed
842 843 844 845 846 847 848
}

/*
 * Unnamed block devices are dummy devices used by virtual
 * filesystems which don't use real block-devices.  -- jrs
 */

849
static DEFINE_IDA(unnamed_dev_ida);
Linus Torvalds's avatar
Linus Torvalds committed
850
static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
851 852 853 854
/* Many userspace utilities consider an FSID of 0 invalid.
 * Always return at least 1 from get_anon_bdev.
 */
static int unnamed_dev_start = 1;
Linus Torvalds's avatar
Linus Torvalds committed
855

856
int get_anon_bdev(dev_t *p)
Linus Torvalds's avatar
Linus Torvalds committed
857 858 859 860 861
{
	int dev;
	int error;

 retry:
862
	if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
863 864
		return -ENOMEM;
	spin_lock(&unnamed_dev_lock);
865
	error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
866 867
	if (!error)
		unnamed_dev_start = dev + 1;
Linus Torvalds's avatar
Linus Torvalds committed
868 869 870 871 872 873 874
	spin_unlock(&unnamed_dev_lock);
	if (error == -EAGAIN)
		/* We raced and lost with another CPU. */
		goto retry;
	else if (error)
		return -EAGAIN;

875
	if (dev >= (1 << MINORBITS)) {
Linus Torvalds's avatar
Linus Torvalds committed
876
		spin_lock(&unnamed_dev_lock);
877
		ida_remove(&unnamed_dev_ida, dev);
878 879
		if (unnamed_dev_start > dev)
			unnamed_dev_start = dev;
Linus Torvalds's avatar
Linus Torvalds committed
880 881 882
		spin_unlock(&unnamed_dev_lock);
		return -EMFILE;
	}
883
	*p = MKDEV(0, dev & MINORMASK);
Linus Torvalds's avatar
Linus Torvalds committed
884 885
	return 0;
}
886
EXPORT_SYMBOL(get_anon_bdev);
Linus Torvalds's avatar
Linus Torvalds committed
887

888
void free_anon_bdev(dev_t dev)
Linus Torvalds's avatar
Linus Torvalds committed
889
{
890
	int slot = MINOR(dev);
Linus Torvalds's avatar
Linus Torvalds committed
891
	spin_lock(&unnamed_dev_lock);
892
	ida_remove(&unnamed_dev_ida, slot);
893 894
	if (slot < unnamed_dev_start)
		unnamed_dev_start = slot;
Linus Torvalds's avatar
Linus Torvalds committed
895 896
	spin_unlock(&unnamed_dev_lock);
}
897 898 899 900
EXPORT_SYMBOL(free_anon_bdev);

int set_anon_super(struct super_block *s, void *data)
{
901
	return get_anon_bdev(&s->s_dev);
902 903 904 905 906 907 908 909 910 911
}

EXPORT_SYMBOL(set_anon_super);

void kill_anon_super(struct super_block *sb)
{
	dev_t dev = sb->s_dev;
	generic_shutdown_super(sb);
	free_anon_bdev(dev);
}
Linus Torvalds's avatar
Linus Torvalds committed
912 913 914 915 916 917 918 919 920 921 922 923

EXPORT_SYMBOL(kill_anon_super);

void kill_litter_super(struct super_block *sb)
{
	if (sb->s_root)
		d_genocide(sb->s_root);
	kill_anon_super(sb);
}

EXPORT_SYMBOL(kill_litter_super);

924 925 926 927 928 929 930 931 932 933 934
static int ns_test_super(struct super_block *sb, void *data)
{
	return sb->s_fs_info == data;
}

static int ns_set_super(struct super_block *sb, void *data)
{
	sb->s_fs_info = data;
	return set_anon_super(sb, NULL);
}

Al Viro's avatar
Al Viro committed
935 936
struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
	void *data, int (*fill_super)(struct super_block *, void *, int))
937 938 939
{
	struct super_block *sb;

940
	sb = sget(fs_type, ns_test_super, ns_set_super, flags, data);
941
	if (IS_ERR(sb))
Al Viro's avatar
Al Viro committed
942
		return ERR_CAST(sb);
943 944 945 946 947

	if (!sb->s_root) {
		int err;
		err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
		if (err) {
948
			deactivate_locked_super(sb);
Al Viro's avatar
Al Viro committed
949
			return ERR_PTR(err);
950 951 952 953 954
		}

		sb->s_flags |= MS_ACTIVE;
	}

Al Viro's avatar
Al Viro committed
955
	return dget(sb->s_root);
956 957
}

Al Viro's avatar
Al Viro committed
958
EXPORT_SYMBOL(mount_ns);
959

960
#ifdef CONFIG_BLOCK
Linus Torvalds's avatar
Linus Torvalds committed
961 962 963 964
static int set_bdev_super(struct super_block *s, void *data)
{
	s->s_bdev = data;
	s->s_dev = s->s_bdev->bd_dev;
Jens Axboe's avatar
Jens Axboe committed
965 966 967 968 969 970

	/*
	 * We set the bdi here to the queue backing, file systems can
	 * overwrite this in ->fill_super()
	 */
	s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
Linus Torvalds's avatar
Linus Torvalds committed
971 972 973 974 975 976 977 978
	return 0;
}

static int test_bdev_super(struct super_block *s, void *data)
{
	return (void *)s->s_bdev == data;
}

Al Viro's avatar
Al Viro committed
979
struct dentry *mount_bdev(struct file_system_type *fs_type,
Linus Torvalds's avatar
Linus Torvalds committed
980
	int flags, const char *dev_name, void *data,
Al Viro's avatar
Al Viro committed
981
	int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds's avatar
Linus Torvalds committed
982 983 984
{
	struct block_device *bdev;
	struct super_block *s;
985
	fmode_t mode = FMODE_READ | FMODE_EXCL;
Linus Torvalds's avatar
Linus Torvalds committed
986 987
	int error = 0;

988 989 990
	if (!(flags & MS_RDONLY))
		mode |= FMODE_WRITE;

991
	bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Linus Torvalds's avatar
Linus Torvalds committed
992
	if (IS_ERR(bdev))
Al Viro's avatar
Al Viro committed
993
		return ERR_CAST(bdev);
Linus Torvalds's avatar
Linus Torvalds committed
994 995 996 997 998 999

	/*
	 * once the super is inserted into the list by sget, s_umount
	 * will protect the lockfs code from trying to start a snapshot
	 * while we are mounting
	 */
1000 1001 1002 1003 1004 1005
	mutex_lock(&bdev->bd_fsfreeze_mutex);
	if (bdev->bd_fsfreeze_count > 0) {
		mutex_unlock(&bdev->bd_fsfreeze_mutex);
		error = -EBUSY;
		goto error_bdev;
	}
1006 1007
	s = sget(fs_type, test_bdev_super, set_bdev_super, flags | MS_NOSEC,
		 bdev);
1008
	mutex_unlock(&bdev->bd_fsfreeze_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1009
	if (IS_ERR(s))
1010
		goto error_s;
Linus Torvalds's avatar
Linus Torvalds committed
1011 1012 1013

	if (s->s_root) {
		if ((flags ^ s->s_flags) & MS_RDONLY) {
1014
			deactivate_locked_super(s);
1015 1016
			error = -EBUSY;
			goto error_bdev;
Linus Torvalds's avatar
Linus Torvalds committed
1017
		}
1018

1019 1020
		/*
		 * s_umount nests inside bd_mutex during
1021 1022 1023 1024
		 * __invalidate_device().  blkdev_put() acquires
		 * bd_mutex and can't be called under s_umount.  Drop
		 * s_umount temporarily.  This is safe as we're
		 * holding an active reference.
1025 1026
		 */
		up_write(&s->s_umount);
1027
		blkdev_put(bdev, mode);
1028
		down_write(&s->s_umount);
Linus Torvalds's avatar
Linus Torvalds committed
1029 1030 1031
	} else {
		char b[BDEVNAME_SIZE];

1032
		s->s_mode = mode;
Linus Torvalds's avatar
Linus Torvalds committed
1033
		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1034
		sb_set_blocksize(s, block_size(bdev));
1035
		error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds's avatar
Linus Torvalds committed
1036
		if (error) {
1037
			deactivate_locked_super(s);
1038
			goto error;
1039
		}
1040 1041

		s->s_flags |= MS_ACTIVE;
1042
		bdev->bd_super = s;
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044
	}

Al Viro's avatar
Al Viro committed
1045
	return dget(s->s_root);
Linus Torvalds's avatar
Linus Torvalds committed
1046

1047 1048 1049
error_s:
	error = PTR_ERR(s);
error_bdev:
1050
	blkdev_put(bdev, mode);
1051
error:
Al Viro's avatar
Al Viro committed
1052 1053 1054 1055
	return ERR_PTR(error);
}
EXPORT_SYMBOL(mount_bdev);

Linus Torvalds's avatar
Linus Torvalds committed
1056 1057 1058
void kill_block_super(struct super_block *sb)
{
	struct block_device *bdev = sb->s_bdev;
1059
	fmode_t mode = sb->s_mode;
Linus Torvalds's avatar
Linus Torvalds committed
1060

1061
	bdev->bd_super = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1062 1063
	generic_shutdown_super(sb);
	sync_blockdev(bdev);
1064
	WARN_ON_ONCE(!(mode & FMODE_EXCL));
1065
	blkdev_put(bdev, mode | FMODE_EXCL);
Linus Torvalds's avatar
Linus Torvalds committed
1066 1067 1068
}

EXPORT_SYMBOL(kill_block_super);
1069
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1070

Al Viro's avatar
Al Viro committed
1071
struct dentry *mount_nodev(struct file_system_type *fs_type,
Linus Torvalds's avatar
Linus Torvalds committed
1072
	int flags, void *data,
Al Viro's avatar
Al Viro committed
1073
	int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds's avatar
Linus Torvalds committed
1074 1075
{
	int error;
1076
	struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1077 1078

	if (IS_ERR(s))
Al Viro's avatar
Al Viro committed
1079
		return ERR_CAST(s);
Linus Torvalds's avatar
Linus Torvalds committed
1080

1081
	error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds's avatar
Linus Torvalds committed
1082
	if (error) {
1083
		deactivate_locked_super(s);
Al Viro's avatar
Al Viro committed
1084
		return ERR_PTR(error);
Linus Torvalds's avatar
Linus Torvalds committed
1085 1086
	}
	s->s_flags |= MS_ACTIVE;
Al Viro's avatar
Al Viro committed
1087
	return dget(s->s_root);
Linus Torvalds's avatar
Linus Torvalds committed
1088
}
Al Viro's avatar
Al Viro committed
1089 1090
EXPORT_SYMBOL(mount_nodev);

Linus Torvalds's avatar
Linus Torvalds committed
1091 1092 1093 1094 1095
static int compare_single(struct super_block *s, void *p)
{
	return 1;
}

Al Viro's avatar
Al Viro committed
1096
struct dentry *mount_single(struct file_system_type *fs_type,
Linus Torvalds's avatar
Linus Torvalds committed
1097
	int flags, void *data,
Al Viro's avatar
Al Viro committed
1098
	int (*fill_super)(struct super_block *, void *, int))
Linus Torvalds's avatar
Linus Torvalds committed
1099 1100 1101 1102
{
	struct super_block *s;
	int error;

1103
	s = sget(fs_type, compare_single, set_anon_super, flags, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1104
	if (IS_ERR(s))
Al Viro's avatar
Al Viro committed
1105
		return ERR_CAST(s);
Linus Torvalds's avatar
Linus Torvalds committed
1106
	if (!s->s_root) {
1107
		error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds's avatar
Linus Torvalds committed
1108
		if (error) {
1109
			deactivate_locked_super(s);
Al Viro's avatar
Al Viro committed
1110
			return ERR_PTR(error);
Linus Torvalds's avatar
Linus Torvalds committed
1111 1112
		}
		s->s_flags |= MS_ACTIVE;
1113 1114
	} else {
		do_remount_sb(s, flags, data, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1115
	}
Al Viro's avatar
Al Viro committed
1116 1117 1118 1119
	return dget(s->s_root);
}
EXPORT_SYMBOL(mount_single);

1120 1121
struct dentry *
mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
1122
{
Al Viro's avatar
Al Viro committed
1123
	struct dentry *root;
1124
	struct super_block *sb;
Linus Torvalds's avatar
Linus Torvalds committed
1125
	char *secdata = NULL;
1126
	int error = -ENOMEM;
Al Viro's avatar
Al Viro committed
1127

1128
	if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
Linus Torvalds's avatar
Linus Torvalds committed
1129
		secdata = alloc_secdata();
1130
		if (!secdata)
1131
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1132

1133
		error = security_sb_copy_data(data, secdata);
1134
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
1135 1136 1137
			goto out_free_secdata;
	}

Al Viro's avatar
Al Viro committed
1138 1139 1140 1141
	root = type->mount(type, flags, name, data);
	if (IS_ERR(root)) {
		error = PTR_ERR(root);
		goto out_free_secdata;
Al Viro's avatar
Al Viro committed
1142
	}
1143 1144 1145
	sb = root->d_sb;
	BUG_ON(!sb);
	WARN_ON(!sb->s_bdi);
1146 1147 1148 1149 1150 1151 1152 1153

	/*
	 * Write barrier is for super_cache_count(). We place it before setting
	 * MS_BORN as the data dependency between the two functions is the
	 * superblock structure contents that we just set up, not the MS_BORN
	 * flag.
	 */
	smp_wmb();
1154
	sb->s_flags |= MS_BORN;
1155

1156
	error = security_sb_kern_mount(sb, flags, secdata);
1157 1158
	if (error)
		goto out_sb;
1159

1160 1161 1162 1163
	/*
	 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
	 * but s_maxbytes was an unsigned long long for many releases. Throw
	 * this warning for a little while to try and catch filesystems that
1164
	 * violate this rule.
1165
	 */
1166 1167
	WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
		"negative value (%lld)\n", type->name, sb->s_maxbytes);