pnode.c 7.6 KB

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