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"
29 #include "lib/sys_rw.h"
30 #include "lib/sys_rw_data.h"
32 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
33 # error Can not pass file descriptors
42 struct aio_fork_config
{
43 bool erratic_testing_mode
;
51 static int mmap_area_destructor(struct mmap_area
*area
)
53 munmap((void *)area
->ptr
, area
->size
);
57 static struct mmap_area
*mmap_area_init(TALLOC_CTX
*mem_ctx
, size_t size
)
59 struct mmap_area
*result
;
62 result
= talloc(mem_ctx
, struct mmap_area
);
64 DEBUG(0, ("talloc failed\n"));
68 fd
= open("/dev/zero", O_RDWR
);
70 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
75 result
->ptr
= mmap(NULL
, size
, PROT_READ
|PROT_WRITE
,
76 MAP_SHARED
|MAP_FILE
, fd
, 0);
78 if (result
->ptr
== MAP_FAILED
) {
79 DEBUG(1, ("mmap failed: %s\n", strerror(errno
)));
84 talloc_set_destructor(result
, mmap_area_destructor
);
99 static const char *cmd_type_str(enum cmd_type cmd
)
114 result
= "<UNKNOWN>";
124 bool erratic_testing_mode
;
132 struct aio_child_list
;
135 struct aio_child
*prev
, *next
;
136 struct aio_child_list
*list
;
139 struct mmap_area
*map
;
140 bool dont_delete
; /* Marked as in use since last cleanup */
144 struct aio_child_list
{
145 struct aio_child
*children
;
146 struct tevent_timer
*cleanup_event
;
149 static void free_aio_children(void **p
)
154 static ssize_t
read_fd(int fd
, void *ptr
, size_t nbytes
, int *recvfd
)
159 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
163 msg
.msg_accrights
= (caddr_t
) &newfd
;
164 msg
.msg_accrightslen
= sizeof(int);
169 char control
[CMSG_SPACE(sizeof(int))];
171 struct cmsghdr
*cmptr
;
174 ZERO_STRUCT(control_un
);
176 msg
.msg_control
= control_un
.control
;
177 msg
.msg_controllen
= sizeof(control_un
.control
);
183 iov
[0].iov_base
= (void *)ptr
;
184 iov
[0].iov_len
= nbytes
;
188 if ( (n
= recvmsg(fd
, &msg
, 0)) <= 0) {
192 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
193 if ((cmptr
= CMSG_FIRSTHDR(&msg
)) != NULL
194 && cmptr
->cmsg_len
== CMSG_LEN(sizeof(int))) {
195 if (cmptr
->cmsg_level
!= SOL_SOCKET
) {
196 DEBUG(10, ("control level != SOL_SOCKET"));
200 if (cmptr
->cmsg_type
!= SCM_RIGHTS
) {
201 DEBUG(10, ("control type != SCM_RIGHTS"));
205 memcpy(recvfd
, CMSG_DATA(cmptr
), sizeof(*recvfd
));
207 *recvfd
= -1; /* descriptor was not passed */
210 if (msg
.msg_accrightslen
== sizeof(int)) {
214 *recvfd
= -1; /* descriptor was not passed */
221 static ssize_t
write_fd(int fd
, void *ptr
, size_t nbytes
, int sendfd
)
226 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
229 char control
[CMSG_SPACE(sizeof(int))];
231 struct cmsghdr
*cmptr
;
234 ZERO_STRUCT(control_un
);
236 msg
.msg_control
= control_un
.control
;
237 msg
.msg_controllen
= sizeof(control_un
.control
);
239 cmptr
= CMSG_FIRSTHDR(&msg
);
240 cmptr
->cmsg_len
= CMSG_LEN(sizeof(int));
241 cmptr
->cmsg_level
= SOL_SOCKET
;
242 cmptr
->cmsg_type
= SCM_RIGHTS
;
243 memcpy(CMSG_DATA(cmptr
), &sendfd
, sizeof(sendfd
));
246 msg
.msg_accrights
= (caddr_t
) &sendfd
;
247 msg
.msg_accrightslen
= sizeof(int);
254 iov
[0].iov_base
= (void *)ptr
;
255 iov
[0].iov_len
= nbytes
;
259 return (sendmsg(fd
, &msg
, 0));
262 static void aio_child_cleanup(struct tevent_context
*event_ctx
,
263 struct tevent_timer
*te
,
267 struct aio_child_list
*list
= talloc_get_type_abort(
268 private_data
, struct aio_child_list
);
269 struct aio_child
*child
, *next
;
271 TALLOC_FREE(list
->cleanup_event
);
273 for (child
= list
->children
; child
!= NULL
; child
= next
) {
277 DEBUG(10, ("child %d currently active\n",
282 if (child
->dont_delete
) {
283 DEBUG(10, ("Child %d was active since last cleanup\n",
285 child
->dont_delete
= false;
289 DEBUG(10, ("Child %d idle for more than 30 seconds, "
290 "deleting\n", (int)child
->pid
));
296 if (list
->children
!= NULL
) {
298 * Re-schedule the next cleanup round
300 list
->cleanup_event
= tevent_add_timer(server_event_context(), list
,
301 timeval_add(&now
, 30, 0),
302 aio_child_cleanup
, list
);
307 static struct aio_child_list
*init_aio_children(struct vfs_handle_struct
*handle
)
309 struct aio_child_list
*data
= NULL
;
311 if (SMB_VFS_HANDLE_TEST_DATA(handle
)) {
312 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct aio_child_list
,
317 data
= talloc_zero(NULL
, struct aio_child_list
);
324 * Regardless of whether the child_list had been around or not, make
325 * sure that we have a cleanup timed event. This timed event will
326 * delete itself when it finds that no children are around anymore.
329 if (data
->cleanup_event
== NULL
) {
330 data
->cleanup_event
= tevent_add_timer(server_event_context(), data
,
331 timeval_current_ofs(30, 0),
332 aio_child_cleanup
, data
);
333 if (data
->cleanup_event
== NULL
) {
339 if (!SMB_VFS_HANDLE_TEST_DATA(handle
)) {
340 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_aio_children
,
341 struct aio_child_list
, return False
);
347 static void aio_child_loop(int sockfd
, struct mmap_area
*map
)
352 struct rw_cmd cmd_struct
;
353 struct rw_ret ret_struct
;
355 ret
= read_fd(sockfd
, &cmd_struct
, sizeof(cmd_struct
), &fd
);
356 if (ret
!= sizeof(cmd_struct
)) {
357 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret
,
362 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
363 cmd_type_str(cmd_struct
.cmd
),
364 (int)cmd_struct
.n
, (int)cmd_struct
.offset
, fd
));
366 if (cmd_struct
.erratic_testing_mode
) {
368 * For developer testing, we want erratic behaviour for
374 * use generate_random_buffer, we just forked from a
375 * common parent state
377 generate_random_buffer(&randval
, sizeof(randval
));
378 msecs
= randval
+ 20;
379 DEBUG(10, ("delaying for %u msecs\n", msecs
));
383 ZERO_STRUCT(ret_struct
);
385 switch (cmd_struct
.cmd
) {
387 ret_struct
.size
= sys_pread(
388 fd
, (void *)map
->ptr
, cmd_struct
.n
,
391 /* This breaks "make test" when run with aio_fork module. */
393 ret_struct
.size
= MAX(1, ret_struct
.size
* 0.9);
398 ret_struct
.size
= sys_pwrite(
399 fd
, (void *)map
->ptr
, cmd_struct
.n
,
403 ret_struct
.size
= fsync(fd
);
406 ret_struct
.size
= -1;
410 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
411 (int)ret_struct
.size
));
413 if (ret_struct
.size
== -1) {
414 ret_struct
.ret_errno
= errno
;
418 * Close the fd before telling our parent we're done. The
419 * parent might close and re-open the file very quickly, and
420 * with system-level share modes (GPFS) we would get an
421 * unjustified SHARING_VIOLATION.
425 ret
= write_data(sockfd
, (char *)&ret_struct
,
427 if (ret
!= sizeof(ret_struct
)) {
428 DEBUG(10, ("could not write ret_struct: %s\n",
435 static int aio_child_destructor(struct aio_child
*child
)
439 SMB_ASSERT(!child
->busy
);
441 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
442 child
->pid
, child
->sockfd
));
445 * closing the sockfd makes the child not return from recvmsg() on RHEL
446 * 5.5 so instead force the child to exit by writing bad data to it
448 write(child
->sockfd
, &c
, sizeof(c
));
449 close(child
->sockfd
);
450 DLIST_REMOVE(child
->list
->children
, child
);
455 * We have to close all fd's in open files, we might incorrectly hold a system
456 * level share mode on a file.
459 static struct files_struct
*close_fsp_fd(struct files_struct
*fsp
,
462 if ((fsp
->fh
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
469 static int create_aio_child(struct smbd_server_connection
*sconn
,
470 struct aio_child_list
*children
,
472 struct aio_child
**presult
)
474 struct aio_child
*result
;
478 fdpair
[0] = fdpair
[1] = -1;
480 result
= talloc_zero(children
, struct aio_child
);
481 if (result
== NULL
) {
485 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) == -1) {
487 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno
)));
491 DEBUG(10, ("fdpair = %d/%d\n", fdpair
[0], fdpair
[1]));
493 result
->map
= mmap_area_init(result
, map_size
);
494 if (result
->map
== NULL
) {
496 DEBUG(0, ("Could not create mmap area\n"));
500 result
->pid
= fork();
501 if (result
->pid
== -1) {
503 DEBUG(0, ("fork failed: %s\n", strerror(errno
)));
507 if (result
->pid
== 0) {
509 result
->sockfd
= fdpair
[1];
510 files_forall(sconn
, close_fsp_fd
, NULL
);
511 aio_child_loop(result
->sockfd
, result
->map
);
514 DEBUG(10, ("Child %d created with sockfd %d\n",
515 result
->pid
, fdpair
[0]));
517 result
->sockfd
= fdpair
[0];
520 result
->list
= children
;
521 DLIST_ADD(children
->children
, result
);
523 talloc_set_destructor(result
, aio_child_destructor
);
530 if (fdpair
[0] != -1) close(fdpair
[0]);
531 if (fdpair
[1] != -1) close(fdpair
[1]);
537 static int get_idle_child(struct vfs_handle_struct
*handle
,
538 struct aio_child
**pchild
)
540 struct aio_child_list
*children
;
541 struct aio_child
*child
;
543 children
= init_aio_children(handle
);
544 if (children
== NULL
) {
548 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
557 DEBUG(10, ("no idle child found, creating new one\n"));
559 ret
= create_aio_child(handle
->conn
->sconn
, children
,
562 DEBUG(10, ("create_aio_child failed: %s\n",
568 child
->dont_delete
= true;
575 struct aio_fork_pread_state
{
576 struct aio_child
*child
;
581 static void aio_fork_pread_done(struct tevent_req
*subreq
);
583 static struct tevent_req
*aio_fork_pread_send(struct vfs_handle_struct
*handle
,
585 struct tevent_context
*ev
,
586 struct files_struct
*fsp
,
588 size_t n
, off_t offset
)
590 struct tevent_req
*req
, *subreq
;
591 struct aio_fork_pread_state
*state
;
595 struct aio_fork_config
*config
;
597 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
598 struct aio_fork_config
,
601 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pread_state
);
607 /* TODO: support variable buffers */
608 tevent_req_error(req
, EINVAL
);
609 return tevent_req_post(req
, ev
);
612 err
= get_idle_child(handle
, &state
->child
);
614 tevent_req_error(req
, err
);
615 return tevent_req_post(req
, ev
);
622 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
624 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
625 (int)state
->child
->pid
));
628 * Not making this async. We're writing into an empty unix
629 * domain socket. This should never block.
631 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
636 TALLOC_FREE(state
->child
);
638 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
639 tevent_req_error(req
, err
);
640 return tevent_req_post(req
, ev
);
643 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
644 sizeof(struct rw_ret
), NULL
, NULL
);
645 if (tevent_req_nomem(subreq
, req
)) {
646 TALLOC_FREE(state
->child
); /* we sent sth down */
647 return tevent_req_post(req
, ev
);
649 tevent_req_set_callback(subreq
, aio_fork_pread_done
, req
);
653 static void aio_fork_pread_done(struct tevent_req
*subreq
)
655 struct tevent_req
*req
= tevent_req_callback_data(
656 subreq
, struct tevent_req
);
657 struct aio_fork_pread_state
*state
= tevent_req_data(
658 req
, struct aio_fork_pread_state
);
662 struct rw_ret
*retbuf
;
664 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
667 TALLOC_FREE(state
->child
);
668 tevent_req_error(req
, err
);
672 state
->child
->busy
= false;
674 retbuf
= (struct rw_ret
*)buf
;
675 state
->ret
= retbuf
->size
;
676 state
->err
= retbuf
->ret_errno
;
677 tevent_req_done(req
);
680 static ssize_t
aio_fork_pread_recv(struct tevent_req
*req
, int *err
)
682 struct aio_fork_pread_state
*state
= tevent_req_data(
683 req
, struct aio_fork_pread_state
);
685 if (tevent_req_is_unix_error(req
, err
)) {
688 if (state
->ret
== -1) {
694 struct aio_fork_pwrite_state
{
695 struct aio_child
*child
;
700 static void aio_fork_pwrite_done(struct tevent_req
*subreq
);
702 static struct tevent_req
*aio_fork_pwrite_send(
703 struct vfs_handle_struct
*handle
, TALLOC_CTX
*mem_ctx
,
704 struct tevent_context
*ev
, struct files_struct
*fsp
,
705 const void *data
, size_t n
, off_t offset
)
707 struct tevent_req
*req
, *subreq
;
708 struct aio_fork_pwrite_state
*state
;
712 struct aio_fork_config
*config
;
713 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
714 struct aio_fork_config
,
717 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pwrite_state
);
723 /* TODO: support variable buffers */
724 tevent_req_error(req
, EINVAL
);
725 return tevent_req_post(req
, ev
);
728 err
= get_idle_child(handle
, &state
->child
);
730 tevent_req_error(req
, err
);
731 return tevent_req_post(req
, ev
);
738 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
740 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
741 (int)state
->child
->pid
));
744 * Not making this async. We're writing into an empty unix
745 * domain socket. This should never block.
747 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
752 TALLOC_FREE(state
->child
);
754 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
755 tevent_req_error(req
, err
);
756 return tevent_req_post(req
, ev
);
759 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
760 sizeof(struct rw_ret
), NULL
, NULL
);
761 if (tevent_req_nomem(subreq
, req
)) {
762 TALLOC_FREE(state
->child
); /* we sent sth down */
763 return tevent_req_post(req
, ev
);
765 tevent_req_set_callback(subreq
, aio_fork_pwrite_done
, req
);
769 static void aio_fork_pwrite_done(struct tevent_req
*subreq
)
771 struct tevent_req
*req
= tevent_req_callback_data(
772 subreq
, struct tevent_req
);
773 struct aio_fork_pwrite_state
*state
= tevent_req_data(
774 req
, struct aio_fork_pwrite_state
);
778 struct rw_ret
*retbuf
;
780 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
783 TALLOC_FREE(state
->child
);
784 tevent_req_error(req
, err
);
788 state
->child
->busy
= false;
790 retbuf
= (struct rw_ret
*)buf
;
791 state
->ret
= retbuf
->size
;
792 state
->err
= retbuf
->ret_errno
;
793 tevent_req_done(req
);
796 static ssize_t
aio_fork_pwrite_recv(struct tevent_req
*req
, int *err
)
798 struct aio_fork_pwrite_state
*state
= tevent_req_data(
799 req
, struct aio_fork_pwrite_state
);
801 if (tevent_req_is_unix_error(req
, err
)) {
804 if (state
->ret
== -1) {
810 struct aio_fork_fsync_state
{
811 struct aio_child
*child
;
816 static void aio_fork_fsync_done(struct tevent_req
*subreq
);
818 static struct tevent_req
*aio_fork_fsync_send(
819 struct vfs_handle_struct
*handle
, TALLOC_CTX
*mem_ctx
,
820 struct tevent_context
*ev
, struct files_struct
*fsp
)
822 struct tevent_req
*req
, *subreq
;
823 struct aio_fork_fsync_state
*state
;
827 struct aio_fork_config
*config
;
829 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
830 struct aio_fork_config
,
833 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_fsync_state
);
838 err
= get_idle_child(handle
, &state
->child
);
840 tevent_req_error(req
, err
);
841 return tevent_req_post(req
, ev
);
846 cmd
.erratic_testing_mode
= config
->erratic_testing_mode
;
848 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
849 (int)state
->child
->pid
));
852 * Not making this async. We're writing into an empty unix
853 * domain socket. This should never block.
855 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
860 TALLOC_FREE(state
->child
);
862 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
863 tevent_req_error(req
, err
);
864 return tevent_req_post(req
, ev
);
867 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
868 sizeof(struct rw_ret
), NULL
, NULL
);
869 if (tevent_req_nomem(subreq
, req
)) {
870 TALLOC_FREE(state
->child
); /* we sent sth down */
871 return tevent_req_post(req
, ev
);
873 tevent_req_set_callback(subreq
, aio_fork_fsync_done
, req
);
877 static void aio_fork_fsync_done(struct tevent_req
*subreq
)
879 struct tevent_req
*req
= tevent_req_callback_data(
880 subreq
, struct tevent_req
);
881 struct aio_fork_fsync_state
*state
= tevent_req_data(
882 req
, struct aio_fork_fsync_state
);
886 struct rw_ret
*retbuf
;
888 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
891 TALLOC_FREE(state
->child
);
892 tevent_req_error(req
, err
);
896 state
->child
->busy
= false;
898 retbuf
= (struct rw_ret
*)buf
;
899 state
->ret
= retbuf
->size
;
900 state
->err
= retbuf
->ret_errno
;
901 tevent_req_done(req
);
904 static int aio_fork_fsync_recv(struct tevent_req
*req
, int *err
)
906 struct aio_fork_fsync_state
*state
= tevent_req_data(
907 req
, struct aio_fork_fsync_state
);
909 if (tevent_req_is_unix_error(req
, err
)) {
912 if (state
->ret
== -1) {
918 static int aio_fork_connect(vfs_handle_struct
*handle
, const char *service
,
922 struct aio_fork_config
*config
;
923 ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
929 config
= talloc_zero(handle
->conn
, struct aio_fork_config
);
931 SMB_VFS_NEXT_DISCONNECT(handle
);
932 DEBUG(0, ("talloc_zero() failed\n"));
936 config
->erratic_testing_mode
= lp_parm_bool(SNUM(handle
->conn
), "vfs_aio_fork",
937 "erratic_testing_mode", false);
939 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
940 NULL
, struct aio_fork_config
,
943 /*********************************************************************
944 * How many threads to initialize ?
945 * 100 per process seems insane as a default until you realize that
946 * (a) Threads terminate after 1 second when idle.
947 * (b) Throttling is done in SMB2 via the crediting algorithm.
948 * (c) SMB1 clients are limited to max_mux (50) outstanding
949 * requests and Windows clients don't use this anyway.
950 * Essentially we want this to be unlimited unless smb.conf
952 *********************************************************************/
953 aio_pending_size
= 100;
957 static struct vfs_fn_pointers vfs_aio_fork_fns
= {
958 .connect_fn
= aio_fork_connect
,
959 .pread_send_fn
= aio_fork_pread_send
,
960 .pread_recv_fn
= aio_fork_pread_recv
,
961 .pwrite_send_fn
= aio_fork_pwrite_send
,
962 .pwrite_recv_fn
= aio_fork_pwrite_recv
,
963 .fsync_send_fn
= aio_fork_fsync_send
,
964 .fsync_recv_fn
= aio_fork_fsync_recv
,
967 NTSTATUS
vfs_aio_fork_init(void);
968 NTSTATUS
vfs_aio_fork_init(void)
970 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
971 "aio_fork", &vfs_aio_fork_fns
);