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