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"
30 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
31 # error Can not pass file descriptors
40 struct aio_fork_config {
41 bool erratic_testing_mode;
49 static int mmap_area_destructor(struct mmap_area *area)
51 munmap((void *)area->ptr, area->size);
55 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
57 struct mmap_area *result;
60 result = talloc(mem_ctx, struct mmap_area);
62 DEBUG(0, ("talloc failed\n"));
66 fd = open("/dev/zero", O_RDWR);
68 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
73 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
74 MAP_SHARED|MAP_FILE, fd, 0);
76 if (result->ptr == MAP_FAILED) {
77 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
82 talloc_set_destructor(result, mmap_area_destructor);
97 static const char *cmd_type_str(enum cmd_type cmd)
112 result = "<UNKNOWN>";
122 bool erratic_testing_mode;
130 struct aio_child_list;
133 struct aio_child *prev, *next;
134 struct aio_child_list *list;
137 struct mmap_area *map;
138 bool dont_delete; /* Marked as in use since last cleanup */
142 struct aio_child_list {
143 struct aio_child *children;
144 struct tevent_timer *cleanup_event;
147 static void free_aio_children(void **p)
152 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
157 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
161 msg.msg_accrights = (caddr_t) &newfd;
162 msg.msg_accrightslen = sizeof(int);
167 char control[CMSG_SPACE(sizeof(int))];
169 struct cmsghdr *cmptr;
172 ZERO_STRUCT(control_un);
174 msg.msg_control = control_un.control;
175 msg.msg_controllen = sizeof(control_un.control);
181 iov[0].iov_base = (void *)ptr;
182 iov[0].iov_len = nbytes;
186 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
190 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
191 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
192 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
193 if (cmptr->cmsg_level != SOL_SOCKET) {
194 DEBUG(10, ("control level != SOL_SOCKET"));
198 if (cmptr->cmsg_type != SCM_RIGHTS) {
199 DEBUG(10, ("control type != SCM_RIGHTS"));
203 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
205 *recvfd = -1; /* descriptor was not passed */
208 if (msg.msg_accrightslen == sizeof(int)) {
212 *recvfd = -1; /* descriptor was not passed */
219 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
224 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
227 char control[CMSG_SPACE(sizeof(int))];
229 struct cmsghdr *cmptr;
232 ZERO_STRUCT(control_un);
234 msg.msg_control = control_un.control;
235 msg.msg_controllen = sizeof(control_un.control);
237 cmptr = CMSG_FIRSTHDR(&msg);
238 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
239 cmptr->cmsg_level = SOL_SOCKET;
240 cmptr->cmsg_type = SCM_RIGHTS;
241 memcpy(CMSG_DATA(cmptr), &sendfd, sizeof(sendfd));
244 msg.msg_accrights = (caddr_t) &sendfd;
245 msg.msg_accrightslen = sizeof(int);
252 iov[0].iov_base = (void *)ptr;
253 iov[0].iov_len = nbytes;
257 return (sendmsg(fd, &msg, 0));
260 static void aio_child_cleanup(struct tevent_context *event_ctx,
261 struct tevent_timer *te,
265 struct aio_child_list *list = talloc_get_type_abort(
266 private_data, struct aio_child_list);
267 struct aio_child *child, *next;
269 TALLOC_FREE(list->cleanup_event);
271 for (child = list->children; child != NULL; child = next) {
275 DEBUG(10, ("child %d currently active\n",
280 if (child->dont_delete) {
281 DEBUG(10, ("Child %d was active since last cleanup\n",
283 child->dont_delete = false;
287 DEBUG(10, ("Child %d idle for more than 30 seconds, "
288 "deleting\n", (int)child->pid));
294 if (list->children != NULL) {
296 * Re-schedule the next cleanup round
298 list->cleanup_event = tevent_add_timer(server_event_context(), list,
299 timeval_add(&now, 30, 0),
300 aio_child_cleanup, list);
305 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
307 struct aio_child_list *data = NULL;
309 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
310 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
315 data = talloc_zero(NULL, struct aio_child_list);
322 * Regardless of whether the child_list had been around or not, make
323 * sure that we have a cleanup timed event. This timed event will
324 * delete itself when it finds that no children are around anymore.
327 if (data->cleanup_event == NULL) {
328 data->cleanup_event = tevent_add_timer(server_event_context(), data,
329 timeval_current_ofs(30, 0),
330 aio_child_cleanup, data);
331 if (data->cleanup_event == NULL) {
337 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
338 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
339 struct aio_child_list, return False);
345 static void aio_child_loop(int sockfd, struct mmap_area *map)
350 struct rw_cmd cmd_struct;
351 struct rw_ret ret_struct;
353 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
354 if (ret != sizeof(cmd_struct)) {
355 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
360 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
361 cmd_type_str(cmd_struct.cmd),
362 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
364 if (cmd_struct.erratic_testing_mode) {
366 * For developer testing, we want erratic behaviour for
372 * use generate_random_buffer, we just forked from a
373 * common parent state
375 generate_random_buffer(&randval, sizeof(randval));
376 msecs = randval + 20;
377 DEBUG(10, ("delaying for %u msecs\n", msecs));
381 ZERO_STRUCT(ret_struct);
383 switch (cmd_struct.cmd) {
385 ret_struct.size = sys_pread(
386 fd, (void *)map->ptr, cmd_struct.n,
389 /* This breaks "make test" when run with aio_fork module. */
391 ret_struct.size = MAX(1, ret_struct.size * 0.9);
396 ret_struct.size = sys_pwrite(
397 fd, (void *)map->ptr, cmd_struct.n,
401 ret_struct.size = fsync(fd);
404 ret_struct.size = -1;
408 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
409 (int)ret_struct.size));
411 if (ret_struct.size == -1) {
412 ret_struct.ret_errno = errno;
416 * Close the fd before telling our parent we're done. The
417 * parent might close and re-open the file very quickly, and
418 * with system-level share modes (GPFS) we would get an
419 * unjustified SHARING_VIOLATION.
423 ret = write_data(sockfd, (char *)&ret_struct,
425 if (ret != sizeof(ret_struct)) {
426 DEBUG(10, ("could not write ret_struct: %s\n",
433 static int aio_child_destructor(struct aio_child *child)
437 SMB_ASSERT(!child->busy);
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 int 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(children, struct aio_child);
479 if (result == NULL) {
483 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
485 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
489 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
491 result->map = mmap_area_init(result, map_size);
492 if (result->map == NULL) {
494 DEBUG(0, ("Could not create mmap area\n"));
498 result->pid = fork();
499 if (result->pid == -1) {
501 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
505 if (result->pid == 0) {
507 result->sockfd = fdpair[1];
508 files_forall(sconn, close_fsp_fd, NULL);
509 aio_child_loop(result->sockfd, result->map);
512 DEBUG(10, ("Child %d created with sockfd %d\n",
513 result->pid, fdpair[0]));
515 result->sockfd = fdpair[0];
518 result->list = children;
519 DLIST_ADD(children->children, result);
521 talloc_set_destructor(result, aio_child_destructor);
528 if (fdpair[0] != -1) close(fdpair[0]);
529 if (fdpair[1] != -1) close(fdpair[1]);
535 static int get_idle_child(struct vfs_handle_struct *handle,
536 struct aio_child **pchild)
538 struct aio_child_list *children;
539 struct aio_child *child;
541 children = init_aio_children(handle);
542 if (children == NULL) {
546 for (child = children->children; child != NULL; child = child->next) {
555 DEBUG(10, ("no idle child found, creating new one\n"));
557 ret = create_aio_child(handle->conn->sconn, children,
560 DEBUG(10, ("create_aio_child failed: %s\n",
566 child->dont_delete = true;
573 struct aio_fork_pread_state {
574 struct aio_child *child;
579 static void aio_fork_pread_done(struct tevent_req *subreq);
581 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
583 struct tevent_context *ev,
584 struct files_struct *fsp,
586 size_t n, off_t offset)
588 struct tevent_req *req, *subreq;
589 struct aio_fork_pread_state *state;
593 struct aio_fork_config *config;
595 SMB_VFS_HANDLE_GET_DATA(handle, config,
596 struct aio_fork_config,
599 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
605 /* TODO: support variable buffers */
606 tevent_req_error(req, EINVAL);
607 return tevent_req_post(req, ev);
610 err = get_idle_child(handle, &state->child);
612 tevent_req_error(req, err);
613 return tevent_req_post(req, ev);
620 cmd.erratic_testing_mode = config->erratic_testing_mode;
622 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
623 (int)state->child->pid));
626 * Not making this async. We're writing into an empty unix
627 * domain socket. This should never block.
629 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
634 TALLOC_FREE(state->child);
636 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
637 tevent_req_error(req, err);
638 return tevent_req_post(req, ev);
641 subreq = read_packet_send(state, ev, state->child->sockfd,
642 sizeof(struct rw_ret), NULL, NULL);
643 if (tevent_req_nomem(subreq, req)) {
644 TALLOC_FREE(state->child); /* we sent sth down */
645 return tevent_req_post(req, ev);
647 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
651 static void aio_fork_pread_done(struct tevent_req *subreq)
653 struct tevent_req *req = tevent_req_callback_data(
654 subreq, struct tevent_req);
655 struct aio_fork_pread_state *state = tevent_req_data(
656 req, struct aio_fork_pread_state);
660 struct rw_ret *retbuf;
662 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
665 TALLOC_FREE(state->child);
666 tevent_req_error(req, err);
670 state->child->busy = false;
672 retbuf = (struct rw_ret *)buf;
673 state->ret = retbuf->size;
674 state->err = retbuf->ret_errno;
675 tevent_req_done(req);
678 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
680 struct aio_fork_pread_state *state = tevent_req_data(
681 req, struct aio_fork_pread_state);
683 if (tevent_req_is_unix_error(req, err)) {
686 if (state->ret == -1) {
692 struct aio_fork_pwrite_state {
693 struct aio_child *child;
698 static void aio_fork_pwrite_done(struct tevent_req *subreq);
700 static struct tevent_req *aio_fork_pwrite_send(
701 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
702 struct tevent_context *ev, struct files_struct *fsp,
703 const void *data, size_t n, off_t offset)
705 struct tevent_req *req, *subreq;
706 struct aio_fork_pwrite_state *state;
710 struct aio_fork_config *config;
711 SMB_VFS_HANDLE_GET_DATA(handle, config,
712 struct aio_fork_config,
715 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
721 /* TODO: support variable buffers */
722 tevent_req_error(req, EINVAL);
723 return tevent_req_post(req, ev);
726 err = get_idle_child(handle, &state->child);
728 tevent_req_error(req, err);
729 return tevent_req_post(req, ev);
736 cmd.erratic_testing_mode = config->erratic_testing_mode;
738 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
739 (int)state->child->pid));
742 * Not making this async. We're writing into an empty unix
743 * domain socket. This should never block.
745 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
750 TALLOC_FREE(state->child);
752 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
753 tevent_req_error(req, err);
754 return tevent_req_post(req, ev);
757 subreq = read_packet_send(state, ev, state->child->sockfd,
758 sizeof(struct rw_ret), NULL, NULL);
759 if (tevent_req_nomem(subreq, req)) {
760 TALLOC_FREE(state->child); /* we sent sth down */
761 return tevent_req_post(req, ev);
763 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
767 static void aio_fork_pwrite_done(struct tevent_req *subreq)
769 struct tevent_req *req = tevent_req_callback_data(
770 subreq, struct tevent_req);
771 struct aio_fork_pwrite_state *state = tevent_req_data(
772 req, struct aio_fork_pwrite_state);
776 struct rw_ret *retbuf;
778 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
781 TALLOC_FREE(state->child);
782 tevent_req_error(req, err);
786 state->child->busy = false;
788 retbuf = (struct rw_ret *)buf;
789 state->ret = retbuf->size;
790 state->err = retbuf->ret_errno;
791 tevent_req_done(req);
794 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
796 struct aio_fork_pwrite_state *state = tevent_req_data(
797 req, struct aio_fork_pwrite_state);
799 if (tevent_req_is_unix_error(req, err)) {
802 if (state->ret == -1) {
808 struct aio_fork_fsync_state {
809 struct aio_child *child;
814 static void aio_fork_fsync_done(struct tevent_req *subreq);
816 static struct tevent_req *aio_fork_fsync_send(
817 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
818 struct tevent_context *ev, struct files_struct *fsp)
820 struct tevent_req *req, *subreq;
821 struct aio_fork_fsync_state *state;
825 struct aio_fork_config *config;
827 SMB_VFS_HANDLE_GET_DATA(handle, config,
828 struct aio_fork_config,
831 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
836 err = get_idle_child(handle, &state->child);
838 tevent_req_error(req, err);
839 return tevent_req_post(req, ev);
844 cmd.erratic_testing_mode = config->erratic_testing_mode;
846 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
847 (int)state->child->pid));
850 * Not making this async. We're writing into an empty unix
851 * domain socket. This should never block.
853 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
858 TALLOC_FREE(state->child);
860 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
861 tevent_req_error(req, err);
862 return tevent_req_post(req, ev);
865 subreq = read_packet_send(state, ev, state->child->sockfd,
866 sizeof(struct rw_ret), NULL, NULL);
867 if (tevent_req_nomem(subreq, req)) {
868 TALLOC_FREE(state->child); /* we sent sth down */
869 return tevent_req_post(req, ev);
871 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
875 static void aio_fork_fsync_done(struct tevent_req *subreq)
877 struct tevent_req *req = tevent_req_callback_data(
878 subreq, struct tevent_req);
879 struct aio_fork_fsync_state *state = tevent_req_data(
880 req, struct aio_fork_fsync_state);
884 struct rw_ret *retbuf;
886 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
889 TALLOC_FREE(state->child);
890 tevent_req_error(req, err);
894 state->child->busy = false;
896 retbuf = (struct rw_ret *)buf;
897 state->ret = retbuf->size;
898 state->err = retbuf->ret_errno;
899 tevent_req_done(req);
902 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
904 struct aio_fork_fsync_state *state = tevent_req_data(
905 req, struct aio_fork_fsync_state);
907 if (tevent_req_is_unix_error(req, err)) {
910 if (state->ret == -1) {
916 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
920 struct aio_fork_config *config;
921 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
927 config = talloc_zero(handle->conn, struct aio_fork_config);
929 SMB_VFS_NEXT_DISCONNECT(handle);
930 DEBUG(0, ("talloc_zero() failed\n"));
934 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
935 "erratic_testing_mode", false);
937 SMB_VFS_HANDLE_SET_DATA(handle, config,
938 NULL, struct aio_fork_config,
941 /*********************************************************************
942 * How many threads to initialize ?
943 * 100 per process seems insane as a default until you realize that
944 * (a) Threads terminate after 1 second when idle.
945 * (b) Throttling is done in SMB2 via the crediting algorithm.
946 * (c) SMB1 clients are limited to max_mux (50) outstanding
947 * requests and Windows clients don't use this anyway.
948 * Essentially we want this to be unlimited unless smb.conf
950 *********************************************************************/
951 aio_pending_size = 100;
955 static struct vfs_fn_pointers vfs_aio_fork_fns = {
956 .connect_fn = aio_fork_connect,
957 .pread_send_fn = aio_fork_pread_send,
958 .pread_recv_fn = aio_fork_pread_recv,
959 .pwrite_send_fn = aio_fork_pwrite_send,
960 .pwrite_recv_fn = aio_fork_pwrite_recv,
961 .fsync_send_fn = aio_fork_fsync_send,
962 .fsync_recv_fn = aio_fork_fsync_recv,
965 NTSTATUS vfs_aio_fork_init(void);
966 NTSTATUS vfs_aio_fork_init(void)
968 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
969 "aio_fork", &vfs_aio_fork_fns);