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