8702b2f3bce0466bbd473450dbe6eb6c4746fc4f
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / pnode.c
1 /*
2 * linux/fs/pnode.c
3 *
4 * (C) Copyright IBM Corporation 2005.
5 * Released under GPL v2.
6 * Author : Ram Pai (linuxram@us.ibm.com)
7 *
8 */
9 #include <linux/mnt_namespace.h>
10 #include <linux/mount.h>
11 #include <linux/fs.h>
12 #include <linux/nsproxy.h>
13 #include "internal.h"
14 #include "pnode.h"
15
16 #ifdef CONFIG_RKP_NS_PROT
17 void rkp_set_mnt_flags(struct vfsmount *mnt,int flags);
18 void rkp_reset_mnt_flags(struct vfsmount *mnt,int flags);
19 #endif
20 /* return the next shared peer mount of @p */
21 static inline struct mount *next_peer(struct mount *p)
22 {
23 return list_entry(p->mnt_share.next, struct mount, mnt_share);
24 }
25
26 static inline struct mount *first_slave(struct mount *p)
27 {
28 return list_entry(p->mnt_slave_list.next, struct mount, mnt_slave);
29 }
30
31 static inline struct mount *last_slave(struct mount *p)
32 {
33 return list_entry(p->mnt_slave_list.prev, struct mount, mnt_slave);
34 }
35
36 static inline struct mount *next_slave(struct mount *p)
37 {
38 return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
39 }
40
41 static struct mount *get_peer_under_root(struct mount *mnt,
42 struct mnt_namespace *ns,
43 const struct path *root)
44 {
45 struct mount *m = mnt;
46
47 do {
48 /* Check the namespace first for optimization */
49 #ifdef CONFIG_RKP_NS_PROT
50 if (m->mnt_ns == ns && is_path_reachable(m, m->mnt->mnt_root, root))
51 #else
52 if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
53 #endif
54 return m;
55
56 m = next_peer(m);
57 } while (m != mnt);
58
59 return NULL;
60 }
61
62 /*
63 * Get ID of closest dominating peer group having a representative
64 * under the given root.
65 *
66 * Caller must hold namespace_sem
67 */
68 int get_dominating_id(struct mount *mnt, const struct path *root)
69 {
70 struct mount *m;
71
72 for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
73 struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
74 if (d)
75 return d->mnt_group_id;
76 }
77
78 return 0;
79 }
80
81 static int do_make_slave(struct mount *mnt)
82 {
83 struct mount *peer_mnt = mnt, *master = mnt->mnt_master;
84 struct mount *slave_mnt;
85
86 /*
87 * slave 'mnt' to a peer mount that has the
88 * same root dentry. If none is available then
89 * slave it to anything that is available.
90 */
91 while ((peer_mnt = next_peer(peer_mnt)) != mnt &&
92 #ifdef CONFIG_RKP_NS_PROT
93 peer_mnt->mnt->mnt_root != mnt->mnt->mnt_root) ;
94 #else
95 peer_mnt->mnt.mnt_root != mnt->mnt.mnt_root) ;
96 #endif
97
98 if (peer_mnt == mnt) {
99 peer_mnt = next_peer(mnt);
100 if (peer_mnt == mnt)
101 peer_mnt = NULL;
102 }
103 if (mnt->mnt_group_id && IS_MNT_SHARED(mnt) &&
104 list_empty(&mnt->mnt_share))
105 mnt_release_group_id(mnt);
106
107 list_del_init(&mnt->mnt_share);
108 mnt->mnt_group_id = 0;
109
110 if (peer_mnt)
111 master = peer_mnt;
112
113 if (master) {
114 list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
115 slave_mnt->mnt_master = master;
116 list_move(&mnt->mnt_slave, &master->mnt_slave_list);
117 list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
118 INIT_LIST_HEAD(&mnt->mnt_slave_list);
119 } else {
120 struct list_head *p = &mnt->mnt_slave_list;
121 while (!list_empty(p)) {
122 slave_mnt = list_first_entry(p,
123 struct mount, mnt_slave);
124 list_del_init(&slave_mnt->mnt_slave);
125 slave_mnt->mnt_master = NULL;
126 }
127 }
128 mnt->mnt_master = master;
129 CLEAR_MNT_SHARED(mnt);
130 return 0;
131 }
132
133 /*
134 * vfsmount lock must be held for write
135 */
136 void change_mnt_propagation(struct mount *mnt, int type)
137 {
138 if (type == MS_SHARED) {
139 set_mnt_shared(mnt);
140 return;
141 }
142 do_make_slave(mnt);
143 if (type != MS_SLAVE) {
144 list_del_init(&mnt->mnt_slave);
145 mnt->mnt_master = NULL;
146 if (type == MS_UNBINDABLE) {
147 #ifdef CONFIG_RKP_NS_PROT
148 rkp_set_mnt_flags(mnt->mnt,MNT_UNBINDABLE);
149 #else
150 mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
151 #endif
152 }
153 else {
154 #ifdef CONFIG_RKP_NS_PROT
155 rkp_reset_mnt_flags(mnt->mnt,MNT_UNBINDABLE);
156 #else
157 mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
158 #endif
159 }
160 }
161 }
162
163 /*
164 * get the next mount in the propagation tree.
165 * @m: the mount seen last
166 * @origin: the original mount from where the tree walk initiated
167 *
168 * Note that peer groups form contiguous segments of slave lists.
169 * We rely on that in get_source() to be able to find out if
170 * vfsmount found while iterating with propagation_next() is
171 * a peer of one we'd found earlier.
172 */
173 static struct mount *propagation_next(struct mount *m,
174 struct mount *origin)
175 {
176 /* are there any slaves of this mount? */
177 if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
178 return first_slave(m);
179
180 while (1) {
181 struct mount *master = m->mnt_master;
182
183 if (master == origin->mnt_master) {
184 struct mount *next = next_peer(m);
185 return (next == origin) ? NULL : next;
186 } else if (m->mnt_slave.next != &master->mnt_slave_list)
187 return next_slave(m);
188
189 /* back at master */
190 m = master;
191 }
192 }
193
194 static struct mount *skip_propagation_subtree(struct mount *m,
195 struct mount *origin)
196 {
197 /*
198 * Advance m such that propagation_next will not return
199 * the slaves of m.
200 */
201 if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
202 m = last_slave(m);
203
204 return m;
205 }
206
207 static struct mount *next_group(struct mount *m, struct mount *origin)
208 {
209 while (1) {
210 while (1) {
211 struct mount *next;
212 if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
213 return first_slave(m);
214 next = next_peer(m);
215 if (m->mnt_group_id == origin->mnt_group_id) {
216 if (next == origin)
217 return NULL;
218 } else if (m->mnt_slave.next != &next->mnt_slave)
219 break;
220 m = next;
221 }
222 /* m is the last peer */
223 while (1) {
224 struct mount *master = m->mnt_master;
225 if (m->mnt_slave.next != &master->mnt_slave_list)
226 return next_slave(m);
227 m = next_peer(master);
228 if (master->mnt_group_id == origin->mnt_group_id)
229 break;
230 if (master->mnt_slave.next == &m->mnt_slave)
231 break;
232 m = master;
233 }
234 if (m == origin)
235 return NULL;
236 }
237 }
238
239 /* all accesses are serialized by namespace_sem */
240 static struct user_namespace *user_ns;
241 static struct mount *last_dest, *first_source, *last_source, *dest_master;
242 static struct mountpoint *mp;
243 static struct hlist_head *list;
244
245 static inline bool peers(struct mount *m1, struct mount *m2)
246 {
247 return m1->mnt_group_id == m2->mnt_group_id && m1->mnt_group_id;
248 }
249
250 static int propagate_one(struct mount *m)
251 {
252 struct mount *child;
253 int type;
254 /* skip ones added by this propagate_mnt() */
255 if (IS_MNT_NEW(m))
256 return 0;
257 /* skip if mountpoint isn't covered by it */
258 #ifdef CONFIG_RKP_NS_PROT
259 if (!is_subdir(mp->m_dentry, m->mnt->mnt_root))
260 #else
261 if (!is_subdir(mp->m_dentry, m->mnt.mnt_root))
262 #endif
263 return 0;
264 if (peers(m, last_dest)) {
265 type = CL_MAKE_SHARED;
266 } else {
267 struct mount *n, *p;
268 bool done;
269 for (n = m; ; n = p) {
270 p = n->mnt_master;
271 if (p == dest_master || IS_MNT_MARKED(p))
272 break;
273 }
274 do {
275 struct mount *parent = last_source->mnt_parent;
276 if (last_source == first_source)
277 break;
278 done = parent->mnt_master == p;
279 if (done && peers(n, parent))
280 break;
281 last_source = last_source->mnt_master;
282 } while (!done);
283
284 type = CL_SLAVE;
285 /* beginning of peer group among the slaves? */
286 if (IS_MNT_SHARED(m))
287 type |= CL_MAKE_SHARED;
288 }
289
290 /* Notice when we are propagating across user namespaces */
291 if (m->mnt_ns->user_ns != user_ns)
292 type |= CL_UNPRIVILEGED;
293 #ifdef CONFIG_RKP_NS_PROT
294 child = copy_tree(last_source, last_source->mnt->mnt_root, type);
295 #else
296 child = copy_tree(last_source, last_source->mnt.mnt_root, type);
297 #endif
298 if (IS_ERR(child))
299 return PTR_ERR(child);
300 #ifdef CONFIG_RKP_NS_PROT
301 rkp_reset_mnt_flags(child->mnt,MNT_LOCKED);
302 #else
303 child->mnt.mnt_flags &= ~MNT_LOCKED;
304 #endif
305 mnt_set_mountpoint(m, mp, child);
306 last_dest = m;
307 last_source = child;
308 if (m->mnt_master != dest_master) {
309 read_seqlock_excl(&mount_lock);
310 SET_MNT_MARK(m->mnt_master);
311 read_sequnlock_excl(&mount_lock);
312 }
313 hlist_add_head(&child->mnt_hash, list);
314 return count_mounts(m->mnt_ns, child);
315 }
316
317 /*
318 * mount 'source_mnt' under the destination 'dest_mnt' at
319 * dentry 'dest_dentry'. And propagate that mount to
320 * all the peer and slave mounts of 'dest_mnt'.
321 * Link all the new mounts into a propagation tree headed at
322 * source_mnt. Also link all the new mounts using ->mnt_list
323 * headed at source_mnt's ->mnt_list
324 *
325 * @dest_mnt: destination mount.
326 * @dest_dentry: destination dentry.
327 * @source_mnt: source mount.
328 * @tree_list : list of heads of trees to be attached.
329 */
330 int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
331 struct mount *source_mnt, struct hlist_head *tree_list)
332 {
333 struct mount *m, *n;
334 int ret = 0;
335
336 /*
337 * we don't want to bother passing tons of arguments to
338 * propagate_one(); everything is serialized by namespace_sem,
339 * so globals will do just fine.
340 */
341 user_ns = current->nsproxy->mnt_ns->user_ns;
342 last_dest = dest_mnt;
343 first_source = source_mnt;
344 last_source = source_mnt;
345 mp = dest_mp;
346 list = tree_list;
347 dest_master = dest_mnt->mnt_master;
348
349 /* all peers of dest_mnt, except dest_mnt itself */
350 for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
351 ret = propagate_one(n);
352 if (ret)
353 goto out;
354 }
355
356 /* all slave groups */
357 for (m = next_group(dest_mnt, dest_mnt); m;
358 m = next_group(m, dest_mnt)) {
359 /* everything in that slave group */
360 n = m;
361 do {
362 ret = propagate_one(n);
363 if (ret)
364 goto out;
365 n = next_peer(n);
366 } while (n != m);
367 }
368 out:
369 read_seqlock_excl(&mount_lock);
370 hlist_for_each_entry(n, tree_list, mnt_hash) {
371 m = n->mnt_parent;
372 if (m->mnt_master != dest_mnt->mnt_master)
373 CLEAR_MNT_MARK(m->mnt_master);
374 }
375 read_sequnlock_excl(&mount_lock);
376 return ret;
377 }
378
379 static struct mount *find_topper(struct mount *mnt)
380 {
381 /* If there is exactly one mount covering mnt completely return it. */
382 struct mount *child;
383
384 if (!list_is_singular(&mnt->mnt_mounts))
385 return NULL;
386
387 child = list_first_entry(&mnt->mnt_mounts, struct mount, mnt_child);
388 #ifdef CONFIG_RKP_NS_PROT
389 if (child->mnt_mountpoint != mnt->mnt->mnt_root)
390 #else
391 if (child->mnt_mountpoint != mnt->mnt.mnt_root)
392 #endif
393 return NULL;
394
395 return child;
396 }
397
398 /*
399 * return true if the refcount is greater than count
400 */
401 static inline int do_refcount_check(struct mount *mnt, int count)
402 {
403 return mnt_get_count(mnt) > count;
404 }
405
406 /*
407 * check if the mount 'mnt' can be unmounted successfully.
408 * @mnt: the mount to be checked for unmount
409 * NOTE: unmounting 'mnt' would naturally propagate to all
410 * other mounts its parent propagates to.
411 * Check if any of these mounts that **do not have submounts**
412 * have more references than 'refcnt'. If so return busy.
413 *
414 * vfsmount lock must be held for write
415 */
416 int propagate_mount_busy(struct mount *mnt, int refcnt)
417 {
418 struct mount *m, *child, *topper;
419 struct mount *parent = mnt->mnt_parent;
420
421 if (mnt == parent)
422 return do_refcount_check(mnt, refcnt);
423
424 /*
425 * quickly check if the current mount can be unmounted.
426 * If not, we don't have to go checking for all other
427 * mounts
428 */
429 if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
430 return 1;
431
432 for (m = propagation_next(parent, parent); m;
433 m = propagation_next(m, parent)) {
434 int count = 1;
435 #ifdef CONFIG_RKP_NS_PROT
436 child = __lookup_mnt(m->mnt, mnt->mnt_mountpoint);
437 #else
438 child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
439 #endif
440 if (!child)
441 continue;
442
443 /* Is there exactly one mount on the child that covers
444 * it completely whose reference should be ignored?
445 */
446 topper = find_topper(child);
447 if (topper)
448 count += 1;
449 else if (!list_empty(&child->mnt_mounts))
450 continue;
451
452 if (do_refcount_check(child, count))
453 return 1;
454 }
455 return 0;
456 }
457
458 /*
459 * Clear MNT_LOCKED when it can be shown to be safe.
460 *
461 * mount_lock lock must be held for write
462 */
463 void propagate_mount_unlock(struct mount *mnt)
464 {
465 struct mount *parent = mnt->mnt_parent;
466 struct mount *m, *child;
467
468 BUG_ON(parent == mnt);
469
470 for (m = propagation_next(parent, parent); m;
471 m = propagation_next(m, parent)) {
472 #ifdef CONFIG_RKP_NS_PROT
473 child = __lookup_mnt(m->mnt, mnt->mnt_mountpoint);
474 if (child)
475 rkp_reset_mnt_flags(child->mnt, MNT_LOCKED);
476 #else
477 child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
478 if (child)
479 child->mnt.mnt_flags &= ~MNT_LOCKED;
480 #endif
481 }
482 }
483
484 static void umount_one(struct mount *mnt, struct list_head *to_umount)
485 {
486 CLEAR_MNT_MARK(mnt);
487 #ifdef CONFIG_RKP_NS_PROT
488 rkp_set_mnt_flags(mnt->mnt, MNT_UMOUNT);
489 #else
490 mnt->mnt.mnt_flags |= MNT_UMOUNT;
491 #endif
492 list_del_init(&mnt->mnt_child);
493 list_del_init(&mnt->mnt_umounting);
494 list_move_tail(&mnt->mnt_list, to_umount);
495 }
496
497 /*
498 * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
499 * parent propagates to.
500 */
501 static bool __propagate_umount(struct mount *mnt,
502 struct list_head *to_umount,
503 struct list_head *to_restore)
504 {
505 bool progress = false;
506 struct mount *child;
507
508 /*
509 * The state of the parent won't change if this mount is
510 * already unmounted or marked as without children.
511 */
512 #ifdef CONFIG_RKP_NS_PROT
513 if (mnt->mnt->mnt_flags & (MNT_UMOUNT | MNT_MARKED))
514 #else
515 if (mnt->mnt.mnt_flags & (MNT_UMOUNT | MNT_MARKED))
516 #endif
517 goto out;
518
519 /* Verify topper is the only grandchild that has not been
520 * speculatively unmounted.
521 */
522 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
523 #ifdef CONFIG_RKP_NS_PROT
524 if (child->mnt_mountpoint == mnt->mnt->mnt_root)
525 #else
526 if (child->mnt_mountpoint == mnt->mnt.mnt_root)
527 #endif
528 continue;
529 if (!list_empty(&child->mnt_umounting) && IS_MNT_MARKED(child))
530 continue;
531 /* Found a mounted child */
532 goto children;
533 }
534
535 /* Mark mounts that can be unmounted if not locked */
536 SET_MNT_MARK(mnt);
537 progress = true;
538
539 /* If a mount is without children and not locked umount it. */
540 if (!IS_MNT_LOCKED(mnt)) {
541 umount_one(mnt, to_umount);
542 } else {
543 children:
544 list_move_tail(&mnt->mnt_umounting, to_restore);
545 }
546 out:
547 return progress;
548 }
549
550 static void umount_list(struct list_head *to_umount,
551 struct list_head *to_restore)
552 {
553 struct mount *mnt, *child, *tmp;
554 list_for_each_entry(mnt, to_umount, mnt_list) {
555 list_for_each_entry_safe(child, tmp, &mnt->mnt_mounts, mnt_child) {
556 /* topper? */
557 #ifdef CONFIG_RKP_NS_PROT
558 if (child->mnt_mountpoint == mnt->mnt->mnt_root)
559 #else
560 if (child->mnt_mountpoint == mnt->mnt.mnt_root)
561 #endif
562 list_move_tail(&child->mnt_umounting, to_restore);
563 else
564 umount_one(child, to_umount);
565 }
566 }
567 }
568
569 static void restore_mounts(struct list_head *to_restore)
570 {
571 /* Restore mounts to a clean working state */
572 while (!list_empty(to_restore)) {
573 struct mount *mnt, *parent;
574 struct mountpoint *mp;
575
576 mnt = list_first_entry(to_restore, struct mount, mnt_umounting);
577 CLEAR_MNT_MARK(mnt);
578 list_del_init(&mnt->mnt_umounting);
579
580 /* Should this mount be reparented? */
581 mp = mnt->mnt_mp;
582 parent = mnt->mnt_parent;
583 #ifdef CONFIG_RKP_NS_PROT
584 while (parent->mnt->mnt_flags & MNT_UMOUNT) {
585 #else
586 while (parent->mnt.mnt_flags & MNT_UMOUNT) {
587 #endif
588 mp = parent->mnt_mp;
589 parent = parent->mnt_parent;
590 }
591 if (parent != mnt->mnt_parent)
592 mnt_change_mountpoint(parent, mp, mnt);
593 }
594 }
595
596 static void cleanup_umount_visitations(struct list_head *visited)
597 {
598 while (!list_empty(visited)) {
599 struct mount *mnt =
600 list_first_entry(visited, struct mount, mnt_umounting);
601 list_del_init(&mnt->mnt_umounting);
602 }
603 }
604
605 /*
606 * collect all mounts that receive propagation from the mount in @list,
607 * and return these additional mounts in the same list.
608 * @list: the list of mounts to be unmounted.
609 *
610 * vfsmount lock must be held for write
611 */
612 int propagate_umount(struct list_head *list)
613 {
614 struct mount *mnt;
615 LIST_HEAD(to_restore);
616 LIST_HEAD(to_umount);
617 LIST_HEAD(visited);
618
619 /* Find candidates for unmounting */
620 list_for_each_entry_reverse(mnt, list, mnt_list) {
621 struct mount *parent = mnt->mnt_parent;
622 struct mount *m;
623
624 /*
625 * If this mount has already been visited it is known that it's
626 * entire peer group and all of their slaves in the propagation
627 * tree for the mountpoint has already been visited and there is
628 * no need to visit them again.
629 */
630 if (!list_empty(&mnt->mnt_umounting))
631 continue;
632
633 list_add_tail(&mnt->mnt_umounting, &visited);
634 for (m = propagation_next(parent, parent); m;
635 m = propagation_next(m, parent)) {
636 #ifdef CONFIG_RKP_NS_PROT
637 struct mount *child = __lookup_mnt(m->mnt,
638 mnt->mnt_mountpoint);
639 #else
640 struct mount *child = __lookup_mnt(&m->mnt,
641 mnt->mnt_mountpoint);
642 #endif
643 if (!child)
644 continue;
645
646 if (!list_empty(&child->mnt_umounting)) {
647 /*
648 * If the child has already been visited it is
649 * know that it's entire peer group and all of
650 * their slaves in the propgation tree for the
651 * mountpoint has already been visited and there
652 * is no need to visit this subtree again.
653 */
654 m = skip_propagation_subtree(m, parent);
655 continue;
656 #ifdef CONFIG_RKP_NS_PROT
657 } else if (child->mnt->mnt_flags & MNT_UMOUNT) {
658 #else
659 } else if (child->mnt.mnt_flags & MNT_UMOUNT) {
660 #endif
661 /*
662 * We have come accross an partially unmounted
663 * mount in list that has not been visited yet.
664 * Remember it has been visited and continue
665 * about our merry way.
666 */
667 list_add_tail(&child->mnt_umounting, &visited);
668 continue;
669 }
670
671 /* Check the child and parents while progress is made */
672 while (__propagate_umount(child,
673 &to_umount, &to_restore)) {
674 /* Is the parent a umount candidate? */
675 child = child->mnt_parent;
676 if (list_empty(&child->mnt_umounting))
677 break;
678 }
679 }
680 }
681
682 umount_list(&to_umount, &to_restore);
683 restore_mounts(&to_restore);
684 cleanup_umount_visitations(&visited);
685 list_splice_tail(&to_umount, list);
686
687 return 0;
688 }
689
690 /*
691 * Iterates over all slaves, and slaves of slaves.
692 */
693 static struct mount *next_descendent(struct mount *root, struct mount *cur)
694 {
695 if (!IS_MNT_NEW(cur) && !list_empty(&cur->mnt_slave_list))
696 return first_slave(cur);
697 do {
698 struct mount *master = cur->mnt_master;
699
700 if (!master || cur->mnt_slave.next != &master->mnt_slave_list) {
701 struct mount *next = next_slave(cur);
702
703 return (next == root) ? NULL : next;
704 }
705 cur = master;
706 } while (cur != root);
707 return NULL;
708 }
709
710 void propagate_remount(struct mount *mnt)
711 {
712 struct mount *m = mnt;
713 #ifdef CONFIG_RKP_NS_PROT
714 struct super_block *sb = mnt->mnt->mnt_sb;
715 #else
716 struct super_block *sb = mnt->mnt.mnt_sb;
717 #endif
718
719 if (sb->s_op->copy_mnt_data) {
720 m = next_descendent(mnt, m);
721 while (m) {
722 #ifdef CONFIG_RKP_NS_PROT
723 sb->s_op->copy_mnt_data(m->mnt->data, mnt->mnt->data);
724 #else
725 sb->s_op->copy_mnt_data(m->mnt.data, mnt->mnt.data);
726 #endif
727 m = next_descendent(mnt, m);
728 }
729 }
730 }