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/shmem.h"
30 static int mmap_area_destructor(struct mmap_area *area)
32 munmap((void *)area->ptr, area->size);
36 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
38 struct mmap_area *result;
41 result = talloc(mem_ctx, struct mmap_area);
43 DEBUG(0, ("talloc failed\n"));
47 fd = open("/dev/zero", O_RDWR);
49 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
54 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
55 MAP_SHARED|MAP_FILE, fd, 0);
56 if (result->ptr == MAP_FAILED) {
57 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
64 talloc_set_destructor(result, mmap_area_destructor);
84 struct aio_child_list;
87 struct aio_child *prev, *next;
88 struct aio_child_list *list;
89 SMB_STRUCT_AIOCB *aiocb;
92 struct fd_event *sock_event;
94 struct mmap_area *map; /* ==NULL means write request */
95 bool dont_delete; /* Marked as in use since last cleanup */
100 struct aio_child_list {
101 struct aio_child *children;
102 struct timed_event *cleanup_event;
105 static void free_aio_children(void **p)
110 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
115 #ifndef HAVE_MSGHDR_MSG_CONTROL
119 #ifdef HAVE_MSGHDR_MSG_CONTROL
122 char control[CMSG_SPACE(sizeof(int))];
124 struct cmsghdr *cmptr;
126 msg.msg_control = control_un.control;
127 msg.msg_controllen = sizeof(control_un.control);
129 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
130 msg.msg_accrights = (caddr_t) &newfd;
131 msg.msg_accrightslen = sizeof(int);
133 #error Can not pass file descriptors
140 iov[0].iov_base = (void *)ptr;
141 iov[0].iov_len = nbytes;
145 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
149 #ifdef HAVE_MSGHDR_MSG_CONTROL
150 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
151 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
152 if (cmptr->cmsg_level != SOL_SOCKET) {
153 DEBUG(10, ("control level != SOL_SOCKET"));
157 if (cmptr->cmsg_type != SCM_RIGHTS) {
158 DEBUG(10, ("control type != SCM_RIGHTS"));
162 *recvfd = *((int *) CMSG_DATA(cmptr));
164 *recvfd = -1; /* descriptor was not passed */
167 if (msg.msg_accrightslen == sizeof(int)) {
171 *recvfd = -1; /* descriptor was not passed */
178 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
183 #ifdef HAVE_MSGHDR_MSG_CONTROL
186 char control[CMSG_SPACE(sizeof(int))];
188 struct cmsghdr *cmptr;
191 ZERO_STRUCT(control_un);
193 msg.msg_control = control_un.control;
194 msg.msg_controllen = sizeof(control_un.control);
196 cmptr = CMSG_FIRSTHDR(&msg);
197 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
198 cmptr->cmsg_level = SOL_SOCKET;
199 cmptr->cmsg_type = SCM_RIGHTS;
200 *((int *) CMSG_DATA(cmptr)) = sendfd;
203 msg.msg_accrights = (caddr_t) &sendfd;
204 msg.msg_accrightslen = sizeof(int);
211 iov[0].iov_base = (void *)ptr;
212 iov[0].iov_len = nbytes;
216 return (sendmsg(fd, &msg, 0));
219 static void aio_child_cleanup(struct event_context *event_ctx,
220 struct timed_event *te,
224 struct aio_child_list *list = talloc_get_type_abort(
225 private_data, struct aio_child_list);
226 struct aio_child *child, *next;
228 TALLOC_FREE(list->cleanup_event);
230 for (child = list->children; child != NULL; child = next) {
233 if (child->aiocb != NULL) {
234 DEBUG(10, ("child %d currently active\n",
239 if (child->dont_delete) {
240 DEBUG(10, ("Child %d was active since last cleanup\n",
242 child->dont_delete = false;
246 DEBUG(10, ("Child %d idle for more than 30 seconds, "
247 "deleting\n", (int)child->pid));
253 if (list->children != NULL) {
255 * Re-schedule the next cleanup round
257 list->cleanup_event = event_add_timed(smbd_event_context(), list,
258 timeval_add(&now, 30, 0),
259 aio_child_cleanup, list);
264 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
266 struct aio_child_list *data = NULL;
268 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
269 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
274 data = TALLOC_ZERO_P(NULL, struct aio_child_list);
281 * Regardless of whether the child_list had been around or not, make
282 * sure that we have a cleanup timed event. This timed event will
283 * delete itself when it finds that no children are around anymore.
286 if (data->cleanup_event == NULL) {
287 data->cleanup_event = event_add_timed(smbd_event_context(), data,
288 timeval_current_ofs(30, 0),
289 aio_child_cleanup, data);
290 if (data->cleanup_event == NULL) {
296 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
297 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
298 struct aio_child_list, return False);
304 static void aio_child_loop(int sockfd, struct mmap_area *map)
309 struct rw_cmd cmd_struct;
310 struct rw_ret ret_struct;
312 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
313 if (ret != sizeof(cmd_struct)) {
314 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
319 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
320 cmd_struct.read_cmd ? "read" : "write",
321 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
323 #ifdef ENABLE_BUILD_FARM_HACKS
326 * In the build farm, we want erratic behaviour for
332 * use generate_random_buffer, we just forked from a
333 * common parent state
335 generate_random_buffer(&randval, sizeof(randval));
336 msecs = randval + 20;
337 DEBUG(10, ("delaying for %u msecs\n", msecs));
343 ZERO_STRUCT(ret_struct);
345 if (cmd_struct.read_cmd) {
346 ret_struct.size = sys_pread(
347 fd, (void *)map->ptr, cmd_struct.n,
350 /* This breaks "make test" when run with aio_fork module. */
351 #ifdef ENABLE_BUILD_FARM_HACKS
352 ret_struct.size = MAX(1, ret_struct.size * 0.9);
357 ret_struct.size = sys_pwrite(
358 fd, (void *)map->ptr, cmd_struct.n,
362 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
363 (int)ret_struct.size));
365 if (ret_struct.size == -1) {
366 ret_struct.ret_errno = errno;
370 * Close the fd before telling our parent we're done. The
371 * parent might close and re-open the file very quickly, and
372 * with system-level share modes (GPFS) we would get an
373 * unjustified SHARING_VIOLATION.
377 ret = write_data(sockfd, (char *)&ret_struct,
379 if (ret != sizeof(ret_struct)) {
380 DEBUG(10, ("could not write ret_struct: %s\n",
387 static void handle_aio_completion(struct event_context *event_ctx,
388 struct fd_event *event, uint16 flags,
391 struct aio_extra *aio_ex = NULL;
392 struct aio_child *child = (struct aio_child *)p;
395 DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags));
397 if ((flags & EVENT_FD_READ) == 0) {
401 status = read_data(child->sockfd, (char *)&child->retval,
402 sizeof(child->retval));
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(1, ("aio child %d died: %s\n", (int)child->pid,
407 child->retval.size = -1;
408 child->retval.ret_errno = EIO;
411 if (child->aiocb == NULL) {
412 DEBUG(1, ("Inactive child died\n"));
417 if (child->cancelled) {
419 child->cancelled = false;
423 if (child->read_cmd && (child->retval.size > 0)) {
424 SMB_ASSERT(child->retval.size <= child->aiocb->aio_nbytes);
425 memcpy((void *)child->aiocb->aio_buf, (void *)child->map->ptr,
429 aio_ex = (struct aio_extra *)child->aiocb->aio_sigevent.sigev_value.sival_ptr;
430 smbd_aio_complete_aio_ex(aio_ex);
433 static int aio_child_destructor(struct aio_child *child)
437 SMB_ASSERT((child->aiocb == NULL) || child->cancelled);
439 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
440 child->pid, child->sockfd));
443 * closing the sockfd makes the child not return from recvmsg() on RHEL
444 * 5.5 so instead force the child to exit by writing bad data to it
446 write(child->sockfd, &c, sizeof(c));
447 close(child->sockfd);
448 DLIST_REMOVE(child->list->children, child);
453 * We have to close all fd's in open files, we might incorrectly hold a system
454 * level share mode on a file.
457 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
460 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
467 static NTSTATUS create_aio_child(struct smbd_server_connection *sconn,
468 struct aio_child_list *children,
470 struct aio_child **presult)
472 struct aio_child *result;
476 fdpair[0] = fdpair[1] = -1;
478 result = TALLOC_ZERO_P(children, struct aio_child);
479 NT_STATUS_HAVE_NO_MEMORY(result);
481 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
482 status = map_nt_error_from_unix(errno);
483 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
487 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
489 result->map = mmap_area_init(result, map_size);
490 if (result->map == NULL) {
491 status = map_nt_error_from_unix(errno);
492 DEBUG(0, ("Could not create mmap area\n"));
496 result->pid = sys_fork();
497 if (result->pid == -1) {
498 status = map_nt_error_from_unix(errno);
499 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
503 if (result->pid == 0) {
505 result->sockfd = fdpair[1];
506 files_forall(sconn, close_fsp_fd, NULL);
507 aio_child_loop(result->sockfd, result->map);
510 DEBUG(10, ("Child %d created with sockfd %d\n",
511 result->pid, fdpair[0]));
513 result->sockfd = fdpair[0];
516 result->sock_event = event_add_fd(smbd_event_context(), result,
517 result->sockfd, EVENT_FD_READ,
518 handle_aio_completion,
520 if (result->sock_event == NULL) {
521 status = NT_STATUS_NO_MEMORY;
522 DEBUG(0, ("event_add_fd failed\n"));
526 result->list = children;
527 DLIST_ADD(children->children, result);
529 talloc_set_destructor(result, aio_child_destructor);
536 if (fdpair[0] != -1) close(fdpair[0]);
537 if (fdpair[1] != -1) close(fdpair[1]);
543 static NTSTATUS get_idle_child(struct vfs_handle_struct *handle,
544 struct aio_child **pchild)
546 struct aio_child_list *children;
547 struct aio_child *child;
550 children = init_aio_children(handle);
551 if (children == NULL) {
552 return NT_STATUS_NO_MEMORY;
555 for (child = children->children; child != NULL; child = child->next) {
556 if (child->aiocb == NULL) {
563 DEBUG(10, ("no idle child found, creating new one\n"));
565 status = create_aio_child(handle->conn->sconn, children,
567 if (!NT_STATUS_IS_OK(status)) {
568 DEBUG(10, ("create_aio_child failed: %s\n",
574 child->dont_delete = true;
580 static int aio_fork_read(struct vfs_handle_struct *handle,
581 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
583 struct aio_child *child;
588 if (aiocb->aio_nbytes > 128*1024) {
589 /* TODO: support variable buffers */
594 status = get_idle_child(handle, &child);
595 if (!NT_STATUS_IS_OK(status)) {
596 DEBUG(10, ("Could not get an idle child\n"));
600 child->read_cmd = true;
601 child->aiocb = aiocb;
602 child->retval.ret_errno = EINPROGRESS;
605 cmd.n = aiocb->aio_nbytes;
606 cmd.offset = aiocb->aio_offset;
607 cmd.read_cmd = child->read_cmd;
609 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
612 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
614 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
621 static int aio_fork_write(struct vfs_handle_struct *handle,
622 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
624 struct aio_child *child;
629 if (aiocb->aio_nbytes > 128*1024) {
630 /* TODO: support variable buffers */
635 status = get_idle_child(handle, &child);
636 if (!NT_STATUS_IS_OK(status)) {
637 DEBUG(10, ("Could not get an idle child\n"));
641 child->read_cmd = false;
642 child->aiocb = aiocb;
643 child->retval.ret_errno = EINPROGRESS;
645 memcpy((void *)child->map->ptr, (void *)aiocb->aio_buf,
649 cmd.n = aiocb->aio_nbytes;
650 cmd.offset = aiocb->aio_offset;
651 cmd.read_cmd = child->read_cmd;
653 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
656 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
658 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
665 static struct aio_child *aio_fork_find_child(struct vfs_handle_struct *handle,
666 SMB_STRUCT_AIOCB *aiocb)
668 struct aio_child_list *children;
669 struct aio_child *child;
671 children = init_aio_children(handle);
672 if (children == NULL) {
676 for (child = children->children; child != NULL; child = child->next) {
677 if (child->aiocb == aiocb) {
685 static ssize_t aio_fork_return_fn(struct vfs_handle_struct *handle,
686 struct files_struct *fsp,
687 SMB_STRUCT_AIOCB *aiocb)
689 struct aio_child *child = aio_fork_find_child(handle, aiocb);
693 DEBUG(0, ("returning EINVAL\n"));
699 if (child->retval.size == -1) {
700 errno = child->retval.ret_errno;
703 return child->retval.size;
706 static int aio_fork_cancel(struct vfs_handle_struct *handle,
707 struct files_struct *fsp,
708 SMB_STRUCT_AIOCB *aiocb)
710 struct aio_child_list *children;
711 struct aio_child *child;
713 children = init_aio_children(handle);
714 if (children == NULL) {
719 for (child = children->children; child != NULL; child = child->next) {
720 if (child->aiocb == NULL) {
723 if (child->aiocb->aio_fildes != fsp->fh->fd) {
726 if ((aiocb != NULL) && (child->aiocb != aiocb)) {
731 * We let the child do its job, but we discard the result when
735 child->cancelled = true;
741 static int aio_fork_error_fn(struct vfs_handle_struct *handle,
742 struct files_struct *fsp,
743 SMB_STRUCT_AIOCB *aiocb)
745 struct aio_child *child = aio_fork_find_child(handle, aiocb);
752 return child->retval.ret_errno;
755 static void aio_fork_suspend_timed_out(struct tevent_context *event_ctx,
756 struct tevent_timer *te,
760 bool *timed_out = (bool *)private_data;
761 /* Remove this timed event handler. */
766 static int aio_fork_suspend(struct vfs_handle_struct *handle,
767 struct files_struct *fsp,
768 const SMB_STRUCT_AIOCB * const aiocb_array[],
770 const struct timespec *timeout)
772 struct aio_child_list *children = NULL;
773 TALLOC_CTX *frame = talloc_stackframe();
774 struct event_context *ev = NULL;
777 bool timed_out = false;
779 children = init_aio_children(handle);
780 if (children == NULL) {
785 /* This is a blocking call, and has to use a sub-event loop. */
786 ev = event_context_init(frame);
793 struct timeval tv = convert_timespec_to_timeval(*timeout);
794 struct tevent_timer *te = tevent_add_timer(ev,
796 timeval_current_ofs(tv.tv_sec,
798 aio_fork_suspend_timed_out,
806 for (i = 0; i < n; i++) {
807 struct aio_child *child = NULL;
808 const SMB_STRUCT_AIOCB *aiocb = aiocb_array[i];
815 * We're going to cheat here. We know that smbd/aio.c
816 * only calls this when it's waiting for every single
817 * outstanding call to finish on a close, so just wait
818 * individually for each IO to complete. We don't care
819 * what order they finish - only that they all do. JRA.
822 for (child = children->children; child != NULL; child = child->next) {
823 if (child->aiocb == NULL) {
826 if (child->aiocb->aio_fildes != fsp->fh->fd) {
829 if (child->aiocb != aiocb) {
833 if (child->aiocb->aio_sigevent.sigev_value.sival_ptr == NULL) {
837 /* We're never using this event on the
838 * main event context again... */
839 TALLOC_FREE(child->sock_event);
841 child->sock_event = event_add_fd(ev,
845 handle_aio_completion,
849 if (tevent_loop_once(ev) == -1) {
858 /* We set child->aiocb to NULL in our hooked
860 if (child->aiocb == NULL) {
875 static struct vfs_fn_pointers vfs_aio_fork_fns = {
876 .aio_read = aio_fork_read,
877 .aio_write = aio_fork_write,
878 .aio_return_fn = aio_fork_return_fn,
879 .aio_cancel = aio_fork_cancel,
880 .aio_error_fn = aio_fork_error_fn,
881 .aio_suspend = aio_fork_suspend,
884 NTSTATUS vfs_aio_fork_init(void);
885 NTSTATUS vfs_aio_fork_init(void)
887 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
888 "aio_fork", &vfs_aio_fork_fns);