From 471e9a108e51e92ef645223efcde59c8ab1b9db7 Mon Sep 17 00:00:00 2001 From: наб Date: Mon, 28 Feb 2022 15:46:25 +0100 Subject: Publish libshare protocols, use enum-based API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Brian Behlendorf Signed-off-by: Ahelenia Ziemiańska Closes #13165 --- lib/libshare/libshare.c | 88 ++++++++++++++++--------------------------- lib/libshare/libshare_impl.h | 8 +--- lib/libshare/os/freebsd/nfs.c | 4 +- lib/libshare/os/freebsd/smb.c | 4 +- lib/libshare/os/linux/nfs.c | 4 +- lib/libshare/os/linux/smb.c | 4 +- 6 files changed, 37 insertions(+), 75 deletions(-) (limited to 'lib/libshare') diff --git a/lib/libshare/libshare.c b/lib/libshare/libshare.c index 4a8e32d77..09529e5b4 100644 --- a/lib/libshare/libshare.c +++ b/lib/libshare/libshare.c @@ -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; - -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)); -} -__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); -} +#define VALIDATE_PROTOCOL(proto, ...) \ + if ((proto) < 0 || (proto) >= SA_PROTOCOL_COUNT) \ + return __VA_ARGS__ + +const char *const sa_protocol_names[SA_PROTOCOL_COUNT] = { + [SA_PROTOCOL_NFS] = "nfs", + [SA_PROTOCOL_SMB] = "smb", +}; + +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) +{ + /* CSTYLED */ + VALIDATE_PROTOCOL(protocol, ); + + fstypes[protocol]->commit_shares(); +} + +int +sa_validate_shareopts(const char *options, enum sa_protocol protocol) { - sa_fstype_t *fstype = find_proto(protocol); - if (!fstype) - return; + VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL); - fstype->commit_shares(); + 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)); -} diff --git a/lib/libshare/libshare_impl.h b/lib/libshare/libshare_impl.h index c79b4f532..110fe93d2 100644 --- a/lib/libshare/libshare_impl.h +++ b/lib/libshare/libshare_impl.h @@ -27,8 +27,6 @@ #ifndef _LIBSPL_LIBSHARE_IMPL_H #define _LIBSPL_LIBSHARE_IMPL_H -#include - 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 */ diff --git a/lib/libshare/os/freebsd/nfs.c b/lib/libshare/os/freebsd/nfs.c index 388bafc28..338d25fcd 100644 --- a/lib/libshare/os/freebsd/nfs.c +++ b/lib/libshare/os/freebsd/nfs.c @@ -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, diff --git a/lib/libshare/os/freebsd/smb.c b/lib/libshare/os/freebsd/smb.c index fd61d473d..0f546dc55 100644 --- a/lib/libshare/os/freebsd/smb.c +++ b/lib/libshare/os/freebsd/smb.c @@ -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, diff --git a/lib/libshare/os/linux/nfs.c b/lib/libshare/os/linux/nfs.c index f8a34924c..09c7395c6 100644 --- a/lib/libshare/os/linux/nfs.c +++ b/lib/libshare/os/linux/nfs.c @@ -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, diff --git a/lib/libshare/os/linux/smb.c b/lib/libshare/os/linux/smb.c index 91dffc9bb..1f32d0e10 100644 --- a/lib/libshare/os/linux/smb.c +++ b/lib/libshare/os/linux/smb.c @@ -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, -- cgit v1.2.3