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.
29 static int mmap_area_destructor(struct mmap_area *area)
31 munmap((void *)area->ptr, area->size);
35 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
37 struct mmap_area *result;
40 result = talloc(mem_ctx, struct mmap_area);
42 DEBUG(0, ("talloc failed\n"));
46 fd = open("/dev/zero", O_RDWR);
48 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
53 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
54 MAP_SHARED|MAP_FILE, fd, 0);
55 if (result->ptr == MAP_FAILED) {
56 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
63 talloc_set_destructor(result, mmap_area_destructor);
83 struct aio_child_list;
86 struct aio_child *prev, *next;
87 struct aio_child_list *list;
88 SMB_STRUCT_AIOCB *aiocb;
91 struct fd_event *sock_event;
93 struct mmap_area *map; /* ==NULL means write request */
94 bool dont_delete; /* Marked as in use since last cleanup */
99 struct aio_child_list {
100 struct aio_child *children;
101 struct timed_event *cleanup_event;
104 static void free_aio_children(void **p)
109 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
114 #ifndef HAVE_MSGHDR_MSG_CONTROL
118 #ifdef HAVE_MSGHDR_MSG_CONTROL
121 char control[CMSG_SPACE(sizeof(int))];
123 struct cmsghdr *cmptr;
125 msg.msg_control = control_un.control;
126 msg.msg_controllen = sizeof(control_un.control);
128 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
129 msg.msg_accrights = (caddr_t) &newfd;
130 msg.msg_accrightslen = sizeof(int);
132 #error Can not pass file descriptors
139 iov[0].iov_base = (void *)ptr;
140 iov[0].iov_len = nbytes;
144 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
148 #ifdef HAVE_MSGHDR_MSG_CONTROL
149 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
150 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
151 if (cmptr->cmsg_level != SOL_SOCKET) {
152 DEBUG(10, ("control level != SOL_SOCKET"));
156 if (cmptr->cmsg_type != SCM_RIGHTS) {
157 DEBUG(10, ("control type != SCM_RIGHTS"));
161 *recvfd = *((int *) CMSG_DATA(cmptr));
163 *recvfd = -1; /* descriptor was not passed */
166 if (msg.msg_accrightslen == sizeof(int)) {
170 *recvfd = -1; /* descriptor was not passed */
177 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
182 #ifdef HAVE_MSGHDR_MSG_CONTROL
185 char control[CMSG_SPACE(sizeof(int))];
187 struct cmsghdr *cmptr;
190 ZERO_STRUCT(control_un);
192 msg.msg_control = control_un.control;
193 msg.msg_controllen = sizeof(control_un.control);
195 cmptr = CMSG_FIRSTHDR(&msg);
196 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
197 cmptr->cmsg_level = SOL_SOCKET;
198 cmptr->cmsg_type = SCM_RIGHTS;
199 *((int *) CMSG_DATA(cmptr)) = sendfd;
202 msg.msg_accrights = (caddr_t) &sendfd;
203 msg.msg_accrightslen = sizeof(int);
210 iov[0].iov_base = (void *)ptr;
211 iov[0].iov_len = nbytes;
215 return (sendmsg(fd, &msg, 0));
218 static void aio_child_cleanup(struct event_context *event_ctx,
219 struct timed_event *te,
223 struct aio_child_list *list = talloc_get_type_abort(
224 private_data, struct aio_child_list);
225 struct aio_child *child, *next;
227 TALLOC_FREE(list->cleanup_event);
229 for (child = list->children; child != NULL; child = next) {
232 if (child->aiocb != NULL) {
233 DEBUG(10, ("child %d currently active\n",
238 if (child->dont_delete) {
239 DEBUG(10, ("Child %d was active since last cleanup\n",
241 child->dont_delete = false;
245 DEBUG(10, ("Child %d idle for more than 30 seconds, "
246 "deleting\n", (int)child->pid));
252 if (list->children != NULL) {
254 * Re-schedule the next cleanup round
256 list->cleanup_event = event_add_timed(smbd_event_context(), list,
257 timeval_add(&now, 30, 0),
258 aio_child_cleanup, list);
263 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
265 struct aio_child_list *data = NULL;
267 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
268 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
273 data = TALLOC_ZERO_P(NULL, struct aio_child_list);
280 * Regardless of whether the child_list had been around or not, make
281 * sure that we have a cleanup timed event. This timed event will
282 * delete itself when it finds that no children are around anymore.
285 if (data->cleanup_event == NULL) {
286 data->cleanup_event = event_add_timed(smbd_event_context(), data,
287 timeval_current_ofs(30, 0),
288 aio_child_cleanup, data);
289 if (data->cleanup_event == NULL) {
295 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
296 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
297 struct aio_child_list, return False);
303 static void aio_child_loop(int sockfd, struct mmap_area *map)
308 struct rw_cmd cmd_struct;
309 struct rw_ret ret_struct;
311 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
312 if (ret != sizeof(cmd_struct)) {
313 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
318 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
319 cmd_struct.read_cmd ? "read" : "write",
320 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
322 #ifdef ENABLE_BUILD_FARM_HACKS
325 * In the build farm, we want erratic behaviour for
331 * use generate_random_buffer, we just forked from a
332 * common parent state
334 generate_random_buffer(&randval, sizeof(randval));
335 msecs = randval + 20;
336 DEBUG(10, ("delaying for %u msecs\n", msecs));
342 ZERO_STRUCT(ret_struct);
344 if (cmd_struct.read_cmd) {
345 ret_struct.size = sys_pread(
346 fd, (void *)map->ptr, cmd_struct.n,
349 /* This breaks "make test" when run with aio_fork module. */
350 #ifdef ENABLE_BUILD_FARM_HACKS
351 ret_struct.size = MAX(1, ret_struct.size * 0.9);
356 ret_struct.size = sys_pwrite(
357 fd, (void *)map->ptr, cmd_struct.n,
361 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
362 (int)ret_struct.size));
364 if (ret_struct.size == -1) {
365 ret_struct.ret_errno = errno;
369 * Close the fd before telling our parent we're done. The
370 * parent might close and re-open the file very quickly, and
371 * with system-level share modes (GPFS) we would get an
372 * unjustified SHARING_VIOLATION.
376 ret = write_data(sockfd, (char *)&ret_struct,
378 if (ret != sizeof(ret_struct)) {
379 DEBUG(10, ("could not write ret_struct: %s\n",
386 static void handle_aio_completion(struct event_context *event_ctx,
387 struct fd_event *event, uint16 flags,
390 struct aio_extra *aio_ex = NULL;
391 struct aio_child *child = (struct aio_child *)p;
394 DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags));
396 if ((flags & EVENT_FD_READ) == 0) {
400 status = read_data(child->sockfd, (char *)&child->retval,
401 sizeof(child->retval));
403 if (!NT_STATUS_IS_OK(status)) {
404 DEBUG(1, ("aio child %d died: %s\n", (int)child->pid,
406 child->retval.size = -1;
407 child->retval.ret_errno = EIO;
410 if (child->cancelled) {
412 child->cancelled = false;
416 if (child->read_cmd && (child->retval.size > 0)) {
417 SMB_ASSERT(child->retval.size <= child->aiocb->aio_nbytes);
418 memcpy((void *)child->aiocb->aio_buf, (void *)child->map->ptr,
422 aio_ex = (struct aio_extra *)child->aiocb->aio_sigevent.sigev_value.sival_ptr;
423 smbd_aio_complete_aio_ex(aio_ex);
426 static int aio_child_destructor(struct aio_child *child)
430 SMB_ASSERT((child->aiocb == NULL) || child->cancelled);
432 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
433 child->pid, child->sockfd));
436 * closing the sockfd makes the child not return from recvmsg() on RHEL
437 * 5.5 so instead force the child to exit by writing bad data to it
439 write(child->sockfd, &c, sizeof(c));
440 close(child->sockfd);
441 DLIST_REMOVE(child->list->children, child);
446 * We have to close all fd's in open files, we might incorrectly hold a system
447 * level share mode on a file.
450 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
453 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
460 static NTSTATUS create_aio_child(struct smbd_server_connection *sconn,
461 struct aio_child_list *children,
463 struct aio_child **presult)
465 struct aio_child *result;
469 fdpair[0] = fdpair[1] = -1;
471 result = TALLOC_ZERO_P(children, struct aio_child);
472 NT_STATUS_HAVE_NO_MEMORY(result);
474 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
475 status = map_nt_error_from_unix(errno);
476 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
480 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
482 result->map = mmap_area_init(result, map_size);
483 if (result->map == NULL) {
484 status = map_nt_error_from_unix(errno);
485 DEBUG(0, ("Could not create mmap area\n"));
489 result->pid = sys_fork();
490 if (result->pid == -1) {
491 status = map_nt_error_from_unix(errno);
492 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
496 if (result->pid == 0) {
498 result->sockfd = fdpair[1];
499 files_forall(sconn, close_fsp_fd, NULL);
500 aio_child_loop(result->sockfd, result->map);
503 DEBUG(10, ("Child %d created with sockfd %d\n",
504 result->pid, fdpair[0]));
506 result->sockfd = fdpair[0];
509 result->sock_event = event_add_fd(smbd_event_context(), result,
510 result->sockfd, EVENT_FD_READ,
511 handle_aio_completion,
513 if (result->sock_event == NULL) {
514 status = NT_STATUS_NO_MEMORY;
515 DEBUG(0, ("event_add_fd failed\n"));
519 result->list = children;
520 DLIST_ADD(children->children, result);
522 talloc_set_destructor(result, aio_child_destructor);
529 if (fdpair[0] != -1) close(fdpair[0]);
530 if (fdpair[1] != -1) close(fdpair[1]);
536 static NTSTATUS get_idle_child(struct vfs_handle_struct *handle,
537 struct aio_child **pchild)
539 struct aio_child_list *children;
540 struct aio_child *child;
543 children = init_aio_children(handle);
544 if (children == NULL) {
545 return NT_STATUS_NO_MEMORY;
548 for (child = children->children; child != NULL; child = child->next) {
549 if (child->aiocb == NULL) {
556 DEBUG(10, ("no idle child found, creating new one\n"));
558 status = create_aio_child(handle->conn->sconn, children,
560 if (!NT_STATUS_IS_OK(status)) {
561 DEBUG(10, ("create_aio_child failed: %s\n",
567 child->dont_delete = true;
573 static int aio_fork_read(struct vfs_handle_struct *handle,
574 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
576 struct aio_child *child;
581 if (aiocb->aio_nbytes > 128*1024) {
582 /* TODO: support variable buffers */
587 status = get_idle_child(handle, &child);
588 if (!NT_STATUS_IS_OK(status)) {
589 DEBUG(10, ("Could not get an idle child\n"));
593 child->read_cmd = true;
594 child->aiocb = aiocb;
595 child->retval.ret_errno = EINPROGRESS;
598 cmd.n = aiocb->aio_nbytes;
599 cmd.offset = aiocb->aio_offset;
600 cmd.read_cmd = child->read_cmd;
602 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
605 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
607 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
614 static int aio_fork_write(struct vfs_handle_struct *handle,
615 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
617 struct aio_child *child;
622 if (aiocb->aio_nbytes > 128*1024) {
623 /* TODO: support variable buffers */
628 status = get_idle_child(handle, &child);
629 if (!NT_STATUS_IS_OK(status)) {
630 DEBUG(10, ("Could not get an idle child\n"));
634 child->read_cmd = false;
635 child->aiocb = aiocb;
636 child->retval.ret_errno = EINPROGRESS;
638 memcpy((void *)child->map->ptr, (void *)aiocb->aio_buf,
642 cmd.n = aiocb->aio_nbytes;
643 cmd.offset = aiocb->aio_offset;
644 cmd.read_cmd = child->read_cmd;
646 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
649 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
651 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
658 static struct aio_child *aio_fork_find_child(struct vfs_handle_struct *handle,
659 SMB_STRUCT_AIOCB *aiocb)
661 struct aio_child_list *children;
662 struct aio_child *child;
664 children = init_aio_children(handle);
665 if (children == NULL) {
669 for (child = children->children; child != NULL; child = child->next) {
670 if (child->aiocb == aiocb) {
678 static ssize_t aio_fork_return_fn(struct vfs_handle_struct *handle,
679 struct files_struct *fsp,
680 SMB_STRUCT_AIOCB *aiocb)
682 struct aio_child *child = aio_fork_find_child(handle, aiocb);
686 DEBUG(0, ("returning EINVAL\n"));
692 if (child->retval.size == -1) {
693 errno = child->retval.ret_errno;
696 return child->retval.size;
699 static int aio_fork_cancel(struct vfs_handle_struct *handle,
700 struct files_struct *fsp,
701 SMB_STRUCT_AIOCB *aiocb)
703 struct aio_child_list *children;
704 struct aio_child *child;
706 children = init_aio_children(handle);
707 if (children == NULL) {
712 for (child = children->children; child != NULL; child = child->next) {
713 if (child->aiocb == NULL) {
716 if (child->aiocb->aio_fildes != fsp->fh->fd) {
719 if ((aiocb != NULL) && (child->aiocb != aiocb)) {
724 * We let the child do its job, but we discard the result when
728 child->cancelled = true;
734 static int aio_fork_error_fn(struct vfs_handle_struct *handle,
735 struct files_struct *fsp,
736 SMB_STRUCT_AIOCB *aiocb)
738 struct aio_child *child = aio_fork_find_child(handle, aiocb);
745 return child->retval.ret_errno;
748 static void aio_fork_suspend_timed_out(struct tevent_context *event_ctx,
749 struct tevent_timer *te,
753 bool *timed_out = (bool *)private_data;
754 /* Remove this timed event handler. */
759 static int aio_fork_suspend(struct vfs_handle_struct *handle,
760 struct files_struct *fsp,
761 const SMB_STRUCT_AIOCB * const aiocb_array[],
763 const struct timespec *timeout)
765 struct aio_child_list *children = NULL;
766 TALLOC_CTX *frame = talloc_stackframe();
767 struct event_context *ev = NULL;
770 bool timed_out = false;
772 children = init_aio_children(handle);
773 if (children == NULL) {
778 /* This is a blocking call, and has to use a sub-event loop. */
779 ev = event_context_init(frame);
786 struct timeval tv = convert_timespec_to_timeval(*timeout);
787 struct tevent_timer *te = tevent_add_timer(ev,
789 timeval_current_ofs(tv.tv_sec,
791 aio_fork_suspend_timed_out,
799 for (i = 0; i < n; i++) {
800 struct aio_child *child = NULL;
801 const SMB_STRUCT_AIOCB *aiocb = aiocb_array[i];
808 * We're going to cheat here. We know that smbd/aio.c
809 * only calls this when it's waiting for every single
810 * outstanding call to finish on a close, so just wait
811 * individually for each IO to complete. We don't care
812 * what order they finish - only that they all do. JRA.
815 for (child = children->children; child != NULL; child = child->next) {
816 if (child->aiocb == NULL) {
819 if (child->aiocb->aio_fildes != fsp->fh->fd) {
822 if (child->aiocb != aiocb) {
826 if (child->aiocb->aio_sigevent.sigev_value.sival_ptr == NULL) {
830 /* We're never using this event on the
831 * main event context again... */
832 TALLOC_FREE(child->sock_event);
834 child->sock_event = event_add_fd(ev,
838 handle_aio_completion,
842 if (tevent_loop_once(ev) == -1) {
851 /* We set child->aiocb to NULL in our hooked
853 if (child->aiocb == NULL) {
868 static struct vfs_fn_pointers vfs_aio_fork_fns = {
869 .aio_read = aio_fork_read,
870 .aio_write = aio_fork_write,
871 .aio_return_fn = aio_fork_return_fn,
872 .aio_cancel = aio_fork_cancel,
873 .aio_error_fn = aio_fork_error_fn,
874 .aio_suspend = aio_fork_suspend,
877 NTSTATUS vfs_aio_fork_init(void);
878 NTSTATUS vfs_aio_fork_init(void)
880 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
881 "aio_fork", &vfs_aio_fork_fns);