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"
36 static int mmap_area_destructor(struct mmap_area
*area
)
38 munmap((void *)area
->ptr
, area
->size
);
42 static struct mmap_area
*mmap_area_init(TALLOC_CTX
*mem_ctx
, size_t size
)
44 struct mmap_area
*result
;
47 result
= talloc(mem_ctx
, struct mmap_area
);
49 DEBUG(0, ("talloc failed\n"));
53 fd
= open("/dev/zero", O_RDWR
);
55 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
60 result
->ptr
= mmap(NULL
, size
, PROT_READ
|PROT_WRITE
,
61 MAP_SHARED
|MAP_FILE
, fd
, 0);
62 if (result
->ptr
== MAP_FAILED
) {
63 DEBUG(1, ("mmap failed: %s\n", strerror(errno
)));
70 talloc_set_destructor(result
, mmap_area_destructor
);
90 struct aio_child_list
;
93 struct aio_child
*prev
, *next
;
94 struct aio_child_list
*list
;
95 SMB_STRUCT_AIOCB
*aiocb
;
98 struct fd_event
*sock_event
;
100 struct mmap_area
*map
; /* ==NULL means write request */
101 bool dont_delete
; /* Marked as in use since last cleanup */
104 bool called_from_suspend
;
105 bool completion_done
;
108 struct aio_child_list
{
109 struct aio_child
*children
;
110 struct timed_event
*cleanup_event
;
113 static void free_aio_children(void **p
)
118 static ssize_t
read_fd(int fd
, void *ptr
, size_t nbytes
, int *recvfd
)
123 #ifndef HAVE_MSGHDR_MSG_CONTROL
127 #ifdef HAVE_MSGHDR_MSG_CONTROL
130 char control
[CMSG_SPACE(sizeof(int))];
132 struct cmsghdr
*cmptr
;
134 msg
.msg_control
= control_un
.control
;
135 msg
.msg_controllen
= sizeof(control_un
.control
);
137 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
138 msg
.msg_accrights
= (caddr_t
) &newfd
;
139 msg
.msg_accrightslen
= sizeof(int);
141 #error Can not pass file descriptors
148 iov
[0].iov_base
= (void *)ptr
;
149 iov
[0].iov_len
= nbytes
;
153 if ( (n
= recvmsg(fd
, &msg
, 0)) <= 0) {
157 #ifdef HAVE_MSGHDR_MSG_CONTROL
158 if ((cmptr
= CMSG_FIRSTHDR(&msg
)) != NULL
159 && cmptr
->cmsg_len
== CMSG_LEN(sizeof(int))) {
160 if (cmptr
->cmsg_level
!= SOL_SOCKET
) {
161 DEBUG(10, ("control level != SOL_SOCKET"));
165 if (cmptr
->cmsg_type
!= SCM_RIGHTS
) {
166 DEBUG(10, ("control type != SCM_RIGHTS"));
170 *recvfd
= *((int *) CMSG_DATA(cmptr
));
172 *recvfd
= -1; /* descriptor was not passed */
175 if (msg
.msg_accrightslen
== sizeof(int)) {
179 *recvfd
= -1; /* descriptor was not passed */
186 static ssize_t
write_fd(int fd
, void *ptr
, size_t nbytes
, int sendfd
)
191 #ifdef HAVE_MSGHDR_MSG_CONTROL
194 char control
[CMSG_SPACE(sizeof(int))];
196 struct cmsghdr
*cmptr
;
199 ZERO_STRUCT(control_un
);
201 msg
.msg_control
= control_un
.control
;
202 msg
.msg_controllen
= sizeof(control_un
.control
);
204 cmptr
= CMSG_FIRSTHDR(&msg
);
205 cmptr
->cmsg_len
= CMSG_LEN(sizeof(int));
206 cmptr
->cmsg_level
= SOL_SOCKET
;
207 cmptr
->cmsg_type
= SCM_RIGHTS
;
208 *((int *) CMSG_DATA(cmptr
)) = sendfd
;
211 msg
.msg_accrights
= (caddr_t
) &sendfd
;
212 msg
.msg_accrightslen
= sizeof(int);
219 iov
[0].iov_base
= (void *)ptr
;
220 iov
[0].iov_len
= nbytes
;
224 return (sendmsg(fd
, &msg
, 0));
227 static void aio_child_cleanup(struct event_context
*event_ctx
,
228 struct timed_event
*te
,
232 struct aio_child_list
*list
= talloc_get_type_abort(
233 private_data
, struct aio_child_list
);
234 struct aio_child
*child
, *next
;
236 TALLOC_FREE(list
->cleanup_event
);
238 for (child
= list
->children
; child
!= NULL
; child
= next
) {
241 if (child
->aiocb
!= NULL
) {
242 DEBUG(10, ("child %d currently active\n",
247 if (child
->dont_delete
) {
248 DEBUG(10, ("Child %d was active since last cleanup\n",
250 child
->dont_delete
= false;
254 DEBUG(10, ("Child %d idle for more than 30 seconds, "
255 "deleting\n", (int)child
->pid
));
261 if (list
->children
!= NULL
) {
263 * Re-schedule the next cleanup round
265 list
->cleanup_event
= event_add_timed(smbd_event_context(), list
,
266 timeval_add(&now
, 30, 0),
267 aio_child_cleanup
, list
);
272 static struct aio_child_list
*init_aio_children(struct vfs_handle_struct
*handle
)
274 struct aio_child_list
*data
= NULL
;
276 if (SMB_VFS_HANDLE_TEST_DATA(handle
)) {
277 SMB_VFS_HANDLE_GET_DATA(handle
, data
, struct aio_child_list
,
282 data
= TALLOC_ZERO_P(NULL
, struct aio_child_list
);
289 * Regardless of whether the child_list had been around or not, make
290 * sure that we have a cleanup timed event. This timed event will
291 * delete itself when it finds that no children are around anymore.
294 if (data
->cleanup_event
== NULL
) {
295 data
->cleanup_event
= event_add_timed(smbd_event_context(), data
,
296 timeval_current_ofs(30, 0),
297 aio_child_cleanup
, data
);
298 if (data
->cleanup_event
== NULL
) {
304 if (!SMB_VFS_HANDLE_TEST_DATA(handle
)) {
305 SMB_VFS_HANDLE_SET_DATA(handle
, data
, free_aio_children
,
306 struct aio_child_list
, return False
);
312 static void aio_child_loop(int sockfd
, struct mmap_area
*map
)
317 struct rw_cmd cmd_struct
;
318 struct rw_ret ret_struct
;
320 ret
= read_fd(sockfd
, &cmd_struct
, sizeof(cmd_struct
), &fd
);
321 if (ret
!= sizeof(cmd_struct
)) {
322 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret
,
327 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
328 cmd_struct
.read_cmd
? "read" : "write",
329 (int)cmd_struct
.n
, (int)cmd_struct
.offset
, fd
));
331 #ifdef ENABLE_BUILD_FARM_HACKS
334 * In the build farm, we want erratic behaviour for
340 * use generate_random_buffer, we just forked from a
341 * common parent state
343 generate_random_buffer(&randval
, sizeof(randval
));
344 msecs
= randval
+ 20;
345 DEBUG(10, ("delaying for %u msecs\n", msecs
));
351 ZERO_STRUCT(ret_struct
);
353 if (cmd_struct
.read_cmd
) {
354 ret_struct
.size
= sys_pread(
355 fd
, (void *)map
->ptr
, cmd_struct
.n
,
358 /* This breaks "make test" when run with aio_fork module. */
359 #ifdef ENABLE_BUILD_FARM_HACKS
360 ret_struct
.size
= MAX(1, ret_struct
.size
* 0.9);
365 ret_struct
.size
= sys_pwrite(
366 fd
, (void *)map
->ptr
, cmd_struct
.n
,
370 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
371 (int)ret_struct
.size
));
373 if (ret_struct
.size
== -1) {
374 ret_struct
.ret_errno
= errno
;
378 * Close the fd before telling our parent we're done. The
379 * parent might close and re-open the file very quickly, and
380 * with system-level share modes (GPFS) we would get an
381 * unjustified SHARING_VIOLATION.
385 ret
= write_data(sockfd
, (char *)&ret_struct
,
387 if (ret
!= sizeof(ret_struct
)) {
388 DEBUG(10, ("could not write ret_struct: %s\n",
395 static void handle_aio_completion(struct event_context
*event_ctx
,
396 struct fd_event
*event
, uint16 flags
,
399 struct aio_extra
*aio_ex
= NULL
;
400 struct aio_child
*child
= (struct aio_child
*)p
;
403 DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags
));
405 if ((flags
& EVENT_FD_READ
) == 0) {
409 status
= read_data(child
->sockfd
, (char *)&child
->retval
,
410 sizeof(child
->retval
));
412 if (!NT_STATUS_IS_OK(status
)) {
413 DEBUG(1, ("aio child %d died: %s\n", (int)child
->pid
,
415 child
->retval
.size
= -1;
416 child
->retval
.ret_errno
= EIO
;
419 if (child
->aiocb
== NULL
) {
420 DEBUG(1, ("Inactive child died\n"));
425 if (child
->cancelled
) {
427 child
->cancelled
= false;
431 if (child
->read_cmd
&& (child
->retval
.size
> 0)) {
432 SMB_ASSERT(child
->retval
.size
<= child
->aiocb
->aio_nbytes
);
433 memcpy((void *)child
->aiocb
->aio_buf
, (void *)child
->map
->ptr
,
437 if (child
->called_from_suspend
) {
438 child
->completion_done
= true;
441 aio_ex
= (struct aio_extra
*)child
->aiocb
->aio_sigevent
.sigev_value
.sival_ptr
;
442 smbd_aio_complete_aio_ex(aio_ex
);
446 static int aio_child_destructor(struct aio_child
*child
)
450 SMB_ASSERT((child
->aiocb
== NULL
) || child
->cancelled
);
452 DEBUG(10, ("aio_child_destructor: removing child %d on fd %d\n",
453 child
->pid
, child
->sockfd
));
456 * closing the sockfd makes the child not return from recvmsg() on RHEL
457 * 5.5 so instead force the child to exit by writing bad data to it
459 write(child
->sockfd
, &c
, sizeof(c
));
460 close(child
->sockfd
);
461 DLIST_REMOVE(child
->list
->children
, child
);
466 * We have to close all fd's in open files, we might incorrectly hold a system
467 * level share mode on a file.
470 static struct files_struct
*close_fsp_fd(struct files_struct
*fsp
,
473 if ((fsp
->fh
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
480 static NTSTATUS
create_aio_child(struct smbd_server_connection
*sconn
,
481 struct aio_child_list
*children
,
483 struct aio_child
**presult
)
485 struct aio_child
*result
;
489 fdpair
[0] = fdpair
[1] = -1;
491 result
= TALLOC_ZERO_P(children
, struct aio_child
);
492 NT_STATUS_HAVE_NO_MEMORY(result
);
494 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) == -1) {
495 status
= map_nt_error_from_unix(errno
);
496 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno
)));
500 DEBUG(10, ("fdpair = %d/%d\n", fdpair
[0], fdpair
[1]));
502 result
->map
= mmap_area_init(result
, map_size
);
503 if (result
->map
== NULL
) {
504 status
= map_nt_error_from_unix(errno
);
505 DEBUG(0, ("Could not create mmap area\n"));
509 result
->pid
= sys_fork();
510 if (result
->pid
== -1) {
511 status
= map_nt_error_from_unix(errno
);
512 DEBUG(0, ("fork failed: %s\n", strerror(errno
)));
516 if (result
->pid
== 0) {
518 result
->sockfd
= fdpair
[1];
519 files_forall(sconn
, close_fsp_fd
, NULL
);
520 aio_child_loop(result
->sockfd
, result
->map
);
523 DEBUG(10, ("Child %d created with sockfd %d\n",
524 result
->pid
, fdpair
[0]));
526 result
->sockfd
= fdpair
[0];
529 result
->sock_event
= event_add_fd(smbd_event_context(), result
,
530 result
->sockfd
, EVENT_FD_READ
,
531 handle_aio_completion
,
533 if (result
->sock_event
== NULL
) {
534 status
= NT_STATUS_NO_MEMORY
;
535 DEBUG(0, ("event_add_fd failed\n"));
539 result
->list
= children
;
540 DLIST_ADD(children
->children
, result
);
542 talloc_set_destructor(result
, aio_child_destructor
);
549 if (fdpair
[0] != -1) close(fdpair
[0]);
550 if (fdpair
[1] != -1) close(fdpair
[1]);
556 static NTSTATUS
get_idle_child(struct vfs_handle_struct
*handle
,
557 struct aio_child
**pchild
)
559 struct aio_child_list
*children
;
560 struct aio_child
*child
;
563 children
= init_aio_children(handle
);
564 if (children
== NULL
) {
565 return NT_STATUS_NO_MEMORY
;
568 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
569 if (child
->aiocb
== NULL
) {
576 DEBUG(10, ("no idle child found, creating new one\n"));
578 status
= create_aio_child(handle
->conn
->sconn
, children
,
580 if (!NT_STATUS_IS_OK(status
)) {
581 DEBUG(10, ("create_aio_child failed: %s\n",
587 child
->dont_delete
= true;
593 static int aio_fork_read(struct vfs_handle_struct
*handle
,
594 struct files_struct
*fsp
, SMB_STRUCT_AIOCB
*aiocb
)
596 struct aio_child
*child
;
601 if (aiocb
->aio_nbytes
> 128*1024) {
602 /* TODO: support variable buffers */
607 status
= get_idle_child(handle
, &child
);
608 if (!NT_STATUS_IS_OK(status
)) {
609 DEBUG(10, ("Could not get an idle child\n"));
613 child
->read_cmd
= true;
614 child
->aiocb
= aiocb
;
615 child
->retval
.ret_errno
= EINPROGRESS
;
618 cmd
.n
= aiocb
->aio_nbytes
;
619 cmd
.offset
= aiocb
->aio_offset
;
620 cmd
.read_cmd
= child
->read_cmd
;
622 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
625 ret
= write_fd(child
->sockfd
, &cmd
, sizeof(cmd
), fsp
->fh
->fd
);
627 DEBUG(10, ("write_fd failed: %s\n", strerror(errno
)));
634 static int aio_fork_write(struct vfs_handle_struct
*handle
,
635 struct files_struct
*fsp
, SMB_STRUCT_AIOCB
*aiocb
)
637 struct aio_child
*child
;
642 if (aiocb
->aio_nbytes
> 128*1024) {
643 /* TODO: support variable buffers */
648 status
= get_idle_child(handle
, &child
);
649 if (!NT_STATUS_IS_OK(status
)) {
650 DEBUG(10, ("Could not get an idle child\n"));
654 child
->read_cmd
= false;
655 child
->aiocb
= aiocb
;
656 child
->retval
.ret_errno
= EINPROGRESS
;
658 memcpy((void *)child
->map
->ptr
, (void *)aiocb
->aio_buf
,
662 cmd
.n
= aiocb
->aio_nbytes
;
663 cmd
.offset
= aiocb
->aio_offset
;
664 cmd
.read_cmd
= child
->read_cmd
;
666 DEBUG(10, ("sending fd %d to child %d\n", fsp
->fh
->fd
,
669 ret
= write_fd(child
->sockfd
, &cmd
, sizeof(cmd
), fsp
->fh
->fd
);
671 DEBUG(10, ("write_fd failed: %s\n", strerror(errno
)));
678 static struct aio_child
*aio_fork_find_child(struct vfs_handle_struct
*handle
,
679 SMB_STRUCT_AIOCB
*aiocb
)
681 struct aio_child_list
*children
;
682 struct aio_child
*child
;
684 children
= init_aio_children(handle
);
685 if (children
== NULL
) {
689 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
690 if (child
->aiocb
== aiocb
) {
698 static ssize_t
aio_fork_return_fn(struct vfs_handle_struct
*handle
,
699 struct files_struct
*fsp
,
700 SMB_STRUCT_AIOCB
*aiocb
)
702 struct aio_child
*child
= aio_fork_find_child(handle
, aiocb
);
706 DEBUG(0, ("returning EINVAL\n"));
712 if (child
->retval
.size
== -1) {
713 errno
= child
->retval
.ret_errno
;
716 return child
->retval
.size
;
719 static int aio_fork_cancel(struct vfs_handle_struct
*handle
,
720 struct files_struct
*fsp
,
721 SMB_STRUCT_AIOCB
*aiocb
)
723 struct aio_child_list
*children
;
724 struct aio_child
*child
;
726 children
= init_aio_children(handle
);
727 if (children
== NULL
) {
732 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
733 if (child
->aiocb
== NULL
) {
736 if (child
->aiocb
->aio_fildes
!= fsp
->fh
->fd
) {
739 if ((aiocb
!= NULL
) && (child
->aiocb
!= aiocb
)) {
744 * We let the child do its job, but we discard the result when
748 child
->cancelled
= true;
754 static int aio_fork_error_fn(struct vfs_handle_struct
*handle
,
755 struct files_struct
*fsp
,
756 SMB_STRUCT_AIOCB
*aiocb
)
758 struct aio_child
*child
= aio_fork_find_child(handle
, aiocb
);
765 return child
->retval
.ret_errno
;
768 static void aio_fork_suspend_timed_out(struct tevent_context
*event_ctx
,
769 struct tevent_timer
*te
,
773 bool *timed_out
= (bool *)private_data
;
774 /* Remove this timed event handler. */
779 static int aio_fork_suspend(struct vfs_handle_struct
*handle
,
780 struct files_struct
*fsp
,
781 const SMB_STRUCT_AIOCB
* const aiocb_array
[],
783 const struct timespec
*timeout
)
785 struct aio_child_list
*children
= NULL
;
786 TALLOC_CTX
*frame
= talloc_stackframe();
787 struct event_context
*ev
= NULL
;
790 bool timed_out
= false;
792 children
= init_aio_children(handle
);
793 if (children
== NULL
) {
798 /* This is a blocking call, and has to use a sub-event loop. */
799 ev
= event_context_init(frame
);
806 struct timeval tv
= convert_timespec_to_timeval(*timeout
);
807 struct tevent_timer
*te
= tevent_add_timer(ev
,
809 timeval_current_ofs(tv
.tv_sec
,
811 aio_fork_suspend_timed_out
,
819 for (i
= 0; i
< n
; i
++) {
820 struct aio_child
*child
= NULL
;
821 const SMB_STRUCT_AIOCB
*aiocb
= aiocb_array
[i
];
828 * We're going to cheat here. We know that smbd/aio.c
829 * only calls this when it's waiting for every single
830 * outstanding call to finish on a close, so just wait
831 * individually for each IO to complete. We don't care
832 * what order they finish - only that they all do. JRA.
835 for (child
= children
->children
; child
!= NULL
; child
= child
->next
) {
836 struct tevent_fd
*event
;
838 if (child
->aiocb
== NULL
) {
841 if (child
->aiocb
->aio_fildes
!= fsp
->fh
->fd
) {
844 if (child
->aiocb
!= aiocb
) {
848 if (child
->aiocb
->aio_sigevent
.sigev_value
.sival_ptr
== NULL
) {
852 event
= event_add_fd(ev
,
856 handle_aio_completion
,
859 child
->called_from_suspend
= true;
861 while (!child
->completion_done
) {
862 if (tevent_loop_once(ev
) == -1) {
882 static struct vfs_fn_pointers vfs_aio_fork_fns
= {
883 .aio_read
= aio_fork_read
,
884 .aio_write
= aio_fork_write
,
885 .aio_return_fn
= aio_fork_return_fn
,
886 .aio_cancel
= aio_fork_cancel
,
887 .aio_error_fn
= aio_fork_error_fn
,
888 .aio_suspend
= aio_fork_suspend
,
891 NTSTATUS
vfs_aio_fork_init(void);
892 NTSTATUS
vfs_aio_fork_init(void)
894 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
895 "aio_fork", &vfs_aio_fork_fns
);