smbd: Use msghdr_prep_fds in vfs_aio_fork
[Samba.git] / source3 / modules / vfs_aio_fork.c
blob3b6699bd1fa81a885ed8b98ae4c91fb6ca6a62de
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"
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
35 #endif
37 #undef recvmsg
39 #ifndef MAP_FILE
40 #define MAP_FILE 0
41 #endif
43 struct aio_fork_config {
44 bool erratic_testing_mode;
47 struct mmap_area {
48 size_t size;
49 volatile void *ptr;
52 static int mmap_area_destructor(struct mmap_area *area)
54 munmap((void *)area->ptr, area->size);
55 return 0;
58 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
60 struct mmap_area *result;
61 int fd;
63 result = talloc(mem_ctx, struct mmap_area);
64 if (result == NULL) {
65 DEBUG(0, ("talloc failed\n"));
66 goto fail;
69 fd = open("/dev/zero", O_RDWR);
70 if (fd == -1) {
71 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
72 strerror(errno)));
73 goto fail;
76 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
77 MAP_SHARED|MAP_FILE, fd, 0);
78 close(fd);
79 if (result->ptr == MAP_FAILED) {
80 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
81 goto fail;
84 result->size = size;
85 talloc_set_destructor(result, mmap_area_destructor);
87 return result;
89 fail:
90 TALLOC_FREE(result);
91 return NULL;
94 enum cmd_type {
95 READ_CMD,
96 WRITE_CMD,
97 FSYNC_CMD
100 static const char *cmd_type_str(enum cmd_type cmd)
102 const char *result;
104 switch (cmd) {
105 case READ_CMD:
106 result = "READ";
107 break;
108 case WRITE_CMD:
109 result = "WRITE";
110 break;
111 case FSYNC_CMD:
112 result = "FSYNC";
113 break;
114 default:
115 result = "<UNKNOWN>";
116 break;
118 return result;
121 struct rw_cmd {
122 size_t n;
123 off_t offset;
124 enum cmd_type cmd;
125 bool erratic_testing_mode;
128 struct rw_ret {
129 ssize_t size;
130 int ret_errno;
133 struct aio_child_list;
135 struct aio_child {
136 struct aio_child *prev, *next;
137 struct aio_child_list *list;
138 pid_t pid;
139 int sockfd;
140 struct mmap_area *map;
141 bool dont_delete; /* Marked as in use since last cleanup */
142 bool busy;
145 struct aio_child_list {
146 struct aio_child *children;
147 struct tevent_timer *cleanup_event;
150 static void free_aio_children(void **p)
152 TALLOC_FREE(*p);
155 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
157 struct msghdr msg;
158 struct iovec iov[1];
159 ssize_t n;
160 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
161 int newfd;
163 ZERO_STRUCT(msg);
164 msg.msg_accrights = (caddr_t) &newfd;
165 msg.msg_accrightslen = sizeof(int);
166 #else
168 union {
169 struct cmsghdr cm;
170 char control[CMSG_SPACE(sizeof(int))];
171 } control_un;
172 struct cmsghdr *cmptr;
174 ZERO_STRUCT(msg);
175 ZERO_STRUCT(control_un);
177 msg.msg_control = control_un.control;
178 msg.msg_controllen = sizeof(control_un.control);
179 #endif
181 msg.msg_name = NULL;
182 msg.msg_namelen = 0;
184 iov[0].iov_base = (void *)ptr;
185 iov[0].iov_len = nbytes;
186 msg.msg_iov = iov;
187 msg.msg_iovlen = 1;
189 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
190 return(n);
193 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
194 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
195 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
196 if (cmptr->cmsg_level != SOL_SOCKET) {
197 DEBUG(10, ("control level != SOL_SOCKET"));
198 errno = EINVAL;
199 return -1;
201 if (cmptr->cmsg_type != SCM_RIGHTS) {
202 DEBUG(10, ("control type != SCM_RIGHTS"));
203 errno = EINVAL;
204 return -1;
206 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
207 } else {
208 *recvfd = -1; /* descriptor was not passed */
210 #else
211 if (msg.msg_accrightslen == sizeof(int)) {
212 *recvfd = newfd;
214 else {
215 *recvfd = -1; /* descriptor was not passed */
217 #endif
219 return(n);
222 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
224 struct msghdr msg;
225 size_t bufsize = msghdr_prep_fds(NULL, NULL, 0, &sendfd, 1);
226 uint8_t buf[bufsize];
227 struct iovec iov;
229 msghdr_prep_fds(&msg, buf, bufsize, &sendfd, 1);
230 msg.msg_name = NULL;
231 msg.msg_namelen = 0;
233 iov.iov_base = (void *)ptr;
234 iov.iov_len = nbytes;
235 msg.msg_iov = &iov;
236 msg.msg_iovlen = 1;
238 return (sendmsg(fd, &msg, 0));
241 static void aio_child_cleanup(struct tevent_context *event_ctx,
242 struct tevent_timer *te,
243 struct timeval now,
244 void *private_data)
246 struct aio_child_list *list = talloc_get_type_abort(
247 private_data, struct aio_child_list);
248 struct aio_child *child, *next;
250 TALLOC_FREE(list->cleanup_event);
252 for (child = list->children; child != NULL; child = next) {
253 next = child->next;
255 if (child->busy) {
256 DEBUG(10, ("child %d currently active\n",
257 (int)child->pid));
258 continue;
261 if (child->dont_delete) {
262 DEBUG(10, ("Child %d was active since last cleanup\n",
263 (int)child->pid));
264 child->dont_delete = false;
265 continue;
268 DEBUG(10, ("Child %d idle for more than 30 seconds, "
269 "deleting\n", (int)child->pid));
271 TALLOC_FREE(child);
272 child = next;
275 if (list->children != NULL) {
277 * Re-schedule the next cleanup round
279 list->cleanup_event = tevent_add_timer(server_event_context(), list,
280 timeval_add(&now, 30, 0),
281 aio_child_cleanup, list);
286 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
288 struct aio_child_list *data = NULL;
290 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
291 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
292 return NULL);
295 if (data == NULL) {
296 data = talloc_zero(NULL, struct aio_child_list);
297 if (data == NULL) {
298 return NULL;
303 * Regardless of whether the child_list had been around or not, make
304 * sure that we have a cleanup timed event. This timed event will
305 * delete itself when it finds that no children are around anymore.
308 if (data->cleanup_event == NULL) {
309 data->cleanup_event = tevent_add_timer(server_event_context(), data,
310 timeval_current_ofs(30, 0),
311 aio_child_cleanup, data);
312 if (data->cleanup_event == NULL) {
313 TALLOC_FREE(data);
314 return NULL;
318 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
319 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
320 struct aio_child_list, return False);
323 return data;
326 static void aio_child_loop(int sockfd, struct mmap_area *map)
328 while (true) {
329 int fd = -1;
330 ssize_t ret;
331 struct rw_cmd cmd_struct;
332 struct rw_ret ret_struct;
334 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
335 if (ret != sizeof(cmd_struct)) {
336 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
337 strerror(errno)));
338 exit(1);
341 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
342 cmd_type_str(cmd_struct.cmd),
343 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
345 if (cmd_struct.erratic_testing_mode) {
347 * For developer testing, we want erratic behaviour for
348 * async I/O times
350 uint8_t randval;
351 unsigned msecs;
353 * use generate_random_buffer, we just forked from a
354 * common parent state
356 generate_random_buffer(&randval, sizeof(randval));
357 msecs = randval + 20;
358 DEBUG(10, ("delaying for %u msecs\n", msecs));
359 smb_msleep(msecs);
362 ZERO_STRUCT(ret_struct);
364 switch (cmd_struct.cmd) {
365 case READ_CMD:
366 ret_struct.size = sys_pread(
367 fd, (void *)map->ptr, cmd_struct.n,
368 cmd_struct.offset);
369 #if 0
370 /* This breaks "make test" when run with aio_fork module. */
371 #ifdef DEVELOPER
372 ret_struct.size = MAX(1, ret_struct.size * 0.9);
373 #endif
374 #endif
375 break;
376 case WRITE_CMD:
377 ret_struct.size = sys_pwrite(
378 fd, (void *)map->ptr, cmd_struct.n,
379 cmd_struct.offset);
380 break;
381 case FSYNC_CMD:
382 ret_struct.size = fsync(fd);
383 break;
384 default:
385 ret_struct.size = -1;
386 errno = EINVAL;
389 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
390 (int)ret_struct.size));
392 if (ret_struct.size == -1) {
393 ret_struct.ret_errno = errno;
397 * Close the fd before telling our parent we're done. The
398 * parent might close and re-open the file very quickly, and
399 * with system-level share modes (GPFS) we would get an
400 * unjustified SHARING_VIOLATION.
402 close(fd);
404 ret = write_data(sockfd, (char *)&ret_struct,
405 sizeof(ret_struct));
406 if (ret != sizeof(ret_struct)) {
407 DEBUG(10, ("could not write ret_struct: %s\n",
408 strerror(errno)));
409 exit(2);
414 static int aio_child_destructor(struct aio_child *child)
416 char c=0;
418 SMB_ASSERT(!child->busy);
420 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
421 child->pid, child->sockfd));
424 * closing the sockfd makes the child not return from recvmsg() on RHEL
425 * 5.5 so instead force the child to exit by writing bad data to it
427 write(child->sockfd, &c, sizeof(c));
428 close(child->sockfd);
429 DLIST_REMOVE(child->list->children, child);
430 return 0;
434 * We have to close all fd's in open files, we might incorrectly hold a system
435 * level share mode on a file.
438 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
439 void *private_data)
441 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
442 close(fsp->fh->fd);
443 fsp->fh->fd = -1;
445 return NULL;
448 static int create_aio_child(struct smbd_server_connection *sconn,
449 struct aio_child_list *children,
450 size_t map_size,
451 struct aio_child **presult)
453 struct aio_child *result;
454 int fdpair[2];
455 int ret;
457 fdpair[0] = fdpair[1] = -1;
459 result = talloc_zero(children, struct aio_child);
460 if (result == NULL) {
461 return ENOMEM;
464 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
465 ret = errno;
466 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
467 goto fail;
470 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
472 result->map = mmap_area_init(result, map_size);
473 if (result->map == NULL) {
474 ret = errno;
475 DEBUG(0, ("Could not create mmap area\n"));
476 goto fail;
479 result->pid = fork();
480 if (result->pid == -1) {
481 ret = errno;
482 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
483 goto fail;
486 if (result->pid == 0) {
487 close(fdpair[0]);
488 result->sockfd = fdpair[1];
489 files_forall(sconn, close_fsp_fd, NULL);
490 aio_child_loop(result->sockfd, result->map);
493 DEBUG(10, ("Child %d created with sockfd %d\n",
494 result->pid, fdpair[0]));
496 result->sockfd = fdpair[0];
497 close(fdpair[1]);
499 result->list = children;
500 DLIST_ADD(children->children, result);
502 talloc_set_destructor(result, aio_child_destructor);
504 *presult = result;
506 return 0;
508 fail:
509 if (fdpair[0] != -1) close(fdpair[0]);
510 if (fdpair[1] != -1) close(fdpair[1]);
511 TALLOC_FREE(result);
513 return ret;
516 static int get_idle_child(struct vfs_handle_struct *handle,
517 struct aio_child **pchild)
519 struct aio_child_list *children;
520 struct aio_child *child;
522 children = init_aio_children(handle);
523 if (children == NULL) {
524 return ENOMEM;
527 for (child = children->children; child != NULL; child = child->next) {
528 if (!child->busy) {
529 break;
533 if (child == NULL) {
534 int ret;
536 DEBUG(10, ("no idle child found, creating new one\n"));
538 ret = create_aio_child(handle->conn->sconn, children,
539 128*1024, &child);
540 if (ret != 0) {
541 DEBUG(10, ("create_aio_child failed: %s\n",
542 strerror(errno)));
543 return ret;
547 child->dont_delete = true;
548 child->busy = true;
550 *pchild = child;
551 return 0;
554 struct aio_fork_pread_state {
555 struct aio_child *child;
556 ssize_t ret;
557 int err;
560 static void aio_fork_pread_done(struct tevent_req *subreq);
562 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
563 TALLOC_CTX *mem_ctx,
564 struct tevent_context *ev,
565 struct files_struct *fsp,
566 void *data,
567 size_t n, off_t offset)
569 struct tevent_req *req, *subreq;
570 struct aio_fork_pread_state *state;
571 struct rw_cmd cmd;
572 ssize_t written;
573 int err;
574 struct aio_fork_config *config;
576 SMB_VFS_HANDLE_GET_DATA(handle, config,
577 struct aio_fork_config,
578 return NULL);
580 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
581 if (req == NULL) {
582 return NULL;
585 if (n > 128*1024) {
586 /* TODO: support variable buffers */
587 tevent_req_error(req, EINVAL);
588 return tevent_req_post(req, ev);
591 err = get_idle_child(handle, &state->child);
592 if (err != 0) {
593 tevent_req_error(req, err);
594 return tevent_req_post(req, ev);
597 ZERO_STRUCT(cmd);
598 cmd.n = n;
599 cmd.offset = offset;
600 cmd.cmd = READ_CMD;
601 cmd.erratic_testing_mode = config->erratic_testing_mode;
603 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
604 (int)state->child->pid));
607 * Not making this async. We're writing into an empty unix
608 * domain socket. This should never block.
610 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
611 fsp->fh->fd);
612 if (written == -1) {
613 err = errno;
615 TALLOC_FREE(state->child);
617 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
618 tevent_req_error(req, err);
619 return tevent_req_post(req, ev);
622 subreq = read_packet_send(state, ev, state->child->sockfd,
623 sizeof(struct rw_ret), NULL, NULL);
624 if (tevent_req_nomem(subreq, req)) {
625 TALLOC_FREE(state->child); /* we sent sth down */
626 return tevent_req_post(req, ev);
628 tevent_req_set_callback(subreq, aio_fork_pread_done, req);
629 return req;
632 static void aio_fork_pread_done(struct tevent_req *subreq)
634 struct tevent_req *req = tevent_req_callback_data(
635 subreq, struct tevent_req);
636 struct aio_fork_pread_state *state = tevent_req_data(
637 req, struct aio_fork_pread_state);
638 ssize_t nread;
639 uint8_t *buf;
640 int err;
641 struct rw_ret *retbuf;
643 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
644 TALLOC_FREE(subreq);
645 if (nread == -1) {
646 TALLOC_FREE(state->child);
647 tevent_req_error(req, err);
648 return;
651 state->child->busy = false;
653 retbuf = (struct rw_ret *)buf;
654 state->ret = retbuf->size;
655 state->err = retbuf->ret_errno;
656 tevent_req_done(req);
659 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
661 struct aio_fork_pread_state *state = tevent_req_data(
662 req, struct aio_fork_pread_state);
664 if (tevent_req_is_unix_error(req, err)) {
665 return -1;
667 if (state->ret == -1) {
668 *err = state->err;
670 return state->ret;
673 struct aio_fork_pwrite_state {
674 struct aio_child *child;
675 ssize_t ret;
676 int err;
679 static void aio_fork_pwrite_done(struct tevent_req *subreq);
681 static struct tevent_req *aio_fork_pwrite_send(
682 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
683 struct tevent_context *ev, struct files_struct *fsp,
684 const void *data, size_t n, off_t offset)
686 struct tevent_req *req, *subreq;
687 struct aio_fork_pwrite_state *state;
688 struct rw_cmd cmd;
689 ssize_t written;
690 int err;
691 struct aio_fork_config *config;
692 SMB_VFS_HANDLE_GET_DATA(handle, config,
693 struct aio_fork_config,
694 return NULL);
696 req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
697 if (req == NULL) {
698 return NULL;
701 if (n > 128*1024) {
702 /* TODO: support variable buffers */
703 tevent_req_error(req, EINVAL);
704 return tevent_req_post(req, ev);
707 err = get_idle_child(handle, &state->child);
708 if (err != 0) {
709 tevent_req_error(req, err);
710 return tevent_req_post(req, ev);
713 ZERO_STRUCT(cmd);
714 cmd.n = n;
715 cmd.offset = offset;
716 cmd.cmd = WRITE_CMD;
717 cmd.erratic_testing_mode = config->erratic_testing_mode;
719 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
720 (int)state->child->pid));
723 * Not making this async. We're writing into an empty unix
724 * domain socket. This should never block.
726 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
727 fsp->fh->fd);
728 if (written == -1) {
729 err = errno;
731 TALLOC_FREE(state->child);
733 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
734 tevent_req_error(req, err);
735 return tevent_req_post(req, ev);
738 subreq = read_packet_send(state, ev, state->child->sockfd,
739 sizeof(struct rw_ret), NULL, NULL);
740 if (tevent_req_nomem(subreq, req)) {
741 TALLOC_FREE(state->child); /* we sent sth down */
742 return tevent_req_post(req, ev);
744 tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
745 return req;
748 static void aio_fork_pwrite_done(struct tevent_req *subreq)
750 struct tevent_req *req = tevent_req_callback_data(
751 subreq, struct tevent_req);
752 struct aio_fork_pwrite_state *state = tevent_req_data(
753 req, struct aio_fork_pwrite_state);
754 ssize_t nread;
755 uint8_t *buf;
756 int err;
757 struct rw_ret *retbuf;
759 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
760 TALLOC_FREE(subreq);
761 if (nread == -1) {
762 TALLOC_FREE(state->child);
763 tevent_req_error(req, err);
764 return;
767 state->child->busy = false;
769 retbuf = (struct rw_ret *)buf;
770 state->ret = retbuf->size;
771 state->err = retbuf->ret_errno;
772 tevent_req_done(req);
775 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
777 struct aio_fork_pwrite_state *state = tevent_req_data(
778 req, struct aio_fork_pwrite_state);
780 if (tevent_req_is_unix_error(req, err)) {
781 return -1;
783 if (state->ret == -1) {
784 *err = state->err;
786 return state->ret;
789 struct aio_fork_fsync_state {
790 struct aio_child *child;
791 ssize_t ret;
792 int err;
795 static void aio_fork_fsync_done(struct tevent_req *subreq);
797 static struct tevent_req *aio_fork_fsync_send(
798 struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
799 struct tevent_context *ev, struct files_struct *fsp)
801 struct tevent_req *req, *subreq;
802 struct aio_fork_fsync_state *state;
803 struct rw_cmd cmd;
804 ssize_t written;
805 int err;
806 struct aio_fork_config *config;
808 SMB_VFS_HANDLE_GET_DATA(handle, config,
809 struct aio_fork_config,
810 return NULL);
812 req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
813 if (req == NULL) {
814 return NULL;
817 err = get_idle_child(handle, &state->child);
818 if (err != 0) {
819 tevent_req_error(req, err);
820 return tevent_req_post(req, ev);
823 ZERO_STRUCT(cmd);
824 cmd.cmd = FSYNC_CMD;
825 cmd.erratic_testing_mode = config->erratic_testing_mode;
827 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
828 (int)state->child->pid));
831 * Not making this async. We're writing into an empty unix
832 * domain socket. This should never block.
834 written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
835 fsp->fh->fd);
836 if (written == -1) {
837 err = errno;
839 TALLOC_FREE(state->child);
841 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
842 tevent_req_error(req, err);
843 return tevent_req_post(req, ev);
846 subreq = read_packet_send(state, ev, state->child->sockfd,
847 sizeof(struct rw_ret), NULL, NULL);
848 if (tevent_req_nomem(subreq, req)) {
849 TALLOC_FREE(state->child); /* we sent sth down */
850 return tevent_req_post(req, ev);
852 tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
853 return req;
856 static void aio_fork_fsync_done(struct tevent_req *subreq)
858 struct tevent_req *req = tevent_req_callback_data(
859 subreq, struct tevent_req);
860 struct aio_fork_fsync_state *state = tevent_req_data(
861 req, struct aio_fork_fsync_state);
862 ssize_t nread;
863 uint8_t *buf;
864 int err;
865 struct rw_ret *retbuf;
867 nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
868 TALLOC_FREE(subreq);
869 if (nread == -1) {
870 TALLOC_FREE(state->child);
871 tevent_req_error(req, err);
872 return;
875 state->child->busy = false;
877 retbuf = (struct rw_ret *)buf;
878 state->ret = retbuf->size;
879 state->err = retbuf->ret_errno;
880 tevent_req_done(req);
883 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
885 struct aio_fork_fsync_state *state = tevent_req_data(
886 req, struct aio_fork_fsync_state);
888 if (tevent_req_is_unix_error(req, err)) {
889 return -1;
891 if (state->ret == -1) {
892 *err = state->err;
894 return state->ret;
897 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
898 const char *user)
900 int ret;
901 struct aio_fork_config *config;
902 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
904 if (ret < 0) {
905 return ret;
908 config = talloc_zero(handle->conn, struct aio_fork_config);
909 if (!config) {
910 SMB_VFS_NEXT_DISCONNECT(handle);
911 DEBUG(0, ("talloc_zero() failed\n"));
912 return -1;
915 config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
916 "erratic_testing_mode", false);
918 SMB_VFS_HANDLE_SET_DATA(handle, config,
919 NULL, struct aio_fork_config,
920 return -1);
922 /*********************************************************************
923 * How many threads to initialize ?
924 * 100 per process seems insane as a default until you realize that
925 * (a) Threads terminate after 1 second when idle.
926 * (b) Throttling is done in SMB2 via the crediting algorithm.
927 * (c) SMB1 clients are limited to max_mux (50) outstanding
928 * requests and Windows clients don't use this anyway.
929 * Essentially we want this to be unlimited unless smb.conf
930 * says different.
931 *********************************************************************/
932 aio_pending_size = 100;
933 return 0;
936 static struct vfs_fn_pointers vfs_aio_fork_fns = {
937 .connect_fn = aio_fork_connect,
938 .pread_send_fn = aio_fork_pread_send,
939 .pread_recv_fn = aio_fork_pread_recv,
940 .pwrite_send_fn = aio_fork_pwrite_send,
941 .pwrite_recv_fn = aio_fork_pwrite_recv,
942 .fsync_send_fn = aio_fork_fsync_send,
943 .fsync_recv_fn = aio_fork_fsync_recv,
946 NTSTATUS vfs_aio_fork_init(void);
947 NTSTATUS vfs_aio_fork_init(void)
949 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
950 "aio_fork", &vfs_aio_fork_fns);