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