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