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