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);
85 down_write(&s->s_umount);
87 atomic_set(&s->s_active, 1);
88 mutex_init(&s->s_vfs_rename_mutex);
89 mutex_init(&s->s_dquot.dqio_mutex);
90 mutex_init(&s->s_dquot.dqonoff_mutex);
91 init_rwsem(&s->s_dquot.dqptr_sem);
92 init_waitqueue_head(&s->s_wait_unfrozen);
93 s->s_maxbytes = MAX_NON_LFS;
94 s->dq_op = sb_dquot_ops;
95 s->s_qcop = sb_quotactl_ops;
96 s->s_op = &default_op;
97 s->s_time_gran = 1000000000;
104 * destroy_super - frees a superblock
105 * @s: superblock to free
107 * Frees a superblock.
109 static inline void destroy_super(struct super_block *s)
117 /* Superblock refcounting */
120 * Drop a superblock's refcount. Returns non-zero if the superblock was
121 * destroyed. The caller must hold sb_lock.
123 static int __put_super(struct super_block *sb)
127 if (!--sb->s_count) {
135 * Drop a superblock's refcount.
136 * Returns non-zero if the superblock is about to be destroyed and
137 * at least is already removed from super_blocks list, so if we are
138 * making a loop through super blocks then we need to restart.
139 * The caller must hold sb_lock.
141 int __put_super_and_need_restart(struct super_block *sb)
143 /* check for race with generic_shutdown_super() */
144 if (list_empty(&sb->s_list)) {
145 /* super block is removed, need to restart... */
149 /* can't be the last, since s_list is still in use */
151 BUG_ON(sb->s_count == 0);
156 * put_super - drop a temporary reference to superblock
157 * @sb: superblock in question
159 * Drops a temporary reference, frees superblock if there's no
162 static void put_super(struct super_block *sb)
166 spin_unlock(&sb_lock);
171 * deactivate_super - drop an active reference to superblock
172 * @s: superblock to deactivate
174 * Drops an active reference to superblock, acquiring a temprory one if
175 * there is no active references left. In that case we lock superblock,
176 * tell fs driver to shut it down and drop the temporary reference we
179 void deactivate_super(struct super_block *s)
181 struct file_system_type *fs = s->s_type;
182 if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
183 s->s_count -= S_BIAS-1;
184 spin_unlock(&sb_lock);
186 down_write(&s->s_umount);
193 EXPORT_SYMBOL(deactivate_super);
196 * grab_super - acquire an active reference
197 * @s: reference we are trying to make active
199 * Tries to acquire an active reference. grab_super() is used when we
200 * had just found a superblock in super_blocks or fs_type->fs_supers
201 * and want to turn it into a full-blown active reference. grab_super()
202 * is called with sb_lock held and drops it. Returns 1 in case of
203 * success, 0 if we had failed (superblock contents was already dead or
204 * dying when grab_super() had been called).
206 static int grab_super(struct super_block *s) __releases(sb_lock)
209 spin_unlock(&sb_lock);
210 down_write(&s->s_umount);
213 if (s->s_count > S_BIAS) {
214 atomic_inc(&s->s_active);
216 spin_unlock(&sb_lock);
219 spin_unlock(&sb_lock);
221 up_write(&s->s_umount);
228 * Superblock locking. We really ought to get rid of these two.
230 void lock_super(struct super_block * sb)
233 mutex_lock(&sb->s_lock);
236 void unlock_super(struct super_block * sb)
239 mutex_unlock(&sb->s_lock);
242 EXPORT_SYMBOL(lock_super);
243 EXPORT_SYMBOL(unlock_super);
246 * Write out and wait upon all dirty data associated with this
247 * superblock. Filesystem data as well as the underlying block
248 * device. Takes the superblock lock. Requires a second blkdev
249 * flush by the caller to complete the operation.
251 void __fsync_super(struct super_block *sb)
253 sync_inodes_sb(sb, 0);
256 if (sb->s_dirt && sb->s_op->write_super)
257 sb->s_op->write_super(sb);
259 if (sb->s_op->sync_fs)
260 sb->s_op->sync_fs(sb, 1);
261 sync_blockdev(sb->s_bdev);
262 sync_inodes_sb(sb, 1);
266 * Write out and wait upon all dirty data associated with this
267 * superblock. Filesystem data as well as the underlying block
268 * device. Takes the superblock lock.
270 int fsync_super(struct super_block *sb)
273 return sync_blockdev(sb->s_bdev);
277 * generic_shutdown_super - common helper for ->kill_sb()
278 * @sb: superblock to kill
280 * generic_shutdown_super() does all fs-independent work on superblock
281 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
282 * that need destruction out of superblock, call generic_shutdown_super()
283 * and release aforementioned objects. Note: dentries and inodes _are_
284 * taken care of and do not need specific handling.
286 * Upon calling this function, the filesystem may no longer alter or
287 * rearrange the set of dentries belonging to this super_block, nor may it
288 * change the attachments of dentries to inodes.
290 void generic_shutdown_super(struct super_block *sb)
292 const struct super_operations *sop = sb->s_op;
296 shrink_dcache_for_umount(sb);
299 sb->s_flags &= ~MS_ACTIVE;
302 * wait for asynchronous fs operations to finish before going further
304 async_synchronize_full_domain(&sb->s_async_list);
306 /* bad name - it should be evict_inodes() */
307 invalidate_inodes(sb);
310 if (sop->write_super && sb->s_dirt)
311 sop->write_super(sb);
315 /* Forget any remaining inodes */
316 if (invalidate_inodes(sb)) {
317 printk("VFS: Busy inodes after unmount of %s. "
318 "Self-destruct in 5 seconds. Have a nice day...\n",
326 /* should be initialized for __put_super_and_need_restart() */
327 list_del_init(&sb->s_list);
328 list_del(&sb->s_instances);
329 spin_unlock(&sb_lock);
330 up_write(&sb->s_umount);
333 EXPORT_SYMBOL(generic_shutdown_super);
336 * sget - find or create a superblock
337 * @type: filesystem type superblock should belong to
338 * @test: comparison callback
339 * @set: setup callback
340 * @data: argument to each of them
342 struct super_block *sget(struct file_system_type *type,
343 int (*test)(struct super_block *,void *),
344 int (*set)(struct super_block *,void *),
347 struct super_block *s = NULL;
348 struct super_block *old;
354 list_for_each_entry(old, &type->fs_supers, s_instances) {
355 if (!test(old, data))
357 if (!grab_super(old))
365 spin_unlock(&sb_lock);
366 s = alloc_super(type);
368 return ERR_PTR(-ENOMEM);
374 spin_unlock(&sb_lock);
379 strlcpy(s->s_id, type->name, sizeof(s->s_id));
380 list_add_tail(&s->s_list, &super_blocks);
381 list_add(&s->s_instances, &type->fs_supers);
382 spin_unlock(&sb_lock);
383 get_filesystem(type);
389 void drop_super(struct super_block *sb)
391 up_read(&sb->s_umount);
395 EXPORT_SYMBOL(drop_super);
397 static inline void write_super(struct super_block *sb)
400 if (sb->s_root && sb->s_dirt)
401 if (sb->s_op->write_super)
402 sb->s_op->write_super(sb);
407 * Note: check the dirty flag before waiting, so we don't
408 * hold up the sync while mounting a device. (The newly
409 * mounted device won't need syncing.)
411 void sync_supers(void)
413 struct super_block *sb;
417 list_for_each_entry(sb, &super_blocks, s_list) {
420 spin_unlock(&sb_lock);
421 down_read(&sb->s_umount);
423 up_read(&sb->s_umount);
425 if (__put_super_and_need_restart(sb))
429 spin_unlock(&sb_lock);
433 * Call the ->sync_fs super_op against all filesystems which are r/w and
434 * which implement it.
436 * This operation is careful to avoid the livelock which could easily happen
437 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
438 * is used only here. We set it against all filesystems and then clear it as
439 * we sync them. So redirtied filesystems are skipped.
441 * But if process A is currently running sync_filesystems and then process B
442 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
443 * flags again, which will cause process A to resync everything. Fix that with
446 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
448 void sync_filesystems(int wait)
450 struct super_block *sb;
451 static DEFINE_MUTEX(mutex);
453 mutex_lock(&mutex); /* Could be down_interruptible */
455 list_for_each_entry(sb, &super_blocks, s_list) {
456 if (!sb->s_op->sync_fs)
458 if (sb->s_flags & MS_RDONLY)
460 sb->s_need_sync_fs = 1;
464 list_for_each_entry(sb, &super_blocks, s_list) {
465 if (!sb->s_need_sync_fs)
467 sb->s_need_sync_fs = 0;
468 if (sb->s_flags & MS_RDONLY)
469 continue; /* hm. Was remounted r/o meanwhile */
471 spin_unlock(&sb_lock);
472 down_read(&sb->s_umount);
473 async_synchronize_full_domain(&sb->s_async_list);
474 if (sb->s_root && (wait || sb->s_dirt))
475 sb->s_op->sync_fs(sb, wait);
476 up_read(&sb->s_umount);
477 /* restart only when sb is no longer on the list */
479 if (__put_super_and_need_restart(sb))
482 spin_unlock(&sb_lock);
483 mutex_unlock(&mutex);
487 * get_super - get the superblock of a device
488 * @bdev: device to get the superblock for
490 * Scans the superblock list and finds the superblock of the file system
491 * mounted on the device given. %NULL is returned if no match is found.
494 struct super_block * get_super(struct block_device *bdev)
496 struct super_block *sb;
503 list_for_each_entry(sb, &super_blocks, s_list) {
504 if (sb->s_bdev == bdev) {
506 spin_unlock(&sb_lock);
507 down_read(&sb->s_umount);
510 up_read(&sb->s_umount);
511 /* restart only when sb is no longer on the list */
513 if (__put_super_and_need_restart(sb))
517 spin_unlock(&sb_lock);
521 EXPORT_SYMBOL(get_super);
523 struct super_block * user_get_super(dev_t dev)
525 struct super_block *sb;
529 list_for_each_entry(sb, &super_blocks, s_list) {
530 if (sb->s_dev == dev) {
532 spin_unlock(&sb_lock);
533 down_read(&sb->s_umount);
536 up_read(&sb->s_umount);
537 /* restart only when sb is no longer on the list */
539 if (__put_super_and_need_restart(sb))
543 spin_unlock(&sb_lock);
547 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
549 struct super_block *s;
554 s = user_get_super(new_decode_dev(dev));
557 err = vfs_statfs(s->s_root, &sbuf);
562 memset(&tmp,0,sizeof(struct ustat));
563 tmp.f_tfree = sbuf.f_bfree;
564 tmp.f_tinode = sbuf.f_ffree;
566 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
572 * mark_files_ro - mark all files read-only
573 * @sb: superblock in question
575 * All files are marked read-only. We don't care about pending
576 * delete files so this should be used in 'force' mode only.
579 static void mark_files_ro(struct super_block *sb)
585 list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
586 struct vfsmount *mnt;
587 if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
591 if (!(f->f_mode & FMODE_WRITE))
593 f->f_mode &= ~FMODE_WRITE;
594 if (file_check_writeable(f) != 0)
596 file_release_write(f);
597 mnt = mntget(f->f_path.mnt);
600 * This can sleep, so we can't hold
601 * the file_list_lock() spinlock.
611 * do_remount_sb - asks filesystem to change mount options.
612 * @sb: superblock in question
613 * @flags: numeric part of options
614 * @data: the rest of options
615 * @force: whether or not to force the change
617 * Alters the mount options of a mounted file system.
619 int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
625 if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
628 if (flags & MS_RDONLY)
630 shrink_dcache_sb(sb);
633 /* If we are remounting RDONLY and current sb is read/write,
634 make sure there are no rw files opened */
635 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
638 else if (!fs_may_remount_ro(sb))
640 retval = DQUOT_OFF(sb, 1);
641 if (retval < 0 && retval != -ENOSYS)
644 remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
646 if (sb->s_op->remount_fs) {
648 retval = sb->s_op->remount_fs(sb, &flags, data);
653 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
655 DQUOT_ON_REMOUNT(sb);
659 static void do_emergency_remount(unsigned long foo)
661 struct super_block *sb;
664 list_for_each_entry(sb, &super_blocks, s_list) {
666 spin_unlock(&sb_lock);
667 down_read(&sb->s_umount);
668 if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
670 * ->remount_fs needs lock_kernel().
672 * What lock protects sb->s_flags??
675 do_remount_sb(sb, MS_RDONLY, NULL, 1);
681 spin_unlock(&sb_lock);
682 printk("Emergency Remount complete\n");
685 void emergency_remount(void)
687 pdflush_operation(do_emergency_remount, 0);
691 * Unnamed block devices are dummy devices used by virtual
692 * filesystems which don't use real block-devices. -- jrs
695 static DEFINE_IDA(unnamed_dev_ida);
696 static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
698 int set_anon_super(struct super_block *s, void *data)
704 if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
706 spin_lock(&unnamed_dev_lock);
707 error = ida_get_new(&unnamed_dev_ida, &dev);
708 spin_unlock(&unnamed_dev_lock);
709 if (error == -EAGAIN)
710 /* We raced and lost with another CPU. */
715 if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
716 spin_lock(&unnamed_dev_lock);
717 ida_remove(&unnamed_dev_ida, dev);
718 spin_unlock(&unnamed_dev_lock);
721 s->s_dev = MKDEV(0, dev & MINORMASK);
725 EXPORT_SYMBOL(set_anon_super);
727 void kill_anon_super(struct super_block *sb)
729 int slot = MINOR(sb->s_dev);
731 generic_shutdown_super(sb);
732 spin_lock(&unnamed_dev_lock);
733 ida_remove(&unnamed_dev_ida, slot);
734 spin_unlock(&unnamed_dev_lock);
737 EXPORT_SYMBOL(kill_anon_super);
739 void kill_litter_super(struct super_block *sb)
742 d_genocide(sb->s_root);
746 EXPORT_SYMBOL(kill_litter_super);
749 static int set_bdev_super(struct super_block *s, void *data)
752 s->s_dev = s->s_bdev->bd_dev;
756 static int test_bdev_super(struct super_block *s, void *data)
758 return (void *)s->s_bdev == data;
761 int get_sb_bdev(struct file_system_type *fs_type,
762 int flags, const char *dev_name, void *data,
763 int (*fill_super)(struct super_block *, void *, int),
764 struct vfsmount *mnt)
766 struct block_device *bdev;
767 struct super_block *s;
768 fmode_t mode = FMODE_READ;
771 if (!(flags & MS_RDONLY))
774 bdev = open_bdev_exclusive(dev_name, mode, fs_type);
776 return PTR_ERR(bdev);
779 * once the super is inserted into the list by sget, s_umount
780 * will protect the lockfs code from trying to start a snapshot
781 * while we are mounting
783 down(&bdev->bd_mount_sem);
784 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
785 up(&bdev->bd_mount_sem);
790 if ((flags ^ s->s_flags) & MS_RDONLY) {
791 up_write(&s->s_umount);
797 close_bdev_exclusive(bdev, mode);
799 char b[BDEVNAME_SIZE];
803 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
804 sb_set_blocksize(s, block_size(bdev));
805 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
807 up_write(&s->s_umount);
812 s->s_flags |= MS_ACTIVE;
816 return simple_set_mnt(mnt, s);
821 close_bdev_exclusive(bdev, mode);
826 EXPORT_SYMBOL(get_sb_bdev);
828 void kill_block_super(struct super_block *sb)
830 struct block_device *bdev = sb->s_bdev;
831 fmode_t mode = sb->s_mode;
834 generic_shutdown_super(sb);
836 close_bdev_exclusive(bdev, mode);
839 EXPORT_SYMBOL(kill_block_super);
842 int get_sb_nodev(struct file_system_type *fs_type,
843 int flags, void *data,
844 int (*fill_super)(struct super_block *, void *, int),
845 struct vfsmount *mnt)
848 struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
855 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
857 up_write(&s->s_umount);
861 s->s_flags |= MS_ACTIVE;
862 return simple_set_mnt(mnt, s);
865 EXPORT_SYMBOL(get_sb_nodev);
867 static int compare_single(struct super_block *s, void *p)
872 int get_sb_single(struct file_system_type *fs_type,
873 int flags, void *data,
874 int (*fill_super)(struct super_block *, void *, int),
875 struct vfsmount *mnt)
877 struct super_block *s;
880 s = sget(fs_type, compare_single, set_anon_super, NULL);
885 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
887 up_write(&s->s_umount);
891 s->s_flags |= MS_ACTIVE;
893 do_remount_sb(s, flags, data, 0);
894 return simple_set_mnt(mnt, s);
897 EXPORT_SYMBOL(get_sb_single);
900 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
902 struct vfsmount *mnt;
903 char *secdata = NULL;
907 return ERR_PTR(-ENODEV);
910 mnt = alloc_vfsmnt(name);
914 if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
915 secdata = alloc_secdata();
919 error = security_sb_copy_data(data, secdata);
921 goto out_free_secdata;
924 error = type->get_sb(type, flags, name, data, mnt);
926 goto out_free_secdata;
927 BUG_ON(!mnt->mnt_sb);
929 error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
933 mnt->mnt_mountpoint = mnt->mnt_root;
934 mnt->mnt_parent = mnt;
935 up_write(&mnt->mnt_sb->s_umount);
936 free_secdata(secdata);
940 up_write(&mnt->mnt_sb->s_umount);
941 deactivate_super(mnt->mnt_sb);
943 free_secdata(secdata);
947 return ERR_PTR(error);
950 EXPORT_SYMBOL_GPL(vfs_kern_mount);
952 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
955 const char *subtype = strchr(fstype, '.');
964 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
966 if (!mnt->mnt_sb->s_subtype)
976 do_kern_mount(const char *fstype, int flags, const char *name, void *data)
978 struct file_system_type *type = get_fs_type(fstype);
979 struct vfsmount *mnt;
981 return ERR_PTR(-ENODEV);
982 mnt = vfs_kern_mount(type, flags, name, data);
983 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
984 !mnt->mnt_sb->s_subtype)
985 mnt = fs_set_subtype(mnt, fstype);
986 put_filesystem(type);
989 EXPORT_SYMBOL_GPL(do_kern_mount);
991 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
993 return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
996 EXPORT_SYMBOL_GPL(kern_mount_data);