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, (dev->si_flags & SI_UNMAPPED) == 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 knlist_init_mtx(&aiocbe->klist, AIO_MTX(ki)); 1599 1600 error = ops->copyin(job, &aiocbe->uaiocb); 1601 if (error) { 1602 ops->store_error(job, error); 1603 uma_zfree(aiocb_zone, aiocbe); 1604 return (error); 1605 } 1606 1607 /* XXX: aio_nbytes is later casted to signed types. */ 1608 if (aiocbe->uaiocb.aio_nbytes > INT_MAX) { 1609 uma_zfree(aiocb_zone, aiocbe); 1610 return (EINVAL); 1611 } 1612 1613 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT && 1614 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL && 1615 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID && 1616 aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) { 1617 ops->store_error(job, EINVAL); 1618 uma_zfree(aiocb_zone, aiocbe); 1619 return (EINVAL); 1620 } 1621 1622 if ((aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL || 1623 aiocbe->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) && 1624 !_SIG_VALID(aiocbe->uaiocb.aio_sigevent.sigev_signo)) { 1625 uma_zfree(aiocb_zone, aiocbe); 1626 return (EINVAL); 1627 } 1628 1629 ksiginfo_init(&aiocbe->ksi); 1630 1631 /* Save userspace address of the job info. */ 1632 aiocbe->uuaiocb = job; 1633 1634 /* Get the opcode. */ 1635 if (type != LIO_NOP) 1636 aiocbe->uaiocb.aio_lio_opcode = type; 1637 opcode = aiocbe->uaiocb.aio_lio_opcode; 1638 1639 /* 1640 * Validate the opcode and fetch the file object for the specified 1641 * file descriptor. 1642 * 1643 * XXXRW: Moved the opcode validation up here so that we don't 1644 * retrieve a file descriptor without knowing what the capabiltity 1645 * should be. 1646 */ 1647 fd = aiocbe->uaiocb.aio_fildes; 1648 switch (opcode) { 1649 case LIO_WRITE: 1650 error = fget_write(td, fd, CAP_PWRITE, &fp); 1651 break; 1652 case LIO_READ: 1653 error = fget_read(td, fd, CAP_PREAD, &fp); 1654 break; 1655 case LIO_SYNC: 1656 error = fget(td, fd, CAP_FSYNC, &fp); 1657 break; 1658 case LIO_MLOCK: 1659 fp = NULL; 1660 break; 1661 case LIO_NOP: 1662 error = fget(td, fd, CAP_NONE, &fp); 1663 break; 1664 default: 1665 error = EINVAL; 1666 } 1667 if (error) { 1668 uma_zfree(aiocb_zone, aiocbe); 1669 ops->store_error(job, error); 1670 return (error); 1671 } 1672 1673 if (opcode == LIO_SYNC && fp->f_vnode == NULL) { 1674 error = EINVAL; 1675 goto aqueue_fail; 1676 } 1677 1678 if (opcode != LIO_SYNC && aiocbe->uaiocb.aio_offset == -1LL) { 1679 error = EINVAL; 1680 goto aqueue_fail; 1681 } 1682 1683 aiocbe->fd_file = fp; 1684 1685 mtx_lock(&aio_job_mtx); 1686 jid = jobrefid++; 1687 aiocbe->seqno = jobseqno++; 1688 mtx_unlock(&aio_job_mtx); 1689 error = ops->store_kernelinfo(job, jid); 1690 if (error) { 1691 error = EINVAL; 1692 goto aqueue_fail; 1693 } 1694 aiocbe->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid; 1695 1696 if (opcode == LIO_NOP) { 1697 fdrop(fp, td); 1698 uma_zfree(aiocb_zone, aiocbe); 1699 return (0); 1700 } 1701 1702 if (aiocbe->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT) 1703 goto no_kqueue; 1704 evflags = aiocbe->uaiocb.aio_sigevent.sigev_notify_kevent_flags; 1705 if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) { 1706 error = EINVAL; 1707 goto aqueue_fail; 1708 } 1709 kqfd = aiocbe->uaiocb.aio_sigevent.sigev_notify_kqueue; 1710 kev.ident = (uintptr_t)aiocbe->uuaiocb; 1711 kev.filter = EVFILT_AIO; 1712 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags; 1713 kev.data = (intptr_t)aiocbe; 1714 kev.udata = aiocbe->uaiocb.aio_sigevent.sigev_value.sival_ptr; 1715 error = kqfd_register(kqfd, &kev, td, 1); 1716 aqueue_fail: 1717 if (error) { 1718 if (fp) 1719 fdrop(fp, td); 1720 uma_zfree(aiocb_zone, aiocbe); 1721 ops->store_error(job, error); 1722 goto done; 1723 } 1724 no_kqueue: 1725 1726 ops->store_error(job, EINPROGRESS); 1727 aiocbe->uaiocb._aiocb_private.error = EINPROGRESS; 1728 aiocbe->userproc = p; 1729 aiocbe->cred = crhold(td->td_ucred); 1730 aiocbe->jobflags = 0; 1731 aiocbe->lio = lj; 1732 1733 if (opcode == LIO_SYNC) 1734 goto queueit; 1735 1736 if (fp && fp->f_type == DTYPE_SOCKET) { 1737 /* 1738 * Alternate queueing for socket ops: Reach down into the 1739 * descriptor to get the socket data. Then check to see if the 1740 * socket is ready to be read or written (based on the requested 1741 * operation). 1742 * 1743 * If it is not ready for io, then queue the aiocbe on the 1744 * socket, and set the flags so we get a call when sbnotify() 1745 * happens. 1746 * 1747 * Note if opcode is neither LIO_WRITE nor LIO_READ we lock 1748 * and unlock the snd sockbuf for no reason. 1749 */ 1750 so = fp->f_data; 1751 sb = (opcode == LIO_READ) ? &so->so_rcv : &so->so_snd; 1752 SOCKBUF_LOCK(sb); 1753 if (((opcode == LIO_READ) && (!soreadable(so))) || ((opcode == 1754 LIO_WRITE) && (!sowriteable(so)))) { 1755 sb->sb_flags |= SB_AIO; 1756 1757 mtx_lock(&aio_job_mtx); 1758 TAILQ_INSERT_TAIL(&so->so_aiojobq, aiocbe, list); 1759 mtx_unlock(&aio_job_mtx); 1760 1761 AIO_LOCK(ki); 1762 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist); 1763 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist); 1764 aiocbe->jobstate = JOBST_JOBQSOCK; 1765 ki->kaio_count++; 1766 if (lj) 1767 lj->lioj_count++; 1768 AIO_UNLOCK(ki); 1769 SOCKBUF_UNLOCK(sb); 1770 atomic_add_int(&num_queue_count, 1); 1771 error = 0; 1772 goto done; 1773 } 1774 SOCKBUF_UNLOCK(sb); 1775 } 1776 1777 if ((error = aio_qphysio(p, aiocbe)) == 0) 1778 goto done; 1779 #if 0 1780 if (error > 0) { 1781 aiocbe->uaiocb._aiocb_private.error = error; 1782 ops->store_error(job, error); 1783 goto done; 1784 } 1785 #endif 1786 queueit: 1787 /* No buffer for daemon I/O. */ 1788 aiocbe->bp = NULL; 1789 atomic_add_int(&num_queue_count, 1); 1790 1791 AIO_LOCK(ki); 1792 ki->kaio_count++; 1793 if (lj) 1794 lj->lioj_count++; 1795 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, aiocbe, plist); 1796 TAILQ_INSERT_TAIL(&ki->kaio_all, aiocbe, allist); 1797 if (opcode == LIO_SYNC) { 1798 TAILQ_FOREACH(cb, &ki->kaio_jobqueue, plist) { 1799 if (cb->fd_file == aiocbe->fd_file && 1800 cb->uaiocb.aio_lio_opcode != LIO_SYNC && 1801 cb->seqno < aiocbe->seqno) { 1802 cb->jobflags |= AIOCBLIST_CHECKSYNC; 1803 aiocbe->pending++; 1804 } 1805 } 1806 TAILQ_FOREACH(cb, &ki->kaio_bufqueue, plist) { 1807 if (cb->fd_file == aiocbe->fd_file && 1808 cb->uaiocb.aio_lio_opcode != LIO_SYNC && 1809 cb->seqno < aiocbe->seqno) { 1810 cb->jobflags |= AIOCBLIST_CHECKSYNC; 1811 aiocbe->pending++; 1812 } 1813 } 1814 if (aiocbe->pending != 0) { 1815 TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, aiocbe, list); 1816 aiocbe->jobstate = JOBST_JOBQSYNC; 1817 AIO_UNLOCK(ki); 1818 goto done; 1819 } 1820 } 1821 mtx_lock(&aio_job_mtx); 1822 TAILQ_INSERT_TAIL(&aio_jobs, aiocbe, list); 1823 aiocbe->jobstate = JOBST_JOBQGLOBAL; 1824 aio_kick_nowait(p); 1825 mtx_unlock(&aio_job_mtx); 1826 AIO_UNLOCK(ki); 1827 error = 0; 1828 done: 1829 return (error); 1830 } 1831 1832 static void 1833 aio_kick_nowait(struct proc *userp) 1834 { 1835 struct kaioinfo *ki = userp->p_aioinfo; 1836 struct aiothreadlist *aiop; 1837 1838 mtx_assert(&aio_job_mtx, MA_OWNED); 1839 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1840 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1841 aiop->aiothreadflags &= ~AIOP_FREE; 1842 wakeup(aiop->aiothread); 1843 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) && 1844 ((ki->kaio_active_count + num_aio_resv_start) < 1845 ki->kaio_maxactive_count)) { 1846 taskqueue_enqueue(taskqueue_aiod_bio, &ki->kaio_task); 1847 } 1848 } 1849 1850 static int 1851 aio_kick(struct proc *userp) 1852 { 1853 struct kaioinfo *ki = userp->p_aioinfo; 1854 struct aiothreadlist *aiop; 1855 int error, ret = 0; 1856 1857 mtx_assert(&aio_job_mtx, MA_OWNED); 1858 retryproc: 1859 if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) { 1860 TAILQ_REMOVE(&aio_freeproc, aiop, list); 1861 aiop->aiothreadflags &= ~AIOP_FREE; 1862 wakeup(aiop->aiothread); 1863 } else if (((num_aio_resv_start + num_aio_procs) < max_aio_procs) && 1864 ((ki->kaio_active_count + num_aio_resv_start) < 1865 ki->kaio_maxactive_count)) { 1866 num_aio_resv_start++; 1867 mtx_unlock(&aio_job_mtx); 1868 error = aio_newproc(&num_aio_resv_start); 1869 mtx_lock(&aio_job_mtx); 1870 if (error) { 1871 num_aio_resv_start--; 1872 goto retryproc; 1873 } 1874 } else { 1875 ret = -1; 1876 } 1877 return (ret); 1878 } 1879 1880 static void 1881 aio_kick_helper(void *context, int pending) 1882 { 1883 struct proc *userp = context; 1884 1885 mtx_lock(&aio_job_mtx); 1886 while (--pending >= 0) { 1887 if (aio_kick(userp)) 1888 break; 1889 } 1890 mtx_unlock(&aio_job_mtx); 1891 } 1892 1893 /* 1894 * Support the aio_return system call, as a side-effect, kernel resources are 1895 * released. 1896 */ 1897 static int 1898 kern_aio_return(struct thread *td, struct aiocb *uaiocb, struct aiocb_ops *ops) 1899 { 1900 struct proc *p = td->td_proc; 1901 struct aiocblist *cb; 1902 struct kaioinfo *ki; 1903 int status, error; 1904 1905 ki = p->p_aioinfo; 1906 if (ki == NULL) 1907 return (EINVAL); 1908 AIO_LOCK(ki); 1909 TAILQ_FOREACH(cb, &ki->kaio_done, plist) { 1910 if (cb->uuaiocb == uaiocb) 1911 break; 1912 } 1913 if (cb != NULL) { 1914 MPASS(cb->jobstate == JOBST_JOBFINISHED); 1915 status = cb->uaiocb._aiocb_private.status; 1916 error = cb->uaiocb._aiocb_private.error; 1917 td->td_retval[0] = status; 1918 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 1919 td->td_ru.ru_oublock += cb->outputcharge; 1920 cb->outputcharge = 0; 1921 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 1922 td->td_ru.ru_inblock += cb->inputcharge; 1923 cb->inputcharge = 0; 1924 } 1925 aio_free_entry(cb); 1926 AIO_UNLOCK(ki); 1927 ops->store_error(uaiocb, error); 1928 ops->store_status(uaiocb, status); 1929 } else { 1930 error = EINVAL; 1931 AIO_UNLOCK(ki); 1932 } 1933 return (error); 1934 } 1935 1936 int 1937 sys_aio_return(struct thread *td, struct aio_return_args *uap) 1938 { 1939 1940 return (kern_aio_return(td, uap->aiocbp, &aiocb_ops)); 1941 } 1942 1943 /* 1944 * Allow a process to wakeup when any of the I/O requests are completed. 1945 */ 1946 static int 1947 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist, 1948 struct timespec *ts) 1949 { 1950 struct proc *p = td->td_proc; 1951 struct timeval atv; 1952 struct kaioinfo *ki; 1953 struct aiocblist *cb, *cbfirst; 1954 int error, i, timo; 1955 1956 timo = 0; 1957 if (ts) { 1958 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 1959 return (EINVAL); 1960 1961 TIMESPEC_TO_TIMEVAL(&atv, ts); 1962 if (itimerfix(&atv)) 1963 return (EINVAL); 1964 timo = tvtohz(&atv); 1965 } 1966 1967 ki = p->p_aioinfo; 1968 if (ki == NULL) 1969 return (EAGAIN); 1970 1971 if (njoblist == 0) 1972 return (0); 1973 1974 AIO_LOCK(ki); 1975 for (;;) { 1976 cbfirst = NULL; 1977 error = 0; 1978 TAILQ_FOREACH(cb, &ki->kaio_all, allist) { 1979 for (i = 0; i < njoblist; i++) { 1980 if (cb->uuaiocb == ujoblist[i]) { 1981 if (cbfirst == NULL) 1982 cbfirst = cb; 1983 if (cb->jobstate == JOBST_JOBFINISHED) 1984 goto RETURN; 1985 } 1986 } 1987 } 1988 /* All tasks were finished. */ 1989 if (cbfirst == NULL) 1990 break; 1991 1992 ki->kaio_flags |= KAIO_WAKEUP; 1993 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 1994 "aiospn", timo); 1995 if (error == ERESTART) 1996 error = EINTR; 1997 if (error) 1998 break; 1999 } 2000 RETURN: 2001 AIO_UNLOCK(ki); 2002 return (error); 2003 } 2004 2005 int 2006 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap) 2007 { 2008 struct timespec ts, *tsp; 2009 struct aiocb **ujoblist; 2010 int error; 2011 2012 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX) 2013 return (EINVAL); 2014 2015 if (uap->timeout) { 2016 /* Get timespec struct. */ 2017 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) 2018 return (error); 2019 tsp = &ts; 2020 } else 2021 tsp = NULL; 2022 2023 ujoblist = uma_zalloc(aiol_zone, M_WAITOK); 2024 error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0])); 2025 if (error == 0) 2026 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 2027 uma_zfree(aiol_zone, ujoblist); 2028 return (error); 2029 } 2030 2031 /* 2032 * aio_cancel cancels any non-physio aio operations not currently in 2033 * progress. 2034 */ 2035 int 2036 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap) 2037 { 2038 struct proc *p = td->td_proc; 2039 struct kaioinfo *ki; 2040 struct aiocblist *cbe, *cbn; 2041 struct file *fp; 2042 struct socket *so; 2043 int error; 2044 int remove; 2045 int cancelled = 0; 2046 int notcancelled = 0; 2047 struct vnode *vp; 2048 2049 /* Lookup file object. */ 2050 error = fget(td, uap->fd, 0, &fp); 2051 if (error) 2052 return (error); 2053 2054 ki = p->p_aioinfo; 2055 if (ki == NULL) 2056 goto done; 2057 2058 if (fp->f_type == DTYPE_VNODE) { 2059 vp = fp->f_vnode; 2060 if (vn_isdisk(vp, &error)) { 2061 fdrop(fp, td); 2062 td->td_retval[0] = AIO_NOTCANCELED; 2063 return (0); 2064 } 2065 } 2066 2067 AIO_LOCK(ki); 2068 TAILQ_FOREACH_SAFE(cbe, &ki->kaio_jobqueue, plist, cbn) { 2069 if ((uap->fd == cbe->uaiocb.aio_fildes) && 2070 ((uap->aiocbp == NULL) || 2071 (uap->aiocbp == cbe->uuaiocb))) { 2072 remove = 0; 2073 2074 mtx_lock(&aio_job_mtx); 2075 if (cbe->jobstate == JOBST_JOBQGLOBAL) { 2076 TAILQ_REMOVE(&aio_jobs, cbe, list); 2077 remove = 1; 2078 } else if (cbe->jobstate == JOBST_JOBQSOCK) { 2079 MPASS(fp->f_type == DTYPE_SOCKET); 2080 so = fp->f_data; 2081 TAILQ_REMOVE(&so->so_aiojobq, cbe, list); 2082 remove = 1; 2083 } else if (cbe->jobstate == JOBST_JOBQSYNC) { 2084 TAILQ_REMOVE(&ki->kaio_syncqueue, cbe, list); 2085 remove = 1; 2086 } 2087 mtx_unlock(&aio_job_mtx); 2088 2089 if (remove) { 2090 TAILQ_REMOVE(&ki->kaio_jobqueue, cbe, plist); 2091 cbe->uaiocb._aiocb_private.status = -1; 2092 cbe->uaiocb._aiocb_private.error = ECANCELED; 2093 aio_bio_done_notify(p, cbe, DONE_QUEUE); 2094 cancelled++; 2095 } else { 2096 notcancelled++; 2097 } 2098 if (uap->aiocbp != NULL) 2099 break; 2100 } 2101 } 2102 AIO_UNLOCK(ki); 2103 2104 done: 2105 fdrop(fp, td); 2106 2107 if (uap->aiocbp != NULL) { 2108 if (cancelled) { 2109 td->td_retval[0] = AIO_CANCELED; 2110 return (0); 2111 } 2112 } 2113 2114 if (notcancelled) { 2115 td->td_retval[0] = AIO_NOTCANCELED; 2116 return (0); 2117 } 2118 2119 if (cancelled) { 2120 td->td_retval[0] = AIO_CANCELED; 2121 return (0); 2122 } 2123 2124 td->td_retval[0] = AIO_ALLDONE; 2125 2126 return (0); 2127 } 2128 2129 /* 2130 * aio_error is implemented in the kernel level for compatibility purposes 2131 * only. For a user mode async implementation, it would be best to do it in 2132 * a userland subroutine. 2133 */ 2134 static int 2135 kern_aio_error(struct thread *td, struct aiocb *aiocbp, struct aiocb_ops *ops) 2136 { 2137 struct proc *p = td->td_proc; 2138 struct aiocblist *cb; 2139 struct kaioinfo *ki; 2140 int status; 2141 2142 ki = p->p_aioinfo; 2143 if (ki == NULL) { 2144 td->td_retval[0] = EINVAL; 2145 return (0); 2146 } 2147 2148 AIO_LOCK(ki); 2149 TAILQ_FOREACH(cb, &ki->kaio_all, allist) { 2150 if (cb->uuaiocb == aiocbp) { 2151 if (cb->jobstate == JOBST_JOBFINISHED) 2152 td->td_retval[0] = 2153 cb->uaiocb._aiocb_private.error; 2154 else 2155 td->td_retval[0] = EINPROGRESS; 2156 AIO_UNLOCK(ki); 2157 return (0); 2158 } 2159 } 2160 AIO_UNLOCK(ki); 2161 2162 /* 2163 * Hack for failure of aio_aqueue. 2164 */ 2165 status = ops->fetch_status(aiocbp); 2166 if (status == -1) { 2167 td->td_retval[0] = ops->fetch_error(aiocbp); 2168 return (0); 2169 } 2170 2171 td->td_retval[0] = EINVAL; 2172 return (0); 2173 } 2174 2175 int 2176 sys_aio_error(struct thread *td, struct aio_error_args *uap) 2177 { 2178 2179 return (kern_aio_error(td, uap->aiocbp, &aiocb_ops)); 2180 } 2181 2182 /* syscall - asynchronous read from a file (REALTIME) */ 2183 int 2184 sys_oaio_read(struct thread *td, struct oaio_read_args *uap) 2185 { 2186 2187 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2188 &aiocb_ops_osigevent)); 2189 } 2190 2191 int 2192 sys_aio_read(struct thread *td, struct aio_read_args *uap) 2193 { 2194 2195 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops)); 2196 } 2197 2198 /* syscall - asynchronous write to a file (REALTIME) */ 2199 int 2200 sys_oaio_write(struct thread *td, struct oaio_write_args *uap) 2201 { 2202 2203 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2204 &aiocb_ops_osigevent)); 2205 } 2206 2207 int 2208 sys_aio_write(struct thread *td, struct aio_write_args *uap) 2209 { 2210 2211 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops)); 2212 } 2213 2214 int 2215 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap) 2216 { 2217 2218 return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops)); 2219 } 2220 2221 static int 2222 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list, 2223 struct aiocb **acb_list, int nent, struct sigevent *sig, 2224 struct aiocb_ops *ops) 2225 { 2226 struct proc *p = td->td_proc; 2227 struct aiocb *iocb; 2228 struct kaioinfo *ki; 2229 struct aioliojob *lj; 2230 struct kevent kev; 2231 int error; 2232 int nerror; 2233 int i; 2234 2235 if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT)) 2236 return (EINVAL); 2237 2238 if (nent < 0 || nent > AIO_LISTIO_MAX) 2239 return (EINVAL); 2240 2241 if (p->p_aioinfo == NULL) 2242 aio_init_aioinfo(p); 2243 2244 ki = p->p_aioinfo; 2245 2246 lj = uma_zalloc(aiolio_zone, M_WAITOK); 2247 lj->lioj_flags = 0; 2248 lj->lioj_count = 0; 2249 lj->lioj_finished_count = 0; 2250 knlist_init_mtx(&lj->klist, AIO_MTX(ki)); 2251 ksiginfo_init(&lj->lioj_ksi); 2252 2253 /* 2254 * Setup signal. 2255 */ 2256 if (sig && (mode == LIO_NOWAIT)) { 2257 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal)); 2258 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 2259 /* Assume only new style KEVENT */ 2260 kev.filter = EVFILT_LIO; 2261 kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1; 2262 kev.ident = (uintptr_t)uacb_list; /* something unique */ 2263 kev.data = (intptr_t)lj; 2264 /* pass user defined sigval data */ 2265 kev.udata = lj->lioj_signal.sigev_value.sival_ptr; 2266 error = kqfd_register( 2267 lj->lioj_signal.sigev_notify_kqueue, &kev, td, 1); 2268 if (error) { 2269 uma_zfree(aiolio_zone, lj); 2270 return (error); 2271 } 2272 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) { 2273 ; 2274 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 2275 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) { 2276 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) { 2277 uma_zfree(aiolio_zone, lj); 2278 return EINVAL; 2279 } 2280 lj->lioj_flags |= LIOJ_SIGNAL; 2281 } else { 2282 uma_zfree(aiolio_zone, lj); 2283 return EINVAL; 2284 } 2285 } 2286 2287 AIO_LOCK(ki); 2288 TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list); 2289 /* 2290 * Add extra aiocb count to avoid the lio to be freed 2291 * by other threads doing aio_waitcomplete or aio_return, 2292 * and prevent event from being sent until we have queued 2293 * all tasks. 2294 */ 2295 lj->lioj_count = 1; 2296 AIO_UNLOCK(ki); 2297 2298 /* 2299 * Get pointers to the list of I/O requests. 2300 */ 2301 nerror = 0; 2302 for (i = 0; i < nent; i++) { 2303 iocb = acb_list[i]; 2304 if (iocb != NULL) { 2305 error = aio_aqueue(td, iocb, lj, LIO_NOP, ops); 2306 if (error != 0) 2307 nerror++; 2308 } 2309 } 2310 2311 error = 0; 2312 AIO_LOCK(ki); 2313 if (mode == LIO_WAIT) { 2314 while (lj->lioj_count - 1 != lj->lioj_finished_count) { 2315 ki->kaio_flags |= KAIO_WAKEUP; 2316 error = msleep(&p->p_aioinfo, AIO_MTX(ki), 2317 PRIBIO | PCATCH, "aiospn", 0); 2318 if (error == ERESTART) 2319 error = EINTR; 2320 if (error) 2321 break; 2322 } 2323 } else { 2324 if (lj->lioj_count - 1 == lj->lioj_finished_count) { 2325 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) { 2326 lj->lioj_flags |= LIOJ_KEVENT_POSTED; 2327 KNOTE_LOCKED(&lj->klist, 1); 2328 } 2329 if ((lj->lioj_flags & (LIOJ_SIGNAL|LIOJ_SIGNAL_POSTED)) 2330 == LIOJ_SIGNAL 2331 && (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL || 2332 lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) { 2333 aio_sendsig(p, &lj->lioj_signal, 2334 &lj->lioj_ksi); 2335 lj->lioj_flags |= LIOJ_SIGNAL_POSTED; 2336 } 2337 } 2338 } 2339 lj->lioj_count--; 2340 if (lj->lioj_count == 0) { 2341 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list); 2342 knlist_delete(&lj->klist, curthread, 1); 2343 PROC_LOCK(p); 2344 sigqueue_take(&lj->lioj_ksi); 2345 PROC_UNLOCK(p); 2346 AIO_UNLOCK(ki); 2347 uma_zfree(aiolio_zone, lj); 2348 } else 2349 AIO_UNLOCK(ki); 2350 2351 if (nerror) 2352 return (EIO); 2353 return (error); 2354 } 2355 2356 /* syscall - list directed I/O (REALTIME) */ 2357 int 2358 sys_olio_listio(struct thread *td, struct olio_listio_args *uap) 2359 { 2360 struct aiocb **acb_list; 2361 struct sigevent *sigp, sig; 2362 struct osigevent osig; 2363 int error, nent; 2364 2365 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2366 return (EINVAL); 2367 2368 nent = uap->nent; 2369 if (nent < 0 || nent > AIO_LISTIO_MAX) 2370 return (EINVAL); 2371 2372 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2373 error = copyin(uap->sig, &osig, sizeof(osig)); 2374 if (error) 2375 return (error); 2376 error = convert_old_sigevent(&osig, &sig); 2377 if (error) 2378 return (error); 2379 sigp = &sig; 2380 } else 2381 sigp = NULL; 2382 2383 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2384 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 2385 if (error == 0) 2386 error = kern_lio_listio(td, uap->mode, 2387 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 2388 &aiocb_ops_osigevent); 2389 free(acb_list, M_LIO); 2390 return (error); 2391 } 2392 2393 /* syscall - list directed I/O (REALTIME) */ 2394 int 2395 sys_lio_listio(struct thread *td, struct lio_listio_args *uap) 2396 { 2397 struct aiocb **acb_list; 2398 struct sigevent *sigp, sig; 2399 int error, nent; 2400 2401 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2402 return (EINVAL); 2403 2404 nent = uap->nent; 2405 if (nent < 0 || nent > AIO_LISTIO_MAX) 2406 return (EINVAL); 2407 2408 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2409 error = copyin(uap->sig, &sig, sizeof(sig)); 2410 if (error) 2411 return (error); 2412 sigp = &sig; 2413 } else 2414 sigp = NULL; 2415 2416 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 2417 error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0])); 2418 if (error == 0) 2419 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list, 2420 nent, sigp, &aiocb_ops); 2421 free(acb_list, M_LIO); 2422 return (error); 2423 } 2424 2425 /* 2426 * Called from interrupt thread for physio, we should return as fast 2427 * as possible, so we schedule a biohelper task. 2428 */ 2429 static void 2430 aio_physwakeup(struct buf *bp) 2431 { 2432 struct aiocblist *aiocbe; 2433 2434 aiocbe = (struct aiocblist *)bp->b_caller1; 2435 taskqueue_enqueue(taskqueue_aiod_bio, &aiocbe->biotask); 2436 } 2437 2438 /* 2439 * Task routine to perform heavy tasks, process wakeup, and signals. 2440 */ 2441 static void 2442 biohelper(void *context, int pending) 2443 { 2444 struct aiocblist *aiocbe = context; 2445 struct buf *bp; 2446 struct proc *userp; 2447 struct kaioinfo *ki; 2448 int nblks; 2449 2450 bp = aiocbe->bp; 2451 userp = aiocbe->userproc; 2452 ki = userp->p_aioinfo; 2453 AIO_LOCK(ki); 2454 aiocbe->uaiocb._aiocb_private.status -= bp->b_resid; 2455 aiocbe->uaiocb._aiocb_private.error = 0; 2456 if (bp->b_ioflags & BIO_ERROR) 2457 aiocbe->uaiocb._aiocb_private.error = bp->b_error; 2458 nblks = btodb(aiocbe->uaiocb.aio_nbytes); 2459 if (aiocbe->uaiocb.aio_lio_opcode == LIO_WRITE) 2460 aiocbe->outputcharge += nblks; 2461 else 2462 aiocbe->inputcharge += nblks; 2463 aiocbe->bp = NULL; 2464 TAILQ_REMOVE(&userp->p_aioinfo->kaio_bufqueue, aiocbe, plist); 2465 ki->kaio_buffer_count--; 2466 aio_bio_done_notify(userp, aiocbe, DONE_BUF); 2467 AIO_UNLOCK(ki); 2468 2469 /* Release mapping into kernel space. */ 2470 vunmapbuf(bp); 2471 relpbuf(bp, NULL); 2472 atomic_subtract_int(&num_buf_aio, 1); 2473 } 2474 2475 /* syscall - wait for the next completion of an aio request */ 2476 static int 2477 kern_aio_waitcomplete(struct thread *td, struct aiocb **aiocbp, 2478 struct timespec *ts, struct aiocb_ops *ops) 2479 { 2480 struct proc *p = td->td_proc; 2481 struct timeval atv; 2482 struct kaioinfo *ki; 2483 struct aiocblist *cb; 2484 struct aiocb *uuaiocb; 2485 int error, status, timo; 2486 2487 ops->store_aiocb(aiocbp, NULL); 2488 2489 timo = 0; 2490 if (ts) { 2491 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000)) 2492 return (EINVAL); 2493 2494 TIMESPEC_TO_TIMEVAL(&atv, ts); 2495 if (itimerfix(&atv)) 2496 return (EINVAL); 2497 timo = tvtohz(&atv); 2498 } 2499 2500 if (p->p_aioinfo == NULL) 2501 aio_init_aioinfo(p); 2502 ki = p->p_aioinfo; 2503 2504 error = 0; 2505 cb = NULL; 2506 AIO_LOCK(ki); 2507 while ((cb = TAILQ_FIRST(&ki->kaio_done)) == NULL) { 2508 ki->kaio_flags |= KAIO_WAKEUP; 2509 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH, 2510 "aiowc", timo); 2511 if (timo && error == ERESTART) 2512 error = EINTR; 2513 if (error) 2514 break; 2515 } 2516 2517 if (cb != NULL) { 2518 MPASS(cb->jobstate == JOBST_JOBFINISHED); 2519 uuaiocb = cb->uuaiocb; 2520 status = cb->uaiocb._aiocb_private.status; 2521 error = cb->uaiocb._aiocb_private.error; 2522 td->td_retval[0] = status; 2523 if (cb->uaiocb.aio_lio_opcode == LIO_WRITE) { 2524 td->td_ru.ru_oublock += cb->outputcharge; 2525 cb->outputcharge = 0; 2526 } else if (cb->uaiocb.aio_lio_opcode == LIO_READ) { 2527 td->td_ru.ru_inblock += cb->inputcharge; 2528 cb->inputcharge = 0; 2529 } 2530 aio_free_entry(cb); 2531 AIO_UNLOCK(ki); 2532 ops->store_aiocb(aiocbp, uuaiocb); 2533 ops->store_error(uuaiocb, error); 2534 ops->store_status(uuaiocb, status); 2535 } else 2536 AIO_UNLOCK(ki); 2537 2538 return (error); 2539 } 2540 2541 int 2542 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap) 2543 { 2544 struct timespec ts, *tsp; 2545 int error; 2546 2547 if (uap->timeout) { 2548 /* Get timespec struct. */ 2549 error = copyin(uap->timeout, &ts, sizeof(ts)); 2550 if (error) 2551 return (error); 2552 tsp = &ts; 2553 } else 2554 tsp = NULL; 2555 2556 return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops)); 2557 } 2558 2559 static int 2560 kern_aio_fsync(struct thread *td, int op, struct aiocb *aiocbp, 2561 struct aiocb_ops *ops) 2562 { 2563 struct proc *p = td->td_proc; 2564 struct kaioinfo *ki; 2565 2566 if (op != O_SYNC) /* XXX lack of O_DSYNC */ 2567 return (EINVAL); 2568 ki = p->p_aioinfo; 2569 if (ki == NULL) 2570 aio_init_aioinfo(p); 2571 return (aio_aqueue(td, aiocbp, NULL, LIO_SYNC, ops)); 2572 } 2573 2574 int 2575 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap) 2576 { 2577 2578 return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops)); 2579 } 2580 2581 /* kqueue attach function */ 2582 static int 2583 filt_aioattach(struct knote *kn) 2584 { 2585 struct aiocblist *aiocbe = (struct aiocblist *)kn->kn_sdata; 2586 2587 /* 2588 * The aiocbe pointer must be validated before using it, so 2589 * registration is restricted to the kernel; the user cannot 2590 * set EV_FLAG1. 2591 */ 2592 if ((kn->kn_flags & EV_FLAG1) == 0) 2593 return (EPERM); 2594 kn->kn_ptr.p_aio = aiocbe; 2595 kn->kn_flags &= ~EV_FLAG1; 2596 2597 knlist_add(&aiocbe->klist, kn, 0); 2598 2599 return (0); 2600 } 2601 2602 /* kqueue detach function */ 2603 static void 2604 filt_aiodetach(struct knote *kn) 2605 { 2606 struct knlist *knl; 2607 2608 knl = &kn->kn_ptr.p_aio->klist; 2609 knl->kl_lock(knl->kl_lockarg); 2610 if (!knlist_empty(knl)) 2611 knlist_remove(knl, kn, 1); 2612 knl->kl_unlock(knl->kl_lockarg); 2613 } 2614 2615 /* kqueue filter function */ 2616 /*ARGSUSED*/ 2617 static int 2618 filt_aio(struct knote *kn, long hint) 2619 { 2620 struct aiocblist *aiocbe = kn->kn_ptr.p_aio; 2621 2622 kn->kn_data = aiocbe->uaiocb._aiocb_private.error; 2623 if (aiocbe->jobstate != JOBST_JOBFINISHED) 2624 return (0); 2625 kn->kn_flags |= EV_EOF; 2626 return (1); 2627 } 2628 2629 /* kqueue attach function */ 2630 static int 2631 filt_lioattach(struct knote *kn) 2632 { 2633 struct aioliojob * lj = (struct aioliojob *)kn->kn_sdata; 2634 2635 /* 2636 * The aioliojob pointer must be validated before using it, so 2637 * registration is restricted to the kernel; the user cannot 2638 * set EV_FLAG1. 2639 */ 2640 if ((kn->kn_flags & EV_FLAG1) == 0) 2641 return (EPERM); 2642 kn->kn_ptr.p_lio = lj; 2643 kn->kn_flags &= ~EV_FLAG1; 2644 2645 knlist_add(&lj->klist, kn, 0); 2646 2647 return (0); 2648 } 2649 2650 /* kqueue detach function */ 2651 static void 2652 filt_liodetach(struct knote *kn) 2653 { 2654 struct knlist *knl; 2655 2656 knl = &kn->kn_ptr.p_lio->klist; 2657 knl->kl_lock(knl->kl_lockarg); 2658 if (!knlist_empty(knl)) 2659 knlist_remove(knl, kn, 1); 2660 knl->kl_unlock(knl->kl_lockarg); 2661 } 2662 2663 /* kqueue filter function */ 2664 /*ARGSUSED*/ 2665 static int 2666 filt_lio(struct knote *kn, long hint) 2667 { 2668 struct aioliojob * lj = kn->kn_ptr.p_lio; 2669 2670 return (lj->lioj_flags & LIOJ_KEVENT_POSTED); 2671 } 2672 2673 #ifdef COMPAT_FREEBSD32 2674 2675 struct __aiocb_private32 { 2676 int32_t status; 2677 int32_t error; 2678 uint32_t kernelinfo; 2679 }; 2680 2681 typedef struct oaiocb32 { 2682 int aio_fildes; /* File descriptor */ 2683 uint64_t aio_offset __packed; /* File offset for I/O */ 2684 uint32_t aio_buf; /* I/O buffer in process space */ 2685 uint32_t aio_nbytes; /* Number of bytes for I/O */ 2686 struct osigevent32 aio_sigevent; /* Signal to deliver */ 2687 int aio_lio_opcode; /* LIO opcode */ 2688 int aio_reqprio; /* Request priority -- ignored */ 2689 struct __aiocb_private32 _aiocb_private; 2690 } oaiocb32_t; 2691 2692 typedef struct aiocb32 { 2693 int32_t aio_fildes; /* File descriptor */ 2694 uint64_t aio_offset __packed; /* File offset for I/O */ 2695 uint32_t aio_buf; /* I/O buffer in process space */ 2696 uint32_t aio_nbytes; /* Number of bytes for I/O */ 2697 int __spare__[2]; 2698 uint32_t __spare2__; 2699 int aio_lio_opcode; /* LIO opcode */ 2700 int aio_reqprio; /* Request priority -- ignored */ 2701 struct __aiocb_private32 _aiocb_private; 2702 struct sigevent32 aio_sigevent; /* Signal to deliver */ 2703 } aiocb32_t; 2704 2705 static int 2706 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig) 2707 { 2708 2709 /* 2710 * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are 2711 * supported by AIO with the old sigevent structure. 2712 */ 2713 CP(*osig, *nsig, sigev_notify); 2714 switch (nsig->sigev_notify) { 2715 case SIGEV_NONE: 2716 break; 2717 case SIGEV_SIGNAL: 2718 nsig->sigev_signo = osig->__sigev_u.__sigev_signo; 2719 break; 2720 case SIGEV_KEVENT: 2721 nsig->sigev_notify_kqueue = 2722 osig->__sigev_u.__sigev_notify_kqueue; 2723 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr); 2724 break; 2725 default: 2726 return (EINVAL); 2727 } 2728 return (0); 2729 } 2730 2731 static int 2732 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct aiocb *kjob) 2733 { 2734 struct oaiocb32 job32; 2735 int error; 2736 2737 bzero(kjob, sizeof(struct aiocb)); 2738 error = copyin(ujob, &job32, sizeof(job32)); 2739 if (error) 2740 return (error); 2741 2742 CP(job32, *kjob, aio_fildes); 2743 CP(job32, *kjob, aio_offset); 2744 PTRIN_CP(job32, *kjob, aio_buf); 2745 CP(job32, *kjob, aio_nbytes); 2746 CP(job32, *kjob, aio_lio_opcode); 2747 CP(job32, *kjob, aio_reqprio); 2748 CP(job32, *kjob, _aiocb_private.status); 2749 CP(job32, *kjob, _aiocb_private.error); 2750 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo); 2751 return (convert_old_sigevent32(&job32.aio_sigevent, 2752 &kjob->aio_sigevent)); 2753 } 2754 2755 static int 2756 aiocb32_copyin(struct aiocb *ujob, struct aiocb *kjob) 2757 { 2758 struct aiocb32 job32; 2759 int error; 2760 2761 error = copyin(ujob, &job32, sizeof(job32)); 2762 if (error) 2763 return (error); 2764 CP(job32, *kjob, aio_fildes); 2765 CP(job32, *kjob, aio_offset); 2766 PTRIN_CP(job32, *kjob, aio_buf); 2767 CP(job32, *kjob, aio_nbytes); 2768 CP(job32, *kjob, aio_lio_opcode); 2769 CP(job32, *kjob, aio_reqprio); 2770 CP(job32, *kjob, _aiocb_private.status); 2771 CP(job32, *kjob, _aiocb_private.error); 2772 PTRIN_CP(job32, *kjob, _aiocb_private.kernelinfo); 2773 return (convert_sigevent32(&job32.aio_sigevent, &kjob->aio_sigevent)); 2774 } 2775 2776 static long 2777 aiocb32_fetch_status(struct aiocb *ujob) 2778 { 2779 struct aiocb32 *ujob32; 2780 2781 ujob32 = (struct aiocb32 *)ujob; 2782 return (fuword32(&ujob32->_aiocb_private.status)); 2783 } 2784 2785 static long 2786 aiocb32_fetch_error(struct aiocb *ujob) 2787 { 2788 struct aiocb32 *ujob32; 2789 2790 ujob32 = (struct aiocb32 *)ujob; 2791 return (fuword32(&ujob32->_aiocb_private.error)); 2792 } 2793 2794 static int 2795 aiocb32_store_status(struct aiocb *ujob, long status) 2796 { 2797 struct aiocb32 *ujob32; 2798 2799 ujob32 = (struct aiocb32 *)ujob; 2800 return (suword32(&ujob32->_aiocb_private.status, status)); 2801 } 2802 2803 static int 2804 aiocb32_store_error(struct aiocb *ujob, long error) 2805 { 2806 struct aiocb32 *ujob32; 2807 2808 ujob32 = (struct aiocb32 *)ujob; 2809 return (suword32(&ujob32->_aiocb_private.error, error)); 2810 } 2811 2812 static int 2813 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref) 2814 { 2815 struct aiocb32 *ujob32; 2816 2817 ujob32 = (struct aiocb32 *)ujob; 2818 return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref)); 2819 } 2820 2821 static int 2822 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob) 2823 { 2824 2825 return (suword32(ujobp, (long)ujob)); 2826 } 2827 2828 static struct aiocb_ops aiocb32_ops = { 2829 .copyin = aiocb32_copyin, 2830 .fetch_status = aiocb32_fetch_status, 2831 .fetch_error = aiocb32_fetch_error, 2832 .store_status = aiocb32_store_status, 2833 .store_error = aiocb32_store_error, 2834 .store_kernelinfo = aiocb32_store_kernelinfo, 2835 .store_aiocb = aiocb32_store_aiocb, 2836 }; 2837 2838 static struct aiocb_ops aiocb32_ops_osigevent = { 2839 .copyin = aiocb32_copyin_old_sigevent, 2840 .fetch_status = aiocb32_fetch_status, 2841 .fetch_error = aiocb32_fetch_error, 2842 .store_status = aiocb32_store_status, 2843 .store_error = aiocb32_store_error, 2844 .store_kernelinfo = aiocb32_store_kernelinfo, 2845 .store_aiocb = aiocb32_store_aiocb, 2846 }; 2847 2848 int 2849 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap) 2850 { 2851 2852 return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 2853 } 2854 2855 int 2856 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap) 2857 { 2858 struct timespec32 ts32; 2859 struct timespec ts, *tsp; 2860 struct aiocb **ujoblist; 2861 uint32_t *ujoblist32; 2862 int error, i; 2863 2864 if (uap->nent < 0 || uap->nent > AIO_LISTIO_MAX) 2865 return (EINVAL); 2866 2867 if (uap->timeout) { 2868 /* Get timespec struct. */ 2869 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0) 2870 return (error); 2871 CP(ts32, ts, tv_sec); 2872 CP(ts32, ts, tv_nsec); 2873 tsp = &ts; 2874 } else 2875 tsp = NULL; 2876 2877 ujoblist = uma_zalloc(aiol_zone, M_WAITOK); 2878 ujoblist32 = (uint32_t *)ujoblist; 2879 error = copyin(uap->aiocbp, ujoblist32, uap->nent * 2880 sizeof(ujoblist32[0])); 2881 if (error == 0) { 2882 for (i = uap->nent; i > 0; i--) 2883 ujoblist[i] = PTRIN(ujoblist32[i]); 2884 2885 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp); 2886 } 2887 uma_zfree(aiol_zone, ujoblist); 2888 return (error); 2889 } 2890 2891 int 2892 freebsd32_aio_cancel(struct thread *td, struct freebsd32_aio_cancel_args *uap) 2893 { 2894 2895 return (sys_aio_cancel(td, (struct aio_cancel_args *)uap)); 2896 } 2897 2898 int 2899 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap) 2900 { 2901 2902 return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops)); 2903 } 2904 2905 int 2906 freebsd32_oaio_read(struct thread *td, struct freebsd32_oaio_read_args *uap) 2907 { 2908 2909 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2910 &aiocb32_ops_osigevent)); 2911 } 2912 2913 int 2914 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap) 2915 { 2916 2917 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ, 2918 &aiocb32_ops)); 2919 } 2920 2921 int 2922 freebsd32_oaio_write(struct thread *td, struct freebsd32_oaio_write_args *uap) 2923 { 2924 2925 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2926 &aiocb32_ops_osigevent)); 2927 } 2928 2929 int 2930 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap) 2931 { 2932 2933 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE, 2934 &aiocb32_ops)); 2935 } 2936 2937 int 2938 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap) 2939 { 2940 2941 return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK, 2942 &aiocb32_ops)); 2943 } 2944 2945 int 2946 freebsd32_aio_waitcomplete(struct thread *td, 2947 struct freebsd32_aio_waitcomplete_args *uap) 2948 { 2949 struct timespec32 ts32; 2950 struct timespec ts, *tsp; 2951 int error; 2952 2953 if (uap->timeout) { 2954 /* Get timespec struct. */ 2955 error = copyin(uap->timeout, &ts32, sizeof(ts32)); 2956 if (error) 2957 return (error); 2958 CP(ts32, ts, tv_sec); 2959 CP(ts32, ts, tv_nsec); 2960 tsp = &ts; 2961 } else 2962 tsp = NULL; 2963 2964 return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp, 2965 &aiocb32_ops)); 2966 } 2967 2968 int 2969 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap) 2970 { 2971 2972 return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp, 2973 &aiocb32_ops)); 2974 } 2975 2976 int 2977 freebsd32_olio_listio(struct thread *td, struct freebsd32_olio_listio_args *uap) 2978 { 2979 struct aiocb **acb_list; 2980 struct sigevent *sigp, sig; 2981 struct osigevent32 osig; 2982 uint32_t *acb_list32; 2983 int error, i, nent; 2984 2985 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 2986 return (EINVAL); 2987 2988 nent = uap->nent; 2989 if (nent < 0 || nent > AIO_LISTIO_MAX) 2990 return (EINVAL); 2991 2992 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 2993 error = copyin(uap->sig, &osig, sizeof(osig)); 2994 if (error) 2995 return (error); 2996 error = convert_old_sigevent32(&osig, &sig); 2997 if (error) 2998 return (error); 2999 sigp = &sig; 3000 } else 3001 sigp = NULL; 3002 3003 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 3004 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 3005 if (error) { 3006 free(acb_list32, M_LIO); 3007 return (error); 3008 } 3009 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 3010 for (i = 0; i < nent; i++) 3011 acb_list[i] = PTRIN(acb_list32[i]); 3012 free(acb_list32, M_LIO); 3013 3014 error = kern_lio_listio(td, uap->mode, 3015 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 3016 &aiocb32_ops_osigevent); 3017 free(acb_list, M_LIO); 3018 return (error); 3019 } 3020 3021 int 3022 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap) 3023 { 3024 struct aiocb **acb_list; 3025 struct sigevent *sigp, sig; 3026 struct sigevent32 sig32; 3027 uint32_t *acb_list32; 3028 int error, i, nent; 3029 3030 if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT)) 3031 return (EINVAL); 3032 3033 nent = uap->nent; 3034 if (nent < 0 || nent > AIO_LISTIO_MAX) 3035 return (EINVAL); 3036 3037 if (uap->sig && (uap->mode == LIO_NOWAIT)) { 3038 error = copyin(uap->sig, &sig32, sizeof(sig32)); 3039 if (error) 3040 return (error); 3041 error = convert_sigevent32(&sig32, &sig); 3042 if (error) 3043 return (error); 3044 sigp = &sig; 3045 } else 3046 sigp = NULL; 3047 3048 acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK); 3049 error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t)); 3050 if (error) { 3051 free(acb_list32, M_LIO); 3052 return (error); 3053 } 3054 acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK); 3055 for (i = 0; i < nent; i++) 3056 acb_list[i] = PTRIN(acb_list32[i]); 3057 free(acb_list32, M_LIO); 3058 3059 error = kern_lio_listio(td, uap->mode, 3060 (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp, 3061 &aiocb32_ops); 3062 free(acb_list, M_LIO); 3063 return (error); 3064 } 3065 3066 #endif 3067