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"
30 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
31 # error Can not pass file descriptors
40 struct aio_fork_config
{
41 bool erratic_testing_mode
;
49 static int mmap_area_destructor(struct mmap_area
*area
)
51 munmap((void *)area
->ptr
, area
->size
);
55 static struct mmap_area
*mmap_area_init(TALLOC_CTX
*mem_ctx
, size_t size
)
57 struct mmap_area
*result
;
60 result
= talloc(mem_ctx
, struct mmap_area
);
62 DEBUG(0, ("talloc failed\n"));
66 fd
= open("/dev/zero", O_RDWR
);
68 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
73 result
->ptr
= mmap(NULL
, size
, PROT_READ
|PROT_WRITE
,
74 MAP_SHARED
|MAP_FILE
, fd
, 0);
76 if (result
->ptr
== MAP_FAILED
) {
77 DEBUG(1, ("mmap failed: %s\n", strerror(errno
)));
82 talloc_set_destructor(result
, mmap_area_destructor
);
97 static const char *cmd_type_str(enum cmd_type cmd
)
112 result
= "<UNKNOWN>";
122 bool erratic_testing_mode
;
130 struct aio_child_list
;
133 struct aio_child
*prev
, *next
;
134 struct aio_child_list
*list
;
137 struct mmap_area
*map
;
138 bool dont_delete
; /* Marked as in use since last cleanup */
142 struct aio_child_list
{
143 struct aio_child
*children
;
144 struct tevent_timer
*cleanup_event
;
147 static void free_aio_children(void **p
)
152 static ssize_t
read_fd(int fd
, void *ptr
, size_t nbytes
, int *recvfd
)
157 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
160 msg
.msg_accrights
= (caddr_t
) &newfd
;
161 msg
.msg_accrightslen
= sizeof(int);
166 char control
[CMSG_SPACE(sizeof(int))];
168 struct cmsghdr
*cmptr
;
170 msg
.msg_control
= control_un
.control
;
171 msg
.msg_controllen
= sizeof(control_un
.control
);
178 iov
[0].iov_base
= (void *)ptr
;
179 iov
[0].iov_len
= nbytes
;
183 if ( (n
= recvmsg(fd
, &msg
, 0)) <= 0) {
187 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
188 if ((cmptr
= CMSG_FIRSTHDR(&msg
)) != NULL
189 && cmptr
->cmsg_len
== CMSG_LEN(sizeof(int))) {
190 if (cmptr
->cmsg_level
!= SOL_SOCKET
) {
191 DEBUG(10, ("control level != SOL_SOCKET"));
195 if (cmptr
->cmsg_type
!= SCM_RIGHTS
) {
196 DEBUG(10, ("control type != SCM_RIGHTS"));
200 memcpy(recvfd
, CMSG_DATA(cmptr
), sizeof(*recvfd
));
202 *recvfd
= -1; /* descriptor was not passed */
205 if (msg
.msg_accrightslen
== sizeof(int)) {
209 *recvfd
= -1; /* descriptor was not passed */
216 static ssize_t
write_fd(int fd
, void *ptr
, size_t nbytes
, int sendfd
)
221 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
224 char control
[CMSG_SPACE(sizeof(int))];
226 struct cmsghdr
*cmptr
;
229 ZERO_STRUCT(control_un
);
231 msg
.msg_control
= control_un
.control
;
232 msg
.msg_controllen
= sizeof(control_un
.control
);
234 cmptr
= CMSG_FIRSTHDR(&msg
);
235 cmptr
->cmsg_len
= CMSG_LEN(sizeof(int));
236 cmptr
->cmsg_level
= SOL_SOCKET
;
237 cmptr
->cmsg_type
= SCM_RIGHTS
;
238 memcpy(CMSG_DATA(cmptr
), &sendfd
, sizeof(sendfd
));
241 msg
.msg_accrights
= (caddr_t
) &sendfd
;
242 msg
.msg_accrightslen
= sizeof(int);
249 iov
[0].iov_base
= (void *)ptr
;
250 iov
[0].iov_len
= nbytes
;
254 return (sendmsg(fd
, &msg
, 0));
257 static void aio_child_cleanup(struct tevent_context
*event_ctx
,
258 struct tevent_timer
*te
,
262 struct aio_child_list
*list
= talloc_get_type_abort(
263 private_data
, struct aio_child_list
);
264 struct aio_child
*child
, *next
;
266 TALLOC_FREE(list
->cleanup_event
);
268 for (child
= list
->children
; child
!= NULL
; child
= next
) {
272 DEBUG(10, ("child %d currently active\n",
277 if (child
->dont_delete
) {
278 DEBUG(10, ("Child %d was active since last cleanup\n",
280 child
->dont_delete
= false;
284 DEBUG(10, ("Child %d idle for more than 30 seconds, "
285 "deleting\n", (int)child
->pid
));
291 if (list
->children
!= NULL
) {
293 * Re-schedule the next cleanup round
295 list
->cleanup_event
= tevent_add_timer(server_event_context(), list
,
296 timeval_add(&now
, 30, 0),
297 aio_child_cleanup
, list
);
302 static struct aio_child_list
*init_aio_children(struct vfs_handle_struct
*handle
)
304 struct aio_child_list
*data
= NULL
;
306 if (SMB_VFS_HANDLE_TEST_DATA(handle
)) {
307 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct aio_child_list
,
312 data
= talloc_zero(NULL
, struct aio_child_list
);
319 * Regardless of whether the child_list had been around or not, make
320 * sure that we have a cleanup timed event. This timed event will
321 * delete itself when it finds that no children are around anymore.
324 if (data
->cleanup_event
== NULL
) {
325 data
->cleanup_event
= tevent_add_timer(server_event_context(), data
,
326 timeval_current_ofs(30, 0),
327 aio_child_cleanup
, data
);
328 if (data
->cleanup_event
== NULL
) {
334 if (!SMB_VFS_HANDLE_TEST_DATA(handle
)) {
335 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_aio_children
,
336 struct aio_child_list
, return False
);
342 static void aio_child_loop(int sockfd
, struct mmap_area
*map
)
347 struct rw_cmd cmd_struct
;
348 struct rw_ret ret_struct
;
350 ret
= read_fd(sockfd
, &cmd_struct
, sizeof(cmd_struct
), &fd
);
351 if (ret
!= sizeof(cmd_struct
)) {
352 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret
,
357 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
358 cmd_type_str(cmd_struct
.cmd
),
359 (int)cmd_struct
.n
, (int)cmd_struct
.offset
, fd
));
361 if (cmd_struct
.erratic_testing_mode
) {
363 * For developer testing, we want erratic behaviour for
369 * use generate_random_buffer, we just forked from a
370 * common parent state
372 generate_random_buffer(&randval
, sizeof(randval
));
373 msecs
= randval
+ 20;
374 DEBUG(10, ("delaying for %u msecs\n", msecs
));
378 ZERO_STRUCT(ret_struct
);
380 switch (cmd_struct
.cmd
) {
382 ret_struct
.size
= sys_pread(
383 fd
, (void *)map
->ptr
, cmd_struct
.n
,
386 /* This breaks "make test" when run with aio_fork module. */
388 ret_struct
.size
= MAX(1, ret_struct
.size
* 0.9);
393 ret_struct
.size
= sys_pwrite(
394 fd
, (void *)map
->ptr
, cmd_struct
.n
,
398 ret_struct
.size
= fsync(fd
);
401 ret_struct
.size
= -1;
405 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
406 (int)ret_struct
.size
));
408 if (ret_struct
.size
== -1) {
409 ret_struct
.ret_errno
= errno
;
413 * Close the fd before telling our parent we're done. The
414 * parent might close and re-open the file very quickly, and
415 * with system-level share modes (GPFS) we would get an
416 * unjustified SHARING_VIOLATION.
420 ret
= write_data(sockfd
, (char *)&ret_struct
,
422 if (ret
!= sizeof(ret_struct
)) {
423 DEBUG(10, ("could not write ret_struct: %s\n",
430 static int aio_child_destructor(struct aio_child
*child
)
434 SMB_ASSERT(!child
->busy
);
436 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
437 child
->pid
, child
->sockfd
));
440 * closing the sockfd makes the child not return from recvmsg() on RHEL
441 * 5.5 so instead force the child to exit by writing bad data to it
443 write(child
->sockfd
, &c
, sizeof(c
));
444 close(child
->sockfd
);
445 DLIST_REMOVE(child
->list
->children
, child
);
450 * We have to close all fd's in open files, we might incorrectly hold a system
451 * level share mode on a file.
454 static struct files_struct
*close_fsp_fd(struct files_struct
*fsp
,
457 if ((fsp
->fh
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
464 static int create_aio_child(struct smbd_server_connection
*sconn
,
465 struct aio_child_list
*children
,
467 struct aio_child
**presult
)
469 struct aio_child
*result
;
473 fdpair
[0] = fdpair
[1] = -1;
475 result
= talloc_zero(children
, struct aio_child
);
476 if (result
== NULL
) {
480 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) == -1) {
482 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno
)));
486 DEBUG(10, ("fdpair = %d/%d\n", fdpair
[0], fdpair
[1]));
488 result
->map
= mmap_area_init(result
, map_size
);
489 if (result
->map
== NULL
) {
491 DEBUG(0, ("Could not create mmap area\n"));
495 result
->pid
= fork();
496 if (result
->pid
== -1) {
498 DEBUG(0, ("fork failed: %s\n", strerror(errno
)));
502 if (result
->pid
== 0) {
504 result
->sockfd
= fdpair
[1];
505 files_forall(sconn
, close_fsp_fd
, NULL
);
506 aio_child_loop(result
->sockfd
, result
->map
);
509 DEBUG(10, ("Child %d created with sockfd %d\n",
510 result
->pid
, fdpair
[0]));
512 result
->sockfd
= fdpair
[0];
515 result
->list
= children
;
516 DLIST_ADD(children
->children
, result
);
518 talloc_set_destructor(result
, aio_child_destructor
);
525 if (fdpair
[0] != -1) close(fdpair
[0]);
526 if (fdpair
[1] != -1) close(fdpair
[1]);
532 static int get_idle_child(struct vfs_handle_struct
*handle
,
533 struct aio_child
**pchild
)
535 struct aio_child_list
*children
;
536 struct aio_child
*child
;
538 children
= init_aio_children(handle
);
539 if (children
== NULL
) {
543 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
552 DEBUG(10, ("no idle child found, creating new one\n"));
554 ret
= create_aio_child(handle
->conn
->sconn
, children
,
557 DEBUG(10, ("create_aio_child failed: %s\n",
563 child
->dont_delete
= true;
570 struct aio_fork_pread_state
{
571 struct aio_child
*child
;
576 static void aio_fork_pread_done(struct tevent_req
*subreq
);
578 static struct tevent_req
*aio_fork_pread_send(struct vfs_handle_struct
*handle
,
580 struct tevent_context
*ev
,
581 struct files_struct
*fsp
,
583 size_t n
, off_t offset
)
585 struct tevent_req
*req
, *subreq
;
586 struct aio_fork_pread_state
*state
;
590 struct aio_fork_config
*config
;
592 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
593 struct aio_fork_config
,
596 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pread_state
);
602 /* TODO: support variable buffers */
603 tevent_req_error(req
, EINVAL
);
604 return tevent_req_post(req
, ev
);
607 err
= get_idle_child(handle
, &state
->child
);
609 tevent_req_error(req
, err
);
610 return tevent_req_post(req
, ev
);
617 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
619 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
620 (int)state
->child
->pid
));
623 * Not making this async. We're writing into an empty unix
624 * domain socket. This should never block.
626 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
631 TALLOC_FREE(state
->child
);
633 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
634 tevent_req_error(req
, err
);
635 return tevent_req_post(req
, ev
);
638 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
639 sizeof(struct rw_ret
), NULL
, NULL
);
640 if (tevent_req_nomem(subreq
, req
)) {
641 TALLOC_FREE(state
->child
); /* we sent sth down */
642 return tevent_req_post(req
, ev
);
644 tevent_req_set_callback(subreq
, aio_fork_pread_done
, req
);
648 static void aio_fork_pread_done(struct tevent_req
*subreq
)
650 struct tevent_req
*req
= tevent_req_callback_data(
651 subreq
, struct tevent_req
);
652 struct aio_fork_pread_state
*state
= tevent_req_data(
653 req
, struct aio_fork_pread_state
);
657 struct rw_ret
*retbuf
;
659 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
662 TALLOC_FREE(state
->child
);
663 tevent_req_error(req
, err
);
667 state
->child
->busy
= false;
669 retbuf
= (struct rw_ret
*)buf
;
670 state
->ret
= retbuf
->size
;
671 state
->err
= retbuf
->ret_errno
;
672 tevent_req_done(req
);
675 static ssize_t
aio_fork_pread_recv(struct tevent_req
*req
, int *err
)
677 struct aio_fork_pread_state
*state
= tevent_req_data(
678 req
, struct aio_fork_pread_state
);
680 if (tevent_req_is_unix_error(req
, err
)) {
683 if (state
->ret
== -1) {
689 struct aio_fork_pwrite_state
{
690 struct aio_child
*child
;
695 static void aio_fork_pwrite_done(struct tevent_req
*subreq
);
697 static struct tevent_req
*aio_fork_pwrite_send(
698 struct vfs_handle_struct
*handle
, TALLOC_CTX
*mem_ctx
,
699 struct tevent_context
*ev
, struct files_struct
*fsp
,
700 const void *data
, size_t n
, off_t offset
)
702 struct tevent_req
*req
, *subreq
;
703 struct aio_fork_pwrite_state
*state
;
707 struct aio_fork_config
*config
;
708 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
709 struct aio_fork_config
,
712 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pwrite_state
);
718 /* TODO: support variable buffers */
719 tevent_req_error(req
, EINVAL
);
720 return tevent_req_post(req
, ev
);
723 err
= get_idle_child(handle
, &state
->child
);
725 tevent_req_error(req
, err
);
726 return tevent_req_post(req
, ev
);
733 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
735 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
736 (int)state
->child
->pid
));
739 * Not making this async. We're writing into an empty unix
740 * domain socket. This should never block.
742 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
747 TALLOC_FREE(state
->child
);
749 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
750 tevent_req_error(req
, err
);
751 return tevent_req_post(req
, ev
);
754 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
755 sizeof(struct rw_ret
), NULL
, NULL
);
756 if (tevent_req_nomem(subreq
, req
)) {
757 TALLOC_FREE(state
->child
); /* we sent sth down */
758 return tevent_req_post(req
, ev
);
760 tevent_req_set_callback(subreq
, aio_fork_pwrite_done
, req
);
764 static void aio_fork_pwrite_done(struct tevent_req
*subreq
)
766 struct tevent_req
*req
= tevent_req_callback_data(
767 subreq
, struct tevent_req
);
768 struct aio_fork_pwrite_state
*state
= tevent_req_data(
769 req
, struct aio_fork_pwrite_state
);
773 struct rw_ret
*retbuf
;
775 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
778 TALLOC_FREE(state
->child
);
779 tevent_req_error(req
, err
);
783 state
->child
->busy
= false;
785 retbuf
= (struct rw_ret
*)buf
;
786 state
->ret
= retbuf
->size
;
787 state
->err
= retbuf
->ret_errno
;
788 tevent_req_done(req
);
791 static ssize_t
aio_fork_pwrite_recv(struct tevent_req
*req
, int *err
)
793 struct aio_fork_pwrite_state
*state
= tevent_req_data(
794 req
, struct aio_fork_pwrite_state
);
796 if (tevent_req_is_unix_error(req
, err
)) {
799 if (state
->ret
== -1) {
805 struct aio_fork_fsync_state
{
806 struct aio_child
*child
;
811 static void aio_fork_fsync_done(struct tevent_req
*subreq
);
813 static struct tevent_req
*aio_fork_fsync_send(
814 struct vfs_handle_struct
*handle
, TALLOC_CTX
*mem_ctx
,
815 struct tevent_context
*ev
, struct files_struct
*fsp
)
817 struct tevent_req
*req
, *subreq
;
818 struct aio_fork_fsync_state
*state
;
822 struct aio_fork_config
*config
;
824 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
825 struct aio_fork_config
,
828 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_fsync_state
);
833 err
= get_idle_child(handle
, &state
->child
);
835 tevent_req_error(req
, err
);
836 return tevent_req_post(req
, ev
);
841 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
843 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
844 (int)state
->child
->pid
));
847 * Not making this async. We're writing into an empty unix
848 * domain socket. This should never block.
850 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
855 TALLOC_FREE(state
->child
);
857 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
858 tevent_req_error(req
, err
);
859 return tevent_req_post(req
, ev
);
862 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
863 sizeof(struct rw_ret
), NULL
, NULL
);
864 if (tevent_req_nomem(subreq
, req
)) {
865 TALLOC_FREE(state
->child
); /* we sent sth down */
866 return tevent_req_post(req
, ev
);
868 tevent_req_set_callback(subreq
, aio_fork_fsync_done
, req
);
872 static void aio_fork_fsync_done(struct tevent_req
*subreq
)
874 struct tevent_req
*req
= tevent_req_callback_data(
875 subreq
, struct tevent_req
);
876 struct aio_fork_fsync_state
*state
= tevent_req_data(
877 req
, struct aio_fork_fsync_state
);
881 struct rw_ret
*retbuf
;
883 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
886 TALLOC_FREE(state
->child
);
887 tevent_req_error(req
, err
);
891 state
->child
->busy
= false;
893 retbuf
= (struct rw_ret
*)buf
;
894 state
->ret
= retbuf
->size
;
895 state
->err
= retbuf
->ret_errno
;
896 tevent_req_done(req
);
899 static int aio_fork_fsync_recv(struct tevent_req
*req
, int *err
)
901 struct aio_fork_fsync_state
*state
= tevent_req_data(
902 req
, struct aio_fork_fsync_state
);
904 if (tevent_req_is_unix_error(req
, err
)) {
907 if (state
->ret
== -1) {
913 static int aio_fork_connect(vfs_handle_struct
*handle
, const char *service
,
917 struct aio_fork_config
*config
;
918 ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
924 config
= talloc_zero(handle
->conn
, struct aio_fork_config
);
926 SMB_VFS_NEXT_DISCONNECT(handle
);
927 DEBUG(0, ("talloc_zero() failed\n"));
931 config
->erratic_testing_mode
= lp_parm_bool(SNUM(handle
->conn
), "vfs_aio_fork",
932 "erratic_testing_mode", false);
934 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
935 NULL
, struct aio_fork_config
,
938 /*********************************************************************
939 * How many threads to initialize ?
940 * 100 per process seems insane as a default until you realize that
941 * (a) Threads terminate after 1 second when idle.
942 * (b) Throttling is done in SMB2 via the crediting algorithm.
943 * (c) SMB1 clients are limited to max_mux (50) outstanding
944 * requests and Windows clients don't use this anyway.
945 * Essentially we want this to be unlimited unless smb.conf
947 *********************************************************************/
948 aio_pending_size
= 100;
952 static struct vfs_fn_pointers vfs_aio_fork_fns
= {
953 .connect_fn
= aio_fork_connect
,
954 .pread_send_fn
= aio_fork_pread_send
,
955 .pread_recv_fn
= aio_fork_pread_recv
,
956 .pwrite_send_fn
= aio_fork_pwrite_send
,
957 .pwrite_recv_fn
= aio_fork_pwrite_recv
,
958 .fsync_send_fn
= aio_fork_fsync_send
,
959 .fsync_recv_fn
= aio_fork_fsync_recv
,
962 NTSTATUS
vfs_aio_fork_init(void);
963 NTSTATUS
vfs_aio_fork_init(void)
965 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
966 "aio_fork", &vfs_aio_fork_fns
);