Skip to content

Commit f110ed8

Browse files
committed
io_uring: split out fixed file installation and removal
Put it with the filetable code, which is where it belongs. While doing so, have the helpers take a ctx rather than an io_kiocb. It doesn't make sense to use a request, as it's not an operation on the request itself. It applies to the ring itself. Signed-off-by: Jens Axboe <[email protected]>
1 parent 8fcf4c4 commit f110ed8

File tree

5 files changed

+60
-48
lines changed

5 files changed

+60
-48
lines changed

io_uring/filetable.c

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ void io_free_file_tables(struct io_file_table *table)
5858
table->bitmap = NULL;
5959
}
6060

61-
static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
62-
unsigned int issue_flags, u32 slot_index)
61+
static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
62+
u32 slot_index)
6363
__must_hold(&req->ctx->uring_lock)
6464
{
65-
struct io_ring_ctx *ctx = req->ctx;
6665
bool needs_switch = false;
6766
struct io_fixed_file *file_slot;
6867
int ret;
@@ -108,34 +107,71 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
108107
return ret;
109108
}
110109

111-
/*
112-
* Note when io_fixed_fd_install() returns error value, it will ensure
113-
* fput() is called correspondingly.
114-
*/
115-
int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
116-
struct file *file, unsigned int file_slot)
110+
int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
111+
unsigned int file_slot)
117112
{
118113
bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
119-
struct io_ring_ctx *ctx = req->ctx;
120114
int ret;
121115

122-
io_ring_submit_lock(ctx, issue_flags);
123-
124116
if (alloc_slot) {
125117
ret = io_file_bitmap_get(ctx);
126118
if (unlikely(ret < 0))
127-
goto err;
119+
return ret;
128120
file_slot = ret;
129121
} else {
130122
file_slot--;
131123
}
132124

133-
ret = io_install_fixed_file(req, file, issue_flags, file_slot);
125+
ret = io_install_fixed_file(ctx, file, file_slot);
134126
if (!ret && alloc_slot)
135127
ret = file_slot;
136-
err:
128+
return ret;
129+
}
130+
/*
131+
* Note when io_fixed_fd_install() returns error value, it will ensure
132+
* fput() is called correspondingly.
133+
*/
134+
int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
135+
struct file *file, unsigned int file_slot)
136+
{
137+
struct io_ring_ctx *ctx = req->ctx;
138+
int ret;
139+
140+
io_ring_submit_lock(ctx, issue_flags);
141+
ret = __io_fixed_fd_install(ctx, file, file_slot);
137142
io_ring_submit_unlock(ctx, issue_flags);
143+
138144
if (unlikely(ret < 0))
139145
fput(file);
140146
return ret;
141147
}
148+
149+
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
150+
{
151+
struct io_fixed_file *file_slot;
152+
struct file *file;
153+
int ret;
154+
155+
if (unlikely(!ctx->file_data))
156+
return -ENXIO;
157+
if (offset >= ctx->nr_user_files)
158+
return -EINVAL;
159+
ret = io_rsrc_node_switch_start(ctx);
160+
if (ret)
161+
return ret;
162+
163+
offset = array_index_nospec(offset, ctx->nr_user_files);
164+
file_slot = io_fixed_file_slot(&ctx->file_table, offset);
165+
if (!file_slot->file_ptr)
166+
return -EBADF;
167+
168+
file = (struct file *)(file_slot->file_ptr & FFS_MASK);
169+
ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
170+
if (ret)
171+
return ret;
172+
173+
file_slot->file_ptr = 0;
174+
io_file_bitmap_clear(&ctx->file_table, offset);
175+
io_rsrc_node_switch(ctx, ctx->file_data);
176+
return 0;
177+
}

io_uring/filetable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ void io_free_file_tables(struct io_file_table *table);
2929

3030
int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
3131
struct file *file, unsigned int file_slot);
32+
int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
33+
unsigned int file_slot);
34+
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset);
3235

3336
unsigned int io_file_get_flags(struct file *file);
3437

io_uring/openclose.c

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -173,50 +173,23 @@ void io_open_cleanup(struct io_kiocb *req)
173173
putname(open->filename);
174174
}
175175

176-
int __io_close_fixed(struct io_kiocb *req, unsigned int issue_flags,
176+
int __io_close_fixed(struct io_ring_ctx *ctx, unsigned int issue_flags,
177177
unsigned int offset)
178178
{
179-
struct io_ring_ctx *ctx = req->ctx;
180-
struct io_fixed_file *file_slot;
181-
struct file *file;
182179
int ret;
183180

184181
io_ring_submit_lock(ctx, issue_flags);
185-
ret = -ENXIO;
186-
if (unlikely(!ctx->file_data))
187-
goto out;
188-
ret = -EINVAL;
189-
if (offset >= ctx->nr_user_files)
190-
goto out;
191-
ret = io_rsrc_node_switch_start(ctx);
192-
if (ret)
193-
goto out;
194-
195-
offset = array_index_nospec(offset, ctx->nr_user_files);
196-
file_slot = io_fixed_file_slot(&ctx->file_table, offset);
197-
ret = -EBADF;
198-
if (!file_slot->file_ptr)
199-
goto out;
200-
201-
file = (struct file *)(file_slot->file_ptr & FFS_MASK);
202-
ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
203-
if (ret)
204-
goto out;
205-
206-
file_slot->file_ptr = 0;
207-
io_file_bitmap_clear(&ctx->file_table, offset);
208-
io_rsrc_node_switch(ctx, ctx->file_data);
209-
ret = 0;
210-
out:
182+
ret = io_fixed_fd_remove(ctx, offset);
211183
io_ring_submit_unlock(ctx, issue_flags);
184+
212185
return ret;
213186
}
214187

215188
static inline int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
216189
{
217190
struct io_close *close = io_kiocb_to_cmd(req);
218191

219-
return __io_close_fixed(req, issue_flags, close->file_slot - 1);
192+
return __io_close_fixed(req->ctx, issue_flags, close->file_slot - 1);
220193
}
221194

222195
int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)

io_uring/openclose.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
int __io_close_fixed(struct io_kiocb *req, unsigned int issue_flags,
3+
int __io_close_fixed(struct io_ring_ctx *ctx, unsigned int issue_flags,
44
unsigned int offset);
55

66
int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);

io_uring/rsrc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ static int io_files_update_with_index_alloc(struct io_kiocb *req,
703703
if (ret < 0)
704704
break;
705705
if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
706-
__io_close_fixed(req, issue_flags, ret);
706+
__io_close_fixed(req->ctx, issue_flags, ret);
707707
ret = -EFAULT;
708708
break;
709709
}

0 commit comments

Comments
 (0)