4 #include <linux/list.h>
5 #include <linux/workqueue.h>
6 #include <linux/aio_abi.h>
9 #include <asm/atomic.h>
12 #define AIO_KIOGRP_NR_ATOMIC 8
16 /* Notes on cancelling a kiocb:
17 * If a kiocb is cancelled, aio_complete may return 0 to indicate
18 * that cancel has not yet disposed of the kiocb. All cancel
19 * operations *must* call aio_put_req to dispose of the kiocb
20 * to guard against races with the completion code.
22 #define KIOCB_C_CANCELLED 0x01
23 #define KIOCB_C_COMPLETE 0x02
25 #define KIOCB_SYNC_KEY (~0U)
29 * This may be used for cancel/retry serialization in the future, but
30 * for now it's unused and we probably don't want modules to even
31 * think they can use it.
33 /* #define KIF_LOCKED 0 */
35 #define KIF_CANCELLED 2
37 #define kiocbTryLock(iocb) test_and_set_bit(KIF_LOCKED, &(iocb)->ki_flags)
38 #define kiocbTryKick(iocb) test_and_set_bit(KIF_KICKED, &(iocb)->ki_flags)
40 #define kiocbSetLocked(iocb) set_bit(KIF_LOCKED, &(iocb)->ki_flags)
41 #define kiocbSetKicked(iocb) set_bit(KIF_KICKED, &(iocb)->ki_flags)
42 #define kiocbSetCancelled(iocb) set_bit(KIF_CANCELLED, &(iocb)->ki_flags)
44 #define kiocbClearLocked(iocb) clear_bit(KIF_LOCKED, &(iocb)->ki_flags)
45 #define kiocbClearKicked(iocb) clear_bit(KIF_KICKED, &(iocb)->ki_flags)
46 #define kiocbClearCancelled(iocb) clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)
48 #define kiocbIsLocked(iocb) test_bit(KIF_LOCKED, &(iocb)->ki_flags)
49 #define kiocbIsKicked(iocb) test_bit(KIF_KICKED, &(iocb)->ki_flags)
50 #define kiocbIsCancelled(iocb) test_bit(KIF_CANCELLED, &(iocb)->ki_flags)
52 /* is there a better place to document function pointer methods? */
54 * ki_retry - iocb forward progress callback
55 * @kiocb: The kiocb struct to advance by performing an operation.
57 * This callback is called when the AIO core wants a given AIO operation
58 * to make forward progress. The kiocb argument describes the operation
59 * that is to be performed. As the operation proceeds, perhaps partially,
60 * ki_retry is expected to update the kiocb with progress made. Typically
61 * ki_retry is set in the AIO core and it itself calls file_operations
64 * ki_retry's return value determines when the AIO operation is completed
65 * and an event is generated in the AIO event ring. Except the special
66 * return values described below, the value that is returned from ki_retry
67 * is transferred directly into the completion ring as the operation's
68 * resulting status. Once this has happened ki_retry *MUST NOT* reference
69 * the kiocb pointer again.
71 * If ki_retry returns -EIOCBQUEUED it has made a promise that aio_complete()
72 * will be called on the kiocb pointer in the future. The AIO core will
73 * not ask the method again -- ki_retry must ensure forward progress.
74 * aio_complete() must be called once and only once in the future, multiple
75 * calls may result in undefined behaviour.
77 * If ki_retry returns -EIOCBRETRY it has made a promise that kick_iocb()
78 * will be called on the kiocb pointer in the future. This may happen
79 * through generic helpers that associate kiocb->ki_wait with a wait
80 * queue head that ki_retry uses via current->io_wait. It can also happen
81 * with custom tracking and manual calls to kick_iocb(), though that is
82 * discouraged. In either case, kick_iocb() must be called once and only
83 * once. ki_retry must ensure forward progress, the AIO core will wait
84 * indefinitely for kick_iocb() to be called.
87 struct list_head ki_run_list;
88 unsigned long ki_flags;
90 unsigned ki_key; /* id of this request */
93 struct kioctx *ki_ctx; /* may be NULL for sync ops */
94 int (*ki_cancel)(struct kiocb *, struct io_event *);
95 ssize_t (*ki_retry)(struct kiocb *);
96 void (*ki_dtor)(struct kiocb *);
100 struct task_struct *tsk;
103 __u64 ki_user_data; /* user's data for completion */
104 wait_queue_t ki_wait;
108 /* State that we remember to be able to restart/retry */
109 unsigned short ki_opcode;
110 size_t ki_nbytes; /* copy of iocb->aio_nbytes */
111 char __user *ki_buf; /* remaining iocb->aio_buf */
112 size_t ki_left; /* remaining bytes */
113 struct iovec ki_inline_vec; /* inline vector */
114 struct iovec *ki_iovec;
115 unsigned long ki_nr_segs;
116 unsigned long ki_cur_seg;
118 struct list_head ki_list; /* the aio core uses this
119 * for cancellation */
122 * If the aio_resfd field of the userspace iocb is not zero,
123 * this is the underlying file* to deliver event to.
125 struct file *ki_eventfd;
128 #define is_sync_kiocb(iocb) ((iocb)->ki_key == KIOCB_SYNC_KEY)
129 #define init_sync_kiocb(x, filp) \
131 struct task_struct *tsk = current; \
134 (x)->ki_key = KIOCB_SYNC_KEY; \
135 (x)->ki_filp = (filp); \
136 (x)->ki_ctx = NULL; \
137 (x)->ki_cancel = NULL; \
138 (x)->ki_retry = NULL; \
139 (x)->ki_dtor = NULL; \
140 (x)->ki_obj.tsk = tsk; \
141 (x)->ki_user_data = 0; \
142 init_wait((&(x)->ki_wait)); \
145 #define AIO_RING_MAGIC 0xa10a10a1
146 #define AIO_RING_COMPAT_FEATURES 1
147 #define AIO_RING_INCOMPAT_FEATURES 0
149 unsigned id; /* kernel internal index number */
150 unsigned nr; /* number of io_events */
155 unsigned compat_features;
156 unsigned incompat_features;
157 unsigned header_length; /* size of aio_ring */
160 struct io_event io_events[0];
161 }; /* 128 bytes + ring size */
163 #define aio_ring_avail(info, ring) (((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr)
165 #define AIO_RING_PAGES 8
166 struct aio_ring_info {
167 unsigned long mmap_base;
168 unsigned long mmap_size;
170 struct page **ring_pages;
171 spinlock_t ring_lock;
176 struct page *internal_pages[AIO_RING_PAGES];
182 struct mm_struct *mm;
184 /* This needs improving */
185 unsigned long user_id;
188 wait_queue_head_t wait;
193 struct list_head active_reqs; /* used for cancellation */
194 struct list_head run_list; /* used for kicked reqs */
196 /* sys_io_setup currently limits this to an unsigned int */
199 struct aio_ring_info ring_info;
201 struct delayed_work wq;
205 extern unsigned aio_max_size;
207 extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
208 extern int aio_put_req(struct kiocb *iocb);
209 extern void kick_iocb(struct kiocb *iocb);
210 extern int aio_complete(struct kiocb *iocb, long res, long res2);
212 extern void exit_aio(struct mm_struct *mm);
214 #define io_wait_to_kiocb(wait) container_of(wait, struct kiocb, ki_wait)
216 #include <linux/aio_abi.h>
218 static inline struct kiocb *list_kiocb(struct list_head *h)
220 return list_entry(h, struct kiocb, ki_list);
224 extern unsigned long aio_nr;
225 extern unsigned long aio_max_nr;
227 #endif /* __LINUX__AIO_H */