xref: /netbsd-src/sys/sys/aio.h (revision f82d7874c259b2a6cc59b714f844919f32bf7b51)
1 /*	$NetBSD: aio.h,v 1.6 2007/11/28 19:30:55 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_AIO_H_
30 #define _SYS_AIO_H_
31 
32 /* Returned by aio_cancel() */
33 #define AIO_CANCELED		0x1
34 #define AIO_NOTCANCELED		0x2
35 #define AIO_ALLDONE		0x3
36 
37 /* LIO opcodes */
38 #define LIO_NOP			0x0
39 #define LIO_WRITE		0x1
40 #define LIO_READ		0x2
41 
42 /* LIO modes */
43 #define LIO_NOWAIT		0x0
44 #define LIO_WAIT		0x1
45 
46 /*
47  * Asynchronous I/O structure.
48  * Defined in the Base Definitions volume of IEEE Std 1003.1-2001 .
49  */
50 struct aiocb {
51 	off_t aio_offset;		/* File offset */
52 	volatile void *aio_buf;		/* I/O buffer in process space */
53 	size_t aio_nbytes;		/* Length of transfer */
54 	int aio_fildes;			/* File descriptor */
55 	int aio_lio_opcode;		/* LIO opcode */
56 	int aio_reqprio;		/* Request priority offset */
57 	struct sigevent aio_sigevent;	/* Signal to deliver */
58 
59 	/* Internal kernel variables */
60 	int _state;			/* State of the job */
61 	int _errno;			/* Error value */
62 	ssize_t _retval;		/* Return value */
63 };
64 
65 /* Internal kernel data */
66 #ifdef _KERNEL
67 
68 /* Default limits of allowed AIO operations */
69 #define AIO_LISTIO_MAX		512
70 #define AIO_MAX			AIO_LISTIO_MAX * 16
71 
72 #include <sys/condvar.h>
73 #include <sys/lwp.h>
74 #include <sys/mutex.h>
75 #include <sys/pool.h>
76 #include <sys/queue.h>
77 
78 /* Operations (as flags) */
79 #define AIO_LIO			0x00
80 #define AIO_READ		0x01
81 #define AIO_WRITE		0x02
82 #define AIO_SYNC		0x04
83 #define AIO_DSYNC		0x08
84 
85 /* Job states */
86 #define JOB_NONE		0x0
87 #define JOB_WIP			0x1
88 #define JOB_DONE		0x2
89 
90 /* Structure of AIO job */
91 struct aio_job {
92 	int aio_op;		/* Operation code */
93 	struct aiocb aiocbp;	/* AIO data structure */
94 	void *aiocb_uptr;	/* User-space pointer for identification of job */
95 	TAILQ_ENTRY(aio_job) list;
96 	struct lio_req *lio;
97 };
98 
99 /* LIO structure */
100 struct lio_req {
101 	u_int refcnt;		/* Reference counter */
102 	struct sigevent sig;	/* Signal of lio_listio() calls */
103 };
104 
105 /* Structure of AIO data for process */
106 struct aioproc {
107 	kmutex_t aio_mtx;		/* Protects the entire structure */
108 	kcondvar_t aio_worker_cv;	/* Signals on a new job */
109 	kcondvar_t done_cv;		/* Signals when the job is done */
110 	struct aio_job *curjob;		/* Currently processing AIO job */
111 	unsigned int jobs_count;	/* Count of the jobs */
112 	TAILQ_HEAD(, aio_job) jobs_queue;/* Queue of the AIO jobs */
113 	struct lwp *aio_worker;		/* AIO worker thread */
114 };
115 
116 /* Prototypes */
117 void	aio_sysinit(void);
118 int	aio_init(struct proc *);
119 void	aio_exit(struct proc *, struct aioproc *);
120 void	aio_print_jobs(void (*pr)(const char *, ...));
121 
122 #endif /* _KERNEL */
123 
124 #endif /* _SYS_AIO_H_ */
125