xref: /illumos-gate/usr/src/cmd/ndmpd/ndmp/ndmpd_zfs.c (revision 19b94df933188a15d4f0d6c568f0bab3f127892e)
1 /*
2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2011 by Delphix. All rights reserved.
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 /* Copyright (c) 2007, The Storage Networking Industry Association. */
40 /* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/lwp.h>
45 #include <sys/fs/zfs.h>
46 #include <sys/mtio.h>
47 #include <sys/time.h>
48 #include <unistd.h>
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <ctype.h>
53 #include <libzfs.h>
54 #include <stdio.h>
55 #include "ndmpd_common.h"
56 #include "ndmpd.h"
57 
58 typedef struct {
59 	char nzs_findprop[ZFS_MAXPROPLEN]; /* prop substring to find */
60 	char nzs_snapname[ZFS_MAXNAMELEN]; /* snap's name */
61 	char nzs_snapprop[ZFS_MAXPROPLEN]; /* snap's prop value */
62 	char nzs_snapskip[ZFS_MAXPROPLEN]; /* snap to skip */
63 	uint32_t nzs_prop_major;	   /* property major version */
64 	uint32_t nzs_prop_minor;	   /* property minor version */
65 } ndmpd_zfs_snapfind_t;
66 
67 mutex_t ndmpd_zfs_fd_lock;
68 
69 static int ndmpd_zfs_open_fds(ndmpd_zfs_args_t *);
70 static void ndmpd_zfs_close_fds(ndmpd_zfs_args_t *);
71 
72 static void ndmpd_zfs_close_one_fd(ndmpd_zfs_args_t *, int);
73 
74 static int ndmpd_zfs_header_write(ndmpd_session_t *);
75 static int ndmpd_zfs_header_read(ndmpd_zfs_args_t *);
76 
77 static int ndmpd_zfs_backup_send_read(ndmpd_zfs_args_t *);
78 static int ndmpd_zfs_backup_tape_write(ndmpd_zfs_args_t *);
79 
80 static int ndmpd_zfs_restore(ndmpd_zfs_args_t *);
81 static int ndmpd_zfs_restore_tape_read(ndmpd_zfs_args_t *);
82 static int ndmpd_zfs_restore_recv_write(ndmpd_zfs_args_t *);
83 
84 static int ndmpd_zfs_reader_writer(ndmpd_zfs_args_t *, int **, int **);
85 
86 static int ndmpd_zfs_addenv_backup_size(ndmpd_zfs_args_t *, u_longlong_t);
87 
88 static boolean_t ndmpd_zfs_backup_pathvalid(ndmpd_zfs_args_t *);
89 static int ndmpd_zfs_backup_getpath(ndmpd_zfs_args_t *, char *, int);
90 static int ndmpd_zfs_backup_getenv(ndmpd_zfs_args_t *);
91 
92 static boolean_t ndmpd_zfs_restore_pathvalid(ndmpd_zfs_args_t *);
93 static int ndmpd_zfs_restore_getpath(ndmpd_zfs_args_t *);
94 static int ndmpd_zfs_restore_getenv(ndmpd_zfs_args_t *);
95 
96 static int ndmpd_zfs_getenv(ndmpd_zfs_args_t *);
97 static int ndmpd_zfs_getenv_zfs_mode(ndmpd_zfs_args_t *);
98 static int ndmpd_zfs_getenv_zfs_force(ndmpd_zfs_args_t *);
99 static int ndmpd_zfs_getenv_level(ndmpd_zfs_args_t *);
100 static int ndmpd_zfs_getenv_update(ndmpd_zfs_args_t *);
101 static int ndmpd_zfs_getenv_dmp_name(ndmpd_zfs_args_t *);
102 static int ndmpd_zfs_getenv_zfs_backup_size(ndmpd_zfs_args_t *);
103 
104 static boolean_t ndmpd_zfs_dmp_name_valid(ndmpd_zfs_args_t *, char *);
105 static boolean_t ndmpd_zfs_is_incremental(ndmpd_zfs_args_t *);
106 static int ndmpd_zfs_send_fhist(ndmpd_zfs_args_t *);
107 
108 static int ndmpd_zfs_snapshot_prepare(ndmpd_zfs_args_t *);
109 static int ndmpd_zfs_snapshot_cleanup(ndmpd_zfs_args_t *, int);
110 static int ndmpd_zfs_snapshot_create(ndmpd_zfs_args_t *);
111 static int ndmpd_zfs_snapshot_unuse(ndmpd_zfs_args_t *,
112     boolean_t, ndmpd_zfs_snapfind_t *);
113 static boolean_t ndmpd_zfs_snapshot_ndmpd_generated(char *);
114 static int ndmpd_zfs_snapshot_find(ndmpd_zfs_args_t *, ndmpd_zfs_snapfind_t *);
115 
116 static int ndmpd_zfs_snapshot_prop_find(zfs_handle_t *, void *);
117 static int ndmpd_zfs_snapshot_prop_get(zfs_handle_t *, char *);
118 static int ndmpd_zfs_snapshot_prop_add(ndmpd_zfs_args_t *);
119 static int ndmpd_zfs_snapshot_prop_create(ndmpd_zfs_args_t *, char *,
120     boolean_t *);
121 static int ndmpd_zfs_prop_create_subprop(ndmpd_zfs_args_t *, char *, int,
122     boolean_t);
123 static int ndmpd_zfs_snapshot_prop_remove(ndmpd_zfs_args_t *,
124     ndmpd_zfs_snapfind_t *);
125 static boolean_t ndmpd_zfs_prop_version_check(char *, uint32_t *, uint32_t *);
126 
127 static int ndmpd_zfs_snapname_create(ndmpd_zfs_args_t *, char *, int);
128 
129 static void ndmpd_zfs_zerr_dma_log(ndmpd_zfs_args_t *);
130 
131 static int ndmpd_zfs_backup(ndmpd_zfs_args_t *);
132 
133 /*
134  * Syntax for com.sun.ndmp:incr property value:
135  *	#.#.n|u/$LEVEL.$DMP_NAME.$ZFS_MODE(/ ...)
136  *
137  * where
138  *	#.# is the version number
139  *	'n' means ndmp-generated; 'u' means user-supplied
140  *	$LEVEL: backup (incremental) level [0-9]
141  *	$DMP_NAME: set name [default: "level"]
142  *	$ZFS_MODE: d | r | p [for dataset, recursive, or package]
143  *
144  * Examples:
145  *
146  * 	0.0.n/0.bob.p
147  * 	0.0.u/1.bob.p/0.jane.d
148  *
149  * Note: NDMPD_ZFS_SUBPROP_MAX is calculated based on ZFS_MAXPROPLEN
150  */
151 
152 #define	NDMPD_ZFS_PROP_INCR "com.sun.ndmp:incr"
153 #define	NDMPD_ZFS_SUBPROP_MAX	28
154 
155 /*
156  * NDMPD_ZFS_LOG_ZERR
157  *
158  * As coded, there should be no races in the retrieval of the ZFS errno
159  * from the ndmpd_zfs_args->nz_zlibh.  I.e., for a given ndmpd_zfs backup
160  * or restore, there should only ever be one ZFS library call taking place
161  * at any one moment in time.
162  */
163 
164 #define	NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, ...) {			\
165 	NDMP_LOG(LOG_ERR, __VA_ARGS__);					\
166 	NDMP_LOG(LOG_ERR, "%s--%s",					\
167 	    libzfs_error_action((ndmpd_zfs_args)->nz_zlibh),           	\
168 	    libzfs_error_description((ndmpd_zfs_args)->nz_zlibh));     	\
169 	ndmpd_zfs_zerr_dma_log((ndmpd_zfs_args));			\
170 }
171 
172 int
173 ndmpd_zfs_init(ndmpd_session_t *session)
174 {
175 	ndmpd_zfs_args_t *ndmpd_zfs_args = &session->ns_ndmpd_zfs_args;
176 	int version = session->ns_protocol_version;
177 
178 	bzero(ndmpd_zfs_args, sizeof (*ndmpd_zfs_args));
179 
180 	if ((version < NDMPV3) || (version > NDMPV4)) {
181 		NDMP_LOG(LOG_ERR, "Unknown or unsupported version %d", version);
182 		return (-1);
183 	}
184 
185 	if ((ndmpd_zfs_args->nz_zlibh = libzfs_init()) == NULL) {
186 		NDMP_LOG(LOG_ERR, "libzfs init error [%d]", errno);
187 		return (-1);
188 	}
189 
190 	if (ndmpd_zfs_open_fds(ndmpd_zfs_args) < 0) {
191 		NDMP_LOG(LOG_ERR, "open_fds() failure(): %d\n", errno);
192 		return (-1);
193 	}
194 
195 	ndmpd_zfs_args->nz_bufsize = ndmp_buffer_get_size(session);
196 	ndmpd_zfs_args->nz_window_len = session->ns_mover.md_window_length;
197 
198 	ndmpd_zfs_args->nz_nlp = ndmp_get_nlp(session);
199 
200 	assert(ndmpd_zfs_args->nz_nlp != NULL);
201 
202 	ndmpd_zfs_args->nz_nlp->nlp_bytes_total = 0;
203 
204 	session->ns_data.dd_module.dm_module_cookie = ndmpd_zfs_args;
205 	session->ns_data.dd_data_size = 0;
206 	session->ns_data.dd_module.dm_stats.ms_est_bytes_remaining = 0;
207 	session->ns_data.dd_module.dm_stats.ms_est_time_remaining  = 0;
208 
209 	session->ns_data.dd_bytes_left_to_read = 0;
210 	session->ns_data.dd_position = 0;
211 	session->ns_data.dd_discard_length = 0;
212 	session->ns_data.dd_read_offset = 0;
213 	session->ns_data.dd_read_length = 0;
214 
215 	ndmpd_zfs_params->mp_get_env_func = ndmpd_api_get_env;
216 	ndmpd_zfs_params->mp_add_env_func = ndmpd_api_add_env;
217 	ndmpd_zfs_params->mp_set_env_func = ndmpd_api_set_env;
218 	ndmpd_zfs_params->mp_dispatch_func = ndmpd_api_dispatch;
219 	ndmpd_zfs_params->mp_daemon_cookie = (void *)session;
220 	ndmpd_zfs_params->mp_protocol_version = session->ns_protocol_version;
221 	ndmpd_zfs_params->mp_stats = &session->ns_data.dd_module.dm_stats;
222 	ndmpd_zfs_params->mp_add_file_handler_func =
223 	    ndmpd_api_add_file_handler;
224 	ndmpd_zfs_params->mp_remove_file_handler_func =
225 	    ndmpd_api_remove_file_handler;
226 	ndmpd_zfs_params->mp_seek_func = 0;
227 
228 	switch (version) {
229 	case NDMPV3:
230 		ndmpd_zfs_params->mp_write_func = ndmpd_api_write_v3;
231 		ndmpd_zfs_params->mp_read_func = ndmpd_api_read_v3;
232 		ndmpd_zfs_params->mp_get_name_func = ndmpd_api_get_name_v3;
233 		ndmpd_zfs_params->mp_done_func = ndmpd_api_done_v3;
234 		ndmpd_zfs_params->mp_log_func_v3 = ndmpd_api_log_v3;
235 		ndmpd_zfs_params->mp_file_recovered_func =
236 		    ndmpd_api_file_recovered_v3;
237 		break;
238 	case NDMPV4:
239 		ndmpd_zfs_params->mp_write_func = ndmpd_api_write_v3;
240 		ndmpd_zfs_params->mp_read_func = ndmpd_api_read_v3;
241 		ndmpd_zfs_params->mp_get_name_func = ndmpd_api_get_name_v3;
242 		ndmpd_zfs_params->mp_done_func = ndmpd_api_done_v3;
243 		ndmpd_zfs_params->mp_log_func_v3 = ndmpd_api_log_v4;
244 		ndmpd_zfs_params->mp_file_recovered_func =
245 		    ndmpd_api_file_recovered_v4;
246 		break;
247 	default:
248 		/* error already returned above for this case */
249 		break;
250 	}
251 
252 	return (0);
253 }
254 
255 void
256 ndmpd_zfs_fini(ndmpd_zfs_args_t *ndmpd_zfs_args)
257 {
258 	libzfs_fini(ndmpd_zfs_args->nz_zlibh);
259 
260 	ndmpd_zfs_close_fds(ndmpd_zfs_args);
261 }
262 
263 static int
264 ndmpd_zfs_open_fds(ndmpd_zfs_args_t *ndmpd_zfs_args)
265 {
266 	int err;
267 
268 	err = pipe(ndmpd_zfs_args->nz_pipe_fd);
269 	if (err)
270 		NDMP_LOG(LOG_ERR, "pipe(2) failed: %s:\n", strerror(errno));
271 
272 	return (err);
273 }
274 
275 /*
276  * ndmpd_zfs_close_fds()
277  *
278  * In the abort case, use dup2() to redirect the end of the pipe that is
279  * being written to (to a new pipe).  Close the ends of the new pipe to cause
280  * EPIPE to be returned to the writing thread.  This will cause the writer
281  * and reader to terminate without having any of the writer's data erroneously
282  * go to any reopened descriptor.
283  */
284 
285 static void
286 ndmpd_zfs_close_fds(ndmpd_zfs_args_t *ndmpd_zfs_args)
287 {
288 	ndmpd_session_t *session = (ndmpd_session_t *)
289 	    (ndmpd_zfs_params->mp_daemon_cookie);
290 	int pipe_end;
291 	int fds[2];
292 
293 	if (session->ns_data.dd_state != NDMP_DATA_STATE_ACTIVE) {
294 		ndmpd_zfs_close_one_fd(ndmpd_zfs_args, PIPE_ZFS);
295 		ndmpd_zfs_close_one_fd(ndmpd_zfs_args, PIPE_TAPE);
296 		return;
297 	}
298 
299 	(void) mutex_lock(&ndmpd_zfs_fd_lock);
300 
301 	if (ndmpd_zfs_params->mp_operation == NDMP_DATA_OP_BACKUP) {
302 		pipe_end = PIPE_ZFS;
303 	} else {
304 		pipe_end = PIPE_TAPE;
305 	}
306 
307 	if (ndmpd_zfs_args->nz_pipe_fd[pipe_end] != -1) {
308 		if (pipe(fds) != 0) {
309 			(void) mutex_unlock(&ndmpd_zfs_fd_lock);
310 			NDMP_LOG(LOG_ERR, "pipe(2) failed: %s:\n",
311 			    strerror(errno));
312 			return;
313 		}
314 
315 		(void) dup2(fds[0], ndmpd_zfs_args->nz_pipe_fd[pipe_end]);
316 		(void) close(fds[0]);
317 		(void) close(fds[1]);
318 
319 		ndmpd_zfs_args->nz_pipe_fd[pipe_end] = -1;
320 	}
321 
322 	(void) mutex_unlock(&ndmpd_zfs_fd_lock);
323 }
324 
325 static void
326 ndmpd_zfs_close_one_fd(ndmpd_zfs_args_t *ndmpd_zfs_args, int pipe_end)
327 {
328 	(void) mutex_lock(&ndmpd_zfs_fd_lock);
329 	(void) close(ndmpd_zfs_args->nz_pipe_fd[pipe_end]);
330 	ndmpd_zfs_args->nz_pipe_fd[pipe_end] = -1;
331 	(void) mutex_unlock(&ndmpd_zfs_fd_lock);
332 }
333 
334 static int
335 ndmpd_zfs_header_write(ndmpd_session_t *session)
336 {
337 	ndmpd_zfs_args_t *ndmpd_zfs_args = &session->ns_ndmpd_zfs_args;
338 	int32_t bufsize = ndmpd_zfs_args->nz_bufsize;
339 	ndmpd_zfs_header_t *tape_header = &ndmpd_zfs_args->nz_tape_header;
340 	char *buf;
341 
342 	buf = ndmp_malloc(bufsize);
343 	if (buf == NULL) {
344 		NDMP_LOG(LOG_DEBUG, "buf NULL");
345 		return (-1);
346 	}
347 
348 	(void) strlcpy(tape_header->nzh_magic, NDMPUTF8MAGIC,
349 	    sizeof (NDMPUTF8MAGIC));
350 	tape_header->nzh_major = LE_32(NDMPD_ZFS_MAJOR_VERSION);
351 	tape_header->nzh_minor = LE_32(NDMPD_ZFS_MINOR_VERSION);
352 	tape_header->nzh_hdrlen = LE_32(bufsize);
353 
354 	bzero(buf, bufsize);
355 	(void) memcpy(buf, tape_header, sizeof (ndmpd_zfs_header_t));
356 
357 	NDMP_LOG(LOG_DEBUG, "header (major, minor, length): %u %u %u",
358 	    NDMPD_ZFS_MAJOR_VERSION,
359 	    NDMPD_ZFS_MINOR_VERSION,
360 	    bufsize);
361 
362 	if (MOD_WRITE(ndmpd_zfs_params, buf, bufsize) != 0) {
363 		free(buf);
364 		NDMP_LOG(LOG_ERR, "MOD_WRITE error");
365 		return (-1);
366 	}
367 
368 	free(buf);
369 
370 	session->ns_data.dd_module.dm_stats.ms_bytes_processed = bufsize;
371 
372 	return (0);
373 }
374 
375 static int
376 ndmpd_zfs_header_read(ndmpd_zfs_args_t *ndmpd_zfs_args)
377 {
378 	int32_t bufsize = ndmpd_zfs_args->nz_bufsize;
379 	ndmpd_zfs_header_t *tape_header = &ndmpd_zfs_args->nz_tape_header;
380 	uint32_t hdrlen;
381 	int32_t header_left;
382 	int err;
383 	char *buf;
384 
385 	buf = ndmp_malloc(bufsize);
386 	if (buf == NULL) {
387 		NDMP_LOG(LOG_DEBUG, "buf NULL");
388 		return (-1);
389 	}
390 
391 	bzero(buf, bufsize);
392 
393 	/*
394 	 * Read nz_bufsize worth of bytes first (the size of a mover record).
395 	 */
396 
397 	err = MOD_READ(ndmpd_zfs_params, buf, bufsize);
398 
399 	if (err != 0) {
400 		NDMP_LOG(LOG_ERR, "MOD_READ error: %d", err);
401 		free(buf);
402 		return (-1);
403 	}
404 
405 	(void) memcpy(tape_header, buf, sizeof (ndmpd_zfs_header_t));
406 
407 	if (strcmp(tape_header->nzh_magic, NDMPUTF8MAGIC) != 0) {
408 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
409 		    "bad magic string\n");
410 		goto _err;
411 	}
412 
413 	if (tape_header->nzh_major > LE_32(NDMPD_ZFS_MAJOR_VERSION)) {
414 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
415 		    "major number larger than supported: (%d %d)\n",
416 		    LE_32(tape_header->nzh_major), NDMPD_ZFS_MAJOR_VERSION);
417 		goto _err;
418 	}
419 
420 	/*
421 	 * Major version 0 (regardless of minor version):
422 	 * Header must be a multiple of the mover record size.
423 	 */
424 
425 	hdrlen = LE_32(tape_header->nzh_hdrlen);
426 	if (hdrlen > bufsize) {
427 		header_left = hdrlen - bufsize;
428 		while (header_left > 0) {
429 			err = MOD_READ(ndmpd_zfs_params, buf, bufsize);
430 			if (err == -1) {
431 				ndmpd_zfs_dma_log(ndmpd_zfs_args,
432 				    NDMP_LOG_ERROR, "bad header\n");
433 				goto _err;
434 			}
435 			header_left -= bufsize;
436 		}
437 	}
438 
439 	NDMP_LOG(LOG_DEBUG, "tape header: %s; %u %u; %u ",
440 	    tape_header->nzh_magic,
441 	    LE_32(tape_header->nzh_major),
442 	    LE_32(tape_header->nzh_minor),
443 	    LE_32(tape_header->nzh_hdrlen));
444 
445 	ndmpd_zfs_args->nz_nlp->nlp_bytes_total = hdrlen;
446 
447 	free(buf);
448 	return (0);
449 
450 _err:
451 
452 	NDMP_LOG(LOG_ERR, "tape header: %s; %u %u; %u ",
453 	    tape_header->nzh_magic,
454 	    LE_32(tape_header->nzh_major),
455 	    LE_32(tape_header->nzh_minor),
456 	    LE_32(tape_header->nzh_hdrlen));
457 
458 	free(buf);
459 	return (-1);
460 }
461 
462 int
463 ndmpd_zfs_backup_starter(void *arg)
464 {
465 	ndmpd_zfs_args_t *ndmpd_zfs_args = arg;
466 	ndmpd_session_t *session = (ndmpd_session_t *)
467 	    (ndmpd_zfs_params->mp_daemon_cookie);
468 	int cleanup_err = 0;
469 	int err = 0;
470 
471 	ndmp_session_ref(session);
472 
473 	if (ndmpd_zfs_snapshot_prepare(ndmpd_zfs_args) != 0) {
474 		err = -1;
475 		goto _done;
476 	}
477 
478 	err = ndmpd_zfs_backup(ndmpd_zfs_args);
479 
480 	cleanup_err = ndmpd_zfs_snapshot_cleanup(ndmpd_zfs_args, err);
481 
482 	NDMP_LOG(LOG_DEBUG,
483 	    "data bytes_total(including header):%llu",
484 	    session->ns_data.dd_module.dm_stats.ms_bytes_processed);
485 
486 	if (err == 0)
487 		err = ndmpd_zfs_send_fhist(ndmpd_zfs_args);
488 
489 _done:
490 	NS_DEC(nbk);
491 	MOD_DONE(ndmpd_zfs_params, err ? err : cleanup_err);
492 	ndmp_session_unref(session);
493 	ndmpd_zfs_fini(ndmpd_zfs_args);
494 
495 	return (err);
496 }
497 
498 static int
499 ndmpd_zfs_send_fhist(ndmpd_zfs_args_t *ndmpd_zfs_args)
500 {
501 	ndmpd_session_t *session = (ndmpd_session_t *)
502 	    (ndmpd_zfs_params->mp_daemon_cookie);
503 	struct stat64 st;
504 	char *envp;
505 
506 	envp = MOD_GETENV(ndmpd_zfs_params, "HIST");
507 	if (!envp)
508 		return (0);
509 
510 	if (!(strchr("YT", toupper(*envp)))) {
511 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_WARNING,
512 		    "HIST is not set.  No file history will be "
513 		    "generated.\n");
514 		return (0);
515 	}
516 
517 	/* Build up a sample root dir stat */
518 	(void) memset(&st, 0, sizeof (struct stat64));
519 	st.st_mode = S_IFDIR | 0777;
520 	st.st_mtime = st.st_atime = st.st_ctime = time(NULL);
521 	st.st_uid = st.st_gid = 0;
522 	st.st_size = 1;
523 	st.st_nlink = 1;
524 
525 	if (ndmpd_api_file_history_dir_v3(session, ".", ROOT_INODE,
526 	    ROOT_INODE) != 0)
527 		return (-1);
528 	if (ndmpd_api_file_history_dir_v3(session, "..", ROOT_INODE,
529 	    ROOT_INODE) != 0)
530 		return (-1);
531 	if (ndmpd_api_file_history_node_v3(session, ROOT_INODE, &st, 0) != 0)
532 		return (-1);
533 
534 	ndmpd_file_history_cleanup(session, TRUE);
535 	return (0);
536 }
537 
538 static int
539 ndmpd_zfs_backup(ndmpd_zfs_args_t *ndmpd_zfs_args)
540 {
541 	ndmpd_session_t *session = (ndmpd_session_t *)
542 	    (ndmpd_zfs_params->mp_daemon_cookie);
543 	int *read_err = NULL;
544 	int *write_err = NULL;
545 	int result = 0;
546 	int err;
547 
548 	if (session->ns_eof)
549 		return (-1);
550 
551 	if (!session->ns_data.dd_abort) {
552 		if (ndmpd_zfs_header_write(session)) {
553 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
554 			    "ndmpd_zfs header write error\n");
555 			return (-1);
556 		}
557 
558 		err = ndmpd_zfs_reader_writer(ndmpd_zfs_args,
559 		    &read_err, &write_err);
560 
561 		if (err || read_err || write_err || session->ns_eof)
562 			result = EPIPE;
563 	}
564 
565 	if (session->ns_data.dd_abort) {
566 		ndmpd_audit_backup(session->ns_connection,
567 		    ndmpd_zfs_args->nz_dataset,
568 		    session->ns_data.dd_data_addr.addr_type,
569 		    ndmpd_zfs_args->nz_dataset, EINTR);
570 		NDMP_LOG(LOG_DEBUG, "Backing up \"%s\" aborted.",
571 		    ndmpd_zfs_args->nz_dataset);
572 
573 		(void) ndmpd_zfs_post_backup(ndmpd_zfs_args);
574 		err = -1;
575 	} else {
576 		ndmpd_audit_backup(session->ns_connection,
577 		    ndmpd_zfs_args->nz_dataset,
578 		    session->ns_data.dd_data_addr.addr_type,
579 		    ndmpd_zfs_args->nz_dataset, result);
580 
581 		err = ndmpd_zfs_post_backup(ndmpd_zfs_args);
582 		if (err || result)
583 			err = -1;
584 
585 		if (err == 0)  {
586 			NDMP_LOG(LOG_DEBUG, "Backing up \"%s\" finished.",
587 			    ndmpd_zfs_args->nz_dataset);
588 		} else {
589 			NDMP_LOG(LOG_DEBUG, "An error occurred while backing up"
590 			    " \"%s\"", ndmpd_zfs_args->nz_dataset);
591 		}
592 	}
593 
594 	return (err);
595 }
596 
597 /*
598  * ndmpd_zfs_backup_send_read()
599  *
600  * This routine executes zfs_send() to create the backup data stream.
601  * The value of ZFS_MODE determines the type of zfs_send():
602  * 	dataset ('d'): Only the dataset specified (i.e., top level) is backed up
603  * 	recursive ('r'): The dataset and its child file systems are backed up
604  * 	package ('p'): Same as 'r', except all intermediate snapshots are also
605  *			backed up
606  *
607  * Volumes do not have descednants, so 'd' and 'r' produce equivalent results.
608  */
609 
610 static int
611 ndmpd_zfs_backup_send_read(ndmpd_zfs_args_t *ndmpd_zfs_args)
612 {
613 	ndmpd_session_t *session = (ndmpd_session_t *)
614 	    (ndmpd_zfs_params->mp_daemon_cookie);
615 	sendflags_t flags = { 0 };
616 	char *fromsnap = NULL;
617 	zfs_handle_t *zhp;
618 	int err;
619 
620 	zhp = zfs_open(ndmpd_zfs_args->nz_zlibh,
621 	    ndmpd_zfs_args->nz_dataset, ndmpd_zfs_args->nz_type);
622 
623 	if (!zhp) {
624 		if (!session->ns_data.dd_abort)
625 			NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_open");
626 		return (-1);
627 	}
628 
629 	switch (ndmpd_zfs_args->nz_zfs_mode) {
630 	case ('d'):
631 		flags.props = B_TRUE;
632 		break;
633 	case ('r'):
634 		flags.replicate = B_TRUE;
635 		break;
636 	case ('p'):
637 		flags.doall = B_TRUE;
638 		flags.replicate = B_TRUE;
639 		break;
640 	default:
641 		NDMP_LOG(LOG_ERR, "unknown zfs_mode: %c",
642 		    ndmpd_zfs_args->nz_zfs_mode);
643 		zfs_close(zhp);
644 		return (-1);
645 	}
646 
647 	if (ndmpd_zfs_is_incremental(ndmpd_zfs_args)) {
648 		if (ndmpd_zfs_args->nz_fromsnap[0] == '\0') {
649 			NDMP_LOG(LOG_ERR, "no fromsnap");
650 			zfs_close(zhp);
651 			return (-1);
652 		}
653 		fromsnap = ndmpd_zfs_args->nz_fromsnap;
654 	}
655 
656 	err = zfs_send(zhp, fromsnap, ndmpd_zfs_args->nz_snapname, &flags,
657 	    ndmpd_zfs_args->nz_pipe_fd[PIPE_ZFS], NULL, NULL, NULL);
658 
659 	if (err && !session->ns_data.dd_abort)
660 		NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_send: %d", err);
661 
662 	zfs_close(zhp);
663 
664 	return (err);
665 }
666 
667 /*
668  * ndmpd_zfs_backup_tape_write()
669  *
670  * The data begins on a mover record boundary (because
671  * the header is the size of a mover record--i.e.
672  * ndmpd_zfs_args->nz_bufsize).
673  */
674 
675 static int
676 ndmpd_zfs_backup_tape_write(ndmpd_zfs_args_t *ndmpd_zfs_args)
677 {
678 	ndmpd_session_t *session = (ndmpd_session_t *)
679 	    (ndmpd_zfs_params->mp_daemon_cookie);
680 	int bufsize = ndmpd_zfs_args->nz_bufsize;
681 	u_longlong_t *bytes_totalp;
682 	int count;
683 	char *buf;
684 
685 	buf = ndmp_malloc(bufsize);
686 	if (buf == NULL) {
687 		NDMP_LOG(LOG_DEBUG, "buf NULL");
688 		return (-1);
689 	}
690 
691 	bytes_totalp =
692 	    &(session->ns_data.dd_module.dm_stats.ms_bytes_processed);
693 
694 	for (;;) {
695 		bzero(buf, bufsize);
696 
697 		count = read(ndmpd_zfs_args->nz_pipe_fd[PIPE_TAPE], buf,
698 		    bufsize);
699 
700 		if (count == 0) /* EOF */ {
701 			NDMP_LOG(LOG_DEBUG,
702 			    "zfs_send stream size: %llu bytes; "
703 			    "full backup size (including header): %llu",
704 			    *bytes_totalp - bufsize, *bytes_totalp);
705 			free(buf);
706 
707 			return (ndmpd_zfs_addenv_backup_size(ndmpd_zfs_args,
708 			    *bytes_totalp));
709 		}
710 
711 		if (count == -1) {
712 			NDMP_LOG(LOG_DEBUG, "pipe read error (errno %d)",
713 			    errno);
714 			free(buf);
715 			return (-1);
716 		}
717 		NS_ADD(rdisk, count);
718 
719 		if (MOD_WRITE(ndmpd_zfs_params, buf, count) != 0) {
720 			(void) ndmpd_zfs_abort((void *) ndmpd_zfs_args);
721 			NDMP_LOG(LOG_ERR, "MOD_WRITE error");
722 			free(buf);
723 			return (-1);
724 		}
725 
726 		*bytes_totalp += count;
727 	}
728 	/* NOTREACHED */
729 }
730 
731 static int
732 ndmpd_zfs_addenv_backup_size(ndmpd_zfs_args_t *ndmpd_zfs_args,
733     u_longlong_t bytes_total)
734 {
735 	char zfs_backup_size[32];
736 	int err;
737 
738 	(void) snprintf(zfs_backup_size, sizeof (zfs_backup_size), "%llu",
739 	    bytes_total);
740 
741 	err = MOD_ADDENV(ndmpd_zfs_params, "ZFS_BACKUP_SIZE", zfs_backup_size);
742 
743 	if (err) {
744 		NDMP_LOG(LOG_ERR, "Failed to add ZFS_BACKUP_SIZE env");
745 		return (-1);
746 	}
747 
748 	NDMP_LOG(LOG_DEBUG, "Added ZFS_BACKUP_SIZE env: %s", zfs_backup_size);
749 
750 	return (0);
751 }
752 
753 int
754 ndmpd_zfs_restore_starter(void *arg)
755 {
756 	ndmpd_zfs_args_t *ndmpd_zfs_args = arg;
757 	ndmpd_session_t *session = (ndmpd_session_t *)
758 	    (ndmpd_zfs_params->mp_daemon_cookie);
759 	int err;
760 
761 	ndmp_session_ref(session);
762 
763 	err = ndmpd_zfs_restore(ndmpd_zfs_args);
764 
765 	MOD_DONE(ndmpd_zfs_params, err);
766 
767 	NS_DEC(nrs);
768 
769 	ndmp_session_unref(session);
770 
771 	ndmpd_zfs_fini(ndmpd_zfs_args);
772 
773 	return (err);
774 }
775 
776 static int
777 ndmpd_zfs_restore(ndmpd_zfs_args_t *ndmpd_zfs_args)
778 {
779 	ndmpd_session_t *session = (ndmpd_session_t *)
780 	    (ndmpd_zfs_params->mp_daemon_cookie);
781 	int *read_err = NULL;
782 	int *write_err = NULL;
783 	int result = 0;
784 	int err;
785 
786 	if (!session->ns_data.dd_abort) {
787 		if (ndmpd_zfs_header_read(ndmpd_zfs_args)) {
788 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
789 			    "ndmpd_zfs header read error\n");
790 			return (-1);
791 		}
792 
793 		err = ndmpd_zfs_reader_writer(ndmpd_zfs_args,
794 		    &write_err, &read_err);
795 
796 		if (err || read_err || write_err || session->ns_eof)
797 			result = EIO;
798 	}
799 
800 	if (session->ns_data.dd_abort) {
801 		NDMP_LOG(LOG_DEBUG, "Restoring to \"%s\" aborted.",
802 		    ndmpd_zfs_args->nz_dataset);
803 		ndmpd_audit_restore(session->ns_connection,
804 		    ndmpd_zfs_args->nz_dataset,
805 		    session->ns_data.dd_data_addr.addr_type,
806 		    ndmpd_zfs_args->nz_dataset, EINTR);
807 		(void) ndmpd_zfs_post_restore(ndmpd_zfs_args);
808 		err = -1;
809 	} else {
810 		ndmpd_audit_restore(session->ns_connection,
811 		    ndmpd_zfs_args->nz_dataset,
812 		    session->ns_data.dd_data_addr.addr_type,
813 		    ndmpd_zfs_args->nz_dataset, result);
814 		err = ndmpd_zfs_post_restore(ndmpd_zfs_args);
815 		if (err || result)
816 			err = -1;
817 
818 		if (err == 0) {
819 			NDMP_LOG(LOG_DEBUG, "Restoring to \"%s\" finished",
820 			    ndmpd_zfs_args->nz_dataset);
821 		} else {
822 			NDMP_LOG(LOG_DEBUG, "An error occurred while restoring"
823 			    " to \"%s\"", ndmpd_zfs_args->nz_dataset);
824 		}
825 	}
826 
827 	return (err);
828 }
829 
830 static int
831 ndmpd_zfs_restore_tape_read(ndmpd_zfs_args_t *ndmpd_zfs_args)
832 {
833 	ndmpd_session_t *session = (ndmpd_session_t *)
834 	    (ndmpd_zfs_params->mp_daemon_cookie);
835 	int bufsize = ndmpd_zfs_args->nz_bufsize;
836 	u_longlong_t backup_size = ndmpd_zfs_args->nz_zfs_backup_size;
837 	u_longlong_t *bytes_totalp;
838 	u_longlong_t bytes;
839 	char *buf;
840 	int count;
841 	int err;
842 
843 	buf = ndmp_malloc(bufsize);
844 	if (buf == NULL) {
845 		NDMP_LOG(LOG_DEBUG, "buf NULL");
846 		return (-1);
847 	}
848 
849 	bytes_totalp = &ndmpd_zfs_args->nz_nlp->nlp_bytes_total;
850 
851 	while (*bytes_totalp < backup_size) {
852 
853 		bytes = backup_size - *bytes_totalp;
854 
855 		if (bytes >= bufsize)
856 			bytes = bufsize;
857 
858 		err = MOD_READ(ndmpd_zfs_params, buf, bytes);
859 
860 		if (err != 0) {
861 			NDMP_LOG(LOG_ERR, "MOD_READ error: %d; returning -1",
862 			    err);
863 			free(buf);
864 			return (-1);
865 		}
866 
867 		count = write(ndmpd_zfs_args->nz_pipe_fd[PIPE_TAPE], buf,
868 		    bytes);
869 
870 		if (count != bytes) {
871 			NDMP_LOG(LOG_ERR, "count (%d) != bytes (%d)",
872 			    count, bytes);
873 
874 			if (count == -1) {
875 				NDMP_LOG(LOG_ERR, "pipe write error: errno: %d",
876 				    errno);
877 
878 				if (session->ns_data.dd_abort)
879 					NDMP_LOG(LOG_DEBUG, "abort set");
880 			}
881 
882 			free(buf);
883 			return (-1);
884 		}
885 
886 		NS_ADD(wdisk, count);
887 
888 		*bytes_totalp += count;
889 	}
890 
891 	free(buf);
892 	return (0);
893 }
894 
895 /*
896  * ndmpd_zfs_restore_recv_write()
897  *
898  * This routine executes zfs_receive() to restore the backup.
899  */
900 
901 static int
902 ndmpd_zfs_restore_recv_write(ndmpd_zfs_args_t *ndmpd_zfs_args)
903 {
904 	ndmpd_session_t *session = (ndmpd_session_t *)
905 	    (ndmpd_zfs_params->mp_daemon_cookie);
906 	recvflags_t flags;
907 	int err;
908 
909 	bzero(&flags, sizeof (recvflags_t));
910 
911 	flags.nomount = B_TRUE;
912 
913 	NDMP_LOG(LOG_DEBUG, "nz_zfs_force: %d\n", ndmpd_zfs_args->nz_zfs_force);
914 
915 	if (ndmpd_zfs_args->nz_zfs_force)
916 		flags.force = B_TRUE;
917 
918 	err = zfs_receive(ndmpd_zfs_args->nz_zlibh, ndmpd_zfs_args->nz_dataset,
919 	    &flags, ndmpd_zfs_args->nz_pipe_fd[PIPE_ZFS], NULL);
920 
921 	if (err && !session->ns_data.dd_abort)
922 		NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_receive: %d", err);
923 
924 	return (err);
925 }
926 
927 /*
928  * ndmpd_zfs_reader_writer()
929  *
930  * Two separate threads are used for actual backup or restore.
931  */
932 
933 static int
934 ndmpd_zfs_reader_writer(ndmpd_zfs_args_t *ndmpd_zfs_args,
935     int **sendrecv_errp, int **tape_errp)
936 {
937 	funct_t sendrecv_func;
938 	funct_t tape_func;
939 	int sendrecv_err;
940 	int tape_err;
941 
942 	switch (ndmpd_zfs_params->mp_operation) {
943 	case NDMP_DATA_OP_BACKUP:
944 		sendrecv_func = (funct_t)ndmpd_zfs_backup_send_read;
945 		tape_func = (funct_t)ndmpd_zfs_backup_tape_write;
946 		break;
947 	case NDMP_DATA_OP_RECOVER:
948 		sendrecv_func = (funct_t)ndmpd_zfs_restore_recv_write;
949 		tape_func = (funct_t)ndmpd_zfs_restore_tape_read;
950 		break;
951 	}
952 
953 	sendrecv_err = pthread_create(&ndmpd_zfs_args->nz_sendrecv_thread,
954 	    NULL, sendrecv_func, ndmpd_zfs_args);
955 
956 	if (sendrecv_err == 0) {
957 		tape_err = pthread_create(&ndmpd_zfs_args->nz_tape_thread,
958 		    NULL, tape_func, ndmpd_zfs_args);
959 
960 		if (tape_err) {
961 			/*
962 			 * The close of the tape side of the pipe will cause
963 			 * nz_sendrecv_thread to error in the zfs_send/recv()
964 			 * call and to return.  Hence we do not need
965 			 * to explicitly cancel the sendrecv_thread here
966 			 * (the pthread_join() below is sufficient).
967 			 */
968 
969 			(void) close(ndmpd_zfs_args->nz_pipe_fd[PIPE_TAPE]);
970 			NDMP_LOG(LOG_ERR, "Could not start tape thread; "
971 			    "aborting z-op");
972 		}
973 
974 		(void) pthread_join(ndmpd_zfs_args->nz_sendrecv_thread,
975 		    (void **) sendrecv_errp);
976 	}
977 
978 	if ((tape_err == 0) &&
979 	    (ndmpd_zfs_params->mp_operation == NDMP_DATA_OP_RECOVER))
980 		ndmpd_zfs_close_one_fd(ndmpd_zfs_args, PIPE_TAPE);
981 
982 	ndmpd_zfs_close_one_fd(ndmpd_zfs_args, PIPE_ZFS);
983 
984 	if ((sendrecv_err == 0) && (tape_err == 0)) {
985 		(void) pthread_join(ndmpd_zfs_args->nz_tape_thread,
986 		    (void **) tape_errp);
987 	}
988 
989 	if ((tape_err == 0) &&
990 	    (ndmpd_zfs_params->mp_operation == NDMP_DATA_OP_BACKUP))
991 		ndmpd_zfs_close_one_fd(ndmpd_zfs_args, PIPE_TAPE);
992 
993 	return (sendrecv_err ? sendrecv_err : tape_err);
994 }
995 
996 int
997 ndmpd_zfs_abort(void *arg)
998 {
999 	ndmpd_zfs_args_t *ndmpd_zfs_args = arg;
1000 	char str[8];
1001 
1002 	if (ndmpd_zfs_params->mp_operation == NDMP_DATA_OP_BACKUP)
1003 		(void) strlcpy(str, "backup", 8);
1004 	else
1005 		(void) strlcpy(str, "recover", 8);
1006 
1007 	NDMP_LOG(LOG_ERR, "ndmpd_zfs_abort() called...aborting %s operation",
1008 	    str);
1009 
1010 	ndmpd_zfs_close_fds(ndmpd_zfs_args);
1011 
1012 	return (0);
1013 }
1014 
1015 /*
1016  * ndmpd_zfs_pre_backup()
1017  *
1018  * Note: The memset to 0 of nctxp ensures that nctx->nc_cmds == NULL.
1019  * This ensures that ndmp_include_zfs() will fail, which is
1020  * a requirement for "zfs"-type backup.
1021  */
1022 
1023 int
1024 ndmpd_zfs_pre_backup(ndmpd_zfs_args_t *ndmpd_zfs_args)
1025 {
1026 	ndmpd_session_t *session = (ndmpd_session_t *)
1027 	    (ndmpd_zfs_params->mp_daemon_cookie);
1028 	ndmp_context_t *nctxp = &ndmpd_zfs_args->nz_nctx;
1029 	int err;
1030 
1031 	if (ndmp_pl == NULL || ndmp_pl->np_pre_backup == NULL)
1032 		return (0);
1033 
1034 	(void) memset(nctxp, 0, sizeof (ndmp_context_t));
1035 	nctxp->nc_plversion = ndmp_pl->np_plversion;
1036 	nctxp->nc_plname = ndmpd_get_prop(NDMP_PLUGIN_PATH);
1037 	nctxp->nc_ddata = (void *) session;
1038 	nctxp->nc_params = ndmpd_zfs_params;
1039 
1040 	err = ndmp_pl->np_pre_backup(ndmp_pl, nctxp,
1041 	    ndmpd_zfs_args->nz_dataset);
1042 
1043 	if (err != 0) {
1044 		NDMP_LOG(LOG_DEBUG, "Pre-backup plug-in: %m");
1045 		(void) ndmpd_zfs_post_backup(ndmpd_zfs_args);
1046 	}
1047 
1048 	return (err);
1049 }
1050 
1051 int
1052 ndmpd_zfs_post_backup(ndmpd_zfs_args_t *ndmpd_zfs_args)
1053 {
1054 	ndmp_context_t *nctxp = &ndmpd_zfs_args->nz_nctx;
1055 	int err = 0;
1056 
1057 	if (ndmp_pl == NULL || ndmp_pl->np_post_backup == NULL)
1058 		return (0);
1059 
1060 	err = ndmp_pl->np_post_backup(ndmp_pl, nctxp, err);
1061 
1062 	if (err == -1)
1063 		NDMP_LOG(LOG_DEBUG, "Post-backup plug-in: %m");
1064 
1065 	return (err);
1066 }
1067 
1068 int
1069 ndmpd_zfs_pre_restore(ndmpd_zfs_args_t *ndmpd_zfs_args)
1070 {
1071 	ndmpd_session_t *session = (ndmpd_session_t *)
1072 	    (ndmpd_zfs_params->mp_daemon_cookie);
1073 	ndmp_context_t *nctxp = &ndmpd_zfs_args->nz_nctx;
1074 	char bkpath[ZFS_MAXNAMELEN];
1075 	int err;
1076 
1077 	if (ndmp_pl == NULL || ndmp_pl->np_pre_restore == NULL)
1078 		return (0);
1079 
1080 	err = ndmpd_zfs_backup_getpath(ndmpd_zfs_args, bkpath, ZFS_MAXNAMELEN);
1081 
1082 	if (err != 0) {
1083 		NDMP_LOG(LOG_DEBUG, "error getting bkup path: %d", err);
1084 		return (-1);
1085 	}
1086 
1087 	err = ndmpd_zfs_restore_getpath(ndmpd_zfs_args);
1088 
1089 	if (err != 0) {
1090 		NDMP_LOG(LOG_DEBUG, "error getting restore path: %d", err);
1091 		return (-1);
1092 	}
1093 
1094 	(void) memset(nctxp, 0, sizeof (ndmp_context_t));
1095 	nctxp->nc_ddata = (void *) session;
1096 	nctxp->nc_params = ndmpd_zfs_params;
1097 
1098 	err = ndmp_pl->np_pre_restore(ndmp_pl, nctxp, bkpath,
1099 	    ndmpd_zfs_args->nz_dataset);
1100 
1101 	if (err != 0) {
1102 		NDMP_LOG(LOG_DEBUG, "Pre-restore plug-in: %m");
1103 		return (-1);
1104 	}
1105 
1106 	return (0);
1107 }
1108 
1109 int
1110 ndmpd_zfs_post_restore(ndmpd_zfs_args_t *ndmpd_zfs_args)
1111 {
1112 	ndmp_context_t *nctxp = &ndmpd_zfs_args->nz_nctx;
1113 	int err = 0;
1114 
1115 	if (ndmp_pl == NULL || ndmp_pl->np_post_restore == NULL)
1116 		return (0);
1117 
1118 	err = ndmp_pl->np_post_restore(ndmp_pl, nctxp, err);
1119 
1120 	if (err == -1)
1121 		NDMP_LOG(LOG_DEBUG, "Post-restore plug-in: %m");
1122 
1123 	return (err);
1124 }
1125 
1126 boolean_t
1127 ndmpd_zfs_backup_parms_valid(ndmpd_zfs_args_t *ndmpd_zfs_args)
1128 {
1129 	ndmpd_zfs_snapfind_t snapdata;
1130 
1131 	if (ndmpd_zfs_backup_getenv(ndmpd_zfs_args) != 0)
1132 		return (B_FALSE);
1133 
1134 	if (!ndmpd_zfs_backup_pathvalid(ndmpd_zfs_args))
1135 		return (B_FALSE);
1136 
1137 	if (ndmpd_zfs_is_incremental(ndmpd_zfs_args)) {
1138 		(void) ndmpd_zfs_prop_create_subprop(ndmpd_zfs_args,
1139 		    snapdata.nzs_findprop, ZFS_MAXPROPLEN, B_TRUE);
1140 
1141 		snapdata.nzs_snapname[0] = '\0';
1142 		snapdata.nzs_snapprop[0] = '\0';
1143 
1144 		if (ndmpd_zfs_snapshot_find(ndmpd_zfs_args, &snapdata))
1145 			return (B_FALSE);
1146 
1147 		if (snapdata.nzs_snapname[0] == '\0') { /* not found */
1148 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1149 			    "Snapshot for level %d does not exist\n",
1150 			    ndmpd_zfs_args->nz_level-1);
1151 			return (B_FALSE);
1152 		}
1153 
1154 		(void) strlcpy(ndmpd_zfs_args->nz_fromsnap,
1155 		    snapdata.nzs_snapname, ZFS_MAXNAMELEN);
1156 	}
1157 
1158 	return (B_TRUE);
1159 }
1160 
1161 /*
1162  * ndmpd_zfs_backup_pathvalid()
1163  *
1164  * Make sure the path is of an existing dataset
1165  */
1166 
1167 static boolean_t
1168 ndmpd_zfs_backup_pathvalid(ndmpd_zfs_args_t *ndmpd_zfs_args)
1169 {
1170 	char zpath[ZFS_MAXNAMELEN];
1171 	char propstr[ZFS_MAXPROPLEN];
1172 	zfs_handle_t *zhp;
1173 	zfs_type_t ztype = 0;
1174 	int err;
1175 
1176 	if (ndmpd_zfs_backup_getpath(ndmpd_zfs_args, zpath, ZFS_MAXNAMELEN)
1177 	    != 0)
1178 		return (B_FALSE);
1179 
1180 	if (ndmpd_zfs_args->nz_snapname[0] != '\0') {
1181 		zhp = zfs_open(ndmpd_zfs_args->nz_zlibh, zpath,
1182 		    ZFS_TYPE_SNAPSHOT);
1183 
1184 		if (!zhp) {
1185 			NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args,
1186 			    "zfs_open (snap)");
1187 			ndmpd_zfs_args->nz_snapname[0] = '\0';
1188 			ndmpd_zfs_args->nz_dataset[0] = '\0';
1189 			return (B_FALSE);
1190 		}
1191 
1192 		err = ndmpd_zfs_snapshot_prop_get(zhp, propstr);
1193 
1194 		zfs_close(zhp);
1195 
1196 		if (err) {
1197 			NDMP_LOG(LOG_DEBUG,
1198 			    "ndmpd_zfs_snapshot_prop_get failed");
1199 			return (-1);
1200 		}
1201 
1202 		if (propstr && ndmpd_zfs_snapshot_ndmpd_generated(propstr)) {
1203 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1204 			    "cannot use an ndmpd-generated snapshot\n");
1205 			return (B_FALSE);
1206 		}
1207 	}
1208 
1209 	zhp = zfs_open(ndmpd_zfs_args->nz_zlibh,
1210 	    ndmpd_zfs_args->nz_dataset, ZFS_TYPE_DATASET);
1211 
1212 	if (zhp) {
1213 		ztype = zfs_get_type(zhp);
1214 		zfs_close(zhp);
1215 	}
1216 
1217 	if ((ztype == ZFS_TYPE_VOLUME) ||
1218 	    (ztype == ZFS_TYPE_FILESYSTEM)) {
1219 		ndmpd_zfs_args->nz_type = ztype;
1220 		return (B_TRUE);
1221 	}
1222 
1223 	ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1224 	    "Invalid file system or volume.\n");
1225 
1226 	return (B_FALSE);
1227 }
1228 
1229 /*
1230  * ndmpd_zfs_backup_getpath()
1231  *
1232  * Retrieve the backup path from the environment, which should
1233  * be of the form "/dataset[@snap]".  The leading slash is required
1234  * by certain DMA's but can otherwise be ignored.
1235  *
1236  * (Note: "dataset" can consist of more than one component,
1237  * e.g. "pool", "pool/volume", "pool/fs/fs2".)
1238  *
1239  * The dataset name and the snapshot name (if any) will be
1240  * stored in ndmpd_zfs_args.
1241  */
1242 
1243 static int
1244 ndmpd_zfs_backup_getpath(ndmpd_zfs_args_t *ndmpd_zfs_args, char *zpath,
1245     int zlen)
1246 {
1247 	char *env_path;
1248 	char *at;
1249 
1250 	env_path = get_backup_path_v3(ndmpd_zfs_params);
1251 	if (env_path == NULL)
1252 		return (-1);
1253 
1254 	if (env_path[0] != '/') {
1255 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1256 		    "Invalid path: %s (leading slash required)\n", env_path);
1257 		return (-1);
1258 	}
1259 
1260 	(void) strlcpy(zpath, &env_path[1], zlen);
1261 	(void) strlcpy(ndmpd_zfs_args->nz_dataset, &env_path[1],
1262 	    ZFS_MAXNAMELEN);
1263 
1264 	at = strchr(ndmpd_zfs_args->nz_dataset, '@');
1265 	if (at) {
1266 		*at = '\0';
1267 		(void) strlcpy(ndmpd_zfs_args->nz_snapname, ++at,
1268 		    ZFS_MAXNAMELEN);
1269 	} else {
1270 		ndmpd_zfs_args->nz_snapname[0] = '\0';
1271 	}
1272 
1273 	(void) trim_whitespace(ndmpd_zfs_args->nz_dataset);
1274 
1275 	return (0);
1276 }
1277 
1278 static int
1279 ndmpd_zfs_backup_getenv(ndmpd_zfs_args_t *ndmpd_zfs_args)
1280 {
1281 	return (ndmpd_zfs_getenv(ndmpd_zfs_args));
1282 }
1283 
1284 boolean_t
1285 ndmpd_zfs_restore_parms_valid(ndmpd_zfs_args_t *ndmpd_zfs_args)
1286 {
1287 	if (ndmpd_zfs_restore_getenv(ndmpd_zfs_args) != 0)
1288 		return (B_FALSE);
1289 
1290 	if (!ndmpd_zfs_restore_pathvalid(ndmpd_zfs_args))
1291 		return (B_FALSE);
1292 
1293 	return (B_TRUE);
1294 }
1295 
1296 static boolean_t
1297 ndmpd_zfs_restore_pathvalid(ndmpd_zfs_args_t *ndmpd_zfs_args)
1298 {
1299 	zfs_handle_t *zhp;
1300 	char *at;
1301 
1302 	if (ndmpd_zfs_restore_getpath(ndmpd_zfs_args) != 0)
1303 		return (B_FALSE);
1304 
1305 	at = strchr(ndmpd_zfs_args->nz_dataset, '@');
1306 
1307 	if (at) {
1308 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_WARNING,
1309 		    "%s ignored in restore path\n", at);
1310 		*at = '\0';
1311 	}
1312 
1313 	ndmpd_zfs_args->nz_type = ZFS_TYPE_VOLUME | ZFS_TYPE_FILESYSTEM;
1314 
1315 	zhp = zfs_open(ndmpd_zfs_args->nz_zlibh,
1316 	    ndmpd_zfs_args->nz_dataset, ndmpd_zfs_args->nz_type);
1317 
1318 	if (zhp) {
1319 		zfs_close(zhp);
1320 
1321 		if (!ndmpd_zfs_is_incremental(ndmpd_zfs_args)) {
1322 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1323 			    "Restore dataset exists.\n"
1324 			    "A nonexistent dataset must be specified "
1325 			    "for 'zfs' non-incremental restore.\n");
1326 			return (B_FALSE);
1327 		}
1328 	}
1329 
1330 	NDMP_LOG(LOG_DEBUG, "restore path: %s\n", ndmpd_zfs_args->nz_dataset);
1331 
1332 	return (B_TRUE);
1333 }
1334 
1335 /*
1336  * ndmpd_zfs_restore_getpath()
1337  *
1338  * Be sure to not include the leading slash, which is required for
1339  * compatibility with backup applications (NBU) but which is not part
1340  * of the ZFS syntax.  (Note that this done explicitly in all paths
1341  * below except those calling ndmpd_zfs_backup_getpath(), because it is
1342  * already stripped in that function.)
1343  *
1344  * In addition, the DMA might add a trailing slash to the path.
1345  * Strip all such slashes.
1346  */
1347 
1348 static int
1349 ndmpd_zfs_restore_getpath(ndmpd_zfs_args_t *ndmpd_zfs_args)
1350 {
1351 	int version = ndmpd_zfs_params->mp_protocol_version;
1352 	char zpath[ZFS_MAXNAMELEN];
1353 	mem_ndmp_name_v3_t *namep_v3;
1354 	char *dataset = ndmpd_zfs_args->nz_dataset;
1355 	char *nm;
1356 	char *p;
1357 	int len;
1358 	int err;
1359 
1360 	dataset = ndmpd_zfs_args->nz_dataset;
1361 
1362 	namep_v3 = (mem_ndmp_name_v3_t *)MOD_GETNAME(ndmpd_zfs_params, 0);
1363 
1364 	if (namep_v3 == NULL) {
1365 		NDMP_LOG(LOG_DEBUG, "Can't get Nlist[0]");
1366 		return (-1);
1367 	}
1368 
1369 	if (namep_v3->nm3_dpath) {
1370 		if (namep_v3->nm3_dpath[0] != '/') {
1371 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1372 			    "Invalid path: %s (leading slash required)\n",
1373 			    namep_v3->nm3_dpath);
1374 			return (-1);
1375 		}
1376 
1377 		(void) strlcpy(dataset, &(namep_v3->nm3_dpath[1]),
1378 		    ZFS_MAXNAMELEN);
1379 
1380 		if (namep_v3->nm3_newnm) {
1381 			(void) strlcat(dataset, "/", ZFS_MAXNAMELEN);
1382 			(void) strlcat(dataset, namep_v3->nm3_newnm,
1383 			    ZFS_MAXNAMELEN);
1384 
1385 		} else {
1386 			if (version == NDMPV3) {
1387 				/*
1388 				 * The following does not apply for V4.
1389 				 *
1390 				 * Find the last component of nm3_opath.
1391 				 * nm3_opath has no trailing '/'.
1392 				 */
1393 				p = strrchr(namep_v3->nm3_opath, '/');
1394 				nm = p? p : namep_v3->nm3_opath;
1395 				(void) strlcat(dataset, "/", ZFS_MAXNAMELEN);
1396 				(void) strlcat(dataset, nm, ZFS_MAXNAMELEN);
1397 			}
1398 		}
1399 	} else {
1400 		err = ndmpd_zfs_backup_getpath(ndmpd_zfs_args, zpath,
1401 		    ZFS_MAXNAMELEN);
1402 		if (err)
1403 			return (err);
1404 	}
1405 
1406 	len = strlen(dataset);
1407 	while (dataset[len-1] == '/') {
1408 		dataset[len-1] = '\0';
1409 		len--;
1410 	}
1411 
1412 	return (0);
1413 }
1414 
1415 static int
1416 ndmpd_zfs_restore_getenv(ndmpd_zfs_args_t *ndmpd_zfs_args)
1417 {
1418 	if (ndmpd_zfs_getenv_zfs_backup_size(ndmpd_zfs_args) != 0)
1419 		return (-1);
1420 
1421 	return (ndmpd_zfs_getenv(ndmpd_zfs_args));
1422 }
1423 
1424 static int
1425 ndmpd_zfs_getenv(ndmpd_zfs_args_t *ndmpd_zfs_args)
1426 {
1427 
1428 	if (ndmpd_zfs_getenv_level(ndmpd_zfs_args) != 0)
1429 		return (-1);
1430 
1431 	if (ndmpd_zfs_getenv_zfs_mode(ndmpd_zfs_args) != 0)
1432 		return (-1);
1433 
1434 	if (ndmpd_zfs_getenv_zfs_force(ndmpd_zfs_args) != 0)
1435 		return (-1);
1436 
1437 	if (ndmpd_zfs_getenv_update(ndmpd_zfs_args) != 0)
1438 		return (-1);
1439 
1440 	if (ndmpd_zfs_getenv_dmp_name(ndmpd_zfs_args) != 0)
1441 		return (-1);
1442 
1443 	return (0);
1444 }
1445 
1446 static int
1447 ndmpd_zfs_getenv_zfs_mode(ndmpd_zfs_args_t *ndmpd_zfs_args)
1448 {
1449 	char *envp;
1450 
1451 	envp = MOD_GETENV(ndmpd_zfs_params, "ZFS_MODE");
1452 
1453 	if (envp == NULL) {
1454 		NDMP_LOG(LOG_DEBUG, "env(ZFS_MODE) not specified, "
1455 		    "defaulting to recursive");
1456 		ndmpd_zfs_args->nz_zfs_mode = 'r';
1457 		return (0);
1458 	}
1459 
1460 	if ((strcmp(envp, "dataset") == 0) || (strcmp(envp, "d") == 0)) {
1461 		ndmpd_zfs_args->nz_zfs_mode = 'd';
1462 	} else if ((strcmp(envp, "recursive") == 0) ||
1463 	    (strcmp(envp, "r") == 0)) {
1464 		ndmpd_zfs_args->nz_zfs_mode = 'r';
1465 	} else if ((strcmp(envp, "package") == 0) || (strcmp(envp, "p") == 0)) {
1466 		ndmpd_zfs_args->nz_zfs_mode = 'p';
1467 	} else {
1468 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1469 		    "Invalid ZFS_MODE value \"%s\".\n", envp);
1470 		return (-1);
1471 	}
1472 
1473 	NDMP_LOG(LOG_DEBUG, "env(ZFS_MODE): \"%c\"",
1474 	    ndmpd_zfs_args->nz_zfs_mode);
1475 
1476 	return (0);
1477 }
1478 
1479 /*
1480  * ndmpd_zfs_getenv_zfs_force()
1481  *
1482  * If SMF property zfs-force-override is set to "yes" or "no", this
1483  * value will override any value of NDMP environment variable ZFS_FORCE
1484  * as set by the DMA admin (or override the default of 'n', if ZFS_FORCE
1485  * is not set).  By default, zfs-force-override is "off", which means it
1486  * will not override ZFS_FORCE.
1487  */
1488 
1489 static int
1490 ndmpd_zfs_getenv_zfs_force(ndmpd_zfs_args_t *ndmpd_zfs_args)
1491 {
1492 	char *envp_force;
1493 	char *override;
1494 
1495 	override = ndmpd_get_prop(NDMP_ZFS_FORCE_OVERRIDE);
1496 
1497 	if (strcasecmp(override, "yes") == 0) {
1498 		ndmpd_zfs_args->nz_zfs_force = B_TRUE;
1499 		NDMP_LOG(LOG_NOTICE,
1500 		    "SMF property zfs-force-override set to 'yes', "
1501 		    "overriding ZFS_FORCE");
1502 		return (0);
1503 	}
1504 
1505 	if (strcasecmp(override, "no") == 0) {
1506 		ndmpd_zfs_args->nz_zfs_force = B_FALSE;
1507 		NDMP_LOG(LOG_NOTICE,
1508 		    "SMF property zfs-force-override set to 'no', "
1509 		    "overriding ZFS_FORCE");
1510 		return (0);
1511 	}
1512 
1513 	if (strcasecmp(override, "off") != 0) {
1514 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1515 		    "SMF property zfs-force-override set to invalid value of "
1516 		    "'%s'; treating it as 'off'.", override);
1517 	}
1518 
1519 	envp_force = MOD_GETENV(ndmpd_zfs_params, "ZFS_FORCE");
1520 
1521 	if (envp_force == NULL) {
1522 		NDMP_LOG(LOG_DEBUG,
1523 		    "env(ZFS_FORCE) not specified, defaulting to FALSE");
1524 		ndmpd_zfs_args->nz_zfs_force = B_FALSE;
1525 		return (0);
1526 	}
1527 
1528 	/*
1529 	 * The value can be either 't' ("true" for v3) or 'y' ("yes" for v4).
1530 	 */
1531 
1532 	if (strchr("tTyY", *envp_force))
1533 		ndmpd_zfs_args->nz_zfs_force = B_TRUE;
1534 
1535 	NDMP_LOG(LOG_DEBUG, "env(ZFS_FORCE): \"%s\"", envp_force);
1536 
1537 	return (0);
1538 }
1539 
1540 static int
1541 ndmpd_zfs_getenv_level(ndmpd_zfs_args_t *ndmpd_zfs_args)
1542 {
1543 	char *envp;
1544 
1545 	envp = MOD_GETENV(ndmpd_zfs_params, "LEVEL");
1546 
1547 	if (envp == NULL) {
1548 		NDMP_LOG(LOG_DEBUG, "env(LEVEL) not specified, "
1549 		    "defaulting to 0");
1550 		ndmpd_zfs_args->nz_level = 0;
1551 		return (0);
1552 	}
1553 
1554 	if (envp[1] != '\0') {
1555 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1556 		    "Invalid backup level \"%s\".\n", envp);
1557 		return (-1);
1558 	}
1559 
1560 	if (!isdigit(*envp)) {
1561 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1562 		    "Invalid backup level \"%s\".\n", envp);
1563 		return (-1);
1564 	}
1565 
1566 	ndmpd_zfs_args->nz_level = atoi(envp);
1567 
1568 	NDMP_LOG(LOG_DEBUG, "env(LEVEL): \"%d\"",
1569 	    ndmpd_zfs_args->nz_level);
1570 
1571 	return (0);
1572 }
1573 
1574 static int
1575 ndmpd_zfs_getenv_update(ndmpd_zfs_args_t *ndmpd_zfs_args)
1576 {
1577 	char *envp_update;
1578 
1579 	envp_update = MOD_GETENV(ndmpd_zfs_params, "UPDATE");
1580 
1581 	if (envp_update == NULL) {
1582 		NDMP_LOG(LOG_DEBUG,
1583 		    "env(UPDATE) not specified, defaulting to TRUE");
1584 		ndmpd_zfs_args->nz_update = B_TRUE;
1585 		return (0);
1586 	}
1587 
1588 	/*
1589 	 * The value can be either 't' ("true" for v3) or 'y' ("yes" for v4).
1590 	 */
1591 
1592 	if (strchr("tTyY", *envp_update))
1593 		ndmpd_zfs_args->nz_update = B_TRUE;
1594 
1595 	NDMP_LOG(LOG_DEBUG, "env(UPDATE): \"%s\"", envp_update);
1596 
1597 	return (0);
1598 }
1599 
1600 static int
1601 ndmpd_zfs_getenv_dmp_name(ndmpd_zfs_args_t *ndmpd_zfs_args)
1602 {
1603 	char *envp;
1604 
1605 	envp = MOD_GETENV(ndmpd_zfs_params, "DMP_NAME");
1606 
1607 	if (envp == NULL) {
1608 		NDMP_LOG(LOG_DEBUG,
1609 		    "env(DMP_NAME) not specified, defaulting to 'level'");
1610 		(void) strlcpy(ndmpd_zfs_args->nz_dmp_name, "level",
1611 		    NDMPD_ZFS_DMP_NAME_MAX);
1612 		return (0);
1613 	}
1614 
1615 	if (!ndmpd_zfs_dmp_name_valid(ndmpd_zfs_args, envp))
1616 		return (-1);
1617 
1618 	(void) strlcpy(ndmpd_zfs_args->nz_dmp_name, envp,
1619 	    NDMPD_ZFS_DMP_NAME_MAX);
1620 
1621 	NDMP_LOG(LOG_DEBUG, "env(DMP_NAME): \"%s\"", envp);
1622 
1623 	return (0);
1624 }
1625 
1626 static int
1627 ndmpd_zfs_getenv_zfs_backup_size(ndmpd_zfs_args_t *ndmpd_zfs_args)
1628 {
1629 	char *zfs_backup_size;
1630 
1631 	zfs_backup_size = MOD_GETENV(ndmpd_zfs_params, "ZFS_BACKUP_SIZE");
1632 
1633 	if (zfs_backup_size == NULL) {
1634 		NDMP_LOG(LOG_ERR, "ZFS_BACKUP_SIZE env is NULL");
1635 		return (-1);
1636 	}
1637 
1638 	NDMP_LOG(LOG_DEBUG, "ZFS_BACKUP_SIZE: %s\n", zfs_backup_size);
1639 
1640 	(void) sscanf(zfs_backup_size, "%llu",
1641 	    &ndmpd_zfs_args->nz_zfs_backup_size);
1642 
1643 	return (0);
1644 }
1645 
1646 /*
1647  * ndmpd_zfs_dmp_name_valid()
1648  *
1649  * This function verifies that the dmp_name is valid.
1650  *
1651  * The dmp_name is restricted to alphanumeric characters plus
1652  * the underscore and hyphen, and must be 31 characters or less.
1653  * This is due to its use in the NDMPD_ZFS_PROP_INCR property
1654  * and in the ZFS snapshot name (if an ndmpd-generated snapshot
1655  * is required).
1656  */
1657 
1658 static boolean_t
1659 ndmpd_zfs_dmp_name_valid(ndmpd_zfs_args_t *ndmpd_zfs_args, char *dmp_name)
1660 {
1661 	char *c;
1662 
1663 	if (strlen(dmp_name) >= NDMPD_ZFS_DMP_NAME_MAX) {
1664 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1665 		    "DMP_NAME %s is longer than %d\n",
1666 		    dmp_name, NDMPD_ZFS_DMP_NAME_MAX-1);
1667 		return (B_FALSE);
1668 	}
1669 
1670 	for (c = dmp_name; *c != '\0'; c++) {
1671 		if (!isalpha(*c) && !isdigit(*c) &&
1672 		    (*c != '_') && (*c != '-')) {
1673 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1674 			    "DMP_NAME %s contains illegal character %c\n",
1675 			    dmp_name, *c);
1676 			return (B_FALSE);
1677 		}
1678 	}
1679 
1680 	NDMP_LOG(LOG_DEBUG, "DMP_NAME is valid: %s\n", dmp_name);
1681 	return (B_TRUE);
1682 }
1683 
1684 /*
1685  * ndmpd_zfs_is_incremental()
1686  *
1687  * This can only be called after ndmpd_zfs_getenv_level()
1688  * has been called.
1689  */
1690 
1691 static boolean_t
1692 ndmpd_zfs_is_incremental(ndmpd_zfs_args_t *ndmpd_zfs_args)
1693 {
1694 	return (ndmpd_zfs_args->nz_level != 0);
1695 }
1696 
1697 /*
1698  * ndmpd_zfs_snapshot_prepare()
1699  *
1700  * If no snapshot was supplied by the user, create a snapshot
1701  * for use by ndmpd.
1702  */
1703 
1704 static int
1705 ndmpd_zfs_snapshot_prepare(ndmpd_zfs_args_t *ndmpd_zfs_args)
1706 {
1707 	ndmpd_session_t *session = (ndmpd_session_t *)
1708 	    (ndmpd_zfs_params->mp_daemon_cookie);
1709 	boolean_t recursive = B_FALSE;
1710 	int zfs_err = 0;
1711 
1712 	if (session->ns_data.dd_abort) {
1713 		NDMP_LOG(LOG_DEBUG, "Backing up \"%s\" aborted.",
1714 		    ndmpd_zfs_args->nz_dataset);
1715 		return (-1);
1716 	}
1717 
1718 	if (ndmpd_zfs_args->nz_snapname[0] == '\0') {
1719 		ndmpd_zfs_args->nz_ndmpd_snap = B_TRUE;
1720 
1721 		if (ndmpd_zfs_snapshot_create(ndmpd_zfs_args) != 0) {
1722 			ndmpd_zfs_args->nz_snapname[0] = '\0';
1723 
1724 			ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1725 			    "Error creating snapshot for %s\n",
1726 			    ndmpd_zfs_args->nz_dataset);
1727 
1728 			return (-1);
1729 		}
1730 	}
1731 
1732 	if (ndmpd_zfs_snapshot_prop_add(ndmpd_zfs_args)) {
1733 		NDMP_LOG(LOG_DEBUG,
1734 		    "ndmpd_zfs_snapshot_prop_add error\n");
1735 
1736 		if (ndmpd_zfs_args->nz_ndmpd_snap) {
1737 
1738 			if (ndmpd_zfs_args->nz_zfs_mode != 'd')
1739 				recursive = B_TRUE;
1740 
1741 			(void) snapshot_destroy(ndmpd_zfs_args->nz_dataset,
1742 			    ndmpd_zfs_args->nz_snapname, recursive, B_FALSE,
1743 			    &zfs_err);
1744 		}
1745 
1746 		return (-1);
1747 	}
1748 
1749 	return (0);
1750 }
1751 
1752 /*
1753  * ndmpd_zfs_snapshot_cleanup()
1754  *
1755  * If UPDATE = y, find the old snapshot (if any) corresponding to
1756  * {LEVEL, DMP_NAME, ZFS_MODE}. If it was ndmpd-generated,
1757  * remove the snapshot.  Otherwise, update its NDMPD_ZFS_PROP_INCR
1758  * property to remove {L, D, Z}.
1759  *
1760  * If UPDATE = n, if an ndmpd-generated snapshot was used for backup,
1761  * remove the snapshot.  Otherwise, update its NDMPD_ZFS_PROP_INCR
1762  * property to remove {L, D, Z}.
1763  */
1764 
1765 static int
1766 ndmpd_zfs_snapshot_cleanup(ndmpd_zfs_args_t *ndmpd_zfs_args, int err)
1767 {
1768 	ndmpd_session_t *session = (ndmpd_session_t *)
1769 	    (ndmpd_zfs_params->mp_daemon_cookie);
1770 	ndmpd_zfs_snapfind_t snapdata;
1771 	boolean_t ndmpd_generated = B_FALSE;
1772 
1773 	bzero(&snapdata, sizeof (ndmpd_zfs_snapfind_t));
1774 
1775 	(void) ndmpd_zfs_prop_create_subprop(ndmpd_zfs_args,
1776 	    snapdata.nzs_findprop, ZFS_MAXPROPLEN, B_FALSE);
1777 
1778 	if (ndmpd_zfs_args->nz_update && !session->ns_data.dd_abort && !err) {
1779 		/*
1780 		 * Find the existing snapshot, if any, to "unuse."
1781 		 * Indicate that the current snapshot used for backup
1782 		 * should be skipped in the search.  (The search is
1783 		 * sorted by creation time but this cannot be relied
1784 		 * upon for user-supplied snapshots.)
1785 		 */
1786 
1787 		(void) snprintf(snapdata.nzs_snapskip, ZFS_MAXNAMELEN, "%s",
1788 		    ndmpd_zfs_args->nz_snapname);
1789 
1790 		if (ndmpd_zfs_snapshot_find(ndmpd_zfs_args, &snapdata)) {
1791 			NDMP_LOG(LOG_DEBUG, "ndmpd_zfs_snapshot_find error\n");
1792 			goto _remove_tmp_snap;
1793 		}
1794 
1795 		if (snapdata.nzs_snapname[0] != '\0') { /* snapshot found */
1796 			ndmpd_generated = ndmpd_zfs_snapshot_ndmpd_generated
1797 			    (snapdata.nzs_snapprop);
1798 
1799 			if (ndmpd_zfs_snapshot_unuse(ndmpd_zfs_args,
1800 			    ndmpd_generated, &snapdata) != 0) {
1801 				NDMP_LOG(LOG_DEBUG,
1802 				    "ndmpd_zfs_snapshot_unuse error\n");
1803 				goto _remove_tmp_snap;
1804 			}
1805 		}
1806 
1807 		if (session->ns_data.dd_abort)
1808 			goto _remove_tmp_snap;
1809 
1810 		return (0);
1811 	}
1812 
1813 _remove_tmp_snap:
1814 
1815 	(void) snprintf(snapdata.nzs_snapname, ZFS_MAXNAMELEN, "%s",
1816 	    ndmpd_zfs_args->nz_snapname);
1817 
1818 	(void) strlcpy(snapdata.nzs_snapprop, ndmpd_zfs_args->nz_snapprop,
1819 	    ZFS_MAXPROPLEN);
1820 
1821 	snapdata.nzs_snapskip[0] = '\0';
1822 
1823 	if (ndmpd_zfs_snapshot_unuse(ndmpd_zfs_args,
1824 	    ndmpd_zfs_args->nz_ndmpd_snap, &snapdata) != 0) {
1825 		NDMP_LOG(LOG_DEBUG, "ndmpd_zfs_snapshot_unuse error\n");
1826 		return (-1);
1827 	}
1828 
1829 	if (!ndmpd_zfs_args->nz_update)
1830 		return (0);
1831 
1832 	return (-1);
1833 }
1834 
1835 static int
1836 ndmpd_zfs_snapshot_create(ndmpd_zfs_args_t *ndmpd_zfs_args)
1837 {
1838 	boolean_t recursive = B_FALSE;
1839 
1840 	if (ndmpd_zfs_snapname_create(ndmpd_zfs_args,
1841 	    ndmpd_zfs_args->nz_snapname, ZFS_MAXNAMELEN -1) < 0) {
1842 		NDMP_LOG(LOG_ERR, "Error (%d) creating snapshot name for %s",
1843 		    errno, ndmpd_zfs_args->nz_dataset);
1844 		return (-1);
1845 	}
1846 
1847 	if (ndmpd_zfs_args->nz_zfs_mode != 'd')
1848 		recursive = B_TRUE;
1849 
1850 	if (snapshot_create(ndmpd_zfs_args->nz_dataset,
1851 	    ndmpd_zfs_args->nz_snapname, recursive, B_FALSE) != 0) {
1852 		NDMP_LOG(LOG_ERR, "could not create snapshot %s@%s",
1853 		    ndmpd_zfs_args->nz_dataset, ndmpd_zfs_args->nz_snapname);
1854 		return (-1);
1855 	}
1856 
1857 	NDMP_LOG(LOG_DEBUG, "created snapshot %s@%s",
1858 	    ndmpd_zfs_args->nz_dataset, ndmpd_zfs_args->nz_snapname);
1859 
1860 	return (0);
1861 }
1862 
1863 /*
1864  * ndmpd_zfs_snapshot_unuse()
1865  *
1866  * Given a pre-existing snapshot of the given {L, D, Z}:
1867  * If snapshot is ndmpd-generated, remove snapshot.
1868  * If not ndmpd-generated, or if the ndmpd-generated snapshot
1869  * cannot be destroyed, remove the {L, D, Z} substring from the
1870  * snapshot's NDMPD_ZFS_PROP_INCR property.
1871  *
1872  * In the event of a failure, it may be that two snapshots will
1873  * have the {L, D, Z} property set on them.  This is not desirable,
1874  * so return an error and log the failure.
1875  */
1876 
1877 static int
1878 ndmpd_zfs_snapshot_unuse(ndmpd_zfs_args_t *ndmpd_zfs_args,
1879     boolean_t ndmpd_generated, ndmpd_zfs_snapfind_t *snapdata_p)
1880 {
1881 	boolean_t recursive = B_FALSE;
1882 	int zfs_err = 0;
1883 	int err = 0;
1884 
1885 	if (ndmpd_generated) {
1886 		if (ndmpd_zfs_args->nz_zfs_mode != 'd')
1887 			recursive = B_TRUE;
1888 
1889 		err = snapshot_destroy(ndmpd_zfs_args->nz_dataset,
1890 		    snapdata_p->nzs_snapname, recursive, B_FALSE, &zfs_err);
1891 
1892 		if (err) {
1893 			NDMP_LOG(LOG_ERR, "snapshot_destroy: %s@%s;"
1894 			    " err: %d; zfs_err: %d",
1895 			    ndmpd_zfs_args->nz_dataset,
1896 			    snapdata_p->nzs_snapname, err, zfs_err);
1897 			return (-1);
1898 		}
1899 	}
1900 
1901 	if (!ndmpd_generated || zfs_err) {
1902 		if (ndmpd_zfs_snapshot_prop_remove(ndmpd_zfs_args, snapdata_p))
1903 			return (-1);
1904 	}
1905 
1906 	return (0);
1907 }
1908 
1909 static boolean_t
1910 ndmpd_zfs_snapshot_ndmpd_generated(char *snapprop)
1911 {
1912 	char origin;
1913 
1914 	(void) sscanf(snapprop, "%*u.%*u.%c%*s", &origin);
1915 
1916 	return (origin == 'n');
1917 }
1918 
1919 /*
1920  * ndmpd_zfs_snapshot_find()
1921  *
1922  * Find a snapshot with a particular value for
1923  * the NDMPD_ZFS_PROP_INCR property.
1924  */
1925 
1926 static int
1927 ndmpd_zfs_snapshot_find(ndmpd_zfs_args_t *ndmpd_zfs_args,
1928     ndmpd_zfs_snapfind_t *snapdata)
1929 {
1930 	zfs_handle_t *zhp;
1931 	int err;
1932 
1933 	zhp = zfs_open(ndmpd_zfs_args->nz_zlibh, ndmpd_zfs_args->nz_dataset,
1934 	    ndmpd_zfs_args->nz_type);
1935 
1936 	if (!zhp) {
1937 		NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_open");
1938 		return (-1);
1939 	}
1940 
1941 	err = zfs_iter_snapshots_sorted(zhp, ndmpd_zfs_snapshot_prop_find,
1942 	    snapdata);
1943 
1944 	zfs_close(zhp);
1945 
1946 	if (err) {
1947 		NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_iter_snapshots: %d",
1948 		    err);
1949 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
1950 		    "Error iterating snapshots\n");
1951 		return (-1);
1952 	}
1953 
1954 	return (0);
1955 }
1956 
1957 /*
1958  * ndmpd_zfs_snapshot_prop_find()
1959  *
1960  * Find a snapshot with a particular value for
1961  * NDMPD_ZFS_PROP_INCR.  Fill in data for the first one
1962  * found (sorted by creation time).  However, skip the
1963  * the snapshot indicated in nzs_snapskip, if any.
1964  */
1965 
1966 static int
1967 ndmpd_zfs_snapshot_prop_find(zfs_handle_t *zhp, void *arg)
1968 {
1969 	ndmpd_zfs_snapfind_t *snapdata_p = (ndmpd_zfs_snapfind_t *)arg;
1970 	char propstr[ZFS_MAXPROPLEN];
1971 	char findprop_plus_slash[ZFS_MAXPROPLEN];
1972 	char *justsnap;
1973 	int err = 0;
1974 
1975 	if (snapdata_p->nzs_snapname[0] != '\0') {
1976 		NDMP_LOG(LOG_DEBUG, "no need to get prop for snapshot %s",
1977 		    (char *)zfs_get_name(zhp));
1978 		goto _done;
1979 	}
1980 
1981 	err = ndmpd_zfs_snapshot_prop_get(zhp, propstr);
1982 
1983 	if (err) {
1984 		NDMP_LOG(LOG_DEBUG, "ndmpd_zfs_snapshot_prop_get failed");
1985 		goto _done;
1986 	}
1987 
1988 	if (propstr[0] == '\0') {
1989 		NDMP_LOG(LOG_DEBUG, "snapshot %s: propr not set",
1990 		    (char *)zfs_get_name(zhp));
1991 		goto _done;
1992 	}
1993 
1994 	(void) snprintf(findprop_plus_slash, ZFS_MAXPROPLEN, "/%s",
1995 	    snapdata_p->nzs_findprop);
1996 
1997 	if (!strstr((const char *)propstr,
1998 	    (const char *)findprop_plus_slash)) {
1999 		NDMP_LOG(LOG_DEBUG, "snapshot %s: property %s (%s not found)",
2000 		    (char *)zfs_get_name(zhp), propstr,
2001 		    snapdata_p->nzs_findprop);
2002 		goto _done;
2003 	}
2004 
2005 	if (!ndmpd_zfs_prop_version_check(propstr,
2006 	    &snapdata_p->nzs_prop_major, &snapdata_p->nzs_prop_minor)) {
2007 		NDMP_LOG(LOG_DEBUG, "snapshot %s: property %s (%s found)",
2008 		    (char *)zfs_get_name(zhp),  propstr,
2009 		    snapdata_p->nzs_findprop);
2010 		NDMP_LOG(LOG_DEBUG, "did not pass version check");
2011 		goto _done;
2012 	}
2013 
2014 	justsnap = strchr(zfs_get_name(zhp), '@') + 1;
2015 
2016 	if (strcmp(justsnap, snapdata_p->nzs_snapskip) != 0) {
2017 		(void) strlcpy(snapdata_p->nzs_snapname, justsnap,
2018 		    ZFS_MAXNAMELEN);
2019 
2020 		(void) strlcpy(snapdata_p->nzs_snapprop, propstr,
2021 		    ZFS_MAXPROPLEN);
2022 
2023 		NDMP_LOG(LOG_DEBUG, "found match: %s [%s]\n",
2024 		    snapdata_p->nzs_snapname, snapdata_p->nzs_snapprop);
2025 	}
2026 
2027 _done:
2028 	zfs_close(zhp);
2029 	return (err);
2030 }
2031 
2032 /*
2033  * ndmpd_zfs_snapshot_prop_get()
2034  *
2035  * Retrieve NDMPD_ZFS_PROP_INCR property from snapshot
2036  */
2037 
2038 static int
2039 ndmpd_zfs_snapshot_prop_get(zfs_handle_t *zhp, char *propstr)
2040 {
2041 	nvlist_t *userprop;
2042 	nvlist_t *propval;
2043 	char *strval;
2044 	int err;
2045 
2046 	propstr[0] = '\0';
2047 
2048 	userprop = zfs_get_user_props(zhp);
2049 
2050 	if (userprop == NULL)
2051 		return (0);
2052 
2053 	err = nvlist_lookup_nvlist(userprop, NDMPD_ZFS_PROP_INCR, &propval);
2054 
2055 	if (err != 0) {
2056 		if (err == ENOENT)
2057 			return (0);
2058 
2059 		NDMP_LOG(LOG_DEBUG, "nvlist_lookup_nvlist error: %d\n", err);
2060 
2061 		return (-1);
2062 	}
2063 
2064 	err = nvlist_lookup_string(propval, ZPROP_VALUE, &strval);
2065 
2066 	if (err != 0) {
2067 		if (err == ENOENT)
2068 			return (0);
2069 
2070 		NDMP_LOG(LOG_DEBUG, "nvlist_lookup_string error: %d\n", err);
2071 
2072 		return (-1);
2073 	}
2074 
2075 	(void) strlcpy(propstr, strval, ZFS_MAXPROPLEN);
2076 
2077 	return (0);
2078 }
2079 
2080 /*
2081  * ndmpd_zfs_snapshot_prop_add()
2082  *
2083  * Update snapshot's NDMPD_ZFS_PROP_INCR property with
2084  * the current LEVEL, DMP_NAME, and ZFS_MODE values
2085  * (add property if it doesn't exist)
2086  */
2087 
2088 static int
2089 ndmpd_zfs_snapshot_prop_add(ndmpd_zfs_args_t *ndmpd_zfs_args)
2090 {
2091 	char fullname[ZFS_MAXNAMELEN];
2092 	char propstr[ZFS_MAXPROPLEN];
2093 	zfs_handle_t *zhp;
2094 	boolean_t set;
2095 	int err;
2096 
2097 	(void) snprintf(fullname, ZFS_MAXNAMELEN, "%s@%s",
2098 	    ndmpd_zfs_args->nz_dataset,
2099 	    ndmpd_zfs_args->nz_snapname);
2100 
2101 	zhp = zfs_open(ndmpd_zfs_args->nz_zlibh, fullname, ZFS_TYPE_SNAPSHOT);
2102 
2103 	if (!zhp) {
2104 		NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_open (snap)");
2105 		return (-1);
2106 	}
2107 
2108 	bzero(propstr, ZFS_MAXPROPLEN);
2109 
2110 	if (ndmpd_zfs_snapshot_prop_get(zhp, propstr)) {
2111 		NDMP_LOG(LOG_DEBUG, "error getting property");
2112 		zfs_close(zhp);
2113 		return (-1);
2114 	}
2115 
2116 	if (ndmpd_zfs_snapshot_prop_create(ndmpd_zfs_args, propstr, &set)) {
2117 		zfs_close(zhp);
2118 		return (-1);
2119 	}
2120 
2121 	if (set) {
2122 		err = zfs_prop_set(zhp, NDMPD_ZFS_PROP_INCR, propstr);
2123 		if (err) {
2124 			NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_prop_set: %d",
2125 			    err);
2126 			NDMP_LOG(LOG_ERR, "error setting property %s",
2127 			    propstr);
2128 			zfs_close(zhp);
2129 			return (-1);
2130 		}
2131 	}
2132 
2133 	zfs_close(zhp);
2134 
2135 	(void) strlcpy(ndmpd_zfs_args->nz_snapprop, propstr, ZFS_MAXPROPLEN);
2136 
2137 	return (0);
2138 }
2139 
2140 static int
2141 ndmpd_zfs_snapshot_prop_create(ndmpd_zfs_args_t *ndmpd_zfs_args,
2142     char *propstr, boolean_t *set)
2143 {
2144 	char subprop[ZFS_MAXPROPLEN];
2145 	char *p = propstr;
2146 	int slash_count = 0;
2147 
2148 	*set = B_TRUE;
2149 
2150 	(void) ndmpd_zfs_prop_create_subprop(ndmpd_zfs_args,
2151 	    subprop, ZFS_MAXPROPLEN, B_FALSE);
2152 
2153 	if (propstr[0] == '\0') {
2154 		(void) snprintf(propstr, ZFS_MAXPROPLEN, "%u.%u.%c/%s",
2155 		    NDMPD_ZFS_PROP_MAJOR_VERSION,
2156 		    NDMPD_ZFS_PROP_MINOR_VERSION,
2157 		    (ndmpd_zfs_args->nz_ndmpd_snap) ? 'n' : 'u',
2158 		    subprop);
2159 		return (0);
2160 	}
2161 
2162 	if (strstr((const char *)propstr, (const char *)subprop)) {
2163 		NDMP_LOG(LOG_DEBUG, "Did not add entry %s as it already exists",
2164 		    subprop);
2165 		*set = B_FALSE;
2166 		return (0);
2167 	}
2168 
2169 	while (*p) {
2170 		if (*(p++) == '/')
2171 			slash_count++;
2172 	}
2173 
2174 	if (slash_count >= NDMPD_ZFS_SUBPROP_MAX) {
2175 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2176 		    "snapshot %s: user property %s limit of %d subprops "
2177 		    "reached; cannot complete operation",
2178 		    ndmpd_zfs_args->nz_snapname,
2179 		    NDMPD_ZFS_PROP_INCR,
2180 		    NDMPD_ZFS_SUBPROP_MAX);
2181 		return (-1);
2182 	}
2183 
2184 	assert((strlen(propstr) + strlen(subprop) + 2) < ZFS_MAXPROPLEN);
2185 
2186 	(void) strlcat(propstr, "/", ZFS_MAXPROPLEN);
2187 	(void) strlcat(propstr, subprop, ZFS_MAXPROPLEN);
2188 
2189 	return (0);
2190 }
2191 
2192 static int
2193 ndmpd_zfs_prop_create_subprop(ndmpd_zfs_args_t *ndmpd_zfs_args,
2194     char *subprop, int len, boolean_t prev_level)
2195 {
2196 	return (snprintf(subprop, len, "%d.%s.%c",
2197 	    prev_level ? ndmpd_zfs_args->nz_level-1 : ndmpd_zfs_args->nz_level,
2198 	    ndmpd_zfs_args->nz_dmp_name,
2199 	    ndmpd_zfs_args->nz_zfs_mode));
2200 }
2201 
2202 /*
2203  * ndmpd_zfs_snapshot_prop_remove()
2204  *
2205  * Remove specified substring from the snapshot's
2206  * NDMPD_ZFS_PROP_INCR property
2207  */
2208 
2209 static int
2210 ndmpd_zfs_snapshot_prop_remove(ndmpd_zfs_args_t *ndmpd_zfs_args,
2211     ndmpd_zfs_snapfind_t *snapdata_p)
2212 {
2213 	char findprop_plus_slash[ZFS_MAXPROPLEN];
2214 	char fullname[ZFS_MAXNAMELEN];
2215 	char newprop[ZFS_MAXPROPLEN];
2216 	char tmpstr[ZFS_MAXPROPLEN];
2217 	zfs_handle_t *zhp;
2218 	char *ptr;
2219 	int err;
2220 
2221 	(void) snprintf(fullname, ZFS_MAXNAMELEN, "%s@%s",
2222 	    ndmpd_zfs_args->nz_dataset,
2223 	    snapdata_p->nzs_snapname);
2224 
2225 	zhp = zfs_open(ndmpd_zfs_args->nz_zlibh, fullname, ZFS_TYPE_SNAPSHOT);
2226 
2227 	if (!zhp) {
2228 		NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_open");
2229 		return (-1);
2230 	}
2231 
2232 	bzero(newprop, ZFS_MAXPROPLEN);
2233 
2234 	/*
2235 	 * If the substring to be removed is the only {L, D, Z}
2236 	 * in the property, remove the entire property
2237 	 */
2238 
2239 	tmpstr[0] = '\0';
2240 
2241 	(void) sscanf(snapdata_p->nzs_snapprop, "%*u.%*u.%*c/%1023s", tmpstr);
2242 
2243 	if (strcmp(tmpstr, snapdata_p->nzs_findprop) == 0) {
2244 
2245 		err = zfs_prop_set(zhp, NDMPD_ZFS_PROP_INCR, newprop);
2246 
2247 		zfs_close(zhp);
2248 
2249 		if (err) {
2250 			NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_prop_set: %d",
2251 			    err);
2252 			NDMP_LOG(LOG_ERR, "error setting property %s", newprop);
2253 			return (-1);
2254 		}
2255 
2256 		return (0);
2257 	}
2258 
2259 	(void) snprintf(findprop_plus_slash, ZFS_MAXPROPLEN, "/%s",
2260 	    snapdata_p->nzs_findprop);
2261 
2262 	ptr = strstr((const char *)snapdata_p->nzs_snapprop,
2263 	    (const char *)findprop_plus_slash);
2264 
2265 	if (ptr == NULL) {
2266 		/*
2267 		 * This shouldn't happen.  Just return success.
2268 		 */
2269 		zfs_close(zhp);
2270 
2271 		return (0);
2272 	}
2273 
2274 	/*
2275 	 * Remove "nzs_findprop" substring from property
2276 	 *
2277 	 * Example property:
2278 	 *	0.0.u/1.bob.p/0.jane.d
2279 	 *
2280 	 * Note that there will always be a prefix to the
2281 	 * strstr() result.  Hence the below code works for
2282 	 * all cases.
2283 	 */
2284 
2285 	ptr--;
2286 	(void) strncpy(newprop, snapdata_p->nzs_snapprop,
2287 	    (char *)ptr - snapdata_p->nzs_snapprop);
2288 	ptr += strlen(snapdata_p->nzs_findprop) + 1;
2289 	(void) strlcat(newprop, ptr, ZFS_MAXPROPLEN);
2290 
2291 	err = zfs_prop_set(zhp, NDMPD_ZFS_PROP_INCR, newprop);
2292 
2293 	zfs_close(zhp);
2294 
2295 	if (err) {
2296 		NDMPD_ZFS_LOG_ZERR(ndmpd_zfs_args, "zfs_prop_set: %d", err);
2297 		NDMP_LOG(LOG_ERR, "error modifying property %s", newprop);
2298 		return (-1);
2299 	}
2300 
2301 	return (0);
2302 }
2303 
2304 static boolean_t
2305 ndmpd_zfs_prop_version_check(char *propstr, uint32_t *major, uint32_t *minor)
2306 {
2307 	(void) sscanf(propstr, "%u.%u.%*c%*s", major, minor);
2308 
2309 	if (*major > NDMPD_ZFS_PROP_MAJOR_VERSION) {
2310 		NDMP_LOG(LOG_ERR, "unsupported prop major (%u > %u)",
2311 		    *major, NDMPD_ZFS_PROP_MAJOR_VERSION);
2312 		return (B_FALSE);
2313 	}
2314 
2315 	if (*minor > NDMPD_ZFS_PROP_MINOR_VERSION) {
2316 		NDMP_LOG(LOG_ERR, "later prop minor (%u > %u)",
2317 		    *minor, NDMPD_ZFS_PROP_MINOR_VERSION);
2318 	}
2319 
2320 	NDMP_LOG(LOG_DEBUG, "passed version check: "
2321 	    "supported prop major (%u <= %u); (snapprop minor: %u [%u])",
2322 	    *major, NDMPD_ZFS_PROP_MAJOR_VERSION,
2323 	    *minor, NDMPD_ZFS_PROP_MINOR_VERSION);
2324 
2325 	return (B_TRUE);
2326 }
2327 
2328 static int
2329 ndmpd_zfs_snapname_create(ndmpd_zfs_args_t *ndmpd_zfs_args,
2330     char *snapname, int namelen)
2331 {
2332 	char subprop[ZFS_MAXPROPLEN];
2333 	struct timeval tp;
2334 	int err = 0;
2335 
2336 	(void) ndmpd_zfs_prop_create_subprop(ndmpd_zfs_args,
2337 	    subprop, ZFS_MAXPROPLEN, B_FALSE);
2338 
2339 	(void) gettimeofday(&tp, NULL);
2340 
2341 	err = snprintf(snapname, namelen,
2342 	    "ndmp.%s.%ld.%ld",
2343 	    subprop,
2344 	    tp.tv_sec,
2345 	    tp.tv_usec);
2346 
2347 	if (err > namelen) {
2348 		NDMP_LOG(LOG_ERR, "name of snapshot [%s...] would exceed %d",
2349 		    snapname, namelen);
2350 		return (-1);
2351 	}
2352 
2353 	return (0);
2354 }
2355 
2356 static void
2357 ndmpd_zfs_zerr_dma_log(ndmpd_zfs_args_t *ndmpd_zfs_args)
2358 {
2359 	switch (libzfs_errno(ndmpd_zfs_args->nz_zlibh)) {
2360 	case EZFS_EXISTS:
2361 	case EZFS_BUSY:
2362 	case EZFS_NOENT:
2363 	case EZFS_INVALIDNAME:
2364 	case EZFS_MOUNTFAILED:
2365 	case EZFS_UMOUNTFAILED:
2366 	case EZFS_NAMETOOLONG:
2367 	case EZFS_BADRESTORE:
2368 
2369 		/* use existing error text */
2370 
2371 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2372 		    "%s: %s: %s\n",
2373 		    ndmpd_zfs_args->nz_dataset,
2374 		    libzfs_error_action(ndmpd_zfs_args->nz_zlibh),
2375 		    libzfs_error_description(ndmpd_zfs_args->nz_zlibh));
2376 
2377 		break;
2378 
2379 	case EZFS_NOMEM:
2380 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2381 		    "Unable to obtain memory for operation\n");
2382 		break;
2383 
2384 	case EZFS_PROPSPACE:
2385 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2386 		    "A bad ZFS quota or reservation was encountered.\n");
2387 		break;
2388 
2389 	case EZFS_BADSTREAM:
2390 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2391 		    "The backup stream is invalid.\n");
2392 		break;
2393 
2394 	case EZFS_ZONED:
2395 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2396 		    "An error related to the local zone occurred.\n");
2397 		break;
2398 
2399 	case EZFS_NOSPC:
2400 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2401 		    "No more space is available\n");
2402 		break;
2403 
2404 	case EZFS_IO:
2405 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2406 		    "An I/O error occurred.\n");
2407 		break;
2408 
2409 	default:
2410 		ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_ERROR,
2411 		    "An internal ndmpd error occurred.  "
2412 		    "Please contact support\n");
2413 		break;
2414 	}
2415 }
2416 
2417 void
2418 ndmpd_zfs_dma_log(ndmpd_zfs_args_t *ndmpd_zfs_args, ndmp_log_type log_type,
2419     char *format, ...)
2420 {
2421 	static char buf[1024];
2422 	va_list ap;
2423 
2424 	va_start(ap, format);
2425 
2426 	/*LINTED variable format specifier */
2427 	(void) vsnprintf(buf, sizeof (buf), format, ap);
2428 	va_end(ap);
2429 
2430 	MOD_LOGV3(ndmpd_zfs_params, log_type, buf);
2431 
2432 	if ((log_type) == NDMP_LOG_ERROR) {
2433 		NDMP_LOG(LOG_ERR, buf);
2434 	} else {
2435 		NDMP_LOG(LOG_NOTICE, buf);
2436 	}
2437 }
2438