pnode.c 7.7 KB

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