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