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