blob: cc1375eff88c75abdf14aeb22a55c3d8b802895f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namespace.c
3 *
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
6 *
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/syscalls.h>
Al Virod10577a2011-12-07 13:06:11 -050012#include <linux/export.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080014#include <linux/mnt_namespace.h>
Eric W. Biederman771b1372012-07-26 21:08:32 -070015#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/namei.h>
17#include <linux/security.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010018#include <linux/cred.h>
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010019#include <linux/idr.h>
Rob Landley57f150a2013-09-11 14:26:10 -070020#include <linux/init.h> /* init_rootfs */
Al Virod10577a2011-12-07 13:06:11 -050021#include <linux/fs_struct.h> /* get_fs_root et.al. */
22#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
23#include <linux/uaccess.h>
David Howells0bb80f22013-04-12 01:50:06 +010024#include <linux/proc_ns.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070025#include <linux/magic.h>
Al Viro0818bf22014-02-28 13:46:44 -050026#include <linux/bootmem.h>
Al Viro9ea459e2014-08-08 13:08:20 -040027#include <linux/task_work.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010028#include <linux/sched/task.h>
29
Ram Pai07b20882005-11-07 17:19:07 -050030#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070031#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Eric W. Biedermand2921682016-09-28 00:27:17 -050033/* Maximum number of mounts in a mount namespace */
34unsigned int sysctl_mount_max __read_mostly = 100000;
35
Al Viro0818bf22014-02-28 13:46:44 -050036static unsigned int m_hash_mask __read_mostly;
37static unsigned int m_hash_shift __read_mostly;
38static unsigned int mp_hash_mask __read_mostly;
39static unsigned int mp_hash_shift __read_mostly;
40
41static __initdata unsigned long mhash_entries;
42static int __init set_mhash_entries(char *str)
43{
44 if (!str)
45 return 0;
46 mhash_entries = simple_strtoul(str, &str, 0);
47 return 1;
48}
49__setup("mhash_entries=", set_mhash_entries);
50
51static __initdata unsigned long mphash_entries;
52static int __init set_mphash_entries(char *str)
53{
54 if (!str)
55 return 0;
56 mphash_entries = simple_strtoul(str, &str, 0);
57 return 1;
58}
59__setup("mphash_entries=", set_mphash_entries);
Eric Dumazet13f14b42008-02-06 01:37:57 -080060
Al Viroc7999c32014-02-27 14:40:10 -050061static u64 event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010062static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010063static DEFINE_IDA(mnt_group_ida);
Nick Piggin99b7db72010-08-18 04:37:39 +100064static DEFINE_SPINLOCK(mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040065static int mnt_id_start = 0;
66static int mnt_group_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Al Viro38129a12014-03-20 21:10:51 -040068static struct hlist_head *mount_hashtable __read_mostly;
Al Viro0818bf22014-02-28 13:46:44 -050069static struct hlist_head *mountpoint_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *mnt_cache __read_mostly;
Al Viro59aa0da2013-09-16 21:34:53 -040071static DECLARE_RWSEM(namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080073/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060074struct kobject *fs_kobj;
75EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080076
Nick Piggin99b7db72010-08-18 04:37:39 +100077/*
78 * vfsmount lock may be taken for read to prevent changes to the
79 * vfsmount hash, ie. during mountpoint lookups or walking back
80 * up the tree.
81 *
82 * It should be taken for write in all cases where the vfsmount
83 * tree or hash is modified or when a vfsmount structure is modified.
84 */
Al Viro48a066e2013-09-29 22:06:07 -040085__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +100086
Al Viro38129a12014-03-20 21:10:51 -040087static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Ram Paib58fed82005-11-07 17:16:09 -050089 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
90 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Al Viro0818bf22014-02-28 13:46:44 -050091 tmp = tmp + (tmp >> m_hash_shift);
92 return &mount_hashtable[tmp & m_hash_mask];
93}
94
95static inline struct hlist_head *mp_hash(struct dentry *dentry)
96{
97 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
98 tmp = tmp + (tmp >> mp_hash_shift);
99 return &mountpoint_hashtable[tmp & mp_hash_mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Al Virob105e272011-11-24 20:38:33 -0500102static int mnt_alloc_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100103{
104 int res;
105
106retry:
107 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
Nick Piggin99b7db72010-08-18 04:37:39 +1000108 spin_lock(&mnt_id_lock);
Al Viro15169fe2011-11-25 00:50:41 -0500109 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
Al Virof21f6222009-06-24 03:12:00 -0400110 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -0500111 mnt_id_start = mnt->mnt_id + 1;
Nick Piggin99b7db72010-08-18 04:37:39 +1000112 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100113 if (res == -EAGAIN)
114 goto retry;
115
116 return res;
117}
118
Al Virob105e272011-11-24 20:38:33 -0500119static void mnt_free_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100120{
Al Viro15169fe2011-11-25 00:50:41 -0500121 int id = mnt->mnt_id;
Nick Piggin99b7db72010-08-18 04:37:39 +1000122 spin_lock(&mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -0400123 ida_remove(&mnt_id_ida, id);
124 if (mnt_id_start > id)
125 mnt_id_start = id;
Nick Piggin99b7db72010-08-18 04:37:39 +1000126 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100127}
128
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100129/*
130 * Allocate a new peer group ID
131 *
132 * mnt_group_ida is protected by namespace_sem
133 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500134static int mnt_alloc_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100135{
Al Virof21f6222009-06-24 03:12:00 -0400136 int res;
137
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100138 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
139 return -ENOMEM;
140
Al Virof21f6222009-06-24 03:12:00 -0400141 res = ida_get_new_above(&mnt_group_ida,
142 mnt_group_start,
Al Viro15169fe2011-11-25 00:50:41 -0500143 &mnt->mnt_group_id);
Al Virof21f6222009-06-24 03:12:00 -0400144 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -0500145 mnt_group_start = mnt->mnt_group_id + 1;
Al Virof21f6222009-06-24 03:12:00 -0400146
147 return res;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100148}
149
150/*
151 * Release a peer group ID
152 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500153void mnt_release_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100154{
Al Viro15169fe2011-11-25 00:50:41 -0500155 int id = mnt->mnt_group_id;
Al Virof21f6222009-06-24 03:12:00 -0400156 ida_remove(&mnt_group_ida, id);
157 if (mnt_group_start > id)
158 mnt_group_start = id;
Al Viro15169fe2011-11-25 00:50:41 -0500159 mnt->mnt_group_id = 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100160}
161
Nick Pigginb3e19d92011-01-07 17:50:11 +1100162/*
163 * vfsmount lock must be held for read
164 */
Al Viro83adc752011-11-24 22:37:54 -0500165static inline void mnt_add_count(struct mount *mnt, int n)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100166{
167#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500168 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100169#else
170 preempt_disable();
Al Viro68e8a9f2011-11-24 22:53:09 -0500171 mnt->mnt_count += n;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100172 preempt_enable();
173#endif
174}
175
Nick Pigginb3e19d92011-01-07 17:50:11 +1100176/*
177 * vfsmount lock must be held for write
178 */
Al Viro83adc752011-11-24 22:37:54 -0500179unsigned int mnt_get_count(struct mount *mnt)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100180{
181#ifdef CONFIG_SMP
Al Virof03c6592011-01-14 22:30:21 -0500182 unsigned int count = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100183 int cpu;
184
185 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500186 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100187 }
188
189 return count;
190#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500191 return mnt->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100192#endif
193}
194
Al Viro87b95ce2015-01-10 19:01:08 -0500195static void drop_mountpoint(struct fs_pin *p)
196{
197 struct mount *m = container_of(p, struct mount, mnt_umount);
198 dput(m->mnt_ex_mountpoint);
199 pin_remove(p);
200 mntput(&m->mnt);
201}
202
Al Virob105e272011-11-24 20:38:33 -0500203static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Al Viroc63181e2011-11-25 02:35:16 -0500205 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
206 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100207 int err;
208
Al Viroc63181e2011-11-25 02:35:16 -0500209 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800210 if (err)
211 goto out_free_cache;
212
213 if (name) {
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800214 mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
Al Viroc63181e2011-11-25 02:35:16 -0500215 if (!mnt->mnt_devname)
Li Zefan88b38782008-07-21 18:06:36 +0800216 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100217 }
218
Nick Pigginb3e19d92011-01-07 17:50:11 +1100219#ifdef CONFIG_SMP
Al Viroc63181e2011-11-25 02:35:16 -0500220 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
221 if (!mnt->mnt_pcp)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100222 goto out_free_devname;
223
Al Viroc63181e2011-11-25 02:35:16 -0500224 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100225#else
Al Viroc63181e2011-11-25 02:35:16 -0500226 mnt->mnt_count = 1;
227 mnt->mnt_writers = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100228#endif
229
Al Viro38129a12014-03-20 21:10:51 -0400230 INIT_HLIST_NODE(&mnt->mnt_hash);
Al Viroc63181e2011-11-25 02:35:16 -0500231 INIT_LIST_HEAD(&mnt->mnt_child);
232 INIT_LIST_HEAD(&mnt->mnt_mounts);
233 INIT_LIST_HEAD(&mnt->mnt_list);
234 INIT_LIST_HEAD(&mnt->mnt_expire);
235 INIT_LIST_HEAD(&mnt->mnt_share);
236 INIT_LIST_HEAD(&mnt->mnt_slave_list);
237 INIT_LIST_HEAD(&mnt->mnt_slave);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700238 INIT_HLIST_NODE(&mnt->mnt_mp_list);
Andreas Gruenbacher2504c5d2009-12-17 21:24:27 -0500239#ifdef CONFIG_FSNOTIFY
240 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
241#endif
Al Viro87b95ce2015-01-10 19:01:08 -0500242 init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Al Viroc63181e2011-11-25 02:35:16 -0500244 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800245
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000246#ifdef CONFIG_SMP
247out_free_devname:
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800248 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000249#endif
Li Zefan88b38782008-07-21 18:06:36 +0800250out_free_id:
Al Viroc63181e2011-11-25 02:35:16 -0500251 mnt_free_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800252out_free_cache:
Al Viroc63181e2011-11-25 02:35:16 -0500253 kmem_cache_free(mnt_cache, mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800254 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Dave Hansen83660252008-02-15 14:37:30 -0800257/*
258 * Most r/o checks on a fs are for operations that take
259 * discrete amounts of time, like a write() or unlink().
260 * We must keep track of when those operations start
261 * (for permission checks) and when they end, so that
262 * we can determine when writes are able to occur to
263 * a filesystem.
264 */
Dave Hansen3d733632008-02-15 14:37:59 -0800265/*
266 * __mnt_is_readonly: check whether a mount is read-only
267 * @mnt: the mount to check for its write status
268 *
269 * This shouldn't be used directly ouside of the VFS.
270 * It does not guarantee that the filesystem will stay
271 * r/w, just that it is right *now*. This can not and
272 * should not be used in place of IS_RDONLY(inode).
273 * mnt_want/drop_write() will _keep_ the filesystem
274 * r/w.
275 */
276int __mnt_is_readonly(struct vfsmount *mnt)
277{
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800278 if (mnt->mnt_flags & MNT_READONLY)
279 return 1;
280 if (mnt->mnt_sb->s_flags & MS_RDONLY)
281 return 1;
282 return 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800283}
284EXPORT_SYMBOL_GPL(__mnt_is_readonly);
285
Al Viro83adc752011-11-24 22:37:54 -0500286static inline void mnt_inc_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800287{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000288#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500289 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000290#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500291 mnt->mnt_writers++;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000292#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800293}
Dave Hansen3d733632008-02-15 14:37:59 -0800294
Al Viro83adc752011-11-24 22:37:54 -0500295static inline void mnt_dec_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800296{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000297#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500298 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000299#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500300 mnt->mnt_writers--;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000301#endif
302}
303
Al Viro83adc752011-11-24 22:37:54 -0500304static unsigned int mnt_get_writers(struct mount *mnt)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000305{
306#ifdef CONFIG_SMP
307 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800308 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800309
310 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500311 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
Dave Hansen3d733632008-02-15 14:37:59 -0800312 }
Dave Hansen3d733632008-02-15 14:37:59 -0800313
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000314 return count;
315#else
316 return mnt->mnt_writers;
317#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800318}
319
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100320static int mnt_is_readonly(struct vfsmount *mnt)
321{
322 if (mnt->mnt_sb->s_readonly_remount)
323 return 1;
324 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
325 smp_rmb();
326 return __mnt_is_readonly(mnt);
327}
328
Dave Hansen3d733632008-02-15 14:37:59 -0800329/*
Jan Karaeb04c282012-06-12 16:20:35 +0200330 * Most r/o & frozen checks on a fs are for operations that take discrete
331 * amounts of time, like a write() or unlink(). We must keep track of when
332 * those operations start (for permission checks) and when they end, so that we
333 * can determine when writes are able to occur to a filesystem.
Dave Hansen3d733632008-02-15 14:37:59 -0800334 */
Dave Hansen83660252008-02-15 14:37:30 -0800335/**
Jan Karaeb04c282012-06-12 16:20:35 +0200336 * __mnt_want_write - get write access to a mount without freeze protection
Al Viro83adc752011-11-24 22:37:54 -0500337 * @m: the mount on which to take a write
Dave Hansen83660252008-02-15 14:37:30 -0800338 *
Jan Karaeb04c282012-06-12 16:20:35 +0200339 * This tells the low-level filesystem that a write is about to be performed to
340 * it, and makes sure that writes are allowed (mnt it read-write) before
341 * returning success. This operation does not protect against filesystem being
342 * frozen. When the write operation is finished, __mnt_drop_write() must be
343 * called. This is effectively a refcount.
Dave Hansen83660252008-02-15 14:37:30 -0800344 */
Jan Karaeb04c282012-06-12 16:20:35 +0200345int __mnt_want_write(struct vfsmount *m)
Dave Hansen83660252008-02-15 14:37:30 -0800346{
Al Viro83adc752011-11-24 22:37:54 -0500347 struct mount *mnt = real_mount(m);
Dave Hansen3d733632008-02-15 14:37:59 -0800348 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800349
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000350 preempt_disable();
Nick Pigginc6653a82011-01-07 17:50:10 +1100351 mnt_inc_writers(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000352 /*
Nick Pigginc6653a82011-01-07 17:50:10 +1100353 * The store to mnt_inc_writers must be visible before we pass
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000354 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
355 * incremented count after it has set MNT_WRITE_HOLD.
356 */
357 smp_mb();
Miao Xie1e755292012-11-16 17:23:50 +0800358 while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000359 cpu_relax();
360 /*
361 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
362 * be set to match its requirements. So we must not load that until
363 * MNT_WRITE_HOLD is cleared.
364 */
365 smp_rmb();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100366 if (mnt_is_readonly(m)) {
Nick Pigginc6653a82011-01-07 17:50:10 +1100367 mnt_dec_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800368 ret = -EROFS;
Dave Hansen3d733632008-02-15 14:37:59 -0800369 }
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000370 preempt_enable();
Jan Karaeb04c282012-06-12 16:20:35 +0200371
372 return ret;
373}
374
375/**
376 * mnt_want_write - get write access to a mount
377 * @m: the mount on which to take a write
378 *
379 * This tells the low-level filesystem that a write is about to be performed to
380 * it, and makes sure that writes are allowed (mount is read-write, filesystem
381 * is not frozen) before returning success. When the write operation is
382 * finished, mnt_drop_write() must be called. This is effectively a refcount.
383 */
384int mnt_want_write(struct vfsmount *m)
385{
386 int ret;
387
388 sb_start_write(m->mnt_sb);
389 ret = __mnt_want_write(m);
390 if (ret)
391 sb_end_write(m->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800392 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800393}
394EXPORT_SYMBOL_GPL(mnt_want_write);
395
396/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000397 * mnt_clone_write - get write access to a mount
398 * @mnt: the mount on which to take a write
399 *
400 * This is effectively like mnt_want_write, except
401 * it must only be used to take an extra write reference
402 * on a mountpoint that we already know has a write reference
403 * on it. This allows some optimisation.
404 *
405 * After finished, mnt_drop_write must be called as usual to
406 * drop the reference.
407 */
408int mnt_clone_write(struct vfsmount *mnt)
409{
410 /* superblock may be r/o */
411 if (__mnt_is_readonly(mnt))
412 return -EROFS;
413 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500414 mnt_inc_writers(real_mount(mnt));
npiggin@suse.de96029c42009-04-26 20:25:55 +1000415 preempt_enable();
416 return 0;
417}
418EXPORT_SYMBOL_GPL(mnt_clone_write);
419
420/**
Jan Karaeb04c282012-06-12 16:20:35 +0200421 * __mnt_want_write_file - get write access to a file's mount
422 * @file: the file who's mount on which to take a write
423 *
424 * This is like __mnt_want_write, but it takes a file and can
425 * do some optimisations if the file is open for write already
426 */
427int __mnt_want_write_file(struct file *file)
428{
Al Viro83f936c2014-03-14 12:02:47 -0400429 if (!(file->f_mode & FMODE_WRITER))
Jan Karaeb04c282012-06-12 16:20:35 +0200430 return __mnt_want_write(file->f_path.mnt);
431 else
432 return mnt_clone_write(file->f_path.mnt);
433}
434
435/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000436 * mnt_want_write_file - get write access to a file's mount
437 * @file: the file who's mount on which to take a write
438 *
439 * This is like mnt_want_write, but it takes a file and can
440 * do some optimisations if the file is open for write already
441 */
442int mnt_want_write_file(struct file *file)
443{
Jan Karaeb04c282012-06-12 16:20:35 +0200444 int ret;
445
446 sb_start_write(file->f_path.mnt->mnt_sb);
447 ret = __mnt_want_write_file(file);
448 if (ret)
449 sb_end_write(file->f_path.mnt->mnt_sb);
450 return ret;
npiggin@suse.de96029c42009-04-26 20:25:55 +1000451}
452EXPORT_SYMBOL_GPL(mnt_want_write_file);
453
454/**
Jan Karaeb04c282012-06-12 16:20:35 +0200455 * __mnt_drop_write - give up write access to a mount
Dave Hansen83660252008-02-15 14:37:30 -0800456 * @mnt: the mount on which to give up write access
457 *
458 * Tells the low-level filesystem that we are done
459 * performing writes to it. Must be matched with
Jan Karaeb04c282012-06-12 16:20:35 +0200460 * __mnt_want_write() call above.
Dave Hansen83660252008-02-15 14:37:30 -0800461 */
Jan Karaeb04c282012-06-12 16:20:35 +0200462void __mnt_drop_write(struct vfsmount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800463{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000464 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500465 mnt_dec_writers(real_mount(mnt));
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000466 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800467}
Jan Karaeb04c282012-06-12 16:20:35 +0200468
469/**
470 * mnt_drop_write - give up write access to a mount
471 * @mnt: the mount on which to give up write access
472 *
473 * Tells the low-level filesystem that we are done performing writes to it and
474 * also allows filesystem to be frozen again. Must be matched with
475 * mnt_want_write() call above.
476 */
477void mnt_drop_write(struct vfsmount *mnt)
478{
479 __mnt_drop_write(mnt);
480 sb_end_write(mnt->mnt_sb);
481}
Dave Hansen83660252008-02-15 14:37:30 -0800482EXPORT_SYMBOL_GPL(mnt_drop_write);
483
Jan Karaeb04c282012-06-12 16:20:35 +0200484void __mnt_drop_write_file(struct file *file)
485{
486 __mnt_drop_write(file->f_path.mnt);
487}
488
Al Viro2a79f172011-12-09 08:06:57 -0500489void mnt_drop_write_file(struct file *file)
490{
491 mnt_drop_write(file->f_path.mnt);
492}
493EXPORT_SYMBOL(mnt_drop_write_file);
494
Al Viro83adc752011-11-24 22:37:54 -0500495static int mnt_make_readonly(struct mount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800496{
Dave Hansen3d733632008-02-15 14:37:59 -0800497 int ret = 0;
498
Al Viro719ea2f2013-09-29 11:24:49 -0400499 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -0500500 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000501 /*
502 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
503 * should be visible before we do.
504 */
505 smp_mb();
506
507 /*
508 * With writers on hold, if this value is zero, then there are
509 * definitely no active writers (although held writers may subsequently
510 * increment the count, they'll have to wait, and decrement it after
511 * seeing MNT_READONLY).
512 *
513 * It is OK to have counter incremented on one CPU and decremented on
514 * another: the sum will add up correctly. The danger would be when we
515 * sum up each counter, if we read a counter before it is incremented,
516 * but then read another CPU's count which it has been subsequently
517 * decremented from -- we would see more decrements than we should.
518 * MNT_WRITE_HOLD protects against this scenario, because
519 * mnt_want_write first increments count, then smp_mb, then spins on
520 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
521 * we're counting up here.
522 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100523 if (mnt_get_writers(mnt) > 0)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000524 ret = -EBUSY;
525 else
Al Viro83adc752011-11-24 22:37:54 -0500526 mnt->mnt.mnt_flags |= MNT_READONLY;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000527 /*
528 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
529 * that become unheld will see MNT_READONLY.
530 */
531 smp_wmb();
Al Viro83adc752011-11-24 22:37:54 -0500532 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
Al Viro719ea2f2013-09-29 11:24:49 -0400533 unlock_mount_hash();
Dave Hansen3d733632008-02-15 14:37:59 -0800534 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800535}
Dave Hansen83660252008-02-15 14:37:30 -0800536
Al Viro83adc752011-11-24 22:37:54 -0500537static void __mnt_unmake_readonly(struct mount *mnt)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800538{
Al Viro719ea2f2013-09-29 11:24:49 -0400539 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -0500540 mnt->mnt.mnt_flags &= ~MNT_READONLY;
Al Viro719ea2f2013-09-29 11:24:49 -0400541 unlock_mount_hash();
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800542}
543
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100544int sb_prepare_remount_readonly(struct super_block *sb)
545{
546 struct mount *mnt;
547 int err = 0;
548
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100549 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
550 if (atomic_long_read(&sb->s_remove_count))
551 return -EBUSY;
552
Al Viro719ea2f2013-09-29 11:24:49 -0400553 lock_mount_hash();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100554 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
555 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
556 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
557 smp_mb();
558 if (mnt_get_writers(mnt) > 0) {
559 err = -EBUSY;
560 break;
561 }
562 }
563 }
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100564 if (!err && atomic_long_read(&sb->s_remove_count))
565 err = -EBUSY;
566
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100567 if (!err) {
568 sb->s_readonly_remount = 1;
569 smp_wmb();
570 }
571 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
572 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
573 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
574 }
Al Viro719ea2f2013-09-29 11:24:49 -0400575 unlock_mount_hash();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100576
577 return err;
578}
579
Al Virob105e272011-11-24 20:38:33 -0500580static void free_vfsmnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800582 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000583#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500584 free_percpu(mnt->mnt_pcp);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000585#endif
Al Virob105e272011-11-24 20:38:33 -0500586 kmem_cache_free(mnt_cache, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
David Howells8ffcb322014-01-24 12:17:54 +0000589static void delayed_free_vfsmnt(struct rcu_head *head)
590{
591 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
592}
593
Al Viro48a066e2013-09-29 22:06:07 -0400594/* call under rcu_read_lock */
Al Viro294d71f2015-05-08 11:43:53 -0400595int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
Al Viro48a066e2013-09-29 22:06:07 -0400596{
597 struct mount *mnt;
598 if (read_seqretry(&mount_lock, seq))
Al Viro294d71f2015-05-08 11:43:53 -0400599 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400600 if (bastard == NULL)
Al Viro294d71f2015-05-08 11:43:53 -0400601 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400602 mnt = real_mount(bastard);
603 mnt_add_count(mnt, 1);
604 if (likely(!read_seqretry(&mount_lock, seq)))
Al Viro294d71f2015-05-08 11:43:53 -0400605 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400606 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
607 mnt_add_count(mnt, -1);
Al Viro294d71f2015-05-08 11:43:53 -0400608 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400609 }
Al Viro294d71f2015-05-08 11:43:53 -0400610 return -1;
611}
612
613/* call under rcu_read_lock */
614bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
615{
616 int res = __legitimize_mnt(bastard, seq);
617 if (likely(!res))
618 return true;
619 if (unlikely(res < 0)) {
620 rcu_read_unlock();
621 mntput(bastard);
622 rcu_read_lock();
623 }
Al Viro48a066e2013-09-29 22:06:07 -0400624 return false;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/*
Al Viro474279d2013-10-01 16:11:26 -0400628 * find the first mount at @dentry on vfsmount @mnt.
Al Viro48a066e2013-09-29 22:06:07 -0400629 * call under rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Al Viro474279d2013-10-01 16:11:26 -0400631struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Al Viro38129a12014-03-20 21:10:51 -0400633 struct hlist_head *head = m_hash(mnt, dentry);
Al Viro474279d2013-10-01 16:11:26 -0400634 struct mount *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Al Viro38129a12014-03-20 21:10:51 -0400636 hlist_for_each_entry_rcu(p, head, mnt_hash)
Al Viro474279d2013-10-01 16:11:26 -0400637 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
638 return p;
639 return NULL;
640}
641
642/*
David Howellsf015f1262012-06-25 12:55:28 +0100643 * lookup_mnt - Return the first child mount mounted at path
644 *
645 * "First" means first mounted chronologically. If you create the
646 * following mounts:
647 *
648 * mount /dev/sda1 /mnt
649 * mount /dev/sda2 /mnt
650 * mount /dev/sda3 /mnt
651 *
652 * Then lookup_mnt() on the base /mnt dentry in the root mount will
653 * return successively the root dentry and vfsmount of /dev/sda1, then
654 * /dev/sda2, then /dev/sda3, then NULL.
655 *
656 * lookup_mnt takes a reference to the found vfsmount.
Ram Paia05964f2005-11-07 17:20:17 -0500657 */
Al Viroca71cf72016-11-20 19:45:28 -0500658struct vfsmount *lookup_mnt(const struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500659{
Al Viroc7105362011-11-24 18:22:03 -0500660 struct mount *child_mnt;
Al Viro48a066e2013-09-29 22:06:07 -0400661 struct vfsmount *m;
662 unsigned seq;
Nick Piggin99b7db72010-08-18 04:37:39 +1000663
Al Viro48a066e2013-09-29 22:06:07 -0400664 rcu_read_lock();
665 do {
666 seq = read_seqbegin(&mount_lock);
667 child_mnt = __lookup_mnt(path->mnt, path->dentry);
668 m = child_mnt ? &child_mnt->mnt : NULL;
669 } while (!legitimize_mnt(m, seq));
670 rcu_read_unlock();
671 return m;
Ram Paia05964f2005-11-07 17:20:17 -0500672}
673
Eric W. Biederman7af13642013-10-04 19:15:13 -0700674/*
675 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
676 * current mount namespace.
677 *
678 * The common case is dentries are not mountpoints at all and that
679 * test is handled inline. For the slow case when we are actually
680 * dealing with a mountpoint of some kind, walk through all of the
681 * mounts in the current mount namespace and test to see if the dentry
682 * is a mountpoint.
683 *
684 * The mount_hashtable is not usable in the context because we
685 * need to identify all mounts that may be in the current mount
686 * namespace not just a mount that happens to have some specified
687 * parent mount.
688 */
689bool __is_local_mountpoint(struct dentry *dentry)
690{
691 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
692 struct mount *mnt;
693 bool is_covered = false;
694
695 if (!d_mountpoint(dentry))
696 goto out;
697
698 down_read(&namespace_sem);
699 list_for_each_entry(mnt, &ns->list, mnt_list) {
700 is_covered = (mnt->mnt_mountpoint == dentry);
701 if (is_covered)
702 break;
703 }
704 up_read(&namespace_sem);
705out:
706 return is_covered;
707}
708
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800709static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
Al Viro84d17192013-03-15 10:53:28 -0400710{
Al Viro0818bf22014-02-28 13:46:44 -0500711 struct hlist_head *chain = mp_hash(dentry);
Al Viro84d17192013-03-15 10:53:28 -0400712 struct mountpoint *mp;
713
Al Viro0818bf22014-02-28 13:46:44 -0500714 hlist_for_each_entry(mp, chain, m_hash) {
Al Viro84d17192013-03-15 10:53:28 -0400715 if (mp->m_dentry == dentry) {
716 /* might be worth a WARN_ON() */
717 if (d_unlinked(dentry))
718 return ERR_PTR(-ENOENT);
719 mp->m_count++;
720 return mp;
721 }
722 }
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800723 return NULL;
724}
725
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300726static struct mountpoint *get_mountpoint(struct dentry *dentry)
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800727{
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300728 struct mountpoint *mp, *new = NULL;
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800729 int ret;
Al Viro84d17192013-03-15 10:53:28 -0400730
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300731 if (d_mountpoint(dentry)) {
732mountpoint:
733 read_seqlock_excl(&mount_lock);
734 mp = lookup_mountpoint(dentry);
735 read_sequnlock_excl(&mount_lock);
736 if (mp)
737 goto done;
Al Viro84d17192013-03-15 10:53:28 -0400738 }
Miklos Szeredieed81002013-09-05 14:39:11 +0200739
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300740 if (!new)
741 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
742 if (!new)
743 return ERR_PTR(-ENOMEM);
744
745
746 /* Exactly one processes may set d_mounted */
747 ret = d_set_mounted(dentry);
748
749 /* Someone else set d_mounted? */
750 if (ret == -EBUSY)
751 goto mountpoint;
752
753 /* The dentry is not available as a mountpoint? */
754 mp = ERR_PTR(ret);
755 if (ret)
756 goto done;
757
758 /* Add the new mountpoint to the hash table */
759 read_seqlock_excl(&mount_lock);
760 new->m_dentry = dentry;
761 new->m_count = 1;
762 hlist_add_head(&new->m_hash, mp_hash(dentry));
763 INIT_HLIST_HEAD(&new->m_list);
764 read_sequnlock_excl(&mount_lock);
765
766 mp = new;
767 new = NULL;
768done:
769 kfree(new);
Al Viro84d17192013-03-15 10:53:28 -0400770 return mp;
771}
772
773static void put_mountpoint(struct mountpoint *mp)
774{
775 if (!--mp->m_count) {
776 struct dentry *dentry = mp->m_dentry;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700777 BUG_ON(!hlist_empty(&mp->m_list));
Al Viro84d17192013-03-15 10:53:28 -0400778 spin_lock(&dentry->d_lock);
779 dentry->d_flags &= ~DCACHE_MOUNTED;
780 spin_unlock(&dentry->d_lock);
Al Viro0818bf22014-02-28 13:46:44 -0500781 hlist_del(&mp->m_hash);
Al Viro84d17192013-03-15 10:53:28 -0400782 kfree(mp);
783 }
784}
785
Al Viro143c8c92011-11-25 00:46:35 -0500786static inline int check_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800788 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Nick Piggin99b7db72010-08-18 04:37:39 +1000791/*
792 * vfsmount lock must be held for write
793 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800794static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500795{
796 if (ns) {
797 ns->event = ++event;
798 wake_up_interruptible(&ns->poll);
799 }
800}
801
Nick Piggin99b7db72010-08-18 04:37:39 +1000802/*
803 * vfsmount lock must be held for write
804 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800805static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500806{
807 if (ns && ns->event != event) {
808 ns->event = event;
809 wake_up_interruptible(&ns->poll);
810 }
811}
812
Nick Piggin99b7db72010-08-18 04:37:39 +1000813/*
814 * vfsmount lock must be held for write
815 */
Eric W. Biederman7bdb11de2014-12-29 13:03:41 -0600816static void unhash_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Al Viro0714a532011-11-24 22:19:58 -0500818 mnt->mnt_parent = mnt;
Al Viroa73324d2011-11-24 22:25:07 -0500819 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro6b41d532011-11-24 23:24:33 -0500820 list_del_init(&mnt->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -0400821 hlist_del_init_rcu(&mnt->mnt_hash);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700822 hlist_del_init(&mnt->mnt_mp_list);
Al Viro84d17192013-03-15 10:53:28 -0400823 put_mountpoint(mnt->mnt_mp);
824 mnt->mnt_mp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Nick Piggin99b7db72010-08-18 04:37:39 +1000827/*
828 * vfsmount lock must be held for write
829 */
Eric W. Biederman7bdb11de2014-12-29 13:03:41 -0600830static void detach_mnt(struct mount *mnt, struct path *old_path)
831{
832 old_path->dentry = mnt->mnt_mountpoint;
833 old_path->mnt = &mnt->mnt_parent->mnt;
834 unhash_mnt(mnt);
835}
836
837/*
838 * vfsmount lock must be held for write
839 */
Eric W. Biederman6a46c572015-01-15 22:58:33 -0600840static void umount_mnt(struct mount *mnt)
841{
842 /* old mountpoint will be dropped when we can do that */
843 mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
844 unhash_mnt(mnt);
845}
846
847/*
848 * vfsmount lock must be held for write
849 */
Al Viro84d17192013-03-15 10:53:28 -0400850void mnt_set_mountpoint(struct mount *mnt,
851 struct mountpoint *mp,
Al Viro44d964d62011-11-24 21:28:22 -0500852 struct mount *child_mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500853{
Al Viro84d17192013-03-15 10:53:28 -0400854 mp->m_count++;
Al Viro3a2393d2011-11-25 03:19:09 -0500855 mnt_add_count(mnt, 1); /* essentially, that's mntget */
Al Viro84d17192013-03-15 10:53:28 -0400856 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
Al Viro3a2393d2011-11-25 03:19:09 -0500857 child_mnt->mnt_parent = mnt;
Al Viro84d17192013-03-15 10:53:28 -0400858 child_mnt->mnt_mp = mp;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700859 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
Ram Paib90fa9a2005-11-07 17:19:50 -0500860}
861
Eric W. Biederman1064f872017-01-20 18:28:35 +1300862static void __attach_mnt(struct mount *mnt, struct mount *parent)
863{
864 hlist_add_head_rcu(&mnt->mnt_hash,
865 m_hash(&parent->mnt, mnt->mnt_mountpoint));
866 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
867}
868
Nick Piggin99b7db72010-08-18 04:37:39 +1000869/*
870 * vfsmount lock must be held for write
871 */
Al Viro84d17192013-03-15 10:53:28 -0400872static void attach_mnt(struct mount *mnt,
873 struct mount *parent,
874 struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Al Viro84d17192013-03-15 10:53:28 -0400876 mnt_set_mountpoint(parent, mp, mnt);
Eric W. Biederman1064f872017-01-20 18:28:35 +1300877 __attach_mnt(mnt, parent);
Ram Paib90fa9a2005-11-07 17:19:50 -0500878}
879
Eric W. Biederman1064f872017-01-20 18:28:35 +1300880void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
Al Viro12a5b522014-08-10 03:44:55 -0400881{
Eric W. Biederman1064f872017-01-20 18:28:35 +1300882 struct mountpoint *old_mp = mnt->mnt_mp;
883 struct dentry *old_mountpoint = mnt->mnt_mountpoint;
884 struct mount *old_parent = mnt->mnt_parent;
885
886 list_del_init(&mnt->mnt_child);
887 hlist_del_init(&mnt->mnt_mp_list);
888 hlist_del_init_rcu(&mnt->mnt_hash);
889
890 attach_mnt(mnt, parent, mp);
891
892 put_mountpoint(old_mp);
893
894 /*
895 * Safely avoid even the suggestion this code might sleep or
896 * lock the mount hash by taking advantage of the knowledge that
897 * mnt_change_mountpoint will not release the final reference
898 * to a mountpoint.
899 *
900 * During mounting, the mount passed in as the parent mount will
901 * continue to use the old mountpoint and during unmounting, the
902 * old mountpoint will continue to exist until namespace_unlock,
903 * which happens well after mnt_change_mountpoint.
904 */
905 spin_lock(&old_mountpoint->d_lock);
906 old_mountpoint->d_lockref.count--;
907 spin_unlock(&old_mountpoint->d_lock);
908
909 mnt_add_count(old_parent, -1);
Al Viro12a5b522014-08-10 03:44:55 -0400910}
911
Ram Paib90fa9a2005-11-07 17:19:50 -0500912/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000913 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500914 */
Eric W. Biederman1064f872017-01-20 18:28:35 +1300915static void commit_tree(struct mount *mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500916{
Al Viro0714a532011-11-24 22:19:58 -0500917 struct mount *parent = mnt->mnt_parent;
Al Viro83adc752011-11-24 22:37:54 -0500918 struct mount *m;
Ram Paib90fa9a2005-11-07 17:19:50 -0500919 LIST_HEAD(head);
Al Viro143c8c92011-11-25 00:46:35 -0500920 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500921
Al Viro0714a532011-11-24 22:19:58 -0500922 BUG_ON(parent == mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500923
Al Viro1a4eeaf2011-11-25 02:19:55 -0500924 list_add_tail(&head, &mnt->mnt_list);
Al Virof7a99c52012-06-09 00:59:08 -0400925 list_for_each_entry(m, &head, mnt_list)
Al Viro143c8c92011-11-25 00:46:35 -0500926 m->mnt_ns = n;
Al Virof03c6592011-01-14 22:30:21 -0500927
Ram Paib90fa9a2005-11-07 17:19:50 -0500928 list_splice(&head, n->list.prev);
929
Eric W. Biedermand2921682016-09-28 00:27:17 -0500930 n->mounts += n->pending_mounts;
931 n->pending_mounts = 0;
932
Eric W. Biederman1064f872017-01-20 18:28:35 +1300933 __attach_mnt(mnt, parent);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800934 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Al Viro909b0a82011-11-25 03:06:56 -0500937static struct mount *next_mnt(struct mount *p, struct mount *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Al Viro6b41d532011-11-24 23:24:33 -0500939 struct list_head *next = p->mnt_mounts.next;
940 if (next == &p->mnt_mounts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 while (1) {
Al Viro909b0a82011-11-25 03:06:56 -0500942 if (p == root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return NULL;
Al Viro6b41d532011-11-24 23:24:33 -0500944 next = p->mnt_child.next;
945 if (next != &p->mnt_parent->mnt_mounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
Al Viro0714a532011-11-24 22:19:58 -0500947 p = p->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 }
Al Viro6b41d532011-11-24 23:24:33 -0500950 return list_entry(next, struct mount, mnt_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Al Viro315fc832011-11-24 18:57:30 -0500953static struct mount *skip_mnt_tree(struct mount *p)
Ram Pai9676f0c2005-11-07 17:21:20 -0500954{
Al Viro6b41d532011-11-24 23:24:33 -0500955 struct list_head *prev = p->mnt_mounts.prev;
956 while (prev != &p->mnt_mounts) {
957 p = list_entry(prev, struct mount, mnt_child);
958 prev = p->mnt_mounts.prev;
Ram Pai9676f0c2005-11-07 17:21:20 -0500959 }
960 return p;
961}
962
Al Viro9d412a42011-03-17 22:08:28 -0400963struct vfsmount *
964vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
965{
Al Virob105e272011-11-24 20:38:33 -0500966 struct mount *mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400967 struct dentry *root;
968
969 if (!type)
970 return ERR_PTR(-ENODEV);
971
972 mnt = alloc_vfsmnt(name);
973 if (!mnt)
974 return ERR_PTR(-ENOMEM);
975
976 if (flags & MS_KERNMOUNT)
Al Virob105e272011-11-24 20:38:33 -0500977 mnt->mnt.mnt_flags = MNT_INTERNAL;
Al Viro9d412a42011-03-17 22:08:28 -0400978
979 root = mount_fs(type, flags, name, data);
980 if (IS_ERR(root)) {
David Howells8ffcb322014-01-24 12:17:54 +0000981 mnt_free_id(mnt);
Al Viro9d412a42011-03-17 22:08:28 -0400982 free_vfsmnt(mnt);
983 return ERR_CAST(root);
984 }
985
Al Virob105e272011-11-24 20:38:33 -0500986 mnt->mnt.mnt_root = root;
987 mnt->mnt.mnt_sb = root->d_sb;
Al Viroa73324d2011-11-24 22:25:07 -0500988 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -0500989 mnt->mnt_parent = mnt;
Al Viro719ea2f2013-09-29 11:24:49 -0400990 lock_mount_hash();
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100991 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -0400992 unlock_mount_hash();
Al Virob105e272011-11-24 20:38:33 -0500993 return &mnt->mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400994}
995EXPORT_SYMBOL_GPL(vfs_kern_mount);
996
Eric W. Biederman93faccbb2017-02-01 06:06:16 +1300997struct vfsmount *
998vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
999 const char *name, void *data)
1000{
1001 /* Until it is worked out how to pass the user namespace
1002 * through from the parent mount to the submount don't support
1003 * unprivileged mounts with submounts.
1004 */
1005 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1006 return ERR_PTR(-EPERM);
1007
1008 return vfs_kern_mount(type, MS_SUBMOUNT, name, data);
1009}
1010EXPORT_SYMBOL_GPL(vfs_submount);
1011
Al Viro87129cc2011-11-24 21:24:27 -05001012static struct mount *clone_mnt(struct mount *old, struct dentry *root,
Ram Pai36341f62005-11-07 17:17:22 -05001013 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Al Viro87129cc2011-11-24 21:24:27 -05001015 struct super_block *sb = old->mnt.mnt_sb;
David Howellsbe34d1a2012-06-25 12:55:18 +01001016 struct mount *mnt;
1017 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
David Howellsbe34d1a2012-06-25 12:55:18 +01001019 mnt = alloc_vfsmnt(old->mnt_devname);
1020 if (!mnt)
1021 return ERR_PTR(-ENOMEM);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001022
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001023 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
David Howellsbe34d1a2012-06-25 12:55:18 +01001024 mnt->mnt_group_id = 0; /* not a peer of original */
1025 else
1026 mnt->mnt_group_id = old->mnt_group_id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001027
David Howellsbe34d1a2012-06-25 12:55:18 +01001028 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1029 err = mnt_alloc_group_id(mnt);
1030 if (err)
1031 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001033
Al Virof2ebb3a2014-02-27 09:35:45 -05001034 mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
Eric W. Biederman132c94e2013-03-22 04:08:05 -07001035 /* Don't allow unprivileged users to change mount flags */
Eric W. Biederman9566d672014-07-28 17:26:07 -07001036 if (flag & CL_UNPRIVILEGED) {
1037 mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
1038
1039 if (mnt->mnt.mnt_flags & MNT_READONLY)
1040 mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
1041
1042 if (mnt->mnt.mnt_flags & MNT_NODEV)
1043 mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
1044
1045 if (mnt->mnt.mnt_flags & MNT_NOSUID)
1046 mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
1047
1048 if (mnt->mnt.mnt_flags & MNT_NOEXEC)
1049 mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
1050 }
Eric W. Biederman132c94e2013-03-22 04:08:05 -07001051
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001052 /* Don't allow unprivileged users to reveal what is under a mount */
Eric W. Biederman381cacb2014-10-07 17:11:46 -07001053 if ((flag & CL_UNPRIVILEGED) &&
1054 (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001055 mnt->mnt.mnt_flags |= MNT_LOCKED;
1056
David Howellsbe34d1a2012-06-25 12:55:18 +01001057 atomic_inc(&sb->s_active);
1058 mnt->mnt.mnt_sb = sb;
1059 mnt->mnt.mnt_root = dget(root);
1060 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1061 mnt->mnt_parent = mnt;
Al Viro719ea2f2013-09-29 11:24:49 -04001062 lock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001063 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -04001064 unlock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001065
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001066 if ((flag & CL_SLAVE) ||
1067 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
David Howellsbe34d1a2012-06-25 12:55:18 +01001068 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1069 mnt->mnt_master = old;
1070 CLEAR_MNT_SHARED(mnt);
1071 } else if (!(flag & CL_PRIVATE)) {
1072 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1073 list_add(&mnt->mnt_share, &old->mnt_share);
1074 if (IS_MNT_SLAVE(old))
1075 list_add(&mnt->mnt_slave, &old->mnt_slave);
1076 mnt->mnt_master = old->mnt_master;
Al Viro5235d442016-11-20 19:33:09 -05001077 } else {
1078 CLEAR_MNT_SHARED(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +01001079 }
1080 if (flag & CL_MAKE_SHARED)
1081 set_mnt_shared(mnt);
1082
1083 /* stick the duplicate mount on the same expiry list
1084 * as the original if that was on one */
1085 if (flag & CL_EXPIRE) {
1086 if (!list_empty(&old->mnt_expire))
1087 list_add(&mnt->mnt_expire, &old->mnt_expire);
1088 }
1089
Al Virocb338d02011-11-24 20:55:08 -05001090 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001091
1092 out_free:
David Howells8ffcb322014-01-24 12:17:54 +00001093 mnt_free_id(mnt);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001094 free_vfsmnt(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +01001095 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Al Viro9ea459e2014-08-08 13:08:20 -04001098static void cleanup_mnt(struct mount *mnt)
1099{
1100 /*
1101 * This probably indicates that somebody messed
1102 * up a mnt_want/drop_write() pair. If this
1103 * happens, the filesystem was probably unable
1104 * to make r/w->r/o transitions.
1105 */
1106 /*
1107 * The locking used to deal with mnt_count decrement provides barriers,
1108 * so mnt_get_writers() below is safe.
1109 */
1110 WARN_ON(mnt_get_writers(mnt));
1111 if (unlikely(mnt->mnt_pins.first))
1112 mnt_pin_kill(mnt);
1113 fsnotify_vfsmount_delete(&mnt->mnt);
1114 dput(mnt->mnt.mnt_root);
1115 deactivate_super(mnt->mnt.mnt_sb);
1116 mnt_free_id(mnt);
1117 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1118}
1119
1120static void __cleanup_mnt(struct rcu_head *head)
1121{
1122 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1123}
1124
1125static LLIST_HEAD(delayed_mntput_list);
1126static void delayed_mntput(struct work_struct *unused)
1127{
1128 struct llist_node *node = llist_del_all(&delayed_mntput_list);
1129 struct llist_node *next;
1130
1131 for (; node; node = next) {
1132 next = llist_next(node);
1133 cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
1134 }
1135}
1136static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1137
Al Viro900148d2011-11-25 00:33:11 -05001138static void mntput_no_expire(struct mount *mnt)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001139{
Al Viro48a066e2013-09-29 22:06:07 -04001140 rcu_read_lock();
1141 mnt_add_count(mnt, -1);
1142 if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
1143 rcu_read_unlock();
Al Virof03c6592011-01-14 22:30:21 -05001144 return;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001145 }
Al Viro719ea2f2013-09-29 11:24:49 -04001146 lock_mount_hash();
Nick Pigginb3e19d92011-01-07 17:50:11 +11001147 if (mnt_get_count(mnt)) {
Al Viro48a066e2013-09-29 22:06:07 -04001148 rcu_read_unlock();
Al Viro719ea2f2013-09-29 11:24:49 -04001149 unlock_mount_hash();
Nick Pigginb3e19d92011-01-07 17:50:11 +11001150 return;
1151 }
Al Viro48a066e2013-09-29 22:06:07 -04001152 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1153 rcu_read_unlock();
1154 unlock_mount_hash();
1155 return;
1156 }
1157 mnt->mnt.mnt_flags |= MNT_DOOMED;
1158 rcu_read_unlock();
Andi Kleen962830d2012-05-08 13:32:02 +09301159
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +01001160 list_del(&mnt->mnt_instance);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001161
1162 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1163 struct mount *p, *tmp;
1164 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
1165 umount_mnt(p);
1166 }
1167 }
Al Viro719ea2f2013-09-29 11:24:49 -04001168 unlock_mount_hash();
Al Viro649a7952013-09-28 12:41:25 -04001169
Al Viro9ea459e2014-08-08 13:08:20 -04001170 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1171 struct task_struct *task = current;
1172 if (likely(!(task->flags & PF_KTHREAD))) {
1173 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1174 if (!task_work_add(task, &mnt->mnt_rcu, true))
1175 return;
1176 }
1177 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1178 schedule_delayed_work(&delayed_mntput_work, 1);
1179 return;
1180 }
1181 cleanup_mnt(mnt);
Al Viro7b7b1ac2005-11-07 17:13:39 -05001182}
Nick Pigginb3e19d92011-01-07 17:50:11 +11001183
1184void mntput(struct vfsmount *mnt)
1185{
1186 if (mnt) {
Al Viro863d6842011-11-25 00:57:42 -05001187 struct mount *m = real_mount(mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001188 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
Al Viro863d6842011-11-25 00:57:42 -05001189 if (unlikely(m->mnt_expiry_mark))
1190 m->mnt_expiry_mark = 0;
1191 mntput_no_expire(m);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001192 }
1193}
1194EXPORT_SYMBOL(mntput);
1195
1196struct vfsmount *mntget(struct vfsmount *mnt)
1197{
1198 if (mnt)
Al Viro83adc752011-11-24 22:37:54 -05001199 mnt_add_count(real_mount(mnt), 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001200 return mnt;
1201}
1202EXPORT_SYMBOL(mntget);
1203
Ian Kentc6609c02016-11-24 08:03:41 +11001204/* path_is_mountpoint() - Check if path is a mount in the current
1205 * namespace.
1206 *
1207 * d_mountpoint() can only be used reliably to establish if a dentry is
1208 * not mounted in any namespace and that common case is handled inline.
1209 * d_mountpoint() isn't aware of the possibility there may be multiple
1210 * mounts using a given dentry in a different namespace. This function
1211 * checks if the passed in path is a mountpoint rather than the dentry
1212 * alone.
1213 */
1214bool path_is_mountpoint(const struct path *path)
1215{
1216 unsigned seq;
1217 bool res;
1218
1219 if (!d_mountpoint(path->dentry))
1220 return false;
1221
1222 rcu_read_lock();
1223 do {
1224 seq = read_seqbegin(&mount_lock);
1225 res = __path_is_mountpoint(path);
1226 } while (read_seqretry(&mount_lock, seq));
1227 rcu_read_unlock();
1228
1229 return res;
1230}
1231EXPORT_SYMBOL(path_is_mountpoint);
1232
Al Viroca71cf72016-11-20 19:45:28 -05001233struct vfsmount *mnt_clone_internal(const struct path *path)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001234{
Al Viro3064c352014-08-07 09:12:31 -04001235 struct mount *p;
1236 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1237 if (IS_ERR(p))
1238 return ERR_CAST(p);
1239 p->mnt.mnt_flags |= MNT_INTERNAL;
1240 return &p->mnt;
Al Viro7b7b1ac2005-11-07 17:13:39 -05001241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001243static inline void mangle(struct seq_file *m, const char *s)
1244{
1245 seq_escape(m, s, " \t\n\\");
1246}
1247
1248/*
1249 * Simple .show_options callback for filesystems which don't want to
1250 * implement more complex mount option showing.
1251 *
1252 * See also save_mount_options().
1253 */
Al Viro34c80b12011-12-08 21:32:45 -05001254int generic_show_options(struct seq_file *m, struct dentry *root)
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001255{
Al Viro2a32ceb2009-05-08 16:05:57 -04001256 const char *options;
1257
1258 rcu_read_lock();
Al Viro34c80b12011-12-08 21:32:45 -05001259 options = rcu_dereference(root->d_sb->s_options);
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001260
1261 if (options != NULL && options[0]) {
1262 seq_putc(m, ',');
1263 mangle(m, options);
1264 }
Al Viro2a32ceb2009-05-08 16:05:57 -04001265 rcu_read_unlock();
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001266
1267 return 0;
1268}
1269EXPORT_SYMBOL(generic_show_options);
1270
1271/*
1272 * If filesystem uses generic_show_options(), this function should be
1273 * called from the fill_super() callback.
1274 *
1275 * The .remount_fs callback usually needs to be handled in a special
1276 * way, to make sure, that previous options are not overwritten if the
1277 * remount fails.
1278 *
1279 * Also note, that if the filesystem's .remount_fs function doesn't
1280 * reset all options to their default value, but changes only newly
1281 * given options, then the displayed options will not reflect reality
1282 * any more.
1283 */
1284void save_mount_options(struct super_block *sb, char *options)
1285{
Al Viro2a32ceb2009-05-08 16:05:57 -04001286 BUG_ON(sb->s_options);
1287 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001288}
1289EXPORT_SYMBOL(save_mount_options);
1290
Al Viro2a32ceb2009-05-08 16:05:57 -04001291void replace_mount_options(struct super_block *sb, char *options)
1292{
1293 char *old = sb->s_options;
1294 rcu_assign_pointer(sb->s_options, options);
1295 if (old) {
1296 synchronize_rcu();
1297 kfree(old);
1298 }
1299}
1300EXPORT_SYMBOL(replace_mount_options);
1301
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001302#ifdef CONFIG_PROC_FS
Al Viro0226f492011-12-06 12:21:54 -05001303/* iterator; we want it to have access to namespace_sem, thus here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304static void *m_start(struct seq_file *m, loff_t *pos)
1305{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001306 struct proc_mounts *p = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Ram Pai390c6842005-11-07 17:17:51 -05001308 down_read(&namespace_sem);
Al Viroc7999c32014-02-27 14:40:10 -05001309 if (p->cached_event == p->ns->event) {
1310 void *v = p->cached_mount;
1311 if (*pos == p->cached_index)
1312 return v;
1313 if (*pos == p->cached_index + 1) {
1314 v = seq_list_next(v, &p->ns->list, &p->cached_index);
1315 return p->cached_mount = v;
1316 }
1317 }
1318
1319 p->cached_event = p->ns->event;
1320 p->cached_mount = seq_list_start(&p->ns->list, *pos);
1321 p->cached_index = *pos;
1322 return p->cached_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1326{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001327 struct proc_mounts *p = m->private;
Pavel Emelianovb0765fb2007-07-15 23:39:55 -07001328
Al Viroc7999c32014-02-27 14:40:10 -05001329 p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1330 p->cached_index = *pos;
1331 return p->cached_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334static void m_stop(struct seq_file *m, void *v)
1335{
Ram Pai390c6842005-11-07 17:17:51 -05001336 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Al Viro0226f492011-12-06 12:21:54 -05001339static int m_show(struct seq_file *m, void *v)
Al Viro9f5596a2010-02-05 00:40:25 -05001340{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001341 struct proc_mounts *p = m->private;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001342 struct mount *r = list_entry(v, struct mount, mnt_list);
Al Viro0226f492011-12-06 12:21:54 -05001343 return p->show(m, &r->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001346const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 .start = m_start,
1348 .next = m_next,
1349 .stop = m_stop,
Al Viro0226f492011-12-06 12:21:54 -05001350 .show = m_show,
Chuck Leverb4629fe2006-03-20 13:44:12 -05001351};
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001352#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -05001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354/**
1355 * may_umount_tree - check if a mount tree is busy
1356 * @mnt: root of mount tree
1357 *
1358 * This is called to check if a tree of mounts has any
1359 * open files, pwds, chroots or sub mounts that are
1360 * busy.
1361 */
Al Viro909b0a82011-11-25 03:06:56 -05001362int may_umount_tree(struct vfsmount *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Al Viro909b0a82011-11-25 03:06:56 -05001364 struct mount *mnt = real_mount(m);
Ram Pai36341f62005-11-07 17:17:22 -05001365 int actual_refs = 0;
1366 int minimum_refs = 0;
Al Viro315fc832011-11-24 18:57:30 -05001367 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001368 BUG_ON(!m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Nick Pigginb3e19d92011-01-07 17:50:11 +11001370 /* write lock needed for mnt_get_count */
Al Viro719ea2f2013-09-29 11:24:49 -04001371 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05001372 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Viro83adc752011-11-24 22:37:54 -05001373 actual_refs += mnt_get_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Al Viro719ea2f2013-09-29 11:24:49 -04001376 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -08001379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Ian Kente3474a82006-03-27 01:14:51 -08001381 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384EXPORT_SYMBOL(may_umount_tree);
1385
1386/**
1387 * may_umount - check if a mount point is busy
1388 * @mnt: root of mount
1389 *
1390 * This is called to check if a mount point has any
1391 * open files, pwds, chroots or sub mounts. If the
1392 * mount has sub mounts this will return busy
1393 * regardless of whether the sub mounts are busy.
1394 *
1395 * Doesn't take quota and stuff into account. IOW, in some cases it will
1396 * give false negatives. The main reason why it's here is that we need
1397 * a non-destructive way to look for easily umountable filesystems.
1398 */
1399int may_umount(struct vfsmount *mnt)
1400{
Ian Kente3474a82006-03-27 01:14:51 -08001401 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001402 down_read(&namespace_sem);
Al Viro719ea2f2013-09-29 11:24:49 -04001403 lock_mount_hash();
Al Viro1ab59732011-11-24 21:35:16 -05001404 if (propagate_mount_busy(real_mount(mnt), 2))
Ian Kente3474a82006-03-27 01:14:51 -08001405 ret = 0;
Al Viro719ea2f2013-09-29 11:24:49 -04001406 unlock_mount_hash();
Al Viro8ad08d82010-01-16 12:56:08 -05001407 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411EXPORT_SYMBOL(may_umount);
1412
Al Viro38129a12014-03-20 21:10:51 -04001413static HLIST_HEAD(unmounted); /* protected by namespace_sem */
Al Viroe3197d82013-03-16 14:35:16 -04001414
Al Viro97216be2013-03-16 15:12:40 -04001415static void namespace_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001417 struct hlist_head head;
Al Viro97216be2013-03-16 15:12:40 -04001418
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001419 hlist_move_list(&unmounted, &head);
Al Viro97216be2013-03-16 15:12:40 -04001420
Al Viro97216be2013-03-16 15:12:40 -04001421 up_write(&namespace_sem);
1422
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001423 if (likely(hlist_empty(&head)))
1424 return;
1425
Al Viro48a066e2013-09-29 22:06:07 -04001426 synchronize_rcu();
1427
Al Viro87b95ce2015-01-10 19:01:08 -05001428 group_pin_kill(&head);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001429}
1430
Al Viro97216be2013-03-16 15:12:40 -04001431static inline void namespace_lock(void)
Al Viroe3197d82013-03-16 14:35:16 -04001432{
Al Viro97216be2013-03-16 15:12:40 -04001433 down_write(&namespace_sem);
Al Viroe3197d82013-03-16 14:35:16 -04001434}
1435
Eric W. Biedermane819f152014-12-24 07:20:01 -06001436enum umount_tree_flags {
1437 UMOUNT_SYNC = 1,
1438 UMOUNT_PROPAGATE = 2,
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001439 UMOUNT_CONNECTED = 4,
Eric W. Biedermane819f152014-12-24 07:20:01 -06001440};
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001441
1442static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1443{
1444 /* Leaving mounts connected is only valid for lazy umounts */
1445 if (how & UMOUNT_SYNC)
1446 return true;
1447
1448 /* A mount without a parent has nothing to be connected to */
1449 if (!mnt_has_parent(mnt))
1450 return true;
1451
1452 /* Because the reference counting rules change when mounts are
1453 * unmounted and connected, umounted mounts may not be
1454 * connected to mounted mounts.
1455 */
1456 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1457 return true;
1458
1459 /* Has it been requested that the mount remain connected? */
1460 if (how & UMOUNT_CONNECTED)
1461 return false;
1462
1463 /* Is the mount locked such that it needs to remain connected? */
1464 if (IS_MNT_LOCKED(mnt))
1465 return false;
1466
1467 /* By default disconnect the mount */
1468 return true;
1469}
1470
Nick Piggin99b7db72010-08-18 04:37:39 +10001471/*
Al Viro48a066e2013-09-29 22:06:07 -04001472 * mount_lock must be held
Nick Piggin99b7db72010-08-18 04:37:39 +10001473 * namespace_sem must be held for write
1474 */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001475static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001476{
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001477 LIST_HEAD(tmp_list);
Al Viro315fc832011-11-24 18:57:30 -05001478 struct mount *p;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001479
Eric W. Biederman5d884572015-01-03 05:39:35 -06001480 if (how & UMOUNT_PROPAGATE)
1481 propagate_mount_unlock(mnt);
1482
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001483 /* Gather the mounts to umount */
Eric W. Biederman590ce4b2014-12-22 18:30:08 -06001484 for (p = mnt; p; p = next_mnt(p, mnt)) {
1485 p->mnt.mnt_flags |= MNT_UMOUNT;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001486 list_move(&p->mnt_list, &tmp_list);
Eric W. Biederman590ce4b2014-12-22 18:30:08 -06001487 }
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001488
Eric W. Biederman411a9382014-12-22 19:12:07 -06001489 /* Hide the mounts from mnt_mounts */
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001490 list_for_each_entry(p, &tmp_list, mnt_list) {
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001491 list_del_init(&p->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -04001492 }
Ram Pai70fbcdf2005-11-07 17:17:04 -05001493
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001494 /* Add propogated mounts to the tmp_list */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001495 if (how & UMOUNT_PROPAGATE)
Al Viro7b8a53f2011-01-15 20:08:44 -05001496 propagate_umount(&tmp_list);
Ram Paia05964f2005-11-07 17:20:17 -05001497
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001498 while (!list_empty(&tmp_list)) {
Eric W. Biedermand2921682016-09-28 00:27:17 -05001499 struct mnt_namespace *ns;
Eric W. Biedermance07d892014-12-23 21:37:03 -06001500 bool disconnect;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001501 p = list_first_entry(&tmp_list, struct mount, mnt_list);
Al Viro6776db3d2011-11-25 00:22:05 -05001502 list_del_init(&p->mnt_expire);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001503 list_del_init(&p->mnt_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05001504 ns = p->mnt_ns;
1505 if (ns) {
1506 ns->mounts--;
1507 __touch_mnt_namespace(ns);
1508 }
Al Viro143c8c92011-11-25 00:46:35 -05001509 p->mnt_ns = NULL;
Eric W. Biedermane819f152014-12-24 07:20:01 -06001510 if (how & UMOUNT_SYNC)
Al Viro48a066e2013-09-29 22:06:07 -04001511 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
Al Viro87b95ce2015-01-10 19:01:08 -05001512
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001513 disconnect = disconnect_mount(p, how);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001514
1515 pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1516 disconnect ? &unmounted : NULL);
Al Viro676da582011-11-24 21:47:05 -05001517 if (mnt_has_parent(p)) {
Al Viro81b6b062014-08-30 18:32:05 -04001518 mnt_add_count(p->mnt_parent, -1);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001519 if (!disconnect) {
1520 /* Don't forget about p */
1521 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1522 } else {
1523 umount_mnt(p);
1524 }
Al Viro7c4b93d2008-03-21 23:59:49 -04001525 }
Al Viro0f0afb12011-11-24 20:43:10 -05001526 change_mnt_propagation(p, MS_PRIVATE);
Al Viro38129a12014-03-20 21:10:51 -04001527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Al Virob54b9be2013-03-16 14:39:34 -04001530static void shrink_submounts(struct mount *mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001531
Al Viro1ab59732011-11-24 21:35:16 -05001532static int do_umount(struct mount *mnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Al Viro1ab59732011-11-24 21:35:16 -05001534 struct super_block *sb = mnt->mnt.mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int retval;
1536
Al Viro1ab59732011-11-24 21:35:16 -05001537 retval = security_sb_umount(&mnt->mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (retval)
1539 return retval;
1540
1541 /*
1542 * Allow userspace to request a mountpoint be expired rather than
1543 * unmounting unconditionally. Unmount only happens if:
1544 * (1) the mark is already set (the mark is cleared by mntput())
1545 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1546 */
1547 if (flags & MNT_EXPIRE) {
Al Viro1ab59732011-11-24 21:35:16 -05001548 if (&mnt->mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 flags & (MNT_FORCE | MNT_DETACH))
1550 return -EINVAL;
1551
Nick Pigginb3e19d92011-01-07 17:50:11 +11001552 /*
1553 * probably don't strictly need the lock here if we examined
1554 * all race cases, but it's a slowpath.
1555 */
Al Viro719ea2f2013-09-29 11:24:49 -04001556 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -05001557 if (mnt_get_count(mnt) != 2) {
Al Viro719ea2f2013-09-29 11:24:49 -04001558 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -EBUSY;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001560 }
Al Viro719ea2f2013-09-29 11:24:49 -04001561 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Al Viro863d6842011-11-25 00:57:42 -05001563 if (!xchg(&mnt->mnt_expiry_mark, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return -EAGAIN;
1565 }
1566
1567 /*
1568 * If we may have to abort operations to get out of this
1569 * mount, and they will themselves hold resources we must
1570 * allow the fs to do things. In the Unix tradition of
1571 * 'Gee thats tricky lets do it in userspace' the umount_begin
1572 * might fail to complete on the first run through as other tasks
1573 * must return, and the like. Thats for the mount program to worry
1574 * about for the moment.
1575 */
1576
Al Viro42faad92008-04-24 07:21:56 -04001577 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001578 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /*
1582 * No sense to grab the lock for this test, but test itself looks
1583 * somewhat bogus. Suggestions for better replacement?
1584 * Ho-hum... In principle, we might treat that as umount + switch
1585 * to rootfs. GC would eventually take care of the old vfsmount.
1586 * Actually it makes sense, especially if rootfs would contain a
1587 * /reboot - static binary that would close all descriptors and
1588 * call reboot(9). Then init(8) could umount root and exec /reboot.
1589 */
Al Viro1ab59732011-11-24 21:35:16 -05001590 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Special case for "unmounting" root ...
1593 * we just try to remount it readonly.
1594 */
Andy Lutomirskia1480dc2014-10-08 12:32:47 -07001595 if (!capable(CAP_SYS_ADMIN))
1596 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 down_write(&sb->s_umount);
Al Viro4aa98cf2009-05-08 13:36:58 -04001598 if (!(sb->s_flags & MS_RDONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 up_write(&sb->s_umount);
1601 return retval;
1602 }
1603
Al Viro97216be2013-03-16 15:12:40 -04001604 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001605 lock_mount_hash();
Al Viro5addc5d2005-11-07 17:15:49 -05001606 event++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Al Viro48a066e2013-09-29 22:06:07 -04001608 if (flags & MNT_DETACH) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05001609 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001610 umount_tree(mnt, UMOUNT_PROPAGATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 retval = 0;
Al Viro48a066e2013-09-29 22:06:07 -04001612 } else {
1613 shrink_submounts(mnt);
1614 retval = -EBUSY;
1615 if (!propagate_mount_busy(mnt, 2)) {
1616 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001617 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Viro48a066e2013-09-29 22:06:07 -04001618 retval = 0;
1619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
Al Viro719ea2f2013-09-29 11:24:49 -04001621 unlock_mount_hash();
Al Viroe3197d82013-03-16 14:35:16 -04001622 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return retval;
1624}
1625
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001626/*
1627 * __detach_mounts - lazily unmount all mounts on the specified dentry
1628 *
1629 * During unlink, rmdir, and d_drop it is possible to loose the path
1630 * to an existing mountpoint, and wind up leaking the mount.
1631 * detach_mounts allows lazily unmounting those mounts instead of
1632 * leaking them.
1633 *
1634 * The caller may hold dentry->d_inode->i_mutex.
1635 */
1636void __detach_mounts(struct dentry *dentry)
1637{
1638 struct mountpoint *mp;
1639 struct mount *mnt;
1640
1641 namespace_lock();
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001642 lock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001643 mp = lookup_mountpoint(dentry);
Eric W. Biedermanf53e5792015-01-19 11:48:45 -06001644 if (IS_ERR_OR_NULL(mp))
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001645 goto out_unlock;
1646
Andrey Ulanove06b9332016-04-15 14:24:41 -07001647 event++;
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001648 while (!hlist_empty(&mp->m_list)) {
1649 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001650 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
Eric W. Biedermanfe78fcc2015-07-17 14:54:27 -05001651 hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1652 umount_mnt(mnt);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001653 }
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001654 else umount_tree(mnt, UMOUNT_CONNECTED);
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001655 }
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001656 put_mountpoint(mp);
1657out_unlock:
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001658 unlock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001659 namespace_unlock();
1660}
1661
Al Viro9b40bc92013-02-22 22:45:42 -05001662/*
1663 * Is the caller allowed to modify his namespace?
1664 */
1665static inline bool may_mount(void)
1666{
1667 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1668}
1669
Jeff Layton9e8925b2015-11-16 09:49:34 -05001670static inline bool may_mandlock(void)
1671{
1672#ifndef CONFIG_MANDATORY_FILE_LOCKING
1673 return false;
1674#endif
Eric W. Biederman95ace752015-11-11 17:22:33 -06001675 return capable(CAP_SYS_ADMIN);
Jeff Layton9e8925b2015-11-16 09:49:34 -05001676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
1679 * Now umount can handle mount points as well as block devices.
1680 * This is important for filesystems which use unnamed block devices.
1681 *
1682 * We now support a flag for forced unmount like the other 'big iron'
1683 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1684 */
1685
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001686SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Al Viro2d8f3032008-07-22 09:59:21 -04001688 struct path path;
Al Viro900148d2011-11-25 00:33:11 -05001689 struct mount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int retval;
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001691 int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001693 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1694 return -EINVAL;
1695
Al Viro9b40bc92013-02-22 22:45:42 -05001696 if (!may_mount())
1697 return -EPERM;
1698
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001699 if (!(flags & UMOUNT_NOFOLLOW))
1700 lookup_flags |= LOOKUP_FOLLOW;
1701
Al Viro197df042013-09-08 14:03:27 -04001702 retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (retval)
1704 goto out;
Al Viro900148d2011-11-25 00:33:11 -05001705 mnt = real_mount(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 retval = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04001707 if (path.dentry != path.mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto dput_and_out;
Al Viro143c8c92011-11-25 00:46:35 -05001709 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto dput_and_out;
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001711 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1712 goto dput_and_out;
Eric W. Biedermanb2f5d4d2014-10-04 14:44:03 -07001713 retval = -EPERM;
1714 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1715 goto dput_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Al Viro900148d2011-11-25 00:33:11 -05001717 retval = do_umount(mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718dput_and_out:
Jan Blunck429731b2008-02-14 19:34:31 -08001719 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Al Viro2d8f3032008-07-22 09:59:21 -04001720 dput(path.dentry);
Al Viro900148d2011-11-25 00:33:11 -05001721 mntput_no_expire(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722out:
1723 return retval;
1724}
1725
1726#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1727
1728/*
Ram Paib58fed82005-11-07 17:16:09 -05001729 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001731SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Ram Paib58fed82005-11-07 17:16:09 -05001733 return sys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
1736#endif
1737
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001738static bool is_mnt_ns_file(struct dentry *dentry)
Eric W. Biederman8823c072010-03-07 18:49:36 -08001739{
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001740 /* Is this a proxy for a mount namespace? */
Al Viroe149ed22014-11-01 10:57:28 -04001741 return dentry->d_op == &ns_dentry_operations &&
1742 dentry->d_fsdata == &mntns_operations;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001743}
1744
Al Viro58be28252014-11-01 00:00:23 -04001745struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
1746{
1747 return container_of(ns, struct mnt_namespace, ns);
1748}
1749
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001750static bool mnt_ns_loop(struct dentry *dentry)
1751{
1752 /* Could bind mounting the mount namespace inode cause a
1753 * mount namespace loop?
1754 */
1755 struct mnt_namespace *mnt_ns;
1756 if (!is_mnt_ns_file(dentry))
1757 return false;
1758
Al Virof77c8012014-11-01 03:13:17 -04001759 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
Eric W. Biederman8823c072010-03-07 18:49:36 -08001760 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1761}
1762
Al Viro87129cc2011-11-24 21:24:27 -05001763struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001764 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Al Viro84d17192013-03-15 10:53:28 -04001766 struct mount *res, *p, *q, *r, *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001768 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1769 return ERR_PTR(-EINVAL);
1770
1771 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
David Howellsbe34d1a2012-06-25 12:55:18 +01001772 return ERR_PTR(-EINVAL);
Ram Pai9676f0c2005-11-07 17:21:20 -05001773
Ram Pai36341f62005-11-07 17:17:22 -05001774 res = q = clone_mnt(mnt, dentry, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001775 if (IS_ERR(q))
1776 return q;
1777
Al Viroa73324d2011-11-24 22:25:07 -05001778 q->mnt_mountpoint = mnt->mnt_mountpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 p = mnt;
Al Viro6b41d532011-11-24 23:24:33 -05001781 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Al Viro315fc832011-11-24 18:57:30 -05001782 struct mount *s;
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001783 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 continue;
1785
Al Viro909b0a82011-11-25 03:06:56 -05001786 for (s = r; s; s = next_mnt(s, r)) {
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001787 if (!(flag & CL_COPY_UNBINDABLE) &&
1788 IS_MNT_UNBINDABLE(s)) {
1789 s = skip_mnt_tree(s);
1790 continue;
1791 }
1792 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1793 is_mnt_ns_file(s->mnt.mnt_root)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001794 s = skip_mnt_tree(s);
1795 continue;
1796 }
Al Viro0714a532011-11-24 22:19:58 -05001797 while (p != s->mnt_parent) {
1798 p = p->mnt_parent;
1799 q = q->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Al Viro87129cc2011-11-24 21:24:27 -05001801 p = s;
Al Viro84d17192013-03-15 10:53:28 -04001802 parent = q;
Al Viro87129cc2011-11-24 21:24:27 -05001803 q = clone_mnt(p, p->mnt.mnt_root, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001804 if (IS_ERR(q))
1805 goto out;
Al Viro719ea2f2013-09-29 11:24:49 -04001806 lock_mount_hash();
Al Viro1a4eeaf2011-11-25 02:19:55 -05001807 list_add_tail(&q->mnt_list, &res->mnt_list);
Eric W. Biederman1064f872017-01-20 18:28:35 +13001808 attach_mnt(q, parent, p->mnt_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04001809 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811 }
1812 return res;
David Howellsbe34d1a2012-06-25 12:55:18 +01001813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (res) {
Al Viro719ea2f2013-09-29 11:24:49 -04001815 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06001816 umount_tree(res, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04001817 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001819 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
David Howellsbe34d1a2012-06-25 12:55:18 +01001822/* Caller should check returned pointer for errors */
1823
Al Viroca71cf72016-11-20 19:45:28 -05001824struct vfsmount *collect_mounts(const struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001825{
Al Virocb338d02011-11-24 20:55:08 -05001826 struct mount *tree;
Al Viro97216be2013-03-16 15:12:40 -04001827 namespace_lock();
Eric W. Biedermancd4a4012015-01-07 14:28:26 -06001828 if (!check_mnt(real_mount(path->mnt)))
1829 tree = ERR_PTR(-EINVAL);
1830 else
1831 tree = copy_tree(real_mount(path->mnt), path->dentry,
1832 CL_COPY_ALL | CL_PRIVATE);
Al Viro328e6d92013-03-16 14:49:45 -04001833 namespace_unlock();
David Howellsbe34d1a2012-06-25 12:55:18 +01001834 if (IS_ERR(tree))
Dan Carpenter52e220d2013-08-14 12:44:39 +03001835 return ERR_CAST(tree);
David Howellsbe34d1a2012-06-25 12:55:18 +01001836 return &tree->mnt;
Al Viro8aec0802007-06-07 12:20:32 -04001837}
1838
1839void drop_collected_mounts(struct vfsmount *mnt)
1840{
Al Viro97216be2013-03-16 15:12:40 -04001841 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001842 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06001843 umount_tree(real_mount(mnt), UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04001844 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04001845 namespace_unlock();
Al Viro8aec0802007-06-07 12:20:32 -04001846}
1847
Miklos Szeredic771d682014-10-24 00:14:36 +02001848/**
1849 * clone_private_mount - create a private clone of a path
1850 *
1851 * This creates a new vfsmount, which will be the clone of @path. The new will
1852 * not be attached anywhere in the namespace and will be private (i.e. changes
1853 * to the originating mount won't be propagated into this).
1854 *
1855 * Release with mntput().
1856 */
Al Viroca71cf72016-11-20 19:45:28 -05001857struct vfsmount *clone_private_mount(const struct path *path)
Miklos Szeredic771d682014-10-24 00:14:36 +02001858{
1859 struct mount *old_mnt = real_mount(path->mnt);
1860 struct mount *new_mnt;
1861
1862 if (IS_MNT_UNBINDABLE(old_mnt))
1863 return ERR_PTR(-EINVAL);
1864
Miklos Szeredic771d682014-10-24 00:14:36 +02001865 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
Miklos Szeredic771d682014-10-24 00:14:36 +02001866 if (IS_ERR(new_mnt))
1867 return ERR_CAST(new_mnt);
1868
1869 return &new_mnt->mnt;
1870}
1871EXPORT_SYMBOL_GPL(clone_private_mount);
1872
Al Viro1f707132010-01-30 22:51:25 -05001873int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1874 struct vfsmount *root)
1875{
Al Viro1a4eeaf2011-11-25 02:19:55 -05001876 struct mount *mnt;
Al Viro1f707132010-01-30 22:51:25 -05001877 int res = f(root, arg);
1878 if (res)
1879 return res;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001880 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1881 res = f(&mnt->mnt, arg);
Al Viro1f707132010-01-30 22:51:25 -05001882 if (res)
1883 return res;
1884 }
1885 return 0;
1886}
1887
Al Viro4b8b21f2011-11-24 19:54:23 -05001888static void cleanup_group_ids(struct mount *mnt, struct mount *end)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001889{
Al Viro315fc832011-11-24 18:57:30 -05001890 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001891
Al Viro909b0a82011-11-25 03:06:56 -05001892 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001893 if (p->mnt_group_id && !IS_MNT_SHARED(p))
Al Viro4b8b21f2011-11-24 19:54:23 -05001894 mnt_release_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001895 }
1896}
1897
Al Viro4b8b21f2011-11-24 19:54:23 -05001898static int invent_group_ids(struct mount *mnt, bool recurse)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001899{
Al Viro315fc832011-11-24 18:57:30 -05001900 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001901
Al Viro909b0a82011-11-25 03:06:56 -05001902 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
Al Virofc7be132011-11-25 01:05:37 -05001903 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001904 int err = mnt_alloc_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001905 if (err) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001906 cleanup_group_ids(mnt, p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001907 return err;
1908 }
1909 }
1910 }
1911
1912 return 0;
1913}
1914
Eric W. Biedermand2921682016-09-28 00:27:17 -05001915int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1916{
1917 unsigned int max = READ_ONCE(sysctl_mount_max);
1918 unsigned int mounts = 0, old, pending, sum;
1919 struct mount *p;
1920
1921 for (p = mnt; p; p = next_mnt(p, mnt))
1922 mounts++;
1923
1924 old = ns->mounts;
1925 pending = ns->pending_mounts;
1926 sum = old + pending;
1927 if ((old > sum) ||
1928 (pending > sum) ||
1929 (max < sum) ||
1930 (mounts > (max - sum)))
1931 return -ENOSPC;
1932
1933 ns->pending_mounts = pending + mounts;
1934 return 0;
1935}
1936
Ram Paib90fa9a2005-11-07 17:19:50 -05001937/*
1938 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05001939 * @nd : place the mount tree @source_mnt is attached
1940 * @parent_nd : if non-null, detach the source_mnt from its parent and
1941 * store the parent mount and mountpoint dentry.
1942 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05001943 *
1944 * NOTE: in the table below explains the semantics when a source mount
1945 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05001946 * ---------------------------------------------------------------------------
1947 * | BIND MOUNT OPERATION |
1948 * |**************************************************************************
1949 * | source-->| shared | private | slave | unbindable |
1950 * | dest | | | | |
1951 * | | | | | | |
1952 * | v | | | | |
1953 * |**************************************************************************
1954 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1955 * | | | | | |
1956 * |non-shared| shared (+) | private | slave (*) | invalid |
1957 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05001958 * A bind operation clones the source mount and mounts the clone on the
1959 * destination mount.
1960 *
1961 * (++) the cloned mount is propagated to all the mounts in the propagation
1962 * tree of the destination mount and the cloned mount is added to
1963 * the peer group of the source mount.
1964 * (+) the cloned mount is created under the destination mount and is marked
1965 * as shared. The cloned mount is added to the peer group of the source
1966 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05001967 * (+++) the mount is propagated to all the mounts in the propagation tree
1968 * of the destination mount and the cloned mount is made slave
1969 * of the same master as that of the source mount. The cloned mount
1970 * is marked as 'shared and slave'.
1971 * (*) the cloned mount is made a slave of the same master as that of the
1972 * source mount.
1973 *
Ram Pai9676f0c2005-11-07 17:21:20 -05001974 * ---------------------------------------------------------------------------
1975 * | MOVE MOUNT OPERATION |
1976 * |**************************************************************************
1977 * | source-->| shared | private | slave | unbindable |
1978 * | dest | | | | |
1979 * | | | | | | |
1980 * | v | | | | |
1981 * |**************************************************************************
1982 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1983 * | | | | | |
1984 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1985 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05001986 *
1987 * (+) the mount is moved to the destination. And is then propagated to
1988 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05001989 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05001990 * (+++) the mount is moved to the destination and is then propagated to
1991 * all the mounts belonging to the destination mount's propagation tree.
1992 * the mount is marked as 'shared and slave'.
1993 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05001994 *
1995 * if the source mount is a tree, the operations explained above is
1996 * applied to each mount in the tree.
1997 * Must be called without spinlocks held, since this function can sleep
1998 * in allocations.
1999 */
Al Viro0fb54e52011-11-24 19:59:16 -05002000static int attach_recursive_mnt(struct mount *source_mnt,
Al Viro84d17192013-03-15 10:53:28 -04002001 struct mount *dest_mnt,
2002 struct mountpoint *dest_mp,
2003 struct path *parent_path)
Ram Paib90fa9a2005-11-07 17:19:50 -05002004{
Al Viro38129a12014-03-20 21:10:51 -04002005 HLIST_HEAD(tree_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002006 struct mnt_namespace *ns = dest_mnt->mnt_ns;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002007 struct mountpoint *smp;
Al Viro315fc832011-11-24 18:57:30 -05002008 struct mount *child, *p;
Al Viro38129a12014-03-20 21:10:51 -04002009 struct hlist_node *n;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002010 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002011
Eric W. Biederman1064f872017-01-20 18:28:35 +13002012 /* Preallocate a mountpoint in case the new mounts need
2013 * to be tucked under other mounts.
2014 */
2015 smp = get_mountpoint(source_mnt->mnt.mnt_root);
2016 if (IS_ERR(smp))
2017 return PTR_ERR(smp);
2018
Eric W. Biedermand2921682016-09-28 00:27:17 -05002019 /* Is there space to add these mounts to the mount namespace? */
2020 if (!parent_path) {
2021 err = count_mounts(ns, source_mnt);
2022 if (err)
2023 goto out;
2024 }
2025
Al Virofc7be132011-11-25 01:05:37 -05002026 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro0fb54e52011-11-24 19:59:16 -05002027 err = invent_group_ids(source_mnt, true);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002028 if (err)
2029 goto out;
Al Viro0b1b9012014-03-21 10:14:08 -04002030 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
Al Virof2ebb3a2014-02-27 09:35:45 -05002031 lock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002032 if (err)
2033 goto out_cleanup_ids;
Al Viro909b0a82011-11-25 03:06:56 -05002034 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
Al Viro0f0afb12011-11-24 20:43:10 -05002035 set_mnt_shared(p);
Al Viro0b1b9012014-03-21 10:14:08 -04002036 } else {
2037 lock_mount_hash();
Ram Paib90fa9a2005-11-07 17:19:50 -05002038 }
Al Viro1a390682008-03-21 20:48:19 -04002039 if (parent_path) {
Al Viro0fb54e52011-11-24 19:59:16 -05002040 detach_mnt(source_mnt, parent_path);
Al Viro84d17192013-03-15 10:53:28 -04002041 attach_mnt(source_mnt, dest_mnt, dest_mp);
Al Viro143c8c92011-11-25 00:46:35 -05002042 touch_mnt_namespace(source_mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05002043 } else {
Al Viro84d17192013-03-15 10:53:28 -04002044 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
Eric W. Biederman1064f872017-01-20 18:28:35 +13002045 commit_tree(source_mnt);
Ram Pai21444402005-11-07 17:20:03 -05002046 }
Ram Paib90fa9a2005-11-07 17:19:50 -05002047
Al Viro38129a12014-03-20 21:10:51 -04002048 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
Al Viro1d6a32a2014-03-20 20:34:43 -04002049 struct mount *q;
Al Viro38129a12014-03-20 21:10:51 -04002050 hlist_del_init(&child->mnt_hash);
Eric W. Biederman1064f872017-01-20 18:28:35 +13002051 q = __lookup_mnt(&child->mnt_parent->mnt,
2052 child->mnt_mountpoint);
2053 if (q)
2054 mnt_change_mountpoint(child, smp, q);
2055 commit_tree(child);
Ram Paib90fa9a2005-11-07 17:19:50 -05002056 }
Eric W. Biederman1064f872017-01-20 18:28:35 +13002057 put_mountpoint(smp);
Al Viro719ea2f2013-09-29 11:24:49 -04002058 unlock_mount_hash();
Nick Piggin99b7db72010-08-18 04:37:39 +10002059
Ram Paib90fa9a2005-11-07 17:19:50 -05002060 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002061
2062 out_cleanup_ids:
Al Virof2ebb3a2014-02-27 09:35:45 -05002063 while (!hlist_empty(&tree_list)) {
2064 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002065 child->mnt_parent->mnt_ns->pending_mounts = 0;
Eric W. Biedermane819f152014-12-24 07:20:01 -06002066 umount_tree(child, UMOUNT_SYNC);
Al Virof2ebb3a2014-02-27 09:35:45 -05002067 }
2068 unlock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002069 cleanup_group_ids(source_mnt, NULL);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002070 out:
Eric W. Biedermand2921682016-09-28 00:27:17 -05002071 ns->pending_mounts = 0;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002072
2073 read_seqlock_excl(&mount_lock);
2074 put_mountpoint(smp);
2075 read_sequnlock_excl(&mount_lock);
2076
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002077 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002078}
2079
Al Viro84d17192013-03-15 10:53:28 -04002080static struct mountpoint *lock_mount(struct path *path)
Al Virob12cea92011-03-18 08:55:38 -04002081{
2082 struct vfsmount *mnt;
Al Viro84d17192013-03-15 10:53:28 -04002083 struct dentry *dentry = path->dentry;
Al Virob12cea92011-03-18 08:55:38 -04002084retry:
Al Viro59551022016-01-22 15:40:57 -05002085 inode_lock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002086 if (unlikely(cant_mount(dentry))) {
Al Viro59551022016-01-22 15:40:57 -05002087 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002088 return ERR_PTR(-ENOENT);
Al Virob12cea92011-03-18 08:55:38 -04002089 }
Al Viro97216be2013-03-16 15:12:40 -04002090 namespace_lock();
Al Virob12cea92011-03-18 08:55:38 -04002091 mnt = lookup_mnt(path);
Al Viro84d17192013-03-15 10:53:28 -04002092 if (likely(!mnt)) {
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002093 struct mountpoint *mp = get_mountpoint(dentry);
Al Viro84d17192013-03-15 10:53:28 -04002094 if (IS_ERR(mp)) {
Al Viro97216be2013-03-16 15:12:40 -04002095 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002096 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002097 return mp;
2098 }
2099 return mp;
2100 }
Al Viro97216be2013-03-16 15:12:40 -04002101 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002102 inode_unlock(path->dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002103 path_put(path);
2104 path->mnt = mnt;
Al Viro84d17192013-03-15 10:53:28 -04002105 dentry = path->dentry = dget(mnt->mnt_root);
Al Virob12cea92011-03-18 08:55:38 -04002106 goto retry;
2107}
2108
Al Viro84d17192013-03-15 10:53:28 -04002109static void unlock_mount(struct mountpoint *where)
Al Virob12cea92011-03-18 08:55:38 -04002110{
Al Viro84d17192013-03-15 10:53:28 -04002111 struct dentry *dentry = where->m_dentry;
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002112
2113 read_seqlock_excl(&mount_lock);
Al Viro84d17192013-03-15 10:53:28 -04002114 put_mountpoint(where);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002115 read_sequnlock_excl(&mount_lock);
2116
Al Viro328e6d92013-03-16 14:49:45 -04002117 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002118 inode_unlock(dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002119}
2120
Al Viro84d17192013-03-15 10:53:28 -04002121static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Al Viro95bc5f22011-11-25 00:30:56 -05002123 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return -EINVAL;
2125
David Howellse36cb0b2015-01-29 12:02:35 +00002126 if (d_is_dir(mp->m_dentry) !=
2127 d_is_dir(mnt->mnt.mnt_root))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return -ENOTDIR;
2129
Al Viro84d17192013-03-15 10:53:28 -04002130 return attach_recursive_mnt(mnt, p, mp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002134 * Sanity check the flags to change_mnt_propagation.
2135 */
2136
2137static int flags_to_propagation_type(int flags)
2138{
Roman Borisov7c6e9842011-05-25 16:26:48 -07002139 int type = flags & ~(MS_REC | MS_SILENT);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002140
2141 /* Fail if any non-propagation flags are set */
2142 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2143 return 0;
2144 /* Only one propagation flag should be set */
2145 if (!is_power_of_2(type))
2146 return 0;
2147 return type;
2148}
2149
2150/*
Ram Pai07b20882005-11-07 17:19:07 -05002151 * recursively change the type of the mountpoint.
2152 */
Al Viro0a0d8a42008-08-02 00:55:27 -04002153static int do_change_type(struct path *path, int flag)
Ram Pai07b20882005-11-07 17:19:07 -05002154{
Al Viro315fc832011-11-24 18:57:30 -05002155 struct mount *m;
Al Viro4b8b21f2011-11-24 19:54:23 -05002156 struct mount *mnt = real_mount(path->mnt);
Ram Pai07b20882005-11-07 17:19:07 -05002157 int recurse = flag & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002158 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002159 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05002160
Al Viro2d92ab32008-08-02 00:51:11 -04002161 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05002162 return -EINVAL;
2163
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002164 type = flags_to_propagation_type(flag);
2165 if (!type)
2166 return -EINVAL;
2167
Al Viro97216be2013-03-16 15:12:40 -04002168 namespace_lock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002169 if (type == MS_SHARED) {
2170 err = invent_group_ids(mnt, recurse);
2171 if (err)
2172 goto out_unlock;
2173 }
2174
Al Viro719ea2f2013-09-29 11:24:49 -04002175 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05002176 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
Al Viro0f0afb12011-11-24 20:43:10 -05002177 change_mnt_propagation(m, type);
Al Viro719ea2f2013-09-29 11:24:49 -04002178 unlock_mount_hash();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002179
2180 out_unlock:
Al Viro97216be2013-03-16 15:12:40 -04002181 namespace_unlock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002182 return err;
Ram Pai07b20882005-11-07 17:19:07 -05002183}
2184
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002185static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2186{
2187 struct mount *child;
2188 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2189 if (!is_subdir(child->mnt_mountpoint, dentry))
2190 continue;
2191
2192 if (child->mnt.mnt_flags & MNT_LOCKED)
2193 return true;
2194 }
2195 return false;
2196}
2197
Ram Pai07b20882005-11-07 17:19:07 -05002198/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 * do loopback mount.
2200 */
Al Viro808d4e32012-10-11 11:42:01 -04002201static int do_loopback(struct path *path, const char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08002202 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Al Viro2d92ab32008-08-02 00:51:11 -04002204 struct path old_path;
Al Viro84d17192013-03-15 10:53:28 -04002205 struct mount *mnt = NULL, *old, *parent;
2206 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05002207 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (!old_name || !*old_name)
2209 return -EINVAL;
Trond Myklebust815d4052011-09-26 20:36:09 -04002210 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (err)
2212 return err;
2213
Eric W. Biederman8823c072010-03-07 18:49:36 -08002214 err = -EINVAL;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002215 if (mnt_ns_loop(old_path.dentry))
Eric W. Biederman8823c072010-03-07 18:49:36 -08002216 goto out;
2217
Al Viro84d17192013-03-15 10:53:28 -04002218 mp = lock_mount(path);
2219 err = PTR_ERR(mp);
2220 if (IS_ERR(mp))
Jan Blunck4ac91372008-02-14 19:34:32 -08002221 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05002222
Al Viro87129cc2011-11-24 21:24:27 -05002223 old = real_mount(old_path.mnt);
Al Viro84d17192013-03-15 10:53:28 -04002224 parent = real_mount(path->mnt);
Al Viro87129cc2011-11-24 21:24:27 -05002225
Al Virob12cea92011-03-18 08:55:38 -04002226 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05002227 if (IS_MNT_UNBINDABLE(old))
Al Virob12cea92011-03-18 08:55:38 -04002228 goto out2;
2229
Al Viroe149ed22014-11-01 10:57:28 -04002230 if (!check_mnt(parent))
2231 goto out2;
2232
2233 if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
Al Virob12cea92011-03-18 08:55:38 -04002234 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002236 if (!recurse && has_locked_children(old, old_path.dentry))
2237 goto out2;
2238
Al Viroccd48bc2005-11-07 17:15:04 -05002239 if (recurse)
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002240 mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
Al Viroccd48bc2005-11-07 17:15:04 -05002241 else
Al Viro87129cc2011-11-24 21:24:27 -05002242 mnt = clone_mnt(old, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05002243
David Howellsbe34d1a2012-06-25 12:55:18 +01002244 if (IS_ERR(mnt)) {
2245 err = PTR_ERR(mnt);
Andrey Vagine9c5d8a2013-04-09 17:33:29 +04002246 goto out2;
David Howellsbe34d1a2012-06-25 12:55:18 +01002247 }
Al Viroccd48bc2005-11-07 17:15:04 -05002248
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002249 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2250
Al Viro84d17192013-03-15 10:53:28 -04002251 err = graft_tree(mnt, parent, mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002252 if (err) {
Al Viro719ea2f2013-09-29 11:24:49 -04002253 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06002254 umount_tree(mnt, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04002255 unlock_mount_hash();
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05002256 }
Al Virob12cea92011-03-18 08:55:38 -04002257out2:
Al Viro84d17192013-03-15 10:53:28 -04002258 unlock_mount(mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002259out:
Al Viro2d92ab32008-08-02 00:51:11 -04002260 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return err;
2262}
2263
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002264static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
2265{
2266 int error = 0;
2267 int readonly_request = 0;
2268
2269 if (ms_flags & MS_RDONLY)
2270 readonly_request = 1;
2271 if (readonly_request == __mnt_is_readonly(mnt))
2272 return 0;
2273
2274 if (readonly_request)
Al Viro83adc752011-11-24 22:37:54 -05002275 error = mnt_make_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002276 else
Al Viro83adc752011-11-24 22:37:54 -05002277 __mnt_unmake_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002278 return error;
2279}
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281/*
2282 * change filesystem flags. dir should be a physical root of filesystem.
2283 * If you've mounted a non-root directory somewhere and want to do remount
2284 * on it - tough luck.
2285 */
Al Viro0a0d8a42008-08-02 00:55:27 -04002286static int do_remount(struct path *path, int flags, int mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 void *data)
2288{
2289 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04002290 struct super_block *sb = path->mnt->mnt_sb;
Al Viro143c8c92011-11-25 00:46:35 -05002291 struct mount *mnt = real_mount(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Al Viro143c8c92011-11-25 00:46:35 -05002293 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return -EINVAL;
2295
Al Viro2d92ab32008-08-02 00:51:11 -04002296 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return -EINVAL;
2298
Eric W. Biederman07b64552014-07-28 17:10:56 -07002299 /* Don't allow changing of locked mnt flags.
2300 *
2301 * No locks need to be held here while testing the various
2302 * MNT_LOCK flags because those flags can never be cleared
2303 * once they are set.
2304 */
2305 if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
2306 !(mnt_flags & MNT_READONLY)) {
2307 return -EPERM;
2308 }
Eric W. Biederman9566d672014-07-28 17:26:07 -07002309 if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
2310 !(mnt_flags & MNT_NODEV)) {
Eric W. Biederman67690f92016-05-18 13:50:06 -05002311 return -EPERM;
Eric W. Biederman9566d672014-07-28 17:26:07 -07002312 }
2313 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
2314 !(mnt_flags & MNT_NOSUID)) {
2315 return -EPERM;
2316 }
2317 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
2318 !(mnt_flags & MNT_NOEXEC)) {
2319 return -EPERM;
2320 }
2321 if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
2322 ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
2323 return -EPERM;
2324 }
2325
Eric Parisff36fe22011-03-03 16:09:14 -05002326 err = security_sb_remount(sb, data);
2327 if (err)
2328 return err;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 down_write(&sb->s_umount);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002331 if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002332 err = change_mount_flags(path->mnt, flags);
Al Viro57eccb82013-02-22 22:49:10 -05002333 else if (!capable(CAP_SYS_ADMIN))
2334 err = -EPERM;
Al Viro4aa98cf2009-05-08 13:36:58 -04002335 else
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002336 err = do_remount_sb(sb, flags, data, 0);
Al Viro7b43a792010-01-16 13:01:26 -05002337 if (!err) {
Al Viro719ea2f2013-09-29 11:24:49 -04002338 lock_mount_hash();
Eric W. Biedermana6138db2014-07-28 16:26:53 -07002339 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
Al Viro143c8c92011-11-25 00:46:35 -05002340 mnt->mnt.mnt_flags = mnt_flags;
Al Viro143c8c92011-11-25 00:46:35 -05002341 touch_mnt_namespace(mnt->mnt_ns);
Al Viro719ea2f2013-09-29 11:24:49 -04002342 unlock_mount_hash();
Dan Williams0e55a7c2008-09-26 19:01:20 -07002343 }
Al Viro6339dab2013-09-16 22:41:01 -04002344 up_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return err;
2346}
2347
Al Virocbbe3622011-11-24 20:01:19 -05002348static inline int tree_contains_unbindable(struct mount *mnt)
Ram Pai9676f0c2005-11-07 17:21:20 -05002349{
Al Viro315fc832011-11-24 18:57:30 -05002350 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05002351 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05002352 if (IS_MNT_UNBINDABLE(p))
Ram Pai9676f0c2005-11-07 17:21:20 -05002353 return 1;
2354 }
2355 return 0;
2356}
2357
Al Viro808d4e32012-10-11 11:42:01 -04002358static int do_move_mount(struct path *path, const char *old_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Al Viro2d92ab32008-08-02 00:51:11 -04002360 struct path old_path, parent_path;
Al Viro676da582011-11-24 21:47:05 -05002361 struct mount *p;
Al Viro0fb54e52011-11-24 19:59:16 -05002362 struct mount *old;
Al Viro84d17192013-03-15 10:53:28 -04002363 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05002364 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!old_name || !*old_name)
2366 return -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04002367 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (err)
2369 return err;
2370
Al Viro84d17192013-03-15 10:53:28 -04002371 mp = lock_mount(path);
2372 err = PTR_ERR(mp);
2373 if (IS_ERR(mp))
David Howellscc53ce52011-01-14 18:45:26 +00002374 goto out;
2375
Al Viro143c8c92011-11-25 00:46:35 -05002376 old = real_mount(old_path.mnt);
Al Virofc7be132011-11-25 01:05:37 -05002377 p = real_mount(path->mnt);
Al Viro143c8c92011-11-25 00:46:35 -05002378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05002380 if (!check_mnt(p) || !check_mnt(old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 goto out1;
2382
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002383 if (old->mnt.mnt_flags & MNT_LOCKED)
2384 goto out1;
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04002387 if (old_path.dentry != old_path.mnt->mnt_root)
Ram Pai21444402005-11-07 17:20:03 -05002388 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Al Viro676da582011-11-24 21:47:05 -05002390 if (!mnt_has_parent(old))
Ram Pai21444402005-11-07 17:20:03 -05002391 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
David Howellse36cb0b2015-01-29 12:02:35 +00002393 if (d_is_dir(path->dentry) !=
2394 d_is_dir(old_path.dentry))
Ram Pai21444402005-11-07 17:20:03 -05002395 goto out1;
2396 /*
2397 * Don't move a mount residing in a shared parent.
2398 */
Al Virofc7be132011-11-25 01:05:37 -05002399 if (IS_MNT_SHARED(old->mnt_parent))
Ram Pai21444402005-11-07 17:20:03 -05002400 goto out1;
Ram Pai9676f0c2005-11-07 17:21:20 -05002401 /*
2402 * Don't move a mount tree containing unbindable mounts to a destination
2403 * mount which is shared.
2404 */
Al Virofc7be132011-11-25 01:05:37 -05002405 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
Ram Pai9676f0c2005-11-07 17:21:20 -05002406 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 err = -ELOOP;
Al Virofc7be132011-11-25 01:05:37 -05002408 for (; mnt_has_parent(p); p = p->mnt_parent)
Al Viro676da582011-11-24 21:47:05 -05002409 if (p == old)
Ram Pai21444402005-11-07 17:20:03 -05002410 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Al Viro84d17192013-03-15 10:53:28 -04002412 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
Jan Blunck4ac91372008-02-14 19:34:32 -08002413 if (err)
Ram Pai21444402005-11-07 17:20:03 -05002414 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 /* if the mount is moved, it should no longer be expire
2417 * automatically */
Al Viro6776db3d2011-11-25 00:22:05 -05002418 list_del_init(&old->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419out1:
Al Viro84d17192013-03-15 10:53:28 -04002420 unlock_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (!err)
Al Viro1a390682008-03-21 20:48:19 -04002423 path_put(&parent_path);
Al Viro2d92ab32008-08-02 00:51:11 -04002424 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return err;
2426}
2427
Al Viro9d412a42011-03-17 22:08:28 -04002428static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
2429{
2430 int err;
2431 const char *subtype = strchr(fstype, '.');
2432 if (subtype) {
2433 subtype++;
2434 err = -EINVAL;
2435 if (!subtype[0])
2436 goto err;
2437 } else
2438 subtype = "";
2439
2440 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
2441 err = -ENOMEM;
2442 if (!mnt->mnt_sb->s_subtype)
2443 goto err;
2444 return mnt;
2445
2446 err:
2447 mntput(mnt);
2448 return ERR_PTR(err);
2449}
2450
Al Viro9d412a42011-03-17 22:08:28 -04002451/*
2452 * add a mount into a namespace's mount tree
2453 */
Al Viro95bc5f22011-11-25 00:30:56 -05002454static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
Al Viro9d412a42011-03-17 22:08:28 -04002455{
Al Viro84d17192013-03-15 10:53:28 -04002456 struct mountpoint *mp;
2457 struct mount *parent;
Al Viro9d412a42011-03-17 22:08:28 -04002458 int err;
2459
Al Virof2ebb3a2014-02-27 09:35:45 -05002460 mnt_flags &= ~MNT_INTERNAL_FLAGS;
Al Viro9d412a42011-03-17 22:08:28 -04002461
Al Viro84d17192013-03-15 10:53:28 -04002462 mp = lock_mount(path);
2463 if (IS_ERR(mp))
2464 return PTR_ERR(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002465
Al Viro84d17192013-03-15 10:53:28 -04002466 parent = real_mount(path->mnt);
Al Viro9d412a42011-03-17 22:08:28 -04002467 err = -EINVAL;
Al Viro84d17192013-03-15 10:53:28 -04002468 if (unlikely(!check_mnt(parent))) {
Al Viro156cacb2012-09-21 08:19:02 -04002469 /* that's acceptable only for automounts done in private ns */
2470 if (!(mnt_flags & MNT_SHRINKABLE))
2471 goto unlock;
2472 /* ... and for those we'd better have mountpoint still alive */
Al Viro84d17192013-03-15 10:53:28 -04002473 if (!parent->mnt_ns)
Al Viro156cacb2012-09-21 08:19:02 -04002474 goto unlock;
2475 }
Al Viro9d412a42011-03-17 22:08:28 -04002476
2477 /* Refuse the same filesystem on the same mount point */
2478 err = -EBUSY;
Al Viro95bc5f22011-11-25 00:30:56 -05002479 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
Al Viro9d412a42011-03-17 22:08:28 -04002480 path->mnt->mnt_root == path->dentry)
2481 goto unlock;
2482
2483 err = -EINVAL;
David Howellse36cb0b2015-01-29 12:02:35 +00002484 if (d_is_symlink(newmnt->mnt.mnt_root))
Al Viro9d412a42011-03-17 22:08:28 -04002485 goto unlock;
2486
Al Viro95bc5f22011-11-25 00:30:56 -05002487 newmnt->mnt.mnt_flags = mnt_flags;
Al Viro84d17192013-03-15 10:53:28 -04002488 err = graft_tree(newmnt, parent, mp);
Al Viro9d412a42011-03-17 22:08:28 -04002489
2490unlock:
Al Viro84d17192013-03-15 10:53:28 -04002491 unlock_mount(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002492 return err;
2493}
Al Virob1e75df2011-01-17 01:47:59 -05002494
Eric W. Biederman8654df42016-06-09 16:06:06 -05002495static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
Eric W. Biederman1b852bc2015-05-08 23:22:29 -05002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497/*
2498 * create a new mount for userspace and request it to be added into the
2499 * namespace's tree
2500 */
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002501static int do_new_mount(struct path *path, const char *fstype, int flags,
Al Viro808d4e32012-10-11 11:42:01 -04002502 int mnt_flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002504 struct file_system_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct vfsmount *mnt;
Al Viro15f9a3f2011-01-17 01:41:58 -05002506 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002508 if (!fstype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return -EINVAL;
2510
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002511 type = get_fs_type(fstype);
2512 if (!type)
2513 return -ENODEV;
2514
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002515 mnt = vfs_kern_mount(type, flags, name, data);
2516 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2517 !mnt->mnt_sb->s_subtype)
2518 mnt = fs_set_subtype(mnt, fstype);
2519
2520 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (IS_ERR(mnt))
2522 return PTR_ERR(mnt);
2523
Eric W. Biederman8654df42016-06-09 16:06:06 -05002524 if (mount_too_revealing(mnt, &mnt_flags)) {
2525 mntput(mnt);
2526 return -EPERM;
2527 }
2528
Al Viro95bc5f22011-11-25 00:30:56 -05002529 err = do_add_mount(real_mount(mnt), path, mnt_flags);
Al Viro15f9a3f2011-01-17 01:41:58 -05002530 if (err)
2531 mntput(mnt);
2532 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
Al Viro19a167a2011-01-17 01:35:23 -05002535int finish_automount(struct vfsmount *m, struct path *path)
2536{
Al Viro6776db3d2011-11-25 00:22:05 -05002537 struct mount *mnt = real_mount(m);
Al Viro19a167a2011-01-17 01:35:23 -05002538 int err;
2539 /* The new mount record should have at least 2 refs to prevent it being
2540 * expired before we get a chance to add it
2541 */
Al Viro6776db3d2011-11-25 00:22:05 -05002542 BUG_ON(mnt_get_count(mnt) < 2);
Al Viro19a167a2011-01-17 01:35:23 -05002543
2544 if (m->mnt_sb == path->mnt->mnt_sb &&
2545 m->mnt_root == path->dentry) {
Al Virob1e75df2011-01-17 01:47:59 -05002546 err = -ELOOP;
2547 goto fail;
Al Viro19a167a2011-01-17 01:35:23 -05002548 }
2549
Al Viro95bc5f22011-11-25 00:30:56 -05002550 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
Al Virob1e75df2011-01-17 01:47:59 -05002551 if (!err)
2552 return 0;
2553fail:
2554 /* remove m from any expiration list it may be on */
Al Viro6776db3d2011-11-25 00:22:05 -05002555 if (!list_empty(&mnt->mnt_expire)) {
Al Viro97216be2013-03-16 15:12:40 -04002556 namespace_lock();
Al Viro6776db3d2011-11-25 00:22:05 -05002557 list_del_init(&mnt->mnt_expire);
Al Viro97216be2013-03-16 15:12:40 -04002558 namespace_unlock();
Al Viro19a167a2011-01-17 01:35:23 -05002559 }
Al Virob1e75df2011-01-17 01:47:59 -05002560 mntput(m);
2561 mntput(m);
Al Viro19a167a2011-01-17 01:35:23 -05002562 return err;
2563}
2564
David Howellsea5b7782011-01-14 19:10:03 +00002565/**
2566 * mnt_set_expiry - Put a mount on an expiration list
2567 * @mnt: The mount to list.
2568 * @expiry_list: The list to add the mount to.
2569 */
2570void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2571{
Al Viro97216be2013-03-16 15:12:40 -04002572 namespace_lock();
David Howellsea5b7782011-01-14 19:10:03 +00002573
Al Viro6776db3d2011-11-25 00:22:05 -05002574 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
David Howellsea5b7782011-01-14 19:10:03 +00002575
Al Viro97216be2013-03-16 15:12:40 -04002576 namespace_unlock();
David Howellsea5b7782011-01-14 19:10:03 +00002577}
2578EXPORT_SYMBOL(mnt_set_expiry);
2579
2580/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 * process a list of expirable mountpoints with the intent of discarding any
2582 * mountpoints that aren't in use and haven't been touched since last we came
2583 * here
2584 */
2585void mark_mounts_for_expiry(struct list_head *mounts)
2586{
Al Viro761d5c32011-11-24 21:07:43 -05002587 struct mount *mnt, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 LIST_HEAD(graveyard);
2589
2590 if (list_empty(mounts))
2591 return;
2592
Al Viro97216be2013-03-16 15:12:40 -04002593 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04002594 lock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 /* extract from the expiration list every vfsmount that matches the
2597 * following criteria:
2598 * - only referenced by its parent vfsmount
2599 * - still marked for expiry (marked on the last call here; marks are
2600 * cleared by mntput())
2601 */
Al Viro6776db3d2011-11-25 00:22:05 -05002602 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Al Viro863d6842011-11-25 00:57:42 -05002603 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Viro1ab59732011-11-24 21:35:16 -05002604 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 continue;
Al Viro6776db3d2011-11-25 00:22:05 -05002606 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
Al Virobcc5c7d2008-03-22 00:21:53 -04002608 while (!list_empty(&graveyard)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002609 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002610 touch_mnt_namespace(mnt->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06002611 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04002612 }
Al Viro719ea2f2013-09-29 11:24:49 -04002613 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04002614 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2618
2619/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04002620 * Ripoff of 'select_parent()'
2621 *
2622 * search the list of submounts for a given mountpoint, and move any
2623 * shrinkable submounts to the 'graveyard' list.
2624 */
Al Viro692afc32011-11-24 21:15:14 -05002625static int select_submounts(struct mount *parent, struct list_head *graveyard)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002626{
Al Viro692afc32011-11-24 21:15:14 -05002627 struct mount *this_parent = parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002628 struct list_head *next;
2629 int found = 0;
2630
2631repeat:
Al Viro6b41d532011-11-24 23:24:33 -05002632 next = this_parent->mnt_mounts.next;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002633resume:
Al Viro6b41d532011-11-24 23:24:33 -05002634 while (next != &this_parent->mnt_mounts) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002635 struct list_head *tmp = next;
Al Viro6b41d532011-11-24 23:24:33 -05002636 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002637
2638 next = tmp->next;
Al Viro692afc32011-11-24 21:15:14 -05002639 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
Trond Myklebust5528f9112006-06-09 09:34:17 -04002640 continue;
2641 /*
2642 * Descend a level if the d_mounts list is non-empty.
2643 */
Al Viro6b41d532011-11-24 23:24:33 -05002644 if (!list_empty(&mnt->mnt_mounts)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002645 this_parent = mnt;
2646 goto repeat;
2647 }
2648
Al Viro1ab59732011-11-24 21:35:16 -05002649 if (!propagate_mount_busy(mnt, 1)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002650 list_move_tail(&mnt->mnt_expire, graveyard);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002651 found++;
2652 }
2653 }
2654 /*
2655 * All done at this level ... ascend and resume the search
2656 */
2657 if (this_parent != parent) {
Al Viro6b41d532011-11-24 23:24:33 -05002658 next = this_parent->mnt_child.next;
Al Viro0714a532011-11-24 22:19:58 -05002659 this_parent = this_parent->mnt_parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002660 goto resume;
2661 }
2662 return found;
2663}
2664
2665/*
2666 * process a list of expirable mountpoints with the intent of discarding any
2667 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10002668 *
Al Viro48a066e2013-09-29 22:06:07 -04002669 * mount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04002670 */
Al Virob54b9be2013-03-16 14:39:34 -04002671static void shrink_submounts(struct mount *mnt)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002672{
2673 LIST_HEAD(graveyard);
Al Viro761d5c32011-11-24 21:07:43 -05002674 struct mount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002675
Trond Myklebust5528f9112006-06-09 09:34:17 -04002676 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04002677 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04002678 while (!list_empty(&graveyard)) {
Al Viro761d5c32011-11-24 21:07:43 -05002679 m = list_first_entry(&graveyard, struct mount,
Al Viro6776db3d2011-11-25 00:22:05 -05002680 mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002681 touch_mnt_namespace(m->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06002682 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04002683 }
2684 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04002685}
2686
Trond Myklebust5528f9112006-06-09 09:34:17 -04002687/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 * Some copy_from_user() implementations do not return the exact number of
2689 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2690 * Note that this function differs from copy_from_user() in that it will oops
2691 * on bad values of `to', rather than returning a short copy.
2692 */
Ram Paib58fed82005-11-07 17:16:09 -05002693static long exact_copy_from_user(void *to, const void __user * from,
2694 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
2696 char *t = to;
2697 const char __user *f = from;
2698 char c;
2699
2700 if (!access_ok(VERIFY_READ, from, n))
2701 return n;
2702
2703 while (n) {
2704 if (__get_user(c, f)) {
2705 memset(t, 0, n);
2706 break;
2707 }
2708 *t++ = c;
2709 f++;
2710 n--;
2711 }
2712 return n;
2713}
2714
Al Virob40ef862015-12-14 18:44:44 -05002715void *copy_mount_options(const void __user * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
2717 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 unsigned long size;
Al Virob40ef862015-12-14 18:44:44 -05002719 char *copy;
Ram Paib58fed82005-11-07 17:16:09 -05002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (!data)
Al Virob40ef862015-12-14 18:44:44 -05002722 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Al Virob40ef862015-12-14 18:44:44 -05002724 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2725 if (!copy)
2726 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 /* We only care that *some* data at the address the user
2729 * gave us is valid. Just in case, we'll zero
2730 * the remainder of the page.
2731 */
2732 /* copy_from_user cannot cross TASK_SIZE ! */
2733 size = TASK_SIZE - (unsigned long)data;
2734 if (size > PAGE_SIZE)
2735 size = PAGE_SIZE;
2736
Al Virob40ef862015-12-14 18:44:44 -05002737 i = size - exact_copy_from_user(copy, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (!i) {
Al Virob40ef862015-12-14 18:44:44 -05002739 kfree(copy);
2740 return ERR_PTR(-EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742 if (i != PAGE_SIZE)
Al Virob40ef862015-12-14 18:44:44 -05002743 memset(copy + i, 0, PAGE_SIZE - i);
2744 return copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Tim Gardnerb8850d12014-08-28 11:26:03 -06002747char *copy_mount_string(const void __user *data)
Vegard Nossumeca6f532009-09-18 13:05:45 -07002748{
Tim Gardnerb8850d12014-08-28 11:26:03 -06002749 return data ? strndup_user(data, PAGE_SIZE) : NULL;
Vegard Nossumeca6f532009-09-18 13:05:45 -07002750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752/*
2753 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2754 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2755 *
2756 * data is a (void *) that can point to any structure up to
2757 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2758 * information (or be NULL).
2759 *
2760 * Pre-0.97 versions of mount() didn't have a flags word.
2761 * When the flags word was introduced its top half was required
2762 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2763 * Therefore, if this magic number is present, it carries no information
2764 * and must be discarded.
2765 */
Seunghun Lee5e6123f2014-09-14 22:15:10 +09002766long do_mount(const char *dev_name, const char __user *dir_name,
Al Viro808d4e32012-10-11 11:42:01 -04002767 const char *type_page, unsigned long flags, void *data_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Al Viro2d92ab32008-08-02 00:51:11 -04002769 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 int retval = 0;
2771 int mnt_flags = 0;
2772
2773 /* Discard magic */
2774 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2775 flags &= ~MS_MGC_MSK;
2776
2777 /* Basic sanity checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (data_page)
2779 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2780
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002781 /* ... and get the mountpoint */
Seunghun Lee5e6123f2014-09-14 22:15:10 +09002782 retval = user_path(dir_name, &path);
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002783 if (retval)
2784 return retval;
2785
2786 retval = security_sb_mount(dev_name, &path,
2787 type_page, flags, data_page);
Al Viro0d5cadb2013-05-04 14:40:51 -04002788 if (!retval && !may_mount())
2789 retval = -EPERM;
Jeff Layton9e8925b2015-11-16 09:49:34 -05002790 if (!retval && (flags & MS_MANDLOCK) && !may_mandlock())
2791 retval = -EPERM;
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002792 if (retval)
2793 goto dput_out;
2794
Andi Kleen613cbe32009-04-19 18:40:43 +02002795 /* Default to relatime unless overriden */
2796 if (!(flags & MS_NOATIME))
2797 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /* Separate the per-mountpoint flags */
2800 if (flags & MS_NOSUID)
2801 mnt_flags |= MNT_NOSUID;
2802 if (flags & MS_NODEV)
2803 mnt_flags |= MNT_NODEV;
2804 if (flags & MS_NOEXEC)
2805 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002806 if (flags & MS_NOATIME)
2807 mnt_flags |= MNT_NOATIME;
2808 if (flags & MS_NODIRATIME)
2809 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00002810 if (flags & MS_STRICTATIME)
2811 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002812 if (flags & MS_RDONLY)
2813 mnt_flags |= MNT_READONLY;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002814
Eric W. Biedermanffbc6f02014-07-28 17:36:04 -07002815 /* The default atime for remount is preservation */
2816 if ((flags & MS_REMOUNT) &&
2817 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2818 MS_STRICTATIME)) == 0)) {
2819 mnt_flags &= ~MNT_ATIME_MASK;
2820 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2821 }
2822
Al Viro7a4dec52010-08-09 12:05:43 -04002823 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
Matthew Garrettd0adde52009-03-26 17:49:56 +00002824 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13002825 MS_STRICTATIME | MS_NOREMOTELOCK | MS_SUBMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (flags & MS_REMOUNT)
Al Viro2d92ab32008-08-02 00:51:11 -04002828 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 data_page);
2830 else if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002831 retval = do_loopback(&path, dev_name, flags & MS_REC);
Ram Pai9676f0c2005-11-07 17:21:20 -05002832 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Al Viro2d92ab32008-08-02 00:51:11 -04002833 retval = do_change_type(&path, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 else if (flags & MS_MOVE)
Al Viro2d92ab32008-08-02 00:51:11 -04002835 retval = do_move_mount(&path, dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 else
Al Viro2d92ab32008-08-02 00:51:11 -04002837 retval = do_new_mount(&path, type_page, flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 dev_name, data_page);
2839dput_out:
Al Viro2d92ab32008-08-02 00:51:11 -04002840 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 return retval;
2842}
2843
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002844static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
2845{
2846 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
2847}
2848
2849static void dec_mnt_namespaces(struct ucounts *ucounts)
2850{
2851 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
2852}
2853
Eric W. Biederman771b1372012-07-26 21:08:32 -07002854static void free_mnt_ns(struct mnt_namespace *ns)
2855{
Al Viro6344c432014-11-01 00:45:45 -04002856 ns_free_inum(&ns->ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002857 dec_mnt_namespaces(ns->ucounts);
Eric W. Biederman771b1372012-07-26 21:08:32 -07002858 put_user_ns(ns->user_ns);
2859 kfree(ns);
2860}
2861
Eric W. Biederman8823c072010-03-07 18:49:36 -08002862/*
2863 * Assign a sequence number so we can detect when we attempt to bind
2864 * mount a reference to an older mount namespace into the current
2865 * mount namespace, preventing reference counting loops. A 64bit
2866 * number incrementing at 10Ghz will take 12,427 years to wrap which
2867 * is effectively never, so we can ignore the possibility.
2868 */
2869static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2870
Eric W. Biederman771b1372012-07-26 21:08:32 -07002871static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002872{
2873 struct mnt_namespace *new_ns;
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002874 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002875 int ret;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002876
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002877 ucounts = inc_mnt_namespaces(user_ns);
2878 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -05002879 return ERR_PTR(-ENOSPC);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002880
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002881 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002882 if (!new_ns) {
2883 dec_mnt_namespaces(ucounts);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002884 return ERR_PTR(-ENOMEM);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002885 }
Al Viro6344c432014-11-01 00:45:45 -04002886 ret = ns_alloc_inum(&new_ns->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002887 if (ret) {
2888 kfree(new_ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002889 dec_mnt_namespaces(ucounts);
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002890 return ERR_PTR(ret);
2891 }
Al Viro33c42942014-11-01 02:32:53 -04002892 new_ns->ns.ops = &mntns_operations;
Eric W. Biederman8823c072010-03-07 18:49:36 -08002893 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002894 atomic_set(&new_ns->count, 1);
2895 new_ns->root = NULL;
2896 INIT_LIST_HEAD(&new_ns->list);
2897 init_waitqueue_head(&new_ns->poll);
2898 new_ns->event = 0;
Eric W. Biederman771b1372012-07-26 21:08:32 -07002899 new_ns->user_ns = get_user_ns(user_ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002900 new_ns->ucounts = ucounts;
Eric W. Biedermand2921682016-09-28 00:27:17 -05002901 new_ns->mounts = 0;
2902 new_ns->pending_mounts = 0;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002903 return new_ns;
2904}
2905
Emese Revfy0766f782016-06-20 20:42:34 +02002906__latent_entropy
Al Viro9559f682013-09-28 20:47:57 -04002907struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2908 struct user_namespace *user_ns, struct fs_struct *new_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002910 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04002911 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Al Viro315fc832011-11-24 18:57:30 -05002912 struct mount *p, *q;
Al Viro9559f682013-09-28 20:47:57 -04002913 struct mount *old;
Al Virocb338d02011-11-24 20:55:08 -05002914 struct mount *new;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002915 int copy_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Al Viro9559f682013-09-28 20:47:57 -04002917 BUG_ON(!ns);
2918
2919 if (likely(!(flags & CLONE_NEWNS))) {
2920 get_mnt_ns(ns);
2921 return ns;
2922 }
2923
2924 old = ns->root;
2925
Eric W. Biederman771b1372012-07-26 21:08:32 -07002926 new_ns = alloc_mnt_ns(user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002927 if (IS_ERR(new_ns))
2928 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Al Viro97216be2013-03-16 15:12:40 -04002930 namespace_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 /* First pass: copy the tree topology */
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002932 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
Al Viro9559f682013-09-28 20:47:57 -04002933 if (user_ns != ns->user_ns)
Eric W. Biederman132c94e2013-03-22 04:08:05 -07002934 copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002935 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
David Howellsbe34d1a2012-06-25 12:55:18 +01002936 if (IS_ERR(new)) {
Al Viro328e6d92013-03-16 14:49:45 -04002937 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07002938 free_mnt_ns(new_ns);
David Howellsbe34d1a2012-06-25 12:55:18 +01002939 return ERR_CAST(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
Al Virobe08d6d2011-12-06 13:32:36 -05002941 new_ns->root = new;
Al Viro1a4eeaf2011-11-25 02:19:55 -05002942 list_add_tail(&new_ns->list, &new->mnt_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 /*
2945 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2946 * as belonging to new namespace. We have already acquired a private
2947 * fs_struct, so tsk->fs->lock is not needed.
2948 */
Al Viro909b0a82011-11-25 03:06:56 -05002949 p = old;
Al Virocb338d02011-11-24 20:55:08 -05002950 q = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 while (p) {
Al Viro143c8c92011-11-25 00:46:35 -05002952 q->mnt_ns = new_ns;
Eric W. Biedermand2921682016-09-28 00:27:17 -05002953 new_ns->mounts++;
Al Viro9559f682013-09-28 20:47:57 -04002954 if (new_fs) {
2955 if (&p->mnt == new_fs->root.mnt) {
2956 new_fs->root.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05002957 rootmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
Al Viro9559f682013-09-28 20:47:57 -04002959 if (&p->mnt == new_fs->pwd.mnt) {
2960 new_fs->pwd.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05002961 pwdmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
Al Viro909b0a82011-11-25 03:06:56 -05002964 p = next_mnt(p, old);
2965 q = next_mnt(q, new);
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002966 if (!q)
2967 break;
2968 while (p->mnt.mnt_root != q->mnt.mnt_root)
2969 p = next_mnt(p, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Al Viro328e6d92013-03-16 14:49:45 -04002971 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (rootmnt)
Al Virof03c6592011-01-14 22:30:21 -05002974 mntput(rootmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (pwdmnt)
Al Virof03c6592011-01-14 22:30:21 -05002976 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
JANAK DESAI741a2952006-02-07 12:59:00 -08002978 return new_ns;
2979}
2980
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002981/**
2982 * create_mnt_ns - creates a private namespace and adds a root filesystem
2983 * @mnt: pointer to the new root filesystem mountpoint
2984 */
Al Viro1a4eeaf2011-11-25 02:19:55 -05002985static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002986{
Eric W. Biederman771b1372012-07-26 21:08:32 -07002987 struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002988 if (!IS_ERR(new_ns)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002989 struct mount *mnt = real_mount(m);
2990 mnt->mnt_ns = new_ns;
Al Virobe08d6d2011-12-06 13:32:36 -05002991 new_ns->root = mnt;
Eric W. Biedermand2921682016-09-28 00:27:17 -05002992 new_ns->mounts++;
Al Virob1983cd2013-05-04 15:18:53 -04002993 list_add(&mnt->mnt_list, &new_ns->list);
Al Viroc1334492011-11-16 16:12:14 -05002994 } else {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002995 mntput(m);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002996 }
2997 return new_ns;
2998}
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002999
Al Viroea441d12011-11-16 21:43:59 -05003000struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
3001{
3002 struct mnt_namespace *ns;
Al Virod31da0f2011-11-22 12:31:21 -05003003 struct super_block *s;
Al Viroea441d12011-11-16 21:43:59 -05003004 struct path path;
3005 int err;
3006
3007 ns = create_mnt_ns(mnt);
3008 if (IS_ERR(ns))
3009 return ERR_CAST(ns);
3010
3011 err = vfs_path_lookup(mnt->mnt_root, mnt,
3012 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3013
3014 put_mnt_ns(ns);
3015
3016 if (err)
3017 return ERR_PTR(err);
3018
3019 /* trade a vfsmount reference for active sb one */
Al Virod31da0f2011-11-22 12:31:21 -05003020 s = path.mnt->mnt_sb;
3021 atomic_inc(&s->s_active);
Al Viroea441d12011-11-16 21:43:59 -05003022 mntput(path.mnt);
3023 /* lock the sucker */
Al Virod31da0f2011-11-22 12:31:21 -05003024 down_write(&s->s_umount);
Al Viroea441d12011-11-16 21:43:59 -05003025 /* ... and return the root of (sub)tree on it */
3026 return path.dentry;
3027}
3028EXPORT_SYMBOL(mount_subtree);
3029
Heiko Carstensbdc480e2009-01-14 14:14:12 +01003030SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3031 char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Vegard Nossumeca6f532009-09-18 13:05:45 -07003033 int ret;
3034 char *kernel_type;
Vegard Nossumeca6f532009-09-18 13:05:45 -07003035 char *kernel_dev;
Al Virob40ef862015-12-14 18:44:44 -05003036 void *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Tim Gardnerb8850d12014-08-28 11:26:03 -06003038 kernel_type = copy_mount_string(type);
3039 ret = PTR_ERR(kernel_type);
3040 if (IS_ERR(kernel_type))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003041 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Tim Gardnerb8850d12014-08-28 11:26:03 -06003043 kernel_dev = copy_mount_string(dev_name);
3044 ret = PTR_ERR(kernel_dev);
3045 if (IS_ERR(kernel_dev))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003046 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Al Virob40ef862015-12-14 18:44:44 -05003048 options = copy_mount_options(data);
3049 ret = PTR_ERR(options);
3050 if (IS_ERR(options))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003051 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Al Virob40ef862015-12-14 18:44:44 -05003053 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003054
Al Virob40ef862015-12-14 18:44:44 -05003055 kfree(options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003056out_data:
3057 kfree(kernel_dev);
3058out_dev:
Vegard Nossumeca6f532009-09-18 13:05:45 -07003059 kfree(kernel_type);
3060out_type:
3061 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
3064/*
Al Viroafac7cb2011-11-23 19:34:49 -05003065 * Return true if path is reachable from root
3066 *
Al Viro48a066e2013-09-29 22:06:07 -04003067 * namespace_sem or mount_lock is held
Al Viroafac7cb2011-11-23 19:34:49 -05003068 */
Al Viro643822b2011-11-24 22:00:28 -05003069bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
Al Viroafac7cb2011-11-23 19:34:49 -05003070 const struct path *root)
3071{
Al Viro643822b2011-11-24 22:00:28 -05003072 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
Al Viroa73324d2011-11-24 22:25:07 -05003073 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05003074 mnt = mnt->mnt_parent;
Al Viroafac7cb2011-11-23 19:34:49 -05003075 }
Al Viro643822b2011-11-24 22:00:28 -05003076 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
Al Viroafac7cb2011-11-23 19:34:49 -05003077}
3078
Mickaël Salaün640eb7e2016-11-14 22:14:35 +01003079bool path_is_under(const struct path *path1, const struct path *path2)
Al Viroafac7cb2011-11-23 19:34:49 -05003080{
Yaowei Bai25ab4c92015-11-17 14:40:10 +08003081 bool res;
Al Viro48a066e2013-09-29 22:06:07 -04003082 read_seqlock_excl(&mount_lock);
Al Viro643822b2011-11-24 22:00:28 -05003083 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
Al Viro48a066e2013-09-29 22:06:07 -04003084 read_sequnlock_excl(&mount_lock);
Al Viroafac7cb2011-11-23 19:34:49 -05003085 return res;
3086}
3087EXPORT_SYMBOL(path_is_under);
3088
3089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 * pivot_root Semantics:
3091 * Moves the root file system of the current process to the directory put_old,
3092 * makes new_root as the new root file system of the current process, and sets
3093 * root/cwd of all processes which had them on the current root to new_root.
3094 *
3095 * Restrictions:
3096 * The new_root and put_old must be directories, and must not be on the
3097 * same file system as the current process root. The put_old must be
3098 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3099 * pointed to by put_old must yield the same directory as new_root. No other
3100 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3101 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08003102 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
3103 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
3104 * in this situation.
3105 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 * Notes:
3107 * - we don't move root/cwd if they are not at the root (reason: if something
3108 * cared enough to change them, it's probably wrong to force them elsewhere)
3109 * - it's okay to pick a root that isn't the root of a file system, e.g.
3110 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3111 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3112 * first.
3113 */
Heiko Carstens3480b252009-01-14 14:14:16 +01003114SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3115 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Al Viro2d8f3032008-07-22 09:59:21 -04003117 struct path new, old, parent_path, root_parent, root;
Al Viro84d17192013-03-15 10:53:28 -04003118 struct mount *new_mnt, *root_mnt, *old_mnt;
3119 struct mountpoint *old_mp, *root_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 int error;
3121
Al Viro9b40bc92013-02-22 22:45:42 -05003122 if (!may_mount())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return -EPERM;
3124
Al Viro2d8f3032008-07-22 09:59:21 -04003125 error = user_path_dir(new_root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (error)
3127 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Al Viro2d8f3032008-07-22 09:59:21 -04003129 error = user_path_dir(put_old, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (error)
3131 goto out1;
3132
Al Viro2d8f3032008-07-22 09:59:21 -04003133 error = security_sb_pivotroot(&old, &new);
Al Virob12cea92011-03-18 08:55:38 -04003134 if (error)
3135 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02003137 get_fs_root(current->fs, &root);
Al Viro84d17192013-03-15 10:53:28 -04003138 old_mp = lock_mount(&old);
3139 error = PTR_ERR(old_mp);
3140 if (IS_ERR(old_mp))
Al Virob12cea92011-03-18 08:55:38 -04003141 goto out3;
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 error = -EINVAL;
Al Viro419148d2011-11-24 19:41:16 -05003144 new_mnt = real_mount(new.mnt);
3145 root_mnt = real_mount(root.mnt);
Al Viro84d17192013-03-15 10:53:28 -04003146 old_mnt = real_mount(old.mnt);
3147 if (IS_MNT_SHARED(old_mnt) ||
Al Virofc7be132011-11-25 01:05:37 -05003148 IS_MNT_SHARED(new_mnt->mnt_parent) ||
3149 IS_MNT_SHARED(root_mnt->mnt_parent))
Al Virob12cea92011-03-18 08:55:38 -04003150 goto out4;
Al Viro143c8c92011-11-25 00:46:35 -05003151 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003152 goto out4;
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003153 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3154 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003156 if (d_unlinked(new.dentry))
Al Virob12cea92011-03-18 08:55:38 -04003157 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 error = -EBUSY;
Al Viro84d17192013-03-15 10:53:28 -04003159 if (new_mnt == root_mnt || old_mnt == root_mnt)
Al Virob12cea92011-03-18 08:55:38 -04003160 goto out4; /* loop, on the same file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04003162 if (root.mnt->mnt_root != root.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003163 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003164 if (!mnt_has_parent(root_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003165 goto out4; /* not attached */
Al Viro84d17192013-03-15 10:53:28 -04003166 root_mp = root_mnt->mnt_mp;
Al Viro2d8f3032008-07-22 09:59:21 -04003167 if (new.mnt->mnt_root != new.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003168 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003169 if (!mnt_has_parent(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003170 goto out4; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08003171 /* make sure we can reach put_old from new_root */
Al Viro84d17192013-03-15 10:53:28 -04003172 if (!is_path_reachable(old_mnt, old.dentry, &new))
Al Virob12cea92011-03-18 08:55:38 -04003173 goto out4;
Eric W. Biederman0d082602014-10-08 10:42:27 -07003174 /* make certain new is below the root */
3175 if (!is_path_reachable(new_mnt, new.dentry, &root))
3176 goto out4;
Al Viro84d17192013-03-15 10:53:28 -04003177 root_mp->m_count++; /* pin it so it won't go away */
Al Viro719ea2f2013-09-29 11:24:49 -04003178 lock_mount_hash();
Al Viro419148d2011-11-24 19:41:16 -05003179 detach_mnt(new_mnt, &parent_path);
3180 detach_mnt(root_mnt, &root_parent);
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003181 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3182 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3183 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3184 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003185 /* mount old root on put_old */
Al Viro84d17192013-03-15 10:53:28 -04003186 attach_mnt(root_mnt, old_mnt, old_mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08003187 /* mount new_root on / */
Al Viro84d17192013-03-15 10:53:28 -04003188 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003189 touch_mnt_namespace(current->nsproxy->mnt_ns);
Eric W. Biederman4fed6552014-10-08 10:42:57 -07003190 /* A moved mount should not expire automatically */
3191 list_del_init(&new_mnt->mnt_expire);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13003192 put_mountpoint(root_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04003193 unlock_mount_hash();
Al Viro2d8f3032008-07-22 09:59:21 -04003194 chroot_fs_refs(&root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 error = 0;
Al Virob12cea92011-03-18 08:55:38 -04003196out4:
Al Viro84d17192013-03-15 10:53:28 -04003197 unlock_mount(old_mp);
Al Virob12cea92011-03-18 08:55:38 -04003198 if (!error) {
3199 path_put(&root_parent);
3200 path_put(&parent_path);
3201 }
3202out3:
Al Viro8c3ee422008-03-22 18:00:39 -04003203 path_put(&root);
Al Virob12cea92011-03-18 08:55:38 -04003204out2:
Al Viro2d8f3032008-07-22 09:59:21 -04003205 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206out1:
Al Viro2d8f3032008-07-22 09:59:21 -04003207 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
3212static void __init init_mount_tree(void)
3213{
3214 struct vfsmount *mnt;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003215 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08003216 struct path root;
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003217 struct file_system_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003219 type = get_fs_type("rootfs");
3220 if (!type)
3221 panic("Can't find rootfs type");
3222 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
3223 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (IS_ERR(mnt))
3225 panic("Can't create rootfs");
Nick Pigginb3e19d92011-01-07 17:50:11 +11003226
Trond Myklebust3b22edc2009-06-23 17:29:49 -04003227 ns = create_mnt_ns(mnt);
3228 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 panic("Can't allocate initial namespace");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003231 init_task.nsproxy->mnt_ns = ns;
3232 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Al Virobe08d6d2011-12-06 13:32:36 -05003234 root.mnt = mnt;
3235 root.dentry = mnt->mnt_root;
Eric W. Biedermanda362b02014-10-07 12:19:53 -07003236 mnt->mnt_flags |= MNT_LOCKED;
Jan Blunckac748a02008-02-14 19:34:39 -08003237
3238 set_fs_pwd(current->fs, &root);
3239 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Denis Cheng74bf17c2007-10-16 23:26:30 -07003242void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Eric Dumazet13f14b42008-02-06 01:37:57 -08003244 unsigned u;
Randy Dunlap15a67dd2006-09-29 01:58:57 -07003245 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Al Viro7d6fec42011-11-23 12:14:10 -05003247 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
Paul Mundt20c2df82007-07-20 10:11:58 +09003248 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Al Viro0818bf22014-02-28 13:46:44 -05003250 mount_hashtable = alloc_large_system_hash("Mount-cache",
Al Viro38129a12014-03-20 21:10:51 -04003251 sizeof(struct hlist_head),
Al Viro0818bf22014-02-28 13:46:44 -05003252 mhash_entries, 19,
3253 0,
3254 &m_hash_shift, &m_hash_mask, 0, 0);
3255 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
3256 sizeof(struct hlist_head),
3257 mphash_entries, 19,
3258 0,
3259 &mp_hash_shift, &mp_hash_mask, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Al Viro84d17192013-03-15 10:53:28 -04003261 if (!mount_hashtable || !mountpoint_hashtable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 panic("Failed to allocate mount hash table\n");
3263
Al Viro0818bf22014-02-28 13:46:44 -05003264 for (u = 0; u <= m_hash_mask; u++)
Al Viro38129a12014-03-20 21:10:51 -04003265 INIT_HLIST_HEAD(&mount_hashtable[u]);
Al Viro0818bf22014-02-28 13:46:44 -05003266 for (u = 0; u <= mp_hash_mask; u++)
3267 INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Tejun Heo4b93dc92013-11-28 14:54:43 -05003269 kernfs_init();
3270
Randy Dunlap15a67dd2006-09-29 01:58:57 -07003271 err = sysfs_init();
3272 if (err)
3273 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07003274 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06003275 fs_kobj = kobject_create_and_add("fs", NULL);
3276 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07003277 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 init_rootfs();
3279 init_mount_tree();
3280}
3281
Trond Myklebust616511d2009-06-22 15:09:13 -04003282void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Al Virod498b252010-02-05 02:21:06 -05003284 if (!atomic_dec_and_test(&ns->count))
Trond Myklebust616511d2009-06-22 15:09:13 -04003285 return;
Al Viro7b00ed62013-09-16 21:19:20 -04003286 drop_collected_mounts(&ns->root->mnt);
Eric W. Biederman771b1372012-07-26 21:08:32 -07003287 free_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
Al Viro9d412a42011-03-17 22:08:28 -04003289
3290struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
3291{
Tim Chen423e0ab2011-07-19 09:32:38 -07003292 struct vfsmount *mnt;
3293 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
3294 if (!IS_ERR(mnt)) {
3295 /*
3296 * it is a longterm mount, don't release mnt until
3297 * we unmount before file sys is unregistered
3298 */
Al Virof7a99c52012-06-09 00:59:08 -04003299 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
Tim Chen423e0ab2011-07-19 09:32:38 -07003300 }
3301 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04003302}
3303EXPORT_SYMBOL_GPL(kern_mount_data);
Tim Chen423e0ab2011-07-19 09:32:38 -07003304
3305void kern_unmount(struct vfsmount *mnt)
3306{
3307 /* release long term mount so mount point can be released */
3308 if (!IS_ERR_OR_NULL(mnt)) {
Al Virof7a99c52012-06-09 00:59:08 -04003309 real_mount(mnt)->mnt_ns = NULL;
Al Viro48a066e2013-09-29 22:06:07 -04003310 synchronize_rcu(); /* yecchhh... */
Tim Chen423e0ab2011-07-19 09:32:38 -07003311 mntput(mnt);
3312 }
3313}
3314EXPORT_SYMBOL(kern_unmount);
Al Viro02125a82011-12-05 08:43:34 -05003315
3316bool our_mnt(struct vfsmount *mnt)
3317{
Al Viro143c8c92011-11-25 00:46:35 -05003318 return check_mnt(real_mount(mnt));
Al Viro02125a82011-12-05 08:43:34 -05003319}
Eric W. Biederman8823c072010-03-07 18:49:36 -08003320
Eric W. Biederman31515272013-03-15 01:45:51 -07003321bool current_chrooted(void)
3322{
3323 /* Does the current process have a non-standard root */
3324 struct path ns_root;
3325 struct path fs_root;
3326 bool chrooted;
3327
3328 /* Find the namespace root */
3329 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3330 ns_root.dentry = ns_root.mnt->mnt_root;
3331 path_get(&ns_root);
3332 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3333 ;
3334
3335 get_fs_root(current->fs, &fs_root);
3336
3337 chrooted = !path_equal(&fs_root, &ns_root);
3338
3339 path_put(&fs_root);
3340 path_put(&ns_root);
3341
3342 return chrooted;
3343}
3344
Eric W. Biederman8654df42016-06-09 16:06:06 -05003345static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
3346 int *new_mnt_flags)
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003347{
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003348 int new_flags = *new_mnt_flags;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003349 struct mount *mnt;
Eric W. Biedermane51db732013-03-30 19:57:41 -07003350 bool visible = false;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003351
Al Viro44bb4382013-09-16 21:37:36 -04003352 down_read(&namespace_sem);
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003353 list_for_each_entry(mnt, &ns->list, mnt_list) {
Eric W. Biedermane51db732013-03-30 19:57:41 -07003354 struct mount *child;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003355 int mnt_flags;
3356
Eric W. Biederman8654df42016-06-09 16:06:06 -05003357 if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
Eric W. Biedermane51db732013-03-30 19:57:41 -07003358 continue;
3359
Eric W. Biederman7e96c1b2015-05-08 16:36:50 -05003360 /* This mount is not fully visible if it's root directory
3361 * is not the root directory of the filesystem.
3362 */
3363 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
3364 continue;
3365
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003366 /* A local view of the mount flags */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003367 mnt_flags = mnt->mnt.mnt_flags;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003368
Eric W. Biederman695e9df2016-06-10 12:21:40 -05003369 /* Don't miss readonly hidden in the superblock flags */
3370 if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY)
3371 mnt_flags |= MNT_LOCK_READONLY;
3372
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003373 /* Verify the mount flags are equal to or more permissive
3374 * than the proposed new mount.
3375 */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003376 if ((mnt_flags & MNT_LOCK_READONLY) &&
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003377 !(new_flags & MNT_READONLY))
3378 continue;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003379 if ((mnt_flags & MNT_LOCK_ATIME) &&
3380 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003381 continue;
3382
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003383 /* This mount is not fully visible if there are any
3384 * locked child mounts that cover anything except for
3385 * empty directories.
Eric W. Biedermane51db732013-03-30 19:57:41 -07003386 */
3387 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3388 struct inode *inode = child->mnt_mountpoint->d_inode;
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003389 /* Only worry about locked mounts */
Eric W. Biedermand71ed6c2016-05-27 14:50:05 -05003390 if (!(child->mnt.mnt_flags & MNT_LOCKED))
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003391 continue;
Eric W. Biederman7236c852015-05-13 20:51:09 -05003392 /* Is the directory permanetly empty? */
3393 if (!is_empty_dir_inode(inode))
Eric W. Biedermane51db732013-03-30 19:57:41 -07003394 goto next;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003395 }
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003396 /* Preserve the locked attributes */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003397 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003398 MNT_LOCK_ATIME);
Eric W. Biedermane51db732013-03-30 19:57:41 -07003399 visible = true;
3400 goto found;
3401 next: ;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003402 }
Eric W. Biedermane51db732013-03-30 19:57:41 -07003403found:
Al Viro44bb4382013-09-16 21:37:36 -04003404 up_read(&namespace_sem);
Eric W. Biedermane51db732013-03-30 19:57:41 -07003405 return visible;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003406}
3407
Eric W. Biederman8654df42016-06-09 16:06:06 -05003408static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
3409{
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003410 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
Eric W. Biederman8654df42016-06-09 16:06:06 -05003411 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3412 unsigned long s_iflags;
3413
3414 if (ns->user_ns == &init_user_ns)
3415 return false;
3416
3417 /* Can this filesystem be too revealing? */
3418 s_iflags = mnt->mnt_sb->s_iflags;
3419 if (!(s_iflags & SB_I_USERNS_VISIBLE))
3420 return false;
3421
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003422 if ((s_iflags & required_iflags) != required_iflags) {
3423 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3424 required_iflags);
3425 return true;
3426 }
3427
Eric W. Biederman8654df42016-06-09 16:06:06 -05003428 return !mnt_already_visible(ns, mnt, new_mnt_flags);
3429}
3430
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05003431bool mnt_may_suid(struct vfsmount *mnt)
3432{
3433 /*
3434 * Foreign mounts (accessed via fchdir or through /proc
3435 * symlinks) are always treated as if they are nosuid. This
3436 * prevents namespaces from trusting potentially unsafe
3437 * suid/sgid bits, file caps, or security labels that originate
3438 * in other namespaces.
3439 */
3440 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3441 current_in_userns(mnt->mnt_sb->s_user_ns);
3442}
3443
Al Viro64964522014-11-01 00:37:32 -04003444static struct ns_common *mntns_get(struct task_struct *task)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003445{
Al Viro58be28252014-11-01 00:00:23 -04003446 struct ns_common *ns = NULL;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003447 struct nsproxy *nsproxy;
3448
Eric W. Biederman728dba32014-02-03 19:13:49 -08003449 task_lock(task);
3450 nsproxy = task->nsproxy;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003451 if (nsproxy) {
Al Viro58be28252014-11-01 00:00:23 -04003452 ns = &nsproxy->mnt_ns->ns;
3453 get_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08003454 }
Eric W. Biederman728dba32014-02-03 19:13:49 -08003455 task_unlock(task);
Eric W. Biederman8823c072010-03-07 18:49:36 -08003456
3457 return ns;
3458}
3459
Al Viro64964522014-11-01 00:37:32 -04003460static void mntns_put(struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003461{
Al Viro58be28252014-11-01 00:00:23 -04003462 put_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08003463}
3464
Al Viro64964522014-11-01 00:37:32 -04003465static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003466{
3467 struct fs_struct *fs = current->fs;
Al Viro58be28252014-11-01 00:00:23 -04003468 struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
Eric W. Biederman8823c072010-03-07 18:49:36 -08003469 struct path root;
3470
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003471 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07003472 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3473 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Zhao Hongjiangae11e0f2012-09-13 16:38:03 +08003474 return -EPERM;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003475
3476 if (fs->users != 1)
3477 return -EINVAL;
3478
3479 get_mnt_ns(mnt_ns);
3480 put_mnt_ns(nsproxy->mnt_ns);
3481 nsproxy->mnt_ns = mnt_ns;
3482
3483 /* Find the root */
3484 root.mnt = &mnt_ns->root->mnt;
3485 root.dentry = mnt_ns->root->mnt.mnt_root;
3486 path_get(&root);
3487 while(d_mountpoint(root.dentry) && follow_down_one(&root))
3488 ;
3489
3490 /* Update the pwd and root */
3491 set_fs_pwd(fs, &root);
3492 set_fs_root(fs, &root);
3493
3494 path_put(&root);
3495 return 0;
3496}
3497
Andrey Vaginbcac25a2016-09-06 00:47:13 -07003498static struct user_namespace *mntns_owner(struct ns_common *ns)
3499{
3500 return to_mnt_ns(ns)->user_ns;
3501}
3502
Eric W. Biederman8823c072010-03-07 18:49:36 -08003503const struct proc_ns_operations mntns_operations = {
3504 .name = "mnt",
3505 .type = CLONE_NEWNS,
3506 .get = mntns_get,
3507 .put = mntns_put,
3508 .install = mntns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07003509 .owner = mntns_owner,
Eric W. Biederman8823c072010-03-07 18:49:36 -08003510};