From b4d9a82f6244df1b500a2988cf60849866fb4265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Mon, 28 Feb 2022 16:52:07 +0100 Subject: [PATCH] Replace libzfs sharing _nfs() and _smb() APIs with protocol lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the additional benefit of removing all the _all() functions and treating a NULL list as "all" ‒ the remaining all function is for all /datasets/, which is consistent with the rest of the API Reviewed-by: Brian Behlendorf Signed-off-by: Ahelenia Ziemiańska Closes #13165 --- cmd/zfs/zfs_main.c | 97 ++++++------- cmd/zpool/zpool_main.c | 4 +- include/libzfs.h | 39 ++--- lib/libzfs/libzfs.abi | 205 +++++++++++++------------- lib/libzfs/libzfs_changelist.c | 34 +++-- lib/libzfs/libzfs_dataset.c | 4 +- lib/libzfs/libzfs_impl.h | 19 --- lib/libzfs/libzfs_mount.c | 254 +++++++-------------------------- 8 files changed, 235 insertions(+), 421 deletions(-) diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c index c7c826aece..69f5bdf4d4 100644 --- a/cmd/zfs/zfs_main.c +++ b/cmd/zfs/zfs_main.c @@ -764,12 +764,12 @@ zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type) (void) fprintf(stderr, gettext("filesystem " "successfully created, but not mounted\n")); ret = 1; - } else if (zfs_share(zhp) != 0) { + } else if (zfs_share(zhp, NULL) != 0) { (void) fprintf(stderr, gettext("filesystem " "successfully created, but not shared\n")); ret = 1; } - zfs_commit_all_shares(); + zfs_commit_shares(NULL); } zfs_close(zhp); @@ -6658,7 +6658,7 @@ typedef struct share_mount_state { boolean_t sm_verbose; int sm_flags; char *sm_options; - char *sm_proto; /* only valid for OP_SHARE */ + enum sa_protocol sm_proto; /* only valid for OP_SHARE */ pthread_mutex_t sm_lock; /* protects the remaining fields */ uint_t sm_total; /* number of filesystems to process */ uint_t sm_done; /* number of filesystems processed */ @@ -6669,7 +6669,7 @@ typedef struct share_mount_state { * Share or mount a dataset. */ static int -share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, +share_mount_one(zfs_handle_t *zhp, int op, int flags, enum sa_protocol protocol, boolean_t explicit, const char *options) { char mountpoint[ZFS_MAXPROPLEN]; @@ -6787,7 +6787,7 @@ share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, return (0); if (op == OP_SHARE && !zfs_is_mounted(zhp, NULL)) { /* also purge it from existing exports */ - zfs_unshareall_bypath(zhp, mountpoint); + zfs_unshare(zhp, mountpoint, NULL); return (0); } } @@ -6845,10 +6845,11 @@ share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, * filesystem. */ switch (op) { - case OP_SHARE: - - shared_nfs = zfs_is_shared_nfs(zhp, NULL); - shared_smb = zfs_is_shared_smb(zhp, NULL); + case OP_SHARE: { + enum sa_protocol prot[] = {SA_PROTOCOL_NFS, SA_NO_PROTOCOL}; + shared_nfs = zfs_is_shared(zhp, NULL, prot); + *prot = SA_PROTOCOL_SMB; + shared_smb = zfs_is_shared(zhp, NULL, prot); if ((shared_nfs && shared_smb) || (shared_nfs && strcmp(shareopts, "on") == 0 && @@ -6868,23 +6869,11 @@ share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, zfs_mount(zhp, NULL, flags) != 0) return (1); - if (protocol == NULL) { - if (zfs_shareall(zhp) != 0) - return (1); - } else if (strcmp(protocol, "nfs") == 0) { - if (zfs_share_nfs(zhp)) - return (1); - } else if (strcmp(protocol, "smb") == 0) { - if (zfs_share_smb(zhp)) - return (1); - } else { - (void) fprintf(stderr, gettext("cannot share " - "'%s': invalid share type '%s' " - "specified\n"), - zfs_get_name(zhp), protocol); + *prot = protocol; + if (zfs_share(zhp, protocol == SA_NO_PROTOCOL ? NULL : prot)) return (1); - } + } break; case OP_MOUNT: @@ -6982,6 +6971,22 @@ append_options(char *mntopts, char *newopts) (void) strcpy(&mntopts[len], newopts); } +static enum sa_protocol +sa_protocol_decode(const char *protocol) +{ + for (enum sa_protocol i = 0; i < ARRAY_SIZE(sa_protocol_names); ++i) + if (strcmp(protocol, sa_protocol_names[i]) == 0) + return (i); + + (void) fputs(gettext("share type must be one of: "), stderr); + for (enum sa_protocol i = 0; + i < ARRAY_SIZE(sa_protocol_names); ++i) + (void) fprintf(stderr, "%s%s", + i != 0 ? ", " : "", sa_protocol_names[i]); + (void) fputc('\n', stderr); + usage(B_FALSE); +} + static int share_mount(int op, int argc, char **argv) { @@ -7040,16 +7045,10 @@ share_mount(int op, int argc, char **argv) /* check number of arguments */ if (do_all) { - char *protocol = NULL; + enum sa_protocol protocol = SA_NO_PROTOCOL; if (op == OP_SHARE && argc > 0) { - if (strcmp(argv[0], "nfs") != 0 && - strcmp(argv[0], "smb") != 0) { - (void) fprintf(stderr, gettext("share type " - "must be 'nfs' or 'smb'\n")); - usage(B_FALSE); - } - protocol = argv[0]; + protocol = sa_protocol_decode(argv[0]); argc--; argv++; } @@ -7086,7 +7085,7 @@ share_mount(int op, int argc, char **argv) zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used, share_mount_one_cb, &share_mount_state, op == OP_MOUNT && !(flags & MS_CRYPT)); - zfs_commit_all_shares(); + zfs_commit_shares(NULL); ret = share_mount_state.sm_status; @@ -7138,9 +7137,9 @@ share_mount(int op, int argc, char **argv) ZFS_TYPE_FILESYSTEM)) == NULL) { ret = 1; } else { - ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, - options); - zfs_commit_all_shares(); + ret = share_mount_one(zhp, op, flags, SA_NO_PROTOCOL, + B_TRUE, options); + zfs_commit_shares(NULL); zfs_close(zhp); } } @@ -7150,7 +7149,7 @@ share_mount(int op, int argc, char **argv) } /* - * zfs mount -a [nfs] + * zfs mount -a * zfs mount filesystem * * Mount all filesystems, or mount the given filesystem. @@ -7259,12 +7258,12 @@ unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) "'%s': legacy share\n"), path); (void) fprintf(stderr, gettext("use exportfs(8) " "or smbcontrol(1) to unshare this filesystem\n")); - } else if (!zfs_is_shared(zhp)) { + } else if (!zfs_is_shared(zhp, NULL, NULL)) { (void) fprintf(stderr, gettext("cannot unshare '%s': " "not currently shared\n"), path); } else { - ret = zfs_unshareall_bypath(zhp, path); - zfs_commit_all_shares(); + ret = zfs_unshare(zhp, path, NULL); + zfs_commit_shares(NULL); } } else { char mtpt_prop[ZFS_MAXPROPLEN]; @@ -7354,16 +7353,12 @@ unshare_unmount(int op, int argc, char **argv) unshare_unmount_node_t *node; uu_avl_index_t idx; uu_avl_walk_t *walk; - char *protocol = NULL; + enum sa_protocol *protocol = NULL, + single_protocol[] = {SA_NO_PROTOCOL, SA_NO_PROTOCOL}; if (op == OP_SHARE && argc > 0) { - if (strcmp(argv[0], "nfs") != 0 && - strcmp(argv[0], "smb") != 0) { - (void) fprintf(stderr, gettext("share type " - "must be 'nfs' or 'smb'\n")); - usage(B_FALSE); - } - protocol = argv[0]; + *single_protocol = sa_protocol_decode(argv[0]); + protocol = single_protocol; argc--; argv++; } @@ -7470,7 +7465,7 @@ unshare_unmount(int op, int argc, char **argv) uu_avl_remove(tree, node); switch (op) { case OP_SHARE: - if (zfs_unshareall_bytype(node->un_zhp, + if (zfs_unshare(node->un_zhp, node->un_mountp, protocol) != 0) ret = 1; break; @@ -7543,12 +7538,12 @@ unshare_unmount(int op, int argc, char **argv) "exports(5) or smb.conf(5) to unshare " "this filesystem\n")); ret = 1; - } else if (!zfs_is_shared(zhp)) { + } else if (!zfs_is_shared(zhp, NULL, NULL)) { (void) fprintf(stderr, gettext("cannot " "unshare '%s': not currently " "shared\n"), zfs_get_name(zhp)); ret = 1; - } else if (zfs_unshareall(zhp) != 0) { + } else if (zfs_unshareall(zhp, NULL) != 0) { ret = 1; } break; diff --git a/cmd/zpool/zpool_main.c b/cmd/zpool/zpool_main.c index aa61ff7c94..bfe798ffe4 100644 --- a/cmd/zpool/zpool_main.c +++ b/cmd/zpool/zpool_main.c @@ -1790,8 +1790,8 @@ zpool_do_create(int argc, char **argv) tname ? tname : poolname, ZFS_TYPE_FILESYSTEM); if (pool != NULL) { if (zfs_mount(pool, NULL, 0) == 0) { - ret = zfs_shareall(pool); - zfs_commit_all_shares(); + ret = zfs_share(pool, NULL); + zfs_commit_shares(NULL); } zfs_close(pool); } diff --git a/include/libzfs.h b/include/libzfs.h index c0e53b88a6..04f464d12f 100644 --- a/include/libzfs.h +++ b/include/libzfs.h @@ -35,6 +35,7 @@ #define _LIBZFS_H extern __attribute__((visibility("default"))) #include +#include #include #include #include @@ -878,37 +879,25 @@ _LIBZFS_H void zfs_adjust_mount_options(zfs_handle_t *zhp, const char *mntpoint, /* * Share support functions. + * + * enum sa_protocol * lists are terminated with SA_NO_PROTOCOL, + * NULL means "all/any known to this libzfs". */ -_LIBZFS_H boolean_t zfs_is_shared(zfs_handle_t *); -_LIBZFS_H int zfs_share(zfs_handle_t *); -_LIBZFS_H int zfs_unshare(zfs_handle_t *); +#define SA_NO_PROTOCOL -1 -/* - * Protocol-specific share support functions. - */ -_LIBZFS_H boolean_t zfs_is_shared_nfs(zfs_handle_t *, char **); -_LIBZFS_H boolean_t zfs_is_shared_smb(zfs_handle_t *, char **); -_LIBZFS_H int zfs_share_nfs(zfs_handle_t *); -_LIBZFS_H int zfs_share_smb(zfs_handle_t *); -_LIBZFS_H int zfs_shareall(zfs_handle_t *); -_LIBZFS_H int zfs_unshare_nfs(zfs_handle_t *, const char *); -_LIBZFS_H int zfs_unshare_smb(zfs_handle_t *, const char *); -_LIBZFS_H int zfs_unshareall_nfs(zfs_handle_t *); -_LIBZFS_H int zfs_unshareall_smb(zfs_handle_t *); -_LIBZFS_H int zfs_unshareall_bypath(zfs_handle_t *, const char *); -_LIBZFS_H int zfs_unshareall_bytype(zfs_handle_t *, const char *, const char *); -_LIBZFS_H int zfs_unshareall(zfs_handle_t *); -_LIBZFS_H int zfs_deleg_share_nfs(libzfs_handle_t *, char *, char *, char *, - void *, void *, int, zfs_share_op_t); -_LIBZFS_H void zfs_commit_nfs_shares(void); -_LIBZFS_H void zfs_commit_smb_shares(void); -_LIBZFS_H void zfs_commit_all_shares(void); -_LIBZFS_H void zfs_commit_shares(const char *); +_LIBZFS_H boolean_t zfs_is_shared(zfs_handle_t *zhp, char **where, + const enum sa_protocol *proto); +_LIBZFS_H int zfs_share(zfs_handle_t *zhp, const enum sa_protocol *proto); +_LIBZFS_H int zfs_unshare(zfs_handle_t *zhp, const char *mountpoint, + const enum sa_protocol *proto); +_LIBZFS_H int zfs_unshareall(zfs_handle_t *zhp, + const enum sa_protocol *proto); +_LIBZFS_H void zfs_commit_shares(const enum sa_protocol *proto); _LIBZFS_H int zfs_nicestrtonum(libzfs_handle_t *, const char *, uint64_t *); /* - * Utility functions to run an _LIBZFS_Hal process. + * Utility functions to run an external process. */ #define STDOUT_VERBOSE 0x01 #define STDERR_VERBOSE 0x02 diff --git a/lib/libzfs/libzfs.abi b/lib/libzfs/libzfs.abi index 298cfbd9fb..40bd6a5574 100644 --- a/lib/libzfs/libzfs.abi +++ b/lib/libzfs/libzfs.abi @@ -288,10 +288,7 @@ - - - @@ -339,8 +336,6 @@ - - @@ -419,9 +414,6 @@ - - - @@ -440,13 +432,7 @@ - - - - - - @@ -596,6 +582,7 @@ + @@ -849,62 +836,108 @@ + + + + - + - + - + - + + + + + + - - - - - - + + + + + + + - - - - - + + - + - - + + - - - - - - - - + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -4102,6 +4135,14 @@ + + + + + + + + @@ -4139,80 +4180,30 @@ - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - + diff --git a/lib/libzfs/libzfs_changelist.c b/lib/libzfs/libzfs_changelist.c index a70c7424b9..d31bbc4a3b 100644 --- a/lib/libzfs/libzfs_changelist.c +++ b/lib/libzfs/libzfs_changelist.c @@ -98,6 +98,7 @@ changelist_prefix(prop_changelist_t *clp) prop_changenode_t *cn; uu_avl_walk_t *walk; int ret = 0; + const enum sa_protocol smb[] = {SA_PROTOCOL_SMB, SA_NO_PROTOCOL}; boolean_t commit_smb_shares = B_FALSE; if (clp->cl_prop != ZFS_PROP_MOUNTPOINT && @@ -137,7 +138,8 @@ changelist_prefix(prop_changelist_t *clp) } break; case ZFS_PROP_SHARESMB: - (void) zfs_unshare_smb(cn->cn_handle, NULL); + (void) zfs_unshare(cn->cn_handle, NULL, + smb); commit_smb_shares = B_TRUE; break; @@ -148,7 +150,7 @@ changelist_prefix(prop_changelist_t *clp) } if (commit_smb_shares) - zfs_commit_smb_shares(); + zfs_commit_shares(smb); uu_avl_walk_end(walk); if (ret == -1) @@ -257,25 +259,33 @@ changelist_postfix(prop_changelist_t *clp) * if the filesystem is currently shared, so that we can * adopt any new options. */ + const enum sa_protocol nfs[] = + {SA_PROTOCOL_NFS, SA_NO_PROTOCOL}; if (sharenfs && mounted) { - errors += zfs_share_nfs(cn->cn_handle); + errors += zfs_share(cn->cn_handle, nfs); commit_nfs_shares = B_TRUE; } else if (cn->cn_shared || clp->cl_waslegacy) { - errors += zfs_unshare_nfs(cn->cn_handle, NULL); + errors += zfs_unshare(cn->cn_handle, NULL, nfs); commit_nfs_shares = B_TRUE; } + const enum sa_protocol smb[] = + {SA_PROTOCOL_SMB, SA_NO_PROTOCOL}; if (sharesmb && mounted) { - errors += zfs_share_smb(cn->cn_handle); + errors += zfs_share(cn->cn_handle, smb); commit_smb_shares = B_TRUE; } else if (cn->cn_shared || clp->cl_waslegacy) { - errors += zfs_unshare_smb(cn->cn_handle, NULL); + errors += zfs_unshare(cn->cn_handle, NULL, smb); commit_smb_shares = B_TRUE; } } + + enum sa_protocol proto[SA_PROTOCOL_COUNT + 1], *p = proto; if (commit_nfs_shares) - zfs_commit_nfs_shares(); + *p++ = SA_PROTOCOL_NFS; if (commit_smb_shares) - zfs_commit_smb_shares(); + *p++ = SA_PROTOCOL_SMB; + *p++ = SA_NO_PROTOCOL; + zfs_commit_shares(proto); uu_avl_walk_end(walk); return (errors ? -1 : 0); @@ -359,7 +369,7 @@ changelist_unshare(prop_changelist_t *clp, const enum sa_protocol *proto) return (-1); while ((cn = uu_avl_walk_next(walk)) != NULL) { - if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0) + if (zfs_unshare(cn->cn_handle, NULL, proto) != 0) ret = -1; } @@ -452,7 +462,7 @@ changelist_add_mounted(zfs_handle_t *zhp, void *data) cn->cn_handle = zhp; cn->cn_mounted = zfs_is_mounted(zhp, NULL); ASSERT3U(cn->cn_mounted, ==, B_TRUE); - cn->cn_shared = zfs_is_shared(zhp); + cn->cn_shared = zfs_is_shared(zhp, NULL, NULL); cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); cn->cn_needpost = B_TRUE; @@ -522,7 +532,7 @@ change_one(zfs_handle_t *zhp, void *data) cn->cn_handle = zhp; cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) || zfs_is_mounted(zhp, NULL); - cn->cn_shared = zfs_is_shared(zhp); + cn->cn_shared = zfs_is_shared(zhp, NULL, NULL); cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); cn->cn_needpost = B_TRUE; @@ -738,7 +748,7 @@ changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags, cn->cn_handle = temp; cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) || zfs_is_mounted(temp, NULL); - cn->cn_shared = zfs_is_shared(temp); + cn->cn_shared = zfs_is_shared(temp, NULL, NULL); cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); cn->cn_needpost = B_TRUE; diff --git a/lib/libzfs/libzfs_dataset.c b/lib/libzfs/libzfs_dataset.c index e5c1bd62d5..50053e35f0 100644 --- a/lib/libzfs/libzfs_dataset.c +++ b/lib/libzfs/libzfs_dataset.c @@ -3556,14 +3556,14 @@ create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) goto ancestorerr; } - if (zfs_share(h) != 0) { + if (zfs_share(h, NULL) != 0) { opname = dgettext(TEXT_DOMAIN, "share"); goto ancestorerr; } zfs_close(h); } - zfs_commit_all_shares(); + zfs_commit_shares(NULL); return (0); diff --git a/lib/libzfs/libzfs_impl.h b/lib/libzfs/libzfs_impl.h index bf1a8359ce..926e14a371 100644 --- a/lib/libzfs/libzfs_impl.h +++ b/lib/libzfs/libzfs_impl.h @@ -105,15 +105,6 @@ struct zpool_handle { diskaddr_t zpool_start_block; }; -/* - * Bitmask of shared types: - * 0 means none, otherwise | (1 << (enum sa_protocol + 1)). - */ -typedef unsigned zfs_share_type_t; -#define SHARED_NOT_SHARED 0 - -#define SA_NO_PROTOCOL -1 - typedef int (*zfs_uri_handler_fn_t)(struct libzfs_handle *, const char *, const char *, zfs_keyformat_t, boolean_t, uint8_t **, size_t *); @@ -233,16 +224,6 @@ typedef struct differ_info { extern int do_mount(zfs_handle_t *zhp, const char *mntpt, char *opts, int flags); extern int do_unmount(zfs_handle_t *zhp, const char *mntpt, int flags); -extern int zfs_mount_delegation_check(void); -extern int zfs_share_proto(zfs_handle_t *zhp, const enum sa_protocol *proto); -extern int zfs_unshare_proto(zfs_handle_t *, const char *, - const enum sa_protocol *); -extern int unshare_one(libzfs_handle_t *hdl, const char *name, - const char *mountpoint, enum sa_protocol proto); -extern boolean_t zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, - zprop_source_t *source, int flags); -extern zfs_share_type_t is_shared(const char *mountpoint, - enum sa_protocol proto); extern int libzfs_load_module(void); extern int zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg); diff --git a/lib/libzfs/libzfs_mount.c b/lib/libzfs/libzfs_mount.c index ea7e97c293..77837a3b0f 100644 --- a/lib/libzfs/libzfs_mount.c +++ b/lib/libzfs/libzfs_mount.c @@ -41,23 +41,13 @@ * zfs_unmount() * zfs_unmountall() * - * This file also contains the functions used to manage sharing filesystems via - * NFS and iSCSI: + * This file also contains the functions used to manage sharing filesystems: * * zfs_is_shared() * zfs_share() * zfs_unshare() - * - * zfs_is_shared_nfs() - * zfs_is_shared_smb() - * zfs_share_proto() - * zfs_shareall(); - * zfs_unshare_nfs() - * zfs_unshare_smb() - * zfs_unshareall_nfs() - * zfs_unshareall_smb() * zfs_unshareall() - * zfs_unshareall_bypath() + * zfs_commit_shares() * * The following functions are available for pool consumers, and will * mount/unmount and share/unshare all datasets within pool: @@ -95,8 +85,6 @@ static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */ static void zfs_mount_task(void *); -static zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **, - enum sa_protocol); static const proto_table_t proto_table[SA_PROTOCOL_COUNT] = { [SA_PROTOCOL_NFS] = @@ -249,7 +237,7 @@ zfs_is_mountable_internal(zfs_handle_t *zhp) * Returns true if the given dataset is mountable, false otherwise. Returns the * mountpoint in 'buf'. */ -boolean_t +static boolean_t zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, zprop_source_t *source, int flags) { @@ -623,16 +611,16 @@ zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) /* * Unshare and unmount the filesystem */ - if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) { + if (zfs_unshare(zhp, mntpt, share_all_proto) != 0) { free(mntpt); return (-1); } - zfs_commit_all_shares(); + zfs_commit_shares(NULL); if (unmount_one(zhp, mntpt, flags) != 0) { free(mntpt); - (void) zfs_shareall(zhp); - zfs_commit_all_shares(); + (void) zfs_share(zhp, NULL); + zfs_commit_shares(NULL); return (-1); } @@ -691,23 +679,10 @@ zfs_unmountall(zfs_handle_t *zhp, int flags) return (ret); } -boolean_t -zfs_is_shared(zfs_handle_t *zhp) -{ - if (ZFS_IS_VOLUME(zhp)) - return (B_FALSE); - - for (enum sa_protocol i = 0; i < SA_PROTOCOL_COUNT; ++i) - if (zfs_is_shared_proto(zhp, NULL, i)) - return (B_TRUE); - - return (B_FALSE); -} - /* * Unshare a filesystem by mountpoint. */ -int +static int unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, enum sa_protocol proto) { @@ -720,26 +695,13 @@ unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, return (0); } -/* - * Query libshare for the given mountpoint and protocol, returning - * a zfs_share_type_t value. - */ -zfs_share_type_t -is_shared(const char *mountpoint, enum sa_protocol proto) -{ - if (sa_is_shared(mountpoint, proto)) - return (1 << (proto + 1)); - else - return (SHARED_NOT_SHARED); -} - /* * 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, const enum sa_protocol *proto) +zfs_share(zfs_handle_t *zhp, const enum sa_protocol *proto) { char mountpoint[ZFS_MAXPROPLEN]; char shareopts[ZFS_MAXPROPLEN]; @@ -748,6 +710,9 @@ zfs_share_proto(zfs_handle_t *zhp, const enum sa_protocol *proto) zprop_source_t sourcetype; int err = 0; + if (proto == NULL) + proto = share_all_proto; + if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0)) return (0); @@ -783,138 +748,74 @@ zfs_share_proto(zfs_handle_t *zhp, const enum sa_protocol *proto) return (0); } -int -zfs_share(zfs_handle_t *zhp) -{ - assert(!ZFS_IS_VOLUME(zhp)); - return (zfs_share_proto(zhp, share_all_proto)); -} - -int -zfs_unshare(zfs_handle_t *zhp) -{ - assert(!ZFS_IS_VOLUME(zhp)); - return (zfs_unshareall(zhp)); -} - /* * Check to see if the filesystem is currently shared. */ -static zfs_share_type_t -zfs_is_shared_proto(zfs_handle_t *zhp, char **where, enum sa_protocol proto) +boolean_t +zfs_is_shared(zfs_handle_t *zhp, char **where, + const enum sa_protocol *proto) { char *mountpoint; - zfs_share_type_t rc; + if (proto == NULL) + proto = share_all_proto; + + if (ZFS_IS_VOLUME(zhp)) + return (B_FALSE); if (!zfs_is_mounted(zhp, &mountpoint)) - return (SHARED_NOT_SHARED); + return (B_FALSE); - if ((rc = is_shared(mountpoint, proto)) - != SHARED_NOT_SHARED) { - if (where != NULL) - *where = mountpoint; - else - free(mountpoint); - return (rc); - } else { - free(mountpoint); - return (SHARED_NOT_SHARED); - } -} + for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p) + if (sa_is_shared(mountpoint, *p)) { + if (where != NULL) + *where = mountpoint; + else + free(mountpoint); + return (B_TRUE); + } -boolean_t -zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) -{ - return (zfs_is_shared_proto(zhp, where, - SA_PROTOCOL_NFS) != SHARED_NOT_SHARED); -} - -boolean_t -zfs_is_shared_smb(zfs_handle_t *zhp, char **where) -{ - return (zfs_is_shared_proto(zhp, where, - SA_PROTOCOL_SMB) != SHARED_NOT_SHARED); + free(mountpoint); + return (B_FALSE); } void -zfs_commit_nfs_shares(void) -{ - sa_commit_shares(SA_PROTOCOL_NFS); -} - -void -zfs_commit_smb_shares(void) -{ - sa_commit_shares(SA_PROTOCOL_SMB); -} - -void -zfs_commit_all_shares(void) -{ - for (enum sa_protocol i = 0; i < SA_PROTOCOL_COUNT; ++i) - sa_commit_shares(i); -} - -void -zfs_commit_shares(const char *proto) +zfs_commit_shares(const enum sa_protocol *proto) { if (proto == NULL) - zfs_commit_all_shares(); - else - for (enum sa_protocol i = 0; i < ARRAY_SIZE(sa_protocol_names); - ++i) - if (strcmp(proto, sa_protocol_names[i]) == 0) { - sa_commit_shares(i); - return; - } -} + proto = share_all_proto; -int -zfs_share_nfs(zfs_handle_t *zhp) -{ - const enum sa_protocol nfs_only[] = {SA_PROTOCOL_NFS, SA_NO_PROTOCOL}; - return (zfs_share_proto(zhp, nfs_only)); -} - -int -zfs_share_smb(zfs_handle_t *zhp) -{ - const enum sa_protocol smb_only[] = {SA_PROTOCOL_SMB, SA_NO_PROTOCOL}; - return (zfs_share_proto(zhp, smb_only)); -} - -int -zfs_shareall(zfs_handle_t *zhp) -{ - return (zfs_share_proto(zhp, share_all_proto)); + for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p) + sa_commit_shares(*p); } /* * Unshare the given filesystem. */ int -zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, +zfs_unshare(zfs_handle_t *zhp, const char *mountpoint, const enum sa_protocol *proto) { libzfs_handle_t *hdl = zhp->zfs_hdl; struct mnttab entry; char *mntpt = NULL; + if (proto == NULL) + proto = share_all_proto; + /* check to see if need to unmount the filesystem */ if (mountpoint != NULL) mntpt = zfs_strdup(hdl, mountpoint); if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) { - const enum sa_protocol *curr_proto; if (mountpoint == NULL) mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); - for (curr_proto = proto; *curr_proto != SA_NO_PROTOCOL; - curr_proto++) { + for (const enum sa_protocol *curr_proto = proto; + *curr_proto != SA_NO_PROTOCOL; curr_proto++) { - if (is_shared(mntpt, *curr_proto)) { + if (sa_is_shared(mntpt, *curr_proto)) { if (unshare_one(hdl, zhp->zfs_name, mntpt, *curr_proto) != 0) { if (mntpt != NULL) @@ -930,29 +831,18 @@ zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, return (0); } -int -zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) -{ - const enum sa_protocol nfs_only[] = {SA_PROTOCOL_NFS, SA_NO_PROTOCOL}; - return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); -} - -int -zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) -{ - const enum sa_protocol smb_only[] = {SA_PROTOCOL_SMB, SA_NO_PROTOCOL}; - return (zfs_unshare_proto(zhp, mountpoint, smb_only)); -} - /* * Same as zfs_unmountall(), but for NFS and SMB unshares. */ -static int -zfs_unshareall_proto(zfs_handle_t *zhp, const enum sa_protocol *proto) +int +zfs_unshareall(zfs_handle_t *zhp, const enum sa_protocol *proto) { prop_changelist_t *clp; int ret; + if (proto == NULL) + proto = share_all_proto; + clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0); if (clp == NULL) return (-1); @@ -963,48 +853,6 @@ zfs_unshareall_proto(zfs_handle_t *zhp, const enum sa_protocol *proto) return (ret); } -int -zfs_unshareall_nfs(zfs_handle_t *zhp) -{ - const enum sa_protocol nfs_only[] = {SA_PROTOCOL_NFS, SA_NO_PROTOCOL}; - return (zfs_unshareall_proto(zhp, nfs_only)); -} - -int -zfs_unshareall_smb(zfs_handle_t *zhp) -{ - const enum sa_protocol smb_only[] = {SA_PROTOCOL_SMB, SA_NO_PROTOCOL}; - return (zfs_unshareall_proto(zhp, smb_only)); -} - -int -zfs_unshareall(zfs_handle_t *zhp) -{ - return (zfs_unshareall_proto(zhp, share_all_proto)); -} - -int -zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) -{ - return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); -} - -int -zfs_unshareall_bytype(zfs_handle_t *zhp, const char *mountpoint, - const char *proto) -{ - if (proto == NULL) - return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); - - for (enum sa_protocol i = 0; i < ARRAY_SIZE(sa_protocol_names); ++i) - if (strcmp(proto, sa_protocol_names[i]) == 0) { - enum sa_protocol protocols[] = {i, SA_NO_PROTOCOL}; - return (zfs_unshare_proto(zhp, mountpoint, protocols)); - } - - return (1); -} - /* * Remove the mountpoint associated with the current dataset, if necessary. * We only remove the underlying directory if: @@ -1295,7 +1143,7 @@ zfs_share_one(zfs_handle_t *zhp, void *arg) mount_state_t *ms = arg; int ret = 0; - if (zfs_share(zhp) != 0) + if (zfs_share(zhp, NULL) != 0) ret = ms->ms_mntstatus = -1; return (ret); } @@ -1468,7 +1316,7 @@ zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) if (ms.ms_mntstatus != 0) ret = ms.ms_mntstatus; else - zfs_commit_all_shares(); + zfs_commit_shares(NULL); out: for (int i = 0; i < cb.cb_used; i++) @@ -1578,13 +1426,13 @@ zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) */ for (i = 0; i < used; i++) { for (enum sa_protocol i = 0; i < SA_PROTOCOL_COUNT; ++i) { - if (is_shared(sets[i].mountpoint, i) && + if (sa_is_shared(sets[i].mountpoint, i) && unshare_one(hdl, sets[i].mountpoint, sets[i].mountpoint, i) != 0) goto out; } } - zfs_commit_all_shares(); + zfs_commit_shares(NULL); /* * Now unmount everything, removing the underlying directories as