blob: 1766bc9a4e31dd7c8d77606aee15f02fccbaf9bb [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>
Thomas Gleixner3c536052012-03-07 21:00:34 +010026#include <linux/delay.h>
Ram Pai07b20882005-11-07 17:19:07 -050027#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070028#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Eric Dumazet13f14b42008-02-06 01:37:57 -080030#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
31#define HASH_SIZE (1UL << HASH_SHIFT)
32
Al Viro5addc5d2005-11-07 17:15:49 -050033static int event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010034static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010035static DEFINE_IDA(mnt_group_ida);
Nick Piggin99b7db72010-08-18 04:37:39 +100036static DEFINE_SPINLOCK(mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040037static int mnt_id_start = 0;
38static int mnt_group_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Eric Dumazetfa3536c2006-03-26 01:37:24 -080040static struct list_head *mount_hashtable __read_mostly;
Al Viro84d17192013-03-15 10:53:28 -040041static struct list_head *mountpoint_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080042static struct kmem_cache *mnt_cache __read_mostly;
Ram Pai390c6842005-11-07 17:17:51 -050043static struct rw_semaphore namespace_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080045/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060046struct kobject *fs_kobj;
47EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080048
Nick Piggin99b7db72010-08-18 04:37:39 +100049/*
50 * vfsmount lock may be taken for read to prevent changes to the
51 * vfsmount hash, ie. during mountpoint lookups or walking back
52 * up the tree.
53 *
54 * It should be taken for write in all cases where the vfsmount
55 * tree or hash is modified or when a vfsmount structure is modified.
56 */
57DEFINE_BRLOCK(vfsmount_lock);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
60{
Ram Paib58fed82005-11-07 17:16:09 -050061 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
62 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Eric Dumazet13f14b42008-02-06 01:37:57 -080063 tmp = tmp + (tmp >> HASH_SHIFT);
64 return tmp & (HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Dave Hansen3d733632008-02-15 14:37:59 -080067#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
68
Nick Piggin99b7db72010-08-18 04:37:39 +100069/*
70 * allocation is serialized by namespace_sem, but we need the spinlock to
71 * serialize with freeing.
72 */
Al Virob105e272011-11-24 20:38:33 -050073static int mnt_alloc_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010074{
75 int res;
76
77retry:
78 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
Nick Piggin99b7db72010-08-18 04:37:39 +100079 spin_lock(&mnt_id_lock);
Al Viro15169fe2011-11-25 00:50:41 -050080 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
Al Virof21f6222009-06-24 03:12:00 -040081 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -050082 mnt_id_start = mnt->mnt_id + 1;
Nick Piggin99b7db72010-08-18 04:37:39 +100083 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010084 if (res == -EAGAIN)
85 goto retry;
86
87 return res;
88}
89
Al Virob105e272011-11-24 20:38:33 -050090static void mnt_free_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010091{
Al Viro15169fe2011-11-25 00:50:41 -050092 int id = mnt->mnt_id;
Nick Piggin99b7db72010-08-18 04:37:39 +100093 spin_lock(&mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040094 ida_remove(&mnt_id_ida, id);
95 if (mnt_id_start > id)
96 mnt_id_start = id;
Nick Piggin99b7db72010-08-18 04:37:39 +100097 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010098}
99
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100100/*
101 * Allocate a new peer group ID
102 *
103 * mnt_group_ida is protected by namespace_sem
104 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500105static int mnt_alloc_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100106{
Al Virof21f6222009-06-24 03:12:00 -0400107 int res;
108
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100109 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
110 return -ENOMEM;
111
Al Virof21f6222009-06-24 03:12:00 -0400112 res = ida_get_new_above(&mnt_group_ida,
113 mnt_group_start,
Al Viro15169fe2011-11-25 00:50:41 -0500114 &mnt->mnt_group_id);
Al Virof21f6222009-06-24 03:12:00 -0400115 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -0500116 mnt_group_start = mnt->mnt_group_id + 1;
Al Virof21f6222009-06-24 03:12:00 -0400117
118 return res;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100119}
120
121/*
122 * Release a peer group ID
123 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500124void mnt_release_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100125{
Al Viro15169fe2011-11-25 00:50:41 -0500126 int id = mnt->mnt_group_id;
Al Virof21f6222009-06-24 03:12:00 -0400127 ida_remove(&mnt_group_ida, id);
128 if (mnt_group_start > id)
129 mnt_group_start = id;
Al Viro15169fe2011-11-25 00:50:41 -0500130 mnt->mnt_group_id = 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100131}
132
Nick Pigginb3e19d92011-01-07 17:50:11 +1100133/*
134 * vfsmount lock must be held for read
135 */
Al Viro83adc752011-11-24 22:37:54 -0500136static inline void mnt_add_count(struct mount *mnt, int n)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100137{
138#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500139 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100140#else
141 preempt_disable();
Al Viro68e8a9f2011-11-24 22:53:09 -0500142 mnt->mnt_count += n;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100143 preempt_enable();
144#endif
145}
146
Nick Pigginb3e19d92011-01-07 17:50:11 +1100147/*
148 * vfsmount lock must be held for write
149 */
Al Viro83adc752011-11-24 22:37:54 -0500150unsigned int mnt_get_count(struct mount *mnt)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100151{
152#ifdef CONFIG_SMP
Al Virof03c6592011-01-14 22:30:21 -0500153 unsigned int count = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100154 int cpu;
155
156 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500157 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100158 }
159
160 return count;
161#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500162 return mnt->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100163#endif
164}
165
Al Virob105e272011-11-24 20:38:33 -0500166static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Al Viroc63181e2011-11-25 02:35:16 -0500168 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
169 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100170 int err;
171
Al Viroc63181e2011-11-25 02:35:16 -0500172 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800173 if (err)
174 goto out_free_cache;
175
176 if (name) {
Al Viroc63181e2011-11-25 02:35:16 -0500177 mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
178 if (!mnt->mnt_devname)
Li Zefan88b38782008-07-21 18:06:36 +0800179 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100180 }
181
Nick Pigginb3e19d92011-01-07 17:50:11 +1100182#ifdef CONFIG_SMP
Al Viroc63181e2011-11-25 02:35:16 -0500183 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
184 if (!mnt->mnt_pcp)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100185 goto out_free_devname;
186
Al Viroc63181e2011-11-25 02:35:16 -0500187 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100188#else
Al Viroc63181e2011-11-25 02:35:16 -0500189 mnt->mnt_count = 1;
190 mnt->mnt_writers = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100191#endif
192
Al Viroc63181e2011-11-25 02:35:16 -0500193 INIT_LIST_HEAD(&mnt->mnt_hash);
194 INIT_LIST_HEAD(&mnt->mnt_child);
195 INIT_LIST_HEAD(&mnt->mnt_mounts);
196 INIT_LIST_HEAD(&mnt->mnt_list);
197 INIT_LIST_HEAD(&mnt->mnt_expire);
198 INIT_LIST_HEAD(&mnt->mnt_share);
199 INIT_LIST_HEAD(&mnt->mnt_slave_list);
200 INIT_LIST_HEAD(&mnt->mnt_slave);
Andreas Gruenbacher2504c5d2009-12-17 21:24:27 -0500201#ifdef CONFIG_FSNOTIFY
202 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Al Viroc63181e2011-11-25 02:35:16 -0500205 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800206
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000207#ifdef CONFIG_SMP
208out_free_devname:
Al Viroc63181e2011-11-25 02:35:16 -0500209 kfree(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000210#endif
Li Zefan88b38782008-07-21 18:06:36 +0800211out_free_id:
Al Viroc63181e2011-11-25 02:35:16 -0500212 mnt_free_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800213out_free_cache:
Al Viroc63181e2011-11-25 02:35:16 -0500214 kmem_cache_free(mnt_cache, mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800215 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Dave Hansen83660252008-02-15 14:37:30 -0800218/*
219 * Most r/o checks on a fs are for operations that take
220 * discrete amounts of time, like a write() or unlink().
221 * We must keep track of when those operations start
222 * (for permission checks) and when they end, so that
223 * we can determine when writes are able to occur to
224 * a filesystem.
225 */
Dave Hansen3d733632008-02-15 14:37:59 -0800226/*
227 * __mnt_is_readonly: check whether a mount is read-only
228 * @mnt: the mount to check for its write status
229 *
230 * This shouldn't be used directly ouside of the VFS.
231 * It does not guarantee that the filesystem will stay
232 * r/w, just that it is right *now*. This can not and
233 * should not be used in place of IS_RDONLY(inode).
234 * mnt_want/drop_write() will _keep_ the filesystem
235 * r/w.
236 */
237int __mnt_is_readonly(struct vfsmount *mnt)
238{
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800239 if (mnt->mnt_flags & MNT_READONLY)
240 return 1;
241 if (mnt->mnt_sb->s_flags & MS_RDONLY)
242 return 1;
243 return 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800244}
245EXPORT_SYMBOL_GPL(__mnt_is_readonly);
246
Al Viro83adc752011-11-24 22:37:54 -0500247static inline void mnt_inc_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800248{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000249#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500250 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000251#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500252 mnt->mnt_writers++;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000253#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800254}
Dave Hansen3d733632008-02-15 14:37:59 -0800255
Al Viro83adc752011-11-24 22:37:54 -0500256static inline void mnt_dec_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800257{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000258#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500259 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000260#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500261 mnt->mnt_writers--;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000262#endif
263}
264
Al Viro83adc752011-11-24 22:37:54 -0500265static unsigned int mnt_get_writers(struct mount *mnt)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000266{
267#ifdef CONFIG_SMP
268 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800269 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800270
271 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500272 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
Dave Hansen3d733632008-02-15 14:37:59 -0800273 }
Dave Hansen3d733632008-02-15 14:37:59 -0800274
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000275 return count;
276#else
277 return mnt->mnt_writers;
278#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800279}
280
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100281static int mnt_is_readonly(struct vfsmount *mnt)
282{
283 if (mnt->mnt_sb->s_readonly_remount)
284 return 1;
285 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
286 smp_rmb();
287 return __mnt_is_readonly(mnt);
288}
289
Dave Hansen3d733632008-02-15 14:37:59 -0800290/*
Jan Karaeb04c282012-06-12 16:20:35 +0200291 * Most r/o & frozen checks on a fs are for operations that take discrete
292 * amounts of time, like a write() or unlink(). We must keep track of when
293 * those operations start (for permission checks) and when they end, so that we
294 * can determine when writes are able to occur to a filesystem.
Dave Hansen3d733632008-02-15 14:37:59 -0800295 */
Dave Hansen83660252008-02-15 14:37:30 -0800296/**
Jan Karaeb04c282012-06-12 16:20:35 +0200297 * __mnt_want_write - get write access to a mount without freeze protection
Al Viro83adc752011-11-24 22:37:54 -0500298 * @m: the mount on which to take a write
Dave Hansen83660252008-02-15 14:37:30 -0800299 *
Jan Karaeb04c282012-06-12 16:20:35 +0200300 * This tells the low-level filesystem that a write is about to be performed to
301 * it, and makes sure that writes are allowed (mnt it read-write) before
302 * returning success. This operation does not protect against filesystem being
303 * frozen. When the write operation is finished, __mnt_drop_write() must be
304 * called. This is effectively a refcount.
Dave Hansen83660252008-02-15 14:37:30 -0800305 */
Jan Karaeb04c282012-06-12 16:20:35 +0200306int __mnt_want_write(struct vfsmount *m)
Dave Hansen83660252008-02-15 14:37:30 -0800307{
Al Viro83adc752011-11-24 22:37:54 -0500308 struct mount *mnt = real_mount(m);
Dave Hansen3d733632008-02-15 14:37:59 -0800309 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800310
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000311 preempt_disable();
Nick Pigginc6653a82011-01-07 17:50:10 +1100312 mnt_inc_writers(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000313 /*
Nick Pigginc6653a82011-01-07 17:50:10 +1100314 * The store to mnt_inc_writers must be visible before we pass
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000315 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
316 * incremented count after it has set MNT_WRITE_HOLD.
317 */
318 smp_mb();
Thomas Gleixner90351032009-07-19 08:44:27 -0500319 while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
320 preempt_enable();
Thomas Gleixner3c536052012-03-07 21:00:34 +0100321 cpu_chill();
Thomas Gleixner90351032009-07-19 08:44:27 -0500322 preempt_disable();
323 }
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000324 /*
325 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
326 * be set to match its requirements. So we must not load that until
327 * MNT_WRITE_HOLD is cleared.
328 */
329 smp_rmb();
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100330 if (mnt_is_readonly(m)) {
Nick Pigginc6653a82011-01-07 17:50:10 +1100331 mnt_dec_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800332 ret = -EROFS;
Dave Hansen3d733632008-02-15 14:37:59 -0800333 }
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000334 preempt_enable();
Jan Karaeb04c282012-06-12 16:20:35 +0200335
336 return ret;
337}
338
339/**
340 * mnt_want_write - get write access to a mount
341 * @m: the mount on which to take a write
342 *
343 * This tells the low-level filesystem that a write is about to be performed to
344 * it, and makes sure that writes are allowed (mount is read-write, filesystem
345 * is not frozen) before returning success. When the write operation is
346 * finished, mnt_drop_write() must be called. This is effectively a refcount.
347 */
348int mnt_want_write(struct vfsmount *m)
349{
350 int ret;
351
352 sb_start_write(m->mnt_sb);
353 ret = __mnt_want_write(m);
354 if (ret)
355 sb_end_write(m->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800356 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800357}
358EXPORT_SYMBOL_GPL(mnt_want_write);
359
360/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000361 * mnt_clone_write - get write access to a mount
362 * @mnt: the mount on which to take a write
363 *
364 * This is effectively like mnt_want_write, except
365 * it must only be used to take an extra write reference
366 * on a mountpoint that we already know has a write reference
367 * on it. This allows some optimisation.
368 *
369 * After finished, mnt_drop_write must be called as usual to
370 * drop the reference.
371 */
372int mnt_clone_write(struct vfsmount *mnt)
373{
374 /* superblock may be r/o */
375 if (__mnt_is_readonly(mnt))
376 return -EROFS;
377 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500378 mnt_inc_writers(real_mount(mnt));
npiggin@suse.de96029c42009-04-26 20:25:55 +1000379 preempt_enable();
380 return 0;
381}
382EXPORT_SYMBOL_GPL(mnt_clone_write);
383
384/**
Jan Karaeb04c282012-06-12 16:20:35 +0200385 * __mnt_want_write_file - get write access to a file's mount
386 * @file: the file who's mount on which to take a write
387 *
388 * This is like __mnt_want_write, but it takes a file and can
389 * do some optimisations if the file is open for write already
390 */
391int __mnt_want_write_file(struct file *file)
392{
Al Viro496ad9a2013-01-23 17:07:38 -0500393 struct inode *inode = file_inode(file);
Jan Karaeb04c282012-06-12 16:20:35 +0200394
395 if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
396 return __mnt_want_write(file->f_path.mnt);
397 else
398 return mnt_clone_write(file->f_path.mnt);
399}
400
401/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000402 * mnt_want_write_file - get write access to a file's mount
403 * @file: the file who's mount on which to take a write
404 *
405 * This is like mnt_want_write, but it takes a file and can
406 * do some optimisations if the file is open for write already
407 */
408int mnt_want_write_file(struct file *file)
409{
Jan Karaeb04c282012-06-12 16:20:35 +0200410 int ret;
411
412 sb_start_write(file->f_path.mnt->mnt_sb);
413 ret = __mnt_want_write_file(file);
414 if (ret)
415 sb_end_write(file->f_path.mnt->mnt_sb);
416 return ret;
npiggin@suse.de96029c42009-04-26 20:25:55 +1000417}
418EXPORT_SYMBOL_GPL(mnt_want_write_file);
419
420/**
Jan Karaeb04c282012-06-12 16:20:35 +0200421 * __mnt_drop_write - give up write access to a mount
Dave Hansen83660252008-02-15 14:37:30 -0800422 * @mnt: the mount on which to give up write access
423 *
424 * Tells the low-level filesystem that we are done
425 * performing writes to it. Must be matched with
Jan Karaeb04c282012-06-12 16:20:35 +0200426 * __mnt_want_write() call above.
Dave Hansen83660252008-02-15 14:37:30 -0800427 */
Jan Karaeb04c282012-06-12 16:20:35 +0200428void __mnt_drop_write(struct vfsmount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800429{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000430 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500431 mnt_dec_writers(real_mount(mnt));
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000432 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800433}
Jan Karaeb04c282012-06-12 16:20:35 +0200434
435/**
436 * mnt_drop_write - give up write access to a mount
437 * @mnt: the mount on which to give up write access
438 *
439 * Tells the low-level filesystem that we are done performing writes to it and
440 * also allows filesystem to be frozen again. Must be matched with
441 * mnt_want_write() call above.
442 */
443void mnt_drop_write(struct vfsmount *mnt)
444{
445 __mnt_drop_write(mnt);
446 sb_end_write(mnt->mnt_sb);
447}
Dave Hansen83660252008-02-15 14:37:30 -0800448EXPORT_SYMBOL_GPL(mnt_drop_write);
449
Jan Karaeb04c282012-06-12 16:20:35 +0200450void __mnt_drop_write_file(struct file *file)
451{
452 __mnt_drop_write(file->f_path.mnt);
453}
454
Al Viro2a79f172011-12-09 08:06:57 -0500455void mnt_drop_write_file(struct file *file)
456{
457 mnt_drop_write(file->f_path.mnt);
458}
459EXPORT_SYMBOL(mnt_drop_write_file);
460
Al Viro83adc752011-11-24 22:37:54 -0500461static int mnt_make_readonly(struct mount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800462{
Dave Hansen3d733632008-02-15 14:37:59 -0800463 int ret = 0;
464
Andi Kleen962830d2012-05-08 13:32:02 +0930465 br_write_lock(&vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -0500466 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000467 /*
468 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
469 * should be visible before we do.
470 */
471 smp_mb();
472
473 /*
474 * With writers on hold, if this value is zero, then there are
475 * definitely no active writers (although held writers may subsequently
476 * increment the count, they'll have to wait, and decrement it after
477 * seeing MNT_READONLY).
478 *
479 * It is OK to have counter incremented on one CPU and decremented on
480 * another: the sum will add up correctly. The danger would be when we
481 * sum up each counter, if we read a counter before it is incremented,
482 * but then read another CPU's count which it has been subsequently
483 * decremented from -- we would see more decrements than we should.
484 * MNT_WRITE_HOLD protects against this scenario, because
485 * mnt_want_write first increments count, then smp_mb, then spins on
486 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
487 * we're counting up here.
488 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100489 if (mnt_get_writers(mnt) > 0)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000490 ret = -EBUSY;
491 else
Al Viro83adc752011-11-24 22:37:54 -0500492 mnt->mnt.mnt_flags |= MNT_READONLY;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000493 /*
494 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
495 * that become unheld will see MNT_READONLY.
496 */
497 smp_wmb();
Al Viro83adc752011-11-24 22:37:54 -0500498 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
Andi Kleen962830d2012-05-08 13:32:02 +0930499 br_write_unlock(&vfsmount_lock);
Dave Hansen3d733632008-02-15 14:37:59 -0800500 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800501}
Dave Hansen83660252008-02-15 14:37:30 -0800502
Al Viro83adc752011-11-24 22:37:54 -0500503static void __mnt_unmake_readonly(struct mount *mnt)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800504{
Andi Kleen962830d2012-05-08 13:32:02 +0930505 br_write_lock(&vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -0500506 mnt->mnt.mnt_flags &= ~MNT_READONLY;
Andi Kleen962830d2012-05-08 13:32:02 +0930507 br_write_unlock(&vfsmount_lock);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800508}
509
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100510int sb_prepare_remount_readonly(struct super_block *sb)
511{
512 struct mount *mnt;
513 int err = 0;
514
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100515 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
516 if (atomic_long_read(&sb->s_remove_count))
517 return -EBUSY;
518
Andi Kleen962830d2012-05-08 13:32:02 +0930519 br_write_lock(&vfsmount_lock);
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100520 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
521 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
522 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
523 smp_mb();
524 if (mnt_get_writers(mnt) > 0) {
525 err = -EBUSY;
526 break;
527 }
528 }
529 }
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100530 if (!err && atomic_long_read(&sb->s_remove_count))
531 err = -EBUSY;
532
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100533 if (!err) {
534 sb->s_readonly_remount = 1;
535 smp_wmb();
536 }
537 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
538 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
539 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
540 }
Andi Kleen962830d2012-05-08 13:32:02 +0930541 br_write_unlock(&vfsmount_lock);
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100542
543 return err;
544}
545
Al Virob105e272011-11-24 20:38:33 -0500546static void free_vfsmnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Al Viro52ba1622011-11-25 02:25:17 -0500548 kfree(mnt->mnt_devname);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100549 mnt_free_id(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000550#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500551 free_percpu(mnt->mnt_pcp);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000552#endif
Al Virob105e272011-11-24 20:38:33 -0500553 kmem_cache_free(mnt_cache, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556/*
Ram Paia05964f2005-11-07 17:20:17 -0500557 * find the first or last mount at @dentry on vfsmount @mnt depending on
558 * @dir. If @dir is set return the first mount else return the last mount.
Nick Piggin99b7db72010-08-18 04:37:39 +1000559 * vfsmount_lock must be held for read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Al Viroc7105362011-11-24 18:22:03 -0500561struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
Ram Paia05964f2005-11-07 17:20:17 -0500562 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Ram Paib58fed82005-11-07 17:16:09 -0500564 struct list_head *head = mount_hashtable + hash(mnt, dentry);
565 struct list_head *tmp = head;
Al Viroc7105362011-11-24 18:22:03 -0500566 struct mount *p, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 for (;;) {
Ram Paia05964f2005-11-07 17:20:17 -0500569 tmp = dir ? tmp->next : tmp->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 p = NULL;
571 if (tmp == head)
572 break;
Al Viro1b8e5562011-11-24 21:01:32 -0500573 p = list_entry(tmp, struct mount, mnt_hash);
Al Viroa73324d2011-11-24 22:25:07 -0500574 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
Ram Paia05964f2005-11-07 17:20:17 -0500575 found = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 }
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return found;
580}
581
Ram Paia05964f2005-11-07 17:20:17 -0500582/*
David Howellsf015f1262012-06-25 12:55:28 +0100583 * lookup_mnt - Return the first child mount mounted at path
584 *
585 * "First" means first mounted chronologically. If you create the
586 * following mounts:
587 *
588 * mount /dev/sda1 /mnt
589 * mount /dev/sda2 /mnt
590 * mount /dev/sda3 /mnt
591 *
592 * Then lookup_mnt() on the base /mnt dentry in the root mount will
593 * return successively the root dentry and vfsmount of /dev/sda1, then
594 * /dev/sda2, then /dev/sda3, then NULL.
595 *
596 * lookup_mnt takes a reference to the found vfsmount.
Ram Paia05964f2005-11-07 17:20:17 -0500597 */
Al Viro1c755af2009-04-18 14:06:57 -0400598struct vfsmount *lookup_mnt(struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500599{
Al Viroc7105362011-11-24 18:22:03 -0500600 struct mount *child_mnt;
Nick Piggin99b7db72010-08-18 04:37:39 +1000601
Andi Kleen962830d2012-05-08 13:32:02 +0930602 br_read_lock(&vfsmount_lock);
Al Viroc7105362011-11-24 18:22:03 -0500603 child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
604 if (child_mnt) {
605 mnt_add_count(child_mnt, 1);
Andi Kleen962830d2012-05-08 13:32:02 +0930606 br_read_unlock(&vfsmount_lock);
Al Viroc7105362011-11-24 18:22:03 -0500607 return &child_mnt->mnt;
608 } else {
Andi Kleen962830d2012-05-08 13:32:02 +0930609 br_read_unlock(&vfsmount_lock);
Al Viroc7105362011-11-24 18:22:03 -0500610 return NULL;
611 }
Ram Paia05964f2005-11-07 17:20:17 -0500612}
613
Al Viro84d17192013-03-15 10:53:28 -0400614static struct mountpoint *new_mountpoint(struct dentry *dentry)
615{
616 struct list_head *chain = mountpoint_hashtable + hash(NULL, dentry);
617 struct mountpoint *mp;
618
619 list_for_each_entry(mp, chain, m_hash) {
620 if (mp->m_dentry == dentry) {
621 /* might be worth a WARN_ON() */
622 if (d_unlinked(dentry))
623 return ERR_PTR(-ENOENT);
624 mp->m_count++;
625 return mp;
626 }
627 }
628
629 mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
630 if (!mp)
631 return ERR_PTR(-ENOMEM);
632
633 spin_lock(&dentry->d_lock);
634 if (d_unlinked(dentry)) {
635 spin_unlock(&dentry->d_lock);
636 kfree(mp);
637 return ERR_PTR(-ENOENT);
638 }
639 dentry->d_flags |= DCACHE_MOUNTED;
640 spin_unlock(&dentry->d_lock);
641 mp->m_dentry = dentry;
642 mp->m_count = 1;
643 list_add(&mp->m_hash, chain);
644 return mp;
645}
646
647static void put_mountpoint(struct mountpoint *mp)
648{
649 if (!--mp->m_count) {
650 struct dentry *dentry = mp->m_dentry;
651 spin_lock(&dentry->d_lock);
652 dentry->d_flags &= ~DCACHE_MOUNTED;
653 spin_unlock(&dentry->d_lock);
654 list_del(&mp->m_hash);
655 kfree(mp);
656 }
657}
658
Al Viro143c8c92011-11-25 00:46:35 -0500659static inline int check_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800661 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Nick Piggin99b7db72010-08-18 04:37:39 +1000664/*
665 * vfsmount lock must be held for write
666 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800667static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500668{
669 if (ns) {
670 ns->event = ++event;
671 wake_up_interruptible(&ns->poll);
672 }
673}
674
Nick Piggin99b7db72010-08-18 04:37:39 +1000675/*
676 * vfsmount lock must be held for write
677 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800678static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500679{
680 if (ns && ns->event != event) {
681 ns->event = event;
682 wake_up_interruptible(&ns->poll);
683 }
684}
685
Nick Piggin99b7db72010-08-18 04:37:39 +1000686/*
687 * vfsmount lock must be held for write
688 */
Al Viro419148d2011-11-24 19:41:16 -0500689static void detach_mnt(struct mount *mnt, struct path *old_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Al Viroa73324d2011-11-24 22:25:07 -0500691 old_path->dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500692 old_path->mnt = &mnt->mnt_parent->mnt;
693 mnt->mnt_parent = mnt;
Al Viroa73324d2011-11-24 22:25:07 -0500694 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro6b41d532011-11-24 23:24:33 -0500695 list_del_init(&mnt->mnt_child);
Al Viro1b8e5562011-11-24 21:01:32 -0500696 list_del_init(&mnt->mnt_hash);
Al Viro84d17192013-03-15 10:53:28 -0400697 put_mountpoint(mnt->mnt_mp);
698 mnt->mnt_mp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Nick Piggin99b7db72010-08-18 04:37:39 +1000701/*
702 * vfsmount lock must be held for write
703 */
Al Viro84d17192013-03-15 10:53:28 -0400704void mnt_set_mountpoint(struct mount *mnt,
705 struct mountpoint *mp,
Al Viro44d964d62011-11-24 21:28:22 -0500706 struct mount *child_mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500707{
Al Viro84d17192013-03-15 10:53:28 -0400708 mp->m_count++;
Al Viro3a2393d2011-11-25 03:19:09 -0500709 mnt_add_count(mnt, 1); /* essentially, that's mntget */
Al Viro84d17192013-03-15 10:53:28 -0400710 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
Al Viro3a2393d2011-11-25 03:19:09 -0500711 child_mnt->mnt_parent = mnt;
Al Viro84d17192013-03-15 10:53:28 -0400712 child_mnt->mnt_mp = mp;
Ram Paib90fa9a2005-11-07 17:19:50 -0500713}
714
Nick Piggin99b7db72010-08-18 04:37:39 +1000715/*
716 * vfsmount lock must be held for write
717 */
Al Viro84d17192013-03-15 10:53:28 -0400718static void attach_mnt(struct mount *mnt,
719 struct mount *parent,
720 struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Al Viro84d17192013-03-15 10:53:28 -0400722 mnt_set_mountpoint(parent, mp, mnt);
Al Viro1b8e5562011-11-24 21:01:32 -0500723 list_add_tail(&mnt->mnt_hash, mount_hashtable +
Al Viro84d17192013-03-15 10:53:28 -0400724 hash(&parent->mnt, mp->m_dentry));
725 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
Ram Paib90fa9a2005-11-07 17:19:50 -0500726}
727
728/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000729 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500730 */
Al Viro4b2619a2011-11-24 19:47:15 -0500731static void commit_tree(struct mount *mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500732{
Al Viro0714a532011-11-24 22:19:58 -0500733 struct mount *parent = mnt->mnt_parent;
Al Viro83adc752011-11-24 22:37:54 -0500734 struct mount *m;
Ram Paib90fa9a2005-11-07 17:19:50 -0500735 LIST_HEAD(head);
Al Viro143c8c92011-11-25 00:46:35 -0500736 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500737
Al Viro0714a532011-11-24 22:19:58 -0500738 BUG_ON(parent == mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500739
Al Viro1a4eeaf2011-11-25 02:19:55 -0500740 list_add_tail(&head, &mnt->mnt_list);
Al Virof7a99c52012-06-09 00:59:08 -0400741 list_for_each_entry(m, &head, mnt_list)
Al Viro143c8c92011-11-25 00:46:35 -0500742 m->mnt_ns = n;
Al Virof03c6592011-01-14 22:30:21 -0500743
Ram Paib90fa9a2005-11-07 17:19:50 -0500744 list_splice(&head, n->list.prev);
745
Al Viro1b8e5562011-11-24 21:01:32 -0500746 list_add_tail(&mnt->mnt_hash, mount_hashtable +
Al Viroa73324d2011-11-24 22:25:07 -0500747 hash(&parent->mnt, mnt->mnt_mountpoint));
Al Viro6b41d532011-11-24 23:24:33 -0500748 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800749 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Al Viro909b0a82011-11-25 03:06:56 -0500752static struct mount *next_mnt(struct mount *p, struct mount *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Al Viro6b41d532011-11-24 23:24:33 -0500754 struct list_head *next = p->mnt_mounts.next;
755 if (next == &p->mnt_mounts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 while (1) {
Al Viro909b0a82011-11-25 03:06:56 -0500757 if (p == root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return NULL;
Al Viro6b41d532011-11-24 23:24:33 -0500759 next = p->mnt_child.next;
760 if (next != &p->mnt_parent->mnt_mounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
Al Viro0714a532011-11-24 22:19:58 -0500762 p = p->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 }
Al Viro6b41d532011-11-24 23:24:33 -0500765 return list_entry(next, struct mount, mnt_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Al Viro315fc832011-11-24 18:57:30 -0500768static struct mount *skip_mnt_tree(struct mount *p)
Ram Pai9676f0c2005-11-07 17:21:20 -0500769{
Al Viro6b41d532011-11-24 23:24:33 -0500770 struct list_head *prev = p->mnt_mounts.prev;
771 while (prev != &p->mnt_mounts) {
772 p = list_entry(prev, struct mount, mnt_child);
773 prev = p->mnt_mounts.prev;
Ram Pai9676f0c2005-11-07 17:21:20 -0500774 }
775 return p;
776}
777
Al Viro9d412a42011-03-17 22:08:28 -0400778struct vfsmount *
779vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
780{
Al Virob105e272011-11-24 20:38:33 -0500781 struct mount *mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400782 struct dentry *root;
783
784 if (!type)
785 return ERR_PTR(-ENODEV);
786
787 mnt = alloc_vfsmnt(name);
788 if (!mnt)
789 return ERR_PTR(-ENOMEM);
790
791 if (flags & MS_KERNMOUNT)
Al Virob105e272011-11-24 20:38:33 -0500792 mnt->mnt.mnt_flags = MNT_INTERNAL;
Al Viro9d412a42011-03-17 22:08:28 -0400793
794 root = mount_fs(type, flags, name, data);
795 if (IS_ERR(root)) {
796 free_vfsmnt(mnt);
797 return ERR_CAST(root);
798 }
799
Al Virob105e272011-11-24 20:38:33 -0500800 mnt->mnt.mnt_root = root;
801 mnt->mnt.mnt_sb = root->d_sb;
Al Viroa73324d2011-11-24 22:25:07 -0500802 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -0500803 mnt->mnt_parent = mnt;
Andi Kleen962830d2012-05-08 13:32:02 +0930804 br_write_lock(&vfsmount_lock);
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100805 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
Andi Kleen962830d2012-05-08 13:32:02 +0930806 br_write_unlock(&vfsmount_lock);
Al Virob105e272011-11-24 20:38:33 -0500807 return &mnt->mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400808}
809EXPORT_SYMBOL_GPL(vfs_kern_mount);
810
Al Viro87129cc2011-11-24 21:24:27 -0500811static struct mount *clone_mnt(struct mount *old, struct dentry *root,
Ram Pai36341f62005-11-07 17:17:22 -0500812 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Al Viro87129cc2011-11-24 21:24:27 -0500814 struct super_block *sb = old->mnt.mnt_sb;
David Howellsbe34d1a2012-06-25 12:55:18 +0100815 struct mount *mnt;
816 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
David Howellsbe34d1a2012-06-25 12:55:18 +0100818 mnt = alloc_vfsmnt(old->mnt_devname);
819 if (!mnt)
820 return ERR_PTR(-ENOMEM);
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100821
Eric W. Biederman7a472ef2012-07-31 13:13:04 -0700822 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
David Howellsbe34d1a2012-06-25 12:55:18 +0100823 mnt->mnt_group_id = 0; /* not a peer of original */
824 else
825 mnt->mnt_group_id = old->mnt_group_id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100826
David Howellsbe34d1a2012-06-25 12:55:18 +0100827 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
828 err = mnt_alloc_group_id(mnt);
829 if (err)
830 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
David Howellsbe34d1a2012-06-25 12:55:18 +0100832
833 mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
Eric W. Biederman132c94e2013-03-22 04:08:05 -0700834 /* Don't allow unprivileged users to change mount flags */
Eric W. Biederman187985d2014-07-28 17:26:07 -0700835 if (flag & CL_UNPRIVILEGED) {
836 mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
837
838 if (mnt->mnt.mnt_flags & MNT_READONLY)
839 mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
840
841 if (mnt->mnt.mnt_flags & MNT_NODEV)
842 mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
843
844 if (mnt->mnt.mnt_flags & MNT_NOSUID)
845 mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
846
847 if (mnt->mnt.mnt_flags & MNT_NOEXEC)
848 mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
849 }
Eric W. Biederman132c94e2013-03-22 04:08:05 -0700850
David Howellsbe34d1a2012-06-25 12:55:18 +0100851 atomic_inc(&sb->s_active);
852 mnt->mnt.mnt_sb = sb;
853 mnt->mnt.mnt_root = dget(root);
854 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
855 mnt->mnt_parent = mnt;
856 br_write_lock(&vfsmount_lock);
857 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
858 br_write_unlock(&vfsmount_lock);
859
Eric W. Biederman7a472ef2012-07-31 13:13:04 -0700860 if ((flag & CL_SLAVE) ||
861 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
David Howellsbe34d1a2012-06-25 12:55:18 +0100862 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
863 mnt->mnt_master = old;
864 CLEAR_MNT_SHARED(mnt);
865 } else if (!(flag & CL_PRIVATE)) {
866 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
867 list_add(&mnt->mnt_share, &old->mnt_share);
868 if (IS_MNT_SLAVE(old))
869 list_add(&mnt->mnt_slave, &old->mnt_slave);
870 mnt->mnt_master = old->mnt_master;
871 }
872 if (flag & CL_MAKE_SHARED)
873 set_mnt_shared(mnt);
874
875 /* stick the duplicate mount on the same expiry list
876 * as the original if that was on one */
877 if (flag & CL_EXPIRE) {
878 if (!list_empty(&old->mnt_expire))
879 list_add(&mnt->mnt_expire, &old->mnt_expire);
880 }
881
Al Virocb338d02011-11-24 20:55:08 -0500882 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100883
884 out_free:
885 free_vfsmnt(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +0100886 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Al Viro83adc752011-11-24 22:37:54 -0500889static inline void mntfree(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Al Viro83adc752011-11-24 22:37:54 -0500891 struct vfsmount *m = &mnt->mnt;
892 struct super_block *sb = m->mnt_sb;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100893
Dave Hansen3d733632008-02-15 14:37:59 -0800894 /*
Dave Hansen3d733632008-02-15 14:37:59 -0800895 * This probably indicates that somebody messed
896 * up a mnt_want/drop_write() pair. If this
897 * happens, the filesystem was probably unable
898 * to make r/w->r/o transitions.
899 */
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000900 /*
Nick Pigginb3e19d92011-01-07 17:50:11 +1100901 * The locking used to deal with mnt_count decrement provides barriers,
902 * so mnt_get_writers() below is safe.
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000903 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100904 WARN_ON(mnt_get_writers(mnt));
Al Viro83adc752011-11-24 22:37:54 -0500905 fsnotify_vfsmount_delete(m);
906 dput(m->mnt_root);
907 free_vfsmnt(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 deactivate_super(sb);
909}
910
Al Viro900148d2011-11-25 00:33:11 -0500911static void mntput_no_expire(struct mount *mnt)
Al Viro7b7b1ac2005-11-07 17:13:39 -0500912{
Nick Pigginb3e19d92011-01-07 17:50:11 +1100913put_again:
Al Virof03c6592011-01-14 22:30:21 -0500914#ifdef CONFIG_SMP
Andi Kleen962830d2012-05-08 13:32:02 +0930915 br_read_lock(&vfsmount_lock);
Al Virof7a99c52012-06-09 00:59:08 -0400916 if (likely(mnt->mnt_ns)) {
917 /* shouldn't be the last one */
Al Viroaa9c0e02011-11-23 19:04:52 -0500918 mnt_add_count(mnt, -1);
Andi Kleen962830d2012-05-08 13:32:02 +0930919 br_read_unlock(&vfsmount_lock);
Al Virof03c6592011-01-14 22:30:21 -0500920 return;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100921 }
Andi Kleen962830d2012-05-08 13:32:02 +0930922 br_read_unlock(&vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100923
Andi Kleen962830d2012-05-08 13:32:02 +0930924 br_write_lock(&vfsmount_lock);
Al Viroaa9c0e02011-11-23 19:04:52 -0500925 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100926 if (mnt_get_count(mnt)) {
Andi Kleen962830d2012-05-08 13:32:02 +0930927 br_write_unlock(&vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100928 return;
929 }
Nick Pigginb3e19d92011-01-07 17:50:11 +1100930#else
Al Viroaa9c0e02011-11-23 19:04:52 -0500931 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100932 if (likely(mnt_get_count(mnt)))
Nick Piggin99b7db72010-08-18 04:37:39 +1000933 return;
Andi Kleen962830d2012-05-08 13:32:02 +0930934 br_write_lock(&vfsmount_lock);
Al Virof03c6592011-01-14 22:30:21 -0500935#endif
Al Viro863d6842011-11-25 00:57:42 -0500936 if (unlikely(mnt->mnt_pinned)) {
937 mnt_add_count(mnt, mnt->mnt_pinned + 1);
938 mnt->mnt_pinned = 0;
Andi Kleen962830d2012-05-08 13:32:02 +0930939 br_write_unlock(&vfsmount_lock);
Al Viro900148d2011-11-25 00:33:11 -0500940 acct_auto_close_mnt(&mnt->mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100941 goto put_again;
Al Viro7b7b1ac2005-11-07 17:13:39 -0500942 }
Andi Kleen962830d2012-05-08 13:32:02 +0930943
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100944 list_del(&mnt->mnt_instance);
Andi Kleen962830d2012-05-08 13:32:02 +0930945 br_write_unlock(&vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100946 mntfree(mnt);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500947}
Nick Pigginb3e19d92011-01-07 17:50:11 +1100948
949void mntput(struct vfsmount *mnt)
950{
951 if (mnt) {
Al Viro863d6842011-11-25 00:57:42 -0500952 struct mount *m = real_mount(mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100953 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
Al Viro863d6842011-11-25 00:57:42 -0500954 if (unlikely(m->mnt_expiry_mark))
955 m->mnt_expiry_mark = 0;
956 mntput_no_expire(m);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100957 }
958}
959EXPORT_SYMBOL(mntput);
960
961struct vfsmount *mntget(struct vfsmount *mnt)
962{
963 if (mnt)
Al Viro83adc752011-11-24 22:37:54 -0500964 mnt_add_count(real_mount(mnt), 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100965 return mnt;
966}
967EXPORT_SYMBOL(mntget);
968
Al Viro7b7b1ac2005-11-07 17:13:39 -0500969void mnt_pin(struct vfsmount *mnt)
970{
Andi Kleen962830d2012-05-08 13:32:02 +0930971 br_write_lock(&vfsmount_lock);
Al Viro863d6842011-11-25 00:57:42 -0500972 real_mount(mnt)->mnt_pinned++;
Andi Kleen962830d2012-05-08 13:32:02 +0930973 br_write_unlock(&vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500974}
Al Viro7b7b1ac2005-11-07 17:13:39 -0500975EXPORT_SYMBOL(mnt_pin);
976
Al Viro863d6842011-11-25 00:57:42 -0500977void mnt_unpin(struct vfsmount *m)
Al Viro7b7b1ac2005-11-07 17:13:39 -0500978{
Al Viro863d6842011-11-25 00:57:42 -0500979 struct mount *mnt = real_mount(m);
Andi Kleen962830d2012-05-08 13:32:02 +0930980 br_write_lock(&vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500981 if (mnt->mnt_pinned) {
Al Viro863d6842011-11-25 00:57:42 -0500982 mnt_add_count(mnt, 1);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500983 mnt->mnt_pinned--;
984 }
Andi Kleen962830d2012-05-08 13:32:02 +0930985 br_write_unlock(&vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500986}
Al Viro7b7b1ac2005-11-07 17:13:39 -0500987EXPORT_SYMBOL(mnt_unpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800989static inline void mangle(struct seq_file *m, const char *s)
990{
991 seq_escape(m, s, " \t\n\\");
992}
993
994/*
995 * Simple .show_options callback for filesystems which don't want to
996 * implement more complex mount option showing.
997 *
998 * See also save_mount_options().
999 */
Al Viro34c80b12011-12-08 21:32:45 -05001000int generic_show_options(struct seq_file *m, struct dentry *root)
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001001{
Al Viro2a32ceb2009-05-08 16:05:57 -04001002 const char *options;
1003
1004 rcu_read_lock();
Al Viro34c80b12011-12-08 21:32:45 -05001005 options = rcu_dereference(root->d_sb->s_options);
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001006
1007 if (options != NULL && options[0]) {
1008 seq_putc(m, ',');
1009 mangle(m, options);
1010 }
Al Viro2a32ceb2009-05-08 16:05:57 -04001011 rcu_read_unlock();
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001012
1013 return 0;
1014}
1015EXPORT_SYMBOL(generic_show_options);
1016
1017/*
1018 * If filesystem uses generic_show_options(), this function should be
1019 * called from the fill_super() callback.
1020 *
1021 * The .remount_fs callback usually needs to be handled in a special
1022 * way, to make sure, that previous options are not overwritten if the
1023 * remount fails.
1024 *
1025 * Also note, that if the filesystem's .remount_fs function doesn't
1026 * reset all options to their default value, but changes only newly
1027 * given options, then the displayed options will not reflect reality
1028 * any more.
1029 */
1030void save_mount_options(struct super_block *sb, char *options)
1031{
Al Viro2a32ceb2009-05-08 16:05:57 -04001032 BUG_ON(sb->s_options);
1033 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001034}
1035EXPORT_SYMBOL(save_mount_options);
1036
Al Viro2a32ceb2009-05-08 16:05:57 -04001037void replace_mount_options(struct super_block *sb, char *options)
1038{
1039 char *old = sb->s_options;
1040 rcu_assign_pointer(sb->s_options, options);
1041 if (old) {
1042 synchronize_rcu();
1043 kfree(old);
1044 }
1045}
1046EXPORT_SYMBOL(replace_mount_options);
1047
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001048#ifdef CONFIG_PROC_FS
Al Viro0226f492011-12-06 12:21:54 -05001049/* iterator; we want it to have access to namespace_sem, thus here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050static void *m_start(struct seq_file *m, loff_t *pos)
1051{
Al Viro6ce6e242012-06-09 01:16:59 -04001052 struct proc_mounts *p = proc_mounts(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Ram Pai390c6842005-11-07 17:17:51 -05001054 down_read(&namespace_sem);
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001055 return seq_list_start(&p->ns->list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1059{
Al Viro6ce6e242012-06-09 01:16:59 -04001060 struct proc_mounts *p = proc_mounts(m);
Pavel Emelianovb0765fb2007-07-15 23:39:55 -07001061
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001062 return seq_list_next(v, &p->ns->list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065static void m_stop(struct seq_file *m, void *v)
1066{
Ram Pai390c6842005-11-07 17:17:51 -05001067 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Al Viro0226f492011-12-06 12:21:54 -05001070static int m_show(struct seq_file *m, void *v)
Al Viro9f5596a2010-02-05 00:40:25 -05001071{
Al Viro6ce6e242012-06-09 01:16:59 -04001072 struct proc_mounts *p = proc_mounts(m);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001073 struct mount *r = list_entry(v, struct mount, mnt_list);
Al Viro0226f492011-12-06 12:21:54 -05001074 return p->show(m, &r->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001077const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 .start = m_start,
1079 .next = m_next,
1080 .stop = m_stop,
Al Viro0226f492011-12-06 12:21:54 -05001081 .show = m_show,
Chuck Leverb4629fe2006-03-20 13:44:12 -05001082};
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001083#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -05001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/**
1086 * may_umount_tree - check if a mount tree is busy
1087 * @mnt: root of mount tree
1088 *
1089 * This is called to check if a tree of mounts has any
1090 * open files, pwds, chroots or sub mounts that are
1091 * busy.
1092 */
Al Viro909b0a82011-11-25 03:06:56 -05001093int may_umount_tree(struct vfsmount *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Al Viro909b0a82011-11-25 03:06:56 -05001095 struct mount *mnt = real_mount(m);
Ram Pai36341f62005-11-07 17:17:22 -05001096 int actual_refs = 0;
1097 int minimum_refs = 0;
Al Viro315fc832011-11-24 18:57:30 -05001098 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001099 BUG_ON(!m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Nick Pigginb3e19d92011-01-07 17:50:11 +11001101 /* write lock needed for mnt_get_count */
Andi Kleen962830d2012-05-08 13:32:02 +09301102 br_write_lock(&vfsmount_lock);
Al Viro909b0a82011-11-25 03:06:56 -05001103 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Viro83adc752011-11-24 22:37:54 -05001104 actual_refs += mnt_get_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Andi Kleen962830d2012-05-08 13:32:02 +09301107 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -08001110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Ian Kente3474a82006-03-27 01:14:51 -08001112 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115EXPORT_SYMBOL(may_umount_tree);
1116
1117/**
1118 * may_umount - check if a mount point is busy
1119 * @mnt: root of mount
1120 *
1121 * This is called to check if a mount point has any
1122 * open files, pwds, chroots or sub mounts. If the
1123 * mount has sub mounts this will return busy
1124 * regardless of whether the sub mounts are busy.
1125 *
1126 * Doesn't take quota and stuff into account. IOW, in some cases it will
1127 * give false negatives. The main reason why it's here is that we need
1128 * a non-destructive way to look for easily umountable filesystems.
1129 */
1130int may_umount(struct vfsmount *mnt)
1131{
Ian Kente3474a82006-03-27 01:14:51 -08001132 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001133 down_read(&namespace_sem);
Andi Kleen962830d2012-05-08 13:32:02 +09301134 br_write_lock(&vfsmount_lock);
Al Viro1ab59732011-11-24 21:35:16 -05001135 if (propagate_mount_busy(real_mount(mnt), 2))
Ian Kente3474a82006-03-27 01:14:51 -08001136 ret = 0;
Andi Kleen962830d2012-05-08 13:32:02 +09301137 br_write_unlock(&vfsmount_lock);
Al Viro8ad08d82010-01-16 12:56:08 -05001138 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001139 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142EXPORT_SYMBOL(may_umount);
1143
Al Viroe3197d82013-03-16 14:35:16 -04001144static LIST_HEAD(unmounted); /* protected by namespace_sem */
1145
Al Viro97216be2013-03-16 15:12:40 -04001146static void namespace_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Al Virod5e50f72011-11-24 20:58:57 -05001148 struct mount *mnt;
Al Viro97216be2013-03-16 15:12:40 -04001149 LIST_HEAD(head);
1150
1151 if (likely(list_empty(&unmounted))) {
1152 up_write(&namespace_sem);
1153 return;
1154 }
1155
1156 list_splice_init(&unmounted, &head);
1157 up_write(&namespace_sem);
1158
1159 while (!list_empty(&head)) {
1160 mnt = list_first_entry(&head, struct mount, mnt_hash);
Al Viro1b8e5562011-11-24 21:01:32 -05001161 list_del_init(&mnt->mnt_hash);
Al Viro676da582011-11-24 21:47:05 -05001162 if (mnt_has_parent(mnt)) {
Ram Pai70fbcdf2005-11-07 17:17:04 -05001163 struct dentry *dentry;
Al Viro863d6842011-11-25 00:57:42 -05001164 struct mount *m;
Nick Piggin99b7db72010-08-18 04:37:39 +10001165
Andi Kleen962830d2012-05-08 13:32:02 +09301166 br_write_lock(&vfsmount_lock);
Al Viroa73324d2011-11-24 22:25:07 -05001167 dentry = mnt->mnt_mountpoint;
Al Viro863d6842011-11-25 00:57:42 -05001168 m = mnt->mnt_parent;
Al Viroa73324d2011-11-24 22:25:07 -05001169 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -05001170 mnt->mnt_parent = mnt;
Al Viro7c4b93d2008-03-21 23:59:49 -04001171 m->mnt_ghosts--;
Andi Kleen962830d2012-05-08 13:32:02 +09301172 br_write_unlock(&vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001173 dput(dentry);
Al Viro863d6842011-11-25 00:57:42 -05001174 mntput(&m->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Al Virod5e50f72011-11-24 20:58:57 -05001176 mntput(&mnt->mnt);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001177 }
1178}
1179
Al Viro97216be2013-03-16 15:12:40 -04001180static inline void namespace_lock(void)
Al Viroe3197d82013-03-16 14:35:16 -04001181{
Al Viro97216be2013-03-16 15:12:40 -04001182 down_write(&namespace_sem);
Al Viroe3197d82013-03-16 14:35:16 -04001183}
1184
Nick Piggin99b7db72010-08-18 04:37:39 +10001185/*
1186 * vfsmount lock must be held for write
1187 * namespace_sem must be held for write
1188 */
Al Viro328e6d92013-03-16 14:49:45 -04001189void umount_tree(struct mount *mnt, int propagate)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001190{
Al Viro7b8a53f2011-01-15 20:08:44 -05001191 LIST_HEAD(tmp_list);
Al Viro315fc832011-11-24 18:57:30 -05001192 struct mount *p;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001193
Al Viro909b0a82011-11-25 03:06:56 -05001194 for (p = mnt; p; p = next_mnt(p, mnt))
Al Viro1b8e5562011-11-24 21:01:32 -05001195 list_move(&p->mnt_hash, &tmp_list);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001196
Ram Paia05964f2005-11-07 17:20:17 -05001197 if (propagate)
Al Viro7b8a53f2011-01-15 20:08:44 -05001198 propagate_umount(&tmp_list);
Ram Paia05964f2005-11-07 17:20:17 -05001199
Al Viro1b8e5562011-11-24 21:01:32 -05001200 list_for_each_entry(p, &tmp_list, mnt_hash) {
Al Viro6776db3d2011-11-25 00:22:05 -05001201 list_del_init(&p->mnt_expire);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001202 list_del_init(&p->mnt_list);
Al Viro143c8c92011-11-25 00:46:35 -05001203 __touch_mnt_namespace(p->mnt_ns);
1204 p->mnt_ns = NULL;
Al Viro6b41d532011-11-24 23:24:33 -05001205 list_del_init(&p->mnt_child);
Al Viro676da582011-11-24 21:47:05 -05001206 if (mnt_has_parent(p)) {
Al Viro863d6842011-11-25 00:57:42 -05001207 p->mnt_parent->mnt_ghosts++;
Al Viro84d17192013-03-15 10:53:28 -04001208 put_mountpoint(p->mnt_mp);
1209 p->mnt_mp = NULL;
Al Viro7c4b93d2008-03-21 23:59:49 -04001210 }
Al Viro0f0afb12011-11-24 20:43:10 -05001211 change_mnt_propagation(p, MS_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Al Viro328e6d92013-03-16 14:49:45 -04001213 list_splice(&tmp_list, &unmounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Al Virob54b9be2013-03-16 14:39:34 -04001216static void shrink_submounts(struct mount *mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001217
Al Viro1ab59732011-11-24 21:35:16 -05001218static int do_umount(struct mount *mnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Al Viro1ab59732011-11-24 21:35:16 -05001220 struct super_block *sb = mnt->mnt.mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int retval;
1222
Al Viro1ab59732011-11-24 21:35:16 -05001223 retval = security_sb_umount(&mnt->mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (retval)
1225 return retval;
1226
1227 /*
1228 * Allow userspace to request a mountpoint be expired rather than
1229 * unmounting unconditionally. Unmount only happens if:
1230 * (1) the mark is already set (the mark is cleared by mntput())
1231 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1232 */
1233 if (flags & MNT_EXPIRE) {
Al Viro1ab59732011-11-24 21:35:16 -05001234 if (&mnt->mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 flags & (MNT_FORCE | MNT_DETACH))
1236 return -EINVAL;
1237
Nick Pigginb3e19d92011-01-07 17:50:11 +11001238 /*
1239 * probably don't strictly need the lock here if we examined
1240 * all race cases, but it's a slowpath.
1241 */
Andi Kleen962830d2012-05-08 13:32:02 +09301242 br_write_lock(&vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -05001243 if (mnt_get_count(mnt) != 2) {
Andi Kleen962830d2012-05-08 13:32:02 +09301244 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EBUSY;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001246 }
Andi Kleen962830d2012-05-08 13:32:02 +09301247 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Al Viro863d6842011-11-25 00:57:42 -05001249 if (!xchg(&mnt->mnt_expiry_mark, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return -EAGAIN;
1251 }
1252
1253 /*
1254 * If we may have to abort operations to get out of this
1255 * mount, and they will themselves hold resources we must
1256 * allow the fs to do things. In the Unix tradition of
1257 * 'Gee thats tricky lets do it in userspace' the umount_begin
1258 * might fail to complete on the first run through as other tasks
1259 * must return, and the like. Thats for the mount program to worry
1260 * about for the moment.
1261 */
1262
Al Viro42faad92008-04-24 07:21:56 -04001263 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001264 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * No sense to grab the lock for this test, but test itself looks
1269 * somewhat bogus. Suggestions for better replacement?
1270 * Ho-hum... In principle, we might treat that as umount + switch
1271 * to rootfs. GC would eventually take care of the old vfsmount.
1272 * Actually it makes sense, especially if rootfs would contain a
1273 * /reboot - static binary that would close all descriptors and
1274 * call reboot(9). Then init(8) could umount root and exec /reboot.
1275 */
Al Viro1ab59732011-11-24 21:35:16 -05001276 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * Special case for "unmounting" root ...
1279 * we just try to remount it readonly.
1280 */
Andy Lutomirskia7dbb3e2014-10-08 12:32:47 -07001281 if (!capable(CAP_SYS_ADMIN))
1282 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 down_write(&sb->s_umount);
Al Viro4aa98cf2009-05-08 13:36:58 -04001284 if (!(sb->s_flags & MS_RDONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 up_write(&sb->s_umount);
1287 return retval;
1288 }
1289
Al Viro97216be2013-03-16 15:12:40 -04001290 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09301291 br_write_lock(&vfsmount_lock);
Al Viro5addc5d2005-11-07 17:15:49 -05001292 event++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Al Viroc35038b2008-03-22 00:46:23 -04001294 if (!(flags & MNT_DETACH))
Al Virob54b9be2013-03-16 14:39:34 -04001295 shrink_submounts(mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 retval = -EBUSY;
Ram Paia05964f2005-11-07 17:20:17 -05001298 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05001299 if (!list_empty(&mnt->mnt_list))
Al Viro328e6d92013-03-16 14:49:45 -04001300 umount_tree(mnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 retval = 0;
1302 }
Andi Kleen962830d2012-05-08 13:32:02 +09301303 br_write_unlock(&vfsmount_lock);
Al Viroe3197d82013-03-16 14:35:16 -04001304 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return retval;
1306}
1307
Thomas Gleixner3c536052012-03-07 21:00:34 +01001308/*
Al Viro9b40bc92013-02-22 22:45:42 -05001309 * Is the caller allowed to modify his namespace?
1310 */
1311static inline bool may_mount(void)
1312{
1313 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/*
1317 * Now umount can handle mount points as well as block devices.
1318 * This is important for filesystems which use unnamed block devices.
1319 *
1320 * We now support a flag for forced unmount like the other 'big iron'
1321 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1322 */
1323
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001324SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Al Viro2d8f3032008-07-22 09:59:21 -04001326 struct path path;
Al Viro900148d2011-11-25 00:33:11 -05001327 struct mount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int retval;
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001329 int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001331 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1332 return -EINVAL;
1333
Al Viro9b40bc92013-02-22 22:45:42 -05001334 if (!may_mount())
1335 return -EPERM;
1336
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001337 if (!(flags & UMOUNT_NOFOLLOW))
1338 lookup_flags |= LOOKUP_FOLLOW;
1339
1340 retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (retval)
1342 goto out;
Al Viro900148d2011-11-25 00:33:11 -05001343 mnt = real_mount(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 retval = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04001345 if (path.dentry != path.mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 goto dput_and_out;
Al Viro143c8c92011-11-25 00:46:35 -05001347 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto dput_and_out;
Eric W. Biedermanc65d3b02014-10-04 14:44:03 -07001349 retval = -EPERM;
1350 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1351 goto dput_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Al Viro900148d2011-11-25 00:33:11 -05001353 retval = do_umount(mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354dput_and_out:
Jan Blunck429731b2008-02-14 19:34:31 -08001355 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Al Viro2d8f3032008-07-22 09:59:21 -04001356 dput(path.dentry);
Al Viro900148d2011-11-25 00:33:11 -05001357 mntput_no_expire(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358out:
1359 return retval;
1360}
1361
1362#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1363
1364/*
Ram Paib58fed82005-11-07 17:16:09 -05001365 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001367SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Ram Paib58fed82005-11-07 17:16:09 -05001369 return sys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372#endif
1373
Eric W. Biederman8823c072010-03-07 18:49:36 -08001374static bool mnt_ns_loop(struct path *path)
1375{
1376 /* Could bind mounting the mount namespace inode cause a
1377 * mount namespace loop?
1378 */
1379 struct inode *inode = path->dentry->d_inode;
David Howells0bb80f22013-04-12 01:50:06 +01001380 struct proc_ns *ei;
Eric W. Biederman8823c072010-03-07 18:49:36 -08001381 struct mnt_namespace *mnt_ns;
1382
1383 if (!proc_ns_inode(inode))
1384 return false;
1385
David Howells0bb80f22013-04-12 01:50:06 +01001386 ei = get_proc_ns(inode);
Eric W. Biederman8823c072010-03-07 18:49:36 -08001387 if (ei->ns_ops != &mntns_operations)
1388 return false;
1389
1390 mnt_ns = ei->ns;
1391 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1392}
1393
Al Viro87129cc2011-11-24 21:24:27 -05001394struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001395 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Al Viro84d17192013-03-15 10:53:28 -04001397 struct mount *res, *p, *q, *r, *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Al Virofc7be132011-11-25 01:05:37 -05001399 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
David Howellsbe34d1a2012-06-25 12:55:18 +01001400 return ERR_PTR(-EINVAL);
Ram Pai9676f0c2005-11-07 17:21:20 -05001401
Ram Pai36341f62005-11-07 17:17:22 -05001402 res = q = clone_mnt(mnt, dentry, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001403 if (IS_ERR(q))
1404 return q;
1405
Al Viroa73324d2011-11-24 22:25:07 -05001406 q->mnt_mountpoint = mnt->mnt_mountpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 p = mnt;
Al Viro6b41d532011-11-24 23:24:33 -05001409 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Al Viro315fc832011-11-24 18:57:30 -05001410 struct mount *s;
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001411 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 continue;
1413
Al Viro909b0a82011-11-25 03:06:56 -05001414 for (s = r; s; s = next_mnt(s, r)) {
Al Virofc7be132011-11-25 01:05:37 -05001415 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001416 s = skip_mnt_tree(s);
1417 continue;
1418 }
Al Viro0714a532011-11-24 22:19:58 -05001419 while (p != s->mnt_parent) {
1420 p = p->mnt_parent;
1421 q = q->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Al Viro87129cc2011-11-24 21:24:27 -05001423 p = s;
Al Viro84d17192013-03-15 10:53:28 -04001424 parent = q;
Al Viro87129cc2011-11-24 21:24:27 -05001425 q = clone_mnt(p, p->mnt.mnt_root, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001426 if (IS_ERR(q))
1427 goto out;
Andi Kleen962830d2012-05-08 13:32:02 +09301428 br_write_lock(&vfsmount_lock);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001429 list_add_tail(&q->mnt_list, &res->mnt_list);
Al Viro84d17192013-03-15 10:53:28 -04001430 attach_mnt(q, parent, p->mnt_mp);
Andi Kleen962830d2012-05-08 13:32:02 +09301431 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 }
1434 return res;
David Howellsbe34d1a2012-06-25 12:55:18 +01001435out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (res) {
Andi Kleen962830d2012-05-08 13:32:02 +09301437 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04001438 umount_tree(res, 0);
Andi Kleen962830d2012-05-08 13:32:02 +09301439 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001441 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
David Howellsbe34d1a2012-06-25 12:55:18 +01001444/* Caller should check returned pointer for errors */
1445
Al Viro589ff872009-04-18 03:28:19 -04001446struct vfsmount *collect_mounts(struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001447{
Al Virocb338d02011-11-24 20:55:08 -05001448 struct mount *tree;
Al Viro97216be2013-03-16 15:12:40 -04001449 namespace_lock();
Al Viro87129cc2011-11-24 21:24:27 -05001450 tree = copy_tree(real_mount(path->mnt), path->dentry,
1451 CL_COPY_ALL | CL_PRIVATE);
Al Viro328e6d92013-03-16 14:49:45 -04001452 namespace_unlock();
David Howellsbe34d1a2012-06-25 12:55:18 +01001453 if (IS_ERR(tree))
Dan Carpenter2bee1e02013-08-14 12:44:39 +03001454 return ERR_CAST(tree);
David Howellsbe34d1a2012-06-25 12:55:18 +01001455 return &tree->mnt;
Al Viro8aec0802007-06-07 12:20:32 -04001456}
1457
1458void drop_collected_mounts(struct vfsmount *mnt)
1459{
Al Viro97216be2013-03-16 15:12:40 -04001460 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09301461 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04001462 umount_tree(real_mount(mnt), 0);
Andi Kleen962830d2012-05-08 13:32:02 +09301463 br_write_unlock(&vfsmount_lock);
Al Viro3ab6abe2013-03-16 14:42:19 -04001464 namespace_unlock();
Al Viro8aec0802007-06-07 12:20:32 -04001465}
1466
Al Viro1f707132010-01-30 22:51:25 -05001467int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1468 struct vfsmount *root)
1469{
Al Viro1a4eeaf2011-11-25 02:19:55 -05001470 struct mount *mnt;
Al Viro1f707132010-01-30 22:51:25 -05001471 int res = f(root, arg);
1472 if (res)
1473 return res;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001474 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1475 res = f(&mnt->mnt, arg);
Al Viro1f707132010-01-30 22:51:25 -05001476 if (res)
1477 return res;
1478 }
1479 return 0;
1480}
1481
Al Viro4b8b21f2011-11-24 19:54:23 -05001482static void cleanup_group_ids(struct mount *mnt, struct mount *end)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001483{
Al Viro315fc832011-11-24 18:57:30 -05001484 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001485
Al Viro909b0a82011-11-25 03:06:56 -05001486 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001487 if (p->mnt_group_id && !IS_MNT_SHARED(p))
Al Viro4b8b21f2011-11-24 19:54:23 -05001488 mnt_release_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001489 }
1490}
1491
Al Viro4b8b21f2011-11-24 19:54:23 -05001492static int invent_group_ids(struct mount *mnt, bool recurse)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001493{
Al Viro315fc832011-11-24 18:57:30 -05001494 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001495
Al Viro909b0a82011-11-25 03:06:56 -05001496 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
Al Virofc7be132011-11-25 01:05:37 -05001497 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001498 int err = mnt_alloc_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001499 if (err) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001500 cleanup_group_ids(mnt, p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001501 return err;
1502 }
1503 }
1504 }
1505
1506 return 0;
1507}
1508
Ram Paib90fa9a2005-11-07 17:19:50 -05001509/*
1510 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05001511 * @nd : place the mount tree @source_mnt is attached
1512 * @parent_nd : if non-null, detach the source_mnt from its parent and
1513 * store the parent mount and mountpoint dentry.
1514 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05001515 *
1516 * NOTE: in the table below explains the semantics when a source mount
1517 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05001518 * ---------------------------------------------------------------------------
1519 * | BIND MOUNT OPERATION |
1520 * |**************************************************************************
1521 * | source-->| shared | private | slave | unbindable |
1522 * | dest | | | | |
1523 * | | | | | | |
1524 * | v | | | | |
1525 * |**************************************************************************
1526 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1527 * | | | | | |
1528 * |non-shared| shared (+) | private | slave (*) | invalid |
1529 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05001530 * A bind operation clones the source mount and mounts the clone on the
1531 * destination mount.
1532 *
1533 * (++) the cloned mount is propagated to all the mounts in the propagation
1534 * tree of the destination mount and the cloned mount is added to
1535 * the peer group of the source mount.
1536 * (+) the cloned mount is created under the destination mount and is marked
1537 * as shared. The cloned mount is added to the peer group of the source
1538 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05001539 * (+++) the mount is propagated to all the mounts in the propagation tree
1540 * of the destination mount and the cloned mount is made slave
1541 * of the same master as that of the source mount. The cloned mount
1542 * is marked as 'shared and slave'.
1543 * (*) the cloned mount is made a slave of the same master as that of the
1544 * source mount.
1545 *
Ram Pai9676f0c2005-11-07 17:21:20 -05001546 * ---------------------------------------------------------------------------
1547 * | MOVE MOUNT OPERATION |
1548 * |**************************************************************************
1549 * | source-->| shared | private | slave | unbindable |
1550 * | dest | | | | |
1551 * | | | | | | |
1552 * | v | | | | |
1553 * |**************************************************************************
1554 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1555 * | | | | | |
1556 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1557 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05001558 *
1559 * (+) the mount is moved to the destination. And is then propagated to
1560 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05001561 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05001562 * (+++) the mount is moved to the destination and is then propagated to
1563 * all the mounts belonging to the destination mount's propagation tree.
1564 * the mount is marked as 'shared and slave'.
1565 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05001566 *
1567 * if the source mount is a tree, the operations explained above is
1568 * applied to each mount in the tree.
1569 * Must be called without spinlocks held, since this function can sleep
1570 * in allocations.
1571 */
Al Viro0fb54e52011-11-24 19:59:16 -05001572static int attach_recursive_mnt(struct mount *source_mnt,
Al Viro84d17192013-03-15 10:53:28 -04001573 struct mount *dest_mnt,
1574 struct mountpoint *dest_mp,
1575 struct path *parent_path)
Ram Paib90fa9a2005-11-07 17:19:50 -05001576{
1577 LIST_HEAD(tree_list);
Al Viro315fc832011-11-24 18:57:30 -05001578 struct mount *child, *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001579 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001580
Al Virofc7be132011-11-25 01:05:37 -05001581 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro0fb54e52011-11-24 19:59:16 -05001582 err = invent_group_ids(source_mnt, true);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001583 if (err)
1584 goto out;
1585 }
Al Viro84d17192013-03-15 10:53:28 -04001586 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001587 if (err)
1588 goto out_cleanup_ids;
Ram Paib90fa9a2005-11-07 17:19:50 -05001589
Andi Kleen962830d2012-05-08 13:32:02 +09301590 br_write_lock(&vfsmount_lock);
Al Virodf1a1ad2010-01-16 12:57:40 -05001591
Al Virofc7be132011-11-25 01:05:37 -05001592 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro909b0a82011-11-25 03:06:56 -05001593 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
Al Viro0f0afb12011-11-24 20:43:10 -05001594 set_mnt_shared(p);
Ram Paib90fa9a2005-11-07 17:19:50 -05001595 }
Al Viro1a390682008-03-21 20:48:19 -04001596 if (parent_path) {
Al Viro0fb54e52011-11-24 19:59:16 -05001597 detach_mnt(source_mnt, parent_path);
Al Viro84d17192013-03-15 10:53:28 -04001598 attach_mnt(source_mnt, dest_mnt, dest_mp);
Al Viro143c8c92011-11-25 00:46:35 -05001599 touch_mnt_namespace(source_mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05001600 } else {
Al Viro84d17192013-03-15 10:53:28 -04001601 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
Al Viro0fb54e52011-11-24 19:59:16 -05001602 commit_tree(source_mnt);
Ram Pai21444402005-11-07 17:20:03 -05001603 }
Ram Paib90fa9a2005-11-07 17:19:50 -05001604
Al Viro1b8e5562011-11-24 21:01:32 -05001605 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1606 list_del_init(&child->mnt_hash);
Al Viro4b2619a2011-11-24 19:47:15 -05001607 commit_tree(child);
Ram Paib90fa9a2005-11-07 17:19:50 -05001608 }
Andi Kleen962830d2012-05-08 13:32:02 +09301609 br_write_unlock(&vfsmount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +10001610
Ram Paib90fa9a2005-11-07 17:19:50 -05001611 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001612
1613 out_cleanup_ids:
Al Virofc7be132011-11-25 01:05:37 -05001614 if (IS_MNT_SHARED(dest_mnt))
Al Viro0fb54e52011-11-24 19:59:16 -05001615 cleanup_group_ids(source_mnt, NULL);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001616 out:
1617 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001618}
1619
Al Viro84d17192013-03-15 10:53:28 -04001620static struct mountpoint *lock_mount(struct path *path)
Al Virob12cea92011-03-18 08:55:38 -04001621{
1622 struct vfsmount *mnt;
Al Viro84d17192013-03-15 10:53:28 -04001623 struct dentry *dentry = path->dentry;
Al Virob12cea92011-03-18 08:55:38 -04001624retry:
Al Viro84d17192013-03-15 10:53:28 -04001625 mutex_lock(&dentry->d_inode->i_mutex);
1626 if (unlikely(cant_mount(dentry))) {
1627 mutex_unlock(&dentry->d_inode->i_mutex);
1628 return ERR_PTR(-ENOENT);
Al Virob12cea92011-03-18 08:55:38 -04001629 }
Al Viro97216be2013-03-16 15:12:40 -04001630 namespace_lock();
Al Virob12cea92011-03-18 08:55:38 -04001631 mnt = lookup_mnt(path);
Al Viro84d17192013-03-15 10:53:28 -04001632 if (likely(!mnt)) {
1633 struct mountpoint *mp = new_mountpoint(dentry);
1634 if (IS_ERR(mp)) {
Al Viro97216be2013-03-16 15:12:40 -04001635 namespace_unlock();
Al Viro84d17192013-03-15 10:53:28 -04001636 mutex_unlock(&dentry->d_inode->i_mutex);
1637 return mp;
1638 }
1639 return mp;
1640 }
Al Viro97216be2013-03-16 15:12:40 -04001641 namespace_unlock();
Al Virob12cea92011-03-18 08:55:38 -04001642 mutex_unlock(&path->dentry->d_inode->i_mutex);
1643 path_put(path);
1644 path->mnt = mnt;
Al Viro84d17192013-03-15 10:53:28 -04001645 dentry = path->dentry = dget(mnt->mnt_root);
Al Virob12cea92011-03-18 08:55:38 -04001646 goto retry;
1647}
1648
Al Viro84d17192013-03-15 10:53:28 -04001649static void unlock_mount(struct mountpoint *where)
Al Virob12cea92011-03-18 08:55:38 -04001650{
Al Viro84d17192013-03-15 10:53:28 -04001651 struct dentry *dentry = where->m_dentry;
1652 put_mountpoint(where);
Al Viro328e6d92013-03-16 14:49:45 -04001653 namespace_unlock();
Al Viro84d17192013-03-15 10:53:28 -04001654 mutex_unlock(&dentry->d_inode->i_mutex);
Al Virob12cea92011-03-18 08:55:38 -04001655}
1656
Al Viro84d17192013-03-15 10:53:28 -04001657static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Al Viro95bc5f22011-11-25 00:30:56 -05001659 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return -EINVAL;
1661
Al Viro84d17192013-03-15 10:53:28 -04001662 if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
Al Viro95bc5f22011-11-25 00:30:56 -05001663 S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return -ENOTDIR;
1665
Al Viro84d17192013-03-15 10:53:28 -04001666 return attach_recursive_mnt(mnt, p, mp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
1669/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001670 * Sanity check the flags to change_mnt_propagation.
1671 */
1672
1673static int flags_to_propagation_type(int flags)
1674{
Roman Borisov7c6e9842011-05-25 16:26:48 -07001675 int type = flags & ~(MS_REC | MS_SILENT);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001676
1677 /* Fail if any non-propagation flags are set */
1678 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1679 return 0;
1680 /* Only one propagation flag should be set */
1681 if (!is_power_of_2(type))
1682 return 0;
1683 return type;
1684}
1685
1686/*
Ram Pai07b20882005-11-07 17:19:07 -05001687 * recursively change the type of the mountpoint.
1688 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001689static int do_change_type(struct path *path, int flag)
Ram Pai07b20882005-11-07 17:19:07 -05001690{
Al Viro315fc832011-11-24 18:57:30 -05001691 struct mount *m;
Al Viro4b8b21f2011-11-24 19:54:23 -05001692 struct mount *mnt = real_mount(path->mnt);
Ram Pai07b20882005-11-07 17:19:07 -05001693 int recurse = flag & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001694 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001695 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05001696
Al Viro2d92ab32008-08-02 00:51:11 -04001697 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05001698 return -EINVAL;
1699
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001700 type = flags_to_propagation_type(flag);
1701 if (!type)
1702 return -EINVAL;
1703
Al Viro97216be2013-03-16 15:12:40 -04001704 namespace_lock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001705 if (type == MS_SHARED) {
1706 err = invent_group_ids(mnt, recurse);
1707 if (err)
1708 goto out_unlock;
1709 }
1710
Andi Kleen962830d2012-05-08 13:32:02 +09301711 br_write_lock(&vfsmount_lock);
Al Viro909b0a82011-11-25 03:06:56 -05001712 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
Al Viro0f0afb12011-11-24 20:43:10 -05001713 change_mnt_propagation(m, type);
Andi Kleen962830d2012-05-08 13:32:02 +09301714 br_write_unlock(&vfsmount_lock);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001715
1716 out_unlock:
Al Viro97216be2013-03-16 15:12:40 -04001717 namespace_unlock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001718 return err;
Ram Pai07b20882005-11-07 17:19:07 -05001719}
1720
1721/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * do loopback mount.
1723 */
Al Viro808d4e32012-10-11 11:42:01 -04001724static int do_loopback(struct path *path, const char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08001725 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Al Viro2d92ab32008-08-02 00:51:11 -04001727 struct path old_path;
Al Viro84d17192013-03-15 10:53:28 -04001728 struct mount *mnt = NULL, *old, *parent;
1729 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05001730 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (!old_name || !*old_name)
1732 return -EINVAL;
Trond Myklebust815d4052011-09-26 20:36:09 -04001733 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (err)
1735 return err;
1736
Eric W. Biederman8823c072010-03-07 18:49:36 -08001737 err = -EINVAL;
1738 if (mnt_ns_loop(&old_path))
Thomas Gleixner3c536052012-03-07 21:00:34 +01001739 goto out;
Eric W. Biederman8823c072010-03-07 18:49:36 -08001740
Al Viro84d17192013-03-15 10:53:28 -04001741 mp = lock_mount(path);
1742 err = PTR_ERR(mp);
1743 if (IS_ERR(mp))
Jan Blunck4ac91372008-02-14 19:34:32 -08001744 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05001745
Al Viro87129cc2011-11-24 21:24:27 -05001746 old = real_mount(old_path.mnt);
Al Viro84d17192013-03-15 10:53:28 -04001747 parent = real_mount(path->mnt);
Al Viro87129cc2011-11-24 21:24:27 -05001748
Al Virob12cea92011-03-18 08:55:38 -04001749 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05001750 if (IS_MNT_UNBINDABLE(old))
Al Virob12cea92011-03-18 08:55:38 -04001751 goto out2;
1752
Al Viro84d17192013-03-15 10:53:28 -04001753 if (!check_mnt(parent) || !check_mnt(old))
Al Virob12cea92011-03-18 08:55:38 -04001754 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Al Viroccd48bc2005-11-07 17:15:04 -05001756 if (recurse)
Al Viro87129cc2011-11-24 21:24:27 -05001757 mnt = copy_tree(old, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05001758 else
Al Viro87129cc2011-11-24 21:24:27 -05001759 mnt = clone_mnt(old, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05001760
David Howellsbe34d1a2012-06-25 12:55:18 +01001761 if (IS_ERR(mnt)) {
1762 err = PTR_ERR(mnt);
Andrey Vagine9c5d8a2013-04-09 17:33:29 +04001763 goto out2;
David Howellsbe34d1a2012-06-25 12:55:18 +01001764 }
Al Viroccd48bc2005-11-07 17:15:04 -05001765
Al Viro84d17192013-03-15 10:53:28 -04001766 err = graft_tree(mnt, parent, mp);
Al Viroccd48bc2005-11-07 17:15:04 -05001767 if (err) {
Andi Kleen962830d2012-05-08 13:32:02 +09301768 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04001769 umount_tree(mnt, 0);
Andi Kleen962830d2012-05-08 13:32:02 +09301770 br_write_unlock(&vfsmount_lock);
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05001771 }
Al Virob12cea92011-03-18 08:55:38 -04001772out2:
Al Viro84d17192013-03-15 10:53:28 -04001773 unlock_mount(mp);
Al Viroccd48bc2005-11-07 17:15:04 -05001774out:
Al Viro2d92ab32008-08-02 00:51:11 -04001775 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return err;
1777}
1778
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001779static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1780{
1781 int error = 0;
1782 int readonly_request = 0;
1783
1784 if (ms_flags & MS_RDONLY)
1785 readonly_request = 1;
1786 if (readonly_request == __mnt_is_readonly(mnt))
1787 return 0;
1788
1789 if (readonly_request)
Al Viro83adc752011-11-24 22:37:54 -05001790 error = mnt_make_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001791 else
Al Viro83adc752011-11-24 22:37:54 -05001792 __mnt_unmake_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001793 return error;
1794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
1797 * change filesystem flags. dir should be a physical root of filesystem.
1798 * If you've mounted a non-root directory somewhere and want to do remount
1799 * on it - tough luck.
1800 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001801static int do_remount(struct path *path, int flags, int mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 void *data)
1803{
1804 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04001805 struct super_block *sb = path->mnt->mnt_sb;
Al Viro143c8c92011-11-25 00:46:35 -05001806 struct mount *mnt = real_mount(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Al Viro143c8c92011-11-25 00:46:35 -05001808 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return -EINVAL;
1810
Al Viro2d92ab32008-08-02 00:51:11 -04001811 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return -EINVAL;
1813
Eric W. Biederman81d4c132014-07-28 17:10:56 -07001814 /* Don't allow changing of locked mnt flags.
1815 *
1816 * No locks need to be held here while testing the various
1817 * MNT_LOCK flags because those flags can never be cleared
1818 * once they are set.
1819 */
1820 if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
1821 !(mnt_flags & MNT_READONLY)) {
1822 return -EPERM;
1823 }
Eric W. Biederman187985d2014-07-28 17:26:07 -07001824 if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
1825 !(mnt_flags & MNT_NODEV)) {
Eric W. Biederman4be461b2014-08-13 01:33:38 -07001826 /* Was the nodev implicitly added in mount? */
1827 if ((mnt->mnt_ns->user_ns != &init_user_ns) &&
1828 !(sb->s_type->fs_flags & FS_USERNS_DEV_MOUNT)) {
1829 mnt_flags |= MNT_NODEV;
1830 } else {
1831 return -EPERM;
1832 }
Eric W. Biederman187985d2014-07-28 17:26:07 -07001833 }
1834 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
1835 !(mnt_flags & MNT_NOSUID)) {
1836 return -EPERM;
1837 }
1838 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
1839 !(mnt_flags & MNT_NOEXEC)) {
1840 return -EPERM;
1841 }
1842 if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
1843 ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
1844 return -EPERM;
1845 }
1846
Eric Parisff36fe22011-03-03 16:09:14 -05001847 err = security_sb_remount(sb, data);
1848 if (err)
1849 return err;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 down_write(&sb->s_umount);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001852 if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04001853 err = change_mount_flags(path->mnt, flags);
Al Viro57eccb82013-02-22 22:49:10 -05001854 else if (!capable(CAP_SYS_ADMIN))
1855 err = -EPERM;
Al Viro4aa98cf2009-05-08 13:36:58 -04001856 else
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001857 err = do_remount_sb(sb, flags, data, 0);
Al Viro7b43a792010-01-16 13:01:26 -05001858 if (!err) {
Andi Kleen962830d2012-05-08 13:32:02 +09301859 br_write_lock(&vfsmount_lock);
Eric W. Biederman8c30f222014-07-28 16:26:53 -07001860 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
Al Viro143c8c92011-11-25 00:46:35 -05001861 mnt->mnt.mnt_flags = mnt_flags;
Andi Kleen962830d2012-05-08 13:32:02 +09301862 br_write_unlock(&vfsmount_lock);
Al Viro7b43a792010-01-16 13:01:26 -05001863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 up_write(&sb->s_umount);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001865 if (!err) {
Andi Kleen962830d2012-05-08 13:32:02 +09301866 br_write_lock(&vfsmount_lock);
Al Viro143c8c92011-11-25 00:46:35 -05001867 touch_mnt_namespace(mnt->mnt_ns);
Andi Kleen962830d2012-05-08 13:32:02 +09301868 br_write_unlock(&vfsmount_lock);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return err;
1871}
1872
Al Virocbbe3622011-11-24 20:01:19 -05001873static inline int tree_contains_unbindable(struct mount *mnt)
Ram Pai9676f0c2005-11-07 17:21:20 -05001874{
Al Viro315fc832011-11-24 18:57:30 -05001875 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001876 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001877 if (IS_MNT_UNBINDABLE(p))
Ram Pai9676f0c2005-11-07 17:21:20 -05001878 return 1;
1879 }
1880 return 0;
1881}
1882
Al Viro808d4e32012-10-11 11:42:01 -04001883static int do_move_mount(struct path *path, const char *old_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Al Viro2d92ab32008-08-02 00:51:11 -04001885 struct path old_path, parent_path;
Al Viro676da582011-11-24 21:47:05 -05001886 struct mount *p;
Al Viro0fb54e52011-11-24 19:59:16 -05001887 struct mount *old;
Al Viro84d17192013-03-15 10:53:28 -04001888 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05001889 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (!old_name || !*old_name)
1891 return -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001892 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (err)
1894 return err;
1895
Al Viro84d17192013-03-15 10:53:28 -04001896 mp = lock_mount(path);
1897 err = PTR_ERR(mp);
1898 if (IS_ERR(mp))
David Howellscc53ce52011-01-14 18:45:26 +00001899 goto out;
1900
Al Viro143c8c92011-11-25 00:46:35 -05001901 old = real_mount(old_path.mnt);
Al Virofc7be132011-11-25 01:05:37 -05001902 p = real_mount(path->mnt);
Al Viro143c8c92011-11-25 00:46:35 -05001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05001905 if (!check_mnt(p) || !check_mnt(old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 goto out1;
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001909 if (old_path.dentry != old_path.mnt->mnt_root)
Ram Pai21444402005-11-07 17:20:03 -05001910 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Al Viro676da582011-11-24 21:47:05 -05001912 if (!mnt_has_parent(old))
Ram Pai21444402005-11-07 17:20:03 -05001913 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Al Viro2d92ab32008-08-02 00:51:11 -04001915 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1916 S_ISDIR(old_path.dentry->d_inode->i_mode))
Ram Pai21444402005-11-07 17:20:03 -05001917 goto out1;
1918 /*
1919 * Don't move a mount residing in a shared parent.
1920 */
Al Virofc7be132011-11-25 01:05:37 -05001921 if (IS_MNT_SHARED(old->mnt_parent))
Ram Pai21444402005-11-07 17:20:03 -05001922 goto out1;
Ram Pai9676f0c2005-11-07 17:21:20 -05001923 /*
1924 * Don't move a mount tree containing unbindable mounts to a destination
1925 * mount which is shared.
1926 */
Al Virofc7be132011-11-25 01:05:37 -05001927 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
Ram Pai9676f0c2005-11-07 17:21:20 -05001928 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 err = -ELOOP;
Al Virofc7be132011-11-25 01:05:37 -05001930 for (; mnt_has_parent(p); p = p->mnt_parent)
Al Viro676da582011-11-24 21:47:05 -05001931 if (p == old)
Ram Pai21444402005-11-07 17:20:03 -05001932 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Al Viro84d17192013-03-15 10:53:28 -04001934 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
Jan Blunck4ac91372008-02-14 19:34:32 -08001935 if (err)
Ram Pai21444402005-11-07 17:20:03 -05001936 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* if the mount is moved, it should no longer be expire
1939 * automatically */
Al Viro6776db3d2011-11-25 00:22:05 -05001940 list_del_init(&old->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941out1:
Al Viro84d17192013-03-15 10:53:28 -04001942 unlock_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (!err)
Al Viro1a390682008-03-21 20:48:19 -04001945 path_put(&parent_path);
Al Viro2d92ab32008-08-02 00:51:11 -04001946 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return err;
1948}
1949
Al Viro9d412a42011-03-17 22:08:28 -04001950static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1951{
1952 int err;
1953 const char *subtype = strchr(fstype, '.');
1954 if (subtype) {
1955 subtype++;
1956 err = -EINVAL;
1957 if (!subtype[0])
1958 goto err;
1959 } else
1960 subtype = "";
1961
1962 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1963 err = -ENOMEM;
1964 if (!mnt->mnt_sb->s_subtype)
1965 goto err;
1966 return mnt;
1967
1968 err:
1969 mntput(mnt);
1970 return ERR_PTR(err);
1971}
1972
Al Viro9d412a42011-03-17 22:08:28 -04001973/*
1974 * add a mount into a namespace's mount tree
1975 */
Al Viro95bc5f22011-11-25 00:30:56 -05001976static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
Al Viro9d412a42011-03-17 22:08:28 -04001977{
Al Viro84d17192013-03-15 10:53:28 -04001978 struct mountpoint *mp;
1979 struct mount *parent;
Al Viro9d412a42011-03-17 22:08:28 -04001980 int err;
1981
1982 mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
1983
Al Viro84d17192013-03-15 10:53:28 -04001984 mp = lock_mount(path);
1985 if (IS_ERR(mp))
1986 return PTR_ERR(mp);
Al Viro9d412a42011-03-17 22:08:28 -04001987
Al Viro84d17192013-03-15 10:53:28 -04001988 parent = real_mount(path->mnt);
Al Viro9d412a42011-03-17 22:08:28 -04001989 err = -EINVAL;
Al Viro84d17192013-03-15 10:53:28 -04001990 if (unlikely(!check_mnt(parent))) {
Al Viro156cacb2012-09-21 08:19:02 -04001991 /* that's acceptable only for automounts done in private ns */
1992 if (!(mnt_flags & MNT_SHRINKABLE))
1993 goto unlock;
1994 /* ... and for those we'd better have mountpoint still alive */
Al Viro84d17192013-03-15 10:53:28 -04001995 if (!parent->mnt_ns)
Al Viro156cacb2012-09-21 08:19:02 -04001996 goto unlock;
1997 }
Al Viro9d412a42011-03-17 22:08:28 -04001998
1999 /* Refuse the same filesystem on the same mount point */
2000 err = -EBUSY;
Al Viro95bc5f22011-11-25 00:30:56 -05002001 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
Al Viro9d412a42011-03-17 22:08:28 -04002002 path->mnt->mnt_root == path->dentry)
2003 goto unlock;
2004
2005 err = -EINVAL;
Al Viro95bc5f22011-11-25 00:30:56 -05002006 if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
Al Viro9d412a42011-03-17 22:08:28 -04002007 goto unlock;
2008
Al Viro95bc5f22011-11-25 00:30:56 -05002009 newmnt->mnt.mnt_flags = mnt_flags;
Al Viro84d17192013-03-15 10:53:28 -04002010 err = graft_tree(newmnt, parent, mp);
Al Viro9d412a42011-03-17 22:08:28 -04002011
2012unlock:
Al Viro84d17192013-03-15 10:53:28 -04002013 unlock_mount(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002014 return err;
2015}
Al Virob1e75df2011-01-17 01:47:59 -05002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/*
2018 * create a new mount for userspace and request it to be added into the
2019 * namespace's tree
2020 */
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002021static int do_new_mount(struct path *path, const char *fstype, int flags,
Al Viro808d4e32012-10-11 11:42:01 -04002022 int mnt_flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002024 struct file_system_type *type;
Al Viro9b40bc92013-02-22 22:45:42 -05002025 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 struct vfsmount *mnt;
Al Viro15f9a3f2011-01-17 01:41:58 -05002027 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002029 if (!fstype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return -EINVAL;
2031
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002032 type = get_fs_type(fstype);
2033 if (!type)
2034 return -ENODEV;
2035
2036 if (user_ns != &init_user_ns) {
2037 if (!(type->fs_flags & FS_USERNS_MOUNT)) {
2038 put_filesystem(type);
2039 return -EPERM;
2040 }
2041 /* Only in special cases allow devices from mounts
2042 * created outside the initial user namespace.
2043 */
2044 if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
2045 flags |= MS_NODEV;
Eric W. Biederman187985d2014-07-28 17:26:07 -07002046 mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV;
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002047 }
2048 }
2049
2050 mnt = vfs_kern_mount(type, flags, name, data);
2051 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2052 !mnt->mnt_sb->s_subtype)
2053 mnt = fs_set_subtype(mnt, fstype);
2054
2055 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (IS_ERR(mnt))
2057 return PTR_ERR(mnt);
2058
Al Viro95bc5f22011-11-25 00:30:56 -05002059 err = do_add_mount(real_mount(mnt), path, mnt_flags);
Al Viro15f9a3f2011-01-17 01:41:58 -05002060 if (err)
2061 mntput(mnt);
2062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Al Viro19a167a2011-01-17 01:35:23 -05002065int finish_automount(struct vfsmount *m, struct path *path)
2066{
Al Viro6776db3d2011-11-25 00:22:05 -05002067 struct mount *mnt = real_mount(m);
Al Viro19a167a2011-01-17 01:35:23 -05002068 int err;
2069 /* The new mount record should have at least 2 refs to prevent it being
2070 * expired before we get a chance to add it
2071 */
Al Viro6776db3d2011-11-25 00:22:05 -05002072 BUG_ON(mnt_get_count(mnt) < 2);
Al Viro19a167a2011-01-17 01:35:23 -05002073
2074 if (m->mnt_sb == path->mnt->mnt_sb &&
2075 m->mnt_root == path->dentry) {
Al Virob1e75df2011-01-17 01:47:59 -05002076 err = -ELOOP;
2077 goto fail;
Al Viro19a167a2011-01-17 01:35:23 -05002078 }
2079
Al Viro95bc5f22011-11-25 00:30:56 -05002080 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
Al Virob1e75df2011-01-17 01:47:59 -05002081 if (!err)
2082 return 0;
2083fail:
2084 /* remove m from any expiration list it may be on */
Al Viro6776db3d2011-11-25 00:22:05 -05002085 if (!list_empty(&mnt->mnt_expire)) {
Al Viro97216be2013-03-16 15:12:40 -04002086 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302087 br_write_lock(&vfsmount_lock);
Al Viro6776db3d2011-11-25 00:22:05 -05002088 list_del_init(&mnt->mnt_expire);
Andi Kleen962830d2012-05-08 13:32:02 +09302089 br_write_unlock(&vfsmount_lock);
Al Viro97216be2013-03-16 15:12:40 -04002090 namespace_unlock();
Al Viro19a167a2011-01-17 01:35:23 -05002091 }
Al Virob1e75df2011-01-17 01:47:59 -05002092 mntput(m);
2093 mntput(m);
Al Viro19a167a2011-01-17 01:35:23 -05002094 return err;
2095}
2096
David Howellsea5b7782011-01-14 19:10:03 +00002097/**
2098 * mnt_set_expiry - Put a mount on an expiration list
2099 * @mnt: The mount to list.
2100 * @expiry_list: The list to add the mount to.
2101 */
2102void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2103{
Al Viro97216be2013-03-16 15:12:40 -04002104 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302105 br_write_lock(&vfsmount_lock);
David Howellsea5b7782011-01-14 19:10:03 +00002106
Al Viro6776db3d2011-11-25 00:22:05 -05002107 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
David Howellsea5b7782011-01-14 19:10:03 +00002108
Andi Kleen962830d2012-05-08 13:32:02 +09302109 br_write_unlock(&vfsmount_lock);
Al Viro97216be2013-03-16 15:12:40 -04002110 namespace_unlock();
David Howellsea5b7782011-01-14 19:10:03 +00002111}
2112EXPORT_SYMBOL(mnt_set_expiry);
2113
2114/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 * process a list of expirable mountpoints with the intent of discarding any
2116 * mountpoints that aren't in use and haven't been touched since last we came
2117 * here
2118 */
2119void mark_mounts_for_expiry(struct list_head *mounts)
2120{
Al Viro761d5c32011-11-24 21:07:43 -05002121 struct mount *mnt, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 LIST_HEAD(graveyard);
2123
2124 if (list_empty(mounts))
2125 return;
2126
Al Viro97216be2013-03-16 15:12:40 -04002127 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302128 br_write_lock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* extract from the expiration list every vfsmount that matches the
2131 * following criteria:
2132 * - only referenced by its parent vfsmount
2133 * - still marked for expiry (marked on the last call here; marks are
2134 * cleared by mntput())
2135 */
Al Viro6776db3d2011-11-25 00:22:05 -05002136 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Al Viro863d6842011-11-25 00:57:42 -05002137 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Viro1ab59732011-11-24 21:35:16 -05002138 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 continue;
Al Viro6776db3d2011-11-25 00:22:05 -05002140 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Al Virobcc5c7d2008-03-22 00:21:53 -04002142 while (!list_empty(&graveyard)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002143 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002144 touch_mnt_namespace(mnt->mnt_ns);
Al Viro328e6d92013-03-16 14:49:45 -04002145 umount_tree(mnt, 1);
Al Virobcc5c7d2008-03-22 00:21:53 -04002146 }
Andi Kleen962830d2012-05-08 13:32:02 +09302147 br_write_unlock(&vfsmount_lock);
Al Viro3ab6abe2013-03-16 14:42:19 -04002148 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2152
2153/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04002154 * Ripoff of 'select_parent()'
2155 *
2156 * search the list of submounts for a given mountpoint, and move any
2157 * shrinkable submounts to the 'graveyard' list.
2158 */
Al Viro692afc32011-11-24 21:15:14 -05002159static int select_submounts(struct mount *parent, struct list_head *graveyard)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002160{
Al Viro692afc32011-11-24 21:15:14 -05002161 struct mount *this_parent = parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002162 struct list_head *next;
2163 int found = 0;
2164
2165repeat:
Al Viro6b41d532011-11-24 23:24:33 -05002166 next = this_parent->mnt_mounts.next;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002167resume:
Al Viro6b41d532011-11-24 23:24:33 -05002168 while (next != &this_parent->mnt_mounts) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002169 struct list_head *tmp = next;
Al Viro6b41d532011-11-24 23:24:33 -05002170 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002171
2172 next = tmp->next;
Al Viro692afc32011-11-24 21:15:14 -05002173 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
Trond Myklebust5528f9112006-06-09 09:34:17 -04002174 continue;
2175 /*
2176 * Descend a level if the d_mounts list is non-empty.
2177 */
Al Viro6b41d532011-11-24 23:24:33 -05002178 if (!list_empty(&mnt->mnt_mounts)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002179 this_parent = mnt;
2180 goto repeat;
2181 }
2182
Al Viro1ab59732011-11-24 21:35:16 -05002183 if (!propagate_mount_busy(mnt, 1)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002184 list_move_tail(&mnt->mnt_expire, graveyard);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002185 found++;
2186 }
2187 }
2188 /*
2189 * All done at this level ... ascend and resume the search
2190 */
2191 if (this_parent != parent) {
Al Viro6b41d532011-11-24 23:24:33 -05002192 next = this_parent->mnt_child.next;
Al Viro0714a532011-11-24 22:19:58 -05002193 this_parent = this_parent->mnt_parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002194 goto resume;
2195 }
2196 return found;
2197}
2198
2199/*
2200 * process a list of expirable mountpoints with the intent of discarding any
2201 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10002202 *
2203 * vfsmount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04002204 */
Al Virob54b9be2013-03-16 14:39:34 -04002205static void shrink_submounts(struct mount *mnt)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002206{
2207 LIST_HEAD(graveyard);
Al Viro761d5c32011-11-24 21:07:43 -05002208 struct mount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002209
Trond Myklebust5528f9112006-06-09 09:34:17 -04002210 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04002211 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04002212 while (!list_empty(&graveyard)) {
Al Viro761d5c32011-11-24 21:07:43 -05002213 m = list_first_entry(&graveyard, struct mount,
Al Viro6776db3d2011-11-25 00:22:05 -05002214 mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002215 touch_mnt_namespace(m->mnt_ns);
Al Viro328e6d92013-03-16 14:49:45 -04002216 umount_tree(m, 1);
Al Virobcc5c7d2008-03-22 00:21:53 -04002217 }
2218 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04002219}
2220
Trond Myklebust5528f9112006-06-09 09:34:17 -04002221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 * Some copy_from_user() implementations do not return the exact number of
2223 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2224 * Note that this function differs from copy_from_user() in that it will oops
2225 * on bad values of `to', rather than returning a short copy.
2226 */
Ram Paib58fed82005-11-07 17:16:09 -05002227static long exact_copy_from_user(void *to, const void __user * from,
2228 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 char *t = to;
2231 const char __user *f = from;
2232 char c;
2233
2234 if (!access_ok(VERIFY_READ, from, n))
2235 return n;
2236
2237 while (n) {
2238 if (__get_user(c, f)) {
2239 memset(t, 0, n);
2240 break;
2241 }
2242 *t++ = c;
2243 f++;
2244 n--;
2245 }
2246 return n;
2247}
2248
Ram Paib58fed82005-11-07 17:16:09 -05002249int copy_mount_options(const void __user * data, unsigned long *where)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 int i;
2252 unsigned long page;
2253 unsigned long size;
Ram Paib58fed82005-11-07 17:16:09 -05002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 *where = 0;
2256 if (!data)
2257 return 0;
2258
2259 if (!(page = __get_free_page(GFP_KERNEL)))
2260 return -ENOMEM;
2261
2262 /* We only care that *some* data at the address the user
2263 * gave us is valid. Just in case, we'll zero
2264 * the remainder of the page.
2265 */
2266 /* copy_from_user cannot cross TASK_SIZE ! */
2267 size = TASK_SIZE - (unsigned long)data;
2268 if (size > PAGE_SIZE)
2269 size = PAGE_SIZE;
2270
2271 i = size - exact_copy_from_user((void *)page, data, size);
2272 if (!i) {
Ram Paib58fed82005-11-07 17:16:09 -05002273 free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return -EFAULT;
2275 }
2276 if (i != PAGE_SIZE)
2277 memset((char *)page + i, 0, PAGE_SIZE - i);
2278 *where = page;
2279 return 0;
2280}
2281
Vegard Nossumeca6f532009-09-18 13:05:45 -07002282int copy_mount_string(const void __user *data, char **where)
2283{
2284 char *tmp;
2285
2286 if (!data) {
2287 *where = NULL;
2288 return 0;
2289 }
2290
2291 tmp = strndup_user(data, PAGE_SIZE);
2292 if (IS_ERR(tmp))
2293 return PTR_ERR(tmp);
2294
2295 *where = tmp;
2296 return 0;
2297}
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299/*
2300 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2301 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2302 *
2303 * data is a (void *) that can point to any structure up to
2304 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2305 * information (or be NULL).
2306 *
2307 * Pre-0.97 versions of mount() didn't have a flags word.
2308 * When the flags word was introduced its top half was required
2309 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2310 * Therefore, if this magic number is present, it carries no information
2311 * and must be discarded.
2312 */
Al Viro808d4e32012-10-11 11:42:01 -04002313long do_mount(const char *dev_name, const char *dir_name,
2314 const char *type_page, unsigned long flags, void *data_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Al Viro2d92ab32008-08-02 00:51:11 -04002316 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 int retval = 0;
2318 int mnt_flags = 0;
2319
2320 /* Discard magic */
2321 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2322 flags &= ~MS_MGC_MSK;
2323
2324 /* Basic sanity checks */
2325
2326 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 if (data_page)
2330 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2331
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002332 /* ... and get the mountpoint */
2333 retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2334 if (retval)
2335 return retval;
2336
2337 retval = security_sb_mount(dev_name, &path,
2338 type_page, flags, data_page);
Al Viro0d5cadb2013-05-04 14:40:51 -04002339 if (!retval && !may_mount())
2340 retval = -EPERM;
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002341 if (retval)
2342 goto dput_out;
2343
Andi Kleen613cbe32009-04-19 18:40:43 +02002344 /* Default to relatime unless overriden */
2345 if (!(flags & MS_NOATIME))
2346 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /* Separate the per-mountpoint flags */
2349 if (flags & MS_NOSUID)
2350 mnt_flags |= MNT_NOSUID;
2351 if (flags & MS_NODEV)
2352 mnt_flags |= MNT_NODEV;
2353 if (flags & MS_NOEXEC)
2354 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002355 if (flags & MS_NOATIME)
2356 mnt_flags |= MNT_NOATIME;
2357 if (flags & MS_NODIRATIME)
2358 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00002359 if (flags & MS_STRICTATIME)
2360 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002361 if (flags & MS_RDONLY)
2362 mnt_flags |= MNT_READONLY;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002363
Eric W. Biederman99dd97b2014-07-28 17:36:04 -07002364 /* The default atime for remount is preservation */
2365 if ((flags & MS_REMOUNT) &&
2366 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2367 MS_STRICTATIME)) == 0)) {
2368 mnt_flags &= ~MNT_ATIME_MASK;
2369 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2370 }
2371
Al Viro7a4dec52010-08-09 12:05:43 -04002372 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
Matthew Garrettd0adde52009-03-26 17:49:56 +00002373 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2374 MS_STRICTATIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (flags & MS_REMOUNT)
Al Viro2d92ab32008-08-02 00:51:11 -04002377 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 data_page);
2379 else if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002380 retval = do_loopback(&path, dev_name, flags & MS_REC);
Ram Pai9676f0c2005-11-07 17:21:20 -05002381 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Al Viro2d92ab32008-08-02 00:51:11 -04002382 retval = do_change_type(&path, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 else if (flags & MS_MOVE)
Al Viro2d92ab32008-08-02 00:51:11 -04002384 retval = do_move_mount(&path, dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 else
Al Viro2d92ab32008-08-02 00:51:11 -04002386 retval = do_new_mount(&path, type_page, flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 dev_name, data_page);
2388dput_out:
Al Viro2d92ab32008-08-02 00:51:11 -04002389 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return retval;
2391}
2392
Eric W. Biederman771b1372012-07-26 21:08:32 -07002393static void free_mnt_ns(struct mnt_namespace *ns)
2394{
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002395 proc_free_inum(ns->proc_inum);
Eric W. Biederman771b1372012-07-26 21:08:32 -07002396 put_user_ns(ns->user_ns);
2397 kfree(ns);
2398}
2399
Eric W. Biederman8823c072010-03-07 18:49:36 -08002400/*
2401 * Assign a sequence number so we can detect when we attempt to bind
2402 * mount a reference to an older mount namespace into the current
2403 * mount namespace, preventing reference counting loops. A 64bit
2404 * number incrementing at 10Ghz will take 12,427 years to wrap which
2405 * is effectively never, so we can ignore the possibility.
2406 */
2407static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2408
Eric W. Biederman771b1372012-07-26 21:08:32 -07002409static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002410{
2411 struct mnt_namespace *new_ns;
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002412 int ret;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002413
2414 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2415 if (!new_ns)
2416 return ERR_PTR(-ENOMEM);
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002417 ret = proc_alloc_inum(&new_ns->proc_inum);
2418 if (ret) {
2419 kfree(new_ns);
2420 return ERR_PTR(ret);
2421 }
Eric W. Biederman8823c072010-03-07 18:49:36 -08002422 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002423 atomic_set(&new_ns->count, 1);
2424 new_ns->root = NULL;
2425 INIT_LIST_HEAD(&new_ns->list);
2426 init_waitqueue_head(&new_ns->poll);
2427 new_ns->event = 0;
Eric W. Biederman771b1372012-07-26 21:08:32 -07002428 new_ns->user_ns = get_user_ns(user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002429 return new_ns;
2430}
2431
JANAK DESAI741a2952006-02-07 12:59:00 -08002432/*
2433 * Allocate a new namespace structure and populate it with contents
2434 * copied from the namespace of the passed in task structure.
2435 */
Badari Pulavartye3222c42007-05-08 00:25:21 -07002436static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
Eric W. Biederman771b1372012-07-26 21:08:32 -07002437 struct user_namespace *user_ns, struct fs_struct *fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002439 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04002440 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Al Viro315fc832011-11-24 18:57:30 -05002441 struct mount *p, *q;
Al Virobe08d6d2011-12-06 13:32:36 -05002442 struct mount *old = mnt_ns->root;
Al Virocb338d02011-11-24 20:55:08 -05002443 struct mount *new;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002444 int copy_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Eric W. Biederman771b1372012-07-26 21:08:32 -07002446 new_ns = alloc_mnt_ns(user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002447 if (IS_ERR(new_ns))
2448 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Al Viro97216be2013-03-16 15:12:40 -04002450 namespace_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* First pass: copy the tree topology */
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002452 copy_flags = CL_COPY_ALL | CL_EXPIRE;
2453 if (user_ns != mnt_ns->user_ns)
Eric W. Biederman132c94e2013-03-22 04:08:05 -07002454 copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07002455 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
David Howellsbe34d1a2012-06-25 12:55:18 +01002456 if (IS_ERR(new)) {
Al Viro328e6d92013-03-16 14:49:45 -04002457 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07002458 free_mnt_ns(new_ns);
David Howellsbe34d1a2012-06-25 12:55:18 +01002459 return ERR_CAST(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
Al Virobe08d6d2011-12-06 13:32:36 -05002461 new_ns->root = new;
Andi Kleen962830d2012-05-08 13:32:02 +09302462 br_write_lock(&vfsmount_lock);
Al Viro1a4eeaf2011-11-25 02:19:55 -05002463 list_add_tail(&new_ns->list, &new->mnt_list);
Andi Kleen962830d2012-05-08 13:32:02 +09302464 br_write_unlock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 /*
2467 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2468 * as belonging to new namespace. We have already acquired a private
2469 * fs_struct, so tsk->fs->lock is not needed.
2470 */
Al Viro909b0a82011-11-25 03:06:56 -05002471 p = old;
Al Virocb338d02011-11-24 20:55:08 -05002472 q = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 while (p) {
Al Viro143c8c92011-11-25 00:46:35 -05002474 q->mnt_ns = new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (fs) {
Al Viro315fc832011-11-24 18:57:30 -05002476 if (&p->mnt == fs->root.mnt) {
2477 fs->root.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05002478 rootmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Al Viro315fc832011-11-24 18:57:30 -05002480 if (&p->mnt == fs->pwd.mnt) {
2481 fs->pwd.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05002482 pwdmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Al Viro909b0a82011-11-25 03:06:56 -05002485 p = next_mnt(p, old);
2486 q = next_mnt(q, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Al Viro328e6d92013-03-16 14:49:45 -04002488 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (rootmnt)
Al Virof03c6592011-01-14 22:30:21 -05002491 mntput(rootmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (pwdmnt)
Al Virof03c6592011-01-14 22:30:21 -05002493 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
JANAK DESAI741a2952006-02-07 12:59:00 -08002495 return new_ns;
2496}
2497
Eric W. Biederman213dd262007-07-15 23:41:15 -07002498struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
Eric W. Biederman771b1372012-07-26 21:08:32 -07002499 struct user_namespace *user_ns, struct fs_struct *new_fs)
JANAK DESAI741a2952006-02-07 12:59:00 -08002500{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002501 struct mnt_namespace *new_ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002502
Badari Pulavartye3222c42007-05-08 00:25:21 -07002503 BUG_ON(!ns);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002504 get_mnt_ns(ns);
JANAK DESAI741a2952006-02-07 12:59:00 -08002505
2506 if (!(flags & CLONE_NEWNS))
Badari Pulavartye3222c42007-05-08 00:25:21 -07002507 return ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002508
Eric W. Biederman771b1372012-07-26 21:08:32 -07002509 new_ns = dup_mnt_ns(ns, user_ns, new_fs);
JANAK DESAI741a2952006-02-07 12:59:00 -08002510
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002511 put_mnt_ns(ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -07002512 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002515/**
2516 * create_mnt_ns - creates a private namespace and adds a root filesystem
2517 * @mnt: pointer to the new root filesystem mountpoint
2518 */
Al Viro1a4eeaf2011-11-25 02:19:55 -05002519static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002520{
Eric W. Biederman771b1372012-07-26 21:08:32 -07002521 struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002522 if (!IS_ERR(new_ns)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002523 struct mount *mnt = real_mount(m);
2524 mnt->mnt_ns = new_ns;
Al Virobe08d6d2011-12-06 13:32:36 -05002525 new_ns->root = mnt;
Al Virob1983cd2013-05-04 15:18:53 -04002526 list_add(&mnt->mnt_list, &new_ns->list);
Al Viroc1334492011-11-16 16:12:14 -05002527 } else {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002528 mntput(m);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002529 }
2530 return new_ns;
2531}
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002532
Al Viroea441d112011-11-16 21:43:59 -05002533struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2534{
2535 struct mnt_namespace *ns;
Al Virod31da0f2011-11-22 12:31:21 -05002536 struct super_block *s;
Al Viroea441d112011-11-16 21:43:59 -05002537 struct path path;
2538 int err;
2539
2540 ns = create_mnt_ns(mnt);
2541 if (IS_ERR(ns))
2542 return ERR_CAST(ns);
2543
2544 err = vfs_path_lookup(mnt->mnt_root, mnt,
2545 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2546
2547 put_mnt_ns(ns);
2548
2549 if (err)
2550 return ERR_PTR(err);
2551
2552 /* trade a vfsmount reference for active sb one */
Al Virod31da0f2011-11-22 12:31:21 -05002553 s = path.mnt->mnt_sb;
2554 atomic_inc(&s->s_active);
Al Viroea441d112011-11-16 21:43:59 -05002555 mntput(path.mnt);
2556 /* lock the sucker */
Al Virod31da0f2011-11-22 12:31:21 -05002557 down_write(&s->s_umount);
Al Viroea441d112011-11-16 21:43:59 -05002558 /* ... and return the root of (sub)tree on it */
2559 return path.dentry;
2560}
2561EXPORT_SYMBOL(mount_subtree);
2562
Heiko Carstensbdc480e2009-01-14 14:14:12 +01002563SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2564 char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Vegard Nossumeca6f532009-09-18 13:05:45 -07002566 int ret;
2567 char *kernel_type;
Jeff Layton91a27b22012-10-10 15:25:28 -04002568 struct filename *kernel_dir;
Vegard Nossumeca6f532009-09-18 13:05:45 -07002569 char *kernel_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned long data_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Vegard Nossumeca6f532009-09-18 13:05:45 -07002572 ret = copy_mount_string(type, &kernel_type);
2573 if (ret < 0)
2574 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Vegard Nossumeca6f532009-09-18 13:05:45 -07002576 kernel_dir = getname(dir_name);
2577 if (IS_ERR(kernel_dir)) {
2578 ret = PTR_ERR(kernel_dir);
2579 goto out_dir;
2580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Vegard Nossumeca6f532009-09-18 13:05:45 -07002582 ret = copy_mount_string(dev_name, &kernel_dev);
2583 if (ret < 0)
2584 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Vegard Nossumeca6f532009-09-18 13:05:45 -07002586 ret = copy_mount_options(data, &data_page);
2587 if (ret < 0)
2588 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Jeff Layton91a27b22012-10-10 15:25:28 -04002590 ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
Vegard Nossumeca6f532009-09-18 13:05:45 -07002591 (void *) data_page);
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 free_page(data_page);
Vegard Nossumeca6f532009-09-18 13:05:45 -07002594out_data:
2595 kfree(kernel_dev);
2596out_dev:
2597 putname(kernel_dir);
2598out_dir:
2599 kfree(kernel_type);
2600out_type:
2601 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
2604/*
Al Viroafac7cb2011-11-23 19:34:49 -05002605 * Return true if path is reachable from root
2606 *
2607 * namespace_sem or vfsmount_lock is held
2608 */
Al Viro643822b2011-11-24 22:00:28 -05002609bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
Al Viroafac7cb2011-11-23 19:34:49 -05002610 const struct path *root)
2611{
Al Viro643822b2011-11-24 22:00:28 -05002612 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
Al Viroa73324d2011-11-24 22:25:07 -05002613 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05002614 mnt = mnt->mnt_parent;
Al Viroafac7cb2011-11-23 19:34:49 -05002615 }
Al Viro643822b2011-11-24 22:00:28 -05002616 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
Al Viroafac7cb2011-11-23 19:34:49 -05002617}
2618
2619int path_is_under(struct path *path1, struct path *path2)
2620{
2621 int res;
Andi Kleen962830d2012-05-08 13:32:02 +09302622 br_read_lock(&vfsmount_lock);
Al Viro643822b2011-11-24 22:00:28 -05002623 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
Andi Kleen962830d2012-05-08 13:32:02 +09302624 br_read_unlock(&vfsmount_lock);
Al Viroafac7cb2011-11-23 19:34:49 -05002625 return res;
2626}
2627EXPORT_SYMBOL(path_is_under);
2628
2629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 * pivot_root Semantics:
2631 * Moves the root file system of the current process to the directory put_old,
2632 * makes new_root as the new root file system of the current process, and sets
2633 * root/cwd of all processes which had them on the current root to new_root.
2634 *
2635 * Restrictions:
2636 * The new_root and put_old must be directories, and must not be on the
2637 * same file system as the current process root. The put_old must be
2638 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2639 * pointed to by put_old must yield the same directory as new_root. No other
2640 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2641 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08002642 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2643 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2644 * in this situation.
2645 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 * Notes:
2647 * - we don't move root/cwd if they are not at the root (reason: if something
2648 * cared enough to change them, it's probably wrong to force them elsewhere)
2649 * - it's okay to pick a root that isn't the root of a file system, e.g.
2650 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2651 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2652 * first.
2653 */
Heiko Carstens3480b252009-01-14 14:14:16 +01002654SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2655 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Al Viro2d8f3032008-07-22 09:59:21 -04002657 struct path new, old, parent_path, root_parent, root;
Al Viro84d17192013-03-15 10:53:28 -04002658 struct mount *new_mnt, *root_mnt, *old_mnt;
2659 struct mountpoint *old_mp, *root_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 int error;
2661
Al Viro9b40bc92013-02-22 22:45:42 -05002662 if (!may_mount())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return -EPERM;
2664
Al Viro2d8f3032008-07-22 09:59:21 -04002665 error = user_path_dir(new_root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (error)
2667 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Al Viro2d8f3032008-07-22 09:59:21 -04002669 error = user_path_dir(put_old, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (error)
2671 goto out1;
2672
Al Viro2d8f3032008-07-22 09:59:21 -04002673 error = security_sb_pivotroot(&old, &new);
Al Virob12cea92011-03-18 08:55:38 -04002674 if (error)
2675 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002677 get_fs_root(current->fs, &root);
Al Viro84d17192013-03-15 10:53:28 -04002678 old_mp = lock_mount(&old);
2679 error = PTR_ERR(old_mp);
2680 if (IS_ERR(old_mp))
Al Virob12cea92011-03-18 08:55:38 -04002681 goto out3;
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 error = -EINVAL;
Al Viro419148d2011-11-24 19:41:16 -05002684 new_mnt = real_mount(new.mnt);
2685 root_mnt = real_mount(root.mnt);
Al Viro84d17192013-03-15 10:53:28 -04002686 old_mnt = real_mount(old.mnt);
2687 if (IS_MNT_SHARED(old_mnt) ||
Al Virofc7be132011-11-25 01:05:37 -05002688 IS_MNT_SHARED(new_mnt->mnt_parent) ||
2689 IS_MNT_SHARED(root_mnt->mnt_parent))
Al Virob12cea92011-03-18 08:55:38 -04002690 goto out4;
Al Viro143c8c92011-11-25 00:46:35 -05002691 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002692 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002694 if (d_unlinked(new.dentry))
Al Virob12cea92011-03-18 08:55:38 -04002695 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 error = -EBUSY;
Al Viro84d17192013-03-15 10:53:28 -04002697 if (new_mnt == root_mnt || old_mnt == root_mnt)
Al Virob12cea92011-03-18 08:55:38 -04002698 goto out4; /* loop, on the same file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04002700 if (root.mnt->mnt_root != root.dentry)
Al Virob12cea92011-03-18 08:55:38 -04002701 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05002702 if (!mnt_has_parent(root_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002703 goto out4; /* not attached */
Al Viro84d17192013-03-15 10:53:28 -04002704 root_mp = root_mnt->mnt_mp;
Al Viro2d8f3032008-07-22 09:59:21 -04002705 if (new.mnt->mnt_root != new.dentry)
Al Virob12cea92011-03-18 08:55:38 -04002706 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05002707 if (!mnt_has_parent(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002708 goto out4; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08002709 /* make sure we can reach put_old from new_root */
Al Viro84d17192013-03-15 10:53:28 -04002710 if (!is_path_reachable(old_mnt, old.dentry, &new))
Al Virob12cea92011-03-18 08:55:38 -04002711 goto out4;
Eric W. Biederman315a75e2014-10-08 10:42:27 -07002712 /* make certain new is below the root */
2713 if (!is_path_reachable(new_mnt, new.dentry, &root))
2714 goto out4;
Al Viro84d17192013-03-15 10:53:28 -04002715 root_mp->m_count++; /* pin it so it won't go away */
Andi Kleen962830d2012-05-08 13:32:02 +09302716 br_write_lock(&vfsmount_lock);
Al Viro419148d2011-11-24 19:41:16 -05002717 detach_mnt(new_mnt, &parent_path);
2718 detach_mnt(root_mnt, &root_parent);
Jan Blunck4ac91372008-02-14 19:34:32 -08002719 /* mount old root on put_old */
Al Viro84d17192013-03-15 10:53:28 -04002720 attach_mnt(root_mnt, old_mnt, old_mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08002721 /* mount new_root on / */
Al Viro84d17192013-03-15 10:53:28 -04002722 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002723 touch_mnt_namespace(current->nsproxy->mnt_ns);
Andi Kleen962830d2012-05-08 13:32:02 +09302724 br_write_unlock(&vfsmount_lock);
Al Viro2d8f3032008-07-22 09:59:21 -04002725 chroot_fs_refs(&root, &new);
Al Viro84d17192013-03-15 10:53:28 -04002726 put_mountpoint(root_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 error = 0;
Al Virob12cea92011-03-18 08:55:38 -04002728out4:
Al Viro84d17192013-03-15 10:53:28 -04002729 unlock_mount(old_mp);
Al Virob12cea92011-03-18 08:55:38 -04002730 if (!error) {
2731 path_put(&root_parent);
2732 path_put(&parent_path);
2733 }
2734out3:
Al Viro8c3ee422008-03-22 18:00:39 -04002735 path_put(&root);
Al Virob12cea92011-03-18 08:55:38 -04002736out2:
Al Viro2d8f3032008-07-22 09:59:21 -04002737 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738out1:
Al Viro2d8f3032008-07-22 09:59:21 -04002739 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
2744static void __init init_mount_tree(void)
2745{
2746 struct vfsmount *mnt;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002747 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08002748 struct path root;
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002749 struct file_system_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002751 type = get_fs_type("rootfs");
2752 if (!type)
2753 panic("Can't find rootfs type");
2754 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
2755 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (IS_ERR(mnt))
2757 panic("Can't create rootfs");
Nick Pigginb3e19d92011-01-07 17:50:11 +11002758
Trond Myklebust3b22edc2009-06-23 17:29:49 -04002759 ns = create_mnt_ns(mnt);
2760 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 panic("Can't allocate initial namespace");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002763 init_task.nsproxy->mnt_ns = ns;
2764 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Al Virobe08d6d2011-12-06 13:32:36 -05002766 root.mnt = mnt;
2767 root.dentry = mnt->mnt_root;
Jan Blunckac748a02008-02-14 19:34:39 -08002768
2769 set_fs_pwd(current->fs, &root);
2770 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
Denis Cheng74bf17c2007-10-16 23:26:30 -07002773void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Eric Dumazet13f14b42008-02-06 01:37:57 -08002775 unsigned u;
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002776 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Ram Pai390c6842005-11-07 17:17:51 -05002778 init_rwsem(&namespace_sem);
2779
Al Viro7d6fec42011-11-23 12:14:10 -05002780 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
Paul Mundt20c2df82007-07-20 10:11:58 +09002781 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Ram Paib58fed82005-11-07 17:16:09 -05002783 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
Al Viro84d17192013-03-15 10:53:28 -04002784 mountpoint_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Al Viro84d17192013-03-15 10:53:28 -04002786 if (!mount_hashtable || !mountpoint_hashtable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 panic("Failed to allocate mount hash table\n");
2788
Mandeep Singh Baines80cdc6d2011-03-22 16:33:54 -07002789 printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Eric Dumazet13f14b42008-02-06 01:37:57 -08002791 for (u = 0; u < HASH_SIZE; u++)
2792 INIT_LIST_HEAD(&mount_hashtable[u]);
Al Viro84d17192013-03-15 10:53:28 -04002793 for (u = 0; u < HASH_SIZE; u++)
2794 INIT_LIST_HEAD(&mountpoint_hashtable[u]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Andi Kleen962830d2012-05-08 13:32:02 +09302796 br_lock_init(&vfsmount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +10002797
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002798 err = sysfs_init();
2799 if (err)
2800 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002801 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06002802 fs_kobj = kobject_create_and_add("fs", NULL);
2803 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002804 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 init_rootfs();
2806 init_mount_tree();
2807}
2808
Trond Myklebust616511d2009-06-22 15:09:13 -04002809void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Al Virod498b252010-02-05 02:21:06 -05002811 if (!atomic_dec_and_test(&ns->count))
Trond Myklebust616511d2009-06-22 15:09:13 -04002812 return;
Al Viro97216be2013-03-16 15:12:40 -04002813 namespace_lock();
Andi Kleen962830d2012-05-08 13:32:02 +09302814 br_write_lock(&vfsmount_lock);
Al Viro328e6d92013-03-16 14:49:45 -04002815 umount_tree(ns->root, 0);
Andi Kleen962830d2012-05-08 13:32:02 +09302816 br_write_unlock(&vfsmount_lock);
Al Viro3ab6abe2013-03-16 14:42:19 -04002817 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07002818 free_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
Al Viro9d412a42011-03-17 22:08:28 -04002820
2821struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
2822{
Tim Chen423e0ab2011-07-19 09:32:38 -07002823 struct vfsmount *mnt;
2824 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2825 if (!IS_ERR(mnt)) {
2826 /*
2827 * it is a longterm mount, don't release mnt until
2828 * we unmount before file sys is unregistered
2829 */
Al Virof7a99c52012-06-09 00:59:08 -04002830 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
Tim Chen423e0ab2011-07-19 09:32:38 -07002831 }
2832 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04002833}
2834EXPORT_SYMBOL_GPL(kern_mount_data);
Tim Chen423e0ab2011-07-19 09:32:38 -07002835
2836void kern_unmount(struct vfsmount *mnt)
2837{
2838 /* release long term mount so mount point can be released */
2839 if (!IS_ERR_OR_NULL(mnt)) {
Al Virof7a99c52012-06-09 00:59:08 -04002840 br_write_lock(&vfsmount_lock);
2841 real_mount(mnt)->mnt_ns = NULL;
2842 br_write_unlock(&vfsmount_lock);
Tim Chen423e0ab2011-07-19 09:32:38 -07002843 mntput(mnt);
2844 }
2845}
2846EXPORT_SYMBOL(kern_unmount);
Al Viro02125a82011-12-05 08:43:34 -05002847
2848bool our_mnt(struct vfsmount *mnt)
2849{
Al Viro143c8c92011-11-25 00:46:35 -05002850 return check_mnt(real_mount(mnt));
Al Viro02125a82011-12-05 08:43:34 -05002851}
Eric W. Biederman8823c072010-03-07 18:49:36 -08002852
Eric W. Biederman31515272013-03-15 01:45:51 -07002853bool current_chrooted(void)
2854{
2855 /* Does the current process have a non-standard root */
2856 struct path ns_root;
2857 struct path fs_root;
2858 bool chrooted;
2859
2860 /* Find the namespace root */
2861 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
2862 ns_root.dentry = ns_root.mnt->mnt_root;
2863 path_get(&ns_root);
2864 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
2865 ;
2866
2867 get_fs_root(current->fs, &fs_root);
2868
2869 chrooted = !path_equal(&fs_root, &ns_root);
2870
2871 path_put(&fs_root);
2872 path_put(&ns_root);
2873
2874 return chrooted;
2875}
2876
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07002877void update_mnt_policy(struct user_namespace *userns)
2878{
2879 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
2880 struct mount *mnt;
2881
2882 down_read(&namespace_sem);
2883 list_for_each_entry(mnt, &ns->list, mnt_list) {
2884 switch (mnt->mnt.mnt_sb->s_magic) {
2885 case SYSFS_MAGIC:
2886 userns->may_mount_sysfs = true;
2887 break;
2888 case PROC_SUPER_MAGIC:
2889 userns->may_mount_proc = true;
2890 break;
2891 }
2892 if (userns->may_mount_sysfs && userns->may_mount_proc)
2893 break;
2894 }
2895 up_read(&namespace_sem);
2896}
2897
Eric W. Biederman8823c072010-03-07 18:49:36 -08002898static void *mntns_get(struct task_struct *task)
2899{
2900 struct mnt_namespace *ns = NULL;
2901 struct nsproxy *nsproxy;
2902
2903 rcu_read_lock();
2904 nsproxy = task_nsproxy(task);
2905 if (nsproxy) {
2906 ns = nsproxy->mnt_ns;
2907 get_mnt_ns(ns);
2908 }
2909 rcu_read_unlock();
2910
2911 return ns;
2912}
2913
2914static void mntns_put(void *ns)
2915{
2916 put_mnt_ns(ns);
2917}
2918
2919static int mntns_install(struct nsproxy *nsproxy, void *ns)
2920{
2921 struct fs_struct *fs = current->fs;
2922 struct mnt_namespace *mnt_ns = ns;
2923 struct path root;
2924
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002925 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biederman5e4a0842012-12-14 07:55:36 -08002926 !nsown_capable(CAP_SYS_CHROOT) ||
2927 !nsown_capable(CAP_SYS_ADMIN))
Zhao Hongjiangae11e0f2012-09-13 16:38:03 +08002928 return -EPERM;
Eric W. Biederman8823c072010-03-07 18:49:36 -08002929
2930 if (fs->users != 1)
2931 return -EINVAL;
2932
2933 get_mnt_ns(mnt_ns);
2934 put_mnt_ns(nsproxy->mnt_ns);
2935 nsproxy->mnt_ns = mnt_ns;
2936
2937 /* Find the root */
2938 root.mnt = &mnt_ns->root->mnt;
2939 root.dentry = mnt_ns->root->mnt.mnt_root;
2940 path_get(&root);
2941 while(d_mountpoint(root.dentry) && follow_down_one(&root))
2942 ;
2943
2944 /* Update the pwd and root */
2945 set_fs_pwd(fs, &root);
2946 set_fs_root(fs, &root);
2947
2948 path_put(&root);
2949 return 0;
2950}
2951
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002952static unsigned int mntns_inum(void *ns)
2953{
2954 struct mnt_namespace *mnt_ns = ns;
2955 return mnt_ns->proc_inum;
2956}
2957
Eric W. Biederman8823c072010-03-07 18:49:36 -08002958const struct proc_ns_operations mntns_operations = {
2959 .name = "mnt",
2960 .type = CLONE_NEWNS,
2961 .get = mntns_get,
2962 .put = mntns_put,
2963 .install = mntns_install,
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002964 .inum = mntns_inum,
Eric W. Biederman8823c072010-03-07 18:49:36 -08002965};