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