pnode.c 7.7 KB

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