inode: remove iprune_sem
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / super.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/super.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * super.c contains code to handle: - mount structures
7 * - super-block tables
8 * - filesystem drivers list
9 * - mount system call
10 * - umount system call
11 * - ustat system call
12 *
13 * GK 2/5/95 - Changed to support mounting the root fs via NFS
14 *
15 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
16 * Added change_root: Werner Almesberger & Hans Lermen, Feb '96
17 * Added options to /proc/mounts:
96de0e25 18 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
1da177e4
LT
19 * Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
20 * Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
21 */
22
1da177e4
LT
23#include <linux/module.h>
24#include <linux/slab.h>
1da177e4
LT
25#include <linux/acct.h>
26#include <linux/blkdev.h>
1da177e4
LT
27#include <linux/mount.h>
28#include <linux/security.h>
1da177e4
LT
29#include <linux/writeback.h> /* for the emergency remount stuff */
30#include <linux/idr.h>
353ab6e9 31#include <linux/mutex.h>
5477d0fa 32#include <linux/backing-dev.h>
ceb5bdc2 33#include <linux/rculist_bl.h>
c515e1fd 34#include <linux/cleancache.h>
6d59e7f5 35#include "internal.h"
1da177e4
LT
36
37
1da177e4
LT
38LIST_HEAD(super_blocks);
39DEFINE_SPINLOCK(sb_lock);
40
b0d40c92
DC
41/*
42 * One thing we have to be careful of with a per-sb shrinker is that we don't
43 * drop the last active reference to the superblock from within the shrinker.
44 * If that happens we could trigger unregistering the shrinker from within the
45 * shrinker path and that leads to deadlock on the shrinker_rwsem. Hence we
46 * take a passive reference to the superblock to avoid this from occurring.
47 */
48static int prune_super(struct shrinker *shrink, struct shrink_control *sc)
49{
50 struct super_block *sb;
51 int count;
52
53 sb = container_of(shrink, struct super_block, s_shrink);
54
55 /*
56 * Deadlock avoidance. We may hold various FS locks, and we don't want
57 * to recurse into the FS that called us in clear_inode() and friends..
58 */
59 if (sc->nr_to_scan && !(sc->gfp_mask & __GFP_FS))
60 return -1;
61
62 if (!grab_super_passive(sb))
63 return -1;
64
65 if (sc->nr_to_scan) {
66 /* proportion the scan between the two caches */
67 int total;
68
69 total = sb->s_nr_dentry_unused + sb->s_nr_inodes_unused + 1;
70 count = (sc->nr_to_scan * sb->s_nr_dentry_unused) / total;
71
72 /* prune dcache first as icache is pinned by it */
73 prune_dcache_sb(sb, count);
74 prune_icache_sb(sb, sc->nr_to_scan - count);
75 }
76
77 count = ((sb->s_nr_dentry_unused + sb->s_nr_inodes_unused) / 100)
78 * sysctl_vfs_cache_pressure;
79 drop_super(sb);
80 return count;
81}
82
1da177e4
LT
83/**
84 * alloc_super - create new superblock
fe2bbc48 85 * @type: filesystem type superblock should belong to
1da177e4
LT
86 *
87 * Allocates and initializes a new &struct super_block. alloc_super()
88 * returns a pointer new superblock or %NULL if allocation had failed.
89 */
cf516249 90static struct super_block *alloc_super(struct file_system_type *type)
1da177e4 91{
11b0b5ab 92 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
b87221de 93 static const struct super_operations default_op;
1da177e4
LT
94
95 if (s) {
1da177e4
LT
96 if (security_sb_alloc(s)) {
97 kfree(s);
98 s = NULL;
99 goto out;
100 }
6416ccb7
NP
101#ifdef CONFIG_SMP
102 s->s_files = alloc_percpu(struct list_head);
103 if (!s->s_files) {
104 security_sb_free(s);
105 kfree(s);
106 s = NULL;
107 goto out;
108 } else {
109 int i;
110
111 for_each_possible_cpu(i)
112 INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i));
113 }
114#else
1da177e4 115 INIT_LIST_HEAD(&s->s_files);
6416ccb7 116#endif
95f28604 117 s->s_bdi = &default_backing_dev_info;
1da177e4 118 INIT_LIST_HEAD(&s->s_instances);
ceb5bdc2 119 INIT_HLIST_BL_HEAD(&s->s_anon);
1da177e4 120 INIT_LIST_HEAD(&s->s_inodes);
da3bbdd4 121 INIT_LIST_HEAD(&s->s_dentry_lru);
98b745c6 122 INIT_LIST_HEAD(&s->s_inode_lru);
09cc9fc7 123 spin_lock_init(&s->s_inode_lru_lock);
1da177e4 124 init_rwsem(&s->s_umount);
7892f2f4 125 mutex_init(&s->s_lock);
897c6ff9 126 lockdep_set_class(&s->s_umount, &type->s_umount_key);
cf516249
IM
127 /*
128 * The locking rules for s_lock are up to the
129 * filesystem. For example ext3fs has different
130 * lock ordering than usbfs:
131 */
132 lockdep_set_class(&s->s_lock, &type->s_lock_key);
ada723dc
PZ
133 /*
134 * sget() can have s_umount recursion.
135 *
136 * When it cannot find a suitable sb, it allocates a new
137 * one (this one), and tries again to find a suitable old
138 * one.
139 *
140 * In case that succeeds, it will acquire the s_umount
141 * lock of the old one. Since these are clearly distrinct
142 * locks, and this object isn't exposed yet, there's no
143 * risk of deadlocks.
144 *
145 * Annotate this by putting this lock in a different
146 * subclass.
147 */
148 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
b20bd1a5 149 s->s_count = 1;
1da177e4 150 atomic_set(&s->s_active, 1);
a11f3a05 151 mutex_init(&s->s_vfs_rename_mutex);
51ee049e 152 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
d3be915f
IM
153 mutex_init(&s->s_dquot.dqio_mutex);
154 mutex_init(&s->s_dquot.dqonoff_mutex);
1da177e4
LT
155 init_rwsem(&s->s_dquot.dqptr_sem);
156 init_waitqueue_head(&s->s_wait_unfrozen);
157 s->s_maxbytes = MAX_NON_LFS;
1da177e4
LT
158 s->s_op = &default_op;
159 s->s_time_gran = 1000000000;
c515e1fd 160 s->cleancache_poolid = -1;
b0d40c92
DC
161
162 s->s_shrink.seeks = DEFAULT_SEEKS;
163 s->s_shrink.shrink = prune_super;
1da177e4
LT
164 }
165out:
166 return s;
167}
168
169/**
170 * destroy_super - frees a superblock
171 * @s: superblock to free
172 *
173 * Frees a superblock.
174 */
175static inline void destroy_super(struct super_block *s)
176{
6416ccb7
NP
177#ifdef CONFIG_SMP
178 free_percpu(s->s_files);
179#endif
1da177e4 180 security_sb_free(s);
79c0b2df 181 kfree(s->s_subtype);
b3b304a2 182 kfree(s->s_options);
1da177e4
LT
183 kfree(s);
184}
185
186/* Superblock refcounting */
187
188/*
35cf7ba0 189 * Drop a superblock's refcount. The caller must hold sb_lock.
1da177e4 190 */
35cf7ba0 191void __put_super(struct super_block *sb)
1da177e4 192{
1da177e4 193 if (!--sb->s_count) {
551de6f3 194 list_del_init(&sb->s_list);
1da177e4 195 destroy_super(sb);
1da177e4 196 }
1da177e4
LT
197}
198
199/**
200 * put_super - drop a temporary reference to superblock
201 * @sb: superblock in question
202 *
203 * Drops a temporary reference, frees superblock if there's no
204 * references left.
205 */
03ba3782 206void put_super(struct super_block *sb)
1da177e4
LT
207{
208 spin_lock(&sb_lock);
209 __put_super(sb);
210 spin_unlock(&sb_lock);
211}
212
213
214/**
1712ac8f 215 * deactivate_locked_super - drop an active reference to superblock
1da177e4
LT
216 * @s: superblock to deactivate
217 *
1712ac8f
AV
218 * Drops an active reference to superblock, converting it into a temprory
219 * one if there is no other active references left. In that case we
1da177e4
LT
220 * tell fs driver to shut it down and drop the temporary reference we
221 * had just acquired.
1712ac8f
AV
222 *
223 * Caller holds exclusive lock on superblock; that lock is released.
1da177e4 224 */
1712ac8f 225void deactivate_locked_super(struct super_block *s)
1da177e4
LT
226{
227 struct file_system_type *fs = s->s_type;
b20bd1a5 228 if (atomic_dec_and_test(&s->s_active)) {
c515e1fd 229 cleancache_flush_fs(s);
1da177e4 230 fs->kill_sb(s);
b0d40c92
DC
231
232 /* caches are now gone, we can safely kill the shrinker now */
233 unregister_shrinker(&s->s_shrink);
234
d863b50a
BH
235 /*
236 * We need to call rcu_barrier so all the delayed rcu free
237 * inodes are flushed before we release the fs module.
238 */
239 rcu_barrier();
1da177e4
LT
240 put_filesystem(fs);
241 put_super(s);
1712ac8f
AV
242 } else {
243 up_write(&s->s_umount);
1da177e4
LT
244 }
245}
246
1712ac8f 247EXPORT_SYMBOL(deactivate_locked_super);
1da177e4 248
74dbbdd7 249/**
1712ac8f 250 * deactivate_super - drop an active reference to superblock
74dbbdd7
AV
251 * @s: superblock to deactivate
252 *
1712ac8f
AV
253 * Variant of deactivate_locked_super(), except that superblock is *not*
254 * locked by caller. If we are going to drop the final active reference,
255 * lock will be acquired prior to that.
74dbbdd7 256 */
1712ac8f 257void deactivate_super(struct super_block *s)
74dbbdd7 258{
1712ac8f
AV
259 if (!atomic_add_unless(&s->s_active, -1, 1)) {
260 down_write(&s->s_umount);
261 deactivate_locked_super(s);
74dbbdd7
AV
262 }
263}
264
1712ac8f 265EXPORT_SYMBOL(deactivate_super);
74dbbdd7 266
1da177e4
LT
267/**
268 * grab_super - acquire an active reference
269 * @s: reference we are trying to make active
270 *
271 * Tries to acquire an active reference. grab_super() is used when we
272 * had just found a superblock in super_blocks or fs_type->fs_supers
273 * and want to turn it into a full-blown active reference. grab_super()
274 * is called with sb_lock held and drops it. Returns 1 in case of
275 * success, 0 if we had failed (superblock contents was already dead or
276 * dying when grab_super() had been called).
277 */
9c4dbee7 278static int grab_super(struct super_block *s) __releases(sb_lock)
1da177e4 279{
b20bd1a5
AV
280 if (atomic_inc_not_zero(&s->s_active)) {
281 spin_unlock(&sb_lock);
b20bd1a5
AV
282 return 1;
283 }
284 /* it's going away */
1da177e4
LT
285 s->s_count++;
286 spin_unlock(&sb_lock);
1712ac8f 287 /* wait for it to die */
1da177e4 288 down_write(&s->s_umount);
1da177e4
LT
289 up_write(&s->s_umount);
290 put_super(s);
1da177e4
LT
291 return 0;
292}
293
12ad3ab6
DC
294/*
295 * grab_super_passive - acquire a passive reference
296 * @s: reference we are trying to grab
297 *
298 * Tries to acquire a passive reference. This is used in places where we
299 * cannot take an active reference but we need to ensure that the
300 * superblock does not go away while we are working on it. It returns
301 * false if a reference was not gained, and returns true with the s_umount
302 * lock held in read mode if a reference is gained. On successful return,
303 * the caller must drop the s_umount lock and the passive reference when
304 * done.
305 */
306bool grab_super_passive(struct super_block *sb)
307{
308 spin_lock(&sb_lock);
309 if (list_empty(&sb->s_instances)) {
310 spin_unlock(&sb_lock);
311 return false;
312 }
313
314 sb->s_count++;
315 spin_unlock(&sb_lock);
316
317 if (down_read_trylock(&sb->s_umount)) {
318 if (sb->s_root)
319 return true;
320 up_read(&sb->s_umount);
321 }
322
323 put_super(sb);
324 return false;
325}
326
914e2637
AV
327/*
328 * Superblock locking. We really ought to get rid of these two.
329 */
330void lock_super(struct super_block * sb)
331{
332 get_fs_excl();
333 mutex_lock(&sb->s_lock);
334}
335
336void unlock_super(struct super_block * sb)
337{
338 put_fs_excl();
339 mutex_unlock(&sb->s_lock);
340}
341
342EXPORT_SYMBOL(lock_super);
343EXPORT_SYMBOL(unlock_super);
344
1da177e4
LT
345/**
346 * generic_shutdown_super - common helper for ->kill_sb()
347 * @sb: superblock to kill
348 *
349 * generic_shutdown_super() does all fs-independent work on superblock
350 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
351 * that need destruction out of superblock, call generic_shutdown_super()
352 * and release aforementioned objects. Note: dentries and inodes _are_
353 * taken care of and do not need specific handling.
c636ebdb
DH
354 *
355 * Upon calling this function, the filesystem may no longer alter or
356 * rearrange the set of dentries belonging to this super_block, nor may it
357 * change the attachments of dentries to inodes.
1da177e4
LT
358 */
359void generic_shutdown_super(struct super_block *sb)
360{
ee9b6d61 361 const struct super_operations *sop = sb->s_op;
1da177e4 362
c636ebdb
DH
363 if (sb->s_root) {
364 shrink_dcache_for_umount(sb);
60b0680f 365 sync_filesystem(sb);
a9e220f8 366 get_fs_excl();
1da177e4 367 sb->s_flags &= ~MS_ACTIVE;
efaee192 368
63997e98
AV
369 fsnotify_unmount_inodes(&sb->s_inodes);
370
371 evict_inodes(sb);
1da177e4 372
1da177e4
LT
373 if (sop->put_super)
374 sop->put_super(sb);
375
63997e98 376 if (!list_empty(&sb->s_inodes)) {
7b4fe29e
DJ
377 printk("VFS: Busy inodes after unmount of %s. "
378 "Self-destruct in 5 seconds. Have a nice day...\n",
379 sb->s_id);
1da177e4 380 }
a9e220f8 381 put_fs_excl();
1da177e4
LT
382 }
383 spin_lock(&sb_lock);
384 /* should be initialized for __put_super_and_need_restart() */
551de6f3 385 list_del_init(&sb->s_instances);
1da177e4
LT
386 spin_unlock(&sb_lock);
387 up_write(&sb->s_umount);
388}
389
390EXPORT_SYMBOL(generic_shutdown_super);
391
392/**
393 * sget - find or create a superblock
394 * @type: filesystem type superblock should belong to
395 * @test: comparison callback
396 * @set: setup callback
397 * @data: argument to each of them
398 */
399struct super_block *sget(struct file_system_type *type,
400 int (*test)(struct super_block *,void *),
401 int (*set)(struct super_block *,void *),
402 void *data)
403{
404 struct super_block *s = NULL;
d4730127 405 struct super_block *old;
1da177e4
LT
406 int err;
407
408retry:
409 spin_lock(&sb_lock);
d4730127
MK
410 if (test) {
411 list_for_each_entry(old, &type->fs_supers, s_instances) {
412 if (!test(old, data))
413 continue;
414 if (!grab_super(old))
415 goto retry;
a3cfbb53
LZ
416 if (s) {
417 up_write(&s->s_umount);
d4730127 418 destroy_super(s);
7a4dec53 419 s = NULL;
a3cfbb53 420 }
d3f21473 421 down_write(&old->s_umount);
7a4dec53
AV
422 if (unlikely(!(old->s_flags & MS_BORN))) {
423 deactivate_locked_super(old);
424 goto retry;
425 }
d4730127
MK
426 return old;
427 }
1da177e4
LT
428 }
429 if (!s) {
430 spin_unlock(&sb_lock);
cf516249 431 s = alloc_super(type);
1da177e4
LT
432 if (!s)
433 return ERR_PTR(-ENOMEM);
434 goto retry;
435 }
436
437 err = set(s, data);
438 if (err) {
439 spin_unlock(&sb_lock);
a3cfbb53 440 up_write(&s->s_umount);
1da177e4
LT
441 destroy_super(s);
442 return ERR_PTR(err);
443 }
444 s->s_type = type;
445 strlcpy(s->s_id, type->name, sizeof(s->s_id));
446 list_add_tail(&s->s_list, &super_blocks);
447 list_add(&s->s_instances, &type->fs_supers);
448 spin_unlock(&sb_lock);
449 get_filesystem(type);
b0d40c92 450 register_shrinker(&s->s_shrink);
1da177e4
LT
451 return s;
452}
453
454EXPORT_SYMBOL(sget);
455
456void drop_super(struct super_block *sb)
457{
458 up_read(&sb->s_umount);
459 put_super(sb);
460}
461
462EXPORT_SYMBOL(drop_super);
463
e5004753
CH
464/**
465 * sync_supers - helper for periodic superblock writeback
466 *
467 * Call the write_super method if present on all dirty superblocks in
468 * the system. This is for the periodic writeback used by most older
469 * filesystems. For data integrity superblock writeback use
470 * sync_filesystems() instead.
471 *
1da177e4
LT
472 * Note: check the dirty flag before waiting, so we don't
473 * hold up the sync while mounting a device. (The newly
474 * mounted device won't need syncing.)
475 */
476void sync_supers(void)
477{
dca33252 478 struct super_block *sb, *p = NULL;
618f0636 479
1da177e4 480 spin_lock(&sb_lock);
dca33252 481 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
482 if (list_empty(&sb->s_instances))
483 continue;
e5004753 484 if (sb->s_op->write_super && sb->s_dirt) {
1da177e4
LT
485 sb->s_count++;
486 spin_unlock(&sb_lock);
e5004753 487
1da177e4 488 down_read(&sb->s_umount);
e5004753
CH
489 if (sb->s_root && sb->s_dirt)
490 sb->s_op->write_super(sb);
618f0636 491 up_read(&sb->s_umount);
e5004753 492
618f0636 493 spin_lock(&sb_lock);
dca33252
AV
494 if (p)
495 __put_super(p);
496 p = sb;
618f0636
KK
497 }
498 }
dca33252
AV
499 if (p)
500 __put_super(p);
1da177e4
LT
501 spin_unlock(&sb_lock);
502}
503
01a05b33
AV
504/**
505 * iterate_supers - call function for all active superblocks
506 * @f: function to call
507 * @arg: argument to pass to it
508 *
509 * Scans the superblock list and calls given function, passing it
510 * locked superblock and given argument.
511 */
512void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
513{
dca33252 514 struct super_block *sb, *p = NULL;
01a05b33
AV
515
516 spin_lock(&sb_lock);
dca33252 517 list_for_each_entry(sb, &super_blocks, s_list) {
01a05b33
AV
518 if (list_empty(&sb->s_instances))
519 continue;
520 sb->s_count++;
521 spin_unlock(&sb_lock);
522
523 down_read(&sb->s_umount);
524 if (sb->s_root)
525 f(sb, arg);
526 up_read(&sb->s_umount);
527
528 spin_lock(&sb_lock);
dca33252
AV
529 if (p)
530 __put_super(p);
531 p = sb;
01a05b33 532 }
dca33252
AV
533 if (p)
534 __put_super(p);
01a05b33
AV
535 spin_unlock(&sb_lock);
536}
537
43e15cdb
AV
538/**
539 * iterate_supers_type - call function for superblocks of given type
540 * @type: fs type
541 * @f: function to call
542 * @arg: argument to pass to it
543 *
544 * Scans the superblock list and calls given function, passing it
545 * locked superblock and given argument.
546 */
547void iterate_supers_type(struct file_system_type *type,
548 void (*f)(struct super_block *, void *), void *arg)
549{
550 struct super_block *sb, *p = NULL;
551
552 spin_lock(&sb_lock);
553 list_for_each_entry(sb, &type->fs_supers, s_instances) {
554 sb->s_count++;
555 spin_unlock(&sb_lock);
556
557 down_read(&sb->s_umount);
558 if (sb->s_root)
559 f(sb, arg);
560 up_read(&sb->s_umount);
561
562 spin_lock(&sb_lock);
563 if (p)
564 __put_super(p);
565 p = sb;
566 }
567 if (p)
568 __put_super(p);
569 spin_unlock(&sb_lock);
570}
571
572EXPORT_SYMBOL(iterate_supers_type);
573
1da177e4
LT
574/**
575 * get_super - get the superblock of a device
576 * @bdev: device to get the superblock for
577 *
578 * Scans the superblock list and finds the superblock of the file system
579 * mounted on the device given. %NULL is returned if no match is found.
580 */
581
df40c01a 582struct super_block *get_super(struct block_device *bdev)
1da177e4 583{
618f0636
KK
584 struct super_block *sb;
585
1da177e4
LT
586 if (!bdev)
587 return NULL;
618f0636 588
1da177e4 589 spin_lock(&sb_lock);
618f0636
KK
590rescan:
591 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
592 if (list_empty(&sb->s_instances))
593 continue;
618f0636
KK
594 if (sb->s_bdev == bdev) {
595 sb->s_count++;
1da177e4 596 spin_unlock(&sb_lock);
618f0636 597 down_read(&sb->s_umount);
df40c01a 598 /* still alive? */
618f0636
KK
599 if (sb->s_root)
600 return sb;
601 up_read(&sb->s_umount);
df40c01a 602 /* nope, got unmounted */
618f0636 603 spin_lock(&sb_lock);
df40c01a
AV
604 __put_super(sb);
605 goto rescan;
1da177e4
LT
606 }
607 }
608 spin_unlock(&sb_lock);
609 return NULL;
610}
611
612EXPORT_SYMBOL(get_super);
4504230a
CH
613
614/**
615 * get_active_super - get an active reference to the superblock of a device
616 * @bdev: device to get the superblock for
617 *
618 * Scans the superblock list and finds the superblock of the file system
619 * mounted on the device given. Returns the superblock with an active
d3f21473 620 * reference or %NULL if none was found.
4504230a
CH
621 */
622struct super_block *get_active_super(struct block_device *bdev)
623{
624 struct super_block *sb;
625
626 if (!bdev)
627 return NULL;
628
1494583d 629restart:
4504230a
CH
630 spin_lock(&sb_lock);
631 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
632 if (list_empty(&sb->s_instances))
633 continue;
1494583d
AV
634 if (sb->s_bdev == bdev) {
635 if (grab_super(sb)) /* drops sb_lock */
636 return sb;
637 else
638 goto restart;
639 }
4504230a
CH
640 }
641 spin_unlock(&sb_lock);
642 return NULL;
643}
1da177e4 644
df40c01a 645struct super_block *user_get_super(dev_t dev)
1da177e4 646{
618f0636 647 struct super_block *sb;
1da177e4 648
1da177e4 649 spin_lock(&sb_lock);
618f0636
KK
650rescan:
651 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
652 if (list_empty(&sb->s_instances))
653 continue;
618f0636
KK
654 if (sb->s_dev == dev) {
655 sb->s_count++;
1da177e4 656 spin_unlock(&sb_lock);
618f0636 657 down_read(&sb->s_umount);
df40c01a 658 /* still alive? */
618f0636
KK
659 if (sb->s_root)
660 return sb;
661 up_read(&sb->s_umount);
df40c01a 662 /* nope, got unmounted */
618f0636 663 spin_lock(&sb_lock);
df40c01a
AV
664 __put_super(sb);
665 goto rescan;
1da177e4
LT
666 }
667 }
668 spin_unlock(&sb_lock);
669 return NULL;
670}
671
1da177e4
LT
672/**
673 * do_remount_sb - asks filesystem to change mount options.
674 * @sb: superblock in question
675 * @flags: numeric part of options
676 * @data: the rest of options
677 * @force: whether or not to force the change
678 *
679 * Alters the mount options of a mounted file system.
680 */
681int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
682{
683 int retval;
c79d967d 684 int remount_ro;
4504230a
CH
685
686 if (sb->s_frozen != SB_UNFROZEN)
687 return -EBUSY;
688
9361401e 689#ifdef CONFIG_BLOCK
1da177e4
LT
690 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
691 return -EACCES;
9361401e 692#endif
4504230a 693
1da177e4
LT
694 if (flags & MS_RDONLY)
695 acct_auto_close(sb);
696 shrink_dcache_sb(sb);
60b0680f 697 sync_filesystem(sb);
1da177e4 698
d208bbdd 699 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
d208bbdd 700
1da177e4
LT
701 /* If we are remounting RDONLY and current sb is read/write,
702 make sure there are no rw files opened */
d208bbdd 703 if (remount_ro) {
1da177e4
LT
704 if (force)
705 mark_files_ro(sb);
b0895513 706 else if (!fs_may_remount_ro(sb))
1da177e4
LT
707 return -EBUSY;
708 }
709
710 if (sb->s_op->remount_fs) {
1da177e4 711 retval = sb->s_op->remount_fs(sb, &flags, data);
b0895513 712 if (retval)
1da177e4
LT
713 return retval;
714 }
715 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
c79d967d 716
d208bbdd
NP
717 /*
718 * Some filesystems modify their metadata via some other path than the
719 * bdev buffer cache (eg. use a private mapping, or directories in
720 * pagecache, etc). Also file data modifications go via their own
721 * mappings. So If we try to mount readonly then copy the filesystem
722 * from bdev, we could get stale data, so invalidate it to give a best
723 * effort at coherency.
724 */
725 if (remount_ro && sb->s_bdev)
726 invalidate_bdev(sb->s_bdev);
1da177e4
LT
727 return 0;
728}
729
a2a9537a 730static void do_emergency_remount(struct work_struct *work)
1da177e4 731{
dca33252 732 struct super_block *sb, *p = NULL;
1da177e4
LT
733
734 spin_lock(&sb_lock);
dca33252 735 list_for_each_entry(sb, &super_blocks, s_list) {
551de6f3
AV
736 if (list_empty(&sb->s_instances))
737 continue;
1da177e4
LT
738 sb->s_count++;
739 spin_unlock(&sb_lock);
443b94ba 740 down_write(&sb->s_umount);
1da177e4
LT
741 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
742 /*
1da177e4
LT
743 * What lock protects sb->s_flags??
744 */
1da177e4 745 do_remount_sb(sb, MS_RDONLY, NULL, 1);
1da177e4 746 }
443b94ba 747 up_write(&sb->s_umount);
1da177e4 748 spin_lock(&sb_lock);
dca33252
AV
749 if (p)
750 __put_super(p);
751 p = sb;
1da177e4 752 }
dca33252
AV
753 if (p)
754 __put_super(p);
1da177e4 755 spin_unlock(&sb_lock);
a2a9537a 756 kfree(work);
1da177e4
LT
757 printk("Emergency Remount complete\n");
758}
759
760void emergency_remount(void)
761{
a2a9537a
JA
762 struct work_struct *work;
763
764 work = kmalloc(sizeof(*work), GFP_ATOMIC);
765 if (work) {
766 INIT_WORK(work, do_emergency_remount);
767 schedule_work(work);
768 }
1da177e4
LT
769}
770
771/*
772 * Unnamed block devices are dummy devices used by virtual
773 * filesystems which don't use real block-devices. -- jrs
774 */
775
ad76cbc6 776static DEFINE_IDA(unnamed_dev_ida);
1da177e4 777static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
c63e09ec 778static int unnamed_dev_start = 0; /* don't bother trying below it */
1da177e4 779
0ee5dc67 780int get_anon_bdev(dev_t *p)
1da177e4
LT
781{
782 int dev;
783 int error;
784
785 retry:
ad76cbc6 786 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
1da177e4
LT
787 return -ENOMEM;
788 spin_lock(&unnamed_dev_lock);
c63e09ec 789 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
f21f6220
AV
790 if (!error)
791 unnamed_dev_start = dev + 1;
1da177e4
LT
792 spin_unlock(&unnamed_dev_lock);
793 if (error == -EAGAIN)
794 /* We raced and lost with another CPU. */
795 goto retry;
796 else if (error)
797 return -EAGAIN;
798
799 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
800 spin_lock(&unnamed_dev_lock);
ad76cbc6 801 ida_remove(&unnamed_dev_ida, dev);
f21f6220
AV
802 if (unnamed_dev_start > dev)
803 unnamed_dev_start = dev;
1da177e4
LT
804 spin_unlock(&unnamed_dev_lock);
805 return -EMFILE;
806 }
0ee5dc67 807 *p = MKDEV(0, dev & MINORMASK);
1da177e4
LT
808 return 0;
809}
0ee5dc67 810EXPORT_SYMBOL(get_anon_bdev);
1da177e4 811
0ee5dc67 812void free_anon_bdev(dev_t dev)
1da177e4 813{
0ee5dc67 814 int slot = MINOR(dev);
1da177e4 815 spin_lock(&unnamed_dev_lock);
ad76cbc6 816 ida_remove(&unnamed_dev_ida, slot);
c63e09ec
AV
817 if (slot < unnamed_dev_start)
818 unnamed_dev_start = slot;
1da177e4
LT
819 spin_unlock(&unnamed_dev_lock);
820}
0ee5dc67
AV
821EXPORT_SYMBOL(free_anon_bdev);
822
823int set_anon_super(struct super_block *s, void *data)
824{
825 int error = get_anon_bdev(&s->s_dev);
826 if (!error)
827 s->s_bdi = &noop_backing_dev_info;
828 return error;
829}
830
831EXPORT_SYMBOL(set_anon_super);
832
833void kill_anon_super(struct super_block *sb)
834{
835 dev_t dev = sb->s_dev;
836 generic_shutdown_super(sb);
837 free_anon_bdev(dev);
838}
1da177e4
LT
839
840EXPORT_SYMBOL(kill_anon_super);
841
1da177e4
LT
842void kill_litter_super(struct super_block *sb)
843{
844 if (sb->s_root)
845 d_genocide(sb->s_root);
846 kill_anon_super(sb);
847}
848
849EXPORT_SYMBOL(kill_litter_super);
850
909e6d94
SH
851static int ns_test_super(struct super_block *sb, void *data)
852{
853 return sb->s_fs_info == data;
854}
855
856static int ns_set_super(struct super_block *sb, void *data)
857{
858 sb->s_fs_info = data;
859 return set_anon_super(sb, NULL);
860}
861
ceefda69
AV
862struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
863 void *data, int (*fill_super)(struct super_block *, void *, int))
909e6d94
SH
864{
865 struct super_block *sb;
866
867 sb = sget(fs_type, ns_test_super, ns_set_super, data);
868 if (IS_ERR(sb))
ceefda69 869 return ERR_CAST(sb);
909e6d94
SH
870
871 if (!sb->s_root) {
872 int err;
873 sb->s_flags = flags;
874 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
875 if (err) {
74dbbdd7 876 deactivate_locked_super(sb);
ceefda69 877 return ERR_PTR(err);
909e6d94
SH
878 }
879
880 sb->s_flags |= MS_ACTIVE;
881 }
882
ceefda69 883 return dget(sb->s_root);
909e6d94
SH
884}
885
ceefda69 886EXPORT_SYMBOL(mount_ns);
909e6d94 887
9361401e 888#ifdef CONFIG_BLOCK
1da177e4
LT
889static int set_bdev_super(struct super_block *s, void *data)
890{
891 s->s_bdev = data;
892 s->s_dev = s->s_bdev->bd_dev;
32a88aa1
JA
893
894 /*
895 * We set the bdi here to the queue backing, file systems can
896 * overwrite this in ->fill_super()
897 */
898 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
1da177e4
LT
899 return 0;
900}
901
902static int test_bdev_super(struct super_block *s, void *data)
903{
904 return (void *)s->s_bdev == data;
905}
906
152a0836 907struct dentry *mount_bdev(struct file_system_type *fs_type,
1da177e4 908 int flags, const char *dev_name, void *data,
152a0836 909 int (*fill_super)(struct super_block *, void *, int))
1da177e4
LT
910{
911 struct block_device *bdev;
912 struct super_block *s;
d4d77629 913 fmode_t mode = FMODE_READ | FMODE_EXCL;
1da177e4
LT
914 int error = 0;
915
30c40d2c
AV
916 if (!(flags & MS_RDONLY))
917 mode |= FMODE_WRITE;
918
d4d77629 919 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1da177e4 920 if (IS_ERR(bdev))
152a0836 921 return ERR_CAST(bdev);
1da177e4
LT
922
923 /*
924 * once the super is inserted into the list by sget, s_umount
925 * will protect the lockfs code from trying to start a snapshot
926 * while we are mounting
927 */
4fadd7bb
CH
928 mutex_lock(&bdev->bd_fsfreeze_mutex);
929 if (bdev->bd_fsfreeze_count > 0) {
930 mutex_unlock(&bdev->bd_fsfreeze_mutex);
931 error = -EBUSY;
932 goto error_bdev;
933 }
1da177e4 934 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
4fadd7bb 935 mutex_unlock(&bdev->bd_fsfreeze_mutex);
1da177e4 936 if (IS_ERR(s))
454e2398 937 goto error_s;
1da177e4
LT
938
939 if (s->s_root) {
940 if ((flags ^ s->s_flags) & MS_RDONLY) {
74dbbdd7 941 deactivate_locked_super(s);
454e2398
DH
942 error = -EBUSY;
943 goto error_bdev;
1da177e4 944 }
454e2398 945
4f331f01
TH
946 /*
947 * s_umount nests inside bd_mutex during
e525fd89
TH
948 * __invalidate_device(). blkdev_put() acquires
949 * bd_mutex and can't be called under s_umount. Drop
950 * s_umount temporarily. This is safe as we're
951 * holding an active reference.
4f331f01
TH
952 */
953 up_write(&s->s_umount);
d4d77629 954 blkdev_put(bdev, mode);
4f331f01 955 down_write(&s->s_umount);
1da177e4
LT
956 } else {
957 char b[BDEVNAME_SIZE];
958
9e1f1de0 959 s->s_flags = flags | MS_NOSEC;
30c40d2c 960 s->s_mode = mode;
1da177e4 961 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
e78c9a00 962 sb_set_blocksize(s, block_size(bdev));
9b04c997 963 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1da177e4 964 if (error) {
74dbbdd7 965 deactivate_locked_super(s);
454e2398 966 goto error;
fa675765 967 }
454e2398
DH
968
969 s->s_flags |= MS_ACTIVE;
87d8fe1e 970 bdev->bd_super = s;
1da177e4
LT
971 }
972
152a0836 973 return dget(s->s_root);
1da177e4 974
454e2398
DH
975error_s:
976 error = PTR_ERR(s);
977error_bdev:
d4d77629 978 blkdev_put(bdev, mode);
454e2398 979error:
152a0836
AV
980 return ERR_PTR(error);
981}
982EXPORT_SYMBOL(mount_bdev);
983
1da177e4
LT
984void kill_block_super(struct super_block *sb)
985{
986 struct block_device *bdev = sb->s_bdev;
30c40d2c 987 fmode_t mode = sb->s_mode;
1da177e4 988
ddbaaf30 989 bdev->bd_super = NULL;
1da177e4
LT
990 generic_shutdown_super(sb);
991 sync_blockdev(bdev);
d4d77629 992 WARN_ON_ONCE(!(mode & FMODE_EXCL));
e525fd89 993 blkdev_put(bdev, mode | FMODE_EXCL);
1da177e4
LT
994}
995
996EXPORT_SYMBOL(kill_block_super);
9361401e 997#endif
1da177e4 998
3c26ff6e 999struct dentry *mount_nodev(struct file_system_type *fs_type,
1da177e4 1000 int flags, void *data,
3c26ff6e 1001 int (*fill_super)(struct super_block *, void *, int))
1da177e4
LT
1002{
1003 int error;
1004 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
1005
1006 if (IS_ERR(s))
3c26ff6e 1007 return ERR_CAST(s);
1da177e4
LT
1008
1009 s->s_flags = flags;
1010
9b04c997 1011 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1da177e4 1012 if (error) {
74dbbdd7 1013 deactivate_locked_super(s);
3c26ff6e 1014 return ERR_PTR(error);
1da177e4
LT
1015 }
1016 s->s_flags |= MS_ACTIVE;
3c26ff6e 1017 return dget(s->s_root);
1da177e4 1018}
3c26ff6e
AV
1019EXPORT_SYMBOL(mount_nodev);
1020
1da177e4
LT
1021static int compare_single(struct super_block *s, void *p)
1022{
1023 return 1;
1024}
1025
fc14f2fe 1026struct dentry *mount_single(struct file_system_type *fs_type,
1da177e4 1027 int flags, void *data,
fc14f2fe 1028 int (*fill_super)(struct super_block *, void *, int))
1da177e4
LT
1029{
1030 struct super_block *s;
1031 int error;
1032
1033 s = sget(fs_type, compare_single, set_anon_super, NULL);
1034 if (IS_ERR(s))
fc14f2fe 1035 return ERR_CAST(s);
1da177e4
LT
1036 if (!s->s_root) {
1037 s->s_flags = flags;
9b04c997 1038 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1da177e4 1039 if (error) {
74dbbdd7 1040 deactivate_locked_super(s);
fc14f2fe 1041 return ERR_PTR(error);
1da177e4
LT
1042 }
1043 s->s_flags |= MS_ACTIVE;
9329d1be
KS
1044 } else {
1045 do_remount_sb(s, flags, data, 0);
1da177e4 1046 }
fc14f2fe
AV
1047 return dget(s->s_root);
1048}
1049EXPORT_SYMBOL(mount_single);
1050
9d412a43
AV
1051struct dentry *
1052mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
1da177e4 1053{
c96e41e9 1054 struct dentry *root;
9d412a43 1055 struct super_block *sb;
1da177e4 1056 char *secdata = NULL;
9d412a43 1057 int error = -ENOMEM;
8089352a 1058
e0007529 1059 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
1da177e4 1060 secdata = alloc_secdata();
454e2398 1061 if (!secdata)
9d412a43 1062 goto out;
1da177e4 1063
e0007529 1064 error = security_sb_copy_data(data, secdata);
454e2398 1065 if (error)
1da177e4 1066 goto out_free_secdata;
1da177e4
LT
1067 }
1068
1a102ff9
AV
1069 root = type->mount(type, flags, name, data);
1070 if (IS_ERR(root)) {
1071 error = PTR_ERR(root);
1072 goto out_free_secdata;
c96e41e9 1073 }
9d412a43
AV
1074 sb = root->d_sb;
1075 BUG_ON(!sb);
1076 WARN_ON(!sb->s_bdi);
6c510389 1077 WARN_ON(sb->s_bdi == &default_backing_dev_info);
9d412a43 1078 sb->s_flags |= MS_BORN;
454e2398 1079
9d412a43 1080 error = security_sb_kern_mount(sb, flags, secdata);
5129a469
JE
1081 if (error)
1082 goto out_sb;
454e2398 1083
42cb56ae
JL
1084 /*
1085 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1086 * but s_maxbytes was an unsigned long long for many releases. Throw
1087 * this warning for a little while to try and catch filesystems that
4358b567 1088 * violate this rule.
42cb56ae 1089 */
9d412a43
AV
1090 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1091 "negative value (%lld)\n", type->name, sb->s_maxbytes);
42cb56ae 1092
9d412a43 1093 up_write(&sb->s_umount);
8680e22f 1094 free_secdata(secdata);
9d412a43 1095 return root;
1da177e4 1096out_sb:
9d412a43
AV
1097 dput(root);
1098 deactivate_locked_super(sb);
1da177e4
LT
1099out_free_secdata:
1100 free_secdata(secdata);
1da177e4 1101out:
454e2398 1102 return ERR_PTR(error);
1da177e4
LT
1103}
1104
18e9e510 1105/**
7000d3c4
RD
1106 * freeze_super - lock the filesystem and force it into a consistent state
1107 * @sb: the super to lock
18e9e510
JB
1108 *
1109 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1110 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1111 * -EBUSY.
1112 */
1113int freeze_super(struct super_block *sb)
1114{
1115 int ret;
1116
1117 atomic_inc(&sb->s_active);
1118 down_write(&sb->s_umount);
1119 if (sb->s_frozen) {
1120 deactivate_locked_super(sb);
1121 return -EBUSY;
1122 }
1123
1124 if (sb->s_flags & MS_RDONLY) {
1125 sb->s_frozen = SB_FREEZE_TRANS;
1126 smp_wmb();
1127 up_write(&sb->s_umount);
1128 return 0;
1129 }
1130
1131 sb->s_frozen = SB_FREEZE_WRITE;
1132 smp_wmb();
1133
1134 sync_filesystem(sb);
1135
1136 sb->s_frozen = SB_FREEZE_TRANS;
1137 smp_wmb();
1138
1139 sync_blockdev(sb->s_bdev);
1140 if (sb->s_op->freeze_fs) {
1141 ret = sb->s_op->freeze_fs(sb);
1142 if (ret) {
1143 printk(KERN_ERR
1144 "VFS:Filesystem freeze failed\n");
1145 sb->s_frozen = SB_UNFROZEN;
1146 deactivate_locked_super(sb);
1147 return ret;
1148 }
1149 }
1150 up_write(&sb->s_umount);
1151 return 0;
1152}
1153EXPORT_SYMBOL(freeze_super);
1154
1155/**
1156 * thaw_super -- unlock filesystem
1157 * @sb: the super to thaw
1158 *
1159 * Unlocks the filesystem and marks it writeable again after freeze_super().
1160 */
1161int thaw_super(struct super_block *sb)
1162{
1163 int error;
1164
1165 down_write(&sb->s_umount);
1166 if (sb->s_frozen == SB_UNFROZEN) {
1167 up_write(&sb->s_umount);
1168 return -EINVAL;
1169 }
1170
1171 if (sb->s_flags & MS_RDONLY)
1172 goto out;
1173
1174 if (sb->s_op->unfreeze_fs) {
1175 error = sb->s_op->unfreeze_fs(sb);
1176 if (error) {
1177 printk(KERN_ERR
1178 "VFS:Filesystem thaw failed\n");
1179 sb->s_frozen = SB_FREEZE_TRANS;
1180 up_write(&sb->s_umount);
1181 return error;
1182 }
1183 }
1184
1185out:
1186 sb->s_frozen = SB_UNFROZEN;
1187 smp_wmb();
1188 wake_up(&sb->s_wait_unfrozen);
1189 deactivate_locked_super(sb);
1190
1191 return 0;
1192}
1193EXPORT_SYMBOL(thaw_super);