epoll.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/fs.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/io_uring.h>
  8. #include <linux/eventpoll.h>
  9. #include <uapi/linux/io_uring.h>
  10. #include "io_uring.h"
  11. #include "epoll.h"
  12. #if defined(CONFIG_EPOLL)
  13. struct io_epoll {
  14. struct file *file;
  15. int epfd;
  16. int op;
  17. int fd;
  18. struct epoll_event event;
  19. };
  20. int io_epoll_ctl_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  21. {
  22. struct io_epoll *epoll = io_kiocb_to_cmd(req, struct io_epoll);
  23. if (sqe->buf_index || sqe->splice_fd_in)
  24. return -EINVAL;
  25. epoll->epfd = READ_ONCE(sqe->fd);
  26. epoll->op = READ_ONCE(sqe->len);
  27. epoll->fd = READ_ONCE(sqe->off);
  28. if (ep_op_has_event(epoll->op)) {
  29. struct epoll_event __user *ev;
  30. ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
  31. if (copy_from_user(&epoll->event, ev, sizeof(*ev)))
  32. return -EFAULT;
  33. }
  34. return 0;
  35. }
  36. int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
  37. {
  38. struct io_epoll *ie = io_kiocb_to_cmd(req, struct io_epoll);
  39. int ret;
  40. bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
  41. ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
  42. if (force_nonblock && ret == -EAGAIN)
  43. return -EAGAIN;
  44. if (ret < 0)
  45. req_set_fail(req);
  46. io_req_set_res(req, ret, 0);
  47. return IOU_OK;
  48. }
  49. #endif