build: In install_with_python.sh force using the python from the install we just...
[Samba/bb.git] / source3 / modules / vfs_aio_fork.c
blob3db336f9d5b9da3fa38138ed5e4781398eca043a
1 /*
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.
22 #include "includes.h"
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 #undef recvmsg
32 #ifndef MAP_FILE
33 #define MAP_FILE 0
34 #endif
36 struct aio_fork_config {
37 bool erratic_testing_mode;
40 struct mmap_area {
41 size_t size;
42 volatile void *ptr;
45 static int mmap_area_destructor(struct mmap_area *area)
47 munmap((void *)area->ptr, area->size);
48 return 0;
51 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
53 struct mmap_area *result;
54 int fd;
56 result = talloc(mem_ctx, struct mmap_area);
57 if (result == NULL) {
58 DEBUG(0, ("talloc failed\n"));
59 goto fail;
62 fd = open("/dev/zero", O_RDWR);
63 if (fd == -1) {
64 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
65 strerror(errno)));
66 goto fail;
69 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
70 MAP_SHARED|MAP_FILE, fd, 0);
71 if (result->ptr == MAP_FAILED) {
72 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
73 goto fail;
76 close(fd);
78 result->size = size;
79 talloc_set_destructor(result, mmap_area_destructor);
81 return result;
83 fail:
84 TALLOC_FREE(result);
85 return NULL;
88 enum cmd_type {
89 READ_CMD,
90 WRITE_CMD,
91 FSYNC_CMD
94 static const char *cmd_type_str(enum cmd_type cmd)
96 const char *result;
98 switch (cmd) {
99 case READ_CMD:
100 result = "READ";
101 break;
102 case WRITE_CMD:
103 result = "WRITE";
104 break;
105 case FSYNC_CMD:
106 result = "FSYNC";
107 break;
108 default:
109 result = "<UNKNOWN>";
110 break;
112 return result;
115 struct rw_cmd {
116 size_t n;
117 off_t offset;
118 enum cmd_type cmd;
119 bool erratic_testing_mode;
122 struct rw_ret {
123 ssize_t size;
124 int ret_errno;
127 struct aio_child_list;
129 struct aio_child {
130 struct aio_child *prev, *next;
131 struct aio_child_list *list;
132 pid_t pid;
133 int sockfd;
134 struct mmap_area *map;
135 bool dont_delete; /* Marked as in use since last cleanup */
136 bool busy;
139 struct aio_child_list {
140 struct aio_child *children;
141 struct timed_event *cleanup_event;
144 static void free_aio_children(void **p)
146 TALLOC_FREE(*p);
149 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
151 struct msghdr msg;
152 struct iovec iov[1];
153 ssize_t n;
154 #ifndef HAVE_MSGHDR_MSG_CONTROL
155 int newfd;
156 #endif
158 #ifdef HAVE_MSGHDR_MSG_CONTROL
159 union {
160 struct cmsghdr cm;
161 char control[CMSG_SPACE(sizeof(int))];
162 } control_un;
163 struct cmsghdr *cmptr;
165 msg.msg_control = control_un.control;
166 msg.msg_controllen = sizeof(control_un.control);
167 #else
168 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
169 msg.msg_accrights = (caddr_t) &newfd;
170 msg.msg_accrightslen = sizeof(int);
171 #else
172 #error Can not pass file descriptors
173 #endif
174 #endif
176 msg.msg_name = NULL;
177 msg.msg_namelen = 0;
178 msg.msg_flags = 0;
180 iov[0].iov_base = (void *)ptr;
181 iov[0].iov_len = nbytes;
182 msg.msg_iov = iov;
183 msg.msg_iovlen = 1;
185 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
186 return(n);
189 #ifdef HAVE_MSGHDR_MSG_CONTROL
190 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
191 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
192 if (cmptr->cmsg_level != SOL_SOCKET) {
193 DEBUG(10, ("control level != SOL_SOCKET"));
194 errno = EINVAL;
195 return -1;
197 if (cmptr->cmsg_type != SCM_RIGHTS) {
198 DEBUG(10, ("control type != SCM_RIGHTS"));
199 errno = EINVAL;
200 return -1;
202 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
203 } else {
204 *recvfd = -1; /* descriptor was not passed */
206 #else
207 if (msg.msg_accrightslen == sizeof(int)) {
208 *recvfd = newfd;
210 else {
211 *recvfd = -1; /* descriptor was not passed */
213 #endif
215 return(n);
218 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
220 struct msghdr msg;
221 struct iovec iov[1];
223 #ifdef HAVE_MSGHDR_MSG_CONTROL
224 union {
225 struct cmsghdr cm;
226 char control[CMSG_SPACE(sizeof(int))];
227 } control_un;
228 struct cmsghdr *cmptr;
230 ZERO_STRUCT(msg);
231 ZERO_STRUCT(control_un);
233 msg.msg_control = control_un.control;
234 msg.msg_controllen = sizeof(control_un.control);
236 cmptr = CMSG_FIRSTHDR(&msg);
237 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
238 cmptr->cmsg_level = SOL_SOCKET;
239 cmptr->cmsg_type = SCM_RIGHTS;
240 memcpy(CMSG_DATA(cmptr), &sendfd, sizeof(sendfd));
241 #else
242 ZERO_STRUCT(msg);
243 msg.msg_accrights = (caddr_t) &sendfd;
244 msg.msg_accrightslen = sizeof(int);
245 #endif
247 msg.msg_name = NULL;
248 msg.msg_namelen = 0;
250 ZERO_STRUCT(iov);
251 iov[0].iov_base = (void *)ptr;
252 iov[0].iov_len = nbytes;
253 msg.msg_iov = iov;
254 msg.msg_iovlen = 1;
256 return (sendmsg(fd, &msg, 0));
259 static void aio_child_cleanup(struct event_context *event_ctx,
260 struct timed_event *te,
261 struct timeval now,
262 void *private_data)
264 struct aio_child_list *list = talloc_get_type_abort(
265 private_data, struct aio_child_list);
266 struct aio_child *child, *next;
268 TALLOC_FREE(list->cleanup_event);
270 for (child = list->children; child != NULL; child = next) {
271 next = child->next;
273 if (child->busy) {
274 DEBUG(10, ("child %d currently active\n",
275 (int)child->pid));
276 continue;
279 if (child->dont_delete) {
280 DEBUG(10, ("Child %d was active since last cleanup\n",
281 (int)child->pid));
282 child->dont_delete = false;
283 continue;
286 DEBUG(10, ("Child %d idle for more than 30 seconds, "
287 "deleting\n", (int)child->pid));
289 TALLOC_FREE(child);
290 child = next;
293 if (list->children != NULL) {
295 * Re-schedule the next cleanup round
297 list->cleanup_event = event_add_timed(server_event_context(), list,
298 timeval_add(&now, 30, 0),
299 aio_child_cleanup, list);
304 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
306 struct aio_child_list *data = NULL;
308 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
309 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
310 return NULL);
313 if (data == NULL) {
314 data = talloc_zero(NULL, struct aio_child_list);
315 if (data == NULL) {
316 return NULL;
321 * Regardless of whether the child_list had been around or not, make
322 * sure that we have a cleanup timed event. This timed event will
323 * delete itself when it finds that no children are around anymore.
326 if (data->cleanup_event == NULL) {
327 data->cleanup_event = event_add_timed(server_event_context(), data,
328 timeval_current_ofs(30, 0),
329 aio_child_cleanup, data);
330 if (data->cleanup_event == NULL) {
331 TALLOC_FREE(data);
332 return NULL;
336 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
337 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
338 struct aio_child_list, return False);
341 return data;
344 static void aio_child_loop(int sockfd, struct mmap_area *map)
346 while (true) {
347 int fd = -1;
348 ssize_t ret;
349 struct rw_cmd cmd_struct;
350 struct rw_ret ret_struct;
352 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
353 if (ret != sizeof(cmd_struct)) {
354 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
355 strerror(errno)));
356 exit(1);
359 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
360 cmd_type_str(cmd_struct.cmd),
361 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
363 if (cmd_struct.erratic_testing_mode) {
365 * For developer testing, we want erratic behaviour for
366 * async I/O times
368 uint8_t randval;
369 unsigned msecs;
371 * use generate_random_buffer, we just forked from a
372 * common parent state
374 generate_random_buffer(&randval, sizeof(randval));
375 msecs = randval + 20;
376 DEBUG(10, ("delaying for %u msecs\n", msecs));
377 smb_msleep(msecs);
380 ZERO_STRUCT(ret_struct);
382 switch (cmd_struct.cmd) {
383 case READ_CMD:
384 ret_struct.size = sys_pread(
385 fd, (void *)map->ptr, cmd_struct.n,
386 cmd_struct.offset);
387 #if 0
388 /* This breaks "make test" when run with aio_fork module. */
389 #ifdef DEVELOPER
390 ret_struct.size = MAX(1, ret_struct.size * 0.9);
391 #endif
392 #endif
393 break;
394 case WRITE_CMD:
395 ret_struct.size = sys_pwrite(
396 fd, (void *)map->ptr, cmd_struct.n,
397 cmd_struct.offset);
398 break;
399 case FSYNC_CMD:
400 ret_struct.size = fsync(fd);
401 break;
402 default:
403 ret_struct.size = -1;
404 errno = EINVAL;
407 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
408 (int)ret_struct.size));
410 if (ret_struct.size == -1) {
411 ret_struct.ret_errno = errno;
415 * Close the fd before telling our parent we're done. The
416 * parent might close and re-open the file very quickly, and
417 * with system-level share modes (GPFS) we would get an
418 * unjustified SHARING_VIOLATION.
420 close(fd);
422 ret = write_data(sockfd, (char *)&ret_struct,
423 sizeof(ret_struct));
424 if (ret != sizeof(ret_struct)) {
425 DEBUG(10, ("could not write ret_struct: %s\n",
426 strerror(errno)));
427 exit(2);
432 static int aio_child_destructor(struct aio_child *child)
434 char c=0;
436 SMB_ASSERT(!child->busy);
438 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
439 child->pid, child->sockfd));
442 * closing the sockfd makes the child not return from recvmsg() on RHEL
443 * 5.5 so instead force the child to exit by writing bad data to it
445 write(child->sockfd, &c, sizeof(c));
446 close(child->sockfd);
447 DLIST_REMOVE(child->list->children, child);
448 return 0;
452 * We have to close all fd's in open files, we might incorrectly hold a system
453 * level share mode on a file.
456 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
457 void *private_data)
459 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
460 close(fsp->fh->fd);
461 fsp->fh->fd = -1;
463 return NULL;
466 static int create_aio_child(struct smbd_server_connection *sconn,
467 struct aio_child_list *children,
468 size_t map_size,
469 struct aio_child **presult)
471 struct aio_child *result;
472 int fdpair[2];
473 int ret;
475 fdpair[0] = fdpair[1] = -1;
477 result = talloc_zero(children, struct aio_child);
478 if (result == NULL) {
479 return ENOMEM;
482 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
483 ret = errno;
484 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
485 goto fail;
488 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
490 result->map = mmap_area_init(result, map_size);
491 if (result->map == NULL) {
492 ret = errno;
493 DEBUG(0, ("Could not create mmap area\n"));
494 goto fail;
497 result->pid = fork();
498 if (result->pid == -1) {
499 ret = errno;
500 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
501 goto fail;
504 if (result->pid == 0) {
505 close(fdpair[0]);
506 result->sockfd = fdpair[1];
507 files_forall(sconn, close_fsp_fd, NULL);
508 aio_child_loop(result->sockfd, result->map);
511 DEBUG(10, ("Child %d created with sockfd %d\n",
512 result->pid, fdpair[0]));
514 result->sockfd = fdpair[0];
515 close(fdpair[1]);
517 result->list = children;
518 DLIST_ADD(children->children, result);
520 talloc_set_destructor(result, aio_child_destructor);
522 *presult = result;
524 return 0;
526 fail:
527 if (fdpair[0] != -1) close(fdpair[0]);
528 if (fdpair[1] != -1) close(fdpair[1]);
529 TALLOC_FREE(result);
531 return ret;
534 static int get_idle_child(struct vfs_handle_struct *handle,
535 struct aio_child **pchild)
537 struct aio_child_list *children;
538 struct aio_child *child;
540 children = init_aio_children(handle);
541 if (children == NULL) {
542 return ENOMEM;
545 for (child = children->children; child != NULL; child = child->next) {
546 if (!child->busy) {
547 break;
551 if (child == NULL) {
552 int ret;
554 DEBUG(10, ("no idle child found, creating new one\n"));
556 ret = create_aio_child(handle->conn->sconn, children,
557 128*1024, &child);
558 if (ret != 0) {
559 DEBUG(10, ("create_aio_child failed: %s\n",
560 strerror(errno)));
561 return ret;
565 child->dont_delete = true;
566 child->busy = true;
568 *pchild = child;
569 return 0;
572 struct aio_fork_pread_state {
573 struct aio_child *child;
574 ssize_t ret;
575 int err;
578 static void aio_fork_pread_done(struct tevent_req *subreq);
580 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
581 TALLOC_CTX *mem_ctx,
582 struct tevent_context *ev,
583 struct files_struct *fsp,
584 void *data,
585 size_t n, off_t offset)
587 struct tevent_req *req, *subreq;
588 struct aio_fork_pread_state *state;
589 struct rw_cmd cmd;
590 ssize_t written;
591 int err;
592 struct aio_fork_config *config;
594 SMB_VFS_HANDLE_GET_DATA(handle, config,
595 struct aio_fork_config,
596 return NULL);
598 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
599 if (req == NULL) {
600 return NULL;
603 if (n > 128*1024) {
604 /* TODO: support variable buffers */
605 tevent_req_error(req, EINVAL);
606 return tevent_req_post(req, ev);
609 err = get_idle_child(handle, &state->child);
610 if (err != 0) {
611 tevent_req_error(req, err);
612 return tevent_req_post(req, ev);
615 ZERO_STRUCT(cmd);
616 cmd.n = n;
617 cmd.offset = offset;
618 cmd.cmd = READ_CMD;
619 cmd.erratic_testing_mode = config->erratic_testing_mode;
621 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
622 (int)state->child->pid));
625 * Not making this async. We're writing into an empty unix
626 * domain socket. This should never block.
628 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
629 fsp->fh->fd);
630 if (written == -1) {
631 err = errno;
633 TALLOC_FREE(state->child);
635 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
636 tevent_req_error(req, err);
637 return tevent_req_post(req, ev);
640 subreq = read_packet_send(state, ev, state->child->sockfd,
641 sizeof(struct rw_ret), NULL, NULL);
642 if (tevent_req_nomem(subreq, req)) {
643 TALLOC_FREE(state->child); /* we sent sth down */
644 return tevent_req_post(req, ev);
646 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
647 return req;
650 static void aio_fork_pread_done(struct tevent_req *subreq)
652 struct tevent_req *req = tevent_req_callback_data(
653 subreq, struct tevent_req);
654 struct aio_fork_pread_state *state = tevent_req_data(
655 req, struct aio_fork_pread_state);
656 ssize_t nread;
657 uint8_t *buf;
658 int err;
659 struct rw_ret *retbuf;
661 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
662 TALLOC_FREE(subreq);
663 if (nread == -1) {
664 TALLOC_FREE(state->child);
665 tevent_req_error(req, err);
666 return;
669 state->child->busy = false;
671 retbuf = (struct rw_ret *)buf;
672 state->ret = retbuf->size;
673 state->err = retbuf->ret_errno;
674 tevent_req_done(req);
677 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
679 struct aio_fork_pread_state *state = tevent_req_data(
680 req, struct aio_fork_pread_state);
682 if (tevent_req_is_unix_error(req, err)) {
683 return -1;
685 if (state->ret == -1) {
686 *err = state->err;
688 return state->ret;
691 struct aio_fork_pwrite_state {
692 struct aio_child *child;
693 ssize_t ret;
694 int err;
697 static void aio_fork_pwrite_done(struct tevent_req *subreq);
699 static struct tevent_req *aio_fork_pwrite_send(
700 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
701 struct tevent_context *ev, struct files_struct *fsp,
702 const void *data, size_t n, off_t offset)
704 struct tevent_req *req, *subreq;
705 struct aio_fork_pwrite_state *state;
706 struct rw_cmd cmd;
707 ssize_t written;
708 int err;
709 struct aio_fork_config *config;
710 SMB_VFS_HANDLE_GET_DATA(handle, config,
711 struct aio_fork_config,
712 return NULL);
714 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
715 if (req == NULL) {
716 return NULL;
719 if (n > 128*1024) {
720 /* TODO: support variable buffers */
721 tevent_req_error(req, EINVAL);
722 return tevent_req_post(req, ev);
725 err = get_idle_child(handle, &state->child);
726 if (err != 0) {
727 tevent_req_error(req, err);
728 return tevent_req_post(req, ev);
731 ZERO_STRUCT(cmd);
732 cmd.n = n;
733 cmd.offset = offset;
734 cmd.cmd = WRITE_CMD;
735 cmd.erratic_testing_mode = config->erratic_testing_mode;
737 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
738 (int)state->child->pid));
741 * Not making this async. We're writing into an empty unix
742 * domain socket. This should never block.
744 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
745 fsp->fh->fd);
746 if (written == -1) {
747 err = errno;
749 TALLOC_FREE(state->child);
751 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
752 tevent_req_error(req, err);
753 return tevent_req_post(req, ev);
756 subreq = read_packet_send(state, ev, state->child->sockfd,
757 sizeof(struct rw_ret), NULL, NULL);
758 if (tevent_req_nomem(subreq, req)) {
759 TALLOC_FREE(state->child); /* we sent sth down */
760 return tevent_req_post(req, ev);
762 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
763 return req;
766 static void aio_fork_pwrite_done(struct tevent_req *subreq)
768 struct tevent_req *req = tevent_req_callback_data(
769 subreq, struct tevent_req);
770 struct aio_fork_pwrite_state *state = tevent_req_data(
771 req, struct aio_fork_pwrite_state);
772 ssize_t nread;
773 uint8_t *buf;
774 int err;
775 struct rw_ret *retbuf;
777 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
778 TALLOC_FREE(subreq);
779 if (nread == -1) {
780 TALLOC_FREE(state->child);
781 tevent_req_error(req, err);
782 return;
785 state->child->busy = false;
787 retbuf = (struct rw_ret *)buf;
788 state->ret = retbuf->size;
789 state->err = retbuf->ret_errno;
790 tevent_req_done(req);
793 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
795 struct aio_fork_pwrite_state *state = tevent_req_data(
796 req, struct aio_fork_pwrite_state);
798 if (tevent_req_is_unix_error(req, err)) {
799 return -1;
801 if (state->ret == -1) {
802 *err = state->err;
804 return state->ret;
807 struct aio_fork_fsync_state {
808 struct aio_child *child;
809 ssize_t ret;
810 int err;
813 static void aio_fork_fsync_done(struct tevent_req *subreq);
815 static struct tevent_req *aio_fork_fsync_send(
816 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
817 struct tevent_context *ev, struct files_struct *fsp)
819 struct tevent_req *req, *subreq;
820 struct aio_fork_fsync_state *state;
821 struct rw_cmd cmd;
822 ssize_t written;
823 int err;
824 struct aio_fork_config *config;
826 SMB_VFS_HANDLE_GET_DATA(handle, config,
827 struct aio_fork_config,
828 return NULL);
830 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
831 if (req == NULL) {
832 return NULL;
835 err = get_idle_child(handle, &state->child);
836 if (err != 0) {
837 tevent_req_error(req, err);
838 return tevent_req_post(req, ev);
841 ZERO_STRUCT(cmd);
842 cmd.cmd = FSYNC_CMD;
843 cmd.erratic_testing_mode = config->erratic_testing_mode;
845 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
846 (int)state->child->pid));
849 * Not making this async. We're writing into an empty unix
850 * domain socket. This should never block.
852 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
853 fsp->fh->fd);
854 if (written == -1) {
855 err = errno;
857 TALLOC_FREE(state->child);
859 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
860 tevent_req_error(req, err);
861 return tevent_req_post(req, ev);
864 subreq = read_packet_send(state, ev, state->child->sockfd,
865 sizeof(struct rw_ret), NULL, NULL);
866 if (tevent_req_nomem(subreq, req)) {
867 TALLOC_FREE(state->child); /* we sent sth down */
868 return tevent_req_post(req, ev);
870 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
871 return req;
874 static void aio_fork_fsync_done(struct tevent_req *subreq)
876 struct tevent_req *req = tevent_req_callback_data(
877 subreq, struct tevent_req);
878 struct aio_fork_fsync_state *state = tevent_req_data(
879 req, struct aio_fork_fsync_state);
880 ssize_t nread;
881 uint8_t *buf;
882 int err;
883 struct rw_ret *retbuf;
885 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
886 TALLOC_FREE(subreq);
887 if (nread == -1) {
888 TALLOC_FREE(state->child);
889 tevent_req_error(req, err);
890 return;
893 state->child->busy = false;
895 retbuf = (struct rw_ret *)buf;
896 state->ret = retbuf->size;
897 state->err = retbuf->ret_errno;
898 tevent_req_done(req);
901 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
903 struct aio_fork_fsync_state *state = tevent_req_data(
904 req, struct aio_fork_fsync_state);
906 if (tevent_req_is_unix_error(req, err)) {
907 return -1;
909 if (state->ret == -1) {
910 *err = state->err;
912 return state->ret;
915 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
916 const char *user)
918 int ret;
919 struct aio_fork_config *config;
920 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
922 if (ret < 0) {
923 return ret;
926 config = talloc_zero(handle->conn, struct aio_fork_config);
927 if (!config) {
928 SMB_VFS_NEXT_DISCONNECT(handle);
929 DEBUG(0, ("talloc_zero() failed\n"));
930 return -1;
933 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
934 "erratic_testing_mode", false);
936 SMB_VFS_HANDLE_SET_DATA(handle, config,
937 NULL, struct aio_fork_config,
938 return -1);
940 /*********************************************************************
941 * How many threads to initialize ?
942 * 100 per process seems insane as a default until you realize that
943 * (a) Threads terminate after 1 second when idle.
944 * (b) Throttling is done in SMB2 via the crediting algorithm.
945 * (c) SMB1 clients are limited to max_mux (50) outstanding
946 * requests and Windows clients don't use this anyway.
947 * Essentially we want this to be unlimited unless smb.conf
948 * says different.
949 *********************************************************************/
950 aio_pending_size = 100;
951 return 0;
954 static struct vfs_fn_pointers vfs_aio_fork_fns = {
955 .connect_fn = aio_fork_connect,
956 .pread_send_fn = aio_fork_pread_send,
957 .pread_recv_fn = aio_fork_pread_recv,
958 .pwrite_send_fn = aio_fork_pwrite_send,
959 .pwrite_recv_fn = aio_fork_pwrite_recv,
960 .fsync_send_fn = aio_fork_fsync_send,
961 .fsync_recv_fn = aio_fork_fsync_recv,
964 NTSTATUS vfs_aio_fork_init(void);
965 NTSTATUS vfs_aio_fork_init(void)
967 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
968 "aio_fork", &vfs_aio_fork_fns);