fat: support O_TMPFILE
Subsystems (3)
Christian Brauner
maintainerv2 cover fat: support O_TMPFILE
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.
---
Changes in v2:
- Add fat_new_inode() and use it in fat_build_inode() and fat_tmpfile().
- Add msdos_format_new_name() and use it in msdos_create(),
msdos_mkdir() and msdos_link().
- Add the FIXME comments Hirofumi asked for to vfat_link() and
msdos_link().
- Add Alexander's Reviewed-by to the patches from v1.
- Link to v1: https://patch.msgid.link/[email protected]
---
Christian Brauner (6):
fat: add fat_new_inode()
fat: add msdos_format_new_name()
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 | 58 +++- fs/fat/namei_msdos.c | 103 +++++-- fs/fat/namei_vfat.c | 47 +++ 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, 547 insertions(+), 32 deletions(-)
---
base-commit: 2dc60bd9975fc8666a13d58410499e06e6799ba9
change-id: 20261126-work-fat-tmpfile-0d0f49e95d8f
Christian Brauner
maintainerPATCH v2 1/6 fat: add fat_new_inode()
Move allocating and filling in the inode out of fat_build_inode() into fat_new_inode(). The O_TMPFILE support in the following patches needs an inode built the same way but without attaching it.
No functional changes.
--- fs/fat/inode.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-)
fs/fat/inode.c +22 -13
@@ -582,6 +582,26 @@ int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)return 0 ; } +/* Build an inode from @de without attaching or hashing it. */ +static struct inode *fat_new_inode (struct super_block *sb, +struct msdos_dir_entry *de) +{ +struct inode *inode; +int err; + + inode =new_inode (sb); +if (!inode) +return ERR_PTR (-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_PTR (err); + } +return inode; +} +static inline void fat_lock_build_inode (struct msdos_sb_info *sbi) {if (sbi->options.nfs ==FAT_NFS_NOSTALE_RO )
@@ -598,25 +618,14 @@ struct inode *fat_build_inode(struct super_block *sb, struct msdos_dir_entry *de, loff_t i_pos) {struct inode *inode; -int err;fat_lock_build_inode (MSDOS_SB (sb)); inode =fat_iget (sb, i_pos);if (inode) goto out; - inode =new_inode (sb); -if (!inode) { - inode =ERR_PTR (-ENOMEM ); - goto out; - } - inode->i_ino =iunique (sb,MSDOS_ROOT_INO ); -inode_set_iversion (inode,1 ); - err =fat_fill_inode (inode, de); -if (err) { -iput (inode); - inode =ERR_PTR (err); + inode =fat_new_inode (sb, de); +if (IS_ERR (inode)) goto out; - }fat_attach (inode, i_pos);insert_inode_hash (inode); out:
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 2/6 fat: add msdos_format_new_name()
In reply to Christian Braunermsdos_create() and msdos_mkdir() both format the new name, figure out whether it's hidden and refuse it if it clashes with an existing entry because of foo vs. .foo. Move that into msdos_format_new_name(). The msdos O_TMPFILE support in the following patches uses it as well.
No functional changes.
--- fs/fat/namei_msdos.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-)
fs/fat/namei_msdos.c +22 -18
@@ -262,6 +262,26 @@ static int msdos_add_entry(struct inode *dir, const unsigned char *name,return 0 ; } +/***** Formats the name of a new entry. Rejects foo vs. .foo clashes. */ +static int msdos_format_new_name (struct inode *dir,struct dentry *dentry, +unsigned char *msdos_name,int *is_hid) +{ +struct fat_slot_info sinfo; +int err; + + err =msdos_format_name (dentry->d_name.name, dentry->d_name.len, + msdos_name, &MSDOS_SB (dir->i_sb)->options); +if (err) +return err; + *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); +return -EINVAL ; + } +return 0 ; +} +/***** Create a file */ static int msdos_create (const struct mnt_idmap *idmap,struct inode *dir,struct dentry *dentry,umode_t mode)
@@ -275,17 +295,9 @@ static int msdos_create(const struct mnt_idmap *idmap, struct inode *dir,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); + err =msdos_format_new_name (dir, dentry, msdos_name, &is_hid);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);
@@ -358,17 +370,9 @@ static struct dentry *msdos_mkdir(const struct mnt_idmap *idmap, struct inode *dmutex_lock (&MSDOS_SB (sb)->s_lock); - err =msdos_format_name (dentry->d_name.name, dentry->d_name.len, - msdos_name, &MSDOS_SB (sb)->options); + err =msdos_format_new_name (dir, dentry, msdos_name, &is_hid);if (err) goto out; - is_hid = (dentry->d_name.name[0 ] =='.' ) && (msdos_name[0 ] !='.' ); -/* foo vs .foo situation */ -if (!fat_scan (dir, msdos_name, &sinfo)) { -brelse (sinfo.bh); - err = -EINVAL ; - goto out; - } ts =current_time (dir); cluster =fat_alloc_new_dir (dir, &ts);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 3/6 fat: add fat_tmpfile()
In reply to Christian BraunerFAT 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.
--- fs/fat/fat.h | 2 ++ fs/fat/inode.c | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+)
fs/fat/fat.h +2 -0
@@ -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);
fs/fat/inode.c +23 -0
@@ -635,6 +635,29 @@ 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; + +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 =fat_new_inode (sb, &de); +if (IS_ERR (inode)) +return PTR_ERR (inode); +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,
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 4/6 fat: support O_TMPFILE on vfat
In reply to Christian BraunerCurrently, 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.
--- fs/fat/namei_vfat.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
fs/fat/namei_vfat.c +47 -0
@@ -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,
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 5/6 fat: support O_TMPFILE on msdos
In reply to Christian BraunerDo 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.
--- fs/fat/namei_msdos.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
fs/fat/namei_msdos.c +63 -0
@@ -443,6 +443,67 @@ 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_new_name (dir, dentry, msdos_name, &is_hid); +if (err) + 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)) { +/* FIXME: this sync should be done within msdos_add_entry() */ + 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,
@@ -655,10 +716,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,
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 6/6 selftests/filesystems: add FAT O_TMPFILE tests
In reply to Christian BraunerAdd 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.
--- 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(-)
tools/testing/selftests/filesystems/fat/.gitignore +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only rename_exchange +tmpfile
tools/testing/selftests/filesystems/fat/Makefile +1 -1
@@ -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
tools/testing/selftests/filesystems/fat/config +1 -0
@@ -1,2 +1,3 @@ CONFIG_BLK_DEV_LOOP=y +CONFIG_MSDOS_FS=y CONFIG_VFAT_FS=y
tools/testing/selftests/filesystems/fat/run_fat_tests.sh +41 -0
58 lines not on this page · raw message
tools/testing/selftests/filesystems/fat/tmpfile.c new file +324 -0
324 lines not on this page · raw message
Signature · 1 line
2.53.0