fs: factor out final file open logic out of openat()
PATCH 1/2 fs: factor out final file open logic out of openat()
The final open logic will be reused for the tmpfile logic in a follow-up commit, so factor it out into a function with semantics similar to the Linux equivalent.
No functional change intended.
--- fs/fs.c | 53 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 21 deletions(-)
fs/fs.c +32 -21
@@ -2561,10 +2561,37 @@ static int rmdirat(int dirfd, const char *pathname)return errno_set (error); } +static int do_dentry_open (struct file *f) +{ +int error; + + f->f_inode =d_inode (f->f_path.dentry); + +if (unlikely (f->f_flags &O_PATH )) +return 0 ; + +if (f->f_inode->i_fop->open) { + error = f->f_inode->i_fop->open (f->f_inode, f); +if (error) +return error; + } + +if (f->f_flags &O_TRUNC ) { + error =fsdev_truncate (f,0 ); + f->f_size =0 ; +if (error) +return error; + } + +if (f->f_flags &O_APPEND ) + f->f_pos = f->f_size; + +return 0 ; +} +int openat (int dirfd,const char *pathname,int flags) {struct fs_device *fsdev; -struct fs_driver *fsdrv;struct super_block *sb;struct file *f;int error =0 ;
@@ -2660,29 +2687,13 @@ int openat(int dirfd, const char *pathname, int flags) f->path =dpath (dentry, d_root); f->f_path = path; - f->f_inode =iget (inode); f->f_flags = flags; - fsdrv = fsdev->driver; +iget (inode); -if (flags &O_PATH ) -return file_to_fd (f); - -if (f->f_inode->i_fop->open) { - error = f->f_inode->i_fop->open (inode, f); -if (error) - goto out; - } - -if (flags &O_TRUNC ) { - error =fsdev_truncate (f,0 ); - f->f_size =0 ; -if (error) - goto out; - } - -if (flags &O_APPEND ) - f->f_pos = f->f_size; + error =do_dentry_open (f); +if (error) + goto out;return file_to_fd (f);
Signature · 1 line
2.47.3
PATCH 2/2 fs: allocate inodes for O_TMPFILE-created files
We support O_TMPFILE only for ramfs and the code enabling it was so far hacked into the file system core.
With upcoming patches to move read/write operations from struct fs_driver to struct file_operations, this will break:
- struct inode::i_fop holds the file operations
- we do not allocate inodes for temporary files
- there is no way for the core to read/write the temporary files
Fix this by properly implementing O_TMPFILE similarly to how Linux does it:
- File systems implement an optional file_operations::tmpfile
- Temporary files are allocated an inode, but no dentries, so nlink is zero
---
Please place before "fs: move fs_driver operations to struct file_operations" to avoid intermittent breakage and so "upcoming patches" makes sense.
--- fs/fs.c | 104 ++++++++++++++++++++++++++++++++++----------- fs/ramfs.c | 14 ++++++ include/errno.h | 6 +++ include/linux/fs.h | 26 ++++++++++++ 4 files changed, 125 insertions(+), 25 deletions(-)
fs/fs.c +79 -25
@@ -2589,6 +2589,70 @@ static int do_dentry_open(struct file *f)return 0 ; } +/** +* finish_open - finish opening a file +* @file: file pointer +* @dentry: pointer to dentry +* +* If the open callback is set to NULL, then the standard f_op->open() +* filesystem callback is substituted. +* +* In Linux, this function accepts an open callback, but we don't yet +* need this in barebox. Thus the standard f_op()->open() callback +* will be used for non-O_PATH. +* +* Returns zero on success or -errno if the open failed. +*/ +int finish_open (struct file *file,struct dentry *dentry) +{ + file->f_path.dentry = dentry; +return do_dentry_open (file); +} + +/** +* tmpfile_create - create tmpfile +* @parentpath: pointer to the path of the base directory +* @mode: mode of the new tmpfile +* @flags: flags used to open the file +* +* Create a temporary file. +*/ +static struct file *tmpfile_create (const struct path *parentpath, +umode_t mode,int flags) +{ +struct inode *dir =d_inode (parentpath->dentry); +struct fs_device *fsdev; +struct file *f; +int error; + + fsdev =get_fsdevice_by_dentry (parentpath->dentry); +if (!fsdev) +return ERR_PTR (-ENOENT ); + +if (!dir->i_op->tmpfile) +return ERR_PTR (-EOPNOTSUPP ); + + f =get_file (fsdev); +if (!f) +return ERR_PTR (-EMFILE ); + + f->f_path.mnt = parentpath->mnt; + f->f_path.dentry =d_alloc_anon (&fsdev->sb); + f->f_flags = flags; + + error = dir->i_op->tmpfile (dir, f, mode); + +free (f->f_path.dentry); + f->f_path.dentry =NULL ; + +if (error) { +put_file (f); +return ERR_PTR (error); + } + +return f; +} +int openat (int dirfd,const char *pathname,int flags) {struct fs_device *fsdev;
@@ -2604,33 +2668,10 @@ int openat(int dirfd, const char *pathname, int flags)if (error)return errno_set (error); - fsdev =get_fsdevice_by_dentry (path.dentry); + f =tmpfile_create (&path,S_IFREG , flags);path_put (&path); -if (!fsdev) { - errno =ENOENT ; -return -errno; - } - -if (fsdev->driver != ramfs_driver) { - errno =EOPNOTSUPP ; -return -errno; - } - - f =get_file (fsdev); -if (!f) { - errno =EMFILE ; -return -errno; - } - - f->path =NULL ; - f->f_path.dentry =NULL ; - f->f_inode =new_inode (&fsdev->sb); - f->f_inode->i_mode =S_IFREG ; - f->f_flags = flags; - f->f_size =0 ; - -return file_to_fd (f); +return errno_setp (f) ?:file_to_fd (f); }if (flags &O_CREAT ) {
@@ -2989,6 +3030,19 @@ static char *__dpath(struct dentry *dentry, struct dentry *root)return res; } +void d_tmpfile (struct file *file,struct inode *inode) +{ +struct dentry *dentry = file->f_path.dentry; + +inode_dec_link_count (inode); + + file->path =xasprintf (dentry->name,"#%llu" , + (unsigned long long )inode->i_ino); + +d_instantiate (dentry, inode); +} +EXPORT_SYMBOL (d_tmpfile); +/** * dpath - return path of a dentry * @dentry: The dentry to return the path from
fs/ramfs.c +14 -0
@@ -174,6 +174,19 @@ static const struct inode_operations ramfs_symlink_inode_operations = .get_link = ramfs_get_link, }; +static int ramfs_tmpfile (struct inode *dir,struct file *file,umode_t mode) +{ +struct inode *inode; + + inode =ramfs_get_inode (dir->i_sb, dir, mode); +if (!inode) +return -ENOSPC ; + +d_tmpfile (file, inode); + +return finish_open_simple (file,0 ); +} +static const struct inode_operations ramfs_dir_inode_operations = { .lookup = simple_lookup,
@@ -182,6 +195,7 @@ static const struct inode_operations ramfs_dir_inode_operations = .rmdir = simple_rmdir, .unlink = simple_unlink, .create = ramfs_create, + .tmpfile = ramfs_tmpfile, }; static struct ramfs_chunk *ramfs_find_chunk(struct ramfs_inode *node,
include/errno.h +6 -0
@@ -27,4 +27,10 @@ static inline int errno_set(int err) return err; } +static inline int errno_setp (const void *errp) +{ +int error =IS_ERR (errp) ?PTR_ERR (errp) :0 ; +return errno_set (error); +} +#endif /* __ERRNO_H */
include/linux/fs.h +26 -0
@@ -443,6 +443,7 @@ struct inode_operations {int (*rmdir) (struct inode *,struct dentry *);int (*rename) (struct inode *,struct dentry *,struct inode *,struct dentry *,unsigned int ); +int (*tmpfile)(struct inode *,struct file *,umode_t ); };static inline ino_t parent_ino (struct dentry *dentry)
@@ -508,6 +509,8 @@ void drop_nlink(struct inode *inode);extern const struct file_operations simple_dir_operations;extern const struct inode_operations simple_symlink_inode_operations; +extern void d_tmpfile (struct file *,struct inode *); +int simple_empty (struct dentry *dentry);int simple_unlink (struct inode *dir,struct dentry *dentry);int simple_rmdir (struct inode *dir,struct dentry *dentry);
@@ -533,4 +536,27 @@ static inline void inode_init_owner(struct inode *inode,static inline void mark_inode_dirty (struct inode *inode) {} +int finish_open (struct file *file,struct dentry *dentry); + +/* Helper for the simple case when original dentry is used */ +static inline int finish_open_simple (struct file *file,int error) +{ +if (error) +return error; + +return finish_open (file, file->f_path.dentry); +} + +static inline void inode_inc_link_count (struct inode *inode) +{ +inc_nlink (inode); +mark_inode_dirty (inode); +} + +static inline void inode_dec_link_count (struct inode *inode) +{ +drop_nlink (inode); +mark_inode_dirty (inode); +} +#endif /* _LINUX_FS_H */
Signature · 1 line
2.47.3
Sascha Hauer
AppliedQuoted 8 lines · Ahmad Fatoum
The final open logic will be reused for the tmpfile logic in a follow-up commit, so factor it out into a function with semantics similar to the Linux equivalent.
No functional change intended.
[...]
Applied, thanks!
[1/2] fs: factor out final file open logic out of openat()
https://git.pengutronix.de/cgit/barebox/commit/?id=3a63fc70b88d (link may not be stable)
[2/2] fs: allocate inodes for O_TMPFILE-created files
https://git.pengutronix.de/cgit/barebox/commit/?id=e5591127b7d3 (link may not be stable)
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>