--- old/usr/src/lib/libshare/common/libshare_zfs.c	2013-09-10 06:31:58.873079212 +0100
+++ new/usr/src/lib/libshare/common/libshare_zfs.c	2013-09-10 06:31:58.884322398 +0100
@@ -21,9 +21,8 @@
 
 /*
  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
- */
-/*
  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright (c) 2013 RackTop Systems.
  */
 
 #include <stdio.h>
@@ -41,7 +40,7 @@
 extern sa_group_t _sa_create_zfs_group(sa_group_t, char *);
 extern char *sa_fstype(char *);
 extern void set_node_attr(void *, char *, char *);
-extern int sa_is_share(void *);
+extern boolean_t sa_is_share(void *);
 extern void sa_update_sharetab_ts(sa_handle_t);
 
 /*
@@ -58,7 +57,7 @@
 } get_all_cbdata_t;
 
 /*
- * sa_zfs_init(impl_handle)
+ * sa_zfs_init(handle)
  *
  * Initialize an access handle into libzfs.  The handle needs to stay
  * around until sa_zfs_fini() in order to maintain the cache of
@@ -66,45 +65,45 @@
  */
 
 int
-sa_zfs_init(sa_handle_impl_t impl_handle)
+sa_zfs_init(sa_handle_t handle)
 {
-	impl_handle->zfs_libhandle = libzfs_init();
-	if (impl_handle->zfs_libhandle != NULL) {
-		libzfs_print_on_error(impl_handle->zfs_libhandle, B_TRUE);
+	handle->zfs_libhandle = libzfs_init();
+	if (handle->zfs_libhandle != NULL) {
+		libzfs_print_on_error(handle->zfs_libhandle, B_TRUE);
 		return (B_TRUE);
 	}
 	return (B_FALSE);
 }
 
 /*
- * sa_zfs_fini(impl_handle)
+ * sa_zfs_fini(handle)
  *
  * cleanup data structures and the libzfs handle used for accessing
  * zfs file share info.
  */
 
 void
-sa_zfs_fini(sa_handle_impl_t impl_handle)
+sa_zfs_fini(sa_handle_t handle)
 {
-	if (impl_handle->zfs_libhandle != NULL) {
-		if (impl_handle->zfs_list != NULL) {
-			zfs_handle_t **zhp = impl_handle->zfs_list;
+	if (handle->zfs_libhandle != NULL) {
+		if (handle->zfs_list != NULL) {
+			zfs_handle_t **zhp = handle->zfs_list;
 			size_t i;
 
 			/*
 			 * Contents of zfs_list need to be freed so we
 			 * don't lose ZFS handles.
 			 */
-			for (i = 0; i < impl_handle->zfs_list_count; i++) {
+			for (i = 0; i < handle->zfs_list_count; i++) {
 				zfs_close(zhp[i]);
 			}
-			free(impl_handle->zfs_list);
-			impl_handle->zfs_list = NULL;
-			impl_handle->zfs_list_count = 0;
+			free(handle->zfs_list);
+			handle->zfs_list = NULL;
+			handle->zfs_list_count = 0;
 		}
 
-		libzfs_fini(impl_handle->zfs_libhandle);
-		impl_handle->zfs_libhandle = NULL;
+		libzfs_fini(handle->zfs_libhandle);
+		handle->zfs_libhandle = NULL;
 	}
 }
 
@@ -183,23 +182,23 @@
  */
 
 static void
-get_all_filesystems(sa_handle_impl_t impl_handle,
+get_all_filesystems(sa_handle_t handle,
 			zfs_handle_t ***fslist, size_t *count)
 {
 	get_all_cbdata_t cb = { 0 };
 	cb.cb_types = ZFS_TYPE_FILESYSTEM;
 
-	if (impl_handle->zfs_list != NULL) {
-		*fslist = impl_handle->zfs_list;
-		*count = impl_handle->zfs_list_count;
+	if (handle->zfs_list != NULL) {
+		*fslist = handle->zfs_list;
+		*count = handle->zfs_list_count;
 		return;
 	}
 
-	(void) zfs_iter_root(impl_handle->zfs_libhandle,
+	(void) zfs_iter_root(handle->zfs_libhandle,
 	    get_one_filesystem, &cb);
 
-	impl_handle->zfs_list = *fslist = cb.cb_handles;
-	impl_handle->zfs_list_count = *count = cb.cb_used;
+	handle->zfs_list = *fslist = cb.cb_handles;
+	handle->zfs_list_count = *count = cb.cb_used;
 }
 
 /*
@@ -261,7 +260,7 @@
 }
 
 /*
- * get_zfs_dataset(impl_handle, path)
+ * get_zfs_dataset(handle, path)
  *
  * get the name of the ZFS dataset the path is equivalent to.  The
  * dataset name is used for get/set of ZFS properties since libzfs
@@ -269,7 +268,7 @@
  */
 
 static char *
-get_zfs_dataset(sa_handle_impl_t impl_handle, char *path,
+get_zfs_dataset(sa_handle_t handle, char *path,
     boolean_t search_mnttab)
 {
 	size_t i, count = 0;
@@ -278,7 +277,7 @@
 	char mountpoint[ZFS_MAXPROPLEN];
 	char canmount[ZFS_MAXPROPLEN];
 
-	get_all_filesystems(impl_handle, &zlist, &count);
+	get_all_filesystems(handle, &zlist, &count);
 	qsort(zlist, count, sizeof (void *), mountpoint_compare);
 	for (i = 0; i < count; i++) {
 		/* must have a mountpoint */
@@ -335,26 +334,20 @@
  */
 
 static char *
-get_zfs_property(char *dataset, zfs_prop_t property)
+get_zfs_property(sa_handle_t handle, char *dataset, zfs_prop_t property)
 {
-	zfs_handle_t *handle = NULL;
+	zfs_handle_t *z_fs;
 	char shareopts[ZFS_MAXPROPLEN];
-	libzfs_handle_t *libhandle;
 
-	libhandle = libzfs_init();
-	if (libhandle != NULL) {
-		handle = zfs_open(libhandle, dataset, ZFS_TYPE_FILESYSTEM);
-		if (handle != NULL) {
-			if (zfs_prop_get(handle, property, shareopts,
-			    sizeof (shareopts), NULL, NULL, 0,
-			    B_FALSE) == 0) {
-				zfs_close(handle);
-				libzfs_fini(libhandle);
-				return (strdup(shareopts));
-			}
-			zfs_close(handle);
+	z_fs = zfs_open(handle->zfs_libhandle, dataset, ZFS_TYPE_FILESYSTEM);
+	if (z_fs != NULL) {
+		if (zfs_prop_get(z_fs, property, shareopts,
+		    sizeof (shareopts), NULL, NULL, 0,
+		    B_FALSE) == 0) {
+			zfs_close(z_fs);
+			return (strdup(shareopts));
 		}
-		libzfs_fini(libhandle);
+		zfs_close(z_fs);
 	}
 	return (NULL);
 }
@@ -366,31 +359,26 @@
  * or not.
  */
 
-int
-sa_zfs_is_shared(sa_handle_t sahandle, char *path)
+boolean_t
+sa_zfs_is_shared(sa_handle_t handle, char *path)
 {
-	int ret = 0;
+	int ret = B_FALSE;
 	char *dataset;
-	zfs_handle_t *handle = NULL;
+	zfs_handle_t *z_fs = NULL;
 	char shareopts[ZFS_MAXPROPLEN];
-	libzfs_handle_t *libhandle;
 
-	dataset = get_zfs_dataset((sa_handle_t)sahandle, path, B_FALSE);
+	dataset = get_zfs_dataset(handle, path, B_FALSE);
 	if (dataset != NULL) {
-		libhandle = libzfs_init();
-		if (libhandle != NULL) {
-			handle = zfs_open(libhandle, dataset,
-			    ZFS_TYPE_FILESYSTEM);
-			if (handle != NULL) {
-				if (zfs_prop_get(handle, ZFS_PROP_SHARENFS,
-				    shareopts, sizeof (shareopts), NULL, NULL,
-				    0, B_FALSE) == 0 &&
-				    strcmp(shareopts, "off") != 0) {
-					ret = 1; /* it is shared */
-				}
-				zfs_close(handle);
+		z_fs = zfs_open(handle->zfs_libhandle, dataset,
+		    ZFS_TYPE_FILESYSTEM);
+		if (z_fs != NULL) {
+			if (zfs_prop_get(z_fs, ZFS_PROP_SHARENFS,
+			    shareopts, sizeof (shareopts), NULL, NULL,
+			    0, B_FALSE) == 0 &&
+			    strcmp(shareopts, "off") != 0) {
+				ret = B_TRUE; /* it is shared */
 			}
-			libzfs_fini(libhandle);
+			zfs_close(z_fs);
 		}
 		free(dataset);
 	}
@@ -763,7 +751,7 @@
 	/*
 	 * If we can't access libzfs, don't bother doing anything.
 	 */
-	zfs_libhandle = ((sa_handle_impl_t)handle)->zfs_libhandle;
+	zfs_libhandle = handle->zfs_libhandle;
 	if (zfs_libhandle == NULL)
 		return (SA_SYSTEM_ERR);
 
@@ -776,7 +764,7 @@
 	 * need to walk the mounted ZFS pools and datasets to
 	 * find shares that are possible.
 	 */
-	get_all_filesystems((sa_handle_impl_t)handle, &zlist, &count);
+	get_all_filesystems(handle, &zlist, &count);
 	qsort(zlist, count, sizeof (void *), mountpoint_compare);
 
 	for (i = 0; i < count; i++) {
@@ -932,7 +920,7 @@
 		char *opts = NULL;
 		char *dataset = NULL;
 		FILE *pfile;
-		sa_handle_impl_t impl_handle;
+		sa_handle_t handle;
 		/* for now, NFS is always available for "zfs" */
 		if (on) {
 			opts = sa_proto_legacy_format("nfs", group, 1);
@@ -942,10 +930,10 @@
 			}
 		}
 
-		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
-		assert(impl_handle != NULL);
-		if (impl_handle != NULL)
-			dataset = get_zfs_dataset(impl_handle, path, B_FALSE);
+		handle = sa_find_group_handle(group);
+		assert(handle != NULL);
+		if (handle != NULL)
+			dataset = get_zfs_dataset(handle, path, B_FALSE);
 		else
 			ret = SA_SYSTEM_ERR;
 
@@ -1043,7 +1031,7 @@
 		char *opts = NULL;
 		char *dataset = NULL;
 		FILE *pfile;
-		sa_handle_impl_t impl_handle;
+		sa_handle_t handle;
 
 		if (on) {
 			char *newopt;
@@ -1059,10 +1047,10 @@
 			opts = newopt;
 		}
 
-		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
-		assert(impl_handle != NULL);
-		if (impl_handle != NULL)
-			dataset = get_zfs_dataset(impl_handle, path, B_FALSE);
+		handle = sa_find_group_handle(group);
+		assert(handle != NULL);
+		if (handle != NULL)
+			dataset = get_zfs_dataset(handle, path, B_FALSE);
 		else
 			ret = SA_SYSTEM_ERR;
 
@@ -1126,13 +1114,13 @@
 				path = sa_get_share_attr((sa_share_t)group,
 				    "path");
 				if (path != NULL) {
-					sa_handle_impl_t impl_handle;
+					sa_handle_t handle;
 
-					impl_handle = sa_find_group_handle(
+					handle = sa_find_group_handle(
 					    group);
-					if (impl_handle != NULL)
+					if (handle != NULL)
 						dataset = get_zfs_dataset(
-						    impl_handle, path, B_FALSE);
+						    handle, path, B_FALSE);
 					else
 						ret = SA_SYSTEM_ERR;
 
@@ -1144,9 +1132,12 @@
 			/* update only when there is an optstring found */
 			doupdate = 0;
 			if (proto != NULL && dataset != NULL) {
+				sa_handle_t handle;
+
 				optstring = sa_proto_legacy_format(proto,
 				    group, 1);
-				zfsopts = get_zfs_property(dataset,
+				handle = sa_find_group_handle(group);
+				zfsopts = get_zfs_property(handle, dataset,
 				    ZFS_PROP_SHARENFS);
 
 				if (optstring != NULL && zfsopts != NULL) {
@@ -1204,15 +1195,15 @@
  * Given the group, determine if the zfs attribute is set.
  */
 
-int
+boolean_t
 sa_group_is_zfs(sa_group_t group)
 {
 	char *zfs;
-	int ret = 0;
+	int ret = B_FALSE;
 
 	zfs = sa_get_group_attr(group, "zfs");
 	if (zfs != NULL) {
-		ret = 1;
+		ret = B_TRUE;
 		sa_free_attr_string(zfs);
 	}
 	return (ret);
@@ -1224,15 +1215,15 @@
  * Check to see if the file system path represents is of type "zfs".
  */
 
-int
+boolean_t
 sa_path_is_zfs(char *path)
 {
 	char *fstype;
-	int ret = 0;
+	int ret = B_FALSE;
 
 	fstype = sa_fstype(path);
 	if (fstype != NULL && strcmp(fstype, "zfs") == 0)
-		ret = 1;
+		ret = B_TRUE;
 	if (fstype != NULL)
 		sa_free_fstype(fstype);
 	return (ret);
@@ -1264,14 +1255,14 @@
 sa_share_zfs(sa_share_t share, sa_resource_t resource, char *path, share_t *sh,
     void *exportdata, zfs_share_op_t operation)
 {
-	libzfs_handle_t *libhandle;
 	sa_group_t group;
-	sa_handle_t sahandle;
+	sa_handle_t handle;
 	char *dataset;
 	int err = EINVAL;
 	int i, j;
 	char newpath[MAXPATHLEN];
 	char *pathp;
+	char *resource_name;
 
 	/*
 	 * First find the dataset name
@@ -1279,7 +1270,7 @@
 	if ((group = sa_get_parent_group(share)) == NULL)  {
 		return (EINVAL);
 	}
-	if ((sahandle = sa_find_group_handle(group)) == NULL) {
+	if ((handle = sa_find_group_handle(group)) == NULL) {
 		return (EINVAL);
 	}
 
@@ -1288,7 +1279,7 @@
 	 */
 
 	pathp = path;
-	while ((dataset = get_zfs_dataset(sahandle, pathp, B_TRUE)) == NULL) {
+	while ((dataset = get_zfs_dataset(handle, pathp, B_TRUE)) == NULL) {
 		char *p;
 
 		if (pathp == path) {
@@ -1322,42 +1313,36 @@
 		}
 	}
 
-	libhandle = libzfs_init();
-	if (libhandle != NULL) {
-		char *resource_name;
-
-		i = (sh->sh_path ? strlen(sh->sh_path) : 0);
-		sh->sh_size = i;
-
-		j = (sh->sh_res ? strlen(sh->sh_res) : 0);
-		sh->sh_size += j;
-		SMAX(i, j);
-
-		j = (sh->sh_fstype ? strlen(sh->sh_fstype) : 0);
-		sh->sh_size += j;
-		SMAX(i, j);
-
-		j = (sh->sh_opts ? strlen(sh->sh_opts) : 0);
-		sh->sh_size += j;
-		SMAX(i, j);
-
-		j = (sh->sh_descr ? strlen(sh->sh_descr) : 0);
-		sh->sh_size += j;
-		SMAX(i, j);
-
-		resource_name = sa_get_resource_attr(resource, "name");
-
-		err = zfs_deleg_share_nfs(libhandle, dataset, path,
-		    resource_name, exportdata, sh, i, operation);
-		if (err == SA_OK)
-			sa_update_sharetab_ts(sahandle);
-		else
-			err = errno;
-		if (resource_name)
-			sa_free_attr_string(resource_name);
+	i = (sh->sh_path ? strlen(sh->sh_path) : 0);
+	sh->sh_size = i;
+
+	j = (sh->sh_res ? strlen(sh->sh_res) : 0);
+	sh->sh_size += j;
+	SMAX(i, j);
+
+	j = (sh->sh_fstype ? strlen(sh->sh_fstype) : 0);
+	sh->sh_size += j;
+	SMAX(i, j);
+
+	j = (sh->sh_opts ? strlen(sh->sh_opts) : 0);
+	sh->sh_size += j;
+	SMAX(i, j);
+
+	j = (sh->sh_descr ? strlen(sh->sh_descr) : 0);
+	sh->sh_size += j;
+	SMAX(i, j);
+
+	resource_name = sa_get_resource_attr(resource, "name");
+
+	err = zfs_deleg_share_nfs(handle->zfs_libhandle, dataset, path,
+	    resource_name, exportdata, sh, i, operation);
+	if (err == SA_OK)
+		sa_update_sharetab_ts(handle);
+	else
+		err = errno;
+	if (resource_name != NULL)
+		sa_free_attr_string(resource_name);
 
-		libzfs_fini(libhandle);
-	}
 	free(dataset);
 	return (err);
 }
@@ -1373,89 +1358,7 @@
 libzfs_handle_t *
 sa_get_zfs_handle(sa_handle_t handle)
 {
-	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
-
-	return (implhandle->zfs_libhandle);
-}
-
-/*
- * sa_get_zfs_info(libzfs, path, mountpoint, dataset)
- *
- * Find the ZFS dataset and mountpoint for a given path
- */
-int
-sa_zfs_get_info(libzfs_handle_t *libzfs, char *path, char *mountpointp,
-    char *datasetp)
-{
-	get_all_cbdata_t cb = { 0 };
-	int i;
-	char mountpoint[ZFS_MAXPROPLEN];
-	char dataset[ZFS_MAXPROPLEN];
-	char canmount[ZFS_MAXPROPLEN];
-	char *dp;
-	int count;
-	int ret = 0;
-
-	cb.cb_types = ZFS_TYPE_FILESYSTEM;
-
-	if (libzfs == NULL)
-		return (0);
-
-	(void) zfs_iter_root(libzfs, get_one_filesystem, &cb);
-	count = cb.cb_used;
-
-	qsort(cb.cb_handles, count, sizeof (void *), mountpoint_compare);
-	for (i = 0; i < count; i++) {
-		/* must have a mountpoint */
-		if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_MOUNTPOINT,
-		    mountpoint, sizeof (mountpoint),
-		    NULL, NULL, 0, B_FALSE) != 0) {
-			/* no mountpoint */
-			continue;
-		}
-
-		/* mountpoint must be a path */
-		if (strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) == 0 ||
-		    strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
-			/*
-			 * Search mmttab for mountpoint
-			 */
-
-			if (get_legacy_mountpoint(path, dataset,
-			    ZFS_MAXPROPLEN, mountpoint,
-			    ZFS_MAXPROPLEN) == 0) {
-				ret = 1;
-				break;
-			}
-			continue;
-		}
-
-		/* canmount must be set */
-		canmount[0] = '\0';
-		if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_CANMOUNT, canmount,
-		    sizeof (canmount), NULL, NULL, 0, B_FALSE) != 0 ||
-		    strcmp(canmount, "off") == 0)
-			continue;
-
-		/*
-		 * have a mountable handle but want to skip those marked none
-		 * and legacy
-		 */
-		if (strcmp(mountpoint, path) == 0) {
-			dp = (char *)zfs_get_name(cb.cb_handles[i]);
-			if (dp != NULL) {
-				if (datasetp != NULL)
-					(void) strcpy(datasetp, dp);
-				if (mountpointp != NULL)
-					(void) strcpy(mountpointp, mountpoint);
-				ret = 1;
-			}
-			break;
-		}
-
-	}
-
-	return (ret);
+	return (handle->zfs_libhandle);
 }
 
 /*
@@ -1465,7 +1368,7 @@
 static int
 sa_zfs_sprintf_new_prop(nvlist_t *nvl, char *sharesmb_val)
 {
-	char cur_val[MAXPATHLEN];
+	char cur_val[ZFS_MAXPROPLEN];
 	char *name, *val;
 	nvpair_t *cur;
 	int err = 0;
@@ -1477,8 +1380,8 @@
 		if ((err != 0) || (name == NULL) || (val == NULL))
 			return (-1);
 
-		(void) snprintf(cur_val, MAXPATHLEN, "%s=%s,", name, val);
-		(void) strlcat(sharesmb_val, cur_val, MAXPATHLEN);
+		(void) snprintf(cur_val, ZFS_MAXPROPLEN, "%s=%s,", name, val);
+		(void) strlcat(sharesmb_val, cur_val, ZFS_MAXPROPLEN);
 
 		cur = nvlist_next_nvpair(nvl, cur);
 	}
@@ -1497,7 +1400,7 @@
 static int
 sa_zfs_sprintf_existing_prop(zfs_handle_t *handle, char *sharesmb_val)
 {
-	char shareopts[ZFS_MAXPROPLEN], cur_val[MAXPATHLEN];
+	char shareopts[ZFS_MAXPROPLEN], cur_val[ZFS_MAXPROPLEN];
 	char *token, *last, *value;
 
 	if (zfs_prop_get(handle, ZFS_PROP_SHARESMB, shareopts,
@@ -1514,11 +1417,11 @@
 			return (-1);
 		*value++ = '\0';
 
-		(void) snprintf(cur_val, MAXPATHLEN, "%s=", token);
+		(void) snprintf(cur_val, ZFS_MAXPROPLEN, "%s=", token);
 		if (strstr(sharesmb_val, cur_val) == NULL) {
-			(void) strlcat(cur_val, value, MAXPATHLEN);
-			(void) strlcat(cur_val, ",", MAXPATHLEN);
-			(void) strlcat(sharesmb_val, cur_val, MAXPATHLEN);
+			(void) strlcat(cur_val, value, ZFS_MAXPROPLEN);
+			(void) strlcat(cur_val, ",", ZFS_MAXPROPLEN);
+			(void) strlcat(sharesmb_val, cur_val, ZFS_MAXPROPLEN);
 		}
 	}
 
@@ -1540,8 +1443,7 @@
 sa_zfs_setprop(sa_handle_t handle, char *path, nvlist_t *nvl)
 {
 	zfs_handle_t *z_fs;
-	libzfs_handle_t *z_lib;
-	char sharesmb_val[MAXPATHLEN];
+	char sharesmb_val[ZFS_MAXPROPLEN];
 	char *dataset, *lastcomma;
 
 	if (nvlist_empty(nvl))
@@ -1553,30 +1455,22 @@
 	if ((dataset = get_zfs_dataset(handle, path, B_FALSE)) == NULL)
 		return (-1);
 
-	if ((z_lib = libzfs_init()) == NULL) {
-		free(dataset);
-		return (-1);
-	}
-
-	z_fs = zfs_open(z_lib, dataset, ZFS_TYPE_DATASET);
+	z_fs = zfs_open(handle->zfs_libhandle, dataset, ZFS_TYPE_DATASET);
 	if (z_fs == NULL) {
 		free(dataset);
-		libzfs_fini(z_lib);
 		return (-1);
 	}
 
-	bzero(sharesmb_val, MAXPATHLEN);
+	bzero(sharesmb_val, ZFS_MAXPROPLEN);
 	if (sa_zfs_sprintf_new_prop(nvl, sharesmb_val) != 0) {
 		free(dataset);
 		zfs_close(z_fs);
-		libzfs_fini(z_lib);
 		return (-1);
 	}
 
 	if (sa_zfs_sprintf_existing_prop(z_fs, sharesmb_val) != 0) {
 		free(dataset);
 		zfs_close(z_fs);
-		libzfs_fini(z_lib);
 		return (-1);
 	}
 
@@ -1588,7 +1482,6 @@
 	    sharesmb_val);
 	free(dataset);
 	zfs_close(z_fs);
-	libzfs_fini(z_lib);
 
 	return (0);
 }
