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/init.h>
26 #include <linux/smp_lock.h>
27 #include <linux/acct.h>
28 #include <linux/blkdev.h>
29 #include <linux/quotaops.h>
30 #include <linux/namei.h>
31 #include <linux/buffer_head.h> /* for fsync_super() */
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/syscalls.h>
35 #include <linux/vfs.h>
36 #include <linux/writeback.h> /* for the emergency remount stuff */
37 #include <linux/idr.h>
38 #include <linux/kobject.h>
39 #include <linux/mutex.h>
40 #include <linux/devpts_fs.h>
41 #include <linux/proc_fs.h>
42 #include <asm/uaccess.h>
43 #include <linux/vs_base.h>
46 void get_filesystem(struct file_system_type *fs);
47 void put_filesystem(struct file_system_type *fs);
48 struct file_system_type *get_fs_type(const char *name);
50 LIST_HEAD(super_blocks);
51 DEFINE_SPINLOCK(sb_lock);
54 * alloc_super - create new superblock
55 * @type: filesystem type superblock should belong to
57 * Allocates and initializes a new &struct super_block. alloc_super()
58 * returns a pointer new superblock or %NULL if allocation had failed.
60 static struct super_block *alloc_super(struct file_system_type *type)
62 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
63 static struct super_operations default_op;
66 if (security_sb_alloc(s)) {
71 INIT_LIST_HEAD(&s->s_dirty);
72 INIT_LIST_HEAD(&s->s_io);
73 INIT_LIST_HEAD(&s->s_files);
74 INIT_LIST_HEAD(&s->s_instances);
75 INIT_HLIST_HEAD(&s->s_anon);
76 INIT_LIST_HEAD(&s->s_inodes);
77 init_rwsem(&s->s_umount);
78 mutex_init(&s->s_lock);
79 lockdep_set_class(&s->s_umount, &type->s_umount_key);
81 * The locking rules for s_lock are up to the
82 * filesystem. For example ext3fs has different
83 * lock ordering than usbfs:
85 lockdep_set_class(&s->s_lock, &type->s_lock_key);
86 down_write(&s->s_umount);
88 atomic_set(&s->s_active, 1);
89 mutex_init(&s->s_vfs_rename_mutex);
90 mutex_init(&s->s_dquot.dqio_mutex);
91 mutex_init(&s->s_dquot.dqonoff_mutex);
92 init_rwsem(&s->s_dquot.dqptr_sem);
93 init_waitqueue_head(&s->s_wait_unfrozen);
94 s->s_maxbytes = MAX_NON_LFS;
95 s->dq_op = sb_dquot_ops;
96 s->s_qcop = sb_quotactl_ops;
97 s->s_op = &default_op;
98 s->s_time_gran = 1000000000;
105 * destroy_super - frees a superblock
106 * @s: superblock to free
108 * Frees a superblock.
110 static inline void destroy_super(struct super_block *s)
116 /* Superblock refcounting */
119 * Drop a superblock's refcount. Returns non-zero if the superblock was
120 * destroyed. The caller must hold sb_lock.
122 int __put_super(struct super_block *sb)
126 if (!--sb->s_count) {
134 * Drop a superblock's refcount.
135 * Returns non-zero if the superblock is about to be destroyed and
136 * at least is already removed from super_blocks list, so if we are
137 * making a loop through super blocks then we need to restart.
138 * The caller must hold sb_lock.
140 int __put_super_and_need_restart(struct super_block *sb)
142 /* check for race with generic_shutdown_super() */
143 if (list_empty(&sb->s_list)) {
144 /* super block is removed, need to restart... */
148 /* can't be the last, since s_list is still in use */
150 BUG_ON(sb->s_count == 0);
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
161 static void put_super(struct super_block *sb)
165 spin_unlock(&sb_lock);
170 * deactivate_super - drop an active reference to superblock
171 * @s: superblock to deactivate
173 * Drops an active reference to superblock, acquiring a temprory one if
174 * there is no active references left. In that case we lock superblock,
175 * tell fs driver to shut it down and drop the temporary reference we
178 void deactivate_super(struct super_block *s)
180 struct file_system_type *fs = s->s_type;
181 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
182 s->s_count -= S_BIAS-1;
183 spin_unlock(&sb_lock);
185 down_write(&s->s_umount);
192 EXPORT_SYMBOL(deactivate_super);
195 * grab_super - acquire an active reference
196 * @s: reference we are trying to make active
198 * Tries to acquire an active reference. grab_super() is used when we
199 * had just found a superblock in super_blocks or fs_type->fs_supers
200 * and want to turn it into a full-blown active reference. grab_super()
201 * is called with sb_lock held and drops it. Returns 1 in case of
202 * success, 0 if we had failed (superblock contents was already dead or
203 * dying when grab_super() had been called).
205 static int grab_super(struct super_block *s)
208 spin_unlock(&sb_lock);
209 down_write(&s->s_umount);
212 if (s->s_count > S_BIAS) {
213 atomic_inc(&s->s_active);
215 spin_unlock(&sb_lock);
218 spin_unlock(&sb_lock);
220 up_write(&s->s_umount);
227 * generic_shutdown_super - common helper for ->kill_sb()
228 * @sb: superblock to kill
230 * generic_shutdown_super() does all fs-independent work on superblock
231 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
232 * that need destruction out of superblock, call generic_shutdown_super()
233 * and release aforementioned objects. Note: dentries and inodes _are_
234 * taken care of and do not need specific handling.
236 * Upon calling this function, the filesystem may no longer alter or
237 * rearrange the set of dentries belonging to this super_block, nor may it
238 * change the attachments of dentries to inodes.
240 void generic_shutdown_super(struct super_block *sb)
242 struct super_operations *sop = sb->s_op;
245 shrink_dcache_for_umount(sb);
248 sb->s_flags &= ~MS_ACTIVE;
249 /* bad name - it should be evict_inodes() */
250 invalidate_inodes(sb);
253 if (sop->write_super && sb->s_dirt)
254 sop->write_super(sb);
258 /* Forget any remaining inodes */
259 if (invalidate_inodes(sb)) {
260 printk("VFS: Busy inodes after unmount of %s. "
261 "Self-destruct in 5 seconds. Have a nice day...\n",
269 /* should be initialized for __put_super_and_need_restart() */
270 list_del_init(&sb->s_list);
271 list_del(&sb->s_instances);
272 spin_unlock(&sb_lock);
273 up_write(&sb->s_umount);
276 EXPORT_SYMBOL(generic_shutdown_super);
279 * sget - find or create a superblock
280 * @type: filesystem type superblock should belong to
281 * @test: comparison callback
282 * @set: setup callback
283 * @data: argument to each of them
285 struct super_block *sget(struct file_system_type *type,
286 int (*test)(struct super_block *,void *),
287 int (*set)(struct super_block *,void *),
290 struct super_block *s = NULL;
296 if (test) list_for_each(p, &type->fs_supers) {
297 struct super_block *old;
298 old = list_entry(p, struct super_block, s_instances);
299 if (!test(old, data))
301 if (!grab_super(old))
308 spin_unlock(&sb_lock);
309 s = alloc_super(type);
311 return ERR_PTR(-ENOMEM);
317 spin_unlock(&sb_lock);
322 strlcpy(s->s_id, type->name, sizeof(s->s_id));
323 list_add_tail(&s->s_list, &super_blocks);
324 list_add(&s->s_instances, &type->fs_supers);
325 spin_unlock(&sb_lock);
326 get_filesystem(type);
332 void drop_super(struct super_block *sb)
334 up_read(&sb->s_umount);
338 EXPORT_SYMBOL(drop_super);
340 static inline void write_super(struct super_block *sb)
343 if (sb->s_root && sb->s_dirt)
344 if (sb->s_op->write_super)
345 sb->s_op->write_super(sb);
350 * Note: check the dirty flag before waiting, so we don't
351 * hold up the sync while mounting a device. (The newly
352 * mounted device won't need syncing.)
354 void sync_supers(void)
356 struct super_block *sb;
360 list_for_each_entry(sb, &super_blocks, s_list) {
363 spin_unlock(&sb_lock);
364 down_read(&sb->s_umount);
366 up_read(&sb->s_umount);
368 if (__put_super_and_need_restart(sb))
372 spin_unlock(&sb_lock);
376 * Call the ->sync_fs super_op against all filesytems which are r/w and
377 * which implement it.
379 * This operation is careful to avoid the livelock which could easily happen
380 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
381 * is used only here. We set it against all filesystems and then clear it as
382 * we sync them. So redirtied filesystems are skipped.
384 * But if process A is currently running sync_filesytems and then process B
385 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
386 * flags again, which will cause process A to resync everything. Fix that with
389 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
391 void sync_filesystems(int wait)
393 struct super_block *sb;
394 static DEFINE_MUTEX(mutex);
396 mutex_lock(&mutex); /* Could be down_interruptible */
398 list_for_each_entry(sb, &super_blocks, s_list) {
399 if (!sb->s_op->sync_fs)
401 if (sb->s_flags & MS_RDONLY)
403 sb->s_need_sync_fs = 1;
407 list_for_each_entry(sb, &super_blocks, s_list) {
408 if (!sb->s_need_sync_fs)
410 sb->s_need_sync_fs = 0;
411 if (sb->s_flags & MS_RDONLY)
412 continue; /* hm. Was remounted r/o meanwhile */
414 spin_unlock(&sb_lock);
415 down_read(&sb->s_umount);
416 if (sb->s_root && (wait || sb->s_dirt))
417 sb->s_op->sync_fs(sb, wait);
418 up_read(&sb->s_umount);
419 /* restart only when sb is no longer on the list */
421 if (__put_super_and_need_restart(sb))
424 spin_unlock(&sb_lock);
425 mutex_unlock(&mutex);
429 * get_super - get the superblock of a device
430 * @bdev: device to get the superblock for
432 * Scans the superblock list and finds the superblock of the file system
433 * mounted on the device given. %NULL is returned if no match is found.
436 struct super_block * get_super(struct block_device *bdev)
438 struct super_block *sb;
445 list_for_each_entry(sb, &super_blocks, s_list) {
446 if (sb->s_bdev == bdev) {
448 spin_unlock(&sb_lock);
449 down_read(&sb->s_umount);
452 up_read(&sb->s_umount);
453 /* restart only when sb is no longer on the list */
455 if (__put_super_and_need_restart(sb))
459 spin_unlock(&sb_lock);
463 EXPORT_SYMBOL(get_super);
465 struct super_block * user_get_super(dev_t dev)
467 struct super_block *sb;
471 list_for_each_entry(sb, &super_blocks, s_list) {
472 if (sb->s_dev == dev) {
474 spin_unlock(&sb_lock);
475 down_read(&sb->s_umount);
478 up_read(&sb->s_umount);
479 /* restart only when sb is no longer on the list */
481 if (__put_super_and_need_restart(sb))
485 spin_unlock(&sb_lock);
489 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
491 struct super_block *s;
496 s = user_get_super(new_decode_dev(dev));
499 err = vfs_statfs(s->s_root, &sbuf);
504 memset(&tmp,0,sizeof(struct ustat));
505 tmp.f_tfree = sbuf.f_bfree;
506 tmp.f_tinode = sbuf.f_ffree;
508 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
515 * @sb: superblock in question
517 * All files are marked read/only. We don't care about pending
518 * delete files so this should be used in 'force' mode only
521 static void mark_files_ro(struct super_block *sb)
526 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
527 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
528 f->f_mode &= ~FMODE_WRITE;
534 * do_remount_sb - asks filesystem to change mount options.
535 * @sb: superblock in question
536 * @flags: numeric part of options
537 * @data: the rest of options
538 * @force: whether or not to force the change
540 * Alters the mount options of a mounted file system.
542 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
546 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
548 if (flags & MS_RDONLY)
550 shrink_dcache_sb(sb);
553 /* If we are remounting RDONLY and current sb is read/write,
554 make sure there are no rw files opened */
555 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
558 else if (!fs_may_remount_ro(sb))
562 if (sb->s_op->remount_fs) {
564 retval = sb->s_op->remount_fs(sb, &flags, data);
569 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
573 static void do_emergency_remount(unsigned long foo)
575 struct super_block *sb;
578 list_for_each_entry(sb, &super_blocks, s_list) {
580 spin_unlock(&sb_lock);
581 down_read(&sb->s_umount);
582 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
584 * ->remount_fs needs lock_kernel().
586 * What lock protects sb->s_flags??
589 do_remount_sb(sb, MS_RDONLY, NULL, 1);
595 spin_unlock(&sb_lock);
596 printk("Emergency Remount complete\n");
599 void emergency_remount(void)
601 pdflush_operation(do_emergency_remount, 0);
605 * Unnamed block devices are dummy devices used by virtual
606 * filesystems which don't use real block-devices. -- jrs
609 static struct idr unnamed_dev_idr;
610 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
612 int set_anon_super(struct super_block *s, void *data)
618 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
620 spin_lock(&unnamed_dev_lock);
621 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
622 spin_unlock(&unnamed_dev_lock);
623 if (error == -EAGAIN)
624 /* We raced and lost with another CPU. */
629 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
630 spin_lock(&unnamed_dev_lock);
631 idr_remove(&unnamed_dev_idr, dev);
632 spin_unlock(&unnamed_dev_lock);
635 s->s_dev = MKDEV(0, dev & MINORMASK);
639 EXPORT_SYMBOL(set_anon_super);
641 void kill_anon_super(struct super_block *sb)
643 int slot = MINOR(sb->s_dev);
645 generic_shutdown_super(sb);
646 spin_lock(&unnamed_dev_lock);
647 idr_remove(&unnamed_dev_idr, slot);
648 spin_unlock(&unnamed_dev_lock);
651 EXPORT_SYMBOL(kill_anon_super);
653 void __init unnamed_dev_init(void)
655 idr_init(&unnamed_dev_idr);
658 void kill_litter_super(struct super_block *sb)
661 d_genocide(sb->s_root);
665 EXPORT_SYMBOL(kill_litter_super);
667 static int set_bdev_super(struct super_block *s, void *data)
670 s->s_dev = s->s_bdev->bd_dev;
674 static int test_bdev_super(struct super_block *s, void *data)
676 return (void *)s->s_bdev == data;
679 static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
683 kobject_uevent(&bdev->bd_part->kobj, action);
685 kobject_uevent(&bdev->bd_disk->kobj, action);
689 int get_sb_bdev(struct file_system_type *fs_type,
690 int flags, const char *dev_name, void *data,
691 int (*fill_super)(struct super_block *, void *, int),
692 struct vfsmount *mnt)
694 struct block_device *bdev;
695 struct super_block *s;
698 bdev = open_bdev_excl(dev_name, flags, fs_type);
700 return PTR_ERR(bdev);
703 * once the super is inserted into the list by sget, s_umount
704 * will protect the lockfs code from trying to start a snapshot
705 * while we are mounting
707 mutex_lock(&bdev->bd_mount_mutex);
708 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
709 mutex_unlock(&bdev->bd_mount_mutex);
714 if ((flags ^ s->s_flags) & MS_RDONLY) {
715 up_write(&s->s_umount);
721 close_bdev_excl(bdev);
723 char b[BDEVNAME_SIZE];
726 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
727 sb_set_blocksize(s, block_size(bdev));
728 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
730 up_write(&s->s_umount);
735 s->s_flags |= MS_ACTIVE;
736 bdev_uevent(bdev, KOBJ_MOUNT);
739 return simple_set_mnt(mnt, s);
744 close_bdev_excl(bdev);
749 EXPORT_SYMBOL(get_sb_bdev);
751 void kill_block_super(struct super_block *sb)
753 struct block_device *bdev = sb->s_bdev;
755 bdev_uevent(bdev, KOBJ_UMOUNT);
756 generic_shutdown_super(sb);
758 close_bdev_excl(bdev);
761 EXPORT_SYMBOL(kill_block_super);
763 int get_sb_nodev(struct file_system_type *fs_type,
764 int flags, void *data,
765 int (*fill_super)(struct super_block *, void *, int),
766 struct vfsmount *mnt)
769 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
776 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
778 up_write(&s->s_umount);
782 s->s_flags |= MS_ACTIVE;
783 return simple_set_mnt(mnt, s);
786 EXPORT_SYMBOL(get_sb_nodev);
788 static int compare_single(struct super_block *s, void *p)
793 int get_sb_single(struct file_system_type *fs_type,
794 int flags, void *data,
795 int (*fill_super)(struct super_block *, void *, int),
796 struct vfsmount *mnt)
798 struct super_block *s;
801 s = sget(fs_type, compare_single, set_anon_super, NULL);
806 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
808 up_write(&s->s_umount);
812 s->s_flags |= MS_ACTIVE;
814 do_remount_sb(s, flags, data, 0);
815 return simple_set_mnt(mnt, s);
818 EXPORT_SYMBOL(get_sb_single);
821 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
823 struct vfsmount *mnt;
824 struct super_block *sb;
825 char *secdata = NULL;
829 return ERR_PTR(-ENODEV);
832 mnt = alloc_vfsmnt(name);
837 secdata = alloc_secdata();
841 error = security_sb_copy_data(type, data, secdata);
843 goto out_free_secdata;
846 error = type->get_sb(type, flags, name, data, mnt);
848 goto out_free_secdata;
852 if (!capable(CAP_SYS_ADMIN) && !sb->s_bdev &&
853 (sb->s_magic != PROC_SUPER_MAGIC) &&
854 (sb->s_magic != DEVPTS_SUPER_MAGIC))
857 error = security_sb_kern_mount(sb, secdata);
861 mnt->mnt_mountpoint = mnt->mnt_root;
862 mnt->mnt_parent = mnt;
863 up_write(&mnt->mnt_sb->s_umount);
864 free_secdata(secdata);
868 up_write(&mnt->mnt_sb->s_umount);
869 deactivate_super(mnt->mnt_sb);
871 free_secdata(secdata);
875 return ERR_PTR(error);
878 EXPORT_SYMBOL_GPL(vfs_kern_mount);
881 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
883 struct file_system_type *type = get_fs_type(fstype);
884 struct vfsmount *mnt;
887 return ERR_PTR(-ENODEV);
889 mnt = ERR_PTR(-EPERM);
890 if ((type->fs_flags & FS_BINARY_MOUNTDATA) &&
891 !vx_capable(CAP_SYS_ADMIN, VXC_BINARY_MOUNT))
894 mnt = vfs_kern_mount(type, flags, name, data);
896 put_filesystem(type);
900 struct vfsmount *kern_mount(struct file_system_type *type)
902 return vfs_kern_mount(type, 0, type->name, NULL);
905 EXPORT_SYMBOL(kern_mount);