arm64: reduce el2_setup branching
[linux/fpc-iii.git] / fs / namespace.c
blobcc1375eff88c75abdf14aeb22a55c3d8b802895f
1 /*
2 * linux/fs/namespace.c
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
11 #include <linux/syscalls.h>
12 #include <linux/export.h>
13 #include <linux/capability.h>
14 #include <linux/mnt_namespace.h>
15 #include <linux/user_namespace.h>
16 #include <linux/namei.h>
17 #include <linux/security.h>
18 #include <linux/cred.h>
19 #include <linux/idr.h>
20 #include <linux/init.h> /* init_rootfs */
21 #include <linux/fs_struct.h> /* get_fs_root et.al. */
22 #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
23 #include <linux/uaccess.h>
24 #include <linux/proc_ns.h>
25 #include <linux/magic.h>
26 #include <linux/bootmem.h>
27 #include <linux/task_work.h>
28 #include <linux/sched/task.h>
30 #include "pnode.h"
31 #include "internal.h"
33 /* Maximum number of mounts in a mount namespace */
34 unsigned int sysctl_mount_max __read_mostly = 100000;
36 static unsigned int m_hash_mask __read_mostly;
37 static unsigned int m_hash_shift __read_mostly;
38 static unsigned int mp_hash_mask __read_mostly;
39 static unsigned int mp_hash_shift __read_mostly;
41 static __initdata unsigned long mhash_entries;
42 static int __init set_mhash_entries(char *str)
44 if (!str)
45 return 0;
46 mhash_entries = simple_strtoul(str, &str, 0);
47 return 1;
49 __setup("mhash_entries=", set_mhash_entries);
51 static __initdata unsigned long mphash_entries;
52 static int __init set_mphash_entries(char *str)
54 if (!str)
55 return 0;
56 mphash_entries = simple_strtoul(str, &str, 0);
57 return 1;
59 __setup("mphash_entries=", set_mphash_entries);
61 static u64 event;
62 static DEFINE_IDA(mnt_id_ida);
63 static DEFINE_IDA(mnt_group_ida);
64 static DEFINE_SPINLOCK(mnt_id_lock);
65 static int mnt_id_start = 0;
66 static int mnt_group_start = 1;
68 static struct hlist_head *mount_hashtable __read_mostly;
69 static struct hlist_head *mountpoint_hashtable __read_mostly;
70 static struct kmem_cache *mnt_cache __read_mostly;
71 static DECLARE_RWSEM(namespace_sem);
73 /* /sys/fs */
74 struct kobject *fs_kobj;
75 EXPORT_SYMBOL_GPL(fs_kobj);
78 * vfsmount lock may be taken for read to prevent changes to the
79 * vfsmount hash, ie. during mountpoint lookups or walking back
80 * up the tree.
82 * It should be taken for write in all cases where the vfsmount
83 * tree or hash is modified or when a vfsmount structure is modified.
85 __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
87 static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
89 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
90 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
91 tmp = tmp + (tmp >> m_hash_shift);
92 return &mount_hashtable[tmp & m_hash_mask];
95 static inline struct hlist_head *mp_hash(struct dentry *dentry)
97 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
98 tmp = tmp + (tmp >> mp_hash_shift);
99 return &mountpoint_hashtable[tmp & mp_hash_mask];
102 static int mnt_alloc_id(struct mount *mnt)
104 int res;
106 retry:
107 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
108 spin_lock(&mnt_id_lock);
109 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
110 if (!res)
111 mnt_id_start = mnt->mnt_id + 1;
112 spin_unlock(&mnt_id_lock);
113 if (res == -EAGAIN)
114 goto retry;
116 return res;
119 static void mnt_free_id(struct mount *mnt)
121 int id = mnt->mnt_id;
122 spin_lock(&mnt_id_lock);
123 ida_remove(&mnt_id_ida, id);
124 if (mnt_id_start > id)
125 mnt_id_start = id;
126 spin_unlock(&mnt_id_lock);
130 * Allocate a new peer group ID
132 * mnt_group_ida is protected by namespace_sem
134 static int mnt_alloc_group_id(struct mount *mnt)
136 int res;
138 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
139 return -ENOMEM;
141 res = ida_get_new_above(&mnt_group_ida,
142 mnt_group_start,
143 &mnt->mnt_group_id);
144 if (!res)
145 mnt_group_start = mnt->mnt_group_id + 1;
147 return res;
151 * Release a peer group ID
153 void mnt_release_group_id(struct mount *mnt)
155 int id = mnt->mnt_group_id;
156 ida_remove(&mnt_group_ida, id);
157 if (mnt_group_start > id)
158 mnt_group_start = id;
159 mnt->mnt_group_id = 0;
163 * vfsmount lock must be held for read
165 static inline void mnt_add_count(struct mount *mnt, int n)
167 #ifdef CONFIG_SMP
168 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
169 #else
170 preempt_disable();
171 mnt->mnt_count += n;
172 preempt_enable();
173 #endif
177 * vfsmount lock must be held for write
179 unsigned int mnt_get_count(struct mount *mnt)
181 #ifdef CONFIG_SMP
182 unsigned int count = 0;
183 int cpu;
185 for_each_possible_cpu(cpu) {
186 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
189 return count;
190 #else
191 return mnt->mnt_count;
192 #endif
195 static void drop_mountpoint(struct fs_pin *p)
197 struct mount *m = container_of(p, struct mount, mnt_umount);
198 dput(m->mnt_ex_mountpoint);
199 pin_remove(p);
200 mntput(&m->mnt);
203 static struct mount *alloc_vfsmnt(const char *name)
205 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
206 if (mnt) {
207 int err;
209 err = mnt_alloc_id(mnt);
210 if (err)
211 goto out_free_cache;
213 if (name) {
214 mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
215 if (!mnt->mnt_devname)
216 goto out_free_id;
219 #ifdef CONFIG_SMP
220 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
221 if (!mnt->mnt_pcp)
222 goto out_free_devname;
224 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
225 #else
226 mnt->mnt_count = 1;
227 mnt->mnt_writers = 0;
228 #endif
230 INIT_HLIST_NODE(&mnt->mnt_hash);
231 INIT_LIST_HEAD(&mnt->mnt_child);
232 INIT_LIST_HEAD(&mnt->mnt_mounts);
233 INIT_LIST_HEAD(&mnt->mnt_list);
234 INIT_LIST_HEAD(&mnt->mnt_expire);
235 INIT_LIST_HEAD(&mnt->mnt_share);
236 INIT_LIST_HEAD(&mnt->mnt_slave_list);
237 INIT_LIST_HEAD(&mnt->mnt_slave);
238 INIT_HLIST_NODE(&mnt->mnt_mp_list);
239 #ifdef CONFIG_FSNOTIFY
240 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
241 #endif
242 init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
244 return mnt;
246 #ifdef CONFIG_SMP
247 out_free_devname:
248 kfree_const(mnt->mnt_devname);
249 #endif
250 out_free_id:
251 mnt_free_id(mnt);
252 out_free_cache:
253 kmem_cache_free(mnt_cache, mnt);
254 return NULL;
258 * Most r/o checks on a fs are for operations that take
259 * discrete amounts of time, like a write() or unlink().
260 * We must keep track of when those operations start
261 * (for permission checks) and when they end, so that
262 * we can determine when writes are able to occur to
263 * a filesystem.
266 * __mnt_is_readonly: check whether a mount is read-only
267 * @mnt: the mount to check for its write status
269 * This shouldn't be used directly ouside of the VFS.
270 * It does not guarantee that the filesystem will stay
271 * r/w, just that it is right *now*. This can not and
272 * should not be used in place of IS_RDONLY(inode).
273 * mnt_want/drop_write() will _keep_ the filesystem
274 * r/w.
276 int __mnt_is_readonly(struct vfsmount *mnt)
278 if (mnt->mnt_flags & MNT_READONLY)
279 return 1;
280 if (mnt->mnt_sb->s_flags & MS_RDONLY)
281 return 1;
282 return 0;
284 EXPORT_SYMBOL_GPL(__mnt_is_readonly);
286 static inline void mnt_inc_writers(struct mount *mnt)
288 #ifdef CONFIG_SMP
289 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
290 #else
291 mnt->mnt_writers++;
292 #endif
295 static inline void mnt_dec_writers(struct mount *mnt)
297 #ifdef CONFIG_SMP
298 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
299 #else
300 mnt->mnt_writers--;
301 #endif
304 static unsigned int mnt_get_writers(struct mount *mnt)
306 #ifdef CONFIG_SMP
307 unsigned int count = 0;
308 int cpu;
310 for_each_possible_cpu(cpu) {
311 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
314 return count;
315 #else
316 return mnt->mnt_writers;
317 #endif
320 static int mnt_is_readonly(struct vfsmount *mnt)
322 if (mnt->mnt_sb->s_readonly_remount)
323 return 1;
324 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
325 smp_rmb();
326 return __mnt_is_readonly(mnt);
330 * Most r/o & frozen checks on a fs are for operations that take discrete
331 * amounts of time, like a write() or unlink(). We must keep track of when
332 * those operations start (for permission checks) and when they end, so that we
333 * can determine when writes are able to occur to a filesystem.
336 * __mnt_want_write - get write access to a mount without freeze protection
337 * @m: the mount on which to take a write
339 * This tells the low-level filesystem that a write is about to be performed to
340 * it, and makes sure that writes are allowed (mnt it read-write) before
341 * returning success. This operation does not protect against filesystem being
342 * frozen. When the write operation is finished, __mnt_drop_write() must be
343 * called. This is effectively a refcount.
345 int __mnt_want_write(struct vfsmount *m)
347 struct mount *mnt = real_mount(m);
348 int ret = 0;
350 preempt_disable();
351 mnt_inc_writers(mnt);
353 * The store to mnt_inc_writers must be visible before we pass
354 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
355 * incremented count after it has set MNT_WRITE_HOLD.
357 smp_mb();
358 while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
359 cpu_relax();
361 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
362 * be set to match its requirements. So we must not load that until
363 * MNT_WRITE_HOLD is cleared.
365 smp_rmb();
366 if (mnt_is_readonly(m)) {
367 mnt_dec_writers(mnt);
368 ret = -EROFS;
370 preempt_enable();
372 return ret;
376 * mnt_want_write - get write access to a mount
377 * @m: the mount on which to take a write
379 * This tells the low-level filesystem that a write is about to be performed to
380 * it, and makes sure that writes are allowed (mount is read-write, filesystem
381 * is not frozen) before returning success. When the write operation is
382 * finished, mnt_drop_write() must be called. This is effectively a refcount.
384 int mnt_want_write(struct vfsmount *m)
386 int ret;
388 sb_start_write(m->mnt_sb);
389 ret = __mnt_want_write(m);
390 if (ret)
391 sb_end_write(m->mnt_sb);
392 return ret;
394 EXPORT_SYMBOL_GPL(mnt_want_write);
397 * mnt_clone_write - get write access to a mount
398 * @mnt: the mount on which to take a write
400 * This is effectively like mnt_want_write, except
401 * it must only be used to take an extra write reference
402 * on a mountpoint that we already know has a write reference
403 * on it. This allows some optimisation.
405 * After finished, mnt_drop_write must be called as usual to
406 * drop the reference.
408 int mnt_clone_write(struct vfsmount *mnt)
410 /* superblock may be r/o */
411 if (__mnt_is_readonly(mnt))
412 return -EROFS;
413 preempt_disable();
414 mnt_inc_writers(real_mount(mnt));
415 preempt_enable();
416 return 0;
418 EXPORT_SYMBOL_GPL(mnt_clone_write);
421 * __mnt_want_write_file - get write access to a file's mount
422 * @file: the file who's mount on which to take a write
424 * This is like __mnt_want_write, but it takes a file and can
425 * do some optimisations if the file is open for write already
427 int __mnt_want_write_file(struct file *file)
429 if (!(file->f_mode & FMODE_WRITER))
430 return __mnt_want_write(file->f_path.mnt);
431 else
432 return mnt_clone_write(file->f_path.mnt);
436 * mnt_want_write_file - get write access to a file's mount
437 * @file: the file who's mount on which to take a write
439 * This is like mnt_want_write, but it takes a file and can
440 * do some optimisations if the file is open for write already
442 int mnt_want_write_file(struct file *file)
444 int ret;
446 sb_start_write(file->f_path.mnt->mnt_sb);
447 ret = __mnt_want_write_file(file);
448 if (ret)
449 sb_end_write(file->f_path.mnt->mnt_sb);
450 return ret;
452 EXPORT_SYMBOL_GPL(mnt_want_write_file);
455 * __mnt_drop_write - give up write access to a mount
456 * @mnt: the mount on which to give up write access
458 * Tells the low-level filesystem that we are done
459 * performing writes to it. Must be matched with
460 * __mnt_want_write() call above.
462 void __mnt_drop_write(struct vfsmount *mnt)
464 preempt_disable();
465 mnt_dec_writers(real_mount(mnt));
466 preempt_enable();
470 * mnt_drop_write - give up write access to a mount
471 * @mnt: the mount on which to give up write access
473 * Tells the low-level filesystem that we are done performing writes to it and
474 * also allows filesystem to be frozen again. Must be matched with
475 * mnt_want_write() call above.
477 void mnt_drop_write(struct vfsmount *mnt)
479 __mnt_drop_write(mnt);
480 sb_end_write(mnt->mnt_sb);
482 EXPORT_SYMBOL_GPL(mnt_drop_write);
484 void __mnt_drop_write_file(struct file *file)
486 __mnt_drop_write(file->f_path.mnt);
489 void mnt_drop_write_file(struct file *file)
491 mnt_drop_write(file->f_path.mnt);
493 EXPORT_SYMBOL(mnt_drop_write_file);
495 static int mnt_make_readonly(struct mount *mnt)
497 int ret = 0;
499 lock_mount_hash();
500 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
502 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
503 * should be visible before we do.
505 smp_mb();
508 * With writers on hold, if this value is zero, then there are
509 * definitely no active writers (although held writers may subsequently
510 * increment the count, they'll have to wait, and decrement it after
511 * seeing MNT_READONLY).
513 * It is OK to have counter incremented on one CPU and decremented on
514 * another: the sum will add up correctly. The danger would be when we
515 * sum up each counter, if we read a counter before it is incremented,
516 * but then read another CPU's count which it has been subsequently
517 * decremented from -- we would see more decrements than we should.
518 * MNT_WRITE_HOLD protects against this scenario, because
519 * mnt_want_write first increments count, then smp_mb, then spins on
520 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
521 * we're counting up here.
523 if (mnt_get_writers(mnt) > 0)
524 ret = -EBUSY;
525 else
526 mnt->mnt.mnt_flags |= MNT_READONLY;
528 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
529 * that become unheld will see MNT_READONLY.
531 smp_wmb();
532 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
533 unlock_mount_hash();
534 return ret;
537 static void __mnt_unmake_readonly(struct mount *mnt)
539 lock_mount_hash();
540 mnt->mnt.mnt_flags &= ~MNT_READONLY;
541 unlock_mount_hash();
544 int sb_prepare_remount_readonly(struct super_block *sb)
546 struct mount *mnt;
547 int err = 0;
549 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
550 if (atomic_long_read(&sb->s_remove_count))
551 return -EBUSY;
553 lock_mount_hash();
554 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
555 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
556 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
557 smp_mb();
558 if (mnt_get_writers(mnt) > 0) {
559 err = -EBUSY;
560 break;
564 if (!err && atomic_long_read(&sb->s_remove_count))
565 err = -EBUSY;
567 if (!err) {
568 sb->s_readonly_remount = 1;
569 smp_wmb();
571 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
572 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
573 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
575 unlock_mount_hash();
577 return err;
580 static void free_vfsmnt(struct mount *mnt)
582 kfree_const(mnt->mnt_devname);
583 #ifdef CONFIG_SMP
584 free_percpu(mnt->mnt_pcp);
585 #endif
586 kmem_cache_free(mnt_cache, mnt);
589 static void delayed_free_vfsmnt(struct rcu_head *head)
591 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
594 /* call under rcu_read_lock */
595 int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
597 struct mount *mnt;
598 if (read_seqretry(&mount_lock, seq))
599 return 1;
600 if (bastard == NULL)
601 return 0;
602 mnt = real_mount(bastard);
603 mnt_add_count(mnt, 1);
604 if (likely(!read_seqretry(&mount_lock, seq)))
605 return 0;
606 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
607 mnt_add_count(mnt, -1);
608 return 1;
610 return -1;
613 /* call under rcu_read_lock */
614 bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
616 int res = __legitimize_mnt(bastard, seq);
617 if (likely(!res))
618 return true;
619 if (unlikely(res < 0)) {
620 rcu_read_unlock();
621 mntput(bastard);
622 rcu_read_lock();
624 return false;
628 * find the first mount at @dentry on vfsmount @mnt.
629 * call under rcu_read_lock()
631 struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
633 struct hlist_head *head = m_hash(mnt, dentry);
634 struct mount *p;
636 hlist_for_each_entry_rcu(p, head, mnt_hash)
637 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
638 return p;
639 return NULL;
643 * lookup_mnt - Return the first child mount mounted at path
645 * "First" means first mounted chronologically. If you create the
646 * following mounts:
648 * mount /dev/sda1 /mnt
649 * mount /dev/sda2 /mnt
650 * mount /dev/sda3 /mnt
652 * Then lookup_mnt() on the base /mnt dentry in the root mount will
653 * return successively the root dentry and vfsmount of /dev/sda1, then
654 * /dev/sda2, then /dev/sda3, then NULL.
656 * lookup_mnt takes a reference to the found vfsmount.
658 struct vfsmount *lookup_mnt(const struct path *path)
660 struct mount *child_mnt;
661 struct vfsmount *m;
662 unsigned seq;
664 rcu_read_lock();
665 do {
666 seq = read_seqbegin(&mount_lock);
667 child_mnt = __lookup_mnt(path->mnt, path->dentry);
668 m = child_mnt ? &child_mnt->mnt : NULL;
669 } while (!legitimize_mnt(m, seq));
670 rcu_read_unlock();
671 return m;
675 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
676 * current mount namespace.
678 * The common case is dentries are not mountpoints at all and that
679 * test is handled inline. For the slow case when we are actually
680 * dealing with a mountpoint of some kind, walk through all of the
681 * mounts in the current mount namespace and test to see if the dentry
682 * is a mountpoint.
684 * The mount_hashtable is not usable in the context because we
685 * need to identify all mounts that may be in the current mount
686 * namespace not just a mount that happens to have some specified
687 * parent mount.
689 bool __is_local_mountpoint(struct dentry *dentry)
691 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
692 struct mount *mnt;
693 bool is_covered = false;
695 if (!d_mountpoint(dentry))
696 goto out;
698 down_read(&namespace_sem);
699 list_for_each_entry(mnt, &ns->list, mnt_list) {
700 is_covered = (mnt->mnt_mountpoint == dentry);
701 if (is_covered)
702 break;
704 up_read(&namespace_sem);
705 out:
706 return is_covered;
709 static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
711 struct hlist_head *chain = mp_hash(dentry);
712 struct mountpoint *mp;
714 hlist_for_each_entry(mp, chain, m_hash) {
715 if (mp->m_dentry == dentry) {
716 /* might be worth a WARN_ON() */
717 if (d_unlinked(dentry))
718 return ERR_PTR(-ENOENT);
719 mp->m_count++;
720 return mp;
723 return NULL;
726 static struct mountpoint *get_mountpoint(struct dentry *dentry)
728 struct mountpoint *mp, *new = NULL;
729 int ret;
731 if (d_mountpoint(dentry)) {
732 mountpoint:
733 read_seqlock_excl(&mount_lock);
734 mp = lookup_mountpoint(dentry);
735 read_sequnlock_excl(&mount_lock);
736 if (mp)
737 goto done;
740 if (!new)
741 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
742 if (!new)
743 return ERR_PTR(-ENOMEM);
746 /* Exactly one processes may set d_mounted */
747 ret = d_set_mounted(dentry);
749 /* Someone else set d_mounted? */
750 if (ret == -EBUSY)
751 goto mountpoint;
753 /* The dentry is not available as a mountpoint? */
754 mp = ERR_PTR(ret);
755 if (ret)
756 goto done;
758 /* Add the new mountpoint to the hash table */
759 read_seqlock_excl(&mount_lock);
760 new->m_dentry = dentry;
761 new->m_count = 1;
762 hlist_add_head(&new->m_hash, mp_hash(dentry));
763 INIT_HLIST_HEAD(&new->m_list);
764 read_sequnlock_excl(&mount_lock);
766 mp = new;
767 new = NULL;
768 done:
769 kfree(new);
770 return mp;
773 static void put_mountpoint(struct mountpoint *mp)
775 if (!--mp->m_count) {
776 struct dentry *dentry = mp->m_dentry;
777 BUG_ON(!hlist_empty(&mp->m_list));
778 spin_lock(&dentry->d_lock);
779 dentry->d_flags &= ~DCACHE_MOUNTED;
780 spin_unlock(&dentry->d_lock);
781 hlist_del(&mp->m_hash);
782 kfree(mp);
786 static inline int check_mnt(struct mount *mnt)
788 return mnt->mnt_ns == current->nsproxy->mnt_ns;
792 * vfsmount lock must be held for write
794 static void touch_mnt_namespace(struct mnt_namespace *ns)
796 if (ns) {
797 ns->event = ++event;
798 wake_up_interruptible(&ns->poll);
803 * vfsmount lock must be held for write
805 static void __touch_mnt_namespace(struct mnt_namespace *ns)
807 if (ns && ns->event != event) {
808 ns->event = event;
809 wake_up_interruptible(&ns->poll);
814 * vfsmount lock must be held for write
816 static void unhash_mnt(struct mount *mnt)
818 mnt->mnt_parent = mnt;
819 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
820 list_del_init(&mnt->mnt_child);
821 hlist_del_init_rcu(&mnt->mnt_hash);
822 hlist_del_init(&mnt->mnt_mp_list);
823 put_mountpoint(mnt->mnt_mp);
824 mnt->mnt_mp = NULL;
828 * vfsmount lock must be held for write
830 static void detach_mnt(struct mount *mnt, struct path *old_path)
832 old_path->dentry = mnt->mnt_mountpoint;
833 old_path->mnt = &mnt->mnt_parent->mnt;
834 unhash_mnt(mnt);
838 * vfsmount lock must be held for write
840 static void umount_mnt(struct mount *mnt)
842 /* old mountpoint will be dropped when we can do that */
843 mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
844 unhash_mnt(mnt);
848 * vfsmount lock must be held for write
850 void mnt_set_mountpoint(struct mount *mnt,
851 struct mountpoint *mp,
852 struct mount *child_mnt)
854 mp->m_count++;
855 mnt_add_count(mnt, 1); /* essentially, that's mntget */
856 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
857 child_mnt->mnt_parent = mnt;
858 child_mnt->mnt_mp = mp;
859 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
862 static void __attach_mnt(struct mount *mnt, struct mount *parent)
864 hlist_add_head_rcu(&mnt->mnt_hash,
865 m_hash(&parent->mnt, mnt->mnt_mountpoint));
866 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
870 * vfsmount lock must be held for write
872 static void attach_mnt(struct mount *mnt,
873 struct mount *parent,
874 struct mountpoint *mp)
876 mnt_set_mountpoint(parent, mp, mnt);
877 __attach_mnt(mnt, parent);
880 void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
882 struct mountpoint *old_mp = mnt->mnt_mp;
883 struct dentry *old_mountpoint = mnt->mnt_mountpoint;
884 struct mount *old_parent = mnt->mnt_parent;
886 list_del_init(&mnt->mnt_child);
887 hlist_del_init(&mnt->mnt_mp_list);
888 hlist_del_init_rcu(&mnt->mnt_hash);
890 attach_mnt(mnt, parent, mp);
892 put_mountpoint(old_mp);
895 * Safely avoid even the suggestion this code might sleep or
896 * lock the mount hash by taking advantage of the knowledge that
897 * mnt_change_mountpoint will not release the final reference
898 * to a mountpoint.
900 * During mounting, the mount passed in as the parent mount will
901 * continue to use the old mountpoint and during unmounting, the
902 * old mountpoint will continue to exist until namespace_unlock,
903 * which happens well after mnt_change_mountpoint.
905 spin_lock(&old_mountpoint->d_lock);
906 old_mountpoint->d_lockref.count--;
907 spin_unlock(&old_mountpoint->d_lock);
909 mnt_add_count(old_parent, -1);
913 * vfsmount lock must be held for write
915 static void commit_tree(struct mount *mnt)
917 struct mount *parent = mnt->mnt_parent;
918 struct mount *m;
919 LIST_HEAD(head);
920 struct mnt_namespace *n = parent->mnt_ns;
922 BUG_ON(parent == mnt);
924 list_add_tail(&head, &mnt->mnt_list);
925 list_for_each_entry(m, &head, mnt_list)
926 m->mnt_ns = n;
928 list_splice(&head, n->list.prev);
930 n->mounts += n->pending_mounts;
931 n->pending_mounts = 0;
933 __attach_mnt(mnt, parent);
934 touch_mnt_namespace(n);
937 static struct mount *next_mnt(struct mount *p, struct mount *root)
939 struct list_head *next = p->mnt_mounts.next;
940 if (next == &p->mnt_mounts) {
941 while (1) {
942 if (p == root)
943 return NULL;
944 next = p->mnt_child.next;
945 if (next != &p->mnt_parent->mnt_mounts)
946 break;
947 p = p->mnt_parent;
950 return list_entry(next, struct mount, mnt_child);
953 static struct mount *skip_mnt_tree(struct mount *p)
955 struct list_head *prev = p->mnt_mounts.prev;
956 while (prev != &p->mnt_mounts) {
957 p = list_entry(prev, struct mount, mnt_child);
958 prev = p->mnt_mounts.prev;
960 return p;
963 struct vfsmount *
964 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
966 struct mount *mnt;
967 struct dentry *root;
969 if (!type)
970 return ERR_PTR(-ENODEV);
972 mnt = alloc_vfsmnt(name);
973 if (!mnt)
974 return ERR_PTR(-ENOMEM);
976 if (flags & MS_KERNMOUNT)
977 mnt->mnt.mnt_flags = MNT_INTERNAL;
979 root = mount_fs(type, flags, name, data);
980 if (IS_ERR(root)) {
981 mnt_free_id(mnt);
982 free_vfsmnt(mnt);
983 return ERR_CAST(root);
986 mnt->mnt.mnt_root = root;
987 mnt->mnt.mnt_sb = root->d_sb;
988 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
989 mnt->mnt_parent = mnt;
990 lock_mount_hash();
991 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
992 unlock_mount_hash();
993 return &mnt->mnt;
995 EXPORT_SYMBOL_GPL(vfs_kern_mount);
997 struct vfsmount *
998 vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
999 const char *name, void *data)
1001 /* Until it is worked out how to pass the user namespace
1002 * through from the parent mount to the submount don't support
1003 * unprivileged mounts with submounts.
1005 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1006 return ERR_PTR(-EPERM);
1008 return vfs_kern_mount(type, MS_SUBMOUNT, name, data);
1010 EXPORT_SYMBOL_GPL(vfs_submount);
1012 static struct mount *clone_mnt(struct mount *old, struct dentry *root,
1013 int flag)
1015 struct super_block *sb = old->mnt.mnt_sb;
1016 struct mount *mnt;
1017 int err;
1019 mnt = alloc_vfsmnt(old->mnt_devname);
1020 if (!mnt)
1021 return ERR_PTR(-ENOMEM);
1023 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
1024 mnt->mnt_group_id = 0; /* not a peer of original */
1025 else
1026 mnt->mnt_group_id = old->mnt_group_id;
1028 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1029 err = mnt_alloc_group_id(mnt);
1030 if (err)
1031 goto out_free;
1034 mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
1035 /* Don't allow unprivileged users to change mount flags */
1036 if (flag & CL_UNPRIVILEGED) {
1037 mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
1039 if (mnt->mnt.mnt_flags & MNT_READONLY)
1040 mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
1042 if (mnt->mnt.mnt_flags & MNT_NODEV)
1043 mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
1045 if (mnt->mnt.mnt_flags & MNT_NOSUID)
1046 mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
1048 if (mnt->mnt.mnt_flags & MNT_NOEXEC)
1049 mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
1052 /* Don't allow unprivileged users to reveal what is under a mount */
1053 if ((flag & CL_UNPRIVILEGED) &&
1054 (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
1055 mnt->mnt.mnt_flags |= MNT_LOCKED;
1057 atomic_inc(&sb->s_active);
1058 mnt->mnt.mnt_sb = sb;
1059 mnt->mnt.mnt_root = dget(root);
1060 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1061 mnt->mnt_parent = mnt;
1062 lock_mount_hash();
1063 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1064 unlock_mount_hash();
1066 if ((flag & CL_SLAVE) ||
1067 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
1068 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1069 mnt->mnt_master = old;
1070 CLEAR_MNT_SHARED(mnt);
1071 } else if (!(flag & CL_PRIVATE)) {
1072 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1073 list_add(&mnt->mnt_share, &old->mnt_share);
1074 if (IS_MNT_SLAVE(old))
1075 list_add(&mnt->mnt_slave, &old->mnt_slave);
1076 mnt->mnt_master = old->mnt_master;
1077 } else {
1078 CLEAR_MNT_SHARED(mnt);
1080 if (flag & CL_MAKE_SHARED)
1081 set_mnt_shared(mnt);
1083 /* stick the duplicate mount on the same expiry list
1084 * as the original if that was on one */
1085 if (flag & CL_EXPIRE) {
1086 if (!list_empty(&old->mnt_expire))
1087 list_add(&mnt->mnt_expire, &old->mnt_expire);
1090 return mnt;
1092 out_free:
1093 mnt_free_id(mnt);
1094 free_vfsmnt(mnt);
1095 return ERR_PTR(err);
1098 static void cleanup_mnt(struct mount *mnt)
1101 * This probably indicates that somebody messed
1102 * up a mnt_want/drop_write() pair. If this
1103 * happens, the filesystem was probably unable
1104 * to make r/w->r/o transitions.
1107 * The locking used to deal with mnt_count decrement provides barriers,
1108 * so mnt_get_writers() below is safe.
1110 WARN_ON(mnt_get_writers(mnt));
1111 if (unlikely(mnt->mnt_pins.first))
1112 mnt_pin_kill(mnt);
1113 fsnotify_vfsmount_delete(&mnt->mnt);
1114 dput(mnt->mnt.mnt_root);
1115 deactivate_super(mnt->mnt.mnt_sb);
1116 mnt_free_id(mnt);
1117 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1120 static void __cleanup_mnt(struct rcu_head *head)
1122 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1125 static LLIST_HEAD(delayed_mntput_list);
1126 static void delayed_mntput(struct work_struct *unused)
1128 struct llist_node *node = llist_del_all(&delayed_mntput_list);
1129 struct llist_node *next;
1131 for (; node; node = next) {
1132 next = llist_next(node);
1133 cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
1136 static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1138 static void mntput_no_expire(struct mount *mnt)
1140 rcu_read_lock();
1141 mnt_add_count(mnt, -1);
1142 if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
1143 rcu_read_unlock();
1144 return;
1146 lock_mount_hash();
1147 if (mnt_get_count(mnt)) {
1148 rcu_read_unlock();
1149 unlock_mount_hash();
1150 return;
1152 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1153 rcu_read_unlock();
1154 unlock_mount_hash();
1155 return;
1157 mnt->mnt.mnt_flags |= MNT_DOOMED;
1158 rcu_read_unlock();
1160 list_del(&mnt->mnt_instance);
1162 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1163 struct mount *p, *tmp;
1164 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
1165 umount_mnt(p);
1168 unlock_mount_hash();
1170 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1171 struct task_struct *task = current;
1172 if (likely(!(task->flags & PF_KTHREAD))) {
1173 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1174 if (!task_work_add(task, &mnt->mnt_rcu, true))
1175 return;
1177 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1178 schedule_delayed_work(&delayed_mntput_work, 1);
1179 return;
1181 cleanup_mnt(mnt);
1184 void mntput(struct vfsmount *mnt)
1186 if (mnt) {
1187 struct mount *m = real_mount(mnt);
1188 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1189 if (unlikely(m->mnt_expiry_mark))
1190 m->mnt_expiry_mark = 0;
1191 mntput_no_expire(m);
1194 EXPORT_SYMBOL(mntput);
1196 struct vfsmount *mntget(struct vfsmount *mnt)
1198 if (mnt)
1199 mnt_add_count(real_mount(mnt), 1);
1200 return mnt;
1202 EXPORT_SYMBOL(mntget);
1204 /* path_is_mountpoint() - Check if path is a mount in the current
1205 * namespace.
1207 * d_mountpoint() can only be used reliably to establish if a dentry is
1208 * not mounted in any namespace and that common case is handled inline.
1209 * d_mountpoint() isn't aware of the possibility there may be multiple
1210 * mounts using a given dentry in a different namespace. This function
1211 * checks if the passed in path is a mountpoint rather than the dentry
1212 * alone.
1214 bool path_is_mountpoint(const struct path *path)
1216 unsigned seq;
1217 bool res;
1219 if (!d_mountpoint(path->dentry))
1220 return false;
1222 rcu_read_lock();
1223 do {
1224 seq = read_seqbegin(&mount_lock);
1225 res = __path_is_mountpoint(path);
1226 } while (read_seqretry(&mount_lock, seq));
1227 rcu_read_unlock();
1229 return res;
1231 EXPORT_SYMBOL(path_is_mountpoint);
1233 struct vfsmount *mnt_clone_internal(const struct path *path)
1235 struct mount *p;
1236 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1237 if (IS_ERR(p))
1238 return ERR_CAST(p);
1239 p->mnt.mnt_flags |= MNT_INTERNAL;
1240 return &p->mnt;
1243 static inline void mangle(struct seq_file *m, const char *s)
1245 seq_escape(m, s, " \t\n\\");
1249 * Simple .show_options callback for filesystems which don't want to
1250 * implement more complex mount option showing.
1252 * See also save_mount_options().
1254 int generic_show_options(struct seq_file *m, struct dentry *root)
1256 const char *options;
1258 rcu_read_lock();
1259 options = rcu_dereference(root->d_sb->s_options);
1261 if (options != NULL && options[0]) {
1262 seq_putc(m, ',');
1263 mangle(m, options);
1265 rcu_read_unlock();
1267 return 0;
1269 EXPORT_SYMBOL(generic_show_options);
1272 * If filesystem uses generic_show_options(), this function should be
1273 * called from the fill_super() callback.
1275 * The .remount_fs callback usually needs to be handled in a special
1276 * way, to make sure, that previous options are not overwritten if the
1277 * remount fails.
1279 * Also note, that if the filesystem's .remount_fs function doesn't
1280 * reset all options to their default value, but changes only newly
1281 * given options, then the displayed options will not reflect reality
1282 * any more.
1284 void save_mount_options(struct super_block *sb, char *options)
1286 BUG_ON(sb->s_options);
1287 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1289 EXPORT_SYMBOL(save_mount_options);
1291 void replace_mount_options(struct super_block *sb, char *options)
1293 char *old = sb->s_options;
1294 rcu_assign_pointer(sb->s_options, options);
1295 if (old) {
1296 synchronize_rcu();
1297 kfree(old);
1300 EXPORT_SYMBOL(replace_mount_options);
1302 #ifdef CONFIG_PROC_FS
1303 /* iterator; we want it to have access to namespace_sem, thus here... */
1304 static void *m_start(struct seq_file *m, loff_t *pos)
1306 struct proc_mounts *p = m->private;
1308 down_read(&namespace_sem);
1309 if (p->cached_event == p->ns->event) {
1310 void *v = p->cached_mount;
1311 if (*pos == p->cached_index)
1312 return v;
1313 if (*pos == p->cached_index + 1) {
1314 v = seq_list_next(v, &p->ns->list, &p->cached_index);
1315 return p->cached_mount = v;
1319 p->cached_event = p->ns->event;
1320 p->cached_mount = seq_list_start(&p->ns->list, *pos);
1321 p->cached_index = *pos;
1322 return p->cached_mount;
1325 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1327 struct proc_mounts *p = m->private;
1329 p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1330 p->cached_index = *pos;
1331 return p->cached_mount;
1334 static void m_stop(struct seq_file *m, void *v)
1336 up_read(&namespace_sem);
1339 static int m_show(struct seq_file *m, void *v)
1341 struct proc_mounts *p = m->private;
1342 struct mount *r = list_entry(v, struct mount, mnt_list);
1343 return p->show(m, &r->mnt);
1346 const struct seq_operations mounts_op = {
1347 .start = m_start,
1348 .next = m_next,
1349 .stop = m_stop,
1350 .show = m_show,
1352 #endif /* CONFIG_PROC_FS */
1355 * may_umount_tree - check if a mount tree is busy
1356 * @mnt: root of mount tree
1358 * This is called to check if a tree of mounts has any
1359 * open files, pwds, chroots or sub mounts that are
1360 * busy.
1362 int may_umount_tree(struct vfsmount *m)
1364 struct mount *mnt = real_mount(m);
1365 int actual_refs = 0;
1366 int minimum_refs = 0;
1367 struct mount *p;
1368 BUG_ON(!m);
1370 /* write lock needed for mnt_get_count */
1371 lock_mount_hash();
1372 for (p = mnt; p; p = next_mnt(p, mnt)) {
1373 actual_refs += mnt_get_count(p);
1374 minimum_refs += 2;
1376 unlock_mount_hash();
1378 if (actual_refs > minimum_refs)
1379 return 0;
1381 return 1;
1384 EXPORT_SYMBOL(may_umount_tree);
1387 * may_umount - check if a mount point is busy
1388 * @mnt: root of mount
1390 * This is called to check if a mount point has any
1391 * open files, pwds, chroots or sub mounts. If the
1392 * mount has sub mounts this will return busy
1393 * regardless of whether the sub mounts are busy.
1395 * Doesn't take quota and stuff into account. IOW, in some cases it will
1396 * give false negatives. The main reason why it's here is that we need
1397 * a non-destructive way to look for easily umountable filesystems.
1399 int may_umount(struct vfsmount *mnt)
1401 int ret = 1;
1402 down_read(&namespace_sem);
1403 lock_mount_hash();
1404 if (propagate_mount_busy(real_mount(mnt), 2))
1405 ret = 0;
1406 unlock_mount_hash();
1407 up_read(&namespace_sem);
1408 return ret;
1411 EXPORT_SYMBOL(may_umount);
1413 static HLIST_HEAD(unmounted); /* protected by namespace_sem */
1415 static void namespace_unlock(void)
1417 struct hlist_head head;
1419 hlist_move_list(&unmounted, &head);
1421 up_write(&namespace_sem);
1423 if (likely(hlist_empty(&head)))
1424 return;
1426 synchronize_rcu();
1428 group_pin_kill(&head);
1431 static inline void namespace_lock(void)
1433 down_write(&namespace_sem);
1436 enum umount_tree_flags {
1437 UMOUNT_SYNC = 1,
1438 UMOUNT_PROPAGATE = 2,
1439 UMOUNT_CONNECTED = 4,
1442 static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1444 /* Leaving mounts connected is only valid for lazy umounts */
1445 if (how & UMOUNT_SYNC)
1446 return true;
1448 /* A mount without a parent has nothing to be connected to */
1449 if (!mnt_has_parent(mnt))
1450 return true;
1452 /* Because the reference counting rules change when mounts are
1453 * unmounted and connected, umounted mounts may not be
1454 * connected to mounted mounts.
1456 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1457 return true;
1459 /* Has it been requested that the mount remain connected? */
1460 if (how & UMOUNT_CONNECTED)
1461 return false;
1463 /* Is the mount locked such that it needs to remain connected? */
1464 if (IS_MNT_LOCKED(mnt))
1465 return false;
1467 /* By default disconnect the mount */
1468 return true;
1472 * mount_lock must be held
1473 * namespace_sem must be held for write
1475 static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1477 LIST_HEAD(tmp_list);
1478 struct mount *p;
1480 if (how & UMOUNT_PROPAGATE)
1481 propagate_mount_unlock(mnt);
1483 /* Gather the mounts to umount */
1484 for (p = mnt; p; p = next_mnt(p, mnt)) {
1485 p->mnt.mnt_flags |= MNT_UMOUNT;
1486 list_move(&p->mnt_list, &tmp_list);
1489 /* Hide the mounts from mnt_mounts */
1490 list_for_each_entry(p, &tmp_list, mnt_list) {
1491 list_del_init(&p->mnt_child);
1494 /* Add propogated mounts to the tmp_list */
1495 if (how & UMOUNT_PROPAGATE)
1496 propagate_umount(&tmp_list);
1498 while (!list_empty(&tmp_list)) {
1499 struct mnt_namespace *ns;
1500 bool disconnect;
1501 p = list_first_entry(&tmp_list, struct mount, mnt_list);
1502 list_del_init(&p->mnt_expire);
1503 list_del_init(&p->mnt_list);
1504 ns = p->mnt_ns;
1505 if (ns) {
1506 ns->mounts--;
1507 __touch_mnt_namespace(ns);
1509 p->mnt_ns = NULL;
1510 if (how & UMOUNT_SYNC)
1511 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
1513 disconnect = disconnect_mount(p, how);
1515 pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1516 disconnect ? &unmounted : NULL);
1517 if (mnt_has_parent(p)) {
1518 mnt_add_count(p->mnt_parent, -1);
1519 if (!disconnect) {
1520 /* Don't forget about p */
1521 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1522 } else {
1523 umount_mnt(p);
1526 change_mnt_propagation(p, MS_PRIVATE);
1530 static void shrink_submounts(struct mount *mnt);
1532 static int do_umount(struct mount *mnt, int flags)
1534 struct super_block *sb = mnt->mnt.mnt_sb;
1535 int retval;
1537 retval = security_sb_umount(&mnt->mnt, flags);
1538 if (retval)
1539 return retval;
1542 * Allow userspace to request a mountpoint be expired rather than
1543 * unmounting unconditionally. Unmount only happens if:
1544 * (1) the mark is already set (the mark is cleared by mntput())
1545 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1547 if (flags & MNT_EXPIRE) {
1548 if (&mnt->mnt == current->fs->root.mnt ||
1549 flags & (MNT_FORCE | MNT_DETACH))
1550 return -EINVAL;
1553 * probably don't strictly need the lock here if we examined
1554 * all race cases, but it's a slowpath.
1556 lock_mount_hash();
1557 if (mnt_get_count(mnt) != 2) {
1558 unlock_mount_hash();
1559 return -EBUSY;
1561 unlock_mount_hash();
1563 if (!xchg(&mnt->mnt_expiry_mark, 1))
1564 return -EAGAIN;
1568 * If we may have to abort operations to get out of this
1569 * mount, and they will themselves hold resources we must
1570 * allow the fs to do things. In the Unix tradition of
1571 * 'Gee thats tricky lets do it in userspace' the umount_begin
1572 * might fail to complete on the first run through as other tasks
1573 * must return, and the like. Thats for the mount program to worry
1574 * about for the moment.
1577 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1578 sb->s_op->umount_begin(sb);
1582 * No sense to grab the lock for this test, but test itself looks
1583 * somewhat bogus. Suggestions for better replacement?
1584 * Ho-hum... In principle, we might treat that as umount + switch
1585 * to rootfs. GC would eventually take care of the old vfsmount.
1586 * Actually it makes sense, especially if rootfs would contain a
1587 * /reboot - static binary that would close all descriptors and
1588 * call reboot(9). Then init(8) could umount root and exec /reboot.
1590 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1592 * Special case for "unmounting" root ...
1593 * we just try to remount it readonly.
1595 if (!capable(CAP_SYS_ADMIN))
1596 return -EPERM;
1597 down_write(&sb->s_umount);
1598 if (!(sb->s_flags & MS_RDONLY))
1599 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
1600 up_write(&sb->s_umount);
1601 return retval;
1604 namespace_lock();
1605 lock_mount_hash();
1606 event++;
1608 if (flags & MNT_DETACH) {
1609 if (!list_empty(&mnt->mnt_list))
1610 umount_tree(mnt, UMOUNT_PROPAGATE);
1611 retval = 0;
1612 } else {
1613 shrink_submounts(mnt);
1614 retval = -EBUSY;
1615 if (!propagate_mount_busy(mnt, 2)) {
1616 if (!list_empty(&mnt->mnt_list))
1617 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
1618 retval = 0;
1621 unlock_mount_hash();
1622 namespace_unlock();
1623 return retval;
1627 * __detach_mounts - lazily unmount all mounts on the specified dentry
1629 * During unlink, rmdir, and d_drop it is possible to loose the path
1630 * to an existing mountpoint, and wind up leaking the mount.
1631 * detach_mounts allows lazily unmounting those mounts instead of
1632 * leaking them.
1634 * The caller may hold dentry->d_inode->i_mutex.
1636 void __detach_mounts(struct dentry *dentry)
1638 struct mountpoint *mp;
1639 struct mount *mnt;
1641 namespace_lock();
1642 lock_mount_hash();
1643 mp = lookup_mountpoint(dentry);
1644 if (IS_ERR_OR_NULL(mp))
1645 goto out_unlock;
1647 event++;
1648 while (!hlist_empty(&mp->m_list)) {
1649 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1650 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1651 hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1652 umount_mnt(mnt);
1654 else umount_tree(mnt, UMOUNT_CONNECTED);
1656 put_mountpoint(mp);
1657 out_unlock:
1658 unlock_mount_hash();
1659 namespace_unlock();
1663 * Is the caller allowed to modify his namespace?
1665 static inline bool may_mount(void)
1667 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1670 static inline bool may_mandlock(void)
1672 #ifndef CONFIG_MANDATORY_FILE_LOCKING
1673 return false;
1674 #endif
1675 return capable(CAP_SYS_ADMIN);
1679 * Now umount can handle mount points as well as block devices.
1680 * This is important for filesystems which use unnamed block devices.
1682 * We now support a flag for forced unmount like the other 'big iron'
1683 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1686 SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1688 struct path path;
1689 struct mount *mnt;
1690 int retval;
1691 int lookup_flags = 0;
1693 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1694 return -EINVAL;
1696 if (!may_mount())
1697 return -EPERM;
1699 if (!(flags & UMOUNT_NOFOLLOW))
1700 lookup_flags |= LOOKUP_FOLLOW;
1702 retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
1703 if (retval)
1704 goto out;
1705 mnt = real_mount(path.mnt);
1706 retval = -EINVAL;
1707 if (path.dentry != path.mnt->mnt_root)
1708 goto dput_and_out;
1709 if (!check_mnt(mnt))
1710 goto dput_and_out;
1711 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1712 goto dput_and_out;
1713 retval = -EPERM;
1714 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1715 goto dput_and_out;
1717 retval = do_umount(mnt, flags);
1718 dput_and_out:
1719 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
1720 dput(path.dentry);
1721 mntput_no_expire(mnt);
1722 out:
1723 return retval;
1726 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
1729 * The 2.0 compatible umount. No flags.
1731 SYSCALL_DEFINE1(oldumount, char __user *, name)
1733 return sys_umount(name, 0);
1736 #endif
1738 static bool is_mnt_ns_file(struct dentry *dentry)
1740 /* Is this a proxy for a mount namespace? */
1741 return dentry->d_op == &ns_dentry_operations &&
1742 dentry->d_fsdata == &mntns_operations;
1745 struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
1747 return container_of(ns, struct mnt_namespace, ns);
1750 static bool mnt_ns_loop(struct dentry *dentry)
1752 /* Could bind mounting the mount namespace inode cause a
1753 * mount namespace loop?
1755 struct mnt_namespace *mnt_ns;
1756 if (!is_mnt_ns_file(dentry))
1757 return false;
1759 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
1760 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1763 struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
1764 int flag)
1766 struct mount *res, *p, *q, *r, *parent;
1768 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1769 return ERR_PTR(-EINVAL);
1771 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1772 return ERR_PTR(-EINVAL);
1774 res = q = clone_mnt(mnt, dentry, flag);
1775 if (IS_ERR(q))
1776 return q;
1778 q->mnt_mountpoint = mnt->mnt_mountpoint;
1780 p = mnt;
1781 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1782 struct mount *s;
1783 if (!is_subdir(r->mnt_mountpoint, dentry))
1784 continue;
1786 for (s = r; s; s = next_mnt(s, r)) {
1787 if (!(flag & CL_COPY_UNBINDABLE) &&
1788 IS_MNT_UNBINDABLE(s)) {
1789 s = skip_mnt_tree(s);
1790 continue;
1792 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1793 is_mnt_ns_file(s->mnt.mnt_root)) {
1794 s = skip_mnt_tree(s);
1795 continue;
1797 while (p != s->mnt_parent) {
1798 p = p->mnt_parent;
1799 q = q->mnt_parent;
1801 p = s;
1802 parent = q;
1803 q = clone_mnt(p, p->mnt.mnt_root, flag);
1804 if (IS_ERR(q))
1805 goto out;
1806 lock_mount_hash();
1807 list_add_tail(&q->mnt_list, &res->mnt_list);
1808 attach_mnt(q, parent, p->mnt_mp);
1809 unlock_mount_hash();
1812 return res;
1813 out:
1814 if (res) {
1815 lock_mount_hash();
1816 umount_tree(res, UMOUNT_SYNC);
1817 unlock_mount_hash();
1819 return q;
1822 /* Caller should check returned pointer for errors */
1824 struct vfsmount *collect_mounts(const struct path *path)
1826 struct mount *tree;
1827 namespace_lock();
1828 if (!check_mnt(real_mount(path->mnt)))
1829 tree = ERR_PTR(-EINVAL);
1830 else
1831 tree = copy_tree(real_mount(path->mnt), path->dentry,
1832 CL_COPY_ALL | CL_PRIVATE);
1833 namespace_unlock();
1834 if (IS_ERR(tree))
1835 return ERR_CAST(tree);
1836 return &tree->mnt;
1839 void drop_collected_mounts(struct vfsmount *mnt)
1841 namespace_lock();
1842 lock_mount_hash();
1843 umount_tree(real_mount(mnt), UMOUNT_SYNC);
1844 unlock_mount_hash();
1845 namespace_unlock();
1849 * clone_private_mount - create a private clone of a path
1851 * This creates a new vfsmount, which will be the clone of @path. The new will
1852 * not be attached anywhere in the namespace and will be private (i.e. changes
1853 * to the originating mount won't be propagated into this).
1855 * Release with mntput().
1857 struct vfsmount *clone_private_mount(const struct path *path)
1859 struct mount *old_mnt = real_mount(path->mnt);
1860 struct mount *new_mnt;
1862 if (IS_MNT_UNBINDABLE(old_mnt))
1863 return ERR_PTR(-EINVAL);
1865 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1866 if (IS_ERR(new_mnt))
1867 return ERR_CAST(new_mnt);
1869 return &new_mnt->mnt;
1871 EXPORT_SYMBOL_GPL(clone_private_mount);
1873 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1874 struct vfsmount *root)
1876 struct mount *mnt;
1877 int res = f(root, arg);
1878 if (res)
1879 return res;
1880 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1881 res = f(&mnt->mnt, arg);
1882 if (res)
1883 return res;
1885 return 0;
1888 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1890 struct mount *p;
1892 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1893 if (p->mnt_group_id && !IS_MNT_SHARED(p))
1894 mnt_release_group_id(p);
1898 static int invent_group_ids(struct mount *mnt, bool recurse)
1900 struct mount *p;
1902 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1903 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1904 int err = mnt_alloc_group_id(p);
1905 if (err) {
1906 cleanup_group_ids(mnt, p);
1907 return err;
1912 return 0;
1915 int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1917 unsigned int max = READ_ONCE(sysctl_mount_max);
1918 unsigned int mounts = 0, old, pending, sum;
1919 struct mount *p;
1921 for (p = mnt; p; p = next_mnt(p, mnt))
1922 mounts++;
1924 old = ns->mounts;
1925 pending = ns->pending_mounts;
1926 sum = old + pending;
1927 if ((old > sum) ||
1928 (pending > sum) ||
1929 (max < sum) ||
1930 (mounts > (max - sum)))
1931 return -ENOSPC;
1933 ns->pending_mounts = pending + mounts;
1934 return 0;
1938 * @source_mnt : mount tree to be attached
1939 * @nd : place the mount tree @source_mnt is attached
1940 * @parent_nd : if non-null, detach the source_mnt from its parent and
1941 * store the parent mount and mountpoint dentry.
1942 * (done when source_mnt is moved)
1944 * NOTE: in the table below explains the semantics when a source mount
1945 * of a given type is attached to a destination mount of a given type.
1946 * ---------------------------------------------------------------------------
1947 * | BIND MOUNT OPERATION |
1948 * |**************************************************************************
1949 * | source-->| shared | private | slave | unbindable |
1950 * | dest | | | | |
1951 * | | | | | | |
1952 * | v | | | | |
1953 * |**************************************************************************
1954 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1955 * | | | | | |
1956 * |non-shared| shared (+) | private | slave (*) | invalid |
1957 * ***************************************************************************
1958 * A bind operation clones the source mount and mounts the clone on the
1959 * destination mount.
1961 * (++) the cloned mount is propagated to all the mounts in the propagation
1962 * tree of the destination mount and the cloned mount is added to
1963 * the peer group of the source mount.
1964 * (+) the cloned mount is created under the destination mount and is marked
1965 * as shared. The cloned mount is added to the peer group of the source
1966 * mount.
1967 * (+++) the mount is propagated to all the mounts in the propagation tree
1968 * of the destination mount and the cloned mount is made slave
1969 * of the same master as that of the source mount. The cloned mount
1970 * is marked as 'shared and slave'.
1971 * (*) the cloned mount is made a slave of the same master as that of the
1972 * source mount.
1974 * ---------------------------------------------------------------------------
1975 * | MOVE MOUNT OPERATION |
1976 * |**************************************************************************
1977 * | source-->| shared | private | slave | unbindable |
1978 * | dest | | | | |
1979 * | | | | | | |
1980 * | v | | | | |
1981 * |**************************************************************************
1982 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1983 * | | | | | |
1984 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1985 * ***************************************************************************
1987 * (+) the mount is moved to the destination. And is then propagated to
1988 * all the mounts in the propagation tree of the destination mount.
1989 * (+*) the mount is moved to the destination.
1990 * (+++) the mount is moved to the destination and is then propagated to
1991 * all the mounts belonging to the destination mount's propagation tree.
1992 * the mount is marked as 'shared and slave'.
1993 * (*) the mount continues to be a slave at the new location.
1995 * if the source mount is a tree, the operations explained above is
1996 * applied to each mount in the tree.
1997 * Must be called without spinlocks held, since this function can sleep
1998 * in allocations.
2000 static int attach_recursive_mnt(struct mount *source_mnt,
2001 struct mount *dest_mnt,
2002 struct mountpoint *dest_mp,
2003 struct path *parent_path)
2005 HLIST_HEAD(tree_list);
2006 struct mnt_namespace *ns = dest_mnt->mnt_ns;
2007 struct mountpoint *smp;
2008 struct mount *child, *p;
2009 struct hlist_node *n;
2010 int err;
2012 /* Preallocate a mountpoint in case the new mounts need
2013 * to be tucked under other mounts.
2015 smp = get_mountpoint(source_mnt->mnt.mnt_root);
2016 if (IS_ERR(smp))
2017 return PTR_ERR(smp);
2019 /* Is there space to add these mounts to the mount namespace? */
2020 if (!parent_path) {
2021 err = count_mounts(ns, source_mnt);
2022 if (err)
2023 goto out;
2026 if (IS_MNT_SHARED(dest_mnt)) {
2027 err = invent_group_ids(source_mnt, true);
2028 if (err)
2029 goto out;
2030 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2031 lock_mount_hash();
2032 if (err)
2033 goto out_cleanup_ids;
2034 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
2035 set_mnt_shared(p);
2036 } else {
2037 lock_mount_hash();
2039 if (parent_path) {
2040 detach_mnt(source_mnt, parent_path);
2041 attach_mnt(source_mnt, dest_mnt, dest_mp);
2042 touch_mnt_namespace(source_mnt->mnt_ns);
2043 } else {
2044 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
2045 commit_tree(source_mnt);
2048 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
2049 struct mount *q;
2050 hlist_del_init(&child->mnt_hash);
2051 q = __lookup_mnt(&child->mnt_parent->mnt,
2052 child->mnt_mountpoint);
2053 if (q)
2054 mnt_change_mountpoint(child, smp, q);
2055 commit_tree(child);
2057 put_mountpoint(smp);
2058 unlock_mount_hash();
2060 return 0;
2062 out_cleanup_ids:
2063 while (!hlist_empty(&tree_list)) {
2064 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2065 child->mnt_parent->mnt_ns->pending_mounts = 0;
2066 umount_tree(child, UMOUNT_SYNC);
2068 unlock_mount_hash();
2069 cleanup_group_ids(source_mnt, NULL);
2070 out:
2071 ns->pending_mounts = 0;
2073 read_seqlock_excl(&mount_lock);
2074 put_mountpoint(smp);
2075 read_sequnlock_excl(&mount_lock);
2077 return err;
2080 static struct mountpoint *lock_mount(struct path *path)
2082 struct vfsmount *mnt;
2083 struct dentry *dentry = path->dentry;
2084 retry:
2085 inode_lock(dentry->d_inode);
2086 if (unlikely(cant_mount(dentry))) {
2087 inode_unlock(dentry->d_inode);
2088 return ERR_PTR(-ENOENT);
2090 namespace_lock();
2091 mnt = lookup_mnt(path);
2092 if (likely(!mnt)) {
2093 struct mountpoint *mp = get_mountpoint(dentry);
2094 if (IS_ERR(mp)) {
2095 namespace_unlock();
2096 inode_unlock(dentry->d_inode);
2097 return mp;
2099 return mp;
2101 namespace_unlock();
2102 inode_unlock(path->dentry->d_inode);
2103 path_put(path);
2104 path->mnt = mnt;
2105 dentry = path->dentry = dget(mnt->mnt_root);
2106 goto retry;
2109 static void unlock_mount(struct mountpoint *where)
2111 struct dentry *dentry = where->m_dentry;
2113 read_seqlock_excl(&mount_lock);
2114 put_mountpoint(where);
2115 read_sequnlock_excl(&mount_lock);
2117 namespace_unlock();
2118 inode_unlock(dentry->d_inode);
2121 static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
2123 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
2124 return -EINVAL;
2126 if (d_is_dir(mp->m_dentry) !=
2127 d_is_dir(mnt->mnt.mnt_root))
2128 return -ENOTDIR;
2130 return attach_recursive_mnt(mnt, p, mp, NULL);
2134 * Sanity check the flags to change_mnt_propagation.
2137 static int flags_to_propagation_type(int flags)
2139 int type = flags & ~(MS_REC | MS_SILENT);
2141 /* Fail if any non-propagation flags are set */
2142 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2143 return 0;
2144 /* Only one propagation flag should be set */
2145 if (!is_power_of_2(type))
2146 return 0;
2147 return type;
2151 * recursively change the type of the mountpoint.
2153 static int do_change_type(struct path *path, int flag)
2155 struct mount *m;
2156 struct mount *mnt = real_mount(path->mnt);
2157 int recurse = flag & MS_REC;
2158 int type;
2159 int err = 0;
2161 if (path->dentry != path->mnt->mnt_root)
2162 return -EINVAL;
2164 type = flags_to_propagation_type(flag);
2165 if (!type)
2166 return -EINVAL;
2168 namespace_lock();
2169 if (type == MS_SHARED) {
2170 err = invent_group_ids(mnt, recurse);
2171 if (err)
2172 goto out_unlock;
2175 lock_mount_hash();
2176 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
2177 change_mnt_propagation(m, type);
2178 unlock_mount_hash();
2180 out_unlock:
2181 namespace_unlock();
2182 return err;
2185 static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2187 struct mount *child;
2188 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2189 if (!is_subdir(child->mnt_mountpoint, dentry))
2190 continue;
2192 if (child->mnt.mnt_flags & MNT_LOCKED)
2193 return true;
2195 return false;
2199 * do loopback mount.
2201 static int do_loopback(struct path *path, const char *old_name,
2202 int recurse)
2204 struct path old_path;
2205 struct mount *mnt = NULL, *old, *parent;
2206 struct mountpoint *mp;
2207 int err;
2208 if (!old_name || !*old_name)
2209 return -EINVAL;
2210 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
2211 if (err)
2212 return err;
2214 err = -EINVAL;
2215 if (mnt_ns_loop(old_path.dentry))
2216 goto out;
2218 mp = lock_mount(path);
2219 err = PTR_ERR(mp);
2220 if (IS_ERR(mp))
2221 goto out;
2223 old = real_mount(old_path.mnt);
2224 parent = real_mount(path->mnt);
2226 err = -EINVAL;
2227 if (IS_MNT_UNBINDABLE(old))
2228 goto out2;
2230 if (!check_mnt(parent))
2231 goto out2;
2233 if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
2234 goto out2;
2236 if (!recurse && has_locked_children(old, old_path.dentry))
2237 goto out2;
2239 if (recurse)
2240 mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
2241 else
2242 mnt = clone_mnt(old, old_path.dentry, 0);
2244 if (IS_ERR(mnt)) {
2245 err = PTR_ERR(mnt);
2246 goto out2;
2249 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2251 err = graft_tree(mnt, parent, mp);
2252 if (err) {
2253 lock_mount_hash();
2254 umount_tree(mnt, UMOUNT_SYNC);
2255 unlock_mount_hash();
2257 out2:
2258 unlock_mount(mp);
2259 out:
2260 path_put(&old_path);
2261 return err;
2264 static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
2266 int error = 0;
2267 int readonly_request = 0;
2269 if (ms_flags & MS_RDONLY)
2270 readonly_request = 1;
2271 if (readonly_request == __mnt_is_readonly(mnt))
2272 return 0;
2274 if (readonly_request)
2275 error = mnt_make_readonly(real_mount(mnt));
2276 else
2277 __mnt_unmake_readonly(real_mount(mnt));
2278 return error;
2282 * change filesystem flags. dir should be a physical root of filesystem.
2283 * If you've mounted a non-root directory somewhere and want to do remount
2284 * on it - tough luck.
2286 static int do_remount(struct path *path, int flags, int mnt_flags,
2287 void *data)
2289 int err;
2290 struct super_block *sb = path->mnt->mnt_sb;
2291 struct mount *mnt = real_mount(path->mnt);
2293 if (!check_mnt(mnt))
2294 return -EINVAL;
2296 if (path->dentry != path->mnt->mnt_root)
2297 return -EINVAL;
2299 /* Don't allow changing of locked mnt flags.
2301 * No locks need to be held here while testing the various
2302 * MNT_LOCK flags because those flags can never be cleared
2303 * once they are set.
2305 if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
2306 !(mnt_flags & MNT_READONLY)) {
2307 return -EPERM;
2309 if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
2310 !(mnt_flags & MNT_NODEV)) {
2311 return -EPERM;
2313 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
2314 !(mnt_flags & MNT_NOSUID)) {
2315 return -EPERM;
2317 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
2318 !(mnt_flags & MNT_NOEXEC)) {
2319 return -EPERM;
2321 if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
2322 ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
2323 return -EPERM;
2326 err = security_sb_remount(sb, data);
2327 if (err)
2328 return err;
2330 down_write(&sb->s_umount);
2331 if (flags & MS_BIND)
2332 err = change_mount_flags(path->mnt, flags);
2333 else if (!capable(CAP_SYS_ADMIN))
2334 err = -EPERM;
2335 else
2336 err = do_remount_sb(sb, flags, data, 0);
2337 if (!err) {
2338 lock_mount_hash();
2339 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2340 mnt->mnt.mnt_flags = mnt_flags;
2341 touch_mnt_namespace(mnt->mnt_ns);
2342 unlock_mount_hash();
2344 up_write(&sb->s_umount);
2345 return err;
2348 static inline int tree_contains_unbindable(struct mount *mnt)
2350 struct mount *p;
2351 for (p = mnt; p; p = next_mnt(p, mnt)) {
2352 if (IS_MNT_UNBINDABLE(p))
2353 return 1;
2355 return 0;
2358 static int do_move_mount(struct path *path, const char *old_name)
2360 struct path old_path, parent_path;
2361 struct mount *p;
2362 struct mount *old;
2363 struct mountpoint *mp;
2364 int err;
2365 if (!old_name || !*old_name)
2366 return -EINVAL;
2367 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2368 if (err)
2369 return err;
2371 mp = lock_mount(path);
2372 err = PTR_ERR(mp);
2373 if (IS_ERR(mp))
2374 goto out;
2376 old = real_mount(old_path.mnt);
2377 p = real_mount(path->mnt);
2379 err = -EINVAL;
2380 if (!check_mnt(p) || !check_mnt(old))
2381 goto out1;
2383 if (old->mnt.mnt_flags & MNT_LOCKED)
2384 goto out1;
2386 err = -EINVAL;
2387 if (old_path.dentry != old_path.mnt->mnt_root)
2388 goto out1;
2390 if (!mnt_has_parent(old))
2391 goto out1;
2393 if (d_is_dir(path->dentry) !=
2394 d_is_dir(old_path.dentry))
2395 goto out1;
2397 * Don't move a mount residing in a shared parent.
2399 if (IS_MNT_SHARED(old->mnt_parent))
2400 goto out1;
2402 * Don't move a mount tree containing unbindable mounts to a destination
2403 * mount which is shared.
2405 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
2406 goto out1;
2407 err = -ELOOP;
2408 for (; mnt_has_parent(p); p = p->mnt_parent)
2409 if (p == old)
2410 goto out1;
2412 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
2413 if (err)
2414 goto out1;
2416 /* if the mount is moved, it should no longer be expire
2417 * automatically */
2418 list_del_init(&old->mnt_expire);
2419 out1:
2420 unlock_mount(mp);
2421 out:
2422 if (!err)
2423 path_put(&parent_path);
2424 path_put(&old_path);
2425 return err;
2428 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
2430 int err;
2431 const char *subtype = strchr(fstype, '.');
2432 if (subtype) {
2433 subtype++;
2434 err = -EINVAL;
2435 if (!subtype[0])
2436 goto err;
2437 } else
2438 subtype = "";
2440 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
2441 err = -ENOMEM;
2442 if (!mnt->mnt_sb->s_subtype)
2443 goto err;
2444 return mnt;
2446 err:
2447 mntput(mnt);
2448 return ERR_PTR(err);
2452 * add a mount into a namespace's mount tree
2454 static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
2456 struct mountpoint *mp;
2457 struct mount *parent;
2458 int err;
2460 mnt_flags &= ~MNT_INTERNAL_FLAGS;
2462 mp = lock_mount(path);
2463 if (IS_ERR(mp))
2464 return PTR_ERR(mp);
2466 parent = real_mount(path->mnt);
2467 err = -EINVAL;
2468 if (unlikely(!check_mnt(parent))) {
2469 /* that's acceptable only for automounts done in private ns */
2470 if (!(mnt_flags & MNT_SHRINKABLE))
2471 goto unlock;
2472 /* ... and for those we'd better have mountpoint still alive */
2473 if (!parent->mnt_ns)
2474 goto unlock;
2477 /* Refuse the same filesystem on the same mount point */
2478 err = -EBUSY;
2479 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
2480 path->mnt->mnt_root == path->dentry)
2481 goto unlock;
2483 err = -EINVAL;
2484 if (d_is_symlink(newmnt->mnt.mnt_root))
2485 goto unlock;
2487 newmnt->mnt.mnt_flags = mnt_flags;
2488 err = graft_tree(newmnt, parent, mp);
2490 unlock:
2491 unlock_mount(mp);
2492 return err;
2495 static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
2498 * create a new mount for userspace and request it to be added into the
2499 * namespace's tree
2501 static int do_new_mount(struct path *path, const char *fstype, int flags,
2502 int mnt_flags, const char *name, void *data)
2504 struct file_system_type *type;
2505 struct vfsmount *mnt;
2506 int err;
2508 if (!fstype)
2509 return -EINVAL;
2511 type = get_fs_type(fstype);
2512 if (!type)
2513 return -ENODEV;
2515 mnt = vfs_kern_mount(type, flags, name, data);
2516 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2517 !mnt->mnt_sb->s_subtype)
2518 mnt = fs_set_subtype(mnt, fstype);
2520 put_filesystem(type);
2521 if (IS_ERR(mnt))
2522 return PTR_ERR(mnt);
2524 if (mount_too_revealing(mnt, &mnt_flags)) {
2525 mntput(mnt);
2526 return -EPERM;
2529 err = do_add_mount(real_mount(mnt), path, mnt_flags);
2530 if (err)
2531 mntput(mnt);
2532 return err;
2535 int finish_automount(struct vfsmount *m, struct path *path)
2537 struct mount *mnt = real_mount(m);
2538 int err;
2539 /* The new mount record should have at least 2 refs to prevent it being
2540 * expired before we get a chance to add it
2542 BUG_ON(mnt_get_count(mnt) < 2);
2544 if (m->mnt_sb == path->mnt->mnt_sb &&
2545 m->mnt_root == path->dentry) {
2546 err = -ELOOP;
2547 goto fail;
2550 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2551 if (!err)
2552 return 0;
2553 fail:
2554 /* remove m from any expiration list it may be on */
2555 if (!list_empty(&mnt->mnt_expire)) {
2556 namespace_lock();
2557 list_del_init(&mnt->mnt_expire);
2558 namespace_unlock();
2560 mntput(m);
2561 mntput(m);
2562 return err;
2566 * mnt_set_expiry - Put a mount on an expiration list
2567 * @mnt: The mount to list.
2568 * @expiry_list: The list to add the mount to.
2570 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2572 namespace_lock();
2574 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2576 namespace_unlock();
2578 EXPORT_SYMBOL(mnt_set_expiry);
2581 * process a list of expirable mountpoints with the intent of discarding any
2582 * mountpoints that aren't in use and haven't been touched since last we came
2583 * here
2585 void mark_mounts_for_expiry(struct list_head *mounts)
2587 struct mount *mnt, *next;
2588 LIST_HEAD(graveyard);
2590 if (list_empty(mounts))
2591 return;
2593 namespace_lock();
2594 lock_mount_hash();
2596 /* extract from the expiration list every vfsmount that matches the
2597 * following criteria:
2598 * - only referenced by its parent vfsmount
2599 * - still marked for expiry (marked on the last call here; marks are
2600 * cleared by mntput())
2602 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2603 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
2604 propagate_mount_busy(mnt, 1))
2605 continue;
2606 list_move(&mnt->mnt_expire, &graveyard);
2608 while (!list_empty(&graveyard)) {
2609 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2610 touch_mnt_namespace(mnt->mnt_ns);
2611 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2613 unlock_mount_hash();
2614 namespace_unlock();
2617 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2620 * Ripoff of 'select_parent()'
2622 * search the list of submounts for a given mountpoint, and move any
2623 * shrinkable submounts to the 'graveyard' list.
2625 static int select_submounts(struct mount *parent, struct list_head *graveyard)
2627 struct mount *this_parent = parent;
2628 struct list_head *next;
2629 int found = 0;
2631 repeat:
2632 next = this_parent->mnt_mounts.next;
2633 resume:
2634 while (next != &this_parent->mnt_mounts) {
2635 struct list_head *tmp = next;
2636 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
2638 next = tmp->next;
2639 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
2640 continue;
2642 * Descend a level if the d_mounts list is non-empty.
2644 if (!list_empty(&mnt->mnt_mounts)) {
2645 this_parent = mnt;
2646 goto repeat;
2649 if (!propagate_mount_busy(mnt, 1)) {
2650 list_move_tail(&mnt->mnt_expire, graveyard);
2651 found++;
2655 * All done at this level ... ascend and resume the search
2657 if (this_parent != parent) {
2658 next = this_parent->mnt_child.next;
2659 this_parent = this_parent->mnt_parent;
2660 goto resume;
2662 return found;
2666 * process a list of expirable mountpoints with the intent of discarding any
2667 * submounts of a specific parent mountpoint
2669 * mount_lock must be held for write
2671 static void shrink_submounts(struct mount *mnt)
2673 LIST_HEAD(graveyard);
2674 struct mount *m;
2676 /* extract submounts of 'mountpoint' from the expiration list */
2677 while (select_submounts(mnt, &graveyard)) {
2678 while (!list_empty(&graveyard)) {
2679 m = list_first_entry(&graveyard, struct mount,
2680 mnt_expire);
2681 touch_mnt_namespace(m->mnt_ns);
2682 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2688 * Some copy_from_user() implementations do not return the exact number of
2689 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2690 * Note that this function differs from copy_from_user() in that it will oops
2691 * on bad values of `to', rather than returning a short copy.
2693 static long exact_copy_from_user(void *to, const void __user * from,
2694 unsigned long n)
2696 char *t = to;
2697 const char __user *f = from;
2698 char c;
2700 if (!access_ok(VERIFY_READ, from, n))
2701 return n;
2703 while (n) {
2704 if (__get_user(c, f)) {
2705 memset(t, 0, n);
2706 break;
2708 *t++ = c;
2709 f++;
2710 n--;
2712 return n;
2715 void *copy_mount_options(const void __user * data)
2717 int i;
2718 unsigned long size;
2719 char *copy;
2721 if (!data)
2722 return NULL;
2724 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2725 if (!copy)
2726 return ERR_PTR(-ENOMEM);
2728 /* We only care that *some* data at the address the user
2729 * gave us is valid. Just in case, we'll zero
2730 * the remainder of the page.
2732 /* copy_from_user cannot cross TASK_SIZE ! */
2733 size = TASK_SIZE - (unsigned long)data;
2734 if (size > PAGE_SIZE)
2735 size = PAGE_SIZE;
2737 i = size - exact_copy_from_user(copy, data, size);
2738 if (!i) {
2739 kfree(copy);
2740 return ERR_PTR(-EFAULT);
2742 if (i != PAGE_SIZE)
2743 memset(copy + i, 0, PAGE_SIZE - i);
2744 return copy;
2747 char *copy_mount_string(const void __user *data)
2749 return data ? strndup_user(data, PAGE_SIZE) : NULL;
2753 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2754 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2756 * data is a (void *) that can point to any structure up to
2757 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2758 * information (or be NULL).
2760 * Pre-0.97 versions of mount() didn't have a flags word.
2761 * When the flags word was introduced its top half was required
2762 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2763 * Therefore, if this magic number is present, it carries no information
2764 * and must be discarded.
2766 long do_mount(const char *dev_name, const char __user *dir_name,
2767 const char *type_page, unsigned long flags, void *data_page)
2769 struct path path;
2770 int retval = 0;
2771 int mnt_flags = 0;
2773 /* Discard magic */
2774 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2775 flags &= ~MS_MGC_MSK;
2777 /* Basic sanity checks */
2778 if (data_page)
2779 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2781 /* ... and get the mountpoint */
2782 retval = user_path(dir_name, &path);
2783 if (retval)
2784 return retval;
2786 retval = security_sb_mount(dev_name, &path,
2787 type_page, flags, data_page);
2788 if (!retval && !may_mount())
2789 retval = -EPERM;
2790 if (!retval && (flags & MS_MANDLOCK) && !may_mandlock())
2791 retval = -EPERM;
2792 if (retval)
2793 goto dput_out;
2795 /* Default to relatime unless overriden */
2796 if (!(flags & MS_NOATIME))
2797 mnt_flags |= MNT_RELATIME;
2799 /* Separate the per-mountpoint flags */
2800 if (flags & MS_NOSUID)
2801 mnt_flags |= MNT_NOSUID;
2802 if (flags & MS_NODEV)
2803 mnt_flags |= MNT_NODEV;
2804 if (flags & MS_NOEXEC)
2805 mnt_flags |= MNT_NOEXEC;
2806 if (flags & MS_NOATIME)
2807 mnt_flags |= MNT_NOATIME;
2808 if (flags & MS_NODIRATIME)
2809 mnt_flags |= MNT_NODIRATIME;
2810 if (flags & MS_STRICTATIME)
2811 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2812 if (flags & MS_RDONLY)
2813 mnt_flags |= MNT_READONLY;
2815 /* The default atime for remount is preservation */
2816 if ((flags & MS_REMOUNT) &&
2817 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2818 MS_STRICTATIME)) == 0)) {
2819 mnt_flags &= ~MNT_ATIME_MASK;
2820 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2823 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2824 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2825 MS_STRICTATIME | MS_NOREMOTELOCK | MS_SUBMOUNT);
2827 if (flags & MS_REMOUNT)
2828 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
2829 data_page);
2830 else if (flags & MS_BIND)
2831 retval = do_loopback(&path, dev_name, flags & MS_REC);
2832 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2833 retval = do_change_type(&path, flags);
2834 else if (flags & MS_MOVE)
2835 retval = do_move_mount(&path, dev_name);
2836 else
2837 retval = do_new_mount(&path, type_page, flags, mnt_flags,
2838 dev_name, data_page);
2839 dput_out:
2840 path_put(&path);
2841 return retval;
2844 static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
2846 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
2849 static void dec_mnt_namespaces(struct ucounts *ucounts)
2851 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
2854 static void free_mnt_ns(struct mnt_namespace *ns)
2856 ns_free_inum(&ns->ns);
2857 dec_mnt_namespaces(ns->ucounts);
2858 put_user_ns(ns->user_ns);
2859 kfree(ns);
2863 * Assign a sequence number so we can detect when we attempt to bind
2864 * mount a reference to an older mount namespace into the current
2865 * mount namespace, preventing reference counting loops. A 64bit
2866 * number incrementing at 10Ghz will take 12,427 years to wrap which
2867 * is effectively never, so we can ignore the possibility.
2869 static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2871 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2873 struct mnt_namespace *new_ns;
2874 struct ucounts *ucounts;
2875 int ret;
2877 ucounts = inc_mnt_namespaces(user_ns);
2878 if (!ucounts)
2879 return ERR_PTR(-ENOSPC);
2881 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2882 if (!new_ns) {
2883 dec_mnt_namespaces(ucounts);
2884 return ERR_PTR(-ENOMEM);
2886 ret = ns_alloc_inum(&new_ns->ns);
2887 if (ret) {
2888 kfree(new_ns);
2889 dec_mnt_namespaces(ucounts);
2890 return ERR_PTR(ret);
2892 new_ns->ns.ops = &mntns_operations;
2893 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2894 atomic_set(&new_ns->count, 1);
2895 new_ns->root = NULL;
2896 INIT_LIST_HEAD(&new_ns->list);
2897 init_waitqueue_head(&new_ns->poll);
2898 new_ns->event = 0;
2899 new_ns->user_ns = get_user_ns(user_ns);
2900 new_ns->ucounts = ucounts;
2901 new_ns->mounts = 0;
2902 new_ns->pending_mounts = 0;
2903 return new_ns;
2906 __latent_entropy
2907 struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2908 struct user_namespace *user_ns, struct fs_struct *new_fs)
2910 struct mnt_namespace *new_ns;
2911 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2912 struct mount *p, *q;
2913 struct mount *old;
2914 struct mount *new;
2915 int copy_flags;
2917 BUG_ON(!ns);
2919 if (likely(!(flags & CLONE_NEWNS))) {
2920 get_mnt_ns(ns);
2921 return ns;
2924 old = ns->root;
2926 new_ns = alloc_mnt_ns(user_ns);
2927 if (IS_ERR(new_ns))
2928 return new_ns;
2930 namespace_lock();
2931 /* First pass: copy the tree topology */
2932 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
2933 if (user_ns != ns->user_ns)
2934 copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
2935 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2936 if (IS_ERR(new)) {
2937 namespace_unlock();
2938 free_mnt_ns(new_ns);
2939 return ERR_CAST(new);
2941 new_ns->root = new;
2942 list_add_tail(&new_ns->list, &new->mnt_list);
2945 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2946 * as belonging to new namespace. We have already acquired a private
2947 * fs_struct, so tsk->fs->lock is not needed.
2949 p = old;
2950 q = new;
2951 while (p) {
2952 q->mnt_ns = new_ns;
2953 new_ns->mounts++;
2954 if (new_fs) {
2955 if (&p->mnt == new_fs->root.mnt) {
2956 new_fs->root.mnt = mntget(&q->mnt);
2957 rootmnt = &p->mnt;
2959 if (&p->mnt == new_fs->pwd.mnt) {
2960 new_fs->pwd.mnt = mntget(&q->mnt);
2961 pwdmnt = &p->mnt;
2964 p = next_mnt(p, old);
2965 q = next_mnt(q, new);
2966 if (!q)
2967 break;
2968 while (p->mnt.mnt_root != q->mnt.mnt_root)
2969 p = next_mnt(p, old);
2971 namespace_unlock();
2973 if (rootmnt)
2974 mntput(rootmnt);
2975 if (pwdmnt)
2976 mntput(pwdmnt);
2978 return new_ns;
2982 * create_mnt_ns - creates a private namespace and adds a root filesystem
2983 * @mnt: pointer to the new root filesystem mountpoint
2985 static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2987 struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2988 if (!IS_ERR(new_ns)) {
2989 struct mount *mnt = real_mount(m);
2990 mnt->mnt_ns = new_ns;
2991 new_ns->root = mnt;
2992 new_ns->mounts++;
2993 list_add(&mnt->mnt_list, &new_ns->list);
2994 } else {
2995 mntput(m);
2997 return new_ns;
3000 struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
3002 struct mnt_namespace *ns;
3003 struct super_block *s;
3004 struct path path;
3005 int err;
3007 ns = create_mnt_ns(mnt);
3008 if (IS_ERR(ns))
3009 return ERR_CAST(ns);
3011 err = vfs_path_lookup(mnt->mnt_root, mnt,
3012 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3014 put_mnt_ns(ns);
3016 if (err)
3017 return ERR_PTR(err);
3019 /* trade a vfsmount reference for active sb one */
3020 s = path.mnt->mnt_sb;
3021 atomic_inc(&s->s_active);
3022 mntput(path.mnt);
3023 /* lock the sucker */
3024 down_write(&s->s_umount);
3025 /* ... and return the root of (sub)tree on it */
3026 return path.dentry;
3028 EXPORT_SYMBOL(mount_subtree);
3030 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3031 char __user *, type, unsigned long, flags, void __user *, data)
3033 int ret;
3034 char *kernel_type;
3035 char *kernel_dev;
3036 void *options;
3038 kernel_type = copy_mount_string(type);
3039 ret = PTR_ERR(kernel_type);
3040 if (IS_ERR(kernel_type))
3041 goto out_type;
3043 kernel_dev = copy_mount_string(dev_name);
3044 ret = PTR_ERR(kernel_dev);
3045 if (IS_ERR(kernel_dev))
3046 goto out_dev;
3048 options = copy_mount_options(data);
3049 ret = PTR_ERR(options);
3050 if (IS_ERR(options))
3051 goto out_data;
3053 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3055 kfree(options);
3056 out_data:
3057 kfree(kernel_dev);
3058 out_dev:
3059 kfree(kernel_type);
3060 out_type:
3061 return ret;
3065 * Return true if path is reachable from root
3067 * namespace_sem or mount_lock is held
3069 bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
3070 const struct path *root)
3072 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
3073 dentry = mnt->mnt_mountpoint;
3074 mnt = mnt->mnt_parent;
3076 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
3079 bool path_is_under(const struct path *path1, const struct path *path2)
3081 bool res;
3082 read_seqlock_excl(&mount_lock);
3083 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
3084 read_sequnlock_excl(&mount_lock);
3085 return res;
3087 EXPORT_SYMBOL(path_is_under);
3090 * pivot_root Semantics:
3091 * Moves the root file system of the current process to the directory put_old,
3092 * makes new_root as the new root file system of the current process, and sets
3093 * root/cwd of all processes which had them on the current root to new_root.
3095 * Restrictions:
3096 * The new_root and put_old must be directories, and must not be on the
3097 * same file system as the current process root. The put_old must be
3098 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3099 * pointed to by put_old must yield the same directory as new_root. No other
3100 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3102 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
3103 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
3104 * in this situation.
3106 * Notes:
3107 * - we don't move root/cwd if they are not at the root (reason: if something
3108 * cared enough to change them, it's probably wrong to force them elsewhere)
3109 * - it's okay to pick a root that isn't the root of a file system, e.g.
3110 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3111 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3112 * first.
3114 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3115 const char __user *, put_old)
3117 struct path new, old, parent_path, root_parent, root;
3118 struct mount *new_mnt, *root_mnt, *old_mnt;
3119 struct mountpoint *old_mp, *root_mp;
3120 int error;
3122 if (!may_mount())
3123 return -EPERM;
3125 error = user_path_dir(new_root, &new);
3126 if (error)
3127 goto out0;
3129 error = user_path_dir(put_old, &old);
3130 if (error)
3131 goto out1;
3133 error = security_sb_pivotroot(&old, &new);
3134 if (error)
3135 goto out2;
3137 get_fs_root(current->fs, &root);
3138 old_mp = lock_mount(&old);
3139 error = PTR_ERR(old_mp);
3140 if (IS_ERR(old_mp))
3141 goto out3;
3143 error = -EINVAL;
3144 new_mnt = real_mount(new.mnt);
3145 root_mnt = real_mount(root.mnt);
3146 old_mnt = real_mount(old.mnt);
3147 if (IS_MNT_SHARED(old_mnt) ||
3148 IS_MNT_SHARED(new_mnt->mnt_parent) ||
3149 IS_MNT_SHARED(root_mnt->mnt_parent))
3150 goto out4;
3151 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
3152 goto out4;
3153 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3154 goto out4;
3155 error = -ENOENT;
3156 if (d_unlinked(new.dentry))
3157 goto out4;
3158 error = -EBUSY;
3159 if (new_mnt == root_mnt || old_mnt == root_mnt)
3160 goto out4; /* loop, on the same file system */
3161 error = -EINVAL;
3162 if (root.mnt->mnt_root != root.dentry)
3163 goto out4; /* not a mountpoint */
3164 if (!mnt_has_parent(root_mnt))
3165 goto out4; /* not attached */
3166 root_mp = root_mnt->mnt_mp;
3167 if (new.mnt->mnt_root != new.dentry)
3168 goto out4; /* not a mountpoint */
3169 if (!mnt_has_parent(new_mnt))
3170 goto out4; /* not attached */
3171 /* make sure we can reach put_old from new_root */
3172 if (!is_path_reachable(old_mnt, old.dentry, &new))
3173 goto out4;
3174 /* make certain new is below the root */
3175 if (!is_path_reachable(new_mnt, new.dentry, &root))
3176 goto out4;
3177 root_mp->m_count++; /* pin it so it won't go away */
3178 lock_mount_hash();
3179 detach_mnt(new_mnt, &parent_path);
3180 detach_mnt(root_mnt, &root_parent);
3181 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3182 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3183 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3185 /* mount old root on put_old */
3186 attach_mnt(root_mnt, old_mnt, old_mp);
3187 /* mount new_root on / */
3188 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
3189 touch_mnt_namespace(current->nsproxy->mnt_ns);
3190 /* A moved mount should not expire automatically */
3191 list_del_init(&new_mnt->mnt_expire);
3192 put_mountpoint(root_mp);
3193 unlock_mount_hash();
3194 chroot_fs_refs(&root, &new);
3195 error = 0;
3196 out4:
3197 unlock_mount(old_mp);
3198 if (!error) {
3199 path_put(&root_parent);
3200 path_put(&parent_path);
3202 out3:
3203 path_put(&root);
3204 out2:
3205 path_put(&old);
3206 out1:
3207 path_put(&new);
3208 out0:
3209 return error;
3212 static void __init init_mount_tree(void)
3214 struct vfsmount *mnt;
3215 struct mnt_namespace *ns;
3216 struct path root;
3217 struct file_system_type *type;
3219 type = get_fs_type("rootfs");
3220 if (!type)
3221 panic("Can't find rootfs type");
3222 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
3223 put_filesystem(type);
3224 if (IS_ERR(mnt))
3225 panic("Can't create rootfs");
3227 ns = create_mnt_ns(mnt);
3228 if (IS_ERR(ns))
3229 panic("Can't allocate initial namespace");
3231 init_task.nsproxy->mnt_ns = ns;
3232 get_mnt_ns(ns);
3234 root.mnt = mnt;
3235 root.dentry = mnt->mnt_root;
3236 mnt->mnt_flags |= MNT_LOCKED;
3238 set_fs_pwd(current->fs, &root);
3239 set_fs_root(current->fs, &root);
3242 void __init mnt_init(void)
3244 unsigned u;
3245 int err;
3247 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
3248 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
3250 mount_hashtable = alloc_large_system_hash("Mount-cache",
3251 sizeof(struct hlist_head),
3252 mhash_entries, 19,
3254 &m_hash_shift, &m_hash_mask, 0, 0);
3255 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
3256 sizeof(struct hlist_head),
3257 mphash_entries, 19,
3259 &mp_hash_shift, &mp_hash_mask, 0, 0);
3261 if (!mount_hashtable || !mountpoint_hashtable)
3262 panic("Failed to allocate mount hash table\n");
3264 for (u = 0; u <= m_hash_mask; u++)
3265 INIT_HLIST_HEAD(&mount_hashtable[u]);
3266 for (u = 0; u <= mp_hash_mask; u++)
3267 INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
3269 kernfs_init();
3271 err = sysfs_init();
3272 if (err)
3273 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
3274 __func__, err);
3275 fs_kobj = kobject_create_and_add("fs", NULL);
3276 if (!fs_kobj)
3277 printk(KERN_WARNING "%s: kobj create error\n", __func__);
3278 init_rootfs();
3279 init_mount_tree();
3282 void put_mnt_ns(struct mnt_namespace *ns)
3284 if (!atomic_dec_and_test(&ns->count))
3285 return;
3286 drop_collected_mounts(&ns->root->mnt);
3287 free_mnt_ns(ns);
3290 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
3292 struct vfsmount *mnt;
3293 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
3294 if (!IS_ERR(mnt)) {
3296 * it is a longterm mount, don't release mnt until
3297 * we unmount before file sys is unregistered
3299 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3301 return mnt;
3303 EXPORT_SYMBOL_GPL(kern_mount_data);
3305 void kern_unmount(struct vfsmount *mnt)
3307 /* release long term mount so mount point can be released */
3308 if (!IS_ERR_OR_NULL(mnt)) {
3309 real_mount(mnt)->mnt_ns = NULL;
3310 synchronize_rcu(); /* yecchhh... */
3311 mntput(mnt);
3314 EXPORT_SYMBOL(kern_unmount);
3316 bool our_mnt(struct vfsmount *mnt)
3318 return check_mnt(real_mount(mnt));
3321 bool current_chrooted(void)
3323 /* Does the current process have a non-standard root */
3324 struct path ns_root;
3325 struct path fs_root;
3326 bool chrooted;
3328 /* Find the namespace root */
3329 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3330 ns_root.dentry = ns_root.mnt->mnt_root;
3331 path_get(&ns_root);
3332 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3335 get_fs_root(current->fs, &fs_root);
3337 chrooted = !path_equal(&fs_root, &ns_root);
3339 path_put(&fs_root);
3340 path_put(&ns_root);
3342 return chrooted;
3345 static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
3346 int *new_mnt_flags)
3348 int new_flags = *new_mnt_flags;
3349 struct mount *mnt;
3350 bool visible = false;
3352 down_read(&namespace_sem);
3353 list_for_each_entry(mnt, &ns->list, mnt_list) {
3354 struct mount *child;
3355 int mnt_flags;
3357 if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
3358 continue;
3360 /* This mount is not fully visible if it's root directory
3361 * is not the root directory of the filesystem.
3363 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
3364 continue;
3366 /* A local view of the mount flags */
3367 mnt_flags = mnt->mnt.mnt_flags;
3369 /* Don't miss readonly hidden in the superblock flags */
3370 if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY)
3371 mnt_flags |= MNT_LOCK_READONLY;
3373 /* Verify the mount flags are equal to or more permissive
3374 * than the proposed new mount.
3376 if ((mnt_flags & MNT_LOCK_READONLY) &&
3377 !(new_flags & MNT_READONLY))
3378 continue;
3379 if ((mnt_flags & MNT_LOCK_ATIME) &&
3380 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
3381 continue;
3383 /* This mount is not fully visible if there are any
3384 * locked child mounts that cover anything except for
3385 * empty directories.
3387 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3388 struct inode *inode = child->mnt_mountpoint->d_inode;
3389 /* Only worry about locked mounts */
3390 if (!(child->mnt.mnt_flags & MNT_LOCKED))
3391 continue;
3392 /* Is the directory permanetly empty? */
3393 if (!is_empty_dir_inode(inode))
3394 goto next;
3396 /* Preserve the locked attributes */
3397 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
3398 MNT_LOCK_ATIME);
3399 visible = true;
3400 goto found;
3401 next: ;
3403 found:
3404 up_read(&namespace_sem);
3405 return visible;
3408 static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
3410 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
3411 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3412 unsigned long s_iflags;
3414 if (ns->user_ns == &init_user_ns)
3415 return false;
3417 /* Can this filesystem be too revealing? */
3418 s_iflags = mnt->mnt_sb->s_iflags;
3419 if (!(s_iflags & SB_I_USERNS_VISIBLE))
3420 return false;
3422 if ((s_iflags & required_iflags) != required_iflags) {
3423 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3424 required_iflags);
3425 return true;
3428 return !mnt_already_visible(ns, mnt, new_mnt_flags);
3431 bool mnt_may_suid(struct vfsmount *mnt)
3434 * Foreign mounts (accessed via fchdir or through /proc
3435 * symlinks) are always treated as if they are nosuid. This
3436 * prevents namespaces from trusting potentially unsafe
3437 * suid/sgid bits, file caps, or security labels that originate
3438 * in other namespaces.
3440 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3441 current_in_userns(mnt->mnt_sb->s_user_ns);
3444 static struct ns_common *mntns_get(struct task_struct *task)
3446 struct ns_common *ns = NULL;
3447 struct nsproxy *nsproxy;
3449 task_lock(task);
3450 nsproxy = task->nsproxy;
3451 if (nsproxy) {
3452 ns = &nsproxy->mnt_ns->ns;
3453 get_mnt_ns(to_mnt_ns(ns));
3455 task_unlock(task);
3457 return ns;
3460 static void mntns_put(struct ns_common *ns)
3462 put_mnt_ns(to_mnt_ns(ns));
3465 static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
3467 struct fs_struct *fs = current->fs;
3468 struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
3469 struct path root;
3471 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3472 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3473 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3474 return -EPERM;
3476 if (fs->users != 1)
3477 return -EINVAL;
3479 get_mnt_ns(mnt_ns);
3480 put_mnt_ns(nsproxy->mnt_ns);
3481 nsproxy->mnt_ns = mnt_ns;
3483 /* Find the root */
3484 root.mnt = &mnt_ns->root->mnt;
3485 root.dentry = mnt_ns->root->mnt.mnt_root;
3486 path_get(&root);
3487 while(d_mountpoint(root.dentry) && follow_down_one(&root))
3490 /* Update the pwd and root */
3491 set_fs_pwd(fs, &root);
3492 set_fs_root(fs, &root);
3494 path_put(&root);
3495 return 0;
3498 static struct user_namespace *mntns_owner(struct ns_common *ns)
3500 return to_mnt_ns(ns)->user_ns;
3503 const struct proc_ns_operations mntns_operations = {
3504 .name = "mnt",
3505 .type = CLONE_NEWNS,
3506 .get = mntns_get,
3507 .put = mntns_put,
3508 .install = mntns_install,
3509 .owner = mntns_owner,