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