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