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/util/sys_rw.h"
30 #include "lib/util/sys_rw_data.h"
31 #include "lib/util/msghdr.h"
32 #include "smbprofile.h"
34 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
35 # error Can not pass file descriptors
44 struct aio_child_list;
46 struct aio_fork_config {
47 bool erratic_testing_mode;
48 struct aio_child_list *children;
56 static int mmap_area_destructor(struct mmap_area *area)
58 munmap(discard_const(area->ptr), area->size);
62 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
64 struct mmap_area *result;
67 result = talloc(mem_ctx, struct mmap_area);
69 DEBUG(0, ("talloc failed\n"));
73 fd = open("/dev/zero", O_RDWR);
75 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
80 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
81 MAP_SHARED|MAP_FILE, fd, 0);
83 if (result->ptr == MAP_FAILED) {
84 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
89 talloc_set_destructor(result, mmap_area_destructor);
104 static const char *cmd_type_str(enum cmd_type cmd)
119 result = "<UNKNOWN>";
129 bool erratic_testing_mode;
138 struct aio_child_list;
141 struct aio_child *prev, *next;
142 struct aio_child_list *list;
145 struct mmap_area *map;
146 bool dont_delete; /* Marked as in use since last cleanup */
150 struct aio_child_list {
151 struct aio_child *children;
152 struct tevent_timer *cleanup_event;
155 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
158 struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1 };
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);
165 iov[0].iov_base = (void *)ptr;
166 iov[0].iov_len = nbytes;
169 n = recvmsg(fd, &msg, 0);
170 } while ((n == -1) && (errno == EINTR));
177 size_t num_fds = msghdr_extract_fds(&msg, NULL, 0);
180 msghdr_extract_fds(&msg, fds, num_fds);
185 for (i=0; i<num_fds; i++) {
199 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
201 struct msghdr msg = {0};
202 size_t bufsize = msghdr_prep_fds(NULL, NULL, 0, &sendfd, 1);
203 uint8_t buf[bufsize];
207 msghdr_prep_fds(&msg, buf, bufsize, &sendfd, 1);
209 iov.iov_base = (void *)ptr;
210 iov.iov_len = nbytes;
215 sent = sendmsg(fd, &msg, 0);
216 } while ((sent == -1) && (errno == EINTR));
221 static void aio_child_cleanup(struct tevent_context *event_ctx,
222 struct tevent_timer *te,
226 struct aio_child_list *list = talloc_get_type_abort(
227 private_data, struct aio_child_list);
228 struct aio_child *child, *next;
230 TALLOC_FREE(list->cleanup_event);
232 for (child = list->children; child != NULL; child = next) {
236 DEBUG(10, ("child %d currently active\n",
241 if (child->dont_delete) {
242 DEBUG(10, ("Child %d was active since last cleanup\n",
244 child->dont_delete = false;
248 DEBUG(10, ("Child %d idle for more than 30 seconds, "
249 "deleting\n", (int)child->pid));
255 if (list->children != NULL) {
257 * Re-schedule the next cleanup round
259 list->cleanup_event = tevent_add_timer(global_event_context(), list,
260 timeval_add(&now, 30, 0),
261 aio_child_cleanup, list);
266 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
268 struct aio_fork_config *config;
269 struct aio_child_list *children;
271 SMB_VFS_HANDLE_GET_DATA(handle, config, struct aio_fork_config,
274 if (config->children == NULL) {
275 config->children = talloc_zero(config, struct aio_child_list);
276 if (config->children == NULL) {
280 children = config->children;
283 * Regardless of whether the child_list had been around or not, make
284 * sure that we have a cleanup timed event. This timed event will
285 * delete itself when it finds that no children are around anymore.
288 if (children->cleanup_event == NULL) {
289 children->cleanup_event =
290 tevent_add_timer(global_event_context(), children,
291 timeval_current_ofs(30, 0),
292 aio_child_cleanup, children);
293 if (children->cleanup_event == NULL) {
294 TALLOC_FREE(config->children);
302 static void aio_child_loop(int sockfd, struct mmap_area *map)
307 struct rw_cmd cmd_struct;
308 struct rw_ret ret_struct;
309 struct timespec start, end;
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_type_str(cmd_struct.cmd),
320 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
322 if (cmd_struct.erratic_testing_mode) {
324 * For developer testing, we want erratic behaviour for
330 * use generate_random_buffer, we just forked from a
331 * common parent state
333 generate_random_buffer(&randval, sizeof(randval));
334 msecs = (randval%20)+1;
335 DEBUG(10, ("delaying for %u msecs\n", msecs));
339 ZERO_STRUCT(ret_struct);
341 PROFILE_TIMESTAMP(&start);
343 switch (cmd_struct.cmd) {
345 ret_struct.size = sys_pread_full(
346 fd, discard_const(map->ptr), cmd_struct.n,
349 /* This breaks "make test" when run with aio_fork module. */
351 ret_struct.size = MAX(1, ret_struct.size * 0.9);
356 ret_struct.size = sys_pwrite_full(
357 fd, discard_const(map->ptr), cmd_struct.n,
361 ret_struct.size = fsync(fd);
364 ret_struct.size = -1;
368 PROFILE_TIMESTAMP(&end);
369 ret_struct.duration = nsec_time_diff(&end, &start);
370 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
371 (int)ret_struct.size));
373 if (ret_struct.size == -1) {
374 ret_struct.ret_errno = errno;
378 * Close the fd before telling our parent we're done. The
379 * parent might close and re-open the file very quickly, and
380 * with system-level share modes (GPFS) we would get an
381 * unjustified SHARING_VIOLATION.
385 ret = write_data(sockfd, (char *)&ret_struct,
387 if (ret != sizeof(ret_struct)) {
388 DEBUG(10, ("could not write ret_struct: %s\n",
395 static int aio_child_destructor(struct aio_child *child)
399 SMB_ASSERT(!child->busy);
401 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
402 (int)child->pid, child->sockfd));
405 * closing the sockfd makes the child not return from recvmsg() on RHEL
406 * 5.5 so instead force the child to exit by writing bad data to it
408 sys_write_v(child->sockfd, &c, sizeof(c));
409 close(child->sockfd);
410 DLIST_REMOVE(child->list->children, child);
415 * We have to close all fd's in open files, we might incorrectly hold a system
416 * level share mode on a file.
419 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
422 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
429 static int create_aio_child(struct smbd_server_connection *sconn,
430 struct aio_child_list *children,
432 struct aio_child **presult)
434 struct aio_child *result;
438 fdpair[0] = fdpair[1] = -1;
440 result = talloc_zero(children, struct aio_child);
441 if (result == NULL) {
445 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
447 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
451 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
453 result->map = mmap_area_init(result, map_size);
454 if (result->map == NULL) {
456 DEBUG(0, ("Could not create mmap area\n"));
460 result->pid = fork();
461 if (result->pid == -1) {
463 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
467 if (result->pid == 0) {
469 result->sockfd = fdpair[1];
470 files_forall(sconn, close_fsp_fd, NULL);
471 aio_child_loop(result->sockfd, result->map);
474 DEBUG(10, ("Child %d created with sockfd %d\n",
475 (int)result->pid, fdpair[0]));
477 result->sockfd = fdpair[0];
480 result->list = children;
481 DLIST_ADD(children->children, result);
483 talloc_set_destructor(result, aio_child_destructor);
490 if (fdpair[0] != -1) close(fdpair[0]);
491 if (fdpair[1] != -1) close(fdpair[1]);
497 static int get_idle_child(struct vfs_handle_struct *handle,
498 struct aio_child **pchild)
500 struct aio_child_list *children;
501 struct aio_child *child;
503 children = init_aio_children(handle);
504 if (children == NULL) {
508 for (child = children->children; child != NULL; child = child->next) {
517 DEBUG(10, ("no idle child found, creating new one\n"));
519 ret = create_aio_child(handle->conn->sconn, children,
522 DEBUG(10, ("create_aio_child failed: %s\n",
528 child->dont_delete = true;
535 struct aio_fork_pread_state {
536 struct aio_child *child;
540 struct vfs_aio_state vfs_aio_state;
543 static void aio_fork_pread_done(struct tevent_req *subreq);
545 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
547 struct tevent_context *ev,
548 struct files_struct *fsp,
550 size_t n, off_t offset)
552 struct tevent_req *req, *subreq;
553 struct aio_fork_pread_state *state;
557 struct aio_fork_config *config;
559 SMB_VFS_HANDLE_GET_DATA(handle, config,
560 struct aio_fork_config,
563 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
571 /* TODO: support variable buffers */
572 tevent_req_error(req, EINVAL);
573 return tevent_req_post(req, ev);
576 err = get_idle_child(handle, &state->child);
578 tevent_req_error(req, err);
579 return tevent_req_post(req, ev);
586 cmd.erratic_testing_mode = config->erratic_testing_mode;
588 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
589 (int)state->child->pid));
592 * Not making this async. We're writing into an empty unix
593 * domain socket. This should never block.
595 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
600 TALLOC_FREE(state->child);
602 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
603 tevent_req_error(req, err);
604 return tevent_req_post(req, ev);
607 subreq = read_packet_send(state, ev, state->child->sockfd,
608 sizeof(struct rw_ret), NULL, NULL);
609 if (tevent_req_nomem(subreq, req)) {
610 TALLOC_FREE(state->child); /* we sent sth down */
611 return tevent_req_post(req, ev);
613 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
617 static void aio_fork_pread_done(struct tevent_req *subreq)
619 struct tevent_req *req = tevent_req_callback_data(
620 subreq, struct tevent_req);
621 struct aio_fork_pread_state *state = tevent_req_data(
622 req, struct aio_fork_pread_state);
626 struct rw_ret *retbuf;
628 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
631 TALLOC_FREE(state->child);
632 tevent_req_error(req, err);
636 retbuf = (struct rw_ret *)buf;
637 state->ret = retbuf->size;
638 state->vfs_aio_state.error = retbuf->ret_errno;
639 state->vfs_aio_state.duration = retbuf->duration;
641 if ((size_t)state->ret > state->n) {
642 tevent_req_error(req, EIO);
643 state->child->busy = false;
646 memcpy(state->data, state->child->map->ptr, state->ret);
648 state->child->busy = false;
650 tevent_req_done(req);
653 static ssize_t aio_fork_pread_recv(struct tevent_req *req,
654 struct vfs_aio_state *vfs_aio_state)
656 struct aio_fork_pread_state *state = tevent_req_data(
657 req, struct aio_fork_pread_state);
659 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
662 *vfs_aio_state = state->vfs_aio_state;
666 struct aio_fork_pwrite_state {
667 struct aio_child *child;
669 struct vfs_aio_state vfs_aio_state;
672 static void aio_fork_pwrite_done(struct tevent_req *subreq);
674 static struct tevent_req *aio_fork_pwrite_send(
675 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
676 struct tevent_context *ev, struct files_struct *fsp,
677 const void *data, size_t n, off_t offset)
679 struct tevent_req *req, *subreq;
680 struct aio_fork_pwrite_state *state;
684 struct aio_fork_config *config;
685 SMB_VFS_HANDLE_GET_DATA(handle, config,
686 struct aio_fork_config,
689 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
695 /* TODO: support variable buffers */
696 tevent_req_error(req, EINVAL);
697 return tevent_req_post(req, ev);
700 err = get_idle_child(handle, &state->child);
702 tevent_req_error(req, err);
703 return tevent_req_post(req, ev);
706 memcpy(state->child->map->ptr, data, n);
712 cmd.erratic_testing_mode = config->erratic_testing_mode;
714 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
715 (int)state->child->pid));
718 * Not making this async. We're writing into an empty unix
719 * domain socket. This should never block.
721 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
726 TALLOC_FREE(state->child);
728 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
729 tevent_req_error(req, err);
730 return tevent_req_post(req, ev);
733 subreq = read_packet_send(state, ev, state->child->sockfd,
734 sizeof(struct rw_ret), NULL, NULL);
735 if (tevent_req_nomem(subreq, req)) {
736 TALLOC_FREE(state->child); /* we sent sth down */
737 return tevent_req_post(req, ev);
739 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
743 static void aio_fork_pwrite_done(struct tevent_req *subreq)
745 struct tevent_req *req = tevent_req_callback_data(
746 subreq, struct tevent_req);
747 struct aio_fork_pwrite_state *state = tevent_req_data(
748 req, struct aio_fork_pwrite_state);
752 struct rw_ret *retbuf;
754 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
757 TALLOC_FREE(state->child);
758 tevent_req_error(req, err);
762 state->child->busy = false;
764 retbuf = (struct rw_ret *)buf;
765 state->ret = retbuf->size;
766 state->vfs_aio_state.error = retbuf->ret_errno;
767 state->vfs_aio_state.duration = retbuf->duration;
768 tevent_req_done(req);
771 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req,
772 struct vfs_aio_state *vfs_aio_state)
774 struct aio_fork_pwrite_state *state = tevent_req_data(
775 req, struct aio_fork_pwrite_state);
777 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
780 *vfs_aio_state = state->vfs_aio_state;
784 struct aio_fork_fsync_state {
785 struct aio_child *child;
787 struct vfs_aio_state vfs_aio_state;
790 static void aio_fork_fsync_done(struct tevent_req *subreq);
792 static struct tevent_req *aio_fork_fsync_send(
793 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
794 struct tevent_context *ev, struct files_struct *fsp)
796 struct tevent_req *req, *subreq;
797 struct aio_fork_fsync_state *state;
801 struct aio_fork_config *config;
803 SMB_VFS_HANDLE_GET_DATA(handle, config,
804 struct aio_fork_config,
807 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
812 err = get_idle_child(handle, &state->child);
814 tevent_req_error(req, err);
815 return tevent_req_post(req, ev);
820 cmd.erratic_testing_mode = config->erratic_testing_mode;
822 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
823 (int)state->child->pid));
826 * Not making this async. We're writing into an empty unix
827 * domain socket. This should never block.
829 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
834 TALLOC_FREE(state->child);
836 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
837 tevent_req_error(req, err);
838 return tevent_req_post(req, ev);
841 subreq = read_packet_send(state, ev, state->child->sockfd,
842 sizeof(struct rw_ret), NULL, NULL);
843 if (tevent_req_nomem(subreq, req)) {
844 TALLOC_FREE(state->child); /* we sent sth down */
845 return tevent_req_post(req, ev);
847 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
851 static void aio_fork_fsync_done(struct tevent_req *subreq)
853 struct tevent_req *req = tevent_req_callback_data(
854 subreq, struct tevent_req);
855 struct aio_fork_fsync_state *state = tevent_req_data(
856 req, struct aio_fork_fsync_state);
860 struct rw_ret *retbuf;
862 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
865 TALLOC_FREE(state->child);
866 tevent_req_error(req, err);
870 state->child->busy = false;
872 retbuf = (struct rw_ret *)buf;
873 state->ret = retbuf->size;
874 state->vfs_aio_state.error = retbuf->ret_errno;
875 state->vfs_aio_state.duration = retbuf->duration;
876 tevent_req_done(req);
879 static int aio_fork_fsync_recv(struct tevent_req *req,
880 struct vfs_aio_state *vfs_aio_state)
882 struct aio_fork_fsync_state *state = tevent_req_data(
883 req, struct aio_fork_fsync_state);
885 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
888 *vfs_aio_state = state->vfs_aio_state;
892 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
896 struct aio_fork_config *config;
897 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
903 config = talloc_zero(handle->conn, struct aio_fork_config);
905 SMB_VFS_NEXT_DISCONNECT(handle);
906 DEBUG(0, ("talloc_zero() failed\n"));
910 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
911 "erratic_testing_mode", false);
913 SMB_VFS_HANDLE_SET_DATA(handle, config,
914 NULL, struct aio_fork_config,
920 static struct vfs_fn_pointers vfs_aio_fork_fns = {
921 .connect_fn = aio_fork_connect,
922 .pread_send_fn = aio_fork_pread_send,
923 .pread_recv_fn = aio_fork_pread_recv,
924 .pwrite_send_fn = aio_fork_pwrite_send,
925 .pwrite_recv_fn = aio_fork_pwrite_recv,
926 .fsync_send_fn = aio_fork_fsync_send,
927 .fsync_recv_fn = aio_fork_fsync_recv,
931 NTSTATUS vfs_aio_fork_init(TALLOC_CTX *ctx)
933 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
934 "aio_fork", &vfs_aio_fork_fns);