blob: d2a00038ec7778f14ae0eef9fabebb5eb94b410d [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;
Kent Overstreet0460fef2013-05-07 16:18:49 -070013struct kiocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define KIOCB_SYNC_KEY (~0U)
16
Kent Overstreet0460fef2013-05-07 16:18:49 -070017/*
18 * We use ki_cancel == KIOCB_CANCELLED to indicate that a kiocb has been either
19 * cancelled or completed (this makes a certain amount of sense because
20 * successful cancellation - io_cancel() - does deliver the completion to
21 * userspace).
22 *
23 * And since most things don't implement kiocb cancellation and we'd really like
24 * kiocb completion to be lockless when possible, we use ki_cancel to
25 * synchronize cancellation and completion - we only set it to KIOCB_CANCELLED
26 * with xchg() or cmpxchg(), see batch_complete_aio() and kiocb_cancel().
27 */
28#define KIOCB_CANCELLED ((void *) (~0ULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Kent Overstreet0460fef2013-05-07 16:18:49 -070030typedef int (kiocb_cancel_fn)(struct kiocb *, struct io_event *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Zach Brown897f15f2005-09-30 11:58:55 -070032/* is there a better place to document function pointer methods? */
33/**
34 * ki_retry - iocb forward progress callback
35 * @kiocb: The kiocb struct to advance by performing an operation.
36 *
37 * This callback is called when the AIO core wants a given AIO operation
38 * to make forward progress. The kiocb argument describes the operation
39 * that is to be performed. As the operation proceeds, perhaps partially,
40 * ki_retry is expected to update the kiocb with progress made. Typically
41 * ki_retry is set in the AIO core and it itself calls file_operations
42 * helpers.
43 *
44 * ki_retry's return value determines when the AIO operation is completed
45 * and an event is generated in the AIO event ring. Except the special
46 * return values described below, the value that is returned from ki_retry
47 * is transferred directly into the completion ring as the operation's
48 * resulting status. Once this has happened ki_retry *MUST NOT* reference
49 * the kiocb pointer again.
50 *
51 * If ki_retry returns -EIOCBQUEUED it has made a promise that aio_complete()
52 * will be called on the kiocb pointer in the future. The AIO core will
53 * not ask the method again -- ki_retry must ensure forward progress.
54 * aio_complete() must be called once and only once in the future, multiple
55 * calls may result in undefined behaviour.
Zach Brown897f15f2005-09-30 11:58:55 -070056 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct kiocb {
Kent Overstreet11599eb2013-05-07 16:18:39 -070058 atomic_t ki_users;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned ki_key; /* id of this request */
60
61 struct file *ki_filp;
62 struct kioctx *ki_ctx; /* may be NULL for sync ops */
Kent Overstreet0460fef2013-05-07 16:18:49 -070063 kiocb_cancel_fn *ki_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ssize_t (*ki_retry)(struct kiocb *);
65 void (*ki_dtor)(struct kiocb *);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 union {
68 void __user *user;
69 struct task_struct *tsk;
70 } ki_obj;
Benjamin LaHaise59d91362006-01-08 01:04:34 -080071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 __u64 ki_user_data; /* user's data for completion */
73 loff_t ki_pos;
Benjamin LaHaise59d91362006-01-08 01:04:34 -080074
75 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* State that we remember to be able to restart/retry */
77 unsigned short ki_opcode;
78 size_t ki_nbytes; /* copy of iocb->aio_nbytes */
79 char __user *ki_buf; /* remaining iocb->aio_buf */
80 size_t ki_left; /* remaining bytes */
Badari Pulavarty027445c2006-09-30 23:28:46 -070081 struct iovec ki_inline_vec; /* inline vector */
Badari Pulavartyeed4e512006-09-30 23:28:49 -070082 struct iovec *ki_iovec;
83 unsigned long ki_nr_segs;
84 unsigned long ki_cur_seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Benjamin LaHaise59d91362006-01-08 01:04:34 -080086 struct list_head ki_list; /* the aio core uses this
87 * for cancellation */
Jeff Moyer080d6762011-11-02 13:40:10 -070088 struct list_head ki_batch; /* batch allocation */
Davide Libenzi9c3060b2007-05-10 22:23:21 -070089
90 /*
91 * If the aio_resfd field of the userspace iocb is not zero,
Davide Libenzi13389012009-06-30 11:41:11 -070092 * this is the underlying eventfd context to deliver events to.
Davide Libenzi9c3060b2007-05-10 22:23:21 -070093 */
Davide Libenzi13389012009-06-30 11:41:11 -070094 struct eventfd_ctx *ki_eventfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
Andrew Mortonf7e1bec2012-07-30 14:42:56 -070097static inline bool is_sync_kiocb(struct kiocb *kiocb)
98{
99 return kiocb->ki_key == KIOCB_SYNC_KEY;
100}
101
102static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
103{
104 *kiocb = (struct kiocb) {
Kent Overstreet11599eb2013-05-07 16:18:39 -0700105 .ki_users = ATOMIC_INIT(1),
Andrew Mortonf7e1bec2012-07-30 14:42:56 -0700106 .ki_key = KIOCB_SYNC_KEY,
107 .ki_filp = filp,
108 .ki_obj.tsk = current,
109 };
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* prototypes */
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700113#ifdef CONFIG_AIO
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800114extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
Kent Overstreet2d684492013-05-07 16:18:29 -0700115extern void aio_put_req(struct kiocb *iocb);
116extern void aio_complete(struct kiocb *iocb, long res, long res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117struct mm_struct;
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800118extern void exit_aio(struct mm_struct *mm);
Jeff Moyer9d85cba2010-05-26 14:44:26 -0700119extern long do_io_submit(aio_context_t ctx_id, long nr,
120 struct iocb __user *__user *iocbpp, bool compat);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700121void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel);
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700122#else
123static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
Kent Overstreet2d684492013-05-07 16:18:29 -0700124static inline void aio_put_req(struct kiocb *iocb) { }
125static inline void aio_complete(struct kiocb *iocb, long res, long res2) { }
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700126struct mm_struct;
127static inline void exit_aio(struct mm_struct *mm) { }
Jeff Moyer9d85cba2010-05-26 14:44:26 -0700128static inline long do_io_submit(aio_context_t ctx_id, long nr,
129 struct iocb __user * __user *iocbpp,
130 bool compat) { return 0; }
Kent Overstreet0460fef2013-05-07 16:18:49 -0700131static inline void kiocb_set_cancel_fn(struct kiocb *req,
132 kiocb_cancel_fn *cancel) { }
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700133#endif /* CONFIG_AIO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static inline struct kiocb *list_kiocb(struct list_head *h)
136{
137 return list_entry(h, struct kiocb, ki_list);
138}
139
140/* for sysctl: */
Zach Brownd55b5fd2005-11-07 00:59:31 -0800141extern unsigned long aio_nr;
142extern unsigned long aio_max_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#endif /* __LINUX__AIO_H */