io_uring: separate out file table handling code
[linux-block.git] / io_uring / filetable.h
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 static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
40 {
41         __clear_bit(bit, table->bitmap);
42         table->alloc_hint = bit;
43 }
44
45 static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
46 {
47         WARN_ON_ONCE(test_bit(bit, table->bitmap));
48         __set_bit(bit, table->bitmap);
49         table->alloc_hint = bit + 1;
50 }
51
52 static inline struct io_fixed_file *
53 io_fixed_file_slot(struct io_file_table *table, unsigned i)
54 {
55         return &table->files[i];
56 }
57
58 #endif