2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * 1) ramesh is looking into how to replace taking a reference on
33 * the user's map (vm_map_reference()) since it is believed that
34 * would not hold the process for us.
35 * 2) david is looking into a way for us to set the priority of the
36 * worker threads to match that of the user's thread when the
37 * async IO was queued.
42 * This file contains support for the POSIX 1003.1B AIO/LIO facility.
45 #include <sys/systm.h>
46 #include <sys/fcntl.h>
47 #include <sys/file_internal.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode_internal.h>
51 #include <sys/malloc.h>
52 #include <sys/mount_internal.h>
53 #include <sys/param.h>
54 #include <sys/proc_internal.h>
55 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
59 #include <sys/aio_kern.h>
60 #include <sys/sysproto.h>
62 #include <machine/limits.h>
64 #include <mach/mach_types.h>
65 #include <kern/kern_types.h>
66 #include <kern/zalloc.h>
67 #include <kern/task.h>
68 #include <kern/sched_prim.h>
70 #include <vm/vm_map.h>
72 #include <sys/kdebug.h>
73 #define AIO_work_queued 1
74 #define AIO_worker_wake 2
75 #define AIO_completion_sig 3
76 #define AIO_completion_cleanup_wait 4
77 #define AIO_completion_cleanup_wake 5
78 #define AIO_completion_suspend_wake 6
79 #define AIO_fsync_delay 7
81 #define AIO_cancel_async_workq 11
82 #define AIO_cancel_sync_workq 12
83 #define AIO_cancel_activeq 13
84 #define AIO_cancel_doneq 14
90 #define AIO_error_val 61
91 #define AIO_error_activeq 62
92 #define AIO_error_workq 63
94 #define AIO_return_val 71
95 #define AIO_return_activeq 72
96 #define AIO_return_workq 73
99 #define AIO_exit_sleep 91
100 #define AIO_close 100
101 #define AIO_close_sleep 101
102 #define AIO_suspend 110
103 #define AIO_suspend_sleep 111
104 #define AIO_worker_thread 120
108 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
112 * aio requests queue up on the aio_async_workq or lio_sync_workq (for
113 * lio_listio LIO_WAIT). Requests then move to the per process aio_activeq
114 * (proc.aio_activeq) when one of our worker threads start the IO.
115 * And finally, requests move to the per process aio_doneq (proc.aio_doneq)
116 * when the IO request completes. The request remains on aio_doneq until
117 * user process calls aio_return or the process exits, either way that is our
118 * trigger to release aio resources.
122 int aio_async_workq_count
; /* entries on aio_async_workq */
123 int lio_sync_workq_count
; /* entries on lio_sync_workq */
124 int aio_active_count
; /* entries on all active queues (proc.aio_activeq) */
125 int aio_done_count
; /* entries on all done queues (proc.aio_doneq) */
126 TAILQ_HEAD( , aio_workq_entry
) aio_async_workq
;
127 TAILQ_HEAD( , aio_workq_entry
) lio_sync_workq
;
129 typedef struct aio_anchor_cb aio_anchor_cb
;
133 * Notes on aio sleep / wake channels.
134 * We currently pick a couple fields within the proc structure that will allow
135 * us sleep channels that currently do not collide with any other kernel routines.
136 * At this time, for binary compatibility reasons, we cannot create new proc fields.
138 #define AIO_SUSPEND_SLEEP_CHAN p_estcpu
139 #define AIO_CLEANUP_SLEEP_CHAN p_pctcpu
143 * aysnc IO locking macros used to protect critical sections.
145 #define AIO_LOCK lck_mtx_lock(aio_lock)
146 #define AIO_UNLOCK lck_mtx_unlock(aio_lock)
152 static int aio_active_requests_for_process( struct proc
*procp
);
153 static boolean_t
aio_delay_fsync_request( aio_workq_entry
*entryp
);
154 static int aio_free_request( aio_workq_entry
*entryp
, vm_map_t the_map
);
155 static int aio_get_all_queues_count( void );
156 static int aio_get_process_count( struct proc
*procp
);
157 static aio_workq_entry
* aio_get_some_work( void );
158 static boolean_t
aio_last_group_io( aio_workq_entry
*entryp
);
159 static void aio_mark_requests( aio_workq_entry
*entryp
);
160 static int aio_queue_async_request( struct proc
*procp
,
163 static int aio_validate( aio_workq_entry
*entryp
);
164 static void aio_work_thread( void );
165 static int do_aio_cancel( struct proc
*p
,
168 boolean_t wait_for_completion
,
169 boolean_t disable_notification
);
170 static void do_aio_completion( aio_workq_entry
*entryp
);
171 static int do_aio_fsync( aio_workq_entry
*entryp
);
172 static int do_aio_read( aio_workq_entry
*entryp
);
173 static int do_aio_write( aio_workq_entry
*entryp
);
174 static void do_munge_aiocb( struct aiocb
*my_aiocbp
, struct user_aiocb
*the_user_aiocbp
);
175 static boolean_t
is_already_queued( struct proc
*procp
,
176 user_addr_t aiocbp
);
177 static int lio_create_async_entry( struct proc
*procp
,
181 aio_workq_entry
**entrypp
);
182 static int lio_create_sync_entry( struct proc
*procp
,
185 aio_workq_entry
**entrypp
);
189 * EXTERNAL PROTOTYPES
192 /* in ...bsd/kern/sys_generic.c */
193 extern int dofileread( struct proc
*p
, struct fileproc
*fp
, int fd
,
194 user_addr_t bufp
, user_size_t nbyte
,
195 off_t offset
, int flags
, user_ssize_t
*retval
);
196 extern int dofilewrite( struct proc
*p
, struct fileproc
*fp
, int fd
,
197 user_addr_t bufp
, user_size_t nbyte
, off_t offset
,
198 int flags
, user_ssize_t
*retval
);
201 * aio external global variables.
203 extern int aio_max_requests
; /* AIO_MAX - configurable */
204 extern int aio_max_requests_per_process
; /* AIO_PROCESS_MAX - configurable */
205 extern int aio_worker_threads
; /* AIO_THREAD_COUNT - configurable */
209 * aio static variables.
211 static aio_anchor_cb aio_anchor
;
212 static lck_mtx_t
* aio_lock
;
213 static lck_grp_t
* aio_lock_grp
;
214 static lck_attr_t
* aio_lock_attr
;
215 static lck_grp_attr_t
* aio_lock_grp_attr
;
216 static struct zone
*aio_workq_zonep
;
222 * aio_cancel - attempt to cancel one or more async IO requests currently
223 * outstanding against file descriptor uap->fd. If uap->aiocbp is not
224 * NULL then only one specific IO is cancelled (if possible). If uap->aiocbp
225 * is NULL then all outstanding async IO request for the given file
226 * descriptor are cancelled (if possible).
230 aio_cancel( struct proc
*p
, struct aio_cancel_args
*uap
, int *retval
)
232 struct user_aiocb my_aiocb
;
235 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel
)) | DBG_FUNC_START
,
236 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
238 /* quick check to see if there are any async IO requests queued up */
240 result
= aio_get_all_queues_count( );
248 if ( uap
->aiocbp
!= USER_ADDR_NULL
) {
249 if ( !IS_64BIT_PROCESS(p
) ) {
250 struct aiocb aiocb32
;
252 result
= copyin( uap
->aiocbp
, &aiocb32
, sizeof(aiocb32
) );
254 do_munge_aiocb( &aiocb32
, &my_aiocb
);
256 result
= copyin( uap
->aiocbp
, &my_aiocb
, sizeof(my_aiocb
) );
263 /* NOTE - POSIX standard says a mismatch between the file */
264 /* descriptor passed in and the file descriptor embedded in */
265 /* the aiocb causes unspecified results. We return EBADF in */
266 /* that situation. */
267 if ( uap
->fd
!= my_aiocb
.aio_fildes
) {
272 result
= do_aio_cancel( p
, uap
->fd
, uap
->aiocbp
, FALSE
, FALSE
);
274 if ( result
!= -1 ) {
283 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel
)) | DBG_FUNC_END
,
284 (int)p
, (int)uap
->aiocbp
, result
, 0, 0 );
292 * _aio_close - internal function used to clean up async IO requests for
293 * a file descriptor that is closing.
297 __private_extern__
void
298 _aio_close( struct proc
*p
, int fd
)
302 /* quick check to see if there are any async IO requests queued up */
304 count
= aio_get_all_queues_count( );
309 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_close
)) | DBG_FUNC_START
,
310 (int)p
, fd
, 0, 0, 0 );
312 /* cancel all async IO requests on our todo queues for this file descriptor */
313 error
= do_aio_cancel( p
, fd
, 0, TRUE
, FALSE
);
314 if ( error
== AIO_NOTCANCELED
) {
316 * AIO_NOTCANCELED is returned when we find an aio request for this process
317 * and file descriptor on the active async IO queue. Active requests cannot
318 * be cancelled so we must wait for them to complete. We will get a special
319 * wake up call on our channel used to sleep for ALL active requests to
320 * complete. This sleep channel (proc.AIO_CLEANUP_SLEEP_CHAN) is only used
321 * when we must wait for all active aio requests.
324 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_close_sleep
)) | DBG_FUNC_NONE
,
325 (int)p
, fd
, 0, 0, 0 );
327 tsleep( &p
->AIO_CLEANUP_SLEEP_CHAN
, PRIBIO
, "aio_close", 0 );
330 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_close
)) | DBG_FUNC_END
,
331 (int)p
, fd
, 0, 0, 0 );
339 * aio_error - return the error status associated with the async IO
340 * request referred to by uap->aiocbp. The error status is the errno
341 * value that would be set by the corresponding IO request (read, wrtie,
342 * fdatasync, or sync).
346 aio_error( struct proc
*p
, struct aio_error_args
*uap
, int *retval
)
348 aio_workq_entry
*entryp
;
351 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error
)) | DBG_FUNC_START
,
352 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
356 /* quick check to see if there are any async IO requests queued up */
357 if ( aio_get_all_queues_count( ) < 1 ) {
362 /* look for a match on our queue of async IO requests that have completed */
363 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
364 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
365 *retval
= entryp
->errorval
;
367 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error_val
)) | DBG_FUNC_NONE
,
368 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
373 /* look for a match on our queue of active async IO requests */
374 TAILQ_FOREACH( entryp
, &p
->aio_activeq
, aio_workq_link
) {
375 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
376 *retval
= EINPROGRESS
;
378 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error_activeq
)) | DBG_FUNC_NONE
,
379 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
384 /* look for a match on our queue of todo work */
385 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
386 if ( p
== entryp
->procp
&& entryp
->uaiocbp
== uap
->aiocbp
) {
387 *retval
= EINPROGRESS
;
389 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error_workq
)) | DBG_FUNC_NONE
,
390 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
397 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_error
)) | DBG_FUNC_END
,
398 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
407 * aio_fsync - asynchronously force all IO operations associated
408 * with the file indicated by the file descriptor (uap->aiocbp->aio_fildes) and
409 * queued at the time of the call to the synchronized completion state.
410 * NOTE - we do not support op O_DSYNC at this point since we do not support the
415 aio_fsync( struct proc
*p
, struct aio_fsync_args
*uap
, int *retval
)
420 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_fsync
)) | DBG_FUNC_START
,
421 (int)p
, (int)uap
->aiocbp
, uap
->op
, 0, 0 );
424 /* 0 := O_SYNC for binary backward compatibility with Panther */
425 if (uap
->op
== O_SYNC
|| uap
->op
== 0)
426 fsync_kind
= AIO_FSYNC
;
427 #if 0 // we don't support fdatasync() call yet
428 else if ( uap
->op
== O_DSYNC
)
429 fsync_kind
= AIO_DSYNC
;
437 error
= aio_queue_async_request( p
, uap
->aiocbp
, fsync_kind
);
442 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_fsync
)) | DBG_FUNC_END
,
443 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
450 /* aio_read - asynchronously read uap->aiocbp->aio_nbytes bytes from the
451 * file descriptor (uap->aiocbp->aio_fildes) into the buffer
452 * (uap->aiocbp->aio_buf).
456 aio_read( struct proc
*p
, struct aio_read_args
*uap
, int *retval
)
460 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_read
)) | DBG_FUNC_START
,
461 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
465 error
= aio_queue_async_request( p
, uap
->aiocbp
, AIO_READ
);
469 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_read
)) | DBG_FUNC_END
,
470 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
478 * aio_return - return the return status associated with the async IO
479 * request referred to by uap->aiocbp. The return status is the value
480 * that would be returned by corresponding IO request (read, wrtie,
481 * fdatasync, or sync). This is where we release kernel resources
482 * held for async IO call associated with the given aiocb pointer.
486 aio_return( struct proc
*p
, struct aio_return_args
*uap
, user_ssize_t
*retval
)
488 aio_workq_entry
*entryp
;
492 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return
)) | DBG_FUNC_START
,
493 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
499 /* quick check to see if there are any async IO requests queued up */
500 if ( aio_get_all_queues_count( ) < 1 ) {
505 /* look for a match on our queue of async IO requests that have completed */
506 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
507 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
508 TAILQ_REMOVE( &p
->aio_doneq
, entryp
, aio_workq_link
);
509 aio_anchor
.aio_done_count
--;
512 *retval
= entryp
->returnval
;
514 /* we cannot free requests that are still completing */
515 if ( (entryp
->flags
& AIO_COMPLETION
) == 0 ) {
518 my_map
= entryp
->aio_map
;
519 entryp
->aio_map
= VM_MAP_NULL
;
522 aio_free_request( entryp
, my_map
);
525 /* tell completion code to free this request */
526 entryp
->flags
|= AIO_DO_FREE
;
528 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return_val
)) | DBG_FUNC_NONE
,
529 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
534 /* look for a match on our queue of active async IO requests */
535 TAILQ_FOREACH( entryp
, &p
->aio_activeq
, aio_workq_link
) {
536 if ( entryp
->uaiocbp
== uap
->aiocbp
) {
538 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return_activeq
)) | DBG_FUNC_NONE
,
539 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
544 /* look for a match on our queue of todo work */
545 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
546 if ( p
== entryp
->procp
&& entryp
->uaiocbp
== uap
->aiocbp
) {
548 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return_workq
)) | DBG_FUNC_NONE
,
549 (int)p
, (int)uap
->aiocbp
, *retval
, 0, 0 );
558 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_return
)) | DBG_FUNC_END
,
559 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
567 * _aio_exec - internal function used to clean up async IO requests for
568 * a process that is going away due to exec(). We cancel any async IOs
569 * we can and wait for those already active. We also disable signaling
570 * for cancelled or active aio requests that complete.
571 * This routine MAY block!
574 __private_extern__
void
575 _aio_exec( struct proc
*p
)
578 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exec
)) | DBG_FUNC_START
,
579 (int)p
, 0, 0, 0, 0 );
583 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exec
)) | DBG_FUNC_END
,
584 (int)p
, 0, 0, 0, 0 );
592 * _aio_exit - internal function used to clean up async IO requests for
593 * a process that is terminating (via exit() or exec() ). We cancel any async IOs
594 * we can and wait for those already active. We also disable signaling
595 * for cancelled or active aio requests that complete. This routine MAY block!
598 __private_extern__
void
599 _aio_exit( struct proc
*p
)
602 aio_workq_entry
*entryp
;
604 /* quick check to see if there are any async IO requests queued up */
606 count
= aio_get_all_queues_count( );
612 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exit
)) | DBG_FUNC_START
,
613 (int)p
, 0, 0, 0, 0 );
616 * cancel async IO requests on the todo work queue and wait for those
617 * already active to complete.
619 error
= do_aio_cancel( p
, 0, 0, TRUE
, TRUE
);
620 if ( error
== AIO_NOTCANCELED
) {
622 * AIO_NOTCANCELED is returned when we find an aio request for this process
623 * on the active async IO queue. Active requests cannot be cancelled so we
624 * must wait for them to complete. We will get a special wake up call on
625 * our channel used to sleep for ALL active requests to complete. This sleep
626 * channel (proc.AIO_CLEANUP_SLEEP_CHAN) is only used when we must wait for all
627 * active aio requests.
630 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exit_sleep
)) | DBG_FUNC_NONE
,
631 (int)p
, 0, 0, 0, 0 );
633 tsleep( &p
->AIO_CLEANUP_SLEEP_CHAN
, PRIBIO
, "aio_exit", 0 );
636 /* release all aio resources used by this process */
638 entryp
= TAILQ_FIRST( &p
->aio_doneq
);
639 while ( entryp
!= NULL
) {
640 aio_workq_entry
*next_entryp
;
642 next_entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
643 TAILQ_REMOVE( &p
->aio_doneq
, entryp
, aio_workq_link
);
644 aio_anchor
.aio_done_count
--;
647 /* we cannot free requests that are still completing */
648 if ( (entryp
->flags
& AIO_COMPLETION
) == 0 ) {
651 my_map
= entryp
->aio_map
;
652 entryp
->aio_map
= VM_MAP_NULL
;
654 aio_free_request( entryp
, my_map
);
656 /* need to start over since aio_doneq may have been */
657 /* changed while we were away. */
659 entryp
= TAILQ_FIRST( &p
->aio_doneq
);
663 /* tell completion code to free this request */
664 entryp
->flags
|= AIO_DO_FREE
;
665 entryp
= next_entryp
;
669 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_exit
)) | DBG_FUNC_END
,
670 (int)p
, 0, 0, 0, 0 );
678 * do_aio_cancel - cancel async IO requests (if possible). We get called by
679 * aio_cancel, close, and at exit.
680 * There are three modes of operation: 1) cancel all async IOs for a process -
681 * fd is 0 and aiocbp is NULL 2) cancel all async IOs for file descriptor - fd
682 * is > 0 and aiocbp is NULL 3) cancel one async IO associated with the given
684 * Returns -1 if no matches were found, AIO_CANCELED when we cancelled all
685 * target async IO requests, AIO_NOTCANCELED if we could not cancel all
686 * target async IO requests, and AIO_ALLDONE if all target async IO requests
687 * were already complete.
688 * WARNING - do not deference aiocbp in this routine, it may point to user
689 * land data that has not been copied in (when called from aio_cancel() )
693 do_aio_cancel( struct proc
*p
, int fd
, user_addr_t aiocbp
,
694 boolean_t wait_for_completion
, boolean_t disable_notification
)
696 aio_workq_entry
*entryp
;
701 /* look for a match on our queue of async todo work. */
703 entryp
= TAILQ_FIRST( &aio_anchor
.aio_async_workq
);
704 while ( entryp
!= NULL
) {
705 aio_workq_entry
*next_entryp
;
707 next_entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
708 if ( p
== entryp
->procp
) {
709 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
710 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
711 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
712 /* we found a match so we remove the entry from the */
713 /* todo work queue and place it on the done queue */
714 TAILQ_REMOVE( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
715 aio_anchor
.aio_async_workq_count
--;
716 entryp
->errorval
= ECANCELED
;
717 entryp
->returnval
= -1;
718 if ( disable_notification
)
719 entryp
->flags
|= AIO_DISABLE
; /* flag for special completion processing */
720 result
= AIO_CANCELED
;
722 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_async_workq
)) | DBG_FUNC_NONE
,
723 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
725 TAILQ_INSERT_TAIL( &p
->aio_doneq
, entryp
, aio_workq_link
);
726 aio_anchor
.aio_done_count
++;
728 entryp
->flags
|= AIO_COMPLETION
;
731 /* do completion processing for this request */
732 do_aio_completion( entryp
);
735 entryp
->flags
&= ~AIO_COMPLETION
;
736 if ( (entryp
->flags
& AIO_DO_FREE
) != 0 ) {
739 my_map
= entryp
->aio_map
;
740 entryp
->aio_map
= VM_MAP_NULL
;
742 aio_free_request( entryp
, my_map
);
747 if ( aiocbp
!= USER_ADDR_NULL
) {
751 /* need to start over since aio_async_workq may have been */
752 /* changed while we were away doing completion processing. */
754 entryp
= TAILQ_FIRST( &aio_anchor
.aio_async_workq
);
758 entryp
= next_entryp
;
762 * look for a match on our queue of synchronous todo work. This will
763 * be a rare occurrence but could happen if a process is terminated while
764 * processing a lio_listio call.
766 entryp
= TAILQ_FIRST( &aio_anchor
.lio_sync_workq
);
767 while ( entryp
!= NULL
) {
768 aio_workq_entry
*next_entryp
;
770 next_entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
771 if ( p
== entryp
->procp
) {
772 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
773 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
774 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
775 /* we found a match so we remove the entry from the */
776 /* todo work queue and place it on the done queue */
777 TAILQ_REMOVE( &aio_anchor
.lio_sync_workq
, entryp
, aio_workq_link
);
778 aio_anchor
.lio_sync_workq_count
--;
779 entryp
->errorval
= ECANCELED
;
780 entryp
->returnval
= -1;
781 if ( disable_notification
)
782 entryp
->flags
|= AIO_DISABLE
; /* flag for special completion processing */
783 result
= AIO_CANCELED
;
785 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_sync_workq
)) | DBG_FUNC_NONE
,
786 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
788 TAILQ_INSERT_TAIL( &p
->aio_doneq
, entryp
, aio_workq_link
);
789 aio_anchor
.aio_done_count
++;
791 if ( aiocbp
!= USER_ADDR_NULL
) {
797 entryp
= next_entryp
;
801 * look for a match on our queue of active async IO requests and
802 * return AIO_NOTCANCELED result.
804 TAILQ_FOREACH( entryp
, &p
->aio_activeq
, aio_workq_link
) {
805 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
806 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
807 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
808 result
= AIO_NOTCANCELED
;
810 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_activeq
)) | DBG_FUNC_NONE
,
811 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
813 if ( wait_for_completion
)
814 entryp
->flags
|= AIO_WAITING
; /* flag for special completion processing */
815 if ( disable_notification
)
816 entryp
->flags
|= AIO_DISABLE
; /* flag for special completion processing */
817 if ( aiocbp
!= USER_ADDR_NULL
) {
825 * if we didn't find any matches on the todo or active queues then look for a
826 * match on our queue of async IO requests that have completed and if found
827 * return AIO_ALLDONE result.
829 if ( result
== -1 ) {
830 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
831 if ( (aiocbp
== USER_ADDR_NULL
&& fd
== 0) ||
832 (aiocbp
!= USER_ADDR_NULL
&& entryp
->uaiocbp
== aiocbp
) ||
833 (aiocbp
== USER_ADDR_NULL
&& fd
== entryp
->aiocb
.aio_fildes
) ) {
834 result
= AIO_ALLDONE
;
836 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_cancel_doneq
)) | DBG_FUNC_NONE
,
837 (int)entryp
->procp
, (int)entryp
->uaiocbp
, fd
, 0, 0 );
839 if ( aiocbp
!= USER_ADDR_NULL
) {
850 } /* do_aio_cancel */
854 * aio_suspend - suspend the calling thread until at least one of the async
855 * IO operations referenced by uap->aiocblist has completed, until a signal
856 * interrupts the function, or uap->timeoutp time interval (optional) has
858 * Returns 0 if one or more async IOs have completed else -1 and errno is
859 * set appropriately - EAGAIN if timeout elapses or EINTR if an interrupt
864 aio_suspend( struct proc
*p
, struct aio_suspend_args
*uap
, int *retval
)
869 struct user_timespec ts
;
870 aio_workq_entry
*entryp
;
871 user_addr_t
*aiocbpp
;
873 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_suspend
)) | DBG_FUNC_START
,
874 (int)p
, uap
->nent
, 0, 0, 0 );
880 /* quick check to see if there are any async IO requests queued up */
882 count
= aio_get_all_queues_count( );
886 goto ExitThisRoutine
;
889 if ( uap
->nent
< 1 || uap
->nent
> aio_max_requests_per_process
) {
891 goto ExitThisRoutine
;
894 if ( uap
->timeoutp
!= USER_ADDR_NULL
) {
895 if ( proc_is64bit(p
) ) {
896 error
= copyin( uap
->timeoutp
, &ts
, sizeof(ts
) );
899 struct timespec temp
;
900 error
= copyin( uap
->timeoutp
, &temp
, sizeof(temp
) );
902 ts
.tv_sec
= temp
.tv_sec
;
903 ts
.tv_nsec
= temp
.tv_nsec
;
908 goto ExitThisRoutine
;
911 if ( ts
.tv_nsec
< 0 || ts
.tv_nsec
>= 1000000000 ) {
913 goto ExitThisRoutine
;
916 nanoseconds_to_absolutetime( (uint64_t)ts
.tv_sec
* NSEC_PER_SEC
+ ts
.tv_nsec
,
918 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
921 /* we reserve enough space for largest possible pointer size */
922 MALLOC( aiocbpp
, user_addr_t
*, (uap
->nent
* sizeof(user_addr_t
)), M_TEMP
, M_WAITOK
);
923 if ( aiocbpp
== NULL
) {
925 goto ExitThisRoutine
;
928 /* copyin our aiocb pointers from list */
929 error
= copyin( uap
->aiocblist
, aiocbpp
,
930 proc_is64bit(p
) ? (uap
->nent
* sizeof(user_addr_t
))
931 : (uap
->nent
* sizeof(uintptr_t)) );
934 goto ExitThisRoutine
;
937 /* we depend on a list of user_addr_t's so we need to munge and expand */
938 /* when these pointers came from a 32-bit process */
939 if ( !proc_is64bit(p
) && sizeof(uintptr_t) < sizeof(user_addr_t
) ) {
940 /* position to the last entry and work back from there */
941 uintptr_t *my_ptrp
= ((uintptr_t *)aiocbpp
) + (uap
->nent
- 1);
942 user_addr_t
*my_addrp
= aiocbpp
+ (uap
->nent
- 1);
943 for (i
= 0; i
< uap
->nent
; i
++, my_ptrp
--, my_addrp
--) {
944 *my_addrp
= (user_addr_t
) (*my_ptrp
);
948 /* check list of aio requests to see if any have completed */
950 for ( i
= 0; i
< uap
->nent
; i
++ ) {
953 /* NULL elements are legal so check for 'em */
954 aiocbp
= *(aiocbpp
+ i
);
955 if ( aiocbp
== USER_ADDR_NULL
)
958 /* return immediately if any aio request in the list is done */
959 TAILQ_FOREACH( entryp
, &p
->aio_doneq
, aio_workq_link
) {
960 if ( entryp
->uaiocbp
== aiocbp
) {
964 goto ExitThisRoutine
;
967 } /* for ( ; i < uap->nent; ) */
969 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_suspend_sleep
)) | DBG_FUNC_NONE
,
970 (int)p
, uap
->nent
, 0, 0, 0 );
973 * wait for an async IO to complete or a signal fires or timeout expires.
974 * we return EAGAIN (35) for timeout expiration and EINTR (4) when a signal
975 * interrupts us. If an async IO completes before a signal fires or our
976 * timeout expires, we get a wakeup call from aio_work_thread().
978 assert_wait_deadline( (event_t
) &p
->AIO_SUSPEND_SLEEP_CHAN
, THREAD_ABORTSAFE
, abstime
);
981 error
= thread_block( THREAD_CONTINUE_NULL
);
983 if ( error
== THREAD_AWAKENED
) {
984 /* got our wakeup call from aio_work_thread() */
988 else if ( error
== THREAD_TIMED_OUT
) {
989 /* our timeout expired */
993 /* we were interrupted */
998 if ( aiocbpp
!= NULL
)
999 FREE( aiocbpp
, M_TEMP
);
1001 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_suspend
)) | DBG_FUNC_END
,
1002 (int)p
, uap
->nent
, error
, 0, 0 );
1009 /* aio_write - asynchronously write uap->aiocbp->aio_nbytes bytes to the
1010 * file descriptor (uap->aiocbp->aio_fildes) from the buffer
1011 * (uap->aiocbp->aio_buf).
1015 aio_write( struct proc
*p
, struct aio_write_args
*uap
, int *retval
)
1021 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_write
)) | DBG_FUNC_START
,
1022 (int)p
, (int)uap
->aiocbp
, 0, 0, 0 );
1024 error
= aio_queue_async_request( p
, uap
->aiocbp
, AIO_WRITE
);
1028 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_write
)) | DBG_FUNC_END
,
1029 (int)p
, (int)uap
->aiocbp
, error
, 0, 0 );
1037 * lio_listio - initiate a list of IO requests. We process the list of aiocbs
1038 * either synchronously (mode == LIO_WAIT) or asynchronously (mode == LIO_NOWAIT).
1039 * The caller gets error and return status for each aiocb in the list via aio_error
1040 * and aio_return. We must keep completed requests until released by the
1045 lio_listio( struct proc
*p
, struct lio_listio_args
*uap
, int *retval
)
1051 aio_workq_entry
* *entryp_listp
;
1052 user_addr_t
*aiocbpp
;
1054 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_listio
)) | DBG_FUNC_START
,
1055 (int)p
, uap
->nent
, uap
->mode
, 0, 0 );
1057 entryp_listp
= NULL
;
1061 if ( !(uap
->mode
== LIO_NOWAIT
|| uap
->mode
== LIO_WAIT
) ) {
1062 call_result
= EINVAL
;
1066 if ( uap
->nent
< 1 || uap
->nent
> AIO_LISTIO_MAX
) {
1067 call_result
= EINVAL
;
1072 * we use group_tag to mark IO requests for delayed completion processing
1073 * which means we wait until all IO requests in the group have completed
1074 * before we either return to the caller when mode is LIO_WAIT or signal
1075 * user when mode is LIO_NOWAIT.
1077 group_tag
= random();
1080 * allocate a list of aio_workq_entry pointers that we will use to queue
1081 * up all our requests at once while holding our lock.
1083 MALLOC( entryp_listp
, void *, (uap
->nent
* sizeof(aio_workq_entry
*)), M_TEMP
, M_WAITOK
);
1084 if ( entryp_listp
== NULL
) {
1085 call_result
= EAGAIN
;
1089 /* we reserve enough space for largest possible pointer size */
1090 MALLOC( aiocbpp
, user_addr_t
*, (uap
->nent
* sizeof(user_addr_t
)), M_TEMP
, M_WAITOK
);
1091 if ( aiocbpp
== NULL
) {
1092 call_result
= EAGAIN
;
1096 /* copyin our aiocb pointers from list */
1097 result
= copyin( uap
->aiocblist
, aiocbpp
,
1098 IS_64BIT_PROCESS(p
) ? (uap
->nent
* sizeof(user_addr_t
))
1099 : (uap
->nent
* sizeof(uintptr_t)) );
1100 if ( result
!= 0 ) {
1101 call_result
= EAGAIN
;
1105 /* we depend on a list of user_addr_t's so we need to munge and expand */
1106 /* when these pointers came from a 32-bit process */
1107 if ( !IS_64BIT_PROCESS(p
) && sizeof(uintptr_t) < sizeof(user_addr_t
) ) {
1108 /* position to the last entry and work back from there */
1109 uintptr_t *my_ptrp
= ((uintptr_t *)aiocbpp
) + (uap
->nent
- 1);
1110 user_addr_t
*my_addrp
= aiocbpp
+ (uap
->nent
- 1);
1111 for (i
= 0; i
< uap
->nent
; i
++, my_ptrp
--, my_addrp
--) {
1112 *my_addrp
= (user_addr_t
) (*my_ptrp
);
1116 /* process list of aio requests */
1117 for ( i
= 0; i
< uap
->nent
; i
++ ) {
1118 user_addr_t my_aiocbp
;
1120 *(entryp_listp
+ i
) = NULL
;
1121 my_aiocbp
= *(aiocbpp
+ i
);
1123 /* NULL elements are legal so check for 'em */
1124 if ( my_aiocbp
== USER_ADDR_NULL
)
1127 if ( uap
->mode
== LIO_NOWAIT
)
1128 result
= lio_create_async_entry( p
, my_aiocbp
, uap
->sigp
,
1129 group_tag
, (entryp_listp
+ i
) );
1131 result
= lio_create_sync_entry( p
, my_aiocbp
, group_tag
,
1132 (entryp_listp
+ i
) );
1134 if ( result
!= 0 && call_result
== -1 )
1135 call_result
= result
;
1139 * we need to protect this section since we do not want any of these grouped
1140 * IO requests to begin until we have them all on the queue.
1143 for ( i
= 0; i
< uap
->nent
; i
++ ) {
1144 aio_workq_entry
*entryp
;
1146 /* NULL elements are legal so check for 'em */
1147 entryp
= *(entryp_listp
+ i
);
1148 if ( entryp
== NULL
)
1151 /* check our aio limits to throttle bad or rude user land behavior */
1152 if ( aio_get_all_queues_count( ) >= aio_max_requests
||
1153 aio_get_process_count( entryp
->procp
) >= aio_max_requests_per_process
||
1154 is_already_queued( entryp
->procp
, entryp
->uaiocbp
) == TRUE
) {
1157 my_map
= entryp
->aio_map
;
1158 entryp
->aio_map
= VM_MAP_NULL
;
1159 if ( call_result
== -1 )
1160 call_result
= EAGAIN
;
1162 aio_free_request( entryp
, my_map
);
1167 /* place the request on the appropriate queue */
1168 if ( uap
->mode
== LIO_NOWAIT
) {
1169 TAILQ_INSERT_TAIL( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
1170 aio_anchor
.aio_async_workq_count
++;
1172 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_work_queued
)) | DBG_FUNC_NONE
,
1173 (int)p
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1176 TAILQ_INSERT_TAIL( &aio_anchor
.lio_sync_workq
, entryp
, aio_workq_link
);
1177 aio_anchor
.lio_sync_workq_count
++;
1181 if ( uap
->mode
== LIO_NOWAIT
) {
1182 /* caller does not want to wait so we'll fire off a worker thread and return */
1183 wakeup_one( (caddr_t
) &aio_anchor
.aio_async_workq
);
1186 aio_workq_entry
*entryp
;
1190 * mode is LIO_WAIT - handle the IO requests now.
1192 entryp
= TAILQ_FIRST( &aio_anchor
.lio_sync_workq
);
1193 while ( entryp
!= NULL
) {
1194 if ( p
== entryp
->procp
&& group_tag
== entryp
->group_tag
) {
1196 TAILQ_REMOVE( &aio_anchor
.lio_sync_workq
, entryp
, aio_workq_link
);
1197 aio_anchor
.lio_sync_workq_count
--;
1200 if ( (entryp
->flags
& AIO_READ
) != 0 ) {
1201 error
= do_aio_read( entryp
);
1203 else if ( (entryp
->flags
& AIO_WRITE
) != 0 ) {
1204 error
= do_aio_write( entryp
);
1206 else if ( (entryp
->flags
& AIO_FSYNC
) != 0 ) {
1207 error
= do_aio_fsync( entryp
);
1210 printf( "%s - unknown aio request - flags 0x%02X \n",
1211 __FUNCTION__
, entryp
->flags
);
1214 entryp
->errorval
= error
;
1215 if ( error
!= 0 && call_result
== -1 )
1219 /* we're done with the IO request so move it on the done queue */
1220 TAILQ_INSERT_TAIL( &p
->aio_doneq
, entryp
, aio_workq_link
);
1221 aio_anchor
.aio_done_count
++;
1222 p
->aio_done_count
++;
1224 /* need to start over since lio_sync_workq may have been changed while we */
1225 /* were away doing the IO. */
1226 entryp
= TAILQ_FIRST( &aio_anchor
.lio_sync_workq
);
1228 } /* p == entryp->procp */
1230 entryp
= TAILQ_NEXT( entryp
, aio_workq_link
);
1231 } /* while ( entryp != NULL ) */
1232 } /* uap->mode == LIO_WAIT */
1235 /* call_result == -1 means we had no trouble queueing up requests */
1236 if ( call_result
== -1 ) {
1242 if ( entryp_listp
!= NULL
)
1243 FREE( entryp_listp
, M_TEMP
);
1244 if ( aiocbpp
!= NULL
)
1245 FREE( aiocbpp
, M_TEMP
);
1247 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_listio
)) | DBG_FUNC_END
,
1248 (int)p
, call_result
, 0, 0, 0 );
1250 return( call_result
);
1256 * aio worker thread. this is where all the real work gets done.
1257 * we get a wake up call on sleep channel &aio_anchor.aio_async_workq
1258 * after new work is queued up.
1262 aio_work_thread( void )
1264 aio_workq_entry
*entryp
;
1268 entryp
= aio_get_some_work();
1269 if ( entryp
== NULL
) {
1271 * aio worker threads wait for some work to get queued up
1272 * by aio_queue_async_request. Once some work gets queued
1273 * it will wake up one of these worker threads just before
1274 * returning to our caller in user land.
1276 assert_wait( (event_t
) &aio_anchor
.aio_async_workq
, THREAD_UNINT
);
1279 thread_block( (thread_continue_t
)aio_work_thread
);
1284 vm_map_t currentmap
;
1285 vm_map_t oldmap
= VM_MAP_NULL
;
1286 task_t oldaiotask
= TASK_NULL
;
1287 struct uthread
*uthreadp
= NULL
;
1291 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_worker_thread
)) | DBG_FUNC_START
,
1292 (int)entryp
->procp
, (int)entryp
->uaiocbp
, entryp
->flags
, 0, 0 );
1295 * Assume the target's address space identity for the duration
1298 currentmap
= get_task_map( (current_proc())->task
);
1299 if ( currentmap
!= entryp
->aio_map
) {
1300 uthreadp
= (struct uthread
*) get_bsdthread_info(current_thread());
1301 oldaiotask
= uthreadp
->uu_aio_task
;
1302 uthreadp
->uu_aio_task
= entryp
->procp
->task
;
1303 oldmap
= vm_map_switch( entryp
->aio_map
);
1306 if ( (entryp
->flags
& AIO_READ
) != 0 ) {
1307 error
= do_aio_read( entryp
);
1309 else if ( (entryp
->flags
& AIO_WRITE
) != 0 ) {
1310 error
= do_aio_write( entryp
);
1312 else if ( (entryp
->flags
& AIO_FSYNC
) != 0 ) {
1313 error
= do_aio_fsync( entryp
);
1316 printf( "%s - unknown aio request - flags 0x%02X \n",
1317 __FUNCTION__
, entryp
->flags
);
1320 entryp
->errorval
= error
;
1321 if ( currentmap
!= entryp
->aio_map
) {
1322 (void) vm_map_switch( oldmap
);
1323 uthreadp
->uu_aio_task
= oldaiotask
;
1326 /* we're done with the IO request so pop it off the active queue and */
1327 /* push it on the done queue */
1329 TAILQ_REMOVE( &entryp
->procp
->aio_activeq
, entryp
, aio_workq_link
);
1330 aio_anchor
.aio_active_count
--;
1331 entryp
->procp
->aio_active_count
--;
1332 TAILQ_INSERT_TAIL( &entryp
->procp
->aio_doneq
, entryp
, aio_workq_link
);
1333 aio_anchor
.aio_done_count
++;
1334 entryp
->procp
->aio_done_count
++;
1335 entryp
->flags
|= AIO_COMPLETION
;
1337 /* remove our reference to the user land map. */
1338 if ( VM_MAP_NULL
!= entryp
->aio_map
) {
1341 my_map
= entryp
->aio_map
;
1342 entryp
->aio_map
= VM_MAP_NULL
;
1343 AIO_UNLOCK
; /* must unlock before calling vm_map_deallocate() */
1344 vm_map_deallocate( my_map
);
1350 do_aio_completion( entryp
);
1352 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_worker_thread
)) | DBG_FUNC_END
,
1353 (int)entryp
->procp
, (int)entryp
->uaiocbp
, entryp
->errorval
,
1354 entryp
->returnval
, 0 );
1357 entryp
->flags
&= ~AIO_COMPLETION
;
1358 if ( (entryp
->flags
& AIO_DO_FREE
) != 0 ) {
1361 my_map
= entryp
->aio_map
;
1362 entryp
->aio_map
= VM_MAP_NULL
;
1364 aio_free_request( entryp
, my_map
);
1373 } /* aio_work_thread */
1377 * aio_get_some_work - get the next async IO request that is ready to be executed.
1378 * aio_fsync complicates matters a bit since we cannot do the fsync until all async
1379 * IO requests at the time the aio_fsync call came in have completed.
1380 * NOTE - AIO_LOCK must be held by caller
1383 static aio_workq_entry
*
1384 aio_get_some_work( void )
1386 aio_workq_entry
*entryp
;
1388 /* pop some work off the work queue and add to our active queue */
1389 for ( entryp
= TAILQ_FIRST( &aio_anchor
.aio_async_workq
);
1391 entryp
= TAILQ_NEXT( entryp
, aio_workq_link
) ) {
1393 if ( (entryp
->flags
& AIO_FSYNC
) != 0 ) {
1394 /* leave aio_fsync calls on the work queue if there are IO */
1395 /* requests on the active queue for the same file descriptor. */
1396 if ( aio_delay_fsync_request( entryp
) ) {
1398 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_fsync_delay
)) | DBG_FUNC_NONE
,
1399 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1406 if ( entryp
!= NULL
) {
1407 TAILQ_REMOVE( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
1408 aio_anchor
.aio_async_workq_count
--;
1409 TAILQ_INSERT_TAIL( &entryp
->procp
->aio_activeq
, entryp
, aio_workq_link
);
1410 aio_anchor
.aio_active_count
++;
1411 entryp
->procp
->aio_active_count
++;
1416 } /* aio_get_some_work */
1420 * aio_delay_fsync_request - look to see if this aio_fsync request should be delayed at
1421 * this time. Delay will happen when there are any active IOs for the same file
1422 * descriptor that were queued at time the aio_sync call was queued.
1423 * NOTE - AIO_LOCK must be held by caller
1426 aio_delay_fsync_request( aio_workq_entry
*entryp
)
1428 aio_workq_entry
*my_entryp
;
1430 TAILQ_FOREACH( my_entryp
, &entryp
->procp
->aio_activeq
, aio_workq_link
) {
1431 if ( my_entryp
->fsyncp
!= USER_ADDR_NULL
&&
1432 entryp
->uaiocbp
== my_entryp
->fsyncp
&&
1433 entryp
->aiocb
.aio_fildes
== my_entryp
->aiocb
.aio_fildes
) {
1440 } /* aio_delay_fsync_request */
1444 * aio_queue_async_request - queue up an async IO request on our work queue then
1445 * wake up one of our worker threads to do the actual work. We get a reference
1446 * to our caller's user land map in order to keep it around while we are
1447 * processing the request.
1451 aio_queue_async_request( struct proc
*procp
, user_addr_t aiocbp
, int kindOfIO
)
1453 aio_workq_entry
*entryp
;
1456 entryp
= (aio_workq_entry
*) zalloc( aio_workq_zonep
);
1457 if ( entryp
== NULL
) {
1461 bzero( entryp
, sizeof(*entryp
) );
1463 /* fill in the rest of the aio_workq_entry */
1464 entryp
->procp
= procp
;
1465 entryp
->uaiocbp
= aiocbp
;
1466 entryp
->flags
|= kindOfIO
;
1467 entryp
->aio_map
= VM_MAP_NULL
;
1469 if ( !IS_64BIT_PROCESS(procp
) ) {
1470 struct aiocb aiocb32
;
1472 result
= copyin( aiocbp
, &aiocb32
, sizeof(aiocb32
) );
1474 do_munge_aiocb( &aiocb32
, &entryp
->aiocb
);
1476 result
= copyin( aiocbp
, &entryp
->aiocb
, sizeof(entryp
->aiocb
) );
1478 if ( result
!= 0 ) {
1483 /* do some more validation on the aiocb and embedded file descriptor */
1484 result
= aio_validate( entryp
);
1488 /* get a reference to the user land map in order to keep it around */
1489 entryp
->aio_map
= get_task_map( procp
->task
);
1490 vm_map_reference( entryp
->aio_map
);
1494 if ( is_already_queued( entryp
->procp
, entryp
->uaiocbp
) == TRUE
) {
1500 /* check our aio limits to throttle bad or rude user land behavior */
1501 if ( aio_get_all_queues_count( ) >= aio_max_requests
||
1502 aio_get_process_count( procp
) >= aio_max_requests_per_process
) {
1509 * aio_fsync calls sync up all async IO requests queued at the time
1510 * the aio_fsync call was made. So we mark each currently queued async
1511 * IO with a matching file descriptor as must complete before we do the
1512 * fsync. We set the fsyncp field of each matching async IO
1513 * request with the aiocb pointer passed in on the aio_fsync call to
1514 * know which IOs must complete before we process the aio_fsync call.
1516 if ( (kindOfIO
& AIO_FSYNC
) != 0 )
1517 aio_mark_requests( entryp
);
1519 /* queue up on our aio asynchronous work queue */
1520 TAILQ_INSERT_TAIL( &aio_anchor
.aio_async_workq
, entryp
, aio_workq_link
);
1521 aio_anchor
.aio_async_workq_count
++;
1523 wakeup_one( (caddr_t
) &aio_anchor
.aio_async_workq
);
1526 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_work_queued
)) | DBG_FUNC_NONE
,
1527 (int)procp
, (int)aiocbp
, 0, 0, 0 );
1532 if ( entryp
!= NULL
) {
1533 /* this entry has not been queued up so no worries about unlocked */
1534 /* state and aio_map */
1535 aio_free_request( entryp
, entryp
->aio_map
);
1540 } /* aio_queue_async_request */
1544 * lio_create_async_entry - allocate an aio_workq_entry and fill it in.
1545 * If all goes well return 0 and pass the aio_workq_entry pointer back to
1546 * our caller. We get a reference to our caller's user land map in order to keep
1547 * it around while we are processing the request.
1548 * lio_listio calls behave differently at completion they do completion notification
1549 * when all async IO requests have completed. We use group_tag to tag IO requests
1550 * that behave in the delay notification manner.
1554 lio_create_async_entry( struct proc
*procp
, user_addr_t aiocbp
,
1555 user_addr_t sigp
, long group_tag
,
1556 aio_workq_entry
**entrypp
)
1558 aio_workq_entry
*entryp
;
1561 entryp
= (aio_workq_entry
*) zalloc( aio_workq_zonep
);
1562 if ( entryp
== NULL
) {
1566 bzero( entryp
, sizeof(*entryp
) );
1568 /* fill in the rest of the aio_workq_entry */
1569 entryp
->procp
= procp
;
1570 entryp
->uaiocbp
= aiocbp
;
1571 entryp
->flags
|= AIO_LIO
;
1572 entryp
->group_tag
= group_tag
;
1573 entryp
->aio_map
= VM_MAP_NULL
;
1575 if ( !IS_64BIT_PROCESS(procp
) ) {
1576 struct aiocb aiocb32
;
1578 result
= copyin( aiocbp
, &aiocb32
, sizeof(aiocb32
) );
1580 do_munge_aiocb( &aiocb32
, &entryp
->aiocb
);
1582 result
= copyin( aiocbp
, &entryp
->aiocb
, sizeof(entryp
->aiocb
) );
1584 if ( result
!= 0 ) {
1589 /* look for lio_listio LIO_NOP requests and ignore them. */
1590 /* Not really an error, but we need to free our aio_workq_entry. */
1591 if ( entryp
->aiocb
.aio_lio_opcode
== LIO_NOP
) {
1596 /* use sigevent passed in to lio_listio for each of our calls, but only */
1597 /* do completion notification after the last request completes. */
1598 if ( sigp
!= USER_ADDR_NULL
) {
1599 if ( !IS_64BIT_PROCESS(procp
) ) {
1600 struct sigevent sigevent32
;
1602 result
= copyin( sigp
, &sigevent32
, sizeof(sigevent32
) );
1603 if ( result
== 0 ) {
1604 /* also need to munge aio_sigevent since it contains pointers */
1605 /* special case here. since we do not know if sigev_value is an */
1606 /* int or a ptr we do NOT cast the ptr to a user_addr_t. This */
1607 /* means if we send this info back to user space we need to remember */
1608 /* sigev_value was not expanded for the 32-bit case. */
1609 /* NOTE - this does NOT affect us since we don't support sigev_value */
1610 /* yet in the aio context. */
1612 entryp
->aiocb
.aio_sigevent
.sigev_notify
= sigevent32
.sigev_notify
;
1613 entryp
->aiocb
.aio_sigevent
.sigev_signo
= sigevent32
.sigev_signo
;
1614 entryp
->aiocb
.aio_sigevent
.sigev_value
.size_equivalent
.sival_int
=
1615 sigevent32
.sigev_value
.sival_int
;
1616 entryp
->aiocb
.aio_sigevent
.sigev_notify_function
=
1617 CAST_USER_ADDR_T(sigevent32
.sigev_notify_function
);
1618 entryp
->aiocb
.aio_sigevent
.sigev_notify_attributes
=
1619 CAST_USER_ADDR_T(sigevent32
.sigev_notify_attributes
);
1622 result
= copyin( sigp
, &entryp
->aiocb
.aio_sigevent
, sizeof(entryp
->aiocb
.aio_sigevent
) );
1624 if ( result
!= 0 ) {
1630 /* do some more validation on the aiocb and embedded file descriptor */
1631 result
= aio_validate( entryp
);
1635 /* get a reference to the user land map in order to keep it around */
1636 entryp
->aio_map
= get_task_map( procp
->task
);
1637 vm_map_reference( entryp
->aio_map
);
1643 if ( entryp
!= NULL
)
1644 zfree( aio_workq_zonep
, entryp
);
1648 } /* lio_create_async_entry */
1652 * aio_mark_requests - aio_fsync calls synchronize file data for all queued async IO
1653 * requests at the moment the aio_fsync call is queued. We use aio_workq_entry.fsyncp
1654 * to mark each async IO that must complete before the fsync is done. We use the uaiocbp
1655 * field from the aio_fsync call as the aio_workq_entry.fsyncp in marked requests.
1656 * NOTE - AIO_LOCK must be held by caller
1660 aio_mark_requests( aio_workq_entry
*entryp
)
1662 aio_workq_entry
*my_entryp
;
1664 TAILQ_FOREACH( my_entryp
, &entryp
->procp
->aio_activeq
, aio_workq_link
) {
1665 if ( entryp
->aiocb
.aio_fildes
== my_entryp
->aiocb
.aio_fildes
) {
1666 my_entryp
->fsyncp
= entryp
->uaiocbp
;
1670 TAILQ_FOREACH( my_entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
1671 if ( entryp
->procp
== my_entryp
->procp
&&
1672 entryp
->aiocb
.aio_fildes
== my_entryp
->aiocb
.aio_fildes
) {
1673 my_entryp
->fsyncp
= entryp
->uaiocbp
;
1677 } /* aio_mark_requests */
1681 * lio_create_sync_entry - allocate an aio_workq_entry and fill it in.
1682 * If all goes well return 0 and pass the aio_workq_entry pointer back to
1684 * lio_listio calls behave differently at completion they do completion notification
1685 * when all async IO requests have completed. We use group_tag to tag IO requests
1686 * that behave in the delay notification manner.
1690 lio_create_sync_entry( struct proc
*procp
, user_addr_t aiocbp
,
1691 long group_tag
, aio_workq_entry
**entrypp
)
1693 aio_workq_entry
*entryp
;
1696 entryp
= (aio_workq_entry
*) zalloc( aio_workq_zonep
);
1697 if ( entryp
== NULL
) {
1701 bzero( entryp
, sizeof(*entryp
) );
1703 /* fill in the rest of the aio_workq_entry */
1704 entryp
->procp
= procp
;
1705 entryp
->uaiocbp
= aiocbp
;
1706 entryp
->flags
|= AIO_LIO
;
1707 entryp
->group_tag
= group_tag
;
1708 entryp
->aio_map
= VM_MAP_NULL
;
1710 if ( !IS_64BIT_PROCESS(procp
) ) {
1711 struct aiocb aiocb32
;
1713 result
= copyin( aiocbp
, &aiocb32
, sizeof(aiocb32
) );
1715 do_munge_aiocb( &aiocb32
, &entryp
->aiocb
);
1717 result
= copyin( aiocbp
, &entryp
->aiocb
, sizeof(entryp
->aiocb
) );
1719 if ( result
!= 0 ) {
1724 /* look for lio_listio LIO_NOP requests and ignore them. */
1725 /* Not really an error, but we need to free our aio_workq_entry. */
1726 if ( entryp
->aiocb
.aio_lio_opcode
== LIO_NOP
) {
1731 result
= aio_validate( entryp
);
1732 if ( result
!= 0 ) {
1740 if ( entryp
!= NULL
)
1741 zfree( aio_workq_zonep
, entryp
);
1745 } /* lio_create_sync_entry */
1749 * aio_free_request - remove our reference on the user land map and
1750 * free the work queue entry resources.
1751 * We are not holding the lock here thus aio_map is passed in and
1752 * zeroed while we did have the lock.
1756 aio_free_request( aio_workq_entry
*entryp
, vm_map_t the_map
)
1758 /* remove our reference to the user land map. */
1759 if ( VM_MAP_NULL
!= the_map
) {
1760 vm_map_deallocate( the_map
);
1763 zfree( aio_workq_zonep
, entryp
);
1767 } /* aio_free_request */
1770 /* aio_validate - validate the aiocb passed in by one of the aio syscalls.
1774 aio_validate( aio_workq_entry
*entryp
)
1776 struct fileproc
*fp
;
1782 if ( (entryp
->flags
& AIO_LIO
) != 0 ) {
1783 if ( entryp
->aiocb
.aio_lio_opcode
== LIO_READ
)
1784 entryp
->flags
|= AIO_READ
;
1785 else if ( entryp
->aiocb
.aio_lio_opcode
== LIO_WRITE
)
1786 entryp
->flags
|= AIO_WRITE
;
1787 else if ( entryp
->aiocb
.aio_lio_opcode
== LIO_NOP
)
1794 if ( (entryp
->flags
& (AIO_WRITE
| AIO_FSYNC
)) != 0 ) {
1798 if ( (entryp
->flags
& (AIO_READ
| AIO_WRITE
)) != 0 ) {
1799 // LP64todo - does max value for aio_nbytes need to grow?
1800 if ( entryp
->aiocb
.aio_nbytes
> INT_MAX
||
1801 entryp
->aiocb
.aio_buf
== USER_ADDR_NULL
||
1802 entryp
->aiocb
.aio_offset
< 0 )
1806 /* validate aiocb.aio_sigevent. at this point we only support sigev_notify
1807 * equal to SIGEV_SIGNAL or SIGEV_NONE. this means sigev_value,
1808 * sigev_notify_function, and sigev_notify_attributes are ignored.
1810 if ( entryp
->aiocb
.aio_sigevent
.sigev_notify
== SIGEV_SIGNAL
) {
1812 /* make sure we have a valid signal number */
1813 signum
= entryp
->aiocb
.aio_sigevent
.sigev_signo
;
1814 if ( signum
<= 0 || signum
>= NSIG
||
1815 signum
== SIGKILL
|| signum
== SIGSTOP
)
1818 else if ( entryp
->aiocb
.aio_sigevent
.sigev_notify
!= SIGEV_NONE
)
1821 /* validate the file descriptor and that the file was opened
1822 * for the appropriate read / write access.
1824 proc_fdlock(entryp
->procp
);
1826 result
= fp_lookup( entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, 1);
1827 if ( result
== 0 ) {
1828 if ( (fp
->f_fglob
->fg_flag
& flag
) == 0 ) {
1829 /* we don't have read or write access */
1832 else if ( fp
->f_fglob
->fg_type
!= DTYPE_VNODE
) {
1833 /* this is not a file */
1836 fp
->f_flags
|= FP_AIOISSUED
;
1838 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 1);
1844 proc_fdunlock(entryp
->procp
);
1848 } /* aio_validate */
1852 * aio_get_process_count - runs through our queues that hold outstanding
1853 * async IO reqests and totals up number of requests for the given
1855 * NOTE - caller must hold aio lock!
1859 aio_get_process_count( struct proc
*procp
)
1861 aio_workq_entry
*entryp
;
1864 /* begin with count of completed async IO requests for this process */
1865 count
= procp
->aio_done_count
;
1867 /* add in count of active async IO requests for this process */
1868 count
+= procp
->aio_active_count
;
1870 /* look for matches on our queue of asynchronous todo work */
1871 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
1872 if ( procp
== entryp
->procp
) {
1877 /* look for matches on our queue of synchronous todo work */
1878 TAILQ_FOREACH( entryp
, &aio_anchor
.lio_sync_workq
, aio_workq_link
) {
1879 if ( procp
== entryp
->procp
) {
1886 } /* aio_get_process_count */
1890 * aio_get_all_queues_count - get total number of entries on all aio work queues.
1891 * NOTE - caller must hold aio lock!
1895 aio_get_all_queues_count( void )
1899 count
= aio_anchor
.aio_async_workq_count
;
1900 count
+= aio_anchor
.lio_sync_workq_count
;
1901 count
+= aio_anchor
.aio_active_count
;
1902 count
+= aio_anchor
.aio_done_count
;
1906 } /* aio_get_all_queues_count */
1910 * do_aio_completion. Handle async IO completion.
1914 do_aio_completion( aio_workq_entry
*entryp
)
1916 /* signal user land process if appropriate */
1917 if ( entryp
->aiocb
.aio_sigevent
.sigev_notify
== SIGEV_SIGNAL
&&
1918 (entryp
->flags
& AIO_DISABLE
) == 0 ) {
1921 * if group_tag is non zero then make sure this is the last IO request
1922 * in the group before we signal.
1924 if ( entryp
->group_tag
== 0 ||
1925 (entryp
->group_tag
!= 0 && aio_last_group_io( entryp
)) ) {
1926 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_sig
)) | DBG_FUNC_NONE
,
1927 (int)entryp
->procp
, (int)entryp
->uaiocbp
,
1928 entryp
->aiocb
.aio_sigevent
.sigev_signo
, 0, 0 );
1930 psignal( entryp
->procp
, entryp
->aiocb
.aio_sigevent
.sigev_signo
);
1936 * need to handle case where a process is trying to exit, exec, or close
1937 * and is currently waiting for active aio requests to complete. If
1938 * AIO_WAITING is set then we need to look to see if there are any
1939 * other requests in the active queue for this process. If there are
1940 * none then wakeup using the AIO_CLEANUP_SLEEP_CHAN tsleep channel. If
1941 * there are some still active then do nothing - we only want to wakeup
1942 * when all active aio requests for the process are complete.
1944 if ( (entryp
->flags
& AIO_WAITING
) != 0 ) {
1945 int active_requests
;
1947 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_cleanup_wait
)) | DBG_FUNC_NONE
,
1948 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1951 active_requests
= aio_active_requests_for_process( entryp
->procp
);
1953 if ( active_requests
< 1 ) {
1954 /* no active aio requests for this process, continue exiting */
1955 wakeup_one( (caddr_t
) &entryp
->procp
->AIO_CLEANUP_SLEEP_CHAN
);
1957 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_cleanup_wake
)) | DBG_FUNC_NONE
,
1958 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1965 * aio_suspend case when a signal was not requested. In that scenario we
1966 * are sleeping on the AIO_SUSPEND_SLEEP_CHAN channel.
1967 * NOTE - the assumption here is that this wakeup call is inexpensive.
1968 * we really only need to do this when an aio_suspend call is pending.
1969 * If we find the wakeup call should be avoided we could mark the
1970 * async IO requests given in the list provided by aio_suspend and only
1971 * call wakeup for them. If we do mark them we should unmark them after
1972 * the aio_suspend wakes up.
1975 wakeup_one( (caddr_t
) &entryp
->procp
->AIO_SUSPEND_SLEEP_CHAN
);
1978 KERNEL_DEBUG( (BSDDBG_CODE(DBG_BSD_AIO
, AIO_completion_suspend_wake
)) | DBG_FUNC_NONE
,
1979 (int)entryp
->procp
, (int)entryp
->uaiocbp
, 0, 0, 0 );
1983 } /* do_aio_completion */
1987 * aio_last_group_io - checks to see if this is the last unfinished IO request
1988 * for the given group_tag. Returns TRUE if there are no other active IO
1989 * requests for this group or FALSE if the are active IO requests
1990 * NOTE - AIO_LOCK must be held by caller
1994 aio_last_group_io( aio_workq_entry
*entryp
)
1996 aio_workq_entry
*my_entryp
;
1998 /* look for matches on our queue of active async IO requests */
1999 TAILQ_FOREACH( my_entryp
, &entryp
->procp
->aio_activeq
, aio_workq_link
) {
2000 if ( my_entryp
->group_tag
== entryp
->group_tag
)
2004 /* look for matches on our queue of asynchronous todo work */
2005 TAILQ_FOREACH( my_entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
2006 if ( my_entryp
->group_tag
== entryp
->group_tag
)
2010 /* look for matches on our queue of synchronous todo work */
2011 TAILQ_FOREACH( my_entryp
, &aio_anchor
.lio_sync_workq
, aio_workq_link
) {
2012 if ( my_entryp
->group_tag
== entryp
->group_tag
)
2018 } /* aio_last_group_io */
2025 do_aio_read( aio_workq_entry
*entryp
)
2027 struct fileproc
*fp
;
2030 if ( (error
= fp_lookup(entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, 0)) )
2032 if ( (fp
->f_fglob
->fg_flag
& FREAD
) == 0 ) {
2033 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2037 error
= dofileread( entryp
->procp
, fp
, entryp
->aiocb
.aio_fildes
,
2038 entryp
->aiocb
.aio_buf
,
2039 entryp
->aiocb
.aio_nbytes
,
2040 entryp
->aiocb
.aio_offset
, FOF_OFFSET
,
2041 &entryp
->returnval
);
2042 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2045 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2058 do_aio_write( aio_workq_entry
*entryp
)
2060 struct fileproc
*fp
;
2063 if ( (error
= fp_lookup(entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, 0)) )
2065 if ( (fp
->f_fglob
->fg_flag
& FWRITE
) == 0 ) {
2066 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2070 /* NB: tell dofilewrite the offset, and to use the proc cred */
2071 error
= dofilewrite( entryp
->procp
,
2073 entryp
->aiocb
.aio_fildes
,
2074 entryp
->aiocb
.aio_buf
,
2075 entryp
->aiocb
.aio_nbytes
,
2076 entryp
->aiocb
.aio_offset
,
2077 FOF_OFFSET
| FOF_PCRED
,
2078 &entryp
->returnval
);
2080 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2083 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2089 } /* do_aio_write */
2093 * aio_active_requests_for_process - return number of active async IO
2094 * requests for the given process.
2095 * NOTE - caller must hold aio lock!
2099 aio_active_requests_for_process( struct proc
*procp
)
2102 return( procp
->aio_active_count
);
2104 } /* aio_active_requests_for_process */
2111 do_aio_fsync( aio_workq_entry
*entryp
)
2113 struct vfs_context context
;
2115 struct fileproc
*fp
;
2119 * NOTE - we will not support AIO_DSYNC until fdatasync() is supported.
2120 * AIO_DSYNC is caught before we queue up a request and flagged as an error.
2121 * The following was shamelessly extracted from fsync() implementation.
2124 error
= fp_getfvp( entryp
->procp
, entryp
->aiocb
.aio_fildes
, &fp
, &vp
);
2126 if ( (error
= vnode_getwithref(vp
)) ) {
2127 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2128 entryp
->returnval
= -1;
2131 context
.vc_proc
= entryp
->procp
;
2132 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
2134 error
= VNOP_FSYNC( vp
, MNT_WAIT
, &context
);
2136 (void)vnode_put(vp
);
2138 fp_drop(entryp
->procp
, entryp
->aiocb
.aio_fildes
, fp
, 0);
2141 entryp
->returnval
= -1;
2145 } /* do_aio_fsync */
2149 * is_already_queued - runs through our queues to see if the given
2150 * aiocbp / process is there. Returns TRUE if there is a match
2151 * on any of our aio queues.
2152 * NOTE - callers must hold aio lock!
2156 is_already_queued( struct proc
*procp
,
2157 user_addr_t aiocbp
)
2159 aio_workq_entry
*entryp
;
2164 /* look for matches on our queue of async IO requests that have completed */
2165 TAILQ_FOREACH( entryp
, &procp
->aio_doneq
, aio_workq_link
) {
2166 if ( aiocbp
== entryp
->uaiocbp
) {
2168 goto ExitThisRoutine
;
2172 /* look for matches on our queue of active async IO requests */
2173 TAILQ_FOREACH( entryp
, &procp
->aio_activeq
, aio_workq_link
) {
2174 if ( aiocbp
== entryp
->uaiocbp
) {
2176 goto ExitThisRoutine
;
2180 /* look for matches on our queue of asynchronous todo work */
2181 TAILQ_FOREACH( entryp
, &aio_anchor
.aio_async_workq
, aio_workq_link
) {
2182 if ( procp
== entryp
->procp
&& aiocbp
== entryp
->uaiocbp
) {
2184 goto ExitThisRoutine
;
2188 /* look for matches on our queue of synchronous todo work */
2189 TAILQ_FOREACH( entryp
, &aio_anchor
.lio_sync_workq
, aio_workq_link
) {
2190 if ( procp
== entryp
->procp
&& aiocbp
== entryp
->uaiocbp
) {
2192 goto ExitThisRoutine
;
2199 } /* is_already_queued */
2203 * aio initialization
2205 __private_extern__
void
2210 aio_lock_grp_attr
= lck_grp_attr_alloc_init();
2211 lck_grp_attr_setstat(aio_lock_grp_attr
);
2212 aio_lock_grp
= lck_grp_alloc_init("aio", aio_lock_grp_attr
);
2213 aio_lock_attr
= lck_attr_alloc_init();
2214 //lck_attr_setdebug(aio_lock_attr);
2216 aio_lock
= lck_mtx_alloc_init(aio_lock_grp
, aio_lock_attr
);
2219 TAILQ_INIT( &aio_anchor
.aio_async_workq
);
2220 TAILQ_INIT( &aio_anchor
.lio_sync_workq
);
2221 aio_anchor
.aio_async_workq_count
= 0;
2222 aio_anchor
.lio_sync_workq_count
= 0;
2223 aio_anchor
.aio_active_count
= 0;
2224 aio_anchor
.aio_done_count
= 0;
2227 i
= sizeof( aio_workq_entry
);
2228 aio_workq_zonep
= zinit( i
, i
* aio_max_requests
, i
* aio_max_requests
, "aiowq" );
2230 _aio_create_worker_threads( aio_worker_threads
);
2238 * aio worker threads created here.
2240 __private_extern__
void
2241 _aio_create_worker_threads( int num
)
2245 /* create some worker threads to handle the async IO requests */
2246 for ( i
= 0; i
< num
; i
++ ) {
2249 myThread
= kernel_thread( kernel_task
, aio_work_thread
);
2250 if ( THREAD_NULL
== myThread
) {
2251 printf( "%s - failed to create a work thread \n", __FUNCTION__
);
2257 } /* _aio_create_worker_threads */
2260 * Return the current activation utask
2265 return ((struct uthread
*)get_bsdthread_info(current_thread()))->uu_aio_task
;
2270 * In the case of an aiocb from a
2271 * 32-bit process we need to expand some longs and pointers to the correct
2272 * sizes in order to let downstream code always work on the same type of
2273 * aiocb (in our case that is a user_aiocb)
2276 do_munge_aiocb( struct aiocb
*my_aiocbp
, struct user_aiocb
*the_user_aiocbp
)
2278 the_user_aiocbp
->aio_fildes
= my_aiocbp
->aio_fildes
;
2279 the_user_aiocbp
->aio_offset
= my_aiocbp
->aio_offset
;
2280 the_user_aiocbp
->aio_buf
= CAST_USER_ADDR_T(my_aiocbp
->aio_buf
);
2281 the_user_aiocbp
->aio_nbytes
= my_aiocbp
->aio_nbytes
;
2282 the_user_aiocbp
->aio_reqprio
= my_aiocbp
->aio_reqprio
;
2283 the_user_aiocbp
->aio_lio_opcode
= my_aiocbp
->aio_lio_opcode
;
2285 /* special case here. since we do not know if sigev_value is an */
2286 /* int or a ptr we do NOT cast the ptr to a user_addr_t. This */
2287 /* means if we send this info back to user space we need to remember */
2288 /* sigev_value was not expanded for the 32-bit case. */
2289 /* NOTE - this does NOT affect us since we don't support sigev_value */
2290 /* yet in the aio context. */
2292 the_user_aiocbp
->aio_sigevent
.sigev_notify
= my_aiocbp
->aio_sigevent
.sigev_notify
;
2293 the_user_aiocbp
->aio_sigevent
.sigev_signo
= my_aiocbp
->aio_sigevent
.sigev_signo
;
2294 the_user_aiocbp
->aio_sigevent
.sigev_value
.size_equivalent
.sival_int
=
2295 my_aiocbp
->aio_sigevent
.sigev_value
.sival_int
;
2296 the_user_aiocbp
->aio_sigevent
.sigev_notify_function
=
2297 CAST_USER_ADDR_T(my_aiocbp
->aio_sigevent
.sigev_notify_function
);
2298 the_user_aiocbp
->aio_sigevent
.sigev_notify_attributes
=
2299 CAST_USER_ADDR_T(my_aiocbp
->aio_sigevent
.sigev_notify_attributes
);