[PATCH 02/11] fs: factor out a set_fmode_can_read_write helper

From: Christoph Hellwig
Date: Wed Jun 24 2020 - 12:32:20 EST


Add a helper to set the FMODE_CAN_READ and FMODE_CAN_WRITE logic
instead of duplicating it in two places.

Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
fs/file_table.c | 7 +------
fs/internal.h | 10 ++++++++++
fs/open.c | 8 +-------
3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/fs/file_table.c b/fs/file_table.c
index 656647f9575a7c..646b83f07a9589 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -199,16 +199,11 @@ static struct file *alloc_file(const struct path *path, int flags,
file->f_mapping = path->dentry->d_inode->i_mapping;
file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
file->f_sb_err = file_sample_sb_err(file);
- if ((file->f_mode & FMODE_READ) &&
- likely(fop->read || fop->read_iter))
- file->f_mode |= FMODE_CAN_READ;
- if ((file->f_mode & FMODE_WRITE) &&
- likely(fop->write || fop->write_iter))
- file->f_mode |= FMODE_CAN_WRITE;
file->f_mode |= FMODE_OPENED;
file->f_op = fop;
if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
i_readcount_inc(path->dentry->d_inode);
+ set_fmode_can_read_write(file);
return file;
}

diff --git a/fs/internal.h b/fs/internal.h
index 9b863a7bd70892..242f2845b3428b 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -185,3 +185,13 @@ int sb_init_dio_done_wq(struct super_block *sb);
*/
int do_statx(int dfd, const char __user *filename, unsigned flags,
unsigned int mask, struct statx __user *buffer);
+
+static inline void set_fmode_can_read_write(struct file *f)
+{
+ if ((f->f_mode & FMODE_READ) &&
+ (f->f_op->read || f->f_op->read_iter))
+ f->f_mode |= FMODE_CAN_READ;
+ if ((f->f_mode & FMODE_WRITE) &&
+ (f->f_op->write || f->f_op->write_iter))
+ f->f_mode |= FMODE_CAN_WRITE;
+}
diff --git a/fs/open.c b/fs/open.c
index 6cd48a61cda3b9..01f2de93c91710 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -832,13 +832,7 @@ static int do_dentry_open(struct file *f,
f->f_mode |= FMODE_OPENED;
if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
i_readcount_inc(inode);
- if ((f->f_mode & FMODE_READ) &&
- likely(f->f_op->read || f->f_op->read_iter))
- f->f_mode |= FMODE_CAN_READ;
- if ((f->f_mode & FMODE_WRITE) &&
- likely(f->f_op->write || f->f_op->write_iter))
- f->f_mode |= FMODE_CAN_WRITE;
-
+ set_fmode_can_read_write(f);
f->f_write_hint = WRITE_LIFE_NOT_SET;
f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

--
2.26.2