1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/file.h>
6 #include <linux/slab.h>
7 #include <linux/nospec.h>
8 #include <linux/io_uring.h>
10 #include <uapi/linux/io_uring.h>
14 #include "filetable.h"
16 static int io_file_bitmap_get(struct io_ring_ctx *ctx)
18 struct io_file_table *table = &ctx->file_table;
19 unsigned long nr = ctx->file_alloc_end;
23 ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
27 if (table->alloc_hint == ctx->file_alloc_start)
29 nr = table->alloc_hint;
30 table->alloc_hint = ctx->file_alloc_start;
36 bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
38 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
40 if (unlikely(!table->files))
43 table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
44 if (unlikely(!table->bitmap)) {
52 void io_free_file_tables(struct io_file_table *table)
55 bitmap_free(table->bitmap);
60 static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
62 __must_hold(&req->ctx->uring_lock)
64 bool needs_switch = false;
65 struct io_fixed_file *file_slot;
68 if (io_is_uring_fops(file))
72 if (slot_index >= ctx->nr_user_files)
75 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
76 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
78 if (file_slot->file_ptr) {
79 struct file *old_file;
81 ret = io_rsrc_node_switch_start(ctx);
85 old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
86 ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
87 ctx->rsrc_node, old_file);
90 file_slot->file_ptr = 0;
91 io_file_bitmap_clear(&ctx->file_table, slot_index);
95 ret = io_scm_file_account(ctx, file);
97 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
98 io_fixed_file_set(file_slot, file);
99 io_file_bitmap_set(&ctx->file_table, slot_index);
103 io_rsrc_node_switch(ctx, ctx->file_data);
109 int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
110 unsigned int file_slot)
112 bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
116 ret = io_file_bitmap_get(ctx);
117 if (unlikely(ret < 0))
124 ret = io_install_fixed_file(ctx, file, file_slot);
125 if (!ret && alloc_slot)
130 * Note when io_fixed_fd_install() returns error value, it will ensure
131 * fput() is called correspondingly.
133 int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
134 struct file *file, unsigned int file_slot)
136 struct io_ring_ctx *ctx = req->ctx;
139 io_ring_submit_lock(ctx, issue_flags);
140 ret = __io_fixed_fd_install(ctx, file, file_slot);
141 io_ring_submit_unlock(ctx, issue_flags);
143 if (unlikely(ret < 0))
148 int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
150 struct io_fixed_file *file_slot;
154 if (unlikely(!ctx->file_data))
156 if (offset >= ctx->nr_user_files)
158 ret = io_rsrc_node_switch_start(ctx);
162 offset = array_index_nospec(offset, ctx->nr_user_files);
163 file_slot = io_fixed_file_slot(&ctx->file_table, offset);
164 if (!file_slot->file_ptr)
167 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
168 ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file);
172 file_slot->file_ptr = 0;
173 io_file_bitmap_clear(&ctx->file_table, offset);
174 io_rsrc_node_switch(ctx, ctx->file_data);
178 int io_register_file_alloc_range(struct io_ring_ctx *ctx,
179 struct io_uring_file_index_range __user *arg)
181 struct io_uring_file_index_range range;
184 if (copy_from_user(&range, arg, sizeof(range)))
186 if (check_add_overflow(range.off, range.len, &end))
188 if (range.resv || end > ctx->nr_user_files)
191 io_file_table_set_alloc_range(ctx, range.off, range.len);