1*2248Sraf /* 2*2248Sraf * CDDL HEADER START 3*2248Sraf * 4*2248Sraf * The contents of this file are subject to the terms of the 5*2248Sraf * Common Development and Distribution License (the "License"). 6*2248Sraf * You may not use this file except in compliance with the License. 7*2248Sraf * 8*2248Sraf * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*2248Sraf * or http://www.opensolaris.org/os/licensing. 10*2248Sraf * See the License for the specific language governing permissions 11*2248Sraf * and limitations under the License. 12*2248Sraf * 13*2248Sraf * When distributing Covered Code, include this CDDL HEADER in each 14*2248Sraf * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*2248Sraf * If applicable, add the following below this CDDL HEADER, with the 16*2248Sraf * fields enclosed by brackets "[]" replaced with your own identifying 17*2248Sraf * information: Portions Copyright [yyyy] [name of copyright owner] 18*2248Sraf * 19*2248Sraf * CDDL HEADER END 20*2248Sraf */ 21*2248Sraf 22*2248Sraf /* 23*2248Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24*2248Sraf * Use is subject to license terms. 25*2248Sraf */ 26*2248Sraf 27*2248Sraf #ifndef _ASYNCIO_H 28*2248Sraf #define _ASYNCIO_H 29*2248Sraf 30*2248Sraf #pragma ident "%Z%%M% %I% %E% SMI" 31*2248Sraf 32*2248Sraf #ifdef __cplusplus 33*2248Sraf extern "C" { 34*2248Sraf #endif 35*2248Sraf 36*2248Sraf #include <stdio.h> 37*2248Sraf #include <stdlib.h> 38*2248Sraf #include <unistd.h> 39*2248Sraf #include <string.h> 40*2248Sraf #include <errno.h> 41*2248Sraf #include <sys/types.h> 42*2248Sraf #include <sys/stat.h> 43*2248Sraf #include <thread.h> 44*2248Sraf #include <pthread.h> 45*2248Sraf #include <setjmp.h> 46*2248Sraf #include <signal.h> 47*2248Sraf #include <siginfo.h> 48*2248Sraf #include <aio.h> 49*2248Sraf #include <limits.h> 50*2248Sraf #include <ucontext.h> 51*2248Sraf #include <sys/asynch.h> 52*2248Sraf #include <sys/mman.h> 53*2248Sraf 54*2248Sraf #if !defined(_LP64) 55*2248Sraf #define AIOSTKSIZE (64 * 1024) 56*2248Sraf #else 57*2248Sraf #define AIOSTKSIZE (128 * 1024) 58*2248Sraf #endif 59*2248Sraf 60*2248Sraf #define SIGAIOCANCEL SIGLWP /* special aio cancelation signal */ 61*2248Sraf 62*2248Sraf #define AIO_WAITN_MAXIOCBS 32768 /* max. iocbs per system call */ 63*2248Sraf 64*2248Sraf /* 65*2248Sraf * Declare structure types. The structures themselves are defined below. 66*2248Sraf */ 67*2248Sraf typedef struct aio_args aio_args_t; 68*2248Sraf typedef struct aio_lio aio_lio_t; 69*2248Sraf typedef struct notif_param notif_param_t; 70*2248Sraf typedef struct aio_req aio_req_t; 71*2248Sraf typedef struct aio_worker aio_worker_t; 72*2248Sraf typedef struct aio_hash aio_hash_t; 73*2248Sraf 74*2248Sraf struct aio_args { 75*2248Sraf int fd; 76*2248Sraf caddr_t buf; 77*2248Sraf size_t bufsz; 78*2248Sraf offset_t offset; 79*2248Sraf }; 80*2248Sraf 81*2248Sraf /* 82*2248Sraf * list head for UFS list I/O 83*2248Sraf */ 84*2248Sraf struct aio_lio { 85*2248Sraf mutex_t lio_mutex; /* list mutex */ 86*2248Sraf cond_t lio_cond_cv; /* list notification for I/O done */ 87*2248Sraf aio_lio_t *lio_next; /* pointer to next on freelist */ 88*2248Sraf char lio_mode; /* LIO_WAIT/LIO_NOWAIT */ 89*2248Sraf char lio_canned; /* lio was canceled */ 90*2248Sraf char lio_largefile; /* largefile operation */ 91*2248Sraf char lio_waiting; /* waiting in __lio_listio() */ 92*2248Sraf int lio_nent; /* Number of list I/O's */ 93*2248Sraf int lio_refcnt; /* outstanding I/O's */ 94*2248Sraf int lio_event; /* Event number for notification */ 95*2248Sraf int lio_port; /* Port number for notification */ 96*2248Sraf int lio_signo; /* Signal number for notification */ 97*2248Sraf union sigval lio_sigval; /* Signal parameter */ 98*2248Sraf uintptr_t lio_object; /* for SIGEV_THREAD or SIGEV_PORT */ 99*2248Sraf struct sigevent *lio_sigevent; /* Notification function and attr. */ 100*2248Sraf }; 101*2248Sraf 102*2248Sraf /* 103*2248Sraf * Notification parameters 104*2248Sraf */ 105*2248Sraf struct notif_param { 106*2248Sraf int np_signo; /* SIGEV_SIGNAL */ 107*2248Sraf int np_port; /* SIGEV_THREAD or SIGEV_PORT */ 108*2248Sraf void *np_user; 109*2248Sraf int np_event; 110*2248Sraf uintptr_t np_object; 111*2248Sraf int np_lio_signo; /* listio: SIGEV_SIGNAL */ 112*2248Sraf int np_lio_port; /* listio: SIGEV_THREAD or SIGEV_PORT */ 113*2248Sraf void *np_lio_user; 114*2248Sraf int np_lio_event; 115*2248Sraf uintptr_t np_lio_object; 116*2248Sraf }; 117*2248Sraf 118*2248Sraf struct aio_req { 119*2248Sraf /* 120*2248Sraf * fields protected by _aio_mutex lock. 121*2248Sraf */ 122*2248Sraf aio_req_t *req_link; /* hash/freelist chain link */ 123*2248Sraf /* 124*2248Sraf * when req is on the doneq, then req_next is protected by 125*2248Sraf * the _aio_mutex lock. when the req is on a work q, then 126*2248Sraf * req_next is protected by a worker's work_qlock1 lock. 127*2248Sraf */ 128*2248Sraf aio_req_t *req_next; /* request/done queue link */ 129*2248Sraf aio_req_t *req_prev; /* double linked list */ 130*2248Sraf /* 131*2248Sraf * fields protected by a worker's work_qlock1 lock. 132*2248Sraf */ 133*2248Sraf char req_state; /* AIO_REQ_QUEUED, ... */ 134*2248Sraf /* 135*2248Sraf * fields require no locking. 136*2248Sraf */ 137*2248Sraf char req_type; /* AIO_POSIX_REQ or not */ 138*2248Sraf char req_largefile; /* largefile operation */ 139*2248Sraf char req_op; /* AIOREAD, etc. */ 140*2248Sraf aio_worker_t *req_worker; /* associate request with worker */ 141*2248Sraf aio_result_t *req_resultp; /* address of result buffer */ 142*2248Sraf aio_args_t req_args; /* arglist */ 143*2248Sraf aio_lio_t *req_head; /* list head for LIO */ 144*2248Sraf struct sigevent req_sigevent; 145*2248Sraf void *req_aiocbp; /* ptr to aiocb or aiocb64 */ 146*2248Sraf notif_param_t req_notify; /* notification parameters */ 147*2248Sraf }; 148*2248Sraf 149*2248Sraf /* special lio type that destroys itself when lio refcnt becomes zero */ 150*2248Sraf #define LIO_FSYNC LIO_WAIT+1 151*2248Sraf #define LIO_DESTROY LIO_FSYNC+1 152*2248Sraf 153*2248Sraf /* lio flags */ 154*2248Sraf #define LIO_FSYNC_CANCELED 0x1 155*2248Sraf 156*2248Sraf /* values for aio_state */ 157*2248Sraf 158*2248Sraf #define AIO_REQ_QUEUED 1 159*2248Sraf #define AIO_REQ_INPROGRESS 2 160*2248Sraf #define AIO_REQ_CANCELED 3 161*2248Sraf #define AIO_REQ_DONE 4 162*2248Sraf #define AIO_REQ_FREE 5 163*2248Sraf #define AIO_REQ_DONEQ 6 164*2248Sraf 165*2248Sraf /* use KAIO in _aio_rw() */ 166*2248Sraf #define AIO_NO_KAIO 0x0 167*2248Sraf #define AIO_KAIO 0x1 168*2248Sraf #define AIO_NO_DUPS 0x2 169*2248Sraf 170*2248Sraf #define AIO_POSIX_REQ 0x1 171*2248Sraf 172*2248Sraf #define CHECK 1 173*2248Sraf #define NOCHECK 2 174*2248Sraf #define CHECKED 3 175*2248Sraf #define USERAIO 4 176*2248Sraf #define USERAIO_DONE 5 177*2248Sraf 178*2248Sraf /* values for _aio_flags */ 179*2248Sraf 180*2248Sraf /* if set, _aiodone() notifies aio_waitn about done requests */ 181*2248Sraf #define AIO_WAIT_INPROGRESS 0x1 182*2248Sraf /* if set, _aiodone() wakes up functions waiting for completed I/Os */ 183*2248Sraf #define AIO_IO_WAITING 0x2 184*2248Sraf #define AIO_LIB_WAITN 0x4 /* aio_waitn in progress */ 185*2248Sraf #define AIO_LIB_WAITN_PENDING 0x8 /* aio_waitn requests pending */ 186*2248Sraf 187*2248Sraf /* 188*2248Sraf * Before a kaio() system call, the fd will be checked 189*2248Sraf * to ensure that kernel async. I/O is supported for this file. 190*2248Sraf * The only way to find out is if a kaio() call returns ENOTSUP, 191*2248Sraf * so the default will always be to try the kaio() call. Only in 192*2248Sraf * the specific instance of a kaio() call returning ENOTSUP 193*2248Sraf * will we stop submitting kaio() calls for that fd. 194*2248Sraf * If the fd is outside the array bounds, we will allow the kaio() 195*2248Sraf * call. 196*2248Sraf * 197*2248Sraf * The only way that an fd entry can go from ENOTSUP to supported 198*2248Sraf * is if that fd is freed up by a close(), and close will clear 199*2248Sraf * the entry for that fd. 200*2248Sraf * 201*2248Sraf * Each fd gets a bit in the array _kaio_supported[]. 202*2248Sraf * 203*2248Sraf * uint32_t _kaio_supported[MAX_KAIO_FDARRAY_SIZE]; 204*2248Sraf * 205*2248Sraf * Array is MAX_KAIO_ARRAY_SIZE of 32-bit elements, for 8kb. 206*2248Sraf * If more than (MAX_KAIO_FDARRAY_SIZE * KAIO_FDARRAY_ELEM_SIZE) 207*2248Sraf * files are open, this can be expanded. 208*2248Sraf */ 209*2248Sraf 210*2248Sraf #define MAX_KAIO_FDARRAY_SIZE 2048 211*2248Sraf #define KAIO_FDARRAY_ELEM_SIZE WORD_BIT /* uint32_t */ 212*2248Sraf 213*2248Sraf #define MAX_KAIO_FDS (MAX_KAIO_FDARRAY_SIZE * KAIO_FDARRAY_ELEM_SIZE) 214*2248Sraf 215*2248Sraf #define VALID_FD(fdes) ((fdes) >= 0 && (fdes) < MAX_KAIO_FDS) 216*2248Sraf 217*2248Sraf #define KAIO_SUPPORTED(fdes) \ 218*2248Sraf (!VALID_FD(fdes) || \ 219*2248Sraf ((_kaio_supported[(fdes) / KAIO_FDARRAY_ELEM_SIZE] & \ 220*2248Sraf (uint32_t)(1 << ((fdes) % KAIO_FDARRAY_ELEM_SIZE))) == 0)) 221*2248Sraf 222*2248Sraf #define SET_KAIO_NOT_SUPPORTED(fdes) \ 223*2248Sraf if (VALID_FD(fdes)) \ 224*2248Sraf _kaio_supported[(fdes) / KAIO_FDARRAY_ELEM_SIZE] |= \ 225*2248Sraf (uint32_t)(1 << ((fdes) % KAIO_FDARRAY_ELEM_SIZE)) 226*2248Sraf 227*2248Sraf #define CLEAR_KAIO_SUPPORTED(fdes) \ 228*2248Sraf if (VALID_FD(fdes)) \ 229*2248Sraf _kaio_supported[(fdes) / KAIO_FDARRAY_ELEM_SIZE] &= \ 230*2248Sraf ~(uint32_t)(1 << ((fdes) % KAIO_FDARRAY_ELEM_SIZE)) 231*2248Sraf 232*2248Sraf struct aio_worker { 233*2248Sraf aio_worker_t *work_forw; /* forward link in list of workers */ 234*2248Sraf aio_worker_t *work_backw; /* backwards link in list of workers */ 235*2248Sraf mutex_t work_qlock1; /* lock for work queue 1 */ 236*2248Sraf cond_t work_idle_cv; /* place to sleep when idle */ 237*2248Sraf aio_req_t *work_head1; /* head of work request queue 1 */ 238*2248Sraf aio_req_t *work_tail1; /* tail of work request queue 1 */ 239*2248Sraf aio_req_t *work_next1; /* work queue one's next pointer */ 240*2248Sraf aio_req_t *work_prev1; /* last request done from queue 1 */ 241*2248Sraf aio_req_t *work_req; /* active work request */ 242*2248Sraf thread_t work_tid; /* worker's thread-id */ 243*2248Sraf int work_count1; /* length of work queue one */ 244*2248Sraf int work_done1; /* number of requests done */ 245*2248Sraf int work_minload1; /* min length of queue */ 246*2248Sraf int work_idleflg; /* when set, worker is idle */ 247*2248Sraf sigjmp_buf work_jmp_buf; /* cancellation point */ 248*2248Sraf }; 249*2248Sraf 250*2248Sraf struct aio_hash { /* resultp hash table */ 251*2248Sraf mutex_t hash_lock; 252*2248Sraf aio_req_t *hash_ptr; 253*2248Sraf #if !defined(_LP64) 254*2248Sraf void *hash_pad; /* ensure sizeof (aio_hash_t) == 32 */ 255*2248Sraf #endif 256*2248Sraf }; 257*2248Sraf 258*2248Sraf extern aio_hash_t *_aio_hash; 259*2248Sraf 260*2248Sraf #define HASHSZ 2048 /* power of 2 */ 261*2248Sraf #define AIOHASH(resultp) ((((uintptr_t)(resultp) >> 17) ^ \ 262*2248Sraf ((uintptr_t)(resultp) >> 2)) & (HASHSZ - 1)) 263*2248Sraf #define POSIX_AIO(x) ((x)->req_type == AIO_POSIX_REQ) 264*2248Sraf 265*2248Sraf extern int __uaio_init(void); 266*2248Sraf extern void _kaio_init(void); 267*2248Sraf extern intptr_t _kaio(int, ...); 268*2248Sraf extern int _aiorw(int, caddr_t, int, offset_t, int, aio_result_t *, int); 269*2248Sraf extern int _aio_rw(aiocb_t *, aio_lio_t *, aio_worker_t **, int, int); 270*2248Sraf #if !defined(_LP64) 271*2248Sraf extern int _aio_rw64(aiocb64_t *, aio_lio_t *, aio_worker_t **, int, int); 272*2248Sraf #endif 273*2248Sraf extern int _aio_create_worker(aio_req_t *, int); 274*2248Sraf extern int _aio_cancel_req(aio_worker_t *, aio_req_t *, int *, int *); 275*2248Sraf extern int aiocancel_all(int); 276*2248Sraf extern void aio_panic(const char *); 277*2248Sraf extern aio_req_t *_aio_hash_find(aio_result_t *); 278*2248Sraf extern aio_req_t *_aio_hash_del(aio_result_t *); 279*2248Sraf extern void _aio_req_mark_done(aio_req_t *); 280*2248Sraf extern void _aio_waitn_wakeup(void); 281*2248Sraf extern aio_worker_t *_aio_worker_alloc(void); 282*2248Sraf extern void _aio_worker_free(void *); 283*2248Sraf extern aio_req_t *_aio_req_alloc(void); 284*2248Sraf extern void _aio_req_free(aio_req_t *); 285*2248Sraf extern aio_lio_t *_aio_lio_alloc(void); 286*2248Sraf extern void _aio_lio_free(aio_lio_t *); 287*2248Sraf extern int _aio_idle(aio_worker_t *); 288*2248Sraf extern void *_aio_do_request(void *); 289*2248Sraf extern void *_aio_do_notify(void *); 290*2248Sraf extern void _lio_remove(aio_req_t *); 291*2248Sraf extern aio_req_t *_aio_req_remove(aio_req_t *); 292*2248Sraf extern int _aio_get_timedelta(timespec_t *, timespec_t *); 293*2248Sraf extern aio_result_t *_aio_req_done(void); 294*2248Sraf extern void _aio_set_result(aio_req_t *, ssize_t, int); 295*2248Sraf extern int _aio_sigev_thread_init(struct sigevent *); 296*2248Sraf extern int _aio_sigev_thread(aiocb_t *); 297*2248Sraf #if !defined(_LP64) 298*2248Sraf extern int _aio_sigev_thread64(aiocb64_t *); 299*2248Sraf #endif 300*2248Sraf 301*2248Sraf extern aio_worker_t *_kaiowp; /* points to kaio cleanup thread */ 302*2248Sraf extern aio_worker_t *__workers_rw; /* list of all rw workers */ 303*2248Sraf extern aio_worker_t *__nextworker_rw; /* worker chosen for next rw request */ 304*2248Sraf extern int __rw_workerscnt; /* number of rw workers */ 305*2248Sraf extern aio_worker_t *__workers_no; /* list of all notification workers */ 306*2248Sraf extern aio_worker_t *__nextworker_no; /* worker chosen, next notification */ 307*2248Sraf extern int __no_workerscnt; /* number of notification workers */ 308*2248Sraf extern mutex_t __aio_initlock; /* makes aio initialization atomic */ 309*2248Sraf extern cond_t __aio_initcv; 310*2248Sraf extern int __aio_initbusy; 311*2248Sraf extern mutex_t __aio_mutex; /* global aio lock */ 312*2248Sraf extern cond_t _aio_iowait_cv; /* wait for userland I/Os */ 313*2248Sraf extern cond_t _aio_waitn_cv; /* wait for end of aio_waitn */ 314*2248Sraf extern int _max_workers; /* max number of workers permitted */ 315*2248Sraf extern int _min_workers; /* min number of workers */ 316*2248Sraf extern sigset_t _worker_set; /* worker's signal mask */ 317*2248Sraf extern int _aio_worker_cnt; /* number of AIO workers */ 318*2248Sraf extern int _sigio_enabled; /* when set, send SIGIO signal */ 319*2248Sraf extern pid_t __pid; /* process's PID */ 320*2248Sraf extern int __uaio_ok; /* indicates if aio is initialized */ 321*2248Sraf extern int _kaio_ok; /* indicates if kaio is initialized */ 322*2248Sraf extern pthread_key_t _aio_key; /* for thread-specific data */ 323*2248Sraf extern aio_req_t *_aio_done_tail; /* list of done requests */ 324*2248Sraf extern aio_req_t *_aio_done_head; 325*2248Sraf extern aio_req_t *_aio_doneq; 326*2248Sraf extern int _aio_freelist_cnt; 327*2248Sraf extern int _aio_allocated_cnt; 328*2248Sraf extern int _aio_donecnt; 329*2248Sraf extern int _aio_doneq_cnt; 330*2248Sraf extern int _aio_waitncnt; /* # of requests for aio_waitn */ 331*2248Sraf extern int _aio_outstand_cnt; /* # of outstanding requests */ 332*2248Sraf extern int _kaio_outstand_cnt; /* # of outstanding kaio requests */ 333*2248Sraf extern int _aio_req_done_cnt; /* req. done but not in "done queue" */ 334*2248Sraf extern int _aio_kernel_suspend; /* active kernel kaio calls */ 335*2248Sraf extern int _aio_suscv_cnt; /* aio_suspend calls waiting on cv's */ 336*2248Sraf extern int _aiowait_flag; /* when set, aiowait() is inprogress */ 337*2248Sraf extern int _aio_flags; /* see defines, above */ 338*2248Sraf extern uint32_t *_kaio_supported; 339*2248Sraf 340*2248Sraf extern const sigset_t maskset; /* all maskable signals */ 341*2248Sraf 342*2248Sraf #ifdef __cplusplus 343*2248Sraf } 344*2248Sraf #endif 345*2248Sraf 346*2248Sraf #endif /* _ASYNCIO_H */ 347