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