inode: move to per-sb LRU locks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / super.c
blob73ab9f9b35716c7d20bb896de267b866054bbfec
1 /*
2 * linux/fs/super.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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
13 * GK 2/5/95 - Changed to support mounting the root fs via NFS
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:
18 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
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
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/acct.h>
26 #include <linux/blkdev.h>
27 #include <linux/mount.h>
28 #include <linux/security.h>
29 #include <linux/writeback.h> /* for the emergency remount stuff */
30 #include <linux/idr.h>
31 #include <linux/mutex.h>
32 #include <linux/backing-dev.h>
33 #include <linux/rculist_bl.h>
34 #include <linux/cleancache.h>
35 #include "internal.h"
38 LIST_HEAD(super_blocks);
39 DEFINE_SPINLOCK(sb_lock);
41 /**
42 * alloc_super - create new superblock
43 * @type: filesystem type superblock should belong to
45 * Allocates and initializes a new &struct super_block. alloc_super()
46 * returns a pointer new superblock or %NULL if allocation had failed.
48 static struct super_block *alloc_super(struct file_system_type *type)
50 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
51 static const struct super_operations default_op;
53 if (s) {
54 if (security_sb_alloc(s)) {
55 kfree(s);
56 s = NULL;
57 goto out;
59 #ifdef CONFIG_SMP
60 s->s_files = alloc_percpu(struct list_head);
61 if (!s->s_files) {
62 security_sb_free(s);
63 kfree(s);
64 s = NULL;
65 goto out;
66 } else {
67 int i;
69 for_each_possible_cpu(i)
70 INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i));
72 #else
73 INIT_LIST_HEAD(&s->s_files);
74 #endif
75 s->s_bdi = &default_backing_dev_info;
76 INIT_LIST_HEAD(&s->s_instances);
77 INIT_HLIST_BL_HEAD(&s->s_anon);
78 INIT_LIST_HEAD(&s->s_inodes);
79 INIT_LIST_HEAD(&s->s_dentry_lru);
80 INIT_LIST_HEAD(&s->s_inode_lru);
81 spin_lock_init(&s->s_inode_lru_lock);
82 init_rwsem(&s->s_umount);
83 mutex_init(&s->s_lock);
84 lockdep_set_class(&s->s_umount, &type->s_umount_key);
86 * The locking rules for s_lock are up to the
87 * filesystem. For example ext3fs has different
88 * lock ordering than usbfs:
90 lockdep_set_class(&s->s_lock, &type->s_lock_key);
92 * sget() can have s_umount recursion.
94 * When it cannot find a suitable sb, it allocates a new
95 * one (this one), and tries again to find a suitable old
96 * one.
98 * In case that succeeds, it will acquire the s_umount
99 * lock of the old one. Since these are clearly distrinct
100 * locks, and this object isn't exposed yet, there's no
101 * risk of deadlocks.
103 * Annotate this by putting this lock in a different
104 * subclass.
106 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
107 s->s_count = 1;
108 atomic_set(&s->s_active, 1);
109 mutex_init(&s->s_vfs_rename_mutex);
110 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
111 mutex_init(&s->s_dquot.dqio_mutex);
112 mutex_init(&s->s_dquot.dqonoff_mutex);
113 init_rwsem(&s->s_dquot.dqptr_sem);
114 init_waitqueue_head(&s->s_wait_unfrozen);
115 s->s_maxbytes = MAX_NON_LFS;
116 s->s_op = &default_op;
117 s->s_time_gran = 1000000000;
118 s->cleancache_poolid = -1;
120 out:
121 return s;
125 * destroy_super - frees a superblock
126 * @s: superblock to free
128 * Frees a superblock.
130 static inline void destroy_super(struct super_block *s)
132 #ifdef CONFIG_SMP
133 free_percpu(s->s_files);
134 #endif
135 security_sb_free(s);
136 kfree(s->s_subtype);
137 kfree(s->s_options);
138 kfree(s);
141 /* Superblock refcounting */
144 * Drop a superblock's refcount. The caller must hold sb_lock.
146 void __put_super(struct super_block *sb)
148 if (!--sb->s_count) {
149 list_del_init(&sb->s_list);
150 destroy_super(sb);
155 * put_super - drop a temporary reference to superblock
156 * @sb: superblock in question
158 * Drops a temporary reference, frees superblock if there's no
159 * references left.
161 void put_super(struct super_block *sb)
163 spin_lock(&sb_lock);
164 __put_super(sb);
165 spin_unlock(&sb_lock);
170 * deactivate_locked_super - drop an active reference to superblock
171 * @s: superblock to deactivate
173 * Drops an active reference to superblock, converting it into a temprory
174 * one if there is no other active references left. In that case we
175 * tell fs driver to shut it down and drop the temporary reference we
176 * had just acquired.
178 * Caller holds exclusive lock on superblock; that lock is released.
180 void deactivate_locked_super(struct super_block *s)
182 struct file_system_type *fs = s->s_type;
183 if (atomic_dec_and_test(&s->s_active)) {
184 cleancache_flush_fs(s);
185 fs->kill_sb(s);
187 * We need to call rcu_barrier so all the delayed rcu free
188 * inodes are flushed before we release the fs module.
190 rcu_barrier();
191 put_filesystem(fs);
192 put_super(s);
193 } else {
194 up_write(&s->s_umount);
198 EXPORT_SYMBOL(deactivate_locked_super);
201 * deactivate_super - drop an active reference to superblock
202 * @s: superblock to deactivate
204 * Variant of deactivate_locked_super(), except that superblock is *not*
205 * locked by caller. If we are going to drop the final active reference,
206 * lock will be acquired prior to that.
208 void deactivate_super(struct super_block *s)
210 if (!atomic_add_unless(&s->s_active, -1, 1)) {
211 down_write(&s->s_umount);
212 deactivate_locked_super(s);
216 EXPORT_SYMBOL(deactivate_super);
219 * grab_super - acquire an active reference
220 * @s: reference we are trying to make active
222 * Tries to acquire an active reference. grab_super() is used when we
223 * had just found a superblock in super_blocks or fs_type->fs_supers
224 * and want to turn it into a full-blown active reference. grab_super()
225 * is called with sb_lock held and drops it. Returns 1 in case of
226 * success, 0 if we had failed (superblock contents was already dead or
227 * dying when grab_super() had been called).
229 static int grab_super(struct super_block *s) __releases(sb_lock)
231 if (atomic_inc_not_zero(&s->s_active)) {
232 spin_unlock(&sb_lock);
233 return 1;
235 /* it's going away */
236 s->s_count++;
237 spin_unlock(&sb_lock);
238 /* wait for it to die */
239 down_write(&s->s_umount);
240 up_write(&s->s_umount);
241 put_super(s);
242 return 0;
246 * Superblock locking. We really ought to get rid of these two.
248 void lock_super(struct super_block * sb)
250 get_fs_excl();
251 mutex_lock(&sb->s_lock);
254 void unlock_super(struct super_block * sb)
256 put_fs_excl();
257 mutex_unlock(&sb->s_lock);
260 EXPORT_SYMBOL(lock_super);
261 EXPORT_SYMBOL(unlock_super);
264 * generic_shutdown_super - common helper for ->kill_sb()
265 * @sb: superblock to kill
267 * generic_shutdown_super() does all fs-independent work on superblock
268 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
269 * that need destruction out of superblock, call generic_shutdown_super()
270 * and release aforementioned objects. Note: dentries and inodes _are_
271 * taken care of and do not need specific handling.
273 * Upon calling this function, the filesystem may no longer alter or
274 * rearrange the set of dentries belonging to this super_block, nor may it
275 * change the attachments of dentries to inodes.
277 void generic_shutdown_super(struct super_block *sb)
279 const struct super_operations *sop = sb->s_op;
282 if (sb->s_root) {
283 shrink_dcache_for_umount(sb);
284 sync_filesystem(sb);
285 get_fs_excl();
286 sb->s_flags &= ~MS_ACTIVE;
288 fsnotify_unmount_inodes(&sb->s_inodes);
290 evict_inodes(sb);
292 if (sop->put_super)
293 sop->put_super(sb);
295 if (!list_empty(&sb->s_inodes)) {
296 printk("VFS: Busy inodes after unmount of %s. "
297 "Self-destruct in 5 seconds. Have a nice day...\n",
298 sb->s_id);
300 put_fs_excl();
302 spin_lock(&sb_lock);
303 /* should be initialized for __put_super_and_need_restart() */
304 list_del_init(&sb->s_instances);
305 spin_unlock(&sb_lock);
306 up_write(&sb->s_umount);
309 EXPORT_SYMBOL(generic_shutdown_super);
312 * sget - find or create a superblock
313 * @type: filesystem type superblock should belong to
314 * @test: comparison callback
315 * @set: setup callback
316 * @data: argument to each of them
318 struct super_block *sget(struct file_system_type *type,
319 int (*test)(struct super_block *,void *),
320 int (*set)(struct super_block *,void *),
321 void *data)
323 struct super_block *s = NULL;
324 struct super_block *old;
325 int err;
327 retry:
328 spin_lock(&sb_lock);
329 if (test) {
330 list_for_each_entry(old, &type->fs_supers, s_instances) {
331 if (!test(old, data))
332 continue;
333 if (!grab_super(old))
334 goto retry;
335 if (s) {
336 up_write(&s->s_umount);
337 destroy_super(s);
338 s = NULL;
340 down_write(&old->s_umount);
341 if (unlikely(!(old->s_flags & MS_BORN))) {
342 deactivate_locked_super(old);
343 goto retry;
345 return old;
348 if (!s) {
349 spin_unlock(&sb_lock);
350 s = alloc_super(type);
351 if (!s)
352 return ERR_PTR(-ENOMEM);
353 goto retry;
356 err = set(s, data);
357 if (err) {
358 spin_unlock(&sb_lock);
359 up_write(&s->s_umount);
360 destroy_super(s);
361 return ERR_PTR(err);
363 s->s_type = type;
364 strlcpy(s->s_id, type->name, sizeof(s->s_id));
365 list_add_tail(&s->s_list, &super_blocks);
366 list_add(&s->s_instances, &type->fs_supers);
367 spin_unlock(&sb_lock);
368 get_filesystem(type);
369 return s;
372 EXPORT_SYMBOL(sget);
374 void drop_super(struct super_block *sb)
376 up_read(&sb->s_umount);
377 put_super(sb);
380 EXPORT_SYMBOL(drop_super);
383 * sync_supers - helper for periodic superblock writeback
385 * Call the write_super method if present on all dirty superblocks in
386 * the system. This is for the periodic writeback used by most older
387 * filesystems. For data integrity superblock writeback use
388 * sync_filesystems() instead.
390 * Note: check the dirty flag before waiting, so we don't
391 * hold up the sync while mounting a device. (The newly
392 * mounted device won't need syncing.)
394 void sync_supers(void)
396 struct super_block *sb, *p = NULL;
398 spin_lock(&sb_lock);
399 list_for_each_entry(sb, &super_blocks, s_list) {
400 if (list_empty(&sb->s_instances))
401 continue;
402 if (sb->s_op->write_super && sb->s_dirt) {
403 sb->s_count++;
404 spin_unlock(&sb_lock);
406 down_read(&sb->s_umount);
407 if (sb->s_root && sb->s_dirt)
408 sb->s_op->write_super(sb);
409 up_read(&sb->s_umount);
411 spin_lock(&sb_lock);
412 if (p)
413 __put_super(p);
414 p = sb;
417 if (p)
418 __put_super(p);
419 spin_unlock(&sb_lock);
423 * iterate_supers - call function for all active superblocks
424 * @f: function to call
425 * @arg: argument to pass to it
427 * Scans the superblock list and calls given function, passing it
428 * locked superblock and given argument.
430 void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
432 struct super_block *sb, *p = NULL;
434 spin_lock(&sb_lock);
435 list_for_each_entry(sb, &super_blocks, s_list) {
436 if (list_empty(&sb->s_instances))
437 continue;
438 sb->s_count++;
439 spin_unlock(&sb_lock);
441 down_read(&sb->s_umount);
442 if (sb->s_root)
443 f(sb, arg);
444 up_read(&sb->s_umount);
446 spin_lock(&sb_lock);
447 if (p)
448 __put_super(p);
449 p = sb;
451 if (p)
452 __put_super(p);
453 spin_unlock(&sb_lock);
457 * iterate_supers_type - call function for superblocks of given type
458 * @type: fs type
459 * @f: function to call
460 * @arg: argument to pass to it
462 * Scans the superblock list and calls given function, passing it
463 * locked superblock and given argument.
465 void iterate_supers_type(struct file_system_type *type,
466 void (*f)(struct super_block *, void *), void *arg)
468 struct super_block *sb, *p = NULL;
470 spin_lock(&sb_lock);
471 list_for_each_entry(sb, &type->fs_supers, s_instances) {
472 sb->s_count++;
473 spin_unlock(&sb_lock);
475 down_read(&sb->s_umount);
476 if (sb->s_root)
477 f(sb, arg);
478 up_read(&sb->s_umount);
480 spin_lock(&sb_lock);
481 if (p)
482 __put_super(p);
483 p = sb;
485 if (p)
486 __put_super(p);
487 spin_unlock(&sb_lock);
490 EXPORT_SYMBOL(iterate_supers_type);
493 * get_super - get the superblock of a device
494 * @bdev: device to get the superblock for
496 * Scans the superblock list and finds the superblock of the file system
497 * mounted on the device given. %NULL is returned if no match is found.
500 struct super_block *get_super(struct block_device *bdev)
502 struct super_block *sb;
504 if (!bdev)
505 return NULL;
507 spin_lock(&sb_lock);
508 rescan:
509 list_for_each_entry(sb, &super_blocks, s_list) {
510 if (list_empty(&sb->s_instances))
511 continue;
512 if (sb->s_bdev == bdev) {
513 sb->s_count++;
514 spin_unlock(&sb_lock);
515 down_read(&sb->s_umount);
516 /* still alive? */
517 if (sb->s_root)
518 return sb;
519 up_read(&sb->s_umount);
520 /* nope, got unmounted */
521 spin_lock(&sb_lock);
522 __put_super(sb);
523 goto rescan;
526 spin_unlock(&sb_lock);
527 return NULL;
530 EXPORT_SYMBOL(get_super);
533 * get_active_super - get an active reference to the superblock of a device
534 * @bdev: device to get the superblock for
536 * Scans the superblock list and finds the superblock of the file system
537 * mounted on the device given. Returns the superblock with an active
538 * reference or %NULL if none was found.
540 struct super_block *get_active_super(struct block_device *bdev)
542 struct super_block *sb;
544 if (!bdev)
545 return NULL;
547 restart:
548 spin_lock(&sb_lock);
549 list_for_each_entry(sb, &super_blocks, s_list) {
550 if (list_empty(&sb->s_instances))
551 continue;
552 if (sb->s_bdev == bdev) {
553 if (grab_super(sb)) /* drops sb_lock */
554 return sb;
555 else
556 goto restart;
559 spin_unlock(&sb_lock);
560 return NULL;
563 struct super_block *user_get_super(dev_t dev)
565 struct super_block *sb;
567 spin_lock(&sb_lock);
568 rescan:
569 list_for_each_entry(sb, &super_blocks, s_list) {
570 if (list_empty(&sb->s_instances))
571 continue;
572 if (sb->s_dev == dev) {
573 sb->s_count++;
574 spin_unlock(&sb_lock);
575 down_read(&sb->s_umount);
576 /* still alive? */
577 if (sb->s_root)
578 return sb;
579 up_read(&sb->s_umount);
580 /* nope, got unmounted */
581 spin_lock(&sb_lock);
582 __put_super(sb);
583 goto rescan;
586 spin_unlock(&sb_lock);
587 return NULL;
591 * do_remount_sb - asks filesystem to change mount options.
592 * @sb: superblock in question
593 * @flags: numeric part of options
594 * @data: the rest of options
595 * @force: whether or not to force the change
597 * Alters the mount options of a mounted file system.
599 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
601 int retval;
602 int remount_ro;
604 if (sb->s_frozen != SB_UNFROZEN)
605 return -EBUSY;
607 #ifdef CONFIG_BLOCK
608 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
609 return -EACCES;
610 #endif
612 if (flags & MS_RDONLY)
613 acct_auto_close(sb);
614 shrink_dcache_sb(sb);
615 sync_filesystem(sb);
617 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
619 /* If we are remounting RDONLY and current sb is read/write,
620 make sure there are no rw files opened */
621 if (remount_ro) {
622 if (force)
623 mark_files_ro(sb);
624 else if (!fs_may_remount_ro(sb))
625 return -EBUSY;
628 if (sb->s_op->remount_fs) {
629 retval = sb->s_op->remount_fs(sb, &flags, data);
630 if (retval)
631 return retval;
633 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
636 * Some filesystems modify their metadata via some other path than the
637 * bdev buffer cache (eg. use a private mapping, or directories in
638 * pagecache, etc). Also file data modifications go via their own
639 * mappings. So If we try to mount readonly then copy the filesystem
640 * from bdev, we could get stale data, so invalidate it to give a best
641 * effort at coherency.
643 if (remount_ro && sb->s_bdev)
644 invalidate_bdev(sb->s_bdev);
645 return 0;
648 static void do_emergency_remount(struct work_struct *work)
650 struct super_block *sb, *p = NULL;
652 spin_lock(&sb_lock);
653 list_for_each_entry(sb, &super_blocks, s_list) {
654 if (list_empty(&sb->s_instances))
655 continue;
656 sb->s_count++;
657 spin_unlock(&sb_lock);
658 down_write(&sb->s_umount);
659 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
661 * What lock protects sb->s_flags??
663 do_remount_sb(sb, MS_RDONLY, NULL, 1);
665 up_write(&sb->s_umount);
666 spin_lock(&sb_lock);
667 if (p)
668 __put_super(p);
669 p = sb;
671 if (p)
672 __put_super(p);
673 spin_unlock(&sb_lock);
674 kfree(work);
675 printk("Emergency Remount complete\n");
678 void emergency_remount(void)
680 struct work_struct *work;
682 work = kmalloc(sizeof(*work), GFP_ATOMIC);
683 if (work) {
684 INIT_WORK(work, do_emergency_remount);
685 schedule_work(work);
690 * Unnamed block devices are dummy devices used by virtual
691 * filesystems which don't use real block-devices. -- jrs
694 static DEFINE_IDA(unnamed_dev_ida);
695 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
696 static int unnamed_dev_start = 0; /* don't bother trying below it */
698 int get_anon_bdev(dev_t *p)
700 int dev;
701 int error;
703 retry:
704 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
705 return -ENOMEM;
706 spin_lock(&unnamed_dev_lock);
707 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
708 if (!error)
709 unnamed_dev_start = dev + 1;
710 spin_unlock(&unnamed_dev_lock);
711 if (error == -EAGAIN)
712 /* We raced and lost with another CPU. */
713 goto retry;
714 else if (error)
715 return -EAGAIN;
717 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
718 spin_lock(&unnamed_dev_lock);
719 ida_remove(&unnamed_dev_ida, dev);
720 if (unnamed_dev_start > dev)
721 unnamed_dev_start = dev;
722 spin_unlock(&unnamed_dev_lock);
723 return -EMFILE;
725 *p = MKDEV(0, dev & MINORMASK);
726 return 0;
728 EXPORT_SYMBOL(get_anon_bdev);
730 void free_anon_bdev(dev_t dev)
732 int slot = MINOR(dev);
733 spin_lock(&unnamed_dev_lock);
734 ida_remove(&unnamed_dev_ida, slot);
735 if (slot < unnamed_dev_start)
736 unnamed_dev_start = slot;
737 spin_unlock(&unnamed_dev_lock);
739 EXPORT_SYMBOL(free_anon_bdev);
741 int set_anon_super(struct super_block *s, void *data)
743 int error = get_anon_bdev(&s->s_dev);
744 if (!error)
745 s->s_bdi = &noop_backing_dev_info;
746 return error;
749 EXPORT_SYMBOL(set_anon_super);
751 void kill_anon_super(struct super_block *sb)
753 dev_t dev = sb->s_dev;
754 generic_shutdown_super(sb);
755 free_anon_bdev(dev);
758 EXPORT_SYMBOL(kill_anon_super);
760 void kill_litter_super(struct super_block *sb)
762 if (sb->s_root)
763 d_genocide(sb->s_root);
764 kill_anon_super(sb);
767 EXPORT_SYMBOL(kill_litter_super);
769 static int ns_test_super(struct super_block *sb, void *data)
771 return sb->s_fs_info == data;
774 static int ns_set_super(struct super_block *sb, void *data)
776 sb->s_fs_info = data;
777 return set_anon_super(sb, NULL);
780 struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
781 void *data, int (*fill_super)(struct super_block *, void *, int))
783 struct super_block *sb;
785 sb = sget(fs_type, ns_test_super, ns_set_super, data);
786 if (IS_ERR(sb))
787 return ERR_CAST(sb);
789 if (!sb->s_root) {
790 int err;
791 sb->s_flags = flags;
792 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
793 if (err) {
794 deactivate_locked_super(sb);
795 return ERR_PTR(err);
798 sb->s_flags |= MS_ACTIVE;
801 return dget(sb->s_root);
804 EXPORT_SYMBOL(mount_ns);
806 #ifdef CONFIG_BLOCK
807 static int set_bdev_super(struct super_block *s, void *data)
809 s->s_bdev = data;
810 s->s_dev = s->s_bdev->bd_dev;
813 * We set the bdi here to the queue backing, file systems can
814 * overwrite this in ->fill_super()
816 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
817 return 0;
820 static int test_bdev_super(struct super_block *s, void *data)
822 return (void *)s->s_bdev == data;
825 struct dentry *mount_bdev(struct file_system_type *fs_type,
826 int flags, const char *dev_name, void *data,
827 int (*fill_super)(struct super_block *, void *, int))
829 struct block_device *bdev;
830 struct super_block *s;
831 fmode_t mode = FMODE_READ | FMODE_EXCL;
832 int error = 0;
834 if (!(flags & MS_RDONLY))
835 mode |= FMODE_WRITE;
837 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
838 if (IS_ERR(bdev))
839 return ERR_CAST(bdev);
842 * once the super is inserted into the list by sget, s_umount
843 * will protect the lockfs code from trying to start a snapshot
844 * while we are mounting
846 mutex_lock(&bdev->bd_fsfreeze_mutex);
847 if (bdev->bd_fsfreeze_count > 0) {
848 mutex_unlock(&bdev->bd_fsfreeze_mutex);
849 error = -EBUSY;
850 goto error_bdev;
852 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
853 mutex_unlock(&bdev->bd_fsfreeze_mutex);
854 if (IS_ERR(s))
855 goto error_s;
857 if (s->s_root) {
858 if ((flags ^ s->s_flags) & MS_RDONLY) {
859 deactivate_locked_super(s);
860 error = -EBUSY;
861 goto error_bdev;
865 * s_umount nests inside bd_mutex during
866 * __invalidate_device(). blkdev_put() acquires
867 * bd_mutex and can't be called under s_umount. Drop
868 * s_umount temporarily. This is safe as we're
869 * holding an active reference.
871 up_write(&s->s_umount);
872 blkdev_put(bdev, mode);
873 down_write(&s->s_umount);
874 } else {
875 char b[BDEVNAME_SIZE];
877 s->s_flags = flags | MS_NOSEC;
878 s->s_mode = mode;
879 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
880 sb_set_blocksize(s, block_size(bdev));
881 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
882 if (error) {
883 deactivate_locked_super(s);
884 goto error;
887 s->s_flags |= MS_ACTIVE;
888 bdev->bd_super = s;
891 return dget(s->s_root);
893 error_s:
894 error = PTR_ERR(s);
895 error_bdev:
896 blkdev_put(bdev, mode);
897 error:
898 return ERR_PTR(error);
900 EXPORT_SYMBOL(mount_bdev);
902 void kill_block_super(struct super_block *sb)
904 struct block_device *bdev = sb->s_bdev;
905 fmode_t mode = sb->s_mode;
907 bdev->bd_super = NULL;
908 generic_shutdown_super(sb);
909 sync_blockdev(bdev);
910 WARN_ON_ONCE(!(mode & FMODE_EXCL));
911 blkdev_put(bdev, mode | FMODE_EXCL);
914 EXPORT_SYMBOL(kill_block_super);
915 #endif
917 struct dentry *mount_nodev(struct file_system_type *fs_type,
918 int flags, void *data,
919 int (*fill_super)(struct super_block *, void *, int))
921 int error;
922 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
924 if (IS_ERR(s))
925 return ERR_CAST(s);
927 s->s_flags = flags;
929 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
930 if (error) {
931 deactivate_locked_super(s);
932 return ERR_PTR(error);
934 s->s_flags |= MS_ACTIVE;
935 return dget(s->s_root);
937 EXPORT_SYMBOL(mount_nodev);
939 static int compare_single(struct super_block *s, void *p)
941 return 1;
944 struct dentry *mount_single(struct file_system_type *fs_type,
945 int flags, void *data,
946 int (*fill_super)(struct super_block *, void *, int))
948 struct super_block *s;
949 int error;
951 s = sget(fs_type, compare_single, set_anon_super, NULL);
952 if (IS_ERR(s))
953 return ERR_CAST(s);
954 if (!s->s_root) {
955 s->s_flags = flags;
956 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
957 if (error) {
958 deactivate_locked_super(s);
959 return ERR_PTR(error);
961 s->s_flags |= MS_ACTIVE;
962 } else {
963 do_remount_sb(s, flags, data, 0);
965 return dget(s->s_root);
967 EXPORT_SYMBOL(mount_single);
969 struct dentry *
970 mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
972 struct dentry *root;
973 struct super_block *sb;
974 char *secdata = NULL;
975 int error = -ENOMEM;
977 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
978 secdata = alloc_secdata();
979 if (!secdata)
980 goto out;
982 error = security_sb_copy_data(data, secdata);
983 if (error)
984 goto out_free_secdata;
987 root = type->mount(type, flags, name, data);
988 if (IS_ERR(root)) {
989 error = PTR_ERR(root);
990 goto out_free_secdata;
992 sb = root->d_sb;
993 BUG_ON(!sb);
994 WARN_ON(!sb->s_bdi);
995 WARN_ON(sb->s_bdi == &default_backing_dev_info);
996 sb->s_flags |= MS_BORN;
998 error = security_sb_kern_mount(sb, flags, secdata);
999 if (error)
1000 goto out_sb;
1003 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1004 * but s_maxbytes was an unsigned long long for many releases. Throw
1005 * this warning for a little while to try and catch filesystems that
1006 * violate this rule.
1008 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
1009 "negative value (%lld)\n", type->name, sb->s_maxbytes);
1011 up_write(&sb->s_umount);
1012 free_secdata(secdata);
1013 return root;
1014 out_sb:
1015 dput(root);
1016 deactivate_locked_super(sb);
1017 out_free_secdata:
1018 free_secdata(secdata);
1019 out:
1020 return ERR_PTR(error);
1024 * freeze_super - lock the filesystem and force it into a consistent state
1025 * @sb: the super to lock
1027 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1028 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1029 * -EBUSY.
1031 int freeze_super(struct super_block *sb)
1033 int ret;
1035 atomic_inc(&sb->s_active);
1036 down_write(&sb->s_umount);
1037 if (sb->s_frozen) {
1038 deactivate_locked_super(sb);
1039 return -EBUSY;
1042 if (sb->s_flags & MS_RDONLY) {
1043 sb->s_frozen = SB_FREEZE_TRANS;
1044 smp_wmb();
1045 up_write(&sb->s_umount);
1046 return 0;
1049 sb->s_frozen = SB_FREEZE_WRITE;
1050 smp_wmb();
1052 sync_filesystem(sb);
1054 sb->s_frozen = SB_FREEZE_TRANS;
1055 smp_wmb();
1057 sync_blockdev(sb->s_bdev);
1058 if (sb->s_op->freeze_fs) {
1059 ret = sb->s_op->freeze_fs(sb);
1060 if (ret) {
1061 printk(KERN_ERR
1062 "VFS:Filesystem freeze failed\n");
1063 sb->s_frozen = SB_UNFROZEN;
1064 deactivate_locked_super(sb);
1065 return ret;
1068 up_write(&sb->s_umount);
1069 return 0;
1071 EXPORT_SYMBOL(freeze_super);
1074 * thaw_super -- unlock filesystem
1075 * @sb: the super to thaw
1077 * Unlocks the filesystem and marks it writeable again after freeze_super().
1079 int thaw_super(struct super_block *sb)
1081 int error;
1083 down_write(&sb->s_umount);
1084 if (sb->s_frozen == SB_UNFROZEN) {
1085 up_write(&sb->s_umount);
1086 return -EINVAL;
1089 if (sb->s_flags & MS_RDONLY)
1090 goto out;
1092 if (sb->s_op->unfreeze_fs) {
1093 error = sb->s_op->unfreeze_fs(sb);
1094 if (error) {
1095 printk(KERN_ERR
1096 "VFS:Filesystem thaw failed\n");
1097 sb->s_frozen = SB_FREEZE_TRANS;
1098 up_write(&sb->s_umount);
1099 return error;
1103 out:
1104 sb->s_frozen = SB_UNFROZEN;
1105 smp_wmb();
1106 wake_up(&sb->s_wait_unfrozen);
1107 deactivate_locked_super(sb);
1109 return 0;
1111 EXPORT_SYMBOL(thaw_super);