summaryrefslogtreecommitdiff
path: root/io_uring/filetable.c
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2022-05-24 21:43:10 -0600
committerJens Axboe <axboe@kernel.dk>2022-07-24 18:39:11 -0600
commit453b329be5eacfc48dd43035af82bc7f28ecfedf (patch)
tree072ae9d9fd40554f65537c92cc1e9116b5dfe79f /io_uring/filetable.c
parentf4c163dd7d4b1031772317cd3cd58dd6711ee51e (diff)
downloadlwn-453b329be5eacfc48dd43035af82bc7f28ecfedf.tar.gz
lwn-453b329be5eacfc48dd43035af82bc7f28ecfedf.zip
io_uring: separate out file table handling code
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'io_uring/filetable.c')
-rw-r--r--io_uring/filetable.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/io_uring/filetable.c b/io_uring/filetable.c
new file mode 100644
index 000000000000..560629a93c04
--- /dev/null
+++ b/io_uring/filetable.c
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/file.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/io_uring.h>
+
+#include <uapi/linux/io_uring.h>
+
+#include "io_uring_types.h"
+#include "io_uring.h"
+
+int io_file_bitmap_get(struct io_ring_ctx *ctx)
+{
+ struct io_file_table *table = &ctx->file_table;
+ unsigned long nr = ctx->nr_user_files;
+ int ret;
+
+ do {
+ ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
+ if (ret != nr)
+ return ret;
+
+ if (!table->alloc_hint)
+ break;
+
+ nr = table->alloc_hint;
+ table->alloc_hint = 0;
+ } while (1);
+
+ return -ENFILE;
+}
+
+bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
+{
+ table->files = kvcalloc(nr_files, sizeof(table->files[0]),
+ GFP_KERNEL_ACCOUNT);
+ if (unlikely(!table->files))
+ return false;
+
+ table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
+ if (unlikely(!table->bitmap)) {
+ kvfree(table->files);
+ return false;
+ }
+
+ return true;
+}
+
+void io_free_file_tables(struct io_file_table *table)
+{
+ kvfree(table->files);
+ bitmap_free(table->bitmap);
+ table->files = NULL;
+ table->bitmap = NULL;
+}