smbd: Use msghdr.[ch] 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         size_t bufsize = msghdr_prep_recv_fds(NULL, NULL, 0, 1);
161         uint8_t buf[bufsize];
162
163         msghdr_prep_recv_fds(&msg, buf, bufsize, 1);
164
165         msg.msg_name = NULL;
166         msg.msg_namelen = 0;
167
168         iov[0].iov_base = (void *)ptr;
169         iov[0].iov_len = nbytes;
170         msg.msg_iov = iov;
171         msg.msg_iovlen = 1;
172
173         if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
174                 return(n);
175         }
176
177         {
178                 size_t num_fds = msghdr_extract_fds(&msg, NULL, 0);
179                 int fds[num_fds];
180
181                 msghdr_extract_fds(&msg, fds, num_fds);
182
183                 if (num_fds != 1) {
184                         size_t i;
185
186                         for (i=0; i<num_fds; i++) {
187                                 close(fds[i]);
188                         }
189
190                         *recvfd = -1;
191                         return n;
192                 }
193
194                 *recvfd = fds[0];
195         }
196
197         return(n);
198 }
199
200 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
201 {
202         struct msghdr msg;
203         size_t bufsize = msghdr_prep_fds(NULL, NULL, 0, &sendfd, 1);
204         uint8_t buf[bufsize];
205         struct iovec iov;
206
207         msghdr_prep_fds(&msg, buf, bufsize, &sendfd, 1);
208         msg.msg_name = NULL;
209         msg.msg_namelen = 0;
210
211         iov.iov_base = (void *)ptr;
212         iov.iov_len = nbytes;
213         msg.msg_iov = &iov;
214         msg.msg_iovlen = 1;
215
216         return (sendmsg(fd, &msg, 0));
217 }
218
219 static void aio_child_cleanup(struct tevent_context *event_ctx,
220                               struct tevent_timer *te,
221                               struct timeval now,
222                               void *private_data)
223 {
224         struct aio_child_list *list = talloc_get_type_abort(
225                 private_data, struct aio_child_list);
226         struct aio_child *child, *next;
227
228         TALLOC_FREE(list->cleanup_event);
229
230         for (child = list->children; child != NULL; child = next) {
231                 next = child->next;
232
233                 if (child->busy) {
234                         DEBUG(10, ("child %d currently active\n",
235                                    (int)child->pid));
236                         continue;
237                 }
238
239                 if (child->dont_delete) {
240                         DEBUG(10, ("Child %d was active since last cleanup\n",
241                                    (int)child->pid));
242                         child->dont_delete = false;
243                         continue;
244                 }
245
246                 DEBUG(10, ("Child %d idle for more than 30 seconds, "
247                            "deleting\n", (int)child->pid));
248
249                 TALLOC_FREE(child);
250                 child = next;
251         }
252
253         if (list->children != NULL) {
254                 /*
255                  * Re-schedule the next cleanup round
256                  */
257                 list->cleanup_event = tevent_add_timer(server_event_context(), list,
258                                                       timeval_add(&now, 30, 0),
259                                                       aio_child_cleanup, list);
260
261         }
262 }
263
264 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
265 {
266         struct aio_child_list *data = NULL;
267
268         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
269                 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
270                                         return NULL);
271         }
272
273         if (data == NULL) {
274                 data = talloc_zero(NULL, struct aio_child_list);
275                 if (data == NULL) {
276                         return NULL;
277                 }
278         }
279
280         /*
281          * Regardless of whether the child_list had been around or not, make
282          * sure that we have a cleanup timed event. This timed event will
283          * delete itself when it finds that no children are around anymore.
284          */
285
286         if (data->cleanup_event == NULL) {
287                 data->cleanup_event = tevent_add_timer(server_event_context(), data,
288                                                       timeval_current_ofs(30, 0),
289                                                       aio_child_cleanup, data);
290                 if (data->cleanup_event == NULL) {
291                         TALLOC_FREE(data);
292                         return NULL;
293                 }
294         }
295
296         if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
297                 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
298                                         struct aio_child_list, return False);
299         }
300
301         return data;
302 }
303
304 static void aio_child_loop(int sockfd, struct mmap_area *map)
305 {
306         while (true) {
307                 int fd = -1;
308                 ssize_t ret;
309                 struct rw_cmd cmd_struct;
310                 struct rw_ret ret_struct;
311
312                 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
313                 if (ret != sizeof(cmd_struct)) {
314                         DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
315                                    strerror(errno)));
316                         exit(1);
317                 }
318
319                 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
320                            cmd_type_str(cmd_struct.cmd),
321                            (int)cmd_struct.n, (int)cmd_struct.offset, fd));
322
323                 if (cmd_struct.erratic_testing_mode) {
324                         /*
325                          * For developer testing, we want erratic behaviour for
326                          * async I/O times
327                          */
328                         uint8_t randval;
329                         unsigned msecs;
330                         /*
331                          * use generate_random_buffer, we just forked from a
332                          * common parent state
333                          */
334                         generate_random_buffer(&randval, sizeof(randval));
335                         msecs = randval + 20;
336                         DEBUG(10, ("delaying for %u msecs\n", msecs));
337                         smb_msleep(msecs);
338                 }
339
340                 ZERO_STRUCT(ret_struct);
341
342                 switch (cmd_struct.cmd) {
343                 case READ_CMD:
344                         ret_struct.size = sys_pread(
345                                 fd, (void *)map->ptr, cmd_struct.n,
346                                 cmd_struct.offset);
347 #if 0
348 /* This breaks "make test" when run with aio_fork module. */
349 #ifdef DEVELOPER
350                         ret_struct.size = MAX(1, ret_struct.size * 0.9);
351 #endif
352 #endif
353                         break;
354                 case WRITE_CMD:
355                         ret_struct.size = sys_pwrite(
356                                 fd, (void *)map->ptr, cmd_struct.n,
357                                 cmd_struct.offset);
358                         break;
359                 case FSYNC_CMD:
360                         ret_struct.size = fsync(fd);
361                         break;
362                 default:
363                         ret_struct.size = -1;
364                         errno = EINVAL;
365                 }
366
367                 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
368                            (int)ret_struct.size));
369
370                 if (ret_struct.size == -1) {
371                         ret_struct.ret_errno = errno;
372                 }
373
374                 /*
375                  * Close the fd before telling our parent we're done. The
376                  * parent might close and re-open the file very quickly, and
377                  * with system-level share modes (GPFS) we would get an
378                  * unjustified SHARING_VIOLATION.
379                  */
380                 close(fd);
381
382                 ret = write_data(sockfd, (char *)&ret_struct,
383                                  sizeof(ret_struct));
384                 if (ret != sizeof(ret_struct)) {
385                         DEBUG(10, ("could not write ret_struct: %s\n",
386                                    strerror(errno)));
387                         exit(2);
388                 }
389         }
390 }
391
392 static int aio_child_destructor(struct aio_child *child)
393 {
394         char c=0;
395
396         SMB_ASSERT(!child->busy);
397
398         DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
399                         child->pid, child->sockfd));
400
401         /*
402          * closing the sockfd makes the child not return from recvmsg() on RHEL
403          * 5.5 so instead force the child to exit by writing bad data to it
404          */
405         write(child->sockfd, &c, sizeof(c));
406         close(child->sockfd);
407         DLIST_REMOVE(child->list->children, child);
408         return 0;
409 }
410
411 /*
412  * We have to close all fd's in open files, we might incorrectly hold a system
413  * level share mode on a file.
414  */
415
416 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
417                                          void *private_data)
418 {
419         if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
420                 close(fsp->fh->fd);
421                 fsp->fh->fd = -1;
422         }
423         return NULL;
424 }
425
426 static int create_aio_child(struct smbd_server_connection *sconn,
427                             struct aio_child_list *children,
428                             size_t map_size,
429                             struct aio_child **presult)
430 {
431         struct aio_child *result;
432         int fdpair[2];
433         int ret;
434
435         fdpair[0] = fdpair[1] = -1;
436
437         result = talloc_zero(children, struct aio_child);
438         if (result == NULL) {
439                 return ENOMEM;
440         }
441
442         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
443                 ret = errno;
444                 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
445                 goto fail;
446         }
447
448         DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
449
450         result->map = mmap_area_init(result, map_size);
451         if (result->map == NULL) {
452                 ret = errno;
453                 DEBUG(0, ("Could not create mmap area\n"));
454                 goto fail;
455         }
456
457         result->pid = fork();
458         if (result->pid == -1) {
459                 ret = errno;
460                 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
461                 goto fail;
462         }
463
464         if (result->pid == 0) {
465                 close(fdpair[0]);
466                 result->sockfd = fdpair[1];
467                 files_forall(sconn, close_fsp_fd, NULL);
468                 aio_child_loop(result->sockfd, result->map);
469         }
470
471         DEBUG(10, ("Child %d created with sockfd %d\n",
472                         result->pid, fdpair[0]));
473
474         result->sockfd = fdpair[0];
475         close(fdpair[1]);
476
477         result->list = children;
478         DLIST_ADD(children->children, result);
479
480         talloc_set_destructor(result, aio_child_destructor);
481
482         *presult = result;
483
484         return 0;
485
486  fail:
487         if (fdpair[0] != -1) close(fdpair[0]);
488         if (fdpair[1] != -1) close(fdpair[1]);
489         TALLOC_FREE(result);
490
491         return ret;
492 }
493
494 static int get_idle_child(struct vfs_handle_struct *handle,
495                           struct aio_child **pchild)
496 {
497         struct aio_child_list *children;
498         struct aio_child *child;
499
500         children = init_aio_children(handle);
501         if (children == NULL) {
502                 return ENOMEM;
503         }
504
505         for (child = children->children; child != NULL; child = child->next) {
506                 if (!child->busy) {
507                         break;
508                 }
509         }
510
511         if (child == NULL) {
512                 int ret;
513
514                 DEBUG(10, ("no idle child found, creating new one\n"));
515
516                 ret = create_aio_child(handle->conn->sconn, children,
517                                           128*1024, &child);
518                 if (ret != 0) {
519                         DEBUG(10, ("create_aio_child failed: %s\n",
520                                    strerror(errno)));
521                         return ret;
522                 }
523         }
524
525         child->dont_delete = true;
526         child->busy = true;
527
528         *pchild = child;
529         return 0;
530 }
531
532 struct aio_fork_pread_state {
533         struct aio_child *child;
534         ssize_t ret;
535         int err;
536 };
537
538 static void aio_fork_pread_done(struct tevent_req *subreq);
539
540 static struct tevent_req *aio_fork_pread_send(struct vfs_handle_struct *handle,
541                                               TALLOC_CTX *mem_ctx,
542                                               struct tevent_context *ev,
543                                               struct files_struct *fsp,
544                                               void *data,
545                                               size_t n, off_t offset)
546 {
547         struct tevent_req *req, *subreq;
548         struct aio_fork_pread_state *state;
549         struct rw_cmd cmd;
550         ssize_t written;
551         int err;
552         struct aio_fork_config *config;
553
554         SMB_VFS_HANDLE_GET_DATA(handle, config,
555                                 struct aio_fork_config,
556                                 return NULL);
557
558         req = tevent_req_create(mem_ctx, &state, struct aio_fork_pread_state);
559         if (req == NULL) {
560                 return NULL;
561         }
562
563         if (n > 128*1024) {
564                 /* TODO: support variable buffers */
565                 tevent_req_error(req, EINVAL);
566                 return tevent_req_post(req, ev);
567         }
568
569         err = get_idle_child(handle, &state->child);
570         if (err != 0) {
571                 tevent_req_error(req, err);
572                 return tevent_req_post(req, ev);
573         }
574
575         ZERO_STRUCT(cmd);
576         cmd.n = n;
577         cmd.offset = offset;
578         cmd.cmd = READ_CMD;
579         cmd.erratic_testing_mode = config->erratic_testing_mode;
580
581         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
582                    (int)state->child->pid));
583
584         /*
585          * Not making this async. We're writing into an empty unix
586          * domain socket. This should never block.
587          */
588         written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
589                            fsp->fh->fd);
590         if (written == -1) {
591                 err = errno;
592
593                 TALLOC_FREE(state->child);
594
595                 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
596                 tevent_req_error(req, err);
597                 return tevent_req_post(req, ev);
598         }
599
600         subreq = read_packet_send(state, ev, state->child->sockfd,
601                                   sizeof(struct rw_ret), NULL, NULL);
602         if (tevent_req_nomem(subreq, req)) {
603                 TALLOC_FREE(state->child); /* we sent sth down */
604                 return tevent_req_post(req, ev);
605         }
606         tevent_req_set_callback(subreq, aio_fork_pread_done, req);
607         return req;
608 }
609
610 static void aio_fork_pread_done(struct tevent_req *subreq)
611 {
612         struct tevent_req *req = tevent_req_callback_data(
613                 subreq, struct tevent_req);
614         struct aio_fork_pread_state *state = tevent_req_data(
615                 req, struct aio_fork_pread_state);
616         ssize_t nread;
617         uint8_t *buf;
618         int err;
619         struct rw_ret *retbuf;
620
621         nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
622         TALLOC_FREE(subreq);
623         if (nread == -1) {
624                 TALLOC_FREE(state->child);
625                 tevent_req_error(req, err);
626                 return;
627         }
628
629         state->child->busy = false;
630
631         retbuf = (struct rw_ret *)buf;
632         state->ret = retbuf->size;
633         state->err = retbuf->ret_errno;
634         tevent_req_done(req);
635 }
636
637 static ssize_t aio_fork_pread_recv(struct tevent_req *req, int *err)
638 {
639         struct aio_fork_pread_state *state = tevent_req_data(
640                 req, struct aio_fork_pread_state);
641
642         if (tevent_req_is_unix_error(req, err)) {
643                 return -1;
644         }
645         if (state->ret == -1) {
646                 *err = state->err;
647         }
648         return state->ret;
649 }
650
651 struct aio_fork_pwrite_state {
652         struct aio_child *child;
653         ssize_t ret;
654         int err;
655 };
656
657 static void aio_fork_pwrite_done(struct tevent_req *subreq);
658
659 static struct tevent_req *aio_fork_pwrite_send(
660         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
661         struct tevent_context *ev, struct files_struct *fsp,
662         const void *data, size_t n, off_t offset)
663 {
664         struct tevent_req *req, *subreq;
665         struct aio_fork_pwrite_state *state;
666         struct rw_cmd cmd;
667         ssize_t written;
668         int err;
669         struct aio_fork_config *config;
670         SMB_VFS_HANDLE_GET_DATA(handle, config,
671                                 struct aio_fork_config,
672                                 return NULL);
673
674         req = tevent_req_create(mem_ctx, &state, struct aio_fork_pwrite_state);
675         if (req == NULL) {
676                 return NULL;
677         }
678
679         if (n > 128*1024) {
680                 /* TODO: support variable buffers */
681                 tevent_req_error(req, EINVAL);
682                 return tevent_req_post(req, ev);
683         }
684
685         err = get_idle_child(handle, &state->child);
686         if (err != 0) {
687                 tevent_req_error(req, err);
688                 return tevent_req_post(req, ev);
689         }
690
691         ZERO_STRUCT(cmd);
692         cmd.n = n;
693         cmd.offset = offset;
694         cmd.cmd = WRITE_CMD;
695         cmd.erratic_testing_mode = config->erratic_testing_mode;
696
697         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
698                    (int)state->child->pid));
699
700         /*
701          * Not making this async. We're writing into an empty unix
702          * domain socket. This should never block.
703          */
704         written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
705                            fsp->fh->fd);
706         if (written == -1) {
707                 err = errno;
708
709                 TALLOC_FREE(state->child);
710
711                 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
712                 tevent_req_error(req, err);
713                 return tevent_req_post(req, ev);
714         }
715
716         subreq = read_packet_send(state, ev, state->child->sockfd,
717                                   sizeof(struct rw_ret), NULL, NULL);
718         if (tevent_req_nomem(subreq, req)) {
719                 TALLOC_FREE(state->child); /* we sent sth down */
720                 return tevent_req_post(req, ev);
721         }
722         tevent_req_set_callback(subreq, aio_fork_pwrite_done, req);
723         return req;
724 }
725
726 static void aio_fork_pwrite_done(struct tevent_req *subreq)
727 {
728         struct tevent_req *req = tevent_req_callback_data(
729                 subreq, struct tevent_req);
730         struct aio_fork_pwrite_state *state = tevent_req_data(
731                 req, struct aio_fork_pwrite_state);
732         ssize_t nread;
733         uint8_t *buf;
734         int err;
735         struct rw_ret *retbuf;
736
737         nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
738         TALLOC_FREE(subreq);
739         if (nread == -1) {
740                 TALLOC_FREE(state->child);
741                 tevent_req_error(req, err);
742                 return;
743         }
744
745         state->child->busy = false;
746
747         retbuf = (struct rw_ret *)buf;
748         state->ret = retbuf->size;
749         state->err = retbuf->ret_errno;
750         tevent_req_done(req);
751 }
752
753 static ssize_t aio_fork_pwrite_recv(struct tevent_req *req, int *err)
754 {
755         struct aio_fork_pwrite_state *state = tevent_req_data(
756                 req, struct aio_fork_pwrite_state);
757
758         if (tevent_req_is_unix_error(req, err)) {
759                 return -1;
760         }
761         if (state->ret == -1) {
762                 *err = state->err;
763         }
764         return state->ret;
765 }
766
767 struct aio_fork_fsync_state {
768         struct aio_child *child;
769         ssize_t ret;
770         int err;
771 };
772
773 static void aio_fork_fsync_done(struct tevent_req *subreq);
774
775 static struct tevent_req *aio_fork_fsync_send(
776         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
777         struct tevent_context *ev, struct files_struct *fsp)
778 {
779         struct tevent_req *req, *subreq;
780         struct aio_fork_fsync_state *state;
781         struct rw_cmd cmd;
782         ssize_t written;
783         int err;
784         struct aio_fork_config *config;
785
786         SMB_VFS_HANDLE_GET_DATA(handle, config,
787                                 struct aio_fork_config,
788                                 return NULL);
789
790         req = tevent_req_create(mem_ctx, &state, struct aio_fork_fsync_state);
791         if (req == NULL) {
792                 return NULL;
793         }
794
795         err = get_idle_child(handle, &state->child);
796         if (err != 0) {
797                 tevent_req_error(req, err);
798                 return tevent_req_post(req, ev);
799         }
800
801         ZERO_STRUCT(cmd);
802         cmd.cmd = FSYNC_CMD;
803         cmd.erratic_testing_mode = config->erratic_testing_mode;
804
805         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
806                    (int)state->child->pid));
807
808         /*
809          * Not making this async. We're writing into an empty unix
810          * domain socket. This should never block.
811          */
812         written = write_fd(state->child->sockfd, &cmd, sizeof(cmd),
813                            fsp->fh->fd);
814         if (written == -1) {
815                 err = errno;
816
817                 TALLOC_FREE(state->child);
818
819                 DEBUG(10, ("write_fd failed: %s\n", strerror(err)));
820                 tevent_req_error(req, err);
821                 return tevent_req_post(req, ev);
822         }
823
824         subreq = read_packet_send(state, ev, state->child->sockfd,
825                                   sizeof(struct rw_ret), NULL, NULL);
826         if (tevent_req_nomem(subreq, req)) {
827                 TALLOC_FREE(state->child); /* we sent sth down */
828                 return tevent_req_post(req, ev);
829         }
830         tevent_req_set_callback(subreq, aio_fork_fsync_done, req);
831         return req;
832 }
833
834 static void aio_fork_fsync_done(struct tevent_req *subreq)
835 {
836         struct tevent_req *req = tevent_req_callback_data(
837                 subreq, struct tevent_req);
838         struct aio_fork_fsync_state *state = tevent_req_data(
839                 req, struct aio_fork_fsync_state);
840         ssize_t nread;
841         uint8_t *buf;
842         int err;
843         struct rw_ret *retbuf;
844
845         nread = read_packet_recv(subreq, talloc_tos(), &buf, &err);
846         TALLOC_FREE(subreq);
847         if (nread == -1) {
848                 TALLOC_FREE(state->child);
849                 tevent_req_error(req, err);
850                 return;
851         }
852
853         state->child->busy = false;
854
855         retbuf = (struct rw_ret *)buf;
856         state->ret = retbuf->size;
857         state->err = retbuf->ret_errno;
858         tevent_req_done(req);
859 }
860
861 static int aio_fork_fsync_recv(struct tevent_req *req, int *err)
862 {
863         struct aio_fork_fsync_state *state = tevent_req_data(
864                 req, struct aio_fork_fsync_state);
865
866         if (tevent_req_is_unix_error(req, err)) {
867                 return -1;
868         }
869         if (state->ret == -1) {
870                 *err = state->err;
871         }
872         return state->ret;
873 }
874
875 static int aio_fork_connect(vfs_handle_struct *handle, const char *service,
876                             const char *user)
877 {
878         int ret;
879         struct aio_fork_config *config;
880         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
881
882         if (ret < 0) {
883                 return ret;
884         }
885
886         config = talloc_zero(handle->conn, struct aio_fork_config);
887         if (!config) {
888                 SMB_VFS_NEXT_DISCONNECT(handle);
889                 DEBUG(0, ("talloc_zero() failed\n"));
890                 return -1;
891         }
892
893         config->erratic_testing_mode = lp_parm_bool(SNUM(handle->conn), "vfs_aio_fork",
894                                                     "erratic_testing_mode", false);
895         
896         SMB_VFS_HANDLE_SET_DATA(handle, config,
897                                 NULL, struct aio_fork_config,
898                                 return -1);
899
900         /*********************************************************************
901          * How many threads to initialize ?
902          * 100 per process seems insane as a default until you realize that
903          * (a) Threads terminate after 1 second when idle.
904          * (b) Throttling is done in SMB2 via the crediting algorithm.
905          * (c) SMB1 clients are limited to max_mux (50) outstanding
906          *     requests and Windows clients don't use this anyway.
907          * Essentially we want this to be unlimited unless smb.conf
908          * says different.
909          *********************************************************************/
910         aio_pending_size = 100;
911         return 0;
912 }
913
914 static struct vfs_fn_pointers vfs_aio_fork_fns = {
915         .connect_fn = aio_fork_connect,
916         .pread_send_fn = aio_fork_pread_send,
917         .pread_recv_fn = aio_fork_pread_recv,
918         .pwrite_send_fn = aio_fork_pwrite_send,
919         .pwrite_recv_fn = aio_fork_pwrite_recv,
920         .fsync_send_fn = aio_fork_fsync_send,
921         .fsync_recv_fn = aio_fork_fsync_recv,
922 };
923
924 NTSTATUS vfs_aio_fork_init(void);
925 NTSTATUS vfs_aio_fork_init(void)
926 {
927         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
928                                 "aio_fork", &vfs_aio_fork_fns);
929 }