pnode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/pnode.c
  4. *
  5. * (C) Copyright IBM Corporation 2005.
  6. * Author : Ram Pai (linuxram@us.ibm.com)
  7. */
  8. #include <linux/mnt_namespace.h>
  9. #include <linux/mount.h>
  10. #include <linux/fs.h>
  11. #include <linux/nsproxy.h>
  12. #include <uapi/linux/mount.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 *last_slave(struct mount *p)
  25. {
  26. return list_entry(p->mnt_slave_list.prev, struct mount, mnt_slave);
  27. }
  28. static inline struct mount *next_slave(struct mount *p)
  29. {
  30. return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
  31. }
  32. static struct mount *get_peer_under_root(struct mount *mnt,
  33. struct mnt_namespace *ns,
  34. const struct path *root)
  35. {
  36. struct mount *m = mnt;
  37. do {
  38. /* Check the namespace first for optimization */
  39. if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
  40. return m;
  41. m = next_peer(m);
  42. } while (m != mnt);
  43. return NULL;
  44. }
  45. /*
  46. * Get ID of closest dominating peer group having a representative
  47. * under the given root.
  48. *
  49. * Caller must hold namespace_sem
  50. */
  51. int get_dominating_id(struct mount *mnt, const struct path *root)
  52. {
  53. struct mount *m;
  54. for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
  55. struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
  56. if (d)
  57. return d->mnt_group_id;
  58. }
  59. return 0;
  60. }
  61. static int do_make_slave(struct mount *mnt)
  62. {
  63. struct mount *master, *slave_mnt;
  64. if (list_empty(&mnt->mnt_share)) {
  65. if (IS_MNT_SHARED(mnt)) {
  66. mnt_release_group_id(mnt);
  67. CLEAR_MNT_SHARED(mnt);
  68. }
  69. master = mnt->mnt_master;
  70. if (!master) {
  71. struct list_head *p = &mnt->mnt_slave_list;
  72. while (!list_empty(p)) {
  73. slave_mnt = list_first_entry(p,
  74. struct mount, mnt_slave);
  75. list_del_init(&slave_mnt->mnt_slave);
  76. slave_mnt->mnt_master = NULL;
  77. }
  78. return 0;
  79. }
  80. } else {
  81. struct mount *m;
  82. /*
  83. * slave 'mnt' to a peer mount that has the
  84. * same root dentry. If none is available then
  85. * slave it to anything that is available.
  86. */
  87. for (m = master = next_peer(mnt); m != mnt; m = next_peer(m)) {
  88. if (m->mnt.mnt_root == mnt->mnt.mnt_root) {
  89. master = m;
  90. break;
  91. }
  92. }
  93. list_del_init(&mnt->mnt_share);
  94. mnt->mnt_group_id = 0;
  95. CLEAR_MNT_SHARED(mnt);
  96. }
  97. list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
  98. slave_mnt->mnt_master = master;
  99. list_move(&mnt->mnt_slave, &master->mnt_slave_list);
  100. list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
  101. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  102. mnt->mnt_master = master;
  103. return 0;
  104. }
  105. /*
  106. * vfsmount lock must be held for write
  107. */
  108. void change_mnt_propagation(struct mount *mnt, int type)
  109. {
  110. if (type == MS_SHARED) {
  111. set_mnt_shared(mnt);
  112. return;
  113. }
  114. do_make_slave(mnt);
  115. if (type != MS_SLAVE) {
  116. list_del_init(&mnt->mnt_slave);
  117. mnt->mnt_master = NULL;
  118. if (type == MS_UNBINDABLE)
  119. mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
  120. else
  121. mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
  122. }
  123. }
  124. /*
  125. * get the next mount in the propagation tree.
  126. * @m: the mount seen last
  127. * @origin: the original mount from where the tree walk initiated
  128. *
  129. * Note that peer groups form contiguous segments of slave lists.
  130. * We rely on that in get_source() to be able to find out if
  131. * vfsmount found while iterating with propagation_next() is
  132. * a peer of one we'd found earlier.
  133. */
  134. static struct mount *propagation_next(struct mount *m,
  135. struct mount *origin)
  136. {
  137. /* are there any slaves of this mount? */
  138. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  139. return first_slave(m);
  140. while (1) {
  141. struct mount *master = m->mnt_master;
  142. if (master == origin->mnt_master) {
  143. struct mount *next = next_peer(m);
  144. return (next == origin) ? NULL : next;
  145. } else if (m->mnt_slave.next != &master->mnt_slave_list)
  146. return next_slave(m);
  147. /* back at master */
  148. m = master;
  149. }
  150. }
  151. static struct mount *skip_propagation_subtree(struct mount *m,
  152. struct mount *origin)
  153. {
  154. /*
  155. * Advance m such that propagation_next will not return
  156. * the slaves of m.
  157. */
  158. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  159. m = last_slave(m);
  160. return m;
  161. }
  162. static struct mount *next_group(struct mount *m, struct mount *origin)
  163. {
  164. while (1) {
  165. while (1) {
  166. struct mount *next;
  167. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  168. return first_slave(m);
  169. next = next_peer(m);
  170. if (m->mnt_group_id == origin->mnt_group_id) {
  171. if (next == origin)
  172. return NULL;
  173. } else if (m->mnt_slave.next != &next->mnt_slave)
  174. break;
  175. m = next;
  176. }
  177. /* m is the last peer */
  178. while (1) {
  179. struct mount *master = m->mnt_master;
  180. if (m->mnt_slave.next != &master->mnt_slave_list)
  181. return next_slave(m);
  182. m = next_peer(master);
  183. if (master->mnt_group_id == origin->mnt_group_id)
  184. break;
  185. if (master->mnt_slave.next == &m->mnt_slave)
  186. break;
  187. m = master;
  188. }
  189. if (m == origin)
  190. return NULL;
  191. }
  192. }
  193. /* all accesses are serialized by namespace_sem */
  194. static struct mount *last_dest, *first_source, *last_source, *dest_master;
  195. static struct hlist_head *list;
  196. static inline bool peers(const struct mount *m1, const struct mount *m2)
  197. {
  198. return m1->mnt_group_id == m2->mnt_group_id && m1->mnt_group_id;
  199. }
  200. static int propagate_one(struct mount *m, struct mountpoint *dest_mp)
  201. {
  202. struct mount *child;
  203. int type;
  204. /* skip ones added by this propagate_mnt() */
  205. if (IS_MNT_NEW(m))
  206. return 0;
  207. /* skip if mountpoint isn't covered by it */
  208. if (!is_subdir(dest_mp->m_dentry, m->mnt.mnt_root))
  209. return 0;
  210. if (peers(m, last_dest)) {
  211. type = CL_MAKE_SHARED;
  212. } else {
  213. struct mount *n, *p;
  214. bool done;
  215. for (n = m; ; n = p) {
  216. p = n->mnt_master;
  217. if (p == dest_master || IS_MNT_MARKED(p))
  218. break;
  219. }
  220. do {
  221. struct mount *parent = last_source->mnt_parent;
  222. if (peers(last_source, first_source))
  223. break;
  224. done = parent->mnt_master == p;
  225. if (done && peers(n, parent))
  226. break;
  227. last_source = last_source->mnt_master;
  228. } while (!done);
  229. type = CL_SLAVE;
  230. /* beginning of peer group among the slaves? */
  231. if (IS_MNT_SHARED(m))
  232. type |= CL_MAKE_SHARED;
  233. }
  234. child = copy_tree(last_source, last_source->mnt.mnt_root, type);
  235. if (IS_ERR(child))
  236. return PTR_ERR(child);
  237. read_seqlock_excl(&mount_lock);
  238. mnt_set_mountpoint(m, dest_mp, child);
  239. if (m->mnt_master != dest_master)
  240. SET_MNT_MARK(m->mnt_master);
  241. read_sequnlock_excl(&mount_lock);
  242. last_dest = m;
  243. last_source = child;
  244. hlist_add_head(&child->mnt_hash, list);
  245. return count_mounts(m->mnt_ns, child);
  246. }
  247. /*
  248. * mount 'source_mnt' under the destination 'dest_mnt' at
  249. * dentry 'dest_dentry'. And propagate that mount to
  250. * all the peer and slave mounts of 'dest_mnt'.
  251. * Link all the new mounts into a propagation tree headed at
  252. * source_mnt. Also link all the new mounts using ->mnt_list
  253. * headed at source_mnt's ->mnt_list
  254. *
  255. * @dest_mnt: destination mount.
  256. * @dest_dentry: destination dentry.
  257. * @source_mnt: source mount.
  258. * @tree_list : list of heads of trees to be attached.
  259. */
  260. int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
  261. struct mount *source_mnt, struct hlist_head *tree_list)
  262. {
  263. struct mount *m, *n;
  264. int ret = 0;
  265. /*
  266. * we don't want to bother passing tons of arguments to
  267. * propagate_one(); everything is serialized by namespace_sem,
  268. * so globals will do just fine.
  269. */
  270. last_dest = dest_mnt;
  271. first_source = source_mnt;
  272. last_source = source_mnt;
  273. list = tree_list;
  274. dest_master = dest_mnt->mnt_master;
  275. /* all peers of dest_mnt, except dest_mnt itself */
  276. for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
  277. ret = propagate_one(n, dest_mp);
  278. if (ret)
  279. goto out;
  280. }
  281. /* all slave groups */
  282. for (m = next_group(dest_mnt, dest_mnt); m;
  283. m = next_group(m, dest_mnt)) {
  284. /* everything in that slave group */
  285. n = m;
  286. do {
  287. ret = propagate_one(n, dest_mp);
  288. if (ret)
  289. goto out;
  290. n = next_peer(n);
  291. } while (n != m);
  292. }
  293. out:
  294. read_seqlock_excl(&mount_lock);
  295. hlist_for_each_entry(n, tree_list, mnt_hash) {
  296. m = n->mnt_parent;
  297. if (m->mnt_master != dest_mnt->mnt_master)
  298. CLEAR_MNT_MARK(m->mnt_master);
  299. }
  300. read_sequnlock_excl(&mount_lock);
  301. return ret;
  302. }
  303. static struct mount *find_topper(struct mount *mnt)
  304. {
  305. /* If there is exactly one mount covering mnt completely return it. */
  306. struct mount *child;
  307. if (!list_is_singular(&mnt->mnt_mounts))
  308. return NULL;
  309. child = list_first_entry(&mnt->mnt_mounts, struct mount, mnt_child);
  310. if (child->mnt_mountpoint != mnt->mnt.mnt_root)
  311. return NULL;
  312. return child;
  313. }
  314. /*
  315. * return true if the refcount is greater than count
  316. */
  317. static inline int do_refcount_check(struct mount *mnt, int count)
  318. {
  319. return mnt_get_count(mnt) > count;
  320. }
  321. /**
  322. * propagation_would_overmount - check whether propagation from @from
  323. * would overmount @to
  324. * @from: shared mount
  325. * @to: mount to check
  326. * @mp: future mountpoint of @to on @from
  327. *
  328. * If @from propagates mounts to @to, @from and @to must either be peers
  329. * or one of the masters in the hierarchy of masters of @to must be a
  330. * peer of @from.
  331. *
  332. * If the root of the @to mount is equal to the future mountpoint @mp of
  333. * the @to mount on @from then @to will be overmounted by whatever is
  334. * propagated to it.
  335. *
  336. * Context: This function expects namespace_lock() to be held and that
  337. * @mp is stable.
  338. * Return: If @from overmounts @to, true is returned, false if not.
  339. */
  340. bool propagation_would_overmount(const struct mount *from,
  341. const struct mount *to,
  342. const struct mountpoint *mp)
  343. {
  344. if (!IS_MNT_SHARED(from))
  345. return false;
  346. if (IS_MNT_NEW(to))
  347. return false;
  348. if (to->mnt.mnt_root != mp->m_dentry)
  349. return false;
  350. for (const struct mount *m = to; m; m = m->mnt_master) {
  351. if (peers(from, m))
  352. return true;
  353. }
  354. return false;
  355. }
  356. /*
  357. * check if the mount 'mnt' can be unmounted successfully.
  358. * @mnt: the mount to be checked for unmount
  359. * NOTE: unmounting 'mnt' would naturally propagate to all
  360. * other mounts its parent propagates to.
  361. * Check if any of these mounts that **do not have submounts**
  362. * have more references than 'refcnt'. If so return busy.
  363. *
  364. * vfsmount lock must be held for write
  365. */
  366. int propagate_mount_busy(struct mount *mnt, int refcnt)
  367. {
  368. struct mount *m, *child, *topper;
  369. struct mount *parent = mnt->mnt_parent;
  370. if (mnt == parent)
  371. return do_refcount_check(mnt, refcnt);
  372. /*
  373. * quickly check if the current mount can be unmounted.
  374. * If not, we don't have to go checking for all other
  375. * mounts
  376. */
  377. if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
  378. return 1;
  379. for (m = propagation_next(parent, parent); m;
  380. m = propagation_next(m, parent)) {
  381. int count = 1;
  382. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
  383. if (!child)
  384. continue;
  385. /* Is there exactly one mount on the child that covers
  386. * it completely whose reference should be ignored?
  387. */
  388. topper = find_topper(child);
  389. if (topper)
  390. count += 1;
  391. else if (!list_empty(&child->mnt_mounts))
  392. continue;
  393. if (do_refcount_check(child, count))
  394. return 1;
  395. }
  396. return 0;
  397. }
  398. /*
  399. * Clear MNT_LOCKED when it can be shown to be safe.
  400. *
  401. * mount_lock lock must be held for write
  402. */
  403. void propagate_mount_unlock(struct mount *mnt)
  404. {
  405. struct mount *parent = mnt->mnt_parent;
  406. struct mount *m, *child;
  407. BUG_ON(parent == mnt);
  408. for (m = propagation_next(parent, parent); m;
  409. m = propagation_next(m, parent)) {
  410. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
  411. if (child)
  412. child->mnt.mnt_flags &= ~MNT_LOCKED;
  413. }
  414. }
  415. static void umount_one(struct mount *mnt, struct list_head *to_umount)
  416. {
  417. CLEAR_MNT_MARK(mnt);
  418. mnt->mnt.mnt_flags |= MNT_UMOUNT;
  419. list_del_init(&mnt->mnt_child);
  420. list_del_init(&mnt->mnt_umounting);
  421. move_from_ns(mnt, to_umount);
  422. }
  423. /*
  424. * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
  425. * parent propagates to.
  426. */
  427. static bool __propagate_umount(struct mount *mnt,
  428. struct list_head *to_umount,
  429. struct list_head *to_restore)
  430. {
  431. bool progress = false;
  432. struct mount *child;
  433. /*
  434. * The state of the parent won't change if this mount is
  435. * already unmounted or marked as without children.
  436. */
  437. if (mnt->mnt.mnt_flags & (MNT_UMOUNT | MNT_MARKED))
  438. goto out;
  439. /* Verify topper is the only grandchild that has not been
  440. * speculatively unmounted.
  441. */
  442. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  443. if (child->mnt_mountpoint == mnt->mnt.mnt_root)
  444. continue;
  445. if (!list_empty(&child->mnt_umounting) && IS_MNT_MARKED(child))
  446. continue;
  447. /* Found a mounted child */
  448. goto children;
  449. }
  450. /* Mark mounts that can be unmounted if not locked */
  451. SET_MNT_MARK(mnt);
  452. progress = true;
  453. /* If a mount is without children and not locked umount it. */
  454. if (!IS_MNT_LOCKED(mnt)) {
  455. umount_one(mnt, to_umount);
  456. } else {
  457. children:
  458. list_move_tail(&mnt->mnt_umounting, to_restore);
  459. }
  460. out:
  461. return progress;
  462. }
  463. static void umount_list(struct list_head *to_umount,
  464. struct list_head *to_restore)
  465. {
  466. struct mount *mnt, *child, *tmp;
  467. list_for_each_entry(mnt, to_umount, mnt_list) {
  468. list_for_each_entry_safe(child, tmp, &mnt->mnt_mounts, mnt_child) {
  469. /* topper? */
  470. if (child->mnt_mountpoint == mnt->mnt.mnt_root)
  471. list_move_tail(&child->mnt_umounting, to_restore);
  472. else
  473. umount_one(child, to_umount);
  474. }
  475. }
  476. }
  477. static void restore_mounts(struct list_head *to_restore)
  478. {
  479. /* Restore mounts to a clean working state */
  480. while (!list_empty(to_restore)) {
  481. struct mount *mnt, *parent;
  482. struct mountpoint *mp;
  483. mnt = list_first_entry(to_restore, struct mount, mnt_umounting);
  484. CLEAR_MNT_MARK(mnt);
  485. list_del_init(&mnt->mnt_umounting);
  486. /* Should this mount be reparented? */
  487. mp = mnt->mnt_mp;
  488. parent = mnt->mnt_parent;
  489. while (parent->mnt.mnt_flags & MNT_UMOUNT) {
  490. mp = parent->mnt_mp;
  491. parent = parent->mnt_parent;
  492. }
  493. if (parent != mnt->mnt_parent)
  494. mnt_change_mountpoint(parent, mp, mnt);
  495. }
  496. }
  497. static void cleanup_umount_visitations(struct list_head *visited)
  498. {
  499. while (!list_empty(visited)) {
  500. struct mount *mnt =
  501. list_first_entry(visited, struct mount, mnt_umounting);
  502. list_del_init(&mnt->mnt_umounting);
  503. }
  504. }
  505. /*
  506. * collect all mounts that receive propagation from the mount in @list,
  507. * and return these additional mounts in the same list.
  508. * @list: the list of mounts to be unmounted.
  509. *
  510. * vfsmount lock must be held for write
  511. */
  512. int propagate_umount(struct list_head *list)
  513. {
  514. struct mount *mnt;
  515. LIST_HEAD(to_restore);
  516. LIST_HEAD(to_umount);
  517. LIST_HEAD(visited);
  518. /* Find candidates for unmounting */
  519. list_for_each_entry_reverse(mnt, list, mnt_list) {
  520. struct mount *parent = mnt->mnt_parent;
  521. struct mount *m;
  522. /*
  523. * If this mount has already been visited it is known that it's
  524. * entire peer group and all of their slaves in the propagation
  525. * tree for the mountpoint has already been visited and there is
  526. * no need to visit them again.
  527. */
  528. if (!list_empty(&mnt->mnt_umounting))
  529. continue;
  530. list_add_tail(&mnt->mnt_umounting, &visited);
  531. for (m = propagation_next(parent, parent); m;
  532. m = propagation_next(m, parent)) {
  533. struct mount *child = __lookup_mnt(&m->mnt,
  534. mnt->mnt_mountpoint);
  535. if (!child)
  536. continue;
  537. if (!list_empty(&child->mnt_umounting)) {
  538. /*
  539. * If the child has already been visited it is
  540. * know that it's entire peer group and all of
  541. * their slaves in the propgation tree for the
  542. * mountpoint has already been visited and there
  543. * is no need to visit this subtree again.
  544. */
  545. m = skip_propagation_subtree(m, parent);
  546. continue;
  547. } else if (child->mnt.mnt_flags & MNT_UMOUNT) {
  548. /*
  549. * We have come accross an partially unmounted
  550. * mount in list that has not been visited yet.
  551. * Remember it has been visited and continue
  552. * about our merry way.
  553. */
  554. list_add_tail(&child->mnt_umounting, &visited);
  555. continue;
  556. }
  557. /* Check the child and parents while progress is made */
  558. while (__propagate_umount(child,
  559. &to_umount, &to_restore)) {
  560. /* Is the parent a umount candidate? */
  561. child = child->mnt_parent;
  562. if (list_empty(&child->mnt_umounting))
  563. break;
  564. }
  565. }
  566. }
  567. umount_list(&to_umount, &to_restore);
  568. restore_mounts(&to_restore);
  569. cleanup_umount_visitations(&visited);
  570. list_splice_tail(&to_umount, list);
  571. return 0;
  572. }