c1defd3309bc0466226b391de7ec1dc4bd421f34
[linux-flexiantxendom0-3.2.10.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
8 #include <asm/atomic.h>
9
10 #define AIO_MAXSEGS             4
11 #define AIO_KIOGRP_NR_ATOMIC    8
12
13 struct kioctx;
14
15 /* Notes on cancelling a kiocb:
16  *      If a kiocb is cancelled, aio_complete may return 0 to indicate 
17  *      that cancel has not yet disposed of the kiocb.  All cancel 
18  *      operations *must* call aio_put_req to dispose of the kiocb 
19  *      to guard against races with the completion code.
20  */
21 #define KIOCB_C_CANCELLED       0x01
22 #define KIOCB_C_COMPLETE        0x02
23
24 #define KIOCB_SYNC_KEY          (~0U)
25
26 #define KIOCB_PRIVATE_SIZE      (24 * sizeof(long))
27
28 /* ki_flags bits */
29 #define KIF_LOCKED              0
30 #define KIF_KICKED              1
31 #define KIF_CANCELLED           2
32
33 #define kiocbTryLock(iocb)      test_and_set_bit(KIF_LOCKED, &(iocb)->ki_flags)
34 #define kiocbTryKick(iocb)      test_and_set_bit(KIF_KICKED, &(iocb)->ki_flags)
35
36 #define kiocbSetLocked(iocb)    set_bit(KIF_LOCKED, &(iocb)->ki_flags)
37 #define kiocbSetKicked(iocb)    set_bit(KIF_KICKED, &(iocb)->ki_flags)
38 #define kiocbSetCancelled(iocb) set_bit(KIF_CANCELLED, &(iocb)->ki_flags)
39
40 #define kiocbClearLocked(iocb)  clear_bit(KIF_LOCKED, &(iocb)->ki_flags)
41 #define kiocbClearKicked(iocb)  clear_bit(KIF_KICKED, &(iocb)->ki_flags)
42 #define kiocbClearCancelled(iocb)       clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)
43
44 #define kiocbIsLocked(iocb)     test_bit(KIF_LOCKED, &(iocb)->ki_flags)
45 #define kiocbIsKicked(iocb)     test_bit(KIF_KICKED, &(iocb)->ki_flags)
46 #define kiocbIsCancelled(iocb)  test_bit(KIF_CANCELLED, &(iocb)->ki_flags)
47
48 struct kiocb {
49         struct list_head        ki_run_list;
50         long                    ki_flags;
51         int                     ki_users;
52         unsigned                ki_key;         /* id of this request */
53
54         struct file             *ki_filp;
55         struct kioctx           *ki_ctx;        /* may be NULL for sync ops */
56         int                     (*ki_cancel)(struct kiocb *, struct io_event *);
57         ssize_t                 (*ki_retry)(struct kiocb *);
58
59         struct list_head        ki_list;        /* the aio core uses this
60                                                  * for cancellation */
61
62         void                    *ki_user_obj;   /* pointer to userland's iocb */
63         __u64                   ki_user_data;   /* user's data for completion */
64         loff_t                  ki_pos;
65
66         /* State that we remember to be able to restart/retry  */
67         unsigned short          ki_opcode;
68         size_t                  ki_nbytes;      /* copy of iocb->aio_nbytes */
69         char                    *ki_buf;        /* remaining iocb->aio_buf */
70         size_t                  ki_left;        /* remaining bytes */
71         wait_queue_t            ki_wait;
72         long                    ki_retried;     /* just for testing */
73         long                    ki_kicked;      /* just for testing */
74         long                    ki_queued;      /* just for testing */
75
76         char                    private[KIOCB_PRIVATE_SIZE];
77 };
78
79 #define is_sync_kiocb(iocb)     ((iocb)->ki_key == KIOCB_SYNC_KEY)
80 #define init_sync_kiocb(x, filp)                        \
81         do {                                            \
82                 struct task_struct *tsk = current;      \
83                 (x)->ki_flags = 0;                      \
84                 (x)->ki_users = 1;                      \
85                 (x)->ki_key = KIOCB_SYNC_KEY;           \
86                 (x)->ki_filp = (filp);                  \
87                 (x)->ki_ctx = &tsk->active_mm->default_kioctx;  \
88                 (x)->ki_cancel = NULL;                  \
89                 (x)->ki_user_obj = tsk;                 \
90                 (x)->ki_user_data = 0;                  \
91                 init_wait((&(x)->ki_wait));             \
92         } while (0)
93
94 #define AIO_RING_MAGIC                  0xa10a10a1
95 #define AIO_RING_COMPAT_FEATURES        1
96 #define AIO_RING_INCOMPAT_FEATURES      0
97 struct aio_ring {
98         unsigned        id;     /* kernel internal index number */
99         unsigned        nr;     /* number of io_events */
100         unsigned        head;
101         unsigned        tail;
102
103         unsigned        magic;
104         unsigned        compat_features;
105         unsigned        incompat_features;
106         unsigned        header_length;  /* size of aio_ring */
107
108
109         struct io_event         io_events[0];
110 }; /* 128 bytes + ring size */
111
112 #define aio_ring_avail(info, ring)      (((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr)
113
114 #define AIO_RING_PAGES  8
115 struct aio_ring_info {
116         unsigned long           mmap_base;
117         unsigned long           mmap_size;
118
119         struct page             **ring_pages;
120         spinlock_t              ring_lock;
121         long                    nr_pages;
122
123         unsigned                nr, tail;
124
125         struct page             *internal_pages[AIO_RING_PAGES];
126 };
127
128 struct kioctx {
129         atomic_t                users;
130         int                     dead;
131         struct mm_struct        *mm;
132
133         /* This needs improving */
134         unsigned long           user_id;
135         struct kioctx           *next;
136
137         wait_queue_head_t       wait;
138
139         spinlock_t              ctx_lock;
140
141         int                     reqs_active;
142         struct list_head        active_reqs;    /* used for cancellation */
143         struct list_head        run_list;       /* used for kicked reqs */
144
145         unsigned                max_reqs;
146
147         struct aio_ring_info    ring_info;
148
149         struct work_struct      wq;
150 };
151
152 /* prototypes */
153 extern unsigned aio_max_size;
154
155 extern ssize_t FASTCALL(wait_on_sync_kiocb(struct kiocb *iocb));
156 extern int FASTCALL(aio_put_req(struct kiocb *iocb));
157 extern void FASTCALL(kick_iocb(struct kiocb *iocb));
158 extern int FASTCALL(aio_complete(struct kiocb *iocb, long res, long res2));
159 extern void FASTCALL(__put_ioctx(struct kioctx *ctx));
160 struct mm_struct;
161 extern void FASTCALL(exit_aio(struct mm_struct *mm));
162 extern struct kioctx *lookup_ioctx(unsigned long ctx_id);
163 extern int FASTCALL(io_submit_one(struct kioctx *ctx,
164                         struct iocb *user_iocb, struct iocb *iocb));
165
166 /* semi private, but used by the 32bit emulations: */
167 struct kioctx *lookup_ioctx(unsigned long ctx_id);
168 int FASTCALL(io_submit_one(struct kioctx *ctx, struct iocb *user_iocb,
169                                   struct iocb *iocb));
170
171 #define get_ioctx(kioctx)       do { if (unlikely(atomic_read(&(kioctx)->users) <= 0)) BUG(); atomic_inc(&(kioctx)->users); } while (0)
172 #define put_ioctx(kioctx)       do { if (unlikely(atomic_dec_and_test(&(kioctx)->users))) __put_ioctx(kioctx); else if (unlikely(atomic_read(&(kioctx)->users) < 0)) BUG(); } while (0)
173
174 #define in_aio() !is_sync_wait(current->io_wait)
175 /* may be used for debugging */
176 #define warn_if_async() if (in_aio()) {\
177         printk(KERN_ERR "%s(%s:%d) called in async context!\n", \
178         __FUNCTION__, __FILE__, __LINE__); \
179         dump_stack(); \
180         }
181
182 #define io_wait_to_kiocb(wait) container_of(wait, struct kiocb, ki_wait)
183 #define is_retried_kiocb(iocb) ((iocb)->ki_retried > 1)
184
185 #include <linux/aio_abi.h>
186
187 static inline struct kiocb *list_kiocb(struct list_head *h)
188 {
189         return list_entry(h, struct kiocb, ki_list);
190 }
191
192 /* for sysctl: */
193 extern unsigned aio_max_nr, aio_max_size, aio_max_pinned;
194
195 #endif /* __LINUX__AIO_H */