s3-vfs: Make vfs_aio_fork erratic timing behaviour a run-time option
[Samba/gebeck_regimport.git] / source3 / modules / vfs_aio_fork.c
blob2ec3d3d537c46830f0dcb8d91b94d063b457430a
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;
593 SMB_VFS_HANDLE_GET_DATA(handle, config,
594 struct aio_fork_config,
595 return -1);
597 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
598 if (req == NULL) {
599 return NULL;
602 if (n > 128*1024) {
603 /* TODO: support variable buffers */
604 tevent_req_error(req, EINVAL);
605 return tevent_req_post(req, ev);
608 err = get_idle_child(handle, &state->child);
609 if (err != 0) {
610 tevent_req_error(req, err);
611 return tevent_req_post(req, ev);
614 ZERO_STRUCT(cmd);
615 cmd.n = n;
616 cmd.offset = offset;
617 cmd.cmd = READ_CMD;
618 cmd.erratic_testing_mode = config->erratic_testing_mode;
620 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
621 (int)state->child->pid));
624 * Not making this async. We're writing into an empty unix
625 * domain socket. This should never block.
627 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
628 fsp->fh->fd);
629 if (written == -1) {
630 err = errno;
632 TALLOC_FREE(state->child);
634 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
635 tevent_req_error(req, err);
636 return tevent_req_post(req, ev);
639 subreq = read_packet_send(state, ev, state->child->sockfd,
640 sizeof(struct rw_ret), NULL, NULL);
641 if (tevent_req_nomem(subreq, req)) {
642 TALLOC_FREE(state->child); /* we sent sth down */
643 return tevent_req_post(req, ev);
645 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
646 return req;
649 static void aio_fork_pread_done(struct tevent_req *subreq)
651 struct tevent_req *req = tevent_req_callback_data(
652 subreq, struct tevent_req);
653 struct aio_fork_pread_state *state = tevent_req_data(
654 req, struct aio_fork_pread_state);
655 ssize_t nread;
656 uint8_t *buf;
657 int err;
658 struct rw_ret *retbuf;
660 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
661 TALLOC_FREE(subreq);
662 if (nread == -1) {
663 TALLOC_FREE(state->child);
664 tevent_req_error(req, err);
665 return;
668 state->child->busy = false;
670 retbuf = (struct rw_ret *)buf;
671 state->ret = retbuf->size;
672 state->err = retbuf->ret_errno;
673 tevent_req_done(req);
676 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
678 struct aio_fork_pread_state *state = tevent_req_data(
679 req, struct aio_fork_pread_state);
681 if (tevent_req_is_unix_error(req, err)) {
682 return -1;
684 if (state->ret == -1) {
685 *err = state->err;
687 return state->ret;
690 struct aio_fork_pwrite_state {
691 struct aio_child *child;
692 ssize_t ret;
693 int err;
696 static void aio_fork_pwrite_done(struct tevent_req *subreq);
698 static struct tevent_req *aio_fork_pwrite_send(
699 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
700 struct tevent_context *ev, struct files_struct *fsp,
701 const void *data, size_t n, off_t offset)
703 struct tevent_req *req, *subreq;
704 struct aio_fork_pwrite_state *state;
705 struct rw_cmd cmd;
706 ssize_t written;
707 int err;
708 struct aio_fork_config *config;
709 SMB_VFS_HANDLE_GET_DATA(handle, config,
710 struct aio_fork_config,
711 return NULL);
713 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
714 if (req == NULL) {
715 return NULL;
718 if (n > 128*1024) {
719 /* TODO: support variable buffers */
720 tevent_req_error(req, EINVAL);
721 return tevent_req_post(req, ev);
724 err = get_idle_child(handle, &state->child);
725 if (err != 0) {
726 tevent_req_error(req, err);
727 return tevent_req_post(req, ev);
730 ZERO_STRUCT(cmd);
731 cmd.n = n;
732 cmd.offset = offset;
733 cmd.cmd = WRITE_CMD;
734 cmd.erratic_testing_mode = config->erratic_testing_mode;
736 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
737 (int)state->child->pid));
740 * Not making this async. We're writing into an empty unix
741 * domain socket. This should never block.
743 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
744 fsp->fh->fd);
745 if (written == -1) {
746 err = errno;
748 TALLOC_FREE(state->child);
750 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
751 tevent_req_error(req, err);
752 return tevent_req_post(req, ev);
755 subreq = read_packet_send(state, ev, state->child->sockfd,
756 sizeof(struct rw_ret), NULL, NULL);
757 if (tevent_req_nomem(subreq, req)) {
758 TALLOC_FREE(state->child); /* we sent sth down */
759 return tevent_req_post(req, ev);
761 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
762 return req;
765 static void aio_fork_pwrite_done(struct tevent_req *subreq)
767 struct tevent_req *req = tevent_req_callback_data(
768 subreq, struct tevent_req);
769 struct aio_fork_pwrite_state *state = tevent_req_data(
770 req, struct aio_fork_pwrite_state);
771 ssize_t nread;
772 uint8_t *buf;
773 int err;
774 struct rw_ret *retbuf;
776 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
777 TALLOC_FREE(subreq);
778 if (nread == -1) {
779 TALLOC_FREE(state->child);
780 tevent_req_error(req, err);
781 return;
784 state->child->busy = false;
786 retbuf = (struct rw_ret *)buf;
787 state->ret = retbuf->size;
788 state->err = retbuf->ret_errno;
789 tevent_req_done(req);
792 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
794 struct aio_fork_pwrite_state *state = tevent_req_data(
795 req, struct aio_fork_pwrite_state);
797 if (tevent_req_is_unix_error(req, err)) {
798 return -1;
800 if (state->ret == -1) {
801 *err = state->err;
803 return state->ret;
806 struct aio_fork_fsync_state {
807 struct aio_child *child;
808 ssize_t ret;
809 int err;
812 static void aio_fork_fsync_done(struct tevent_req *subreq);
814 static struct tevent_req *aio_fork_fsync_send(
815 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
816 struct tevent_context *ev, struct files_struct *fsp)
818 struct tevent_req *req, *subreq;
819 struct aio_fork_fsync_state *state;
820 struct rw_cmd cmd;
821 ssize_t written;
822 int err;
823 struct aio_fork_config *config;
824 SMB_VFS_HANDLE_GET_DATA(handle, config,
825 struct aio_fork_config,
826 return -1);
828 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
829 if (req == NULL) {
830 return NULL;
833 err = get_idle_child(handle, &state->child);
834 if (err != 0) {
835 tevent_req_error(req, err);
836 return tevent_req_post(req, ev);
839 ZERO_STRUCT(cmd);
840 cmd.cmd = FSYNC_CMD;
841 cmd.erratic_testing_mode = config->erratic_testing_mode;
843 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
844 (int)state->child->pid));
847 * Not making this async. We're writing into an empty unix
848 * domain socket. This should never block.
850 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
851 fsp->fh->fd);
852 if (written == -1) {
853 err = errno;
855 TALLOC_FREE(state->child);
857 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
858 tevent_req_error(req, err);
859 return tevent_req_post(req, ev);
862 subreq = read_packet_send(state, ev, state->child->sockfd,
863 sizeof(struct rw_ret), NULL, NULL);
864 if (tevent_req_nomem(subreq, req)) {
865 TALLOC_FREE(state->child); /* we sent sth down */
866 return tevent_req_post(req, ev);
868 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
869 return req;
872 static void aio_fork_fsync_done(struct tevent_req *subreq)
874 struct tevent_req *req = tevent_req_callback_data(
875 subreq, struct tevent_req);
876 struct aio_fork_fsync_state *state = tevent_req_data(
877 req, struct aio_fork_fsync_state);
878 ssize_t nread;
879 uint8_t *buf;
880 int err;
881 struct rw_ret *retbuf;
883 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
884 TALLOC_FREE(subreq);
885 if (nread == -1) {
886 TALLOC_FREE(state->child);
887 tevent_req_error(req, err);
888 return;
891 state->child->busy = false;
893 retbuf = (struct rw_ret *)buf;
894 state->ret = retbuf->size;
895 state->err = retbuf->ret_errno;
896 tevent_req_done(req);
899 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
901 struct aio_fork_fsync_state *state = tevent_req_data(
902 req, struct aio_fork_fsync_state);
904 if (tevent_req_is_unix_error(req, err)) {
905 return -1;
907 if (state->ret == -1) {
908 *err = state->err;
910 return state->ret;
913 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
914 const char *user)
916 int ret;
917 struct aio_fork_config *config;
918 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
920 if (ret < 0) {
921 return ret;
924 config = talloc_zero(handle->conn, struct aio_fork_config);
925 if (!config) {
926 SMB_VFS_NEXT_DISCONNECT(handle);
927 DEBUG(0, ("talloc_zero() failed\n"));
928 return -1;
931 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
932 "erratic_testing_mode", false);
934 SMB_VFS_HANDLE_SET_DATA(handle, config,
935 NULL, struct aio_fork_config,
936 return -1);
938 /*********************************************************************
939 * How many threads to initialize ?
940 * 100 per process seems insane as a default until you realize that
941 * (a) Threads terminate after 1 second when idle.
942 * (b) Throttling is done in SMB2 via the crediting algorithm.
943 * (c) SMB1 clients are limited to max_mux (50) outstanding
944 * requests and Windows clients don't use this anyway.
945 * Essentially we want this to be unlimited unless smb.conf
946 * says different.
947 *********************************************************************/
948 aio_pending_size = 100;
949 return 0;
952 static struct vfs_fn_pointers vfs_aio_fork_fns = {
953 .connect_fn = aio_fork_connect,
954 .pread_send_fn = aio_fork_pread_send,
955 .pread_recv_fn = aio_fork_pread_recv,
956 .pwrite_send_fn = aio_fork_pwrite_send,
957 .pwrite_recv_fn = aio_fork_pwrite_recv,
958 .fsync_send_fn = aio_fork_fsync_send,
959 .fsync_recv_fn = aio_fork_fsync_recv,
962 NTSTATUS vfs_aio_fork_init(void);
963 NTSTATUS vfs_aio_fork_init(void)
965 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
966 "aio_fork", &vfs_aio_fork_fns);