disable some mediatekl custom warnings
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / namespace.c
1 /*
2 * linux/fs/namespace.c
3 *
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
6 *
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
10
11 #include <linux/syscalls.h>
12 #include <linux/export.h>
13 #include <linux/capability.h>
14 #include <linux/mnt_namespace.h>
15 #include <linux/user_namespace.h>
16 #include <linux/namei.h>
17 #include <linux/security.h>
18 #include <linux/idr.h>
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>
24 //#define UMOUNT_LOG //enable kernel layer unmount log when unmount fail
25 #ifdef UMOUNT_LOG
26 #include <linux/types.h>
27 #endif
28 #include <linux/proc_ns.h>
29 #include <linux/magic.h>
30 #include "pnode.h"
31 #include "internal.h"
32
33 #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
34 #define HASH_SIZE (1UL << HASH_SHIFT)
35
36 static int event;
37 static DEFINE_IDA(mnt_id_ida);
38 static DEFINE_IDA(mnt_group_ida);
39 static DEFINE_SPINLOCK(mnt_id_lock);
40 static int mnt_id_start = 0;
41 static int mnt_group_start = 1;
42
43 static struct list_head *mount_hashtable __read_mostly;
44 static struct list_head *mountpoint_hashtable __read_mostly;
45 static struct kmem_cache *mnt_cache __read_mostly;
46 static struct rw_semaphore namespace_sem;
47
48 /* /sys/fs */
49 struct kobject *fs_kobj;
50 EXPORT_SYMBOL_GPL(fs_kobj);
51
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 */
60 DEFINE_BRLOCK(vfsmount_lock);
61
62 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
63 {
64 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
65 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
66 tmp = tmp + (tmp >> HASH_SHIFT);
67 return tmp & (HASH_SIZE - 1);
68 }
69
70 #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
71
72 /*
73 * allocation is serialized by namespace_sem, but we need the spinlock to
74 * serialize with freeing.
75 */
76 static int mnt_alloc_id(struct mount *mnt)
77 {
78 int res;
79
80 retry:
81 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
82 spin_lock(&mnt_id_lock);
83 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
84 if (!res)
85 mnt_id_start = mnt->mnt_id + 1;
86 spin_unlock(&mnt_id_lock);
87 if (res == -EAGAIN)
88 goto retry;
89
90 return res;
91 }
92
93 static void mnt_free_id(struct mount *mnt)
94 {
95 int id = mnt->mnt_id;
96 spin_lock(&mnt_id_lock);
97 ida_remove(&mnt_id_ida, id);
98 if (mnt_id_start > id)
99 mnt_id_start = id;
100 spin_unlock(&mnt_id_lock);
101 }
102
103 /*
104 * Allocate a new peer group ID
105 *
106 * mnt_group_ida is protected by namespace_sem
107 */
108 static int mnt_alloc_group_id(struct mount *mnt)
109 {
110 int res;
111
112 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
113 return -ENOMEM;
114
115 res = ida_get_new_above(&mnt_group_ida,
116 mnt_group_start,
117 &mnt->mnt_group_id);
118 if (!res)
119 mnt_group_start = mnt->mnt_group_id + 1;
120
121 return res;
122 }
123
124 /*
125 * Release a peer group ID
126 */
127 void mnt_release_group_id(struct mount *mnt)
128 {
129 int id = mnt->mnt_group_id;
130 ida_remove(&mnt_group_ida, id);
131 if (mnt_group_start > id)
132 mnt_group_start = id;
133 mnt->mnt_group_id = 0;
134 }
135
136 /*
137 * vfsmount lock must be held for read
138 */
139 #ifdef UMOUNT_LOG
140 #define UMOUNT_Partition "/emmc@usrdata"
141 struct record_ref_count{
142 pid_t pid;
143 char name[TASK_COMM_LEN];
144 int count;
145 struct record_ref_count *next;
146 };
147
148 struct record_ref_count *ref_head = NULL;
149 struct record_ref_count *ref_current = NULL;
150 struct record_ref_count *ref_prev = NULL;
151 int s_total_count = 0;
152 #endif
153 static inline void mnt_add_count(struct mount *mnt, int n)
154 {
155 #ifdef UMOUNT_LOG
156 int 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
233 #ifdef CONFIG_SMP
234 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
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
263 #else
264 preempt_disable();
265 mnt->mnt_count += n;
266 preempt_enable();
267 #endif
268 }
269
270 /*
271 * vfsmount lock must be held for write
272 */
273 unsigned int mnt_get_count(struct mount *mnt)
274 {
275 #ifdef CONFIG_SMP
276 unsigned int count = 0;
277 int cpu;
278
279 for_each_possible_cpu(cpu) {
280 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
281 }
282
283 return count;
284 #else
285 return mnt->mnt_count;
286 #endif
287 }
288
289 static struct mount *alloc_vfsmnt(const char *name)
290 {
291 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
292 if (mnt) {
293 int err;
294
295 err = mnt_alloc_id(mnt);
296 if (err)
297 goto out_free_cache;
298
299 if (name) {
300 mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
301 if (!mnt->mnt_devname)
302 goto out_free_id;
303 }
304
305 #ifdef CONFIG_SMP
306 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
307 if (!mnt->mnt_pcp)
308 goto out_free_devname;
309
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
316 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
317 #else
318 mnt->mnt_count = 1;
319 mnt->mnt_writers = 0;
320 #endif
321
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);
330 #ifdef CONFIG_FSNOTIFY
331 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
332 #endif
333 }
334 return mnt;
335
336 #ifdef CONFIG_SMP
337 out_free_devname:
338 kfree(mnt->mnt_devname);
339 #endif
340 out_free_id:
341 mnt_free_id(mnt);
342 out_free_cache:
343 kmem_cache_free(mnt_cache, mnt);
344 return NULL;
345 }
346
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 */
366 int __mnt_is_readonly(struct vfsmount *mnt)
367 {
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;
373 }
374 EXPORT_SYMBOL_GPL(__mnt_is_readonly);
375
376 static inline void mnt_inc_writers(struct mount *mnt)
377 {
378 #ifdef CONFIG_SMP
379 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
380 #else
381 mnt->mnt_writers++;
382 #endif
383 }
384
385 static inline void mnt_dec_writers(struct mount *mnt)
386 {
387 #ifdef CONFIG_SMP
388 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
389 #else
390 mnt->mnt_writers--;
391 #endif
392 }
393
394 static unsigned int mnt_get_writers(struct mount *mnt)
395 {
396 #ifdef CONFIG_SMP
397 unsigned int count = 0;
398 int cpu;
399
400 for_each_possible_cpu(cpu) {
401 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
402 }
403
404 return count;
405 #else
406 return mnt->mnt_writers;
407 #endif
408 }
409
410 static 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
419 /*
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.
424 */
425 /**
426 * __mnt_want_write - get write access to a mount without freeze protection
427 * @m: the mount on which to take a write
428 *
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.
434 */
435 int __mnt_want_write(struct vfsmount *m)
436 {
437 struct mount *mnt = real_mount(m);
438 int ret = 0;
439
440 preempt_disable();
441 mnt_inc_writers(mnt);
442 /*
443 * The store to mnt_inc_writers must be visible before we pass
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();
448 while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
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();
456 if (mnt_is_readonly(m)) {
457 mnt_dec_writers(mnt);
458 ret = -EROFS;
459 }
460 preempt_enable();
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 */
474 int 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);
482 return ret;
483 }
484 EXPORT_SYMBOL_GPL(mnt_want_write);
485
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 */
498 int 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();
504 mnt_inc_writers(real_mount(mnt));
505 preempt_enable();
506 return 0;
507 }
508 EXPORT_SYMBOL_GPL(mnt_clone_write);
509
510 /**
511 * __mnt_want_write_file - get write access to a file's mount
512 * @file: the file who's mount on which to take a write
513 *
514 * This is like __mnt_want_write, but it takes a file and can
515 * do some optimisations if the file is open for write already
516 */
517 int __mnt_want_write_file(struct file *file)
518 {
519 struct inode *inode = file_inode(file);
520
521 if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
522 return __mnt_want_write(file->f_path.mnt);
523 else
524 return mnt_clone_write(file->f_path.mnt);
525 }
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 */
534 int 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 }
544 EXPORT_SYMBOL_GPL(mnt_want_write_file);
545
546 /**
547 * __mnt_drop_write - give up write access to a mount
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
552 * __mnt_want_write() call above.
553 */
554 void __mnt_drop_write(struct vfsmount *mnt)
555 {
556 preempt_disable();
557 mnt_dec_writers(real_mount(mnt));
558 preempt_enable();
559 }
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 */
569 void mnt_drop_write(struct vfsmount *mnt)
570 {
571 __mnt_drop_write(mnt);
572 sb_end_write(mnt->mnt_sb);
573 }
574 EXPORT_SYMBOL_GPL(mnt_drop_write);
575
576 void __mnt_drop_write_file(struct file *file)
577 {
578 __mnt_drop_write(file->f_path.mnt);
579 }
580
581 void mnt_drop_write_file(struct file *file)
582 {
583 mnt_drop_write(file->f_path.mnt);
584 }
585 EXPORT_SYMBOL(mnt_drop_write_file);
586
587 static int mnt_make_readonly(struct mount *mnt)
588 {
589 int ret = 0;
590
591 br_write_lock(&vfsmount_lock);
592 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
593 /*
594 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
595 * should be visible before we do.
596 */
597 smp_mb();
598
599 /*
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.
614 */
615 if (mnt_get_writers(mnt) > 0)
616 ret = -EBUSY;
617 else
618 mnt->mnt.mnt_flags |= MNT_READONLY;
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();
624 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
625 br_write_unlock(&vfsmount_lock);
626 return ret;
627 }
628
629 static void __mnt_unmake_readonly(struct mount *mnt)
630 {
631 br_write_lock(&vfsmount_lock);
632 mnt->mnt.mnt_flags &= ~MNT_READONLY;
633 br_write_unlock(&vfsmount_lock);
634 }
635
636 int sb_prepare_remount_readonly(struct super_block *sb)
637 {
638 struct mount *mnt;
639 int err = 0;
640
641 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
642 if (atomic_long_read(&sb->s_remove_count))
643 return -EBUSY;
644
645 br_write_lock(&vfsmount_lock);
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 }
656 if (!err && atomic_long_read(&sb->s_remove_count))
657 err = -EBUSY;
658
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 }
667 br_write_unlock(&vfsmount_lock);
668
669 return err;
670 }
671
672 static void free_vfsmnt(struct mount *mnt)
673 {
674 kfree(mnt->mnt_devname);
675 mnt_free_id(mnt);
676 #ifdef CONFIG_SMP
677 free_percpu(mnt->mnt_pcp);
678 #endif
679 kmem_cache_free(mnt_cache, mnt);
680 }
681
682 /*
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.
685 * vfsmount_lock must be held for read or write.
686 */
687 struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
688 int dir)
689 {
690 struct list_head *head = mount_hashtable + hash(mnt, dentry);
691 struct list_head *tmp = head;
692 struct mount *p, *found = NULL;
693
694 for (;;) {
695 tmp = dir ? tmp->next : tmp->prev;
696 p = NULL;
697 if (tmp == head)
698 break;
699 p = list_entry(tmp, struct mount, mnt_hash);
700 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
701 found = p;
702 break;
703 }
704 }
705 return found;
706 }
707
708 /*
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.
723 */
724 struct vfsmount *lookup_mnt(struct path *path)
725 {
726 struct mount *child_mnt;
727
728 br_read_lock(&vfsmount_lock);
729 child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
730 if (child_mnt) {
731 mnt_add_count(child_mnt, 1);
732 br_read_unlock(&vfsmount_lock);
733 return &child_mnt->mnt;
734 } else {
735 br_read_unlock(&vfsmount_lock);
736 return NULL;
737 }
738 }
739
740 static 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
773 static 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
785 static inline int check_mnt(struct mount *mnt)
786 {
787 return mnt->mnt_ns == current->nsproxy->mnt_ns;
788 }
789
790 /*
791 * vfsmount lock must be held for write
792 */
793 static void touch_mnt_namespace(struct mnt_namespace *ns)
794 {
795 if (ns) {
796 ns->event = ++event;
797 wake_up_interruptible(&ns->poll);
798 }
799 }
800
801 /*
802 * vfsmount lock must be held for write
803 */
804 static void __touch_mnt_namespace(struct mnt_namespace *ns)
805 {
806 if (ns && ns->event != event) {
807 ns->event = event;
808 wake_up_interruptible(&ns->poll);
809 }
810 }
811
812 /*
813 * vfsmount lock must be held for write
814 */
815 static void detach_mnt(struct mount *mnt, struct path *old_path)
816 {
817 old_path->dentry = mnt->mnt_mountpoint;
818 old_path->mnt = &mnt->mnt_parent->mnt;
819 mnt->mnt_parent = mnt;
820 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
821 list_del_init(&mnt->mnt_child);
822 list_del_init(&mnt->mnt_hash);
823 put_mountpoint(mnt->mnt_mp);
824 mnt->mnt_mp = NULL;
825 }
826
827 /*
828 * vfsmount lock must be held for write
829 */
830 void mnt_set_mountpoint(struct mount *mnt,
831 struct mountpoint *mp,
832 struct mount *child_mnt)
833 {
834 mp->m_count++;
835 mnt_add_count(mnt, 1); /* essentially, that's mntget */
836 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
837 child_mnt->mnt_parent = mnt;
838 child_mnt->mnt_mp = mp;
839 }
840
841 /*
842 * vfsmount lock must be held for write
843 */
844 static void attach_mnt(struct mount *mnt,
845 struct mount *parent,
846 struct mountpoint *mp)
847 {
848 mnt_set_mountpoint(parent, mp, mnt);
849 list_add_tail(&mnt->mnt_hash, mount_hashtable +
850 hash(&parent->mnt, mp->m_dentry));
851 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
852 }
853
854 /*
855 * vfsmount lock must be held for write
856 */
857 static void commit_tree(struct mount *mnt)
858 {
859 struct mount *parent = mnt->mnt_parent;
860 struct mount *m;
861 LIST_HEAD(head);
862 struct mnt_namespace *n = parent->mnt_ns;
863
864 BUG_ON(parent == mnt);
865
866 list_add_tail(&head, &mnt->mnt_list);
867 list_for_each_entry(m, &head, mnt_list)
868 m->mnt_ns = n;
869
870 list_splice(&head, n->list.prev);
871
872 list_add_tail(&mnt->mnt_hash, mount_hashtable +
873 hash(&parent->mnt, mnt->mnt_mountpoint));
874 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
875 touch_mnt_namespace(n);
876 }
877
878 static struct mount *next_mnt(struct mount *p, struct mount *root)
879 {
880 struct list_head *next = p->mnt_mounts.next;
881 if (next == &p->mnt_mounts) {
882 while (1) {
883 if (p == root)
884 return NULL;
885 next = p->mnt_child.next;
886 if (next != &p->mnt_parent->mnt_mounts)
887 break;
888 p = p->mnt_parent;
889 }
890 }
891 return list_entry(next, struct mount, mnt_child);
892 }
893
894 static struct mount *skip_mnt_tree(struct mount *p)
895 {
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;
900 }
901 return p;
902 }
903
904 struct vfsmount *
905 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
906 {
907 struct mount *mnt;
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)
918 mnt->mnt.mnt_flags = MNT_INTERNAL;
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
926 mnt->mnt.mnt_root = root;
927 mnt->mnt.mnt_sb = root->d_sb;
928 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
929 mnt->mnt_parent = mnt;
930 br_write_lock(&vfsmount_lock);
931 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
932 br_write_unlock(&vfsmount_lock);
933 return &mnt->mnt;
934 }
935 EXPORT_SYMBOL_GPL(vfs_kern_mount);
936
937 static struct mount *clone_mnt(struct mount *old, struct dentry *root,
938 int flag)
939 {
940 struct super_block *sb = old->mnt.mnt_sb;
941 struct mount *mnt;
942 int err;
943
944 mnt = alloc_vfsmnt(old->mnt_devname);
945 if (!mnt)
946 return ERR_PTR(-ENOMEM);
947
948 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
949 mnt->mnt_group_id = 0; /* not a peer of original */
950 else
951 mnt->mnt_group_id = old->mnt_group_id;
952
953 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
954 err = mnt_alloc_group_id(mnt);
955 if (err)
956 goto out_free;
957 }
958
959 mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
960 /* Don't allow unprivileged users to change mount flags */
961 if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY))
962 mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
963
964 atomic_inc(&sb->s_active);
965 mnt->mnt.mnt_sb = sb;
966 mnt->mnt.mnt_root = dget(root);
967 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
968 mnt->mnt_parent = mnt;
969 br_write_lock(&vfsmount_lock);
970 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
971 br_write_unlock(&vfsmount_lock);
972
973 if ((flag & CL_SLAVE) ||
974 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
975 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
976 mnt->mnt_master = old;
977 CLEAR_MNT_SHARED(mnt);
978 } else if (!(flag & CL_PRIVATE)) {
979 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
980 list_add(&mnt->mnt_share, &old->mnt_share);
981 if (IS_MNT_SLAVE(old))
982 list_add(&mnt->mnt_slave, &old->mnt_slave);
983 mnt->mnt_master = old->mnt_master;
984 }
985 if (flag & CL_MAKE_SHARED)
986 set_mnt_shared(mnt);
987
988 /* stick the duplicate mount on the same expiry list
989 * as the original if that was on one */
990 if (flag & CL_EXPIRE) {
991 if (!list_empty(&old->mnt_expire))
992 list_add(&mnt->mnt_expire, &old->mnt_expire);
993 }
994
995 return mnt;
996
997 out_free:
998 free_vfsmnt(mnt);
999 return ERR_PTR(err);
1000 }
1001
1002 static inline void mntfree(struct mount *mnt)
1003 {
1004 struct vfsmount *m = &mnt->mnt;
1005 struct super_block *sb = m->mnt_sb;
1006
1007 /*
1008 * This probably indicates that somebody messed
1009 * up a mnt_want/drop_write() pair. If this
1010 * happens, the filesystem was probably unable
1011 * to make r/w->r/o transitions.
1012 */
1013 /*
1014 * The locking used to deal with mnt_count decrement provides barriers,
1015 * so mnt_get_writers() below is safe.
1016 */
1017 WARN_ON(mnt_get_writers(mnt));
1018 fsnotify_vfsmount_delete(m);
1019 dput(m->mnt_root);
1020 free_vfsmnt(mnt);
1021 deactivate_super(sb);
1022 }
1023
1024 static void mntput_no_expire(struct mount *mnt)
1025 {
1026 put_again:
1027 #ifdef CONFIG_SMP
1028 br_read_lock(&vfsmount_lock);
1029 if (likely(mnt->mnt_ns)) {
1030 /* shouldn't be the last one */
1031 mnt_add_count(mnt, -1);
1032 br_read_unlock(&vfsmount_lock);
1033 return;
1034 }
1035 br_read_unlock(&vfsmount_lock);
1036
1037 br_write_lock(&vfsmount_lock);
1038 mnt_add_count(mnt, -1);
1039 if (mnt_get_count(mnt)) {
1040 br_write_unlock(&vfsmount_lock);
1041 return;
1042 }
1043 #else
1044 mnt_add_count(mnt, -1);
1045 if (likely(mnt_get_count(mnt)))
1046 return;
1047 br_write_lock(&vfsmount_lock);
1048 #endif
1049 if (unlikely(mnt->mnt_pinned)) {
1050 mnt_add_count(mnt, mnt->mnt_pinned + 1);
1051 mnt->mnt_pinned = 0;
1052 br_write_unlock(&vfsmount_lock);
1053 acct_auto_close_mnt(&mnt->mnt);
1054 goto put_again;
1055 }
1056
1057 list_del(&mnt->mnt_instance);
1058 br_write_unlock(&vfsmount_lock);
1059 mntfree(mnt);
1060 }
1061
1062 void mntput(struct vfsmount *mnt)
1063 {
1064 if (mnt) {
1065 struct mount *m = real_mount(mnt);
1066 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1067 if (unlikely(m->mnt_expiry_mark))
1068 m->mnt_expiry_mark = 0;
1069 mntput_no_expire(m);
1070 }
1071 }
1072 EXPORT_SYMBOL(mntput);
1073
1074 struct vfsmount *mntget(struct vfsmount *mnt)
1075 {
1076 if (mnt)
1077 mnt_add_count(real_mount(mnt), 1);
1078 return mnt;
1079 }
1080 EXPORT_SYMBOL(mntget);
1081
1082 void mnt_pin(struct vfsmount *mnt)
1083 {
1084 br_write_lock(&vfsmount_lock);
1085 real_mount(mnt)->mnt_pinned++;
1086 br_write_unlock(&vfsmount_lock);
1087 }
1088 EXPORT_SYMBOL(mnt_pin);
1089
1090 void mnt_unpin(struct vfsmount *m)
1091 {
1092 struct mount *mnt = real_mount(m);
1093 br_write_lock(&vfsmount_lock);
1094 if (mnt->mnt_pinned) {
1095 mnt_add_count(mnt, 1);
1096 mnt->mnt_pinned--;
1097 }
1098 br_write_unlock(&vfsmount_lock);
1099 }
1100 EXPORT_SYMBOL(mnt_unpin);
1101
1102 static inline void mangle(struct seq_file *m, const char *s)
1103 {
1104 seq_escape(m, s, " \t\n\\");
1105 }
1106
1107 /*
1108 * Simple .show_options callback for filesystems which don't want to
1109 * implement more complex mount option showing.
1110 *
1111 * See also save_mount_options().
1112 */
1113 int generic_show_options(struct seq_file *m, struct dentry *root)
1114 {
1115 const char *options;
1116
1117 rcu_read_lock();
1118 options = rcu_dereference(root->d_sb->s_options);
1119
1120 if (options != NULL && options[0]) {
1121 seq_putc(m, ',');
1122 mangle(m, options);
1123 }
1124 rcu_read_unlock();
1125
1126 return 0;
1127 }
1128 EXPORT_SYMBOL(generic_show_options);
1129
1130 /*
1131 * If filesystem uses generic_show_options(), this function should be
1132 * called from the fill_super() callback.
1133 *
1134 * The .remount_fs callback usually needs to be handled in a special
1135 * way, to make sure, that previous options are not overwritten if the
1136 * remount fails.
1137 *
1138 * Also note, that if the filesystem's .remount_fs function doesn't
1139 * reset all options to their default value, but changes only newly
1140 * given options, then the displayed options will not reflect reality
1141 * any more.
1142 */
1143 void save_mount_options(struct super_block *sb, char *options)
1144 {
1145 BUG_ON(sb->s_options);
1146 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1147 }
1148 EXPORT_SYMBOL(save_mount_options);
1149
1150 void replace_mount_options(struct super_block *sb, char *options)
1151 {
1152 char *old = sb->s_options;
1153 rcu_assign_pointer(sb->s_options, options);
1154 if (old) {
1155 synchronize_rcu();
1156 kfree(old);
1157 }
1158 }
1159 EXPORT_SYMBOL(replace_mount_options);
1160
1161 #ifdef CONFIG_PROC_FS
1162 /* iterator; we want it to have access to namespace_sem, thus here... */
1163 static void *m_start(struct seq_file *m, loff_t *pos)
1164 {
1165 struct proc_mounts *p = proc_mounts(m);
1166
1167 down_read(&namespace_sem);
1168 return seq_list_start(&p->ns->list, *pos);
1169 }
1170
1171 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1172 {
1173 struct proc_mounts *p = proc_mounts(m);
1174
1175 return seq_list_next(v, &p->ns->list, pos);
1176 }
1177
1178 static void m_stop(struct seq_file *m, void *v)
1179 {
1180 up_read(&namespace_sem);
1181 }
1182
1183 static int m_show(struct seq_file *m, void *v)
1184 {
1185 struct proc_mounts *p = proc_mounts(m);
1186 struct mount *r = list_entry(v, struct mount, mnt_list);
1187 return p->show(m, &r->mnt);
1188 }
1189
1190 const struct seq_operations mounts_op = {
1191 .start = m_start,
1192 .next = m_next,
1193 .stop = m_stop,
1194 .show = m_show,
1195 };
1196 #endif /* CONFIG_PROC_FS */
1197
1198 /**
1199 * may_umount_tree - check if a mount tree is busy
1200 * @mnt: root of mount tree
1201 *
1202 * This is called to check if a tree of mounts has any
1203 * open files, pwds, chroots or sub mounts that are
1204 * busy.
1205 */
1206 int may_umount_tree(struct vfsmount *m)
1207 {
1208 struct mount *mnt = real_mount(m);
1209 int actual_refs = 0;
1210 int minimum_refs = 0;
1211 struct mount *p;
1212 BUG_ON(!m);
1213
1214 /* write lock needed for mnt_get_count */
1215 br_write_lock(&vfsmount_lock);
1216 for (p = mnt; p; p = next_mnt(p, mnt)) {
1217 actual_refs += mnt_get_count(p);
1218 minimum_refs += 2;
1219 }
1220 br_write_unlock(&vfsmount_lock);
1221
1222 if (actual_refs > minimum_refs)
1223 return 0;
1224
1225 return 1;
1226 }
1227
1228 EXPORT_SYMBOL(may_umount_tree);
1229
1230 /**
1231 * may_umount - check if a mount point is busy
1232 * @mnt: root of mount
1233 *
1234 * This is called to check if a mount point has any
1235 * open files, pwds, chroots or sub mounts. If the
1236 * mount has sub mounts this will return busy
1237 * regardless of whether the sub mounts are busy.
1238 *
1239 * Doesn't take quota and stuff into account. IOW, in some cases it will
1240 * give false negatives. The main reason why it's here is that we need
1241 * a non-destructive way to look for easily umountable filesystems.
1242 */
1243 int may_umount(struct vfsmount *mnt)
1244 {
1245 int ret = 1;
1246 down_read(&namespace_sem);
1247 br_write_lock(&vfsmount_lock);
1248 if (propagate_mount_busy(real_mount(mnt), 2))
1249 ret = 0;
1250 br_write_unlock(&vfsmount_lock);
1251 up_read(&namespace_sem);
1252 return ret;
1253 }
1254
1255 EXPORT_SYMBOL(may_umount);
1256
1257 static LIST_HEAD(unmounted); /* protected by namespace_sem */
1258
1259 static void namespace_unlock(void)
1260 {
1261 struct mount *mnt;
1262 LIST_HEAD(head);
1263
1264 if (likely(list_empty(&unmounted))) {
1265 up_write(&namespace_sem);
1266 return;
1267 }
1268
1269 list_splice_init(&unmounted, &head);
1270 up_write(&namespace_sem);
1271
1272 while (!list_empty(&head)) {
1273 mnt = list_first_entry(&head, struct mount, mnt_hash);
1274 list_del_init(&mnt->mnt_hash);
1275 if (mnt_has_parent(mnt)) {
1276 struct dentry *dentry;
1277 struct mount *m;
1278
1279 br_write_lock(&vfsmount_lock);
1280 dentry = mnt->mnt_mountpoint;
1281 m = mnt->mnt_parent;
1282 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1283 mnt->mnt_parent = mnt;
1284 m->mnt_ghosts--;
1285 br_write_unlock(&vfsmount_lock);
1286 dput(dentry);
1287 mntput(&m->mnt);
1288 }
1289 mntput(&mnt->mnt);
1290 }
1291 }
1292
1293 static inline void namespace_lock(void)
1294 {
1295 down_write(&namespace_sem);
1296 }
1297
1298 /*
1299 * vfsmount lock must be held for write
1300 * namespace_sem must be held for write
1301 */
1302 void umount_tree(struct mount *mnt, int propagate)
1303 {
1304 LIST_HEAD(tmp_list);
1305 struct mount *p;
1306
1307 for (p = mnt; p; p = next_mnt(p, mnt))
1308 list_move(&p->mnt_hash, &tmp_list);
1309
1310 if (propagate)
1311 propagate_umount(&tmp_list);
1312
1313 list_for_each_entry(p, &tmp_list, mnt_hash) {
1314 list_del_init(&p->mnt_expire);
1315 list_del_init(&p->mnt_list);
1316 __touch_mnt_namespace(p->mnt_ns);
1317 p->mnt_ns = NULL;
1318 list_del_init(&p->mnt_child);
1319 if (mnt_has_parent(p)) {
1320 p->mnt_parent->mnt_ghosts++;
1321 put_mountpoint(p->mnt_mp);
1322 p->mnt_mp = NULL;
1323 }
1324 change_mnt_propagation(p, MS_PRIVATE);
1325 }
1326 list_splice(&tmp_list, &unmounted);
1327 }
1328
1329 static void shrink_submounts(struct mount *mnt);
1330
1331 static int do_umount(struct mount *mnt, int flags)
1332 {
1333 struct super_block *sb = mnt->mnt.mnt_sb;
1334 int retval;
1335
1336 retval = security_sb_umount(&mnt->mnt, flags);
1337 if (retval)
1338 return retval;
1339
1340 /*
1341 * Allow userspace to request a mountpoint be expired rather than
1342 * unmounting unconditionally. Unmount only happens if:
1343 * (1) the mark is already set (the mark is cleared by mntput())
1344 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1345 */
1346 if (flags & MNT_EXPIRE) {
1347 if (&mnt->mnt == current->fs->root.mnt ||
1348 flags & (MNT_FORCE | MNT_DETACH))
1349 return -EINVAL;
1350
1351 /*
1352 * probably don't strictly need the lock here if we examined
1353 * all race cases, but it's a slowpath.
1354 */
1355 br_write_lock(&vfsmount_lock);
1356 if (mnt_get_count(mnt) != 2) {
1357 br_write_unlock(&vfsmount_lock);
1358 return -EBUSY;
1359 }
1360 br_write_unlock(&vfsmount_lock);
1361
1362 if (!xchg(&mnt->mnt_expiry_mark, 1))
1363 return -EAGAIN;
1364 }
1365
1366 /*
1367 * If we may have to abort operations to get out of this
1368 * mount, and they will themselves hold resources we must
1369 * allow the fs to do things. In the Unix tradition of
1370 * 'Gee thats tricky lets do it in userspace' the umount_begin
1371 * might fail to complete on the first run through as other tasks
1372 * must return, and the like. Thats for the mount program to worry
1373 * about for the moment.
1374 */
1375
1376 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1377 sb->s_op->umount_begin(sb);
1378 }
1379
1380 /*
1381 * No sense to grab the lock for this test, but test itself looks
1382 * somewhat bogus. Suggestions for better replacement?
1383 * Ho-hum... In principle, we might treat that as umount + switch
1384 * to rootfs. GC would eventually take care of the old vfsmount.
1385 * Actually it makes sense, especially if rootfs would contain a
1386 * /reboot - static binary that would close all descriptors and
1387 * call reboot(9). Then init(8) could umount root and exec /reboot.
1388 */
1389 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1390 /*
1391 * Special case for "unmounting" root ...
1392 * we just try to remount it readonly.
1393 */
1394 down_write(&sb->s_umount);
1395 if (!(sb->s_flags & MS_RDONLY))
1396 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
1397 up_write(&sb->s_umount);
1398 return retval;
1399 }
1400
1401 namespace_lock();
1402 br_write_lock(&vfsmount_lock);
1403 event++;
1404
1405 if (!(flags & MNT_DETACH))
1406 shrink_submounts(mnt);
1407
1408 retval = -EBUSY;
1409 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
1410 if (!list_empty(&mnt->mnt_list))
1411 umount_tree(mnt, 1);
1412 retval = 0;
1413 }
1414 br_write_unlock(&vfsmount_lock);
1415 namespace_unlock();
1416 return retval;
1417 }
1418
1419 /*
1420 * Is the caller allowed to modify his namespace?
1421 */
1422 static inline bool may_mount(void)
1423 {
1424 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1425 }
1426
1427 /*
1428 * Now umount can handle mount points as well as block devices.
1429 * This is important for filesystems which use unnamed block devices.
1430 *
1431 * We now support a flag for forced unmount like the other 'big iron'
1432 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1433 */
1434
1435 SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1436 {
1437 struct path path;
1438 struct mount *mnt;
1439 int retval;
1440 int lookup_flags = 0;
1441 #ifdef UMOUNT_LOG
1442 int total_value =0;
1443 #endif
1444 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1445 return -EINVAL;
1446
1447 if (!may_mount())
1448 return -EPERM;
1449
1450 if (!(flags & UMOUNT_NOFOLLOW))
1451 lookup_flags |= LOOKUP_FOLLOW;
1452
1453 retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
1454 if (retval)
1455 goto out;
1456 mnt = real_mount(path.mnt);
1457 retval = -EINVAL;
1458 if (path.dentry != path.mnt->mnt_root)
1459 goto dput_and_out;
1460 if (!check_mnt(mnt))
1461 goto dput_and_out;
1462
1463 retval = do_umount(mnt, flags);
1464 #ifdef UMOUNT_LOG
1465 {
1466 printk("Ahsin do_umount retval=%d \n",retval);
1467 //do_umount success: 0, do_umount busy: -16
1468 //if do_umount fail, need to dump the link list here
1469
1470 if(retval)
1471 printk("Ahsin do_umount fail; mnt_get_count=%d mnt->mnt_devname=%s\n",mnt_get_count(mnt),mnt->mnt_devname);
1472 else
1473 printk("Ahsin do_umount success; mnt_get_count=%d mnt->mnt_devname=%s\n",mnt_get_count(mnt),mnt->mnt_devname);
1474
1475 // print linked list
1476 spin_lock(&mnt_id_lock);
1477 ref_current = ref_head;
1478 while(ref_current != NULL)
1479 {
1480 total_value = total_value + ref_current->count;
1481
1482 if (ref_current->count)
1483 printk("Ahsin PID= %d, Name = %s, Count= %d \n", ref_current->pid, ref_current->name, ref_current->count);
1484 ref_current = ref_current->next;
1485 }
1486 spin_unlock(&mnt_id_lock);
1487 printk("Ahsin total_value=%d \n",total_value);
1488 }
1489 #endif
1490 dput_and_out:
1491 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
1492 dput(path.dentry);
1493 mntput_no_expire(mnt);
1494 out:
1495 return retval;
1496 }
1497
1498 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
1499
1500 /*
1501 * The 2.0 compatible umount. No flags.
1502 */
1503 SYSCALL_DEFINE1(oldumount, char __user *, name)
1504 {
1505 return sys_umount(name, 0);
1506 }
1507
1508 #endif
1509
1510 static bool mnt_ns_loop(struct path *path)
1511 {
1512 /* Could bind mounting the mount namespace inode cause a
1513 * mount namespace loop?
1514 */
1515 struct inode *inode = path->dentry->d_inode;
1516 struct proc_ns *ei;
1517 struct mnt_namespace *mnt_ns;
1518
1519 if (!proc_ns_inode(inode))
1520 return false;
1521
1522 ei = get_proc_ns(inode);
1523 if (ei->ns_ops != &mntns_operations)
1524 return false;
1525
1526 mnt_ns = ei->ns;
1527 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1528 }
1529
1530 struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
1531 int flag)
1532 {
1533 struct mount *res, *p, *q, *r, *parent;
1534
1535 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
1536 return ERR_PTR(-EINVAL);
1537
1538 res = q = clone_mnt(mnt, dentry, flag);
1539 if (IS_ERR(q))
1540 return q;
1541
1542 q->mnt_mountpoint = mnt->mnt_mountpoint;
1543
1544 p = mnt;
1545 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1546 struct mount *s;
1547 if (!is_subdir(r->mnt_mountpoint, dentry))
1548 continue;
1549
1550 for (s = r; s; s = next_mnt(s, r)) {
1551 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
1552 s = skip_mnt_tree(s);
1553 continue;
1554 }
1555 while (p != s->mnt_parent) {
1556 p = p->mnt_parent;
1557 q = q->mnt_parent;
1558 }
1559 p = s;
1560 parent = q;
1561 q = clone_mnt(p, p->mnt.mnt_root, flag);
1562 if (IS_ERR(q))
1563 goto out;
1564 br_write_lock(&vfsmount_lock);
1565 list_add_tail(&q->mnt_list, &res->mnt_list);
1566 attach_mnt(q, parent, p->mnt_mp);
1567 br_write_unlock(&vfsmount_lock);
1568 }
1569 }
1570 return res;
1571 out:
1572 if (res) {
1573 br_write_lock(&vfsmount_lock);
1574 umount_tree(res, 0);
1575 br_write_unlock(&vfsmount_lock);
1576 }
1577 return q;
1578 }
1579
1580 /* Caller should check returned pointer for errors */
1581
1582 struct vfsmount *collect_mounts(struct path *path)
1583 {
1584 struct mount *tree;
1585 namespace_lock();
1586 tree = copy_tree(real_mount(path->mnt), path->dentry,
1587 CL_COPY_ALL | CL_PRIVATE);
1588 namespace_unlock();
1589 if (IS_ERR(tree))
1590 return ERR_CAST(tree);
1591 return &tree->mnt;
1592 }
1593
1594 void drop_collected_mounts(struct vfsmount *mnt)
1595 {
1596 namespace_lock();
1597 br_write_lock(&vfsmount_lock);
1598 umount_tree(real_mount(mnt), 0);
1599 br_write_unlock(&vfsmount_lock);
1600 namespace_unlock();
1601 }
1602
1603 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1604 struct vfsmount *root)
1605 {
1606 struct mount *mnt;
1607 int res = f(root, arg);
1608 if (res)
1609 return res;
1610 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1611 res = f(&mnt->mnt, arg);
1612 if (res)
1613 return res;
1614 }
1615 return 0;
1616 }
1617
1618 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1619 {
1620 struct mount *p;
1621
1622 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1623 if (p->mnt_group_id && !IS_MNT_SHARED(p))
1624 mnt_release_group_id(p);
1625 }
1626 }
1627
1628 static int invent_group_ids(struct mount *mnt, bool recurse)
1629 {
1630 struct mount *p;
1631
1632 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1633 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1634 int err = mnt_alloc_group_id(p);
1635 if (err) {
1636 cleanup_group_ids(mnt, p);
1637 return err;
1638 }
1639 }
1640 }
1641
1642 return 0;
1643 }
1644
1645 /*
1646 * @source_mnt : mount tree to be attached
1647 * @nd : place the mount tree @source_mnt is attached
1648 * @parent_nd : if non-null, detach the source_mnt from its parent and
1649 * store the parent mount and mountpoint dentry.
1650 * (done when source_mnt is moved)
1651 *
1652 * NOTE: in the table below explains the semantics when a source mount
1653 * of a given type is attached to a destination mount of a given type.
1654 * ---------------------------------------------------------------------------
1655 * | BIND MOUNT OPERATION |
1656 * |**************************************************************************
1657 * | source-->| shared | private | slave | unbindable |
1658 * | dest | | | | |
1659 * | | | | | | |
1660 * | v | | | | |
1661 * |**************************************************************************
1662 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1663 * | | | | | |
1664 * |non-shared| shared (+) | private | slave (*) | invalid |
1665 * ***************************************************************************
1666 * A bind operation clones the source mount and mounts the clone on the
1667 * destination mount.
1668 *
1669 * (++) the cloned mount is propagated to all the mounts in the propagation
1670 * tree of the destination mount and the cloned mount is added to
1671 * the peer group of the source mount.
1672 * (+) the cloned mount is created under the destination mount and is marked
1673 * as shared. The cloned mount is added to the peer group of the source
1674 * mount.
1675 * (+++) the mount is propagated to all the mounts in the propagation tree
1676 * of the destination mount and the cloned mount is made slave
1677 * of the same master as that of the source mount. The cloned mount
1678 * is marked as 'shared and slave'.
1679 * (*) the cloned mount is made a slave of the same master as that of the
1680 * source mount.
1681 *
1682 * ---------------------------------------------------------------------------
1683 * | MOVE MOUNT OPERATION |
1684 * |**************************************************************************
1685 * | source-->| shared | private | slave | unbindable |
1686 * | dest | | | | |
1687 * | | | | | | |
1688 * | v | | | | |
1689 * |**************************************************************************
1690 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1691 * | | | | | |
1692 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1693 * ***************************************************************************
1694 *
1695 * (+) the mount is moved to the destination. And is then propagated to
1696 * all the mounts in the propagation tree of the destination mount.
1697 * (+*) the mount is moved to the destination.
1698 * (+++) the mount is moved to the destination and is then propagated to
1699 * all the mounts belonging to the destination mount's propagation tree.
1700 * the mount is marked as 'shared and slave'.
1701 * (*) the mount continues to be a slave at the new location.
1702 *
1703 * if the source mount is a tree, the operations explained above is
1704 * applied to each mount in the tree.
1705 * Must be called without spinlocks held, since this function can sleep
1706 * in allocations.
1707 */
1708 static int attach_recursive_mnt(struct mount *source_mnt,
1709 struct mount *dest_mnt,
1710 struct mountpoint *dest_mp,
1711 struct path *parent_path)
1712 {
1713 LIST_HEAD(tree_list);
1714 struct mount *child, *p;
1715 int err;
1716
1717 if (IS_MNT_SHARED(dest_mnt)) {
1718 err = invent_group_ids(source_mnt, true);
1719 if (err)
1720 goto out;
1721 }
1722 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1723 if (err)
1724 goto out_cleanup_ids;
1725
1726 br_write_lock(&vfsmount_lock);
1727
1728 if (IS_MNT_SHARED(dest_mnt)) {
1729 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1730 set_mnt_shared(p);
1731 }
1732 if (parent_path) {
1733 detach_mnt(source_mnt, parent_path);
1734 attach_mnt(source_mnt, dest_mnt, dest_mp);
1735 touch_mnt_namespace(source_mnt->mnt_ns);
1736 } else {
1737 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
1738 commit_tree(source_mnt);
1739 }
1740
1741 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1742 list_del_init(&child->mnt_hash);
1743 commit_tree(child);
1744 }
1745 br_write_unlock(&vfsmount_lock);
1746
1747 return 0;
1748
1749 out_cleanup_ids:
1750 if (IS_MNT_SHARED(dest_mnt))
1751 cleanup_group_ids(source_mnt, NULL);
1752 out:
1753 return err;
1754 }
1755
1756 static struct mountpoint *lock_mount(struct path *path)
1757 {
1758 struct vfsmount *mnt;
1759 struct dentry *dentry = path->dentry;
1760 retry:
1761 mutex_lock(&dentry->d_inode->i_mutex);
1762 if (unlikely(cant_mount(dentry))) {
1763 mutex_unlock(&dentry->d_inode->i_mutex);
1764 return ERR_PTR(-ENOENT);
1765 }
1766 namespace_lock();
1767 mnt = lookup_mnt(path);
1768 if (likely(!mnt)) {
1769 struct mountpoint *mp = new_mountpoint(dentry);
1770 if (IS_ERR(mp)) {
1771 namespace_unlock();
1772 mutex_unlock(&dentry->d_inode->i_mutex);
1773 return mp;
1774 }
1775 return mp;
1776 }
1777 namespace_unlock();
1778 mutex_unlock(&path->dentry->d_inode->i_mutex);
1779 path_put(path);
1780 path->mnt = mnt;
1781 dentry = path->dentry = dget(mnt->mnt_root);
1782 goto retry;
1783 }
1784
1785 static void unlock_mount(struct mountpoint *where)
1786 {
1787 struct dentry *dentry = where->m_dentry;
1788 put_mountpoint(where);
1789 namespace_unlock();
1790 mutex_unlock(&dentry->d_inode->i_mutex);
1791 }
1792
1793 static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
1794 {
1795 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
1796 return -EINVAL;
1797
1798 if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
1799 S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
1800 return -ENOTDIR;
1801
1802 return attach_recursive_mnt(mnt, p, mp, NULL);
1803 }
1804
1805 /*
1806 * Sanity check the flags to change_mnt_propagation.
1807 */
1808
1809 static int flags_to_propagation_type(int flags)
1810 {
1811 int type = flags & ~(MS_REC | MS_SILENT);
1812
1813 /* Fail if any non-propagation flags are set */
1814 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1815 return 0;
1816 /* Only one propagation flag should be set */
1817 if (!is_power_of_2(type))
1818 return 0;
1819 return type;
1820 }
1821
1822 /*
1823 * recursively change the type of the mountpoint.
1824 */
1825 static int do_change_type(struct path *path, int flag)
1826 {
1827 struct mount *m;
1828 struct mount *mnt = real_mount(path->mnt);
1829 int recurse = flag & MS_REC;
1830 int type;
1831 int err = 0;
1832
1833 if (path->dentry != path->mnt->mnt_root)
1834 return -EINVAL;
1835
1836 type = flags_to_propagation_type(flag);
1837 if (!type)
1838 return -EINVAL;
1839
1840 namespace_lock();
1841 if (type == MS_SHARED) {
1842 err = invent_group_ids(mnt, recurse);
1843 if (err)
1844 goto out_unlock;
1845 }
1846
1847 br_write_lock(&vfsmount_lock);
1848 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
1849 change_mnt_propagation(m, type);
1850 br_write_unlock(&vfsmount_lock);
1851
1852 out_unlock:
1853 namespace_unlock();
1854 return err;
1855 }
1856
1857 /*
1858 * do loopback mount.
1859 */
1860 static int do_loopback(struct path *path, const char *old_name,
1861 int recurse)
1862 {
1863 struct path old_path;
1864 struct mount *mnt = NULL, *old, *parent;
1865 struct mountpoint *mp;
1866 int err;
1867 if (!old_name || !*old_name)
1868 return -EINVAL;
1869 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
1870 if (err)
1871 return err;
1872
1873 err = -EINVAL;
1874 if (mnt_ns_loop(&old_path))
1875 goto out;
1876
1877 mp = lock_mount(path);
1878 err = PTR_ERR(mp);
1879 if (IS_ERR(mp))
1880 goto out;
1881
1882 old = real_mount(old_path.mnt);
1883 parent = real_mount(path->mnt);
1884
1885 err = -EINVAL;
1886 if (IS_MNT_UNBINDABLE(old))
1887 goto out2;
1888
1889 if (!check_mnt(parent) || !check_mnt(old))
1890 goto out2;
1891
1892 if (recurse)
1893 mnt = copy_tree(old, old_path.dentry, 0);
1894 else
1895 mnt = clone_mnt(old, old_path.dentry, 0);
1896
1897 if (IS_ERR(mnt)) {
1898 err = PTR_ERR(mnt);
1899 goto out2;
1900 }
1901
1902 err = graft_tree(mnt, parent, mp);
1903 if (err) {
1904 br_write_lock(&vfsmount_lock);
1905 umount_tree(mnt, 0);
1906 br_write_unlock(&vfsmount_lock);
1907 }
1908 out2:
1909 unlock_mount(mp);
1910 out:
1911 path_put(&old_path);
1912 return err;
1913 }
1914
1915 static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1916 {
1917 int error = 0;
1918 int readonly_request = 0;
1919
1920 if (ms_flags & MS_RDONLY)
1921 readonly_request = 1;
1922 if (readonly_request == __mnt_is_readonly(mnt))
1923 return 0;
1924
1925 if (mnt->mnt_flags & MNT_LOCK_READONLY)
1926 return -EPERM;
1927
1928 if (readonly_request)
1929 error = mnt_make_readonly(real_mount(mnt));
1930 else
1931 __mnt_unmake_readonly(real_mount(mnt));
1932 return error;
1933 }
1934
1935 /*
1936 * change filesystem flags. dir should be a physical root of filesystem.
1937 * If you've mounted a non-root directory somewhere and want to do remount
1938 * on it - tough luck.
1939 */
1940 static int do_remount(struct path *path, int flags, int mnt_flags,
1941 void *data)
1942 {
1943 int err;
1944 struct super_block *sb = path->mnt->mnt_sb;
1945 struct mount *mnt = real_mount(path->mnt);
1946
1947 if (!check_mnt(mnt))
1948 return -EINVAL;
1949
1950 if (path->dentry != path->mnt->mnt_root)
1951 return -EINVAL;
1952
1953 err = security_sb_remount(sb, data);
1954 if (err)
1955 return err;
1956
1957 down_write(&sb->s_umount);
1958 if (flags & MS_BIND)
1959 err = change_mount_flags(path->mnt, flags);
1960 else if (!capable(CAP_SYS_ADMIN))
1961 err = -EPERM;
1962 else
1963 err = do_remount_sb(sb, flags, data, 0);
1964 if (!err) {
1965 br_write_lock(&vfsmount_lock);
1966 mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
1967 mnt->mnt.mnt_flags = mnt_flags;
1968 br_write_unlock(&vfsmount_lock);
1969 }
1970 up_write(&sb->s_umount);
1971 if (!err) {
1972 br_write_lock(&vfsmount_lock);
1973 touch_mnt_namespace(mnt->mnt_ns);
1974 br_write_unlock(&vfsmount_lock);
1975 }
1976 return err;
1977 }
1978
1979 static inline int tree_contains_unbindable(struct mount *mnt)
1980 {
1981 struct mount *p;
1982 for (p = mnt; p; p = next_mnt(p, mnt)) {
1983 if (IS_MNT_UNBINDABLE(p))
1984 return 1;
1985 }
1986 return 0;
1987 }
1988
1989 static int do_move_mount(struct path *path, const char *old_name)
1990 {
1991 struct path old_path, parent_path;
1992 struct mount *p;
1993 struct mount *old;
1994 struct mountpoint *mp;
1995 int err;
1996 if (!old_name || !*old_name)
1997 return -EINVAL;
1998 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
1999 if (err)
2000 return err;
2001
2002 mp = lock_mount(path);
2003 err = PTR_ERR(mp);
2004 if (IS_ERR(mp))
2005 goto out;
2006
2007 old = real_mount(old_path.mnt);
2008 p = real_mount(path->mnt);
2009
2010 err = -EINVAL;
2011 if (!check_mnt(p) || !check_mnt(old))
2012 goto out1;
2013
2014 err = -EINVAL;
2015 if (old_path.dentry != old_path.mnt->mnt_root)
2016 goto out1;
2017
2018 if (!mnt_has_parent(old))
2019 goto out1;
2020
2021 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
2022 S_ISDIR(old_path.dentry->d_inode->i_mode))
2023 goto out1;
2024 /*
2025 * Don't move a mount residing in a shared parent.
2026 */
2027 if (IS_MNT_SHARED(old->mnt_parent))
2028 goto out1;
2029 /*
2030 * Don't move a mount tree containing unbindable mounts to a destination
2031 * mount which is shared.
2032 */
2033 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
2034 goto out1;
2035 err = -ELOOP;
2036 for (; mnt_has_parent(p); p = p->mnt_parent)
2037 if (p == old)
2038 goto out1;
2039
2040 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
2041 if (err)
2042 goto out1;
2043
2044 /* if the mount is moved, it should no longer be expire
2045 * automatically */
2046 list_del_init(&old->mnt_expire);
2047 out1:
2048 unlock_mount(mp);
2049 out:
2050 if (!err)
2051 path_put(&parent_path);
2052 path_put(&old_path);
2053 return err;
2054 }
2055
2056 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
2057 {
2058 int err;
2059 const char *subtype = strchr(fstype, '.');
2060 if (subtype) {
2061 subtype++;
2062 err = -EINVAL;
2063 if (!subtype[0])
2064 goto err;
2065 } else
2066 subtype = "";
2067
2068 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
2069 err = -ENOMEM;
2070 if (!mnt->mnt_sb->s_subtype)
2071 goto err;
2072 return mnt;
2073
2074 err:
2075 mntput(mnt);
2076 return ERR_PTR(err);
2077 }
2078
2079 /*
2080 * add a mount into a namespace's mount tree
2081 */
2082 static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
2083 {
2084 struct mountpoint *mp;
2085 struct mount *parent;
2086 int err;
2087
2088 mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
2089
2090 mp = lock_mount(path);
2091 if (IS_ERR(mp))
2092 return PTR_ERR(mp);
2093
2094 parent = real_mount(path->mnt);
2095 err = -EINVAL;
2096 if (unlikely(!check_mnt(parent))) {
2097 /* that's acceptable only for automounts done in private ns */
2098 if (!(mnt_flags & MNT_SHRINKABLE))
2099 goto unlock;
2100 /* ... and for those we'd better have mountpoint still alive */
2101 if (!parent->mnt_ns)
2102 goto unlock;
2103 }
2104
2105 /* Refuse the same filesystem on the same mount point */
2106 err = -EBUSY;
2107 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
2108 path->mnt->mnt_root == path->dentry)
2109 goto unlock;
2110
2111 err = -EINVAL;
2112 if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
2113 goto unlock;
2114
2115 newmnt->mnt.mnt_flags = mnt_flags;
2116 err = graft_tree(newmnt, parent, mp);
2117
2118 unlock:
2119 unlock_mount(mp);
2120 return err;
2121 }
2122
2123 /*
2124 * create a new mount for userspace and request it to be added into the
2125 * namespace's tree
2126 */
2127 static int do_new_mount(struct path *path, const char *fstype, int flags,
2128 int mnt_flags, const char *name, void *data)
2129 {
2130 struct file_system_type *type;
2131 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2132 struct vfsmount *mnt;
2133 int err;
2134
2135 if (!fstype)
2136 return -EINVAL;
2137
2138 type = get_fs_type(fstype);
2139 if (!type)
2140 return -ENODEV;
2141
2142 if (user_ns != &init_user_ns) {
2143 if (!(type->fs_flags & FS_USERNS_MOUNT)) {
2144 put_filesystem(type);
2145 return -EPERM;
2146 }
2147 /* Only in special cases allow devices from mounts
2148 * created outside the initial user namespace.
2149 */
2150 if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
2151 flags |= MS_NODEV;
2152 mnt_flags |= MNT_NODEV;
2153 }
2154 }
2155
2156 mnt = vfs_kern_mount(type, flags, name, data);
2157 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2158 !mnt->mnt_sb->s_subtype)
2159 mnt = fs_set_subtype(mnt, fstype);
2160
2161 put_filesystem(type);
2162 if (IS_ERR(mnt))
2163 return PTR_ERR(mnt);
2164
2165 err = do_add_mount(real_mount(mnt), path, mnt_flags);
2166 if (err)
2167 mntput(mnt);
2168 return err;
2169 }
2170
2171 int finish_automount(struct vfsmount *m, struct path *path)
2172 {
2173 struct mount *mnt = real_mount(m);
2174 int err;
2175 /* The new mount record should have at least 2 refs to prevent it being
2176 * expired before we get a chance to add it
2177 */
2178 BUG_ON(mnt_get_count(mnt) < 2);
2179
2180 if (m->mnt_sb == path->mnt->mnt_sb &&
2181 m->mnt_root == path->dentry) {
2182 err = -ELOOP;
2183 goto fail;
2184 }
2185
2186 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2187 if (!err)
2188 return 0;
2189 fail:
2190 /* remove m from any expiration list it may be on */
2191 if (!list_empty(&mnt->mnt_expire)) {
2192 namespace_lock();
2193 br_write_lock(&vfsmount_lock);
2194 list_del_init(&mnt->mnt_expire);
2195 br_write_unlock(&vfsmount_lock);
2196 namespace_unlock();
2197 }
2198 mntput(m);
2199 mntput(m);
2200 return err;
2201 }
2202
2203 /**
2204 * mnt_set_expiry - Put a mount on an expiration list
2205 * @mnt: The mount to list.
2206 * @expiry_list: The list to add the mount to.
2207 */
2208 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2209 {
2210 namespace_lock();
2211 br_write_lock(&vfsmount_lock);
2212
2213 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2214
2215 br_write_unlock(&vfsmount_lock);
2216 namespace_unlock();
2217 }
2218 EXPORT_SYMBOL(mnt_set_expiry);
2219
2220 /*
2221 * process a list of expirable mountpoints with the intent of discarding any
2222 * mountpoints that aren't in use and haven't been touched since last we came
2223 * here
2224 */
2225 void mark_mounts_for_expiry(struct list_head *mounts)
2226 {
2227 struct mount *mnt, *next;
2228 LIST_HEAD(graveyard);
2229
2230 if (list_empty(mounts))
2231 return;
2232
2233 namespace_lock();
2234 br_write_lock(&vfsmount_lock);
2235
2236 /* extract from the expiration list every vfsmount that matches the
2237 * following criteria:
2238 * - only referenced by its parent vfsmount
2239 * - still marked for expiry (marked on the last call here; marks are
2240 * cleared by mntput())
2241 */
2242 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2243 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
2244 propagate_mount_busy(mnt, 1))
2245 continue;
2246 list_move(&mnt->mnt_expire, &graveyard);
2247 }
2248 while (!list_empty(&graveyard)) {
2249 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2250 touch_mnt_namespace(mnt->mnt_ns);
2251 umount_tree(mnt, 1);
2252 }
2253 br_write_unlock(&vfsmount_lock);
2254 namespace_unlock();
2255 }
2256
2257 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2258
2259 /*
2260 * Ripoff of 'select_parent()'
2261 *
2262 * search the list of submounts for a given mountpoint, and move any
2263 * shrinkable submounts to the 'graveyard' list.
2264 */
2265 static int select_submounts(struct mount *parent, struct list_head *graveyard)
2266 {
2267 struct mount *this_parent = parent;
2268 struct list_head *next;
2269 int found = 0;
2270
2271 repeat:
2272 next = this_parent->mnt_mounts.next;
2273 resume:
2274 while (next != &this_parent->mnt_mounts) {
2275 struct list_head *tmp = next;
2276 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
2277
2278 next = tmp->next;
2279 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
2280 continue;
2281 /*
2282 * Descend a level if the d_mounts list is non-empty.
2283 */
2284 if (!list_empty(&mnt->mnt_mounts)) {
2285 this_parent = mnt;
2286 goto repeat;
2287 }
2288
2289 if (!propagate_mount_busy(mnt, 1)) {
2290 list_move_tail(&mnt->mnt_expire, graveyard);
2291 found++;
2292 }
2293 }
2294 /*
2295 * All done at this level ... ascend and resume the search
2296 */
2297 if (this_parent != parent) {
2298 next = this_parent->mnt_child.next;
2299 this_parent = this_parent->mnt_parent;
2300 goto resume;
2301 }
2302 return found;
2303 }
2304
2305 /*
2306 * process a list of expirable mountpoints with the intent of discarding any
2307 * submounts of a specific parent mountpoint
2308 *
2309 * vfsmount_lock must be held for write
2310 */
2311 static void shrink_submounts(struct mount *mnt)
2312 {
2313 LIST_HEAD(graveyard);
2314 struct mount *m;
2315
2316 /* extract submounts of 'mountpoint' from the expiration list */
2317 while (select_submounts(mnt, &graveyard)) {
2318 while (!list_empty(&graveyard)) {
2319 m = list_first_entry(&graveyard, struct mount,
2320 mnt_expire);
2321 touch_mnt_namespace(m->mnt_ns);
2322 umount_tree(m, 1);
2323 }
2324 }
2325 }
2326
2327 /*
2328 * Some copy_from_user() implementations do not return the exact number of
2329 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2330 * Note that this function differs from copy_from_user() in that it will oops
2331 * on bad values of `to', rather than returning a short copy.
2332 */
2333 static long exact_copy_from_user(void *to, const void __user * from,
2334 unsigned long n)
2335 {
2336 char *t = to;
2337 const char __user *f = from;
2338 char c;
2339
2340 if (!access_ok(VERIFY_READ, from, n))
2341 return n;
2342
2343 while (n) {
2344 if (__get_user(c, f)) {
2345 memset(t, 0, n);
2346 break;
2347 }
2348 *t++ = c;
2349 f++;
2350 n--;
2351 }
2352 return n;
2353 }
2354
2355 int copy_mount_options(const void __user * data, unsigned long *where)
2356 {
2357 int i;
2358 unsigned long page;
2359 unsigned long size;
2360
2361 *where = 0;
2362 if (!data)
2363 return 0;
2364
2365 if (!(page = __get_free_page(GFP_KERNEL)))
2366 return -ENOMEM;
2367
2368 /* We only care that *some* data at the address the user
2369 * gave us is valid. Just in case, we'll zero
2370 * the remainder of the page.
2371 */
2372 /* copy_from_user cannot cross TASK_SIZE ! */
2373 size = TASK_SIZE - (unsigned long)data;
2374 if (size > PAGE_SIZE)
2375 size = PAGE_SIZE;
2376
2377 i = size - exact_copy_from_user((void *)page, data, size);
2378 if (!i) {
2379 free_page(page);
2380 return -EFAULT;
2381 }
2382 if (i != PAGE_SIZE)
2383 memset((char *)page + i, 0, PAGE_SIZE - i);
2384 *where = page;
2385 return 0;
2386 }
2387
2388 int copy_mount_string(const void __user *data, char **where)
2389 {
2390 char *tmp;
2391
2392 if (!data) {
2393 *where = NULL;
2394 return 0;
2395 }
2396
2397 tmp = strndup_user(data, PAGE_SIZE);
2398 if (IS_ERR(tmp))
2399 return PTR_ERR(tmp);
2400
2401 *where = tmp;
2402 return 0;
2403 }
2404
2405 /*
2406 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2407 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2408 *
2409 * data is a (void *) that can point to any structure up to
2410 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2411 * information (or be NULL).
2412 *
2413 * Pre-0.97 versions of mount() didn't have a flags word.
2414 * When the flags word was introduced its top half was required
2415 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2416 * Therefore, if this magic number is present, it carries no information
2417 * and must be discarded.
2418 */
2419 long do_mount(const char *dev_name, const char *dir_name,
2420 const char *type_page, unsigned long flags, void *data_page)
2421 {
2422 struct path path;
2423 int retval = 0;
2424 int mnt_flags = 0;
2425
2426 /* Discard magic */
2427 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2428 flags &= ~MS_MGC_MSK;
2429
2430 /* Basic sanity checks */
2431
2432 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2433 return -EINVAL;
2434
2435 if (data_page)
2436 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2437
2438 /* ... and get the mountpoint */
2439 retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2440 if (retval)
2441 return retval;
2442
2443 retval = security_sb_mount(dev_name, &path,
2444 type_page, flags, data_page);
2445 if (!retval && !may_mount())
2446 retval = -EPERM;
2447 if (retval)
2448 goto dput_out;
2449
2450 /* Default to relatime unless overriden */
2451 if (!(flags & MS_NOATIME))
2452 mnt_flags |= MNT_RELATIME;
2453
2454 /* Separate the per-mountpoint flags */
2455 if (flags & MS_NOSUID)
2456 mnt_flags |= MNT_NOSUID;
2457 if (flags & MS_NODEV)
2458 mnt_flags |= MNT_NODEV;
2459 if (flags & MS_NOEXEC)
2460 mnt_flags |= MNT_NOEXEC;
2461 if (flags & MS_NOATIME)
2462 mnt_flags |= MNT_NOATIME;
2463 if (flags & MS_NODIRATIME)
2464 mnt_flags |= MNT_NODIRATIME;
2465 if (flags & MS_STRICTATIME)
2466 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2467 if (flags & MS_RDONLY)
2468 mnt_flags |= MNT_READONLY;
2469
2470 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2471 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2472 MS_STRICTATIME);
2473
2474 if (flags & MS_REMOUNT)
2475 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
2476 data_page);
2477 else if (flags & MS_BIND)
2478 retval = do_loopback(&path, dev_name, flags & MS_REC);
2479 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2480 retval = do_change_type(&path, flags);
2481 else if (flags & MS_MOVE)
2482 retval = do_move_mount(&path, dev_name);
2483 else
2484 retval = do_new_mount(&path, type_page, flags, mnt_flags,
2485 dev_name, data_page);
2486 dput_out:
2487 path_put(&path);
2488 return retval;
2489 }
2490
2491 static void free_mnt_ns(struct mnt_namespace *ns)
2492 {
2493 proc_free_inum(ns->proc_inum);
2494 put_user_ns(ns->user_ns);
2495 kfree(ns);
2496 }
2497
2498 /*
2499 * Assign a sequence number so we can detect when we attempt to bind
2500 * mount a reference to an older mount namespace into the current
2501 * mount namespace, preventing reference counting loops. A 64bit
2502 * number incrementing at 10Ghz will take 12,427 years to wrap which
2503 * is effectively never, so we can ignore the possibility.
2504 */
2505 static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2506
2507 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2508 {
2509 struct mnt_namespace *new_ns;
2510 int ret;
2511
2512 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2513 if (!new_ns)
2514 return ERR_PTR(-ENOMEM);
2515 ret = proc_alloc_inum(&new_ns->proc_inum);
2516 if (ret) {
2517 kfree(new_ns);
2518 return ERR_PTR(ret);
2519 }
2520 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2521 atomic_set(&new_ns->count, 1);
2522 new_ns->root = NULL;
2523 INIT_LIST_HEAD(&new_ns->list);
2524 init_waitqueue_head(&new_ns->poll);
2525 new_ns->event = 0;
2526 new_ns->user_ns = get_user_ns(user_ns);
2527 return new_ns;
2528 }
2529
2530 /*
2531 * Allocate a new namespace structure and populate it with contents
2532 * copied from the namespace of the passed in task structure.
2533 */
2534 static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
2535 struct user_namespace *user_ns, struct fs_struct *fs)
2536 {
2537 struct mnt_namespace *new_ns;
2538 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2539 struct mount *p, *q;
2540 struct mount *old = mnt_ns->root;
2541 struct mount *new;
2542 int copy_flags;
2543
2544 new_ns = alloc_mnt_ns(user_ns);
2545 if (IS_ERR(new_ns))
2546 return new_ns;
2547
2548 namespace_lock();
2549 /* First pass: copy the tree topology */
2550 copy_flags = CL_COPY_ALL | CL_EXPIRE;
2551 if (user_ns != mnt_ns->user_ns)
2552 copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
2553 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2554 if (IS_ERR(new)) {
2555 namespace_unlock();
2556 free_mnt_ns(new_ns);
2557 return ERR_CAST(new);
2558 }
2559 new_ns->root = new;
2560 br_write_lock(&vfsmount_lock);
2561 list_add_tail(&new_ns->list, &new->mnt_list);
2562 br_write_unlock(&vfsmount_lock);
2563
2564 /*
2565 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2566 * as belonging to new namespace. We have already acquired a private
2567 * fs_struct, so tsk->fs->lock is not needed.
2568 */
2569 p = old;
2570 q = new;
2571 while (p) {
2572 q->mnt_ns = new_ns;
2573 if (fs) {
2574 if (&p->mnt == fs->root.mnt) {
2575 fs->root.mnt = mntget(&q->mnt);
2576 rootmnt = &p->mnt;
2577 }
2578 if (&p->mnt == fs->pwd.mnt) {
2579 fs->pwd.mnt = mntget(&q->mnt);
2580 pwdmnt = &p->mnt;
2581 }
2582 }
2583 p = next_mnt(p, old);
2584 q = next_mnt(q, new);
2585 }
2586 namespace_unlock();
2587
2588 if (rootmnt)
2589 mntput(rootmnt);
2590 if (pwdmnt)
2591 mntput(pwdmnt);
2592
2593 return new_ns;
2594 }
2595
2596 struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2597 struct user_namespace *user_ns, struct fs_struct *new_fs)
2598 {
2599 struct mnt_namespace *new_ns;
2600
2601 BUG_ON(!ns);
2602 get_mnt_ns(ns);
2603
2604 if (!(flags & CLONE_NEWNS))
2605 return ns;
2606
2607 new_ns = dup_mnt_ns(ns, user_ns, new_fs);
2608
2609 put_mnt_ns(ns);
2610 return new_ns;
2611 }
2612
2613 /**
2614 * create_mnt_ns - creates a private namespace and adds a root filesystem
2615 * @mnt: pointer to the new root filesystem mountpoint
2616 */
2617 static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2618 {
2619 struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2620 if (!IS_ERR(new_ns)) {
2621 struct mount *mnt = real_mount(m);
2622 mnt->mnt_ns = new_ns;
2623 new_ns->root = mnt;
2624 list_add(&mnt->mnt_list, &new_ns->list);
2625 } else {
2626 mntput(m);
2627 }
2628 return new_ns;
2629 }
2630
2631 struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2632 {
2633 struct mnt_namespace *ns;
2634 struct super_block *s;
2635 struct path path;
2636 int err;
2637
2638 ns = create_mnt_ns(mnt);
2639 if (IS_ERR(ns))
2640 return ERR_CAST(ns);
2641
2642 err = vfs_path_lookup(mnt->mnt_root, mnt,
2643 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2644
2645 put_mnt_ns(ns);
2646
2647 if (err)
2648 return ERR_PTR(err);
2649
2650 /* trade a vfsmount reference for active sb one */
2651 s = path.mnt->mnt_sb;
2652 atomic_inc(&s->s_active);
2653 mntput(path.mnt);
2654 /* lock the sucker */
2655 down_write(&s->s_umount);
2656 /* ... and return the root of (sub)tree on it */
2657 return path.dentry;
2658 }
2659 EXPORT_SYMBOL(mount_subtree);
2660
2661 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2662 char __user *, type, unsigned long, flags, void __user *, data)
2663 {
2664 int ret;
2665 char *kernel_type;
2666 struct filename *kernel_dir;
2667 char *kernel_dev;
2668 unsigned long data_page;
2669
2670 ret = copy_mount_string(type, &kernel_type);
2671 if (ret < 0)
2672 goto out_type;
2673
2674 kernel_dir = getname(dir_name);
2675 if (IS_ERR(kernel_dir)) {
2676 ret = PTR_ERR(kernel_dir);
2677 goto out_dir;
2678 }
2679
2680 ret = copy_mount_string(dev_name, &kernel_dev);
2681 if (ret < 0)
2682 goto out_dev;
2683
2684 ret = copy_mount_options(data, &data_page);
2685 if (ret < 0)
2686 goto out_data;
2687
2688 ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
2689 (void *) data_page);
2690
2691 free_page(data_page);
2692 out_data:
2693 kfree(kernel_dev);
2694 out_dev:
2695 putname(kernel_dir);
2696 out_dir:
2697 kfree(kernel_type);
2698 out_type:
2699 return ret;
2700 }
2701
2702 /*
2703 * Return true if path is reachable from root
2704 *
2705 * namespace_sem or vfsmount_lock is held
2706 */
2707 bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2708 const struct path *root)
2709 {
2710 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2711 dentry = mnt->mnt_mountpoint;
2712 mnt = mnt->mnt_parent;
2713 }
2714 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2715 }
2716
2717 int path_is_under(struct path *path1, struct path *path2)
2718 {
2719 int res;
2720 br_read_lock(&vfsmount_lock);
2721 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
2722 br_read_unlock(&vfsmount_lock);
2723 return res;
2724 }
2725 EXPORT_SYMBOL(path_is_under);
2726
2727 /*
2728 * pivot_root Semantics:
2729 * Moves the root file system of the current process to the directory put_old,
2730 * makes new_root as the new root file system of the current process, and sets
2731 * root/cwd of all processes which had them on the current root to new_root.
2732 *
2733 * Restrictions:
2734 * The new_root and put_old must be directories, and must not be on the
2735 * same file system as the current process root. The put_old must be
2736 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2737 * pointed to by put_old must yield the same directory as new_root. No other
2738 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2739 *
2740 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2741 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2742 * in this situation.
2743 *
2744 * Notes:
2745 * - we don't move root/cwd if they are not at the root (reason: if something
2746 * cared enough to change them, it's probably wrong to force them elsewhere)
2747 * - it's okay to pick a root that isn't the root of a file system, e.g.
2748 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2749 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2750 * first.
2751 */
2752 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2753 const char __user *, put_old)
2754 {
2755 struct path new, old, parent_path, root_parent, root;
2756 struct mount *new_mnt, *root_mnt, *old_mnt;
2757 struct mountpoint *old_mp, *root_mp;
2758 int error;
2759
2760 if (!may_mount())
2761 return -EPERM;
2762
2763 error = user_path_dir(new_root, &new);
2764 if (error)
2765 goto out0;
2766
2767 error = user_path_dir(put_old, &old);
2768 if (error)
2769 goto out1;
2770
2771 error = security_sb_pivotroot(&old, &new);
2772 if (error)
2773 goto out2;
2774
2775 get_fs_root(current->fs, &root);
2776 old_mp = lock_mount(&old);
2777 error = PTR_ERR(old_mp);
2778 if (IS_ERR(old_mp))
2779 goto out3;
2780
2781 error = -EINVAL;
2782 new_mnt = real_mount(new.mnt);
2783 root_mnt = real_mount(root.mnt);
2784 old_mnt = real_mount(old.mnt);
2785 if (IS_MNT_SHARED(old_mnt) ||
2786 IS_MNT_SHARED(new_mnt->mnt_parent) ||
2787 IS_MNT_SHARED(root_mnt->mnt_parent))
2788 goto out4;
2789 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2790 goto out4;
2791 error = -ENOENT;
2792 if (d_unlinked(new.dentry))
2793 goto out4;
2794 error = -EBUSY;
2795 if (new_mnt == root_mnt || old_mnt == root_mnt)
2796 goto out4; /* loop, on the same file system */
2797 error = -EINVAL;
2798 if (root.mnt->mnt_root != root.dentry)
2799 goto out4; /* not a mountpoint */
2800 if (!mnt_has_parent(root_mnt))
2801 goto out4; /* not attached */
2802 root_mp = root_mnt->mnt_mp;
2803 if (new.mnt->mnt_root != new.dentry)
2804 goto out4; /* not a mountpoint */
2805 if (!mnt_has_parent(new_mnt))
2806 goto out4; /* not attached */
2807 /* make sure we can reach put_old from new_root */
2808 if (!is_path_reachable(old_mnt, old.dentry, &new))
2809 goto out4;
2810 root_mp->m_count++; /* pin it so it won't go away */
2811 br_write_lock(&vfsmount_lock);
2812 detach_mnt(new_mnt, &parent_path);
2813 detach_mnt(root_mnt, &root_parent);
2814 /* mount old root on put_old */
2815 attach_mnt(root_mnt, old_mnt, old_mp);
2816 /* mount new_root on / */
2817 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
2818 touch_mnt_namespace(current->nsproxy->mnt_ns);
2819 br_write_unlock(&vfsmount_lock);
2820 chroot_fs_refs(&root, &new);
2821 put_mountpoint(root_mp);
2822 error = 0;
2823 out4:
2824 unlock_mount(old_mp);
2825 if (!error) {
2826 path_put(&root_parent);
2827 path_put(&parent_path);
2828 }
2829 out3:
2830 path_put(&root);
2831 out2:
2832 path_put(&old);
2833 out1:
2834 path_put(&new);
2835 out0:
2836 return error;
2837 }
2838
2839 static void __init init_mount_tree(void)
2840 {
2841 struct vfsmount *mnt;
2842 struct mnt_namespace *ns;
2843 struct path root;
2844 struct file_system_type *type;
2845
2846 type = get_fs_type("rootfs");
2847 if (!type)
2848 panic("Can't find rootfs type");
2849 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
2850 put_filesystem(type);
2851 if (IS_ERR(mnt))
2852 panic("Can't create rootfs");
2853
2854 ns = create_mnt_ns(mnt);
2855 if (IS_ERR(ns))
2856 panic("Can't allocate initial namespace");
2857
2858 init_task.nsproxy->mnt_ns = ns;
2859 get_mnt_ns(ns);
2860
2861 root.mnt = mnt;
2862 root.dentry = mnt->mnt_root;
2863
2864 set_fs_pwd(current->fs, &root);
2865 set_fs_root(current->fs, &root);
2866 }
2867
2868 void __init mnt_init(void)
2869 {
2870 unsigned u;
2871 int err;