Path: blob/main/sys/contrib/openzfs/lib/libzfs/libzfs_share.c
178703 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.24* Copyright (c) 2011 Gunnar Beutner25* Copyright (c) 2018, 2022 by Delphix. All rights reserved.26*/2728#include <stdio.h>29#include <stdlib.h>30#include <stddef.h>31#include <string.h>32#include <errno.h>33#include <libintl.h>34#include <sys/file.h>35#include <sys/types.h>36#include <sys/stat.h>37#include <unistd.h>38#include <libzfs.h>39#include "libzfs_impl.h"4041#define init_share(zfsname, path, shareopts) \42{ \43.sa_zfsname = zfsname, \44.sa_mountpoint = path, \45.sa_shareopts = shareopts, \46}4748#define VALIDATE_PROTOCOL(proto, ...) \49if ((proto) < 0 || (proto) >= SA_PROTOCOL_COUNT) \50return __VA_ARGS__5152const char *const sa_protocol_names[SA_PROTOCOL_COUNT] = {53[SA_PROTOCOL_NFS] = "nfs",54[SA_PROTOCOL_SMB] = "smb",55};5657static const sa_fstype_t *fstypes[SA_PROTOCOL_COUNT] =58{&libshare_nfs_type, &libshare_smb_type};5960int61sa_enable_share(const char *zfsname, const char *mountpoint,62const char *shareopts, enum sa_protocol protocol)63{64VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);6566const struct sa_share_impl args =67init_share(zfsname, mountpoint, shareopts);68return (fstypes[protocol]->enable_share(&args));69}7071int72sa_disable_share(const char *mountpoint, enum sa_protocol protocol)73{74VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);7576const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);77return (fstypes[protocol]->disable_share(&args));78}7980boolean_t81sa_is_shared(const char *mountpoint, enum sa_protocol protocol)82{83VALIDATE_PROTOCOL(protocol, B_FALSE);8485const struct sa_share_impl args = init_share(NULL, mountpoint, NULL);86return (fstypes[protocol]->is_shared(&args));87}8889void90sa_commit_shares(enum sa_protocol protocol)91{92/* CSTYLED */93VALIDATE_PROTOCOL(protocol, );9495fstypes[protocol]->commit_shares();96}9798void99sa_truncate_shares(enum sa_protocol protocol)100{101/* CSTYLED */102VALIDATE_PROTOCOL(protocol, );103104if (fstypes[protocol]->truncate_shares != NULL)105fstypes[protocol]->truncate_shares();106}107108int109sa_validate_shareopts(const char *options, enum sa_protocol protocol)110{111VALIDATE_PROTOCOL(protocol, SA_INVALID_PROTOCOL);112113return (fstypes[protocol]->validate_shareopts(options));114}115116/*117* sa_errorstr(err)118*119* convert an error value to an error string120*/121const char *122sa_errorstr(int err)123{124static char errstr[32];125126switch (err) {127case SA_OK:128return (dgettext(TEXT_DOMAIN, "ok"));129case SA_NO_SUCH_PATH:130return (dgettext(TEXT_DOMAIN, "path doesn't exist"));131case SA_NO_MEMORY:132return (dgettext(TEXT_DOMAIN, "no memory"));133case SA_DUPLICATE_NAME:134return (dgettext(TEXT_DOMAIN, "name in use"));135case SA_BAD_PATH:136return (dgettext(TEXT_DOMAIN, "bad path"));137case SA_NO_SUCH_GROUP:138return (dgettext(TEXT_DOMAIN, "no such group"));139case SA_CONFIG_ERR:140return (dgettext(TEXT_DOMAIN, "configuration error"));141case SA_SYSTEM_ERR:142return (dgettext(TEXT_DOMAIN, "system error"));143case SA_SYNTAX_ERR:144return (dgettext(TEXT_DOMAIN, "syntax error"));145case SA_NO_PERMISSION:146return (dgettext(TEXT_DOMAIN, "no permission"));147case SA_BUSY:148return (dgettext(TEXT_DOMAIN, "busy"));149case SA_NO_SUCH_PROP:150return (dgettext(TEXT_DOMAIN, "no such property"));151case SA_INVALID_NAME:152return (dgettext(TEXT_DOMAIN, "invalid name"));153case SA_INVALID_PROTOCOL:154return (dgettext(TEXT_DOMAIN, "invalid protocol"));155case SA_NOT_ALLOWED:156return (dgettext(TEXT_DOMAIN, "operation not allowed"));157case SA_BAD_VALUE:158return (dgettext(TEXT_DOMAIN, "bad property value"));159case SA_INVALID_SECURITY:160return (dgettext(TEXT_DOMAIN, "invalid security type"));161case SA_NO_SUCH_SECURITY:162return (dgettext(TEXT_DOMAIN, "security type not found"));163case SA_VALUE_CONFLICT:164return (dgettext(TEXT_DOMAIN, "property value conflict"));165case SA_NOT_IMPLEMENTED:166return (dgettext(TEXT_DOMAIN, "not implemented"));167case SA_INVALID_PATH:168return (dgettext(TEXT_DOMAIN, "invalid path"));169case SA_NOT_SUPPORTED:170return (dgettext(TEXT_DOMAIN, "operation not supported"));171case SA_PROP_SHARE_ONLY:172return (dgettext(TEXT_DOMAIN, "property not valid for group"));173case SA_NOT_SHARED:174return (dgettext(TEXT_DOMAIN, "not shared"));175case SA_NO_SUCH_RESOURCE:176return (dgettext(TEXT_DOMAIN, "no such resource"));177case SA_RESOURCE_REQUIRED:178return (dgettext(TEXT_DOMAIN, "resource name required"));179case SA_MULTIPLE_ERROR:180return (dgettext(TEXT_DOMAIN,181"errors from multiple protocols"));182case SA_PATH_IS_SUBDIR:183return (dgettext(TEXT_DOMAIN, "path is a subpath of share"));184case SA_PATH_IS_PARENTDIR:185return (dgettext(TEXT_DOMAIN, "path is parent of a share"));186case SA_NO_SECTION:187return (dgettext(TEXT_DOMAIN, "protocol requires a section"));188case SA_NO_PROPERTIES:189return (dgettext(TEXT_DOMAIN, "properties not found"));190case SA_NO_SUCH_SECTION:191return (dgettext(TEXT_DOMAIN, "section not found"));192case SA_PASSWORD_ENC:193return (dgettext(TEXT_DOMAIN, "passwords must be encrypted"));194case SA_SHARE_EXISTS:195return (dgettext(TEXT_DOMAIN,196"path or file is already shared"));197default:198(void) snprintf(errstr, sizeof (errstr),199dgettext(TEXT_DOMAIN, "unknown %d"), err);200return (errstr);201}202}203204205