* [PATCH 0/4] fat: support O_TMPFILE
@ 2026-11-26 22:15 Christian Brauner
2026-11-26 22:15 ` [PATCH 1/4] fat: add fat_tmpfile() Christian Brauner
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Christian Brauner @ 2026-11-26 22:15 UTC (permalink / raw)
To: OGAWA Hirofumi; +Cc: linux-kernel, linux-fsdevel, Christian Brauner (Amutable)
This adds O_TMPFILE support to vfat and msdos.
Currently, every O_TMPFILE open on FAT fails with EOPNOTSUPP and
userspace falls back to creating a named temporary file and renaming it
into place. systemd does that in bootctl when it writes to the ESP.
FAT can do O_TMPFILE just fine. It already handles files without a
directory entry: when an open file is unlinked i_pos is reset to 0,
writing the inode does nothing and the clusters are freed when the
inode is evicted. An O_TMPFILE simply starts out in that state.
The only tricky part is linkat(). FAT has no hard links so I added a
->link() that only gives an O_TMPFILE its first name. Everything else
still fails with EPERM. Adding just ->tmpfile() would've been a
regression. systemd only falls back to the named temporary file if the
O_TMPFILE open fails. If the open succeeds it calls linkat() and gives
up if that fails.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (4):
fat: add fat_tmpfile()
fat: support O_TMPFILE on vfat
fat: support O_TMPFILE on msdos
selftests/filesystems: add FAT O_TMPFILE tests
fs/fat/fat.h | 2 +
fs/fat/inode.c | 31 ++
fs/fat/namei_msdos.c | 70 +++++
fs/fat/namei_vfat.c | 46 +++
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 +++++++++++++++++++++
9 files changed, 517 insertions(+), 1 deletion(-)
---
base-commit: 2dc60bd9975fc8666a13d58410499e06e6799ba9
change-id: 20261126-work-fat-tmpfile-0d0f49e95d8f
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 1/4] fat: add fat_tmpfile() 2026-11-26 22:15 [PATCH 0/4] fat: support O_TMPFILE Christian Brauner @ 2026-11-26 22:15 ` Christian Brauner 2026-11-26 22:15 ` [PATCH 2/4] fat: support O_TMPFILE on vfat Christian Brauner ` (3 subsequent siblings) 4 siblings, 0 replies; 6+ messages in thread From: Christian Brauner @ 2026-11-26 22:15 UTC (permalink / raw) To: OGAWA Hirofumi; +Cc: linux-kernel, linux-fsdevel, Christian Brauner (Amutable) FAT already knows how to deal with a file that is open but has no directory entry. When an open file is unlinked fat_detach() resets i_pos to 0. From then on __fat_write_inode() has nothing to write, the data still goes to the file's cluster chain, and fat_evict_inode() frees the chain when the last reference is put. An O_TMPFILE is a file that starts out in that state. So build the inode like fat_build_inode() does, from a directory entry that only exists in memory, but don't attach it. fat_build_inode() itself can't be used for this. It would attach the inode at i_pos 0 and the next O_TMPFILE would find it through fat_iget(). The mode is ignored, just like in vfat_create() and msdos_create(). On FAT it's derived from the mount options. Nothing uses fat_tmpfile() yet. vfat and msdos need a ->link() method first and wire it up in the following patches. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> --- fs/fat/fat.h | 2 ++ fs/fat/inode.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/fs/fat/fat.h b/fs/fat/fat.h index a8a3ca4f465bf..cc706d89d1b96 100644 --- a/fs/fat/fat.h +++ b/fs/fat/fat.h @@ -423,6 +423,8 @@ extern void fat_detach(struct inode *inode); extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos); extern struct inode *fat_build_inode(struct super_block *sb, struct msdos_dir_entry *de, loff_t i_pos); +int fat_tmpfile(const struct mnt_idmap *idmap, struct inode *dir, + struct file *file, umode_t mode); extern int fat_fill_super(struct super_block *sb, struct fs_context *fc, void (*setup)(struct super_block *)); extern int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de); diff --git a/fs/fat/inode.c b/fs/fat/inode.c index 642e690632d83..1481ef1a3efac 100644 --- a/fs/fat/inode.c +++ b/fs/fat/inode.c @@ -626,6 +626,37 @@ struct inode *fat_build_inode(struct super_block *sb, EXPORT_SYMBOL_GPL(fat_build_inode); +/* Like an open file that got unlinked: no directory entry, i_pos is 0. */ +int fat_tmpfile(const struct mnt_idmap *idmap, struct inode *dir, + struct file *file, umode_t mode) +{ + struct super_block *sb = dir->i_sb; + struct msdos_dir_entry de = { .attr = ATTR_ARCH }; + struct timespec64 ts = current_time(dir); + struct inode *inode; + int err; + + fat_time_unix2fat(MSDOS_SB(sb), &ts, &de.time, &de.date, &de.ctime_cs); + de.ctime = de.time; + de.cdate = de.date; + de.adate = de.date; + + inode = new_inode(sb); + if (!inode) + return -ENOMEM; + inode->i_ino = iunique(sb, MSDOS_ROOT_INO); + inode_set_iversion(inode, 1); + err = fat_fill_inode(inode, &de); + if (err) { + iput(inode); + return err; + } + insert_inode_hash(inode); + d_tmpfile(file, inode); + return finish_open_simple(file, 0); +} +EXPORT_SYMBOL_GPL(fat_tmpfile); + static int __fat_write_inode(struct inode *inode); static int fat_sync_inode_metadata(struct inode *inode, -- 2.53.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/4] fat: support O_TMPFILE on vfat 2026-11-26 22:15 [PATCH 0/4] fat: support O_TMPFILE Christian Brauner 2026-11-26 22:15 ` [PATCH 1/4] fat: add fat_tmpfile() Christian Brauner @ 2026-11-26 22:15 ` Christian Brauner 2026-11-26 22:15 ` [PATCH 3/4] fat: support O_TMPFILE on msdos Christian Brauner ` (2 subsequent siblings) 4 siblings, 0 replies; 6+ messages in thread From: Christian Brauner @ 2026-11-26 22:15 UTC (permalink / raw) To: OGAWA Hirofumi; +Cc: linux-kernel, linux-fsdevel, Christian Brauner (Amutable) Currently, vfat doesn't implement ->link() since FAT has no hard links and link(2) fails with EPERM. But O_TMPFILE is only half a feature without linkat(). Worse, implementing just ->tmpfile() would break userspace that works fine right now. systemd's open_tmpfile_linkable_at() only falls back to a named temporary file if opening with O_TMPFILE fails. If the open succeeds link_tmpfile_at() calls linkat() and gives up if that fails. bootctl writes loader.conf, entries.srel and the boot loader binaries to the ESP like that. So add a ->link() that only gives an O_TMPFILE its first name. vfs_link() refuses to link an inode without links unless it is I_LINKABLE and it checks that under the inode lock. So an inode without links that makes it into vfat_link() is an O_TMPFILE that may be linked. Everything else still gets EPERM. Linking does what vfat_rename() does for a new name: add the directory entry, attach the inode at its position and let the inode write fill in the start cluster and the size. If the system crashes while an O_TMPFILE is open its clusters stay allocated but no directory entry refers to them. The same thing happens to an open file that was unlinked. fsck.fat finds them as lost clusters, with -a it saves them to FSCKnnnn.REC files. ext2, minix and udf implement O_TMPFILE without orphan handling as well. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> --- fs/fat/namei_vfat.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/fs/fat/namei_vfat.c b/fs/fat/namei_vfat.c index 791f2d7b85092..31b4a293c0493 100644 --- a/fs/fat/namei_vfat.c +++ b/fs/fat/namei_vfat.c @@ -938,6 +938,50 @@ static void vfat_update_dir_metadata(struct inode *dir, struct timespec64 *ts) (void)sync_inode_metadata(dir, 1); } +static int vfat_link(struct dentry *old_dentry, struct inode *dir, + struct dentry *dentry) +{ + struct inode *inode = d_inode(old_dentry); + struct super_block *sb = dir->i_sb; + struct fat_slot_info sinfo; + struct timespec64 ts; + int err, err2; + + /* No hard links on FAT, only give an O_TMPFILE its first name. */ + if (inode->i_nlink) + return -EPERM; + + mutex_lock(&MSDOS_SB(sb)->s_lock); + + ts = current_time(dir); + err = vfat_add_entry(dir, &dentry->d_name, 0, 0, &ts, &sinfo); + if (err) + goto out; + inode_inc_iversion(dir); + + fat_attach(inode, sinfo.i_pos); + err = vfat_sync_ipos(dir, inode); + if (err) + goto out_detach; + brelse(sinfo.bh); + + inc_nlink(inode); + ihold(inode); + d_instantiate(dentry, inode); + mutex_unlock(&MSDOS_SB(sb)->s_lock); + return 0; + +out_detach: + fat_detach(inode); + err2 = fat_remove_entries(dir, &sinfo); /* and releases bh */ + if (err2) + fat_msg(sb, KERN_WARNING, + "failed to roll back new entry (%d)", err2); +out: + mutex_unlock(&MSDOS_SB(sb)->s_lock); + return err; +} + static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) { @@ -1190,10 +1234,12 @@ static int vfat_rename2(const struct mnt_idmap *idmap, struct inode *old_dir, static const struct inode_operations vfat_dir_inode_operations = { .create = vfat_create, .lookup = vfat_lookup, + .link = vfat_link, .unlink = vfat_unlink, .mkdir = vfat_mkdir, .rmdir = vfat_rmdir, .rename = vfat_rename2, + .tmpfile = fat_tmpfile, .setattr = fat_setattr, .getattr = fat_getattr, .fileattr_get = fat_fileattr_get, -- 2.53.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 3/4] fat: support O_TMPFILE on msdos 2026-11-26 22:15 [PATCH 0/4] fat: support O_TMPFILE Christian Brauner 2026-11-26 22:15 ` [PATCH 1/4] fat: add fat_tmpfile() Christian Brauner 2026-11-26 22:15 ` [PATCH 2/4] fat: support O_TMPFILE on vfat Christian Brauner @ 2026-11-26 22:15 ` Christian Brauner 2026-11-26 22:15 ` [PATCH 4/4] selftests/filesystems: add FAT O_TMPFILE tests Christian Brauner 2026-11-27 8:43 ` [PATCH 0/4] fat: support O_TMPFILE Alexander Mikhalitsyn 4 siblings, 0 replies; 6+ messages in thread From: Christian Brauner @ 2026-11-26 22:15 UTC (permalink / raw) To: OGAWA Hirofumi; +Cc: linux-kernel, linux-fsdevel, Christian Brauner (Amutable) Do what vfat does. msdos_link() creates the directory entry the same way msdos_create() does and handles ATTR_HIDDEN like do_msdos_rename(). With the dots mount option a leading dot means the file is hidden and __fat_write_inode() writes the attributes from i_attrs. So i_attrs has to agree with the new name. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> --- fs/fat/namei_msdos.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/fs/fat/namei_msdos.c b/fs/fat/namei_msdos.c index 5a43d042eb01f..5709650a29986 100644 --- a/fs/fat/namei_msdos.c +++ b/fs/fat/namei_msdos.c @@ -439,6 +439,74 @@ static int msdos_unlink(struct inode *dir, struct dentry *dentry) return err; } +/***** Give an O_TMPFILE its first name */ +static int msdos_link(struct dentry *old_dentry, struct inode *dir, + struct dentry *dentry) +{ + struct inode *inode = d_inode(old_dentry); + struct super_block *sb = dir->i_sb; + struct fat_slot_info sinfo; + unsigned char msdos_name[MSDOS_NAME]; + struct timespec64 ts; + int err, err2, is_hid, old_attrs; + + /* FAT has no hard links. */ + if (inode->i_nlink) + return -EPERM; + + mutex_lock(&MSDOS_SB(sb)->s_lock); + + err = msdos_format_name(dentry->d_name.name, dentry->d_name.len, + msdos_name, &MSDOS_SB(sb)->options); + if (err) + goto out; + is_hid = (dentry->d_name.name[0] == '.') && (msdos_name[0] != '.'); + /* Have to do it due to foo vs. .foo conflicts */ + if (!fat_scan(dir, msdos_name, &sinfo)) { + brelse(sinfo.bh); + err = -EINVAL; + goto out; + } + + ts = current_time(dir); + err = msdos_add_entry(dir, msdos_name, 0, is_hid, 0, &ts, &sinfo); + if (err) + goto out; + inode_inc_iversion(dir); + + old_attrs = MSDOS_I(inode)->i_attrs; + fat_attach(inode, sinfo.i_pos); + if (is_hid) + MSDOS_I(inode)->i_attrs |= ATTR_HIDDEN; + else + MSDOS_I(inode)->i_attrs &= ~ATTR_HIDDEN; + mark_inode_dirty(inode); + if (IS_DIRSYNC(dir)) { + err = sync_inode_metadata(inode, 1); + if (err) + goto out_detach; + } + brelse(sinfo.bh); + + inc_nlink(inode); + ihold(inode); + d_instantiate(dentry, inode); + mutex_unlock(&MSDOS_SB(sb)->s_lock); + fat_flush_inodes(sb, dir, inode); + return 0; + +out_detach: + fat_detach(inode); + MSDOS_I(inode)->i_attrs = old_attrs; + err2 = fat_remove_entries(dir, &sinfo); /* and releases bh */ + if (err2) + fat_msg(sb, KERN_WARNING, + "failed to roll back new entry (%d)", err2); +out: + mutex_unlock(&MSDOS_SB(sb)->s_lock); + return err; +} + static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name, struct dentry *old_dentry, struct inode *new_dir, unsigned char *new_name, @@ -651,10 +719,12 @@ static int msdos_rename(const struct mnt_idmap *idmap, static const struct inode_operations msdos_dir_inode_operations = { .create = msdos_create, .lookup = msdos_lookup, + .link = msdos_link, .unlink = msdos_unlink, .mkdir = msdos_mkdir, .rmdir = msdos_rmdir, .rename = msdos_rename, + .tmpfile = fat_tmpfile, .setattr = fat_setattr, .getattr = fat_getattr, .fileattr_get = fat_fileattr_get, -- 2.53.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 4/4] selftests/filesystems: add FAT O_TMPFILE tests 2026-11-26 22:15 [PATCH 0/4] fat: support O_TMPFILE Christian Brauner ` (2 preceding siblings ...) 2026-11-26 22:15 ` [PATCH 3/4] fat: support O_TMPFILE on msdos Christian Brauner @ 2026-11-26 22:15 ` Christian Brauner 2026-11-27 8:43 ` [PATCH 0/4] fat: support O_TMPFILE Alexander Mikhalitsyn 4 siblings, 0 replies; 6+ messages in thread From: Christian Brauner @ 2026-11-26 22:15 UTC (permalink / raw) To: OGAWA Hirofumi; +Cc: linux-kernel, linux-fsdevel, Christian Brauner (Amutable) 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. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> --- 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 <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <sys/statvfs.h> +#include <unistd.h> +#include <linux/msdos_fs.h> + +#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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/4] fat: support O_TMPFILE 2026-11-26 22:15 [PATCH 0/4] fat: support O_TMPFILE Christian Brauner ` (3 preceding siblings ...) 2026-11-26 22:15 ` [PATCH 4/4] selftests/filesystems: add FAT O_TMPFILE tests Christian Brauner @ 2026-11-27 8:43 ` Alexander Mikhalitsyn 4 siblings, 0 replies; 6+ messages in thread From: Alexander Mikhalitsyn @ 2026-11-27 8:43 UTC (permalink / raw) To: Christian Brauner, OGAWA Hirofumi; +Cc: linux-kernel, linux-fsdevel On Thu, 2026-11-26 at 23:15 +0100, Christian Brauner wrote: > This adds O_TMPFILE support to vfat and msdos. > > Currently, every O_TMPFILE open on FAT fails with EOPNOTSUPP and > userspace falls back to creating a named temporary file and renaming > it > into place. systemd does that in bootctl when it writes to the ESP. > > FAT can do O_TMPFILE just fine. It already handles files without a > directory entry: when an open file is unlinked i_pos is reset to 0, > writing the inode does nothing and the clusters are freed when the > inode is evicted. An O_TMPFILE simply starts out in that state. > > The only tricky part is linkat(). FAT has no hard links so I added a > ->link() that only gives an O_TMPFILE its first name. Everything else > still fails with EPERM. Adding just ->tmpfile() would've been a > regression. systemd only falls back to the named temporary file if > the > O_TMPFILE open fails. If the open succeeds it calls linkat() and > gives > up if that fails. Hi Christian, > > Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> For the entire series: Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com> Kind regards, Alex > --- > Christian Brauner (4): > fat: add fat_tmpfile() > fat: support O_TMPFILE on vfat > fat: support O_TMPFILE on msdos > selftests/filesystems: add FAT O_TMPFILE tests > > fs/fat/fat.h | 2 + > fs/fat/inode.c | 31 ++ > fs/fat/namei_msdos.c | 70 +++++ > fs/fat/namei_vfat.c | 46 +++ > 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 > +++++++++++++++++++++ > 9 files changed, 517 insertions(+), 1 deletion(-) > --- > base-commit: 2dc60bd9975fc8666a13d58410499e06e6799ba9 > change-id: 20261126-work-fat-tmpfile-0d0f49e95d8f ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-27 8:43 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-26 22:15 [PATCH 0/4] fat: support O_TMPFILE Christian Brauner 2026-11-26 22:15 ` [PATCH 1/4] fat: add fat_tmpfile() Christian Brauner 2026-11-26 22:15 ` [PATCH 2/4] fat: support O_TMPFILE on vfat Christian Brauner 2026-11-26 22:15 ` [PATCH 3/4] fat: support O_TMPFILE on msdos Christian Brauner 2026-11-26 22:15 ` [PATCH 4/4] selftests/filesystems: add FAT O_TMPFILE tests Christian Brauner 2026-11-27 8:43 ` [PATCH 0/4] fat: support O_TMPFILE Alexander Mikhalitsyn
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®