aio.h 5.5 KB
Newer Older
L
Linus Torvalds 已提交
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>
7
#include <linux/uio.h>
J
Jens Axboe 已提交
8
#include <linux/rcupdate.h>
L
Linus Torvalds 已提交
9

A
Arun Sharma 已提交
10
#include <linux/atomic.h>
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24

struct kioctx;

#define KIOCB_SYNC_KEY		(~0U)

/* ki_flags bits */
#define KIF_CANCELLED		2

#define kiocbSetCancelled(iocb)	set_bit(KIF_CANCELLED, &(iocb)->ki_flags)

#define kiocbClearCancelled(iocb)	clear_bit(KIF_CANCELLED, &(iocb)->ki_flags)

#define kiocbIsCancelled(iocb)	test_bit(KIF_CANCELLED, &(iocb)->ki_flags)

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/* 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.
 */
L
Linus Torvalds 已提交
50
struct kiocb {
51
	unsigned long		ki_flags;
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
	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 *);

	union {
		void __user		*user;
		struct task_struct	*tsk;
	} ki_obj;
65

L
Linus Torvalds 已提交
66 67
	__u64			ki_user_data;	/* user's data for completion */
	loff_t			ki_pos;
68 69

	void			*private;
L
Linus Torvalds 已提交
70 71 72 73 74
	/* 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 */
75
	struct iovec		ki_inline_vec;	/* inline vector */
B
Badari Pulavarty 已提交
76 77 78
 	struct iovec		*ki_iovec;
 	unsigned long		ki_nr_segs;
 	unsigned long		ki_cur_seg;
L
Linus Torvalds 已提交
79

80 81
	struct list_head	ki_list;	/* the aio core uses this
						 * for cancellation */
J
Jeff Moyer 已提交
82
	struct list_head	ki_batch;	/* batch allocation */
83 84 85

	/*
	 * If the aio_resfd field of the userspace iocb is not zero,
86
	 * this is the underlying eventfd context to deliver events to.
87
	 */
88
	struct eventfd_ctx	*ki_eventfd;
L
Linus Torvalds 已提交
89 90
};

91 92 93 94 95 96 97 98 99 100 101 102 103 104
static inline bool is_sync_kiocb(struct kiocb *kiocb)
{
	return kiocb->ki_key == KIOCB_SYNC_KEY;
}

static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
{
	*kiocb = (struct kiocb) {
			.ki_users = 1,
			.ki_key = KIOCB_SYNC_KEY,
			.ki_filp = filp,
			.ki_obj.tsk = current,
		};
}
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

#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_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];
};

138 139 140 141 142 143
static inline unsigned aio_ring_avail(struct aio_ring_info *info,
					struct aio_ring *ring)
{
	return (ring->head + info->nr - 1 - ring->tail) % info->nr;
}

L
Linus Torvalds 已提交
144 145 146 147 148 149
struct kioctx {
	atomic_t		users;
	int			dead;

	/* This needs improving */
	unsigned long		user_id;
J
Jens Axboe 已提交
150
	struct hlist_node	list;
L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158

	wait_queue_head_t	wait;

	spinlock_t		ctx_lock;

	int			reqs_active;
	struct list_head	active_reqs;	/* used for cancellation */

159
	/* sys_io_setup currently limits this to an unsigned int */
L
Linus Torvalds 已提交
160 161 162 163
	unsigned		max_reqs;

	struct aio_ring_info	ring_info;

J
Jens Axboe 已提交
164
	struct rcu_head		rcu_head;
L
Linus Torvalds 已提交
165 166 167
};

/* prototypes */
T
Thomas Petazzoni 已提交
168
#ifdef CONFIG_AIO
169 170 171
extern ssize_t wait_on_sync_kiocb(struct kiocb *iocb);
extern int aio_put_req(struct kiocb *iocb);
extern int aio_complete(struct kiocb *iocb, long res, long res2);
L
Linus Torvalds 已提交
172
struct mm_struct;
173
extern void exit_aio(struct mm_struct *mm);
174 175
extern long do_io_submit(aio_context_t ctx_id, long nr,
			 struct iocb __user *__user *iocbpp, bool compat);
T
Thomas Petazzoni 已提交
176 177 178 179 180 181
#else
static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
static inline int aio_put_req(struct kiocb *iocb) { return 0; }
static inline int aio_complete(struct kiocb *iocb, long res, long res2) { return 0; }
struct mm_struct;
static inline void exit_aio(struct mm_struct *mm) { }
182 183 184
static inline long do_io_submit(aio_context_t ctx_id, long nr,
				struct iocb __user * __user *iocbpp,
				bool compat) { return 0; }
T
Thomas Petazzoni 已提交
185
#endif /* CONFIG_AIO */
L
Linus Torvalds 已提交
186 187 188 189 190 191 192

static inline struct kiocb *list_kiocb(struct list_head *h)
{
	return list_entry(h, struct kiocb, ki_list);
}

/* for sysctl: */
193 194
extern unsigned long aio_nr;
extern unsigned long aio_max_nr;
L
Linus Torvalds 已提交
195 196

#endif /* __LINUX__AIO_H */