blob: 615d55a5d0be882f2d3ced1a81fb68dd7063759c [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>
Badari Pulavarty027445c2006-09-30 23:28:46 -07007#include <linux/uio.h>
Jens Axboeabf137d2008-12-09 08:11:22 +01008#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Arun Sharma600634972011-07-26 16:09:06 -070010#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012struct kioctx;
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define KIOCB_SYNC_KEY (~0U)
15
16/* ki_flags bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define KIF_CANCELLED 2
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define kiocbSetCancelled(iocb) set_bit(KIF_CANCELLED, &(iocb)->ki_flags)
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define kiocbClearCancelled(iocb) clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define kiocbIsCancelled(iocb) test_bit(KIF_CANCELLED, &(iocb)->ki_flags)
24
Zach Brown897f15f2005-09-30 11:58:55 -070025/* is there a better place to document function pointer methods? */
26/**
27 * ki_retry - iocb forward progress callback
28 * @kiocb: The kiocb struct to advance by performing an operation.
29 *
30 * This callback is called when the AIO core wants a given AIO operation
31 * to make forward progress. The kiocb argument describes the operation
32 * that is to be performed. As the operation proceeds, perhaps partially,
33 * ki_retry is expected to update the kiocb with progress made. Typically
34 * ki_retry is set in the AIO core and it itself calls file_operations
35 * helpers.
36 *
37 * ki_retry's return value determines when the AIO operation is completed
38 * and an event is generated in the AIO event ring. Except the special
39 * return values described below, the value that is returned from ki_retry
40 * is transferred directly into the completion ring as the operation's
41 * resulting status. Once this has happened ki_retry *MUST NOT* reference
42 * the kiocb pointer again.
43 *
44 * If ki_retry returns -EIOCBQUEUED it has made a promise that aio_complete()
45 * will be called on the kiocb pointer in the future. The AIO core will
46 * not ask the method again -- ki_retry must ensure forward progress.
47 * aio_complete() must be called once and only once in the future, multiple
48 * calls may result in undefined behaviour.
Zach Brown897f15f2005-09-30 11:58:55 -070049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct kiocb {
David Brownell2ba2d002007-07-19 01:47:55 -070051 unsigned long ki_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int ki_users;
53 unsigned ki_key; /* id of this request */
54
55 struct file *ki_filp;
56 struct kioctx *ki_ctx; /* may be NULL for sync ops */
57 int (*ki_cancel)(struct kiocb *, struct io_event *);
58 ssize_t (*ki_retry)(struct kiocb *);
59 void (*ki_dtor)(struct kiocb *);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 union {
62 void __user *user;
63 struct task_struct *tsk;
64 } ki_obj;
Benjamin LaHaise59d91362006-01-08 01:04:34 -080065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 __u64 ki_user_data; /* user's data for completion */
67 loff_t ki_pos;
Benjamin LaHaise59d91362006-01-08 01:04:34 -080068
69 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* State that we remember to be able to restart/retry */
71 unsigned short ki_opcode;
72 size_t ki_nbytes; /* copy of iocb->aio_nbytes */
73 char __user *ki_buf; /* remaining iocb->aio_buf */
74 size_t ki_left; /* remaining bytes */
Badari Pulavarty027445c2006-09-30 23:28:46 -070075 struct iovec ki_inline_vec; /* inline vector */
Badari Pulavartyeed4e512006-09-30 23:28:49 -070076 struct iovec *ki_iovec;
77 unsigned long ki_nr_segs;
78 unsigned long ki_cur_seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Benjamin LaHaise59d91362006-01-08 01:04:34 -080080 struct list_head ki_list; /* the aio core uses this
81 * for cancellation */
Jeff Moyer080d6762011-11-02 13:40:10 -070082 struct list_head ki_batch; /* batch allocation */
Davide Libenzi9c3060b2007-05-10 22:23:21 -070083
84 /*
85 * If the aio_resfd field of the userspace iocb is not zero,
Davide Libenzi13389012009-06-30 11:41:11 -070086 * this is the underlying eventfd context to deliver events to.
Davide Libenzi9c3060b2007-05-10 22:23:21 -070087 */
Davide Libenzi13389012009-06-30 11:41:11 -070088 struct eventfd_ctx *ki_eventfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Andrew Mortonf7e1bec2012-07-30 14:42:56 -070091static inline bool is_sync_kiocb(struct kiocb *kiocb)
92{
93 return kiocb->ki_key == KIOCB_SYNC_KEY;
94}
95
96static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
97{
98 *kiocb = (struct kiocb) {
99 .ki_users = 1,
100 .ki_key = KIOCB_SYNC_KEY,
101 .ki_filp = filp,
102 .ki_obj.tsk = current,
103 };
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#define AIO_RING_MAGIC 0xa10a10a1
107#define AIO_RING_COMPAT_FEATURES 1
108#define AIO_RING_INCOMPAT_FEATURES 0
109struct aio_ring {
110 unsigned id; /* kernel internal index number */
111 unsigned nr; /* number of io_events */
112 unsigned head;
113 unsigned tail;
114
115 unsigned magic;
116 unsigned compat_features;
117 unsigned incompat_features;
118 unsigned header_length; /* size of aio_ring */
119
120
121 struct io_event io_events[0];
122}; /* 128 bytes + ring size */
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define AIO_RING_PAGES 8
125struct aio_ring_info {
126 unsigned long mmap_base;
127 unsigned long mmap_size;
128
129 struct page **ring_pages;
130 spinlock_t ring_lock;
131 long nr_pages;
132
133 unsigned nr, tail;
134
135 struct page *internal_pages[AIO_RING_PAGES];
136};
137
Andrew Mortonf7e1bec2012-07-30 14:42:56 -0700138static inline unsigned aio_ring_avail(struct aio_ring_info *info,
139 struct aio_ring *ring)
140{
141 return (ring->head + info->nr - 1 - ring->tail) % info->nr;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144struct kioctx {
145 atomic_t users;
146 int dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* This needs improving */
149 unsigned long user_id;
Jens Axboeabf137d2008-12-09 08:11:22 +0100150 struct hlist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 wait_queue_head_t wait;
153
154 spinlock_t ctx_lock;
155
156 int reqs_active;
157 struct list_head active_reqs; /* used for cancellation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Zach Brownd55b5fd2005-11-07 00:59:31 -0800159 /* sys_io_setup currently limits this to an unsigned int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned max_reqs;
161
162 struct aio_ring_info ring_info;
163
Jens Axboeabf137d2008-12-09 08:11:22 +0100164 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167/* prototypes */
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700168#ifdef CONFIG_AIO
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800169extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
Kent Overstreet2d684492013-05-07 16:18:29 -0700170extern void aio_put_req(struct kiocb *iocb);
171extern void aio_complete(struct kiocb *iocb, long res, long res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172struct mm_struct;
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800173extern void exit_aio(struct mm_struct *mm);
Jeff Moyer9d85cba2010-05-26 14:44:26 -0700174extern long do_io_submit(aio_context_t ctx_id, long nr,
175 struct iocb __user *__user *iocbpp, bool compat);
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700176#else
177static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
Kent Overstreet2d684492013-05-07 16:18:29 -0700178static inline void aio_put_req(struct kiocb *iocb) { }
179static inline void aio_complete(struct kiocb *iocb, long res, long res2) { }
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700180struct mm_struct;
181static inline void exit_aio(struct mm_struct *mm) { }
Jeff Moyer9d85cba2010-05-26 14:44:26 -0700182static inline long do_io_submit(aio_context_t ctx_id, long nr,
183 struct iocb __user * __user *iocbpp,
184 bool compat) { return 0; }
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700185#endif /* CONFIG_AIO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static inline struct kiocb *list_kiocb(struct list_head *h)
188{
189 return list_entry(h, struct kiocb, ki_list);
190}
191
192/* for sysctl: */
Zach Brownd55b5fd2005-11-07 00:59:31 -0800193extern unsigned long aio_nr;
194extern unsigned long aio_max_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196#endif /* __LINUX__AIO_H */