Commit | Line | Data |
---|---|---|
07b20889 RP |
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 | ||
03e06e68 RP |
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 | ||
a58b0eb8 RP |
20 | static int do_make_slave(struct vfsmount *mnt) |
21 | { | |
22 | struct vfsmount *peer_mnt = mnt, *master = mnt->mnt_master; | |
23 | struct vfsmount *slave_mnt; | |
24 | ||
25 | /* | |
26 | * slave 'mnt' to a peer mount that has the | |
27 | * same root dentry. If none is available than | |
28 | * slave it to anything that is available. | |
29 | */ | |
30 | while ((peer_mnt = next_peer(peer_mnt)) != mnt && | |
31 | peer_mnt->mnt_root != mnt->mnt_root) ; | |
32 | ||
33 | if (peer_mnt == mnt) { | |
34 | peer_mnt = next_peer(mnt); | |
35 | if (peer_mnt == mnt) | |
36 | peer_mnt = NULL; | |
37 | } | |
38 | list_del_init(&mnt->mnt_share); | |
39 | ||
40 | if (peer_mnt) | |
41 | master = peer_mnt; | |
42 | ||
43 | if (master) { | |
44 | list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave) | |
45 | slave_mnt->mnt_master = master; | |
46 | list_del(&mnt->mnt_slave); | |
47 | list_add(&mnt->mnt_slave, &master->mnt_slave_list); | |
48 | list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev); | |
49 | INIT_LIST_HEAD(&mnt->mnt_slave_list); | |
50 | } else { | |
51 | struct list_head *p = &mnt->mnt_slave_list; | |
52 | while (!list_empty(p)) { | |
53 | slave_mnt = list_entry(p->next, | |
54 | struct vfsmount, mnt_slave); | |
55 | list_del_init(&slave_mnt->mnt_slave); | |
56 | slave_mnt->mnt_master = NULL; | |
57 | } | |
58 | } | |
59 | mnt->mnt_master = master; | |
60 | CLEAR_MNT_SHARED(mnt); | |
61 | INIT_LIST_HEAD(&mnt->mnt_slave_list); | |
62 | return 0; | |
63 | } | |
64 | ||
07b20889 RP |
65 | void change_mnt_propagation(struct vfsmount *mnt, int type) |
66 | { | |
03e06e68 | 67 | if (type == MS_SHARED) { |
b90fa9ae | 68 | set_mnt_shared(mnt); |
a58b0eb8 RP |
69 | return; |
70 | } | |
71 | do_make_slave(mnt); | |
72 | if (type != MS_SLAVE) { | |
73 | list_del_init(&mnt->mnt_slave); | |
74 | mnt->mnt_master = NULL; | |
03e06e68 | 75 | } |
07b20889 | 76 | } |
b90fa9ae RP |
77 | |
78 | /* | |
79 | * get the next mount in the propagation tree. | |
80 | * @m: the mount seen last | |
81 | * @origin: the original mount from where the tree walk initiated | |
82 | */ | |
83 | static struct vfsmount *propagation_next(struct vfsmount *m, | |
84 | struct vfsmount *origin) | |
85 | { | |
86 | m = next_peer(m); | |
87 | if (m == origin) | |
88 | return NULL; | |
89 | return m; | |
90 | } | |
91 | ||
92 | /* | |
93 | * mount 'source_mnt' under the destination 'dest_mnt' at | |
94 | * dentry 'dest_dentry'. And propagate that mount to | |
95 | * all the peer and slave mounts of 'dest_mnt'. | |
96 | * Link all the new mounts into a propagation tree headed at | |
97 | * source_mnt. Also link all the new mounts using ->mnt_list | |
98 | * headed at source_mnt's ->mnt_list | |
99 | * | |
100 | * @dest_mnt: destination mount. | |
101 | * @dest_dentry: destination dentry. | |
102 | * @source_mnt: source mount. | |
103 | * @tree_list : list of heads of trees to be attached. | |
104 | */ | |
105 | int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry, | |
106 | struct vfsmount *source_mnt, struct list_head *tree_list) | |
107 | { | |
108 | struct vfsmount *m, *child; | |
109 | int ret = 0; | |
110 | struct vfsmount *prev_dest_mnt = dest_mnt; | |
111 | struct vfsmount *prev_src_mnt = source_mnt; | |
112 | LIST_HEAD(tmp_list); | |
113 | LIST_HEAD(umount_list); | |
114 | ||
115 | for (m = propagation_next(dest_mnt, dest_mnt); m; | |
116 | m = propagation_next(m, dest_mnt)) { | |
117 | int type = CL_PROPAGATION; | |
118 | ||
119 | if (IS_MNT_NEW(m)) | |
120 | continue; | |
121 | ||
122 | if (IS_MNT_SHARED(m)) | |
123 | type |= CL_MAKE_SHARED; | |
124 | ||
125 | if (!(child = copy_tree(source_mnt, source_mnt->mnt_root, | |
126 | type))) { | |
127 | ret = -ENOMEM; | |
128 | list_splice(tree_list, tmp_list.prev); | |
129 | goto out; | |
130 | } | |
131 | ||
132 | if (is_subdir(dest_dentry, m->mnt_root)) { | |
133 | mnt_set_mountpoint(m, dest_dentry, child); | |
134 | list_add_tail(&child->mnt_hash, tree_list); | |
135 | } else { | |
136 | /* | |
137 | * This can happen if the parent mount was bind mounted | |
138 | * on some subdirectory of a shared/slave mount. | |
139 | */ | |
140 | list_add_tail(&child->mnt_hash, &tmp_list); | |
141 | } | |
142 | prev_dest_mnt = m; | |
143 | prev_src_mnt = child; | |
144 | } | |
145 | out: | |
146 | spin_lock(&vfsmount_lock); | |
147 | while (!list_empty(&tmp_list)) { | |
148 | child = list_entry(tmp_list.next, struct vfsmount, mnt_hash); | |
149 | list_del_init(&child->mnt_hash); | |
a05964f3 | 150 | umount_tree(child, 0, &umount_list); |
b90fa9ae RP |
151 | } |
152 | spin_unlock(&vfsmount_lock); | |
153 | release_mounts(&umount_list); | |
154 | return ret; | |
155 | } | |
a05964f3 RP |
156 | |
157 | /* | |
158 | * return true if the refcount is greater than count | |
159 | */ | |
160 | static inline int do_refcount_check(struct vfsmount *mnt, int count) | |
161 | { | |
162 | int mycount = atomic_read(&mnt->mnt_count); | |
163 | return (mycount > count); | |
164 | } | |
165 | ||
166 | /* | |
167 | * check if the mount 'mnt' can be unmounted successfully. | |
168 | * @mnt: the mount to be checked for unmount | |
169 | * NOTE: unmounting 'mnt' would naturally propagate to all | |
170 | * other mounts its parent propagates to. | |
171 | * Check if any of these mounts that **do not have submounts** | |
172 | * have more references than 'refcnt'. If so return busy. | |
173 | */ | |
174 | int propagate_mount_busy(struct vfsmount *mnt, int refcnt) | |
175 | { | |
176 | struct vfsmount *m, *child; | |
177 | struct vfsmount *parent = mnt->mnt_parent; | |
178 | int ret = 0; | |
179 | ||
180 | if (mnt == parent) | |
181 | return do_refcount_check(mnt, refcnt); | |
182 | ||
183 | /* | |
184 | * quickly check if the current mount can be unmounted. | |
185 | * If not, we don't have to go checking for all other | |
186 | * mounts | |
187 | */ | |
188 | if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt)) | |
189 | return 1; | |
190 | ||
191 | for (m = propagation_next(parent, parent); m; | |
192 | m = propagation_next(m, parent)) { | |
193 | child = __lookup_mnt(m, mnt->mnt_mountpoint, 0); | |
194 | if (child && list_empty(&child->mnt_mounts) && | |
195 | (ret = do_refcount_check(child, 1))) | |
196 | break; | |
197 | } | |
198 | return ret; | |
199 | } | |
200 | ||
201 | /* | |
202 | * NOTE: unmounting 'mnt' naturally propagates to all other mounts its | |
203 | * parent propagates to. | |
204 | */ | |
205 | static void __propagate_umount(struct vfsmount *mnt) | |
206 | { | |
207 | struct vfsmount *parent = mnt->mnt_parent; | |
208 | struct vfsmount *m; | |
209 | ||
210 | BUG_ON(parent == mnt); | |
211 | ||
212 | for (m = propagation_next(parent, parent); m; | |
213 | m = propagation_next(m, parent)) { | |
214 | ||
215 | struct vfsmount *child = __lookup_mnt(m, | |
216 | mnt->mnt_mountpoint, 0); | |
217 | /* | |
218 | * umount the child only if the child has no | |
219 | * other children | |
220 | */ | |
221 | if (child && list_empty(&child->mnt_mounts)) { | |
222 | list_del(&child->mnt_hash); | |
223 | list_add_tail(&child->mnt_hash, &mnt->mnt_hash); | |
224 | } | |
225 | } | |
226 | } | |
227 | ||
228 | /* | |
229 | * collect all mounts that receive propagation from the mount in @list, | |
230 | * and return these additional mounts in the same list. | |
231 | * @list: the list of mounts to be unmounted. | |
232 | */ | |
233 | int propagate_umount(struct list_head *list) | |
234 | { | |
235 | struct vfsmount *mnt; | |
236 | ||
237 | list_for_each_entry(mnt, list, mnt_hash) | |
238 | __propagate_umount(mnt); | |
239 | return 0; | |
240 | } |