Skip to content

Commit 210a03c

Browse files
committed
fs: claw back a few FMODE_* bits
There's a bunch of flags that are purely based on what the file operations support while also never being conditionally set or unset. IOW, they're not subject to change for individual files. Imho, such flags don't need to live in f_mode they might as well live in the fops structs itself. And the fops struct already has that lonely mmap_supported_flags member. We might as well turn that into a generic fop_flags member and move a few flags from FMODE_* space into FOP_* space. That gets us four FMODE_* bits back and the ability for new static flags that are about file ops to not have to live in FMODE_* space but in their own FOP_* space. It's not the most beautiful thing ever but it gets the job done. Yes, there'll be an additional pointer chase but hopefully that won't matter for these flags. I suspect there's a few more we can move into there and that we can also redirect a bunch of new flag suggestions that follow this pattern into the fop_flags field instead of f_mode. Link: https://lore.kernel.org/r/20240328-gewendet-spargel-aa60a030ef74@brauner Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Jan Kara <jack@suse.cz> Reviewed-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 68d6f4f commit 210a03c

12 files changed

Lines changed: 37 additions & 28 deletions

File tree

block/bdev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder,
904904
disk_unblock_events(disk);
905905

906906
bdev_file->f_flags |= O_LARGEFILE;
907-
bdev_file->f_mode |= FMODE_BUF_RASYNC | FMODE_CAN_ODIRECT;
907+
bdev_file->f_mode |= FMODE_CAN_ODIRECT;
908908
if (bdev_nowait(bdev))
909909
bdev_file->f_mode |= FMODE_NOWAIT;
910910
bdev_file->f_mapping = bdev->bd_inode->i_mapping;

block/fops.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ const struct file_operations def_blk_fops = {
863863
.splice_read = filemap_splice_read,
864864
.splice_write = iter_file_splice_write,
865865
.fallocate = blkdev_fallocate,
866+
.fop_flags = FOP_BUFFER_RASYNC,
866867
};
867868

868869
static __init int blkdev_init(void)

drivers/dax/device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ static const struct file_operations dax_fops = {
377377
.release = dax_release,
378378
.get_unmapped_area = dax_get_unmapped_area,
379379
.mmap = dax_mmap,
380-
.mmap_supported_flags = MAP_SYNC,
380+
.fop_flags = FOP_MMAP_SYNC,
381381
};
382382

383383
static void dev_dax_cdev_del(void *cdev)

fs/btrfs/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,8 +3719,7 @@ static int btrfs_file_open(struct inode *inode, struct file *filp)
37193719
{
37203720
int ret;
37213721

3722-
filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC | FMODE_BUF_WASYNC |
3723-
FMODE_CAN_ODIRECT;
3722+
filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
37243723

37253724
ret = fsverity_file_open(inode, filp);
37263725
if (ret)
@@ -3850,6 +3849,7 @@ const struct file_operations btrfs_file_operations = {
38503849
.compat_ioctl = btrfs_compat_ioctl,
38513850
#endif
38523851
.remap_file_range = btrfs_remap_file_range,
3852+
.fop_flags = FOP_BUFFER_RASYNC | FOP_BUFFER_WASYNC,
38533853
};
38543854

38553855
int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end)

fs/ext4/file.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,7 @@ static int ext4_file_open(struct inode *inode, struct file *filp)
885885
return ret;
886886
}
887887

888-
filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC |
889-
FMODE_DIO_PARALLEL_WRITE;
888+
filp->f_mode |= FMODE_NOWAIT;
890889
return dquot_file_open(inode, filp);
891890
}
892891

@@ -938,14 +937,15 @@ const struct file_operations ext4_file_operations = {
938937
.compat_ioctl = ext4_compat_ioctl,
939938
#endif
940939
.mmap = ext4_file_mmap,
941-
.mmap_supported_flags = MAP_SYNC,
942940
.open = ext4_file_open,
943941
.release = ext4_release_file,
944942
.fsync = ext4_sync_file,
945943
.get_unmapped_area = thp_get_unmapped_area,
946944
.splice_read = ext4_file_splice_read,
947945
.splice_write = iter_file_splice_write,
948946
.fallocate = ext4_fallocate,
947+
.fop_flags = FOP_MMAP_SYNC | FOP_BUFFER_RASYNC |
948+
FOP_DIO_PARALLEL_WRITE,
949949
};
950950

951951
const struct inode_operations ext4_file_inode_operations = {

fs/f2fs/file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ static int f2fs_file_open(struct inode *inode, struct file *filp)
569569
if (err)
570570
return err;
571571

572-
filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
572+
filp->f_mode |= FMODE_NOWAIT;
573573
filp->f_mode |= FMODE_CAN_ODIRECT;
574574

575575
return dquot_file_open(inode, filp);
@@ -5045,4 +5045,5 @@ const struct file_operations f2fs_file_operations = {
50455045
.splice_read = f2fs_file_splice_read,
50465046
.splice_write = iter_file_splice_write,
50475047
.fadvise = f2fs_file_fadvise,
5048+
.fop_flags = FOP_BUFFER_RASYNC,
50485049
};

fs/read_write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,7 @@ int generic_write_checks_count(struct kiocb *iocb, loff_t *count)
16851685

16861686
if ((iocb->ki_flags & IOCB_NOWAIT) &&
16871687
!((iocb->ki_flags & IOCB_DIRECT) ||
1688-
(file->f_mode & FMODE_BUF_WASYNC)))
1688+
(file->f_op->fop_flags & FOP_BUFFER_WASYNC)))
16891689
return -EINVAL;
16901690

16911691
return generic_write_check_limits(iocb->ki_filp, iocb->ki_pos, count);

fs/xfs/xfs_file.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,7 @@ xfs_file_open(
12301230
{
12311231
if (xfs_is_shutdown(XFS_M(inode->i_sb)))
12321232
return -EIO;
1233-
file->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC | FMODE_BUF_WASYNC |
1234-
FMODE_DIO_PARALLEL_WRITE | FMODE_CAN_ODIRECT;
1233+
file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
12351234
return generic_file_open(inode, file);
12361235
}
12371236

@@ -1490,14 +1489,15 @@ const struct file_operations xfs_file_operations = {
14901489
.compat_ioctl = xfs_file_compat_ioctl,
14911490
#endif
14921491
.mmap = xfs_file_mmap,
1493-
.mmap_supported_flags = MAP_SYNC,
14941492
.open = xfs_file_open,
14951493
.release = xfs_file_release,
14961494
.fsync = xfs_file_fsync,
14971495
.get_unmapped_area = thp_get_unmapped_area,
14981496
.fallocate = xfs_file_fallocate,
14991497
.fadvise = xfs_file_fadvise,
15001498
.remap_file_range = xfs_file_remap_range,
1499+
.fop_flags = FOP_MMAP_SYNC | FOP_BUFFER_RASYNC | FOP_BUFFER_WASYNC |
1500+
FOP_DIO_PARALLEL_WRITE,
15011501
};
15021502

15031503
const struct file_operations xfs_dir_file_operations = {
@@ -1510,4 +1510,6 @@ const struct file_operations xfs_dir_file_operations = {
15101510
.compat_ioctl = xfs_file_compat_ioctl,
15111511
#endif
15121512
.fsync = xfs_dir_fsync,
1513+
.fop_flags = FOP_MMAP_SYNC | FOP_BUFFER_RASYNC | FOP_BUFFER_WASYNC |
1514+
FOP_DIO_PARALLEL_WRITE,
15131515
};

include/linux/fs.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
163163

164164
#define FMODE_NOREUSE ((__force fmode_t)0x800000)
165165

166-
/* File supports non-exclusive O_DIRECT writes from multiple threads */
167-
#define FMODE_DIO_PARALLEL_WRITE ((__force fmode_t)0x1000000)
168-
169166
/* File is embedded in backing_file object */
170167
#define FMODE_BACKING ((__force fmode_t)0x2000000)
171168

@@ -181,12 +178,6 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
181178
/* File does not contribute to nr_files count */
182179
#define FMODE_NOACCOUNT ((__force fmode_t)0x20000000)
183180

184-
/* File supports async buffered reads */
185-
#define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000)
186-
187-
/* File supports async nowait buffered writes */
188-
#define FMODE_BUF_WASYNC ((__force fmode_t)0x80000000)
189-
190181
/*
191182
* Attribute flags. These should be or-ed together to figure out what
192183
* has been changed!
@@ -2001,8 +1992,11 @@ struct iov_iter;
20011992
struct io_uring_cmd;
20021993
struct offset_ctx;
20031994

1995+
typedef unsigned int __bitwise fop_flags_t;
1996+
20041997
struct file_operations {
20051998
struct module *owner;
1999+
fop_flags_t fop_flags;
20062000
loff_t (*llseek) (struct file *, loff_t, int);
20072001
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
20082002
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
@@ -2015,7 +2009,6 @@ struct file_operations {
20152009
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
20162010
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
20172011
int (*mmap) (struct file *, struct vm_area_struct *);
2018-
unsigned long mmap_supported_flags;
20192012
int (*open) (struct inode *, struct file *);
20202013
int (*flush) (struct file *, fl_owner_t id);
20212014
int (*release) (struct inode *, struct file *);
@@ -2046,6 +2039,15 @@ struct file_operations {
20462039
unsigned int poll_flags);
20472040
} __randomize_layout;
20482041

2042+
/* Supports async buffered reads */
2043+
#define FOP_BUFFER_RASYNC ((__force fop_flags_t)(1 << 0))
2044+
/* Supports async buffered writes */
2045+
#define FOP_BUFFER_WASYNC ((__force fop_flags_t)(1 << 1))
2046+
/* Supports synchronous page faults for mappings */
2047+
#define FOP_MMAP_SYNC ((__force fop_flags_t)(1 << 2))
2048+
/* Supports non-exclusive O_DIRECT writes from multiple threads */
2049+
#define FOP_DIO_PARALLEL_WRITE ((__force fop_flags_t)(1 << 3))
2050+
20492051
/* Wrap a directory iterator that needs exclusive inode access */
20502052
int wrap_directory_iterator(struct file *, struct dir_context *,
20512053
int (*) (struct file *, struct dir_context *));

io_uring/io_uring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ static void io_prep_async_work(struct io_kiocb *req)
471471

472472
/* don't serialize this request if the fs doesn't need it */
473473
if (should_hash && (req->file->f_flags & O_DIRECT) &&
474-
(req->file->f_mode & FMODE_DIO_PARALLEL_WRITE))
474+
(req->file->f_op->fop_flags & FOP_DIO_PARALLEL_WRITE))
475475
should_hash = false;
476476
if (should_hash || (ctx->flags & IORING_SETUP_IOPOLL))
477477
io_wq_hash_work(&req->work, file_inode(req->file));

0 commit comments

Comments
 (0)