pnode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 *master, *slave_mnt;
  60. if (list_empty(&mnt->mnt_share)) {
  61. if (IS_MNT_SHARED(mnt)) {
  62. mnt_release_group_id(mnt);
  63. CLEAR_MNT_SHARED(mnt);
  64. }
  65. master = mnt->mnt_master;
  66. if (!master) {
  67. struct list_head *p = &mnt->mnt_slave_list;
  68. while (!list_empty(p)) {
  69. slave_mnt = list_first_entry(p,
  70. struct mount, mnt_slave);
  71. list_del_init(&slave_mnt->mnt_slave);
  72. slave_mnt->mnt_master = NULL;
  73. }
  74. return 0;
  75. }
  76. } else {
  77. struct mount *m;
  78. /*
  79. * slave 'mnt' to a peer mount that has the
  80. * same root dentry. If none is available then
  81. * slave it to anything that is available.
  82. */
  83. for (m = master = next_peer(mnt); m != mnt; m = next_peer(m)) {
  84. if (m->mnt.mnt_root == mnt->mnt.mnt_root) {
  85. master = m;
  86. break;
  87. }
  88. }
  89. list_del_init(&mnt->mnt_share);
  90. mnt->mnt_group_id = 0;
  91. CLEAR_MNT_SHARED(mnt);
  92. }
  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. mnt->mnt_master = master;
  99. return 0;
  100. }
  101. /*
  102. * vfsmount lock must be held for write
  103. */
  104. void change_mnt_propagation(struct mount *mnt, int type)
  105. {
  106. if (type == MS_SHARED) {
  107. set_mnt_shared(mnt);
  108. return;
  109. }
  110. do_make_slave(mnt);
  111. if (type != MS_SLAVE) {
  112. list_del_init(&mnt->mnt_slave);
  113. mnt->mnt_master = NULL;
  114. if (type == MS_UNBINDABLE)
  115. mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
  116. else
  117. mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
  118. }
  119. }
  120. /*
  121. * get the next mount in the propagation tree.
  122. * @m: the mount seen last
  123. * @origin: the original mount from where the tree walk initiated
  124. *
  125. * Note that peer groups form contiguous segments of slave lists.
  126. * We rely on that in get_source() to be able to find out if
  127. * vfsmount found while iterating with propagation_next() is
  128. * a peer of one we'd found earlier.
  129. */
  130. static struct mount *propagation_next(struct mount *m,
  131. struct mount *origin)
  132. {
  133. /* are there any slaves of this mount? */
  134. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  135. return first_slave(m);
  136. while (1) {
  137. struct mount *master = m->mnt_master;
  138. if (master == origin->mnt_master) {
  139. struct mount *next = next_peer(m);
  140. return (next == origin) ? NULL : next;
  141. } else if (m->mnt_slave.next != &master->mnt_slave_list)
  142. return next_slave(m);
  143. /* back at master */
  144. m = master;
  145. }
  146. }
  147. static struct mount *next_group(struct mount *m, struct mount *origin)
  148. {
  149. while (1) {
  150. while (1) {
  151. struct mount *next;
  152. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  153. return first_slave(m);
  154. next = next_peer(m);
  155. if (m->mnt_group_id == origin->mnt_group_id) {
  156. if (next == origin)
  157. return NULL;
  158. } else if (m->mnt_slave.next != &next->mnt_slave)
  159. break;
  160. m = next;
  161. }
  162. /* m is the last peer */
  163. while (1) {
  164. struct mount *master = m->mnt_master;
  165. if (m->mnt_slave.next != &master->mnt_slave_list)
  166. return next_slave(m);
  167. m = next_peer(master);
  168. if (master->mnt_group_id == origin->mnt_group_id)
  169. break;
  170. if (master->mnt_slave.next == &m->mnt_slave)
  171. break;
  172. m = master;
  173. }
  174. if (m == origin)
  175. return NULL;
  176. }
  177. }
  178. /* all accesses are serialized by namespace_sem */
  179. static struct user_namespace *user_ns;
  180. static struct mount *last_dest, *first_source, *last_source, *dest_master;
  181. static struct mountpoint *mp;
  182. static struct hlist_head *list;
  183. static inline bool peers(struct mount *m1, struct mount *m2)
  184. {
  185. return m1->mnt_group_id == m2->mnt_group_id && m1->mnt_group_id;
  186. }
  187. static int propagate_one(struct mount *m)
  188. {
  189. struct mount *child;
  190. int type;
  191. /* skip ones added by this propagate_mnt() */
  192. if (IS_MNT_NEW(m))
  193. return 0;
  194. /* skip if mountpoint isn't covered by it */
  195. if (!is_subdir(mp->m_dentry, m->mnt.mnt_root))
  196. return 0;
  197. if (peers(m, last_dest)) {
  198. type = CL_MAKE_SHARED;
  199. } else {
  200. struct mount *n, *p;
  201. bool done;
  202. for (n = m; ; n = p) {
  203. p = n->mnt_master;
  204. if (p == dest_master || IS_MNT_MARKED(p))
  205. break;
  206. }
  207. do {
  208. struct mount *parent = last_source->mnt_parent;
  209. if (last_source == first_source)
  210. break;
  211. done = parent->mnt_master == p;
  212. if (done && peers(n, parent))
  213. break;
  214. last_source = last_source->mnt_master;
  215. } while (!done);
  216. type = CL_SLAVE;
  217. /* beginning of peer group among the slaves? */
  218. if (IS_MNT_SHARED(m))
  219. type |= CL_MAKE_SHARED;
  220. }
  221. /* Notice when we are propagating across user namespaces */
  222. if (m->mnt_ns->user_ns != user_ns)
  223. type |= CL_UNPRIVILEGED;
  224. child = copy_tree(last_source, last_source->mnt.mnt_root, type);
  225. if (IS_ERR(child))
  226. return PTR_ERR(child);
  227. child->mnt.mnt_flags &= ~MNT_LOCKED;
  228. mnt_set_mountpoint(m, mp, child);
  229. last_dest = m;
  230. last_source = child;
  231. if (m->mnt_master != dest_master) {
  232. read_seqlock_excl(&mount_lock);
  233. SET_MNT_MARK(m->mnt_master);
  234. read_sequnlock_excl(&mount_lock);
  235. }
  236. hlist_add_head(&child->mnt_hash, list);
  237. return count_mounts(m->mnt_ns, child);
  238. }
  239. /*
  240. * mount 'source_mnt' under the destination 'dest_mnt' at
  241. * dentry 'dest_dentry'. And propagate that mount to
  242. * all the peer and slave mounts of 'dest_mnt'.
  243. * Link all the new mounts into a propagation tree headed at
  244. * source_mnt. Also link all the new mounts using ->mnt_list
  245. * headed at source_mnt's ->mnt_list
  246. *
  247. * @dest_mnt: destination mount.
  248. * @dest_dentry: destination dentry.
  249. * @source_mnt: source mount.
  250. * @tree_list : list of heads of trees to be attached.
  251. */
  252. int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
  253. struct mount *source_mnt, struct hlist_head *tree_list)
  254. {
  255. struct mount *m, *n;
  256. int ret = 0;
  257. /*
  258. * we don't want to bother passing tons of arguments to
  259. * propagate_one(); everything is serialized by namespace_sem,
  260. * so globals will do just fine.
  261. */
  262. user_ns = current->nsproxy->mnt_ns->user_ns;
  263. last_dest = dest_mnt;
  264. first_source = source_mnt;
  265. last_source = source_mnt;
  266. mp = dest_mp;
  267. list = tree_list;
  268. dest_master = dest_mnt->mnt_master;
  269. /* all peers of dest_mnt, except dest_mnt itself */
  270. for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
  271. ret = propagate_one(n);
  272. if (ret)
  273. goto out;
  274. }
  275. /* all slave groups */
  276. for (m = next_group(dest_mnt, dest_mnt); m;
  277. m = next_group(m, dest_mnt)) {
  278. /* everything in that slave group */
  279. n = m;
  280. do {
  281. ret = propagate_one(n);
  282. if (ret)
  283. goto out;
  284. n = next_peer(n);
  285. } while (n != m);
  286. }
  287. out:
  288. read_seqlock_excl(&mount_lock);
  289. hlist_for_each_entry(n, tree_list, mnt_hash) {
  290. m = n->mnt_parent;
  291. if (m->mnt_master != dest_mnt->mnt_master)
  292. CLEAR_MNT_MARK(m->mnt_master);
  293. }
  294. read_sequnlock_excl(&mount_lock);
  295. return ret;
  296. }
  297. static struct mount *find_topper(struct mount *mnt)
  298. {
  299. /* If there is exactly one mount covering mnt completely return it. */
  300. struct mount *child;
  301. if (!list_is_singular(&mnt->mnt_mounts))
  302. return NULL;
  303. child = list_first_entry(&mnt->mnt_mounts, struct mount, mnt_child);
  304. if (child->mnt_mountpoint != mnt->mnt.mnt_root)
  305. return NULL;
  306. return child;
  307. }
  308. /*
  309. * return true if the refcount is greater than count
  310. */
  311. static inline int do_refcount_check(struct mount *mnt, int count)
  312. {
  313. return mnt_get_count(mnt) > count;
  314. }
  315. /*
  316. * check if the mount 'mnt' can be unmounted successfully.
  317. * @mnt: the mount to be checked for unmount
  318. * NOTE: unmounting 'mnt' would naturally propagate to all
  319. * other mounts its parent propagates to.
  320. * Check if any of these mounts that **do not have submounts**
  321. * have more references than 'refcnt'. If so return busy.
  322. *
  323. * vfsmount lock must be held for write
  324. */
  325. int propagate_mount_busy(struct mount *mnt, int refcnt)
  326. {
  327. struct mount *m, *child, *topper;
  328. struct mount *parent = mnt->mnt_parent;
  329. if (mnt == parent)
  330. return do_refcount_check(mnt, refcnt);
  331. /*
  332. * quickly check if the current mount can be unmounted.
  333. * If not, we don't have to go checking for all other
  334. * mounts
  335. */
  336. if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
  337. return 1;
  338. for (m = propagation_next(parent, parent); m;
  339. m = propagation_next(m, parent)) {
  340. int count = 1;
  341. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
  342. if (!child)
  343. continue;
  344. /* Is there exactly one mount on the child that covers
  345. * it completely whose reference should be ignored?
  346. */
  347. topper = find_topper(child);
  348. if (topper)
  349. count += 1;
  350. else if (!list_empty(&child->mnt_mounts))
  351. continue;
  352. if (do_refcount_check(child, count))
  353. return 1;
  354. }
  355. return 0;
  356. }
  357. /*
  358. * Clear MNT_LOCKED when it can be shown to be safe.
  359. *
  360. * mount_lock lock must be held for write
  361. */
  362. void propagate_mount_unlock(struct mount *mnt)
  363. {
  364. struct mount *parent = mnt->mnt_parent;
  365. struct mount *m, *child;
  366. BUG_ON(parent == mnt);
  367. for (m = propagation_next(parent, parent); m;
  368. m = propagation_next(m, parent)) {
  369. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
  370. if (child)
  371. child->mnt.mnt_flags &= ~MNT_LOCKED;
  372. }
  373. }
  374. /*
  375. * Mark all mounts that the MNT_LOCKED logic will allow to be unmounted.
  376. */
  377. static void mark_umount_candidates(struct mount *mnt)
  378. {
  379. struct mount *parent = mnt->mnt_parent;
  380. struct mount *m;
  381. BUG_ON(parent == mnt);
  382. for (m = propagation_next(parent, parent); m;
  383. m = propagation_next(m, parent)) {
  384. struct mount *child = __lookup_mnt(&m->mnt,
  385. mnt->mnt_mountpoint);
  386. if (!child || (child->mnt.mnt_flags & MNT_UMOUNT))
  387. continue;
  388. if (!IS_MNT_LOCKED(child) || IS_MNT_MARKED(m)) {
  389. SET_MNT_MARK(child);
  390. }
  391. }
  392. }
  393. /*
  394. * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
  395. * parent propagates to.
  396. */
  397. static void __propagate_umount(struct mount *mnt)
  398. {
  399. struct mount *parent = mnt->mnt_parent;
  400. struct mount *m;
  401. BUG_ON(parent == mnt);
  402. for (m = propagation_next(parent, parent); m;
  403. m = propagation_next(m, parent)) {
  404. struct mount *topper;
  405. struct mount *child = __lookup_mnt(&m->mnt,
  406. mnt->mnt_mountpoint);
  407. /*
  408. * umount the child only if the child has no children
  409. * and the child is marked safe to unmount.
  410. */
  411. if (!child || !IS_MNT_MARKED(child))
  412. continue;
  413. CLEAR_MNT_MARK(child);
  414. /* If there is exactly one mount covering all of child
  415. * replace child with that mount.
  416. */
  417. topper = find_topper(child);
  418. if (topper)
  419. mnt_change_mountpoint(child->mnt_parent, child->mnt_mp,
  420. topper);
  421. if (list_empty(&child->mnt_mounts)) {
  422. list_del_init(&child->mnt_child);
  423. child->mnt.mnt_flags |= MNT_UMOUNT;
  424. list_move_tail(&child->mnt_list, &mnt->mnt_list);
  425. }
  426. }
  427. }
  428. /*
  429. * collect all mounts that receive propagation from the mount in @list,
  430. * and return these additional mounts in the same list.
  431. * @list: the list of mounts to be unmounted.
  432. *
  433. * vfsmount lock must be held for write
  434. */
  435. int propagate_umount(struct list_head *list)
  436. {
  437. struct mount *mnt;
  438. list_for_each_entry_reverse(mnt, list, mnt_list)
  439. mark_umount_candidates(mnt);
  440. list_for_each_entry(mnt, list, mnt_list)
  441. __propagate_umount(mnt);
  442. return 0;
  443. }