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:
@@ -0,0 +1,448 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* Copyright (c) 2020 by Delphix. All rights reserved.
|
||||
*/
|
||||
|
||||
#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"
|
||||
#include "libshare_impl.h"
|
||||
#include "nfs.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)
|
||||
#define ZFS_EXPORTS_FILE "/etc/zfs/exports"
|
||||
#define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
|
||||
|
||||
static sa_fstype_t *nfs_fstype;
|
||||
|
||||
static int nfs_lock_fd = -1;
|
||||
|
||||
/*
|
||||
* The nfs_exports_[lock|unlock] is used to guard against conconcurrent
|
||||
* updates to the exports file. Each protocol is responsible for
|
||||
* providing the necessary locking to ensure consistency.
|
||||
*/
|
||||
static int
|
||||
nfs_exports_lock(void)
|
||||
{
|
||||
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
|
||||
O_RDWR | O_CREAT, 0600);
|
||||
if (nfs_lock_fd == -1) {
|
||||
fprintf(stderr, "failed to lock %s: %s\n",
|
||||
ZFS_EXPORTS_LOCK, strerror(errno));
|
||||
return (errno);
|
||||
}
|
||||
if (flock(nfs_lock_fd, LOCK_EX) != 0) {
|
||||
fprintf(stderr, "failed to lock %s: %s\n",
|
||||
ZFS_EXPORTS_LOCK, strerror(errno));
|
||||
return (errno);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
nfs_exports_unlock(void)
|
||||
{
|
||||
verify(nfs_lock_fd > 0);
|
||||
|
||||
if (flock(nfs_lock_fd, LOCK_UN) != 0) {
|
||||
fprintf(stderr, "failed to unlock %s: %s\n",
|
||||
ZFS_EXPORTS_LOCK, strerror(errno));
|
||||
}
|
||||
close(nfs_lock_fd);
|
||||
nfs_lock_fd = -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read one line from a file. Skip comments, empty lines and a line with a
|
||||
* mountpoint specified in the 'skip' argument.
|
||||
*
|
||||
* NOTE: This function returns a static buffer and thus is not thread-safe.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function translate options to a format acceptable by exports(5), eg.
|
||||
*
|
||||
* -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
|
||||
* zfs.freebsd.org 69.147.83.54
|
||||
*
|
||||
* Accepted input formats:
|
||||
*
|
||||
* ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org
|
||||
* ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org
|
||||
* -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org
|
||||
* -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
|
||||
* zfs.freebsd.org
|
||||
*
|
||||
* Recognized keywords:
|
||||
*
|
||||
* ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs,
|
||||
* index, quiet
|
||||
*
|
||||
* NOTE: This function returns a static buffer and thus is not thread-safe.
|
||||
*/
|
||||
static char *
|
||||
translate_opts(const char *shareopts)
|
||||
{
|
||||
static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
|
||||
"network", "sec", "alldirs", "public", "webnfs", "index", "quiet",
|
||||
NULL };
|
||||
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 char *
|
||||
nfs_init_tmpfile(void)
|
||||
{
|
||||
char *tmpfile = NULL;
|
||||
|
||||
if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
|
||||
fprintf(stderr, "Unable to allocate buffer for temporary "
|
||||
"file name\n");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int fd = mkstemp(tmpfile);
|
||||
if (fd == -1) {
|
||||
fprintf(stderr, "Unable to create temporary file: %s",
|
||||
strerror(errno));
|
||||
free(tmpfile);
|
||||
return (NULL);
|
||||
}
|
||||
close(fd);
|
||||
return (tmpfile);
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_fini_tmpfile(char *tmpfile)
|
||||
{
|
||||
if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
|
||||
fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
|
||||
strerror(errno));
|
||||
unlink(tmpfile);
|
||||
free(tmpfile);
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
free(tmpfile);
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function copies all entries from the exports file to "filename",
|
||||
* omitting any entries for the specified mountpoint.
|
||||
*/
|
||||
static int
|
||||
nfs_copy_entries(char *filename, const char *mountpoint)
|
||||
{
|
||||
int error = SA_OK;
|
||||
char *line;
|
||||
|
||||
/*
|
||||
* If the file doesn't exist then there is nothing more
|
||||
* we need to do.
|
||||
*/
|
||||
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
|
||||
if (oldfp == NULL)
|
||||
return (SA_OK);
|
||||
|
||||
FILE *newfp = fopen(filename, "w+");
|
||||
fputs(FILE_HEADER, newfp);
|
||||
while ((line = zgetline(oldfp, mountpoint)) != NULL)
|
||||
fprintf(newfp, "%s\n", line);
|
||||
if (ferror(oldfp) != 0) {
|
||||
error = ferror(oldfp);
|
||||
}
|
||||
if (error == 0 && ferror(newfp) != 0) {
|
||||
error = ferror(newfp);
|
||||
}
|
||||
|
||||
if (fclose(newfp) != 0) {
|
||||
fprintf(stderr, "Unable to close file %s: %s\n",
|
||||
filename, strerror(errno));
|
||||
error = error != 0 ? error : SA_SYSTEM_ERR;
|
||||
}
|
||||
fclose(oldfp);
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_enable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
char *filename = NULL;
|
||||
int error;
|
||||
|
||||
if ((filename = nfs_init_tmpfile()) == NULL)
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
error = nfs_exports_lock();
|
||||
if (error != 0) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
return (error);
|
||||
}
|
||||
|
||||
error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
|
||||
if (error != SA_OK) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
FILE *fp = fopen(filename, "a+");
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "failed to open %s file: %s", filename,
|
||||
strerror(errno));
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
|
||||
if (strcmp(shareopts, "on") == 0)
|
||||
shareopts = "";
|
||||
|
||||
if (fprintf(fp, "%s\t%s\n", impl_share->sa_mountpoint,
|
||||
translate_opts(shareopts)) < 0) {
|
||||
fprintf(stderr, "failed to write to %s\n", filename);
|
||||
fclose(fp);
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
|
||||
if (fclose(fp) != 0) {
|
||||
fprintf(stderr, "Unable to close file %s: %s\n",
|
||||
filename, strerror(errno));
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
error = nfs_fini_tmpfile(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_disable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
int error;
|
||||
char *filename = NULL;
|
||||
|
||||
if ((filename = nfs_init_tmpfile()) == NULL)
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
error = nfs_exports_lock();
|
||||
if (error != 0) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
return (error);
|
||||
}
|
||||
|
||||
error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
|
||||
if (error != SA_OK) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
error = nfs_fini_tmpfile(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
/*
|
||||
* NOTE: This function returns a static buffer and thus is not thread-safe.
|
||||
*/
|
||||
static boolean_t
|
||||
nfs_is_shared(sa_share_impl_t impl_share)
|
||||
{
|
||||
static char line[MAXLINESIZE];
|
||||
char *s, last;
|
||||
size_t len;
|
||||
char *mntpoint = impl_share->sa_mountpoint;
|
||||
size_t mntlen = strlen(mntpoint);
|
||||
|
||||
FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
|
||||
if (fp == NULL)
|
||||
return (B_FALSE);
|
||||
|
||||
for (;;) {
|
||||
s = fgets(line, sizeof (line), fp);
|
||||
if (s == NULL)
|
||||
return (B_FALSE);
|
||||
/* 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[mntlen];
|
||||
/* Skip the given mountpoint. */
|
||||
if (strncmp(mntpoint, line, mntlen) == 0 &&
|
||||
(last == '\t' || last == ' ' || last == '\0')) {
|
||||
fclose(fp);
|
||||
return (B_TRUE);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
return (B_FALSE);
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_validate_shareopts(const char *shareopts)
|
||||
{
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
|
||||
{
|
||||
FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
static void
|
||||
nfs_clear_shareopts(sa_share_impl_t impl_share)
|
||||
{
|
||||
FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Commit the shares by restarting mountd.
|
||||
*/
|
||||
static int
|
||||
nfs_commit_shares(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 (SA_OK);
|
||||
}
|
||||
if (errno != EEXIST) {
|
||||
/* Cannot open pidfile for some reason. */
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
/* We have mountd(8) PID in mountdpid variable. */
|
||||
kill(mountdpid, SIGHUP);
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
static const sa_share_ops_t nfs_shareops = {
|
||||
.enable_share = nfs_enable_share,
|
||||
.disable_share = nfs_disable_share,
|
||||
.is_shared = nfs_is_shared,
|
||||
|
||||
.validate_shareopts = nfs_validate_shareopts,
|
||||
.update_shareopts = nfs_update_shareopts,
|
||||
.clear_shareopts = nfs_clear_shareopts,
|
||||
.commit_shares = nfs_commit_shares,
|
||||
};
|
||||
|
||||
/*
|
||||
* Initializes the NFS functionality of libshare.
|
||||
*/
|
||||
void
|
||||
libshare_nfs_init(void)
|
||||
{
|
||||
nfs_fstype = register_fstype("nfs", &nfs_shareops);
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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
|
||||
* or http://www.opensolaris.org/os/licensing.
|
||||
* 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 (c) 2020 by Delphix. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <libzfs.h>
|
||||
#include <libshare.h>
|
||||
#include "libshare_impl.h"
|
||||
#include "smb.h"
|
||||
|
||||
static sa_fstype_t *smb_fstype;
|
||||
|
||||
/*
|
||||
* Enables SMB sharing for the specified share.
|
||||
*/
|
||||
static int
|
||||
smb_enable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
fprintf(stderr, "No SMB support in FreeBSD yet.\n");
|
||||
return (SA_NOT_SUPPORTED);
|
||||
}
|
||||
/*
|
||||
* Disables SMB sharing for the specified share.
|
||||
*/
|
||||
static int
|
||||
smb_disable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
fprintf(stderr, "No SMB support in FreeBSD yet.\n");
|
||||
return (SA_NOT_SUPPORTED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks whether the specified SMB share options are syntactically correct.
|
||||
*/
|
||||
static int
|
||||
smb_validate_shareopts(const char *shareopts)
|
||||
{
|
||||
fprintf(stderr, "No SMB support in FreeBSD yet.\n");
|
||||
return (SA_NOT_SUPPORTED);
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks whether a share is currently active.
|
||||
*/
|
||||
static boolean_t
|
||||
smb_is_share_active(sa_share_impl_t impl_share)
|
||||
{
|
||||
return (B_FALSE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Called to update a share's options. A share's options might be out of
|
||||
* date if the share was loaded from disk and the "sharesmb" dataset
|
||||
* property has changed in the meantime. This function also takes care
|
||||
* of re-enabling the share if necessary.
|
||||
*/
|
||||
static int
|
||||
smb_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
|
||||
{
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
static int
|
||||
smb_update_shares(void)
|
||||
{
|
||||
/* Not implemented */
|
||||
return (0);
|
||||
}
|
||||
/*
|
||||
* Clears a share's SMB options. Used by libshare to
|
||||
* clean up shares that are about to be free()'d.
|
||||
*/
|
||||
static void
|
||||
smb_clear_shareopts(sa_share_impl_t impl_share)
|
||||
{
|
||||
FSINFO(impl_share, smb_fstype)->shareopts = NULL;
|
||||
}
|
||||
|
||||
static const sa_share_ops_t smb_shareops = {
|
||||
.enable_share = smb_enable_share,
|
||||
.disable_share = smb_disable_share,
|
||||
.is_shared = smb_is_share_active,
|
||||
|
||||
.validate_shareopts = smb_validate_shareopts,
|
||||
.update_shareopts = smb_update_shareopts,
|
||||
.clear_shareopts = smb_clear_shareopts,
|
||||
.commit_shares = smb_update_shares,
|
||||
};
|
||||
|
||||
/*
|
||||
* Initializes the SMB functionality of libshare.
|
||||
*/
|
||||
void
|
||||
libshare_smb_init(void)
|
||||
{
|
||||
smb_fstype = register_fstype("smb", &smb_shareops);
|
||||
}
|
||||
@@ -0,0 +1,712 @@
|
||||
/*
|
||||
* 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
|
||||
* or http://www.opensolaris.org/os/licensing.
|
||||
* 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 (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011 Gunnar Beutner
|
||||
* Copyright (c) 2012 Cyril Plisko. All rights reserved.
|
||||
* Copyright (c) 2019, 2020 by Delphix. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <errno.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <libzfs.h>
|
||||
#include <libshare.h>
|
||||
#include "libshare_impl.h"
|
||||
#include "nfs.h"
|
||||
|
||||
#define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
|
||||
#define ZFS_EXPORTS_DIR "/etc/exports.d"
|
||||
#define ZFS_EXPORTS_FILE ZFS_EXPORTS_DIR"/zfs.exports"
|
||||
#define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
|
||||
|
||||
static sa_fstype_t *nfs_fstype;
|
||||
|
||||
typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
|
||||
void *cookie);
|
||||
|
||||
typedef int (*nfs_host_callback_t)(const char *sharepath, const char *filename,
|
||||
const char *host, const char *security, const char *access, void *cookie);
|
||||
|
||||
static int nfs_lock_fd = -1;
|
||||
|
||||
/*
|
||||
* The nfs_exports_[lock|unlock] is used to guard against conconcurrent
|
||||
* updates to the exports file. Each protocol is responsible for
|
||||
* providing the necessary locking to ensure consistency.
|
||||
*/
|
||||
static int
|
||||
nfs_exports_lock(void)
|
||||
{
|
||||
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
|
||||
O_RDWR | O_CREAT, 0600);
|
||||
if (nfs_lock_fd == -1) {
|
||||
fprintf(stderr, "failed to lock %s: %s\n",
|
||||
ZFS_EXPORTS_LOCK, strerror(errno));
|
||||
return (errno);
|
||||
}
|
||||
if (flock(nfs_lock_fd, LOCK_EX) != 0) {
|
||||
fprintf(stderr, "failed to lock %s: %s\n",
|
||||
ZFS_EXPORTS_LOCK, strerror(errno));
|
||||
return (errno);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
nfs_exports_unlock(void)
|
||||
{
|
||||
verify(nfs_lock_fd > 0);
|
||||
|
||||
if (flock(nfs_lock_fd, LOCK_UN) != 0) {
|
||||
fprintf(stderr, "failed to unlock %s: %s\n",
|
||||
ZFS_EXPORTS_LOCK, strerror(errno));
|
||||
}
|
||||
close(nfs_lock_fd);
|
||||
nfs_lock_fd = -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Invokes the specified callback function for each Solaris share option
|
||||
* listed in the specified string.
|
||||
*/
|
||||
static int
|
||||
foreach_nfs_shareopt(const char *shareopts,
|
||||
nfs_shareopt_callback_t callback, void *cookie)
|
||||
{
|
||||
char *shareopts_dup, *opt, *cur, *value;
|
||||
int was_nul, error;
|
||||
|
||||
if (shareopts == NULL)
|
||||
return (SA_OK);
|
||||
|
||||
if (strcmp(shareopts, "on") == 0)
|
||||
shareopts = "rw,crossmnt";
|
||||
|
||||
shareopts_dup = strdup(shareopts);
|
||||
|
||||
|
||||
if (shareopts_dup == NULL)
|
||||
return (SA_NO_MEMORY);
|
||||
|
||||
opt = shareopts_dup;
|
||||
was_nul = 0;
|
||||
|
||||
while (1) {
|
||||
cur = opt;
|
||||
|
||||
while (*cur != ',' && *cur != '\0')
|
||||
cur++;
|
||||
|
||||
if (*cur == '\0')
|
||||
was_nul = 1;
|
||||
|
||||
*cur = '\0';
|
||||
|
||||
if (cur > opt) {
|
||||
value = strchr(opt, '=');
|
||||
|
||||
if (value != NULL) {
|
||||
*value = '\0';
|
||||
value++;
|
||||
}
|
||||
|
||||
error = callback(opt, value, cookie);
|
||||
|
||||
if (error != SA_OK) {
|
||||
free(shareopts_dup);
|
||||
return (error);
|
||||
}
|
||||
}
|
||||
|
||||
opt = cur + 1;
|
||||
|
||||
if (was_nul)
|
||||
break;
|
||||
}
|
||||
|
||||
free(shareopts_dup);
|
||||
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
typedef struct nfs_host_cookie_s {
|
||||
nfs_host_callback_t callback;
|
||||
const char *sharepath;
|
||||
void *cookie;
|
||||
const char *filename;
|
||||
const char *security;
|
||||
} nfs_host_cookie_t;
|
||||
|
||||
/*
|
||||
* Helper function for foreach_nfs_host. This function checks whether the
|
||||
* current share option is a host specification and invokes a callback
|
||||
* function with information about the host.
|
||||
*/
|
||||
static int
|
||||
foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
|
||||
{
|
||||
int error;
|
||||
const char *access;
|
||||
char *host_dup, *host, *next;
|
||||
nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
|
||||
#endif
|
||||
|
||||
if (strcmp(opt, "sec") == 0)
|
||||
udata->security = value;
|
||||
|
||||
if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
|
||||
if (value == NULL)
|
||||
value = "*";
|
||||
|
||||
access = opt;
|
||||
|
||||
host_dup = strdup(value);
|
||||
|
||||
if (host_dup == NULL)
|
||||
return (SA_NO_MEMORY);
|
||||
|
||||
host = host_dup;
|
||||
|
||||
do {
|
||||
next = strchr(host, ':');
|
||||
if (next != NULL) {
|
||||
*next = '\0';
|
||||
next++;
|
||||
}
|
||||
|
||||
error = udata->callback(udata->filename,
|
||||
udata->sharepath, host, udata->security,
|
||||
access, udata->cookie);
|
||||
|
||||
if (error != SA_OK) {
|
||||
free(host_dup);
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
host = next;
|
||||
} while (host != NULL);
|
||||
|
||||
free(host_dup);
|
||||
}
|
||||
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Invokes a callback function for all NFS hosts that are set for a share.
|
||||
*/
|
||||
static int
|
||||
foreach_nfs_host(sa_share_impl_t impl_share, char *filename,
|
||||
nfs_host_callback_t callback, void *cookie)
|
||||
{
|
||||
nfs_host_cookie_t udata;
|
||||
char *shareopts;
|
||||
|
||||
udata.callback = callback;
|
||||
udata.sharepath = impl_share->sa_mountpoint;
|
||||
udata.cookie = cookie;
|
||||
udata.filename = filename;
|
||||
udata.security = "sys";
|
||||
|
||||
shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
|
||||
|
||||
return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb,
|
||||
&udata));
|
||||
}
|
||||
|
||||
/*
|
||||
* Converts a Solaris NFS host specification to its Linux equivalent.
|
||||
*/
|
||||
static int
|
||||
get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec)
|
||||
{
|
||||
/*
|
||||
* For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
|
||||
* wildcards (e.g. *.example.org).
|
||||
*/
|
||||
if (solaris_hostspec[0] == '@') {
|
||||
/*
|
||||
* Solaris host specifier, e.g. @192.168.0.0/16; we just need
|
||||
* to skip the @ in this case
|
||||
*/
|
||||
*plinux_hostspec = strdup(solaris_hostspec + 1);
|
||||
} else {
|
||||
*plinux_hostspec = strdup(solaris_hostspec);
|
||||
}
|
||||
|
||||
if (*plinux_hostspec == NULL) {
|
||||
return (SA_NO_MEMORY);
|
||||
}
|
||||
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Adds a Linux share option to an array of NFS options.
|
||||
*/
|
||||
static int
|
||||
add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
|
||||
{
|
||||
size_t len = 0;
|
||||
char *new_linux_opts;
|
||||
|
||||
if (*plinux_opts != NULL)
|
||||
len = strlen(*plinux_opts);
|
||||
|
||||
new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
|
||||
(value ? 1 + strlen(value) : 0) + 1);
|
||||
|
||||
if (new_linux_opts == NULL)
|
||||
return (SA_NO_MEMORY);
|
||||
|
||||
new_linux_opts[len] = '\0';
|
||||
|
||||
if (len > 0)
|
||||
strcat(new_linux_opts, ",");
|
||||
|
||||
strcat(new_linux_opts, key);
|
||||
|
||||
if (value != NULL) {
|
||||
strcat(new_linux_opts, "=");
|
||||
strcat(new_linux_opts, value);
|
||||
}
|
||||
|
||||
*plinux_opts = new_linux_opts;
|
||||
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Validates and converts a single Solaris share option to its Linux
|
||||
* equivalent.
|
||||
*/
|
||||
static int
|
||||
get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
|
||||
{
|
||||
char **plinux_opts = (char **)cookie;
|
||||
|
||||
/* host-specific options, these are taken care of elsewhere */
|
||||
if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 ||
|
||||
strcmp(key, "sec") == 0)
|
||||
return (SA_OK);
|
||||
|
||||
if (strcmp(key, "anon") == 0)
|
||||
key = "anonuid";
|
||||
|
||||
if (strcmp(key, "root_mapping") == 0) {
|
||||
(void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
|
||||
key = "anonuid";
|
||||
}
|
||||
|
||||
if (strcmp(key, "nosub") == 0)
|
||||
key = "subtree_check";
|
||||
|
||||
if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 &&
|
||||
strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 &&
|
||||
strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 &&
|
||||
strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 &&
|
||||
strcmp(key, "crossmnt") != 0 &&
|
||||
strcmp(key, "no_subtree_check") != 0 &&
|
||||
strcmp(key, "subtree_check") != 0 &&
|
||||
strcmp(key, "insecure_locks") != 0 &&
|
||||
strcmp(key, "secure_locks") != 0 &&
|
||||
strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 &&
|
||||
strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 &&
|
||||
strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 &&
|
||||
strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 &&
|
||||
strcmp(key, "root_squash") != 0 &&
|
||||
strcmp(key, "no_root_squash") != 0 &&
|
||||
strcmp(key, "all_squash") != 0 &&
|
||||
strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 &&
|
||||
strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) {
|
||||
return (SA_SYNTAX_ERR);
|
||||
}
|
||||
|
||||
(void) add_linux_shareopt(plinux_opts, key, value);
|
||||
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Takes a string containing Solaris share options (e.g. "sync,no_acl") and
|
||||
* converts them to a NULL-terminated array of Linux NFS options.
|
||||
*/
|
||||
static int
|
||||
get_linux_shareopts(const char *shareopts, char **plinux_opts)
|
||||
{
|
||||
int error;
|
||||
|
||||
assert(plinux_opts != NULL);
|
||||
|
||||
*plinux_opts = NULL;
|
||||
|
||||
/* no_subtree_check - Default as of nfs-utils v1.1.0 */
|
||||
(void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
|
||||
|
||||
/* mountpoint - Restrict exports to ZFS mountpoints */
|
||||
(void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
|
||||
|
||||
error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
|
||||
plinux_opts);
|
||||
|
||||
if (error != SA_OK) {
|
||||
free(*plinux_opts);
|
||||
*plinux_opts = NULL;
|
||||
}
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static char *
|
||||
nfs_init_tmpfile(void)
|
||||
{
|
||||
char *tmpfile = NULL;
|
||||
|
||||
if (asprintf(&tmpfile, "%s%s", ZFS_EXPORTS_FILE, ".XXXXXXXX") == -1) {
|
||||
fprintf(stderr, "Unable to allocate temporary file\n");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int fd = mkstemp(tmpfile);
|
||||
if (fd == -1) {
|
||||
fprintf(stderr, "Unable to create temporary file: %s",
|
||||
strerror(errno));
|
||||
free(tmpfile);
|
||||
return (NULL);
|
||||
}
|
||||
close(fd);
|
||||
return (tmpfile);
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_fini_tmpfile(char *tmpfile)
|
||||
{
|
||||
if (rename(tmpfile, ZFS_EXPORTS_FILE) == -1) {
|
||||
fprintf(stderr, "Unable to rename %s: %s\n", tmpfile,
|
||||
strerror(errno));
|
||||
unlink(tmpfile);
|
||||
free(tmpfile);
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
free(tmpfile);
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function populates an entry into /etc/exports.d/zfs.exports.
|
||||
* This file is consumed by the linux nfs server so that zfs shares are
|
||||
* automatically exported upon boot or whenever the nfs server restarts.
|
||||
*/
|
||||
static int
|
||||
nfs_add_entry(const char *filename, const char *sharepath,
|
||||
const char *host, const char *security, const char *access_opts,
|
||||
void *pcookie)
|
||||
{
|
||||
int error;
|
||||
char *linuxhost;
|
||||
const char *linux_opts = (const char *)pcookie;
|
||||
|
||||
error = get_linux_hostspec(host, &linuxhost);
|
||||
if (error != SA_OK)
|
||||
return (error);
|
||||
|
||||
if (linux_opts == NULL)
|
||||
linux_opts = "";
|
||||
|
||||
FILE *fp = fopen(filename, "a+");
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "failed to open %s file: %s", filename,
|
||||
strerror(errno));
|
||||
free(linuxhost);
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
|
||||
if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost,
|
||||
security, access_opts, linux_opts) < 0) {
|
||||
fprintf(stderr, "failed to write to %s\n", filename);
|
||||
free(linuxhost);
|
||||
fclose(fp);
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
|
||||
free(linuxhost);
|
||||
if (fclose(fp) != 0) {
|
||||
fprintf(stderr, "Unable to close file %s: %s\n",
|
||||
filename, strerror(errno));
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function copies all entries from the exports file to "filename",
|
||||
* omitting any entries for the specified mountpoint.
|
||||
*/
|
||||
static int
|
||||
nfs_copy_entries(char *filename, const char *mountpoint)
|
||||
{
|
||||
char *buf = NULL;
|
||||
size_t buflen = 0;
|
||||
int error = SA_OK;
|
||||
|
||||
/*
|
||||
* If the file doesn't exist then there is nothing more
|
||||
* we need to do.
|
||||
*/
|
||||
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
|
||||
if (oldfp == NULL)
|
||||
return (SA_OK);
|
||||
|
||||
FILE *newfp = fopen(filename, "w+");
|
||||
fputs(FILE_HEADER, newfp);
|
||||
while ((getline(&buf, &buflen, oldfp)) != -1) {
|
||||
char *space = NULL;
|
||||
|
||||
if (buf[0] == '\n' || buf[0] == '#')
|
||||
continue;
|
||||
|
||||
if ((space = strchr(buf, ' ')) != NULL) {
|
||||
int mountpoint_len = strlen(mountpoint);
|
||||
|
||||
if (space - buf == mountpoint_len &&
|
||||
strncmp(mountpoint, buf, mountpoint_len) == 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
fputs(buf, newfp);
|
||||
}
|
||||
|
||||
if (oldfp != NULL && ferror(oldfp) != 0) {
|
||||
error = ferror(oldfp);
|
||||
}
|
||||
if (error == 0 && ferror(newfp) != 0) {
|
||||
error = ferror(newfp);
|
||||
}
|
||||
|
||||
free(buf);
|
||||
if (fclose(newfp) != 0) {
|
||||
fprintf(stderr, "Unable to close file %s: %s\n",
|
||||
filename, strerror(errno));
|
||||
error = error != 0 ? error : SA_SYSTEM_ERR;
|
||||
}
|
||||
fclose(oldfp);
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
/*
|
||||
* Enables NFS sharing for the specified share.
|
||||
*/
|
||||
static int
|
||||
nfs_enable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
char *shareopts, *linux_opts;
|
||||
char *filename = NULL;
|
||||
int error;
|
||||
|
||||
if ((filename = nfs_init_tmpfile()) == NULL)
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
error = nfs_exports_lock();
|
||||
if (error != 0) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
return (error);
|
||||
}
|
||||
|
||||
error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
|
||||
if (error != SA_OK) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
|
||||
error = get_linux_shareopts(shareopts, &linux_opts);
|
||||
if (error != SA_OK) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
error = foreach_nfs_host(impl_share, filename, nfs_add_entry,
|
||||
linux_opts);
|
||||
free(linux_opts);
|
||||
if (error == 0) {
|
||||
error = nfs_fini_tmpfile(filename);
|
||||
} else {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
}
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
/*
|
||||
* Disables NFS sharing for the specified share.
|
||||
*/
|
||||
static int
|
||||
nfs_disable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
int error;
|
||||
char *filename = NULL;
|
||||
|
||||
if ((filename = nfs_init_tmpfile()) == NULL)
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
error = nfs_exports_lock();
|
||||
if (error != 0) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
return (error);
|
||||
}
|
||||
|
||||
error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
|
||||
if (error != SA_OK) {
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
error = nfs_fini_tmpfile(filename);
|
||||
nfs_exports_unlock();
|
||||
return (error);
|
||||
}
|
||||
|
||||
static boolean_t
|
||||
nfs_is_shared(sa_share_impl_t impl_share)
|
||||
{
|
||||
size_t buflen = 0;
|
||||
char *buf = NULL;
|
||||
|
||||
FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
|
||||
if (fp == NULL) {
|
||||
return (B_FALSE);
|
||||
}
|
||||
while ((getline(&buf, &buflen, fp)) != -1) {
|
||||
char *space = NULL;
|
||||
|
||||
if ((space = strchr(buf, ' ')) != NULL) {
|
||||
int mountpoint_len = strlen(impl_share->sa_mountpoint);
|
||||
|
||||
if (space - buf == mountpoint_len &&
|
||||
strncmp(impl_share->sa_mountpoint, buf,
|
||||
mountpoint_len) == 0) {
|
||||
fclose(fp);
|
||||
free(buf);
|
||||
return (B_TRUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
fclose(fp);
|
||||
return (B_FALSE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks whether the specified NFS share options are syntactically correct.
|
||||
*/
|
||||
static int
|
||||
nfs_validate_shareopts(const char *shareopts)
|
||||
{
|
||||
char *linux_opts;
|
||||
int error;
|
||||
|
||||
error = get_linux_shareopts(shareopts, &linux_opts);
|
||||
|
||||
if (error != SA_OK)
|
||||
return (error);
|
||||
|
||||
free(linux_opts);
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
|
||||
{
|
||||
FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts;
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Clears a share's NFS options. Used by libshare to
|
||||
* clean up shares that are about to be free()'d.
|
||||
*/
|
||||
static void
|
||||
nfs_clear_shareopts(sa_share_impl_t impl_share)
|
||||
{
|
||||
FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
nfs_commit_shares(void)
|
||||
{
|
||||
char *argv[] = {
|
||||
"/usr/sbin/exportfs",
|
||||
"-ra",
|
||||
NULL
|
||||
};
|
||||
|
||||
return (libzfs_run_process(argv[0], argv, 0));
|
||||
}
|
||||
|
||||
static const sa_share_ops_t nfs_shareops = {
|
||||
.enable_share = nfs_enable_share,
|
||||
.disable_share = nfs_disable_share,
|
||||
.is_shared = nfs_is_shared,
|
||||
|
||||
.validate_shareopts = nfs_validate_shareopts,
|
||||
.update_shareopts = nfs_update_shareopts,
|
||||
.clear_shareopts = nfs_clear_shareopts,
|
||||
.commit_shares = nfs_commit_shares,
|
||||
};
|
||||
|
||||
/*
|
||||
* Initializes the NFS functionality of libshare.
|
||||
*/
|
||||
void
|
||||
libshare_nfs_init(void)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
nfs_fstype = register_fstype("nfs", &nfs_shareops);
|
||||
|
||||
if (stat(ZFS_EXPORTS_DIR, &sb) < 0 &&
|
||||
mkdir(ZFS_EXPORTS_DIR, 0755) < 0) {
|
||||
fprintf(stderr, "failed to create %s: %s\n",
|
||||
ZFS_EXPORTS_DIR, strerror(errno));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,453 @@
|
||||
/*
|
||||
* 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
|
||||
* or http://www.opensolaris.org/os/licensing.
|
||||
* 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 (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011,2012 Turbo Fredriksson <turbo@bayour.com>, based on nfs.c
|
||||
* by Gunnar Beutner
|
||||
* Copyright (c) 2019, 2020 by Delphix. All rights reserved.
|
||||
*
|
||||
* This is an addition to the zfs device driver to add, modify and remove SMB
|
||||
* shares using the 'net share' command that comes with Samba.
|
||||
*
|
||||
* TESTING
|
||||
* Make sure that samba listens to 'localhost' (127.0.0.1) and that the options
|
||||
* 'usershare max shares' and 'usershare owner only' have been reviewed/set
|
||||
* accordingly (see zfs(8) for information).
|
||||
*
|
||||
* Once configuration in samba have been done, test that this
|
||||
* works with the following three commands (in this case, my ZFS
|
||||
* filesystem is called 'share/Test1'):
|
||||
*
|
||||
* (root)# net -U root -S 127.0.0.1 usershare add Test1 /share/Test1 \
|
||||
* "Comment: /share/Test1" "Everyone:F"
|
||||
* (root)# net usershare list | grep -i test
|
||||
* (root)# net -U root -S 127.0.0.1 usershare delete Test1
|
||||
*
|
||||
* The first command will create a user share that gives everyone full access.
|
||||
* To limit the access below that, use normal UNIX commands (chmod, chown etc).
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <libzfs.h>
|
||||
#include <libshare.h>
|
||||
#include "libshare_impl.h"
|
||||
#include "smb.h"
|
||||
|
||||
static boolean_t smb_available(void);
|
||||
|
||||
static sa_fstype_t *smb_fstype;
|
||||
|
||||
smb_share_t *smb_shares;
|
||||
static int smb_disable_share(sa_share_impl_t impl_share);
|
||||
static boolean_t smb_is_share_active(sa_share_impl_t impl_share);
|
||||
|
||||
/*
|
||||
* Retrieve the list of SMB shares.
|
||||
*/
|
||||
static int
|
||||
smb_retrieve_shares(void)
|
||||
{
|
||||
int rc = SA_OK;
|
||||
char file_path[PATH_MAX], line[512], *token, *key, *value;
|
||||
char *dup_value = NULL, *path = NULL, *comment = NULL, *name = NULL;
|
||||
char *guest_ok = NULL;
|
||||
DIR *shares_dir;
|
||||
FILE *share_file_fp = NULL;
|
||||
struct dirent *directory;
|
||||
struct stat eStat;
|
||||
smb_share_t *shares, *new_shares = NULL;
|
||||
|
||||
/* opendir(), stat() */
|
||||
shares_dir = opendir(SHARE_DIR);
|
||||
if (shares_dir == NULL)
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
/* Go through the directory, looking for shares */
|
||||
while ((directory = readdir(shares_dir))) {
|
||||
if (directory->d_name[0] == '.')
|
||||
continue;
|
||||
|
||||
snprintf(file_path, sizeof (file_path),
|
||||
"%s/%s", SHARE_DIR, directory->d_name);
|
||||
|
||||
if (stat(file_path, &eStat) == -1) {
|
||||
rc = SA_SYSTEM_ERR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!S_ISREG(eStat.st_mode))
|
||||
continue;
|
||||
|
||||
if ((share_file_fp = fopen(file_path, "r")) == NULL) {
|
||||
rc = SA_SYSTEM_ERR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
name = strdup(directory->d_name);
|
||||
if (name == NULL) {
|
||||
rc = SA_NO_MEMORY;
|
||||
goto out;
|
||||
}
|
||||
|
||||
while (fgets(line, sizeof (line), share_file_fp)) {
|
||||
if (line[0] == '#')
|
||||
continue;
|
||||
|
||||
/* Trim trailing new-line character(s). */
|
||||
while (line[strlen(line) - 1] == '\r' ||
|
||||
line[strlen(line) - 1] == '\n')
|
||||
line[strlen(line) - 1] = '\0';
|
||||
|
||||
/* Split the line in two, separated by '=' */
|
||||
token = strchr(line, '=');
|
||||
if (token == NULL)
|
||||
continue;
|
||||
|
||||
key = line;
|
||||
value = token + 1;
|
||||
*token = '\0';
|
||||
|
||||
dup_value = strdup(value);
|
||||
if (dup_value == NULL) {
|
||||
rc = SA_NO_MEMORY;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (strcmp(key, "path") == 0) {
|
||||
free(path);
|
||||
path = dup_value;
|
||||
} else if (strcmp(key, "comment") == 0) {
|
||||
free(comment);
|
||||
comment = dup_value;
|
||||
} else if (strcmp(key, "guest_ok") == 0) {
|
||||
free(guest_ok);
|
||||
guest_ok = dup_value;
|
||||
} else
|
||||
free(dup_value);
|
||||
|
||||
dup_value = NULL;
|
||||
|
||||
if (path == NULL || comment == NULL || guest_ok == NULL)
|
||||
continue; /* Incomplete share definition */
|
||||
else {
|
||||
shares = (smb_share_t *)
|
||||
malloc(sizeof (smb_share_t));
|
||||
if (shares == NULL) {
|
||||
rc = SA_NO_MEMORY;
|
||||
goto out;
|
||||
}
|
||||
|
||||
(void) strlcpy(shares->name, name,
|
||||
sizeof (shares->name));
|
||||
|
||||
(void) strlcpy(shares->path, path,
|
||||
sizeof (shares->path));
|
||||
|
||||
(void) strlcpy(shares->comment, comment,
|
||||
sizeof (shares->comment));
|
||||
|
||||
shares->guest_ok = atoi(guest_ok);
|
||||
|
||||
shares->next = new_shares;
|
||||
new_shares = shares;
|
||||
|
||||
free(path);
|
||||
free(comment);
|
||||
free(guest_ok);
|
||||
|
||||
path = NULL;
|
||||
comment = NULL;
|
||||
guest_ok = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
if (share_file_fp != NULL) {
|
||||
fclose(share_file_fp);
|
||||
share_file_fp = NULL;
|
||||
}
|
||||
|
||||
free(name);
|
||||
free(path);
|
||||
free(comment);
|
||||
free(guest_ok);
|
||||
|
||||
name = NULL;
|
||||
path = NULL;
|
||||
comment = NULL;
|
||||
guest_ok = NULL;
|
||||
}
|
||||
closedir(shares_dir);
|
||||
|
||||
smb_shares = new_shares;
|
||||
|
||||
return (rc);
|
||||
}
|
||||
|
||||
/*
|
||||
* Used internally by smb_enable_share to enable sharing for a single host.
|
||||
*/
|
||||
static int
|
||||
smb_enable_share_one(const char *sharename, const char *sharepath)
|
||||
{
|
||||
char *argv[10], *pos;
|
||||
char name[SMB_NAME_MAX], comment[SMB_COMMENT_MAX];
|
||||
int rc;
|
||||
|
||||
/* Support ZFS share name regexp '[[:alnum:]_-.: ]' */
|
||||
strlcpy(name, sharename, sizeof (name));
|
||||
name [sizeof (name)-1] = '\0';
|
||||
|
||||
pos = name;
|
||||
while (*pos != '\0') {
|
||||
switch (*pos) {
|
||||
case '/':
|
||||
case '-':
|
||||
case ':':
|
||||
case ' ':
|
||||
*pos = '_';
|
||||
}
|
||||
|
||||
++pos;
|
||||
}
|
||||
|
||||
/*
|
||||
* CMD: net -S NET_CMD_ARG_HOST usershare add Test1 /share/Test1 \
|
||||
* "Comment" "Everyone:F"
|
||||
*/
|
||||
snprintf(comment, sizeof (comment), "Comment: %s", sharepath);
|
||||
|
||||
argv[0] = NET_CMD_PATH;
|
||||
argv[1] = (char *)"-S";
|
||||
argv[2] = NET_CMD_ARG_HOST;
|
||||
argv[3] = (char *)"usershare";
|
||||
argv[4] = (char *)"add";
|
||||
argv[5] = (char *)name;
|
||||
argv[6] = (char *)sharepath;
|
||||
argv[7] = (char *)comment;
|
||||
argv[8] = "Everyone:F";
|
||||
argv[9] = NULL;
|
||||
|
||||
rc = libzfs_run_process(argv[0], argv, 0);
|
||||
if (rc < 0)
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
/* Reload the share file */
|
||||
(void) smb_retrieve_shares();
|
||||
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Enables SMB sharing for the specified share.
|
||||
*/
|
||||
static int
|
||||
smb_enable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
char *shareopts;
|
||||
|
||||
if (!smb_available())
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
if (smb_is_share_active(impl_share))
|
||||
smb_disable_share(impl_share);
|
||||
|
||||
shareopts = FSINFO(impl_share, smb_fstype)->shareopts;
|
||||
if (shareopts == NULL) /* on/off */
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
if (strcmp(shareopts, "off") == 0)
|
||||
return (SA_OK);
|
||||
|
||||
/* Magic: Enable (i.e., 'create new') share */
|
||||
return (smb_enable_share_one(impl_share->sa_zfsname,
|
||||
impl_share->sa_mountpoint));
|
||||
}
|
||||
|
||||
/*
|
||||
* Used internally by smb_disable_share to disable sharing for a single host.
|
||||
*/
|
||||
static int
|
||||
smb_disable_share_one(const char *sharename)
|
||||
{
|
||||
int rc;
|
||||
char *argv[7];
|
||||
|
||||
/* CMD: net -S NET_CMD_ARG_HOST usershare delete Test1 */
|
||||
argv[0] = NET_CMD_PATH;
|
||||
argv[1] = (char *)"-S";
|
||||
argv[2] = NET_CMD_ARG_HOST;
|
||||
argv[3] = (char *)"usershare";
|
||||
argv[4] = (char *)"delete";
|
||||
argv[5] = strdup(sharename);
|
||||
argv[6] = NULL;
|
||||
|
||||
rc = libzfs_run_process(argv[0], argv, 0);
|
||||
if (rc < 0)
|
||||
return (SA_SYSTEM_ERR);
|
||||
else
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Disables SMB sharing for the specified share.
|
||||
*/
|
||||
static int
|
||||
smb_disable_share(sa_share_impl_t impl_share)
|
||||
{
|
||||
smb_share_t *shares = smb_shares;
|
||||
|
||||
if (!smb_available()) {
|
||||
/*
|
||||
* The share can't possibly be active, so nothing
|
||||
* needs to be done to disable it.
|
||||
*/
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
while (shares != NULL) {
|
||||
if (strcmp(impl_share->sa_mountpoint, shares->path) == 0)
|
||||
return (smb_disable_share_one(shares->name));
|
||||
|
||||
shares = shares->next;
|
||||
}
|
||||
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks whether the specified SMB share options are syntactically correct.
|
||||
*/
|
||||
static int
|
||||
smb_validate_shareopts(const char *shareopts)
|
||||
{
|
||||
/* TODO: Accept 'name' and sec/acl (?) */
|
||||
if ((strcmp(shareopts, "off") == 0) || (strcmp(shareopts, "on") == 0))
|
||||
return (SA_OK);
|
||||
|
||||
return (SA_SYNTAX_ERR);
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks whether a share is currently active.
|
||||
*/
|
||||
static boolean_t
|
||||
smb_is_share_active(sa_share_impl_t impl_share)
|
||||
{
|
||||
smb_share_t *iter = smb_shares;
|
||||
|
||||
if (!smb_available())
|
||||
return (B_FALSE);
|
||||
|
||||
/* Retrieve the list of (possible) active shares */
|
||||
smb_retrieve_shares();
|
||||
|
||||
while (iter != NULL) {
|
||||
if (strcmp(impl_share->sa_mountpoint, iter->path) == 0)
|
||||
return (B_TRUE);
|
||||
|
||||
iter = iter->next;
|
||||
}
|
||||
|
||||
return (B_FALSE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Called to update a share's options. A share's options might be out of
|
||||
* date if the share was loaded from disk and the "sharesmb" dataset
|
||||
* property has changed in the meantime. This function also takes care
|
||||
* of re-enabling the share if necessary.
|
||||
*/
|
||||
static int
|
||||
smb_update_shareopts(sa_share_impl_t impl_share, const char *shareopts)
|
||||
{
|
||||
if (!impl_share)
|
||||
return (SA_SYSTEM_ERR);
|
||||
|
||||
FSINFO(impl_share, smb_fstype)->shareopts = (char *)shareopts;
|
||||
return (SA_OK);
|
||||
}
|
||||
|
||||
static int
|
||||
smb_update_shares(void)
|
||||
{
|
||||
/* Not implemented */
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Clears a share's SMB options. Used by libshare to
|
||||
* clean up shares that are about to be free()'d.
|
||||
*/
|
||||
static void
|
||||
smb_clear_shareopts(sa_share_impl_t impl_share)
|
||||
{
|
||||
FSINFO(impl_share, smb_fstype)->shareopts = NULL;
|
||||
}
|
||||
|
||||
static const sa_share_ops_t smb_shareops = {
|
||||
.enable_share = smb_enable_share,
|
||||
.disable_share = smb_disable_share,
|
||||
.is_shared = smb_is_share_active,
|
||||
|
||||
.validate_shareopts = smb_validate_shareopts,
|
||||
.update_shareopts = smb_update_shareopts,
|
||||
.clear_shareopts = smb_clear_shareopts,
|
||||
.commit_shares = smb_update_shares,
|
||||
};
|
||||
|
||||
/*
|
||||
* Provides a convenient wrapper for determining SMB availability
|
||||
*/
|
||||
static boolean_t
|
||||
smb_available(void)
|
||||
{
|
||||
struct stat statbuf;
|
||||
|
||||
if (lstat(SHARE_DIR, &statbuf) != 0 ||
|
||||
!S_ISDIR(statbuf.st_mode))
|
||||
return (B_FALSE);
|
||||
|
||||
if (access(NET_CMD_PATH, F_OK) != 0)
|
||||
return (B_FALSE);
|
||||
|
||||
return (B_TRUE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initializes the SMB functionality of libshare.
|
||||
*/
|
||||
void
|
||||
libshare_smb_init(void)
|
||||
{
|
||||
smb_fstype = register_fstype("smb", &smb_shareops);
|
||||
}
|
||||
Reference in New Issue
Block a user