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