aio.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #define KIF_LOCKED 0
  21. #define KIF_KICKED 1
  22. #define KIF_CANCELLED 2
  23. #define kiocbTryLock(iocb) test_and_set_bit(KIF_LOCKED, &(iocb)->ki_flags)
  24. #define kiocbTryKick(iocb) test_and_set_bit(KIF_KICKED, &(iocb)->ki_flags)
  25. #define kiocbSetLocked(iocb) set_bit(KIF_LOCKED, &(iocb)->ki_flags)
  26. #define kiocbSetKicked(iocb) set_bit(KIF_KICKED, &(iocb)->ki_flags)
  27. #define kiocbSetCancelled(iocb) set_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  28. #define kiocbClearLocked(iocb) clear_bit(KIF_LOCKED, &(iocb)->ki_flags)
  29. #define kiocbClearKicked(iocb) clear_bit(KIF_KICKED, &(iocb)->ki_flags)
  30. #define kiocbClearCancelled(iocb) clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  31. #define kiocbIsLocked(iocb) test_bit(KIF_LOCKED, &(iocb)->ki_flags)
  32. #define kiocbIsKicked(iocb) test_bit(KIF_KICKED, &(iocb)->ki_flags)
  33. #define kiocbIsCancelled(iocb) test_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  34. struct kiocb {
  35. struct list_head ki_run_list;
  36. long ki_flags;
  37. int ki_users;
  38. unsigned ki_key; /* id of this request */
  39. struct file *ki_filp;
  40. struct kioctx *ki_ctx; /* may be NULL for sync ops */
  41. int (*ki_cancel)(struct kiocb *, struct io_event *);
  42. ssize_t (*ki_retry)(struct kiocb *);
  43. void (*ki_dtor)(struct kiocb *);
  44. struct list_head ki_list; /* the aio core uses this
  45. * for cancellation */
  46. union {
  47. void __user *user;
  48. struct task_struct *tsk;
  49. } ki_obj;
  50. __u64 ki_user_data; /* user's data for completion */
  51. loff_t ki_pos;
  52. /* State that we remember to be able to restart/retry */
  53. unsigned short ki_opcode;
  54. size_t ki_nbytes; /* copy of iocb->aio_nbytes */
  55. char __user *ki_buf; /* remaining iocb->aio_buf */
  56. size_t ki_left; /* remaining bytes */
  57. wait_queue_t ki_wait;
  58. long ki_retried; /* just for testing */
  59. long ki_kicked; /* just for testing */
  60. long ki_queued; /* just for testing */
  61. void *private;
  62. };
  63. #define is_sync_kiocb(iocb) ((iocb)->ki_key == KIOCB_SYNC_KEY)
  64. #define init_sync_kiocb(x, filp) \
  65. do { \
  66. struct task_struct *tsk = current; \
  67. (x)->ki_flags = 0; \
  68. (x)->ki_users = 1; \
  69. (x)->ki_key = KIOCB_SYNC_KEY; \
  70. (x)->ki_filp = (filp); \
  71. (x)->ki_ctx = &tsk->active_mm->default_kioctx; \
  72. (x)->ki_cancel = NULL; \
  73. (x)->ki_dtor = NULL; \
  74. (x)->ki_obj.tsk = tsk; \
  75. (x)->ki_user_data = 0; \
  76. init_wait((&(x)->ki_wait)); \
  77. } while (0)
  78. #define AIO_RING_MAGIC 0xa10a10a1
  79. #define AIO_RING_COMPAT_FEATURES 1
  80. #define AIO_RING_INCOMPAT_FEATURES 0
  81. struct aio_ring {
  82. unsigned id; /* kernel internal index number */
  83. unsigned nr; /* number of io_events */
  84. unsigned head;
  85. unsigned tail;
  86. unsigned magic;
  87. unsigned compat_features;
  88. unsigned incompat_features;
  89. unsigned header_length; /* size of aio_ring */
  90. struct io_event io_events[0];
  91. }; /* 128 bytes + ring size */
  92. #define aio_ring_avail(info, ring) (((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr)
  93. #define AIO_RING_PAGES 8
  94. struct aio_ring_info {
  95. unsigned long mmap_base;
  96. unsigned long mmap_size;
  97. struct page **ring_pages;
  98. spinlock_t ring_lock;
  99. long nr_pages;
  100. unsigned nr, tail;
  101. struct page *internal_pages[AIO_RING_PAGES];
  102. };
  103. struct kioctx {
  104. atomic_t users;
  105. int dead;
  106. struct mm_struct *mm;
  107. /* This needs improving */
  108. unsigned long user_id;
  109. struct kioctx *next;
  110. wait_queue_head_t wait;
  111. spinlock_t ctx_lock;
  112. int reqs_active;
  113. struct list_head active_reqs; /* used for cancellation */
  114. struct list_head run_list; /* used for kicked reqs */
  115. unsigned max_reqs;
  116. struct aio_ring_info ring_info;
  117. struct work_struct wq;
  118. };
  119. /* prototypes */
  120. extern unsigned aio_max_size;
  121. extern ssize_t FASTCALL(wait_on_sync_kiocb(struct kiocb *iocb));
  122. extern int FASTCALL(aio_put_req(struct kiocb *iocb));
  123. extern void FASTCALL(kick_iocb(struct kiocb *iocb));
  124. extern int FASTCALL(aio_complete(struct kiocb *iocb, long res, long res2));
  125. extern void FASTCALL(__put_ioctx(struct kioctx *ctx));
  126. struct mm_struct;
  127. extern void FASTCALL(exit_aio(struct mm_struct *mm));
  128. extern struct kioctx *lookup_ioctx(unsigned long ctx_id);
  129. extern int FASTCALL(io_submit_one(struct kioctx *ctx,
  130. struct iocb __user *user_iocb, struct iocb *iocb));
  131. /* semi private, but used by the 32bit emulations: */
  132. struct kioctx *lookup_ioctx(unsigned long ctx_id);
  133. int FASTCALL(io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  134. struct iocb *iocb));
  135. #define get_ioctx(kioctx) do { if (unlikely(atomic_read(&(kioctx)->users) <= 0)) BUG(); atomic_inc(&(kioctx)->users); } while (0)
  136. #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)
  137. #define in_aio() !is_sync_wait(current->io_wait)
  138. /* may be used for debugging */
  139. #define warn_if_async() \
  140. do { \
  141. if (in_aio()) { \
  142. printk(KERN_ERR "%s(%s:%d) called in async context!\n", \
  143. __FUNCTION__, __FILE__, __LINE__); \
  144. dump_stack(); \
  145. } \
  146. } while (0)
  147. #define io_wait_to_kiocb(wait) container_of(wait, struct kiocb, ki_wait)
  148. #define is_retried_kiocb(iocb) ((iocb)->ki_retried > 1)
  149. #include <linux/aio_abi.h>
  150. static inline struct kiocb *list_kiocb(struct list_head *h)
  151. {
  152. return list_entry(h, struct kiocb, ki_list);
  153. }
  154. /* for sysctl: */
  155. extern atomic_t aio_nr;
  156. extern unsigned aio_max_nr;
  157. #endif /* __LINUX__AIO_H */