pnode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 mount *next_peer(struct mount *p)
  16. {
  17. return list_entry(p->mnt_share.next, struct mount, mnt_share);
  18. }
  19. static inline struct mount *first_slave(struct mount *p)
  20. {
  21. return list_entry(p->mnt_slave_list.next, struct mount, mnt_slave);
  22. }
  23. static inline struct mount *next_slave(struct mount *p)
  24. {
  25. return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
  26. }
  27. static struct mount *get_peer_under_root(struct mount *mnt,
  28. struct mnt_namespace *ns,
  29. const struct path *root)
  30. {
  31. struct mount *m = mnt;
  32. do {
  33. /* Check the namespace first for optimization */
  34. if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.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 mount *mnt, const struct path *root)
  47. {
  48. struct mount *m;
  49. for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
  50. struct mount *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 mount *mnt)
  57. {
  58. struct mount *peer_mnt = mnt, *master = mnt->mnt_master;
  59. struct mount *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.mnt_root != mnt->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 mount, 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 mount *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.mnt_flags |= MNT_UNBINDABLE;
  112. else
  113. mnt->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 mount *propagation_next(struct mount *m,
  127. struct mount *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 mount *master = m->mnt_master;
  134. if (master == origin->mnt_master) {
  135. struct mount *next = next_peer(m);
  136. return (next == origin) ? NULL : next;
  137. } else if (m->mnt_slave.next != &master->mnt_slave_list)
  138. return next_slave(m);
  139. /* back at master */
  140. m = master;
  141. }
  142. }
  143. static struct mount *next_group(struct mount *m, struct mount *origin)
  144. {
  145. while (1) {
  146. while (1) {
  147. struct mount *next;
  148. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  149. return first_slave(m);
  150. next = next_peer(m);
  151. if (m->mnt_group_id == origin->mnt_group_id) {
  152. if (next == origin)
  153. return NULL;
  154. } else if (m->mnt_slave.next != &next->mnt_slave)
  155. break;
  156. m = next;
  157. }
  158. /* m is the last peer */
  159. while (1) {
  160. struct mount *master = m->mnt_master;
  161. if (m->mnt_slave.next != &master->mnt_slave_list)
  162. return next_slave(m);
  163. m = next_peer(master);
  164. if (master->mnt_group_id == origin->mnt_group_id)
  165. break;
  166. if (master->mnt_slave.next == &m->mnt_slave)
  167. break;
  168. m = master;
  169. }
  170. if (m == origin)
  171. return NULL;
  172. }
  173. }
  174. /* all accesses are serialized by namespace_sem */
  175. static struct mount *last_dest, *last_source, *dest_master;
  176. static struct dentry *mp_dentry;
  177. static struct list_head *list;
  178. static int propagate_one(struct mount *m)
  179. {
  180. struct mount *child;
  181. int type;
  182. /* skip ones added by this propagate_mnt() */
  183. if (IS_MNT_NEW(m))
  184. return 0;
  185. /* skip if mountpoint isn't covered by it */
  186. if (!is_subdir(mp_dentry, m->mnt.mnt_root))
  187. return 0;
  188. if (m->mnt_group_id == last_dest->mnt_group_id) {
  189. type = CL_MAKE_SHARED;
  190. } else {
  191. struct mount *n, *p;
  192. for (n = m; ; n = p) {
  193. p = n->mnt_master;
  194. if (p == dest_master || IS_MNT_MARKED(p)) {
  195. while (last_dest->mnt_master != p) {
  196. last_source = last_source->mnt_master;
  197. last_dest = last_source->mnt_parent;
  198. }
  199. if (n->mnt_group_id != last_dest->mnt_group_id) {
  200. last_source = last_source->mnt_master;
  201. last_dest = last_source->mnt_parent;
  202. }
  203. break;
  204. }
  205. }
  206. type = CL_SLAVE;
  207. /* beginning of peer group among the slaves? */
  208. if (IS_MNT_SHARED(m))
  209. type |= CL_MAKE_SHARED;
  210. }
  211. child = copy_tree(last_source, last_source->mnt.mnt_root, type);
  212. if (IS_ERR(child))
  213. return PTR_ERR(child);
  214. mnt_set_mountpoint(m, mp_dentry, child);
  215. last_dest = m;
  216. last_source = child;
  217. if (m->mnt_master != dest_master) {
  218. br_write_lock(&vfsmount_lock);
  219. SET_MNT_MARK(m->mnt_master);
  220. br_write_unlock(&vfsmount_lock);
  221. }
  222. list_add_tail(&child->mnt_hash, list);
  223. return 0;
  224. }
  225. /*
  226. * mount 'source_mnt' under the destination 'dest_mnt' at
  227. * dentry 'dest_dentry'. And propagate that mount to
  228. * all the peer and slave mounts of 'dest_mnt'.
  229. * Link all the new mounts into a propagation tree headed at
  230. * source_mnt. Also link all the new mounts using ->mnt_list
  231. * headed at source_mnt's ->mnt_list
  232. *
  233. * @dest_mnt: destination mount.
  234. * @dest_dentry: destination dentry.
  235. * @source_mnt: source mount.
  236. * @tree_list : list of heads of trees to be attached.
  237. */
  238. int propagate_mnt(struct mount *dest_mnt, struct dentry *dest_dentry,
  239. struct mount *source_mnt, struct list_head *tree_list)
  240. {
  241. struct mount *m, *n;
  242. int ret = 0;
  243. /*
  244. * we don't want to bother passing tons of arguments to
  245. * propagate_one(); everything is serialized by namespace_sem,
  246. * so globals will do just fine.
  247. */
  248. last_dest = dest_mnt;
  249. last_source = source_mnt;
  250. mp_dentry = dest_dentry;
  251. list = tree_list;
  252. dest_master = dest_mnt->mnt_master;
  253. /* all peers of dest_mnt, except dest_mnt itself */
  254. for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
  255. ret = propagate_one(n);
  256. if (ret)
  257. goto out;
  258. }
  259. /* all slave groups */
  260. for (m = next_group(dest_mnt, dest_mnt); m;
  261. m = next_group(m, dest_mnt)) {
  262. /* everything in that slave group */
  263. n = m;
  264. do {
  265. ret = propagate_one(n);
  266. if (ret)
  267. goto out;
  268. n = next_peer(n);
  269. } while (n != m);
  270. }
  271. out:
  272. br_write_lock(&vfsmount_lock);
  273. list_for_each_entry(n, tree_list, mnt_hash) {
  274. m = n->mnt_parent;
  275. if (m->mnt_master != dest_mnt->mnt_master)
  276. CLEAR_MNT_MARK(m->mnt_master);
  277. }
  278. br_write_unlock(&vfsmount_lock);
  279. return ret;
  280. }
  281. /*
  282. * return true if the refcount is greater than count
  283. */
  284. static inline int do_refcount_check(struct mount *mnt, int count)
  285. {
  286. int mycount = mnt_get_count(mnt) - mnt->mnt_ghosts;
  287. return (mycount > count);
  288. }
  289. /*
  290. * check if the mount 'mnt' can be unmounted successfully.
  291. * @mnt: the mount to be checked for unmount
  292. * NOTE: unmounting 'mnt' would naturally propagate to all
  293. * other mounts its parent propagates to.
  294. * Check if any of these mounts that **do not have submounts**
  295. * have more references than 'refcnt'. If so return busy.
  296. *
  297. * vfsmount lock must be held for write
  298. */
  299. int propagate_mount_busy(struct mount *mnt, int refcnt)
  300. {
  301. struct mount *m, *child;
  302. struct mount *parent = mnt->mnt_parent;
  303. int ret = 0;
  304. if (mnt == parent)
  305. return do_refcount_check(mnt, refcnt);
  306. /*
  307. * quickly check if the current mount can be unmounted.
  308. * If not, we don't have to go checking for all other
  309. * mounts
  310. */
  311. if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
  312. return 1;
  313. for (m = propagation_next(parent, parent); m;
  314. m = propagation_next(m, parent)) {
  315. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint, 0);
  316. if (child && list_empty(&child->mnt_mounts) &&
  317. (ret = do_refcount_check(child, 1)))
  318. break;
  319. }
  320. return ret;
  321. }
  322. /*
  323. * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
  324. * parent propagates to.
  325. */
  326. static void __propagate_umount(struct mount *mnt)
  327. {
  328. struct mount *parent = mnt->mnt_parent;
  329. struct mount *m;
  330. BUG_ON(parent == mnt);
  331. for (m = propagation_next(parent, parent); m;
  332. m = propagation_next(m, parent)) {
  333. struct mount *child = __lookup_mnt(&m->mnt,
  334. mnt->mnt_mountpoint, 0);
  335. /*
  336. * umount the child only if the child has no
  337. * other children
  338. */
  339. if (child && list_empty(&child->mnt_mounts)) {
  340. list_del_init(&child->mnt_child);
  341. list_move_tail(&child->mnt_hash, &mnt->mnt_hash);
  342. }
  343. }
  344. }
  345. /*
  346. * collect all mounts that receive propagation from the mount in @list,
  347. * and return these additional mounts in the same list.
  348. * @list: the list of mounts to be unmounted.
  349. *
  350. * vfsmount lock must be held for write
  351. */
  352. int propagate_umount(struct list_head *list)
  353. {
  354. struct mount *mnt;
  355. list_for_each_entry(mnt, list, mnt_hash)
  356. __propagate_umount(mnt);
  357. return 0;
  358. }
  359. /*
  360. * Iterates over all slaves, and slaves of slaves.
  361. */
  362. static struct mount *next_descendent(struct mount *root, struct mount *cur)
  363. {
  364. if (!IS_MNT_NEW(cur) && !list_empty(&cur->mnt_slave_list))
  365. return first_slave(cur);
  366. do {
  367. struct mount *master = cur->mnt_master;
  368. if (!master || cur->mnt_slave.next != &master->mnt_slave_list) {
  369. struct mount *next = next_slave(cur);
  370. return (next == root) ? NULL : next;
  371. }
  372. cur = master;
  373. } while (cur != root);
  374. return NULL;
  375. }
  376. void propagate_remount(struct mount *mnt)
  377. {
  378. struct mount *m = mnt;
  379. struct super_block *sb = mnt->mnt.mnt_sb;
  380. if (sb->s_op->copy_mnt_data) {
  381. m = next_descendent(mnt, m);
  382. while (m) {
  383. sb->s_op->copy_mnt_data(m->mnt.data, mnt->mnt.data);
  384. m = next_descendent(mnt, m);
  385. }
  386. }
  387. }