Fix off-by-one error in working out the limit of the NetServerEnum comment.
[Samba.git] / source / modules / vfs_aio_fork.c
blob30b14f280f2a741201c0e16d15c59b55ad73b4c4
1 /*
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.
21 #include "includes.h"
23 struct mmap_area {
24 size_t size;
25 volatile void *ptr;
28 static int mmap_area_destructor(struct mmap_area *area)
30 munmap((void *)area->ptr, area->size);
31 return 0;
34 static struct mmap_area *mmap_area_init(TALLOC_CTX *mem_ctx, size_t size)
36 struct mmap_area *result;
37 int fd;
39 result = talloc(mem_ctx, struct mmap_area);
40 if (result == NULL) {
41 DEBUG(0, ("talloc failed\n"));
42 goto fail;
45 fd = open("/dev/zero", O_RDWR);
46 if (fd == -1) {
47 DEBUG(3, ("open(\"/dev/zero\") failed: %s\n",
48 strerror(errno)));
49 goto fail;
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)));
56 goto fail;
59 close(fd);
61 result->size = size;
62 talloc_set_destructor(result, mmap_area_destructor);
64 return result;
66 fail:
67 TALLOC_FREE(result);
68 return NULL;
71 struct rw_cmd {
72 size_t n;
73 SMB_OFF_T offset;
74 bool read_cmd;
77 struct rw_ret {
78 ssize_t size;
79 int ret_errno;
82 struct aio_child_list;
84 struct aio_child {
85 struct aio_child *prev, *next;
86 struct aio_child_list *list;
87 SMB_STRUCT_AIOCB *aiocb;
88 pid_t pid;
89 int sockfd;
90 struct fd_event *sock_event;
91 struct rw_ret retval;
92 struct mmap_area *map; /* ==NULL means write request */
93 bool dont_delete; /* Marked as in use since last cleanup */
94 bool cancelled;
95 bool read_cmd;
98 struct aio_child_list {
99 struct aio_child *children;
100 struct timed_event *cleanup_event;
103 static void free_aio_children(void **p)
105 TALLOC_FREE(*p);
108 static ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
110 struct msghdr msg;
111 struct iovec iov[1];
112 ssize_t n;
113 #ifndef HAVE_MSGHDR_MSG_CONTROL
114 int newfd;
115 #endif
117 #ifdef HAVE_MSGHDR_MSG_CONTROL
118 union {
119 struct cmsghdr cm;
120 char control[CMSG_SPACE(sizeof(int))];
121 } control_un;
122 struct cmsghdr *cmptr;
124 msg.msg_control = control_un.control;
125 msg.msg_controllen = sizeof(control_un.control);
126 #else
127 #if HAVE_MSGHDR_MSG_ACCTRIGHTS
128 msg.msg_accrights = (caddr_t) &newfd;
129 msg.msg_accrightslen = sizeof(int);
130 #else
131 #error Can not pass file descriptors
132 #endif
133 #endif
135 msg.msg_name = NULL;
136 msg.msg_namelen = 0;
138 iov[0].iov_base = ptr;
139 iov[0].iov_len = nbytes;
140 msg.msg_iov = iov;
141 msg.msg_iovlen = 1;
143 if ( (n = recvmsg(fd, &msg, 0)) <= 0) {
144 return(n);
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"));
152 errno = EINVAL;
153 return -1;
155 if (cmptr->cmsg_type != SCM_RIGHTS) {
156 DEBUG(10, ("control type != SCM_RIGHTS"));
157 errno = EINVAL;
158 return -1;
160 *recvfd = *((int *) CMSG_DATA(cmptr));
161 } else {
162 *recvfd = -1; /* descriptor was not passed */
164 #else
165 if (msg.msg_accrightslen == sizeof(int)) {
166 *recvfd = newfd;
168 else {
169 *recvfd = -1; /* descriptor was not passed */
171 #endif
173 return(n);
176 static ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
178 struct msghdr msg;
179 struct iovec iov[1];
181 #ifdef HAVE_MSGHDR_MSG_CONTROL
182 union {
183 struct cmsghdr cm;
184 char control[CMSG_SPACE(sizeof(int))];
185 } control_un;
186 struct cmsghdr *cmptr;
188 ZERO_STRUCT(msg);
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;
199 #else
200 ZERO_STRUCT(msg);
201 msg.msg_accrights = (caddr_t) &sendfd;
202 msg.msg_accrightslen = sizeof(int);
203 #endif
205 msg.msg_name = NULL;
206 msg.msg_namelen = 0;
208 ZERO_STRUCT(iov);
209 iov[0].iov_base = ptr;
210 iov[0].iov_len = nbytes;
211 msg.msg_iov = iov;
212 msg.msg_iovlen = 1;
214 return (sendmsg(fd, &msg, 0));
217 static void aio_child_cleanup(struct event_context *event_ctx,
218 struct timed_event *te,
219 struct timeval now,
220 void *private_data)
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) {
229 next = child->next;
231 if (child->aiocb != NULL) {
232 DEBUG(10, ("child %d currently active\n",
233 (int)child->pid));
234 continue;
237 if (child->dont_delete) {
238 DEBUG(10, ("Child %d was active since last cleanup\n",
239 (int)child->pid));
240 child->dont_delete = false;
241 continue;
244 DEBUG(10, ("Child %d idle for more than 30 seconds, "
245 "deleting\n", (int)child->pid));
247 TALLOC_FREE(child);
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),
256 aio_child_cleanup, list);
261 static struct aio_child_list *init_aio_children(struct vfs_handle_struct *handle)
263 struct aio_child_list *data = NULL;
265 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
266 SMB_VFS_HANDLE_GET_DATA(handle, data, struct aio_child_list,
267 return NULL);
270 if (data == NULL) {
271 data = TALLOC_ZERO_P(NULL, struct aio_child_list);
272 if (data == NULL) {
273 return NULL;
278 * Regardless of whether the child_list had been around or not, make
279 * sure that we have a cleanup timed event. This timed event will
280 * delete itself when it finds that no children are around anymore.
283 if (data->cleanup_event == NULL) {
284 data->cleanup_event = event_add_timed(smbd_event_context(), data,
285 timeval_current_ofs(30, 0),
286 aio_child_cleanup, data);
287 if (data->cleanup_event == NULL) {
288 TALLOC_FREE(data);
289 return NULL;
293 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
294 SMB_VFS_HANDLE_SET_DATA(handle, data, free_aio_children,
295 struct aio_child_list, return False);
298 return data;
301 static void aio_child_loop(int sockfd, struct mmap_area *map)
303 while (true) {
304 int fd = -1;
305 ssize_t ret;
306 struct rw_cmd cmd_struct;
307 struct rw_ret ret_struct;
309 ret = read_fd(sockfd, &cmd_struct, sizeof(cmd_struct), &fd);
310 if (ret != sizeof(cmd_struct)) {
311 DEBUG(10, ("read_fd returned %d: %s\n", (int)ret,
312 strerror(errno)));
313 exit(1);
316 DEBUG(10, ("aio_child_loop: %s %d bytes at %d from fd %d\n",
317 cmd_struct.read_cmd ? "read" : "write",
318 (int)cmd_struct.n, (int)cmd_struct.offset, fd));
320 #ifdef ENABLE_BUILD_FARM_HACKS
323 * In the build farm, we want erratic behaviour for
324 * async I/O times
326 uint8_t randval;
327 unsigned msecs;
329 * use generate_random_buffer, we just forked from a
330 * common parent state
332 generate_random_buffer(&randval, sizeof(randval));
333 msecs = randval + 20;
334 DEBUG(10, ("delaying for %u msecs\n", msecs));
335 smb_msleep(msecs);
337 #endif
340 ZERO_STRUCT(ret_struct);
342 if (cmd_struct.read_cmd) {
343 ret_struct.size = sys_pread(
344 fd, (void *)map->ptr, cmd_struct.n,
345 cmd_struct.offset);
347 else {
348 ret_struct.size = sys_pwrite(
349 fd, (void *)map->ptr, cmd_struct.n,
350 cmd_struct.offset);
353 DEBUG(10, ("aio_child_loop: syscall returned %d\n",
354 (int)ret_struct.size));
356 if (ret_struct.size == -1) {
357 ret_struct.ret_errno = errno;
360 ret = write_data(sockfd, (char *)&ret_struct,
361 sizeof(ret_struct));
362 if (ret != sizeof(ret_struct)) {
363 DEBUG(10, ("could not write ret_struct: %s\n",
364 strerror(errno)));
365 exit(2);
368 close(fd);
372 static void handle_aio_completion(struct event_context *event_ctx,
373 struct fd_event *event, uint16 flags,
374 void *p)
376 struct aio_child *child = (struct aio_child *)p;
377 uint16 mid;
379 DEBUG(10, ("handle_aio_completion called with flags=%d\n", flags));
381 if ((flags & EVENT_FD_READ) == 0) {
382 return;
385 if (!NT_STATUS_IS_OK(read_data(child->sockfd,
386 (char *)&child->retval,
387 sizeof(child->retval)))) {
388 DEBUG(0, ("aio child %d died\n", (int)child->pid));
389 child->retval.size = -1;
390 child->retval.ret_errno = EIO;
393 if (child->cancelled) {
394 child->aiocb = NULL;
395 child->cancelled = false;
396 return;
399 if (child->read_cmd && (child->retval.size > 0)) {
400 SMB_ASSERT(child->retval.size <= child->aiocb->aio_nbytes);
401 memcpy((void *)child->aiocb->aio_buf, (void *)child->map->ptr,
402 child->retval.size);
405 mid = child->aiocb->aio_sigevent.sigev_value.sival_int;
407 DEBUG(10, ("mid %d finished\n", (int)mid));
409 aio_request_done(mid);
410 process_aio_queue();
413 static int aio_child_destructor(struct aio_child *child)
415 SMB_ASSERT((child->aiocb == NULL) || child->cancelled);
416 close(child->sockfd);
417 DLIST_REMOVE(child->list->children, child);
418 return 0;
421 static NTSTATUS create_aio_child(struct aio_child_list *children,
422 size_t map_size,
423 struct aio_child **presult)
425 struct aio_child *result;
426 int fdpair[2];
427 NTSTATUS status;
429 fdpair[0] = fdpair[1] = -1;
431 result = TALLOC_ZERO_P(children, struct aio_child);
432 NT_STATUS_HAVE_NO_MEMORY(result);
434 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
435 status = map_nt_error_from_unix(errno);
436 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
437 goto fail;
440 DEBUG(10, ("fdpair = %d/%d\n", fdpair[0], fdpair[1]));
442 result->map = mmap_area_init(result, map_size);
443 if (result->map == NULL) {
444 status = map_nt_error_from_unix(errno);
445 DEBUG(0, ("Could not create mmap area\n"));
446 goto fail;
449 result->pid = sys_fork();
450 if (result->pid == -1) {
451 status = map_nt_error_from_unix(errno);
452 DEBUG(0, ("fork failed: %s\n", strerror(errno)));
453 goto fail;
456 if (result->pid == 0) {
457 close(fdpair[0]);
458 result->sockfd = fdpair[1];
459 aio_child_loop(result->sockfd, result->map);
462 DEBUG(10, ("Child %d created\n", result->pid));
464 result->sockfd = fdpair[0];
465 close(fdpair[1]);
467 result->sock_event = event_add_fd(smbd_event_context(), result,
468 result->sockfd, EVENT_FD_READ,
469 handle_aio_completion,
470 result);
471 if (result->sock_event == NULL) {
472 status = NT_STATUS_NO_MEMORY;
473 DEBUG(0, ("event_add_fd failed\n"));
474 goto fail;
477 result->list = children;
478 DLIST_ADD(children->children, result);
480 talloc_set_destructor(result, aio_child_destructor);
482 *presult = result;
484 return NT_STATUS_OK;
486 fail:
487 if (fdpair[0] != -1) close(fdpair[0]);
488 if (fdpair[1] != -1) close(fdpair[1]);
489 TALLOC_FREE(result);
491 return status;
494 static NTSTATUS get_idle_child(struct vfs_handle_struct *handle,
495 struct aio_child **pchild)
497 struct aio_child_list *children;
498 struct aio_child *child;
499 NTSTATUS status;
501 children = init_aio_children(handle);
502 if (children == NULL) {
503 return NT_STATUS_NO_MEMORY;
506 for (child = children->children; child != NULL; child = child->next) {
507 if (child->aiocb == NULL) {
508 /* idle */
509 break;
513 if (child == NULL) {
514 DEBUG(10, ("no idle child found, creating new one\n"));
516 status = create_aio_child(children, 128*1024, &child);
517 if (!NT_STATUS_IS_OK(status)) {
518 DEBUG(10, ("create_aio_child failed: %s\n",
519 nt_errstr(status)));
520 return status;
524 child->dont_delete = true;
526 *pchild = child;
527 return NT_STATUS_OK;
530 static int aio_fork_read(struct vfs_handle_struct *handle,
531 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
533 struct aio_child *child;
534 struct rw_cmd cmd;
535 ssize_t ret;
536 NTSTATUS status;
538 if (aiocb->aio_nbytes > 128*1024) {
539 /* TODO: support variable buffers */
540 errno = EINVAL;
541 return -1;
544 status = get_idle_child(handle, &child);
545 if (!NT_STATUS_IS_OK(status)) {
546 DEBUG(10, ("Could not get an idle child\n"));
547 return -1;
550 child->read_cmd = true;
551 child->aiocb = aiocb;
552 child->retval.ret_errno = EINPROGRESS;
554 ZERO_STRUCT(cmd);
555 cmd.n = aiocb->aio_nbytes;
556 cmd.offset = aiocb->aio_offset;
557 cmd.read_cmd = child->read_cmd;
559 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
560 (int)child->pid));
562 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
563 if (ret == -1) {
564 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
565 return -1;
568 return 0;
571 static int aio_fork_write(struct vfs_handle_struct *handle,
572 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
574 struct aio_child *child;
575 struct rw_cmd cmd;
576 ssize_t ret;
577 NTSTATUS status;
579 if (aiocb->aio_nbytes > 128*1024) {
580 /* TODO: support variable buffers */
581 errno = EINVAL;
582 return -1;
585 status = get_idle_child(handle, &child);
586 if (!NT_STATUS_IS_OK(status)) {
587 DEBUG(10, ("Could not get an idle child\n"));
588 return -1;
591 child->read_cmd = false;
592 child->aiocb = aiocb;
593 child->retval.ret_errno = EINPROGRESS;
595 memcpy((void *)child->map->ptr, (void *)aiocb->aio_buf,
596 aiocb->aio_nbytes);
598 ZERO_STRUCT(cmd);
599 cmd.n = aiocb->aio_nbytes;
600 cmd.offset = aiocb->aio_offset;
601 cmd.read_cmd = child->read_cmd;
603 DEBUG(10, ("sending fd %d to child %d\n", fsp->fh->fd,
604 (int)child->pid));
606 ret = write_fd(child->sockfd, &cmd, sizeof(cmd), fsp->fh->fd);
607 if (ret == -1) {
608 DEBUG(10, ("write_fd failed: %s\n", strerror(errno)));
609 return -1;
612 return 0;
615 static struct aio_child *aio_fork_find_child(struct vfs_handle_struct *handle,
616 SMB_STRUCT_AIOCB *aiocb)
618 struct aio_child_list *children;
619 struct aio_child *child;
621 children = init_aio_children(handle);
622 if (children == NULL) {
623 return NULL;
626 for (child = children->children; child != NULL; child = child->next) {
627 if (child->aiocb == aiocb) {
628 return child;
632 return NULL;
635 static ssize_t aio_fork_return_fn(struct vfs_handle_struct *handle,
636 struct files_struct *fsp,
637 SMB_STRUCT_AIOCB *aiocb)
639 struct aio_child *child = aio_fork_find_child(handle, aiocb);
641 if (child == NULL) {
642 errno = EINVAL;
643 DEBUG(0, ("returning EINVAL\n"));
644 return -1;
647 child->aiocb = NULL;
649 if (child->retval.size == -1) {
650 errno = child->retval.ret_errno;
653 return child->retval.size;
656 static int aio_fork_cancel(struct vfs_handle_struct *handle,
657 struct files_struct *fsp,
658 SMB_STRUCT_AIOCB *aiocb)
660 struct aio_child_list *children;
661 struct aio_child *child;
663 children = init_aio_children(handle);
664 if (children == NULL) {
665 errno = EINVAL;
666 return -1;
669 for (child = children->children; child != NULL; child = child->next) {
670 if (child->aiocb == NULL) {
671 continue;
673 if (child->aiocb->aio_fildes != fsp->fh->fd) {
674 continue;
676 if ((aiocb != NULL) && (child->aiocb != aiocb)) {
677 continue;
681 * We let the child do its job, but we discard the result when
682 * it's finished.
685 child->cancelled = true;
688 return AIO_CANCELED;
691 static int aio_fork_error_fn(struct vfs_handle_struct *handle,
692 struct files_struct *fsp,
693 SMB_STRUCT_AIOCB *aiocb)
695 struct aio_child *child = aio_fork_find_child(handle, aiocb);
697 if (child == NULL) {
698 errno = EINVAL;
699 return -1;
702 return child->retval.ret_errno;
705 /* VFS operations structure */
707 static vfs_op_tuple aio_fork_ops[] = {
708 {SMB_VFS_OP(aio_fork_read), SMB_VFS_OP_AIO_READ,
709 SMB_VFS_LAYER_TRANSPARENT},
710 {SMB_VFS_OP(aio_fork_write), SMB_VFS_OP_AIO_WRITE,
711 SMB_VFS_LAYER_TRANSPARENT},
712 {SMB_VFS_OP(aio_fork_return_fn), SMB_VFS_OP_AIO_RETURN,
713 SMB_VFS_LAYER_TRANSPARENT},
714 {SMB_VFS_OP(aio_fork_cancel), SMB_VFS_OP_AIO_CANCEL,
715 SMB_VFS_LAYER_TRANSPARENT},
716 {SMB_VFS_OP(aio_fork_error_fn), SMB_VFS_OP_AIO_ERROR,
717 SMB_VFS_LAYER_TRANSPARENT},
718 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP,
719 SMB_VFS_LAYER_NOOP}
722 NTSTATUS vfs_aio_fork_init(void);
723 NTSTATUS vfs_aio_fork_init(void)
725 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
726 "aio_fork", aio_fork_ops);