patch-2_6_7-vs1_9_1_12
[linux-2.6.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         long                    (*ki_retry)(struct kiocb *);
58
59         struct list_head        ki_list;        /* the aio core uses this
60                                                  * for cancellation */
61
62         union {
63                 void __user             *user;
64                 struct task_struct      *tsk;
65         } ki_obj;
66         __u64                   ki_user_data;   /* user's data for completion */
67         loff_t                  ki_pos;
68
69         char                    private[KIOCB_PRIVATE_SIZE];
70 };
71
72 #define is_sync_kiocb(iocb)     ((iocb)->ki_key == KIOCB_SYNC_KEY)
73 #define init_sync_kiocb(x, filp)                        \
74         do {                                            \
75                 struct task_struct *tsk = current;      \
76                 (x)->ki_flags = 0;                      \
77                 (x)->ki_users = 1;                      \
78                 (x)->ki_key = KIOCB_SYNC_KEY;           \
79                 (x)->ki_filp = (filp);                  \
80                 (x)->ki_ctx = &tsk->active_mm->default_kioctx;  \
81                 (x)->ki_cancel = NULL;                  \
82                 (x)->ki_obj.tsk = tsk;                  \
83         } while (0)
84
85 #define AIO_RING_MAGIC                  0xa10a10a1
86 #define AIO_RING_COMPAT_FEATURES        1
87 #define AIO_RING_INCOMPAT_FEATURES      0
88 struct aio_ring {
89         unsigned        id;     /* kernel internal index number */
90         unsigned        nr;     /* number of io_events */
91         unsigned        head;
92         unsigned        tail;
93
94         unsigned        magic;
95         unsigned        compat_features;
96         unsigned        incompat_features;
97         unsigned        header_length;  /* size of aio_ring */
98
99
100         struct io_event         io_events[0];
101 }; /* 128 bytes + ring size */
102
103 #define aio_ring_avail(info, ring)      (((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr)
104
105 #define AIO_RING_PAGES  8
106 struct aio_ring_info {
107         unsigned long           mmap_base;
108         unsigned long           mmap_size;
109
110         struct page             **ring_pages;
111         spinlock_t              ring_lock;
112         long                    nr_pages;
113
114         unsigned                nr, tail;
115
116         struct page             *internal_pages[AIO_RING_PAGES];
117 };
118
119 struct kioctx {
120         atomic_t                users;
121         int                     dead;
122         struct mm_struct        *mm;
123
124         /* This needs improving */
125         unsigned long           user_id;
126         struct kioctx           *next;
127
128         wait_queue_head_t       wait;
129
130         spinlock_t              ctx_lock;
131
132         int                     reqs_active;
133         struct list_head        active_reqs;    /* used for cancellation */
134         struct list_head        run_list;       /* used for kicked reqs */
135
136         unsigned                max_reqs;
137
138         struct aio_ring_info    ring_info;
139
140         struct work_struct      wq;
141 };
142
143 /* prototypes */
144 extern unsigned aio_max_size;
145
146 extern ssize_t FASTCALL(wait_on_sync_kiocb(struct kiocb *iocb));
147 extern int FASTCALL(aio_put_req(struct kiocb *iocb));
148 extern void FASTCALL(kick_iocb(struct kiocb *iocb));
149 extern int FASTCALL(aio_complete(struct kiocb *iocb, long res, long res2));
150 extern void FASTCALL(__put_ioctx(struct kioctx *ctx));
151 struct mm_struct;
152 extern void FASTCALL(exit_aio(struct mm_struct *mm));
153 extern struct kioctx *lookup_ioctx(unsigned long ctx_id);
154 extern int FASTCALL(io_submit_one(struct kioctx *ctx,
155                         struct iocb __user *user_iocb, struct iocb *iocb));
156
157 /* semi private, but used by the 32bit emulations: */
158 struct kioctx *lookup_ioctx(unsigned long ctx_id);
159 int FASTCALL(io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
160                                   struct iocb *iocb));
161
162 #define get_ioctx(kioctx)       do { if (unlikely(atomic_read(&(kioctx)->users) <= 0)) BUG(); atomic_inc(&(kioctx)->users); } while (0)
163 #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)
164
165 #include <linux/aio_abi.h>
166
167 static inline struct kiocb *list_kiocb(struct list_head *h)
168 {
169         return list_entry(h, struct kiocb, ki_list);
170 }
171
172 /* for sysctl: */
173 extern atomic_t aio_nr;
174 extern unsigned aio_max_nr;
175
176 #endif /* __LINUX__AIO_H */