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