2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * 1) ramesh is looking into how to replace taking a reference on
28 * the user's map (vm_map_reference()) since it is believed that
29 * would not hold the process for us.
30 * 2) david is looking into a way for us to set the priority of the
31 * worker threads to match that of the user's thread when the
32 * async IO was queued.
37 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
40 #include <sys/systm.h>
41 #include <sys/fcntl.h>
42 #include <sys/file_internal.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode_internal.h>
46 #include <sys/malloc.h>
47 #include <sys/mount_internal.h>
48 #include <sys/param.h>
49 #include <sys/proc_internal.h>
50 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
54 #include <sys/aio_kern.h>
55 #include <sys/sysproto.h>
57 #include <machine/limits.h>
59 #include <mach/mach_types.h>
60 #include <kern/kern_types.h>
61 #include <kern/zalloc.h>
62 #include <kern/task.h>
63 #include <kern/sched_prim.h>
65 #include <vm/vm_map.h>
67 #include <sys/kdebug.h>
68 #define AIO_work_queued 1
69 #define AIO_worker_wake 2
70 #define AIO_completion_sig 3
71 #define AIO_completion_cleanup_wait 4
72 #define AIO_completion_cleanup_wake 5
73 #define AIO_completion_suspend_wake 6
74 #define AIO_fsync_delay 7
76 #define AIO_cancel_async_workq 11
77 #define AIO_cancel_sync_workq 12
78 #define AIO_cancel_activeq 13
79 #define AIO_cancel_doneq 14
85 #define AIO_error_val 61
86 #define AIO_error_activeq 62
87 #define AIO_error_workq 63
89 #define AIO_return_val 71
90 #define AIO_return_activeq 72
91 #define AIO_return_workq 73
94 #define AIO_exit_sleep 91
96 #define AIO_close_sleep 101
97 #define AIO_suspend 110
98 #define AIO_suspend_sleep 111
99 #define AIO_worker_thread 120
103 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
107 * aio requests queue up on the aio_async_workq or lio_sync_workq (for
108 * lio_listio LIO_WAIT). Requests then move to the per process aio_activeq
109 * (proc.aio_activeq) when one of our worker threads start the IO.
110 * And finally, requests move to the per process aio_doneq (proc.aio_doneq)
111 * when the IO request completes. The request remains on aio_doneq until
112 * user process calls aio_return or the process exits, either way that is our
113 * trigger to release aio resources.
117 int aio_async_workq_count
; /* entries on aio_async_workq */
118 int lio_sync_workq_count
; /* entries on lio_sync_workq */
119 int aio_active_count
; /* entries on all active queues (proc.aio_activeq) */
120 int aio_done_count
; /* entries on all done queues (proc.aio_doneq) */
121 TAILQ_HEAD( , aio_workq_entry
) aio_async_workq
;
122 TAILQ_HEAD( , aio_workq_entry
) lio_sync_workq
;
124 typedef struct aio_anchor_cb aio_anchor_cb
;
128 * Notes on aio sleep / wake channels.
129 * We currently pick a couple fields within the proc structure that will allow
130 * us sleep channels that currently do not collide with any other kernel routines.
131 * At this time, for binary compatibility reasons, we cannot create new proc fields.
133 #define AIO_SUSPEND_SLEEP_CHAN p_estcpu
134 #define AIO_CLEANUP_SLEEP_CHAN p_pctcpu
138 * aysnc IO locking macros used to protect critical sections.
140 #define AIO_LOCK lck_mtx_lock(aio_lock)
141 #define AIO_UNLOCK lck_mtx_unlock(aio_lock)
147 static int aio_active_requests_for_process( struct proc
*procp
);
148 static boolean_t
aio_delay_fsync_request( aio_workq_entry
*entryp
);
149 static int aio_free_request( aio_workq_entry
*entryp
, vm_map_t the_map
);
150 static int aio_get_all_queues_count( void );
151 static int aio_get_process_count( struct proc
*procp
);
152 static aio_workq_entry
* aio_get_some_work( void );
153 static boolean_t
aio_last_group_io( aio_workq_entry
*entryp
);
154 static void aio_mark_requests( aio_workq_entry
*entryp
);
155 static int aio_queue_async_request( struct proc
*procp
,
158 static int aio_validate( aio_workq_entry
*entryp
);
159 static void aio_work_thread( void );
160 static int do_aio_cancel( struct proc
*p
,
163 boolean_t wait_for_completion
,
164 boolean_t disable_notification
);
165 static void do_aio_completion( aio_workq_entry
*entryp
);
166 static int do_aio_fsync( aio_workq_entry
*entryp
);
167 static int do_aio_read( aio_workq_entry
*entryp
);
168 static int do_aio_write( aio_workq_entry
*entryp
);
169 static void do_munge_aiocb( struct aiocb
*my_aiocbp
, struct user_aiocb
*the_user_aiocbp
);
170 static boolean_t
is_already_queued( struct proc
*procp
,
171 user_addr_t aiocbp
);
172 static int lio_create_async_entry( struct proc
*procp
,
176 aio_workq_entry
**entrypp
);
177 static int lio_create_sync_entry( struct proc
*procp
,
180 aio_workq_entry
**entrypp
);
184 * EXTERNAL PROTOTYPES
187 /* in ...bsd/kern/sys_generic.c */
188 extern int dofileread( struct proc
*p
, struct fileproc
*fp
, int fd
,
189 user_addr_t bufp
, user_size_t nbyte
,
190 off_t offset
, int flags
, user_ssize_t
*retval
);
191 extern int dofilewrite( struct proc
*p
, struct fileproc
*fp
, int fd
,
192 user_addr_t bufp
, user_size_t nbyte
, off_t offset
,
193 int flags
, user_ssize_t
*retval
);
196 * aio external global variables.
198 extern int aio_max_requests
; /* AIO_MAX - configurable */
199 extern int aio_max_requests_per_process
; /* AIO_PROCESS_MAX - configurable */
200 extern int aio_worker_threads
; /* AIO_THREAD_COUNT - configurable */
204 * aio static variables.
206 static aio_anchor_cb aio_anchor
;
207 static lck_mtx_t
* aio_lock
;
208 static lck_grp_t
* aio_lock_grp
;
209 static lck_attr_t
* aio_lock_attr
;
210 static lck_grp_attr_t
* aio_lock_grp_attr
;
211 static struct zone
*aio_workq_zonep
;
217 * aio_cancel - attempt to cancel one or more async IO requests currently
218 * outstanding against file descriptor uap->fd. If uap->aiocbp is not
219 * NULL then only one specific IO is cancelled (if possible). If uap->aiocbp
220 * is NULL then all outstanding async IO request for the given file
221 * descriptor are cancelled (if possible).
225 aio_cancel( struct proc
*p
, struct aio_cancel_args
*uap
, int *retval
)
227 struct user_aiocb my_aiocb
;
230 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel
)) | DBG_FUNC_START
,
231 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
233 /* quick check to see if there are any async IO requests queued up */
235 result
= aio_get_all_queues_count( );
243 if ( uap
->aiocbp
!= USER_ADDR_NULL
) {
244 if ( !IS_64BIT_PROCESS(p
) ) {
245 struct aiocb aiocb32
;
247 result
= copyin( uap
->aiocbp
, &aiocb32
, sizeof(aiocb32
) );
249 do_munge_aiocb( &aiocb32
, &my_aiocb
);
251 result
= copyin( uap
->aiocbp
, &my_aiocb
, sizeof(my_aiocb
) );
258 /* NOTE - POSIX standard says a mismatch between the file */
259 /* descriptor passed in and the file descriptor embedded in */
260 /* the aiocb causes unspecified results. We return EBADF in */
261 /* that situation. */
262 if ( uap
->fd
!= my_aiocb
.aio_fildes
) {
267 result
= do_aio_cancel( p
, uap
->fd
, uap
->aiocbp
, FALSE
, FALSE
);
269 if ( result
!= -1 ) {
278 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel
)) | DBG_FUNC_END
,
279 (int)p
, (int)uap
->aiocbp
, result
, 0, 0 );
287 * _aio_close - internal function used to clean up async IO requests for
288 * a file descriptor that is closing.
292 __private_extern__
void
293 _aio_close( struct proc
*p
, int fd
)
297 /* quick check to see if there are any async IO requests queued up */
299 count
= aio_get_all_queues_count( );
304 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_close
)) | DBG_FUNC_START
,
305 (int)p
, fd
, 0, 0, 0 );
307 /* cancel all async IO requests on our todo queues for this file descriptor */
308 error
= do_aio_cancel( p
, fd
, 0, TRUE
, FALSE
);
309 if ( error
== AIO_NOTCANCELED
) {
311 * AIO_NOTCANCELED is returned when we find an aio request for this process
312 * and file descriptor on the active async IO queue. Active requests cannot
313 * be cancelled so we must wait for them to complete. We will get a special
314 * wake up call on our channel used to sleep for ALL active requests to
315 * complete. This sleep channel (proc.AIO_CLEANUP_SLEEP_CHAN) is only used
316 * when we must wait for all active aio requests.
319 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_close_sleep
)) | DBG_FUNC_NONE
,
320 (int)p
, fd
, 0, 0, 0 );
322 tsleep( &p
->AIO_CLEANUP_SLEEP_CHAN
, PRIBIO
, "aio_close", 0 );
325 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_close
)) | DBG_FUNC_END
,
326 (int)p
, fd
, 0, 0, 0 );
334 * aio_error - return the error status associated with the async IO
335 * request referred to by uap->aiocbp. The error status is the errno
336 * value that would be set by the corresponding IO request (read, wrtie,
337 * fdatasync, or sync).
341 aio_error( struct proc
*p
, struct aio_error_args
*uap
, int *retval
)
343 aio_workq_entry
*entryp
;
346 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error
)) | DBG_FUNC_START
,
347 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
351 /* quick check to see if there are any async IO requests queued up */
352 if ( aio_get_all_queues_count( ) < 1 ) {
357 /* look for a match on our queue of async IO requests that have completed */
358 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
359 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
360 *retval
= entryp
->errorval
;
362 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error_val
)) | DBG_FUNC_NONE
,
363 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
368 /* look for a match on our queue of active async IO requests */
369 TAILQ_FOREACH( entryp
, &p
->aio_activeq
, aio_workq_link
) {
370 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
371 *retval
= EINPROGRESS
;
373 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error_activeq
)) | DBG_FUNC_NONE
,
374 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
379 /* look for a match on our queue of todo work */
380 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
381 if ( p
== entryp
->procp
&& entryp
->uaiocbp
== uap
->aiocbp
) {
382 *retval
= EINPROGRESS
;
384 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error_workq
)) | DBG_FUNC_NONE
,
385 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
392 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error
)) | DBG_FUNC_END
,
393 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
402 * aio_fsync - asynchronously force all IO operations associated
403 * with the file indicated by the file descriptor (uap->aiocbp->aio_fildes) and
404 * queued at the time of the call to the synchronized completion state.
405 * NOTE - we do not support op O_DSYNC at this point since we do not support the
410 aio_fsync( struct proc
*p
, struct aio_fsync_args
*uap
, int *retval
)
415 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_fsync
)) | DBG_FUNC_START
,
416 (int)p
, (int)uap
->aiocbp
, uap
->op
, 0, 0 );
419 /* 0 := O_SYNC for binary backward compatibility with Panther */
420 if (uap
->op
== O_SYNC
|| uap
->op
== 0)
421 fsync_kind
= AIO_FSYNC
;
422 #if 0 // we don't support fdatasync() call yet
423 else if ( uap
->op
== O_DSYNC
)
424 fsync_kind
= AIO_DSYNC
;
432 error
= aio_queue_async_request( p
, uap
->aiocbp
, fsync_kind
);
437 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_fsync
)) | DBG_FUNC_END
,
438 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
445 /* aio_read - asynchronously read uap->aiocbp->aio_nbytes bytes from the
446 * file descriptor (uap->aiocbp->aio_fildes) into the buffer
447 * (uap->aiocbp->aio_buf).
451 aio_read( struct proc
*p
, struct aio_read_args
*uap
, int *retval
)
455 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_read
)) | DBG_FUNC_START
,
456 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
460 error
= aio_queue_async_request( p
, uap
->aiocbp
, AIO_READ
);
464 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_read
)) | DBG_FUNC_END
,
465 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
473 * aio_return - return the return status associated with the async IO
474 * request referred to by uap->aiocbp. The return status is the value
475 * that would be returned by corresponding IO request (read, wrtie,
476 * fdatasync, or sync). This is where we release kernel resources
477 * held for async IO call associated with the given aiocb pointer.
481 aio_return( struct proc
*p
, struct aio_return_args
*uap
, user_ssize_t
*retval
)
483 aio_workq_entry
*entryp
;
487 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return
)) | DBG_FUNC_START
,
488 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
494 /* quick check to see if there are any async IO requests queued up */
495 if ( aio_get_all_queues_count( ) < 1 ) {
500 /* look for a match on our queue of async IO requests that have completed */
501 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
502 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
503 TAILQ_REMOVE( &p
->aio_doneq
, entryp
, aio_workq_link
);
504 aio_anchor
.aio_done_count
--;
507 *retval
= entryp
->returnval
;
509 /* we cannot free requests that are still completing */
510 if ( (entryp
->flags
& AIO_COMPLETION
) == 0 ) {
513 my_map
= entryp
->aio_map
;
514 entryp
->aio_map
= VM_MAP_NULL
;
517 aio_free_request( entryp
, my_map
);
520 /* tell completion code to free this request */
521 entryp
->flags
|= AIO_DO_FREE
;
523 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return_val
)) | DBG_FUNC_NONE
,
524 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
529 /* look for a match on our queue of active async IO requests */
530 TAILQ_FOREACH( entryp
, &p
->aio_activeq
, aio_workq_link
) {
531 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
533 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return_activeq
)) | DBG_FUNC_NONE
,
534 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
539 /* look for a match on our queue of todo work */
540 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
541 if ( p
== entryp
->procp
&& entryp
->uaiocbp
== uap
->aiocbp
) {
543 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return_workq
)) | DBG_FUNC_NONE
,
544 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
553 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return
)) | DBG_FUNC_END
,
554 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
562 * _aio_exec - internal function used to clean up async IO requests for
563 * a process that is going away due to exec(). We cancel any async IOs
564 * we can and wait for those already active. We also disable signaling
565 * for cancelled or active aio requests that complete.
566 * This routine MAY block!
569 __private_extern__
void
570 _aio_exec( struct proc
*p
)
573 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exec
)) | DBG_FUNC_START
,
574 (int)p
, 0, 0, 0, 0 );
578 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exec
)) | DBG_FUNC_END
,
579 (int)p
, 0, 0, 0, 0 );
587 * _aio_exit - internal function used to clean up async IO requests for
588 * a process that is terminating (via exit() or exec() ). We cancel any async IOs
589 * we can and wait for those already active. We also disable signaling
590 * for cancelled or active aio requests that complete. This routine MAY block!
593 __private_extern__
void
594 _aio_exit( struct proc
*p
)
597 aio_workq_entry
*entryp
;
599 /* quick check to see if there are any async IO requests queued up */
601 count
= aio_get_all_queues_count( );
607 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exit
)) | DBG_FUNC_START
,
608 (int)p
, 0, 0, 0, 0 );
611 * cancel async IO requests on the todo work queue and wait for those
612 * already active to complete.
614 error
= do_aio_cancel( p
, 0, 0, TRUE
, TRUE
);
615 if ( error
== AIO_NOTCANCELED
) {
617 * AIO_NOTCANCELED is returned when we find an aio request for this process
618 * on the active async IO queue. Active requests cannot be cancelled so we
619 * must wait for them to complete. We will get a special wake up call on
620 * our channel used to sleep for ALL active requests to complete. This sleep
621 * channel (proc.AIO_CLEANUP_SLEEP_CHAN) is only used when we must wait for all
622 * active aio requests.
625 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exit_sleep
)) | DBG_FUNC_NONE
,
626 (int)p
, 0, 0, 0, 0 );
628 tsleep( &p
->AIO_CLEANUP_SLEEP_CHAN
, PRIBIO
, "aio_exit", 0 );
631 /* release all aio resources used by this process */
633 entryp
= TAILQ_FIRST( &p
->aio_doneq
);
634 while ( entryp
!= NULL
) {
635 aio_workq_entry
*next_entryp
;
637 next_entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
638 TAILQ_REMOVE( &p
->aio_doneq
, entryp
, aio_workq_link
);
639 aio_anchor
.aio_done_count
--;
642 /* we cannot free requests that are still completing */
643 if ( (entryp
->flags
& AIO_COMPLETION
) == 0 ) {
646 my_map
= entryp
->aio_map
;
647 entryp
->aio_map
= VM_MAP_NULL
;
649 aio_free_request( entryp
, my_map
);
651 /* need to start over since aio_doneq may have been */
652 /* changed while we were away. */
654 entryp
= TAILQ_FIRST( &p
->aio_doneq
);
658 /* tell completion code to free this request */
659 entryp
->flags
|= AIO_DO_FREE
;
660 entryp
= next_entryp
;
664 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exit
)) | DBG_FUNC_END
,
665 (int)p
, 0, 0, 0, 0 );
673 * do_aio_cancel - cancel async IO requests (if possible). We get called by
674 * aio_cancel, close, and at exit.
675 * There are three modes of operation: 1) cancel all async IOs for a process -
676 * fd is 0 and aiocbp is NULL 2) cancel all async IOs for file descriptor - fd
677 * is > 0 and aiocbp is NULL 3) cancel one async IO associated with the given
679 * Returns -1 if no matches were found, AIO_CANCELED when we cancelled all
680 * target async IO requests, AIO_NOTCANCELED if we could not cancel all
681 * target async IO requests, and AIO_ALLDONE if all target async IO requests
682 * were already complete.
683 * WARNING - do not deference aiocbp in this routine, it may point to user
684 * land data that has not been copied in (when called from aio_cancel() )
688 do_aio_cancel( struct proc
*p
, int fd
, user_addr_t aiocbp
,
689 boolean_t wait_for_completion
, boolean_t disable_notification
)
691 aio_workq_entry
*entryp
;
696 /* look for a match on our queue of async todo work. */
698 entryp
= TAILQ_FIRST( &aio_anchor
.aio_async_workq
);
699 while ( entryp
!= NULL
) {
700 aio_workq_entry
*next_entryp
;
702 next_entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
703 if ( p
== entryp
->procp
) {
704 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
705 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
706 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
707 /* we found a match so we remove the entry from the */
708 /* todo work queue and place it on the done queue */
709 TAILQ_REMOVE( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
710 aio_anchor
.aio_async_workq_count
--;
711 entryp
->errorval
= ECANCELED
;
712 entryp
->returnval
= -1;
713 if ( disable_notification
)
714 entryp
->flags
|= AIO_DISABLE
; /* flag for special completion processing */
715 result
= AIO_CANCELED
;
717 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_async_workq
)) | DBG_FUNC_NONE
,
718 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
720 TAILQ_INSERT_TAIL( &p
->aio_doneq
, entryp
, aio_workq_link
);
721 aio_anchor
.aio_done_count
++;
723 entryp
->flags
|= AIO_COMPLETION
;
726 /* do completion processing for this request */
727 do_aio_completion( entryp
);
730 entryp
->flags
&= ~AIO_COMPLETION
;
731 if ( (entryp
->flags
& AIO_DO_FREE
) != 0 ) {
734 my_map
= entryp
->aio_map
;
735 entryp
->aio_map
= VM_MAP_NULL
;
737 aio_free_request( entryp
, my_map
);
742 if ( aiocbp
!= USER_ADDR_NULL
) {
746 /* need to start over since aio_async_workq may have been */
747 /* changed while we were away doing completion processing. */
749 entryp
= TAILQ_FIRST( &aio_anchor
.aio_async_workq
);
753 entryp
= next_entryp
;
757 * look for a match on our queue of synchronous todo work. This will
758 * be a rare occurrence but could happen if a process is terminated while
759 * processing a lio_listio call.
761 entryp
= TAILQ_FIRST( &aio_anchor
.lio_sync_workq
);
762 while ( entryp
!= NULL
) {
763 aio_workq_entry
*next_entryp
;
765 next_entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
766 if ( p
== entryp
->procp
) {
767 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
768 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
769 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
770 /* we found a match so we remove the entry from the */
771 /* todo work queue and place it on the done queue */
772 TAILQ_REMOVE( &aio_anchor
.lio_sync_workq
, entryp
, aio_workq_link
);
773 aio_anchor
.lio_sync_workq_count
--;
774 entryp
->errorval
= ECANCELED
;
775 entryp
->returnval
= -1;
776 if ( disable_notification
)
777 entryp
->flags
|= AIO_DISABLE
; /* flag for special completion processing */
778 result
= AIO_CANCELED
;
780 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_sync_workq
)) | DBG_FUNC_NONE
,
781 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
783 TAILQ_INSERT_TAIL( &p
->aio_doneq
, entryp
, aio_workq_link
);
784 aio_anchor
.aio_done_count
++;
786 if ( aiocbp
!= USER_ADDR_NULL
) {
792 entryp
= next_entryp
;
796 * look for a match on our queue of active async IO requests and
797 * return AIO_NOTCANCELED result.
799 TAILQ_FOREACH( entryp
, &p
->aio_activeq
, aio_workq_link
) {
800 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
801 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
802 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
803 result
= AIO_NOTCANCELED
;
805 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_activeq
)) | DBG_FUNC_NONE
,
806 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
808 if ( wait_for_completion
)
809 entryp
->flags
|= AIO_WAITING
; /* flag for special completion processing */
810 if ( disable_notification
)
811 entryp
->flags
|= AIO_DISABLE
; /* flag for special completion processing */
812 if ( aiocbp
!= USER_ADDR_NULL
) {
820 * if we didn't find any matches on the todo or active queues then look for a
821 * match on our queue of async IO requests that have completed and if found
822 * return AIO_ALLDONE result.
824 if ( result
== -1 ) {
825 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
826 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
827 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
828 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
829 result
= AIO_ALLDONE
;
831 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_doneq
)) | DBG_FUNC_NONE
,
832 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
834 if ( aiocbp
!= USER_ADDR_NULL
) {
845 } /* do_aio_cancel */
849 * aio_suspend - suspend the calling thread until at least one of the async
850 * IO operations referenced by uap->aiocblist has completed, until a signal
851 * interrupts the function, or uap->timeoutp time interval (optional) has
853 * Returns 0 if one or more async IOs have completed else -1 and errno is
854 * set appropriately - EAGAIN if timeout elapses or EINTR if an interrupt
859 aio_suspend( struct proc
*p
, struct aio_suspend_args
*uap
, int *retval
)
864 struct user_timespec ts
;
865 aio_workq_entry
*entryp
;
866 user_addr_t
*aiocbpp
;
868 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_suspend
)) | DBG_FUNC_START
,
869 (int)p
, uap
->nent
, 0, 0, 0 );
875 /* quick check to see if there are any async IO requests queued up */
877 count
= aio_get_all_queues_count( );
881 goto ExitThisRoutine
;
884 if ( uap
->nent
< 1 || uap
->nent
> aio_max_requests_per_process
) {
886 goto ExitThisRoutine
;
889 if ( uap
->timeoutp
!= USER_ADDR_NULL
) {
890 if ( proc_is64bit(p
) ) {
891 error
= copyin( uap
->timeoutp
, &ts
, sizeof(ts
) );
894 struct timespec temp
;
895 error
= copyin( uap
->timeoutp
, &temp
, sizeof(temp
) );
897 ts
.tv_sec
= temp
.tv_sec
;
898 ts
.tv_nsec
= temp
.tv_nsec
;
903 goto ExitThisRoutine
;
906 if ( ts
.tv_nsec
< 0 || ts
.tv_nsec
>= 1000000000 ) {
908 goto ExitThisRoutine
;
911 nanoseconds_to_absolutetime( (uint64_t)ts
.tv_sec
* NSEC_PER_SEC
+ ts
.tv_nsec
,
913 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
916 /* we reserve enough space for largest possible pointer size */
917 MALLOC( aiocbpp
, user_addr_t
*, (uap
->nent
* sizeof(user_addr_t
)), M_TEMP
, M_WAITOK
);
918 if ( aiocbpp
== NULL
) {
920 goto ExitThisRoutine
;
923 /* copyin our aiocb pointers from list */
924 error
= copyin( uap
->aiocblist
, aiocbpp
,
925 proc_is64bit(p
) ? (uap
->nent
* sizeof(user_addr_t
))
926 : (uap
->nent
* sizeof(uintptr_t)) );
929 goto ExitThisRoutine
;
932 /* we depend on a list of user_addr_t's so we need to munge and expand */
933 /* when these pointers came from a 32-bit process */
934 if ( !proc_is64bit(p
) && sizeof(uintptr_t) < sizeof(user_addr_t
) ) {
935 /* position to the last entry and work back from there */
936 uintptr_t *my_ptrp
= ((uintptr_t *)aiocbpp
) + (uap
->nent
- 1);
937 user_addr_t
*my_addrp
= aiocbpp
+ (uap
->nent
- 1);
938 for (i
= 0; i
< uap
->nent
; i
++, my_ptrp
--, my_addrp
--) {
939 *my_addrp
= (user_addr_t
) (*my_ptrp
);
943 /* check list of aio requests to see if any have completed */
945 for ( i
= 0; i
< uap
->nent
; i
++ ) {
948 /* NULL elements are legal so check for 'em */
949 aiocbp
= *(aiocbpp
+ i
);
950 if ( aiocbp
== USER_ADDR_NULL
)
953 /* return immediately if any aio request in the list is done */
954 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
955 if ( entryp
->uaiocbp
== aiocbp
) {
959 goto ExitThisRoutine
;
962 } /* for ( ; i < uap->nent; ) */
964 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_suspend_sleep
)) | DBG_FUNC_NONE
,
965 (int)p
, uap
->nent
, 0, 0, 0 );
968 * wait for an async IO to complete or a signal fires or timeout expires.
969 * we return EAGAIN (35) for timeout expiration and EINTR (4) when a signal
970 * interrupts us. If an async IO completes before a signal fires or our
971 * timeout expires, we get a wakeup call from aio_work_thread().
973 assert_wait_deadline( (event_t
) &p
->AIO_SUSPEND_SLEEP_CHAN
, THREAD_ABORTSAFE
, abstime
);
976 error
= thread_block( THREAD_CONTINUE_NULL
);
978 if ( error
== THREAD_AWAKENED
) {
979 /* got our wakeup call from aio_work_thread() */
983 else if ( error
== THREAD_TIMED_OUT
) {
984 /* our timeout expired */
988 /* we were interrupted */
993 if ( aiocbpp
!= NULL
)
994 FREE( aiocbpp
, M_TEMP
);
996 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_suspend
)) | DBG_FUNC_END
,
997 (int)p
, uap
->nent
, error
, 0, 0 );
1004 /* aio_write - asynchronously write uap->aiocbp->aio_nbytes bytes to the
1005 * file descriptor (uap->aiocbp->aio_fildes) from the buffer
1006 * (uap->aiocbp->aio_buf).
1010 aio_write( struct proc
*p
, struct aio_write_args
*uap
, int *retval
)
1016 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_write
)) | DBG_FUNC_START
,
1017 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
1019 error
= aio_queue_async_request( p
, uap
->aiocbp
, AIO_WRITE
);
1023 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_write
)) | DBG_FUNC_END
,
1024 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
1032 * lio_listio - initiate a list of IO requests. We process the list of aiocbs
1033 * either synchronously (mode == LIO_WAIT) or asynchronously (mode == LIO_NOWAIT).
1034 * The caller gets error and return status for each aiocb in the list via aio_error
1035 * and aio_return. We must keep completed requests until released by the
1040 lio_listio( struct proc
*p
, struct lio_listio_args
*uap
, int *retval
)
1046 aio_workq_entry
* *entryp_listp
;
1047 user_addr_t
*aiocbpp
;
1049 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_listio
)) | DBG_FUNC_START
,
1050 (int)p
, uap
->nent
, uap
->mode
, 0, 0 );
1052 entryp_listp
= NULL
;
1056 if ( !(uap
->mode
== LIO_NOWAIT
|| uap
->mode
== LIO_WAIT
) ) {
1057 call_result
= EINVAL
;
1061 if ( uap
->nent
< 1 || uap
->nent
> AIO_LISTIO_MAX
) {
1062 call_result
= EINVAL
;
1067 * we use group_tag to mark IO requests for delayed completion processing
1068 * which means we wait until all IO requests in the group have completed
1069 * before we either return to the caller when mode is LIO_WAIT or signal
1070 * user when mode is LIO_NOWAIT.
1072 group_tag
= random();
1075 * allocate a list of aio_workq_entry pointers that we will use to queue
1076 * up all our requests at once while holding our lock.
1078 MALLOC( entryp_listp
, void *, (uap
->nent
* sizeof(aio_workq_entry
*)), M_TEMP
, M_WAITOK
);
1079 if ( entryp_listp
== NULL
) {
1080 call_result
= EAGAIN
;
1084 /* we reserve enough space for largest possible pointer size */
1085 MALLOC( aiocbpp
, user_addr_t
*, (uap
->nent
* sizeof(user_addr_t
)), M_TEMP
, M_WAITOK
);
1086 if ( aiocbpp
== NULL
) {
1087 call_result
= EAGAIN
;
1091 /* copyin our aiocb pointers from list */
1092 result
= copyin( uap
->aiocblist
, aiocbpp
,
1093 IS_64BIT_PROCESS(p
) ? (uap
->nent
* sizeof(user_addr_t
))
1094 : (uap
->nent
* sizeof(uintptr_t)) );
1095 if ( result
!= 0 ) {
1096 call_result
= EAGAIN
;
1100 /* we depend on a list of user_addr_t's so we need to munge and expand */
1101 /* when these pointers came from a 32-bit process */
1102 if ( !IS_64BIT_PROCESS(p
) && sizeof(uintptr_t) < sizeof(user_addr_t
) ) {
1103 /* position to the last entry and work back from there */
1104 uintptr_t *my_ptrp
= ((uintptr_t *)aiocbpp
) + (uap
->nent
- 1);
1105 user_addr_t
*my_addrp
= aiocbpp
+ (uap
->nent
- 1);
1106 for (i
= 0; i
< uap
->nent
; i
++, my_ptrp
--, my_addrp
--) {
1107 *my_addrp
= (user_addr_t
) (*my_ptrp
);
1111 /* process list of aio requests */
1112 for ( i
= 0; i
< uap
->nent
; i
++ ) {
1113 user_addr_t my_aiocbp
;
1115 *(entryp_listp
+ i
) = NULL
;
1116 my_aiocbp
= *(aiocbpp
+ i
);
1118 /* NULL elements are legal so check for 'em */
1119 if ( my_aiocbp
== USER_ADDR_NULL
)
1122 if ( uap
->mode
== LIO_NOWAIT
)
1123 result
= lio_create_async_entry( p
, my_aiocbp
, uap
->sigp
,
1124 group_tag
, (entryp_listp
+ i
) );
1126 result
= lio_create_sync_entry( p
, my_aiocbp
, group_tag
,
1127 (entryp_listp
+ i
) );
1129 if ( result
!= 0 && call_result
== -1 )
1130 call_result
= result
;
1134 * we need to protect this section since we do not want any of these grouped
1135 * IO requests to begin until we have them all on the queue.
1138 for ( i
= 0; i
< uap
->nent
; i
++ ) {
1139 aio_workq_entry
*entryp
;
1141 /* NULL elements are legal so check for 'em */
1142 entryp
= *(entryp_listp
+ i
);
1143 if ( entryp
== NULL
)
1146 /* check our aio limits to throttle bad or rude user land behavior */
1147 if ( aio_get_all_queues_count( ) >= aio_max_requests
||
1148 aio_get_process_count( entryp
->procp
) >= aio_max_requests_per_process
||
1149 is_already_queued( entryp
->procp
, entryp
->uaiocbp
) == TRUE
) {
1152 my_map
= entryp
->aio_map
;
1153 entryp
->aio_map
= VM_MAP_NULL
;
1154 if ( call_result
== -1 )
1155 call_result
= EAGAIN
;
1157 aio_free_request( entryp
, my_map
);
1162 /* place the request on the appropriate queue */
1163 if ( uap
->mode
== LIO_NOWAIT
) {
1164 TAILQ_INSERT_TAIL( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
1165 aio_anchor
.aio_async_workq_count
++;
1167 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_work_queued
)) | DBG_FUNC_NONE
,
1168 (int)p
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1171 TAILQ_INSERT_TAIL( &aio_anchor
.lio_sync_workq
, entryp
, aio_workq_link
);
1172 aio_anchor
.lio_sync_workq_count
++;
1176 if ( uap
->mode
== LIO_NOWAIT
) {
1177 /* caller does not want to wait so we'll fire off a worker thread and return */
1178 wakeup_one( (caddr_t
) &aio_anchor
.aio_async_workq
);
1181 aio_workq_entry
*entryp
;
1185 * mode is LIO_WAIT - handle the IO requests now.
1187 entryp
= TAILQ_FIRST( &aio_anchor
.lio_sync_workq
);
1188 while ( entryp
!= NULL
) {
1189 if ( p
== entryp
->procp
&& group_tag
== entryp
->group_tag
) {
1191 TAILQ_REMOVE( &aio_anchor
.lio_sync_workq
, entryp
, aio_workq_link
);
1192 aio_anchor
.lio_sync_workq_count
--;
1195 if ( (entryp
->flags
& AIO_READ
) != 0 ) {
1196 error
= do_aio_read( entryp
);
1198 else if ( (entryp
->flags
& AIO_WRITE
) != 0 ) {
1199 error
= do_aio_write( entryp
);
1201 else if ( (entryp
->flags
& AIO_FSYNC
) != 0 ) {
1202 error
= do_aio_fsync( entryp
);
1205 printf( "%s - unknown aio request - flags 0x%02X \n",
1206 __FUNCTION__
, entryp
->flags
);
1209 entryp
->errorval
= error
;
1210 if ( error
!= 0 && call_result
== -1 )
1214 /* we're done with the IO request so move it on the done queue */
1215 TAILQ_INSERT_TAIL( &p
->aio_doneq
, entryp
, aio_workq_link
);
1216 aio_anchor
.aio_done_count
++;
1217 p
->aio_done_count
++;
1219 /* need to start over since lio_sync_workq may have been changed while we */
1220 /* were away doing the IO. */
1221 entryp
= TAILQ_FIRST( &aio_anchor
.lio_sync_workq
);
1223 } /* p == entryp->procp */
1225 entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
1226 } /* while ( entryp != NULL ) */
1227 } /* uap->mode == LIO_WAIT */
1230 /* call_result == -1 means we had no trouble queueing up requests */
1231 if ( call_result
== -1 ) {
1237 if ( entryp_listp
!= NULL
)
1238 FREE( entryp_listp
, M_TEMP
);
1239 if ( aiocbpp
!= NULL
)
1240 FREE( aiocbpp
, M_TEMP
);
1242 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_listio
)) | DBG_FUNC_END
,
1243 (int)p
, call_result
, 0, 0, 0 );
1245 return( call_result
);
1251 * aio worker thread. this is where all the real work gets done.
1252 * we get a wake up call on sleep channel &aio_anchor.aio_async_workq
1253 * after new work is queued up.
1257 aio_work_thread( void )
1259 aio_workq_entry
*entryp
;
1263 entryp
= aio_get_some_work();
1264 if ( entryp
== NULL
) {
1266 * aio worker threads wait for some work to get queued up
1267 * by aio_queue_async_request. Once some work gets queued
1268 * it will wake up one of these worker threads just before
1269 * returning to our caller in user land.
1271 assert_wait( (event_t
) &aio_anchor
.aio_async_workq
, THREAD_UNINT
);
1274 thread_block( (thread_continue_t
)aio_work_thread
);
1279 vm_map_t currentmap
;
1280 vm_map_t oldmap
= VM_MAP_NULL
;
1281 task_t oldaiotask
= TASK_NULL
;
1282 struct uthread
*uthreadp
= NULL
;
1286 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_worker_thread
)) | DBG_FUNC_START
,
1287 (int)entryp
->procp
, (int)entryp
->uaiocbp
, entryp
->flags
, 0, 0 );
1290 * Assume the target's address space identity for the duration
1293 currentmap
= get_task_map( (current_proc())->task
);
1294 if ( currentmap
!= entryp
->aio_map
) {
1295 uthreadp
= (struct uthread
*) get_bsdthread_info(current_thread());
1296 oldaiotask
= uthreadp
->uu_aio_task
;
1297 uthreadp
->uu_aio_task
= entryp
->procp
->task
;
1298 oldmap
= vm_map_switch( entryp
->aio_map
);
1301 if ( (entryp
->flags
& AIO_READ
) != 0 ) {
1302 error
= do_aio_read( entryp
);
1304 else if ( (entryp
->flags
& AIO_WRITE
) != 0 ) {
1305 error
= do_aio_write( entryp
);
1307 else if ( (entryp
->flags
& AIO_FSYNC
) != 0 ) {
1308 error
= do_aio_fsync( entryp
);
1311 printf( "%s - unknown aio request - flags 0x%02X \n",
1312 __FUNCTION__
, entryp
->flags
);
1315 entryp
->errorval
= error
;
1316 if ( currentmap
!= entryp
->aio_map
) {
1317 (void) vm_map_switch( oldmap
);
1318 uthreadp
->uu_aio_task
= oldaiotask
;
1321 /* we're done with the IO request so pop it off the active queue and */
1322 /* push it on the done queue */
1324 TAILQ_REMOVE( &entryp
->procp
->aio_activeq
, entryp
, aio_workq_link
);
1325 aio_anchor
.aio_active_count
--;
1326 entryp
->procp
->aio_active_count
--;
1327 TAILQ_INSERT_TAIL( &entryp
->procp
->aio_doneq
, entryp
, aio_workq_link
);
1328 aio_anchor
.aio_done_count
++;
1329 entryp
->procp
->aio_done_count
++;
1330 entryp
->flags
|= AIO_COMPLETION
;
1332 /* remove our reference to the user land map. */
1333 if ( VM_MAP_NULL
!= entryp
->aio_map
) {
1336 my_map
= entryp
->aio_map
;
1337 entryp
->aio_map
= VM_MAP_NULL
;
1338 AIO_UNLOCK
; /* must unlock before calling vm_map_deallocate() */
1339 vm_map_deallocate( my_map
);
1345 do_aio_completion( entryp
);
1347 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_worker_thread
)) | DBG_FUNC_END
,
1348 (int)entryp
->procp
, (int)entryp
->uaiocbp
, entryp
->errorval
,
1349 entryp
->returnval
, 0 );
1352 entryp
->flags
&= ~AIO_COMPLETION
;
1353 if ( (entryp
->flags
& AIO_DO_FREE
) != 0 ) {
1356 my_map
= entryp
->aio_map
;
1357 entryp
->aio_map
= VM_MAP_NULL
;
1359 aio_free_request( entryp
, my_map
);
1368 } /* aio_work_thread */
1372 * aio_get_some_work - get the next async IO request that is ready to be executed.
1373 * aio_fsync complicates matters a bit since we cannot do the fsync until all async
1374 * IO requests at the time the aio_fsync call came in have completed.
1375 * NOTE - AIO_LOCK must be held by caller
1378 static aio_workq_entry
*
1379 aio_get_some_work( void )
1381 aio_workq_entry
*entryp
;
1383 /* pop some work off the work queue and add to our active queue */
1384 for ( entryp
= TAILQ_FIRST( &aio_anchor
.aio_async_workq
);
1386 entryp
= TAILQ_NEXT( entryp
, aio_workq_link
) ) {
1388 if ( (entryp
->flags
& AIO_FSYNC
) != 0 ) {
1389 /* leave aio_fsync calls on the work queue if there are IO */
1390 /* requests on the active queue for the same file descriptor. */
1391 if ( aio_delay_fsync_request( entryp
) ) {
1393 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_fsync_delay
)) | DBG_FUNC_NONE
,
1394 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1401 if ( entryp
!= NULL
) {
1402 TAILQ_REMOVE( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
1403 aio_anchor
.aio_async_workq_count
--;
1404 TAILQ_INSERT_TAIL( &entryp
->procp
->aio_activeq
, entryp
, aio_workq_link
);
1405 aio_anchor
.aio_active_count
++;
1406 entryp
->procp
->aio_active_count
++;
1411 } /* aio_get_some_work */
1415 * aio_delay_fsync_request - look to see if this aio_fsync request should be delayed at
1416 * this time. Delay will happen when there are any active IOs for the same file
1417 * descriptor that were queued at time the aio_sync call was queued.
1418 * NOTE - AIO_LOCK must be held by caller
1421 aio_delay_fsync_request( aio_workq_entry
*entryp
)
1423 aio_workq_entry
*my_entryp
;
1425 TAILQ_FOREACH( my_entryp
, &entryp
->procp
->aio_activeq
, aio_workq_link
) {
1426 if ( my_entryp
->fsyncp
!= USER_ADDR_NULL
&&
1427 entryp
->uaiocbp
== my_entryp
->fsyncp
&&
1428 entryp
->aiocb
.aio_fildes
== my_entryp
->aiocb
.aio_fildes
) {
1435 } /* aio_delay_fsync_request */
1439 * aio_queue_async_request - queue up an async IO request on our work queue then
1440 * wake up one of our worker threads to do the actual work. We get a reference
1441 * to our caller's user land map in order to keep it around while we are
1442 * processing the request.
1446 aio_queue_async_request( struct proc
*procp
, user_addr_t aiocbp
, int kindOfIO
)
1448 aio_workq_entry
*entryp
;
1451 entryp
= (aio_workq_entry
*) zalloc( aio_workq_zonep
);
1452 if ( entryp
== NULL
) {
1456 bzero( entryp
, sizeof(*entryp
) );
1458 /* fill in the rest of the aio_workq_entry */
1459 entryp
->procp
= procp
;
1460 entryp
->uaiocbp
= aiocbp
;
1461 entryp
->flags
|= kindOfIO
;
1462 entryp
->aio_map
= VM_MAP_NULL
;
1464 if ( !IS_64BIT_PROCESS(procp
) ) {
1465 struct aiocb aiocb32
;
1467 result
= copyin( aiocbp
, &aiocb32
, sizeof(aiocb32
) );
1469 do_munge_aiocb( &aiocb32
, &entryp
->aiocb
);
1471 result
= copyin( aiocbp
, &entryp
->aiocb
, sizeof(entryp
->aiocb
) );
1473 if ( result
!= 0 ) {
1478 /* do some more validation on the aiocb and embedded file descriptor */
1479 result
= aio_validate( entryp
);
1483 /* get a reference to the user land map in order to keep it around */
1484 entryp
->aio_map
= get_task_map( procp
->task
);
1485 vm_map_reference( entryp
->aio_map
);
1489 if ( is_already_queued( entryp
->procp
, entryp
->uaiocbp
) == TRUE
) {
1495 /* check our aio limits to throttle bad or rude user land behavior */
1496 if ( aio_get_all_queues_count( ) >= aio_max_requests
||
1497 aio_get_process_count( procp
) >= aio_max_requests_per_process
) {
1504 * aio_fsync calls sync up all async IO requests queued at the time
1505 * the aio_fsync call was made. So we mark each currently queued async
1506 * IO with a matching file descriptor as must complete before we do the
1507 * fsync. We set the fsyncp field of each matching async IO
1508 * request with the aiocb pointer passed in on the aio_fsync call to
1509 * know which IOs must complete before we process the aio_fsync call.
1511 if ( (kindOfIO
& AIO_FSYNC
) != 0 )
1512 aio_mark_requests( entryp
);
1514 /* queue up on our aio asynchronous work queue */
1515 TAILQ_INSERT_TAIL( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
1516 aio_anchor
.aio_async_workq_count
++;
1518 wakeup_one( (caddr_t
) &aio_anchor
.aio_async_workq
);
1521 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_work_queued
)) | DBG_FUNC_NONE
,
1522 (int)procp
, (int)aiocbp
, 0, 0, 0 );
1527 if ( entryp
!= NULL
) {
1528 /* this entry has not been queued up so no worries about unlocked */
1529 /* state and aio_map */
1530 aio_free_request( entryp
, entryp
->aio_map
);
1535 } /* aio_queue_async_request */
1539 * lio_create_async_entry - allocate an aio_workq_entry and fill it in.
1540 * If all goes well return 0 and pass the aio_workq_entry pointer back to
1541 * our caller. We get a reference to our caller's user land map in order to keep
1542 * it around while we are processing the request.
1543 * lio_listio calls behave differently at completion they do completion notification
1544 * when all async IO requests have completed. We use group_tag to tag IO requests
1545 * that behave in the delay notification manner.
1549 lio_create_async_entry( struct proc
*procp
, user_addr_t aiocbp
,
1550 user_addr_t sigp
, long group_tag
,
1551 aio_workq_entry
**entrypp
)
1553 aio_workq_entry
*entryp
;
1556 entryp
= (aio_workq_entry
*) zalloc( aio_workq_zonep
);
1557 if ( entryp
== NULL
) {
1561 bzero( entryp
, sizeof(*entryp
) );
1563 /* fill in the rest of the aio_workq_entry */
1564 entryp
->procp
= procp
;
1565 entryp
->uaiocbp
= aiocbp
;
1566 entryp
->flags
|= AIO_LIO
;
1567 entryp
->group_tag
= group_tag
;
1568 entryp
->aio_map
= VM_MAP_NULL
;
1570 if ( !IS_64BIT_PROCESS(procp
) ) {
1571 struct aiocb aiocb32
;
1573 result
= copyin( aiocbp
, &aiocb32
, sizeof(aiocb32
) );
1575 do_munge_aiocb( &aiocb32
, &entryp
->aiocb
);
1577 result
= copyin( aiocbp
, &entryp
->aiocb
, sizeof(entryp
->aiocb
) );
1579 if ( result
!= 0 ) {
1584 /* look for lio_listio LIO_NOP requests and ignore them. */
1585 /* Not really an error, but we need to free our aio_workq_entry. */
1586 if ( entryp
->aiocb
.aio_lio_opcode
== LIO_NOP
) {
1591 /* use sigevent passed in to lio_listio for each of our calls, but only */
1592 /* do completion notification after the last request completes. */
1593 if ( sigp
!= USER_ADDR_NULL
) {
1594 if ( !IS_64BIT_PROCESS(procp
) ) {
1595 struct sigevent sigevent32
;
1597 result
= copyin( sigp
, &sigevent32
, sizeof(sigevent32
) );
1598 if ( result
== 0 ) {
1599 /* also need to munge aio_sigevent since it contains pointers */
1600 /* special case here. since we do not know if sigev_value is an */
1601 /* int or a ptr we do NOT cast the ptr to a user_addr_t. This */
1602 /* means if we send this info back to user space we need to remember */
1603 /* sigev_value was not expanded for the 32-bit case. */
1604 /* NOTE - this does NOT affect us since we don't support sigev_value */
1605 /* yet in the aio context. */
1607 entryp
->aiocb
.aio_sigevent
.sigev_notify
= sigevent32
.sigev_notify
;
1608 entryp
->aiocb
.aio_sigevent
.sigev_signo
= sigevent32
.sigev_signo
;
1609 entryp
->aiocb
.aio_sigevent
.sigev_value
.size_equivalent
.sival_int
=
1610 sigevent32
.sigev_value
.sival_int
;
1611 entryp
->aiocb
.aio_sigevent
.sigev_notify_function
=
1612 CAST_USER_ADDR_T(sigevent32
.sigev_notify_function
);
1613 entryp
->aiocb
.aio_sigevent
.sigev_notify_attributes
=
1614 CAST_USER_ADDR_T(sigevent32
.sigev_notify_attributes
);
1617 result
= copyin( sigp
, &entryp
->aiocb
.aio_sigevent
, sizeof(entryp
->aiocb
.aio_sigevent
) );
1619 if ( result
!= 0 ) {
1625 /* do some more validation on the aiocb and embedded file descriptor */
1626 result
= aio_validate( entryp
);
1630 /* get a reference to the user land map in order to keep it around */
1631 entryp
->aio_map
= get_task_map( procp
->task
);
1632 vm_map_reference( entryp
->aio_map
);
1638 if ( entryp
!= NULL
)
1639 zfree( aio_workq_zonep
, entryp
);
1643 } /* lio_create_async_entry */
1647 * aio_mark_requests - aio_fsync calls synchronize file data for all queued async IO
1648 * requests at the moment the aio_fsync call is queued. We use aio_workq_entry.fsyncp
1649 * to mark each async IO that must complete before the fsync is done. We use the uaiocbp
1650 * field from the aio_fsync call as the aio_workq_entry.fsyncp in marked requests.
1651 * NOTE - AIO_LOCK must be held by caller
1655 aio_mark_requests( aio_workq_entry
*entryp
)
1657 aio_workq_entry
*my_entryp
;
1659 TAILQ_FOREACH( my_entryp
, &entryp
->procp
->aio_activeq
, aio_workq_link
) {
1660 if ( entryp
->aiocb
.aio_fildes
== my_entryp
->aiocb
.aio_fildes
) {
1661 my_entryp
->fsyncp
= entryp
->uaiocbp
;
1665 TAILQ_FOREACH( my_entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
1666 if ( entryp
->procp
== my_entryp
->procp
&&
1667 entryp
->aiocb
.aio_fildes
== my_entryp
->aiocb
.aio_fildes
) {
1668 my_entryp
->fsyncp
= entryp
->uaiocbp
;
1672 } /* aio_mark_requests */
1676 * lio_create_sync_entry - allocate an aio_workq_entry and fill it in.
1677 * If all goes well return 0 and pass the aio_workq_entry pointer back to
1679 * lio_listio calls behave differently at completion they do completion notification
1680 * when all async IO requests have completed. We use group_tag to tag IO requests
1681 * that behave in the delay notification manner.
1685 lio_create_sync_entry( struct proc
*procp
, user_addr_t aiocbp
,
1686 long group_tag
, aio_workq_entry
**entrypp
)
1688 aio_workq_entry
*entryp
;
1691 entryp
= (aio_workq_entry
*) zalloc( aio_workq_zonep
);
1692 if ( entryp
== NULL
) {
1696 bzero( entryp
, sizeof(*entryp
) );
1698 /* fill in the rest of the aio_workq_entry */
1699 entryp
->procp
= procp
;
1700 entryp
->uaiocbp
= aiocbp
;
1701 entryp
->flags
|= AIO_LIO
;
1702 entryp
->group_tag
= group_tag
;
1703 entryp
->aio_map
= VM_MAP_NULL
;
1705 if ( !IS_64BIT_PROCESS(procp
) ) {
1706 struct aiocb aiocb32
;
1708 result
= copyin( aiocbp
, &aiocb32
, sizeof(aiocb32
) );
1710 do_munge_aiocb( &aiocb32
, &entryp
->aiocb
);
1712 result
= copyin( aiocbp
, &entryp
->aiocb
, sizeof(entryp
->aiocb
) );
1714 if ( result
!= 0 ) {
1719 /* look for lio_listio LIO_NOP requests and ignore them. */
1720 /* Not really an error, but we need to free our aio_workq_entry. */
1721 if ( entryp
->aiocb
.aio_lio_opcode
== LIO_NOP
) {
1726 result
= aio_validate( entryp
);
1727 if ( result
!= 0 ) {
1735 if ( entryp
!= NULL
)
1736 zfree( aio_workq_zonep
, entryp
);
1740 } /* lio_create_sync_entry */
1744 * aio_free_request - remove our reference on the user land map and
1745 * free the work queue entry resources.
1746 * We are not holding the lock here thus aio_map is passed in and
1747 * zeroed while we did have the lock.
1751 aio_free_request( aio_workq_entry
*entryp
, vm_map_t the_map
)
1753 /* remove our reference to the user land map. */
1754 if ( VM_MAP_NULL
!= the_map
) {
1755 vm_map_deallocate( the_map
);
1758 zfree( aio_workq_zonep
, entryp
);
1762 } /* aio_free_request */
1765 /* aio_validate - validate the aiocb passed in by one of the aio syscalls.
1769 aio_validate( aio_workq_entry
*entryp
)
1771 struct fileproc
*fp
;
1777 if ( (entryp
->flags
& AIO_LIO
) != 0 ) {
1778 if ( entryp
->aiocb
.aio_lio_opcode
== LIO_READ
)
1779 entryp
->flags
|= AIO_READ
;
1780 else if ( entryp
->aiocb
.aio_lio_opcode
== LIO_WRITE
)
1781 entryp
->flags
|= AIO_WRITE
;
1782 else if ( entryp
->aiocb
.aio_lio_opcode
== LIO_NOP
)
1789 if ( (entryp
->flags
& (AIO_WRITE
| AIO_FSYNC
)) != 0 ) {
1793 if ( (entryp
->flags
& (AIO_READ
| AIO_WRITE
)) != 0 ) {
1794 // LP64todo - does max value for aio_nbytes need to grow?
1795 if ( entryp
->aiocb
.aio_nbytes
> INT_MAX
||
1796 entryp
->aiocb
.aio_buf
== USER_ADDR_NULL
||
1797 entryp
->aiocb
.aio_offset
< 0 )
1801 /* validate aiocb.aio_sigevent. at this point we only support sigev_notify
1802 * equal to SIGEV_SIGNAL or SIGEV_NONE. this means sigev_value,
1803 * sigev_notify_function, and sigev_notify_attributes are ignored.
1805 if ( entryp
->aiocb
.aio_sigevent
.sigev_notify
== SIGEV_SIGNAL
) {
1807 /* make sure we have a valid signal number */
1808 signum
= entryp
->aiocb
.aio_sigevent
.sigev_signo
;
1809 if ( signum
<= 0 || signum
>= NSIG
||
1810 signum
== SIGKILL
|| signum
== SIGSTOP
)
1813 else if ( entryp
->aiocb
.aio_sigevent
.sigev_notify
!= SIGEV_NONE
)
1816 /* validate the file descriptor and that the file was opened
1817 * for the appropriate read / write access.
1819 proc_fdlock(entryp
->procp
);
1821 result
= fp_lookup( entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, 1);
1822 if ( result
== 0 ) {
1823 if ( (fp
->f_fglob
->fg_flag
& flag
) == 0 ) {
1824 /* we don't have read or write access */
1827 else if ( fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
1828 /* this is not a file */
1831 fp
->f_flags
|= FP_AIOISSUED
;
1833 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 1);
1839 proc_fdunlock(entryp
->procp
);
1843 } /* aio_validate */
1847 * aio_get_process_count - runs through our queues that hold outstanding
1848 * async IO reqests and totals up number of requests for the given
1850 * NOTE - caller must hold aio lock!
1854 aio_get_process_count( struct proc
*procp
)
1856 aio_workq_entry
*entryp
;
1859 /* begin with count of completed async IO requests for this process */
1860 count
= procp
->aio_done_count
;
1862 /* add in count of active async IO requests for this process */
1863 count
+= procp
->aio_active_count
;
1865 /* look for matches on our queue of asynchronous todo work */
1866 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
1867 if ( procp
== entryp
->procp
) {
1872 /* look for matches on our queue of synchronous todo work */
1873 TAILQ_FOREACH( entryp
, &aio_anchor
.lio_sync_workq
, aio_workq_link
) {
1874 if ( procp
== entryp
->procp
) {
1881 } /* aio_get_process_count */
1885 * aio_get_all_queues_count - get total number of entries on all aio work queues.
1886 * NOTE - caller must hold aio lock!
1890 aio_get_all_queues_count( void )
1894 count
= aio_anchor
.aio_async_workq_count
;
1895 count
+= aio_anchor
.lio_sync_workq_count
;
1896 count
+= aio_anchor
.aio_active_count
;
1897 count
+= aio_anchor
.aio_done_count
;
1901 } /* aio_get_all_queues_count */
1905 * do_aio_completion. Handle async IO completion.
1909 do_aio_completion( aio_workq_entry
*entryp
)
1911 /* signal user land process if appropriate */
1912 if ( entryp
->aiocb
.aio_sigevent
.sigev_notify
== SIGEV_SIGNAL
&&
1913 (entryp
->flags
& AIO_DISABLE
) == 0 ) {
1916 * if group_tag is non zero then make sure this is the last IO request
1917 * in the group before we signal.
1919 if ( entryp
->group_tag
== 0 ||
1920 (entryp
->group_tag
!= 0 && aio_last_group_io( entryp
)) ) {
1921 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_sig
)) | DBG_FUNC_NONE
,
1922 (int)entryp
->procp
, (int)entryp
->uaiocbp
,
1923 entryp
->aiocb
.aio_sigevent
.sigev_signo
, 0, 0 );
1925 psignal( entryp
->procp
, entryp
->aiocb
.aio_sigevent
.sigev_signo
);
1931 * need to handle case where a process is trying to exit, exec, or close
1932 * and is currently waiting for active aio requests to complete. If
1933 * AIO_WAITING is set then we need to look to see if there are any
1934 * other requests in the active queue for this process. If there are
1935 * none then wakeup using the AIO_CLEANUP_SLEEP_CHAN tsleep channel. If
1936 * there are some still active then do nothing - we only want to wakeup
1937 * when all active aio requests for the process are complete.
1939 if ( (entryp
->flags
& AIO_WAITING
) != 0 ) {
1940 int active_requests
;
1942 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_cleanup_wait
)) | DBG_FUNC_NONE
,
1943 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1946 active_requests
= aio_active_requests_for_process( entryp
->procp
);
1948 if ( active_requests
< 1 ) {
1949 /* no active aio requests for this process, continue exiting */
1950 wakeup_one( (caddr_t
) &entryp
->procp
->AIO_CLEANUP_SLEEP_CHAN
);
1952 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_cleanup_wake
)) | DBG_FUNC_NONE
,
1953 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1960 * aio_suspend case when a signal was not requested. In that scenario we
1961 * are sleeping on the AIO_SUSPEND_SLEEP_CHAN channel.
1962 * NOTE - the assumption here is that this wakeup call is inexpensive.
1963 * we really only need to do this when an aio_suspend call is pending.
1964 * If we find the wakeup call should be avoided we could mark the
1965 * async IO requests given in the list provided by aio_suspend and only
1966 * call wakeup for them. If we do mark them we should unmark them after
1967 * the aio_suspend wakes up.
1970 wakeup_one( (caddr_t
) &entryp
->procp
->AIO_SUSPEND_SLEEP_CHAN
);
1973 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_suspend_wake
)) | DBG_FUNC_NONE
,
1974 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1978 } /* do_aio_completion */
1982 * aio_last_group_io - checks to see if this is the last unfinished IO request
1983 * for the given group_tag. Returns TRUE if there are no other active IO
1984 * requests for this group or FALSE if the are active IO requests
1985 * NOTE - AIO_LOCK must be held by caller
1989 aio_last_group_io( aio_workq_entry
*entryp
)
1991 aio_workq_entry
*my_entryp
;
1993 /* look for matches on our queue of active async IO requests */
1994 TAILQ_FOREACH( my_entryp
, &entryp
->procp
->aio_activeq
, aio_workq_link
) {
1995 if ( my_entryp
->group_tag
== entryp
->group_tag
)
1999 /* look for matches on our queue of asynchronous todo work */
2000 TAILQ_FOREACH( my_entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
2001 if ( my_entryp
->group_tag
== entryp
->group_tag
)
2005 /* look for matches on our queue of synchronous todo work */
2006 TAILQ_FOREACH( my_entryp
, &aio_anchor
.lio_sync_workq
, aio_workq_link
) {
2007 if ( my_entryp
->group_tag
== entryp
->group_tag
)
2013 } /* aio_last_group_io */
2020 do_aio_read( aio_workq_entry
*entryp
)
2022 struct fileproc
*fp
;
2025 if ( (error
= fp_lookup(entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, 0)) )
2027 if ( (fp
->f_fglob
->fg_flag
& FREAD
) == 0 ) {
2028 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2032 error
= dofileread( entryp
->procp
, fp
, entryp
->aiocb
.aio_fildes
,
2033 entryp
->aiocb
.aio_buf
,
2034 entryp
->aiocb
.aio_nbytes
,
2035 entryp
->aiocb
.aio_offset
, FOF_OFFSET
,
2036 &entryp
->returnval
);
2037 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2040 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2053 do_aio_write( aio_workq_entry
*entryp
)
2055 struct fileproc
*fp
;
2058 if ( (error
= fp_lookup(entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, 0)) )
2060 if ( (fp
->f_fglob
->fg_flag
& FWRITE
) == 0 ) {
2061 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2065 error
= dofilewrite( entryp
->procp
, fp
, entryp
->aiocb
.aio_fildes
,
2066 entryp
->aiocb
.aio_buf
,
2067 entryp
->aiocb
.aio_nbytes
,
2068 entryp
->aiocb
.aio_offset
, FOF_OFFSET
,
2069 &entryp
->returnval
);
2071 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2074 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2080 } /* do_aio_write */
2084 * aio_active_requests_for_process - return number of active async IO
2085 * requests for the given process.
2086 * NOTE - caller must hold aio lock!
2090 aio_active_requests_for_process( struct proc
*procp
)
2093 return( procp
->aio_active_count
);
2095 } /* aio_active_requests_for_process */
2102 do_aio_fsync( aio_workq_entry
*entryp
)
2104 struct vfs_context context
;
2106 struct fileproc
*fp
;
2110 * NOTE - we will not support AIO_DSYNC until fdatasync() is supported.
2111 * AIO_DSYNC is caught before we queue up a request and flagged as an error.
2112 * The following was shamelessly extracted from fsync() implementation.
2115 error
= fp_getfvp( entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, &vp
);
2117 if ( (error
= vnode_getwithref(vp
)) ) {
2118 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2119 entryp
->returnval
= -1;
2122 context
.vc_proc
= entryp
->procp
;
2123 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
2125 error
= VNOP_FSYNC( vp
, MNT_WAIT
, &context
);
2127 (void)vnode_put(vp
);
2129 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2132 entryp
->returnval
= -1;
2136 } /* do_aio_fsync */
2140 * is_already_queued - runs through our queues to see if the given
2141 * aiocbp / process is there. Returns TRUE if there is a match
2142 * on any of our aio queues.
2143 * NOTE - callers must hold aio lock!
2147 is_already_queued( struct proc
*procp
,
2148 user_addr_t aiocbp
)
2150 aio_workq_entry
*entryp
;
2155 /* look for matches on our queue of async IO requests that have completed */
2156 TAILQ_FOREACH( entryp
, &procp
->aio_doneq
, aio_workq_link
) {
2157 if ( aiocbp
== entryp
->uaiocbp
) {
2159 goto ExitThisRoutine
;
2163 /* look for matches on our queue of active async IO requests */
2164 TAILQ_FOREACH( entryp
, &procp
->aio_activeq
, aio_workq_link
) {
2165 if ( aiocbp
== entryp
->uaiocbp
) {
2167 goto ExitThisRoutine
;
2171 /* look for matches on our queue of asynchronous todo work */
2172 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
2173 if ( procp
== entryp
->procp
&& aiocbp
== entryp
->uaiocbp
) {
2175 goto ExitThisRoutine
;
2179 /* look for matches on our queue of synchronous todo work */
2180 TAILQ_FOREACH( entryp
, &aio_anchor
.lio_sync_workq
, aio_workq_link
) {
2181 if ( procp
== entryp
->procp
&& aiocbp
== entryp
->uaiocbp
) {
2183 goto ExitThisRoutine
;
2190 } /* is_already_queued */
2194 * aio initialization
2196 __private_extern__
void
2201 aio_lock_grp_attr
= lck_grp_attr_alloc_init();
2202 lck_grp_attr_setstat(aio_lock_grp_attr
);
2203 aio_lock_grp
= lck_grp_alloc_init("aio", aio_lock_grp_attr
);
2204 aio_lock_attr
= lck_attr_alloc_init();
2205 //lck_attr_setdebug(aio_lock_attr);
2207 aio_lock
= lck_mtx_alloc_init(aio_lock_grp
, aio_lock_attr
);
2210 TAILQ_INIT( &aio_anchor
.aio_async_workq
);
2211 TAILQ_INIT( &aio_anchor
.lio_sync_workq
);
2212 aio_anchor
.aio_async_workq_count
= 0;
2213 aio_anchor
.lio_sync_workq_count
= 0;
2214 aio_anchor
.aio_active_count
= 0;
2215 aio_anchor
.aio_done_count
= 0;
2218 i
= sizeof( aio_workq_entry
);
2219 aio_workq_zonep
= zinit( i
, i
* aio_max_requests
, i
* aio_max_requests
, "aiowq" );
2221 _aio_create_worker_threads( aio_worker_threads
);
2229 * aio worker threads created here.
2231 __private_extern__
void
2232 _aio_create_worker_threads( int num
)
2236 /* create some worker threads to handle the async IO requests */
2237 for ( i
= 0; i
< num
; i
++ ) {
2240 myThread
= kernel_thread( kernel_task
, aio_work_thread
);
2241 if ( THREAD_NULL
== myThread
) {
2242 printf( "%s - failed to create a work thread \n", __FUNCTION__
);
2248 } /* _aio_create_worker_threads */
2251 * Return the current activation utask
2256 return ((struct uthread
*)get_bsdthread_info(current_thread()))->uu_aio_task
;
2261 * In the case of an aiocb from a
2262 * 32-bit process we need to expand some longs and pointers to the correct
2263 * sizes in order to let downstream code always work on the same type of
2264 * aiocb (in our case that is a user_aiocb)
2267 do_munge_aiocb( struct aiocb
*my_aiocbp
, struct user_aiocb
*the_user_aiocbp
)
2269 the_user_aiocbp
->aio_fildes
= my_aiocbp
->aio_fildes
;
2270 the_user_aiocbp
->aio_offset
= my_aiocbp
->aio_offset
;
2271 the_user_aiocbp
->aio_buf
= CAST_USER_ADDR_T(my_aiocbp
->aio_buf
);
2272 the_user_aiocbp
->aio_nbytes
= my_aiocbp
->aio_nbytes
;
2273 the_user_aiocbp
->aio_reqprio
= my_aiocbp
->aio_reqprio
;
2274 the_user_aiocbp
->aio_lio_opcode
= my_aiocbp
->aio_lio_opcode
;
2276 /* special case here. since we do not know if sigev_value is an */
2277 /* int or a ptr we do NOT cast the ptr to a user_addr_t. This */
2278 /* means if we send this info back to user space we need to remember */
2279 /* sigev_value was not expanded for the 32-bit case. */
2280 /* NOTE - this does NOT affect us since we don't support sigev_value */
2281 /* yet in the aio context. */
2283 the_user_aiocbp
->aio_sigevent
.sigev_notify
= my_aiocbp
->aio_sigevent
.sigev_notify
;
2284 the_user_aiocbp
->aio_sigevent
.sigev_signo
= my_aiocbp
->aio_sigevent
.sigev_signo
;
2285 the_user_aiocbp
->aio_sigevent
.sigev_value
.size_equivalent
.sival_int
=
2286 my_aiocbp
->aio_sigevent
.sigev_value
.sival_int
;
2287 the_user_aiocbp
->aio_sigevent
.sigev_notify_function
=
2288 CAST_USER_ADDR_T(my_aiocbp
->aio_sigevent
.sigev_notify_function
);
2289 the_user_aiocbp
->aio_sigevent
.sigev_notify_attributes
=
2290 CAST_USER_ADDR_T(my_aiocbp
->aio_sigevent
.sigev_notify_attributes
);