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