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