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