blob: 3dbfc072ec70aa0099b79b3ff1040455543c46ca [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>
12#include <linux/slab.h>
13#include <linux/sched.h>
Nick Piggin99b7db72010-08-18 04:37:39 +100014#include <linux/spinlock.h>
15#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
Randy Dunlap15a67dd2006-09-29 01:58:57 -070017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/acct.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080019#include <linux/capability.h>
Dave Hansen3d733632008-02-15 14:37:59 -080020#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
Andrew Mortonf20a9ea2006-08-14 22:43:23 -070022#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/seq_file.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080024#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/namei.h>
Alexey Dobriyanb43f3cb2009-07-08 01:54:37 +040026#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/security.h>
28#include <linux/mount.h>
David Howells07f3f052006-09-30 20:52:18 +020029#include <linux/ramfs.h>
Eric Dumazet13f14b42008-02-06 01:37:57 -080030#include <linux/log2.h>
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010031#include <linux/idr.h>
Al Viro5ad4e532009-03-29 19:50:06 -040032#include <linux/fs_struct.h>
Andreas Gruenbacher2504c5d2009-12-17 21:24:27 -050033#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/uaccess.h>
35#include <asm/unistd.h>
Ram Pai07b20882005-11-07 17:19:07 -050036#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070037#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Eric Dumazet13f14b42008-02-06 01:37:57 -080039#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
40#define HASH_SIZE (1UL << HASH_SHIFT)
41
Al Viro5addc5d2005-11-07 17:15:49 -050042static int event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010043static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010044static DEFINE_IDA(mnt_group_ida);
Nick Piggin99b7db72010-08-18 04:37:39 +100045static DEFINE_SPINLOCK(mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040046static int mnt_id_start = 0;
47static int mnt_group_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Eric Dumazetfa3536c2006-03-26 01:37:24 -080049static struct list_head *mount_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080050static struct kmem_cache *mnt_cache __read_mostly;
Ram Pai390c6842005-11-07 17:17:51 -050051static struct rw_semaphore namespace_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080053/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060054struct kobject *fs_kobj;
55EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080056
Nick Piggin99b7db72010-08-18 04:37:39 +100057/*
58 * vfsmount lock may be taken for read to prevent changes to the
59 * vfsmount hash, ie. during mountpoint lookups or walking back
60 * up the tree.
61 *
62 * It should be taken for write in all cases where the vfsmount
63 * tree or hash is modified or when a vfsmount structure is modified.
64 */
65DEFINE_BRLOCK(vfsmount_lock);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
68{
Ram Paib58fed82005-11-07 17:16:09 -050069 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
70 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Eric Dumazet13f14b42008-02-06 01:37:57 -080071 tmp = tmp + (tmp >> HASH_SHIFT);
72 return tmp & (HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Dave Hansen3d733632008-02-15 14:37:59 -080075#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
76
Nick Piggin99b7db72010-08-18 04:37:39 +100077/*
78 * allocation is serialized by namespace_sem, but we need the spinlock to
79 * serialize with freeing.
80 */
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010081static int mnt_alloc_id(struct vfsmount *mnt)
82{
83 int res;
84
85retry:
86 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
Nick Piggin99b7db72010-08-18 04:37:39 +100087 spin_lock(&mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040088 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
89 if (!res)
90 mnt_id_start = mnt->mnt_id + 1;
Nick Piggin99b7db72010-08-18 04:37:39 +100091 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010092 if (res == -EAGAIN)
93 goto retry;
94
95 return res;
96}
97
98static void mnt_free_id(struct vfsmount *mnt)
99{
Al Virof21f6222009-06-24 03:12:00 -0400100 int id = mnt->mnt_id;
Nick Piggin99b7db72010-08-18 04:37:39 +1000101 spin_lock(&mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -0400102 ida_remove(&mnt_id_ida, id);
103 if (mnt_id_start > id)
104 mnt_id_start = id;
Nick Piggin99b7db72010-08-18 04:37:39 +1000105 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100106}
107
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100108/*
109 * Allocate a new peer group ID
110 *
111 * mnt_group_ida is protected by namespace_sem
112 */
113static int mnt_alloc_group_id(struct vfsmount *mnt)
114{
Al Virof21f6222009-06-24 03:12:00 -0400115 int res;
116
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100117 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
118 return -ENOMEM;
119
Al Virof21f6222009-06-24 03:12:00 -0400120 res = ida_get_new_above(&mnt_group_ida,
121 mnt_group_start,
122 &mnt->mnt_group_id);
123 if (!res)
124 mnt_group_start = mnt->mnt_group_id + 1;
125
126 return res;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100127}
128
129/*
130 * Release a peer group ID
131 */
132void mnt_release_group_id(struct vfsmount *mnt)
133{
Al Virof21f6222009-06-24 03:12:00 -0400134 int id = mnt->mnt_group_id;
135 ida_remove(&mnt_group_ida, id);
136 if (mnt_group_start > id)
137 mnt_group_start = id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100138 mnt->mnt_group_id = 0;
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141struct vfsmount *alloc_vfsmnt(const char *name)
142{
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800143 struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100145 int err;
146
147 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800148 if (err)
149 goto out_free_cache;
150
151 if (name) {
152 mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
153 if (!mnt->mnt_devname)
154 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100155 }
156
Ram Paib58fed82005-11-07 17:16:09 -0500157 atomic_set(&mnt->mnt_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 INIT_LIST_HEAD(&mnt->mnt_hash);
159 INIT_LIST_HEAD(&mnt->mnt_child);
160 INIT_LIST_HEAD(&mnt->mnt_mounts);
161 INIT_LIST_HEAD(&mnt->mnt_list);
Miklos Szeredi55e700b2005-07-07 17:57:30 -0700162 INIT_LIST_HEAD(&mnt->mnt_expire);
Ram Pai03e06e62005-11-07 17:19:33 -0500163 INIT_LIST_HEAD(&mnt->mnt_share);
Ram Paia58b0eb2005-11-07 17:20:48 -0500164 INIT_LIST_HEAD(&mnt->mnt_slave_list);
165 INIT_LIST_HEAD(&mnt->mnt_slave);
Andreas Gruenbacher2504c5d2009-12-17 21:24:27 -0500166#ifdef CONFIG_FSNOTIFY
167 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
168#endif
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000169#ifdef CONFIG_SMP
170 mnt->mnt_writers = alloc_percpu(int);
171 if (!mnt->mnt_writers)
172 goto out_free_devname;
173#else
174 mnt->mnt_writers = 0;
175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800178
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000179#ifdef CONFIG_SMP
180out_free_devname:
181 kfree(mnt->mnt_devname);
182#endif
Li Zefan88b38782008-07-21 18:06:36 +0800183out_free_id:
184 mnt_free_id(mnt);
185out_free_cache:
186 kmem_cache_free(mnt_cache, mnt);
187 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Dave Hansen83660252008-02-15 14:37:30 -0800190/*
191 * Most r/o checks on a fs are for operations that take
192 * discrete amounts of time, like a write() or unlink().
193 * We must keep track of when those operations start
194 * (for permission checks) and when they end, so that
195 * we can determine when writes are able to occur to
196 * a filesystem.
197 */
Dave Hansen3d733632008-02-15 14:37:59 -0800198/*
199 * __mnt_is_readonly: check whether a mount is read-only
200 * @mnt: the mount to check for its write status
201 *
202 * This shouldn't be used directly ouside of the VFS.
203 * It does not guarantee that the filesystem will stay
204 * r/w, just that it is right *now*. This can not and
205 * should not be used in place of IS_RDONLY(inode).
206 * mnt_want/drop_write() will _keep_ the filesystem
207 * r/w.
208 */
209int __mnt_is_readonly(struct vfsmount *mnt)
210{
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800211 if (mnt->mnt_flags & MNT_READONLY)
212 return 1;
213 if (mnt->mnt_sb->s_flags & MS_RDONLY)
214 return 1;
215 return 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800216}
217EXPORT_SYMBOL_GPL(__mnt_is_readonly);
218
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000219static inline void inc_mnt_writers(struct vfsmount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800220{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000221#ifdef CONFIG_SMP
222 (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++;
223#else
224 mnt->mnt_writers++;
225#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800226}
Dave Hansen3d733632008-02-15 14:37:59 -0800227
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000228static inline void dec_mnt_writers(struct vfsmount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800229{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000230#ifdef CONFIG_SMP
231 (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--;
232#else
233 mnt->mnt_writers--;
234#endif
235}
236
237static unsigned int count_mnt_writers(struct vfsmount *mnt)
238{
239#ifdef CONFIG_SMP
240 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800241 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800242
243 for_each_possible_cpu(cpu) {
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000244 count += *per_cpu_ptr(mnt->mnt_writers, cpu);
Dave Hansen3d733632008-02-15 14:37:59 -0800245 }
Dave Hansen3d733632008-02-15 14:37:59 -0800246
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000247 return count;
248#else
249 return mnt->mnt_writers;
250#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800251}
252
253/*
254 * Most r/o checks on a fs are for operations that take
255 * discrete amounts of time, like a write() or unlink().
256 * We must keep track of when those operations start
257 * (for permission checks) and when they end, so that
258 * we can determine when writes are able to occur to
259 * a filesystem.
260 */
Dave Hansen83660252008-02-15 14:37:30 -0800261/**
262 * mnt_want_write - get write access to a mount
263 * @mnt: the mount on which to take a write
264 *
265 * This tells the low-level filesystem that a write is
266 * about to be performed to it, and makes sure that
267 * writes are allowed before returning success. When
268 * the write operation is finished, mnt_drop_write()
269 * must be called. This is effectively a refcount.
270 */
271int mnt_want_write(struct vfsmount *mnt)
272{
Dave Hansen3d733632008-02-15 14:37:59 -0800273 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800274
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000275 preempt_disable();
276 inc_mnt_writers(mnt);
277 /*
278 * The store to inc_mnt_writers must be visible before we pass
279 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
280 * incremented count after it has set MNT_WRITE_HOLD.
281 */
282 smp_mb();
283 while (mnt->mnt_flags & MNT_WRITE_HOLD)
284 cpu_relax();
285 /*
286 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
287 * be set to match its requirements. So we must not load that until
288 * MNT_WRITE_HOLD is cleared.
289 */
290 smp_rmb();
Dave Hansen3d733632008-02-15 14:37:59 -0800291 if (__mnt_is_readonly(mnt)) {
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000292 dec_mnt_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800293 ret = -EROFS;
294 goto out;
295 }
Dave Hansen3d733632008-02-15 14:37:59 -0800296out:
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000297 preempt_enable();
Dave Hansen3d733632008-02-15 14:37:59 -0800298 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800299}
300EXPORT_SYMBOL_GPL(mnt_want_write);
301
302/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000303 * mnt_clone_write - get write access to a mount
304 * @mnt: the mount on which to take a write
305 *
306 * This is effectively like mnt_want_write, except
307 * it must only be used to take an extra write reference
308 * on a mountpoint that we already know has a write reference
309 * on it. This allows some optimisation.
310 *
311 * After finished, mnt_drop_write must be called as usual to
312 * drop the reference.
313 */
314int mnt_clone_write(struct vfsmount *mnt)
315{
316 /* superblock may be r/o */
317 if (__mnt_is_readonly(mnt))
318 return -EROFS;
319 preempt_disable();
320 inc_mnt_writers(mnt);
321 preempt_enable();
322 return 0;
323}
324EXPORT_SYMBOL_GPL(mnt_clone_write);
325
326/**
327 * mnt_want_write_file - get write access to a file's mount
328 * @file: the file who's mount on which to take a write
329 *
330 * This is like mnt_want_write, but it takes a file and can
331 * do some optimisations if the file is open for write already
332 */
333int mnt_want_write_file(struct file *file)
334{
OGAWA Hirofumi2d8dd382009-08-06 15:07:39 -0700335 struct inode *inode = file->f_dentry->d_inode;
336 if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
npiggin@suse.de96029c42009-04-26 20:25:55 +1000337 return mnt_want_write(file->f_path.mnt);
338 else
339 return mnt_clone_write(file->f_path.mnt);
340}
341EXPORT_SYMBOL_GPL(mnt_want_write_file);
342
343/**
Dave Hansen83660252008-02-15 14:37:30 -0800344 * mnt_drop_write - give up write access to a mount
345 * @mnt: the mount on which to give up write access
346 *
347 * Tells the low-level filesystem that we are done
348 * performing writes to it. Must be matched with
349 * mnt_want_write() call above.
350 */
351void mnt_drop_write(struct vfsmount *mnt)
352{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000353 preempt_disable();
354 dec_mnt_writers(mnt);
355 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800356}
357EXPORT_SYMBOL_GPL(mnt_drop_write);
358
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800359static int mnt_make_readonly(struct vfsmount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800360{
Dave Hansen3d733632008-02-15 14:37:59 -0800361 int ret = 0;
362
Nick Piggin99b7db72010-08-18 04:37:39 +1000363 br_write_lock(vfsmount_lock);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000364 mnt->mnt_flags |= MNT_WRITE_HOLD;
365 /*
366 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
367 * should be visible before we do.
368 */
369 smp_mb();
370
371 /*
372 * With writers on hold, if this value is zero, then there are
373 * definitely no active writers (although held writers may subsequently
374 * increment the count, they'll have to wait, and decrement it after
375 * seeing MNT_READONLY).
376 *
377 * It is OK to have counter incremented on one CPU and decremented on
378 * another: the sum will add up correctly. The danger would be when we
379 * sum up each counter, if we read a counter before it is incremented,
380 * but then read another CPU's count which it has been subsequently
381 * decremented from -- we would see more decrements than we should.
382 * MNT_WRITE_HOLD protects against this scenario, because
383 * mnt_want_write first increments count, then smp_mb, then spins on
384 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
385 * we're counting up here.
386 */
387 if (count_mnt_writers(mnt) > 0)
388 ret = -EBUSY;
389 else
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800390 mnt->mnt_flags |= MNT_READONLY;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000391 /*
392 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
393 * that become unheld will see MNT_READONLY.
394 */
395 smp_wmb();
396 mnt->mnt_flags &= ~MNT_WRITE_HOLD;
Nick Piggin99b7db72010-08-18 04:37:39 +1000397 br_write_unlock(vfsmount_lock);
Dave Hansen3d733632008-02-15 14:37:59 -0800398 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800399}
Dave Hansen83660252008-02-15 14:37:30 -0800400
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800401static void __mnt_unmake_readonly(struct vfsmount *mnt)
402{
Nick Piggin99b7db72010-08-18 04:37:39 +1000403 br_write_lock(vfsmount_lock);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800404 mnt->mnt_flags &= ~MNT_READONLY;
Nick Piggin99b7db72010-08-18 04:37:39 +1000405 br_write_unlock(vfsmount_lock);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800406}
407
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -0800408void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
David Howells454e2392006-06-23 02:02:57 -0700409{
410 mnt->mnt_sb = sb;
411 mnt->mnt_root = dget(sb->s_root);
David Howells454e2392006-06-23 02:02:57 -0700412}
413
414EXPORT_SYMBOL(simple_set_mnt);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416void free_vfsmnt(struct vfsmount *mnt)
417{
418 kfree(mnt->mnt_devname);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100419 mnt_free_id(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000420#ifdef CONFIG_SMP
421 free_percpu(mnt->mnt_writers);
422#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 kmem_cache_free(mnt_cache, mnt);
424}
425
426/*
Ram Paia05964f2005-11-07 17:20:17 -0500427 * find the first or last mount at @dentry on vfsmount @mnt depending on
428 * @dir. If @dir is set return the first mount else return the last mount.
Nick Piggin99b7db72010-08-18 04:37:39 +1000429 * vfsmount_lock must be held for read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Ram Paia05964f2005-11-07 17:20:17 -0500431struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
432 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Ram Paib58fed82005-11-07 17:16:09 -0500434 struct list_head *head = mount_hashtable + hash(mnt, dentry);
435 struct list_head *tmp = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct vfsmount *p, *found = NULL;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 for (;;) {
Ram Paia05964f2005-11-07 17:20:17 -0500439 tmp = dir ? tmp->next : tmp->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 p = NULL;
441 if (tmp == head)
442 break;
443 p = list_entry(tmp, struct vfsmount, mnt_hash);
444 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
Ram Paia05964f2005-11-07 17:20:17 -0500445 found = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447 }
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return found;
450}
451
Ram Paia05964f2005-11-07 17:20:17 -0500452/*
453 * lookup_mnt increments the ref count before returning
454 * the vfsmount struct.
455 */
Al Viro1c755af2009-04-18 14:06:57 -0400456struct vfsmount *lookup_mnt(struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500457{
458 struct vfsmount *child_mnt;
Nick Piggin99b7db72010-08-18 04:37:39 +1000459
460 br_read_lock(vfsmount_lock);
Al Viro1c755af2009-04-18 14:06:57 -0400461 if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
Ram Paia05964f2005-11-07 17:20:17 -0500462 mntget(child_mnt);
Nick Piggin99b7db72010-08-18 04:37:39 +1000463 br_read_unlock(vfsmount_lock);
Ram Paia05964f2005-11-07 17:20:17 -0500464 return child_mnt;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static inline int check_mnt(struct vfsmount *mnt)
468{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800469 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Nick Piggin99b7db72010-08-18 04:37:39 +1000472/*
473 * vfsmount lock must be held for write
474 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800475static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500476{
477 if (ns) {
478 ns->event = ++event;
479 wake_up_interruptible(&ns->poll);
480 }
481}
482
Nick Piggin99b7db72010-08-18 04:37:39 +1000483/*
484 * vfsmount lock must be held for write
485 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800486static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500487{
488 if (ns && ns->event != event) {
489 ns->event = event;
490 wake_up_interruptible(&ns->poll);
491 }
492}
493
Nick Piggin99b7db72010-08-18 04:37:39 +1000494/*
495 * vfsmount lock must be held for write
496 */
Al Viro1a390682008-03-21 20:48:19 -0400497static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Al Viro1a390682008-03-21 20:48:19 -0400499 old_path->dentry = mnt->mnt_mountpoint;
500 old_path->mnt = mnt->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 mnt->mnt_parent = mnt;
502 mnt->mnt_mountpoint = mnt->mnt_root;
503 list_del_init(&mnt->mnt_child);
504 list_del_init(&mnt->mnt_hash);
Al Viro1a390682008-03-21 20:48:19 -0400505 old_path->dentry->d_mounted--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Nick Piggin99b7db72010-08-18 04:37:39 +1000508/*
509 * vfsmount lock must be held for write
510 */
Ram Paib90fa9a2005-11-07 17:19:50 -0500511void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
512 struct vfsmount *child_mnt)
513{
514 child_mnt->mnt_parent = mntget(mnt);
515 child_mnt->mnt_mountpoint = dget(dentry);
516 dentry->d_mounted++;
517}
518
Nick Piggin99b7db72010-08-18 04:37:39 +1000519/*
520 * vfsmount lock must be held for write
521 */
Al Viro1a390682008-03-21 20:48:19 -0400522static void attach_mnt(struct vfsmount *mnt, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Al Viro1a390682008-03-21 20:48:19 -0400524 mnt_set_mountpoint(path->mnt, path->dentry, mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500525 list_add_tail(&mnt->mnt_hash, mount_hashtable +
Al Viro1a390682008-03-21 20:48:19 -0400526 hash(path->mnt, path->dentry));
527 list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
Ram Paib90fa9a2005-11-07 17:19:50 -0500528}
529
530/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000531 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500532 */
533static void commit_tree(struct vfsmount *mnt)
534{
535 struct vfsmount *parent = mnt->mnt_parent;
536 struct vfsmount *m;
537 LIST_HEAD(head);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800538 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500539
540 BUG_ON(parent == mnt);
541
542 list_add_tail(&head, &mnt->mnt_list);
543 list_for_each_entry(m, &head, mnt_list)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800544 m->mnt_ns = n;
Ram Paib90fa9a2005-11-07 17:19:50 -0500545 list_splice(&head, n->list.prev);
546
547 list_add_tail(&mnt->mnt_hash, mount_hashtable +
548 hash(parent, mnt->mnt_mountpoint));
549 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800550 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
554{
555 struct list_head *next = p->mnt_mounts.next;
556 if (next == &p->mnt_mounts) {
557 while (1) {
558 if (p == root)
559 return NULL;
560 next = p->mnt_child.next;
561 if (next != &p->mnt_parent->mnt_mounts)
562 break;
563 p = p->mnt_parent;
564 }
565 }
566 return list_entry(next, struct vfsmount, mnt_child);
567}
568
Ram Pai9676f0c2005-11-07 17:21:20 -0500569static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
570{
571 struct list_head *prev = p->mnt_mounts.prev;
572 while (prev != &p->mnt_mounts) {
573 p = list_entry(prev, struct vfsmount, mnt_child);
574 prev = p->mnt_mounts.prev;
575 }
576 return p;
577}
578
Ram Pai36341f62005-11-07 17:17:22 -0500579static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
580 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct super_block *sb = old->mnt_sb;
583 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
584
585 if (mnt) {
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100586 if (flag & (CL_SLAVE | CL_PRIVATE))
587 mnt->mnt_group_id = 0; /* not a peer of original */
588 else
589 mnt->mnt_group_id = old->mnt_group_id;
590
591 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
592 int err = mnt_alloc_group_id(mnt);
593 if (err)
594 goto out_free;
595 }
596
Miklos Szeredibe1a16a2010-10-05 12:31:09 +0200597 mnt->mnt_flags = old->mnt_flags & ~MNT_WRITE_HOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 atomic_inc(&sb->s_active);
599 mnt->mnt_sb = sb;
600 mnt->mnt_root = dget(root);
601 mnt->mnt_mountpoint = mnt->mnt_root;
602 mnt->mnt_parent = mnt;
Ram Paib90fa9a2005-11-07 17:19:50 -0500603
Ram Pai5afe0022005-11-07 17:21:01 -0500604 if (flag & CL_SLAVE) {
605 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
606 mnt->mnt_master = old;
607 CLEAR_MNT_SHARED(mnt);
Al Viro8aec0802007-06-07 12:20:32 -0400608 } else if (!(flag & CL_PRIVATE)) {
Al Viro796a6b52010-01-16 13:28:47 -0500609 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
Ram Pai5afe0022005-11-07 17:21:01 -0500610 list_add(&mnt->mnt_share, &old->mnt_share);
611 if (IS_MNT_SLAVE(old))
612 list_add(&mnt->mnt_slave, &old->mnt_slave);
613 mnt->mnt_master = old->mnt_master;
614 }
Ram Paib90fa9a2005-11-07 17:19:50 -0500615 if (flag & CL_MAKE_SHARED)
616 set_mnt_shared(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* stick the duplicate mount on the same expiry list
619 * as the original if that was on one */
Ram Pai36341f62005-11-07 17:17:22 -0500620 if (flag & CL_EXPIRE) {
Ram Pai36341f62005-11-07 17:17:22 -0500621 if (!list_empty(&old->mnt_expire))
622 list_add(&mnt->mnt_expire, &old->mnt_expire);
Ram Pai36341f62005-11-07 17:17:22 -0500623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100626
627 out_free:
628 free_vfsmnt(mnt);
629 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Al Viro7b7b1ac2005-11-07 17:13:39 -0500632static inline void __mntput(struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct super_block *sb = mnt->mnt_sb;
Dave Hansen3d733632008-02-15 14:37:59 -0800635 /*
Dave Hansen3d733632008-02-15 14:37:59 -0800636 * This probably indicates that somebody messed
637 * up a mnt_want/drop_write() pair. If this
638 * happens, the filesystem was probably unable
639 * to make r/w->r/o transitions.
640 */
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000641 /*
642 * atomic_dec_and_lock() used to deal with ->mnt_count decrements
643 * provides barriers, so count_mnt_writers() below is safe. AV
644 */
645 WARN_ON(count_mnt_writers(mnt));
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500646 fsnotify_vfsmount_delete(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 dput(mnt->mnt_root);
648 free_vfsmnt(mnt);
649 deactivate_super(sb);
650}
651
Al Viro7b7b1ac2005-11-07 17:13:39 -0500652void mntput_no_expire(struct vfsmount *mnt)
653{
654repeat:
Nick Piggin99b7db72010-08-18 04:37:39 +1000655 if (atomic_add_unless(&mnt->mnt_count, -1, 1))
656 return;
657 br_write_lock(vfsmount_lock);
658 if (!atomic_dec_and_test(&mnt->mnt_count)) {
659 br_write_unlock(vfsmount_lock);
660 return;
Al Viro7b7b1ac2005-11-07 17:13:39 -0500661 }
Nick Piggin99b7db72010-08-18 04:37:39 +1000662 if (likely(!mnt->mnt_pinned)) {
663 br_write_unlock(vfsmount_lock);
664 __mntput(mnt);
665 return;
666 }
667 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
668 mnt->mnt_pinned = 0;
669 br_write_unlock(vfsmount_lock);
670 acct_auto_close_mnt(mnt);
671 goto repeat;
Al Viro7b7b1ac2005-11-07 17:13:39 -0500672}
Al Viro7b7b1ac2005-11-07 17:13:39 -0500673EXPORT_SYMBOL(mntput_no_expire);
674
675void mnt_pin(struct vfsmount *mnt)
676{
Nick Piggin99b7db72010-08-18 04:37:39 +1000677 br_write_lock(vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500678 mnt->mnt_pinned++;
Nick Piggin99b7db72010-08-18 04:37:39 +1000679 br_write_unlock(vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500680}
681
682EXPORT_SYMBOL(mnt_pin);
683
684void mnt_unpin(struct vfsmount *mnt)
685{
Nick Piggin99b7db72010-08-18 04:37:39 +1000686 br_write_lock(vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500687 if (mnt->mnt_pinned) {
688 atomic_inc(&mnt->mnt_count);
689 mnt->mnt_pinned--;
690 }
Nick Piggin99b7db72010-08-18 04:37:39 +1000691 br_write_unlock(vfsmount_lock);
Al Viro7b7b1ac2005-11-07 17:13:39 -0500692}
693
694EXPORT_SYMBOL(mnt_unpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800696static inline void mangle(struct seq_file *m, const char *s)
697{
698 seq_escape(m, s, " \t\n\\");
699}
700
701/*
702 * Simple .show_options callback for filesystems which don't want to
703 * implement more complex mount option showing.
704 *
705 * See also save_mount_options().
706 */
707int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
708{
Al Viro2a32ceb2009-05-08 16:05:57 -0400709 const char *options;
710
711 rcu_read_lock();
712 options = rcu_dereference(mnt->mnt_sb->s_options);
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800713
714 if (options != NULL && options[0]) {
715 seq_putc(m, ',');
716 mangle(m, options);
717 }
Al Viro2a32ceb2009-05-08 16:05:57 -0400718 rcu_read_unlock();
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800719
720 return 0;
721}
722EXPORT_SYMBOL(generic_show_options);
723
724/*
725 * If filesystem uses generic_show_options(), this function should be
726 * called from the fill_super() callback.
727 *
728 * The .remount_fs callback usually needs to be handled in a special
729 * way, to make sure, that previous options are not overwritten if the
730 * remount fails.
731 *
732 * Also note, that if the filesystem's .remount_fs function doesn't
733 * reset all options to their default value, but changes only newly
734 * given options, then the displayed options will not reflect reality
735 * any more.
736 */
737void save_mount_options(struct super_block *sb, char *options)
738{
Al Viro2a32ceb2009-05-08 16:05:57 -0400739 BUG_ON(sb->s_options);
740 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
Miklos Szeredib3b304a2008-02-08 04:21:35 -0800741}
742EXPORT_SYMBOL(save_mount_options);
743
Al Viro2a32ceb2009-05-08 16:05:57 -0400744void replace_mount_options(struct super_block *sb, char *options)
745{
746 char *old = sb->s_options;
747 rcu_assign_pointer(sb->s_options, options);
748 if (old) {
749 synchronize_rcu();
750 kfree(old);
751 }
752}
753EXPORT_SYMBOL(replace_mount_options);
754
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100755#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/* iterator */
757static void *m_start(struct seq_file *m, loff_t *pos)
758{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100759 struct proc_mounts *p = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Ram Pai390c6842005-11-07 17:17:51 -0500761 down_read(&namespace_sem);
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100762 return seq_list_start(&p->ns->list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765static void *m_next(struct seq_file *m, void *v, loff_t *pos)
766{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100767 struct proc_mounts *p = m->private;
Pavel Emelianovb0765fb2007-07-15 23:39:55 -0700768
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100769 return seq_list_next(v, &p->ns->list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772static void m_stop(struct seq_file *m, void *v)
773{
Ram Pai390c6842005-11-07 17:17:51 -0500774 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Al Viro9f5596a2010-02-05 00:40:25 -0500777int mnt_had_events(struct proc_mounts *p)
778{
779 struct mnt_namespace *ns = p->ns;
780 int res = 0;
781
Nick Piggin99b7db72010-08-18 04:37:39 +1000782 br_read_lock(vfsmount_lock);
Al Viro9f5596a2010-02-05 00:40:25 -0500783 if (p->event != ns->event) {
784 p->event = ns->event;
785 res = 1;
786 }
Nick Piggin99b7db72010-08-18 04:37:39 +1000787 br_read_unlock(vfsmount_lock);
Al Viro9f5596a2010-02-05 00:40:25 -0500788
789 return res;
790}
791
Ram Pai2d4d4862008-03-27 13:06:25 +0100792struct proc_fs_info {
793 int flag;
794 const char *str;
795};
796
Eric Paris2069f452008-07-04 09:47:13 +1000797static int show_sb_opts(struct seq_file *m, struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Ram Pai2d4d4862008-03-27 13:06:25 +0100799 static const struct proc_fs_info fs_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 { MS_SYNCHRONOUS, ",sync" },
801 { MS_DIRSYNC, ",dirsync" },
802 { MS_MANDLOCK, ",mand" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 { 0, NULL }
804 };
Ram Pai2d4d4862008-03-27 13:06:25 +0100805 const struct proc_fs_info *fs_infop;
806
807 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
808 if (sb->s_flags & fs_infop->flag)
809 seq_puts(m, fs_infop->str);
810 }
Eric Paris2069f452008-07-04 09:47:13 +1000811
812 return security_sb_show_options(m, sb);
Ram Pai2d4d4862008-03-27 13:06:25 +0100813}
814
815static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
816{
817 static const struct proc_fs_info mnt_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 { MNT_NOSUID, ",nosuid" },
819 { MNT_NODEV, ",nodev" },
820 { MNT_NOEXEC, ",noexec" },
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -0800821 { MNT_NOATIME, ",noatime" },
822 { MNT_NODIRATIME, ",nodiratime" },
Valerie Henson47ae32d2006-12-13 00:34:34 -0800823 { MNT_RELATIME, ",relatime" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 { 0, NULL }
825 };
Ram Pai2d4d4862008-03-27 13:06:25 +0100826 const struct proc_fs_info *fs_infop;
827
828 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
829 if (mnt->mnt_flags & fs_infop->flag)
830 seq_puts(m, fs_infop->str);
831 }
832}
833
834static void show_type(struct seq_file *m, struct super_block *sb)
835{
836 mangle(m, sb->s_type->name);
837 if (sb->s_subtype && sb->s_subtype[0]) {
838 seq_putc(m, '.');
839 mangle(m, sb->s_subtype);
840 }
841}
842
843static int show_vfsmnt(struct seq_file *m, void *v)
844{
845 struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
846 int err = 0;
Jan Blunckc32c2f62008-02-14 19:38:43 -0800847 struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
850 seq_putc(m, ' ');
Jan Blunckc32c2f62008-02-14 19:38:43 -0800851 seq_path(m, &mnt_path, " \t\n\\");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 seq_putc(m, ' ');
Ram Pai2d4d4862008-03-27 13:06:25 +0100853 show_type(m, mnt->mnt_sb);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800854 seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
Eric Paris2069f452008-07-04 09:47:13 +1000855 err = show_sb_opts(m, mnt->mnt_sb);
856 if (err)
857 goto out;
Ram Pai2d4d4862008-03-27 13:06:25 +0100858 show_mnt_opts(m, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (mnt->mnt_sb->s_op->show_options)
860 err = mnt->mnt_sb->s_op->show_options(m, mnt);
861 seq_puts(m, " 0 0\n");
Eric Paris2069f452008-07-04 09:47:13 +1000862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return err;
864}
865
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100866const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .start = m_start,
868 .next = m_next,
869 .stop = m_stop,
870 .show = show_vfsmnt
871};
872
Ram Pai2d4d4862008-03-27 13:06:25 +0100873static int show_mountinfo(struct seq_file *m, void *v)
874{
875 struct proc_mounts *p = m->private;
876 struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
877 struct super_block *sb = mnt->mnt_sb;
878 struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
879 struct path root = p->root;
880 int err = 0;
881
882 seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
883 MAJOR(sb->s_dev), MINOR(sb->s_dev));
884 seq_dentry(m, mnt->mnt_root, " \t\n\\");
885 seq_putc(m, ' ');
886 seq_path_root(m, &mnt_path, &root, " \t\n\\");
887 if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
888 /*
889 * Mountpoint is outside root, discard that one. Ugly,
890 * but less so than trying to do that in iterator in a
891 * race-free way (due to renames).
892 */
893 return SEQ_SKIP;
894 }
895 seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
896 show_mnt_opts(m, mnt);
897
898 /* Tagged fields ("foo:X" or "bar") */
899 if (IS_MNT_SHARED(mnt))
900 seq_printf(m, " shared:%i", mnt->mnt_group_id);
Miklos Szeredi97e7e0f2008-03-27 13:06:26 +0100901 if (IS_MNT_SLAVE(mnt)) {
902 int master = mnt->mnt_master->mnt_group_id;
903 int dom = get_dominating_id(mnt, &p->root);
904 seq_printf(m, " master:%i", master);
905 if (dom && dom != master)
906 seq_printf(m, " propagate_from:%i", dom);
907 }
Ram Pai2d4d4862008-03-27 13:06:25 +0100908 if (IS_MNT_UNBINDABLE(mnt))
909 seq_puts(m, " unbindable");
910
911 /* Filesystem specific data */
912 seq_puts(m, " - ");
913 show_type(m, sb);
914 seq_putc(m, ' ');
915 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
916 seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
Eric Paris2069f452008-07-04 09:47:13 +1000917 err = show_sb_opts(m, sb);
918 if (err)
919 goto out;
Ram Pai2d4d4862008-03-27 13:06:25 +0100920 if (sb->s_op->show_options)
921 err = sb->s_op->show_options(m, mnt);
922 seq_putc(m, '\n');
Eric Paris2069f452008-07-04 09:47:13 +1000923out:
Ram Pai2d4d4862008-03-27 13:06:25 +0100924 return err;
925}
926
927const struct seq_operations mountinfo_op = {
928 .start = m_start,
929 .next = m_next,
930 .stop = m_stop,
931 .show = show_mountinfo,
932};
933
Chuck Leverb4629fe2006-03-20 13:44:12 -0500934static int show_vfsstat(struct seq_file *m, void *v)
935{
Pavel Emelianovb0765fb2007-07-15 23:39:55 -0700936 struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
Jan Blunckc32c2f62008-02-14 19:38:43 -0800937 struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
Chuck Leverb4629fe2006-03-20 13:44:12 -0500938 int err = 0;
939
940 /* device */
941 if (mnt->mnt_devname) {
942 seq_puts(m, "device ");
943 mangle(m, mnt->mnt_devname);
944 } else
945 seq_puts(m, "no device");
946
947 /* mount point */
948 seq_puts(m, " mounted on ");
Jan Blunckc32c2f62008-02-14 19:38:43 -0800949 seq_path(m, &mnt_path, " \t\n\\");
Chuck Leverb4629fe2006-03-20 13:44:12 -0500950 seq_putc(m, ' ');
951
952 /* file system type */
953 seq_puts(m, "with fstype ");
Ram Pai2d4d4862008-03-27 13:06:25 +0100954 show_type(m, mnt->mnt_sb);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500955
956 /* optional statistics */
957 if (mnt->mnt_sb->s_op->show_stats) {
958 seq_putc(m, ' ');
959 err = mnt->mnt_sb->s_op->show_stats(m, mnt);
960 }
961
962 seq_putc(m, '\n');
963 return err;
964}
965
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100966const struct seq_operations mountstats_op = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500967 .start = m_start,
968 .next = m_next,
969 .stop = m_stop,
970 .show = show_vfsstat,
971};
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100972#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -0500973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974/**
975 * may_umount_tree - check if a mount tree is busy
976 * @mnt: root of mount tree
977 *
978 * This is called to check if a tree of mounts has any
979 * open files, pwds, chroots or sub mounts that are
980 * busy.
981 */
982int may_umount_tree(struct vfsmount *mnt)
983{
Ram Pai36341f62005-11-07 17:17:22 -0500984 int actual_refs = 0;
985 int minimum_refs = 0;
986 struct vfsmount *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Nick Piggin99b7db72010-08-18 04:37:39 +1000988 br_read_lock(vfsmount_lock);
Ram Pai36341f62005-11-07 17:17:22 -0500989 for (p = mnt; p; p = next_mnt(p, mnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 actual_refs += atomic_read(&p->mnt_count);
991 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Nick Piggin99b7db72010-08-18 04:37:39 +1000993 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -0800996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Ian Kente3474a82006-03-27 01:14:51 -0800998 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001EXPORT_SYMBOL(may_umount_tree);
1002
1003/**
1004 * may_umount - check if a mount point is busy
1005 * @mnt: root of mount
1006 *
1007 * This is called to check if a mount point has any
1008 * open files, pwds, chroots or sub mounts. If the
1009 * mount has sub mounts this will return busy
1010 * regardless of whether the sub mounts are busy.
1011 *
1012 * Doesn't take quota and stuff into account. IOW, in some cases it will
1013 * give false negatives. The main reason why it's here is that we need
1014 * a non-destructive way to look for easily umountable filesystems.
1015 */
1016int may_umount(struct vfsmount *mnt)
1017{
Ian Kente3474a82006-03-27 01:14:51 -08001018 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001019 down_read(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10001020 br_read_lock(vfsmount_lock);
Ram Paia05964f2005-11-07 17:20:17 -05001021 if (propagate_mount_busy(mnt, 2))
Ian Kente3474a82006-03-27 01:14:51 -08001022 ret = 0;
Nick Piggin99b7db72010-08-18 04:37:39 +10001023 br_read_unlock(vfsmount_lock);
Al Viro8ad08d82010-01-16 12:56:08 -05001024 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028EXPORT_SYMBOL(may_umount);
1029
Ram Paib90fa9a2005-11-07 17:19:50 -05001030void release_mounts(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Ram Pai70fbcdf2005-11-07 17:17:04 -05001032 struct vfsmount *mnt;
Miklos Szeredibf066c72006-01-08 01:03:19 -08001033 while (!list_empty(head)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001034 mnt = list_first_entry(head, struct vfsmount, mnt_hash);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001035 list_del_init(&mnt->mnt_hash);
1036 if (mnt->mnt_parent != mnt) {
1037 struct dentry *dentry;
1038 struct vfsmount *m;
Nick Piggin99b7db72010-08-18 04:37:39 +10001039
1040 br_write_lock(vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001041 dentry = mnt->mnt_mountpoint;
1042 m = mnt->mnt_parent;
1043 mnt->mnt_mountpoint = mnt->mnt_root;
1044 mnt->mnt_parent = mnt;
Al Viro7c4b93d2008-03-21 23:59:49 -04001045 m->mnt_ghosts--;
Nick Piggin99b7db72010-08-18 04:37:39 +10001046 br_write_unlock(vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001047 dput(dentry);
1048 mntput(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050 mntput(mnt);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001051 }
1052}
1053
Nick Piggin99b7db72010-08-18 04:37:39 +10001054/*
1055 * vfsmount lock must be held for write
1056 * namespace_sem must be held for write
1057 */
Ram Paia05964f2005-11-07 17:20:17 -05001058void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001059{
1060 struct vfsmount *p;
1061
Akinobu Mita1bfba4e2006-06-26 00:24:40 -07001062 for (p = mnt; p; p = next_mnt(p, mnt))
1063 list_move(&p->mnt_hash, kill);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001064
Ram Paia05964f2005-11-07 17:20:17 -05001065 if (propagate)
1066 propagate_umount(kill);
1067
Ram Pai70fbcdf2005-11-07 17:17:04 -05001068 list_for_each_entry(p, kill, mnt_hash) {
1069 list_del_init(&p->mnt_expire);
1070 list_del_init(&p->mnt_list);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08001071 __touch_mnt_namespace(p->mnt_ns);
1072 p->mnt_ns = NULL;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001073 list_del_init(&p->mnt_child);
Al Viro7c4b93d2008-03-21 23:59:49 -04001074 if (p->mnt_parent != p) {
1075 p->mnt_parent->mnt_ghosts++;
Al Virof30ac312006-02-01 07:53:21 -05001076 p->mnt_mountpoint->d_mounted--;
Al Viro7c4b93d2008-03-21 23:59:49 -04001077 }
Ram Paia05964f2005-11-07 17:20:17 -05001078 change_mnt_propagation(p, MS_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080}
1081
Al Viroc35038b2008-03-22 00:46:23 -04001082static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084static int do_umount(struct vfsmount *mnt, int flags)
1085{
Ram Paib58fed82005-11-07 17:16:09 -05001086 struct super_block *sb = mnt->mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int retval;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001088 LIST_HEAD(umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 retval = security_sb_umount(mnt, flags);
1091 if (retval)
1092 return retval;
1093
1094 /*
1095 * Allow userspace to request a mountpoint be expired rather than
1096 * unmounting unconditionally. Unmount only happens if:
1097 * (1) the mark is already set (the mark is cleared by mntput())
1098 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1099 */
1100 if (flags & MNT_EXPIRE) {
Jan Blunck6ac08c32008-02-14 19:34:38 -08001101 if (mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 flags & (MNT_FORCE | MNT_DETACH))
1103 return -EINVAL;
1104
1105 if (atomic_read(&mnt->mnt_count) != 2)
1106 return -EBUSY;
1107
1108 if (!xchg(&mnt->mnt_expiry_mark, 1))
1109 return -EAGAIN;
1110 }
1111
1112 /*
1113 * If we may have to abort operations to get out of this
1114 * mount, and they will themselves hold resources we must
1115 * allow the fs to do things. In the Unix tradition of
1116 * 'Gee thats tricky lets do it in userspace' the umount_begin
1117 * might fail to complete on the first run through as other tasks
1118 * must return, and the like. Thats for the mount program to worry
1119 * about for the moment.
1120 */
1121
Al Viro42faad92008-04-24 07:21:56 -04001122 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001123 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * No sense to grab the lock for this test, but test itself looks
1128 * somewhat bogus. Suggestions for better replacement?
1129 * Ho-hum... In principle, we might treat that as umount + switch
1130 * to rootfs. GC would eventually take care of the old vfsmount.
1131 * Actually it makes sense, especially if rootfs would contain a
1132 * /reboot - static binary that would close all descriptors and
1133 * call reboot(9). Then init(8) could umount root and exec /reboot.
1134 */
Jan Blunck6ac08c32008-02-14 19:34:38 -08001135 if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /*
1137 * Special case for "unmounting" root ...
1138 * we just try to remount it readonly.
1139 */
1140 down_write(&sb->s_umount);
Al Viro4aa98cf2009-05-08 13:36:58 -04001141 if (!(sb->s_flags & MS_RDONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 up_write(&sb->s_umount);
1144 return retval;
1145 }
1146
Ram Pai390c6842005-11-07 17:17:51 -05001147 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10001148 br_write_lock(vfsmount_lock);
Al Viro5addc5d2005-11-07 17:15:49 -05001149 event++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Al Viroc35038b2008-03-22 00:46:23 -04001151 if (!(flags & MNT_DETACH))
1152 shrink_submounts(mnt, &umount_list);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 retval = -EBUSY;
Ram Paia05964f2005-11-07 17:20:17 -05001155 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!list_empty(&mnt->mnt_list))
Ram Paia05964f2005-11-07 17:20:17 -05001157 umount_tree(mnt, 1, &umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 retval = 0;
1159 }
Nick Piggin99b7db72010-08-18 04:37:39 +10001160 br_write_unlock(vfsmount_lock);
Ram Pai390c6842005-11-07 17:17:51 -05001161 up_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001162 release_mounts(&umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return retval;
1164}
1165
1166/*
1167 * Now umount can handle mount points as well as block devices.
1168 * This is important for filesystems which use unnamed block devices.
1169 *
1170 * We now support a flag for forced unmount like the other 'big iron'
1171 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1172 */
1173
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001174SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Al Viro2d8f3032008-07-22 09:59:21 -04001176 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int retval;
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001178 int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001180 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1181 return -EINVAL;
1182
1183 if (!(flags & UMOUNT_NOFOLLOW))
1184 lookup_flags |= LOOKUP_FOLLOW;
1185
1186 retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (retval)
1188 goto out;
1189 retval = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04001190 if (path.dentry != path.mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto dput_and_out;
Al Viro2d8f3032008-07-22 09:59:21 -04001192 if (!check_mnt(path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 goto dput_and_out;
1194
1195 retval = -EPERM;
1196 if (!capable(CAP_SYS_ADMIN))
1197 goto dput_and_out;
1198
Al Viro2d8f3032008-07-22 09:59:21 -04001199 retval = do_umount(path.mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200dput_and_out:
Jan Blunck429731b2008-02-14 19:34:31 -08001201 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Al Viro2d8f3032008-07-22 09:59:21 -04001202 dput(path.dentry);
1203 mntput_no_expire(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204out:
1205 return retval;
1206}
1207
1208#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1209
1210/*
Ram Paib58fed82005-11-07 17:16:09 -05001211 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001213SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Ram Paib58fed82005-11-07 17:16:09 -05001215 return sys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218#endif
1219
Al Viro2d92ab32008-08-02 00:51:11 -04001220static int mount_is_safe(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 if (capable(CAP_SYS_ADMIN))
1223 return 0;
1224 return -EPERM;
1225#ifdef notyet
Al Viro2d92ab32008-08-02 00:51:11 -04001226 if (S_ISLNK(path->dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -EPERM;
Al Viro2d92ab32008-08-02 00:51:11 -04001228 if (path->dentry->d_inode->i_mode & S_ISVTX) {
David Howellsda9592e2008-11-14 10:39:05 +11001229 if (current_uid() != path->dentry->d_inode->i_uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -EPERM;
1231 }
Al Viro2d92ab32008-08-02 00:51:11 -04001232 if (inode_permission(path->dentry->d_inode, MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -EPERM;
1234 return 0;
1235#endif
1236}
1237
Ram Paib90fa9a2005-11-07 17:19:50 -05001238struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001239 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct vfsmount *res, *p, *q, *r, *s;
Al Viro1a390682008-03-21 20:48:19 -04001242 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Ram Pai9676f0c2005-11-07 17:21:20 -05001244 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
1245 return NULL;
1246
Ram Pai36341f62005-11-07 17:17:22 -05001247 res = q = clone_mnt(mnt, dentry, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!q)
1249 goto Enomem;
1250 q->mnt_mountpoint = mnt->mnt_mountpoint;
1251
1252 p = mnt;
Domen Puncerfdadd652005-09-10 00:27:07 -07001253 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001254 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 continue;
1256
1257 for (s = r; s; s = next_mnt(s, r)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001258 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
1259 s = skip_mnt_tree(s);
1260 continue;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 while (p != s->mnt_parent) {
1263 p = p->mnt_parent;
1264 q = q->mnt_parent;
1265 }
1266 p = s;
Al Viro1a390682008-03-21 20:48:19 -04001267 path.mnt = q;
1268 path.dentry = p->mnt_mountpoint;
Ram Pai36341f62005-11-07 17:17:22 -05001269 q = clone_mnt(p, p->mnt_root, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (!q)
1271 goto Enomem;
Nick Piggin99b7db72010-08-18 04:37:39 +10001272 br_write_lock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 list_add_tail(&q->mnt_list, &res->mnt_list);
Al Viro1a390682008-03-21 20:48:19 -04001274 attach_mnt(q, &path);
Nick Piggin99b7db72010-08-18 04:37:39 +10001275 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 }
1278 return res;
Ram Paib58fed82005-11-07 17:16:09 -05001279Enomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (res) {
Ram Pai70fbcdf2005-11-07 17:17:04 -05001281 LIST_HEAD(umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001282 br_write_lock(vfsmount_lock);
Ram Paia05964f2005-11-07 17:20:17 -05001283 umount_tree(res, 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001284 br_write_unlock(vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001285 release_mounts(&umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 return NULL;
1288}
1289
Al Viro589ff872009-04-18 03:28:19 -04001290struct vfsmount *collect_mounts(struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001291{
1292 struct vfsmount *tree;
Al Viro1a60a282008-03-22 16:19:49 -04001293 down_write(&namespace_sem);
Al Viro589ff872009-04-18 03:28:19 -04001294 tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
Al Viro1a60a282008-03-22 16:19:49 -04001295 up_write(&namespace_sem);
Al Viro8aec0802007-06-07 12:20:32 -04001296 return tree;
1297}
1298
1299void drop_collected_mounts(struct vfsmount *mnt)
1300{
1301 LIST_HEAD(umount_list);
Al Viro1a60a282008-03-22 16:19:49 -04001302 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10001303 br_write_lock(vfsmount_lock);
Al Viro8aec0802007-06-07 12:20:32 -04001304 umount_tree(mnt, 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001305 br_write_unlock(vfsmount_lock);
Al Viro1a60a282008-03-22 16:19:49 -04001306 up_write(&namespace_sem);
Al Viro8aec0802007-06-07 12:20:32 -04001307 release_mounts(&umount_list);
1308}
1309
Al Viro1f707132010-01-30 22:51:25 -05001310int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1311 struct vfsmount *root)
1312{
1313 struct vfsmount *mnt;
1314 int res = f(root, arg);
1315 if (res)
1316 return res;
1317 list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
1318 res = f(mnt, arg);
1319 if (res)
1320 return res;
1321 }
1322 return 0;
1323}
1324
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001325static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
1326{
1327 struct vfsmount *p;
1328
1329 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1330 if (p->mnt_group_id && !IS_MNT_SHARED(p))
1331 mnt_release_group_id(p);
1332 }
1333}
1334
1335static int invent_group_ids(struct vfsmount *mnt, bool recurse)
1336{
1337 struct vfsmount *p;
1338
1339 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1340 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1341 int err = mnt_alloc_group_id(p);
1342 if (err) {
1343 cleanup_group_ids(mnt, p);
1344 return err;
1345 }
1346 }
1347 }
1348
1349 return 0;
1350}
1351
Ram Paib90fa9a2005-11-07 17:19:50 -05001352/*
1353 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05001354 * @nd : place the mount tree @source_mnt is attached
1355 * @parent_nd : if non-null, detach the source_mnt from its parent and
1356 * store the parent mount and mountpoint dentry.
1357 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05001358 *
1359 * NOTE: in the table below explains the semantics when a source mount
1360 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05001361 * ---------------------------------------------------------------------------
1362 * | BIND MOUNT OPERATION |
1363 * |**************************************************************************
1364 * | source-->| shared | private | slave | unbindable |
1365 * | dest | | | | |
1366 * | | | | | | |
1367 * | v | | | | |
1368 * |**************************************************************************
1369 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1370 * | | | | | |
1371 * |non-shared| shared (+) | private | slave (*) | invalid |
1372 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05001373 * A bind operation clones the source mount and mounts the clone on the
1374 * destination mount.
1375 *
1376 * (++) the cloned mount is propagated to all the mounts in the propagation
1377 * tree of the destination mount and the cloned mount is added to
1378 * the peer group of the source mount.
1379 * (+) the cloned mount is created under the destination mount and is marked
1380 * as shared. The cloned mount is added to the peer group of the source
1381 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05001382 * (+++) the mount is propagated to all the mounts in the propagation tree
1383 * of the destination mount and the cloned mount is made slave
1384 * of the same master as that of the source mount. The cloned mount
1385 * is marked as 'shared and slave'.
1386 * (*) the cloned mount is made a slave of the same master as that of the
1387 * source mount.
1388 *
Ram Pai9676f0c2005-11-07 17:21:20 -05001389 * ---------------------------------------------------------------------------
1390 * | MOVE MOUNT OPERATION |
1391 * |**************************************************************************
1392 * | source-->| shared | private | slave | unbindable |
1393 * | dest | | | | |
1394 * | | | | | | |
1395 * | v | | | | |
1396 * |**************************************************************************
1397 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1398 * | | | | | |
1399 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1400 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05001401 *
1402 * (+) the mount is moved to the destination. And is then propagated to
1403 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05001404 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05001405 * (+++) the mount is moved to the destination and is then propagated to
1406 * all the mounts belonging to the destination mount's propagation tree.
1407 * the mount is marked as 'shared and slave'.
1408 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05001409 *
1410 * if the source mount is a tree, the operations explained above is
1411 * applied to each mount in the tree.
1412 * Must be called without spinlocks held, since this function can sleep
1413 * in allocations.
1414 */
1415static int attach_recursive_mnt(struct vfsmount *source_mnt,
Al Viro1a390682008-03-21 20:48:19 -04001416 struct path *path, struct path *parent_path)
Ram Paib90fa9a2005-11-07 17:19:50 -05001417{
1418 LIST_HEAD(tree_list);
Al Viro1a390682008-03-21 20:48:19 -04001419 struct vfsmount *dest_mnt = path->mnt;
1420 struct dentry *dest_dentry = path->dentry;
Ram Paib90fa9a2005-11-07 17:19:50 -05001421 struct vfsmount *child, *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001422 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001423
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001424 if (IS_MNT_SHARED(dest_mnt)) {
1425 err = invent_group_ids(source_mnt, true);
1426 if (err)
1427 goto out;
1428 }
1429 err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1430 if (err)
1431 goto out_cleanup_ids;
Ram Paib90fa9a2005-11-07 17:19:50 -05001432
Nick Piggin99b7db72010-08-18 04:37:39 +10001433 br_write_lock(vfsmount_lock);
Al Virodf1a1ad2010-01-16 12:57:40 -05001434
Ram Paib90fa9a2005-11-07 17:19:50 -05001435 if (IS_MNT_SHARED(dest_mnt)) {
1436 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1437 set_mnt_shared(p);
1438 }
Al Viro1a390682008-03-21 20:48:19 -04001439 if (parent_path) {
1440 detach_mnt(source_mnt, parent_path);
1441 attach_mnt(source_mnt, path);
Al Viroe5d67f02009-04-07 12:15:39 -04001442 touch_mnt_namespace(parent_path->mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05001443 } else {
1444 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1445 commit_tree(source_mnt);
1446 }
Ram Paib90fa9a2005-11-07 17:19:50 -05001447
1448 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1449 list_del_init(&child->mnt_hash);
1450 commit_tree(child);
1451 }
Nick Piggin99b7db72010-08-18 04:37:39 +10001452 br_write_unlock(vfsmount_lock);
1453
Ram Paib90fa9a2005-11-07 17:19:50 -05001454 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001455
1456 out_cleanup_ids:
1457 if (IS_MNT_SHARED(dest_mnt))
1458 cleanup_group_ids(source_mnt, NULL);
1459 out:
1460 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05001461}
1462
Al Viro8c3ee422008-03-22 18:00:39 -04001463static int graft_tree(struct vfsmount *mnt, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int err;
1466 if (mnt->mnt_sb->s_flags & MS_NOUSER)
1467 return -EINVAL;
1468
Al Viro8c3ee422008-03-22 18:00:39 -04001469 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
1471 return -ENOTDIR;
1472
1473 err = -ENOENT;
Al Viro8c3ee422008-03-22 18:00:39 -04001474 mutex_lock(&path->dentry->d_inode->i_mutex);
Al Virod83c49f2010-04-30 17:17:09 -04001475 if (cant_mount(path->dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 goto out_unlock;
1477
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04001478 if (!d_unlinked(path->dentry))
Al Viro8c3ee422008-03-22 18:00:39 -04001479 err = attach_recursive_mnt(mnt, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480out_unlock:
Al Viro8c3ee422008-03-22 18:00:39 -04001481 mutex_unlock(&path->dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return err;
1483}
1484
1485/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001486 * Sanity check the flags to change_mnt_propagation.
1487 */
1488
1489static int flags_to_propagation_type(int flags)
1490{
1491 int type = flags & ~MS_REC;
1492
1493 /* Fail if any non-propagation flags are set */
1494 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1495 return 0;
1496 /* Only one propagation flag should be set */
1497 if (!is_power_of_2(type))
1498 return 0;
1499 return type;
1500}
1501
1502/*
Ram Pai07b20882005-11-07 17:19:07 -05001503 * recursively change the type of the mountpoint.
1504 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001505static int do_change_type(struct path *path, int flag)
Ram Pai07b20882005-11-07 17:19:07 -05001506{
Al Viro2d92ab32008-08-02 00:51:11 -04001507 struct vfsmount *m, *mnt = path->mnt;
Ram Pai07b20882005-11-07 17:19:07 -05001508 int recurse = flag & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001509 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001510 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05001511
Miklos Szerediee6f9582007-05-08 00:30:40 -07001512 if (!capable(CAP_SYS_ADMIN))
1513 return -EPERM;
1514
Al Viro2d92ab32008-08-02 00:51:11 -04001515 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05001516 return -EINVAL;
1517
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07001518 type = flags_to_propagation_type(flag);
1519 if (!type)
1520 return -EINVAL;
1521
Ram Pai07b20882005-11-07 17:19:07 -05001522 down_write(&namespace_sem);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001523 if (type == MS_SHARED) {
1524 err = invent_group_ids(mnt, recurse);
1525 if (err)
1526 goto out_unlock;
1527 }
1528
Nick Piggin99b7db72010-08-18 04:37:39 +10001529 br_write_lock(vfsmount_lock);
Ram Pai07b20882005-11-07 17:19:07 -05001530 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
1531 change_mnt_propagation(m, type);
Nick Piggin99b7db72010-08-18 04:37:39 +10001532 br_write_unlock(vfsmount_lock);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001533
1534 out_unlock:
Ram Pai07b20882005-11-07 17:19:07 -05001535 up_write(&namespace_sem);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001536 return err;
Ram Pai07b20882005-11-07 17:19:07 -05001537}
1538
1539/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * do loopback mount.
1541 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001542static int do_loopback(struct path *path, char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08001543 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Al Viro2d92ab32008-08-02 00:51:11 -04001545 struct path old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 struct vfsmount *mnt = NULL;
Al Viro2d92ab32008-08-02 00:51:11 -04001547 int err = mount_is_safe(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (err)
1549 return err;
1550 if (!old_name || !*old_name)
1551 return -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001552 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (err)
1554 return err;
1555
Ram Pai390c6842005-11-07 17:17:51 -05001556 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001558 if (IS_MNT_UNBINDABLE(old_path.mnt))
Jan Blunck4ac91372008-02-14 19:34:32 -08001559 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05001560
Al Viro2d92ab32008-08-02 00:51:11 -04001561 if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
Al Viroccd48bc2005-11-07 17:15:04 -05001562 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Al Viroccd48bc2005-11-07 17:15:04 -05001564 err = -ENOMEM;
1565 if (recurse)
Al Viro2d92ab32008-08-02 00:51:11 -04001566 mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05001567 else
Al Viro2d92ab32008-08-02 00:51:11 -04001568 mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05001569
1570 if (!mnt)
1571 goto out;
1572
Al Viro2d92ab32008-08-02 00:51:11 -04001573 err = graft_tree(mnt, path);
Al Viroccd48bc2005-11-07 17:15:04 -05001574 if (err) {
Ram Pai70fbcdf2005-11-07 17:17:04 -05001575 LIST_HEAD(umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001576
1577 br_write_lock(vfsmount_lock);
Ram Paia05964f2005-11-07 17:20:17 -05001578 umount_tree(mnt, 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10001579 br_write_unlock(vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001580 release_mounts(&umount_list);
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Al Viroccd48bc2005-11-07 17:15:04 -05001583out:
Ram Pai390c6842005-11-07 17:17:51 -05001584 up_write(&namespace_sem);
Al Viro2d92ab32008-08-02 00:51:11 -04001585 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return err;
1587}
1588
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001589static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1590{
1591 int error = 0;
1592 int readonly_request = 0;
1593
1594 if (ms_flags & MS_RDONLY)
1595 readonly_request = 1;
1596 if (readonly_request == __mnt_is_readonly(mnt))
1597 return 0;
1598
1599 if (readonly_request)
1600 error = mnt_make_readonly(mnt);
1601 else
1602 __mnt_unmake_readonly(mnt);
1603 return error;
1604}
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606/*
1607 * change filesystem flags. dir should be a physical root of filesystem.
1608 * If you've mounted a non-root directory somewhere and want to do remount
1609 * on it - tough luck.
1610 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001611static int do_remount(struct path *path, int flags, int mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 void *data)
1613{
1614 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04001615 struct super_block *sb = path->mnt->mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if (!capable(CAP_SYS_ADMIN))
1618 return -EPERM;
1619
Al Viro2d92ab32008-08-02 00:51:11 -04001620 if (!check_mnt(path->mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return -EINVAL;
1622
Al Viro2d92ab32008-08-02 00:51:11 -04001623 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return -EINVAL;
1625
1626 down_write(&sb->s_umount);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001627 if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04001628 err = change_mount_flags(path->mnt, flags);
Al Viro4aa98cf2009-05-08 13:36:58 -04001629 else
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08001630 err = do_remount_sb(sb, flags, data, 0);
Al Viro7b43a792010-01-16 13:01:26 -05001631 if (!err) {
Nick Piggin99b7db72010-08-18 04:37:39 +10001632 br_write_lock(vfsmount_lock);
Valerie Aurora495d6c92010-01-26 14:20:47 -05001633 mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
Al Viro2d92ab32008-08-02 00:51:11 -04001634 path->mnt->mnt_flags = mnt_flags;
Nick Piggin99b7db72010-08-18 04:37:39 +10001635 br_write_unlock(vfsmount_lock);
Al Viro7b43a792010-01-16 13:01:26 -05001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 up_write(&sb->s_umount);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001638 if (!err) {
Nick Piggin99b7db72010-08-18 04:37:39 +10001639 br_write_lock(vfsmount_lock);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001640 touch_mnt_namespace(path->mnt->mnt_ns);
Nick Piggin99b7db72010-08-18 04:37:39 +10001641 br_write_unlock(vfsmount_lock);
Dan Williams0e55a7c2008-09-26 19:01:20 -07001642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return err;
1644}
1645
Ram Pai9676f0c2005-11-07 17:21:20 -05001646static inline int tree_contains_unbindable(struct vfsmount *mnt)
1647{
1648 struct vfsmount *p;
1649 for (p = mnt; p; p = next_mnt(p, mnt)) {
1650 if (IS_MNT_UNBINDABLE(p))
1651 return 1;
1652 }
1653 return 0;
1654}
1655
Al Viro0a0d8a42008-08-02 00:55:27 -04001656static int do_move_mount(struct path *path, char *old_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Al Viro2d92ab32008-08-02 00:51:11 -04001658 struct path old_path, parent_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct vfsmount *p;
1660 int err = 0;
1661 if (!capable(CAP_SYS_ADMIN))
1662 return -EPERM;
1663 if (!old_name || !*old_name)
1664 return -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001665 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (err)
1667 return err;
1668
Ram Pai390c6842005-11-07 17:17:51 -05001669 down_write(&namespace_sem);
Al Viro2d92ab32008-08-02 00:51:11 -04001670 while (d_mountpoint(path->dentry) &&
Al Viro9393bd02009-04-18 13:58:15 -04001671 follow_down(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ;
1673 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001674 if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto out;
1676
1677 err = -ENOENT;
Al Viro2d92ab32008-08-02 00:51:11 -04001678 mutex_lock(&path->dentry->d_inode->i_mutex);
Al Virod83c49f2010-04-30 17:17:09 -04001679 if (cant_mount(path->dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 goto out1;
1681
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04001682 if (d_unlinked(path->dentry))
Ram Pai21444402005-11-07 17:20:03 -05001683 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04001686 if (old_path.dentry != old_path.mnt->mnt_root)
Ram Pai21444402005-11-07 17:20:03 -05001687 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Al Viro2d92ab32008-08-02 00:51:11 -04001689 if (old_path.mnt == old_path.mnt->mnt_parent)
Ram Pai21444402005-11-07 17:20:03 -05001690 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Al Viro2d92ab32008-08-02 00:51:11 -04001692 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1693 S_ISDIR(old_path.dentry->d_inode->i_mode))
Ram Pai21444402005-11-07 17:20:03 -05001694 goto out1;
1695 /*
1696 * Don't move a mount residing in a shared parent.
1697 */
Al Viro2d92ab32008-08-02 00:51:11 -04001698 if (old_path.mnt->mnt_parent &&
1699 IS_MNT_SHARED(old_path.mnt->mnt_parent))
Ram Pai21444402005-11-07 17:20:03 -05001700 goto out1;
Ram Pai9676f0c2005-11-07 17:21:20 -05001701 /*
1702 * Don't move a mount tree containing unbindable mounts to a destination
1703 * mount which is shared.
1704 */
Al Viro2d92ab32008-08-02 00:51:11 -04001705 if (IS_MNT_SHARED(path->mnt) &&
1706 tree_contains_unbindable(old_path.mnt))
Ram Pai9676f0c2005-11-07 17:21:20 -05001707 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 err = -ELOOP;
Al Viro2d92ab32008-08-02 00:51:11 -04001709 for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent)
1710 if (p == old_path.mnt)
Ram Pai21444402005-11-07 17:20:03 -05001711 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Al Viro2d92ab32008-08-02 00:51:11 -04001713 err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
Jan Blunck4ac91372008-02-14 19:34:32 -08001714 if (err)
Ram Pai21444402005-11-07 17:20:03 -05001715 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /* if the mount is moved, it should no longer be expire
1718 * automatically */
Al Viro2d92ab32008-08-02 00:51:11 -04001719 list_del_init(&old_path.mnt->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720out1:
Al Viro2d92ab32008-08-02 00:51:11 -04001721 mutex_unlock(&path->dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722out:
Ram Pai390c6842005-11-07 17:17:51 -05001723 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (!err)
Al Viro1a390682008-03-21 20:48:19 -04001725 path_put(&parent_path);
Al Viro2d92ab32008-08-02 00:51:11 -04001726 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return err;
1728}
1729
1730/*
1731 * create a new mount for userspace and request it to be added into the
1732 * namespace's tree
1733 */
Al Viro0a0d8a42008-08-02 00:55:27 -04001734static int do_new_mount(struct path *path, char *type, int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 int mnt_flags, char *name, void *data)
1736{
1737 struct vfsmount *mnt;
1738
Vegard Nossumeca6f532009-09-18 13:05:45 -07001739 if (!type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return -EINVAL;
1741
1742 /* we need capabilities... */
1743 if (!capable(CAP_SYS_ADMIN))
1744 return -EPERM;
1745
1746 mnt = do_kern_mount(type, flags, name, data);
1747 if (IS_ERR(mnt))
1748 return PTR_ERR(mnt);
1749
Al Viro2d92ab32008-08-02 00:51:11 -04001750 return do_add_mount(mnt, path, mnt_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753/*
1754 * add a mount into a namespace's mount tree
1755 * - provide the option of adding the new mount to an expiration list
1756 */
Al Viro8d66bf52008-08-01 09:05:54 -04001757int do_add_mount(struct vfsmount *newmnt, struct path *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int mnt_flags, struct list_head *fslist)
1759{
1760 int err;
1761
Al Viro80893522010-02-05 09:30:46 -05001762 mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
Al Viro27d55f12010-01-16 13:07:36 -05001763
Ram Pai390c6842005-11-07 17:17:51 -05001764 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /* Something was mounted here while we slept */
Al Viro8d66bf52008-08-01 09:05:54 -04001766 while (d_mountpoint(path->dentry) &&
Al Viro9393bd02009-04-18 13:58:15 -04001767 follow_down(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 ;
1769 err = -EINVAL;
Al Virodd5cae62009-04-07 12:21:18 -04001770 if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 goto unlock;
1772
1773 /* Refuse the same filesystem on the same mount point */
1774 err = -EBUSY;
Al Viro8d66bf52008-08-01 09:05:54 -04001775 if (path->mnt->mnt_sb == newmnt->mnt_sb &&
1776 path->mnt->mnt_root == path->dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto unlock;
1778
1779 err = -EINVAL;
1780 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1781 goto unlock;
1782
1783 newmnt->mnt_flags = mnt_flags;
Al Viro8d66bf52008-08-01 09:05:54 -04001784 if ((err = graft_tree(newmnt, path)))
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05001785 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Al Viro6758f952008-03-22 16:14:30 -04001787 if (fslist) /* add to the specified expiration list */
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001788 list_add_tail(&newmnt->mnt_expire, fslist);
Al Viro6758f952008-03-22 16:14:30 -04001789
Ram Pai390c6842005-11-07 17:17:51 -05001790 up_write(&namespace_sem);
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05001791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793unlock:
Ram Pai390c6842005-11-07 17:17:51 -05001794 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 mntput(newmnt);
1796 return err;
1797}
1798
1799EXPORT_SYMBOL_GPL(do_add_mount);
1800
Trond Myklebust5528f9112006-06-09 09:34:17 -04001801/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 * process a list of expirable mountpoints with the intent of discarding any
1803 * mountpoints that aren't in use and haven't been touched since last we came
1804 * here
1805 */
1806void mark_mounts_for_expiry(struct list_head *mounts)
1807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct vfsmount *mnt, *next;
1809 LIST_HEAD(graveyard);
Al Virobcc5c7d2008-03-22 00:21:53 -04001810 LIST_HEAD(umounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 if (list_empty(mounts))
1813 return;
1814
Al Virobcc5c7d2008-03-22 00:21:53 -04001815 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10001816 br_write_lock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 /* extract from the expiration list every vfsmount that matches the
1819 * following criteria:
1820 * - only referenced by its parent vfsmount
1821 * - still marked for expiry (marked on the last call here; marks are
1822 * cleared by mntput())
1823 */
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001824 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Virobcc5c7d2008-03-22 00:21:53 -04001826 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 continue;
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001828 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Al Virobcc5c7d2008-03-22 00:21:53 -04001830 while (!list_empty(&graveyard)) {
1831 mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1832 touch_mnt_namespace(mnt->mnt_ns);
1833 umount_tree(mnt, 1, &umounts);
1834 }
Nick Piggin99b7db72010-08-18 04:37:39 +10001835 br_write_unlock(vfsmount_lock);
Al Virobcc5c7d2008-03-22 00:21:53 -04001836 up_write(&namespace_sem);
1837
1838 release_mounts(&umounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
1841EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1842
1843/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04001844 * Ripoff of 'select_parent()'
1845 *
1846 * search the list of submounts for a given mountpoint, and move any
1847 * shrinkable submounts to the 'graveyard' list.
1848 */
1849static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
1850{
1851 struct vfsmount *this_parent = parent;
1852 struct list_head *next;
1853 int found = 0;
1854
1855repeat:
1856 next = this_parent->mnt_mounts.next;
1857resume:
1858 while (next != &this_parent->mnt_mounts) {
1859 struct list_head *tmp = next;
1860 struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
1861
1862 next = tmp->next;
1863 if (!(mnt->mnt_flags & MNT_SHRINKABLE))
1864 continue;
1865 /*
1866 * Descend a level if the d_mounts list is non-empty.
1867 */
1868 if (!list_empty(&mnt->mnt_mounts)) {
1869 this_parent = mnt;
1870 goto repeat;
1871 }
1872
1873 if (!propagate_mount_busy(mnt, 1)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04001874 list_move_tail(&mnt->mnt_expire, graveyard);
1875 found++;
1876 }
1877 }
1878 /*
1879 * All done at this level ... ascend and resume the search
1880 */
1881 if (this_parent != parent) {
1882 next = this_parent->mnt_child.next;
1883 this_parent = this_parent->mnt_parent;
1884 goto resume;
1885 }
1886 return found;
1887}
1888
1889/*
1890 * process a list of expirable mountpoints with the intent of discarding any
1891 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10001892 *
1893 * vfsmount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04001894 */
Al Viroc35038b2008-03-22 00:46:23 -04001895static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
Trond Myklebust5528f9112006-06-09 09:34:17 -04001896{
1897 LIST_HEAD(graveyard);
Al Viroc35038b2008-03-22 00:46:23 -04001898 struct vfsmount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04001899
Trond Myklebust5528f9112006-06-09 09:34:17 -04001900 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04001901 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04001902 while (!list_empty(&graveyard)) {
Al Viroc35038b2008-03-22 00:46:23 -04001903 m = list_first_entry(&graveyard, struct vfsmount,
Al Virobcc5c7d2008-03-22 00:21:53 -04001904 mnt_expire);
Eric W. Biedermanafef80b2008-11-12 13:26:54 -08001905 touch_mnt_namespace(m->mnt_ns);
1906 umount_tree(m, 1, umounts);
Al Virobcc5c7d2008-03-22 00:21:53 -04001907 }
1908 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04001909}
1910
Trond Myklebust5528f9112006-06-09 09:34:17 -04001911/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 * Some copy_from_user() implementations do not return the exact number of
1913 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1914 * Note that this function differs from copy_from_user() in that it will oops
1915 * on bad values of `to', rather than returning a short copy.
1916 */
Ram Paib58fed82005-11-07 17:16:09 -05001917static long exact_copy_from_user(void *to, const void __user * from,
1918 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 char *t = to;
1921 const char __user *f = from;
1922 char c;
1923
1924 if (!access_ok(VERIFY_READ, from, n))
1925 return n;
1926
1927 while (n) {
1928 if (__get_user(c, f)) {
1929 memset(t, 0, n);
1930 break;
1931 }
1932 *t++ = c;
1933 f++;
1934 n--;
1935 }
1936 return n;
1937}
1938
Ram Paib58fed82005-11-07 17:16:09 -05001939int copy_mount_options(const void __user * data, unsigned long *where)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 int i;
1942 unsigned long page;
1943 unsigned long size;
Ram Paib58fed82005-11-07 17:16:09 -05001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 *where = 0;
1946 if (!data)
1947 return 0;
1948
1949 if (!(page = __get_free_page(GFP_KERNEL)))
1950 return -ENOMEM;
1951
1952 /* We only care that *some* data at the address the user
1953 * gave us is valid. Just in case, we'll zero
1954 * the remainder of the page.
1955 */
1956 /* copy_from_user cannot cross TASK_SIZE ! */
1957 size = TASK_SIZE - (unsigned long)data;
1958 if (size > PAGE_SIZE)
1959 size = PAGE_SIZE;
1960
1961 i = size - exact_copy_from_user((void *)page, data, size);
1962 if (!i) {
Ram Paib58fed82005-11-07 17:16:09 -05001963 free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return -EFAULT;
1965 }
1966 if (i != PAGE_SIZE)
1967 memset((char *)page + i, 0, PAGE_SIZE - i);
1968 *where = page;
1969 return 0;
1970}
1971
Vegard Nossumeca6f532009-09-18 13:05:45 -07001972int copy_mount_string(const void __user *data, char **where)
1973{
1974 char *tmp;
1975
1976 if (!data) {
1977 *where = NULL;
1978 return 0;
1979 }
1980
1981 tmp = strndup_user(data, PAGE_SIZE);
1982 if (IS_ERR(tmp))
1983 return PTR_ERR(tmp);
1984
1985 *where = tmp;
1986 return 0;
1987}
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989/*
1990 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1991 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1992 *
1993 * data is a (void *) that can point to any structure up to
1994 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1995 * information (or be NULL).
1996 *
1997 * Pre-0.97 versions of mount() didn't have a flags word.
1998 * When the flags word was introduced its top half was required
1999 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2000 * Therefore, if this magic number is present, it carries no information
2001 * and must be discarded.
2002 */
Ram Paib58fed82005-11-07 17:16:09 -05002003long do_mount(char *dev_name, char *dir_name, char *type_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 unsigned long flags, void *data_page)
2005{
Al Viro2d92ab32008-08-02 00:51:11 -04002006 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int retval = 0;
2008 int mnt_flags = 0;
2009
2010 /* Discard magic */
2011 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2012 flags &= ~MS_MGC_MSK;
2013
2014 /* Basic sanity checks */
2015
2016 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2017 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 if (data_page)
2020 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2021
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002022 /* ... and get the mountpoint */
2023 retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2024 if (retval)
2025 return retval;
2026
2027 retval = security_sb_mount(dev_name, &path,
2028 type_page, flags, data_page);
2029 if (retval)
2030 goto dput_out;
2031
Andi Kleen613cbe32009-04-19 18:40:43 +02002032 /* Default to relatime unless overriden */
2033 if (!(flags & MS_NOATIME))
2034 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* Separate the per-mountpoint flags */
2037 if (flags & MS_NOSUID)
2038 mnt_flags |= MNT_NOSUID;
2039 if (flags & MS_NODEV)
2040 mnt_flags |= MNT_NODEV;
2041 if (flags & MS_NOEXEC)
2042 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002043 if (flags & MS_NOATIME)
2044 mnt_flags |= MNT_NOATIME;
2045 if (flags & MS_NODIRATIME)
2046 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00002047 if (flags & MS_STRICTATIME)
2048 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002049 if (flags & MS_RDONLY)
2050 mnt_flags |= MNT_READONLY;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002051
Al Viro7a4dec52010-08-09 12:05:43 -04002052 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
Matthew Garrettd0adde52009-03-26 17:49:56 +00002053 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2054 MS_STRICTATIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (flags & MS_REMOUNT)
Al Viro2d92ab32008-08-02 00:51:11 -04002057 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 data_page);
2059 else if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002060 retval = do_loopback(&path, dev_name, flags & MS_REC);
Ram Pai9676f0c2005-11-07 17:21:20 -05002061 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Al Viro2d92ab32008-08-02 00:51:11 -04002062 retval = do_change_type(&path, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 else if (flags & MS_MOVE)
Al Viro2d92ab32008-08-02 00:51:11 -04002064 retval = do_move_mount(&path, dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 else
Al Viro2d92ab32008-08-02 00:51:11 -04002066 retval = do_new_mount(&path, type_page, flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 dev_name, data_page);
2068dput_out:
Al Viro2d92ab32008-08-02 00:51:11 -04002069 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return retval;
2071}
2072
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002073static struct mnt_namespace *alloc_mnt_ns(void)
2074{
2075 struct mnt_namespace *new_ns;
2076
2077 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2078 if (!new_ns)
2079 return ERR_PTR(-ENOMEM);
2080 atomic_set(&new_ns->count, 1);
2081 new_ns->root = NULL;
2082 INIT_LIST_HEAD(&new_ns->list);
2083 init_waitqueue_head(&new_ns->poll);
2084 new_ns->event = 0;
2085 return new_ns;
2086}
2087
JANAK DESAI741a2952006-02-07 12:59:00 -08002088/*
2089 * Allocate a new namespace structure and populate it with contents
2090 * copied from the namespace of the passed in task structure.
2091 */
Badari Pulavartye3222c42007-05-08 00:25:21 -07002092static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002093 struct fs_struct *fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002095 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04002096 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct vfsmount *p, *q;
2098
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002099 new_ns = alloc_mnt_ns();
2100 if (IS_ERR(new_ns))
2101 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Ram Pai390c6842005-11-07 17:17:51 -05002103 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /* First pass: copy the tree topology */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002105 new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
Ram Pai9676f0c2005-11-07 17:21:20 -05002106 CL_COPY_ALL | CL_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!new_ns->root) {
Ram Pai390c6842005-11-07 17:17:51 -05002108 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 kfree(new_ns);
Julia Lawall5cc4a032008-12-01 14:34:51 -08002110 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Nick Piggin99b7db72010-08-18 04:37:39 +10002112 br_write_lock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10002114 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /*
2117 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2118 * as belonging to new namespace. We have already acquired a private
2119 * fs_struct, so tsk->fs->lock is not needed.
2120 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002121 p = mnt_ns->root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 q = new_ns->root;
2123 while (p) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002124 q->mnt_ns = new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (fs) {
Jan Blunck6ac08c32008-02-14 19:34:38 -08002126 if (p == fs->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 rootmnt = p;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002128 fs->root.mnt = mntget(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
Jan Blunck6ac08c32008-02-14 19:34:38 -08002130 if (p == fs->pwd.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 pwdmnt = p;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002132 fs->pwd.mnt = mntget(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002135 p = next_mnt(p, mnt_ns->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 q = next_mnt(q, new_ns->root);
2137 }
Ram Pai390c6842005-11-07 17:17:51 -05002138 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (rootmnt)
2141 mntput(rootmnt);
2142 if (pwdmnt)
2143 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
JANAK DESAI741a2952006-02-07 12:59:00 -08002145 return new_ns;
2146}
2147
Eric W. Biederman213dd262007-07-15 23:41:15 -07002148struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
Badari Pulavartye3222c42007-05-08 00:25:21 -07002149 struct fs_struct *new_fs)
JANAK DESAI741a2952006-02-07 12:59:00 -08002150{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002151 struct mnt_namespace *new_ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002152
Badari Pulavartye3222c42007-05-08 00:25:21 -07002153 BUG_ON(!ns);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002154 get_mnt_ns(ns);
JANAK DESAI741a2952006-02-07 12:59:00 -08002155
2156 if (!(flags & CLONE_NEWNS))
Badari Pulavartye3222c42007-05-08 00:25:21 -07002157 return ns;
JANAK DESAI741a2952006-02-07 12:59:00 -08002158
Badari Pulavartye3222c42007-05-08 00:25:21 -07002159 new_ns = dup_mnt_ns(ns, new_fs);
JANAK DESAI741a2952006-02-07 12:59:00 -08002160
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002161 put_mnt_ns(ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -07002162 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002165/**
2166 * create_mnt_ns - creates a private namespace and adds a root filesystem
2167 * @mnt: pointer to the new root filesystem mountpoint
2168 */
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002169struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002170{
2171 struct mnt_namespace *new_ns;
2172
2173 new_ns = alloc_mnt_ns();
2174 if (!IS_ERR(new_ns)) {
2175 mnt->mnt_ns = new_ns;
2176 new_ns->root = mnt;
2177 list_add(&new_ns->list, &new_ns->root->mnt_list);
2178 }
2179 return new_ns;
2180}
Linus Torvaldsa2770d82009-12-17 12:51:05 -08002181EXPORT_SYMBOL(create_mnt_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002182
Heiko Carstensbdc480e2009-01-14 14:14:12 +01002183SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2184 char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Vegard Nossumeca6f532009-09-18 13:05:45 -07002186 int ret;
2187 char *kernel_type;
2188 char *kernel_dir;
2189 char *kernel_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 unsigned long data_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Vegard Nossumeca6f532009-09-18 13:05:45 -07002192 ret = copy_mount_string(type, &kernel_type);
2193 if (ret < 0)
2194 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Vegard Nossumeca6f532009-09-18 13:05:45 -07002196 kernel_dir = getname(dir_name);
2197 if (IS_ERR(kernel_dir)) {
2198 ret = PTR_ERR(kernel_dir);
2199 goto out_dir;
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Vegard Nossumeca6f532009-09-18 13:05:45 -07002202 ret = copy_mount_string(dev_name, &kernel_dev);
2203 if (ret < 0)
2204 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Vegard Nossumeca6f532009-09-18 13:05:45 -07002206 ret = copy_mount_options(data, &data_page);
2207 if (ret < 0)
2208 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Vegard Nossumeca6f532009-09-18 13:05:45 -07002210 ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
2211 (void *) data_page);
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 free_page(data_page);
Vegard Nossumeca6f532009-09-18 13:05:45 -07002214out_data:
2215 kfree(kernel_dev);
2216out_dev:
2217 putname(kernel_dir);
2218out_dir:
2219 kfree(kernel_type);
2220out_type:
2221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
2224/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 * pivot_root Semantics:
2226 * Moves the root file system of the current process to the directory put_old,
2227 * makes new_root as the new root file system of the current process, and sets
2228 * root/cwd of all processes which had them on the current root to new_root.
2229 *
2230 * Restrictions:
2231 * The new_root and put_old must be directories, and must not be on the
2232 * same file system as the current process root. The put_old must be
2233 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2234 * pointed to by put_old must yield the same directory as new_root. No other
2235 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2236 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08002237 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2238 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2239 * in this situation.
2240 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 * Notes:
2242 * - we don't move root/cwd if they are not at the root (reason: if something
2243 * cared enough to change them, it's probably wrong to force them elsewhere)
2244 * - it's okay to pick a root that isn't the root of a file system, e.g.
2245 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2246 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2247 * first.
2248 */
Heiko Carstens3480b252009-01-14 14:14:16 +01002249SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2250 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 struct vfsmount *tmp;
Al Viro2d8f3032008-07-22 09:59:21 -04002253 struct path new, old, parent_path, root_parent, root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int error;
2255
2256 if (!capable(CAP_SYS_ADMIN))
2257 return -EPERM;
2258
Al Viro2d8f3032008-07-22 09:59:21 -04002259 error = user_path_dir(new_root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (error)
2261 goto out0;
2262 error = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04002263 if (!check_mnt(new.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 goto out1;
2265
Al Viro2d8f3032008-07-22 09:59:21 -04002266 error = user_path_dir(put_old, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (error)
2268 goto out1;
2269
Al Viro2d8f3032008-07-22 09:59:21 -04002270 error = security_sb_pivotroot(&old, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (error) {
Al Viro2d8f3032008-07-22 09:59:21 -04002272 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 goto out1;
2274 }
2275
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002276 get_fs_root(current->fs, &root);
Ram Pai390c6842005-11-07 17:17:51 -05002277 down_write(&namespace_sem);
Al Viro2d8f3032008-07-22 09:59:21 -04002278 mutex_lock(&old.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 error = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04002280 if (IS_MNT_SHARED(old.mnt) ||
2281 IS_MNT_SHARED(new.mnt->mnt_parent) ||
Al Viro8c3ee422008-03-22 18:00:39 -04002282 IS_MNT_SHARED(root.mnt->mnt_parent))
Ram Pai21444402005-11-07 17:20:03 -05002283 goto out2;
Al Viro8c3ee422008-03-22 18:00:39 -04002284 if (!check_mnt(root.mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto out2;
2286 error = -ENOENT;
Al Virod83c49f2010-04-30 17:17:09 -04002287 if (cant_mount(old.dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto out2;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002289 if (d_unlinked(new.dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto out2;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002291 if (d_unlinked(old.dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto out2;
2293 error = -EBUSY;
Al Viro2d8f3032008-07-22 09:59:21 -04002294 if (new.mnt == root.mnt ||
2295 old.mnt == root.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto out2; /* loop, on the same file system */
2297 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04002298 if (root.mnt->mnt_root != root.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto out2; /* not a mountpoint */
Al Viro8c3ee422008-03-22 18:00:39 -04002300 if (root.mnt->mnt_parent == root.mnt)
Miklos Szeredi0bb6fcc2005-09-06 15:19:36 -07002301 goto out2; /* not attached */
Al Viro2d8f3032008-07-22 09:59:21 -04002302 if (new.mnt->mnt_root != new.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto out2; /* not a mountpoint */
Al Viro2d8f3032008-07-22 09:59:21 -04002304 if (new.mnt->mnt_parent == new.mnt)
Miklos Szeredi0bb6fcc2005-09-06 15:19:36 -07002305 goto out2; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08002306 /* make sure we can reach put_old from new_root */
Al Viro2d8f3032008-07-22 09:59:21 -04002307 tmp = old.mnt;
Nick Piggin99b7db72010-08-18 04:37:39 +10002308 br_write_lock(vfsmount_lock);
Al Viro2d8f3032008-07-22 09:59:21 -04002309 if (tmp != new.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 for (;;) {
2311 if (tmp->mnt_parent == tmp)
2312 goto out3; /* already mounted on put_old */
Al Viro2d8f3032008-07-22 09:59:21 -04002313 if (tmp->mnt_parent == new.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 break;
2315 tmp = tmp->mnt_parent;
2316 }
Al Viro2d8f3032008-07-22 09:59:21 -04002317 if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 goto out3;
Al Viro2d8f3032008-07-22 09:59:21 -04002319 } else if (!is_subdir(old.dentry, new.dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 goto out3;
Al Viro2d8f3032008-07-22 09:59:21 -04002321 detach_mnt(new.mnt, &parent_path);
Al Viro8c3ee422008-03-22 18:00:39 -04002322 detach_mnt(root.mnt, &root_parent);
Jan Blunck4ac91372008-02-14 19:34:32 -08002323 /* mount old root on put_old */
Al Viro2d8f3032008-07-22 09:59:21 -04002324 attach_mnt(root.mnt, &old);
Jan Blunck4ac91372008-02-14 19:34:32 -08002325 /* mount new_root on / */
Al Viro2d8f3032008-07-22 09:59:21 -04002326 attach_mnt(new.mnt, &root_parent);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002327 touch_mnt_namespace(current->nsproxy->mnt_ns);
Nick Piggin99b7db72010-08-18 04:37:39 +10002328 br_write_unlock(vfsmount_lock);
Al Viro2d8f3032008-07-22 09:59:21 -04002329 chroot_fs_refs(&root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 error = 0;
Al Viro1a390682008-03-21 20:48:19 -04002331 path_put(&root_parent);
2332 path_put(&parent_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333out2:
Al Viro2d8f3032008-07-22 09:59:21 -04002334 mutex_unlock(&old.dentry->d_inode->i_mutex);
Ram Pai390c6842005-11-07 17:17:51 -05002335 up_write(&namespace_sem);
Al Viro8c3ee422008-03-22 18:00:39 -04002336 path_put(&root);
Al Viro2d8f3032008-07-22 09:59:21 -04002337 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338out1:
Al Viro2d8f3032008-07-22 09:59:21 -04002339 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return error;
2342out3:
Nick Piggin99b7db72010-08-18 04:37:39 +10002343 br_write_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 goto out2;
2345}
2346
2347static void __init init_mount_tree(void)
2348{
2349 struct vfsmount *mnt;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002350 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08002351 struct path root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
2354 if (IS_ERR(mnt))
2355 panic("Can't create rootfs");
Trond Myklebust3b22edc2009-06-23 17:29:49 -04002356 ns = create_mnt_ns(mnt);
2357 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 panic("Can't allocate initial namespace");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002360 init_task.nsproxy->mnt_ns = ns;
2361 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Jan Blunckac748a02008-02-14 19:34:39 -08002363 root.mnt = ns->root;
2364 root.dentry = ns->root->mnt_root;
2365
2366 set_fs_pwd(current->fs, &root);
2367 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
Denis Cheng74bf17c2007-10-16 23:26:30 -07002370void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Eric Dumazet13f14b42008-02-06 01:37:57 -08002372 unsigned u;
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002373 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Ram Pai390c6842005-11-07 17:17:51 -05002375 init_rwsem(&namespace_sem);
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
Paul Mundt20c2df82007-07-20 10:11:58 +09002378 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Ram Paib58fed82005-11-07 17:16:09 -05002380 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (!mount_hashtable)
2383 panic("Failed to allocate mount hash table\n");
2384
Eric Dumazet13f14b42008-02-06 01:37:57 -08002385 printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Eric Dumazet13f14b42008-02-06 01:37:57 -08002387 for (u = 0; u < HASH_SIZE; u++)
2388 INIT_LIST_HEAD(&mount_hashtable[u]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Nick Piggin99b7db72010-08-18 04:37:39 +10002390 br_lock_init(vfsmount_lock);
2391
Randy Dunlap15a67dd2006-09-29 01:58:57 -07002392 err = sysfs_init();
2393 if (err)
2394 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002395 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06002396 fs_kobj = kobject_create_and_add("fs", NULL);
2397 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07002398 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 init_rootfs();
2400 init_mount_tree();
2401}
2402
Trond Myklebust616511d2009-06-22 15:09:13 -04002403void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Ram Pai70fbcdf2005-11-07 17:17:04 -05002405 LIST_HEAD(umount_list);
Trond Myklebust616511d2009-06-22 15:09:13 -04002406
Al Virod498b252010-02-05 02:21:06 -05002407 if (!atomic_dec_and_test(&ns->count))
Trond Myklebust616511d2009-06-22 15:09:13 -04002408 return;
Ram Pai390c6842005-11-07 17:17:51 -05002409 down_write(&namespace_sem);
Nick Piggin99b7db72010-08-18 04:37:39 +10002410 br_write_lock(vfsmount_lock);
Al Virod498b252010-02-05 02:21:06 -05002411 umount_tree(ns->root, 0, &umount_list);
Nick Piggin99b7db72010-08-18 04:37:39 +10002412 br_write_unlock(vfsmount_lock);
Ram Pai390c6842005-11-07 17:17:51 -05002413 up_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -05002414 release_mounts(&umount_list);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002415 kfree(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002417EXPORT_SYMBOL(put_mnt_ns);