Linux filesystem development
 help / color / mirror / Atom feed
From: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Christian Brauner <brauner@kernel.org>,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Alexander Mikhalitsyn <alex@amutable.com>
Subject: Re: [PATCH v2 4/6] fat: support O_TMPFILE on vfat
Date: Sat, 05 Dec 2026 16:44:01 +0900	[thread overview]
Message-ID: <871p7w2mv2.fsf@mail.parknet.co.jp> (raw)
In-Reply-To: <20261130-work-fat-tmpfile-v2-4-99af97d95683@kernel.org>

Christian Brauner <brauner@kernel.org> 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.
>
> Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

Thanks.

> ---
>  fs/fat/namei_vfat.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 47 insertions(+)
>
> diff --git a/fs/fat/namei_vfat.c b/fs/fat/namei_vfat.c
> index 791f2d7b85092..f17f5a2aaed99 100644
> --- a/fs/fat/namei_vfat.c
> +++ b/fs/fat/namei_vfat.c
> @@ -938,6 +938,51 @@ 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);
> +	/* FIXME: this sync should be done within vfat_add_entry() */
> +	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 +1235,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,

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

  reply	other threads:[~2026-12-05  7:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:57 [PATCH v2 0/6] fat: support O_TMPFILE Christian Brauner
2026-11-30  8:57 ` [PATCH v2 1/6] fat: add fat_new_inode() Christian Brauner
2026-12-05  7:43   ` OGAWA Hirofumi
2026-11-30  8:57 ` [PATCH v2 2/6] fat: add msdos_format_new_name() Christian Brauner
2026-12-05  7:43   ` OGAWA Hirofumi
2026-11-30  8:57 ` [PATCH v2 3/6] fat: add fat_tmpfile() Christian Brauner
2026-12-05  7:43   ` OGAWA Hirofumi
2026-11-30  8:57 ` [PATCH v2 4/6] fat: support O_TMPFILE on vfat Christian Brauner
2026-12-05  7:44   ` OGAWA Hirofumi [this message]
2026-11-30  8:57 ` [PATCH v2 5/6] fat: support O_TMPFILE on msdos Christian Brauner
2026-12-05  7:44   ` OGAWA Hirofumi
2026-11-30  8:57 ` [PATCH v2 6/6] selftests/filesystems: add FAT O_TMPFILE tests Christian Brauner
2026-12-05  7:44   ` OGAWA Hirofumi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871p7w2mv2.fsf@mail.parknet.co.jp \
    --to=hirofumi@mail.parknet.co.jp \
    --cc=akpm@linux-foundation.org \
    --cc=alex@amutable.com \
    --cc=brauner@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox