xref: /openbmc/linux/io_uring/filetable.h (revision 024f15e0)
1453b329bSJens Axboe // SPDX-License-Identifier: GPL-2.0
2453b329bSJens Axboe #ifndef IOU_FILE_TABLE_H
3453b329bSJens Axboe #define IOU_FILE_TABLE_H
4453b329bSJens Axboe 
5*024f15e0SPavel Begunkov #include <linux/file.h>
6*024f15e0SPavel Begunkov 
7453b329bSJens Axboe struct io_ring_ctx;
8c98817e6SJens Axboe struct io_kiocb;
9453b329bSJens Axboe 
10453b329bSJens Axboe /*
11453b329bSJens Axboe  * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0
12453b329bSJens Axboe  * and define IO_URING_SCM_ALL. For this case, we use SCM for all files as we
13453b329bSJens Axboe  * can't safely always dereference the file when the task has exited and ring
14453b329bSJens Axboe  * cleanup is done. If a file is tracked and part of SCM, then unix gc on
15453b329bSJens Axboe  * process exit may reap it before __io_sqe_files_unregister() is run.
16453b329bSJens Axboe  */
17453b329bSJens Axboe #define FFS_NOWAIT		0x1UL
18453b329bSJens Axboe #define FFS_ISREG		0x2UL
19453b329bSJens Axboe #if defined(CONFIG_64BIT)
20453b329bSJens Axboe #define FFS_SCM			0x4UL
21453b329bSJens Axboe #else
22453b329bSJens Axboe #define IO_URING_SCM_ALL
23453b329bSJens Axboe #define FFS_SCM			0x0UL
24453b329bSJens Axboe #endif
25453b329bSJens Axboe #define FFS_MASK		~(FFS_NOWAIT|FFS_ISREG|FFS_SCM)
26453b329bSJens Axboe 
27453b329bSJens Axboe bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
28453b329bSJens Axboe void io_free_file_tables(struct io_file_table *table);
29c98817e6SJens Axboe 
30c98817e6SJens Axboe int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
31c98817e6SJens Axboe 			struct file *file, unsigned int file_slot);
32453b329bSJens Axboe 
33a4ad4f74SJens Axboe unsigned int io_file_get_flags(struct file *file);
34a4ad4f74SJens Axboe 
35453b329bSJens Axboe static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
36453b329bSJens Axboe {
37453b329bSJens Axboe 	__clear_bit(bit, table->bitmap);
38453b329bSJens Axboe 	table->alloc_hint = bit;
39453b329bSJens Axboe }
40453b329bSJens Axboe 
41453b329bSJens Axboe static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
42453b329bSJens Axboe {
43453b329bSJens Axboe 	WARN_ON_ONCE(test_bit(bit, table->bitmap));
44453b329bSJens Axboe 	__set_bit(bit, table->bitmap);
45453b329bSJens Axboe 	table->alloc_hint = bit + 1;
46453b329bSJens Axboe }
47453b329bSJens Axboe 
48453b329bSJens Axboe static inline struct io_fixed_file *
49453b329bSJens Axboe io_fixed_file_slot(struct io_file_table *table, unsigned i)
50453b329bSJens Axboe {
51453b329bSJens Axboe 	return &table->files[i];
52453b329bSJens Axboe }
53453b329bSJens Axboe 
54a4ad4f74SJens Axboe static inline struct file *io_file_from_index(struct io_file_table *table,
55a4ad4f74SJens Axboe 					      int index)
56a4ad4f74SJens Axboe {
57a4ad4f74SJens Axboe 	struct io_fixed_file *slot = io_fixed_file_slot(table, index);
58a4ad4f74SJens Axboe 
59a4ad4f74SJens Axboe 	return (struct file *) (slot->file_ptr & FFS_MASK);
60a4ad4f74SJens Axboe }
61a4ad4f74SJens Axboe 
62a4ad4f74SJens Axboe static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
63a4ad4f74SJens Axboe 				     struct file *file)
64a4ad4f74SJens Axboe {
65a4ad4f74SJens Axboe 	unsigned long file_ptr = (unsigned long) file;
66a4ad4f74SJens Axboe 
67a4ad4f74SJens Axboe 	file_ptr |= io_file_get_flags(file);
68a4ad4f74SJens Axboe 	file_slot->file_ptr = file_ptr;
69a4ad4f74SJens Axboe }
70a4ad4f74SJens Axboe 
71453b329bSJens Axboe #endif
72