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