s3:vfs fix children cleanup in aio_fork
[mat/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                 child = next;
250         }
251
252         if (list->children != NULL) {
253                 /*
254                  * Re-schedule the next cleanup round
255                  */
256                 list->cleanup_event = event_add_timed(smbd_event_context(), list,
257                                                       timeval_add(&now, 30, 0),
258                                                       aio_child_cleanup, list);
259
260         }
261 }
262
263 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
264 {
265         struct aio_child_list *data = NULL;
266
267         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
268                 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
269                                         return NULL);
270         }
271
272         if (data == NULL) {
273                 data = TALLOC_ZERO_P(NULL, struct aio_child_list);
274                 if (data == NULL) {
275                         return NULL;
276                 }
277         }
278
279         /*
280          * Regardless of whether the child_list had been around or not, make
281          * sure that we have a cleanup timed event. This timed event will
282          * delete itself when it finds that no children are around anymore.
283          */
284
285         if (data->cleanup_event == NULL) {
286                 data->cleanup_event = event_add_timed(smbd_event_context(), data,
287                                                       timeval_current_ofs(30, 0),
288                                                       aio_child_cleanup, data);
289                 if (data->cleanup_event == NULL) {
290                         TALLOC_FREE(data);
291                         return NULL;
292                 }
293         }
294
295         if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
296                 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
297                                         struct aio_child_list, return False);
298         }
299
300         return data;
301 }
302
303 static void aio_child_loop(int sockfd, struct mmap_area *map)
304 {
305         while (true) {
306                 int fd = -1;
307                 ssize_t ret;
308                 struct rw_cmd cmd_struct;
309                 struct rw_ret ret_struct;
310
311                 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
312                 if (ret != sizeof(cmd_struct)) {
313                         DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
314                                    strerror(errno)));
315                         exit(1);
316                 }
317
318                 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
319                            cmd_struct.read_cmd ? "read" : "write",
320                            (int)cmd_struct.n, (int)cmd_struct.offset, fd));
321
322 #ifdef ENABLE_BUILD_FARM_HACKS
323                 {
324                         /*
325                          * In the build farm, 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 #endif
340
341
342                 ZERO_STRUCT(ret_struct);
343
344                 if (cmd_struct.read_cmd) {
345                         ret_struct.size = sys_pread(
346                                 fd, (void *)map->ptr, cmd_struct.n,
347                                 cmd_struct.offset);
348 #if 0
349 /* This breaks "make test" when run with aio_fork module. */
350 #ifdef ENABLE_BUILD_FARM_HACKS
351                         ret_struct.size = MAX(1, ret_struct.size * 0.9);
352 #endif
353 #endif
354                 }
355                 else {
356                         ret_struct.size = sys_pwrite(
357                                 fd, (void *)map->ptr, cmd_struct.n,
358                                 cmd_struct.offset);
359                 }
360
361                 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
362                            (int)ret_struct.size));
363
364                 if (ret_struct.size == -1) {
365                         ret_struct.ret_errno = errno;
366                 }
367
368                 /*
369                  * Close the fd before telling our parent we're done. The
370                  * parent might close and re-open the file very quickly, and
371                  * with system-level share modes (GPFS) we would get an
372                  * unjustified SHARING_VIOLATION.
373                  */
374                 close(fd);
375
376                 ret = write_data(sockfd, (char *)&ret_struct,
377                                  sizeof(ret_struct));
378                 if (ret != sizeof(ret_struct)) {
379                         DEBUG(10, ("could not write ret_struct: %s\n",
380                                    strerror(errno)));
381                         exit(2);
382                 }
383         }
384 }
385
386 static void handle_aio_completion(struct event_context *event_ctx,
387                                   struct fd_event *event, uint16 flags,
388                                   void *p)
389 {
390         struct aio_extra *aio_ex = NULL;
391         struct aio_child *child = (struct aio_child *)p;
392
393         DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags));
394
395         if ((flags & EVENT_FD_READ) == 0) {
396                 return;
397         }
398
399         if (!NT_STATUS_IS_OK(read_data(child->sockfd,
400                                        (char *)&child->retval,
401                                        sizeof(child->retval)))) {
402                 DEBUG(0, ("aio child %d died\n", (int)child->pid));
403                 child->retval.size = -1;
404                 child->retval.ret_errno = EIO;
405         }
406
407         if (child->cancelled) {
408                 child->aiocb = NULL;
409                 child->cancelled = false;
410                 return;
411         }
412
413         if (child->read_cmd && (child->retval.size > 0)) {
414                 SMB_ASSERT(child->retval.size <= child->aiocb->aio_nbytes);
415                 memcpy((void *)child->aiocb->aio_buf, (void *)child->map->ptr,
416                        child->retval.size);
417         }
418
419         aio_ex = (struct aio_extra *)child->aiocb->aio_sigevent.sigev_value.sival_ptr;
420         smbd_aio_complete_aio_ex(aio_ex);
421 }
422
423 static int aio_child_destructor(struct aio_child *child)
424 {
425         SMB_ASSERT((child->aiocb == NULL) || child->cancelled);
426         close(child->sockfd);
427         DLIST_REMOVE(child->list->children, child);
428         return 0;
429 }
430
431 /*
432  * We have to close all fd's in open files, we might incorrectly hold a system
433  * level share mode on a file.
434  */
435
436 static struct files_struct *close_fsp_fd(struct files_struct *fsp,
437                                          void *private_data)
438 {
439         if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
440                 close(fsp->fh->fd);
441                 fsp->fh->fd = -1;
442         }
443         return NULL;
444 }
445
446 static NTSTATUS create_aio_child(struct smbd_server_connection *sconn,
447                                  struct aio_child_list *children,
448                                  size_t map_size,
449                                  struct aio_child **presult)
450 {
451         struct aio_child *result;
452         int fdpair[2];
453         NTSTATUS status;
454
455         fdpair[0] = fdpair[1] = -1;
456
457         result = TALLOC_ZERO_P(children, struct aio_child);
458         NT_STATUS_HAVE_NO_MEMORY(result);
459
460         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
461                 status = map_nt_error_from_unix(errno);
462                 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
463                 goto fail;
464         }
465
466         DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
467
468         result->map = mmap_area_init(result, map_size);
469         if (result->map == NULL) {
470                 status = map_nt_error_from_unix(errno);
471                 DEBUG(0, ("Could not create mmap area\n"));
472                 goto fail;
473         }
474
475         result->pid = sys_fork();
476         if (result->pid == -1) {
477                 status = map_nt_error_from_unix(errno);
478                 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
479                 goto fail;
480         }
481
482         if (result->pid == 0) {
483                 close(fdpair[0]);
484                 result->sockfd = fdpair[1];
485                 files_forall(sconn, close_fsp_fd, NULL);
486                 aio_child_loop(result->sockfd, result->map);
487         }
488
489         DEBUG(10, ("Child %d created\n", result->pid));
490
491         result->sockfd = fdpair[0];
492         close(fdpair[1]);
493
494         result->sock_event = event_add_fd(smbd_event_context(), result,
495                                           result->sockfd, EVENT_FD_READ,
496                                           handle_aio_completion,
497                                           result);
498         if (result->sock_event == NULL) {
499                 status = NT_STATUS_NO_MEMORY;
500                 DEBUG(0, ("event_add_fd failed\n"));
501                 goto fail;
502         }
503
504         result->list = children;
505         DLIST_ADD(children->children, result);
506
507         talloc_set_destructor(result, aio_child_destructor);
508
509         *presult = result;
510
511         return NT_STATUS_OK;
512
513  fail:
514         if (fdpair[0] != -1) close(fdpair[0]);
515         if (fdpair[1] != -1) close(fdpair[1]);
516         TALLOC_FREE(result);
517
518         return status;
519 }
520
521 static NTSTATUS get_idle_child(struct vfs_handle_struct *handle,
522                                struct aio_child **pchild)
523 {
524         struct aio_child_list *children;
525         struct aio_child *child;
526         NTSTATUS status;
527
528         children = init_aio_children(handle);
529         if (children == NULL) {
530                 return NT_STATUS_NO_MEMORY;
531         }
532
533         for (child = children->children; child != NULL; child = child->next) {
534                 if (child->aiocb == NULL) {
535                         /* idle */
536                         break;
537                 }
538         }
539
540         if (child == NULL) {
541                 DEBUG(10, ("no idle child found, creating new one\n"));
542
543                 status = create_aio_child(handle->conn->sconn, children,
544                                           128*1024, &child);
545                 if (!NT_STATUS_IS_OK(status)) {
546                         DEBUG(10, ("create_aio_child failed: %s\n",
547                                    nt_errstr(status)));
548                         return status;
549                 }
550         }
551
552         child->dont_delete = true;
553
554         *pchild = child;
555         return NT_STATUS_OK;
556 }
557
558 static int aio_fork_read(struct vfs_handle_struct *handle,
559                          struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
560 {
561         struct aio_child *child;
562         struct rw_cmd cmd;
563         ssize_t ret;
564         NTSTATUS status;
565
566         if (aiocb->aio_nbytes > 128*1024) {
567                 /* TODO: support variable buffers */
568                 errno = EINVAL;
569                 return -1;
570         }
571
572         status = get_idle_child(handle, &child);
573         if (!NT_STATUS_IS_OK(status)) {
574                 DEBUG(10, ("Could not get an idle child\n"));
575                 return -1;
576         }
577
578         child->read_cmd = true;
579         child->aiocb = aiocb;
580         child->retval.ret_errno = EINPROGRESS;
581
582         ZERO_STRUCT(cmd);
583         cmd.n = aiocb->aio_nbytes;
584         cmd.offset = aiocb->aio_offset;
585         cmd.read_cmd = child->read_cmd;
586
587         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
588                    (int)child->pid));
589
590         ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
591         if (ret == -1) {
592                 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
593                 return -1;
594         }
595
596         return 0;
597 }
598
599 static int aio_fork_write(struct vfs_handle_struct *handle,
600                           struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
601 {
602         struct aio_child *child;
603         struct rw_cmd cmd;
604         ssize_t ret;
605         NTSTATUS status;
606
607         if (aiocb->aio_nbytes > 128*1024) {
608                 /* TODO: support variable buffers */
609                 errno = EINVAL;
610                 return -1;
611         }
612
613         status = get_idle_child(handle, &child);
614         if (!NT_STATUS_IS_OK(status)) {
615                 DEBUG(10, ("Could not get an idle child\n"));
616                 return -1;
617         }
618
619         child->read_cmd = false;
620         child->aiocb = aiocb;
621         child->retval.ret_errno = EINPROGRESS;
622
623         memcpy((void *)child->map->ptr, (void *)aiocb->aio_buf,
624                aiocb->aio_nbytes);
625
626         ZERO_STRUCT(cmd);
627         cmd.n = aiocb->aio_nbytes;
628         cmd.offset = aiocb->aio_offset;
629         cmd.read_cmd = child->read_cmd;
630
631         DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
632                    (int)child->pid));
633
634         ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
635         if (ret == -1) {
636                 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
637                 return -1;
638         }
639
640         return 0;
641 }
642
643 static struct aio_child *aio_fork_find_child(struct vfs_handle_struct *handle,
644                                              SMB_STRUCT_AIOCB *aiocb)
645 {
646         struct aio_child_list *children;
647         struct aio_child *child;
648
649         children = init_aio_children(handle);
650         if (children == NULL) {
651                 return NULL;
652         }
653
654         for (child = children->children; child != NULL; child = child->next) {
655                 if (child->aiocb == aiocb) {
656                         return child;
657                 }
658         }
659
660         return NULL;
661 }
662
663 static ssize_t aio_fork_return_fn(struct vfs_handle_struct *handle,
664                                   struct files_struct *fsp,
665                                   SMB_STRUCT_AIOCB *aiocb)
666 {
667         struct aio_child *child = aio_fork_find_child(handle, aiocb);
668
669         if (child == NULL) {
670                 errno = EINVAL;
671                 DEBUG(0, ("returning EINVAL\n"));
672                 return -1;
673         }
674
675         child->aiocb = NULL;
676
677         if (child->retval.size == -1) {
678                 errno = child->retval.ret_errno;
679         }
680
681         return child->retval.size;
682 }
683
684 static int aio_fork_cancel(struct vfs_handle_struct *handle,
685                            struct files_struct *fsp,
686                            SMB_STRUCT_AIOCB *aiocb)
687 {
688         struct aio_child_list *children;
689         struct aio_child *child;
690
691         children = init_aio_children(handle);
692         if (children == NULL) {
693                 errno = EINVAL;
694                 return -1;
695         }
696
697         for (child = children->children; child != NULL; child = child->next) {
698                 if (child->aiocb == NULL) {
699                         continue;
700                 }
701                 if (child->aiocb->aio_fildes != fsp->fh->fd) {
702                         continue;
703                 }
704                 if ((aiocb != NULL) && (child->aiocb != aiocb)) {
705                         continue;
706                 }
707
708                 /*
709                  * We let the child do its job, but we discard the result when
710                  * it's finished.
711                  */
712
713                 child->cancelled = true;
714         }
715
716         return AIO_CANCELED;
717 }
718
719 static int aio_fork_error_fn(struct vfs_handle_struct *handle,
720                              struct files_struct *fsp,
721                              SMB_STRUCT_AIOCB *aiocb)
722 {
723         struct aio_child *child = aio_fork_find_child(handle, aiocb);
724
725         if (child == NULL) {
726                 errno = EINVAL;
727                 return -1;
728         }
729
730         return child->retval.ret_errno;
731 }
732
733 static void aio_fork_suspend_timed_out(struct tevent_context *event_ctx,
734                                         struct tevent_timer *te,
735                                         struct timeval now,
736                                         void *private_data)
737 {
738         bool *timed_out = (bool *)private_data;
739         /* Remove this timed event handler. */
740         TALLOC_FREE(te);
741         *timed_out = true;
742 }
743
744 static int aio_fork_suspend(struct vfs_handle_struct *handle,
745                         struct files_struct *fsp,
746                         const SMB_STRUCT_AIOCB * const aiocb_array[],
747                         int n,
748                         const struct timespec *timeout)
749 {
750         struct aio_child_list *children = NULL;
751         TALLOC_CTX *frame = talloc_stackframe();
752         struct event_context *ev = NULL;
753         int i;
754         int ret = -1;
755         bool timed_out = false;
756
757         children = init_aio_children(handle);
758         if (children == NULL) {
759                 errno = EINVAL;
760                 goto out;
761         }
762
763         /* This is a blocking call, and has to use a sub-event loop. */
764         ev = event_context_init(frame);
765         if (ev == NULL) {
766                 errno = ENOMEM;
767                 goto out;
768         }
769
770         if (timeout) {
771                 struct timeval tv = convert_timespec_to_timeval(*timeout);
772                 struct tevent_timer *te = tevent_add_timer(ev,
773                                                 frame,
774                                                 timeval_current_ofs(tv.tv_sec,
775                                                                     tv.tv_usec),
776                                                 aio_fork_suspend_timed_out,
777                                                 &timed_out);
778                 if (!te) {
779                         errno = ENOMEM;
780                         goto out;
781                 }
782         }
783
784         for (i = 0; i < n; i++) {
785                 struct aio_child *child = NULL;
786                 const SMB_STRUCT_AIOCB *aiocb = aiocb_array[i];
787
788                 if (!aiocb) {
789                         continue;
790                 }
791
792                 /*
793                  * We're going to cheat here. We know that smbd/aio.c
794                  * only calls this when it's waiting for every single
795                  * outstanding call to finish on a close, so just wait
796                  * individually for each IO to complete. We don't care
797                  * what order they finish - only that they all do. JRA.
798                  */
799
800                 for (child = children->children; child != NULL; child = child->next) {
801                         if (child->aiocb == NULL) {
802                                 continue;
803                         }
804                         if (child->aiocb->aio_fildes != fsp->fh->fd) {
805                                 continue;
806                         }
807                         if (child->aiocb != aiocb) {
808                                 continue;
809                         }
810
811                         if (child->aiocb->aio_sigevent.sigev_value.sival_ptr == NULL) {
812                                 continue;
813                         }
814
815                         /* We're never using this event on the
816                          * main event context again... */
817                         TALLOC_FREE(child->sock_event);
818
819                         child->sock_event = event_add_fd(ev,
820                                                 child,
821                                                 child->sockfd,
822                                                 EVENT_FD_READ,
823                                                 handle_aio_completion,
824                                                 child);
825
826                         while (1) {
827                                 if (tevent_loop_once(ev) == -1) {
828                                         goto out;
829                                 }
830
831                                 if (timed_out) {
832                                         errno = EAGAIN;
833                                         goto out;
834                                 }
835
836                                 /* We set child->aiocb to NULL in our hooked
837                                  * AIO_RETURN(). */
838                                 if (child->aiocb == NULL) {
839                                         break;
840                                 }
841                         }
842                 }
843         }
844
845         ret = 0;
846
847   out:
848
849         TALLOC_FREE(frame);
850         return ret;
851 }
852
853 static struct vfs_fn_pointers vfs_aio_fork_fns = {
854         .aio_read = aio_fork_read,
855         .aio_write = aio_fork_write,
856         .aio_return_fn = aio_fork_return_fn,
857         .aio_cancel = aio_fork_cancel,
858         .aio_error_fn = aio_fork_error_fn,
859         .aio_suspend = aio_fork_suspend,
860 };
861
862 NTSTATUS vfs_aio_fork_init(void);
863 NTSTATUS vfs_aio_fork_init(void)
864 {
865         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
866                                 "aio_fork", &vfs_aio_fork_fns);
867 }