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/acct.h>
26 #include <linux/blkdev.h>
27 #include <linux/mount.h>
28 #include <linux/security.h>
29 #include <linux/writeback.h> /* for the emergency remount stuff */
30 #include <linux/idr.h>
31 #include <linux/mutex.h>
32 #include <linux/backing-dev.h>
33 #include <linux/rculist_bl.h>
34 #include <linux/cleancache.h>
38 LIST_HEAD(super_blocks
);
39 DEFINE_SPINLOCK(sb_lock
);
42 * One thing we have to be careful of with a per-sb shrinker is that we don't
43 * drop the last active reference to the superblock from within the shrinker.
44 * If that happens we could trigger unregistering the shrinker from within the
45 * shrinker path and that leads to deadlock on the shrinker_rwsem. Hence we
46 * take a passive reference to the superblock to avoid this from occurring.
48 static int prune_super(struct shrinker
*shrink
, struct shrink_control
*sc
)
50 struct super_block
*sb
;
53 sb
= container_of(shrink
, struct super_block
, s_shrink
);
56 * Deadlock avoidance. We may hold various FS locks, and we don't want
57 * to recurse into the FS that called us in clear_inode() and friends..
59 if (sc
->nr_to_scan
&& !(sc
->gfp_mask
& __GFP_FS
))
62 if (!grab_super_passive(sb
))
66 /* proportion the scan between the two caches */
69 total
= sb
->s_nr_dentry_unused
+ sb
->s_nr_inodes_unused
+ 1;
70 count
= (sc
->nr_to_scan
* sb
->s_nr_dentry_unused
) / total
;
72 /* prune dcache first as icache is pinned by it */
73 prune_dcache_sb(sb
, count
);
74 prune_icache_sb(sb
, sc
->nr_to_scan
- count
);
77 count
= ((sb
->s_nr_dentry_unused
+ sb
->s_nr_inodes_unused
) / 100)
78 * sysctl_vfs_cache_pressure
;
84 * alloc_super - create new superblock
85 * @type: filesystem type superblock should belong to
87 * Allocates and initializes a new &struct super_block. alloc_super()
88 * returns a pointer new superblock or %NULL if allocation had failed.
90 static struct super_block
*alloc_super(struct file_system_type
*type
)
92 struct super_block
*s
= kzalloc(sizeof(struct super_block
), GFP_USER
);
93 static const struct super_operations default_op
;
96 if (security_sb_alloc(s
)) {
102 s
->s_files
= alloc_percpu(struct list_head
);
111 for_each_possible_cpu(i
)
112 INIT_LIST_HEAD(per_cpu_ptr(s
->s_files
, i
));
115 INIT_LIST_HEAD(&s
->s_files
);
117 s
->s_bdi
= &default_backing_dev_info
;
118 INIT_LIST_HEAD(&s
->s_instances
);
119 INIT_HLIST_BL_HEAD(&s
->s_anon
);
120 INIT_LIST_HEAD(&s
->s_inodes
);
121 INIT_LIST_HEAD(&s
->s_dentry_lru
);
122 INIT_LIST_HEAD(&s
->s_inode_lru
);
123 spin_lock_init(&s
->s_inode_lru_lock
);
124 init_rwsem(&s
->s_umount
);
125 mutex_init(&s
->s_lock
);
126 lockdep_set_class(&s
->s_umount
, &type
->s_umount_key
);
128 * The locking rules for s_lock are up to the
129 * filesystem. For example ext3fs has different
130 * lock ordering than usbfs:
132 lockdep_set_class(&s
->s_lock
, &type
->s_lock_key
);
134 * sget() can have s_umount recursion.
136 * When it cannot find a suitable sb, it allocates a new
137 * one (this one), and tries again to find a suitable old
140 * In case that succeeds, it will acquire the s_umount
141 * lock of the old one. Since these are clearly distrinct
142 * locks, and this object isn't exposed yet, there's no
145 * Annotate this by putting this lock in a different
148 down_write_nested(&s
->s_umount
, SINGLE_DEPTH_NESTING
);
150 atomic_set(&s
->s_active
, 1);
151 mutex_init(&s
->s_vfs_rename_mutex
);
152 lockdep_set_class(&s
->s_vfs_rename_mutex
, &type
->s_vfs_rename_key
);
153 mutex_init(&s
->s_dquot
.dqio_mutex
);
154 mutex_init(&s
->s_dquot
.dqonoff_mutex
);
155 init_rwsem(&s
->s_dquot
.dqptr_sem
);
156 init_waitqueue_head(&s
->s_wait_unfrozen
);
157 s
->s_maxbytes
= MAX_NON_LFS
;
158 s
->s_op
= &default_op
;
159 s
->s_time_gran
= 1000000000;
160 s
->cleancache_poolid
= -1;
162 s
->s_shrink
.seeks
= DEFAULT_SEEKS
;
163 s
->s_shrink
.shrink
= prune_super
;
170 * destroy_super - frees a superblock
171 * @s: superblock to free
173 * Frees a superblock.
175 static inline void destroy_super(struct super_block
*s
)
178 free_percpu(s
->s_files
);
186 /* Superblock refcounting */
189 * Drop a superblock's refcount. The caller must hold sb_lock.
191 void __put_super(struct super_block
*sb
)
193 if (!--sb
->s_count
) {
194 list_del_init(&sb
->s_list
);
200 * put_super - drop a temporary reference to superblock
201 * @sb: superblock in question
203 * Drops a temporary reference, frees superblock if there's no
206 void put_super(struct super_block
*sb
)
210 spin_unlock(&sb_lock
);
215 * deactivate_locked_super - drop an active reference to superblock
216 * @s: superblock to deactivate
218 * Drops an active reference to superblock, converting it into a temprory
219 * one if there is no other active references left. In that case we
220 * tell fs driver to shut it down and drop the temporary reference we
223 * Caller holds exclusive lock on superblock; that lock is released.
225 void deactivate_locked_super(struct super_block
*s
)
227 struct file_system_type
*fs
= s
->s_type
;
228 if (atomic_dec_and_test(&s
->s_active
)) {
229 cleancache_flush_fs(s
);
232 /* caches are now gone, we can safely kill the shrinker now */
233 unregister_shrinker(&s
->s_shrink
);
236 * We need to call rcu_barrier so all the delayed rcu free
237 * inodes are flushed before we release the fs module.
243 up_write(&s
->s_umount
);
247 EXPORT_SYMBOL(deactivate_locked_super
);
250 * deactivate_super - drop an active reference to superblock
251 * @s: superblock to deactivate
253 * Variant of deactivate_locked_super(), except that superblock is *not*
254 * locked by caller. If we are going to drop the final active reference,
255 * lock will be acquired prior to that.
257 void deactivate_super(struct super_block
*s
)
259 if (!atomic_add_unless(&s
->s_active
, -1, 1)) {
260 down_write(&s
->s_umount
);
261 deactivate_locked_super(s
);
265 EXPORT_SYMBOL(deactivate_super
);
268 * grab_super - acquire an active reference
269 * @s: reference we are trying to make active
271 * Tries to acquire an active reference. grab_super() is used when we
272 * had just found a superblock in super_blocks or fs_type->fs_supers
273 * and want to turn it into a full-blown active reference. grab_super()
274 * is called with sb_lock held and drops it. Returns 1 in case of
275 * success, 0 if we had failed (superblock contents was already dead or
276 * dying when grab_super() had been called).
278 static int grab_super(struct super_block
*s
) __releases(sb_lock
)
280 if (atomic_inc_not_zero(&s
->s_active
)) {
281 spin_unlock(&sb_lock
);
284 /* it's going away */
286 spin_unlock(&sb_lock
);
287 /* wait for it to die */
288 down_write(&s
->s_umount
);
289 up_write(&s
->s_umount
);
295 * grab_super_passive - acquire a passive reference
296 * @s: reference we are trying to grab
298 * Tries to acquire a passive reference. This is used in places where we
299 * cannot take an active reference but we need to ensure that the
300 * superblock does not go away while we are working on it. It returns
301 * false if a reference was not gained, and returns true with the s_umount
302 * lock held in read mode if a reference is gained. On successful return,
303 * the caller must drop the s_umount lock and the passive reference when
306 bool grab_super_passive(struct super_block
*sb
)
309 if (list_empty(&sb
->s_instances
)) {
310 spin_unlock(&sb_lock
);
315 spin_unlock(&sb_lock
);
317 if (down_read_trylock(&sb
->s_umount
)) {
320 up_read(&sb
->s_umount
);
328 * Superblock locking. We really ought to get rid of these two.
330 void lock_super(struct super_block
* sb
)
333 mutex_lock(&sb
->s_lock
);
336 void unlock_super(struct super_block
* sb
)
339 mutex_unlock(&sb
->s_lock
);
342 EXPORT_SYMBOL(lock_super
);
343 EXPORT_SYMBOL(unlock_super
);
346 * generic_shutdown_super - common helper for ->kill_sb()
347 * @sb: superblock to kill
349 * generic_shutdown_super() does all fs-independent work on superblock
350 * shutdown. Typical ->kill_sb() should pick all fs-specific objects
351 * that need destruction out of superblock, call generic_shutdown_super()
352 * and release aforementioned objects. Note: dentries and inodes _are_
353 * taken care of and do not need specific handling.
355 * Upon calling this function, the filesystem may no longer alter or
356 * rearrange the set of dentries belonging to this super_block, nor may it
357 * change the attachments of dentries to inodes.
359 void generic_shutdown_super(struct super_block
*sb
)
361 const struct super_operations
*sop
= sb
->s_op
;
364 shrink_dcache_for_umount(sb
);
367 sb
->s_flags
&= ~MS_ACTIVE
;
369 fsnotify_unmount_inodes(&sb
->s_inodes
);
376 if (!list_empty(&sb
->s_inodes
)) {
377 printk("VFS: Busy inodes after unmount of %s. "
378 "Self-destruct in 5 seconds. Have a nice day...\n",
384 /* should be initialized for __put_super_and_need_restart() */
385 list_del_init(&sb
->s_instances
);
386 spin_unlock(&sb_lock
);
387 up_write(&sb
->s_umount
);
390 EXPORT_SYMBOL(generic_shutdown_super
);
393 * sget - find or create a superblock
394 * @type: filesystem type superblock should belong to
395 * @test: comparison callback
396 * @set: setup callback
397 * @data: argument to each of them
399 struct super_block
*sget(struct file_system_type
*type
,
400 int (*test
)(struct super_block
*,void *),
401 int (*set
)(struct super_block
*,void *),
404 struct super_block
*s
= NULL
;
405 struct super_block
*old
;
411 list_for_each_entry(old
, &type
->fs_supers
, s_instances
) {
412 if (!test(old
, data
))
414 if (!grab_super(old
))
417 up_write(&s
->s_umount
);
421 down_write(&old
->s_umount
);
422 if (unlikely(!(old
->s_flags
& MS_BORN
))) {
423 deactivate_locked_super(old
);
430 spin_unlock(&sb_lock
);
431 s
= alloc_super(type
);
433 return ERR_PTR(-ENOMEM
);
439 spin_unlock(&sb_lock
);
440 up_write(&s
->s_umount
);
445 strlcpy(s
->s_id
, type
->name
, sizeof(s
->s_id
));
446 list_add_tail(&s
->s_list
, &super_blocks
);
447 list_add(&s
->s_instances
, &type
->fs_supers
);
448 spin_unlock(&sb_lock
);
449 get_filesystem(type
);
450 register_shrinker(&s
->s_shrink
);
456 void drop_super(struct super_block
*sb
)
458 up_read(&sb
->s_umount
);
462 EXPORT_SYMBOL(drop_super
);
465 * sync_supers - helper for periodic superblock writeback
467 * Call the write_super method if present on all dirty superblocks in
468 * the system. This is for the periodic writeback used by most older
469 * filesystems. For data integrity superblock writeback use
470 * sync_filesystems() instead.
472 * Note: check the dirty flag before waiting, so we don't
473 * hold up the sync while mounting a device. (The newly
474 * mounted device won't need syncing.)
476 void sync_supers(void)
478 struct super_block
*sb
, *p
= NULL
;
481 list_for_each_entry(sb
, &super_blocks
, s_list
) {
482 if (list_empty(&sb
->s_instances
))
484 if (sb
->s_op
->write_super
&& sb
->s_dirt
) {
486 spin_unlock(&sb_lock
);
488 down_read(&sb
->s_umount
);
489 if (sb
->s_root
&& sb
->s_dirt
)
490 sb
->s_op
->write_super(sb
);
491 up_read(&sb
->s_umount
);
501 spin_unlock(&sb_lock
);
505 * iterate_supers - call function for all active superblocks
506 * @f: function to call
507 * @arg: argument to pass to it
509 * Scans the superblock list and calls given function, passing it
510 * locked superblock and given argument.
512 void iterate_supers(void (*f
)(struct super_block
*, void *), void *arg
)
514 struct super_block
*sb
, *p
= NULL
;
517 list_for_each_entry(sb
, &super_blocks
, s_list
) {
518 if (list_empty(&sb
->s_instances
))
521 spin_unlock(&sb_lock
);
523 down_read(&sb
->s_umount
);
526 up_read(&sb
->s_umount
);
535 spin_unlock(&sb_lock
);
539 * iterate_supers_type - call function for superblocks of given type
541 * @f: function to call
542 * @arg: argument to pass to it
544 * Scans the superblock list and calls given function, passing it
545 * locked superblock and given argument.
547 void iterate_supers_type(struct file_system_type
*type
,
548 void (*f
)(struct super_block
*, void *), void *arg
)
550 struct super_block
*sb
, *p
= NULL
;
553 list_for_each_entry(sb
, &type
->fs_supers
, s_instances
) {
555 spin_unlock(&sb_lock
);
557 down_read(&sb
->s_umount
);
560 up_read(&sb
->s_umount
);
569 spin_unlock(&sb_lock
);
572 EXPORT_SYMBOL(iterate_supers_type
);
575 * get_super - get the superblock of a device
576 * @bdev: device to get the superblock for
578 * Scans the superblock list and finds the superblock of the file system
579 * mounted on the device given. %NULL is returned if no match is found.
582 struct super_block
*get_super(struct block_device
*bdev
)
584 struct super_block
*sb
;
591 list_for_each_entry(sb
, &super_blocks
, s_list
) {
592 if (list_empty(&sb
->s_instances
))
594 if (sb
->s_bdev
== bdev
) {
596 spin_unlock(&sb_lock
);
597 down_read(&sb
->s_umount
);
601 up_read(&sb
->s_umount
);
602 /* nope, got unmounted */
608 spin_unlock(&sb_lock
);
612 EXPORT_SYMBOL(get_super
);
615 * get_active_super - get an active reference to the superblock of a device
616 * @bdev: device to get the superblock for
618 * Scans the superblock list and finds the superblock of the file system
619 * mounted on the device given. Returns the superblock with an active
620 * reference or %NULL if none was found.
622 struct super_block
*get_active_super(struct block_device
*bdev
)
624 struct super_block
*sb
;
631 list_for_each_entry(sb
, &super_blocks
, s_list
) {
632 if (list_empty(&sb
->s_instances
))
634 if (sb
->s_bdev
== bdev
) {
635 if (grab_super(sb
)) /* drops sb_lock */
641 spin_unlock(&sb_lock
);
645 struct super_block
*user_get_super(dev_t dev
)
647 struct super_block
*sb
;
651 list_for_each_entry(sb
, &super_blocks
, s_list
) {
652 if (list_empty(&sb
->s_instances
))
654 if (sb
->s_dev
== dev
) {
656 spin_unlock(&sb_lock
);
657 down_read(&sb
->s_umount
);
661 up_read(&sb
->s_umount
);
662 /* nope, got unmounted */
668 spin_unlock(&sb_lock
);
673 * do_remount_sb - asks filesystem to change mount options.
674 * @sb: superblock in question
675 * @flags: numeric part of options
676 * @data: the rest of options
677 * @force: whether or not to force the change
679 * Alters the mount options of a mounted file system.
681 int do_remount_sb(struct super_block
*sb
, int flags
, void *data
, int force
)
686 if (sb
->s_frozen
!= SB_UNFROZEN
)
690 if (!(flags
& MS_RDONLY
) && bdev_read_only(sb
->s_bdev
))
694 if (flags
& MS_RDONLY
)
696 shrink_dcache_sb(sb
);
699 remount_ro
= (flags
& MS_RDONLY
) && !(sb
->s_flags
& MS_RDONLY
);
701 /* If we are remounting RDONLY and current sb is read/write,
702 make sure there are no rw files opened */
706 else if (!fs_may_remount_ro(sb
))
710 if (sb
->s_op
->remount_fs
) {
711 retval
= sb
->s_op
->remount_fs(sb
, &flags
, data
);
715 sb
->s_flags
= (sb
->s_flags
& ~MS_RMT_MASK
) | (flags
& MS_RMT_MASK
);
718 * Some filesystems modify their metadata via some other path than the
719 * bdev buffer cache (eg. use a private mapping, or directories in
720 * pagecache, etc). Also file data modifications go via their own
721 * mappings. So If we try to mount readonly then copy the filesystem
722 * from bdev, we could get stale data, so invalidate it to give a best
723 * effort at coherency.
725 if (remount_ro
&& sb
->s_bdev
)
726 invalidate_bdev(sb
->s_bdev
);
730 static void do_emergency_remount(struct work_struct
*work
)
732 struct super_block
*sb
, *p
= NULL
;
735 list_for_each_entry(sb
, &super_blocks
, s_list
) {
736 if (list_empty(&sb
->s_instances
))
739 spin_unlock(&sb_lock
);
740 down_write(&sb
->s_umount
);
741 if (sb
->s_root
&& sb
->s_bdev
&& !(sb
->s_flags
& MS_RDONLY
)) {
743 * What lock protects sb->s_flags??
745 do_remount_sb(sb
, MS_RDONLY
, NULL
, 1);
747 up_write(&sb
->s_umount
);
755 spin_unlock(&sb_lock
);
757 printk("Emergency Remount complete\n");
760 void emergency_remount(void)
762 struct work_struct
*work
;
764 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
766 INIT_WORK(work
, do_emergency_remount
);
772 * Unnamed block devices are dummy devices used by virtual
773 * filesystems which don't use real block-devices. -- jrs
776 static DEFINE_IDA(unnamed_dev_ida
);
777 static DEFINE_SPINLOCK(unnamed_dev_lock
);/* protects the above */
778 static int unnamed_dev_start
= 0; /* don't bother trying below it */
780 int get_anon_bdev(dev_t
*p
)
786 if (ida_pre_get(&unnamed_dev_ida
, GFP_ATOMIC
) == 0)
788 spin_lock(&unnamed_dev_lock
);
789 error
= ida_get_new_above(&unnamed_dev_ida
, unnamed_dev_start
, &dev
);
791 unnamed_dev_start
= dev
+ 1;
792 spin_unlock(&unnamed_dev_lock
);
793 if (error
== -EAGAIN
)
794 /* We raced and lost with another CPU. */
799 if ((dev
& MAX_ID_MASK
) == (1 << MINORBITS
)) {
800 spin_lock(&unnamed_dev_lock
);
801 ida_remove(&unnamed_dev_ida
, dev
);
802 if (unnamed_dev_start
> dev
)
803 unnamed_dev_start
= dev
;
804 spin_unlock(&unnamed_dev_lock
);
807 *p
= MKDEV(0, dev
& MINORMASK
);
810 EXPORT_SYMBOL(get_anon_bdev
);
812 void free_anon_bdev(dev_t dev
)
814 int slot
= MINOR(dev
);
815 spin_lock(&unnamed_dev_lock
);
816 ida_remove(&unnamed_dev_ida
, slot
);
817 if (slot
< unnamed_dev_start
)
818 unnamed_dev_start
= slot
;
819 spin_unlock(&unnamed_dev_lock
);
821 EXPORT_SYMBOL(free_anon_bdev
);
823 int set_anon_super(struct super_block
*s
, void *data
)
825 int error
= get_anon_bdev(&s
->s_dev
);
827 s
->s_bdi
= &noop_backing_dev_info
;
831 EXPORT_SYMBOL(set_anon_super
);
833 void kill_anon_super(struct super_block
*sb
)
835 dev_t dev
= sb
->s_dev
;
836 generic_shutdown_super(sb
);
840 EXPORT_SYMBOL(kill_anon_super
);
842 void kill_litter_super(struct super_block
*sb
)
845 d_genocide(sb
->s_root
);
849 EXPORT_SYMBOL(kill_litter_super
);
851 static int ns_test_super(struct super_block
*sb
, void *data
)
853 return sb
->s_fs_info
== data
;
856 static int ns_set_super(struct super_block
*sb
, void *data
)
858 sb
->s_fs_info
= data
;
859 return set_anon_super(sb
, NULL
);
862 struct dentry
*mount_ns(struct file_system_type
*fs_type
, int flags
,
863 void *data
, int (*fill_super
)(struct super_block
*, void *, int))
865 struct super_block
*sb
;
867 sb
= sget(fs_type
, ns_test_super
, ns_set_super
, data
);
874 err
= fill_super(sb
, data
, flags
& MS_SILENT
? 1 : 0);
876 deactivate_locked_super(sb
);
880 sb
->s_flags
|= MS_ACTIVE
;
883 return dget(sb
->s_root
);
886 EXPORT_SYMBOL(mount_ns
);
889 static int set_bdev_super(struct super_block
*s
, void *data
)
892 s
->s_dev
= s
->s_bdev
->bd_dev
;
895 * We set the bdi here to the queue backing, file systems can
896 * overwrite this in ->fill_super()
898 s
->s_bdi
= &bdev_get_queue(s
->s_bdev
)->backing_dev_info
;
902 static int test_bdev_super(struct super_block
*s
, void *data
)
904 return (void *)s
->s_bdev
== data
;
907 struct dentry
*mount_bdev(struct file_system_type
*fs_type
,
908 int flags
, const char *dev_name
, void *data
,
909 int (*fill_super
)(struct super_block
*, void *, int))
911 struct block_device
*bdev
;
912 struct super_block
*s
;
913 fmode_t mode
= FMODE_READ
| FMODE_EXCL
;
916 if (!(flags
& MS_RDONLY
))
919 bdev
= blkdev_get_by_path(dev_name
, mode
, fs_type
);
921 return ERR_CAST(bdev
);
924 * once the super is inserted into the list by sget, s_umount
925 * will protect the lockfs code from trying to start a snapshot
926 * while we are mounting
928 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
929 if (bdev
->bd_fsfreeze_count
> 0) {
930 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
934 s
= sget(fs_type
, test_bdev_super
, set_bdev_super
, bdev
);
935 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
940 if ((flags
^ s
->s_flags
) & MS_RDONLY
) {
941 deactivate_locked_super(s
);
947 * s_umount nests inside bd_mutex during
948 * __invalidate_device(). blkdev_put() acquires
949 * bd_mutex and can't be called under s_umount. Drop
950 * s_umount temporarily. This is safe as we're
951 * holding an active reference.
953 up_write(&s
->s_umount
);
954 blkdev_put(bdev
, mode
);
955 down_write(&s
->s_umount
);
957 char b
[BDEVNAME_SIZE
];
959 s
->s_flags
= flags
| MS_NOSEC
;
961 strlcpy(s
->s_id
, bdevname(bdev
, b
), sizeof(s
->s_id
));
962 sb_set_blocksize(s
, block_size(bdev
));
963 error
= fill_super(s
, data
, flags
& MS_SILENT
? 1 : 0);
965 deactivate_locked_super(s
);
969 s
->s_flags
|= MS_ACTIVE
;
973 return dget(s
->s_root
);
978 blkdev_put(bdev
, mode
);
980 return ERR_PTR(error
);
982 EXPORT_SYMBOL(mount_bdev
);
984 void kill_block_super(struct super_block
*sb
)
986 struct block_device
*bdev
= sb
->s_bdev
;
987 fmode_t mode
= sb
->s_mode
;
989 bdev
->bd_super
= NULL
;
990 generic_shutdown_super(sb
);
992 WARN_ON_ONCE(!(mode
& FMODE_EXCL
));
993 blkdev_put(bdev
, mode
| FMODE_EXCL
);
996 EXPORT_SYMBOL(kill_block_super
);
999 struct dentry
*mount_nodev(struct file_system_type
*fs_type
,
1000 int flags
, void *data
,
1001 int (*fill_super
)(struct super_block
*, void *, int))
1004 struct super_block
*s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
1011 error
= fill_super(s
, data
, flags
& MS_SILENT
? 1 : 0);
1013 deactivate_locked_super(s
);
1014 return ERR_PTR(error
);
1016 s
->s_flags
|= MS_ACTIVE
;
1017 return dget(s
->s_root
);
1019 EXPORT_SYMBOL(mount_nodev
);
1021 static int compare_single(struct super_block
*s
, void *p
)
1026 struct dentry
*mount_single(struct file_system_type
*fs_type
,
1027 int flags
, void *data
,
1028 int (*fill_super
)(struct super_block
*, void *, int))
1030 struct super_block
*s
;
1033 s
= sget(fs_type
, compare_single
, set_anon_super
, NULL
);
1038 error
= fill_super(s
, data
, flags
& MS_SILENT
? 1 : 0);
1040 deactivate_locked_super(s
);
1041 return ERR_PTR(error
);
1043 s
->s_flags
|= MS_ACTIVE
;
1045 do_remount_sb(s
, flags
, data
, 0);
1047 return dget(s
->s_root
);
1049 EXPORT_SYMBOL(mount_single
);
1052 mount_fs(struct file_system_type
*type
, int flags
, const char *name
, void *data
)
1054 struct dentry
*root
;
1055 struct super_block
*sb
;
1056 char *secdata
= NULL
;
1057 int error
= -ENOMEM
;
1059 if (data
&& !(type
->fs_flags
& FS_BINARY_MOUNTDATA
)) {
1060 secdata
= alloc_secdata();
1064 error
= security_sb_copy_data(data
, secdata
);
1066 goto out_free_secdata
;
1069 root
= type
->mount(type
, flags
, name
, data
);
1071 error
= PTR_ERR(root
);
1072 goto out_free_secdata
;
1076 WARN_ON(!sb
->s_bdi
);
1077 WARN_ON(sb
->s_bdi
== &default_backing_dev_info
);
1078 sb
->s_flags
|= MS_BORN
;
1080 error
= security_sb_kern_mount(sb
, flags
, secdata
);
1085 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
1086 * but s_maxbytes was an unsigned long long for many releases. Throw
1087 * this warning for a little while to try and catch filesystems that
1088 * violate this rule.
1090 WARN((sb
->s_maxbytes
< 0), "%s set sb->s_maxbytes to "
1091 "negative value (%lld)\n", type
->name
, sb
->s_maxbytes
);
1093 up_write(&sb
->s_umount
);
1094 free_secdata(secdata
);
1098 deactivate_locked_super(sb
);
1100 free_secdata(secdata
);
1102 return ERR_PTR(error
);
1106 * freeze_super - lock the filesystem and force it into a consistent state
1107 * @sb: the super to lock
1109 * Syncs the super to make sure the filesystem is consistent and calls the fs's
1110 * freeze_fs. Subsequent calls to this without first thawing the fs will return
1113 int freeze_super(struct super_block
*sb
)
1117 atomic_inc(&sb
->s_active
);
1118 down_write(&sb
->s_umount
);
1120 deactivate_locked_super(sb
);
1124 if (sb
->s_flags
& MS_RDONLY
) {
1125 sb
->s_frozen
= SB_FREEZE_TRANS
;
1127 up_write(&sb
->s_umount
);
1131 sb
->s_frozen
= SB_FREEZE_WRITE
;
1134 sync_filesystem(sb
);
1136 sb
->s_frozen
= SB_FREEZE_TRANS
;
1139 sync_blockdev(sb
->s_bdev
);
1140 if (sb
->s_op
->freeze_fs
) {
1141 ret
= sb
->s_op
->freeze_fs(sb
);
1144 "VFS:Filesystem freeze failed\n");
1145 sb
->s_frozen
= SB_UNFROZEN
;
1146 deactivate_locked_super(sb
);
1150 up_write(&sb
->s_umount
);
1153 EXPORT_SYMBOL(freeze_super
);
1156 * thaw_super -- unlock filesystem
1157 * @sb: the super to thaw
1159 * Unlocks the filesystem and marks it writeable again after freeze_super().
1161 int thaw_super(struct super_block
*sb
)
1165 down_write(&sb
->s_umount
);
1166 if (sb
->s_frozen
== SB_UNFROZEN
) {
1167 up_write(&sb
->s_umount
);
1171 if (sb
->s_flags
& MS_RDONLY
)
1174 if (sb
->s_op
->unfreeze_fs
) {
1175 error
= sb
->s_op
->unfreeze_fs(sb
);
1178 "VFS:Filesystem thaw failed\n");
1179 sb
->s_frozen
= SB_FREEZE_TRANS
;
1180 up_write(&sb
->s_umount
);
1186 sb
->s_frozen
= SB_UNFROZEN
;
1188 wake_up(&sb
->s_wait_unfrozen
);
1189 deactivate_locked_super(sb
);
1193 EXPORT_SYMBOL(thaw_super
);