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