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