Initial Linux ZFS GIT Repo
[zfs.git] / zfs / lib / libdmu-ctl / include / sys / dmu_ctl_impl.h
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26
27 #ifndef _SYS_DMU_CTL_IMPL_H
28 #define _SYS_DMU_CTL_IMPL_H
29
30 #include <sys/list.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <pthread.h>
35
36 #define SOCKNAME "dmu_socket"
37
38 #define DCTL_PROTOCOL_VER 1
39 #define DCTL_MAGIC 0xdc71b1070c01dc71ll
40
41 /* Message types */
42 enum {
43         DCTL_IOCTL,
44         DCTL_IOCTL_REPLY,
45         DCTL_COPYIN,
46         DCTL_COPYINSTR,
47         DCTL_COPYOUT,
48         DCTL_FD_READ,
49         DCTL_FD_WRITE,
50         DCTL_GEN_REPLY /* generic reply */
51 };
52
53 /* On-the-wire message */
54 typedef struct dctl_cmd {
55         uint64_t dcmd_magic;
56         int8_t   dcmd_version;
57         int8_t   dcmd_msg;
58         uint8_t  dcmd_pad[6];
59         union {
60                 struct dcmd_ioctl {
61                         uint64_t arg;
62                         int32_t cmd;
63                         uint8_t pad[4];
64                 } dcmd_ioctl;
65
66                 struct dcmd_copy_req {
67                         uint64_t ptr;
68                         uint64_t size;
69                 } dcmd_copy;
70
71                 struct dcmd_fd_req {
72                         int64_t size;
73                         int32_t fd;
74                         uint8_t pad[4];
75                 } dcmd_fd_io;
76
77                 struct dcmd_reply {
78                         uint64_t size;  /* used by reply to DCTL_COPYINSTR,
79                                            DCTL_FD_READ and DCTL_FD_WRITE */
80                         int32_t rc;     /* return code */
81                         uint8_t pad[4];
82                 } dcmd_reply;
83         } u;
84 } dctl_cmd_t;
85
86 #define DCTL_CMD_HEADER_SIZE (sizeof(uint64_t) + sizeof(uint8_t))
87
88 /*
89  * The following definitions are only used by the server code.
90  */
91
92 #define LISTEN_BACKLOG 5
93
94 /* Worker thread data */
95 typedef struct wthr_info {
96         list_node_t wthr_node;
97         pthread_t   wthr_id;
98         boolean_t   wthr_exit; /* termination flag */
99         boolean_t   wthr_free;
100 } wthr_info_t;
101
102 /* Control socket data */
103 typedef struct dctl_sock_info {
104         pthread_mutex_t    dsi_mtx;
105         char               *dsi_path;
106         struct sockaddr_un dsi_addr;
107         int                dsi_fd;
108 } dctl_sock_info_t;
109
110 typedef void *thr_func_t(void *);
111
112 /* Thread pool data */
113 typedef struct dctl_thr_info {
114         thr_func_t *dti_thr_func;
115
116         pthread_mutex_t dti_mtx; /* protects the thread lists and dti_free */
117         list_t dti_list;         /* list of threads in the thread pool */
118         list_t dti_join_list;    /* list of threads that are waiting to be
119                                     joined */
120         int    dti_free;         /* number of free worker threads */
121
122         int dti_min;
123         int dti_max_free;
124
125         boolean_t dti_exit; /* global termination flag */
126 } dctl_thr_info_t;
127
128 /* Messaging functions functions */
129 int dctl_read_msg(int fd, dctl_cmd_t *cmd);
130 int dctl_send_msg(int fd, dctl_cmd_t *cmd);
131
132 int dctl_read_data(int fd, void *ptr, size_t size);
133 int dctl_send_data(int fd, const void *ptr, size_t size);
134
135 /* Thread pool functions */
136 int dctl_thr_pool_create(int min_thr, int max_free_thr,
137     thr_func_t *thr_func);
138 void dctl_thr_pool_stop();
139
140 void dctl_thr_join();
141 void dctl_thr_die(wthr_info_t *thr);
142 void dctl_thr_rebalance(wthr_info_t *thr, boolean_t set_free);
143
144 #endif