xref: /openbmc/linux/io_uring/filetable.h (revision 8487f083)
1453b329bSJens Axboe // SPDX-License-Identifier: GPL-2.0
2453b329bSJens Axboe #ifndef IOU_FILE_TABLE_H
3453b329bSJens Axboe #define IOU_FILE_TABLE_H
4453b329bSJens Axboe 
5024f15e0SPavel Begunkov #include <linux/file.h>
66e73dffbSPavel Begunkov #include <linux/io_uring_types.h>
7453b329bSJens Axboe 
8453b329bSJens Axboe #define FFS_NOWAIT		0x1UL
9453b329bSJens Axboe #define FFS_ISREG		0x2UL
1038eddb2cSPavel Begunkov #define FFS_MASK		~(FFS_NOWAIT|FFS_ISREG)
11453b329bSJens Axboe 
12453b329bSJens Axboe bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
13453b329bSJens Axboe void io_free_file_tables(struct io_file_table *table);
14c98817e6SJens Axboe 
15c98817e6SJens Axboe int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
16c98817e6SJens Axboe 			struct file *file, unsigned int file_slot);
17f110ed84SJens Axboe int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
18f110ed84SJens Axboe 				unsigned int file_slot);
19f110ed84SJens Axboe int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset);
20453b329bSJens Axboe 
216e73dffbSPavel Begunkov int io_register_file_alloc_range(struct io_ring_ctx *ctx,
226e73dffbSPavel Begunkov 				 struct io_uring_file_index_range __user *arg);
236e73dffbSPavel Begunkov 
24a4ad4f74SJens Axboe unsigned int io_file_get_flags(struct file *file);
25a4ad4f74SJens Axboe 
26453b329bSJens Axboe static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
27453b329bSJens Axboe {
284d505951SPavel Begunkov 	WARN_ON_ONCE(!test_bit(bit, table->bitmap));
29453b329bSJens Axboe 	__clear_bit(bit, table->bitmap);
30453b329bSJens Axboe 	table->alloc_hint = bit;
31453b329bSJens Axboe }
32453b329bSJens Axboe 
33453b329bSJens Axboe static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
34453b329bSJens Axboe {
35453b329bSJens Axboe 	WARN_ON_ONCE(test_bit(bit, table->bitmap));
36453b329bSJens Axboe 	__set_bit(bit, table->bitmap);
37453b329bSJens Axboe 	table->alloc_hint = bit + 1;
38453b329bSJens Axboe }
39453b329bSJens Axboe 
40453b329bSJens Axboe static inline struct io_fixed_file *
41453b329bSJens Axboe io_fixed_file_slot(struct io_file_table *table, unsigned i)
42453b329bSJens Axboe {
43453b329bSJens Axboe 	return &table->files[i];
44453b329bSJens Axboe }
45453b329bSJens Axboe 
46a4ad4f74SJens Axboe static inline struct file *io_file_from_index(struct io_file_table *table,
47a4ad4f74SJens Axboe 					      int index)
48a4ad4f74SJens Axboe {
49a4ad4f74SJens Axboe 	struct io_fixed_file *slot = io_fixed_file_slot(table, index);
50a4ad4f74SJens Axboe 
51a4ad4f74SJens Axboe 	return (struct file *) (slot->file_ptr & FFS_MASK);
52a4ad4f74SJens Axboe }
53a4ad4f74SJens Axboe 
54a4ad4f74SJens Axboe static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
55a4ad4f74SJens Axboe 				     struct file *file)
56a4ad4f74SJens Axboe {
57*8487f083SChristoph Hellwig 	file_slot->file_ptr = (unsigned long)file |
58*8487f083SChristoph Hellwig 		(io_file_get_flags(file) >> REQ_F_SUPPORT_NOWAIT_BIT);
59a4ad4f74SJens Axboe }
60a4ad4f74SJens Axboe 
616e73dffbSPavel Begunkov static inline void io_reset_alloc_hint(struct io_ring_ctx *ctx)
626e73dffbSPavel Begunkov {
636e73dffbSPavel Begunkov 	ctx->file_table.alloc_hint = ctx->file_alloc_start;
646e73dffbSPavel Begunkov }
656e73dffbSPavel Begunkov 
666e73dffbSPavel Begunkov static inline void io_file_table_set_alloc_range(struct io_ring_ctx *ctx,
676e73dffbSPavel Begunkov 						 unsigned off, unsigned len)
686e73dffbSPavel Begunkov {
696e73dffbSPavel Begunkov 	ctx->file_alloc_start = off;
706e73dffbSPavel Begunkov 	ctx->file_alloc_end = off + len;
716e73dffbSPavel Begunkov 	io_reset_alloc_hint(ctx);
726e73dffbSPavel Begunkov }
736e73dffbSPavel Begunkov 
74453b329bSJens Axboe #endif
75