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"
39 static int mmap_area_destructor(struct mmap_area *area)
41 munmap((void *)area->ptr, area->size);
45 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
47 struct mmap_area *result;
50 result = talloc(mem_ctx, struct mmap_area);
52 DEBUG(0, ("talloc failed\n"));
56 fd = open("/dev/zero", O_RDWR);
58 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
63 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
64 MAP_SHARED|MAP_FILE, fd, 0);
65 if (result->ptr == MAP_FAILED) {
66 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
73 talloc_set_destructor(result, mmap_area_destructor);
93 struct aio_child_list;
96 struct aio_child *prev, *next;
97 struct aio_child_list *list;
100 struct mmap_area *map;
101 bool dont_delete; /* Marked as in use since last cleanup */
105 struct aio_child_list {
106 struct aio_child *children;
107 struct timed_event *cleanup_event;
110 static void free_aio_children(void **p)
115 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
120 #ifndef HAVE_MSGHDR_MSG_CONTROL
124 #ifdef HAVE_MSGHDR_MSG_CONTROL
127 char control[CMSG_SPACE(sizeof(int))];
129 struct cmsghdr *cmptr;
131 msg.msg_control = control_un.control;
132 msg.msg_controllen = sizeof(control_un.control);
134 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
135 msg.msg_accrights = (caddr_t) &newfd;
136 msg.msg_accrightslen = sizeof(int);
138 #error Can not pass file descriptors
146 iov[0].iov_base = (void *)ptr;
147 iov[0].iov_len = nbytes;
151 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
155 #ifdef HAVE_MSGHDR_MSG_CONTROL
156 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
157 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
158 if (cmptr->cmsg_level != SOL_SOCKET) {
159 DEBUG(10, ("control level != SOL_SOCKET"));
163 if (cmptr->cmsg_type != SCM_RIGHTS) {
164 DEBUG(10, ("control type != SCM_RIGHTS"));
168 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
170 *recvfd = -1; /* descriptor was not passed */
173 if (msg.msg_accrightslen == sizeof(int)) {
177 *recvfd = -1; /* descriptor was not passed */
184 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
189 #ifdef HAVE_MSGHDR_MSG_CONTROL
192 char control[CMSG_SPACE(sizeof(int))];
194 struct cmsghdr *cmptr;
197 ZERO_STRUCT(control_un);
199 msg.msg_control = control_un.control;
200 msg.msg_controllen = sizeof(control_un.control);
202 cmptr = CMSG_FIRSTHDR(&msg);
203 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
204 cmptr->cmsg_level = SOL_SOCKET;
205 cmptr->cmsg_type = SCM_RIGHTS;
206 memcpy(CMSG_DATA(cmptr), &sendfd, sizeof(sendfd));
209 msg.msg_accrights = (caddr_t) &sendfd;
210 msg.msg_accrightslen = sizeof(int);
217 iov[0].iov_base = (void *)ptr;
218 iov[0].iov_len = nbytes;
222 return (sendmsg(fd, &msg, 0));
225 static void aio_child_cleanup(struct event_context *event_ctx,
226 struct timed_event *te,
230 struct aio_child_list *list = talloc_get_type_abort(
231 private_data, struct aio_child_list);
232 struct aio_child *child, *next;
234 TALLOC_FREE(list->cleanup_event);
236 for (child = list->children; child != NULL; child = next) {
240 DEBUG(10, ("child %d currently active\n",
245 if (child->dont_delete) {
246 DEBUG(10, ("Child %d was active since last cleanup\n",
248 child->dont_delete = false;
252 DEBUG(10, ("Child %d idle for more than 30 seconds, "
253 "deleting\n", (int)child->pid));
259 if (list->children != NULL) {
261 * Re-schedule the next cleanup round
263 list->cleanup_event = event_add_timed(server_event_context(), list,
264 timeval_add(&now, 30, 0),
265 aio_child_cleanup, list);
270 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
272 struct aio_child_list *data = NULL;
274 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
275 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
280 data = talloc_zero(NULL, struct aio_child_list);
287 * Regardless of whether the child_list had been around or not, make
288 * sure that we have a cleanup timed event. This timed event will
289 * delete itself when it finds that no children are around anymore.
292 if (data->cleanup_event == NULL) {
293 data->cleanup_event = event_add_timed(server_event_context(), data,
294 timeval_current_ofs(30, 0),
295 aio_child_cleanup, data);
296 if (data->cleanup_event == NULL) {
302 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
303 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
304 struct aio_child_list, return False);
310 static void aio_child_loop(int sockfd, struct mmap_area *map)
315 struct rw_cmd cmd_struct;
316 struct rw_ret ret_struct;
318 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
319 if (ret != sizeof(cmd_struct)) {
320 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
325 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
326 cmd_struct.read_cmd ? "read" : "write",
327 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
329 #ifdef ENABLE_BUILD_FARM_HACKS
332 * In the build farm, we want erratic behaviour for
338 * use generate_random_buffer, we just forked from a
339 * common parent state
341 generate_random_buffer(&randval, sizeof(randval));
342 msecs = randval + 20;
343 DEBUG(10, ("delaying for %u msecs\n", msecs));
349 ZERO_STRUCT(ret_struct);
351 if (cmd_struct.read_cmd) {
352 ret_struct.size = sys_pread(
353 fd, (void *)map->ptr, cmd_struct.n,
356 /* This breaks "make test" when run with aio_fork module. */
357 #ifdef ENABLE_BUILD_FARM_HACKS
358 ret_struct.size = MAX(1, ret_struct.size * 0.9);
363 ret_struct.size = sys_pwrite(
364 fd, (void *)map->ptr, cmd_struct.n,
368 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
369 (int)ret_struct.size));
371 if (ret_struct.size == -1) {
372 ret_struct.ret_errno = errno;
376 * Close the fd before telling our parent we're done. The
377 * parent might close and re-open the file very quickly, and
378 * with system-level share modes (GPFS) we would get an
379 * unjustified SHARING_VIOLATION.
383 ret = write_data(sockfd, (char *)&ret_struct,
385 if (ret != sizeof(ret_struct)) {
386 DEBUG(10, ("could not write ret_struct: %s\n",
393 static int aio_child_destructor(struct aio_child *child)
397 SMB_ASSERT(!child->busy);
399 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
400 child->pid, child->sockfd));
403 * closing the sockfd makes the child not return from recvmsg() on RHEL
404 * 5.5 so instead force the child to exit by writing bad data to it
406 write(child->sockfd, &c, sizeof(c));
407 close(child->sockfd);
408 DLIST_REMOVE(child->list->children, child);
413 * We have to close all fd's in open files, we might incorrectly hold a system
414 * level share mode on a file.
417 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
420 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
427 static int create_aio_child(struct smbd_server_connection *sconn,
428 struct aio_child_list *children,
430 struct aio_child **presult)
432 struct aio_child *result;
436 fdpair[0] = fdpair[1] = -1;
438 result = talloc_zero(children, struct aio_child);
439 if (result == NULL) {
443 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
445 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
449 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
451 result->map = mmap_area_init(result, map_size);
452 if (result->map == NULL) {
454 DEBUG(0, ("Could not create mmap area\n"));
458 result->pid = fork();
459 if (result->pid == -1) {
461 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
465 if (result->pid == 0) {
467 result->sockfd = fdpair[1];
468 files_forall(sconn, close_fsp_fd, NULL);
469 aio_child_loop(result->sockfd, result->map);
472 DEBUG(10, ("Child %d created with sockfd %d\n",
473 result->pid, fdpair[0]));
475 result->sockfd = fdpair[0];
478 result->list = children;
479 DLIST_ADD(children->children, result);
481 talloc_set_destructor(result, aio_child_destructor);
488 if (fdpair[0] != -1) close(fdpair[0]);
489 if (fdpair[1] != -1) close(fdpair[1]);
495 static int get_idle_child(struct vfs_handle_struct *handle,
496 struct aio_child **pchild)
498 struct aio_child_list *children;
499 struct aio_child *child;
501 children = init_aio_children(handle);
502 if (children == NULL) {
506 for (child = children->children; child != NULL; child = child->next) {
515 DEBUG(10, ("no idle child found, creating new one\n"));
517 ret = create_aio_child(handle->conn->sconn, children,
520 DEBUG(10, ("create_aio_child failed: %s\n",
526 child->dont_delete = true;
533 struct aio_fork_pread_state {
534 struct aio_child *child;
539 static void aio_fork_pread_done(struct tevent_req *subreq);
541 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
543 struct tevent_context *ev,
544 struct files_struct *fsp,
546 size_t n, off_t offset)
548 struct tevent_req *req, *subreq;
549 struct aio_fork_pread_state *state;
554 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
560 /* TODO: support variable buffers */
561 tevent_req_error(req, EINVAL);
562 return tevent_req_post(req, ev);
565 err = get_idle_child(handle, &state->child);
567 tevent_req_error(req, err);
568 return tevent_req_post(req, ev);
576 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
577 (int)state->child->pid));
580 * Not making this async. We're writing into an empty unix
581 * domain socket. This should never block.
583 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
588 TALLOC_FREE(state->child);
590 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
591 tevent_req_error(req, err);
592 return tevent_req_post(req, ev);
595 subreq = read_packet_send(state, ev, state->child->sockfd,
596 sizeof(struct rw_ret), NULL, NULL);
597 if (tevent_req_nomem(subreq, req)) {
598 TALLOC_FREE(state->child); /* we sent sth down */
599 return tevent_req_post(req, ev);
601 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
605 static void aio_fork_pread_done(struct tevent_req *subreq)
607 struct tevent_req *req = tevent_req_callback_data(
608 subreq, struct tevent_req);
609 struct aio_fork_pread_state *state = tevent_req_data(
610 req, struct aio_fork_pread_state);
614 struct rw_ret *retbuf;
616 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
619 TALLOC_FREE(state->child);
620 tevent_req_error(req, err);
624 state->child->busy = false;
626 retbuf = (struct rw_ret *)buf;
627 state->ret = retbuf->size;
628 state->err = retbuf->ret_errno;
629 tevent_req_done(req);
632 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
634 struct aio_fork_pread_state *state = tevent_req_data(
635 req, struct aio_fork_pread_state);
637 if (tevent_req_is_unix_error(req, err)) {
640 if (state->ret == -1) {
646 struct aio_fork_pwrite_state {
647 struct aio_child *child;
652 static void aio_fork_pwrite_done(struct tevent_req *subreq);
654 static struct tevent_req *aio_fork_pwrite_send(
655 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
656 struct tevent_context *ev, struct files_struct *fsp,
657 const void *data, size_t n, off_t offset)
659 struct tevent_req *req, *subreq;
660 struct aio_fork_pwrite_state *state;
665 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
671 /* TODO: support variable buffers */
672 tevent_req_error(req, EINVAL);
673 return tevent_req_post(req, ev);
676 err = get_idle_child(handle, &state->child);
678 tevent_req_error(req, err);
679 return tevent_req_post(req, ev);
685 cmd.read_cmd = false;
687 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
688 (int)state->child->pid));
691 * Not making this async. We're writing into an empty unix
692 * domain socket. This should never block.
694 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
699 TALLOC_FREE(state->child);
701 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
702 tevent_req_error(req, err);
703 return tevent_req_post(req, ev);
706 subreq = read_packet_send(state, ev, state->child->sockfd,
707 sizeof(struct rw_ret), NULL, NULL);
708 if (tevent_req_nomem(subreq, req)) {
709 TALLOC_FREE(state->child); /* we sent sth down */
710 return tevent_req_post(req, ev);
712 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
716 static void aio_fork_pwrite_done(struct tevent_req *subreq)
718 struct tevent_req *req = tevent_req_callback_data(
719 subreq, struct tevent_req);
720 struct aio_fork_pwrite_state *state = tevent_req_data(
721 req, struct aio_fork_pwrite_state);
725 struct rw_ret *retbuf;
727 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
730 TALLOC_FREE(state->child);
731 tevent_req_error(req, err);
735 state->child->busy = false;
737 retbuf = (struct rw_ret *)buf;
738 state->ret = retbuf->size;
739 state->err = retbuf->ret_errno;
740 tevent_req_done(req);
743 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
745 struct aio_fork_pwrite_state *state = tevent_req_data(
746 req, struct aio_fork_pwrite_state);
748 if (tevent_req_is_unix_error(req, err)) {
751 if (state->ret == -1) {
757 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
760 /*********************************************************************
761 * How many threads to initialize ?
762 * 100 per process seems insane as a default until you realize that
763 * (a) Threads terminate after 1 second when idle.
764 * (b) Throttling is done in SMB2 via the crediting algorithm.
765 * (c) SMB1 clients are limited to max_mux (50) outstanding
766 * requests and Windows clients don't use this anyway.
767 * Essentially we want this to be unlimited unless smb.conf
769 *********************************************************************/
770 aio_pending_size = 100;
771 return SMB_VFS_NEXT_CONNECT(handle, service, user);
774 static struct vfs_fn_pointers vfs_aio_fork_fns = {
775 .connect_fn = aio_fork_connect,
776 .pread_send_fn = aio_fork_pread_send,
777 .pread_recv_fn = aio_fork_pread_recv,
778 .pwrite_send_fn = aio_fork_pwrite_send,
779 .pwrite_recv_fn = aio_fork_pwrite_recv,
782 NTSTATUS vfs_aio_fork_init(void);
783 NTSTATUS vfs_aio_fork_init(void)
785 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
786 "aio_fork", &vfs_aio_fork_fns);