/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* Copyright 2012 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2012, 2014 by Delphix. All rights reserved.
*/
#include <stdio.h>
#include <libzfs.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <libshare.h>
#include "libshare_impl.h"
#include <libintl.h>
extern char *sa_fstype(char *);
extern void set_node_attr(void *, char *, char *);
extern int 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
* usage.
*/
typedef struct get_all_cbdata {
/*
* sa_zfs_init(impl_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
{
return (B_TRUE);
}
return (B_FALSE);
}
/*
* sa_zfs_fini(impl_handle)
*
* cleanup data structures and the libzfs handle used for accessing
* zfs file share info.
*/
void
{
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++) {
}
impl_handle->zfs_list_count = 0;
}
}
}
/*
* get_one_filesystem(zfs_handle_t, data)
*
* an iterator function called while iterating through the ZFS
* root. It accumulates into an array of file system handles that can
* be used to derive info about those file systems.
*
* Note that as this function is called, we close all zhp handles that
* are not going to be places into the cp_handles list. We don't want
* to close the ones we are keeping, but all others would be leaked if
* not closed here.
*/
static int
{
/*
* Interate over any nested datasets.
*/
if (type == ZFS_TYPE_FILESYSTEM &&
return (1);
}
/*
* Skip any datasets whose type does not match.
*/
return (0);
}
else
return (0);
}
if (cbp->cb_handles) {
}
}
return (0);
}
/*
* get_all_filesystems(zfs_handle_t ***fslist, size_t *count)
*
* iterate through all ZFS file systems starting at the root. Returns
* a count and an array of handle pointers. Allocating is only done
* once. The caller does not need to free since it will be done at
* sa_zfs_fini() time.
*/
static void
{
return;
}
get_one_filesystem, &cb);
}
/*
* mountpoint_compare(a, b)
*
* compares the mountpoint on two zfs file systems handles.
* returns values following strcmp() model.
*/
static int
mountpoint_compare(const void *a, const void *b)
{
}
/*
* return legacy mountpoint. Caller provides space for mountpoint and
* dataset.
*/
int
{
return (1);
}
continue;
if (mlen > 0)
mlen);
if (dlen > 0)
dlen);
break;
}
}
return (1);
}
/*
* get_zfs_dataset(impl_handle, path)
*
* get the name of the ZFS dataset the path is equivalent to. The
* requires a dataset to do a zfs_open().
*/
static char *
{
for (i = 0; i < count; i++) {
/* must have a mountpoint */
/* no mountpoint */
continue;
}
/* mountpoint must be a path */
/*
* Search mmttab for mountpoint and get dataset.
*/
if (search_mnttab == B_TRUE &&
sizeof (mountpoint), NULL, 0) == 0) {
break;
}
continue;
}
/* canmount must be set */
canmount[0] = '\0';
continue;
/*
* have a mountable handle but want to skip those marked none
* and legacy
*/
break;
}
}
return (dataset);
}
/*
* get_zfs_property(dataset, property)
*
* Get the file system property specified from the ZFS dataset.
*/
static char *
{
libhandle = libzfs_init();
B_FALSE) == 0) {
}
}
}
return (NULL);
}
/*
* sa_zfs_is_shared(handle, path)
*
* Check to see if the ZFS path provided has the sharenfs option set
* or not.
*/
int
{
int ret = 0;
char *dataset;
libhandle = libzfs_init();
0, B_FALSE) == 0 &&
}
}
}
}
return (ret);
}
/*
* find_or_create_group(handle, groupname, proto, *err)
*
* While walking the ZFS tree, we need to add shares to a defined
* group. If the group doesn't exist, create it first, making sure it
* is marked as a ZFS group.
*
* Note that all ZFS shares are in a subgroup of the top level group
* called "zfs".
*/
static sa_group_t
{
/*
* we check to see if the "zfs" group exists. Since this
* should be the top level group, we don't want the
* parent. This is to make sure the zfs group has been created
* and to created if it hasn't been.
*/
/* make sure this is flagged as a ZFS group */
}
}
}
return (group);
}
/*
* find_or_create_zfs_subgroup(groupname, optstring, *err)
*
* ZFS shares will be in a subgroup of the "zfs" master group. This
* function looks to see if the groupname exists and returns it if it
* does or else creates a new one with the specified name and returns
* that. The "zfs" group will exist before we get here, but we make
* sure just in case.
*
* err must be a valid pointer.
*/
static sa_group_t
{
char *name;
char *options;
/* start with the top-level "zfs" group */
/* have the group so break out of here */
break;
}
}
/*
* Need to create the sub-group since it doesn't exist
*/
*err = SA_NO_MEMORY;
return (NULL);
}
}
optstring = "rw";
proto);
/* If no optionset, add one. */
/*
* Do not forget to update an optionset of
* the parent group so that it contains
* all protocols its subgroups have.
*/
} else {
*err = SA_NO_MEMORY;
}
}
return (group);
}
/*
* zfs_construct_resource(share, name, base, dataset)
*
* Add a resource to the share using name as a template. If name ==
* NULL, then construct a name based on the dataset value.
* name.
*/
static void
{
}
/*
* zfs_inherited(handle, source, sourcestr)
*
* handle case of inherited share{nfs,smb}. Pulled out of sa_get_zfs_shares
* for readability.
*/
static int
{
int doshopt = 0;
/*
* Need to find the "real" parent sub-group. It may not be
* mounted, but it was identified in the "sourcestr"
* variable. The real parent not mounted can occur if
* "canmount=off and sharenfs=on".
*/
/*
* We may need the first share for resource
* prototype. We only care about it if it has a
* resource that sets a prefix value.
*/
/*
* some options may only be on shares. If the opt
* string contains one of those, we put it just on the
* share.
*/
char *options;
proto);
}
}
/*
* We have a share and the protocol requires
* that at least one resource exist (probably
* SMB). We need to make sure that there is at
* least one.
*/
}
}
} else {
err = SA_NO_MEMORY;
}
return (err);
}
/*
* zfs_notinherited(group, share, mountpoint, shareopts, proto, dataset,
* grouperr)
*
* handle case where this is the top of a sub-group in ZFS. Pulled out
* of sa_get_zfs_shares for readability. We need the grouperr from the
* creation of the subgroup to know whether to add the public
* property, etc. to the specific share.
*/
static int
{
return (err);
shareopts = "";
char *options;
if (grouperr == SA_PROP_SHARE_ONLY) {
/*
* Some properties may only be on shares, but
* due to the ZFS sub-groups being artificial,
* we sometimes get this and have to deal with
* it. We do it by attempting to put it on the
* share.
*/
}
}
/* Unmark the share's changed state */
}
/*
* We have a share and the protocol requires that at
* least one resource exist (probably SMB). We need to
* make sure that there is at least one.
*/
}
}
return (err);
}
/*
* zfs_grp_error(err)
*
* Print group create error, but only once. If err is 0 do the
* print else don't.
*/
static void
{
if (err == 0) {
/* only print error once */
"Cannot create ZFS subgroup during initialization:"
}
}
/*
* zfs_process_share(handle, share, mountpoint, proto, source,
* shareopts, sourcestr)
*
* Creates the subgroup, if necessary and adds shares, resources
* and properties.
*/
int
{
if (source & ZPROP_SRC_INHERITED) {
} else {
/*
* There is a problem, but we can't do
* anything about it at this point so we issue
* a warning and move on.
*/
}
/*
* Add share with local opts via zfs_notinherited.
*/
}
return (err);
}
/*
* sa_get_zfs_shares(handle, groupname)
*
* Walk the mnttab for all zfs mounts and determine which are
* the shares.
*
* All shares are in a sub-group that will hold the properties. This
* allows representing the inherited property model.
*
* One area of complication is if "sharenfs" is set at one level of
* the directory tree and "sharesmb" is set at a different level, the
* a sub-group must be formed at the lower level for both
* protocols. That is the nature of the problem in CR 6667349.
*/
int
{
/*
* If we can't access libzfs, don't bother doing anything.
*/
if (zfs_libhandle == NULL)
return (SA_SYSTEM_ERR);
/* Not an error, this could be a legacy condition */
return (SA_OK);
/*
* need to walk the mounted ZFS pools and datasets to
* find shares that are possible.
*/
for (i = 0; i < count; i++) {
char *dataset;
/* If no mountpoint, skip. */
B_FALSE) != 0)
continue;
/*
* zfs_get_name value must not be freed. It is just a
* pointer to a value in the handle.
*/
continue;
/*
* only deal with "mounted" file systems since
* unmounted file systems can't actually be shared.
*/
continue;
ZFS_MAXPROPLEN, B_FALSE) == 0 &&
if (source & ZPROP_SRC_INHERITED)
else
}
ZFS_MAXPROPLEN, B_FALSE) == 0 &&
if (source & ZPROP_SRC_INHERITED)
else
}
/*
* If the mountpoint is already shared, it must be a
* non-ZFS share. We want to remove the share from its
* parent group and reshare it under ZFS.
*/
}
/*
* At this point, we have the information needed to
* determine what to do with the share.
*
* If smb or nfs is set, we have a new sub-group.
* place on an existing sub-group. If both are set,
* the existing sub-group is the closest up the tree.
*/
/*
* Non-inherited is the straightforward
* case. sa_zfs_process_share handles it
* directly. Make sure that if the "other"
* protocol is inherited, that we treat it as
* non-inherited as well.
*/
if (nfs || nfs_inherited) {
0, nfsshareopts,
}
if (smb || smb_inherited) {
0, smbshareopts,
}
} else if (nfs_inherited || smb_inherited) {
char *grpdataset;
/*
* If we only have inherited groups, it is
* important to find the closer of the two if
* the protocols are set at different
* levels. The closest sub-group is the one we
* want to work with.
*/
if (nfs_inherited && smb_inherited) {
else
} else if (nfs_inherited) {
} else if (smb_inherited) {
}
if (nfs_inherited) {
}
if (smb_inherited) {
}
}
}
/*
* Don't need to free the "zlist" variable since it is only a
* pointer to a cached value that will be freed when
* sa_fini() is called.
*/
return (err);
}
/*
* sa_zfs_set_sharenfs(group, path, on)
*
* Update the "sharenfs" property on the path. If on is true, then set
* to the properties on the group or "on" if no properties are
* defined. Set to "off" if on is false.
*/
int
{
char *command;
/* for now, NFS is always available for "zfs" */
if (on) {
}
}
if (impl_handle != NULL)
else
ret = SA_SYSTEM_ERR;
"%s set sharenfs=\"%s\" %s", COMMAND,
if (ret != 0)
ret = SA_SYSTEM_ERR;
}
}
}
return (ret);
}
/*
* add_resources(share, opt)
*
* Add resource properties to those in "opt". Resources are prefixed
* with name=resourcename.
*/
static char *
{
char *propstr;
return (newopt);
char *name;
return (NULL);
}
return (NULL);
}
}
return (opt);
}
/*
* sa_zfs_set_sharesmb(group, path, on)
*
* Update the "sharesmb" property on the path. If on is true, then set
* to the properties on the group or "on" if no properties are
* defined. Set to "off" if on is false.
*/
int
{
char *command;
/* In case SMB not enabled */
return (SA_NOT_SUPPORTED);
if (on) {
char *newopt;
}
}
if (impl_handle != NULL)
else
ret = SA_SYSTEM_ERR;
"echo %s set sharesmb=\"%s\" %s", COMMAND,
if (ret != 0)
ret = SA_SYSTEM_ERR;
}
}
}
return (ret);
}
/*
* sa_zfs_update(group)
*
* call back to ZFS to update the share if necessary.
* Don't do it if it isn't a real change.
*/
int
{
char *command;
char *optstring;
int doupdate = 0;
if (sa_is_share(group))
else
return (SA_NO_MEMORY);
*command = '\0';
char *path;
if (sa_is_share(group)) {
"path");
group);
if (impl_handle != NULL)
else
ret = SA_SYSTEM_ERR;
}
} else {
}
/* update only when there is an optstring found */
doupdate = 0;
group, 1);
doupdate++;
}
if (doupdate) {
ZFS_MAXPROPLEN * 2,
"%s set share%s=%s %s",
} else {
ZFS_MAXPROPLEN * 2,
"%s set share%s=on %s",
dataset);
}
switch (ret) {
default:
case 1:
ret = SA_SYSTEM_ERR;
break;
case 2:
ret = SA_SYNTAX_ERR;
break;
case 0:
break;
}
}
}
}
}
return (ret);
}
/*
* sa_group_is_zfs(group)
*
* Given the group, determine if the zfs attribute is set.
*/
int
{
char *zfs;
int ret = 0;
ret = 1;
}
return (ret);
}
/*
* sa_path_is_zfs(path)
*
* Check to see if the file system path represents is of type "zfs".
*/
int
{
char *fstype;
int ret = 0;
ret = 1;
return (ret);
}
int
{
char *path;
/* Make sure path is valid */
return (0);
} else
return (1);
}
#define SMAX(i, j) \
if ((j) > (i)) { \
(i) = (j); \
}
int
{
char *dataset;
int i, j;
char *pathp;
/*
* First find the dataset name
*/
return (EINVAL);
}
return (EINVAL);
}
/*
* If get_zfs_dataset fails, see if it is a subdirectory
*/
char *p;
}
/*
* Make sure only one leading '/' This condition came
* about when using HAStoragePlus which insisted on
* putting an extra leading '/' in the ZFS path
* name. The problem is fixed in other areas, but this
* will catch any other ways that a double slash might
* get introduced.
*/
pathp++;
/*
* chop off part of path, but if we are at root then
* make sure path is a /
*/
if (pathp == p) {
*(p + 1) = '\0'; /* skip over /, root case */
} else {
*p = '\0';
}
} else {
return (EINVAL);
}
}
libhandle = libzfs_init();
char *resource_name;
SMAX(i, j);
SMAX(i, j);
SMAX(i, j);
SMAX(i, j);
else
if (resource_name)
}
return (err);
}
/*
* sa_get_zfs_handle(handle)
*
* Given an sa_handle_t, return the libzfs_handle_t *. This is only
* used internally by libzfs. Needed in order to avoid including
* libshare_impl.h in libzfs.
*/
{
return (implhandle->zfs_libhandle);
}
/*
* sa_get_zfs_info(libzfs, path, mountpoint, dataset)
*
* Find the ZFS dataset and mountpoint for a given path
*/
int
char *datasetp)
{
int i;
char *dp;
int count;
int ret = 0;
return (0);
for (i = 0; i < count; i++) {
/* must have a mountpoint */
mountpoint, sizeof (mountpoint),
/* no mountpoint */
continue;
}
/* mountpoint must be a path */
/*
* Search mmttab for mountpoint
*/
ZFS_MAXPROPLEN) == 0) {
ret = 1;
break;
}
continue;
}
/* canmount must be set */
canmount[0] = '\0';
continue;
/*
* have a mountable handle but want to skip those marked none
* and legacy
*/
if (mountpointp != NULL)
ret = 1;
}
break;
}
}
return (ret);
}
/*
* This method builds values for "sharesmb" property from the
* nvlist argument. The values are returned in sharesmb_val variable.
*/
static int
{
int err = 0;
return (-1);
}
return (0);
}
/*
* This method builds values for "sharesmb" property from values
* already existing on the share. The properties set via sa_zfs_sprint_new_prop
* method are passed in sharesmb_val. If a existing property is already
* set via sa_zfs_sprint_new_prop method, then they are not appended
* to the sharesmb_val string. The returned sharesmb_val string is a combination
* of new and existing values for 'sharesmb' property.
*/
static int
{
return (-1);
return (0);
return (-1);
*value++ = '\0';
}
}
return (0);
}
/*
* Sets the share properties on a ZFS share. For now, this method sets only
* the "sharesmb" property.
*
* This method includes building a comma seperated name-value string to be
* set on the "sharesmb" property of a ZFS share. This name-value string is
* build in 2 steps:
* - New property values given as name-value pair are set first.
* - Existing optionset properties, which are not part of the new properties
* passed in step 1, are appended to the newly set properties.
*/
int
{
if (nvlist_empty(nvl))
return (0);
return (-1);
return (-1);
return (-1);
}
return (-1);
}
return (-1);
}
return (-1);
}
*lastcomma = '\0';
return (0);
}