8702b2f3bce0466bbd473450dbe6eb6c4746fc4f
4 * (C) Copyright IBM Corporation 2005.
5 * Released under GPL v2.
6 * Author : Ram Pai (linuxram@us.ibm.com)
9 #include <linux/mnt_namespace.h>
10 #include <linux/mount.h>
12 #include <linux/nsproxy.h>
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
);
20 /* return the next shared peer mount of @p */
21 static inline struct mount
*next_peer(struct mount
*p
)
23 return list_entry(p
->mnt_share
.next
, struct mount
, mnt_share
);
26 static inline struct mount
*first_slave(struct mount
*p
)
28 return list_entry(p
->mnt_slave_list
.next
, struct mount
, mnt_slave
);
31 static inline struct mount
*last_slave(struct mount
*p
)
33 return list_entry(p
->mnt_slave_list
.prev
, struct mount
, mnt_slave
);
36 static inline struct mount
*next_slave(struct mount
*p
)
38 return list_entry(p
->mnt_slave
.next
, struct mount
, mnt_slave
);
41 static struct mount
*get_peer_under_root(struct mount
*mnt
,
42 struct mnt_namespace
*ns
,
43 const struct path
*root
)
45 struct mount
*m
= mnt
;
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
))
52 if (m
->mnt_ns
== ns
&& is_path_reachable(m
, m
->mnt
.mnt_root
, root
))
63 * Get ID of closest dominating peer group having a representative
64 * under the given root.
66 * Caller must hold namespace_sem
68 int get_dominating_id(struct mount
*mnt
, const struct path
*root
)
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
);
75 return d
->mnt_group_id
;
81 static int do_make_slave(struct mount
*mnt
)
83 struct mount
*peer_mnt
= mnt
, *master
= mnt
->mnt_master
;
84 struct mount
*slave_mnt
;
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.
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
) ;
95 peer_mnt
->mnt
.mnt_root
!= mnt
->mnt
.mnt_root
) ;
98 if (peer_mnt
== mnt
) {
99 peer_mnt
= next_peer(mnt
);
103 if (mnt
->mnt_group_id
&& IS_MNT_SHARED(mnt
) &&
104 list_empty(&mnt
->mnt_share
))
105 mnt_release_group_id(mnt
);
107 list_del_init(&mnt
->mnt_share
);
108 mnt
->mnt_group_id
= 0;
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
);
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
;
128 mnt
->mnt_master
= master
;
129 CLEAR_MNT_SHARED(mnt
);
134 * vfsmount lock must be held for write
136 void change_mnt_propagation(struct mount
*mnt
, int type
)
138 if (type
== MS_SHARED
) {
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
);
150 mnt
->mnt
.mnt_flags
|= MNT_UNBINDABLE
;
154 #ifdef CONFIG_RKP_NS_PROT
155 rkp_reset_mnt_flags(mnt
->mnt
,MNT_UNBINDABLE
);
157 mnt
->mnt
.mnt_flags
&= ~MNT_UNBINDABLE
;
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
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.
173 static struct mount
*propagation_next(struct mount
*m
,
174 struct mount
*origin
)
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
);
181 struct mount
*master
= m
->mnt_master
;
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
);
194 static struct mount
*skip_propagation_subtree(struct mount
*m
,
195 struct mount
*origin
)
198 * Advance m such that propagation_next will not return
201 if (!IS_MNT_NEW(m
) && !list_empty(&m
->mnt_slave_list
))
207 static struct mount
*next_group(struct mount
*m
, struct mount
*origin
)
212 if (!IS_MNT_NEW(m
) && !list_empty(&m
->mnt_slave_list
))
213 return first_slave(m
);
215 if (m
->mnt_group_id
== origin
->mnt_group_id
) {
218 } else if (m
->mnt_slave
.next
!= &next
->mnt_slave
)
222 /* m is the last peer */
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
)
230 if (master
->mnt_slave
.next
== &m
->mnt_slave
)
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
;
245 static inline bool peers(struct mount
*m1
, struct mount
*m2
)
247 return m1
->mnt_group_id
== m2
->mnt_group_id
&& m1
->mnt_group_id
;
250 static int propagate_one(struct mount
*m
)
254 /* skip ones added by this propagate_mnt() */
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
))
261 if (!is_subdir(mp
->m_dentry
, m
->mnt
.mnt_root
))
264 if (peers(m
, last_dest
)) {
265 type
= CL_MAKE_SHARED
;
269 for (n
= m
; ; n
= p
) {
271 if (p
== dest_master
|| IS_MNT_MARKED(p
))
275 struct mount
*parent
= last_source
->mnt_parent
;
276 if (last_source
== first_source
)
278 done
= parent
->mnt_master
== p
;
279 if (done
&& peers(n
, parent
))
281 last_source
= last_source
->mnt_master
;
285 /* beginning of peer group among the slaves? */
286 if (IS_MNT_SHARED(m
))
287 type
|= CL_MAKE_SHARED
;
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
);
296 child
= copy_tree(last_source
, last_source
->mnt
.mnt_root
, type
);
299 return PTR_ERR(child
);
300 #ifdef CONFIG_RKP_NS_PROT
301 rkp_reset_mnt_flags(child
->mnt
,MNT_LOCKED
);
303 child
->mnt
.mnt_flags
&= ~MNT_LOCKED
;
305 mnt_set_mountpoint(m
, mp
, 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
);
313 hlist_add_head(&child
->mnt_hash
, list
);
314 return count_mounts(m
->mnt_ns
, child
);
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
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.
330 int propagate_mnt(struct mount
*dest_mnt
, struct mountpoint
*dest_mp
,
331 struct mount
*source_mnt
, struct hlist_head
*tree_list
)
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.
341 user_ns
= current
->nsproxy
->mnt_ns
->user_ns
;
342 last_dest
= dest_mnt
;
343 first_source
= source_mnt
;
344 last_source
= source_mnt
;
347 dest_master
= dest_mnt
->mnt_master
;
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
);
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 */
362 ret
= propagate_one(n
);
369 read_seqlock_excl(&mount_lock
);
370 hlist_for_each_entry(n
, tree_list
, mnt_hash
) {
372 if (m
->mnt_master
!= dest_mnt
->mnt_master
)
373 CLEAR_MNT_MARK(m
->mnt_master
);
375 read_sequnlock_excl(&mount_lock
);
379 static struct mount
*find_topper(struct mount
*mnt
)
381 /* If there is exactly one mount covering mnt completely return it. */
384 if (!list_is_singular(&mnt
->mnt_mounts
))
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
)
391 if (child
->mnt_mountpoint
!= mnt
->mnt
.mnt_root
)
399 * return true if the refcount is greater than count
401 static inline int do_refcount_check(struct mount
*mnt
, int count
)
403 return mnt_get_count(mnt
) > count
;
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.
414 * vfsmount lock must be held for write
416 int propagate_mount_busy(struct mount
*mnt
, int refcnt
)
418 struct mount
*m
, *child
, *topper
;
419 struct mount
*parent
= mnt
->mnt_parent
;
422 return do_refcount_check(mnt
, refcnt
);
425 * quickly check if the current mount can be unmounted.
426 * If not, we don't have to go checking for all other
429 if (!list_empty(&mnt
->mnt_mounts
) || do_refcount_check(mnt
, refcnt
))
432 for (m
= propagation_next(parent
, parent
); m
;
433 m
= propagation_next(m
, parent
)) {
435 #ifdef CONFIG_RKP_NS_PROT
436 child
= __lookup_mnt(m
->mnt
, mnt
->mnt_mountpoint
);
438 child
= __lookup_mnt(&m
->mnt
, mnt
->mnt_mountpoint
);
443 /* Is there exactly one mount on the child that covers
444 * it completely whose reference should be ignored?
446 topper
= find_topper(child
);
449 else if (!list_empty(&child
->mnt_mounts
))
452 if (do_refcount_check(child
, count
))
459 * Clear MNT_LOCKED when it can be shown to be safe.
461 * mount_lock lock must be held for write
463 void propagate_mount_unlock(struct mount
*mnt
)
465 struct mount
*parent
= mnt
->mnt_parent
;
466 struct mount
*m
, *child
;
468 BUG_ON(parent
== mnt
);
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
);
475 rkp_reset_mnt_flags(child
->mnt
, MNT_LOCKED
);
477 child
= __lookup_mnt(&m
->mnt
, mnt
->mnt_mountpoint
);
479 child
->mnt
.mnt_flags
&= ~MNT_LOCKED
;
484 static void umount_one(struct mount
*mnt
, struct list_head
*to_umount
)
487 #ifdef CONFIG_RKP_NS_PROT
488 rkp_set_mnt_flags(mnt
->mnt
, MNT_UMOUNT
);
490 mnt
->mnt
.mnt_flags
|= MNT_UMOUNT
;
492 list_del_init(&mnt
->mnt_child
);
493 list_del_init(&mnt
->mnt_umounting
);
494 list_move_tail(&mnt
->mnt_list
, to_umount
);
498 * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
499 * parent propagates to.
501 static bool __propagate_umount(struct mount
*mnt
,
502 struct list_head
*to_umount
,
503 struct list_head
*to_restore
)
505 bool progress
= false;
509 * The state of the parent won't change if this mount is
510 * already unmounted or marked as without children.
512 #ifdef CONFIG_RKP_NS_PROT
513 if (mnt
->mnt
->mnt_flags
& (MNT_UMOUNT
| MNT_MARKED
))
515 if (mnt
->mnt
.mnt_flags
& (MNT_UMOUNT
| MNT_MARKED
))
519 /* Verify topper is the only grandchild that has not been
520 * speculatively unmounted.
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
)
526 if (child
->mnt_mountpoint
== mnt
->mnt
.mnt_root
)
529 if (!list_empty(&child
->mnt_umounting
) && IS_MNT_MARKED(child
))
531 /* Found a mounted child */
535 /* Mark mounts that can be unmounted if not locked */
539 /* If a mount is without children and not locked umount it. */
540 if (!IS_MNT_LOCKED(mnt
)) {
541 umount_one(mnt
, to_umount
);
544 list_move_tail(&mnt
->mnt_umounting
, to_restore
);
550 static void umount_list(struct list_head
*to_umount
,
551 struct list_head
*to_restore
)
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
) {
557 #ifdef CONFIG_RKP_NS_PROT
558 if (child
->mnt_mountpoint
== mnt
->mnt
->mnt_root
)
560 if (child
->mnt_mountpoint
== mnt
->mnt
.mnt_root
)
562 list_move_tail(&child
->mnt_umounting
, to_restore
);
564 umount_one(child
, to_umount
);
569 static void restore_mounts(struct list_head
*to_restore
)
571 /* Restore mounts to a clean working state */
572 while (!list_empty(to_restore
)) {
573 struct mount
*mnt
, *parent
;
574 struct mountpoint
*mp
;
576 mnt
= list_first_entry(to_restore
, struct mount
, mnt_umounting
);
578 list_del_init(&mnt
->mnt_umounting
);
580 /* Should this mount be reparented? */
582 parent
= mnt
->mnt_parent
;
583 #ifdef CONFIG_RKP_NS_PROT
584 while (parent
->mnt
->mnt_flags
& MNT_UMOUNT
) {
586 while (parent
->mnt
.mnt_flags
& MNT_UMOUNT
) {
589 parent
= parent
->mnt_parent
;
591 if (parent
!= mnt
->mnt_parent
)
592 mnt_change_mountpoint(parent
, mp
, mnt
);
596 static void cleanup_umount_visitations(struct list_head
*visited
)
598 while (!list_empty(visited
)) {
600 list_first_entry(visited
, struct mount
, mnt_umounting
);
601 list_del_init(&mnt
->mnt_umounting
);
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.
610 * vfsmount lock must be held for write
612 int propagate_umount(struct list_head
*list
)
615 LIST_HEAD(to_restore
);
616 LIST_HEAD(to_umount
);
619 /* Find candidates for unmounting */
620 list_for_each_entry_reverse(mnt
, list
, mnt_list
) {
621 struct mount
*parent
= mnt
->mnt_parent
;
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.
630 if (!list_empty(&mnt
->mnt_umounting
))
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
);
640 struct mount
*child
= __lookup_mnt(&m
->mnt
,
641 mnt
->mnt_mountpoint
);
646 if (!list_empty(&child
->mnt_umounting
)) {
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.
654 m
= skip_propagation_subtree(m
, parent
);
656 #ifdef CONFIG_RKP_NS_PROT
657 } else if (child
->mnt
->mnt_flags
& MNT_UMOUNT
) {
659 } else if (child
->mnt
.mnt_flags
& MNT_UMOUNT
) {
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.
667 list_add_tail(&child
->mnt_umounting
, &visited
);
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
))
682 umount_list(&to_umount
, &to_restore
);
683 restore_mounts(&to_restore
);
684 cleanup_umount_visitations(&visited
);
685 list_splice_tail(&to_umount
, list
);
691 * Iterates over all slaves, and slaves of slaves.
693 static struct mount
*next_descendent(struct mount
*root
, struct mount
*cur
)
695 if (!IS_MNT_NEW(cur
) && !list_empty(&cur
->mnt_slave_list
))
696 return first_slave(cur
);
698 struct mount
*master
= cur
->mnt_master
;
700 if (!master
|| cur
->mnt_slave
.next
!= &master
->mnt_slave_list
) {
701 struct mount
*next
= next_slave(cur
);
703 return (next
== root
) ? NULL
: next
;
706 } while (cur
!= root
);
710 void propagate_remount(struct mount
*mnt
)
712 struct mount
*m
= mnt
;
713 #ifdef CONFIG_RKP_NS_PROT
714 struct super_block
*sb
= mnt
->mnt
->mnt_sb
;
716 struct super_block
*sb
= mnt
->mnt
.mnt_sb
;
719 if (sb
->s_op
->copy_mnt_data
) {
720 m
= next_descendent(mnt
, m
);
722 #ifdef CONFIG_RKP_NS_PROT
723 sb
->s_op
->copy_mnt_data(m
->mnt
->data
, mnt
->mnt
->data
);
725 sb
->s_op
->copy_mnt_data(m
->mnt
.data
, mnt
->mnt
.data
);
727 m
= next_descendent(mnt
, m
);