7bb766e739687a67088a7a409b771973f622a8ff
[cascardo/linux.git] / include / linux / aio.h
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>
7 #include <linux/uio.h>
8 #include <linux/rcupdate.h>
9
10 #include <linux/atomic.h>
11
12 struct kioctx;
13 struct kiocb;
14
15 #define KIOCB_KEY               0
16
17 /*
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))
29
30 typedef int (kiocb_cancel_fn)(struct kiocb *);
31
32 struct kiocb {
33         atomic_t                ki_users;
34
35         struct file             *ki_filp;
36         struct kioctx           *ki_ctx;        /* NULL for sync ops */
37         kiocb_cancel_fn         *ki_cancel;
38         void                    (*ki_dtor)(struct kiocb *);
39
40         union {
41                 void __user             *user;
42                 struct task_struct      *tsk;
43         } ki_obj;
44
45         __u64                   ki_user_data;   /* user's data for completion */
46         loff_t                  ki_pos;
47
48         void                    *private;
49         /* 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 */
53         struct iovec            ki_inline_vec;  /* inline vector */
54         struct iovec            *ki_iovec;
55         unsigned long           ki_nr_segs;
56
57         struct list_head        ki_list;        /* the aio core uses this
58                                                  * for cancellation */
59
60         /*
61          * If the aio_resfd field of the userspace iocb is not zero,
62          * this is the underlying eventfd context to deliver events to.
63          */
64         struct eventfd_ctx      *ki_eventfd;
65 };
66
67 static inline bool is_sync_kiocb(struct kiocb *kiocb)
68 {
69         return kiocb->ki_ctx == NULL;
70 }
71
72 static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
73 {
74         *kiocb = (struct kiocb) {
75                         .ki_users = ATOMIC_INIT(1),
76                         .ki_ctx = NULL,
77                         .ki_filp = filp,
78                         .ki_obj.tsk = current,
79                 };
80 }
81
82 /* prototypes */
83 #ifdef CONFIG_AIO
84 extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
85 extern void aio_put_req(struct kiocb *iocb);
86 extern void aio_complete(struct kiocb *iocb, long res, long res2);
87 struct mm_struct;
88 extern void exit_aio(struct mm_struct *mm);
89 extern long do_io_submit(aio_context_t ctx_id, long nr,
90                          struct iocb __user *__user *iocbpp, bool compat);
91 void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel);
92 #else
93 static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
94 static inline void aio_put_req(struct kiocb *iocb) { }
95 static inline void aio_complete(struct kiocb *iocb, long res, long res2) { }
96 struct mm_struct;
97 static inline void exit_aio(struct mm_struct *mm) { }
98 static inline long do_io_submit(aio_context_t ctx_id, long nr,
99                                 struct iocb __user * __user *iocbpp,
100                                 bool compat) { return 0; }
101 static inline void kiocb_set_cancel_fn(struct kiocb *req,
102                                        kiocb_cancel_fn *cancel) { }
103 #endif /* CONFIG_AIO */
104
105 static inline struct kiocb *list_kiocb(struct list_head *h)
106 {
107         return list_entry(h, struct kiocb, ki_list);
108 }
109
110 /* for sysctl: */
111 extern unsigned long aio_nr;
112 extern unsigned long aio_max_nr;
113
114 #endif /* __LINUX__AIO_H */