pnode.c 7.6 KB

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