xref: /openbmc/linux/io_uring/filetable.h (revision 024f15e0)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef IOU_FILE_TABLE_H
3 #define IOU_FILE_TABLE_H
4 
5 #include <linux/file.h>
6 
7 struct io_ring_ctx;
8 struct io_kiocb;
9 
10 /*
11  * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0
12  * and define IO_URING_SCM_ALL. For this case, we use SCM for all files as we
13  * can't safely always dereference the file when the task has exited and ring
14  * cleanup is done. If a file is tracked and part of SCM, then unix gc on
15  * process exit may reap it before __io_sqe_files_unregister() is run.
16  */
17 #define FFS_NOWAIT		0x1UL
18 #define FFS_ISREG		0x2UL
19 #if defined(CONFIG_64BIT)
20 #define FFS_SCM			0x4UL
21 #else
22 #define IO_URING_SCM_ALL
23 #define FFS_SCM			0x0UL
24 #endif
25 #define FFS_MASK		~(FFS_NOWAIT|FFS_ISREG|FFS_SCM)
26 
27 bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
28 void io_free_file_tables(struct io_file_table *table);
29 
30 int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
31 			struct file *file, unsigned int file_slot);
32 
33 unsigned int io_file_get_flags(struct file *file);
34 
35 static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
36 {
37 	__clear_bit(bit, table->bitmap);
38 	table->alloc_hint = bit;
39 }
40 
41 static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
42 {
43 	WARN_ON_ONCE(test_bit(bit, table->bitmap));
44 	__set_bit(bit, table->bitmap);
45 	table->alloc_hint = bit + 1;
46 }
47 
48 static inline struct io_fixed_file *
49 io_fixed_file_slot(struct io_file_table *table, unsigned i)
50 {
51 	return &table->files[i];
52 }
53 
54 static inline struct file *io_file_from_index(struct io_file_table *table,
55 					      int index)
56 {
57 	struct io_fixed_file *slot = io_fixed_file_slot(table, index);
58 
59 	return (struct file *) (slot->file_ptr & FFS_MASK);
60 }
61 
62 static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
63 				     struct file *file)
64 {
65 	unsigned long file_ptr = (unsigned long) file;
66 
67 	file_ptr |= io_file_get_flags(file);
68 	file_slot->file_ptr = file_ptr;
69 }
70 
71 #endif
72