pnode.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/namespace.h>
  10. #include <linux/mount.h>
  11. #include <linux/fs.h>
  12. #include "pnode.h"
  13. /* return the next shared peer mount of @p */
  14. static inline struct vfsmount *next_peer(struct vfsmount *p)
  15. {
  16. return list_entry(p->mnt_share.next, struct vfsmount, mnt_share);
  17. }
  18. void change_mnt_propagation(struct vfsmount *mnt, int type)
  19. {
  20. if (type == MS_SHARED) {
  21. set_mnt_shared(mnt);
  22. } else {
  23. list_del_init(&mnt->mnt_share);
  24. mnt->mnt_flags &= ~MNT_PNODE_MASK;
  25. }
  26. }
  27. /*
  28. * get the next mount in the propagation tree.
  29. * @m: the mount seen last
  30. * @origin: the original mount from where the tree walk initiated
  31. */
  32. static struct vfsmount *propagation_next(struct vfsmount *m,
  33. struct vfsmount *origin)
  34. {
  35. m = next_peer(m);
  36. if (m == origin)
  37. return NULL;
  38. return m;
  39. }
  40. /*
  41. * mount 'source_mnt' under the destination 'dest_mnt' at
  42. * dentry 'dest_dentry'. And propagate that mount to
  43. * all the peer and slave mounts of 'dest_mnt'.
  44. * Link all the new mounts into a propagation tree headed at
  45. * source_mnt. Also link all the new mounts using ->mnt_list
  46. * headed at source_mnt's ->mnt_list
  47. *
  48. * @dest_mnt: destination mount.
  49. * @dest_dentry: destination dentry.
  50. * @source_mnt: source mount.
  51. * @tree_list : list of heads of trees to be attached.
  52. */
  53. int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry,
  54. struct vfsmount *source_mnt, struct list_head *tree_list)
  55. {
  56. struct vfsmount *m, *child;
  57. int ret = 0;
  58. struct vfsmount *prev_dest_mnt = dest_mnt;
  59. struct vfsmount *prev_src_mnt = source_mnt;
  60. LIST_HEAD(tmp_list);
  61. LIST_HEAD(umount_list);
  62. for (m = propagation_next(dest_mnt, dest_mnt); m;
  63. m = propagation_next(m, dest_mnt)) {
  64. int type = CL_PROPAGATION;
  65. if (IS_MNT_NEW(m))
  66. continue;
  67. if (IS_MNT_SHARED(m))
  68. type |= CL_MAKE_SHARED;
  69. if (!(child = copy_tree(source_mnt, source_mnt->mnt_root,
  70. type))) {
  71. ret = -ENOMEM;
  72. list_splice(tree_list, tmp_list.prev);
  73. goto out;
  74. }
  75. if (is_subdir(dest_dentry, m->mnt_root)) {
  76. mnt_set_mountpoint(m, dest_dentry, child);
  77. list_add_tail(&child->mnt_hash, tree_list);
  78. } else {
  79. /*
  80. * This can happen if the parent mount was bind mounted
  81. * on some subdirectory of a shared/slave mount.
  82. */
  83. list_add_tail(&child->mnt_hash, &tmp_list);
  84. }
  85. prev_dest_mnt = m;
  86. prev_src_mnt = child;
  87. }
  88. out:
  89. spin_lock(&vfsmount_lock);
  90. while (!list_empty(&tmp_list)) {
  91. child = list_entry(tmp_list.next, struct vfsmount, mnt_hash);
  92. list_del_init(&child->mnt_hash);
  93. umount_tree(child, 0, &umount_list);
  94. }
  95. spin_unlock(&vfsmount_lock);
  96. release_mounts(&umount_list);
  97. return ret;
  98. }
  99. /*
  100. * return true if the refcount is greater than count
  101. */
  102. static inline int do_refcount_check(struct vfsmount *mnt, int count)
  103. {
  104. int mycount = atomic_read(&mnt->mnt_count);
  105. return (mycount > count);
  106. }
  107. /*
  108. * check if the mount 'mnt' can be unmounted successfully.
  109. * @mnt: the mount to be checked for unmount
  110. * NOTE: unmounting 'mnt' would naturally propagate to all
  111. * other mounts its parent propagates to.
  112. * Check if any of these mounts that **do not have submounts**
  113. * have more references than 'refcnt'. If so return busy.
  114. */
  115. int propagate_mount_busy(struct vfsmount *mnt, int refcnt)
  116. {
  117. struct vfsmount *m, *child;
  118. struct vfsmount *parent = mnt->mnt_parent;
  119. int ret = 0;
  120. if (mnt == parent)
  121. return do_refcount_check(mnt, refcnt);
  122. /*
  123. * quickly check if the current mount can be unmounted.
  124. * If not, we don't have to go checking for all other
  125. * mounts
  126. */
  127. if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
  128. return 1;
  129. for (m = propagation_next(parent, parent); m;
  130. m = propagation_next(m, parent)) {
  131. child = __lookup_mnt(m, mnt->mnt_mountpoint, 0);
  132. if (child && list_empty(&child->mnt_mounts) &&
  133. (ret = do_refcount_check(child, 1)))
  134. break;
  135. }
  136. return ret;
  137. }
  138. /*
  139. * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
  140. * parent propagates to.
  141. */
  142. static void __propagate_umount(struct vfsmount *mnt)
  143. {
  144. struct vfsmount *parent = mnt->mnt_parent;
  145. struct vfsmount *m;
  146. BUG_ON(parent == mnt);
  147. for (m = propagation_next(parent, parent); m;
  148. m = propagation_next(m, parent)) {
  149. struct vfsmount *child = __lookup_mnt(m,
  150. mnt->mnt_mountpoint, 0);
  151. /*
  152. * umount the child only if the child has no
  153. * other children
  154. */
  155. if (child && list_empty(&child->mnt_mounts)) {
  156. list_del(&child->mnt_hash);
  157. list_add_tail(&child->mnt_hash, &mnt->mnt_hash);
  158. }
  159. }
  160. }
  161. /*
  162. * collect all mounts that receive propagation from the mount in @list,
  163. * and return these additional mounts in the same list.
  164. * @list: the list of mounts to be unmounted.
  165. */
  166. int propagate_umount(struct list_head *list)
  167. {
  168. struct vfsmount *mnt;
  169. list_for_each_entry(mnt, list, mnt_hash)
  170. __propagate_umount(mnt);
  171. return 0;
  172. }