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