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 <asm/uaccess.h>
43 void get_filesystem(struct file_system_type
*fs
);
44 void put_filesystem(struct file_system_type
*fs
);
45 struct file_system_type
*get_fs_type(const char *name
);
47 LIST_HEAD(super_blocks
);
48 DEFINE_SPINLOCK(sb_lock
);
51 * alloc_super - create new superblock
52 * @type: filesystem type superblock should belong to
54 * Allocates and initializes a new &struct super_block. alloc_super()
55 * returns a pointer new superblock or %NULL if allocation had failed.
57 static struct super_block
*alloc_super(struct file_system_type
*type
)
59 struct super_block
*s
= kzalloc(sizeof(struct super_block
), GFP_USER
);
60 static struct super_operations default_op
;
63 if (security_sb_alloc(s
)) {
68 INIT_LIST_HEAD(&s
->s_dirty
);
69 INIT_LIST_HEAD(&s
->s_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_rwsem(&s
->s_umount
);
75 mutex_init(&s
->s_lock
);
76 lockdep_set_class(&s
->s_umount
, &type
->s_umount_key
);
78 * The locking rules for s_lock are up to the
79 * filesystem. For example ext3fs has different
80 * lock ordering than usbfs:
82 lockdep_set_class(&s
->s_lock
, &type
->s_lock_key
);
83 down_write(&s
->s_umount
);
85 atomic_set(&s
->s_active
, 1);
86 mutex_init(&s
->s_vfs_rename_mutex
);
87 mutex_init(&s
->s_dquot
.dqio_mutex
);
88 mutex_init(&s
->s_dquot
.dqonoff_mutex
);
89 init_rwsem(&s
->s_dquot
.dqptr_sem
);
90 init_waitqueue_head(&s
->s_wait_unfrozen
);
91 s
->s_maxbytes
= MAX_NON_LFS
;
92 s
->dq_op
= sb_dquot_ops
;
93 s
->s_qcop
= sb_quotactl_ops
;
94 s
->s_op
= &default_op
;
95 s
->s_time_gran
= 1000000000;
102 * destroy_super - frees a superblock
103 * @s: superblock to free
105 * Frees a superblock.
107 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 * Write out and wait upon all dirty data associated with this
225 * superblock. Filesystem data as well as the underlying block
226 * device. Takes the superblock lock. Requires a second blkdev
227 * flush by the caller to complete the operation.
229 void __fsync_super(struct super_block
*sb
)
231 sync_inodes_sb(sb
, 0);
234 if (sb
->s_dirt
&& sb
->s_op
->write_super
)
235 sb
->s_op
->write_super(sb
);
237 if (sb
->s_op
->sync_fs
)
238 sb
->s_op
->sync_fs(sb
, 1);
239 sync_blockdev(sb
->s_bdev
);
240 sync_inodes_sb(sb
, 1);
244 * Write out and wait upon all dirty data associated with this
245 * superblock. Filesystem data as well as the underlying block
246 * device. Takes the superblock lock.
248 int fsync_super(struct super_block
*sb
)
251 return sync_blockdev(sb
->s_bdev
);
255 * generic_shutdown_super - common helper for ->kill_sb()
256 * @sb: superblock to kill
258 * generic_shutdown_super() does all fs-independent work on superblock
259 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
260 * that need destruction out of superblock, call generic_shutdown_super()
261 * and release aforementioned objects. Note: dentries and inodes _are_
262 * taken care of and do not need specific handling.
264 * Upon calling this function, the filesystem may no longer alter or
265 * rearrange the set of dentries belonging to this super_block, nor may it
266 * change the attachments of dentries to inodes.
268 void generic_shutdown_super(struct super_block
*sb
)
270 struct super_operations
*sop
= sb
->s_op
;
273 shrink_dcache_for_umount(sb
);
276 sb
->s_flags
&= ~MS_ACTIVE
;
277 /* bad name - it should be evict_inodes() */
278 invalidate_inodes(sb
);
281 if (sop
->write_super
&& sb
->s_dirt
)
282 sop
->write_super(sb
);
286 /* Forget any remaining inodes */
287 if (invalidate_inodes(sb
)) {
288 printk("VFS: Busy inodes after unmount of %s. "
289 "Self-destruct in 5 seconds. Have a nice day...\n",
297 /* should be initialized for __put_super_and_need_restart() */
298 list_del_init(&sb
->s_list
);
299 list_del(&sb
->s_instances
);
300 spin_unlock(&sb_lock
);
301 up_write(&sb
->s_umount
);
304 EXPORT_SYMBOL(generic_shutdown_super
);
307 * sget - find or create a superblock
308 * @type: filesystem type superblock should belong to
309 * @test: comparison callback
310 * @set: setup callback
311 * @data: argument to each of them
313 struct super_block
*sget(struct file_system_type
*type
,
314 int (*test
)(struct super_block
*,void *),
315 int (*set
)(struct super_block
*,void *),
318 struct super_block
*s
= NULL
;
324 if (test
) list_for_each(p
, &type
->fs_supers
) {
325 struct super_block
*old
;
326 old
= list_entry(p
, struct super_block
, s_instances
);
327 if (!test(old
, data
))
329 if (!grab_super(old
))
336 spin_unlock(&sb_lock
);
337 s
= alloc_super(type
);
339 return ERR_PTR(-ENOMEM
);
345 spin_unlock(&sb_lock
);
350 strlcpy(s
->s_id
, type
->name
, sizeof(s
->s_id
));
351 list_add_tail(&s
->s_list
, &super_blocks
);
352 list_add(&s
->s_instances
, &type
->fs_supers
);
353 spin_unlock(&sb_lock
);
354 get_filesystem(type
);
360 void drop_super(struct super_block
*sb
)
362 up_read(&sb
->s_umount
);
366 EXPORT_SYMBOL(drop_super
);
368 static inline void write_super(struct super_block
*sb
)
371 if (sb
->s_root
&& sb
->s_dirt
)
372 if (sb
->s_op
->write_super
)
373 sb
->s_op
->write_super(sb
);
378 * Note: check the dirty flag before waiting, so we don't
379 * hold up the sync while mounting a device. (The newly
380 * mounted device won't need syncing.)
382 void sync_supers(void)
384 struct super_block
*sb
;
388 list_for_each_entry(sb
, &super_blocks
, s_list
) {
391 spin_unlock(&sb_lock
);
392 down_read(&sb
->s_umount
);
394 up_read(&sb
->s_umount
);
396 if (__put_super_and_need_restart(sb
))
400 spin_unlock(&sb_lock
);
404 * Call the ->sync_fs super_op against all filesytems which are r/w and
405 * which implement it.
407 * This operation is careful to avoid the livelock which could easily happen
408 * if two or more filesystems are being continuously dirtied. s_need_sync_fs
409 * is used only here. We set it against all filesystems and then clear it as
410 * we sync them. So redirtied filesystems are skipped.
412 * But if process A is currently running sync_filesytems and then process B
413 * calls sync_filesystems as well, process B will set all the s_need_sync_fs
414 * flags again, which will cause process A to resync everything. Fix that with
417 * (Fabian) Avoid sync_fs with clean fs & wait mode 0
419 void sync_filesystems(int wait
)
421 struct super_block
*sb
;
422 static DEFINE_MUTEX(mutex
);
424 mutex_lock(&mutex
); /* Could be down_interruptible */
426 list_for_each_entry(sb
, &super_blocks
, s_list
) {
427 if (!sb
->s_op
->sync_fs
)
429 if (sb
->s_flags
& MS_RDONLY
)
431 sb
->s_need_sync_fs
= 1;
435 list_for_each_entry(sb
, &super_blocks
, s_list
) {
436 if (!sb
->s_need_sync_fs
)
438 sb
->s_need_sync_fs
= 0;
439 if (sb
->s_flags
& MS_RDONLY
)
440 continue; /* hm. Was remounted r/o meanwhile */
442 spin_unlock(&sb_lock
);
443 down_read(&sb
->s_umount
);
444 if (sb
->s_root
&& (wait
|| sb
->s_dirt
))
445 sb
->s_op
->sync_fs(sb
, wait
);
446 up_read(&sb
->s_umount
);
447 /* restart only when sb is no longer on the list */
449 if (__put_super_and_need_restart(sb
))
452 spin_unlock(&sb_lock
);
453 mutex_unlock(&mutex
);
457 * get_super - get the superblock of a device
458 * @bdev: device to get the superblock for
460 * Scans the superblock list and finds the superblock of the file system
461 * mounted on the device given. %NULL is returned if no match is found.
464 struct super_block
* get_super(struct block_device
*bdev
)
466 struct super_block
*sb
;
473 list_for_each_entry(sb
, &super_blocks
, s_list
) {
474 if (sb
->s_bdev
== bdev
) {
476 spin_unlock(&sb_lock
);
477 down_read(&sb
->s_umount
);
480 up_read(&sb
->s_umount
);
481 /* restart only when sb is no longer on the list */
483 if (__put_super_and_need_restart(sb
))
487 spin_unlock(&sb_lock
);
491 EXPORT_SYMBOL(get_super
);
493 struct super_block
* user_get_super(dev_t dev
)
495 struct super_block
*sb
;
499 list_for_each_entry(sb
, &super_blocks
, s_list
) {
500 if (sb
->s_dev
== dev
) {
502 spin_unlock(&sb_lock
);
503 down_read(&sb
->s_umount
);
506 up_read(&sb
->s_umount
);
507 /* restart only when sb is no longer on the list */
509 if (__put_super_and_need_restart(sb
))
513 spin_unlock(&sb_lock
);
517 asmlinkage
long sys_ustat(unsigned dev
, struct ustat __user
* ubuf
)
519 struct super_block
*s
;
524 s
= user_get_super(new_decode_dev(dev
));
527 err
= vfs_statfs(s
->s_root
, &sbuf
);
532 memset(&tmp
,0,sizeof(struct ustat
));
533 tmp
.f_tfree
= sbuf
.f_bfree
;
534 tmp
.f_tinode
= sbuf
.f_ffree
;
536 err
= copy_to_user(ubuf
,&tmp
,sizeof(struct ustat
)) ? -EFAULT
: 0;
543 * @sb: superblock in question
545 * All files are marked read/only. We don't care about pending
546 * delete files so this should be used in 'force' mode only
549 static void mark_files_ro(struct super_block
*sb
)
554 list_for_each_entry(f
, &sb
->s_files
, f_u
.fu_list
) {
555 if (S_ISREG(f
->f_dentry
->d_inode
->i_mode
) && file_count(f
))
556 f
->f_mode
&= ~FMODE_WRITE
;
562 * do_remount_sb - asks filesystem to change mount options.
563 * @sb: superblock in question
564 * @flags: numeric part of options
565 * @data: the rest of options
566 * @force: whether or not to force the change
568 * Alters the mount options of a mounted file system.
570 int do_remount_sb(struct super_block
*sb
, int flags
, void *data
, int force
)
575 if (!(flags
& MS_RDONLY
) && bdev_read_only(sb
->s_bdev
))
578 if (flags
& MS_RDONLY
)
580 shrink_dcache_sb(sb
);
583 /* If we are remounting RDONLY and current sb is read/write,
584 make sure there are no rw files opened */
585 if ((flags
& MS_RDONLY
) && !(sb
->s_flags
& MS_RDONLY
)) {
588 else if (!fs_may_remount_ro(sb
))
592 if (sb
->s_op
->remount_fs
) {
594 retval
= sb
->s_op
->remount_fs(sb
, &flags
, data
);
599 sb
->s_flags
= (sb
->s_flags
& ~MS_RMT_MASK
) | (flags
& MS_RMT_MASK
);
603 static void do_emergency_remount(unsigned long foo
)
605 struct super_block
*sb
;
608 list_for_each_entry(sb
, &super_blocks
, s_list
) {
610 spin_unlock(&sb_lock
);
611 down_read(&sb
->s_umount
);
612 if (sb
->s_root
&& sb
->s_bdev
&& !(sb
->s_flags
& MS_RDONLY
)) {
614 * ->remount_fs needs lock_kernel().
616 * What lock protects sb->s_flags??
619 do_remount_sb(sb
, MS_RDONLY
, NULL
, 1);
625 spin_unlock(&sb_lock
);
626 printk("Emergency Remount complete\n");
629 void emergency_remount(void)
631 pdflush_operation(do_emergency_remount
, 0);
635 * Unnamed block devices are dummy devices used by virtual
636 * filesystems which don't use real block-devices. -- jrs
639 static struct idr unnamed_dev_idr
;
640 static DEFINE_SPINLOCK(unnamed_dev_lock
);/* protects the above */
642 int set_anon_super(struct super_block
*s
, void *data
)
648 if (idr_pre_get(&unnamed_dev_idr
, GFP_ATOMIC
) == 0)
650 spin_lock(&unnamed_dev_lock
);
651 error
= idr_get_new(&unnamed_dev_idr
, NULL
, &dev
);
652 spin_unlock(&unnamed_dev_lock
);
653 if (error
== -EAGAIN
)
654 /* We raced and lost with another CPU. */
659 if ((dev
& MAX_ID_MASK
) == (1 << MINORBITS
)) {
660 spin_lock(&unnamed_dev_lock
);
661 idr_remove(&unnamed_dev_idr
, dev
);
662 spin_unlock(&unnamed_dev_lock
);
665 s
->s_dev
= MKDEV(0, dev
& MINORMASK
);
669 EXPORT_SYMBOL(set_anon_super
);
671 void kill_anon_super(struct super_block
*sb
)
673 int slot
= MINOR(sb
->s_dev
);
675 generic_shutdown_super(sb
);
676 spin_lock(&unnamed_dev_lock
);
677 idr_remove(&unnamed_dev_idr
, slot
);
678 spin_unlock(&unnamed_dev_lock
);
681 EXPORT_SYMBOL(kill_anon_super
);
683 void __init
unnamed_dev_init(void)
685 idr_init(&unnamed_dev_idr
);
688 void kill_litter_super(struct super_block
*sb
)
691 d_genocide(sb
->s_root
);
695 EXPORT_SYMBOL(kill_litter_super
);
698 static int set_bdev_super(struct super_block
*s
, void *data
)
701 s
->s_dev
= s
->s_bdev
->bd_dev
;
705 static int test_bdev_super(struct super_block
*s
, void *data
)
707 return (void *)s
->s_bdev
== data
;
710 static void bdev_uevent(struct block_device
*bdev
, enum kobject_action action
)
714 kobject_uevent(&bdev
->bd_part
->kobj
, action
);
716 kobject_uevent(&bdev
->bd_disk
->kobj
, action
);
720 int get_sb_bdev(struct file_system_type
*fs_type
,
721 int flags
, const char *dev_name
, void *data
,
722 int (*fill_super
)(struct super_block
*, void *, int),
723 struct vfsmount
*mnt
)
725 struct block_device
*bdev
;
726 struct super_block
*s
;
729 bdev
= open_bdev_excl(dev_name
, flags
, fs_type
);
731 return PTR_ERR(bdev
);
734 * once the super is inserted into the list by sget, s_umount
735 * will protect the lockfs code from trying to start a snapshot
736 * while we are mounting
738 mutex_lock(&bdev
->bd_mount_mutex
);
739 s
= sget(fs_type
, test_bdev_super
, set_bdev_super
, bdev
);
740 mutex_unlock(&bdev
->bd_mount_mutex
);
745 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
746 up_write(&s
->s_umount
);
752 close_bdev_excl(bdev
);
754 char b
[BDEVNAME_SIZE
];
757 strlcpy(s
->s_id
, bdevname(bdev
, b
), sizeof(s
->s_id
));
758 sb_set_blocksize(s
, block_size(bdev
));
759 error
= fill_super(s
, data
, flags
& MS_SILENT
? 1 : 0);
761 up_write(&s
->s_umount
);
766 s
->s_flags
|= MS_ACTIVE
;
767 bdev_uevent(bdev
, KOBJ_MOUNT
);
770 return simple_set_mnt(mnt
, s
);
775 close_bdev_excl(bdev
);
780 EXPORT_SYMBOL(get_sb_bdev
);
782 void kill_block_super(struct super_block
*sb
)
784 struct block_device
*bdev
= sb
->s_bdev
;
786 bdev_uevent(bdev
, KOBJ_UMOUNT
);
787 generic_shutdown_super(sb
);
789 close_bdev_excl(bdev
);
792 EXPORT_SYMBOL(kill_block_super
);
795 int get_sb_nodev(struct file_system_type
*fs_type
,
796 int flags
, void *data
,
797 int (*fill_super
)(struct super_block
*, void *, int),
798 struct vfsmount
*mnt
)
801 struct super_block
*s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
808 error
= fill_super(s
, data
, flags
& MS_SILENT
? 1 : 0);
810 up_write(&s
->s_umount
);
814 s
->s_flags
|= MS_ACTIVE
;
815 return simple_set_mnt(mnt
, s
);
818 EXPORT_SYMBOL(get_sb_nodev
);
820 static int compare_single(struct super_block
*s
, void *p
)
825 int get_sb_single(struct file_system_type
*fs_type
,
826 int flags
, void *data
,
827 int (*fill_super
)(struct super_block
*, void *, int),
828 struct vfsmount
*mnt
)
830 struct super_block
*s
;
833 s
= sget(fs_type
, compare_single
, set_anon_super
, NULL
);
838 error
= fill_super(s
, data
, flags
& MS_SILENT
? 1 : 0);
840 up_write(&s
->s_umount
);
844 s
->s_flags
|= MS_ACTIVE
;
846 do_remount_sb(s
, flags
, data
, 0);
847 return simple_set_mnt(mnt
, s
);
850 EXPORT_SYMBOL(get_sb_single
);
853 vfs_kern_mount(struct file_system_type
*type
, int flags
, const char *name
, void *data
)
855 struct vfsmount
*mnt
;
856 char *secdata
= NULL
;
860 return ERR_PTR(-ENODEV
);
863 mnt
= alloc_vfsmnt(name
);
868 secdata
= alloc_secdata();
872 error
= security_sb_copy_data(type
, data
, secdata
);
874 goto out_free_secdata
;
877 error
= type
->get_sb(type
, flags
, name
, data
, mnt
);
879 goto out_free_secdata
;
881 error
= security_sb_kern_mount(mnt
->mnt_sb
, secdata
);
885 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
886 mnt
->mnt_parent
= mnt
;
887 up_write(&mnt
->mnt_sb
->s_umount
);
888 free_secdata(secdata
);
892 up_write(&mnt
->mnt_sb
->s_umount
);
893 deactivate_super(mnt
->mnt_sb
);
895 free_secdata(secdata
);
899 return ERR_PTR(error
);
902 EXPORT_SYMBOL_GPL(vfs_kern_mount
);
905 do_kern_mount(const char *fstype
, int flags
, const char *name
, void *data
)
907 struct file_system_type
*type
= get_fs_type(fstype
);
908 struct vfsmount
*mnt
;
910 return ERR_PTR(-ENODEV
);
911 mnt
= vfs_kern_mount(type
, flags
, name
, data
);
912 put_filesystem(type
);
916 struct vfsmount
*kern_mount(struct file_system_type
*type
)
918 return vfs_kern_mount(type
, 0, type
->name
, NULL
);
921 EXPORT_SYMBOL(kern_mount
);