1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef IOU_FILE_TABLE_H
3 #define IOU_FILE_TABLE_H
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.
14 #define FFS_NOWAIT 0x1UL
15 #define FFS_ISREG 0x2UL
16 #if defined(CONFIG_64BIT)
19 #define IO_URING_SCM_ALL
22 #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG|FFS_SCM)
24 struct io_fixed_file {
25 /* file * with additional FFS_* flags */
26 unsigned long file_ptr;
29 struct io_file_table {
30 struct io_fixed_file *files;
31 unsigned long *bitmap;
32 unsigned int alloc_hint;
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);
39 unsigned int io_file_get_flags(struct file *file);
41 static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
43 __clear_bit(bit, table->bitmap);
44 table->alloc_hint = bit;
47 static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
49 WARN_ON_ONCE(test_bit(bit, table->bitmap));
50 __set_bit(bit, table->bitmap);
51 table->alloc_hint = bit + 1;
54 static inline struct io_fixed_file *
55 io_fixed_file_slot(struct io_file_table *table, unsigned i)
57 return &table->files[i];
60 static inline struct file *io_file_from_index(struct io_file_table *table,
63 struct io_fixed_file *slot = io_fixed_file_slot(table, index);
65 return (struct file *) (slot->file_ptr & FFS_MASK);
68 static inline void io_fixed_file_set(struct io_fixed_file *file_slot,
71 unsigned long file_ptr = (unsigned long) file;
73 file_ptr |= io_file_get_flags(file);
74 file_slot->file_ptr = file_ptr;