]>
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 | */ | |
6b3286ed | 9 | #include <linux/mnt_namespace.h> |
07b20889 RP |
10 | #include <linux/mount.h> |
11 | #include <linux/fs.h> | |
132c94e3 | 12 | #include <linux/nsproxy.h> |
6d59e7f5 | 13 | #include "internal.h" |
07b20889 RP |
14 | #include "pnode.h" |
15 | ||
03e06e68 | 16 | /* return the next shared peer mount of @p */ |
c937135d | 17 | static inline struct mount *next_peer(struct mount *p) |
03e06e68 | 18 | { |
6776db3d | 19 | return list_entry(p->mnt_share.next, struct mount, mnt_share); |
03e06e68 RP |
20 | } |
21 | ||
c937135d | 22 | static inline struct mount *first_slave(struct mount *p) |
5afe0022 | 23 | { |
6776db3d | 24 | return list_entry(p->mnt_slave_list.next, struct mount, mnt_slave); |
5afe0022 RP |
25 | } |
26 | ||
c937135d | 27 | static inline struct mount *next_slave(struct mount *p) |
5afe0022 | 28 | { |
6776db3d | 29 | return list_entry(p->mnt_slave.next, struct mount, mnt_slave); |
5afe0022 RP |
30 | } |
31 | ||
6fc7871f AV |
32 | static struct mount *get_peer_under_root(struct mount *mnt, |
33 | struct mnt_namespace *ns, | |
34 | const struct path *root) | |
97e7e0f7 | 35 | { |
6fc7871f | 36 | struct mount *m = mnt; |
97e7e0f7 MS |
37 | |
38 | do { | |
39 | /* Check the namespace first for optimization */ | |
143c8c91 | 40 | if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root)) |
6fc7871f | 41 | return m; |
97e7e0f7 | 42 | |
c937135d | 43 | m = next_peer(m); |
6fc7871f | 44 | } while (m != mnt); |
97e7e0f7 MS |
45 | |
46 | return NULL; | |
47 | } | |
48 | ||
49 | /* | |
50 | * Get ID of closest dominating peer group having a representative | |
51 | * under the given root. | |
52 | * | |
53 | * Caller must hold namespace_sem | |
54 | */ | |
6fc7871f | 55 | int get_dominating_id(struct mount *mnt, const struct path *root) |
97e7e0f7 | 56 | { |
6fc7871f | 57 | struct mount *m; |
97e7e0f7 | 58 | |
32301920 | 59 | for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) { |
143c8c91 | 60 | struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root); |
97e7e0f7 | 61 | if (d) |
15169fe7 | 62 | return d->mnt_group_id; |
97e7e0f7 MS |
63 | } |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
6fc7871f | 68 | static int do_make_slave(struct mount *mnt) |
a58b0eb8 | 69 | { |
32301920 | 70 | struct mount *peer_mnt = mnt, *master = mnt->mnt_master; |
d10e8def | 71 | struct mount *slave_mnt; |
a58b0eb8 RP |
72 | |
73 | /* | |
74 | * slave 'mnt' to a peer mount that has the | |
796a6b52 | 75 | * same root dentry. If none is available then |
a58b0eb8 RP |
76 | * slave it to anything that is available. |
77 | */ | |
c937135d | 78 | while ((peer_mnt = next_peer(peer_mnt)) != mnt && |
6fc7871f | 79 | peer_mnt->mnt.mnt_root != mnt->mnt.mnt_root) ; |
a58b0eb8 RP |
80 | |
81 | if (peer_mnt == mnt) { | |
c937135d | 82 | peer_mnt = next_peer(mnt); |
a58b0eb8 RP |
83 | if (peer_mnt == mnt) |
84 | peer_mnt = NULL; | |
85 | } | |
5d477b60 TI |
86 | if (mnt->mnt_group_id && IS_MNT_SHARED(mnt) && |
87 | list_empty(&mnt->mnt_share)) | |
6fc7871f | 88 | mnt_release_group_id(mnt); |
719f5d7f | 89 | |
6776db3d | 90 | list_del_init(&mnt->mnt_share); |
15169fe7 | 91 | mnt->mnt_group_id = 0; |
a58b0eb8 RP |
92 | |
93 | if (peer_mnt) | |
94 | master = peer_mnt; | |
95 | ||
96 | if (master) { | |
6776db3d | 97 | list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave) |
32301920 | 98 | slave_mnt->mnt_master = master; |
6776db3d AV |
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); | |
a58b0eb8 | 102 | } else { |
6776db3d | 103 | struct list_head *p = &mnt->mnt_slave_list; |
a58b0eb8 | 104 | while (!list_empty(p)) { |
b5e61818 | 105 | slave_mnt = list_first_entry(p, |
6776db3d AV |
106 | struct mount, mnt_slave); |
107 | list_del_init(&slave_mnt->mnt_slave); | |
a58b0eb8 RP |
108 | slave_mnt->mnt_master = NULL; |
109 | } | |
110 | } | |
32301920 | 111 | mnt->mnt_master = master; |
fc7be130 | 112 | CLEAR_MNT_SHARED(mnt); |
a58b0eb8 RP |
113 | return 0; |
114 | } | |
115 | ||
99b7db7b NP |
116 | /* |
117 | * vfsmount lock must be held for write | |
118 | */ | |
0f0afb1d | 119 | void change_mnt_propagation(struct mount *mnt, int type) |
07b20889 | 120 | { |
03e06e68 | 121 | if (type == MS_SHARED) { |
b90fa9ae | 122 | set_mnt_shared(mnt); |
a58b0eb8 RP |
123 | return; |
124 | } | |
6fc7871f | 125 | do_make_slave(mnt); |
a58b0eb8 | 126 | if (type != MS_SLAVE) { |
6776db3d | 127 | list_del_init(&mnt->mnt_slave); |
d10e8def | 128 | mnt->mnt_master = NULL; |
9676f0c6 | 129 | if (type == MS_UNBINDABLE) |
0f0afb1d | 130 | mnt->mnt.mnt_flags |= MNT_UNBINDABLE; |
0b03cfb2 | 131 | else |
0f0afb1d | 132 | mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE; |
03e06e68 | 133 | } |
07b20889 | 134 | } |
b90fa9ae RP |
135 | |
136 | /* | |
137 | * get the next mount in the propagation tree. | |
138 | * @m: the mount seen last | |
139 | * @origin: the original mount from where the tree walk initiated | |
796a6b52 AV |
140 | * |
141 | * Note that peer groups form contiguous segments of slave lists. | |
142 | * We rely on that in get_source() to be able to find out if | |
143 | * vfsmount found while iterating with propagation_next() is | |
144 | * a peer of one we'd found earlier. | |
b90fa9ae | 145 | */ |
c937135d AV |
146 | static struct mount *propagation_next(struct mount *m, |
147 | struct mount *origin) | |
b90fa9ae | 148 | { |
5afe0022 | 149 | /* are there any slaves of this mount? */ |
143c8c91 | 150 | if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list)) |
5afe0022 RP |
151 | return first_slave(m); |
152 | ||
153 | while (1) { | |
32301920 | 154 | struct mount *master = m->mnt_master; |
5afe0022 | 155 | |
32301920 | 156 | if (master == origin->mnt_master) { |
c937135d AV |
157 | struct mount *next = next_peer(m); |
158 | return (next == origin) ? NULL : next; | |
6776db3d | 159 | } else if (m->mnt_slave.next != &master->mnt_slave_list) |
5afe0022 RP |
160 | return next_slave(m); |
161 | ||
162 | /* back at master */ | |
163 | m = master; | |
164 | } | |
165 | } | |
166 | ||
f2ebb3a9 | 167 | static struct mount *next_group(struct mount *m, struct mount *origin) |
5afe0022 | 168 | { |
f2ebb3a9 AV |
169 | while (1) { |
170 | while (1) { | |
171 | struct mount *next; | |
172 | if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list)) | |
173 | return first_slave(m); | |
174 | next = next_peer(m); | |
175 | if (m->mnt_group_id == origin->mnt_group_id) { | |
176 | if (next == origin) | |
177 | return NULL; | |
178 | } else if (m->mnt_slave.next != &next->mnt_slave) | |
179 | break; | |
180 | m = next; | |
181 | } | |
182 | /* m is the last peer */ | |
183 | while (1) { | |
184 | struct mount *master = m->mnt_master; | |
185 | if (m->mnt_slave.next != &master->mnt_slave_list) | |
186 | return next_slave(m); | |
187 | m = next_peer(master); | |
188 | if (master->mnt_group_id == origin->mnt_group_id) | |
189 | break; | |
190 | if (master->mnt_slave.next == &m->mnt_slave) | |
191 | break; | |
192 | m = master; | |
193 | } | |
194 | if (m == origin) | |
195 | return NULL; | |
5afe0022 | 196 | } |
f2ebb3a9 | 197 | } |
5afe0022 | 198 | |
f2ebb3a9 AV |
199 | /* all accesses are serialized by namespace_sem */ |
200 | static struct user_namespace *user_ns; | |
201 | static struct mount *last_dest, *last_source, *dest_master; | |
202 | static struct mountpoint *mp; | |
203 | static struct hlist_head *list; | |
204 | ||
205 | static int propagate_one(struct mount *m) | |
206 | { | |
207 | struct mount *child; | |
208 | int type; | |
209 | /* skip ones added by this propagate_mnt() */ | |
210 | if (IS_MNT_NEW(m)) | |
211 | return 0; | |
212 | /* skip if mountpoint isn't covered by it */ | |
213 | if (!is_subdir(mp->m_dentry, m->mnt.mnt_root)) | |
214 | return 0; | |
215 | if (m->mnt_group_id == last_dest->mnt_group_id) { | |
216 | type = CL_MAKE_SHARED; | |
217 | } else { | |
218 | struct mount *n, *p; | |
219 | for (n = m; ; n = p) { | |
220 | p = n->mnt_master; | |
221 | if (p == dest_master || IS_MNT_MARKED(p)) { | |
222 | while (last_dest->mnt_master != p) { | |
223 | last_source = last_source->mnt_master; | |
224 | last_dest = last_source->mnt_parent; | |
225 | } | |
226 | if (n->mnt_group_id != last_dest->mnt_group_id) { | |
227 | last_source = last_source->mnt_master; | |
228 | last_dest = last_source->mnt_parent; | |
229 | } | |
230 | break; | |
231 | } | |
796a6b52 | 232 | } |
f2ebb3a9 AV |
233 | type = CL_SLAVE; |
234 | /* beginning of peer group among the slaves? */ | |
235 | if (IS_MNT_SHARED(m)) | |
236 | type |= CL_MAKE_SHARED; | |
5afe0022 | 237 | } |
f2ebb3a9 AV |
238 | |
239 | /* Notice when we are propagating across user namespaces */ | |
240 | if (m->mnt_ns->user_ns != user_ns) | |
241 | type |= CL_UNPRIVILEGED; | |
242 | child = copy_tree(last_source, last_source->mnt.mnt_root, type); | |
243 | if (IS_ERR(child)) | |
244 | return PTR_ERR(child); | |
8486a788 | 245 | child->mnt.mnt_flags &= ~MNT_LOCKED; |
f2ebb3a9 AV |
246 | mnt_set_mountpoint(m, mp, child); |
247 | last_dest = m; | |
248 | last_source = child; | |
249 | if (m->mnt_master != dest_master) { | |
250 | read_seqlock_excl(&mount_lock); | |
251 | SET_MNT_MARK(m->mnt_master); | |
252 | read_sequnlock_excl(&mount_lock); | |
253 | } | |
254 | hlist_add_head(&child->mnt_hash, list); | |
255 | return 0; | |
b90fa9ae RP |
256 | } |
257 | ||
258 | /* | |
259 | * mount 'source_mnt' under the destination 'dest_mnt' at | |
260 | * dentry 'dest_dentry'. And propagate that mount to | |
261 | * all the peer and slave mounts of 'dest_mnt'. | |
262 | * Link all the new mounts into a propagation tree headed at | |
263 | * source_mnt. Also link all the new mounts using ->mnt_list | |
264 | * headed at source_mnt's ->mnt_list | |
265 | * | |
266 | * @dest_mnt: destination mount. | |
267 | * @dest_dentry: destination dentry. | |
268 | * @source_mnt: source mount. | |
269 | * @tree_list : list of heads of trees to be attached. | |
270 | */ | |
84d17192 | 271 | int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp, |
38129a13 | 272 | struct mount *source_mnt, struct hlist_head *tree_list) |
b90fa9ae | 273 | { |
f2ebb3a9 | 274 | struct mount *m, *n; |
b90fa9ae | 275 | int ret = 0; |
132c94e3 | 276 | |
f2ebb3a9 AV |
277 | /* |
278 | * we don't want to bother passing tons of arguments to | |
279 | * propagate_one(); everything is serialized by namespace_sem, | |
280 | * so globals will do just fine. | |
281 | */ | |
282 | user_ns = current->nsproxy->mnt_ns->user_ns; | |
283 | last_dest = dest_mnt; | |
284 | last_source = source_mnt; | |
285 | mp = dest_mp; | |
286 | list = tree_list; | |
287 | dest_master = dest_mnt->mnt_master; | |
288 | ||
289 | /* all peers of dest_mnt, except dest_mnt itself */ | |
290 | for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) { | |
291 | ret = propagate_one(n); | |
292 | if (ret) | |
b90fa9ae | 293 | goto out; |
f2ebb3a9 | 294 | } |
b90fa9ae | 295 | |
f2ebb3a9 AV |
296 | /* all slave groups */ |
297 | for (m = next_group(dest_mnt, dest_mnt); m; | |
298 | m = next_group(m, dest_mnt)) { | |
299 | /* everything in that slave group */ | |
300 | n = m; | |
301 | do { | |
302 | ret = propagate_one(n); | |
303 | if (ret) | |
304 | goto out; | |
305 | n = next_peer(n); | |
306 | } while (n != m); | |
b90fa9ae RP |
307 | } |
308 | out: | |
f2ebb3a9 AV |
309 | read_seqlock_excl(&mount_lock); |
310 | hlist_for_each_entry(n, tree_list, mnt_hash) { | |
311 | m = n->mnt_parent; | |
312 | if (m->mnt_master != dest_mnt->mnt_master) | |
313 | CLEAR_MNT_MARK(m->mnt_master); | |
b90fa9ae | 314 | } |
f2ebb3a9 | 315 | read_sequnlock_excl(&mount_lock); |
b90fa9ae RP |
316 | return ret; |
317 | } | |
a05964f3 RP |
318 | |
319 | /* | |
320 | * return true if the refcount is greater than count | |
321 | */ | |
1ab59738 | 322 | static inline int do_refcount_check(struct mount *mnt, int count) |
a05964f3 | 323 | { |
aba809cf | 324 | return mnt_get_count(mnt) > count; |
a05964f3 RP |
325 | } |
326 | ||
327 | /* | |
328 | * check if the mount 'mnt' can be unmounted successfully. | |
329 | * @mnt: the mount to be checked for unmount | |
330 | * NOTE: unmounting 'mnt' would naturally propagate to all | |
331 | * other mounts its parent propagates to. | |
332 | * Check if any of these mounts that **do not have submounts** | |
333 | * have more references than 'refcnt'. If so return busy. | |
99b7db7b | 334 | * |
b3e19d92 | 335 | * vfsmount lock must be held for write |
a05964f3 | 336 | */ |
1ab59738 | 337 | int propagate_mount_busy(struct mount *mnt, int refcnt) |
a05964f3 | 338 | { |
c937135d | 339 | struct mount *m, *child; |
0714a533 | 340 | struct mount *parent = mnt->mnt_parent; |
a05964f3 RP |
341 | int ret = 0; |
342 | ||
0714a533 | 343 | if (mnt == parent) |
a05964f3 RP |
344 | return do_refcount_check(mnt, refcnt); |
345 | ||
346 | /* | |
347 | * quickly check if the current mount can be unmounted. | |
348 | * If not, we don't have to go checking for all other | |
349 | * mounts | |
350 | */ | |
6b41d536 | 351 | if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt)) |
a05964f3 RP |
352 | return 1; |
353 | ||
c937135d AV |
354 | for (m = propagation_next(parent, parent); m; |
355 | m = propagation_next(m, parent)) { | |
474279dc | 356 | child = __lookup_mnt_last(&m->mnt, mnt->mnt_mountpoint); |
6b41d536 | 357 | if (child && list_empty(&child->mnt_mounts) && |
1ab59738 | 358 | (ret = do_refcount_check(child, 1))) |
a05964f3 RP |
359 | break; |
360 | } | |
361 | return ret; | |
362 | } | |
363 | ||
364 | /* | |
365 | * NOTE: unmounting 'mnt' naturally propagates to all other mounts its | |
366 | * parent propagates to. | |
367 | */ | |
61ef47b1 | 368 | static void __propagate_umount(struct mount *mnt) |
a05964f3 | 369 | { |
0714a533 | 370 | struct mount *parent = mnt->mnt_parent; |
c937135d | 371 | struct mount *m; |
a05964f3 | 372 | |
0714a533 | 373 | BUG_ON(parent == mnt); |
a05964f3 | 374 | |
c937135d AV |
375 | for (m = propagation_next(parent, parent); m; |
376 | m = propagation_next(m, parent)) { | |
a05964f3 | 377 | |
474279dc AV |
378 | struct mount *child = __lookup_mnt_last(&m->mnt, |
379 | mnt->mnt_mountpoint); | |
a05964f3 RP |
380 | /* |
381 | * umount the child only if the child has no | |
382 | * other children | |
383 | */ | |
38129a13 | 384 | if (child && list_empty(&child->mnt_mounts)) { |
88b368f2 | 385 | list_del_init(&child->mnt_child); |
38129a13 AV |
386 | hlist_del_init_rcu(&child->mnt_hash); |
387 | hlist_add_before_rcu(&child->mnt_hash, &mnt->mnt_hash); | |
388 | } | |
a05964f3 RP |
389 | } |
390 | } | |
391 | ||
392 | /* | |
393 | * collect all mounts that receive propagation from the mount in @list, | |
394 | * and return these additional mounts in the same list. | |
395 | * @list: the list of mounts to be unmounted. | |
99b7db7b NP |
396 | * |
397 | * vfsmount lock must be held for write | |
a05964f3 | 398 | */ |
38129a13 | 399 | int propagate_umount(struct hlist_head *list) |
a05964f3 | 400 | { |
61ef47b1 | 401 | struct mount *mnt; |
a05964f3 | 402 | |
38129a13 | 403 | hlist_for_each_entry(mnt, list, mnt_hash) |
a05964f3 RP |
404 | __propagate_umount(mnt); |
405 | return 0; | |
406 | } |