Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame^] | 7 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | #include <asm/atomic.h> |
| 10 | |
| 11 | #define AIO_MAXSEGS 4 |
| 12 | #define AIO_KIOGRP_NR_ATOMIC 8 |
| 13 | |
| 14 | struct kioctx; |
| 15 | |
| 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. |
| 21 | */ |
| 22 | #define KIOCB_C_CANCELLED 0x01 |
| 23 | #define KIOCB_C_COMPLETE 0x02 |
| 24 | |
| 25 | #define KIOCB_SYNC_KEY (~0U) |
| 26 | |
| 27 | /* ki_flags bits */ |
Zach Brown | 4faa528 | 2005-10-17 16:43:33 -0700 | [diff] [blame] | 28 | /* |
| 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. |
| 32 | */ |
| 33 | /* #define KIF_LOCKED 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define KIF_KICKED 1 |
| 35 | #define KIF_CANCELLED 2 |
| 36 | |
| 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) |
| 39 | |
| 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) |
| 43 | |
| 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) |
| 47 | |
| 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) |
| 51 | |
Zach Brown | 897f15f | 2005-09-30 11:58:55 -0700 | [diff] [blame] | 52 | /* is there a better place to document function pointer methods? */ |
| 53 | /** |
| 54 | * ki_retry - iocb forward progress callback |
| 55 | * @kiocb: The kiocb struct to advance by performing an operation. |
| 56 | * |
| 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 |
| 62 | * helpers. |
| 63 | * |
| 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. |
| 70 | * |
| 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. |
| 76 | * |
| 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. |
| 85 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | struct kiocb { |
| 87 | struct list_head ki_run_list; |
| 88 | long ki_flags; |
| 89 | int ki_users; |
| 90 | unsigned ki_key; /* id of this request */ |
| 91 | |
| 92 | struct file *ki_filp; |
| 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 *); |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | union { |
| 99 | void __user *user; |
| 100 | struct task_struct *tsk; |
| 101 | } ki_obj; |
Benjamin LaHaise | 59d9136 | 2006-01-08 01:04:34 -0800 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | __u64 ki_user_data; /* user's data for completion */ |
Benjamin LaHaise | 59d9136 | 2006-01-08 01:04:34 -0800 | [diff] [blame] | 104 | wait_queue_t ki_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | loff_t ki_pos; |
Benjamin LaHaise | 59d9136 | 2006-01-08 01:04:34 -0800 | [diff] [blame] | 106 | |
| 107 | void *private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | long ki_retried; /* just for testing */ |
| 114 | long ki_kicked; /* just for testing */ |
| 115 | long ki_queued; /* just for testing */ |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame^] | 116 | struct iovec ki_inline_vec; /* inline vector */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Benjamin LaHaise | 59d9136 | 2006-01-08 01:04:34 -0800 | [diff] [blame] | 118 | struct list_head ki_list; /* the aio core uses this |
| 119 | * for cancellation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | #define is_sync_kiocb(iocb) ((iocb)->ki_key == KIOCB_SYNC_KEY) |
| 123 | #define init_sync_kiocb(x, filp) \ |
| 124 | do { \ |
| 125 | struct task_struct *tsk = current; \ |
| 126 | (x)->ki_flags = 0; \ |
| 127 | (x)->ki_users = 1; \ |
| 128 | (x)->ki_key = KIOCB_SYNC_KEY; \ |
| 129 | (x)->ki_filp = (filp); \ |
Zach Brown | 20dcae3 | 2005-11-13 16:07:33 -0800 | [diff] [blame] | 130 | (x)->ki_ctx = NULL; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | (x)->ki_cancel = NULL; \ |
Benjamin LaHaise | 59d9136 | 2006-01-08 01:04:34 -0800 | [diff] [blame] | 132 | (x)->ki_retry = NULL; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | (x)->ki_dtor = NULL; \ |
| 134 | (x)->ki_obj.tsk = tsk; \ |
| 135 | (x)->ki_user_data = 0; \ |
| 136 | init_wait((&(x)->ki_wait)); \ |
| 137 | } while (0) |
| 138 | |
| 139 | #define AIO_RING_MAGIC 0xa10a10a1 |
| 140 | #define AIO_RING_COMPAT_FEATURES 1 |
| 141 | #define AIO_RING_INCOMPAT_FEATURES 0 |
| 142 | struct aio_ring { |
| 143 | unsigned id; /* kernel internal index number */ |
| 144 | unsigned nr; /* number of io_events */ |
| 145 | unsigned head; |
| 146 | unsigned tail; |
| 147 | |
| 148 | unsigned magic; |
| 149 | unsigned compat_features; |
| 150 | unsigned incompat_features; |
| 151 | unsigned header_length; /* size of aio_ring */ |
| 152 | |
| 153 | |
| 154 | struct io_event io_events[0]; |
| 155 | }; /* 128 bytes + ring size */ |
| 156 | |
| 157 | #define aio_ring_avail(info, ring) (((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr) |
| 158 | |
| 159 | #define AIO_RING_PAGES 8 |
| 160 | struct aio_ring_info { |
| 161 | unsigned long mmap_base; |
| 162 | unsigned long mmap_size; |
| 163 | |
| 164 | struct page **ring_pages; |
| 165 | spinlock_t ring_lock; |
| 166 | long nr_pages; |
| 167 | |
| 168 | unsigned nr, tail; |
| 169 | |
| 170 | struct page *internal_pages[AIO_RING_PAGES]; |
| 171 | }; |
| 172 | |
| 173 | struct kioctx { |
| 174 | atomic_t users; |
| 175 | int dead; |
| 176 | struct mm_struct *mm; |
| 177 | |
| 178 | /* This needs improving */ |
| 179 | unsigned long user_id; |
| 180 | struct kioctx *next; |
| 181 | |
| 182 | wait_queue_head_t wait; |
| 183 | |
| 184 | spinlock_t ctx_lock; |
| 185 | |
| 186 | int reqs_active; |
| 187 | struct list_head active_reqs; /* used for cancellation */ |
| 188 | struct list_head run_list; /* used for kicked reqs */ |
| 189 | |
Zach Brown | d55b5fd | 2005-11-07 00:59:31 -0800 | [diff] [blame] | 190 | /* sys_io_setup currently limits this to an unsigned int */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | unsigned max_reqs; |
| 192 | |
| 193 | struct aio_ring_info ring_info; |
| 194 | |
| 195 | struct work_struct wq; |
| 196 | }; |
| 197 | |
| 198 | /* prototypes */ |
| 199 | extern unsigned aio_max_size; |
| 200 | |
| 201 | extern ssize_t FASTCALL(wait_on_sync_kiocb(struct kiocb *iocb)); |
| 202 | extern int FASTCALL(aio_put_req(struct kiocb *iocb)); |
| 203 | extern void FASTCALL(kick_iocb(struct kiocb *iocb)); |
| 204 | extern int FASTCALL(aio_complete(struct kiocb *iocb, long res, long res2)); |
| 205 | extern void FASTCALL(__put_ioctx(struct kioctx *ctx)); |
| 206 | struct mm_struct; |
| 207 | extern void FASTCALL(exit_aio(struct mm_struct *mm)); |
| 208 | extern struct kioctx *lookup_ioctx(unsigned long ctx_id); |
| 209 | extern int FASTCALL(io_submit_one(struct kioctx *ctx, |
| 210 | struct iocb __user *user_iocb, struct iocb *iocb)); |
| 211 | |
| 212 | /* semi private, but used by the 32bit emulations: */ |
| 213 | struct kioctx *lookup_ioctx(unsigned long ctx_id); |
| 214 | int FASTCALL(io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb, |
| 215 | struct iocb *iocb)); |
| 216 | |
Zach Brown | 5ef1c49 | 2005-11-13 16:07:35 -0800 | [diff] [blame] | 217 | #define get_ioctx(kioctx) do { \ |
Rolf Eike Beer | 3a27111 | 2006-09-30 23:28:09 -0700 | [diff] [blame] | 218 | BUG_ON(atomic_read(&(kioctx)->users) <= 0); \ |
Zach Brown | 5ef1c49 | 2005-11-13 16:07:35 -0800 | [diff] [blame] | 219 | atomic_inc(&(kioctx)->users); \ |
| 220 | } while (0) |
| 221 | #define put_ioctx(kioctx) do { \ |
Rolf Eike Beer | 3a27111 | 2006-09-30 23:28:09 -0700 | [diff] [blame] | 222 | BUG_ON(atomic_read(&(kioctx)->users) <= 0); \ |
Zach Brown | 5ef1c49 | 2005-11-13 16:07:35 -0800 | [diff] [blame] | 223 | if (unlikely(atomic_dec_and_test(&(kioctx)->users))) \ |
| 224 | __put_ioctx(kioctx); \ |
| 225 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | #define in_aio() !is_sync_wait(current->io_wait) |
| 228 | /* may be used for debugging */ |
| 229 | #define warn_if_async() \ |
| 230 | do { \ |
| 231 | if (in_aio()) { \ |
| 232 | printk(KERN_ERR "%s(%s:%d) called in async context!\n", \ |
| 233 | __FUNCTION__, __FILE__, __LINE__); \ |
| 234 | dump_stack(); \ |
| 235 | } \ |
| 236 | } while (0) |
| 237 | |
| 238 | #define io_wait_to_kiocb(wait) container_of(wait, struct kiocb, ki_wait) |
| 239 | #define is_retried_kiocb(iocb) ((iocb)->ki_retried > 1) |
| 240 | |
| 241 | #include <linux/aio_abi.h> |
| 242 | |
| 243 | static inline struct kiocb *list_kiocb(struct list_head *h) |
| 244 | { |
| 245 | return list_entry(h, struct kiocb, ki_list); |
| 246 | } |
| 247 | |
| 248 | /* for sysctl: */ |
Zach Brown | d55b5fd | 2005-11-07 00:59:31 -0800 | [diff] [blame] | 249 | extern unsigned long aio_nr; |
| 250 | extern unsigned long aio_max_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | #endif /* __LINUX__AIO_H */ |