1453b329bSJens Axboe // SPDX-License-Identifier: GPL-2.0 2453b329bSJens Axboe #ifndef IOU_FILE_TABLE_H 3453b329bSJens Axboe #define IOU_FILE_TABLE_H 4453b329bSJens Axboe 5024f15e0SPavel Begunkov #include <linux/file.h> 6024f15e0SPavel Begunkov 7453b329bSJens Axboe struct io_ring_ctx; 8c98817e6SJens Axboe struct io_kiocb; 9453b329bSJens Axboe 10453b329bSJens Axboe /* 11453b329bSJens Axboe * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0 12453b329bSJens Axboe * and define IO_URING_SCM_ALL. For this case, we use SCM for all files as we 13453b329bSJens Axboe * can't safely always dereference the file when the task has exited and ring 14453b329bSJens Axboe * cleanup is done. If a file is tracked and part of SCM, then unix gc on 15453b329bSJens Axboe * process exit may reap it before __io_sqe_files_unregister() is run. 16453b329bSJens Axboe */ 17453b329bSJens Axboe #define FFS_NOWAIT 0x1UL 18453b329bSJens Axboe #define FFS_ISREG 0x2UL 19453b329bSJens Axboe #if defined(CONFIG_64BIT) 20453b329bSJens Axboe #define FFS_SCM 0x4UL 21453b329bSJens Axboe #else 22453b329bSJens Axboe #define IO_URING_SCM_ALL 23453b329bSJens Axboe #define FFS_SCM 0x0UL 24453b329bSJens Axboe #endif 25453b329bSJens Axboe #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG|FFS_SCM) 26453b329bSJens Axboe 27453b329bSJens Axboe bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files); 28453b329bSJens Axboe void io_free_file_tables(struct io_file_table *table); 29c98817e6SJens Axboe 30c98817e6SJens Axboe int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags, 31c98817e6SJens Axboe struct file *file, unsigned int file_slot); 32*f110ed84SJens Axboe int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file, 33*f110ed84SJens Axboe unsigned int file_slot); 34*f110ed84SJens Axboe int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset); 35453b329bSJens Axboe 36a4ad4f74SJens Axboe unsigned int io_file_get_flags(struct file *file); 37a4ad4f74SJens Axboe 38453b329bSJens Axboe static inline void io_file_bitmap_clear(struct io_file_table *table, int bit) 39453b329bSJens Axboe { 40453b329bSJens Axboe __clear_bit(bit, table->bitmap); 41453b329bSJens Axboe table->alloc_hint = bit; 42453b329bSJens Axboe } 43453b329bSJens Axboe 44453b329bSJens Axboe static inline void io_file_bitmap_set(struct io_file_table *table, int bit) 45453b329bSJens Axboe { 46453b329bSJens Axboe WARN_ON_ONCE(test_bit(bit, table->bitmap)); 47453b329bSJens Axboe __set_bit(bit, table->bitmap); 48453b329bSJens Axboe table->alloc_hint = bit + 1; 49453b329bSJens Axboe } 50453b329bSJens Axboe 51453b329bSJens Axboe static inline struct io_fixed_file * 52453b329bSJens Axboe io_fixed_file_slot(struct io_file_table *table, unsigned i) 53453b329bSJens Axboe { 54453b329bSJens Axboe return &table->files[i]; 55453b329bSJens Axboe } 56453b329bSJens Axboe 57a4ad4f74SJens Axboe static inline struct file *io_file_from_index(struct io_file_table *table, 58a4ad4f74SJens Axboe int index) 59a4ad4f74SJens Axboe { 60a4ad4f74SJens Axboe struct io_fixed_file *slot = io_fixed_file_slot(table, index); 61a4ad4f74SJens Axboe 62a4ad4f74SJens Axboe return (struct file *) (slot->file_ptr & FFS_MASK); 63a4ad4f74SJens Axboe } 64a4ad4f74SJens Axboe 65a4ad4f74SJens Axboe static inline void io_fixed_file_set(struct io_fixed_file *file_slot, 66a4ad4f74SJens Axboe struct file *file) 67a4ad4f74SJens Axboe { 68a4ad4f74SJens Axboe unsigned long file_ptr = (unsigned long) file; 69a4ad4f74SJens Axboe 70a4ad4f74SJens Axboe file_ptr |= io_file_get_flags(file); 71a4ad4f74SJens Axboe file_slot->file_ptr = file_ptr; 72a4ad4f74SJens Axboe } 73a4ad4f74SJens Axboe 74453b329bSJens Axboe #endif 75