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) --- 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