12 #include <linux/export.h>
13 #include <linux/capability.h>
18 #include <linux/acct.h>
26 #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
27 #define HASH_SIZE (1UL << HASH_SHIFT)
33 static int mnt_id_start = 0;
34 static int mnt_group_start = 1;
62 #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
68 static int mnt_alloc_id(
struct mount *mnt)
74 spin_lock(&mnt_id_lock);
77 mnt_id_start = mnt->
mnt_id + 1;
78 spin_unlock(&mnt_id_lock);
85 static void mnt_free_id(
struct mount *mnt)
88 spin_lock(&mnt_id_lock);
90 if (mnt_id_start >
id)
92 spin_unlock(&mnt_id_lock);
100 static int mnt_alloc_group_id(
struct mount *mnt)
123 if (mnt_group_start >
id)
124 mnt_group_start =
id;
131 static inline void mnt_add_count(
struct mount *mnt,
int n)
148 unsigned int count = 0;
152 count +=
per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
161 static struct mount *alloc_vfsmnt(
const char *
name)
167 err = mnt_alloc_id(mnt);
180 goto out_free_devname;
196 #ifdef CONFIG_FSNOTIFY
242 static inline void mnt_inc_writers(
struct mount *mnt)
251 static inline void mnt_dec_writers(
struct mount *mnt)
260 static unsigned int mnt_get_writers(
struct mount *mnt)
263 unsigned int count = 0;
267 count +=
per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
276 static int mnt_is_readonly(
struct vfsmount *mnt)
278 if (mnt->
mnt_sb->s_readonly_remount)
303 struct mount *mnt = real_mount(m);
307 mnt_inc_writers(mnt);
322 if (mnt_is_readonly(m)) {
323 mnt_dec_writers(mnt);
344 sb_start_write(m->
mnt_sb);
370 mnt_inc_writers(real_mount(mnt));
385 struct inode *
inode = file->f_dentry->d_inode;
404 sb_start_write(file->
f_path.mnt->mnt_sb);
407 sb_end_write(file->
f_path.mnt->mnt_sb);
423 mnt_dec_writers(real_mount(mnt));
438 sb_end_write(mnt->
mnt_sb);
453 static int mnt_make_readonly(
struct mount *mnt)
481 if (mnt_get_writers(mnt) > 0)
495 static void __mnt_unmake_readonly(
struct mount *mnt)
516 if (mnt_get_writers(mnt) > 0) {
538 static void free_vfsmnt(
struct mount *mnt)
592 struct mount *child_mnt;
597 mnt_add_count(child_mnt, 1);
599 return &child_mnt->
mnt;
606 static inline int check_mnt(
struct mount *mnt)
649 spin_lock(&dentry->
d_lock);
651 spin_unlock(&dentry->
d_lock);
657 static void detach_mnt(
struct mount *mnt,
struct path *old_path)
665 dentry_reset_mounted(old_path->
dentry);
672 struct mount *child_mnt)
674 mnt_add_count(mnt, 1);
677 spin_lock(&dentry->
d_lock);
679 spin_unlock(&dentry->
d_lock);
696 static void commit_tree(
struct mount *mnt)
712 hash(&parent->mnt, mnt->mnt_mountpoint));
714 touch_mnt_namespace(n);
720 if (next == &p->mnt_mounts) {
724 next = p->mnt_child.
next;
725 if (next != &p->mnt_parent->mnt_mounts)
733 static struct mount *skip_mnt_tree(
struct mount *p)
752 mnt = alloc_vfsmnt(name);
759 root =
mount_fs(type, flags, name, data);
762 return ERR_CAST(root);
765 mnt->
mnt.mnt_root = root;
776 static struct mount *clone_mnt(
struct mount *old,
struct dentry *root,
793 err = mnt_alloc_group_id(mnt);
800 mnt->
mnt.mnt_sb =
sb;
801 mnt->
mnt.mnt_root = dget(root);
819 if (flag & CL_MAKE_SHARED)
836 static inline void mntfree(
struct mount *mnt)
852 fsnotify_vfsmount_delete(m);
858 static void mntput_no_expire(
struct mount *mnt)
865 mnt_add_count(mnt, -1);
872 mnt_add_count(mnt, -1);
878 mnt_add_count(mnt, -1);
899 struct mount *m = real_mount(mnt);
911 mnt_add_count(real_mount(mnt), 1);
919 real_mount(mnt)->mnt_pinned++;
926 struct mount *mnt = real_mount(m);
929 mnt_add_count(mnt, 1);
936 static inline void mangle(
struct seq_file *m,
const char *
s)
954 if (options !=
NULL && options[0]) {
995 #ifdef CONFIG_PROC_FS
997 static void *m_start(
struct seq_file *m, loff_t *
pos)
1005 static void *m_next(
struct seq_file *m,
void *
v, loff_t *
pos)
1012 static void m_stop(
struct seq_file *m,
void *
v)
1017 static int m_show(
struct seq_file *m,
void *
v)
1042 struct mount *mnt = real_mount(m);
1043 int actual_refs = 0;
1044 int minimum_refs = 0;
1050 for (p = mnt;
p; p = next_mnt(p, mnt)) {
1056 if (actual_refs > minimum_refs)
1094 while (!list_empty(head)) {
1097 if (mnt_has_parent(mnt)) {
1098 struct dentry *dentry;
1124 for (p = mnt;
p; p = next_mnt(p, mnt))
1125 list_move(&p->
mnt_hash, &tmp_list);
1133 __touch_mnt_namespace(p->
mnt_ns);
1136 if (mnt_has_parent(p)) {
1142 list_splice(&tmp_list, kill);
1145 static void shrink_submounts(
struct mount *mnt,
struct list_head *umounts);
1147 static int do_umount(
struct mount *mnt,
int flags)
1194 sb->
s_op->umount_begin(sb);
1222 if (!(flags & MNT_DETACH))
1223 shrink_submounts(mnt, &umount_list);
1250 int lookup_flags = 0;
1261 mnt = real_mount(path.
mnt);
1265 if (!check_mnt(mnt))
1272 retval = do_umount(mnt, flags);
1276 mntput_no_expire(mnt);
1281 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
1293 static int mount_is_safe(
struct path *path)
1320 res = q = clone_mnt(mnt, dentry, flag);
1332 for (s = r;
s; s = next_mnt(s, r)) {
1334 s = skip_mnt_tree(s);
1344 q = clone_mnt(p, p->
mnt.mnt_root, flag);
1349 attach_mnt(q, &path);
1394 int res =
f(root, arg);
1398 res =
f(&mnt->
mnt, arg);
1405 static void cleanup_group_ids(
struct mount *mnt,
struct mount *
end)
1409 for (p = mnt; p !=
end; p = next_mnt(p, mnt)) {
1415 static int invent_group_ids(
struct mount *mnt,
bool recurse)
1419 for (p = mnt;
p; p = recurse ? next_mnt(p, mnt) :
NULL) {
1421 int err = mnt_alloc_group_id(p);
1423 cleanup_group_ids(mnt, p);
1495 static int attach_recursive_mnt(
struct mount *source_mnt,
1496 struct path *path,
struct path *parent_path)
1499 struct mount *dest_mnt = real_mount(path->
mnt);
1500 struct dentry *dest_dentry = path->
dentry;
1505 err = invent_group_ids(source_mnt,
true);
1509 err =
propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1511 goto out_cleanup_ids;
1516 for (p = source_mnt;
p; p = next_mnt(p, source_mnt))
1520 detach_mnt(source_mnt, parent_path);
1521 attach_mnt(source_mnt, path);
1522 touch_mnt_namespace(source_mnt->
mnt_ns);
1525 commit_tree(source_mnt);
1538 cleanup_group_ids(source_mnt,
NULL);
1543 static int lock_mount(
struct path *path)
1564 static void unlock_mount(
struct path *path)
1570 static int graft_tree(
struct mount *mnt,
struct path *path)
1579 if (d_unlinked(path->
dentry))
1582 return attach_recursive_mnt(mnt, path,
NULL);
1589 static int flags_to_propagation_type(
int flags)
1605 static int do_change_type(
struct path *path,
int flag)
1608 struct mount *mnt = real_mount(path->
mnt);
1609 int recurse = flag &
MS_REC;
1616 if (path->
dentry != path->
mnt->mnt_root)
1619 type = flags_to_propagation_type(flag);
1625 err = invent_group_ids(mnt, recurse);
1631 for (m = mnt;
m; m = (recurse ? next_mnt(m, mnt) :
NULL))
1643 static int do_loopback(
struct path *path,
const char *old_name,
1647 struct path old_path;
1649 int err = mount_is_safe(path);
1652 if (!old_name || !*old_name)
1658 err = lock_mount(path);
1662 old = real_mount(old_path.mnt);
1668 if (!check_mnt(real_mount(path->
mnt)) || !check_mnt(old))
1672 mnt =
copy_tree(old, old_path.dentry, 0);
1674 mnt = clone_mnt(old, old_path.dentry, 0);
1681 err = graft_tree(mnt, path);
1695 static int change_mount_flags(
struct vfsmount *mnt,
int ms_flags)
1698 int readonly_request = 0;
1701 readonly_request = 1;
1705 if (readonly_request)
1706 error = mnt_make_readonly(real_mount(mnt));
1708 __mnt_unmake_readonly(real_mount(mnt));
1717 static int do_remount(
struct path *path,
int flags,
int mnt_flags,
1722 struct mount *mnt = real_mount(path->
mnt);
1727 if (!check_mnt(mnt))
1730 if (path->
dentry != path->
mnt->mnt_root)
1739 err = change_mount_flags(path->
mnt, flags);
1745 mnt->
mnt.mnt_flags = mnt_flags;
1751 touch_mnt_namespace(mnt->
mnt_ns);
1757 static inline int tree_contains_unbindable(
struct mount *mnt)
1760 for (p = mnt;
p; p = next_mnt(p, mnt)) {
1767 static int do_move_mount(
struct path *path,
const char *old_name)
1769 struct path old_path, parent_path;
1775 if (!old_name || !*old_name)
1781 err = lock_mount(path);
1785 old = real_mount(old_path.mnt);
1786 p = real_mount(path->
mnt);
1789 if (!check_mnt(p) || !check_mnt(old))
1792 if (d_unlinked(path->
dentry))
1796 if (old_path.dentry != old_path.mnt->mnt_root)
1799 if (!mnt_has_parent(old))
1803 S_ISDIR(old_path.dentry->d_inode->i_mode))
1821 err = attach_recursive_mnt(old, path, &parent_path);
1837 static struct vfsmount *fs_set_subtype(
struct vfsmount *mnt,
const char *fstype)
1851 if (!mnt->
mnt_sb->s_subtype)
1857 return ERR_PTR(err);
1861 do_kern_mount(
const char *fstype,
int flags,
const char *name,
void *data)
1870 mnt = fs_set_subtype(mnt, fstype);
1878 static int do_add_mount(
struct mount *newmnt,
struct path *path,
int mnt_flags)
1884 err = lock_mount(path);
1889 if (
unlikely(!check_mnt(real_mount(path->
mnt)))) {
1894 if (!real_mount(path->
mnt)->mnt_ns)
1900 if (path->
mnt->mnt_sb == newmnt->
mnt.mnt_sb &&
1905 if (
S_ISLNK(newmnt->
mnt.mnt_root->d_inode->i_mode))
1909 err = graft_tree(newmnt, path);
1920 static int do_new_mount(
struct path *path,
const char *type,
int flags,
1921 int mnt_flags,
const char *name,
void *data)
1933 mnt = do_kern_mount(type, flags, name, data);
1935 return PTR_ERR(mnt);
1937 err = do_add_mount(real_mount(mnt), path, mnt_flags);
1945 struct mount *mnt = real_mount(m);
1958 err = do_add_mount(mnt, path, path->
mnt->mnt_flags | MNT_SHRINKABLE);
2003 if (list_empty(mounts))
2021 while (!list_empty(&graveyard)) {
2023 touch_mnt_namespace(mnt->
mnt_ns);
2040 static int select_submounts(
struct mount *parent,
struct list_head *graveyard)
2042 struct mount *this_parent = parent;
2054 if (!(mnt->
mnt.mnt_flags & MNT_SHRINKABLE))
2072 if (this_parent != parent) {
2086 static void shrink_submounts(
struct mount *mnt,
struct list_head *umounts)
2092 while (select_submounts(mnt, &graveyard)) {
2093 while (!list_empty(&graveyard)) {
2096 touch_mnt_namespace(m->
mnt_ns);
2108 static long exact_copy_from_user(
void *to,
const void __user *
from,
2152 i = size - exact_copy_from_user((
void *)page, data, size);
2174 return PTR_ERR(tmp);
2194 long do_mount(
const char *dev_name,
const char *dir_name,
2195 const char *type_page,
unsigned long flags,
void *data_page)
2203 flags &= ~MS_MGC_MSK;
2219 type_page, flags, data_page);
2234 if (flags & MS_NOATIME)
2240 if (flags & MS_RDONLY)
2248 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
2250 else if (flags & MS_BIND)
2251 retval = do_loopback(&path, dev_name, flags & MS_REC);
2253 retval = do_change_type(&path, flags);
2255 retval = do_move_mount(&path, dev_name);
2257 retval = do_new_mount(&path, type_page, flags, mnt_flags,
2258 dev_name, data_page);
2273 INIT_LIST_HEAD(&new_ns->
list);
2292 new_ns = alloc_mnt_ns();
2302 return ERR_CAST(
new);
2319 if (&p->
mnt == fs->root.mnt) {
2323 if (&p->
mnt == fs->
pwd.mnt) {
2328 p = next_mnt(p, old);
2329 q = next_mnt(q,
new);
2352 new_ns = dup_mnt_ns(ns, new_fs);
2365 if (!IS_ERR(new_ns)) {
2366 struct mount *mnt = real_mount(m);
2383 ns = create_mnt_ns(mnt);
2385 return ERR_CAST(ns);
2393 return ERR_PTR(err);
2396 s = path.
mnt->mnt_sb;
2407 char __user *, type,
unsigned long, flags,
void __user *, data)
2413 unsigned long data_page;
2419 kernel_dir =
getname(dir_name);
2420 if (IS_ERR(kernel_dir)) {
2421 ret = PTR_ERR(kernel_dir);
2433 ret =
do_mount(kernel_dev, kernel_dir->
name, kernel_type, flags,
2434 (
void *) data_page);
2453 const struct path *root)
2455 while (&mnt->
mnt != root->
mnt && mnt_has_parent(mnt)) {
2498 const char __user *, put_old)
2500 struct path new, old, parent_path, root_parent, root;
2501 struct mount *new_mnt, *root_mnt;
2519 get_fs_root(
current->fs, &root);
2520 error = lock_mount(&old);
2525 new_mnt = real_mount(
new.mnt);
2526 root_mnt = real_mount(root.
mnt);
2531 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2534 if (d_unlinked(
new.dentry))
2536 if (d_unlinked(old.
dentry))
2539 if (
new.mnt == root.
mnt ||
2545 if (!mnt_has_parent(root_mnt))
2547 if (
new.mnt->mnt_root !=
new.dentry)
2549 if (!mnt_has_parent(new_mnt))
2555 detach_mnt(new_mnt, &parent_path);
2556 detach_mnt(root_mnt, &root_parent);
2558 attach_mnt(root_mnt, &old);
2560 attach_mnt(new_mnt, &root_parent);
2561 touch_mnt_namespace(
current->nsproxy->mnt_ns);
2581 static void __init init_mount_tree(
void)
2587 mnt = do_kern_mount(
"rootfs", 0,
"rootfs",
NULL);
2589 panic(
"Can't create rootfs");
2591 ns = create_mnt_ns(mnt);
2593 panic(
"Can't allocate initial namespace");
2599 root.dentry = mnt->mnt_root;
2617 if (!mount_hashtable)
2618 panic(
"Failed to allocate mount hash table\n");
2623 INIT_LIST_HEAD(&mount_hashtable[u]);
2671 if (!IS_ERR_OR_NULL(mnt)) {
2673 real_mount(mnt)->mnt_ns =
NULL;
2682 return check_mnt(real_mount(mnt));