blob: 7bb766e739687a67088a7a409b771973f622a8ff [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
Kent Overstreet8a660892013-05-07 16:19:10 -070015#define KIOCB_KEY 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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 Overstreetbec68faa2013-05-13 14:45:08 -070030typedef int (kiocb_cancel_fn)(struct kiocb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct kiocb {
Kent Overstreet11599eb2013-05-07 16:18:39 -070033 atomic_t ki_users;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 struct file *ki_filp;
Kent Overstreet8a660892013-05-07 16:19:10 -070036 struct kioctx *ki_ctx; /* NULL for sync ops */
Kent Overstreet0460fef2013-05-07 16:18:49 -070037 kiocb_cancel_fn *ki_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 void (*ki_dtor)(struct kiocb *);
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 union {
41 void __user *user;
42 struct task_struct *tsk;
43 } ki_obj;
Benjamin LaHaise59d91362006-01-08 01:04:34 -080044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 __u64 ki_user_data; /* user's data for completion */
46 loff_t ki_pos;
Benjamin LaHaise59d91362006-01-08 01:04:34 -080047
48 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* State that we remember to be able to restart/retry */
50 unsigned short ki_opcode;
51 size_t ki_nbytes; /* copy of iocb->aio_nbytes */
52 char __user *ki_buf; /* remaining iocb->aio_buf */
Badari Pulavarty027445c2006-09-30 23:28:46 -070053 struct iovec ki_inline_vec; /* inline vector */
Badari Pulavartyeed4e512006-09-30 23:28:49 -070054 struct iovec *ki_iovec;
55 unsigned long ki_nr_segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Benjamin LaHaise59d91362006-01-08 01:04:34 -080057 struct list_head ki_list; /* the aio core uses this
58 * for cancellation */
Davide Libenzi9c3060b2007-05-10 22:23:21 -070059
60 /*
61 * If the aio_resfd field of the userspace iocb is not zero,
Davide Libenzi13389012009-06-30 11:41:11 -070062 * this is the underlying eventfd context to deliver events to.
Davide Libenzi9c3060b2007-05-10 22:23:21 -070063 */
Davide Libenzi13389012009-06-30 11:41:11 -070064 struct eventfd_ctx *ki_eventfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Andrew Mortonf7e1bec2012-07-30 14:42:56 -070067static inline bool is_sync_kiocb(struct kiocb *kiocb)
68{
Kent Overstreet8a660892013-05-07 16:19:10 -070069 return kiocb->ki_ctx == NULL;
Andrew Mortonf7e1bec2012-07-30 14:42:56 -070070}
71
72static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
73{
74 *kiocb = (struct kiocb) {
Kent Overstreet11599eb2013-05-07 16:18:39 -070075 .ki_users = ATOMIC_INIT(1),
Kent Overstreet8a660892013-05-07 16:19:10 -070076 .ki_ctx = NULL,
Andrew Mortonf7e1bec2012-07-30 14:42:56 -070077 .ki_filp = filp,
78 .ki_obj.tsk = current,
79 };
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* prototypes */
Thomas Petazzoniebf3f092008-10-15 22:05:12 -070083#ifdef CONFIG_AIO
Harvey Harrisonb3c97522008-02-13 15:03:15 -080084extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
Kent Overstreet2d684492013-05-07 16:18:29 -070085extern void aio_put_req(struct kiocb *iocb);
86extern void aio_complete(struct kiocb *iocb, long res, long res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct mm_struct;
Harvey Harrisonb3c97522008-02-13 15:03:15 -080088extern void exit_aio(struct mm_struct *mm);
Jeff Moyer9d85cba2010-05-26 14:44:26 -070089extern long do_io_submit(aio_context_t ctx_id, long nr,
90 struct iocb __user *__user *iocbpp, bool compat);
Kent Overstreet0460fef2013-05-07 16:18:49 -070091void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel);
Thomas Petazzoniebf3f092008-10-15 22:05:12 -070092#else
93static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
Kent Overstreet2d684492013-05-07 16:18:29 -070094static inline void aio_put_req(struct kiocb *iocb) { }
95static inline void aio_complete(struct kiocb *iocb, long res, long res2) { }
Thomas Petazzoniebf3f092008-10-15 22:05:12 -070096struct mm_struct;
97static inline void exit_aio(struct mm_struct *mm) { }
Jeff Moyer9d85cba2010-05-26 14:44:26 -070098static inline long do_io_submit(aio_context_t ctx_id, long nr,
99 struct iocb __user * __user *iocbpp,
100 bool compat) { return 0; }
Kent Overstreet0460fef2013-05-07 16:18:49 -0700101static inline void kiocb_set_cancel_fn(struct kiocb *req,
102 kiocb_cancel_fn *cancel) { }
Thomas Petazzoniebf3f092008-10-15 22:05:12 -0700103#endif /* CONFIG_AIO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static inline struct kiocb *list_kiocb(struct list_head *h)
106{
107 return list_entry(h, struct kiocb, ki_list);
108}
109
110/* for sysctl: */
Zach Brownd55b5fd2005-11-07 00:59:31 -0800111extern unsigned long aio_nr;
112extern unsigned long aio_max_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#endif /* __LINUX__AIO_H */