pnode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 struct vfsmount *get_peer_under_root(struct vfsmount *mnt,
  28. struct mnt_namespace *ns,
  29. const struct path *root)
  30. {
  31. struct vfsmount *m = mnt;
  32. do {
  33. /* Check the namespace first for optimization */
  34. if (m->mnt_ns == ns && is_path_reachable(m, m->mnt_root, root))
  35. return m;
  36. m = next_peer(m);
  37. } while (m != mnt);
  38. return NULL;
  39. }
  40. /*
  41. * Get ID of closest dominating peer group having a representative
  42. * under the given root.
  43. *
  44. * Caller must hold namespace_sem
  45. */
  46. int get_dominating_id(struct vfsmount *mnt, const struct path *root)
  47. {
  48. struct vfsmount *m;
  49. for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
  50. struct vfsmount *d = get_peer_under_root(m, mnt->mnt_ns, root);
  51. if (d)
  52. return d->mnt_group_id;
  53. }
  54. return 0;
  55. }
  56. static int do_make_slave(struct vfsmount *mnt)
  57. {
  58. struct vfsmount *peer_mnt = mnt, *master = mnt->mnt_master;
  59. struct vfsmount *slave_mnt;
  60. /*
  61. * slave 'mnt' to a peer mount that has the
  62. * same root dentry. If none is available then
  63. * slave it to anything that is available.
  64. */
  65. while ((peer_mnt = next_peer(peer_mnt)) != mnt &&
  66. peer_mnt->mnt_root != mnt->mnt_root) ;
  67. if (peer_mnt == mnt) {
  68. peer_mnt = next_peer(mnt);
  69. if (peer_mnt == mnt)
  70. peer_mnt = NULL;
  71. }
  72. if (IS_MNT_SHARED(mnt) && list_empty(&mnt->mnt_share))
  73. mnt_release_group_id(mnt);
  74. list_del_init(&mnt->mnt_share);
  75. mnt->mnt_group_id = 0;
  76. if (peer_mnt)
  77. master = peer_mnt;
  78. if (master) {
  79. list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
  80. slave_mnt->mnt_master = master;
  81. list_move(&mnt->mnt_slave, &master->mnt_slave_list);
  82. list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
  83. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  84. } else {
  85. struct list_head *p = &mnt->mnt_slave_list;
  86. while (!list_empty(p)) {
  87. slave_mnt = list_first_entry(p,
  88. struct vfsmount, mnt_slave);
  89. list_del_init(&slave_mnt->mnt_slave);
  90. slave_mnt->mnt_master = NULL;
  91. }
  92. }
  93. mnt->mnt_master = master;
  94. CLEAR_MNT_SHARED(mnt);
  95. return 0;
  96. }
  97. /*
  98. * vfsmount lock must be held for write
  99. */
  100. void change_mnt_propagation(struct vfsmount *mnt, int type)
  101. {
  102. if (type == MS_SHARED) {
  103. set_mnt_shared(mnt);
  104. return;
  105. }
  106. do_make_slave(mnt);
  107. if (type != MS_SLAVE) {
  108. list_del_init(&mnt->mnt_slave);
  109. mnt->mnt_master = NULL;
  110. if (type == MS_UNBINDABLE)
  111. mnt->mnt_flags |= MNT_UNBINDABLE;
  112. else
  113. mnt->mnt_flags &= ~MNT_UNBINDABLE;
  114. }
  115. }
  116. /*
  117. * get the next mount in the propagation tree.
  118. * @m: the mount seen last
  119. * @origin: the original mount from where the tree walk initiated
  120. *
  121. * Note that peer groups form contiguous segments of slave lists.
  122. * We rely on that in get_source() to be able to find out if
  123. * vfsmount found while iterating with propagation_next() is
  124. * a peer of one we'd found earlier.
  125. */
  126. static struct vfsmount *propagation_next(struct vfsmount *m,
  127. struct vfsmount *origin)
  128. {
  129. /* are there any slaves of this mount? */
  130. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  131. return first_slave(m);
  132. while (1) {
  133. struct vfsmount *next;
  134. struct vfsmount *master = m->mnt_master;
  135. if (master == origin->mnt_master) {
  136. next = next_peer(m);
  137. return ((next == origin) ? NULL : next);
  138. } else if (m->mnt_slave.next != &master->mnt_slave_list)
  139. return next_slave(m);
  140. /* back at master */
  141. m = master;
  142. }
  143. }
  144. /*
  145. * return the source mount to be used for cloning
  146. *
  147. * @dest the current destination mount
  148. * @last_dest the last seen destination mount
  149. * @last_src the last seen source mount
  150. * @type return CL_SLAVE if the new mount has to be
  151. * cloned as a slave.
  152. */
  153. static struct vfsmount *get_source(struct vfsmount *dest,
  154. struct vfsmount *last_dest,
  155. struct vfsmount *last_src,
  156. int *type)
  157. {
  158. struct vfsmount *p_last_src = NULL;
  159. struct vfsmount *p_last_dest = NULL;
  160. while (last_dest != dest->mnt_master) {
  161. p_last_dest = last_dest;
  162. p_last_src = last_src;
  163. last_dest = last_dest->mnt_master;
  164. last_src = last_src->mnt_master;
  165. }
  166. if (p_last_dest) {
  167. do {
  168. p_last_dest = next_peer(p_last_dest);
  169. } while (IS_MNT_NEW(p_last_dest));
  170. /* is that a peer of the earlier? */
  171. if (dest == p_last_dest) {
  172. *type = CL_MAKE_SHARED;
  173. return p_last_src;
  174. }
  175. }
  176. /* slave of the earlier, then */
  177. *type = CL_SLAVE;
  178. /* beginning of peer group among the slaves? */
  179. if (IS_MNT_SHARED(dest))
  180. *type |= CL_MAKE_SHARED;
  181. return last_src;
  182. }
  183. /*
  184. * mount 'source_mnt' under the destination 'dest_mnt' at
  185. * dentry 'dest_dentry'. And propagate that mount to
  186. * all the peer and slave mounts of 'dest_mnt'.
  187. * Link all the new mounts into a propagation tree headed at
  188. * source_mnt. Also link all the new mounts using ->mnt_list
  189. * headed at source_mnt's ->mnt_list
  190. *
  191. * @dest_mnt: destination mount.
  192. * @dest_dentry: destination dentry.
  193. * @source_mnt: source mount.
  194. * @tree_list : list of heads of trees to be attached.
  195. */
  196. int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry,
  197. struct vfsmount *source_mnt, struct list_head *tree_list)
  198. {
  199. struct vfsmount *m, *child;
  200. int ret = 0;
  201. struct vfsmount *prev_dest_mnt = dest_mnt;
  202. struct vfsmount *prev_src_mnt = source_mnt;
  203. LIST_HEAD(tmp_list);
  204. LIST_HEAD(umount_list);
  205. for (m = propagation_next(dest_mnt, dest_mnt); m;
  206. m = propagation_next(m, dest_mnt)) {
  207. int type;
  208. struct vfsmount *source;
  209. if (IS_MNT_NEW(m))
  210. continue;
  211. source = get_source(m, prev_dest_mnt, prev_src_mnt, &type);
  212. if (!(child = copy_tree(source, source->mnt_root, type))) {
  213. ret = -ENOMEM;
  214. list_splice(tree_list, tmp_list.prev);
  215. goto out;
  216. }
  217. if (is_subdir(dest_dentry, m->mnt_root)) {
  218. mnt_set_mountpoint(m, dest_dentry, child);
  219. list_add_tail(&child->mnt_hash, tree_list);
  220. } else {
  221. /*
  222. * This can happen if the parent mount was bind mounted
  223. * on some subdirectory of a shared/slave mount.
  224. */
  225. list_add_tail(&child->mnt_hash, &tmp_list);
  226. }
  227. prev_dest_mnt = m;
  228. prev_src_mnt = child;
  229. }
  230. out:
  231. br_write_lock(vfsmount_lock);
  232. while (!list_empty(&tmp_list)) {
  233. child = list_first_entry(&tmp_list, struct vfsmount, mnt_hash);
  234. umount_tree(child, 0, &umount_list);
  235. }
  236. br_write_unlock(vfsmount_lock);
  237. release_mounts(&umount_list);
  238. return ret;
  239. }
  240. /*
  241. * return true if the refcount is greater than count
  242. */
  243. static inline int do_refcount_check(struct vfsmount *mnt, int count)
  244. {
  245. int mycount = mnt_get_count(mnt) - mnt->mnt_ghosts;
  246. return (mycount > count);
  247. }
  248. /*
  249. * check if the mount 'mnt' can be unmounted successfully.
  250. * @mnt: the mount to be checked for unmount
  251. * NOTE: unmounting 'mnt' would naturally propagate to all
  252. * other mounts its parent propagates to.
  253. * Check if any of these mounts that **do not have submounts**
  254. * have more references than 'refcnt'. If so return busy.
  255. *
  256. * vfsmount lock must be held for write
  257. */
  258. int propagate_mount_busy(struct vfsmount *mnt, int refcnt)
  259. {
  260. struct vfsmount *m;
  261. struct mount *child;
  262. struct vfsmount *parent = mnt->mnt_parent;
  263. int ret = 0;
  264. if (mnt == parent)
  265. return do_refcount_check(mnt, refcnt);
  266. /*
  267. * quickly check if the current mount can be unmounted.
  268. * If not, we don't have to go checking for all other
  269. * mounts
  270. */
  271. if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
  272. return 1;
  273. for (m = propagation_next(parent, parent); m;
  274. m = propagation_next(m, parent)) {
  275. child = __lookup_mnt(m, mnt->mnt_mountpoint, 0);
  276. if (child && list_empty(&child->mnt.mnt_mounts) &&
  277. (ret = do_refcount_check(&child->mnt, 1)))
  278. break;
  279. }
  280. return ret;
  281. }
  282. /*
  283. * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
  284. * parent propagates to.
  285. */
  286. static void __propagate_umount(struct vfsmount *mnt)
  287. {
  288. struct vfsmount *parent = mnt->mnt_parent;
  289. struct vfsmount *m;
  290. BUG_ON(parent == mnt);
  291. for (m = propagation_next(parent, parent); m;
  292. m = propagation_next(m, parent)) {
  293. struct mount *child = __lookup_mnt(m,
  294. mnt->mnt_mountpoint, 0);
  295. /*
  296. * umount the child only if the child has no
  297. * other children
  298. */
  299. if (child && list_empty(&child->mnt.mnt_mounts))
  300. list_move_tail(&child->mnt.mnt_hash, &mnt->mnt_hash);
  301. }
  302. }
  303. /*
  304. * collect all mounts that receive propagation from the mount in @list,
  305. * and return these additional mounts in the same list.
  306. * @list: the list of mounts to be unmounted.
  307. *
  308. * vfsmount lock must be held for write
  309. */
  310. int propagate_umount(struct list_head *list)
  311. {
  312. struct vfsmount *mnt;
  313. list_for_each_entry(mnt, list, mnt_hash)
  314. __propagate_umount(mnt);
  315. return 0;
  316. }