Merge tag 'v3.10.59' into update
[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) {
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 }
976
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
986 if ((flag & CL_SLAVE) ||
987 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
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
1008 return mnt;
1009
1010 out_free:
1011 free_vfsmnt(mnt);
1012 return ERR_PTR(err);
1013 }
1014
1015 static inline void mntfree(struct mount *mnt)
1016 {
1017 struct vfsmount *m = &mnt->mnt;
1018 struct super_block *sb = m->mnt_sb;
1019
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 */
1026 /*
1027 * The locking used to deal with mnt_count decrement provides barriers,
1028 * so mnt_get_writers() below is safe.
1029 */
1030 WARN_ON(mnt_get_writers(mnt));
1031 fsnotify_vfsmount_delete(m);
1032 dput(m->mnt_root);
1033 free_vfsmnt(mnt);
1034 deactivate_super(sb);
1035 }
1036
1037 static void mntput_no_expire(struct mount *mnt)
1038 {
1039 put_again:
1040 #ifdef CONFIG_SMP
1041 br_read_lock(&vfsmount_lock);
1042 if (likely(mnt->mnt_ns)) {
1043 /* shouldn't be the last one */
1044 mnt_add_count(mnt, -1);
1045 br_read_unlock(&vfsmount_lock);
1046 return;
1047 }
1048 br_read_unlock(&vfsmount_lock);
1049
1050 br_write_lock(&vfsmount_lock);
1051 mnt_add_count(mnt, -1);
1052 if (mnt_get_count(mnt)) {
1053 br_write_unlock(&vfsmount_lock);
1054 return;
1055 }
1056 #else
1057 mnt_add_count(mnt, -1);
1058 if (likely(mnt_get_count(mnt)))
1059 return;
1060 br_write_lock(&vfsmount_lock);
1061 #endif
1062 if (unlikely(mnt->mnt_pinned)) {
1063 mnt_add_count(mnt, mnt->mnt_pinned + 1);
1064 mnt->mnt_pinned = 0;
1065 br_write_unlock(&vfsmount_lock);
1066 acct_auto_close_mnt(&mnt->mnt);
1067 goto put_again;
1068 }
1069
1070 list_del(&mnt->mnt_instance);
1071 br_write_unlock(&vfsmount_lock);
1072 mntfree(mnt);
1073 }
1074
1075 void mntput(struct vfsmount *mnt)
1076 {
1077 if (mnt) {
1078 struct mount *m = real_mount(mnt);
1079 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1080 if (unlikely(m->mnt_expiry_mark))
1081 m->mnt_expiry_mark = 0;
1082 mntput_no_expire(m);
1083 }
1084 }
1085 EXPORT_SYMBOL(mntput);
1086
1087 struct vfsmount *mntget(struct vfsmount *mnt)
1088 {
1089 if (mnt)
1090 mnt_add_count(real_mount(mnt), 1);
1091 return mnt;
1092 }
1093 EXPORT_SYMBOL(mntget);
1094
1095 void mnt_pin(struct vfsmount *mnt)
1096 {
1097 br_write_lock(&vfsmount_lock);
1098 real_mount(mnt)->mnt_pinned++;
1099 br_write_unlock(&vfsmount_lock);
1100 }
1101 EXPORT_SYMBOL(mnt_pin);
1102
1103 void mnt_unpin(struct vfsmount *m)
1104 {
1105 struct mount *mnt = real_mount(m);
1106 br_write_lock(&vfsmount_lock);
1107 if (mnt->mnt_pinned) {
1108 mnt_add_count(mnt, 1);
1109 mnt->mnt_pinned--;
1110 }
1111 br_write_unlock(&vfsmount_lock);
1112 }
1113 EXPORT_SYMBOL(mnt_unpin);
1114
1115 static 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 */
1126 int generic_show_options(struct seq_file *m, struct dentry *root)
1127 {
1128 const char *options;
1129
1130 rcu_read_lock();
1131 options = rcu_dereference(root->d_sb->s_options);
1132
1133 if (options != NULL && options[0]) {
1134 seq_putc(m, ',');
1135 mangle(m, options);
1136 }
1137 rcu_read_unlock();
1138
1139 return 0;
1140 }
1141 EXPORT_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 */
1156 void save_mount_options(struct super_block *sb, char *options)
1157 {
1158 BUG_ON(sb->s_options);
1159 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1160 }
1161 EXPORT_SYMBOL(save_mount_options);
1162
1163 void 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 }
1172 EXPORT_SYMBOL(replace_mount_options);
1173
1174 #ifdef CONFIG_PROC_FS
1175 /* iterator; we want it to have access to namespace_sem, thus here... */
1176 static void *m_start(struct seq_file *m, loff_t *pos)
1177 {
1178 struct proc_mounts *p = proc_mounts(m);
1179
1180 down_read(&namespace_sem);
1181 return seq_list_start(&p->ns->list, *pos);
1182 }
1183
1184 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1185 {
1186 struct proc_mounts *p = proc_mounts(m);
1187
1188 return seq_list_next(v, &p->ns->list, pos);
1189 }
1190
1191 static void m_stop(struct seq_file *m, void *v)
1192 {
1193 up_read(&namespace_sem);
1194 }
1195
1196 static int m_show(struct seq_file *m, void *v)
1197 {
1198 struct proc_mounts *p = proc_mounts(m);
1199 struct mount *r = list_entry(v, struct mount, mnt_list);
1200 return p->show(m, &r->mnt);
1201 }
1202
1203 const struct seq_operations mounts_op = {
1204 .start = m_start,
1205 .next = m_next,
1206 .stop = m_stop,
1207 .show = m_show,
1208 };
1209 #endif /* CONFIG_PROC_FS */
1210
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 */
1219 int may_umount_tree(struct vfsmount *m)
1220 {
1221 struct mount *mnt = real_mount(m);
1222 int actual_refs = 0;
1223 int minimum_refs = 0;
1224 struct mount *p;
1225 BUG_ON(!m);
1226
1227 /* write lock needed for mnt_get_count */
1228 br_write_lock(&vfsmount_lock);
1229 for (p = mnt; p; p = next_mnt(p, mnt)) {
1230 actual_refs += mnt_get_count(p);
1231 minimum_refs += 2;
1232 }
1233 br_write_unlock(&vfsmount_lock);
1234
1235 if (actual_refs > minimum_refs)
1236 return 0;
1237
1238 return 1;
1239 }
1240
1241 EXPORT_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 */
1256 int may_umount(struct vfsmount *mnt)
1257 {
1258 int ret = 1;
1259 down_read(&namespace_sem);
1260 br_write_lock(&vfsmount_lock);
1261 if (propagate_mount_busy(real_mount(mnt), 2))
1262 ret = 0;
1263 br_write_unlock(&vfsmount_lock);
1264 up_read(&namespace_sem);
1265 return ret;
1266 }
1267
1268 EXPORT_SYMBOL(may_umount);
1269
1270 static LIST_HEAD(unmounted); /* protected by namespace_sem */
1271
1272 static void namespace_unlock(void)
1273 {
1274 struct mount *mnt;
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);
1287 list_del_init(&mnt->mnt_hash);
1288 if (mnt_has_parent(mnt)) {
1289 struct dentry *dentry;
1290 struct mount *m;
1291
1292 br_write_lock(&vfsmount_lock);
1293 dentry = mnt->mnt_mountpoint;
1294 m = mnt->mnt_parent;
1295 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1296 mnt->mnt_parent = mnt;
1297 m->mnt_ghosts--;
1298 br_write_unlock(&vfsmount_lock);
1299 dput(dentry);
1300 mntput(&m->mnt);
1301 }
1302 mntput(&mnt->mnt);
1303 }
1304 }
1305
1306 static inline void namespace_lock(void)
1307 {
1308 down_write(&namespace_sem);
1309 }
1310
1311 /*
1312 * vfsmount lock must be held for write
1313 * namespace_sem must be held for write
1314 */
1315 void umount_tree(struct mount *mnt, int propagate)
1316 {
1317 LIST_HEAD(tmp_list);
1318 struct mount *p;
1319
1320 for (p = mnt; p; p = next_mnt(p, mnt))
1321 list_move(&p->mnt_hash, &tmp_list);
1322
1323 if (propagate)
1324 propagate_umount(&tmp_list);
1325
1326 list_for_each_entry(p, &tmp_list, mnt_hash) {
1327 list_del_init(&p->mnt_expire);
1328 list_del_init(&p->mnt_list);
1329 __touch_mnt_namespace(p->mnt_ns);
1330 p->mnt_ns = NULL;
1331 list_del_init(&p->mnt_child);
1332 if (mnt_has_parent(p)) {
1333 p->mnt_parent->mnt_ghosts++;
1334 put_mountpoint(p->mnt_mp);
1335 p->mnt_mp = NULL;
1336 }
1337 change_mnt_propagation(p, MS_PRIVATE);
1338 }
1339 list_splice(&tmp_list, &unmounted);
1340 }
1341
1342 static void shrink_submounts(struct mount *mnt);
1343
1344 static int do_umount(struct mount *mnt, int flags)
1345 {
1346 struct super_block *sb = mnt->mnt.mnt_sb;
1347 int retval;
1348
1349 retval = security_sb_umount(&mnt->mnt, flags);
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) {
1360 if (&mnt->mnt == current->fs->root.mnt ||
1361 flags & (MNT_FORCE | MNT_DETACH))
1362 return -EINVAL;
1363
1364 /*
1365 * probably don't strictly need the lock here if we examined
1366 * all race cases, but it's a slowpath.
1367 */
1368 br_write_lock(&vfsmount_lock);
1369 if (mnt_get_count(mnt) != 2) {
1370 br_write_unlock(&vfsmount_lock);
1371 return -EBUSY;
1372 }
1373 br_write_unlock(&vfsmount_lock);
1374
1375 if (!xchg(&mnt->mnt_expiry_mark, 1))
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
1389 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1390 sb->s_op->umount_begin(sb);
1391 }
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 */
1402 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1403 /*
1404 * Special case for "unmounting" root ...
1405 * we just try to remount it readonly.
1406 */
1407 if (!capable(CAP_SYS_ADMIN))
1408 return -EPERM;
1409 down_write(&sb->s_umount);
1410 if (!(sb->s_flags & MS_RDONLY))
1411 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
1412 up_write(&sb->s_umount);
1413 return retval;
1414 }
1415
1416 namespace_lock();
1417 br_write_lock(&vfsmount_lock);
1418 event++;
1419
1420 if (!(flags & MNT_DETACH))
1421 shrink_submounts(mnt);
1422
1423 retval = -EBUSY;
1424 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
1425 if (!list_empty(&mnt->mnt_list))
1426 umount_tree(mnt, 1);
1427 retval = 0;
1428 }
1429 br_write_unlock(&vfsmount_lock);
1430 namespace_unlock();
1431 return retval;
1432 }
1433
1434 /*
1435 * Is the caller allowed to modify his namespace?
1436 */
1437 static inline bool may_mount(void)
1438 {
1439 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1440 }
1441
1442 /*
1443 * Now umount can handle mount points as well as block devices.
1444 * This is important for filesystems which use unnamed block devices.
1445 *
1446 * We now support a flag for forced unmount like the other 'big iron'
1447 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1448 */
1449
1450 SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1451 {
1452 struct path path;
1453 struct mount *mnt;
1454 int retval;
1455 int lookup_flags = 0;
1456 #ifdef UMOUNT_LOG
1457 int total_value =0;
1458 #endif
1459 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1460 return -EINVAL;
1461
1462 if (!may_mount())
1463 return -EPERM;
1464
1465 if (!(flags & UMOUNT_NOFOLLOW))
1466 lookup_flags |= LOOKUP_FOLLOW;
1467
1468 retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
1469 if (retval)
1470 goto out;
1471 mnt = real_mount(path.mnt);
1472 retval = -EINVAL;
1473 if (path.dentry != path.mnt->mnt_root)
1474 goto dput_and_out;
1475 if (!check_mnt(mnt))
1476 goto dput_and_out;
1477
1478 retval = do_umount(mnt, flags);
1479 #ifdef UMOUNT_LOG
1480 {
1481 printk("Ahsin do_umount retval=%d \n",retval);
1482 //do_umount success: 0, do_umount busy: -16
1483 //if do_umount fail, need to dump the link list here
1484
1485 if(retval)
1486 printk("Ahsin do_umount fail; mnt_get_count=%d mnt->mnt_devname=%s\n",mnt_get_count(mnt),mnt->mnt_devname);
1487 else
1488 printk("Ahsin do_umount success; mnt_get_count=%d mnt->mnt_devname=%s\n",mnt_get_count(mnt),mnt->mnt_devname);
1489
1490 // print linked list
1491 spin_lock(&mnt_id_lock);
1492 ref_current = ref_head;
1493 while(ref_current != NULL)
1494 {
1495 total_value = total_value + ref_current->count;
1496
1497 if (ref_current->count)
1498 printk("Ahsin PID= %d, Name = %s, Count= %d \n", ref_current->pid, ref_current->name, ref_current->count);
1499 ref_current = ref_current->next;
1500 }
1501 spin_unlock(&mnt_id_lock);
1502 printk("Ahsin total_value=%d \n",total_value);
1503 }
1504 #endif
1505 dput_and_out:
1506 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
1507 dput(path.dentry);
1508 mntput_no_expire(mnt);
1509 out:
1510 return retval;
1511 }
1512
1513 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
1514
1515 /*
1516 * The 2.0 compatible umount. No flags.
1517 */
1518 SYSCALL_DEFINE1(oldumount, char __user *, name)
1519 {
1520 return sys_umount(name, 0);
1521 }
1522
1523 #endif
1524
1525 static bool mnt_ns_loop(struct path *path)
1526 {
1527 /* Could bind mounting the mount namespace inode cause a
1528 * mount namespace loop?
1529 */
1530 struct inode *inode = path->dentry->d_inode;
1531 struct proc_ns *ei;
1532 struct mnt_namespace *mnt_ns;
1533
1534 if (!proc_ns_inode(inode))
1535 return false;
1536
1537 ei = get_proc_ns(inode);
1538 if (ei->ns_ops != &mntns_operations)
1539 return false;
1540
1541 mnt_ns = ei->ns;
1542 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1543 }
1544
1545 struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
1546 int flag)
1547 {
1548 struct mount *res, *p, *q, *r, *parent;
1549
1550 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
1551 return ERR_PTR(-EINVAL);
1552
1553 res = q = clone_mnt(mnt, dentry, flag);
1554 if (IS_ERR(q))
1555 return q;
1556
1557 q->mnt_mountpoint = mnt->mnt_mountpoint;
1558
1559 p = mnt;
1560 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1561 struct mount *s;
1562 if (!is_subdir(r->mnt_mountpoint, dentry))
1563 continue;
1564
1565 for (s = r; s; s = next_mnt(s, r)) {
1566 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
1567 s = skip_mnt_tree(s);
1568 continue;
1569 }
1570 while (p != s->mnt_parent) {
1571 p = p->mnt_parent;
1572 q = q->mnt_parent;
1573 }
1574 p = s;
1575 parent = q;
1576 q = clone_mnt(p, p->mnt.mnt_root, flag);
1577 if (IS_ERR(q))
1578 goto out;
1579 br_write_lock(&vfsmount_lock);
1580 list_add_tail(&q->mnt_list, &res->mnt_list);
1581 attach_mnt(q, parent, p->mnt_mp);
1582 br_write_unlock(&vfsmount_lock);
1583 }
1584 }
1585 return res;
1586 out:
1587 if (res) {
1588 br_write_lock(&vfsmount_lock);
1589 umount_tree(res, 0);
1590 br_write_unlock(&vfsmount_lock);
1591 }
1592 return q;
1593 }
1594
1595 /* Caller should check returned pointer for errors */
1596
1597 struct vfsmount *collect_mounts(struct path *path)
1598 {
1599 struct mount *tree;
1600 namespace_lock();
1601 tree = copy_tree(real_mount(path->mnt), path->dentry,
1602 CL_COPY_ALL | CL_PRIVATE);
1603 namespace_unlock();
1604 if (IS_ERR(tree))
1605 return ERR_CAST(tree);
1606 return &tree->mnt;
1607 }
1608
1609 void drop_collected_mounts(struct vfsmount *mnt)
1610 {
1611 namespace_lock();
1612 br_write_lock(&vfsmount_lock);
1613 umount_tree(real_mount(mnt), 0);
1614 br_write_unlock(&vfsmount_lock);
1615 namespace_unlock();
1616 }
1617
1618 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1619 struct vfsmount *root)
1620 {
1621 struct mount *mnt;
1622 int res = f(root, arg);
1623 if (res)
1624 return res;
1625 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1626 res = f(&mnt->mnt, arg);
1627 if (res)
1628 return res;
1629 }
1630 return 0;
1631 }
1632
1633 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1634 {
1635 struct mount *p;
1636
1637 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1638 if (p->mnt_group_id && !IS_MNT_SHARED(p))
1639 mnt_release_group_id(p);
1640 }
1641 }
1642
1643 static int invent_group_ids(struct mount *mnt, bool recurse)
1644 {
1645 struct mount *p;
1646
1647 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1648 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1649 int err = mnt_alloc_group_id(p);
1650 if (err) {
1651 cleanup_group_ids(mnt, p);
1652 return err;
1653 }
1654 }
1655 }
1656
1657 return 0;
1658 }
1659
1660 /*
1661 * @source_mnt : mount tree to be attached
1662 * @nd : place the mount tree @source_mnt is attached
1663 * @parent_nd : if non-null, detach the source_mnt from its parent and
1664 * store the parent mount and mountpoint dentry.
1665 * (done when source_mnt is moved)
1666 *
1667 * NOTE: in the table below explains the semantics when a source mount
1668 * of a given type is attached to a destination mount of a given type.
1669 * ---------------------------------------------------------------------------
1670 * | BIND MOUNT OPERATION |
1671 * |**************************************************************************
1672 * | source-->| shared | private | slave | unbindable |
1673 * | dest | | | | |
1674 * | | | | | | |
1675 * | v | | | | |
1676 * |**************************************************************************
1677 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1678 * | | | | | |
1679 * |non-shared| shared (+) | private | slave (*) | invalid |
1680 * ***************************************************************************
1681 * A bind operation clones the source mount and mounts the clone on the
1682 * destination mount.
1683 *
1684 * (++) the cloned mount is propagated to all the mounts in the propagation
1685 * tree of the destination mount and the cloned mount is added to
1686 * the peer group of the source mount.
1687 * (+) the cloned mount is created under the destination mount and is marked
1688 * as shared. The cloned mount is added to the peer group of the source
1689 * mount.
1690 * (+++) the mount is propagated to all the mounts in the propagation tree
1691 * of the destination mount and the cloned mount is made slave
1692 * of the same master as that of the source mount. The cloned mount
1693 * is marked as 'shared and slave'.
1694 * (*) the cloned mount is made a slave of the same master as that of the
1695 * source mount.
1696 *
1697 * ---------------------------------------------------------------------------
1698 * | MOVE MOUNT OPERATION |
1699 * |**************************************************************************
1700 * | source-->| shared | private | slave | unbindable |
1701 * | dest | | | | |
1702 * | | | | | | |
1703 * | v | | | | |
1704 * |**************************************************************************
1705 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1706 * | | | | | |
1707 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1708 * ***************************************************************************
1709 *
1710 * (+) the mount is moved to the destination. And is then propagated to
1711 * all the mounts in the propagation tree of the destination mount.
1712 * (+*) the mount is moved to the destination.
1713 * (+++) the mount is moved to the destination and is then propagated to
1714 * all the mounts belonging to the destination mount's propagation tree.
1715 * the mount is marked as 'shared and slave'.
1716 * (*) the mount continues to be a slave at the new location.
1717 *
1718 * if the source mount is a tree, the operations explained above is
1719 * applied to each mount in the tree.
1720 * Must be called without spinlocks held, since this function can sleep
1721 * in allocations.
1722 */
1723 static int attach_recursive_mnt(struct mount *source_mnt,
1724 struct mount *dest_mnt,
1725 struct mountpoint *dest_mp,
1726 struct path *parent_path)
1727 {
1728 LIST_HEAD(tree_list);
1729 struct mount *child, *p;
1730 int err;
1731
1732 if (IS_MNT_SHARED(dest_mnt)) {
1733 err = invent_group_ids(source_mnt, true);
1734 if (err)
1735 goto out;
1736 }
1737 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1738 if (err)
1739 goto out_cleanup_ids;
1740
1741 br_write_lock(&vfsmount_lock);
1742
1743 if (IS_MNT_SHARED(dest_mnt)) {
1744 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1745 set_mnt_shared(p);
1746 }
1747 if (parent_path) {
1748 detach_mnt(source_mnt, parent_path);
1749 attach_mnt(source_mnt, dest_mnt, dest_mp);
1750 touch_mnt_namespace(source_mnt->mnt_ns);
1751 } else {
1752 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
1753 commit_tree(source_mnt);
1754 }
1755
1756 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1757 list_del_init(&child->mnt_hash);
1758 commit_tree(child);
1759 }
1760 br_write_unlock(&vfsmount_lock);
1761
1762 return 0;
1763
1764 out_cleanup_ids:
1765 if (IS_MNT_SHARED(dest_mnt))
1766 cleanup_group_ids(source_mnt, NULL);
1767 out:
1768 return err;
1769 }
1770
1771 static struct mountpoint *lock_mount(struct path *path)
1772 {
1773 struct vfsmount *mnt;
1774 struct dentry *dentry = path->dentry;
1775 retry:
1776 mutex_lock(&dentry->d_inode->i_mutex);
1777 if (unlikely(cant_mount(dentry))) {
1778 mutex_unlock(&dentry->d_inode->i_mutex);
1779 return ERR_PTR(-ENOENT);
1780 }
1781 namespace_lock();
1782 mnt = lookup_mnt(path);
1783 if (likely(!mnt)) {
1784 struct mountpoint *mp = new_mountpoint(dentry);
1785 if (IS_ERR(mp)) {
1786 namespace_unlock();
1787 mutex_unlock(&dentry->d_inode->i_mutex);
1788 return mp;
1789 }
1790 return mp;
1791 }
1792 namespace_unlock();
1793 mutex_unlock(&path->dentry->d_inode->i_mutex);
1794 path_put(path);
1795 path->mnt = mnt;
1796 dentry = path->dentry = dget(mnt->mnt_root);
1797 goto retry;
1798 }
1799
1800 static void unlock_mount(struct mountpoint *where)
1801 {
1802 struct dentry *dentry = where->m_dentry;
1803 put_mountpoint(where);
1804 namespace_unlock();
1805 mutex_unlock(&dentry->d_inode->i_mutex);
1806 }
1807
1808 static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
1809 {
1810 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
1811 return -EINVAL;
1812
1813 if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
1814 S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
1815 return -ENOTDIR;
1816
1817 return attach_recursive_mnt(mnt, p, mp, NULL);
1818 }
1819
1820 /*
1821 * Sanity check the flags to change_mnt_propagation.
1822 */
1823
1824 static int flags_to_propagation_type(int flags)
1825 {
1826 int type = flags & ~(MS_REC | MS_SILENT);
1827
1828 /* Fail if any non-propagation flags are set */
1829 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1830 return 0;
1831 /* Only one propagation flag should be set */
1832 if (!is_power_of_2(type))
1833 return 0;
1834 return type;
1835 }
1836
1837 /*
1838 * recursively change the type of the mountpoint.
1839 */
1840 static int do_change_type(struct path *path, int flag)
1841 {
1842 struct mount *m;
1843 struct mount *mnt = real_mount(path->mnt);
1844 int recurse = flag & MS_REC;
1845 int type;
1846 int err = 0;
1847
1848 if (path->dentry != path->mnt->mnt_root)
1849 return -EINVAL;
1850
1851 type = flags_to_propagation_type(flag);
1852 if (!type)
1853 return -EINVAL;
1854
1855 namespace_lock();
1856 if (type == MS_SHARED) {
1857 err = invent_group_ids(mnt, recurse);
1858 if (err)
1859 goto out_unlock;
1860 }
1861
1862 br_write_lock(&vfsmount_lock);
1863 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
1864 change_mnt_propagation(m, type);
1865 br_write_unlock(&vfsmount_lock);
1866
1867 out_unlock:
1868 namespace_unlock();
1869 return err;
1870 }
1871
1872 /*
1873 * do loopback mount.
1874 */
1875 static int do_loopback(struct path *path, const char *old_name,
1876 int recurse)
1877 {
1878 struct path old_path;
1879 struct mount *mnt = NULL, *old, *parent;
1880 struct mountpoint *mp;
1881 int err;
1882 if (!old_name || !*old_name)
1883 return -EINVAL;
1884 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
1885 if (err)
1886 return err;
1887
1888 err = -EINVAL;
1889 if (mnt_ns_loop(&old_path))
1890 goto out;
1891
1892 mp = lock_mount(path);
1893 err = PTR_ERR(mp);
1894 if (IS_ERR(mp))
1895 goto out;
1896
1897 old = real_mount(old_path.mnt);
1898 parent = real_mount(path->mnt);
1899
1900 err = -EINVAL;
1901 if (IS_MNT_UNBINDABLE(old))
1902 goto out2;
1903
1904 if (!check_mnt(parent) || !check_mnt(old))
1905 goto out2;
1906
1907 if (recurse)
1908 mnt = copy_tree(old, old_path.dentry, 0);
1909 else
1910 mnt = clone_mnt(old, old_path.dentry, 0);
1911
1912 if (IS_ERR(mnt)) {
1913 err = PTR_ERR(mnt);
1914 goto out2;
1915 }
1916
1917 err = graft_tree(mnt, parent, mp);
1918 if (err) {
1919 br_write_lock(&vfsmount_lock);
1920 umount_tree(mnt, 0);
1921 br_write_unlock(&vfsmount_lock);
1922 }
1923 out2:
1924 unlock_mount(mp);
1925 out:
1926 path_put(&old_path);
1927 return err;
1928 }
1929
1930 static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1931 {
1932 int error = 0;
1933 int readonly_request = 0;
1934
1935 if (ms_flags & MS_RDONLY)
1936 readonly_request = 1;
1937 if (readonly_request == __mnt_is_readonly(mnt))
1938 return 0;
1939
1940 if (readonly_request)
1941 error = mnt_make_readonly(real_mount(mnt));
1942 else
1943 __mnt_unmake_readonly(real_mount(mnt));
1944 return error;
1945 }
1946
1947 /*
1948 * change filesystem flags. dir should be a physical root of filesystem.
1949 * If you've mounted a non-root directory somewhere and want to do remount
1950 * on it - tough luck.
1951 */
1952 static int do_remount(struct path *path, int flags, int mnt_flags,
1953 void *data)
1954 {
1955 int err;
1956 struct super_block *sb = path->mnt->mnt_sb;
1957 struct mount *mnt = real_mount(path->mnt);
1958
1959 if (!check_mnt(mnt))
1960 return -EINVAL;
1961
1962 if (path->dentry != path->mnt->mnt_root)
1963 return -EINVAL;
1964
1965 /* Don't allow changing of locked mnt flags.
1966 *
1967 * No locks need to be held here while testing the various
1968 * MNT_LOCK flags because those flags can never be cleared
1969 * once they are set.
1970 */
1971 if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
1972 !(mnt_flags & MNT_READONLY)) {
1973 return -EPERM;
1974 }
1975 if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
1976 !(mnt_flags & MNT_NODEV)) {
1977 return -EPERM;
1978 }
1979 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
1980 !(mnt_flags & MNT_NOSUID)) {
1981 return -EPERM;
1982 }
1983 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
1984 !(mnt_flags & MNT_NOEXEC)) {
1985 return -EPERM;
1986 }
1987 if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
1988 ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
1989 return -EPERM;
1990 }
1991
1992 err = security_sb_remount(sb, data);
1993 if (err)
1994 return err;
1995
1996 down_write(&sb->s_umount);
1997 if (flags & MS_BIND)
1998 err = change_mount_flags(path->mnt, flags);
1999 else if (!capable(CAP_SYS_ADMIN))
2000 err = -EPERM;
2001 else
2002 err = do_remount_sb(sb, flags, data, 0);
2003 if (!err) {
2004 br_write_lock(&vfsmount_lock);
2005 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2006 mnt->mnt.mnt_flags = mnt_flags;
2007 br_write_unlock(&vfsmount_lock);
2008 }
2009 up_write(&sb->s_umount);
2010 if (!err) {
2011 br_write_lock(&vfsmount_lock);
2012 touch_mnt_namespace(mnt->mnt_ns);
2013 br_write_unlock(&vfsmount_lock);
2014 }
2015 return err;
2016 }
2017
2018 static inline int tree_contains_unbindable(struct mount *mnt)
2019 {
2020 struct mount *p;
2021 for (p = mnt; p; p = next_mnt(p, mnt)) {
2022 if (IS_MNT_UNBINDABLE(p))
2023 return 1;
2024 }
2025 return 0;
2026 }
2027
2028 static int do_move_mount(struct path *path, const char *old_name)
2029 {
2030 struct path old_path, parent_path;
2031 struct mount *p;
2032 struct mount *old;
2033 struct mountpoint *mp;
2034 int err;
2035 if (!old_name || !*old_name)
2036 return -EINVAL;
2037 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2038 if (err)
2039 return err;
2040
2041 mp = lock_mount(path);
2042 err = PTR_ERR(mp);
2043 if (IS_ERR(mp))
2044 goto out;
2045
2046 old = real_mount(old_path.mnt);
2047 p = real_mount(path->mnt);
2048
2049 err = -EINVAL;
2050 if (!check_mnt(p) || !check_mnt(old))
2051 goto out1;
2052
2053 err = -EINVAL;
2054 if (old_path.dentry != old_path.mnt->mnt_root)
2055 goto out1;
2056
2057 if (!mnt_has_parent(old))
2058 goto out1;
2059
2060 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
2061 S_ISDIR(old_path.dentry->d_inode->i_mode))
2062 goto out1;
2063 /*
2064 * Don't move a mount residing in a shared parent.
2065 */
2066 if (IS_MNT_SHARED(old->mnt_parent))
2067 goto out1;
2068 /*
2069 * Don't move a mount tree containing unbindable mounts to a destination
2070 * mount which is shared.
2071 */
2072 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
2073 goto out1;
2074 err = -ELOOP;
2075 for (; mnt_has_parent(p); p = p->mnt_parent)
2076 if (p == old)
2077 goto out1;
2078
2079 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
2080 if (err)
2081 goto out1;
2082
2083 /* if the mount is moved, it should no longer be expire
2084 * automatically */
2085 list_del_init(&old->mnt_expire);
2086 out1:
2087 unlock_mount(mp);
2088 out:
2089 if (!err)
2090 path_put(&parent_path);
2091 path_put(&old_path);
2092 return err;
2093 }
2094
2095 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
2096 {
2097 int err;
2098 const char *subtype = strchr(fstype, '.');
2099 if (subtype) {
2100 subtype++;
2101 err = -EINVAL;
2102 if (!subtype[0])
2103 goto err;
2104 } else
2105 subtype = "";
2106
2107 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
2108 err = -ENOMEM;
2109 if (!mnt->mnt_sb->s_subtype)
2110 goto err;
2111 return mnt;
2112
2113 err:
2114 mntput(mnt);
2115 return ERR_PTR(err);
2116 }
2117
2118 /*
2119 * add a mount into a namespace's mount tree
2120 */
2121 static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
2122 {
2123 struct mountpoint *mp;
2124 struct mount *parent;
2125 int err;
2126
2127 mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
2128
2129 mp = lock_mount(path);
2130 if (IS_ERR(mp))
2131 return PTR_ERR(mp);
2132
2133 parent = real_mount(path->mnt);
2134 err = -EINVAL;
2135 if (unlikely(!check_mnt(parent))) {
2136 /* that's acceptable only for automounts done in private ns */
2137 if (!(mnt_flags & MNT_SHRINKABLE))
2138 goto unlock;
2139 /* ... and for those we'd better have mountpoint still alive */
2140 if (!parent->mnt_ns)
2141 goto unlock;
2142 }
2143
2144 /* Refuse the same filesystem on the same mount point */
2145 err = -EBUSY;
2146 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
2147 path->mnt->mnt_root == path->dentry)
2148 goto unlock;
2149
2150 err = -EINVAL;
2151 if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
2152 goto unlock;
2153
2154 newmnt->mnt.mnt_flags = mnt_flags;
2155 err = graft_tree(newmnt, parent, mp);
2156
2157 unlock:
2158 unlock_mount(mp);
2159 return err;
2160 }
2161
2162 /*
2163 * create a new mount for userspace and request it to be added into the
2164 * namespace's tree
2165 */
2166 static int do_new_mount(struct path *path, const char *fstype, int flags,
2167 int mnt_flags, const char *name, void *data)
2168 {
2169 struct file_system_type *type;
2170 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2171 struct vfsmount *mnt;
2172 int err;
2173
2174 if (!fstype)
2175 return -EINVAL;
2176
2177 type = get_fs_type(fstype);
2178 if (!type)
2179 return -ENODEV;
2180
2181 if (user_ns != &init_user_ns) {
2182 if (!(type->fs_flags & FS_USERNS_MOUNT)) {
2183 put_filesystem(type);
2184 return -EPERM;
2185 }
2186 /* Only in special cases allow devices from mounts
2187 * created outside the initial user namespace.
2188 */
2189 if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
2190 flags |= MS_NODEV;
2191 mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV;
2192 }
2193 }
2194
2195 mnt = vfs_kern_mount(type, flags, name, data);
2196 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2197 !mnt->mnt_sb->s_subtype)
2198 mnt = fs_set_subtype(mnt, fstype);
2199
2200 put_filesystem(type);
2201 if (IS_ERR(mnt))
2202 return PTR_ERR(mnt);
2203
2204 err = do_add_mount(real_mount(mnt), path, mnt_flags);
2205 if (err)
2206 mntput(mnt);
2207 return err;
2208 }
2209
2210 int finish_automount(struct vfsmount *m, struct path *path)
2211 {
2212 struct mount *mnt = real_mount(m);
2213 int err;
2214 /* The new mount record should have at least 2 refs to prevent it being
2215 * expired before we get a chance to add it
2216 */
2217 BUG_ON(mnt_get_count(mnt) < 2);
2218
2219 if (m->mnt_sb == path->mnt->mnt_sb &&
2220 m->mnt_root == path->dentry) {
2221 err = -ELOOP;
2222 goto fail;
2223 }
2224
2225 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2226 if (!err)
2227 return 0;
2228 fail:
2229 /* remove m from any expiration list it may be on */
2230 if (!list_empty(&mnt->mnt_expire)) {
2231 namespace_lock();
2232 br_write_lock(&vfsmount_lock);
2233 list_del_init(&mnt->mnt_expire);
2234 br_write_unlock(&vfsmount_lock);
2235 namespace_unlock();
2236 }
2237 mntput(m);
2238 mntput(m);
2239 return err;
2240 }
2241
2242 /**
2243 * mnt_set_expiry - Put a mount on an expiration list
2244 * @mnt: The mount to list.
2245 * @expiry_list: The list to add the mount to.
2246 */
2247 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2248 {
2249 namespace_lock();
2250 br_write_lock(&vfsmount_lock);
2251
2252 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2253
2254 br_write_unlock(&vfsmount_lock);
2255 namespace_unlock();
2256 }
2257 EXPORT_SYMBOL(mnt_set_expiry);
2258
2259 /*
2260 * process a list of expirable mountpoints with the intent of discarding any
2261 * mountpoints that aren't in use and haven't been touched since last we came
2262 * here
2263 */
2264 void mark_mounts_for_expiry(struct list_head *mounts)
2265 {
2266 struct mount *mnt, *next;
2267 LIST_HEAD(graveyard);
2268
2269 if (list_empty(mounts))
2270 return;
2271
2272 namespace_lock();
2273 br_write_lock(&vfsmount_lock);
2274
2275 /* extract from the expiration list every vfsmount that matches the
2276 * following criteria:
2277 * - only referenced by its parent vfsmount
2278 * - still marked for expiry (marked on the last call here; marks are
2279 * cleared by mntput())
2280 */
2281 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2282 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
2283 propagate_mount_busy(mnt, 1))
2284 continue;
2285 list_move(&mnt->mnt_expire, &graveyard);
2286 }
2287 while (!list_empty(&graveyard)) {
2288 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2289 touch_mnt_namespace(mnt->mnt_ns);
2290 umount_tree(mnt, 1);
2291 }
2292 br_write_unlock(&vfsmount_lock);
2293 namespace_unlock();
2294 }
2295
2296 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2297
2298 /*
2299 * Ripoff of 'select_parent()'
2300 *
2301 * search the list of submounts for a given mountpoint, and move any
2302 * shrinkable submounts to the 'graveyard' list.
2303 */
2304 static int select_submounts(struct mount *parent, struct list_head *graveyard)
2305 {
2306 struct mount *this_parent = parent;
2307 struct list_head *next;
2308 int found = 0;
2309
2310 repeat:
2311 next = this_parent->mnt_mounts.next;
2312 resume:
2313 while (next != &this_parent->mnt_mounts) {
2314 struct list_head *tmp = next;
2315 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
2316
2317 next = tmp->next;
2318 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
2319 continue;
2320 /*
2321 * Descend a level if the d_mounts list is non-empty.
2322 */
2323 if (!list_empty(&mnt->mnt_mounts)) {
2324 this_parent = mnt;
2325 goto repeat;
2326 }
2327
2328 if (!propagate_mount_busy(mnt, 1)) {
2329 list_move_tail(&mnt->mnt_expire, graveyard);
2330 found++;
2331 }
2332 }
2333 /*
2334 * All done at this level ... ascend and resume the search
2335 */
2336 if (this_parent != parent) {
2337 next = this_parent->mnt_child.next;
2338 this_parent = this_parent->mnt_parent;
2339 goto resume;
2340 }
2341 return found;
2342 }
2343
2344 /*
2345 * process a list of expirable mountpoints with the intent of discarding any
2346 * submounts of a specific parent mountpoint
2347 *
2348 * vfsmount_lock must be held for write
2349 */
2350 static void shrink_submounts(struct mount *mnt)
2351 {
2352 LIST_HEAD(graveyard);
2353 struct mount *m;
2354
2355 /* extract submounts of 'mountpoint' from the expiration list */
2356 while (select_submounts(mnt, &graveyard)) {
2357 while (!list_empty(&graveyard)) {
2358 m = list_first_entry(&graveyard, struct mount,
2359 mnt_expire);
2360 touch_mnt_namespace(m->mnt_ns);
2361 umount_tree(m, 1);
2362 }
2363 }
2364 }
2365
2366 /*
2367 * Some copy_from_user() implementations do not return the exact number of
2368 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2369 * Note that this function differs from copy_from_user() in that it will oops
2370 * on bad values of `to', rather than returning a short copy.
2371 */
2372 static long exact_copy_from_user(void *to, const void __user * from,
2373 unsigned long n)
2374 {
2375 char *t = to;
2376 const char __user *f = from;
2377 char c;
2378
2379 if (!access_ok(VERIFY_READ, from, n))
2380 return n;
2381
2382 while (n) {
2383 if (__get_user(c, f)) {
2384 memset(t, 0, n);
2385 break;
2386 }
2387 *t++ = c;
2388 f++;
2389 n--;
2390 }
2391 return n;
2392 }
2393
2394 int copy_mount_options(const void __user * data, unsigned long *where)
2395 {
2396 int i;
2397 unsigned long page;
2398 unsigned long size;
2399
2400 *where = 0;
2401 if (!data)
2402 return 0;
2403
2404 if (!(page = __get_free_page(GFP_KERNEL)))
2405 return -ENOMEM;
2406
2407 /* We only care that *some* data at the address the user
2408 * gave us is valid. Just in case, we'll zero
2409 * the remainder of the page.
2410 */
2411 /* copy_from_user cannot cross TASK_SIZE ! */
2412 size = TASK_SIZE - (unsigned long)data;
2413 if (size > PAGE_SIZE)
2414 size = PAGE_SIZE;
2415
2416 i = size - exact_copy_from_user((void *)page, data, size);
2417 if (!i) {
2418 free_page(page);
2419 return -EFAULT;
2420 }
2421 if (i != PAGE_SIZE)
2422 memset((char *)page + i, 0, PAGE_SIZE - i);
2423 *where = page;
2424 return 0;
2425 }
2426
2427 int copy_mount_string(const void __user *data, char **where)
2428 {
2429 char *tmp;
2430
2431 if (!data) {
2432 *where = NULL;
2433 return 0;
2434 }
2435
2436 tmp = strndup_user(data, PAGE_SIZE);
2437 if (IS_ERR(tmp))
2438 return PTR_ERR(tmp);
2439
2440 *where = tmp;
2441 return 0;
2442 }
2443
2444 /*
2445 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2446 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2447 *
2448 * data is a (void *) that can point to any structure up to
2449 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2450 * information (or be NULL).
2451 *
2452 * Pre-0.97 versions of mount() didn't have a flags word.
2453 * When the flags word was introduced its top half was required
2454 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2455 * Therefore, if this magic number is present, it carries no information
2456 * and must be discarded.
2457 */
2458 long do_mount(const char *dev_name, const char *dir_name,
2459 const char *type_page, unsigned long flags, void *data_page)
2460 {
2461 struct path path;
2462 int retval = 0;
2463 int mnt_flags = 0;
2464
2465 /* Discard magic */
2466 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2467 flags &= ~MS_MGC_MSK;
2468
2469 /* Basic sanity checks */
2470
2471 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2472 return -EINVAL;
2473
2474 if (data_page)
2475 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2476
2477 /* ... and get the mountpoint */
2478 retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2479 if (retval)
2480 return retval;
2481
2482 retval = security_sb_mount(dev_name, &path,
2483 type_page, flags, data_page);
2484 if (!retval && !may_mount())
2485 retval = -EPERM;
2486 if (retval)
2487 goto dput_out;
2488
2489 /* Default to relatime unless overriden */
2490 if (!(flags & MS_NOATIME))
2491 mnt_flags |= MNT_RELATIME;
2492
2493 /* Separate the per-mountpoint flags */
2494 if (flags & MS_NOSUID)
2495 mnt_flags |= MNT_NOSUID;
2496 if (flags & MS_NODEV)
2497 mnt_flags |= MNT_NODEV;
2498 if (flags & MS_NOEXEC)
2499 mnt_flags |= MNT_NOEXEC;
2500 if (flags & MS_NOATIME)
2501 mnt_flags |= MNT_NOATIME;
2502 if (flags & MS_NODIRATIME)
2503 mnt_flags |= MNT_NODIRATIME;
2504 if (flags & MS_STRICTATIME)
2505 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2506 if (flags & MS_RDONLY)
2507 mnt_flags |= MNT_READONLY;
2508
2509 /* The default atime for remount is preservation */
2510 if ((flags & MS_REMOUNT) &&
2511 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2512 MS_STRICTATIME)) == 0)) {
2513 mnt_flags &= ~MNT_ATIME_MASK;
2514 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2515 }
2516
2517 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2518 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2519 MS_STRICTATIME);
2520
2521 if (flags & MS_REMOUNT)
2522 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
2523 data_page);
2524 else if (flags & MS_BIND)
2525 retval = do_loopback(&path, dev_name, flags & MS_REC);
2526 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2527 retval = do_change_type(&path, flags);
2528 else if (flags & MS_MOVE)
2529 retval = do_move_mount(&path, dev_name);
2530 else
2531 retval = do_new_mount(&path, type_page, flags, mnt_flags,
2532 dev_name, data_page);
2533 dput_out:
2534 path_put(&path);
2535 return retval;
2536 }
2537
2538 static void free_mnt_ns(struct mnt_namespace *ns)
2539 {
2540 proc_free_inum(ns->proc_inum);
2541 put_user_ns(ns->user_ns);
2542 kfree(ns);
2543 }
2544
2545 /*
2546 * Assign a sequence number so we can detect when we attempt to bind
2547 * mount a reference to an older mount namespace into the current
2548 * mount namespace, preventing reference counting loops. A 64bit
2549 * number incrementing at 10Ghz will take 12,427 years to wrap which
2550 * is effectively never, so we can ignore the possibility.
2551 */
2552 static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2553
2554 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2555 {
2556 struct mnt_namespace *new_ns;
2557 int ret;
2558
2559 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2560 if (!new_ns)
2561 return ERR_PTR(-ENOMEM);
2562 ret = proc_alloc_inum(&new_ns->proc_inum);
2563 if (ret) {
2564 kfree(new_ns);
2565 return ERR_PTR(ret);
2566 }
2567 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2568 atomic_set(&new_ns->count, 1);
2569 new_ns->root = NULL;
2570 INIT_LIST_HEAD(&new_ns->list);
2571 init_waitqueue_head(&new_ns->poll);
2572 new_ns->event = 0;
2573 new_ns->user_ns = get_user_ns(user_ns);
2574 return new_ns;
2575 }
2576
2577 /*
2578 * Allocate a new namespace structure and populate it with contents
2579 * copied from the namespace of the passed in task structure.
2580 */
2581 static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
2582 struct user_namespace *user_ns, struct fs_struct *fs)
2583 {
2584 struct mnt_namespace *new_ns;
2585 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2586 struct mount *p, *q;
2587 struct mount *old = mnt_ns->root;
2588 struct mount *new;
2589 int copy_flags;
2590
2591 new_ns = alloc_mnt_ns(user_ns);
2592 if (IS_ERR(new_ns))
2593 return new_ns;
2594
2595 namespace_lock();
2596 /* First pass: copy the tree topology */
2597 copy_flags = CL_COPY_ALL | CL_EXPIRE;
2598 if (user_ns != mnt_ns->user_ns)
2599 copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
2600 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2601 if (IS_ERR(new)) {
2602 namespace_unlock();
2603 free_mnt_ns(new_ns);
2604 return ERR_CAST(new);
2605 }
2606 new_ns->root = new;
2607 br_write_lock(&vfsmount_lock);
2608 list_add_tail(&new_ns->list, &new->mnt_list);
2609 br_write_unlock(&vfsmount_lock);
2610
2611 /*
2612 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2613 * as belonging to new namespace. We have already acquired a private
2614 * fs_struct, so tsk->fs->lock is not needed.
2615 */
2616 p = old;
2617 q = new;
2618 while (p) {
2619 q->mnt_ns = new_ns;
2620 if (fs) {
2621 if (&p->mnt == fs->root.mnt) {
2622 fs->root.mnt = mntget(&q->mnt);
2623 rootmnt = &p->mnt;
2624 }
2625 if (&p->mnt == fs->pwd.mnt) {
2626 fs->pwd.mnt = mntget(&q->mnt);
2627 pwdmnt = &p->mnt;
2628 }
2629 }
2630 p = next_mnt(p, old);
2631 q = next_mnt(q, new);
2632 }
2633 namespace_unlock();
2634
2635 if (rootmnt)
2636 mntput(rootmnt);
2637 if (pwdmnt)
2638 mntput(pwdmnt);
2639
2640 return new_ns;
2641 }
2642
2643 struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2644 struct user_namespace *user_ns, struct fs_struct *new_fs)
2645 {
2646 struct mnt_namespace *new_ns;
2647
2648 BUG_ON(!ns);
2649 get_mnt_ns(ns);
2650
2651 if (!(flags & CLONE_NEWNS))
2652 return ns;
2653
2654 new_ns = dup_mnt_ns(ns, user_ns, new_fs);
2655
2656 put_mnt_ns(ns);
2657 return new_ns;
2658 }
2659
2660 /**
2661 * create_mnt_ns - creates a private namespace and adds a root filesystem
2662 * @mnt: pointer to the new root filesystem mountpoint
2663 */
2664 static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2665 {
2666 struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2667 if (!IS_ERR(new_ns)) {
2668 struct mount *mnt = real_mount(m);
2669 mnt->mnt_ns = new_ns;
2670 new_ns->root = mnt;
2671 list_add(&mnt->mnt_list, &new_ns->list);
2672 } else {
2673 mntput(m);
2674 }
2675 return new_ns;
2676 }
2677
2678 struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2679 {
2680 struct mnt_namespace *ns;
2681 struct super_block *s;
2682 struct path path;
2683 int err;
2684
2685 ns = create_mnt_ns(mnt);
2686 if (IS_ERR(ns))
2687 return ERR_CAST(ns);
2688
2689 err = vfs_path_lookup(mnt->mnt_root, mnt,
2690 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2691
2692 put_mnt_ns(ns);
2693
2694 if (err)
2695 return ERR_PTR(err);
2696
2697 /* trade a vfsmount reference for active sb one */
2698 s = path.mnt->mnt_sb;
2699 atomic_inc(&s->s_active);
2700 mntput(path.mnt);
2701 /* lock the sucker */
2702 down_write(&s->s_umount);
2703 /* ... and return the root of (sub)tree on it */
2704 return path.dentry;
2705 }
2706 EXPORT_SYMBOL(mount_subtree);
2707
2708 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2709 char __user *, type, unsigned long, flags, void __user *, data)
2710 {
2711 int ret;
2712 char *kernel_type;
2713 struct filename *kernel_dir;
2714 char *kernel_dev;
2715 unsigned long data_page;
2716
2717 ret = copy_mount_string(type, &kernel_type);
2718 if (ret < 0)
2719 goto out_type;
2720
2721 kernel_dir = getname(dir_name);
2722 if (IS_ERR(kernel_dir)) {
2723 ret = PTR_ERR(kernel_dir);
2724 goto out_dir;
2725 }
2726
2727 ret = copy_mount_string(dev_name, &kernel_dev);
2728 if (ret < 0)
2729 goto out_dev;
2730
2731 ret = copy_mount_options(data, &data_page);
2732 if (ret < 0)
2733 goto out_data;
2734
2735 ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
2736 (void *) data_page);
2737
2738 free_page(data_page);
2739 out_data:
2740 kfree(kernel_dev);
2741 out_dev:
2742 putname(kernel_dir);
2743 out_dir:
2744 kfree(kernel_type);
2745 out_type:
2746 return ret;
2747 }
2748
2749 /*
2750 * Return true if path is reachable from root
2751 *
2752 * namespace_sem or vfsmount_lock is held
2753 */
2754 bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2755 const struct path *root)
2756 {
2757 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2758 dentry = mnt->mnt_mountpoint;
2759 mnt = mnt->mnt_parent;
2760 }
2761 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2762 }
2763
2764 int path_is_under(struct path *path1, struct path *path2)
2765 {
2766 int res;
2767 br_read_lock(&vfsmount_lock);
2768 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
2769 br_read_unlock(&vfsmount_lock);
2770 return res;
2771 }
2772 EXPORT_SYMBOL(path_is_under);
2773
2774 /*
2775 * pivot_root Semantics:
2776 * Moves the root file system of the current process to the directory put_old,
2777 * makes new_root as the new root file system of the current process, and sets
2778 * root/cwd of all processes which had them on the current root to new_root.
2779 *
2780 * Restrictions:
2781 * The new_root and put_old must be directories, and must not be on the
2782 * same file system as the current process root. The put_old must be
2783 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2784 * pointed to by put_old must yield the same directory as new_root. No other
2785 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2786 *
2787 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2788 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2789 * in this situation.
2790 *
2791 * Notes:
2792 * - we don't move root/cwd if they are not at the root (reason: if something
2793 * cared enough to change them, it's probably wrong to force them elsewhere)
2794 * - it's okay to pick a root that isn't the root of a file system, e.g.
2795 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2796 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2797 * first.
2798 */
2799 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2800 const char __user *, put_old)
2801 {
2802 struct path new, old, parent_path, root_parent, root;
2803 struct mount *new_mnt, *root_mnt, *old_mnt;
2804 struct mountpoint *old_mp, *root_mp;
2805 int error;
2806
2807 if (!may_mount())
2808 return -EPERM;
2809
2810 error = user_path_dir(new_root, &new);
2811 if (error)
2812 goto out0;
2813
2814 error = user_path_dir(put_old, &old);
2815 if (error)
2816 goto out1;
2817
2818 error = security_sb_pivotroot(&old, &new);
2819 if (error)
2820 goto out2;
2821
2822 get_fs_root(current->fs, &root);
2823 old_mp = lock_mount(&old);
2824 error = PTR_ERR(old_mp);
2825 if (IS_ERR(old_mp))
2826 goto out3;
2827
2828 error = -EINVAL;
2829 new_mnt = real_mount(new.mnt);
2830 root_mnt = real_mount(root.mnt);
2831 old_mnt = real_mount(old.mnt);
2832 if (IS_MNT_SHARED(old_mnt) ||
2833 IS_MNT_SHARED(new_mnt->mnt_parent) ||
2834 IS_MNT_SHARED(root_mnt->mnt_parent))
2835 goto out4;
2836 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2837 goto out4;
2838 error = -ENOENT;
2839 if (d_unlinked(new.dentry))
2840 goto out4;
2841 error = -EBUSY;
2842 if (new_mnt == root_mnt || old_mnt == root_mnt)
2843 goto out4; /* loop, on the same file system */
2844 error = -EINVAL;
2845 if (root.mnt->mnt_root != root.dentry)
2846 goto out4; /* not a mountpoint */
2847 if (!mnt_has_parent(root_mnt))
2848 goto out4; /* not attached */
2849 root_mp = root_mnt->mnt_mp;
2850 if (new.mnt->mnt_root != new.dentry)
2851 goto out4; /* not a mountpoint */
2852 if (!mnt_has_parent(new_mnt))
2853 goto out4; /* not attached */
2854 /* make sure we can reach put_old from new_root */
2855 if (!is_path_reachable(old_mnt, old.dentry, &new))
2856 goto out4;
2857 root_mp->m_count++; /* pin it so it won't go away */
2858 br_write_lock(&vfsmount_lock);
2859 detach_mnt(new_mnt, &parent_path);
2860 detach_mnt(root_mnt, &root_parent);
2861 /* mount old root on put_old */
2862 attach_mnt(root_mnt, old_mnt, old_mp);
2863 /* mount new_root on / */
2864 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
2865 touch_mnt_namespace(current->nsproxy->mnt_ns);
2866 br_write_unlock(&vfsmount_lock);
2867 chroot_fs_refs(&root, &new);
2868 put_mountpoint(root_mp);
2869 error = 0;
2870 out4:
2871 unlock_mount(old_mp);
2872 if (!error) {
2873 path_put(&root_parent);
2874 path_put(&parent_path);
2875 }
2876 out3:
2877 path_put(&root);
2878 out2:
2879 path_put(&old);
2880 out1:
2881 path_put(&new);
2882 out0:
2883 return error;
2884 }
2885
2886 static void __init init_mount_tree(void)
2887 {
2888 struct vfsmount *mnt;
2889 struct mnt_namespace *ns;
2890 struct path root;
2891 struct file_system_type *type;
2892
2893 type = get_fs_type("rootfs");
2894 if (!type)
2895 panic("Can't find rootfs type");
2896 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
2897 put_filesystem(type);
2898 if (IS_ERR(mnt))
2899 panic("Can't create rootfs");
2900
2901 ns = create_mnt_ns(mnt);
2902 if (IS_ERR(ns))
2903 panic("Can't allocate initial namespace");
2904
2905 init_task.nsproxy->mnt_ns = ns;
2906 get_mnt_ns(ns);
2907
2908 root.mnt = mnt;
2909 root.dentry = mnt->mnt_root;
2910
2911 set_fs_pwd(current->fs, &root);
2912 set_fs_root(current->fs, &root);
2913 }
2914
2915 void __init mnt_init(void)
2916 {
2917 unsigned u;
2918 int err;
2919
2920 init_rwsem(&namespace_sem);
2921
2922 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
2923 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
2924
2925 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
2926 mountpoint_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
2927
2928 if (!mount_hashtable || !mountpoint_hashtable)
2929 panic("Failed to allocate mount hash table\n");
2930
2931 printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
2932
2933 for (u = 0; u < HASH_SIZE; u++)
2934 INIT_LIST_HEAD(&mount_hashtable[u]);
2935 for (u = 0; u < HASH_SIZE; u++)
2936 INIT_LIST_HEAD(&mountpoint_hashtable[u]);
2937
2938 br_lock_init(&vfsmount_lock);
2939
2940 err = sysfs_init();
2941 if (err)
2942 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
2943 __func__, err);
2944 fs_kobj = kobject_create_and_add("fs", NULL);
2945 if (!fs_kobj)
2946 printk(KERN_WARNING "%s: kobj create error\n", __func__);
2947 init_rootfs();
2948 init_mount_tree();
2949 }
2950
2951 void put_mnt_ns(struct mnt_namespace *ns)
2952 {
2953 if (!atomic_dec_and_test(&ns->count))
2954 return;
2955 namespace_lock();
2956 br_write_lock(&vfsmount_lock);
2957 umount_tree(ns->root, 0);
2958 br_write_unlock(&vfsmount_lock);
2959 namespace_unlock();
2960 free_mnt_ns(ns);
2961 }
2962
2963 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
2964 {
2965 struct vfsmount *mnt;
2966 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2967 if (!IS_ERR(mnt)) {
2968 /*
2969 * it is a longterm mount, don't release mnt until
2970 * we unmount before file sys is unregistered
2971 */
2972 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
2973 }
2974 return mnt;
2975 }
2976 EXPORT_SYMBOL_GPL(kern_mount_data);
2977
2978 void kern_unmount(struct vfsmount *mnt)
2979 {
2980 /* release long term mount so mount point can be released */
2981 if (!IS_ERR_OR_NULL(mnt)) {
2982 br_write_lock(&vfsmount_lock);
2983 real_mount(mnt)->mnt_ns = NULL;
2984 br_write_unlock(&vfsmount_lock);
2985 mntput(mnt);
2986 }
2987 }
2988 EXPORT_SYMBOL(kern_unmount);
2989
2990 bool our_mnt(struct vfsmount *mnt)
2991 {
2992 return check_mnt(real_mount(mnt));
2993 }
2994
2995 bool current_chrooted(void)
2996 {
2997 /* Does the current process have a non-standard root */
2998 struct path ns_root;
2999 struct path fs_root;
3000 bool chrooted;
3001
3002 /* Find the namespace root */
3003 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3004 ns_root.dentry = ns_root.mnt->mnt_root;
3005 path_get(&ns_root);
3006 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3007 ;
3008
3009 get_fs_root(current->fs, &fs_root);
3010
3011 chrooted = !path_equal(&fs_root, &ns_root);
3012
3013 path_put(&fs_root);
3014 path_put(&ns_root);
3015
3016 return chrooted;
3017 }
3018
3019 void update_mnt_policy(struct user_namespace *userns)
3020 {
3021 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3022 struct mount *mnt;
3023
3024 down_read(&namespace_sem);
3025 list_for_each_entry(mnt, &ns->list, mnt_list) {
3026 switch (mnt->mnt.mnt_sb->s_magic) {
3027 case SYSFS_MAGIC:
3028 userns->may_mount_sysfs = true;
3029 break;
3030 case PROC_SUPER_MAGIC:
3031 userns->may_mount_proc = true;
3032 break;
3033 }
3034 if (userns->may_mount_sysfs && userns->may_mount_proc)
3035 break;
3036 }
3037 up_read(&namespace_sem);
3038 }
3039
3040 static void *mntns_get(struct task_struct *task)
3041 {
3042 struct mnt_namespace *ns = NULL;
3043 struct nsproxy *nsproxy;
3044
3045 rcu_read_lock();
3046 nsproxy = task_nsproxy(task);
3047 if (nsproxy) {
3048 ns = nsproxy->mnt_ns;
3049 get_mnt_ns(ns);
3050 }
3051 rcu_read_unlock();
3052
3053 return ns;
3054 }
3055
3056 static void mntns_put(void *ns)
3057 {
3058 put_mnt_ns(ns);
3059 }
3060
3061 static int mntns_install(struct nsproxy *nsproxy, void *ns)
3062 {
3063 struct fs_struct *fs = current->fs;
3064 struct mnt_namespace *mnt_ns = ns;
3065 struct path root;
3066
3067 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3068 !nsown_capable(CAP_SYS_CHROOT) ||
3069 !nsown_capable(CAP_SYS_ADMIN))
3070 return -EPERM;
3071
3072 if (fs->users != 1)
3073 return -EINVAL;
3074
3075 get_mnt_ns(mnt_ns);
3076 put_mnt_ns(nsproxy->mnt_ns);
3077 nsproxy->mnt_ns = mnt_ns;
3078
3079 /* Find the root */
3080 root.mnt = &mnt_ns->root->mnt;
3081 root.dentry = mnt_ns->root->mnt.mnt_root;
3082 path_get(&root);
3083 while(d_mountpoint(root.dentry) && follow_down_one(&root))
3084 ;
3085
3086 /* Update the pwd and root */
3087 set_fs_pwd(fs, &root);
3088 set_fs_root(fs, &root);
3089
3090 path_put(&root);
3091 return 0;
3092 }
3093
3094 static unsigned int mntns_inum(void *ns)
3095 {
3096 struct mnt_namespace *mnt_ns = ns;
3097 return mnt_ns->proc_inum;
3098 }
3099
3100 const struct proc_ns_operations mntns_operations = {
3101 .name = "mnt",
3102 .type = CLONE_NEWNS,
3103 .get = mntns_get,
3104 .put = mntns_put,
3105 .install = mntns_install,
3106 .inum = mntns_inum,
3107 };