From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796029063; b=ZLtcQDZGdd9dYIKZKiurjCq+UzJ/rWI6MeqDgf48OsU0Cd/+it0cyMms7/0l82RUTx6F ORUzGyew0db/vx1Oq/R2dFba9bjJhAPVzjDz1KJGGxdtOPMxHS5qP2RtUccbQkMUJQICb Vv55emSECWbOhDdlIhJCyWxCcEG+eLR6zd1Az//e7QiOtWlZ2kxj4HvbBnMcg4fA+j3pC +EFXvfiJoL1MrBF8B2nIY+doCgT3QNtUTOEFA89Mb7BveWHm9GpRmoHxmFNAoElX0lMKN SDOu4f6OYPzTFNSUIvprJHAYRNOoC0OAEcBo7dKklEb0BFAkOsXlkeevGED4FfPPJxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796029063; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=M9dPprCyZnd+wFzEVwPRsEh9lxGHwCH7osPQrLi4w2U=; b=wQdb/diZ/K13/tMYBxPkXIldRi+j32oJ+vK++GbX9FWuXBAAcmVeKYW3Joh5BfUgjNjv 4MCBwxscs4t1k7bHpu55jULGUGDmlZeuwHQIyS8FgMXSHQ8WgyAwDMaLTnf9mWi/5YJvi k+SZJ8M3uWKBuxC592Y+eHbto/6rJWd46iaQZhWi8fKuOF1kbkM238VlERp3TvXoN98f+ 8G8eAzluKzvahySlEirqb36TeX93np3OTKT5EyDVqXis9f3Gs/5eInelELQGtsboqGzmk ey1gUzu3CmcvN0P1iJvsgyNz7YlTS12ZmXEnwtatu4LzdAVeVFWaDONWRLixVhqhhmQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nVOlJvt9; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 452B1606A33A; Mon, 30 Nov 2026 08:57:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nVOlJvt9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D4381F0089B; Mon, 30 Nov 2026 08:57:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796029062; bh=M9dPprCyZnd+wFzEVwPRsEh9lxGHwCH7osPQrLi4w2U=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=nVOlJvt9kp7nfMBc28CwgOggfPr//BMujyTwpNMN4esdSPDf1XzrwyY61uB14ldx5 WxXdtDWUbh2QJR8BEMyvyor9vA9BFy3cp/ITBdruoL/2C8wSHAqqfevkW1h6s5o7Gw awUIdnRaz94MESYwabpf2NfrcnEEXdSB9lyxt2yZu6oZN6U8Xoqo4IaavWc+aZ0+bh DoIVU+PpTsUKinbaIkaEmbpuyGZr/NkVzKMf8DOCsbgmxt1mPXD+1rNL5WqD8buU82 JQGVWI3H3tdvxNL+Khgly8lPqTF/6Tv8Dcs4VDlfhOjoKtyCyYRzyItN4UrFLIJKky gygp41RyCCUwA== From: Christian Brauner Date: Mon, 30 Nov 2026 09:57:11 +0100 Subject: [PATCH v2 6/6] selftests/filesystems: add FAT O_TMPFILE tests X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261130-work-fat-tmpfile-v2-6-99af97d95683@kernel.org> References: <20261130-work-fat-tmpfile-v2-0-99af97d95683@kernel.org> In-Reply-To: <20261130-work-fat-tmpfile-v2-0-99af97d95683@kernel.org> To: OGAWA Hirofumi Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, "Christian Brauner (Amutable)" , Alexander Mikhalitsyn X-Mailer: b4 0.17-dev-04696 X-Developer-Signature: v=1; a=openpgp-sha256; l=13795; i=brauner@kernel.org; h=from:subject:message-id; bh=ogXig+1ibaiEuQh6RyEUFLqyEuRqc0bD9HfsLG24LrU=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTzWpWprZg7a4/aDv77wktVGTYcuZSjL1wr9sXN6Z5Jo cDBVruijlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgIkItDMyfL6+qWWOhMKbz0f6 Iz/rxuXeE4rwjuM9c+3DgR1G9Xe9XzH8L1sanHu9S1bs7O2zcVW2vqql216/f3wjoCI1vObxBMf ZLAA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add tests for O_TMPFILE on vfat and msdos. run_fat_tests.sh runs them on a vfat mount and on an msdos mount with the dots option. This tests that: - an O_TMPFILE can be linked into its own and into another directory - data written before and after linkat() ends up in the named file - O_EXCL keeps it from being linked - it can't be linked twice, nor again after it was unlinked - a failed linkat() leaves it linkable - link(2) of a regular file still fails with EPERM - linkat() to a temporary name plus rename() replaces a file - closing an unnamed O_TMPFILE frees its clusters - with dots a leading dot makes the linked file hidden After the tests the image is unmounted, checked with fsck.vfat -n and mounted again to compare a linked O_TMPFILE with a regular copy. Reviewed-by: Alexander Mikhalitsyn Signed-off-by: Christian Brauner (Amutable) --- tools/testing/selftests/filesystems/fat/.gitignore | 1 + tools/testing/selftests/filesystems/fat/Makefile | 2 +- tools/testing/selftests/filesystems/fat/config | 1 + .../selftests/filesystems/fat/run_fat_tests.sh | 41 +++ tools/testing/selftests/filesystems/fat/tmpfile.c | 324 +++++++++++++++++++++ 5 files changed, 368 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/filesystems/fat/.gitignore b/tools/testing/selftests/filesystems/fat/.gitignore index b89920ed841cc..b541a20590e3f 100644 --- a/tools/testing/selftests/filesystems/fat/.gitignore +++ b/tools/testing/selftests/filesystems/fat/.gitignore @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only rename_exchange +tmpfile diff --git a/tools/testing/selftests/filesystems/fat/Makefile b/tools/testing/selftests/filesystems/fat/Makefile index 902033f6ef098..4f3fb279056a7 100644 --- a/tools/testing/selftests/filesystems/fat/Makefile +++ b/tools/testing/selftests/filesystems/fat/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 TEST_PROGS := run_fat_tests.sh -TEST_GEN_PROGS_EXTENDED := rename_exchange +TEST_GEN_PROGS_EXTENDED := rename_exchange tmpfile CFLAGS += -O2 -g -Wall $(KHDR_INCLUDES) include ../../lib.mk diff --git a/tools/testing/selftests/filesystems/fat/config b/tools/testing/selftests/filesystems/fat/config index 6cf95e787a17b..49ee111e2e379 100644 --- a/tools/testing/selftests/filesystems/fat/config +++ b/tools/testing/selftests/filesystems/fat/config @@ -1,2 +1,3 @@ CONFIG_BLK_DEV_LOOP=y +CONFIG_MSDOS_FS=y CONFIG_VFAT_FS=y diff --git a/tools/testing/selftests/filesystems/fat/run_fat_tests.sh b/tools/testing/selftests/filesystems/fat/run_fat_tests.sh index 0712c8d23fd7e..38be991a44db1 100755 --- a/tools/testing/selftests/filesystems/fat/run_fat_tests.sh +++ b/tools/testing/selftests/filesystems/fat/run_fat_tests.sh @@ -51,6 +51,15 @@ mount_image() sudo mount -o loop "${IMG_PATH}" "${MNT_PATH}" } +mount_image_as() +{ + local fstype="$1" + local opts="$2" + + mkdir -p "${MNT_PATH}" + sudo mount -t "${fstype}" -o "loop${opts:+,${opts}}" "${IMG_PATH}" "${MNT_PATH}" +} + rename_exchange_test() { local rename_exchange="${BASE_DIR}/rename_exchange" @@ -86,11 +95,43 @@ unmount_image() sudo umount "${MNT_PATH}" &> /dev/null } +tmpfile_test() +{ + local tmpfile="${BASE_DIR}/tmpfile" + local fstype="$1" + local opts="$2" + local dots=0 + + if [ "${opts}" = "dots" ]; then + dots=1 + fi + + create_loopback + mount_image_as "${fstype}" "${opts}" + sudo env FAT_MNT="${MNT_PATH}" FAT_DOTS="${dots}" "${tmpfile}" + unmount_image + + # No clusters leaked by unnamed O_TMPFILEs, named ones are complete. + fsck.vfat -n "${IMG_PATH}" >/dev/null 2>&1 + + mount_image_as "${fstype}" "${opts}" + # Missing if the tests skipped because O_TMPFILE isn't supported. + if [ -e "${MNT_PATH}/regular.txt" ]; then + cmp "${MNT_PATH}/tmpfile.txt" "${MNT_PATH}/regular.txt" >/dev/null 2>&1 + if [ "${dots}" -eq 1 ]; then + test -f "${MNT_PATH}/.hidden" + fi + fi + unmount_image +} + check_prereqs create_loopback mount_image rename_exchange_test rename_exchange_subdir_test unmount_image +tmpfile_test vfat "" +tmpfile_test msdos dots exit 0 diff --git a/tools/testing/selftests/filesystems/fat/tmpfile.c b/tools/testing/selftests/filesystems/fat/tmpfile.c new file mode 100644 index 0000000000000..324660267d1b1 --- /dev/null +++ b/tools/testing/selftests/filesystems/fat/tmpfile.c @@ -0,0 +1,324 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * O_TMPFILE on vfat and msdos. run_fat_tests.sh mounts the image, passes the + * mount point in FAT_MNT and sets FAT_DOTS=1 for msdos mounted with "dots". + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest_harness.h" + +/* Left behind for run_fat_tests.sh to check after a remount. */ +#define TMPFILE_NAME "tmpfile.txt" +#define REGULAR_NAME "regular.txt" +#define HIDDEN_NAME ".hidden" + +/* Clusters the space test writes. */ +#define SPACE_CLUSTERS 8 + +/* Several clusters plus a partial one, a prime period so no two look alike. */ +#define PERSIST_SIZE (3 * 4096 + 123) +#define PERSIST_PERIOD 251 + +static const char data[] = "written through an O_TMPFILE\n"; +static const char old_data[] = "old\n"; + +static int open_tmpfile(int dir_fd, int flags) +{ + return openat(dir_fd, ".", O_TMPFILE | O_RDWR | O_CLOEXEC | flags, 0600); +} + +static int link_tmpfile(int fd, int dir_fd, const char *name) +{ + return linkat(fd, "", dir_fd, name, AT_EMPTY_PATH); +} + +static ssize_t read_file(int dir_fd, const char *name, char *buf, size_t size) +{ + ssize_t ret; + int fd; + + fd = openat(dir_fd, name, O_RDONLY | O_CLOEXEC); + if (fd < 0) + return -1; + ret = read(fd, buf, size); + close(fd); + return ret; +} + +FIXTURE(tmpfile) { + int mnt_fd; + int dir_fd; +}; + +FIXTURE_SETUP(tmpfile) +{ + const char *mnt = getenv("FAT_MNT"); + char name[16]; + int fd; + + if (!mnt) + SKIP(return, "FAT_MNT not set, run this from run_fat_tests.sh"); + + self->mnt_fd = open(mnt, O_DIRECTORY | O_RDONLY | O_CLOEXEC); + ASSERT_GE(self->mnt_fd, 0); + + /* A fresh directory per test, short enough for an 8.3 name. */ + snprintf(name, sizeof(name), "t%d", getpid()); + ASSERT_EQ(mkdirat(self->mnt_fd, name, 0755), 0); + self->dir_fd = openat(self->mnt_fd, name, + O_DIRECTORY | O_RDONLY | O_CLOEXEC); + ASSERT_GE(self->dir_fd, 0); + + fd = open_tmpfile(self->dir_fd, 0); + if (fd < 0 && errno == EOPNOTSUPP) + SKIP(return, "O_TMPFILE not supported"); + ASSERT_GE(fd, 0); + close(fd); +} + +FIXTURE_TEARDOWN(tmpfile) +{ + close(self->dir_fd); + close(self->mnt_fd); +} + +/* An O_TMPFILE gets a name and keeps what was written to it. */ +TEST_F(tmpfile, link) +{ + char buf[sizeof(data)]; + struct stat st; + int fd; + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(fstat(fd, &st), 0); + ASSERT_EQ(st.st_nlink, 0); + ASSERT_EQ(write(fd, data, sizeof(data)), sizeof(data)); + + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "file"), 0); + ASSERT_EQ(fstat(fd, &st), 0); + ASSERT_EQ(st.st_nlink, 1); + ASSERT_EQ(st.st_size, sizeof(data)); + ASSERT_EQ(close(fd), 0); + + ASSERT_EQ(read_file(self->dir_fd, "file", buf, sizeof(buf)), sizeof(data)); + ASSERT_EQ(memcmp(buf, data, sizeof(data)), 0); +} + +/* The name can be in another directory than the one O_TMPFILE was given. */ +TEST_F(tmpfile, link_other_dir) +{ + char buf[sizeof(data)]; + int fd, sub_fd; + + ASSERT_EQ(mkdirat(self->dir_fd, "sub", 0755), 0); + sub_fd = openat(self->dir_fd, "sub", O_DIRECTORY | O_RDONLY | O_CLOEXEC); + ASSERT_GE(sub_fd, 0); + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(write(fd, data, sizeof(data)), sizeof(data)); + ASSERT_EQ(link_tmpfile(fd, sub_fd, "file"), 0); + ASSERT_EQ(close(fd), 0); + + ASSERT_EQ(read_file(sub_fd, "file", buf, sizeof(buf)), sizeof(data)); + ASSERT_EQ(memcmp(buf, data, sizeof(data)), 0); + close(sub_fd); +} + +/* Data written after linkat() ends up in the named file too. */ +TEST_F(tmpfile, write_after_link) +{ + char buf[2 * sizeof(data)]; + struct stat st; + int fd; + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(write(fd, data, sizeof(data)), sizeof(data)); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "file"), 0); + ASSERT_EQ(write(fd, data, sizeof(data)), sizeof(data)); + ASSERT_EQ(close(fd), 0); + + ASSERT_EQ(fstatat(self->dir_fd, "file", &st, 0), 0); + ASSERT_EQ(st.st_size, sizeof(buf)); + ASSERT_EQ(read_file(self->dir_fd, "file", buf, sizeof(buf)), sizeof(buf)); + ASSERT_EQ(memcmp(buf, data, sizeof(data)), 0); + ASSERT_EQ(memcmp(buf + sizeof(data), data, sizeof(data)), 0); +} + +/* O_EXCL keeps an O_TMPFILE from ever getting a name. */ +TEST_F(tmpfile, excl) +{ + int fd; + + fd = open_tmpfile(self->dir_fd, O_EXCL); + ASSERT_GE(fd, 0); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "file"), -1); + ASSERT_EQ(errno, ENOENT); + close(fd); +} + +/* Only the first name sticks. */ +TEST_F(tmpfile, link_twice) +{ + int fd; + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "first"), 0); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "second"), -1); + ASSERT_EQ(errno, EPERM); + ASSERT_EQ(faccessat(self->dir_fd, "second", F_OK, 0), -1); + ASSERT_EQ(errno, ENOENT); + close(fd); +} + +/* Once named and unlinked it is a plain unlinked file. */ +TEST_F(tmpfile, relink_after_unlink) +{ + int fd; + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "file"), 0); + ASSERT_EQ(unlinkat(self->dir_fd, "file", 0), 0); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "file"), -1); + ASSERT_EQ(errno, ENOENT); + close(fd); +} + +/* A failed linkat() leaves the O_TMPFILE linkable. */ +TEST_F(tmpfile, link_invalid_name) +{ + int fd; + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "bad*name"), -1); + ASSERT_EQ(errno, EINVAL); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "file"), 0); + close(fd); +} + +/* FAT still has no hard links. */ +TEST_F(tmpfile, hardlink) +{ + int fd; + + fd = openat(self->dir_fd, "file", O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, + 0600); + ASSERT_GE(fd, 0); + close(fd); + ASSERT_EQ(linkat(self->dir_fd, "file", self->dir_fd, "link", 0), -1); + ASSERT_EQ(errno, EPERM); +} + +/* Link under a temporary name and rename over the target, as systemd does. */ +TEST_F(tmpfile, replace) +{ + char buf[sizeof(data)]; + int fd; + + fd = openat(self->dir_fd, "target", O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, + 0600); + ASSERT_GE(fd, 0); + ASSERT_EQ(write(fd, old_data, sizeof(old_data)), sizeof(old_data)); + close(fd); + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(write(fd, data, sizeof(data)), sizeof(data)); + ASSERT_EQ(link_tmpfile(fd, self->dir_fd, "target.tmp"), 0); + ASSERT_EQ(renameat(self->dir_fd, "target.tmp", self->dir_fd, "target"), 0); + ASSERT_EQ(close(fd), 0); + + ASSERT_EQ(read_file(self->dir_fd, "target", buf, sizeof(buf)), sizeof(data)); + ASSERT_EQ(memcmp(buf, data, sizeof(data)), 0); +} + +/* Closing an unnamed O_TMPFILE gives its clusters back. */ +TEST_F(tmpfile, space) +{ + struct statvfs before, during, after; + size_t len; + char *buf; + int fd; + + ASSERT_EQ(fstatvfs(self->dir_fd, &before), 0); + len = SPACE_CLUSTERS * before.f_bsize; + buf = malloc(len); + ASSERT_NE(buf, NULL); + memset(buf, 'x', len); + + fd = open_tmpfile(self->dir_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(write(fd, buf, len), len); + ASSERT_EQ(fstatvfs(fd, &during), 0); + ASSERT_LE(during.f_bfree, before.f_bfree - SPACE_CLUSTERS); + ASSERT_EQ(close(fd), 0); + + ASSERT_EQ(fstatvfs(self->dir_fd, &after), 0); + ASSERT_EQ(after.f_bfree, before.f_bfree); + free(buf); +} + +/* A named O_TMPFILE and a regular copy, compared after a remount. */ +TEST_F(tmpfile, persist) +{ + char buf[PERSIST_SIZE]; + size_t half = sizeof(buf) / 2; + size_t i; + int fd; + + for (i = 0; i < sizeof(buf); i++) + buf[i] = i % PERSIST_PERIOD; + + fd = openat(self->mnt_fd, REGULAR_NAME, + O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0644); + ASSERT_GE(fd, 0); + ASSERT_EQ(write(fd, buf, sizeof(buf)), sizeof(buf)); + ASSERT_EQ(fsync(fd), 0); + ASSERT_EQ(close(fd), 0); + + fd = open_tmpfile(self->mnt_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(write(fd, buf, half), half); + ASSERT_EQ(link_tmpfile(fd, self->mnt_fd, TMPFILE_NAME), 0); + ASSERT_EQ(write(fd, buf + half, sizeof(buf) - half), sizeof(buf) - half); + ASSERT_EQ(fsync(fd), 0); + ASSERT_EQ(close(fd), 0); + ASSERT_EQ(fsync(self->mnt_fd), 0); +} + +/* On msdos with "dots" a leading dot makes the named O_TMPFILE hidden. */ +TEST_F(tmpfile, hidden) +{ + const char *dots = getenv("FAT_DOTS"); + __u32 attrs; + int fd; + + if (!dots || strcmp(dots, "1")) + SKIP(return, "needs msdos mounted with the dots option"); + + fd = open_tmpfile(self->mnt_fd, 0); + ASSERT_GE(fd, 0); + ASSERT_EQ(link_tmpfile(fd, self->mnt_fd, HIDDEN_NAME), 0); + ASSERT_EQ(ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attrs), 0); + ASSERT_TRUE(attrs & ATTR_HIDDEN); + ASSERT_EQ(fsync(fd), 0); + ASSERT_EQ(close(fd), 0); + ASSERT_EQ(fsync(self->mnt_fd), 0); +} + +TEST_HARNESS_MAIN -- 2.53.0