smbd: Use msghdr_prep_fds in vfs_aio_fork
[samba.git] / source3 / modules / vfs_aio_fork.c
1 /*
2  * Simulate the Posix AIO using mmap/fork
3  *
4  * Copyright (C) Volker Lendecke 2008
5  * Copyright (C) Jeremy Allison 2010
6  *
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.
11  *
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.
16  *
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.
20  */
21
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"
32
33 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
34 # error Can not pass file descriptors
35 #endif
36
37 #undef recvmsg
38
39 #ifndef MAP_FILE
40 #define MAP_FILE 0
41 #endif
42
43 struct aio_fork_config {
44         bool erratic_testing_mode;
45 };
46
47 struct mmap_area {
48         size_t size;
49         volatile void *ptr;
50 };
51
52 static int mmap_area_destructor(struct mmap_area *area)
53 {
54         munmap((void *)area->ptr, area->size);
55         return 0;
56 }
57
58 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
59 {
60         struct mmap_area *result;
61         int fd;
62
63         result = talloc(mem_ctx, struct mmap_area);
64         if (result == NULL) {
65                 DEBUG(0, ("talloc failed\n"));
66                 goto fail;
67         }
68
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;
74         }
75
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;
82         }
83
84         result->size = size;
85         talloc_set_destructor(result, mmap_area_destructor);
86
87         return result;
88
89 fail:
90         TALLOC_FREE(result);
91         return NULL;
92 }
93
94 enum cmd_type {
95         READ_CMD,
96         WRITE_CMD,
97         FSYNC_CMD
98 };
99
100 static const char *cmd_type_str(enum cmd_type cmd)
101 {
102         const char *result;
103
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;
117         }
118         return result;
119 }
120
121 struct rw_cmd {
122         size_t n;
123         off_t offset;
124         enum cmd_type cmd;
125         bool erratic_testing_mode;
126 };
127
128 struct rw_ret {
129         ssize_t size;
130         int ret_errno;
131 };
132
133 struct aio_child_list;
134
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;
143 };
144
145 struct aio_child_list {
146         struct aio_child *children;
147         struct tevent_timer *cleanup_event;
148 };
149
150 static void free_aio_children(void **p)
151 {
152         TALLOC_FREE(*p);
153 }
154
155 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
156 {
157         struct msghdr msg;
158         struct iovec iov[1];
159         ssize_t n;
160 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
161         int newfd;
162
163         ZERO_STRUCT(msg);
164         msg.msg_accrights = (caddr_t) &newfd;
165         msg.msg_accrightslen = sizeof(int);
166 #else
167
168         union {
169           struct cmsghdr        cm;
170           char                          control[CMSG_SPACE(sizeof(int))];
171         } control_un;
172         struct cmsghdr  *cmptr;
173
174         ZERO_STRUCT(msg);
175         ZERO_STRUCT(control_un);
176
177         msg.msg_control = control_un.control;
178         msg.msg_controllen = sizeof(control_un.control);
179 #endif
180
181         msg.msg_name = NULL;
182         msg.msg_namelen = 0;
183
184         iov[0].iov_base = (void *)ptr;
185         iov[0].iov_len = nbytes;
186         msg.msg_iov = iov;
187         msg.msg_iovlen = 1;
188
189         if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
190                 return(n);
191         }
192
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;
200                 }
201                 if (cmptr->cmsg_type != SCM_RIGHTS) {
202                         DEBUG(10, ("control type != SCM_RIGHTS"));
203                         errno = EINVAL;
204                         return -1;
205                 }
206                 memcpy(recvfd, CMSG_DATA(cmptr), sizeof(*recvfd));
207         } else {
208                 *recvfd = -1;           /* descriptor was not passed */
209         }
210 #else
211         if (msg.msg_accrightslen == sizeof(int)) {
212                 *recvfd = newfd;
213         }
214         else {
215                 *recvfd = -1;           /* descriptor was not passed */
216         }
217 #endif
218
219         return(n);
220 }
221
222 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
223 {
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;
228
229         msghdr_prep_fds(&msg, buf, bufsize, &sendfd, 1);
230         msg.msg_name = NULL;
231         msg.msg_namelen = 0;
232
233         iov.iov_base = (void *)ptr;
234         iov.iov_len = nbytes;
235         msg.msg_iov = &iov;
236         msg.msg_iovlen = 1;
237
238         return (sendmsg(fd, &msg, 0));
239 }
240
241 static void aio_child_cleanup(struct tevent_context *event_ctx,
242                               struct tevent_timer *te,
243                               struct timeval now,
244                               void *private_data)
245 {
246         struct aio_child_list *list = talloc_get_type_abort(
247                 private_data, struct aio_child_list);
248         struct aio_child *child, *next;
249
250         TALLOC_FREE(list->cleanup_event);
251
252         for (child = list->children; child != NULL; child = next) {
253                 next = child->next;
254
255                 if (child->busy) {
256                         DEBUG(10, ("child %d currently active\n",
257                                    (int)child->pid));
258                         continue;
259                 }
260
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;
266                 }
267
268                 DEBUG(10, ("Child %d idle for more than 30 seconds, "
269                            "deleting\n", (int)child->pid));
270
271                 TALLOC_FREE(child);
272                 child = next;
273         }
274
275         if (list->children != NULL) {
276                 /*
277                  * Re-schedule the next cleanup round
278                  */
279                 list->cleanup_event = tevent_add_timer(server_event_context(), list,
280                                                       timeval_add(&now, 30, 0),
281                                                       aio_child_cleanup, list);
282
283         }
284 }
285
286 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
287 {
288         struct aio_child_list *data = NULL;
289
290         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
291                 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
292                                         return NULL);
293         }
294
295         if (data == NULL) {
296                 data = talloc_zero(NULL, struct aio_child_list);
297                 if (data == NULL) {
298                         return NULL;
299                 }
300         }
301
302         /*
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.
306          */
307
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;
315                 }
316         }
317
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);
321         }
322
323         return data;
324 }
325
326 static void aio_child_loop(int sockfd, struct mmap_area *map)
327 {
328         while (true) {
329                 int fd = -1;
330                 ssize_t ret;
331                 struct rw_cmd cmd_struct;
332                 struct rw_ret ret_struct;
333
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);
339                 }
340
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));
344
345                 if (cmd_struct.erratic_testing_mode) {
346                         /*
347                          * For developer testing, we want erratic behaviour for
348                          * async I/O times
349                          */
350                         uint8_t randval;
351                         unsigned msecs;
352                         /*
353                          * use generate_random_buffer, we just forked from a
354                          * common parent state
355                          */
356                         generate_random_buffer(&randval, sizeof(randval));
357                         msecs = randval + 20;
358                         DEBUG(10, ("delaying for %u msecs\n", msecs));
359                         smb_msleep(msecs);
360                 }
361
362                 ZERO_STRUCT(ret_struct);
363
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;
387                 }
388
389                 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
390                            (int)ret_struct.size));
391
392                 if (ret_struct.size == -1) {
393                         ret_struct.ret_errno = errno;
394                 }
395
396                 /*
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.
401                  */
402                 close(fd);
403
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);
410                 }
411         }
412 }
413
414 static int aio_child_destructor(struct aio_child *child)
415 {
416         char c=0;
417
418         SMB_ASSERT(!child->busy);
419
420         DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
421                         child->pid, child->sockfd));
422
423         /*
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
426          */
427         write(child->sockfd, &c, sizeof(c));
428         close(child->sockfd);
429         DLIST_REMOVE(child->list->children, child);
430         return 0;
431 }
432
433 /*
434  * We have to close all fd's in open files, we might incorrectly hold a system
435  * level share mode on a file.
436  */
437
438 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
439                                          void *private_data)
440 {
441         if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
442                 close(fsp->fh->fd);
443                 fsp->fh->fd = -1;
444         }
445         return NULL;
446 }
447
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)
452 {
453         struct aio_child *result;
454         int fdpair[2];
455         int ret;
456
457         fdpair[0] = fdpair[1] = -1;
458
459         result = talloc_zero(children, struct aio_child);
460         if (result == NULL) {
461                 return ENOMEM;
462         }
463
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;
468         }
469
470         DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
471
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;
477         }
478
479         result->pid = fork();
480         if (result->pid == -1) {
481                 ret = errno;
482                 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
483                 goto fail;
484         }
485
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);
491         }
492
493         DEBUG(10, ("Child %d created with sockfd %d\n",
494                         result->pid, fdpair[0]));
495
496         result->sockfd = fdpair[0];
497         close(fdpair[1]);
498
499         result->list = children;
500         DLIST_ADD(children->children, result);
501
502         talloc_set_destructor(result, aio_child_destructor);
503
504         *presult = result;
505
506         return 0;
507
508  fail:
509         if (fdpair[0] != -1) close(fdpair[0]);
510         if (fdpair[1] != -1) close(fdpair[1]);
511         TALLOC_FREE(result);
512
513         return ret;
514 }
515
516 static int get_idle_child(struct vfs_handle_struct *handle,
517                           struct aio_child **pchild)
518 {
519         struct aio_child_list *children;
520         struct aio_child *child;
521
522         children = init_aio_children(handle);
523         if (children == NULL) {
524                 return ENOMEM;
525         }
526
527         for (child = children->children; child != NULL; child = child->next) {
528                 if (!child->busy) {
529                         break;
530                 }
531         }
532
533         if (child == NULL) {
534                 int ret;
535
536                 DEBUG(10, ("no idle child found, creating new one\n"));
537
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;
544                 }
545         }
546
547         child->dont_delete = true;
548         child->busy = true;
549
550         *pchild = child;
551         return 0;
552 }
553
554 struct aio_fork_pread_state {
555         struct aio_child *child;
556         ssize_t ret;
557         int err;
558 };
559
560 static void aio_fork_pread_done(struct tevent_req *subreq);
561
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)
568 {
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;
575
576         SMB_VFS_HANDLE_GET_DATA(handle, config,
577                                 struct aio_fork_config,
578                                 return NULL);
579
580         req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
581         if (req == NULL) {
582                 return NULL;
583         }
584
585         if (n > 128*1024) {
586                 /* TODO: support variable buffers */
587                 tevent_req_error(req, EINVAL);
588                 return tevent_req_post(req, ev);
589         }
590
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);
595         }
596
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;
602
603         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
604                    (int)state->child->pid));
605
606         /*
607          * Not making this async. We're writing into an empty unix
608          * domain socket. This should never block.
609          */
610         written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
611                            fsp->fh->fd);
612         if (written == -1) {
613                 err = errno;
614
615                 TALLOC_FREE(state->child);
616
617                 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
618                 tevent_req_error(req, err);
619                 return tevent_req_post(req, ev);
620         }
621
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);
627         }
628         tevent_req_set_callback(subreq, aio_fork_pread_done, req);
629         return req;
630 }
631
632 static void aio_fork_pread_done(struct tevent_req *subreq)
633 {
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;
642
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;
649         }
650
651         state->child->busy = false;
652
653         retbuf = (struct rw_ret *)buf;
654         state->ret = retbuf->size;
655         state->err = retbuf->ret_errno;
656         tevent_req_done(req);
657 }
658
659 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
660 {
661         struct aio_fork_pread_state *state = tevent_req_data(
662                 req, struct aio_fork_pread_state);
663
664         if (tevent_req_is_unix_error(req, err)) {
665                 return -1;
666         }
667         if (state->ret == -1) {
668                 *err = state->err;
669         }
670         return state->ret;
671 }
672
673 struct aio_fork_pwrite_state {
674         struct aio_child *child;
675         ssize_t ret;
676         int err;
677 };
678
679 static void aio_fork_pwrite_done(struct tevent_req *subreq);
680
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)
685 {
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);
695
696         req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
697         if (req == NULL) {
698                 return NULL;
699         }
700
701         if (n > 128*1024) {
702                 /* TODO: support variable buffers */
703                 tevent_req_error(req, EINVAL);
704                 return tevent_req_post(req, ev);
705         }
706
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);
711         }
712
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;
718
719         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
720                    (int)state->child->pid));
721
722         /*
723          * Not making this async. We're writing into an empty unix
724          * domain socket. This should never block.
725          */
726         written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
727                            fsp->fh->fd);
728         if (written == -1) {
729                 err = errno;
730
731                 TALLOC_FREE(state->child);
732
733                 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
734                 tevent_req_error(req, err);
735                 return tevent_req_post(req, ev);
736         }
737
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);
743         }
744         tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
745         return req;
746 }
747
748 static void aio_fork_pwrite_done(struct tevent_req *subreq)
749 {
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;
758
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;
765         }
766
767         state->child->busy = false;
768
769         retbuf = (struct rw_ret *)buf;
770         state->ret = retbuf->size;
771         state->err = retbuf->ret_errno;
772         tevent_req_done(req);
773 }
774
775 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
776 {
777         struct aio_fork_pwrite_state *state = tevent_req_data(
778                 req, struct aio_fork_pwrite_state);
779
780         if (tevent_req_is_unix_error(req, err)) {
781                 return -1;
782         }
783         if (state->ret == -1) {
784                 *err = state->err;
785         }
786         return state->ret;
787 }
788
789 struct aio_fork_fsync_state {
790         struct aio_child *child;
791         ssize_t ret;
792         int err;
793 };
794
795 static void aio_fork_fsync_done(struct tevent_req *subreq);
796
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)
800 {
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;
807
808         SMB_VFS_HANDLE_GET_DATA(handle, config,
809                                 struct aio_fork_config,
810                                 return NULL);
811
812         req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
813         if (req == NULL) {
814                 return NULL;
815         }
816
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);
821         }
822
823         ZERO_STRUCT(cmd);
824         cmd.cmd = FSYNC_CMD;
825         cmd.erratic_testing_mode = config->erratic_testing_mode;
826
827         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
828                    (int)state->child->pid));
829
830         /*
831          * Not making this async. We're writing into an empty unix
832          * domain socket. This should never block.
833          */
834         written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
835                            fsp->fh->fd);
836         if (written == -1) {
837                 err = errno;
838
839                 TALLOC_FREE(state->child);
840
841                 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
842                 tevent_req_error(req, err);
843                 return tevent_req_post(req, ev);
844         }
845
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);
851         }
852         tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
853         return req;
854 }
855
856 static void aio_fork_fsync_done(struct tevent_req *subreq)
857 {
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;
866
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;
873         }
874
875         state->child->busy = false;
876
877         retbuf = (struct rw_ret *)buf;
878         state->ret = retbuf->size;
879         state->err = retbuf->ret_errno;
880         tevent_req_done(req);
881 }
882
883 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
884 {
885         struct aio_fork_fsync_state *state = tevent_req_data(
886                 req, struct aio_fork_fsync_state);
887
888         if (tevent_req_is_unix_error(req, err)) {
889                 return -1;
890         }
891         if (state->ret == -1) {
892                 *err = state->err;
893         }
894         return state->ret;
895 }
896
897 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
898                             const char *user)
899 {
900         int ret;
901         struct aio_fork_config *config;
902         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
903
904         if (ret < 0) {
905                 return ret;
906         }
907
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;
913         }
914
915         config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
916                                                     "erratic_testing_mode", false);
917         
918         SMB_VFS_HANDLE_SET_DATA(handle, config,
919                                 NULL, struct aio_fork_config,
920                                 return -1);
921
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;
934 }
935
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,
944 };
945
946 NTSTATUS vfs_aio_fork_init(void);
947 NTSTATUS vfs_aio_fork_init(void)
948 {
949         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
950                                 "aio_fork", &vfs_aio_fork_fns);
951 }