--- old/usr/src/cmd/ndmpd/tlm/tlm_lib.c	Mon Aug  5 17:15:20 2019
+++ new/usr/src/cmd/ndmpd/tlm/tlm_lib.c	Mon Aug  5 17:15:20 2019
@@ -36,7 +36,10 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
+/* Copyright 2017 Nexenta Systems, Inc. All rights reserved. */
+
 #include <sys/errno.h>
+#include <syslog.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <time.h>
@@ -318,7 +321,7 @@
 	}
 
 	if (sum == 0) {
-		NDMP_LOG(LOG_DEBUG,
+		syslog(LOG_DEBUG,
 		    "should be %d, is 0", chksum);
 		/* a zero record ==> end of tar file */
 		return (0);
@@ -338,9 +341,10 @@
 	 */
 	sum += ' ' * 8;
 
-	if (sum != chksum)
-		NDMP_LOG(LOG_DEBUG,
+	if (sum != chksum) {
+		syslog(LOG_DEBUG,
 		    "should be %d, is %d", chksum, sum);
+	}
 
 	return ((sum == chksum) ? 1 : -1);
 }
@@ -359,11 +363,11 @@
 	entry = -1;
 	dp = tlm_drive(lib, drv);
 	if (!dp) {
-		NDMP_LOG(LOG_DEBUG, "NULL dp for (%d.%d)", lib, drv);
+		syslog(LOG_DEBUG, "NULL dp for (%d.%d)", lib, drv);
 	} else if (!dp->td_slink) {
-		NDMP_LOG(LOG_DEBUG, "NULL dp->td_slink for (%d.%d)", lib, drv);
+		syslog(LOG_DEBUG, "NULL dp->td_slink for (%d.%d)", lib, drv);
 	} else if (!dp->td_slink->sl_sa) {
-		NDMP_LOG(LOG_DEBUG, "NULL dp->td_slink->sl_sa for (%d.%d)",
+		syslog(LOG_DEBUG, "NULL dp->td_slink->sl_sa for (%d.%d)",
 		    lib, drv);
 	} else {
 		/* search through the SASD table */
@@ -496,16 +500,16 @@
 
 	rv = 0;
 	if (job_stats == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhdir: jstat is NULL");
+		syslog(LOG_DEBUG, "log_fhdir: jstat is NULL");
 	} else if (dir == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhdir: dir is NULL");
+		syslog(LOG_DEBUG, "log_fhdir: dir is NULL");
 	} else if (stp == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhdir: stp is NULL");
+		syslog(LOG_DEBUG, "log_fhdir: stp is NULL");
 	} else if ((cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks)
 	    == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhdir: cbp is NULL");
+		syslog(LOG_DEBUG, "log_fhdir: cbp is NULL");
 	} else if (cbp->fh_log_dir == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhdir: callback is NULL");
+		syslog(LOG_DEBUG, "log_fhdir: callback is NULL");
 	} else
 		rv = (*cbp->fh_log_dir)(cbp, dir, stp, fhp);
 
@@ -524,18 +528,18 @@
 
 	rv = 0;
 	if (job_stats == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhnode: jstat is NULL");
+		syslog(LOG_DEBUG, "log_fhnode: jstat is NULL");
 	} else if (dir == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhnode: dir is NULL");
+		syslog(LOG_DEBUG, "log_fhnode: dir is NULL");
 	} else if (file == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhnode: file is NULL");
+		syslog(LOG_DEBUG, "log_fhnode: file is NULL");
 	} else if (stp == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhnode: stp is NULL");
+		syslog(LOG_DEBUG, "log_fhnode: stp is NULL");
 	} else if ((cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks)
 	    == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhnode: cbp is NULL");
+		syslog(LOG_DEBUG, "log_fhnode: cbp is NULL");
 	} else if (cbp->fh_log_node == NULL) {
-		NDMP_LOG(LOG_DEBUG, "log_fhnode: callback is NULL");
+		syslog(LOG_DEBUG, "log_fhnode: callback is NULL");
 	} else
 		rv = (*cbp->fh_log_node)(cbp, dir, file, stp, off);
 
@@ -554,16 +558,16 @@
 
 	rv = 0;
 	if (!job_stats) {
-		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: jstat is NULL");
+		syslog(LOG_DEBUG, "log_fhpath_name: jstat is NULL");
 	} else if (!pathname) {
-		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: pathname is NULL");
+		syslog(LOG_DEBUG, "log_fhpath_name: pathname is NULL");
 	} else if (!stp) {
-		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: stp is NULL");
+		syslog(LOG_DEBUG, "log_fhpath_name: stp is NULL");
 	} else if ((cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks)
 	    == 0) {
-		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: cbp is NULL");
+		syslog(LOG_DEBUG, "log_fhpath_name: cbp is NULL");
 	} else if (!cbp->fh_logpname) {
-		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: callback is NULL");
+		syslog(LOG_DEBUG, "log_fhpath_name: callback is NULL");
 	} else
 		rv = (*cbp->fh_logpname)(cbp, pathname, stp, off);
 
@@ -579,15 +583,13 @@
 {
 	lbr_fhlog_call_backs_t *cbp; /* callbacks pointer */
 
-	NDMP_LOG(LOG_DEBUG, "name: \"%s\", pos: %d", name, pos);
-
 	if (job_stats == NULL) {
-		NDMP_LOG(LOG_DEBUG, "entry_restored: jstat is NULL");
+		syslog(LOG_DEBUG, "entry_restored: jstat is NULL");
 		return (0);
 	}
 	cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks;
 	if (cbp == NULL) {
-		NDMP_LOG(LOG_DEBUG, "entry_restored is NULL");
+		syslog(LOG_DEBUG, "entry_restored is NULL");
 		return (0);
 	}
 	return (*cbp->fh_logpname)(cbp, name, 0, (longlong_t)pos);
@@ -646,31 +648,29 @@
  * This is necessary to check for checkpoints not being stale.
  */
 int
-tlm_get_chkpnt_time(char *path, int auto_checkpoint, time_t *tp, char *jname)
+tlm_get_chkpnt_time(char *path, time_t *tp)
 {
-	char volname[TLM_VOLNAME_MAX_LENGTH];
-	char chk_name[PATH_MAX];
-	char *cp_nm;
+	zfs_handle_t *zhp;
 
-	NDMP_LOG(LOG_DEBUG, "path [%s] auto_checkpoint: %d",
-	    path, auto_checkpoint);
-
-	if (path == NULL || *path == '\0' || tp == NULL)
+	if (path == NULL || *path == '\0' || tp == NULL) {
+		syslog(LOG_ERR, "tlm_get_chkpnt_time: bad params");
 		return (-1);
+	}
 
-	if (get_zfsvolname(volname, TLM_VOLNAME_MAX_LENGTH,
-	    path) == -1)
+	(void) mutex_lock(&zlib_mtx);
+	if ((zhp = zfs_open(zlibh, path, ZFS_TYPE_DATASET)) == NULL) {
+		syslog(LOG_DEBUG, "tlm_get_chkpnt_time: open %s failed",
+		    path);
+		(void) mutex_unlock(&zlib_mtx);
 		return (-1);
-
-	if (auto_checkpoint) {
-		NDMP_LOG(LOG_DEBUG, "volname [%s]", volname);
-		(void) snprintf(chk_name, PATH_MAX, "%s", jname);
-		return (chkpnt_creationtime_bypattern(volname, chk_name, tp));
 	}
-	cp_nm = strchr(volname, '@');
-	NDMP_LOG(LOG_DEBUG, "volname [%s] cp_nm [%s]", volname, cp_nm);
 
-	return (chkpnt_creationtime_bypattern(volname, cp_nm, tp));
+	*tp = zfs_prop_get_int(zhp, ZFS_PROP_CREATION);
+
+	zfs_close(zhp);
+	(void) mutex_unlock(&zlib_mtx);
+
+	return (0);
 }
 
 /*
@@ -701,10 +701,10 @@
 	if (!lpp)
 		return;
 
-	NDMP_LOG(LOG_DEBUG, "%s:", title);
+	syslog(LOG_DEBUG, "%s:", title);
 
 	for (i = 0; *lpp; lpp++, i++)
-		NDMP_LOG(LOG_DEBUG, "%d: [%s]", i, *lpp);
+		syslog(LOG_DEBUG, "%d: [%s]", i, *lpp);
 }
 
 /*
@@ -723,9 +723,10 @@
 tlm_build_snapshot_name(char *name, char *sname, char *jname)
 {
 	zfs_handle_t *zhp;
-	char *rest;
-	char volname[ZFS_MAX_DATASET_NAME_LEN];
-	char mountpoint[PATH_MAX];
+	char volname[ZFS_MAX_DATASET_NAME_LEN] = {'\0'};
+	char mountpoint[PATH_MAX] = {'\0'};
+	char zpoolname[ZFS_MAX_DATASET_NAME_LEN] = {'\0'};
+	char *slash, *rest;
 
 	if (get_zfsvolname(volname, ZFS_MAX_DATASET_NAME_LEN, name) == -1)
 		goto notzfs;
@@ -747,9 +748,15 @@
 	zfs_close(zhp);
 	(void) mutex_unlock(&zlib_mtx);
 
+	(void) strlcpy(zpoolname, volname, ZFS_MAX_DATASET_NAME_LEN);
+	slash = strchr(zpoolname, '/');
+	if (slash != 0) {
+		*slash = '\0';
+	}
+
 	rest = name + strlen(mountpoint);
-	(void) snprintf(sname, TLM_MAX_PATH_NAME, "%s/%s/%s%s", mountpoint,
-	    TLM_SNAPSHOT_DIR, jname, rest);
+	(void) snprintf(sname,
+	    TLM_MAX_PATH_NAME, "/%s/%s%s", zpoolname, jname, rest);
 
 	return (sname);
 
@@ -826,7 +833,7 @@
 		return (FALSE);
 
 	if (!tlm_cat_path(full_name, dir, name)) {
-		NDMP_LOG(LOG_DEBUG, "Path too long [%s][%s]",
+		syslog(LOG_DEBUG, "Path too long [%s][%s]",
 		    dir, name);
 		return (FALSE);
 	}
@@ -880,7 +887,7 @@
 
 	if ((lp = tlm_library(l))) {
 		lp->tl_capability_barcodes = TRUE;
-		NDMP_LOG(LOG_DEBUG,
+		syslog(LOG_DEBUG,
 		    "Barcode capability on library %d enabled.", l);
 	}
 }
@@ -1025,7 +1032,7 @@
 	/* Scan for the changer */
 	dirp = opendir(SCSI_CHANGER_DIR);
 	if (dirp == NULL) {
-		NDMP_LOG(LOG_DEBUG,
+		syslog(LOG_DEBUG,
 		    "Changer directory read error %s", SCSI_CHANGER_DIR);
 	} else {
 		while ((dp = readdir(dirp)) != NULL) {
@@ -1050,7 +1057,7 @@
 	/* Scan for tape drives */
 	dirp = opendir(SCSI_TAPE_DIR);
 	if (dirp == NULL) {
-		NDMP_LOG(LOG_DEBUG,
+		syslog(LOG_DEBUG,
 		    "Tape directory read error %s", SCSI_TAPE_DIR);
 	} else {
 		drive_type = ndmpd_get_prop(NDMP_DRIVE_TYPE);
@@ -1057,7 +1064,7 @@
 
 		if ((strcasecmp(drive_type, "sysv") != 0) &&
 		    (strcasecmp(drive_type, "bsd") != 0)) {
-			NDMP_LOG(LOG_ERR, "Invalid ndmpd/drive-type value. "
+			syslog(LOG_ERR, "Invalid ndmpd/drive-type value. "
 			    "Valid values are 'sysv' and 'bsd'.");
 			return (-1);
 		}
@@ -1165,7 +1172,7 @@
 {
 	int retries = 0;
 
-	NDMP_LOG(LOG_DEBUG, "tlm_ioctl fd %d cmd %d", fd, cmd);
+	syslog(LOG_DEBUG, "tlm_ioctl fd %d cmd %d", fd, cmd);
 	if (fd == 0 || data == NULL)
 		return (EINVAL);
 
@@ -1174,9 +1181,9 @@
 			break;
 
 		if (errno != EIO && errno != 0) {
-			NDMP_LOG(LOG_ERR,
+			syslog(LOG_ERR,
 			    "Failed to send command to device: %m.");
-			NDMP_LOG(LOG_DEBUG, "IOCTL error %d", errno);
+			syslog(LOG_DEBUG, "IOCTL error %d", errno);
 			return (errno);
 		}
 		(void) sleep(1);
@@ -1215,7 +1222,7 @@
 
 	(void) mutex_lock(&zlib_mtx);
 	if ((zhp = zfs_open(zlibh, chk_name, ZFS_TYPE_DATASET)) == NULL) {
-		NDMP_LOG(LOG_DEBUG, "chkpnt_creationtime: open %s failed",
+		syslog(LOG_DEBUG, "chkpnt_creationtime: open %s failed",
 		    chk_name);
 		(void) mutex_unlock(&zlib_mtx);
 		return (-1);
@@ -1238,14 +1245,17 @@
 	struct stat64 stbuf;
 	struct extmnttab ent;
 	FILE *mntfp;
-	int rv;
+	int rv = 0;
 
 	*volname = '\0';
 	if (stat64(path, &stbuf) != 0) {
+		syslog(LOG_DEBUG, "stat64 failed open %s - %s",
+		    volname, path);
 		return (-1);
 	}
 
 	if ((mntfp = fopen(MNTTAB, "r")) == NULL) {
+		syslog(LOG_DEBUG, "failed open mnttab");
 		return (-1);
 	}
 	while ((rv = getextmntent(mntfp, &ent, 0)) == 0) {
@@ -1255,11 +1265,11 @@
 	}
 
 	if (rv == 0 &&
-	    strcmp(ent.mnt_fstype, MNTTYPE_ZFS) == 0)
+	    strcmp(ent.mnt_fstype, MNTTYPE_ZFS) == 0) {
 		(void) strlcpy(volname, ent.mnt_special, len);
-	else
+	} else {
 		rv = -1;
-
+	}
 	(void) fclose(mntfp);
 	return (rv);
 }
