pnode.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/namespace.h>
  10. #include <linux/mount.h>
  11. #include <linux/fs.h>
  12. #include "pnode.h"
  13. /* return the next shared peer mount of @p */
  14. static inline struct vfsmount *next_peer(struct vfsmount *p)
  15. {
  16. return list_entry(p->mnt_share.next, struct vfsmount, mnt_share);
  17. }
  18. void change_mnt_propagation(struct vfsmount *mnt, int type)
  19. {
  20. if (type == MS_SHARED) {
  21. set_mnt_shared(mnt);
  22. } else {
  23. list_del_init(&mnt->mnt_share);
  24. mnt->mnt_flags &= ~MNT_PNODE_MASK;
  25. }
  26. }
  27. /*
  28. * get the next mount in the propagation tree.
  29. * @m: the mount seen last
  30. * @origin: the original mount from where the tree walk initiated
  31. */
  32. static struct vfsmount *propagation_next(struct vfsmount *m,
  33. struct vfsmount *origin)
  34. {
  35. m = next_peer(m);
  36. if (m == origin)
  37. return NULL;
  38. return m;
  39. }
  40. /*
  41. * mount 'source_mnt' under the destination 'dest_mnt' at
  42. * dentry 'dest_dentry'. And propagate that mount to
  43. * all the peer and slave mounts of 'dest_mnt'.
  44. * Link all the new mounts into a propagation tree headed at
  45. * source_mnt. Also link all the new mounts using ->mnt_list
  46. * headed at source_mnt's ->mnt_list
  47. *
  48. * @dest_mnt: destination mount.
  49. * @dest_dentry: destination dentry.
  50. * @source_mnt: source mount.
  51. * @tree_list : list of heads of trees to be attached.
  52. */
  53. int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry,
  54. struct vfsmount *source_mnt, struct list_head *tree_list)
  55. {
  56. struct vfsmount *m, *child;
  57. int ret = 0;
  58. struct vfsmount *prev_dest_mnt = dest_mnt;
  59. struct vfsmount *prev_src_mnt = source_mnt;
  60. LIST_HEAD(tmp_list);
  61. LIST_HEAD(umount_list);
  62. for (m = propagation_next(dest_mnt, dest_mnt); m;
  63. m = propagation_next(m, dest_mnt)) {
  64. int type = CL_PROPAGATION;
  65. if (IS_MNT_NEW(m))
  66. continue;
  67. if (IS_MNT_SHARED(m))
  68. type |= CL_MAKE_SHARED;
  69. if (!(child = copy_tree(source_mnt, source_mnt->mnt_root,
  70. type))) {
  71. ret = -ENOMEM;
  72. list_splice(tree_list, tmp_list.prev);
  73. goto out;
  74. }
  75. if (is_subdir(dest_dentry, m->mnt_root)) {
  76. mnt_set_mountpoint(m, dest_dentry, child);
  77. list_add_tail(&child->mnt_hash, tree_list);
  78. } else {
  79. /*
  80. * This can happen if the parent mount was bind mounted
  81. * on some subdirectory of a shared/slave mount.
  82. */
  83. list_add_tail(&child->mnt_hash, &tmp_list);
  84. }
  85. prev_dest_mnt = m;
  86. prev_src_mnt = child;
  87. }
  88. out:
  89. spin_lock(&vfsmount_lock);
  90. while (!list_empty(&tmp_list)) {
  91. child = list_entry(tmp_list.next, struct vfsmount, mnt_hash);
  92. list_del_init(&child->mnt_hash);
  93. umount_tree(child, &umount_list);
  94. }
  95. spin_unlock(&vfsmount_lock);
  96. release_mounts(&umount_list);
  97. return ret;
  98. }