2 * Simulate the Posix AIO using mmap/fork
4 * Copyright (C) Volker Lendecke 2008
5 * Copyright (C) Jeremy Allison 2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "system/filesys.h"
24 #include "system/shmem.h"
25 #include "smbd/smbd.h"
36 static int mmap_area_destructor(struct mmap_area *area)
38 munmap((void *)area->ptr, area->size);
42 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
44 struct mmap_area *result;
47 result = talloc(mem_ctx, struct mmap_area);
49 DEBUG(0, ("talloc failed\n"));
53 fd = open("/dev/zero", O_RDWR);
55 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
60 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
61 MAP_SHARED|MAP_FILE, fd, 0);
62 if (result->ptr == MAP_FAILED) {
63 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
70 talloc_set_destructor(result, mmap_area_destructor);
90 struct aio_child_list;
93 struct aio_child *prev, *next;
94 struct aio_child_list *list;
95 SMB_STRUCT_AIOCB *aiocb;
98 struct fd_event *sock_event;
100 struct mmap_area *map; /* ==NULL means write request */
101 bool dont_delete; /* Marked as in use since last cleanup */
106 struct aio_child_list {
107 struct aio_child *children;
108 struct timed_event *cleanup_event;
111 static void free_aio_children(void **p)
116 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
121 #ifndef HAVE_MSGHDR_MSG_CONTROL
125 #ifdef HAVE_MSGHDR_MSG_CONTROL
128 char control[CMSG_SPACE(sizeof(int))];
130 struct cmsghdr *cmptr;
132 msg.msg_control = control_un.control;
133 msg.msg_controllen = sizeof(control_un.control);
135 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
136 msg.msg_accrights = (caddr_t) &newfd;
137 msg.msg_accrightslen = sizeof(int);
139 #error Can not pass file descriptors
146 iov[0].iov_base = (void *)ptr;
147 iov[0].iov_len = nbytes;
151 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
155 #ifdef HAVE_MSGHDR_MSG_CONTROL
156 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
157 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
158 if (cmptr->cmsg_level != SOL_SOCKET) {
159 DEBUG(10, ("control level != SOL_SOCKET"));
163 if (cmptr->cmsg_type != SCM_RIGHTS) {
164 DEBUG(10, ("control type != SCM_RIGHTS"));
168 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
170 *recvfd = -1; /* descriptor was not passed */
173 if (msg.msg_accrightslen == sizeof(int)) {
177 *recvfd = -1; /* descriptor was not passed */
184 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
189 #ifdef HAVE_MSGHDR_MSG_CONTROL
192 char control[CMSG_SPACE(sizeof(int))];
194 struct cmsghdr *cmptr;
197 ZERO_STRUCT(control_un);
199 msg.msg_control = control_un.control;
200 msg.msg_controllen = sizeof(control_un.control);
202 cmptr = CMSG_FIRSTHDR(&msg);
203 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
204 cmptr->cmsg_level = SOL_SOCKET;
205 cmptr->cmsg_type = SCM_RIGHTS;
206 memcpy(CMSG_DATA(cmptr), &sendfd, sizeof(sendfd));
209 msg.msg_accrights = (caddr_t) &sendfd;
210 msg.msg_accrightslen = sizeof(int);
217 iov[0].iov_base = (void *)ptr;
218 iov[0].iov_len = nbytes;
222 return (sendmsg(fd, &msg, 0));
225 static void aio_child_cleanup(struct event_context *event_ctx,
226 struct timed_event *te,
230 struct aio_child_list *list = talloc_get_type_abort(
231 private_data, struct aio_child_list);
232 struct aio_child *child, *next;
234 TALLOC_FREE(list->cleanup_event);
236 for (child = list->children; child != NULL; child = next) {
239 if (child->aiocb != NULL) {
240 DEBUG(10, ("child %d currently active\n",
245 if (child->dont_delete) {
246 DEBUG(10, ("Child %d was active since last cleanup\n",
248 child->dont_delete = false;
252 DEBUG(10, ("Child %d idle for more than 30 seconds, "
253 "deleting\n", (int)child->pid));
259 if (list->children != NULL) {
261 * Re-schedule the next cleanup round
263 list->cleanup_event = event_add_timed(server_event_context(), list,
264 timeval_add(&now, 30, 0),
265 aio_child_cleanup, list);
270 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
272 struct aio_child_list *data = NULL;
274 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
275 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
280 data = talloc_zero(NULL, struct aio_child_list);
287 * Regardless of whether the child_list had been around or not, make
288 * sure that we have a cleanup timed event. This timed event will
289 * delete itself when it finds that no children are around anymore.
292 if (data->cleanup_event == NULL) {
293 data->cleanup_event = event_add_timed(server_event_context(), data,
294 timeval_current_ofs(30, 0),
295 aio_child_cleanup, data);
296 if (data->cleanup_event == NULL) {
302 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
303 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
304 struct aio_child_list, return False);
310 static void aio_child_loop(int sockfd, struct mmap_area *map)
315 struct rw_cmd cmd_struct;
316 struct rw_ret ret_struct;
318 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
319 if (ret != sizeof(cmd_struct)) {
320 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
325 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
326 cmd_struct.read_cmd ? "read" : "write",
327 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
329 #ifdef ENABLE_BUILD_FARM_HACKS
332 * In the build farm, we want erratic behaviour for
338 * use generate_random_buffer, we just forked from a
339 * common parent state
341 generate_random_buffer(&randval, sizeof(randval));
342 msecs = randval + 20;
343 DEBUG(10, ("delaying for %u msecs\n", msecs));
349 ZERO_STRUCT(ret_struct);
351 if (cmd_struct.read_cmd) {
352 ret_struct.size = sys_pread(
353 fd, (void *)map->ptr, cmd_struct.n,
356 /* This breaks "make test" when run with aio_fork module. */
357 #ifdef ENABLE_BUILD_FARM_HACKS
358 ret_struct.size = MAX(1, ret_struct.size * 0.9);
363 ret_struct.size = sys_pwrite(
364 fd, (void *)map->ptr, cmd_struct.n,
368 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
369 (int)ret_struct.size));
371 if (ret_struct.size == -1) {
372 ret_struct.ret_errno = errno;
376 * Close the fd before telling our parent we're done. The
377 * parent might close and re-open the file very quickly, and
378 * with system-level share modes (GPFS) we would get an
379 * unjustified SHARING_VIOLATION.
383 ret = write_data(sockfd, (char *)&ret_struct,
385 if (ret != sizeof(ret_struct)) {
386 DEBUG(10, ("could not write ret_struct: %s\n",
393 static void handle_aio_completion(struct event_context *event_ctx,
394 struct fd_event *event, uint16 flags,
397 struct aio_extra *aio_ex = NULL;
398 struct aio_child *child = (struct aio_child *)p;
401 DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags));
403 if ((flags & EVENT_FD_READ) == 0) {
407 status = read_data(child->sockfd, (char *)&child->retval,
408 sizeof(child->retval));
410 if (!NT_STATUS_IS_OK(status)) {
411 DEBUG(1, ("aio child %d died: %s\n", (int)child->pid,
413 child->retval.size = -1;
414 child->retval.ret_errno = EIO;
417 if (child->aiocb == NULL) {
418 DEBUG(1, ("Inactive child died\n"));
423 if (child->cancelled) {
425 child->cancelled = false;
429 if (child->read_cmd && (child->retval.size > 0)) {
430 SMB_ASSERT(child->retval.size <= child->aiocb->aio_nbytes);
431 memcpy((void *)child->aiocb->aio_buf, (void *)child->map->ptr,
435 aio_ex = (struct aio_extra *)child->aiocb->aio_sigevent.sigev_value.sival_ptr;
436 smbd_aio_complete_aio_ex(aio_ex);
440 static int aio_child_destructor(struct aio_child *child)
444 SMB_ASSERT((child->aiocb == NULL) || child->cancelled);
446 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
447 child->pid, child->sockfd));
450 * closing the sockfd makes the child not return from recvmsg() on RHEL
451 * 5.5 so instead force the child to exit by writing bad data to it
453 write(child->sockfd, &c, sizeof(c));
454 close(child->sockfd);
455 DLIST_REMOVE(child->list->children, child);
460 * We have to close all fd's in open files, we might incorrectly hold a system
461 * level share mode on a file.
464 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
467 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
474 static NTSTATUS create_aio_child(struct smbd_server_connection *sconn,
475 struct aio_child_list *children,
477 struct aio_child **presult)
479 struct aio_child *result;
483 fdpair[0] = fdpair[1] = -1;
485 result = talloc_zero(children, struct aio_child);
486 NT_STATUS_HAVE_NO_MEMORY(result);
488 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
489 status = map_nt_error_from_unix(errno);
490 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
494 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
496 result->map = mmap_area_init(result, map_size);
497 if (result->map == NULL) {
498 status = map_nt_error_from_unix(errno);
499 DEBUG(0, ("Could not create mmap area\n"));
503 result->pid = fork();
504 if (result->pid == -1) {
505 status = map_nt_error_from_unix(errno);
506 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
510 if (result->pid == 0) {
512 result->sockfd = fdpair[1];
513 files_forall(sconn, close_fsp_fd, NULL);
514 aio_child_loop(result->sockfd, result->map);
517 DEBUG(10, ("Child %d created with sockfd %d\n",
518 result->pid, fdpair[0]));
520 result->sockfd = fdpair[0];
523 result->sock_event = event_add_fd(server_event_context(), result,
524 result->sockfd, EVENT_FD_READ,
525 handle_aio_completion,
527 if (result->sock_event == NULL) {
528 status = NT_STATUS_NO_MEMORY;
529 DEBUG(0, ("event_add_fd failed\n"));
533 result->list = children;
534 DLIST_ADD(children->children, result);
536 talloc_set_destructor(result, aio_child_destructor);
543 if (fdpair[0] != -1) close(fdpair[0]);
544 if (fdpair[1] != -1) close(fdpair[1]);
550 static NTSTATUS get_idle_child(struct vfs_handle_struct *handle,
551 struct aio_child **pchild)
553 struct aio_child_list *children;
554 struct aio_child *child;
557 children = init_aio_children(handle);
558 if (children == NULL) {
559 return NT_STATUS_NO_MEMORY;
562 for (child = children->children; child != NULL; child = child->next) {
563 if (child->aiocb == NULL) {
570 DEBUG(10, ("no idle child found, creating new one\n"));
572 status = create_aio_child(handle->conn->sconn, children,
574 if (!NT_STATUS_IS_OK(status)) {
575 DEBUG(10, ("create_aio_child failed: %s\n",
581 child->dont_delete = true;
587 static int aio_fork_read(struct vfs_handle_struct *handle,
588 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
590 struct aio_child *child;
595 if (aiocb->aio_nbytes > 128*1024) {
596 /* TODO: support variable buffers */
601 status = get_idle_child(handle, &child);
602 if (!NT_STATUS_IS_OK(status)) {
603 DEBUG(10, ("Could not get an idle child\n"));
607 child->read_cmd = true;
608 child->aiocb = aiocb;
609 child->retval.ret_errno = EINPROGRESS;
612 cmd.n = aiocb->aio_nbytes;
613 cmd.offset = aiocb->aio_offset;
614 cmd.read_cmd = child->read_cmd;
616 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
619 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
621 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
628 static int aio_fork_write(struct vfs_handle_struct *handle,
629 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
631 struct aio_child *child;
636 if (aiocb->aio_nbytes > 128*1024) {
637 /* TODO: support variable buffers */
642 status = get_idle_child(handle, &child);
643 if (!NT_STATUS_IS_OK(status)) {
644 DEBUG(10, ("Could not get an idle child\n"));
648 child->read_cmd = false;
649 child->aiocb = aiocb;
650 child->retval.ret_errno = EINPROGRESS;
652 memcpy((void *)child->map->ptr, (void *)aiocb->aio_buf,
656 cmd.n = aiocb->aio_nbytes;
657 cmd.offset = aiocb->aio_offset;
658 cmd.read_cmd = child->read_cmd;
660 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
663 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
665 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
672 static struct aio_child *aio_fork_find_child(struct vfs_handle_struct *handle,
673 SMB_STRUCT_AIOCB *aiocb)
675 struct aio_child_list *children;
676 struct aio_child *child;
678 children = init_aio_children(handle);
679 if (children == NULL) {
683 for (child = children->children; child != NULL; child = child->next) {
684 if (child->aiocb == aiocb) {
692 static ssize_t aio_fork_return_fn(struct vfs_handle_struct *handle,
693 struct files_struct *fsp,
694 SMB_STRUCT_AIOCB *aiocb)
696 struct aio_child *child = aio_fork_find_child(handle, aiocb);
700 DEBUG(0, ("returning EINVAL\n"));
706 if (child->retval.size == -1) {
707 errno = child->retval.ret_errno;
710 return child->retval.size;
713 static int aio_fork_cancel(struct vfs_handle_struct *handle,
714 struct files_struct *fsp,
715 SMB_STRUCT_AIOCB *aiocb)
717 struct aio_child_list *children;
718 struct aio_child *child;
720 children = init_aio_children(handle);
721 if (children == NULL) {
726 for (child = children->children; child != NULL; child = child->next) {
727 if (child->aiocb == NULL) {
730 if (child->aiocb->aio_fildes != fsp->fh->fd) {
733 if ((aiocb != NULL) && (child->aiocb != aiocb)) {
738 * We let the child do its job, but we discard the result when
742 child->cancelled = true;
748 static int aio_fork_error_fn(struct vfs_handle_struct *handle,
749 struct files_struct *fsp,
750 SMB_STRUCT_AIOCB *aiocb)
752 struct aio_child *child = aio_fork_find_child(handle, aiocb);
759 return child->retval.ret_errno;
762 static void aio_fork_suspend_timed_out(struct tevent_context *event_ctx,
763 struct tevent_timer *te,
767 bool *timed_out = (bool *)private_data;
768 /* Remove this timed event handler. */
773 static int aio_fork_suspend(struct vfs_handle_struct *handle,
774 struct files_struct *fsp,
775 const SMB_STRUCT_AIOCB * const aiocb_array[],
777 const struct timespec *timeout)
779 struct aio_child_list *children = NULL;
780 TALLOC_CTX *frame = talloc_stackframe();
781 struct event_context *ev = NULL;
784 bool timed_out = false;
786 children = init_aio_children(handle);
787 if (children == NULL) {
792 /* This is a blocking call, and has to use a sub-event loop. */
793 ev = event_context_init(frame);
800 struct timeval tv = convert_timespec_to_timeval(*timeout);
801 struct tevent_timer *te = tevent_add_timer(ev,
803 timeval_current_ofs(tv.tv_sec,
805 aio_fork_suspend_timed_out,
813 for (i = 0; i < n; i++) {
814 struct aio_child *child = NULL;
815 const SMB_STRUCT_AIOCB *aiocb = aiocb_array[i];
822 * We're going to cheat here. We know that smbd/aio.c
823 * only calls this when it's waiting for every single
824 * outstanding call to finish on a close, so just wait
825 * individually for each IO to complete. We don't care
826 * what order they finish - only that they all do. JRA.
829 for (child = children->children; child != NULL; child = child->next) {
830 struct tevent_fd *event;
832 if (child->aiocb == NULL) {
835 if (child->aiocb->aio_fildes != fsp->fh->fd) {
838 if (child->aiocb != aiocb) {
842 if (child->aiocb->aio_sigevent.sigev_value.sival_ptr == NULL) {
846 event = event_add_fd(ev,
850 handle_aio_completion,
854 if (tevent_loop_once(ev) == -1) {
863 /* We set child->aiocb to NULL in our hooked
865 if (child->aiocb == NULL) {
880 static struct vfs_fn_pointers vfs_aio_fork_fns = {
881 .aio_read_fn = aio_fork_read,
882 .aio_write_fn = aio_fork_write,
883 .aio_return_fn = aio_fork_return_fn,
884 .aio_cancel_fn = aio_fork_cancel,
885 .aio_error_fn = aio_fork_error_fn,
886 .aio_suspend_fn = aio_fork_suspend,
889 NTSTATUS vfs_aio_fork_init(void);
890 NTSTATUS vfs_aio_fork_init(void)
892 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
893 "aio_fork", &vfs_aio_fork_fns);