aio.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <asm/atomic.h>
  7. #define AIO_MAXSEGS 4
  8. #define AIO_KIOGRP_NR_ATOMIC 8
  9. struct kioctx;
  10. /* Notes on cancelling a kiocb:
  11. * If a kiocb is cancelled, aio_complete may return 0 to indicate
  12. * that cancel has not yet disposed of the kiocb. All cancel
  13. * operations *must* call aio_put_req to dispose of the kiocb
  14. * to guard against races with the completion code.
  15. */
  16. #define KIOCB_C_CANCELLED 0x01
  17. #define KIOCB_C_COMPLETE 0x02
  18. #define KIOCB_SYNC_KEY (~0U)
  19. /* ki_flags bits */
  20. /*
  21. * This may be used for cancel/retry serialization in the future, but
  22. * for now it's unused and we probably don't want modules to even
  23. * think they can use it.
  24. */
  25. /* #define KIF_LOCKED 0 */
  26. #define KIF_KICKED 1
  27. #define KIF_CANCELLED 2
  28. #define kiocbTryLock(iocb) test_and_set_bit(KIF_LOCKED, &(iocb)->ki_flags)
  29. #define kiocbTryKick(iocb) test_and_set_bit(KIF_KICKED, &(iocb)->ki_flags)
  30. #define kiocbSetLocked(iocb) set_bit(KIF_LOCKED, &(iocb)->ki_flags)
  31. #define kiocbSetKicked(iocb) set_bit(KIF_KICKED, &(iocb)->ki_flags)
  32. #define kiocbSetCancelled(iocb) set_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  33. #define kiocbClearLocked(iocb) clear_bit(KIF_LOCKED, &(iocb)->ki_flags)
  34. #define kiocbClearKicked(iocb) clear_bit(KIF_KICKED, &(iocb)->ki_flags)
  35. #define kiocbClearCancelled(iocb) clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  36. #define kiocbIsLocked(iocb) test_bit(KIF_LOCKED, &(iocb)->ki_flags)
  37. #define kiocbIsKicked(iocb) test_bit(KIF_KICKED, &(iocb)->ki_flags)
  38. #define kiocbIsCancelled(iocb) test_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  39. /* is there a better place to document function pointer methods? */
  40. /**
  41. * ki_retry - iocb forward progress callback
  42. * @kiocb: The kiocb struct to advance by performing an operation.
  43. *
  44. * This callback is called when the AIO core wants a given AIO operation
  45. * to make forward progress. The kiocb argument describes the operation
  46. * that is to be performed. As the operation proceeds, perhaps partially,
  47. * ki_retry is expected to update the kiocb with progress made. Typically
  48. * ki_retry is set in the AIO core and it itself calls file_operations
  49. * helpers.
  50. *
  51. * ki_retry's return value determines when the AIO operation is completed
  52. * and an event is generated in the AIO event ring. Except the special
  53. * return values described below, the value that is returned from ki_retry
  54. * is transferred directly into the completion ring as the operation's
  55. * resulting status. Once this has happened ki_retry *MUST NOT* reference
  56. * the kiocb pointer again.
  57. *
  58. * If ki_retry returns -EIOCBQUEUED it has made a promise that aio_complete()
  59. * will be called on the kiocb pointer in the future. The AIO core will
  60. * not ask the method again -- ki_retry must ensure forward progress.
  61. * aio_complete() must be called once and only once in the future, multiple
  62. * calls may result in undefined behaviour.
  63. *
  64. * If ki_retry returns -EIOCBRETRY it has made a promise that kick_iocb()
  65. * will be called on the kiocb pointer in the future. This may happen
  66. * through generic helpers that associate kiocb->ki_wait with a wait
  67. * queue head that ki_retry uses via current->io_wait. It can also happen
  68. * with custom tracking and manual calls to kick_iocb(), though that is
  69. * discouraged. In either case, kick_iocb() must be called once and only
  70. * once. ki_retry must ensure forward progress, the AIO core will wait
  71. * indefinitely for kick_iocb() to be called.
  72. */
  73. struct kiocb {
  74. struct list_head ki_run_list;
  75. long ki_flags;
  76. int ki_users;
  77. unsigned ki_key; /* id of this request */
  78. struct file *ki_filp;
  79. struct kioctx *ki_ctx; /* may be NULL for sync ops */
  80. int (*ki_cancel)(struct kiocb *, struct io_event *);
  81. ssize_t (*ki_retry)(struct kiocb *);
  82. void (*ki_dtor)(struct kiocb *);
  83. struct list_head ki_list; /* the aio core uses this
  84. * for cancellation */
  85. union {
  86. void __user *user;
  87. struct task_struct *tsk;
  88. } ki_obj;
  89. __u64 ki_user_data; /* user's data for completion */
  90. loff_t ki_pos;
  91. /* State that we remember to be able to restart/retry */
  92. unsigned short ki_opcode;
  93. size_t ki_nbytes; /* copy of iocb->aio_nbytes */
  94. char __user *ki_buf; /* remaining iocb->aio_buf */
  95. size_t ki_left; /* remaining bytes */
  96. wait_queue_t ki_wait;
  97. long ki_retried; /* just for testing */
  98. long ki_kicked; /* just for testing */
  99. long ki_queued; /* just for testing */
  100. void *private;
  101. };
  102. #define is_sync_kiocb(iocb) ((iocb)->ki_key == KIOCB_SYNC_KEY)
  103. #define init_sync_kiocb(x, filp) \
  104. do { \
  105. struct task_struct *tsk = current; \
  106. (x)->ki_flags = 0; \
  107. (x)->ki_users = 1; \
  108. (x)->ki_key = KIOCB_SYNC_KEY; \
  109. (x)->ki_filp = (filp); \
  110. (x)->ki_ctx = &tsk->active_mm->default_kioctx; \
  111. (x)->ki_cancel = NULL; \
  112. (x)->ki_dtor = NULL; \
  113. (x)->ki_obj.tsk = tsk; \
  114. (x)->ki_user_data = 0; \
  115. init_wait((&(x)->ki_wait)); \
  116. } while (0)
  117. #define AIO_RING_MAGIC 0xa10a10a1
  118. #define AIO_RING_COMPAT_FEATURES 1
  119. #define AIO_RING_INCOMPAT_FEATURES 0
  120. struct aio_ring {
  121. unsigned id; /* kernel internal index number */
  122. unsigned nr; /* number of io_events */
  123. unsigned head;
  124. unsigned tail;
  125. unsigned magic;
  126. unsigned compat_features;
  127. unsigned incompat_features;
  128. unsigned header_length; /* size of aio_ring */
  129. struct io_event io_events[0];
  130. }; /* 128 bytes + ring size */
  131. #define aio_ring_avail(info, ring) (((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr)
  132. #define AIO_RING_PAGES 8
  133. struct aio_ring_info {
  134. unsigned long mmap_base;
  135. unsigned long mmap_size;
  136. struct page **ring_pages;
  137. spinlock_t ring_lock;
  138. long nr_pages;
  139. unsigned nr, tail;
  140. struct page *internal_pages[AIO_RING_PAGES];
  141. };
  142. struct kioctx {
  143. atomic_t users;
  144. int dead;
  145. struct mm_struct *mm;
  146. /* This needs improving */
  147. unsigned long user_id;
  148. struct kioctx *next;
  149. wait_queue_head_t wait;
  150. spinlock_t ctx_lock;
  151. int reqs_active;
  152. struct list_head active_reqs; /* used for cancellation */
  153. struct list_head run_list; /* used for kicked reqs */
  154. unsigned max_reqs;
  155. struct aio_ring_info ring_info;
  156. struct work_struct wq;
  157. };
  158. /* prototypes */
  159. extern unsigned aio_max_size;
  160. extern ssize_t FASTCALL(wait_on_sync_kiocb(struct kiocb *iocb));
  161. extern int FASTCALL(aio_put_req(struct kiocb *iocb));
  162. extern void FASTCALL(kick_iocb(struct kiocb *iocb));
  163. extern int FASTCALL(aio_complete(struct kiocb *iocb, long res, long res2));
  164. extern void FASTCALL(__put_ioctx(struct kioctx *ctx));
  165. struct mm_struct;
  166. extern void FASTCALL(exit_aio(struct mm_struct *mm));
  167. extern struct kioctx *lookup_ioctx(unsigned long ctx_id);
  168. extern int FASTCALL(io_submit_one(struct kioctx *ctx,
  169. struct iocb __user *user_iocb, struct iocb *iocb));
  170. /* semi private, but used by the 32bit emulations: */
  171. struct kioctx *lookup_ioctx(unsigned long ctx_id);
  172. int FASTCALL(io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  173. struct iocb *iocb));
  174. #define get_ioctx(kioctx) do { if (unlikely(atomic_read(&(kioctx)->users) <= 0)) BUG(); atomic_inc(&(kioctx)->users); } while (0)
  175. #define put_ioctx(kioctx) do { if (unlikely(atomic_dec_and_test(&(kioctx)->users))) __put_ioctx(kioctx); else if (unlikely(atomic_read(&(kioctx)->users) < 0)) BUG(); } while (0)
  176. #define in_aio() !is_sync_wait(current->io_wait)
  177. /* may be used for debugging */
  178. #define warn_if_async() \
  179. do { \
  180. if (in_aio()) { \
  181. printk(KERN_ERR "%s(%s:%d) called in async context!\n", \
  182. __FUNCTION__, __FILE__, __LINE__); \
  183. dump_stack(); \
  184. } \
  185. } while (0)
  186. #define io_wait_to_kiocb(wait) container_of(wait, struct kiocb, ki_wait)
  187. #define is_retried_kiocb(iocb) ((iocb)->ki_retried > 1)
  188. #include <linux/aio_abi.h>
  189. static inline struct kiocb *list_kiocb(struct list_head *h)
  190. {
  191. return list_entry(h, struct kiocb, ki_list);
  192. }
  193. /* for sysctl: */
  194. extern atomic_t aio_nr;
  195. extern unsigned aio_max_nr;
  196. #endif /* __LINUX__AIO_H */