Publish libshare protocols, use enum-based API

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #13165
This commit is contained in:
наб
2022-02-28 15:46:25 +01:00
committed by Brian Behlendorf
parent 21d976a621
commit 471e9a108e
11 changed files with 132 additions and 202 deletions
+30 -54
View File
@@ -45,77 +45,63 @@
.sa_mountpoint = path, \
.sa_shareopts = shareopts, \
}
#define find_proto(pcol) \
/* CSTYLED */ \
({ \
sa_fstype_t prot = { \
.protocol = pcol, \
}; \
avl_find(&fstypes, &prot, NULL); \
})
static avl_tree_t fstypes;
#define VALIDATE_PROTOCOL(proto, ...) \
if ((proto) < 0 || (proto) >= SA_PROTOCOL_COUNT) \
return __VA_ARGS__
static int
fstypes_compar(const void *lhs, const void *rhs)
{
const sa_fstype_t *l = lhs, *r = rhs;
int cmp = strcmp(l->protocol, r->protocol);
return ((0 < cmp) - (cmp < 0));
}
const char *const sa_protocol_names[SA_PROTOCOL_COUNT] = {
[SA_PROTOCOL_NFS] = "nfs",
[SA_PROTOCOL_SMB] = "smb",
};
__attribute__((constructor)) static void
libshare_init(void)
{
avl_create(&fstypes, fstypes_compar,
sizeof (sa_fstype_t), offsetof(sa_fstype_t, node));
avl_add(&fstypes, &libshare_nfs_type);
avl_add(&fstypes, &libshare_smb_type);
}
static const sa_fstype_t *fstypes[SA_PROTOCOL_COUNT] =
{&libshare_nfs_type, &libshare_smb_type};
int
sa_enable_share(const char *zfsname, const char *mountpoint,
const char *shareopts, const char *protocol)
const char *shareopts, enum sa_protocol protocol)
{
sa_fstype_t *fstype = find_proto(protocol);
if (!fstype)
return (SA_INVALID_PROTOCOL);
VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);
const struct sa_share_impl args =
init_share(zfsname, mountpoint, shareopts);
return (fstype->enable_share(&args));
return (fstypes[protocol]->enable_share(&args));
}
int
sa_disable_share(const char *mountpoint, const char *protocol)
sa_disable_share(const char *mountpoint, enum sa_protocol protocol)
{
sa_fstype_t *fstype = find_proto(protocol);
if (!fstype)
return (SA_INVALID_PROTOCOL);
VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);
const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);
return (fstype->disable_share(&args));
return (fstypes[protocol]->disable_share(&args));
}
boolean_t
sa_is_shared(const char *mountpoint, const char *protocol)
sa_is_shared(const char *mountpoint, enum sa_protocol protocol)
{
sa_fstype_t *fstype = find_proto(protocol);
if (!fstype)
return (B_FALSE);
VALIDATE_PROTOCOL(protocol, B_FALSE);
const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);
return (fstype->is_shared(&args));
return (fstypes[protocol]->is_shared(&args));
}
void
sa_commit_shares(const char *protocol)
sa_commit_shares(enum sa_protocol protocol)
{
sa_fstype_t *fstype = find_proto(protocol);
if (!fstype)
return;
/* CSTYLED */
VALIDATE_PROTOCOL(protocol, );
fstype->commit_shares();
fstypes[protocol]->commit_shares();
}
int
sa_validate_shareopts(const char *options, enum sa_protocol protocol)
{
VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);
return (fstypes[protocol]->validate_shareopts(options));
}
/*
@@ -205,13 +191,3 @@ sa_errorstr(int err)
return (errstr);
}
}
int
sa_validate_shareopts(const char *options, const char *protocol)
{
sa_fstype_t *fstype = find_proto(protocol);
if (!fstype)
return (SA_INVALID_PROTOCOL);
return (fstype->validate_shareopts(options));
}
+1 -7
View File
@@ -27,8 +27,6 @@
#ifndef _LIBSPL_LIBSHARE_IMPL_H
#define _LIBSPL_LIBSHARE_IMPL_H
#include <sys/avl.h>
typedef const struct sa_share_impl {
const char *sa_zfsname;
const char *sa_mountpoint;
@@ -36,17 +34,13 @@ typedef const struct sa_share_impl {
} *sa_share_impl_t;
typedef struct {
const char *protocol;
int (*const enable_share)(sa_share_impl_t share);
int (*const disable_share)(sa_share_impl_t share);
boolean_t (*const is_shared)(sa_share_impl_t share);
int (*const validate_shareopts)(const char *shareopts);
int (*const commit_shares)(void);
avl_node_t node;
} sa_fstype_t;
extern sa_fstype_t libshare_nfs_type, libshare_smb_type;
extern const sa_fstype_t libshare_nfs_type, libshare_smb_type;
#endif /* _LIBSPL_LIBSHARE_IMPL_H */
+1 -3
View File
@@ -186,9 +186,7 @@ start:
return (SA_OK);
}
sa_fstype_t libshare_nfs_type = {
.protocol = "nfs",
const sa_fstype_t libshare_nfs_type = {
.enable_share = nfs_enable_share,
.disable_share = nfs_disable_share,
.is_shared = nfs_is_shared,
+1 -3
View File
@@ -76,9 +76,7 @@ smb_update_shares(void)
return (0);
}
sa_fstype_t libshare_smb_type = {
.protocol = "smb",
const sa_fstype_t libshare_smb_type = {
.enable_share = smb_enable_share,
.disable_share = smb_disable_share,
.is_shared = smb_is_share_active,
+1 -3
View File
@@ -481,9 +481,7 @@ nfs_commit_shares(void)
return (libzfs_run_process(argv[0], argv, 0));
}
sa_fstype_t libshare_nfs_type = {
.protocol = "nfs",
const sa_fstype_t libshare_nfs_type = {
.enable_share = nfs_enable_share,
.disable_share = nfs_disable_share,
.is_shared = nfs_is_shared,
+1 -3
View File
@@ -363,9 +363,7 @@ smb_update_shares(void)
return (0);
}
sa_fstype_t libshare_smb_type = {
.protocol = "smb",
const sa_fstype_t libshare_smb_type = {
.enable_share = smb_enable_share,
.disable_share = smb_disable_share,
.is_shared = smb_is_share_active,