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