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