Skip to content

Commit 4fd0aea

Browse files
committed
selftests: third test for mounting detached mounts onto detached mounts
Add a test to verify that detached mounts behave correctly. Link: https://lore.kernel.org/r/20250221-brauner-open_tree-v1-12-dbcfcb98c676@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 89ddfd4 commit 4fd0aea

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

tools/testing/selftests/mount_setattr/mount_setattr_test.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,4 +1878,35 @@ TEST_F(mount_setattr, mount_detached_mount_on_detached_mount_and_attach)
18781878
EXPECT_EQ(close(fd_tree_subdir), 0);
18791879
}
18801880

1881+
TEST_F(mount_setattr, move_mount_detached_fail)
1882+
{
1883+
int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
1884+
struct statx stx;
1885+
1886+
fd_tree_base = sys_open_tree(-EBADF, "/mnt",
1887+
AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
1888+
OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
1889+
ASSERT_GE(fd_tree_base, 0);
1890+
1891+
/* Attach the mount to the caller's mount namespace. */
1892+
ASSERT_EQ(move_mount(fd_tree_base, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
1893+
1894+
ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
1895+
ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
1896+
1897+
fd_tree_subdir = sys_open_tree(-EBADF, "/tmp/B",
1898+
AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
1899+
OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
1900+
ASSERT_GE(fd_tree_subdir, 0);
1901+
ASSERT_EQ(statx(fd_tree_subdir, "BB", 0, 0, &stx), 0);
1902+
ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
1903+
1904+
/* Not allowed to move an attached mount to a detached mount. */
1905+
ASSERT_NE(move_mount(fd_tree_base, "", fd_tree_subdir, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
1906+
ASSERT_EQ(errno, EINVAL);
1907+
1908+
EXPECT_EQ(close(fd_tree_base), 0);
1909+
EXPECT_EQ(close(fd_tree_subdir), 0);
1910+
}
1911+
18811912
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)