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