selftests: fourth test for mounting detached mounts onto detached mounts
authorChristian Brauner <brauner@kernel.org>
Fri, 21 Feb 2025 13:13:12 +0000 (14:13 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 4 Mar 2025 08:29:53 +0000 (09:29 +0100)
Add a test to verify that detached mounts behave correctly.

Link: https://lore.kernel.org/r/20250221-brauner-open_tree-v1-13-dbcfcb98c676@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
tools/testing/selftests/mount_setattr/mount_setattr_test.c

index 66eed84..0ee4e5b 100644 (file)
@@ -1909,4 +1909,34 @@ TEST_F(mount_setattr, move_mount_detached_fail)
        EXPECT_EQ(close(fd_tree_subdir), 0);
 }
 
+TEST_F(mount_setattr, attach_detached_mount_then_umount_then_close)
+{
+       int fd_tree = -EBADF;
+       struct statx stx;
+
+       fd_tree = sys_open_tree(-EBADF, "/mnt",
+                               AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
+                               AT_RECURSIVE | OPEN_TREE_CLOEXEC |
+                               OPEN_TREE_CLONE);
+       ASSERT_GE(fd_tree, 0);
+
+       ASSERT_EQ(statx(fd_tree, "A", 0, 0, &stx), 0);
+       /* We copied with AT_RECURSIVE so /mnt/A must be a mountpoint. */
+       ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
+
+       /* Attach the mount to the caller's mount namespace. */
+       ASSERT_EQ(move_mount(fd_tree, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
+
+       ASSERT_EQ(statx(-EBADF, "/tmp/target1", 0, 0, &stx), 0);
+       ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
+
+       ASSERT_EQ(umount2("/tmp/target1", MNT_DETACH), 0);
+
+       /*
+        * This tests whether dissolve_on_fput() handles a NULL mount
+        * namespace correctly, i.e., that it doesn't splat.
+        */
+       EXPECT_EQ(close(fd_tree), 0);
+}
+
 TEST_HARNESS_MAIN