Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 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 | |
Ram Pai | 03e06e6 | 2005-11-07 17:19:33 -0500 | [diff] [blame] | 14 | /* return the next shared peer mount of @p */ |
| 15 | static inline struct vfsmount *next_peer(struct vfsmount *p) |
| 16 | { |
| 17 | return list_entry(p->mnt_share.next, struct vfsmount, mnt_share); |
| 18 | } |
| 19 | |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 20 | void change_mnt_propagation(struct vfsmount *mnt, int type) |
| 21 | { |
Ram Pai | 03e06e6 | 2005-11-07 17:19:33 -0500 | [diff] [blame] | 22 | if (type == MS_SHARED) { |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 23 | set_mnt_shared(mnt); |
Ram Pai | 03e06e6 | 2005-11-07 17:19:33 -0500 | [diff] [blame] | 24 | } else { |
| 25 | list_del_init(&mnt->mnt_share); |
| 26 | mnt->mnt_flags &= ~MNT_PNODE_MASK; |
| 27 | } |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 28 | } |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * get the next mount in the propagation tree. |
| 32 | * @m: the mount seen last |
| 33 | * @origin: the original mount from where the tree walk initiated |
| 34 | */ |
| 35 | static struct vfsmount *propagation_next(struct vfsmount *m, |
| 36 | struct vfsmount *origin) |
| 37 | { |
| 38 | m = next_peer(m); |
| 39 | if (m == origin) |
| 40 | return NULL; |
| 41 | return m; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * mount 'source_mnt' under the destination 'dest_mnt' at |
| 46 | * dentry 'dest_dentry'. And propagate that mount to |
| 47 | * all the peer and slave mounts of 'dest_mnt'. |
| 48 | * Link all the new mounts into a propagation tree headed at |
| 49 | * source_mnt. Also link all the new mounts using ->mnt_list |
| 50 | * headed at source_mnt's ->mnt_list |
| 51 | * |
| 52 | * @dest_mnt: destination mount. |
| 53 | * @dest_dentry: destination dentry. |
| 54 | * @source_mnt: source mount. |
| 55 | * @tree_list : list of heads of trees to be attached. |
| 56 | */ |
| 57 | int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry, |
| 58 | struct vfsmount *source_mnt, struct list_head *tree_list) |
| 59 | { |
| 60 | struct vfsmount *m, *child; |
| 61 | int ret = 0; |
| 62 | struct vfsmount *prev_dest_mnt = dest_mnt; |
| 63 | struct vfsmount *prev_src_mnt = source_mnt; |
| 64 | LIST_HEAD(tmp_list); |
| 65 | LIST_HEAD(umount_list); |
| 66 | |
| 67 | for (m = propagation_next(dest_mnt, dest_mnt); m; |
| 68 | m = propagation_next(m, dest_mnt)) { |
| 69 | int type = CL_PROPAGATION; |
| 70 | |
| 71 | if (IS_MNT_NEW(m)) |
| 72 | continue; |
| 73 | |
| 74 | if (IS_MNT_SHARED(m)) |
| 75 | type |= CL_MAKE_SHARED; |
| 76 | |
| 77 | if (!(child = copy_tree(source_mnt, source_mnt->mnt_root, |
| 78 | type))) { |
| 79 | ret = -ENOMEM; |
| 80 | list_splice(tree_list, tmp_list.prev); |
| 81 | goto out; |
| 82 | } |
| 83 | |
| 84 | if (is_subdir(dest_dentry, m->mnt_root)) { |
| 85 | mnt_set_mountpoint(m, dest_dentry, child); |
| 86 | list_add_tail(&child->mnt_hash, tree_list); |
| 87 | } else { |
| 88 | /* |
| 89 | * This can happen if the parent mount was bind mounted |
| 90 | * on some subdirectory of a shared/slave mount. |
| 91 | */ |
| 92 | list_add_tail(&child->mnt_hash, &tmp_list); |
| 93 | } |
| 94 | prev_dest_mnt = m; |
| 95 | prev_src_mnt = child; |
| 96 | } |
| 97 | out: |
| 98 | spin_lock(&vfsmount_lock); |
| 99 | while (!list_empty(&tmp_list)) { |
| 100 | child = list_entry(tmp_list.next, struct vfsmount, mnt_hash); |
| 101 | list_del_init(&child->mnt_hash); |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame^] | 102 | umount_tree(child, 0, &umount_list); |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 103 | } |
| 104 | spin_unlock(&vfsmount_lock); |
| 105 | release_mounts(&umount_list); |
| 106 | return ret; |
| 107 | } |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame^] | 108 | |
| 109 | /* |
| 110 | * return true if the refcount is greater than count |
| 111 | */ |
| 112 | static inline int do_refcount_check(struct vfsmount *mnt, int count) |
| 113 | { |
| 114 | int mycount = atomic_read(&mnt->mnt_count); |
| 115 | return (mycount > count); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * check if the mount 'mnt' can be unmounted successfully. |
| 120 | * @mnt: the mount to be checked for unmount |
| 121 | * NOTE: unmounting 'mnt' would naturally propagate to all |
| 122 | * other mounts its parent propagates to. |
| 123 | * Check if any of these mounts that **do not have submounts** |
| 124 | * have more references than 'refcnt'. If so return busy. |
| 125 | */ |
| 126 | int propagate_mount_busy(struct vfsmount *mnt, int refcnt) |
| 127 | { |
| 128 | struct vfsmount *m, *child; |
| 129 | struct vfsmount *parent = mnt->mnt_parent; |
| 130 | int ret = 0; |
| 131 | |
| 132 | if (mnt == parent) |
| 133 | return do_refcount_check(mnt, refcnt); |
| 134 | |
| 135 | /* |
| 136 | * quickly check if the current mount can be unmounted. |
| 137 | * If not, we don't have to go checking for all other |
| 138 | * mounts |
| 139 | */ |
| 140 | if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt)) |
| 141 | return 1; |
| 142 | |
| 143 | for (m = propagation_next(parent, parent); m; |
| 144 | m = propagation_next(m, parent)) { |
| 145 | child = __lookup_mnt(m, mnt->mnt_mountpoint, 0); |
| 146 | if (child && list_empty(&child->mnt_mounts) && |
| 147 | (ret = do_refcount_check(child, 1))) |
| 148 | break; |
| 149 | } |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * NOTE: unmounting 'mnt' naturally propagates to all other mounts its |
| 155 | * parent propagates to. |
| 156 | */ |
| 157 | static void __propagate_umount(struct vfsmount *mnt) |
| 158 | { |
| 159 | struct vfsmount *parent = mnt->mnt_parent; |
| 160 | struct vfsmount *m; |
| 161 | |
| 162 | BUG_ON(parent == mnt); |
| 163 | |
| 164 | for (m = propagation_next(parent, parent); m; |
| 165 | m = propagation_next(m, parent)) { |
| 166 | |
| 167 | struct vfsmount *child = __lookup_mnt(m, |
| 168 | mnt->mnt_mountpoint, 0); |
| 169 | /* |
| 170 | * umount the child only if the child has no |
| 171 | * other children |
| 172 | */ |
| 173 | if (child && list_empty(&child->mnt_mounts)) { |
| 174 | list_del(&child->mnt_hash); |
| 175 | list_add_tail(&child->mnt_hash, &mnt->mnt_hash); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * collect all mounts that receive propagation from the mount in @list, |
| 182 | * and return these additional mounts in the same list. |
| 183 | * @list: the list of mounts to be unmounted. |
| 184 | */ |
| 185 | int propagate_umount(struct list_head *list) |
| 186 | { |
| 187 | struct vfsmount *mnt; |
| 188 | |
| 189 | list_for_each_entry(mnt, list, mnt_hash) |
| 190 | __propagate_umount(mnt); |
| 191 | return 0; |
| 192 | } |