2 * Simulate the Posix AIO using mmap/fork
4 * Copyright (C) Volker Lendecke 2008
5 * Copyright (C) Jeremy Allison 2010
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.
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.
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.
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"
36 struct aio_fork_config
{
37 bool erratic_testing_mode
;
45 static int mmap_area_destructor(struct mmap_area
*area
)
47 munmap((void *)area
->ptr
, area
->size
);
51 static struct mmap_area
*mmap_area_init(TALLOC_CTX
*mem_ctx
, size_t size
)
53 struct mmap_area
*result
;
56 result
= talloc(mem_ctx
, struct mmap_area
);
58 DEBUG(0, ("talloc failed\n"));
62 fd
= open("/dev/zero", O_RDWR
);
64 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
69 result
->ptr
= mmap(NULL
, size
, PROT_READ
|PROT_WRITE
,
70 MAP_SHARED
|MAP_FILE
, fd
, 0);
71 if (result
->ptr
== MAP_FAILED
) {
72 DEBUG(1, ("mmap failed: %s\n", strerror(errno
)));
79 talloc_set_destructor(result
, mmap_area_destructor
);
94 static const char *cmd_type_str(enum cmd_type cmd
)
109 result
= "<UNKNOWN>";
119 bool erratic_testing_mode
;
127 struct aio_child_list
;
130 struct aio_child
*prev
, *next
;
131 struct aio_child_list
*list
;
134 struct mmap_area
*map
;
135 bool dont_delete
; /* Marked as in use since last cleanup */
139 struct aio_child_list
{
140 struct aio_child
*children
;
141 struct timed_event
*cleanup_event
;
144 static void free_aio_children(void **p
)
149 static ssize_t
read_fd(int fd
, void *ptr
, size_t nbytes
, int *recvfd
)
154 #ifndef HAVE_MSGHDR_MSG_CONTROL
158 #ifdef HAVE_MSGHDR_MSG_CONTROL
161 char control
[CMSG_SPACE(sizeof(int))];
163 struct cmsghdr
*cmptr
;
165 msg
.msg_control
= control_un
.control
;
166 msg
.msg_controllen
= sizeof(control_un
.control
);
168 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
169 msg
.msg_accrights
= (caddr_t
) &newfd
;
170 msg
.msg_accrightslen
= sizeof(int);
172 #error Can not pass file descriptors
180 iov
[0].iov_base
= (void *)ptr
;
181 iov
[0].iov_len
= nbytes
;
185 if ( (n
= recvmsg(fd
, &msg
, 0)) <= 0) {
189 #ifdef HAVE_MSGHDR_MSG_CONTROL
190 if ((cmptr
= CMSG_FIRSTHDR(&msg
)) != NULL
191 && cmptr
->cmsg_len
== CMSG_LEN(sizeof(int))) {
192 if (cmptr
->cmsg_level
!= SOL_SOCKET
) {
193 DEBUG(10, ("control level != SOL_SOCKET"));
197 if (cmptr
->cmsg_type
!= SCM_RIGHTS
) {
198 DEBUG(10, ("control type != SCM_RIGHTS"));
202 memcpy(recvfd
, CMSG_DATA(cmptr
), sizeof(*recvfd
));
204 *recvfd
= -1; /* descriptor was not passed */
207 if (msg
.msg_accrightslen
== sizeof(int)) {
211 *recvfd
= -1; /* descriptor was not passed */
218 static ssize_t
write_fd(int fd
, void *ptr
, size_t nbytes
, int sendfd
)
223 #ifdef HAVE_MSGHDR_MSG_CONTROL
226 char control
[CMSG_SPACE(sizeof(int))];
228 struct cmsghdr
*cmptr
;
231 ZERO_STRUCT(control_un
);
233 msg
.msg_control
= control_un
.control
;
234 msg
.msg_controllen
= sizeof(control_un
.control
);
236 cmptr
= CMSG_FIRSTHDR(&msg
);
237 cmptr
->cmsg_len
= CMSG_LEN(sizeof(int));
238 cmptr
->cmsg_level
= SOL_SOCKET
;
239 cmptr
->cmsg_type
= SCM_RIGHTS
;
240 memcpy(CMSG_DATA(cmptr
), &sendfd
, sizeof(sendfd
));
243 msg
.msg_accrights
= (caddr_t
) &sendfd
;
244 msg
.msg_accrightslen
= sizeof(int);
251 iov
[0].iov_base
= (void *)ptr
;
252 iov
[0].iov_len
= nbytes
;
256 return (sendmsg(fd
, &msg
, 0));
259 static void aio_child_cleanup(struct event_context
*event_ctx
,
260 struct timed_event
*te
,
264 struct aio_child_list
*list
= talloc_get_type_abort(
265 private_data
, struct aio_child_list
);
266 struct aio_child
*child
, *next
;
268 TALLOC_FREE(list
->cleanup_event
);
270 for (child
= list
->children
; child
!= NULL
; child
= next
) {
274 DEBUG(10, ("child %d currently active\n",
279 if (child
->dont_delete
) {
280 DEBUG(10, ("Child %d was active since last cleanup\n",
282 child
->dont_delete
= false;
286 DEBUG(10, ("Child %d idle for more than 30 seconds, "
287 "deleting\n", (int)child
->pid
));
293 if (list
->children
!= NULL
) {
295 * Re-schedule the next cleanup round
297 list
->cleanup_event
= event_add_timed(server_event_context(), list
,
298 timeval_add(&now
, 30, 0),
299 aio_child_cleanup
, list
);
304 static struct aio_child_list
*init_aio_children(struct vfs_handle_struct
*handle
)
306 struct aio_child_list
*data
= NULL
;
308 if (SMB_VFS_HANDLE_TEST_DATA(handle
)) {
309 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct aio_child_list
,
314 data
= talloc_zero(NULL
, struct aio_child_list
);
321 * Regardless of whether the child_list had been around or not, make
322 * sure that we have a cleanup timed event. This timed event will
323 * delete itself when it finds that no children are around anymore.
326 if (data
->cleanup_event
== NULL
) {
327 data
->cleanup_event
= event_add_timed(server_event_context(), data
,
328 timeval_current_ofs(30, 0),
329 aio_child_cleanup
, data
);
330 if (data
->cleanup_event
== NULL
) {
336 if (!SMB_VFS_HANDLE_TEST_DATA(handle
)) {
337 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_aio_children
,
338 struct aio_child_list
, return False
);
344 static void aio_child_loop(int sockfd
, struct mmap_area
*map
)
349 struct rw_cmd cmd_struct
;
350 struct rw_ret ret_struct
;
352 ret
= read_fd(sockfd
, &cmd_struct
, sizeof(cmd_struct
), &fd
);
353 if (ret
!= sizeof(cmd_struct
)) {
354 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret
,
359 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
360 cmd_type_str(cmd_struct
.cmd
),
361 (int)cmd_struct
.n
, (int)cmd_struct
.offset
, fd
));
363 if (cmd_struct
.erratic_testing_mode
) {
365 * For developer testing, we want erratic behaviour for
371 * use generate_random_buffer, we just forked from a
372 * common parent state
374 generate_random_buffer(&randval
, sizeof(randval
));
375 msecs
= randval
+ 20;
376 DEBUG(10, ("delaying for %u msecs\n", msecs
));
380 ZERO_STRUCT(ret_struct
);
382 switch (cmd_struct
.cmd
) {
384 ret_struct
.size
= sys_pread(
385 fd
, (void *)map
->ptr
, cmd_struct
.n
,
388 /* This breaks "make test" when run with aio_fork module. */
390 ret_struct
.size
= MAX(1, ret_struct
.size
* 0.9);
395 ret_struct
.size
= sys_pwrite(
396 fd
, (void *)map
->ptr
, cmd_struct
.n
,
400 ret_struct
.size
= fsync(fd
);
403 ret_struct
.size
= -1;
407 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
408 (int)ret_struct
.size
));
410 if (ret_struct
.size
== -1) {
411 ret_struct
.ret_errno
= errno
;
415 * Close the fd before telling our parent we're done. The
416 * parent might close and re-open the file very quickly, and
417 * with system-level share modes (GPFS) we would get an
418 * unjustified SHARING_VIOLATION.
422 ret
= write_data(sockfd
, (char *)&ret_struct
,
424 if (ret
!= sizeof(ret_struct
)) {
425 DEBUG(10, ("could not write ret_struct: %s\n",
432 static int aio_child_destructor(struct aio_child
*child
)
436 SMB_ASSERT(!child
->busy
);
438 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
439 child
->pid
, child
->sockfd
));
442 * closing the sockfd makes the child not return from recvmsg() on RHEL
443 * 5.5 so instead force the child to exit by writing bad data to it
445 write(child
->sockfd
, &c
, sizeof(c
));
446 close(child
->sockfd
);
447 DLIST_REMOVE(child
->list
->children
, child
);
452 * We have to close all fd's in open files, we might incorrectly hold a system
453 * level share mode on a file.
456 static struct files_struct
*close_fsp_fd(struct files_struct
*fsp
,
459 if ((fsp
->fh
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
466 static int create_aio_child(struct smbd_server_connection
*sconn
,
467 struct aio_child_list
*children
,
469 struct aio_child
**presult
)
471 struct aio_child
*result
;
475 fdpair
[0] = fdpair
[1] = -1;
477 result
= talloc_zero(children
, struct aio_child
);
478 if (result
== NULL
) {
482 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) == -1) {
484 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno
)));
488 DEBUG(10, ("fdpair = %d/%d\n", fdpair
[0], fdpair
[1]));
490 result
->map
= mmap_area_init(result
, map_size
);
491 if (result
->map
== NULL
) {
493 DEBUG(0, ("Could not create mmap area\n"));
497 result
->pid
= fork();
498 if (result
->pid
== -1) {
500 DEBUG(0, ("fork failed: %s\n", strerror(errno
)));
504 if (result
->pid
== 0) {
506 result
->sockfd
= fdpair
[1];
507 files_forall(sconn
, close_fsp_fd
, NULL
);
508 aio_child_loop(result
->sockfd
, result
->map
);
511 DEBUG(10, ("Child %d created with sockfd %d\n",
512 result
->pid
, fdpair
[0]));
514 result
->sockfd
= fdpair
[0];
517 result
->list
= children
;
518 DLIST_ADD(children
->children
, result
);
520 talloc_set_destructor(result
, aio_child_destructor
);
527 if (fdpair
[0] != -1) close(fdpair
[0]);
528 if (fdpair
[1] != -1) close(fdpair
[1]);
534 static int get_idle_child(struct vfs_handle_struct
*handle
,
535 struct aio_child
**pchild
)
537 struct aio_child_list
*children
;
538 struct aio_child
*child
;
540 children
= init_aio_children(handle
);
541 if (children
== NULL
) {
545 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
554 DEBUG(10, ("no idle child found, creating new one\n"));
556 ret
= create_aio_child(handle
->conn
->sconn
, children
,
559 DEBUG(10, ("create_aio_child failed: %s\n",
565 child
->dont_delete
= true;
572 struct aio_fork_pread_state
{
573 struct aio_child
*child
;
578 static void aio_fork_pread_done(struct tevent_req
*subreq
);
580 static struct tevent_req
*aio_fork_pread_send(struct vfs_handle_struct
*handle
,
582 struct tevent_context
*ev
,
583 struct files_struct
*fsp
,
585 size_t n
, off_t offset
)
587 struct tevent_req
*req
, *subreq
;
588 struct aio_fork_pread_state
*state
;
592 struct aio_fork_config
*config
;
594 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
595 struct aio_fork_config
,
598 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pread_state
);
604 /* TODO: support variable buffers */
605 tevent_req_error(req
, EINVAL
);
606 return tevent_req_post(req
, ev
);
609 err
= get_idle_child(handle
, &state
->child
);
611 tevent_req_error(req
, err
);
612 return tevent_req_post(req
, ev
);
619 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
621 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
622 (int)state
->child
->pid
));
625 * Not making this async. We're writing into an empty unix
626 * domain socket. This should never block.
628 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
633 TALLOC_FREE(state
->child
);
635 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
636 tevent_req_error(req
, err
);
637 return tevent_req_post(req
, ev
);
640 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
641 sizeof(struct rw_ret
), NULL
, NULL
);
642 if (tevent_req_nomem(subreq
, req
)) {
643 TALLOC_FREE(state
->child
); /* we sent sth down */
644 return tevent_req_post(req
, ev
);
646 tevent_req_set_callback(subreq
, aio_fork_pread_done
, req
);
650 static void aio_fork_pread_done(struct tevent_req
*subreq
)
652 struct tevent_req
*req
= tevent_req_callback_data(
653 subreq
, struct tevent_req
);
654 struct aio_fork_pread_state
*state
= tevent_req_data(
655 req
, struct aio_fork_pread_state
);
659 struct rw_ret
*retbuf
;
661 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
664 TALLOC_FREE(state
->child
);
665 tevent_req_error(req
, err
);
669 state
->child
->busy
= false;
671 retbuf
= (struct rw_ret
*)buf
;
672 state
->ret
= retbuf
->size
;
673 state
->err
= retbuf
->ret_errno
;
674 tevent_req_done(req
);
677 static ssize_t
aio_fork_pread_recv(struct tevent_req
*req
, int *err
)
679 struct aio_fork_pread_state
*state
= tevent_req_data(
680 req
, struct aio_fork_pread_state
);
682 if (tevent_req_is_unix_error(req
, err
)) {
685 if (state
->ret
== -1) {
691 struct aio_fork_pwrite_state
{
692 struct aio_child
*child
;
697 static void aio_fork_pwrite_done(struct tevent_req
*subreq
);
699 static struct tevent_req
*aio_fork_pwrite_send(
700 struct vfs_handle_struct
*handle
, TALLOC_CTX
*mem_ctx
,
701 struct tevent_context
*ev
, struct files_struct
*fsp
,
702 const void *data
, size_t n
, off_t offset
)
704 struct tevent_req
*req
, *subreq
;
705 struct aio_fork_pwrite_state
*state
;
709 struct aio_fork_config
*config
;
710 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
711 struct aio_fork_config
,
714 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pwrite_state
);
720 /* TODO: support variable buffers */
721 tevent_req_error(req
, EINVAL
);
722 return tevent_req_post(req
, ev
);
725 err
= get_idle_child(handle
, &state
->child
);
727 tevent_req_error(req
, err
);
728 return tevent_req_post(req
, ev
);
735 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
737 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
738 (int)state
->child
->pid
));
741 * Not making this async. We're writing into an empty unix
742 * domain socket. This should never block.
744 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
749 TALLOC_FREE(state
->child
);
751 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
752 tevent_req_error(req
, err
);
753 return tevent_req_post(req
, ev
);
756 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
757 sizeof(struct rw_ret
), NULL
, NULL
);
758 if (tevent_req_nomem(subreq
, req
)) {
759 TALLOC_FREE(state
->child
); /* we sent sth down */
760 return tevent_req_post(req
, ev
);
762 tevent_req_set_callback(subreq
, aio_fork_pwrite_done
, req
);
766 static void aio_fork_pwrite_done(struct tevent_req
*subreq
)
768 struct tevent_req
*req
= tevent_req_callback_data(
769 subreq
, struct tevent_req
);
770 struct aio_fork_pwrite_state
*state
= tevent_req_data(
771 req
, struct aio_fork_pwrite_state
);
775 struct rw_ret
*retbuf
;
777 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
780 TALLOC_FREE(state
->child
);
781 tevent_req_error(req
, err
);
785 state
->child
->busy
= false;
787 retbuf
= (struct rw_ret
*)buf
;
788 state
->ret
= retbuf
->size
;
789 state
->err
= retbuf
->ret_errno
;
790 tevent_req_done(req
);
793 static ssize_t
aio_fork_pwrite_recv(struct tevent_req
*req
, int *err
)
795 struct aio_fork_pwrite_state
*state
= tevent_req_data(
796 req
, struct aio_fork_pwrite_state
);
798 if (tevent_req_is_unix_error(req
, err
)) {
801 if (state
->ret
== -1) {
807 struct aio_fork_fsync_state
{
808 struct aio_child
*child
;
813 static void aio_fork_fsync_done(struct tevent_req
*subreq
);
815 static struct tevent_req
*aio_fork_fsync_send(
816 struct vfs_handle_struct
*handle
, TALLOC_CTX
*mem_ctx
,
817 struct tevent_context
*ev
, struct files_struct
*fsp
)
819 struct tevent_req
*req
, *subreq
;
820 struct aio_fork_fsync_state
*state
;
824 struct aio_fork_config
*config
;
826 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
827 struct aio_fork_config
,
830 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_fsync_state
);
835 err
= get_idle_child(handle
, &state
->child
);
837 tevent_req_error(req
, err
);
838 return tevent_req_post(req
, ev
);
843 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
845 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
846 (int)state
->child
->pid
));
849 * Not making this async. We're writing into an empty unix
850 * domain socket. This should never block.
852 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
857 TALLOC_FREE(state
->child
);
859 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
860 tevent_req_error(req
, err
);
861 return tevent_req_post(req
, ev
);
864 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
865 sizeof(struct rw_ret
), NULL
, NULL
);
866 if (tevent_req_nomem(subreq
, req
)) {
867 TALLOC_FREE(state
->child
); /* we sent sth down */
868 return tevent_req_post(req
, ev
);
870 tevent_req_set_callback(subreq
, aio_fork_fsync_done
, req
);
874 static void aio_fork_fsync_done(struct tevent_req
*subreq
)
876 struct tevent_req
*req
= tevent_req_callback_data(
877 subreq
, struct tevent_req
);
878 struct aio_fork_fsync_state
*state
= tevent_req_data(
879 req
, struct aio_fork_fsync_state
);
883 struct rw_ret
*retbuf
;
885 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
888 TALLOC_FREE(state
->child
);
889 tevent_req_error(req
, err
);
893 state
->child
->busy
= false;
895 retbuf
= (struct rw_ret
*)buf
;
896 state
->ret
= retbuf
->size
;
897 state
->err
= retbuf
->ret_errno
;
898 tevent_req_done(req
);
901 static int aio_fork_fsync_recv(struct tevent_req
*req
, int *err
)
903 struct aio_fork_fsync_state
*state
= tevent_req_data(
904 req
, struct aio_fork_fsync_state
);
906 if (tevent_req_is_unix_error(req
, err
)) {
909 if (state
->ret
== -1) {
915 static int aio_fork_connect(vfs_handle_struct
*handle
, const char *service
,
919 struct aio_fork_config
*config
;
920 ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
926 config
= talloc_zero(handle
->conn
, struct aio_fork_config
);
928 SMB_VFS_NEXT_DISCONNECT(handle
);
929 DEBUG(0, ("talloc_zero() failed\n"));
933 config
->erratic_testing_mode
= lp_parm_bool(SNUM(handle
->conn
), "vfs_aio_fork",
934 "erratic_testing_mode", false);
936 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
937 NULL
, struct aio_fork_config
,
940 /*********************************************************************
941 * How many threads to initialize ?
942 * 100 per process seems insane as a default until you realize that
943 * (a) Threads terminate after 1 second when idle.
944 * (b) Throttling is done in SMB2 via the crediting algorithm.
945 * (c) SMB1 clients are limited to max_mux (50) outstanding
946 * requests and Windows clients don't use this anyway.
947 * Essentially we want this to be unlimited unless smb.conf
949 *********************************************************************/
950 aio_pending_size
= 100;
954 static struct vfs_fn_pointers vfs_aio_fork_fns
= {
955 .connect_fn
= aio_fork_connect
,
956 .pread_send_fn
= aio_fork_pread_send
,
957 .pread_recv_fn
= aio_fork_pread_recv
,
958 .pwrite_send_fn
= aio_fork_pwrite_send
,
959 .pwrite_recv_fn
= aio_fork_pwrite_recv
,
960 .fsync_send_fn
= aio_fork_fsync_send
,
961 .fsync_recv_fn
= aio_fork_fsync_recv
,
964 NTSTATUS
vfs_aio_fork_init(void);
965 NTSTATUS
vfs_aio_fork_init(void)
967 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
968 "aio_fork", &vfs_aio_fork_fns
);