Skip to content

Commit 49acacd

Browse files
author
Al Viro
committed
clone_mnt(): simplify the propagation-related logics
The underlying rules are simple: * MNT_SHARED should be set iff ->mnt_group_id of new mount ends up non-zero. * mounts should be on the same ->mnt_share cyclic list iff they have the same non-zero ->mnt_group_id value. * CL_PRIVATE is mutually exclusive with MNT_SHARED, MNT_SLAVE, MNT_SHARED_TO_SLAVE and MNT_EXPIRE; the whole point of that thing is to get a clone of old mount that would *not* be on any namespace-related lists. The above allows to make the logics more straightforward; what's more, it makes the proof that invariants are maintained much simpler. The variant in mainline is safe (aside of a very narrow race with unsafe modification of mnt_flags right after we had the mount exposed in superblock's ->s_mounts; theoretically it can race with ro remount of the original, but it's not easy to hit), but proof of its correctness is really unpleasant. Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent d08fa7f commit 49acacd

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

fs/namespace.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,9 @@ static struct mount *clone_mnt(struct mount *old, struct dentry *root,
13011301
if (!mnt)
13021302
return ERR_PTR(-ENOMEM);
13031303

1304+
mnt->mnt.mnt_flags = READ_ONCE(old->mnt.mnt_flags) &
1305+
~MNT_INTERNAL_FLAGS;
1306+
13041307
if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
13051308
mnt->mnt_group_id = 0; /* not a peer of original */
13061309
else
@@ -1312,8 +1315,8 @@ static struct mount *clone_mnt(struct mount *old, struct dentry *root,
13121315
goto out_free;
13131316
}
13141317

1315-
mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1316-
mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL|MNT_LOCKED);
1318+
if (mnt->mnt_group_id)
1319+
set_mnt_shared(mnt);
13171320

13181321
atomic_inc(&sb->s_active);
13191322
mnt->mnt.mnt_idmap = mnt_idmap_get(mnt_idmap(&old->mnt));
@@ -1326,30 +1329,27 @@ static struct mount *clone_mnt(struct mount *old, struct dentry *root,
13261329
list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
13271330
unlock_mount_hash();
13281331

1332+
if (flag & CL_PRIVATE) // we are done with it
1333+
return mnt;
1334+
1335+
if (peers(mnt, old))
1336+
list_add(&mnt->mnt_share, &old->mnt_share);
1337+
13291338
if ((flag & CL_SLAVE) ||
13301339
((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
13311340
list_add(&mnt->mnt_slave, &old->mnt_slave_list);
13321341
mnt->mnt_master = old;
1333-
CLEAR_MNT_SHARED(mnt);
1334-
} else if (!(flag & CL_PRIVATE)) {
1335-
if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1336-
list_add(&mnt->mnt_share, &old->mnt_share);
1337-
if (IS_MNT_SLAVE(old))
1338-
list_add(&mnt->mnt_slave, &old->mnt_slave);
1342+
} else if (IS_MNT_SLAVE(old)) {
1343+
list_add(&mnt->mnt_slave, &old->mnt_slave);
13391344
mnt->mnt_master = old->mnt_master;
1340-
} else {
1341-
CLEAR_MNT_SHARED(mnt);
13421345
}
1343-
if (flag & CL_MAKE_SHARED)
1344-
set_mnt_shared(mnt);
13451346

13461347
/* stick the duplicate mount on the same expiry list
13471348
* as the original if that was on one */
13481349
if (flag & CL_EXPIRE) {
13491350
if (!list_empty(&old->mnt_expire))
13501351
list_add(&mnt->mnt_expire, &old->mnt_expire);
13511352
}
1352-
13531353
return mnt;
13541354

13551355
out_free:

0 commit comments

Comments
 (0)