Print this page
    
OS-1566 filesystem limits for ZFS datasets
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/lib/libzfs/common/libzfs_dataset.c
          +++ new/usr/src/lib/libzfs/common/libzfs_dataset.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  25   25   * Copyright (c) 2012 by Delphix. All rights reserved.
  26   26   * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
  27   27   * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
  28   28   */
  29   29  
  30   30  #include <ctype.h>
  31   31  #include <errno.h>
  32   32  #include <libintl.h>
  33   33  #include <math.h>
  34   34  #include <stdio.h>
  35   35  #include <stdlib.h>
  36   36  #include <strings.h>
  37   37  #include <unistd.h>
  38   38  #include <stddef.h>
  39   39  #include <zone.h>
  40   40  #include <fcntl.h>
  41   41  #include <sys/mntent.h>
  42   42  #include <sys/mount.h>
  43   43  #include <priv.h>
  44   44  #include <pwd.h>
  45   45  #include <grp.h>
  46   46  #include <stddef.h>
  47   47  #include <ucred.h>
  48   48  #include <idmap.h>
  49   49  #include <aclutils.h>
  50   50  #include <directory.h>
  51   51  
  52   52  #include <sys/dnode.h>
  53   53  #include <sys/spa.h>
  54   54  #include <sys/zap.h>
  55   55  #include <libzfs.h>
  56   56  
  57   57  #include "zfs_namecheck.h"
  58   58  #include "zfs_prop.h"
  59   59  #include "libzfs_impl.h"
  60   60  #include "zfs_deleg.h"
  61   61  
  62   62  static int userquota_propname_decode(const char *propname, boolean_t zoned,
  63   63      zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
  64   64  
  65   65  /*
  66   66   * Given a single type (not a mask of types), return the type in a human
  67   67   * readable form.
  68   68   */
  69   69  const char *
  70   70  zfs_type_to_name(zfs_type_t type)
  71   71  {
  72   72          switch (type) {
  73   73          case ZFS_TYPE_FILESYSTEM:
  74   74                  return (dgettext(TEXT_DOMAIN, "filesystem"));
  75   75          case ZFS_TYPE_SNAPSHOT:
  76   76                  return (dgettext(TEXT_DOMAIN, "snapshot"));
  77   77          case ZFS_TYPE_VOLUME:
  78   78                  return (dgettext(TEXT_DOMAIN, "volume"));
  79   79          }
  80   80  
  81   81          return (NULL);
  82   82  }
  83   83  
  84   84  /*
  85   85   * Given a path and mask of ZFS types, return a string describing this dataset.
  86   86   * This is used when we fail to open a dataset and we cannot get an exact type.
  87   87   * We guess what the type would have been based on the path and the mask of
  88   88   * acceptable types.
  89   89   */
  90   90  static const char *
  91   91  path_to_str(const char *path, int types)
  92   92  {
  93   93          /*
  94   94           * When given a single type, always report the exact type.
  95   95           */
  96   96          if (types == ZFS_TYPE_SNAPSHOT)
  97   97                  return (dgettext(TEXT_DOMAIN, "snapshot"));
  98   98          if (types == ZFS_TYPE_FILESYSTEM)
  99   99                  return (dgettext(TEXT_DOMAIN, "filesystem"));
 100  100          if (types == ZFS_TYPE_VOLUME)
 101  101                  return (dgettext(TEXT_DOMAIN, "volume"));
 102  102  
 103  103          /*
 104  104           * The user is requesting more than one type of dataset.  If this is the
 105  105           * case, consult the path itself.  If we're looking for a snapshot, and
 106  106           * a '@' is found, then report it as "snapshot".  Otherwise, remove the
 107  107           * snapshot attribute and try again.
 108  108           */
 109  109          if (types & ZFS_TYPE_SNAPSHOT) {
 110  110                  if (strchr(path, '@') != NULL)
 111  111                          return (dgettext(TEXT_DOMAIN, "snapshot"));
 112  112                  return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
 113  113          }
 114  114  
 115  115          /*
 116  116           * The user has requested either filesystems or volumes.
 117  117           * We have no way of knowing a priori what type this would be, so always
 118  118           * report it as "filesystem" or "volume", our two primitive types.
 119  119           */
 120  120          if (types & ZFS_TYPE_FILESYSTEM)
 121  121                  return (dgettext(TEXT_DOMAIN, "filesystem"));
 122  122  
 123  123          assert(types & ZFS_TYPE_VOLUME);
 124  124          return (dgettext(TEXT_DOMAIN, "volume"));
 125  125  }
 126  126  
 127  127  /*
 128  128   * Validate a ZFS path.  This is used even before trying to open the dataset, to
 129  129   * provide a more meaningful error message.  We call zfs_error_aux() to
 130  130   * explain exactly why the name was not valid.
 131  131   */
 132  132  int
 133  133  zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
 134  134      boolean_t modifying)
 135  135  {
 136  136          namecheck_err_t why;
 137  137          char what;
 138  138  
 139  139          (void) zfs_prop_get_table();
 140  140          if (dataset_namecheck(path, &why, &what) != 0) {
 141  141                  if (hdl != NULL) {
 142  142                          switch (why) {
 143  143                          case NAME_ERR_TOOLONG:
 144  144                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 145  145                                      "name is too long"));
 146  146                                  break;
 147  147  
 148  148                          case NAME_ERR_LEADING_SLASH:
 149  149                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 150  150                                      "leading slash in name"));
 151  151                                  break;
 152  152  
 153  153                          case NAME_ERR_EMPTY_COMPONENT:
 154  154                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 155  155                                      "empty component in name"));
 156  156                                  break;
 157  157  
 158  158                          case NAME_ERR_TRAILING_SLASH:
 159  159                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 160  160                                      "trailing slash in name"));
 161  161                                  break;
 162  162  
 163  163                          case NAME_ERR_INVALCHAR:
 164  164                                  zfs_error_aux(hdl,
 165  165                                      dgettext(TEXT_DOMAIN, "invalid character "
 166  166                                      "'%c' in name"), what);
 167  167                                  break;
 168  168  
 169  169                          case NAME_ERR_MULTIPLE_AT:
 170  170                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 171  171                                      "multiple '@' delimiters in name"));
 172  172                                  break;
 173  173  
 174  174                          case NAME_ERR_NOLETTER:
 175  175                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 176  176                                      "pool doesn't begin with a letter"));
 177  177                                  break;
 178  178  
 179  179                          case NAME_ERR_RESERVED:
 180  180                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 181  181                                      "name is reserved"));
 182  182                                  break;
 183  183  
 184  184                          case NAME_ERR_DISKLIKE:
 185  185                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 186  186                                      "reserved disk name"));
 187  187                                  break;
 188  188                          }
 189  189                  }
 190  190  
 191  191                  return (0);
 192  192          }
 193  193  
 194  194          if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
 195  195                  if (hdl != NULL)
 196  196                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 197  197                              "snapshot delimiter '@' in filesystem name"));
 198  198                  return (0);
 199  199          }
 200  200  
 201  201          if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
 202  202                  if (hdl != NULL)
 203  203                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 204  204                              "missing '@' delimiter in snapshot name"));
 205  205                  return (0);
 206  206          }
 207  207  
 208  208          if (modifying && strchr(path, '%') != NULL) {
 209  209                  if (hdl != NULL)
 210  210                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 211  211                              "invalid character %c in name"), '%');
 212  212                  return (0);
 213  213          }
 214  214  
 215  215          return (-1);
 216  216  }
 217  217  
 218  218  int
 219  219  zfs_name_valid(const char *name, zfs_type_t type)
 220  220  {
 221  221          if (type == ZFS_TYPE_POOL)
 222  222                  return (zpool_name_valid(NULL, B_FALSE, name));
 223  223          return (zfs_validate_name(NULL, name, type, B_FALSE));
 224  224  }
 225  225  
 226  226  /*
 227  227   * This function takes the raw DSL properties, and filters out the user-defined
 228  228   * properties into a separate nvlist.
 229  229   */
 230  230  static nvlist_t *
 231  231  process_user_props(zfs_handle_t *zhp, nvlist_t *props)
 232  232  {
 233  233          libzfs_handle_t *hdl = zhp->zfs_hdl;
 234  234          nvpair_t *elem;
 235  235          nvlist_t *propval;
 236  236          nvlist_t *nvl;
 237  237  
 238  238          if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
 239  239                  (void) no_memory(hdl);
 240  240                  return (NULL);
 241  241          }
 242  242  
 243  243          elem = NULL;
 244  244          while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
 245  245                  if (!zfs_prop_user(nvpair_name(elem)))
 246  246                          continue;
 247  247  
 248  248                  verify(nvpair_value_nvlist(elem, &propval) == 0);
 249  249                  if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
 250  250                          nvlist_free(nvl);
 251  251                          (void) no_memory(hdl);
 252  252                          return (NULL);
 253  253                  }
 254  254          }
 255  255  
 256  256          return (nvl);
 257  257  }
 258  258  
 259  259  static zpool_handle_t *
 260  260  zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
 261  261  {
 262  262          libzfs_handle_t *hdl = zhp->zfs_hdl;
 263  263          zpool_handle_t *zph;
 264  264  
 265  265          if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
 266  266                  if (hdl->libzfs_pool_handles != NULL)
 267  267                          zph->zpool_next = hdl->libzfs_pool_handles;
 268  268                  hdl->libzfs_pool_handles = zph;
 269  269          }
 270  270          return (zph);
 271  271  }
 272  272  
 273  273  static zpool_handle_t *
 274  274  zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
 275  275  {
 276  276          libzfs_handle_t *hdl = zhp->zfs_hdl;
 277  277          zpool_handle_t *zph = hdl->libzfs_pool_handles;
 278  278  
 279  279          while ((zph != NULL) &&
 280  280              (strncmp(pool_name, zpool_get_name(zph), len) != 0))
 281  281                  zph = zph->zpool_next;
 282  282          return (zph);
 283  283  }
 284  284  
 285  285  /*
 286  286   * Returns a handle to the pool that contains the provided dataset.
 287  287   * If a handle to that pool already exists then that handle is returned.
 288  288   * Otherwise, a new handle is created and added to the list of handles.
 289  289   */
 290  290  static zpool_handle_t *
 291  291  zpool_handle(zfs_handle_t *zhp)
 292  292  {
 293  293          char *pool_name;
 294  294          int len;
 295  295          zpool_handle_t *zph;
 296  296  
 297  297          len = strcspn(zhp->zfs_name, "/@") + 1;
 298  298          pool_name = zfs_alloc(zhp->zfs_hdl, len);
 299  299          (void) strlcpy(pool_name, zhp->zfs_name, len);
 300  300  
 301  301          zph = zpool_find_handle(zhp, pool_name, len);
 302  302          if (zph == NULL)
 303  303                  zph = zpool_add_handle(zhp, pool_name);
 304  304  
 305  305          free(pool_name);
 306  306          return (zph);
 307  307  }
 308  308  
 309  309  void
 310  310  zpool_free_handles(libzfs_handle_t *hdl)
 311  311  {
 312  312          zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
 313  313  
 314  314          while (zph != NULL) {
 315  315                  next = zph->zpool_next;
 316  316                  zpool_close(zph);
 317  317                  zph = next;
 318  318          }
 319  319          hdl->libzfs_pool_handles = NULL;
 320  320  }
 321  321  
 322  322  /*
 323  323   * Utility function to gather stats (objset and zpl) for the given object.
 324  324   */
 325  325  static int
 326  326  get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
 327  327  {
 328  328          libzfs_handle_t *hdl = zhp->zfs_hdl;
 329  329  
 330  330          if (hdl->libzfs_cachedprops &&
 331  331              libzfs_cmd_set_cachedprops(hdl, zc) != 0)
 332  332                  return (-1);
 333  333  
 334  334          (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
 335  335  
 336  336          while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
 337  337                  if (errno == ENOMEM) {
 338  338                          if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
 339  339                                  return (-1);
 340  340                          }
 341  341                  } else {
 342  342                          return (-1);
 343  343                  }
 344  344          }
 345  345          return (0);
 346  346  }
 347  347  
 348  348  /*
 349  349   * Utility function to get the received properties of the given object.
 350  350   */
 351  351  static int
 352  352  get_recvd_props_ioctl(zfs_handle_t *zhp)
 353  353  {
 354  354          libzfs_handle_t *hdl = zhp->zfs_hdl;
 355  355          nvlist_t *recvdprops;
 356  356          zfs_cmd_t zc = { 0 };
 357  357          int err;
 358  358  
 359  359          if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
 360  360                  return (-1);
 361  361  
 362  362          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
 363  363  
 364  364          while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
 365  365                  if (errno == ENOMEM) {
 366  366                          if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
 367  367                                  return (-1);
 368  368                          }
 369  369                  } else {
 370  370                          zcmd_free_nvlists(&zc);
 371  371                          return (-1);
 372  372                  }
 373  373          }
 374  374  
 375  375          err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
 376  376          zcmd_free_nvlists(&zc);
 377  377          if (err != 0)
 378  378                  return (-1);
 379  379  
 380  380          nvlist_free(zhp->zfs_recvd_props);
 381  381          zhp->zfs_recvd_props = recvdprops;
 382  382  
 383  383          return (0);
 384  384  }
 385  385  
 386  386  static int
 387  387  put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
 388  388  {
 389  389          nvlist_t *allprops, *userprops;
 390  390  
 391  391          zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
 392  392  
 393  393          if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
 394  394                  return (-1);
 395  395          }
 396  396  
 397  397          /*
 398  398           * XXX Why do we store the user props separately, in addition to
 399  399           * storing them in zfs_props?
 400  400           */
 401  401          if ((userprops = process_user_props(zhp, allprops)) == NULL) {
 402  402                  nvlist_free(allprops);
 403  403                  return (-1);
 404  404          }
 405  405  
 406  406          nvlist_free(zhp->zfs_props);
 407  407          nvlist_free(zhp->zfs_user_props);
 408  408  
 409  409          zhp->zfs_props = allprops;
 410  410          zhp->zfs_user_props = userprops;
 411  411  
 412  412          return (0);
 413  413  }
 414  414  
 415  415  static int
 416  416  get_stats(zfs_handle_t *zhp)
 417  417  {
 418  418          int rc = 0;
 419  419          zfs_cmd_t zc = { 0 };
 420  420  
 421  421          if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
 422  422                  return (-1);
 423  423          if (get_stats_ioctl(zhp, &zc) != 0)
 424  424                  rc = -1;
 425  425          else if (put_stats_zhdl(zhp, &zc) != 0)
 426  426                  rc = -1;
 427  427          zcmd_free_nvlists(&zc);
 428  428          return (rc);
 429  429  }
 430  430  
 431  431  /*
 432  432   * Refresh the properties currently stored in the handle.
 433  433   */
 434  434  void
 435  435  zfs_refresh_properties(zfs_handle_t *zhp)
 436  436  {
 437  437          (void) get_stats(zhp);
 438  438  }
 439  439  
 440  440  /*
 441  441   * Makes a handle from the given dataset name.  Used by zfs_open() and
 442  442   * zfs_iter_* to create child handles on the fly.
 443  443   */
 444  444  static int
 445  445  make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
 446  446  {
 447  447          if (put_stats_zhdl(zhp, zc) != 0)
 448  448                  return (-1);
 449  449  
 450  450          /*
 451  451           * We've managed to open the dataset and gather statistics.  Determine
 452  452           * the high-level type.
 453  453           */
 454  454          if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
 455  455                  zhp->zfs_head_type = ZFS_TYPE_VOLUME;
 456  456          else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
 457  457                  zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
 458  458          else
 459  459                  abort();
 460  460  
 461  461          if (zhp->zfs_dmustats.dds_is_snapshot)
 462  462                  zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
 463  463          else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
 464  464                  zhp->zfs_type = ZFS_TYPE_VOLUME;
 465  465          else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
 466  466                  zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
 467  467          else
 468  468                  abort();        /* we should never see any other types */
 469  469  
 470  470          if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
 471  471                  return (-1);
 472  472  
 473  473          return (0);
 474  474  }
 475  475  
 476  476  zfs_handle_t *
 477  477  make_dataset_handle(libzfs_handle_t *hdl, const char *path)
 478  478  {
 479  479          zfs_cmd_t zc = { 0 };
 480  480  
 481  481          zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
 482  482  
 483  483          if (zhp == NULL)
 484  484                  return (NULL);
 485  485  
 486  486          zhp->zfs_hdl = hdl;
 487  487          (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
 488  488          if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
 489  489                  free(zhp);
 490  490                  return (NULL);
 491  491          }
 492  492          if (get_stats_ioctl(zhp, &zc) == -1) {
 493  493                  zcmd_free_nvlists(&zc);
 494  494                  free(zhp);
 495  495                  return (NULL);
 496  496          }
 497  497          if (make_dataset_handle_common(zhp, &zc) == -1) {
 498  498                  free(zhp);
 499  499                  zhp = NULL;
 500  500          }
 501  501          zcmd_free_nvlists(&zc);
 502  502          return (zhp);
 503  503  }
 504  504  
 505  505  zfs_handle_t *
 506  506  make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
 507  507  {
 508  508          zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
 509  509  
 510  510          if (zhp == NULL)
 511  511                  return (NULL);
 512  512  
 513  513          zhp->zfs_hdl = hdl;
 514  514          (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
 515  515          if (make_dataset_handle_common(zhp, zc) == -1) {
 516  516                  free(zhp);
 517  517                  return (NULL);
 518  518          }
 519  519          return (zhp);
 520  520  }
 521  521  
 522  522  zfs_handle_t *
 523  523  zfs_handle_dup(zfs_handle_t *zhp_orig)
 524  524  {
 525  525          zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
 526  526  
 527  527          if (zhp == NULL)
 528  528                  return (NULL);
 529  529  
 530  530          zhp->zfs_hdl = zhp_orig->zfs_hdl;
 531  531          zhp->zpool_hdl = zhp_orig->zpool_hdl;
 532  532          (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
 533  533              sizeof (zhp->zfs_name));
 534  534          zhp->zfs_type = zhp_orig->zfs_type;
 535  535          zhp->zfs_head_type = zhp_orig->zfs_head_type;
 536  536          zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
 537  537          if (zhp_orig->zfs_props != NULL) {
 538  538                  if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
 539  539                          (void) no_memory(zhp->zfs_hdl);
 540  540                          zfs_close(zhp);
 541  541                          return (NULL);
 542  542                  }
 543  543          }
 544  544          if (zhp_orig->zfs_user_props != NULL) {
 545  545                  if (nvlist_dup(zhp_orig->zfs_user_props,
 546  546                      &zhp->zfs_user_props, 0) != 0) {
 547  547                          (void) no_memory(zhp->zfs_hdl);
 548  548                          zfs_close(zhp);
 549  549                          return (NULL);
 550  550                  }
 551  551          }
 552  552          if (zhp_orig->zfs_recvd_props != NULL) {
 553  553                  if (nvlist_dup(zhp_orig->zfs_recvd_props,
 554  554                      &zhp->zfs_recvd_props, 0)) {
 555  555                          (void) no_memory(zhp->zfs_hdl);
 556  556                          zfs_close(zhp);
 557  557                          return (NULL);
 558  558                  }
 559  559          }
 560  560          zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
 561  561          if (zhp_orig->zfs_mntopts != NULL) {
 562  562                  zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
 563  563                      zhp_orig->zfs_mntopts);
 564  564          }
 565  565          zhp->zfs_props_table = zhp_orig->zfs_props_table;
 566  566          return (zhp);
 567  567  }
 568  568  
 569  569  /*
 570  570   * Opens the given snapshot, filesystem, or volume.   The 'types'
 571  571   * argument is a mask of acceptable types.  The function will print an
 572  572   * appropriate error message and return NULL if it can't be opened.
 573  573   */
 574  574  zfs_handle_t *
 575  575  zfs_open(libzfs_handle_t *hdl, const char *path, int types)
 576  576  {
 577  577          zfs_handle_t *zhp;
 578  578          char errbuf[1024];
 579  579  
 580  580          (void) snprintf(errbuf, sizeof (errbuf),
 581  581              dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
 582  582  
 583  583          /*
 584  584           * Validate the name before we even try to open it.
 585  585           */
 586  586          if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
 587  587                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 588  588                      "invalid dataset name"));
 589  589                  (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
 590  590                  return (NULL);
 591  591          }
 592  592  
 593  593          /*
 594  594           * Try to get stats for the dataset, which will tell us if it exists.
 595  595           */
 596  596          errno = 0;
 597  597          if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
 598  598                  (void) zfs_standard_error(hdl, errno, errbuf);
 599  599                  return (NULL);
 600  600          }
 601  601  
 602  602          if (!(types & zhp->zfs_type)) {
 603  603                  (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
 604  604                  zfs_close(zhp);
 605  605                  return (NULL);
 606  606          }
 607  607  
 608  608          return (zhp);
 609  609  }
 610  610  
 611  611  /*
 612  612   * Release a ZFS handle.  Nothing to do but free the associated memory.
 613  613   */
 614  614  void
 615  615  zfs_close(zfs_handle_t *zhp)
 616  616  {
 617  617          if (zhp->zfs_mntopts)
 618  618                  free(zhp->zfs_mntopts);
 619  619          nvlist_free(zhp->zfs_props);
 620  620          nvlist_free(zhp->zfs_user_props);
 621  621          nvlist_free(zhp->zfs_recvd_props);
 622  622          free(zhp);
 623  623  }
 624  624  
 625  625  typedef struct mnttab_node {
 626  626          struct mnttab mtn_mt;
 627  627          avl_node_t mtn_node;
 628  628  } mnttab_node_t;
 629  629  
 630  630  static int
 631  631  libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
 632  632  {
 633  633          const mnttab_node_t *mtn1 = arg1;
 634  634          const mnttab_node_t *mtn2 = arg2;
 635  635          int rv;
 636  636  
 637  637          rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
 638  638  
 639  639          if (rv == 0)
 640  640                  return (0);
 641  641          return (rv > 0 ? 1 : -1);
 642  642  }
 643  643  
 644  644  void
 645  645  libzfs_mnttab_init(libzfs_handle_t *hdl)
 646  646  {
 647  647          assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
 648  648          avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
 649  649              sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
 650  650  }
 651  651  
 652  652  void
 653  653  libzfs_mnttab_update(libzfs_handle_t *hdl)
 654  654  {
 655  655          struct mnttab entry;
 656  656  
 657  657          rewind(hdl->libzfs_mnttab);
 658  658          while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
 659  659                  mnttab_node_t *mtn;
 660  660  
 661  661                  if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
 662  662                          continue;
 663  663                  mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
 664  664                  mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
 665  665                  mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
 666  666                  mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
 667  667                  mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
 668  668                  avl_add(&hdl->libzfs_mnttab_cache, mtn);
 669  669          }
 670  670  }
 671  671  
 672  672  void
 673  673  libzfs_mnttab_fini(libzfs_handle_t *hdl)
 674  674  {
 675  675          void *cookie = NULL;
 676  676          mnttab_node_t *mtn;
 677  677  
 678  678          while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
 679  679                  free(mtn->mtn_mt.mnt_special);
 680  680                  free(mtn->mtn_mt.mnt_mountp);
 681  681                  free(mtn->mtn_mt.mnt_fstype);
 682  682                  free(mtn->mtn_mt.mnt_mntopts);
 683  683                  free(mtn);
 684  684          }
 685  685          avl_destroy(&hdl->libzfs_mnttab_cache);
 686  686  }
 687  687  
 688  688  void
 689  689  libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
 690  690  {
 691  691          hdl->libzfs_mnttab_enable = enable;
 692  692  }
 693  693  
 694  694  int
 695  695  libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
 696  696      struct mnttab *entry)
 697  697  {
 698  698          mnttab_node_t find;
 699  699          mnttab_node_t *mtn;
 700  700  
 701  701          if (!hdl->libzfs_mnttab_enable) {
 702  702                  struct mnttab srch = { 0 };
 703  703  
 704  704                  if (avl_numnodes(&hdl->libzfs_mnttab_cache))
 705  705                          libzfs_mnttab_fini(hdl);
 706  706                  rewind(hdl->libzfs_mnttab);
 707  707                  srch.mnt_special = (char *)fsname;
 708  708                  srch.mnt_fstype = MNTTYPE_ZFS;
 709  709                  if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
 710  710                          return (0);
 711  711                  else
 712  712                          return (ENOENT);
 713  713          }
 714  714  
 715  715          if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
 716  716                  libzfs_mnttab_update(hdl);
 717  717  
 718  718          find.mtn_mt.mnt_special = (char *)fsname;
 719  719          mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
 720  720          if (mtn) {
 721  721                  *entry = mtn->mtn_mt;
 722  722                  return (0);
 723  723          }
 724  724          return (ENOENT);
 725  725  }
 726  726  
 727  727  void
 728  728  libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
 729  729      const char *mountp, const char *mntopts)
 730  730  {
 731  731          mnttab_node_t *mtn;
 732  732  
 733  733          if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
 734  734                  return;
 735  735          mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
 736  736          mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
 737  737          mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
 738  738          mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
 739  739          mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
 740  740          avl_add(&hdl->libzfs_mnttab_cache, mtn);
 741  741  }
 742  742  
 743  743  void
 744  744  libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
 745  745  {
 746  746          mnttab_node_t find;
 747  747          mnttab_node_t *ret;
 748  748  
 749  749          find.mtn_mt.mnt_special = (char *)fsname;
 750  750          if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
 751  751                  avl_remove(&hdl->libzfs_mnttab_cache, ret);
 752  752                  free(ret->mtn_mt.mnt_special);
 753  753                  free(ret->mtn_mt.mnt_mountp);
 754  754                  free(ret->mtn_mt.mnt_fstype);
 755  755                  free(ret->mtn_mt.mnt_mntopts);
 756  756                  free(ret);
 757  757          }
 758  758  }
 759  759  
 760  760  int
 761  761  zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
 762  762  {
 763  763          zpool_handle_t *zpool_handle = zhp->zpool_hdl;
 764  764  
 765  765          if (zpool_handle == NULL)
 766  766                  return (-1);
 767  767  
 768  768          *spa_version = zpool_get_prop_int(zpool_handle,
 769  769              ZPOOL_PROP_VERSION, NULL);
 770  770          return (0);
 771  771  }
 772  772  
 773  773  /*
 774  774   * The choice of reservation property depends on the SPA version.
 775  775   */
 776  776  static int
 777  777  zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
 778  778  {
 779  779          int spa_version;
 780  780  
 781  781          if (zfs_spa_version(zhp, &spa_version) < 0)
 782  782                  return (-1);
 783  783  
 784  784          if (spa_version >= SPA_VERSION_REFRESERVATION)
 785  785                  *resv_prop = ZFS_PROP_REFRESERVATION;
 786  786          else
 787  787                  *resv_prop = ZFS_PROP_RESERVATION;
 788  788  
 789  789          return (0);
 790  790  }
 791  791  
 792  792  /*
 793  793   * Given an nvlist of properties to set, validates that they are correct, and
 794  794   * parses any numeric properties (index, boolean, etc) if they are specified as
 795  795   * strings.
 796  796   */
 797  797  nvlist_t *
 798  798  zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
 799  799      uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
 800  800  {
 801  801          nvpair_t *elem;
 802  802          uint64_t intval;
 803  803          char *strval;
 804  804          zfs_prop_t prop;
 805  805          nvlist_t *ret;
 806  806          int chosen_normal = -1;
 807  807          int chosen_utf = -1;
 808  808  
 809  809          if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
 810  810                  (void) no_memory(hdl);
 811  811                  return (NULL);
 812  812          }
 813  813  
 814  814          /*
 815  815           * Make sure this property is valid and applies to this type.
 816  816           */
 817  817  
 818  818          elem = NULL;
 819  819          while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
 820  820                  const char *propname = nvpair_name(elem);
 821  821  
 822  822                  prop = zfs_name_to_prop(propname);
 823  823                  if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
 824  824                          /*
 825  825                           * This is a user property: make sure it's a
 826  826                           * string, and that it's less than ZAP_MAXNAMELEN.
 827  827                           */
 828  828                          if (nvpair_type(elem) != DATA_TYPE_STRING) {
 829  829                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 830  830                                      "'%s' must be a string"), propname);
 831  831                                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
 832  832                                  goto error;
 833  833                          }
 834  834  
 835  835                          if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
 836  836                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 837  837                                      "property name '%s' is too long"),
 838  838                                      propname);
 839  839                                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
 840  840                                  goto error;
 841  841                          }
 842  842  
 843  843                          (void) nvpair_value_string(elem, &strval);
 844  844                          if (nvlist_add_string(ret, propname, strval) != 0) {
 845  845                                  (void) no_memory(hdl);
 846  846                                  goto error;
 847  847                          }
 848  848                          continue;
 849  849                  }
 850  850  
 851  851                  /*
 852  852                   * Currently, only user properties can be modified on
 853  853                   * snapshots.
 854  854                   */
 855  855                  if (type == ZFS_TYPE_SNAPSHOT) {
 856  856                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 857  857                              "this property can not be modified for snapshots"));
 858  858                          (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
 859  859                          goto error;
 860  860                  }
 861  861  
 862  862                  if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
 863  863                          zfs_userquota_prop_t uqtype;
 864  864                          char newpropname[128];
 865  865                          char domain[128];
 866  866                          uint64_t rid;
 867  867                          uint64_t valary[3];
 868  868  
 869  869                          if (userquota_propname_decode(propname, zoned,
 870  870                              &uqtype, domain, sizeof (domain), &rid) != 0) {
 871  871                                  zfs_error_aux(hdl,
 872  872                                      dgettext(TEXT_DOMAIN,
 873  873                                      "'%s' has an invalid user/group name"),
 874  874                                      propname);
 875  875                                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
 876  876                                  goto error;
 877  877                          }
 878  878  
 879  879                          if (uqtype != ZFS_PROP_USERQUOTA &&
 880  880                              uqtype != ZFS_PROP_GROUPQUOTA) {
 881  881                                  zfs_error_aux(hdl,
 882  882                                      dgettext(TEXT_DOMAIN, "'%s' is readonly"),
 883  883                                      propname);
 884  884                                  (void) zfs_error(hdl, EZFS_PROPREADONLY,
 885  885                                      errbuf);
 886  886                                  goto error;
 887  887                          }
 888  888  
 889  889                          if (nvpair_type(elem) == DATA_TYPE_STRING) {
 890  890                                  (void) nvpair_value_string(elem, &strval);
 891  891                                  if (strcmp(strval, "none") == 0) {
 892  892                                          intval = 0;
 893  893                                  } else if (zfs_nicestrtonum(hdl,
 894  894                                      strval, &intval) != 0) {
 895  895                                          (void) zfs_error(hdl,
 896  896                                              EZFS_BADPROP, errbuf);
 897  897                                          goto error;
 898  898                                  }
 899  899                          } else if (nvpair_type(elem) ==
 900  900                              DATA_TYPE_UINT64) {
 901  901                                  (void) nvpair_value_uint64(elem, &intval);
 902  902                                  if (intval == 0) {
 903  903                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 904  904                                              "use 'none' to disable "
 905  905                                              "userquota/groupquota"));
 906  906                                          goto error;
 907  907                                  }
 908  908                          } else {
 909  909                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 910  910                                      "'%s' must be a number"), propname);
 911  911                                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
 912  912                                  goto error;
 913  913                          }
 914  914  
 915  915                          /*
 916  916                           * Encode the prop name as
 917  917                           * userquota@<hex-rid>-domain, to make it easy
 918  918                           * for the kernel to decode.
 919  919                           */
 920  920                          (void) snprintf(newpropname, sizeof (newpropname),
 921  921                              "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
 922  922                              (longlong_t)rid, domain);
 923  923                          valary[0] = uqtype;
 924  924                          valary[1] = rid;
 925  925                          valary[2] = intval;
 926  926                          if (nvlist_add_uint64_array(ret, newpropname,
 927  927                              valary, 3) != 0) {
 928  928                                  (void) no_memory(hdl);
 929  929                                  goto error;
 930  930                          }
 931  931                          continue;
 932  932                  } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
 933  933                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 934  934                              "'%s' is readonly"),
 935  935                              propname);
 936  936                          (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
 937  937                          goto error;
 938  938                  }
 939  939  
 940  940                  if (prop == ZPROP_INVAL) {
 941  941                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 942  942                              "invalid property '%s'"), propname);
 943  943                          (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
 944  944                          goto error;
 945  945                  }
 946  946  
 947  947                  if (!zfs_prop_valid_for_type(prop, type)) {
 948  948                          zfs_error_aux(hdl,
 949  949                              dgettext(TEXT_DOMAIN, "'%s' does not "
 950  950                              "apply to datasets of this type"), propname);
 951  951                          (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
 952  952                          goto error;
 953  953                  }
  
    | 
      ↓ open down ↓ | 
    953 lines elided | 
    
      ↑ open up ↑ | 
  
 954  954  
 955  955                  if (zfs_prop_readonly(prop) &&
 956  956                      (!zfs_prop_setonce(prop) || zhp != NULL)) {
 957  957                          zfs_error_aux(hdl,
 958  958                              dgettext(TEXT_DOMAIN, "'%s' is readonly"),
 959  959                              propname);
 960  960                          (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
 961  961                          goto error;
 962  962                  }
 963  963  
      964 +                /*
      965 +                 * Special handling for "xxx_limit=none". In this case its not
      966 +                 * 0 but MAXLIMIT.
      967 +                 */
      968 +                if ((prop == ZFS_PROP_FILESYSTEM_LIMIT ||
      969 +                    prop == ZFS_PROP_SNAPSHOT_LIMIT) &&
      970 +                    nvpair_type(elem) == DATA_TYPE_STRING) {
      971 +                        (void) nvpair_value_string(elem, &strval);
      972 +                        if (strcmp(strval, "none") == 0) {
      973 +                                if (nvlist_add_uint64(ret, propname,
      974 +                                    MAXLIMIT) != 0) {
      975 +                                        (void) no_memory(hdl);
      976 +                                        goto error;
      977 +                                }
      978 +                                continue;
      979 +                        }
      980 +                }
      981 +
 964  982                  if (zprop_parse_value(hdl, elem, prop, type, ret,
 965  983                      &strval, &intval, errbuf) != 0)
 966  984                          goto error;
 967  985  
 968  986                  /*
 969  987                   * Perform some additional checks for specific properties.
 970  988                   */
 971  989                  switch (prop) {
 972  990                  case ZFS_PROP_VERSION:
 973  991                  {
 974  992                          int version;
 975  993  
 976  994                          if (zhp == NULL)
 977  995                                  break;
 978  996                          version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
 979  997                          if (intval < version) {
 980  998                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 981  999                                      "Can not downgrade; already at version %u"),
 982 1000                                      version);
 983 1001                                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
 984 1002                                  goto error;
 985 1003                          }
 986 1004                          break;
 987 1005                  }
 988 1006  
 989 1007                  case ZFS_PROP_RECORDSIZE:
 990 1008                  case ZFS_PROP_VOLBLOCKSIZE:
 991 1009                          /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
 992 1010                          if (intval < SPA_MINBLOCKSIZE ||
 993 1011                              intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
 994 1012                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
 995 1013                                      "'%s' must be power of 2 from %u "
 996 1014                                      "to %uk"), propname,
 997 1015                                      (uint_t)SPA_MINBLOCKSIZE,
 998 1016                                      (uint_t)SPA_MAXBLOCKSIZE >> 10);
 999 1017                                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1000 1018                                  goto error;
1001 1019                          }
1002 1020                          break;
1003 1021  
1004 1022                  case ZFS_PROP_MLSLABEL:
1005 1023                  {
1006 1024                          /*
1007 1025                           * Verify the mlslabel string and convert to
1008 1026                           * internal hex label string.
1009 1027                           */
1010 1028  
1011 1029                          m_label_t *new_sl;
1012 1030                          char *hex = NULL;       /* internal label string */
1013 1031  
1014 1032                          /* Default value is already OK. */
1015 1033                          if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1016 1034                                  break;
1017 1035  
1018 1036                          /* Verify the label can be converted to binary form */
1019 1037                          if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1020 1038                              (str_to_label(strval, &new_sl, MAC_LABEL,
1021 1039                              L_NO_CORRECTION, NULL) == -1)) {
1022 1040                                  goto badlabel;
1023 1041                          }
1024 1042  
1025 1043                          /* Now translate to hex internal label string */
1026 1044                          if (label_to_str(new_sl, &hex, M_INTERNAL,
1027 1045                              DEF_NAMES) != 0) {
1028 1046                                  if (hex)
1029 1047                                          free(hex);
1030 1048                                  goto badlabel;
1031 1049                          }
1032 1050                          m_label_free(new_sl);
1033 1051  
1034 1052                          /* If string is already in internal form, we're done. */
1035 1053                          if (strcmp(strval, hex) == 0) {
1036 1054                                  free(hex);
1037 1055                                  break;
1038 1056                          }
1039 1057  
1040 1058                          /* Replace the label string with the internal form. */
1041 1059                          (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1042 1060                              DATA_TYPE_STRING);
1043 1061                          verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1044 1062                              hex) == 0);
1045 1063                          free(hex);
1046 1064  
1047 1065                          break;
1048 1066  
1049 1067  badlabel:
1050 1068                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1051 1069                              "invalid mlslabel '%s'"), strval);
1052 1070                          (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1053 1071                          m_label_free(new_sl);   /* OK if null */
1054 1072                          goto error;
1055 1073  
1056 1074                  }
1057 1075  
1058 1076                  case ZFS_PROP_MOUNTPOINT:
1059 1077                  {
1060 1078                          namecheck_err_t why;
1061 1079  
1062 1080                          if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1063 1081                              strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1064 1082                                  break;
1065 1083  
1066 1084                          if (mountpoint_namecheck(strval, &why)) {
1067 1085                                  switch (why) {
1068 1086                                  case NAME_ERR_LEADING_SLASH:
1069 1087                                          zfs_error_aux(hdl,
1070 1088                                              dgettext(TEXT_DOMAIN,
1071 1089                                              "'%s' must be an absolute path, "
1072 1090                                              "'none', or 'legacy'"), propname);
1073 1091                                          break;
1074 1092                                  case NAME_ERR_TOOLONG:
1075 1093                                          zfs_error_aux(hdl,
1076 1094                                              dgettext(TEXT_DOMAIN,
1077 1095                                              "component of '%s' is too long"),
1078 1096                                              propname);
1079 1097                                          break;
1080 1098                                  }
1081 1099                                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1082 1100                                  goto error;
1083 1101                          }
1084 1102                  }
1085 1103  
1086 1104                          /*FALLTHRU*/
1087 1105  
1088 1106                  case ZFS_PROP_SHARESMB:
1089 1107                  case ZFS_PROP_SHARENFS:
1090 1108                          /*
1091 1109                           * For the mountpoint and sharenfs or sharesmb
1092 1110                           * properties, check if it can be set in a
1093 1111                           * global/non-global zone based on
1094 1112                           * the zoned property value:
1095 1113                           *
1096 1114                           *              global zone         non-global zone
1097 1115                           * --------------------------------------------------
1098 1116                           * zoned=on     mountpoint (no)     mountpoint (yes)
1099 1117                           *              sharenfs (no)       sharenfs (no)
1100 1118                           *              sharesmb (no)       sharesmb (no)
1101 1119                           *
1102 1120                           * zoned=off    mountpoint (yes)        N/A
1103 1121                           *              sharenfs (yes)
1104 1122                           *              sharesmb (yes)
1105 1123                           */
1106 1124                          if (zoned) {
1107 1125                                  if (getzoneid() == GLOBAL_ZONEID) {
1108 1126                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1109 1127                                              "'%s' cannot be set on "
1110 1128                                              "dataset in a non-global zone"),
1111 1129                                              propname);
1112 1130                                          (void) zfs_error(hdl, EZFS_ZONED,
1113 1131                                              errbuf);
1114 1132                                          goto error;
1115 1133                                  } else if (prop == ZFS_PROP_SHARENFS ||
1116 1134                                      prop == ZFS_PROP_SHARESMB) {
1117 1135                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1118 1136                                              "'%s' cannot be set in "
1119 1137                                              "a non-global zone"), propname);
1120 1138                                          (void) zfs_error(hdl, EZFS_ZONED,
1121 1139                                              errbuf);
1122 1140                                          goto error;
1123 1141                                  }
1124 1142                          } else if (getzoneid() != GLOBAL_ZONEID) {
1125 1143                                  /*
1126 1144                                   * If zoned property is 'off', this must be in
1127 1145                                   * a global zone. If not, something is wrong.
1128 1146                                   */
1129 1147                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1130 1148                                      "'%s' cannot be set while dataset "
1131 1149                                      "'zoned' property is set"), propname);
1132 1150                                  (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1133 1151                                  goto error;
1134 1152                          }
1135 1153  
1136 1154                          /*
1137 1155                           * At this point, it is legitimate to set the
1138 1156                           * property. Now we want to make sure that the
1139 1157                           * property value is valid if it is sharenfs.
1140 1158                           */
1141 1159                          if ((prop == ZFS_PROP_SHARENFS ||
1142 1160                              prop == ZFS_PROP_SHARESMB) &&
1143 1161                              strcmp(strval, "on") != 0 &&
1144 1162                              strcmp(strval, "off") != 0) {
1145 1163                                  zfs_share_proto_t proto;
1146 1164  
1147 1165                                  if (prop == ZFS_PROP_SHARESMB)
1148 1166                                          proto = PROTO_SMB;
1149 1167                                  else
1150 1168                                          proto = PROTO_NFS;
1151 1169  
1152 1170                                  /*
1153 1171                                   * Must be an valid sharing protocol
1154 1172                                   * option string so init the libshare
1155 1173                                   * in order to enable the parser and
1156 1174                                   * then parse the options. We use the
1157 1175                                   * control API since we don't care about
1158 1176                                   * the current configuration and don't
1159 1177                                   * want the overhead of loading it
1160 1178                                   * until we actually do something.
1161 1179                                   */
1162 1180  
1163 1181                                  if (zfs_init_libshare(hdl,
1164 1182                                      SA_INIT_CONTROL_API) != SA_OK) {
1165 1183                                          /*
1166 1184                                           * An error occurred so we can't do
1167 1185                                           * anything
1168 1186                                           */
1169 1187                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1170 1188                                              "'%s' cannot be set: problem "
1171 1189                                              "in share initialization"),
1172 1190                                              propname);
1173 1191                                          (void) zfs_error(hdl, EZFS_BADPROP,
1174 1192                                              errbuf);
1175 1193                                          goto error;
1176 1194                                  }
1177 1195  
1178 1196                                  if (zfs_parse_options(strval, proto) != SA_OK) {
1179 1197                                          /*
1180 1198                                           * There was an error in parsing so
1181 1199                                           * deal with it by issuing an error
1182 1200                                           * message and leaving after
1183 1201                                           * uninitializing the the libshare
1184 1202                                           * interface.
1185 1203                                           */
1186 1204                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1187 1205                                              "'%s' cannot be set to invalid "
1188 1206                                              "options"), propname);
1189 1207                                          (void) zfs_error(hdl, EZFS_BADPROP,
1190 1208                                              errbuf);
1191 1209                                          zfs_uninit_libshare(hdl);
1192 1210                                          goto error;
1193 1211                                  }
1194 1212                                  zfs_uninit_libshare(hdl);
1195 1213                          }
1196 1214  
1197 1215                          break;
1198 1216                  case ZFS_PROP_UTF8ONLY:
1199 1217                          chosen_utf = (int)intval;
1200 1218                          break;
1201 1219                  case ZFS_PROP_NORMALIZE:
1202 1220                          chosen_normal = (int)intval;
1203 1221                          break;
1204 1222                  }
1205 1223  
1206 1224                  /*
1207 1225                   * For changes to existing volumes, we have some additional
1208 1226                   * checks to enforce.
1209 1227                   */
1210 1228                  if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1211 1229                          uint64_t volsize = zfs_prop_get_int(zhp,
1212 1230                              ZFS_PROP_VOLSIZE);
1213 1231                          uint64_t blocksize = zfs_prop_get_int(zhp,
1214 1232                              ZFS_PROP_VOLBLOCKSIZE);
1215 1233                          char buf[64];
1216 1234  
1217 1235                          switch (prop) {
1218 1236                          case ZFS_PROP_RESERVATION:
1219 1237                          case ZFS_PROP_REFRESERVATION:
1220 1238                                  if (intval > volsize) {
1221 1239                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1222 1240                                              "'%s' is greater than current "
1223 1241                                              "volume size"), propname);
1224 1242                                          (void) zfs_error(hdl, EZFS_BADPROP,
1225 1243                                              errbuf);
1226 1244                                          goto error;
1227 1245                                  }
1228 1246                                  break;
1229 1247  
1230 1248                          case ZFS_PROP_VOLSIZE:
1231 1249                                  if (intval % blocksize != 0) {
1232 1250                                          zfs_nicenum(blocksize, buf,
1233 1251                                              sizeof (buf));
1234 1252                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1235 1253                                              "'%s' must be a multiple of "
1236 1254                                              "volume block size (%s)"),
1237 1255                                              propname, buf);
1238 1256                                          (void) zfs_error(hdl, EZFS_BADPROP,
1239 1257                                              errbuf);
1240 1258                                          goto error;
1241 1259                                  }
1242 1260  
1243 1261                                  if (intval == 0) {
1244 1262                                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1245 1263                                              "'%s' cannot be zero"),
1246 1264                                              propname);
1247 1265                                          (void) zfs_error(hdl, EZFS_BADPROP,
1248 1266                                              errbuf);
1249 1267                                          goto error;
1250 1268                                  }
1251 1269                                  break;
1252 1270                          }
1253 1271                  }
1254 1272          }
1255 1273  
1256 1274          /*
1257 1275           * If normalization was chosen, but no UTF8 choice was made,
1258 1276           * enforce rejection of non-UTF8 names.
1259 1277           *
1260 1278           * If normalization was chosen, but rejecting non-UTF8 names
1261 1279           * was explicitly not chosen, it is an error.
1262 1280           */
1263 1281          if (chosen_normal > 0 && chosen_utf < 0) {
1264 1282                  if (nvlist_add_uint64(ret,
1265 1283                      zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1266 1284                          (void) no_memory(hdl);
1267 1285                          goto error;
1268 1286                  }
1269 1287          } else if (chosen_normal > 0 && chosen_utf == 0) {
1270 1288                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1271 1289                      "'%s' must be set 'on' if normalization chosen"),
1272 1290                      zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1273 1291                  (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1274 1292                  goto error;
1275 1293          }
1276 1294          return (ret);
1277 1295  
1278 1296  error:
1279 1297          nvlist_free(ret);
1280 1298          return (NULL);
1281 1299  }
1282 1300  
1283 1301  int
1284 1302  zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1285 1303  {
1286 1304          uint64_t old_volsize;
1287 1305          uint64_t new_volsize;
1288 1306          uint64_t old_reservation;
1289 1307          uint64_t new_reservation;
1290 1308          zfs_prop_t resv_prop;
1291 1309  
1292 1310          /*
1293 1311           * If this is an existing volume, and someone is setting the volsize,
1294 1312           * make sure that it matches the reservation, or add it if necessary.
1295 1313           */
1296 1314          old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1297 1315          if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1298 1316                  return (-1);
1299 1317          old_reservation = zfs_prop_get_int(zhp, resv_prop);
1300 1318          if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1301 1319              old_reservation) || nvlist_lookup_uint64(nvl,
1302 1320              zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1303 1321                  return (0);
1304 1322          }
1305 1323          if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1306 1324              &new_volsize) != 0)
1307 1325                  return (-1);
1308 1326          new_reservation = zvol_volsize_to_reservation(new_volsize,
1309 1327              zhp->zfs_props);
1310 1328          if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1311 1329              new_reservation) != 0) {
1312 1330                  (void) no_memory(zhp->zfs_hdl);
1313 1331                  return (-1);
1314 1332          }
1315 1333          return (1);
1316 1334  }
1317 1335  
1318 1336  void
1319 1337  zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1320 1338      char *errbuf)
1321 1339  {
1322 1340          switch (err) {
1323 1341  
1324 1342          case ENOSPC:
1325 1343                  /*
1326 1344                   * For quotas and reservations, ENOSPC indicates
1327 1345                   * something different; setting a quota or reservation
1328 1346                   * doesn't use any disk space.
1329 1347                   */
1330 1348                  switch (prop) {
1331 1349                  case ZFS_PROP_QUOTA:
1332 1350                  case ZFS_PROP_REFQUOTA:
1333 1351                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1334 1352                              "size is less than current used or "
1335 1353                              "reserved space"));
1336 1354                          (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1337 1355                          break;
1338 1356  
1339 1357                  case ZFS_PROP_RESERVATION:
1340 1358                  case ZFS_PROP_REFRESERVATION:
1341 1359                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1342 1360                              "size is greater than available space"));
1343 1361                          (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1344 1362                          break;
1345 1363  
1346 1364                  default:
1347 1365                          (void) zfs_standard_error(hdl, err, errbuf);
1348 1366                          break;
1349 1367                  }
1350 1368                  break;
1351 1369  
1352 1370          case EBUSY:
1353 1371                  (void) zfs_standard_error(hdl, EBUSY, errbuf);
1354 1372                  break;
1355 1373  
1356 1374          case EROFS:
1357 1375                  (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1358 1376                  break;
1359 1377  
1360 1378          case ENOTSUP:
1361 1379                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1362 1380                      "pool and or dataset must be upgraded to set this "
1363 1381                      "property or value"));
1364 1382                  (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1365 1383                  break;
1366 1384  
1367 1385          case ERANGE:
1368 1386                  if (prop == ZFS_PROP_COMPRESSION) {
1369 1387                          (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1370 1388                              "property setting is not allowed on "
1371 1389                              "bootable datasets"));
1372 1390                          (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1373 1391                  } else {
1374 1392                          (void) zfs_standard_error(hdl, err, errbuf);
1375 1393                  }
1376 1394                  break;
1377 1395  
1378 1396          case EINVAL:
1379 1397                  if (prop == ZPROP_INVAL) {
1380 1398                          (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1381 1399                  } else {
1382 1400                          (void) zfs_standard_error(hdl, err, errbuf);
1383 1401                  }
1384 1402                  break;
1385 1403  
1386 1404          case EOVERFLOW:
1387 1405                  /*
1388 1406                   * This platform can't address a volume this big.
1389 1407                   */
1390 1408  #ifdef _ILP32
1391 1409                  if (prop == ZFS_PROP_VOLSIZE) {
1392 1410                          (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1393 1411                          break;
1394 1412                  }
1395 1413  #endif
1396 1414                  /* FALLTHROUGH */
1397 1415          default:
1398 1416                  (void) zfs_standard_error(hdl, err, errbuf);
1399 1417          }
1400 1418  }
1401 1419  
1402 1420  /*
1403 1421   * Given a property name and value, set the property for the given dataset.
1404 1422   */
1405 1423  int
1406 1424  zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1407 1425  {
1408 1426          zfs_cmd_t zc = { 0 };
1409 1427          int ret = -1;
1410 1428          prop_changelist_t *cl = NULL;
1411 1429          char errbuf[1024];
1412 1430          libzfs_handle_t *hdl = zhp->zfs_hdl;
1413 1431          nvlist_t *nvl = NULL, *realprops;
1414 1432          zfs_prop_t prop;
1415 1433          boolean_t do_prefix = B_TRUE;
1416 1434          int added_resv;
1417 1435  
1418 1436          (void) snprintf(errbuf, sizeof (errbuf),
1419 1437              dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1420 1438              zhp->zfs_name);
1421 1439  
1422 1440          if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1423 1441              nvlist_add_string(nvl, propname, propval) != 0) {
1424 1442                  (void) no_memory(hdl);
1425 1443                  goto error;
1426 1444          }
1427 1445  
1428 1446          if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1429 1447              zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1430 1448                  goto error;
1431 1449  
1432 1450          nvlist_free(nvl);
1433 1451          nvl = realprops;
1434 1452  
1435 1453          prop = zfs_name_to_prop(propname);
1436 1454  
1437 1455          if (prop == ZFS_PROP_VOLSIZE) {
1438 1456                  if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1439 1457                          goto error;
1440 1458          }
1441 1459  
1442 1460          if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1443 1461                  goto error;
1444 1462  
1445 1463          if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1446 1464                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1447 1465                      "child dataset with inherited mountpoint is used "
1448 1466                      "in a non-global zone"));
1449 1467                  ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1450 1468                  goto error;
1451 1469          }
1452 1470  
1453 1471          /*
1454 1472           * We don't want to unmount & remount the dataset when changing
1455 1473           * its canmount property to 'on' or 'noauto'.  We only use
1456 1474           * the changelist logic to unmount when setting canmount=off.
1457 1475           */
1458 1476          if (prop == ZFS_PROP_CANMOUNT) {
1459 1477                  uint64_t idx;
1460 1478                  int err = zprop_string_to_index(prop, propval, &idx,
1461 1479                      ZFS_TYPE_DATASET);
1462 1480                  if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1463 1481                          do_prefix = B_FALSE;
1464 1482          }
1465 1483  
1466 1484          if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1467 1485                  goto error;
1468 1486  
1469 1487          /*
1470 1488           * Execute the corresponding ioctl() to set this property.
1471 1489           */
1472 1490          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1473 1491  
1474 1492          if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1475 1493                  goto error;
1476 1494  
1477 1495          ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1478 1496  
1479 1497          if (ret != 0) {
1480 1498                  zfs_setprop_error(hdl, prop, errno, errbuf);
1481 1499                  if (added_resv && errno == ENOSPC) {
1482 1500                          /* clean up the volsize property we tried to set */
1483 1501                          uint64_t old_volsize = zfs_prop_get_int(zhp,
1484 1502                              ZFS_PROP_VOLSIZE);
1485 1503                          nvlist_free(nvl);
1486 1504                          zcmd_free_nvlists(&zc);
1487 1505                          if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1488 1506                                  goto error;
1489 1507                          if (nvlist_add_uint64(nvl,
1490 1508                              zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1491 1509                              old_volsize) != 0)
1492 1510                                  goto error;
1493 1511                          if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1494 1512                                  goto error;
1495 1513                          (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1496 1514                  }
1497 1515          } else {
1498 1516                  if (do_prefix)
1499 1517                          ret = changelist_postfix(cl);
1500 1518  
1501 1519                  /*
1502 1520                   * Refresh the statistics so the new property value
1503 1521                   * is reflected.
1504 1522                   */
1505 1523                  if (ret == 0)
1506 1524                          (void) get_stats(zhp);
1507 1525          }
1508 1526  
1509 1527  error:
1510 1528          nvlist_free(nvl);
1511 1529          zcmd_free_nvlists(&zc);
1512 1530          if (cl)
1513 1531                  changelist_free(cl);
1514 1532          return (ret);
1515 1533  }
1516 1534  
1517 1535  /*
1518 1536   * Given a property, inherit the value from the parent dataset, or if received
1519 1537   * is TRUE, revert to the received value, if any.
1520 1538   */
1521 1539  int
1522 1540  zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1523 1541  {
1524 1542          zfs_cmd_t zc = { 0 };
1525 1543          int ret;
1526 1544          prop_changelist_t *cl;
1527 1545          libzfs_handle_t *hdl = zhp->zfs_hdl;
1528 1546          char errbuf[1024];
1529 1547          zfs_prop_t prop;
1530 1548  
1531 1549          (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1532 1550              "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1533 1551  
1534 1552          zc.zc_cookie = received;
1535 1553          if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1536 1554                  /*
1537 1555                   * For user properties, the amount of work we have to do is very
1538 1556                   * small, so just do it here.
1539 1557                   */
1540 1558                  if (!zfs_prop_user(propname)) {
1541 1559                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1542 1560                              "invalid property"));
1543 1561                          return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1544 1562                  }
1545 1563  
1546 1564                  (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1547 1565                  (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1548 1566  
1549 1567                  if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1550 1568                          return (zfs_standard_error(hdl, errno, errbuf));
1551 1569  
1552 1570                  return (0);
1553 1571          }
1554 1572  
1555 1573          /*
1556 1574           * Verify that this property is inheritable.
1557 1575           */
1558 1576          if (zfs_prop_readonly(prop))
1559 1577                  return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1560 1578  
1561 1579          if (!zfs_prop_inheritable(prop) && !received)
1562 1580                  return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1563 1581  
1564 1582          /*
1565 1583           * Check to see if the value applies to this type
1566 1584           */
1567 1585          if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1568 1586                  return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1569 1587  
1570 1588          /*
1571 1589           * Normalize the name, to get rid of shorthand abbreviations.
1572 1590           */
1573 1591          propname = zfs_prop_to_name(prop);
1574 1592          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1575 1593          (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1576 1594  
1577 1595          if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1578 1596              zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1579 1597                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1580 1598                      "dataset is used in a non-global zone"));
1581 1599                  return (zfs_error(hdl, EZFS_ZONED, errbuf));
1582 1600          }
1583 1601  
1584 1602          /*
1585 1603           * Determine datasets which will be affected by this change, if any.
1586 1604           */
1587 1605          if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1588 1606                  return (-1);
1589 1607  
1590 1608          if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1591 1609                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1592 1610                      "child dataset with inherited mountpoint is used "
1593 1611                      "in a non-global zone"));
1594 1612                  ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1595 1613                  goto error;
1596 1614          }
1597 1615  
1598 1616          if ((ret = changelist_prefix(cl)) != 0)
1599 1617                  goto error;
1600 1618  
1601 1619          if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1602 1620                  return (zfs_standard_error(hdl, errno, errbuf));
1603 1621          } else {
1604 1622  
1605 1623                  if ((ret = changelist_postfix(cl)) != 0)
1606 1624                          goto error;
1607 1625  
1608 1626                  /*
1609 1627                   * Refresh the statistics so the new property is reflected.
1610 1628                   */
1611 1629                  (void) get_stats(zhp);
1612 1630          }
1613 1631  
1614 1632  error:
1615 1633          changelist_free(cl);
1616 1634          return (ret);
1617 1635  }
1618 1636  
1619 1637  /*
1620 1638   * True DSL properties are stored in an nvlist.  The following two functions
1621 1639   * extract them appropriately.
1622 1640   */
1623 1641  static uint64_t
1624 1642  getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1625 1643  {
1626 1644          nvlist_t *nv;
1627 1645          uint64_t value;
1628 1646  
1629 1647          *source = NULL;
1630 1648          if (nvlist_lookup_nvlist(zhp->zfs_props,
1631 1649              zfs_prop_to_name(prop), &nv) == 0) {
1632 1650                  verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1633 1651                  (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1634 1652          } else {
1635 1653                  verify(!zhp->zfs_props_table ||
1636 1654                      zhp->zfs_props_table[prop] == B_TRUE);
1637 1655                  value = zfs_prop_default_numeric(prop);
1638 1656                  *source = "";
1639 1657          }
1640 1658  
1641 1659          return (value);
1642 1660  }
1643 1661  
1644 1662  static char *
1645 1663  getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1646 1664  {
1647 1665          nvlist_t *nv;
1648 1666          char *value;
1649 1667  
1650 1668          *source = NULL;
1651 1669          if (nvlist_lookup_nvlist(zhp->zfs_props,
1652 1670              zfs_prop_to_name(prop), &nv) == 0) {
1653 1671                  verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1654 1672                  (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1655 1673          } else {
1656 1674                  verify(!zhp->zfs_props_table ||
1657 1675                      zhp->zfs_props_table[prop] == B_TRUE);
1658 1676                  if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1659 1677                          value = "";
1660 1678                  *source = "";
1661 1679          }
1662 1680  
1663 1681          return (value);
1664 1682  }
1665 1683  
1666 1684  static boolean_t
1667 1685  zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1668 1686  {
1669 1687          return (zhp->zfs_props == zhp->zfs_recvd_props);
1670 1688  }
1671 1689  
1672 1690  static void
1673 1691  zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1674 1692  {
1675 1693          *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1676 1694          zhp->zfs_props = zhp->zfs_recvd_props;
1677 1695  }
1678 1696  
1679 1697  static void
1680 1698  zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1681 1699  {
1682 1700          zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1683 1701          *cookie = 0;
1684 1702  }
1685 1703  
1686 1704  /*
1687 1705   * Internal function for getting a numeric property.  Both zfs_prop_get() and
1688 1706   * zfs_prop_get_int() are built using this interface.
1689 1707   *
1690 1708   * Certain properties can be overridden using 'mount -o'.  In this case, scan
1691 1709   * the contents of the /etc/mnttab entry, searching for the appropriate options.
1692 1710   * If they differ from the on-disk values, report the current values and mark
1693 1711   * the source "temporary".
1694 1712   */
1695 1713  static int
1696 1714  get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1697 1715      char **source, uint64_t *val)
1698 1716  {
1699 1717          zfs_cmd_t zc = { 0 };
1700 1718          nvlist_t *zplprops = NULL;
1701 1719          struct mnttab mnt;
1702 1720          char *mntopt_on = NULL;
1703 1721          char *mntopt_off = NULL;
1704 1722          boolean_t received = zfs_is_recvd_props_mode(zhp);
1705 1723  
1706 1724          *source = NULL;
1707 1725  
1708 1726          switch (prop) {
1709 1727          case ZFS_PROP_ATIME:
1710 1728                  mntopt_on = MNTOPT_ATIME;
1711 1729                  mntopt_off = MNTOPT_NOATIME;
1712 1730                  break;
1713 1731  
1714 1732          case ZFS_PROP_DEVICES:
1715 1733                  mntopt_on = MNTOPT_DEVICES;
1716 1734                  mntopt_off = MNTOPT_NODEVICES;
1717 1735                  break;
1718 1736  
1719 1737          case ZFS_PROP_EXEC:
1720 1738                  mntopt_on = MNTOPT_EXEC;
1721 1739                  mntopt_off = MNTOPT_NOEXEC;
1722 1740                  break;
1723 1741  
1724 1742          case ZFS_PROP_READONLY:
1725 1743                  mntopt_on = MNTOPT_RO;
1726 1744                  mntopt_off = MNTOPT_RW;
1727 1745                  break;
1728 1746  
1729 1747          case ZFS_PROP_SETUID:
1730 1748                  mntopt_on = MNTOPT_SETUID;
1731 1749                  mntopt_off = MNTOPT_NOSETUID;
1732 1750                  break;
1733 1751  
1734 1752          case ZFS_PROP_XATTR:
1735 1753                  mntopt_on = MNTOPT_XATTR;
1736 1754                  mntopt_off = MNTOPT_NOXATTR;
1737 1755                  break;
1738 1756  
1739 1757          case ZFS_PROP_NBMAND:
1740 1758                  mntopt_on = MNTOPT_NBMAND;
1741 1759                  mntopt_off = MNTOPT_NONBMAND;
1742 1760                  break;
1743 1761          }
1744 1762  
1745 1763          /*
1746 1764           * Because looking up the mount options is potentially expensive
1747 1765           * (iterating over all of /etc/mnttab), we defer its calculation until
1748 1766           * we're looking up a property which requires its presence.
1749 1767           */
1750 1768          if (!zhp->zfs_mntcheck &&
1751 1769              (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1752 1770                  libzfs_handle_t *hdl = zhp->zfs_hdl;
1753 1771                  struct mnttab entry;
1754 1772  
1755 1773                  if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1756 1774                          zhp->zfs_mntopts = zfs_strdup(hdl,
1757 1775                              entry.mnt_mntopts);
1758 1776                          if (zhp->zfs_mntopts == NULL)
1759 1777                                  return (-1);
1760 1778                  }
1761 1779  
1762 1780                  zhp->zfs_mntcheck = B_TRUE;
1763 1781          }
1764 1782  
1765 1783          if (zhp->zfs_mntopts == NULL)
1766 1784                  mnt.mnt_mntopts = "";
1767 1785          else
1768 1786                  mnt.mnt_mntopts = zhp->zfs_mntopts;
1769 1787  
1770 1788          switch (prop) {
1771 1789          case ZFS_PROP_ATIME:
1772 1790          case ZFS_PROP_DEVICES:
1773 1791          case ZFS_PROP_EXEC:
1774 1792          case ZFS_PROP_READONLY:
1775 1793          case ZFS_PROP_SETUID:
1776 1794          case ZFS_PROP_XATTR:
1777 1795          case ZFS_PROP_NBMAND:
1778 1796                  *val = getprop_uint64(zhp, prop, source);
1779 1797  
1780 1798                  if (received)
1781 1799                          break;
1782 1800  
1783 1801                  if (hasmntopt(&mnt, mntopt_on) && !*val) {
1784 1802                          *val = B_TRUE;
1785 1803                          if (src)
1786 1804                                  *src = ZPROP_SRC_TEMPORARY;
1787 1805                  } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1788 1806                          *val = B_FALSE;
1789 1807                          if (src)
1790 1808                                  *src = ZPROP_SRC_TEMPORARY;
1791 1809                  }
1792 1810                  break;
1793 1811  
1794 1812          case ZFS_PROP_CANMOUNT:
1795 1813          case ZFS_PROP_VOLSIZE:
1796 1814          case ZFS_PROP_QUOTA:
1797 1815          case ZFS_PROP_REFQUOTA:
1798 1816          case ZFS_PROP_RESERVATION:
1799 1817          case ZFS_PROP_REFRESERVATION:
1800 1818                  *val = getprop_uint64(zhp, prop, source);
1801 1819  
1802 1820                  if (*source == NULL) {
1803 1821                          /* not default, must be local */
1804 1822                          *source = zhp->zfs_name;
1805 1823                  }
1806 1824                  break;
1807 1825  
1808 1826          case ZFS_PROP_MOUNTED:
1809 1827                  *val = (zhp->zfs_mntopts != NULL);
1810 1828                  break;
1811 1829  
1812 1830          case ZFS_PROP_NUMCLONES:
1813 1831                  *val = zhp->zfs_dmustats.dds_num_clones;
1814 1832                  break;
1815 1833  
1816 1834          case ZFS_PROP_VERSION:
1817 1835          case ZFS_PROP_NORMALIZE:
1818 1836          case ZFS_PROP_UTF8ONLY:
1819 1837          case ZFS_PROP_CASE:
1820 1838                  if (zhp->zfs_hdl->libzfs_cachedprops) {
1821 1839                          return (zfs_error(zhp->zfs_hdl, EZFS_PROPCACHED,
1822 1840                              "property unavailable since not cached"));
1823 1841                  }
1824 1842  
1825 1843                  if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1826 1844                      zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1827 1845                          return (-1);
1828 1846                  (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1829 1847                  if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1830 1848                          zcmd_free_nvlists(&zc);
1831 1849                          return (-1);
1832 1850                  }
1833 1851                  if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1834 1852                      nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1835 1853                      val) != 0) {
1836 1854                          zcmd_free_nvlists(&zc);
1837 1855                          return (-1);
1838 1856                  }
1839 1857                  if (zplprops)
1840 1858                          nvlist_free(zplprops);
1841 1859                  zcmd_free_nvlists(&zc);
1842 1860                  break;
1843 1861  
1844 1862          default:
1845 1863                  switch (zfs_prop_get_type(prop)) {
1846 1864                  case PROP_TYPE_NUMBER:
1847 1865                  case PROP_TYPE_INDEX:
1848 1866                          *val = getprop_uint64(zhp, prop, source);
1849 1867                          /*
1850 1868                           * If we tried to use a default value for a
1851 1869                           * readonly property, it means that it was not
1852 1870                           * present.
1853 1871                           */
1854 1872                          if (zfs_prop_readonly(prop) &&
1855 1873                              *source != NULL && (*source)[0] == '\0') {
1856 1874                                  *source = NULL;
1857 1875                          }
1858 1876                          break;
1859 1877  
1860 1878                  case PROP_TYPE_STRING:
1861 1879                  default:
1862 1880                          zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1863 1881                              "cannot get non-numeric property"));
1864 1882                          return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1865 1883                              dgettext(TEXT_DOMAIN, "internal error")));
1866 1884                  }
1867 1885          }
1868 1886  
1869 1887          return (0);
1870 1888  }
1871 1889  
1872 1890  /*
1873 1891   * Calculate the source type, given the raw source string.
1874 1892   */
1875 1893  static void
1876 1894  get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1877 1895      char *statbuf, size_t statlen)
1878 1896  {
1879 1897          if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1880 1898                  return;
1881 1899  
1882 1900          if (source == NULL) {
1883 1901                  *srctype = ZPROP_SRC_NONE;
1884 1902          } else if (source[0] == '\0') {
1885 1903                  *srctype = ZPROP_SRC_DEFAULT;
1886 1904          } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1887 1905                  *srctype = ZPROP_SRC_RECEIVED;
1888 1906          } else {
1889 1907                  if (strcmp(source, zhp->zfs_name) == 0) {
1890 1908                          *srctype = ZPROP_SRC_LOCAL;
1891 1909                  } else {
1892 1910                          (void) strlcpy(statbuf, source, statlen);
1893 1911                          *srctype = ZPROP_SRC_INHERITED;
1894 1912                  }
1895 1913          }
1896 1914  
1897 1915  }
1898 1916  
1899 1917  int
1900 1918  zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1901 1919      size_t proplen, boolean_t literal)
1902 1920  {
1903 1921          zfs_prop_t prop;
1904 1922          int err = 0;
1905 1923  
1906 1924          if (zhp->zfs_recvd_props == NULL)
1907 1925                  if (get_recvd_props_ioctl(zhp) != 0)
1908 1926                          return (-1);
1909 1927  
1910 1928          prop = zfs_name_to_prop(propname);
1911 1929  
1912 1930          if (prop != ZPROP_INVAL) {
1913 1931                  uint64_t cookie;
1914 1932                  if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1915 1933                          return (-1);
1916 1934                  zfs_set_recvd_props_mode(zhp, &cookie);
1917 1935                  err = zfs_prop_get(zhp, prop, propbuf, proplen,
1918 1936                      NULL, NULL, 0, literal);
1919 1937                  zfs_unset_recvd_props_mode(zhp, &cookie);
1920 1938          } else {
1921 1939                  nvlist_t *propval;
1922 1940                  char *recvdval;
1923 1941                  if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1924 1942                      propname, &propval) != 0)
1925 1943                          return (-1);
1926 1944                  verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1927 1945                      &recvdval) == 0);
1928 1946                  (void) strlcpy(propbuf, recvdval, proplen);
1929 1947          }
1930 1948  
1931 1949          return (err == 0 ? 0 : -1);
1932 1950  }
1933 1951  
1934 1952  static int
1935 1953  get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1936 1954  {
1937 1955          nvlist_t *value;
1938 1956          nvpair_t *pair;
1939 1957  
1940 1958          value = zfs_get_clones_nvl(zhp);
1941 1959          if (value == NULL)
1942 1960                  return (-1);
1943 1961  
1944 1962          propbuf[0] = '\0';
1945 1963          for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1946 1964              pair = nvlist_next_nvpair(value, pair)) {
1947 1965                  if (propbuf[0] != '\0')
1948 1966                          (void) strlcat(propbuf, ",", proplen);
1949 1967                  (void) strlcat(propbuf, nvpair_name(pair), proplen);
1950 1968          }
1951 1969  
1952 1970          return (0);
1953 1971  }
1954 1972  
1955 1973  struct get_clones_arg {
1956 1974          uint64_t numclones;
1957 1975          nvlist_t *value;
1958 1976          const char *origin;
1959 1977          char buf[ZFS_MAXNAMELEN];
1960 1978  };
1961 1979  
1962 1980  int
1963 1981  get_clones_cb(zfs_handle_t *zhp, void *arg)
1964 1982  {
1965 1983          struct get_clones_arg *gca = arg;
1966 1984  
1967 1985          if (gca->numclones == 0) {
1968 1986                  zfs_close(zhp);
1969 1987                  return (0);
1970 1988          }
1971 1989  
1972 1990          if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
1973 1991              NULL, NULL, 0, B_TRUE) != 0)
1974 1992                  goto out;
1975 1993          if (strcmp(gca->buf, gca->origin) == 0) {
1976 1994                  if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
1977 1995                          zfs_close(zhp);
1978 1996                          return (no_memory(zhp->zfs_hdl));
1979 1997                  }
1980 1998                  gca->numclones--;
1981 1999          }
1982 2000  
1983 2001  out:
1984 2002          (void) zfs_iter_children(zhp, get_clones_cb, gca);
1985 2003          zfs_close(zhp);
1986 2004          return (0);
1987 2005  }
1988 2006  
1989 2007  nvlist_t *
1990 2008  zfs_get_clones_nvl(zfs_handle_t *zhp)
1991 2009  {
1992 2010          nvlist_t *nv, *value;
1993 2011  
1994 2012          if (nvlist_lookup_nvlist(zhp->zfs_props,
1995 2013              zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
1996 2014                  struct get_clones_arg gca;
1997 2015  
1998 2016                  /*
1999 2017                   * if this is a snapshot, then the kernel wasn't able
2000 2018                   * to get the clones.  Do it by slowly iterating.
2001 2019                   */
2002 2020                  if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2003 2021                          return (NULL);
2004 2022                  if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2005 2023                          return (NULL);
2006 2024                  if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2007 2025                          nvlist_free(nv);
2008 2026                          return (NULL);
2009 2027                  }
2010 2028  
2011 2029                  gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2012 2030                  gca.value = value;
2013 2031                  gca.origin = zhp->zfs_name;
2014 2032  
2015 2033                  if (gca.numclones != 0) {
2016 2034                          zfs_handle_t *root;
2017 2035                          char pool[ZFS_MAXNAMELEN];
2018 2036                          char *cp = pool;
2019 2037  
2020 2038                          /* get the pool name */
2021 2039                          (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2022 2040                          (void) strsep(&cp, "/@");
2023 2041                          root = zfs_open(zhp->zfs_hdl, pool,
2024 2042                              ZFS_TYPE_FILESYSTEM);
2025 2043  
2026 2044                          (void) get_clones_cb(root, &gca);
2027 2045                  }
2028 2046  
2029 2047                  if (gca.numclones != 0 ||
2030 2048                      nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2031 2049                      nvlist_add_nvlist(zhp->zfs_props,
2032 2050                      zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2033 2051                          nvlist_free(nv);
2034 2052                          nvlist_free(value);
2035 2053                          return (NULL);
2036 2054                  }
2037 2055                  nvlist_free(nv);
2038 2056                  nvlist_free(value);
2039 2057                  verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2040 2058                      zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2041 2059          }
2042 2060  
2043 2061          verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2044 2062  
2045 2063          return (value);
2046 2064  }
2047 2065  
2048 2066  /*
2049 2067   * Retrieve a property from the given object.  If 'literal' is specified, then
2050 2068   * numbers are left as exact values.  Otherwise, numbers are converted to a
2051 2069   * human-readable form.
2052 2070   *
2053 2071   * Returns 0 on success, or -1 on error.
2054 2072   */
2055 2073  int
2056 2074  zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2057 2075      zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2058 2076  {
2059 2077          char *source = NULL;
2060 2078          uint64_t val;
2061 2079          char *str;
2062 2080          const char *strval;
2063 2081          boolean_t received = zfs_is_recvd_props_mode(zhp);
2064 2082          boolean_t printerr;
2065 2083  
2066 2084          /*
2067 2085           * Check to see if this property applies to our object
2068 2086           */
2069 2087          if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2070 2088                  return (-1);
2071 2089  
2072 2090          if (received && zfs_prop_readonly(prop))
2073 2091                  return (-1);
2074 2092  
2075 2093          if (zhp->zfs_hdl->libzfs_cachedprops &&
2076 2094              zfs_prop_cacheable(prop)) {
2077 2095                  printerr = zhp->zfs_hdl->libzfs_printerr;
2078 2096                  libzfs_print_on_error(zhp->zfs_hdl, B_FALSE);
2079 2097                  (void) zfs_error(zhp->zfs_hdl, EZFS_PROPCACHED,
2080 2098                      "property unavailable since not cached");
2081 2099                  libzfs_print_on_error(zhp->zfs_hdl, printerr);
2082 2100                  return (-1);
2083 2101          }
2084 2102  
2085 2103          if (src)
2086 2104                  *src = ZPROP_SRC_NONE;
2087 2105  
2088 2106          switch (prop) {
2089 2107          case ZFS_PROP_CREATION:
2090 2108                  /*
2091 2109                   * 'creation' is a time_t stored in the statistics.  We convert
2092 2110                   * this into a string unless 'literal' is specified.
2093 2111                   */
2094 2112                  {
2095 2113                          val = getprop_uint64(zhp, prop, &source);
2096 2114                          time_t time = (time_t)val;
2097 2115                          struct tm t;
2098 2116  
2099 2117                          if (literal ||
2100 2118                              localtime_r(&time, &t) == NULL ||
2101 2119                              strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2102 2120                              &t) == 0)
2103 2121                                  (void) snprintf(propbuf, proplen, "%llu", val);
2104 2122                  }
2105 2123                  break;
2106 2124  
2107 2125          case ZFS_PROP_MOUNTPOINT:
2108 2126                  /*
2109 2127                   * Getting the precise mountpoint can be tricky.
2110 2128                   *
2111 2129                   *  - for 'none' or 'legacy', return those values.
2112 2130                   *  - for inherited mountpoints, we want to take everything
2113 2131                   *    after our ancestor and append it to the inherited value.
2114 2132                   *
2115 2133                   * If the pool has an alternate root, we want to prepend that
2116 2134                   * root to any values we return.
2117 2135                   */
2118 2136  
2119 2137                  str = getprop_string(zhp, prop, &source);
2120 2138  
2121 2139                  if (str[0] == '/') {
2122 2140                          char buf[MAXPATHLEN];
2123 2141                          char *root = buf;
2124 2142                          const char *relpath;
2125 2143  
2126 2144                          /*
2127 2145                           * If we inherit the mountpoint, even from a dataset
2128 2146                           * with a received value, the source will be the path of
2129 2147                           * the dataset we inherit from. If source is
2130 2148                           * ZPROP_SOURCE_VAL_RECVD, the received value is not
2131 2149                           * inherited.
2132 2150                           */
2133 2151                          if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2134 2152                                  relpath = "";
2135 2153                          } else {
2136 2154                                  relpath = zhp->zfs_name + strlen(source);
2137 2155                                  if (relpath[0] == '/')
2138 2156                                          relpath++;
2139 2157                          }
2140 2158  
2141 2159                          if ((zpool_get_prop(zhp->zpool_hdl,
2142 2160                              ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2143 2161                              B_FALSE)) || (strcmp(root, "-") == 0))
2144 2162                                  root[0] = '\0';
2145 2163                          /*
2146 2164                           * Special case an alternate root of '/'. This will
2147 2165                           * avoid having multiple leading slashes in the
2148 2166                           * mountpoint path.
2149 2167                           */
2150 2168                          if (strcmp(root, "/") == 0)
2151 2169                                  root++;
2152 2170  
2153 2171                          /*
2154 2172                           * If the mountpoint is '/' then skip over this
2155 2173                           * if we are obtaining either an alternate root or
2156 2174                           * an inherited mountpoint.
2157 2175                           */
2158 2176                          if (str[1] == '\0' && (root[0] != '\0' ||
2159 2177                              relpath[0] != '\0'))
2160 2178                                  str++;
2161 2179  
2162 2180                          if (relpath[0] == '\0')
2163 2181                                  (void) snprintf(propbuf, proplen, "%s%s",
2164 2182                                      root, str);
2165 2183                          else
2166 2184                                  (void) snprintf(propbuf, proplen, "%s%s%s%s",
2167 2185                                      root, str, relpath[0] == '@' ? "" : "/",
2168 2186                                      relpath);
2169 2187                  } else {
2170 2188                          /* 'legacy' or 'none' */
2171 2189                          (void) strlcpy(propbuf, str, proplen);
2172 2190                  }
2173 2191  
2174 2192                  break;
2175 2193  
2176 2194          case ZFS_PROP_ORIGIN:
2177 2195                  (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2178 2196                      proplen);
2179 2197                  /*
2180 2198                   * If there is no parent at all, return failure to indicate that
2181 2199                   * it doesn't apply to this dataset.
2182 2200                   */
2183 2201                  if (propbuf[0] == '\0')
2184 2202                          return (-1);
2185 2203                  break;
2186 2204  
2187 2205          case ZFS_PROP_CLONES:
2188 2206                  if (get_clones_string(zhp, propbuf, proplen) != 0)
2189 2207                          return (-1);
2190 2208                  break;
2191 2209  
2192 2210          case ZFS_PROP_QUOTA:
2193 2211          case ZFS_PROP_REFQUOTA:
2194 2212          case ZFS_PROP_RESERVATION:
2195 2213          case ZFS_PROP_REFRESERVATION:
2196 2214  
2197 2215                  if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2198 2216                          return (-1);
2199 2217  
2200 2218                  /*
2201 2219                   * If quota or reservation is 0, we translate this into 'none'
2202 2220                   * (unless literal is set), and indicate that it's the default
2203 2221                   * value.  Otherwise, we print the number nicely and indicate
2204 2222                   * that its set locally.
2205 2223                   */
2206 2224                  if (val == 0) {
2207 2225                          if (literal)
2208 2226                                  (void) strlcpy(propbuf, "0", proplen);
  
    | 
      ↓ open down ↓ | 
    1235 lines elided | 
    
      ↑ open up ↑ | 
  
2209 2227                          else
2210 2228                                  (void) strlcpy(propbuf, "none", proplen);
2211 2229                  } else {
2212 2230                          if (literal)
2213 2231                                  (void) snprintf(propbuf, proplen, "%llu",
2214 2232                                      (u_longlong_t)val);
2215 2233                          else
2216 2234                                  zfs_nicenum(val, propbuf, proplen);
2217 2235                  }
2218 2236                  break;
     2237 +
     2238 +        case ZFS_PROP_FILESYSTEM_LIMIT:
     2239 +        case ZFS_PROP_SNAPSHOT_LIMIT:
     2240 +
     2241 +                if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
     2242 +                        return (-1);
     2243 +
     2244 +                /*
     2245 +                 * If limit is MAXLIMIT, we translate this into 'none' (unless
     2246 +                 * literal is set), and indicate that it's the default value.
     2247 +                 * Otherwise, we print the number nicely and indicate that its
     2248 +                 * set locally.
     2249 +                 */
     2250 +                if (literal) {
     2251 +                        (void) snprintf(propbuf, proplen, "%llu",
     2252 +                            (u_longlong_t)val);
     2253 +                } else if (val == MAXLIMIT) {
     2254 +                        (void) strlcpy(propbuf, "none", proplen);
     2255 +                } else {
     2256 +                        zfs_nicenum(val, propbuf, proplen);
     2257 +                }
     2258 +                break;
2219 2259  
2220 2260          case ZFS_PROP_REFRATIO:
2221 2261          case ZFS_PROP_COMPRESSRATIO:
2222 2262                  if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2223 2263                          return (-1);
2224 2264                  (void) snprintf(propbuf, proplen, "%llu.%02llux",
2225 2265                      (u_longlong_t)(val / 100),
2226 2266                      (u_longlong_t)(val % 100));
2227 2267                  break;
2228 2268  
2229 2269          case ZFS_PROP_TYPE:
2230 2270                  switch (zhp->zfs_type) {
2231 2271                  case ZFS_TYPE_FILESYSTEM:
2232 2272                          str = "filesystem";
2233 2273                          break;
2234 2274                  case ZFS_TYPE_VOLUME:
2235 2275                          str = "volume";
2236 2276                          break;
2237 2277                  case ZFS_TYPE_SNAPSHOT:
2238 2278                          str = "snapshot";
2239 2279                          break;
2240 2280                  default:
2241 2281                          abort();
2242 2282                  }
2243 2283                  (void) snprintf(propbuf, proplen, "%s", str);
2244 2284                  break;
2245 2285  
2246 2286          case ZFS_PROP_MOUNTED:
2247 2287                  /*
2248 2288                   * The 'mounted' property is a pseudo-property that described
2249 2289                   * whether the filesystem is currently mounted.  Even though
2250 2290                   * it's a boolean value, the typical values of "on" and "off"
2251 2291                   * don't make sense, so we translate to "yes" and "no".
2252 2292                   */
2253 2293                  if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2254 2294                      src, &source, &val) != 0)
2255 2295                          return (-1);
2256 2296                  if (val)
2257 2297                          (void) strlcpy(propbuf, "yes", proplen);
2258 2298                  else
2259 2299                          (void) strlcpy(propbuf, "no", proplen);
2260 2300                  break;
2261 2301  
2262 2302          case ZFS_PROP_NAME:
2263 2303                  /*
2264 2304                   * The 'name' property is a pseudo-property derived from the
2265 2305                   * dataset name.  It is presented as a real property to simplify
2266 2306                   * consumers.
2267 2307                   */
2268 2308                  (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2269 2309                  break;
2270 2310  
2271 2311          case ZFS_PROP_MLSLABEL:
2272 2312                  {
2273 2313                          m_label_t *new_sl = NULL;
2274 2314                          char *ascii = NULL;     /* human readable label */
2275 2315  
2276 2316                          (void) strlcpy(propbuf,
2277 2317                              getprop_string(zhp, prop, &source), proplen);
2278 2318  
2279 2319                          if (literal || (strcasecmp(propbuf,
2280 2320                              ZFS_MLSLABEL_DEFAULT) == 0))
2281 2321                                  break;
2282 2322  
2283 2323                          /*
2284 2324                           * Try to translate the internal hex string to
2285 2325                           * human-readable output.  If there are any
2286 2326                           * problems just use the hex string.
2287 2327                           */
2288 2328  
2289 2329                          if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2290 2330                              L_NO_CORRECTION, NULL) == -1) {
2291 2331                                  m_label_free(new_sl);
2292 2332                                  break;
2293 2333                          }
2294 2334  
2295 2335                          if (label_to_str(new_sl, &ascii, M_LABEL,
2296 2336                              DEF_NAMES) != 0) {
2297 2337                                  if (ascii)
2298 2338                                          free(ascii);
2299 2339                                  m_label_free(new_sl);
2300 2340                                  break;
2301 2341                          }
2302 2342                          m_label_free(new_sl);
2303 2343  
2304 2344                          (void) strlcpy(propbuf, ascii, proplen);
2305 2345                          free(ascii);
2306 2346                  }
2307 2347                  break;
2308 2348  
2309 2349          case ZFS_PROP_GUID:
2310 2350                  /*
2311 2351                   * GUIDs are stored as numbers, but they are identifiers.
2312 2352                   * We don't want them to be pretty printed, because pretty
2313 2353                   * printing mangles the ID into a truncated and useless value.
2314 2354                   */
2315 2355                  if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2316 2356                          return (-1);
2317 2357                  (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2318 2358                  break;
2319 2359  
2320 2360          default:
2321 2361                  switch (zfs_prop_get_type(prop)) {
2322 2362                  case PROP_TYPE_NUMBER:
2323 2363                          if (get_numeric_property(zhp, prop, src,
2324 2364                              &source, &val) != 0)
2325 2365                                  return (-1);
2326 2366                          if (literal)
2327 2367                                  (void) snprintf(propbuf, proplen, "%llu",
2328 2368                                      (u_longlong_t)val);
2329 2369                          else
2330 2370                                  zfs_nicenum(val, propbuf, proplen);
2331 2371                          break;
2332 2372  
2333 2373                  case PROP_TYPE_STRING:
2334 2374                          (void) strlcpy(propbuf,
2335 2375                              getprop_string(zhp, prop, &source), proplen);
2336 2376                          break;
2337 2377  
2338 2378                  case PROP_TYPE_INDEX:
2339 2379                          if (get_numeric_property(zhp, prop, src,
2340 2380                              &source, &val) != 0)
2341 2381                                  return (-1);
2342 2382                          if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2343 2383                                  return (-1);
2344 2384                          (void) strlcpy(propbuf, strval, proplen);
2345 2385                          break;
2346 2386  
2347 2387                  default:
2348 2388                          abort();
2349 2389                  }
2350 2390          }
2351 2391  
2352 2392          get_source(zhp, src, source, statbuf, statlen);
2353 2393  
2354 2394          return (0);
2355 2395  }
2356 2396  
2357 2397  /*
2358 2398   * Utility function to get the given numeric property.  Does no validation that
2359 2399   * the given property is the appropriate type; should only be used with
2360 2400   * hard-coded property types.
2361 2401   */
2362 2402  uint64_t
2363 2403  zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2364 2404  {
2365 2405          char *source;
2366 2406          uint64_t val;
2367 2407  
2368 2408          (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2369 2409  
2370 2410          return (val);
2371 2411  }
2372 2412  
2373 2413  int
2374 2414  zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2375 2415  {
2376 2416          char buf[64];
2377 2417  
2378 2418          (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2379 2419          return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2380 2420  }
2381 2421  
2382 2422  /*
2383 2423   * Similar to zfs_prop_get(), but returns the value as an integer.
2384 2424   */
2385 2425  int
2386 2426  zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2387 2427      zprop_source_t *src, char *statbuf, size_t statlen)
2388 2428  {
2389 2429          char *source;
2390 2430  
2391 2431          /*
2392 2432           * Check to see if this property applies to our object
2393 2433           */
2394 2434          if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2395 2435                  return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2396 2436                      dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2397 2437                      zfs_prop_to_name(prop)));
2398 2438          }
2399 2439  
2400 2440          if (src)
2401 2441                  *src = ZPROP_SRC_NONE;
2402 2442  
2403 2443          if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2404 2444                  return (-1);
2405 2445  
2406 2446          get_source(zhp, src, source, statbuf, statlen);
2407 2447  
2408 2448          return (0);
2409 2449  }
2410 2450  
2411 2451  static int
2412 2452  idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2413 2453      char **domainp, idmap_rid_t *ridp)
2414 2454  {
2415 2455          idmap_get_handle_t *get_hdl = NULL;
2416 2456          idmap_stat status;
2417 2457          int err = EINVAL;
2418 2458  
2419 2459          if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2420 2460                  goto out;
2421 2461  
2422 2462          if (isuser) {
2423 2463                  err = idmap_get_sidbyuid(get_hdl, id,
2424 2464                      IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2425 2465          } else {
2426 2466                  err = idmap_get_sidbygid(get_hdl, id,
2427 2467                      IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2428 2468          }
2429 2469          if (err == IDMAP_SUCCESS &&
2430 2470              idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2431 2471              status == IDMAP_SUCCESS)
2432 2472                  err = 0;
2433 2473          else
2434 2474                  err = EINVAL;
2435 2475  out:
2436 2476          if (get_hdl)
2437 2477                  idmap_get_destroy(get_hdl);
2438 2478          return (err);
2439 2479  }
2440 2480  
2441 2481  /*
2442 2482   * convert the propname into parameters needed by kernel
2443 2483   * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2444 2484   * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2445 2485   */
2446 2486  static int
2447 2487  userquota_propname_decode(const char *propname, boolean_t zoned,
2448 2488      zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2449 2489  {
2450 2490          zfs_userquota_prop_t type;
2451 2491          char *cp, *end;
2452 2492          char *numericsid = NULL;
2453 2493          boolean_t isuser;
2454 2494  
2455 2495          domain[0] = '\0';
2456 2496  
2457 2497          /* Figure out the property type ({user|group}{quota|space}) */
2458 2498          for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2459 2499                  if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2460 2500                      strlen(zfs_userquota_prop_prefixes[type])) == 0)
2461 2501                          break;
2462 2502          }
2463 2503          if (type == ZFS_NUM_USERQUOTA_PROPS)
2464 2504                  return (EINVAL);
2465 2505          *typep = type;
2466 2506  
2467 2507          isuser = (type == ZFS_PROP_USERQUOTA ||
2468 2508              type == ZFS_PROP_USERUSED);
2469 2509  
2470 2510          cp = strchr(propname, '@') + 1;
2471 2511  
2472 2512          if (strchr(cp, '@')) {
2473 2513                  /*
2474 2514                   * It's a SID name (eg "user@domain") that needs to be
2475 2515                   * turned into S-1-domainID-RID.
2476 2516                   */
2477 2517                  directory_error_t e;
2478 2518                  if (zoned && getzoneid() == GLOBAL_ZONEID)
2479 2519                          return (ENOENT);
2480 2520                  if (isuser) {
2481 2521                          e = directory_sid_from_user_name(NULL,
2482 2522                              cp, &numericsid);
2483 2523                  } else {
2484 2524                          e = directory_sid_from_group_name(NULL,
2485 2525                              cp, &numericsid);
2486 2526                  }
2487 2527                  if (e != NULL) {
2488 2528                          directory_error_free(e);
2489 2529                          return (ENOENT);
2490 2530                  }
2491 2531                  if (numericsid == NULL)
2492 2532                          return (ENOENT);
2493 2533                  cp = numericsid;
2494 2534                  /* will be further decoded below */
2495 2535          }
2496 2536  
2497 2537          if (strncmp(cp, "S-1-", 4) == 0) {
2498 2538                  /* It's a numeric SID (eg "S-1-234-567-89") */
2499 2539                  (void) strlcpy(domain, cp, domainlen);
2500 2540                  cp = strrchr(domain, '-');
2501 2541                  *cp = '\0';
2502 2542                  cp++;
2503 2543  
2504 2544                  errno = 0;
2505 2545                  *ridp = strtoull(cp, &end, 10);
2506 2546                  if (numericsid) {
2507 2547                          free(numericsid);
2508 2548                          numericsid = NULL;
2509 2549                  }
2510 2550                  if (errno != 0 || *end != '\0')
2511 2551                          return (EINVAL);
2512 2552          } else if (!isdigit(*cp)) {
2513 2553                  /*
2514 2554                   * It's a user/group name (eg "user") that needs to be
2515 2555                   * turned into a uid/gid
2516 2556                   */
2517 2557                  if (zoned && getzoneid() == GLOBAL_ZONEID)
2518 2558                          return (ENOENT);
2519 2559                  if (isuser) {
2520 2560                          struct passwd *pw;
2521 2561                          pw = getpwnam(cp);
2522 2562                          if (pw == NULL)
2523 2563                                  return (ENOENT);
2524 2564                          *ridp = pw->pw_uid;
2525 2565                  } else {
2526 2566                          struct group *gr;
2527 2567                          gr = getgrnam(cp);
2528 2568                          if (gr == NULL)
2529 2569                                  return (ENOENT);
2530 2570                          *ridp = gr->gr_gid;
2531 2571                  }
2532 2572          } else {
2533 2573                  /* It's a user/group ID (eg "12345"). */
2534 2574                  uid_t id = strtoul(cp, &end, 10);
2535 2575                  idmap_rid_t rid;
2536 2576                  char *mapdomain;
2537 2577  
2538 2578                  if (*end != '\0')
2539 2579                          return (EINVAL);
2540 2580                  if (id > MAXUID) {
2541 2581                          /* It's an ephemeral ID. */
2542 2582                          if (idmap_id_to_numeric_domain_rid(id, isuser,
2543 2583                              &mapdomain, &rid) != 0)
2544 2584                                  return (ENOENT);
2545 2585                          (void) strlcpy(domain, mapdomain, domainlen);
2546 2586                          *ridp = rid;
2547 2587                  } else {
2548 2588                          *ridp = id;
2549 2589                  }
2550 2590          }
2551 2591  
2552 2592          ASSERT3P(numericsid, ==, NULL);
2553 2593          return (0);
2554 2594  }
2555 2595  
2556 2596  static int
2557 2597  zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2558 2598      uint64_t *propvalue, zfs_userquota_prop_t *typep)
2559 2599  {
2560 2600          int err;
2561 2601          zfs_cmd_t zc = { 0 };
2562 2602  
2563 2603          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2564 2604  
2565 2605          err = userquota_propname_decode(propname,
2566 2606              zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2567 2607              typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2568 2608          zc.zc_objset_type = *typep;
2569 2609          if (err)
2570 2610                  return (err);
2571 2611  
2572 2612          err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2573 2613          if (err)
2574 2614                  return (err);
2575 2615  
2576 2616          *propvalue = zc.zc_cookie;
2577 2617          return (0);
2578 2618  }
2579 2619  
2580 2620  int
2581 2621  zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2582 2622      uint64_t *propvalue)
2583 2623  {
2584 2624          zfs_userquota_prop_t type;
2585 2625  
2586 2626          return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2587 2627              &type));
2588 2628  }
2589 2629  
2590 2630  int
2591 2631  zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2592 2632      char *propbuf, int proplen, boolean_t literal)
2593 2633  {
2594 2634          int err;
2595 2635          uint64_t propvalue;
2596 2636          zfs_userquota_prop_t type;
2597 2637  
2598 2638          err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2599 2639              &type);
2600 2640  
2601 2641          if (err)
2602 2642                  return (err);
2603 2643  
2604 2644          if (literal) {
2605 2645                  (void) snprintf(propbuf, proplen, "%llu", propvalue);
2606 2646          } else if (propvalue == 0 &&
2607 2647              (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2608 2648                  (void) strlcpy(propbuf, "none", proplen);
2609 2649          } else {
2610 2650                  zfs_nicenum(propvalue, propbuf, proplen);
2611 2651          }
2612 2652          return (0);
2613 2653  }
2614 2654  
2615 2655  int
2616 2656  zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2617 2657      uint64_t *propvalue)
2618 2658  {
2619 2659          int err;
2620 2660          zfs_cmd_t zc = { 0 };
2621 2661          const char *snapname;
2622 2662  
2623 2663          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2624 2664  
2625 2665          snapname = strchr(propname, '@') + 1;
2626 2666          if (strchr(snapname, '@')) {
2627 2667                  (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2628 2668          } else {
2629 2669                  /* snapname is the short name, append it to zhp's fsname */
2630 2670                  char *cp;
2631 2671  
2632 2672                  (void) strlcpy(zc.zc_value, zhp->zfs_name,
2633 2673                      sizeof (zc.zc_value));
2634 2674                  cp = strchr(zc.zc_value, '@');
2635 2675                  if (cp != NULL)
2636 2676                          *cp = '\0';
2637 2677                  (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2638 2678                  (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2639 2679          }
2640 2680  
2641 2681          err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2642 2682          if (err)
2643 2683                  return (err);
2644 2684  
2645 2685          *propvalue = zc.zc_cookie;
2646 2686          return (0);
2647 2687  }
2648 2688  
2649 2689  int
2650 2690  zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2651 2691      char *propbuf, int proplen, boolean_t literal)
2652 2692  {
2653 2693          int err;
2654 2694          uint64_t propvalue;
2655 2695  
2656 2696          err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2657 2697  
2658 2698          if (err)
2659 2699                  return (err);
2660 2700  
2661 2701          if (literal) {
2662 2702                  (void) snprintf(propbuf, proplen, "%llu", propvalue);
2663 2703          } else {
2664 2704                  zfs_nicenum(propvalue, propbuf, proplen);
2665 2705          }
2666 2706          return (0);
2667 2707  }
2668 2708  
2669 2709  /*
2670 2710   * Returns the name of the given zfs handle.
2671 2711   */
2672 2712  const char *
2673 2713  zfs_get_name(const zfs_handle_t *zhp)
2674 2714  {
2675 2715          return (zhp->zfs_name);
2676 2716  }
2677 2717  
2678 2718  /*
2679 2719   * Returns the type of the given zfs handle.
2680 2720   */
2681 2721  zfs_type_t
2682 2722  zfs_get_type(const zfs_handle_t *zhp)
2683 2723  {
2684 2724          return (zhp->zfs_type);
2685 2725  }
2686 2726  
2687 2727  /*
2688 2728   * Is one dataset name a child dataset of another?
2689 2729   *
2690 2730   * Needs to handle these cases:
2691 2731   * Dataset 1    "a/foo"         "a/foo"         "a/foo"         "a/foo"
2692 2732   * Dataset 2    "a/fo"          "a/foobar"      "a/bar/baz"     "a/foo/bar"
2693 2733   * Descendant?  No.             No.             No.             Yes.
2694 2734   */
2695 2735  static boolean_t
2696 2736  is_descendant(const char *ds1, const char *ds2)
2697 2737  {
2698 2738          size_t d1len = strlen(ds1);
2699 2739  
2700 2740          /* ds2 can't be a descendant if it's smaller */
2701 2741          if (strlen(ds2) < d1len)
2702 2742                  return (B_FALSE);
2703 2743  
2704 2744          /* otherwise, compare strings and verify that there's a '/' char */
2705 2745          return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2706 2746  }
2707 2747  
2708 2748  /*
2709 2749   * Given a complete name, return just the portion that refers to the parent.
2710 2750   * Will return -1 if there is no parent (path is just the name of the
2711 2751   * pool).
2712 2752   */
2713 2753  static int
2714 2754  parent_name(const char *path, char *buf, size_t buflen)
2715 2755  {
2716 2756          char *slashp;
2717 2757  
2718 2758          (void) strlcpy(buf, path, buflen);
2719 2759  
2720 2760          if ((slashp = strrchr(buf, '/')) == NULL)
2721 2761                  return (-1);
2722 2762          *slashp = '\0';
2723 2763  
2724 2764          return (0);
2725 2765  }
2726 2766  
2727 2767  /*
2728 2768   * If accept_ancestor is false, then check to make sure that the given path has
2729 2769   * a parent, and that it exists.  If accept_ancestor is true, then find the
2730 2770   * closest existing ancestor for the given path.  In prefixlen return the
2731 2771   * length of already existing prefix of the given path.  We also fetch the
2732 2772   * 'zoned' property, which is used to validate property settings when creating
2733 2773   * new datasets.
2734 2774   */
2735 2775  static int
2736 2776  check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2737 2777      boolean_t accept_ancestor, int *prefixlen)
2738 2778  {
2739 2779          zfs_cmd_t zc = { 0 };
2740 2780          char parent[ZFS_MAXNAMELEN];
2741 2781          char *slash;
2742 2782          zfs_handle_t *zhp;
2743 2783          char errbuf[1024];
2744 2784          uint64_t is_zoned;
2745 2785  
2746 2786          (void) snprintf(errbuf, sizeof (errbuf),
2747 2787              dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2748 2788  
2749 2789          /* get parent, and check to see if this is just a pool */
2750 2790          if (parent_name(path, parent, sizeof (parent)) != 0) {
2751 2791                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2752 2792                      "missing dataset name"));
2753 2793                  return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2754 2794          }
2755 2795  
2756 2796          /* check to see if the pool exists */
2757 2797          if ((slash = strchr(parent, '/')) == NULL)
2758 2798                  slash = parent + strlen(parent);
2759 2799          (void) strncpy(zc.zc_name, parent, slash - parent);
2760 2800          zc.zc_name[slash - parent] = '\0';
2761 2801          if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2762 2802              errno == ENOENT) {
2763 2803                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2764 2804                      "no such pool '%s'"), zc.zc_name);
2765 2805                  return (zfs_error(hdl, EZFS_NOENT, errbuf));
2766 2806          }
2767 2807  
2768 2808          /* check to see if the parent dataset exists */
2769 2809          while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2770 2810                  if (errno == ENOENT && accept_ancestor) {
2771 2811                          /*
2772 2812                           * Go deeper to find an ancestor, give up on top level.
2773 2813                           */
2774 2814                          if (parent_name(parent, parent, sizeof (parent)) != 0) {
2775 2815                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2776 2816                                      "no such pool '%s'"), zc.zc_name);
2777 2817                                  return (zfs_error(hdl, EZFS_NOENT, errbuf));
2778 2818                          }
2779 2819                  } else if (errno == ENOENT) {
2780 2820                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2781 2821                              "parent does not exist"));
2782 2822                          return (zfs_error(hdl, EZFS_NOENT, errbuf));
2783 2823                  } else
2784 2824                          return (zfs_standard_error(hdl, errno, errbuf));
2785 2825          }
2786 2826  
2787 2827          is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2788 2828          if (zoned != NULL)
2789 2829                  *zoned = is_zoned;
2790 2830  
2791 2831          /* we are in a non-global zone, but parent is in the global zone */
2792 2832          if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2793 2833                  (void) zfs_standard_error(hdl, EPERM, errbuf);
2794 2834                  zfs_close(zhp);
2795 2835                  return (-1);
2796 2836          }
2797 2837  
2798 2838          /* make sure parent is a filesystem */
2799 2839          if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2800 2840                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2801 2841                      "parent is not a filesystem"));
2802 2842                  (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2803 2843                  zfs_close(zhp);
2804 2844                  return (-1);
2805 2845          }
2806 2846  
2807 2847          zfs_close(zhp);
2808 2848          if (prefixlen != NULL)
2809 2849                  *prefixlen = strlen(parent);
2810 2850          return (0);
2811 2851  }
2812 2852  
2813 2853  /*
2814 2854   * Finds whether the dataset of the given type(s) exists.
2815 2855   */
2816 2856  boolean_t
2817 2857  zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2818 2858  {
2819 2859          zfs_handle_t *zhp;
2820 2860  
2821 2861          if (!zfs_validate_name(hdl, path, types, B_FALSE))
2822 2862                  return (B_FALSE);
2823 2863  
2824 2864          /*
2825 2865           * Try to get stats for the dataset, which will tell us if it exists.
2826 2866           */
2827 2867          if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2828 2868                  int ds_type = zhp->zfs_type;
2829 2869  
2830 2870                  zfs_close(zhp);
2831 2871                  if (types & ds_type)
2832 2872                          return (B_TRUE);
2833 2873          }
2834 2874          return (B_FALSE);
2835 2875  }
2836 2876  
2837 2877  /*
2838 2878   * Given a path to 'target', create all the ancestors between
2839 2879   * the prefixlen portion of the path, and the target itself.
2840 2880   * Fail if the initial prefixlen-ancestor does not already exist.
2841 2881   */
2842 2882  int
2843 2883  create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2844 2884  {
2845 2885          zfs_handle_t *h;
2846 2886          char *cp;
2847 2887          const char *opname;
2848 2888  
2849 2889          /* make sure prefix exists */
2850 2890          cp = target + prefixlen;
2851 2891          if (*cp != '/') {
2852 2892                  assert(strchr(cp, '/') == NULL);
2853 2893                  h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2854 2894          } else {
2855 2895                  *cp = '\0';
2856 2896                  h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2857 2897                  *cp = '/';
2858 2898          }
2859 2899          if (h == NULL)
2860 2900                  return (-1);
2861 2901          zfs_close(h);
2862 2902  
2863 2903          /*
2864 2904           * Attempt to create, mount, and share any ancestor filesystems,
2865 2905           * up to the prefixlen-long one.
2866 2906           */
2867 2907          for (cp = target + prefixlen + 1;
2868 2908              cp = strchr(cp, '/'); *cp = '/', cp++) {
2869 2909  
2870 2910                  *cp = '\0';
2871 2911  
2872 2912                  h = make_dataset_handle(hdl, target);
2873 2913                  if (h) {
2874 2914                          /* it already exists, nothing to do here */
2875 2915                          zfs_close(h);
2876 2916                          continue;
2877 2917                  }
2878 2918  
2879 2919                  if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2880 2920                      NULL) != 0) {
2881 2921                          opname = dgettext(TEXT_DOMAIN, "create");
2882 2922                          goto ancestorerr;
2883 2923                  }
2884 2924  
2885 2925                  h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2886 2926                  if (h == NULL) {
2887 2927                          opname = dgettext(TEXT_DOMAIN, "open");
2888 2928                          goto ancestorerr;
2889 2929                  }
2890 2930  
2891 2931                  if (zfs_mount(h, NULL, 0) != 0) {
2892 2932                          opname = dgettext(TEXT_DOMAIN, "mount");
2893 2933                          goto ancestorerr;
2894 2934                  }
2895 2935  
2896 2936                  if (zfs_share(h) != 0) {
2897 2937                          opname = dgettext(TEXT_DOMAIN, "share");
2898 2938                          goto ancestorerr;
2899 2939                  }
2900 2940  
2901 2941                  zfs_close(h);
2902 2942          }
2903 2943  
2904 2944          return (0);
2905 2945  
2906 2946  ancestorerr:
2907 2947          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2908 2948              "failed to %s ancestor '%s'"), opname, target);
2909 2949          return (-1);
2910 2950  }
2911 2951  
2912 2952  /*
2913 2953   * Creates non-existing ancestors of the given path.
2914 2954   */
2915 2955  int
2916 2956  zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2917 2957  {
2918 2958          int prefix;
2919 2959          char *path_copy;
2920 2960          int rc;
2921 2961  
2922 2962          if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2923 2963                  return (-1);
2924 2964  
2925 2965          if ((path_copy = strdup(path)) != NULL) {
2926 2966                  rc = create_parents(hdl, path_copy, prefix);
2927 2967                  free(path_copy);
2928 2968          }
2929 2969          if (path_copy == NULL || rc != 0)
2930 2970                  return (-1);
2931 2971  
2932 2972          return (0);
2933 2973  }
2934 2974  
2935 2975  /*
2936 2976   * Create a new filesystem or volume.
2937 2977   */
2938 2978  int
2939 2979  zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2940 2980      nvlist_t *props)
2941 2981  {
2942 2982          int ret;
2943 2983          uint64_t size = 0;
2944 2984          uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2945 2985          char errbuf[1024];
2946 2986          uint64_t zoned;
2947 2987          dmu_objset_type_t ost;
2948 2988  
2949 2989          (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2950 2990              "cannot create '%s'"), path);
2951 2991  
2952 2992          /* validate the path, taking care to note the extended error message */
2953 2993          if (!zfs_validate_name(hdl, path, type, B_TRUE))
2954 2994                  return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2955 2995  
2956 2996          /* validate parents exist */
2957 2997          if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2958 2998                  return (-1);
2959 2999  
2960 3000          /*
2961 3001           * The failure modes when creating a dataset of a different type over
2962 3002           * one that already exists is a little strange.  In particular, if you
2963 3003           * try to create a dataset on top of an existing dataset, the ioctl()
2964 3004           * will return ENOENT, not EEXIST.  To prevent this from happening, we
2965 3005           * first try to see if the dataset exists.
2966 3006           */
2967 3007          if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
2968 3008                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2969 3009                      "dataset already exists"));
2970 3010                  return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2971 3011          }
2972 3012  
2973 3013          if (type == ZFS_TYPE_VOLUME)
2974 3014                  ost = DMU_OST_ZVOL;
2975 3015          else
2976 3016                  ost = DMU_OST_ZFS;
2977 3017  
2978 3018          if (props && (props = zfs_valid_proplist(hdl, type, props,
2979 3019              zoned, NULL, errbuf)) == 0)
2980 3020                  return (-1);
2981 3021  
2982 3022          if (type == ZFS_TYPE_VOLUME) {
2983 3023                  /*
2984 3024                   * If we are creating a volume, the size and block size must
2985 3025                   * satisfy a few restraints.  First, the blocksize must be a
2986 3026                   * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
2987 3027                   * volsize must be a multiple of the block size, and cannot be
2988 3028                   * zero.
2989 3029                   */
2990 3030                  if (props == NULL || nvlist_lookup_uint64(props,
2991 3031                      zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2992 3032                          nvlist_free(props);
2993 3033                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2994 3034                              "missing volume size"));
2995 3035                          return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2996 3036                  }
2997 3037  
2998 3038                  if ((ret = nvlist_lookup_uint64(props,
2999 3039                      zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3000 3040                      &blocksize)) != 0) {
3001 3041                          if (ret == ENOENT) {
3002 3042                                  blocksize = zfs_prop_default_numeric(
3003 3043                                      ZFS_PROP_VOLBLOCKSIZE);
3004 3044                          } else {
3005 3045                                  nvlist_free(props);
3006 3046                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3007 3047                                      "missing volume block size"));
3008 3048                                  return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3009 3049                          }
3010 3050                  }
3011 3051  
3012 3052                  if (size == 0) {
3013 3053                          nvlist_free(props);
3014 3054                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3015 3055                              "volume size cannot be zero"));
3016 3056                          return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3017 3057                  }
3018 3058  
3019 3059                  if (size % blocksize != 0) {
3020 3060                          nvlist_free(props);
3021 3061                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3022 3062                              "volume size must be a multiple of volume block "
3023 3063                              "size"));
3024 3064                          return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3025 3065                  }
3026 3066          }
3027 3067  
3028 3068          /* create the dataset */
3029 3069          ret = lzc_create(path, ost, props);
3030 3070          nvlist_free(props);
3031 3071  
3032 3072          /* check for failure */
3033 3073          if (ret != 0) {
3034 3074                  char parent[ZFS_MAXNAMELEN];
3035 3075                  (void) parent_name(path, parent, sizeof (parent));
3036 3076  
3037 3077                  switch (errno) {
3038 3078                  case ENOENT:
3039 3079                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3040 3080                              "no such parent '%s'"), parent);
3041 3081                          return (zfs_error(hdl, EZFS_NOENT, errbuf));
3042 3082  
3043 3083                  case EINVAL:
3044 3084                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3045 3085                              "parent '%s' is not a filesystem"), parent);
3046 3086                          return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3047 3087  
3048 3088                  case EDOM:
3049 3089                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3050 3090                              "volume block size must be power of 2 from "
3051 3091                              "%u to %uk"),
3052 3092                              (uint_t)SPA_MINBLOCKSIZE,
3053 3093                              (uint_t)SPA_MAXBLOCKSIZE >> 10);
3054 3094  
3055 3095                          return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3056 3096  
3057 3097                  case ENOTSUP:
3058 3098                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3059 3099                              "pool must be upgraded to set this "
3060 3100                              "property or value"));
3061 3101                          return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3062 3102  #ifdef _ILP32
3063 3103                  case EOVERFLOW:
3064 3104                          /*
3065 3105                           * This platform can't address a volume this big.
3066 3106                           */
3067 3107                          if (type == ZFS_TYPE_VOLUME)
3068 3108                                  return (zfs_error(hdl, EZFS_VOLTOOBIG,
3069 3109                                      errbuf));
3070 3110  #endif
3071 3111                          /* FALLTHROUGH */
3072 3112                  default:
3073 3113                          return (zfs_standard_error(hdl, errno, errbuf));
3074 3114                  }
3075 3115          }
3076 3116  
3077 3117          return (0);
3078 3118  }
3079 3119  
3080 3120  /*
3081 3121   * Destroys the given dataset.  The caller must make sure that the filesystem
3082 3122   * isn't mounted, and that there are no active dependents. If the file system
3083 3123   * does not exist this function does nothing.
3084 3124   */
3085 3125  int
3086 3126  zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3087 3127  {
3088 3128          zfs_cmd_t zc = { 0 };
3089 3129  
3090 3130          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3091 3131  
3092 3132          if (ZFS_IS_VOLUME(zhp)) {
3093 3133                  zc.zc_objset_type = DMU_OST_ZVOL;
3094 3134          } else {
3095 3135                  zc.zc_objset_type = DMU_OST_ZFS;
3096 3136          }
3097 3137  
3098 3138          zc.zc_defer_destroy = defer;
3099 3139          if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3100 3140              errno != ENOENT) {
3101 3141                  return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3102 3142                      dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3103 3143                      zhp->zfs_name));
3104 3144          }
3105 3145  
3106 3146          remove_mountpoint(zhp);
3107 3147  
3108 3148          return (0);
3109 3149  }
3110 3150  
3111 3151  struct destroydata {
3112 3152          nvlist_t *nvl;
3113 3153          const char *snapname;
3114 3154  };
3115 3155  
3116 3156  static int
3117 3157  zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3118 3158  {
3119 3159          struct destroydata *dd = arg;
3120 3160          zfs_handle_t *szhp;
3121 3161          char name[ZFS_MAXNAMELEN];
3122 3162          int rv = 0;
3123 3163  
3124 3164          (void) snprintf(name, sizeof (name),
3125 3165              "%s@%s", zhp->zfs_name, dd->snapname);
3126 3166  
3127 3167          szhp = make_dataset_handle(zhp->zfs_hdl, name);
3128 3168          if (szhp) {
3129 3169                  verify(nvlist_add_boolean(dd->nvl, name) == 0);
3130 3170                  zfs_close(szhp);
3131 3171          }
3132 3172  
3133 3173          rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3134 3174          zfs_close(zhp);
3135 3175          return (rv);
3136 3176  }
3137 3177  
3138 3178  /*
3139 3179   * Destroys all snapshots with the given name in zhp & descendants.
3140 3180   */
3141 3181  int
3142 3182  zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3143 3183  {
3144 3184          int ret;
3145 3185          struct destroydata dd = { 0 };
3146 3186  
3147 3187          dd.snapname = snapname;
3148 3188          verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3149 3189          (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3150 3190  
3151 3191          if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3152 3192                  ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3153 3193                      dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3154 3194                      zhp->zfs_name, snapname);
3155 3195          } else {
3156 3196                  ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3157 3197          }
3158 3198          nvlist_free(dd.nvl);
3159 3199          return (ret);
3160 3200  }
3161 3201  
3162 3202  /*
3163 3203   * Destroys all the snapshots named in the nvlist.  They must be underneath
3164 3204   * the zhp (either snapshots of it, or snapshots of its descendants).
3165 3205   */
3166 3206  int
3167 3207  zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3168 3208  {
3169 3209          int ret;
3170 3210          nvlist_t *errlist;
3171 3211  
3172 3212          ret = lzc_destroy_snaps(snaps, defer, &errlist);
3173 3213  
3174 3214          if (ret != 0) {
3175 3215                  for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3176 3216                      pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3177 3217                          char errbuf[1024];
3178 3218                          (void) snprintf(errbuf, sizeof (errbuf),
3179 3219                              dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3180 3220                              nvpair_name(pair));
3181 3221  
3182 3222                          switch (fnvpair_value_int32(pair)) {
3183 3223                          case EEXIST:
3184 3224                                  zfs_error_aux(zhp->zfs_hdl,
3185 3225                                      dgettext(TEXT_DOMAIN,
3186 3226                                      "snapshot is cloned"));
3187 3227                                  ret = zfs_error(zhp->zfs_hdl, EZFS_EXISTS,
3188 3228                                      errbuf);
3189 3229                                  break;
3190 3230                          default:
3191 3231                                  ret = zfs_standard_error(zhp->zfs_hdl, errno,
3192 3232                                      errbuf);
3193 3233                                  break;
3194 3234                          }
3195 3235                  }
3196 3236          }
3197 3237  
3198 3238          return (ret);
3199 3239  }
3200 3240  
3201 3241  /*
3202 3242   * Clones the given dataset.  The target must be of the same type as the source.
3203 3243   */
3204 3244  int
3205 3245  zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3206 3246  {
3207 3247          char parent[ZFS_MAXNAMELEN];
3208 3248          int ret;
3209 3249          char errbuf[1024];
3210 3250          libzfs_handle_t *hdl = zhp->zfs_hdl;
3211 3251          uint64_t zoned;
3212 3252  
3213 3253          assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3214 3254  
3215 3255          (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3216 3256              "cannot create '%s'"), target);
3217 3257  
3218 3258          /* validate the target/clone name */
3219 3259          if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3220 3260                  return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3221 3261  
3222 3262          /* validate parents exist */
3223 3263          if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3224 3264                  return (-1);
3225 3265  
3226 3266          (void) parent_name(target, parent, sizeof (parent));
3227 3267  
3228 3268          /* do the clone */
3229 3269  
3230 3270          if (props) {
3231 3271                  zfs_type_t type;
3232 3272                  if (ZFS_IS_VOLUME(zhp)) {
3233 3273                          type = ZFS_TYPE_VOLUME;
3234 3274                  } else {
3235 3275                          type = ZFS_TYPE_FILESYSTEM;
3236 3276                  }
3237 3277                  if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3238 3278                      zhp, errbuf)) == NULL)
3239 3279                          return (-1);
3240 3280          }
3241 3281  
3242 3282          ret = lzc_clone(target, zhp->zfs_name, props);
3243 3283          nvlist_free(props);
3244 3284  
3245 3285          if (ret != 0) {
3246 3286                  switch (errno) {
3247 3287  
3248 3288                  case ENOENT:
3249 3289                          /*
3250 3290                           * The parent doesn't exist.  We should have caught this
3251 3291                           * above, but there may a race condition that has since
3252 3292                           * destroyed the parent.
3253 3293                           *
3254 3294                           * At this point, we don't know whether it's the source
3255 3295                           * that doesn't exist anymore, or whether the target
3256 3296                           * dataset doesn't exist.
3257 3297                           */
3258 3298                          zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3259 3299                              "no such parent '%s'"), parent);
3260 3300                          return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3261 3301  
3262 3302                  case EXDEV:
3263 3303                          zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3264 3304                              "source and target pools differ"));
3265 3305                          return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3266 3306                              errbuf));
3267 3307  
3268 3308                  default:
3269 3309                          return (zfs_standard_error(zhp->zfs_hdl, errno,
3270 3310                              errbuf));
3271 3311                  }
3272 3312          }
3273 3313  
3274 3314          return (ret);
3275 3315  }
3276 3316  
3277 3317  /*
3278 3318   * Promotes the given clone fs to be the clone parent.
3279 3319   */
3280 3320  int
3281 3321  zfs_promote(zfs_handle_t *zhp)
3282 3322  {
3283 3323          libzfs_handle_t *hdl = zhp->zfs_hdl;
3284 3324          zfs_cmd_t zc = { 0 };
3285 3325          char parent[MAXPATHLEN];
3286 3326          int ret;
3287 3327          char errbuf[1024];
3288 3328  
3289 3329          (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3290 3330              "cannot promote '%s'"), zhp->zfs_name);
3291 3331  
3292 3332          if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3293 3333                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3294 3334                      "snapshots can not be promoted"));
3295 3335                  return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3296 3336          }
3297 3337  
3298 3338          (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3299 3339          if (parent[0] == '\0') {
3300 3340                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3301 3341                      "not a cloned filesystem"));
3302 3342                  return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3303 3343          }
3304 3344  
3305 3345          (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3306 3346              sizeof (zc.zc_value));
3307 3347          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3308 3348          ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3309 3349  
3310 3350          if (ret != 0) {
3311 3351                  int save_errno = errno;
3312 3352  
3313 3353                  switch (save_errno) {
3314 3354                  case EEXIST:
3315 3355                          /* There is a conflicting snapshot name. */
3316 3356                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3317 3357                              "conflicting snapshot '%s' from parent '%s'"),
3318 3358                              zc.zc_string, parent);
3319 3359                          return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3320 3360  
3321 3361                  default:
3322 3362                          return (zfs_standard_error(hdl, save_errno, errbuf));
3323 3363                  }
3324 3364          }
3325 3365          return (ret);
3326 3366  }
3327 3367  
3328 3368  typedef struct snapdata {
3329 3369          nvlist_t *sd_nvl;
3330 3370          const char *sd_snapname;
3331 3371  } snapdata_t;
3332 3372  
3333 3373  static int
3334 3374  zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3335 3375  {
3336 3376          snapdata_t *sd = arg;
3337 3377          char name[ZFS_MAXNAMELEN];
3338 3378          int rv = 0;
3339 3379  
3340 3380          (void) snprintf(name, sizeof (name),
3341 3381              "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3342 3382  
3343 3383          fnvlist_add_boolean(sd->sd_nvl, name);
3344 3384  
3345 3385          rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3346 3386          zfs_close(zhp);
3347 3387          return (rv);
3348 3388  }
3349 3389  
3350 3390  /*
3351 3391   * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
3352 3392   * created.
3353 3393   */
3354 3394  int
3355 3395  zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3356 3396  {
3357 3397          int ret;
3358 3398          char errbuf[1024];
3359 3399          nvpair_t *elem;
3360 3400          nvlist_t *errors;
3361 3401  
3362 3402          (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3363 3403              "cannot create snapshots "));
3364 3404  
3365 3405          elem = NULL;
3366 3406          while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3367 3407                  const char *snapname = nvpair_name(elem);
3368 3408  
3369 3409                  /* validate the target name */
3370 3410                  if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3371 3411                      B_TRUE)) {
3372 3412                          (void) snprintf(errbuf, sizeof (errbuf),
3373 3413                              dgettext(TEXT_DOMAIN,
3374 3414                              "cannot create snapshot '%s'"), snapname);
3375 3415                          return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3376 3416                  }
3377 3417          }
3378 3418  
3379 3419          if (props != NULL &&
3380 3420              (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3381 3421              props, B_FALSE, NULL, errbuf)) == NULL) {
3382 3422                  return (-1);
3383 3423          }
3384 3424  
3385 3425          ret = lzc_snapshot(snaps, props, &errors);
3386 3426  
3387 3427          if (ret != 0) {
3388 3428                  boolean_t printed = B_FALSE;
3389 3429                  for (elem = nvlist_next_nvpair(errors, NULL);
3390 3430                      elem != NULL;
3391 3431                      elem = nvlist_next_nvpair(errors, elem)) {
3392 3432                          (void) snprintf(errbuf, sizeof (errbuf),
3393 3433                              dgettext(TEXT_DOMAIN,
3394 3434                              "cannot create snapshot '%s'"), nvpair_name(elem));
3395 3435                          (void) zfs_standard_error(hdl,
3396 3436                              fnvpair_value_int32(elem), errbuf);
3397 3437                          printed = B_TRUE;
3398 3438                  }
3399 3439                  if (!printed) {
3400 3440                          switch (ret) {
3401 3441                          case EXDEV:
3402 3442                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3403 3443                                      "multiple snapshots of same "
3404 3444                                      "fs not allowed"));
3405 3445                                  (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3406 3446  
3407 3447                                  break;
3408 3448                          default:
3409 3449                                  (void) zfs_standard_error(hdl, ret, errbuf);
3410 3450                          }
3411 3451                  }
3412 3452          }
3413 3453  
3414 3454          nvlist_free(props);
3415 3455          nvlist_free(errors);
3416 3456          return (ret);
3417 3457  }
3418 3458  
3419 3459  int
3420 3460  zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3421 3461      nvlist_t *props)
3422 3462  {
3423 3463          int ret;
3424 3464          snapdata_t sd = { 0 };
3425 3465          char fsname[ZFS_MAXNAMELEN];
3426 3466          char *cp;
3427 3467          zfs_handle_t *zhp;
3428 3468          char errbuf[1024];
3429 3469  
3430 3470          (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3431 3471              "cannot snapshot %s"), path);
3432 3472  
3433 3473          if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3434 3474                  return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3435 3475  
3436 3476          (void) strlcpy(fsname, path, sizeof (fsname));
3437 3477          cp = strchr(fsname, '@');
3438 3478          *cp = '\0';
3439 3479          sd.sd_snapname = cp + 1;
3440 3480  
3441 3481          if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3442 3482              ZFS_TYPE_VOLUME)) == NULL) {
3443 3483                  return (-1);
3444 3484          }
3445 3485  
3446 3486          verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3447 3487          if (recursive) {
3448 3488                  (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3449 3489          } else {
3450 3490                  fnvlist_add_boolean(sd.sd_nvl, path);
3451 3491          }
3452 3492  
3453 3493          ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3454 3494          nvlist_free(sd.sd_nvl);
3455 3495          zfs_close(zhp);
3456 3496          return (ret);
3457 3497  }
3458 3498  
3459 3499  /*
3460 3500   * Destroy any more recent snapshots.  We invoke this callback on any dependents
3461 3501   * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
3462 3502   * is a dependent and we should just destroy it without checking the transaction
3463 3503   * group.
3464 3504   */
3465 3505  typedef struct rollback_data {
3466 3506          const char      *cb_target;             /* the snapshot */
3467 3507          uint64_t        cb_create;              /* creation time reference */
3468 3508          boolean_t       cb_error;
3469 3509          boolean_t       cb_dependent;
3470 3510          boolean_t       cb_force;
3471 3511  } rollback_data_t;
3472 3512  
3473 3513  static int
3474 3514  rollback_destroy(zfs_handle_t *zhp, void *data)
3475 3515  {
3476 3516          rollback_data_t *cbp = data;
3477 3517  
3478 3518          if (!cbp->cb_dependent) {
3479 3519                  if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3480 3520                      zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3481 3521                      zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3482 3522                      cbp->cb_create) {
3483 3523  
3484 3524                          cbp->cb_dependent = B_TRUE;
3485 3525                          cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3486 3526                              rollback_destroy, cbp);
3487 3527                          cbp->cb_dependent = B_FALSE;
3488 3528  
3489 3529                          cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3490 3530                  }
3491 3531          } else {
3492 3532                  /* We must destroy this clone; first unmount it */
3493 3533                  prop_changelist_t *clp;
3494 3534  
3495 3535                  clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3496 3536                      cbp->cb_force ? MS_FORCE: 0);
3497 3537                  if (clp == NULL || changelist_prefix(clp) != 0) {
3498 3538                          cbp->cb_error = B_TRUE;
3499 3539                          zfs_close(zhp);
3500 3540                          return (0);
3501 3541                  }
3502 3542                  if (zfs_destroy(zhp, B_FALSE) != 0)
3503 3543                          cbp->cb_error = B_TRUE;
3504 3544                  else
3505 3545                          changelist_remove(clp, zhp->zfs_name);
3506 3546                  (void) changelist_postfix(clp);
3507 3547                  changelist_free(clp);
3508 3548          }
3509 3549  
3510 3550          zfs_close(zhp);
3511 3551          return (0);
3512 3552  }
3513 3553  
3514 3554  /*
3515 3555   * Given a dataset, rollback to a specific snapshot, discarding any
3516 3556   * data changes since then and making it the active dataset.
3517 3557   *
3518 3558   * Any snapshots more recent than the target are destroyed, along with
3519 3559   * their dependents.
3520 3560   */
3521 3561  int
3522 3562  zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3523 3563  {
3524 3564          rollback_data_t cb = { 0 };
3525 3565          int err;
3526 3566          zfs_cmd_t zc = { 0 };
3527 3567          boolean_t restore_resv = 0;
3528 3568          uint64_t old_volsize, new_volsize;
3529 3569          zfs_prop_t resv_prop;
3530 3570  
3531 3571          assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3532 3572              zhp->zfs_type == ZFS_TYPE_VOLUME);
3533 3573  
3534 3574          /*
3535 3575           * Destroy all recent snapshots and their dependents.
3536 3576           */
3537 3577          cb.cb_force = force;
3538 3578          cb.cb_target = snap->zfs_name;
3539 3579          cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3540 3580          (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3541 3581  
3542 3582          if (cb.cb_error)
3543 3583                  return (-1);
3544 3584  
3545 3585          /*
3546 3586           * Now that we have verified that the snapshot is the latest,
3547 3587           * rollback to the given snapshot.
3548 3588           */
3549 3589  
3550 3590          if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3551 3591                  if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3552 3592                          return (-1);
3553 3593                  old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3554 3594                  restore_resv =
3555 3595                      (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3556 3596          }
3557 3597  
3558 3598          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3559 3599  
3560 3600          if (ZFS_IS_VOLUME(zhp))
3561 3601                  zc.zc_objset_type = DMU_OST_ZVOL;
3562 3602          else
3563 3603                  zc.zc_objset_type = DMU_OST_ZFS;
3564 3604  
3565 3605          /*
3566 3606           * We rely on zfs_iter_children() to verify that there are no
3567 3607           * newer snapshots for the given dataset.  Therefore, we can
3568 3608           * simply pass the name on to the ioctl() call.  There is still
3569 3609           * an unlikely race condition where the user has taken a
3570 3610           * snapshot since we verified that this was the most recent.
3571 3611           *
3572 3612           */
3573 3613          if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3574 3614                  (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3575 3615                      dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3576 3616                      zhp->zfs_name);
3577 3617                  return (err);
3578 3618          }
3579 3619  
3580 3620          /*
3581 3621           * For volumes, if the pre-rollback volsize matched the pre-
3582 3622           * rollback reservation and the volsize has changed then set
3583 3623           * the reservation property to the post-rollback volsize.
3584 3624           * Make a new handle since the rollback closed the dataset.
3585 3625           */
3586 3626          if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3587 3627              (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3588 3628                  if (restore_resv) {
3589 3629                          new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3590 3630                          if (old_volsize != new_volsize)
3591 3631                                  err = zfs_prop_set_int(zhp, resv_prop,
3592 3632                                      new_volsize);
3593 3633                  }
3594 3634                  zfs_close(zhp);
3595 3635          }
3596 3636          return (err);
3597 3637  }
3598 3638  
3599 3639  /*
3600 3640   * Renames the given dataset.
3601 3641   */
3602 3642  int
3603 3643  zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3604 3644      boolean_t force_unmount)
3605 3645  {
3606 3646          int ret;
3607 3647          zfs_cmd_t zc = { 0 };
3608 3648          char *delim;
3609 3649          prop_changelist_t *cl = NULL;
3610 3650          zfs_handle_t *zhrp = NULL;
3611 3651          char *parentname = NULL;
3612 3652          char parent[ZFS_MAXNAMELEN];
3613 3653          libzfs_handle_t *hdl = zhp->zfs_hdl;
3614 3654          char errbuf[1024];
3615 3655  
3616 3656          /* if we have the same exact name, just return success */
3617 3657          if (strcmp(zhp->zfs_name, target) == 0)
3618 3658                  return (0);
3619 3659  
3620 3660          (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3621 3661              "cannot rename to '%s'"), target);
3622 3662  
3623 3663          /*
3624 3664           * Make sure the target name is valid
3625 3665           */
3626 3666          if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3627 3667                  if ((strchr(target, '@') == NULL) ||
3628 3668                      *target == '@') {
3629 3669                          /*
3630 3670                           * Snapshot target name is abbreviated,
3631 3671                           * reconstruct full dataset name
3632 3672                           */
3633 3673                          (void) strlcpy(parent, zhp->zfs_name,
3634 3674                              sizeof (parent));
3635 3675                          delim = strchr(parent, '@');
3636 3676                          if (strchr(target, '@') == NULL)
3637 3677                                  *(++delim) = '\0';
3638 3678                          else
3639 3679                                  *delim = '\0';
3640 3680                          (void) strlcat(parent, target, sizeof (parent));
3641 3681                          target = parent;
3642 3682                  } else {
3643 3683                          /*
3644 3684                           * Make sure we're renaming within the same dataset.
3645 3685                           */
3646 3686                          delim = strchr(target, '@');
3647 3687                          if (strncmp(zhp->zfs_name, target, delim - target)
3648 3688                              != 0 || zhp->zfs_name[delim - target] != '@') {
3649 3689                                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3650 3690                                      "snapshots must be part of same "
3651 3691                                      "dataset"));
3652 3692                                  return (zfs_error(hdl, EZFS_CROSSTARGET,
3653 3693                                      errbuf));
3654 3694                          }
3655 3695                  }
3656 3696                  if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3657 3697                          return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3658 3698          } else {
3659 3699                  if (recursive) {
3660 3700                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3661 3701                              "recursive rename must be a snapshot"));
3662 3702                          return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3663 3703                  }
3664 3704  
3665 3705                  if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3666 3706                          return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3667 3707  
3668 3708                  /* validate parents */
3669 3709                  if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3670 3710                          return (-1);
3671 3711  
3672 3712                  /* make sure we're in the same pool */
3673 3713                  verify((delim = strchr(target, '/')) != NULL);
3674 3714                  if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3675 3715                      zhp->zfs_name[delim - target] != '/') {
3676 3716                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3677 3717                              "datasets must be within same pool"));
3678 3718                          return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3679 3719                  }
3680 3720  
3681 3721                  /* new name cannot be a child of the current dataset name */
3682 3722                  if (is_descendant(zhp->zfs_name, target)) {
3683 3723                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3684 3724                              "New dataset name cannot be a descendant of "
3685 3725                              "current dataset name"));
3686 3726                          return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3687 3727                  }
3688 3728          }
3689 3729  
3690 3730          (void) snprintf(errbuf, sizeof (errbuf),
3691 3731              dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3692 3732  
3693 3733          if (getzoneid() == GLOBAL_ZONEID &&
3694 3734              zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3695 3735                  zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3696 3736                      "dataset is used in a non-global zone"));
3697 3737                  return (zfs_error(hdl, EZFS_ZONED, errbuf));
3698 3738          }
3699 3739  
3700 3740          if (recursive) {
3701 3741  
3702 3742                  parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3703 3743                  if (parentname == NULL) {
3704 3744                          ret = -1;
3705 3745                          goto error;
3706 3746                  }
3707 3747                  delim = strchr(parentname, '@');
3708 3748                  *delim = '\0';
3709 3749                  zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3710 3750                  if (zhrp == NULL) {
3711 3751                          ret = -1;
3712 3752                          goto error;
3713 3753                  }
3714 3754  
3715 3755          } else {
3716 3756                  if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3717 3757                      force_unmount ? MS_FORCE : 0)) == NULL)
3718 3758                          return (-1);
3719 3759  
3720 3760                  if (changelist_haszonedchild(cl)) {
3721 3761                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3722 3762                              "child dataset with inherited mountpoint is used "
3723 3763                              "in a non-global zone"));
3724 3764                          (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3725 3765                          goto error;
3726 3766                  }
3727 3767  
3728 3768                  if ((ret = changelist_prefix(cl)) != 0)
3729 3769                          goto error;
3730 3770          }
3731 3771  
3732 3772          if (ZFS_IS_VOLUME(zhp))
3733 3773                  zc.zc_objset_type = DMU_OST_ZVOL;
3734 3774          else
3735 3775                  zc.zc_objset_type = DMU_OST_ZFS;
3736 3776  
3737 3777          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3738 3778          (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3739 3779  
3740 3780          zc.zc_cookie = recursive;
3741 3781  
3742 3782          if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3743 3783                  /*
3744 3784                   * if it was recursive, the one that actually failed will
3745 3785                   * be in zc.zc_name
3746 3786                   */
3747 3787                  (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3748 3788                      "cannot rename '%s'"), zc.zc_name);
3749 3789  
3750 3790                  if (recursive && errno == EEXIST) {
3751 3791                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3752 3792                              "a child dataset already has a snapshot "
3753 3793                              "with the new name"));
3754 3794                          (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3755 3795                  } else {
3756 3796                          (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3757 3797                  }
3758 3798  
3759 3799                  /*
3760 3800                   * On failure, we still want to remount any filesystems that
3761 3801                   * were previously mounted, so we don't alter the system state.
3762 3802                   */
3763 3803                  if (!recursive)
3764 3804                          (void) changelist_postfix(cl);
3765 3805          } else {
3766 3806                  if (!recursive) {
3767 3807                          changelist_rename(cl, zfs_get_name(zhp), target);
3768 3808                          ret = changelist_postfix(cl);
3769 3809                  }
3770 3810          }
3771 3811  
3772 3812  error:
3773 3813          if (parentname) {
3774 3814                  free(parentname);
3775 3815          }
3776 3816          if (zhrp) {
3777 3817                  zfs_close(zhrp);
3778 3818          }
3779 3819          if (cl) {
3780 3820                  changelist_free(cl);
3781 3821          }
3782 3822          return (ret);
3783 3823  }
3784 3824  
3785 3825  nvlist_t *
3786 3826  zfs_get_user_props(zfs_handle_t *zhp)
3787 3827  {
3788 3828          return (zhp->zfs_user_props);
3789 3829  }
3790 3830  
3791 3831  nvlist_t *
3792 3832  zfs_get_recvd_props(zfs_handle_t *zhp)
3793 3833  {
3794 3834          if (zhp->zfs_recvd_props == NULL)
3795 3835                  if (get_recvd_props_ioctl(zhp) != 0)
3796 3836                          return (NULL);
3797 3837          return (zhp->zfs_recvd_props);
3798 3838  }
3799 3839  
3800 3840  /*
3801 3841   * This function is used by 'zfs list' to determine the exact set of columns to
3802 3842   * display, and their maximum widths.  This does two main things:
3803 3843   *
3804 3844   *      - If this is a list of all properties, then expand the list to include
3805 3845   *        all native properties, and set a flag so that for each dataset we look
3806 3846   *        for new unique user properties and add them to the list.
3807 3847   *
3808 3848   *      - For non fixed-width properties, keep track of the maximum width seen
3809 3849   *        so that we can size the column appropriately. If the user has
3810 3850   *        requested received property values, we also need to compute the width
3811 3851   *        of the RECEIVED column.
3812 3852   */
3813 3853  int
3814 3854  zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3815 3855  {
3816 3856          libzfs_handle_t *hdl = zhp->zfs_hdl;
3817 3857          zprop_list_t *entry;
3818 3858          zprop_list_t **last, **start;
3819 3859          nvlist_t *userprops, *propval;
3820 3860          nvpair_t *elem;
3821 3861          char *strval;
3822 3862          char buf[ZFS_MAXPROPLEN];
3823 3863  
3824 3864          if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3825 3865                  return (-1);
3826 3866  
3827 3867          userprops = zfs_get_user_props(zhp);
3828 3868  
3829 3869          entry = *plp;
3830 3870          if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3831 3871                  /*
3832 3872                   * Go through and add any user properties as necessary.  We
3833 3873                   * start by incrementing our list pointer to the first
3834 3874                   * non-native property.
3835 3875                   */
3836 3876                  start = plp;
3837 3877                  while (*start != NULL) {
3838 3878                          if ((*start)->pl_prop == ZPROP_INVAL)
3839 3879                                  break;
3840 3880                          start = &(*start)->pl_next;
3841 3881                  }
3842 3882  
3843 3883                  elem = NULL;
3844 3884                  while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3845 3885                          /*
3846 3886                           * See if we've already found this property in our list.
3847 3887                           */
3848 3888                          for (last = start; *last != NULL;
3849 3889                              last = &(*last)->pl_next) {
3850 3890                                  if (strcmp((*last)->pl_user_prop,
3851 3891                                      nvpair_name(elem)) == 0)
3852 3892                                          break;
3853 3893                          }
3854 3894  
3855 3895                          if (*last == NULL) {
3856 3896                                  if ((entry = zfs_alloc(hdl,
3857 3897                                      sizeof (zprop_list_t))) == NULL ||
3858 3898                                      ((entry->pl_user_prop = zfs_strdup(hdl,
3859 3899                                      nvpair_name(elem)))) == NULL) {
3860 3900                                          free(entry);
3861 3901                                          return (-1);
3862 3902                                  }
3863 3903  
3864 3904                                  entry->pl_prop = ZPROP_INVAL;
3865 3905                                  entry->pl_width = strlen(nvpair_name(elem));
3866 3906                                  entry->pl_all = B_TRUE;
3867 3907                                  *last = entry;
3868 3908                          }
3869 3909                  }
3870 3910          }
3871 3911  
3872 3912          /*
3873 3913           * Now go through and check the width of any non-fixed columns
3874 3914           */
3875 3915          for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3876 3916                  if (entry->pl_fixed)
3877 3917                          continue;
3878 3918  
3879 3919                  if (entry->pl_prop != ZPROP_INVAL) {
3880 3920                          if (zfs_prop_get(zhp, entry->pl_prop,
3881 3921                              buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3882 3922                                  if (strlen(buf) > entry->pl_width)
3883 3923                                          entry->pl_width = strlen(buf);
3884 3924                          }
3885 3925                          if (received && zfs_prop_get_recvd(zhp,
3886 3926                              zfs_prop_to_name(entry->pl_prop),
3887 3927                              buf, sizeof (buf), B_FALSE) == 0)
3888 3928                                  if (strlen(buf) > entry->pl_recvd_width)
3889 3929                                          entry->pl_recvd_width = strlen(buf);
3890 3930                  } else {
3891 3931                          if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3892 3932                              &propval) == 0) {
3893 3933                                  verify(nvlist_lookup_string(propval,
3894 3934                                      ZPROP_VALUE, &strval) == 0);
3895 3935                                  if (strlen(strval) > entry->pl_width)
3896 3936                                          entry->pl_width = strlen(strval);
3897 3937                          }
3898 3938                          if (received && zfs_prop_get_recvd(zhp,
3899 3939                              entry->pl_user_prop,
3900 3940                              buf, sizeof (buf), B_FALSE) == 0)
3901 3941                                  if (strlen(buf) > entry->pl_recvd_width)
3902 3942                                          entry->pl_recvd_width = strlen(buf);
3903 3943                  }
3904 3944          }
3905 3945  
3906 3946          return (0);
3907 3947  }
3908 3948  
3909 3949  int
3910 3950  zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3911 3951      char *resource, void *export, void *sharetab,
3912 3952      int sharemax, zfs_share_op_t operation)
3913 3953  {
3914 3954          zfs_cmd_t zc = { 0 };
3915 3955          int error;
3916 3956  
3917 3957          (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3918 3958          (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3919 3959          if (resource)
3920 3960                  (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3921 3961          zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3922 3962          zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3923 3963          zc.zc_share.z_sharetype = operation;
3924 3964          zc.zc_share.z_sharemax = sharemax;
3925 3965          error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3926 3966          return (error);
3927 3967  }
3928 3968  
3929 3969  void
3930 3970  zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3931 3971  {
3932 3972          nvpair_t *curr;
3933 3973  
3934 3974          /*
3935 3975           * Keep a reference to the props-table against which we prune the
3936 3976           * properties.
3937 3977           */
3938 3978          zhp->zfs_props_table = props;
3939 3979  
3940 3980          curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3941 3981  
3942 3982          while (curr) {
3943 3983                  zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3944 3984                  nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3945 3985  
3946 3986                  /*
3947 3987                   * User properties will result in ZPROP_INVAL, and since we
3948 3988                   * only know how to prune standard ZFS properties, we always
3949 3989                   * leave these in the list.  This can also happen if we
3950 3990                   * encounter an unknown DSL property (when running older
3951 3991                   * software, for example).
3952 3992                   */
3953 3993                  if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3954 3994                          (void) nvlist_remove(zhp->zfs_props,
3955 3995                              nvpair_name(curr), nvpair_type(curr));
3956 3996                  curr = next;
3957 3997          }
3958 3998  }
3959 3999  
3960 4000  static int
3961 4001  zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3962 4002      zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3963 4003  {
3964 4004          zfs_cmd_t zc = { 0 };
3965 4005          nvlist_t *nvlist = NULL;
3966 4006          int error;
3967 4007  
3968 4008          (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3969 4009          (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3970 4010          zc.zc_cookie = (uint64_t)cmd;
3971 4011  
3972 4012          if (cmd == ZFS_SMB_ACL_RENAME) {
3973 4013                  if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3974 4014                          (void) no_memory(hdl);
3975 4015                          return (NULL);
3976 4016                  }
3977 4017          }
3978 4018  
3979 4019          switch (cmd) {
3980 4020          case ZFS_SMB_ACL_ADD:
3981 4021          case ZFS_SMB_ACL_REMOVE:
3982 4022                  (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3983 4023                  break;
3984 4024          case ZFS_SMB_ACL_RENAME:
3985 4025                  if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3986 4026                      resource1) != 0) {
3987 4027                                  (void) no_memory(hdl);
3988 4028                                  return (-1);
3989 4029                  }
3990 4030                  if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3991 4031                      resource2) != 0) {
3992 4032                                  (void) no_memory(hdl);
3993 4033                                  return (-1);
3994 4034                  }
3995 4035                  if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3996 4036                          nvlist_free(nvlist);
3997 4037                          return (-1);
3998 4038                  }
3999 4039                  break;
4000 4040          case ZFS_SMB_ACL_PURGE:
4001 4041                  break;
4002 4042          default:
4003 4043                  return (-1);
4004 4044          }
4005 4045          error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4006 4046          if (nvlist)
4007 4047                  nvlist_free(nvlist);
4008 4048          return (error);
4009 4049  }
4010 4050  
4011 4051  int
4012 4052  zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4013 4053      char *path, char *resource)
4014 4054  {
4015 4055          return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4016 4056              resource, NULL));
4017 4057  }
4018 4058  
4019 4059  int
4020 4060  zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4021 4061      char *path, char *resource)
4022 4062  {
4023 4063          return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4024 4064              resource, NULL));
4025 4065  }
4026 4066  
4027 4067  int
4028 4068  zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4029 4069  {
4030 4070          return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4031 4071              NULL, NULL));
4032 4072  }
4033 4073  
4034 4074  int
4035 4075  zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4036 4076      char *oldname, char *newname)
4037 4077  {
4038 4078          return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4039 4079              oldname, newname));
4040 4080  }
4041 4081  
4042 4082  int
4043 4083  zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4044 4084      zfs_userspace_cb_t func, void *arg)
4045 4085  {
4046 4086          zfs_cmd_t zc = { 0 };
4047 4087          zfs_useracct_t buf[100];
4048 4088          libzfs_handle_t *hdl = zhp->zfs_hdl;
4049 4089          int ret;
4050 4090  
4051 4091          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4052 4092  
4053 4093          zc.zc_objset_type = type;
4054 4094          zc.zc_nvlist_dst = (uintptr_t)buf;
4055 4095  
4056 4096          for (;;) {
4057 4097                  zfs_useracct_t *zua = buf;
4058 4098  
4059 4099                  zc.zc_nvlist_dst_size = sizeof (buf);
4060 4100                  if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4061 4101                          char errbuf[ZFS_MAXNAMELEN + 32];
4062 4102  
4063 4103                          (void) snprintf(errbuf, sizeof (errbuf),
4064 4104                              dgettext(TEXT_DOMAIN,
4065 4105                              "cannot get used/quota for %s"), zc.zc_name);
4066 4106                          return (zfs_standard_error_fmt(hdl, errno, errbuf));
4067 4107                  }
4068 4108                  if (zc.zc_nvlist_dst_size == 0)
4069 4109                          break;
4070 4110  
4071 4111                  while (zc.zc_nvlist_dst_size > 0) {
4072 4112                          if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4073 4113                              zua->zu_space)) != 0)
4074 4114                                  return (ret);
4075 4115                          zua++;
4076 4116                          zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4077 4117                  }
4078 4118          }
4079 4119  
4080 4120          return (0);
4081 4121  }
4082 4122  
4083 4123  int
4084 4124  zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4085 4125      boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4086 4126      int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4087 4127  {
4088 4128          zfs_cmd_t zc = { 0 };
4089 4129          libzfs_handle_t *hdl = zhp->zfs_hdl;
4090 4130  
4091 4131          ASSERT(!recursive || dsobj == 0);
4092 4132  
4093 4133          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4094 4134          (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4095 4135          if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4096 4136              >= sizeof (zc.zc_string))
4097 4137                  return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4098 4138          zc.zc_cookie = recursive;
4099 4139          zc.zc_temphold = temphold;
4100 4140          zc.zc_cleanup_fd = cleanup_fd;
4101 4141          zc.zc_sendobj = dsobj;
4102 4142          zc.zc_createtxg = createtxg;
4103 4143  
4104 4144          if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4105 4145                  char errbuf[ZFS_MAXNAMELEN+32];
4106 4146  
4107 4147                  /*
4108 4148                   * if it was recursive, the one that actually failed will be in
4109 4149                   * zc.zc_name.
4110 4150                   */
4111 4151                  (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4112 4152                      "cannot hold '%s@%s'"), zc.zc_name, snapname);
4113 4153                  switch (errno) {
4114 4154                  case E2BIG:
4115 4155                          /*
4116 4156                           * Temporary tags wind up having the ds object id
4117 4157                           * prepended. So even if we passed the length check
4118 4158                           * above, it's still possible for the tag to wind
4119 4159                           * up being slightly too long.
4120 4160                           */
4121 4161                          return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4122 4162                  case ENOTSUP:
4123 4163                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4124 4164                              "pool must be upgraded"));
4125 4165                          return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4126 4166                  case EINVAL:
4127 4167                          return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4128 4168                  case EEXIST:
4129 4169                          return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4130 4170                  case ENOENT:
4131 4171                          if (enoent_ok)
4132 4172                                  return (ENOENT);
4133 4173                          /* FALLTHROUGH */
4134 4174                  default:
4135 4175                          return (zfs_standard_error_fmt(hdl, errno, errbuf));
4136 4176                  }
4137 4177          }
4138 4178  
4139 4179          return (0);
4140 4180  }
4141 4181  
4142 4182  int
4143 4183  zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4144 4184      boolean_t recursive)
4145 4185  {
4146 4186          zfs_cmd_t zc = { 0 };
4147 4187          libzfs_handle_t *hdl = zhp->zfs_hdl;
4148 4188  
4149 4189          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4150 4190          (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4151 4191          if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4152 4192              >= sizeof (zc.zc_string))
4153 4193                  return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4154 4194          zc.zc_cookie = recursive;
4155 4195  
4156 4196          if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4157 4197                  char errbuf[ZFS_MAXNAMELEN+32];
4158 4198  
4159 4199                  /*
4160 4200                   * if it was recursive, the one that actually failed will be in
4161 4201                   * zc.zc_name.
4162 4202                   */
4163 4203                  (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4164 4204                      "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4165 4205                      snapname);
4166 4206                  switch (errno) {
4167 4207                  case ESRCH:
4168 4208                          return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4169 4209                  case ENOTSUP:
4170 4210                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4171 4211                              "pool must be upgraded"));
4172 4212                          return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4173 4213                  case EINVAL:
4174 4214                          return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4175 4215                  default:
4176 4216                          return (zfs_standard_error_fmt(hdl, errno, errbuf));
4177 4217                  }
4178 4218          }
4179 4219  
4180 4220          return (0);
4181 4221  }
4182 4222  
4183 4223  int
4184 4224  zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4185 4225  {
4186 4226          zfs_cmd_t zc = { 0 };
4187 4227          libzfs_handle_t *hdl = zhp->zfs_hdl;
4188 4228          int nvsz = 2048;
4189 4229          void *nvbuf;
4190 4230          int err = 0;
4191 4231          char errbuf[ZFS_MAXNAMELEN+32];
4192 4232  
4193 4233          assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4194 4234              zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4195 4235  
4196 4236  tryagain:
4197 4237  
4198 4238          nvbuf = malloc(nvsz);
4199 4239          if (nvbuf == NULL) {
4200 4240                  err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4201 4241                  goto out;
4202 4242          }
4203 4243  
4204 4244          zc.zc_nvlist_dst_size = nvsz;
4205 4245          zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4206 4246  
4207 4247          (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4208 4248  
4209 4249          if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4210 4250                  (void) snprintf(errbuf, sizeof (errbuf),
4211 4251                      dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4212 4252                      zc.zc_name);
4213 4253                  switch (errno) {
4214 4254                  case ENOMEM:
4215 4255                          free(nvbuf);
4216 4256                          nvsz = zc.zc_nvlist_dst_size;
4217 4257                          goto tryagain;
4218 4258  
4219 4259                  case ENOTSUP:
4220 4260                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4221 4261                              "pool must be upgraded"));
4222 4262                          err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4223 4263                          break;
4224 4264                  case EINVAL:
4225 4265                          err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4226 4266                          break;
4227 4267                  case ENOENT:
4228 4268                          err = zfs_error(hdl, EZFS_NOENT, errbuf);
4229 4269                          break;
4230 4270                  default:
4231 4271                          err = zfs_standard_error_fmt(hdl, errno, errbuf);
4232 4272                          break;
4233 4273                  }
4234 4274          } else {
4235 4275                  /* success */
4236 4276                  int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4237 4277                  if (rc) {
4238 4278                          (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4239 4279                              TEXT_DOMAIN, "cannot get permissions on '%s'"),
4240 4280                              zc.zc_name);
4241 4281                          err = zfs_standard_error_fmt(hdl, rc, errbuf);
4242 4282                  }
4243 4283          }
4244 4284  
4245 4285          free(nvbuf);
4246 4286  out:
4247 4287          return (err);
4248 4288  }
4249 4289  
4250 4290  int
4251 4291  zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4252 4292  {
4253 4293          zfs_cmd_t zc = { 0 };
4254 4294          libzfs_handle_t *hdl = zhp->zfs_hdl;
4255 4295          char *nvbuf;
4256 4296          char errbuf[ZFS_MAXNAMELEN+32];
4257 4297          size_t nvsz;
4258 4298          int err;
4259 4299  
4260 4300          assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4261 4301              zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4262 4302  
4263 4303          err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4264 4304          assert(err == 0);
4265 4305  
4266 4306          nvbuf = malloc(nvsz);
4267 4307  
4268 4308          err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4269 4309          assert(err == 0);
4270 4310  
4271 4311          zc.zc_nvlist_src_size = nvsz;
4272 4312          zc.zc_nvlist_src = (uintptr_t)nvbuf;
4273 4313          zc.zc_perm_action = un;
4274 4314  
4275 4315          (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4276 4316  
4277 4317          if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4278 4318                  (void) snprintf(errbuf, sizeof (errbuf),
4279 4319                      dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4280 4320                      zc.zc_name);
4281 4321                  switch (errno) {
4282 4322                  case ENOTSUP:
4283 4323                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4284 4324                              "pool must be upgraded"));
4285 4325                          err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4286 4326                          break;
4287 4327                  case EINVAL:
4288 4328                          err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4289 4329                          break;
4290 4330                  case ENOENT:
4291 4331                          err = zfs_error(hdl, EZFS_NOENT, errbuf);
4292 4332                          break;
4293 4333                  default:
4294 4334                          err = zfs_standard_error_fmt(hdl, errno, errbuf);
4295 4335                          break;
4296 4336                  }
4297 4337          }
4298 4338  
4299 4339          free(nvbuf);
4300 4340  
4301 4341          return (err);
4302 4342  }
4303 4343  
4304 4344  int
4305 4345  zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4306 4346  {
4307 4347          zfs_cmd_t zc = { 0 };
4308 4348          libzfs_handle_t *hdl = zhp->zfs_hdl;
4309 4349          int nvsz = 2048;
4310 4350          void *nvbuf;
4311 4351          int err = 0;
4312 4352          char errbuf[ZFS_MAXNAMELEN+32];
4313 4353  
4314 4354          assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4315 4355  
4316 4356  tryagain:
4317 4357  
4318 4358          nvbuf = malloc(nvsz);
4319 4359          if (nvbuf == NULL) {
4320 4360                  err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4321 4361                  goto out;
4322 4362          }
4323 4363  
4324 4364          zc.zc_nvlist_dst_size = nvsz;
4325 4365          zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4326 4366  
4327 4367          (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4328 4368  
4329 4369          if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4330 4370                  (void) snprintf(errbuf, sizeof (errbuf),
4331 4371                      dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4332 4372                      zc.zc_name);
4333 4373                  switch (errno) {
4334 4374                  case ENOMEM:
4335 4375                          free(nvbuf);
4336 4376                          nvsz = zc.zc_nvlist_dst_size;
4337 4377                          goto tryagain;
4338 4378  
4339 4379                  case ENOTSUP:
4340 4380                          zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4341 4381                              "pool must be upgraded"));
4342 4382                          err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4343 4383                          break;
4344 4384                  case EINVAL:
4345 4385                          err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4346 4386                          break;
4347 4387                  case ENOENT:
4348 4388                          err = zfs_error(hdl, EZFS_NOENT, errbuf);
4349 4389                          break;
4350 4390                  default:
4351 4391                          err = zfs_standard_error_fmt(hdl, errno, errbuf);
4352 4392                          break;
4353 4393                  }
4354 4394          } else {
4355 4395                  /* success */
4356 4396                  int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4357 4397                  if (rc) {
4358 4398                          (void) snprintf(errbuf, sizeof (errbuf),
4359 4399                              dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4360 4400                              zc.zc_name);
4361 4401                          err = zfs_standard_error_fmt(hdl, rc, errbuf);
4362 4402                  }
4363 4403          }
4364 4404  
4365 4405          free(nvbuf);
4366 4406  out:
4367 4407          return (err);
4368 4408  }
4369 4409  
4370 4410  uint64_t
4371 4411  zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4372 4412  {
4373 4413          uint64_t numdb;
4374 4414          uint64_t nblocks, volblocksize;
4375 4415          int ncopies;
4376 4416          char *strval;
4377 4417  
4378 4418          if (nvlist_lookup_string(props,
4379 4419              zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4380 4420                  ncopies = atoi(strval);
4381 4421          else
4382 4422                  ncopies = 1;
4383 4423          if (nvlist_lookup_uint64(props,
4384 4424              zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4385 4425              &volblocksize) != 0)
4386 4426                  volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4387 4427          nblocks = volsize/volblocksize;
4388 4428          /* start with metadnode L0-L6 */
4389 4429          numdb = 7;
4390 4430          /* calculate number of indirects */
4391 4431          while (nblocks > 1) {
4392 4432                  nblocks += DNODES_PER_LEVEL - 1;
4393 4433                  nblocks /= DNODES_PER_LEVEL;
4394 4434                  numdb += nblocks;
4395 4435          }
4396 4436          numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4397 4437          volsize *= ncopies;
4398 4438          /*
4399 4439           * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4400 4440           * compressed, but in practice they compress down to about
4401 4441           * 1100 bytes
4402 4442           */
4403 4443          numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4404 4444          volsize += numdb;
4405 4445          return (volsize);
4406 4446  }
  
    | 
      ↓ open down ↓ | 
    2178 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX