1 /*
2  * Copyright 2009 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 #ifndef	_LIBNDMP_H
40 #define	_LIBNDMP_H
41 
42 #include <rpc/types.h>
43 #include <libscf.h>
44 #include <libnvpair.h>
45 
46 #ifdef	__cplusplus
47 extern "C" {
48 #endif
49 
50 /* NDMP supported versions */
51 #define	NDMP_V2		2
52 #define	NDMP_V3		3
53 #define	NDMP_V4		4
54 
55 /* Device type */
56 #define	NDMP_SINQ_SEQ_ACCESS_DEVICE	0x01
57 #define	NDMP_SINQ_TAPE_ROBOT		0x08
58 
59 extern int ndmp_errno;
60 
61 /* NDMP plugin module API */
62 #define	NDMP_PLUGIN_VERSION	1
63 
64 typedef struct ndmp_context {
65 	char *nc_plname;
66 	uint_t nc_plversion;
67 	void *nc_pldata;
68 	void *nc_cmds;
69 	void *nc_params;
70 } ndmp_context_t;
71 
72 typedef struct ndmp_plugin {
73 	const char *np_plid;	/* plugin identifier */
74 	uint_t np_plversion;	/* plugin version */
75 	void *np_pldata;	/* for private use by the plugin */
76 
77 	/* Plugin entry points */
78 	int (*np_pre_backup)(struct ndmp_plugin *, ndmp_context_t *,
79 		const char *);
80 	int (*np_post_backup)(struct ndmp_plugin *, ndmp_context_t *,
81 		int);
82 	int (*np_pre_restore)(struct ndmp_plugin *, ndmp_context_t *,
83 		const char *, const char *);
84 	int (*np_post_restore)(struct ndmp_plugin *, ndmp_context_t *,
85 		int);
86 } ndmp_plugin_t;
87 
88 typedef enum ndmp_log_dma_type {
89 	NDMP_LOGD_NORMAL = 0,
90 	NDMP_LOGD_DEBUG = 1,
91 	NDMP_LOGD_ERROR = 2,
92 	NDMP_LOGD_WARNING = 3
93 } ndmp_log_dma_type_t;
94 
95 /* libndmp error codes */
96 #define	ENDMP_BASE	2000
97 enum {
98 	ENDMP_DOOR_SRV_TIMEOUT = ENDMP_BASE,
99 	ENDMP_INVALID_ARG,
100 	ENDMP_DOOR_SRV_OPERATION,
101 	ENDMP_DOOR_OPEN,
102 	ENDMP_MEM_ALLOC,
103 	ENDMP_DOOR_ENCODE_START,
104 	ENDMP_DOOR_ENCODE_FINISH,
105 	ENDMP_DOOR_DECODE_FINISH,
106 	ENDMP_SMF_PERM,
107 	ENDMP_SMF_INTERNAL,
108 	ENDMP_SMF_PROP,
109 	ENDMP_SMF_PROP_GRP
110 };
111 
112 /* Tape device open mode */
113 typedef enum ndmp_tp_open_mode {
114 	NDMP_TP_READ_MODE,
115 	NDMP_TP_WRITE_MODE,
116 	NDMP_TP_RAW_MODE,
117 	NDMP_TP_RAW1_MODE = 0x7fffffff,
118 	NDMP_TP_RAW2_MODE = NDMP_TP_RAW_MODE
119 } ndmp_tp_open_mode_t;
120 
121 /* Mover state */
122 typedef enum ndmp_mv_state {
123 	NDMP_MV_STATE_IDLE,
124 	NDMP_MV_STATE_LISTEN,
125 	NDMP_MV_STATE_ACTIVE,
126 	NDMP_MV_STATE_PAUSED,
127 	NDMP_MV_STATE_HALTED
128 } ndmp_mv_state_t;
129 
130 /* Mover mode */
131 typedef enum ndmp_mv_mode {
132 	NDMP_MV_MODE_READ,
133 	NDMP_MV_MODE_WRITE,
134 	NDMP_MV_MODE_NOACTION
135 } ndmp_mv_mode_t;
136 
137 /* Mover pause reson */
138 typedef enum ndmp_mv_pause_reason {
139 	NDMP_MV_PAUSE_NA,
140 	NDMP_MV_PAUSE_EOM,
141 	NDMP_MV_PAUSE_EOF,
142 	NDMP_MV_PAUSE_SEEK,
143 	NDMP_MV_PAUSE_MEDIA_ERROR,
144 	NDMP_MV_PAUSE_EOW
145 } ndmp_mv_pause_reason_t;
146 
147 /* Mover halt reason */
148 typedef enum ndmp_mv_halt_reason {
149 	NDMP_MV_HALT_NA,
150 	NDMP_MV_HALT_CONNECT_CLOSED,
151 	NDMP_MV_HALT_ABORTED,
152 	NDMP_MV_HALT_INTERNAL_ERROR,
153 	NDMP_MV_HALT_CONNECT_ERROR,
154 	NDMP_MV_HALT_MEDIA_ERROR
155 } ndmp_mv_halt_reason_t;
156 
157 /* Address type */
158 typedef enum ndmp_ad_type {
159 	NDMP_AD_LOCAL,
160 	NDMP_AD_TCP,
161 	NDMP_AD_FC,
162 	NDMP_AD_IPC
163 } ndmp_ad_type_t;
164 
165 /* NDMP data operation */
166 typedef enum ndmp_dt_operation {
167 	NDMP_DT_OP_NOACTION,
168 	NDMP_DT_OP_BACKUP,
169 	NDMP_DT_OP_RECOVER,
170 	NDMP_DT_OP_RECOVER_FILEHIST
171 } ndmp_dt_operation_t;
172 
173 /* NDMP data state */
174 typedef enum ndmp_dt_state {
175 	NDMP_DT_STATE_IDLE,
176 	NDMP_DT_STATE_ACTIVE,
177 	NDMP_DT_STATE_HALTED,
178 	NDMP_DT_STATE_LISTEN,
179 	NDMP_DT_STATE_CONNECTED
180 } ndmp_dt_state_t;
181 
182 /* NDMP data halt reason */
183 typedef enum ndmp_dt_halt_reason {
184 	NDMP_DT_HALT_NA,
185 	NDMP_DT_HALT_SUCCESSFUL,
186 	NDMP_DT_HALT_ABORTED,
187 	NDMP_DT_HALT_INTERNAL_ERROR,
188 	NDMP_DT_HALT_CONNECT_ERROR
189 } ndmp_dt_halt_reason_t;
190 
191 /* Device information structure */
192 typedef struct ndmp_devinfo {
193 	uint_t nd_dev_type;	/* SCSI device type */
194 	char *nd_name;		/* Device name */
195 	uint_t nd_lun;		/* Lun number */
196 	uint_t nd_sid;		/* Scsi id */
197 	char *nd_vendor;	/* Vendor name */
198 	char *nd_product;	/* Product name */
199 	char *nd_revision;	/* Revision */
200 	char *nd_serial;	/* Serial */
201 	char *nd_wwn;		/* World wide name */
202 } ndmp_devinfo_t;
203 
204 /* Scsi device info sturcture */
205 typedef struct ndmp_scsi {
206 	int ns_scsi_open;		/* Scsi device open */
207 					/* -1 if not open */
208 	char *ns_adapter_name;		/* Scsi adapter name */
209 	int ns_valid_target_set;	/* Scsi valid target */
210 	/* scsi_id and lun are set only if valid_target_set is set */
211 	int ns_scsi_id;			/* Scsi id */
212 	int ns_lun;			/* Scsi lun */
213 } ndmp_scsi_t;
214 
215 typedef struct ndmp_tape {
216 	int nt_fd;			/* Tape device file descriptor */
217 	/* The data below is set only if "fd" is not -1 */
218 	ulong_t nt_rec_count;		/* Number of records written */
219 	ndmp_tp_open_mode_t nt_mode;	/* Tape device open mode */
220 	char *nt_dev_name;		/* Device name */
221 	char *nt_adapter_name;		/* Adapter name */
222 	int nt_sid;			/* Scsi id	*/
223 	int nt_lun;			/* Lun number	*/
224 } ndmp_tape_t;
225 
226 /* NDMP mover info structure */
227 typedef struct ndmp_mover {
228 	ndmp_mv_state_t nm_state;		/* Current state */
229 	ndmp_mv_mode_t nm_mode;			/* Current mode */
230 	ndmp_mv_pause_reason_t nm_pause_reason;	/* Current reason */
231 	ndmp_mv_halt_reason_t nm_halt_reason;	/* Current reason */
232 	ulong_t	nm_rec_size;			/* Tape I/O record size */
233 	ulong_t	nm_rec_num;			/* Current record num */
234 	u_longlong_t nm_mov_pos;		/* Current data stream pos */
235 	u_longlong_t nm_window_offset;		/* Valid data window begin */
236 	u_longlong_t nm_window_length;		/* Valid data window length */
237 	int nm_sock;				/* Data conn socket */
238 
239 	/* Filled in V3 and V4 only */
240 	int nm_listen_sock;			/* Data conn listen socket */
241 	ndmp_ad_type_t nm_addr_type;		/* Current address type */
242 	char *nm_tcp_addr;			/* Only if addr_type is tcp */
243 } ndmp_mover_t;
244 
245 typedef struct ndmp_dt_name {
246 	char *nn_name;
247 	char *nn_dest;
248 } ndmp_dt_name_t;
249 
250 /* NDMP name/value pair structure */
251 typedef struct ndmp_dt_pval {
252 	char *np_name;
253 	char *np_value;
254 } ndmp_dt_pval_t;
255 
256 typedef struct ndmp_dt_name_v3 {
257 	char *nn3_opath;
258 	char *nn3_dpath;
259 	u_longlong_t nn3_node;
260 	u_longlong_t nn3_fh_info;
261 } ndmp_dt_name_v3_t;
262 
263 typedef struct ndmp_dt_v3 {
264 	int dv3_listen_sock;
265 	u_longlong_t dv3_bytes_processed;
266 	ndmp_dt_name_v3_t *dv3_nlist;		/* V3 recover file list */
267 } ndmp_dt_v3_t;
268 
269 /* NDMP data structure */
270 typedef struct ndmp_data {
271 	ndmp_dt_operation_t nd_oper;		/* Current operation */
272 	ndmp_dt_state_t nd_state;		/* Current state */
273 	ndmp_dt_halt_reason_t nd_halt_reason;	/* Current reason */
274 	int nd_sock;				/* Listen and data socket */
275 	ndmp_ad_type_t nd_addr_type;		/* Current address type */
276 	char *nd_tcp_addr;			/* Only if addr_type is tcp */
277 	int nd_abort;				/* Abort operation flag */
278 						/* 0 = No, otherwise Yes */
279 	u_longlong_t nd_read_offset;		/* Data read seek offset */
280 	u_longlong_t nd_read_length;		/* Data read length */
281 	u_longlong_t nd_total_size;		/* Backup data size */
282 	ulong_t nd_env_len;			/* Environment length */
283 	ndmp_dt_pval_t *nd_env;			/* Environment from backup */
284 						/* or recover request */
285 	ulong_t nld_nlist_len;			/* Recover file list length */
286 	union {
287 		/* Filled in V2 */
288 		ndmp_dt_name_t *nld_nlist;	/* Recover file list */
289 		/* Filled in V3 */
290 		ndmp_dt_v3_t nld_dt_v3;		/* V3 data */
291 	} nd_nlist;
292 } ndmp_data_t;
293 
294 /* NDMP session information  */
295 typedef struct ndmp_session_info {
296 	int nsi_sid;		/* Session id   */
297 	int nsi_pver;		/* NDMP protocol version */
298 	int nsi_auth;		/* Authorized ? 0="no", else "yes" */
299 	int nsi_eof;		/* Connection EOF flag */
300 				/* 0="no", else "yes" */
301 	char *nsi_cl_addr;	/* Client address */
302 	ndmp_scsi_t nsi_scsi;	/* Scsi device information */
303 	ndmp_tape_t nsi_tape;	/* Tape device information */
304 	ndmp_mover_t nsi_mover;	/* Mover information */
305 	ndmp_data_t nsi_data;	/* Data information */
306 } ndmp_session_info_t;
307 
308 /* Stats data */
309 typedef struct ndmp_stat {
310 	int ns_trun;		/* Number of worker threads running */
311 	int ns_twait;		/* Number of blocked worker threads */
312 	int ns_nbk;		/* Number of backups operations running */
313 	int ns_nrs;		/* Number of restores operations running */
314 	int ns_rfile;		/* Number of files being read */
315 	int ns_wfile;		/* Number of files being written */
316 	uint64_t ns_rdisk;	/* Number of disk blocks being read */
317 	uint64_t ns_wdisk;	/* Number of disk blocks being written */
318 	uint64_t ns_rtape;	/* Number of tape blocks being read */
319 	uint64_t ns_wtape;	/* Number of tape blocks being written */
320 } ndmp_stat_t;
321 
322 /* Common encode/decode functions used by door clients/servers.  */
323 typedef struct ndmp_door_ctx {
324 	char *ptr;
325 	char *start_ptr;
326 	char *end_ptr;
327 	int status;
328 } ndmp_door_ctx_t;
329 
330 extern int ndmp_get_devinfo(ndmp_devinfo_t **, size_t *);
331 extern void ndmp_get_devinfo_free(ndmp_devinfo_t *, size_t);
332 extern int ndmp_get_dbglevel(void);
333 extern int ndmp_get_session_info(ndmp_session_info_t **, size_t *);
334 extern void ndmp_get_session_info_free(ndmp_session_info_t *, size_t);
335 extern int ndmp_get_stats(ndmp_stat_t *);
336 extern int ndmp_terminate_session(int);
337 extern int ndmp_set_dbglevel(int);
338 extern const char *ndmp_strerror(int);
339 extern int ndmp_door_status(void);
340 extern int ndmp_get_prop(char *, char **);
341 extern int ndmp_set_prop(char *, char *);
342 extern int ndmp_service_refresh(void);
343 extern char *ndmp_base64_encode(char *);
344 extern char *ndmp_base64_decode(char *);
345 extern ndmp_door_ctx_t *ndmp_door_decode_start(char *, int);
346 extern int ndmp_door_decode_finish(ndmp_door_ctx_t *);
347 extern ndmp_door_ctx_t *ndmp_door_encode_start(char *, int);
348 extern int ndmp_door_encode_finish(ndmp_door_ctx_t *, unsigned int *);
349 extern int32_t ndmp_door_get_int32(ndmp_door_ctx_t *);
350 extern uint32_t ndmp_door_get_uint32(ndmp_door_ctx_t *);
351 extern char *ndmp_door_get_string(ndmp_door_ctx_t *);
352 extern void ndmp_door_put_int32(ndmp_door_ctx_t *, int32_t);
353 extern void ndmp_door_put_uint32(ndmp_door_ctx_t *, uint32_t);
354 extern void ndmp_door_put_string(ndmp_door_ctx_t *, char *);
355 extern void ndmp_door_free_string(char *);
356 extern int64_t ndmp_door_get_int64(ndmp_door_ctx_t *);
357 extern uint64_t ndmp_door_get_uint64(ndmp_door_ctx_t *);
358 extern void ndmp_door_put_uint64(ndmp_door_ctx_t *, uint64_t);
359 extern void ndmp_door_put_short(ndmp_door_ctx_t *, short);
360 extern short ndmp_door_get_short(ndmp_door_ctx_t *);
361 extern void ndmp_door_put_ushort(ndmp_door_ctx_t *, unsigned short);
362 extern unsigned short ndmp_door_get_ushort(ndmp_door_ctx_t *);
363 extern void ndmp_door_put_buf(ndmp_door_ctx_t *, unsigned char *, int);
364 extern int ndmp_door_get_buf(ndmp_door_ctx_t *, unsigned char *, int);
365 
366 extern int ndmp_include_zfs(ndmp_context_t *, const char *);
367 extern int ndmp_iter_zfs(ndmp_context_t *, int (*)(nvlist_t *, void *), void *);
368 extern uint_t ndmp_context_get_version(ndmp_context_t *);
369 extern void ndmp_context_set_specific(ndmp_context_t *, void *);
370 extern void *ndmp_context_get_specific(ndmp_context_t *);
371 void ndmp_log_dma(ndmp_context_t *, ndmp_log_dma_type_t, const char *, ...);
372 
373 #ifdef	__cplusplus
374 }
375 #endif
376 
377 #endif /* _LIBNDMP_H */
378