pnode.c 9.0 KB

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