Teach shrink_dcache_parent() to cope with mixed-filesystem shrink lists
[linux-2.6-block.git] / fs / namespace.c
CommitLineData
59bd9ded 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/fs/namespace.c
4 *
5 * (C) Copyright Al Viro 2000, 2001
1da177e4
LT
6 *
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
10
1da177e4 11#include <linux/syscalls.h>
d10577a8 12#include <linux/export.h>
16f7e0fe 13#include <linux/capability.h>
6b3286ed 14#include <linux/mnt_namespace.h>
771b1371 15#include <linux/user_namespace.h>
1da177e4
LT
16#include <linux/namei.h>
17#include <linux/security.h>
5b825c3a 18#include <linux/cred.h>
73cd49ec 19#include <linux/idr.h>
57f150a5 20#include <linux/init.h> /* init_rootfs */
d10577a8
AV
21#include <linux/fs_struct.h> /* get_fs_root et.al. */
22#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
a07b2000 23#include <linux/file.h>
d10577a8 24#include <linux/uaccess.h>
0bb80f24 25#include <linux/proc_ns.h>
20b4fb48 26#include <linux/magic.h>
57c8a661 27#include <linux/memblock.h>
9ea459e1 28#include <linux/task_work.h>
9164bb4a 29#include <linux/sched/task.h>
e262e32d 30#include <uapi/linux/mount.h>
9bc61ab1 31#include <linux/fs_context.h>
9164bb4a 32
07b20889 33#include "pnode.h"
948730b0 34#include "internal.h"
1da177e4 35
d2921684
EB
36/* Maximum number of mounts in a mount namespace */
37unsigned int sysctl_mount_max __read_mostly = 100000;
38
0818bf27
AV
39static unsigned int m_hash_mask __read_mostly;
40static unsigned int m_hash_shift __read_mostly;
41static unsigned int mp_hash_mask __read_mostly;
42static unsigned int mp_hash_shift __read_mostly;
43
44static __initdata unsigned long mhash_entries;
45static int __init set_mhash_entries(char *str)
46{
47 if (!str)
48 return 0;
49 mhash_entries = simple_strtoul(str, &str, 0);
50 return 1;
51}
52__setup("mhash_entries=", set_mhash_entries);
53
54static __initdata unsigned long mphash_entries;
55static int __init set_mphash_entries(char *str)
56{
57 if (!str)
58 return 0;
59 mphash_entries = simple_strtoul(str, &str, 0);
60 return 1;
61}
62__setup("mphash_entries=", set_mphash_entries);
13f14b4d 63
c7999c36 64static u64 event;
73cd49ec 65static DEFINE_IDA(mnt_id_ida);
719f5d7f 66static DEFINE_IDA(mnt_group_ida);
1da177e4 67
38129a13 68static struct hlist_head *mount_hashtable __read_mostly;
0818bf27 69static struct hlist_head *mountpoint_hashtable __read_mostly;
e18b890b 70static struct kmem_cache *mnt_cache __read_mostly;
59aa0da8 71static DECLARE_RWSEM(namespace_sem);
1da177e4 72
f87fd4c2 73/* /sys/fs */
00d26666
GKH
74struct kobject *fs_kobj;
75EXPORT_SYMBOL_GPL(fs_kobj);
f87fd4c2 76
99b7db7b
NP
77/*
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 */
48a066e7 85__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
99b7db7b 86
38129a13 87static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 88{
b58fed8b
RP
89 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
90 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
0818bf27
AV
91 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];
1da177e4
LT
100}
101
b105e270 102static int mnt_alloc_id(struct mount *mnt)
73cd49ec 103{
169b480e
MW
104 int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
105
106 if (res < 0)
107 return res;
108 mnt->mnt_id = res;
109 return 0;
73cd49ec
MS
110}
111
b105e270 112static void mnt_free_id(struct mount *mnt)
73cd49ec 113{
169b480e 114 ida_free(&mnt_id_ida, mnt->mnt_id);
73cd49ec
MS
115}
116
719f5d7f
MS
117/*
118 * Allocate a new peer group ID
719f5d7f 119 */
4b8b21f4 120static int mnt_alloc_group_id(struct mount *mnt)
719f5d7f 121{
169b480e 122 int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
f21f6220 123
169b480e
MW
124 if (res < 0)
125 return res;
126 mnt->mnt_group_id = res;
127 return 0;
719f5d7f
MS
128}
129
130/*
131 * Release a peer group ID
132 */
4b8b21f4 133void mnt_release_group_id(struct mount *mnt)
719f5d7f 134{
169b480e 135 ida_free(&mnt_group_ida, mnt->mnt_group_id);
15169fe7 136 mnt->mnt_group_id = 0;
719f5d7f
MS
137}
138
b3e19d92
NP
139/*
140 * vfsmount lock must be held for read
141 */
83adc753 142static inline void mnt_add_count(struct mount *mnt, int n)
b3e19d92
NP
143{
144#ifdef CONFIG_SMP
68e8a9fe 145 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
b3e19d92
NP
146#else
147 preempt_disable();
68e8a9fe 148 mnt->mnt_count += n;
b3e19d92
NP
149 preempt_enable();
150#endif
151}
152
b3e19d92
NP
153/*
154 * vfsmount lock must be held for write
155 */
83adc753 156unsigned int mnt_get_count(struct mount *mnt)
b3e19d92
NP
157{
158#ifdef CONFIG_SMP
f03c6599 159 unsigned int count = 0;
b3e19d92
NP
160 int cpu;
161
162 for_each_possible_cpu(cpu) {
68e8a9fe 163 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
b3e19d92
NP
164 }
165
166 return count;
167#else
68e8a9fe 168 return mnt->mnt_count;
b3e19d92
NP
169#endif
170}
171
87b95ce0
AV
172static void drop_mountpoint(struct fs_pin *p)
173{
174 struct mount *m = container_of(p, struct mount, mnt_umount);
175 dput(m->mnt_ex_mountpoint);
176 pin_remove(p);
177 mntput(&m->mnt);
178}
179
b105e270 180static struct mount *alloc_vfsmnt(const char *name)
1da177e4 181{
c63181e6
AV
182 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
183 if (mnt) {
73cd49ec
MS
184 int err;
185
c63181e6 186 err = mnt_alloc_id(mnt);
88b38782
LZ
187 if (err)
188 goto out_free_cache;
189
190 if (name) {
fcc139ae 191 mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
c63181e6 192 if (!mnt->mnt_devname)
88b38782 193 goto out_free_id;
73cd49ec
MS
194 }
195
b3e19d92 196#ifdef CONFIG_SMP
c63181e6
AV
197 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
198 if (!mnt->mnt_pcp)
b3e19d92
NP
199 goto out_free_devname;
200
c63181e6 201 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
b3e19d92 202#else
c63181e6
AV
203 mnt->mnt_count = 1;
204 mnt->mnt_writers = 0;
b3e19d92
NP
205#endif
206
38129a13 207 INIT_HLIST_NODE(&mnt->mnt_hash);
c63181e6
AV
208 INIT_LIST_HEAD(&mnt->mnt_child);
209 INIT_LIST_HEAD(&mnt->mnt_mounts);
210 INIT_LIST_HEAD(&mnt->mnt_list);
211 INIT_LIST_HEAD(&mnt->mnt_expire);
212 INIT_LIST_HEAD(&mnt->mnt_share);
213 INIT_LIST_HEAD(&mnt->mnt_slave_list);
214 INIT_LIST_HEAD(&mnt->mnt_slave);
0a5eb7c8 215 INIT_HLIST_NODE(&mnt->mnt_mp_list);
99b19d16 216 INIT_LIST_HEAD(&mnt->mnt_umounting);
87b95ce0 217 init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
1da177e4 218 }
c63181e6 219 return mnt;
88b38782 220
d3ef3d73 221#ifdef CONFIG_SMP
222out_free_devname:
fcc139ae 223 kfree_const(mnt->mnt_devname);
d3ef3d73 224#endif
88b38782 225out_free_id:
c63181e6 226 mnt_free_id(mnt);
88b38782 227out_free_cache:
c63181e6 228 kmem_cache_free(mnt_cache, mnt);
88b38782 229 return NULL;
1da177e4
LT
230}
231
3d733633
DH
232/*
233 * Most r/o checks on a fs are for operations that take
234 * discrete amounts of time, like a write() or unlink().
235 * We must keep track of when those operations start
236 * (for permission checks) and when they end, so that
237 * we can determine when writes are able to occur to
238 * a filesystem.
239 */
240/*
241 * __mnt_is_readonly: check whether a mount is read-only
242 * @mnt: the mount to check for its write status
243 *
244 * This shouldn't be used directly ouside of the VFS.
245 * It does not guarantee that the filesystem will stay
246 * r/w, just that it is right *now*. This can not and
247 * should not be used in place of IS_RDONLY(inode).
248 * mnt_want/drop_write() will _keep_ the filesystem
249 * r/w.
250 */
43f5e655 251bool __mnt_is_readonly(struct vfsmount *mnt)
3d733633 252{
43f5e655 253 return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
3d733633
DH
254}
255EXPORT_SYMBOL_GPL(__mnt_is_readonly);
256
83adc753 257static inline void mnt_inc_writers(struct mount *mnt)
d3ef3d73 258{
259#ifdef CONFIG_SMP
68e8a9fe 260 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
d3ef3d73 261#else
68e8a9fe 262 mnt->mnt_writers++;
d3ef3d73 263#endif
264}
3d733633 265
83adc753 266static inline void mnt_dec_writers(struct mount *mnt)
3d733633 267{
d3ef3d73 268#ifdef CONFIG_SMP
68e8a9fe 269 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
d3ef3d73 270#else
68e8a9fe 271 mnt->mnt_writers--;
d3ef3d73 272#endif
3d733633 273}
3d733633 274
83adc753 275static unsigned int mnt_get_writers(struct mount *mnt)
3d733633 276{
d3ef3d73 277#ifdef CONFIG_SMP
278 unsigned int count = 0;
3d733633 279 int cpu;
3d733633
DH
280
281 for_each_possible_cpu(cpu) {
68e8a9fe 282 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3d733633 283 }
3d733633 284
d3ef3d73 285 return count;
286#else
287 return mnt->mnt_writers;
288#endif
3d733633
DH
289}
290
4ed5e82f
MS
291static int mnt_is_readonly(struct vfsmount *mnt)
292{
293 if (mnt->mnt_sb->s_readonly_remount)
294 return 1;
295 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
296 smp_rmb();
297 return __mnt_is_readonly(mnt);
298}
299
8366025e 300/*
eb04c282
JK
301 * Most r/o & frozen checks on a fs are for operations that take discrete
302 * amounts of time, like a write() or unlink(). We must keep track of when
303 * those operations start (for permission checks) and when they end, so that we
304 * can determine when writes are able to occur to a filesystem.
8366025e
DH
305 */
306/**
eb04c282 307 * __mnt_want_write - get write access to a mount without freeze protection
83adc753 308 * @m: the mount on which to take a write
8366025e 309 *
eb04c282
JK
310 * This tells the low-level filesystem that a write is about to be performed to
311 * it, and makes sure that writes are allowed (mnt it read-write) before
312 * returning success. This operation does not protect against filesystem being
313 * frozen. When the write operation is finished, __mnt_drop_write() must be
314 * called. This is effectively a refcount.
8366025e 315 */
eb04c282 316int __mnt_want_write(struct vfsmount *m)
8366025e 317{
83adc753 318 struct mount *mnt = real_mount(m);
3d733633 319 int ret = 0;
3d733633 320
d3ef3d73 321 preempt_disable();
c6653a83 322 mnt_inc_writers(mnt);
d3ef3d73 323 /*
c6653a83 324 * The store to mnt_inc_writers must be visible before we pass
d3ef3d73 325 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
326 * incremented count after it has set MNT_WRITE_HOLD.
327 */
328 smp_mb();
6aa7de05 329 while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
d3ef3d73 330 cpu_relax();
331 /*
332 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
333 * be set to match its requirements. So we must not load that until
334 * MNT_WRITE_HOLD is cleared.
335 */
336 smp_rmb();
4ed5e82f 337 if (mnt_is_readonly(m)) {
c6653a83 338 mnt_dec_writers(mnt);
3d733633 339 ret = -EROFS;
3d733633 340 }
d3ef3d73 341 preempt_enable();
eb04c282
JK
342
343 return ret;
344}
345
346/**
347 * mnt_want_write - get write access to a mount
348 * @m: the mount on which to take a write
349 *
350 * This tells the low-level filesystem that a write is about to be performed to
351 * it, and makes sure that writes are allowed (mount is read-write, filesystem
352 * is not frozen) before returning success. When the write operation is
353 * finished, mnt_drop_write() must be called. This is effectively a refcount.
354 */
355int mnt_want_write(struct vfsmount *m)
356{
357 int ret;
358
359 sb_start_write(m->mnt_sb);
360 ret = __mnt_want_write(m);
361 if (ret)
362 sb_end_write(m->mnt_sb);
3d733633 363 return ret;
8366025e
DH
364}
365EXPORT_SYMBOL_GPL(mnt_want_write);
366
96029c4e 367/**
368 * mnt_clone_write - get write access to a mount
369 * @mnt: the mount on which to take a write
370 *
371 * This is effectively like mnt_want_write, except
372 * it must only be used to take an extra write reference
373 * on a mountpoint that we already know has a write reference
374 * on it. This allows some optimisation.
375 *
376 * After finished, mnt_drop_write must be called as usual to
377 * drop the reference.
378 */
379int mnt_clone_write(struct vfsmount *mnt)
380{
381 /* superblock may be r/o */
382 if (__mnt_is_readonly(mnt))
383 return -EROFS;
384 preempt_disable();
83adc753 385 mnt_inc_writers(real_mount(mnt));
96029c4e 386 preempt_enable();
387 return 0;
388}
389EXPORT_SYMBOL_GPL(mnt_clone_write);
390
391/**
eb04c282 392 * __mnt_want_write_file - get write access to a file's mount
96029c4e 393 * @file: the file who's mount on which to take a write
394 *
eb04c282 395 * This is like __mnt_want_write, but it takes a file and can
96029c4e 396 * do some optimisations if the file is open for write already
397 */
eb04c282 398int __mnt_want_write_file(struct file *file)
96029c4e 399{
83f936c7 400 if (!(file->f_mode & FMODE_WRITER))
eb04c282 401 return __mnt_want_write(file->f_path.mnt);
96029c4e 402 else
403 return mnt_clone_write(file->f_path.mnt);
404}
eb04c282 405
7c6893e3
MS
406/**
407 * mnt_want_write_file - get write access to a file's mount
408 * @file: the file who's mount on which to take a write
409 *
410 * This is like mnt_want_write, but it takes a file and can
411 * do some optimisations if the file is open for write already
7c6893e3
MS
412 */
413int mnt_want_write_file(struct file *file)
414{
415 int ret;
416
a6795a58 417 sb_start_write(file_inode(file)->i_sb);
eb04c282
JK
418 ret = __mnt_want_write_file(file);
419 if (ret)
a6795a58 420 sb_end_write(file_inode(file)->i_sb);
7c6893e3
MS
421 return ret;
422}
96029c4e 423EXPORT_SYMBOL_GPL(mnt_want_write_file);
424
8366025e 425/**
eb04c282 426 * __mnt_drop_write - give up write access to a mount
8366025e
DH
427 * @mnt: the mount on which to give up write access
428 *
429 * Tells the low-level filesystem that we are done
430 * performing writes to it. Must be matched with
eb04c282 431 * __mnt_want_write() call above.
8366025e 432 */
eb04c282 433void __mnt_drop_write(struct vfsmount *mnt)
8366025e 434{
d3ef3d73 435 preempt_disable();
83adc753 436 mnt_dec_writers(real_mount(mnt));
d3ef3d73 437 preempt_enable();
8366025e 438}
eb04c282
JK
439
440/**
441 * mnt_drop_write - give up write access to a mount
442 * @mnt: the mount on which to give up write access
443 *
444 * Tells the low-level filesystem that we are done performing writes to it and
445 * also allows filesystem to be frozen again. Must be matched with
446 * mnt_want_write() call above.
447 */
448void mnt_drop_write(struct vfsmount *mnt)
449{
450 __mnt_drop_write(mnt);
451 sb_end_write(mnt->mnt_sb);
452}
8366025e
DH
453EXPORT_SYMBOL_GPL(mnt_drop_write);
454
eb04c282
JK
455void __mnt_drop_write_file(struct file *file)
456{
457 __mnt_drop_write(file->f_path.mnt);
458}
459
7c6893e3
MS
460void mnt_drop_write_file(struct file *file)
461{
a6795a58 462 __mnt_drop_write_file(file);
7c6893e3
MS
463 sb_end_write(file_inode(file)->i_sb);
464}
2a79f17e
AV
465EXPORT_SYMBOL(mnt_drop_write_file);
466
83adc753 467static int mnt_make_readonly(struct mount *mnt)
8366025e 468{
3d733633
DH
469 int ret = 0;
470
719ea2fb 471 lock_mount_hash();
83adc753 472 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
3d733633 473 /*
d3ef3d73 474 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
475 * should be visible before we do.
3d733633 476 */
d3ef3d73 477 smp_mb();
478
3d733633 479 /*
d3ef3d73 480 * With writers on hold, if this value is zero, then there are
481 * definitely no active writers (although held writers may subsequently
482 * increment the count, they'll have to wait, and decrement it after
483 * seeing MNT_READONLY).
484 *
485 * It is OK to have counter incremented on one CPU and decremented on
486 * another: the sum will add up correctly. The danger would be when we
487 * sum up each counter, if we read a counter before it is incremented,
488 * but then read another CPU's count which it has been subsequently
489 * decremented from -- we would see more decrements than we should.
490 * MNT_WRITE_HOLD protects against this scenario, because
491 * mnt_want_write first increments count, then smp_mb, then spins on
492 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
493 * we're counting up here.
3d733633 494 */
c6653a83 495 if (mnt_get_writers(mnt) > 0)
d3ef3d73 496 ret = -EBUSY;
497 else
83adc753 498 mnt->mnt.mnt_flags |= MNT_READONLY;
d3ef3d73 499 /*
500 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
501 * that become unheld will see MNT_READONLY.
502 */
503 smp_wmb();
83adc753 504 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
719ea2fb 505 unlock_mount_hash();
3d733633 506 return ret;
8366025e 507}
8366025e 508
43f5e655 509static int __mnt_unmake_readonly(struct mount *mnt)
2e4b7fcd 510{
719ea2fb 511 lock_mount_hash();
83adc753 512 mnt->mnt.mnt_flags &= ~MNT_READONLY;
719ea2fb 513 unlock_mount_hash();
43f5e655 514 return 0;
2e4b7fcd
DH
515}
516
4ed5e82f
MS
517int sb_prepare_remount_readonly(struct super_block *sb)
518{
519 struct mount *mnt;
520 int err = 0;
521
8e8b8796
MS
522 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
523 if (atomic_long_read(&sb->s_remove_count))
524 return -EBUSY;
525
719ea2fb 526 lock_mount_hash();
4ed5e82f
MS
527 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
528 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
529 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
530 smp_mb();
531 if (mnt_get_writers(mnt) > 0) {
532 err = -EBUSY;
533 break;
534 }
535 }
536 }
8e8b8796
MS
537 if (!err && atomic_long_read(&sb->s_remove_count))
538 err = -EBUSY;
539
4ed5e82f
MS
540 if (!err) {
541 sb->s_readonly_remount = 1;
542 smp_wmb();
543 }
544 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
545 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
546 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
547 }
719ea2fb 548 unlock_mount_hash();
4ed5e82f
MS
549
550 return err;
551}
552
b105e270 553static void free_vfsmnt(struct mount *mnt)
1da177e4 554{
fcc139ae 555 kfree_const(mnt->mnt_devname);
d3ef3d73 556#ifdef CONFIG_SMP
68e8a9fe 557 free_percpu(mnt->mnt_pcp);
d3ef3d73 558#endif
b105e270 559 kmem_cache_free(mnt_cache, mnt);
1da177e4
LT
560}
561
8ffcb32e
DH
562static void delayed_free_vfsmnt(struct rcu_head *head)
563{
564 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
565}
566
48a066e7 567/* call under rcu_read_lock */
294d71ff 568int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
48a066e7
AV
569{
570 struct mount *mnt;
571 if (read_seqretry(&mount_lock, seq))
294d71ff 572 return 1;
48a066e7 573 if (bastard == NULL)
294d71ff 574 return 0;
48a066e7
AV
575 mnt = real_mount(bastard);
576 mnt_add_count(mnt, 1);
119e1ef8 577 smp_mb(); // see mntput_no_expire()
48a066e7 578 if (likely(!read_seqretry(&mount_lock, seq)))
294d71ff 579 return 0;
48a066e7
AV
580 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
581 mnt_add_count(mnt, -1);
294d71ff
AV
582 return 1;
583 }
119e1ef8
AV
584 lock_mount_hash();
585 if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
586 mnt_add_count(mnt, -1);
587 unlock_mount_hash();
588 return 1;
589 }
590 unlock_mount_hash();
591 /* caller will mntput() */
294d71ff
AV
592 return -1;
593}
594
595/* call under rcu_read_lock */
596bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
597{
598 int res = __legitimize_mnt(bastard, seq);
599 if (likely(!res))
600 return true;
601 if (unlikely(res < 0)) {
602 rcu_read_unlock();
603 mntput(bastard);
604 rcu_read_lock();
48a066e7 605 }
48a066e7
AV
606 return false;
607}
608
1da177e4 609/*
474279dc 610 * find the first mount at @dentry on vfsmount @mnt.
48a066e7 611 * call under rcu_read_lock()
1da177e4 612 */
474279dc 613struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 614{
38129a13 615 struct hlist_head *head = m_hash(mnt, dentry);
474279dc
AV
616 struct mount *p;
617
38129a13 618 hlist_for_each_entry_rcu(p, head, mnt_hash)
474279dc
AV
619 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
620 return p;
621 return NULL;
622}
623
a05964f3 624/*
f015f126
DH
625 * lookup_mnt - Return the first child mount mounted at path
626 *
627 * "First" means first mounted chronologically. If you create the
628 * following mounts:
629 *
630 * mount /dev/sda1 /mnt
631 * mount /dev/sda2 /mnt
632 * mount /dev/sda3 /mnt
633 *
634 * Then lookup_mnt() on the base /mnt dentry in the root mount will
635 * return successively the root dentry and vfsmount of /dev/sda1, then
636 * /dev/sda2, then /dev/sda3, then NULL.
637 *
638 * lookup_mnt takes a reference to the found vfsmount.
a05964f3 639 */
ca71cf71 640struct vfsmount *lookup_mnt(const struct path *path)
a05964f3 641{
c7105365 642 struct mount *child_mnt;
48a066e7
AV
643 struct vfsmount *m;
644 unsigned seq;
99b7db7b 645
48a066e7
AV
646 rcu_read_lock();
647 do {
648 seq = read_seqbegin(&mount_lock);
649 child_mnt = __lookup_mnt(path->mnt, path->dentry);
650 m = child_mnt ? &child_mnt->mnt : NULL;
651 } while (!legitimize_mnt(m, seq));
652 rcu_read_unlock();
653 return m;
a05964f3
RP
654}
655
7af1364f
EB
656/*
657 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
658 * current mount namespace.
659 *
660 * The common case is dentries are not mountpoints at all and that
661 * test is handled inline. For the slow case when we are actually
662 * dealing with a mountpoint of some kind, walk through all of the
663 * mounts in the current mount namespace and test to see if the dentry
664 * is a mountpoint.
665 *
666 * The mount_hashtable is not usable in the context because we
667 * need to identify all mounts that may be in the current mount
668 * namespace not just a mount that happens to have some specified
669 * parent mount.
670 */
671bool __is_local_mountpoint(struct dentry *dentry)
672{
673 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
674 struct mount *mnt;
675 bool is_covered = false;
676
677 if (!d_mountpoint(dentry))
678 goto out;
679
680 down_read(&namespace_sem);
681 list_for_each_entry(mnt, &ns->list, mnt_list) {
682 is_covered = (mnt->mnt_mountpoint == dentry);
683 if (is_covered)
684 break;
685 }
686 up_read(&namespace_sem);
687out:
688 return is_covered;
689}
690
e2dfa935 691static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
84d17192 692{
0818bf27 693 struct hlist_head *chain = mp_hash(dentry);
84d17192
AV
694 struct mountpoint *mp;
695
0818bf27 696 hlist_for_each_entry(mp, chain, m_hash) {
84d17192 697 if (mp->m_dentry == dentry) {
84d17192
AV
698 mp->m_count++;
699 return mp;
700 }
701 }
e2dfa935
EB
702 return NULL;
703}
704
3895dbf8 705static struct mountpoint *get_mountpoint(struct dentry *dentry)
e2dfa935 706{
3895dbf8 707 struct mountpoint *mp, *new = NULL;
e2dfa935 708 int ret;
84d17192 709
3895dbf8 710 if (d_mountpoint(dentry)) {
1e9c75fb
BC
711 /* might be worth a WARN_ON() */
712 if (d_unlinked(dentry))
713 return ERR_PTR(-ENOENT);
3895dbf8
EB
714mountpoint:
715 read_seqlock_excl(&mount_lock);
716 mp = lookup_mountpoint(dentry);
717 read_sequnlock_excl(&mount_lock);
718 if (mp)
719 goto done;
720 }
721
722 if (!new)
723 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
724 if (!new)
84d17192
AV
725 return ERR_PTR(-ENOMEM);
726
3895dbf8
EB
727
728 /* Exactly one processes may set d_mounted */
eed81007 729 ret = d_set_mounted(dentry);
eed81007 730
3895dbf8
EB
731 /* Someone else set d_mounted? */
732 if (ret == -EBUSY)
733 goto mountpoint;
734
735 /* The dentry is not available as a mountpoint? */
736 mp = ERR_PTR(ret);
737 if (ret)
738 goto done;
739
740 /* Add the new mountpoint to the hash table */
741 read_seqlock_excl(&mount_lock);
742 new->m_dentry = dentry;
743 new->m_count = 1;
744 hlist_add_head(&new->m_hash, mp_hash(dentry));
745 INIT_HLIST_HEAD(&new->m_list);
746 read_sequnlock_excl(&mount_lock);
747
748 mp = new;
749 new = NULL;
750done:
751 kfree(new);
84d17192
AV
752 return mp;
753}
754
755static void put_mountpoint(struct mountpoint *mp)
756{
757 if (!--mp->m_count) {
758 struct dentry *dentry = mp->m_dentry;
0a5eb7c8 759 BUG_ON(!hlist_empty(&mp->m_list));
84d17192
AV
760 spin_lock(&dentry->d_lock);
761 dentry->d_flags &= ~DCACHE_MOUNTED;
762 spin_unlock(&dentry->d_lock);
0818bf27 763 hlist_del(&mp->m_hash);
84d17192
AV
764 kfree(mp);
765 }
766}
767
143c8c91 768static inline int check_mnt(struct mount *mnt)
1da177e4 769{
6b3286ed 770 return mnt->mnt_ns == current->nsproxy->mnt_ns;
1da177e4
LT
771}
772
99b7db7b
NP
773/*
774 * vfsmount lock must be held for write
775 */
6b3286ed 776static void touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
777{
778 if (ns) {
779 ns->event = ++event;
780 wake_up_interruptible(&ns->poll);
781 }
782}
783
99b7db7b
NP
784/*
785 * vfsmount lock must be held for write
786 */
6b3286ed 787static void __touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
788{
789 if (ns && ns->event != event) {
790 ns->event = event;
791 wake_up_interruptible(&ns->poll);
792 }
793}
794
99b7db7b
NP
795/*
796 * vfsmount lock must be held for write
797 */
e4e59906 798static struct mountpoint *unhash_mnt(struct mount *mnt)
419148da 799{
e4e59906 800 struct mountpoint *mp;
0714a533 801 mnt->mnt_parent = mnt;
a73324da 802 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
6b41d536 803 list_del_init(&mnt->mnt_child);
38129a13 804 hlist_del_init_rcu(&mnt->mnt_hash);
0a5eb7c8 805 hlist_del_init(&mnt->mnt_mp_list);
e4e59906 806 mp = mnt->mnt_mp;
84d17192 807 mnt->mnt_mp = NULL;
e4e59906 808 return mp;
1da177e4
LT
809}
810
7bdb11de
EB
811/*
812 * vfsmount lock must be held for write
813 */
814static void detach_mnt(struct mount *mnt, struct path *old_path)
815{
816 old_path->dentry = mnt->mnt_mountpoint;
817 old_path->mnt = &mnt->mnt_parent->mnt;
e4e59906 818 put_mountpoint(unhash_mnt(mnt));
7bdb11de
EB
819}
820
6a46c573
EB
821/*
822 * vfsmount lock must be held for write
823 */
824static void umount_mnt(struct mount *mnt)
825{
826 /* old mountpoint will be dropped when we can do that */
827 mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
e4e59906 828 put_mountpoint(unhash_mnt(mnt));
6a46c573
EB
829}
830
99b7db7b
NP
831/*
832 * vfsmount lock must be held for write
833 */
84d17192
AV
834void mnt_set_mountpoint(struct mount *mnt,
835 struct mountpoint *mp,
44d964d6 836 struct mount *child_mnt)
b90fa9ae 837{
84d17192 838 mp->m_count++;
3a2393d7 839 mnt_add_count(mnt, 1); /* essentially, that's mntget */
84d17192 840 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
3a2393d7 841 child_mnt->mnt_parent = mnt;
84d17192 842 child_mnt->mnt_mp = mp;
0a5eb7c8 843 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
b90fa9ae
RP
844}
845
1064f874
EB
846static void __attach_mnt(struct mount *mnt, struct mount *parent)
847{
848 hlist_add_head_rcu(&mnt->mnt_hash,
849 m_hash(&parent->mnt, mnt->mnt_mountpoint));
850 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
851}
852
99b7db7b
NP
853/*
854 * vfsmount lock must be held for write
855 */
84d17192
AV
856static void attach_mnt(struct mount *mnt,
857 struct mount *parent,
858 struct mountpoint *mp)
1da177e4 859{
84d17192 860 mnt_set_mountpoint(parent, mp, mnt);
1064f874 861 __attach_mnt(mnt, parent);
b90fa9ae
RP
862}
863
1064f874 864void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
12a5b529 865{
1064f874
EB
866 struct mountpoint *old_mp = mnt->mnt_mp;
867 struct dentry *old_mountpoint = mnt->mnt_mountpoint;
868 struct mount *old_parent = mnt->mnt_parent;
869
870 list_del_init(&mnt->mnt_child);
871 hlist_del_init(&mnt->mnt_mp_list);
872 hlist_del_init_rcu(&mnt->mnt_hash);
873
874 attach_mnt(mnt, parent, mp);
875
876 put_mountpoint(old_mp);
877
878 /*
879 * Safely avoid even the suggestion this code might sleep or
880 * lock the mount hash by taking advantage of the knowledge that
881 * mnt_change_mountpoint will not release the final reference
882 * to a mountpoint.
883 *
884 * During mounting, the mount passed in as the parent mount will
885 * continue to use the old mountpoint and during unmounting, the
886 * old mountpoint will continue to exist until namespace_unlock,
887 * which happens well after mnt_change_mountpoint.
888 */
889 spin_lock(&old_mountpoint->d_lock);
890 old_mountpoint->d_lockref.count--;
891 spin_unlock(&old_mountpoint->d_lock);
892
893 mnt_add_count(old_parent, -1);
12a5b529
AV
894}
895
b90fa9ae 896/*
99b7db7b 897 * vfsmount lock must be held for write
b90fa9ae 898 */
1064f874 899static void commit_tree(struct mount *mnt)
b90fa9ae 900{
0714a533 901 struct mount *parent = mnt->mnt_parent;
83adc753 902 struct mount *m;
b90fa9ae 903 LIST_HEAD(head);
143c8c91 904 struct mnt_namespace *n = parent->mnt_ns;
b90fa9ae 905
0714a533 906 BUG_ON(parent == mnt);
b90fa9ae 907
1a4eeaf2 908 list_add_tail(&head, &mnt->mnt_list);
f7a99c5b 909 list_for_each_entry(m, &head, mnt_list)
143c8c91 910 m->mnt_ns = n;
f03c6599 911
b90fa9ae
RP
912 list_splice(&head, n->list.prev);
913
d2921684
EB
914 n->mounts += n->pending_mounts;
915 n->pending_mounts = 0;
916
1064f874 917 __attach_mnt(mnt, parent);
6b3286ed 918 touch_mnt_namespace(n);
1da177e4
LT
919}
920
909b0a88 921static struct mount *next_mnt(struct mount *p, struct mount *root)
1da177e4 922{
6b41d536
AV
923 struct list_head *next = p->mnt_mounts.next;
924 if (next == &p->mnt_mounts) {
1da177e4 925 while (1) {
909b0a88 926 if (p == root)
1da177e4 927 return NULL;
6b41d536
AV
928 next = p->mnt_child.next;
929 if (next != &p->mnt_parent->mnt_mounts)
1da177e4 930 break;
0714a533 931 p = p->mnt_parent;
1da177e4
LT
932 }
933 }
6b41d536 934 return list_entry(next, struct mount, mnt_child);
1da177e4
LT
935}
936
315fc83e 937static struct mount *skip_mnt_tree(struct mount *p)
9676f0c6 938{
6b41d536
AV
939 struct list_head *prev = p->mnt_mounts.prev;
940 while (prev != &p->mnt_mounts) {
941 p = list_entry(prev, struct mount, mnt_child);
942 prev = p->mnt_mounts.prev;
9676f0c6
RP
943 }
944 return p;
945}
946
8f291889
AV
947/**
948 * vfs_create_mount - Create a mount for a configured superblock
949 * @fc: The configuration context with the superblock attached
950 *
951 * Create a mount to an already configured superblock. If necessary, the
952 * caller should invoke vfs_get_tree() before calling this.
953 *
954 * Note that this does not attach the mount to anything.
955 */
956struct vfsmount *vfs_create_mount(struct fs_context *fc)
9d412a43 957{
b105e270 958 struct mount *mnt;
9d412a43 959
8f291889
AV
960 if (!fc->root)
961 return ERR_PTR(-EINVAL);
9d412a43 962
8f291889 963 mnt = alloc_vfsmnt(fc->source ?: "none");
9d412a43
AV
964 if (!mnt)
965 return ERR_PTR(-ENOMEM);
966
8f291889 967 if (fc->sb_flags & SB_KERNMOUNT)
b105e270 968 mnt->mnt.mnt_flags = MNT_INTERNAL;
9d412a43 969
8f291889
AV
970 atomic_inc(&fc->root->d_sb->s_active);
971 mnt->mnt.mnt_sb = fc->root->d_sb;
972 mnt->mnt.mnt_root = dget(fc->root);
973 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
974 mnt->mnt_parent = mnt;
9d412a43 975
719ea2fb 976 lock_mount_hash();
8f291889 977 list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
719ea2fb 978 unlock_mount_hash();
b105e270 979 return &mnt->mnt;
9d412a43 980}
8f291889
AV
981EXPORT_SYMBOL(vfs_create_mount);
982
983struct vfsmount *fc_mount(struct fs_context *fc)
984{
985 int err = vfs_get_tree(fc);
986 if (!err) {
987 up_write(&fc->root->d_sb->s_umount);
988 return vfs_create_mount(fc);
989 }
990 return ERR_PTR(err);
991}
992EXPORT_SYMBOL(fc_mount);
993
9bc61ab1
DH
994struct vfsmount *vfs_kern_mount(struct file_system_type *type,
995 int flags, const char *name,
996 void *data)
9d412a43 997{
9bc61ab1 998 struct fs_context *fc;
8f291889 999 struct vfsmount *mnt;
9bc61ab1 1000 int ret = 0;
9d412a43
AV
1001
1002 if (!type)
3e1aeb00 1003 return ERR_PTR(-EINVAL);
9d412a43 1004
9bc61ab1
DH
1005 fc = fs_context_for_mount(type, flags);
1006 if (IS_ERR(fc))
1007 return ERR_CAST(fc);
1008
3e1aeb00
DH
1009 if (name)
1010 ret = vfs_parse_fs_string(fc, "source",
1011 name, strlen(name));
9bc61ab1
DH
1012 if (!ret)
1013 ret = parse_monolithic_mount_data(fc, data);
1014 if (!ret)
8f291889
AV
1015 mnt = fc_mount(fc);
1016 else
1017 mnt = ERR_PTR(ret);
9d412a43 1018
9bc61ab1 1019 put_fs_context(fc);
8f291889 1020 return mnt;
9d412a43
AV
1021}
1022EXPORT_SYMBOL_GPL(vfs_kern_mount);
1023
93faccbb
EB
1024struct vfsmount *
1025vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
1026 const char *name, void *data)
1027{
1028 /* Until it is worked out how to pass the user namespace
1029 * through from the parent mount to the submount don't support
1030 * unprivileged mounts with submounts.
1031 */
1032 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1033 return ERR_PTR(-EPERM);
1034
e462ec50 1035 return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
93faccbb
EB
1036}
1037EXPORT_SYMBOL_GPL(vfs_submount);
1038
87129cc0 1039static struct mount *clone_mnt(struct mount *old, struct dentry *root,
36341f64 1040 int flag)
1da177e4 1041{
87129cc0 1042 struct super_block *sb = old->mnt.mnt_sb;
be34d1a3
DH
1043 struct mount *mnt;
1044 int err;
1da177e4 1045
be34d1a3
DH
1046 mnt = alloc_vfsmnt(old->mnt_devname);
1047 if (!mnt)
1048 return ERR_PTR(-ENOMEM);
719f5d7f 1049
7a472ef4 1050 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
be34d1a3
DH
1051 mnt->mnt_group_id = 0; /* not a peer of original */
1052 else
1053 mnt->mnt_group_id = old->mnt_group_id;
b90fa9ae 1054
be34d1a3
DH
1055 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1056 err = mnt_alloc_group_id(mnt);
1057 if (err)
1058 goto out_free;
1da177e4 1059 }
be34d1a3 1060
16a34adb
AV
1061 mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1062 mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
5ff9d8a6 1063
be34d1a3
DH
1064 atomic_inc(&sb->s_active);
1065 mnt->mnt.mnt_sb = sb;
1066 mnt->mnt.mnt_root = dget(root);
1067 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1068 mnt->mnt_parent = mnt;
719ea2fb 1069 lock_mount_hash();
be34d1a3 1070 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
719ea2fb 1071 unlock_mount_hash();
be34d1a3 1072
7a472ef4
EB
1073 if ((flag & CL_SLAVE) ||
1074 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
be34d1a3
DH
1075 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1076 mnt->mnt_master = old;
1077 CLEAR_MNT_SHARED(mnt);
1078 } else if (!(flag & CL_PRIVATE)) {
1079 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1080 list_add(&mnt->mnt_share, &old->mnt_share);
1081 if (IS_MNT_SLAVE(old))
1082 list_add(&mnt->mnt_slave, &old->mnt_slave);
1083 mnt->mnt_master = old->mnt_master;
5235d448
AV
1084 } else {
1085 CLEAR_MNT_SHARED(mnt);
be34d1a3
DH
1086 }
1087 if (flag & CL_MAKE_SHARED)
1088 set_mnt_shared(mnt);
1089
1090 /* stick the duplicate mount on the same expiry list
1091 * as the original if that was on one */
1092 if (flag & CL_EXPIRE) {
1093 if (!list_empty(&old->mnt_expire))
1094 list_add(&mnt->mnt_expire, &old->mnt_expire);
1095 }
1096
cb338d06 1097 return mnt;
719f5d7f
MS
1098
1099 out_free:
8ffcb32e 1100 mnt_free_id(mnt);
719f5d7f 1101 free_vfsmnt(mnt);
be34d1a3 1102 return ERR_PTR(err);
1da177e4
LT
1103}
1104
9ea459e1
AV
1105static void cleanup_mnt(struct mount *mnt)
1106{
1107 /*
1108 * This probably indicates that somebody messed
1109 * up a mnt_want/drop_write() pair. If this
1110 * happens, the filesystem was probably unable
1111 * to make r/w->r/o transitions.
1112 */
1113 /*
1114 * The locking used to deal with mnt_count decrement provides barriers,
1115 * so mnt_get_writers() below is safe.
1116 */
1117 WARN_ON(mnt_get_writers(mnt));
1118 if (unlikely(mnt->mnt_pins.first))
1119 mnt_pin_kill(mnt);
1120 fsnotify_vfsmount_delete(&mnt->mnt);
1121 dput(mnt->mnt.mnt_root);
1122 deactivate_super(mnt->mnt.mnt_sb);
1123 mnt_free_id(mnt);
1124 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1125}
1126
1127static void __cleanup_mnt(struct rcu_head *head)
1128{
1129 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1130}
1131
1132static LLIST_HEAD(delayed_mntput_list);
1133static void delayed_mntput(struct work_struct *unused)
1134{
1135 struct llist_node *node = llist_del_all(&delayed_mntput_list);
29785735 1136 struct mount *m, *t;
9ea459e1 1137
29785735
BP
1138 llist_for_each_entry_safe(m, t, node, mnt_llist)
1139 cleanup_mnt(m);
9ea459e1
AV
1140}
1141static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1142
900148dc 1143static void mntput_no_expire(struct mount *mnt)
b3e19d92 1144{
48a066e7 1145 rcu_read_lock();
9ea0a46c
AV
1146 if (likely(READ_ONCE(mnt->mnt_ns))) {
1147 /*
1148 * Since we don't do lock_mount_hash() here,
1149 * ->mnt_ns can change under us. However, if it's
1150 * non-NULL, then there's a reference that won't
1151 * be dropped until after an RCU delay done after
1152 * turning ->mnt_ns NULL. So if we observe it
1153 * non-NULL under rcu_read_lock(), the reference
1154 * we are dropping is not the final one.
1155 */
1156 mnt_add_count(mnt, -1);
48a066e7 1157 rcu_read_unlock();
f03c6599 1158 return;
b3e19d92 1159 }
719ea2fb 1160 lock_mount_hash();
119e1ef8
AV
1161 /*
1162 * make sure that if __legitimize_mnt() has not seen us grab
1163 * mount_lock, we'll see their refcount increment here.
1164 */
1165 smp_mb();
9ea0a46c 1166 mnt_add_count(mnt, -1);
b3e19d92 1167 if (mnt_get_count(mnt)) {
48a066e7 1168 rcu_read_unlock();
719ea2fb 1169 unlock_mount_hash();
99b7db7b
NP
1170 return;
1171 }
48a066e7
AV
1172 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1173 rcu_read_unlock();
1174 unlock_mount_hash();
1175 return;
1176 }
1177 mnt->mnt.mnt_flags |= MNT_DOOMED;
1178 rcu_read_unlock();
962830df 1179
39f7c4db 1180 list_del(&mnt->mnt_instance);
ce07d891
EB
1181
1182 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1183 struct mount *p, *tmp;
1184 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
1185 umount_mnt(p);
1186 }
1187 }
719ea2fb 1188 unlock_mount_hash();
649a795a 1189
9ea459e1
AV
1190 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1191 struct task_struct *task = current;
1192 if (likely(!(task->flags & PF_KTHREAD))) {
1193 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1194 if (!task_work_add(task, &mnt->mnt_rcu, true))
1195 return;
1196 }
1197 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1198 schedule_delayed_work(&delayed_mntput_work, 1);
1199 return;
1200 }
1201 cleanup_mnt(mnt);
b3e19d92 1202}
b3e19d92
NP
1203
1204void mntput(struct vfsmount *mnt)
1205{
1206 if (mnt) {
863d684f 1207 struct mount *m = real_mount(mnt);
b3e19d92 1208 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
863d684f
AV
1209 if (unlikely(m->mnt_expiry_mark))
1210 m->mnt_expiry_mark = 0;
1211 mntput_no_expire(m);
b3e19d92
NP
1212 }
1213}
1214EXPORT_SYMBOL(mntput);
1215
1216struct vfsmount *mntget(struct vfsmount *mnt)
1217{
1218 if (mnt)
83adc753 1219 mnt_add_count(real_mount(mnt), 1);
b3e19d92
NP
1220 return mnt;
1221}
1222EXPORT_SYMBOL(mntget);
1223
c6609c0a
IK
1224/* path_is_mountpoint() - Check if path is a mount in the current
1225 * namespace.
1226 *
1227 * d_mountpoint() can only be used reliably to establish if a dentry is
1228 * not mounted in any namespace and that common case is handled inline.
1229 * d_mountpoint() isn't aware of the possibility there may be multiple
1230 * mounts using a given dentry in a different namespace. This function
1231 * checks if the passed in path is a mountpoint rather than the dentry
1232 * alone.
1233 */
1234bool path_is_mountpoint(const struct path *path)
1235{
1236 unsigned seq;
1237 bool res;
1238
1239 if (!d_mountpoint(path->dentry))
1240 return false;
1241
1242 rcu_read_lock();
1243 do {
1244 seq = read_seqbegin(&mount_lock);
1245 res = __path_is_mountpoint(path);
1246 } while (read_seqretry(&mount_lock, seq));
1247 rcu_read_unlock();
1248
1249 return res;
1250}
1251EXPORT_SYMBOL(path_is_mountpoint);
1252
ca71cf71 1253struct vfsmount *mnt_clone_internal(const struct path *path)
7b7b1ace 1254{
3064c356
AV
1255 struct mount *p;
1256 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1257 if (IS_ERR(p))
1258 return ERR_CAST(p);
1259 p->mnt.mnt_flags |= MNT_INTERNAL;
1260 return &p->mnt;
7b7b1ace 1261}
1da177e4 1262
a1a2c409 1263#ifdef CONFIG_PROC_FS
0226f492 1264/* iterator; we want it to have access to namespace_sem, thus here... */
1da177e4
LT
1265static void *m_start(struct seq_file *m, loff_t *pos)
1266{
ede1bf0d 1267 struct proc_mounts *p = m->private;
1da177e4 1268
390c6843 1269 down_read(&namespace_sem);
c7999c36
AV
1270 if (p->cached_event == p->ns->event) {
1271 void *v = p->cached_mount;
1272 if (*pos == p->cached_index)
1273 return v;
1274 if (*pos == p->cached_index + 1) {
1275 v = seq_list_next(v, &p->ns->list, &p->cached_index);
1276 return p->cached_mount = v;
1277 }
1278 }
1279
1280 p->cached_event = p->ns->event;
1281 p->cached_mount = seq_list_start(&p->ns->list, *pos);
1282 p->cached_index = *pos;
1283 return p->cached_mount;
1da177e4
LT
1284}
1285
1286static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1287{
ede1bf0d 1288 struct proc_mounts *p = m->private;
b0765fb8 1289
c7999c36
AV
1290 p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1291 p->cached_index = *pos;
1292 return p->cached_mount;
1da177e4
LT
1293}
1294
1295static void m_stop(struct seq_file *m, void *v)
1296{
390c6843 1297 up_read(&namespace_sem);
1da177e4
LT
1298}
1299
0226f492 1300static int m_show(struct seq_file *m, void *v)
2d4d4864 1301{
ede1bf0d 1302 struct proc_mounts *p = m->private;
1a4eeaf2 1303 struct mount *r = list_entry(v, struct mount, mnt_list);
0226f492 1304 return p->show(m, &r->mnt);
1da177e4
LT
1305}
1306
a1a2c409 1307const struct seq_operations mounts_op = {
1da177e4
LT
1308 .start = m_start,
1309 .next = m_next,
1310 .stop = m_stop,
0226f492 1311 .show = m_show,
b4629fe2 1312};
a1a2c409 1313#endif /* CONFIG_PROC_FS */
b4629fe2 1314
1da177e4
LT
1315/**
1316 * may_umount_tree - check if a mount tree is busy
1317 * @mnt: root of mount tree
1318 *
1319 * This is called to check if a tree of mounts has any
1320 * open files, pwds, chroots or sub mounts that are
1321 * busy.
1322 */
909b0a88 1323int may_umount_tree(struct vfsmount *m)
1da177e4 1324{
909b0a88 1325 struct mount *mnt = real_mount(m);
36341f64
RP
1326 int actual_refs = 0;
1327 int minimum_refs = 0;
315fc83e 1328 struct mount *p;
909b0a88 1329 BUG_ON(!m);
1da177e4 1330
b3e19d92 1331 /* write lock needed for mnt_get_count */
719ea2fb 1332 lock_mount_hash();
909b0a88 1333 for (p = mnt; p; p = next_mnt(p, mnt)) {
83adc753 1334 actual_refs += mnt_get_count(p);
1da177e4 1335 minimum_refs += 2;
1da177e4 1336 }
719ea2fb 1337 unlock_mount_hash();
1da177e4
LT
1338
1339 if (actual_refs > minimum_refs)
e3474a8e 1340 return 0;
1da177e4 1341
e3474a8e 1342 return 1;
1da177e4
LT
1343}
1344
1345EXPORT_SYMBOL(may_umount_tree);
1346
1347/**
1348 * may_umount - check if a mount point is busy
1349 * @mnt: root of mount
1350 *
1351 * This is called to check if a mount point has any
1352 * open files, pwds, chroots or sub mounts. If the
1353 * mount has sub mounts this will return busy
1354 * regardless of whether the sub mounts are busy.
1355 *
1356 * Doesn't take quota and stuff into account. IOW, in some cases it will
1357 * give false negatives. The main reason why it's here is that we need
1358 * a non-destructive way to look for easily umountable filesystems.
1359 */
1360int may_umount(struct vfsmount *mnt)
1361{
e3474a8e 1362 int ret = 1;
8ad08d8a 1363 down_read(&namespace_sem);
719ea2fb 1364 lock_mount_hash();
1ab59738 1365 if (propagate_mount_busy(real_mount(mnt), 2))
e3474a8e 1366 ret = 0;
719ea2fb 1367 unlock_mount_hash();
8ad08d8a 1368 up_read(&namespace_sem);
a05964f3 1369 return ret;
1da177e4
LT
1370}
1371
1372EXPORT_SYMBOL(may_umount);
1373
38129a13 1374static HLIST_HEAD(unmounted); /* protected by namespace_sem */
e3197d83 1375
97216be0 1376static void namespace_unlock(void)
70fbcdf4 1377{
a3b3c562 1378 struct hlist_head head;
97216be0 1379
a3b3c562 1380 hlist_move_list(&unmounted, &head);
97216be0 1381
97216be0
AV
1382 up_write(&namespace_sem);
1383
a3b3c562
EB
1384 if (likely(hlist_empty(&head)))
1385 return;
1386
22cb7405 1387 synchronize_rcu_expedited();
48a066e7 1388
87b95ce0 1389 group_pin_kill(&head);
70fbcdf4
RP
1390}
1391
97216be0 1392static inline void namespace_lock(void)
e3197d83 1393{
97216be0 1394 down_write(&namespace_sem);
e3197d83
AV
1395}
1396
e819f152
EB
1397enum umount_tree_flags {
1398 UMOUNT_SYNC = 1,
1399 UMOUNT_PROPAGATE = 2,
e0c9c0af 1400 UMOUNT_CONNECTED = 4,
e819f152 1401};
f2d0a123
EB
1402
1403static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1404{
1405 /* Leaving mounts connected is only valid for lazy umounts */
1406 if (how & UMOUNT_SYNC)
1407 return true;
1408
1409 /* A mount without a parent has nothing to be connected to */
1410 if (!mnt_has_parent(mnt))
1411 return true;
1412
1413 /* Because the reference counting rules change when mounts are
1414 * unmounted and connected, umounted mounts may not be
1415 * connected to mounted mounts.
1416 */
1417 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1418 return true;
1419
1420 /* Has it been requested that the mount remain connected? */
1421 if (how & UMOUNT_CONNECTED)
1422 return false;
1423
1424 /* Is the mount locked such that it needs to remain connected? */
1425 if (IS_MNT_LOCKED(mnt))
1426 return false;
1427
1428 /* By default disconnect the mount */
1429 return true;
1430}
1431
99b7db7b 1432/*
48a066e7 1433 * mount_lock must be held
99b7db7b
NP
1434 * namespace_sem must be held for write
1435 */
e819f152 1436static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1da177e4 1437{
c003b26f 1438 LIST_HEAD(tmp_list);
315fc83e 1439 struct mount *p;
1da177e4 1440
5d88457e
EB
1441 if (how & UMOUNT_PROPAGATE)
1442 propagate_mount_unlock(mnt);
1443
c003b26f 1444 /* Gather the mounts to umount */
590ce4bc
EB
1445 for (p = mnt; p; p = next_mnt(p, mnt)) {
1446 p->mnt.mnt_flags |= MNT_UMOUNT;
c003b26f 1447 list_move(&p->mnt_list, &tmp_list);
590ce4bc 1448 }
1da177e4 1449
411a938b 1450 /* Hide the mounts from mnt_mounts */
c003b26f 1451 list_for_each_entry(p, &tmp_list, mnt_list) {
88b368f2 1452 list_del_init(&p->mnt_child);
c003b26f 1453 }
88b368f2 1454
c003b26f 1455 /* Add propogated mounts to the tmp_list */
e819f152 1456 if (how & UMOUNT_PROPAGATE)
7b8a53fd 1457 propagate_umount(&tmp_list);
a05964f3 1458
c003b26f 1459 while (!list_empty(&tmp_list)) {
d2921684 1460 struct mnt_namespace *ns;
ce07d891 1461 bool disconnect;
c003b26f 1462 p = list_first_entry(&tmp_list, struct mount, mnt_list);
6776db3d 1463 list_del_init(&p->mnt_expire);
1a4eeaf2 1464 list_del_init(&p->mnt_list);
d2921684
EB
1465 ns = p->mnt_ns;
1466 if (ns) {
1467 ns->mounts--;
1468 __touch_mnt_namespace(ns);
1469 }
143c8c91 1470 p->mnt_ns = NULL;
e819f152 1471 if (how & UMOUNT_SYNC)
48a066e7 1472 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
87b95ce0 1473
f2d0a123 1474 disconnect = disconnect_mount(p, how);
ce07d891
EB
1475
1476 pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1477 disconnect ? &unmounted : NULL);
676da58d 1478 if (mnt_has_parent(p)) {
81b6b061 1479 mnt_add_count(p->mnt_parent, -1);
ce07d891
EB
1480 if (!disconnect) {
1481 /* Don't forget about p */
1482 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1483 } else {
1484 umount_mnt(p);
1485 }
7c4b93d8 1486 }
0f0afb1d 1487 change_mnt_propagation(p, MS_PRIVATE);
1da177e4
LT
1488 }
1489}
1490
b54b9be7 1491static void shrink_submounts(struct mount *mnt);
c35038be 1492
8d0347f6
DH
1493static int do_umount_root(struct super_block *sb)
1494{
1495 int ret = 0;
1496
1497 down_write(&sb->s_umount);
1498 if (!sb_rdonly(sb)) {
1499 struct fs_context *fc;
1500
1501 fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
1502 SB_RDONLY);
1503 if (IS_ERR(fc)) {
1504 ret = PTR_ERR(fc);
1505 } else {
1506 ret = parse_monolithic_mount_data(fc, NULL);
1507 if (!ret)
1508 ret = reconfigure_super(fc);
1509 put_fs_context(fc);
1510 }
1511 }
1512 up_write(&sb->s_umount);
1513 return ret;
1514}
1515
1ab59738 1516static int do_umount(struct mount *mnt, int flags)
1da177e4 1517{
1ab59738 1518 struct super_block *sb = mnt->mnt.mnt_sb;
1da177e4
LT
1519 int retval;
1520
1ab59738 1521 retval = security_sb_umount(&mnt->mnt, flags);
1da177e4
LT
1522 if (retval)
1523 return retval;
1524
1525 /*
1526 * Allow userspace to request a mountpoint be expired rather than
1527 * unmounting unconditionally. Unmount only happens if:
1528 * (1) the mark is already set (the mark is cleared by mntput())
1529 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1530 */
1531 if (flags & MNT_EXPIRE) {
1ab59738 1532 if (&mnt->mnt == current->fs->root.mnt ||
1da177e4
LT
1533 flags & (MNT_FORCE | MNT_DETACH))
1534 return -EINVAL;
1535
b3e19d92
NP
1536 /*
1537 * probably don't strictly need the lock here if we examined
1538 * all race cases, but it's a slowpath.
1539 */
719ea2fb 1540 lock_mount_hash();
83adc753 1541 if (mnt_get_count(mnt) != 2) {
719ea2fb 1542 unlock_mount_hash();
1da177e4 1543 return -EBUSY;
b3e19d92 1544 }
719ea2fb 1545 unlock_mount_hash();
1da177e4 1546
863d684f 1547 if (!xchg(&mnt->mnt_expiry_mark, 1))
1da177e4
LT
1548 return -EAGAIN;
1549 }
1550
1551 /*
1552 * If we may have to abort operations to get out of this
1553 * mount, and they will themselves hold resources we must
1554 * allow the fs to do things. In the Unix tradition of
1555 * 'Gee thats tricky lets do it in userspace' the umount_begin
1556 * might fail to complete on the first run through as other tasks
1557 * must return, and the like. Thats for the mount program to worry
1558 * about for the moment.
1559 */
1560
42faad99 1561 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
42faad99 1562 sb->s_op->umount_begin(sb);
42faad99 1563 }
1da177e4
LT
1564
1565 /*
1566 * No sense to grab the lock for this test, but test itself looks
1567 * somewhat bogus. Suggestions for better replacement?
1568 * Ho-hum... In principle, we might treat that as umount + switch
1569 * to rootfs. GC would eventually take care of the old vfsmount.
1570 * Actually it makes sense, especially if rootfs would contain a
1571 * /reboot - static binary that would close all descriptors and
1572 * call reboot(9). Then init(8) could umount root and exec /reboot.
1573 */
1ab59738 1574 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1da177e4
LT
1575 /*
1576 * Special case for "unmounting" root ...
1577 * we just try to remount it readonly.
1578 */
bc6155d1 1579 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
a1480dcc 1580 return -EPERM;
8d0347f6 1581 return do_umount_root(sb);
1da177e4
LT
1582 }
1583
97216be0 1584 namespace_lock();
719ea2fb 1585 lock_mount_hash();
1da177e4 1586
25d202ed
EB
1587 /* Recheck MNT_LOCKED with the locks held */
1588 retval = -EINVAL;
1589 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1590 goto out;
1591
1592 event++;
48a066e7 1593 if (flags & MNT_DETACH) {
1a4eeaf2 1594 if (!list_empty(&mnt->mnt_list))
e819f152 1595 umount_tree(mnt, UMOUNT_PROPAGATE);
1da177e4 1596 retval = 0;
48a066e7
AV
1597 } else {
1598 shrink_submounts(mnt);
1599 retval = -EBUSY;
1600 if (!propagate_mount_busy(mnt, 2)) {
1601 if (!list_empty(&mnt->mnt_list))
e819f152 1602 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
48a066e7
AV
1603 retval = 0;
1604 }
1da177e4 1605 }
25d202ed 1606out:
719ea2fb 1607 unlock_mount_hash();
e3197d83 1608 namespace_unlock();
1da177e4
LT
1609 return retval;
1610}
1611
80b5dce8
EB
1612/*
1613 * __detach_mounts - lazily unmount all mounts on the specified dentry
1614 *
1615 * During unlink, rmdir, and d_drop it is possible to loose the path
1616 * to an existing mountpoint, and wind up leaking the mount.
1617 * detach_mounts allows lazily unmounting those mounts instead of
1618 * leaking them.
1619 *
1620 * The caller may hold dentry->d_inode->i_mutex.
1621 */
1622void __detach_mounts(struct dentry *dentry)
1623{
1624 struct mountpoint *mp;
1625 struct mount *mnt;
1626
1627 namespace_lock();
3895dbf8 1628 lock_mount_hash();
80b5dce8 1629 mp = lookup_mountpoint(dentry);
adc9b5c0 1630 if (!mp)
80b5dce8
EB
1631 goto out_unlock;
1632
e06b933e 1633 event++;
80b5dce8
EB
1634 while (!hlist_empty(&mp->m_list)) {
1635 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
ce07d891 1636 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
fe78fcc8
EB
1637 hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1638 umount_mnt(mnt);
ce07d891 1639 }
e0c9c0af 1640 else umount_tree(mnt, UMOUNT_CONNECTED);
80b5dce8 1641 }
80b5dce8
EB
1642 put_mountpoint(mp);
1643out_unlock:
3895dbf8 1644 unlock_mount_hash();
80b5dce8
EB
1645 namespace_unlock();
1646}
1647
dd111b31 1648/*
9b40bc90
AV
1649 * Is the caller allowed to modify his namespace?
1650 */
1651static inline bool may_mount(void)
1652{
1653 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1654}
1655
9e8925b6
JL
1656static inline bool may_mandlock(void)
1657{
1658#ifndef CONFIG_MANDATORY_FILE_LOCKING
1659 return false;
1660#endif
95ace754 1661 return capable(CAP_SYS_ADMIN);
9e8925b6
JL
1662}
1663
1da177e4
LT
1664/*
1665 * Now umount can handle mount points as well as block devices.
1666 * This is important for filesystems which use unnamed block devices.
1667 *
1668 * We now support a flag for forced unmount like the other 'big iron'
1669 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1670 */
1671
3a18ef5c 1672int ksys_umount(char __user *name, int flags)
1da177e4 1673{
2d8f3038 1674 struct path path;
900148dc 1675 struct mount *mnt;
1da177e4 1676 int retval;
db1f05bb 1677 int lookup_flags = 0;
1da177e4 1678
db1f05bb
MS
1679 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1680 return -EINVAL;
1681
9b40bc90
AV
1682 if (!may_mount())
1683 return -EPERM;
1684
db1f05bb
MS
1685 if (!(flags & UMOUNT_NOFOLLOW))
1686 lookup_flags |= LOOKUP_FOLLOW;
1687
57d46577
RGB
1688 lookup_flags |= LOOKUP_NO_EVAL;
1689
197df04c 1690 retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
1da177e4
LT
1691 if (retval)
1692 goto out;
900148dc 1693 mnt = real_mount(path.mnt);
1da177e4 1694 retval = -EINVAL;
2d8f3038 1695 if (path.dentry != path.mnt->mnt_root)
1da177e4 1696 goto dput_and_out;
143c8c91 1697 if (!check_mnt(mnt))
1da177e4 1698 goto dput_and_out;
25d202ed 1699 if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
5ff9d8a6 1700 goto dput_and_out;
b2f5d4dc
EB
1701 retval = -EPERM;
1702 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1703 goto dput_and_out;
1da177e4 1704
900148dc 1705 retval = do_umount(mnt, flags);
1da177e4 1706dput_and_out:
429731b1 1707 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
2d8f3038 1708 dput(path.dentry);
900148dc 1709 mntput_no_expire(mnt);
1da177e4
LT
1710out:
1711 return retval;
1712}
1713
3a18ef5c
DB
1714SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1715{
1716 return ksys_umount(name, flags);
1717}
1718
1da177e4
LT
1719#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1720
1721/*
b58fed8b 1722 * The 2.0 compatible umount. No flags.
1da177e4 1723 */
bdc480e3 1724SYSCALL_DEFINE1(oldumount, char __user *, name)
1da177e4 1725{
3a18ef5c 1726 return ksys_umount(name, 0);
1da177e4
LT
1727}
1728
1729#endif
1730
4ce5d2b1 1731static bool is_mnt_ns_file(struct dentry *dentry)
8823c079 1732{
4ce5d2b1 1733 /* Is this a proxy for a mount namespace? */
e149ed2b
AV
1734 return dentry->d_op == &ns_dentry_operations &&
1735 dentry->d_fsdata == &mntns_operations;
4ce5d2b1
EB
1736}
1737
58be2825
AV
1738struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
1739{
1740 return container_of(ns, struct mnt_namespace, ns);
1741}
1742
4ce5d2b1
EB
1743static bool mnt_ns_loop(struct dentry *dentry)
1744{
1745 /* Could bind mounting the mount namespace inode cause a
1746 * mount namespace loop?
1747 */
1748 struct mnt_namespace *mnt_ns;
1749 if (!is_mnt_ns_file(dentry))
1750 return false;
1751
f77c8014 1752 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
8823c079
EB
1753 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1754}
1755
87129cc0 1756struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
36341f64 1757 int flag)
1da177e4 1758{
84d17192 1759 struct mount *res, *p, *q, *r, *parent;
1da177e4 1760
4ce5d2b1
EB
1761 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1762 return ERR_PTR(-EINVAL);
1763
1764 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
be34d1a3 1765 return ERR_PTR(-EINVAL);
9676f0c6 1766
36341f64 1767 res = q = clone_mnt(mnt, dentry, flag);
be34d1a3
DH
1768 if (IS_ERR(q))
1769 return q;
1770
a73324da 1771 q->mnt_mountpoint = mnt->mnt_mountpoint;
1da177e4
LT
1772
1773 p = mnt;
6b41d536 1774 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
315fc83e 1775 struct mount *s;
7ec02ef1 1776 if (!is_subdir(r->mnt_mountpoint, dentry))
1da177e4
LT
1777 continue;
1778
909b0a88 1779 for (s = r; s; s = next_mnt(s, r)) {
4ce5d2b1
EB
1780 if (!(flag & CL_COPY_UNBINDABLE) &&
1781 IS_MNT_UNBINDABLE(s)) {
df7342b2
EB
1782 if (s->mnt.mnt_flags & MNT_LOCKED) {
1783 /* Both unbindable and locked. */
1784 q = ERR_PTR(-EPERM);
1785 goto out;
1786 } else {
1787 s = skip_mnt_tree(s);
1788 continue;
1789 }
4ce5d2b1
EB
1790 }
1791 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1792 is_mnt_ns_file(s->mnt.mnt_root)) {
9676f0c6
RP
1793 s = skip_mnt_tree(s);
1794 continue;
1795 }
0714a533
AV
1796 while (p != s->mnt_parent) {
1797 p = p->mnt_parent;
1798 q = q->mnt_parent;
1da177e4 1799 }
87129cc0 1800 p = s;
84d17192 1801 parent = q;
87129cc0 1802 q = clone_mnt(p, p->mnt.mnt_root, flag);
be34d1a3
DH
1803 if (IS_ERR(q))
1804 goto out;
719ea2fb 1805 lock_mount_hash();
1a4eeaf2 1806 list_add_tail(&q->mnt_list, &res->mnt_list);
1064f874 1807 attach_mnt(q, parent, p->mnt_mp);
719ea2fb 1808 unlock_mount_hash();
1da177e4
LT
1809 }
1810 }
1811 return res;
be34d1a3 1812out:
1da177e4 1813 if (res) {
719ea2fb 1814 lock_mount_hash();
e819f152 1815 umount_tree(res, UMOUNT_SYNC);
719ea2fb 1816 unlock_mount_hash();
1da177e4 1817 }
be34d1a3 1818 return q;
1da177e4
LT
1819}
1820
be34d1a3
DH
1821/* Caller should check returned pointer for errors */
1822
ca71cf71 1823struct vfsmount *collect_mounts(const struct path *path)
8aec0809 1824{
cb338d06 1825 struct mount *tree;
97216be0 1826 namespace_lock();
cd4a4017
EB
1827 if (!check_mnt(real_mount(path->mnt)))
1828 tree = ERR_PTR(-EINVAL);
1829 else
1830 tree = copy_tree(real_mount(path->mnt), path->dentry,
1831 CL_COPY_ALL | CL_PRIVATE);
328e6d90 1832 namespace_unlock();
be34d1a3 1833 if (IS_ERR(tree))
52e220d3 1834 return ERR_CAST(tree);
be34d1a3 1835 return &tree->mnt;
8aec0809
AV
1836}
1837
a07b2000
AV
1838static void free_mnt_ns(struct mnt_namespace *);
1839static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
1840
1841void dissolve_on_fput(struct vfsmount *mnt)
1842{
1843 struct mnt_namespace *ns;
1844 namespace_lock();
1845 lock_mount_hash();
1846 ns = real_mount(mnt)->mnt_ns;
44dfd84a
DH
1847 if (ns) {
1848 if (is_anon_ns(ns))
1849 umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
1850 else
1851 ns = NULL;
1852 }
a07b2000
AV
1853 unlock_mount_hash();
1854 namespace_unlock();
44dfd84a
DH
1855 if (ns)
1856 free_mnt_ns(ns);
a07b2000
AV
1857}
1858
8aec0809
AV
1859void drop_collected_mounts(struct vfsmount *mnt)
1860{
97216be0 1861 namespace_lock();
719ea2fb 1862 lock_mount_hash();
9c8e0a1b 1863 umount_tree(real_mount(mnt), 0);
719ea2fb 1864 unlock_mount_hash();
3ab6abee 1865 namespace_unlock();
8aec0809
AV
1866}
1867
c771d683
MS
1868/**
1869 * clone_private_mount - create a private clone of a path
1870 *
1871 * This creates a new vfsmount, which will be the clone of @path. The new will
1872 * not be attached anywhere in the namespace and will be private (i.e. changes
1873 * to the originating mount won't be propagated into this).
1874 *
1875 * Release with mntput().
1876 */
ca71cf71 1877struct vfsmount *clone_private_mount(const struct path *path)
c771d683
MS
1878{
1879 struct mount *old_mnt = real_mount(path->mnt);
1880 struct mount *new_mnt;
1881
1882 if (IS_MNT_UNBINDABLE(old_mnt))
1883 return ERR_PTR(-EINVAL);
1884
c771d683 1885 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
c771d683
MS
1886 if (IS_ERR(new_mnt))
1887 return ERR_CAST(new_mnt);
1888
1889 return &new_mnt->mnt;
1890}
1891EXPORT_SYMBOL_GPL(clone_private_mount);
1892
1f707137
AV
1893int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1894 struct vfsmount *root)
1895{
1a4eeaf2 1896 struct mount *mnt;
1f707137
AV
1897 int res = f(root, arg);
1898 if (res)
1899 return res;
1a4eeaf2
AV
1900 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1901 res = f(&mnt->mnt, arg);
1f707137
AV
1902 if (res)
1903 return res;
1904 }
1905 return 0;
1906}
1907
3bd045cc
AV
1908static void lock_mnt_tree(struct mount *mnt)
1909{
1910 struct mount *p;
1911
1912 for (p = mnt; p; p = next_mnt(p, mnt)) {
1913 int flags = p->mnt.mnt_flags;
1914 /* Don't allow unprivileged users to change mount flags */
1915 flags |= MNT_LOCK_ATIME;
1916
1917 if (flags & MNT_READONLY)
1918 flags |= MNT_LOCK_READONLY;
1919
1920 if (flags & MNT_NODEV)
1921 flags |= MNT_LOCK_NODEV;
1922
1923 if (flags & MNT_NOSUID)
1924 flags |= MNT_LOCK_NOSUID;
1925
1926 if (flags & MNT_NOEXEC)
1927 flags |= MNT_LOCK_NOEXEC;
1928 /* Don't allow unprivileged users to reveal what is under a mount */
1929 if (list_empty(&p->mnt_expire))
1930 flags |= MNT_LOCKED;
1931 p->mnt.mnt_flags = flags;
1932 }
1933}
1934
4b8b21f4 1935static void cleanup_group_ids(struct mount *mnt, struct mount *end)
719f5d7f 1936{
315fc83e 1937 struct mount *p;
719f5d7f 1938
909b0a88 1939 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
fc7be130 1940 if (p->mnt_group_id && !IS_MNT_SHARED(p))
4b8b21f4 1941 mnt_release_group_id(p);
719f5d7f
MS
1942 }
1943}
1944
4b8b21f4 1945static int invent_group_ids(struct mount *mnt, bool recurse)
719f5d7f 1946{
315fc83e 1947 struct mount *p;
719f5d7f 1948
909b0a88 1949 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
fc7be130 1950 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
4b8b21f4 1951 int err = mnt_alloc_group_id(p);
719f5d7f 1952 if (err) {
4b8b21f4 1953 cleanup_group_ids(mnt, p);
719f5d7f
MS
1954 return err;
1955 }
1956 }
1957 }
1958
1959 return 0;
1960}
1961
d2921684
EB
1962int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1963{
1964 unsigned int max = READ_ONCE(sysctl_mount_max);
1965 unsigned int mounts = 0, old, pending, sum;
1966 struct mount *p;
1967
1968 for (p = mnt; p; p = next_mnt(p, mnt))
1969 mounts++;
1970
1971 old = ns->mounts;
1972 pending = ns->pending_mounts;
1973 sum = old + pending;
1974 if ((old > sum) ||
1975 (pending > sum) ||
1976 (max < sum) ||
1977 (mounts > (max - sum)))
1978 return -ENOSPC;
1979
1980 ns->pending_mounts = pending + mounts;
1981 return 0;
1982}
1983
b90fa9ae
RP
1984/*
1985 * @source_mnt : mount tree to be attached
21444403
RP
1986 * @nd : place the mount tree @source_mnt is attached
1987 * @parent_nd : if non-null, detach the source_mnt from its parent and
1988 * store the parent mount and mountpoint dentry.
1989 * (done when source_mnt is moved)
b90fa9ae
RP
1990 *
1991 * NOTE: in the table below explains the semantics when a source mount
1992 * of a given type is attached to a destination mount of a given type.
9676f0c6
RP
1993 * ---------------------------------------------------------------------------
1994 * | BIND MOUNT OPERATION |
1995 * |**************************************************************************
1996 * | source-->| shared | private | slave | unbindable |
1997 * | dest | | | | |
1998 * | | | | | | |
1999 * | v | | | | |
2000 * |**************************************************************************
2001 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
2002 * | | | | | |
2003 * |non-shared| shared (+) | private | slave (*) | invalid |
2004 * ***************************************************************************
b90fa9ae
RP
2005 * A bind operation clones the source mount and mounts the clone on the
2006 * destination mount.
2007 *
2008 * (++) the cloned mount is propagated to all the mounts in the propagation
2009 * tree of the destination mount and the cloned mount is added to
2010 * the peer group of the source mount.
2011 * (+) the cloned mount is created under the destination mount and is marked
2012 * as shared. The cloned mount is added to the peer group of the source
2013 * mount.
5afe0022
RP
2014 * (+++) the mount is propagated to all the mounts in the propagation tree
2015 * of the destination mount and the cloned mount is made slave
2016 * of the same master as that of the source mount. The cloned mount
2017 * is marked as 'shared and slave'.
2018 * (*) the cloned mount is made a slave of the same master as that of the
2019 * source mount.
2020 *
9676f0c6
RP
2021 * ---------------------------------------------------------------------------
2022 * | MOVE MOUNT OPERATION |
2023 * |**************************************************************************
2024 * | source-->| shared | private | slave | unbindable |
2025 * | dest | | | | |
2026 * | | | | | | |
2027 * | v | | | | |
2028 * |**************************************************************************
2029 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
2030 * | | | | | |
2031 * |non-shared| shared (+*) | private | slave (*) | unbindable |
2032 * ***************************************************************************
5afe0022
RP
2033 *
2034 * (+) the mount is moved to the destination. And is then propagated to
2035 * all the mounts in the propagation tree of the destination mount.
21444403 2036 * (+*) the mount is moved to the destination.
5afe0022
RP
2037 * (+++) the mount is moved to the destination and is then propagated to
2038 * all the mounts belonging to the destination mount's propagation tree.
2039 * the mount is marked as 'shared and slave'.
2040 * (*) the mount continues to be a slave at the new location.
b90fa9ae
RP
2041 *
2042 * if the source mount is a tree, the operations explained above is
2043 * applied to each mount in the tree.
2044 * Must be called without spinlocks held, since this function can sleep
2045 * in allocations.
2046 */
0fb54e50 2047static int attach_recursive_mnt(struct mount *source_mnt,
84d17192
AV
2048 struct mount *dest_mnt,
2049 struct mountpoint *dest_mp,
2050 struct path *parent_path)
b90fa9ae 2051{
3bd045cc 2052 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
38129a13 2053 HLIST_HEAD(tree_list);
d2921684 2054 struct mnt_namespace *ns = dest_mnt->mnt_ns;
1064f874 2055 struct mountpoint *smp;
315fc83e 2056 struct mount *child, *p;
38129a13 2057 struct hlist_node *n;
719f5d7f 2058 int err;
b90fa9ae 2059
1064f874
EB
2060 /* Preallocate a mountpoint in case the new mounts need
2061 * to be tucked under other mounts.
2062 */
2063 smp = get_mountpoint(source_mnt->mnt.mnt_root);
2064 if (IS_ERR(smp))
2065 return PTR_ERR(smp);
2066
d2921684
EB
2067 /* Is there space to add these mounts to the mount namespace? */
2068 if (!parent_path) {
2069 err = count_mounts(ns, source_mnt);
2070 if (err)
2071 goto out;
2072 }
2073
fc7be130 2074 if (IS_MNT_SHARED(dest_mnt)) {
0fb54e50 2075 err = invent_group_ids(source_mnt, true);
719f5d7f
MS
2076 if (err)
2077 goto out;
0b1b901b 2078 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
f2ebb3a9 2079 lock_mount_hash();
0b1b901b
AV
2080 if (err)
2081 goto out_cleanup_ids;
909b0a88 2082 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
0f0afb1d 2083 set_mnt_shared(p);
0b1b901b
AV
2084 } else {
2085 lock_mount_hash();
b90fa9ae 2086 }
1a390689 2087 if (parent_path) {
0fb54e50 2088 detach_mnt(source_mnt, parent_path);
84d17192 2089 attach_mnt(source_mnt, dest_mnt, dest_mp);
143c8c91 2090 touch_mnt_namespace(source_mnt->mnt_ns);
21444403 2091 } else {
44dfd84a
DH
2092 if (source_mnt->mnt_ns) {
2093 /* move from anon - the caller will destroy */
2094 list_del_init(&source_mnt->mnt_ns->list);
2095 }
84d17192 2096 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
1064f874 2097 commit_tree(source_mnt);
21444403 2098 }
b90fa9ae 2099
38129a13 2100 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
1d6a32ac 2101 struct mount *q;
38129a13 2102 hlist_del_init(&child->mnt_hash);
1064f874
EB
2103 q = __lookup_mnt(&child->mnt_parent->mnt,
2104 child->mnt_mountpoint);
2105 if (q)
2106 mnt_change_mountpoint(child, smp, q);
3bd045cc
AV
2107 /* Notice when we are propagating across user namespaces */
2108 if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2109 lock_mnt_tree(child);
d728cf79 2110 child->mnt.mnt_flags &= ~MNT_LOCKED;
1064f874 2111 commit_tree(child);
b90fa9ae 2112 }
1064f874 2113 put_mountpoint(smp);
719ea2fb 2114 unlock_mount_hash();
99b7db7b 2115
b90fa9ae 2116 return 0;
719f5d7f
MS
2117
2118 out_cleanup_ids:
f2ebb3a9
AV
2119 while (!hlist_empty(&tree_list)) {
2120 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
d2921684 2121 child->mnt_parent->mnt_ns->pending_mounts = 0;
e819f152 2122 umount_tree(child, UMOUNT_SYNC);
f2ebb3a9
AV
2123 }
2124 unlock_mount_hash();
0b1b901b 2125 cleanup_group_ids(source_mnt, NULL);
719f5d7f 2126 out:
d2921684 2127 ns->pending_mounts = 0;
1064f874
EB
2128
2129 read_seqlock_excl(&mount_lock);
2130 put_mountpoint(smp);
2131 read_sequnlock_excl(&mount_lock);
2132
719f5d7f 2133 return err;
b90fa9ae
RP
2134}
2135
84d17192 2136static struct mountpoint *lock_mount(struct path *path)
b12cea91
AV
2137{
2138 struct vfsmount *mnt;
84d17192 2139 struct dentry *dentry = path->dentry;
b12cea91 2140retry:
5955102c 2141 inode_lock(dentry->d_inode);
84d17192 2142 if (unlikely(cant_mount(dentry))) {
5955102c 2143 inode_unlock(dentry->d_inode);
84d17192 2144 return ERR_PTR(-ENOENT);
b12cea91 2145 }
97216be0 2146 namespace_lock();
b12cea91 2147 mnt = lookup_mnt(path);
84d17192 2148 if (likely(!mnt)) {
3895dbf8 2149 struct mountpoint *mp = get_mountpoint(dentry);
84d17192 2150 if (IS_ERR(mp)) {
97216be0 2151 namespace_unlock();
5955102c 2152 inode_unlock(dentry->d_inode);
84d17192
AV
2153 return mp;
2154 }
2155 return mp;
2156 }
97216be0 2157 namespace_unlock();
5955102c 2158 inode_unlock(path->dentry->d_inode);
b12cea91
AV
2159 path_put(path);
2160 path->mnt = mnt;
84d17192 2161 dentry = path->dentry = dget(mnt->mnt_root);
b12cea91
AV
2162 goto retry;
2163}
2164
84d17192 2165static void unlock_mount(struct mountpoint *where)
b12cea91 2166{
84d17192 2167 struct dentry *dentry = where->m_dentry;
3895dbf8
EB
2168
2169 read_seqlock_excl(&mount_lock);
84d17192 2170 put_mountpoint(where);
3895dbf8
EB
2171 read_sequnlock_excl(&mount_lock);
2172
328e6d90 2173 namespace_unlock();
5955102c 2174 inode_unlock(dentry->d_inode);
b12cea91
AV
2175}
2176
84d17192 2177static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
1da177e4 2178{
e462ec50 2179 if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
1da177e4
LT
2180 return -EINVAL;
2181
e36cb0b8
DH
2182 if (d_is_dir(mp->m_dentry) !=
2183 d_is_dir(mnt->mnt.mnt_root))
1da177e4
LT
2184 return -ENOTDIR;
2185
84d17192 2186 return attach_recursive_mnt(mnt, p, mp, NULL);
1da177e4
LT
2187}
2188
7a2e8a8f
VA
2189/*
2190 * Sanity check the flags to change_mnt_propagation.
2191 */
2192
e462ec50 2193static int flags_to_propagation_type(int ms_flags)
7a2e8a8f 2194{
e462ec50 2195 int type = ms_flags & ~(MS_REC | MS_SILENT);
7a2e8a8f
VA
2196
2197 /* Fail if any non-propagation flags are set */
2198 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2199 return 0;
2200 /* Only one propagation flag should be set */
2201 if (!is_power_of_2(type))
2202 return 0;
2203 return type;
2204}
2205
07b20889
RP
2206/*
2207 * recursively change the type of the mountpoint.
2208 */
e462ec50 2209static int do_change_type(struct path *path, int ms_flags)
07b20889 2210{
315fc83e 2211 struct mount *m;
4b8b21f4 2212 struct mount *mnt = real_mount(path->mnt);
e462ec50 2213 int recurse = ms_flags & MS_REC;
7a2e8a8f 2214 int type;
719f5d7f 2215 int err = 0;
07b20889 2216
2d92ab3c 2217 if (path->dentry != path->mnt->mnt_root)
07b20889
RP
2218 return -EINVAL;
2219
e462ec50 2220 type = flags_to_propagation_type(ms_flags);
7a2e8a8f
VA
2221 if (!type)
2222 return -EINVAL;
2223
97216be0 2224 namespace_lock();
719f5d7f
MS
2225 if (type == MS_SHARED) {
2226 err = invent_group_ids(mnt, recurse);
2227 if (err)
2228 goto out_unlock;
2229 }
2230
719ea2fb 2231 lock_mount_hash();
909b0a88 2232 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
0f0afb1d 2233 change_mnt_propagation(m, type);
719ea2fb 2234 unlock_mount_hash();
719f5d7f
MS
2235
2236 out_unlock:
97216be0 2237 namespace_unlock();
719f5d7f 2238 return err;
07b20889
RP
2239}
2240
5ff9d8a6
EB
2241static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2242{
2243 struct mount *child;
2244 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2245 if (!is_subdir(child->mnt_mountpoint, dentry))
2246 continue;
2247
2248 if (child->mnt.mnt_flags & MNT_LOCKED)
2249 return true;
2250 }
2251 return false;
2252}
2253
a07b2000
AV
2254static struct mount *__do_loopback(struct path *old_path, int recurse)
2255{
2256 struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2257
2258 if (IS_MNT_UNBINDABLE(old))
2259 return mnt;
2260
2261 if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2262 return mnt;
2263
2264 if (!recurse && has_locked_children(old, old_path->dentry))
2265 return mnt;
2266
2267 if (recurse)
2268 mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2269 else
2270 mnt = clone_mnt(old, old_path->dentry, 0);
2271
2272 if (!IS_ERR(mnt))
2273 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2274
2275 return mnt;
2276}
2277
1da177e4
LT
2278/*
2279 * do loopback mount.
2280 */
808d4e3c 2281static int do_loopback(struct path *path, const char *old_name,
2dafe1c4 2282 int recurse)
1da177e4 2283{
2d92ab3c 2284 struct path old_path;
a07b2000 2285 struct mount *mnt = NULL, *parent;
84d17192 2286 struct mountpoint *mp;
57eccb83 2287 int err;
1da177e4
LT
2288 if (!old_name || !*old_name)
2289 return -EINVAL;
815d405c 2290 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
1da177e4
LT
2291 if (err)
2292 return err;
2293
8823c079 2294 err = -EINVAL;
4ce5d2b1 2295 if (mnt_ns_loop(old_path.dentry))
dd111b31 2296 goto out;
8823c079 2297
84d17192 2298 mp = lock_mount(path);
a07b2000
AV
2299 if (IS_ERR(mp)) {
2300 err = PTR_ERR(mp);
b12cea91 2301 goto out;
a07b2000 2302 }
b12cea91 2303
84d17192 2304 parent = real_mount(path->mnt);
e149ed2b
AV
2305 if (!check_mnt(parent))
2306 goto out2;
2307
a07b2000 2308 mnt = __do_loopback(&old_path, recurse);
be34d1a3
DH
2309 if (IS_ERR(mnt)) {
2310 err = PTR_ERR(mnt);
e9c5d8a5 2311 goto out2;
be34d1a3 2312 }
ccd48bc7 2313
84d17192 2314 err = graft_tree(mnt, parent, mp);
ccd48bc7 2315 if (err) {
719ea2fb 2316 lock_mount_hash();
e819f152 2317 umount_tree(mnt, UMOUNT_SYNC);
719ea2fb 2318 unlock_mount_hash();
5b83d2c5 2319 }
b12cea91 2320out2:
84d17192 2321 unlock_mount(mp);
ccd48bc7 2322out:
2d92ab3c 2323 path_put(&old_path);
1da177e4
LT
2324 return err;
2325}
2326
a07b2000
AV
2327static struct file *open_detached_copy(struct path *path, bool recursive)
2328{
2329 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2330 struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2331 struct mount *mnt, *p;
2332 struct file *file;
2333
2334 if (IS_ERR(ns))
2335 return ERR_CAST(ns);
2336
2337 namespace_lock();
2338 mnt = __do_loopback(path, recursive);
2339 if (IS_ERR(mnt)) {
2340 namespace_unlock();
2341 free_mnt_ns(ns);
2342 return ERR_CAST(mnt);
2343 }
2344
2345 lock_mount_hash();
2346 for (p = mnt; p; p = next_mnt(p, mnt)) {
2347 p->mnt_ns = ns;
2348 ns->mounts++;
2349 }
2350 ns->root = mnt;
2351 list_add_tail(&ns->list, &mnt->mnt_list);
2352 mntget(&mnt->mnt);
2353 unlock_mount_hash();
2354 namespace_unlock();
2355
2356 mntput(path->mnt);
2357 path->mnt = &mnt->mnt;
2358 file = dentry_open(path, O_PATH, current_cred());
2359 if (IS_ERR(file))
2360 dissolve_on_fput(path->mnt);
2361 else
2362 file->f_mode |= FMODE_NEED_UNMOUNT;
2363 return file;
2364}
2365
2366SYSCALL_DEFINE3(open_tree, int, dfd, const char *, filename, unsigned, flags)
2367{
2368 struct file *file;
2369 struct path path;
2370 int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2371 bool detached = flags & OPEN_TREE_CLONE;
2372 int error;
2373 int fd;
2374
2375 BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2376
2377 if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2378 AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2379 OPEN_TREE_CLOEXEC))
2380 return -EINVAL;
2381
2382 if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2383 return -EINVAL;
2384
2385 if (flags & AT_NO_AUTOMOUNT)
2386 lookup_flags &= ~LOOKUP_AUTOMOUNT;
2387 if (flags & AT_SYMLINK_NOFOLLOW)
2388 lookup_flags &= ~LOOKUP_FOLLOW;
2389 if (flags & AT_EMPTY_PATH)
2390 lookup_flags |= LOOKUP_EMPTY;
2391
2392 if (detached && !may_mount())
2393 return -EPERM;
2394
2395 fd = get_unused_fd_flags(flags & O_CLOEXEC);
2396 if (fd < 0)
2397 return fd;
2398
2399 error = user_path_at(dfd, filename, lookup_flags, &path);
2400 if (unlikely(error)) {
2401 file = ERR_PTR(error);
2402 } else {
2403 if (detached)
2404 file = open_detached_copy(&path, flags & AT_RECURSIVE);
2405 else
2406 file = dentry_open(&path, O_PATH, current_cred());
2407 path_put(&path);
2408 }
2409 if (IS_ERR(file)) {
2410 put_unused_fd(fd);
2411 return PTR_ERR(file);
2412 }
2413 fd_install(fd, file);
2414 return fd;
2415}
2416
43f5e655
DH
2417/*
2418 * Don't allow locked mount flags to be cleared.
2419 *
2420 * No locks need to be held here while testing the various MNT_LOCK
2421 * flags because those flags can never be cleared once they are set.
2422 */
2423static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd 2424{
43f5e655
DH
2425 unsigned int fl = mnt->mnt.mnt_flags;
2426
2427 if ((fl & MNT_LOCK_READONLY) &&
2428 !(mnt_flags & MNT_READONLY))
2429 return false;
2430
2431 if ((fl & MNT_LOCK_NODEV) &&
2432 !(mnt_flags & MNT_NODEV))
2433 return false;
2434
2435 if ((fl & MNT_LOCK_NOSUID) &&
2436 !(mnt_flags & MNT_NOSUID))
2437 return false;
2438
2439 if ((fl & MNT_LOCK_NOEXEC) &&
2440 !(mnt_flags & MNT_NOEXEC))
2441 return false;
2442
2443 if ((fl & MNT_LOCK_ATIME) &&
2444 ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
2445 return false;
2e4b7fcd 2446
43f5e655
DH
2447 return true;
2448}
2449
2450static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd 2451{
43f5e655 2452 bool readonly_request = (mnt_flags & MNT_READONLY);
2e4b7fcd 2453
43f5e655 2454 if (readonly_request == __mnt_is_readonly(&mnt->mnt))
2e4b7fcd
DH
2455 return 0;
2456
2457 if (readonly_request)
43f5e655
DH
2458 return mnt_make_readonly(mnt);
2459
2460 return __mnt_unmake_readonly(mnt);
2461}
2462
2463/*
2464 * Update the user-settable attributes on a mount. The caller must hold
2465 * sb->s_umount for writing.
2466 */
2467static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
2468{
2469 lock_mount_hash();
2470 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2471 mnt->mnt.mnt_flags = mnt_flags;
2472 touch_mnt_namespace(mnt->mnt_ns);
2473 unlock_mount_hash();
2474}
2475
2476/*
2477 * Handle reconfiguration of the mountpoint only without alteration of the
2478 * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
2479 * to mount(2).
2480 */
2481static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
2482{
2483 struct super_block *sb = path->mnt->mnt_sb;
2484 struct mount *mnt = real_mount(path->mnt);
2485 int ret;
2486
2487 if (!check_mnt(mnt))
2488 return -EINVAL;
2489
2490 if (path->dentry != mnt->mnt.mnt_root)
2491 return -EINVAL;
2492
2493 if (!can_change_locked_flags(mnt, mnt_flags))
2494 return -EPERM;
2495
2496 down_write(&sb->s_umount);
2497 ret = change_mount_ro_state(mnt, mnt_flags);
2498 if (ret == 0)
2499 set_mount_attributes(mnt, mnt_flags);
2500 up_write(&sb->s_umount);
2501 return ret;
2e4b7fcd
DH
2502}
2503
1da177e4
LT
2504/*
2505 * change filesystem flags. dir should be a physical root of filesystem.
2506 * If you've mounted a non-root directory somewhere and want to do remount
2507 * on it - tough luck.
2508 */
e462ec50
DH
2509static int do_remount(struct path *path, int ms_flags, int sb_flags,
2510 int mnt_flags, void *data)
1da177e4
LT
2511{
2512 int err;
2d92ab3c 2513 struct super_block *sb = path->mnt->mnt_sb;
143c8c91 2514 struct mount *mnt = real_mount(path->mnt);
8d0347f6 2515 struct fs_context *fc;
1da177e4 2516
143c8c91 2517 if (!check_mnt(mnt))
1da177e4
LT
2518 return -EINVAL;
2519
2d92ab3c 2520 if (path->dentry != path->mnt->mnt_root)
1da177e4
LT
2521 return -EINVAL;
2522
43f5e655 2523 if (!can_change_locked_flags(mnt, mnt_flags))
9566d674 2524 return -EPERM;
9566d674 2525
8d0347f6
DH
2526 fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
2527 if (IS_ERR(fc))
2528 return PTR_ERR(fc);
ff36fe2c 2529
8d0347f6
DH
2530 err = parse_monolithic_mount_data(fc, data);
2531 if (!err) {
2532 down_write(&sb->s_umount);
2533 err = -EPERM;
2534 if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
2535 err = reconfigure_super(fc);
2536 if (!err)
2537 set_mount_attributes(mnt, mnt_flags);
2538 }
2539 up_write(&sb->s_umount);
0e55a7cc 2540 }
8d0347f6 2541 put_fs_context(fc);
1da177e4
LT
2542 return err;
2543}
2544
cbbe362c 2545static inline int tree_contains_unbindable(struct mount *mnt)
9676f0c6 2546{
315fc83e 2547 struct mount *p;
909b0a88 2548 for (p = mnt; p; p = next_mnt(p, mnt)) {
fc7be130 2549 if (IS_MNT_UNBINDABLE(p))
9676f0c6
RP
2550 return 1;
2551 }
2552 return 0;
2553}
2554
44dfd84a
DH
2555/*
2556 * Check that there aren't references to earlier/same mount namespaces in the
2557 * specified subtree. Such references can act as pins for mount namespaces
2558 * that aren't checked by the mount-cycle checking code, thereby allowing
2559 * cycles to be made.
2560 */
2561static bool check_for_nsfs_mounts(struct mount *subtree)
2562{
2563 struct mount *p;
2564 bool ret = false;
2565
2566 lock_mount_hash();
2567 for (p = subtree; p; p = next_mnt(p, subtree))
2568 if (mnt_ns_loop(p->mnt.mnt_root))
2569 goto out;
2570
2571 ret = true;
2572out:
2573 unlock_mount_hash();
2574 return ret;
2575}
2576
2db154b3 2577static int do_move_mount(struct path *old_path, struct path *new_path)
1da177e4 2578{
2db154b3 2579 struct path parent_path = {.mnt = NULL, .dentry = NULL};
44dfd84a 2580 struct mnt_namespace *ns;
676da58d 2581 struct mount *p;
0fb54e50 2582 struct mount *old;
84d17192 2583 struct mountpoint *mp;
57eccb83 2584 int err;
44dfd84a 2585 bool attached;
1da177e4 2586
2db154b3 2587 mp = lock_mount(new_path);
84d17192 2588 if (IS_ERR(mp))
2db154b3 2589 return PTR_ERR(mp);
cc53ce53 2590
2db154b3
DH
2591 old = real_mount(old_path->mnt);
2592 p = real_mount(new_path->mnt);
44dfd84a
DH
2593 attached = mnt_has_parent(old);
2594 ns = old->mnt_ns;
143c8c91 2595
1da177e4 2596 err = -EINVAL;
44dfd84a
DH
2597 /* The mountpoint must be in our namespace. */
2598 if (!check_mnt(p))
2db154b3 2599 goto out;
1da177e4 2600
570d7a98
EB
2601 /* The thing moved must be mounted... */
2602 if (!is_mounted(&old->mnt))
44dfd84a
DH
2603 goto out;
2604
570d7a98
EB
2605 /* ... and either ours or the root of anon namespace */
2606 if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
2db154b3 2607 goto out;
5ff9d8a6 2608
2db154b3
DH
2609 if (old->mnt.mnt_flags & MNT_LOCKED)
2610 goto out;
1da177e4 2611
2db154b3
DH
2612 if (old_path->dentry != old_path->mnt->mnt_root)
2613 goto out;
1da177e4 2614
2db154b3
DH
2615 if (d_is_dir(new_path->dentry) !=
2616 d_is_dir(old_path->dentry))
2617 goto out;
21444403
RP
2618 /*
2619 * Don't move a mount residing in a shared parent.
2620 */
44dfd84a 2621 if (attached && IS_MNT_SHARED(old->mnt_parent))
2db154b3 2622 goto out;
9676f0c6
RP
2623 /*
2624 * Don't move a mount tree containing unbindable mounts to a destination
2625 * mount which is shared.
2626 */
fc7be130 2627 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
2db154b3 2628 goto out;
1da177e4 2629 err = -ELOOP;
44dfd84a
DH
2630 if (!check_for_nsfs_mounts(old))
2631 goto out;
fc7be130 2632 for (; mnt_has_parent(p); p = p->mnt_parent)
676da58d 2633 if (p == old)
2db154b3 2634 goto out;
1da177e4 2635
2db154b3 2636 err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
44dfd84a 2637 attached ? &parent_path : NULL);
4ac91378 2638 if (err)
2db154b3 2639 goto out;
1da177e4
LT
2640
2641 /* if the mount is moved, it should no longer be expire
2642 * automatically */
6776db3d 2643 list_del_init(&old->mnt_expire);
1da177e4 2644out:
2db154b3 2645 unlock_mount(mp);
44dfd84a 2646 if (!err) {
1a390689 2647 path_put(&parent_path);
44dfd84a
DH
2648 if (!attached)
2649 free_mnt_ns(ns);
2650 }
2db154b3
DH
2651 return err;
2652}
2653
2654static int do_move_mount_old(struct path *path, const char *old_name)
2655{
2656 struct path old_path;
2657 int err;
2658
2659 if (!old_name || !*old_name)
2660 return -EINVAL;
2661
2662 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2663 if (err)
2664 return err;
2665
2666 err = do_move_mount(&old_path, path);
2d92ab3c 2667 path_put(&old_path);
1da177e4
LT
2668 return err;
2669}
2670
9d412a43
AV
2671/*
2672 * add a mount into a namespace's mount tree
2673 */
95bc5f25 2674static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
9d412a43 2675{
84d17192
AV
2676 struct mountpoint *mp;
2677 struct mount *parent;
9d412a43
AV
2678 int err;
2679
f2ebb3a9 2680 mnt_flags &= ~MNT_INTERNAL_FLAGS;
9d412a43 2681
84d17192
AV
2682 mp = lock_mount(path);
2683 if (IS_ERR(mp))
2684 return PTR_ERR(mp);
9d412a43 2685
84d17192 2686 parent = real_mount(path->mnt);
9d412a43 2687 err = -EINVAL;
84d17192 2688 if (unlikely(!check_mnt(parent))) {
156cacb1
AV
2689 /* that's acceptable only for automounts done in private ns */
2690 if (!(mnt_flags & MNT_SHRINKABLE))
2691 goto unlock;
2692 /* ... and for those we'd better have mountpoint still alive */
84d17192 2693 if (!parent->mnt_ns)
156cacb1
AV
2694 goto unlock;
2695 }
9d412a43
AV
2696
2697 /* Refuse the same filesystem on the same mount point */
2698 err = -EBUSY;
95bc5f25 2699 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
9d412a43
AV
2700 path->mnt->mnt_root == path->dentry)
2701 goto unlock;
2702
2703 err = -EINVAL;
e36cb0b8 2704 if (d_is_symlink(newmnt->mnt.mnt_root))
9d412a43
AV
2705 goto unlock;
2706
95bc5f25 2707 newmnt->mnt.mnt_flags = mnt_flags;
84d17192 2708 err = graft_tree(newmnt, parent, mp);
9d412a43
AV
2709
2710unlock:
84d17192 2711 unlock_mount(mp);
9d412a43
AV
2712 return err;
2713}
b1e75df4 2714
132e4608
DH
2715static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
2716
2717/*
2718 * Create a new mount using a superblock configuration and request it
2719 * be added to the namespace tree.
2720 */
2721static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
2722 unsigned int mnt_flags)
2723{
2724 struct vfsmount *mnt;
2725 struct super_block *sb = fc->root->d_sb;
2726 int error;
2727
c9ce29ed
AV
2728 error = security_sb_kern_mount(sb);
2729 if (!error && mount_too_revealing(sb, &mnt_flags))
2730 error = -EPERM;
2731
2732 if (unlikely(error)) {
2733 fc_drop_locked(fc);
2734 return error;
132e4608
DH
2735 }
2736
2737 up_write(&sb->s_umount);
2738
2739 mnt = vfs_create_mount(fc);
2740 if (IS_ERR(mnt))
2741 return PTR_ERR(mnt);
2742
2743 error = do_add_mount(real_mount(mnt), mountpoint, mnt_flags);
2744 if (error < 0)
2745 mntput(mnt);
2746 return error;
2747}
1b852bce 2748
1da177e4
LT
2749/*
2750 * create a new mount for userspace and request it to be added into the
2751 * namespace's tree
2752 */
e462ec50 2753static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
808d4e3c 2754 int mnt_flags, const char *name, void *data)
1da177e4 2755{
0c55cfc4 2756 struct file_system_type *type;
a0c9a8b8
AV
2757 struct fs_context *fc;
2758 const char *subtype = NULL;
2759 int err = 0;
1da177e4 2760
0c55cfc4 2761 if (!fstype)
1da177e4
LT
2762 return -EINVAL;
2763
0c55cfc4
EB
2764 type = get_fs_type(fstype);
2765 if (!type)
2766 return -ENODEV;
2767
a0c9a8b8
AV
2768 if (type->fs_flags & FS_HAS_SUBTYPE) {
2769 subtype = strchr(fstype, '.');
2770 if (subtype) {
2771 subtype++;
2772 if (!*subtype) {
2773 put_filesystem(type);
2774 return -EINVAL;
2775 }
2776 } else {
2777 subtype = "";
2778 }
2779 }
0c55cfc4 2780
a0c9a8b8 2781 fc = fs_context_for_mount(type, sb_flags);
0c55cfc4 2782 put_filesystem(type);
a0c9a8b8
AV
2783 if (IS_ERR(fc))
2784 return PTR_ERR(fc);
2785
3e1aeb00
DH
2786 if (subtype)
2787 err = vfs_parse_fs_string(fc, "subtype",
2788 subtype, strlen(subtype));
2789 if (!err && name)
2790 err = vfs_parse_fs_string(fc, "source", name, strlen(name));
a0c9a8b8
AV
2791 if (!err)
2792 err = parse_monolithic_mount_data(fc, data);
2793 if (!err)
2794 err = vfs_get_tree(fc);
132e4608
DH
2795 if (!err)
2796 err = do_new_mount_fc(fc, path, mnt_flags);
8654df4e 2797
a0c9a8b8 2798 put_fs_context(fc);
15f9a3f3 2799 return err;
1da177e4
LT
2800}
2801
19a167af
AV
2802int finish_automount(struct vfsmount *m, struct path *path)
2803{
6776db3d 2804 struct mount *mnt = real_mount(m);
19a167af
AV
2805 int err;
2806 /* The new mount record should have at least 2 refs to prevent it being
2807 * expired before we get a chance to add it
2808 */
6776db3d 2809 BUG_ON(mnt_get_count(mnt) < 2);
19a167af
AV
2810
2811 if (m->mnt_sb == path->mnt->mnt_sb &&
2812 m->mnt_root == path->dentry) {
b1e75df4
AV
2813 err = -ELOOP;
2814 goto fail;
19a167af
AV
2815 }
2816
95bc5f25 2817 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
b1e75df4
AV
2818 if (!err)
2819 return 0;
2820fail:
2821 /* remove m from any expiration list it may be on */
6776db3d 2822 if (!list_empty(&mnt->mnt_expire)) {
97216be0 2823 namespace_lock();
6776db3d 2824 list_del_init(&mnt->mnt_expire);
97216be0 2825 namespace_unlock();
19a167af 2826 }
b1e75df4
AV
2827 mntput(m);
2828 mntput(m);
19a167af
AV
2829 return err;
2830}
2831
ea5b778a
DH
2832/**
2833 * mnt_set_expiry - Put a mount on an expiration list
2834 * @mnt: The mount to list.
2835 * @expiry_list: The list to add the mount to.
2836 */
2837void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2838{
97216be0 2839 namespace_lock();
ea5b778a 2840
6776db3d 2841 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
ea5b778a 2842
97216be0 2843 namespace_unlock();
ea5b778a
DH
2844}
2845EXPORT_SYMBOL(mnt_set_expiry);
2846
1da177e4
LT
2847/*
2848 * process a list of expirable mountpoints with the intent of discarding any
2849 * mountpoints that aren't in use and haven't been touched since last we came
2850 * here
2851 */
2852void mark_mounts_for_expiry(struct list_head *mounts)
2853{
761d5c38 2854 struct mount *mnt, *next;
1da177e4
LT
2855 LIST_HEAD(graveyard);
2856
2857 if (list_empty(mounts))
2858 return;
2859
97216be0 2860 namespace_lock();
719ea2fb 2861 lock_mount_hash();
1da177e4
LT
2862
2863 /* extract from the expiration list every vfsmount that matches the
2864 * following criteria:
2865 * - only referenced by its parent vfsmount
2866 * - still marked for expiry (marked on the last call here; marks are
2867 * cleared by mntput())
2868 */
6776db3d 2869 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
863d684f 2870 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1ab59738 2871 propagate_mount_busy(mnt, 1))
1da177e4 2872 continue;
6776db3d 2873 list_move(&mnt->mnt_expire, &graveyard);
1da177e4 2874 }
bcc5c7d2 2875 while (!list_empty(&graveyard)) {
6776db3d 2876 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
143c8c91 2877 touch_mnt_namespace(mnt->mnt_ns);
e819f152 2878 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2 2879 }
719ea2fb 2880 unlock_mount_hash();
3ab6abee 2881 namespace_unlock();
5528f911
TM
2882}
2883
2884EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2885
2886/*
2887 * Ripoff of 'select_parent()'
2888 *
2889 * search the list of submounts for a given mountpoint, and move any
2890 * shrinkable submounts to the 'graveyard' list.
2891 */
692afc31 2892static int select_submounts(struct mount *parent, struct list_head *graveyard)
5528f911 2893{
692afc31 2894 struct mount *this_parent = parent;
5528f911
TM
2895 struct list_head *next;
2896 int found = 0;
2897
2898repeat:
6b41d536 2899 next = this_parent->mnt_mounts.next;
5528f911 2900resume:
6b41d536 2901 while (next != &this_parent->mnt_mounts) {
5528f911 2902 struct list_head *tmp = next;
6b41d536 2903 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
5528f911
TM
2904
2905 next = tmp->next;
692afc31 2906 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
1da177e4 2907 continue;
5528f911
TM
2908 /*
2909 * Descend a level if the d_mounts list is non-empty.
2910 */
6b41d536 2911 if (!list_empty(&mnt->mnt_mounts)) {
5528f911
TM
2912 this_parent = mnt;
2913 goto repeat;
2914 }
1da177e4 2915
1ab59738 2916 if (!propagate_mount_busy(mnt, 1)) {
6776db3d 2917 list_move_tail(&mnt->mnt_expire, graveyard);
5528f911
TM
2918 found++;
2919 }
1da177e4 2920 }
5528f911
TM
2921 /*
2922 * All done at this level ... ascend and resume the search
2923 */
2924 if (this_parent != parent) {
6b41d536 2925 next = this_parent->mnt_child.next;
0714a533 2926 this_parent = this_parent->mnt_parent;
5528f911
TM
2927 goto resume;
2928 }
2929 return found;
2930}
2931
2932/*
2933 * process a list of expirable mountpoints with the intent of discarding any
2934 * submounts of a specific parent mountpoint
99b7db7b 2935 *
48a066e7 2936 * mount_lock must be held for write
5528f911 2937 */
b54b9be7 2938static void shrink_submounts(struct mount *mnt)
5528f911
TM
2939{
2940 LIST_HEAD(graveyard);
761d5c38 2941 struct mount *m;
5528f911 2942
5528f911 2943 /* extract submounts of 'mountpoint' from the expiration list */
c35038be 2944 while (select_submounts(mnt, &graveyard)) {
bcc5c7d2 2945 while (!list_empty(&graveyard)) {
761d5c38 2946 m = list_first_entry(&graveyard, struct mount,
6776db3d 2947 mnt_expire);
143c8c91 2948 touch_mnt_namespace(m->mnt_ns);
e819f152 2949 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2
AV
2950 }
2951 }
1da177e4
LT
2952}
2953
1da177e4
LT
2954/*
2955 * Some copy_from_user() implementations do not return the exact number of
2956 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2957 * Note that this function differs from copy_from_user() in that it will oops
2958 * on bad values of `to', rather than returning a short copy.
2959 */
b58fed8b
RP
2960static long exact_copy_from_user(void *to, const void __user * from,
2961 unsigned long n)
1da177e4
LT
2962{
2963 char *t = to;
2964 const char __user *f = from;
2965 char c;
2966
96d4f267 2967 if (!access_ok(from, n))
1da177e4
LT
2968 return n;
2969
2970 while (n) {
2971 if (__get_user(c, f)) {
2972 memset(t, 0, n);
2973 break;
2974 }
2975 *t++ = c;
2976 f++;
2977 n--;
2978 }
2979 return n;
2980}
2981
b40ef869 2982void *copy_mount_options(const void __user * data)
1da177e4
LT
2983{
2984 int i;
1da177e4 2985 unsigned long size;
b40ef869 2986 char *copy;
b58fed8b 2987
1da177e4 2988 if (!data)
b40ef869 2989 return NULL;
1da177e4 2990
b40ef869
AV
2991 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2992 if (!copy)
2993 return ERR_PTR(-ENOMEM);
1da177e4
LT
2994
2995 /* We only care that *some* data at the address the user
2996 * gave us is valid. Just in case, we'll zero
2997 * the remainder of the page.
2998 */
2999 /* copy_from_user cannot cross TASK_SIZE ! */
3000 size = TASK_SIZE - (unsigned long)data;
3001 if (size > PAGE_SIZE)
3002 size = PAGE_SIZE;
3003
b40ef869 3004 i = size - exact_copy_from_user(copy, data, size);
1da177e4 3005 if (!i) {
b40ef869
AV
3006 kfree(copy);
3007 return ERR_PTR(-EFAULT);
1da177e4
LT
3008 }
3009 if (i != PAGE_SIZE)
b40ef869
AV
3010 memset(copy + i, 0, PAGE_SIZE - i);
3011 return copy;
1da177e4
LT
3012}
3013
b8850d1f 3014char *copy_mount_string(const void __user *data)
eca6f534 3015{
fbdb4401 3016 return data ? strndup_user(data, PATH_MAX) : NULL;
eca6f534
VN
3017}
3018
1da177e4
LT
3019/*
3020 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
3021 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
3022 *
3023 * data is a (void *) that can point to any structure up to
3024 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
3025 * information (or be NULL).
3026 *
3027 * Pre-0.97 versions of mount() didn't have a flags word.
3028 * When the flags word was introduced its top half was required
3029 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
3030 * Therefore, if this magic number is present, it carries no information
3031 * and must be discarded.
3032 */
5e6123f3 3033long do_mount(const char *dev_name, const char __user *dir_name,
808d4e3c 3034 const char *type_page, unsigned long flags, void *data_page)
1da177e4 3035{
2d92ab3c 3036 struct path path;
e462ec50 3037 unsigned int mnt_flags = 0, sb_flags;
1da177e4 3038 int retval = 0;
1da177e4
LT
3039
3040 /* Discard magic */
3041 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
3042 flags &= ~MS_MGC_MSK;
3043
3044 /* Basic sanity checks */
1da177e4
LT
3045 if (data_page)
3046 ((char *)data_page)[PAGE_SIZE - 1] = 0;
3047
e462ec50
DH
3048 if (flags & MS_NOUSER)
3049 return -EINVAL;
3050
a27ab9f2 3051 /* ... and get the mountpoint */
5e6123f3 3052 retval = user_path(dir_name, &path);
a27ab9f2
TH
3053 if (retval)
3054 return retval;
3055
3056 retval = security_sb_mount(dev_name, &path,
3057 type_page, flags, data_page);
0d5cadb8
AV
3058 if (!retval && !may_mount())
3059 retval = -EPERM;
e462ec50 3060 if (!retval && (flags & SB_MANDLOCK) && !may_mandlock())
9e8925b6 3061 retval = -EPERM;
a27ab9f2
TH
3062 if (retval)
3063 goto dput_out;
3064
613cbe3d
AK
3065 /* Default to relatime unless overriden */
3066 if (!(flags & MS_NOATIME))
3067 mnt_flags |= MNT_RELATIME;
0a1c01c9 3068
1da177e4
LT
3069 /* Separate the per-mountpoint flags */
3070 if (flags & MS_NOSUID)
3071 mnt_flags |= MNT_NOSUID;
3072 if (flags & MS_NODEV)
3073 mnt_flags |= MNT_NODEV;
3074 if (flags & MS_NOEXEC)
3075 mnt_flags |= MNT_NOEXEC;
fc33a7bb
CH
3076 if (flags & MS_NOATIME)
3077 mnt_flags |= MNT_NOATIME;
3078 if (flags & MS_NODIRATIME)
3079 mnt_flags |= MNT_NODIRATIME;
d0adde57
MG
3080 if (flags & MS_STRICTATIME)
3081 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
a9e5b732 3082 if (flags & MS_RDONLY)
2e4b7fcd 3083 mnt_flags |= MNT_READONLY;
fc33a7bb 3084
ffbc6f0e
EB
3085 /* The default atime for remount is preservation */
3086 if ((flags & MS_REMOUNT) &&
3087 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3088 MS_STRICTATIME)) == 0)) {
3089 mnt_flags &= ~MNT_ATIME_MASK;
3090 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
3091 }
3092
e462ec50
DH
3093 sb_flags = flags & (SB_RDONLY |
3094 SB_SYNCHRONOUS |
3095 SB_MANDLOCK |
3096 SB_DIRSYNC |
3097 SB_SILENT |
917086ff 3098 SB_POSIXACL |
d7ee9469 3099 SB_LAZYTIME |
917086ff 3100 SB_I_VERSION);
1da177e4 3101
43f5e655
DH
3102 if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
3103 retval = do_reconfigure_mnt(&path, mnt_flags);
3104 else if (flags & MS_REMOUNT)
e462ec50 3105 retval = do_remount(&path, flags, sb_flags, mnt_flags,
1da177e4
LT
3106 data_page);
3107 else if (flags & MS_BIND)
2d92ab3c 3108 retval = do_loopback(&path, dev_name, flags & MS_REC);
9676f0c6 3109 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2d92ab3c 3110 retval = do_change_type(&path, flags);
1da177e4 3111 else if (flags & MS_MOVE)
2db154b3 3112 retval = do_move_mount_old(&path, dev_name);
1da177e4 3113 else
e462ec50 3114 retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
1da177e4
LT
3115 dev_name, data_page);
3116dput_out:
2d92ab3c 3117 path_put(&path);
1da177e4
LT
3118 return retval;
3119}
3120
537f7ccb
EB
3121static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3122{
3123 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3124}
3125
3126static void dec_mnt_namespaces(struct ucounts *ucounts)
3127{
3128 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3129}
3130
771b1371
EB
3131static void free_mnt_ns(struct mnt_namespace *ns)
3132{
74e83122
AV
3133 if (!is_anon_ns(ns))
3134 ns_free_inum(&ns->ns);
537f7ccb 3135 dec_mnt_namespaces(ns->ucounts);
771b1371
EB
3136 put_user_ns(ns->user_ns);
3137 kfree(ns);
3138}
3139
8823c079
EB
3140/*
3141 * Assign a sequence number so we can detect when we attempt to bind
3142 * mount a reference to an older mount namespace into the current
3143 * mount namespace, preventing reference counting loops. A 64bit
3144 * number incrementing at 10Ghz will take 12,427 years to wrap which
3145 * is effectively never, so we can ignore the possibility.
3146 */
3147static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
3148
74e83122 3149static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
cf8d2c11
TM
3150{
3151 struct mnt_namespace *new_ns;
537f7ccb 3152 struct ucounts *ucounts;
98f842e6 3153 int ret;
cf8d2c11 3154
537f7ccb
EB
3155 ucounts = inc_mnt_namespaces(user_ns);
3156 if (!ucounts)
df75e774 3157 return ERR_PTR(-ENOSPC);
537f7ccb 3158
74e83122 3159 new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
537f7ccb
EB
3160 if (!new_ns) {
3161 dec_mnt_namespaces(ucounts);
cf8d2c11 3162 return ERR_PTR(-ENOMEM);
537f7ccb 3163 }
74e83122
AV
3164 if (!anon) {
3165 ret = ns_alloc_inum(&new_ns->ns);
3166 if (ret) {
3167 kfree(new_ns);
3168 dec_mnt_namespaces(ucounts);
3169 return ERR_PTR(ret);
3170 }
98f842e6 3171 }
33c42940 3172 new_ns->ns.ops = &mntns_operations;
74e83122
AV
3173 if (!anon)
3174 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
cf8d2c11 3175 atomic_set(&new_ns->count, 1);
cf8d2c11
TM
3176 INIT_LIST_HEAD(&new_ns->list);
3177 init_waitqueue_head(&new_ns->poll);
771b1371 3178 new_ns->user_ns = get_user_ns(user_ns);
537f7ccb 3179 new_ns->ucounts = ucounts;
cf8d2c11
TM
3180 return new_ns;
3181}
3182
0766f788 3183__latent_entropy
9559f689
AV
3184struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
3185 struct user_namespace *user_ns, struct fs_struct *new_fs)
1da177e4 3186{
6b3286ed 3187 struct mnt_namespace *new_ns;
7f2da1e7 3188 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
315fc83e 3189 struct mount *p, *q;
9559f689 3190 struct mount *old;
cb338d06 3191 struct mount *new;
7a472ef4 3192 int copy_flags;
1da177e4 3193
9559f689
AV
3194 BUG_ON(!ns);
3195
3196 if (likely(!(flags & CLONE_NEWNS))) {
3197 get_mnt_ns(ns);
3198 return ns;
3199 }
3200
3201 old = ns->root;
3202
74e83122 3203 new_ns = alloc_mnt_ns(user_ns, false);
cf8d2c11
TM
3204 if (IS_ERR(new_ns))
3205 return new_ns;
1da177e4 3206
97216be0 3207 namespace_lock();
1da177e4 3208 /* First pass: copy the tree topology */
4ce5d2b1 3209 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
9559f689 3210 if (user_ns != ns->user_ns)
3bd045cc 3211 copy_flags |= CL_SHARED_TO_SLAVE;
7a472ef4 3212 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
be34d1a3 3213 if (IS_ERR(new)) {
328e6d90 3214 namespace_unlock();
771b1371 3215 free_mnt_ns(new_ns);
be34d1a3 3216 return ERR_CAST(new);
1da177e4 3217 }
3bd045cc
AV
3218 if (user_ns != ns->user_ns) {
3219 lock_mount_hash();
3220 lock_mnt_tree(new);
3221 unlock_mount_hash();
3222 }
be08d6d2 3223 new_ns->root = new;
1a4eeaf2 3224 list_add_tail(&new_ns->list, &new->mnt_list);
1da177e4
LT
3225
3226 /*
3227 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
3228 * as belonging to new namespace. We have already acquired a private
3229 * fs_struct, so tsk->fs->lock is not needed.
3230 */
909b0a88 3231 p = old;
cb338d06 3232 q = new;
1da177e4 3233 while (p) {
143c8c91 3234 q->mnt_ns = new_ns;
d2921684 3235 new_ns->mounts++;
9559f689
AV
3236 if (new_fs) {
3237 if (&p->mnt == new_fs->root.mnt) {
3238 new_fs->root.mnt = mntget(&q->mnt);
315fc83e 3239 rootmnt = &p->mnt;
1da177e4 3240 }
9559f689
AV
3241 if (&p->mnt == new_fs->pwd.mnt) {
3242 new_fs->pwd.mnt = mntget(&q->mnt);
315fc83e 3243 pwdmnt = &p->mnt;
1da177e4 3244 }
1da177e4 3245 }
909b0a88
AV
3246 p = next_mnt(p, old);
3247 q = next_mnt(q, new);
4ce5d2b1
EB
3248 if (!q)
3249 break;
3250 while (p->mnt.mnt_root != q->mnt.mnt_root)
3251 p = next_mnt(p, old);
1da177e4 3252 }
328e6d90 3253 namespace_unlock();
1da177e4 3254
1da177e4 3255 if (rootmnt)
f03c6599 3256 mntput(rootmnt);
1da177e4 3257 if (pwdmnt)
f03c6599 3258 mntput(pwdmnt);
1da177e4 3259
741a2951 3260 return new_ns;
1da177e4
LT
3261}
3262
74e83122 3263struct dentry *mount_subtree(struct vfsmount *m, const char *name)
ea441d11 3264{
74e83122 3265 struct mount *mnt = real_mount(m);
ea441d11 3266 struct mnt_namespace *ns;
d31da0f0 3267 struct super_block *s;
ea441d11
AV
3268 struct path path;
3269 int err;
3270
74e83122
AV
3271 ns = alloc_mnt_ns(&init_user_ns, true);
3272 if (IS_ERR(ns)) {
3273 mntput(m);
ea441d11 3274 return ERR_CAST(ns);
74e83122
AV
3275 }
3276 mnt->mnt_ns = ns;
3277 ns->root = mnt;
3278 ns->mounts++;
3279 list_add(&mnt->mnt_list, &ns->list);
ea441d11 3280
74e83122 3281 err = vfs_path_lookup(m->mnt_root, m,
ea441d11
AV
3282 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3283
3284 put_mnt_ns(ns);
3285
3286 if (err)
3287 return ERR_PTR(err);
3288
3289 /* trade a vfsmount reference for active sb one */
d31da0f0
AV
3290 s = path.mnt->mnt_sb;
3291 atomic_inc(&s->s_active);
ea441d11
AV
3292 mntput(path.mnt);
3293 /* lock the sucker */
d31da0f0 3294 down_write(&s->s_umount);
ea441d11
AV
3295 /* ... and return the root of (sub)tree on it */
3296 return path.dentry;
3297}
3298EXPORT_SYMBOL(mount_subtree);
3299
312db1aa
DB
3300int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
3301 unsigned long flags, void __user *data)
1da177e4 3302{
eca6f534
VN
3303 int ret;
3304 char *kernel_type;
eca6f534 3305 char *kernel_dev;
b40ef869 3306 void *options;
1da177e4 3307
b8850d1f
TG
3308 kernel_type = copy_mount_string(type);
3309 ret = PTR_ERR(kernel_type);
3310 if (IS_ERR(kernel_type))
eca6f534 3311 goto out_type;
1da177e4 3312
b8850d1f
TG
3313 kernel_dev = copy_mount_string(dev_name);
3314 ret = PTR_ERR(kernel_dev);
3315 if (IS_ERR(kernel_dev))
eca6f534 3316 goto out_dev;
1da177e4 3317
b40ef869
AV
3318 options = copy_mount_options(data);
3319 ret = PTR_ERR(options);
3320 if (IS_ERR(options))
eca6f534 3321 goto out_data;
1da177e4 3322
b40ef869 3323 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
1da177e4 3324
b40ef869 3325 kfree(options);
eca6f534
VN
3326out_data:
3327 kfree(kernel_dev);
3328out_dev:
eca6f534
VN
3329 kfree(kernel_type);
3330out_type:
3331 return ret;
1da177e4
LT
3332}
3333
312db1aa
DB
3334SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3335 char __user *, type, unsigned long, flags, void __user *, data)
3336{
3337 return ksys_mount(dev_name, dir_name, type, flags, data);
3338}
3339
2db154b3 3340/*
93766fbd
DH
3341 * Create a kernel mount representation for a new, prepared superblock
3342 * (specified by fs_fd) and attach to an open_tree-like file descriptor.
3343 */
3344SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
3345 unsigned int, attr_flags)
3346{
3347 struct mnt_namespace *ns;
3348 struct fs_context *fc;
3349 struct file *file;
3350 struct path newmount;
3351 struct mount *mnt;
3352 struct fd f;
3353 unsigned int mnt_flags = 0;
3354 long ret;
3355
3356 if (!may_mount())
3357 return -EPERM;
3358
3359 if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
3360 return -EINVAL;
3361
3362 if (attr_flags & ~(MOUNT_ATTR_RDONLY |
3363 MOUNT_ATTR_NOSUID |
3364 MOUNT_ATTR_NODEV |
3365 MOUNT_ATTR_NOEXEC |
3366 MOUNT_ATTR__ATIME |
3367 MOUNT_ATTR_NODIRATIME))
3368 return -EINVAL;
3369
3370 if (attr_flags & MOUNT_ATTR_RDONLY)
3371 mnt_flags |= MNT_READONLY;
3372 if (attr_flags & MOUNT_ATTR_NOSUID)
3373 mnt_flags |= MNT_NOSUID;
3374 if (attr_flags & MOUNT_ATTR_NODEV)
3375 mnt_flags |= MNT_NODEV;
3376 if (attr_flags & MOUNT_ATTR_NOEXEC)
3377 mnt_flags |= MNT_NOEXEC;
3378 if (attr_flags & MOUNT_ATTR_NODIRATIME)
3379 mnt_flags |= MNT_NODIRATIME;
3380
3381 switch (attr_flags & MOUNT_ATTR__ATIME) {
3382 case MOUNT_ATTR_STRICTATIME:
3383 break;
3384 case MOUNT_ATTR_NOATIME:
3385 mnt_flags |= MNT_NOATIME;
3386 break;
3387 case MOUNT_ATTR_RELATIME:
3388 mnt_flags |= MNT_RELATIME;
3389 break;
3390 default:
3391 return -EINVAL;
3392 }
3393
3394 f = fdget(fs_fd);
3395 if (!f.file)
3396 return -EBADF;
3397
3398 ret = -EINVAL;
3399 if (f.file->f_op != &fscontext_fops)
3400 goto err_fsfd;
3401
3402 fc = f.file->private_data;
3403
3404 ret = mutex_lock_interruptible(&fc->uapi_mutex);
3405 if (ret < 0)
3406 goto err_fsfd;
3407
3408 /* There must be a valid superblock or we can't mount it */
3409 ret = -EINVAL;
3410 if (!fc->root)
3411 goto err_unlock;
3412
3413 ret = -EPERM;
3414 if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
3415 pr_warn("VFS: Mount too revealing\n");
3416 goto err_unlock;
3417 }
3418
3419 ret = -EBUSY;
3420 if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
3421 goto err_unlock;
3422
3423 ret = -EPERM;
3424 if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock())
3425 goto err_unlock;
3426
3427 newmount.mnt = vfs_create_mount(fc);
3428 if (IS_ERR(newmount.mnt)) {
3429 ret = PTR_ERR(newmount.mnt);
3430 goto err_unlock;
3431 }
3432 newmount.dentry = dget(fc->root);
3433 newmount.mnt->mnt_flags = mnt_flags;
3434
3435 /* We've done the mount bit - now move the file context into more or
3436 * less the same state as if we'd done an fspick(). We don't want to
3437 * do any memory allocation or anything like that at this point as we
3438 * don't want to have to handle any errors incurred.
3439 */
3440 vfs_clean_context(fc);
3441
3442 ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
3443 if (IS_ERR(ns)) {
3444 ret = PTR_ERR(ns);
3445 goto err_path;
3446 }
3447 mnt = real_mount(newmount.mnt);
3448 mnt->mnt_ns = ns;
3449 ns->root = mnt;
3450 ns->mounts = 1;
3451 list_add(&mnt->mnt_list, &ns->list);
1b0b9cc8 3452 mntget(newmount.mnt);
93766fbd
DH
3453
3454 /* Attach to an apparent O_PATH fd with a note that we need to unmount
3455 * it, not just simply put it.
3456 */
3457 file = dentry_open(&newmount, O_PATH, fc->cred);
3458 if (IS_ERR(file)) {
3459 dissolve_on_fput(newmount.mnt);
3460 ret = PTR_ERR(file);
3461 goto err_path;
3462 }
3463 file->f_mode |= FMODE_NEED_UNMOUNT;
3464
3465 ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
3466 if (ret >= 0)
3467 fd_install(ret, file);
3468 else
3469 fput(file);
3470
3471err_path:
3472 path_put(&newmount);
3473err_unlock:
3474 mutex_unlock(&fc->uapi_mutex);
3475err_fsfd:
3476 fdput(f);
3477 return ret;
3478}
3479
3480/*
3481 * Move a mount from one place to another. In combination with
3482 * fsopen()/fsmount() this is used to install a new mount and in combination
3483 * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
3484 * a mount subtree.
2db154b3
DH
3485 *
3486 * Note the flags value is a combination of MOVE_MOUNT_* flags.
3487 */
3488SYSCALL_DEFINE5(move_mount,
3489 int, from_dfd, const char *, from_pathname,
3490 int, to_dfd, const char *, to_pathname,
3491 unsigned int, flags)
3492{
3493 struct path from_path, to_path;
3494 unsigned int lflags;
3495 int ret = 0;
3496
3497 if (!may_mount())
3498 return -EPERM;
3499
3500 if (flags & ~MOVE_MOUNT__MASK)
3501 return -EINVAL;
3502
3503 /* If someone gives a pathname, they aren't permitted to move
3504 * from an fd that requires unmount as we can't get at the flag
3505 * to clear it afterwards.
3506 */
3507 lflags = 0;
3508 if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3509 if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3510 if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3511
3512 ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
3513 if (ret < 0)
3514 return ret;
3515
3516 lflags = 0;
3517 if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3518 if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3519 if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3520
3521 ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
3522 if (ret < 0)
3523 goto out_from;
3524
3525 ret = security_move_mount(&from_path, &to_path);
3526 if (ret < 0)
3527 goto out_to;
3528
3529 ret = do_move_mount(&from_path, &to_path);
3530
3531out_to:
3532 path_put(&to_path);
3533out_from:
3534 path_put(&from_path);
3535 return ret;
3536}
3537
afac7cba
AV
3538/*
3539 * Return true if path is reachable from root
3540 *
48a066e7 3541 * namespace_sem or mount_lock is held
afac7cba 3542 */
643822b4 3543bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
afac7cba
AV
3544 const struct path *root)
3545{
643822b4 3546 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
a73324da 3547 dentry = mnt->mnt_mountpoint;
0714a533 3548 mnt = mnt->mnt_parent;
afac7cba 3549 }
643822b4 3550 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
afac7cba
AV
3551}
3552
640eb7e7 3553bool path_is_under(const struct path *path1, const struct path *path2)
afac7cba 3554{
25ab4c9b 3555 bool res;
48a066e7 3556 read_seqlock_excl(&mount_lock);
643822b4 3557 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
48a066e7 3558 read_sequnlock_excl(&mount_lock);
afac7cba
AV
3559 return res;
3560}
3561EXPORT_SYMBOL(path_is_under);
3562
1da177e4
LT
3563/*
3564 * pivot_root Semantics:
3565 * Moves the root file system of the current process to the directory put_old,
3566 * makes new_root as the new root file system of the current process, and sets
3567 * root/cwd of all processes which had them on the current root to new_root.
3568 *
3569 * Restrictions:
3570 * The new_root and put_old must be directories, and must not be on the
3571 * same file system as the current process root. The put_old must be
3572 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3573 * pointed to by put_old must yield the same directory as new_root. No other
3574 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3575 *
4a0d11fa
NB
3576 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
3577 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
3578 * in this situation.
3579 *
1da177e4
LT
3580 * Notes:
3581 * - we don't move root/cwd if they are not at the root (reason: if something
3582 * cared enough to change them, it's probably wrong to force them elsewhere)
3583 * - it's okay to pick a root that isn't the root of a file system, e.g.
3584 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3585 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3586 * first.
3587 */
3480b257
HC
3588SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3589 const char __user *, put_old)
1da177e4 3590{
2d8f3038 3591 struct path new, old, parent_path, root_parent, root;
84d17192
AV
3592 struct mount *new_mnt, *root_mnt, *old_mnt;
3593 struct mountpoint *old_mp, *root_mp;
1da177e4
LT
3594 int error;
3595
9b40bc90 3596 if (!may_mount())
1da177e4
LT
3597 return -EPERM;
3598
2d8f3038 3599 error = user_path_dir(new_root, &new);
1da177e4
LT
3600 if (error)
3601 goto out0;
1da177e4 3602
2d8f3038 3603 error = user_path_dir(put_old, &old);
1da177e4
LT
3604 if (error)
3605 goto out1;
3606
2d8f3038 3607 error = security_sb_pivotroot(&old, &new);
b12cea91
AV
3608 if (error)
3609 goto out2;
1da177e4 3610
f7ad3c6b 3611 get_fs_root(current->fs, &root);
84d17192
AV
3612 old_mp = lock_mount(&old);
3613 error = PTR_ERR(old_mp);
3614 if (IS_ERR(old_mp))
b12cea91
AV
3615 goto out3;
3616
1da177e4 3617 error = -EINVAL;
419148da
AV
3618 new_mnt = real_mount(new.mnt);
3619 root_mnt = real_mount(root.mnt);
84d17192
AV
3620 old_mnt = real_mount(old.mnt);
3621 if (IS_MNT_SHARED(old_mnt) ||
fc7be130
AV
3622 IS_MNT_SHARED(new_mnt->mnt_parent) ||
3623 IS_MNT_SHARED(root_mnt->mnt_parent))
b12cea91 3624 goto out4;
143c8c91 3625 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
b12cea91 3626 goto out4;
5ff9d8a6
EB
3627 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3628 goto out4;
1da177e4 3629 error = -ENOENT;
f3da392e 3630 if (d_unlinked(new.dentry))
b12cea91 3631 goto out4;
1da177e4 3632 error = -EBUSY;
84d17192 3633 if (new_mnt == root_mnt || old_mnt == root_mnt)
b12cea91 3634 goto out4; /* loop, on the same file system */
1da177e4 3635 error = -EINVAL;
8c3ee42e 3636 if (root.mnt->mnt_root != root.dentry)
b12cea91 3637 goto out4; /* not a mountpoint */
676da58d 3638 if (!mnt_has_parent(root_mnt))
b12cea91 3639 goto out4; /* not attached */
84d17192 3640 root_mp = root_mnt->mnt_mp;
2d8f3038 3641 if (new.mnt->mnt_root != new.dentry)
b12cea91 3642 goto out4; /* not a mountpoint */
676da58d 3643 if (!mnt_has_parent(new_mnt))
b12cea91 3644 goto out4; /* not attached */
4ac91378 3645 /* make sure we can reach put_old from new_root */
84d17192 3646 if (!is_path_reachable(old_mnt, old.dentry, &new))
b12cea91 3647 goto out4;
0d082601
EB
3648 /* make certain new is below the root */
3649 if (!is_path_reachable(new_mnt, new.dentry, &root))
3650 goto out4;
84d17192 3651 root_mp->m_count++; /* pin it so it won't go away */
719ea2fb 3652 lock_mount_hash();
419148da
AV
3653 detach_mnt(new_mnt, &parent_path);
3654 detach_mnt(root_mnt, &root_parent);
5ff9d8a6
EB
3655 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3656 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3657 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3658 }
4ac91378 3659 /* mount old root on put_old */
84d17192 3660 attach_mnt(root_mnt, old_mnt, old_mp);
4ac91378 3661 /* mount new_root on / */
84d17192 3662 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
6b3286ed 3663 touch_mnt_namespace(current->nsproxy->mnt_ns);
4fed655c
EB
3664 /* A moved mount should not expire automatically */
3665 list_del_init(&new_mnt->mnt_expire);
3895dbf8 3666 put_mountpoint(root_mp);
719ea2fb 3667 unlock_mount_hash();
2d8f3038 3668 chroot_fs_refs(&root, &new);
1da177e4 3669 error = 0;
b12cea91 3670out4:
84d17192 3671 unlock_mount(old_mp);
b12cea91
AV
3672 if (!error) {
3673 path_put(&root_parent);
3674 path_put(&parent_path);
3675 }
3676out3:
8c3ee42e 3677 path_put(&root);
b12cea91 3678out2:
2d8f3038 3679 path_put(&old);
1da177e4 3680out1:
2d8f3038 3681 path_put(&new);
1da177e4 3682out0:
1da177e4 3683 return error;
1da177e4
LT
3684}
3685
3686static void __init init_mount_tree(void)
3687{
3688 struct vfsmount *mnt;
74e83122 3689 struct mount *m;
6b3286ed 3690 struct mnt_namespace *ns;
ac748a09 3691 struct path root;
0c55cfc4 3692 struct file_system_type *type;
1da177e4 3693
0c55cfc4
EB
3694 type = get_fs_type("rootfs");
3695 if (!type)
3696 panic("Can't find rootfs type");
3697 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
3698 put_filesystem(type);
1da177e4
LT
3699 if (IS_ERR(mnt))
3700 panic("Can't create rootfs");
b3e19d92 3701
74e83122 3702 ns = alloc_mnt_ns(&init_user_ns, false);
3b22edc5 3703 if (IS_ERR(ns))
1da177e4 3704 panic("Can't allocate initial namespace");
74e83122
AV
3705 m = real_mount(mnt);
3706 m->mnt_ns = ns;
3707 ns->root = m;
3708 ns->mounts = 1;
3709 list_add(&m->mnt_list, &ns->list);
6b3286ed
KK
3710 init_task.nsproxy->mnt_ns = ns;
3711 get_mnt_ns(ns);
3712
be08d6d2
AV
3713 root.mnt = mnt;
3714 root.dentry = mnt->mnt_root;
da362b09 3715 mnt->mnt_flags |= MNT_LOCKED;
ac748a09
JB
3716
3717 set_fs_pwd(current->fs, &root);
3718 set_fs_root(current->fs, &root);
1da177e4
LT
3719}
3720
74bf17cf 3721void __init mnt_init(void)
1da177e4 3722{
15a67dd8 3723 int err;
1da177e4 3724
7d6fec45 3725 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
20c2df83 3726 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1da177e4 3727
0818bf27 3728 mount_hashtable = alloc_large_system_hash("Mount-cache",
38129a13 3729 sizeof(struct hlist_head),
0818bf27 3730 mhash_entries, 19,
3d375d78 3731 HASH_ZERO,
0818bf27
AV
3732 &m_hash_shift, &m_hash_mask, 0, 0);
3733 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
3734 sizeof(struct hlist_head),
3735 mphash_entries, 19,
3d375d78 3736 HASH_ZERO,
0818bf27 3737 &mp_hash_shift, &mp_hash_mask, 0, 0);
1da177e4 3738
84d17192 3739 if (!mount_hashtable || !mountpoint_hashtable)
1da177e4
LT
3740 panic("Failed to allocate mount hash table\n");
3741
4b93dc9b
TH
3742 kernfs_init();
3743
15a67dd8
RD
3744 err = sysfs_init();
3745 if (err)
3746 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
8e24eea7 3747 __func__, err);
00d26666
GKH
3748 fs_kobj = kobject_create_and_add("fs", NULL);
3749 if (!fs_kobj)
8e24eea7 3750 printk(KERN_WARNING "%s: kobj create error\n", __func__);
1da177e4
LT
3751 init_rootfs();
3752 init_mount_tree();
3753}
3754
616511d0 3755void put_mnt_ns(struct mnt_namespace *ns)
1da177e4 3756{
d498b25a 3757 if (!atomic_dec_and_test(&ns->count))
616511d0 3758 return;
7b00ed6f 3759 drop_collected_mounts(&ns->root->mnt);
771b1371 3760 free_mnt_ns(ns);
1da177e4 3761}
9d412a43 3762
d911b458 3763struct vfsmount *kern_mount(struct file_system_type *type)
9d412a43 3764{
423e0ab0 3765 struct vfsmount *mnt;
d911b458 3766 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
423e0ab0
TC
3767 if (!IS_ERR(mnt)) {
3768 /*
3769 * it is a longterm mount, don't release mnt until
3770 * we unmount before file sys is unregistered
3771 */
f7a99c5b 3772 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
423e0ab0
TC
3773 }
3774 return mnt;
9d412a43 3775}
d911b458 3776EXPORT_SYMBOL_GPL(kern_mount);
423e0ab0
TC
3777
3778void kern_unmount(struct vfsmount *mnt)
3779{
3780 /* release long term mount so mount point can be released */
3781 if (!IS_ERR_OR_NULL(mnt)) {
f7a99c5b 3782 real_mount(mnt)->mnt_ns = NULL;
48a066e7 3783 synchronize_rcu(); /* yecchhh... */
423e0ab0
TC
3784 mntput(mnt);
3785 }
3786}
3787EXPORT_SYMBOL(kern_unmount);
02125a82
AV
3788
3789bool our_mnt(struct vfsmount *mnt)
3790{
143c8c91 3791 return check_mnt(real_mount(mnt));
02125a82 3792}
8823c079 3793
3151527e
EB
3794bool current_chrooted(void)
3795{
3796 /* Does the current process have a non-standard root */
3797 struct path ns_root;
3798 struct path fs_root;
3799 bool chrooted;
3800
3801 /* Find the namespace root */
3802 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3803 ns_root.dentry = ns_root.mnt->mnt_root;
3804 path_get(&ns_root);
3805 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3806 ;
3807
3808 get_fs_root(current->fs, &fs_root);
3809
3810 chrooted = !path_equal(&fs_root, &ns_root);
3811
3812 path_put(&fs_root);
3813 path_put(&ns_root);
3814
3815 return chrooted;
3816}
3817
132e4608
DH
3818static bool mnt_already_visible(struct mnt_namespace *ns,
3819 const struct super_block *sb,
8654df4e 3820 int *new_mnt_flags)
87a8ebd6 3821{
8c6cf9cc 3822 int new_flags = *new_mnt_flags;
87a8ebd6 3823 struct mount *mnt;
e51db735 3824 bool visible = false;
87a8ebd6 3825
44bb4385 3826 down_read(&namespace_sem);
87a8ebd6 3827 list_for_each_entry(mnt, &ns->list, mnt_list) {
e51db735 3828 struct mount *child;
77b1a97d
EB
3829 int mnt_flags;
3830
132e4608 3831 if (mnt->mnt.mnt_sb->s_type != sb->s_type)
e51db735
EB
3832 continue;
3833
7e96c1b0
EB
3834 /* This mount is not fully visible if it's root directory
3835 * is not the root directory of the filesystem.
3836 */
3837 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
3838 continue;
3839
a1935c17 3840 /* A local view of the mount flags */
77b1a97d 3841 mnt_flags = mnt->mnt.mnt_flags;
77b1a97d 3842
695e9df0 3843 /* Don't miss readonly hidden in the superblock flags */
bc98a42c 3844 if (sb_rdonly(mnt->mnt.mnt_sb))
695e9df0
EB
3845 mnt_flags |= MNT_LOCK_READONLY;
3846
8c6cf9cc
EB
3847 /* Verify the mount flags are equal to or more permissive
3848 * than the proposed new mount.
3849 */
77b1a97d 3850 if ((mnt_flags & MNT_LOCK_READONLY) &&
8c6cf9cc
EB
3851 !(new_flags & MNT_READONLY))
3852 continue;
77b1a97d
EB
3853 if ((mnt_flags & MNT_LOCK_ATIME) &&
3854 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
8c6cf9cc
EB
3855 continue;
3856
ceeb0e5d
EB
3857 /* This mount is not fully visible if there are any
3858 * locked child mounts that cover anything except for
3859 * empty directories.
e51db735
EB
3860 */
3861 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3862 struct inode *inode = child->mnt_mountpoint->d_inode;
ceeb0e5d 3863 /* Only worry about locked mounts */
d71ed6c9 3864 if (!(child->mnt.mnt_flags & MNT_LOCKED))
ceeb0e5d 3865 continue;
7236c85e
EB
3866 /* Is the directory permanetly empty? */
3867 if (!is_empty_dir_inode(inode))
e51db735 3868 goto next;
87a8ebd6 3869 }
8c6cf9cc 3870 /* Preserve the locked attributes */
77b1a97d 3871 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
77b1a97d 3872 MNT_LOCK_ATIME);
e51db735
EB
3873 visible = true;
3874 goto found;
3875 next: ;
87a8ebd6 3876 }
e51db735 3877found:
44bb4385 3878 up_read(&namespace_sem);
e51db735 3879 return visible;
87a8ebd6
EB
3880}
3881
132e4608 3882static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
8654df4e 3883{
a1935c17 3884 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
8654df4e
EB
3885 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3886 unsigned long s_iflags;
3887
3888 if (ns->user_ns == &init_user_ns)
3889 return false;
3890
3891 /* Can this filesystem be too revealing? */
132e4608 3892 s_iflags = sb->s_iflags;
8654df4e
EB
3893 if (!(s_iflags & SB_I_USERNS_VISIBLE))
3894 return false;
3895
a1935c17
EB
3896 if ((s_iflags & required_iflags) != required_iflags) {
3897 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3898 required_iflags);
3899 return true;
3900 }
3901
132e4608 3902 return !mnt_already_visible(ns, sb, new_mnt_flags);
8654df4e
EB
3903}
3904
380cf5ba
AL
3905bool mnt_may_suid(struct vfsmount *mnt)
3906{
3907 /*
3908 * Foreign mounts (accessed via fchdir or through /proc
3909 * symlinks) are always treated as if they are nosuid. This
3910 * prevents namespaces from trusting potentially unsafe
3911 * suid/sgid bits, file caps, or security labels that originate
3912 * in other namespaces.
3913 */
3914 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3915 current_in_userns(mnt->mnt_sb->s_user_ns);
3916}
3917
64964528 3918static struct ns_common *mntns_get(struct task_struct *task)
8823c079 3919{
58be2825 3920 struct ns_common *ns = NULL;
8823c079
EB
3921 struct nsproxy *nsproxy;
3922
728dba3a
EB
3923 task_lock(task);
3924 nsproxy = task->nsproxy;
8823c079 3925 if (nsproxy) {
58be2825
AV
3926 ns = &nsproxy->mnt_ns->ns;
3927 get_mnt_ns(to_mnt_ns(ns));
8823c079 3928 }
728dba3a 3929 task_unlock(task);
8823c079
EB
3930
3931 return ns;
3932}
3933
64964528 3934static void mntns_put(struct ns_common *ns)
8823c079 3935{
58be2825 3936 put_mnt_ns(to_mnt_ns(ns));
8823c079
EB
3937}
3938
64964528 3939static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
8823c079
EB
3940{
3941 struct fs_struct *fs = current->fs;
4f757f3c 3942 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
8823c079 3943 struct path root;
4f757f3c 3944 int err;
8823c079 3945
0c55cfc4 3946 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
c7b96acf
EB
3947 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3948 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
ae11e0f1 3949 return -EPERM;
8823c079 3950
74e83122
AV
3951 if (is_anon_ns(mnt_ns))
3952 return -EINVAL;
3953
8823c079
EB
3954 if (fs->users != 1)
3955 return -EINVAL;
3956
3957 get_mnt_ns(mnt_ns);
4f757f3c 3958 old_mnt_ns = nsproxy->mnt_ns;
8823c079
EB
3959 nsproxy->mnt_ns = mnt_ns;
3960
3961 /* Find the root */
4f757f3c
AV
3962 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
3963 "/", LOOKUP_DOWN, &root);
3964 if (err) {
3965 /* revert to old namespace */
3966 nsproxy->mnt_ns = old_mnt_ns;
3967 put_mnt_ns(mnt_ns);
3968 return err;
3969 }
8823c079 3970
4068367c
AV
3971 put_mnt_ns(old_mnt_ns);
3972
8823c079
EB
3973 /* Update the pwd and root */
3974 set_fs_pwd(fs, &root);
3975 set_fs_root(fs, &root);
3976
3977 path_put(&root);
3978 return 0;
3979}
3980
bcac25a5
AV
3981static struct user_namespace *mntns_owner(struct ns_common *ns)
3982{
3983 return to_mnt_ns(ns)->user_ns;
3984}
3985
8823c079
EB
3986const struct proc_ns_operations mntns_operations = {
3987 .name = "mnt",
3988 .type = CLONE_NEWNS,
3989 .get = mntns_get,
3990 .put = mntns_put,
3991 .install = mntns_install,
bcac25a5 3992 .owner = mntns_owner,
8823c079 3993};