Blame view

include/linux/aio.h 8.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
  #ifndef __LINUX__AIO_H
  #define __LINUX__AIO_H
  
  #include <linux/list.h>
  #include <linux/workqueue.h>
  #include <linux/aio_abi.h>
027445c37   Badari Pulavarty   [PATCH] Vectorize...
7
  #include <linux/uio.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
  
  #include <asm/atomic.h>
eed4e51fb   Badari Pulavarty   [PATCH] Add vecto...
10
  #include <linux/uio.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  
  #define AIO_MAXSEGS		4
  #define AIO_KIOGRP_NR_ATOMIC	8
  
  struct kioctx;
  
  /* Notes on cancelling a kiocb:
   *	If a kiocb is cancelled, aio_complete may return 0 to indicate 
   *	that cancel has not yet disposed of the kiocb.  All cancel 
   *	operations *must* call aio_put_req to dispose of the kiocb 
   *	to guard against races with the completion code.
   */
  #define KIOCB_C_CANCELLED	0x01
  #define KIOCB_C_COMPLETE	0x02
  
  #define KIOCB_SYNC_KEY		(~0U)
  
  /* ki_flags bits */
4faa52852   Zach Brown   [PATCH] aio: reve...
29
30
31
32
33
34
  /*
   * This may be used for cancel/retry serialization in the future, but
   * for now it's unused and we probably don't want modules to even
   * think they can use it.
   */
  /* #define KIF_LOCKED		0 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #define KIF_KICKED		1
  #define KIF_CANCELLED		2
  
  #define kiocbTryLock(iocb)	test_and_set_bit(KIF_LOCKED, &(iocb)->ki_flags)
  #define kiocbTryKick(iocb)	test_and_set_bit(KIF_KICKED, &(iocb)->ki_flags)
  
  #define kiocbSetLocked(iocb)	set_bit(KIF_LOCKED, &(iocb)->ki_flags)
  #define kiocbSetKicked(iocb)	set_bit(KIF_KICKED, &(iocb)->ki_flags)
  #define kiocbSetCancelled(iocb)	set_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  
  #define kiocbClearLocked(iocb)	clear_bit(KIF_LOCKED, &(iocb)->ki_flags)
  #define kiocbClearKicked(iocb)	clear_bit(KIF_KICKED, &(iocb)->ki_flags)
  #define kiocbClearCancelled(iocb)	clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)
  
  #define kiocbIsLocked(iocb)	test_bit(KIF_LOCKED, &(iocb)->ki_flags)
  #define kiocbIsKicked(iocb)	test_bit(KIF_KICKED, &(iocb)->ki_flags)
  #define kiocbIsCancelled(iocb)	test_bit(KIF_CANCELLED, &(iocb)->ki_flags)
897f15fb5   Zach Brown   [PATCH] aio: remo...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  /* is there a better place to document function pointer methods? */
  /**
   * ki_retry	-	iocb forward progress callback
   * @kiocb:	The kiocb struct to advance by performing an operation.
   *
   * This callback is called when the AIO core wants a given AIO operation
   * to make forward progress.  The kiocb argument describes the operation
   * that is to be performed.  As the operation proceeds, perhaps partially,
   * ki_retry is expected to update the kiocb with progress made.  Typically
   * ki_retry is set in the AIO core and it itself calls file_operations
   * helpers.
   *
   * ki_retry's return value determines when the AIO operation is completed
   * and an event is generated in the AIO event ring.  Except the special
   * return values described below, the value that is returned from ki_retry
   * is transferred directly into the completion ring as the operation's
   * resulting status.  Once this has happened ki_retry *MUST NOT* reference
   * the kiocb pointer again.
   *
   * If ki_retry returns -EIOCBQUEUED it has made a promise that aio_complete()
   * will be called on the kiocb pointer in the future.  The AIO core will
   * not ask the method again -- ki_retry must ensure forward progress.
   * aio_complete() must be called once and only once in the future, multiple
   * calls may result in undefined behaviour.
   *
   * If ki_retry returns -EIOCBRETRY it has made a promise that kick_iocb()
   * will be called on the kiocb pointer in the future.  This may happen
   * through generic helpers that associate kiocb->ki_wait with a wait
   * queue head that ki_retry uses via current->io_wait.  It can also happen
   * with custom tracking and manual calls to kick_iocb(), though that is
   * discouraged.  In either case, kick_iocb() must be called once and only
   * once.  ki_retry must ensure forward progress, the AIO core will wait
   * indefinitely for kick_iocb() to be called.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
91
92
93
94
95
96
  struct kiocb {
  	struct list_head	ki_run_list;
  	long			ki_flags;
  	int			ki_users;
  	unsigned		ki_key;		/* id of this request */
  
  	struct file		*ki_filp;
  	struct kioctx		*ki_ctx;	/* may be NULL for sync ops */
  	int			(*ki_cancel)(struct kiocb *, struct io_event *);
  	ssize_t			(*ki_retry)(struct kiocb *);
  	void			(*ki_dtor)(struct kiocb *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
100
  	union {
  		void __user		*user;
  		struct task_struct	*tsk;
  	} ki_obj;
59d9136b9   Benjamin LaHaise   [PATCH] aio: reor...
101

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  	__u64			ki_user_data;	/* user's data for completion */
59d9136b9   Benjamin LaHaise   [PATCH] aio: reor...
103
  	wait_queue_t		ki_wait;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  	loff_t			ki_pos;
59d9136b9   Benjamin LaHaise   [PATCH] aio: reor...
105

e61c90188   Kenneth W Chen   [PATCH] optimize ...
106
  	atomic_t		ki_bio_count;	/* num bio used for this iocb */
59d9136b9   Benjamin LaHaise   [PATCH] aio: reor...
107
  	void			*private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
109
110
111
112
  	/* State that we remember to be able to restart/retry  */
  	unsigned short		ki_opcode;
  	size_t			ki_nbytes; 	/* copy of iocb->aio_nbytes */
  	char 			__user *ki_buf;	/* remaining iocb->aio_buf */
  	size_t			ki_left; 	/* remaining bytes */
027445c37   Badari Pulavarty   [PATCH] Vectorize...
113
  	struct iovec		ki_inline_vec;	/* inline vector */
eed4e51fb   Badari Pulavarty   [PATCH] Add vecto...
114
115
116
   	struct iovec		*ki_iovec;
   	unsigned long		ki_nr_segs;
   	unsigned long		ki_cur_seg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117

59d9136b9   Benjamin LaHaise   [PATCH] aio: reor...
118
119
  	struct list_head	ki_list;	/* the aio core uses this
  						 * for cancellation */
9c3060bed   Davide Libenzi   signal/timer/even...
120
121
122
123
124
125
  
  	/*
  	 * If the aio_resfd field of the userspace iocb is not zero,
  	 * this is the underlying file* to deliver event to.
  	 */
  	struct file		*ki_eventfd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
130
131
132
133
134
135
  };
  
  #define is_sync_kiocb(iocb)	((iocb)->ki_key == KIOCB_SYNC_KEY)
  #define init_sync_kiocb(x, filp)			\
  	do {						\
  		struct task_struct *tsk = current;	\
  		(x)->ki_flags = 0;			\
  		(x)->ki_users = 1;			\
  		(x)->ki_key = KIOCB_SYNC_KEY;		\
  		(x)->ki_filp = (filp);			\
20dcae324   Zach Brown   [PATCH] aio: remo...
136
  		(x)->ki_ctx = NULL;			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
  		(x)->ki_cancel = NULL;			\
59d9136b9   Benjamin LaHaise   [PATCH] aio: reor...
138
  		(x)->ki_retry = NULL;			\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  		(x)->ki_dtor = NULL;			\
  		(x)->ki_obj.tsk = tsk;			\
  		(x)->ki_user_data = 0;                  \
  		init_wait((&(x)->ki_wait));             \
  	} while (0)
  
  #define AIO_RING_MAGIC			0xa10a10a1
  #define AIO_RING_COMPAT_FEATURES	1
  #define AIO_RING_INCOMPAT_FEATURES	0
  struct aio_ring {
  	unsigned	id;	/* kernel internal index number */
  	unsigned	nr;	/* number of io_events */
  	unsigned	head;
  	unsigned	tail;
  
  	unsigned	magic;
  	unsigned	compat_features;
  	unsigned	incompat_features;
  	unsigned	header_length;	/* size of aio_ring */
  
  
  	struct io_event		io_events[0];
  }; /* 128 bytes + ring size */
  
  #define aio_ring_avail(info, ring)	(((ring)->head + (info)->nr - 1 - (ring)->tail) % (info)->nr)
  
  #define AIO_RING_PAGES	8
  struct aio_ring_info {
  	unsigned long		mmap_base;
  	unsigned long		mmap_size;
  
  	struct page		**ring_pages;
  	spinlock_t		ring_lock;
  	long			nr_pages;
  
  	unsigned		nr, tail;
  
  	struct page		*internal_pages[AIO_RING_PAGES];
  };
  
  struct kioctx {
  	atomic_t		users;
  	int			dead;
  	struct mm_struct	*mm;
  
  	/* This needs improving */
  	unsigned long		user_id;
  	struct kioctx		*next;
  
  	wait_queue_head_t	wait;
  
  	spinlock_t		ctx_lock;
  
  	int			reqs_active;
  	struct list_head	active_reqs;	/* used for cancellation */
  	struct list_head	run_list;	/* used for kicked reqs */
d55b5fdaf   Zach Brown   [PATCH] aio: remo...
195
  	/* sys_io_setup currently limits this to an unsigned int */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
  	unsigned		max_reqs;
  
  	struct aio_ring_info	ring_info;
52bad64d9   David Howells   WorkStruct: Separ...
199
  	struct delayed_work	wq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  };
  
  /* prototypes */
  extern unsigned aio_max_size;
  
  extern ssize_t FASTCALL(wait_on_sync_kiocb(struct kiocb *iocb));
  extern int FASTCALL(aio_put_req(struct kiocb *iocb));
  extern void FASTCALL(kick_iocb(struct kiocb *iocb));
  extern int FASTCALL(aio_complete(struct kiocb *iocb, long res, long res2));
  extern void FASTCALL(__put_ioctx(struct kioctx *ctx));
  struct mm_struct;
  extern void FASTCALL(exit_aio(struct mm_struct *mm));
  extern struct kioctx *lookup_ioctx(unsigned long ctx_id);
  extern int FASTCALL(io_submit_one(struct kioctx *ctx,
  			struct iocb __user *user_iocb, struct iocb *iocb));
  
  /* semi private, but used by the 32bit emulations: */
  struct kioctx *lookup_ioctx(unsigned long ctx_id);
  int FASTCALL(io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  				  struct iocb *iocb));
5ef1c49f8   Zach Brown   [PATCH] aio: don'...
220
  #define get_ioctx(kioctx) do {						\
3a2711116   Rolf Eike Beer   [PATCH] Remove BU...
221
  	BUG_ON(atomic_read(&(kioctx)->users) <= 0);			\
5ef1c49f8   Zach Brown   [PATCH] aio: don'...
222
223
224
  	atomic_inc(&(kioctx)->users);					\
  } while (0)
  #define put_ioctx(kioctx) do {						\
3a2711116   Rolf Eike Beer   [PATCH] Remove BU...
225
  	BUG_ON(atomic_read(&(kioctx)->users) <= 0);			\
5ef1c49f8   Zach Brown   [PATCH] aio: don'...
226
227
228
  	if (unlikely(atomic_dec_and_test(&(kioctx)->users))) 		\
  		__put_ioctx(kioctx);					\
  } while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229

b8522ead3   Andrew Morton   aio is unlikely
230
  #define in_aio() (unlikely(!is_sync_wait(current->io_wait)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
232
233
234
235
236
237
238
239
240
241
242
  /* may be used for debugging */
  #define warn_if_async()							\
  do {									\
  	if (in_aio()) {							\
  		printk(KERN_ERR "%s(%s:%d) called in async context!
  ",	\
  			__FUNCTION__, __FILE__, __LINE__);		\
  		dump_stack();						\
  	}								\
  } while (0)
  
  #define io_wait_to_kiocb(wait) container_of(wait, struct kiocb, ki_wait)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
244
245
246
247
248
249
250
251
  
  #include <linux/aio_abi.h>
  
  static inline struct kiocb *list_kiocb(struct list_head *h)
  {
  	return list_entry(h, struct kiocb, ki_list);
  }
  
  /* for sysctl: */
d55b5fdaf   Zach Brown   [PATCH] aio: remo...
252
253
  extern unsigned long aio_nr;
  extern unsigned long aio_max_nr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
  
  #endif /* __LINUX__AIO_H */