pnode.c 6.0 KB

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