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"
31 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
32 # error Can not pass file descriptors
41 struct aio_fork_config {
42 bool erratic_testing_mode;
50 static int mmap_area_destructor(struct mmap_area *area)
52 munmap((void *)area->ptr, area->size);
56 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
58 struct mmap_area *result;
61 result = talloc(mem_ctx, struct mmap_area);
63 DEBUG(0, ("talloc failed\n"));
67 fd = open("/dev/zero", O_RDWR);
69 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
74 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
75 MAP_SHARED|MAP_FILE, fd, 0);
77 if (result->ptr == MAP_FAILED) {
78 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
83 talloc_set_destructor(result, mmap_area_destructor);
98 static const char *cmd_type_str(enum cmd_type cmd)
113 result = "<UNKNOWN>";
123 bool erratic_testing_mode;
131 struct aio_child_list;
134 struct aio_child *prev, *next;
135 struct aio_child_list *list;
138 struct mmap_area *map;
139 bool dont_delete; /* Marked as in use since last cleanup */
143 struct aio_child_list {
144 struct aio_child *children;
145 struct tevent_timer *cleanup_event;
148 static void free_aio_children(void **p)
153 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
158 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
162 msg.msg_accrights = (caddr_t) &newfd;
163 msg.msg_accrightslen = sizeof(int);
168 char control[CMSG_SPACE(sizeof(int))];
170 struct cmsghdr *cmptr;
173 ZERO_STRUCT(control_un);
175 msg.msg_control = control_un.control;
176 msg.msg_controllen = sizeof(control_un.control);
182 iov[0].iov_base = (void *)ptr;
183 iov[0].iov_len = nbytes;
187 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
191 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
192 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
193 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
194 if (cmptr->cmsg_level != SOL_SOCKET) {
195 DEBUG(10, ("control level != SOL_SOCKET"));
199 if (cmptr->cmsg_type != SCM_RIGHTS) {
200 DEBUG(10, ("control type != SCM_RIGHTS"));
204 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
206 *recvfd = -1; /* descriptor was not passed */
209 if (msg.msg_accrightslen == sizeof(int)) {
213 *recvfd = -1; /* descriptor was not passed */
220 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
225 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
228 char control[CMSG_SPACE(sizeof(int))];
230 struct cmsghdr *cmptr;
233 ZERO_STRUCT(control_un);
235 msg.msg_control = control_un.control;
236 msg.msg_controllen = sizeof(control_un.control);
238 cmptr = CMSG_FIRSTHDR(&msg);
239 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
240 cmptr->cmsg_level = SOL_SOCKET;
241 cmptr->cmsg_type = SCM_RIGHTS;
242 memcpy(CMSG_DATA(cmptr), &sendfd, sizeof(sendfd));
245 msg.msg_accrights = (caddr_t) &sendfd;
246 msg.msg_accrightslen = sizeof(int);
253 iov[0].iov_base = (void *)ptr;
254 iov[0].iov_len = nbytes;
258 return (sendmsg(fd, &msg, 0));
261 static void aio_child_cleanup(struct tevent_context *event_ctx,
262 struct tevent_timer *te,
266 struct aio_child_list *list = talloc_get_type_abort(
267 private_data, struct aio_child_list);
268 struct aio_child *child, *next;
270 TALLOC_FREE(list->cleanup_event);
272 for (child = list->children; child != NULL; child = next) {
276 DEBUG(10, ("child %d currently active\n",
281 if (child->dont_delete) {
282 DEBUG(10, ("Child %d was active since last cleanup\n",
284 child->dont_delete = false;
288 DEBUG(10, ("Child %d idle for more than 30 seconds, "
289 "deleting\n", (int)child->pid));
295 if (list->children != NULL) {
297 * Re-schedule the next cleanup round
299 list->cleanup_event = tevent_add_timer(server_event_context(), list,
300 timeval_add(&now, 30, 0),
301 aio_child_cleanup, list);
306 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
308 struct aio_child_list *data = NULL;
310 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
311 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
316 data = talloc_zero(NULL, struct aio_child_list);
323 * Regardless of whether the child_list had been around or not, make
324 * sure that we have a cleanup timed event. This timed event will
325 * delete itself when it finds that no children are around anymore.
328 if (data->cleanup_event == NULL) {
329 data->cleanup_event = tevent_add_timer(server_event_context(), data,
330 timeval_current_ofs(30, 0),
331 aio_child_cleanup, data);
332 if (data->cleanup_event == NULL) {
338 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
339 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
340 struct aio_child_list, return False);
346 static void aio_child_loop(int sockfd, struct mmap_area *map)
351 struct rw_cmd cmd_struct;
352 struct rw_ret ret_struct;
354 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
355 if (ret != sizeof(cmd_struct)) {
356 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
361 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
362 cmd_type_str(cmd_struct.cmd),
363 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
365 if (cmd_struct.erratic_testing_mode) {
367 * For developer testing, we want erratic behaviour for
373 * use generate_random_buffer, we just forked from a
374 * common parent state
376 generate_random_buffer(&randval, sizeof(randval));
377 msecs = randval + 20;
378 DEBUG(10, ("delaying for %u msecs\n", msecs));
382 ZERO_STRUCT(ret_struct);
384 switch (cmd_struct.cmd) {
386 ret_struct.size = sys_pread(
387 fd, (void *)map->ptr, cmd_struct.n,
390 /* This breaks "make test" when run with aio_fork module. */
392 ret_struct.size = MAX(1, ret_struct.size * 0.9);
397 ret_struct.size = sys_pwrite(
398 fd, (void *)map->ptr, cmd_struct.n,
402 ret_struct.size = fsync(fd);
405 ret_struct.size = -1;
409 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
410 (int)ret_struct.size));
412 if (ret_struct.size == -1) {
413 ret_struct.ret_errno = errno;
417 * Close the fd before telling our parent we're done. The
418 * parent might close and re-open the file very quickly, and
419 * with system-level share modes (GPFS) we would get an
420 * unjustified SHARING_VIOLATION.
424 ret = write_data(sockfd, (char *)&ret_struct,
426 if (ret != sizeof(ret_struct)) {
427 DEBUG(10, ("could not write ret_struct: %s\n",
434 static int aio_child_destructor(struct aio_child *child)
438 SMB_ASSERT(!child->busy);
440 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
441 child->pid, child->sockfd));
444 * closing the sockfd makes the child not return from recvmsg() on RHEL
445 * 5.5 so instead force the child to exit by writing bad data to it
447 write(child->sockfd, &c, sizeof(c));
448 close(child->sockfd);
449 DLIST_REMOVE(child->list->children, child);
454 * We have to close all fd's in open files, we might incorrectly hold a system
455 * level share mode on a file.
458 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
461 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
468 static int create_aio_child(struct smbd_server_connection *sconn,
469 struct aio_child_list *children,
471 struct aio_child **presult)
473 struct aio_child *result;
477 fdpair[0] = fdpair[1] = -1;
479 result = talloc_zero(children, struct aio_child);
480 if (result == NULL) {
484 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
486 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
490 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
492 result->map = mmap_area_init(result, map_size);
493 if (result->map == NULL) {
495 DEBUG(0, ("Could not create mmap area\n"));
499 result->pid = fork();
500 if (result->pid == -1) {
502 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
506 if (result->pid == 0) {
508 result->sockfd = fdpair[1];
509 files_forall(sconn, close_fsp_fd, NULL);
510 aio_child_loop(result->sockfd, result->map);
513 DEBUG(10, ("Child %d created with sockfd %d\n",
514 result->pid, fdpair[0]));
516 result->sockfd = fdpair[0];
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 int get_idle_child(struct vfs_handle_struct *handle,
537 struct aio_child **pchild)
539 struct aio_child_list *children;
540 struct aio_child *child;
542 children = init_aio_children(handle);
543 if (children == NULL) {
547 for (child = children->children; child != NULL; child = child->next) {
556 DEBUG(10, ("no idle child found, creating new one\n"));
558 ret = create_aio_child(handle->conn->sconn, children,
561 DEBUG(10, ("create_aio_child failed: %s\n",
567 child->dont_delete = true;
574 struct aio_fork_pread_state {
575 struct aio_child *child;
580 static void aio_fork_pread_done(struct tevent_req *subreq);
582 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
584 struct tevent_context *ev,
585 struct files_struct *fsp,
587 size_t n, off_t offset)
589 struct tevent_req *req, *subreq;
590 struct aio_fork_pread_state *state;
594 struct aio_fork_config *config;
596 SMB_VFS_HANDLE_GET_DATA(handle, config,
597 struct aio_fork_config,
600 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
606 /* TODO: support variable buffers */
607 tevent_req_error(req, EINVAL);
608 return tevent_req_post(req, ev);
611 err = get_idle_child(handle, &state->child);
613 tevent_req_error(req, err);
614 return tevent_req_post(req, ev);
621 cmd.erratic_testing_mode = config->erratic_testing_mode;
623 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
624 (int)state->child->pid));
627 * Not making this async. We're writing into an empty unix
628 * domain socket. This should never block.
630 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
635 TALLOC_FREE(state->child);
637 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
638 tevent_req_error(req, err);
639 return tevent_req_post(req, ev);
642 subreq = read_packet_send(state, ev, state->child->sockfd,
643 sizeof(struct rw_ret), NULL, NULL);
644 if (tevent_req_nomem(subreq, req)) {
645 TALLOC_FREE(state->child); /* we sent sth down */
646 return tevent_req_post(req, ev);
648 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
652 static void aio_fork_pread_done(struct tevent_req *subreq)
654 struct tevent_req *req = tevent_req_callback_data(
655 subreq, struct tevent_req);
656 struct aio_fork_pread_state *state = tevent_req_data(
657 req, struct aio_fork_pread_state);
661 struct rw_ret *retbuf;
663 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
666 TALLOC_FREE(state->child);
667 tevent_req_error(req, err);
671 state->child->busy = false;
673 retbuf = (struct rw_ret *)buf;
674 state->ret = retbuf->size;
675 state->err = retbuf->ret_errno;
676 tevent_req_done(req);
679 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
681 struct aio_fork_pread_state *state = tevent_req_data(
682 req, struct aio_fork_pread_state);
684 if (tevent_req_is_unix_error(req, err)) {
687 if (state->ret == -1) {
693 struct aio_fork_pwrite_state {
694 struct aio_child *child;
699 static void aio_fork_pwrite_done(struct tevent_req *subreq);
701 static struct tevent_req *aio_fork_pwrite_send(
702 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
703 struct tevent_context *ev, struct files_struct *fsp,
704 const void *data, size_t n, off_t offset)
706 struct tevent_req *req, *subreq;
707 struct aio_fork_pwrite_state *state;
711 struct aio_fork_config *config;
712 SMB_VFS_HANDLE_GET_DATA(handle, config,
713 struct aio_fork_config,
716 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
722 /* TODO: support variable buffers */
723 tevent_req_error(req, EINVAL);
724 return tevent_req_post(req, ev);
727 err = get_idle_child(handle, &state->child);
729 tevent_req_error(req, err);
730 return tevent_req_post(req, ev);
737 cmd.erratic_testing_mode = config->erratic_testing_mode;
739 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
740 (int)state->child->pid));
743 * Not making this async. We're writing into an empty unix
744 * domain socket. This should never block.
746 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
751 TALLOC_FREE(state->child);
753 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
754 tevent_req_error(req, err);
755 return tevent_req_post(req, ev);
758 subreq = read_packet_send(state, ev, state->child->sockfd,
759 sizeof(struct rw_ret), NULL, NULL);
760 if (tevent_req_nomem(subreq, req)) {
761 TALLOC_FREE(state->child); /* we sent sth down */
762 return tevent_req_post(req, ev);
764 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
768 static void aio_fork_pwrite_done(struct tevent_req *subreq)
770 struct tevent_req *req = tevent_req_callback_data(
771 subreq, struct tevent_req);
772 struct aio_fork_pwrite_state *state = tevent_req_data(
773 req, struct aio_fork_pwrite_state);
777 struct rw_ret *retbuf;
779 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
782 TALLOC_FREE(state->child);
783 tevent_req_error(req, err);
787 state->child->busy = false;
789 retbuf = (struct rw_ret *)buf;
790 state->ret = retbuf->size;
791 state->err = retbuf->ret_errno;
792 tevent_req_done(req);
795 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
797 struct aio_fork_pwrite_state *state = tevent_req_data(
798 req, struct aio_fork_pwrite_state);
800 if (tevent_req_is_unix_error(req, err)) {
803 if (state->ret == -1) {
809 struct aio_fork_fsync_state {
810 struct aio_child *child;
815 static void aio_fork_fsync_done(struct tevent_req *subreq);
817 static struct tevent_req *aio_fork_fsync_send(
818 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
819 struct tevent_context *ev, struct files_struct *fsp)
821 struct tevent_req *req, *subreq;
822 struct aio_fork_fsync_state *state;
826 struct aio_fork_config *config;
828 SMB_VFS_HANDLE_GET_DATA(handle, config,
829 struct aio_fork_config,
832 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
837 err = get_idle_child(handle, &state->child);
839 tevent_req_error(req, err);
840 return tevent_req_post(req, ev);
845 cmd.erratic_testing_mode = config->erratic_testing_mode;
847 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
848 (int)state->child->pid));
851 * Not making this async. We're writing into an empty unix
852 * domain socket. This should never block.
854 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
859 TALLOC_FREE(state->child);
861 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
862 tevent_req_error(req, err);
863 return tevent_req_post(req, ev);
866 subreq = read_packet_send(state, ev, state->child->sockfd,
867 sizeof(struct rw_ret), NULL, NULL);
868 if (tevent_req_nomem(subreq, req)) {
869 TALLOC_FREE(state->child); /* we sent sth down */
870 return tevent_req_post(req, ev);
872 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
876 static void aio_fork_fsync_done(struct tevent_req *subreq)
878 struct tevent_req *req = tevent_req_callback_data(
879 subreq, struct tevent_req);
880 struct aio_fork_fsync_state *state = tevent_req_data(
881 req, struct aio_fork_fsync_state);
885 struct rw_ret *retbuf;
887 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
890 TALLOC_FREE(state->child);
891 tevent_req_error(req, err);
895 state->child->busy = false;
897 retbuf = (struct rw_ret *)buf;
898 state->ret = retbuf->size;
899 state->err = retbuf->ret_errno;
900 tevent_req_done(req);
903 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
905 struct aio_fork_fsync_state *state = tevent_req_data(
906 req, struct aio_fork_fsync_state);
908 if (tevent_req_is_unix_error(req, err)) {
911 if (state->ret == -1) {
917 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
921 struct aio_fork_config *config;
922 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
928 config = talloc_zero(handle->conn, struct aio_fork_config);
930 SMB_VFS_NEXT_DISCONNECT(handle);
931 DEBUG(0, ("talloc_zero() failed\n"));
935 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
936 "erratic_testing_mode", false);
938 SMB_VFS_HANDLE_SET_DATA(handle, config,
939 NULL, struct aio_fork_config,
942 /*********************************************************************
943 * How many threads to initialize ?
944 * 100 per process seems insane as a default until you realize that
945 * (a) Threads terminate after 1 second when idle.
946 * (b) Throttling is done in SMB2 via the crediting algorithm.
947 * (c) SMB1 clients are limited to max_mux (50) outstanding
948 * requests and Windows clients don't use this anyway.
949 * Essentially we want this to be unlimited unless smb.conf
951 *********************************************************************/
952 aio_pending_size = 100;
956 static struct vfs_fn_pointers vfs_aio_fork_fns = {
957 .connect_fn = aio_fork_connect,
958 .pread_send_fn = aio_fork_pread_send,
959 .pread_recv_fn = aio_fork_pread_recv,
960 .pwrite_send_fn = aio_fork_pwrite_send,
961 .pwrite_recv_fn = aio_fork_pwrite_recv,
962 .fsync_send_fn = aio_fork_fsync_send,
963 .fsync_recv_fn = aio_fork_fsync_recv,
966 NTSTATUS vfs_aio_fork_init(void);
967 NTSTATUS vfs_aio_fork_init(void)
969 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
970 "aio_fork", &vfs_aio_fork_fns);