]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/aio.h
aio: allocate kiocbs in batches
[mirror_ubuntu-artful-kernel.git] / include / linux / aio.h
CommitLineData
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>
abf137dd 8#include <linux/rcupdate.h>
1da177e4 9
60063497 10#include <linux/atomic.h>
1da177e4
LT
11
12#define AIO_MAXSEGS 4
13#define AIO_KIOGRP_NR_ATOMIC 8
14
15struct 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
87struct kiocb {
88 struct list_head ki_run_list;
2ba2d003 89 unsigned long ki_flags;
1da177e4
LT
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
LT
104 __u64 ki_user_data; /* user's data for completion */
105 loff_t ki_pos;
59d9136b
BL
106
107 void *private;
1da177e4
LT
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 */
027445c3 113 struct iovec ki_inline_vec; /* inline vector */
eed4e51f
BP
114 struct iovec *ki_iovec;
115 unsigned long ki_nr_segs;
116 unsigned long ki_cur_seg;
1da177e4 117
59d9136b
BL
118 struct list_head ki_list; /* the aio core uses this
119 * for cancellation */
080d676d 120 struct list_head ki_batch; /* batch allocation */
9c3060be
DL
121
122 /*
123 * If the aio_resfd field of the userspace iocb is not zero,
13389010 124 * this is the underlying eventfd context to deliver events to.
9c3060be 125 */
13389010 126 struct eventfd_ctx *ki_eventfd;
1da177e4
LT
127};
128
129#define is_sync_kiocb(iocb) ((iocb)->ki_key == KIOCB_SYNC_KEY)
130#define init_sync_kiocb(x, filp) \
131 do { \
132 struct task_struct *tsk = current; \
133 (x)->ki_flags = 0; \
134 (x)->ki_users = 1; \
135 (x)->ki_key = KIOCB_SYNC_KEY; \
136 (x)->ki_filp = (filp); \
20dcae32 137 (x)->ki_ctx = NULL; \
1da177e4 138 (x)->ki_cancel = NULL; \
59d9136b 139 (x)->ki_retry = NULL; \
1da177e4
LT
140 (x)->ki_dtor = NULL; \
141 (x)->ki_obj.tsk = tsk; \
142 (x)->ki_user_data = 0; \
1da177e4
LT
143 } while (0)
144
145#define AIO_RING_MAGIC 0xa10a10a1
146#define AIO_RING_COMPAT_FEATURES 1
147#define AIO_RING_INCOMPAT_FEATURES 0
148struct aio_ring {
149 unsigned id; /* kernel internal index number */
150 unsigned nr; /* number of io_events */
151 unsigned head;
152 unsigned tail;
153
154 unsigned magic;
155 unsigned compat_features;
156 unsigned incompat_features;
157 unsigned header_length; /* size of aio_ring */
158
159
160 struct io_event io_events[0];
161}; /* 128 bytes + ring size */
162
163#define aio_ring_avail(info, ring) (((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr)
164
165#define AIO_RING_PAGES 8
166struct aio_ring_info {
167 unsigned long mmap_base;
168 unsigned long mmap_size;
169
170 struct page **ring_pages;
171 spinlock_t ring_lock;
172 long nr_pages;
173
174 unsigned nr, tail;
175
176 struct page *internal_pages[AIO_RING_PAGES];
177};
178
179struct kioctx {
180 atomic_t users;
181 int dead;
182 struct mm_struct *mm;
183
184 /* This needs improving */
185 unsigned long user_id;
abf137dd 186 struct hlist_node list;
1da177e4
LT
187
188 wait_queue_head_t wait;
189
190 spinlock_t ctx_lock;
191
192 int reqs_active;
193 struct list_head active_reqs; /* used for cancellation */
194 struct list_head run_list; /* used for kicked reqs */
195
d55b5fda 196 /* sys_io_setup currently limits this to an unsigned int */
1da177e4
LT
197 unsigned max_reqs;
198
199 struct aio_ring_info ring_info;
200
52bad64d 201 struct delayed_work wq;
abf137dd
JA
202
203 struct rcu_head rcu_head;
1da177e4
LT
204};
205
206/* prototypes */
207extern unsigned aio_max_size;
208
ebf3f09c 209#ifdef CONFIG_AIO
b3c97528
HH
210extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
211extern int aio_put_req(struct kiocb *iocb);
212extern void kick_iocb(struct kiocb *iocb);
213extern int aio_complete(struct kiocb *iocb, long res, long res2);
1da177e4 214struct mm_struct;
b3c97528 215extern void exit_aio(struct mm_struct *mm);
9d85cba7
JM
216extern long do_io_submit(aio_context_t ctx_id, long nr,
217 struct iocb __user *__user *iocbpp, bool compat);
ebf3f09c
TP
218#else
219static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
220static inline int aio_put_req(struct kiocb *iocb) { return 0; }
221static inline void kick_iocb(struct kiocb *iocb) { }
222static inline int aio_complete(struct kiocb *iocb, long res, long res2) { return 0; }
223struct mm_struct;
224static inline void exit_aio(struct mm_struct *mm) { }
9d85cba7
JM
225static inline long do_io_submit(aio_context_t ctx_id, long nr,
226 struct iocb __user * __user *iocbpp,
227 bool compat) { return 0; }
ebf3f09c 228#endif /* CONFIG_AIO */
1da177e4 229
1da177e4
LT
230static inline struct kiocb *list_kiocb(struct list_head *h)
231{
232 return list_entry(h, struct kiocb, ki_list);
233}
234
235/* for sysctl: */
d55b5fda
ZB
236extern unsigned long aio_nr;
237extern unsigned long aio_max_nr;
1da177e4
LT
238
239#endif /* __LINUX__AIO_H */