libshare_zfs.c revision 57b448de658d89a2c88a001d58073c46ed2180f3
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <libzfs.h>
#include <string.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 *);
/*
* 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
{
/*
* contents of zfs_list were already freed by
* the call to libzfs_fini().
*/
impl_handle->zfs_list_count = 0;
}
}
}
/*
* get_one_filesystem(zfs_handle_t, data)
*
* an interator 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.
*/
static int
{
/*
* Skip any zvols
*/
return (0);
}
else
return (0);
}
if (cbp->cb_handles) {
}
}
}
/*
* 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
{
get_all_cbdata_t cb = { 0 };
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)
{
char mounta[MAXPATHLEN];
char mountb[MAXPATHLEN];
}
/*
* 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 *
{
char mountpoint[ZFS_MAXPROPLEN];
char canmount[ZFS_MAXPROPLEN];
for (i = 0; i < count; i++) {
/* must have a mountpoint */
/* no mountpoint */
continue;
}
/* mountpoint must be a path */
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 *
{
char shareopts[ZFS_MAXPROPLEN];
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;
char shareopts[ZFS_MAXPROPLEN];
libhandle = libzfs_init();
0, B_FALSE) == 0 &&
}
}
}
}
return (ret);
}
/*
* find_or_create_group(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 */
}
} else {
char **protolist;
int numprotos, i;
for (i = 0; i < numprotos; i++) {
protolist[i]);
}
}
}
}
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
*/
optstring = "rw";
options, "nfs");
} else {
*err = SA_NO_MEMORY;
}
}
}
}
return (group);
}
/*
* zfs_inherited(handle, source, sourcestr)
*
* handle case of inherited sharenfs. Pulled out of sa_get_zfs_shares
* for readability.
*/
static int
char *shareopts, char *mountpoint)
{
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".
*/
&doshopt);
&err);
/*
* some options may only be on shares. If the opt
* string contains one of those, we put it just on the
* share.
*/
char *options;
"nfs");
}
}
} else {
err = SA_NO_MEMORY;
}
return (err);
}
/*
* zfs_notinherited()
*
* handle case where this is the top of a sub-group in ZFS. Pulled out
* of sa_get_zfs_shares for readability.
*/
static int
{
char *options;
"nfs");
}
if (err == SA_PROP_SHARE_ONLY) {
/*
* Same as above, 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.
*/
options, "nfs");
}
/* unmark the share's changed state */
}
}
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
zfs_grp_error(int err)
{
if (err == 0) {
/* only print error once */
"Cannot create ZFS subgroup during initialization:"
}
}
/*
* 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.
*/
int
{
int legacy = 0;
int err;
char shareopts[ZFS_MAXPROPLEN];
char sourcestr[ZFS_MAXPROPLEN];
char mountpoint[ZFS_MAXPROPLEN];
/*
* If we can't access libzfs, don't bother doing anything.
*/
if (zfs_libhandle == NULL)
return (SA_SYSTEM_ERR);
/*
* need to walk the mounted ZFS pools and datasets to
* find shares that are possible.
*/
for (i = 0; i < count; i++) {
char *dataset;
B_FALSE) != 0) {
/* no mountpoint */
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 &&
/* it is shared so add to list */
/*
* A zfs file system had been shared
* through traditional methods
* group. Now it has been added to a
* ZFS group via the zfs
* command. Remove from previous
* config and setup with current
* options.
*/
}
if (source & ZFS_SRC_INHERITED) {
} else {
static int err = 0;
/*
* there is a problem,
* but we can't do
* anything about it
* at this point so we
* issue a warning an
* move on.
*/
err = 1;
continue;
}
"true");
}
}
}
}
}
/*
* 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 (legacy);
}
/*
* 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
{
int ret = SA_NOT_IMPLEMENTED;
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);
}
/*
* 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)
impl_handle, path);
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 sharenfs=%s %s"
} else {
ZFS_MAXPROPLEN * 2,
"%s set sharenfs=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
sa_path_is_zfs(char *path)
{
char *fstype;
int ret = 0;
ret = 1;
return (ret);
}