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