59bd9ded4d780 (Thomas Gleixner 2019-05-28 10:10:12 -0700 1) // SPDX-License-Identifier: GPL-2.0-only
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2) /*
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 3) * linux/fs/pnode.c
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 4) *
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 5) * (C) Copyright IBM Corporation 2005.
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 6) * Author : Ram Pai (linuxram@us.ibm.com)
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 7) */
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 8) #include <linux/mnt_namespace.h>
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 9) #include <linux/mount.h>
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 10) #include <linux/fs.h>
132c94e31b8bc (Eric W. Biederman 2013-03-22 04:08:05 -0700 11) #include <linux/nsproxy.h>
e262e32d6bde0 (David Howells 2018-11-01 23:07:23 +0000 12) #include <uapi/linux/mount.h>
6d59e7f582ef1 (Al Viro 2008-03-22 15:48:17 -0400 13) #include "internal.h"
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 14) #include "pnode.h"
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 15)
03e06e68ff762 (Ram Pai 2005-11-07 17:19:33 -0500 16) /* return the next shared peer mount of @p */
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 17) static inline struct mount *next_peer(struct mount *p)
03e06e68ff762 (Ram Pai 2005-11-07 17:19:33 -0500 18) {
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 19) return list_entry(p->mnt_share.next, struct mount, mnt_share);
03e06e68ff762 (Ram Pai 2005-11-07 17:19:33 -0500 20) }
03e06e68ff762 (Ram Pai 2005-11-07 17:19:33 -0500 21)
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 22) static inline struct mount *first_slave(struct mount *p)
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 23) {
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 24) return list_entry(p->mnt_slave_list.next, struct mount, mnt_slave);
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 25) }
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 26)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 27) static inline struct mount *last_slave(struct mount *p)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 28) {
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 29) return list_entry(p->mnt_slave_list.prev, struct mount, mnt_slave);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 30) }
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 31)
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 32) static inline struct mount *next_slave(struct mount *p)
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 33) {
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 34) return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 35) }
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 36)
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 37) static struct mount *get_peer_under_root(struct mount *mnt,
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 38) struct mnt_namespace *ns,
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 39) const struct path *root)
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 40) {
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 41) struct mount *m = mnt;
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 42)
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 43) do {
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 44) /* Check the namespace first for optimization */
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 45) if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 46) return m;
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 47)
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 48) m = next_peer(m);
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 49) } while (m != mnt);
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 50)
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 51) return NULL;
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 52) }
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 53)
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 54) /*
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 55) * Get ID of closest dominating peer group having a representative
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 56) * under the given root.
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 57) *
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 58) * Caller must hold namespace_sem
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 59) */
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 60) int get_dominating_id(struct mount *mnt, const struct path *root)
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 61) {
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 62) struct mount *m;
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 63)
32301920f44a9 (Al Viro 2011-11-25 00:10:28 -0500 64) for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 65) struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 66) if (d)
15169fe784a98 (Al Viro 2011-11-25 00:50:41 -0500 67) return d->mnt_group_id;
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 68) }
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 69)
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 70) return 0;
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 71) }
97e7e0f71d6d9 (Miklos Szeredi 2008-03-27 13:06:26 +0100 72)
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 73) static int do_make_slave(struct mount *mnt)
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 74) {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 75) struct mount *master, *slave_mnt;
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 76)
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 77) if (list_empty(&mnt->mnt_share)) {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 78) if (IS_MNT_SHARED(mnt)) {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 79) mnt_release_group_id(mnt);
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 80) CLEAR_MNT_SHARED(mnt);
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 81) }
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 82) master = mnt->mnt_master;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 83) if (!master) {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 84) struct list_head *p = &mnt->mnt_slave_list;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 85) while (!list_empty(p)) {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 86) slave_mnt = list_first_entry(p,
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 87) struct mount, mnt_slave);
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 88) list_del_init(&slave_mnt->mnt_slave);
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 89) slave_mnt->mnt_master = NULL;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 90) }
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 91) return 0;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 92) }
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 93) } else {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 94) struct mount *m;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 95) /*
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 96) * slave 'mnt' to a peer mount that has the
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 97) * same root dentry. If none is available then
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 98) * slave it to anything that is available.
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 99) */
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 100) for (m = master = next_peer(mnt); m != mnt; m = next_peer(m)) {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 101) if (m->mnt.mnt_root == mnt->mnt.mnt_root) {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 102) master = m;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 103) break;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 104) }
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 105) }
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 106) list_del_init(&mnt->mnt_share);
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 107) mnt->mnt_group_id = 0;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 108) CLEAR_MNT_SHARED(mnt);
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 109) }
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 110) list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 111) slave_mnt->mnt_master = master;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 112) list_move(&mnt->mnt_slave, &master->mnt_slave_list);
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 113) list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 114) INIT_LIST_HEAD(&mnt->mnt_slave_list);
32301920f44a9 (Al Viro 2011-11-25 00:10:28 -0500 115) mnt->mnt_master = master;
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 116) return 0;
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 117) }
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 118)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 119) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 120) * vfsmount lock must be held for write
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 121) */
0f0afb1dcf01a (Al Viro 2011-11-24 20:43:10 -0500 122) void change_mnt_propagation(struct mount *mnt, int type)
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 123) {
03e06e68ff762 (Ram Pai 2005-11-07 17:19:33 -0500 124) if (type == MS_SHARED) {
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 125) set_mnt_shared(mnt);
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 126) return;
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 127) }
6fc7871fed915 (Al Viro 2011-11-24 23:35:54 -0500 128) do_make_slave(mnt);
a58b0eb8e64b7 (Ram Pai 2005-11-07 17:20:48 -0500 129) if (type != MS_SLAVE) {
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 130) list_del_init(&mnt->mnt_slave);
d10e8def07fc8 (Al Viro 2011-11-25 00:07:16 -0500 131) mnt->mnt_master = NULL;
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 132) if (type == MS_UNBINDABLE)
0f0afb1dcf01a (Al Viro 2011-11-24 20:43:10 -0500 133) mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
0b03cfb25fa94 (Andries E. Brouwer 2008-02-06 01:36:32 -0800 134) else
0f0afb1dcf01a (Al Viro 2011-11-24 20:43:10 -0500 135) mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
03e06e68ff762 (Ram Pai 2005-11-07 17:19:33 -0500 136) }
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 137) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 138)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 139) /*
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 140) * get the next mount in the propagation tree.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 141) * @m: the mount seen last
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 142) * @origin: the original mount from where the tree walk initiated
796a6b521d0ea (Al Viro 2010-01-16 13:28:47 -0500 143) *
796a6b521d0ea (Al Viro 2010-01-16 13:28:47 -0500 144) * Note that peer groups form contiguous segments of slave lists.
796a6b521d0ea (Al Viro 2010-01-16 13:28:47 -0500 145) * We rely on that in get_source() to be able to find out if
796a6b521d0ea (Al Viro 2010-01-16 13:28:47 -0500 146) * vfsmount found while iterating with propagation_next() is
796a6b521d0ea (Al Viro 2010-01-16 13:28:47 -0500 147) * a peer of one we'd found earlier.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 148) */
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 149) static struct mount *propagation_next(struct mount *m,
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 150) struct mount *origin)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 151) {
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 152) /* are there any slaves of this mount? */
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 153) if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 154) return first_slave(m);
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 155)
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 156) while (1) {
32301920f44a9 (Al Viro 2011-11-25 00:10:28 -0500 157) struct mount *master = m->mnt_master;
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 158)
32301920f44a9 (Al Viro 2011-11-25 00:10:28 -0500 159) if (master == origin->mnt_master) {
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 160) struct mount *next = next_peer(m);
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 161) return (next == origin) ? NULL : next;
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 162) } else if (m->mnt_slave.next != &master->mnt_slave_list)
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 163) return next_slave(m);
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 164)
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 165) /* back at master */
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 166) m = master;
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 167) }
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 168) }
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 169)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 170) static struct mount *skip_propagation_subtree(struct mount *m,
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 171) struct mount *origin)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 172) {
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 173) /*
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 174) * Advance m such that propagation_next will not return
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 175) * the slaves of m.
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 176) */
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 177) if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 178) m = last_slave(m);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 179)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 180) return m;
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 181) }
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 182)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 183) static struct mount *next_group(struct mount *m, struct mount *origin)
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 184) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 185) while (1) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 186) while (1) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 187) struct mount *next;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 188) if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 189) return first_slave(m);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 190) next = next_peer(m);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 191) if (m->mnt_group_id == origin->mnt_group_id) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 192) if (next == origin)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 193) return NULL;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 194) } else if (m->mnt_slave.next != &next->mnt_slave)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 195) break;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 196) m = next;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 197) }
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 198) /* m is the last peer */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 199) while (1) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 200) struct mount *master = m->mnt_master;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 201) if (m->mnt_slave.next != &master->mnt_slave_list)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 202) return next_slave(m);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 203) m = next_peer(master);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 204) if (master->mnt_group_id == origin->mnt_group_id)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 205) break;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 206) if (master->mnt_slave.next == &m->mnt_slave)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 207) break;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 208) m = master;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 209) }
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 210) if (m == origin)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 211) return NULL;
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 212) }
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 213) }
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 214)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 215) /* all accesses are serialized by namespace_sem */
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 216) static struct mount *last_dest, *first_source, *last_source, *dest_master;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 217) static struct mountpoint *mp;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 218) static struct hlist_head *list;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 219)
7ae8fd0351f91 (Maxim Patlasov 2016-02-16 11:45:33 -0800 220) static inline bool peers(struct mount *m1, struct mount *m2)
7ae8fd0351f91 (Maxim Patlasov 2016-02-16 11:45:33 -0800 221) {
7ae8fd0351f91 (Maxim Patlasov 2016-02-16 11:45:33 -0800 222) return m1->mnt_group_id == m2->mnt_group_id && m1->mnt_group_id;
7ae8fd0351f91 (Maxim Patlasov 2016-02-16 11:45:33 -0800 223) }
7ae8fd0351f91 (Maxim Patlasov 2016-02-16 11:45:33 -0800 224)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 225) static int propagate_one(struct mount *m)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 226) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 227) struct mount *child;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 228) int type;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 229) /* skip ones added by this propagate_mnt() */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 230) if (IS_MNT_NEW(m))
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 231) return 0;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 232) /* skip if mountpoint isn't covered by it */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 233) if (!is_subdir(mp->m_dentry, m->mnt.mnt_root))
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 234) return 0;
7ae8fd0351f91 (Maxim Patlasov 2016-02-16 11:45:33 -0800 235) if (peers(m, last_dest)) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 236) type = CL_MAKE_SHARED;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 237) } else {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 238) struct mount *n, *p;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 239) bool done;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 240) for (n = m; ; n = p) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 241) p = n->mnt_master;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 242) if (p == dest_master || IS_MNT_MARKED(p))
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 243) break;
796a6b521d0ea (Al Viro 2010-01-16 13:28:47 -0500 244) }
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 245) do {
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 246) struct mount *parent = last_source->mnt_parent;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 247) if (last_source == first_source)
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 248) break;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 249) done = parent->mnt_master == p;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 250) if (done && peers(n, parent))
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 251) break;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 252) last_source = last_source->mnt_master;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 253) } while (!done);
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 254)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 255) type = CL_SLAVE;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 256) /* beginning of peer group among the slaves? */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 257) if (IS_MNT_SHARED(m))
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 258) type |= CL_MAKE_SHARED;
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 259) }
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 260)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 261) child = copy_tree(last_source, last_source->mnt.mnt_root, type);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 262) if (IS_ERR(child))
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 263) return PTR_ERR(child);
b0d3869ce9eea (Al Viro 2020-04-27 10:26:22 -0400 264) read_seqlock_excl(&mount_lock);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 265) mnt_set_mountpoint(m, mp, child);
b0d3869ce9eea (Al Viro 2020-04-27 10:26:22 -0400 266) if (m->mnt_master != dest_master)
b0d3869ce9eea (Al Viro 2020-04-27 10:26:22 -0400 267) SET_MNT_MARK(m->mnt_master);
b0d3869ce9eea (Al Viro 2020-04-27 10:26:22 -0400 268) read_sequnlock_excl(&mount_lock);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 269) last_dest = m;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 270) last_source = child;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 271) hlist_add_head(&child->mnt_hash, list);
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 272) return count_mounts(m->mnt_ns, child);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 273) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 274)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 275) /*
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 276) * mount 'source_mnt' under the destination 'dest_mnt' at
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 277) * dentry 'dest_dentry'. And propagate that mount to
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 278) * all the peer and slave mounts of 'dest_mnt'.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 279) * Link all the new mounts into a propagation tree headed at
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 280) * source_mnt. Also link all the new mounts using ->mnt_list
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 281) * headed at source_mnt's ->mnt_list
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 282) *
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 283) * @dest_mnt: destination mount.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 284) * @dest_dentry: destination dentry.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 285) * @source_mnt: source mount.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 286) * @tree_list : list of heads of trees to be attached.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 287) */
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 288) int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 289) struct mount *source_mnt, struct hlist_head *tree_list)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 290) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 291) struct mount *m, *n;
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 292) int ret = 0;
132c94e31b8bc (Eric W. Biederman 2013-03-22 04:08:05 -0700 293)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 294) /*
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 295) * we don't want to bother passing tons of arguments to
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 296) * propagate_one(); everything is serialized by namespace_sem,
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 297) * so globals will do just fine.
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 298) */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 299) last_dest = dest_mnt;
5ec0811d30378 (Eric W. Biederman 2016-05-05 09:29:29 -0500 300) first_source = source_mnt;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 301) last_source = source_mnt;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 302) mp = dest_mp;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 303) list = tree_list;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 304) dest_master = dest_mnt->mnt_master;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 305)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 306) /* all peers of dest_mnt, except dest_mnt itself */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 307) for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 308) ret = propagate_one(n);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 309) if (ret)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 310) goto out;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 311) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 312)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 313) /* all slave groups */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 314) for (m = next_group(dest_mnt, dest_mnt); m;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 315) m = next_group(m, dest_mnt)) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 316) /* everything in that slave group */
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 317) n = m;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 318) do {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 319) ret = propagate_one(n);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 320) if (ret)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 321) goto out;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 322) n = next_peer(n);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 323) } while (n != m);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 324) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 325) out:
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 326) read_seqlock_excl(&mount_lock);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 327) hlist_for_each_entry(n, tree_list, mnt_hash) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 328) m = n->mnt_parent;
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 329) if (m->mnt_master != dest_mnt->mnt_master)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 330) CLEAR_MNT_MARK(m->mnt_master);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 331) }
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 332) read_sequnlock_excl(&mount_lock);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 333) return ret;
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 334) }
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 335)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 336) static struct mount *find_topper(struct mount *mnt)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 337) {
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 338) /* If there is exactly one mount covering mnt completely return it. */
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 339) struct mount *child;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 340)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 341) if (!list_is_singular(&mnt->mnt_mounts))
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 342) return NULL;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 343)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 344) child = list_first_entry(&mnt->mnt_mounts, struct mount, mnt_child);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 345) if (child->mnt_mountpoint != mnt->mnt.mnt_root)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 346) return NULL;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 347)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 348) return child;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 349) }
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 350)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 351) /*
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 352) * return true if the refcount is greater than count
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 353) */
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 354) static inline int do_refcount_check(struct mount *mnt, int count)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 355) {
aba809cf0944f (Al Viro 2013-09-28 23:10:55 -0400 356) return mnt_get_count(mnt) > count;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 357) }
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 358)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 359) /*
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 360) * check if the mount 'mnt' can be unmounted successfully.
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 361) * @mnt: the mount to be checked for unmount
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 362) * NOTE: unmounting 'mnt' would naturally propagate to all
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 363) * other mounts its parent propagates to.
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 364) * Check if any of these mounts that **do not have submounts**
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 365) * have more references than 'refcnt'. If so return busy.
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 366) *
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 367) * vfsmount lock must be held for write
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 368) */
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 369) int propagate_mount_busy(struct mount *mnt, int refcnt)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 370) {
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 371) struct mount *m, *child, *topper;
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 372) struct mount *parent = mnt->mnt_parent;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 373)
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 374) if (mnt == parent)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 375) return do_refcount_check(mnt, refcnt);
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 376)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 377) /*
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 378) * quickly check if the current mount can be unmounted.
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 379) * If not, we don't have to go checking for all other
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 380) * mounts
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 381) */
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 382) if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 383) return 1;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 384)
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 385) for (m = propagation_next(parent, parent); m;
c937135d98f23 (Al Viro 2011-11-24 23:56:26 -0500 386) m = propagation_next(m, parent)) {
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 387) int count = 1;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 388) child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 389) if (!child)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 390) continue;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 391)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 392) /* Is there exactly one mount on the child that covers
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 393) * it completely whose reference should be ignored?
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 394) */
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 395) topper = find_topper(child);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 396) if (topper)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 397) count += 1;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 398) else if (!list_empty(&child->mnt_mounts))
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 399) continue;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 400)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 401) if (do_refcount_check(child, count))
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 402) return 1;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 403) }
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 404) return 0;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 405) }
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 406)
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 407) /*
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 408) * Clear MNT_LOCKED when it can be shown to be safe.
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 409) *
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 410) * mount_lock lock must be held for write
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 411) */
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 412) void propagate_mount_unlock(struct mount *mnt)
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 413) {
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 414) struct mount *parent = mnt->mnt_parent;
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 415) struct mount *m, *child;
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 416)
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 417) BUG_ON(parent == mnt);
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 418)
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 419) for (m = propagation_next(parent, parent); m;
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 420) m = propagation_next(m, parent)) {
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 421) child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 422) if (child)
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 423) child->mnt.mnt_flags &= ~MNT_LOCKED;
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 424) }
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 425) }
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 426)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 427) static void umount_one(struct mount *mnt, struct list_head *to_umount)
0c56fe31420ca (Eric W. Biederman 2015-01-05 13:38:04 -0600 428) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 429) CLEAR_MNT_MARK(mnt);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 430) mnt->mnt.mnt_flags |= MNT_UMOUNT;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 431) list_del_init(&mnt->mnt_child);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 432) list_del_init(&mnt->mnt_umounting);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 433) list_move_tail(&mnt->mnt_list, to_umount);
0c56fe31420ca (Eric W. Biederman 2015-01-05 13:38:04 -0600 434) }
0c56fe31420ca (Eric W. Biederman 2015-01-05 13:38:04 -0600 435)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 436) /*
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 437) * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 438) * parent propagates to.
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 439) */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 440) static bool __propagate_umount(struct mount *mnt,
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 441) struct list_head *to_umount,
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 442) struct list_head *to_restore)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 443) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 444) bool progress = false;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 445) struct mount *child;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 446)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 447) /*
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 448) * The state of the parent won't change if this mount is
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 449) * already unmounted or marked as without children.
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 450) */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 451) if (mnt->mnt.mnt_flags & (MNT_UMOUNT | MNT_MARKED))
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 452) goto out;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 453)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 454) /* Verify topper is the only grandchild that has not been
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 455) * speculatively unmounted.
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 456) */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 457) list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 458) if (child->mnt_mountpoint == mnt->mnt.mnt_root)
0c56fe31420ca (Eric W. Biederman 2015-01-05 13:38:04 -0600 459) continue;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 460) if (!list_empty(&child->mnt_umounting) && IS_MNT_MARKED(child))
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 461) continue;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 462) /* Found a mounted child */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 463) goto children;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 464) }
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 465)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 466) /* Mark mounts that can be unmounted if not locked */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 467) SET_MNT_MARK(mnt);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 468) progress = true;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 469)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 470) /* If a mount is without children and not locked umount it. */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 471) if (!IS_MNT_LOCKED(mnt)) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 472) umount_one(mnt, to_umount);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 473) } else {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 474) children:
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 475) list_move_tail(&mnt->mnt_umounting, to_restore);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 476) }
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 477) out:
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 478) return progress;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 479) }
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 480)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 481) static void umount_list(struct list_head *to_umount,
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 482) struct list_head *to_restore)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 483) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 484) struct mount *mnt, *child, *tmp;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 485) list_for_each_entry(mnt, to_umount, mnt_list) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 486) list_for_each_entry_safe(child, tmp, &mnt->mnt_mounts, mnt_child) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 487) /* topper? */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 488) if (child->mnt_mountpoint == mnt->mnt.mnt_root)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 489) list_move_tail(&child->mnt_umounting, to_restore);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 490) else
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 491) umount_one(child, to_umount);
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 492) }
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 493) }
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 494) }
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 495)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 496) static void restore_mounts(struct list_head *to_restore)
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 497) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 498) /* Restore mounts to a clean working state */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 499) while (!list_empty(to_restore)) {
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 500) struct mount *mnt, *parent;
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 501) struct mountpoint *mp;
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 502)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 503) mnt = list_first_entry(to_restore, struct mount, mnt_umounting);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 504) CLEAR_MNT_MARK(mnt);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 505) list_del_init(&mnt->mnt_umounting);
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 506)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 507) /* Should this mount be reparented? */
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 508) mp = mnt->mnt_mp;
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 509) parent = mnt->mnt_parent;
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 510) while (parent->mnt.mnt_flags & MNT_UMOUNT) {
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 511) mp = parent->mnt_mp;
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 512) parent = parent->mnt_parent;
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 513) }
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 514) if (parent != mnt->mnt_parent)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 515) mnt_change_mountpoint(parent, mp, mnt);
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 516) }
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 517) }
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 518)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 519) static void cleanup_umount_visitations(struct list_head *visited)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 520) {
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 521) while (!list_empty(visited)) {
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 522) struct mount *mnt =
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 523) list_first_entry(visited, struct mount, mnt_umounting);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 524) list_del_init(&mnt->mnt_umounting);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 525) }
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 526) }
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 527)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 528) /*
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 529) * collect all mounts that receive propagation from the mount in @list,
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 530) * and return these additional mounts in the same list.
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 531) * @list: the list of mounts to be unmounted.
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 532) *
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 533) * vfsmount lock must be held for write
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 534) */
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 535) int propagate_umount(struct list_head *list)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 536) {
61ef47b1e4ba9 (Al Viro 2011-11-24 18:25:28 -0500 537) struct mount *mnt;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 538) LIST_HEAD(to_restore);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 539) LIST_HEAD(to_umount);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 540) LIST_HEAD(visited);
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 541)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 542) /* Find candidates for unmounting */
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 543) list_for_each_entry_reverse(mnt, list, mnt_list) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 544) struct mount *parent = mnt->mnt_parent;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 545) struct mount *m;
0c56fe31420ca (Eric W. Biederman 2015-01-05 13:38:04 -0600 546)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 547) /*
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 548) * If this mount has already been visited it is known that it's
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 549) * entire peer group and all of their slaves in the propagation
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 550) * tree for the mountpoint has already been visited and there is
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 551) * no need to visit them again.
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 552) */
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 553) if (!list_empty(&mnt->mnt_umounting))
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 554) continue;
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 555)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 556) list_add_tail(&mnt->mnt_umounting, &visited);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 557) for (m = propagation_next(parent, parent); m;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 558) m = propagation_next(m, parent)) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 559) struct mount *child = __lookup_mnt(&m->mnt,
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 560) mnt->mnt_mountpoint);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 561) if (!child)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 562) continue;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 563)
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 564) if (!list_empty(&child->mnt_umounting)) {
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 565) /*
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 566) * If the child has already been visited it is
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 567) * know that it's entire peer group and all of
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 568) * their slaves in the propgation tree for the
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 569) * mountpoint has already been visited and there
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 570) * is no need to visit this subtree again.
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 571) */
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 572) m = skip_propagation_subtree(m, parent);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 573) continue;
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 574) } else if (child->mnt.mnt_flags & MNT_UMOUNT) {
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 575) /*
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 576) * We have come accross an partially unmounted
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 577) * mount in list that has not been visited yet.
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 578) * Remember it has been visited and continue
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 579) * about our merry way.
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 580) */
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 581) list_add_tail(&child->mnt_umounting, &visited);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 582) continue;
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 583) }
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 584)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 585) /* Check the child and parents while progress is made */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 586) while (__propagate_umount(child,
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 587) &to_umount, &to_restore)) {
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 588) /* Is the parent a umount candidate? */
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 589) child = child->mnt_parent;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 590) if (list_empty(&child->mnt_umounting))
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 591) break;
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 592) }
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 593) }
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 594) }
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 595)
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 596) umount_list(&to_umount, &to_restore);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 597) restore_mounts(&to_restore);
296990deb389c (Eric W. Biederman 2016-10-24 17:25:19 -0500 598) cleanup_umount_visitations(&visited);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 599) list_splice_tail(&to_umount, list);
570487d3faf2a (Eric W. Biederman 2017-05-15 14:42:07 -0500 600)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 601) return 0;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 602) }