pnode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 (IS_MNT_SHARED(mnt) && list_empty(&mnt->mnt_share))
  74. mnt_release_group_id(mnt);
  75. list_del_init(&mnt->mnt_share);
  76. mnt->mnt_group_id = 0;
  77. if (peer_mnt)
  78. master = peer_mnt;
  79. if (master) {
  80. list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
  81. slave_mnt->mnt_master = master;
  82. list_move(&mnt->mnt_slave, &master->mnt_slave_list);
  83. list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
  84. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  85. } else {
  86. struct list_head *p = &mnt->mnt_slave_list;
  87. while (!list_empty(p)) {
  88. slave_mnt = list_first_entry(p,
  89. struct mount, mnt_slave);
  90. list_del_init(&slave_mnt->mnt_slave);
  91. slave_mnt->mnt_master = NULL;
  92. }
  93. }
  94. mnt->mnt_master = master;
  95. CLEAR_MNT_SHARED(mnt);
  96. return 0;
  97. }
  98. /*
  99. * vfsmount lock must be held for write
  100. */
  101. void change_mnt_propagation(struct mount *mnt, int type)
  102. {
  103. if (type == MS_SHARED) {
  104. set_mnt_shared(mnt);
  105. return;
  106. }
  107. do_make_slave(mnt);
  108. if (type != MS_SLAVE) {
  109. list_del_init(&mnt->mnt_slave);
  110. mnt->mnt_master = NULL;
  111. if (type == MS_UNBINDABLE)
  112. mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
  113. else
  114. mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
  115. }
  116. }
  117. /*
  118. * get the next mount in the propagation tree.
  119. * @m: the mount seen last
  120. * @origin: the original mount from where the tree walk initiated
  121. *
  122. * Note that peer groups form contiguous segments of slave lists.
  123. * We rely on that in get_source() to be able to find out if
  124. * vfsmount found while iterating with propagation_next() is
  125. * a peer of one we'd found earlier.
  126. */
  127. static struct mount *propagation_next(struct mount *m,
  128. struct mount *origin)
  129. {
  130. /* are there any slaves of this mount? */
  131. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  132. return first_slave(m);
  133. while (1) {
  134. struct mount *master = m->mnt_master;
  135. if (master == origin->mnt_master) {
  136. struct mount *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 mount *get_source(struct mount *dest,
  154. struct mount *last_dest,
  155. struct mount *last_src,
  156. int *type)
  157. {
  158. struct mount *p_last_src = NULL;
  159. struct mount *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 mount *dest_mnt, struct dentry *dest_dentry,
  197. struct mount *source_mnt, struct list_head *tree_list)
  198. {
  199. struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
  200. struct mount *m, *child;
  201. int ret = 0;
  202. struct mount *prev_dest_mnt = dest_mnt;
  203. struct mount *prev_src_mnt = source_mnt;
  204. LIST_HEAD(tmp_list);
  205. LIST_HEAD(umount_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_dentry, m->mnt.mnt_root)) {
  223. mnt_set_mountpoint(m, dest_dentry, 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, &umount_list);
  240. }
  241. br_write_unlock(&vfsmount_lock);
  242. release_mounts(&umount_list);
  243. return ret;
  244. }
  245. /*
  246. * return true if the refcount is greater than count
  247. */
  248. static inline int do_refcount_check(struct mount *mnt, int count)
  249. {
  250. int mycount = mnt_get_count(mnt) - mnt->mnt_ghosts;
  251. return (mycount > count);
  252. }
  253. /*
  254. * check if the mount 'mnt' can be unmounted successfully.
  255. * @mnt: the mount to be checked for unmount
  256. * NOTE: unmounting 'mnt' would naturally propagate to all
  257. * other mounts its parent propagates to.
  258. * Check if any of these mounts that **do not have submounts**
  259. * have more references than 'refcnt'. If so return busy.
  260. *
  261. * vfsmount lock must be held for write
  262. */
  263. int propagate_mount_busy(struct mount *mnt, int refcnt)
  264. {
  265. struct mount *m, *child;
  266. struct mount *parent = mnt->mnt_parent;
  267. int ret = 0;
  268. if (mnt == parent)
  269. return do_refcount_check(mnt, refcnt);
  270. /*
  271. * quickly check if the current mount can be unmounted.
  272. * If not, we don't have to go checking for all other
  273. * mounts
  274. */
  275. if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
  276. return 1;
  277. for (m = propagation_next(parent, parent); m;
  278. m = propagation_next(m, parent)) {
  279. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint, 0);
  280. if (child && list_empty(&child->mnt_mounts) &&
  281. (ret = do_refcount_check(child, 1)))
  282. break;
  283. }
  284. return ret;
  285. }
  286. /*
  287. * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
  288. * parent propagates to.
  289. */
  290. static void __propagate_umount(struct mount *mnt)
  291. {
  292. struct mount *parent = mnt->mnt_parent;
  293. struct mount *m;
  294. BUG_ON(parent == mnt);
  295. for (m = propagation_next(parent, parent); m;
  296. m = propagation_next(m, parent)) {
  297. struct mount *child = __lookup_mnt(&m->mnt,
  298. mnt->mnt_mountpoint, 0);
  299. /*
  300. * umount the child only if the child has no
  301. * other children
  302. */
  303. if (child && list_empty(&child->mnt_mounts))
  304. list_move_tail(&child->mnt_hash, &mnt->mnt_hash);
  305. }
  306. }
  307. /*
  308. * collect all mounts that receive propagation from the mount in @list,
  309. * and return these additional mounts in the same list.
  310. * @list: the list of mounts to be unmounted.
  311. *
  312. * vfsmount lock must be held for write
  313. */
  314. int propagate_umount(struct list_head *list)
  315. {
  316. struct mount *mnt;
  317. list_for_each_entry(mnt, list, mnt_hash)
  318. __propagate_umount(mnt);
  319. return 0;
  320. }