xref: /titanic_52/usr/src/cmd/ndmpd/ndmp/ndmpd_common.h (revision 71269a2275bf5a143dad6461eee2710a344e7261)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * BSD 3 Clause License
8  *
9  * Copyright (c) 2007, The Storage Networking Industry Association.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 	- Redistributions of source code must retain the above copyright
15  *	  notice, this list of conditions and the following disclaimer.
16  *
17  * 	- Redistributions in binary form must reproduce the above copyright
18  *	  notice, this list of conditions and the following disclaimer in
19  *	  the documentation and/or other materials provided with the
20  *	  distribution.
21  *
22  *	- Neither the name of The Storage Networking Industry Association (SNIA)
23  *	  nor the names of its contributors may be used to endorse or promote
24  *	  products derived from this software without specific prior written
25  *	  permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 /* Copyright (c) 2007, The Storage Networking Industry Association. */
40 /* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */
41 
42 #ifndef	_NDMP_COMMON_H
43 #define	_NDMP_COMMON_H
44 
45 #include <thread.h>
46 #include <synch.h>
47 #include "ndmpd_log.h"
48 #include "ndmp.h"
49 #include <unistd.h>
50 #include <sys/types.h>
51 #include <rpc/rpc.h>
52 #include <sys/stat.h>
53 #include <stdio.h>
54 #include <bsm/adt.h>
55 #include <bsm/adt_event.h>
56 
57 
58 #define	XDR_AND_SIZE(func) (bool_t(*)(XDR*, ...))xdr_##func, sizeof (func)
59 #define	AUTH_REQUIRED	TRUE
60 #define	AUTH_NOT_REQUIRED	FALSE
61 #define	NDMP_EOM_MAGIC	"PRCMEOM"
62 #define	KILOBYTE	1024
63 
64 #define	INT_MAXCMD	12
65 
66 extern mutex_t log_lock;
67 
68 /* Connection data structure. */
69 typedef struct msg_info {
70 	ndmp_header mi_hdr;
71 	struct ndmp_msg_handler *mi_handler;
72 	void *mi_body;
73 } msg_info_t;
74 
75 typedef struct ndmp_connection {
76 	int conn_sock;
77 	XDR conn_xdrs;
78 	ulong_t conn_my_sequence;
79 	boolean_t conn_authorized;
80 	boolean_t conn_eof;
81 	msg_info_t conn_msginfo; /* received request or reply message */
82 	ushort_t conn_version;
83 	void *conn_client_data;
84 	mutex_t conn_lock;
85 	adt_session_data_t *conn_ah;
86 } ndmp_connection_t;
87 
88 typedef void (*ndmp_con_handler_func_t) (struct ndmp_connection *);
89 
90 typedef void ndmp_msg_handler_func_t(struct ndmp_connection *, void *);
91 
92 
93 typedef struct ndmp_msg_handler {
94 	ndmp_msg_handler_func_t *mh_func;
95 	bool_t(*mh_xdr_request) (XDR *xdrs, ...);
96 	int mh_sizeof_request;
97 	bool_t(*mh_xdr_reply) (XDR *xdrs, ...);
98 	int mh_sizeof_reply;
99 } ndmp_msg_handler_t;
100 
101 typedef struct ndmp_handler {
102 	int hd_cnt;
103 	struct hd_messages {
104 		ndmp_message hm_message;
105 		boolean_t hm_auth_required;
106 		ndmp_msg_handler_t hm_msg_v[3];
107 	} hd_msgs[INT_MAXCMD];
108 } ndmp_handler_t;
109 
110 /*
111  * Function prototypes.
112  */
113 extern ndmp_connection_t *ndmp_create_connection(void);
114 
115 extern void ndmp_destroy_connection(ndmp_connection_t *);
116 
117 extern void ndmp_close(ndmp_connection_t *);
118 
119 extern int ndmp_connect(ndmp_connection_t *,
120     char *,
121     ulong_t);
122 
123 extern int ndmp_run(ulong_t,
124     ndmp_con_handler_func_t);
125 
126 extern int ndmp_process_requests(ndmp_connection_t *);
127 
128 extern int ndmp_send_response(ndmp_connection_t *,
129     ndmp_error,
130     void *);
131 
132 extern int ndmp_send_request(ndmp_connection_t *,
133     ndmp_message,
134     ndmp_error,
135     void *,
136     void **);
137 
138 extern int ndmp_send_request_lock(ndmp_connection_t *,
139     ndmp_message,
140     ndmp_error,
141     void *,
142     void **);
143 
144 extern void ndmp_free_message(ndmp_connection_t *);
145 
146 extern int ndmp_get_fd(ndmp_connection_t *);
147 
148 extern void ndmp_set_client_data(ndmp_connection_t *,
149     void *);
150 
151 extern void *ndmp_get_client_data(ndmp_connection_t *);
152 
153 extern void ndmp_set_version(ndmp_connection_t *,
154     ushort_t);
155 
156 extern ushort_t ndmp_get_version(ndmp_connection_t *);
157 
158 extern void ndmp_set_authorized(ndmp_connection_t *,
159     boolean_t);
160 
161 
162 /*
163  * NDMP daemon callback functions.
164  * Called by backup/recover modules.
165  */
166 typedef char *ndmpd_get_env_func_t(void *, char *);
167 typedef int ndmpd_add_env_func_t(void *, char *, char *);
168 typedef void *ndmpd_get_name_func_t(void *, ulong_t);
169 typedef int ndmpd_dispatch_func_t(void *, boolean_t);
170 typedef void ndmpd_done_func_t(void *, int);
171 typedef int ndmpd_log_func_t(void *, char *, ...);
172 
173 typedef int ndmpd_log_func_v3_t(void *, ndmp_log_type, ulong_t,
174     char *, ...);
175 
176 
177 #define	NDMPD_SELECT_MODE_READ		1
178 #define	NDMPD_SELECT_MODE_WRITE		2
179 #define	NDMPD_SELECT_MODE_EXCEPTION	4
180 
181 typedef void ndmpd_file_handler_func_t(void *, int, ulong_t);
182 
183 typedef int ndmpd_add_file_handler_func_t(void *, void *, int, ulong_t,
184     ndmpd_file_handler_func_t *);
185 
186 typedef int ndmpd_remove_file_handler_func_t(void *, int);
187 
188 typedef int ndmpd_write_func_t(void *, char *, ulong_t);
189 
190 typedef int ndmpd_file_history_path_func_t(void *, char *, struct stat64 *,
191     u_longlong_t);
192 
193 typedef int ndmpd_file_history_dir_func_t(void *, char *, ulong_t,
194     ulong_t);
195 
196 typedef int ndmpd_file_history_node_func_t(void *, ulong_t, struct stat64 *,
197     u_longlong_t);
198 
199 typedef int ndmpd_seek_func_t(void *, u_longlong_t, u_longlong_t);
200 
201 typedef int ndmpd_read_func_t(void *, char *, ulong_t);
202 
203 typedef int ndmpd_file_recovered_func_t(void *, char *, int);
204 
205 typedef struct ndmpd_module_stats {
206 	u_longlong_t ms_bytes_processed;
207 	u_longlong_t ms_est_bytes_remaining;
208 	ulong_t ms_est_time_remaining;
209 } ndmpd_module_stats;
210 
211 /*
212  * Parameter structure passed to module start function.
213  */
214 typedef struct ndmpd_module_params {
215 	void *mp_daemon_cookie;
216 	void **mp_module_cookie;
217 	ushort_t mp_protocol_version;
218 	ndmp_data_operation mp_operation;
219 	ndmpd_module_stats *mp_stats;
220 	ndmpd_get_env_func_t *mp_get_env_func;
221 	ndmpd_add_env_func_t *mp_add_env_func;
222 	ndmpd_add_env_func_t *mp_set_env_func;
223 	ndmpd_get_name_func_t *mp_get_name_func;
224 	ndmpd_dispatch_func_t *mp_dispatch_func;
225 	ndmpd_done_func_t *mp_done_func;
226 	ndmpd_log_func_t *mp_log_func;
227 	ndmpd_add_file_handler_func_t *mp_add_file_handler_func;
228 	ndmpd_remove_file_handler_func_t *mp_remove_file_handler_func;
229 	ndmpd_write_func_t *mp_write_func;
230 	ndmpd_file_history_path_func_t *mp_file_history_path_func;
231 	ndmpd_file_history_dir_func_t *mp_file_history_dir_func;
232 	ndmpd_file_history_node_func_t *mp_file_history_node_func;
233 	ndmpd_read_func_t *mp_read_func;
234 	ndmpd_seek_func_t *mp_seek_func;
235 	ndmpd_file_recovered_func_t *mp_file_recovered_func;
236 	/*
237 	 * NDMP V3 params.
238 	 */
239 	ndmpd_log_func_v3_t *mp_log_func_v3;
240 } ndmpd_module_params_t;
241 
242 
243 #define	MOD_SETENV(m, n, v) \
244 	(*(m)->mp_set_env_func)((m)->mp_daemon_cookie, n, v)
245 
246 #define	MOD_GETENV(m, e) \
247 	(*(m)->mp_get_env_func)((m)->mp_daemon_cookie, e)
248 
249 #define	MOD_GETNAME(m, i) \
250 	(*(m)->mp_get_name_func)((m)->mp_daemon_cookie, i)
251 
252 #define	MOD_LOG(m, ...)	\
253 	(*(m)->mp_log_func)((m)->mp_daemon_cookie, __VA_ARGS__)
254 
255 #define	MOD_READ(m, b, s) \
256 	(*(m)->mp_read_func)((m)->mp_daemon_cookie, b, s)
257 
258 #define	MOD_WRITE(m, b, s) \
259 	(*(m)->mp_write_func)((m)->mp_daemon_cookie, b, s)
260 
261 #define	MOD_DONE(m, e) \
262 	(*(m)->mp_done_func)((m)->mp_daemon_cookie, e)
263 
264 #define	MOD_FILERECOVERD(m, n, e) \
265 	(*(m)->mp_file_recovered_func)((m)->mp_daemon_cookie, n, e)
266 
267 extern int ndmp_log_msg_id;
268 
269 #define	MOD_LOGV3(m, t, ...) \
270 	(*(m)->mp_log_func_v3)((m)->mp_daemon_cookie, (t), \
271 	++ndmp_log_msg_id, __VA_ARGS__)
272 
273 #define	MOD_LOGCONTV3(m, t, ...) \
274 	(*(m)->mp_log_func_v3)((m)->mp_daemon_cookie, \
275 	(t), ndmp_log_msg_id, __VA_ARGS__)
276 
277 /*
278  * Module function prototypes.
279  */
280 typedef int module_start_func_t(ndmpd_module_params_t *);
281 typedef int module_abort_func_t(void *);
282 #endif	/* _NDMP_COMMON_H */
283