2 * Simulate the Posix AIO using mmap/fork
4 * Copyright (C) Volker Lendecke 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 static int mmap_area_destructor(struct mmap_area *area)
30 munmap((void *)area->ptr, area->size);
34 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
36 struct mmap_area *result;
39 result = talloc(mem_ctx, struct mmap_area);
41 DEBUG(0, ("talloc failed\n"));
45 fd = open("/dev/zero", O_RDWR);
47 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
52 result->ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
53 MAP_SHARED|MAP_FILE, fd, 0);
54 if (result->ptr == MAP_FAILED) {
55 DEBUG(1, ("mmap failed: %s\n", strerror(errno)));
62 talloc_set_destructor(result, mmap_area_destructor);
82 struct aio_child_list;
85 struct aio_child *prev, *next;
86 struct aio_child_list *list;
87 SMB_STRUCT_AIOCB *aiocb;
90 struct fd_event *sock_event;
92 struct mmap_area *map; /* ==NULL means write request */
93 bool dont_delete; /* Marked as in use since last cleanup */
98 struct aio_child_list {
99 struct aio_child *children;
100 struct timed_event *cleanup_event;
103 static void free_aio_children(void **p)
108 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
113 #ifndef HAVE_MSGHDR_MSG_CONTROL
117 #ifdef HAVE_MSGHDR_MSG_CONTROL
120 char control[CMSG_SPACE(sizeof(int))];
122 struct cmsghdr *cmptr;
124 msg.msg_control = control_un.control;
125 msg.msg_controllen = sizeof(control_un.control);
127 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
128 msg.msg_accrights = (caddr_t) &newfd;
129 msg.msg_accrightslen = sizeof(int);
131 #error Can not pass file descriptors
138 iov[0].iov_base = ptr;
139 iov[0].iov_len = nbytes;
143 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
147 #ifdef HAVE_MSGHDR_MSG_CONTROL
148 if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
149 && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
150 if (cmptr->cmsg_level != SOL_SOCKET) {
151 DEBUG(10, ("control level != SOL_SOCKET"));
155 if (cmptr->cmsg_type != SCM_RIGHTS) {
156 DEBUG(10, ("control type != SCM_RIGHTS"));
160 *recvfd = *((int *) CMSG_DATA(cmptr));
162 *recvfd = -1; /* descriptor was not passed */
165 if (msg.msg_accrightslen == sizeof(int)) {
169 *recvfd = -1; /* descriptor was not passed */
176 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
181 #ifdef HAVE_MSGHDR_MSG_CONTROL
184 char control[CMSG_SPACE(sizeof(int))];
186 struct cmsghdr *cmptr;
189 ZERO_STRUCT(control_un);
191 msg.msg_control = control_un.control;
192 msg.msg_controllen = sizeof(control_un.control);
194 cmptr = CMSG_FIRSTHDR(&msg);
195 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
196 cmptr->cmsg_level = SOL_SOCKET;
197 cmptr->cmsg_type = SCM_RIGHTS;
198 *((int *) CMSG_DATA(cmptr)) = sendfd;
201 msg.msg_accrights = (caddr_t) &sendfd;
202 msg.msg_accrightslen = sizeof(int);
209 iov[0].iov_base = ptr;
210 iov[0].iov_len = nbytes;
214 return (sendmsg(fd, &msg, 0));
217 static void aio_child_cleanup(struct event_context *event_ctx,
218 struct timed_event *te,
219 const struct timeval *now,
222 struct aio_child_list *list = talloc_get_type_abort(
223 private_data, struct aio_child_list);
224 struct aio_child *child, *next;
226 TALLOC_FREE(list->cleanup_event);
228 for (child = list->children; child != NULL; child = next) {
231 if (child->aiocb != NULL) {
232 DEBUG(10, ("child %d currently active\n",
237 if (child->dont_delete) {
238 DEBUG(10, ("Child %d was active since last cleanup\n",
240 child->dont_delete = false;
244 DEBUG(10, ("Child %d idle for more than 30 seconds, "
245 "deleting\n", (int)child->pid));
250 if (list->children != NULL) {
252 * Re-schedule the next cleanup round
254 list->cleanup_event = event_add_timed(smbd_event_context(), list,
255 timeval_add(now, 30, 0),
257 aio_child_cleanup, list);
262 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
264 struct aio_child_list *data = NULL;
266 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
267 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
272 data = TALLOC_ZERO_P(NULL, struct aio_child_list);
279 * Regardless of whether the child_list had been around or not, make
280 * sure that we have a cleanup timed event. This timed event will
281 * delete itself when it finds that no children are around anymore.
284 if (data->cleanup_event == NULL) {
285 data->cleanup_event = event_add_timed(smbd_event_context(), data,
286 timeval_current_ofs(30, 0),
288 aio_child_cleanup, data);
289 if (data->cleanup_event == NULL) {
295 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
296 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
297 struct aio_child_list, return False);
303 static void aio_child_loop(int sockfd, struct mmap_area *map)
308 struct rw_cmd cmd_struct;
309 struct rw_ret ret_struct;
311 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
312 if (ret != sizeof(cmd_struct)) {
313 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
318 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
319 cmd_struct.read_cmd ? "read" : "write",
320 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
322 #ifdef ENABLE_BUILD_FARM_HACKS
325 * In the build farm, we want erratic behaviour for
331 * use generate_random_buffer, we just forked from a
332 * common parent state
334 generate_random_buffer(&randval, sizeof(randval));
335 msecs = randval + 20;
336 DEBUG(10, ("delaying for %u msecs\n", msecs));
342 ZERO_STRUCT(ret_struct);
344 if (cmd_struct.read_cmd) {
345 ret_struct.size = sys_pread(
346 fd, (void *)map->ptr, cmd_struct.n,
350 ret_struct.size = sys_pwrite(
351 fd, (void *)map->ptr, cmd_struct.n,
355 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
356 (int)ret_struct.size));
358 if (ret_struct.size == -1) {
359 ret_struct.ret_errno = errno;
362 ret = write_data(sockfd, (char *)&ret_struct,
364 if (ret != sizeof(ret_struct)) {
365 DEBUG(10, ("could not write ret_struct: %s\n",
374 static void handle_aio_completion(struct event_context *event_ctx,
375 struct fd_event *event, uint16 flags,
378 struct aio_child *child = (struct aio_child *)p;
381 DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags));
383 if ((flags & EVENT_FD_READ) == 0) {
387 if (!NT_STATUS_IS_OK(read_data(child->sockfd,
388 (char *)&child->retval,
389 sizeof(child->retval)))) {
390 DEBUG(0, ("aio child %d died\n", (int)child->pid));
391 child->retval.size = -1;
392 child->retval.ret_errno = EIO;
395 if (child->cancelled) {
397 child->cancelled = false;
401 if (child->read_cmd && (child->retval.size > 0)) {
402 SMB_ASSERT(child->retval.size <= child->aiocb->aio_nbytes);
403 memcpy((void *)child->aiocb->aio_buf, (void *)child->map->ptr,
407 mid = child->aiocb->aio_sigevent.sigev_value.sival_int;
409 DEBUG(10, ("mid %d finished\n", (int)mid));
411 aio_request_done(mid);
415 static int aio_child_destructor(struct aio_child *child)
417 SMB_ASSERT((child->aiocb == NULL) || child->cancelled);
418 close(child->sockfd);
419 DLIST_REMOVE(child->list->children, child);
423 static NTSTATUS create_aio_child(struct aio_child_list *children,
425 struct aio_child **presult)
427 struct aio_child *result;
431 fdpair[0] = fdpair[1] = -1;
433 result = TALLOC_ZERO_P(children, struct aio_child);
434 NT_STATUS_HAVE_NO_MEMORY(result);
436 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
437 status = map_nt_error_from_unix(errno);
438 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
442 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
444 result->map = mmap_area_init(result, map_size);
445 if (result->map == NULL) {
446 status = map_nt_error_from_unix(errno);
447 DEBUG(0, ("Could not create mmap area\n"));
451 result->pid = sys_fork();
452 if (result->pid == -1) {
453 status = map_nt_error_from_unix(errno);
454 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
458 if (result->pid == 0) {
460 result->sockfd = fdpair[1];
461 aio_child_loop(result->sockfd, result->map);
464 DEBUG(10, ("Child %d created\n", result->pid));
466 result->sockfd = fdpair[0];
469 result->sock_event = event_add_fd(smbd_event_context(), result,
470 result->sockfd, EVENT_FD_READ,
471 handle_aio_completion,
473 if (result->sock_event == NULL) {
474 status = NT_STATUS_NO_MEMORY;
475 DEBUG(0, ("event_add_fd failed\n"));
479 result->list = children;
480 DLIST_ADD(children->children, result);
482 talloc_set_destructor(result, aio_child_destructor);
489 if (fdpair[0] != -1) close(fdpair[0]);
490 if (fdpair[1] != -1) close(fdpair[1]);
496 static NTSTATUS get_idle_child(struct vfs_handle_struct *handle,
497 struct aio_child **pchild)
499 struct aio_child_list *children;
500 struct aio_child *child;
503 children = init_aio_children(handle);
504 if (children == NULL) {
505 return NT_STATUS_NO_MEMORY;
508 for (child = children->children; child != NULL; child = child->next) {
509 if (child->aiocb == NULL) {
516 DEBUG(10, ("no idle child found, creating new one\n"));
518 status = create_aio_child(children, 128*1024, &child);
519 if (!NT_STATUS_IS_OK(status)) {
520 DEBUG(10, ("create_aio_child failed: %s\n",
526 child->dont_delete = true;
532 static int aio_fork_read(struct vfs_handle_struct *handle,
533 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
535 struct aio_child *child;
540 if (aiocb->aio_nbytes > 128*1024) {
541 /* TODO: support variable buffers */
546 status = get_idle_child(handle, &child);
547 if (!NT_STATUS_IS_OK(status)) {
548 DEBUG(10, ("Could not get an idle child\n"));
552 child->read_cmd = true;
553 child->aiocb = aiocb;
554 child->retval.ret_errno = EINPROGRESS;
557 cmd.n = aiocb->aio_nbytes;
558 cmd.offset = aiocb->aio_offset;
559 cmd.read_cmd = child->read_cmd;
561 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
564 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
566 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
573 static int aio_fork_write(struct vfs_handle_struct *handle,
574 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
576 struct aio_child *child;
581 if (aiocb->aio_nbytes > 128*1024) {
582 /* TODO: support variable buffers */
587 status = get_idle_child(handle, &child);
588 if (!NT_STATUS_IS_OK(status)) {
589 DEBUG(10, ("Could not get an idle child\n"));
593 child->read_cmd = false;
594 child->aiocb = aiocb;
595 child->retval.ret_errno = EINPROGRESS;
597 memcpy((void *)child->map->ptr, (void *)aiocb->aio_buf,
601 cmd.n = aiocb->aio_nbytes;
602 cmd.offset = aiocb->aio_offset;
603 cmd.read_cmd = child->read_cmd;
605 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
608 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
610 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
617 static struct aio_child *aio_fork_find_child(struct vfs_handle_struct *handle,
618 SMB_STRUCT_AIOCB *aiocb)
620 struct aio_child_list *children;
621 struct aio_child *child;
623 children = init_aio_children(handle);
624 if (children == NULL) {
628 for (child = children->children; child != NULL; child = child->next) {
629 if (child->aiocb == aiocb) {
637 static ssize_t aio_fork_return_fn(struct vfs_handle_struct *handle,
638 struct files_struct *fsp,
639 SMB_STRUCT_AIOCB *aiocb)
641 struct aio_child *child = aio_fork_find_child(handle, aiocb);
645 DEBUG(0, ("returning EINVAL\n"));
651 if (child->retval.size == -1) {
652 errno = child->retval.ret_errno;
655 return child->retval.size;
658 static int aio_fork_cancel(struct vfs_handle_struct *handle,
659 struct files_struct *fsp,
660 SMB_STRUCT_AIOCB *aiocb)
662 struct aio_child_list *children;
663 struct aio_child *child;
665 children = init_aio_children(handle);
666 if (children == NULL) {
671 for (child = children->children; child != NULL; child = child->next) {
672 if (child->aiocb == NULL) {
675 if (child->aiocb->aio_fildes != fsp->fh->fd) {
678 if ((aiocb != NULL) && (child->aiocb != aiocb)) {
683 * We let the child do its job, but we discard the result when
687 child->cancelled = true;
693 static int aio_fork_error_fn(struct vfs_handle_struct *handle,
694 struct files_struct *fsp,
695 SMB_STRUCT_AIOCB *aiocb)
697 struct aio_child *child = aio_fork_find_child(handle, aiocb);
704 return child->retval.ret_errno;
707 /* VFS operations structure */
709 static vfs_op_tuple aio_fork_ops[] = {
710 {SMB_VFS_OP(aio_fork_read), SMB_VFS_OP_AIO_READ,
711 SMB_VFS_LAYER_TRANSPARENT},
712 {SMB_VFS_OP(aio_fork_write), SMB_VFS_OP_AIO_WRITE,
713 SMB_VFS_LAYER_TRANSPARENT},
714 {SMB_VFS_OP(aio_fork_return_fn), SMB_VFS_OP_AIO_RETURN,
715 SMB_VFS_LAYER_TRANSPARENT},
716 {SMB_VFS_OP(aio_fork_cancel), SMB_VFS_OP_AIO_CANCEL,
717 SMB_VFS_LAYER_TRANSPARENT},
718 {SMB_VFS_OP(aio_fork_error_fn), SMB_VFS_OP_AIO_ERROR,
719 SMB_VFS_LAYER_TRANSPARENT},
720 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP,
724 NTSTATUS vfs_aio_fork_init(void);
725 NTSTATUS vfs_aio_fork_init(void)
727 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
728 "aio_fork", aio_fork_ops);