selftests: add test for detached mount tree propagation
authorChristian Brauner <brauner@kernel.org>
Tue, 25 Feb 2025 10:15:47 +0000 (11:15 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 4 Mar 2025 08:29:55 +0000 (09:29 +0100)
Test that detached mount trees receive propagation events.

Link: https://lore.kernel.org/r/20250225-work-mount-propagation-v1-2-e6e3724500eb@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
tools/testing/selftests/mount_setattr/mount_setattr_test.c

index 1e0508cb5c2d64179718f150d37c48440831cf26..33fa1f53fdf525a66d69f67e2ed6a5090c478a5d 100644 (file)
@@ -2097,4 +2097,74 @@ TEST_F(mount_setattr, two_detached_subtrees_of_same_anonymous_mount_namespace)
        ASSERT_EQ(move_mount(fd_tree1, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
 }
 
+TEST_F(mount_setattr, detached_tree_propagation)
+{
+       int fd_tree = -EBADF;
+       struct statx stx1, stx2, stx3, stx4;
+
+       ASSERT_EQ(unshare(CLONE_NEWNS), 0);
+       ASSERT_EQ(mount(NULL, "/mnt", NULL, MS_REC | MS_SHARED, NULL), 0);
+
+       /*
+        * Copy the following mount tree:
+        *
+         * /mnt                   testing tmpfs
+         * |-/mnt/A               testing tmpfs
+         * | `-/mnt/A/AA          testing tmpfs
+         * |   `-/mnt/A/AA/B      testing tmpfs
+         * |     `-/mnt/A/AA/B/BB testing tmpfs
+         * `-/mnt/B               testing ramfs
+        */
+       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(-EBADF, "/mnt/A", 0, 0, &stx1), 0);
+       ASSERT_EQ(statx(fd_tree, "A", 0, 0, &stx2), 0);
+
+       /*
+        * Copying the mount namespace like done above doesn't alter the
+        * mounts in any way so the filesystem mounted on /mnt must be
+        * identical even though the mounts will differ. Use the device
+        * information to verify that. Note that tmpfs will have a 0
+        * major number so comparing the major number is misleading.
+        */
+       ASSERT_EQ(stx1.stx_dev_minor, stx2.stx_dev_minor);
+
+       /* Mount a tmpfs filesystem over /mnt/A. */
+       ASSERT_EQ(mount(NULL, "/mnt/A", "tmpfs", 0, NULL), 0);
+
+
+       ASSERT_EQ(statx(-EBADF, "/mnt/A", 0, 0, &stx3), 0);
+       ASSERT_EQ(statx(fd_tree, "A", 0, 0, &stx4), 0);
+
+       /*
+        * A new filesystem has been mounted on top of /mnt/A which
+        * means that the device information will be different for any
+        * statx() that was taken from /mnt/A before the mount compared
+        * to one after the mount.
+        *
+        * Since we already now that the device information between the
+        * stx1 and stx2 samples are identical we also now that stx2 and
+        * stx3 device information will necessarily differ.
+        */
+       ASSERT_NE(stx1.stx_dev_minor, stx3.stx_dev_minor);
+
+       /*
+        * If mount propagation worked correctly then the tmpfs mount
+        * that was created after the mount namespace was unshared will
+        * have propagated onto /mnt/A in the detached mount tree.
+        *
+        * Verify that the device information for stx3 and stx4 are
+        * identical. It is already established that stx3 is different
+        * from both stx1 and stx2 sampled before the tmpfs mount was
+        * done so if stx3 and stx4 are identical the proof is done.
+        */
+       ASSERT_EQ(stx3.stx_dev_minor, stx4.stx_dev_minor);
+
+       EXPECT_EQ(close(fd_tree), 0);
+}
+
 TEST_HARNESS_MAIN