1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * 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.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 /* 26 * Copyright 2012 Nexenta Systems, Inc. All rights reserved. 27 */ 28 29 #include <stdio.h> 30 #include <libzfs.h> 31 #include <string.h> 32 #include <strings.h> 33 #include <errno.h> 34 #include <libshare.h> 35 #include "libshare_impl.h" 36 #include <libintl.h> 37 #include <sys/mnttab.h> 38 #include <sys/mntent.h> 39 40 extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t); 41 extern sa_group_t _sa_create_zfs_group(sa_group_t, char *); 42 extern char *sa_fstype(char *); 43 extern void set_node_attr(void *, char *, char *); 44 extern int sa_is_share(void *); 45 extern void sa_update_sharetab_ts(sa_handle_t); 46 47 /* 48 * File system specific code for ZFS. The original code was stolen 49 * from the "zfs" command and modified to better suit this library's 50 * usage. 51 */ 52 53 typedef struct get_all_cbdata { 54 zfs_handle_t **cb_handles; 55 size_t cb_alloc; 56 size_t cb_used; 57 uint_t cb_types; 58 } get_all_cbdata_t; 59 60 /* 61 * sa_zfs_init(impl_handle) 62 * 63 * Initialize an access handle into libzfs. The handle needs to stay 64 * around until sa_zfs_fini() in order to maintain the cache of 65 * mounts. 66 */ 67 68 int 69 sa_zfs_init(sa_handle_impl_t impl_handle) 70 { 71 impl_handle->zfs_libhandle = libzfs_init(); 72 if (impl_handle->zfs_libhandle != NULL) { 73 libzfs_print_on_error(impl_handle->zfs_libhandle, B_TRUE); 74 return (B_TRUE); 75 } 76 return (B_FALSE); 77 } 78 79 /* 80 * sa_zfs_fini(impl_handle) 81 * 82 * cleanup data structures and the libzfs handle used for accessing 83 * zfs file share info. 84 */ 85 86 void 87 sa_zfs_fini(sa_handle_impl_t impl_handle) 88 { 89 if (impl_handle->zfs_libhandle != NULL) { 90 if (impl_handle->zfs_list != NULL) { 91 zfs_handle_t **zhp = impl_handle->zfs_list; 92 size_t i; 93 94 /* 95 * Contents of zfs_list need to be freed so we 96 * don't lose ZFS handles. 97 */ 98 for (i = 0; i < impl_handle->zfs_list_count; i++) { 99 zfs_close(zhp[i]); 100 } 101 free(impl_handle->zfs_list); 102 impl_handle->zfs_list = NULL; 103 impl_handle->zfs_list_count = 0; 104 } 105 106 libzfs_fini(impl_handle->zfs_libhandle); 107 impl_handle->zfs_libhandle = NULL; 108 } 109 } 110 111 /* 112 * get_one_filesystem(zfs_handle_t, data) 113 * 114 * an iterator function called while iterating through the ZFS 115 * root. It accumulates into an array of file system handles that can 116 * be used to derive info about those file systems. 117 * 118 * Note that as this function is called, we close all zhp handles that 119 * are not going to be places into the cp_handles list. We don't want 120 * to close the ones we are keeping, but all others would be leaked if 121 * not closed here. 122 */ 123 124 static int 125 get_one_filesystem(zfs_handle_t *zhp, void *data) 126 { 127 get_all_cbdata_t *cbp = data; 128 zfs_type_t type = zfs_get_type(zhp); 129 130 /* 131 * Interate over any nested datasets. 132 */ 133 if (type == ZFS_TYPE_FILESYSTEM && 134 zfs_iter_filesystems(zhp, get_one_filesystem, data) != 0) { 135 zfs_close(zhp); 136 return (1); 137 } 138 139 /* 140 * Skip any datasets whose type does not match. 141 */ 142 if ((type & cbp->cb_types) == 0) { 143 zfs_close(zhp); 144 return (0); 145 } 146 147 if (cbp->cb_alloc == cbp->cb_used) { 148 zfs_handle_t **handles; 149 150 if (cbp->cb_alloc == 0) 151 cbp->cb_alloc = 64; 152 else 153 cbp->cb_alloc *= 2; 154 155 handles = (zfs_handle_t **)calloc(1, 156 cbp->cb_alloc * sizeof (void *)); 157 158 if (handles == NULL) { 159 zfs_close(zhp); 160 return (0); 161 } 162 if (cbp->cb_handles) { 163 bcopy(cbp->cb_handles, handles, 164 cbp->cb_used * sizeof (void *)); 165 free(cbp->cb_handles); 166 } 167 168 cbp->cb_handles = handles; 169 } 170 171 cbp->cb_handles[cbp->cb_used++] = zhp; 172 173 return (0); 174 } 175 176 /* 177 * get_all_filesystems(zfs_handle_t ***fslist, size_t *count) 178 * 179 * iterate through all ZFS file systems starting at the root. Returns 180 * a count and an array of handle pointers. Allocating is only done 181 * once. The caller does not need to free since it will be done at 182 * sa_zfs_fini() time. 183 */ 184 185 static void 186 get_all_filesystems(sa_handle_impl_t impl_handle, 187 zfs_handle_t ***fslist, size_t *count) 188 { 189 get_all_cbdata_t cb = { 0 }; 190 cb.cb_types = ZFS_TYPE_FILESYSTEM; 191 192 if (impl_handle->zfs_list != NULL) { 193 *fslist = impl_handle->zfs_list; 194 *count = impl_handle->zfs_list_count; 195 return; 196 } 197 198 (void) zfs_iter_root(impl_handle->zfs_libhandle, 199 get_one_filesystem, &cb); 200 201 impl_handle->zfs_list = *fslist = cb.cb_handles; 202 impl_handle->zfs_list_count = *count = cb.cb_used; 203 } 204 205 /* 206 * mountpoint_compare(a, b) 207 * 208 * compares the mountpoint on two zfs file systems handles. 209 * returns values following strcmp() model. 210 */ 211 212 static int 213 mountpoint_compare(const void *a, const void *b) 214 { 215 zfs_handle_t **za = (zfs_handle_t **)a; 216 zfs_handle_t **zb = (zfs_handle_t **)b; 217 char mounta[MAXPATHLEN]; 218 char mountb[MAXPATHLEN]; 219 220 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 221 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 222 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 223 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 224 225 return (strcmp(mounta, mountb)); 226 } 227 228 /* 229 * return legacy mountpoint. Caller provides space for mountpoint and 230 * dataset. 231 */ 232 int 233 get_legacy_mountpoint(char *path, char *dataset, size_t dlen, 234 char *mountpoint, size_t mlen) 235 { 236 FILE *fp; 237 struct mnttab entry; 238 239 if ((fp = fopen(MNTTAB, "r")) == NULL) { 240 return (1); 241 } 242 243 while (getmntent(fp, &entry) == 0) { 244 245 if (entry.mnt_fstype == NULL || 246 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 247 continue; 248 249 if (strcmp(entry.mnt_mountp, path) == 0) { 250 if (mlen > 0) 251 (void) strlcpy(mountpoint, entry.mnt_mountp, 252 mlen); 253 if (dlen > 0) 254 (void) strlcpy(dataset, entry.mnt_special, 255 dlen); 256 break; 257 } 258 } 259 (void) fclose(fp); 260 return (1); 261 } 262 263 /* 264 * get_zfs_dataset(impl_handle, path) 265 * 266 * get the name of the ZFS dataset the path is equivalent to. The 267 * dataset name is used for get/set of ZFS properties since libzfs 268 * requires a dataset to do a zfs_open(). 269 */ 270 271 static char * 272 get_zfs_dataset(sa_handle_impl_t impl_handle, char *path, 273 boolean_t search_mnttab) 274 { 275 size_t i, count = 0; 276 char *dataset = NULL; 277 zfs_handle_t **zlist; 278 char mountpoint[ZFS_MAXPROPLEN]; 279 char canmount[ZFS_MAXPROPLEN]; 280 281 get_all_filesystems(impl_handle, &zlist, &count); 282 qsort(zlist, count, sizeof (void *), mountpoint_compare); 283 for (i = 0; i < count; i++) { 284 /* must have a mountpoint */ 285 if (zfs_prop_get(zlist[i], ZFS_PROP_MOUNTPOINT, mountpoint, 286 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) != 0) { 287 /* no mountpoint */ 288 continue; 289 } 290 291 /* mountpoint must be a path */ 292 if (strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) == 0 || 293 strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 294 /* 295 * Search mmttab for mountpoint and get dataset. 296 */ 297 298 if (search_mnttab == B_TRUE && 299 get_legacy_mountpoint(path, mountpoint, 300 sizeof (mountpoint), NULL, 0) == 0) { 301 dataset = mountpoint; 302 break; 303 } 304 continue; 305 } 306 307 /* canmount must be set */ 308 canmount[0] = '\0'; 309 if (zfs_prop_get(zlist[i], ZFS_PROP_CANMOUNT, canmount, 310 sizeof (canmount), NULL, NULL, 0, B_FALSE) != 0 || 311 strcmp(canmount, "off") == 0) 312 continue; 313 314 /* 315 * have a mountable handle but want to skip those marked none 316 * and legacy 317 */ 318 if (strcmp(mountpoint, path) == 0) { 319 dataset = (char *)zfs_get_name(zlist[i]); 320 break; 321 } 322 323 } 324 325 if (dataset != NULL) 326 dataset = strdup(dataset); 327 328 return (dataset); 329 } 330 331 /* 332 * get_zfs_property(dataset, property) 333 * 334 * Get the file system property specified from the ZFS dataset. 335 */ 336 337 static char * 338 get_zfs_property(char *dataset, zfs_prop_t property) 339 { 340 zfs_handle_t *handle = NULL; 341 char shareopts[ZFS_MAXPROPLEN]; 342 libzfs_handle_t *libhandle; 343 344 libhandle = libzfs_init(); 345 if (libhandle != NULL) { 346 handle = zfs_open(libhandle, dataset, ZFS_TYPE_FILESYSTEM); 347 if (handle != NULL) { 348 if (zfs_prop_get(handle, property, shareopts, 349 sizeof (shareopts), NULL, NULL, 0, 350 B_FALSE) == 0) { 351 zfs_close(handle); 352 libzfs_fini(libhandle); 353 return (strdup(shareopts)); 354 } 355 zfs_close(handle); 356 } 357 libzfs_fini(libhandle); 358 } 359 return (NULL); 360 } 361 362 /* 363 * sa_zfs_is_shared(handle, path) 364 * 365 * Check to see if the ZFS path provided has the sharenfs option set 366 * or not. 367 */ 368 369 int 370 sa_zfs_is_shared(sa_handle_t sahandle, char *path) 371 { 372 int ret = 0; 373 char *dataset; 374 zfs_handle_t *handle = NULL; 375 char shareopts[ZFS_MAXPROPLEN]; 376 libzfs_handle_t *libhandle; 377 378 dataset = get_zfs_dataset((sa_handle_t)sahandle, path, B_FALSE); 379 if (dataset != NULL) { 380 libhandle = libzfs_init(); 381 if (libhandle != NULL) { 382 handle = zfs_open(libhandle, dataset, 383 ZFS_TYPE_FILESYSTEM); 384 if (handle != NULL) { 385 if (zfs_prop_get(handle, ZFS_PROP_SHARENFS, 386 shareopts, sizeof (shareopts), NULL, NULL, 387 0, B_FALSE) == 0 && 388 strcmp(shareopts, "off") != 0) { 389 ret = 1; /* it is shared */ 390 } 391 zfs_close(handle); 392 } 393 libzfs_fini(libhandle); 394 } 395 free(dataset); 396 } 397 return (ret); 398 } 399 400 /* 401 * find_or_create_group(handle, groupname, proto, *err) 402 * 403 * While walking the ZFS tree, we need to add shares to a defined 404 * group. If the group doesn't exist, create it first, making sure it 405 * is marked as a ZFS group. 406 * 407 * Note that all ZFS shares are in a subgroup of the top level group 408 * called "zfs". 409 */ 410 411 static sa_group_t 412 find_or_create_group(sa_handle_t handle, char *groupname, char *proto, int *err) 413 { 414 sa_group_t group; 415 sa_optionset_t optionset; 416 int ret = SA_OK; 417 418 /* 419 * we check to see if the "zfs" group exists. Since this 420 * should be the top level group, we don't want the 421 * parent. This is to make sure the zfs group has been created 422 * and to created if it hasn't been. 423 */ 424 group = sa_get_group(handle, groupname); 425 if (group == NULL) { 426 group = sa_create_group(handle, groupname, &ret); 427 428 /* make sure this is flagged as a ZFS group */ 429 if (group != NULL) 430 ret = sa_set_group_attr(group, "zfs", "true"); 431 } 432 if (group != NULL) { 433 if (proto != NULL) { 434 optionset = sa_get_optionset(group, proto); 435 if (optionset == NULL) 436 optionset = sa_create_optionset(group, proto); 437 } 438 } 439 if (err != NULL) 440 *err = ret; 441 return (group); 442 } 443 444 /* 445 * find_or_create_zfs_subgroup(groupname, optstring, *err) 446 * 447 * ZFS shares will be in a subgroup of the "zfs" master group. This 448 * function looks to see if the groupname exists and returns it if it 449 * does or else creates a new one with the specified name and returns 450 * that. The "zfs" group will exist before we get here, but we make 451 * sure just in case. 452 * 453 * err must be a valid pointer. 454 */ 455 456 static sa_group_t 457 find_or_create_zfs_subgroup(sa_handle_t handle, char *groupname, char *proto, 458 char *optstring, int *err) 459 { 460 sa_group_t group = NULL; 461 sa_group_t zfs; 462 char *name; 463 char *options; 464 465 /* start with the top-level "zfs" group */ 466 zfs = sa_get_group(handle, "zfs"); 467 *err = SA_OK; 468 if (zfs != NULL) { 469 for (group = sa_get_sub_group(zfs); group != NULL; 470 group = sa_get_next_group(group)) { 471 name = sa_get_group_attr(group, "name"); 472 if (name != NULL && strcmp(name, groupname) == 0) { 473 /* have the group so break out of here */ 474 sa_free_attr_string(name); 475 break; 476 } 477 if (name != NULL) 478 sa_free_attr_string(name); 479 } 480 481 if (group == NULL) { 482 /* 483 * Need to create the sub-group since it doesn't exist 484 */ 485 group = _sa_create_zfs_group(zfs, groupname); 486 if (group == NULL) { 487 *err = SA_NO_MEMORY; 488 return (NULL); 489 } 490 set_node_attr(group, "zfs", "true"); 491 } 492 if (strcmp(optstring, "on") == 0) 493 optstring = "rw"; 494 options = strdup(optstring); 495 if (options != NULL) { 496 *err = sa_parse_legacy_options(group, options, 497 proto); 498 /* If no optionset, add one. */ 499 if (sa_get_optionset(group, proto) == NULL) 500 (void) sa_create_optionset(group, proto); 501 502 /* 503 * Do not forget to update an optionset of 504 * the parent group so that it contains 505 * all protocols its subgroups have. 506 */ 507 if (sa_get_optionset(zfs, proto) == NULL) 508 (void) sa_create_optionset(zfs, proto); 509 510 free(options); 511 } else { 512 *err = SA_NO_MEMORY; 513 } 514 } 515 return (group); 516 } 517 518 /* 519 * zfs_construct_resource(share, name, base, dataset) 520 * 521 * Add a resource to the share using name as a template. If name == 522 * NULL, then construct a name based on the dataset value. 523 * name. 524 */ 525 static void 526 zfs_construct_resource(sa_share_t share, char *dataset) 527 { 528 char buff[SA_MAX_RESOURCE_NAME + 1]; 529 int ret = SA_OK; 530 531 (void) snprintf(buff, SA_MAX_RESOURCE_NAME, "%s", dataset); 532 sa_fix_resource_name(buff); 533 (void) sa_add_resource(share, buff, SA_SHARE_TRANSIENT, &ret); 534 } 535 536 /* 537 * zfs_inherited(handle, source, sourcestr) 538 * 539 * handle case of inherited share{nfs,smb}. Pulled out of sa_get_zfs_shares 540 * for readability. 541 */ 542 static int 543 zfs_inherited(sa_handle_t handle, sa_share_t share, char *sourcestr, 544 char *shareopts, char *mountpoint, char *proto, char *dataset) 545 { 546 int doshopt = 0; 547 int err = SA_OK; 548 sa_group_t group; 549 sa_resource_t resource; 550 uint64_t features; 551 552 /* 553 * Need to find the "real" parent sub-group. It may not be 554 * mounted, but it was identified in the "sourcestr" 555 * variable. The real parent not mounted can occur if 556 * "canmount=off and sharenfs=on". 557 */ 558 group = find_or_create_zfs_subgroup(handle, sourcestr, proto, 559 shareopts, &doshopt); 560 if (group != NULL) { 561 /* 562 * We may need the first share for resource 563 * prototype. We only care about it if it has a 564 * resource that sets a prefix value. 565 */ 566 if (share == NULL) 567 share = _sa_add_share(group, mountpoint, 568 SA_SHARE_TRANSIENT, &err, 569 (uint64_t)SA_FEATURE_NONE); 570 /* 571 * some options may only be on shares. If the opt 572 * string contains one of those, we put it just on the 573 * share. 574 */ 575 if (share != NULL && doshopt == SA_PROP_SHARE_ONLY) { 576 char *options; 577 options = strdup(shareopts); 578 if (options != NULL) { 579 set_node_attr(share, "dataset", dataset); 580 err = sa_parse_legacy_options(share, options, 581 proto); 582 set_node_attr(share, "dataset", NULL); 583 free(options); 584 } 585 if (sa_get_optionset(group, proto) == NULL) 586 (void) sa_create_optionset(group, proto); 587 } 588 features = sa_proto_get_featureset(proto); 589 if (share != NULL && features & SA_FEATURE_RESOURCE) { 590 /* 591 * We have a share and the protocol requires 592 * that at least one resource exist (probably 593 * SMB). We need to make sure that there is at 594 * least one. 595 */ 596 resource = sa_get_share_resource(share, NULL); 597 if (resource == NULL) { 598 zfs_construct_resource(share, dataset); 599 } 600 } 601 } else { 602 err = SA_NO_MEMORY; 603 } 604 return (err); 605 } 606 607 /* 608 * zfs_notinherited(group, share, mountpoint, shareopts, proto, dataset, 609 * grouperr) 610 * 611 * handle case where this is the top of a sub-group in ZFS. Pulled out 612 * of sa_get_zfs_shares for readability. We need the grouperr from the 613 * creation of the subgroup to know whether to add the public 614 * property, etc. to the specific share. 615 */ 616 static int 617 zfs_notinherited(sa_group_t group, sa_share_t share, char *mountpoint, 618 char *shareopts, char *proto, char *dataset, int grouperr) 619 { 620 int err = SA_OK; 621 sa_resource_t resource; 622 uint64_t features; 623 624 set_node_attr(group, "zfs", "true"); 625 if (share == NULL) 626 share = _sa_add_share(group, mountpoint, SA_SHARE_TRANSIENT, 627 &err, (uint64_t)SA_FEATURE_NONE); 628 629 if (err != SA_OK) 630 return (err); 631 632 if (strcmp(shareopts, "on") == 0) 633 shareopts = ""; 634 if (shareopts != NULL) { 635 char *options; 636 if (grouperr == SA_PROP_SHARE_ONLY) { 637 /* 638 * Some properties may only be on shares, but 639 * due to the ZFS sub-groups being artificial, 640 * we sometimes get this and have to deal with 641 * it. We do it by attempting to put it on the 642 * share. 643 */ 644 options = strdup(shareopts); 645 if (options != NULL) { 646 err = sa_parse_legacy_options(share, 647 options, proto); 648 free(options); 649 } 650 } 651 /* Unmark the share's changed state */ 652 set_node_attr(share, "changed", NULL); 653 } 654 features = sa_proto_get_featureset(proto); 655 if (share != NULL && features & SA_FEATURE_RESOURCE) { 656 /* 657 * We have a share and the protocol requires that at 658 * least one resource exist (probably SMB). We need to 659 * make sure that there is at least one. 660 */ 661 resource = sa_get_share_resource(share, NULL); 662 if (resource == NULL) { 663 zfs_construct_resource(share, dataset); 664 } 665 } 666 return (err); 667 } 668 669 /* 670 * zfs_grp_error(err) 671 * 672 * Print group create error, but only once. If err is 0 do the 673 * print else don't. 674 */ 675 676 static void 677 zfs_grp_error(int err) 678 { 679 if (err == 0) { 680 /* only print error once */ 681 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 682 "Cannot create ZFS subgroup during initialization:" 683 " %s\n"), sa_errorstr(SA_SYSTEM_ERR)); 684 } 685 } 686 687 /* 688 * zfs_process_share(handle, share, mountpoint, proto, source, 689 * shareopts, sourcestr) 690 * 691 * Creates the subgroup, if necessary and adds shares, resources 692 * and properties. 693 */ 694 int 695 sa_zfs_process_share(sa_handle_t handle, sa_group_t group, sa_share_t share, 696 char *mountpoint, char *proto, zprop_source_t source, char *shareopts, 697 char *sourcestr, char *dataset) 698 { 699 int err = SA_OK; 700 701 if (source & ZPROP_SRC_INHERITED) { 702 err = zfs_inherited(handle, share, sourcestr, shareopts, 703 mountpoint, proto, dataset); 704 } else { 705 group = find_or_create_zfs_subgroup(handle, dataset, proto, 706 shareopts, &err); 707 if (group == NULL) { 708 static boolean_t reported_error = B_FALSE; 709 /* 710 * There is a problem, but we can't do 711 * anything about it at this point so we issue 712 * a warning and move on. 713 */ 714 zfs_grp_error(reported_error); 715 reported_error = B_TRUE; 716 } 717 set_node_attr(group, "zfs", "true"); 718 /* 719 * Add share with local opts via zfs_notinherited. 720 */ 721 err = zfs_notinherited(group, share, mountpoint, shareopts, 722 proto, dataset, err); 723 } 724 return (err); 725 } 726 727 /* 728 * sa_get_zfs_shares(handle, groupname) 729 * 730 * Walk the mnttab for all zfs mounts and determine which are 731 * shared. Find or create the appropriate group/sub-group to contain 732 * the shares. 733 * 734 * All shares are in a sub-group that will hold the properties. This 735 * allows representing the inherited property model. 736 * 737 * One area of complication is if "sharenfs" is set at one level of 738 * the directory tree and "sharesmb" is set at a different level, the 739 * a sub-group must be formed at the lower level for both 740 * protocols. That is the nature of the problem in CR 6667349. 741 */ 742 743 int 744 sa_get_zfs_shares(sa_handle_t handle, char *groupname) 745 { 746 sa_group_t zfsgroup; 747 boolean_t nfs; 748 boolean_t nfs_inherited; 749 boolean_t smb; 750 boolean_t smb_inherited; 751 zfs_handle_t **zlist; 752 char nfsshareopts[ZFS_MAXPROPLEN]; 753 char smbshareopts[ZFS_MAXPROPLEN]; 754 sa_share_t share; 755 zprop_source_t source; 756 char nfssourcestr[ZFS_MAXPROPLEN]; 757 char smbsourcestr[ZFS_MAXPROPLEN]; 758 char mountpoint[ZFS_MAXPROPLEN]; 759 size_t count = 0, i; 760 libzfs_handle_t *zfs_libhandle; 761 int err = SA_OK; 762 763 /* 764 * If we can't access libzfs, don't bother doing anything. 765 */ 766 zfs_libhandle = ((sa_handle_impl_t)handle)->zfs_libhandle; 767 if (zfs_libhandle == NULL) 768 return (SA_SYSTEM_ERR); 769 770 zfsgroup = find_or_create_group(handle, groupname, NULL, &err); 771 /* Not an error, this could be a legacy condition */ 772 if (zfsgroup == NULL) 773 return (SA_OK); 774 775 /* 776 * need to walk the mounted ZFS pools and datasets to 777 * find shares that are possible. 778 */ 779 get_all_filesystems((sa_handle_impl_t)handle, &zlist, &count); 780 qsort(zlist, count, sizeof (void *), mountpoint_compare); 781 782 for (i = 0; i < count; i++) { 783 char *dataset; 784 785 source = ZPROP_SRC_ALL; 786 /* If no mountpoint, skip. */ 787 if (zfs_prop_get(zlist[i], ZFS_PROP_MOUNTPOINT, 788 mountpoint, sizeof (mountpoint), NULL, NULL, 0, 789 B_FALSE) != 0) 790 continue; 791 792 /* 793 * zfs_get_name value must not be freed. It is just a 794 * pointer to a value in the handle. 795 */ 796 if ((dataset = (char *)zfs_get_name(zlist[i])) == NULL) 797 continue; 798 799 /* 800 * only deal with "mounted" file systems since 801 * unmounted file systems can't actually be shared. 802 */ 803 804 if (!zfs_is_mounted(zlist[i], NULL)) 805 continue; 806 807 nfs = nfs_inherited = B_FALSE; 808 809 if (zfs_prop_get(zlist[i], ZFS_PROP_SHARENFS, nfsshareopts, 810 sizeof (nfsshareopts), &source, nfssourcestr, 811 ZFS_MAXPROPLEN, B_FALSE) == 0 && 812 strcmp(nfsshareopts, "off") != 0) { 813 if (source & ZPROP_SRC_INHERITED) 814 nfs_inherited = B_TRUE; 815 else 816 nfs = B_TRUE; 817 } 818 819 smb = smb_inherited = B_FALSE; 820 if (zfs_prop_get(zlist[i], ZFS_PROP_SHARESMB, smbshareopts, 821 sizeof (smbshareopts), &source, smbsourcestr, 822 ZFS_MAXPROPLEN, B_FALSE) == 0 && 823 strcmp(smbshareopts, "off") != 0) { 824 if (source & ZPROP_SRC_INHERITED) 825 smb_inherited = B_TRUE; 826 else 827 smb = B_TRUE; 828 } 829 830 /* 831 * If the mountpoint is already shared, it must be a 832 * non-ZFS share. We want to remove the share from its 833 * parent group and reshare it under ZFS. 834 */ 835 share = sa_find_share(handle, mountpoint); 836 if (share != NULL && 837 (nfs || smb || nfs_inherited || smb_inherited)) { 838 err = sa_remove_share(share); 839 share = NULL; 840 } 841 842 /* 843 * At this point, we have the information needed to 844 * determine what to do with the share. 845 * 846 * If smb or nfs is set, we have a new sub-group. 847 * If smb_inherit and/or nfs_inherit is set, then 848 * place on an existing sub-group. If both are set, 849 * the existing sub-group is the closest up the tree. 850 */ 851 if (nfs || smb) { 852 /* 853 * Non-inherited is the straightforward 854 * case. sa_zfs_process_share handles it 855 * directly. Make sure that if the "other" 856 * protocol is inherited, that we treat it as 857 * non-inherited as well. 858 */ 859 if (nfs || nfs_inherited) { 860 err = sa_zfs_process_share(handle, zfsgroup, 861 share, mountpoint, "nfs", 862 0, nfsshareopts, 863 nfssourcestr, dataset); 864 share = sa_find_share(handle, mountpoint); 865 } 866 if (smb || smb_inherited) { 867 err = sa_zfs_process_share(handle, zfsgroup, 868 share, mountpoint, "smb", 869 0, smbshareopts, 870 smbsourcestr, dataset); 871 } 872 } else if (nfs_inherited || smb_inherited) { 873 char *grpdataset; 874 /* 875 * If we only have inherited groups, it is 876 * important to find the closer of the two if 877 * the protocols are set at different 878 * levels. The closest sub-group is the one we 879 * want to work with. 880 */ 881 if (nfs_inherited && smb_inherited) { 882 if (strcmp(nfssourcestr, smbsourcestr) <= 0) 883 grpdataset = nfssourcestr; 884 else 885 grpdataset = smbsourcestr; 886 } else if (nfs_inherited) { 887 grpdataset = nfssourcestr; 888 } else if (smb_inherited) { 889 grpdataset = smbsourcestr; 890 } 891 if (nfs_inherited) { 892 err = sa_zfs_process_share(handle, zfsgroup, 893 share, mountpoint, "nfs", 894 ZPROP_SRC_INHERITED, nfsshareopts, 895 grpdataset, dataset); 896 share = sa_find_share(handle, mountpoint); 897 } 898 if (smb_inherited) { 899 err = sa_zfs_process_share(handle, zfsgroup, 900 share, mountpoint, "smb", 901 ZPROP_SRC_INHERITED, smbshareopts, 902 grpdataset, dataset); 903 } 904 } 905 } 906 /* 907 * Don't need to free the "zlist" variable since it is only a 908 * pointer to a cached value that will be freed when 909 * sa_fini() is called. 910 */ 911 return (err); 912 } 913 914 #define COMMAND "/usr/sbin/zfs" 915 916 /* 917 * sa_zfs_set_sharenfs(group, path, on) 918 * 919 * Update the "sharenfs" property on the path. If on is true, then set 920 * to the properties on the group or "on" if no properties are 921 * defined. Set to "off" if on is false. 922 */ 923 924 int 925 sa_zfs_set_sharenfs(sa_group_t group, char *path, int on) 926 { 927 int ret = SA_NOT_IMPLEMENTED; 928 char *command; 929 930 command = malloc(ZFS_MAXPROPLEN * 2); 931 if (command != NULL) { 932 char *opts = NULL; 933 char *dataset = NULL; 934 FILE *pfile; 935 sa_handle_impl_t impl_handle; 936 /* for now, NFS is always available for "zfs" */ 937 if (on) { 938 opts = sa_proto_legacy_format("nfs", group, 1); 939 if (opts != NULL && strlen(opts) == 0) { 940 free(opts); 941 opts = strdup("on"); 942 } 943 } 944 945 impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 946 assert(impl_handle != NULL); 947 if (impl_handle != NULL) 948 dataset = get_zfs_dataset(impl_handle, path, B_FALSE); 949 else 950 ret = SA_SYSTEM_ERR; 951 952 if (dataset != NULL) { 953 (void) snprintf(command, ZFS_MAXPROPLEN * 2, 954 "%s set sharenfs=\"%s\" %s", COMMAND, 955 opts != NULL ? opts : "off", dataset); 956 pfile = popen(command, "r"); 957 if (pfile != NULL) { 958 ret = pclose(pfile); 959 if (ret != 0) 960 ret = SA_SYSTEM_ERR; 961 } 962 } 963 if (opts != NULL) 964 free(opts); 965 if (dataset != NULL) 966 free(dataset); 967 free(command); 968 } 969 return (ret); 970 } 971 972 /* 973 * add_resources(share, opt) 974 * 975 * Add resource properties to those in "opt". Resources are prefixed 976 * with name=resourcename. 977 */ 978 static char * 979 add_resources(sa_share_t share, char *opt) 980 { 981 char *newopt = NULL; 982 char *propstr; 983 sa_resource_t resource; 984 985 newopt = strdup(opt); 986 if (newopt == NULL) 987 return (newopt); 988 989 for (resource = sa_get_share_resource(share, NULL); 990 resource != NULL; 991 resource = sa_get_next_resource(resource)) { 992 char *name; 993 size_t size; 994 995 name = sa_get_resource_attr(resource, "name"); 996 if (name == NULL) { 997 free(newopt); 998 return (NULL); 999 } 1000 size = strlen(name) + strlen(opt) + sizeof ("name=") + 1; 1001 newopt = calloc(1, size); 1002 if (newopt != NULL) 1003 (void) snprintf(newopt, size, "%s,name=%s", opt, name); 1004 sa_free_attr_string(name); 1005 free(opt); 1006 opt = newopt; 1007 propstr = sa_proto_legacy_format("smb", resource, 0); 1008 if (propstr == NULL) { 1009 free(opt); 1010 return (NULL); 1011 } 1012 size = strlen(propstr) + strlen(opt) + 2; 1013 newopt = calloc(1, size); 1014 if (newopt != NULL) 1015 (void) snprintf(newopt, size, "%s,%s", opt, propstr); 1016 free(opt); 1017 opt = newopt; 1018 } 1019 return (opt); 1020 } 1021 1022 /* 1023 * sa_zfs_set_sharesmb(group, path, on) 1024 * 1025 * Update the "sharesmb" property on the path. If on is true, then set 1026 * to the properties on the group or "on" if no properties are 1027 * defined. Set to "off" if on is false. 1028 */ 1029 1030 int 1031 sa_zfs_set_sharesmb(sa_group_t group, char *path, int on) 1032 { 1033 int ret = SA_NOT_IMPLEMENTED; 1034 char *command; 1035 sa_share_t share; 1036 1037 /* In case SMB not enabled */ 1038 if (sa_get_optionset(group, "smb") == NULL) 1039 return (SA_NOT_SUPPORTED); 1040 1041 command = malloc(ZFS_MAXPROPLEN * 2); 1042 if (command != NULL) { 1043 char *opts = NULL; 1044 char *dataset = NULL; 1045 FILE *pfile; 1046 sa_handle_impl_t impl_handle; 1047 1048 if (on) { 1049 char *newopt; 1050 1051 share = sa_get_share(group, NULL); 1052 opts = sa_proto_legacy_format("smb", share, 1); 1053 if (opts != NULL && strlen(opts) == 0) { 1054 free(opts); 1055 opts = strdup("on"); 1056 } 1057 newopt = add_resources(opts, share); 1058 free(opts); 1059 opts = newopt; 1060 } 1061 1062 impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 1063 assert(impl_handle != NULL); 1064 if (impl_handle != NULL) 1065 dataset = get_zfs_dataset(impl_handle, path, B_FALSE); 1066 else 1067 ret = SA_SYSTEM_ERR; 1068 1069 if (dataset != NULL) { 1070 (void) snprintf(command, ZFS_MAXPROPLEN * 2, 1071 "echo %s set sharesmb=\"%s\" %s", COMMAND, 1072 opts != NULL ? opts : "off", dataset); 1073 pfile = popen(command, "r"); 1074 if (pfile != NULL) { 1075 ret = pclose(pfile); 1076 if (ret != 0) 1077 ret = SA_SYSTEM_ERR; 1078 } 1079 } 1080 if (opts != NULL) 1081 free(opts); 1082 if (dataset != NULL) 1083 free(dataset); 1084 free(command); 1085 } 1086 return (ret); 1087 } 1088 1089 /* 1090 * sa_zfs_update(group) 1091 * 1092 * call back to ZFS to update the share if necessary. 1093 * Don't do it if it isn't a real change. 1094 */ 1095 int 1096 sa_zfs_update(sa_group_t group) 1097 { 1098 sa_optionset_t protopt; 1099 sa_group_t parent; 1100 char *command; 1101 char *optstring; 1102 int ret = SA_OK; 1103 int doupdate = 0; 1104 FILE *pfile; 1105 1106 if (sa_is_share(group)) 1107 parent = sa_get_parent_group(group); 1108 else 1109 parent = group; 1110 1111 if (parent != NULL) { 1112 command = malloc(ZFS_MAXPROPLEN * 2); 1113 if (command == NULL) 1114 return (SA_NO_MEMORY); 1115 1116 *command = '\0'; 1117 for (protopt = sa_get_optionset(parent, NULL); protopt != NULL; 1118 protopt = sa_get_next_optionset(protopt)) { 1119 1120 char *proto = sa_get_optionset_attr(protopt, "type"); 1121 char *path; 1122 char *dataset = NULL; 1123 char *zfsopts = NULL; 1124 1125 if (sa_is_share(group)) { 1126 path = sa_get_share_attr((sa_share_t)group, 1127 "path"); 1128 if (path != NULL) { 1129 sa_handle_impl_t impl_handle; 1130 1131 impl_handle = sa_find_group_handle( 1132 group); 1133 if (impl_handle != NULL) 1134 dataset = get_zfs_dataset( 1135 impl_handle, path, B_FALSE); 1136 else 1137 ret = SA_SYSTEM_ERR; 1138 1139 sa_free_attr_string(path); 1140 } 1141 } else { 1142 dataset = sa_get_group_attr(group, "name"); 1143 } 1144 /* update only when there is an optstring found */ 1145 doupdate = 0; 1146 if (proto != NULL && dataset != NULL) { 1147 optstring = sa_proto_legacy_format(proto, 1148 group, 1); 1149 zfsopts = get_zfs_property(dataset, 1150 ZFS_PROP_SHARENFS); 1151 1152 if (optstring != NULL && zfsopts != NULL) { 1153 if (strcmp(optstring, zfsopts) != 0) 1154 doupdate++; 1155 } 1156 if (doupdate) { 1157 if (optstring != NULL && 1158 strlen(optstring) > 0) { 1159 (void) snprintf(command, 1160 ZFS_MAXPROPLEN * 2, 1161 "%s set share%s=%s %s", 1162 COMMAND, proto, 1163 optstring, dataset); 1164 } else { 1165 (void) snprintf(command, 1166 ZFS_MAXPROPLEN * 2, 1167 "%s set share%s=on %s", 1168 COMMAND, proto, 1169 dataset); 1170 } 1171 pfile = popen(command, "r"); 1172 if (pfile != NULL) 1173 ret = pclose(pfile); 1174 switch (ret) { 1175 default: 1176 case 1: 1177 ret = SA_SYSTEM_ERR; 1178 break; 1179 case 2: 1180 ret = SA_SYNTAX_ERR; 1181 break; 1182 case 0: 1183 break; 1184 } 1185 } 1186 if (optstring != NULL) 1187 free(optstring); 1188 if (zfsopts != NULL) 1189 free(zfsopts); 1190 } 1191 if (proto != NULL) 1192 sa_free_attr_string(proto); 1193 if (dataset != NULL) 1194 free(dataset); 1195 } 1196 free(command); 1197 } 1198 return (ret); 1199 } 1200 1201 /* 1202 * sa_group_is_zfs(group) 1203 * 1204 * Given the group, determine if the zfs attribute is set. 1205 */ 1206 1207 int 1208 sa_group_is_zfs(sa_group_t group) 1209 { 1210 char *zfs; 1211 int ret = 0; 1212 1213 zfs = sa_get_group_attr(group, "zfs"); 1214 if (zfs != NULL) { 1215 ret = 1; 1216 sa_free_attr_string(zfs); 1217 } 1218 return (ret); 1219 } 1220 1221 /* 1222 * sa_path_is_zfs(path) 1223 * 1224 * Check to see if the file system path represents is of type "zfs". 1225 */ 1226 1227 int 1228 sa_path_is_zfs(char *path) 1229 { 1230 char *fstype; 1231 int ret = 0; 1232 1233 fstype = sa_fstype(path); 1234 if (fstype != NULL && strcmp(fstype, "zfs") == 0) 1235 ret = 1; 1236 if (fstype != NULL) 1237 sa_free_fstype(fstype); 1238 return (ret); 1239 } 1240 1241 int 1242 sa_sharetab_fill_zfs(sa_share_t share, share_t *sh, char *proto) 1243 { 1244 char *path; 1245 1246 /* Make sure path is valid */ 1247 1248 path = sa_get_share_attr(share, "path"); 1249 if (path != NULL) { 1250 (void) memset(sh, 0, sizeof (sh)); 1251 (void) sa_fillshare(share, proto, sh); 1252 sa_free_attr_string(path); 1253 return (0); 1254 } else 1255 return (1); 1256 } 1257 1258 #define SMAX(i, j) \ 1259 if ((j) > (i)) { \ 1260 (i) = (j); \ 1261 } 1262 1263 int 1264 sa_share_zfs(sa_share_t share, sa_resource_t resource, char *path, share_t *sh, 1265 void *exportdata, zfs_share_op_t operation) 1266 { 1267 libzfs_handle_t *libhandle; 1268 sa_group_t group; 1269 sa_handle_t sahandle; 1270 char *dataset; 1271 int err = EINVAL; 1272 int i, j; 1273 char newpath[MAXPATHLEN]; 1274 char *pathp; 1275 1276 /* 1277 * First find the dataset name 1278 */ 1279 if ((group = sa_get_parent_group(share)) == NULL) { 1280 return (EINVAL); 1281 } 1282 if ((sahandle = sa_find_group_handle(group)) == NULL) { 1283 return (EINVAL); 1284 } 1285 1286 /* 1287 * If get_zfs_dataset fails, see if it is a subdirectory 1288 */ 1289 1290 pathp = path; 1291 while ((dataset = get_zfs_dataset(sahandle, pathp, B_TRUE)) == NULL) { 1292 char *p; 1293 1294 if (pathp == path) { 1295 (void) strlcpy(newpath, path, sizeof (newpath)); 1296 pathp = newpath; 1297 } 1298 1299 /* 1300 * Make sure only one leading '/' This condition came 1301 * about when using HAStoragePlus which insisted on 1302 * putting an extra leading '/' in the ZFS path 1303 * name. The problem is fixed in other areas, but this 1304 * will catch any other ways that a double slash might 1305 * get introduced. 1306 */ 1307 while (*pathp == '/' && *(pathp + 1) == '/') 1308 pathp++; 1309 1310 /* 1311 * chop off part of path, but if we are at root then 1312 * make sure path is a / 1313 */ 1314 if ((strlen(pathp) > 1) && (p = strrchr(pathp, '/'))) { 1315 if (pathp == p) { 1316 *(p + 1) = '\0'; /* skip over /, root case */ 1317 } else { 1318 *p = '\0'; 1319 } 1320 } else { 1321 return (EINVAL); 1322 } 1323 } 1324 1325 libhandle = libzfs_init(); 1326 if (libhandle != NULL) { 1327 char *resource_name; 1328 1329 i = (sh->sh_path ? strlen(sh->sh_path) : 0); 1330 sh->sh_size = i; 1331 1332 j = (sh->sh_res ? strlen(sh->sh_res) : 0); 1333 sh->sh_size += j; 1334 SMAX(i, j); 1335 1336 j = (sh->sh_fstype ? strlen(sh->sh_fstype) : 0); 1337 sh->sh_size += j; 1338 SMAX(i, j); 1339 1340 j = (sh->sh_opts ? strlen(sh->sh_opts) : 0); 1341 sh->sh_size += j; 1342 SMAX(i, j); 1343 1344 j = (sh->sh_descr ? strlen(sh->sh_descr) : 0); 1345 sh->sh_size += j; 1346 SMAX(i, j); 1347 1348 resource_name = sa_get_resource_attr(resource, "name"); 1349 1350 err = zfs_deleg_share_nfs(libhandle, dataset, path, 1351 resource_name, exportdata, sh, i, operation); 1352 if (err == SA_OK) 1353 sa_update_sharetab_ts(sahandle); 1354 else 1355 err = errno; 1356 if (resource_name) 1357 sa_free_attr_string(resource_name); 1358 1359 libzfs_fini(libhandle); 1360 } 1361 free(dataset); 1362 return (err); 1363 } 1364 1365 /* 1366 * sa_get_zfs_handle(handle) 1367 * 1368 * Given an sa_handle_t, return the libzfs_handle_t *. This is only 1369 * used internally by libzfs. Needed in order to avoid including 1370 * libshare_impl.h in libzfs. 1371 */ 1372 1373 libzfs_handle_t * 1374 sa_get_zfs_handle(sa_handle_t handle) 1375 { 1376 sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 1377 1378 return (implhandle->zfs_libhandle); 1379 } 1380 1381 /* 1382 * sa_get_zfs_info(libzfs, path, mountpoint, dataset) 1383 * 1384 * Find the ZFS dataset and mountpoint for a given path 1385 */ 1386 int 1387 sa_zfs_get_info(libzfs_handle_t *libzfs, char *path, char *mountpointp, 1388 char *datasetp) 1389 { 1390 get_all_cbdata_t cb = { 0 }; 1391 int i; 1392 char mountpoint[ZFS_MAXPROPLEN]; 1393 char dataset[ZFS_MAXPROPLEN]; 1394 char canmount[ZFS_MAXPROPLEN]; 1395 char *dp; 1396 int count; 1397 int ret = 0; 1398 1399 cb.cb_types = ZFS_TYPE_FILESYSTEM; 1400 1401 if (libzfs == NULL) 1402 return (0); 1403 1404 (void) zfs_iter_root(libzfs, get_one_filesystem, &cb); 1405 count = cb.cb_used; 1406 1407 qsort(cb.cb_handles, count, sizeof (void *), mountpoint_compare); 1408 for (i = 0; i < count; i++) { 1409 /* must have a mountpoint */ 1410 if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_MOUNTPOINT, 1411 mountpoint, sizeof (mountpoint), 1412 NULL, NULL, 0, B_FALSE) != 0) { 1413 /* no mountpoint */ 1414 continue; 1415 } 1416 1417 /* mountpoint must be a path */ 1418 if (strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) == 0 || 1419 strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 1420 /* 1421 * Search mmttab for mountpoint 1422 */ 1423 1424 if (get_legacy_mountpoint(path, dataset, 1425 ZFS_MAXPROPLEN, mountpoint, 1426 ZFS_MAXPROPLEN) == 0) { 1427 ret = 1; 1428 break; 1429 } 1430 continue; 1431 } 1432 1433 /* canmount must be set */ 1434 canmount[0] = '\0'; 1435 if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_CANMOUNT, canmount, 1436 sizeof (canmount), NULL, NULL, 0, B_FALSE) != 0 || 1437 strcmp(canmount, "off") == 0) 1438 continue; 1439 1440 /* 1441 * have a mountable handle but want to skip those marked none 1442 * and legacy 1443 */ 1444 if (strcmp(mountpoint, path) == 0) { 1445 dp = (char *)zfs_get_name(cb.cb_handles[i]); 1446 if (dp != NULL) { 1447 if (datasetp != NULL) 1448 (void) strcpy(datasetp, dp); 1449 if (mountpointp != NULL) 1450 (void) strcpy(mountpointp, mountpoint); 1451 ret = 1; 1452 } 1453 break; 1454 } 1455 1456 } 1457 1458 return (ret); 1459 } 1460 1461 /* 1462 * This method builds values for "sharesmb" property from the 1463 * nvlist argument. The values are returned in sharesmb_val variable. 1464 */ 1465 static int 1466 sa_zfs_sprintf_new_prop(nvlist_t *nvl, char *sharesmb_val) 1467 { 1468 char cur_val[MAXPATHLEN]; 1469 char *name, *val; 1470 nvpair_t *cur; 1471 int err = 0; 1472 1473 cur = nvlist_next_nvpair(nvl, NULL); 1474 while (cur != NULL) { 1475 name = nvpair_name(cur); 1476 err = nvpair_value_string(cur, &val); 1477 if ((err != 0) || (name == NULL) || (val == NULL)) 1478 return (-1); 1479 1480 (void) snprintf(cur_val, MAXPATHLEN, "%s=%s,", name, val); 1481 (void) strlcat(sharesmb_val, cur_val, MAXPATHLEN); 1482 1483 cur = nvlist_next_nvpair(nvl, cur); 1484 } 1485 1486 return (0); 1487 } 1488 1489 /* 1490 * This method builds values for "sharesmb" property from values 1491 * already existing on the share. The properties set via sa_zfs_sprint_new_prop 1492 * method are passed in sharesmb_val. If a existing property is already 1493 * set via sa_zfs_sprint_new_prop method, then they are not appended 1494 * to the sharesmb_val string. The returned sharesmb_val string is a combination 1495 * of new and existing values for 'sharesmb' property. 1496 */ 1497 static int 1498 sa_zfs_sprintf_existing_prop(zfs_handle_t *handle, char *sharesmb_val) 1499 { 1500 char shareopts[ZFS_MAXPROPLEN], cur_val[MAXPATHLEN]; 1501 char *token, *last, *value; 1502 1503 if (zfs_prop_get(handle, ZFS_PROP_SHARESMB, shareopts, 1504 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0) 1505 return (-1); 1506 1507 if (strstr(shareopts, "=") == NULL) 1508 return (0); 1509 1510 for (token = strtok_r(shareopts, ",", &last); token != NULL; 1511 token = strtok_r(NULL, ",", &last)) { 1512 value = strchr(token, '='); 1513 if (value == NULL) 1514 return (-1); 1515 *value++ = '\0'; 1516 1517 (void) snprintf(cur_val, MAXPATHLEN, "%s=", token); 1518 if (strstr(sharesmb_val, cur_val) == NULL) { 1519 (void) strlcat(cur_val, value, MAXPATHLEN); 1520 (void) strlcat(cur_val, ",", MAXPATHLEN); 1521 (void) strlcat(sharesmb_val, cur_val, MAXPATHLEN); 1522 } 1523 } 1524 1525 return (0); 1526 } 1527 1528 /* 1529 * Sets the share properties on a ZFS share. For now, this method sets only 1530 * the "sharesmb" property. 1531 * 1532 * This method includes building a comma seperated name-value string to be 1533 * set on the "sharesmb" property of a ZFS share. This name-value string is 1534 * build in 2 steps: 1535 * - New property values given as name-value pair are set first. 1536 * - Existing optionset properties, which are not part of the new properties 1537 * passed in step 1, are appended to the newly set properties. 1538 */ 1539 int 1540 sa_zfs_setprop(sa_handle_t handle, char *path, nvlist_t *nvl) 1541 { 1542 zfs_handle_t *z_fs; 1543 libzfs_handle_t *z_lib; 1544 char sharesmb_val[MAXPATHLEN]; 1545 char *dataset, *lastcomma; 1546 1547 if (nvlist_empty(nvl)) 1548 return (0); 1549 1550 if ((handle == NULL) || (path == NULL)) 1551 return (-1); 1552 1553 if ((dataset = get_zfs_dataset(handle, path, B_FALSE)) == NULL) 1554 return (-1); 1555 1556 if ((z_lib = libzfs_init()) == NULL) { 1557 free(dataset); 1558 return (-1); 1559 } 1560 1561 z_fs = zfs_open(z_lib, dataset, ZFS_TYPE_DATASET); 1562 if (z_fs == NULL) { 1563 free(dataset); 1564 libzfs_fini(z_lib); 1565 return (-1); 1566 } 1567 1568 bzero(sharesmb_val, MAXPATHLEN); 1569 if (sa_zfs_sprintf_new_prop(nvl, sharesmb_val) != 0) { 1570 free(dataset); 1571 zfs_close(z_fs); 1572 libzfs_fini(z_lib); 1573 return (-1); 1574 } 1575 1576 if (sa_zfs_sprintf_existing_prop(z_fs, sharesmb_val) != 0) { 1577 free(dataset); 1578 zfs_close(z_fs); 1579 libzfs_fini(z_lib); 1580 return (-1); 1581 } 1582 1583 lastcomma = strrchr(sharesmb_val, ','); 1584 if ((lastcomma != NULL) && (lastcomma[1] == '\0')) 1585 *lastcomma = '\0'; 1586 1587 (void) zfs_prop_set(z_fs, zfs_prop_to_name(ZFS_PROP_SHARESMB), 1588 sharesmb_val); 1589 free(dataset); 1590 zfs_close(z_fs); 1591 libzfs_fini(z_lib); 1592 1593 return (0); 1594 }