pnode.c 9.3 KB

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