13034Sdougm /*
23034Sdougm * CDDL HEADER START
33034Sdougm *
43034Sdougm * The contents of this file are subject to the terms of the
53034Sdougm * Common Development and Distribution License (the "License").
63034Sdougm * You may not use this file except in compliance with the License.
73034Sdougm *
83034Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93034Sdougm * or http://www.opensolaris.org/os/licensing.
103034Sdougm * See the License for the specific language governing permissions
113034Sdougm * and limitations under the License.
123034Sdougm *
133034Sdougm * When distributing Covered Code, include this CDDL HEADER in each
143034Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153034Sdougm * If applicable, add the following below this CDDL HEADER, with the
163034Sdougm * fields enclosed by brackets "[]" replaced with your own identifying
173034Sdougm * information: Portions Copyright [yyyy] [name of copyright owner]
183034Sdougm *
193034Sdougm * CDDL HEADER END
203034Sdougm */
213034Sdougm
223034Sdougm /*
23*12508Samw@Sun.COM * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
243034Sdougm */
253034Sdougm
264592Smarks #include <stdio.h>
273034Sdougm #include <libzfs.h>
283034Sdougm #include <string.h>
294345Sdougm #include <strings.h>
3010761SWilliam.Krier@Sun.COM #include <errno.h>
313034Sdougm #include <libshare.h>
323034Sdougm #include "libshare_impl.h"
333218Sdougm #include <libintl.h>
344592Smarks #include <sys/mnttab.h>
354592Smarks #include <sys/mntent.h>
363034Sdougm
375331Samw extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
383034Sdougm extern sa_group_t _sa_create_zfs_group(sa_group_t, char *);
393034Sdougm extern char *sa_fstype(char *);
403034Sdougm extern void set_node_attr(void *, char *, char *);
413034Sdougm extern int sa_is_share(void *);
425951Sdougm extern void sa_update_sharetab_ts(sa_handle_t);
433218Sdougm
443034Sdougm /*
453218Sdougm * File system specific code for ZFS. The original code was stolen
463218Sdougm * from the "zfs" command and modified to better suit this library's
473218Sdougm * usage.
483218Sdougm */
493218Sdougm
503218Sdougm typedef struct get_all_cbdata {
513218Sdougm zfs_handle_t **cb_handles;
523218Sdougm size_t cb_alloc;
533218Sdougm size_t cb_used;
544345Sdougm uint_t cb_types;
553218Sdougm } get_all_cbdata_t;
563218Sdougm
573218Sdougm /*
583910Sdougm * sa_zfs_init(impl_handle)
593218Sdougm *
603910Sdougm * Initialize an access handle into libzfs. The handle needs to stay
613910Sdougm * around until sa_zfs_fini() in order to maintain the cache of
623910Sdougm * mounts.
633218Sdougm */
643218Sdougm
654327Sdougm int
sa_zfs_init(sa_handle_impl_t impl_handle)663910Sdougm sa_zfs_init(sa_handle_impl_t impl_handle)
673218Sdougm {
683910Sdougm impl_handle->zfs_libhandle = libzfs_init();
694327Sdougm if (impl_handle->zfs_libhandle != NULL) {
704327Sdougm libzfs_print_on_error(impl_handle->zfs_libhandle, B_TRUE);
714327Sdougm return (B_TRUE);
724327Sdougm }
734327Sdougm return (B_FALSE);
743218Sdougm }
753218Sdougm
763218Sdougm /*
773910Sdougm * sa_zfs_fini(impl_handle)
783218Sdougm *
793218Sdougm * cleanup data structures and the libzfs handle used for accessing
803218Sdougm * zfs file share info.
813218Sdougm */
823218Sdougm
833218Sdougm void
sa_zfs_fini(sa_handle_impl_t impl_handle)843910Sdougm sa_zfs_fini(sa_handle_impl_t impl_handle)
853218Sdougm {
863910Sdougm if (impl_handle->zfs_libhandle != NULL) {
874327Sdougm if (impl_handle->zfs_list != NULL) {
884345Sdougm zfs_handle_t **zhp = impl_handle->zfs_list;
894345Sdougm size_t i;
904345Sdougm
914327Sdougm /*
924345Sdougm * Contents of zfs_list need to be freed so we
934345Sdougm * don't lose ZFS handles.
944327Sdougm */
954345Sdougm for (i = 0; i < impl_handle->zfs_list_count; i++) {
964345Sdougm zfs_close(zhp[i]);
974345Sdougm }
984327Sdougm free(impl_handle->zfs_list);
994327Sdougm impl_handle->zfs_list = NULL;
1004327Sdougm impl_handle->zfs_list_count = 0;
1014327Sdougm }
1024345Sdougm
1034345Sdougm libzfs_fini(impl_handle->zfs_libhandle);
1044345Sdougm impl_handle->zfs_libhandle = NULL;
1053218Sdougm }
1063218Sdougm }
1073218Sdougm
1083218Sdougm /*
1093218Sdougm * get_one_filesystem(zfs_handle_t, data)
1103218Sdougm *
1115331Samw * an iterator function called while iterating through the ZFS
1123218Sdougm * root. It accumulates into an array of file system handles that can
1133218Sdougm * be used to derive info about those file systems.
1144345Sdougm *
1154345Sdougm * Note that as this function is called, we close all zhp handles that
1164345Sdougm * are not going to be places into the cp_handles list. We don't want
1174345Sdougm * to close the ones we are keeping, but all others would be leaked if
1184345Sdougm * not closed here.
1193034Sdougm */
1203034Sdougm
1213218Sdougm static int
get_one_filesystem(zfs_handle_t * zhp,void * data)1223218Sdougm get_one_filesystem(zfs_handle_t *zhp, void *data)
1233218Sdougm {
1243218Sdougm get_all_cbdata_t *cbp = data;
1254345Sdougm zfs_type_t type = zfs_get_type(zhp);
1263218Sdougm
1273218Sdougm /*
1284345Sdougm * Interate over any nested datasets.
1293218Sdougm */
1304345Sdougm if (type == ZFS_TYPE_FILESYSTEM &&
1314345Sdougm zfs_iter_filesystems(zhp, get_one_filesystem, data) != 0) {
1324345Sdougm zfs_close(zhp);
1334345Sdougm return (1);
1344345Sdougm }
1354345Sdougm
1364345Sdougm /*
1374345Sdougm * Skip any datasets whose type does not match.
1384345Sdougm */
1394345Sdougm if ((type & cbp->cb_types) == 0) {
1403218Sdougm zfs_close(zhp);
1413218Sdougm return (0);
1423218Sdougm }
1433218Sdougm
1443218Sdougm if (cbp->cb_alloc == cbp->cb_used) {
1453218Sdougm zfs_handle_t **handles;
1463218Sdougm
1473218Sdougm if (cbp->cb_alloc == 0)
1483218Sdougm cbp->cb_alloc = 64;
1493218Sdougm else
1503218Sdougm cbp->cb_alloc *= 2;
1513218Sdougm
1524345Sdougm handles = (zfs_handle_t **)calloc(1,
1534345Sdougm cbp->cb_alloc * sizeof (void *));
1544345Sdougm
1553218Sdougm if (handles == NULL) {
1564345Sdougm zfs_close(zhp);
1574327Sdougm return (0);
1583218Sdougm }
1593218Sdougm if (cbp->cb_handles) {
1604345Sdougm bcopy(cbp->cb_handles, handles,
1613218Sdougm cbp->cb_used * sizeof (void *));
1623218Sdougm free(cbp->cb_handles);
1633218Sdougm }
1643218Sdougm
1653218Sdougm cbp->cb_handles = handles;
1663218Sdougm }
1673218Sdougm
1683218Sdougm cbp->cb_handles[cbp->cb_used++] = zhp;
1693218Sdougm
1704345Sdougm return (0);
1713218Sdougm }
1723218Sdougm
1733218Sdougm /*
1743218Sdougm * get_all_filesystems(zfs_handle_t ***fslist, size_t *count)
1753218Sdougm *
1763218Sdougm * iterate through all ZFS file systems starting at the root. Returns
1773218Sdougm * a count and an array of handle pointers. Allocating is only done
1783218Sdougm * once. The caller does not need to free since it will be done at
1793218Sdougm * sa_zfs_fini() time.
1803218Sdougm */
1813218Sdougm
1823218Sdougm static void
get_all_filesystems(sa_handle_impl_t impl_handle,zfs_handle_t *** fslist,size_t * count)1833910Sdougm get_all_filesystems(sa_handle_impl_t impl_handle,
1843910Sdougm zfs_handle_t ***fslist, size_t *count)
1853218Sdougm {
1863218Sdougm get_all_cbdata_t cb = { 0 };
1874345Sdougm cb.cb_types = ZFS_TYPE_FILESYSTEM;
1883218Sdougm
1893910Sdougm if (impl_handle->zfs_list != NULL) {
1904327Sdougm *fslist = impl_handle->zfs_list;
1914327Sdougm *count = impl_handle->zfs_list_count;
1924327Sdougm return;
1933218Sdougm }
1943218Sdougm
1953910Sdougm (void) zfs_iter_root(impl_handle->zfs_libhandle,
1964327Sdougm get_one_filesystem, &cb);
1973218Sdougm
1983910Sdougm impl_handle->zfs_list = *fslist = cb.cb_handles;
1993910Sdougm impl_handle->zfs_list_count = *count = cb.cb_used;
2003218Sdougm }
2013218Sdougm
2023218Sdougm /*
2033218Sdougm * mountpoint_compare(a, b)
2043218Sdougm *
2053218Sdougm * compares the mountpoint on two zfs file systems handles.
2063218Sdougm * returns values following strcmp() model.
2073218Sdougm */
2083218Sdougm
2093218Sdougm static int
mountpoint_compare(const void * a,const void * b)2103218Sdougm mountpoint_compare(const void *a, const void *b)
2113218Sdougm {
2123218Sdougm zfs_handle_t **za = (zfs_handle_t **)a;
2133218Sdougm zfs_handle_t **zb = (zfs_handle_t **)b;
2143218Sdougm char mounta[MAXPATHLEN];
2153218Sdougm char mountb[MAXPATHLEN];
2163218Sdougm
2173218Sdougm verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
2183218Sdougm sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
2193218Sdougm verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
2203218Sdougm sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
2213218Sdougm
2223218Sdougm return (strcmp(mounta, mountb));
2233218Sdougm }
2243218Sdougm
2253034Sdougm /*
2268845Samw@Sun.COM * return legacy mountpoint. Caller provides space for mountpoint and
2278845Samw@Sun.COM * dataset.
2284592Smarks */
2294592Smarks int
get_legacy_mountpoint(char * path,char * dataset,size_t dlen,char * mountpoint,size_t mlen)2308845Samw@Sun.COM get_legacy_mountpoint(char *path, char *dataset, size_t dlen,
2318845Samw@Sun.COM char *mountpoint, size_t mlen)
2324592Smarks {
2334592Smarks FILE *fp;
2344592Smarks struct mnttab entry;
2354592Smarks
2364592Smarks if ((fp = fopen(MNTTAB, "r")) == NULL) {
2374592Smarks return (1);
2384592Smarks }
2394592Smarks
2404592Smarks while (getmntent(fp, &entry) == 0) {
2414592Smarks
2424592Smarks if (entry.mnt_fstype == NULL ||
2434592Smarks strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
2444592Smarks continue;
2454592Smarks
2464592Smarks if (strcmp(entry.mnt_mountp, path) == 0) {
2478845Samw@Sun.COM if (mlen > 0)
2488845Samw@Sun.COM (void) strlcpy(mountpoint, entry.mnt_mountp,
2498845Samw@Sun.COM mlen);
2508845Samw@Sun.COM if (dlen > 0)
2518845Samw@Sun.COM (void) strlcpy(dataset, entry.mnt_special,
2528845Samw@Sun.COM dlen);
2538845Samw@Sun.COM break;
2544592Smarks }
2554592Smarks }
2564592Smarks (void) fclose(fp);
2574592Smarks return (1);
2584592Smarks }
2594592Smarks
2604592Smarks /*
2613910Sdougm * get_zfs_dataset(impl_handle, path)
2623034Sdougm *
2633034Sdougm * get the name of the ZFS dataset the path is equivalent to. The
2643034Sdougm * dataset name is used for get/set of ZFS properties since libzfs
2653034Sdougm * requires a dataset to do a zfs_open().
2663034Sdougm */
2673034Sdougm
2683034Sdougm static char *
get_zfs_dataset(sa_handle_impl_t impl_handle,char * path,boolean_t search_mnttab)2694592Smarks get_zfs_dataset(sa_handle_impl_t impl_handle, char *path,
2704592Smarks boolean_t search_mnttab)
2713034Sdougm {
2723218Sdougm size_t i, count = 0;
2733034Sdougm char *dataset = NULL;
2743218Sdougm zfs_handle_t **zlist;
2753218Sdougm char mountpoint[ZFS_MAXPROPLEN];
2764180Sdougm char canmount[ZFS_MAXPROPLEN];
2773218Sdougm
2783910Sdougm get_all_filesystems(impl_handle, &zlist, &count);
2793218Sdougm qsort(zlist, count, sizeof (void *), mountpoint_compare);
2803218Sdougm for (i = 0; i < count; i++) {
2814327Sdougm /* must have a mountpoint */
2824327Sdougm if (zfs_prop_get(zlist[i], ZFS_PROP_MOUNTPOINT, mountpoint,
2834327Sdougm sizeof (mountpoint), NULL, NULL, 0, B_FALSE) != 0) {
2844327Sdougm /* no mountpoint */
2854327Sdougm continue;
2864327Sdougm }
2873034Sdougm
2884327Sdougm /* mountpoint must be a path */
2894327Sdougm if (strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) == 0 ||
2904592Smarks strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
2914592Smarks /*
2928845Samw@Sun.COM * Search mmttab for mountpoint and get dataset.
2934592Smarks */
2944592Smarks
2954592Smarks if (search_mnttab == B_TRUE &&
2964592Smarks get_legacy_mountpoint(path, mountpoint,
2978845Samw@Sun.COM sizeof (mountpoint), NULL, 0) == 0) {
2984592Smarks dataset = mountpoint;
2994592Smarks break;
3004592Smarks }
3014327Sdougm continue;
3024592Smarks }
3033218Sdougm
3044327Sdougm /* canmount must be set */
3054327Sdougm canmount[0] = '\0';
3064543Smarks if (zfs_prop_get(zlist[i], ZFS_PROP_CANMOUNT, canmount,
3074180Sdougm sizeof (canmount), NULL, NULL, 0, B_FALSE) != 0 ||
3084327Sdougm strcmp(canmount, "off") == 0)
3094327Sdougm continue;
3103218Sdougm
3114327Sdougm /*
3124327Sdougm * have a mountable handle but want to skip those marked none
3134327Sdougm * and legacy
3144327Sdougm */
3154327Sdougm if (strcmp(mountpoint, path) == 0) {
3164327Sdougm dataset = (char *)zfs_get_name(zlist[i]);
3174327Sdougm break;
3184327Sdougm }
3193218Sdougm
3203034Sdougm }
3213218Sdougm
3224327Sdougm if (dataset != NULL)
3234327Sdougm dataset = strdup(dataset);
3244327Sdougm
3253034Sdougm return (dataset);
3263034Sdougm }
3273034Sdougm
3283034Sdougm /*
3293034Sdougm * get_zfs_property(dataset, property)
3303034Sdougm *
3313034Sdougm * Get the file system property specified from the ZFS dataset.
3323034Sdougm */
3333034Sdougm
3343034Sdougm static char *
get_zfs_property(char * dataset,zfs_prop_t property)3353034Sdougm get_zfs_property(char *dataset, zfs_prop_t property)
3363034Sdougm {
3373034Sdougm zfs_handle_t *handle = NULL;
3383034Sdougm char shareopts[ZFS_MAXPROPLEN];
3393034Sdougm libzfs_handle_t *libhandle;
3403034Sdougm
3413034Sdougm libhandle = libzfs_init();
3423034Sdougm if (libhandle != NULL) {
3434327Sdougm handle = zfs_open(libhandle, dataset, ZFS_TYPE_FILESYSTEM);
3444327Sdougm if (handle != NULL) {
3454327Sdougm if (zfs_prop_get(handle, property, shareopts,
3464327Sdougm sizeof (shareopts), NULL, NULL, 0,
3474327Sdougm B_FALSE) == 0) {
3484327Sdougm zfs_close(handle);
3494327Sdougm libzfs_fini(libhandle);
3504327Sdougm return (strdup(shareopts));
3514327Sdougm }
3524327Sdougm zfs_close(handle);
3533034Sdougm }
3544327Sdougm libzfs_fini(libhandle);
3553034Sdougm }
3563034Sdougm return (NULL);
3573034Sdougm }
3583034Sdougm
3593034Sdougm /*
3603910Sdougm * sa_zfs_is_shared(handle, path)
3613034Sdougm *
3623034Sdougm * Check to see if the ZFS path provided has the sharenfs option set
3633034Sdougm * or not.
3643034Sdougm */
3653034Sdougm
3663034Sdougm int
sa_zfs_is_shared(sa_handle_t sahandle,char * path)3673910Sdougm sa_zfs_is_shared(sa_handle_t sahandle, char *path)
3683034Sdougm {
3693034Sdougm int ret = 0;
3703034Sdougm char *dataset;
3713034Sdougm zfs_handle_t *handle = NULL;
3723034Sdougm char shareopts[ZFS_MAXPROPLEN];
3733034Sdougm libzfs_handle_t *libhandle;
3743034Sdougm
3754592Smarks dataset = get_zfs_dataset((sa_handle_t)sahandle, path, B_FALSE);
3763034Sdougm if (dataset != NULL) {
3774327Sdougm libhandle = libzfs_init();
3784327Sdougm if (libhandle != NULL) {
3794327Sdougm handle = zfs_open(libhandle, dataset,
3804327Sdougm ZFS_TYPE_FILESYSTEM);
3814327Sdougm if (handle != NULL) {
3824327Sdougm if (zfs_prop_get(handle, ZFS_PROP_SHARENFS,
3834327Sdougm shareopts, sizeof (shareopts), NULL, NULL,
3844327Sdougm 0, B_FALSE) == 0 &&
3854327Sdougm strcmp(shareopts, "off") != 0) {
3864327Sdougm ret = 1; /* it is shared */
3874327Sdougm }
3884327Sdougm zfs_close(handle);
3894327Sdougm }
3904327Sdougm libzfs_fini(libhandle);
3913034Sdougm }
3924327Sdougm free(dataset);
3933034Sdougm }
3943034Sdougm return (ret);
3953034Sdougm }
3963034Sdougm
3973034Sdougm /*
3985331Samw * find_or_create_group(handle, groupname, proto, *err)
3993034Sdougm *
4003034Sdougm * While walking the ZFS tree, we need to add shares to a defined
4013034Sdougm * group. If the group doesn't exist, create it first, making sure it
4023034Sdougm * is marked as a ZFS group.
4033034Sdougm *
4043108Sdougm * Note that all ZFS shares are in a subgroup of the top level group
4053108Sdougm * called "zfs".
4063034Sdougm */
4073034Sdougm
4083034Sdougm static sa_group_t
find_or_create_group(sa_handle_t handle,char * groupname,char * proto,int * err)4093910Sdougm find_or_create_group(sa_handle_t handle, char *groupname, char *proto, int *err)
4103034Sdougm {
4113034Sdougm sa_group_t group;
4123034Sdougm sa_optionset_t optionset;
4133034Sdougm int ret = SA_OK;
4143034Sdougm
4153034Sdougm /*
4163034Sdougm * we check to see if the "zfs" group exists. Since this
4173034Sdougm * should be the top level group, we don't want the
4183034Sdougm * parent. This is to make sure the zfs group has been created
4193034Sdougm * and to created if it hasn't been.
4203034Sdougm */
4213910Sdougm group = sa_get_group(handle, groupname);
4223034Sdougm if (group == NULL) {
4234327Sdougm group = sa_create_group(handle, groupname, &ret);
4243108Sdougm
4254327Sdougm /* make sure this is flagged as a ZFS group */
4264327Sdougm if (group != NULL)
4274327Sdougm ret = sa_set_group_attr(group, "zfs", "true");
4283034Sdougm }
4293034Sdougm if (group != NULL) {
4304327Sdougm if (proto != NULL) {
4314327Sdougm optionset = sa_get_optionset(group, proto);
4325331Samw if (optionset == NULL)
4334327Sdougm optionset = sa_create_optionset(group, proto);
4343034Sdougm }
4353034Sdougm }
4363034Sdougm if (err != NULL)
4374327Sdougm *err = ret;
4383034Sdougm return (group);
4393034Sdougm }
4403034Sdougm
4413034Sdougm /*
4423108Sdougm * find_or_create_zfs_subgroup(groupname, optstring, *err)
4433108Sdougm *
4443108Sdougm * ZFS shares will be in a subgroup of the "zfs" master group. This
4453108Sdougm * function looks to see if the groupname exists and returns it if it
4463108Sdougm * does or else creates a new one with the specified name and returns
4473108Sdougm * that. The "zfs" group will exist before we get here, but we make
4483108Sdougm * sure just in case.
4493108Sdougm *
4503108Sdougm * err must be a valid pointer.
4513108Sdougm */
4523108Sdougm
4533108Sdougm static sa_group_t
find_or_create_zfs_subgroup(sa_handle_t handle,char * groupname,char * proto,char * optstring,int * err)4545331Samw find_or_create_zfs_subgroup(sa_handle_t handle, char *groupname, char *proto,
4555331Samw char *optstring, int *err)
4563108Sdougm {
4573108Sdougm sa_group_t group = NULL;
4583108Sdougm sa_group_t zfs;
4593108Sdougm char *name;
4603108Sdougm char *options;
4613108Sdougm
4623108Sdougm /* start with the top-level "zfs" group */
4633910Sdougm zfs = sa_get_group(handle, "zfs");
4643108Sdougm *err = SA_OK;
4653108Sdougm if (zfs != NULL) {
4664327Sdougm for (group = sa_get_sub_group(zfs); group != NULL;
4674327Sdougm group = sa_get_next_group(group)) {
4684327Sdougm name = sa_get_group_attr(group, "name");
4694327Sdougm if (name != NULL && strcmp(name, groupname) == 0) {
4704327Sdougm /* have the group so break out of here */
4714327Sdougm sa_free_attr_string(name);
4724327Sdougm break;
4734327Sdougm }
4744327Sdougm if (name != NULL)
4754327Sdougm sa_free_attr_string(name);
4763108Sdougm }
4773108Sdougm
4784327Sdougm if (group == NULL) {
4794327Sdougm /*
4806149Sdougm * Need to create the sub-group since it doesn't exist
4814327Sdougm */
4824327Sdougm group = _sa_create_zfs_group(zfs, groupname);
4836149Sdougm if (group == NULL) {
4846149Sdougm *err = SA_NO_MEMORY;
4856149Sdougm return (NULL);
4864327Sdougm }
4876149Sdougm set_node_attr(group, "zfs", "true");
4886149Sdougm }
4896149Sdougm if (strcmp(optstring, "on") == 0)
4906149Sdougm optstring = "rw";
4916149Sdougm options = strdup(optstring);
4926149Sdougm if (options != NULL) {
4936149Sdougm *err = sa_parse_legacy_options(group, options,
4946149Sdougm proto);
4956149Sdougm /* If no optionset, add one. */
4966149Sdougm if (sa_get_optionset(group, proto) == NULL)
4976149Sdougm (void) sa_create_optionset(group, proto);
4986149Sdougm free(options);
4996149Sdougm } else {
5006149Sdougm *err = SA_NO_MEMORY;
5013108Sdougm }
5023108Sdougm }
5033108Sdougm return (group);
5043108Sdougm }
5053108Sdougm
5063108Sdougm /*
5075331Samw * zfs_construct_resource(share, name, base, dataset)
5085331Samw *
5095331Samw * Add a resource to the share using name as a template. If name ==
5105331Samw * NULL, then construct a name based on the dataset value.
5115331Samw * name.
5125331Samw */
5135331Samw static void
zfs_construct_resource(sa_share_t share,char * dataset)5145331Samw zfs_construct_resource(sa_share_t share, char *dataset)
5155331Samw {
5165331Samw char buff[SA_MAX_RESOURCE_NAME + 1];
5175331Samw int ret = SA_OK;
5185331Samw
5195331Samw (void) snprintf(buff, SA_MAX_RESOURCE_NAME, "%s", dataset);
5205331Samw sa_fix_resource_name(buff);
5215331Samw (void) sa_add_resource(share, buff, SA_SHARE_TRANSIENT, &ret);
5225331Samw }
5235331Samw
5245331Samw /*
5254327Sdougm * zfs_inherited(handle, source, sourcestr)
5264327Sdougm *
5275331Samw * handle case of inherited share{nfs,smb}. Pulled out of sa_get_zfs_shares
5284327Sdougm * for readability.
5294327Sdougm */
5304327Sdougm static int
zfs_inherited(sa_handle_t handle,sa_share_t share,char * sourcestr,char * shareopts,char * mountpoint,char * proto,char * dataset)5314327Sdougm zfs_inherited(sa_handle_t handle, sa_share_t share, char *sourcestr,
5325331Samw char *shareopts, char *mountpoint, char *proto, char *dataset)
5334327Sdougm {
5344327Sdougm int doshopt = 0;
5354327Sdougm int err = SA_OK;
5364327Sdougm sa_group_t group;
5375331Samw sa_resource_t resource;
5385331Samw uint64_t features;
5394327Sdougm
5404327Sdougm /*
5414327Sdougm * Need to find the "real" parent sub-group. It may not be
5424327Sdougm * mounted, but it was identified in the "sourcestr"
5434327Sdougm * variable. The real parent not mounted can occur if
5444327Sdougm * "canmount=off and sharenfs=on".
5454327Sdougm */
5465331Samw group = find_or_create_zfs_subgroup(handle, sourcestr, proto,
5475331Samw shareopts, &doshopt);
5484327Sdougm if (group != NULL) {
5495331Samw /*
5505331Samw * We may need the first share for resource
5515331Samw * prototype. We only care about it if it has a
5525331Samw * resource that sets a prefix value.
5535331Samw */
5545331Samw if (share == NULL)
5555331Samw share = _sa_add_share(group, mountpoint,
5565331Samw SA_SHARE_TRANSIENT, &err,
5575331Samw (uint64_t)SA_FEATURE_NONE);
5584327Sdougm /*
5594327Sdougm * some options may only be on shares. If the opt
5604327Sdougm * string contains one of those, we put it just on the
5614327Sdougm * share.
5624327Sdougm */
5634327Sdougm if (share != NULL && doshopt == SA_PROP_SHARE_ONLY) {
5644327Sdougm char *options;
5654327Sdougm options = strdup(shareopts);
5664327Sdougm if (options != NULL) {
5675331Samw set_node_attr(share, "dataset", dataset);
5684327Sdougm err = sa_parse_legacy_options(share, options,
5695331Samw proto);
5705331Samw set_node_attr(share, "dataset", NULL);
5714327Sdougm free(options);
5724327Sdougm }
5735331Samw if (sa_get_optionset(group, proto) == NULL)
5745331Samw (void) sa_create_optionset(group, proto);
5755331Samw }
5765331Samw features = sa_proto_get_featureset(proto);
5775331Samw if (share != NULL && features & SA_FEATURE_RESOURCE) {
5785331Samw /*
5795331Samw * We have a share and the protocol requires
5805331Samw * that at least one resource exist (probably
5815331Samw * SMB). We need to make sure that there is at
5825331Samw * least one.
5835331Samw */
5845331Samw resource = sa_get_share_resource(share, NULL);
5855331Samw if (resource == NULL) {
5865331Samw zfs_construct_resource(share, dataset);
5875331Samw }
5884327Sdougm }
5894327Sdougm } else {
5904327Sdougm err = SA_NO_MEMORY;
5914327Sdougm }
5924327Sdougm return (err);
5934327Sdougm }
5944327Sdougm
5954327Sdougm /*
5965454Sdougm * zfs_notinherited(group, share, mountpoint, shareopts, proto, dataset,
5975454Sdougm * grouperr)
5984327Sdougm *
5994327Sdougm * handle case where this is the top of a sub-group in ZFS. Pulled out
6005454Sdougm * of sa_get_zfs_shares for readability. We need the grouperr from the
6015454Sdougm * creation of the subgroup to know whether to add the public
6025454Sdougm * property, etc. to the specific share.
6034327Sdougm */
6044327Sdougm static int
zfs_notinherited(sa_group_t group,sa_share_t share,char * mountpoint,char * shareopts,char * proto,char * dataset,int grouperr)6055331Samw zfs_notinherited(sa_group_t group, sa_share_t share, char *mountpoint,
6065454Sdougm char *shareopts, char *proto, char *dataset, int grouperr)
6074327Sdougm {
6084327Sdougm int err = SA_OK;
6095331Samw sa_resource_t resource;
6105331Samw uint64_t features;
6114327Sdougm
6124327Sdougm set_node_attr(group, "zfs", "true");
6135331Samw if (share == NULL)
6145331Samw share = _sa_add_share(group, mountpoint, SA_SHARE_TRANSIENT,
6155331Samw &err, (uint64_t)SA_FEATURE_NONE);
6166149Sdougm
6176149Sdougm if (err != SA_OK)
6186149Sdougm return (err);
6196149Sdougm
6206149Sdougm if (strcmp(shareopts, "on") == 0)
6216149Sdougm shareopts = "";
6226149Sdougm if (shareopts != NULL) {
6236149Sdougm char *options;
6246149Sdougm if (grouperr == SA_PROP_SHARE_ONLY) {
6256149Sdougm /*
6266149Sdougm * Some properties may only be on shares, but
6276149Sdougm * due to the ZFS sub-groups being artificial,
6286149Sdougm * we sometimes get this and have to deal with
6296149Sdougm * it. We do it by attempting to put it on the
6306149Sdougm * share.
6316149Sdougm */
6326149Sdougm options = strdup(shareopts);
6336149Sdougm if (options != NULL) {
6346149Sdougm err = sa_parse_legacy_options(share,
6356149Sdougm options, proto);
6366149Sdougm free(options);
6375331Samw }
6385331Samw }
6396149Sdougm /* Unmark the share's changed state */
6406149Sdougm set_node_attr(share, "changed", NULL);
6416149Sdougm }
6426149Sdougm features = sa_proto_get_featureset(proto);
6436149Sdougm if (share != NULL && features & SA_FEATURE_RESOURCE) {
6446149Sdougm /*
6456149Sdougm * We have a share and the protocol requires that at
6466149Sdougm * least one resource exist (probably SMB). We need to
6476149Sdougm * make sure that there is at least one.
6486149Sdougm */
6496149Sdougm resource = sa_get_share_resource(share, NULL);
6506149Sdougm if (resource == NULL) {
6516149Sdougm zfs_construct_resource(share, dataset);
6524327Sdougm }
6534327Sdougm }
6544327Sdougm return (err);
6554327Sdougm }
6564327Sdougm
6574327Sdougm /*
6584327Sdougm * zfs_grp_error(err)
6594327Sdougm *
6604327Sdougm * Print group create error, but only once. If err is 0 do the
6614327Sdougm * print else don't.
6624327Sdougm */
6634327Sdougm
6644327Sdougm static void
zfs_grp_error(int err)6654327Sdougm zfs_grp_error(int err)
6664327Sdougm {
6674327Sdougm if (err == 0) {
6684327Sdougm /* only print error once */
6694327Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
6704327Sdougm "Cannot create ZFS subgroup during initialization:"
6714327Sdougm " %s\n"), sa_errorstr(SA_SYSTEM_ERR));
6724327Sdougm }
6734327Sdougm }
6744327Sdougm
6754327Sdougm /*
6765331Samw * zfs_process_share(handle, share, mountpoint, proto, source,
6775331Samw * shareopts, sourcestr)
6785331Samw *
6795951Sdougm * Creates the subgroup, if necessary and adds shares, resources
6805331Samw * and properties.
6815331Samw */
6825951Sdougm int
sa_zfs_process_share(sa_handle_t handle,sa_group_t group,sa_share_t share,char * mountpoint,char * proto,zprop_source_t source,char * shareopts,char * sourcestr,char * dataset)6835951Sdougm sa_zfs_process_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
6845331Samw char *mountpoint, char *proto, zprop_source_t source, char *shareopts,
6855331Samw char *sourcestr, char *dataset)
6865331Samw {
6875331Samw int err = SA_OK;
6885331Samw
6895331Samw if (source & ZPROP_SRC_INHERITED) {
6905331Samw err = zfs_inherited(handle, share, sourcestr, shareopts,
6915331Samw mountpoint, proto, dataset);
6925331Samw } else {
6935331Samw group = find_or_create_zfs_subgroup(handle, dataset, proto,
6945331Samw shareopts, &err);
6955331Samw if (group == NULL) {
6965454Sdougm static boolean_t reported_error = B_FALSE;
6975331Samw /*
6985454Sdougm * There is a problem, but we can't do
6995331Samw * anything about it at this point so we issue
7005454Sdougm * a warning and move on.
7015331Samw */
7025454Sdougm zfs_grp_error(reported_error);
7035454Sdougm reported_error = B_TRUE;
7045331Samw }
7055331Samw set_node_attr(group, "zfs", "true");
7065331Samw /*
7075331Samw * Add share with local opts via zfs_notinherited.
7085331Samw */
7095331Samw err = zfs_notinherited(group, share, mountpoint, shareopts,
7105454Sdougm proto, dataset, err);
7115331Samw }
7125331Samw return (err);
7135331Samw }
7145331Samw
7155331Samw /*
7163910Sdougm * sa_get_zfs_shares(handle, groupname)
7173034Sdougm *
7183034Sdougm * Walk the mnttab for all zfs mounts and determine which are
7193034Sdougm * shared. Find or create the appropriate group/sub-group to contain
7203034Sdougm * the shares.
7213034Sdougm *
7223034Sdougm * All shares are in a sub-group that will hold the properties. This
7233034Sdougm * allows representing the inherited property model.
7246149Sdougm *
7256149Sdougm * One area of complication is if "sharenfs" is set at one level of
7266149Sdougm * the directory tree and "sharesmb" is set at a different level, the
7276149Sdougm * a sub-group must be formed at the lower level for both
7286149Sdougm * protocols. That is the nature of the problem in CR 6667349.
7293034Sdougm */
7303034Sdougm
7313034Sdougm int
sa_get_zfs_shares(sa_handle_t handle,char * groupname)7323910Sdougm sa_get_zfs_shares(sa_handle_t handle, char *groupname)
7333034Sdougm {
7343034Sdougm sa_group_t zfsgroup;
7356149Sdougm boolean_t nfs;
7366149Sdougm boolean_t nfs_inherited;
7376149Sdougm boolean_t smb;
7386149Sdougm boolean_t smb_inherited;
7393218Sdougm zfs_handle_t **zlist;
7406149Sdougm char nfsshareopts[ZFS_MAXPROPLEN];
7416149Sdougm char smbshareopts[ZFS_MAXPROPLEN];
7423034Sdougm sa_share_t share;
7435094Slling zprop_source_t source;
7446149Sdougm char nfssourcestr[ZFS_MAXPROPLEN];
7456149Sdougm char smbsourcestr[ZFS_MAXPROPLEN];
7463218Sdougm char mountpoint[ZFS_MAXPROPLEN];
7473218Sdougm size_t count = 0, i;
7483910Sdougm libzfs_handle_t *zfs_libhandle;
7496149Sdougm int err = SA_OK;
7503034Sdougm
7513034Sdougm /*
7523910Sdougm * If we can't access libzfs, don't bother doing anything.
7533034Sdougm */
7543910Sdougm zfs_libhandle = ((sa_handle_impl_t)handle)->zfs_libhandle;
7553218Sdougm if (zfs_libhandle == NULL)
7564327Sdougm return (SA_SYSTEM_ERR);
7573034Sdougm
7585331Samw zfsgroup = find_or_create_group(handle, groupname, NULL, &err);
7596149Sdougm /* Not an error, this could be a legacy condition */
7604524Sdougm if (zfsgroup == NULL)
7616149Sdougm return (SA_OK);
7623218Sdougm
7634524Sdougm /*
7644524Sdougm * need to walk the mounted ZFS pools and datasets to
7654524Sdougm * find shares that are possible.
7664524Sdougm */
7674524Sdougm get_all_filesystems((sa_handle_impl_t)handle, &zlist, &count);
7684524Sdougm qsort(zlist, count, sizeof (void *), mountpoint_compare);
7694524Sdougm
7704524Sdougm for (i = 0; i < count; i++) {
7714524Sdougm char *dataset;
7723218Sdougm
7735094Slling source = ZPROP_SRC_ALL;
7744524Sdougm /* If no mountpoint, skip. */
7754524Sdougm if (zfs_prop_get(zlist[i], ZFS_PROP_MOUNTPOINT,
7764524Sdougm mountpoint, sizeof (mountpoint), NULL, NULL, 0,
7774524Sdougm B_FALSE) != 0)
7784524Sdougm continue;
7794327Sdougm
7804524Sdougm /*
7814524Sdougm * zfs_get_name value must not be freed. It is just a
7824524Sdougm * pointer to a value in the handle.
7834524Sdougm */
7844524Sdougm if ((dataset = (char *)zfs_get_name(zlist[i])) == NULL)
7854524Sdougm continue;
7864327Sdougm
7874524Sdougm /*
7884524Sdougm * only deal with "mounted" file systems since
7894524Sdougm * unmounted file systems can't actually be shared.
7904524Sdougm */
7914327Sdougm
7924524Sdougm if (!zfs_is_mounted(zlist[i], NULL))
7934524Sdougm continue;
7944327Sdougm
7956149Sdougm nfs = nfs_inherited = B_FALSE;
7966149Sdougm
7976149Sdougm if (zfs_prop_get(zlist[i], ZFS_PROP_SHARENFS, nfsshareopts,
7986149Sdougm sizeof (nfsshareopts), &source, nfssourcestr,
7996149Sdougm ZFS_MAXPROPLEN, B_FALSE) == 0 &&
8006149Sdougm strcmp(nfsshareopts, "off") != 0) {
8016149Sdougm if (source & ZPROP_SRC_INHERITED)
8026149Sdougm nfs_inherited = B_TRUE;
8036149Sdougm else
8046149Sdougm nfs = B_TRUE;
8056149Sdougm }
8066149Sdougm
8076149Sdougm smb = smb_inherited = B_FALSE;
8086149Sdougm if (zfs_prop_get(zlist[i], ZFS_PROP_SHARESMB, smbshareopts,
8096149Sdougm sizeof (smbshareopts), &source, smbsourcestr,
8104524Sdougm ZFS_MAXPROPLEN, B_FALSE) == 0 &&
8116149Sdougm strcmp(smbshareopts, "off") != 0) {
8126149Sdougm if (source & ZPROP_SRC_INHERITED)
8136149Sdougm smb_inherited = B_TRUE;
8146149Sdougm else
8156149Sdougm smb = B_TRUE;
8166149Sdougm }
8176149Sdougm
8186149Sdougm /*
8196149Sdougm * If the mountpoint is already shared, it must be a
8206149Sdougm * non-ZFS share. We want to remove the share from its
8216149Sdougm * parent group and reshare it under ZFS.
8226149Sdougm */
8236149Sdougm share = sa_find_share(handle, mountpoint);
8246149Sdougm if (share != NULL &&
8256149Sdougm (nfs || smb || nfs_inherited || smb_inherited)) {
8266149Sdougm err = sa_remove_share(share);
8276149Sdougm share = NULL;
8286149Sdougm }
8296149Sdougm
8306149Sdougm /*
8316149Sdougm * At this point, we have the information needed to
8326149Sdougm * determine what to do with the share.
8336149Sdougm *
8346149Sdougm * If smb or nfs is set, we have a new sub-group.
8356149Sdougm * If smb_inherit and/or nfs_inherit is set, then
8366149Sdougm * place on an existing sub-group. If both are set,
8376149Sdougm * the existing sub-group is the closest up the tree.
8386149Sdougm */
8396149Sdougm if (nfs || smb) {
8406149Sdougm /*
8416149Sdougm * Non-inherited is the straightforward
8426149Sdougm * case. sa_zfs_process_share handles it
8436149Sdougm * directly. Make sure that if the "other"
8446149Sdougm * protocol is inherited, that we treat it as
8456149Sdougm * non-inherited as well.
8466149Sdougm */
8476149Sdougm if (nfs || nfs_inherited) {
8486149Sdougm err = sa_zfs_process_share(handle, zfsgroup,
8496149Sdougm share, mountpoint, "nfs",
8506149Sdougm 0, nfsshareopts,
8516149Sdougm nfssourcestr, dataset);
8526149Sdougm share = sa_find_share(handle, mountpoint);
8535331Samw }
8546149Sdougm if (smb || smb_inherited) {
8556149Sdougm err = sa_zfs_process_share(handle, zfsgroup,
8566149Sdougm share, mountpoint, "smb",
8576149Sdougm 0, smbshareopts,
8586149Sdougm smbsourcestr, dataset);
8595331Samw }
8606149Sdougm } else if (nfs_inherited || smb_inherited) {
8616149Sdougm char *grpdataset;
8626149Sdougm /*
8636149Sdougm * If we only have inherited groups, it is
8646149Sdougm * important to find the closer of the two if
8656149Sdougm * the protocols are set at different
8666149Sdougm * levels. The closest sub-group is the one we
8676149Sdougm * want to work with.
8686149Sdougm */
8696149Sdougm if (nfs_inherited && smb_inherited) {
8706149Sdougm if (strcmp(nfssourcestr, smbsourcestr) <= 0)
8716149Sdougm grpdataset = nfssourcestr;
8726149Sdougm else
8736149Sdougm grpdataset = smbsourcestr;
8746149Sdougm } else if (nfs_inherited) {
8756149Sdougm grpdataset = nfssourcestr;
8766149Sdougm } else if (smb_inherited) {
8776149Sdougm grpdataset = smbsourcestr;
8786149Sdougm }
8796149Sdougm if (nfs_inherited) {
8806149Sdougm err = sa_zfs_process_share(handle, zfsgroup,
8816149Sdougm share, mountpoint, "nfs",
8826149Sdougm ZPROP_SRC_INHERITED, nfsshareopts,
8836149Sdougm grpdataset, dataset);
8846149Sdougm share = sa_find_share(handle, mountpoint);
8856149Sdougm }
8866149Sdougm if (smb_inherited) {
8876149Sdougm err = sa_zfs_process_share(handle, zfsgroup,
8886149Sdougm share, mountpoint, "smb",
8896149Sdougm ZPROP_SRC_INHERITED, smbshareopts,
8906149Sdougm grpdataset, dataset);
8913034Sdougm }
8923034Sdougm }
8933034Sdougm }
8943218Sdougm /*
8953218Sdougm * Don't need to free the "zlist" variable since it is only a
8963218Sdougm * pointer to a cached value that will be freed when
8973218Sdougm * sa_fini() is called.
8983218Sdougm */
8996149Sdougm return (err);
9003034Sdougm }
9013034Sdougm
9023034Sdougm #define COMMAND "/usr/sbin/zfs"
9033034Sdougm
9043034Sdougm /*
9053034Sdougm * sa_zfs_set_sharenfs(group, path, on)
9063034Sdougm *
9073034Sdougm * Update the "sharenfs" property on the path. If on is true, then set
9083034Sdougm * to the properties on the group or "on" if no properties are
9093034Sdougm * defined. Set to "off" if on is false.
9103034Sdougm */
9113034Sdougm
9123034Sdougm int
sa_zfs_set_sharenfs(sa_group_t group,char * path,int on)9133034Sdougm sa_zfs_set_sharenfs(sa_group_t group, char *path, int on)
9143034Sdougm {
9153034Sdougm int ret = SA_NOT_IMPLEMENTED;
9163034Sdougm char *command;
9173034Sdougm
9183034Sdougm command = malloc(ZFS_MAXPROPLEN * 2);
9193034Sdougm if (command != NULL) {
9204327Sdougm char *opts = NULL;
9214327Sdougm char *dataset = NULL;
9224327Sdougm FILE *pfile;
9234327Sdougm sa_handle_impl_t impl_handle;
9244327Sdougm /* for now, NFS is always available for "zfs" */
9254327Sdougm if (on) {
9264327Sdougm opts = sa_proto_legacy_format("nfs", group, 1);
9274327Sdougm if (opts != NULL && strlen(opts) == 0) {
9284327Sdougm free(opts);
9294327Sdougm opts = strdup("on");
9304327Sdougm }
9313034Sdougm }
9323910Sdougm
9334327Sdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
9344327Sdougm assert(impl_handle != NULL);
9354327Sdougm if (impl_handle != NULL)
9364592Smarks dataset = get_zfs_dataset(impl_handle, path, B_FALSE);
9374327Sdougm else
9384327Sdougm ret = SA_SYSTEM_ERR;
9393910Sdougm
9404327Sdougm if (dataset != NULL) {
9414327Sdougm (void) snprintf(command, ZFS_MAXPROPLEN * 2,
9424327Sdougm "%s set sharenfs=\"%s\" %s", COMMAND,
9434327Sdougm opts != NULL ? opts : "off", dataset);
9444327Sdougm pfile = popen(command, "r");
9454327Sdougm if (pfile != NULL) {
9464327Sdougm ret = pclose(pfile);
9474327Sdougm if (ret != 0)
9484327Sdougm ret = SA_SYSTEM_ERR;
9494327Sdougm }
9503034Sdougm }
9514327Sdougm if (opts != NULL)
9524327Sdougm free(opts);
9534327Sdougm if (dataset != NULL)
9544327Sdougm free(dataset);
9554327Sdougm free(command);
9563034Sdougm }
9573034Sdougm return (ret);
9583034Sdougm }
9593034Sdougm
9603034Sdougm /*
9615331Samw * add_resources(share, opt)
9625331Samw *
9635331Samw * Add resource properties to those in "opt". Resources are prefixed
9645331Samw * with name=resourcename.
9655331Samw */
9665331Samw static char *
add_resources(sa_share_t share,char * opt)9675331Samw add_resources(sa_share_t share, char *opt)
9685331Samw {
9695331Samw char *newopt = NULL;
9705331Samw char *propstr;
9715331Samw sa_resource_t resource;
9725331Samw
9735331Samw newopt = strdup(opt);
9745331Samw if (newopt == NULL)
9755331Samw return (newopt);
9765331Samw
9775331Samw for (resource = sa_get_share_resource(share, NULL);
9785331Samw resource != NULL;
9795331Samw resource = sa_get_next_resource(resource)) {
9805331Samw char *name;
9815331Samw size_t size;
9825331Samw
9835331Samw name = sa_get_resource_attr(resource, "name");
9845331Samw if (name == NULL) {
9855331Samw free(newopt);
9865331Samw return (NULL);
9875331Samw }
9885331Samw size = strlen(name) + strlen(opt) + sizeof ("name=") + 1;
9895331Samw newopt = calloc(1, size);
9905331Samw if (newopt != NULL)
9915331Samw (void) snprintf(newopt, size, "%s,name=%s", opt, name);
99211337SWilliam.Krier@Sun.COM sa_free_attr_string(name);
9935331Samw free(opt);
9945331Samw opt = newopt;
9955331Samw propstr = sa_proto_legacy_format("smb", resource, 0);
9965331Samw if (propstr == NULL) {
9975331Samw free(opt);
9985331Samw return (NULL);
9995331Samw }
10005331Samw size = strlen(propstr) + strlen(opt) + 2;
10015331Samw newopt = calloc(1, size);
10025331Samw if (newopt != NULL)
10035331Samw (void) snprintf(newopt, size, "%s,%s", opt, propstr);
10045331Samw free(opt);
10055331Samw opt = newopt;
10065331Samw }
10075331Samw return (opt);
10085331Samw }
10095331Samw
10105331Samw /*
10115331Samw * sa_zfs_set_sharesmb(group, path, on)
10125331Samw *
10135331Samw * Update the "sharesmb" property on the path. If on is true, then set
10145331Samw * to the properties on the group or "on" if no properties are
10155331Samw * defined. Set to "off" if on is false.
10165331Samw */
10175331Samw
10185331Samw int
sa_zfs_set_sharesmb(sa_group_t group,char * path,int on)10195331Samw sa_zfs_set_sharesmb(sa_group_t group, char *path, int on)
10205331Samw {
10215331Samw int ret = SA_NOT_IMPLEMENTED;
10225331Samw char *command;
10235331Samw sa_share_t share;
10245331Samw
10255331Samw /* In case SMB not enabled */
10265331Samw if (sa_get_optionset(group, "smb") == NULL)
10275331Samw return (SA_NOT_SUPPORTED);
10285331Samw
10295331Samw command = malloc(ZFS_MAXPROPLEN * 2);
10305331Samw if (command != NULL) {
10315331Samw char *opts = NULL;
10325331Samw char *dataset = NULL;
10335331Samw FILE *pfile;
10345331Samw sa_handle_impl_t impl_handle;
10355331Samw
10365331Samw if (on) {
10375331Samw char *newopt;
10385331Samw
10395331Samw share = sa_get_share(group, NULL);
10405331Samw opts = sa_proto_legacy_format("smb", share, 1);
10415331Samw if (opts != NULL && strlen(opts) == 0) {
10425331Samw free(opts);
10435331Samw opts = strdup("on");
10445331Samw }
10455331Samw newopt = add_resources(opts, share);
10465331Samw free(opts);
10475331Samw opts = newopt;
10485331Samw }
10495331Samw
10505331Samw impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
10515331Samw assert(impl_handle != NULL);
10525331Samw if (impl_handle != NULL)
10535331Samw dataset = get_zfs_dataset(impl_handle, path, B_FALSE);
10545331Samw else
10555331Samw ret = SA_SYSTEM_ERR;
10565331Samw
10575331Samw if (dataset != NULL) {
10585331Samw (void) snprintf(command, ZFS_MAXPROPLEN * 2,
10595331Samw "echo %s set sharesmb=\"%s\" %s", COMMAND,
10605331Samw opts != NULL ? opts : "off", dataset);
10615331Samw pfile = popen(command, "r");
10625331Samw if (pfile != NULL) {
10635331Samw ret = pclose(pfile);
10645331Samw if (ret != 0)
10655331Samw ret = SA_SYSTEM_ERR;
10665331Samw }
10675331Samw }
10685331Samw if (opts != NULL)
10695331Samw free(opts);
10705331Samw if (dataset != NULL)
10715331Samw free(dataset);
10725331Samw free(command);
10735331Samw }
10745331Samw return (ret);
10755331Samw }
10765331Samw
10775331Samw /*
10783034Sdougm * sa_zfs_update(group)
10793034Sdougm *
10803034Sdougm * call back to ZFS to update the share if necessary.
10813034Sdougm * Don't do it if it isn't a real change.
10823034Sdougm */
10833034Sdougm int
sa_zfs_update(sa_group_t group)10843034Sdougm sa_zfs_update(sa_group_t group)
10853034Sdougm {
10863034Sdougm sa_optionset_t protopt;
10873034Sdougm sa_group_t parent;
10883034Sdougm char *command;
10893034Sdougm char *optstring;
10903034Sdougm int ret = SA_OK;
10913034Sdougm int doupdate = 0;
10923034Sdougm FILE *pfile;
10933034Sdougm
10943034Sdougm if (sa_is_share(group))
10954327Sdougm parent = sa_get_parent_group(group);
10963034Sdougm else
10974327Sdougm parent = group;
10983034Sdougm
10993034Sdougm if (parent != NULL) {
11004327Sdougm command = malloc(ZFS_MAXPROPLEN * 2);
11014327Sdougm if (command == NULL)
11024327Sdougm return (SA_NO_MEMORY);
11034327Sdougm
11044327Sdougm *command = '\0';
11054327Sdougm for (protopt = sa_get_optionset(parent, NULL); protopt != NULL;
11064327Sdougm protopt = sa_get_next_optionset(protopt)) {
11073034Sdougm
11084327Sdougm char *proto = sa_get_optionset_attr(protopt, "type");
11094327Sdougm char *path;
11104327Sdougm char *dataset = NULL;
11114327Sdougm char *zfsopts = NULL;
11123034Sdougm
11134327Sdougm if (sa_is_share(group)) {
11144327Sdougm path = sa_get_share_attr((sa_share_t)group,
11154327Sdougm "path");
11164327Sdougm if (path != NULL) {
11174327Sdougm sa_handle_impl_t impl_handle;
11183034Sdougm
11194327Sdougm impl_handle = sa_find_group_handle(
11204327Sdougm group);
11214327Sdougm if (impl_handle != NULL)
11224327Sdougm dataset = get_zfs_dataset(
11234592Smarks impl_handle, path, B_FALSE);
11244327Sdougm else
11254327Sdougm ret = SA_SYSTEM_ERR;
11263910Sdougm
11274327Sdougm sa_free_attr_string(path);
11284327Sdougm }
11294327Sdougm } else {
11304327Sdougm dataset = sa_get_group_attr(group, "name");
11314327Sdougm }
11324327Sdougm /* update only when there is an optstring found */
11334327Sdougm doupdate = 0;
11344327Sdougm if (proto != NULL && dataset != NULL) {
11354327Sdougm optstring = sa_proto_legacy_format(proto,
11364327Sdougm group, 1);
11374327Sdougm zfsopts = get_zfs_property(dataset,
11384327Sdougm ZFS_PROP_SHARENFS);
11393034Sdougm
11404327Sdougm if (optstring != NULL && zfsopts != NULL) {
11414327Sdougm if (strcmp(optstring, zfsopts) != 0)
11424327Sdougm doupdate++;
11434327Sdougm }
11444327Sdougm if (doupdate) {
11454327Sdougm if (optstring != NULL &&
11464327Sdougm strlen(optstring) > 0) {
11474327Sdougm (void) snprintf(command,
11484327Sdougm ZFS_MAXPROPLEN * 2,
11497483SDoug.McCallum@Sun.COM "%s set share%s=%s %s",
11507483SDoug.McCallum@Sun.COM COMMAND, proto,
11514327Sdougm optstring, dataset);
11524327Sdougm } else {
11534327Sdougm (void) snprintf(command,
11544327Sdougm ZFS_MAXPROPLEN * 2,
11557483SDoug.McCallum@Sun.COM "%s set share%s=on %s",
11567483SDoug.McCallum@Sun.COM COMMAND, proto,
11574327Sdougm dataset);
11584327Sdougm }
11594327Sdougm pfile = popen(command, "r");
11604327Sdougm if (pfile != NULL)
11614327Sdougm ret = pclose(pfile);
11624327Sdougm switch (ret) {
11634327Sdougm default:
11644327Sdougm case 1:
11654327Sdougm ret = SA_SYSTEM_ERR;
11664327Sdougm break;
11674327Sdougm case 2:
11684327Sdougm ret = SA_SYNTAX_ERR;
11694327Sdougm break;
11704327Sdougm case 0:
11714327Sdougm break;
11724327Sdougm }
11734327Sdougm }
11744327Sdougm if (optstring != NULL)
11754327Sdougm free(optstring);
11764327Sdougm if (zfsopts != NULL)
11774327Sdougm free(zfsopts);
11783034Sdougm }
11794327Sdougm if (proto != NULL)
11804327Sdougm sa_free_attr_string(proto);
11814327Sdougm if (dataset != NULL)
11824327Sdougm free(dataset);
11833034Sdougm }
11844327Sdougm free(command);
11853034Sdougm }
11863034Sdougm return (ret);
11873034Sdougm }
11883034Sdougm
11893034Sdougm /*
11903034Sdougm * sa_group_is_zfs(group)
11913034Sdougm *
11923034Sdougm * Given the group, determine if the zfs attribute is set.
11933034Sdougm */
11943034Sdougm
11953034Sdougm int
sa_group_is_zfs(sa_group_t group)11963034Sdougm sa_group_is_zfs(sa_group_t group)
11973034Sdougm {
11983034Sdougm char *zfs;
11993034Sdougm int ret = 0;
12003034Sdougm
12013034Sdougm zfs = sa_get_group_attr(group, "zfs");
12023034Sdougm if (zfs != NULL) {
12034327Sdougm ret = 1;
12044327Sdougm sa_free_attr_string(zfs);
12053034Sdougm }
12063034Sdougm return (ret);
12073034Sdougm }
12083034Sdougm
12093034Sdougm /*
12103034Sdougm * sa_path_is_zfs(path)
12113034Sdougm *
12123034Sdougm * Check to see if the file system path represents is of type "zfs".
12133034Sdougm */
12143034Sdougm
12153034Sdougm int
sa_path_is_zfs(char * path)12163034Sdougm sa_path_is_zfs(char *path)
12173034Sdougm {
12183034Sdougm char *fstype;
12193034Sdougm int ret = 0;
12203034Sdougm
12213034Sdougm fstype = sa_fstype(path);
12224327Sdougm if (fstype != NULL && strcmp(fstype, "zfs") == 0)
12234327Sdougm ret = 1;
12243034Sdougm if (fstype != NULL)
12254327Sdougm sa_free_fstype(fstype);
12263034Sdougm return (ret);
12273034Sdougm }
12284543Smarks
12294543Smarks int
sa_sharetab_fill_zfs(sa_share_t share,share_t * sh,char * proto)12304543Smarks sa_sharetab_fill_zfs(sa_share_t share, share_t *sh, char *proto)
12314543Smarks {
12324543Smarks char *path;
12334543Smarks
12344713Smarks /* Make sure path is valid */
12354713Smarks
12364543Smarks path = sa_get_share_attr(share, "path");
12374543Smarks if (path != NULL) {
12384543Smarks (void) memset(sh, 0, sizeof (sh));
12394543Smarks (void) sa_fillshare(share, proto, sh);
12404713Smarks sa_free_attr_string(path);
12414543Smarks return (0);
12424543Smarks } else
12434543Smarks return (1);
12444543Smarks }
12454543Smarks
12464543Smarks #define SMAX(i, j) \
12474543Smarks if ((j) > (i)) { \
12484543Smarks (i) = (j); \
12494543Smarks }
12504543Smarks
12514543Smarks int
sa_share_zfs(sa_share_t share,sa_resource_t resource,char * path,share_t * sh,void * exportdata,zfs_share_op_t operation)12528845Samw@Sun.COM sa_share_zfs(sa_share_t share, sa_resource_t resource, char *path, share_t *sh,
12535331Samw void *exportdata, zfs_share_op_t operation)
12544543Smarks {
12554543Smarks libzfs_handle_t *libhandle;
12564543Smarks sa_group_t group;
12574543Smarks sa_handle_t sahandle;
12584543Smarks char *dataset;
12594543Smarks int err = EINVAL;
12604543Smarks int i, j;
12614566Smarks char newpath[MAXPATHLEN];
12624592Smarks char *pathp;
12634543Smarks
12644543Smarks /*
12654543Smarks * First find the dataset name
12664543Smarks */
12674543Smarks if ((group = sa_get_parent_group(share)) == NULL) {
126810761SWilliam.Krier@Sun.COM return (EINVAL);
12694543Smarks }
12704543Smarks if ((sahandle = sa_find_group_handle(group)) == NULL) {
127110761SWilliam.Krier@Sun.COM return (EINVAL);
12724543Smarks }
12734543Smarks
12744566Smarks /*
12754566Smarks * If get_zfs_dataset fails, see if it is a subdirectory
12764566Smarks */
12774592Smarks
12784592Smarks pathp = path;
12794592Smarks while ((dataset = get_zfs_dataset(sahandle, pathp, B_TRUE)) == NULL) {
12804566Smarks char *p;
12814566Smarks
12824592Smarks if (pathp == path) {
12834592Smarks (void) strlcpy(newpath, path, sizeof (newpath));
12844592Smarks pathp = newpath;
12854592Smarks }
12864592Smarks
12874592Smarks /*
12887602SDoug.McCallum@Sun.COM * Make sure only one leading '/' This condition came
12897602SDoug.McCallum@Sun.COM * about when using HAStoragePlus which insisted on
12907602SDoug.McCallum@Sun.COM * putting an extra leading '/' in the ZFS path
12917602SDoug.McCallum@Sun.COM * name. The problem is fixed in other areas, but this
12927602SDoug.McCallum@Sun.COM * will catch any other ways that a double slash might
12937602SDoug.McCallum@Sun.COM * get introduced.
12947602SDoug.McCallum@Sun.COM */
12957602SDoug.McCallum@Sun.COM while (*pathp == '/' && *(pathp + 1) == '/')
12967602SDoug.McCallum@Sun.COM pathp++;
12977602SDoug.McCallum@Sun.COM
12987602SDoug.McCallum@Sun.COM /*
12994592Smarks * chop off part of path, but if we are at root then
13004592Smarks * make sure path is a /
13014592Smarks */
13024592Smarks if ((strlen(pathp) > 1) && (p = strrchr(pathp, '/'))) {
13034592Smarks if (pathp == p) {
13044592Smarks *(p + 1) = '\0'; /* skip over /, root case */
13054592Smarks } else {
13064592Smarks *p = '\0';
13074592Smarks }
13084592Smarks } else {
130910761SWilliam.Krier@Sun.COM return (EINVAL);
13104592Smarks }
13114543Smarks }
13124543Smarks
13134543Smarks libhandle = libzfs_init();
13144543Smarks if (libhandle != NULL) {
13158845Samw@Sun.COM char *resource_name;
13164543Smarks
13174543Smarks i = (sh->sh_path ? strlen(sh->sh_path) : 0);
13184543Smarks sh->sh_size = i;
13194543Smarks
13204543Smarks j = (sh->sh_res ? strlen(sh->sh_res) : 0);
13214543Smarks sh->sh_size += j;
13224543Smarks SMAX(i, j);
13234543Smarks
13244543Smarks j = (sh->sh_fstype ? strlen(sh->sh_fstype) : 0);
13254543Smarks sh->sh_size += j;
13264543Smarks SMAX(i, j);
13274543Smarks
13284543Smarks j = (sh->sh_opts ? strlen(sh->sh_opts) : 0);
13294543Smarks sh->sh_size += j;
13304543Smarks SMAX(i, j);
13314543Smarks
13324543Smarks j = (sh->sh_descr ? strlen(sh->sh_descr) : 0);
13334543Smarks sh->sh_size += j;
13344543Smarks SMAX(i, j);
13358845Samw@Sun.COM
13368845Samw@Sun.COM resource_name = sa_get_resource_attr(resource, "name");
13378845Samw@Sun.COM
13384543Smarks err = zfs_deleg_share_nfs(libhandle, dataset, path,
13398845Samw@Sun.COM resource_name, exportdata, sh, i, operation);
13405951Sdougm if (err == SA_OK)
13415951Sdougm sa_update_sharetab_ts(sahandle);
134210761SWilliam.Krier@Sun.COM else
134310761SWilliam.Krier@Sun.COM err = errno;
13448845Samw@Sun.COM if (resource_name)
13458845Samw@Sun.COM sa_free_attr_string(resource_name);
13468845Samw@Sun.COM
13474543Smarks libzfs_fini(libhandle);
13484543Smarks }
13494543Smarks free(dataset);
13504543Smarks return (err);
13514543Smarks }
13525951Sdougm
13535951Sdougm /*
13545951Sdougm * sa_get_zfs_handle(handle)
13555951Sdougm *
13565951Sdougm * Given an sa_handle_t, return the libzfs_handle_t *. This is only
13575951Sdougm * used internally by libzfs. Needed in order to avoid including
13585951Sdougm * libshare_impl.h in libzfs.
13595951Sdougm */
13605951Sdougm
13615951Sdougm libzfs_handle_t *
sa_get_zfs_handle(sa_handle_t handle)13625951Sdougm sa_get_zfs_handle(sa_handle_t handle)
13635951Sdougm {
13645951Sdougm sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
13655951Sdougm
13665951Sdougm return (implhandle->zfs_libhandle);
13675951Sdougm }
13688845Samw@Sun.COM
13698845Samw@Sun.COM /*
13708845Samw@Sun.COM * sa_get_zfs_info(libzfs, path, mountpoint, dataset)
13718845Samw@Sun.COM *
13728845Samw@Sun.COM * Find the ZFS dataset and mountpoint for a given path
13738845Samw@Sun.COM */
13748845Samw@Sun.COM int
sa_zfs_get_info(libzfs_handle_t * libzfs,char * path,char * mountpointp,char * datasetp)13758845Samw@Sun.COM sa_zfs_get_info(libzfs_handle_t *libzfs, char *path, char *mountpointp,
13768845Samw@Sun.COM char *datasetp)
13778845Samw@Sun.COM {
13788845Samw@Sun.COM get_all_cbdata_t cb = { 0 };
13798845Samw@Sun.COM int i;
13808845Samw@Sun.COM char mountpoint[ZFS_MAXPROPLEN];
13818845Samw@Sun.COM char dataset[ZFS_MAXPROPLEN];
13828845Samw@Sun.COM char canmount[ZFS_MAXPROPLEN];
13838845Samw@Sun.COM char *dp;
13848845Samw@Sun.COM int count;
13858845Samw@Sun.COM int ret = 0;
13868845Samw@Sun.COM
13878845Samw@Sun.COM cb.cb_types = ZFS_TYPE_FILESYSTEM;
13888845Samw@Sun.COM
13898845Samw@Sun.COM if (libzfs == NULL)
13908845Samw@Sun.COM return (0);
13918845Samw@Sun.COM
13928845Samw@Sun.COM (void) zfs_iter_root(libzfs, get_one_filesystem, &cb);
13938845Samw@Sun.COM count = cb.cb_used;
13948845Samw@Sun.COM
13958845Samw@Sun.COM qsort(cb.cb_handles, count, sizeof (void *), mountpoint_compare);
13968845Samw@Sun.COM for (i = 0; i < count; i++) {
13978845Samw@Sun.COM /* must have a mountpoint */
13988845Samw@Sun.COM if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_MOUNTPOINT,
13998845Samw@Sun.COM mountpoint, sizeof (mountpoint),
14008845Samw@Sun.COM NULL, NULL, 0, B_FALSE) != 0) {
14018845Samw@Sun.COM /* no mountpoint */
14028845Samw@Sun.COM continue;
14038845Samw@Sun.COM }
14048845Samw@Sun.COM
14058845Samw@Sun.COM /* mountpoint must be a path */
14068845Samw@Sun.COM if (strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) == 0 ||
14078845Samw@Sun.COM strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
14088845Samw@Sun.COM /*
14098845Samw@Sun.COM * Search mmttab for mountpoint
14108845Samw@Sun.COM */
14118845Samw@Sun.COM
14128845Samw@Sun.COM if (get_legacy_mountpoint(path, dataset,
14138845Samw@Sun.COM ZFS_MAXPROPLEN, mountpoint,
14148845Samw@Sun.COM ZFS_MAXPROPLEN) == 0) {
14158845Samw@Sun.COM ret = 1;
14168845Samw@Sun.COM break;
14178845Samw@Sun.COM }
14188845Samw@Sun.COM continue;
14198845Samw@Sun.COM }
14208845Samw@Sun.COM
14218845Samw@Sun.COM /* canmount must be set */
14228845Samw@Sun.COM canmount[0] = '\0';
14238845Samw@Sun.COM if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_CANMOUNT, canmount,
14248845Samw@Sun.COM sizeof (canmount), NULL, NULL, 0, B_FALSE) != 0 ||
14258845Samw@Sun.COM strcmp(canmount, "off") == 0)
14268845Samw@Sun.COM continue;
14278845Samw@Sun.COM
14288845Samw@Sun.COM /*
14298845Samw@Sun.COM * have a mountable handle but want to skip those marked none
14308845Samw@Sun.COM * and legacy
14318845Samw@Sun.COM */
14328845Samw@Sun.COM if (strcmp(mountpoint, path) == 0) {
14338845Samw@Sun.COM dp = (char *)zfs_get_name(cb.cb_handles[i]);
14348845Samw@Sun.COM if (dp != NULL) {
14358845Samw@Sun.COM if (datasetp != NULL)
14368845Samw@Sun.COM (void) strcpy(datasetp, dp);
14378845Samw@Sun.COM if (mountpointp != NULL)
14388845Samw@Sun.COM (void) strcpy(mountpointp, mountpoint);
14398845Samw@Sun.COM ret = 1;
14408845Samw@Sun.COM }
14418845Samw@Sun.COM break;
14428845Samw@Sun.COM }
14438845Samw@Sun.COM
14448845Samw@Sun.COM }
14458845Samw@Sun.COM
14468845Samw@Sun.COM return (ret);
14478845Samw@Sun.COM }
1448*12508Samw@Sun.COM
1449*12508Samw@Sun.COM /*
1450*12508Samw@Sun.COM * This method builds values for "sharesmb" property from the
1451*12508Samw@Sun.COM * nvlist argument. The values are returned in sharesmb_val variable.
1452*12508Samw@Sun.COM */
1453*12508Samw@Sun.COM static int
sa_zfs_sprintf_new_prop(nvlist_t * nvl,char * sharesmb_val)1454*12508Samw@Sun.COM sa_zfs_sprintf_new_prop(nvlist_t *nvl, char *sharesmb_val)
1455*12508Samw@Sun.COM {
1456*12508Samw@Sun.COM char cur_val[MAXPATHLEN];
1457*12508Samw@Sun.COM char *name, *val;
1458*12508Samw@Sun.COM nvpair_t *cur;
1459*12508Samw@Sun.COM int err = 0;
1460*12508Samw@Sun.COM
1461*12508Samw@Sun.COM cur = nvlist_next_nvpair(nvl, NULL);
1462*12508Samw@Sun.COM while (cur != NULL) {
1463*12508Samw@Sun.COM name = nvpair_name(cur);
1464*12508Samw@Sun.COM err = nvpair_value_string(cur, &val);
1465*12508Samw@Sun.COM if ((err != 0) || (name == NULL) || (val == NULL))
1466*12508Samw@Sun.COM return (-1);
1467*12508Samw@Sun.COM
1468*12508Samw@Sun.COM (void) snprintf(cur_val, MAXPATHLEN, "%s=%s,", name, val);
1469*12508Samw@Sun.COM (void) strlcat(sharesmb_val, cur_val, MAXPATHLEN);
1470*12508Samw@Sun.COM
1471*12508Samw@Sun.COM cur = nvlist_next_nvpair(nvl, cur);
1472*12508Samw@Sun.COM }
1473*12508Samw@Sun.COM
1474*12508Samw@Sun.COM return (0);
1475*12508Samw@Sun.COM }
1476*12508Samw@Sun.COM
1477*12508Samw@Sun.COM /*
1478*12508Samw@Sun.COM * This method builds values for "sharesmb" property from values
1479*12508Samw@Sun.COM * already existing on the share. The properties set via sa_zfs_sprint_new_prop
1480*12508Samw@Sun.COM * method are passed in sharesmb_val. If a existing property is already
1481*12508Samw@Sun.COM * set via sa_zfs_sprint_new_prop method, then they are not appended
1482*12508Samw@Sun.COM * to the sharesmb_val string. The returned sharesmb_val string is a combination
1483*12508Samw@Sun.COM * of new and existing values for 'sharesmb' property.
1484*12508Samw@Sun.COM */
1485*12508Samw@Sun.COM static int
sa_zfs_sprintf_existing_prop(zfs_handle_t * handle,char * sharesmb_val)1486*12508Samw@Sun.COM sa_zfs_sprintf_existing_prop(zfs_handle_t *handle, char *sharesmb_val)
1487*12508Samw@Sun.COM {
1488*12508Samw@Sun.COM char shareopts[ZFS_MAXPROPLEN], cur_val[MAXPATHLEN];
1489*12508Samw@Sun.COM char *token, *last, *value;
1490*12508Samw@Sun.COM
1491*12508Samw@Sun.COM if (zfs_prop_get(handle, ZFS_PROP_SHARESMB, shareopts,
1492*12508Samw@Sun.COM sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0)
1493*12508Samw@Sun.COM return (-1);
1494*12508Samw@Sun.COM
1495*12508Samw@Sun.COM if (strstr(shareopts, "=") == NULL)
1496*12508Samw@Sun.COM return (0);
1497*12508Samw@Sun.COM
1498*12508Samw@Sun.COM for (token = strtok_r(shareopts, ",", &last); token != NULL;
1499*12508Samw@Sun.COM token = strtok_r(NULL, ",", &last)) {
1500*12508Samw@Sun.COM value = strchr(token, '=');
1501*12508Samw@Sun.COM if (value == NULL)
1502*12508Samw@Sun.COM return (-1);
1503*12508Samw@Sun.COM *value++ = '\0';
1504*12508Samw@Sun.COM
1505*12508Samw@Sun.COM (void) snprintf(cur_val, MAXPATHLEN, "%s=", token);
1506*12508Samw@Sun.COM if (strstr(sharesmb_val, cur_val) == NULL) {
1507*12508Samw@Sun.COM (void) strlcat(cur_val, value, MAXPATHLEN);
1508*12508Samw@Sun.COM (void) strlcat(cur_val, ",", MAXPATHLEN);
1509*12508Samw@Sun.COM (void) strlcat(sharesmb_val, cur_val, MAXPATHLEN);
1510*12508Samw@Sun.COM }
1511*12508Samw@Sun.COM }
1512*12508Samw@Sun.COM
1513*12508Samw@Sun.COM return (0);
1514*12508Samw@Sun.COM }
1515*12508Samw@Sun.COM
1516*12508Samw@Sun.COM /*
1517*12508Samw@Sun.COM * Sets the share properties on a ZFS share. For now, this method sets only
1518*12508Samw@Sun.COM * the "sharesmb" property.
1519*12508Samw@Sun.COM *
1520*12508Samw@Sun.COM * This method includes building a comma seperated name-value string to be
1521*12508Samw@Sun.COM * set on the "sharesmb" property of a ZFS share. This name-value string is
1522*12508Samw@Sun.COM * build in 2 steps:
1523*12508Samw@Sun.COM * - New property values given as name-value pair are set first.
1524*12508Samw@Sun.COM * - Existing optionset properties, which are not part of the new properties
1525*12508Samw@Sun.COM * passed in step 1, are appended to the newly set properties.
1526*12508Samw@Sun.COM */
1527*12508Samw@Sun.COM int
sa_zfs_setprop(sa_handle_t handle,char * path,nvlist_t * nvl)1528*12508Samw@Sun.COM sa_zfs_setprop(sa_handle_t handle, char *path, nvlist_t *nvl)
1529*12508Samw@Sun.COM {
1530*12508Samw@Sun.COM zfs_handle_t *z_fs;
1531*12508Samw@Sun.COM libzfs_handle_t *z_lib;
1532*12508Samw@Sun.COM char sharesmb_val[MAXPATHLEN];
1533*12508Samw@Sun.COM char *dataset, *lastcomma;
1534*12508Samw@Sun.COM
1535*12508Samw@Sun.COM if (nvlist_empty(nvl))
1536*12508Samw@Sun.COM return (0);
1537*12508Samw@Sun.COM
1538*12508Samw@Sun.COM if ((handle == NULL) || (path == NULL))
1539*12508Samw@Sun.COM return (-1);
1540*12508Samw@Sun.COM
1541*12508Samw@Sun.COM if ((dataset = get_zfs_dataset(handle, path, B_FALSE)) == NULL)
1542*12508Samw@Sun.COM return (-1);
1543*12508Samw@Sun.COM
1544*12508Samw@Sun.COM if ((z_lib = libzfs_init()) == NULL) {
1545*12508Samw@Sun.COM free(dataset);
1546*12508Samw@Sun.COM return (-1);
1547*12508Samw@Sun.COM }
1548*12508Samw@Sun.COM
1549*12508Samw@Sun.COM z_fs = zfs_open(z_lib, dataset, ZFS_TYPE_DATASET);
1550*12508Samw@Sun.COM if (z_fs == NULL) {
1551*12508Samw@Sun.COM free(dataset);
1552*12508Samw@Sun.COM libzfs_fini(z_lib);
1553*12508Samw@Sun.COM return (-1);
1554*12508Samw@Sun.COM }
1555*12508Samw@Sun.COM
1556*12508Samw@Sun.COM bzero(sharesmb_val, MAXPATHLEN);
1557*12508Samw@Sun.COM if (sa_zfs_sprintf_new_prop(nvl, sharesmb_val) != 0) {
1558*12508Samw@Sun.COM free(dataset);
1559*12508Samw@Sun.COM zfs_close(z_fs);
1560*12508Samw@Sun.COM libzfs_fini(z_lib);
1561*12508Samw@Sun.COM return (-1);
1562*12508Samw@Sun.COM }
1563*12508Samw@Sun.COM
1564*12508Samw@Sun.COM if (sa_zfs_sprintf_existing_prop(z_fs, sharesmb_val) != 0) {
1565*12508Samw@Sun.COM free(dataset);
1566*12508Samw@Sun.COM zfs_close(z_fs);
1567*12508Samw@Sun.COM libzfs_fini(z_lib);
1568*12508Samw@Sun.COM return (-1);
1569*12508Samw@Sun.COM }
1570*12508Samw@Sun.COM
1571*12508Samw@Sun.COM lastcomma = strrchr(sharesmb_val, ',');
1572*12508Samw@Sun.COM if ((lastcomma != NULL) && (lastcomma[1] == '\0'))
1573*12508Samw@Sun.COM *lastcomma = '\0';
1574*12508Samw@Sun.COM
1575*12508Samw@Sun.COM (void) zfs_prop_set(z_fs, zfs_prop_to_name(ZFS_PROP_SHARESMB),
1576*12508Samw@Sun.COM sharesmb_val);
1577*12508Samw@Sun.COM free(dataset);
1578*12508Samw@Sun.COM zfs_close(z_fs);
1579*12508Samw@Sun.COM libzfs_fini(z_lib);
1580*12508Samw@Sun.COM
1581*12508Samw@Sun.COM return (0);
1582*12508Samw@Sun.COM }
1583