pnode.c 7.7 KB

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