aio.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #define KIOCB_SYNC_KEY (~0U)
  11. /* ki_flags bits */
  12. #define KIF_CANCELLED 2
  13. #define kiocbSetCancelled(iocb) set_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  14. #define kiocbClearCancelled(iocb) clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  15. #define kiocbIsCancelled(iocb) test_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  16. /* is there a better place to document function pointer methods? */
  17. /**
  18. * ki_retry - iocb forward progress callback
  19. * @kiocb: The kiocb struct to advance by performing an operation.
  20. *
  21. * This callback is called when the AIO core wants a given AIO operation
  22. * to make forward progress. The kiocb argument describes the operation
  23. * that is to be performed. As the operation proceeds, perhaps partially,
  24. * ki_retry is expected to update the kiocb with progress made. Typically
  25. * ki_retry is set in the AIO core and it itself calls file_operations
  26. * helpers.
  27. *
  28. * ki_retry's return value determines when the AIO operation is completed
  29. * and an event is generated in the AIO event ring. Except the special
  30. * return values described below, the value that is returned from ki_retry
  31. * is transferred directly into the completion ring as the operation's
  32. * resulting status. Once this has happened ki_retry *MUST NOT* reference
  33. * the kiocb pointer again.
  34. *
  35. * If ki_retry returns -EIOCBQUEUED it has made a promise that aio_complete()
  36. * will be called on the kiocb pointer in the future. The AIO core will
  37. * not ask the method again -- ki_retry must ensure forward progress.
  38. * aio_complete() must be called once and only once in the future, multiple
  39. * calls may result in undefined behaviour.
  40. */
  41. struct kiocb {
  42. unsigned long ki_flags;
  43. int ki_users;
  44. unsigned ki_key; /* id of this request */
  45. struct file *ki_filp;
  46. struct kioctx *ki_ctx; /* may be NULL for sync ops */
  47. int (*ki_cancel)(struct kiocb *, struct io_event *);
  48. ssize_t (*ki_retry)(struct kiocb *);
  49. void (*ki_dtor)(struct kiocb *);
  50. union {
  51. void __user *user;
  52. struct task_struct *tsk;
  53. } ki_obj;
  54. __u64 ki_user_data; /* user's data for completion */
  55. loff_t ki_pos;
  56. void *private;
  57. /* State that we remember to be able to restart/retry */
  58. unsigned short ki_opcode;
  59. size_t ki_nbytes; /* copy of iocb->aio_nbytes */
  60. char __user *ki_buf; /* remaining iocb->aio_buf */
  61. size_t ki_left; /* remaining bytes */
  62. struct iovec ki_inline_vec; /* inline vector */
  63. struct iovec *ki_iovec;
  64. unsigned long ki_nr_segs;
  65. unsigned long ki_cur_seg;
  66. struct list_head ki_list; /* the aio core uses this
  67. * for cancellation */
  68. struct list_head ki_batch; /* batch allocation */
  69. /*
  70. * If the aio_resfd field of the userspace iocb is not zero,
  71. * this is the underlying eventfd context to deliver events to.
  72. */
  73. struct eventfd_ctx *ki_eventfd;
  74. };
  75. static inline bool is_sync_kiocb(struct kiocb *kiocb)
  76. {
  77. return kiocb->ki_key == KIOCB_SYNC_KEY;
  78. }
  79. static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
  80. {
  81. *kiocb = (struct kiocb) {
  82. .ki_users = 1,
  83. .ki_key = KIOCB_SYNC_KEY,
  84. .ki_filp = filp,
  85. .ki_obj.tsk = current,
  86. };
  87. }
  88. #define AIO_RING_MAGIC 0xa10a10a1
  89. #define AIO_RING_COMPAT_FEATURES 1
  90. #define AIO_RING_INCOMPAT_FEATURES 0
  91. struct aio_ring {
  92. unsigned id; /* kernel internal index number */
  93. unsigned nr; /* number of io_events */
  94. unsigned head;
  95. unsigned tail;
  96. unsigned magic;
  97. unsigned compat_features;
  98. unsigned incompat_features;
  99. unsigned header_length; /* size of aio_ring */
  100. struct io_event io_events[0];
  101. }; /* 128 bytes + ring size */
  102. #define AIO_RING_PAGES 8
  103. struct aio_ring_info {
  104. unsigned long mmap_base;
  105. unsigned long mmap_size;
  106. struct page **ring_pages;
  107. spinlock_t ring_lock;
  108. long nr_pages;
  109. unsigned nr, tail;
  110. struct page *internal_pages[AIO_RING_PAGES];
  111. };
  112. static inline unsigned aio_ring_avail(struct aio_ring_info *info,
  113. struct aio_ring *ring)
  114. {
  115. return (ring->head + info->nr - 1 - ring->tail) % info->nr;
  116. }
  117. struct kioctx {
  118. atomic_t users;
  119. int dead;
  120. /* This needs improving */
  121. unsigned long user_id;
  122. struct hlist_node list;
  123. wait_queue_head_t wait;
  124. spinlock_t ctx_lock;
  125. int reqs_active;
  126. struct list_head active_reqs; /* used for cancellation */
  127. /* sys_io_setup currently limits this to an unsigned int */
  128. unsigned max_reqs;
  129. struct aio_ring_info ring_info;
  130. struct rcu_head rcu_head;
  131. };
  132. /* prototypes */
  133. #ifdef CONFIG_AIO
  134. extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
  135. extern void aio_put_req(struct kiocb *iocb);
  136. extern void aio_complete(struct kiocb *iocb, long res, long res2);
  137. struct mm_struct;
  138. extern void exit_aio(struct mm_struct *mm);
  139. extern long do_io_submit(aio_context_t ctx_id, long nr,
  140. struct iocb __user *__user *iocbpp, bool compat);
  141. #else
  142. static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
  143. static inline void aio_put_req(struct kiocb *iocb) { }
  144. static inline void aio_complete(struct kiocb *iocb, long res, long res2) { }
  145. struct mm_struct;
  146. static inline void exit_aio(struct mm_struct *mm) { }
  147. static inline long do_io_submit(aio_context_t ctx_id, long nr,
  148. struct iocb __user * __user *iocbpp,
  149. bool compat) { return 0; }
  150. #endif /* CONFIG_AIO */
  151. static inline struct kiocb *list_kiocb(struct list_head *h)
  152. {
  153. return list_entry(h, struct kiocb, ki_list);
  154. }
  155. /* for sysctl: */
  156. extern unsigned long aio_nr;
  157. extern unsigned long aio_max_nr;
  158. #endif /* __LINUX__AIO_H */