mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-22 02:27:36 +03:00
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:
@@ -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) */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user