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 <asm/uaccess.h>
44 LIST_HEAD(super_blocks);
45 DEFINE_SPINLOCK(sb_lock);
48 * alloc_super - create new superblock
49 * @type: filesystem type superblock should belong to
51 * Allocates and initializes a new &struct super_block. alloc_super()
52 * returns a pointer new superblock or %NULL if allocation had failed.
54 static struct super_block *alloc_super(struct file_system_type *type)
56 struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
57 static struct super_operations default_op;
60 if (security_sb_alloc(s)) {
65 INIT_LIST_HEAD(&s->s_dirty);
66 INIT_LIST_HEAD(&s->s_io);
67 INIT_LIST_HEAD(&s->s_more_io);
68 INIT_LIST_HEAD(&s->s_files);
69 INIT_LIST_HEAD(&s->s_instances);
70 INIT_HLIST_HEAD(&s->s_anon);
71 INIT_LIST_HEAD(&s->s_inodes);
72 init_rwsem(&s->s_umount);
73 mutex_init(&s->s_lock);
74 lockdep_set_class(&s->s_umount, &type->s_umount_key);
76 * The locking rules for s_lock are up to the
77 * filesystem. For example ext3fs has different
78 * lock ordering than usbfs:
80 lockdep_set_class(&s->s_lock, &type->s_lock_key);
81 down_write(&s->s_umount);
83 atomic_set(&s->s_active, 1);
84 mutex_init(&s->s_vfs_rename_mutex);
85 mutex_init(&s->s_dquot.dqio_mutex);
86 mutex_init(&s->s_dquot.dqonoff_mutex);
87 init_rwsem(&s->s_dquot.dqptr_sem);
88 init_waitqueue_head(&s->s_wait_unfrozen);
89 s->s_maxbytes = MAX_NON_LFS;
90 s->dq_op = sb_dquot_ops;
91 s->s_qcop = sb_quotactl_ops;
92 s->s_op = &default_op;
93 s->s_time_gran = 1000000000;
100 * destroy_super - frees a superblock
101 * @s: superblock to free
103 * Frees a superblock.
105 static inline void destroy_super(struct super_block *s)
113 /* Superblock refcounting */
116 * Drop a superblock's refcount. Returns non-zero if the superblock was
117 * destroyed. The caller must hold sb_lock.
119 int __put_super(struct super_block *sb)
123 if (!--sb->s_count) {
131 * Drop a superblock's refcount.
132 * Returns non-zero if the superblock is about to be destroyed and
133 * at least is already removed from super_blocks list, so if we are
134 * making a loop through super blocks then we need to restart.
135 * The caller must hold sb_lock.
137 int __put_super_and_need_restart(struct super_block *sb)
139 /* check for race with generic_shutdown_super() */
140 if (list_empty(&sb->s_list)) {
141 /* super block is removed, need to restart... */
145 /* can't be the last, since s_list is still in use */
147 BUG_ON(sb->s_count == 0);
152 * put_super - drop a temporary reference to superblock
153 * @sb: superblock in question
155 * Drops a temporary reference, frees superblock if there's no
158 static void put_super(struct super_block *sb)
162 spin_unlock(&sb_lock);
167 * deactivate_super - drop an active reference to superblock
168 * @s: superblock to deactivate
170 * Drops an active reference to superblock, acquiring a temprory one if
171 * there is no active references left. In that case we lock superblock,
172 * tell fs driver to shut it down and drop the temporary reference we
175 void deactivate_super(struct super_block *s)
177 struct file_system_type *fs = s->s_type;
178 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
179 s->s_count -= S_BIAS-1;
180 spin_unlock(&sb_lock);
182 down_write(&s->s_umount);
189 EXPORT_SYMBOL(deactivate_super);
192 * grab_super - acquire an active reference
193 * @s: reference we are trying to make active
195 * Tries to acquire an active reference. grab_super() is used when we
196 * had just found a superblock in super_blocks or fs_type->fs_supers
197 * and want to turn it into a full-blown active reference. grab_super()
198 * is called with sb_lock held and drops it. Returns 1 in case of
199 * success, 0 if we had failed (superblock contents was already dead or
200 * dying when grab_super() had been called).
202 static int grab_super(struct super_block *s) __releases(sb_lock)
205 spin_unlock(&sb_lock);
206 down_write(&s->s_umount);
209 if (s->s_count > S_BIAS) {
210 atomic_inc(&s->s_active);
212 spin_unlock(&sb_lock);
215 spin_unlock(&sb_lock);
217 up_write(&s->s_umount);
224 * Superblock locking. We really ought to get rid of these two.
226 void lock_super(struct super_block * sb)
229 mutex_lock(&sb->s_lock);
232 void unlock_super(struct super_block * sb)
235 mutex_unlock(&sb->s_lock);
238 EXPORT_SYMBOL(lock_super);
239 EXPORT_SYMBOL(unlock_super);
242 * Write out and wait upon all dirty data associated with this
243 * superblock. Filesystem data as well as the underlying block
244 * device. Takes the superblock lock. Requires a second blkdev
245 * flush by the caller to complete the operation.
247 void __fsync_super(struct super_block *sb)
249 sync_inodes_sb(sb, 0);
252 if (sb->s_dirt && sb->s_op->write_super)
253 sb->s_op->write_super(sb);
255 if (sb->s_op->sync_fs)
256 sb->s_op->sync_fs(sb, 1);
257 sync_blockdev(sb->s_bdev);
258 sync_inodes_sb(sb, 1);
262 * Write out and wait upon all dirty data associated with this
263 * superblock. Filesystem data as well as the underlying block
264 * device. Takes the superblock lock.
266 int fsync_super(struct super_block *sb)
269 return sync_blockdev(sb->s_bdev);
273 * generic_shutdown_super - common helper for ->kill_sb()
274 * @sb: superblock to kill
276 * generic_shutdown_super() does all fs-independent work on superblock
277 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
278 * that need destruction out of superblock, call generic_shutdown_super()
279 * and release aforementioned objects. Note: dentries and inodes _are_
280 * taken care of and do not need specific handling.
282 * Upon calling this function, the filesystem may no longer alter or
283 * rearrange the set of dentries belonging to this super_block, nor may it
284 * change the attachments of dentries to inodes.
286 void generic_shutdown_super(struct super_block *sb)
288 const struct super_operations *sop = sb->s_op;
291 shrink_dcache_for_umount(sb);
294 sb->s_flags &= ~MS_ACTIVE;
295 /* bad name - it should be evict_inodes() */
296 invalidate_inodes(sb);
299 if (sop->write_super && sb->s_dirt)
300 sop->write_super(sb);
304 /* Forget any remaining inodes */
305 if (invalidate_inodes(sb)) {
306 printk("VFS: Busy inodes after unmount of %s. "
307 "Self-destruct in 5 seconds. Have a nice day...\n",
315 /* should be initialized for __put_super_and_need_restart() */
316 list_del_init(&sb->s_list);
317 list_del(&sb->s_instances);
318 spin_unlock(&sb_lock);
319 up_write(&sb->s_umount);
322 EXPORT_SYMBOL(generic_shutdown_super);
325 * sget - find or create a superblock
326 * @type: filesystem type superblock should belong to
327 * @test: comparison callback
328 * @set: setup callback
329 * @data: argument to each of them
331 struct super_block *sget(struct file_system_type *type,
332 int (*test)(struct super_block *,void *),
333 int (*set)(struct super_block *,void *),
336 struct super_block *s = NULL;
337 struct super_block *old;
343 list_for_each_entry(old, &type->fs_supers, s_instances) {
344 if (!test(old, data))
346 if (!grab_super(old))
354 spin_unlock(&sb_lock);
355 s = alloc_super(type);
357 return ERR_PTR(-ENOMEM);
363 spin_unlock(&sb_lock);
368 strlcpy(s->s_id, type->name, sizeof(s->s_id));
369 list_add_tail(&s->s_list, &super_blocks);
370 list_add(&s->s_instances, &type->fs_supers);
371 spin_unlock(&sb_lock);
372 get_filesystem(type);
378 void drop_super(struct super_block *sb)
380 up_read(&sb->s_umount);
384 EXPORT_SYMBOL(drop_super);
386 static inline void write_super(struct super_block *sb)
389 if (sb->s_root && sb->s_dirt)
390 if (sb->s_op->write_super)
391 sb->s_op->write_super(sb);
396 * Note: check the dirty flag before waiting, so we don't
397 * hold up the sync while mounting a device. (The newly
398 * mounted device won't need syncing.)
400 void sync_supers(void)
402 struct super_block *sb;
406 list_for_each_entry(sb, &super_blocks, s_list) {
409 spin_unlock(&sb_lock);
410 down_read(&sb->s_umount);
412 up_read(&sb->s_umount);
414 if (__put_super_and_need_restart(sb))
418 spin_unlock(&sb_lock);
422 * Call the ->sync_fs super_op against all filesystems which are r/w and
423 * which implement it.
425 * This operation is careful to avoid the livelock which could easily happen
426 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
427 * is used only here. We set it against all filesystems and then clear it as
428 * we sync them. So redirtied filesystems are skipped.
430 * But if process A is currently running sync_filesystems and then process B
431 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
432 * flags again, which will cause process A to resync everything. Fix that with
435 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
437 void sync_filesystems(int wait)
439 struct super_block *sb;
440 static DEFINE_MUTEX(mutex);
442 mutex_lock(&mutex); /* Could be down_interruptible */
444 list_for_each_entry(sb, &super_blocks, s_list) {
445 if (!sb->s_op->sync_fs)
447 if (sb->s_flags & MS_RDONLY)
449 sb->s_need_sync_fs = 1;
453 list_for_each_entry(sb, &super_blocks, s_list) {
454 if (!sb->s_need_sync_fs)
456 sb->s_need_sync_fs = 0;
457 if (sb->s_flags & MS_RDONLY)
458 continue; /* hm. Was remounted r/o meanwhile */
460 spin_unlock(&sb_lock);
461 down_read(&sb->s_umount);
462 if (sb->s_root && (wait || sb->s_dirt))
463 sb->s_op->sync_fs(sb, wait);
464 up_read(&sb->s_umount);
465 /* restart only when sb is no longer on the list */
467 if (__put_super_and_need_restart(sb))
470 spin_unlock(&sb_lock);
471 mutex_unlock(&mutex);
475 * get_super - get the superblock of a device
476 * @bdev: device to get the superblock for
478 * Scans the superblock list and finds the superblock of the file system
479 * mounted on the device given. %NULL is returned if no match is found.
482 struct super_block * get_super(struct block_device *bdev)
484 struct super_block *sb;
491 list_for_each_entry(sb, &super_blocks, s_list) {
492 if (sb->s_bdev == bdev) {
494 spin_unlock(&sb_lock);
495 down_read(&sb->s_umount);
498 up_read(&sb->s_umount);
499 /* restart only when sb is no longer on the list */
501 if (__put_super_and_need_restart(sb))
505 spin_unlock(&sb_lock);
509 EXPORT_SYMBOL(get_super);
511 struct super_block * user_get_super(dev_t dev)
513 struct super_block *sb;
517 list_for_each_entry(sb, &super_blocks, s_list) {
518 if (sb->s_dev == dev) {
520 spin_unlock(&sb_lock);
521 down_read(&sb->s_umount);
524 up_read(&sb->s_umount);
525 /* restart only when sb is no longer on the list */
527 if (__put_super_and_need_restart(sb))
531 spin_unlock(&sb_lock);
535 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
537 struct super_block *s;
542 s = user_get_super(new_decode_dev(dev));
545 err = vfs_statfs(s->s_root, &sbuf);
550 memset(&tmp,0,sizeof(struct ustat));
551 tmp.f_tfree = sbuf.f_bfree;
552 tmp.f_tinode = sbuf.f_ffree;
554 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
560 * mark_files_ro - mark all files read-only
561 * @sb: superblock in question
563 * All files are marked read-only. We don't care about pending
564 * delete files so this should be used in 'force' mode only.
567 static void mark_files_ro(struct super_block *sb)
573 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
574 struct vfsmount *mnt;
575 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
579 if (!(f->f_mode & FMODE_WRITE))
581 f->f_mode &= ~FMODE_WRITE;
582 if (file_check_writeable(f) != 0)
584 file_release_write(f);
585 mnt = mntget(f->f_path.mnt);
588 * This can sleep, so we can't hold
589 * the file_list_lock() spinlock.
599 * do_remount_sb - asks filesystem to change mount options.
600 * @sb: superblock in question
601 * @flags: numeric part of options
602 * @data: the rest of options
603 * @force: whether or not to force the change
605 * Alters the mount options of a mounted file system.
607 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
612 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
615 if (flags & MS_RDONLY)
617 shrink_dcache_sb(sb);
620 /* If we are remounting RDONLY and current sb is read/write,
621 make sure there are no rw files opened */
622 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
625 else if (!fs_may_remount_ro(sb))
630 if (sb->s_op->remount_fs) {
632 retval = sb->s_op->remount_fs(sb, &flags, data);
637 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
641 static void do_emergency_remount(unsigned long foo)
643 struct super_block *sb;
646 list_for_each_entry(sb, &super_blocks, s_list) {
648 spin_unlock(&sb_lock);
649 down_read(&sb->s_umount);
650 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
652 * ->remount_fs needs lock_kernel().
654 * What lock protects sb->s_flags??
657 do_remount_sb(sb, MS_RDONLY, NULL, 1);
663 spin_unlock(&sb_lock);
664 printk("Emergency Remount complete\n");
667 void emergency_remount(void)
669 pdflush_operation(do_emergency_remount, 0);
673 * Unnamed block devices are dummy devices used by virtual
674 * filesystems which don't use real block-devices. -- jrs
677 static struct idr unnamed_dev_idr;
678 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
680 int set_anon_super(struct super_block *s, void *data)
686 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
688 spin_lock(&unnamed_dev_lock);
689 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
690 spin_unlock(&unnamed_dev_lock);
691 if (error == -EAGAIN)
692 /* We raced and lost with another CPU. */
697 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
698 spin_lock(&unnamed_dev_lock);
699 idr_remove(&unnamed_dev_idr, dev);
700 spin_unlock(&unnamed_dev_lock);
703 s->s_dev = MKDEV(0, dev & MINORMASK);
707 EXPORT_SYMBOL(set_anon_super);
709 void kill_anon_super(struct super_block *sb)
711 int slot = MINOR(sb->s_dev);
713 generic_shutdown_super(sb);
714 spin_lock(&unnamed_dev_lock);
715 idr_remove(&unnamed_dev_idr, slot);
716 spin_unlock(&unnamed_dev_lock);
719 EXPORT_SYMBOL(kill_anon_super);
721 void __init unnamed_dev_init(void)
723 idr_init(&unnamed_dev_idr);
726 void kill_litter_super(struct super_block *sb)
729 d_genocide(sb->s_root);
733 EXPORT_SYMBOL(kill_litter_super);
736 static int set_bdev_super(struct super_block *s, void *data)
739 s->s_dev = s->s_bdev->bd_dev;
743 static int test_bdev_super(struct super_block *s, void *data)
745 return (void *)s->s_bdev == data;
748 int get_sb_bdev(struct file_system_type *fs_type,
749 int flags, const char *dev_name, void *data,
750 int (*fill_super)(struct super_block *, void *, int),
751 struct vfsmount *mnt)
753 struct block_device *bdev;
754 struct super_block *s;
757 bdev = open_bdev_excl(dev_name, flags, fs_type);
759 return PTR_ERR(bdev);
762 * once the super is inserted into the list by sget, s_umount
763 * will protect the lockfs code from trying to start a snapshot
764 * while we are mounting
766 down(&bdev->bd_mount_sem);
767 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
768 up(&bdev->bd_mount_sem);
773 if ((flags ^ s->s_flags) & MS_RDONLY) {
774 up_write(&s->s_umount);
780 close_bdev_excl(bdev);
782 char b[BDEVNAME_SIZE];
785 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
786 sb_set_blocksize(s, block_size(bdev));
787 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
789 up_write(&s->s_umount);
794 s->s_flags |= MS_ACTIVE;
797 return simple_set_mnt(mnt, s);
802 close_bdev_excl(bdev);
807 EXPORT_SYMBOL(get_sb_bdev);
809 void kill_block_super(struct super_block *sb)
811 struct block_device *bdev = sb->s_bdev;
813 generic_shutdown_super(sb);
815 close_bdev_excl(bdev);
818 EXPORT_SYMBOL(kill_block_super);
821 int get_sb_nodev(struct file_system_type *fs_type,
822 int flags, void *data,
823 int (*fill_super)(struct super_block *, void *, int),
824 struct vfsmount *mnt)
827 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
834 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
836 up_write(&s->s_umount);
840 s->s_flags |= MS_ACTIVE;
841 return simple_set_mnt(mnt, s);
844 EXPORT_SYMBOL(get_sb_nodev);
846 static int compare_single(struct super_block *s, void *p)
851 int get_sb_single(struct file_system_type *fs_type,
852 int flags, void *data,
853 int (*fill_super)(struct super_block *, void *, int),
854 struct vfsmount *mnt)
856 struct super_block *s;
859 s = sget(fs_type, compare_single, set_anon_super, NULL);
864 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
866 up_write(&s->s_umount);
870 s->s_flags |= MS_ACTIVE;
872 do_remount_sb(s, flags, data, 0);
873 return simple_set_mnt(mnt, s);
876 EXPORT_SYMBOL(get_sb_single);
879 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
881 struct vfsmount *mnt;
882 char *secdata = NULL;
886 return ERR_PTR(-ENODEV);
889 mnt = alloc_vfsmnt(name);
893 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
894 secdata = alloc_secdata();
898 error = security_sb_copy_data(data, secdata);
900 goto out_free_secdata;
903 error = type->get_sb(type, flags, name, data, mnt);
905 goto out_free_secdata;
906 BUG_ON(!mnt->mnt_sb);
908 error = security_sb_kern_mount(mnt->mnt_sb, secdata);
912 mnt->mnt_mountpoint = mnt->mnt_root;
913 mnt->mnt_parent = mnt;
914 up_write(&mnt->mnt_sb->s_umount);
915 free_secdata(secdata);
919 up_write(&mnt->mnt_sb->s_umount);
920 deactivate_super(mnt->mnt_sb);
922 free_secdata(secdata);
926 return ERR_PTR(error);
929 EXPORT_SYMBOL_GPL(vfs_kern_mount);
931 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
934 const char *subtype = strchr(fstype, '.');
943 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
945 if (!mnt->mnt_sb->s_subtype)
955 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
957 struct file_system_type *type = get_fs_type(fstype);
958 struct vfsmount *mnt;
960 return ERR_PTR(-ENODEV);
961 mnt = vfs_kern_mount(type, flags, name, data);
962 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
963 !mnt->mnt_sb->s_subtype)
964 mnt = fs_set_subtype(mnt, fstype);
965 put_filesystem(type);
968 EXPORT_SYMBOL_GPL(do_kern_mount);
970 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
972 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
975 EXPORT_SYMBOL_GPL(kern_mount_data);