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/config.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/smp_lock.h>
28 #include <linux/acct.h>
29 #include <linux/blkdev.h>
30 #include <linux/quotaops.h>
31 #include <linux/namei.h>
32 #include <linux/buffer_head.h> /* for fsync_super() */
33 #include <linux/mount.h>
34 #include <linux/security.h>
35 #include <linux/syscalls.h>
36 #include <linux/vfs.h>
37 #include <linux/writeback.h> /* for the emergency remount stuff */
38 #include <linux/idr.h>
39 #include <linux/kobject.h>
40 #include <linux/mutex.h>
41 #include <linux/devpts_fs.h>
42 #include <linux/proc_fs.h>
43 #include <asm/uaccess.h>
44 #include <linux/vs_base.h>
47 void get_filesystem(struct file_system_type *fs);
48 void put_filesystem(struct file_system_type *fs);
49 struct file_system_type *get_fs_type(const char *name);
51 LIST_HEAD(super_blocks);
52 DEFINE_SPINLOCK(sb_lock);
55 * alloc_super - create new superblock
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(void)
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 down_write(&s->s_umount);
81 atomic_set(&s->s_active, 1);
82 mutex_init(&s->s_vfs_rename_mutex);
83 mutex_init(&s->s_dquot.dqio_mutex);
84 mutex_init(&s->s_dquot.dqonoff_mutex);
85 init_rwsem(&s->s_dquot.dqptr_sem);
86 init_waitqueue_head(&s->s_wait_unfrozen);
87 s->s_maxbytes = MAX_NON_LFS;
88 s->dq_op = sb_dquot_ops;
89 s->s_qcop = sb_quotactl_ops;
90 s->s_op = &default_op;
91 s->s_time_gran = 1000000000;
98 * destroy_super - frees a superblock
99 * @s: superblock to free
101 * Frees a superblock.
103 static inline void destroy_super(struct super_block *s)
109 /* Superblock refcounting */
112 * Drop a superblock's refcount. Returns non-zero if the superblock was
113 * destroyed. The caller must hold sb_lock.
115 int __put_super(struct super_block *sb)
119 if (!--sb->s_count) {
127 * Drop a superblock's refcount.
128 * Returns non-zero if the superblock is about to be destroyed and
129 * at least is already removed from super_blocks list, so if we are
130 * making a loop through super blocks then we need to restart.
131 * The caller must hold sb_lock.
133 int __put_super_and_need_restart(struct super_block *sb)
135 /* check for race with generic_shutdown_super() */
136 if (list_empty(&sb->s_list)) {
137 /* super block is removed, need to restart... */
141 /* can't be the last, since s_list is still in use */
143 BUG_ON(sb->s_count == 0);
148 * put_super - drop a temporary reference to superblock
149 * @sb: superblock in question
151 * Drops a temporary reference, frees superblock if there's no
154 static void put_super(struct super_block *sb)
158 spin_unlock(&sb_lock);
163 * deactivate_super - drop an active reference to superblock
164 * @s: superblock to deactivate
166 * Drops an active reference to superblock, acquiring a temprory one if
167 * there is no active references left. In that case we lock superblock,
168 * tell fs driver to shut it down and drop the temporary reference we
171 void deactivate_super(struct super_block *s)
173 struct file_system_type *fs = s->s_type;
174 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
175 s->s_count -= S_BIAS-1;
176 spin_unlock(&sb_lock);
178 down_write(&s->s_umount);
185 EXPORT_SYMBOL(deactivate_super);
188 * grab_super - acquire an active reference
189 * @s: reference we are trying to make active
191 * Tries to acquire an active reference. grab_super() is used when we
192 * had just found a superblock in super_blocks or fs_type->fs_supers
193 * and want to turn it into a full-blown active reference. grab_super()
194 * is called with sb_lock held and drops it. Returns 1 in case of
195 * success, 0 if we had failed (superblock contents was already dead or
196 * dying when grab_super() had been called).
198 static int grab_super(struct super_block *s)
201 spin_unlock(&sb_lock);
202 down_write(&s->s_umount);
205 if (s->s_count > S_BIAS) {
206 atomic_inc(&s->s_active);
208 spin_unlock(&sb_lock);
211 spin_unlock(&sb_lock);
213 up_write(&s->s_umount);
220 * generic_shutdown_super - common helper for ->kill_sb()
221 * @sb: superblock to kill
223 * generic_shutdown_super() does all fs-independent work on superblock
224 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
225 * that need destruction out of superblock, call generic_shutdown_super()
226 * and release aforementioned objects. Note: dentries and inodes _are_
227 * taken care of and do not need specific handling.
229 void generic_shutdown_super(struct super_block *sb)
231 struct dentry *root = sb->s_root;
232 struct super_operations *sop = sb->s_op;
236 shrink_dcache_parent(root);
237 shrink_dcache_anon(&sb->s_anon);
241 sb->s_flags &= ~MS_ACTIVE;
242 /* bad name - it should be evict_inodes() */
243 invalidate_inodes(sb);
246 if (sop->write_super && sb->s_dirt)
247 sop->write_super(sb);
251 /* Forget any remaining inodes */
252 if (invalidate_inodes(sb)) {
253 printk("VFS: Busy inodes after unmount of %s. "
254 "Self-destruct in 5 seconds. Have a nice day...\n",
262 /* should be initialized for __put_super_and_need_restart() */
263 list_del_init(&sb->s_list);
264 list_del(&sb->s_instances);
265 spin_unlock(&sb_lock);
266 up_write(&sb->s_umount);
269 EXPORT_SYMBOL(generic_shutdown_super);
272 * sget - find or create a superblock
273 * @type: filesystem type superblock should belong to
274 * @test: comparison callback
275 * @set: setup callback
276 * @data: argument to each of them
278 struct super_block *sget(struct file_system_type *type,
279 int (*test)(struct super_block *,void *),
280 int (*set)(struct super_block *,void *),
283 struct super_block *s = NULL;
289 if (test) list_for_each(p, &type->fs_supers) {
290 struct super_block *old;
291 old = list_entry(p, struct super_block, s_instances);
292 if (!test(old, data))
294 if (!grab_super(old))
301 spin_unlock(&sb_lock);
304 return ERR_PTR(-ENOMEM);
310 spin_unlock(&sb_lock);
315 strlcpy(s->s_id, type->name, sizeof(s->s_id));
316 list_add_tail(&s->s_list, &super_blocks);
317 list_add(&s->s_instances, &type->fs_supers);
318 spin_unlock(&sb_lock);
319 get_filesystem(type);
325 void drop_super(struct super_block *sb)
327 up_read(&sb->s_umount);
331 EXPORT_SYMBOL(drop_super);
333 static inline void write_super(struct super_block *sb)
336 if (sb->s_root && sb->s_dirt)
337 if (sb->s_op->write_super)
338 sb->s_op->write_super(sb);
343 * Note: check the dirty flag before waiting, so we don't
344 * hold up the sync while mounting a device. (The newly
345 * mounted device won't need syncing.)
347 void sync_supers(void)
349 struct super_block *sb;
353 list_for_each_entry(sb, &super_blocks, s_list) {
356 spin_unlock(&sb_lock);
357 down_read(&sb->s_umount);
359 up_read(&sb->s_umount);
361 if (__put_super_and_need_restart(sb))
365 spin_unlock(&sb_lock);
369 * Call the ->sync_fs super_op against all filesytems which are r/w and
370 * which implement it.
372 * This operation is careful to avoid the livelock which could easily happen
373 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
374 * is used only here. We set it against all filesystems and then clear it as
375 * we sync them. So redirtied filesystems are skipped.
377 * But if process A is currently running sync_filesytems and then process B
378 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
379 * flags again, which will cause process A to resync everything. Fix that with
382 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
384 void sync_filesystems(int wait)
386 struct super_block *sb;
387 static DEFINE_MUTEX(mutex);
389 mutex_lock(&mutex); /* Could be down_interruptible */
391 list_for_each_entry(sb, &super_blocks, s_list) {
392 if (!sb->s_op->sync_fs)
394 if (sb->s_flags & MS_RDONLY)
396 sb->s_need_sync_fs = 1;
400 list_for_each_entry(sb, &super_blocks, s_list) {
401 if (!sb->s_need_sync_fs)
403 sb->s_need_sync_fs = 0;
404 if (sb->s_flags & MS_RDONLY)
405 continue; /* hm. Was remounted r/o meanwhile */
407 spin_unlock(&sb_lock);
408 down_read(&sb->s_umount);
409 if (sb->s_root && (wait || sb->s_dirt))
410 sb->s_op->sync_fs(sb, wait);
411 up_read(&sb->s_umount);
412 /* restart only when sb is no longer on the list */
414 if (__put_super_and_need_restart(sb))
417 spin_unlock(&sb_lock);
418 mutex_unlock(&mutex);
422 * get_super - get the superblock of a device
423 * @bdev: device to get the superblock for
425 * Scans the superblock list and finds the superblock of the file system
426 * mounted on the device given. %NULL is returned if no match is found.
429 struct super_block * get_super(struct block_device *bdev)
431 struct super_block *sb;
438 list_for_each_entry(sb, &super_blocks, s_list) {
439 if (sb->s_bdev == bdev) {
441 spin_unlock(&sb_lock);
442 down_read(&sb->s_umount);
445 up_read(&sb->s_umount);
446 /* restart only when sb is no longer on the list */
448 if (__put_super_and_need_restart(sb))
452 spin_unlock(&sb_lock);
456 EXPORT_SYMBOL(get_super);
458 struct super_block * user_get_super(dev_t dev)
460 struct super_block *sb;
464 list_for_each_entry(sb, &super_blocks, s_list) {
465 if (sb->s_dev == dev) {
467 spin_unlock(&sb_lock);
468 down_read(&sb->s_umount);
471 up_read(&sb->s_umount);
472 /* restart only when sb is no longer on the list */
474 if (__put_super_and_need_restart(sb))
478 spin_unlock(&sb_lock);
482 asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
484 struct super_block *s;
489 s = user_get_super(new_decode_dev(dev));
492 err = vfs_statfs(s, &sbuf);
497 memset(&tmp,0,sizeof(struct ustat));
498 tmp.f_tfree = sbuf.f_bfree;
499 tmp.f_tinode = sbuf.f_ffree;
501 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
508 * @sb: superblock in question
510 * All files are marked read/only. We don't care about pending
511 * delete files so this should be used in 'force' mode only
514 static void mark_files_ro(struct super_block *sb)
519 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
520 if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
521 f->f_mode &= ~FMODE_WRITE;
527 * do_remount_sb - asks filesystem to change mount options.
528 * @sb: superblock in question
529 * @flags: numeric part of options
530 * @data: the rest of options
531 * @force: whether or not to force the change
533 * Alters the mount options of a mounted file system.
535 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
539 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
541 if (flags & MS_RDONLY)
543 shrink_dcache_sb(sb);
546 /* If we are remounting RDONLY and current sb is read/write,
547 make sure there are no rw files opened */
548 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
551 else if (!fs_may_remount_ro(sb))
555 if (sb->s_op->remount_fs) {
557 retval = sb->s_op->remount_fs(sb, &flags, data);
562 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
566 static void do_emergency_remount(unsigned long foo)
568 struct super_block *sb;
571 list_for_each_entry(sb, &super_blocks, s_list) {
573 spin_unlock(&sb_lock);
574 down_read(&sb->s_umount);
575 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
577 * ->remount_fs needs lock_kernel().
579 * What lock protects sb->s_flags??
582 do_remount_sb(sb, MS_RDONLY, NULL, 1);
588 spin_unlock(&sb_lock);
589 printk("Emergency Remount complete\n");
592 void emergency_remount(void)
594 pdflush_operation(do_emergency_remount, 0);
598 * Unnamed block devices are dummy devices used by virtual
599 * filesystems which don't use real block-devices. -- jrs
602 static struct idr unnamed_dev_idr;
603 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
605 int set_anon_super(struct super_block *s, void *data)
611 if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
613 spin_lock(&unnamed_dev_lock);
614 error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
615 spin_unlock(&unnamed_dev_lock);
616 if (error == -EAGAIN)
617 /* We raced and lost with another CPU. */
622 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
623 spin_lock(&unnamed_dev_lock);
624 idr_remove(&unnamed_dev_idr, dev);
625 spin_unlock(&unnamed_dev_lock);
628 s->s_dev = MKDEV(0, dev & MINORMASK);
632 EXPORT_SYMBOL(set_anon_super);
634 void kill_anon_super(struct super_block *sb)
636 int slot = MINOR(sb->s_dev);
638 generic_shutdown_super(sb);
639 spin_lock(&unnamed_dev_lock);
640 idr_remove(&unnamed_dev_idr, slot);
641 spin_unlock(&unnamed_dev_lock);
644 EXPORT_SYMBOL(kill_anon_super);
646 void __init unnamed_dev_init(void)
648 idr_init(&unnamed_dev_idr);
651 void kill_litter_super(struct super_block *sb)
654 d_genocide(sb->s_root);
658 EXPORT_SYMBOL(kill_litter_super);
660 static int set_bdev_super(struct super_block *s, void *data)
663 s->s_dev = s->s_bdev->bd_dev;
667 static int test_bdev_super(struct super_block *s, void *data)
669 return (void *)s->s_bdev == data;
672 static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
676 kobject_uevent(&bdev->bd_part->kobj, action);
678 kobject_uevent(&bdev->bd_disk->kobj, action);
682 struct super_block *get_sb_bdev(struct file_system_type *fs_type,
683 int flags, const char *dev_name, void *data,
684 int (*fill_super)(struct super_block *, void *, int))
686 struct block_device *bdev;
687 struct super_block *s;
690 bdev = open_bdev_excl(dev_name, flags, fs_type);
692 return (struct super_block *)bdev;
695 * once the super is inserted into the list by sget, s_umount
696 * will protect the lockfs code from trying to start a snapshot
697 * while we are mounting
699 mutex_lock(&bdev->bd_mount_mutex);
700 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
701 mutex_unlock(&bdev->bd_mount_mutex);
706 if ((flags ^ s->s_flags) & MS_RDONLY) {
707 up_write(&s->s_umount);
713 char b[BDEVNAME_SIZE];
716 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
717 sb_set_blocksize(s, block_size(bdev));
718 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
720 up_write(&s->s_umount);
724 s->s_flags |= MS_ACTIVE;
725 bdev_uevent(bdev, KOBJ_MOUNT);
732 close_bdev_excl(bdev);
736 EXPORT_SYMBOL(get_sb_bdev);
738 void kill_block_super(struct super_block *sb)
740 struct block_device *bdev = sb->s_bdev;
742 bdev_uevent(bdev, KOBJ_UMOUNT);
743 generic_shutdown_super(sb);
745 close_bdev_excl(bdev);
748 EXPORT_SYMBOL(kill_block_super);
750 struct super_block *get_sb_nodev(struct file_system_type *fs_type,
751 int flags, void *data,
752 int (*fill_super)(struct super_block *, void *, int))
755 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
762 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
764 up_write(&s->s_umount);
766 return ERR_PTR(error);
768 s->s_flags |= MS_ACTIVE;
772 EXPORT_SYMBOL(get_sb_nodev);
774 static int compare_single(struct super_block *s, void *p)
779 struct super_block *get_sb_single(struct file_system_type *fs_type,
780 int flags, void *data,
781 int (*fill_super)(struct super_block *, void *, int))
783 struct super_block *s;
786 s = sget(fs_type, compare_single, set_anon_super, NULL);
791 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
793 up_write(&s->s_umount);
795 return ERR_PTR(error);
797 s->s_flags |= MS_ACTIVE;
799 do_remount_sb(s, flags, data, 0);
803 EXPORT_SYMBOL(get_sb_single);
806 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
808 struct file_system_type *type = get_fs_type(fstype);
809 struct super_block *sb;
810 struct vfsmount *mnt;
812 char *secdata = NULL;
815 return ERR_PTR(-ENODEV);
817 sb = ERR_PTR(-EPERM);
818 if ((type->fs_flags & FS_BINARY_MOUNTDATA) &&
819 !vx_capable(CAP_SYS_ADMIN, VXC_BINARY_MOUNT))
822 sb = ERR_PTR(-ENOMEM);
823 mnt = alloc_vfsmnt(name);
828 secdata = alloc_secdata();
830 sb = ERR_PTR(-ENOMEM);
834 error = security_sb_copy_data(type, data, secdata);
837 goto out_free_secdata;
841 sb = type->get_sb(type, flags, name, data);
843 goto out_free_secdata;
846 if (!capable(CAP_SYS_ADMIN) && !sb->s_bdev &&
847 (sb->s_magic != PROC_SUPER_MAGIC) &&
848 (sb->s_magic != DEVPTS_SUPER_MAGIC))
851 error = security_sb_kern_mount(sb, secdata);
855 mnt->mnt_root = dget(sb->s_root);
856 mnt->mnt_mountpoint = sb->s_root;
857 mnt->mnt_parent = mnt;
858 up_write(&sb->s_umount);
859 free_secdata(secdata);
860 put_filesystem(type);
863 up_write(&sb->s_umount);
864 deactivate_super(sb);
867 free_secdata(secdata);
871 put_filesystem(type);
872 return (struct vfsmount *)sb;
875 EXPORT_SYMBOL_GPL(do_kern_mount);
877 struct vfsmount *kern_mount(struct file_system_type *type)
879 return do_kern_mount(type->name, 0, type->name, NULL);
882 EXPORT_SYMBOL(kern_mount);