4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * super.c contains code to handle: - mount structures
8 * - filesystem drivers list
10 * - umount 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>
38 LIST_HEAD(super_blocks);
39 DEFINE_SPINLOCK(sb_lock);
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;
54 if (security_sb_alloc(s)) {
60 s->s_files = alloc_percpu(struct list_head);
69 for_each_possible_cpu(i)
70 INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i));
73 INIT_LIST_HEAD(&s->s_files);
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_rwsem(&s->s_umount);
81 mutex_init(&s->s_lock);
82 lockdep_set_class(&s->s_umount, &type->s_umount_key);
84 * The locking rules for s_lock are up to the
85 * filesystem. For example ext3fs has different
86 * lock ordering than usbfs:
88 lockdep_set_class(&s->s_lock, &type->s_lock_key);
90 * sget() can have s_umount recursion.
92 * When it cannot find a suitable sb, it allocates a new
93 * one (this one), and tries again to find a suitable old
96 * In case that succeeds, it will acquire the s_umount
97 * lock of the old one. Since these are clearly distrinct
98 * locks, and this object isn't exposed yet, there's no
101 * Annotate this by putting this lock in a different
104 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
106 atomic_set(&s->s_active, 1);
107 mutex_init(&s->s_vfs_rename_mutex);
108 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
109 mutex_init(&s->s_dquot.dqio_mutex);
110 mutex_init(&s->s_dquot.dqonoff_mutex);
111 init_rwsem(&s->s_dquot.dqptr_sem);
112 init_waitqueue_head(&s->s_wait_unfrozen);
113 s->s_maxbytes = MAX_NON_LFS;
114 s->s_op = &default_op;
115 s->s_time_gran = 1000000000;
116 s->cleancache_poolid = -1;
123 * destroy_super - frees a superblock
124 * @s: superblock to free
126 * Frees a superblock.
128 static inline void destroy_super(struct super_block *s)
131 free_percpu(s->s_files);
139 /* Superblock refcounting */
142 * Drop a superblock's refcount. The caller must hold sb_lock.
144 void __put_super(struct super_block *sb)
146 if (!--sb->s_count) {
147 list_del_init(&sb->s_list);
153 * put_super - drop a temporary reference to superblock
154 * @sb: superblock in question
156 * Drops a temporary reference, frees superblock if there's no
159 void put_super(struct super_block *sb)
163 spin_unlock(&sb_lock);
168 * deactivate_locked_super - drop an active reference to superblock
169 * @s: superblock to deactivate
171 * Drops an active reference to superblock, converting it into a temprory
172 * one if there is no other active references left. In that case we
173 * tell fs driver to shut it down and drop the temporary reference we
176 * Caller holds exclusive lock on superblock; that lock is released.
178 void deactivate_locked_super(struct super_block *s)
180 struct file_system_type *fs = s->s_type;
181 if (atomic_dec_and_test(&s->s_active)) {
182 cleancache_flush_fs(s);
185 * We need to call rcu_barrier so all the delayed rcu free
186 * inodes are flushed before we release the fs module.
192 up_write(&s->s_umount);
196 EXPORT_SYMBOL(deactivate_locked_super);
199 * deactivate_super - drop an active reference to superblock
200 * @s: superblock to deactivate
202 * Variant of deactivate_locked_super(), except that superblock is *not*
203 * locked by caller. If we are going to drop the final active reference,
204 * lock will be acquired prior to that.
206 void deactivate_super(struct super_block *s)
208 if (!atomic_add_unless(&s->s_active, -1, 1)) {
209 down_write(&s->s_umount);
210 deactivate_locked_super(s);
214 EXPORT_SYMBOL(deactivate_super);
217 * grab_super - acquire an active reference
218 * @s: reference we are trying to make active
220 * Tries to acquire an active reference. grab_super() is used when we
221 * had just found a superblock in super_blocks or fs_type->fs_supers
222 * and want to turn it into a full-blown active reference. grab_super()
223 * is called with sb_lock held and drops it. Returns 1 in case of
224 * success, 0 if we had failed (superblock contents was already dead or
225 * dying when grab_super() had been called).
227 static int grab_super(struct super_block *s) __releases(sb_lock)
229 if (atomic_inc_not_zero(&s->s_active)) {
230 spin_unlock(&sb_lock);
233 /* it's going away */
235 spin_unlock(&sb_lock);
236 /* wait for it to die */
237 down_write(&s->s_umount);
238 up_write(&s->s_umount);
244 * Superblock locking. We really ought to get rid of these two.
246 void lock_super(struct super_block * sb)
249 mutex_lock(&sb->s_lock);
252 void unlock_super(struct super_block * sb)
255 mutex_unlock(&sb->s_lock);
258 EXPORT_SYMBOL(lock_super);
259 EXPORT_SYMBOL(unlock_super);
262 * generic_shutdown_super - common helper for ->kill_sb()
263 * @sb: superblock to kill
265 * generic_shutdown_super() does all fs-independent work on superblock
266 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
267 * that need destruction out of superblock, call generic_shutdown_super()
268 * and release aforementioned objects. Note: dentries and inodes _are_
269 * taken care of and do not need specific handling.
271 * Upon calling this function, the filesystem may no longer alter or
272 * rearrange the set of dentries belonging to this super_block, nor may it
273 * change the attachments of dentries to inodes.
275 void generic_shutdown_super(struct super_block *sb)
277 const struct super_operations *sop = sb->s_op;
281 shrink_dcache_for_umount(sb);
284 sb->s_flags &= ~MS_ACTIVE;
286 fsnotify_unmount_inodes(&sb->s_inodes);
293 if (!list_empty(&sb->s_inodes)) {
294 printk("VFS: Busy inodes after unmount of %s. "
295 "Self-destruct in 5 seconds. Have a nice day...\n",
301 /* should be initialized for __put_super_and_need_restart() */
302 list_del_init(&sb->s_instances);
303 spin_unlock(&sb_lock);
304 up_write(&sb->s_umount);
307 EXPORT_SYMBOL(generic_shutdown_super);
310 * sget - find or create a superblock
311 * @type: filesystem type superblock should belong to
312 * @test: comparison callback
313 * @set: setup callback
314 * @data: argument to each of them
316 struct super_block *sget(struct file_system_type *type,
317 int (*test)(struct super_block *,void *),
318 int (*set)(struct super_block *,void *),
321 struct super_block *s = NULL;
322 struct super_block *old;
328 list_for_each_entry(old, &type->fs_supers, s_instances) {
329 if (!test(old, data))
331 if (!grab_super(old))
334 up_write(&s->s_umount);
338 down_write(&old->s_umount);
339 if (unlikely(!(old->s_flags & MS_BORN))) {
340 deactivate_locked_super(old);
347 spin_unlock(&sb_lock);
348 s = alloc_super(type);
350 return ERR_PTR(-ENOMEM);
356 spin_unlock(&sb_lock);
357 up_write(&s->s_umount);
362 strlcpy(s->s_id, type->name, sizeof(s->s_id));
363 list_add_tail(&s->s_list, &super_blocks);
364 list_add(&s->s_instances, &type->fs_supers);
365 spin_unlock(&sb_lock);
366 get_filesystem(type);
372 void drop_super(struct super_block *sb)
374 up_read(&sb->s_umount);
378 EXPORT_SYMBOL(drop_super);
381 * sync_supers - helper for periodic superblock writeback
383 * Call the write_super method if present on all dirty superblocks in
384 * the system. This is for the periodic writeback used by most older
385 * filesystems. For data integrity superblock writeback use
386 * sync_filesystems() instead.
388 * Note: check the dirty flag before waiting, so we don't
389 * hold up the sync while mounting a device. (The newly
390 * mounted device won't need syncing.)
392 void sync_supers(void)
394 struct super_block *sb, *p = NULL;
397 list_for_each_entry(sb, &super_blocks, s_list) {
398 if (list_empty(&sb->s_instances))
400 if (sb->s_op->write_super && sb->s_dirt) {
402 spin_unlock(&sb_lock);
404 down_read(&sb->s_umount);
405 if (sb->s_root && sb->s_dirt)
406 sb->s_op->write_super(sb);
407 up_read(&sb->s_umount);
417 spin_unlock(&sb_lock);
421 * iterate_supers - call function for all active superblocks
422 * @f: function to call
423 * @arg: argument to pass to it
425 * Scans the superblock list and calls given function, passing it
426 * locked superblock and given argument.
428 void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
430 struct super_block *sb, *p = NULL;
433 list_for_each_entry(sb, &super_blocks, s_list) {
434 if (list_empty(&sb->s_instances))
437 spin_unlock(&sb_lock);
439 down_read(&sb->s_umount);
442 up_read(&sb->s_umount);
451 spin_unlock(&sb_lock);
455 * get_super - get the superblock of a device
456 * @bdev: device to get the superblock for
458 * Scans the superblock list and finds the superblock of the file system
459 * mounted on the device given. %NULL is returned if no match is found.
462 struct super_block *get_super(struct block_device *bdev)
464 struct super_block *sb;
471 list_for_each_entry(sb, &super_blocks, s_list) {
472 if (list_empty(&sb->s_instances))
474 if (sb->s_bdev == bdev) {
476 spin_unlock(&sb_lock);
477 down_read(&sb->s_umount);
481 up_read(&sb->s_umount);
482 /* nope, got unmounted */
488 spin_unlock(&sb_lock);
492 EXPORT_SYMBOL(get_super);
495 * get_active_super - get an active reference to the superblock of a device
496 * @bdev: device to get the superblock for
498 * Scans the superblock list and finds the superblock of the file system
499 * mounted on the device given. Returns the superblock with an active
500 * reference or %NULL if none was found.
502 struct super_block *get_active_super(struct block_device *bdev)
504 struct super_block *sb;
511 list_for_each_entry(sb, &super_blocks, s_list) {
512 if (list_empty(&sb->s_instances))
514 if (sb->s_bdev == bdev) {
515 if (grab_super(sb)) /* drops sb_lock */
521 spin_unlock(&sb_lock);
525 struct super_block *user_get_super(dev_t dev)
527 struct super_block *sb;
531 list_for_each_entry(sb, &super_blocks, s_list) {
532 if (list_empty(&sb->s_instances))
534 if (sb->s_dev == dev) {
536 spin_unlock(&sb_lock);
537 down_read(&sb->s_umount);
541 up_read(&sb->s_umount);
542 /* nope, got unmounted */
548 spin_unlock(&sb_lock);
553 * do_remount_sb - asks filesystem to change mount options.
554 * @sb: superblock in question
555 * @flags: numeric part of options
556 * @data: the rest of options
557 * @force: whether or not to force the change
559 * Alters the mount options of a mounted file system.
561 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
566 if (sb->s_frozen != SB_UNFROZEN)
570 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
574 if (flags & MS_RDONLY)
576 shrink_dcache_sb(sb);
579 remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
581 /* If we are remounting RDONLY and current sb is read/write,
582 make sure there are no rw files opened */
586 else if (!fs_may_remount_ro(sb))
590 if (sb->s_op->remount_fs) {
591 retval = sb->s_op->remount_fs(sb, &flags, data);
595 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
598 * Some filesystems modify their metadata via some other path than the
599 * bdev buffer cache (eg. use a private mapping, or directories in
600 * pagecache, etc). Also file data modifications go via their own
601 * mappings. So If we try to mount readonly then copy the filesystem
602 * from bdev, we could get stale data, so invalidate it to give a best
603 * effort at coherency.
605 if (remount_ro && sb->s_bdev)
606 invalidate_bdev(sb->s_bdev);
610 static void do_emergency_remount(struct work_struct *work)
612 struct super_block *sb, *p = NULL;
615 list_for_each_entry(sb, &super_blocks, s_list) {
616 if (list_empty(&sb->s_instances))
619 spin_unlock(&sb_lock);
620 down_write(&sb->s_umount);
621 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
623 * What lock protects sb->s_flags??
625 do_remount_sb(sb, MS_RDONLY, NULL, 1);
627 up_write(&sb->s_umount);
635 spin_unlock(&sb_lock);
637 printk("Emergency Remount complete\n");
640 void emergency_remount(void)
642 struct work_struct *work;
644 work = kmalloc(sizeof(*work), GFP_ATOMIC);
646 INIT_WORK(work, do_emergency_remount);
652 * Unnamed block devices are dummy devices used by virtual
653 * filesystems which don't use real block-devices. -- jrs
656 static DEFINE_IDA(unnamed_dev_ida);
657 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
658 static int unnamed_dev_start = 0; /* don't bother trying below it */
660 int set_anon_super(struct super_block *s, void *data)
666 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
668 spin_lock(&unnamed_dev_lock);
669 error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
671 unnamed_dev_start = dev + 1;
672 spin_unlock(&unnamed_dev_lock);
673 if (error == -EAGAIN)
674 /* We raced and lost with another CPU. */
679 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
680 spin_lock(&unnamed_dev_lock);
681 ida_remove(&unnamed_dev_ida, dev);
682 if (unnamed_dev_start > dev)
683 unnamed_dev_start = dev;
684 spin_unlock(&unnamed_dev_lock);
687 s->s_dev = MKDEV(0, dev & MINORMASK);
688 s->s_bdi = &noop_backing_dev_info;
692 EXPORT_SYMBOL(set_anon_super);
694 void kill_anon_super(struct super_block *sb)
696 int slot = MINOR(sb->s_dev);
698 generic_shutdown_super(sb);
699 spin_lock(&unnamed_dev_lock);
700 ida_remove(&unnamed_dev_ida, slot);
701 if (slot < unnamed_dev_start)
702 unnamed_dev_start = slot;
703 spin_unlock(&unnamed_dev_lock);
706 EXPORT_SYMBOL(kill_anon_super);
708 void kill_litter_super(struct super_block *sb)
711 d_genocide(sb->s_root);
715 EXPORT_SYMBOL(kill_litter_super);
717 static int ns_test_super(struct super_block *sb, void *data)
719 return sb->s_fs_info == data;
722 static int ns_set_super(struct super_block *sb, void *data)
724 sb->s_fs_info = data;
725 return set_anon_super(sb, NULL);
728 struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
729 void *data, int (*fill_super)(struct super_block *, void *, int))
731 struct super_block *sb;
733 sb = sget(fs_type, ns_test_super, ns_set_super, data);
740 err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
742 deactivate_locked_super(sb);
746 sb->s_flags |= MS_ACTIVE;
749 return dget(sb->s_root);
752 EXPORT_SYMBOL(mount_ns);
755 static int set_bdev_super(struct super_block *s, void *data)
758 s->s_dev = s->s_bdev->bd_dev;
761 * We set the bdi here to the queue backing, file systems can
762 * overwrite this in ->fill_super()
764 s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
768 static int test_bdev_super(struct super_block *s, void *data)
770 return (void *)s->s_bdev == data;
773 struct dentry *mount_bdev(struct file_system_type *fs_type,
774 int flags, const char *dev_name, void *data,
775 int (*fill_super)(struct super_block *, void *, int))
777 struct block_device *bdev;
778 struct super_block *s;
779 fmode_t mode = FMODE_READ | FMODE_EXCL;
782 if (!(flags & MS_RDONLY))
785 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
787 return ERR_CAST(bdev);
790 * once the super is inserted into the list by sget, s_umount
791 * will protect the lockfs code from trying to start a snapshot
792 * while we are mounting
794 mutex_lock(&bdev->bd_fsfreeze_mutex);
795 if (bdev->bd_fsfreeze_count > 0) {
796 mutex_unlock(&bdev->bd_fsfreeze_mutex);
800 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
801 mutex_unlock(&bdev->bd_fsfreeze_mutex);
806 if ((flags ^ s->s_flags) & MS_RDONLY) {
807 deactivate_locked_super(s);
813 * s_umount nests inside bd_mutex during
814 * __invalidate_device(). blkdev_put() acquires
815 * bd_mutex and can't be called under s_umount. Drop
816 * s_umount temporarily. This is safe as we're
817 * holding an active reference.
819 up_write(&s->s_umount);
820 blkdev_put(bdev, mode);
821 down_write(&s->s_umount);
823 char b[BDEVNAME_SIZE];
825 s->s_flags = flags | MS_NOSEC;
827 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
828 sb_set_blocksize(s, block_size(bdev));
829 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
831 deactivate_locked_super(s);
835 s->s_flags |= MS_ACTIVE;
839 return dget(s->s_root);
844 blkdev_put(bdev, mode);
846 return ERR_PTR(error);
848 EXPORT_SYMBOL(mount_bdev);
850 void kill_block_super(struct super_block *sb)
852 struct block_device *bdev = sb->s_bdev;
853 fmode_t mode = sb->s_mode;
855 bdev->bd_super = NULL;
856 generic_shutdown_super(sb);
858 WARN_ON_ONCE(!(mode & FMODE_EXCL));
859 blkdev_put(bdev, mode | FMODE_EXCL);
862 EXPORT_SYMBOL(kill_block_super);
865 struct dentry *mount_nodev(struct file_system_type *fs_type,
866 int flags, void *data,
867 int (*fill_super)(struct super_block *, void *, int))
870 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
877 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
879 deactivate_locked_super(s);
880 return ERR_PTR(error);
882 s->s_flags |= MS_ACTIVE;
883 return dget(s->s_root);
885 EXPORT_SYMBOL(mount_nodev);
887 static int compare_single(struct super_block *s, void *p)
892 struct dentry *mount_single(struct file_system_type *fs_type,
893 int flags, void *data,
894 int (*fill_super)(struct super_block *, void *, int))
896 struct super_block *s;
899 s = sget(fs_type, compare_single, set_anon_super, NULL);
904 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
906 deactivate_locked_super(s);
907 return ERR_PTR(error);
909 s->s_flags |= MS_ACTIVE;
911 do_remount_sb(s, flags, data, 0);
913 return dget(s->s_root);
915 EXPORT_SYMBOL(mount_single);
918 mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
921 struct super_block *sb;
922 char *secdata = NULL;
925 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
926 secdata = alloc_secdata();
930 error = security_sb_copy_data(data, secdata);
932 goto out_free_secdata;
935 root = type->mount(type, flags, name, data);
937 error = PTR_ERR(root);
938 goto out_free_secdata;
943 WARN_ON(sb->s_bdi == &default_backing_dev_info);
944 sb->s_flags |= MS_BORN;
946 error = security_sb_kern_mount(sb, flags, secdata);
951 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
952 * but s_maxbytes was an unsigned long long for many releases. Throw
953 * this warning for a little while to try and catch filesystems that
956 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
957 "negative value (%lld)\n", type->name, sb->s_maxbytes);
959 up_write(&sb->s_umount);
960 free_secdata(secdata);
964 deactivate_locked_super(sb);
966 free_secdata(secdata);
968 return ERR_PTR(error);
972 * freeze_super - lock the filesystem and force it into a consistent state
973 * @sb: the super to lock
975 * Syncs the super to make sure the filesystem is consistent and calls the fs's
976 * freeze_fs. Subsequent calls to this without first thawing the fs will return
979 int freeze_super(struct super_block *sb)
983 atomic_inc(&sb->s_active);
984 down_write(&sb->s_umount);
986 deactivate_locked_super(sb);
990 if (sb->s_flags & MS_RDONLY) {
991 sb->s_frozen = SB_FREEZE_TRANS;
993 up_write(&sb->s_umount);
997 sb->s_frozen = SB_FREEZE_WRITE;
1000 sync_filesystem(sb);
1002 sb->s_frozen = SB_FREEZE_TRANS;
1005 sync_blockdev(sb->s_bdev);
1006 if (sb->s_op->freeze_fs) {
1007 ret = sb->s_op->freeze_fs(sb);
1010 "VFS:Filesystem freeze failed\n");
1011 sb->s_frozen = SB_UNFROZEN;
1012 deactivate_locked_super(sb);
1016 up_write(&sb->s_umount);
1019 EXPORT_SYMBOL(freeze_super);
1022 * thaw_super -- unlock filesystem
1023 * @sb: the super to thaw
1025 * Unlocks the filesystem and marks it writeable again after freeze_super().
1027 int thaw_super(struct super_block *sb)
1031 down_write(&sb->s_umount);
1032 if (sb->s_frozen == SB_UNFROZEN) {
1033 up_write(&sb->s_umount);
1037 if (sb->s_flags & MS_RDONLY)
1040 if (sb->s_op->unfreeze_fs) {
1041 error = sb->s_op->unfreeze_fs(sb);
1044 "VFS:Filesystem thaw failed\n");
1045 sb->s_frozen = SB_FREEZE_TRANS;
1046 up_write(&sb->s_umount);
1052 sb->s_frozen = SB_UNFROZEN;
1054 wake_up(&sb->s_wait_unfrozen);
1055 deactivate_locked_super(sb);
1059 EXPORT_SYMBOL(thaw_super);