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 "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. list_del_init(&mnt->mnt_share);
  44. if (peer_mnt)
  45. master = peer_mnt;
  46. if (master) {
  47. list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
  48. slave_mnt->mnt_master = master;
  49. list_move(&mnt->mnt_slave, &master->mnt_slave_list);
  50. list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
  51. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  52. } else {
  53. struct list_head *p = &mnt->mnt_slave_list;
  54. while (!list_empty(p)) {
  55. slave_mnt = list_first_entry(p,
  56. struct vfsmount, mnt_slave);
  57. list_del_init(&slave_mnt->mnt_slave);
  58. slave_mnt->mnt_master = NULL;
  59. }
  60. }
  61. mnt->mnt_master = master;
  62. CLEAR_MNT_SHARED(mnt);
  63. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  64. return 0;
  65. }
  66. void change_mnt_propagation(struct vfsmount *mnt, int type)
  67. {
  68. if (type == MS_SHARED) {
  69. set_mnt_shared(mnt);
  70. return;
  71. }
  72. do_make_slave(mnt);
  73. if (type != MS_SLAVE) {
  74. list_del_init(&mnt->mnt_slave);
  75. mnt->mnt_master = NULL;
  76. if (type == MS_UNBINDABLE)
  77. mnt->mnt_flags |= MNT_UNBINDABLE;
  78. else
  79. mnt->mnt_flags &= ~MNT_UNBINDABLE;
  80. }
  81. }
  82. /*
  83. * get the next mount in the propagation tree.
  84. * @m: the mount seen last
  85. * @origin: the original mount from where the tree walk initiated
  86. */
  87. static struct vfsmount *propagation_next(struct vfsmount *m,
  88. struct vfsmount *origin)
  89. {
  90. /* are there any slaves of this mount? */
  91. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  92. return first_slave(m);
  93. while (1) {
  94. struct vfsmount *next;
  95. struct vfsmount *master = m->mnt_master;
  96. if (master == origin->mnt_master) {
  97. next = next_peer(m);
  98. return ((next == origin) ? NULL : next);
  99. } else if (m->mnt_slave.next != &master->mnt_slave_list)
  100. return next_slave(m);
  101. /* back at master */
  102. m = master;
  103. }
  104. }
  105. /*
  106. * return the source mount to be used for cloning
  107. *
  108. * @dest the current destination mount
  109. * @last_dest the last seen destination mount
  110. * @last_src the last seen source mount
  111. * @type return CL_SLAVE if the new mount has to be
  112. * cloned as a slave.
  113. */
  114. static struct vfsmount *get_source(struct vfsmount *dest,
  115. struct vfsmount *last_dest,
  116. struct vfsmount *last_src,
  117. int *type)
  118. {
  119. struct vfsmount *p_last_src = NULL;
  120. struct vfsmount *p_last_dest = NULL;
  121. *type = CL_PROPAGATION;
  122. if (IS_MNT_SHARED(dest))
  123. *type |= CL_MAKE_SHARED;
  124. while (last_dest != dest->mnt_master) {
  125. p_last_dest = last_dest;
  126. p_last_src = last_src;
  127. last_dest = last_dest->mnt_master;
  128. last_src = last_src->mnt_master;
  129. }
  130. if (p_last_dest) {
  131. do {
  132. p_last_dest = next_peer(p_last_dest);
  133. } while (IS_MNT_NEW(p_last_dest));
  134. }
  135. if (dest != p_last_dest) {
  136. *type |= CL_SLAVE;
  137. return last_src;
  138. } else
  139. return p_last_src;
  140. }
  141. /*
  142. * mount 'source_mnt' under the destination 'dest_mnt' at
  143. * dentry 'dest_dentry'. And propagate that mount to
  144. * all the peer and slave mounts of 'dest_mnt'.
  145. * Link all the new mounts into a propagation tree headed at
  146. * source_mnt. Also link all the new mounts using ->mnt_list
  147. * headed at source_mnt's ->mnt_list
  148. *
  149. * @dest_mnt: destination mount.
  150. * @dest_dentry: destination dentry.
  151. * @source_mnt: source mount.
  152. * @tree_list : list of heads of trees to be attached.
  153. */
  154. int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry,
  155. struct vfsmount *source_mnt, struct list_head *tree_list)
  156. {
  157. struct vfsmount *m, *child;
  158. int ret = 0;
  159. struct vfsmount *prev_dest_mnt = dest_mnt;
  160. struct vfsmount *prev_src_mnt = source_mnt;
  161. LIST_HEAD(tmp_list);
  162. LIST_HEAD(umount_list);
  163. for (m = propagation_next(dest_mnt, dest_mnt); m;
  164. m = propagation_next(m, dest_mnt)) {
  165. int type;
  166. struct vfsmount *source;
  167. if (IS_MNT_NEW(m))
  168. continue;
  169. source = get_source(m, prev_dest_mnt, prev_src_mnt, &type);
  170. if (!(child = copy_tree(source, source->mnt_root, type))) {
  171. ret = -ENOMEM;
  172. list_splice(tree_list, tmp_list.prev);
  173. goto out;
  174. }
  175. if (is_subdir(dest_dentry, m->mnt_root)) {
  176. mnt_set_mountpoint(m, dest_dentry, child);
  177. list_add_tail(&child->mnt_hash, tree_list);
  178. } else {
  179. /*
  180. * This can happen if the parent mount was bind mounted
  181. * on some subdirectory of a shared/slave mount.
  182. */
  183. list_add_tail(&child->mnt_hash, &tmp_list);
  184. }
  185. prev_dest_mnt = m;
  186. prev_src_mnt = child;
  187. }
  188. out:
  189. spin_lock(&vfsmount_lock);
  190. while (!list_empty(&tmp_list)) {
  191. child = list_first_entry(&tmp_list, struct vfsmount, 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) - mnt->mnt_ghosts;
  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. }