90d06b10c166e2da2ebb4a446fd75f034a6a1de7
[idra/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
24 struct mmap_area {
25         size_t size;
26         volatile void *ptr;
27 };
28
29 static int mmap_area_destructor(struct mmap_area *area)
30 {
31         munmap((void *)area->ptr, area->size);
32         return 0;
33 }
34
35 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
36 {
37         struct mmap_area *result;
38         int fd;
39
40         result = talloc(mem_ctx, struct mmap_area);
41         if (result == NULL) {
42                 DEBUG(0, ("talloc failed\n"));
43                 goto fail;
44         }
45
46         fd = open("/dev/zero", O_RDWR);
47         if (fd == -1) {
48                 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
49                           strerror(errno)));
50                 goto fail;
51         }
52
53         result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
54                            MAP_SHARED|MAP_FILE, fd, 0);
55         if (result->ptr == MAP_FAILED) {
56                 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
57                 goto fail;
58         }
59
60         close(fd);
61
62         result->size = size;
63         talloc_set_destructor(result, mmap_area_destructor);
64
65         return result;
66
67 fail:
68         TALLOC_FREE(result);
69         return NULL;
70 }
71
72 struct rw_cmd {
73         size_t n;
74         SMB_OFF_T offset;
75         bool read_cmd;
76 };
77
78 struct rw_ret {
79         ssize_t size;
80         int ret_errno;
81 };
82
83 struct aio_child_list;
84
85 struct aio_child {
86         struct aio_child *prev, *next;
87         struct aio_child_list *list;
88         SMB_STRUCT_AIOCB *aiocb;
89         pid_t pid;
90         int sockfd;
91         struct fd_event *sock_event;
92         struct rw_ret retval;
93         struct mmap_area *map;  /* ==NULL means write request */
94         bool dont_delete;       /* Marked as in use since last cleanup */
95         bool cancelled;
96         bool read_cmd;
97 };
98
99 struct aio_child_list {
100         struct aio_child *children;
101         struct timed_event *cleanup_event;
102 };
103
104 static void free_aio_children(void **p)
105 {
106         TALLOC_FREE(*p);
107 }
108
109 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
110 {
111         struct msghdr msg;
112         struct iovec iov[1];
113         ssize_t n;
114 #ifndef HAVE_MSGHDR_MSG_CONTROL
115         int newfd;
116 #endif
117
118 #ifdef  HAVE_MSGHDR_MSG_CONTROL
119         union {
120           struct cmsghdr        cm;
121           char                          control[CMSG_SPACE(sizeof(int))];
122         } control_un;
123         struct cmsghdr  *cmptr;
124
125         msg.msg_control = control_un.control;
126         msg.msg_controllen = sizeof(control_un.control);
127 #else
128 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
129         msg.msg_accrights = (caddr_t) &newfd;
130         msg.msg_accrightslen = sizeof(int);
131 #else
132 #error Can not pass file descriptors
133 #endif
134 #endif
135
136         msg.msg_name = NULL;
137         msg.msg_namelen = 0;
138
139         iov[0].iov_base = (void *)ptr;
140         iov[0].iov_len = nbytes;
141         msg.msg_iov = iov;
142         msg.msg_iovlen = 1;
143
144         if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
145                 return(n);
146         }
147
148 #ifdef  HAVE_MSGHDR_MSG_CONTROL
149         if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
150             && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
151                 if (cmptr->cmsg_level != SOL_SOCKET) {
152                         DEBUG(10, ("control level != SOL_SOCKET"));
153                         errno = EINVAL;
154                         return -1;
155                 }
156                 if (cmptr->cmsg_type != SCM_RIGHTS) {
157                         DEBUG(10, ("control type != SCM_RIGHTS"));
158                         errno = EINVAL;
159                         return -1;
160                 }
161                 *recvfd = *((int *) CMSG_DATA(cmptr));
162         } else {
163                 *recvfd = -1;           /* descriptor was not passed */
164         }
165 #else
166         if (msg.msg_accrightslen == sizeof(int)) {
167                 *recvfd = newfd;
168         }
169         else {
170                 *recvfd = -1;           /* descriptor was not passed */
171         }
172 #endif
173
174         return(n);
175 }
176
177 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
178 {
179         struct msghdr   msg;
180         struct iovec    iov[1];
181
182 #ifdef  HAVE_MSGHDR_MSG_CONTROL
183         union {
184                 struct cmsghdr  cm;
185                 char control[CMSG_SPACE(sizeof(int))];
186         } control_un;
187         struct cmsghdr  *cmptr;
188
189         ZERO_STRUCT(msg);
190         ZERO_STRUCT(control_un);
191
192         msg.msg_control = control_un.control;
193         msg.msg_controllen = sizeof(control_un.control);
194
195         cmptr = CMSG_FIRSTHDR(&msg);
196         cmptr->cmsg_len = CMSG_LEN(sizeof(int));
197         cmptr->cmsg_level = SOL_SOCKET;
198         cmptr->cmsg_type = SCM_RIGHTS;
199         *((int *) CMSG_DATA(cmptr)) = sendfd;
200 #else
201         ZERO_STRUCT(msg);
202         msg.msg_accrights = (caddr_t) &sendfd;
203         msg.msg_accrightslen = sizeof(int);
204 #endif
205
206         msg.msg_name = NULL;
207         msg.msg_namelen = 0;
208
209         ZERO_STRUCT(iov);
210         iov[0].iov_base = (void *)ptr;
211         iov[0].iov_len = nbytes;
212         msg.msg_iov = iov;
213         msg.msg_iovlen = 1;
214
215         return (sendmsg(fd, &msg, 0));
216 }
217
218 static void aio_child_cleanup(struct event_context *event_ctx,
219                               struct timed_event *te,
220                               struct timeval now,
221                               void *private_data)
222 {
223         struct aio_child_list *list = talloc_get_type_abort(
224                 private_data, struct aio_child_list);
225         struct aio_child *child, *next;
226
227         TALLOC_FREE(list->cleanup_event);
228
229         for (child = list->children; child != NULL; child = next) {
230                 next = child->next;
231
232                 if (child->aiocb != NULL) {
233                         DEBUG(10, ("child %d currently active\n",
234                                    (int)child->pid));
235                         continue;
236                 }
237
238                 if (child->dont_delete) {
239                         DEBUG(10, ("Child %d was active since last cleanup\n",
240                                    (int)child->pid));
241                         child->dont_delete = false;
242                         continue;
243                 }
244
245                 DEBUG(10, ("Child %d idle for more than 30 seconds, "
246                            "deleting\n", (int)child->pid));
247
248                 TALLOC_FREE(child);
249         }
250
251         if (list->children != NULL) {
252                 /*
253                  * Re-schedule the next cleanup round
254                  */
255                 list->cleanup_event = event_add_timed(smbd_event_context(), list,
256                                                       timeval_add(&now, 30, 0),
257                                                       aio_child_cleanup, list);
258
259         }
260 }
261
262 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
263 {
264         struct aio_child_list *data = NULL;
265
266         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
267                 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
268                                         return NULL);
269         }
270
271         if (data == NULL) {
272                 data = TALLOC_ZERO_P(NULL, struct aio_child_list);
273                 if (data == NULL) {
274                         return NULL;
275                 }
276         }
277
278         /*
279          * Regardless of whether the child_list had been around or not, make
280          * sure that we have a cleanup timed event. This timed event will
281          * delete itself when it finds that no children are around anymore.
282          */
283
284         if (data->cleanup_event == NULL) {
285                 data->cleanup_event = event_add_timed(smbd_event_context(), data,
286                                                       timeval_current_ofs(30, 0),
287                                                       aio_child_cleanup, data);
288                 if (data->cleanup_event == NULL) {
289                         TALLOC_FREE(data);
290                         return NULL;
291                 }
292         }
293
294         if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
295                 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
296                                         struct aio_child_list, return False);
297         }
298
299         return data;
300 }
301
302 static void aio_child_loop(int sockfd, struct mmap_area *map)
303 {
304         while (true) {
305                 int fd = -1;
306                 ssize_t ret;
307                 struct rw_cmd cmd_struct;
308                 struct rw_ret ret_struct;
309
310                 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
311                 if (ret != sizeof(cmd_struct)) {
312                         DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
313                                    strerror(errno)));
314                         exit(1);
315                 }
316
317                 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
318                            cmd_struct.read_cmd ? "read" : "write",
319                            (int)cmd_struct.n, (int)cmd_struct.offset, fd));
320
321 #ifdef ENABLE_BUILD_FARM_HACKS
322                 {
323                         /*
324                          * In the build farm, we want erratic behaviour for
325                          * async I/O times
326                          */
327                         uint8_t randval;
328                         unsigned msecs;
329                         /*
330                          * use generate_random_buffer, we just forked from a
331                          * common parent state
332                          */
333                         generate_random_buffer(&randval, sizeof(randval));
334                         msecs = randval + 20;
335                         DEBUG(10, ("delaying for %u msecs\n", msecs));
336                         smb_msleep(msecs);
337                 }
338 #endif
339
340
341                 ZERO_STRUCT(ret_struct);
342
343                 if (cmd_struct.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 ENABLE_BUILD_FARM_HACKS
350                         ret_struct.size = MAX(1, ret_struct.size * 0.9);
351 #endif
352 #endif
353                 }
354                 else {
355                         ret_struct.size = sys_pwrite(
356                                 fd, (void *)map->ptr, cmd_struct.n,
357                                 cmd_struct.offset);
358                 }
359
360                 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
361                            (int)ret_struct.size));
362
363                 if (ret_struct.size == -1) {
364                         ret_struct.ret_errno = errno;
365                 }
366
367                 /*
368                  * Close the fd before telling our parent we're done. The
369                  * parent might close and re-open the file very quickly, and
370                  * with system-level share modes (GPFS) we would get an
371                  * unjustified SHARING_VIOLATION.
372                  */
373                 close(fd);
374
375                 ret = write_data(sockfd, (char *)&ret_struct,
376                                  sizeof(ret_struct));
377                 if (ret != sizeof(ret_struct)) {
378                         DEBUG(10, ("could not write ret_struct: %s\n",
379                                    strerror(errno)));
380                         exit(2);
381                 }
382         }
383 }
384
385 static void handle_aio_completion(struct event_context *event_ctx,
386                                   struct fd_event *event, uint16 flags,
387                                   void *p)
388 {
389         struct aio_extra *aio_ex = NULL;
390         struct aio_child *child = (struct aio_child *)p;
391
392         DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags));
393
394         if ((flags & EVENT_FD_READ) == 0) {
395                 return;
396         }
397
398         if (!NT_STATUS_IS_OK(read_data(child->sockfd,
399                                        (char *)&child->retval,
400                                        sizeof(child->retval)))) {
401                 DEBUG(0, ("aio child %d died\n", (int)child->pid));
402                 child->retval.size = -1;
403                 child->retval.ret_errno = EIO;
404         }
405
406         if (child->cancelled) {
407                 child->aiocb = NULL;
408                 child->cancelled = false;
409                 return;
410         }
411
412         if (child->read_cmd && (child->retval.size > 0)) {
413                 SMB_ASSERT(child->retval.size <= child->aiocb->aio_nbytes);
414                 memcpy((void *)child->aiocb->aio_buf, (void *)child->map->ptr,
415                        child->retval.size);
416         }
417
418         aio_ex = (struct aio_extra *)child->aiocb->aio_sigevent.sigev_value.sival_ptr;
419         smbd_aio_complete_aio_ex(aio_ex);
420 }
421
422 static int aio_child_destructor(struct aio_child *child)
423 {
424         SMB_ASSERT((child->aiocb == NULL) || child->cancelled);
425         close(child->sockfd);
426         DLIST_REMOVE(child->list->children, child);
427         return 0;
428 }
429
430 /*
431  * We have to close all fd's in open files, we might incorrectly hold a system
432  * level share mode on a file.
433  */
434
435 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
436                                          void *private_data)
437 {
438         if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
439                 close(fsp->fh->fd);
440                 fsp->fh->fd = -1;
441         }
442         return NULL;
443 }
444
445 static NTSTATUS create_aio_child(struct smbd_server_connection *sconn,
446                                  struct aio_child_list *children,
447                                  size_t map_size,
448                                  struct aio_child **presult)
449 {
450         struct aio_child *result;
451         int fdpair[2];
452         NTSTATUS status;
453
454         fdpair[0] = fdpair[1] = -1;
455
456         result = TALLOC_ZERO_P(children, struct aio_child);
457         NT_STATUS_HAVE_NO_MEMORY(result);
458
459         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
460                 status = map_nt_error_from_unix(errno);
461                 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
462                 goto fail;
463         }
464
465         DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
466
467         result->map = mmap_area_init(result, map_size);
468         if (result->map == NULL) {
469                 status = map_nt_error_from_unix(errno);
470                 DEBUG(0, ("Could not create mmap area\n"));
471                 goto fail;
472         }
473
474         result->pid = sys_fork();
475         if (result->pid == -1) {
476                 status = map_nt_error_from_unix(errno);
477                 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
478                 goto fail;
479         }
480
481         if (result->pid == 0) {
482                 close(fdpair[0]);
483                 result->sockfd = fdpair[1];
484                 files_forall(sconn, close_fsp_fd, NULL);
485                 aio_child_loop(result->sockfd, result->map);
486         }
487
488         DEBUG(10, ("Child %d created\n", result->pid));
489
490         result->sockfd = fdpair[0];
491         close(fdpair[1]);
492
493         result->sock_event = event_add_fd(smbd_event_context(), result,
494                                           result->sockfd, EVENT_FD_READ,
495                                           handle_aio_completion,
496                                           result);
497         if (result->sock_event == NULL) {
498                 status = NT_STATUS_NO_MEMORY;
499                 DEBUG(0, ("event_add_fd failed\n"));
500                 goto fail;
501         }
502
503         result->list = children;
504         DLIST_ADD(children->children, result);
505
506         talloc_set_destructor(result, aio_child_destructor);
507
508         *presult = result;
509
510         return NT_STATUS_OK;
511
512  fail:
513         if (fdpair[0] != -1) close(fdpair[0]);
514         if (fdpair[1] != -1) close(fdpair[1]);
515         TALLOC_FREE(result);
516
517         return status;
518 }
519
520 static NTSTATUS get_idle_child(struct vfs_handle_struct *handle,
521                                struct aio_child **pchild)
522 {
523         struct aio_child_list *children;
524         struct aio_child *child;
525         NTSTATUS status;
526
527         children = init_aio_children(handle);
528         if (children == NULL) {
529                 return NT_STATUS_NO_MEMORY;
530         }
531
532         for (child = children->children; child != NULL; child = child->next) {
533                 if (child->aiocb == NULL) {
534                         /* idle */
535                         break;
536                 }
537         }
538
539         if (child == NULL) {
540                 DEBUG(10, ("no idle child found, creating new one\n"));
541
542                 status = create_aio_child(handle->conn->sconn, children,
543                                           128*1024, &child);
544                 if (!NT_STATUS_IS_OK(status)) {
545                         DEBUG(10, ("create_aio_child failed: %s\n",
546                                    nt_errstr(status)));
547                         return status;
548                 }
549         }
550
551         child->dont_delete = true;
552
553         *pchild = child;
554         return NT_STATUS_OK;
555 }
556
557 static int aio_fork_read(struct vfs_handle_struct *handle,
558                          struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
559 {
560         struct aio_child *child;
561         struct rw_cmd cmd;
562         ssize_t ret;
563         NTSTATUS status;
564
565         if (aiocb->aio_nbytes > 128*1024) {
566                 /* TODO: support variable buffers */
567                 errno = EINVAL;
568                 return -1;
569         }
570
571         status = get_idle_child(handle, &child);
572         if (!NT_STATUS_IS_OK(status)) {
573                 DEBUG(10, ("Could not get an idle child\n"));
574                 return -1;
575         }
576
577         child->read_cmd = true;
578         child->aiocb = aiocb;
579         child->retval.ret_errno = EINPROGRESS;
580
581         ZERO_STRUCT(cmd);
582         cmd.n = aiocb->aio_nbytes;
583         cmd.offset = aiocb->aio_offset;
584         cmd.read_cmd = child->read_cmd;
585
586         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
587                    (int)child->pid));
588
589         ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
590         if (ret == -1) {
591                 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
592                 return -1;
593         }
594
595         return 0;
596 }
597
598 static int aio_fork_write(struct vfs_handle_struct *handle,
599                           struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
600 {
601         struct aio_child *child;
602         struct rw_cmd cmd;
603         ssize_t ret;
604         NTSTATUS status;
605
606         if (aiocb->aio_nbytes > 128*1024) {
607                 /* TODO: support variable buffers */
608                 errno = EINVAL;
609                 return -1;
610         }
611
612         status = get_idle_child(handle, &child);
613         if (!NT_STATUS_IS_OK(status)) {
614                 DEBUG(10, ("Could not get an idle child\n"));
615                 return -1;
616         }
617
618         child->read_cmd = false;
619         child->aiocb = aiocb;
620         child->retval.ret_errno = EINPROGRESS;
621
622         memcpy((void *)child->map->ptr, (void *)aiocb->aio_buf,
623                aiocb->aio_nbytes);
624
625         ZERO_STRUCT(cmd);
626         cmd.n = aiocb->aio_nbytes;
627         cmd.offset = aiocb->aio_offset;
628         cmd.read_cmd = child->read_cmd;
629
630         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
631                    (int)child->pid));
632
633         ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
634         if (ret == -1) {
635                 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
636                 return -1;
637         }
638
639         return 0;
640 }
641
642 static struct aio_child *aio_fork_find_child(struct vfs_handle_struct *handle,
643                                              SMB_STRUCT_AIOCB *aiocb)
644 {
645         struct aio_child_list *children;
646         struct aio_child *child;
647
648         children = init_aio_children(handle);
649         if (children == NULL) {
650                 return NULL;
651         }
652
653         for (child = children->children; child != NULL; child = child->next) {
654                 if (child->aiocb == aiocb) {
655                         return child;
656                 }
657         }
658
659         return NULL;
660 }
661
662 static ssize_t aio_fork_return_fn(struct vfs_handle_struct *handle,
663                                   struct files_struct *fsp,
664                                   SMB_STRUCT_AIOCB *aiocb)
665 {
666         struct aio_child *child = aio_fork_find_child(handle, aiocb);
667
668         if (child == NULL) {
669                 errno = EINVAL;
670                 DEBUG(0, ("returning EINVAL\n"));
671                 return -1;
672         }
673
674         child->aiocb = NULL;
675
676         if (child->retval.size == -1) {
677                 errno = child->retval.ret_errno;
678         }
679
680         return child->retval.size;
681 }
682
683 static int aio_fork_cancel(struct vfs_handle_struct *handle,
684                            struct files_struct *fsp,
685                            SMB_STRUCT_AIOCB *aiocb)
686 {
687         struct aio_child_list *children;
688         struct aio_child *child;
689
690         children = init_aio_children(handle);
691         if (children == NULL) {
692                 errno = EINVAL;
693                 return -1;
694         }
695
696         for (child = children->children; child != NULL; child = child->next) {
697                 if (child->aiocb == NULL) {
698                         continue;
699                 }
700                 if (child->aiocb->aio_fildes != fsp->fh->fd) {
701                         continue;
702                 }
703                 if ((aiocb != NULL) && (child->aiocb != aiocb)) {
704                         continue;
705                 }
706
707                 /*
708                  * We let the child do its job, but we discard the result when
709                  * it's finished.
710                  */
711
712                 child->cancelled = true;
713         }
714
715         return AIO_CANCELED;
716 }
717
718 static int aio_fork_error_fn(struct vfs_handle_struct *handle,
719                              struct files_struct *fsp,
720                              SMB_STRUCT_AIOCB *aiocb)
721 {
722         struct aio_child *child = aio_fork_find_child(handle, aiocb);
723
724         if (child == NULL) {
725                 errno = EINVAL;
726                 return -1;
727         }
728
729         return child->retval.ret_errno;
730 }
731
732 static void aio_fork_suspend_timed_out(struct tevent_context *event_ctx,
733                                         struct tevent_timer *te,
734                                         struct timeval now,
735                                         void *private_data)
736 {
737         bool *timed_out = (bool *)private_data;
738         /* Remove this timed event handler. */
739         TALLOC_FREE(te);
740         *timed_out = true;
741 }
742
743 static int aio_fork_suspend(struct vfs_handle_struct *handle,
744                         struct files_struct *fsp,
745                         const SMB_STRUCT_AIOCB * const aiocb_array[],
746                         int n,
747                         const struct timespec *timeout)
748 {
749         struct aio_child_list *children = NULL;
750         TALLOC_CTX *frame = talloc_stackframe();
751         struct event_context *ev = NULL;
752         int i;
753         int ret = -1;
754         bool timed_out = false;
755
756         children = init_aio_children(handle);
757         if (children == NULL) {
758                 errno = EINVAL;
759                 goto out;
760         }
761
762         /* This is a blocking call, and has to use a sub-event loop. */
763         ev = event_context_init(frame);
764         if (ev == NULL) {
765                 errno = ENOMEM;
766                 goto out;
767         }
768
769         if (timeout) {
770                 struct timeval tv = convert_timespec_to_timeval(*timeout);
771                 struct tevent_timer *te = tevent_add_timer(ev,
772                                                 frame,
773                                                 timeval_current_ofs(tv.tv_sec,
774                                                                     tv.tv_usec),
775                                                 aio_fork_suspend_timed_out,
776                                                 &timed_out);
777                 if (!te) {
778                         errno = ENOMEM;
779                         goto out;
780                 }
781         }
782
783         for (i = 0; i < n; i++) {
784                 struct aio_child *child = NULL;
785                 const SMB_STRUCT_AIOCB *aiocb = aiocb_array[i];
786
787                 if (!aiocb) {
788                         continue;
789                 }
790
791                 /*
792                  * We're going to cheat here. We know that smbd/aio.c
793                  * only calls this when it's waiting for every single
794                  * outstanding call to finish on a close, so just wait
795                  * individually for each IO to complete. We don't care
796                  * what order they finish - only that they all do. JRA.
797                  */
798
799                 for (child = children->children; child != NULL; child = child->next) {
800                         if (child->aiocb == NULL) {
801                                 continue;
802                         }
803                         if (child->aiocb->aio_fildes != fsp->fh->fd) {
804                                 continue;
805                         }
806                         if (child->aiocb != aiocb) {
807                                 continue;
808                         }
809
810                         if (child->aiocb->aio_sigevent.sigev_value.sival_ptr == NULL) {
811                                 continue;
812                         }
813
814                         /* We're never using this event on the
815                          * main event context again... */
816                         TALLOC_FREE(child->sock_event);
817
818                         child->sock_event = event_add_fd(ev,
819                                                 child,
820                                                 child->sockfd,
821                                                 EVENT_FD_READ,
822                                                 handle_aio_completion,
823                                                 child);
824
825                         while (1) {
826                                 if (tevent_loop_once(ev) == -1) {
827                                         goto out;
828                                 }
829
830                                 if (timed_out) {
831                                         errno = EAGAIN;
832                                         goto out;
833                                 }
834
835                                 /* We set child->aiocb to NULL in our hooked
836                                  * AIO_RETURN(). */
837                                 if (child->aiocb == NULL) {
838                                         break;
839                                 }
840                         }
841                 }
842         }
843
844         ret = 0;
845
846   out:
847
848         TALLOC_FREE(frame);
849         return ret;
850 }
851
852 static struct vfs_fn_pointers vfs_aio_fork_fns = {
853         .aio_read = aio_fork_read,
854         .aio_write = aio_fork_write,
855         .aio_return_fn = aio_fork_return_fn,
856         .aio_cancel = aio_fork_cancel,
857         .aio_error_fn = aio_fork_error_fn,
858         .aio_suspend = aio_fork_suspend,
859 };
860
861 NTSTATUS vfs_aio_fork_init(void);
862 NTSTATUS vfs_aio_fork_init(void)
863 {
864         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
865                                 "aio_fork", &vfs_aio_fork_fns);
866 }