blob: 98ebc78b21ab7cc1678f8f40da3b9343e22d5111 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/namei.h>
16#include <linux/security.h>
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010017#include <linux/idr.h>
Al Virod10577a2011-12-07 13:06:11 -050018#include <linux/acct.h> /* acct_auto_close_mnt */
19#include <linux/ramfs.h> /* init_rootfs */
20#include <linux/fs_struct.h> /* get_fs_root et.al. */
21#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
22#include <linux/uaccess.h>
Ram Pai07b20882005-11-07 17:19:07 -050023#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070024#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Eric Dumazet13f14b42008-02-06 01:37:57 -080026#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
27#define HASH_SIZE (1UL << HASH_SHIFT)
28
Al Viro5addc5d2005-11-07 17:15:49 -050029static int event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010030static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010031static DEFINE_IDA(mnt_group_ida);
Nick Piggin99b7db72010-08-18 04:37:39 +100032static DEFINE_SPINLOCK(mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040033static int mnt_id_start = 0;
34static int mnt_group_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric Dumazetfa3536c2006-03-26 01:37:24 -080036static struct list_head *mount_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080037static struct kmem_cache *mnt_cache __read_mostly;
Ram Pai390c6842005-11-07 17:17:51 -050038static struct rw_semaphore namespace_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080040/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060041struct kobject *fs_kobj;
42EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080043
Nick Piggin99b7db72010-08-18 04:37:39 +100044/*
45 * vfsmount lock may be taken for read to prevent changes to the
46 * vfsmount hash, ie. during mountpoint lookups or walking back
47 * up the tree.
48 *
49 * It should be taken for write in all cases where the vfsmount
50 * tree or hash is modified or when a vfsmount structure is modified.
51 */
52DEFINE_BRLOCK(vfsmount_lock);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
55{
Ram Paib58fed82005-11-07 17:16:09 -050056 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
57 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Eric Dumazet13f14b42008-02-06 01:37:57 -080058 tmp = tmp + (tmp >> HASH_SHIFT);
59 return tmp & (HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Dave Hansen3d733632008-02-15 14:37:59 -080062#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
63
Nick Piggin99b7db72010-08-18 04:37:39 +100064/*
65 * allocation is serialized by namespace_sem, but we need the spinlock to
66 * serialize with freeing.
67 */
Al Virob105e272011-11-24 20:38:33 -050068static int mnt_alloc_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010069{
70 int res;
71
72retry:
73 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
Nick Piggin99b7db72010-08-18 04:37:39 +100074 spin_lock(&mnt_id_lock);
Al Viro15169fe2011-11-25 00:50:41 -050075 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
Al Virof21f6222009-06-24 03:12:00 -040076 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -050077 mnt_id_start = mnt->mnt_id + 1;
Nick Piggin99b7db72010-08-18 04:37:39 +100078 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010079 if (res == -EAGAIN)
80 goto retry;
81
82 return res;
83}
84
Al Virob105e272011-11-24 20:38:33 -050085static void mnt_free_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010086{
Al Viro15169fe2011-11-25 00:50:41 -050087 int id = mnt->mnt_id;
Nick Piggin99b7db72010-08-18 04:37:39 +100088 spin_lock(&mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040089 ida_remove(&mnt_id_ida, id);
90 if (mnt_id_start > id)
91 mnt_id_start = id;
Nick Piggin99b7db72010-08-18 04:37:39 +100092 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010093}
94
Miklos Szeredi719f5d72008-03-27 13:06:23 +010095/*
96 * Allocate a new peer group ID
97 *
98 * mnt_group_ida is protected by namespace_sem
99 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500100static int mnt_alloc_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100101{
Al Virof21f6222009-06-24 03:12:00 -0400102 int res;
103
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100104 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
105 return -ENOMEM;
106
Al Virof21f6222009-06-24 03:12:00 -0400107 res = ida_get_new_above(&mnt_group_ida,
108 mnt_group_start,
Al Viro15169fe2011-11-25 00:50:41 -0500109 &mnt->mnt_group_id);
Al Virof21f6222009-06-24 03:12:00 -0400110 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -0500111 mnt_group_start = mnt->mnt_group_id + 1;
Al Virof21f6222009-06-24 03:12:00 -0400112
113 return res;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100114}
115
116/*
117 * Release a peer group ID
118 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500119void mnt_release_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100120{
Al Viro15169fe2011-11-25 00:50:41 -0500121 int id = mnt->mnt_group_id;
Al Virof21f6222009-06-24 03:12:00 -0400122 ida_remove(&mnt_group_ida, id);
123 if (mnt_group_start > id)
124 mnt_group_start = id;
Al Viro15169fe2011-11-25 00:50:41 -0500125 mnt->mnt_group_id = 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100126}
127
Nick Pigginb3e19d92011-01-07 17:50:11 +1100128/*
129 * vfsmount lock must be held for read
130 */
Al Viro83adc752011-11-24 22:37:54 -0500131static inline void mnt_add_count(struct mount *mnt, int n)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100132{
133#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500134 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100135#else
136 preempt_disable();
Al Viro68e8a9f2011-11-24 22:53:09 -0500137 mnt->mnt_count += n;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100138 preempt_enable();
139#endif
140}
141
Nick Pigginb3e19d92011-01-07 17:50:11 +1100142/*
143 * vfsmount lock must be held for write
144 */
Al Viro83adc752011-11-24 22:37:54 -0500145unsigned int mnt_get_count(struct mount *mnt)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100146{
147#ifdef CONFIG_SMP
Al Virof03c6592011-01-14 22:30:21 -0500148 unsigned int count = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100149 int cpu;
150
151 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500152 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100153 }
154
155 return count;
156#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500157 return mnt->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100158#endif
159}
160
Al Virob105e272011-11-24 20:38:33 -0500161static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Al Viroc63181e2011-11-25 02:35:16 -0500163 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
164 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100165 int err;
166
Al Viroc63181e2011-11-25 02:35:16 -0500167 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800168 if (err)
169 goto out_free_cache;
170
171 if (name) {
Al Viroc63181e2011-11-25 02:35:16 -0500172 mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
173 if (!mnt->mnt_devname)
Li Zefan88b38782008-07-21 18:06:36 +0800174 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100175 }
176
Nick Pigginb3e19d92011-01-07 17:50:11 +1100177#ifdef CONFIG_SMP
Al Viroc63181e2011-11-25 02:35:16 -0500178 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
179 if (!mnt->mnt_pcp)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100180 goto out_free_devname;
181
Al Viroc63181e2011-11-25 02:35:16 -0500182 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100183#else
Al Viroc63181e2011-11-25 02:35:16 -0500184 mnt->mnt_count = 1;
185 mnt->mnt_writers = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100186#endif
187
Al Viroc63181e2011-11-25 02:35:16 -0500188 INIT_LIST_HEAD(&mnt->mnt_hash);
189 INIT_LIST_HEAD(&mnt->mnt_child);
190 INIT_LIST_HEAD(&mnt->mnt_mounts);
191 INIT_LIST_HEAD(&mnt->mnt_list);
192 INIT_LIST_HEAD(&mnt->mnt_expire);
193 INIT_LIST_HEAD(&mnt->mnt_share);
194 INIT_LIST_HEAD(&mnt->mnt_slave_list);
195 INIT_LIST_HEAD(&mnt->mnt_slave);
Andreas Gruenbacher2504c5d2009-12-17 21:24:27 -0500196#ifdef CONFIG_FSNOTIFY
197 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Al Viroc63181e2011-11-25 02:35:16 -0500200 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800201
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000202#ifdef CONFIG_SMP
203out_free_devname:
Al Viroc63181e2011-11-25 02:35:16 -0500204 kfree(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000205#endif
Li Zefan88b38782008-07-21 18:06:36 +0800206out_free_id:
Al Viroc63181e2011-11-25 02:35:16 -0500207 mnt_free_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800208out_free_cache:
Al Viroc63181e2011-11-25 02:35:16 -0500209 kmem_cache_free(mnt_cache, mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800210 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Dave Hansen83660252008-02-15 14:37:30 -0800213/*
214 * Most r/o checks on a fs are for operations that take
215 * discrete amounts of time, like a write() or unlink().
216 * We must keep track of when those operations start
217 * (for permission checks) and when they end, so that
218 * we can determine when writes are able to occur to
219 * a filesystem.
220 */
Dave Hansen3d733632008-02-15 14:37:59 -0800221/*
222 * __mnt_is_readonly: check whether a mount is read-only
223 * @mnt: the mount to check for its write status
224 *
225 * This shouldn't be used directly ouside of the VFS.
226 * It does not guarantee that the filesystem will stay
227 * r/w, just that it is right *now*. This can not and
228 * should not be used in place of IS_RDONLY(inode).
229 * mnt_want/drop_write() will _keep_ the filesystem
230 * r/w.
231 */
232int __mnt_is_readonly(struct vfsmount *mnt)
233{
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800234 if (mnt->mnt_flags & MNT_READONLY)
235 return 1;
236 if (mnt->mnt_sb->s_flags & MS_RDONLY)
237 return 1;
238 return 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800239}
240EXPORT_SYMBOL_GPL(__mnt_is_readonly);
241
Al Viro83adc752011-11-24 22:37:54 -0500242static inline void mnt_inc_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800243{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000244#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500245 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000246#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500247 mnt->mnt_writers++;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000248#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800249}
Dave Hansen3d733632008-02-15 14:37:59 -0800250
Al Viro83adc752011-11-24 22:37:54 -0500251static inline void mnt_dec_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800252{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000253#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500254 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000255#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500256 mnt->mnt_writers--;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000257#endif
258}
259
Al Viro83adc752011-11-24 22:37:54 -0500260static unsigned int mnt_get_writers(struct mount *mnt)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000261{
262#ifdef CONFIG_SMP
263 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800264 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800265
266 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500267 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
Dave Hansen3d733632008-02-15 14:37:59 -0800268 }
Dave Hansen3d733632008-02-15 14:37:59 -0800269
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000270 return count;
271#else
272 return mnt->mnt_writers;
273#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800274}
275
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100276static int mnt_is_readonly(struct vfsmount *mnt)
277{
278 if (mnt->mnt_sb->s_readonly_remount)
279 return 1;
280 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
281 smp_rmb();
282 return __mnt_is_readonly(mnt);
283}
284
Dave Hansen3d733632008-02-15 14:37:59 -0800285/*
286 * Most r/o checks on a fs are for operations that take
287 * discrete amounts of time, like a write() or unlink().
288 * We must keep track of when those operations start
289 * (for permission checks) and when they end, so that
290 * we can determine when writes are able to occur to
291 * a filesystem.
292 */
Dave Hansen83660252008-02-15 14:37:30 -0800293/**
294 * mnt_want_write - get write access to a mount
Al Viro83adc752011-11-24 22:37:54 -0500295 * @m: the mount on which to take a write
Dave Hansen83660252008-02-15 14:37:30 -0800296 *
297 * This tells the low-level filesystem that a write is
298 * about to be performed to it, and makes sure that
299 * writes are allowed before returning success. When
300 * the write operation is finished, mnt_drop_write()
301 * must be called. This is effectively a refcount.
302 */
Al Viro83adc752011-11-24 22:37:54 -0500303int mnt_want_write(struct vfsmount *m)
Dave Hansen83660252008-02-15 14:37:30 -0800304{
Al Viro83adc752011-11-24 22:37:54 -0500305 struct mount *mnt = real_mount(m);
Dave Hansen3d733632008-02-15 14:37:59 -0800306 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800307
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000308 preempt_disable();
Nick Pigginc6653a82011-01-07 17:50:10 +1100309 mnt_inc_writers(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000310 /*
Nick Pigginc6653a82011-01-07 17:50:10 +1100311 * The store to mnt_inc_writers must be visible before we pass
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000312 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
313 * incremented count after it has set MNT_WRITE_HOLD.
314 */
315 smp_mb();
Al Viro83adc752011-11-24 22:37:54 -0500316 while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000317 cpu_relax();
318 /*
319 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
320 * be set to match its requirements. So we must not load that until
321 * MNT_WRITE_HOLD is cleared.
322 */
323 smp_rmb();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100324 if (mnt_is_readonly(m)) {
Nick Pigginc6653a82011-01-07 17:50:10 +1100325 mnt_dec_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800326 ret = -EROFS;
327 goto out;
328 }
Dave Hansen3d733632008-02-15 14:37:59 -0800329out:
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000330 preempt_enable();
Dave Hansen3d733632008-02-15 14:37:59 -0800331 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800332}
333EXPORT_SYMBOL_GPL(mnt_want_write);
334
335/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000336 * mnt_clone_write - get write access to a mount
337 * @mnt: the mount on which to take a write
338 *
339 * This is effectively like mnt_want_write, except
340 * it must only be used to take an extra write reference
341 * on a mountpoint that we already know has a write reference
342 * on it. This allows some optimisation.
343 *
344 * After finished, mnt_drop_write must be called as usual to
345 * drop the reference.
346 */
347int mnt_clone_write(struct vfsmount *mnt)
348{
349 /* superblock may be r/o */
350 if (__mnt_is_readonly(mnt))
351 return -EROFS;
352 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500353 mnt_inc_writers(real_mount(mnt));
npiggin@suse.de96029c42009-04-26 20:25:55 +1000354 preempt_enable();
355 return 0;
356}
357EXPORT_SYMBOL_GPL(mnt_clone_write);
358
359/**
360 * mnt_want_write_file - get write access to a file's mount
361 * @file: the file who's mount on which to take a write
362 *
363 * This is like mnt_want_write, but it takes a file and can
364 * do some optimisations if the file is open for write already
365 */
366int mnt_want_write_file(struct file *file)
367{
OGAWA Hirofumi2d8dd382009-08-06 15:07:39 -0700368 struct inode *inode = file->f_dentry->d_inode;
369 if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
npiggin@suse.de96029c42009-04-26 20:25:55 +1000370 return mnt_want_write(file->f_path.mnt);
371 else
372 return mnt_clone_write(file->f_path.mnt);
373}
374EXPORT_SYMBOL_GPL(mnt_want_write_file);
375
376/**
Dave Hansen83660252008-02-15 14:37:30 -0800377 * mnt_drop_write - give up write access to a mount
378 * @mnt: the mount on which to give up write access
379 *
380 * Tells the low-level filesystem that we are done
381 * performing writes to it. Must be matched with
382 * mnt_want_write() call above.
383 */
384void mnt_drop_write(struct vfsmount *mnt)
385{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000386 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500387 mnt_dec_writers(real_mount(mnt));
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000388 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800389}
390EXPORT_SYMBOL_GPL(mnt_drop_write);
391
Al Viro2a79f172011-12-09 08:06:57 -0500392void mnt_drop_write_file(struct file *file)
393{
394 mnt_drop_write(file->f_path.mnt);
395}
396EXPORT_SYMBOL(mnt_drop_write_file);
397
Al Viro83adc752011-11-24 22:37:54 -0500398static int mnt_make_readonly(struct mount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800399{
Dave Hansen3d733632008-02-15 14:37:59 -0800400 int ret = 0;
401
Nick Piggin99b7db72010-08-18 04:37:39 +1000402 br_write_lock(vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -0500403 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000404 /*
405 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
406 * should be visible before we do.
407 */
408 smp_mb();
409
410 /*
411 * With writers on hold, if this value is zero, then there are
412 * definitely no active writers (although held writers may subsequently
413 * increment the count, they'll have to wait, and decrement it after
414 * seeing MNT_READONLY).
415 *
416 * It is OK to have counter incremented on one CPU and decremented on
417 * another: the sum will add up correctly. The danger would be when we
418 * sum up each counter, if we read a counter before it is incremented,
419 * but then read another CPU's count which it has been subsequently
420 * decremented from -- we would see more decrements than we should.
421 * MNT_WRITE_HOLD protects against this scenario, because
422 * mnt_want_write first increments count, then smp_mb, then spins on
423 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
424 * we're counting up here.
425 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100426 if (mnt_get_writers(mnt) > 0)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000427 ret = -EBUSY;
428 else
Al Viro83adc752011-11-24 22:37:54 -0500429 mnt->mnt.mnt_flags |= MNT_READONLY;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000430 /*
431 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
432 * that become unheld will see MNT_READONLY.
433 */
434 smp_wmb();
Al Viro83adc752011-11-24 22:37:54 -0500435 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
Nick Piggin99b7db72010-08-18 04:37:39 +1000436 br_write_unlock(vfsmount_lock);
Dave Hansen3d733632008-02-15 14:37:59 -0800437 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800438}
Dave Hansen83660252008-02-15 14:37:30 -0800439
Al Viro83adc752011-11-24 22:37:54 -0500440static void __mnt_unmake_readonly(struct mount *mnt)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800441{
Nick Piggin99b7db72010-08-18 04:37:39 +1000442 br_write_lock(vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -0500443 mnt->mnt.mnt_flags &= ~MNT_READONLY;
Nick Piggin99b7db72010-08-18 04:37:39 +1000444 br_write_unlock(vfsmount_lock);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800445}
446
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100447int sb_prepare_remount_readonly(struct super_block *sb)
448{
449 struct mount *mnt;
450 int err = 0;
451
452 br_write_lock(vfsmount_lock);
453 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
454 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
455 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
456 smp_mb();
457 if (mnt_get_writers(mnt) > 0) {
458 err = -EBUSY;
459 break;
460 }
461 }
462 }
463 if (!err) {
464 sb->s_readonly_remount = 1;
465 smp_wmb();
466 }
467 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
468 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
469 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
470 }
471 br_write_unlock(vfsmount_lock);
472
473 return err;
474}
475
Al Virob105e272011-11-24 20:38:33 -0500476static void free_vfsmnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Al Viro52ba1622011-11-25 02:25:17 -0500478 kfree(mnt->mnt_devname);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100479 mnt_free_id(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000480#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500481 free_percpu(mnt->mnt_pcp);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000482#endif
Al Virob105e272011-11-24 20:38:33 -0500483 kmem_cache_free(mnt_cache, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/*
Ram Paia05964f2005-11-07 17:20:17 -0500487 * find the first or last mount at @dentry on vfsmount @mnt depending on
488 * @dir. If @dir is set return the first mount else return the last mount.
Nick Piggin99b7db72010-08-18 04:37:39 +1000489 * vfsmount_lock must be held for read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
Al Viroc7105362011-11-24 18:22:03 -0500491struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
Ram Paia05964f2005-11-07 17:20:17 -0500492 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Ram Paib58fed82005-11-07 17:16:09 -0500494 struct list_head *head = mount_hashtable + hash(mnt, dentry);
495 struct list_head *tmp = head;
Al Viroc7105362011-11-24 18:22:03 -0500496 struct mount *p, *found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 for (;;) {
Ram Paia05964f2005-11-07 17:20:17 -0500499 tmp = dir ? tmp->next : tmp->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 p = NULL;
501 if (tmp == head)
502 break;
Al Viro1b8e5562011-11-24 21:01:32 -0500503 p = list_entry(tmp, struct mount, mnt_hash);
Al Viroa73324d2011-11-24 22:25:07 -0500504 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
Ram Paia05964f2005-11-07 17:20:17 -0500505 found = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 }
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return found;
510}
511
Ram Paia05964f2005-11-07 17:20:17 -0500512/*
513 * lookup_mnt increments the ref count before returning
514 * the vfsmount struct.
515 */
Al Viro1c755af2009-04-18 14:06:57 -0400516struct vfsmount *lookup_mnt(struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500517{
Al Viroc7105362011-11-24 18:22:03 -0500518 struct mount *child_mnt;
Nick Piggin99b7db72010-08-18 04:37:39 +1000519
520 br_read_lock(vfsmount_lock);
Al Viroc7105362011-11-24 18:22:03 -0500521 child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
522 if (child_mnt) {
523 mnt_add_count(child_mnt, 1);
524 br_read_unlock(vfsmount_lock);
525 return &child_mnt->mnt;
526 } else {
527 br_read_unlock(vfsmount_lock);
528 return NULL;
529 }
Ram Paia05964f2005-11-07 17:20:17 -0500530}
531
Al Viro143c8c92011-11-25 00:46:35 -0500532static inline int check_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800534 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Nick Piggin99b7db72010-08-18 04:37:39 +1000537/*
538 * vfsmount lock must be held for write
539 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800540static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500541{
542 if (ns) {
543 ns->event = ++event;
544 wake_up_interruptible(&ns->poll);
545 }
546}
547
Nick Piggin99b7db72010-08-18 04:37:39 +1000548/*
549 * vfsmount lock must be held for write
550 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800551static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500552{
553 if (ns && ns->event != event) {
554 ns->event = event;
555 wake_up_interruptible(&ns->poll);
556 }
557}
558
Nick Piggin99b7db72010-08-18 04:37:39 +1000559/*
Nick Piggin5f57cbc2011-01-07 17:49:54 +1100560 * Clear dentry's mounted state if it has no remaining mounts.
561 * vfsmount_lock must be held for write.
562 */
Al Viroaa0a4cf2011-11-24 19:31:36 -0500563static void dentry_reset_mounted(struct dentry *dentry)
Nick Piggin5f57cbc2011-01-07 17:49:54 +1100564{
565 unsigned u;
566
567 for (u = 0; u < HASH_SIZE; u++) {
Al Virod5e50f72011-11-24 20:58:57 -0500568 struct mount *p;
Nick Piggin5f57cbc2011-01-07 17:49:54 +1100569
Al Viro1b8e5562011-11-24 21:01:32 -0500570 list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
Al Viroa73324d2011-11-24 22:25:07 -0500571 if (p->mnt_mountpoint == dentry)
Nick Piggin5f57cbc2011-01-07 17:49:54 +1100572 return;
573 }
574 }
575 spin_lock(&dentry->d_lock);
576 dentry->d_flags &= ~DCACHE_MOUNTED;
577 spin_unlock(&dentry->d_lock);
578}
579
580/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000581 * vfsmount lock must be held for write
582 */
Al Viro419148d2011-11-24 19:41:16 -0500583static void detach_mnt(struct mount *mnt, struct path *old_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Al Viroa73324d2011-11-24 22:25:07 -0500585 old_path->dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -0500586 old_path->mnt = &mnt->mnt_parent->mnt;
587 mnt->mnt_parent = mnt;
Al Viroa73324d2011-11-24 22:25:07 -0500588 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro6b41d532011-11-24 23:24:33 -0500589 list_del_init(&mnt->mnt_child);
Al Viro1b8e5562011-11-24 21:01:32 -0500590 list_del_init(&mnt->mnt_hash);
Al Viroaa0a4cf2011-11-24 19:31:36 -0500591 dentry_reset_mounted(old_path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Nick Piggin99b7db72010-08-18 04:37:39 +1000594/*
595 * vfsmount lock must be held for write
596 */
Al Viro14cf1fa2011-11-25 00:01:17 -0500597void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry,
Al Viro44d964d62011-11-24 21:28:22 -0500598 struct mount *child_mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500599{
Al Viro3a2393d2011-11-25 03:19:09 -0500600 mnt_add_count(mnt, 1); /* essentially, that's mntget */
Al Viroa73324d2011-11-24 22:25:07 -0500601 child_mnt->mnt_mountpoint = dget(dentry);
Al Viro3a2393d2011-11-25 03:19:09 -0500602 child_mnt->mnt_parent = mnt;
Nick Piggin5f57cbc2011-01-07 17:49:54 +1100603 spin_lock(&dentry->d_lock);
604 dentry->d_flags |= DCACHE_MOUNTED;
605 spin_unlock(&dentry->d_lock);
Ram Paib90fa9a2005-11-07 17:19:50 -0500606}
607
Nick Piggin99b7db72010-08-18 04:37:39 +1000608/*
609 * vfsmount lock must be held for write
610 */
Al Viro419148d2011-11-24 19:41:16 -0500611static void attach_mnt(struct mount *mnt, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Al Viro14cf1fa2011-11-25 00:01:17 -0500613 mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt);
Al Viro1b8e5562011-11-24 21:01:32 -0500614 list_add_tail(&mnt->mnt_hash, mount_hashtable +
Al Viro1a390682008-03-21 20:48:19 -0400615 hash(path->mnt, path->dentry));
Al Viro6b41d532011-11-24 23:24:33 -0500616 list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts);
Ram Paib90fa9a2005-11-07 17:19:50 -0500617}
618
Al Viro83adc752011-11-24 22:37:54 -0500619static inline void __mnt_make_longterm(struct mount *mnt)
Al Viro7e3d0eb02011-01-16 16:32:11 -0500620{
621#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500622 atomic_inc(&mnt->mnt_longterm);
Al Viro7e3d0eb02011-01-16 16:32:11 -0500623#endif
624}
625
626/* needs vfsmount lock for write */
Al Viro83adc752011-11-24 22:37:54 -0500627static inline void __mnt_make_shortterm(struct mount *mnt)
Al Viro7e3d0eb02011-01-16 16:32:11 -0500628{
629#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500630 atomic_dec(&mnt->mnt_longterm);
Al Viro7e3d0eb02011-01-16 16:32:11 -0500631#endif
632}
633
Ram Paib90fa9a2005-11-07 17:19:50 -0500634/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000635 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500636 */
Al Viro4b2619a2011-11-24 19:47:15 -0500637static void commit_tree(struct mount *mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500638{
Al Viro0714a532011-11-24 22:19:58 -0500639 struct mount *parent = mnt->mnt_parent;
Al Viro83adc752011-11-24 22:37:54 -0500640 struct mount *m;
Ram Paib90fa9a2005-11-07 17:19:50 -0500641 LIST_HEAD(head);
Al Viro143c8c92011-11-25 00:46:35 -0500642 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500643
Al Viro0714a532011-11-24 22:19:58 -0500644 BUG_ON(parent == mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500645
Al Viro1a4eeaf2011-11-25 02:19:55 -0500646 list_add_tail(&head, &mnt->mnt_list);
647 list_for_each_entry(m, &head, mnt_list) {
Al Viro143c8c92011-11-25 00:46:35 -0500648 m->mnt_ns = n;
Al Viro7e3d0eb02011-01-16 16:32:11 -0500649 __mnt_make_longterm(m);
Al Virof03c6592011-01-14 22:30:21 -0500650 }
651
Ram Paib90fa9a2005-11-07 17:19:50 -0500652 list_splice(&head, n->list.prev);
653
Al Viro1b8e5562011-11-24 21:01:32 -0500654 list_add_tail(&mnt->mnt_hash, mount_hashtable +
Al Viroa73324d2011-11-24 22:25:07 -0500655 hash(&parent->mnt, mnt->mnt_mountpoint));
Al Viro6b41d532011-11-24 23:24:33 -0500656 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800657 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Al Viro909b0a82011-11-25 03:06:56 -0500660static struct mount *next_mnt(struct mount *p, struct mount *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Al Viro6b41d532011-11-24 23:24:33 -0500662 struct list_head *next = p->mnt_mounts.next;
663 if (next == &p->mnt_mounts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 while (1) {
Al Viro909b0a82011-11-25 03:06:56 -0500665 if (p == root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return NULL;
Al Viro6b41d532011-11-24 23:24:33 -0500667 next = p->mnt_child.next;
668 if (next != &p->mnt_parent->mnt_mounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
Al Viro0714a532011-11-24 22:19:58 -0500670 p = p->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 }
Al Viro6b41d532011-11-24 23:24:33 -0500673 return list_entry(next, struct mount, mnt_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Al Viro315fc832011-11-24 18:57:30 -0500676static struct mount *skip_mnt_tree(struct mount *p)
Ram Pai9676f0c2005-11-07 17:21:20 -0500677{
Al Viro6b41d532011-11-24 23:24:33 -0500678 struct list_head *prev = p->mnt_mounts.prev;
679 while (prev != &p->mnt_mounts) {
680 p = list_entry(prev, struct mount, mnt_child);
681 prev = p->mnt_mounts.prev;
Ram Pai9676f0c2005-11-07 17:21:20 -0500682 }
683 return p;
684}
685
Al Viro9d412a42011-03-17 22:08:28 -0400686struct vfsmount *
687vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
688{
Al Virob105e272011-11-24 20:38:33 -0500689 struct mount *mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400690 struct dentry *root;
691
692 if (!type)
693 return ERR_PTR(-ENODEV);
694
695 mnt = alloc_vfsmnt(name);
696 if (!mnt)
697 return ERR_PTR(-ENOMEM);
698
699 if (flags & MS_KERNMOUNT)
Al Virob105e272011-11-24 20:38:33 -0500700 mnt->mnt.mnt_flags = MNT_INTERNAL;
Al Viro9d412a42011-03-17 22:08:28 -0400701
702 root = mount_fs(type, flags, name, data);
703 if (IS_ERR(root)) {
704 free_vfsmnt(mnt);
705 return ERR_CAST(root);
706 }
707
Al Virob105e272011-11-24 20:38:33 -0500708 mnt->mnt.mnt_root = root;
709 mnt->mnt.mnt_sb = root->d_sb;
Al Viroa73324d2011-11-24 22:25:07 -0500710 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -0500711 mnt->mnt_parent = mnt;
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100712 br_write_lock(vfsmount_lock);
713 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
714 br_write_unlock(vfsmount_lock);
Al Virob105e272011-11-24 20:38:33 -0500715 return &mnt->mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400716}
717EXPORT_SYMBOL_GPL(vfs_kern_mount);
718
Al Viro87129cc2011-11-24 21:24:27 -0500719static struct mount *clone_mnt(struct mount *old, struct dentry *root,
Ram Pai36341f62005-11-07 17:17:22 -0500720 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Al Viro87129cc2011-11-24 21:24:27 -0500722 struct super_block *sb = old->mnt.mnt_sb;
Al Viro52ba1622011-11-25 02:25:17 -0500723 struct mount *mnt = alloc_vfsmnt(old->mnt_devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (mnt) {
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100726 if (flag & (CL_SLAVE | CL_PRIVATE))
Al Viro15169fe2011-11-25 00:50:41 -0500727 mnt->mnt_group_id = 0; /* not a peer of original */
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100728 else
Al Viro15169fe2011-11-25 00:50:41 -0500729 mnt->mnt_group_id = old->mnt_group_id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100730
Al Viro15169fe2011-11-25 00:50:41 -0500731 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
Al Virob105e272011-11-24 20:38:33 -0500732 int err = mnt_alloc_group_id(mnt);
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100733 if (err)
734 goto out_free;
735 }
736
Al Viro87129cc2011-11-24 21:24:27 -0500737 mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 atomic_inc(&sb->s_active);
Al Virob105e272011-11-24 20:38:33 -0500739 mnt->mnt.mnt_sb = sb;
740 mnt->mnt.mnt_root = dget(root);
Al Viroa73324d2011-11-24 22:25:07 -0500741 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -0500742 mnt->mnt_parent = mnt;
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100743 br_write_lock(vfsmount_lock);
744 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
745 br_write_unlock(vfsmount_lock);
Ram Paib90fa9a2005-11-07 17:19:50 -0500746
Ram Pai5afe0022005-11-07 17:21:01 -0500747 if (flag & CL_SLAVE) {
Al Viro6776db3d2011-11-25 00:22:05 -0500748 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
Al Viro32301922011-11-25 00:10:28 -0500749 mnt->mnt_master = old;
Al Virofc7be132011-11-25 01:05:37 -0500750 CLEAR_MNT_SHARED(mnt);
Al Viro8aec0802007-06-07 12:20:32 -0400751 } else if (!(flag & CL_PRIVATE)) {
Al Virofc7be132011-11-25 01:05:37 -0500752 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
Al Viro6776db3d2011-11-25 00:22:05 -0500753 list_add(&mnt->mnt_share, &old->mnt_share);
Al Virod10e8de2011-11-25 00:07:16 -0500754 if (IS_MNT_SLAVE(old))
Al Viro6776db3d2011-11-25 00:22:05 -0500755 list_add(&mnt->mnt_slave, &old->mnt_slave);
Al Virod10e8de2011-11-25 00:07:16 -0500756 mnt->mnt_master = old->mnt_master;
Ram Pai5afe0022005-11-07 17:21:01 -0500757 }
Ram Paib90fa9a2005-11-07 17:19:50 -0500758 if (flag & CL_MAKE_SHARED)
Al Viro0f0afb12011-11-24 20:43:10 -0500759 set_mnt_shared(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* stick the duplicate mount on the same expiry list
762 * as the original if that was on one */
Ram Pai36341f62005-11-07 17:17:22 -0500763 if (flag & CL_EXPIRE) {
Al Viro6776db3d2011-11-25 00:22:05 -0500764 if (!list_empty(&old->mnt_expire))
765 list_add(&mnt->mnt_expire, &old->mnt_expire);
Ram Pai36341f62005-11-07 17:17:22 -0500766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Al Virocb338d02011-11-24 20:55:08 -0500768 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100769
770 out_free:
771 free_vfsmnt(mnt);
772 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Al Viro83adc752011-11-24 22:37:54 -0500775static inline void mntfree(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Al Viro83adc752011-11-24 22:37:54 -0500777 struct vfsmount *m = &mnt->mnt;
778 struct super_block *sb = m->mnt_sb;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100779
Dave Hansen3d733632008-02-15 14:37:59 -0800780 /*
Dave Hansen3d733632008-02-15 14:37:59 -0800781 * This probably indicates that somebody messed
782 * up a mnt_want/drop_write() pair. If this
783 * happens, the filesystem was probably unable
784 * to make r/w->r/o transitions.
785 */
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000786 /*
Nick Pigginb3e19d92011-01-07 17:50:11 +1100787 * The locking used to deal with mnt_count decrement provides barriers,
788 * so mnt_get_writers() below is safe.
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000789 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100790 WARN_ON(mnt_get_writers(mnt));
Al Viro83adc752011-11-24 22:37:54 -0500791 fsnotify_vfsmount_delete(m);
792 dput(m->mnt_root);
793 free_vfsmnt(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 deactivate_super(sb);
795}
796
Al Viro900148d2011-11-25 00:33:11 -0500797static void mntput_no_expire(struct mount *mnt)
Al Viro7b7b1ac2005-11-07 17:13:39 -0500798{
Nick Pigginb3e19d92011-01-07 17:50:11 +1100799put_again:
Al Virof03c6592011-01-14 22:30:21 -0500800#ifdef CONFIG_SMP
801 br_read_lock(vfsmount_lock);
Al Viro68e8a9f2011-11-24 22:53:09 -0500802 if (likely(atomic_read(&mnt->mnt_longterm))) {
Al Viroaa9c0e02011-11-23 19:04:52 -0500803 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100804 br_read_unlock(vfsmount_lock);
Al Virof03c6592011-01-14 22:30:21 -0500805 return;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100806 }
Al Virof03c6592011-01-14 22:30:21 -0500807 br_read_unlock(vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100808
809 br_write_lock(vfsmount_lock);
Al Viroaa9c0e02011-11-23 19:04:52 -0500810 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100811 if (mnt_get_count(mnt)) {
812 br_write_unlock(vfsmount_lock);
813 return;
814 }
Nick Pigginb3e19d92011-01-07 17:50:11 +1100815#else
Al Viroaa9c0e02011-11-23 19:04:52 -0500816 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100817 if (likely(mnt_get_count(mnt)))
Nick Piggin99b7db72010-08-18 04:37:39 +1000818 return;
819 br_write_lock(vfsmount_lock);
Al Virof03c6592011-01-14 22:30:21 -0500820#endif
Al Viro863d6842011-11-25 00:57:42 -0500821 if (unlikely(mnt->mnt_pinned)) {
822 mnt_add_count(mnt, mnt->mnt_pinned + 1);
823 mnt->mnt_pinned = 0;
Nick Piggin99b7db72010-08-18 04:37:39 +1000824 br_write_unlock(vfsmount_lock);
Al Viro900148d2011-11-25 00:33:11 -0500825 acct_auto_close_mnt(&mnt->mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100826 goto put_again;
Al Viro7b7b1ac2005-11-07 17:13:39 -0500827 }
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +0100828 list_del(&mnt->mnt_instance);
Nick Piggin99b7db72010-08-18 04:37:39 +1000829 br_write_unlock(vfsmount_lock);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100830 mntfree(mnt);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500831}
Nick Pigginb3e19d92011-01-07 17:50:11 +1100832
833void mntput(struct vfsmount *mnt)
834{
835 if (mnt) {
Al Viro863d6842011-11-25 00:57:42 -0500836 struct mount *m = real_mount(mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100837 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
Al Viro863d6842011-11-25 00:57:42 -0500838 if (unlikely(m->mnt_expiry_mark))
839 m->mnt_expiry_mark = 0;
840 mntput_no_expire(m);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100841 }
842}
843EXPORT_SYMBOL(mntput);
844
845struct vfsmount *mntget(struct vfsmount *mnt)
846{
847 if (mnt)
Al Viro83adc752011-11-24 22:37:54 -0500848 mnt_add_count(real_mount(mnt), 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100849 return mnt;
850}
851EXPORT_SYMBOL(mntget);
852
Al Viro7b7b1ac2005-11-07 17:13:39 -0500853void mnt_pin(struct vfsmount *mnt)
854{
Nick Piggin99b7db72010-08-18 04:37:39 +1000855 br_write_lock(vfsmount_lock);
Al Viro863d6842011-11-25 00:57:42 -0500856 real_mount(mnt)->mnt_pinned++;
Nick Piggin99b7db72010-08-18 04:37:39 +1000857 br_write_unlock(vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500858}
Al Viro7b7b1ac2005-11-07 17:13:39 -0500859EXPORT_SYMBOL(mnt_pin);
860
Al Viro863d6842011-11-25 00:57:42 -0500861void mnt_unpin(struct vfsmount *m)
Al Viro7b7b1ac2005-11-07 17:13:39 -0500862{
Al Viro863d6842011-11-25 00:57:42 -0500863 struct mount *mnt = real_mount(m);
Nick Piggin99b7db72010-08-18 04:37:39 +1000864 br_write_lock(vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500865 if (mnt->mnt_pinned) {
Al Viro863d6842011-11-25 00:57:42 -0500866 mnt_add_count(mnt, 1);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500867 mnt->mnt_pinned--;
868 }
Nick Piggin99b7db72010-08-18 04:37:39 +1000869 br_write_unlock(vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500870}
Al Viro7b7b1ac2005-11-07 17:13:39 -0500871EXPORT_SYMBOL(mnt_unpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800873static inline void mangle(struct seq_file *m, const char *s)
874{
875 seq_escape(m, s, " \t\n\\");
876}
877
878/*
879 * Simple .show_options callback for filesystems which don't want to
880 * implement more complex mount option showing.
881 *
882 * See also save_mount_options().
883 */
Al Viro34c80b12011-12-08 21:32:45 -0500884int generic_show_options(struct seq_file *m, struct dentry *root)
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800885{
Al Viro2a32ceb2009-05-08 16:05:57 -0400886 const char *options;
887
888 rcu_read_lock();
Al Viro34c80b12011-12-08 21:32:45 -0500889 options = rcu_dereference(root->d_sb->s_options);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800890
891 if (options != NULL && options[0]) {
892 seq_putc(m, ',');
893 mangle(m, options);
894 }
Al Viro2a32ceb2009-05-08 16:05:57 -0400895 rcu_read_unlock();
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800896
897 return 0;
898}
899EXPORT_SYMBOL(generic_show_options);
900
901/*
902 * If filesystem uses generic_show_options(), this function should be
903 * called from the fill_super() callback.
904 *
905 * The .remount_fs callback usually needs to be handled in a special
906 * way, to make sure, that previous options are not overwritten if the
907 * remount fails.
908 *
909 * Also note, that if the filesystem's .remount_fs function doesn't
910 * reset all options to their default value, but changes only newly
911 * given options, then the displayed options will not reflect reality
912 * any more.
913 */
914void save_mount_options(struct super_block *sb, char *options)
915{
Al Viro2a32ceb2009-05-08 16:05:57 -0400916 BUG_ON(sb->s_options);
917 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800918}
919EXPORT_SYMBOL(save_mount_options);
920
Al Viro2a32ceb2009-05-08 16:05:57 -0400921void replace_mount_options(struct super_block *sb, char *options)
922{
923 char *old = sb->s_options;
924 rcu_assign_pointer(sb->s_options, options);
925 if (old) {
926 synchronize_rcu();
927 kfree(old);
928 }
929}
930EXPORT_SYMBOL(replace_mount_options);
931
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100932#ifdef CONFIG_PROC_FS
Al Viro0226f492011-12-06 12:21:54 -0500933/* iterator; we want it to have access to namespace_sem, thus here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static void *m_start(struct seq_file *m, loff_t *pos)
935{
Al Viro0226f492011-12-06 12:21:54 -0500936 struct proc_mounts *p = container_of(m, struct proc_mounts, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Ram Pai390c6842005-11-07 17:17:51 -0500938 down_read(&namespace_sem);
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100939 return seq_list_start(&p->ns->list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942static void *m_next(struct seq_file *m, void *v, loff_t *pos)
943{
Al Viro0226f492011-12-06 12:21:54 -0500944 struct proc_mounts *p = container_of(m, struct proc_mounts, m);
Pavel Emelianovb0765fb2007-07-15 23:39:55 -0700945
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100946 return seq_list_next(v, &p->ns->list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949static void m_stop(struct seq_file *m, void *v)
950{
Ram Pai390c6842005-11-07 17:17:51 -0500951 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Al Viro0226f492011-12-06 12:21:54 -0500954static int m_show(struct seq_file *m, void *v)
Al Viro9f5596a2010-02-05 00:40:25 -0500955{
Al Viro0226f492011-12-06 12:21:54 -0500956 struct proc_mounts *p = container_of(m, struct proc_mounts, m);
Al Viro1a4eeaf2011-11-25 02:19:55 -0500957 struct mount *r = list_entry(v, struct mount, mnt_list);
Al Viro0226f492011-12-06 12:21:54 -0500958 return p->show(m, &r->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100961const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 .start = m_start,
963 .next = m_next,
964 .stop = m_stop,
Al Viro0226f492011-12-06 12:21:54 -0500965 .show = m_show,
Chuck Leverb4629fe2006-03-20 13:44:12 -0500966};
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100967#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -0500968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/**
970 * may_umount_tree - check if a mount tree is busy
971 * @mnt: root of mount tree
972 *
973 * This is called to check if a tree of mounts has any
974 * open files, pwds, chroots or sub mounts that are
975 * busy.
976 */
Al Viro909b0a82011-11-25 03:06:56 -0500977int may_umount_tree(struct vfsmount *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Al Viro909b0a82011-11-25 03:06:56 -0500979 struct mount *mnt = real_mount(m);
Ram Pai36341f62005-11-07 17:17:22 -0500980 int actual_refs = 0;
981 int minimum_refs = 0;
Al Viro315fc832011-11-24 18:57:30 -0500982 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -0500983 BUG_ON(!m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Nick Pigginb3e19d92011-01-07 17:50:11 +1100985 /* write lock needed for mnt_get_count */
986 br_write_lock(vfsmount_lock);
Al Viro909b0a82011-11-25 03:06:56 -0500987 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Viro83adc752011-11-24 22:37:54 -0500988 actual_refs += mnt_get_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Nick Pigginb3e19d92011-01-07 17:50:11 +1100991 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -0800994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Ian Kente3474a82006-03-27 01:14:51 -0800996 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999EXPORT_SYMBOL(may_umount_tree);
1000
1001/**
1002 * may_umount - check if a mount point is busy
1003 * @mnt: root of mount
1004 *
1005 * This is called to check if a mount point has any
1006 * open files, pwds, chroots or sub mounts. If the
1007 * mount has sub mounts this will return busy
1008 * regardless of whether the sub mounts are busy.
1009 *
1010 * Doesn't take quota and stuff into account. IOW, in some cases it will
1011 * give false negatives. The main reason why it's here is that we need
1012 * a non-destructive way to look for easily umountable filesystems.
1013 */
1014int may_umount(struct vfsmount *mnt)
1015{
Ian Kente3474a82006-03-27 01:14:51 -08001016 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001017 down_read(&namespace_sem);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001018 br_write_lock(vfsmount_lock);
Al Viro1ab59732011-11-24 21:35:16 -05001019 if (propagate_mount_busy(real_mount(mnt), 2))
Ian Kente3474a82006-03-27 01:14:51 -08001020 ret = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001021 br_write_unlock(vfsmount_lock);
Al Viro8ad08d82010-01-16 12:56:08 -05001022 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001023 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026EXPORT_SYMBOL(may_umount);
1027
Ram Paib90fa9a2005-11-07 17:19:50 -05001028void release_mounts(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Al Virod5e50f72011-11-24 20:58:57 -05001030 struct mount *mnt;
Miklos Szeredibf066c72006-01-08 01:03:19 -08001031 while (!list_empty(head)) {
Al Viro1b8e5562011-11-24 21:01:32 -05001032 mnt = list_first_entry(head, struct mount, mnt_hash);
1033 list_del_init(&mnt->mnt_hash);
Al Viro676da582011-11-24 21:47:05 -05001034 if (mnt_has_parent(mnt)) {
Ram Pai70fbcdf2005-11-07 17:17:04 -05001035 struct dentry *dentry;
Al Viro863d6842011-11-25 00:57:42 -05001036 struct mount *m;
Nick Piggin99b7db72010-08-18 04:37:39 +10001037
1038 br_write_lock(vfsmount_lock);
Al Viroa73324d2011-11-24 22:25:07 -05001039 dentry = mnt->mnt_mountpoint;
Al Viro863d6842011-11-25 00:57:42 -05001040 m = mnt->mnt_parent;
Al Viroa73324d2011-11-24 22:25:07 -05001041 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -05001042 mnt->mnt_parent = mnt;
Al Viro7c4b93d2008-03-21 23:59:49 -04001043 m->mnt_ghosts--;
Nick Piggin99b7db72010-08-18 04:37:39 +10001044 br_write_unlock(vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001045 dput(dentry);
Al Viro863d6842011-11-25 00:57:42 -05001046 mntput(&m->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Al Virod5e50f72011-11-24 20:58:57 -05001048 mntput(&mnt->mnt);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001049 }
1050}
1051
Nick Piggin99b7db72010-08-18 04:37:39 +10001052/*
1053 * vfsmount lock must be held for write
1054 * namespace_sem must be held for write
1055 */
Al Viro761d5c32011-11-24 21:07:43 -05001056void umount_tree(struct mount *mnt, int propagate, struct list_head *kill)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001057{
Al Viro7b8a53f2011-01-15 20:08:44 -05001058 LIST_HEAD(tmp_list);
Al Viro315fc832011-11-24 18:57:30 -05001059 struct mount *p;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001060
Al Viro909b0a82011-11-25 03:06:56 -05001061 for (p = mnt; p; p = next_mnt(p, mnt))
Al Viro1b8e5562011-11-24 21:01:32 -05001062 list_move(&p->mnt_hash, &tmp_list);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001063
Ram Paia05964f2005-11-07 17:20:17 -05001064 if (propagate)
Al Viro7b8a53f2011-01-15 20:08:44 -05001065 propagate_umount(&tmp_list);
Ram Paia05964f2005-11-07 17:20:17 -05001066
Al Viro1b8e5562011-11-24 21:01:32 -05001067 list_for_each_entry(p, &tmp_list, mnt_hash) {
Al Viro6776db3d2011-11-25 00:22:05 -05001068 list_del_init(&p->mnt_expire);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001069 list_del_init(&p->mnt_list);
Al Viro143c8c92011-11-25 00:46:35 -05001070 __touch_mnt_namespace(p->mnt_ns);
1071 p->mnt_ns = NULL;
Al Viro83adc752011-11-24 22:37:54 -05001072 __mnt_make_shortterm(p);
Al Viro6b41d532011-11-24 23:24:33 -05001073 list_del_init(&p->mnt_child);
Al Viro676da582011-11-24 21:47:05 -05001074 if (mnt_has_parent(p)) {
Al Viro863d6842011-11-25 00:57:42 -05001075 p->mnt_parent->mnt_ghosts++;
Al Viroa73324d2011-11-24 22:25:07 -05001076 dentry_reset_mounted(p->mnt_mountpoint);
Al Viro7c4b93d2008-03-21 23:59:49 -04001077 }
Al Viro0f0afb12011-11-24 20:43:10 -05001078 change_mnt_propagation(p, MS_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Al Viro7b8a53f2011-01-15 20:08:44 -05001080 list_splice(&tmp_list, kill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Al Viro692afc32011-11-24 21:15:14 -05001083static void shrink_submounts(struct mount *mnt, struct list_head *umounts);
Al Viroc35038b2008-03-22 00:46:23 -04001084
Al Viro1ab59732011-11-24 21:35:16 -05001085static int do_umount(struct mount *mnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Al Viro1ab59732011-11-24 21:35:16 -05001087 struct super_block *sb = mnt->mnt.mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int retval;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001089 LIST_HEAD(umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Al Viro1ab59732011-11-24 21:35:16 -05001091 retval = security_sb_umount(&mnt->mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (retval)
1093 return retval;
1094
1095 /*
1096 * Allow userspace to request a mountpoint be expired rather than
1097 * unmounting unconditionally. Unmount only happens if:
1098 * (1) the mark is already set (the mark is cleared by mntput())
1099 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1100 */
1101 if (flags & MNT_EXPIRE) {
Al Viro1ab59732011-11-24 21:35:16 -05001102 if (&mnt->mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 flags & (MNT_FORCE | MNT_DETACH))
1104 return -EINVAL;
1105
Nick Pigginb3e19d92011-01-07 17:50:11 +11001106 /*
1107 * probably don't strictly need the lock here if we examined
1108 * all race cases, but it's a slowpath.
1109 */
1110 br_write_lock(vfsmount_lock);
Al Viro83adc752011-11-24 22:37:54 -05001111 if (mnt_get_count(mnt) != 2) {
J. R. Okajimabf9faa22011-02-23 16:59:49 +09001112 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return -EBUSY;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001114 }
1115 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Al Viro863d6842011-11-25 00:57:42 -05001117 if (!xchg(&mnt->mnt_expiry_mark, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return -EAGAIN;
1119 }
1120
1121 /*
1122 * If we may have to abort operations to get out of this
1123 * mount, and they will themselves hold resources we must
1124 * allow the fs to do things. In the Unix tradition of
1125 * 'Gee thats tricky lets do it in userspace' the umount_begin
1126 * might fail to complete on the first run through as other tasks
1127 * must return, and the like. Thats for the mount program to worry
1128 * about for the moment.
1129 */
1130
Al Viro42faad92008-04-24 07:21:56 -04001131 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001132 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /*
1136 * No sense to grab the lock for this test, but test itself looks
1137 * somewhat bogus. Suggestions for better replacement?
1138 * Ho-hum... In principle, we might treat that as umount + switch
1139 * to rootfs. GC would eventually take care of the old vfsmount.
1140 * Actually it makes sense, especially if rootfs would contain a
1141 * /reboot - static binary that would close all descriptors and
1142 * call reboot(9). Then init(8) could umount root and exec /reboot.
1143 */
Al Viro1ab59732011-11-24 21:35:16 -05001144 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /*
1146 * Special case for "unmounting" root ...
1147 * we just try to remount it readonly.
1148 */
1149 down_write(&sb->s_umount);
Al Viro4aa98cf2009-05-08 13:36:58 -04001150 if (!(sb->s_flags & MS_RDONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 up_write(&sb->s_umount);
1153 return retval;
1154 }
1155
Ram Pai390c6842005-11-07 17:17:51 -05001156 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10001157 br_write_lock(vfsmount_lock);
Al Viro5addc5d2005-11-07 17:15:49 -05001158 event++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Al Viroc35038b2008-03-22 00:46:23 -04001160 if (!(flags & MNT_DETACH))
Al Viro1ab59732011-11-24 21:35:16 -05001161 shrink_submounts(mnt, &umount_list);
Al Viroc35038b2008-03-22 00:46:23 -04001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 retval = -EBUSY;
Ram Paia05964f2005-11-07 17:20:17 -05001164 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05001165 if (!list_empty(&mnt->mnt_list))
Al Viro1ab59732011-11-24 21:35:16 -05001166 umount_tree(mnt, 1, &umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 retval = 0;
1168 }
Nick Piggin99b7db72010-08-18 04:37:39 +10001169 br_write_unlock(vfsmount_lock);
Ram Pai390c6842005-11-07 17:17:51 -05001170 up_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001171 release_mounts(&umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return retval;
1173}
1174
1175/*
1176 * Now umount can handle mount points as well as block devices.
1177 * This is important for filesystems which use unnamed block devices.
1178 *
1179 * We now support a flag for forced unmount like the other 'big iron'
1180 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1181 */
1182
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001183SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Al Viro2d8f3032008-07-22 09:59:21 -04001185 struct path path;
Al Viro900148d2011-11-25 00:33:11 -05001186 struct mount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int retval;
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001188 int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001190 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1191 return -EINVAL;
1192
1193 if (!(flags & UMOUNT_NOFOLLOW))
1194 lookup_flags |= LOOKUP_FOLLOW;
1195
1196 retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (retval)
1198 goto out;
Al Viro900148d2011-11-25 00:33:11 -05001199 mnt = real_mount(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 retval = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04001201 if (path.dentry != path.mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 goto dput_and_out;
Al Viro143c8c92011-11-25 00:46:35 -05001203 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto dput_and_out;
1205
1206 retval = -EPERM;
1207 if (!capable(CAP_SYS_ADMIN))
1208 goto dput_and_out;
1209
Al Viro900148d2011-11-25 00:33:11 -05001210 retval = do_umount(mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211dput_and_out:
Jan Blunck429731b2008-02-14 19:34:31 -08001212 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Al Viro2d8f3032008-07-22 09:59:21 -04001213 dput(path.dentry);
Al Viro900148d2011-11-25 00:33:11 -05001214 mntput_no_expire(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215out:
1216 return retval;
1217}
1218
1219#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1220
1221/*
Ram Paib58fed82005-11-07 17:16:09 -05001222 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001224SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Ram Paib58fed82005-11-07 17:16:09 -05001226 return sys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229#endif
1230
Al Viro2d92ab32008-08-02 00:51:11 -04001231static int mount_is_safe(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 if (capable(CAP_SYS_ADMIN))
1234 return 0;
1235 return -EPERM;
1236#ifdef notyet
Al Viro2d92ab32008-08-02 00:51:11 -04001237 if (S_ISLNK(path->dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EPERM;
Al Viro2d92ab32008-08-02 00:51:11 -04001239 if (path->dentry->d_inode->i_mode & S_ISVTX) {
David Howellsda9592e2008-11-14 10:39:05 +11001240 if (current_uid() != path->dentry->d_inode->i_uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -EPERM;
1242 }
Al Viro2d92ab32008-08-02 00:51:11 -04001243 if (inode_permission(path->dentry->d_inode, MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EPERM;
1245 return 0;
1246#endif
1247}
1248
Al Viro87129cc2011-11-24 21:24:27 -05001249struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001250 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Al Viroa73324d2011-11-24 22:25:07 -05001252 struct mount *res, *p, *q, *r;
Al Viro1a390682008-03-21 20:48:19 -04001253 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Al Virofc7be132011-11-25 01:05:37 -05001255 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
Ram Pai9676f0c2005-11-07 17:21:20 -05001256 return NULL;
1257
Ram Pai36341f62005-11-07 17:17:22 -05001258 res = q = clone_mnt(mnt, dentry, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (!q)
1260 goto Enomem;
Al Viroa73324d2011-11-24 22:25:07 -05001261 q->mnt_mountpoint = mnt->mnt_mountpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 p = mnt;
Al Viro6b41d532011-11-24 23:24:33 -05001264 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Al Viro315fc832011-11-24 18:57:30 -05001265 struct mount *s;
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001266 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 continue;
1268
Al Viro909b0a82011-11-25 03:06:56 -05001269 for (s = r; s; s = next_mnt(s, r)) {
Al Virofc7be132011-11-25 01:05:37 -05001270 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001271 s = skip_mnt_tree(s);
1272 continue;
1273 }
Al Viro0714a532011-11-24 22:19:58 -05001274 while (p != s->mnt_parent) {
1275 p = p->mnt_parent;
1276 q = q->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Al Viro87129cc2011-11-24 21:24:27 -05001278 p = s;
Al Virocb338d02011-11-24 20:55:08 -05001279 path.mnt = &q->mnt;
Al Viroa73324d2011-11-24 22:25:07 -05001280 path.dentry = p->mnt_mountpoint;
Al Viro87129cc2011-11-24 21:24:27 -05001281 q = clone_mnt(p, p->mnt.mnt_root, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!q)
1283 goto Enomem;
Nick Piggin99b7db72010-08-18 04:37:39 +10001284 br_write_lock(vfsmount_lock);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001285 list_add_tail(&q->mnt_list, &res->mnt_list);
Al Virocb338d02011-11-24 20:55:08 -05001286 attach_mnt(q, &path);
Nick Piggin99b7db72010-08-18 04:37:39 +10001287 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 }
1290 return res;
Ram Paib58fed82005-11-07 17:16:09 -05001291Enomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (res) {
Ram Pai70fbcdf2005-11-07 17:17:04 -05001293 LIST_HEAD(umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001294 br_write_lock(vfsmount_lock);
Al Viro761d5c32011-11-24 21:07:43 -05001295 umount_tree(res, 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001296 br_write_unlock(vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001297 release_mounts(&umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 return NULL;
1300}
1301
Al Viro589ff872009-04-18 03:28:19 -04001302struct vfsmount *collect_mounts(struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001303{
Al Virocb338d02011-11-24 20:55:08 -05001304 struct mount *tree;
Al Viro1a60a282008-03-22 16:19:49 -04001305 down_write(&namespace_sem);
Al Viro87129cc2011-11-24 21:24:27 -05001306 tree = copy_tree(real_mount(path->mnt), path->dentry,
1307 CL_COPY_ALL | CL_PRIVATE);
Al Viro1a60a282008-03-22 16:19:49 -04001308 up_write(&namespace_sem);
Al Virocb338d02011-11-24 20:55:08 -05001309 return tree ? &tree->mnt : NULL;
Al Viro8aec0802007-06-07 12:20:32 -04001310}
1311
1312void drop_collected_mounts(struct vfsmount *mnt)
1313{
1314 LIST_HEAD(umount_list);
Al Viro1a60a282008-03-22 16:19:49 -04001315 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10001316 br_write_lock(vfsmount_lock);
Al Viro761d5c32011-11-24 21:07:43 -05001317 umount_tree(real_mount(mnt), 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001318 br_write_unlock(vfsmount_lock);
Al Viro1a60a282008-03-22 16:19:49 -04001319 up_write(&namespace_sem);
Al Viro8aec0802007-06-07 12:20:32 -04001320 release_mounts(&umount_list);
1321}
1322
Al Viro1f707132010-01-30 22:51:25 -05001323int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1324 struct vfsmount *root)
1325{
Al Viro1a4eeaf2011-11-25 02:19:55 -05001326 struct mount *mnt;
Al Viro1f707132010-01-30 22:51:25 -05001327 int res = f(root, arg);
1328 if (res)
1329 return res;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001330 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1331 res = f(&mnt->mnt, arg);
Al Viro1f707132010-01-30 22:51:25 -05001332 if (res)
1333 return res;
1334 }
1335 return 0;
1336}
1337
Al Viro4b8b21f2011-11-24 19:54:23 -05001338static void cleanup_group_ids(struct mount *mnt, struct mount *end)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001339{
Al Viro315fc832011-11-24 18:57:30 -05001340 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001341
Al Viro909b0a82011-11-25 03:06:56 -05001342 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001343 if (p->mnt_group_id && !IS_MNT_SHARED(p))
Al Viro4b8b21f2011-11-24 19:54:23 -05001344 mnt_release_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001345 }
1346}
1347
Al Viro4b8b21f2011-11-24 19:54:23 -05001348static int invent_group_ids(struct mount *mnt, bool recurse)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001349{
Al Viro315fc832011-11-24 18:57:30 -05001350 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001351
Al Viro909b0a82011-11-25 03:06:56 -05001352 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
Al Virofc7be132011-11-25 01:05:37 -05001353 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001354 int err = mnt_alloc_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001355 if (err) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001356 cleanup_group_ids(mnt, p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001357 return err;
1358 }
1359 }
1360 }
1361
1362 return 0;
1363}
1364
Ram Paib90fa9a2005-11-07 17:19:50 -05001365/*
1366 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05001367 * @nd : place the mount tree @source_mnt is attached
1368 * @parent_nd : if non-null, detach the source_mnt from its parent and
1369 * store the parent mount and mountpoint dentry.
1370 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05001371 *
1372 * NOTE: in the table below explains the semantics when a source mount
1373 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05001374 * ---------------------------------------------------------------------------
1375 * | BIND MOUNT OPERATION |
1376 * |**************************************************************************
1377 * | source-->| shared | private | slave | unbindable |
1378 * | dest | | | | |
1379 * | | | | | | |
1380 * | v | | | | |
1381 * |**************************************************************************
1382 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1383 * | | | | | |
1384 * |non-shared| shared (+) | private | slave (*) | invalid |
1385 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05001386 * A bind operation clones the source mount and mounts the clone on the
1387 * destination mount.
1388 *
1389 * (++) the cloned mount is propagated to all the mounts in the propagation
1390 * tree of the destination mount and the cloned mount is added to
1391 * the peer group of the source mount.
1392 * (+) the cloned mount is created under the destination mount and is marked
1393 * as shared. The cloned mount is added to the peer group of the source
1394 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05001395 * (+++) the mount is propagated to all the mounts in the propagation tree
1396 * of the destination mount and the cloned mount is made slave
1397 * of the same master as that of the source mount. The cloned mount
1398 * is marked as 'shared and slave'.
1399 * (*) the cloned mount is made a slave of the same master as that of the
1400 * source mount.
1401 *
Ram Pai9676f0c2005-11-07 17:21:20 -05001402 * ---------------------------------------------------------------------------
1403 * | MOVE MOUNT OPERATION |
1404 * |**************************************************************************
1405 * | source-->| shared | private | slave | unbindable |
1406 * | dest | | | | |
1407 * | | | | | | |
1408 * | v | | | | |
1409 * |**************************************************************************
1410 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1411 * | | | | | |
1412 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1413 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05001414 *
1415 * (+) the mount is moved to the destination. And is then propagated to
1416 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05001417 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05001418 * (+++) the mount is moved to the destination and is then propagated to
1419 * all the mounts belonging to the destination mount's propagation tree.
1420 * the mount is marked as 'shared and slave'.
1421 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05001422 *
1423 * if the source mount is a tree, the operations explained above is
1424 * applied to each mount in the tree.
1425 * Must be called without spinlocks held, since this function can sleep
1426 * in allocations.
1427 */
Al Viro0fb54e52011-11-24 19:59:16 -05001428static int attach_recursive_mnt(struct mount *source_mnt,
Al Viro1a390682008-03-21 20:48:19 -04001429 struct path *path, struct path *parent_path)
Ram Paib90fa9a2005-11-07 17:19:50 -05001430{
1431 LIST_HEAD(tree_list);
Al Viroa8d56d82011-11-24 23:59:29 -05001432 struct mount *dest_mnt = real_mount(path->mnt);
Al Viro1a390682008-03-21 20:48:19 -04001433 struct dentry *dest_dentry = path->dentry;
Al Viro315fc832011-11-24 18:57:30 -05001434 struct mount *child, *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001435 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001436
Al Virofc7be132011-11-25 01:05:37 -05001437 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro0fb54e52011-11-24 19:59:16 -05001438 err = invent_group_ids(source_mnt, true);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001439 if (err)
1440 goto out;
1441 }
Al Viroa8d56d82011-11-24 23:59:29 -05001442 err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001443 if (err)
1444 goto out_cleanup_ids;
Ram Paib90fa9a2005-11-07 17:19:50 -05001445
Nick Piggin99b7db72010-08-18 04:37:39 +10001446 br_write_lock(vfsmount_lock);
Al Virodf1a1ad2010-01-16 12:57:40 -05001447
Al Virofc7be132011-11-25 01:05:37 -05001448 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro909b0a82011-11-25 03:06:56 -05001449 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
Al Viro0f0afb12011-11-24 20:43:10 -05001450 set_mnt_shared(p);
Ram Paib90fa9a2005-11-07 17:19:50 -05001451 }
Al Viro1a390682008-03-21 20:48:19 -04001452 if (parent_path) {
Al Viro0fb54e52011-11-24 19:59:16 -05001453 detach_mnt(source_mnt, parent_path);
1454 attach_mnt(source_mnt, path);
Al Viro143c8c92011-11-25 00:46:35 -05001455 touch_mnt_namespace(source_mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05001456 } else {
Al Viro14cf1fa2011-11-25 00:01:17 -05001457 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
Al Viro0fb54e52011-11-24 19:59:16 -05001458 commit_tree(source_mnt);
Ram Pai21444402005-11-07 17:20:03 -05001459 }
Ram Paib90fa9a2005-11-07 17:19:50 -05001460
Al Viro1b8e5562011-11-24 21:01:32 -05001461 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1462 list_del_init(&child->mnt_hash);
Al Viro4b2619a2011-11-24 19:47:15 -05001463 commit_tree(child);
Ram Paib90fa9a2005-11-07 17:19:50 -05001464 }
Nick Piggin99b7db72010-08-18 04:37:39 +10001465 br_write_unlock(vfsmount_lock);
1466
Ram Paib90fa9a2005-11-07 17:19:50 -05001467 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001468
1469 out_cleanup_ids:
Al Virofc7be132011-11-25 01:05:37 -05001470 if (IS_MNT_SHARED(dest_mnt))
Al Viro0fb54e52011-11-24 19:59:16 -05001471 cleanup_group_ids(source_mnt, NULL);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001472 out:
1473 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001474}
1475
Al Virob12cea92011-03-18 08:55:38 -04001476static int lock_mount(struct path *path)
1477{
1478 struct vfsmount *mnt;
1479retry:
1480 mutex_lock(&path->dentry->d_inode->i_mutex);
1481 if (unlikely(cant_mount(path->dentry))) {
1482 mutex_unlock(&path->dentry->d_inode->i_mutex);
1483 return -ENOENT;
1484 }
1485 down_write(&namespace_sem);
1486 mnt = lookup_mnt(path);
1487 if (likely(!mnt))
1488 return 0;
1489 up_write(&namespace_sem);
1490 mutex_unlock(&path->dentry->d_inode->i_mutex);
1491 path_put(path);
1492 path->mnt = mnt;
1493 path->dentry = dget(mnt->mnt_root);
1494 goto retry;
1495}
1496
1497static void unlock_mount(struct path *path)
1498{
1499 up_write(&namespace_sem);
1500 mutex_unlock(&path->dentry->d_inode->i_mutex);
1501}
1502
Al Viro95bc5f22011-11-25 00:30:56 -05001503static int graft_tree(struct mount *mnt, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Al Viro95bc5f22011-11-25 00:30:56 -05001505 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EINVAL;
1507
Al Viro8c3ee422008-03-22 18:00:39 -04001508 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
Al Viro95bc5f22011-11-25 00:30:56 -05001509 S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return -ENOTDIR;
1511
Al Virob12cea92011-03-18 08:55:38 -04001512 if (d_unlinked(path->dentry))
1513 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Al Viro95bc5f22011-11-25 00:30:56 -05001515 return attach_recursive_mnt(mnt, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
1518/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001519 * Sanity check the flags to change_mnt_propagation.
1520 */
1521
1522static int flags_to_propagation_type(int flags)
1523{
Roman Borisov7c6e9842011-05-25 16:26:48 -07001524 int type = flags & ~(MS_REC | MS_SILENT);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001525
1526 /* Fail if any non-propagation flags are set */
1527 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1528 return 0;
1529 /* Only one propagation flag should be set */
1530 if (!is_power_of_2(type))
1531 return 0;
1532 return type;
1533}
1534
1535/*
Ram Pai07b20882005-11-07 17:19:07 -05001536 * recursively change the type of the mountpoint.
1537 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001538static int do_change_type(struct path *path, int flag)
Ram Pai07b20882005-11-07 17:19:07 -05001539{
Al Viro315fc832011-11-24 18:57:30 -05001540 struct mount *m;
Al Viro4b8b21f2011-11-24 19:54:23 -05001541 struct mount *mnt = real_mount(path->mnt);
Ram Pai07b20882005-11-07 17:19:07 -05001542 int recurse = flag & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001543 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001544 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05001545
Miklos Szerediee6f9582007-05-08 00:30:40 -07001546 if (!capable(CAP_SYS_ADMIN))
1547 return -EPERM;
1548
Al Viro2d92ab32008-08-02 00:51:11 -04001549 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05001550 return -EINVAL;
1551
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001552 type = flags_to_propagation_type(flag);
1553 if (!type)
1554 return -EINVAL;
1555
Ram Pai07b20882005-11-07 17:19:07 -05001556 down_write(&namespace_sem);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001557 if (type == MS_SHARED) {
1558 err = invent_group_ids(mnt, recurse);
1559 if (err)
1560 goto out_unlock;
1561 }
1562
Nick Piggin99b7db72010-08-18 04:37:39 +10001563 br_write_lock(vfsmount_lock);
Al Viro909b0a82011-11-25 03:06:56 -05001564 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
Al Viro0f0afb12011-11-24 20:43:10 -05001565 change_mnt_propagation(m, type);
Nick Piggin99b7db72010-08-18 04:37:39 +10001566 br_write_unlock(vfsmount_lock);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001567
1568 out_unlock:
Ram Pai07b20882005-11-07 17:19:07 -05001569 up_write(&namespace_sem);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001570 return err;
Ram Pai07b20882005-11-07 17:19:07 -05001571}
1572
1573/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 * do loopback mount.
1575 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001576static int do_loopback(struct path *path, char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08001577 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Al Virob12cea92011-03-18 08:55:38 -04001579 LIST_HEAD(umount_list);
Al Viro2d92ab32008-08-02 00:51:11 -04001580 struct path old_path;
Al Viro87129cc2011-11-24 21:24:27 -05001581 struct mount *mnt = NULL, *old;
Al Viro2d92ab32008-08-02 00:51:11 -04001582 int err = mount_is_safe(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (err)
1584 return err;
1585 if (!old_name || !*old_name)
1586 return -EINVAL;
Trond Myklebust815d4052011-09-26 20:36:09 -04001587 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (err)
1589 return err;
1590
Al Virob12cea92011-03-18 08:55:38 -04001591 err = lock_mount(path);
1592 if (err)
Jan Blunck4ac91372008-02-14 19:34:32 -08001593 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05001594
Al Viro87129cc2011-11-24 21:24:27 -05001595 old = real_mount(old_path.mnt);
1596
Al Virob12cea92011-03-18 08:55:38 -04001597 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05001598 if (IS_MNT_UNBINDABLE(old))
Al Virob12cea92011-03-18 08:55:38 -04001599 goto out2;
1600
Al Viro143c8c92011-11-25 00:46:35 -05001601 if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old))
Al Virob12cea92011-03-18 08:55:38 -04001602 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Al Viroccd48bc2005-11-07 17:15:04 -05001604 err = -ENOMEM;
1605 if (recurse)
Al Viro87129cc2011-11-24 21:24:27 -05001606 mnt = copy_tree(old, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05001607 else
Al Viro87129cc2011-11-24 21:24:27 -05001608 mnt = clone_mnt(old, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05001609
1610 if (!mnt)
Al Virob12cea92011-03-18 08:55:38 -04001611 goto out2;
Al Viroccd48bc2005-11-07 17:15:04 -05001612
Al Viro95bc5f22011-11-25 00:30:56 -05001613 err = graft_tree(mnt, path);
Al Viroccd48bc2005-11-07 17:15:04 -05001614 if (err) {
Nick Piggin99b7db72010-08-18 04:37:39 +10001615 br_write_lock(vfsmount_lock);
Al Viro761d5c32011-11-24 21:07:43 -05001616 umount_tree(mnt, 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001617 br_write_unlock(vfsmount_lock);
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05001618 }
Al Virob12cea92011-03-18 08:55:38 -04001619out2:
1620 unlock_mount(path);
1621 release_mounts(&umount_list);
Al Viroccd48bc2005-11-07 17:15:04 -05001622out:
Al Viro2d92ab32008-08-02 00:51:11 -04001623 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return err;
1625}
1626
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001627static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1628{
1629 int error = 0;
1630 int readonly_request = 0;
1631
1632 if (ms_flags & MS_RDONLY)
1633 readonly_request = 1;
1634 if (readonly_request == __mnt_is_readonly(mnt))
1635 return 0;
1636
1637 if (readonly_request)
Al Viro83adc752011-11-24 22:37:54 -05001638 error = mnt_make_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001639 else
Al Viro83adc752011-11-24 22:37:54 -05001640 __mnt_unmake_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001641 return error;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/*
1645 * change filesystem flags. dir should be a physical root of filesystem.
1646 * If you've mounted a non-root directory somewhere and want to do remount
1647 * on it - tough luck.
1648 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001649static int do_remount(struct path *path, int flags, int mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 void *data)
1651{
1652 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04001653 struct super_block *sb = path->mnt->mnt_sb;
Al Viro143c8c92011-11-25 00:46:35 -05001654 struct mount *mnt = real_mount(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (!capable(CAP_SYS_ADMIN))
1657 return -EPERM;
1658
Al Viro143c8c92011-11-25 00:46:35 -05001659 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return -EINVAL;
1661
Al Viro2d92ab32008-08-02 00:51:11 -04001662 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return -EINVAL;
1664
Eric Parisff36fe22011-03-03 16:09:14 -05001665 err = security_sb_remount(sb, data);
1666 if (err)
1667 return err;
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 down_write(&sb->s_umount);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001670 if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04001671 err = change_mount_flags(path->mnt, flags);
Al Viro4aa98cf2009-05-08 13:36:58 -04001672 else
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001673 err = do_remount_sb(sb, flags, data, 0);
Al Viro7b43a792010-01-16 13:01:26 -05001674 if (!err) {
Nick Piggin99b7db72010-08-18 04:37:39 +10001675 br_write_lock(vfsmount_lock);
Al Viro143c8c92011-11-25 00:46:35 -05001676 mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
1677 mnt->mnt.mnt_flags = mnt_flags;
Nick Piggin99b7db72010-08-18 04:37:39 +10001678 br_write_unlock(vfsmount_lock);
Al Viro7b43a792010-01-16 13:01:26 -05001679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 up_write(&sb->s_umount);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001681 if (!err) {
Nick Piggin99b7db72010-08-18 04:37:39 +10001682 br_write_lock(vfsmount_lock);
Al Viro143c8c92011-11-25 00:46:35 -05001683 touch_mnt_namespace(mnt->mnt_ns);
Nick Piggin99b7db72010-08-18 04:37:39 +10001684 br_write_unlock(vfsmount_lock);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return err;
1687}
1688
Al Virocbbe3622011-11-24 20:01:19 -05001689static inline int tree_contains_unbindable(struct mount *mnt)
Ram Pai9676f0c2005-11-07 17:21:20 -05001690{
Al Viro315fc832011-11-24 18:57:30 -05001691 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001692 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001693 if (IS_MNT_UNBINDABLE(p))
Ram Pai9676f0c2005-11-07 17:21:20 -05001694 return 1;
1695 }
1696 return 0;
1697}
1698
Al Viro0a0d8a42008-08-02 00:55:27 -04001699static int do_move_mount(struct path *path, char *old_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Al Viro2d92ab32008-08-02 00:51:11 -04001701 struct path old_path, parent_path;
Al Viro676da582011-11-24 21:47:05 -05001702 struct mount *p;
Al Viro0fb54e52011-11-24 19:59:16 -05001703 struct mount *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int err = 0;
1705 if (!capable(CAP_SYS_ADMIN))
1706 return -EPERM;
1707 if (!old_name || !*old_name)
1708 return -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001709 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (err)
1711 return err;
1712
Al Virob12cea92011-03-18 08:55:38 -04001713 err = lock_mount(path);
David Howellscc53ce52011-01-14 18:45:26 +00001714 if (err < 0)
1715 goto out;
1716
Al Viro143c8c92011-11-25 00:46:35 -05001717 old = real_mount(old_path.mnt);
Al Virofc7be132011-11-25 01:05:37 -05001718 p = real_mount(path->mnt);
Al Viro143c8c92011-11-25 00:46:35 -05001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05001721 if (!check_mnt(p) || !check_mnt(old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 goto out1;
1723
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04001724 if (d_unlinked(path->dentry))
Ram Pai21444402005-11-07 17:20:03 -05001725 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001728 if (old_path.dentry != old_path.mnt->mnt_root)
Ram Pai21444402005-11-07 17:20:03 -05001729 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Al Viro676da582011-11-24 21:47:05 -05001731 if (!mnt_has_parent(old))
Ram Pai21444402005-11-07 17:20:03 -05001732 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Al Viro2d92ab32008-08-02 00:51:11 -04001734 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1735 S_ISDIR(old_path.dentry->d_inode->i_mode))
Ram Pai21444402005-11-07 17:20:03 -05001736 goto out1;
1737 /*
1738 * Don't move a mount residing in a shared parent.
1739 */
Al Virofc7be132011-11-25 01:05:37 -05001740 if (IS_MNT_SHARED(old->mnt_parent))
Ram Pai21444402005-11-07 17:20:03 -05001741 goto out1;
Ram Pai9676f0c2005-11-07 17:21:20 -05001742 /*
1743 * Don't move a mount tree containing unbindable mounts to a destination
1744 * mount which is shared.
1745 */
Al Virofc7be132011-11-25 01:05:37 -05001746 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
Ram Pai9676f0c2005-11-07 17:21:20 -05001747 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 err = -ELOOP;
Al Virofc7be132011-11-25 01:05:37 -05001749 for (; mnt_has_parent(p); p = p->mnt_parent)
Al Viro676da582011-11-24 21:47:05 -05001750 if (p == old)
Ram Pai21444402005-11-07 17:20:03 -05001751 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Al Viro0fb54e52011-11-24 19:59:16 -05001753 err = attach_recursive_mnt(old, path, &parent_path);
Jan Blunck4ac91372008-02-14 19:34:32 -08001754 if (err)
Ram Pai21444402005-11-07 17:20:03 -05001755 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /* if the mount is moved, it should no longer be expire
1758 * automatically */
Al Viro6776db3d2011-11-25 00:22:05 -05001759 list_del_init(&old->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760out1:
Al Virob12cea92011-03-18 08:55:38 -04001761 unlock_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (!err)
Al Viro1a390682008-03-21 20:48:19 -04001764 path_put(&parent_path);
Al Viro2d92ab32008-08-02 00:51:11 -04001765 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return err;
1767}
1768
Al Viro9d412a42011-03-17 22:08:28 -04001769static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1770{
1771 int err;
1772 const char *subtype = strchr(fstype, '.');
1773 if (subtype) {
1774 subtype++;
1775 err = -EINVAL;
1776 if (!subtype[0])
1777 goto err;
1778 } else
1779 subtype = "";
1780
1781 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1782 err = -ENOMEM;
1783 if (!mnt->mnt_sb->s_subtype)
1784 goto err;
1785 return mnt;
1786
1787 err:
1788 mntput(mnt);
1789 return ERR_PTR(err);
1790}
1791
Al Viro79e801a2011-12-12 23:07:05 -05001792static struct vfsmount *
Al Viro9d412a42011-03-17 22:08:28 -04001793do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1794{
1795 struct file_system_type *type = get_fs_type(fstype);
1796 struct vfsmount *mnt;
1797 if (!type)
1798 return ERR_PTR(-ENODEV);
1799 mnt = vfs_kern_mount(type, flags, name, data);
1800 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1801 !mnt->mnt_sb->s_subtype)
1802 mnt = fs_set_subtype(mnt, fstype);
1803 put_filesystem(type);
1804 return mnt;
1805}
Al Viro9d412a42011-03-17 22:08:28 -04001806
1807/*
1808 * add a mount into a namespace's mount tree
1809 */
Al Viro95bc5f22011-11-25 00:30:56 -05001810static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
Al Viro9d412a42011-03-17 22:08:28 -04001811{
1812 int err;
1813
1814 mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
1815
Al Virob12cea92011-03-18 08:55:38 -04001816 err = lock_mount(path);
1817 if (err)
1818 return err;
Al Viro9d412a42011-03-17 22:08:28 -04001819
1820 err = -EINVAL;
Al Viro143c8c92011-11-25 00:46:35 -05001821 if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt)))
Al Viro9d412a42011-03-17 22:08:28 -04001822 goto unlock;
1823
1824 /* Refuse the same filesystem on the same mount point */
1825 err = -EBUSY;
Al Viro95bc5f22011-11-25 00:30:56 -05001826 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
Al Viro9d412a42011-03-17 22:08:28 -04001827 path->mnt->mnt_root == path->dentry)
1828 goto unlock;
1829
1830 err = -EINVAL;
Al Viro95bc5f22011-11-25 00:30:56 -05001831 if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
Al Viro9d412a42011-03-17 22:08:28 -04001832 goto unlock;
1833
Al Viro95bc5f22011-11-25 00:30:56 -05001834 newmnt->mnt.mnt_flags = mnt_flags;
Al Viro9d412a42011-03-17 22:08:28 -04001835 err = graft_tree(newmnt, path);
1836
1837unlock:
Al Virob12cea92011-03-18 08:55:38 -04001838 unlock_mount(path);
Al Viro9d412a42011-03-17 22:08:28 -04001839 return err;
1840}
Al Virob1e75df2011-01-17 01:47:59 -05001841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842/*
1843 * create a new mount for userspace and request it to be added into the
1844 * namespace's tree
1845 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001846static int do_new_mount(struct path *path, char *type, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 int mnt_flags, char *name, void *data)
1848{
1849 struct vfsmount *mnt;
Al Viro15f9a3f2011-01-17 01:41:58 -05001850 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Vegard Nossumeca6f532009-09-18 13:05:45 -07001852 if (!type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return -EINVAL;
1854
1855 /* we need capabilities... */
1856 if (!capable(CAP_SYS_ADMIN))
1857 return -EPERM;
1858
1859 mnt = do_kern_mount(type, flags, name, data);
1860 if (IS_ERR(mnt))
1861 return PTR_ERR(mnt);
1862
Al Viro95bc5f22011-11-25 00:30:56 -05001863 err = do_add_mount(real_mount(mnt), path, mnt_flags);
Al Viro15f9a3f2011-01-17 01:41:58 -05001864 if (err)
1865 mntput(mnt);
1866 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Al Viro19a167a2011-01-17 01:35:23 -05001869int finish_automount(struct vfsmount *m, struct path *path)
1870{
Al Viro6776db3d2011-11-25 00:22:05 -05001871 struct mount *mnt = real_mount(m);
Al Viro19a167a2011-01-17 01:35:23 -05001872 int err;
1873 /* The new mount record should have at least 2 refs to prevent it being
1874 * expired before we get a chance to add it
1875 */
Al Viro6776db3d2011-11-25 00:22:05 -05001876 BUG_ON(mnt_get_count(mnt) < 2);
Al Viro19a167a2011-01-17 01:35:23 -05001877
1878 if (m->mnt_sb == path->mnt->mnt_sb &&
1879 m->mnt_root == path->dentry) {
Al Virob1e75df2011-01-17 01:47:59 -05001880 err = -ELOOP;
1881 goto fail;
Al Viro19a167a2011-01-17 01:35:23 -05001882 }
1883
Al Viro95bc5f22011-11-25 00:30:56 -05001884 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
Al Virob1e75df2011-01-17 01:47:59 -05001885 if (!err)
1886 return 0;
1887fail:
1888 /* remove m from any expiration list it may be on */
Al Viro6776db3d2011-11-25 00:22:05 -05001889 if (!list_empty(&mnt->mnt_expire)) {
Al Virob1e75df2011-01-17 01:47:59 -05001890 down_write(&namespace_sem);
1891 br_write_lock(vfsmount_lock);
Al Viro6776db3d2011-11-25 00:22:05 -05001892 list_del_init(&mnt->mnt_expire);
Al Virob1e75df2011-01-17 01:47:59 -05001893 br_write_unlock(vfsmount_lock);
1894 up_write(&namespace_sem);
Al Viro19a167a2011-01-17 01:35:23 -05001895 }
Al Virob1e75df2011-01-17 01:47:59 -05001896 mntput(m);
1897 mntput(m);
Al Viro19a167a2011-01-17 01:35:23 -05001898 return err;
1899}
1900
David Howellsea5b7782011-01-14 19:10:03 +00001901/**
1902 * mnt_set_expiry - Put a mount on an expiration list
1903 * @mnt: The mount to list.
1904 * @expiry_list: The list to add the mount to.
1905 */
1906void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
1907{
1908 down_write(&namespace_sem);
1909 br_write_lock(vfsmount_lock);
1910
Al Viro6776db3d2011-11-25 00:22:05 -05001911 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
David Howellsea5b7782011-01-14 19:10:03 +00001912
1913 br_write_unlock(vfsmount_lock);
1914 up_write(&namespace_sem);
1915}
1916EXPORT_SYMBOL(mnt_set_expiry);
1917
1918/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * process a list of expirable mountpoints with the intent of discarding any
1920 * mountpoints that aren't in use and haven't been touched since last we came
1921 * here
1922 */
1923void mark_mounts_for_expiry(struct list_head *mounts)
1924{
Al Viro761d5c32011-11-24 21:07:43 -05001925 struct mount *mnt, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 LIST_HEAD(graveyard);
Al Virobcc5c7d2008-03-22 00:21:53 -04001927 LIST_HEAD(umounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 if (list_empty(mounts))
1930 return;
1931
Al Virobcc5c7d2008-03-22 00:21:53 -04001932 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10001933 br_write_lock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* extract from the expiration list every vfsmount that matches the
1936 * following criteria:
1937 * - only referenced by its parent vfsmount
1938 * - still marked for expiry (marked on the last call here; marks are
1939 * cleared by mntput())
1940 */
Al Viro6776db3d2011-11-25 00:22:05 -05001941 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Al Viro863d6842011-11-25 00:57:42 -05001942 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Viro1ab59732011-11-24 21:35:16 -05001943 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 continue;
Al Viro6776db3d2011-11-25 00:22:05 -05001945 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Al Virobcc5c7d2008-03-22 00:21:53 -04001947 while (!list_empty(&graveyard)) {
Al Viro6776db3d2011-11-25 00:22:05 -05001948 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05001949 touch_mnt_namespace(mnt->mnt_ns);
Al Virobcc5c7d2008-03-22 00:21:53 -04001950 umount_tree(mnt, 1, &umounts);
1951 }
Nick Piggin99b7db72010-08-18 04:37:39 +10001952 br_write_unlock(vfsmount_lock);
Al Virobcc5c7d2008-03-22 00:21:53 -04001953 up_write(&namespace_sem);
1954
1955 release_mounts(&umounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
1958EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1959
1960/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04001961 * Ripoff of 'select_parent()'
1962 *
1963 * search the list of submounts for a given mountpoint, and move any
1964 * shrinkable submounts to the 'graveyard' list.
1965 */
Al Viro692afc32011-11-24 21:15:14 -05001966static int select_submounts(struct mount *parent, struct list_head *graveyard)
Trond Myklebust5528f9112006-06-09 09:34:17 -04001967{
Al Viro692afc32011-11-24 21:15:14 -05001968 struct mount *this_parent = parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04001969 struct list_head *next;
1970 int found = 0;
1971
1972repeat:
Al Viro6b41d532011-11-24 23:24:33 -05001973 next = this_parent->mnt_mounts.next;
Trond Myklebust5528f9112006-06-09 09:34:17 -04001974resume:
Al Viro6b41d532011-11-24 23:24:33 -05001975 while (next != &this_parent->mnt_mounts) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04001976 struct list_head *tmp = next;
Al Viro6b41d532011-11-24 23:24:33 -05001977 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
Trond Myklebust5528f9112006-06-09 09:34:17 -04001978
1979 next = tmp->next;
Al Viro692afc32011-11-24 21:15:14 -05001980 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
Trond Myklebust5528f9112006-06-09 09:34:17 -04001981 continue;
1982 /*
1983 * Descend a level if the d_mounts list is non-empty.
1984 */
Al Viro6b41d532011-11-24 23:24:33 -05001985 if (!list_empty(&mnt->mnt_mounts)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04001986 this_parent = mnt;
1987 goto repeat;
1988 }
1989
Al Viro1ab59732011-11-24 21:35:16 -05001990 if (!propagate_mount_busy(mnt, 1)) {
Al Viro6776db3d2011-11-25 00:22:05 -05001991 list_move_tail(&mnt->mnt_expire, graveyard);
Trond Myklebust5528f9112006-06-09 09:34:17 -04001992 found++;
1993 }
1994 }
1995 /*
1996 * All done at this level ... ascend and resume the search
1997 */
1998 if (this_parent != parent) {
Al Viro6b41d532011-11-24 23:24:33 -05001999 next = this_parent->mnt_child.next;
Al Viro0714a532011-11-24 22:19:58 -05002000 this_parent = this_parent->mnt_parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002001 goto resume;
2002 }
2003 return found;
2004}
2005
2006/*
2007 * process a list of expirable mountpoints with the intent of discarding any
2008 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10002009 *
2010 * vfsmount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04002011 */
Al Viro692afc32011-11-24 21:15:14 -05002012static void shrink_submounts(struct mount *mnt, struct list_head *umounts)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002013{
2014 LIST_HEAD(graveyard);
Al Viro761d5c32011-11-24 21:07:43 -05002015 struct mount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002016
Trond Myklebust5528f9112006-06-09 09:34:17 -04002017 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04002018 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04002019 while (!list_empty(&graveyard)) {
Al Viro761d5c32011-11-24 21:07:43 -05002020 m = list_first_entry(&graveyard, struct mount,
Al Viro6776db3d2011-11-25 00:22:05 -05002021 mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002022 touch_mnt_namespace(m->mnt_ns);
Eric W. Biedermanafef80b2008-11-12 13:26:54 -08002023 umount_tree(m, 1, umounts);
Al Virobcc5c7d2008-03-22 00:21:53 -04002024 }
2025 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04002026}
2027
Trond Myklebust5528f9112006-06-09 09:34:17 -04002028/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 * Some copy_from_user() implementations do not return the exact number of
2030 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2031 * Note that this function differs from copy_from_user() in that it will oops
2032 * on bad values of `to', rather than returning a short copy.
2033 */
Ram Paib58fed82005-11-07 17:16:09 -05002034static long exact_copy_from_user(void *to, const void __user * from,
2035 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 char *t = to;
2038 const char __user *f = from;
2039 char c;
2040
2041 if (!access_ok(VERIFY_READ, from, n))
2042 return n;
2043
2044 while (n) {
2045 if (__get_user(c, f)) {
2046 memset(t, 0, n);
2047 break;
2048 }
2049 *t++ = c;
2050 f++;
2051 n--;
2052 }
2053 return n;
2054}
2055
Ram Paib58fed82005-11-07 17:16:09 -05002056int copy_mount_options(const void __user * data, unsigned long *where)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 int i;
2059 unsigned long page;
2060 unsigned long size;
Ram Paib58fed82005-11-07 17:16:09 -05002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 *where = 0;
2063 if (!data)
2064 return 0;
2065
2066 if (!(page = __get_free_page(GFP_KERNEL)))
2067 return -ENOMEM;
2068
2069 /* We only care that *some* data at the address the user
2070 * gave us is valid. Just in case, we'll zero
2071 * the remainder of the page.
2072 */
2073 /* copy_from_user cannot cross TASK_SIZE ! */
2074 size = TASK_SIZE - (unsigned long)data;
2075 if (size > PAGE_SIZE)
2076 size = PAGE_SIZE;
2077
2078 i = size - exact_copy_from_user((void *)page, data, size);
2079 if (!i) {
Ram Paib58fed82005-11-07 17:16:09 -05002080 free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return -EFAULT;
2082 }
2083 if (i != PAGE_SIZE)
2084 memset((char *)page + i, 0, PAGE_SIZE - i);
2085 *where = page;
2086 return 0;
2087}
2088
Vegard Nossumeca6f532009-09-18 13:05:45 -07002089int copy_mount_string(const void __user *data, char **where)
2090{
2091 char *tmp;
2092
2093 if (!data) {
2094 *where = NULL;
2095 return 0;
2096 }
2097
2098 tmp = strndup_user(data, PAGE_SIZE);
2099 if (IS_ERR(tmp))
2100 return PTR_ERR(tmp);
2101
2102 *where = tmp;
2103 return 0;
2104}
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106/*
2107 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2108 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2109 *
2110 * data is a (void *) that can point to any structure up to
2111 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2112 * information (or be NULL).
2113 *
2114 * Pre-0.97 versions of mount() didn't have a flags word.
2115 * When the flags word was introduced its top half was required
2116 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2117 * Therefore, if this magic number is present, it carries no information
2118 * and must be discarded.
2119 */
Ram Paib58fed82005-11-07 17:16:09 -05002120long do_mount(char *dev_name, char *dir_name, char *type_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 unsigned long flags, void *data_page)
2122{
Al Viro2d92ab32008-08-02 00:51:11 -04002123 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 int retval = 0;
2125 int mnt_flags = 0;
2126
2127 /* Discard magic */
2128 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2129 flags &= ~MS_MGC_MSK;
2130
2131 /* Basic sanity checks */
2132
2133 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2134 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (data_page)
2137 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2138
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002139 /* ... and get the mountpoint */
2140 retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2141 if (retval)
2142 return retval;
2143
2144 retval = security_sb_mount(dev_name, &path,
2145 type_page, flags, data_page);
2146 if (retval)
2147 goto dput_out;
2148
Andi Kleen613cbe32009-04-19 18:40:43 +02002149 /* Default to relatime unless overriden */
2150 if (!(flags & MS_NOATIME))
2151 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /* Separate the per-mountpoint flags */
2154 if (flags & MS_NOSUID)
2155 mnt_flags |= MNT_NOSUID;
2156 if (flags & MS_NODEV)
2157 mnt_flags |= MNT_NODEV;
2158 if (flags & MS_NOEXEC)
2159 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002160 if (flags & MS_NOATIME)
2161 mnt_flags |= MNT_NOATIME;
2162 if (flags & MS_NODIRATIME)
2163 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00002164 if (flags & MS_STRICTATIME)
2165 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002166 if (flags & MS_RDONLY)
2167 mnt_flags |= MNT_READONLY;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002168
Al Viro7a4dec52010-08-09 12:05:43 -04002169 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
Matthew Garrettd0adde52009-03-26 17:49:56 +00002170 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2171 MS_STRICTATIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (flags & MS_REMOUNT)
Al Viro2d92ab32008-08-02 00:51:11 -04002174 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 data_page);
2176 else if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002177 retval = do_loopback(&path, dev_name, flags & MS_REC);
Ram Pai9676f0c2005-11-07 17:21:20 -05002178 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Al Viro2d92ab32008-08-02 00:51:11 -04002179 retval = do_change_type(&path, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 else if (flags & MS_MOVE)
Al Viro2d92ab32008-08-02 00:51:11 -04002181 retval = do_move_mount(&path, dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 else
Al Viro2d92ab32008-08-02 00:51:11 -04002183 retval = do_new_mount(&path, type_page, flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 dev_name, data_page);
2185dput_out:
Al Viro2d92ab32008-08-02 00:51:11 -04002186 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return retval;
2188}
2189
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002190static struct mnt_namespace *alloc_mnt_ns(void)
2191{
2192 struct mnt_namespace *new_ns;
2193
2194 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2195 if (!new_ns)
2196 return ERR_PTR(-ENOMEM);
2197 atomic_set(&new_ns->count, 1);
2198 new_ns->root = NULL;
2199 INIT_LIST_HEAD(&new_ns->list);
2200 init_waitqueue_head(&new_ns->poll);
2201 new_ns->event = 0;
2202 return new_ns;
2203}
2204
Al Virof03c6592011-01-14 22:30:21 -05002205void mnt_make_longterm(struct vfsmount *mnt)
2206{
Al Viro83adc752011-11-24 22:37:54 -05002207 __mnt_make_longterm(real_mount(mnt));
Al Virof03c6592011-01-14 22:30:21 -05002208}
2209
Al Viro83adc752011-11-24 22:37:54 -05002210void mnt_make_shortterm(struct vfsmount *m)
Al Virof03c6592011-01-14 22:30:21 -05002211{
Al Viro7e3d0eb02011-01-16 16:32:11 -05002212#ifdef CONFIG_SMP
Al Viro83adc752011-11-24 22:37:54 -05002213 struct mount *mnt = real_mount(m);
Al Viro68e8a9f2011-11-24 22:53:09 -05002214 if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
Al Virof03c6592011-01-14 22:30:21 -05002215 return;
2216 br_write_lock(vfsmount_lock);
Al Viro68e8a9f2011-11-24 22:53:09 -05002217 atomic_dec(&mnt->mnt_longterm);
Al Virof03c6592011-01-14 22:30:21 -05002218 br_write_unlock(vfsmount_lock);
Al Viro7e3d0eb02011-01-16 16:32:11 -05002219#endif
Al Virof03c6592011-01-14 22:30:21 -05002220}
2221
JANAK DESAI741a2952006-02-07 12:59:00 -08002222/*
2223 * Allocate a new namespace structure and populate it with contents
2224 * copied from the namespace of the passed in task structure.
2225 */
Badari Pulavartye3222c42007-05-08 00:25:21 -07002226static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002227 struct fs_struct *fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002229 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04002230 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Al Viro315fc832011-11-24 18:57:30 -05002231 struct mount *p, *q;
Al Virobe08d6d2011-12-06 13:32:36 -05002232 struct mount *old = mnt_ns->root;
Al Virocb338d02011-11-24 20:55:08 -05002233 struct mount *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002235 new_ns = alloc_mnt_ns();
2236 if (IS_ERR(new_ns))
2237 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Ram Pai390c6842005-11-07 17:17:51 -05002239 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /* First pass: copy the tree topology */
Al Viro909b0a82011-11-25 03:06:56 -05002241 new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE);
Al Virocb338d02011-11-24 20:55:08 -05002242 if (!new) {
Ram Pai390c6842005-11-07 17:17:51 -05002243 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 kfree(new_ns);
Julia Lawall5cc4a032008-12-01 14:34:51 -08002245 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Al Virobe08d6d2011-12-06 13:32:36 -05002247 new_ns->root = new;
Nick Piggin99b7db72010-08-18 04:37:39 +10002248 br_write_lock(vfsmount_lock);
Al Viro1a4eeaf2011-11-25 02:19:55 -05002249 list_add_tail(&new_ns->list, &new->mnt_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10002250 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 /*
2253 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2254 * as belonging to new namespace. We have already acquired a private
2255 * fs_struct, so tsk->fs->lock is not needed.
2256 */
Al Viro909b0a82011-11-25 03:06:56 -05002257 p = old;
Al Virocb338d02011-11-24 20:55:08 -05002258 q = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 while (p) {
Al Viro143c8c92011-11-25 00:46:35 -05002260 q->mnt_ns = new_ns;
Al Viro83adc752011-11-24 22:37:54 -05002261 __mnt_make_longterm(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (fs) {
Al Viro315fc832011-11-24 18:57:30 -05002263 if (&p->mnt == fs->root.mnt) {
2264 fs->root.mnt = mntget(&q->mnt);
Al Viro83adc752011-11-24 22:37:54 -05002265 __mnt_make_longterm(q);
Al Viro315fc832011-11-24 18:57:30 -05002266 mnt_make_shortterm(&p->mnt);
2267 rootmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
Al Viro315fc832011-11-24 18:57:30 -05002269 if (&p->mnt == fs->pwd.mnt) {
2270 fs->pwd.mnt = mntget(&q->mnt);
Al Viro83adc752011-11-24 22:37:54 -05002271 __mnt_make_longterm(q);
Al Viro315fc832011-11-24 18:57:30 -05002272 mnt_make_shortterm(&p->mnt);
2273 pwdmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
Al Viro909b0a82011-11-25 03:06:56 -05002276 p = next_mnt(p, old);
2277 q = next_mnt(q, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Ram Pai390c6842005-11-07 17:17:51 -05002279 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (rootmnt)
Al Virof03c6592011-01-14 22:30:21 -05002282 mntput(rootmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (pwdmnt)
Al Virof03c6592011-01-14 22:30:21 -05002284 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
JANAK DESAI741a2952006-02-07 12:59:00 -08002286 return new_ns;
2287}
2288
Eric W. Biederman213dd262007-07-15 23:41:15 -07002289struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
Badari Pulavartye3222c42007-05-08 00:25:21 -07002290 struct fs_struct *new_fs)
JANAK DESAI741a2952006-02-07 12:59:00 -08002291{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002292 struct mnt_namespace *new_ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002293
Badari Pulavartye3222c42007-05-08 00:25:21 -07002294 BUG_ON(!ns);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002295 get_mnt_ns(ns);
JANAK DESAI741a2952006-02-07 12:59:00 -08002296
2297 if (!(flags & CLONE_NEWNS))
Badari Pulavartye3222c42007-05-08 00:25:21 -07002298 return ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002299
Badari Pulavartye3222c42007-05-08 00:25:21 -07002300 new_ns = dup_mnt_ns(ns, new_fs);
JANAK DESAI741a2952006-02-07 12:59:00 -08002301
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002302 put_mnt_ns(ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -07002303 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002306/**
2307 * create_mnt_ns - creates a private namespace and adds a root filesystem
2308 * @mnt: pointer to the new root filesystem mountpoint
2309 */
Al Viro1a4eeaf2011-11-25 02:19:55 -05002310static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002311{
Al Viro1a4eeaf2011-11-25 02:19:55 -05002312 struct mnt_namespace *new_ns = alloc_mnt_ns();
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002313 if (!IS_ERR(new_ns)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002314 struct mount *mnt = real_mount(m);
2315 mnt->mnt_ns = new_ns;
2316 __mnt_make_longterm(mnt);
Al Virobe08d6d2011-12-06 13:32:36 -05002317 new_ns->root = mnt;
Al Viro1a4eeaf2011-11-25 02:19:55 -05002318 list_add(&new_ns->list, &mnt->mnt_list);
Al Viroc1334492011-11-16 16:12:14 -05002319 } else {
Al Viro1a4eeaf2011-11-25 02:19:55 -05002320 mntput(m);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002321 }
2322 return new_ns;
2323}
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002324
Al Viroea441d12011-11-16 21:43:59 -05002325struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2326{
2327 struct mnt_namespace *ns;
Al Virod31da0f2011-11-22 12:31:21 -05002328 struct super_block *s;
Al Viroea441d12011-11-16 21:43:59 -05002329 struct path path;
2330 int err;
2331
2332 ns = create_mnt_ns(mnt);
2333 if (IS_ERR(ns))
2334 return ERR_CAST(ns);
2335
2336 err = vfs_path_lookup(mnt->mnt_root, mnt,
2337 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2338
2339 put_mnt_ns(ns);
2340
2341 if (err)
2342 return ERR_PTR(err);
2343
2344 /* trade a vfsmount reference for active sb one */
Al Virod31da0f2011-11-22 12:31:21 -05002345 s = path.mnt->mnt_sb;
2346 atomic_inc(&s->s_active);
Al Viroea441d12011-11-16 21:43:59 -05002347 mntput(path.mnt);
2348 /* lock the sucker */
Al Virod31da0f2011-11-22 12:31:21 -05002349 down_write(&s->s_umount);
Al Viroea441d12011-11-16 21:43:59 -05002350 /* ... and return the root of (sub)tree on it */
2351 return path.dentry;
2352}
2353EXPORT_SYMBOL(mount_subtree);
2354
Heiko Carstensbdc480e2009-01-14 14:14:12 +01002355SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2356 char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Vegard Nossumeca6f532009-09-18 13:05:45 -07002358 int ret;
2359 char *kernel_type;
2360 char *kernel_dir;
2361 char *kernel_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 unsigned long data_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Vegard Nossumeca6f532009-09-18 13:05:45 -07002364 ret = copy_mount_string(type, &kernel_type);
2365 if (ret < 0)
2366 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Vegard Nossumeca6f532009-09-18 13:05:45 -07002368 kernel_dir = getname(dir_name);
2369 if (IS_ERR(kernel_dir)) {
2370 ret = PTR_ERR(kernel_dir);
2371 goto out_dir;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Vegard Nossumeca6f532009-09-18 13:05:45 -07002374 ret = copy_mount_string(dev_name, &kernel_dev);
2375 if (ret < 0)
2376 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Vegard Nossumeca6f532009-09-18 13:05:45 -07002378 ret = copy_mount_options(data, &data_page);
2379 if (ret < 0)
2380 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Vegard Nossumeca6f532009-09-18 13:05:45 -07002382 ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
2383 (void *) data_page);
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 free_page(data_page);
Vegard Nossumeca6f532009-09-18 13:05:45 -07002386out_data:
2387 kfree(kernel_dev);
2388out_dev:
2389 putname(kernel_dir);
2390out_dir:
2391 kfree(kernel_type);
2392out_type:
2393 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396/*
Al Viroafac7cb2011-11-23 19:34:49 -05002397 * Return true if path is reachable from root
2398 *
2399 * namespace_sem or vfsmount_lock is held
2400 */
Al Viro643822b2011-11-24 22:00:28 -05002401bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
Al Viroafac7cb2011-11-23 19:34:49 -05002402 const struct path *root)
2403{
Al Viro643822b2011-11-24 22:00:28 -05002404 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
Al Viroa73324d2011-11-24 22:25:07 -05002405 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05002406 mnt = mnt->mnt_parent;
Al Viroafac7cb2011-11-23 19:34:49 -05002407 }
Al Viro643822b2011-11-24 22:00:28 -05002408 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
Al Viroafac7cb2011-11-23 19:34:49 -05002409}
2410
2411int path_is_under(struct path *path1, struct path *path2)
2412{
2413 int res;
2414 br_read_lock(vfsmount_lock);
Al Viro643822b2011-11-24 22:00:28 -05002415 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
Al Viroafac7cb2011-11-23 19:34:49 -05002416 br_read_unlock(vfsmount_lock);
2417 return res;
2418}
2419EXPORT_SYMBOL(path_is_under);
2420
2421/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * pivot_root Semantics:
2423 * Moves the root file system of the current process to the directory put_old,
2424 * makes new_root as the new root file system of the current process, and sets
2425 * root/cwd of all processes which had them on the current root to new_root.
2426 *
2427 * Restrictions:
2428 * The new_root and put_old must be directories, and must not be on the
2429 * same file system as the current process root. The put_old must be
2430 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2431 * pointed to by put_old must yield the same directory as new_root. No other
2432 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2433 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08002434 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2435 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2436 * in this situation.
2437 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 * Notes:
2439 * - we don't move root/cwd if they are not at the root (reason: if something
2440 * cared enough to change them, it's probably wrong to force them elsewhere)
2441 * - it's okay to pick a root that isn't the root of a file system, e.g.
2442 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2443 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2444 * first.
2445 */
Heiko Carstens3480b252009-01-14 14:14:16 +01002446SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2447 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Al Viro2d8f3032008-07-22 09:59:21 -04002449 struct path new, old, parent_path, root_parent, root;
Al Viro419148d2011-11-24 19:41:16 -05002450 struct mount *new_mnt, *root_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 int error;
2452
2453 if (!capable(CAP_SYS_ADMIN))
2454 return -EPERM;
2455
Al Viro2d8f3032008-07-22 09:59:21 -04002456 error = user_path_dir(new_root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (error)
2458 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Al Viro2d8f3032008-07-22 09:59:21 -04002460 error = user_path_dir(put_old, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (error)
2462 goto out1;
2463
Al Viro2d8f3032008-07-22 09:59:21 -04002464 error = security_sb_pivotroot(&old, &new);
Al Virob12cea92011-03-18 08:55:38 -04002465 if (error)
2466 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002468 get_fs_root(current->fs, &root);
Al Virob12cea92011-03-18 08:55:38 -04002469 error = lock_mount(&old);
2470 if (error)
2471 goto out3;
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 error = -EINVAL;
Al Viro419148d2011-11-24 19:41:16 -05002474 new_mnt = real_mount(new.mnt);
2475 root_mnt = real_mount(root.mnt);
Al Virofc7be132011-11-25 01:05:37 -05002476 if (IS_MNT_SHARED(real_mount(old.mnt)) ||
2477 IS_MNT_SHARED(new_mnt->mnt_parent) ||
2478 IS_MNT_SHARED(root_mnt->mnt_parent))
Al Virob12cea92011-03-18 08:55:38 -04002479 goto out4;
Al Viro143c8c92011-11-25 00:46:35 -05002480 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002481 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002483 if (d_unlinked(new.dentry))
Al Virob12cea92011-03-18 08:55:38 -04002484 goto out4;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002485 if (d_unlinked(old.dentry))
Al Virob12cea92011-03-18 08:55:38 -04002486 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 error = -EBUSY;
Al Viro2d8f3032008-07-22 09:59:21 -04002488 if (new.mnt == root.mnt ||
2489 old.mnt == root.mnt)
Al Virob12cea92011-03-18 08:55:38 -04002490 goto out4; /* loop, on the same file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04002492 if (root.mnt->mnt_root != root.dentry)
Al Virob12cea92011-03-18 08:55:38 -04002493 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05002494 if (!mnt_has_parent(root_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002495 goto out4; /* not attached */
Al Viro2d8f3032008-07-22 09:59:21 -04002496 if (new.mnt->mnt_root != new.dentry)
Al Virob12cea92011-03-18 08:55:38 -04002497 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05002498 if (!mnt_has_parent(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04002499 goto out4; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08002500 /* make sure we can reach put_old from new_root */
Al Viro643822b2011-11-24 22:00:28 -05002501 if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new))
Al Virob12cea92011-03-18 08:55:38 -04002502 goto out4;
Al Viro27cb1572011-03-18 08:29:36 -04002503 br_write_lock(vfsmount_lock);
Al Viro419148d2011-11-24 19:41:16 -05002504 detach_mnt(new_mnt, &parent_path);
2505 detach_mnt(root_mnt, &root_parent);
Jan Blunck4ac91372008-02-14 19:34:32 -08002506 /* mount old root on put_old */
Al Viro419148d2011-11-24 19:41:16 -05002507 attach_mnt(root_mnt, &old);
Jan Blunck4ac91372008-02-14 19:34:32 -08002508 /* mount new_root on / */
Al Viro419148d2011-11-24 19:41:16 -05002509 attach_mnt(new_mnt, &root_parent);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002510 touch_mnt_namespace(current->nsproxy->mnt_ns);
Nick Piggin99b7db72010-08-18 04:37:39 +10002511 br_write_unlock(vfsmount_lock);
Al Viro2d8f3032008-07-22 09:59:21 -04002512 chroot_fs_refs(&root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 error = 0;
Al Virob12cea92011-03-18 08:55:38 -04002514out4:
2515 unlock_mount(&old);
2516 if (!error) {
2517 path_put(&root_parent);
2518 path_put(&parent_path);
2519 }
2520out3:
Al Viro8c3ee422008-03-22 18:00:39 -04002521 path_put(&root);
Al Virob12cea92011-03-18 08:55:38 -04002522out2:
Al Viro2d8f3032008-07-22 09:59:21 -04002523 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524out1:
Al Viro2d8f3032008-07-22 09:59:21 -04002525 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
2530static void __init init_mount_tree(void)
2531{
2532 struct vfsmount *mnt;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002533 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08002534 struct path root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
2537 if (IS_ERR(mnt))
2538 panic("Can't create rootfs");
Nick Pigginb3e19d92011-01-07 17:50:11 +11002539
Trond Myklebust3b22edc2009-06-23 17:29:49 -04002540 ns = create_mnt_ns(mnt);
2541 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 panic("Can't allocate initial namespace");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002544 init_task.nsproxy->mnt_ns = ns;
2545 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Al Virobe08d6d2011-12-06 13:32:36 -05002547 root.mnt = mnt;
2548 root.dentry = mnt->mnt_root;
Jan Blunckac748a02008-02-14 19:34:39 -08002549
2550 set_fs_pwd(current->fs, &root);
2551 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Denis Cheng74bf17c2007-10-16 23:26:30 -07002554void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Eric Dumazet13f14b42008-02-06 01:37:57 -08002556 unsigned u;
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002557 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Ram Pai390c6842005-11-07 17:17:51 -05002559 init_rwsem(&namespace_sem);
2560
Al Viro7d6fec42011-11-23 12:14:10 -05002561 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
Paul Mundt20c2df82007-07-20 10:11:58 +09002562 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Ram Paib58fed82005-11-07 17:16:09 -05002564 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 if (!mount_hashtable)
2567 panic("Failed to allocate mount hash table\n");
2568
Mandeep Singh Baines80cdc6d2011-03-22 16:33:54 -07002569 printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Eric Dumazet13f14b42008-02-06 01:37:57 -08002571 for (u = 0; u < HASH_SIZE; u++)
2572 INIT_LIST_HEAD(&mount_hashtable[u]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Nick Piggin99b7db72010-08-18 04:37:39 +10002574 br_lock_init(vfsmount_lock);
2575
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002576 err = sysfs_init();
2577 if (err)
2578 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002579 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06002580 fs_kobj = kobject_create_and_add("fs", NULL);
2581 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002582 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 init_rootfs();
2584 init_mount_tree();
2585}
2586
Trond Myklebust616511d2009-06-22 15:09:13 -04002587void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Ram Pai70fbcdf2005-11-07 17:17:04 -05002589 LIST_HEAD(umount_list);
Trond Myklebust616511d2009-06-22 15:09:13 -04002590
Al Virod498b252010-02-05 02:21:06 -05002591 if (!atomic_dec_and_test(&ns->count))
Trond Myklebust616511d2009-06-22 15:09:13 -04002592 return;
Ram Pai390c6842005-11-07 17:17:51 -05002593 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10002594 br_write_lock(vfsmount_lock);
Al Virobe08d6d2011-12-06 13:32:36 -05002595 umount_tree(ns->root, 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10002596 br_write_unlock(vfsmount_lock);
Ram Pai390c6842005-11-07 17:17:51 -05002597 up_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -05002598 release_mounts(&umount_list);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002599 kfree(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
Al Viro9d412a42011-03-17 22:08:28 -04002601
2602struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
2603{
Tim Chen423e0ab2011-07-19 09:32:38 -07002604 struct vfsmount *mnt;
2605 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2606 if (!IS_ERR(mnt)) {
2607 /*
2608 * it is a longterm mount, don't release mnt until
2609 * we unmount before file sys is unregistered
2610 */
2611 mnt_make_longterm(mnt);
2612 }
2613 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04002614}
2615EXPORT_SYMBOL_GPL(kern_mount_data);
Tim Chen423e0ab2011-07-19 09:32:38 -07002616
2617void kern_unmount(struct vfsmount *mnt)
2618{
2619 /* release long term mount so mount point can be released */
2620 if (!IS_ERR_OR_NULL(mnt)) {
2621 mnt_make_shortterm(mnt);
2622 mntput(mnt);
2623 }
2624}
2625EXPORT_SYMBOL(kern_unmount);
Al Viro02125a82011-12-05 08:43:34 -05002626
2627bool our_mnt(struct vfsmount *mnt)
2628{
Al Viro143c8c92011-11-25 00:46:35 -05002629 return check_mnt(real_mount(mnt));
Al Viro02125a82011-12-05 08:43:34 -05002630}