From d569f3109dea2239dd935092accb97ce368aacc3 Mon Sep 17 00:00:00 2001
From: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Date: Wed, 20 Feb 2019 19:06:02 -0700
Subject: [PATCH 19/33] NEX-19178 Changing the NFS export path makes the SMB
 share offline Reviewed by: Evan Layton <evan.layton@nexenta.com> Reviewed by:
 Yuri Pankov <yuri.pankov@nexenta.com> Reviewed by: Matt Barden
 <matt.barden@nexenta.com>

---
 usr/src/lib/libshare/common/libshare_zfs.c | 25 +++++++++++++++++-
 usr/src/lib/libshare/common/mapfile-vers   |  4 +--
 usr/src/lib/libzfs/common/libzfs_mount.c   | 30 +++++++++-------------
 3 files changed, 38 insertions(+), 21 deletions(-)

diff --git a/usr/src/lib/libshare/common/libshare_zfs.c b/usr/src/lib/libshare/common/libshare_zfs.c
index 69d1495954..0db6a56cd2 100644
--- a/usr/src/lib/libshare/common/libshare_zfs.c
+++ b/usr/src/lib/libshare/common/libshare_zfs.c
@@ -26,7 +26,7 @@
 /*
  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  * Copyright 2017 RackTop Systems.
- * Copyright 2018 Nexenta Systems, Inc.
+ * Copyright 2019 Nexenta Systems, Inc.
  */
 
 #include <stdio.h>
@@ -987,6 +987,29 @@ sa_get_zfs_shares(sa_handle_t handle, char *groupname)
 	return (err);
 }
 
+/*
+ * Initializes shares for only the dataset specified fs_handle.
+ * This is used as a performance optimization relative to sa_get_zfs_shares.
+ */
+int
+sa_get_zfs_share(sa_handle_t handle, char *groupname, zfs_handle_t *fs_handle)
+{
+	sa_group_t zfsgroup;
+	libzfs_handle_t *zfs_libhandle;
+	int err;
+
+	if ((err = prep_zfs_handle_and_group(handle, groupname, &zfs_libhandle,
+	    &zfsgroup, &err)) != SA_OK) {
+		return (err);
+	}
+	/* Not an error, this could be a legacy condition */
+	if (zfsgroup == NULL)
+		return (SA_OK);
+
+	err = sa_get_zfs_share_common(handle, fs_handle, NULL, zfsgroup);
+	return (err);
+}
+
 /*
  * Initializes only the handles specified in the sharearg for use with libshare.
  * This is used as a performance optimization relative to sa_get_zfs_shares.
diff --git a/usr/src/lib/libshare/common/mapfile-vers b/usr/src/lib/libshare/common/mapfile-vers
index c661589eda..48a62e0bfd 100644
--- a/usr/src/lib/libshare/common/mapfile-vers
+++ b/usr/src/lib/libshare/common/mapfile-vers
@@ -21,7 +21,7 @@
 #
 # Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
 # Copyright (c) 2016 by Delphix. All rights reserved.
-# Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
+# Copyright 2019 Nexenta Systems, Inc.  All rights reserved.
 #
 
 #
@@ -152,7 +152,7 @@ SYMBOL_VERSION SUNWprivate {
 	sa_proto_delete_section;
 	sa_needs_refresh;
 	sa_get_zfs_handle;
-	sa_zfs_process_share;
+	sa_get_zfs_share;
 	sa_update_sharetab_ts;
 	sa_zfs_setprop;
     local:
diff --git a/usr/src/lib/libzfs/common/libzfs_mount.c b/usr/src/lib/libzfs/common/libzfs_mount.c
index db3dac7ab9..08257d73f5 100644
--- a/usr/src/lib/libzfs/common/libzfs_mount.c
+++ b/usr/src/lib/libzfs/common/libzfs_mount.c
@@ -24,7 +24,7 @@
  */
 
 /*
- * Copyright 2018 Nexenta Systems, Inc.
+ * Copyright 2019 Nexenta Systems, Inc.
  * Copyright (c) 2014, 2016 by Delphix. All rights reserved.
  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
  * Copyright 2017 Joyent, Inc.
@@ -624,8 +624,7 @@ static char *(*_sa_errorstr)(int);
 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
-static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
-    char *, char *, zprop_source_t, char *, char *, char *);
+static int (* _sa_get_zfs_share)(sa_handle_t, char *, zfs_handle_t *);
 static void (*_sa_update_sharetab_ts)(sa_handle_t);
 
 /*
@@ -673,9 +672,8 @@ _zfs_init_libshare(void)
 		    dlsym(libshare, "sa_needs_refresh");
 		_sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
 		    dlsym(libshare, "sa_get_zfs_handle");
-		_sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
-		    sa_share_t, char *, char *, zprop_source_t, char *,
-		    char *, char *))dlsym(libshare, "sa_zfs_process_share");
+		_sa_get_zfs_share = (int (*)(sa_handle_t, char *,
+		    zfs_handle_t *)) dlsym(libshare, "sa_get_zfs_share");
 		_sa_update_sharetab_ts = (void (*)(sa_handle_t))
 		    dlsym(libshare, "sa_update_sharetab_ts");
 		if (_sa_init == NULL || _sa_init_arg == NULL ||
@@ -683,7 +681,7 @@ _zfs_init_libshare(void)
 		    _sa_enable_share == NULL || _sa_disable_share == NULL ||
 		    _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
 		    _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
-		    _sa_zfs_process_share == NULL || _sa_service == NULL ||
+		    _sa_get_zfs_share == NULL || _sa_service == NULL ||
 		    _sa_update_sharetab_ts == NULL) {
 			_sa_init = NULL;
 			_sa_init_arg = NULL;
@@ -696,7 +694,7 @@ _zfs_init_libshare(void)
 			(void) dlclose(libshare);
 			_sa_needs_refresh = NULL;
 			_sa_get_zfs_handle = NULL;
-			_sa_zfs_process_share = NULL;
+			_sa_get_zfs_share = NULL;
 			_sa_update_sharetab_ts = NULL;
 		}
 	}
@@ -887,17 +885,13 @@ zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
 		if (share == NULL) {
 			/*
 			 * This may be a new file system that was just
-			 * created so isn't in the internal cache
-			 * (second time through). Rather than
-			 * reloading the entire configuration, we can
-			 * assume ZFS has done the checking and it is
-			 * safe to add this to the internal
-			 * configuration.
+			 * created so isn't in the internal cache.
+			 * Rather than reloading the entire configuration,
+			 * we can add just this one share to the cache.
 			 */
-			if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
-			    NULL, NULL, mountpoint,
-			    proto_table[*curr_proto].p_name, sourcetype,
-			    shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
+			if ((_sa_get_zfs_share == NULL) ||
+			    (_sa_get_zfs_share(hdl->libzfs_sharehdl, "zfs", zhp)
+			    != SA_OK)) {
 				(void) zfs_error_fmt(hdl,
 				    proto_table[*curr_proto].p_share_err,
 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
-- 
2.21.0

