Re: [PATCH v4 14/19] selftests/liveupdate: Test which tmpfs files can be preserved
From: Pratyush Yadav
Date: Fri Nov 20 2026 - 09:29:40 EST
On Wed, Nov 11 2026, David Matlack wrote:
> Add tests to liveupdate.c for the checks done when preserving a tmpfs
> file: A file can be preserved through a read-only fd, but not a
> write-only fd. Preserving it requires permission to write to it, except
> for the owner of an immutable file or of a file on a read-only
> superblock. Append-only files can't be preserved, and files can be
> preserved through a read-only mount. Also check that the size of a
> preserved file is frozen until the file is unpreserved.
>
> These checks don't need a kexec, so they run as part of the regular
> liveupdate selftest.
>
> Enable CONFIG_TMPFS_XATTR for FS_IOC_SETFLAGS. The test cases that use
> it are skipped without it.
>
> Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
> ---
> tools/testing/selftests/liveupdate/config | 1 +
> .../testing/selftests/liveupdate/liveupdate.c | 303 ++++++++++++++++++
> 2 files changed, 304 insertions(+)
>
[...]
> +/*
> + * Test Case: Frozen Size of a Preserved File
> + *
> + * While a file is preserved, its size can't change and no holes can be punched
> + * into it. Writes within the file still work. Closing the session unpreserves
> + * the file, after which its size can change again.
> + */
> +TEST_F(preserve_tmpfs, frozen_size)
Nit: This test isn't really for "frozen size", it makes sure a preserved
file behaves as we expect it to. So perhaps call it "preserved_ops" and
update the comment?
> +{
> + long page_size = getpagesize();
> + struct stat st;
> + char c;
> + int fd;
> +
> + fd = create_file("file", 0644);
> + ASSERT_GE(fd, 0);
> + ASSERT_EQ(ftruncate(fd, 2 * page_size), 0);
> + ASSERT_EQ(luo_session_preserve_fd(self->session_fd, fd, 1), 0);
> +
> + EXPECT_EQ(ftruncate(fd, page_size), -1);
> + EXPECT_EQ(errno, EPERM);
> + EXPECT_EQ(ftruncate(fd, 4 * page_size), -1);
> + EXPECT_EQ(errno, EPERM);
> + EXPECT_EQ(fallocate(fd, 0, 0, 4 * page_size), -1);
> + EXPECT_EQ(errno, EPERM);
> + EXPECT_EQ(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0,
> + page_size), -1);
> + EXPECT_EQ(errno, EPERM);
> + EXPECT_EQ(pwrite(fd, "x", 1, 2 * page_size), -1);
> + EXPECT_EQ(errno, EPERM);
Nit: Is there any point in matching the exact error code? Wouldn't
checking for a failed syscall be enough?
> +
> + /* The size didn't change, but writes within the file work. */
> + ASSERT_EQ(fstat(fd, &st), 0);
> + EXPECT_EQ(st.st_size, 2 * page_size);
> + EXPECT_EQ(pwrite(fd, "x", 1, page_size), 1);
> + EXPECT_EQ(pread(fd, &c, 1, page_size), 1);
> + EXPECT_EQ(c, 'x');
> +
> + ASSERT_EQ(close(self->session_fd), 0);
> + self->session_fd = -1;
> + EXPECT_EQ(ftruncate(fd, page_size), 0);
> +
> + close(fd);
> +}
> +
> TEST_HARNESS_MAIN
--
Regards,
Pratyush Yadav