io_uring: add helpers to decode the fixed file file_ptr
Remove all the open coded magic on slot->file_ptr by introducing two helpers that return the file pointer and the flags instead. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20230620113235.920399-9-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
parent
f432c8c8c1
commit
4bfb0c9af8
|
@ -78,10 +78,8 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
|
||||||
file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
|
file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
|
||||||
|
|
||||||
if (file_slot->file_ptr) {
|
if (file_slot->file_ptr) {
|
||||||
struct file *old_file;
|
ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
|
||||||
|
io_slot_file(file_slot));
|
||||||
old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
|
|
||||||
ret = io_queue_rsrc_removal(ctx->file_data, slot_index, old_file);
|
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -140,7 +138,6 @@ int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
|
||||||
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
|
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
|
||||||
{
|
{
|
||||||
struct io_fixed_file *file_slot;
|
struct io_fixed_file *file_slot;
|
||||||
struct file *file;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (unlikely(!ctx->file_data))
|
if (unlikely(!ctx->file_data))
|
||||||
|
@ -153,8 +150,8 @@ int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
|
||||||
if (!file_slot->file_ptr)
|
if (!file_slot->file_ptr)
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
|
|
||||||
file = (struct file *)(file_slot->file_ptr & FFS_MASK);
|
ret = io_queue_rsrc_removal(ctx->file_data, offset,
|
||||||
ret = io_queue_rsrc_removal(ctx->file_data, offset, file);
|
io_slot_file(file_slot));
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,6 @@
|
||||||
#include <linux/file.h>
|
#include <linux/file.h>
|
||||||
#include <linux/io_uring_types.h>
|
#include <linux/io_uring_types.h>
|
||||||
|
|
||||||
#define FFS_NOWAIT 0x1UL
|
|
||||||
#define FFS_ISREG 0x2UL
|
|
||||||
#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
|
|
||||||
|
|
||||||
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
|
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
|
||||||
void io_free_file_tables(struct io_file_table *table);
|
void io_free_file_tables(struct io_file_table *table);
|
||||||
|
|
||||||
|
@ -43,12 +39,24 @@ io_fixed_file_slot(struct io_file_table *table, unsigned i)
|
||||||
return &table->files[i];
|
return &table->files[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define FFS_NOWAIT 0x1UL
|
||||||
|
#define FFS_ISREG 0x2UL
|
||||||
|
#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
|
||||||
|
|
||||||
|
static inline unsigned int io_slot_flags(struct io_fixed_file *slot)
|
||||||
|
{
|
||||||
|
return (slot->file_ptr & ~FFS_MASK) << REQ_F_SUPPORT_NOWAIT_BIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline struct file *io_slot_file(struct io_fixed_file *slot)
|
||||||
|
{
|
||||||
|
return (struct file *)(slot->file_ptr & FFS_MASK);
|
||||||
|
}
|
||||||
|
|
||||||
static inline struct file *io_file_from_index(struct io_file_table *table,
|
static inline struct file *io_file_from_index(struct io_file_table *table,
|
||||||
int index)
|
int index)
|
||||||
{
|
{
|
||||||
struct io_fixed_file *slot = io_fixed_file_slot(table, index);
|
return io_slot_file(io_fixed_file_slot(table, index));
|
||||||
|
|
||||||
return (struct file *) (slot->file_ptr & FFS_MASK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
|
static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
|
||||||
|
|
|
@ -2028,19 +2028,17 @@ inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
|
||||||
unsigned int issue_flags)
|
unsigned int issue_flags)
|
||||||
{
|
{
|
||||||
struct io_ring_ctx *ctx = req->ctx;
|
struct io_ring_ctx *ctx = req->ctx;
|
||||||
|
struct io_fixed_file *slot;
|
||||||
struct file *file = NULL;
|
struct file *file = NULL;
|
||||||
unsigned long file_ptr;
|
|
||||||
|
|
||||||
io_ring_submit_lock(ctx, issue_flags);
|
io_ring_submit_lock(ctx, issue_flags);
|
||||||
|
|
||||||
if (unlikely((unsigned int)fd >= ctx->nr_user_files))
|
if (unlikely((unsigned int)fd >= ctx->nr_user_files))
|
||||||
goto out;
|
goto out;
|
||||||
fd = array_index_nospec(fd, ctx->nr_user_files);
|
fd = array_index_nospec(fd, ctx->nr_user_files);
|
||||||
file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
|
slot = io_fixed_file_slot(&ctx->file_table, fd);
|
||||||
file = (struct file *) (file_ptr & FFS_MASK);
|
file = io_slot_file(slot);
|
||||||
file_ptr &= ~FFS_MASK;
|
req->flags |= io_slot_flags(slot);
|
||||||
/* mask in overlapping REQ_F and FFS bits */
|
|
||||||
req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
|
|
||||||
io_req_set_rsrc_node(req, ctx, 0);
|
io_req_set_rsrc_node(req, ctx, 0);
|
||||||
out:
|
out:
|
||||||
io_ring_submit_unlock(ctx, issue_flags);
|
io_ring_submit_unlock(ctx, issue_flags);
|
||||||
|
|
|
@ -354,7 +354,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
|
||||||
__s32 __user *fds = u64_to_user_ptr(up->data);
|
__s32 __user *fds = u64_to_user_ptr(up->data);
|
||||||
struct io_rsrc_data *data = ctx->file_data;
|
struct io_rsrc_data *data = ctx->file_data;
|
||||||
struct io_fixed_file *file_slot;
|
struct io_fixed_file *file_slot;
|
||||||
struct file *file;
|
|
||||||
int fd, i, err = 0;
|
int fd, i, err = 0;
|
||||||
unsigned int done;
|
unsigned int done;
|
||||||
|
|
||||||
|
@ -382,15 +381,16 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
|
||||||
file_slot = io_fixed_file_slot(&ctx->file_table, i);
|
file_slot = io_fixed_file_slot(&ctx->file_table, i);
|
||||||
|
|
||||||
if (file_slot->file_ptr) {
|
if (file_slot->file_ptr) {
|
||||||
file = (struct file *)(file_slot->file_ptr & FFS_MASK);
|
err = io_queue_rsrc_removal(data, i,
|
||||||
err = io_queue_rsrc_removal(data, i, file);
|
io_slot_file(file_slot));
|
||||||
if (err)
|
if (err)
|
||||||
break;
|
break;
|
||||||
file_slot->file_ptr = 0;
|
file_slot->file_ptr = 0;
|
||||||
io_file_bitmap_clear(&ctx->file_table, i);
|
io_file_bitmap_clear(&ctx->file_table, i);
|
||||||
}
|
}
|
||||||
if (fd != -1) {
|
if (fd != -1) {
|
||||||
file = fget(fd);
|
struct file *file = fget(fd);
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
err = -EBADF;
|
err = -EBADF;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue