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 size_t bufsize = msghdr_prep_recv_fds(NULL, NULL, 0, 1);
161 uint8_t buf[bufsize];
163 msghdr_prep_recv_fds(&msg, buf, bufsize, 1);
168 iov[0].iov_base = (void *)ptr;
169 iov[0].iov_len = nbytes;
173 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
178 size_t num_fds = msghdr_extract_fds(&msg, NULL, 0);
181 msghdr_extract_fds(&msg, fds, num_fds);
186 for (i=0; i<num_fds; i++) {
200 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
203 size_t bufsize = msghdr_prep_fds(NULL, NULL, 0, &sendfd, 1);
204 uint8_t buf[bufsize];
207 msghdr_prep_fds(&msg, buf, bufsize, &sendfd, 1);
211 iov.iov_base = (void *)ptr;
212 iov.iov_len = nbytes;
216 return (sendmsg(fd, &msg, 0));
219 static void aio_child_cleanup(struct tevent_context *event_ctx,
220 struct tevent_timer *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) {
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 = tevent_add_timer(server_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(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 = tevent_add_timer(server_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_type_str(cmd_struct.cmd),
321 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
323 if (cmd_struct.erratic_testing_mode) {
325 * For developer testing, 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));
340 ZERO_STRUCT(ret_struct);
342 switch (cmd_struct.cmd) {
344 ret_struct.size = sys_pread(
345 fd, (void *)map->ptr, cmd_struct.n,
348 /* This breaks "make test" when run with aio_fork module. */
350 ret_struct.size = MAX(1, ret_struct.size * 0.9);
355 ret_struct.size = sys_pwrite(
356 fd, (void *)map->ptr, cmd_struct.n,
360 ret_struct.size = fsync(fd);
363 ret_struct.size = -1;
367 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
368 (int)ret_struct.size));
370 if (ret_struct.size == -1) {
371 ret_struct.ret_errno = errno;
375 * Close the fd before telling our parent we're done. The
376 * parent might close and re-open the file very quickly, and
377 * with system-level share modes (GPFS) we would get an
378 * unjustified SHARING_VIOLATION.
382 ret = write_data(sockfd, (char *)&ret_struct,
384 if (ret != sizeof(ret_struct)) {
385 DEBUG(10, ("could not write ret_struct: %s\n",
392 static int aio_child_destructor(struct aio_child *child)
396 SMB_ASSERT(!child->busy);
398 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
399 child->pid, child->sockfd));
402 * closing the sockfd makes the child not return from recvmsg() on RHEL
403 * 5.5 so instead force the child to exit by writing bad data to it
405 write(child->sockfd, &c, sizeof(c));
406 close(child->sockfd);
407 DLIST_REMOVE(child->list->children, child);
412 * We have to close all fd's in open files, we might incorrectly hold a system
413 * level share mode on a file.
416 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
419 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
426 static int create_aio_child(struct smbd_server_connection *sconn,
427 struct aio_child_list *children,
429 struct aio_child **presult)
431 struct aio_child *result;
435 fdpair[0] = fdpair[1] = -1;
437 result = talloc_zero(children, struct aio_child);
438 if (result == NULL) {
442 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
444 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
448 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
450 result->map = mmap_area_init(result, map_size);
451 if (result->map == NULL) {
453 DEBUG(0, ("Could not create mmap area\n"));
457 result->pid = fork();
458 if (result->pid == -1) {
460 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
464 if (result->pid == 0) {
466 result->sockfd = fdpair[1];
467 files_forall(sconn, close_fsp_fd, NULL);
468 aio_child_loop(result->sockfd, result->map);
471 DEBUG(10, ("Child %d created with sockfd %d\n",
472 result->pid, fdpair[0]));
474 result->sockfd = fdpair[0];
477 result->list = children;
478 DLIST_ADD(children->children, result);
480 talloc_set_destructor(result, aio_child_destructor);
487 if (fdpair[0] != -1) close(fdpair[0]);
488 if (fdpair[1] != -1) close(fdpair[1]);
494 static int get_idle_child(struct vfs_handle_struct *handle,
495 struct aio_child **pchild)
497 struct aio_child_list *children;
498 struct aio_child *child;
500 children = init_aio_children(handle);
501 if (children == NULL) {
505 for (child = children->children; child != NULL; child = child->next) {
514 DEBUG(10, ("no idle child found, creating new one\n"));
516 ret = create_aio_child(handle->conn->sconn, children,
519 DEBUG(10, ("create_aio_child failed: %s\n",
525 child->dont_delete = true;
532 struct aio_fork_pread_state {
533 struct aio_child *child;
538 static void aio_fork_pread_done(struct tevent_req *subreq);
540 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
542 struct tevent_context *ev,
543 struct files_struct *fsp,
545 size_t n, off_t offset)
547 struct tevent_req *req, *subreq;
548 struct aio_fork_pread_state *state;
552 struct aio_fork_config *config;
554 SMB_VFS_HANDLE_GET_DATA(handle, config,
555 struct aio_fork_config,
558 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
564 /* TODO: support variable buffers */
565 tevent_req_error(req, EINVAL);
566 return tevent_req_post(req, ev);
569 err = get_idle_child(handle, &state->child);
571 tevent_req_error(req, err);
572 return tevent_req_post(req, ev);
579 cmd.erratic_testing_mode = config->erratic_testing_mode;
581 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
582 (int)state->child->pid));
585 * Not making this async. We're writing into an empty unix
586 * domain socket. This should never block.
588 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
593 TALLOC_FREE(state->child);
595 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
596 tevent_req_error(req, err);
597 return tevent_req_post(req, ev);
600 subreq = read_packet_send(state, ev, state->child->sockfd,
601 sizeof(struct rw_ret), NULL, NULL);
602 if (tevent_req_nomem(subreq, req)) {
603 TALLOC_FREE(state->child); /* we sent sth down */
604 return tevent_req_post(req, ev);
606 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
610 static void aio_fork_pread_done(struct tevent_req *subreq)
612 struct tevent_req *req = tevent_req_callback_data(
613 subreq, struct tevent_req);
614 struct aio_fork_pread_state *state = tevent_req_data(
615 req, struct aio_fork_pread_state);
619 struct rw_ret *retbuf;
621 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
624 TALLOC_FREE(state->child);
625 tevent_req_error(req, err);
629 state->child->busy = false;
631 retbuf = (struct rw_ret *)buf;
632 state->ret = retbuf->size;
633 state->err = retbuf->ret_errno;
634 tevent_req_done(req);
637 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
639 struct aio_fork_pread_state *state = tevent_req_data(
640 req, struct aio_fork_pread_state);
642 if (tevent_req_is_unix_error(req, err)) {
645 if (state->ret == -1) {
651 struct aio_fork_pwrite_state {
652 struct aio_child *child;
657 static void aio_fork_pwrite_done(struct tevent_req *subreq);
659 static struct tevent_req *aio_fork_pwrite_send(
660 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
661 struct tevent_context *ev, struct files_struct *fsp,
662 const void *data, size_t n, off_t offset)
664 struct tevent_req *req, *subreq;
665 struct aio_fork_pwrite_state *state;
669 struct aio_fork_config *config;
670 SMB_VFS_HANDLE_GET_DATA(handle, config,
671 struct aio_fork_config,
674 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
680 /* TODO: support variable buffers */
681 tevent_req_error(req, EINVAL);
682 return tevent_req_post(req, ev);
685 err = get_idle_child(handle, &state->child);
687 tevent_req_error(req, err);
688 return tevent_req_post(req, ev);
695 cmd.erratic_testing_mode = config->erratic_testing_mode;
697 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
698 (int)state->child->pid));
701 * Not making this async. We're writing into an empty unix
702 * domain socket. This should never block.
704 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
709 TALLOC_FREE(state->child);
711 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
712 tevent_req_error(req, err);
713 return tevent_req_post(req, ev);
716 subreq = read_packet_send(state, ev, state->child->sockfd,
717 sizeof(struct rw_ret), NULL, NULL);
718 if (tevent_req_nomem(subreq, req)) {
719 TALLOC_FREE(state->child); /* we sent sth down */
720 return tevent_req_post(req, ev);
722 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
726 static void aio_fork_pwrite_done(struct tevent_req *subreq)
728 struct tevent_req *req = tevent_req_callback_data(
729 subreq, struct tevent_req);
730 struct aio_fork_pwrite_state *state = tevent_req_data(
731 req, struct aio_fork_pwrite_state);
735 struct rw_ret *retbuf;
737 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
740 TALLOC_FREE(state->child);
741 tevent_req_error(req, err);
745 state->child->busy = false;
747 retbuf = (struct rw_ret *)buf;
748 state->ret = retbuf->size;
749 state->err = retbuf->ret_errno;
750 tevent_req_done(req);
753 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
755 struct aio_fork_pwrite_state *state = tevent_req_data(
756 req, struct aio_fork_pwrite_state);
758 if (tevent_req_is_unix_error(req, err)) {
761 if (state->ret == -1) {
767 struct aio_fork_fsync_state {
768 struct aio_child *child;
773 static void aio_fork_fsync_done(struct tevent_req *subreq);
775 static struct tevent_req *aio_fork_fsync_send(
776 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
777 struct tevent_context *ev, struct files_struct *fsp)
779 struct tevent_req *req, *subreq;
780 struct aio_fork_fsync_state *state;
784 struct aio_fork_config *config;
786 SMB_VFS_HANDLE_GET_DATA(handle, config,
787 struct aio_fork_config,
790 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
795 err = get_idle_child(handle, &state->child);
797 tevent_req_error(req, err);
798 return tevent_req_post(req, ev);
803 cmd.erratic_testing_mode = config->erratic_testing_mode;
805 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
806 (int)state->child->pid));
809 * Not making this async. We're writing into an empty unix
810 * domain socket. This should never block.
812 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
817 TALLOC_FREE(state->child);
819 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
820 tevent_req_error(req, err);
821 return tevent_req_post(req, ev);
824 subreq = read_packet_send(state, ev, state->child->sockfd,
825 sizeof(struct rw_ret), NULL, NULL);
826 if (tevent_req_nomem(subreq, req)) {
827 TALLOC_FREE(state->child); /* we sent sth down */
828 return tevent_req_post(req, ev);
830 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
834 static void aio_fork_fsync_done(struct tevent_req *subreq)
836 struct tevent_req *req = tevent_req_callback_data(
837 subreq, struct tevent_req);
838 struct aio_fork_fsync_state *state = tevent_req_data(
839 req, struct aio_fork_fsync_state);
843 struct rw_ret *retbuf;
845 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
848 TALLOC_FREE(state->child);
849 tevent_req_error(req, err);
853 state->child->busy = false;
855 retbuf = (struct rw_ret *)buf;
856 state->ret = retbuf->size;
857 state->err = retbuf->ret_errno;
858 tevent_req_done(req);
861 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
863 struct aio_fork_fsync_state *state = tevent_req_data(
864 req, struct aio_fork_fsync_state);
866 if (tevent_req_is_unix_error(req, err)) {
869 if (state->ret == -1) {
875 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
879 struct aio_fork_config *config;
880 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
886 config = talloc_zero(handle->conn, struct aio_fork_config);
888 SMB_VFS_NEXT_DISCONNECT(handle);
889 DEBUG(0, ("talloc_zero() failed\n"));
893 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
894 "erratic_testing_mode", false);
896 SMB_VFS_HANDLE_SET_DATA(handle, config,
897 NULL, struct aio_fork_config,
900 /*********************************************************************
901 * How many threads to initialize ?
902 * 100 per process seems insane as a default until you realize that
903 * (a) Threads terminate after 1 second when idle.
904 * (b) Throttling is done in SMB2 via the crediting algorithm.
905 * (c) SMB1 clients are limited to max_mux (50) outstanding
906 * requests and Windows clients don't use this anyway.
907 * Essentially we want this to be unlimited unless smb.conf
909 *********************************************************************/
910 aio_pending_size = 100;
914 static struct vfs_fn_pointers vfs_aio_fork_fns = {
915 .connect_fn = aio_fork_connect,
916 .pread_send_fn = aio_fork_pread_send,
917 .pread_recv_fn = aio_fork_pread_recv,
918 .pwrite_send_fn = aio_fork_pwrite_send,
919 .pwrite_recv_fn = aio_fork_pwrite_recv,
920 .fsync_send_fn = aio_fork_fsync_send,
921 .fsync_recv_fn = aio_fork_fsync_recv,
924 NTSTATUS vfs_aio_fork_init(void);
925 NTSTATUS vfs_aio_fork_init(void)
927 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
928 "aio_fork", &vfs_aio_fork_fns);