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/file.h>
41 #include <linux/async.h>
42 #include <asm/uaccess.h>
46 LIST_HEAD(super_blocks);
47 DEFINE_SPINLOCK(sb_lock);
50 * alloc_super - create new superblock
51 * @type: filesystem type superblock should belong to
53 * Allocates and initializes a new &struct super_block. alloc_super()
54 * returns a pointer new superblock or %NULL if allocation had failed.
56 static struct super_block *alloc_super(struct file_system_type *type)
58 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
59 static struct super_operations default_op;
62 if (security_sb_alloc(s)) {
67 INIT_LIST_HEAD(&s->s_dirty);
68 INIT_LIST_HEAD(&s->s_io);
69 INIT_LIST_HEAD(&s->s_more_io);
70 INIT_LIST_HEAD(&s->s_files);
71 INIT_LIST_HEAD(&s->s_instances);
72 INIT_HLIST_HEAD(&s->s_anon);
73 INIT_LIST_HEAD(&s->s_inodes);
74 INIT_LIST_HEAD(&s->s_dentry_lru);
75 INIT_LIST_HEAD(&s->s_async_list);
76 init_rwsem(&s->s_umount);
77 mutex_init(&s->s_lock);
78 lockdep_set_class(&s->s_umount, &type->s_umount_key);
80 * The locking rules for s_lock are up to the
81 * filesystem. For example ext3fs has different
82 * lock ordering than usbfs:
84 lockdep_set_class(&s->s_lock, &type->s_lock_key);
86 * sget() can have s_umount recursion.
88 * When it cannot find a suitable sb, it allocates a new
89 * one (this one), and tries again to find a suitable old
92 * In case that succeeds, it will acquire the s_umount
93 * lock of the old one. Since these are clearly distrinct
94 * locks, and this object isn't exposed yet, there's no
97 * Annotate this by putting this lock in a different
100 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
102 atomic_set(&s->s_active, 1);
103 mutex_init(&s->s_vfs_rename_mutex);
104 mutex_init(&s->s_dquot.dqio_mutex);
105 mutex_init(&s->s_dquot.dqonoff_mutex);
106 init_rwsem(&s->s_dquot.dqptr_sem);
107 init_waitqueue_head(&s->s_wait_unfrozen);
108 s->s_maxbytes = MAX_NON_LFS;
109 s->dq_op = sb_dquot_ops;
110 s->s_qcop = sb_quotactl_ops;
111 s->s_op = &default_op;
112 s->s_time_gran = 1000000000;
119 * destroy_super - frees a superblock
120 * @s: superblock to free
122 * Frees a superblock.
124 static inline void destroy_super(struct super_block *s)
132 /* Superblock refcounting */
135 * Drop a superblock's refcount. Returns non-zero if the superblock was
136 * destroyed. The caller must hold sb_lock.
138 static int __put_super(struct super_block *sb)
142 if (!--sb->s_count) {
150 * Drop a superblock's refcount.
151 * Returns non-zero if the superblock is about to be destroyed and
152 * at least is already removed from super_blocks list, so if we are
153 * making a loop through super blocks then we need to restart.
154 * The caller must hold sb_lock.
156 int __put_super_and_need_restart(struct super_block *sb)
158 /* check for race with generic_shutdown_super() */
159 if (list_empty(&sb->s_list)) {
160 /* super block is removed, need to restart... */
164 /* can't be the last, since s_list is still in use */
166 BUG_ON(sb->s_count == 0);
171 * put_super - drop a temporary reference to superblock
172 * @sb: superblock in question
174 * Drops a temporary reference, frees superblock if there's no
177 static void put_super(struct super_block *sb)
181 spin_unlock(&sb_lock);
186 * deactivate_super - drop an active reference to superblock
187 * @s: superblock to deactivate
189 * Drops an active reference to superblock, acquiring a temprory one if
190 * there is no active references left. In that case we lock superblock,
191 * tell fs driver to shut it down and drop the temporary reference we
194 void deactivate_super(struct super_block *s)
196 struct file_system_type *fs = s->s_type;
197 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
198 s->s_count -= S_BIAS-1;
199 spin_unlock(&sb_lock);
201 down_write(&s->s_umount);
208 EXPORT_SYMBOL(deactivate_super);
211 * grab_super - acquire an active reference
212 * @s: reference we are trying to make active
214 * Tries to acquire an active reference. grab_super() is used when we
215 * had just found a superblock in super_blocks or fs_type->fs_supers
216 * and want to turn it into a full-blown active reference. grab_super()
217 * is called with sb_lock held and drops it. Returns 1 in case of
218 * success, 0 if we had failed (superblock contents was already dead or
219 * dying when grab_super() had been called).
221 static int grab_super(struct super_block *s) __releases(sb_lock)
224 spin_unlock(&sb_lock);
225 down_write(&s->s_umount);
228 if (s->s_count > S_BIAS) {
229 atomic_inc(&s->s_active);
231 spin_unlock(&sb_lock);
234 spin_unlock(&sb_lock);
236 up_write(&s->s_umount);
243 * Superblock locking. We really ought to get rid of these two.
245 void lock_super(struct super_block * sb)
248 mutex_lock(&sb->s_lock);
251 void unlock_super(struct super_block * sb)
254 mutex_unlock(&sb->s_lock);
257 EXPORT_SYMBOL(lock_super);
258 EXPORT_SYMBOL(unlock_super);
261 * Write out and wait upon all dirty data associated with this
262 * superblock. Filesystem data as well as the underlying block
263 * device. Takes the superblock lock. Requires a second blkdev
264 * flush by the caller to complete the operation.
266 void __fsync_super(struct super_block *sb)
268 sync_inodes_sb(sb, 0);
271 if (sb->s_dirt && sb->s_op->write_super)
272 sb->s_op->write_super(sb);
274 if (sb->s_op->sync_fs)
275 sb->s_op->sync_fs(sb, 1);
276 sync_blockdev(sb->s_bdev);
277 sync_inodes_sb(sb, 1);
281 * Write out and wait upon all dirty data associated with this
282 * superblock. Filesystem data as well as the underlying block
283 * device. Takes the superblock lock.
285 int fsync_super(struct super_block *sb)
288 return sync_blockdev(sb->s_bdev);
292 * generic_shutdown_super - common helper for ->kill_sb()
293 * @sb: superblock to kill
295 * generic_shutdown_super() does all fs-independent work on superblock
296 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
297 * that need destruction out of superblock, call generic_shutdown_super()
298 * and release aforementioned objects. Note: dentries and inodes _are_
299 * taken care of and do not need specific handling.
301 * Upon calling this function, the filesystem may no longer alter or
302 * rearrange the set of dentries belonging to this super_block, nor may it
303 * change the attachments of dentries to inodes.
305 void generic_shutdown_super(struct super_block *sb)
307 const struct super_operations *sop = sb->s_op;
311 shrink_dcache_for_umount(sb);
314 sb->s_flags &= ~MS_ACTIVE;
317 * wait for asynchronous fs operations to finish before going further
319 async_synchronize_full_domain(&sb->s_async_list);
321 /* bad name - it should be evict_inodes() */
322 invalidate_inodes(sb);
325 if (sop->write_super && sb->s_dirt)
326 sop->write_super(sb);
330 /* Forget any remaining inodes */
331 if (invalidate_inodes(sb)) {
332 printk("VFS: Busy inodes after unmount of %s. "
333 "Self-destruct in 5 seconds. Have a nice day...\n",
341 /* should be initialized for __put_super_and_need_restart() */
342 list_del_init(&sb->s_list);
343 list_del(&sb->s_instances);
344 spin_unlock(&sb_lock);
345 up_write(&sb->s_umount);
348 EXPORT_SYMBOL(generic_shutdown_super);
351 * sget - find or create a superblock
352 * @type: filesystem type superblock should belong to
353 * @test: comparison callback
354 * @set: setup callback
355 * @data: argument to each of them
357 struct super_block *sget(struct file_system_type *type,
358 int (*test)(struct super_block *,void *),
359 int (*set)(struct super_block *,void *),
362 struct super_block *s = NULL;
363 struct super_block *old;
369 list_for_each_entry(old, &type->fs_supers, s_instances) {
370 if (!test(old, data))
372 if (!grab_super(old))
380 spin_unlock(&sb_lock);
381 s = alloc_super(type);
383 return ERR_PTR(-ENOMEM);
389 spin_unlock(&sb_lock);
394 strlcpy(s->s_id, type->name, sizeof(s->s_id));
395 list_add_tail(&s->s_list, &super_blocks);
396 list_add(&s->s_instances, &type->fs_supers);
397 spin_unlock(&sb_lock);
398 get_filesystem(type);
404 void drop_super(struct super_block *sb)
406 up_read(&sb->s_umount);
410 EXPORT_SYMBOL(drop_super);
412 static inline void write_super(struct super_block *sb)
415 if (sb->s_root && sb->s_dirt)
416 if (sb->s_op->write_super)
417 sb->s_op->write_super(sb);
422 * Note: check the dirty flag before waiting, so we don't
423 * hold up the sync while mounting a device. (The newly
424 * mounted device won't need syncing.)
426 void sync_supers(void)
428 struct super_block *sb;
432 list_for_each_entry(sb, &super_blocks, s_list) {
435 spin_unlock(&sb_lock);
436 down_read(&sb->s_umount);
438 up_read(&sb->s_umount);
440 if (__put_super_and_need_restart(sb))
444 spin_unlock(&sb_lock);
448 * Call the ->sync_fs super_op against all filesystems which are r/w and
449 * which implement it.
451 * This operation is careful to avoid the livelock which could easily happen
452 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
453 * is used only here. We set it against all filesystems and then clear it as
454 * we sync them. So redirtied filesystems are skipped.
456 * But if process A is currently running sync_filesystems and then process B
457 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
458 * flags again, which will cause process A to resync everything. Fix that with
461 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
463 void sync_filesystems(int wait)
465 struct super_block *sb;
466 static DEFINE_MUTEX(mutex);
468 mutex_lock(&mutex); /* Could be down_interruptible */
470 list_for_each_entry(sb, &super_blocks, s_list) {
471 if (!sb->s_op->sync_fs)
473 if (sb->s_flags & MS_RDONLY)
475 sb->s_need_sync_fs = 1;
479 list_for_each_entry(sb, &super_blocks, s_list) {
480 if (!sb->s_need_sync_fs)
482 sb->s_need_sync_fs = 0;
483 if (sb->s_flags & MS_RDONLY)
484 continue; /* hm. Was remounted r/o meanwhile */
486 spin_unlock(&sb_lock);
487 down_read(&sb->s_umount);
488 async_synchronize_full_domain(&sb->s_async_list);
489 if (sb->s_root && (wait || sb->s_dirt))
490 sb->s_op->sync_fs(sb, wait);
491 up_read(&sb->s_umount);
492 /* restart only when sb is no longer on the list */
494 if (__put_super_and_need_restart(sb))
497 spin_unlock(&sb_lock);
498 mutex_unlock(&mutex);
502 * get_super - get the superblock of a device
503 * @bdev: device to get the superblock for
505 * Scans the superblock list and finds the superblock of the file system
506 * mounted on the device given. %NULL is returned if no match is found.
509 struct super_block * get_super(struct block_device *bdev)
511 struct super_block *sb;
518 list_for_each_entry(sb, &super_blocks, s_list) {
519 if (sb->s_bdev == bdev) {
521 spin_unlock(&sb_lock);
522 down_read(&sb->s_umount);
525 up_read(&sb->s_umount);
526 /* restart only when sb is no longer on the list */
528 if (__put_super_and_need_restart(sb))
532 spin_unlock(&sb_lock);
536 EXPORT_SYMBOL(get_super);
538 struct super_block * user_get_super(dev_t dev)
540 struct super_block *sb;
544 list_for_each_entry(sb, &super_blocks, s_list) {
545 if (sb->s_dev == dev) {
547 spin_unlock(&sb_lock);
548 down_read(&sb->s_umount);
551 up_read(&sb->s_umount);
552 /* restart only when sb is no longer on the list */
554 if (__put_super_and_need_restart(sb))
558 spin_unlock(&sb_lock);
562 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
564 struct super_block *s;
569 s = user_get_super(new_decode_dev(dev));
572 err = vfs_statfs(s->s_root, &sbuf);
577 memset(&tmp,0,sizeof(struct ustat));
578 tmp.f_tfree = sbuf.f_bfree;
579 tmp.f_tinode = sbuf.f_ffree;
581 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
587 * mark_files_ro - mark all files read-only
588 * @sb: superblock in question
590 * All files are marked read-only. We don't care about pending
591 * delete files so this should be used in 'force' mode only.
594 static void mark_files_ro(struct super_block *sb)
600 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
601 struct vfsmount *mnt;
602 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
606 if (!(f->f_mode & FMODE_WRITE))
608 f->f_mode &= ~FMODE_WRITE;
609 if (file_check_writeable(f) != 0)
611 file_release_write(f);
612 mnt = mntget(f->f_path.mnt);
615 * This can sleep, so we can't hold
616 * the file_list_lock() spinlock.
626 * do_remount_sb - asks filesystem to change mount options.
627 * @sb: superblock in question
628 * @flags: numeric part of options
629 * @data: the rest of options
630 * @force: whether or not to force the change
632 * Alters the mount options of a mounted file system.
634 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
640 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
643 if (flags & MS_RDONLY)
645 shrink_dcache_sb(sb);
648 /* If we are remounting RDONLY and current sb is read/write,
649 make sure there are no rw files opened */
650 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
653 else if (!fs_may_remount_ro(sb))
655 retval = DQUOT_OFF(sb, 1);
656 if (retval < 0 && retval != -ENOSYS)
659 remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
661 if (sb->s_op->remount_fs) {
663 retval = sb->s_op->remount_fs(sb, &flags, data);
668 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
670 DQUOT_ON_REMOUNT(sb);
674 static void do_emergency_remount(unsigned long foo)
676 struct super_block *sb;
679 list_for_each_entry(sb, &super_blocks, s_list) {
681 spin_unlock(&sb_lock);
682 down_read(&sb->s_umount);
683 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
685 * ->remount_fs needs lock_kernel().
687 * What lock protects sb->s_flags??
690 do_remount_sb(sb, MS_RDONLY, NULL, 1);
696 spin_unlock(&sb_lock);
697 printk("Emergency Remount complete\n");
700 void emergency_remount(void)
702 pdflush_operation(do_emergency_remount, 0);
706 * Unnamed block devices are dummy devices used by virtual
707 * filesystems which don't use real block-devices. -- jrs
710 static DEFINE_IDA(unnamed_dev_ida);
711 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
713 int set_anon_super(struct super_block *s, void *data)
719 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
721 spin_lock(&unnamed_dev_lock);
722 error = ida_get_new(&unnamed_dev_ida, &dev);
723 spin_unlock(&unnamed_dev_lock);
724 if (error == -EAGAIN)
725 /* We raced and lost with another CPU. */
730 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
731 spin_lock(&unnamed_dev_lock);
732 ida_remove(&unnamed_dev_ida, dev);
733 spin_unlock(&unnamed_dev_lock);
736 s->s_dev = MKDEV(0, dev & MINORMASK);
740 EXPORT_SYMBOL(set_anon_super);
742 void kill_anon_super(struct super_block *sb)
744 int slot = MINOR(sb->s_dev);
746 generic_shutdown_super(sb);
747 spin_lock(&unnamed_dev_lock);
748 ida_remove(&unnamed_dev_ida, slot);
749 spin_unlock(&unnamed_dev_lock);
752 EXPORT_SYMBOL(kill_anon_super);
754 void kill_litter_super(struct super_block *sb)
757 d_genocide(sb->s_root);
761 EXPORT_SYMBOL(kill_litter_super);
764 static int set_bdev_super(struct super_block *s, void *data)
767 s->s_dev = s->s_bdev->bd_dev;
771 static int test_bdev_super(struct super_block *s, void *data)
773 return (void *)s->s_bdev == data;
776 int get_sb_bdev(struct file_system_type *fs_type,
777 int flags, const char *dev_name, void *data,
778 int (*fill_super)(struct super_block *, void *, int),
779 struct vfsmount *mnt)
781 struct block_device *bdev;
782 struct super_block *s;
783 fmode_t mode = FMODE_READ;
786 if (!(flags & MS_RDONLY))
789 bdev = open_bdev_exclusive(dev_name, mode, fs_type);
791 return PTR_ERR(bdev);
794 * once the super is inserted into the list by sget, s_umount
795 * will protect the lockfs code from trying to start a snapshot
796 * while we are mounting
798 down(&bdev->bd_mount_sem);
799 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
800 up(&bdev->bd_mount_sem);
805 if ((flags ^ s->s_flags) & MS_RDONLY) {
806 up_write(&s->s_umount);
812 close_bdev_exclusive(bdev, mode);
814 char b[BDEVNAME_SIZE];
818 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
819 sb_set_blocksize(s, block_size(bdev));
820 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
822 up_write(&s->s_umount);
827 s->s_flags |= MS_ACTIVE;
831 return simple_set_mnt(mnt, s);
836 close_bdev_exclusive(bdev, mode);
841 EXPORT_SYMBOL(get_sb_bdev);
843 void kill_block_super(struct super_block *sb)
845 struct block_device *bdev = sb->s_bdev;
846 fmode_t mode = sb->s_mode;
849 generic_shutdown_super(sb);
851 close_bdev_exclusive(bdev, mode);
854 EXPORT_SYMBOL(kill_block_super);
857 int get_sb_nodev(struct file_system_type *fs_type,
858 int flags, void *data,
859 int (*fill_super)(struct super_block *, void *, int),
860 struct vfsmount *mnt)
863 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
870 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
872 up_write(&s->s_umount);
876 s->s_flags |= MS_ACTIVE;
877 return simple_set_mnt(mnt, s);
880 EXPORT_SYMBOL(get_sb_nodev);
882 static int compare_single(struct super_block *s, void *p)
887 int get_sb_single(struct file_system_type *fs_type,
888 int flags, void *data,
889 int (*fill_super)(struct super_block *, void *, int),
890 struct vfsmount *mnt)
892 struct super_block *s;
895 s = sget(fs_type, compare_single, set_anon_super, NULL);
900 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
902 up_write(&s->s_umount);
906 s->s_flags |= MS_ACTIVE;
908 do_remount_sb(s, flags, data, 0);
909 return simple_set_mnt(mnt, s);
912 EXPORT_SYMBOL(get_sb_single);
915 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
917 struct vfsmount *mnt;
918 char *secdata = NULL;
922 return ERR_PTR(-ENODEV);
925 mnt = alloc_vfsmnt(name);
929 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
930 secdata = alloc_secdata();
934 error = security_sb_copy_data(data, secdata);
936 goto out_free_secdata;
939 error = type->get_sb(type, flags, name, data, mnt);
941 goto out_free_secdata;
942 BUG_ON(!mnt->mnt_sb);
944 error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
948 mnt->mnt_mountpoint = mnt->mnt_root;
949 mnt->mnt_parent = mnt;
950 up_write(&mnt->mnt_sb->s_umount);
951 free_secdata(secdata);
955 up_write(&mnt->mnt_sb->s_umount);
956 deactivate_super(mnt->mnt_sb);
958 free_secdata(secdata);
962 return ERR_PTR(error);
965 EXPORT_SYMBOL_GPL(vfs_kern_mount);
967 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
970 const char *subtype = strchr(fstype, '.');
979 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
981 if (!mnt->mnt_sb->s_subtype)
991 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
993 struct file_system_type *type = get_fs_type(fstype);
994 struct vfsmount *mnt;
996 return ERR_PTR(-ENODEV);
997 mnt = vfs_kern_mount(type, flags, name, data);
998 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
999 !mnt->mnt_sb->s_subtype)
1000 mnt = fs_set_subtype(mnt, fstype);
1001 put_filesystem(type);
1004 EXPORT_SYMBOL_GPL(do_kern_mount);
1006 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
1008 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
1011 EXPORT_SYMBOL_GPL(kern_mount_data);