--- old/usr/src/cmd/ndmpd/ndmp/ndmpd_data.c	Mon Aug  5 17:15:16 2019
+++ new/usr/src/cmd/ndmpd/ndmp/ndmpd_data.c	Mon Aug  5 17:15:16 2019
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
  */
 
 /*
@@ -41,6 +41,7 @@
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/socket.h>
+#include <syslog.h>
 #include <netinet/in.h>
 #include <errno.h>
 #include <arpa/inet.h>
@@ -71,10 +72,6 @@
 static ndmp_error ndmpd_tar_start_recover_v3(ndmpd_session_t *,
     ndmp_pval *, ulong_t, ndmp_name_v3 *, ulong_t);
 
-static ndmp_error ndmpd_zfs_start_op(ndmpd_session_t *,
-    ndmp_pval *, ulong_t, ndmp_name_v3 *, ulong_t, enum ndmp_data_operation);
-
-
 /*
  * ************************************************************************
  * NDMP V2 HANDLERS
@@ -156,7 +153,7 @@
 	 * Otherwise, send the reply containing the error here.
 	 */
 	if (err != NDMP_NO_ERR) {
-		NDMP_LOG(LOG_DEBUG, "err: %d", err);
+		syslog(LOG_ERR, "err: %d", err);
 		reply.error = err;
 		ndmp_send_reply(connection, &reply,
 		    "sending data_start_backup reply");
@@ -226,7 +223,7 @@
 
 	(void) memset((void*)&reply, 0, sizeof (reply));
 	if (session->ns_data.dd_operation != NDMP_DATA_OP_BACKUP) {
-		NDMP_LOG(LOG_ERR, "Backup operation not active.");
+		syslog(LOG_ERR, "Backup operation not active.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		reply.env.env_len = 0;
 	} else {
@@ -259,7 +256,6 @@
 	ndmpd_session_t *session = ndmp_get_client_data(connection);
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_HALTED) {
-		NDMP_LOG(LOG_ERR, "Invalid state to process stop request.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		ndmp_send_reply(connection, &reply,
 		    "sending data_stop reply");
@@ -303,7 +299,6 @@
 
 	if (session->ns_data.dd_state == NDMP_DATA_STATE_IDLE ||
 	    session->ns_data.dd_state == NDMP_DATA_STATE_HALTED) {
-		NDMP_LOG(LOG_ERR, "Invalid state to process abort request.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		ndmp_send_reply(connection, &reply,
 		    "sending data_abort reply");
@@ -401,10 +396,8 @@
 	(void) memset((void*)&reply, 0, sizeof (reply));
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_CONNECTED) {
-		NDMP_LOG(LOG_ERR,
-		    "Can't start new backup in current state.");
-		NDMP_LOG(LOG_ERR,
-		    "Connection to the mover is not established.");
+		syslog(LOG_ERR,
+		    "Can't start new backup in NOT CONNECTED state.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		goto _error;
 	}
@@ -411,7 +404,7 @@
 
 	if (session->ns_data.dd_data_addr.addr_type == NDMP_ADDR_LOCAL) {
 		if (session->ns_tape.td_mode == NDMP_TAPE_READ_MODE) {
-			NDMP_LOG(LOG_ERR, "Write protected device.");
+			syslog(LOG_ERR, "Write protected device.");
 			reply.error = NDMP_WRITE_PROTECT_ERR;
 			goto _error;
 		}
@@ -430,27 +423,24 @@
 		(void) snprintf(msg_invalid, 32, "Invalid backup type: %s.",
 		    request->bu_type);
 		(void) snprintf(msg_types, 32,
-		    "Supported backup types are tar, dump, and zfs.");
+		    "Supported backup types are tar, dump.");
 
 		NDMP_APILOG((void *) session, NDMP_LOG_ERROR, ++ndmp_log_msg_id,
 		    msg_invalid);
 		NDMP_APILOG((void *) session, NDMP_LOG_ERROR, ++ndmp_log_msg_id,
 		    msg_types);
-		NDMP_LOG(LOG_ERR, msg_invalid);
-		NDMP_LOG(LOG_ERR, msg_types);
+		syslog(LOG_ERR, "Invalid backup type: %s.",
+		    request->bu_type);
+		syslog(LOG_ERR,
+		    "Supported backup types are tar, dump.");
 
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
 		goto _error;
 	}
 
-	if (session->ns_butype == NDMP_BUTYPE_ZFS) {
-		reply.error = ndmpd_zfs_start_op(session, request->env.env_val,
-		    request->env.env_len, NULL, 0, NDMP_DATA_OP_BACKUP);
-	} else {
-		reply.error = ndmpd_tar_start_backup_v3(session,
-		    request->bu_type, request->env.env_val,
-		    request->env.env_len);
-	}
+	reply.error = ndmpd_tar_start_backup_v3(session,
+	    request->bu_type, request->env.env_val,
+	    request->env.env_len);
 
 	/*
 	 * *_start_backup* sends the reply if the backup is
@@ -491,7 +481,7 @@
 	(void) memset((void*)&reply, 0, sizeof (reply));
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_CONNECTED) {
-		NDMP_LOG(LOG_ERR, "Can't start new recover in current state.");
+		syslog(LOG_ERR, "Can't start new recover in current state.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		goto _error;
 	}
@@ -509,28 +499,24 @@
 		(void) snprintf(msg_invalid, 32, "Invalid backup type: %s.",
 		    request->bu_type);
 		(void) snprintf(msg_types, 32,
-		    "Supported backup types are tar, dump, and zfs.");
+		    "Supported backup types are tar, dump.");
 
 		NDMP_APILOG((void *) session, NDMP_LOG_ERROR, ++ndmp_log_msg_id,
 		    msg_invalid);
 		NDMP_APILOG((void *) session, NDMP_LOG_ERROR, ++ndmp_log_msg_id,
 		    msg_types);
-		NDMP_LOG(LOG_ERR, msg_invalid);
-		NDMP_LOG(LOG_ERR, msg_types);
+		syslog(LOG_ERR, "Invalid backup type: %s.",
+		    request->bu_type);
+		syslog(LOG_ERR,
+		    "Supported backup types are tar, dump.");
 
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
 		goto _error;
 	}
 
-	if (session->ns_butype == NDMP_BUTYPE_ZFS) {
-		reply.error = ndmpd_zfs_start_op(session, request->env.env_val,
-		    request->env.env_len, request->nlist.nlist_val,
-		    request->nlist.nlist_len, NDMP_DATA_OP_RECOVER);
-	} else {
-		reply.error = ndmpd_tar_start_recover_v3(session,
-		    request->env.env_val, request->env.env_len,
-		    request->nlist.nlist_val, request->nlist.nlist_len);
-	}
+	reply.error = ndmpd_tar_start_recover_v3(session,
+	    request->env.env_val, request->env.env_len,
+	    request->nlist.nlist_val, request->nlist.nlist_len);
 
 	/*
 	 * *_start_recover* sends the reply if the recover is
@@ -572,7 +558,6 @@
 	switch (session->ns_data.dd_state) {
 	case NDMP_DATA_STATE_IDLE:
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_ERR, "Invalid state to process abort request.");
 		break;
 
 	case NDMP_DATA_STATE_ACTIVE:
@@ -597,7 +582,7 @@
 		break;
 	default:
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_DEBUG, "Unknown data V3 state %d",
+		syslog(LOG_ERR, "Unknown data V3 state %d",
 		    session->ns_data.dd_state);
 	}
 
@@ -626,7 +611,6 @@
 	ndmpd_session_t *session = ndmp_get_client_data(connection);
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_HALTED) {
-		NDMP_LOG(LOG_ERR, "Invalid state to process stop request.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		ndmp_send_reply(connection, &reply,
 		    "sending data_stop_v3 reply");
@@ -674,11 +658,11 @@
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_IDLE) {
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_ERR,
+		syslog(LOG_ERR,
 		    "Invalid internal data state to process listen request.");
 	} else if (session->ns_mover.md_state != NDMP_MOVER_STATE_IDLE) {
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_ERR,
+		syslog(LOG_ERR,
 		    "Invalid mover state to process listen request.");
 	} else {
 		reply.error = NDMP_NO_ERR;
@@ -708,12 +692,12 @@
 		session->ns_data.dd_data_addr.addr_type = NDMP_ADDR_TCP;
 		session->ns_data.dd_data_addr.tcp_ip_v3 = addr;
 		session->ns_data.dd_data_addr.tcp_port_v3 = ntohs(port);
-		NDMP_LOG(LOG_DEBUG, "listen_socket: %d",
+		syslog(LOG_DEBUG, "listen_socket: %d",
 		    session->ns_data.dd_listen_sock);
 		break;
 
 	default:
-		NDMP_LOG(LOG_DEBUG, "Invalid address type: %d",
+		syslog(LOG_ERR, "Invalid address type: %d",
 		    request->addr_type);
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
 		break;
@@ -753,11 +737,10 @@
 
 	if (!ndmp_valid_v3addr_type(request->addr.addr_type)) {
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
-		NDMP_LOG(LOG_DEBUG, "Invalid address type %d",
+		syslog(LOG_ERR, "Invalid address type %d",
 		    request->addr.addr_type);
 	} else if (session->ns_data.dd_state != NDMP_DATA_STATE_IDLE) {
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_ERR, "Invalid state to process connect request.");
 	} else {
 		reply.error = NDMP_NO_ERR;
 	}
@@ -777,7 +760,7 @@
 		if (session->ns_mover.md_state != NDMP_MOVER_STATE_LISTEN ||
 		    session->ns_mover.md_listen_sock != -1) {
 			reply.error = NDMP_ILLEGAL_STATE_ERR;
-			NDMP_LOG(LOG_ERR,
+			syslog(LOG_ERR,
 			    "Mover is not in local listen state.");
 		} else {
 			session->ns_mover.md_state = NDMP_MOVER_STATE_ACTIVE;
@@ -791,7 +774,7 @@
 
 	default:
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
-		NDMP_LOG(LOG_DEBUG, "Invalid address type %d",
+		syslog(LOG_ERR, "Invalid address type %d",
 		    request->addr.addr_type);
 	}
 
@@ -834,11 +817,10 @@
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_ACTIVE &&
 	    session->ns_data.dd_state != NDMP_DATA_STATE_HALTED) {
-		NDMP_LOG(LOG_ERR, "Invalid state for the data server.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		reply.env.env_len = 0;
 	} else if (session->ns_data.dd_operation != NDMP_DATA_OP_BACKUP) {
-		NDMP_LOG(LOG_ERR, "Backup operation not active.");
+		syslog(LOG_ERR, "Backup operation not active.");
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
 		reply.env.env_len = 0;
 	} else {
@@ -926,11 +908,10 @@
 
 	if (!ndmp_valid_v3addr_type(request->addr.addr_type)) {
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
-		NDMP_LOG(LOG_DEBUG, "Invalid address type %d",
+		syslog(LOG_ERR, "Invalid address type %d",
 		    request->addr.addr_type);
 	} else if (session->ns_data.dd_state != NDMP_DATA_STATE_IDLE) {
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_ERR, "Invalid state to process connect request.");
 	} else {
 		reply.error = NDMP_NO_ERR;
 	}
@@ -950,7 +931,7 @@
 		if (session->ns_mover.md_state != NDMP_MOVER_STATE_LISTEN ||
 		    session->ns_mover.md_listen_sock != -1) {
 			reply.error = NDMP_ILLEGAL_STATE_ERR;
-			NDMP_LOG(LOG_ERR,
+			syslog(LOG_ERR,
 			    "Mover is not in local listen state.");
 		} else {
 			session->ns_mover.md_state = NDMP_MOVER_STATE_ACTIVE;
@@ -964,7 +945,7 @@
 
 	default:
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
-		NDMP_LOG(LOG_DEBUG, "Invalid address type %d",
+		syslog(LOG_ERR, "Invalid address type %d",
 		    request->addr.addr_type);
 	}
 
@@ -1003,11 +984,11 @@
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_IDLE) {
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_ERR,
+		syslog(LOG_ERR,
 		    "Invalid internal data state to process listen request.");
 	} else if (session->ns_mover.md_state != NDMP_MOVER_STATE_IDLE) {
 		reply.error = NDMP_ILLEGAL_STATE_ERR;
-		NDMP_LOG(LOG_ERR,
+		syslog(LOG_ERR,
 		    "Invalid mover state to process listen request.");
 	} else {
 		reply.error = NDMP_NO_ERR;
@@ -1051,12 +1032,12 @@
 		session->ns_data.dd_data_addr.addr_type = NDMP_ADDR_TCP;
 		session->ns_data.dd_data_addr.tcp_ip_v3 = addr;
 		session->ns_data.dd_data_addr.tcp_port_v3 = ntohs(port);
-		NDMP_LOG(LOG_DEBUG, "listen_socket: %d",
+		syslog(LOG_DEBUG, "listen_socket: %d",
 		    session->ns_data.dd_listen_sock);
 		break;
 
 	default:
-		NDMP_LOG(LOG_DEBUG, "Invalid address type: %d",
+		syslog(LOG_ERR, "Invalid address type: %d",
 		    request->addr_type);
 		reply.error = NDMP_ILLEGAL_ARGS_ERR;
 		break;
@@ -1089,7 +1070,7 @@
 {
 	ndmp_data_start_recover_filehist_reply_v4 reply;
 
-	NDMP_LOG(LOG_DEBUG, "Request not supported");
+	syslog(LOG_DEBUG, "Request not supported");
 	reply.error = NDMP_NOT_SUPPORTED_ERR;
 
 	ndmp_send_reply(connection, &reply,
@@ -1195,11 +1176,11 @@
 
 	if (session->ns_protocol_version == NDMPV4) {
 		if (ndmpd_data_error_send_v4(session, reason) < 0)
-			NDMP_LOG(LOG_DEBUG,
+			syslog(LOG_ERR,
 			    "Error sending notify_data_halted request");
 	} else {
 		if (ndmpd_data_error_send(session, reason) < 0)
-			NDMP_LOG(LOG_DEBUG,
+			syslog(LOG_ERR,
 			    "Error sending notify_data_halted request");
 	}
 
@@ -1257,9 +1238,9 @@
 	session->ns_data.dd_sock = accept(fd, (struct sockaddr *)&from,
 	    &from_len);
 
-	NDMP_LOG(LOG_DEBUG, "sock fd: %d",
+	syslog(LOG_DEBUG, "sock fd: %d",
 	    session->ns_data.dd_sock);
-	NDMP_LOG(LOG_DEBUG, "sin: port %d addr %s",
+	syslog(LOG_DEBUG, "sin: port %d addr %s",
 	    ntohs(from.sin_port),
 	    inet_ntoa(IN_ADDR(from.sin_addr.s_addr)));
 
@@ -1268,7 +1249,7 @@
 	session->ns_data.dd_listen_sock = -1;
 
 	if (session->ns_data.dd_sock < 0) {
-		NDMP_LOG(LOG_DEBUG, "Accept error: %m");
+		syslog(LOG_ERR, "Accept error: %m");
 		ndmpd_data_error(session, NDMP_DATA_HALT_CONNECT_ERROR);
 		return;
 	}
@@ -1311,7 +1292,7 @@
 		session->ns_data.dd_listen_sock = -1;
 		return (-1);
 	}
-	NDMP_LOG(LOG_DEBUG, "addr: %s:%d",
+	syslog(LOG_DEBUG, "addr: %s:%d",
 	    inet_ntoa(IN_ADDR(*addr)), ntohs(*port));
 
 	return (0);
@@ -1373,6 +1354,7 @@
 	ndmp_lbr_params_t *nlp;
 	ndmpd_module_params_t *params;
 	ndmp_data_start_backup_reply_v3 reply;
+	pthread_t tid;
 
 	(void) memset((void*)&reply, 0, sizeof (reply));
 
@@ -1442,7 +1424,7 @@
 
 	reply.error = ndmp_backup_get_params_v3(session, params);
 	if (reply.error != NDMP_NO_ERR) {
-		NDMP_LOG(LOG_DEBUG, "err: %d", err);
+		syslog(LOG_ERR, "err: %d", err);
 		NDMP_FREE(nlp->nlp_params);
 		return (reply.error);
 	}
@@ -1450,7 +1432,7 @@
 	reply.error = NDMP_NO_ERR;
 	if (ndmp_send_response(session->ns_connection, NDMP_NO_ERR,
 	    &reply) < 0) {
-		NDMP_LOG(LOG_DEBUG, "Sending data_start_backup_v3 reply");
+		syslog(LOG_DEBUG, "Sending data_start_backup_v3 reply");
 		return (NDMP_NO_ERR);
 	}
 
@@ -1465,14 +1447,17 @@
 	 * Cannot wait for the thread to exit as we are replying to the
 	 * client request here.
 	 */
-	err = pthread_create(NULL, NULL,
+	err = pthread_create(&tid, NULL,
 	    (funct_t)session->ns_data.dd_module.dm_start_func,
 	    params);
+
 	if (err != 0) {
-		NDMP_LOG(LOG_ERR, "Can't start backup session.");
+		syslog(LOG_ERR, "Can't start V3 backup session.");
 		return (NDMP_ILLEGAL_ARGS_ERR);
 	}
 
+	(void) pthread_detach(tid);
+
 	return (NDMP_NO_ERR);
 }
 
@@ -1501,6 +1486,7 @@
 	ndmp_data_start_recover_reply_v3 reply;
 	ndmpd_module_params_t *params;
 	ndmp_lbr_params_t *nlp;
+	pthread_t tid;
 	int err;
 
 	(void) memset((void*)&reply, 0, sizeof (reply));
@@ -1577,7 +1563,7 @@
 	    &reply) < 0) {
 		NDMP_FREE(nlp->nlp_params);
 		ndmpd_free_nlist_v3(session);
-		NDMP_LOG(LOG_DEBUG,
+		syslog(LOG_ERR,
 		    "Error sending ndmp_data_start_recover_reply");
 		ndmpd_data_error(session, NDMP_DATA_HALT_CONNECT_ERROR);
 		return (NDMP_NO_ERR);
@@ -1594,124 +1580,17 @@
 	 * Cannot wait for the thread to exit as we are replying to the
 	 * client request here.
 	 */
-	err = pthread_create(NULL, NULL,
+	err = pthread_create(&tid, NULL,
 	    (funct_t)session->ns_data.dd_module.dm_start_func,
 	    params);
 
 	if (err != 0) {
-		NDMP_LOG(LOG_ERR, "Can't start recover session.");
+		syslog(LOG_ERR, "Can't start V3 recover session.");
 		return (NDMP_ILLEGAL_ARGS_ERR);
 	}
-	return (NDMP_NO_ERR);
-}
 
-static ndmp_error
-ndmpd_zfs_start_op(ndmpd_session_t *session, ndmp_pval *env_val,
-    ulong_t env_len, ndmp_name_v3 *nlist_val, ulong_t nlist_len,
-    enum ndmp_data_operation op)
-{
-	ndmpd_zfs_args_t *ndmpd_zfs_args = &session->ns_ndmpd_zfs_args;
-	ndmp_data_start_backup_reply_v3 backup_reply;
-	ndmp_data_start_recover_reply_v3 recover_reply;
-	pthread_t tid;
-	void *reply;
-	char str[8];
-	int err;
-
-	if (ndmpd_zfs_init(session) != 0)
-		return (NDMP_UNDEFINED_ERR);
-
-	err = ndmpd_save_env(session, env_val, env_len);
-	if (err != NDMP_NO_ERR) {
-		ndmpd_zfs_fini(ndmpd_zfs_args);
-		return (err);
-	}
-
-	switch (op) {
-	case NDMP_DATA_OP_BACKUP:
-		if (!ndmpd_zfs_backup_parms_valid(ndmpd_zfs_args)) {
-			ndmpd_zfs_fini(ndmpd_zfs_args);
-			return (NDMP_ILLEGAL_ARGS_ERR);
-		}
-
-		if (ndmpd_zfs_pre_backup(ndmpd_zfs_args)) {
-			NDMP_LOG(LOG_ERR, "pre_backup error");
-			return (NDMP_ILLEGAL_ARGS_ERR);
-		}
-
-		session->ns_data.dd_module.dm_start_func =
-		    ndmpd_zfs_backup_starter;
-		(void) strlcpy(str, "backup", 8);
-		break;
-	case NDMP_DATA_OP_RECOVER:
-		err = ndmpd_save_nlist_v3(session, nlist_val, nlist_len);
-		if (err != NDMP_NO_ERR) {
-			ndmpd_zfs_fini(ndmpd_zfs_args);
-			return (NDMP_NO_MEM_ERR);
-		}
-
-		if (!ndmpd_zfs_restore_parms_valid(ndmpd_zfs_args)) {
-			ndmpd_zfs_fini(ndmpd_zfs_args);
-			return (NDMP_ILLEGAL_ARGS_ERR);
-		}
-
-		if (ndmpd_zfs_pre_restore(ndmpd_zfs_args)) {
-			NDMP_LOG(LOG_ERR, "pre_restore error");
-			(void) ndmpd_zfs_post_restore(ndmpd_zfs_args);
-			return (NDMP_ILLEGAL_ARGS_ERR);
-		}
-		session->ns_data.dd_module.dm_start_func =
-		    ndmpd_zfs_restore_starter;
-		(void) strlcpy(str, "recover", 8);
-		break;
-	}
-
-	ndmpd_zfs_params->mp_operation = op;
-	session->ns_data.dd_operation = op;
-	session->ns_data.dd_module.dm_abort_func = ndmpd_zfs_abort;
-	session->ns_data.dd_state = NDMP_DATA_STATE_ACTIVE;
-	session->ns_data.dd_abort = FALSE;
-
-	if (op == NDMP_DATA_OP_BACKUP) {
-		(void) memset((void*)&backup_reply, 0, sizeof (backup_reply));
-		backup_reply.error = NDMP_NO_ERR;
-		reply = &backup_reply;
-	} else {
-		(void) memset((void*)&recover_reply, 0, sizeof (recover_reply));
-		recover_reply.error = NDMP_NO_ERR;
-		reply = &recover_reply;
-	}
-
-	if (ndmp_send_response(session->ns_connection, NDMP_NO_ERR,
-	    reply) < 0) {
-		NDMP_LOG(LOG_DEBUG, "Sending data_start_%s_v3 reply", str);
-		if (op == NDMP_DATA_OP_RECOVER)
-			ndmpd_data_error(session, NDMP_DATA_HALT_CONNECT_ERROR);
-		ndmpd_zfs_fini(ndmpd_zfs_args);
-		return (NDMP_NO_ERR);
-	}
-
-	err = pthread_create(&tid, NULL,
-	    (funct_t)session->ns_data.dd_module.dm_start_func, ndmpd_zfs_args);
-
-	if (err) {
-		NDMP_LOG(LOG_ERR, "Can't start %s session (errno %d)",
-		    str, err);
-		ndmpd_zfs_fini(ndmpd_zfs_args);
-		MOD_DONE(ndmpd_zfs_params, -1);
-		return (NDMP_NO_ERR);
-	}
-
 	(void) pthread_detach(tid);
 
-	if (op == NDMP_DATA_OP_BACKUP)
-		NS_INC(nbk);
-	else
-		NS_INC(nrs);
-
-	ndmpd_zfs_dma_log(ndmpd_zfs_args, NDMP_LOG_NORMAL,
-	    "'zfs' %s starting\n", str);
-
 	return (NDMP_NO_ERR);
 }
 
@@ -1741,7 +1620,7 @@
 	/* Read and discard the data. */
 	n = read(session->ns_data.dd_sock, buf, toread);
 	if (n < 0) {
-		NDMP_LOG(LOG_ERR, "Socket read error: %m.");
+		syslog(LOG_ERR, "Socket read error: %m.");
 		n = -1;
 	}
 
@@ -1773,13 +1652,6 @@
 	tlm_job_stats_t *jstat;
 	longlong_t fsize;
 
-	NDMP_LOG(LOG_DEBUG, "ns_data.dd_xx: [%llu, %llu, %llu, %llu, %llu]",
-	    session->ns_data.dd_bytes_left_to_read,
-	    session->ns_data.dd_read_offset,
-	    session->ns_data.dd_read_length,
-	    session->ns_data.dd_position,
-	    session->ns_data.dd_discard_length);
-
 	count = 0;
 	while (count < length) {
 		len = length - count;
@@ -1810,9 +1682,6 @@
 				jstat = session->ns_ndmp_lbr_params->nlp_jstat;
 				fsize = jstat->js_bytes_in_file;
 
-				NDMP_LOG(LOG_DEBUG, "bytes_left [%llu / %u]",
-				    fsize, len);
-
 				/*
 				 * Fall back to the old way if fsize if too
 				 * small.
@@ -1831,7 +1700,7 @@
 			request.length =
 			    long_long_to_quad(session->ns_data.dd_read_length);
 
-			NDMP_LOG(LOG_DEBUG, "to NOTIFY_DATA_READ [%llu, %llu]",
+			syslog(LOG_DEBUG, "to NOTIFY_DATA_READ [%lu, %lu]",
 			    session->ns_data.dd_read_offset,
 			    session->ns_data.dd_read_length);
 
@@ -1838,7 +1707,7 @@
 			if (ndmp_send_request_lock(session->ns_connection,
 			    NDMP_NOTIFY_DATA_READ, NDMP_NO_ERR,
 			    &request, 0) < 0) {
-				NDMP_LOG(LOG_DEBUG,
+				syslog(LOG_ERR,
 				    "Sending notify_data_read request");
 				return (-1);
 			}
@@ -1867,13 +1736,13 @@
 
 		if ((n = read(session->ns_data.dd_sock, &data[count],
 		    len)) < 0) {
-			NDMP_LOG(LOG_ERR, "Socket read error: %m.");
+			syslog(LOG_ERR, "Socket read error: %m.");
 			return (-1);
 		}
 
 		/* read returns 0 if the connection was closed */
 		if (n == 0) {
-			NDMP_LOG(LOG_DEBUG, "n 0 errno %d",
+			syslog(LOG_ERR, "n 0 errno %d",
 			    errno);
 			return (-1);
 		}
@@ -1902,7 +1771,6 @@
 	ndmp_lbr_params_t *nlp;
 
 	if ((nlp = ndmp_get_nlp(session)) == NULL) {
-		NDMP_LOG(LOG_DEBUG, "nlp == NULL");
 		return;
 	}
 	if (nlp->nlp_jstat != NULL) {
@@ -1910,8 +1778,7 @@
 		    (u_longlong_t)nlp->nlp_jstat->js_bytes_total;
 		tlm_un_ref_job_stats(nlp->nlp_jstat->js_job_name);
 		nlp->nlp_jstat = NULL;
-	} else
-		NDMP_LOG(LOG_DEBUG, "JSTAT == NULL");
+	}
 }
 
 
@@ -1975,7 +1842,7 @@
 ndmpd_data_cleanup(ndmpd_session_t *session)
 {
 	if (session->ns_data.dd_listen_sock != -1) {
-		NDMP_LOG(LOG_DEBUG, "data.listen_sock: %d",
+		syslog(LOG_DEBUG, "data.listen_sock: %d",
 		    session->ns_data.dd_listen_sock);
 		(void) ndmpd_remove_file_handler(session,
 		    session->ns_data.dd_listen_sock);
@@ -1983,7 +1850,7 @@
 		session->ns_data.dd_listen_sock = -1;
 	}
 	if (session->ns_data.dd_sock != -1) {
-		NDMP_LOG(LOG_DEBUG, "data.sock: %d",
+		syslog(LOG_ERR, "data.sock: %d",
 		    session->ns_data.dd_sock);
 
 		/*
@@ -2035,7 +1902,7 @@
 		break;
 	default:
 		rv = "Unknown";
-		NDMP_LOG(LOG_ERR, "Invalid protocol version %d.",
+		syslog(LOG_ERR, "Invalid protocol version %d.",
 		    session->ns_protocol_version);
 	}
 
@@ -2067,16 +1934,17 @@
 	ndmp_data_start_backup_reply reply;
 	ndmpd_module_params_t *params;
 	ndmp_lbr_params_t *nlp;
+	pthread_t tid;
 	int err;
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_IDLE) {
-		NDMP_LOG(LOG_ERR, "Can't start new backup in current state.");
+		syslog(LOG_ERR, "Can't start new backup in current state.");
 		return (NDMP_ILLEGAL_STATE_ERR);
 	}
 	if (strcmp(bu_type, NDMP_DUMP_TYPE) != 0 &&
 	    strcmp(bu_type, NDMP_TAR_TYPE) != 0) {
-		NDMP_LOG(LOG_ERR, "Invalid backup type: %s.", bu_type);
-		NDMP_LOG(LOG_ERR, "Supported backup types are tar and dump.");
+		syslog(LOG_ERR, "Invalid backup type: %s.", bu_type);
+		syslog(LOG_ERR, "Supported backup types are tar and dump.");
 		return (NDMP_ILLEGAL_ARGS_ERR);
 	}
 	if ((err = ndmpd_save_env(session, env_val, env_len)) != NDMP_NO_ERR)
@@ -2137,7 +2005,7 @@
 
 	if ((err = ndmp_backup_extract_params(session,
 	    params)) != NDMP_NO_ERR) {
-		NDMP_LOG(LOG_DEBUG, "err: %d", err);
+		syslog(LOG_ERR, "err: %d", err);
 		NDMP_FREE(nlp->nlp_params);
 		return (err);
 	}
@@ -2144,7 +2012,7 @@
 
 	err = ndmpd_mover_connect(session, NDMP_MOVER_MODE_READ);
 	if (err != NDMP_NO_ERR) {
-		NDMP_LOG(LOG_DEBUG,
+		syslog(LOG_ERR,
 		    "mover connect err: %d", err);
 		NDMP_FREE(nlp->nlp_params);
 		return (err);
@@ -2155,12 +2023,12 @@
 	session->ns_data.dd_operation = NDMP_DATA_OP_BACKUP;
 	session->ns_data.dd_abort = FALSE;
 
-	NDMP_LOG(LOG_DEBUG, "starting backup");
+	syslog(LOG_DEBUG, "starting backup");
 
 	reply.error = NDMP_NO_ERR;
 	if (ndmp_send_response(session->ns_connection, NDMP_NO_ERR,
 	    &reply) < 0) {
-		NDMP_LOG(LOG_DEBUG, "Sending data_start_backup reply");
+		syslog(LOG_DEBUG, "Sending data_start_backup reply");
 		NDMP_FREE(nlp->nlp_params);
 		if (session->ns_data.dd_mover.addr_type == NDMP_ADDR_TCP) {
 			/*
@@ -2184,10 +2052,17 @@
 	 * Cannot wait for the thread to exit as we are replying to the
 	 * client request here.
 	 */
-	(void) pthread_create(NULL, NULL,
+	(void) pthread_create(&tid, NULL,
 	    (funct_t)session->ns_data.dd_module.dm_start_func,
 	    params);
 
+	if (err) {
+		syslog(LOG_ERR, "Can't start V2 backup session.");
+		return (NDMP_ILLEGAL_ARGS_ERR);
+	}
+
+	(void) pthread_detach(tid);
+
 	return (NDMP_NO_ERR);
 }
 
@@ -2216,17 +2091,18 @@
 	ndmp_data_start_recover_reply_v2 reply;
 	ndmpd_module_params_t *params;
 	ndmp_lbr_params_t *nlp;
+	pthread_t tid;
 	int err;
 
 	if (session->ns_data.dd_state != NDMP_DATA_STATE_IDLE) {
-		NDMP_LOG(LOG_ERR, "Can't start new recover in current state.");
+		syslog(LOG_ERR, "Can't start new recover in current state.");
 		return (NDMP_ILLEGAL_STATE_ERR);
 	}
 
 	if (strcmp(bu_type, NDMP_DUMP_TYPE) != 0 &&
 	    strcmp(bu_type, NDMP_TAR_TYPE) != 0) {
-		NDMP_LOG(LOG_ERR, "Invalid backup type: %s.", bu_type);
-		NDMP_LOG(LOG_ERR, "Supported backup types are tar and dump.");
+		syslog(LOG_ERR, "Invalid backup type: %s.", bu_type);
+		syslog(LOG_ERR, "Supported backup types are tar and dump.");
 		return (NDMP_ILLEGAL_ARGS_ERR);
 	}
 
@@ -2295,7 +2171,7 @@
 	reply.error = NDMP_NO_ERR;
 	if (ndmp_send_response(session->ns_connection, NDMP_NO_ERR,
 	    &reply) < 0) {
-		NDMP_LOG(LOG_DEBUG, "Sending data_start_recover reply");
+		syslog(LOG_DEBUG, "Sending data_start_recover reply");
 		NDMP_FREE(nlp->nlp_params);
 		if (session->ns_data.dd_mover.addr_type == NDMP_ADDR_TCP) {
 			/*
@@ -2321,10 +2197,17 @@
 	 * Cannot wait for the thread to exit as we are replying to the
 	 * client request here.
 	 */
-	(void) pthread_create(NULL, NULL,
+	(void) pthread_create(&tid, NULL,
 	    (funct_t)session->ns_data.dd_module.dm_start_func,
 	    params);
 
+	if (err != 0) {
+		syslog(LOG_ERR, "Can't start V2 recover session.");
+		return (NDMP_ILLEGAL_ARGS_ERR);
+	}
+
+	(void) pthread_detach(tid);
+
 	return (NDMP_NO_ERR);
 }
 
