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