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