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