Print this page
4095 minor cleanup up libshare
@@ -19,13 +19,12 @@
* CDDL HEADER END
*/
/*
* 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>
#include <libzfs.h>
#include <string.h>
@@ -39,11 +38,11 @@
extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
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);
/*
* File system specific code for ZFS. The original code was stolen
* from the "zfs" command and modified to better suit this library's
@@ -56,57 +55,57 @@
size_t cb_used;
uint_t cb_types;
} 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
* mounts.
*/
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;
}
}
/*
* get_one_filesystem(zfs_handle_t, data)
@@ -181,27 +180,27 @@
* once. The caller does not need to free since it will be done at
* sa_zfs_fini() time.
*/
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;
}
/*
* mountpoint_compare(a, b)
*
@@ -259,28 +258,28 @@
(void) fclose(fp);
return (1);
}
/*
- * 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
* requires a dataset to do a zfs_open().
*/
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;
char *dataset = NULL;
zfs_handle_t **zlist;
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 */
if (zfs_prop_get(zlist[i], ZFS_PROP_MOUNTPOINT, mountpoint,
sizeof (mountpoint), NULL, NULL, 0, B_FALSE) != 0) {
@@ -333,30 +332,24 @@
*
* Get the file system property specified from the ZFS dataset.
*/
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,
+ 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(handle);
- libzfs_fini(libhandle);
+ zfs_close(z_fs);
return (strdup(shareopts));
}
- zfs_close(handle);
- }
- libzfs_fini(libhandle);
+ zfs_close(z_fs);
}
return (NULL);
}
/*
@@ -364,35 +357,30 @@
*
* Check to see if the ZFS path provided has the sharenfs option set
* 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,
+ z_fs = zfs_open(handle->zfs_libhandle, dataset,
ZFS_TYPE_FILESYSTEM);
- if (handle != NULL) {
- if (zfs_prop_get(handle, ZFS_PROP_SHARENFS,
+ 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 = 1; /* it is shared */
+ ret = B_TRUE; /* it is shared */
}
- zfs_close(handle);
- }
- libzfs_fini(libhandle);
+ zfs_close(z_fs);
}
free(dataset);
}
return (ret);
}
@@ -761,11 +749,11 @@
int err = SA_OK;
/*
* 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);
zfsgroup = find_or_create_group(handle, groupname, NULL, &err);
/* Not an error, this could be a legacy condition */
@@ -774,11 +762,11 @@
/*
* 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++) {
char *dataset;
@@ -930,24 +918,24 @@
command = malloc(ZFS_MAXPROPLEN * 2);
if (command != NULL) {
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);
if (opts != NULL && strlen(opts) == 0) {
free(opts);
opts = strdup("on");
}
}
- 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;
if (dataset != NULL) {
(void) snprintf(command, ZFS_MAXPROPLEN * 2,
@@ -1041,11 +1029,11 @@
command = malloc(ZFS_MAXPROPLEN * 2);
if (command != NULL) {
char *opts = NULL;
char *dataset = NULL;
FILE *pfile;
- sa_handle_impl_t impl_handle;
+ sa_handle_t handle;
if (on) {
char *newopt;
share = sa_get_share(group, NULL);
@@ -1057,14 +1045,14 @@
newopt = add_resources(opts, share);
free(opts);
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;
if (dataset != NULL) {
(void) snprintf(command, ZFS_MAXPROPLEN * 2,
@@ -1124,17 +1112,17 @@
if (sa_is_share(group)) {
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;
sa_free_attr_string(path);
}
@@ -1142,13 +1130,16 @@
dataset = sa_get_group_attr(group, "name");
}
/* 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) {
if (strcmp(optstring, zfsopts) != 0)
doupdate++;
@@ -1202,19 +1193,19 @@
* sa_group_is_zfs(group)
*
* 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);
}
@@ -1222,19 +1213,19 @@
* sa_path_is_zfs(path)
*
* 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);
}
@@ -1262,35 +1253,35 @@
int
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
*/
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);
}
/*
* If get_zfs_dataset fails, see if it is a subdirectory
*/
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) {
(void) strlcpy(newpath, path, sizeof (newpath));
pathp = newpath;
@@ -1320,14 +1311,10 @@
} else {
return (EINVAL);
}
}
- 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;
@@ -1345,21 +1332,19 @@
sh->sh_size += j;
SMAX(i, j);
resource_name = sa_get_resource_attr(resource, "name");
- err = zfs_deleg_share_nfs(libhandle, dataset, path,
+ err = zfs_deleg_share_nfs(handle->zfs_libhandle, dataset, path,
resource_name, exportdata, sh, i, operation);
if (err == SA_OK)
- sa_update_sharetab_ts(sahandle);
+ sa_update_sharetab_ts(handle);
else
err = errno;
- if (resource_name)
+ if (resource_name != NULL)
sa_free_attr_string(resource_name);
- libzfs_fini(libhandle);
- }
free(dataset);
return (err);
}
/*
@@ -1371,103 +1356,21 @@
*/
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);
}
/*
* This method builds values for "sharesmb" property from the
* nvlist argument. The values are returned in sharesmb_val variable.
*/
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;
cur = nvlist_next_nvpair(nvl, NULL);
@@ -1475,12 +1378,12 @@
name = nvpair_name(cur);
err = nvpair_value_string(cur, &val);
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);
}
return (0);
@@ -1495,11 +1398,11 @@
* of new and existing values for 'sharesmb' property.
*/
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,
sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0)
return (-1);
@@ -1512,15 +1415,15 @@
value = strchr(token, '=');
if (value == NULL)
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);
}
}
return (0);
}
@@ -1538,12 +1441,11 @@
*/
int
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))
return (0);
@@ -1551,34 +1453,26 @@
return (-1);
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);
}
lastcomma = strrchr(sharesmb_val, ',');
if ((lastcomma != NULL) && (lastcomma[1] == '\0'))
@@ -1586,9 +1480,8 @@
(void) zfs_prop_set(z_fs, zfs_prop_to_name(ZFS_PROP_SHARESMB),
sharesmb_val);
free(dataset);
zfs_close(z_fs);
- libzfs_fini(z_lib);
return (0);
}