aio.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef __LINUX__AIO_H
  2. #define __LINUX__AIO_H
  3. #include <linux/list.h>
  4. #include <linux/workqueue.h>
  5. #include <linux/aio_abi.h>
  6. #include <linux/uio.h>
  7. #include <linux/rcupdate.h>
  8. #include <linux/atomic.h>
  9. struct kioctx;
  10. struct kiocb;
  11. #define KIOCB_SYNC_KEY (~0U)
  12. /*
  13. * We use ki_cancel == KIOCB_CANCELLED to indicate that a kiocb has been either
  14. * cancelled or completed (this makes a certain amount of sense because
  15. * successful cancellation - io_cancel() - does deliver the completion to
  16. * userspace).
  17. *
  18. * And since most things don't implement kiocb cancellation and we'd really like
  19. * kiocb completion to be lockless when possible, we use ki_cancel to
  20. * synchronize cancellation and completion - we only set it to KIOCB_CANCELLED
  21. * with xchg() or cmpxchg(), see batch_complete_aio() and kiocb_cancel().
  22. */
  23. #define KIOCB_CANCELLED ((void *) (~0ULL))
  24. typedef int (kiocb_cancel_fn)(struct kiocb *, struct io_event *);
  25. /* is there a better place to document function pointer methods? */
  26. /**
  27. * ki_retry - iocb forward progress callback
  28. * @kiocb: The kiocb struct to advance by performing an operation.
  29. *
  30. * This callback is called when the AIO core wants a given AIO operation
  31. * to make forward progress. The kiocb argument describes the operation
  32. * that is to be performed. As the operation proceeds, perhaps partially,
  33. * ki_retry is expected to update the kiocb with progress made. Typically
  34. * ki_retry is set in the AIO core and it itself calls file_operations
  35. * helpers.
  36. *
  37. * ki_retry's return value determines when the AIO operation is completed
  38. * and an event is generated in the AIO event ring. Except the special
  39. * return values described below, the value that is returned from ki_retry
  40. * is transferred directly into the completion ring as the operation's
  41. * resulting status. Once this has happened ki_retry *MUST NOT* reference
  42. * the kiocb pointer again.
  43. *
  44. * If ki_retry returns -EIOCBQUEUED it has made a promise that aio_complete()
  45. * will be called on the kiocb pointer in the future. The AIO core will
  46. * not ask the method again -- ki_retry must ensure forward progress.
  47. * aio_complete() must be called once and only once in the future, multiple
  48. * calls may result in undefined behaviour.
  49. */
  50. struct kiocb {
  51. atomic_t ki_users;
  52. unsigned ki_key; /* id of this request */
  53. struct file *ki_filp;
  54. struct kioctx *ki_ctx; /* may be NULL for sync ops */
  55. kiocb_cancel_fn *ki_cancel;
  56. ssize_t (*ki_retry)(struct kiocb *);
  57. void (*ki_dtor)(struct kiocb *);
  58. union {
  59. void __user *user;
  60. struct task_struct *tsk;
  61. } ki_obj;
  62. __u64 ki_user_data; /* user's data for completion */
  63. loff_t ki_pos;
  64. void *private;
  65. /* State that we remember to be able to restart/retry */
  66. unsigned short ki_opcode;
  67. size_t ki_nbytes; /* copy of iocb->aio_nbytes */
  68. char __user *ki_buf; /* remaining iocb->aio_buf */
  69. size_t ki_left; /* remaining bytes */
  70. struct iovec ki_inline_vec; /* inline vector */
  71. struct iovec *ki_iovec;
  72. unsigned long ki_nr_segs;
  73. unsigned long ki_cur_seg;
  74. struct list_head ki_list; /* the aio core uses this
  75. * for cancellation */
  76. struct list_head ki_batch; /* batch allocation */
  77. /*
  78. * If the aio_resfd field of the userspace iocb is not zero,
  79. * this is the underlying eventfd context to deliver events to.
  80. */
  81. struct eventfd_ctx *ki_eventfd;
  82. };
  83. static inline bool is_sync_kiocb(struct kiocb *kiocb)
  84. {
  85. return kiocb->ki_key == KIOCB_SYNC_KEY;
  86. }
  87. static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
  88. {
  89. *kiocb = (struct kiocb) {
  90. .ki_users = ATOMIC_INIT(1),
  91. .ki_key = KIOCB_SYNC_KEY,
  92. .ki_filp = filp,
  93. .ki_obj.tsk = current,
  94. };
  95. }
  96. /* prototypes */
  97. #ifdef CONFIG_AIO
  98. extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
  99. extern void aio_put_req(struct kiocb *iocb);
  100. extern void aio_complete(struct kiocb *iocb, long res, long res2);
  101. struct mm_struct;
  102. extern void exit_aio(struct mm_struct *mm);
  103. extern long do_io_submit(aio_context_t ctx_id, long nr,
  104. struct iocb __user *__user *iocbpp, bool compat);
  105. void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel);
  106. #else
  107. static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
  108. static inline void aio_put_req(struct kiocb *iocb) { }
  109. static inline void aio_complete(struct kiocb *iocb, long res, long res2) { }
  110. struct mm_struct;
  111. static inline void exit_aio(struct mm_struct *mm) { }
  112. static inline long do_io_submit(aio_context_t ctx_id, long nr,
  113. struct iocb __user * __user *iocbpp,
  114. bool compat) { return 0; }
  115. static inline void kiocb_set_cancel_fn(struct kiocb *req,
  116. kiocb_cancel_fn *cancel) { }
  117. #endif /* CONFIG_AIO */
  118. static inline struct kiocb *list_kiocb(struct list_head *h)
  119. {
  120. return list_entry(h, struct kiocb, ki_list);
  121. }
  122. /* for sysctl: */
  123. extern unsigned long aio_nr;
  124. extern unsigned long aio_max_nr;
  125. #endif /* __LINUX__AIO_H */