1/*
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
11#include <linux/syscalls.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include <linux/spinlock.h>
15#include <linux/percpu.h>
16#include <linux/smp_lock.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/acct.h>
20#include <linux/capability.h>
21#include <linux/cpumask.h>
22#include <linux/module.h>
23#include <linux/sysfs.h>
24#include <linux/seq_file.h>
25#include <linux/mnt_namespace.h>
26#include <linux/namei.h>
27#include <linux/nsproxy.h>
28#include <linux/security.h>
29#include <linux/mount.h>
30#include <linux/ramfs.h>
31#include <linux/log2.h>
32#include <linux/idr.h>
33#include <linux/fs_struct.h>
34#include <linux/fsnotify.h>
35#include <asm/uaccess.h>
36#include <asm/unistd.h>
37#include "pnode.h"
38#include "internal.h"
39
40#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
41#define HASH_SIZE (1UL << HASH_SHIFT)
42
43static int event;
44static DEFINE_IDA(mnt_id_ida);
45static DEFINE_IDA(mnt_group_ida);
46static DEFINE_SPINLOCK(mnt_id_lock);
47static int mnt_id_start = 0;
48static int mnt_group_start = 1;
49
50static struct list_head *mount_hashtable __read_mostly;
51static struct kmem_cache *mnt_cache __read_mostly;
52static struct rw_semaphore namespace_sem;
53
54/* /sys/fs */
55struct kobject *fs_kobj;
56EXPORT_SYMBOL_GPL(fs_kobj);
57
58/*
59 * vfsmount lock may be taken for read to prevent changes to the
60 * vfsmount hash, ie. during mountpoint lookups or walking back
61 * up the tree.
62 *
63 * It should be taken for write in all cases where the vfsmount
64 * tree or hash is modified or when a vfsmount structure is modified.
65 */
66DEFINE_BRLOCK(vfsmount_lock);
67
68static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
69{
70	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
71	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
72	tmp = tmp + (tmp >> HASH_SHIFT);
73	return tmp & (HASH_SIZE - 1);
74}
75
76#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
77
78/*
79 * allocation is serialized by namespace_sem, but we need the spinlock to
80 * serialize with freeing.
81 */
82static int mnt_alloc_id(struct vfsmount *mnt)
83{
84	int res;
85
86retry:
87	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
88	spin_lock(&mnt_id_lock);
89	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
90	if (!res)
91		mnt_id_start = mnt->mnt_id + 1;
92	spin_unlock(&mnt_id_lock);
93	if (res == -EAGAIN)
94		goto retry;
95
96	return res;
97}
98
99static void mnt_free_id(struct vfsmount *mnt)
100{
101	int id = mnt->mnt_id;
102	spin_lock(&mnt_id_lock);
103	ida_remove(&mnt_id_ida, id);
104	if (mnt_id_start > id)
105		mnt_id_start = id;
106	spin_unlock(&mnt_id_lock);
107}
108
109/*
110 * Allocate a new peer group ID
111 *
112 * mnt_group_ida is protected by namespace_sem
113 */
114static int mnt_alloc_group_id(struct vfsmount *mnt)
115{
116	int res;
117
118	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
119		return -ENOMEM;
120
121	res = ida_get_new_above(&mnt_group_ida,
122				mnt_group_start,
123				&mnt->mnt_group_id);
124	if (!res)
125		mnt_group_start = mnt->mnt_group_id + 1;
126
127	return res;
128}
129
130/*
131 * Release a peer group ID
132 */
133void mnt_release_group_id(struct vfsmount *mnt)
134{
135	int id = mnt->mnt_group_id;
136	ida_remove(&mnt_group_ida, id);
137	if (mnt_group_start > id)
138		mnt_group_start = id;
139	mnt->mnt_group_id = 0;
140}
141
142struct vfsmount *alloc_vfsmnt(const char *name)
143{
144	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
145	if (mnt) {
146		int err;
147
148		err = mnt_alloc_id(mnt);
149		if (err)
150			goto out_free_cache;
151
152		if (name) {
153			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
154			if (!mnt->mnt_devname)
155				goto out_free_id;
156		}
157
158		atomic_set(&mnt->mnt_count, 1);
159		INIT_LIST_HEAD(&mnt->mnt_hash);
160		INIT_LIST_HEAD(&mnt->mnt_child);
161		INIT_LIST_HEAD(&mnt->mnt_mounts);
162		INIT_LIST_HEAD(&mnt->mnt_list);
163		INIT_LIST_HEAD(&mnt->mnt_expire);
164		INIT_LIST_HEAD(&mnt->mnt_share);
165		INIT_LIST_HEAD(&mnt->mnt_slave_list);
166		INIT_LIST_HEAD(&mnt->mnt_slave);
167#ifdef CONFIG_FSNOTIFY
168		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
169#endif
170#ifdef CONFIG_SMP
171		mnt->mnt_writers = alloc_percpu(int);
172		if (!mnt->mnt_writers)
173			goto out_free_devname;
174#else
175		mnt->mnt_writers = 0;
176#endif
177	}
178	return mnt;
179
180#ifdef CONFIG_SMP
181out_free_devname:
182	kfree(mnt->mnt_devname);
183#endif
184out_free_id:
185	mnt_free_id(mnt);
186out_free_cache:
187	kmem_cache_free(mnt_cache, mnt);
188	return NULL;
189}
190
191/*
192 * Most r/o checks on a fs are for operations that take
193 * discrete amounts of time, like a write() or unlink().
194 * We must keep track of when those operations start
195 * (for permission checks) and when they end, so that
196 * we can determine when writes are able to occur to
197 * a filesystem.
198 */
199/*
200 * __mnt_is_readonly: check whether a mount is read-only
201 * @mnt: the mount to check for its write status
202 *
203 * This shouldn't be used directly ouside of the VFS.
204 * It does not guarantee that the filesystem will stay
205 * r/w, just that it is right *now*.  This can not and
206 * should not be used in place of IS_RDONLY(inode).
207 * mnt_want/drop_write() will _keep_ the filesystem
208 * r/w.
209 */
210int __mnt_is_readonly(struct vfsmount *mnt)
211{
212	if (mnt->mnt_flags & MNT_READONLY)
213		return 1;
214	if (mnt->mnt_sb->s_flags & MS_RDONLY)
215		return 1;
216	return 0;
217}
218EXPORT_SYMBOL_GPL(__mnt_is_readonly);
219
220static inline void inc_mnt_writers(struct vfsmount *mnt)
221{
222#ifdef CONFIG_SMP
223	(*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++;
224#else
225	mnt->mnt_writers++;
226#endif
227}
228
229static inline void dec_mnt_writers(struct vfsmount *mnt)
230{
231#ifdef CONFIG_SMP
232	(*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--;
233#else
234	mnt->mnt_writers--;
235#endif
236}
237
238static unsigned int count_mnt_writers(struct vfsmount *mnt)
239{
240#ifdef CONFIG_SMP
241	unsigned int count = 0;
242	int cpu;
243
244	for_each_possible_cpu(cpu) {
245		count += *per_cpu_ptr(mnt->mnt_writers, cpu);
246	}
247
248	return count;
249#else
250	return mnt->mnt_writers;
251#endif
252}
253
254/*
255 * Most r/o checks on a fs are for operations that take
256 * discrete amounts of time, like a write() or unlink().
257 * We must keep track of when those operations start
258 * (for permission checks) and when they end, so that
259 * we can determine when writes are able to occur to
260 * a filesystem.
261 */
262/**
263 * mnt_want_write - get write access to a mount
264 * @mnt: the mount on which to take a write
265 *
266 * This tells the low-level filesystem that a write is
267 * about to be performed to it, and makes sure that
268 * writes are allowed before returning success.  When
269 * the write operation is finished, mnt_drop_write()
270 * must be called.  This is effectively a refcount.
271 */
272int mnt_want_write(struct vfsmount *mnt)
273{
274	int ret = 0;
275
276	preempt_disable();
277	inc_mnt_writers(mnt);
278	/*
279	 * The store to inc_mnt_writers must be visible before we pass
280	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
281	 * incremented count after it has set MNT_WRITE_HOLD.
282	 */
283	smp_mb();
284	while (mnt->mnt_flags & MNT_WRITE_HOLD)
285		cpu_relax();
286	/*
287	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
288	 * be set to match its requirements. So we must not load that until
289	 * MNT_WRITE_HOLD is cleared.
290	 */
291	smp_rmb();
292	if (__mnt_is_readonly(mnt)) {
293		dec_mnt_writers(mnt);
294		ret = -EROFS;
295		goto out;
296	}
297out:
298	preempt_enable();
299	return ret;
300}
301EXPORT_SYMBOL_GPL(mnt_want_write);
302
303/**
304 * mnt_clone_write - get write access to a mount
305 * @mnt: the mount on which to take a write
306 *
307 * This is effectively like mnt_want_write, except
308 * it must only be used to take an extra write reference
309 * on a mountpoint that we already know has a write reference
310 * on it. This allows some optimisation.
311 *
312 * After finished, mnt_drop_write must be called as usual to
313 * drop the reference.
314 */
315int mnt_clone_write(struct vfsmount *mnt)
316{
317	/* superblock may be r/o */
318	if (__mnt_is_readonly(mnt))
319		return -EROFS;
320	preempt_disable();
321	inc_mnt_writers(mnt);
322	preempt_enable();
323	return 0;
324}
325EXPORT_SYMBOL_GPL(mnt_clone_write);
326
327/**
328 * mnt_want_write_file - get write access to a file's mount
329 * @file: the file who's mount on which to take a write
330 *
331 * This is like mnt_want_write, but it takes a file and can
332 * do some optimisations if the file is open for write already
333 */
334int mnt_want_write_file(struct file *file)
335{
336	struct inode *inode = file->f_dentry->d_inode;
337	if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
338		return mnt_want_write(file->f_path.mnt);
339	else
340		return mnt_clone_write(file->f_path.mnt);
341}
342EXPORT_SYMBOL_GPL(mnt_want_write_file);
343
344/**
345 * mnt_drop_write - give up write access to a mount
346 * @mnt: the mount on which to give up write access
347 *
348 * Tells the low-level filesystem that we are done
349 * performing writes to it.  Must be matched with
350 * mnt_want_write() call above.
351 */
352void mnt_drop_write(struct vfsmount *mnt)
353{
354	preempt_disable();
355	dec_mnt_writers(mnt);
356	preempt_enable();
357}
358EXPORT_SYMBOL_GPL(mnt_drop_write);
359
360static int mnt_make_readonly(struct vfsmount *mnt)
361{
362	int ret = 0;
363
364	br_write_lock(vfsmount_lock);
365	mnt->mnt_flags |= MNT_WRITE_HOLD;
366	/*
367	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
368	 * should be visible before we do.
369	 */
370	smp_mb();
371
372	/*
373	 * With writers on hold, if this value is zero, then there are
374	 * definitely no active writers (although held writers may subsequently
375	 * increment the count, they'll have to wait, and decrement it after
376	 * seeing MNT_READONLY).
377	 *
378	 * It is OK to have counter incremented on one CPU and decremented on
379	 * another: the sum will add up correctly. The danger would be when we
380	 * sum up each counter, if we read a counter before it is incremented,
381	 * but then read another CPU's count which it has been subsequently
382	 * decremented from -- we would see more decrements than we should.
383	 * MNT_WRITE_HOLD protects against this scenario, because
384	 * mnt_want_write first increments count, then smp_mb, then spins on
385	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
386	 * we're counting up here.
387	 */
388	if (count_mnt_writers(mnt) > 0)
389		ret = -EBUSY;
390	else
391		mnt->mnt_flags |= MNT_READONLY;
392	/*
393	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
394	 * that become unheld will see MNT_READONLY.
395	 */
396	smp_wmb();
397	mnt->mnt_flags &= ~MNT_WRITE_HOLD;
398	br_write_unlock(vfsmount_lock);
399	return ret;
400}
401
402static void __mnt_unmake_readonly(struct vfsmount *mnt)
403{
404	br_write_lock(vfsmount_lock);
405	mnt->mnt_flags &= ~MNT_READONLY;
406	br_write_unlock(vfsmount_lock);
407}
408
409void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
410{
411	mnt->mnt_sb = sb;
412	mnt->mnt_root = dget(sb->s_root);
413}
414
415EXPORT_SYMBOL(simple_set_mnt);
416
417void free_vfsmnt(struct vfsmount *mnt)
418{
419	kfree(mnt->mnt_devname);
420	mnt_free_id(mnt);
421#ifdef CONFIG_SMP
422	free_percpu(mnt->mnt_writers);
423#endif
424	kmem_cache_free(mnt_cache, mnt);
425}
426
427/*
428 * find the first or last mount at @dentry on vfsmount @mnt depending on
429 * @dir. If @dir is set return the first mount else return the last mount.
430 * vfsmount_lock must be held for read or write.
431 */
432struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
433			      int dir)
434{
435	struct list_head *head = mount_hashtable + hash(mnt, dentry);
436	struct list_head *tmp = head;
437	struct vfsmount *p, *found = NULL;
438
439	for (;;) {
440		tmp = dir ? tmp->next : tmp->prev;
441		p = NULL;
442		if (tmp == head)
443			break;
444		p = list_entry(tmp, struct vfsmount, mnt_hash);
445		if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
446			found = p;
447			break;
448		}
449	}
450	return found;
451}
452
453/*
454 * lookup_mnt increments the ref count before returning
455 * the vfsmount struct.
456 */
457struct vfsmount *lookup_mnt(struct path *path)
458{
459	struct vfsmount *child_mnt;
460
461	br_read_lock(vfsmount_lock);
462	if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
463		mntget(child_mnt);
464	br_read_unlock(vfsmount_lock);
465	return child_mnt;
466}
467
468static inline int check_mnt(struct vfsmount *mnt)
469{
470	return mnt->mnt_ns == current->nsproxy->mnt_ns;
471}
472
473/*
474 * vfsmount lock must be held for write
475 */
476static void touch_mnt_namespace(struct mnt_namespace *ns)
477{
478	if (ns) {
479		ns->event = ++event;
480		wake_up_interruptible(&ns->poll);
481	}
482}
483
484/*
485 * vfsmount lock must be held for write
486 */
487static void __touch_mnt_namespace(struct mnt_namespace *ns)
488{
489	if (ns && ns->event != event) {
490		ns->event = event;
491		wake_up_interruptible(&ns->poll);
492	}
493}
494
495/*
496 * vfsmount lock must be held for write
497 */
498static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
499{
500	old_path->dentry = mnt->mnt_mountpoint;
501	old_path->mnt = mnt->mnt_parent;
502	mnt->mnt_parent = mnt;
503	mnt->mnt_mountpoint = mnt->mnt_root;
504	list_del_init(&mnt->mnt_child);
505	list_del_init(&mnt->mnt_hash);
506	old_path->dentry->d_mounted--;
507}
508
509/*
510 * vfsmount lock must be held for write
511 */
512void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
513			struct vfsmount *child_mnt)
514{
515	child_mnt->mnt_parent = mntget(mnt);
516	child_mnt->mnt_mountpoint = dget(dentry);
517	dentry->d_mounted++;
518}
519
520/*
521 * vfsmount lock must be held for write
522 */
523static void attach_mnt(struct vfsmount *mnt, struct path *path)
524{
525	mnt_set_mountpoint(path->mnt, path->dentry, mnt);
526	list_add_tail(&mnt->mnt_hash, mount_hashtable +
527			hash(path->mnt, path->dentry));
528	list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
529}
530
531/*
532 * vfsmount lock must be held for write
533 */
534static void commit_tree(struct vfsmount *mnt)
535{
536	struct vfsmount *parent = mnt->mnt_parent;
537	struct vfsmount *m;
538	LIST_HEAD(head);
539	struct mnt_namespace *n = parent->mnt_ns;
540
541	BUG_ON(parent == mnt);
542
543	list_add_tail(&head, &mnt->mnt_list);
544	list_for_each_entry(m, &head, mnt_list)
545		m->mnt_ns = n;
546	list_splice(&head, n->list.prev);
547
548	list_add_tail(&mnt->mnt_hash, mount_hashtable +
549				hash(parent, mnt->mnt_mountpoint));
550	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
551	touch_mnt_namespace(n);
552}
553
554static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
555{
556	struct list_head *next = p->mnt_mounts.next;
557	if (next == &p->mnt_mounts) {
558		while (1) {
559			if (p == root)
560				return NULL;
561			next = p->mnt_child.next;
562			if (next != &p->mnt_parent->mnt_mounts)
563				break;
564			p = p->mnt_parent;
565		}
566	}
567	return list_entry(next, struct vfsmount, mnt_child);
568}
569
570static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
571{
572	struct list_head *prev = p->mnt_mounts.prev;
573	while (prev != &p->mnt_mounts) {
574		p = list_entry(prev, struct vfsmount, mnt_child);
575		prev = p->mnt_mounts.prev;
576	}
577	return p;
578}
579
580static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
581					int flag)
582{
583	struct super_block *sb = old->mnt_sb;
584	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
585
586	if (mnt) {
587		if (flag & (CL_SLAVE | CL_PRIVATE))
588			mnt->mnt_group_id = 0; /* not a peer of original */
589		else
590			mnt->mnt_group_id = old->mnt_group_id;
591
592		if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
593			int err = mnt_alloc_group_id(mnt);
594			if (err)
595				goto out_free;
596		}
597
598		mnt->mnt_flags = old->mnt_flags;
599		atomic_inc(&sb->s_active);
600		mnt->mnt_sb = sb;
601		mnt->mnt_root = dget(root);
602		mnt->mnt_mountpoint = mnt->mnt_root;
603		mnt->mnt_parent = mnt;
604
605		if (flag & CL_SLAVE) {
606			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
607			mnt->mnt_master = old;
608			CLEAR_MNT_SHARED(mnt);
609		} else if (!(flag & CL_PRIVATE)) {
610			if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
611				list_add(&mnt->mnt_share, &old->mnt_share);
612			if (IS_MNT_SLAVE(old))
613				list_add(&mnt->mnt_slave, &old->mnt_slave);
614			mnt->mnt_master = old->mnt_master;
615		}
616		if (flag & CL_MAKE_SHARED)
617			set_mnt_shared(mnt);
618
619		/* stick the duplicate mount on the same expiry list
620		 * as the original if that was on one */
621		if (flag & CL_EXPIRE) {
622			if (!list_empty(&old->mnt_expire))
623				list_add(&mnt->mnt_expire, &old->mnt_expire);
624		}
625	}
626	return mnt;
627
628 out_free:
629	free_vfsmnt(mnt);
630	return NULL;
631}
632
633static inline void __mntput(struct vfsmount *mnt)
634{
635	struct super_block *sb = mnt->mnt_sb;
636	/*
637	 * This probably indicates that somebody messed
638	 * up a mnt_want/drop_write() pair.  If this
639	 * happens, the filesystem was probably unable
640	 * to make r/w->r/o transitions.
641	 */
642	/*
643	 * atomic_dec_and_lock() used to deal with ->mnt_count decrements
644	 * provides barriers, so count_mnt_writers() below is safe.  AV
645	 */
646	WARN_ON(count_mnt_writers(mnt));
647	fsnotify_vfsmount_delete(mnt);
648	dput(mnt->mnt_root);
649	free_vfsmnt(mnt);
650	deactivate_super(sb);
651}
652
653void mntput_no_expire(struct vfsmount *mnt)
654{
655repeat:
656	if (atomic_add_unless(&mnt->mnt_count, -1, 1))
657		return;
658	br_write_lock(vfsmount_lock);
659	if (!atomic_dec_and_test(&mnt->mnt_count)) {
660		br_write_unlock(vfsmount_lock);
661		return;
662	}
663	if (likely(!mnt->mnt_pinned)) {
664		br_write_unlock(vfsmount_lock);
665		__mntput(mnt);
666		return;
667	}
668	atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
669	mnt->mnt_pinned = 0;
670	br_write_unlock(vfsmount_lock);
671	acct_auto_close_mnt(mnt);
672	goto repeat;
673}
674EXPORT_SYMBOL(mntput_no_expire);
675
676void mnt_pin(struct vfsmount *mnt)
677{
678	br_write_lock(vfsmount_lock);
679	mnt->mnt_pinned++;
680	br_write_unlock(vfsmount_lock);
681}
682
683EXPORT_SYMBOL(mnt_pin);
684
685void mnt_unpin(struct vfsmount *mnt)
686{
687	br_write_lock(vfsmount_lock);
688	if (mnt->mnt_pinned) {
689		atomic_inc(&mnt->mnt_count);
690		mnt->mnt_pinned--;
691	}
692	br_write_unlock(vfsmount_lock);
693}
694
695EXPORT_SYMBOL(mnt_unpin);
696
697static inline void mangle(struct seq_file *m, const char *s)
698{
699	seq_escape(m, s, " \t\n\\");
700}
701
702/*
703 * Simple .show_options callback for filesystems which don't want to
704 * implement more complex mount option showing.
705 *
706 * See also save_mount_options().
707 */
708int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
709{
710	const char *options;
711
712	rcu_read_lock();
713	options = rcu_dereference(mnt->mnt_sb->s_options);
714
715	if (options != NULL && options[0]) {
716		seq_putc(m, ',');
717		mangle(m, options);
718	}
719	rcu_read_unlock();
720
721	return 0;
722}
723EXPORT_SYMBOL(generic_show_options);
724
725/*
726 * If filesystem uses generic_show_options(), this function should be
727 * called from the fill_super() callback.
728 *
729 * The .remount_fs callback usually needs to be handled in a special
730 * way, to make sure, that previous options are not overwritten if the
731 * remount fails.
732 *
733 * Also note, that if the filesystem's .remount_fs function doesn't
734 * reset all options to their default value, but changes only newly
735 * given options, then the displayed options will not reflect reality
736 * any more.
737 */
738void save_mount_options(struct super_block *sb, char *options)
739{
740	BUG_ON(sb->s_options);
741	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
742}
743EXPORT_SYMBOL(save_mount_options);
744
745void replace_mount_options(struct super_block *sb, char *options)
746{
747	char *old = sb->s_options;
748	rcu_assign_pointer(sb->s_options, options);
749	if (old) {
750		synchronize_rcu();
751		kfree(old);
752	}
753}
754EXPORT_SYMBOL(replace_mount_options);
755
756#ifdef CONFIG_PROC_FS
757/* iterator */
758static void *m_start(struct seq_file *m, loff_t *pos)
759{
760	struct proc_mounts *p = m->private;
761
762	down_read(&namespace_sem);
763	return seq_list_start(&p->ns->list, *pos);
764}
765
766static void *m_next(struct seq_file *m, void *v, loff_t *pos)
767{
768	struct proc_mounts *p = m->private;
769
770	return seq_list_next(v, &p->ns->list, pos);
771}
772
773static void m_stop(struct seq_file *m, void *v)
774{
775	up_read(&namespace_sem);
776}
777
778int mnt_had_events(struct proc_mounts *p)
779{
780	struct mnt_namespace *ns = p->ns;
781	int res = 0;
782
783	br_read_lock(vfsmount_lock);
784	if (p->event != ns->event) {
785		p->event = ns->event;
786		res = 1;
787	}
788	br_read_unlock(vfsmount_lock);
789
790	return res;
791}
792
793struct proc_fs_info {
794	int flag;
795	const char *str;
796};
797
798static int show_sb_opts(struct seq_file *m, struct super_block *sb)
799{
800	static const struct proc_fs_info fs_info[] = {
801		{ MS_SYNCHRONOUS, ",sync" },
802		{ MS_DIRSYNC, ",dirsync" },
803		{ MS_MANDLOCK, ",mand" },
804		{ 0, NULL }
805	};
806	const struct proc_fs_info *fs_infop;
807
808	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
809		if (sb->s_flags & fs_infop->flag)
810			seq_puts(m, fs_infop->str);
811	}
812
813	return security_sb_show_options(m, sb);
814}
815
816static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
817{
818	static const struct proc_fs_info mnt_info[] = {
819		{ MNT_NOSUID, ",nosuid" },
820		{ MNT_NODEV, ",nodev" },
821		{ MNT_NOEXEC, ",noexec" },
822		{ MNT_NOATIME, ",noatime" },
823		{ MNT_NODIRATIME, ",nodiratime" },
824		{ MNT_RELATIME, ",relatime" },
825		{ 0, NULL }
826	};
827	const struct proc_fs_info *fs_infop;
828
829	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
830		if (mnt->mnt_flags & fs_infop->flag)
831			seq_puts(m, fs_infop->str);
832	}
833}
834
835static void show_type(struct seq_file *m, struct super_block *sb)
836{
837	mangle(m, sb->s_type->name);
838	if (sb->s_subtype && sb->s_subtype[0]) {
839		seq_putc(m, '.');
840		mangle(m, sb->s_subtype);
841	}
842}
843
844static int show_vfsmnt(struct seq_file *m, void *v)
845{
846	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
847	int err = 0;
848	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
849
850	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
851	seq_putc(m, ' ');
852	seq_path(m, &mnt_path, " \t\n\\");
853	seq_putc(m, ' ');
854	show_type(m, mnt->mnt_sb);
855	seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
856	err = show_sb_opts(m, mnt->mnt_sb);
857	if (err)
858		goto out;
859	show_mnt_opts(m, mnt);
860	if (mnt->mnt_sb->s_op->show_options)
861		err = mnt->mnt_sb->s_op->show_options(m, mnt);
862	seq_puts(m, " 0 0\n");
863out:
864	return err;
865}
866
867const struct seq_operations mounts_op = {
868	.start	= m_start,
869	.next	= m_next,
870	.stop	= m_stop,
871	.show	= show_vfsmnt
872};
873
874static int show_mountinfo(struct seq_file *m, void *v)
875{
876	struct proc_mounts *p = m->private;
877	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
878	struct super_block *sb = mnt->mnt_sb;
879	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
880	struct path root = p->root;
881	int err = 0;
882
883	seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
884		   MAJOR(sb->s_dev), MINOR(sb->s_dev));
885	seq_dentry(m, mnt->mnt_root, " \t\n\\");
886	seq_putc(m, ' ');
887	seq_path_root(m, &mnt_path, &root, " \t\n\\");
888	if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
889		/*
890		 * Mountpoint is outside root, discard that one.  Ugly,
891		 * but less so than trying to do that in iterator in a
892		 * race-free way (due to renames).
893		 */
894		return SEQ_SKIP;
895	}
896	seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
897	show_mnt_opts(m, mnt);
898
899	/* Tagged fields ("foo:X" or "bar") */
900	if (IS_MNT_SHARED(mnt))
901		seq_printf(m, " shared:%i", mnt->mnt_group_id);
902	if (IS_MNT_SLAVE(mnt)) {
903		int master = mnt->mnt_master->mnt_group_id;
904		int dom = get_dominating_id(mnt, &p->root);
905		seq_printf(m, " master:%i", master);
906		if (dom && dom != master)
907			seq_printf(m, " propagate_from:%i", dom);
908	}
909	if (IS_MNT_UNBINDABLE(mnt))
910		seq_puts(m, " unbindable");
911
912	/* Filesystem specific data */
913	seq_puts(m, " - ");
914	show_type(m, sb);
915	seq_putc(m, ' ');
916	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
917	seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
918	err = show_sb_opts(m, sb);
919	if (err)
920		goto out;
921	if (sb->s_op->show_options)
922		err = sb->s_op->show_options(m, mnt);
923	seq_putc(m, '\n');
924out:
925	return err;
926}
927
928const struct seq_operations mountinfo_op = {
929	.start	= m_start,
930	.next	= m_next,
931	.stop	= m_stop,
932	.show	= show_mountinfo,
933};
934
935static int show_vfsstat(struct seq_file *m, void *v)
936{
937	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
938	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
939	int err = 0;
940
941	/* device */
942	if (mnt->mnt_devname) {
943		seq_puts(m, "device ");
944		mangle(m, mnt->mnt_devname);
945	} else
946		seq_puts(m, "no device");
947
948	/* mount point */
949	seq_puts(m, " mounted on ");
950	seq_path(m, &mnt_path, " \t\n\\");
951	seq_putc(m, ' ');
952
953	/* file system type */
954	seq_puts(m, "with fstype ");
955	show_type(m, mnt->mnt_sb);
956
957	/* optional statistics */
958	if (mnt->mnt_sb->s_op->show_stats) {
959		seq_putc(m, ' ');
960		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
961	}
962
963	seq_putc(m, '\n');
964	return err;
965}
966
967const struct seq_operations mountstats_op = {
968	.start	= m_start,
969	.next	= m_next,
970	.stop	= m_stop,
971	.show	= show_vfsstat,
972};
973#endif  /* CONFIG_PROC_FS */
974
975/**
976 * may_umount_tree - check if a mount tree is busy
977 * @mnt: root of mount tree
978 *
979 * This is called to check if a tree of mounts has any
980 * open files, pwds, chroots or sub mounts that are
981 * busy.
982 */
983int may_umount_tree(struct vfsmount *mnt)
984{
985	int actual_refs = 0;
986	int minimum_refs = 0;
987	struct vfsmount *p;
988
989	br_read_lock(vfsmount_lock);
990	for (p = mnt; p; p = next_mnt(p, mnt)) {
991		actual_refs += atomic_read(&p->mnt_count);
992		minimum_refs += 2;
993	}
994	br_read_unlock(vfsmount_lock);
995
996	if (actual_refs > minimum_refs)
997		return 0;
998
999	return 1;
1000}
1001
1002EXPORT_SYMBOL(may_umount_tree);
1003
1004/**
1005 * may_umount - check if a mount point is busy
1006 * @mnt: root of mount
1007 *
1008 * This is called to check if a mount point has any
1009 * open files, pwds, chroots or sub mounts. If the
1010 * mount has sub mounts this will return busy
1011 * regardless of whether the sub mounts are busy.
1012 *
1013 * Doesn't take quota and stuff into account. IOW, in some cases it will
1014 * give false negatives. The main reason why it's here is that we need
1015 * a non-destructive way to look for easily umountable filesystems.
1016 */
1017int may_umount(struct vfsmount *mnt)
1018{
1019	int ret = 1;
1020	down_read(&namespace_sem);
1021	br_read_lock(vfsmount_lock);
1022	if (propagate_mount_busy(mnt, 2))
1023		ret = 0;
1024	br_read_unlock(vfsmount_lock);
1025	up_read(&namespace_sem);
1026	return ret;
1027}
1028
1029EXPORT_SYMBOL(may_umount);
1030
1031void release_mounts(struct list_head *head)
1032{
1033	struct vfsmount *mnt;
1034	while (!list_empty(head)) {
1035		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
1036		list_del_init(&mnt->mnt_hash);
1037		if (mnt->mnt_parent != mnt) {
1038			struct dentry *dentry;
1039			struct vfsmount *m;
1040
1041			br_write_lock(vfsmount_lock);
1042			dentry = mnt->mnt_mountpoint;
1043			m = mnt->mnt_parent;
1044			mnt->mnt_mountpoint = mnt->mnt_root;
1045			mnt->mnt_parent = mnt;
1046			m->mnt_ghosts--;
1047			br_write_unlock(vfsmount_lock);
1048			dput(dentry);
1049			mntput(m);
1050		}
1051		mntput(mnt);
1052	}
1053}
1054
1055/*
1056 * vfsmount lock must be held for write
1057 * namespace_sem must be held for write
1058 */
1059void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
1060{
1061	struct vfsmount *p;
1062
1063	for (p = mnt; p; p = next_mnt(p, mnt))
1064		list_move(&p->mnt_hash, kill);
1065
1066	if (propagate)
1067		propagate_umount(kill);
1068
1069	list_for_each_entry(p, kill, mnt_hash) {
1070		list_del_init(&p->mnt_expire);
1071		list_del_init(&p->mnt_list);
1072		__touch_mnt_namespace(p->mnt_ns);
1073		p->mnt_ns = NULL;
1074		list_del_init(&p->mnt_child);
1075		if (p->mnt_parent != p) {
1076			p->mnt_parent->mnt_ghosts++;
1077			p->mnt_mountpoint->d_mounted--;
1078		}
1079		change_mnt_propagation(p, MS_PRIVATE);
1080	}
1081}
1082
1083static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
1084
1085static int do_umount(struct vfsmount *mnt, int flags)
1086{
1087	struct super_block *sb = mnt->mnt_sb;
1088	int retval;
1089	LIST_HEAD(umount_list);
1090
1091	retval = security_sb_umount(mnt, flags);
1092	if (retval)
1093		return retval;
1094
1095	/*
1096	 * Allow userspace to request a mountpoint be expired rather than
1097	 * unmounting unconditionally. Unmount only happens if:
1098	 *  (1) the mark is already set (the mark is cleared by mntput())
1099	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1100	 */
1101	if (flags & MNT_EXPIRE) {
1102		if (mnt == current->fs->root.mnt ||
1103		    flags & (MNT_FORCE | MNT_DETACH))
1104			return -EINVAL;
1105
1106		if (atomic_read(&mnt->mnt_count) != 2)
1107			return -EBUSY;
1108
1109		if (!xchg(&mnt->mnt_expiry_mark, 1))
1110			return -EAGAIN;
1111	}
1112
1113	/*
1114	 * If we may have to abort operations to get out of this
1115	 * mount, and they will themselves hold resources we must
1116	 * allow the fs to do things. In the Unix tradition of
1117	 * 'Gee thats tricky lets do it in userspace' the umount_begin
1118	 * might fail to complete on the first run through as other tasks
1119	 * must return, and the like. Thats for the mount program to worry
1120	 * about for the moment.
1121	 */
1122
1123	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1124		sb->s_op->umount_begin(sb);
1125	}
1126
1127	/*
1128	 * No sense to grab the lock for this test, but test itself looks
1129	 * somewhat bogus. Suggestions for better replacement?
1130	 * Ho-hum... In principle, we might treat that as umount + switch
1131	 * to rootfs. GC would eventually take care of the old vfsmount.
1132	 * Actually it makes sense, especially if rootfs would contain a
1133	 * /reboot - static binary that would close all descriptors and
1134	 * call reboot(9). Then init(8) could umount root and exec /reboot.
1135	 */
1136	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1137		/*
1138		 * Special case for "unmounting" root ...
1139		 * we just try to remount it readonly.
1140		 */
1141		down_write(&sb->s_umount);
1142		if (!(sb->s_flags & MS_RDONLY))
1143			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
1144		up_write(&sb->s_umount);
1145		return retval;
1146	}
1147
1148	down_write(&namespace_sem);
1149	br_write_lock(vfsmount_lock);
1150	event++;
1151
1152	if (!(flags & MNT_DETACH))
1153		shrink_submounts(mnt, &umount_list);
1154
1155	retval = -EBUSY;
1156	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
1157		if (!list_empty(&mnt->mnt_list))
1158			umount_tree(mnt, 1, &umount_list);
1159		retval = 0;
1160	}
1161	br_write_unlock(vfsmount_lock);
1162	up_write(&namespace_sem);
1163	release_mounts(&umount_list);
1164	return retval;
1165}
1166
1167/*
1168 * Now umount can handle mount points as well as block devices.
1169 * This is important for filesystems which use unnamed block devices.
1170 *
1171 * We now support a flag for forced unmount like the other 'big iron'
1172 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1173 */
1174
1175SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1176{
1177	struct path path;
1178	int retval;
1179	int lookup_flags = 0;
1180
1181	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1182		return -EINVAL;
1183
1184	if (!(flags & UMOUNT_NOFOLLOW))
1185		lookup_flags |= LOOKUP_FOLLOW;
1186
1187	retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
1188	if (retval)
1189		goto out;
1190	retval = -EINVAL;
1191	if (path.dentry != path.mnt->mnt_root)
1192		goto dput_and_out;
1193	if (!check_mnt(path.mnt))
1194		goto dput_and_out;
1195
1196	retval = -EPERM;
1197	if (!capable(CAP_SYS_ADMIN))
1198		goto dput_and_out;
1199
1200	retval = do_umount(path.mnt, flags);
1201dput_and_out:
1202	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
1203	dput(path.dentry);
1204	mntput_no_expire(path.mnt);
1205out:
1206	return retval;
1207}
1208
1209#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1210
1211/*
1212 *	The 2.0 compatible umount. No flags.
1213 */
1214SYSCALL_DEFINE1(oldumount, char __user *, name)
1215{
1216	return sys_umount(name, 0);
1217}
1218
1219#endif
1220
1221static int mount_is_safe(struct path *path)
1222{
1223	if (capable(CAP_SYS_ADMIN))
1224		return 0;
1225	return -EPERM;
1226#ifdef notyet
1227	if (S_ISLNK(path->dentry->d_inode->i_mode))
1228		return -EPERM;
1229	if (path->dentry->d_inode->i_mode & S_ISVTX) {
1230		if (current_uid() != path->dentry->d_inode->i_uid)
1231			return -EPERM;
1232	}
1233	if (inode_permission(path->dentry->d_inode, MAY_WRITE))
1234		return -EPERM;
1235	return 0;
1236#endif
1237}
1238
1239struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
1240					int flag)
1241{
1242	struct vfsmount *res, *p, *q, *r, *s;
1243	struct path path;
1244
1245	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
1246		return NULL;
1247
1248	res = q = clone_mnt(mnt, dentry, flag);
1249	if (!q)
1250		goto Enomem;
1251	q->mnt_mountpoint = mnt->mnt_mountpoint;
1252
1253	p = mnt;
1254	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1255		if (!is_subdir(r->mnt_mountpoint, dentry))
1256			continue;
1257
1258		for (s = r; s; s = next_mnt(s, r)) {
1259			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
1260				s = skip_mnt_tree(s);
1261				continue;
1262			}
1263			while (p != s->mnt_parent) {
1264				p = p->mnt_parent;
1265				q = q->mnt_parent;
1266			}
1267			p = s;
1268			path.mnt = q;
1269			path.dentry = p->mnt_mountpoint;
1270			q = clone_mnt(p, p->mnt_root, flag);
1271			if (!q)
1272				goto Enomem;
1273			br_write_lock(vfsmount_lock);
1274			list_add_tail(&q->mnt_list, &res->mnt_list);
1275			attach_mnt(q, &path);
1276			br_write_unlock(vfsmount_lock);
1277		}
1278	}
1279	return res;
1280Enomem:
1281	if (res) {
1282		LIST_HEAD(umount_list);
1283		br_write_lock(vfsmount_lock);
1284		umount_tree(res, 0, &umount_list);
1285		br_write_unlock(vfsmount_lock);
1286		release_mounts(&umount_list);
1287	}
1288	return NULL;
1289}
1290
1291struct vfsmount *collect_mounts(struct path *path)
1292{
1293	struct vfsmount *tree;
1294	down_write(&namespace_sem);
1295	tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
1296	up_write(&namespace_sem);
1297	return tree;
1298}
1299
1300void drop_collected_mounts(struct vfsmount *mnt)
1301{
1302	LIST_HEAD(umount_list);
1303	down_write(&namespace_sem);
1304	br_write_lock(vfsmount_lock);
1305	umount_tree(mnt, 0, &umount_list);
1306	br_write_unlock(vfsmount_lock);
1307	up_write(&namespace_sem);
1308	release_mounts(&umount_list);
1309}
1310
1311int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1312		   struct vfsmount *root)
1313{
1314	struct vfsmount *mnt;
1315	int res = f(root, arg);
1316	if (res)
1317		return res;
1318	list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
1319		res = f(mnt, arg);
1320		if (res)
1321			return res;
1322	}
1323	return 0;
1324}
1325
1326static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
1327{
1328	struct vfsmount *p;
1329
1330	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1331		if (p->mnt_group_id && !IS_MNT_SHARED(p))
1332			mnt_release_group_id(p);
1333	}
1334}
1335
1336static int invent_group_ids(struct vfsmount *mnt, bool recurse)
1337{
1338	struct vfsmount *p;
1339
1340	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1341		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1342			int err = mnt_alloc_group_id(p);
1343			if (err) {
1344				cleanup_group_ids(mnt, p);
1345				return err;
1346			}
1347		}
1348	}
1349
1350	return 0;
1351}
1352
1353/*
1354 *  @source_mnt : mount tree to be attached
1355 *  @nd         : place the mount tree @source_mnt is attached
1356 *  @parent_nd  : if non-null, detach the source_mnt from its parent and
1357 *  		   store the parent mount and mountpoint dentry.
1358 *  		   (done when source_mnt is moved)
1359 *
1360 *  NOTE: in the table below explains the semantics when a source mount
1361 *  of a given type is attached to a destination mount of a given type.
1362 * ---------------------------------------------------------------------------
1363 * |         BIND MOUNT OPERATION                                            |
1364 * |**************************************************************************
1365 * | source-->| shared        |       private  |       slave    | unbindable |
1366 * | dest     |               |                |                |            |
1367 * |   |      |               |                |                |            |
1368 * |   v      |               |                |                |            |
1369 * |**************************************************************************
1370 * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
1371 * |          |               |                |                |            |
1372 * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
1373 * ***************************************************************************
1374 * A bind operation clones the source mount and mounts the clone on the
1375 * destination mount.
1376 *
1377 * (++)  the cloned mount is propagated to all the mounts in the propagation
1378 * 	 tree of the destination mount and the cloned mount is added to
1379 * 	 the peer group of the source mount.
1380 * (+)   the cloned mount is created under the destination mount and is marked
1381 *       as shared. The cloned mount is added to the peer group of the source
1382 *       mount.
1383 * (+++) the mount is propagated to all the mounts in the propagation tree
1384 *       of the destination mount and the cloned mount is made slave
1385 *       of the same master as that of the source mount. The cloned mount
1386 *       is marked as 'shared and slave'.
1387 * (*)   the cloned mount is made a slave of the same master as that of the
1388 * 	 source mount.
1389 *
1390 * ---------------------------------------------------------------------------
1391 * |         		MOVE MOUNT OPERATION                                 |
1392 * |**************************************************************************
1393 * | source-->| shared        |       private  |       slave    | unbindable |
1394 * | dest     |               |                |                |            |
1395 * |   |      |               |                |                |            |
1396 * |   v      |               |                |                |            |
1397 * |**************************************************************************
1398 * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
1399 * |          |               |                |                |            |
1400 * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
1401 * ***************************************************************************
1402 *
1403 * (+)  the mount is moved to the destination. And is then propagated to
1404 * 	all the mounts in the propagation tree of the destination mount.
1405 * (+*)  the mount is moved to the destination.
1406 * (+++)  the mount is moved to the destination and is then propagated to
1407 * 	all the mounts belonging to the destination mount's propagation tree.
1408 * 	the mount is marked as 'shared and slave'.
1409 * (*)	the mount continues to be a slave at the new location.
1410 *
1411 * if the source mount is a tree, the operations explained above is
1412 * applied to each mount in the tree.
1413 * Must be called without spinlocks held, since this function can sleep
1414 * in allocations.
1415 */
1416static int attach_recursive_mnt(struct vfsmount *source_mnt,
1417			struct path *path, struct path *parent_path)
1418{
1419	LIST_HEAD(tree_list);
1420	struct vfsmount *dest_mnt = path->mnt;
1421	struct dentry *dest_dentry = path->dentry;
1422	struct vfsmount *child, *p;
1423	int err;
1424
1425	if (IS_MNT_SHARED(dest_mnt)) {
1426		err = invent_group_ids(source_mnt, true);
1427		if (err)
1428			goto out;
1429	}
1430	err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1431	if (err)
1432		goto out_cleanup_ids;
1433
1434	br_write_lock(vfsmount_lock);
1435
1436	if (IS_MNT_SHARED(dest_mnt)) {
1437		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1438			set_mnt_shared(p);
1439	}
1440	if (parent_path) {
1441		detach_mnt(source_mnt, parent_path);
1442		attach_mnt(source_mnt, path);
1443		touch_mnt_namespace(parent_path->mnt->mnt_ns);
1444	} else {
1445		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1446		commit_tree(source_mnt);
1447	}
1448
1449	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1450		list_del_init(&child->mnt_hash);
1451		commit_tree(child);
1452	}
1453	br_write_unlock(vfsmount_lock);
1454
1455	return 0;
1456
1457 out_cleanup_ids:
1458	if (IS_MNT_SHARED(dest_mnt))
1459		cleanup_group_ids(source_mnt, NULL);
1460 out:
1461	return err;
1462}
1463
1464static int graft_tree(struct vfsmount *mnt, struct path *path)
1465{
1466	int err;
1467	if (mnt->mnt_sb->s_flags & MS_NOUSER)
1468		return -EINVAL;
1469
1470	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1471	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
1472		return -ENOTDIR;
1473
1474	err = -ENOENT;
1475	mutex_lock(&path->dentry->d_inode->i_mutex);
1476	if (cant_mount(path->dentry))
1477		goto out_unlock;
1478
1479	if (!d_unlinked(path->dentry))
1480		err = attach_recursive_mnt(mnt, path, NULL);
1481out_unlock:
1482	mutex_unlock(&path->dentry->d_inode->i_mutex);
1483	return err;
1484}
1485
1486/*
1487 * Sanity check the flags to change_mnt_propagation.
1488 */
1489
1490static int flags_to_propagation_type(int flags)
1491{
1492	int type = flags & ~MS_REC;
1493
1494	/* Fail if any non-propagation flags are set */
1495	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1496		return 0;
1497	/* Only one propagation flag should be set */
1498	if (!is_power_of_2(type))
1499		return 0;
1500	return type;
1501}
1502
1503/*
1504 * recursively change the type of the mountpoint.
1505 */
1506static int do_change_type(struct path *path, int flag)
1507{
1508	struct vfsmount *m, *mnt = path->mnt;
1509	int recurse = flag & MS_REC;
1510	int type;
1511	int err = 0;
1512
1513	if (!capable(CAP_SYS_ADMIN))
1514		return -EPERM;
1515
1516	if (path->dentry != path->mnt->mnt_root)
1517		return -EINVAL;
1518
1519	type = flags_to_propagation_type(flag);
1520	if (!type)
1521		return -EINVAL;
1522
1523	down_write(&namespace_sem);
1524	if (type == MS_SHARED) {
1525		err = invent_group_ids(mnt, recurse);
1526		if (err)
1527			goto out_unlock;
1528	}
1529
1530	br_write_lock(vfsmount_lock);
1531	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
1532		change_mnt_propagation(m, type);
1533	br_write_unlock(vfsmount_lock);
1534
1535 out_unlock:
1536	up_write(&namespace_sem);
1537	return err;
1538}
1539
1540/*
1541 * do loopback mount.
1542 */
1543static int do_loopback(struct path *path, char *old_name,
1544				int recurse)
1545{
1546	struct path old_path;
1547	struct vfsmount *mnt = NULL;
1548	int err = mount_is_safe(path);
1549	if (err)
1550		return err;
1551	if (!old_name || !*old_name)
1552		return -EINVAL;
1553	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
1554	if (err)
1555		return err;
1556
1557	down_write(&namespace_sem);
1558	err = -EINVAL;
1559	if (IS_MNT_UNBINDABLE(old_path.mnt))
1560		goto out;
1561
1562	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
1563		goto out;
1564
1565	err = -ENOMEM;
1566	if (recurse)
1567		mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
1568	else
1569		mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
1570
1571	if (!mnt)
1572		goto out;
1573
1574	err = graft_tree(mnt, path);
1575	if (err) {
1576		LIST_HEAD(umount_list);
1577
1578		br_write_lock(vfsmount_lock);
1579		umount_tree(mnt, 0, &umount_list);
1580		br_write_unlock(vfsmount_lock);
1581		release_mounts(&umount_list);
1582	}
1583
1584out:
1585	up_write(&namespace_sem);
1586	path_put(&old_path);
1587	return err;
1588}
1589
1590static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1591{
1592	int error = 0;
1593	int readonly_request = 0;
1594
1595	if (ms_flags & MS_RDONLY)
1596		readonly_request = 1;
1597	if (readonly_request == __mnt_is_readonly(mnt))
1598		return 0;
1599
1600	if (readonly_request)
1601		error = mnt_make_readonly(mnt);
1602	else
1603		__mnt_unmake_readonly(mnt);
1604	return error;
1605}
1606
1607/*
1608 * change filesystem flags. dir should be a physical root of filesystem.
1609 * If you've mounted a non-root directory somewhere and want to do remount
1610 * on it - tough luck.
1611 */
1612static int do_remount(struct path *path, int flags, int mnt_flags,
1613		      void *data)
1614{
1615	int err;
1616	struct super_block *sb = path->mnt->mnt_sb;
1617
1618	if (!capable(CAP_SYS_ADMIN))
1619		return -EPERM;
1620
1621	if (!check_mnt(path->mnt))
1622		return -EINVAL;
1623
1624	if (path->dentry != path->mnt->mnt_root)
1625		return -EINVAL;
1626
1627	down_write(&sb->s_umount);
1628	if (flags & MS_BIND)
1629		err = change_mount_flags(path->mnt, flags);
1630	else
1631		err = do_remount_sb(sb, flags, data, 0);
1632	if (!err) {
1633		br_write_lock(vfsmount_lock);
1634		mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
1635		path->mnt->mnt_flags = mnt_flags;
1636		br_write_unlock(vfsmount_lock);
1637	}
1638	up_write(&sb->s_umount);
1639	if (!err) {
1640		br_write_lock(vfsmount_lock);
1641		touch_mnt_namespace(path->mnt->mnt_ns);
1642		br_write_unlock(vfsmount_lock);
1643	}
1644	return err;
1645}
1646
1647static inline int tree_contains_unbindable(struct vfsmount *mnt)
1648{
1649	struct vfsmount *p;
1650	for (p = mnt; p; p = next_mnt(p, mnt)) {
1651		if (IS_MNT_UNBINDABLE(p))
1652			return 1;
1653	}
1654	return 0;
1655}
1656
1657static int do_move_mount(struct path *path, char *old_name)
1658{
1659	struct path old_path, parent_path;
1660	struct vfsmount *p;
1661	int err = 0;
1662	if (!capable(CAP_SYS_ADMIN))
1663		return -EPERM;
1664	if (!old_name || !*old_name)
1665		return -EINVAL;
1666	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
1667	if (err)
1668		return err;
1669
1670	down_write(&namespace_sem);
1671	while (d_mountpoint(path->dentry) &&
1672	       follow_down(path))
1673		;
1674	err = -EINVAL;
1675	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
1676		goto out;
1677
1678	err = -ENOENT;
1679	mutex_lock(&path->dentry->d_inode->i_mutex);
1680	if (cant_mount(path->dentry))
1681		goto out1;
1682
1683	if (d_unlinked(path->dentry))
1684		goto out1;
1685
1686	err = -EINVAL;
1687	if (old_path.dentry != old_path.mnt->mnt_root)
1688		goto out1;
1689
1690	if (old_path.mnt == old_path.mnt->mnt_parent)
1691		goto out1;
1692
1693	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1694	      S_ISDIR(old_path.dentry->d_inode->i_mode))
1695		goto out1;
1696	/*
1697	 * Don't move a mount residing in a shared parent.
1698	 */
1699	if (old_path.mnt->mnt_parent &&
1700	    IS_MNT_SHARED(old_path.mnt->mnt_parent))
1701		goto out1;
1702	/*
1703	 * Don't move a mount tree containing unbindable mounts to a destination
1704	 * mount which is shared.
1705	 */
1706	if (IS_MNT_SHARED(path->mnt) &&
1707	    tree_contains_unbindable(old_path.mnt))
1708		goto out1;
1709	err = -ELOOP;
1710	for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent)
1711		if (p == old_path.mnt)
1712			goto out1;
1713
1714	err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
1715	if (err)
1716		goto out1;
1717
1718	/* if the mount is moved, it should no longer be expire
1719	 * automatically */
1720	list_del_init(&old_path.mnt->mnt_expire);
1721out1:
1722	mutex_unlock(&path->dentry->d_inode->i_mutex);
1723out:
1724	up_write(&namespace_sem);
1725	if (!err)
1726		path_put(&parent_path);
1727	path_put(&old_path);
1728	return err;
1729}
1730
1731/*
1732 * create a new mount for userspace and request it to be added into the
1733 * namespace's tree
1734 */
1735static int do_new_mount(struct path *path, char *type, int flags,
1736			int mnt_flags, char *name, void *data)
1737{
1738	struct vfsmount *mnt;
1739
1740	if (!type)
1741		return -EINVAL;
1742
1743	/* we need capabilities... */
1744	if (!capable(CAP_SYS_ADMIN))
1745		return -EPERM;
1746
1747	lock_kernel();
1748	mnt = do_kern_mount(type, flags, name, data);
1749	unlock_kernel();
1750	if (IS_ERR(mnt))
1751		return PTR_ERR(mnt);
1752
1753	return do_add_mount(mnt, path, mnt_flags, NULL);
1754}
1755
1756/*
1757 * add a mount into a namespace's mount tree
1758 * - provide the option of adding the new mount to an expiration list
1759 */
1760int do_add_mount(struct vfsmount *newmnt, struct path *path,
1761		 int mnt_flags, struct list_head *fslist)
1762{
1763	int err;
1764
1765	mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
1766
1767	down_write(&namespace_sem);
1768	/* Something was mounted here while we slept */
1769	while (d_mountpoint(path->dentry) &&
1770	       follow_down(path))
1771		;
1772	err = -EINVAL;
1773	if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
1774		goto unlock;
1775
1776	/* Refuse the same filesystem on the same mount point */
1777	err = -EBUSY;
1778	if (path->mnt->mnt_sb == newmnt->mnt_sb &&
1779	    path->mnt->mnt_root == path->dentry)
1780		goto unlock;
1781
1782	err = -EINVAL;
1783	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1784		goto unlock;
1785
1786	newmnt->mnt_flags = mnt_flags;
1787	if ((err = graft_tree(newmnt, path)))
1788		goto unlock;
1789
1790	if (fslist) /* add to the specified expiration list */
1791		list_add_tail(&newmnt->mnt_expire, fslist);
1792
1793	up_write(&namespace_sem);
1794	return 0;
1795
1796unlock:
1797	up_write(&namespace_sem);
1798	mntput(newmnt);
1799	return err;
1800}
1801
1802EXPORT_SYMBOL_GPL(do_add_mount);
1803
1804/*
1805 * process a list of expirable mountpoints with the intent of discarding any
1806 * mountpoints that aren't in use and haven't been touched since last we came
1807 * here
1808 */
1809void mark_mounts_for_expiry(struct list_head *mounts)
1810{
1811	struct vfsmount *mnt, *next;
1812	LIST_HEAD(graveyard);
1813	LIST_HEAD(umounts);
1814
1815	if (list_empty(mounts))
1816		return;
1817
1818	down_write(&namespace_sem);
1819	br_write_lock(vfsmount_lock);
1820
1821	/* extract from the expiration list every vfsmount that matches the
1822	 * following criteria:
1823	 * - only referenced by its parent vfsmount
1824	 * - still marked for expiry (marked on the last call here; marks are
1825	 *   cleared by mntput())
1826	 */
1827	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1828		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1829			propagate_mount_busy(mnt, 1))
1830			continue;
1831		list_move(&mnt->mnt_expire, &graveyard);
1832	}
1833	while (!list_empty(&graveyard)) {
1834		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1835		touch_mnt_namespace(mnt->mnt_ns);
1836		umount_tree(mnt, 1, &umounts);
1837	}
1838	br_write_unlock(vfsmount_lock);
1839	up_write(&namespace_sem);
1840
1841	release_mounts(&umounts);
1842}
1843
1844EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1845
1846/*
1847 * Ripoff of 'select_parent()'
1848 *
1849 * search the list of submounts for a given mountpoint, and move any
1850 * shrinkable submounts to the 'graveyard' list.
1851 */
1852static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
1853{
1854	struct vfsmount *this_parent = parent;
1855	struct list_head *next;
1856	int found = 0;
1857
1858repeat:
1859	next = this_parent->mnt_mounts.next;
1860resume:
1861	while (next != &this_parent->mnt_mounts) {
1862		struct list_head *tmp = next;
1863		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
1864
1865		next = tmp->next;
1866		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
1867			continue;
1868		/*
1869		 * Descend a level if the d_mounts list is non-empty.
1870		 */
1871		if (!list_empty(&mnt->mnt_mounts)) {
1872			this_parent = mnt;
1873			goto repeat;
1874		}
1875
1876		if (!propagate_mount_busy(mnt, 1)) {
1877			list_move_tail(&mnt->mnt_expire, graveyard);
1878			found++;
1879		}
1880	}
1881	/*
1882	 * All done at this level ... ascend and resume the search
1883	 */
1884	if (this_parent != parent) {
1885		next = this_parent->mnt_child.next;
1886		this_parent = this_parent->mnt_parent;
1887		goto resume;
1888	}
1889	return found;
1890}
1891
1892/*
1893 * process a list of expirable mountpoints with the intent of discarding any
1894 * submounts of a specific parent mountpoint
1895 *
1896 * vfsmount_lock must be held for write
1897 */
1898static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
1899{
1900	LIST_HEAD(graveyard);
1901	struct vfsmount *m;
1902
1903	/* extract submounts of 'mountpoint' from the expiration list */
1904	while (select_submounts(mnt, &graveyard)) {
1905		while (!list_empty(&graveyard)) {
1906			m = list_first_entry(&graveyard, struct vfsmount,
1907						mnt_expire);
1908			touch_mnt_namespace(m->mnt_ns);
1909			umount_tree(m, 1, umounts);
1910		}
1911	}
1912}
1913
1914/*
1915 * Some copy_from_user() implementations do not return the exact number of
1916 * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
1917 * Note that this function differs from copy_from_user() in that it will oops
1918 * on bad values of `to', rather than returning a short copy.
1919 */
1920static long exact_copy_from_user(void *to, const void __user * from,
1921				 unsigned long n)
1922{
1923	char *t = to;
1924	const char __user *f = from;
1925	char c;
1926
1927	if (!access_ok(VERIFY_READ, from, n))
1928		return n;
1929
1930	while (n) {
1931		if (__get_user(c, f)) {
1932			memset(t, 0, n);
1933			break;
1934		}
1935		*t++ = c;
1936		f++;
1937		n--;
1938	}
1939	return n;
1940}
1941
1942int copy_mount_options(const void __user * data, unsigned long *where)
1943{
1944	int i;
1945	unsigned long page;
1946	unsigned long size;
1947
1948	*where = 0;
1949	if (!data)
1950		return 0;
1951
1952	if (!(page = __get_free_page(GFP_KERNEL)))
1953		return -ENOMEM;
1954
1955	/* We only care that *some* data at the address the user
1956	 * gave us is valid.  Just in case, we'll zero
1957	 * the remainder of the page.
1958	 */
1959	/* copy_from_user cannot cross TASK_SIZE ! */
1960	size = TASK_SIZE - (unsigned long)data;
1961	if (size > PAGE_SIZE)
1962		size = PAGE_SIZE;
1963
1964	i = size - exact_copy_from_user((void *)page, data, size);
1965	if (!i) {
1966		free_page(page);
1967		return -EFAULT;
1968	}
1969	if (i != PAGE_SIZE)
1970		memset((char *)page + i, 0, PAGE_SIZE - i);
1971	*where = page;
1972	return 0;
1973}
1974
1975int copy_mount_string(const void __user *data, char **where)
1976{
1977	char *tmp;
1978
1979	if (!data) {
1980		*where = NULL;
1981		return 0;
1982	}
1983
1984	tmp = strndup_user(data, PAGE_SIZE);
1985	if (IS_ERR(tmp))
1986		return PTR_ERR(tmp);
1987
1988	*where = tmp;
1989	return 0;
1990}
1991
1992/*
1993 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1994 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1995 *
1996 * data is a (void *) that can point to any structure up to
1997 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1998 * information (or be NULL).
1999 *
2000 * Pre-0.97 versions of mount() didn't have a flags word.
2001 * When the flags word was introduced its top half was required
2002 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2003 * Therefore, if this magic number is present, it carries no information
2004 * and must be discarded.
2005 */
2006long do_mount(char *dev_name, char *dir_name, char *type_page,
2007		  unsigned long flags, void *data_page)
2008{
2009	struct path path;
2010	int retval = 0;
2011	int mnt_flags = 0;
2012
2013	/* Discard magic */
2014	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2015		flags &= ~MS_MGC_MSK;
2016
2017	/* Basic sanity checks */
2018
2019	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2020		return -EINVAL;
2021
2022	if (data_page)
2023		((char *)data_page)[PAGE_SIZE - 1] = 0;
2024
2025	/* ... and get the mountpoint */
2026	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2027	if (retval)
2028		return retval;
2029
2030	retval = security_sb_mount(dev_name, &path,
2031				   type_page, flags, data_page);
2032	if (retval)
2033		goto dput_out;
2034
2035	/* Default to relatime unless overriden */
2036	if (!(flags & MS_NOATIME))
2037		mnt_flags |= MNT_RELATIME;
2038
2039	/* Separate the per-mountpoint flags */
2040	if (flags & MS_NOSUID)
2041		mnt_flags |= MNT_NOSUID;
2042	if (flags & MS_NODEV)
2043		mnt_flags |= MNT_NODEV;
2044	if (flags & MS_NOEXEC)
2045		mnt_flags |= MNT_NOEXEC;
2046	if (flags & MS_NOATIME)
2047		mnt_flags |= MNT_NOATIME;
2048	if (flags & MS_NODIRATIME)
2049		mnt_flags |= MNT_NODIRATIME;
2050	if (flags & MS_STRICTATIME)
2051		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2052	if (flags & MS_RDONLY)
2053		mnt_flags |= MNT_READONLY;
2054
2055	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2056		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2057		   MS_STRICTATIME);
2058
2059	if (flags & MS_REMOUNT)
2060		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
2061				    data_page);
2062	else if (flags & MS_BIND)
2063		retval = do_loopback(&path, dev_name, flags & MS_REC);
2064	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2065		retval = do_change_type(&path, flags);
2066	else if (flags & MS_MOVE)
2067		retval = do_move_mount(&path, dev_name);
2068	else
2069		retval = do_new_mount(&path, type_page, flags, mnt_flags,
2070				      dev_name, data_page);
2071dput_out:
2072	path_put(&path);
2073	return retval;
2074}
2075
2076static struct mnt_namespace *alloc_mnt_ns(void)
2077{
2078	struct mnt_namespace *new_ns;
2079
2080	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2081	if (!new_ns)
2082		return ERR_PTR(-ENOMEM);
2083	atomic_set(&new_ns->count, 1);
2084	new_ns->root = NULL;
2085	INIT_LIST_HEAD(&new_ns->list);
2086	init_waitqueue_head(&new_ns->poll);
2087	new_ns->event = 0;
2088	return new_ns;
2089}
2090
2091/*
2092 * Allocate a new namespace structure and populate it with contents
2093 * copied from the namespace of the passed in task structure.
2094 */
2095static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
2096		struct fs_struct *fs)
2097{
2098	struct mnt_namespace *new_ns;
2099	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2100	struct vfsmount *p, *q;
2101
2102	new_ns = alloc_mnt_ns();
2103	if (IS_ERR(new_ns))
2104		return new_ns;
2105
2106	down_write(&namespace_sem);
2107	/* First pass: copy the tree topology */
2108	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
2109					CL_COPY_ALL | CL_EXPIRE);
2110	if (!new_ns->root) {
2111		up_write(&namespace_sem);
2112		kfree(new_ns);
2113		return ERR_PTR(-ENOMEM);
2114	}
2115	br_write_lock(vfsmount_lock);
2116	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
2117	br_write_unlock(vfsmount_lock);
2118
2119	/*
2120	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2121	 * as belonging to new namespace.  We have already acquired a private
2122	 * fs_struct, so tsk->fs->lock is not needed.
2123	 */
2124	p = mnt_ns->root;
2125	q = new_ns->root;
2126	while (p) {
2127		q->mnt_ns = new_ns;
2128		if (fs) {
2129			if (p == fs->root.mnt) {
2130				rootmnt = p;
2131				fs->root.mnt = mntget(q);
2132			}
2133			if (p == fs->pwd.mnt) {
2134				pwdmnt = p;
2135				fs->pwd.mnt = mntget(q);
2136			}
2137		}
2138		p = next_mnt(p, mnt_ns->root);
2139		q = next_mnt(q, new_ns->root);
2140	}
2141	up_write(&namespace_sem);
2142
2143	if (rootmnt)
2144		mntput(rootmnt);
2145	if (pwdmnt)
2146		mntput(pwdmnt);
2147
2148	return new_ns;
2149}
2150
2151struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2152		struct fs_struct *new_fs)
2153{
2154	struct mnt_namespace *new_ns;
2155
2156	BUG_ON(!ns);
2157	get_mnt_ns(ns);
2158
2159	if (!(flags & CLONE_NEWNS))
2160		return ns;
2161
2162	new_ns = dup_mnt_ns(ns, new_fs);
2163
2164	put_mnt_ns(ns);
2165	return new_ns;
2166}
2167
2168/**
2169 * create_mnt_ns - creates a private namespace and adds a root filesystem
2170 * @mnt: pointer to the new root filesystem mountpoint
2171 */
2172struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
2173{
2174	struct mnt_namespace *new_ns;
2175
2176	new_ns = alloc_mnt_ns();
2177	if (!IS_ERR(new_ns)) {
2178		mnt->mnt_ns = new_ns;
2179		new_ns->root = mnt;
2180		list_add(&new_ns->list, &new_ns->root->mnt_list);
2181	}
2182	return new_ns;
2183}
2184EXPORT_SYMBOL(create_mnt_ns);
2185
2186SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2187		char __user *, type, unsigned long, flags, void __user *, data)
2188{
2189	int ret;
2190	char *kernel_type;
2191	char *kernel_dir;
2192	char *kernel_dev;
2193	unsigned long data_page;
2194
2195	ret = copy_mount_string(type, &kernel_type);
2196	if (ret < 0)
2197		goto out_type;
2198
2199	kernel_dir = getname(dir_name);
2200	if (IS_ERR(kernel_dir)) {
2201		ret = PTR_ERR(kernel_dir);
2202		goto out_dir;
2203	}
2204
2205	ret = copy_mount_string(dev_name, &kernel_dev);
2206	if (ret < 0)
2207		goto out_dev;
2208
2209	ret = copy_mount_options(data, &data_page);
2210	if (ret < 0)
2211		goto out_data;
2212
2213	ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
2214		(void *) data_page);
2215
2216	free_page(data_page);
2217out_data:
2218	kfree(kernel_dev);
2219out_dev:
2220	putname(kernel_dir);
2221out_dir:
2222	kfree(kernel_type);
2223out_type:
2224	return ret;
2225}
2226
2227/*
2228 * pivot_root Semantics:
2229 * Moves the root file system of the current process to the directory put_old,
2230 * makes new_root as the new root file system of the current process, and sets
2231 * root/cwd of all processes which had them on the current root to new_root.
2232 *
2233 * Restrictions:
2234 * The new_root and put_old must be directories, and  must not be on the
2235 * same file  system as the current process root. The put_old  must  be
2236 * underneath new_root,  i.e. adding a non-zero number of /.. to the string
2237 * pointed to by put_old must yield the same directory as new_root. No other
2238 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2239 *
2240 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2241 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2242 * in this situation.
2243 *
2244 * Notes:
2245 *  - we don't move root/cwd if they are not at the root (reason: if something
2246 *    cared enough to change them, it's probably wrong to force them elsewhere)
2247 *  - it's okay to pick a root that isn't the root of a file system, e.g.
2248 *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2249 *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2250 *    first.
2251 */
2252SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2253		const char __user *, put_old)
2254{
2255	struct vfsmount *tmp;
2256	struct path new, old, parent_path, root_parent, root;
2257	int error;
2258
2259	if (!capable(CAP_SYS_ADMIN))
2260		return -EPERM;
2261
2262	error = user_path_dir(new_root, &new);
2263	if (error)
2264		goto out0;
2265	error = -EINVAL;
2266	if (!check_mnt(new.mnt))
2267		goto out1;
2268
2269	error = user_path_dir(put_old, &old);
2270	if (error)
2271		goto out1;
2272
2273	error = security_sb_pivotroot(&old, &new);
2274	if (error) {
2275		path_put(&old);
2276		goto out1;
2277	}
2278
2279	get_fs_root(current->fs, &root);
2280	down_write(&namespace_sem);
2281	mutex_lock(&old.dentry->d_inode->i_mutex);
2282	error = -EINVAL;
2283	if (IS_MNT_SHARED(old.mnt) ||
2284		IS_MNT_SHARED(new.mnt->mnt_parent) ||
2285		IS_MNT_SHARED(root.mnt->mnt_parent))
2286		goto out2;
2287	if (!check_mnt(root.mnt))
2288		goto out2;
2289	error = -ENOENT;
2290	if (cant_mount(old.dentry))
2291		goto out2;
2292	if (d_unlinked(new.dentry))
2293		goto out2;
2294	if (d_unlinked(old.dentry))
2295		goto out2;
2296	error = -EBUSY;
2297	if (new.mnt == root.mnt ||
2298	    old.mnt == root.mnt)
2299		goto out2; /* loop, on the same file system  */
2300	error = -EINVAL;
2301	if (root.mnt->mnt_root != root.dentry)
2302		goto out2; /* not a mountpoint */
2303	if (root.mnt->mnt_parent == root.mnt)
2304		goto out2; /* not attached */
2305	if (new.mnt->mnt_root != new.dentry)
2306		goto out2; /* not a mountpoint */
2307	if (new.mnt->mnt_parent == new.mnt)
2308		goto out2; /* not attached */
2309	/* make sure we can reach put_old from new_root */
2310	tmp = old.mnt;
2311	br_write_lock(vfsmount_lock);
2312	if (tmp != new.mnt) {
2313		for (;;) {
2314			if (tmp->mnt_parent == tmp)
2315				goto out3; /* already mounted on put_old */
2316			if (tmp->mnt_parent == new.mnt)
2317				break;
2318			tmp = tmp->mnt_parent;
2319		}
2320		if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
2321			goto out3;
2322	} else if (!is_subdir(old.dentry, new.dentry))
2323		goto out3;
2324	detach_mnt(new.mnt, &parent_path);
2325	detach_mnt(root.mnt, &root_parent);
2326	/* mount old root on put_old */
2327	attach_mnt(root.mnt, &old);
2328	/* mount new_root on / */
2329	attach_mnt(new.mnt, &root_parent);
2330	touch_mnt_namespace(current->nsproxy->mnt_ns);
2331	br_write_unlock(vfsmount_lock);
2332	chroot_fs_refs(&root, &new);
2333	error = 0;
2334	path_put(&root_parent);
2335	path_put(&parent_path);
2336out2:
2337	mutex_unlock(&old.dentry->d_inode->i_mutex);
2338	up_write(&namespace_sem);
2339	path_put(&root);
2340	path_put(&old);
2341out1:
2342	path_put(&new);
2343out0:
2344	return error;
2345out3:
2346	br_write_unlock(vfsmount_lock);
2347	goto out2;
2348}
2349
2350static void __init init_mount_tree(void)
2351{
2352	struct vfsmount *mnt;
2353	struct mnt_namespace *ns;
2354	struct path root;
2355
2356	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
2357	if (IS_ERR(mnt))
2358		panic("Can't create rootfs");
2359	ns = create_mnt_ns(mnt);
2360	if (IS_ERR(ns))
2361		panic("Can't allocate initial namespace");
2362
2363	init_task.nsproxy->mnt_ns = ns;
2364	get_mnt_ns(ns);
2365
2366	root.mnt = ns->root;
2367	root.dentry = ns->root->mnt_root;
2368
2369	set_fs_pwd(current->fs, &root);
2370	set_fs_root(current->fs, &root);
2371}
2372
2373void __init mnt_init(void)
2374{
2375	unsigned u;
2376	int err;
2377
2378	init_rwsem(&namespace_sem);
2379
2380	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
2381			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
2382
2383	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
2384
2385	if (!mount_hashtable)
2386		panic("Failed to allocate mount hash table\n");
2387
2388	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
2389
2390	for (u = 0; u < HASH_SIZE; u++)
2391		INIT_LIST_HEAD(&mount_hashtable[u]);
2392
2393	br_lock_init(vfsmount_lock);
2394
2395	err = sysfs_init();
2396	if (err)
2397		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
2398			__func__, err);
2399	fs_kobj = kobject_create_and_add("fs", NULL);
2400	if (!fs_kobj)
2401		printk(KERN_WARNING "%s: kobj create error\n", __func__);
2402	init_rootfs();
2403	init_mount_tree();
2404}
2405
2406void put_mnt_ns(struct mnt_namespace *ns)
2407{
2408	LIST_HEAD(umount_list);
2409
2410	if (!atomic_dec_and_test(&ns->count))
2411		return;
2412	down_write(&namespace_sem);
2413	br_write_lock(vfsmount_lock);
2414	umount_tree(ns->root, 0, &umount_list);
2415	br_write_unlock(vfsmount_lock);
2416	up_write(&namespace_sem);
2417	release_mounts(&umount_list);
2418	kfree(ns);
2419}
2420EXPORT_SYMBOL(put_mnt_ns);
2421