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