1 /*- 2 * Copyright (c) 1997 John S. Dyson. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. John S. Dyson's name may not be used to endorse or promote products 10 * derived from this software without specific prior written permission. 11 * 12 * DISCLAIMER: This code isn't warranted to do anything useful. Anything 13 * bad that happens because of using this software isn't the responsibility 14 * of the author. This software is distributed AS-IS. 15 */ 16 17 /* 18 * This file contains support for the POSIX 1003.1B AIO/LIO facility. 19 */ 20 21 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 23 24 #include "opt_compat.h" 25 26 #include <sys/param.h> 27 #include <sys/systm.h> 28 #include <sys/malloc.h> 29 #include <sys/bio.h> 30 #include <sys/buf.h> 31 #include <sys/capability.h> 32 #include <sys/eventhandler.h> 33 #include <sys/sysproto.h> 34 #include <sys/filedesc.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/kthread.h> 38 #include <sys/fcntl.h> 39 #include <sys/file.h> 40 #include <sys/limits.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/unistd.h> 44 #include <sys/posix4.h> 45 #include <sys/proc.h> 46 #include <sys/resourcevar.h> 47 #include <sys/signalvar.h> 48 #include <sys/protosw.h> 49 #include <sys/rwlock.h> 50 #include <sys/sema.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/syscall.h> 54 #include <sys/sysent.h> 55 #include <sys/sysctl.h> 56 #include <sys/sx.h> 57 #include <sys/taskqueue.h> 58 #include <sys/vnode.h> 59 #include <sys/conf.h> 60 #include <sys/event.h> 61 #include <sys/mount.h> 62 63 #include <machine/atomic.h> 64 65 #include <vm/vm.h> 66 #include <vm/vm_extern.h> 67 #include <vm/pmap.h> 68 #include <vm/vm_map.h> 69 #include <vm/vm_object.h> 70 #include <vm/uma.h> 71 #include <sys/aio.h> 72 73 #include "opt_vfs_aio.h" 74 75 /* 76 * Counter for allocating reference ids to new jobs. Wrapped to 1 on 77 * overflow. (XXX will be removed soon.) 78 */ 79 static u_long jobrefid; 80 81 /* 82 * Counter for aio_fsync. 83 */ 84 static uint64_t jobseqno; 85 86 #define JOBST_NULL 0 87 #define JOBST_JOBQSOCK 1 88 #define JOBST_JOBQGLOBAL 2 89 #define JOBST_JOBRUNNING 3 90 #define JOBST_JOBFINISHED 4 91 #define JOBST_JOBQBUF 5 92 #define JOBST_JOBQSYNC 6 93 94 #ifndef MAX_AIO_PER_PROC 95 #define MAX_AIO_PER_PROC 32 96 #endif 97 98 #ifndef MAX_AIO_QUEUE_PER_PROC 99 #define MAX_AIO_QUEUE_PER_PROC 256 /* Bigger than AIO_LISTIO_MAX */ 100 #endif 101 102 #ifndef MAX_AIO_PROCS 103 #define MAX_AIO_PROCS 32 104 #endif 105 106 #ifndef MAX_AIO_QUEUE 107 #define MAX_AIO_QUEUE 1024 /* Bigger than AIO_LISTIO_MAX */ 108 #endif 109 110 #ifndef TARGET_AIO_PROCS 111 #define TARGET_AIO_PROCS 4 112 #endif 113 114 #ifndef MAX_BUF_AIO 115 #define MAX_BUF_AIO 16 116 #endif 117 118 #ifndef AIOD_TIMEOUT_DEFAULT 119 #define AIOD_TIMEOUT_DEFAULT (10 * hz) 120 #endif 121 122 #ifndef AIOD_LIFETIME_DEFAULT 123 #define AIOD_LIFETIME_DEFAULT (30 * hz) 124 #endif 125 126 FEATURE(aio, "Asynchronous I/O"); 127 128 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list"); 129 130 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW, 0, "Async IO management"); 131 132 static int max_aio_procs = MAX_AIO_PROCS; 133 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, 134 CTLFLAG_RW, &max_aio_procs, 0, 135 "Maximum number of kernel threads to use for handling async IO "); 136 137 static int num_aio_procs = 0; 138 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, 139 CTLFLAG_RD, &num_aio_procs, 0, 140 "Number of presently active kernel threads for async IO"); 141 142 /* 143 * The code will adjust the actual number of AIO processes towards this 144 * number when it gets a chance. 145 */ 146 static int target_aio_procs = TARGET_AIO_PROCS; 147 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs, 148 0, "Preferred number of ready kernel threads for async IO"); 149 150 static int max_queue_count = MAX_AIO_QUEUE; 151 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0, 152 "Maximum number of aio requests to queue, globally"); 153 154 static int num_queue_count = 0; 155 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0, 156 "Number of queued aio requests"); 157 158 static int num_buf_aio = 0; 159 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0, 160 "Number of aio requests presently handled by the buf subsystem"); 161 162 /* Number of async I/O thread in the process of being started */ 163 /* XXX This should be local to aio_aqueue() */ 164 static int num_aio_resv_start = 0; 165 166 static int aiod_timeout; 167 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_timeout, CTLFLAG_RW, &aiod_timeout, 0, 168 "Timeout value for synchronous aio operations"); 169 170 static int aiod_lifetime; 171 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0, 172 "Maximum lifetime for idle aiod"); 173 174 static int unloadable = 0; 175 SYSCTL_INT(_vfs_aio, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0, 176 "Allow unload of aio (not recommended)"); 177 178 179 static int max_aio_per_proc = MAX_AIO_PER_PROC; 180 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc, 181 0, "Maximum active aio requests per process (stored in the process)"); 182 183 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC; 184 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW, 185 &max_aio_queue_per_proc, 0, 186 "Maximum queued aio requests per process (stored in the process)"); 187 188 static int max_buf_aio = MAX_BUF_AIO; 189 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0, 190 "Maximum buf aio requests per process (stored in the process)"); 191 192 typedef struct oaiocb { 193 int aio_fildes; /* File descriptor */ 194 off_t aio_offset; /* File offset for I/O */ 195 volatile void *aio_buf; /* I/O buffer in process space */ 196 size_t aio_nbytes; /* Number of bytes for I/O */ 197 struct osigevent aio_sigevent; /* Signal to deliver */ 198 int aio_lio_opcode; /* LIO opcode */ 199 int aio_reqprio; /* Request priority -- ignored */ 200 struct __aiocb_private _aiocb_private; 201 } oaiocb_t; 202 203 /* 204 * Below is a key of locks used to protect each member of struct aiocblist 205 * aioliojob and kaioinfo and any backends. 206 * 207 * * - need not protected 208 * a - locked by kaioinfo lock 209 * b - locked by backend lock, the backend lock can be null in some cases, 210 * for example, BIO belongs to this type, in this case, proc lock is 211 * reused. 212 * c - locked by aio_job_mtx, the lock for the generic file I/O backend. 213 */ 214 215 /* 216 * Current, there is only two backends: BIO and generic file I/O. 217 * socket I/O is served by generic file I/O, this is not a good idea, since 218 * disk file I/O and any other types without O_NONBLOCK flag can block daemon 219 * threads, if there is no thread to serve socket I/O, the socket I/O will be 220 * delayed too long or starved, we should create some threads dedicated to 221 * sockets to do non-blocking I/O, same for pipe and fifo, for these I/O 222 * systems we really need non-blocking interface, fiddling O_NONBLOCK in file 223 * structure is not safe because there is race between userland and aio 224 * daemons. 225 */ 226 227 struct aiocblist { 228 TAILQ_ENTRY(aiocblist) list; /* (b) internal list of for backend */ 229 TAILQ_ENTRY(aiocblist) plist; /* (a) list of jobs for each backend */ 230 TAILQ_ENTRY(aiocblist) allist; /* (a) list of all jobs in proc */ 231 int jobflags; /* (a) job flags */ 232 int jobstate; /* (b) job state */ 233 int inputcharge; /* (*) input blockes */ 234 int outputcharge; /* (*) output blockes */ 235 struct buf *bp; /* (*) private to BIO backend, 236 * buffer pointer 237 */ 238 struct proc *userproc; /* (*) user process */ 239 struct ucred *cred; /* (*) active credential when created */ 240 struct file *fd_file; /* (*) pointer to file structure */ 241 struct aioliojob *lio; /* (*) optional lio job */ 242 struct aiocb *uuaiocb; /* (*) pointer in userspace of aiocb */ 243 struct knlist klist; /* (a) list of knotes */ 244 struct aiocb uaiocb; /* (*) kernel I/O control block */ 245 ksiginfo_t ksi; /* (a) realtime signal info */ 246 struct task biotask; /* (*) private to BIO backend */ 247 uint64_t seqno; /* (*) job number */ 248 int pending; /* (a) number of pending I/O, aio_fsync only */ 249 }; 250 251 /* jobflags */ 252 #define AIOCBLIST_DONE 0x01 253 #define AIOCBLIST_BUFDONE 0x02 254 #define AIOCBLIST_RUNDOWN 0x04 255 #define AIOCBLIST_CHECKSYNC 0x08 256 257 /* 258 * AIO process info 259 */ 260 #define AIOP_FREE 0x1 /* proc on free queue */ 261 262 struct aiothreadlist { 263 int aiothreadflags; /* (c) AIO proc flags */ 264 TAILQ_ENTRY(aiothreadlist) list; /* (c) list of processes */ 265 struct thread *aiothread; /* (*) the AIO thread */ 266 }; 267 268 /* 269 * data-structure for lio signal management 270 */ 271 struct aioliojob { 272 int lioj_flags; /* (a) listio flags */ 273 int lioj_count; /* (a) listio flags */ 274 int lioj_finished_count; /* (a) listio flags */ 275 struct sigevent lioj_signal; /* (a) signal on all I/O done */ 276 TAILQ_ENTRY(aioliojob) lioj_list; /* (a) lio list */ 277 struct knlist klist; /* (a) list of knotes */ 278 ksiginfo_t lioj_ksi; /* (a) Realtime signal info */ 279 }; 280 281 #define LIOJ_SIGNAL 0x1 /* signal on all done (lio) */ 282 #define LIOJ_SIGNAL_POSTED 0x2 /* signal has been posted */ 283 #define LIOJ_KEVENT_POSTED 0x4 /* kevent triggered */ 284 285 /* 286 * per process aio data structure 287 */ 288 struct kaioinfo { 289 struct mtx kaio_mtx; /* the lock to protect this struct */ 290 int kaio_flags; /* (a) per process kaio flags */ 291 int kaio_maxactive_count; /* (*) maximum number of AIOs */ 292 int kaio_active_count; /* (c) number of currently used AIOs */ 293 int kaio_qallowed_count; /* (*) maxiumu size of AIO queue */ 294 int kaio_count; /* (a) size of AIO queue */ 295 int kaio_ballowed_count; /* (*) maximum number of buffers */ 296 int kaio_buffer_count; /* (a) number of physio buffers */ 297 TAILQ_HEAD(,aiocblist) kaio_all; /* (a) all AIOs in the process */ 298 TAILQ_HEAD(,aiocblist) kaio_done; /* (a) done queue for process */ 299 TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */ 300 TAILQ_HEAD(,aiocblist) kaio_jobqueue; /* (a) job queue for process */ 301 TAILQ_HEAD(,aiocblist) kaio_bufqueue; /* (a) buffer job queue for process */ 302 TAILQ_HEAD(,aiocblist) kaio_sockqueue; /* (a) queue for aios waiting on sockets, 303 * NOT USED YET. 304 */ 305 TAILQ_HEAD(,aiocblist) kaio_syncqueue; /* (a) queue for aio_fsync */ 306 struct task kaio_task; /* (*) task to kick aio threads */ 307 }; 308 309 #define AIO_LOCK(ki) mtx_lock(&(ki)->kaio_mtx) 310 #define AIO_UNLOCK(ki) mtx_unlock(&(ki)->kaio_mtx) 311 #define AIO_LOCK_ASSERT(ki, f) mtx_assert(&(ki)->kaio_mtx, (f)) 312 #define AIO_MTX(ki) (&(ki)->kaio_mtx) 313 314 #define KAIO_RUNDOWN 0x1 /* process is being run down */ 315 #define KAIO_WAKEUP 0x2 /* wakeup process when there is a significant event */ 316 317 /* 318 * Operations used to interact with userland aio control blocks. 319 * Different ABIs provide their own operations. 320 */ 321 struct aiocb_ops { 322 int (*copyin)(struct aiocb *ujob, struct aiocb *kjob); 323 long (*fetch_status)(struct aiocb *ujob); 324 long (*fetch_error)(struct aiocb *ujob); 325 int (*store_status)(struct aiocb *ujob, long status); 326 int (*store_error)(struct aiocb *ujob, long error); 327 int (*store_kernelinfo)(struct aiocb *ujob, long jobref); 328 int (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob); 329 }; 330 331 static TAILQ_HEAD(,aiothreadlist) aio_freeproc; /* (c) Idle daemons */ 332 static struct sema aio_newproc_sem; 333 static struct mtx aio_job_mtx; 334 static struct mtx aio_sock_mtx; 335 static TAILQ_HEAD(,aiocblist) aio_jobs; /* (c) Async job list */ 336 static struct unrhdr *aiod_unr; 337 338 void aio_init_aioinfo(struct proc *p); 339 static int aio_onceonly(void); 340 static int aio_free_entry(struct aiocblist *aiocbe); 341 static void aio_process(struct aiocblist *aiocbe); 342 static int aio_newproc(int *); 343 int aio_aqueue(struct thread *td, struct aiocb *job, 344 struct aioliojob *lio, int type, struct aiocb_ops *ops); 345 static void aio_physwakeup(struct buf *bp); 346 static void aio_proc_rundown(void *arg, struct proc *p); 347 static void aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp); 348 static int aio_qphysio(struct proc *p, struct aiocblist *iocb); 349 static void biohelper(void *, int); 350 static void aio_daemon(void *param); 351 static void aio_swake_cb(struct socket *, struct sockbuf *); 352 static int aio_unload(void); 353 static void aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type); 354 #define DONE_BUF 1 355 #define DONE_QUEUE 2 356 static int aio_kick(struct proc *userp); 357 static void aio_kick_nowait(struct proc *userp); 358 static void aio_kick_helper(void *context, int pending); 359 static int filt_aioattach(struct knote *kn); 360 static void filt_aiodetach(struct knote *kn); 361 static int filt_aio(struct knote *kn, long hint); 362 static int filt_lioattach(struct knote *kn); 363 static void filt_liodetach(struct knote *kn); 364 static int filt_lio(struct knote *kn, long hint); 365 366 /* 367 * Zones for: 368 * kaio Per process async io info 369 * aiop async io thread data 370 * aiocb async io jobs 371 * aiol list io job pointer - internal to aio_suspend XXX 372 * aiolio list io jobs 373 */ 374 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiol_zone, aiolio_zone; 375 376 /* kqueue filters for aio */ 377 static struct filterops aio_filtops = { 378 .f_isfd = 0, 379 .f_attach = filt_aioattach, 380 .f_detach = filt_aiodetach, 381 .f_event = filt_aio, 382 }; 383 static struct filterops lio_filtops = { 384 .f_isfd = 0, 385 .f_attach = filt_lioattach, 386 .f_detach = filt_liodetach, 387 .f_event = filt_lio 388 }; 389 390 static eventhandler_tag exit_tag, exec_tag; 391 392 TASKQUEUE_DEFINE_THREAD(aiod_bio); 393 394 /* 395 * Main operations function for use as a kernel module. 396 */ 397 static int 398 aio_modload(struct module *module, int cmd, void *arg) 399 { 400 int error = 0; 401 402 switch (cmd) { 403 case MOD_LOAD: 404 aio_onceonly(); 405 break; 406 case MOD_UNLOAD: 407 error = aio_unload(); 408 break; 409 case MOD_SHUTDOWN: 410 break; 411 default: 412 error = EINVAL; 413 break; 414 } 415 return (error); 416 } 417 418 static moduledata_t aio_mod = { 419 "aio", 420 &aio_modload, 421 NULL 422 }; 423 424 static struct syscall_helper_data aio_syscalls[] = { 425 SYSCALL_INIT_HELPER(aio_cancel), 426 SYSCALL_INIT_HELPER(aio_error), 427 SYSCALL_INIT_HELPER(aio_fsync), 428 SYSCALL_INIT_HELPER(aio_read), 429 SYSCALL_INIT_HELPER(aio_return), 430 SYSCALL_INIT_HELPER(aio_suspend), 431 SYSCALL_INIT_HELPER(aio_waitcomplete), 432 SYSCALL_INIT_HELPER(aio_write), 433 SYSCALL_INIT_HELPER(lio_listio), 434 SYSCALL_INIT_HELPER(oaio_read), 435 SYSCALL_INIT_HELPER(oaio_write), 436 SYSCALL_INIT_HELPER(olio_listio), 437 SYSCALL_INIT_LAST 438 }; 439 440 #ifdef COMPAT_FREEBSD32 441 #include <sys/mount.h> 442 #include <sys/socket.h> 443 #include <compat/freebsd32/freebsd32.h> 444 #include <compat/freebsd32/freebsd32_proto.h> 445 #include <compat/freebsd32/freebsd32_signal.h> 446 #include <compat/freebsd32/freebsd32_syscall.h> 447 #include <compat/freebsd32/freebsd32_util.h> 448 449 static struct syscall_helper_data aio32_syscalls[] = { 450 SYSCALL32_INIT_HELPER(freebsd32_aio_return), 451 SYSCALL32_INIT_HELPER(freebsd32_aio_suspend), 452 SYSCALL32_INIT_HELPER(freebsd32_aio_cancel), 453 SYSCALL32_INIT_HELPER(freebsd32_aio_error), 454 SYSCALL32_INIT_HELPER(freebsd32_aio_fsync), 455 SYSCALL32_INIT_HELPER(freebsd32_aio_read), 456 SYSCALL32_INIT_HELPER(freebsd32_aio_write), 457 SYSCALL32_INIT_HELPER(freebsd32_aio_waitcomplete), 458 SYSCALL32_INIT_HELPER(freebsd32_lio_listio), 459 SYSCALL32_INIT_HELPER(freebsd32_oaio_read), 460 SYSCALL32_INIT_HELPER(freebsd32_oaio_write), 461 SYSCALL32_INIT_HELPER(freebsd32_olio_listio), 462 SYSCALL_INIT_LAST 463 }; 464 #endif 465 466 DECLARE_MODULE(aio, aio_mod, 467 SI_SUB_VFS, SI_ORDER_ANY); 468 MODULE_VERSION(aio, 1); 469 470 /* 471 * Startup initialization 472 */ 473 static int 474 aio_onceonly(void) 475 { 476 int error; 477 478 /* XXX: should probably just use so->callback */ 479 aio_swake = &aio_swake_cb; 480 exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL, 481 EVENTHANDLER_PRI_ANY); 482 exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec, NULL, 483 EVENTHANDLER_PRI_ANY); 484 kqueue_add_filteropts(EVFILT_AIO, &aio_filtops); 485 kqueue_add_filteropts(EVFILT_LIO, &lio_filtops); 486 TAILQ_INIT(&aio_freeproc); 487 sema_init(&aio_newproc_sem, 0, "aio_new_proc"); 488 mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF); 489 mtx_init(&aio_sock_mtx, "aio_sock", NULL, MTX_DEF); 490 TAILQ_INIT(&aio_jobs); 491 aiod_unr = new_unrhdr(1, INT_MAX, NULL); 492 kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL, 493 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 494 aiop_zone = uma_zcreate("AIOP", sizeof(struct aiothreadlist), NULL, 495 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 496 aiocb_zone = uma_zcreate("AIOCB", sizeof(struct aiocblist), NULL, NULL, 497 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 498 aiol_zone = uma_zcreate("AIOL", AIO_LISTIO_MAX*sizeof(intptr_t) , NULL, 499 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 500 aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL, 501 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 502 aiod_timeout = AIOD_TIMEOUT_DEFAULT; 503 aiod_lifetime = AIOD_LIFETIME_DEFAULT; 504 jobrefid = 1; 505 async_io_version = _POSIX_VERSION; 506 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, AIO_LISTIO_MAX); 507 p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE); 508 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0); 509 510 error = syscall_helper_register(aio_syscalls); 511 if (error) 512 return (error); 513 #ifdef COMPAT_FREEBSD32 514 error = syscall32_helper_register(aio32_syscalls); 515 if (error) 516 return (error); 517 #endif 518 return (0); 519 } 520 521 /* 522 * Callback for unload of AIO when used as a module. 523 */ 524 static int 525 aio_unload(void) 526 { 527 int error; 528 529 /* 530 * XXX: no unloads by default, it's too dangerous. 531 * perhaps we could do it if locked out callers and then 532 * did an aio_proc_rundown() on each process. 533 * 534 * jhb: aio_proc_rundown() needs to run on curproc though, 535 * so I don't think that would fly. 536 */ 537 if (!unloadable) 538 return (EOPNOTSUPP); 539 540 #ifdef COMPAT_FREEBSD32 541 syscall32_helper_unregister(aio32_syscalls); 542 #endif 543 syscall_helper_unregister(aio_syscalls); 544 545 error = kqueue_del_filteropts(EVFILT_AIO); 546 if (error) 547 return error; 548 error = kqueue_del_filteropts(EVFILT_LIO); 549 if (error) 550 return error; 551 async_io_version = 0; 552 aio_swake = NULL; 553 taskqueue_free(taskqueue_aiod_bio); 554 delete_unrhdr(aiod_unr); 555 uma_zdestroy(kaio_zone); 556 uma_zdestroy(aiop_zone); 557 uma_zdestroy(aiocb_zone); 558 uma_zdestroy(aiol_zone); 559 uma_zdestroy(aiolio_zone); 560 EVENTHANDLER_DEREGISTER(process_exit, exit_tag); 561 EVENTHANDLER_DEREGISTER(process_exec, exec_tag); 562 mtx_destroy(&aio_job_mtx); 563 mtx_destroy(&aio_sock_mtx); 564 sema_destroy(&aio_newproc_sem); 565 p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, -1); 566 p31b_setcfg(CTL_P1003_1B_AIO_MAX, -1); 567 p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, -1); 568 return (0); 569 } 570 571 /* 572 * Init the per-process aioinfo structure. The aioinfo limits are set 573 * per-process for user limit (resource) management. 574 */ 575 void 576 aio_init_aioinfo(struct proc *p) 577 { 578 struct kaioinfo *ki; 579 580 ki = uma_zalloc(kaio_zone, M_WAITOK); 581 mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF); 582 ki->kaio_flags = 0; 583 ki->kaio_maxactive_count = max_aio_per_proc; 584 ki->kaio_active_count = 0; 585 ki->kaio_qallowed_count = max_aio_queue_per_proc; 586 ki->kaio_count = 0; 587 ki->kaio_ballowed_count = max_buf_aio; 588 ki->kaio_buffer_count = 0; 589 TAILQ_INIT(&ki->kaio_all); 590 TAILQ_INIT(&ki->kaio_done); 591 TAILQ_INIT(&ki->kaio_jobqueue); 592 TAILQ_INIT(&ki->kaio_bufqueue); 593 TAILQ_INIT(&ki->kaio_liojoblist); 594 TAILQ_INIT(&ki->kaio_sockqueue); 595 TAILQ_INIT(&ki->kaio_syncqueue); 596 TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p); 597 PROC_LOCK(p); 598 if (p->p_aioinfo == NULL) { 599 p->p_aioinfo = ki; 600 PROC_UNLOCK(p); 601 } else { 602 PROC_UNLOCK(p); 603 mtx_destroy(&ki->kaio_mtx); 604 uma_zfree(kaio_zone, ki); 605 } 606 607 while (num_aio_procs < MIN(target_aio_procs, max_aio_procs)) 608 aio_newproc(NULL); 609 } 610 611 static int 612 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi) 613 { 614 struct thread *td; 615 int error; 616 617 error = sigev_findtd(p, sigev, &td); 618 if (error) 619 return (error); 620 if (!KSI_ONQ(ksi)) { 621 ksiginfo_set_sigev(ksi, sigev); 622 ksi->ksi_code = SI_ASYNCIO; 623 ksi->ksi_flags |= KSI_EXT | KSI_INS; 624 tdsendsignal(p, td, ksi->ksi_signo, ksi); 625 } 626 PROC_UNLOCK(p); 627 return (error); 628 } 629 630 /* 631 * Free a job entry. Wait for completion if it is currently active, but don't 632 * delay forever. If we delay, we return a flag that says that we have to 633 * restart the queue scan. 634 */ 635 static int 636 aio_free_entry(struct aiocblist *aiocbe) 637 { 638 struct kaioinfo *ki; 639 struct aioliojob *lj; 640 struct proc *p; 641 642 p = aiocbe->userproc; 643 MPASS(curproc == p); 644 ki = p->p_aioinfo; 645 MPASS(ki != NULL); 646 647 AIO_LOCK_ASSERT(ki, MA_OWNED); 648 MPASS(aiocbe->jobstate == JOBST_JOBFINISHED); 649 650 atomic_subtract_int(&num_queue_count, 1); 651 652 ki->kaio_count--; 653 MPASS(ki->kaio_count >= 0); 654 655 TAILQ_REMOVE(&ki->kaio_done, aiocbe, plist); 656 TAILQ_REMOVE(&ki->kaio_all, aiocbe, allist); 657 658 lj = aiocbe->lio; 659 if (lj) { 660 lj->lioj_count--; 661 lj->lioj_finished_count--; 662 663 if (lj->lioj_count == 0) { 664 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 665 /* lio is going away, we need to destroy any knotes */ 666 knlist_delete(&lj->klist, curthread, 1); 667 PROC_LOCK(p); 668 sigqueue_take(&lj->lioj_ksi); 669 PROC_UNLOCK(p); 670 uma_zfree(aiolio_zone, lj); 671 } 672 } 673 674 /* aiocbe is going away, we need to destroy any knotes */ 675 knlist_delete(&aiocbe->klist, curthread, 1); 676 PROC_LOCK(p); 677 sigqueue_take(&aiocbe->ksi); 678 PROC_UNLOCK(p); 679 680 MPASS(aiocbe->bp == NULL); 681 aiocbe->jobstate = JOBST_NULL; 682 AIO_UNLOCK(ki); 683 684 /* 685 * The thread argument here is used to find the owning process 686 * and is also passed to fo_close() which may pass it to various 687 * places such as devsw close() routines. Because of that, we 688 * need a thread pointer from the process owning the job that is 689 * persistent and won't disappear out from under us or move to 690 * another process. 691 * 692 * Currently, all the callers of this function call it to remove 693 * an aiocblist from the current process' job list either via a 694 * syscall or due to the current process calling exit() or 695 * execve(). Thus, we know that p == curproc. We also know that 696 * curthread can't exit since we are curthread. 697 * 698 * Therefore, we use curthread as the thread to pass to 699 * knlist_delete(). This does mean that it is possible for the 700 * thread pointer at close time to differ from the thread pointer 701 * at open time, but this is already true of file descriptors in 702 * a multithreaded process. 703 */ 704 fdrop(aiocbe->fd_file, curthread); 705 crfree(aiocbe->cred); 706 uma_zfree(aiocb_zone, aiocbe); 707 AIO_LOCK(ki); 708 709 return (0); 710 } 711 712 static void 713 aio_proc_rundown_exec(void *arg, struct proc *p, struct image_params *imgp __unused) 714 { 715 aio_proc_rundown(arg, p); 716 } 717 718 /* 719 * Rundown the jobs for a given process. 720 */ 721 static void 722 aio_proc_rundown(void *arg, struct proc *p) 723 { 724 struct kaioinfo *ki; 725 struct aioliojob *lj; 726 struct aiocblist *cbe, *cbn; 727 struct file *fp; 728 struct socket *so; 729 int remove; 730 731 KASSERT(curthread->td_proc == p, 732 ("%s: called on non-curproc", __func__)); 733 ki = p->p_aioinfo; 734 if (ki == NULL) 735 return; 736 737 AIO_LOCK(ki); 738 ki->kaio_flags |= KAIO_RUNDOWN; 739 740 restart: 741 742 /* 743 * Try to cancel all pending requests. This code simulates 744 * aio_cancel on all pending I/O requests. 745 */ 746 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) { 747 remove = 0; 748 mtx_lock(&aio_job_mtx); 749 if (cbe->jobstate == JOBST_JOBQGLOBAL) { 750 TAILQ_REMOVE(&aio_jobs, cbe, list); 751 remove = 1; 752 } else if (cbe->jobstate == JOBST_JOBQSOCK) { 753 fp = cbe->fd_file; 754 MPASS(fp->f_type == DTYPE_SOCKET); 755 so = fp->f_data; 756 TAILQ_REMOVE(&so->so_aiojobq, cbe, list); 757 remove = 1; 758 } else if (cbe->jobstate == JOBST_JOBQSYNC) { 759 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list); 760 remove = 1; 761 } 762 mtx_unlock(&aio_job_mtx); 763 764 if (remove) { 765 cbe->jobstate = JOBST_JOBFINISHED; 766 cbe->uaiocb._aiocb_private.status = -1; 767 cbe->uaiocb._aiocb_private.error = ECANCELED; 768 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist); 769 aio_bio_done_notify(p, cbe, DONE_QUEUE); 770 } 771 } 772 773 /* Wait for all running I/O to be finished */ 774 if (TAILQ_FIRST(&ki->kaio_bufqueue) || 775 TAILQ_FIRST(&ki->kaio_jobqueue)) { 776 ki->kaio_flags |= KAIO_WAKEUP; 777 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz); 778 goto restart; 779 } 780 781 /* Free all completed I/O requests. */ 782 while ((cbe = TAILQ_FIRST(&ki->kaio_done)) != NULL) 783 aio_free_entry(cbe); 784 785 while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) { 786 if (lj->lioj_count == 0) { 787 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 788 knlist_delete(&lj->klist, curthread, 1); 789 PROC_LOCK(p); 790 sigqueue_take(&lj->lioj_ksi); 791 PROC_UNLOCK(p); 792 uma_zfree(aiolio_zone, lj); 793 } else { 794 panic("LIO job not cleaned up: C:%d, FC:%d\n", 795 lj->lioj_count, lj->lioj_finished_count); 796 } 797 } 798 AIO_UNLOCK(ki); 799 taskqueue_drain(taskqueue_aiod_bio, &ki->kaio_task); 800 mtx_destroy(&ki->kaio_mtx); 801 uma_zfree(kaio_zone, ki); 802 p->p_aioinfo = NULL; 803 } 804 805 /* 806 * Select a job to run (called by an AIO daemon). 807 */ 808 static struct aiocblist * 809 aio_selectjob(struct aiothreadlist *aiop) 810 { 811 struct aiocblist *aiocbe; 812 struct kaioinfo *ki; 813 struct proc *userp; 814 815 mtx_assert(&aio_job_mtx, MA_OWNED); 816 TAILQ_FOREACH(aiocbe, &aio_jobs, list) { 817 userp = aiocbe->userproc; 818 ki = userp->p_aioinfo; 819 820 if (ki->kaio_active_count < ki->kaio_maxactive_count) { 821 TAILQ_REMOVE(&aio_jobs, aiocbe, list); 822 /* Account for currently active jobs. */ 823 ki->kaio_active_count++; 824 aiocbe->jobstate = JOBST_JOBRUNNING; 825 break; 826 } 827 } 828 return (aiocbe); 829 } 830 831 /* 832 * Move all data to a permanent storage device, this code 833 * simulates fsync syscall. 834 */ 835 static int 836 aio_fsync_vnode(struct thread *td, struct vnode *vp) 837 { 838 struct mount *mp; 839 int error; 840 841 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 842 goto drop; 843 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 844 if (vp->v_object != NULL) { 845 VM_OBJECT_WLOCK(vp->v_object); 846 vm_object_page_clean(vp->v_object, 0, 0, 0); 847 VM_OBJECT_WUNLOCK(vp->v_object); 848 } 849 error = VOP_FSYNC(vp, MNT_WAIT, td); 850 851 VOP_UNLOCK(vp, 0); 852 vn_finished_write(mp); 853 drop: 854 return (error); 855 } 856 857 /* 858 * The AIO processing activity. This is the code that does the I/O request for 859 * the non-physio version of the operations. The normal vn operations are used, 860 * and this code should work in all instances for every type of file, including 861 * pipes, sockets, fifos, and regular files. 862 * 863 * XXX I don't think it works well for socket, pipe, and fifo. 864 */ 865 static void 866 aio_process(struct aiocblist *aiocbe) 867 { 868 struct ucred *td_savedcred; 869 struct thread *td; 870 struct aiocb *cb; 871 struct file *fp; 872 struct socket *so; 873 struct uio auio; 874 struct iovec aiov; 875 int cnt; 876 int error; 877 int oublock_st, oublock_end; 878 int inblock_st, inblock_end; 879 880 td = curthread; 881 td_savedcred = td->td_ucred; 882 td->td_ucred = aiocbe->cred; 883 cb = &aiocbe->uaiocb; 884 fp = aiocbe->fd_file; 885 886 if (cb->aio_lio_opcode == LIO_SYNC) { 887 error = 0; 888 cnt = 0; 889 if (fp->f_vnode != NULL) 890 error = aio_fsync_vnode(td, fp->f_vnode); 891 cb->_aiocb_private.error = error; 892 cb->_aiocb_private.status = 0; 893 td->td_ucred = td_savedcred; 894 return; 895 } 896 897 aiov.iov_base = (void *)(uintptr_t)cb->aio_buf; 898 aiov.iov_len = cb->aio_nbytes; 899 900 auio.uio_iov = &aiov; 901 auio.uio_iovcnt = 1; 902 auio.uio_offset = cb->aio_offset; 903 auio.uio_resid = cb->aio_nbytes; 904 cnt = cb->aio_nbytes; 905 auio.uio_segflg = UIO_USERSPACE; 906 auio.uio_td = td; 907 908 inblock_st = td->td_ru.ru_inblock; 909 oublock_st = td->td_ru.ru_oublock; 910 /* 911 * aio_aqueue() acquires a reference to the file that is 912 * released in aio_free_entry(). 913 */ 914 if (cb->aio_lio_opcode == LIO_READ) { 915 auio.uio_rw = UIO_READ; 916 if (auio.uio_resid == 0) 917 error = 0; 918 else 919 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, td); 920 } else { 921 if (fp->f_type == DTYPE_VNODE) 922 bwillwrite(); 923 auio.uio_rw = UIO_WRITE; 924 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, td); 925 } 926 inblock_end = td->td_ru.ru_inblock; 927 oublock_end = td->td_ru.ru_oublock; 928 929 aiocbe->inputcharge = inblock_end - inblock_st; 930 aiocbe->outputcharge = oublock_end - oublock_st; 931 932 if ((error) && (auio.uio_resid != cnt)) { 933 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK) 934 error = 0; 935 if ((error == EPIPE) && (cb->aio_lio_opcode == LIO_WRITE)) { 936 int sigpipe = 1; 937 if (fp->f_type == DTYPE_SOCKET) { 938 so = fp->f_data; 939 if (so->so_options & SO_NOSIGPIPE) 940 sigpipe = 0; 941 } 942 if (sigpipe) { 943 PROC_LOCK(aiocbe->userproc); 944 kern_psignal(aiocbe->userproc, SIGPIPE); 945 PROC_UNLOCK(aiocbe->userproc); 946 } 947 } 948 } 949 950 cnt -= auio.uio_resid; 951 cb->_aiocb_private.error = error; 952 cb->_aiocb_private.status = cnt; 953 td->td_ucred = td_savedcred; 954 } 955 956 static void 957 aio_bio_done_notify(struct proc *userp, struct aiocblist *aiocbe, int type) 958 { 959 struct aioliojob *lj; 960 struct kaioinfo *ki; 961 struct aiocblist *scb, *scbn; 962 int lj_done; 963 964 ki = userp->p_aioinfo; 965 AIO_LOCK_ASSERT(ki, MA_OWNED); 966 lj = aiocbe->lio; 967 lj_done = 0; 968 if (lj) { 969 lj->lioj_finished_count++; 970 if (lj->lioj_count == lj->lioj_finished_count) 971 lj_done = 1; 972 } 973 if (type == DONE_QUEUE) { 974 aiocbe->jobflags |= AIOCBLIST_DONE; 975 } else { 976 aiocbe->jobflags |= AIOCBLIST_BUFDONE; 977 } 978 TAILQ_INSERT_TAIL(&ki->kaio_done, aiocbe, plist); 979 aiocbe->jobstate = JOBST_JOBFINISHED; 980 981 if (ki->kaio_flags & KAIO_RUNDOWN) 982 goto notification_done; 983 984 if (aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL || 985 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) 986 aio_sendsig(userp, &aiocbe->uaiocb.aio_sigevent, &aiocbe->ksi); 987 988 KNOTE_LOCKED(&aiocbe->klist, 1); 989 990 if (lj_done) { 991 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 992 lj->lioj_flags |= LIOJ_KEVENT_POSTED; 993 KNOTE_LOCKED(&lj->klist, 1); 994 } 995 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) 996 == LIOJ_SIGNAL 997 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 998 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) { 999 aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi); 1000 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 1001 } 1002 } 1003 1004 notification_done: 1005 if (aiocbe->jobflags & AIOCBLIST_CHECKSYNC) { 1006 TAILQ_FOREACH_SAFE(scb, &ki->kaio_syncqueue, list, scbn) { 1007 if (aiocbe->fd_file == scb->fd_file && 1008 aiocbe->seqno < scb->seqno) { 1009 if (--scb->pending == 0) { 1010 mtx_lock(&aio_job_mtx); 1011 scb->jobstate = JOBST_JOBQGLOBAL; 1012 TAILQ_REMOVE(&ki->kaio_syncqueue, scb, list); 1013 TAILQ_INSERT_TAIL(&aio_jobs, scb, list); 1014 aio_kick_nowait(userp); 1015 mtx_unlock(&aio_job_mtx); 1016 } 1017 } 1018 } 1019 } 1020 if (ki->kaio_flags & KAIO_WAKEUP) { 1021 ki->kaio_flags &= ~KAIO_WAKEUP; 1022 wakeup(&userp->p_aioinfo); 1023 } 1024 } 1025 1026 /* 1027 * The AIO daemon, most of the actual work is done in aio_process, 1028 * but the setup (and address space mgmt) is done in this routine. 1029 */ 1030 static void 1031 aio_daemon(void *_id) 1032 { 1033 struct aiocblist *aiocbe; 1034 struct aiothreadlist *aiop; 1035 struct kaioinfo *ki; 1036 struct proc *curcp, *mycp, *userp; 1037 struct vmspace *myvm, *tmpvm; 1038 struct thread *td = curthread; 1039 int id = (intptr_t)_id; 1040 1041 /* 1042 * Local copies of curproc (cp) and vmspace (myvm) 1043 */ 1044 mycp = td->td_proc; 1045 myvm = mycp->p_vmspace; 1046 1047 KASSERT(mycp->p_textvp == NULL, ("kthread has a textvp")); 1048 1049 /* 1050 * Allocate and ready the aio control info. There is one aiop structure 1051 * per daemon. 1052 */ 1053 aiop = uma_zalloc(aiop_zone, M_WAITOK); 1054 aiop->aiothread = td; 1055 aiop->aiothreadflags = 0; 1056 1057 /* The daemon resides in its own pgrp. */ 1058 sys_setsid(td, NULL); 1059 1060 /* 1061 * Wakeup parent process. (Parent sleeps to keep from blasting away 1062 * and creating too many daemons.) 1063 */ 1064 sema_post(&aio_newproc_sem); 1065 1066 mtx_lock(&aio_job_mtx); 1067 for (;;) { 1068 /* 1069 * curcp is the current daemon process context. 1070 * userp is the current user process context. 1071 */ 1072 curcp = mycp; 1073 1074 /* 1075 * Take daemon off of free queue 1076 */ 1077 if (aiop->aiothreadflags & AIOP_FREE) { 1078 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1079 aiop->aiothreadflags &= ~AIOP_FREE; 1080 } 1081 1082 /* 1083 * Check for jobs. 1084 */ 1085 while ((aiocbe = aio_selectjob(aiop)) != NULL) { 1086 mtx_unlock(&aio_job_mtx); 1087 userp = aiocbe->userproc; 1088 1089 /* 1090 * Connect to process address space for user program. 1091 */ 1092 if (userp != curcp) { 1093 /* 1094 * Save the current address space that we are 1095 * connected to. 1096 */ 1097 tmpvm = mycp->p_vmspace; 1098 1099 /* 1100 * Point to the new user address space, and 1101 * refer to it. 1102 */ 1103 mycp->p_vmspace = userp->p_vmspace; 1104 atomic_add_int(&mycp->p_vmspace->vm_refcnt, 1); 1105 1106 /* Activate the new mapping. */ 1107 pmap_activate(FIRST_THREAD_IN_PROC(mycp)); 1108 1109 /* 1110 * If the old address space wasn't the daemons 1111 * own address space, then we need to remove the 1112 * daemon's reference from the other process 1113 * that it was acting on behalf of. 1114 */ 1115 if (tmpvm != myvm) { 1116 vmspace_free(tmpvm); 1117 } 1118 curcp = userp; 1119 } 1120 1121 ki = userp->p_aioinfo; 1122 1123 /* Do the I/O function. */ 1124 aio_process(aiocbe); 1125 1126 mtx_lock(&aio_job_mtx); 1127 /* Decrement the active job count. */ 1128 ki->kaio_active_count--; 1129 mtx_unlock(&aio_job_mtx); 1130 1131 AIO_LOCK(ki); 1132 TAILQ_REMOVE(&ki->kaio_jobqueue, aiocbe, plist); 1133 aio_bio_done_notify(userp, aiocbe, DONE_QUEUE); 1134 AIO_UNLOCK(ki); 1135 1136 mtx_lock(&aio_job_mtx); 1137 } 1138 1139 /* 1140 * Disconnect from user address space. 1141 */ 1142 if (curcp != mycp) { 1143 1144 mtx_unlock(&aio_job_mtx); 1145 1146 /* Get the user address space to disconnect from. */ 1147 tmpvm = mycp->p_vmspace; 1148 1149 /* Get original address space for daemon. */ 1150 mycp->p_vmspace = myvm; 1151 1152 /* Activate the daemon's address space. */ 1153 pmap_activate(FIRST_THREAD_IN_PROC(mycp)); 1154 #ifdef DIAGNOSTIC 1155 if (tmpvm == myvm) { 1156 printf("AIOD: vmspace problem -- %d\n", 1157 mycp->p_pid); 1158 } 1159 #endif 1160 /* Remove our vmspace reference. */ 1161 vmspace_free(tmpvm); 1162 1163 curcp = mycp; 1164 1165 mtx_lock(&aio_job_mtx); 1166 /* 1167 * We have to restart to avoid race, we only sleep if 1168 * no job can be selected, that should be 1169 * curcp == mycp. 1170 */ 1171 continue; 1172 } 1173 1174 mtx_assert(&aio_job_mtx, MA_OWNED); 1175 1176 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list); 1177 aiop->aiothreadflags |= AIOP_FREE; 1178 1179 /* 1180 * If daemon is inactive for a long time, allow it to exit, 1181 * thereby freeing resources. 1182 */ 1183 if (msleep(aiop->aiothread, &aio_job_mtx, PRIBIO, "aiordy", 1184 aiod_lifetime)) { 1185 if (TAILQ_EMPTY(&aio_jobs)) { 1186 if ((aiop->aiothreadflags & AIOP_FREE) && 1187 (num_aio_procs > target_aio_procs)) { 1188 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1189 num_aio_procs--; 1190 mtx_unlock(&aio_job_mtx); 1191 uma_zfree(aiop_zone, aiop); 1192 free_unr(aiod_unr, id); 1193 #ifdef DIAGNOSTIC 1194 if (mycp->p_vmspace->vm_refcnt <= 1) { 1195 printf("AIOD: bad vm refcnt for" 1196 " exiting daemon: %d\n", 1197 mycp->p_vmspace->vm_refcnt); 1198 } 1199 #endif 1200 kproc_exit(0); 1201 } 1202 } 1203 } 1204 } 1205 mtx_unlock(&aio_job_mtx); 1206 panic("shouldn't be here\n"); 1207 } 1208 1209 /* 1210 * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The 1211 * AIO daemon modifies its environment itself. 1212 */ 1213 static int 1214 aio_newproc(int *start) 1215 { 1216 int error; 1217 struct proc *p; 1218 int id; 1219 1220 id = alloc_unr(aiod_unr); 1221 error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p, 1222 RFNOWAIT, 0, "aiod%d", id); 1223 if (error == 0) { 1224 /* 1225 * Wait until daemon is started. 1226 */ 1227 sema_wait(&aio_newproc_sem); 1228 mtx_lock(&aio_job_mtx); 1229 num_aio_procs++; 1230 if (start != NULL) 1231 (*start)--; 1232 mtx_unlock(&aio_job_mtx); 1233 } else { 1234 free_unr(aiod_unr, id); 1235 } 1236 return (error); 1237 } 1238 1239 /* 1240 * Try the high-performance, low-overhead physio method for eligible 1241 * VCHR devices. This method doesn't use an aio helper thread, and 1242 * thus has very low overhead. 1243 * 1244 * Assumes that the caller, aio_aqueue(), has incremented the file 1245 * structure's reference count, preventing its deallocation for the 1246 * duration of this call. 1247 */ 1248 static int 1249 aio_qphysio(struct proc *p, struct aiocblist *aiocbe) 1250 { 1251 struct aiocb *cb; 1252 struct file *fp; 1253 struct buf *bp; 1254 struct vnode *vp; 1255 struct cdevsw *csw; 1256 struct cdev *dev; 1257 struct kaioinfo *ki; 1258 struct aioliojob *lj; 1259 int error, ref; 1260 1261 cb = &aiocbe->uaiocb; 1262 fp = aiocbe->fd_file; 1263 1264 if (fp->f_type != DTYPE_VNODE) 1265 return (-1); 1266 1267 vp = fp->f_vnode; 1268 1269 /* 1270 * If its not a disk, we don't want to return a positive error. 1271 * It causes the aio code to not fall through to try the thread 1272 * way when you're talking to a regular file. 1273 */ 1274 if (!vn_isdisk(vp, &error)) { 1275 if (error == ENOTBLK) 1276 return (-1); 1277 else 1278 return (error); 1279 } 1280 1281 if (vp->v_bufobj.bo_bsize == 0) 1282 return (-1); 1283 1284 if (cb->aio_nbytes % vp->v_bufobj.bo_bsize) 1285 return (-1); 1286 1287 if (cb->aio_nbytes > 1288 MAXPHYS - (((vm_offset_t) cb->aio_buf) & PAGE_MASK)) 1289 return (-1); 1290 1291 ki = p->p_aioinfo; 1292 if (ki->kaio_buffer_count >= ki->kaio_ballowed_count) 1293 return (-1); 1294 1295 ref = 0; 1296 csw = devvn_refthread(vp, &dev, &ref); 1297 if (csw == NULL) 1298 return (ENXIO); 1299 if (cb->aio_nbytes > dev->si_iosize_max) { 1300 error = -1; 1301 goto unref; 1302 } 1303 1304 /* Create and build a buffer header for a transfer. */ 1305 bp = (struct buf *)getpbuf(NULL); 1306 BUF_KERNPROC(bp); 1307 1308 AIO_LOCK(ki); 1309 ki->kaio_count++; 1310 ki->kaio_buffer_count++; 1311 lj = aiocbe->lio; 1312 if (lj) 1313 lj->lioj_count++; 1314 AIO_UNLOCK(ki); 1315 1316 /* 1317 * Get a copy of the kva from the physical buffer. 1318 */ 1319 error = 0; 1320 1321 bp->b_bcount = cb->aio_nbytes; 1322 bp->b_bufsize = cb->aio_nbytes; 1323 bp->b_iodone = aio_physwakeup; 1324 bp->b_saveaddr = bp->b_data; 1325 bp->b_data = (void *)(uintptr_t)cb->aio_buf; 1326 bp->b_offset = cb->aio_offset; 1327 bp->b_iooffset = cb->aio_offset; 1328 bp->b_blkno = btodb(cb->aio_offset); 1329 bp->b_iocmd = cb->aio_lio_opcode == LIO_WRITE ? BIO_WRITE : BIO_READ; 1330 1331 /* 1332 * Bring buffer into kernel space. 1333 */ 1334 if (vmapbuf(bp, (csw->d_flags & D_UNMAPPED_IO) == 0) < 0) { 1335 error = EFAULT; 1336 goto doerror; 1337 } 1338 1339 AIO_LOCK(ki); 1340 aiocbe->bp = bp; 1341 bp->b_caller1 = (void *)aiocbe; 1342 TAILQ_INSERT_TAIL(&ki->kaio_bufqueue, aiocbe, plist); 1343 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist); 1344 aiocbe->jobstate = JOBST_JOBQBUF; 1345 cb->_aiocb_private.status = cb->aio_nbytes; 1346 AIO_UNLOCK(ki); 1347 1348 atomic_add_int(&num_queue_count, 1); 1349 atomic_add_int(&num_buf_aio, 1); 1350 1351 bp->b_error = 0; 1352 1353 TASK_INIT(&aiocbe->biotask, 0, biohelper, aiocbe); 1354 1355 /* Perform transfer. */ 1356 dev_strategy_csw(dev, csw, bp); 1357 dev_relthread(dev, ref); 1358 return (0); 1359 1360 doerror: 1361 AIO_LOCK(ki); 1362 ki->kaio_count--; 1363 ki->kaio_buffer_count--; 1364 if (lj) 1365 lj->lioj_count--; 1366 aiocbe->bp = NULL; 1367 AIO_UNLOCK(ki); 1368 relpbuf(bp, NULL); 1369 unref: 1370 dev_relthread(dev, ref); 1371 return (error); 1372 } 1373 1374 /* 1375 * Wake up aio requests that may be serviceable now. 1376 */ 1377 static void 1378 aio_swake_cb(struct socket *so, struct sockbuf *sb) 1379 { 1380 struct aiocblist *cb, *cbn; 1381 int opcode; 1382 1383 SOCKBUF_LOCK_ASSERT(sb); 1384 if (sb == &so->so_snd) 1385 opcode = LIO_WRITE; 1386 else 1387 opcode = LIO_READ; 1388 1389 sb->sb_flags &= ~SB_AIO; 1390 mtx_lock(&aio_job_mtx); 1391 TAILQ_FOREACH_SAFE(cb, &so->so_aiojobq, list, cbn) { 1392 if (opcode == cb->uaiocb.aio_lio_opcode) { 1393 if (cb->jobstate != JOBST_JOBQSOCK) 1394 panic("invalid queue value"); 1395 /* XXX 1396 * We don't have actual sockets backend yet, 1397 * so we simply move the requests to the generic 1398 * file I/O backend. 1399 */ 1400 TAILQ_REMOVE(&so->so_aiojobq, cb, list); 1401 TAILQ_INSERT_TAIL(&aio_jobs, cb, list); 1402 aio_kick_nowait(cb->userproc); 1403 } 1404 } 1405 mtx_unlock(&aio_job_mtx); 1406 } 1407 1408 static int 1409 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig) 1410 { 1411 1412 /* 1413 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are 1414 * supported by AIO with the old sigevent structure. 1415 */ 1416 nsig->sigev_notify = osig->sigev_notify; 1417 switch (nsig->sigev_notify) { 1418 case SIGEV_NONE: 1419 break; 1420 case SIGEV_SIGNAL: 1421 nsig->sigev_signo = osig->__sigev_u.__sigev_signo; 1422 break; 1423 case SIGEV_KEVENT: 1424 nsig->sigev_notify_kqueue = 1425 osig->__sigev_u.__sigev_notify_kqueue; 1426 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr; 1427 break; 1428 default: 1429 return (EINVAL); 1430 } 1431 return (0); 1432 } 1433 1434 static int 1435 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob) 1436 { 1437 struct oaiocb *ojob; 1438 int error; 1439 1440 bzero(kjob, sizeof(struct aiocb)); 1441 error = copyin(ujob, kjob, sizeof(struct oaiocb)); 1442 if (error) 1443 return (error); 1444 ojob = (struct oaiocb *)kjob; 1445 return (convert_old_sigevent(&ojob->aio_sigevent, &kjob->aio_sigevent)); 1446 } 1447 1448 static int 1449 aiocb_copyin(struct aiocb *ujob, struct aiocb *kjob) 1450 { 1451 1452 return (copyin(ujob, kjob, sizeof(struct aiocb))); 1453 } 1454 1455 static long 1456 aiocb_fetch_status(struct aiocb *ujob) 1457 { 1458 1459 return (fuword(&ujob->_aiocb_private.status)); 1460 } 1461 1462 static long 1463 aiocb_fetch_error(struct aiocb *ujob) 1464 { 1465 1466 return (fuword(&ujob->_aiocb_private.error)); 1467 } 1468 1469 static int 1470 aiocb_store_status(struct aiocb *ujob, long status) 1471 { 1472 1473 return (suword(&ujob->_aiocb_private.status, status)); 1474 } 1475 1476 static int 1477 aiocb_store_error(struct aiocb *ujob, long error) 1478 { 1479 1480 return (suword(&ujob->_aiocb_private.error, error)); 1481 } 1482 1483 static int 1484 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref) 1485 { 1486 1487 return (suword(&ujob->_aiocb_private.kernelinfo, jobref)); 1488 } 1489 1490 static int 1491 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob) 1492 { 1493 1494 return (suword(ujobp, (long)ujob)); 1495 } 1496 1497 static struct aiocb_ops aiocb_ops = { 1498 .copyin = aiocb_copyin, 1499 .fetch_status = aiocb_fetch_status, 1500 .fetch_error = aiocb_fetch_error, 1501 .store_status = aiocb_store_status, 1502 .store_error = aiocb_store_error, 1503 .store_kernelinfo = aiocb_store_kernelinfo, 1504 .store_aiocb = aiocb_store_aiocb, 1505 }; 1506 1507 static struct aiocb_ops aiocb_ops_osigevent = { 1508 .copyin = aiocb_copyin_old_sigevent, 1509 .fetch_status = aiocb_fetch_status, 1510 .fetch_error = aiocb_fetch_error, 1511 .store_status = aiocb_store_status, 1512 .store_error = aiocb_store_error, 1513 .store_kernelinfo = aiocb_store_kernelinfo, 1514 .store_aiocb = aiocb_store_aiocb, 1515 }; 1516 1517 /* 1518 * Queue a new AIO request. Choosing either the threaded or direct physio VCHR 1519 * technique is done in this code. 1520 */ 1521 int 1522 aio_aqueue(struct thread *td, struct aiocb *job, struct aioliojob *lj, 1523 int type, struct aiocb_ops *ops) 1524 { 1525 struct proc *p = td->td_proc; 1526 struct file *fp; 1527 struct socket *so; 1528 struct aiocblist *aiocbe, *cb; 1529 struct kaioinfo *ki; 1530 struct kevent kev; 1531 struct sockbuf *sb; 1532 int opcode; 1533 int error; 1534 int fd, kqfd; 1535 int jid; 1536 u_short evflags; 1537 1538 if (p->p_aioinfo == NULL) 1539 aio_init_aioinfo(p); 1540 1541 ki = p->p_aioinfo; 1542 1543 ops->store_status(job, -1); 1544 ops->store_error(job, 0); 1545 ops->store_kernelinfo(job, -1); 1546 1547 if (num_queue_count >= max_queue_count || 1548 ki->kaio_count >= ki->kaio_qallowed_count) { 1549 ops->store_error(job, EAGAIN); 1550 return (EAGAIN); 1551 } 1552 1553 aiocbe = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO); 1554 aiocbe->inputcharge = 0; 1555 aiocbe->outputcharge = 0; 1556 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki)); 1557 1558 error = ops->copyin(job, &aiocbe->uaiocb); 1559 if (error) { 1560 ops->store_error(job, error); 1561 uma_zfree(aiocb_zone, aiocbe); 1562 return (error); 1563 } 1564 1565 /* XXX: aio_nbytes is later casted to signed types. */ 1566 if (aiocbe->uaiocb.aio_nbytes > INT_MAX) { 1567 uma_zfree(aiocb_zone, aiocbe); 1568 return (EINVAL); 1569 } 1570 1571 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT && 1572 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL && 1573 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID && 1574 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) { 1575 ops->store_error(job, EINVAL); 1576 uma_zfree(aiocb_zone, aiocbe); 1577 return (EINVAL); 1578 } 1579 1580 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL || 1581 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) && 1582 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) { 1583 uma_zfree(aiocb_zone, aiocbe); 1584 return (EINVAL); 1585 } 1586 1587 ksiginfo_init(&aiocbe->ksi); 1588 1589 /* Save userspace address of the job info. */ 1590 aiocbe->uuaiocb = job; 1591 1592 /* Get the opcode. */ 1593 if (type != LIO_NOP) 1594 aiocbe->uaiocb.aio_lio_opcode = type; 1595 opcode = aiocbe->uaiocb.aio_lio_opcode; 1596 1597 /* 1598 * Validate the opcode and fetch the file object for the specified 1599 * file descriptor. 1600 * 1601 * XXXRW: Moved the opcode validation up here so that we don't 1602 * retrieve a file descriptor without knowing what the capabiltity 1603 * should be. 1604 */ 1605 fd = aiocbe->uaiocb.aio_fildes; 1606 switch (opcode) { 1607 case LIO_WRITE: 1608 error = fget_write(td, fd, CAP_PWRITE, &fp); 1609 break; 1610 case LIO_READ: 1611 error = fget_read(td, fd, CAP_PREAD, &fp); 1612 break; 1613 case LIO_SYNC: 1614 error = fget(td, fd, CAP_FSYNC, &fp); 1615 break; 1616 case LIO_NOP: 1617 error = fget(td, fd, CAP_NONE, &fp); 1618 break; 1619 default: 1620 error = EINVAL; 1621 } 1622 if (error) { 1623 uma_zfree(aiocb_zone, aiocbe); 1624 ops->store_error(job, error); 1625 return (error); 1626 } 1627 1628 if (opcode == LIO_SYNC && fp->f_vnode == NULL) { 1629 error = EINVAL; 1630 goto aqueue_fail; 1631 } 1632 1633 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) { 1634 error = EINVAL; 1635 goto aqueue_fail; 1636 } 1637 1638 aiocbe->fd_file = fp; 1639 1640 mtx_lock(&aio_job_mtx); 1641 jid = jobrefid++; 1642 aiocbe->seqno = jobseqno++; 1643 mtx_unlock(&aio_job_mtx); 1644 error = ops->store_kernelinfo(job, jid); 1645 if (error) { 1646 error = EINVAL; 1647 goto aqueue_fail; 1648 } 1649 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid; 1650 1651 if (opcode == LIO_NOP) { 1652 fdrop(fp, td); 1653 uma_zfree(aiocb_zone, aiocbe); 1654 return (0); 1655 } 1656 1657 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT) 1658 goto no_kqueue; 1659 evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags; 1660 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) { 1661 error = EINVAL; 1662 goto aqueue_fail; 1663 } 1664 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue; 1665 kev.ident = (uintptr_t)aiocbe->uuaiocb; 1666 kev.filter = EVFILT_AIO; 1667 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags; 1668 kev.data = (intptr_t)aiocbe; 1669 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr; 1670 error = kqfd_register(kqfd, &kev, td, 1); 1671 aqueue_fail: 1672 if (error) { 1673 fdrop(fp, td); 1674 uma_zfree(aiocb_zone, aiocbe); 1675 ops->store_error(job, error); 1676 goto done; 1677 } 1678 no_kqueue: 1679 1680 ops->store_error(job, EINPROGRESS); 1681 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS; 1682 aiocbe->userproc = p; 1683 aiocbe->cred = crhold(td->td_ucred); 1684 aiocbe->jobflags = 0; 1685 aiocbe->lio = lj; 1686 1687 if (opcode == LIO_SYNC) 1688 goto queueit; 1689 1690 if (fp->f_type == DTYPE_SOCKET) { 1691 /* 1692 * Alternate queueing for socket ops: Reach down into the 1693 * descriptor to get the socket data. Then check to see if the 1694 * socket is ready to be read or written (based on the requested 1695 * operation). 1696 * 1697 * If it is not ready for io, then queue the aiocbe on the 1698 * socket, and set the flags so we get a call when sbnotify() 1699 * happens. 1700 * 1701 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock 1702 * and unlock the snd sockbuf for no reason. 1703 */ 1704 so = fp->f_data; 1705 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd; 1706 SOCKBUF_LOCK(sb); 1707 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode == 1708 LIO_WRITE) && (!sowriteable(so)))) { 1709 sb->sb_flags |= SB_AIO; 1710 1711 mtx_lock(&aio_job_mtx); 1712 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list); 1713 mtx_unlock(&aio_job_mtx); 1714 1715 AIO_LOCK(ki); 1716 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist); 1717 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist); 1718 aiocbe->jobstate = JOBST_JOBQSOCK; 1719 ki->kaio_count++; 1720 if (lj) 1721 lj->lioj_count++; 1722 AIO_UNLOCK(ki); 1723 SOCKBUF_UNLOCK(sb); 1724 atomic_add_int(&num_queue_count, 1); 1725 error = 0; 1726 goto done; 1727 } 1728 SOCKBUF_UNLOCK(sb); 1729 } 1730 1731 if ((error = aio_qphysio(p, aiocbe)) == 0) 1732 goto done; 1733 #if 0 1734 if (error > 0) { 1735 aiocbe->uaiocb._aiocb_private.error = error; 1736 ops->store_error(job, error); 1737 goto done; 1738 } 1739 #endif 1740 queueit: 1741 /* No buffer for daemon I/O. */ 1742 aiocbe->bp = NULL; 1743 atomic_add_int(&num_queue_count, 1); 1744 1745 AIO_LOCK(ki); 1746 ki->kaio_count++; 1747 if (lj) 1748 lj->lioj_count++; 1749 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist); 1750 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist); 1751 if (opcode == LIO_SYNC) { 1752 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) { 1753 if (cb->fd_file == aiocbe->fd_file && 1754 cb->uaiocb.aio_lio_opcode != LIO_SYNC && 1755 cb->seqno < aiocbe->seqno) { 1756 cb->jobflags |= AIOCBLIST_CHECKSYNC; 1757 aiocbe->pending++; 1758 } 1759 } 1760 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) { 1761 if (cb->fd_file == aiocbe->fd_file && 1762 cb->uaiocb.aio_lio_opcode != LIO_SYNC && 1763 cb->seqno < aiocbe->seqno) { 1764 cb->jobflags |= AIOCBLIST_CHECKSYNC; 1765 aiocbe->pending++; 1766 } 1767 } 1768 if (aiocbe->pending != 0) { 1769 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list); 1770 aiocbe->jobstate = JOBST_JOBQSYNC; 1771 AIO_UNLOCK(ki); 1772 goto done; 1773 } 1774 } 1775 mtx_lock(&aio_job_mtx); 1776 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list); 1777 aiocbe->jobstate = JOBST_JOBQGLOBAL; 1778 aio_kick_nowait(p); 1779 mtx_unlock(&aio_job_mtx); 1780 AIO_UNLOCK(ki); 1781 error = 0; 1782 done: 1783 return (error); 1784 } 1785 1786 static void 1787 aio_kick_nowait(struct proc *userp) 1788 { 1789 struct kaioinfo *ki = userp->p_aioinfo; 1790 struct aiothreadlist *aiop; 1791 1792 mtx_assert(&aio_job_mtx, MA_OWNED); 1793 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1794 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1795 aiop->aiothreadflags &= ~AIOP_FREE; 1796 wakeup(aiop->aiothread); 1797 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) && 1798 ((ki->kaio_active_count + num_aio_resv_start) < 1799 ki->kaio_maxactive_count)) { 1800 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task); 1801 } 1802 } 1803 1804 static int 1805 aio_kick(struct proc *userp) 1806 { 1807 struct kaioinfo *ki = userp->p_aioinfo; 1808 struct aiothreadlist *aiop; 1809 int error, ret = 0; 1810 1811 mtx_assert(&aio_job_mtx, MA_OWNED); 1812 retryproc: 1813 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1814 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1815 aiop->aiothreadflags &= ~AIOP_FREE; 1816 wakeup(aiop->aiothread); 1817 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) && 1818 ((ki->kaio_active_count + num_aio_resv_start) < 1819 ki->kaio_maxactive_count)) { 1820 num_aio_resv_start++; 1821 mtx_unlock(&aio_job_mtx); 1822 error = aio_newproc(&num_aio_resv_start); 1823 mtx_lock(&aio_job_mtx); 1824 if (error) { 1825 num_aio_resv_start--; 1826 goto retryproc; 1827 } 1828 } else { 1829 ret = -1; 1830 } 1831 return (ret); 1832 } 1833 1834 static void 1835 aio_kick_helper(void *context, int pending) 1836 { 1837 struct proc *userp = context; 1838 1839 mtx_lock(&aio_job_mtx); 1840 while (--pending >= 0) { 1841 if (aio_kick(userp)) 1842 break; 1843 } 1844 mtx_unlock(&aio_job_mtx); 1845 } 1846 1847 /* 1848 * Support the aio_return system call, as a side-effect, kernel resources are 1849 * released. 1850 */ 1851 static int 1852 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops) 1853 { 1854 struct proc *p = td->td_proc; 1855 struct aiocblist *cb; 1856 struct kaioinfo *ki; 1857 int status, error; 1858 1859 ki = p->p_aioinfo; 1860 if (ki == NULL) 1861 return (EINVAL); 1862 AIO_LOCK(ki); 1863 TAILQ_FOREACH(cb, &ki->kaio_done, plist) { 1864 if (cb->uuaiocb == uaiocb) 1865 break; 1866 } 1867 if (cb != NULL) { 1868 MPASS(cb->jobstate == JOBST_JOBFINISHED); 1869 status = cb->uaiocb._aiocb_private.status; 1870 error = cb->uaiocb._aiocb_private.error; 1871 td->td_retval[0] = status; 1872 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 1873 td->td_ru.ru_oublock += cb->outputcharge; 1874 cb->outputcharge = 0; 1875 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 1876 td->td_ru.ru_inblock += cb->inputcharge; 1877 cb->inputcharge = 0; 1878 } 1879 aio_free_entry(cb); 1880 AIO_UNLOCK(ki); 1881 ops->store_error(uaiocb, error); 1882 ops->store_status(uaiocb, status); 1883 } else { 1884 error = EINVAL; 1885 AIO_UNLOCK(ki); 1886 } 1887 return (error); 1888 } 1889 1890 int 1891 sys_aio_return(struct thread *td, struct aio_return_args *uap) 1892 { 1893 1894 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops)); 1895 } 1896 1897 /* 1898 * Allow a process to wakeup when any of the I/O requests are completed. 1899 */ 1900 static int 1901 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist, 1902 struct timespec *ts) 1903 { 1904 struct proc *p = td->td_proc; 1905 struct timeval atv; 1906 struct kaioinfo *ki; 1907 struct aiocblist *cb, *cbfirst; 1908 int error, i, timo; 1909 1910 timo = 0; 1911 if (ts) { 1912 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 1913 return (EINVAL); 1914 1915 TIMESPEC_TO_TIMEVAL(&atv, ts); 1916 if (itimerfix(&atv)) 1917 return (EINVAL); 1918 timo = tvtohz(&atv); 1919 } 1920 1921 ki = p->p_aioinfo; 1922 if (ki == NULL) 1923 return (EAGAIN); 1924 1925 if (njoblist == 0) 1926 return (0); 1927 1928 AIO_LOCK(ki); 1929 for (;;) { 1930 cbfirst = NULL; 1931 error = 0; 1932 TAILQ_FOREACH(cb, &ki->kaio_all, allist) { 1933 for (i = 0; i < njoblist; i++) { 1934 if (cb->uuaiocb == ujoblist[i]) { 1935 if (cbfirst == NULL) 1936 cbfirst = cb; 1937 if (cb->jobstate == JOBST_JOBFINISHED) 1938 goto RETURN; 1939 } 1940 } 1941 } 1942 /* All tasks were finished. */ 1943 if (cbfirst == NULL) 1944 break; 1945 1946 ki->kaio_flags |= KAIO_WAKEUP; 1947 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 1948 "aiospn", timo); 1949 if (error == ERESTART) 1950 error = EINTR; 1951 if (error) 1952 break; 1953 } 1954 RETURN: 1955 AIO_UNLOCK(ki); 1956 return (error); 1957 } 1958 1959 int 1960 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap) 1961 { 1962 struct timespec ts, *tsp; 1963 struct aiocb **ujoblist; 1964 int error; 1965 1966 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX) 1967 return (EINVAL); 1968 1969 if (uap->timeout) { 1970 /* Get timespec struct. */ 1971 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) 1972 return (error); 1973 tsp = &ts; 1974 } else 1975 tsp = NULL; 1976 1977 ujoblist = uma_zalloc(aiol_zone, M_WAITOK); 1978 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0])); 1979 if (error == 0) 1980 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 1981 uma_zfree(aiol_zone, ujoblist); 1982 return (error); 1983 } 1984 1985 /* 1986 * aio_cancel cancels any non-physio aio operations not currently in 1987 * progress. 1988 */ 1989 int 1990 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap) 1991 { 1992 struct proc *p = td->td_proc; 1993 struct kaioinfo *ki; 1994 struct aiocblist *cbe, *cbn; 1995 struct file *fp; 1996 struct socket *so; 1997 int error; 1998 int remove; 1999 int cancelled = 0; 2000 int notcancelled = 0; 2001 struct vnode *vp; 2002 2003 /* Lookup file object. */ 2004 error = fget(td, uap->fd, 0, &fp); 2005 if (error) 2006 return (error); 2007 2008 ki = p->p_aioinfo; 2009 if (ki == NULL) 2010 goto done; 2011 2012 if (fp->f_type == DTYPE_VNODE) { 2013 vp = fp->f_vnode; 2014 if (vn_isdisk(vp, &error)) { 2015 fdrop(fp, td); 2016 td->td_retval[0] = AIO_NOTCANCELED; 2017 return (0); 2018 } 2019 } 2020 2021 AIO_LOCK(ki); 2022 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) { 2023 if ((uap->fd == cbe->uaiocb.aio_fildes) && 2024 ((uap->aiocbp == NULL) || 2025 (uap->aiocbp == cbe->uuaiocb))) { 2026 remove = 0; 2027 2028 mtx_lock(&aio_job_mtx); 2029 if (cbe->jobstate == JOBST_JOBQGLOBAL) { 2030 TAILQ_REMOVE(&aio_jobs, cbe, list); 2031 remove = 1; 2032 } else if (cbe->jobstate == JOBST_JOBQSOCK) { 2033 MPASS(fp->f_type == DTYPE_SOCKET); 2034 so = fp->f_data; 2035 TAILQ_REMOVE(&so->so_aiojobq, cbe, list); 2036 remove = 1; 2037 } else if (cbe->jobstate == JOBST_JOBQSYNC) { 2038 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list); 2039 remove = 1; 2040 } 2041 mtx_unlock(&aio_job_mtx); 2042 2043 if (remove) { 2044 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist); 2045 cbe->uaiocb._aiocb_private.status = -1; 2046 cbe->uaiocb._aiocb_private.error = ECANCELED; 2047 aio_bio_done_notify(p, cbe, DONE_QUEUE); 2048 cancelled++; 2049 } else { 2050 notcancelled++; 2051 } 2052 if (uap->aiocbp != NULL) 2053 break; 2054 } 2055 } 2056 AIO_UNLOCK(ki); 2057 2058 done: 2059 fdrop(fp, td); 2060 2061 if (uap->aiocbp != NULL) { 2062 if (cancelled) { 2063 td->td_retval[0] = AIO_CANCELED; 2064 return (0); 2065 } 2066 } 2067 2068 if (notcancelled) { 2069 td->td_retval[0] = AIO_NOTCANCELED; 2070 return (0); 2071 } 2072 2073 if (cancelled) { 2074 td->td_retval[0] = AIO_CANCELED; 2075 return (0); 2076 } 2077 2078 td->td_retval[0] = AIO_ALLDONE; 2079 2080 return (0); 2081 } 2082 2083 /* 2084 * aio_error is implemented in the kernel level for compatibility purposes 2085 * only. For a user mode async implementation, it would be best to do it in 2086 * a userland subroutine. 2087 */ 2088 static int 2089 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops) 2090 { 2091 struct proc *p = td->td_proc; 2092 struct aiocblist *cb; 2093 struct kaioinfo *ki; 2094 int status; 2095 2096 ki = p->p_aioinfo; 2097 if (ki == NULL) { 2098 td->td_retval[0] = EINVAL; 2099 return (0); 2100 } 2101 2102 AIO_LOCK(ki); 2103 TAILQ_FOREACH(cb, &ki->kaio_all, allist) { 2104 if (cb->uuaiocb == aiocbp) { 2105 if (cb->jobstate == JOBST_JOBFINISHED) 2106 td->td_retval[0] = 2107 cb->uaiocb._aiocb_private.error; 2108 else 2109 td->td_retval[0] = EINPROGRESS; 2110 AIO_UNLOCK(ki); 2111 return (0); 2112 } 2113 } 2114 AIO_UNLOCK(ki); 2115 2116 /* 2117 * Hack for failure of aio_aqueue. 2118 */ 2119 status = ops->fetch_status(aiocbp); 2120 if (status == -1) { 2121 td->td_retval[0] = ops->fetch_error(aiocbp); 2122 return (0); 2123 } 2124 2125 td->td_retval[0] = EINVAL; 2126 return (0); 2127 } 2128 2129 int 2130 sys_aio_error(struct thread *td, struct aio_error_args *uap) 2131 { 2132 2133 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops)); 2134 } 2135 2136 /* syscall - asynchronous read from a file (REALTIME) */ 2137 int 2138 sys_oaio_read(struct thread *td, struct oaio_read_args *uap) 2139 { 2140 2141 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2142 &aiocb_ops_osigevent)); 2143 } 2144 2145 int 2146 sys_aio_read(struct thread *td, struct aio_read_args *uap) 2147 { 2148 2149 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops)); 2150 } 2151 2152 /* syscall - asynchronous write to a file (REALTIME) */ 2153 int 2154 sys_oaio_write(struct thread *td, struct oaio_write_args *uap) 2155 { 2156 2157 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2158 &aiocb_ops_osigevent)); 2159 } 2160 2161 int 2162 sys_aio_write(struct thread *td, struct aio_write_args *uap) 2163 { 2164 2165 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops)); 2166 } 2167 2168 static int 2169 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list, 2170 struct aiocb **acb_list, int nent, struct sigevent *sig, 2171 struct aiocb_ops *ops) 2172 { 2173 struct proc *p = td->td_proc; 2174 struct aiocb *iocb; 2175 struct kaioinfo *ki; 2176 struct aioliojob *lj; 2177 struct kevent kev; 2178 int error; 2179 int nerror; 2180 int i; 2181 2182 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT)) 2183 return (EINVAL); 2184 2185 if (nent < 0 || nent > AIO_LISTIO_MAX) 2186 return (EINVAL); 2187 2188 if (p->p_aioinfo == NULL) 2189 aio_init_aioinfo(p); 2190 2191 ki = p->p_aioinfo; 2192 2193 lj = uma_zalloc(aiolio_zone, M_WAITOK); 2194 lj->lioj_flags = 0; 2195 lj->lioj_count = 0; 2196 lj->lioj_finished_count = 0; 2197 knlist_init_mtx(&lj->klist, AIO_MTX(ki)); 2198 ksiginfo_init(&lj->lioj_ksi); 2199 2200 /* 2201 * Setup signal. 2202 */ 2203 if (sig && (mode == LIO_NOWAIT)) { 2204 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal)); 2205 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 2206 /* Assume only new style KEVENT */ 2207 kev.filter = EVFILT_LIO; 2208 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1; 2209 kev.ident = (uintptr_t)uacb_list; /* something unique */ 2210 kev.data = (intptr_t)lj; 2211 /* pass user defined sigval data */ 2212 kev.udata = lj->lioj_signal.sigev_value.sival_ptr; 2213 error = kqfd_register( 2214 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1); 2215 if (error) { 2216 uma_zfree(aiolio_zone, lj); 2217 return (error); 2218 } 2219 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) { 2220 ; 2221 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 2222 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) { 2223 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) { 2224 uma_zfree(aiolio_zone, lj); 2225 return EINVAL; 2226 } 2227 lj->lioj_flags |= LIOJ_SIGNAL; 2228 } else { 2229 uma_zfree(aiolio_zone, lj); 2230 return EINVAL; 2231 } 2232 } 2233 2234 AIO_LOCK(ki); 2235 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list); 2236 /* 2237 * Add extra aiocb count to avoid the lio to be freed 2238 * by other threads doing aio_waitcomplete or aio_return, 2239 * and prevent event from being sent until we have queued 2240 * all tasks. 2241 */ 2242 lj->lioj_count = 1; 2243 AIO_UNLOCK(ki); 2244 2245 /* 2246 * Get pointers to the list of I/O requests. 2247 */ 2248 nerror = 0; 2249 for (i = 0; i < nent; i++) { 2250 iocb = acb_list[i]; 2251 if (iocb != NULL) { 2252 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops); 2253 if (error != 0) 2254 nerror++; 2255 } 2256 } 2257 2258 error = 0; 2259 AIO_LOCK(ki); 2260 if (mode == LIO_WAIT) { 2261 while (lj->lioj_count - 1 != lj->lioj_finished_count) { 2262 ki->kaio_flags |= KAIO_WAKEUP; 2263 error = msleep(&p->p_aioinfo, AIO_MTX(ki), 2264 PRIBIO | PCATCH, "aiospn", 0); 2265 if (error == ERESTART) 2266 error = EINTR; 2267 if (error) 2268 break; 2269 } 2270 } else { 2271 if (lj->lioj_count - 1 == lj->lioj_finished_count) { 2272 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 2273 lj->lioj_flags |= LIOJ_KEVENT_POSTED; 2274 KNOTE_LOCKED(&lj->klist, 1); 2275 } 2276 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) 2277 == LIOJ_SIGNAL 2278 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 2279 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) { 2280 aio_sendsig(p, &lj->lioj_signal, 2281 &lj->lioj_ksi); 2282 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 2283 } 2284 } 2285 } 2286 lj->lioj_count--; 2287 if (lj->lioj_count == 0) { 2288 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 2289 knlist_delete(&lj->klist, curthread, 1); 2290 PROC_LOCK(p); 2291 sigqueue_take(&lj->lioj_ksi); 2292 PROC_UNLOCK(p); 2293 AIO_UNLOCK(ki); 2294 uma_zfree(aiolio_zone, lj); 2295 } else 2296 AIO_UNLOCK(ki); 2297 2298 if (nerror) 2299 return (EIO); 2300 return (error); 2301 } 2302 2303 /* syscall - list directed I/O (REALTIME) */ 2304 int 2305 sys_olio_listio(struct thread *td, struct olio_listio_args *uap) 2306 { 2307 struct aiocb **acb_list; 2308 struct sigevent *sigp, sig; 2309 struct osigevent osig; 2310 int error, nent; 2311 2312 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2313 return (EINVAL); 2314 2315 nent = uap->nent; 2316 if (nent < 0 || nent > AIO_LISTIO_MAX) 2317 return (EINVAL); 2318 2319 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2320 error = copyin(uap->sig, &osig, sizeof(osig)); 2321 if (error) 2322 return (error); 2323 error = convert_old_sigevent(&osig, &sig); 2324 if (error) 2325 return (error); 2326 sigp = &sig; 2327 } else 2328 sigp = NULL; 2329 2330 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2331 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 2332 if (error == 0) 2333 error = kern_lio_listio(td, uap->mode, 2334 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 2335 &aiocb_ops_osigevent); 2336 free(acb_list, M_LIO); 2337 return (error); 2338 } 2339 2340 /* syscall - list directed I/O (REALTIME) */ 2341 int 2342 sys_lio_listio(struct thread *td, struct lio_listio_args *uap) 2343 { 2344 struct aiocb **acb_list; 2345 struct sigevent *sigp, sig; 2346 int error, nent; 2347 2348 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2349 return (EINVAL); 2350 2351 nent = uap->nent; 2352 if (nent < 0 || nent > AIO_LISTIO_MAX) 2353 return (EINVAL); 2354 2355 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2356 error = copyin(uap->sig, &sig, sizeof(sig)); 2357 if (error) 2358 return (error); 2359 sigp = &sig; 2360 } else 2361 sigp = NULL; 2362 2363 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2364 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 2365 if (error == 0) 2366 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list, 2367 nent, sigp, &aiocb_ops); 2368 free(acb_list, M_LIO); 2369 return (error); 2370 } 2371 2372 /* 2373 * Called from interrupt thread for physio, we should return as fast 2374 * as possible, so we schedule a biohelper task. 2375 */ 2376 static void 2377 aio_physwakeup(struct buf *bp) 2378 { 2379 struct aiocblist *aiocbe; 2380 2381 aiocbe = (struct aiocblist *)bp->b_caller1; 2382 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask); 2383 } 2384 2385 /* 2386 * Task routine to perform heavy tasks, process wakeup, and signals. 2387 */ 2388 static void 2389 biohelper(void *context, int pending) 2390 { 2391 struct aiocblist *aiocbe = context; 2392 struct buf *bp; 2393 struct proc *userp; 2394 struct kaioinfo *ki; 2395 int nblks; 2396 2397 bp = aiocbe->bp; 2398 userp = aiocbe->userproc; 2399 ki = userp->p_aioinfo; 2400 AIO_LOCK(ki); 2401 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid; 2402 aiocbe->uaiocb._aiocb_private.error = 0; 2403 if (bp->b_ioflags & BIO_ERROR) 2404 aiocbe->uaiocb._aiocb_private.error = bp->b_error; 2405 nblks = btodb(aiocbe->uaiocb.aio_nbytes); 2406 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE) 2407 aiocbe->outputcharge += nblks; 2408 else 2409 aiocbe->inputcharge += nblks; 2410 aiocbe->bp = NULL; 2411 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist); 2412 ki->kaio_buffer_count--; 2413 aio_bio_done_notify(userp, aiocbe, DONE_BUF); 2414 AIO_UNLOCK(ki); 2415 2416 /* Release mapping into kernel space. */ 2417 vunmapbuf(bp); 2418 relpbuf(bp, NULL); 2419 atomic_subtract_int(&num_buf_aio, 1); 2420 } 2421 2422 /* syscall - wait for the next completion of an aio request */ 2423 static int 2424 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp, 2425 struct timespec *ts, struct aiocb_ops *ops) 2426 { 2427 struct proc *p = td->td_proc; 2428 struct timeval atv; 2429 struct kaioinfo *ki; 2430 struct aiocblist *cb; 2431 struct aiocb *uuaiocb; 2432 int error, status, timo; 2433 2434 ops->store_aiocb(aiocbp, NULL); 2435 2436 timo = 0; 2437 if (ts) { 2438 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000)) 2439 return (EINVAL); 2440 2441 TIMESPEC_TO_TIMEVAL(&atv, ts); 2442 if (itimerfix(&atv)) 2443 return (EINVAL); 2444 timo = tvtohz(&atv); 2445 } 2446 2447 if (p->p_aioinfo == NULL) 2448 aio_init_aioinfo(p); 2449 ki = p->p_aioinfo; 2450 2451 error = 0; 2452 cb = NULL; 2453 AIO_LOCK(ki); 2454 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) { 2455 ki->kaio_flags |= KAIO_WAKEUP; 2456 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 2457 "aiowc", timo); 2458 if (timo && error == ERESTART) 2459 error = EINTR; 2460 if (error) 2461 break; 2462 } 2463 2464 if (cb != NULL) { 2465 MPASS(cb->jobstate == JOBST_JOBFINISHED); 2466 uuaiocb = cb->uuaiocb; 2467 status = cb->uaiocb._aiocb_private.status; 2468 error = cb->uaiocb._aiocb_private.error; 2469 td->td_retval[0] = status; 2470 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 2471 td->td_ru.ru_oublock += cb->outputcharge; 2472 cb->outputcharge = 0; 2473 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 2474 td->td_ru.ru_inblock += cb->inputcharge; 2475 cb->inputcharge = 0; 2476 } 2477 aio_free_entry(cb); 2478 AIO_UNLOCK(ki); 2479 ops->store_aiocb(aiocbp, uuaiocb); 2480 ops->store_error(uuaiocb, error); 2481 ops->store_status(uuaiocb, status); 2482 } else 2483 AIO_UNLOCK(ki); 2484 2485 return (error); 2486 } 2487 2488 int 2489 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap) 2490 { 2491 struct timespec ts, *tsp; 2492 int error; 2493 2494 if (uap->timeout) { 2495 /* Get timespec struct. */ 2496 error = copyin(uap->timeout, &ts, sizeof(ts)); 2497 if (error) 2498 return (error); 2499 tsp = &ts; 2500 } else 2501 tsp = NULL; 2502 2503 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops)); 2504 } 2505 2506 static int 2507 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp, 2508 struct aiocb_ops *ops) 2509 { 2510 struct proc *p = td->td_proc; 2511 struct kaioinfo *ki; 2512 2513 if (op != O_SYNC) /* XXX lack of O_DSYNC */ 2514 return (EINVAL); 2515 ki = p->p_aioinfo; 2516 if (ki == NULL) 2517 aio_init_aioinfo(p); 2518 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops)); 2519 } 2520 2521 int 2522 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap) 2523 { 2524 2525 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops)); 2526 } 2527 2528 /* kqueue attach function */ 2529 static int 2530 filt_aioattach(struct knote *kn) 2531 { 2532 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2533 2534 /* 2535 * The aiocbe pointer must be validated before using it, so 2536 * registration is restricted to the kernel; the user cannot 2537 * set EV_FLAG1. 2538 */ 2539 if ((kn->kn_flags & EV_FLAG1) == 0) 2540 return (EPERM); 2541 kn->kn_ptr.p_aio = aiocbe; 2542 kn->kn_flags &= ~EV_FLAG1; 2543 2544 knlist_add(&aiocbe->klist, kn, 0); 2545 2546 return (0); 2547 } 2548 2549 /* kqueue detach function */ 2550 static void 2551 filt_aiodetach(struct knote *kn) 2552 { 2553 struct knlist *knl; 2554 2555 knl = &kn->kn_ptr.p_aio->klist; 2556 knl->kl_lock(knl->kl_lockarg); 2557 if (!knlist_empty(knl)) 2558 knlist_remove(knl, kn, 1); 2559 knl->kl_unlock(knl->kl_lockarg); 2560 } 2561 2562 /* kqueue filter function */ 2563 /*ARGSUSED*/ 2564 static int 2565 filt_aio(struct knote *kn, long hint) 2566 { 2567 struct aiocblist *aiocbe = kn->kn_ptr.p_aio; 2568 2569 kn->kn_data = aiocbe->uaiocb._aiocb_private.error; 2570 if (aiocbe->jobstate != JOBST_JOBFINISHED) 2571 return (0); 2572 kn->kn_flags |= EV_EOF; 2573 return (1); 2574 } 2575 2576 /* kqueue attach function */ 2577 static int 2578 filt_lioattach(struct knote *kn) 2579 { 2580 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata; 2581 2582 /* 2583 * The aioliojob pointer must be validated before using it, so 2584 * registration is restricted to the kernel; the user cannot 2585 * set EV_FLAG1. 2586 */ 2587 if ((kn->kn_flags & EV_FLAG1) == 0) 2588 return (EPERM); 2589 kn->kn_ptr.p_lio = lj; 2590 kn->kn_flags &= ~EV_FLAG1; 2591 2592 knlist_add(&lj->klist, kn, 0); 2593 2594 return (0); 2595 } 2596 2597 /* kqueue detach function */ 2598 static void 2599 filt_liodetach(struct knote *kn) 2600 { 2601 struct knlist *knl; 2602 2603 knl = &kn->kn_ptr.p_lio->klist; 2604 knl->kl_lock(knl->kl_lockarg); 2605 if (!knlist_empty(knl)) 2606 knlist_remove(knl, kn, 1); 2607 knl->kl_unlock(knl->kl_lockarg); 2608 } 2609 2610 /* kqueue filter function */ 2611 /*ARGSUSED*/ 2612 static int 2613 filt_lio(struct knote *kn, long hint) 2614 { 2615 struct aioliojob * lj = kn->kn_ptr.p_lio; 2616 2617 return (lj->lioj_flags & LIOJ_KEVENT_POSTED); 2618 } 2619 2620 #ifdef COMPAT_FREEBSD32 2621 2622 struct __aiocb_private32 { 2623 int32_t status; 2624 int32_t error; 2625 uint32_t kernelinfo; 2626 }; 2627 2628 typedef struct oaiocb32 { 2629 int aio_fildes; /* File descriptor */ 2630 uint64_t aio_offset __packed; /* File offset for I/O */ 2631 uint32_t aio_buf; /* I/O buffer in process space */ 2632 uint32_t aio_nbytes; /* Number of bytes for I/O */ 2633 struct osigevent32 aio_sigevent; /* Signal to deliver */ 2634 int aio_lio_opcode; /* LIO opcode */ 2635 int aio_reqprio; /* Request priority -- ignored */ 2636 struct __aiocb_private32 _aiocb_private; 2637 } oaiocb32_t; 2638 2639 typedef struct aiocb32 { 2640 int32_t aio_fildes; /* File descriptor */ 2641 uint64_t aio_offset __packed; /* File offset for I/O */ 2642 uint32_t aio_buf; /* I/O buffer in process space */ 2643 uint32_t aio_nbytes; /* Number of bytes for I/O */ 2644 int __spare__[2]; 2645 uint32_t __spare2__; 2646 int aio_lio_opcode; /* LIO opcode */ 2647 int aio_reqprio; /* Request priority -- ignored */ 2648 struct __aiocb_private32 _aiocb_private; 2649 struct sigevent32 aio_sigevent; /* Signal to deliver */ 2650 } aiocb32_t; 2651 2652 static int 2653 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig) 2654 { 2655 2656 /* 2657 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are 2658 * supported by AIO with the old sigevent structure. 2659 */ 2660 CP(*osig, *nsig, sigev_notify); 2661 switch (nsig->sigev_notify) { 2662 case SIGEV_NONE: 2663 break; 2664 case SIGEV_SIGNAL: 2665 nsig->sigev_signo = osig->__sigev_u.__sigev_signo; 2666 break; 2667 case SIGEV_KEVENT: 2668 nsig->sigev_notify_kqueue = 2669 osig->__sigev_u.__sigev_notify_kqueue; 2670 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr); 2671 break; 2672 default: 2673 return (EINVAL); 2674 } 2675 return (0); 2676 } 2677 2678 static int 2679 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob) 2680 { 2681 struct oaiocb32 job32; 2682 int error; 2683 2684 bzero(kjob, sizeof(struct aiocb)); 2685 error = copyin(ujob, &job32, sizeof(job32)); 2686 if (error) 2687 return (error); 2688 2689 CP(job32, *kjob, aio_fildes); 2690 CP(job32, *kjob, aio_offset); 2691 PTRIN_CP(job32, *kjob, aio_buf); 2692 CP(job32, *kjob, aio_nbytes); 2693 CP(job32, *kjob, aio_lio_opcode); 2694 CP(job32, *kjob, aio_reqprio); 2695 CP(job32, *kjob, _aiocb_private.status); 2696 CP(job32, *kjob, _aiocb_private.error); 2697 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo); 2698 return (convert_old_sigevent32(&job32.aio_sigevent, 2699 &kjob->aio_sigevent)); 2700 } 2701 2702 static int 2703 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig) 2704 { 2705 2706 CP(*sig32, *sig, sigev_notify); 2707 switch (sig->sigev_notify) { 2708 case SIGEV_NONE: 2709 break; 2710 case SIGEV_THREAD_ID: 2711 CP(*sig32, *sig, sigev_notify_thread_id); 2712 /* FALLTHROUGH */ 2713 case SIGEV_SIGNAL: 2714 CP(*sig32, *sig, sigev_signo); 2715 break; 2716 case SIGEV_KEVENT: 2717 CP(*sig32, *sig, sigev_notify_kqueue); 2718 CP(*sig32, *sig, sigev_notify_kevent_flags); 2719 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr); 2720 break; 2721 default: 2722 return (EINVAL); 2723 } 2724 return (0); 2725 } 2726 2727 static int 2728 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob) 2729 { 2730 struct aiocb32 job32; 2731 int error; 2732 2733 error = copyin(ujob, &job32, sizeof(job32)); 2734 if (error) 2735 return (error); 2736 CP(job32, *kjob, aio_fildes); 2737 CP(job32, *kjob, aio_offset); 2738 PTRIN_CP(job32, *kjob, aio_buf); 2739 CP(job32, *kjob, aio_nbytes); 2740 CP(job32, *kjob, aio_lio_opcode); 2741 CP(job32, *kjob, aio_reqprio); 2742 CP(job32, *kjob, _aiocb_private.status); 2743 CP(job32, *kjob, _aiocb_private.error); 2744 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo); 2745 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent)); 2746 } 2747 2748 static long 2749 aiocb32_fetch_status(struct aiocb *ujob) 2750 { 2751 struct aiocb32 *ujob32; 2752 2753 ujob32 = (struct aiocb32 *)ujob; 2754 return (fuword32(&ujob32->_aiocb_private.status)); 2755 } 2756 2757 static long 2758 aiocb32_fetch_error(struct aiocb *ujob) 2759 { 2760 struct aiocb32 *ujob32; 2761 2762 ujob32 = (struct aiocb32 *)ujob; 2763 return (fuword32(&ujob32->_aiocb_private.error)); 2764 } 2765 2766 static int 2767 aiocb32_store_status(struct aiocb *ujob, long status) 2768 { 2769 struct aiocb32 *ujob32; 2770 2771 ujob32 = (struct aiocb32 *)ujob; 2772 return (suword32(&ujob32->_aiocb_private.status, status)); 2773 } 2774 2775 static int 2776 aiocb32_store_error(struct aiocb *ujob, long error) 2777 { 2778 struct aiocb32 *ujob32; 2779 2780 ujob32 = (struct aiocb32 *)ujob; 2781 return (suword32(&ujob32->_aiocb_private.error, error)); 2782 } 2783 2784 static int 2785 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref) 2786 { 2787 struct aiocb32 *ujob32; 2788 2789 ujob32 = (struct aiocb32 *)ujob; 2790 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref)); 2791 } 2792 2793 static int 2794 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob) 2795 { 2796 2797 return (suword32(ujobp, (long)ujob)); 2798 } 2799 2800 static struct aiocb_ops aiocb32_ops = { 2801 .copyin = aiocb32_copyin, 2802 .fetch_status = aiocb32_fetch_status, 2803 .fetch_error = aiocb32_fetch_error, 2804 .store_status = aiocb32_store_status, 2805 .store_error = aiocb32_store_error, 2806 .store_kernelinfo = aiocb32_store_kernelinfo, 2807 .store_aiocb = aiocb32_store_aiocb, 2808 }; 2809 2810 static struct aiocb_ops aiocb32_ops_osigevent = { 2811 .copyin = aiocb32_copyin_old_sigevent, 2812 .fetch_status = aiocb32_fetch_status, 2813 .fetch_error = aiocb32_fetch_error, 2814 .store_status = aiocb32_store_status, 2815 .store_error = aiocb32_store_error, 2816 .store_kernelinfo = aiocb32_store_kernelinfo, 2817 .store_aiocb = aiocb32_store_aiocb, 2818 }; 2819 2820 int 2821 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap) 2822 { 2823 2824 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 2825 } 2826 2827 int 2828 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap) 2829 { 2830 struct timespec32 ts32; 2831 struct timespec ts, *tsp; 2832 struct aiocb **ujoblist; 2833 uint32_t *ujoblist32; 2834 int error, i; 2835 2836 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX) 2837 return (EINVAL); 2838 2839 if (uap->timeout) { 2840 /* Get timespec struct. */ 2841 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0) 2842 return (error); 2843 CP(ts32, ts, tv_sec); 2844 CP(ts32, ts, tv_nsec); 2845 tsp = &ts; 2846 } else 2847 tsp = NULL; 2848 2849 ujoblist = uma_zalloc(aiol_zone, M_WAITOK); 2850 ujoblist32 = (uint32_t *)ujoblist; 2851 error = copyin(uap->aiocbp, ujoblist32, uap->nent * 2852 sizeof(ujoblist32[0])); 2853 if (error == 0) { 2854 for (i = uap->nent; i > 0; i--) 2855 ujoblist[i] = PTRIN(ujoblist32[i]); 2856 2857 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 2858 } 2859 uma_zfree(aiol_zone, ujoblist); 2860 return (error); 2861 } 2862 2863 int 2864 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap) 2865 { 2866 2867 return (sys_aio_cancel(td, (struct aio_cancel_args *)uap)); 2868 } 2869 2870 int 2871 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap) 2872 { 2873 2874 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 2875 } 2876 2877 int 2878 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap) 2879 { 2880 2881 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2882 &aiocb32_ops_osigevent)); 2883 } 2884 2885 int 2886 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap) 2887 { 2888 2889 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2890 &aiocb32_ops)); 2891 } 2892 2893 int 2894 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap) 2895 { 2896 2897 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2898 &aiocb32_ops_osigevent)); 2899 } 2900 2901 int 2902 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap) 2903 { 2904 2905 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2906 &aiocb32_ops)); 2907 } 2908 2909 int 2910 freebsd32_aio_waitcomplete(struct thread *td, 2911 struct freebsd32_aio_waitcomplete_args *uap) 2912 { 2913 struct timespec32 ts32; 2914 struct timespec ts, *tsp; 2915 int error; 2916 2917 if (uap->timeout) { 2918 /* Get timespec struct. */ 2919 error = copyin(uap->timeout, &ts32, sizeof(ts32)); 2920 if (error) 2921 return (error); 2922 CP(ts32, ts, tv_sec); 2923 CP(ts32, ts, tv_nsec); 2924 tsp = &ts; 2925 } else 2926 tsp = NULL; 2927 2928 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp, 2929 &aiocb32_ops)); 2930 } 2931 2932 int 2933 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap) 2934 { 2935 2936 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp, 2937 &aiocb32_ops)); 2938 } 2939 2940 int 2941 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap) 2942 { 2943 struct aiocb **acb_list; 2944 struct sigevent *sigp, sig; 2945 struct osigevent32 osig; 2946 uint32_t *acb_list32; 2947 int error, i, nent; 2948 2949 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2950 return (EINVAL); 2951 2952 nent = uap->nent; 2953 if (nent < 0 || nent > AIO_LISTIO_MAX) 2954 return (EINVAL); 2955 2956 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2957 error = copyin(uap->sig, &osig, sizeof(osig)); 2958 if (error) 2959 return (error); 2960 error = convert_old_sigevent32(&osig, &sig); 2961 if (error) 2962 return (error); 2963 sigp = &sig; 2964 } else 2965 sigp = NULL; 2966 2967 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 2968 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 2969 if (error) { 2970 free(acb_list32, M_LIO); 2971 return (error); 2972 } 2973 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2974 for (i = 0; i < nent; i++) 2975 acb_list[i] = PTRIN(acb_list32[i]); 2976 free(acb_list32, M_LIO); 2977 2978 error = kern_lio_listio(td, uap->mode, 2979 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 2980 &aiocb32_ops_osigevent); 2981 free(acb_list, M_LIO); 2982 return (error); 2983 } 2984 2985 int 2986 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap) 2987 { 2988 struct aiocb **acb_list; 2989 struct sigevent *sigp, sig; 2990 struct sigevent32 sig32; 2991 uint32_t *acb_list32; 2992 int error, i, nent; 2993 2994 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2995 return (EINVAL); 2996 2997 nent = uap->nent; 2998 if (nent < 0 || nent > AIO_LISTIO_MAX) 2999 return (EINVAL); 3000 3001 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 3002 error = copyin(uap->sig, &sig32, sizeof(sig32)); 3003 if (error) 3004 return (error); 3005 error = convert_sigevent32(&sig32, &sig); 3006 if (error) 3007 return (error); 3008 sigp = &sig; 3009 } else 3010 sigp = NULL; 3011 3012 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 3013 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 3014 if (error) { 3015 free(acb_list32, M_LIO); 3016 return (error); 3017 } 3018 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 3019 for (i = 0; i < nent; i++) 3020 acb_list[i] = PTRIN(acb_list32[i]); 3021 free(acb_list32, M_LIO); 3022 3023 error = kern_lio_listio(td, uap->mode, 3024 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 3025 &aiocb32_ops); 3026 free(acb_list, M_LIO); 3027 return (error); 3028 } 3029 3030 #endif 3031