filetable.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/file.h>
  5. #include <linux/mm.h>
  6. #include <linux/slab.h>
  7. #include <linux/nospec.h>
  8. #include <linux/io_uring.h>
  9. #include <uapi/linux/io_uring.h>
  10. #include "io_uring.h"
  11. #include "rsrc.h"
  12. #include "filetable.h"
  13. static int io_file_bitmap_get(struct io_ring_ctx *ctx)
  14. {
  15. struct io_file_table *table = &ctx->file_table;
  16. unsigned long nr = ctx->file_alloc_end;
  17. int ret;
  18. if (!table->bitmap)
  19. return -ENFILE;
  20. if (table->alloc_hint < ctx->file_alloc_start ||
  21. table->alloc_hint >= ctx->file_alloc_end)
  22. table->alloc_hint = ctx->file_alloc_start;
  23. do {
  24. ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
  25. if (ret != nr)
  26. return ret;
  27. if (table->alloc_hint == ctx->file_alloc_start)
  28. break;
  29. nr = table->alloc_hint;
  30. table->alloc_hint = ctx->file_alloc_start;
  31. } while (1);
  32. return -ENFILE;
  33. }
  34. bool io_alloc_file_tables(struct io_ring_ctx *ctx, struct io_file_table *table,
  35. unsigned nr_files)
  36. {
  37. if (io_rsrc_data_alloc(&table->data, nr_files))
  38. return false;
  39. table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);
  40. if (table->bitmap)
  41. return true;
  42. io_rsrc_data_free(ctx, &table->data);
  43. return false;
  44. }
  45. void io_free_file_tables(struct io_ring_ctx *ctx, struct io_file_table *table)
  46. {
  47. io_rsrc_data_free(ctx, &table->data);
  48. bitmap_free(table->bitmap);
  49. table->bitmap = NULL;
  50. }
  51. static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
  52. u32 slot_index)
  53. __must_hold(&ctx->uring_lock)
  54. {
  55. struct io_rsrc_node *node;
  56. if (io_is_uring_fops(file))
  57. return -EBADF;
  58. if (!ctx->file_table.data.nr)
  59. return -ENXIO;
  60. if (slot_index >= ctx->file_table.data.nr)
  61. return -EINVAL;
  62. node = io_rsrc_node_alloc(ctx, IORING_RSRC_FILE);
  63. if (!node)
  64. return -ENOMEM;
  65. if (!io_reset_rsrc_node(ctx, &ctx->file_table.data, slot_index))
  66. io_file_bitmap_set(&ctx->file_table, slot_index);
  67. ctx->file_table.data.nodes[slot_index] = node;
  68. io_fixed_file_set(node, file);
  69. return 0;
  70. }
  71. int __io_fixed_fd_install(struct io_ring_ctx *ctx, struct file *file,
  72. unsigned int file_slot)
  73. {
  74. bool alloc_slot = file_slot == IORING_FILE_INDEX_ALLOC;
  75. int ret;
  76. if (alloc_slot) {
  77. ret = io_file_bitmap_get(ctx);
  78. if (unlikely(ret < 0))
  79. return ret;
  80. file_slot = ret;
  81. } else {
  82. file_slot--;
  83. }
  84. ret = io_install_fixed_file(ctx, file, file_slot);
  85. if (!ret && alloc_slot)
  86. ret = file_slot;
  87. return ret;
  88. }
  89. /*
  90. * Note when io_fixed_fd_install() returns error value, it will ensure
  91. * fput() is called correspondingly.
  92. */
  93. int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
  94. struct file *file, unsigned int file_slot)
  95. {
  96. struct io_ring_ctx *ctx = req->ctx;
  97. int ret;
  98. io_ring_submit_lock(ctx, issue_flags);
  99. ret = __io_fixed_fd_install(ctx, file, file_slot);
  100. io_ring_submit_unlock(ctx, issue_flags);
  101. if (unlikely(ret < 0))
  102. fput(file);
  103. return ret;
  104. }
  105. int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
  106. {
  107. struct io_rsrc_node *node;
  108. if (unlikely(!ctx->file_table.data.nr))
  109. return -ENXIO;
  110. if (offset >= ctx->file_table.data.nr)
  111. return -EINVAL;
  112. node = io_rsrc_node_lookup(&ctx->file_table.data, offset);
  113. if (!node)
  114. return -EBADF;
  115. io_reset_rsrc_node(ctx, &ctx->file_table.data, offset);
  116. io_file_bitmap_clear(&ctx->file_table, offset);
  117. return 0;
  118. }
  119. int io_register_file_alloc_range(struct io_ring_ctx *ctx,
  120. struct io_uring_file_index_range __user *arg)
  121. {
  122. struct io_uring_file_index_range range;
  123. u32 end;
  124. if (copy_from_user(&range, arg, sizeof(range)))
  125. return -EFAULT;
  126. if (check_add_overflow(range.off, range.len, &end))
  127. return -EOVERFLOW;
  128. if (range.resv || end > ctx->file_table.data.nr)
  129. return -EINVAL;
  130. io_file_table_set_alloc_range(ctx, range.off, range.len);
  131. return 0;
  132. }