Re: [PATCH 2/4] fat: support O_TMPFILE on vfat
From: OGAWA Hirofumi
Date: Sat Nov 28 2026 - 02:51:18 EST
Christian Brauner <brauner@xxxxxxxxxx> writes:
> 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@xxxxxxxxxx>
> ---
> 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);
Please add "FIXME: this sync should be done within vfat_add_entry()"
Thanks.
> + if (err)
> + goto out_detach;
> + brelse(sinfo.bh);
[...]
--
OGAWA Hirofumi <hirofumi@xxxxxxxxxxxxxxxxxx>