xref: /openbmc/linux/io_uring/filetable.h (revision 4bfb0c9a)
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 bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
9453b329bSJens Axboe void io_free_file_tables(struct io_file_table *table);
10c98817e6SJens Axboe 
11c98817e6SJens Axboe int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
12c98817e6SJens Axboe 			struct file *file, unsigned int file_slot);
13f110ed84SJens Axboe int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
14f110ed84SJens Axboe 				unsigned int file_slot);
15f110ed84SJens Axboe int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset);
16453b329bSJens Axboe 
176e73dffbSPavel Begunkov int io_register_file_alloc_range(struct io_ring_ctx *ctx,
186e73dffbSPavel Begunkov 				 struct io_uring_file_index_range __user *arg);
196e73dffbSPavel Begunkov 
20a4ad4f74SJens Axboe unsigned int io_file_get_flags(struct file *file);
21a4ad4f74SJens Axboe 
io_file_bitmap_clear(struct io_file_table * table,int bit)22453b329bSJens Axboe static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
23453b329bSJens Axboe {
244d505951SPavel Begunkov 	WARN_ON_ONCE(!test_bit(bit, table->bitmap));
25453b329bSJens Axboe 	__clear_bit(bit, table->bitmap);
26453b329bSJens Axboe 	table->alloc_hint = bit;
27453b329bSJens Axboe }
28453b329bSJens Axboe 
io_file_bitmap_set(struct io_file_table * table,int bit)29453b329bSJens Axboe static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
30453b329bSJens Axboe {
31453b329bSJens Axboe 	WARN_ON_ONCE(test_bit(bit, table->bitmap));
32453b329bSJens Axboe 	__set_bit(bit, table->bitmap);
33453b329bSJens Axboe 	table->alloc_hint = bit + 1;
34453b329bSJens Axboe }
35453b329bSJens Axboe 
36453b329bSJens Axboe static inline struct io_fixed_file *
io_fixed_file_slot(struct io_file_table * table,unsigned i)37453b329bSJens Axboe io_fixed_file_slot(struct io_file_table *table, unsigned i)
38453b329bSJens Axboe {
39453b329bSJens Axboe 	return &table->files[i];
40453b329bSJens Axboe }
41453b329bSJens Axboe 
42*4bfb0c9aSChristoph Hellwig #define FFS_NOWAIT		0x1UL
43*4bfb0c9aSChristoph Hellwig #define FFS_ISREG		0x2UL
44*4bfb0c9aSChristoph Hellwig #define FFS_MASK		~(FFS_NOWAIT|FFS_ISREG)
45*4bfb0c9aSChristoph Hellwig 
io_slot_flags(struct io_fixed_file * slot)46*4bfb0c9aSChristoph Hellwig static inline unsigned int io_slot_flags(struct io_fixed_file *slot)
47*4bfb0c9aSChristoph Hellwig {
48*4bfb0c9aSChristoph Hellwig 	return (slot->file_ptr & ~FFS_MASK) << REQ_F_SUPPORT_NOWAIT_BIT;
49*4bfb0c9aSChristoph Hellwig }
50*4bfb0c9aSChristoph Hellwig 
io_slot_file(struct io_fixed_file * slot)51*4bfb0c9aSChristoph Hellwig static inline struct file *io_slot_file(struct io_fixed_file *slot)
52*4bfb0c9aSChristoph Hellwig {
53*4bfb0c9aSChristoph Hellwig 	return (struct file *)(slot->file_ptr & FFS_MASK);
54*4bfb0c9aSChristoph Hellwig }
55*4bfb0c9aSChristoph Hellwig 
io_file_from_index(struct io_file_table * table,int index)56a4ad4f74SJens Axboe static inline struct file *io_file_from_index(struct io_file_table *table,
57a4ad4f74SJens Axboe 					      int index)
58a4ad4f74SJens Axboe {
59*4bfb0c9aSChristoph Hellwig 	return io_slot_file(io_fixed_file_slot(table, index));
60a4ad4f74SJens Axboe }
61a4ad4f74SJens Axboe 
io_fixed_file_set(struct io_fixed_file * file_slot,struct file * file)62a4ad4f74SJens Axboe static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
63a4ad4f74SJens Axboe 				     struct file *file)
64a4ad4f74SJens Axboe {
658487f083SChristoph Hellwig 	file_slot->file_ptr = (unsigned long)file |
668487f083SChristoph Hellwig 		(io_file_get_flags(file) >> REQ_F_SUPPORT_NOWAIT_BIT);
67a4ad4f74SJens Axboe }
68a4ad4f74SJens Axboe 
io_reset_alloc_hint(struct io_ring_ctx * ctx)696e73dffbSPavel Begunkov static inline void io_reset_alloc_hint(struct io_ring_ctx *ctx)
706e73dffbSPavel Begunkov {
716e73dffbSPavel Begunkov 	ctx->file_table.alloc_hint = ctx->file_alloc_start;
726e73dffbSPavel Begunkov }
736e73dffbSPavel Begunkov 
io_file_table_set_alloc_range(struct io_ring_ctx * ctx,unsigned off,unsigned len)746e73dffbSPavel Begunkov static inline void io_file_table_set_alloc_range(struct io_ring_ctx *ctx,
756e73dffbSPavel Begunkov 						 unsigned off, unsigned len)
766e73dffbSPavel Begunkov {
776e73dffbSPavel Begunkov 	ctx->file_alloc_start = off;
786e73dffbSPavel Begunkov 	ctx->file_alloc_end = off + len;
796e73dffbSPavel Begunkov 	io_reset_alloc_hint(ctx);
806e73dffbSPavel Begunkov }
816e73dffbSPavel Begunkov 
82453b329bSJens Axboe #endif
83