pnode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 *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 *peer_mnt = mnt, *master = mnt->mnt_master;
  64. struct mount *slave_mnt;
  65. /*
  66. * slave 'mnt' to a peer mount that has the
  67. * same root dentry. If none is available then
  68. * slave it to anything that is available.
  69. */
  70. while ((peer_mnt = next_peer(peer_mnt)) != mnt &&
  71. peer_mnt->mnt.mnt_root != mnt->mnt.mnt_root) ;
  72. if (peer_mnt == mnt) {
  73. peer_mnt = next_peer(mnt);
  74. if (peer_mnt == mnt)
  75. peer_mnt = NULL;
  76. }
  77. if (mnt->mnt_group_id && IS_MNT_SHARED(mnt) &&
  78. list_empty(&mnt->mnt_share))
  79. mnt_release_group_id(mnt);
  80. list_del_init(&mnt->mnt_share);
  81. mnt->mnt_group_id = 0;
  82. if (peer_mnt)
  83. master = peer_mnt;
  84. if (master) {
  85. list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
  86. slave_mnt->mnt_master = master;
  87. list_move(&mnt->mnt_slave, &master->mnt_slave_list);
  88. list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
  89. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  90. } else {
  91. struct list_head *p = &mnt->mnt_slave_list;
  92. while (!list_empty(p)) {
  93. slave_mnt = list_first_entry(p,
  94. struct mount, mnt_slave);
  95. list_del_init(&slave_mnt->mnt_slave);
  96. slave_mnt->mnt_master = NULL;
  97. }
  98. }
  99. mnt->mnt_master = master;
  100. CLEAR_MNT_SHARED(mnt);
  101. return 0;
  102. }
  103. /*
  104. * vfsmount lock must be held for write
  105. */
  106. void change_mnt_propagation(struct mount *mnt, int type)
  107. {
  108. if (type == MS_SHARED) {
  109. set_mnt_shared(mnt);
  110. return;
  111. }
  112. do_make_slave(mnt);
  113. if (type != MS_SLAVE) {
  114. list_del_init(&mnt->mnt_slave);
  115. mnt->mnt_master = NULL;
  116. if (type == MS_UNBINDABLE)
  117. mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
  118. else
  119. mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
  120. }
  121. }
  122. /*
  123. * get the next mount in the propagation tree.
  124. * @m: the mount seen last
  125. * @origin: the original mount from where the tree walk initiated
  126. *
  127. * Note that peer groups form contiguous segments of slave lists.
  128. * We rely on that in get_source() to be able to find out if
  129. * vfsmount found while iterating with propagation_next() is
  130. * a peer of one we'd found earlier.
  131. */
  132. static struct mount *propagation_next(struct mount *m,
  133. struct mount *origin)
  134. {
  135. /* are there any slaves of this mount? */
  136. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  137. return first_slave(m);
  138. while (1) {
  139. struct mount *master = m->mnt_master;
  140. if (master == origin->mnt_master) {
  141. struct mount *next = next_peer(m);
  142. return (next == origin) ? NULL : next;
  143. } else if (m->mnt_slave.next != &master->mnt_slave_list)
  144. return next_slave(m);
  145. /* back at master */
  146. m = master;
  147. }
  148. }
  149. static struct mount *skip_propagation_subtree(struct mount *m,
  150. struct mount *origin)
  151. {
  152. /*
  153. * Advance m such that propagation_next will not return
  154. * the slaves of m.
  155. */
  156. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  157. m = last_slave(m);
  158. return m;
  159. }
  160. static struct mount *next_group(struct mount *m, struct mount *origin)
  161. {
  162. while (1) {
  163. while (1) {
  164. struct mount *next;
  165. if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
  166. return first_slave(m);
  167. next = next_peer(m);
  168. if (m->mnt_group_id == origin->mnt_group_id) {
  169. if (next == origin)
  170. return NULL;
  171. } else if (m->mnt_slave.next != &next->mnt_slave)
  172. break;
  173. m = next;
  174. }
  175. /* m is the last peer */
  176. while (1) {
  177. struct mount *master = m->mnt_master;
  178. if (m->mnt_slave.next != &master->mnt_slave_list)
  179. return next_slave(m);
  180. m = next_peer(master);
  181. if (master->mnt_group_id == origin->mnt_group_id)
  182. break;
  183. if (master->mnt_slave.next == &m->mnt_slave)
  184. break;
  185. m = master;
  186. }
  187. if (m == origin)
  188. return NULL;
  189. }
  190. }
  191. /* all accesses are serialized by namespace_sem */
  192. static struct user_namespace *user_ns;
  193. static struct mount *last_dest, *first_source, *last_source, *dest_master;
  194. static struct mountpoint *mp;
  195. static struct hlist_head *list;
  196. static inline bool peers(struct mount *m1, 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)
  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(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 (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. /* Notice when we are propagating across user namespaces */
  235. if (m->mnt_ns->user_ns != user_ns)
  236. type |= CL_UNPRIVILEGED;
  237. child = copy_tree(last_source, last_source->mnt.mnt_root, type);
  238. if (IS_ERR(child))
  239. return PTR_ERR(child);
  240. child->mnt.mnt_flags &= ~MNT_LOCKED;
  241. mnt_set_mountpoint(m, mp, child);
  242. last_dest = m;
  243. last_source = child;
  244. if (m->mnt_master != dest_master) {
  245. read_seqlock_excl(&mount_lock);
  246. SET_MNT_MARK(m->mnt_master);
  247. read_sequnlock_excl(&mount_lock);
  248. }
  249. hlist_add_head(&child->mnt_hash, list);
  250. return count_mounts(m->mnt_ns, child);
  251. }
  252. /*
  253. * mount 'source_mnt' under the destination 'dest_mnt' at
  254. * dentry 'dest_dentry'. And propagate that mount to
  255. * all the peer and slave mounts of 'dest_mnt'.
  256. * Link all the new mounts into a propagation tree headed at
  257. * source_mnt. Also link all the new mounts using ->mnt_list
  258. * headed at source_mnt's ->mnt_list
  259. *
  260. * @dest_mnt: destination mount.
  261. * @dest_dentry: destination dentry.
  262. * @source_mnt: source mount.
  263. * @tree_list : list of heads of trees to be attached.
  264. */
  265. int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
  266. struct mount *source_mnt, struct hlist_head *tree_list)
  267. {
  268. struct mount *m, *n;
  269. int ret = 0;
  270. /*
  271. * we don't want to bother passing tons of arguments to
  272. * propagate_one(); everything is serialized by namespace_sem,
  273. * so globals will do just fine.
  274. */
  275. user_ns = current->nsproxy->mnt_ns->user_ns;
  276. last_dest = dest_mnt;
  277. first_source = source_mnt;
  278. last_source = source_mnt;
  279. mp = dest_mp;
  280. list = tree_list;
  281. dest_master = dest_mnt->mnt_master;
  282. /* all peers of dest_mnt, except dest_mnt itself */
  283. for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
  284. ret = propagate_one(n);
  285. if (ret)
  286. goto out;
  287. }
  288. /* all slave groups */
  289. for (m = next_group(dest_mnt, dest_mnt); m;
  290. m = next_group(m, dest_mnt)) {
  291. /* everything in that slave group */
  292. n = m;
  293. do {
  294. ret = propagate_one(n);
  295. if (ret)
  296. goto out;
  297. n = next_peer(n);
  298. } while (n != m);
  299. }
  300. out:
  301. read_seqlock_excl(&mount_lock);
  302. hlist_for_each_entry(n, tree_list, mnt_hash) {
  303. m = n->mnt_parent;
  304. if (m->mnt_master != dest_mnt->mnt_master)
  305. CLEAR_MNT_MARK(m->mnt_master);
  306. }
  307. read_sequnlock_excl(&mount_lock);
  308. return ret;
  309. }
  310. static struct mount *find_topper(struct mount *mnt)
  311. {
  312. /* If there is exactly one mount covering mnt completely return it. */
  313. struct mount *child;
  314. if (!list_is_singular(&mnt->mnt_mounts))
  315. return NULL;
  316. child = list_first_entry(&mnt->mnt_mounts, struct mount, mnt_child);
  317. if (child->mnt_mountpoint != mnt->mnt.mnt_root)
  318. return NULL;
  319. return child;
  320. }
  321. /*
  322. * return true if the refcount is greater than count
  323. */
  324. static inline int do_refcount_check(struct mount *mnt, int count)
  325. {
  326. return mnt_get_count(mnt) > count;
  327. }
  328. /*
  329. * check if the mount 'mnt' can be unmounted successfully.
  330. * @mnt: the mount to be checked for unmount
  331. * NOTE: unmounting 'mnt' would naturally propagate to all
  332. * other mounts its parent propagates to.
  333. * Check if any of these mounts that **do not have submounts**
  334. * have more references than 'refcnt'. If so return busy.
  335. *
  336. * vfsmount lock must be held for write
  337. */
  338. int propagate_mount_busy(struct mount *mnt, int refcnt)
  339. {
  340. struct mount *m, *child, *topper;
  341. struct mount *parent = mnt->mnt_parent;
  342. if (mnt == parent)
  343. return do_refcount_check(mnt, refcnt);
  344. /*
  345. * quickly check if the current mount can be unmounted.
  346. * If not, we don't have to go checking for all other
  347. * mounts
  348. */
  349. if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
  350. return 1;
  351. for (m = propagation_next(parent, parent); m;
  352. m = propagation_next(m, parent)) {
  353. int count = 1;
  354. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
  355. if (!child)
  356. continue;
  357. /* Is there exactly one mount on the child that covers
  358. * it completely whose reference should be ignored?
  359. */
  360. topper = find_topper(child);
  361. if (topper)
  362. count += 1;
  363. else if (!list_empty(&child->mnt_mounts))
  364. continue;
  365. if (do_refcount_check(child, count))
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. /*
  371. * Clear MNT_LOCKED when it can be shown to be safe.
  372. *
  373. * mount_lock lock must be held for write
  374. */
  375. void propagate_mount_unlock(struct mount *mnt)
  376. {
  377. struct mount *parent = mnt->mnt_parent;
  378. struct mount *m, *child;
  379. BUG_ON(parent == mnt);
  380. for (m = propagation_next(parent, parent); m;
  381. m = propagation_next(m, parent)) {
  382. child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
  383. if (child)
  384. child->mnt.mnt_flags &= ~MNT_LOCKED;
  385. }
  386. }
  387. static void umount_one(struct mount *mnt, struct list_head *to_umount)
  388. {
  389. CLEAR_MNT_MARK(mnt);
  390. mnt->mnt.mnt_flags |= MNT_UMOUNT;
  391. list_del_init(&mnt->mnt_child);
  392. list_del_init(&mnt->mnt_umounting);
  393. list_move_tail(&mnt->mnt_list, to_umount);
  394. }
  395. /*
  396. * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
  397. * parent propagates to.
  398. */
  399. static bool __propagate_umount(struct mount *mnt,
  400. struct list_head *to_umount,
  401. struct list_head *to_restore)
  402. {
  403. bool progress = false;
  404. struct mount *child;
  405. /*
  406. * The state of the parent won't change if this mount is
  407. * already unmounted or marked as without children.
  408. */
  409. if (mnt->mnt.mnt_flags & (MNT_UMOUNT | MNT_MARKED))
  410. goto out;
  411. /* Verify topper is the only grandchild that has not been
  412. * speculatively unmounted.
  413. */
  414. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  415. if (child->mnt_mountpoint == mnt->mnt.mnt_root)
  416. continue;
  417. if (!list_empty(&child->mnt_umounting) && IS_MNT_MARKED(child))
  418. continue;
  419. /* Found a mounted child */
  420. goto children;
  421. }
  422. /* Mark mounts that can be unmounted if not locked */
  423. SET_MNT_MARK(mnt);
  424. progress = true;
  425. /* If a mount is without children and not locked umount it. */
  426. if (!IS_MNT_LOCKED(mnt)) {
  427. umount_one(mnt, to_umount);
  428. } else {
  429. children:
  430. list_move_tail(&mnt->mnt_umounting, to_restore);
  431. }
  432. out:
  433. return progress;
  434. }
  435. static void umount_list(struct list_head *to_umount,
  436. struct list_head *to_restore)
  437. {
  438. struct mount *mnt, *child, *tmp;
  439. list_for_each_entry(mnt, to_umount, mnt_list) {
  440. list_for_each_entry_safe(child, tmp, &mnt->mnt_mounts, mnt_child) {
  441. /* topper? */
  442. if (child->mnt_mountpoint == mnt->mnt.mnt_root)
  443. list_move_tail(&child->mnt_umounting, to_restore);
  444. else
  445. umount_one(child, to_umount);
  446. }
  447. }
  448. }
  449. static void restore_mounts(struct list_head *to_restore)
  450. {
  451. /* Restore mounts to a clean working state */
  452. while (!list_empty(to_restore)) {
  453. struct mount *mnt, *parent;
  454. struct mountpoint *mp;
  455. mnt = list_first_entry(to_restore, struct mount, mnt_umounting);
  456. CLEAR_MNT_MARK(mnt);
  457. list_del_init(&mnt->mnt_umounting);
  458. /* Should this mount be reparented? */
  459. mp = mnt->mnt_mp;
  460. parent = mnt->mnt_parent;
  461. while (parent->mnt.mnt_flags & MNT_UMOUNT) {
  462. mp = parent->mnt_mp;
  463. parent = parent->mnt_parent;
  464. }
  465. if (parent != mnt->mnt_parent)
  466. mnt_change_mountpoint(parent, mp, mnt);
  467. }
  468. }
  469. static void cleanup_umount_visitations(struct list_head *visited)
  470. {
  471. while (!list_empty(visited)) {
  472. struct mount *mnt =
  473. list_first_entry(visited, struct mount, mnt_umounting);
  474. list_del_init(&mnt->mnt_umounting);
  475. }
  476. }
  477. /*
  478. * collect all mounts that receive propagation from the mount in @list,
  479. * and return these additional mounts in the same list.
  480. * @list: the list of mounts to be unmounted.
  481. *
  482. * vfsmount lock must be held for write
  483. */
  484. int propagate_umount(struct list_head *list)
  485. {
  486. struct mount *mnt;
  487. LIST_HEAD(to_restore);
  488. LIST_HEAD(to_umount);
  489. LIST_HEAD(visited);
  490. /* Find candidates for unmounting */
  491. list_for_each_entry_reverse(mnt, list, mnt_list) {
  492. struct mount *parent = mnt->mnt_parent;
  493. struct mount *m;
  494. /*
  495. * If this mount has already been visited it is known that it's
  496. * entire peer group and all of their slaves in the propagation
  497. * tree for the mountpoint has already been visited and there is
  498. * no need to visit them again.
  499. */
  500. if (!list_empty(&mnt->mnt_umounting))
  501. continue;
  502. list_add_tail(&mnt->mnt_umounting, &visited);
  503. for (m = propagation_next(parent, parent); m;
  504. m = propagation_next(m, parent)) {
  505. struct mount *child = __lookup_mnt(&m->mnt,
  506. mnt->mnt_mountpoint);
  507. if (!child)
  508. continue;
  509. if (!list_empty(&child->mnt_umounting)) {
  510. /*
  511. * If the child has already been visited it is
  512. * know that it's entire peer group and all of
  513. * their slaves in the propgation tree for the
  514. * mountpoint has already been visited and there
  515. * is no need to visit this subtree again.
  516. */
  517. m = skip_propagation_subtree(m, parent);
  518. continue;
  519. } else if (child->mnt.mnt_flags & MNT_UMOUNT) {
  520. /*
  521. * We have come accross an partially unmounted
  522. * mount in list that has not been visited yet.
  523. * Remember it has been visited and continue
  524. * about our merry way.
  525. */
  526. list_add_tail(&child->mnt_umounting, &visited);
  527. continue;
  528. }
  529. /* Check the child and parents while progress is made */
  530. while (__propagate_umount(child,
  531. &to_umount, &to_restore)) {
  532. /* Is the parent a umount candidate? */
  533. child = child->mnt_parent;
  534. if (list_empty(&child->mnt_umounting))
  535. break;
  536. }
  537. }
  538. }
  539. umount_list(&to_umount, &to_restore);
  540. restore_mounts(&to_restore);
  541. cleanup_umount_visitations(&visited);
  542. list_splice_tail(&to_umount, list);
  543. return 0;
  544. }