Print this page
4095 minor cleanup up libshare

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libshare/common/libshare_zfs.c
          +++ new/usr/src/lib/libshare/common/libshare_zfs.c
↓ open down ↓ 13 lines elided ↑ open up ↑
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  24      - */
  25      -/*
  26   24   * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
       25 + * Copyright (c) 2013 RackTop Systems.
  27   26   */
  28   27  
  29   28  #include <stdio.h>
  30   29  #include <libzfs.h>
  31   30  #include <string.h>
  32   31  #include <strings.h>
  33   32  #include <errno.h>
  34   33  #include <libshare.h>
  35   34  #include "libshare_impl.h"
  36   35  #include <libintl.h>
  37   36  #include <sys/mnttab.h>
  38   37  #include <sys/mntent.h>
  39   38  
  40   39  extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
  41   40  extern sa_group_t _sa_create_zfs_group(sa_group_t, char *);
  42   41  extern char *sa_fstype(char *);
  43   42  extern void set_node_attr(void *, char *, char *);
  44      -extern int sa_is_share(void *);
       43 +extern boolean_t sa_is_share(void *);
  45   44  extern void sa_update_sharetab_ts(sa_handle_t);
  46   45  
  47   46  /*
  48   47   * File system specific code for ZFS. The original code was stolen
  49   48   * from the "zfs" command and modified to better suit this library's
  50   49   * usage.
  51   50   */
  52   51  
  53   52  typedef struct get_all_cbdata {
  54   53          zfs_handle_t    **cb_handles;
  55   54          size_t          cb_alloc;
  56   55          size_t          cb_used;
  57   56          uint_t          cb_types;
  58   57  } get_all_cbdata_t;
  59   58  
  60   59  /*
  61      - * sa_zfs_init(impl_handle)
       60 + * sa_zfs_init(handle)
  62   61   *
  63   62   * Initialize an access handle into libzfs.  The handle needs to stay
  64   63   * around until sa_zfs_fini() in order to maintain the cache of
  65   64   * mounts.
  66   65   */
  67   66  
  68   67  int
  69      -sa_zfs_init(sa_handle_impl_t impl_handle)
       68 +sa_zfs_init(sa_handle_t handle)
  70   69  {
  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);
       70 +        handle->zfs_libhandle = libzfs_init();
       71 +        if (handle->zfs_libhandle != NULL) {
       72 +                libzfs_print_on_error(handle->zfs_libhandle, B_TRUE);
  74   73                  return (B_TRUE);
  75   74          }
  76   75          return (B_FALSE);
  77   76  }
  78   77  
  79   78  /*
  80      - * sa_zfs_fini(impl_handle)
       79 + * sa_zfs_fini(handle)
  81   80   *
  82   81   * cleanup data structures and the libzfs handle used for accessing
  83   82   * zfs file share info.
  84   83   */
  85   84  
  86   85  void
  87      -sa_zfs_fini(sa_handle_impl_t impl_handle)
       86 +sa_zfs_fini(sa_handle_t handle)
  88   87  {
  89      -        if (impl_handle->zfs_libhandle != NULL) {
  90      -                if (impl_handle->zfs_list != NULL) {
  91      -                        zfs_handle_t **zhp = impl_handle->zfs_list;
       88 +        if (handle->zfs_libhandle != NULL) {
       89 +                if (handle->zfs_list != NULL) {
       90 +                        zfs_handle_t **zhp = handle->zfs_list;
  92   91                          size_t i;
  93   92  
  94   93                          /*
  95   94                           * Contents of zfs_list need to be freed so we
  96   95                           * don't lose ZFS handles.
  97   96                           */
  98      -                        for (i = 0; i < impl_handle->zfs_list_count; i++) {
       97 +                        for (i = 0; i < handle->zfs_list_count; i++) {
  99   98                                  zfs_close(zhp[i]);
 100   99                          }
 101      -                        free(impl_handle->zfs_list);
 102      -                        impl_handle->zfs_list = NULL;
 103      -                        impl_handle->zfs_list_count = 0;
      100 +                        free(handle->zfs_list);
      101 +                        handle->zfs_list = NULL;
      102 +                        handle->zfs_list_count = 0;
 104  103                  }
 105  104  
 106      -                libzfs_fini(impl_handle->zfs_libhandle);
 107      -                impl_handle->zfs_libhandle = NULL;
      105 +                libzfs_fini(handle->zfs_libhandle);
      106 +                handle->zfs_libhandle = NULL;
 108  107          }
 109  108  }
 110  109  
 111  110  /*
 112  111   * get_one_filesystem(zfs_handle_t, data)
 113  112   *
 114  113   * an iterator function called while iterating through the ZFS
 115  114   * root. It accumulates into an array of file system handles that can
 116  115   * be used to derive info about those file systems.
 117  116   *
↓ open down ↓ 58 lines elided ↑ open up ↑
 176  175  /*
 177  176   * get_all_filesystems(zfs_handle_t ***fslist, size_t *count)
 178  177   *
 179  178   * iterate through all ZFS file systems starting at the root. Returns
 180  179   * a count and an array of handle pointers. Allocating is only done
 181  180   * once. The caller does not need to free since it will be done at
 182  181   * sa_zfs_fini() time.
 183  182   */
 184  183  
 185  184  static void
 186      -get_all_filesystems(sa_handle_impl_t impl_handle,
      185 +get_all_filesystems(sa_handle_t handle,
 187  186                          zfs_handle_t ***fslist, size_t *count)
 188  187  {
 189  188          get_all_cbdata_t cb = { 0 };
 190  189          cb.cb_types = ZFS_TYPE_FILESYSTEM;
 191  190  
 192      -        if (impl_handle->zfs_list != NULL) {
 193      -                *fslist = impl_handle->zfs_list;
 194      -                *count = impl_handle->zfs_list_count;
      191 +        if (handle->zfs_list != NULL) {
      192 +                *fslist = handle->zfs_list;
      193 +                *count = handle->zfs_list_count;
 195  194                  return;
 196  195          }
 197  196  
 198      -        (void) zfs_iter_root(impl_handle->zfs_libhandle,
      197 +        (void) zfs_iter_root(handle->zfs_libhandle,
 199  198              get_one_filesystem, &cb);
 200  199  
 201      -        impl_handle->zfs_list = *fslist = cb.cb_handles;
 202      -        impl_handle->zfs_list_count = *count = cb.cb_used;
      200 +        handle->zfs_list = *fslist = cb.cb_handles;
      201 +        handle->zfs_list_count = *count = cb.cb_used;
 203  202  }
 204  203  
 205  204  /*
 206  205   * mountpoint_compare(a, b)
 207  206   *
 208  207   * compares the mountpoint on two zfs file systems handles.
 209  208   * returns values following strcmp() model.
 210  209   */
 211  210  
 212  211  static int
↓ open down ↓ 41 lines elided ↑ open up ↑
 254  253                                  (void) strlcpy(dataset, entry.mnt_special,
 255  254                                      dlen);
 256  255                          break;
 257  256                  }
 258  257          }
 259  258          (void) fclose(fp);
 260  259          return (1);
 261  260  }
 262  261  
 263  262  /*
 264      - * get_zfs_dataset(impl_handle, path)
      263 + * get_zfs_dataset(handle, path)
 265  264   *
 266  265   * get the name of the ZFS dataset the path is equivalent to.  The
 267  266   * dataset name is used for get/set of ZFS properties since libzfs
 268  267   * requires a dataset to do a zfs_open().
 269  268   */
 270  269  
 271  270  static char *
 272      -get_zfs_dataset(sa_handle_impl_t impl_handle, char *path,
      271 +get_zfs_dataset(sa_handle_t handle, char *path,
 273  272      boolean_t search_mnttab)
 274  273  {
 275  274          size_t i, count = 0;
 276  275          char *dataset = NULL;
 277  276          zfs_handle_t **zlist;
 278  277          char mountpoint[ZFS_MAXPROPLEN];
 279  278          char canmount[ZFS_MAXPROPLEN];
 280  279  
 281      -        get_all_filesystems(impl_handle, &zlist, &count);
      280 +        get_all_filesystems(handle, &zlist, &count);
 282  281          qsort(zlist, count, sizeof (void *), mountpoint_compare);
 283  282          for (i = 0; i < count; i++) {
 284  283                  /* must have a mountpoint */
 285  284                  if (zfs_prop_get(zlist[i], ZFS_PROP_MOUNTPOINT, mountpoint,
 286  285                      sizeof (mountpoint), NULL, NULL, 0, B_FALSE) != 0) {
 287  286                          /* no mountpoint */
 288  287                          continue;
 289  288                  }
 290  289  
 291  290                  /* mountpoint must be a path */
↓ open down ↓ 36 lines elided ↑ open up ↑
 328  327          return (dataset);
 329  328  }
 330  329  
 331  330  /*
 332  331   * get_zfs_property(dataset, property)
 333  332   *
 334  333   * Get the file system property specified from the ZFS dataset.
 335  334   */
 336  335  
 337  336  static char *
 338      -get_zfs_property(char *dataset, zfs_prop_t property)
      337 +get_zfs_property(sa_handle_t handle, char *dataset, zfs_prop_t property)
 339  338  {
 340      -        zfs_handle_t *handle = NULL;
      339 +        zfs_handle_t *z_fs;
 341  340          char shareopts[ZFS_MAXPROPLEN];
 342      -        libzfs_handle_t *libhandle;
 343  341  
 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);
      342 +        z_fs = zfs_open(handle->zfs_libhandle, dataset, ZFS_TYPE_FILESYSTEM);
      343 +        if (z_fs != NULL) {
      344 +                if (zfs_prop_get(z_fs, property, shareopts,
      345 +                    sizeof (shareopts), NULL, NULL, 0,
      346 +                    B_FALSE) == 0) {
      347 +                        zfs_close(z_fs);
      348 +                        return (strdup(shareopts));
 356  349                  }
 357      -                libzfs_fini(libhandle);
      350 +                zfs_close(z_fs);
 358  351          }
 359  352          return (NULL);
 360  353  }
 361  354  
 362  355  /*
 363  356   * sa_zfs_is_shared(handle, path)
 364  357   *
 365  358   * Check to see if the ZFS path provided has the sharenfs option set
 366  359   * or not.
 367  360   */
 368  361  
 369      -int
 370      -sa_zfs_is_shared(sa_handle_t sahandle, char *path)
      362 +boolean_t
      363 +sa_zfs_is_shared(sa_handle_t handle, char *path)
 371  364  {
 372      -        int ret = 0;
      365 +        int ret = B_FALSE;
 373  366          char *dataset;
 374      -        zfs_handle_t *handle = NULL;
      367 +        zfs_handle_t *z_fs = NULL;
 375  368          char shareopts[ZFS_MAXPROPLEN];
 376      -        libzfs_handle_t *libhandle;
 377  369  
 378      -        dataset = get_zfs_dataset((sa_handle_t)sahandle, path, B_FALSE);
      370 +        dataset = get_zfs_dataset(handle, path, B_FALSE);
 379  371          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);
      372 +                z_fs = zfs_open(handle->zfs_libhandle, dataset,
      373 +                    ZFS_TYPE_FILESYSTEM);
      374 +                if (z_fs != NULL) {
      375 +                        if (zfs_prop_get(z_fs, ZFS_PROP_SHARENFS,
      376 +                            shareopts, sizeof (shareopts), NULL, NULL,
      377 +                            0, B_FALSE) == 0 &&
      378 +                            strcmp(shareopts, "off") != 0) {
      379 +                                ret = B_TRUE; /* it is shared */
 392  380                          }
 393      -                        libzfs_fini(libhandle);
      381 +                        zfs_close(z_fs);
 394  382                  }
 395  383                  free(dataset);
 396  384          }
 397  385          return (ret);
 398  386  }
 399  387  
 400  388  /*
 401  389   * find_or_create_group(handle, groupname, proto, *err)
 402  390   *
 403  391   * While walking the ZFS tree, we need to add shares to a defined
↓ open down ↓ 352 lines elided ↑ open up ↑
 756  744          char nfssourcestr[ZFS_MAXPROPLEN];
 757  745          char smbsourcestr[ZFS_MAXPROPLEN];
 758  746          char mountpoint[ZFS_MAXPROPLEN];
 759  747          size_t count = 0, i;
 760  748          libzfs_handle_t *zfs_libhandle;
 761  749          int err = SA_OK;
 762  750  
 763  751          /*
 764  752           * If we can't access libzfs, don't bother doing anything.
 765  753           */
 766      -        zfs_libhandle = ((sa_handle_impl_t)handle)->zfs_libhandle;
      754 +        zfs_libhandle = handle->zfs_libhandle;
 767  755          if (zfs_libhandle == NULL)
 768  756                  return (SA_SYSTEM_ERR);
 769  757  
 770  758          zfsgroup = find_or_create_group(handle, groupname, NULL, &err);
 771  759          /* Not an error, this could be a legacy condition */
 772  760          if (zfsgroup == NULL)
 773  761                  return (SA_OK);
 774  762  
 775  763          /*
 776  764           * need to walk the mounted ZFS pools and datasets to
 777  765           * find shares that are possible.
 778  766           */
 779      -        get_all_filesystems((sa_handle_impl_t)handle, &zlist, &count);
      767 +        get_all_filesystems(handle, &zlist, &count);
 780  768          qsort(zlist, count, sizeof (void *), mountpoint_compare);
 781  769  
 782  770          for (i = 0; i < count; i++) {
 783  771                  char *dataset;
 784  772  
 785  773                  source = ZPROP_SRC_ALL;
 786  774                  /* If no mountpoint, skip. */
 787  775                  if (zfs_prop_get(zlist[i], ZFS_PROP_MOUNTPOINT,
 788  776                      mountpoint, sizeof (mountpoint), NULL, NULL, 0,
 789  777                      B_FALSE) != 0)
↓ open down ↓ 135 lines elided ↑ open up ↑
 925  913  sa_zfs_set_sharenfs(sa_group_t group, char *path, int on)
 926  914  {
 927  915          int ret = SA_NOT_IMPLEMENTED;
 928  916          char *command;
 929  917  
 930  918          command = malloc(ZFS_MAXPROPLEN * 2);
 931  919          if (command != NULL) {
 932  920                  char *opts = NULL;
 933  921                  char *dataset = NULL;
 934  922                  FILE *pfile;
 935      -                sa_handle_impl_t impl_handle;
      923 +                sa_handle_t handle;
 936  924                  /* for now, NFS is always available for "zfs" */
 937  925                  if (on) {
 938  926                          opts = sa_proto_legacy_format("nfs", group, 1);
 939  927                          if (opts != NULL && strlen(opts) == 0) {
 940  928                                  free(opts);
 941  929                                  opts = strdup("on");
 942  930                          }
 943  931                  }
 944  932  
 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);
      933 +                handle = sa_find_group_handle(group);
      934 +                assert(handle != NULL);
      935 +                if (handle != NULL)
      936 +                        dataset = get_zfs_dataset(handle, path, B_FALSE);
 949  937                  else
 950  938                          ret = SA_SYSTEM_ERR;
 951  939  
 952  940                  if (dataset != NULL) {
 953  941                          (void) snprintf(command, ZFS_MAXPROPLEN * 2,
 954  942                              "%s set sharenfs=\"%s\" %s", COMMAND,
 955  943                              opts != NULL ? opts : "off", dataset);
 956  944                          pfile = popen(command, "r");
 957  945                          if (pfile != NULL) {
 958  946                                  ret = pclose(pfile);
↓ open down ↓ 77 lines elided ↑ open up ↑
1036 1024  
1037 1025          /* In case SMB not enabled */
1038 1026          if (sa_get_optionset(group, "smb") == NULL)
1039 1027                  return (SA_NOT_SUPPORTED);
1040 1028  
1041 1029          command = malloc(ZFS_MAXPROPLEN * 2);
1042 1030          if (command != NULL) {
1043 1031                  char *opts = NULL;
1044 1032                  char *dataset = NULL;
1045 1033                  FILE *pfile;
1046      -                sa_handle_impl_t impl_handle;
     1034 +                sa_handle_t handle;
1047 1035  
1048 1036                  if (on) {
1049 1037                          char *newopt;
1050 1038  
1051 1039                          share = sa_get_share(group, NULL);
1052 1040                          opts = sa_proto_legacy_format("smb", share, 1);
1053 1041                          if (opts != NULL && strlen(opts) == 0) {
1054 1042                                  free(opts);
1055 1043                                  opts = strdup("on");
1056 1044                          }
1057 1045                          newopt = add_resources(opts, share);
1058 1046                          free(opts);
1059 1047                          opts = newopt;
1060 1048                  }
1061 1049  
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);
     1050 +                handle = sa_find_group_handle(group);
     1051 +                assert(handle != NULL);
     1052 +                if (handle != NULL)
     1053 +                        dataset = get_zfs_dataset(handle, path, B_FALSE);
1066 1054                  else
1067 1055                          ret = SA_SYSTEM_ERR;
1068 1056  
1069 1057                  if (dataset != NULL) {
1070 1058                          (void) snprintf(command, ZFS_MAXPROPLEN * 2,
1071 1059                              "echo %s set sharesmb=\"%s\" %s", COMMAND,
1072 1060                              opts != NULL ? opts : "off", dataset);
1073 1061                          pfile = popen(command, "r");
1074 1062                          if (pfile != NULL) {
1075 1063                                  ret = pclose(pfile);
↓ open down ↓ 43 lines elided ↑ open up ↑
1119 1107  
1120 1108                          char *proto = sa_get_optionset_attr(protopt, "type");
1121 1109                          char *path;
1122 1110                          char *dataset = NULL;
1123 1111                          char *zfsopts = NULL;
1124 1112  
1125 1113                          if (sa_is_share(group)) {
1126 1114                                  path = sa_get_share_attr((sa_share_t)group,
1127 1115                                      "path");
1128 1116                                  if (path != NULL) {
1129      -                                        sa_handle_impl_t impl_handle;
     1117 +                                        sa_handle_t handle;
1130 1118  
1131      -                                        impl_handle = sa_find_group_handle(
     1119 +                                        handle = sa_find_group_handle(
1132 1120                                              group);
1133      -                                        if (impl_handle != NULL)
     1121 +                                        if (handle != NULL)
1134 1122                                                  dataset = get_zfs_dataset(
1135      -                                                    impl_handle, path, B_FALSE);
     1123 +                                                    handle, path, B_FALSE);
1136 1124                                          else
1137 1125                                                  ret = SA_SYSTEM_ERR;
1138 1126  
1139 1127                                          sa_free_attr_string(path);
1140 1128                                  }
1141 1129                          } else {
1142 1130                                  dataset = sa_get_group_attr(group, "name");
1143 1131                          }
1144 1132                          /* update only when there is an optstring found */
1145 1133                          doupdate = 0;
1146 1134                          if (proto != NULL && dataset != NULL) {
     1135 +                                sa_handle_t handle;
     1136 +
1147 1137                                  optstring = sa_proto_legacy_format(proto,
1148 1138                                      group, 1);
1149      -                                zfsopts = get_zfs_property(dataset,
     1139 +                                handle = sa_find_group_handle(group);
     1140 +                                zfsopts = get_zfs_property(handle, dataset,
1150 1141                                      ZFS_PROP_SHARENFS);
1151 1142  
1152 1143                                  if (optstring != NULL && zfsopts != NULL) {
1153 1144                                          if (strcmp(optstring, zfsopts) != 0)
1154 1145                                                  doupdate++;
1155 1146                                  }
1156 1147                                  if (doupdate) {
1157 1148                                          if (optstring != NULL &&
1158 1149                                              strlen(optstring) > 0) {
1159 1150                                                  (void) snprintf(command,
↓ open down ↓ 37 lines elided ↑ open up ↑
1197 1188          }
1198 1189          return (ret);
1199 1190  }
1200 1191  
1201 1192  /*
1202 1193   * sa_group_is_zfs(group)
1203 1194   *
1204 1195   * Given the group, determine if the zfs attribute is set.
1205 1196   */
1206 1197  
1207      -int
     1198 +boolean_t
1208 1199  sa_group_is_zfs(sa_group_t group)
1209 1200  {
1210 1201          char *zfs;
1211      -        int ret = 0;
     1202 +        int ret = B_FALSE;
1212 1203  
1213 1204          zfs = sa_get_group_attr(group, "zfs");
1214 1205          if (zfs != NULL) {
1215      -                ret = 1;
     1206 +                ret = B_TRUE;
1216 1207                  sa_free_attr_string(zfs);
1217 1208          }
1218 1209          return (ret);
1219 1210  }
1220 1211  
1221 1212  /*
1222 1213   * sa_path_is_zfs(path)
1223 1214   *
1224 1215   * Check to see if the file system path represents is of type "zfs".
1225 1216   */
1226 1217  
1227      -int
     1218 +boolean_t
1228 1219  sa_path_is_zfs(char *path)
1229 1220  {
1230 1221          char *fstype;
1231      -        int ret = 0;
     1222 +        int ret = B_FALSE;
1232 1223  
1233 1224          fstype = sa_fstype(path);
1234 1225          if (fstype != NULL && strcmp(fstype, "zfs") == 0)
1235      -                ret = 1;
     1226 +                ret = B_TRUE;
1236 1227          if (fstype != NULL)
1237 1228                  sa_free_fstype(fstype);
1238 1229          return (ret);
1239 1230  }
1240 1231  
1241 1232  int
1242 1233  sa_sharetab_fill_zfs(sa_share_t share, share_t *sh, char *proto)
1243 1234  {
1244 1235          char *path;
1245 1236  
↓ open down ↓ 11 lines elided ↑ open up ↑
1257 1248  
1258 1249  #define SMAX(i, j)      \
1259 1250          if ((j) > (i)) { \
1260 1251                  (i) = (j); \
1261 1252          }
1262 1253  
1263 1254  int
1264 1255  sa_share_zfs(sa_share_t share, sa_resource_t resource, char *path, share_t *sh,
1265 1256      void *exportdata, zfs_share_op_t operation)
1266 1257  {
1267      -        libzfs_handle_t *libhandle;
1268 1258          sa_group_t group;
1269      -        sa_handle_t sahandle;
     1259 +        sa_handle_t handle;
1270 1260          char *dataset;
1271 1261          int err = EINVAL;
1272 1262          int i, j;
1273 1263          char newpath[MAXPATHLEN];
1274 1264          char *pathp;
     1265 +        char *resource_name;
1275 1266  
1276 1267          /*
1277 1268           * First find the dataset name
1278 1269           */
1279 1270          if ((group = sa_get_parent_group(share)) == NULL)  {
1280 1271                  return (EINVAL);
1281 1272          }
1282      -        if ((sahandle = sa_find_group_handle(group)) == NULL) {
     1273 +        if ((handle = sa_find_group_handle(group)) == NULL) {
1283 1274                  return (EINVAL);
1284 1275          }
1285 1276  
1286 1277          /*
1287 1278           * If get_zfs_dataset fails, see if it is a subdirectory
1288 1279           */
1289 1280  
1290 1281          pathp = path;
1291      -        while ((dataset = get_zfs_dataset(sahandle, pathp, B_TRUE)) == NULL) {
     1282 +        while ((dataset = get_zfs_dataset(handle, pathp, B_TRUE)) == NULL) {
1292 1283                  char *p;
1293 1284  
1294 1285                  if (pathp == path) {
1295 1286                          (void) strlcpy(newpath, path, sizeof (newpath));
1296 1287                          pathp = newpath;
1297 1288                  }
1298 1289  
1299 1290                  /*
1300 1291                   * Make sure only one leading '/' This condition came
1301 1292                   * about when using HAStoragePlus which insisted on
↓ open down ↓ 13 lines elided ↑ open up ↑
1315 1306                          if (pathp == p) {
1316 1307                                  *(p + 1) = '\0';  /* skip over /, root case */
1317 1308                          } else {
1318 1309                                  *p = '\0';
1319 1310                          }
1320 1311                  } else {
1321 1312                          return (EINVAL);
1322 1313                  }
1323 1314          }
1324 1315  
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);
     1316 +        i = (sh->sh_path ? strlen(sh->sh_path) : 0);
     1317 +        sh->sh_size = i;
     1318 +
     1319 +        j = (sh->sh_res ? strlen(sh->sh_res) : 0);
     1320 +        sh->sh_size += j;
     1321 +        SMAX(i, j);
     1322 +
     1323 +        j = (sh->sh_fstype ? strlen(sh->sh_fstype) : 0);
     1324 +        sh->sh_size += j;
     1325 +        SMAX(i, j);
     1326 +
     1327 +        j = (sh->sh_opts ? strlen(sh->sh_opts) : 0);
     1328 +        sh->sh_size += j;
     1329 +        SMAX(i, j);
     1330 +
     1331 +        j = (sh->sh_descr ? strlen(sh->sh_descr) : 0);
     1332 +        sh->sh_size += j;
     1333 +        SMAX(i, j);
     1334 +
     1335 +        resource_name = sa_get_resource_attr(resource, "name");
     1336 +
     1337 +        err = zfs_deleg_share_nfs(handle->zfs_libhandle, dataset, path,
     1338 +            resource_name, exportdata, sh, i, operation);
     1339 +        if (err == SA_OK)
     1340 +                sa_update_sharetab_ts(handle);
     1341 +        else
     1342 +                err = errno;
     1343 +        if (resource_name != NULL)
     1344 +                sa_free_attr_string(resource_name);
1358 1345  
1359      -                libzfs_fini(libhandle);
1360      -        }
1361 1346          free(dataset);
1362 1347          return (err);
1363 1348  }
1364 1349  
1365 1350  /*
1366 1351   * sa_get_zfs_handle(handle)
1367 1352   *
1368 1353   * Given an sa_handle_t, return the libzfs_handle_t *. This is only
1369 1354   * used internally by libzfs. Needed in order to avoid including
1370 1355   * libshare_impl.h in libzfs.
1371 1356   */
1372 1357  
1373 1358  libzfs_handle_t *
1374 1359  sa_get_zfs_handle(sa_handle_t handle)
1375 1360  {
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);
     1361 +        return (handle->zfs_libhandle);
1459 1362  }
1460 1363  
1461 1364  /*
1462 1365   * This method builds values for "sharesmb" property from the
1463 1366   * nvlist argument. The values are returned in sharesmb_val variable.
1464 1367   */
1465 1368  static int
1466 1369  sa_zfs_sprintf_new_prop(nvlist_t *nvl, char *sharesmb_val)
1467 1370  {
1468      -        char cur_val[MAXPATHLEN];
     1371 +        char cur_val[ZFS_MAXPROPLEN];
1469 1372          char *name, *val;
1470 1373          nvpair_t *cur;
1471 1374          int err = 0;
1472 1375  
1473 1376          cur = nvlist_next_nvpair(nvl, NULL);
1474 1377          while (cur != NULL) {
1475 1378                  name = nvpair_name(cur);
1476 1379                  err = nvpair_value_string(cur, &val);
1477 1380                  if ((err != 0) || (name == NULL) || (val == NULL))
1478 1381                          return (-1);
1479 1382  
1480      -                (void) snprintf(cur_val, MAXPATHLEN, "%s=%s,", name, val);
1481      -                (void) strlcat(sharesmb_val, cur_val, MAXPATHLEN);
     1383 +                (void) snprintf(cur_val, ZFS_MAXPROPLEN, "%s=%s,", name, val);
     1384 +                (void) strlcat(sharesmb_val, cur_val, ZFS_MAXPROPLEN);
1482 1385  
1483 1386                  cur = nvlist_next_nvpair(nvl, cur);
1484 1387          }
1485 1388  
1486 1389          return (0);
1487 1390  }
1488 1391  
1489 1392  /*
1490 1393   * This method builds values for "sharesmb" property from values
1491 1394   * already existing on the share. The properties set via sa_zfs_sprint_new_prop
1492 1395   * method are passed in sharesmb_val. If a existing property is already
1493 1396   * set via sa_zfs_sprint_new_prop method, then they are not appended
1494 1397   * to the sharesmb_val string. The returned sharesmb_val string is a combination
1495 1398   * of new and existing values for 'sharesmb' property.
1496 1399   */
1497 1400  static int
1498 1401  sa_zfs_sprintf_existing_prop(zfs_handle_t *handle, char *sharesmb_val)
1499 1402  {
1500      -        char shareopts[ZFS_MAXPROPLEN], cur_val[MAXPATHLEN];
     1403 +        char shareopts[ZFS_MAXPROPLEN], cur_val[ZFS_MAXPROPLEN];
1501 1404          char *token, *last, *value;
1502 1405  
1503 1406          if (zfs_prop_get(handle, ZFS_PROP_SHARESMB, shareopts,
1504 1407              sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0)
1505 1408                  return (-1);
1506 1409  
1507 1410          if (strstr(shareopts, "=") == NULL)
1508 1411                  return (0);
1509 1412  
1510 1413          for (token = strtok_r(shareopts, ",", &last); token != NULL;
1511 1414              token = strtok_r(NULL, ",", &last)) {
1512 1415                  value = strchr(token, '=');
1513 1416                  if (value == NULL)
1514 1417                          return (-1);
1515 1418                  *value++ = '\0';
1516 1419  
1517      -                (void) snprintf(cur_val, MAXPATHLEN, "%s=", token);
     1420 +                (void) snprintf(cur_val, ZFS_MAXPROPLEN, "%s=", token);
1518 1421                  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);
     1422 +                        (void) strlcat(cur_val, value, ZFS_MAXPROPLEN);
     1423 +                        (void) strlcat(cur_val, ",", ZFS_MAXPROPLEN);
     1424 +                        (void) strlcat(sharesmb_val, cur_val, ZFS_MAXPROPLEN);
1522 1425                  }
1523 1426          }
1524 1427  
1525 1428          return (0);
1526 1429  }
1527 1430  
1528 1431  /*
1529 1432   * Sets the share properties on a ZFS share. For now, this method sets only
1530 1433   * the "sharesmb" property.
1531 1434   *
↓ open down ↓ 1 lines elided ↑ open up ↑
1533 1436   * set on the "sharesmb" property of a ZFS share. This name-value string is
1534 1437   * build in 2 steps:
1535 1438   *    - New property values given as name-value pair are set first.
1536 1439   *    - Existing optionset properties, which are not part of the new properties
1537 1440   *      passed in step 1, are appended to the newly set properties.
1538 1441   */
1539 1442  int
1540 1443  sa_zfs_setprop(sa_handle_t handle, char *path, nvlist_t *nvl)
1541 1444  {
1542 1445          zfs_handle_t *z_fs;
1543      -        libzfs_handle_t *z_lib;
1544      -        char sharesmb_val[MAXPATHLEN];
     1446 +        char sharesmb_val[ZFS_MAXPROPLEN];
1545 1447          char *dataset, *lastcomma;
1546 1448  
1547 1449          if (nvlist_empty(nvl))
1548 1450                  return (0);
1549 1451  
1550 1452          if ((handle == NULL) || (path == NULL))
1551 1453                  return (-1);
1552 1454  
1553 1455          if ((dataset = get_zfs_dataset(handle, path, B_FALSE)) == NULL)
1554 1456                  return (-1);
1555 1457  
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);
     1458 +        z_fs = zfs_open(handle->zfs_libhandle, dataset, ZFS_TYPE_DATASET);
1562 1459          if (z_fs == NULL) {
1563 1460                  free(dataset);
1564      -                libzfs_fini(z_lib);
1565 1461                  return (-1);
1566 1462          }
1567 1463  
1568      -        bzero(sharesmb_val, MAXPATHLEN);
     1464 +        bzero(sharesmb_val, ZFS_MAXPROPLEN);
1569 1465          if (sa_zfs_sprintf_new_prop(nvl, sharesmb_val) != 0) {
1570 1466                  free(dataset);
1571 1467                  zfs_close(z_fs);
1572      -                libzfs_fini(z_lib);
1573 1468                  return (-1);
1574 1469          }
1575 1470  
1576 1471          if (sa_zfs_sprintf_existing_prop(z_fs, sharesmb_val) != 0) {
1577 1472                  free(dataset);
1578 1473                  zfs_close(z_fs);
1579      -                libzfs_fini(z_lib);
1580 1474                  return (-1);
1581 1475          }
1582 1476  
1583 1477          lastcomma = strrchr(sharesmb_val, ',');
1584 1478          if ((lastcomma != NULL) && (lastcomma[1] == '\0'))
1585 1479                  *lastcomma = '\0';
1586 1480  
1587 1481          (void) zfs_prop_set(z_fs, zfs_prop_to_name(ZFS_PROP_SHARESMB),
1588 1482              sharesmb_val);
1589 1483          free(dataset);
1590 1484          zfs_close(z_fs);
1591      -        libzfs_fini(z_lib);
1592 1485  
1593 1486          return (0);
1594 1487  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX