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"
39 static int mmap_area_destructor(struct mmap_area
*area
)
41 munmap((void *)area
->ptr
, area
->size
);
45 static struct mmap_area
*mmap_area_init(TALLOC_CTX
*mem_ctx
, size_t size
)
47 struct mmap_area
*result
;
50 result
= talloc(mem_ctx
, struct mmap_area
);
52 DEBUG(0, ("talloc failed\n"));
56 fd
= open("/dev/zero", O_RDWR
);
58 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
63 result
->ptr
= mmap(NULL
, size
, PROT_READ
|PROT_WRITE
,
64 MAP_SHARED
|MAP_FILE
, fd
, 0);
65 if (result
->ptr
== MAP_FAILED
) {
66 DEBUG(1, ("mmap failed: %s\n", strerror(errno
)));
73 talloc_set_destructor(result
, mmap_area_destructor
);
87 static const char *cmd_type_str(enum cmd_type cmd
)
116 struct aio_child_list
;
119 struct aio_child
*prev
, *next
;
120 struct aio_child_list
*list
;
123 struct mmap_area
*map
;
124 bool dont_delete
; /* Marked as in use since last cleanup */
128 struct aio_child_list
{
129 struct aio_child
*children
;
130 struct timed_event
*cleanup_event
;
133 static void free_aio_children(void **p
)
138 static ssize_t
read_fd(int fd
, void *ptr
, size_t nbytes
, int *recvfd
)
143 #ifndef HAVE_MSGHDR_MSG_CONTROL
147 #ifdef HAVE_MSGHDR_MSG_CONTROL
150 char control
[CMSG_SPACE(sizeof(int))];
152 struct cmsghdr
*cmptr
;
154 msg
.msg_control
= control_un
.control
;
155 msg
.msg_controllen
= sizeof(control_un
.control
);
157 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
158 msg
.msg_accrights
= (caddr_t
) &newfd
;
159 msg
.msg_accrightslen
= sizeof(int);
161 #error Can not pass file descriptors
169 iov
[0].iov_base
= (void *)ptr
;
170 iov
[0].iov_len
= nbytes
;
174 if ( (n
= recvmsg(fd
, &msg
, 0)) <= 0) {
178 #ifdef HAVE_MSGHDR_MSG_CONTROL
179 if ((cmptr
= CMSG_FIRSTHDR(&msg
)) != NULL
180 && cmptr
->cmsg_len
== CMSG_LEN(sizeof(int))) {
181 if (cmptr
->cmsg_level
!= SOL_SOCKET
) {
182 DEBUG(10, ("control level != SOL_SOCKET"));
186 if (cmptr
->cmsg_type
!= SCM_RIGHTS
) {
187 DEBUG(10, ("control type != SCM_RIGHTS"));
191 memcpy(recvfd
, CMSG_DATA(cmptr
), sizeof(*recvfd
));
193 *recvfd
= -1; /* descriptor was not passed */
196 if (msg
.msg_accrightslen
== sizeof(int)) {
200 *recvfd
= -1; /* descriptor was not passed */
207 static ssize_t
write_fd(int fd
, void *ptr
, size_t nbytes
, int sendfd
)
212 #ifdef HAVE_MSGHDR_MSG_CONTROL
215 char control
[CMSG_SPACE(sizeof(int))];
217 struct cmsghdr
*cmptr
;
220 ZERO_STRUCT(control_un
);
222 msg
.msg_control
= control_un
.control
;
223 msg
.msg_controllen
= sizeof(control_un
.control
);
225 cmptr
= CMSG_FIRSTHDR(&msg
);
226 cmptr
->cmsg_len
= CMSG_LEN(sizeof(int));
227 cmptr
->cmsg_level
= SOL_SOCKET
;
228 cmptr
->cmsg_type
= SCM_RIGHTS
;
229 memcpy(CMSG_DATA(cmptr
), &sendfd
, sizeof(sendfd
));
232 msg
.msg_accrights
= (caddr_t
) &sendfd
;
233 msg
.msg_accrightslen
= sizeof(int);
240 iov
[0].iov_base
= (void *)ptr
;
241 iov
[0].iov_len
= nbytes
;
245 return (sendmsg(fd
, &msg
, 0));
248 static void aio_child_cleanup(struct event_context
*event_ctx
,
249 struct timed_event
*te
,
253 struct aio_child_list
*list
= talloc_get_type_abort(
254 private_data
, struct aio_child_list
);
255 struct aio_child
*child
, *next
;
257 TALLOC_FREE(list
->cleanup_event
);
259 for (child
= list
->children
; child
!= NULL
; child
= next
) {
263 DEBUG(10, ("child %d currently active\n",
268 if (child
->dont_delete
) {
269 DEBUG(10, ("Child %d was active since last cleanup\n",
271 child
->dont_delete
= false;
275 DEBUG(10, ("Child %d idle for more than 30 seconds, "
276 "deleting\n", (int)child
->pid
));
282 if (list
->children
!= NULL
) {
284 * Re-schedule the next cleanup round
286 list
->cleanup_event
= event_add_timed(server_event_context(), list
,
287 timeval_add(&now
, 30, 0),
288 aio_child_cleanup
, list
);
293 static struct aio_child_list
*init_aio_children(struct vfs_handle_struct
*handle
)
295 struct aio_child_list
*data
= NULL
;
297 if (SMB_VFS_HANDLE_TEST_DATA(handle
)) {
298 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct aio_child_list
,
303 data
= talloc_zero(NULL
, struct aio_child_list
);
310 * Regardless of whether the child_list had been around or not, make
311 * sure that we have a cleanup timed event. This timed event will
312 * delete itself when it finds that no children are around anymore.
315 if (data
->cleanup_event
== NULL
) {
316 data
->cleanup_event
= event_add_timed(server_event_context(), data
,
317 timeval_current_ofs(30, 0),
318 aio_child_cleanup
, data
);
319 if (data
->cleanup_event
== NULL
) {
325 if (!SMB_VFS_HANDLE_TEST_DATA(handle
)) {
326 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_aio_children
,
327 struct aio_child_list
, return False
);
333 static void aio_child_loop(int sockfd
, struct mmap_area
*map
)
338 struct rw_cmd cmd_struct
;
339 struct rw_ret ret_struct
;
341 ret
= read_fd(sockfd
, &cmd_struct
, sizeof(cmd_struct
), &fd
);
342 if (ret
!= sizeof(cmd_struct
)) {
343 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret
,
348 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
349 cmd_type_str(cmd_struct
.cmd
),
350 (int)cmd_struct
.n
, (int)cmd_struct
.offset
, fd
));
352 #ifdef ENABLE_BUILD_FARM_HACKS
355 * In the build farm, we want erratic behaviour for
361 * use generate_random_buffer, we just forked from a
362 * common parent state
364 generate_random_buffer(&randval
, sizeof(randval
));
365 msecs
= randval
+ 20;
366 DEBUG(10, ("delaying for %u msecs\n", msecs
));
372 ZERO_STRUCT(ret_struct
);
374 switch (cmd_struct
.cmd
) {
376 ret_struct
.size
= sys_pread(
377 fd
, (void *)map
->ptr
, cmd_struct
.n
,
380 /* This breaks "make test" when run with aio_fork module. */
381 #ifdef ENABLE_BUILD_FARM_HACKS
382 ret_struct
.size
= MAX(1, ret_struct
.size
* 0.9);
387 ret_struct
.size
= sys_pwrite(
388 fd
, (void *)map
->ptr
, cmd_struct
.n
,
392 ret_struct
.size
= -1;
396 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
397 (int)ret_struct
.size
));
399 if (ret_struct
.size
== -1) {
400 ret_struct
.ret_errno
= errno
;
404 * Close the fd before telling our parent we're done. The
405 * parent might close and re-open the file very quickly, and
406 * with system-level share modes (GPFS) we would get an
407 * unjustified SHARING_VIOLATION.
411 ret
= write_data(sockfd
, (char *)&ret_struct
,
413 if (ret
!= sizeof(ret_struct
)) {
414 DEBUG(10, ("could not write ret_struct: %s\n",
421 static int aio_child_destructor(struct aio_child
*child
)
425 SMB_ASSERT(!child
->busy
);
427 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
428 child
->pid
, child
->sockfd
));
431 * closing the sockfd makes the child not return from recvmsg() on RHEL
432 * 5.5 so instead force the child to exit by writing bad data to it
434 write(child
->sockfd
, &c
, sizeof(c
));
435 close(child
->sockfd
);
436 DLIST_REMOVE(child
->list
->children
, child
);
441 * We have to close all fd's in open files, we might incorrectly hold a system
442 * level share mode on a file.
445 static struct files_struct
*close_fsp_fd(struct files_struct
*fsp
,
448 if ((fsp
->fh
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
455 static int create_aio_child(struct smbd_server_connection
*sconn
,
456 struct aio_child_list
*children
,
458 struct aio_child
**presult
)
460 struct aio_child
*result
;
464 fdpair
[0] = fdpair
[1] = -1;
466 result
= talloc_zero(children
, struct aio_child
);
467 if (result
== NULL
) {
471 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) == -1) {
473 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno
)));
477 DEBUG(10, ("fdpair = %d/%d\n", fdpair
[0], fdpair
[1]));
479 result
->map
= mmap_area_init(result
, map_size
);
480 if (result
->map
== NULL
) {
482 DEBUG(0, ("Could not create mmap area\n"));
486 result
->pid
= fork();
487 if (result
->pid
== -1) {
489 DEBUG(0, ("fork failed: %s\n", strerror(errno
)));
493 if (result
->pid
== 0) {
495 result
->sockfd
= fdpair
[1];
496 files_forall(sconn
, close_fsp_fd
, NULL
);
497 aio_child_loop(result
->sockfd
, result
->map
);
500 DEBUG(10, ("Child %d created with sockfd %d\n",
501 result
->pid
, fdpair
[0]));
503 result
->sockfd
= fdpair
[0];
506 result
->list
= children
;
507 DLIST_ADD(children
->children
, result
);
509 talloc_set_destructor(result
, aio_child_destructor
);
516 if (fdpair
[0] != -1) close(fdpair
[0]);
517 if (fdpair
[1] != -1) close(fdpair
[1]);
523 static int get_idle_child(struct vfs_handle_struct
*handle
,
524 struct aio_child
**pchild
)
526 struct aio_child_list
*children
;
527 struct aio_child
*child
;
529 children
= init_aio_children(handle
);
530 if (children
== NULL
) {
534 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
543 DEBUG(10, ("no idle child found, creating new one\n"));
545 ret
= create_aio_child(handle
->conn
->sconn
, children
,
548 DEBUG(10, ("create_aio_child failed: %s\n",
554 child
->dont_delete
= true;
561 struct aio_fork_pread_state
{
562 struct aio_child
*child
;
567 static void aio_fork_pread_done(struct tevent_req
*subreq
);
569 static struct tevent_req
*aio_fork_pread_send(struct vfs_handle_struct
*handle
,
571 struct tevent_context
*ev
,
572 struct files_struct
*fsp
,
574 size_t n
, off_t offset
)
576 struct tevent_req
*req
, *subreq
;
577 struct aio_fork_pread_state
*state
;
582 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pread_state
);
588 /* TODO: support variable buffers */
589 tevent_req_error(req
, EINVAL
);
590 return tevent_req_post(req
, ev
);
593 err
= get_idle_child(handle
, &state
->child
);
595 tevent_req_error(req
, err
);
596 return tevent_req_post(req
, ev
);
604 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
605 (int)state
->child
->pid
));
608 * Not making this async. We're writing into an empty unix
609 * domain socket. This should never block.
611 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
616 TALLOC_FREE(state
->child
);
618 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
619 tevent_req_error(req
, err
);
620 return tevent_req_post(req
, ev
);
623 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
624 sizeof(struct rw_ret
), NULL
, NULL
);
625 if (tevent_req_nomem(subreq
, req
)) {
626 TALLOC_FREE(state
->child
); /* we sent sth down */
627 return tevent_req_post(req
, ev
);
629 tevent_req_set_callback(subreq
, aio_fork_pread_done
, req
);
633 static void aio_fork_pread_done(struct tevent_req
*subreq
)
635 struct tevent_req
*req
= tevent_req_callback_data(
636 subreq
, struct tevent_req
);
637 struct aio_fork_pread_state
*state
= tevent_req_data(
638 req
, struct aio_fork_pread_state
);
642 struct rw_ret
*retbuf
;
644 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
647 TALLOC_FREE(state
->child
);
648 tevent_req_error(req
, err
);
652 state
->child
->busy
= false;
654 retbuf
= (struct rw_ret
*)buf
;
655 state
->ret
= retbuf
->size
;
656 state
->err
= retbuf
->ret_errno
;
657 tevent_req_done(req
);
660 static ssize_t
aio_fork_pread_recv(struct tevent_req
*req
, int *err
)
662 struct aio_fork_pread_state
*state
= tevent_req_data(
663 req
, struct aio_fork_pread_state
);
665 if (tevent_req_is_unix_error(req
, err
)) {
668 if (state
->ret
== -1) {
674 struct aio_fork_pwrite_state
{
675 struct aio_child
*child
;
680 static void aio_fork_pwrite_done(struct tevent_req
*subreq
);
682 static struct tevent_req
*aio_fork_pwrite_send(
683 struct vfs_handle_struct
*handle
, TALLOC_CTX
*mem_ctx
,
684 struct tevent_context
*ev
, struct files_struct
*fsp
,
685 const void *data
, size_t n
, off_t offset
)
687 struct tevent_req
*req
, *subreq
;
688 struct aio_fork_pwrite_state
*state
;
693 req
= tevent_req_create(mem_ctx
, &state
, struct aio_fork_pwrite_state
);
699 /* TODO: support variable buffers */
700 tevent_req_error(req
, EINVAL
);
701 return tevent_req_post(req
, ev
);
704 err
= get_idle_child(handle
, &state
->child
);
706 tevent_req_error(req
, err
);
707 return tevent_req_post(req
, ev
);
715 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
716 (int)state
->child
->pid
));
719 * Not making this async. We're writing into an empty unix
720 * domain socket. This should never block.
722 written
= write_fd(state
->child
->sockfd
, &cmd
, sizeof(cmd
),
727 TALLOC_FREE(state
->child
);
729 DEBUG(10, ("write_fd failed: %s\n", strerror(err
)));
730 tevent_req_error(req
, err
);
731 return tevent_req_post(req
, ev
);
734 subreq
= read_packet_send(state
, ev
, state
->child
->sockfd
,
735 sizeof(struct rw_ret
), NULL
, NULL
);
736 if (tevent_req_nomem(subreq
, req
)) {
737 TALLOC_FREE(state
->child
); /* we sent sth down */
738 return tevent_req_post(req
, ev
);
740 tevent_req_set_callback(subreq
, aio_fork_pwrite_done
, req
);
744 static void aio_fork_pwrite_done(struct tevent_req
*subreq
)
746 struct tevent_req
*req
= tevent_req_callback_data(
747 subreq
, struct tevent_req
);
748 struct aio_fork_pwrite_state
*state
= tevent_req_data(
749 req
, struct aio_fork_pwrite_state
);
753 struct rw_ret
*retbuf
;
755 nread
= read_packet_recv(subreq
, talloc_tos(), &buf
, &err
);
758 TALLOC_FREE(state
->child
);
759 tevent_req_error(req
, err
);
763 state
->child
->busy
= false;
765 retbuf
= (struct rw_ret
*)buf
;
766 state
->ret
= retbuf
->size
;
767 state
->err
= retbuf
->ret_errno
;
768 tevent_req_done(req
);
771 static ssize_t
aio_fork_pwrite_recv(struct tevent_req
*req
, int *err
)
773 struct aio_fork_pwrite_state
*state
= tevent_req_data(
774 req
, struct aio_fork_pwrite_state
);
776 if (tevent_req_is_unix_error(req
, err
)) {
779 if (state
->ret
== -1) {
785 static int aio_fork_connect(vfs_handle_struct
*handle
, const char *service
,
788 /*********************************************************************
789 * How many threads to initialize ?
790 * 100 per process seems insane as a default until you realize that
791 * (a) Threads terminate after 1 second when idle.
792 * (b) Throttling is done in SMB2 via the crediting algorithm.
793 * (c) SMB1 clients are limited to max_mux (50) outstanding
794 * requests and Windows clients don't use this anyway.
795 * Essentially we want this to be unlimited unless smb.conf
797 *********************************************************************/
798 aio_pending_size
= 100;
799 return SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
802 static struct vfs_fn_pointers vfs_aio_fork_fns
= {
803 .connect_fn
= aio_fork_connect
,
804 .pread_send_fn
= aio_fork_pread_send
,
805 .pread_recv_fn
= aio_fork_pread_recv
,
806 .pwrite_send_fn
= aio_fork_pwrite_send
,
807 .pwrite_recv_fn
= aio_fork_pwrite_recv
,
810 NTSTATUS
vfs_aio_fork_init(void);
811 NTSTATUS
vfs_aio_fork_init(void)
813 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
814 "aio_fork", &vfs_aio_fork_fns
);