blob: dc519a1437ee949da2e549886c838fc4698dad5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namespace.c
3 *
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
6 *
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/syscalls.h>
Al Virod10577a2011-12-07 13:06:11 -050012#include <linux/export.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080014#include <linux/mnt_namespace.h>
Eric W. Biederman771b1372012-07-26 21:08:32 -070015#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/namei.h>
17#include <linux/security.h>
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010018#include <linux/idr.h>
Al Virod10577a2011-12-07 13:06:11 -050019#include <linux/acct.h> /* acct_auto_close_mnt */
20#include <linux/ramfs.h> /* init_rootfs */
21#include <linux/fs_struct.h> /* get_fs_root et.al. */
22#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
23#include <linux/uaccess.h>
David Howells0bb80f22013-04-12 01:50:06 +010024#include <linux/proc_ns.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070025#include <linux/magic.h>
Ram Pai07b20882005-11-07 17:19:07 -050026#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070027#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Eric Dumazet13f14b42008-02-06 01:37:57 -080029#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
30#define HASH_SIZE (1UL << HASH_SHIFT)
31
Al Viro5addc5d2005-11-07 17:15:49 -050032static int event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010033static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010034static DEFINE_IDA(mnt_group_ida);
Nick Piggin99b7db72010-08-18 04:37:39 +100035static DEFINE_SPINLOCK(mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040036static int mnt_id_start = 0;
37static int mnt_group_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Eric Dumazetfa3536c2006-03-26 01:37:24 -080039static struct list_head *mount_hashtable __read_mostly;
Al Viro84d17192013-03-15 10:53:28 -040040static struct list_head *mountpoint_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080041static struct kmem_cache *mnt_cache __read_mostly;
Ram Pai390c6842005-11-07 17:17:51 -050042static struct rw_semaphore namespace_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080044/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060045struct kobject *fs_kobj;
46EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080047
Nick Piggin99b7db72010-08-18 04:37:39 +100048/*
49 * vfsmount lock may be taken for read to prevent changes to the
50 * vfsmount hash, ie. during mountpoint lookups or walking back
51 * up the tree.
52 *
53 * It should be taken for write in all cases where the vfsmount
54 * tree or hash is modified or when a vfsmount structure is modified.
55 */
56DEFINE_BRLOCK(vfsmount_lock);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
59{
Ram Paib58fed82005-11-07 17:16:09 -050060 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
61 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Eric Dumazet13f14b42008-02-06 01:37:57 -080062 tmp = tmp + (tmp >> HASH_SHIFT);
63 return tmp & (HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Dave Hansen3d733632008-02-15 14:37:59 -080066#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
67
Nick Piggin99b7db72010-08-18 04:37:39 +100068/*
69 * allocation is serialized by namespace_sem, but we need the spinlock to
70 * serialize with freeing.
71 */
Al Virob105e272011-11-24 20:38:33 -050072static int mnt_alloc_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010073{
74 int res;
75
76retry:
77 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
Nick Piggin99b7db72010-08-18 04:37:39 +100078 spin_lock(&mnt_id_lock);
Al Viro15169fe2011-11-25 00:50:41 -050079 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
Al Virof21f6222009-06-24 03:12:00 -040080 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -050081 mnt_id_start = mnt->mnt_id + 1;
Nick Piggin99b7db72010-08-18 04:37:39 +100082 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010083 if (res == -EAGAIN)
84 goto retry;
85
86 return res;
87}
88
Al Virob105e272011-11-24 20:38:33 -050089static void mnt_free_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010090{
Al Viro15169fe2011-11-25 00:50:41 -050091 int id = mnt->mnt_id;
Nick Piggin99b7db72010-08-18 04:37:39 +100092 spin_lock(&mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040093 ida_remove(&mnt_id_ida, id);
94 if (mnt_id_start > id)
95 mnt_id_start = id;
Nick Piggin99b7db72010-08-18 04:37:39 +100096 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010097}
98
Miklos Szeredi719f5d72008-03-27 13:06:23 +010099/*
100 * Allocate a new peer group ID
101 *
102 * mnt_group_ida is protected by namespace_sem
103 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500104static int mnt_alloc_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100105{
Al Virof21f6222009-06-24 03:12:00 -0400106 int res;
107
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100108 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
109 return -ENOMEM;
110
Al Virof21f6222009-06-24 03:12:00 -0400111 res = ida_get_new_above(&mnt_group_ida,
112 mnt_group_start,
Al Viro15169fe2011-11-25 00:50:41 -0500113 &mnt->mnt_group_id);
Al Virof21f6222009-06-24 03:12:00 -0400114 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -0500115 mnt_group_start = mnt->mnt_group_id + 1;
Al Virof21f6222009-06-24 03:12:00 -0400116
117 return res;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100118}
119
120/*
121 * Release a peer group ID
122 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500123void mnt_release_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100124{
Al Viro15169fe2011-11-25 00:50:41 -0500125 int id = mnt->mnt_group_id;
Al Virof21f6222009-06-24 03:12:00 -0400126 ida_remove(&mnt_group_ida, id);
127 if (mnt_group_start > id)
128 mnt_group_start = id;
Al Viro15169fe2011-11-25 00:50:41 -0500129 mnt->mnt_group_id = 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100130}
131
Nick Pigginb3e19d92011-01-07 17:50:11 +1100132/*
133 * vfsmount lock must be held for read
134 */
Al Viro83adc752011-11-24 22:37:54 -0500135static inline void mnt_add_count(struct mount *mnt, int n)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100136{
137#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500138 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100139#else
140 preempt_disable();
Al Viro68e8a9f2011-11-24 22:53:09 -0500141 mnt->mnt_count += n;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100142 preempt_enable();
143#endif
144}
145
Nick Pigginb3e19d92011-01-07 17:50:11 +1100146/*
147 * vfsmount lock must be held for write
148 */
Al Viro83adc752011-11-24 22:37:54 -0500149unsigned int mnt_get_count(struct mount *mnt)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100150{
151#ifdef CONFIG_SMP
Al Virof03c6592011-01-14 22:30:21 -0500152 unsigned int count = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100153 int cpu;
154
155 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500156 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100157 }
158
159 return count;
160#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500161 return mnt->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100162#endif
163}
164
Al Virob105e272011-11-24 20:38:33 -0500165static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Al Viroc63181e2011-11-25 02:35:16 -0500167 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
168 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100169 int err;
170
Al Viroc63181e2011-11-25 02:35:16 -0500171 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800172 if (err)
173 goto out_free_cache;
174
175 if (name) {
Al Viroc63181e2011-11-25 02:35:16 -0500176 mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
177 if (!mnt->mnt_devname)
Li Zefan88b38782008-07-21 18:06:36 +0800178 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100179 }
180
Nick Pigginb3e19d92011-01-07 17:50:11 +1100181#ifdef CONFIG_SMP
Al Viroc63181e2011-11-25 02:35:16 -0500182 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
183 if (!mnt->mnt_pcp)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100184 goto out_free_devname;
185
Al Viroc63181e2011-11-25 02:35:16 -0500186 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100187#else
Al Viroc63181e2011-11-25 02:35:16 -0500188 mnt->mnt_count = 1;
189 mnt->mnt_writers = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100190#endif
191
Al Viroc63181e2011-11-25 02:35:16 -0500192 INIT_LIST_HEAD(&mnt->mnt_hash);
193 INIT_LIST_HEAD(&mnt->mnt_child);
194 INIT_LIST_HEAD(&mnt->mnt_mounts);
195 INIT_LIST_HEAD(&mnt->mnt_list);
196 INIT_LIST_HEAD(&mnt->mnt_expire);
197 INIT_LIST_HEAD(&mnt->mnt_share);
198 INIT_LIST_HEAD(&mnt->mnt_slave_list);
199 INIT_LIST_HEAD(&mnt->mnt_slave);
Andreas Gruenbacher2504c5d2009-12-17 21:24:27 -0500200#ifdef CONFIG_FSNOTIFY
201 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Al Viroc63181e2011-11-25 02:35:16 -0500204 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800205
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000206#ifdef CONFIG_SMP
207out_free_devname:
Al Viroc63181e2011-11-25 02:35:16 -0500208 kfree(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000209#endif
Li Zefan88b38782008-07-21 18:06:36 +0800210out_free_id:
Al Viroc63181e2011-11-25 02:35:16 -0500211 mnt_free_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800212out_free_cache:
Al Viroc63181e2011-11-25 02:35:16 -0500213 kmem_cache_free(mnt_cache, mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800214 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Dave Hansen83660252008-02-15 14:37:30 -0800217/*
218 * Most r/o checks on a fs are for operations that take
219 * discrete amounts of time, like a write() or unlink().
220 * We must keep track of when those operations start
221 * (for permission checks) and when they end, so that
222 * we can determine when writes are able to occur to
223 * a filesystem.
224 */
Dave Hansen3d733632008-02-15 14:37:59 -0800225/*
226 * __mnt_is_readonly: check whether a mount is read-only
227 * @mnt: the mount to check for its write status
228 *
229 * This shouldn't be used directly ouside of the VFS.
230 * It does not guarantee that the filesystem will stay
231 * r/w, just that it is right *now*. This can not and
232 * should not be used in place of IS_RDONLY(inode).
233 * mnt_want/drop_write() will _keep_ the filesystem
234 * r/w.
235 */
236int __mnt_is_readonly(struct vfsmount *mnt)
237{
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800238 if (mnt->mnt_flags & MNT_READONLY)
239 return 1;
240 if (mnt->mnt_sb->s_flags & MS_RDONLY)
241 return 1;
242 return 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800243}
244EXPORT_SYMBOL_GPL(__mnt_is_readonly);
245
Al Viro83adc752011-11-24 22:37:54 -0500246static inline void mnt_inc_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800247{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000248#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500249 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000250#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500251 mnt->mnt_writers++;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000252#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800253}
Dave Hansen3d733632008-02-15 14:37:59 -0800254
Al Viro83adc752011-11-24 22:37:54 -0500255static inline void mnt_dec_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800256{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000257#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500258 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000259#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500260 mnt->mnt_writers--;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000261#endif
262}
263
Al Viro83adc752011-11-24 22:37:54 -0500264static unsigned int mnt_get_writers(struct mount *mnt)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000265{
266#ifdef CONFIG_SMP
267 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800268 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800269
270 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500271 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
Dave Hansen3d733632008-02-15 14:37:59 -0800272 }
Dave Hansen3d733632008-02-15 14:37:59 -0800273
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000274 return count;
275#else
276 return mnt->mnt_writers;
277#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800278}
279
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100280static int mnt_is_readonly(struct vfsmount *mnt)
281{
282 if (mnt->mnt_sb->s_readonly_remount)
283 return 1;
284 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
285 smp_rmb();
286 return __mnt_is_readonly(mnt);
287}
288
Dave Hansen3d733632008-02-15 14:37:59 -0800289/*
Jan Karaeb04c282012-06-12 16:20:35 +0200290 * Most r/o & frozen checks on a fs are for operations that take discrete
291 * amounts of time, like a write() or unlink(). We must keep track of when
292 * those operations start (for permission checks) and when they end, so that we
293 * can determine when writes are able to occur to a filesystem.
Dave Hansen3d733632008-02-15 14:37:59 -0800294 */
Dave Hansen83660252008-02-15 14:37:30 -0800295/**
Jan Karaeb04c282012-06-12 16:20:35 +0200296 * __mnt_want_write - get write access to a mount without freeze protection
Al Viro83adc752011-11-24 22:37:54 -0500297 * @m: the mount on which to take a write
Dave Hansen83660252008-02-15 14:37:30 -0800298 *
Jan Karaeb04c282012-06-12 16:20:35 +0200299 * This tells the low-level filesystem that a write is about to be performed to
300 * it, and makes sure that writes are allowed (mnt it read-write) before
301 * returning success. This operation does not protect against filesystem being
302 * frozen. When the write operation is finished, __mnt_drop_write() must be
303 * called. This is effectively a refcount.
Dave Hansen83660252008-02-15 14:37:30 -0800304 */
Jan Karaeb04c282012-06-12 16:20:35 +0200305int __mnt_want_write(struct vfsmount *m)
Dave Hansen83660252008-02-15 14:37:30 -0800306{
Al Viro83adc752011-11-24 22:37:54 -0500307 struct mount *mnt = real_mount(m);
Dave Hansen3d733632008-02-15 14:37:59 -0800308 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800309
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000310 preempt_disable();
Nick Pigginc6653a82011-01-07 17:50:10 +1100311 mnt_inc_writers(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000312 /*
Nick Pigginc6653a82011-01-07 17:50:10 +1100313 * The store to mnt_inc_writers must be visible before we pass
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000314 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
315 * incremented count after it has set MNT_WRITE_HOLD.
316 */
317 smp_mb();
Miao Xie1e755292012-11-16 17:23:50 +0800318 while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000319 cpu_relax();
320 /*
321 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
322 * be set to match its requirements. So we must not load that until
323 * MNT_WRITE_HOLD is cleared.
324 */
325 smp_rmb();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100326 if (mnt_is_readonly(m)) {
Nick Pigginc6653a82011-01-07 17:50:10 +1100327 mnt_dec_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800328 ret = -EROFS;
Dave Hansen3d733632008-02-15 14:37:59 -0800329 }
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000330 preempt_enable();
Jan Karaeb04c282012-06-12 16:20:35 +0200331
332 return ret;
333}
334
335/**
336 * mnt_want_write - get write access to a mount
337 * @m: the mount on which to take a write
338 *
339 * This tells the low-level filesystem that a write is about to be performed to
340 * it, and makes sure that writes are allowed (mount is read-write, filesystem
341 * is not frozen) before returning success. When the write operation is
342 * finished, mnt_drop_write() must be called. This is effectively a refcount.
343 */
344int mnt_want_write(struct vfsmount *m)
345{
346 int ret;
347
348 sb_start_write(m->mnt_sb);
349 ret = __mnt_want_write(m);
350 if (ret)
351 sb_end_write(m->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800352 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800353}
354EXPORT_SYMBOL_GPL(mnt_want_write);
355
356/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000357 * mnt_clone_write - get write access to a mount
358 * @mnt: the mount on which to take a write
359 *
360 * This is effectively like mnt_want_write, except
361 * it must only be used to take an extra write reference
362 * on a mountpoint that we already know has a write reference
363 * on it. This allows some optimisation.
364 *
365 * After finished, mnt_drop_write must be called as usual to
366 * drop the reference.
367 */
368int mnt_clone_write(struct vfsmount *mnt)
369{
370 /* superblock may be r/o */
371 if (__mnt_is_readonly(mnt))
372 return -EROFS;
373 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500374 mnt_inc_writers(real_mount(mnt));
npiggin@suse.de96029c42009-04-26 20:25:55 +1000375 preempt_enable();
376 return 0;
377}
378EXPORT_SYMBOL_GPL(mnt_clone_write);
379
380/**
Jan Karaeb04c282012-06-12 16:20:35 +0200381 * __mnt_want_write_file - get write access to a file's mount
382 * @file: the file who's mount on which to take a write
383 *
384 * This is like __mnt_want_write, but it takes a file and can
385 * do some optimisations if the file is open for write already
386 */
387int __mnt_want_write_file(struct file *file)
388{
Al Viro496ad9a2013-01-23 17:07:38 -0500389 struct inode *inode = file_inode(file);
Jan Karaeb04c282012-06-12 16:20:35 +0200390
391 if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
392 return __mnt_want_write(file->f_path.mnt);
393 else
394 return mnt_clone_write(file->f_path.mnt);
395}
396
397/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000398 * mnt_want_write_file - get write access to a file's mount
399 * @file: the file who's mount on which to take a write
400 *
401 * This is like mnt_want_write, but it takes a file and can
402 * do some optimisations if the file is open for write already
403 */
404int mnt_want_write_file(struct file *file)
405{
Jan Karaeb04c282012-06-12 16:20:35 +0200406 int ret;
407
408 sb_start_write(file->f_path.mnt->mnt_sb);
409 ret = __mnt_want_write_file(file);
410 if (ret)
411 sb_end_write(file->f_path.mnt->mnt_sb);
412 return ret;
npiggin@suse.de96029c42009-04-26 20:25:55 +1000413}
414EXPORT_SYMBOL_GPL(mnt_want_write_file);
415
416/**
Jan Karaeb04c282012-06-12 16:20:35 +0200417 * __mnt_drop_write - give up write access to a mount
Dave Hansen83660252008-02-15 14:37:30 -0800418 * @mnt: the mount on which to give up write access
419 *
420 * Tells the low-level filesystem that we are done
421 * performing writes to it. Must be matched with
Jan Karaeb04c282012-06-12 16:20:35 +0200422 * __mnt_want_write() call above.
Dave Hansen83660252008-02-15 14:37:30 -0800423 */
Jan Karaeb04c282012-06-12 16:20:35 +0200424void __mnt_drop_write(struct vfsmount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800425{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000426 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500427 mnt_dec_writers(real_mount(mnt));
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000428 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800429}
Jan Karaeb04c282012-06-12 16:20:35 +0200430
431/**
432 * mnt_drop_write - give up write access to a mount
433 * @mnt: the mount on which to give up write access
434 *
435 * Tells the low-level filesystem that we are done performing writes to it and
436 * also allows filesystem to be frozen again. Must be matched with
437 * mnt_want_write() call above.
438 */
439void mnt_drop_write(struct vfsmount *mnt)
440{
441 __mnt_drop_write(mnt);
442 sb_end_write(mnt->mnt_sb);
443}
Dave Hansen83660252008-02-15 14:37:30 -0800444EXPORT_SYMBOL_GPL(mnt_drop_write);
445
Jan Karaeb04c282012-06-12 16:20:35 +0200446void __mnt_drop_write_file(struct file *file)
447{
448 __mnt_drop_write(file->f_path.mnt);
449}
450
Al Viro2a79f172011-12-09 08:06:57 -0500451void mnt_drop_write_file(struct file *file)
452{
453 mnt_drop_write(file->f_path.mnt);
454}
455EXPORT_SYMBOL(mnt_drop_write_file);
456
Al Viro83adc752011-11-24 22:37:54 -0500457static int mnt_make_readonly(struct mount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800458{
Dave Hansen3d733632008-02-15 14:37:59 -0800459 int ret = 0;
460
Andi Kleen962830d2012-05-08 13:32:02 +0930461 br_write_lock(&vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -0500462 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000463 /*
464 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
465 * should be visible before we do.
466 */
467 smp_mb();
468
469 /*
470 * With writers on hold, if this value is zero, then there are
471 * definitely no active writers (although held writers may subsequently
472 * increment the count, they'll have to wait, and decrement it after
473 * seeing MNT_READONLY).
474 *
475 * It is OK to have counter incremented on one CPU and decremented on
476 * another: the sum will add up correctly. The danger would be when we
477 * sum up each counter, if we read a counter before it is incremented,
478 * but then read another CPU's count which it has been subsequently
479 * decremented from -- we would see more decrements than we should.
480 * MNT_WRITE_HOLD protects against this scenario, because
481 * mnt_want_write first increments count, then smp_mb, then spins on
482 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
483 * we're counting up here.
484 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100485 if (mnt_get_writers(mnt) > 0)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000486 ret = -EBUSY;
487 else
Al Viro83adc752011-11-24 22:37:54 -0500488 mnt->mnt.mnt_flags |= MNT_READONLY;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000489 /*
490 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
491 * that become unheld will see MNT_READONLY.
492 */
493 smp_wmb();
Al Viro83adc752011-11-24 22:37:54 -0500494 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
Andi Kleen962830d2012-05-08 13:32:02 +0930495 br_write_unlock(&vfsmount_lock);
Dave Hansen3d733632008-02-15 14:37:59 -0800496 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800497}
Dave Hansen83660252008-02-15 14:37:30 -0800498
Al Viro83adc752011-11-24 22:37:54 -0500499static void __mnt_unmake_readonly(struct mount *mnt)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800500{
Andi Kleen962830d2012-05-08 13:32:02 +0930501 br_write_lock(&vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -0500502 mnt->mnt.mnt_flags &= ~MNT_READONLY;
Andi Kleen962830d2012-05-08 13:32:02 +0930503 br_write_unlock(&vfsmount_lock);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800504}
505
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100506int sb_prepare_remount_readonly(struct super_block *sb)
507{
508 struct mount *mnt;
509 int err = 0;
510
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100511 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
512 if (atomic_long_read(&sb->s_remove_count))
513 return -EBUSY;
514
Andi Kleen962830d2012-05-08 13:32:02 +0930515 br_write_lock(&vfsmount_lock);
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100516 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
517 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
518 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
519 smp_mb();
520 if (mnt_get_writers(mnt) > 0) {
521 err = -EBUSY;
522 break;
523 }
524 }
525 }
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100526 if (!err && atomic_long_read(&sb->s_remove_count))
527 err = -EBUSY;
528
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100529 if (!err) {
530 sb->s_readonly_remount = 1;
531 smp_wmb();
532 }
533 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
534 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
535 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
536 }
Andi Kleen962830d2012-05-08 13:32:02 +0930537 br_write_unlock(&vfsmount_lock);
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100538
539 return err;
540}
541
Al Virob105e272011-11-24 20:38:33 -0500542static void free_vfsmnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Al Viro52ba1622011-11-25 02:25:17 -0500544 kfree(mnt->mnt_devname);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100545 mnt_free_id(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000546#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500547 free_percpu(mnt->mnt_pcp);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000548#endif
Al Virob105e272011-11-24 20:38:33 -0500549 kmem_cache_free(mnt_cache, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/*
Ram Paia05964f2005-11-07 17:20:17 -0500553 * find the first or last mount at @dentry on vfsmount @mnt depending on
554 * @dir. If @dir is set return the first mount else return the last mount.
Nick Piggin99b7db72010-08-18 04:37:39 +1000555 * vfsmount_lock must be held for read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Al Viroc7105362011-11-24 18:22:03 -0500557struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
Ram Paia05964f2005-11-07 17:20:17 -0500558 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Ram Paib58fed82005-11-07 17:16:09 -0500560 struct list_head *head = mount_hashtable + hash(mnt, dentry);
561 struct list_head *tmp = head;
Al Viroc7105362011-11-24 18:22:03 -0500562 struct mount *p, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 for (;;) {
Ram Paia05964f2005-11-07 17:20:17 -0500565 tmp = dir ? tmp->next : tmp->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 p = NULL;
567 if (tmp == head)
568 break;
Al Viro1b8e5562011-11-24 21:01:32 -0500569 p = list_entry(tmp, struct mount, mnt_hash);
Al Viroa73324d2011-11-24 22:25:07 -0500570 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
Ram Paia05964f2005-11-07 17:20:17 -0500571 found = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573 }
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return found;
576}
577
Ram Paia05964f2005-11-07 17:20:17 -0500578/*
David Howellsf015f1262012-06-25 12:55:28 +0100579 * lookup_mnt - Return the first child mount mounted at path
580 *
581 * "First" means first mounted chronologically. If you create the
582 * following mounts:
583 *
584 * mount /dev/sda1 /mnt
585 * mount /dev/sda2 /mnt
586 * mount /dev/sda3 /mnt
587 *
588 * Then lookup_mnt() on the base /mnt dentry in the root mount will
589 * return successively the root dentry and vfsmount of /dev/sda1, then
590 * /dev/sda2, then /dev/sda3, then NULL.
591 *
592 * lookup_mnt takes a reference to the found vfsmount.
Ram Paia05964f2005-11-07 17:20:17 -0500593 */
Al Viro1c755af2009-04-18 14:06:57 -0400594struct vfsmount *lookup_mnt(struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500595{
Al Viroc7105362011-11-24 18:22:03 -0500596 struct mount *child_mnt;
Nick Piggin99b7db72010-08-18 04:37:39 +1000597
Andi Kleen962830d2012-05-08 13:32:02 +0930598 br_read_lock(&vfsmount_lock);
Al Viroc7105362011-11-24 18:22:03 -0500599 child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
600 if (child_mnt) {
601 mnt_add_count(child_mnt, 1);
Andi Kleen962830d2012-05-08 13:32:02 +0930602 br_read_unlock(&vfsmount_lock);
Al Viroc7105362011-11-24 18:22:03 -0500603 return &child_mnt->mnt;
604 } else {
Andi Kleen962830d2012-05-08 13:32:02 +0930605 br_read_unlock(&vfsmount_lock);
Al Viroc7105362011-11-24 18:22:03 -0500606 return NULL;
607 }
Ram Paia05964f2005-11-07 17:20:17 -0500608}
609
Al Viro84d17192013-03-15 10:53:28 -0400610static struct mountpoint *new_mountpoint(struct dentry *dentry)
611{
612 struct list_head *chain = mountpoint_hashtable + hash(NULL, dentry);
613 struct mountpoint *mp;
614
615 list_for_each_entry(mp, chain, m_hash) {
616 if (mp->m_dentry == dentry) {
617 /* might be worth a WARN_ON() */
618 if (d_unlinked(dentry))
619 return ERR_PTR(-ENOENT);
620 mp->m_count++;
621 return mp;
622 }
623 }
624
625 mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
626 if (!mp)
627 return ERR_PTR(-ENOMEM);
628
629 spin_lock(&dentry->d_lock);
630 if (d_unlinked(dentry)) {
631 spin_unlock(&dentry->d_lock);
632 kfree(mp);
633 return ERR_PTR(-ENOENT);
634 }
635 dentry->d_flags |= DCACHE_MOUNTED;
636 spin_unlock(&dentry->d_lock);
637 mp->m_dentry = dentry;
638 mp->m_count = 1;
639 list_add(&mp->m_hash, chain);
640 return mp;
641}
642
643static void put_mountpoint(struct mountpoint *mp)
644{
645 if (!--mp->m_count) {
646 struct dentry *dentry = mp->m_dentry;
647 spin_lock(&dentry->d_lock);
648 dentry->d_flags &= ~DCACHE_MOUNTED;
649 spin_unlock(&dentry->d_lock);
650 list_del(&mp->m_hash);
651 kfree(mp);
652 }
653}
654
Al Viro143c8c92011-11-25 00:46:35 -0500655static inline int check_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800657 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Nick Piggin99b7db72010-08-18 04:37:39 +1000660/*
661 * vfsmount lock must be held for write
662 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800663static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500664{
665 if (ns) {
666 ns->event = ++event;
667 wake_up_interruptible(&ns->poll);
668 }
669}
670
Nick Piggin99b7db72010-08-18 04:37:39 +1000671/*
672 * vfsmount lock must be held for write
673 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800674static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500675{
676 if (ns && ns->event != event) {
677 ns->event = event;
678 wake_up_interruptible(&ns->poll);
679 }
680}
681
Nick Piggin99b7db72010-08-18 04:37:39 +1000682/*
683 * vfsmount lock must be held for write
684 */
Al Viro419148d2011-11-24 19:41:16 -0500685static void detach_mnt(struct mount *mnt, struct path *old_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Al Viroa73324d2011-11-24 22:25:07 -0500687 old_path->dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500688 old_path->mnt = &mnt->mnt_parent->mnt;
689 mnt->mnt_parent = mnt;
Al Viroa73324d2011-11-24 22:25:07 -0500690 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro6b41d532011-11-24 23:24:33 -0500691 list_del_init(&mnt->mnt_child);
Al Viro1b8e5562011-11-24 21:01:32 -0500692 list_del_init(&mnt->mnt_hash);
Al Viro84d17192013-03-15 10:53:28 -0400693 put_mountpoint(mnt->mnt_mp);
694 mnt->mnt_mp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Nick Piggin99b7db72010-08-18 04:37:39 +1000697/*
698 * vfsmount lock must be held for write
699 */
Al Viro84d17192013-03-15 10:53:28 -0400700void mnt_set_mountpoint(struct mount *mnt,
701 struct mountpoint *mp,
Al Viro44d964d62011-11-24 21:28:22 -0500702 struct mount *child_mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500703{
Al Viro84d17192013-03-15 10:53:28 -0400704 mp->m_count++;
Al Viro3a2393d2011-11-25 03:19:09 -0500705 mnt_add_count(mnt, 1); /* essentially, that's mntget */
Al Viro84d17192013-03-15 10:53:28 -0400706 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
Al Viro3a2393d2011-11-25 03:19:09 -0500707 child_mnt->mnt_parent = mnt;
Al Viro84d17192013-03-15 10:53:28 -0400708 child_mnt->mnt_mp = mp;
Ram Paib90fa9a2005-11-07 17:19:50 -0500709}
710
Nick Piggin99b7db72010-08-18 04:37:39 +1000711/*
712 * vfsmount lock must be held for write
713 */
Al Viro84d17192013-03-15 10:53:28 -0400714static void attach_mnt(struct mount *mnt,
715 struct mount *parent,
716 struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Al Viro84d17192013-03-15 10:53:28 -0400718 mnt_set_mountpoint(parent, mp, mnt);
Al Viro1b8e5562011-11-24 21:01:32 -0500719 list_add_tail(&mnt->mnt_hash, mount_hashtable +
Al Viro84d17192013-03-15 10:53:28 -0400720 hash(&parent->mnt, mp->m_dentry));
721 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
Ram Paib90fa9a2005-11-07 17:19:50 -0500722}
723
724/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000725 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500726 */
Al Viro4b2619a2011-11-24 19:47:15 -0500727static void commit_tree(struct mount *mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500728{
Al Viro0714a532011-11-24 22:19:58 -0500729 struct mount *parent = mnt->mnt_parent;
Al Viro83adc752011-11-24 22:37:54 -0500730 struct mount *m;
Ram Paib90fa9a2005-11-07 17:19:50 -0500731 LIST_HEAD(head);
Al Viro143c8c92011-11-25 00:46:35 -0500732 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500733
Al Viro0714a532011-11-24 22:19:58 -0500734 BUG_ON(parent == mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500735
Al Viro1a4eeaf2011-11-25 02:19:55 -0500736 list_add_tail(&head, &mnt->mnt_list);
Al Virof7a99c52012-06-09 00:59:08 -0400737 list_for_each_entry(m, &head, mnt_list)
Al Viro143c8c92011-11-25 00:46:35 -0500738 m->mnt_ns = n;
Al Virof03c6592011-01-14 22:30:21 -0500739
Ram Paib90fa9a2005-11-07 17:19:50 -0500740 list_splice(&head, n->list.prev);
741
Al Viro1b8e5562011-11-24 21:01:32 -0500742 list_add_tail(&mnt->mnt_hash, mount_hashtable +
Al Viroa73324d2011-11-24 22:25:07 -0500743 hash(&parent->mnt, mnt->mnt_mountpoint));
Al Viro6b41d532011-11-24 23:24:33 -0500744 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800745 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Al Viro909b0a82011-11-25 03:06:56 -0500748static struct mount *next_mnt(struct mount *p, struct mount *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Al Viro6b41d532011-11-24 23:24:33 -0500750 struct list_head *next = p->mnt_mounts.next;
751 if (next == &p->mnt_mounts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 while (1) {
Al Viro909b0a82011-11-25 03:06:56 -0500753 if (p == root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return NULL;
Al Viro6b41d532011-11-24 23:24:33 -0500755 next = p->mnt_child.next;
756 if (next != &p->mnt_parent->mnt_mounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
Al Viro0714a532011-11-24 22:19:58 -0500758 p = p->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 }
Al Viro6b41d532011-11-24 23:24:33 -0500761 return list_entry(next, struct mount, mnt_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Al Viro315fc832011-11-24 18:57:30 -0500764static struct mount *skip_mnt_tree(struct mount *p)
Ram Pai9676f0c2005-11-07 17:21:20 -0500765{
Al Viro6b41d532011-11-24 23:24:33 -0500766 struct list_head *prev = p->mnt_mounts.prev;
767 while (prev != &p->mnt_mounts) {
768 p = list_entry(prev, struct mount, mnt_child);
769 prev = p->mnt_mounts.prev;
Ram Pai9676f0c2005-11-07 17:21:20 -0500770 }
771 return p;
772}
773
Al Viro9d412a42011-03-17 22:08:28 -0400774struct vfsmount *
775vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
776{
Al Virob105e272011-11-24 20:38:33 -0500777 struct mount *mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400778 struct dentry *root;
779
780 if (!type)
781 return ERR_PTR(-ENODEV);
782
783 mnt = alloc_vfsmnt(name);
784 if (!mnt)
785 return ERR_PTR(-ENOMEM);
786
787 if (flags & MS_KERNMOUNT)
Al Virob105e272011-11-24 20:38:33 -0500788 mnt->mnt.mnt_flags = MNT_INTERNAL;
Al Viro9d412a42011-03-17 22:08:28 -0400789
790 root = mount_fs(type, flags, name, data);
791 if (IS_ERR(root)) {
792 free_vfsmnt(mnt);
793 return ERR_CAST(root);
794 }
795
Al Virob105e272011-11-24 20:38:33 -0500796 mnt->mnt.mnt_root = root;
797 mnt->mnt.mnt_sb = root->d_sb;
Al Viroa73324d2011-11-24 22:25:07 -0500798 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -0500799 mnt->mnt_parent = mnt;
Andi Kleen962830d2012-05-08 13:32:02 +0930800 br_write_lock(&vfsmount_lock);
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100801 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
Andi Kleen962830d2012-05-08 13:32:02 +0930802 br_write_unlock(&vfsmount_lock);
Al Virob105e272011-11-24 20:38:33 -0500803 return &mnt->mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400804}
805EXPORT_SYMBOL_GPL(vfs_kern_mount);
806
Al Viro87129cc2011-11-24 21:24:27 -0500807static struct mount *clone_mnt(struct mount *old, struct dentry *root,
Ram Pai36341f62005-11-07 17:17:22 -0500808 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Al Viro87129cc2011-11-24 21:24:27 -0500810 struct super_block *sb = old->mnt.mnt_sb;
David Howellsbe34d1a2012-06-25 12:55:18 +0100811 struct mount *mnt;
812 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
David Howellsbe34d1a2012-06-25 12:55:18 +0100814 mnt = alloc_vfsmnt(old->mnt_devname);
815 if (!mnt)
816 return ERR_PTR(-ENOMEM);
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100817
Eric W. Biederman7a472ef2012-07-31 13:13:04 -0700818 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
David Howellsbe34d1a2012-06-25 12:55:18 +0100819 mnt->mnt_group_id = 0; /* not a peer of original */
820 else
821 mnt->mnt_group_id = old->mnt_group_id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100822
David Howellsbe34d1a2012-06-25 12:55:18 +0100823 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
824 err = mnt_alloc_group_id(mnt);
825 if (err)
826 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
David Howellsbe34d1a2012-06-25 12:55:18 +0100828
829 mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
Eric W. Biederman132c94e2013-03-22 04:08:05 -0700830 /* Don't allow unprivileged users to change mount flags */
831 if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY))
832 mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
833
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -0700834 /* Don't allow unprivileged users to reveal what is under a mount */
835 if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire))
836 mnt->mnt.mnt_flags |= MNT_LOCKED;
837
David Howellsbe34d1a2012-06-25 12:55:18 +0100838 atomic_inc(&sb->s_active);
839 mnt->mnt.mnt_sb = sb;
840 mnt->mnt.mnt_root = dget(root);
841 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
842 mnt->mnt_parent = mnt;
843 br_write_lock(&vfsmount_lock);
844 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
845 br_write_unlock(&vfsmount_lock);
846
Eric W. Biederman7a472ef2012-07-31 13:13:04 -0700847 if ((flag & CL_SLAVE) ||
848 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
David Howellsbe34d1a2012-06-25 12:55:18 +0100849 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
850 mnt->mnt_master = old;
851 CLEAR_MNT_SHARED(mnt);
852 } else if (!(flag & CL_PRIVATE)) {
853 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
854 list_add(&mnt->mnt_share, &old->mnt_share);
855 if (IS_MNT_SLAVE(old))
856 list_add(&mnt->mnt_slave, &old->mnt_slave);
857 mnt->mnt_master = old->mnt_master;
858 }
859 if (flag & CL_MAKE_SHARED)
860 set_mnt_shared(mnt);
861
862 /* stick the duplicate mount on the same expiry list
863 * as the original if that was on one */
864 if (flag & CL_EXPIRE) {
865 if (!list_empty(&old->mnt_expire))
866 list_add(&mnt->mnt_expire, &old->mnt_expire);
867 }
868
Al Virocb338d02011-11-24 20:55:08 -0500869 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100870
871 out_free:
872 free_vfsmnt(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +0100873 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Al Viro83adc752011-11-24 22:37:54 -0500876static inline void mntfree(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Al Viro83adc752011-11-24 22:37:54 -0500878 struct vfsmount *m = &mnt->mnt;
879 struct super_block *sb = m->mnt_sb;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100880
Dave Hansen3d733632008-02-15 14:37:59 -0800881 /*
Dave Hansen3d733632008-02-15 14:37:59 -0800882 * This probably indicates that somebody messed
883 * up a mnt_want/drop_write() pair. If this
884 * happens, the filesystem was probably unable
885 * to make r/w->r/o transitions.
886 */
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000887 /*
Nick Pigginb3e19d92011-01-07 17:50:11 +1100888 * The locking used to deal with mnt_count decrement provides barriers,
889 * so mnt_get_writers() below is safe.
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000890 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100891 WARN_ON(mnt_get_writers(mnt));
Al Viro83adc752011-11-24 22:37:54 -0500892 fsnotify_vfsmount_delete(m);
893 dput(m->mnt_root);
894 free_vfsmnt(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 deactivate_super(sb);
896}
897
Al Viro900148d2011-11-25 00:33:11 -0500898static void mntput_no_expire(struct mount *mnt)
Al Viro7b7b1ac2005-11-07 17:13:39 -0500899{
Nick Pigginb3e19d92011-01-07 17:50:11 +1100900put_again:
Al Virof03c6592011-01-14 22:30:21 -0500901#ifdef CONFIG_SMP
Andi Kleen962830d2012-05-08 13:32:02 +0930902 br_read_lock(&vfsmount_lock);
Al Virof7a99c52012-06-09 00:59:08 -0400903 if (likely(mnt->mnt_ns)) {
904 /* shouldn't be the last one */
Al Viroaa9c0e02011-11-23 19:04:52 -0500905 mnt_add_count(mnt, -1);
Andi Kleen962830d2012-05-08 13:32:02 +0930906 br_read_unlock(&vfsmount_lock);
Al Virof03c6592011-01-14 22:30:21 -0500907 return;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100908 }
Andi Kleen962830d2012-05-08 13:32:02 +0930909 br_read_unlock(&vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100910
Andi Kleen962830d2012-05-08 13:32:02 +0930911 br_write_lock(&vfsmount_lock);
Al Viroaa9c0e02011-11-23 19:04:52 -0500912 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100913 if (mnt_get_count(mnt)) {
Andi Kleen962830d2012-05-08 13:32:02 +0930914 br_write_unlock(&vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100915 return;
916 }
Nick Pigginb3e19d92011-01-07 17:50:11 +1100917#else
Al Viroaa9c0e02011-11-23 19:04:52 -0500918 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100919 if (likely(mnt_get_count(mnt)))
Nick Piggin99b7db72010-08-18 04:37:39 +1000920 return;
Andi Kleen962830d2012-05-08 13:32:02 +0930921 br_write_lock(&vfsmount_lock);
Al Virof03c6592011-01-14 22:30:21 -0500922#endif
Al Viro863d6842011-11-25 00:57:42 -0500923 if (unlikely(mnt->mnt_pinned)) {
924 mnt_add_count(mnt, mnt->mnt_pinned + 1);
925 mnt->mnt_pinned = 0;
Andi Kleen962830d2012-05-08 13:32:02 +0930926 br_write_unlock(&vfsmount_lock);
Al Viro900148d2011-11-25 00:33:11 -0500927 acct_auto_close_mnt(&mnt->mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100928 goto put_again;
Al Viro7b7b1ac2005-11-07 17:13:39 -0500929 }
Andi Kleen962830d2012-05-08 13:32:02 +0930930
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100931 list_del(&mnt->mnt_instance);
Andi Kleen962830d2012-05-08 13:32:02 +0930932 br_write_unlock(&vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100933 mntfree(mnt);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500934}
Nick Pigginb3e19d92011-01-07 17:50:11 +1100935
936void mntput(struct vfsmount *mnt)
937{
938 if (mnt) {
Al Viro863d6842011-11-25 00:57:42 -0500939 struct mount *m = real_mount(mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100940 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
Al Viro863d6842011-11-25 00:57:42 -0500941 if (unlikely(m->mnt_expiry_mark))
942 m->mnt_expiry_mark = 0;
943 mntput_no_expire(m);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100944 }
945}
946EXPORT_SYMBOL(mntput);
947
948struct vfsmount *mntget(struct vfsmount *mnt)
949{
950 if (mnt)
Al Viro83adc752011-11-24 22:37:54 -0500951 mnt_add_count(real_mount(mnt), 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100952 return mnt;
953}
954EXPORT_SYMBOL(mntget);
955
Al Viro7b7b1ac2005-11-07 17:13:39 -0500956void mnt_pin(struct vfsmount *mnt)
957{
Andi Kleen962830d2012-05-08 13:32:02 +0930958 br_write_lock(&vfsmount_lock);
Al Viro863d6842011-11-25 00:57:42 -0500959 real_mount(mnt)->mnt_pinned++;
Andi Kleen962830d2012-05-08 13:32:02 +0930960 br_write_unlock(&vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500961}
Al Viro7b7b1ac2005-11-07 17:13:39 -0500962EXPORT_SYMBOL(mnt_pin);
963
Al Viro863d6842011-11-25 00:57:42 -0500964void mnt_unpin(struct vfsmount *m)
Al Viro7b7b1ac2005-11-07 17:13:39 -0500965{
Al Viro863d6842011-11-25 00:57:42 -0500966 struct mount *mnt = real_mount(m);
Andi Kleen962830d2012-05-08 13:32:02 +0930967 br_write_lock(&vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500968 if (mnt->mnt_pinned) {
Al Viro863d6842011-11-25 00:57:42 -0500969 mnt_add_count(mnt, 1);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500970 mnt->mnt_pinned--;
971 }
Andi Kleen962830d2012-05-08 13:32:02 +0930972 br_write_unlock(&vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500973}
Al Viro7b7b1ac2005-11-07 17:13:39 -0500974EXPORT_SYMBOL(mnt_unpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800976static inline void mangle(struct seq_file *m, const char *s)
977{
978 seq_escape(m, s, " \t\n\\");
979}
980
981/*
982 * Simple .show_options callback for filesystems which don't want to
983 * implement more complex mount option showing.
984 *
985 * See also save_mount_options().
986 */
Al Viro34c80b12011-12-08 21:32:45 -0500987int generic_show_options(struct seq_file *m, struct dentry *root)
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800988{
Al Viro2a32ceb2009-05-08 16:05:57 -0400989 const char *options;
990
991 rcu_read_lock();
Al Viro34c80b12011-12-08 21:32:45 -0500992 options = rcu_dereference(root->d_sb->s_options);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800993
994 if (options != NULL && options[0]) {
995 seq_putc(m, ',');
996 mangle(m, options);
997 }
Al Viro2a32ceb2009-05-08 16:05:57 -0400998 rcu_read_unlock();
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800999
1000 return 0;
1001}
1002EXPORT_SYMBOL(generic_show_options);
1003
1004/*
1005 * If filesystem uses generic_show_options(), this function should be
1006 * called from the fill_super() callback.
1007 *
1008 * The .remount_fs callback usually needs to be handled in a special
1009 * way, to make sure, that previous options are not overwritten if the
1010 * remount fails.
1011 *
1012 * Also note, that if the filesystem's .remount_fs function doesn't
1013 * reset all options to their default value, but changes only newly
1014 * given options, then the displayed options will not reflect reality
1015 * any more.
1016 */
1017void save_mount_options(struct super_block *sb, char *options)
1018{
Al Viro2a32ceb2009-05-08 16:05:57 -04001019 BUG_ON(sb->s_options);
1020 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001021}
1022EXPORT_SYMBOL(save_mount_options);
1023
Al Viro2a32ceb2009-05-08 16:05:57 -04001024void replace_mount_options(struct super_block *sb, char *options)
1025{
1026 char *old = sb->s_options;
1027 rcu_assign_pointer(sb->s_options, options);
1028 if (old) {
1029 synchronize_rcu();
1030 kfree(old);
1031 }
1032}
1033EXPORT_SYMBOL(replace_mount_options);
1034
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001035#ifdef CONFIG_PROC_FS
Al Viro0226f492011-12-06 12:21:54 -05001036/* iterator; we want it to have access to namespace_sem, thus here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static void *m_start(struct seq_file *m, loff_t *pos)
1038{
Al Viro6ce6e242012-06-09 01:16:59 -04001039 struct proc_mounts *p = proc_mounts(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Ram Pai390c6842005-11-07 17:17:51 -05001041 down_read(&namespace_sem);
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001042 return seq_list_start(&p->ns->list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1046{
Al Viro6ce6e242012-06-09 01:16:59 -04001047 struct proc_mounts *p = proc_mounts(m);
Pavel Emelianovb0765fb2007-07-15 23:39:55 -07001048
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001049 return seq_list_next(v, &p->ns->list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052static void m_stop(struct seq_file *m, void *v)
1053{
Ram Pai390c6842005-11-07 17:17:51 -05001054 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Al Viro0226f492011-12-06 12:21:54 -05001057static int m_show(struct seq_file *m, void *v)
Al Viro9f5596a2010-02-05 00:40:25 -05001058{
Al Viro6ce6e242012-06-09 01:16:59 -04001059 struct proc_mounts *p = proc_mounts(m);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001060 struct mount *r = list_entry(v, struct mount, mnt_list);
Al Viro0226f492011-12-06 12:21:54 -05001061 return p->show(m, &r->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001064const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 .start = m_start,
1066 .next = m_next,
1067 .stop = m_stop,
Al Viro0226f492011-12-06 12:21:54 -05001068 .show = m_show,
Chuck Leverb4629fe2006-03-20 13:44:12 -05001069};
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001070#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -05001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/**
1073 * may_umount_tree - check if a mount tree is busy
1074 * @mnt: root of mount tree
1075 *
1076 * This is called to check if a tree of mounts has any
1077 * open files, pwds, chroots or sub mounts that are
1078 * busy.
1079 */
Al Viro909b0a82011-11-25 03:06:56 -05001080int may_umount_tree(struct vfsmount *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Al Viro909b0a82011-11-25 03:06:56 -05001082 struct mount *mnt = real_mount(m);
Ram Pai36341f62005-11-07 17:17:22 -05001083 int actual_refs = 0;
1084 int minimum_refs = 0;
Al Viro315fc832011-11-24 18:57:30 -05001085 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001086 BUG_ON(!m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Nick Pigginb3e19d92011-01-07 17:50:11 +11001088 /* write lock needed for mnt_get_count */
Andi Kleen962830d2012-05-08 13:32:02 +09301089 br_write_lock(&vfsmount_lock);
Al Viro909b0a82011-11-25 03:06:56 -05001090 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Viro83adc752011-11-24 22:37:54 -05001091 actual_refs += mnt_get_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Andi Kleen962830d2012-05-08 13:32:02 +09301094 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -08001097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Ian Kente3474a82006-03-27 01:14:51 -08001099 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102EXPORT_SYMBOL(may_umount_tree);
1103
1104/**
1105 * may_umount - check if a mount point is busy
1106 * @mnt: root of mount
1107 *
1108 * This is called to check if a mount point has any
1109 * open files, pwds, chroots or sub mounts. If the
1110 * mount has sub mounts this will return busy
1111 * regardless of whether the sub mounts are busy.
1112 *
1113 * Doesn't take quota and stuff into account. IOW, in some cases it will
1114 * give false negatives. The main reason why it's here is that we need
1115 * a non-destructive way to look for easily umountable filesystems.
1116 */
1117int may_umount(struct vfsmount *mnt)
1118{
Ian Kente3474a82006-03-27 01:14:51 -08001119 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001120 down_read(&namespace_sem);
Andi Kleen962830d2012-05-08 13:32:02 +09301121 br_write_lock(&vfsmount_lock);
Al Viro1ab59732011-11-24 21:35:16 -05001122 if (propagate_mount_busy(real_mount(mnt), 2))
Ian Kente3474a82006-03-27 01:14:51 -08001123 ret = 0;
Andi Kleen962830d2012-05-08 13:32:02 +09301124 br_write_unlock(&vfsmount_lock);
Al Viro8ad08d82010-01-16 12:56:08 -05001125 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129EXPORT_SYMBOL(may_umount);
1130
Al Viroe3197d82013-03-16 14:35:16 -04001131static LIST_HEAD(unmounted); /* protected by namespace_sem */
1132
Al Viro97216be2013-03-16 15:12:40 -04001133static void namespace_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Al Virod5e50f72011-11-24 20:58:57 -05001135 struct mount *mnt;
Al Viro97216be2013-03-16 15:12:40 -04001136 LIST_HEAD(head);
1137
1138 if (likely(list_empty(&unmounted))) {
1139 up_write(&namespace_sem);
1140 return;
1141 }
1142
1143 list_splice_init(&unmounted, &head);
1144 up_write(&namespace_sem);
1145
1146 while (!list_empty(&head)) {
1147 mnt = list_first_entry(&head, struct mount, mnt_hash);
Al Viro1b8e5562011-11-24 21:01:32 -05001148 list_del_init(&mnt->mnt_hash);
Al Viro676da582011-11-24 21:47:05 -05001149 if (mnt_has_parent(mnt)) {
Ram Pai70fbcdf2005-11-07 17:17:04 -05001150 struct dentry *dentry;
Al Viro863d6842011-11-25 00:57:42 -05001151 struct mount *m;
Nick Piggin99b7db72010-08-18 04:37:39 +10001152
Andi Kleen962830d2012-05-08 13:32:02 +09301153 br_write_lock(&vfsmount_lock);
Al Viroa73324d2011-11-24 22:25:07 -05001154 dentry = mnt->mnt_mountpoint;
Al Viro863d6842011-11-25 00:57:42 -05001155 m = mnt->mnt_parent;
Al Viroa73324d2011-11-24 22:25:07 -05001156 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -05001157 mnt->mnt_parent = mnt;
Al Viro7c4b93d2008-03-21 23:59:49 -04001158 m->mnt_ghosts--;
Andi Kleen962830d2012-05-08 13:32:02 +09301159 br_write_unlock(&vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001160 dput(dentry);
Al Viro863d6842011-11-25 00:57:42 -05001161 mntput(&m->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Al Virod5e50f72011-11-24 20:58:57 -05001163 mntput(&mnt->mnt);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001164 }
1165}
1166
Al Viro97216be2013-03-16 15:12:40 -04001167static inline void namespace_lock(void)
Al Viroe3197d82013-03-16 14:35:16 -04001168{
Al Viro97216be2013-03-16 15:12:40 -04001169 down_write(&namespace_sem);
Al Viroe3197d82013-03-16 14:35:16 -04001170}
1171
Nick Piggin99b7db72010-08-18 04:37:39 +10001172/*
1173 * vfsmount lock must be held for write
1174 * namespace_sem must be held for write
1175 */
Al Viro328e6d92013-03-16 14:49:45 -04001176void umount_tree(struct mount *mnt, int propagate)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001177{
Al Viro7b8a53f2011-01-15 20:08:44 -05001178 LIST_HEAD(tmp_list);
Al Viro315fc832011-11-24 18:57:30 -05001179 struct mount *p;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001180
Al Viro909b0a82011-11-25 03:06:56 -05001181 for (p = mnt; p; p = next_mnt(p, mnt))
Al Viro1b8e5562011-11-24 21:01:32 -05001182 list_move(&p->mnt_hash, &tmp_list);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001183
Ram Paia05964f2005-11-07 17:20:17 -05001184 if (propagate)
Al Viro7b8a53f2011-01-15 20:08:44 -05001185 propagate_umount(&tmp_list);
Ram Paia05964f2005-11-07 17:20:17 -05001186
Al Viro1b8e5562011-11-24 21:01:32 -05001187 list_for_each_entry(p, &tmp_list, mnt_hash) {
Al Viro6776db3d2011-11-25 00:22:05 -05001188 list_del_init(&p->mnt_expire);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001189 list_del_init(&p->mnt_list);
Al Viro143c8c92011-11-25 00:46:35 -05001190 __touch_mnt_namespace(p->mnt_ns);
1191 p->mnt_ns = NULL;
Al Viro6b41d532011-11-24 23:24:33 -05001192 list_del_init(&p->mnt_child);
Al Viro676da582011-11-24 21:47:05 -05001193 if (mnt_has_parent(p)) {
Al Viro863d6842011-11-25 00:57:42 -05001194 p->mnt_parent->mnt_ghosts++;
Al Viro84d17192013-03-15 10:53:28 -04001195 put_mountpoint(p->mnt_mp);
1196 p->mnt_mp = NULL;
Al Viro7c4b93d2008-03-21 23:59:49 -04001197 }
Al Viro0f0afb12011-11-24 20:43:10 -05001198 change_mnt_propagation(p, MS_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Al Viro328e6d92013-03-16 14:49:45 -04001200 list_splice(&tmp_list, &unmounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Al Virob54b9be2013-03-16 14:39:34 -04001203static void shrink_submounts(struct mount *mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001204
Al Viro1ab59732011-11-24 21:35:16 -05001205static int do_umount(struct mount *mnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Al Viro1ab59732011-11-24 21:35:16 -05001207 struct super_block *sb = mnt->mnt.mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int retval;
1209
Al Viro1ab59732011-11-24 21:35:16 -05001210 retval = security_sb_umount(&mnt->mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (retval)
1212 return retval;
1213
1214 /*
1215 * Allow userspace to request a mountpoint be expired rather than
1216 * unmounting unconditionally. Unmount only happens if:
1217 * (1) the mark is already set (the mark is cleared by mntput())
1218 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1219 */
1220 if (flags & MNT_EXPIRE) {
Al Viro1ab59732011-11-24 21:35:16 -05001221 if (&mnt->mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 flags & (MNT_FORCE | MNT_DETACH))
1223 return -EINVAL;
1224
Nick Pigginb3e19d92011-01-07 17:50:11 +11001225 /*
1226 * probably don't strictly need the lock here if we examined
1227 * all race cases, but it's a slowpath.
1228 */
Andi Kleen962830d2012-05-08 13:32:02 +09301229 br_write_lock(&vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -05001230 if (mnt_get_count(mnt) != 2) {
Andi Kleen962830d2012-05-08 13:32:02 +09301231 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EBUSY;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001233 }
Andi Kleen962830d2012-05-08 13:32:02 +09301234 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Al Viro863d6842011-11-25 00:57:42 -05001236 if (!xchg(&mnt->mnt_expiry_mark, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -EAGAIN;
1238 }
1239
1240 /*
1241 * If we may have to abort operations to get out of this
1242 * mount, and they will themselves hold resources we must
1243 * allow the fs to do things. In the Unix tradition of
1244 * 'Gee thats tricky lets do it in userspace' the umount_begin
1245 * might fail to complete on the first run through as other tasks
1246 * must return, and the like. Thats for the mount program to worry
1247 * about for the moment.
1248 */
1249
Al Viro42faad92008-04-24 07:21:56 -04001250 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001251 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /*
1255 * No sense to grab the lock for this test, but test itself looks
1256 * somewhat bogus. Suggestions for better replacement?
1257 * Ho-hum... In principle, we might treat that as umount + switch
1258 * to rootfs. GC would eventually take care of the old vfsmount.
1259 * Actually it makes sense, especially if rootfs would contain a
1260 * /reboot - static binary that would close all descriptors and
1261 * call reboot(9). Then init(8) could umount root and exec /reboot.
1262 */
Al Viro1ab59732011-11-24 21:35:16 -05001263 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /*
1265 * Special case for "unmounting" root ...
1266 * we just try to remount it readonly.
1267 */
1268 down_write(&sb->s_umount);
Al Viro4aa98cf2009-05-08 13:36:58 -04001269 if (!(sb->s_flags & MS_RDONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 up_write(&sb->s_umount);
1272 return retval;
1273 }
1274
Al Viro97216be2013-03-16 15:12:40 -04001275 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09301276 br_write_lock(&vfsmount_lock);
Al Viro5addc5d2005-11-07 17:15:49 -05001277 event++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Al Viroc35038b2008-03-22 00:46:23 -04001279 if (!(flags & MNT_DETACH))
Al Virob54b9be2013-03-16 14:39:34 -04001280 shrink_submounts(mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 retval = -EBUSY;
Ram Paia05964f2005-11-07 17:20:17 -05001283 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05001284 if (!list_empty(&mnt->mnt_list))
Al Viro328e6d92013-03-16 14:49:45 -04001285 umount_tree(mnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 retval = 0;
1287 }
Andi Kleen962830d2012-05-08 13:32:02 +09301288 br_write_unlock(&vfsmount_lock);
Al Viroe3197d82013-03-16 14:35:16 -04001289 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return retval;
1291}
1292
Al Viro9b40bc92013-02-22 22:45:42 -05001293/*
1294 * Is the caller allowed to modify his namespace?
1295 */
1296static inline bool may_mount(void)
1297{
1298 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/*
1302 * Now umount can handle mount points as well as block devices.
1303 * This is important for filesystems which use unnamed block devices.
1304 *
1305 * We now support a flag for forced unmount like the other 'big iron'
1306 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1307 */
1308
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001309SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Al Viro2d8f3032008-07-22 09:59:21 -04001311 struct path path;
Al Viro900148d2011-11-25 00:33:11 -05001312 struct mount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int retval;
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001314 int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001316 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1317 return -EINVAL;
1318
Al Viro9b40bc92013-02-22 22:45:42 -05001319 if (!may_mount())
1320 return -EPERM;
1321
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001322 if (!(flags & UMOUNT_NOFOLLOW))
1323 lookup_flags |= LOOKUP_FOLLOW;
1324
1325 retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (retval)
1327 goto out;
Al Viro900148d2011-11-25 00:33:11 -05001328 mnt = real_mount(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 retval = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04001330 if (path.dentry != path.mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 goto dput_and_out;
Al Viro143c8c92011-11-25 00:46:35 -05001332 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto dput_and_out;
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001334 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1335 goto dput_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Al Viro900148d2011-11-25 00:33:11 -05001337 retval = do_umount(mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338dput_and_out:
Jan Blunck429731b2008-02-14 19:34:31 -08001339 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Al Viro2d8f3032008-07-22 09:59:21 -04001340 dput(path.dentry);
Al Viro900148d2011-11-25 00:33:11 -05001341 mntput_no_expire(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342out:
1343 return retval;
1344}
1345
1346#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1347
1348/*
Ram Paib58fed82005-11-07 17:16:09 -05001349 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001351SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Ram Paib58fed82005-11-07 17:16:09 -05001353 return sys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356#endif
1357
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001358static bool is_mnt_ns_file(struct dentry *dentry)
Eric W. Biederman8823c072010-03-07 18:49:36 -08001359{
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001360 /* Is this a proxy for a mount namespace? */
1361 struct inode *inode = dentry->d_inode;
David Howells0bb80f22013-04-12 01:50:06 +01001362 struct proc_ns *ei;
Eric W. Biederman8823c072010-03-07 18:49:36 -08001363
1364 if (!proc_ns_inode(inode))
1365 return false;
1366
David Howells0bb80f22013-04-12 01:50:06 +01001367 ei = get_proc_ns(inode);
Eric W. Biederman8823c072010-03-07 18:49:36 -08001368 if (ei->ns_ops != &mntns_operations)
1369 return false;
1370
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001371 return true;
1372}
1373
1374static bool mnt_ns_loop(struct dentry *dentry)
1375{
1376 /* Could bind mounting the mount namespace inode cause a
1377 * mount namespace loop?
1378 */
1379 struct mnt_namespace *mnt_ns;
1380 if (!is_mnt_ns_file(dentry))
1381 return false;
1382
1383 mnt_ns = get_proc_ns(dentry->d_inode)->ns;
Eric W. Biederman8823c072010-03-07 18:49:36 -08001384 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1385}
1386
Al Viro87129cc2011-11-24 21:24:27 -05001387struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001388 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Al Viro84d17192013-03-15 10:53:28 -04001390 struct mount *res, *p, *q, *r, *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001392 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1393 return ERR_PTR(-EINVAL);
1394
1395 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
David Howellsbe34d1a2012-06-25 12:55:18 +01001396 return ERR_PTR(-EINVAL);
Ram Pai9676f0c2005-11-07 17:21:20 -05001397
Ram Pai36341f62005-11-07 17:17:22 -05001398 res = q = clone_mnt(mnt, dentry, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001399 if (IS_ERR(q))
1400 return q;
1401
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001402 q->mnt.mnt_flags &= ~MNT_LOCKED;
Al Viroa73324d2011-11-24 22:25:07 -05001403 q->mnt_mountpoint = mnt->mnt_mountpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 p = mnt;
Al Viro6b41d532011-11-24 23:24:33 -05001406 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Al Viro315fc832011-11-24 18:57:30 -05001407 struct mount *s;
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001408 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 continue;
1410
Al Viro909b0a82011-11-25 03:06:56 -05001411 for (s = r; s; s = next_mnt(s, r)) {
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001412 if (!(flag & CL_COPY_UNBINDABLE) &&
1413 IS_MNT_UNBINDABLE(s)) {
1414 s = skip_mnt_tree(s);
1415 continue;
1416 }
1417 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1418 is_mnt_ns_file(s->mnt.mnt_root)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001419 s = skip_mnt_tree(s);
1420 continue;
1421 }
Al Viro0714a532011-11-24 22:19:58 -05001422 while (p != s->mnt_parent) {
1423 p = p->mnt_parent;
1424 q = q->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Al Viro87129cc2011-11-24 21:24:27 -05001426 p = s;
Al Viro84d17192013-03-15 10:53:28 -04001427 parent = q;
Al Viro87129cc2011-11-24 21:24:27 -05001428 q = clone_mnt(p, p->mnt.mnt_root, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001429 if (IS_ERR(q))
1430 goto out;
Andi Kleen962830d2012-05-08 13:32:02 +09301431 br_write_lock(&vfsmount_lock);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001432 list_add_tail(&q->mnt_list, &res->mnt_list);
Al Viro84d17192013-03-15 10:53:28 -04001433 attach_mnt(q, parent, p->mnt_mp);
Andi Kleen962830d2012-05-08 13:32:02 +09301434 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 }
1437 return res;
David Howellsbe34d1a2012-06-25 12:55:18 +01001438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (res) {
Andi Kleen962830d2012-05-08 13:32:02 +09301440 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04001441 umount_tree(res, 0);
Andi Kleen962830d2012-05-08 13:32:02 +09301442 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001444 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
David Howellsbe34d1a2012-06-25 12:55:18 +01001447/* Caller should check returned pointer for errors */
1448
Al Viro589ff872009-04-18 03:28:19 -04001449struct vfsmount *collect_mounts(struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001450{
Al Virocb338d02011-11-24 20:55:08 -05001451 struct mount *tree;
Al Viro97216be2013-03-16 15:12:40 -04001452 namespace_lock();
Al Viro87129cc2011-11-24 21:24:27 -05001453 tree = copy_tree(real_mount(path->mnt), path->dentry,
1454 CL_COPY_ALL | CL_PRIVATE);
Al Viro328e6d92013-03-16 14:49:45 -04001455 namespace_unlock();
David Howellsbe34d1a2012-06-25 12:55:18 +01001456 if (IS_ERR(tree))
1457 return NULL;
1458 return &tree->mnt;
Al Viro8aec0802007-06-07 12:20:32 -04001459}
1460
1461void drop_collected_mounts(struct vfsmount *mnt)
1462{
Al Viro97216be2013-03-16 15:12:40 -04001463 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09301464 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04001465 umount_tree(real_mount(mnt), 0);
Andi Kleen962830d2012-05-08 13:32:02 +09301466 br_write_unlock(&vfsmount_lock);
Al Viro3ab6abe2013-03-16 14:42:19 -04001467 namespace_unlock();
Al Viro8aec0802007-06-07 12:20:32 -04001468}
1469
Al Viro1f707132010-01-30 22:51:25 -05001470int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1471 struct vfsmount *root)
1472{
Al Viro1a4eeaf2011-11-25 02:19:55 -05001473 struct mount *mnt;
Al Viro1f707132010-01-30 22:51:25 -05001474 int res = f(root, arg);
1475 if (res)
1476 return res;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001477 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1478 res = f(&mnt->mnt, arg);
Al Viro1f707132010-01-30 22:51:25 -05001479 if (res)
1480 return res;
1481 }
1482 return 0;
1483}
1484
Al Viro4b8b21f2011-11-24 19:54:23 -05001485static void cleanup_group_ids(struct mount *mnt, struct mount *end)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001486{
Al Viro315fc832011-11-24 18:57:30 -05001487 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001488
Al Viro909b0a82011-11-25 03:06:56 -05001489 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001490 if (p->mnt_group_id && !IS_MNT_SHARED(p))
Al Viro4b8b21f2011-11-24 19:54:23 -05001491 mnt_release_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001492 }
1493}
1494
Al Viro4b8b21f2011-11-24 19:54:23 -05001495static int invent_group_ids(struct mount *mnt, bool recurse)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001496{
Al Viro315fc832011-11-24 18:57:30 -05001497 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001498
Al Viro909b0a82011-11-25 03:06:56 -05001499 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
Al Virofc7be132011-11-25 01:05:37 -05001500 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001501 int err = mnt_alloc_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001502 if (err) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001503 cleanup_group_ids(mnt, p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001504 return err;
1505 }
1506 }
1507 }
1508
1509 return 0;
1510}
1511
Ram Paib90fa9a2005-11-07 17:19:50 -05001512/*
1513 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05001514 * @nd : place the mount tree @source_mnt is attached
1515 * @parent_nd : if non-null, detach the source_mnt from its parent and
1516 * store the parent mount and mountpoint dentry.
1517 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05001518 *
1519 * NOTE: in the table below explains the semantics when a source mount
1520 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05001521 * ---------------------------------------------------------------------------
1522 * | BIND MOUNT OPERATION |
1523 * |**************************************************************************
1524 * | source-->| shared | private | slave | unbindable |
1525 * | dest | | | | |
1526 * | | | | | | |
1527 * | v | | | | |
1528 * |**************************************************************************
1529 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1530 * | | | | | |
1531 * |non-shared| shared (+) | private | slave (*) | invalid |
1532 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05001533 * A bind operation clones the source mount and mounts the clone on the
1534 * destination mount.
1535 *
1536 * (++) the cloned mount is propagated to all the mounts in the propagation
1537 * tree of the destination mount and the cloned mount is added to
1538 * the peer group of the source mount.
1539 * (+) the cloned mount is created under the destination mount and is marked
1540 * as shared. The cloned mount is added to the peer group of the source
1541 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05001542 * (+++) the mount is propagated to all the mounts in the propagation tree
1543 * of the destination mount and the cloned mount is made slave
1544 * of the same master as that of the source mount. The cloned mount
1545 * is marked as 'shared and slave'.
1546 * (*) the cloned mount is made a slave of the same master as that of the
1547 * source mount.
1548 *
Ram Pai9676f0c2005-11-07 17:21:20 -05001549 * ---------------------------------------------------------------------------
1550 * | MOVE MOUNT OPERATION |
1551 * |**************************************************************************
1552 * | source-->| shared | private | slave | unbindable |
1553 * | dest | | | | |
1554 * | | | | | | |
1555 * | v | | | | |
1556 * |**************************************************************************
1557 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1558 * | | | | | |
1559 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1560 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05001561 *
1562 * (+) the mount is moved to the destination. And is then propagated to
1563 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05001564 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05001565 * (+++) the mount is moved to the destination and is then propagated to
1566 * all the mounts belonging to the destination mount's propagation tree.
1567 * the mount is marked as 'shared and slave'.
1568 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05001569 *
1570 * if the source mount is a tree, the operations explained above is
1571 * applied to each mount in the tree.
1572 * Must be called without spinlocks held, since this function can sleep
1573 * in allocations.
1574 */
Al Viro0fb54e52011-11-24 19:59:16 -05001575static int attach_recursive_mnt(struct mount *source_mnt,
Al Viro84d17192013-03-15 10:53:28 -04001576 struct mount *dest_mnt,
1577 struct mountpoint *dest_mp,
1578 struct path *parent_path)
Ram Paib90fa9a2005-11-07 17:19:50 -05001579{
1580 LIST_HEAD(tree_list);
Al Viro315fc832011-11-24 18:57:30 -05001581 struct mount *child, *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001582 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001583
Al Virofc7be132011-11-25 01:05:37 -05001584 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro0fb54e52011-11-24 19:59:16 -05001585 err = invent_group_ids(source_mnt, true);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001586 if (err)
1587 goto out;
1588 }
Al Viro84d17192013-03-15 10:53:28 -04001589 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001590 if (err)
1591 goto out_cleanup_ids;
Ram Paib90fa9a2005-11-07 17:19:50 -05001592
Andi Kleen962830d2012-05-08 13:32:02 +09301593 br_write_lock(&vfsmount_lock);
Al Virodf1a1ad2010-01-16 12:57:40 -05001594
Al Virofc7be132011-11-25 01:05:37 -05001595 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro909b0a82011-11-25 03:06:56 -05001596 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
Al Viro0f0afb12011-11-24 20:43:10 -05001597 set_mnt_shared(p);
Ram Paib90fa9a2005-11-07 17:19:50 -05001598 }
Al Viro1a390682008-03-21 20:48:19 -04001599 if (parent_path) {
Al Viro0fb54e52011-11-24 19:59:16 -05001600 detach_mnt(source_mnt, parent_path);
Al Viro84d17192013-03-15 10:53:28 -04001601 attach_mnt(source_mnt, dest_mnt, dest_mp);
Al Viro143c8c92011-11-25 00:46:35 -05001602 touch_mnt_namespace(source_mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05001603 } else {
Al Viro84d17192013-03-15 10:53:28 -04001604 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
Al Viro0fb54e52011-11-24 19:59:16 -05001605 commit_tree(source_mnt);
Ram Pai21444402005-11-07 17:20:03 -05001606 }
Ram Paib90fa9a2005-11-07 17:19:50 -05001607
Al Viro1b8e5562011-11-24 21:01:32 -05001608 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1609 list_del_init(&child->mnt_hash);
Al Viro4b2619a2011-11-24 19:47:15 -05001610 commit_tree(child);
Ram Paib90fa9a2005-11-07 17:19:50 -05001611 }
Andi Kleen962830d2012-05-08 13:32:02 +09301612 br_write_unlock(&vfsmount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +10001613
Ram Paib90fa9a2005-11-07 17:19:50 -05001614 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001615
1616 out_cleanup_ids:
Al Virofc7be132011-11-25 01:05:37 -05001617 if (IS_MNT_SHARED(dest_mnt))
Al Viro0fb54e52011-11-24 19:59:16 -05001618 cleanup_group_ids(source_mnt, NULL);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001619 out:
1620 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001621}
1622
Al Viro84d17192013-03-15 10:53:28 -04001623static struct mountpoint *lock_mount(struct path *path)
Al Virob12cea92011-03-18 08:55:38 -04001624{
1625 struct vfsmount *mnt;
Al Viro84d17192013-03-15 10:53:28 -04001626 struct dentry *dentry = path->dentry;
Al Virob12cea92011-03-18 08:55:38 -04001627retry:
Al Viro84d17192013-03-15 10:53:28 -04001628 mutex_lock(&dentry->d_inode->i_mutex);
1629 if (unlikely(cant_mount(dentry))) {
1630 mutex_unlock(&dentry->d_inode->i_mutex);
1631 return ERR_PTR(-ENOENT);
Al Virob12cea92011-03-18 08:55:38 -04001632 }
Al Viro97216be2013-03-16 15:12:40 -04001633 namespace_lock();
Al Virob12cea92011-03-18 08:55:38 -04001634 mnt = lookup_mnt(path);
Al Viro84d17192013-03-15 10:53:28 -04001635 if (likely(!mnt)) {
1636 struct mountpoint *mp = new_mountpoint(dentry);
1637 if (IS_ERR(mp)) {
Al Viro97216be2013-03-16 15:12:40 -04001638 namespace_unlock();
Al Viro84d17192013-03-15 10:53:28 -04001639 mutex_unlock(&dentry->d_inode->i_mutex);
1640 return mp;
1641 }
1642 return mp;
1643 }
Al Viro97216be2013-03-16 15:12:40 -04001644 namespace_unlock();
Al Virob12cea92011-03-18 08:55:38 -04001645 mutex_unlock(&path->dentry->d_inode->i_mutex);
1646 path_put(path);
1647 path->mnt = mnt;
Al Viro84d17192013-03-15 10:53:28 -04001648 dentry = path->dentry = dget(mnt->mnt_root);
Al Virob12cea92011-03-18 08:55:38 -04001649 goto retry;
1650}
1651
Al Viro84d17192013-03-15 10:53:28 -04001652static void unlock_mount(struct mountpoint *where)
Al Virob12cea92011-03-18 08:55:38 -04001653{
Al Viro84d17192013-03-15 10:53:28 -04001654 struct dentry *dentry = where->m_dentry;
1655 put_mountpoint(where);
Al Viro328e6d92013-03-16 14:49:45 -04001656 namespace_unlock();
Al Viro84d17192013-03-15 10:53:28 -04001657 mutex_unlock(&dentry->d_inode->i_mutex);
Al Virob12cea92011-03-18 08:55:38 -04001658}
1659
Al Viro84d17192013-03-15 10:53:28 -04001660static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Al Viro95bc5f22011-11-25 00:30:56 -05001662 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return -EINVAL;
1664
Al Viro84d17192013-03-15 10:53:28 -04001665 if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
Al Viro95bc5f22011-11-25 00:30:56 -05001666 S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return -ENOTDIR;
1668
Al Viro84d17192013-03-15 10:53:28 -04001669 return attach_recursive_mnt(mnt, p, mp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001673 * Sanity check the flags to change_mnt_propagation.
1674 */
1675
1676static int flags_to_propagation_type(int flags)
1677{
Roman Borisov7c6e9842011-05-25 16:26:48 -07001678 int type = flags & ~(MS_REC | MS_SILENT);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001679
1680 /* Fail if any non-propagation flags are set */
1681 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1682 return 0;
1683 /* Only one propagation flag should be set */
1684 if (!is_power_of_2(type))
1685 return 0;
1686 return type;
1687}
1688
1689/*
Ram Pai07b20882005-11-07 17:19:07 -05001690 * recursively change the type of the mountpoint.
1691 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001692static int do_change_type(struct path *path, int flag)
Ram Pai07b20882005-11-07 17:19:07 -05001693{
Al Viro315fc832011-11-24 18:57:30 -05001694 struct mount *m;
Al Viro4b8b21f2011-11-24 19:54:23 -05001695 struct mount *mnt = real_mount(path->mnt);
Ram Pai07b20882005-11-07 17:19:07 -05001696 int recurse = flag & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001697 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001698 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05001699
Al Viro2d92ab32008-08-02 00:51:11 -04001700 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05001701 return -EINVAL;
1702
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001703 type = flags_to_propagation_type(flag);
1704 if (!type)
1705 return -EINVAL;
1706
Al Viro97216be2013-03-16 15:12:40 -04001707 namespace_lock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001708 if (type == MS_SHARED) {
1709 err = invent_group_ids(mnt, recurse);
1710 if (err)
1711 goto out_unlock;
1712 }
1713
Andi Kleen962830d2012-05-08 13:32:02 +09301714 br_write_lock(&vfsmount_lock);
Al Viro909b0a82011-11-25 03:06:56 -05001715 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
Al Viro0f0afb12011-11-24 20:43:10 -05001716 change_mnt_propagation(m, type);
Andi Kleen962830d2012-05-08 13:32:02 +09301717 br_write_unlock(&vfsmount_lock);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001718
1719 out_unlock:
Al Viro97216be2013-03-16 15:12:40 -04001720 namespace_unlock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001721 return err;
Ram Pai07b20882005-11-07 17:19:07 -05001722}
1723
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001724static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
1725{
1726 struct mount *child;
1727 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
1728 if (!is_subdir(child->mnt_mountpoint, dentry))
1729 continue;
1730
1731 if (child->mnt.mnt_flags & MNT_LOCKED)
1732 return true;
1733 }
1734 return false;
1735}
1736
Ram Pai07b20882005-11-07 17:19:07 -05001737/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 * do loopback mount.
1739 */
Al Viro808d4e32012-10-11 11:42:01 -04001740static int do_loopback(struct path *path, const char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08001741 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Al Viro2d92ab32008-08-02 00:51:11 -04001743 struct path old_path;
Al Viro84d17192013-03-15 10:53:28 -04001744 struct mount *mnt = NULL, *old, *parent;
1745 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05001746 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (!old_name || !*old_name)
1748 return -EINVAL;
Trond Myklebust815d4052011-09-26 20:36:09 -04001749 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (err)
1751 return err;
1752
Eric W. Biederman8823c072010-03-07 18:49:36 -08001753 err = -EINVAL;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001754 if (mnt_ns_loop(old_path.dentry))
Eric W. Biederman8823c072010-03-07 18:49:36 -08001755 goto out;
1756
Al Viro84d17192013-03-15 10:53:28 -04001757 mp = lock_mount(path);
1758 err = PTR_ERR(mp);
1759 if (IS_ERR(mp))
Jan Blunck4ac91372008-02-14 19:34:32 -08001760 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05001761
Al Viro87129cc2011-11-24 21:24:27 -05001762 old = real_mount(old_path.mnt);
Al Viro84d17192013-03-15 10:53:28 -04001763 parent = real_mount(path->mnt);
Al Viro87129cc2011-11-24 21:24:27 -05001764
Al Virob12cea92011-03-18 08:55:38 -04001765 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05001766 if (IS_MNT_UNBINDABLE(old))
Al Virob12cea92011-03-18 08:55:38 -04001767 goto out2;
1768
Al Viro84d17192013-03-15 10:53:28 -04001769 if (!check_mnt(parent) || !check_mnt(old))
Al Virob12cea92011-03-18 08:55:38 -04001770 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001772 if (!recurse && has_locked_children(old, old_path.dentry))
1773 goto out2;
1774
Al Viroccd48bc2005-11-07 17:15:04 -05001775 if (recurse)
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001776 mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
Al Viroccd48bc2005-11-07 17:15:04 -05001777 else
Al Viro87129cc2011-11-24 21:24:27 -05001778 mnt = clone_mnt(old, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05001779
David Howellsbe34d1a2012-06-25 12:55:18 +01001780 if (IS_ERR(mnt)) {
1781 err = PTR_ERR(mnt);
Andrey Vagine9c5d8a2013-04-09 17:33:29 +04001782 goto out2;
David Howellsbe34d1a2012-06-25 12:55:18 +01001783 }
Al Viroccd48bc2005-11-07 17:15:04 -05001784
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001785 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
1786
Al Viro84d17192013-03-15 10:53:28 -04001787 err = graft_tree(mnt, parent, mp);
Al Viroccd48bc2005-11-07 17:15:04 -05001788 if (err) {
Andi Kleen962830d2012-05-08 13:32:02 +09301789 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04001790 umount_tree(mnt, 0);
Andi Kleen962830d2012-05-08 13:32:02 +09301791 br_write_unlock(&vfsmount_lock);
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05001792 }
Al Virob12cea92011-03-18 08:55:38 -04001793out2:
Al Viro84d17192013-03-15 10:53:28 -04001794 unlock_mount(mp);
Al Viroccd48bc2005-11-07 17:15:04 -05001795out:
Al Viro2d92ab32008-08-02 00:51:11 -04001796 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return err;
1798}
1799
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001800static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1801{
1802 int error = 0;
1803 int readonly_request = 0;
1804
1805 if (ms_flags & MS_RDONLY)
1806 readonly_request = 1;
1807 if (readonly_request == __mnt_is_readonly(mnt))
1808 return 0;
1809
Eric W. Biederman90563b12013-03-22 03:10:15 -07001810 if (mnt->mnt_flags & MNT_LOCK_READONLY)
1811 return -EPERM;
1812
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001813 if (readonly_request)
Al Viro83adc752011-11-24 22:37:54 -05001814 error = mnt_make_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001815 else
Al Viro83adc752011-11-24 22:37:54 -05001816 __mnt_unmake_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001817 return error;
1818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820/*
1821 * change filesystem flags. dir should be a physical root of filesystem.
1822 * If you've mounted a non-root directory somewhere and want to do remount
1823 * on it - tough luck.
1824 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001825static int do_remount(struct path *path, int flags, int mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 void *data)
1827{
1828 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04001829 struct super_block *sb = path->mnt->mnt_sb;
Al Viro143c8c92011-11-25 00:46:35 -05001830 struct mount *mnt = real_mount(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Al Viro143c8c92011-11-25 00:46:35 -05001832 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -EINVAL;
1834
Al Viro2d92ab32008-08-02 00:51:11 -04001835 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return -EINVAL;
1837
Eric Parisff36fe22011-03-03 16:09:14 -05001838 err = security_sb_remount(sb, data);
1839 if (err)
1840 return err;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 down_write(&sb->s_umount);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001843 if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04001844 err = change_mount_flags(path->mnt, flags);
Al Viro57eccb82013-02-22 22:49:10 -05001845 else if (!capable(CAP_SYS_ADMIN))
1846 err = -EPERM;
Al Viro4aa98cf2009-05-08 13:36:58 -04001847 else
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001848 err = do_remount_sb(sb, flags, data, 0);
Al Viro7b43a792010-01-16 13:01:26 -05001849 if (!err) {
Andi Kleen962830d2012-05-08 13:32:02 +09301850 br_write_lock(&vfsmount_lock);
Al Viro143c8c92011-11-25 00:46:35 -05001851 mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
1852 mnt->mnt.mnt_flags = mnt_flags;
Andi Kleen962830d2012-05-08 13:32:02 +09301853 br_write_unlock(&vfsmount_lock);
Al Viro7b43a792010-01-16 13:01:26 -05001854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 up_write(&sb->s_umount);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001856 if (!err) {
Andi Kleen962830d2012-05-08 13:32:02 +09301857 br_write_lock(&vfsmount_lock);
Al Viro143c8c92011-11-25 00:46:35 -05001858 touch_mnt_namespace(mnt->mnt_ns);
Andi Kleen962830d2012-05-08 13:32:02 +09301859 br_write_unlock(&vfsmount_lock);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return err;
1862}
1863
Al Virocbbe3622011-11-24 20:01:19 -05001864static inline int tree_contains_unbindable(struct mount *mnt)
Ram Pai9676f0c2005-11-07 17:21:20 -05001865{
Al Viro315fc832011-11-24 18:57:30 -05001866 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001867 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001868 if (IS_MNT_UNBINDABLE(p))
Ram Pai9676f0c2005-11-07 17:21:20 -05001869 return 1;
1870 }
1871 return 0;
1872}
1873
Al Viro808d4e32012-10-11 11:42:01 -04001874static int do_move_mount(struct path *path, const char *old_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Al Viro2d92ab32008-08-02 00:51:11 -04001876 struct path old_path, parent_path;
Al Viro676da582011-11-24 21:47:05 -05001877 struct mount *p;
Al Viro0fb54e52011-11-24 19:59:16 -05001878 struct mount *old;
Al Viro84d17192013-03-15 10:53:28 -04001879 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05001880 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!old_name || !*old_name)
1882 return -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001883 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (err)
1885 return err;
1886
Al Viro84d17192013-03-15 10:53:28 -04001887 mp = lock_mount(path);
1888 err = PTR_ERR(mp);
1889 if (IS_ERR(mp))
David Howellscc53ce52011-01-14 18:45:26 +00001890 goto out;
1891
Al Viro143c8c92011-11-25 00:46:35 -05001892 old = real_mount(old_path.mnt);
Al Virofc7be132011-11-25 01:05:37 -05001893 p = real_mount(path->mnt);
Al Viro143c8c92011-11-25 00:46:35 -05001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05001896 if (!check_mnt(p) || !check_mnt(old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto out1;
1898
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001899 if (old->mnt.mnt_flags & MNT_LOCKED)
1900 goto out1;
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001903 if (old_path.dentry != old_path.mnt->mnt_root)
Ram Pai21444402005-11-07 17:20:03 -05001904 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Al Viro676da582011-11-24 21:47:05 -05001906 if (!mnt_has_parent(old))
Ram Pai21444402005-11-07 17:20:03 -05001907 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Al Viro2d92ab32008-08-02 00:51:11 -04001909 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1910 S_ISDIR(old_path.dentry->d_inode->i_mode))
Ram Pai21444402005-11-07 17:20:03 -05001911 goto out1;
1912 /*
1913 * Don't move a mount residing in a shared parent.
1914 */
Al Virofc7be132011-11-25 01:05:37 -05001915 if (IS_MNT_SHARED(old->mnt_parent))
Ram Pai21444402005-11-07 17:20:03 -05001916 goto out1;
Ram Pai9676f0c2005-11-07 17:21:20 -05001917 /*
1918 * Don't move a mount tree containing unbindable mounts to a destination
1919 * mount which is shared.
1920 */
Al Virofc7be132011-11-25 01:05:37 -05001921 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
Ram Pai9676f0c2005-11-07 17:21:20 -05001922 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 err = -ELOOP;
Al Virofc7be132011-11-25 01:05:37 -05001924 for (; mnt_has_parent(p); p = p->mnt_parent)
Al Viro676da582011-11-24 21:47:05 -05001925 if (p == old)
Ram Pai21444402005-11-07 17:20:03 -05001926 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Al Viro84d17192013-03-15 10:53:28 -04001928 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
Jan Blunck4ac91372008-02-14 19:34:32 -08001929 if (err)
Ram Pai21444402005-11-07 17:20:03 -05001930 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* if the mount is moved, it should no longer be expire
1933 * automatically */
Al Viro6776db3d2011-11-25 00:22:05 -05001934 list_del_init(&old->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935out1:
Al Viro84d17192013-03-15 10:53:28 -04001936 unlock_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (!err)
Al Viro1a390682008-03-21 20:48:19 -04001939 path_put(&parent_path);
Al Viro2d92ab32008-08-02 00:51:11 -04001940 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return err;
1942}
1943
Al Viro9d412a42011-03-17 22:08:28 -04001944static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1945{
1946 int err;
1947 const char *subtype = strchr(fstype, '.');
1948 if (subtype) {
1949 subtype++;
1950 err = -EINVAL;
1951 if (!subtype[0])
1952 goto err;
1953 } else
1954 subtype = "";
1955
1956 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1957 err = -ENOMEM;
1958 if (!mnt->mnt_sb->s_subtype)
1959 goto err;
1960 return mnt;
1961
1962 err:
1963 mntput(mnt);
1964 return ERR_PTR(err);
1965}
1966
Al Viro9d412a42011-03-17 22:08:28 -04001967/*
1968 * add a mount into a namespace's mount tree
1969 */
Al Viro95bc5f22011-11-25 00:30:56 -05001970static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
Al Viro9d412a42011-03-17 22:08:28 -04001971{
Al Viro84d17192013-03-15 10:53:28 -04001972 struct mountpoint *mp;
1973 struct mount *parent;
Al Viro9d412a42011-03-17 22:08:28 -04001974 int err;
1975
1976 mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
1977
Al Viro84d17192013-03-15 10:53:28 -04001978 mp = lock_mount(path);
1979 if (IS_ERR(mp))
1980 return PTR_ERR(mp);
Al Viro9d412a42011-03-17 22:08:28 -04001981
Al Viro84d17192013-03-15 10:53:28 -04001982 parent = real_mount(path->mnt);
Al Viro9d412a42011-03-17 22:08:28 -04001983 err = -EINVAL;
Al Viro84d17192013-03-15 10:53:28 -04001984 if (unlikely(!check_mnt(parent))) {
Al Viro156cacb2012-09-21 08:19:02 -04001985 /* that's acceptable only for automounts done in private ns */
1986 if (!(mnt_flags & MNT_SHRINKABLE))
1987 goto unlock;
1988 /* ... and for those we'd better have mountpoint still alive */
Al Viro84d17192013-03-15 10:53:28 -04001989 if (!parent->mnt_ns)
Al Viro156cacb2012-09-21 08:19:02 -04001990 goto unlock;
1991 }
Al Viro9d412a42011-03-17 22:08:28 -04001992
1993 /* Refuse the same filesystem on the same mount point */
1994 err = -EBUSY;
Al Viro95bc5f22011-11-25 00:30:56 -05001995 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
Al Viro9d412a42011-03-17 22:08:28 -04001996 path->mnt->mnt_root == path->dentry)
1997 goto unlock;
1998
1999 err = -EINVAL;
Al Viro95bc5f22011-11-25 00:30:56 -05002000 if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
Al Viro9d412a42011-03-17 22:08:28 -04002001 goto unlock;
2002
Al Viro95bc5f22011-11-25 00:30:56 -05002003 newmnt->mnt.mnt_flags = mnt_flags;
Al Viro84d17192013-03-15 10:53:28 -04002004 err = graft_tree(newmnt, parent, mp);
Al Viro9d412a42011-03-17 22:08:28 -04002005
2006unlock:
Al Viro84d17192013-03-15 10:53:28 -04002007 unlock_mount(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002008 return err;
2009}
Al Virob1e75df2011-01-17 01:47:59 -05002010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011/*
2012 * create a new mount for userspace and request it to be added into the
2013 * namespace's tree
2014 */
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002015static int do_new_mount(struct path *path, const char *fstype, int flags,
Al Viro808d4e32012-10-11 11:42:01 -04002016 int mnt_flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002018 struct file_system_type *type;
Al Viro9b40bc92013-02-22 22:45:42 -05002019 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 struct vfsmount *mnt;
Al Viro15f9a3f2011-01-17 01:41:58 -05002021 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002023 if (!fstype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return -EINVAL;
2025
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002026 type = get_fs_type(fstype);
2027 if (!type)
2028 return -ENODEV;
2029
2030 if (user_ns != &init_user_ns) {
2031 if (!(type->fs_flags & FS_USERNS_MOUNT)) {
2032 put_filesystem(type);
2033 return -EPERM;
2034 }
2035 /* Only in special cases allow devices from mounts
2036 * created outside the initial user namespace.
2037 */
2038 if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
2039 flags |= MS_NODEV;
2040 mnt_flags |= MNT_NODEV;
2041 }
2042 }
2043
2044 mnt = vfs_kern_mount(type, flags, name, data);
2045 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2046 !mnt->mnt_sb->s_subtype)
2047 mnt = fs_set_subtype(mnt, fstype);
2048
2049 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (IS_ERR(mnt))
2051 return PTR_ERR(mnt);
2052
Al Viro95bc5f22011-11-25 00:30:56 -05002053 err = do_add_mount(real_mount(mnt), path, mnt_flags);
Al Viro15f9a3f2011-01-17 01:41:58 -05002054 if (err)
2055 mntput(mnt);
2056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Al Viro19a167a2011-01-17 01:35:23 -05002059int finish_automount(struct vfsmount *m, struct path *path)
2060{
Al Viro6776db3d2011-11-25 00:22:05 -05002061 struct mount *mnt = real_mount(m);
Al Viro19a167a2011-01-17 01:35:23 -05002062 int err;
2063 /* The new mount record should have at least 2 refs to prevent it being
2064 * expired before we get a chance to add it
2065 */
Al Viro6776db3d2011-11-25 00:22:05 -05002066 BUG_ON(mnt_get_count(mnt) < 2);
Al Viro19a167a2011-01-17 01:35:23 -05002067
2068 if (m->mnt_sb == path->mnt->mnt_sb &&
2069 m->mnt_root == path->dentry) {
Al Virob1e75df2011-01-17 01:47:59 -05002070 err = -ELOOP;
2071 goto fail;
Al Viro19a167a2011-01-17 01:35:23 -05002072 }
2073
Al Viro95bc5f22011-11-25 00:30:56 -05002074 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
Al Virob1e75df2011-01-17 01:47:59 -05002075 if (!err)
2076 return 0;
2077fail:
2078 /* remove m from any expiration list it may be on */
Al Viro6776db3d2011-11-25 00:22:05 -05002079 if (!list_empty(&mnt->mnt_expire)) {
Al Viro97216be2013-03-16 15:12:40 -04002080 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302081 br_write_lock(&vfsmount_lock);
Al Viro6776db3d2011-11-25 00:22:05 -05002082 list_del_init(&mnt->mnt_expire);
Andi Kleen962830d2012-05-08 13:32:02 +09302083 br_write_unlock(&vfsmount_lock);
Al Viro97216be2013-03-16 15:12:40 -04002084 namespace_unlock();
Al Viro19a167a2011-01-17 01:35:23 -05002085 }
Al Virob1e75df2011-01-17 01:47:59 -05002086 mntput(m);
2087 mntput(m);
Al Viro19a167a2011-01-17 01:35:23 -05002088 return err;
2089}
2090
David Howellsea5b7782011-01-14 19:10:03 +00002091/**
2092 * mnt_set_expiry - Put a mount on an expiration list
2093 * @mnt: The mount to list.
2094 * @expiry_list: The list to add the mount to.
2095 */
2096void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2097{
Al Viro97216be2013-03-16 15:12:40 -04002098 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302099 br_write_lock(&vfsmount_lock);
David Howellsea5b7782011-01-14 19:10:03 +00002100
Al Viro6776db3d2011-11-25 00:22:05 -05002101 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
David Howellsea5b7782011-01-14 19:10:03 +00002102
Andi Kleen962830d2012-05-08 13:32:02 +09302103 br_write_unlock(&vfsmount_lock);
Al Viro97216be2013-03-16 15:12:40 -04002104 namespace_unlock();
David Howellsea5b7782011-01-14 19:10:03 +00002105}
2106EXPORT_SYMBOL(mnt_set_expiry);
2107
2108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 * process a list of expirable mountpoints with the intent of discarding any
2110 * mountpoints that aren't in use and haven't been touched since last we came
2111 * here
2112 */
2113void mark_mounts_for_expiry(struct list_head *mounts)
2114{
Al Viro761d5c32011-11-24 21:07:43 -05002115 struct mount *mnt, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 LIST_HEAD(graveyard);
2117
2118 if (list_empty(mounts))
2119 return;
2120
Al Viro97216be2013-03-16 15:12:40 -04002121 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302122 br_write_lock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* extract from the expiration list every vfsmount that matches the
2125 * following criteria:
2126 * - only referenced by its parent vfsmount
2127 * - still marked for expiry (marked on the last call here; marks are
2128 * cleared by mntput())
2129 */
Al Viro6776db3d2011-11-25 00:22:05 -05002130 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Al Viro863d6842011-11-25 00:57:42 -05002131 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Viro1ab59732011-11-24 21:35:16 -05002132 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 continue;
Al Viro6776db3d2011-11-25 00:22:05 -05002134 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
Al Virobcc5c7d2008-03-22 00:21:53 -04002136 while (!list_empty(&graveyard)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002137 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002138 touch_mnt_namespace(mnt->mnt_ns);
Al Viro328e6d92013-03-16 14:49:45 -04002139 umount_tree(mnt, 1);
Al Virobcc5c7d2008-03-22 00:21:53 -04002140 }
Andi Kleen962830d2012-05-08 13:32:02 +09302141 br_write_unlock(&vfsmount_lock);
Al Viro3ab6abe2013-03-16 14:42:19 -04002142 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2146
2147/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04002148 * Ripoff of 'select_parent()'
2149 *
2150 * search the list of submounts for a given mountpoint, and move any
2151 * shrinkable submounts to the 'graveyard' list.
2152 */
Al Viro692afc32011-11-24 21:15:14 -05002153static int select_submounts(struct mount *parent, struct list_head *graveyard)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002154{
Al Viro692afc32011-11-24 21:15:14 -05002155 struct mount *this_parent = parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002156 struct list_head *next;
2157 int found = 0;
2158
2159repeat:
Al Viro6b41d532011-11-24 23:24:33 -05002160 next = this_parent->mnt_mounts.next;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002161resume:
Al Viro6b41d532011-11-24 23:24:33 -05002162 while (next != &this_parent->mnt_mounts) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002163 struct list_head *tmp = next;
Al Viro6b41d532011-11-24 23:24:33 -05002164 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002165
2166 next = tmp->next;
Al Viro692afc32011-11-24 21:15:14 -05002167 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
Trond Myklebust5528f9112006-06-09 09:34:17 -04002168 continue;
2169 /*
2170 * Descend a level if the d_mounts list is non-empty.
2171 */
Al Viro6b41d532011-11-24 23:24:33 -05002172 if (!list_empty(&mnt->mnt_mounts)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002173 this_parent = mnt;
2174 goto repeat;
2175 }
2176
Al Viro1ab59732011-11-24 21:35:16 -05002177 if (!propagate_mount_busy(mnt, 1)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002178 list_move_tail(&mnt->mnt_expire, graveyard);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002179 found++;
2180 }
2181 }
2182 /*
2183 * All done at this level ... ascend and resume the search
2184 */
2185 if (this_parent != parent) {
Al Viro6b41d532011-11-24 23:24:33 -05002186 next = this_parent->mnt_child.next;
Al Viro0714a532011-11-24 22:19:58 -05002187 this_parent = this_parent->mnt_parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002188 goto resume;
2189 }
2190 return found;
2191}
2192
2193/*
2194 * process a list of expirable mountpoints with the intent of discarding any
2195 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10002196 *
2197 * vfsmount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04002198 */
Al Virob54b9be2013-03-16 14:39:34 -04002199static void shrink_submounts(struct mount *mnt)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002200{
2201 LIST_HEAD(graveyard);
Al Viro761d5c32011-11-24 21:07:43 -05002202 struct mount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002203
Trond Myklebust5528f9112006-06-09 09:34:17 -04002204 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04002205 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04002206 while (!list_empty(&graveyard)) {
Al Viro761d5c32011-11-24 21:07:43 -05002207 m = list_first_entry(&graveyard, struct mount,
Al Viro6776db3d2011-11-25 00:22:05 -05002208 mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002209 touch_mnt_namespace(m->mnt_ns);
Al Viro328e6d92013-03-16 14:49:45 -04002210 umount_tree(m, 1);
Al Virobcc5c7d2008-03-22 00:21:53 -04002211 }
2212 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04002213}
2214
Trond Myklebust5528f9112006-06-09 09:34:17 -04002215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * Some copy_from_user() implementations do not return the exact number of
2217 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2218 * Note that this function differs from copy_from_user() in that it will oops
2219 * on bad values of `to', rather than returning a short copy.
2220 */
Ram Paib58fed82005-11-07 17:16:09 -05002221static long exact_copy_from_user(void *to, const void __user * from,
2222 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 char *t = to;
2225 const char __user *f = from;
2226 char c;
2227
2228 if (!access_ok(VERIFY_READ, from, n))
2229 return n;
2230
2231 while (n) {
2232 if (__get_user(c, f)) {
2233 memset(t, 0, n);
2234 break;
2235 }
2236 *t++ = c;
2237 f++;
2238 n--;
2239 }
2240 return n;
2241}
2242
Ram Paib58fed82005-11-07 17:16:09 -05002243int copy_mount_options(const void __user * data, unsigned long *where)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 int i;
2246 unsigned long page;
2247 unsigned long size;
Ram Paib58fed82005-11-07 17:16:09 -05002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 *where = 0;
2250 if (!data)
2251 return 0;
2252
2253 if (!(page = __get_free_page(GFP_KERNEL)))
2254 return -ENOMEM;
2255
2256 /* We only care that *some* data at the address the user
2257 * gave us is valid. Just in case, we'll zero
2258 * the remainder of the page.
2259 */
2260 /* copy_from_user cannot cross TASK_SIZE ! */
2261 size = TASK_SIZE - (unsigned long)data;
2262 if (size > PAGE_SIZE)
2263 size = PAGE_SIZE;
2264
2265 i = size - exact_copy_from_user((void *)page, data, size);
2266 if (!i) {
Ram Paib58fed82005-11-07 17:16:09 -05002267 free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return -EFAULT;
2269 }
2270 if (i != PAGE_SIZE)
2271 memset((char *)page + i, 0, PAGE_SIZE - i);
2272 *where = page;
2273 return 0;
2274}
2275
Vegard Nossumeca6f532009-09-18 13:05:45 -07002276int copy_mount_string(const void __user *data, char **where)
2277{
2278 char *tmp;
2279
2280 if (!data) {
2281 *where = NULL;
2282 return 0;
2283 }
2284
2285 tmp = strndup_user(data, PAGE_SIZE);
2286 if (IS_ERR(tmp))
2287 return PTR_ERR(tmp);
2288
2289 *where = tmp;
2290 return 0;
2291}
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293/*
2294 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2295 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2296 *
2297 * data is a (void *) that can point to any structure up to
2298 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2299 * information (or be NULL).
2300 *
2301 * Pre-0.97 versions of mount() didn't have a flags word.
2302 * When the flags word was introduced its top half was required
2303 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2304 * Therefore, if this magic number is present, it carries no information
2305 * and must be discarded.
2306 */
Al Viro808d4e32012-10-11 11:42:01 -04002307long do_mount(const char *dev_name, const char *dir_name,
2308 const char *type_page, unsigned long flags, void *data_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Al Viro2d92ab32008-08-02 00:51:11 -04002310 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 int retval = 0;
2312 int mnt_flags = 0;
2313
2314 /* Discard magic */
2315 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2316 flags &= ~MS_MGC_MSK;
2317
2318 /* Basic sanity checks */
2319
2320 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (data_page)
2324 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2325
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002326 /* ... and get the mountpoint */
2327 retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2328 if (retval)
2329 return retval;
2330
2331 retval = security_sb_mount(dev_name, &path,
2332 type_page, flags, data_page);
Al Viro0d5cadb2013-05-04 14:40:51 -04002333 if (!retval && !may_mount())
2334 retval = -EPERM;
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002335 if (retval)
2336 goto dput_out;
2337
Andi Kleen613cbe32009-04-19 18:40:43 +02002338 /* Default to relatime unless overriden */
2339 if (!(flags & MS_NOATIME))
2340 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* Separate the per-mountpoint flags */
2343 if (flags & MS_NOSUID)
2344 mnt_flags |= MNT_NOSUID;
2345 if (flags & MS_NODEV)
2346 mnt_flags |= MNT_NODEV;
2347 if (flags & MS_NOEXEC)
2348 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002349 if (flags & MS_NOATIME)
2350 mnt_flags |= MNT_NOATIME;
2351 if (flags & MS_NODIRATIME)
2352 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00002353 if (flags & MS_STRICTATIME)
2354 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002355 if (flags & MS_RDONLY)
2356 mnt_flags |= MNT_READONLY;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002357
Al Viro7a4dec52010-08-09 12:05:43 -04002358 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
Matthew Garrettd0adde52009-03-26 17:49:56 +00002359 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2360 MS_STRICTATIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (flags & MS_REMOUNT)
Al Viro2d92ab32008-08-02 00:51:11 -04002363 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 data_page);
2365 else if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002366 retval = do_loopback(&path, dev_name, flags & MS_REC);
Ram Pai9676f0c2005-11-07 17:21:20 -05002367 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Al Viro2d92ab32008-08-02 00:51:11 -04002368 retval = do_change_type(&path, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 else if (flags & MS_MOVE)
Al Viro2d92ab32008-08-02 00:51:11 -04002370 retval = do_move_mount(&path, dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 else
Al Viro2d92ab32008-08-02 00:51:11 -04002372 retval = do_new_mount(&path, type_page, flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 dev_name, data_page);
2374dput_out:
Al Viro2d92ab32008-08-02 00:51:11 -04002375 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return retval;
2377}
2378
Eric W. Biederman771b1372012-07-26 21:08:32 -07002379static void free_mnt_ns(struct mnt_namespace *ns)
2380{
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002381 proc_free_inum(ns->proc_inum);
Eric W. Biederman771b1372012-07-26 21:08:32 -07002382 put_user_ns(ns->user_ns);
2383 kfree(ns);
2384}
2385
Eric W. Biederman8823c072010-03-07 18:49:36 -08002386/*
2387 * Assign a sequence number so we can detect when we attempt to bind
2388 * mount a reference to an older mount namespace into the current
2389 * mount namespace, preventing reference counting loops. A 64bit
2390 * number incrementing at 10Ghz will take 12,427 years to wrap which
2391 * is effectively never, so we can ignore the possibility.
2392 */
2393static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2394
Eric W. Biederman771b1372012-07-26 21:08:32 -07002395static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002396{
2397 struct mnt_namespace *new_ns;
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002398 int ret;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002399
2400 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2401 if (!new_ns)
2402 return ERR_PTR(-ENOMEM);
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002403 ret = proc_alloc_inum(&new_ns->proc_inum);
2404 if (ret) {
2405 kfree(new_ns);
2406 return ERR_PTR(ret);
2407 }
Eric W. Biederman8823c072010-03-07 18:49:36 -08002408 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002409 atomic_set(&new_ns->count, 1);
2410 new_ns->root = NULL;
2411 INIT_LIST_HEAD(&new_ns->list);
2412 init_waitqueue_head(&new_ns->poll);
2413 new_ns->event = 0;
Eric W. Biederman771b1372012-07-26 21:08:32 -07002414 new_ns->user_ns = get_user_ns(user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002415 return new_ns;
2416}
2417
JANAK DESAI741a2952006-02-07 12:59:00 -08002418/*
2419 * Allocate a new namespace structure and populate it with contents
2420 * copied from the namespace of the passed in task structure.
2421 */
Badari Pulavartye3222c42007-05-08 00:25:21 -07002422static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
Eric W. Biederman771b1372012-07-26 21:08:32 -07002423 struct user_namespace *user_ns, struct fs_struct *fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002425 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04002426 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Al Viro315fc832011-11-24 18:57:30 -05002427 struct mount *p, *q;
Al Virobe08d6d2011-12-06 13:32:36 -05002428 struct mount *old = mnt_ns->root;
Al Virocb338d02011-11-24 20:55:08 -05002429 struct mount *new;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002430 int copy_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Eric W. Biederman771b1372012-07-26 21:08:32 -07002432 new_ns = alloc_mnt_ns(user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002433 if (IS_ERR(new_ns))
2434 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Al Viro97216be2013-03-16 15:12:40 -04002436 namespace_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /* First pass: copy the tree topology */
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002438 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002439 if (user_ns != mnt_ns->user_ns)
Eric W. Biederman132c94e2013-03-22 04:08:05 -07002440 copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002441 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
David Howellsbe34d1a2012-06-25 12:55:18 +01002442 if (IS_ERR(new)) {
Al Viro328e6d92013-03-16 14:49:45 -04002443 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07002444 free_mnt_ns(new_ns);
David Howellsbe34d1a2012-06-25 12:55:18 +01002445 return ERR_CAST(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Al Virobe08d6d2011-12-06 13:32:36 -05002447 new_ns->root = new;
Andi Kleen962830d2012-05-08 13:32:02 +09302448 br_write_lock(&vfsmount_lock);
Al Viro1a4eeaf2011-11-25 02:19:55 -05002449 list_add_tail(&new_ns->list, &new->mnt_list);
Andi Kleen962830d2012-05-08 13:32:02 +09302450 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 /*
2453 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2454 * as belonging to new namespace. We have already acquired a private
2455 * fs_struct, so tsk->fs->lock is not needed.
2456 */
Al Viro909b0a82011-11-25 03:06:56 -05002457 p = old;
Al Virocb338d02011-11-24 20:55:08 -05002458 q = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 while (p) {
Al Viro143c8c92011-11-25 00:46:35 -05002460 q->mnt_ns = new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (fs) {
Al Viro315fc832011-11-24 18:57:30 -05002462 if (&p->mnt == fs->root.mnt) {
2463 fs->root.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05002464 rootmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Al Viro315fc832011-11-24 18:57:30 -05002466 if (&p->mnt == fs->pwd.mnt) {
2467 fs->pwd.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05002468 pwdmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
Al Viro909b0a82011-11-25 03:06:56 -05002471 p = next_mnt(p, old);
2472 q = next_mnt(q, new);
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002473 if (!q)
2474 break;
2475 while (p->mnt.mnt_root != q->mnt.mnt_root)
2476 p = next_mnt(p, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Al Viro328e6d92013-03-16 14:49:45 -04002478 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (rootmnt)
Al Virof03c6592011-01-14 22:30:21 -05002481 mntput(rootmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (pwdmnt)
Al Virof03c6592011-01-14 22:30:21 -05002483 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
JANAK DESAI741a2952006-02-07 12:59:00 -08002485 return new_ns;
2486}
2487
Eric W. Biederman213dd262007-07-15 23:41:15 -07002488struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
Eric W. Biederman771b1372012-07-26 21:08:32 -07002489 struct user_namespace *user_ns, struct fs_struct *new_fs)
JANAK DESAI741a2952006-02-07 12:59:00 -08002490{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002491 struct mnt_namespace *new_ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002492
Badari Pulavartye3222c42007-05-08 00:25:21 -07002493 BUG_ON(!ns);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002494 get_mnt_ns(ns);
JANAK DESAI741a2952006-02-07 12:59:00 -08002495
2496 if (!(flags & CLONE_NEWNS))
Badari Pulavartye3222c42007-05-08 00:25:21 -07002497 return ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002498
Eric W. Biederman771b1372012-07-26 21:08:32 -07002499 new_ns = dup_mnt_ns(ns, user_ns, new_fs);
JANAK DESAI741a2952006-02-07 12:59:00 -08002500
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002501 put_mnt_ns(ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -07002502 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002505/**
2506 * create_mnt_ns - creates a private namespace and adds a root filesystem
2507 * @mnt: pointer to the new root filesystem mountpoint
2508 */
Al Viro1a4eeaf2011-11-25 02:19:55 -05002509static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002510{
Eric W. Biederman771b1372012-07-26 21:08:32 -07002511 struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002512 if (!IS_ERR(new_ns)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002513 struct mount *mnt = real_mount(m);
2514 mnt->mnt_ns = new_ns;
Al Virobe08d6d2011-12-06 13:32:36 -05002515 new_ns->root = mnt;
Al Virob1983cd2013-05-04 15:18:53 -04002516 list_add(&mnt->mnt_list, &new_ns->list);
Al Viroc1334492011-11-16 16:12:14 -05002517 } else {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002518 mntput(m);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002519 }
2520 return new_ns;
2521}
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002522
Al Viroea441d12011-11-16 21:43:59 -05002523struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2524{
2525 struct mnt_namespace *ns;
Al Virod31da0f2011-11-22 12:31:21 -05002526 struct super_block *s;
Al Viroea441d12011-11-16 21:43:59 -05002527 struct path path;
2528 int err;
2529
2530 ns = create_mnt_ns(mnt);
2531 if (IS_ERR(ns))
2532 return ERR_CAST(ns);
2533
2534 err = vfs_path_lookup(mnt->mnt_root, mnt,
2535 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2536
2537 put_mnt_ns(ns);
2538
2539 if (err)
2540 return ERR_PTR(err);
2541
2542 /* trade a vfsmount reference for active sb one */
Al Virod31da0f2011-11-22 12:31:21 -05002543 s = path.mnt->mnt_sb;
2544 atomic_inc(&s->s_active);
Al Viroea441d12011-11-16 21:43:59 -05002545 mntput(path.mnt);
2546 /* lock the sucker */
Al Virod31da0f2011-11-22 12:31:21 -05002547 down_write(&s->s_umount);
Al Viroea441d12011-11-16 21:43:59 -05002548 /* ... and return the root of (sub)tree on it */
2549 return path.dentry;
2550}
2551EXPORT_SYMBOL(mount_subtree);
2552
Heiko Carstensbdc480e2009-01-14 14:14:12 +01002553SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2554 char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Vegard Nossumeca6f532009-09-18 13:05:45 -07002556 int ret;
2557 char *kernel_type;
Jeff Layton91a27b22012-10-10 15:25:28 -04002558 struct filename *kernel_dir;
Vegard Nossumeca6f532009-09-18 13:05:45 -07002559 char *kernel_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 unsigned long data_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Vegard Nossumeca6f532009-09-18 13:05:45 -07002562 ret = copy_mount_string(type, &kernel_type);
2563 if (ret < 0)
2564 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Vegard Nossumeca6f532009-09-18 13:05:45 -07002566 kernel_dir = getname(dir_name);
2567 if (IS_ERR(kernel_dir)) {
2568 ret = PTR_ERR(kernel_dir);
2569 goto out_dir;
2570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Vegard Nossumeca6f532009-09-18 13:05:45 -07002572 ret = copy_mount_string(dev_name, &kernel_dev);
2573 if (ret < 0)
2574 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Vegard Nossumeca6f532009-09-18 13:05:45 -07002576 ret = copy_mount_options(data, &data_page);
2577 if (ret < 0)
2578 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Jeff Layton91a27b22012-10-10 15:25:28 -04002580 ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
Vegard Nossumeca6f532009-09-18 13:05:45 -07002581 (void *) data_page);
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 free_page(data_page);
Vegard Nossumeca6f532009-09-18 13:05:45 -07002584out_data:
2585 kfree(kernel_dev);
2586out_dev:
2587 putname(kernel_dir);
2588out_dir:
2589 kfree(kernel_type);
2590out_type:
2591 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
2594/*
Al Viroafac7cb2011-11-23 19:34:49 -05002595 * Return true if path is reachable from root
2596 *
2597 * namespace_sem or vfsmount_lock is held
2598 */
Al Viro643822b2011-11-24 22:00:28 -05002599bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
Al Viroafac7cb2011-11-23 19:34:49 -05002600 const struct path *root)
2601{
Al Viro643822b2011-11-24 22:00:28 -05002602 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
Al Viroa73324d2011-11-24 22:25:07 -05002603 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05002604 mnt = mnt->mnt_parent;
Al Viroafac7cb2011-11-23 19:34:49 -05002605 }
Al Viro643822b2011-11-24 22:00:28 -05002606 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
Al Viroafac7cb2011-11-23 19:34:49 -05002607}
2608
2609int path_is_under(struct path *path1, struct path *path2)
2610{
2611 int res;
Andi Kleen962830d2012-05-08 13:32:02 +09302612 br_read_lock(&vfsmount_lock);
Al Viro643822b2011-11-24 22:00:28 -05002613 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
Andi Kleen962830d2012-05-08 13:32:02 +09302614 br_read_unlock(&vfsmount_lock);
Al Viroafac7cb2011-11-23 19:34:49 -05002615 return res;
2616}
2617EXPORT_SYMBOL(path_is_under);
2618
2619/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 * pivot_root Semantics:
2621 * Moves the root file system of the current process to the directory put_old,
2622 * makes new_root as the new root file system of the current process, and sets
2623 * root/cwd of all processes which had them on the current root to new_root.
2624 *
2625 * Restrictions:
2626 * The new_root and put_old must be directories, and must not be on the
2627 * same file system as the current process root. The put_old must be
2628 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2629 * pointed to by put_old must yield the same directory as new_root. No other
2630 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2631 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08002632 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2633 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2634 * in this situation.
2635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 * Notes:
2637 * - we don't move root/cwd if they are not at the root (reason: if something
2638 * cared enough to change them, it's probably wrong to force them elsewhere)
2639 * - it's okay to pick a root that isn't the root of a file system, e.g.
2640 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2641 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2642 * first.
2643 */
Heiko Carstens3480b252009-01-14 14:14:16 +01002644SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2645 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Al Viro2d8f3032008-07-22 09:59:21 -04002647 struct path new, old, parent_path, root_parent, root;
Al Viro84d17192013-03-15 10:53:28 -04002648 struct mount *new_mnt, *root_mnt, *old_mnt;
2649 struct mountpoint *old_mp, *root_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 int error;
2651
Al Viro9b40bc92013-02-22 22:45:42 -05002652 if (!may_mount())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return -EPERM;
2654
Al Viro2d8f3032008-07-22 09:59:21 -04002655 error = user_path_dir(new_root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (error)
2657 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Al Viro2d8f3032008-07-22 09:59:21 -04002659 error = user_path_dir(put_old, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if (error)
2661 goto out1;
2662
Al Viro2d8f3032008-07-22 09:59:21 -04002663 error = security_sb_pivotroot(&old, &new);
Al Virob12cea92011-03-18 08:55:38 -04002664 if (error)
2665 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002667 get_fs_root(current->fs, &root);
Al Viro84d17192013-03-15 10:53:28 -04002668 old_mp = lock_mount(&old);
2669 error = PTR_ERR(old_mp);
2670 if (IS_ERR(old_mp))
Al Virob12cea92011-03-18 08:55:38 -04002671 goto out3;
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 error = -EINVAL;
Al Viro419148d2011-11-24 19:41:16 -05002674 new_mnt = real_mount(new.mnt);
2675 root_mnt = real_mount(root.mnt);
Al Viro84d17192013-03-15 10:53:28 -04002676 old_mnt = real_mount(old.mnt);
2677 if (IS_MNT_SHARED(old_mnt) ||
Al Virofc7be132011-11-25 01:05:37 -05002678 IS_MNT_SHARED(new_mnt->mnt_parent) ||
2679 IS_MNT_SHARED(root_mnt->mnt_parent))
Al Virob12cea92011-03-18 08:55:38 -04002680 goto out4;
Al Viro143c8c92011-11-25 00:46:35 -05002681 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002682 goto out4;
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002683 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
2684 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002686 if (d_unlinked(new.dentry))
Al Virob12cea92011-03-18 08:55:38 -04002687 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 error = -EBUSY;
Al Viro84d17192013-03-15 10:53:28 -04002689 if (new_mnt == root_mnt || old_mnt == root_mnt)
Al Virob12cea92011-03-18 08:55:38 -04002690 goto out4; /* loop, on the same file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04002692 if (root.mnt->mnt_root != root.dentry)
Al Virob12cea92011-03-18 08:55:38 -04002693 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05002694 if (!mnt_has_parent(root_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002695 goto out4; /* not attached */
Al Viro84d17192013-03-15 10:53:28 -04002696 root_mp = root_mnt->mnt_mp;
Al Viro2d8f3032008-07-22 09:59:21 -04002697 if (new.mnt->mnt_root != new.dentry)
Al Virob12cea92011-03-18 08:55:38 -04002698 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05002699 if (!mnt_has_parent(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002700 goto out4; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08002701 /* make sure we can reach put_old from new_root */
Al Viro84d17192013-03-15 10:53:28 -04002702 if (!is_path_reachable(old_mnt, old.dentry, &new))
Al Virob12cea92011-03-18 08:55:38 -04002703 goto out4;
Al Viro84d17192013-03-15 10:53:28 -04002704 root_mp->m_count++; /* pin it so it won't go away */
Andi Kleen962830d2012-05-08 13:32:02 +09302705 br_write_lock(&vfsmount_lock);
Al Viro419148d2011-11-24 19:41:16 -05002706 detach_mnt(new_mnt, &parent_path);
2707 detach_mnt(root_mnt, &root_parent);
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002708 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
2709 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
2710 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2711 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002712 /* mount old root on put_old */
Al Viro84d17192013-03-15 10:53:28 -04002713 attach_mnt(root_mnt, old_mnt, old_mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08002714 /* mount new_root on / */
Al Viro84d17192013-03-15 10:53:28 -04002715 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002716 touch_mnt_namespace(current->nsproxy->mnt_ns);
Andi Kleen962830d2012-05-08 13:32:02 +09302717 br_write_unlock(&vfsmount_lock);
Al Viro2d8f3032008-07-22 09:59:21 -04002718 chroot_fs_refs(&root, &new);
Al Viro84d17192013-03-15 10:53:28 -04002719 put_mountpoint(root_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 error = 0;
Al Virob12cea92011-03-18 08:55:38 -04002721out4:
Al Viro84d17192013-03-15 10:53:28 -04002722 unlock_mount(old_mp);
Al Virob12cea92011-03-18 08:55:38 -04002723 if (!error) {
2724 path_put(&root_parent);
2725 path_put(&parent_path);
2726 }
2727out3:
Al Viro8c3ee422008-03-22 18:00:39 -04002728 path_put(&root);
Al Virob12cea92011-03-18 08:55:38 -04002729out2:
Al Viro2d8f3032008-07-22 09:59:21 -04002730 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731out1:
Al Viro2d8f3032008-07-22 09:59:21 -04002732 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
2737static void __init init_mount_tree(void)
2738{
2739 struct vfsmount *mnt;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002740 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08002741 struct path root;
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002742 struct file_system_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002744 type = get_fs_type("rootfs");
2745 if (!type)
2746 panic("Can't find rootfs type");
2747 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
2748 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (IS_ERR(mnt))
2750 panic("Can't create rootfs");
Nick Pigginb3e19d92011-01-07 17:50:11 +11002751
Trond Myklebust3b22edc2009-06-23 17:29:49 -04002752 ns = create_mnt_ns(mnt);
2753 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 panic("Can't allocate initial namespace");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002756 init_task.nsproxy->mnt_ns = ns;
2757 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Al Virobe08d6d2011-12-06 13:32:36 -05002759 root.mnt = mnt;
2760 root.dentry = mnt->mnt_root;
Jan Blunckac748a02008-02-14 19:34:39 -08002761
2762 set_fs_pwd(current->fs, &root);
2763 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Denis Cheng74bf17c2007-10-16 23:26:30 -07002766void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Eric Dumazet13f14b42008-02-06 01:37:57 -08002768 unsigned u;
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002769 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Ram Pai390c6842005-11-07 17:17:51 -05002771 init_rwsem(&namespace_sem);
2772
Al Viro7d6fec42011-11-23 12:14:10 -05002773 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
Paul Mundt20c2df82007-07-20 10:11:58 +09002774 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Ram Paib58fed82005-11-07 17:16:09 -05002776 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
Al Viro84d17192013-03-15 10:53:28 -04002777 mountpoint_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Al Viro84d17192013-03-15 10:53:28 -04002779 if (!mount_hashtable || !mountpoint_hashtable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 panic("Failed to allocate mount hash table\n");
2781
Mandeep Singh Baines80cdc6d2011-03-22 16:33:54 -07002782 printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Eric Dumazet13f14b42008-02-06 01:37:57 -08002784 for (u = 0; u < HASH_SIZE; u++)
2785 INIT_LIST_HEAD(&mount_hashtable[u]);
Al Viro84d17192013-03-15 10:53:28 -04002786 for (u = 0; u < HASH_SIZE; u++)
2787 INIT_LIST_HEAD(&mountpoint_hashtable[u]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Andi Kleen962830d2012-05-08 13:32:02 +09302789 br_lock_init(&vfsmount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +10002790
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002791 err = sysfs_init();
2792 if (err)
2793 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002794 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06002795 fs_kobj = kobject_create_and_add("fs", NULL);
2796 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002797 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 init_rootfs();
2799 init_mount_tree();
2800}
2801
Trond Myklebust616511d2009-06-22 15:09:13 -04002802void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Al Virod498b252010-02-05 02:21:06 -05002804 if (!atomic_dec_and_test(&ns->count))
Trond Myklebust616511d2009-06-22 15:09:13 -04002805 return;
Al Viro97216be2013-03-16 15:12:40 -04002806 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302807 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04002808 umount_tree(ns->root, 0);
Andi Kleen962830d2012-05-08 13:32:02 +09302809 br_write_unlock(&vfsmount_lock);
Al Viro3ab6abe2013-03-16 14:42:19 -04002810 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07002811 free_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
Al Viro9d412a42011-03-17 22:08:28 -04002813
2814struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
2815{
Tim Chen423e0ab2011-07-19 09:32:38 -07002816 struct vfsmount *mnt;
2817 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2818 if (!IS_ERR(mnt)) {
2819 /*
2820 * it is a longterm mount, don't release mnt until
2821 * we unmount before file sys is unregistered
2822 */
Al Virof7a99c52012-06-09 00:59:08 -04002823 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
Tim Chen423e0ab2011-07-19 09:32:38 -07002824 }
2825 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04002826}
2827EXPORT_SYMBOL_GPL(kern_mount_data);
Tim Chen423e0ab2011-07-19 09:32:38 -07002828
2829void kern_unmount(struct vfsmount *mnt)
2830{
2831 /* release long term mount so mount point can be released */
2832 if (!IS_ERR_OR_NULL(mnt)) {
Al Virof7a99c52012-06-09 00:59:08 -04002833 br_write_lock(&vfsmount_lock);
2834 real_mount(mnt)->mnt_ns = NULL;
2835 br_write_unlock(&vfsmount_lock);
Tim Chen423e0ab2011-07-19 09:32:38 -07002836 mntput(mnt);
2837 }
2838}
2839EXPORT_SYMBOL(kern_unmount);
Al Viro02125a82011-12-05 08:43:34 -05002840
2841bool our_mnt(struct vfsmount *mnt)
2842{
Al Viro143c8c92011-11-25 00:46:35 -05002843 return check_mnt(real_mount(mnt));
Al Viro02125a82011-12-05 08:43:34 -05002844}
Eric W. Biederman8823c072010-03-07 18:49:36 -08002845
Eric W. Biederman31515272013-03-15 01:45:51 -07002846bool current_chrooted(void)
2847{
2848 /* Does the current process have a non-standard root */
2849 struct path ns_root;
2850 struct path fs_root;
2851 bool chrooted;
2852
2853 /* Find the namespace root */
2854 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
2855 ns_root.dentry = ns_root.mnt->mnt_root;
2856 path_get(&ns_root);
2857 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
2858 ;
2859
2860 get_fs_root(current->fs, &fs_root);
2861
2862 chrooted = !path_equal(&fs_root, &ns_root);
2863
2864 path_put(&fs_root);
2865 path_put(&ns_root);
2866
2867 return chrooted;
2868}
2869
Eric W. Biedermane51db732013-03-30 19:57:41 -07002870bool fs_fully_visible(struct file_system_type *type)
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07002871{
2872 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
2873 struct mount *mnt;
Eric W. Biedermane51db732013-03-30 19:57:41 -07002874 bool visible = false;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07002875
Eric W. Biedermane51db732013-03-30 19:57:41 -07002876 if (unlikely(!ns))
2877 return false;
2878
2879 namespace_lock();
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07002880 list_for_each_entry(mnt, &ns->list, mnt_list) {
Eric W. Biedermane51db732013-03-30 19:57:41 -07002881 struct mount *child;
2882 if (mnt->mnt.mnt_sb->s_type != type)
2883 continue;
2884
2885 /* This mount is not fully visible if there are any child mounts
2886 * that cover anything except for empty directories.
2887 */
2888 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2889 struct inode *inode = child->mnt_mountpoint->d_inode;
2890 if (!S_ISDIR(inode->i_mode))
2891 goto next;
2892 if (inode->i_nlink != 2)
2893 goto next;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07002894 }
Eric W. Biedermane51db732013-03-30 19:57:41 -07002895 visible = true;
2896 goto found;
2897 next: ;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07002898 }
Eric W. Biedermane51db732013-03-30 19:57:41 -07002899found:
2900 namespace_unlock();
2901 return visible;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07002902}
2903
Eric W. Biederman8823c072010-03-07 18:49:36 -08002904static void *mntns_get(struct task_struct *task)
2905{
2906 struct mnt_namespace *ns = NULL;
2907 struct nsproxy *nsproxy;
2908
2909 rcu_read_lock();
2910 nsproxy = task_nsproxy(task);
2911 if (nsproxy) {
2912 ns = nsproxy->mnt_ns;
2913 get_mnt_ns(ns);
2914 }
2915 rcu_read_unlock();
2916
2917 return ns;
2918}
2919
2920static void mntns_put(void *ns)
2921{
2922 put_mnt_ns(ns);
2923}
2924
2925static int mntns_install(struct nsproxy *nsproxy, void *ns)
2926{
2927 struct fs_struct *fs = current->fs;
2928 struct mnt_namespace *mnt_ns = ns;
2929 struct path root;
2930
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002931 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07002932 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
2933 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Zhao Hongjiangae11e0f2012-09-13 16:38:03 +08002934 return -EPERM;
Eric W. Biederman8823c072010-03-07 18:49:36 -08002935
2936 if (fs->users != 1)
2937 return -EINVAL;
2938
2939 get_mnt_ns(mnt_ns);
2940 put_mnt_ns(nsproxy->mnt_ns);
2941 nsproxy->mnt_ns = mnt_ns;
2942
2943 /* Find the root */
2944 root.mnt = &mnt_ns->root->mnt;
2945 root.dentry = mnt_ns->root->mnt.mnt_root;
2946 path_get(&root);
2947 while(d_mountpoint(root.dentry) && follow_down_one(&root))
2948 ;
2949
2950 /* Update the pwd and root */
2951 set_fs_pwd(fs, &root);
2952 set_fs_root(fs, &root);
2953
2954 path_put(&root);
2955 return 0;
2956}
2957
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002958static unsigned int mntns_inum(void *ns)
2959{
2960 struct mnt_namespace *mnt_ns = ns;
2961 return mnt_ns->proc_inum;
2962}
2963
Eric W. Biederman8823c072010-03-07 18:49:36 -08002964const struct proc_ns_operations mntns_operations = {
2965 .name = "mnt",
2966 .type = CLONE_NEWNS,
2967 .get = mntns_get,
2968 .put = mntns_put,
2969 .install = mntns_install,
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002970 .inum = mntns_inum,
Eric W. Biederman8823c072010-03-07 18:49:36 -08002971};