blob: 31ff6dba4872a96c6bbfa5a78bbe27a782affc41 [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
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 */
Zach Brown4faa5282005-10-17 16:43:33 -070029/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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
Zach Brown897f15f2005-09-30 11:58:55 -070053/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct kiocb {
88 struct list_head ki_run_list;
David Brownell2ba2d002007-07-19 01:47:55 -070089 unsigned long ki_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 union {
100 void __user *user;
101 struct task_struct *tsk;
102 } ki_obj;
Benjamin LaHaise59d91362006-01-08 01:04:34 -0800103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 __u64 ki_user_data; /* user's data for completion */
105 loff_t ki_pos;
Benjamin LaHaise59d91362006-01-08 01:04:34 -0800106
107 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* 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 */
Badari Pulavarty027445c2006-09-30 23:28:46 -0700113 struct iovec ki_inline_vec; /* inline vector */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700114 struct iovec *ki_iovec;
115 unsigned long ki_nr_segs;
116 unsigned long ki_cur_seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Benjamin LaHaise59d91362006-01-08 01:04:34 -0800118 struct list_head ki_list; /* the aio core uses this
119 * for cancellation */
Jeff Moyer080d6762011-11-02 13:40:10 -0700120 struct list_head ki_batch; /* batch allocation */
Davide Libenzi9c3060b2007-05-10 22:23:21 -0700121
122 /*
123 * If the aio_resfd field of the userspace iocb is not zero,
Davide Libenzi13389012009-06-30 11:41:11 -0700124 * this is the underlying eventfd context to deliver events to.
Davide Libenzi9c3060b2007-05-10 22:23:21 -0700125 */
Davide Libenzi13389012009-06-30 11:41:11 -0700126 struct eventfd_ctx *ki_eventfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Andrew Mortonf7e1bec2012-07-30 14:42:56 -0700129static inline bool is_sync_kiocb(struct kiocb *kiocb)
130{
131 return kiocb->ki_key == KIOCB_SYNC_KEY;
132}
133
134static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
135{
136 *kiocb = (struct kiocb) {
137 .ki_users = 1,
138 .ki_key = KIOCB_SYNC_KEY,
139 .ki_filp = filp,
140 .ki_obj.tsk = current,
141 };
142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#define AIO_RING_MAGIC 0xa10a10a1
145#define AIO_RING_COMPAT_FEATURES 1
146#define AIO_RING_INCOMPAT_FEATURES 0
147struct aio_ring {
148 unsigned id; /* kernel internal index number */
149 unsigned nr; /* number of io_events */
150 unsigned head;
151 unsigned tail;
152
153 unsigned magic;
154 unsigned compat_features;
155 unsigned incompat_features;
156 unsigned header_length; /* size of aio_ring */
157
158
159 struct io_event io_events[0];
160}; /* 128 bytes + ring size */
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define AIO_RING_PAGES 8
163struct aio_ring_info {
164 unsigned long mmap_base;
165 unsigned long mmap_size;
166
167 struct page **ring_pages;
168 spinlock_t ring_lock;
169 long nr_pages;
170
171 unsigned nr, tail;
172
173 struct page *internal_pages[AIO_RING_PAGES];
174};
175
Andrew Mortonf7e1bec2012-07-30 14:42:56 -0700176static inline unsigned aio_ring_avail(struct aio_ring_info *info,
177 struct aio_ring *ring)
178{
179 return (ring->head + info->nr - 1 - ring->tail) % info->nr;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182struct kioctx {
183 atomic_t users;
184 int dead;
185 struct mm_struct *mm;
186
187 /* This needs improving */
188 unsigned long user_id;
Jens Axboeabf137d2008-12-09 08:11:22 +0100189 struct hlist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 wait_queue_head_t wait;
192
193 spinlock_t ctx_lock;
194
195 int reqs_active;
196 struct list_head active_reqs; /* used for cancellation */
197 struct list_head run_list; /* used for kicked reqs */
198
Zach Brownd55b5fd2005-11-07 00:59:31 -0800199 /* sys_io_setup currently limits this to an unsigned int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unsigned max_reqs;
201
202 struct aio_ring_info ring_info;
203
David Howells52bad642006-11-22 14:54:01 +0000204 struct delayed_work wq;
Jens Axboeabf137d2008-12-09 08:11:22 +0100205
206 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
209/* prototypes */
210extern unsigned aio_max_size;
211
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700212#ifdef CONFIG_AIO
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800213extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
214extern int aio_put_req(struct kiocb *iocb);
215extern void kick_iocb(struct kiocb *iocb);
216extern int aio_complete(struct kiocb *iocb, long res, long res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217struct mm_struct;
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800218extern void exit_aio(struct mm_struct *mm);
Jeff Moyer9d85cba2010-05-26 14:44:26 -0700219extern long do_io_submit(aio_context_t ctx_id, long nr,
220 struct iocb __user *__user *iocbpp, bool compat);
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700221#else
222static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
223static inline int aio_put_req(struct kiocb *iocb) { return 0; }
224static inline void kick_iocb(struct kiocb *iocb) { }
225static inline int aio_complete(struct kiocb *iocb, long res, long res2) { return 0; }
226struct mm_struct;
227static inline void exit_aio(struct mm_struct *mm) { }
Jeff Moyer9d85cba2010-05-26 14:44:26 -0700228static inline long do_io_submit(aio_context_t ctx_id, long nr,
229 struct iocb __user * __user *iocbpp,
230 bool compat) { return 0; }
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700231#endif /* CONFIG_AIO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static inline struct kiocb *list_kiocb(struct list_head *h)
234{
235 return list_entry(h, struct kiocb, ki_list);
236}
237
238/* for sysctl: */
Zach Brownd55b5fd2005-11-07 00:59:31 -0800239extern unsigned long aio_nr;
240extern unsigned long aio_max_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242#endif /* __LINUX__AIO_H */