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
;
26 ret
= find_next_zero_bit(table
->bitmap
, nr
, table
->alloc_hint
);
30 if (table
->alloc_hint
== ctx
->file_alloc_start
)
32 nr
= table
->alloc_hint
;
33 table
->alloc_hint
= ctx
->file_alloc_start
;
39 bool io_alloc_file_tables(struct io_ring_ctx
*ctx
, struct io_file_table
*table
,
42 if (io_rsrc_data_alloc(&table
->data
, nr_files
))
44 table
->bitmap
= bitmap_zalloc(nr_files
, GFP_KERNEL_ACCOUNT
);
47 io_rsrc_data_free(ctx
, &table
->data
);
51 void io_free_file_tables(struct io_ring_ctx
*ctx
, struct io_file_table
*table
)
53 io_rsrc_data_free(ctx
, &table
->data
);
54 bitmap_free(table
->bitmap
);
58 static int io_install_fixed_file(struct io_ring_ctx
*ctx
, struct file
*file
,
60 __must_hold(&req
->ctx
->uring_lock
)
62 struct io_rsrc_node
*node
;
64 if (io_is_uring_fops(file
))
66 if (!ctx
->file_table
.data
.nr
)
68 if (slot_index
>= ctx
->file_table
.data
.nr
)
71 node
= io_rsrc_node_alloc(ctx
, IORING_RSRC_FILE
);
75 if (!io_reset_rsrc_node(ctx
, &ctx
->file_table
.data
, slot_index
))
76 io_file_bitmap_set(&ctx
->file_table
, slot_index
);
78 ctx
->file_table
.data
.nodes
[slot_index
] = node
;
79 io_fixed_file_set(node
, file
);
83 int __io_fixed_fd_install(struct io_ring_ctx
*ctx
, struct file
*file
,
84 unsigned int file_slot
)
86 bool alloc_slot
= file_slot
== IORING_FILE_INDEX_ALLOC
;
90 ret
= io_file_bitmap_get(ctx
);
91 if (unlikely(ret
< 0))
98 ret
= io_install_fixed_file(ctx
, file
, file_slot
);
99 if (!ret
&& alloc_slot
)
104 * Note when io_fixed_fd_install() returns error value, it will ensure
105 * fput() is called correspondingly.
107 int io_fixed_fd_install(struct io_kiocb
*req
, unsigned int issue_flags
,
108 struct file
*file
, unsigned int file_slot
)
110 struct io_ring_ctx
*ctx
= req
->ctx
;
113 io_ring_submit_lock(ctx
, issue_flags
);
114 ret
= __io_fixed_fd_install(ctx
, file
, file_slot
);
115 io_ring_submit_unlock(ctx
, issue_flags
);
117 if (unlikely(ret
< 0))
122 int io_fixed_fd_remove(struct io_ring_ctx
*ctx
, unsigned int offset
)
124 struct io_rsrc_node
*node
;
126 if (unlikely(!ctx
->file_table
.data
.nr
))
128 if (offset
>= ctx
->file_table
.data
.nr
)
131 node
= io_rsrc_node_lookup(&ctx
->file_table
.data
, offset
);
134 io_reset_rsrc_node(ctx
, &ctx
->file_table
.data
, offset
);
135 io_file_bitmap_clear(&ctx
->file_table
, offset
);
139 int io_register_file_alloc_range(struct io_ring_ctx
*ctx
,
140 struct io_uring_file_index_range __user
*arg
)
142 struct io_uring_file_index_range range
;
145 if (copy_from_user(&range
, arg
, sizeof(range
)))
147 if (check_add_overflow(range
.off
, range
.len
, &end
))
149 if (range
.resv
|| end
> ctx
->file_table
.data
.nr
)
152 io_file_table_set_alloc_range(ctx
, range
.off
, range
.len
);