1/*
2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3 */
4
5/*
6 * BSD 3 Clause License
7 *
8 * Copyright (c) 2007, The Storage Networking Industry Association.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 	- Redistributions of source code must retain the above copyright
14 *	  notice, this list of conditions and the following disclaimer.
15 *
16 * 	- Redistributions in binary form must reproduce the above copyright
17 *	  notice, this list of conditions and the following disclaimer in
18 *	  the documentation and/or other materials provided with the
19 *	  distribution.
20 *
21 *	- Neither the name of The Storage Networking Industry Association (SNIA)
22 *	  nor the names of its contributors may be used to endorse or promote
23 *	  products derived from this software without specific prior written
24 *	  permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38/* Copyright (c) 2007, The Storage Networking Industry Association. */
39/* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */
40
41#ifndef	_NDMP_COMMON_H
42#define	_NDMP_COMMON_H
43
44#include <thread.h>
45#include <synch.h>
46#include "ndmpd_log.h"
47#include "ndmp.h"
48#include <unistd.h>
49#include <sys/types.h>
50#include <rpc/rpc.h>
51#include <sys/stat.h>
52#include <stdio.h>
53#include <bsm/adt.h>
54#include <bsm/adt_event.h>
55
56
57#define	XDR_AND_SIZE(func) (bool_t(*)(XDR*, ...))xdr_##func, sizeof (func)
58#define	AUTH_REQUIRED	TRUE
59#define	AUTH_NOT_REQUIRED	FALSE
60#define	NDMP_EOM_MAGIC	"PRCMEOM"
61#define	KILOBYTE	1024
62
63#define	INT_MAXCMD	12
64
65extern mutex_t log_lock;
66extern mutex_t ndmpd_zfs_fd_lock;
67
68/* Connection data structure. */
69typedef struct msg_info {
70	ndmp_header mi_hdr;
71	struct ndmp_msg_handler *mi_handler;
72	void *mi_body;
73} msg_info_t;
74
75typedef 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
88typedef void (*ndmp_con_handler_func_t) (struct ndmp_connection *);
89
90typedef void ndmp_msg_handler_func_t(struct ndmp_connection *, void *);
91
92
93typedef 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
101typedef 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 */
113extern ndmp_connection_t *ndmp_create_connection(void);
114
115extern void ndmp_destroy_connection(ndmp_connection_t *);
116
117extern void ndmp_close(ndmp_connection_t *);
118
119extern int ndmp_connect(ndmp_connection_t *,
120    char *,
121    ulong_t);
122
123extern int ndmp_run(ulong_t,
124    ndmp_con_handler_func_t);
125
126extern int ndmp_process_requests(ndmp_connection_t *);
127
128extern int ndmp_send_response(ndmp_connection_t *,
129    ndmp_error,
130    void *);
131
132extern int ndmp_send_request(ndmp_connection_t *,
133    ndmp_message,
134    ndmp_error,
135    void *,
136    void **);
137
138extern int ndmp_send_request_lock(ndmp_connection_t *,
139    ndmp_message,
140    ndmp_error,
141    void *,
142    void **);
143
144extern void ndmp_free_message(ndmp_connection_t *);
145
146extern int ndmp_get_fd(ndmp_connection_t *);
147
148extern void ndmp_set_client_data(ndmp_connection_t *,
149    void *);
150
151extern void *ndmp_get_client_data(ndmp_connection_t *);
152
153extern void ndmp_set_version(ndmp_connection_t *,
154    ushort_t);
155
156extern ushort_t ndmp_get_version(ndmp_connection_t *);
157
158extern 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 */
166typedef char *ndmpd_get_env_func_t(void *, char *);
167typedef int ndmpd_add_env_func_t(void *, char *, char *);
168typedef void *ndmpd_get_name_func_t(void *, ulong_t);
169typedef int ndmpd_dispatch_func_t(void *, boolean_t);
170typedef void ndmpd_done_func_t(void *, int);
171typedef int ndmpd_log_func_t(void *, char *, ...);
172
173typedef 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
181typedef void ndmpd_file_handler_func_t(void *, int, ulong_t);
182
183typedef int ndmpd_add_file_handler_func_t(void *, void *, int, ulong_t,
184    ndmpd_file_handler_func_t *);
185
186typedef int ndmpd_remove_file_handler_func_t(void *, int);
187
188typedef int ndmpd_write_func_t(void *, char *, ulong_t);
189
190typedef int ndmpd_file_history_path_func_t(void *, char *, struct stat64 *,
191    u_longlong_t);
192
193typedef int ndmpd_file_history_dir_func_t(void *, char *, ulong_t,
194    ulong_t);
195
196typedef int ndmpd_file_history_node_func_t(void *, ulong_t, struct stat64 *,
197    u_longlong_t);
198
199typedef int ndmpd_seek_func_t(void *, u_longlong_t, u_longlong_t);
200
201typedef int ndmpd_read_func_t(void *, char *, ulong_t);
202
203typedef int ndmpd_file_recovered_func_t(void *, char *, int);
204
205typedef 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 */
214typedef 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#define	MOD_ADDENV(m, n, v) \
243	(*(m)->mp_add_env_func)((m)->mp_daemon_cookie, n, v)
244
245#define	MOD_SETENV(m, n, v) \
246	(*(m)->mp_set_env_func)((m)->mp_daemon_cookie, n, v)
247
248#define	MOD_GETENV(m, e) \
249	(*(m)->mp_get_env_func)((m)->mp_daemon_cookie, e)
250
251#define	MOD_GETNAME(m, i) \
252	(*(m)->mp_get_name_func)((m)->mp_daemon_cookie, i)
253
254#define	MOD_LOG(m, ...)	\
255	(*(m)->mp_log_func)((m)->mp_daemon_cookie, __VA_ARGS__)
256
257#define	MOD_READ(m, b, s) \
258	(*(m)->mp_read_func)((m)->mp_daemon_cookie, b, s)
259
260#define	MOD_WRITE(m, b, s) \
261	(*(m)->mp_write_func)((m)->mp_daemon_cookie, b, s)
262
263#define	MOD_DONE(m, e) \
264	(*(m)->mp_done_func)((m)->mp_daemon_cookie, e)
265
266#define	MOD_FILERECOVERD(m, n, e) \
267	(*(m)->mp_file_recovered_func)((m)->mp_daemon_cookie, n, e)
268
269extern int ndmp_log_msg_id;
270
271#define	MOD_LOGV3(m, t, ...) \
272	(*(m)->mp_log_func_v3)((m)->mp_daemon_cookie, (t), \
273	++ndmp_log_msg_id, __VA_ARGS__)
274
275#define	MOD_LOGCONTV3(m, t, ...) \
276	(*(m)->mp_log_func_v3)((m)->mp_daemon_cookie, \
277	(t), ndmp_log_msg_id, __VA_ARGS__)
278
279/*
280 * Module function prototypes.
281 */
282typedef int module_start_func_t(void *);
283typedef int module_abort_func_t(void *);
284#endif	/* _NDMP_COMMON_H */
285