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 * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
42 */
43
44#ifndef	_NDMP_COMMON_H
45#define	_NDMP_COMMON_H
46
47#include <thread.h>
48#include <synch.h>
49#include "ndmpd_log.h"
50#include "ndmp.h"
51#include <unistd.h>
52#include <sys/types.h>
53#include <rpc/rpc.h>
54#include <sys/stat.h>
55#include <stdio.h>
56#include <bsm/adt.h>
57#include <bsm/adt_event.h>
58
59
60#define	XDR_AND_SIZE(func) (bool_t(*)(XDR*, ...))xdr_##func, sizeof (func)
61#define	AUTH_REQUIRED	TRUE
62#define	AUTH_NOT_REQUIRED	FALSE
63#define	NDMP_EOM_MAGIC	"PRCMEOM"
64#define	KILOBYTE	1024
65
66#define	INT_MAXCMD	12
67
68extern mutex_t ndmpd_zfs_fd_lock;
69
70/* Connection data structure. */
71typedef struct msg_info {
72	ndmp_header mi_hdr;
73	struct ndmp_msg_handler *mi_handler;
74	void *mi_body;
75} msg_info_t;
76
77typedef struct ndmp_connection {
78	int conn_sock;
79	XDR conn_xdrs;
80	ulong_t conn_my_sequence;
81	boolean_t conn_authorized;
82	boolean_t conn_eof;
83	msg_info_t conn_msginfo; /* received request or reply message */
84	ushort_t conn_version;
85	void *conn_client_data;
86	mutex_t conn_lock;
87	adt_session_data_t *conn_ah;
88} ndmp_connection_t;
89
90typedef void (*ndmp_con_handler_func_t) (struct ndmp_connection *);
91
92typedef void ndmp_msg_handler_func_t(struct ndmp_connection *, void *);
93
94
95typedef struct ndmp_msg_handler {
96	ndmp_msg_handler_func_t *mh_func;
97	bool_t(*mh_xdr_request) (XDR *xdrs, ...);
98	int mh_sizeof_request;
99	bool_t(*mh_xdr_reply) (XDR *xdrs, ...);
100	int mh_sizeof_reply;
101} ndmp_msg_handler_t;
102
103typedef struct ndmp_handler {
104	int hd_cnt;
105	struct hd_messages {
106		ndmp_message hm_message;
107		boolean_t hm_auth_required;
108		ndmp_msg_handler_t hm_msg_v[3];
109	} hd_msgs[INT_MAXCMD];
110} ndmp_handler_t;
111
112/*
113 * Function prototypes.
114 */
115extern ndmp_connection_t *ndmp_create_connection(void);
116
117extern void ndmp_destroy_connection(ndmp_connection_t *);
118
119extern void ndmp_close(ndmp_connection_t *);
120
121extern int ndmp_connect(ndmp_connection_t *,
122    char *,
123    ulong_t);
124
125extern int ndmp_run(ulong_t,
126    ndmp_con_handler_func_t);
127
128extern int ndmp_process_requests(ndmp_connection_t *);
129
130extern int ndmp_send_response(ndmp_connection_t *,
131    ndmp_error,
132    void *);
133
134extern int ndmp_send_request(ndmp_connection_t *,
135    ndmp_message,
136    ndmp_error,
137    void *,
138    void **);
139
140extern int ndmp_send_request_lock(ndmp_connection_t *,
141    ndmp_message,
142    ndmp_error,
143    void *,
144    void **);
145
146extern void ndmp_free_message(ndmp_connection_t *);
147
148extern int ndmp_get_fd(ndmp_connection_t *);
149
150extern void ndmp_set_client_data(ndmp_connection_t *,
151    void *);
152
153extern void *ndmp_get_client_data(ndmp_connection_t *);
154
155extern void ndmp_set_version(ndmp_connection_t *,
156    ushort_t);
157
158extern ushort_t ndmp_get_version(ndmp_connection_t *);
159
160extern void ndmp_set_authorized(ndmp_connection_t *,
161    boolean_t);
162
163
164/*
165 * NDMP daemon callback functions.
166 * Called by backup/recover modules.
167 */
168typedef char *ndmpd_get_env_func_t(void *, char *);
169typedef int ndmpd_add_env_func_t(void *, char *, char *);
170typedef void *ndmpd_get_name_func_t(void *, ulong_t);
171typedef int ndmpd_dispatch_func_t(void *, boolean_t);
172typedef void ndmpd_done_func_t(void *, int);
173typedef int ndmpd_log_func_t(void *, char *, ...);
174
175typedef int ndmpd_log_func_v3_t(void *, ndmp_log_type, ulong_t,
176    char *, ...);
177
178
179#define	NDMPD_SELECT_MODE_READ		1
180#define	NDMPD_SELECT_MODE_WRITE		2
181#define	NDMPD_SELECT_MODE_EXCEPTION	4
182
183typedef void ndmpd_file_handler_func_t(void *, int, ulong_t);
184
185typedef int ndmpd_add_file_handler_func_t(void *, void *, int, ulong_t,
186    ndmpd_file_handler_func_t *);
187
188typedef int ndmpd_remove_file_handler_func_t(void *, int);
189
190typedef int ndmpd_write_func_t(void *, char *, ulong_t);
191
192typedef int ndmpd_file_history_path_func_t(void *, char *, struct stat64 *,
193    u_longlong_t);
194
195typedef int ndmpd_file_history_dir_func_t(void *, char *, ulong_t,
196    ulong_t);
197
198typedef int ndmpd_file_history_node_func_t(void *, ulong_t, struct stat64 *,
199    u_longlong_t);
200
201typedef int ndmpd_seek_func_t(void *, u_longlong_t, u_longlong_t);
202
203typedef int ndmpd_read_func_t(void *, char *, ulong_t);
204
205typedef int ndmpd_file_recovered_func_t(void *, char *, int);
206
207typedef struct ndmpd_module_stats {
208	u_longlong_t ms_bytes_processed;
209	u_longlong_t ms_est_bytes_remaining;
210	ulong_t ms_est_time_remaining;
211} ndmpd_module_stats;
212
213/*
214 * Parameter structure passed to module start function.
215 */
216typedef struct ndmpd_module_params {
217	void *mp_daemon_cookie;
218	void **mp_module_cookie;
219	ushort_t mp_protocol_version;
220	ndmp_data_operation mp_operation;
221	ndmpd_module_stats *mp_stats;
222	ndmpd_get_env_func_t *mp_get_env_func;
223	ndmpd_add_env_func_t *mp_add_env_func;
224	ndmpd_add_env_func_t *mp_set_env_func;
225	ndmpd_get_name_func_t *mp_get_name_func;
226	ndmpd_dispatch_func_t *mp_dispatch_func;
227	ndmpd_done_func_t *mp_done_func;
228	ndmpd_log_func_t *mp_log_func;
229	ndmpd_add_file_handler_func_t *mp_add_file_handler_func;
230	ndmpd_remove_file_handler_func_t *mp_remove_file_handler_func;
231	ndmpd_write_func_t *mp_write_func;
232	ndmpd_file_history_path_func_t *mp_file_history_path_func;
233	ndmpd_file_history_dir_func_t *mp_file_history_dir_func;
234	ndmpd_file_history_node_func_t *mp_file_history_node_func;
235	ndmpd_read_func_t *mp_read_func;
236	ndmpd_seek_func_t *mp_seek_func;
237	ndmpd_file_recovered_func_t *mp_file_recovered_func;
238	/*
239	 * NDMP V3 params.
240	 */
241	ndmpd_log_func_v3_t *mp_log_func_v3;
242} ndmpd_module_params_t;
243
244#define	MOD_ADDENV(m, n, v) \
245	(*(m)->mp_add_env_func)((m)->mp_daemon_cookie, n, v)
246
247#define	MOD_SETENV(m, n, v) \
248	(*(m)->mp_set_env_func)((m)->mp_daemon_cookie, n, v)
249
250#define	MOD_GETENV(m, e) \
251	(*(m)->mp_get_env_func)((m)->mp_daemon_cookie, e)
252
253#define	MOD_GETNAME(m, i) \
254	(*(m)->mp_get_name_func)((m)->mp_daemon_cookie, i)
255
256#define	MOD_LOG(m, ...)	\
257	(*(m)->mp_log_func)((m)->mp_daemon_cookie, __VA_ARGS__)
258
259#define	MOD_READ(m, b, s) \
260	(*(m)->mp_read_func)((m)->mp_daemon_cookie, b, s)
261
262#define	MOD_WRITE(m, b, s) \
263	(*(m)->mp_write_func)((m)->mp_daemon_cookie, b, s)
264
265#define	MOD_DONE(m, e) \
266	(*(m)->mp_done_func)((m)->mp_daemon_cookie, e)
267
268#define	MOD_FILERECOVERD(m, n, e) \
269	(*(m)->mp_file_recovered_func)((m)->mp_daemon_cookie, n, e)
270
271extern int ndmp_log_msg_id;
272
273#define	MOD_LOGV3(m, t, ...) \
274	(*(m)->mp_log_func_v3)((m)->mp_daemon_cookie, (t), \
275	++ndmp_log_msg_id, __VA_ARGS__)
276
277#define	MOD_LOGCONTV3(m, t, ...) \
278	(*(m)->mp_log_func_v3)((m)->mp_daemon_cookie, \
279	(t), ndmp_log_msg_id, __VA_ARGS__)
280
281/*
282 * Module function prototypes.
283 */
284typedef int module_start_func_t(void *);
285typedef int module_abort_func_t(void *);
286#endif	/* _NDMP_COMMON_H */
287