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"
26 #include "smbd/globals.h"
27 #include "lib/async_req/async_sock.h"
28 #include "lib/util/tevent_unix.h"
29 #include "lib/sys_rw.h"
30 #include "lib/sys_rw_data.h"
31 #include "lib/msghdr.h"
33 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
34 # error Can not pass file descriptors
43 struct aio_fork_config {
44 bool erratic_testing_mode;
52 static int mmap_area_destructor(struct mmap_area *area)
54 munmap((void *)area->ptr, area->size);
58 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
60 struct mmap_area *result;
63 result = talloc(mem_ctx, struct mmap_area);
65 DEBUG(0, ("talloc failed\n"));
69 fd = open("/dev/zero", O_RDWR);
71 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
76 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
77 MAP_SHARED|MAP_FILE, fd, 0);
79 if (result->ptr == MAP_FAILED) {
80 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
85 talloc_set_destructor(result, mmap_area_destructor);
100 static const char *cmd_type_str(enum cmd_type cmd)
115 result = "<UNKNOWN>";
125 bool erratic_testing_mode;
133 struct aio_child_list;
136 struct aio_child *prev, *next;
137 struct aio_child_list *list;
140 struct mmap_area *map;
141 bool dont_delete; /* Marked as in use since last cleanup */
145 struct aio_child_list {
146 struct aio_child *children;
147 struct tevent_timer *cleanup_event;
150 static void free_aio_children(void **p)
155 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
160 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
164 msg.msg_accrights = (caddr_t) &newfd;
165 msg.msg_accrightslen = sizeof(int);
170 char control[CMSG_SPACE(sizeof(int))];
172 struct cmsghdr *cmptr;
175 ZERO_STRUCT(control_un);
177 msg.msg_control = control_un.control;
178 msg.msg_controllen = sizeof(control_un.control);
184 iov[0].iov_base = (void *)ptr;
185 iov[0].iov_len = nbytes;
189 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
193 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
194 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
195 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
196 if (cmptr->cmsg_level != SOL_SOCKET) {
197 DEBUG(10, ("control level != SOL_SOCKET"));
201 if (cmptr->cmsg_type != SCM_RIGHTS) {
202 DEBUG(10, ("control type != SCM_RIGHTS"));
206 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
208 *recvfd = -1; /* descriptor was not passed */
211 if (msg.msg_accrightslen == sizeof(int)) {
215 *recvfd = -1; /* descriptor was not passed */
222 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
225 size_t bufsize = msghdr_prep_fds(NULL, NULL, 0, &sendfd, 1);
226 uint8_t buf[bufsize];
229 msghdr_prep_fds(&msg, buf, bufsize, &sendfd, 1);
233 iov.iov_base = (void *)ptr;
234 iov.iov_len = nbytes;
238 return (sendmsg(fd, &msg, 0));
241 static void aio_child_cleanup(struct tevent_context *event_ctx,
242 struct tevent_timer *te,
246 struct aio_child_list *list = talloc_get_type_abort(
247 private_data, struct aio_child_list);
248 struct aio_child *child, *next;
250 TALLOC_FREE(list->cleanup_event);
252 for (child = list->children; child != NULL; child = next) {
256 DEBUG(10, ("child %d currently active\n",
261 if (child->dont_delete) {
262 DEBUG(10, ("Child %d was active since last cleanup\n",
264 child->dont_delete = false;
268 DEBUG(10, ("Child %d idle for more than 30 seconds, "
269 "deleting\n", (int)child->pid));
275 if (list->children != NULL) {
277 * Re-schedule the next cleanup round
279 list->cleanup_event = tevent_add_timer(server_event_context(), list,
280 timeval_add(&now, 30, 0),
281 aio_child_cleanup, list);
286 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
288 struct aio_child_list *data = NULL;
290 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
291 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
296 data = talloc_zero(NULL, struct aio_child_list);
303 * Regardless of whether the child_list had been around or not, make
304 * sure that we have a cleanup timed event. This timed event will
305 * delete itself when it finds that no children are around anymore.
308 if (data->cleanup_event == NULL) {
309 data->cleanup_event = tevent_add_timer(server_event_context(), data,
310 timeval_current_ofs(30, 0),
311 aio_child_cleanup, data);
312 if (data->cleanup_event == NULL) {
318 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
319 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
320 struct aio_child_list, return False);
326 static void aio_child_loop(int sockfd, struct mmap_area *map)
331 struct rw_cmd cmd_struct;
332 struct rw_ret ret_struct;
334 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
335 if (ret != sizeof(cmd_struct)) {
336 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
341 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
342 cmd_type_str(cmd_struct.cmd),
343 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
345 if (cmd_struct.erratic_testing_mode) {
347 * For developer testing, we want erratic behaviour for
353 * use generate_random_buffer, we just forked from a
354 * common parent state
356 generate_random_buffer(&randval, sizeof(randval));
357 msecs = randval + 20;
358 DEBUG(10, ("delaying for %u msecs\n", msecs));
362 ZERO_STRUCT(ret_struct);
364 switch (cmd_struct.cmd) {
366 ret_struct.size = sys_pread(
367 fd, (void *)map->ptr, cmd_struct.n,
370 /* This breaks "make test" when run with aio_fork module. */
372 ret_struct.size = MAX(1, ret_struct.size * 0.9);
377 ret_struct.size = sys_pwrite(
378 fd, (void *)map->ptr, cmd_struct.n,
382 ret_struct.size = fsync(fd);
385 ret_struct.size = -1;
389 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
390 (int)ret_struct.size));
392 if (ret_struct.size == -1) {
393 ret_struct.ret_errno = errno;
397 * Close the fd before telling our parent we're done. The
398 * parent might close and re-open the file very quickly, and
399 * with system-level share modes (GPFS) we would get an
400 * unjustified SHARING_VIOLATION.
404 ret = write_data(sockfd, (char *)&ret_struct,
406 if (ret != sizeof(ret_struct)) {
407 DEBUG(10, ("could not write ret_struct: %s\n",
414 static int aio_child_destructor(struct aio_child *child)
418 SMB_ASSERT(!child->busy);
420 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
421 child->pid, child->sockfd));
424 * closing the sockfd makes the child not return from recvmsg() on RHEL
425 * 5.5 so instead force the child to exit by writing bad data to it
427 write(child->sockfd, &c, sizeof(c));
428 close(child->sockfd);
429 DLIST_REMOVE(child->list->children, child);
434 * We have to close all fd's in open files, we might incorrectly hold a system
435 * level share mode on a file.
438 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
441 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
448 static int create_aio_child(struct smbd_server_connection *sconn,
449 struct aio_child_list *children,
451 struct aio_child **presult)
453 struct aio_child *result;
457 fdpair[0] = fdpair[1] = -1;
459 result = talloc_zero(children, struct aio_child);
460 if (result == NULL) {
464 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
466 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
470 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
472 result->map = mmap_area_init(result, map_size);
473 if (result->map == NULL) {
475 DEBUG(0, ("Could not create mmap area\n"));
479 result->pid = fork();
480 if (result->pid == -1) {
482 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
486 if (result->pid == 0) {
488 result->sockfd = fdpair[1];
489 files_forall(sconn, close_fsp_fd, NULL);
490 aio_child_loop(result->sockfd, result->map);
493 DEBUG(10, ("Child %d created with sockfd %d\n",
494 result->pid, fdpair[0]));
496 result->sockfd = fdpair[0];
499 result->list = children;
500 DLIST_ADD(children->children, result);
502 talloc_set_destructor(result, aio_child_destructor);
509 if (fdpair[0] != -1) close(fdpair[0]);
510 if (fdpair[1] != -1) close(fdpair[1]);
516 static int get_idle_child(struct vfs_handle_struct *handle,
517 struct aio_child **pchild)
519 struct aio_child_list *children;
520 struct aio_child *child;
522 children = init_aio_children(handle);
523 if (children == NULL) {
527 for (child = children->children; child != NULL; child = child->next) {
536 DEBUG(10, ("no idle child found, creating new one\n"));
538 ret = create_aio_child(handle->conn->sconn, children,
541 DEBUG(10, ("create_aio_child failed: %s\n",
547 child->dont_delete = true;
554 struct aio_fork_pread_state {
555 struct aio_child *child;
560 static void aio_fork_pread_done(struct tevent_req *subreq);
562 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
564 struct tevent_context *ev,
565 struct files_struct *fsp,
567 size_t n, off_t offset)
569 struct tevent_req *req, *subreq;
570 struct aio_fork_pread_state *state;
574 struct aio_fork_config *config;
576 SMB_VFS_HANDLE_GET_DATA(handle, config,
577 struct aio_fork_config,
580 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
586 /* TODO: support variable buffers */
587 tevent_req_error(req, EINVAL);
588 return tevent_req_post(req, ev);
591 err = get_idle_child(handle, &state->child);
593 tevent_req_error(req, err);
594 return tevent_req_post(req, ev);
601 cmd.erratic_testing_mode = config->erratic_testing_mode;
603 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
604 (int)state->child->pid));
607 * Not making this async. We're writing into an empty unix
608 * domain socket. This should never block.
610 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
615 TALLOC_FREE(state->child);
617 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
618 tevent_req_error(req, err);
619 return tevent_req_post(req, ev);
622 subreq = read_packet_send(state, ev, state->child->sockfd,
623 sizeof(struct rw_ret), NULL, NULL);
624 if (tevent_req_nomem(subreq, req)) {
625 TALLOC_FREE(state->child); /* we sent sth down */
626 return tevent_req_post(req, ev);
628 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
632 static void aio_fork_pread_done(struct tevent_req *subreq)
634 struct tevent_req *req = tevent_req_callback_data(
635 subreq, struct tevent_req);
636 struct aio_fork_pread_state *state = tevent_req_data(
637 req, struct aio_fork_pread_state);
641 struct rw_ret *retbuf;
643 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
646 TALLOC_FREE(state->child);
647 tevent_req_error(req, err);
651 state->child->busy = false;
653 retbuf = (struct rw_ret *)buf;
654 state->ret = retbuf->size;
655 state->err = retbuf->ret_errno;
656 tevent_req_done(req);
659 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
661 struct aio_fork_pread_state *state = tevent_req_data(
662 req, struct aio_fork_pread_state);
664 if (tevent_req_is_unix_error(req, err)) {
667 if (state->ret == -1) {
673 struct aio_fork_pwrite_state {
674 struct aio_child *child;
679 static void aio_fork_pwrite_done(struct tevent_req *subreq);
681 static struct tevent_req *aio_fork_pwrite_send(
682 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
683 struct tevent_context *ev, struct files_struct *fsp,
684 const void *data, size_t n, off_t offset)
686 struct tevent_req *req, *subreq;
687 struct aio_fork_pwrite_state *state;
691 struct aio_fork_config *config;
692 SMB_VFS_HANDLE_GET_DATA(handle, config,
693 struct aio_fork_config,
696 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
702 /* TODO: support variable buffers */
703 tevent_req_error(req, EINVAL);
704 return tevent_req_post(req, ev);
707 err = get_idle_child(handle, &state->child);
709 tevent_req_error(req, err);
710 return tevent_req_post(req, ev);
717 cmd.erratic_testing_mode = config->erratic_testing_mode;
719 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
720 (int)state->child->pid));
723 * Not making this async. We're writing into an empty unix
724 * domain socket. This should never block.
726 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
731 TALLOC_FREE(state->child);
733 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
734 tevent_req_error(req, err);
735 return tevent_req_post(req, ev);
738 subreq = read_packet_send(state, ev, state->child->sockfd,
739 sizeof(struct rw_ret), NULL, NULL);
740 if (tevent_req_nomem(subreq, req)) {
741 TALLOC_FREE(state->child); /* we sent sth down */
742 return tevent_req_post(req, ev);
744 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
748 static void aio_fork_pwrite_done(struct tevent_req *subreq)
750 struct tevent_req *req = tevent_req_callback_data(
751 subreq, struct tevent_req);
752 struct aio_fork_pwrite_state *state = tevent_req_data(
753 req, struct aio_fork_pwrite_state);
757 struct rw_ret *retbuf;
759 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
762 TALLOC_FREE(state->child);
763 tevent_req_error(req, err);
767 state->child->busy = false;
769 retbuf = (struct rw_ret *)buf;
770 state->ret = retbuf->size;
771 state->err = retbuf->ret_errno;
772 tevent_req_done(req);
775 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
777 struct aio_fork_pwrite_state *state = tevent_req_data(
778 req, struct aio_fork_pwrite_state);
780 if (tevent_req_is_unix_error(req, err)) {
783 if (state->ret == -1) {
789 struct aio_fork_fsync_state {
790 struct aio_child *child;
795 static void aio_fork_fsync_done(struct tevent_req *subreq);
797 static struct tevent_req *aio_fork_fsync_send(
798 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
799 struct tevent_context *ev, struct files_struct *fsp)
801 struct tevent_req *req, *subreq;
802 struct aio_fork_fsync_state *state;
806 struct aio_fork_config *config;
808 SMB_VFS_HANDLE_GET_DATA(handle, config,
809 struct aio_fork_config,
812 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
817 err = get_idle_child(handle, &state->child);
819 tevent_req_error(req, err);
820 return tevent_req_post(req, ev);
825 cmd.erratic_testing_mode = config->erratic_testing_mode;
827 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
828 (int)state->child->pid));
831 * Not making this async. We're writing into an empty unix
832 * domain socket. This should never block.
834 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
839 TALLOC_FREE(state->child);
841 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
842 tevent_req_error(req, err);
843 return tevent_req_post(req, ev);
846 subreq = read_packet_send(state, ev, state->child->sockfd,
847 sizeof(struct rw_ret), NULL, NULL);
848 if (tevent_req_nomem(subreq, req)) {
849 TALLOC_FREE(state->child); /* we sent sth down */
850 return tevent_req_post(req, ev);
852 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
856 static void aio_fork_fsync_done(struct tevent_req *subreq)
858 struct tevent_req *req = tevent_req_callback_data(
859 subreq, struct tevent_req);
860 struct aio_fork_fsync_state *state = tevent_req_data(
861 req, struct aio_fork_fsync_state);
865 struct rw_ret *retbuf;
867 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
870 TALLOC_FREE(state->child);
871 tevent_req_error(req, err);
875 state->child->busy = false;
877 retbuf = (struct rw_ret *)buf;
878 state->ret = retbuf->size;
879 state->err = retbuf->ret_errno;
880 tevent_req_done(req);
883 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
885 struct aio_fork_fsync_state *state = tevent_req_data(
886 req, struct aio_fork_fsync_state);
888 if (tevent_req_is_unix_error(req, err)) {
891 if (state->ret == -1) {
897 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
901 struct aio_fork_config *config;
902 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
908 config = talloc_zero(handle->conn, struct aio_fork_config);
910 SMB_VFS_NEXT_DISCONNECT(handle);
911 DEBUG(0, ("talloc_zero() failed\n"));
915 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
916 "erratic_testing_mode", false);
918 SMB_VFS_HANDLE_SET_DATA(handle, config,
919 NULL, struct aio_fork_config,
922 /*********************************************************************
923 * How many threads to initialize ?
924 * 100 per process seems insane as a default until you realize that
925 * (a) Threads terminate after 1 second when idle.
926 * (b) Throttling is done in SMB2 via the crediting algorithm.
927 * (c) SMB1 clients are limited to max_mux (50) outstanding
928 * requests and Windows clients don't use this anyway.
929 * Essentially we want this to be unlimited unless smb.conf
931 *********************************************************************/
932 aio_pending_size = 100;
936 static struct vfs_fn_pointers vfs_aio_fork_fns = {
937 .connect_fn = aio_fork_connect,
938 .pread_send_fn = aio_fork_pread_send,
939 .pread_recv_fn = aio_fork_pread_recv,
940 .pwrite_send_fn = aio_fork_pwrite_send,
941 .pwrite_recv_fn = aio_fork_pwrite_recv,
942 .fsync_send_fn = aio_fork_fsync_send,
943 .fsync_recv_fn = aio_fork_fsync_recv,
946 NTSTATUS vfs_aio_fork_init(void);
947 NTSTATUS vfs_aio_fork_init(void)
949 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
950 "aio_fork", &vfs_aio_fork_fns);