Skip to content

Commit

Permalink
Revert "DLPX-79316 use exportfs finer grain cache control from libsha…
Browse files Browse the repository at this point in the history
…re (openzfs#365)" (openzfs#460)

This reverts commit 66b2e0f.
  • Loading branch information
Don Brady authored Jun 2, 2022
1 parent 5996df3 commit 3ebf253
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 138 deletions.
6 changes: 0 additions & 6 deletions lib/libshare/libshare.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ libshare_init(void)
libshare_smb_init();
}

__attribute__((destructor)) static void
libshare_fini(void)
{
libshare_nfs_fini();
}

int
sa_enable_share(const char *zfsname, const char *mountpoint,
const char *shareopts, char *protocol)
Expand Down
1 change: 0 additions & 1 deletion lib/libshare/nfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"

void libshare_nfs_init(void);
void libshare_nfs_fini(void);

boolean_t nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share);
int nfs_toggle_share(const char *lockfile, const char *exports,
Expand Down
140 changes: 9 additions & 131 deletions lib/libshare/os/linux/nfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@
* 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, 2022 by Delphix. All rights reserved.
* Copyright (c) 2019, 2020 by Delphix. All rights reserved.
*/

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stddef.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/list.h>
#include <unistd.h>
#include <libzfs.h>
#include <libshare.h>
Expand All @@ -48,8 +45,6 @@
#define ZFS_EXPORTS_FILE ZFS_EXPORTS_DIR"/zfs.exports"
#define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"

#define EXPORTFS_PROG "/usr/sbin/exportfs"

static sa_fstype_t *nfs_fstype;

typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
Expand All @@ -58,16 +53,6 @@ typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
typedef int (*nfs_host_callback_t)(FILE *tmpfile, const char *sharepath,
const char *host, const char *security, const char *access, void *cookie);

static boolean_t exportfs_cache_control = B_FALSE;

typedef struct unshared_dataset_entry {
list_node_t ude_node;
char ude_path[0];
} unshared_dataset_entry_t;

static pthread_mutex_t unshared_dataset_lock;
static list_t unshared_dataset_list;

/*
* Invokes the specified callback function for each Solaris share option
* listed in the specified string.
Expand Down Expand Up @@ -154,6 +139,10 @@ foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
int cidr_len;

#ifdef DEBUG
fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
#endif

if (strcmp(opt, "sec") == 0)
udata->security = value;

Expand Down Expand Up @@ -444,37 +433,13 @@ nfs_enable_share(sa_share_impl_t impl_share)
nfs_enable_share_impl));
}

/*
* Save the mountpoint so we can flush the rpc caches after
* the shares are commited.
*/
static int
save_unshared_dataset(const char *mountpoint)
{
pthread_mutex_lock(&unshared_dataset_lock);
unshared_dataset_entry_t *ude =
malloc(sizeof (unshared_dataset_entry_t) +
strlen(mountpoint) + 1);
if (ude == NULL)
return (SA_NO_MEMORY);
strcpy(ude->ude_path, mountpoint);
list_insert_tail(&unshared_dataset_list, ude);
pthread_mutex_unlock(&unshared_dataset_lock);

return (SA_OK);
}

/*
* Disables NFS sharing for the specified share.
*/
static int
nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
{
(void) tmpfile;

if (exportfs_cache_control)
return (save_unshared_dataset(impl_share->sa_mountpoint));

(void) impl_share, (void) tmpfile;
return (SA_OK);
}

Expand Down Expand Up @@ -527,60 +492,16 @@ nfs_clear_shareopts(sa_share_impl_t impl_share)
FSINFO(impl_share, nfs_fstype)->shareopts = NULL;
}

static int
exportfs_flush_cache_entry(char *mountpoint)
{
ASSERT(exportfs_cache_control);

char *argv[] = {
EXPORTFS_PROG,
"-F",
mountpoint,
NULL
};

return (libzfs_run_process(argv[0], argv, 0));
}

static void
flush_unshared_datasets(void)
{
/*
* After calling exportfs to reexport our shares we need to
* flush the rpc cache entries for datasets that were unshared.
*/
unshared_dataset_entry_t *ude = NULL;
pthread_mutex_lock(&unshared_dataset_lock);
while ((ude = list_remove_head(&unshared_dataset_list)) != NULL) {
pthread_mutex_unlock(&unshared_dataset_lock);
exportfs_flush_cache_entry(ude->ude_path);
pthread_mutex_lock(&unshared_dataset_lock);
free(ude);
}
pthread_mutex_unlock(&unshared_dataset_lock);
}

/*
* Reexport all directories including the ones maintained in our
* '/etc/exports.d/zfs.exports' file. This will remove entries
* that have been deleted from our exports file.
*/
static int
nfs_commit_shares(void)
{
char *exportfs_args = exportfs_cache_control ? "-raN" : "-ra";
char *argv[] = {
EXPORTFS_PROG,
exportfs_args,
"/usr/sbin/exportfs",
"-ra",
NULL
};

int rc = libzfs_run_process(argv[0], argv, 0);

if (exportfs_cache_control)
flush_unshared_datasets();

return (rc);
return (libzfs_run_process(argv[0], argv, 0));
}

static const sa_share_ops_t nfs_shareops = {
Expand All @@ -594,54 +515,11 @@ static const sa_share_ops_t nfs_shareops = {
.commit_shares = nfs_commit_shares,
};

static boolean_t
cache_control_supported(void)
{
char *argv[] = {EXPORTFS_PROG, "-vh", NULL};
char **lines = NULL;
int lines_cnt = 0;
int rc;
boolean_t supported;

/*
* Expected output to confirm support for finer-grain cache flushing:
*
* $ exportfs -vh
* supports: no_cache_flush,flush_one_entry
* usage: exportfs [-adfFhiNoruvs] [host:/path]
*/
rc = libzfs_run_process_get_stdout_nopath(EXPORTFS_PROG, argv, NULL,
&lines, &lines_cnt);
supported = (rc == 0 && lines_cnt > 0 &&
strstr(lines[0], "no_cache_flush") != NULL);
libzfs_free_str_array(lines, lines_cnt);

return (supported);
}

/*
* Initializes the NFS functionality of libshare.
*/
void
libshare_nfs_init(void)
{
nfs_fstype = register_fstype("nfs", &nfs_shareops);

exportfs_cache_control = cache_control_supported();
if (exportfs_cache_control) {
pthread_mutex_init(&unshared_dataset_lock, NULL);
list_create(&unshared_dataset_list,
sizeof (unshared_dataset_entry_t),
offsetof(unshared_dataset_entry_t, ude_node));
}
}

void
libshare_nfs_fini(void)
{
if (exportfs_cache_control) {
ASSERT(list_is_empty(&unshared_dataset_list));
list_destroy(&unshared_dataset_list);
pthread_mutex_destroy(&unshared_dataset_lock);
}
}

0 comments on commit 3ebf253

Please sign in to comment.