Remove dependency on sharetab file and refactor sharing logic

== Motivation and Context

The current implementation of 'sharenfs' and 'sharesmb' relies on
the use of the sharetab file. The use of this file is os-specific
and not required by linux or freebsd. Currently the code must
maintain updates to this file which adds complexity and presents
a significant performance impact when sharing many datasets. In
addition, concurrently running 'zfs sharenfs' command results in
missing entries in the sharetab file leading to unexpected failures.

== Description

This change removes the sharetab logic from the linux and freebsd
implementation of 'sharenfs' and 'sharesmb'. It still preserves an
os-specific library which contains the logic required for sharing
NFS or SMB. The following entry points exist in the vastly simplified
libshare library:

- sa_enable_share -- shares a dataset but may not commit the change
- sa_disable_share -- unshares a dataset but may not commit the change
- sa_is_shared -- determine if a dataset is shared
- sa_commit_share -- notify NFS/SMB subsystem to commit the shares
- sa_validate_shareopts -- determine if sharing options are valid

The sa_commit_share entry point is provided as a performance enhancement
and is not required. The sa_enable_share/sa_disable_share may commit
the share as part of the implementation. Libshare provides a framework
for both NFS and SMB but some operating systems may not fully support
these protocols or all features of the protocol.

NFS Operation:
For linux, libshare updates /etc/exports.d/zfs.exports to add
and remove shares and then commits the changes by invoking
'exportfs -r'. This file, is automatically read by the kernel NFS
implementation which makes for better integration with the NFS systemd
service. For FreeBSD, libshare updates /etc/zfs/exports to add and
remove shares and then commits the changes by sending a SIGHUP to
mountd.

SMB Operation:
For linux, libshare adds and removes files in /var/lib/samba/usershares
by calling the 'net' command directly. There is no need to commit the
changes. FreeBSD does not support SMB.

== Performance Results

To test sharing performance we created a pool with an increasing number
of datasets and invoked various zfs actions that would enable and
disable sharing. The performance testing was limited to NFS sharing.
The following tests were performed on an 8 vCPU system with 128GB and
a pool comprised of 4 50GB SSDs:

Scale testing:
- Share all filesystems in parallel -- zfs sharenfs=on <dataset> &
- Unshare all filesystems in parallel -- zfs sharenfs=off <dataset> &

Functional testing:
- share each filesystem serially -- zfs share -a
- unshare each filesystem serially -- zfs unshare -a
- reset sharenfs property and unshare -- zfs inherit -r sharenfs <pool>

For 'zfs sharenfs=on' scale testing we saw an average reduction in time
of 89.43% and for 'zfs sharenfs=off' we saw an average reduction in time
of 83.36%.

Functional testing also shows a huge improvement:
- zfs share -- 97.97% reduction in time
- zfs unshare -- 96.47% reduction in time
- zfs inhert -r sharenfs -- 99.01% reduction in time

Reviewed-by: Matt Ahrens <matt@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@ixsystems.com>
Reviewed-by: Bryant G. Ly <bryangly@gmail.com>
Signed-off-by: George Wilson <gwilson@delphix.com>
External-Issue: DLPX-68690
Closes #1603
Closes #7692
Closes #7943
Closes #10300
This commit is contained in:
George Wilson
2020-07-13 11:19:18 -05:00
committed by GitHub
parent e59a377a8f
commit c15d36c674
30 changed files with 1587 additions and 1693 deletions
+2 -9
View File
@@ -30,7 +30,6 @@ USER_C = \
if BUILD_FREEBSD
USER_C += \
os/freebsd/libzfs_fsshare.c \
os/freebsd/libzfs_compat.c \
os/freebsd/libzfs_ioctl_compat.c \
os/freebsd/libzfs_zmount.c
@@ -69,14 +68,8 @@ dist_libzfs_la_SOURCES = \
nodist_libzfs_la_SOURCES = \
$(KERNEL_C)
libzfs_la_LIBADD =
if BUILD_LINUX
libzfs_la_LIBADD += \
$(abs_top_builddir)/lib/libshare/libshare.la
endif
libzfs_la_LIBADD += \
libzfs_la_LIBADD = \
$(abs_top_builddir)/lib/libshare/libshare.la \
$(abs_top_builddir)/lib/libzfs_core/libzfs_core.la \
$(abs_top_builddir)/lib/libnvpair/libnvpair.la \
$(abs_top_builddir)/lib/libuutil/libuutil.la
+22 -19
View File
@@ -24,7 +24,7 @@
* Use is subject to license terms.
*
* Portions Copyright 2007 Ramprakash Jelari
* Copyright (c) 2014, 2015 by Delphix. All rights reserved.
* Copyright (c) 2014, 2020 by Delphix. All rights reserved.
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
* Copyright (c) 2018 Datto Inc.
*/
@@ -98,6 +98,7 @@ changelist_prefix(prop_changelist_t *clp)
prop_changenode_t *cn;
uu_avl_walk_t *walk;
int ret = 0;
boolean_t commit_smb_shares = B_FALSE;
if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
clp->cl_prop != ZFS_PROP_SHARESMB)
@@ -135,6 +136,7 @@ changelist_prefix(prop_changelist_t *clp)
break;
case ZFS_PROP_SHARESMB:
(void) zfs_unshare_smb(cn->cn_handle, NULL);
commit_smb_shares = B_TRUE;
break;
default:
@@ -143,6 +145,8 @@ changelist_prefix(prop_changelist_t *clp)
}
}
if (commit_smb_shares)
zfs_commit_smb_shares();
uu_avl_walk_end(walk);
if (ret == -1)
@@ -167,7 +171,8 @@ changelist_postfix(prop_changelist_t *clp)
uu_avl_walk_t *walk;
char shareopts[ZFS_MAXPROPLEN];
int errors = 0;
libzfs_handle_t *hdl;
boolean_t commit_smb_shares = B_FALSE;
boolean_t commit_nfs_shares = B_FALSE;
/*
* If we're changing the mountpoint, attempt to destroy the underlying
@@ -182,18 +187,6 @@ changelist_postfix(prop_changelist_t *clp)
if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
remove_mountpoint(cn->cn_handle);
/*
* It is possible that the changelist_prefix() used libshare
* to unshare some entries. Since libshare caches data, an
* attempt to reshare during postfix can fail unless libshare
* is uninitialized here so that it will reinitialize later.
*/
if (cn->cn_handle != NULL) {
hdl = cn->cn_handle->zfs_hdl;
assert(hdl != NULL);
zfs_uninit_libshare(hdl);
}
/*
* We walk the datasets in reverse, because we want to mount any parent
* datasets before mounting the children. We walk all datasets even if
@@ -260,16 +253,25 @@ changelist_postfix(prop_changelist_t *clp)
* if the filesystem is currently shared, so that we can
* adopt any new options.
*/
if (sharenfs && mounted)
if (sharenfs && mounted) {
errors += zfs_share_nfs(cn->cn_handle);
else if (cn->cn_shared || clp->cl_waslegacy)
commit_nfs_shares = B_TRUE;
} else if (cn->cn_shared || clp->cl_waslegacy) {
errors += zfs_unshare_nfs(cn->cn_handle, NULL);
if (sharesmb && mounted)
commit_nfs_shares = B_TRUE;
}
if (sharesmb && mounted) {
errors += zfs_share_smb(cn->cn_handle);
else if (cn->cn_shared || clp->cl_waslegacy)
commit_smb_shares = B_TRUE;
} else if (cn->cn_shared || clp->cl_waslegacy) {
errors += zfs_unshare_smb(cn->cn_handle, NULL);
commit_smb_shares = B_TRUE;
}
}
if (commit_nfs_shares)
zfs_commit_nfs_shares();
if (commit_smb_shares)
zfs_commit_smb_shares();
uu_avl_walk_end(walk);
return (errors ? -1 : 0);
@@ -357,6 +359,7 @@ changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
ret = -1;
}
zfs_commit_proto(proto);
uu_avl_walk_end(walk);
return (ret);
+2 -36
View File
@@ -22,7 +22,7 @@
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2019 Joyent, Inc.
* Copyright (c) 2011, 2018 by Delphix. All rights reserved.
* Copyright (c) 2011, 2020 by Delphix. All rights reserved.
* Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
* Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
* Copyright (c) 2013 Martin Matuska. All rights reserved.
@@ -1432,49 +1432,14 @@ badlabel:
else
proto = PROTO_NFS;
/*
* Must be an valid sharing protocol
* option string so init the libshare
* in order to enable the parser and
* then parse the options. We use the
* control API since we don't care about
* the current configuration and don't
* want the overhead of loading it
* until we actually do something.
*/
if (zfs_init_libshare(hdl,
SA_INIT_CONTROL_API) != SA_OK) {
/*
* An error occurred so we can't do
* anything
*/
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"'%s' cannot be set: problem "
"in share initialization"),
propname);
(void) zfs_error(hdl, EZFS_BADPROP,
errbuf);
goto error;
}
if (zfs_parse_options(strval, proto) != SA_OK) {
/*
* There was an error in parsing so
* deal with it by issuing an error
* message and leaving after
* uninitializing the libshare
* interface.
*/
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
"'%s' cannot be set to invalid "
"options"), propname);
(void) zfs_error(hdl, EZFS_BADPROP,
errbuf);
zfs_uninit_libshare(hdl);
goto error;
}
zfs_uninit_libshare(hdl);
}
break;
@@ -3589,6 +3554,7 @@ create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
zfs_close(h);
}
zfs_commit_all_shares();
return (0);
+142 -26
View File
@@ -22,7 +22,7 @@
/*
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2019 by Delphix. All rights reserved.
* Copyright (c) 2014, 2020 by Delphix. All rights reserved.
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
* Copyright 2017 RackTop Systems.
* Copyright (c) 2018 Datto Inc.
@@ -593,10 +593,12 @@ zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
free(mntpt);
return (-1);
}
zfs_commit_all_shares();
if (unmount_one(hdl, mntpt, flags) != 0) {
free(mntpt);
(void) zfs_shareall(zhp);
zfs_commit_all_shares();
return (-1);
}
@@ -669,6 +671,94 @@ zfs_is_shared(zfs_handle_t *zhp)
return (rc ? B_TRUE : B_FALSE);
}
/*
* Unshare a filesystem by mountpoint.
*/
int
unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
zfs_share_proto_t proto)
{
int err;
err = sa_disable_share(mountpoint, proto_table[proto].p_name);
if (err != SA_OK) {
return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
name, sa_errorstr(err)));
}
return (0);
}
/*
* Query libshare for the given mountpoint and protocol, returning
* a zfs_share_type_t value.
*/
zfs_share_type_t
is_shared(const char *mountpoint, zfs_share_proto_t proto)
{
if (sa_is_shared(mountpoint, proto_table[proto].p_name)) {
switch (proto) {
case PROTO_NFS:
return (SHARED_NFS);
case PROTO_SMB:
return (SHARED_SMB);
default:
return (SHARED_NOT_SHARED);
}
}
return (SHARED_NOT_SHARED);
}
/*
* Share the given filesystem according to the options in the specified
* protocol specific properties (sharenfs, sharesmb). We rely
* on "libshare" to do the dirty work for us.
*/
int
zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
{
char mountpoint[ZFS_MAXPROPLEN];
char shareopts[ZFS_MAXPROPLEN];
char sourcestr[ZFS_MAXPROPLEN];
zfs_share_proto_t *curr_proto;
zprop_source_t sourcetype;
int err = 0;
if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
return (0);
for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
/*
* Return success if there are no share options.
*/
if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
shareopts, sizeof (shareopts), &sourcetype, sourcestr,
ZFS_MAXPROPLEN, B_FALSE) != 0 ||
strcmp(shareopts, "off") == 0)
continue;
/*
* If the 'zoned' property is set, then zfs_is_mountable()
* will have already bailed out if we are in the global zone.
* But local zones cannot be NFS servers, so we ignore it for
* local zones as well.
*/
if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
continue;
err = sa_enable_share(zfs_get_name(zhp), mountpoint, shareopts,
proto_table[*curr_proto].p_name);
if (err != SA_OK) {
return (zfs_error_fmt(zhp->zfs_hdl,
proto_table[*curr_proto].p_share_err,
dgettext(TEXT_DOMAIN, "cannot share '%s: %s'"),
zfs_get_name(zhp), sa_errorstr(err)));
}
}
return (0);
}
int
zfs_share(zfs_handle_t *zhp)
{
@@ -695,7 +785,7 @@ zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
if (!zfs_is_mounted(zhp, &mountpoint))
return (SHARED_NOT_SHARED);
if ((rc = is_shared_impl(zhp->zfs_hdl, mountpoint, proto))
if ((rc = is_shared(mountpoint, proto))
!= SHARED_NOT_SHARED) {
if (where != NULL)
*where = mountpoint;
@@ -722,21 +812,6 @@ zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
PROTO_SMB) != SHARED_NOT_SHARED);
}
/*
* zfs_uninit_libshare(zhandle)
*
* Uninitialize the libshare API if it hasn't already been
* uninitialized. It is OK to call multiple times.
*/
void
zfs_uninit_libshare(libzfs_handle_t *zhandle)
{
if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
sa_fini(zhandle->libzfs_sharehdl);
zhandle->libzfs_sharehdl = NULL;
}
}
/*
* zfs_parse_options(options, proto)
*
@@ -746,8 +821,45 @@ zfs_uninit_libshare(libzfs_handle_t *zhandle)
int
zfs_parse_options(char *options, zfs_share_proto_t proto)
{
return (sa_parse_legacy_options(NULL, options,
proto_table[proto].p_name));
return (sa_validate_shareopts(options, proto_table[proto].p_name));
}
void
zfs_commit_proto(zfs_share_proto_t *proto)
{
zfs_share_proto_t *curr_proto;
for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
sa_commit_shares(proto_table[*curr_proto].p_name);
}
}
void
zfs_commit_nfs_shares(void)
{
zfs_commit_proto(nfs_only);
}
void
zfs_commit_smb_shares(void)
{
zfs_commit_proto(smb_only);
}
void
zfs_commit_all_shares(void)
{
zfs_commit_proto(share_all_proto);
}
void
zfs_commit_shares(const char *proto)
{
if (proto == NULL)
zfs_commit_proto(share_all_proto);
else if (strcmp(proto, "nfs") == 0)
zfs_commit_proto(nfs_only);
else if (strcmp(proto, "smb") == 0)
zfs_commit_proto(smb_only);
}
int
@@ -793,12 +905,13 @@ zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
for (curr_proto = proto; *curr_proto != PROTO_END;
curr_proto++) {
if (is_shared_impl(hdl, mntpt, *curr_proto) &&
unshare_one(hdl, zhp->zfs_name,
mntpt, *curr_proto) != 0) {
if (mntpt != NULL)
free(mntpt);
return (-1);
if (is_shared(mntpt, *curr_proto)) {
if (unshare_one(hdl, zhp->zfs_name,
mntpt, *curr_proto) != 0) {
if (mntpt != NULL)
free(mntpt);
return (-1);
}
}
}
}
@@ -1338,6 +1451,8 @@ zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
zfs_share_one, &ms, B_FALSE);
if (ms.ms_mntstatus != 0)
ret = ms.ms_mntstatus;
else
zfs_commit_all_shares();
out:
for (int i = 0; i < cb.cb_used; i++)
@@ -1463,12 +1578,13 @@ zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
zfs_share_proto_t *curr_proto;
for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
curr_proto++) {
if (is_shared_impl(hdl, mountpoints[i], *curr_proto) &&
if (is_shared(mountpoints[i], *curr_proto) &&
unshare_one(hdl, mountpoints[i],
mountpoints[i], *curr_proto) != 0)
goto out;
}
}
zfs_commit_all_shares();
/*
* Now unmount everything, removing the underlying directories as
+1 -8
View File
@@ -22,7 +22,7 @@
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2020 Joyent, Inc. All rights reserved.
* Copyright (c) 2011, 2018 by Delphix. All rights reserved.
* Copyright (c) 2011, 2020 by Delphix. All rights reserved.
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
* Copyright (c) 2017 Datto Inc.
*/
@@ -1024,13 +1024,9 @@ libzfs_init(void)
return (NULL);
}
hdl->libzfs_sharetab = fopen(ZFS_SHARETAB, "r");
if (libzfs_core_init() != 0) {
(void) close(hdl->libzfs_fd);
(void) fclose(hdl->libzfs_mnttab);
if (hdl->libzfs_sharetab)
(void) fclose(hdl->libzfs_sharetab);
free(hdl);
return (NULL);
}
@@ -1074,9 +1070,6 @@ libzfs_fini(libzfs_handle_t *hdl)
#else
(void) fclose(hdl->libzfs_mnttab);
#endif
if (hdl->libzfs_sharetab)
(void) fclose(hdl->libzfs_sharetab);
zfs_uninit_libshare(hdl);
zpool_free_handles(hdl);
namespace_clear(hdl);
libzfs_mnttab_fini(hdl);
-406
View File
@@ -1,406 +0,0 @@
/*
* Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/vfs.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libutil.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libintl.h>
#include "libzfs_impl.h"
#define _PATH_MOUNTDPID "/var/run/mountd.pid"
#define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
#define OPTSSIZE 1024
#define MAXLINESIZE (PATH_MAX + OPTSSIZE)
void
sa_fini(sa_handle_t handle)
{
}
int
sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
{
return (SA_OK);
}
int
zfs_init_libshare(libzfs_handle_t *zhandle, int service)
{
return (SA_OK);
}
/*
* Share the given filesystem according to the options in the specified
* protocol specific properties (sharenfs, sharesmb). We rely
* on "libshare" to do the dirty work for us.
*/
int
zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
{
char mountpoint[ZFS_MAXPROPLEN];
char shareopts[ZFS_MAXPROPLEN];
char sourcestr[ZFS_MAXPROPLEN];
libzfs_handle_t *hdl = zhp->zfs_hdl;
zfs_share_proto_t *curr_proto;
zprop_source_t sourcetype;
int err, ret;
if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
return (0);
for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
/*
* Return success if there are no share options.
*/
if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
shareopts, sizeof (shareopts), &sourcetype, sourcestr,
ZFS_MAXPROPLEN, B_FALSE) != 0 ||
strcmp(shareopts, "off") == 0)
continue;
ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API);
if (ret != SA_OK) {
(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
zfs_get_name(zhp), sa_errorstr(ret));
return (-1);
}
/*
* If the 'zoned' property is set, then zfs_is_mountable()
* will have already bailed out if we are in the global zone.
* But local zones cannot be NFS servers, so we ignore it for
* local zones as well.
*/
if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
continue;
if (*curr_proto != PROTO_NFS) {
fprintf(stderr, "Unsupported share protocol: %d.\n",
*curr_proto);
continue;
}
if (strcmp(shareopts, "on") == 0)
err = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
else
err = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
if (err != 0) {
(void) zfs_error_fmt(hdl,
proto_table[*curr_proto].p_share_err,
dgettext(TEXT_DOMAIN, "cannot share '%s'"),
zfs_get_name(zhp));
return (-1);
}
}
return (0);
}
/*
* Unshare a filesystem by mountpoint.
*/
int
unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
zfs_share_proto_t proto)
{
int err;
if (proto != PROTO_NFS) {
fprintf(stderr, "No SMB support in FreeBSD yet.\n");
return (EOPNOTSUPP);
}
err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
if (err != 0) {
zfs_error_aux(hdl, "%s", strerror(err));
return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
dgettext(TEXT_DOMAIN,
"cannot unshare '%s'"), name));
}
return (0);
}
zfs_share_type_t
is_shared_impl(libzfs_handle_t *hdl, const char *mountpoint,
zfs_share_proto_t proto)
{
char buf[MAXPATHLEN], *tab;
if (hdl->libzfs_sharetab == NULL)
return (SHARED_NOT_SHARED);
(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
/* the mountpoint is the first entry on each line */
if ((tab = strchr(buf, '\t')) == NULL)
continue;
*tab = '\0';
if (strcmp(buf, mountpoint) == 0) {
if (proto == PROTO_NFS)
return (SHARED_NFS);
}
}
return (SHARED_NOT_SHARED);
}
static void
restart_mountd(void)
{
struct pidfh *pfh;
pid_t mountdpid;
pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
if (pfh != NULL) {
/* Mountd is not running. */
pidfile_remove(pfh);
return;
}
if (errno != EEXIST) {
/* Cannot open pidfile for some reason. */
return;
}
/* We have mountd(8) PID in mountdpid variable. */
kill(mountdpid, SIGHUP);
}
/*
* Read one line from a file. Skip comments, empty lines and a line with a
* mountpoint specified in the 'skip' argument.
*/
static char *
zgetline(FILE *fd, const char *skip)
{
static char line[MAXLINESIZE];
size_t len, skiplen = 0;
char *s, last;
if (skip != NULL)
skiplen = strlen(skip);
for (;;) {
s = fgets(line, sizeof (line), fd);
if (s == NULL)
return (NULL);
/* Skip empty lines and comments. */
if (line[0] == '\n' || line[0] == '#')
continue;
len = strlen(line);
if (line[len - 1] == '\n')
line[len - 1] = '\0';
last = line[skiplen];
/* Skip the given mountpoint. */
if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
(last == '\t' || last == ' ' || last == '\0')) {
continue;
}
break;
}
return (line);
}
/* BEGIN CSTYLED */
/*
* Function translate options to a format acceptable by exports(5), eg.
*
* -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 69.147.83.54
*
* Accepted input formats:
*
* ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,freefall.freebsd.org
* ro network=192.168.0.0 mask=255.255.255.0 maproot=0 freefall.freebsd.org
* -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,freefall.freebsd.org
* -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org
*
* Recognized keywords:
*
* ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs, index, quiet
*
*/
/* END CSTYLED */
static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
"network", "sec", "alldirs", "public", "webnfs", "index", "quiet",
NULL };
static char *
translate_opts(const char *shareopts)
{
static char newopts[OPTSSIZE];
char oldopts[OPTSSIZE];
char *o, *s = NULL;
unsigned int i;
size_t len;
strlcpy(oldopts, shareopts, sizeof (oldopts));
newopts[0] = '\0';
s = oldopts;
while ((o = strsep(&s, "-, ")) != NULL) {
if (o[0] == '\0')
continue;
for (i = 0; known_opts[i] != NULL; i++) {
len = strlen(known_opts[i]);
if (strncmp(known_opts[i], o, len) == 0 &&
(o[len] == '\0' || o[len] == '=')) {
strlcat(newopts, "-", sizeof (newopts));
break;
}
}
strlcat(newopts, o, sizeof (newopts));
strlcat(newopts, " ", sizeof (newopts));
}
return (newopts);
}
static int
fsshare_main(const char *file, const char *mountpoint, const char *shareopts,
int share)
{
char tmpfile[PATH_MAX];
char *line;
FILE *newfd, *oldfd;
int fd, error;
newfd = oldfd = NULL;
error = 0;
/*
* Create temporary file in the same directory, so we can atomically
* rename it.
*/
if (strlcpy(tmpfile, file, sizeof (tmpfile)) >= sizeof (tmpfile))
return (ENAMETOOLONG);
if (strlcat(tmpfile, ".XXXXXXXX", sizeof (tmpfile)) >= sizeof (tmpfile))
return (ENAMETOOLONG);
fd = mkstemp(tmpfile);
if (fd == -1)
return (errno);
/*
* File name is random, so we don't really need file lock now, but it
* will be needed after rename(2).
*/
error = flock(fd, LOCK_EX);
assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
newfd = fdopen(fd, "r+");
assert(newfd != NULL);
/* Open old exports file. */
oldfd = fopen(file, "r");
if (oldfd == NULL) {
if (share) {
if (errno != ENOENT) {
error = errno;
goto out;
}
} else {
/* If there is no exports file, ignore the error. */
if (errno == ENOENT)
errno = 0;
error = errno;
goto out;
}
} else {
error = flock(fileno(oldfd), LOCK_EX);
assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
error = 0;
}
/* Place big, fat warning at the beginning of the file. */
fprintf(newfd, "%s", FILE_HEADER);
while (oldfd != NULL && (line = zgetline(oldfd, mountpoint)) != NULL)
fprintf(newfd, "%s\n", line);
if (oldfd != NULL && ferror(oldfd) != 0) {
error = ferror(oldfd);
goto out;
}
if (ferror(newfd) != 0) {
error = ferror(newfd);
goto out;
}
if (share) {
fprintf(newfd, "%s\t%s\n", mountpoint,
translate_opts(shareopts));
}
out:
if (error != 0)
unlink(tmpfile);
else {
if (rename(tmpfile, file) == -1) {
error = errno;
unlink(tmpfile);
} else {
fflush(newfd);
/*
* Send SIGHUP to mountd, but unlock exports file later.
*/
restart_mountd();
}
}
if (oldfd != NULL) {
flock(fileno(oldfd), LOCK_UN);
fclose(oldfd);
}
if (newfd != NULL) {
flock(fileno(newfd), LOCK_UN);
fclose(newfd);
}
return (error);
}
/*
* Add the given mountpoint to the given exports file.
*/
int
fsshare(const char *file, const char *mountpoint, const char *shareopts)
{
return (fsshare_main(file, mountpoint, shareopts, 1));
}
/*
* Remove the given mountpoint from the given exports file.
*/
int
fsunshare(const char *file, const char *mountpoint)
{
return (fsshare_main(file, mountpoint, NULL, 0));
}
+1 -234
View File
@@ -22,7 +22,7 @@
/*
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2019 by Delphix. All rights reserved.
* Copyright (c) 2014, 2020 by Delphix. All rights reserved.
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
* Copyright 2017 RackTop Systems.
* Copyright (c) 2018 Datto Inc.
@@ -50,239 +50,6 @@
#include "libzfs_impl.h"
#include <thread_pool.h>
/*
* zfs_init_libshare(zhandle, service)
*
* Initialize the libshare API if it hasn't already been initialized.
* In all cases it returns 0 if it succeeded and an error if not. The
* service value is which part(s) of the API to initialize and is a
* direct map to the libshare sa_init(service) interface.
*/
int
zfs_init_libshare(libzfs_handle_t *zhandle, int service)
{
int ret = SA_OK;
if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
/*
* We had a cache miss. Most likely it is a new ZFS
* dataset that was just created. We want to make sure
* so check timestamps to see if a different process
* has updated any of the configuration. If there was
* some non-ZFS change, we need to re-initialize the
* internal cache.
*/
zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
if (sa_needs_refresh(zhandle->libzfs_sharehdl)) {
zfs_uninit_libshare(zhandle);
zhandle->libzfs_sharehdl = sa_init(service);
}
}
if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
zhandle->libzfs_sharehdl = sa_init(service);
if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
ret = SA_NO_MEMORY;
return (ret);
}
/*
* Share the given filesystem according to the options in the specified
* protocol specific properties (sharenfs, sharesmb). We rely
* on "libshare" to do the dirty work for us.
*/
int
zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
{
char mountpoint[ZFS_MAXPROPLEN];
char shareopts[ZFS_MAXPROPLEN];
char sourcestr[ZFS_MAXPROPLEN];
libzfs_handle_t *hdl = zhp->zfs_hdl;
sa_share_t share;
zfs_share_proto_t *curr_proto;
zprop_source_t sourcetype;
int err, ret;
if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
return (0);
for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
/*
* Return success if there are no share options.
*/
if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
shareopts, sizeof (shareopts), &sourcetype, sourcestr,
ZFS_MAXPROPLEN, B_FALSE) != 0 ||
strcmp(shareopts, "off") == 0)
continue;
ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API);
if (ret != SA_OK) {
(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
zfs_get_name(zhp), sa_errorstr(ret));
return (-1);
}
/*
* If the 'zoned' property is set, then zfs_is_mountable()
* will have already bailed out if we are in the global zone.
* But local zones cannot be NFS servers, so we ignore it for
* local zones as well.
*/
if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
continue;
share = sa_find_share(hdl->libzfs_sharehdl, mountpoint);
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.
*/
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) {
(void) zfs_error_fmt(hdl,
proto_table[*curr_proto].p_share_err,
dgettext(TEXT_DOMAIN, "cannot share '%s'"),
zfs_get_name(zhp));
return (-1);
}
hdl->libzfs_shareflags |= ZFSSHARE_MISS;
share = sa_find_share(hdl->libzfs_sharehdl,
mountpoint);
}
if (share != NULL) {
err = sa_enable_share(share,
proto_table[*curr_proto].p_name);
if (err != SA_OK) {
(void) zfs_error_fmt(hdl,
proto_table[*curr_proto].p_share_err,
dgettext(TEXT_DOMAIN, "cannot share '%s'"),
zfs_get_name(zhp));
return (-1);
}
} else {
(void) zfs_error_fmt(hdl,
proto_table[*curr_proto].p_share_err,
dgettext(TEXT_DOMAIN, "cannot share '%s'"),
zfs_get_name(zhp));
return (-1);
}
}
return (0);
}
/*
* Unshare a filesystem by mountpoint.
*/
int
unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
zfs_share_proto_t proto)
{
sa_share_t share;
int err;
char *mntpt;
/*
* Mountpoint could get trashed if libshare calls getmntany
* which it does during API initialization, so strdup the
* value.
*/
mntpt = zfs_strdup(hdl, mountpoint);
/* make sure libshare initialized */
if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
free(mntpt); /* don't need the copy anymore */
return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
name, sa_errorstr(err)));
}
share = sa_find_share(hdl->libzfs_sharehdl, mntpt);
free(mntpt); /* don't need the copy anymore */
if (share != NULL) {
err = sa_disable_share(share, proto_table[proto].p_name);
if (err != SA_OK) {
return (zfs_error_fmt(hdl,
proto_table[proto].p_unshare_err,
dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
name, sa_errorstr(err)));
}
} else {
return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
name));
}
return (0);
}
/*
* Search the sharetab for the given mountpoint and protocol, returning
* a zfs_share_type_t value.
*/
zfs_share_type_t
is_shared_impl(libzfs_handle_t *hdl, const char *mountpoint,
zfs_share_proto_t proto)
{
char buf[MAXPATHLEN], *tab;
char *ptr;
if (hdl->libzfs_sharetab == NULL)
return (SHARED_NOT_SHARED);
/* Reopen ZFS_SHARETAB to prevent reading stale data from open file */
if (freopen(ZFS_SHARETAB, "r", hdl->libzfs_sharetab) == NULL)
return (SHARED_NOT_SHARED);
(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
/* the mountpoint is the first entry on each line */
if ((tab = strchr(buf, '\t')) == NULL)
continue;
*tab = '\0';
if (strcmp(buf, mountpoint) == 0) {
/*
* the protocol field is the third field
* skip over second field
*/
ptr = ++tab;
if ((tab = strchr(ptr, '\t')) == NULL)
continue;
ptr = ++tab;
if ((tab = strchr(ptr, '\t')) == NULL)
continue;
*tab = '\0';
if (strcmp(ptr,
proto_table[proto].p_name) == 0) {
switch (proto) {
case PROTO_NFS:
return (SHARED_NFS);
case PROTO_SMB:
return (SHARED_SMB);
default:
return (0);
}
}
}
}
return (SHARED_NOT_SHARED);
}
#define ZS_COMMENT 0x00000000 /* comment */
#define ZS_ZFSUTIL 0x00000001 /* caller is zfs(8) */