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