2 * Simulate Posix AIO using pthreads.
4 * Based on the aio_fork work from Volker and Volker's pthreadpool library.
6 * Copyright (C) Volker Lendecke 2008
7 * Copyright (C) Jeremy Allison 2012
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/filesys.h"
26 #include "system/shmem.h"
27 #include "smbd/smbd.h"
28 #include "pthreadpool.h"
31 static struct pthreadpool *pool;
32 static int aio_pthread_jobid;
34 struct aio_private_data {
35 struct aio_private_data *prev, *next;
37 SMB_STRUCT_AIOCB *aiocb;
44 /* List of outstanding requests we have. */
45 struct aio_private_data *pd_list;
47 static void aio_pthread_handle_completion(struct event_context *event_ctx,
48 struct fd_event *event,
52 /************************************************************************
53 How many threads to initialize ?
54 100 per process seems insane as a default until you realize that
55 (a) Threads terminate after 1 second when idle.
56 (b) Throttling is done in SMB2 via the crediting algorithm.
57 (c) SMB1 clients are limited to max_mux (50) outstanding requests and
58 Windows clients don't use this anyway.
59 Essentially we want this to be unlimited unless smb.conf says different.
60 ***********************************************************************/
62 static int aio_get_num_threads(struct vfs_handle_struct *handle)
64 return lp_parm_bool(SNUM(handle->conn),
71 /************************************************************************
72 Called every 30 seconds to destroy pool if it's idle.
73 ***********************************************************************/
75 static void idle_pool_destroy_timer(struct tevent_context *ev,
76 struct tevent_timer *te,
77 struct timeval current_time,
84 if (pool && pd_list == NULL) {
85 if (pthreadpool_destroy(pool) == 0) {
88 DEBUG(10,("idle_pool_destroy_timer: destroyed AIO pool.\n"));
92 /* Here, the IO is still active. */
94 /* Set an event up for 30 seconds time - if we have
95 no outstanding IO at this time shut the threadpool
97 ne = tevent_timeval_current_ofs(30, 0);
98 tevent_add_timer(server_event_context(),
101 idle_pool_destroy_timer,
106 /************************************************************************
107 Ensure thread pool is initialized.
108 ***********************************************************************/
110 static bool init_aio_threadpool(struct vfs_handle_struct *handle)
112 struct fd_event *sock_event = NULL;
123 num_threads = aio_get_num_threads(handle);
124 ret = pthreadpool_init(num_threads, &pool);
129 sock_event = tevent_add_fd(server_event_context(),
131 pthreadpool_signal_fd(pool),
133 aio_pthread_handle_completion,
135 if (sock_event == NULL) {
136 pthreadpool_destroy(pool);
142 /* Set an event up for 30 seconds time - if we have
143 no outstanding IO at this time shut the threadpool
145 ne = tevent_timeval_current_ofs(30, 0);
146 tevent_add_timer(server_event_context(),
149 idle_pool_destroy_timer,
153 DEBUG(10,("init_aio_threadpool: initialized with %d threads\n",
160 /************************************************************************
161 Worker function - core of the pthread aio engine.
162 This is the function that actually does the IO.
163 ***********************************************************************/
165 static void aio_worker(void *private_data)
167 struct aio_private_data *pd =
168 (struct aio_private_data *)private_data;
170 if (pd->write_command) {
171 pd->ret_size = pwrite(pd->aiocb->aio_fildes,
172 (const void *)pd->aiocb->aio_buf,
173 pd->aiocb->aio_nbytes,
174 pd->aiocb->aio_offset);
176 pd->ret_size = pread(pd->aiocb->aio_fildes,
177 (void *)pd->aiocb->aio_buf,
178 pd->aiocb->aio_nbytes,
179 pd->aiocb->aio_offset);
181 if (pd->ret_size == -1) {
182 pd->ret_errno = errno;
188 /************************************************************************
189 Private data destructor.
190 ***********************************************************************/
192 static int pd_destructor(struct aio_private_data *pd)
194 DLIST_REMOVE(pd_list, pd);
198 /************************************************************************
199 Create and initialize a private data struct.
200 ***********************************************************************/
202 static struct aio_private_data *create_private_data(TALLOC_CTX *ctx,
203 SMB_STRUCT_AIOCB *aiocb)
205 struct aio_private_data *pd = talloc_zero(ctx, struct aio_private_data);
209 pd->jobid = aio_pthread_jobid++;
212 pd->ret_errno = EINPROGRESS;
213 talloc_set_destructor(pd, pd_destructor);
214 DLIST_ADD_END(pd_list, pd, struct aio_private_data *);
218 /************************************************************************
219 Spin off a threadpool (if needed) and initiate a pread call.
220 ***********************************************************************/
222 static int aio_pthread_read(struct vfs_handle_struct *handle,
223 struct files_struct *fsp,
224 SMB_STRUCT_AIOCB *aiocb)
226 struct aio_extra *aio_ex = (struct aio_extra *)aiocb->aio_sigevent.sigev_value.sival_ptr;
227 struct aio_private_data *pd = NULL;
230 if (!init_aio_threadpool(handle)) {
234 pd = create_private_data(aio_ex, aiocb);
236 DEBUG(10, ("aio_pthread_read: Could not create private data.\n"));
240 ret = pthreadpool_add_job(pool, pd->jobid, aio_worker, (void *)pd);
246 DEBUG(10, ("aio_pthread_read: jobid=%d pread requested "
247 "of %llu bytes at offset %llu\n",
249 (unsigned long long)pd->aiocb->aio_nbytes,
250 (unsigned long long)pd->aiocb->aio_offset));
255 /************************************************************************
256 Spin off a threadpool (if needed) and initiate a pwrite call.
257 ***********************************************************************/
259 static int aio_pthread_write(struct vfs_handle_struct *handle,
260 struct files_struct *fsp,
261 SMB_STRUCT_AIOCB *aiocb)
263 struct aio_extra *aio_ex = (struct aio_extra *)aiocb->aio_sigevent.sigev_value.sival_ptr;
264 struct aio_private_data *pd = NULL;
267 if (!init_aio_threadpool(handle)) {
271 pd = create_private_data(aio_ex, aiocb);
273 DEBUG(10, ("aio_pthread_write: Could not create private data.\n"));
277 pd->write_command = true;
279 ret = pthreadpool_add_job(pool, pd->jobid, aio_worker, (void *)pd);
285 DEBUG(10, ("aio_pthread_write: jobid=%d pwrite requested "
286 "of %llu bytes at offset %llu\n",
288 (unsigned long long)pd->aiocb->aio_nbytes,
289 (unsigned long long)pd->aiocb->aio_offset));
294 /************************************************************************
295 Find the private data by jobid.
296 ***********************************************************************/
298 static struct aio_private_data *find_private_data_by_jobid(int jobid)
300 struct aio_private_data *pd;
302 for (pd = pd_list; pd != NULL; pd = pd->next) {
303 if (pd->jobid == jobid) {
311 /************************************************************************
312 Callback when an IO completes.
313 ***********************************************************************/
315 static void aio_pthread_handle_completion(struct event_context *event_ctx,
316 struct fd_event *event,
320 struct aio_extra *aio_ex = NULL;
321 struct aio_private_data *pd = NULL;
325 DEBUG(10, ("aio_pthread_handle_completion called with flags=%d\n",
328 if ((flags & EVENT_FD_READ) == 0) {
332 ret = pthreadpool_finished_job(pool, &jobid);
334 smb_panic("aio_pthread_handle_completion");
338 pd = find_private_data_by_jobid(jobid);
340 DEBUG(1, ("aio_pthread_handle_completion cannot find jobid %d\n",
345 aio_ex = (struct aio_extra *)pd->aiocb->aio_sigevent.sigev_value.sival_ptr;
346 smbd_aio_complete_aio_ex(aio_ex);
348 DEBUG(10,("aio_pthread_handle_completion: jobid %d completed\n",
353 /************************************************************************
354 Find the private data by aiocb.
355 ***********************************************************************/
357 static struct aio_private_data *find_private_data_by_aiocb(SMB_STRUCT_AIOCB *aiocb)
359 struct aio_private_data *pd;
361 for (pd = pd_list; pd != NULL; pd = pd->next) {
362 if (pd->aiocb == aiocb) {
370 /************************************************************************
371 Called to return the result of a completed AIO.
372 Should only be called if aio_error returns something other than EINPROGRESS.
374 Any other value - return from IO operation.
375 ***********************************************************************/
377 static ssize_t aio_pthread_return_fn(struct vfs_handle_struct *handle,
378 struct files_struct *fsp,
379 SMB_STRUCT_AIOCB *aiocb)
381 struct aio_private_data *pd = find_private_data_by_aiocb(aiocb);
385 DEBUG(0, ("aio_pthread_return_fn: returning EINVAL\n"));
391 if (pd->ret_size == -1) {
392 errno = pd->ret_errno;
398 /************************************************************************
399 Called to check the result of an AIO.
401 EINPROGRESS - still in progress.
402 EINVAL - invalid aiocb.
403 ECANCELED - request was cancelled.
404 0 - request completed successfully.
405 Any other value - errno from IO operation.
406 ***********************************************************************/
408 static int aio_pthread_error_fn(struct vfs_handle_struct *handle,
409 struct files_struct *fsp,
410 SMB_STRUCT_AIOCB *aiocb)
412 struct aio_private_data *pd = find_private_data_by_aiocb(aiocb);
420 return pd->ret_errno;
423 /************************************************************************
424 Called to request the cancel of an AIO, or all of them on a specific
425 fsp if aiocb == NULL.
426 ***********************************************************************/
428 static int aio_pthread_cancel(struct vfs_handle_struct *handle,
429 struct files_struct *fsp,
430 SMB_STRUCT_AIOCB *aiocb)
432 struct aio_private_data *pd = NULL;
434 for (pd = pd_list; pd != NULL; pd = pd->next) {
435 if (pd->aiocb == NULL) {
438 if (pd->aiocb->aio_fildes != fsp->fh->fd) {
441 if ((aiocb != NULL) && (pd->aiocb != aiocb)) {
446 * We let the child do its job, but we discard the result when
450 pd->cancelled = true;
456 /************************************************************************
457 Callback for a previously detected job completion.
458 ***********************************************************************/
460 static void aio_pthread_handle_immediate(struct tevent_context *ctx,
461 struct tevent_immediate *im,
464 struct aio_extra *aio_ex = NULL;
465 int *pjobid = (int *)private_data;
466 struct aio_private_data *pd = find_private_data_by_jobid(*pjobid);
469 DEBUG(1, ("aio_pthread_handle_immediate cannot find jobid %d\n",
476 aio_ex = (struct aio_extra *)pd->aiocb->aio_sigevent.sigev_value.sival_ptr;
477 smbd_aio_complete_aio_ex(aio_ex);
480 /************************************************************************
481 Private data struct used in suspend completion code.
482 ***********************************************************************/
484 struct suspend_private {
487 const SMB_STRUCT_AIOCB * const *aiocb_array;
490 /************************************************************************
491 Callback when an IO completes from a suspend call.
492 ***********************************************************************/
494 static void aio_pthread_handle_suspend_completion(struct event_context *event_ctx,
495 struct fd_event *event,
499 struct suspend_private *sp = (struct suspend_private *)p;
500 struct aio_private_data *pd = NULL;
501 struct tevent_immediate *im = NULL;
505 DEBUG(10, ("aio_pthread_handle_suspend_completion called with flags=%d\n",
508 if ((flags & EVENT_FD_READ) == 0) {
512 pjobid = talloc_array(NULL, int, 1);
514 smb_panic("aio_pthread_handle_suspend_completion: no memory.");
517 if (pthreadpool_finished_job(pool, pjobid)) {
518 smb_panic("aio_pthread_handle_suspend_completion: can't find job.");
522 pd = find_private_data_by_jobid(*pjobid);
524 DEBUG(1, ("aio_pthread_handle_completion cannot find jobid %d\n",
530 /* Is this a jobid with an aiocb we're interested in ? */
531 for (i = 0; i < sp->num_entries; i++) {
532 if (sp->aiocb_array[i] == pd->aiocb) {
539 /* Jobid completed we weren't waiting for.
540 We must reshedule this as an immediate event
541 on the main event context. */
542 im = tevent_create_immediate(NULL);
544 exit_server_cleanly("aio_pthread_handle_suspend_completion: no memory");
547 DEBUG(10,("aio_pthread_handle_suspend_completion: "
548 "re-scheduling job id %d\n",
551 tevent_schedule_immediate(im,
552 server_event_context(),
553 aio_pthread_handle_immediate,
558 static void aio_pthread_suspend_timed_out(struct tevent_context *event_ctx,
559 struct tevent_timer *te,
563 bool *timed_out = (bool *)private_data;
564 /* Remove this timed event handler. */
569 /************************************************************************
570 Called to request everything to stop until all IO is completed.
571 ***********************************************************************/
573 static int aio_pthread_suspend(struct vfs_handle_struct *handle,
574 struct files_struct *fsp,
575 const SMB_STRUCT_AIOCB * const aiocb_array[],
577 const struct timespec *timeout)
579 struct event_context *ev = NULL;
580 struct fd_event *sock_event = NULL;
582 struct suspend_private sp;
583 bool timed_out = false;
584 TALLOC_CTX *frame = talloc_stackframe();
586 /* This is a blocking call, and has to use a sub-event loop. */
587 ev = event_context_init(frame);
594 struct timeval tv = convert_timespec_to_timeval(*timeout);
595 struct tevent_timer *te = tevent_add_timer(ev,
597 timeval_current_ofs(tv.tv_sec,
599 aio_pthread_suspend_timed_out,
609 sp.aiocb_array = aiocb_array;
612 sock_event = tevent_add_fd(ev,
614 pthreadpool_signal_fd(pool),
616 aio_pthread_handle_suspend_completion,
618 if (sock_event == NULL) {
619 pthreadpool_destroy(pool);
624 * We're going to cheat here. We know that smbd/aio.c
625 * only calls this when it's waiting for every single
626 * outstanding call to finish on a close, so just wait
627 * individually for each IO to complete. We don't care
628 * what order they finish - only that they all do. JRA.
630 while (sp.num_entries != sp.num_finished) {
631 if (tevent_loop_once(ev) == -1) {
649 static struct vfs_fn_pointers vfs_aio_pthread_fns = {
650 .aio_read_fn = aio_pthread_read,
651 .aio_write_fn = aio_pthread_write,
652 .aio_return_fn = aio_pthread_return_fn,
653 .aio_cancel_fn = aio_pthread_cancel,
654 .aio_error_fn = aio_pthread_error_fn,
655 .aio_suspend_fn = aio_pthread_suspend,
658 NTSTATUS vfs_aio_pthread_init(void);
659 NTSTATUS vfs_aio_pthread_init(void)
661 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
662 "aio_pthread", &vfs_aio_pthread_fns);