Print this page
    
5882 Temporary pool names
Reviewed by: Matt Ahrens <matt@delphix.com>
Reviewed by: Igor Kozhukhov <igor@dilos.org>
Reviewed by: John Kennedy <john.kennedy@delphix.com>
Approved by: Dan McDonald <danmcd@joyent.com>
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/zfs/zfs_ioctl.c
          +++ new/usr/src/uts/common/fs/zfs/zfs_ioctl.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) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
  25   25   * Portions Copyright 2011 Martin Matuska
  26   26   * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
  27   27   * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  28   28   * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
  29   29   * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
  30   30   * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  31   31   * Copyright (c) 2013 Steven Hartland. All rights reserved.
  32   32   * Copyright (c) 2014 Integros [integros.com]
  33   33   * Copyright 2016 Toomas Soome <tsoome@me.com>
  34   34   * Copyright 2017 RackTop Systems.
  35   35   * Copyright (c) 2017 Datto Inc.
  36   36   */
  37   37  
  38   38  /*
  39   39   * ZFS ioctls.
  40   40   *
  41   41   * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
  42   42   * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
  43   43   *
  44   44   * There are two ways that we handle ioctls: the legacy way where almost
  45   45   * all of the logic is in the ioctl callback, and the new way where most
  46   46   * of the marshalling is handled in the common entry point, zfsdev_ioctl().
  47   47   *
  48   48   * Non-legacy ioctls should be registered by calling
  49   49   * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
  50   50   * from userland by lzc_ioctl().
  51   51   *
  52   52   * The registration arguments are as follows:
  53   53   *
  54   54   * const char *name
  55   55   *   The name of the ioctl.  This is used for history logging.  If the
  56   56   *   ioctl returns successfully (the callback returns 0), and allow_log
  57   57   *   is true, then a history log entry will be recorded with the input &
  58   58   *   output nvlists.  The log entry can be printed with "zpool history -i".
  59   59   *
  60   60   * zfs_ioc_t ioc
  61   61   *   The ioctl request number, which userland will pass to ioctl(2).
  62   62   *   The ioctl numbers can change from release to release, because
  63   63   *   the caller (libzfs) must be matched to the kernel.
  64   64   *
  65   65   * zfs_secpolicy_func_t *secpolicy
  66   66   *   This function will be called before the zfs_ioc_func_t, to
  67   67   *   determine if this operation is permitted.  It should return EPERM
  68   68   *   on failure, and 0 on success.  Checks include determining if the
  69   69   *   dataset is visible in this zone, and if the user has either all
  70   70   *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
  71   71   *   to do this operation on this dataset with "zfs allow".
  72   72   *
  73   73   * zfs_ioc_namecheck_t namecheck
  74   74   *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
  75   75   *   name, a dataset name, or nothing.  If the name is not well-formed,
  76   76   *   the ioctl will fail and the callback will not be called.
  77   77   *   Therefore, the callback can assume that the name is well-formed
  78   78   *   (e.g. is null-terminated, doesn't have more than one '@' character,
  79   79   *   doesn't have invalid characters).
  80   80   *
  81   81   * zfs_ioc_poolcheck_t pool_check
  82   82   *   This specifies requirements on the pool state.  If the pool does
  83   83   *   not meet them (is suspended or is readonly), the ioctl will fail
  84   84   *   and the callback will not be called.  If any checks are specified
  85   85   *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
  86   86   *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
  87   87   *   POOL_CHECK_READONLY).
  88   88   *
  89   89   * boolean_t smush_outnvlist
  90   90   *   If smush_outnvlist is true, then the output is presumed to be a
  91   91   *   list of errors, and it will be "smushed" down to fit into the
  92   92   *   caller's buffer, by removing some entries and replacing them with a
  93   93   *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
  94   94   *   nvlist_smush() for details.  If smush_outnvlist is false, and the
  95   95   *   outnvlist does not fit into the userland-provided buffer, then the
  96   96   *   ioctl will fail with ENOMEM.
  97   97   *
  98   98   * zfs_ioc_func_t *func
  99   99   *   The callback function that will perform the operation.
 100  100   *
 101  101   *   The callback should return 0 on success, or an error number on
 102  102   *   failure.  If the function fails, the userland ioctl will return -1,
 103  103   *   and errno will be set to the callback's return value.  The callback
 104  104   *   will be called with the following arguments:
 105  105   *
 106  106   *   const char *name
 107  107   *     The name of the pool or dataset to operate on, from
 108  108   *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
 109  109   *     expected type (pool, dataset, or none).
 110  110   *
 111  111   *   nvlist_t *innvl
 112  112   *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
 113  113   *     NULL if no input nvlist was provided.  Changes to this nvlist are
 114  114   *     ignored.  If the input nvlist could not be deserialized, the
 115  115   *     ioctl will fail and the callback will not be called.
 116  116   *
 117  117   *   nvlist_t *outnvl
 118  118   *     The output nvlist, initially empty.  The callback can fill it in,
 119  119   *     and it will be returned to userland by serializing it into
 120  120   *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
 121  121   *     fails (e.g. because the caller didn't supply a large enough
 122  122   *     buffer), then the overall ioctl will fail.  See the
 123  123   *     'smush_nvlist' argument above for additional behaviors.
 124  124   *
 125  125   *     There are two typical uses of the output nvlist:
 126  126   *       - To return state, e.g. property values.  In this case,
 127  127   *         smush_outnvlist should be false.  If the buffer was not large
 128  128   *         enough, the caller will reallocate a larger buffer and try
 129  129   *         the ioctl again.
 130  130   *
 131  131   *       - To return multiple errors from an ioctl which makes on-disk
 132  132   *         changes.  In this case, smush_outnvlist should be true.
 133  133   *         Ioctls which make on-disk modifications should generally not
 134  134   *         use the outnvl if they succeed, because the caller can not
 135  135   *         distinguish between the operation failing, and
 136  136   *         deserialization failing.
 137  137   */
 138  138  
 139  139  #include <sys/types.h>
 140  140  #include <sys/param.h>
 141  141  #include <sys/errno.h>
 142  142  #include <sys/uio.h>
 143  143  #include <sys/buf.h>
 144  144  #include <sys/modctl.h>
 145  145  #include <sys/open.h>
 146  146  #include <sys/file.h>
 147  147  #include <sys/kmem.h>
 148  148  #include <sys/conf.h>
 149  149  #include <sys/cmn_err.h>
 150  150  #include <sys/stat.h>
 151  151  #include <sys/zfs_ioctl.h>
 152  152  #include <sys/zfs_vfsops.h>
 153  153  #include <sys/zfs_znode.h>
 154  154  #include <sys/zap.h>
 155  155  #include <sys/spa.h>
 156  156  #include <sys/spa_impl.h>
 157  157  #include <sys/vdev.h>
 158  158  #include <sys/priv_impl.h>
 159  159  #include <sys/dmu.h>
 160  160  #include <sys/dsl_dir.h>
 161  161  #include <sys/dsl_dataset.h>
 162  162  #include <sys/dsl_prop.h>
 163  163  #include <sys/dsl_deleg.h>
 164  164  #include <sys/dmu_objset.h>
 165  165  #include <sys/dmu_impl.h>
 166  166  #include <sys/dmu_tx.h>
 167  167  #include <sys/ddi.h>
 168  168  #include <sys/sunddi.h>
 169  169  #include <sys/sunldi.h>
 170  170  #include <sys/policy.h>
 171  171  #include <sys/zone.h>
 172  172  #include <sys/nvpair.h>
 173  173  #include <sys/pathname.h>
 174  174  #include <sys/mount.h>
 175  175  #include <sys/sdt.h>
 176  176  #include <sys/fs/zfs.h>
 177  177  #include <sys/zfs_ctldir.h>
 178  178  #include <sys/zfs_dir.h>
 179  179  #include <sys/zfs_onexit.h>
 180  180  #include <sys/zvol.h>
 181  181  #include <sys/dsl_scan.h>
 182  182  #include <sharefs/share.h>
 183  183  #include <sys/dmu_objset.h>
 184  184  #include <sys/dmu_send.h>
 185  185  #include <sys/dsl_destroy.h>
 186  186  #include <sys/dsl_bookmark.h>
 187  187  #include <sys/dsl_userhold.h>
 188  188  #include <sys/zfeature.h>
 189  189  #include <sys/zcp.h>
 190  190  #include <sys/zio_checksum.h>
 191  191  #include <sys/vdev_removal.h>
 192  192  #include <sys/vdev_impl.h>
 193  193  #include <sys/vdev_initialize.h>
 194  194  
 195  195  #include "zfs_namecheck.h"
 196  196  #include "zfs_prop.h"
 197  197  #include "zfs_deleg.h"
 198  198  #include "zfs_comutil.h"
 199  199  
 200  200  #include "lua.h"
 201  201  #include "lauxlib.h"
 202  202  
 203  203  extern struct modlfs zfs_modlfs;
 204  204  
 205  205  extern void zfs_init(void);
 206  206  extern void zfs_fini(void);
 207  207  
 208  208  ldi_ident_t zfs_li = NULL;
 209  209  dev_info_t *zfs_dip;
 210  210  
 211  211  uint_t zfs_fsyncer_key;
 212  212  extern uint_t rrw_tsd_key;
 213  213  static uint_t zfs_allow_log_key;
 214  214  
 215  215  typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
 216  216  typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
 217  217  typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
 218  218  
 219  219  typedef enum {
 220  220          NO_NAME,
 221  221          POOL_NAME,
 222  222          DATASET_NAME
 223  223  } zfs_ioc_namecheck_t;
 224  224  
 225  225  typedef enum {
 226  226          POOL_CHECK_NONE         = 1 << 0,
 227  227          POOL_CHECK_SUSPENDED    = 1 << 1,
 228  228          POOL_CHECK_READONLY     = 1 << 2,
 229  229  } zfs_ioc_poolcheck_t;
 230  230  
 231  231  typedef struct zfs_ioc_vec {
 232  232          zfs_ioc_legacy_func_t   *zvec_legacy_func;
 233  233          zfs_ioc_func_t          *zvec_func;
 234  234          zfs_secpolicy_func_t    *zvec_secpolicy;
 235  235          zfs_ioc_namecheck_t     zvec_namecheck;
 236  236          boolean_t               zvec_allow_log;
 237  237          zfs_ioc_poolcheck_t     zvec_pool_check;
 238  238          boolean_t               zvec_smush_outnvlist;
 239  239          const char              *zvec_name;
 240  240  } zfs_ioc_vec_t;
 241  241  
 242  242  /* This array is indexed by zfs_userquota_prop_t */
 243  243  static const char *userquota_perms[] = {
 244  244          ZFS_DELEG_PERM_USERUSED,
 245  245          ZFS_DELEG_PERM_USERQUOTA,
 246  246          ZFS_DELEG_PERM_GROUPUSED,
 247  247          ZFS_DELEG_PERM_GROUPQUOTA,
 248  248  };
 249  249  
 250  250  static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
 251  251  static int zfs_check_settable(const char *name, nvpair_t *property,
 252  252      cred_t *cr);
 253  253  static int zfs_check_clearable(char *dataset, nvlist_t *props,
 254  254      nvlist_t **errors);
 255  255  static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
 256  256      boolean_t *);
 257  257  int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
 258  258  static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
 259  259  
 260  260  static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
 261  261  
 262  262  /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
 263  263  void
 264  264  __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
 265  265  {
 266  266          const char *newfile;
 267  267          char buf[512];
 268  268          va_list adx;
 269  269  
 270  270          /*
 271  271           * Get rid of annoying "../common/" prefix to filename.
 272  272           */
 273  273          newfile = strrchr(file, '/');
 274  274          if (newfile != NULL) {
 275  275                  newfile = newfile + 1; /* Get rid of leading / */
 276  276          } else {
 277  277                  newfile = file;
 278  278          }
 279  279  
 280  280          va_start(adx, fmt);
 281  281          (void) vsnprintf(buf, sizeof (buf), fmt, adx);
 282  282          va_end(adx);
 283  283  
 284  284          /*
 285  285           * To get this data, use the zfs-dprintf probe as so:
 286  286           * dtrace -q -n 'zfs-dprintf \
 287  287           *      /stringof(arg0) == "dbuf.c"/ \
 288  288           *      {printf("%s: %s", stringof(arg1), stringof(arg3))}'
 289  289           * arg0 = file name
 290  290           * arg1 = function name
 291  291           * arg2 = line number
 292  292           * arg3 = message
 293  293           */
 294  294          DTRACE_PROBE4(zfs__dprintf,
 295  295              char *, newfile, char *, func, int, line, char *, buf);
 296  296  }
 297  297  
 298  298  static void
 299  299  history_str_free(char *buf)
 300  300  {
 301  301          kmem_free(buf, HIS_MAX_RECORD_LEN);
 302  302  }
 303  303  
 304  304  static char *
 305  305  history_str_get(zfs_cmd_t *zc)
 306  306  {
 307  307          char *buf;
 308  308  
 309  309          if (zc->zc_history == NULL)
 310  310                  return (NULL);
 311  311  
 312  312          buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
 313  313          if (copyinstr((void *)(uintptr_t)zc->zc_history,
 314  314              buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
 315  315                  history_str_free(buf);
 316  316                  return (NULL);
 317  317          }
 318  318  
 319  319          buf[HIS_MAX_RECORD_LEN -1] = '\0';
 320  320  
 321  321          return (buf);
 322  322  }
 323  323  
 324  324  /*
 325  325   * Check to see if the named dataset is currently defined as bootable
 326  326   */
 327  327  static boolean_t
 328  328  zfs_is_bootfs(const char *name)
 329  329  {
 330  330          objset_t *os;
 331  331  
 332  332          if (dmu_objset_hold(name, FTAG, &os) == 0) {
 333  333                  boolean_t ret;
 334  334                  ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
 335  335                  dmu_objset_rele(os, FTAG);
 336  336                  return (ret);
 337  337          }
 338  338          return (B_FALSE);
 339  339  }
 340  340  
 341  341  /*
 342  342   * Return non-zero if the spa version is less than requested version.
 343  343   */
 344  344  static int
 345  345  zfs_earlier_version(const char *name, int version)
 346  346  {
 347  347          spa_t *spa;
 348  348  
 349  349          if (spa_open(name, &spa, FTAG) == 0) {
 350  350                  if (spa_version(spa) < version) {
 351  351                          spa_close(spa, FTAG);
 352  352                          return (1);
 353  353                  }
 354  354                  spa_close(spa, FTAG);
 355  355          }
 356  356          return (0);
 357  357  }
 358  358  
 359  359  /*
 360  360   * Return TRUE if the ZPL version is less than requested version.
 361  361   */
 362  362  static boolean_t
 363  363  zpl_earlier_version(const char *name, int version)
 364  364  {
 365  365          objset_t *os;
 366  366          boolean_t rc = B_TRUE;
 367  367  
 368  368          if (dmu_objset_hold(name, FTAG, &os) == 0) {
 369  369                  uint64_t zplversion;
 370  370  
 371  371                  if (dmu_objset_type(os) != DMU_OST_ZFS) {
 372  372                          dmu_objset_rele(os, FTAG);
 373  373                          return (B_TRUE);
 374  374                  }
 375  375                  /* XXX reading from non-owned objset */
 376  376                  if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
 377  377                          rc = zplversion < version;
 378  378                  dmu_objset_rele(os, FTAG);
 379  379          }
 380  380          return (rc);
 381  381  }
 382  382  
 383  383  static void
 384  384  zfs_log_history(zfs_cmd_t *zc)
 385  385  {
 386  386          spa_t *spa;
 387  387          char *buf;
 388  388  
 389  389          if ((buf = history_str_get(zc)) == NULL)
 390  390                  return;
 391  391  
 392  392          if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
 393  393                  if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
 394  394                          (void) spa_history_log(spa, buf);
 395  395                  spa_close(spa, FTAG);
 396  396          }
 397  397          history_str_free(buf);
 398  398  }
 399  399  
 400  400  /*
 401  401   * Policy for top-level read operations (list pools).  Requires no privileges,
 402  402   * and can be used in the local zone, as there is no associated dataset.
 403  403   */
 404  404  /* ARGSUSED */
 405  405  static int
 406  406  zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 407  407  {
 408  408          return (0);
 409  409  }
 410  410  
 411  411  /*
 412  412   * Policy for dataset read operations (list children, get statistics).  Requires
 413  413   * no privileges, but must be visible in the local zone.
 414  414   */
 415  415  /* ARGSUSED */
 416  416  static int
 417  417  zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 418  418  {
 419  419          if (INGLOBALZONE(curproc) ||
 420  420              zone_dataset_visible(zc->zc_name, NULL))
 421  421                  return (0);
 422  422  
 423  423          return (SET_ERROR(ENOENT));
 424  424  }
 425  425  
 426  426  static int
 427  427  zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
 428  428  {
 429  429          int writable = 1;
 430  430  
 431  431          /*
 432  432           * The dataset must be visible by this zone -- check this first
 433  433           * so they don't see EPERM on something they shouldn't know about.
 434  434           */
 435  435          if (!INGLOBALZONE(curproc) &&
 436  436              !zone_dataset_visible(dataset, &writable))
 437  437                  return (SET_ERROR(ENOENT));
 438  438  
 439  439          if (INGLOBALZONE(curproc)) {
 440  440                  /*
 441  441                   * If the fs is zoned, only root can access it from the
 442  442                   * global zone.
 443  443                   */
 444  444                  if (secpolicy_zfs(cr) && zoned)
 445  445                          return (SET_ERROR(EPERM));
 446  446          } else {
 447  447                  /*
 448  448                   * If we are in a local zone, the 'zoned' property must be set.
 449  449                   */
 450  450                  if (!zoned)
 451  451                          return (SET_ERROR(EPERM));
 452  452  
 453  453                  /* must be writable by this zone */
 454  454                  if (!writable)
 455  455                          return (SET_ERROR(EPERM));
 456  456          }
 457  457          return (0);
 458  458  }
 459  459  
 460  460  static int
 461  461  zfs_dozonecheck(const char *dataset, cred_t *cr)
 462  462  {
 463  463          uint64_t zoned;
 464  464  
 465  465          if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
 466  466                  return (SET_ERROR(ENOENT));
 467  467  
 468  468          return (zfs_dozonecheck_impl(dataset, zoned, cr));
 469  469  }
 470  470  
 471  471  static int
 472  472  zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
 473  473  {
 474  474          uint64_t zoned;
 475  475  
 476  476          if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
 477  477                  return (SET_ERROR(ENOENT));
 478  478  
 479  479          return (zfs_dozonecheck_impl(dataset, zoned, cr));
 480  480  }
 481  481  
 482  482  static int
 483  483  zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
 484  484      const char *perm, cred_t *cr)
 485  485  {
 486  486          int error;
 487  487  
 488  488          error = zfs_dozonecheck_ds(name, ds, cr);
 489  489          if (error == 0) {
 490  490                  error = secpolicy_zfs(cr);
 491  491                  if (error != 0)
 492  492                          error = dsl_deleg_access_impl(ds, perm, cr);
 493  493          }
 494  494          return (error);
 495  495  }
 496  496  
 497  497  static int
 498  498  zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
 499  499  {
 500  500          int error;
 501  501          dsl_dataset_t *ds;
 502  502          dsl_pool_t *dp;
 503  503  
 504  504          /*
 505  505           * First do a quick check for root in the global zone, which
 506  506           * is allowed to do all write_perms.  This ensures that zfs_ioc_*
 507  507           * will get to handle nonexistent datasets.
 508  508           */
 509  509          if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
 510  510                  return (0);
 511  511  
 512  512          error = dsl_pool_hold(name, FTAG, &dp);
 513  513          if (error != 0)
 514  514                  return (error);
 515  515  
 516  516          error = dsl_dataset_hold(dp, name, FTAG, &ds);
 517  517          if (error != 0) {
 518  518                  dsl_pool_rele(dp, FTAG);
 519  519                  return (error);
 520  520          }
 521  521  
 522  522          error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
 523  523  
 524  524          dsl_dataset_rele(ds, FTAG);
 525  525          dsl_pool_rele(dp, FTAG);
 526  526          return (error);
 527  527  }
 528  528  
 529  529  /*
 530  530   * Policy for setting the security label property.
 531  531   *
 532  532   * Returns 0 for success, non-zero for access and other errors.
 533  533   */
 534  534  static int
 535  535  zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
 536  536  {
 537  537          char            ds_hexsl[MAXNAMELEN];
 538  538          bslabel_t       ds_sl, new_sl;
 539  539          boolean_t       new_default = FALSE;
 540  540          uint64_t        zoned;
 541  541          int             needed_priv = -1;
 542  542          int             error;
 543  543  
 544  544          /* First get the existing dataset label. */
 545  545          error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
 546  546              1, sizeof (ds_hexsl), &ds_hexsl, NULL);
 547  547          if (error != 0)
 548  548                  return (SET_ERROR(EPERM));
 549  549  
 550  550          if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
 551  551                  new_default = TRUE;
 552  552  
 553  553          /* The label must be translatable */
 554  554          if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
 555  555                  return (SET_ERROR(EINVAL));
 556  556  
 557  557          /*
 558  558           * In a non-global zone, disallow attempts to set a label that
 559  559           * doesn't match that of the zone; otherwise no other checks
 560  560           * are needed.
 561  561           */
 562  562          if (!INGLOBALZONE(curproc)) {
 563  563                  if (new_default || !blequal(&new_sl, CR_SL(CRED())))
 564  564                          return (SET_ERROR(EPERM));
 565  565                  return (0);
 566  566          }
 567  567  
 568  568          /*
 569  569           * For global-zone datasets (i.e., those whose zoned property is
 570  570           * "off", verify that the specified new label is valid for the
 571  571           * global zone.
 572  572           */
 573  573          if (dsl_prop_get_integer(name,
 574  574              zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
 575  575                  return (SET_ERROR(EPERM));
 576  576          if (!zoned) {
 577  577                  if (zfs_check_global_label(name, strval) != 0)
 578  578                          return (SET_ERROR(EPERM));
 579  579          }
 580  580  
 581  581          /*
 582  582           * If the existing dataset label is nondefault, check if the
 583  583           * dataset is mounted (label cannot be changed while mounted).
 584  584           * Get the zfsvfs; if there isn't one, then the dataset isn't
 585  585           * mounted (or isn't a dataset, doesn't exist, ...).
 586  586           */
 587  587          if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
 588  588                  objset_t *os;
 589  589                  static char *setsl_tag = "setsl_tag";
 590  590  
 591  591                  /*
 592  592                   * Try to own the dataset; abort if there is any error,
 593  593                   * (e.g., already mounted, in use, or other error).
 594  594                   */
 595  595                  error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
 596  596                      setsl_tag, &os);
 597  597                  if (error != 0)
 598  598                          return (SET_ERROR(EPERM));
 599  599  
 600  600                  dmu_objset_disown(os, setsl_tag);
 601  601  
 602  602                  if (new_default) {
 603  603                          needed_priv = PRIV_FILE_DOWNGRADE_SL;
 604  604                          goto out_check;
 605  605                  }
 606  606  
 607  607                  if (hexstr_to_label(strval, &new_sl) != 0)
 608  608                          return (SET_ERROR(EPERM));
 609  609  
 610  610                  if (blstrictdom(&ds_sl, &new_sl))
 611  611                          needed_priv = PRIV_FILE_DOWNGRADE_SL;
 612  612                  else if (blstrictdom(&new_sl, &ds_sl))
 613  613                          needed_priv = PRIV_FILE_UPGRADE_SL;
 614  614          } else {
 615  615                  /* dataset currently has a default label */
 616  616                  if (!new_default)
 617  617                          needed_priv = PRIV_FILE_UPGRADE_SL;
 618  618          }
 619  619  
 620  620  out_check:
 621  621          if (needed_priv != -1)
 622  622                  return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
 623  623          return (0);
 624  624  }
 625  625  
 626  626  static int
 627  627  zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
 628  628      cred_t *cr)
 629  629  {
 630  630          char *strval;
 631  631  
 632  632          /*
 633  633           * Check permissions for special properties.
 634  634           */
 635  635          switch (prop) {
 636  636          case ZFS_PROP_ZONED:
 637  637                  /*
 638  638                   * Disallow setting of 'zoned' from within a local zone.
 639  639                   */
 640  640                  if (!INGLOBALZONE(curproc))
 641  641                          return (SET_ERROR(EPERM));
 642  642                  break;
 643  643  
 644  644          case ZFS_PROP_QUOTA:
 645  645          case ZFS_PROP_FILESYSTEM_LIMIT:
 646  646          case ZFS_PROP_SNAPSHOT_LIMIT:
 647  647                  if (!INGLOBALZONE(curproc)) {
 648  648                          uint64_t zoned;
 649  649                          char setpoint[ZFS_MAX_DATASET_NAME_LEN];
 650  650                          /*
 651  651                           * Unprivileged users are allowed to modify the
 652  652                           * limit on things *under* (ie. contained by)
 653  653                           * the thing they own.
 654  654                           */
 655  655                          if (dsl_prop_get_integer(dsname, "zoned", &zoned,
 656  656                              setpoint))
 657  657                                  return (SET_ERROR(EPERM));
 658  658                          if (!zoned || strlen(dsname) <= strlen(setpoint))
 659  659                                  return (SET_ERROR(EPERM));
 660  660                  }
 661  661                  break;
 662  662  
 663  663          case ZFS_PROP_MLSLABEL:
 664  664                  if (!is_system_labeled())
 665  665                          return (SET_ERROR(EPERM));
 666  666  
 667  667                  if (nvpair_value_string(propval, &strval) == 0) {
 668  668                          int err;
 669  669  
 670  670                          err = zfs_set_slabel_policy(dsname, strval, CRED());
 671  671                          if (err != 0)
 672  672                                  return (err);
 673  673                  }
 674  674                  break;
 675  675          }
 676  676  
 677  677          return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
 678  678  }
 679  679  
 680  680  /* ARGSUSED */
 681  681  static int
 682  682  zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 683  683  {
 684  684          int error;
 685  685  
 686  686          error = zfs_dozonecheck(zc->zc_name, cr);
 687  687          if (error != 0)
 688  688                  return (error);
 689  689  
 690  690          /*
 691  691           * permission to set permissions will be evaluated later in
 692  692           * dsl_deleg_can_allow()
 693  693           */
 694  694          return (0);
 695  695  }
 696  696  
 697  697  /* ARGSUSED */
 698  698  static int
 699  699  zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 700  700  {
 701  701          return (zfs_secpolicy_write_perms(zc->zc_name,
 702  702              ZFS_DELEG_PERM_ROLLBACK, cr));
 703  703  }
 704  704  
 705  705  /* ARGSUSED */
 706  706  static int
 707  707  zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 708  708  {
 709  709          dsl_pool_t *dp;
 710  710          dsl_dataset_t *ds;
 711  711          char *cp;
 712  712          int error;
 713  713  
 714  714          /*
 715  715           * Generate the current snapshot name from the given objsetid, then
 716  716           * use that name for the secpolicy/zone checks.
 717  717           */
 718  718          cp = strchr(zc->zc_name, '@');
 719  719          if (cp == NULL)
 720  720                  return (SET_ERROR(EINVAL));
 721  721          error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
 722  722          if (error != 0)
 723  723                  return (error);
 724  724  
 725  725          error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
 726  726          if (error != 0) {
 727  727                  dsl_pool_rele(dp, FTAG);
 728  728                  return (error);
 729  729          }
 730  730  
 731  731          dsl_dataset_name(ds, zc->zc_name);
 732  732  
 733  733          error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
 734  734              ZFS_DELEG_PERM_SEND, cr);
 735  735          dsl_dataset_rele(ds, FTAG);
 736  736          dsl_pool_rele(dp, FTAG);
 737  737  
 738  738          return (error);
 739  739  }
 740  740  
 741  741  /* ARGSUSED */
 742  742  static int
 743  743  zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 744  744  {
 745  745          return (zfs_secpolicy_write_perms(zc->zc_name,
 746  746              ZFS_DELEG_PERM_SEND, cr));
 747  747  }
 748  748  
 749  749  /* ARGSUSED */
 750  750  static int
 751  751  zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 752  752  {
 753  753          vnode_t *vp;
 754  754          int error;
 755  755  
 756  756          if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
 757  757              NO_FOLLOW, NULL, &vp)) != 0)
 758  758                  return (error);
 759  759  
 760  760          /* Now make sure mntpnt and dataset are ZFS */
 761  761  
 762  762          if (vp->v_vfsp->vfs_fstype != zfsfstype ||
 763  763              (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
 764  764              zc->zc_name) != 0)) {
 765  765                  VN_RELE(vp);
 766  766                  return (SET_ERROR(EPERM));
 767  767          }
 768  768  
 769  769          VN_RELE(vp);
 770  770          return (dsl_deleg_access(zc->zc_name,
 771  771              ZFS_DELEG_PERM_SHARE, cr));
 772  772  }
 773  773  
 774  774  int
 775  775  zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 776  776  {
 777  777          if (!INGLOBALZONE(curproc))
 778  778                  return (SET_ERROR(EPERM));
 779  779  
 780  780          if (secpolicy_nfs(cr) == 0) {
 781  781                  return (0);
 782  782          } else {
 783  783                  return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 784  784          }
 785  785  }
 786  786  
 787  787  int
 788  788  zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 789  789  {
 790  790          if (!INGLOBALZONE(curproc))
 791  791                  return (SET_ERROR(EPERM));
 792  792  
 793  793          if (secpolicy_smb(cr) == 0) {
 794  794                  return (0);
 795  795          } else {
 796  796                  return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 797  797          }
 798  798  }
 799  799  
 800  800  static int
 801  801  zfs_get_parent(const char *datasetname, char *parent, int parentsize)
 802  802  {
 803  803          char *cp;
 804  804  
 805  805          /*
 806  806           * Remove the @bla or /bla from the end of the name to get the parent.
 807  807           */
 808  808          (void) strncpy(parent, datasetname, parentsize);
 809  809          cp = strrchr(parent, '@');
 810  810          if (cp != NULL) {
 811  811                  cp[0] = '\0';
 812  812          } else {
 813  813                  cp = strrchr(parent, '/');
 814  814                  if (cp == NULL)
 815  815                          return (SET_ERROR(ENOENT));
 816  816                  cp[0] = '\0';
 817  817          }
 818  818  
 819  819          return (0);
 820  820  }
 821  821  
 822  822  int
 823  823  zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
 824  824  {
 825  825          int error;
 826  826  
 827  827          if ((error = zfs_secpolicy_write_perms(name,
 828  828              ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 829  829                  return (error);
 830  830  
 831  831          return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
 832  832  }
 833  833  
 834  834  /* ARGSUSED */
 835  835  static int
 836  836  zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 837  837  {
 838  838          return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
 839  839  }
 840  840  
 841  841  /*
 842  842   * Destroying snapshots with delegated permissions requires
 843  843   * descendant mount and destroy permissions.
 844  844   */
 845  845  /* ARGSUSED */
 846  846  static int
 847  847  zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 848  848  {
 849  849          nvlist_t *snaps;
 850  850          nvpair_t *pair, *nextpair;
 851  851          int error = 0;
 852  852  
 853  853          if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
 854  854                  return (SET_ERROR(EINVAL));
 855  855          for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
 856  856              pair = nextpair) {
 857  857                  nextpair = nvlist_next_nvpair(snaps, pair);
 858  858                  error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
 859  859                  if (error == ENOENT) {
 860  860                          /*
 861  861                           * Ignore any snapshots that don't exist (we consider
 862  862                           * them "already destroyed").  Remove the name from the
 863  863                           * nvl here in case the snapshot is created between
 864  864                           * now and when we try to destroy it (in which case
 865  865                           * we don't want to destroy it since we haven't
 866  866                           * checked for permission).
 867  867                           */
 868  868                          fnvlist_remove_nvpair(snaps, pair);
 869  869                          error = 0;
 870  870                  }
 871  871                  if (error != 0)
 872  872                          break;
 873  873          }
 874  874  
 875  875          return (error);
 876  876  }
 877  877  
 878  878  int
 879  879  zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
 880  880  {
 881  881          char    parentname[ZFS_MAX_DATASET_NAME_LEN];
 882  882          int     error;
 883  883  
 884  884          if ((error = zfs_secpolicy_write_perms(from,
 885  885              ZFS_DELEG_PERM_RENAME, cr)) != 0)
 886  886                  return (error);
 887  887  
 888  888          if ((error = zfs_secpolicy_write_perms(from,
 889  889              ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 890  890                  return (error);
 891  891  
 892  892          if ((error = zfs_get_parent(to, parentname,
 893  893              sizeof (parentname))) != 0)
 894  894                  return (error);
 895  895  
 896  896          if ((error = zfs_secpolicy_write_perms(parentname,
 897  897              ZFS_DELEG_PERM_CREATE, cr)) != 0)
 898  898                  return (error);
 899  899  
 900  900          if ((error = zfs_secpolicy_write_perms(parentname,
 901  901              ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 902  902                  return (error);
 903  903  
 904  904          return (error);
 905  905  }
 906  906  
 907  907  /* ARGSUSED */
 908  908  static int
 909  909  zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 910  910  {
 911  911          return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
 912  912  }
 913  913  
 914  914  /* ARGSUSED */
 915  915  static int
 916  916  zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 917  917  {
 918  918          dsl_pool_t *dp;
 919  919          dsl_dataset_t *clone;
 920  920          int error;
 921  921  
 922  922          error = zfs_secpolicy_write_perms(zc->zc_name,
 923  923              ZFS_DELEG_PERM_PROMOTE, cr);
 924  924          if (error != 0)
 925  925                  return (error);
 926  926  
 927  927          error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
 928  928          if (error != 0)
 929  929                  return (error);
 930  930  
 931  931          error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
 932  932  
 933  933          if (error == 0) {
 934  934                  char parentname[ZFS_MAX_DATASET_NAME_LEN];
 935  935                  dsl_dataset_t *origin = NULL;
 936  936                  dsl_dir_t *dd;
 937  937                  dd = clone->ds_dir;
 938  938  
 939  939                  error = dsl_dataset_hold_obj(dd->dd_pool,
 940  940                      dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
 941  941                  if (error != 0) {
 942  942                          dsl_dataset_rele(clone, FTAG);
 943  943                          dsl_pool_rele(dp, FTAG);
 944  944                          return (error);
 945  945                  }
 946  946  
 947  947                  error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
 948  948                      ZFS_DELEG_PERM_MOUNT, cr);
 949  949  
 950  950                  dsl_dataset_name(origin, parentname);
 951  951                  if (error == 0) {
 952  952                          error = zfs_secpolicy_write_perms_ds(parentname, origin,
 953  953                              ZFS_DELEG_PERM_PROMOTE, cr);
 954  954                  }
 955  955                  dsl_dataset_rele(clone, FTAG);
 956  956                  dsl_dataset_rele(origin, FTAG);
 957  957          }
 958  958          dsl_pool_rele(dp, FTAG);
 959  959          return (error);
 960  960  }
 961  961  
 962  962  /* ARGSUSED */
 963  963  static int
 964  964  zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 965  965  {
 966  966          int error;
 967  967  
 968  968          if ((error = zfs_secpolicy_write_perms(zc->zc_name,
 969  969              ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
 970  970                  return (error);
 971  971  
 972  972          if ((error = zfs_secpolicy_write_perms(zc->zc_name,
 973  973              ZFS_DELEG_PERM_MOUNT, cr)) != 0)
 974  974                  return (error);
 975  975  
 976  976          return (zfs_secpolicy_write_perms(zc->zc_name,
 977  977              ZFS_DELEG_PERM_CREATE, cr));
 978  978  }
 979  979  
 980  980  int
 981  981  zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
 982  982  {
 983  983          return (zfs_secpolicy_write_perms(name,
 984  984              ZFS_DELEG_PERM_SNAPSHOT, cr));
 985  985  }
 986  986  
 987  987  /*
 988  988   * Check for permission to create each snapshot in the nvlist.
 989  989   */
 990  990  /* ARGSUSED */
 991  991  static int
 992  992  zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 993  993  {
 994  994          nvlist_t *snaps;
 995  995          int error = 0;
 996  996          nvpair_t *pair;
 997  997  
 998  998          if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
 999  999                  return (SET_ERROR(EINVAL));
1000 1000          for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1001 1001              pair = nvlist_next_nvpair(snaps, pair)) {
1002 1002                  char *name = nvpair_name(pair);
1003 1003                  char *atp = strchr(name, '@');
1004 1004  
1005 1005                  if (atp == NULL) {
1006 1006                          error = SET_ERROR(EINVAL);
1007 1007                          break;
1008 1008                  }
1009 1009                  *atp = '\0';
1010 1010                  error = zfs_secpolicy_snapshot_perms(name, cr);
1011 1011                  *atp = '@';
1012 1012                  if (error != 0)
1013 1013                          break;
1014 1014          }
1015 1015          return (error);
1016 1016  }
1017 1017  
1018 1018  /*
1019 1019   * Check for permission to create each snapshot in the nvlist.
1020 1020   */
1021 1021  /* ARGSUSED */
1022 1022  static int
1023 1023  zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1024 1024  {
1025 1025          int error = 0;
1026 1026  
1027 1027          for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1028 1028              pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1029 1029                  char *name = nvpair_name(pair);
1030 1030                  char *hashp = strchr(name, '#');
1031 1031  
1032 1032                  if (hashp == NULL) {
1033 1033                          error = SET_ERROR(EINVAL);
1034 1034                          break;
1035 1035                  }
1036 1036                  *hashp = '\0';
1037 1037                  error = zfs_secpolicy_write_perms(name,
1038 1038                      ZFS_DELEG_PERM_BOOKMARK, cr);
1039 1039                  *hashp = '#';
1040 1040                  if (error != 0)
1041 1041                          break;
1042 1042          }
1043 1043          return (error);
1044 1044  }
1045 1045  
1046 1046  /* ARGSUSED */
1047 1047  static int
1048 1048  zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1049 1049  {
1050 1050          return (zfs_secpolicy_write_perms(zc->zc_name,
1051 1051              ZFS_DELEG_PERM_REMAP, cr));
1052 1052  }
1053 1053  
1054 1054  /* ARGSUSED */
1055 1055  static int
1056 1056  zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1057 1057  {
1058 1058          nvpair_t *pair, *nextpair;
1059 1059          int error = 0;
1060 1060  
1061 1061          for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1062 1062              pair = nextpair) {
1063 1063                  char *name = nvpair_name(pair);
1064 1064                  char *hashp = strchr(name, '#');
1065 1065                  nextpair = nvlist_next_nvpair(innvl, pair);
1066 1066  
1067 1067                  if (hashp == NULL) {
1068 1068                          error = SET_ERROR(EINVAL);
1069 1069                          break;
1070 1070                  }
1071 1071  
1072 1072                  *hashp = '\0';
1073 1073                  error = zfs_secpolicy_write_perms(name,
1074 1074                      ZFS_DELEG_PERM_DESTROY, cr);
1075 1075                  *hashp = '#';
1076 1076                  if (error == ENOENT) {
1077 1077                          /*
1078 1078                           * Ignore any filesystems that don't exist (we consider
1079 1079                           * their bookmarks "already destroyed").  Remove
1080 1080                           * the name from the nvl here in case the filesystem
1081 1081                           * is created between now and when we try to destroy
1082 1082                           * the bookmark (in which case we don't want to
1083 1083                           * destroy it since we haven't checked for permission).
1084 1084                           */
1085 1085                          fnvlist_remove_nvpair(innvl, pair);
1086 1086                          error = 0;
1087 1087                  }
1088 1088                  if (error != 0)
1089 1089                          break;
1090 1090          }
1091 1091  
1092 1092          return (error);
1093 1093  }
1094 1094  
1095 1095  /* ARGSUSED */
1096 1096  static int
1097 1097  zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1098 1098  {
1099 1099          /*
1100 1100           * Even root must have a proper TSD so that we know what pool
1101 1101           * to log to.
1102 1102           */
1103 1103          if (tsd_get(zfs_allow_log_key) == NULL)
1104 1104                  return (SET_ERROR(EPERM));
1105 1105          return (0);
1106 1106  }
1107 1107  
1108 1108  static int
1109 1109  zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1110 1110  {
1111 1111          char    parentname[ZFS_MAX_DATASET_NAME_LEN];
1112 1112          int     error;
1113 1113          char    *origin;
1114 1114  
1115 1115          if ((error = zfs_get_parent(zc->zc_name, parentname,
1116 1116              sizeof (parentname))) != 0)
1117 1117                  return (error);
1118 1118  
1119 1119          if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1120 1120              (error = zfs_secpolicy_write_perms(origin,
1121 1121              ZFS_DELEG_PERM_CLONE, cr)) != 0)
1122 1122                  return (error);
1123 1123  
1124 1124          if ((error = zfs_secpolicy_write_perms(parentname,
1125 1125              ZFS_DELEG_PERM_CREATE, cr)) != 0)
1126 1126                  return (error);
1127 1127  
1128 1128          return (zfs_secpolicy_write_perms(parentname,
1129 1129              ZFS_DELEG_PERM_MOUNT, cr));
1130 1130  }
1131 1131  
1132 1132  /*
1133 1133   * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1134 1134   * SYS_CONFIG privilege, which is not available in a local zone.
1135 1135   */
1136 1136  /* ARGSUSED */
1137 1137  static int
1138 1138  zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1139 1139  {
1140 1140          if (secpolicy_sys_config(cr, B_FALSE) != 0)
1141 1141                  return (SET_ERROR(EPERM));
1142 1142  
1143 1143          return (0);
1144 1144  }
1145 1145  
1146 1146  /*
1147 1147   * Policy for object to name lookups.
1148 1148   */
1149 1149  /* ARGSUSED */
1150 1150  static int
1151 1151  zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1152 1152  {
1153 1153          int error;
1154 1154  
1155 1155          if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1156 1156                  return (0);
1157 1157  
1158 1158          error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1159 1159          return (error);
1160 1160  }
1161 1161  
1162 1162  /*
1163 1163   * Policy for fault injection.  Requires all privileges.
1164 1164   */
1165 1165  /* ARGSUSED */
1166 1166  static int
1167 1167  zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1168 1168  {
1169 1169          return (secpolicy_zinject(cr));
1170 1170  }
1171 1171  
1172 1172  /* ARGSUSED */
1173 1173  static int
1174 1174  zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1175 1175  {
1176 1176          zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1177 1177  
1178 1178          if (prop == ZPROP_INVAL) {
1179 1179                  if (!zfs_prop_user(zc->zc_value))
1180 1180                          return (SET_ERROR(EINVAL));
1181 1181                  return (zfs_secpolicy_write_perms(zc->zc_name,
1182 1182                      ZFS_DELEG_PERM_USERPROP, cr));
1183 1183          } else {
1184 1184                  return (zfs_secpolicy_setprop(zc->zc_name, prop,
1185 1185                      NULL, cr));
1186 1186          }
1187 1187  }
1188 1188  
1189 1189  static int
1190 1190  zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1191 1191  {
1192 1192          int err = zfs_secpolicy_read(zc, innvl, cr);
1193 1193          if (err)
1194 1194                  return (err);
1195 1195  
1196 1196          if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1197 1197                  return (SET_ERROR(EINVAL));
1198 1198  
1199 1199          if (zc->zc_value[0] == 0) {
1200 1200                  /*
1201 1201                   * They are asking about a posix uid/gid.  If it's
1202 1202                   * themself, allow it.
1203 1203                   */
1204 1204                  if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1205 1205                      zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1206 1206                          if (zc->zc_guid == crgetuid(cr))
1207 1207                                  return (0);
1208 1208                  } else {
1209 1209                          if (groupmember(zc->zc_guid, cr))
1210 1210                                  return (0);
1211 1211                  }
1212 1212          }
1213 1213  
1214 1214          return (zfs_secpolicy_write_perms(zc->zc_name,
1215 1215              userquota_perms[zc->zc_objset_type], cr));
1216 1216  }
1217 1217  
1218 1218  static int
1219 1219  zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1220 1220  {
1221 1221          int err = zfs_secpolicy_read(zc, innvl, cr);
1222 1222          if (err)
1223 1223                  return (err);
1224 1224  
1225 1225          if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1226 1226                  return (SET_ERROR(EINVAL));
1227 1227  
1228 1228          return (zfs_secpolicy_write_perms(zc->zc_name,
1229 1229              userquota_perms[zc->zc_objset_type], cr));
1230 1230  }
1231 1231  
1232 1232  /* ARGSUSED */
1233 1233  static int
1234 1234  zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1235 1235  {
1236 1236          return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1237 1237              NULL, cr));
1238 1238  }
1239 1239  
1240 1240  /* ARGSUSED */
1241 1241  static int
1242 1242  zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1243 1243  {
1244 1244          nvpair_t *pair;
1245 1245          nvlist_t *holds;
1246 1246          int error;
1247 1247  
1248 1248          error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1249 1249          if (error != 0)
1250 1250                  return (SET_ERROR(EINVAL));
1251 1251  
1252 1252          for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1253 1253              pair = nvlist_next_nvpair(holds, pair)) {
1254 1254                  char fsname[ZFS_MAX_DATASET_NAME_LEN];
1255 1255                  error = dmu_fsname(nvpair_name(pair), fsname);
1256 1256                  if (error != 0)
1257 1257                          return (error);
1258 1258                  error = zfs_secpolicy_write_perms(fsname,
1259 1259                      ZFS_DELEG_PERM_HOLD, cr);
1260 1260                  if (error != 0)
1261 1261                          return (error);
1262 1262          }
1263 1263          return (0);
1264 1264  }
1265 1265  
1266 1266  /* ARGSUSED */
1267 1267  static int
1268 1268  zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1269 1269  {
1270 1270          nvpair_t *pair;
1271 1271          int error;
1272 1272  
1273 1273          for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1274 1274              pair = nvlist_next_nvpair(innvl, pair)) {
1275 1275                  char fsname[ZFS_MAX_DATASET_NAME_LEN];
1276 1276                  error = dmu_fsname(nvpair_name(pair), fsname);
1277 1277                  if (error != 0)
1278 1278                          return (error);
1279 1279                  error = zfs_secpolicy_write_perms(fsname,
1280 1280                      ZFS_DELEG_PERM_RELEASE, cr);
1281 1281                  if (error != 0)
1282 1282                          return (error);
1283 1283          }
1284 1284          return (0);
1285 1285  }
1286 1286  
1287 1287  /*
1288 1288   * Policy for allowing temporary snapshots to be taken or released
1289 1289   */
1290 1290  static int
1291 1291  zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1292 1292  {
1293 1293          /*
1294 1294           * A temporary snapshot is the same as a snapshot,
1295 1295           * hold, destroy and release all rolled into one.
1296 1296           * Delegated diff alone is sufficient that we allow this.
1297 1297           */
1298 1298          int error;
1299 1299  
1300 1300          if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1301 1301              ZFS_DELEG_PERM_DIFF, cr)) == 0)
1302 1302                  return (0);
1303 1303  
1304 1304          error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1305 1305          if (error == 0)
1306 1306                  error = zfs_secpolicy_hold(zc, innvl, cr);
1307 1307          if (error == 0)
1308 1308                  error = zfs_secpolicy_release(zc, innvl, cr);
1309 1309          if (error == 0)
1310 1310                  error = zfs_secpolicy_destroy(zc, innvl, cr);
1311 1311          return (error);
1312 1312  }
1313 1313  
1314 1314  /*
1315 1315   * Returns the nvlist as specified by the user in the zfs_cmd_t.
1316 1316   */
1317 1317  static int
1318 1318  get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1319 1319  {
1320 1320          char *packed;
1321 1321          int error;
1322 1322          nvlist_t *list = NULL;
1323 1323  
1324 1324          /*
1325 1325           * Read in and unpack the user-supplied nvlist.
1326 1326           */
1327 1327          if (size == 0)
1328 1328                  return (SET_ERROR(EINVAL));
1329 1329  
1330 1330          packed = kmem_alloc(size, KM_SLEEP);
1331 1331  
1332 1332          if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1333 1333              iflag)) != 0) {
1334 1334                  kmem_free(packed, size);
1335 1335                  return (SET_ERROR(EFAULT));
1336 1336          }
1337 1337  
1338 1338          if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1339 1339                  kmem_free(packed, size);
1340 1340                  return (error);
1341 1341          }
1342 1342  
1343 1343          kmem_free(packed, size);
1344 1344  
1345 1345          *nvp = list;
1346 1346          return (0);
1347 1347  }
1348 1348  
1349 1349  /*
1350 1350   * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1351 1351   * Entries will be removed from the end of the nvlist, and one int32 entry
1352 1352   * named "N_MORE_ERRORS" will be added indicating how many entries were
1353 1353   * removed.
1354 1354   */
1355 1355  static int
1356 1356  nvlist_smush(nvlist_t *errors, size_t max)
1357 1357  {
1358 1358          size_t size;
1359 1359  
1360 1360          size = fnvlist_size(errors);
1361 1361  
1362 1362          if (size > max) {
1363 1363                  nvpair_t *more_errors;
1364 1364                  int n = 0;
1365 1365  
1366 1366                  if (max < 1024)
1367 1367                          return (SET_ERROR(ENOMEM));
1368 1368  
1369 1369                  fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1370 1370                  more_errors = nvlist_prev_nvpair(errors, NULL);
1371 1371  
1372 1372                  do {
1373 1373                          nvpair_t *pair = nvlist_prev_nvpair(errors,
1374 1374                              more_errors);
1375 1375                          fnvlist_remove_nvpair(errors, pair);
1376 1376                          n++;
1377 1377                          size = fnvlist_size(errors);
1378 1378                  } while (size > max);
1379 1379  
1380 1380                  fnvlist_remove_nvpair(errors, more_errors);
1381 1381                  fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1382 1382                  ASSERT3U(fnvlist_size(errors), <=, max);
1383 1383          }
1384 1384  
1385 1385          return (0);
1386 1386  }
1387 1387  
1388 1388  static int
1389 1389  put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1390 1390  {
1391 1391          char *packed = NULL;
1392 1392          int error = 0;
1393 1393          size_t size;
1394 1394  
1395 1395          size = fnvlist_size(nvl);
1396 1396  
1397 1397          if (size > zc->zc_nvlist_dst_size) {
1398 1398                  error = SET_ERROR(ENOMEM);
1399 1399          } else {
1400 1400                  packed = fnvlist_pack(nvl, &size);
1401 1401                  if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1402 1402                      size, zc->zc_iflags) != 0)
1403 1403                          error = SET_ERROR(EFAULT);
1404 1404                  fnvlist_pack_free(packed, size);
1405 1405          }
1406 1406  
1407 1407          zc->zc_nvlist_dst_size = size;
1408 1408          zc->zc_nvlist_dst_filled = B_TRUE;
1409 1409          return (error);
1410 1410  }
1411 1411  
1412 1412  int
1413 1413  getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1414 1414  {
1415 1415          int error = 0;
1416 1416          if (dmu_objset_type(os) != DMU_OST_ZFS) {
1417 1417                  return (SET_ERROR(EINVAL));
1418 1418          }
1419 1419  
1420 1420          mutex_enter(&os->os_user_ptr_lock);
1421 1421          *zfvp = dmu_objset_get_user(os);
1422 1422          if (*zfvp) {
1423 1423                  VFS_HOLD((*zfvp)->z_vfs);
1424 1424          } else {
1425 1425                  error = SET_ERROR(ESRCH);
1426 1426          }
1427 1427          mutex_exit(&os->os_user_ptr_lock);
1428 1428          return (error);
1429 1429  }
1430 1430  
1431 1431  int
1432 1432  getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1433 1433  {
1434 1434          objset_t *os;
1435 1435          int error;
1436 1436  
1437 1437          error = dmu_objset_hold(dsname, FTAG, &os);
1438 1438          if (error != 0)
1439 1439                  return (error);
1440 1440  
1441 1441          error = getzfsvfs_impl(os, zfvp);
1442 1442          dmu_objset_rele(os, FTAG);
1443 1443          return (error);
1444 1444  }
1445 1445  
1446 1446  /*
1447 1447   * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1448 1448   * case its z_vfs will be NULL, and it will be opened as the owner.
1449 1449   * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1450 1450   * which prevents all vnode ops from running.
1451 1451   */
1452 1452  static int
1453 1453  zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1454 1454  {
1455 1455          int error = 0;
1456 1456  
1457 1457          if (getzfsvfs(name, zfvp) != 0)
1458 1458                  error = zfsvfs_create(name, zfvp);
1459 1459          if (error == 0) {
1460 1460                  rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1461 1461                      RW_READER, tag);
1462 1462                  if ((*zfvp)->z_unmounted) {
1463 1463                          /*
1464 1464                           * XXX we could probably try again, since the unmounting
1465 1465                           * thread should be just about to disassociate the
1466 1466                           * objset from the zfsvfs.
1467 1467                           */
1468 1468                          rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1469 1469                          return (SET_ERROR(EBUSY));
1470 1470                  }
1471 1471          }
1472 1472          return (error);
1473 1473  }
1474 1474  
1475 1475  static void
1476 1476  zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1477 1477  {
1478 1478          rrm_exit(&zfsvfs->z_teardown_lock, tag);
1479 1479  
1480 1480          if (zfsvfs->z_vfs) {
1481 1481                  VFS_RELE(zfsvfs->z_vfs);
1482 1482          } else {
1483 1483                  dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1484 1484                  zfsvfs_free(zfsvfs);
  
    | 
      ↓ open down ↓ | 
    1484 lines elided | 
    
      ↑ open up ↑ | 
  
1485 1485          }
1486 1486  }
1487 1487  
1488 1488  static int
1489 1489  zfs_ioc_pool_create(zfs_cmd_t *zc)
1490 1490  {
1491 1491          int error;
1492 1492          nvlist_t *config, *props = NULL;
1493 1493          nvlist_t *rootprops = NULL;
1494 1494          nvlist_t *zplprops = NULL;
     1495 +        char *spa_name = zc->zc_name;
1495 1496  
1496 1497          if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1497 1498              zc->zc_iflags, &config))
1498 1499                  return (error);
1499 1500  
1500 1501          if (zc->zc_nvlist_src_size != 0 && (error =
1501 1502              get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1502 1503              zc->zc_iflags, &props))) {
1503 1504                  nvlist_free(config);
1504 1505                  return (error);
1505 1506          }
1506 1507  
1507 1508          if (props) {
1508 1509                  nvlist_t *nvl = NULL;
1509 1510                  uint64_t version = SPA_VERSION;
     1511 +                char *tname;
1510 1512  
1511 1513                  (void) nvlist_lookup_uint64(props,
1512 1514                      zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1513 1515                  if (!SPA_VERSION_IS_SUPPORTED(version)) {
1514 1516                          error = SET_ERROR(EINVAL);
1515 1517                          goto pool_props_bad;
1516 1518                  }
1517 1519                  (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1518 1520                  if (nvl) {
1519 1521                          error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1520 1522                          if (error != 0) {
1521 1523                                  nvlist_free(config);
  
    | 
      ↓ open down ↓ | 
    2 lines elided | 
    
      ↑ open up ↑ | 
  
1522 1524                                  nvlist_free(props);
1523 1525                                  return (error);
1524 1526                          }
1525 1527                          (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1526 1528                  }
1527 1529                  VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1528 1530                  error = zfs_fill_zplprops_root(version, rootprops,
1529 1531                      zplprops, NULL);
1530 1532                  if (error != 0)
1531 1533                          goto pool_props_bad;
     1534 +
     1535 +                if (nvlist_lookup_string(props,
     1536 +                    zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
     1537 +                        spa_name = tname;
1532 1538          }
1533 1539  
1534 1540          error = spa_create(zc->zc_name, config, props, zplprops);
1535 1541  
1536 1542          /*
1537 1543           * Set the remaining root properties
1538 1544           */
1539      -        if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
     1545 +        if (!error && (error = zfs_set_prop_nvlist(spa_name,
1540 1546              ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1541      -                (void) spa_destroy(zc->zc_name);
     1547 +                (void) spa_destroy(spa_name);
1542 1548  
1543 1549  pool_props_bad:
1544 1550          nvlist_free(rootprops);
1545 1551          nvlist_free(zplprops);
1546 1552          nvlist_free(config);
1547 1553          nvlist_free(props);
1548 1554  
1549 1555          return (error);
1550 1556  }
1551 1557  
1552 1558  static int
1553 1559  zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1554 1560  {
1555 1561          int error;
1556 1562          zfs_log_history(zc);
1557 1563          error = spa_destroy(zc->zc_name);
1558 1564          if (error == 0)
1559 1565                  zvol_remove_minors(zc->zc_name);
1560 1566          return (error);
1561 1567  }
1562 1568  
1563 1569  static int
1564 1570  zfs_ioc_pool_import(zfs_cmd_t *zc)
1565 1571  {
1566 1572          nvlist_t *config, *props = NULL;
1567 1573          uint64_t guid;
1568 1574          int error;
1569 1575  
1570 1576          if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1571 1577              zc->zc_iflags, &config)) != 0)
1572 1578                  return (error);
1573 1579  
1574 1580          if (zc->zc_nvlist_src_size != 0 && (error =
1575 1581              get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1576 1582              zc->zc_iflags, &props))) {
1577 1583                  nvlist_free(config);
1578 1584                  return (error);
1579 1585          }
1580 1586  
1581 1587          if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1582 1588              guid != zc->zc_guid)
1583 1589                  error = SET_ERROR(EINVAL);
1584 1590          else
1585 1591                  error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1586 1592  
1587 1593          if (zc->zc_nvlist_dst != 0) {
1588 1594                  int err;
1589 1595  
1590 1596                  if ((err = put_nvlist(zc, config)) != 0)
1591 1597                          error = err;
1592 1598          }
1593 1599  
1594 1600          nvlist_free(config);
1595 1601  
1596 1602          nvlist_free(props);
1597 1603  
1598 1604          return (error);
1599 1605  }
1600 1606  
1601 1607  static int
1602 1608  zfs_ioc_pool_export(zfs_cmd_t *zc)
1603 1609  {
1604 1610          int error;
1605 1611          boolean_t force = (boolean_t)zc->zc_cookie;
1606 1612          boolean_t hardforce = (boolean_t)zc->zc_guid;
1607 1613  
1608 1614          zfs_log_history(zc);
1609 1615          error = spa_export(zc->zc_name, NULL, force, hardforce);
1610 1616          if (error == 0)
1611 1617                  zvol_remove_minors(zc->zc_name);
1612 1618          return (error);
1613 1619  }
1614 1620  
1615 1621  static int
1616 1622  zfs_ioc_pool_configs(zfs_cmd_t *zc)
1617 1623  {
1618 1624          nvlist_t *configs;
1619 1625          int error;
1620 1626  
1621 1627          if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1622 1628                  return (SET_ERROR(EEXIST));
1623 1629  
1624 1630          error = put_nvlist(zc, configs);
1625 1631  
1626 1632          nvlist_free(configs);
1627 1633  
1628 1634          return (error);
1629 1635  }
1630 1636  
1631 1637  /*
1632 1638   * inputs:
1633 1639   * zc_name              name of the pool
1634 1640   *
1635 1641   * outputs:
1636 1642   * zc_cookie            real errno
1637 1643   * zc_nvlist_dst        config nvlist
1638 1644   * zc_nvlist_dst_size   size of config nvlist
1639 1645   */
1640 1646  static int
1641 1647  zfs_ioc_pool_stats(zfs_cmd_t *zc)
1642 1648  {
1643 1649          nvlist_t *config;
1644 1650          int error;
1645 1651          int ret = 0;
1646 1652  
1647 1653          error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1648 1654              sizeof (zc->zc_value));
1649 1655  
1650 1656          if (config != NULL) {
1651 1657                  ret = put_nvlist(zc, config);
1652 1658                  nvlist_free(config);
1653 1659  
1654 1660                  /*
1655 1661                   * The config may be present even if 'error' is non-zero.
1656 1662                   * In this case we return success, and preserve the real errno
1657 1663                   * in 'zc_cookie'.
1658 1664                   */
1659 1665                  zc->zc_cookie = error;
1660 1666          } else {
1661 1667                  ret = error;
1662 1668          }
1663 1669  
1664 1670          return (ret);
1665 1671  }
1666 1672  
1667 1673  /*
1668 1674   * Try to import the given pool, returning pool stats as appropriate so that
1669 1675   * user land knows which devices are available and overall pool health.
1670 1676   */
1671 1677  static int
1672 1678  zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1673 1679  {
1674 1680          nvlist_t *tryconfig, *config;
1675 1681          int error;
1676 1682  
1677 1683          if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1678 1684              zc->zc_iflags, &tryconfig)) != 0)
1679 1685                  return (error);
1680 1686  
1681 1687          config = spa_tryimport(tryconfig);
1682 1688  
1683 1689          nvlist_free(tryconfig);
1684 1690  
1685 1691          if (config == NULL)
1686 1692                  return (SET_ERROR(EINVAL));
1687 1693  
1688 1694          error = put_nvlist(zc, config);
1689 1695          nvlist_free(config);
1690 1696  
1691 1697          return (error);
1692 1698  }
1693 1699  
1694 1700  /*
1695 1701   * inputs:
1696 1702   * zc_name              name of the pool
1697 1703   * zc_cookie            scan func (pool_scan_func_t)
1698 1704   * zc_flags             scrub pause/resume flag (pool_scrub_cmd_t)
1699 1705   */
1700 1706  static int
1701 1707  zfs_ioc_pool_scan(zfs_cmd_t *zc)
1702 1708  {
1703 1709          spa_t *spa;
1704 1710          int error;
1705 1711  
1706 1712          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1707 1713                  return (error);
1708 1714  
1709 1715          if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1710 1716                  return (SET_ERROR(EINVAL));
1711 1717  
1712 1718          if (zc->zc_flags == POOL_SCRUB_PAUSE)
1713 1719                  error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1714 1720          else if (zc->zc_cookie == POOL_SCAN_NONE)
1715 1721                  error = spa_scan_stop(spa);
1716 1722          else
1717 1723                  error = spa_scan(spa, zc->zc_cookie);
1718 1724  
1719 1725          spa_close(spa, FTAG);
1720 1726  
1721 1727          return (error);
1722 1728  }
1723 1729  
1724 1730  static int
1725 1731  zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1726 1732  {
1727 1733          spa_t *spa;
1728 1734          int error;
1729 1735  
1730 1736          error = spa_open(zc->zc_name, &spa, FTAG);
1731 1737          if (error == 0) {
1732 1738                  spa_freeze(spa);
1733 1739                  spa_close(spa, FTAG);
1734 1740          }
1735 1741          return (error);
1736 1742  }
1737 1743  
1738 1744  static int
1739 1745  zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1740 1746  {
1741 1747          spa_t *spa;
1742 1748          int error;
1743 1749  
1744 1750          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1745 1751                  return (error);
1746 1752  
1747 1753          if (zc->zc_cookie < spa_version(spa) ||
1748 1754              !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1749 1755                  spa_close(spa, FTAG);
1750 1756                  return (SET_ERROR(EINVAL));
1751 1757          }
1752 1758  
1753 1759          spa_upgrade(spa, zc->zc_cookie);
1754 1760          spa_close(spa, FTAG);
1755 1761  
1756 1762          return (error);
1757 1763  }
1758 1764  
1759 1765  static int
1760 1766  zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1761 1767  {
1762 1768          spa_t *spa;
1763 1769          char *hist_buf;
1764 1770          uint64_t size;
1765 1771          int error;
1766 1772  
1767 1773          if ((size = zc->zc_history_len) == 0)
1768 1774                  return (SET_ERROR(EINVAL));
1769 1775  
1770 1776          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1771 1777                  return (error);
1772 1778  
1773 1779          if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1774 1780                  spa_close(spa, FTAG);
1775 1781                  return (SET_ERROR(ENOTSUP));
1776 1782          }
1777 1783  
1778 1784          hist_buf = kmem_alloc(size, KM_SLEEP);
1779 1785          if ((error = spa_history_get(spa, &zc->zc_history_offset,
1780 1786              &zc->zc_history_len, hist_buf)) == 0) {
1781 1787                  error = ddi_copyout(hist_buf,
1782 1788                      (void *)(uintptr_t)zc->zc_history,
1783 1789                      zc->zc_history_len, zc->zc_iflags);
1784 1790          }
1785 1791  
1786 1792          spa_close(spa, FTAG);
1787 1793          kmem_free(hist_buf, size);
1788 1794          return (error);
1789 1795  }
1790 1796  
1791 1797  static int
1792 1798  zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1793 1799  {
1794 1800          spa_t *spa;
1795 1801          int error;
1796 1802  
1797 1803          error = spa_open(zc->zc_name, &spa, FTAG);
1798 1804          if (error == 0) {
1799 1805                  error = spa_change_guid(spa);
1800 1806                  spa_close(spa, FTAG);
1801 1807          }
1802 1808          return (error);
1803 1809  }
1804 1810  
1805 1811  static int
1806 1812  zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1807 1813  {
1808 1814          return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1809 1815  }
1810 1816  
1811 1817  /*
1812 1818   * inputs:
1813 1819   * zc_name              name of filesystem
1814 1820   * zc_obj               object to find
1815 1821   *
1816 1822   * outputs:
1817 1823   * zc_value             name of object
1818 1824   */
1819 1825  static int
1820 1826  zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1821 1827  {
1822 1828          objset_t *os;
1823 1829          int error;
1824 1830  
1825 1831          /* XXX reading from objset not owned */
1826 1832          if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1827 1833                  return (error);
1828 1834          if (dmu_objset_type(os) != DMU_OST_ZFS) {
1829 1835                  dmu_objset_rele(os, FTAG);
1830 1836                  return (SET_ERROR(EINVAL));
1831 1837          }
1832 1838          error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1833 1839              sizeof (zc->zc_value));
1834 1840          dmu_objset_rele(os, FTAG);
1835 1841  
1836 1842          return (error);
1837 1843  }
1838 1844  
1839 1845  /*
1840 1846   * inputs:
1841 1847   * zc_name              name of filesystem
1842 1848   * zc_obj               object to find
1843 1849   *
1844 1850   * outputs:
1845 1851   * zc_stat              stats on object
1846 1852   * zc_value             path to object
1847 1853   */
1848 1854  static int
1849 1855  zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1850 1856  {
1851 1857          objset_t *os;
1852 1858          int error;
1853 1859  
1854 1860          /* XXX reading from objset not owned */
1855 1861          if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1856 1862                  return (error);
1857 1863          if (dmu_objset_type(os) != DMU_OST_ZFS) {
1858 1864                  dmu_objset_rele(os, FTAG);
1859 1865                  return (SET_ERROR(EINVAL));
1860 1866          }
1861 1867          error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1862 1868              sizeof (zc->zc_value));
1863 1869          dmu_objset_rele(os, FTAG);
1864 1870  
1865 1871          return (error);
1866 1872  }
1867 1873  
1868 1874  static int
1869 1875  zfs_ioc_vdev_add(zfs_cmd_t *zc)
1870 1876  {
1871 1877          spa_t *spa;
1872 1878          int error;
1873 1879          nvlist_t *config, **l2cache, **spares;
1874 1880          uint_t nl2cache = 0, nspares = 0;
1875 1881  
1876 1882          error = spa_open(zc->zc_name, &spa, FTAG);
1877 1883          if (error != 0)
1878 1884                  return (error);
1879 1885  
1880 1886          error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1881 1887              zc->zc_iflags, &config);
1882 1888          (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1883 1889              &l2cache, &nl2cache);
1884 1890  
1885 1891          (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1886 1892              &spares, &nspares);
1887 1893  
1888 1894          /*
1889 1895           * A root pool with concatenated devices is not supported.
1890 1896           * Thus, can not add a device to a root pool.
1891 1897           *
1892 1898           * Intent log device can not be added to a rootpool because
1893 1899           * during mountroot, zil is replayed, a seperated log device
1894 1900           * can not be accessed during the mountroot time.
1895 1901           *
1896 1902           * l2cache and spare devices are ok to be added to a rootpool.
1897 1903           */
1898 1904          if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1899 1905                  nvlist_free(config);
1900 1906                  spa_close(spa, FTAG);
1901 1907                  return (SET_ERROR(EDOM));
1902 1908          }
1903 1909  
1904 1910          if (error == 0) {
1905 1911                  error = spa_vdev_add(spa, config);
1906 1912                  nvlist_free(config);
1907 1913          }
1908 1914          spa_close(spa, FTAG);
1909 1915          return (error);
1910 1916  }
1911 1917  
1912 1918  /*
1913 1919   * inputs:
1914 1920   * zc_name              name of the pool
1915 1921   * zc_guid              guid of vdev to remove
1916 1922   * zc_cookie            cancel removal
1917 1923   */
1918 1924  static int
1919 1925  zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1920 1926  {
1921 1927          spa_t *spa;
1922 1928          int error;
1923 1929  
1924 1930          error = spa_open(zc->zc_name, &spa, FTAG);
1925 1931          if (error != 0)
1926 1932                  return (error);
1927 1933          if (zc->zc_cookie != 0) {
1928 1934                  error = spa_vdev_remove_cancel(spa);
1929 1935          } else {
1930 1936                  error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1931 1937          }
1932 1938          spa_close(spa, FTAG);
1933 1939          return (error);
1934 1940  }
1935 1941  
1936 1942  static int
1937 1943  zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1938 1944  {
1939 1945          spa_t *spa;
1940 1946          int error;
1941 1947          vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1942 1948  
1943 1949          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1944 1950                  return (error);
1945 1951          switch (zc->zc_cookie) {
1946 1952          case VDEV_STATE_ONLINE:
1947 1953                  error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1948 1954                  break;
1949 1955  
1950 1956          case VDEV_STATE_OFFLINE:
1951 1957                  error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1952 1958                  break;
1953 1959  
1954 1960          case VDEV_STATE_FAULTED:
1955 1961                  if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1956 1962                      zc->zc_obj != VDEV_AUX_EXTERNAL)
1957 1963                          zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1958 1964  
1959 1965                  error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1960 1966                  break;
1961 1967  
1962 1968          case VDEV_STATE_DEGRADED:
1963 1969                  if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1964 1970                      zc->zc_obj != VDEV_AUX_EXTERNAL)
1965 1971                          zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1966 1972  
1967 1973                  error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1968 1974                  break;
1969 1975  
1970 1976          default:
1971 1977                  error = SET_ERROR(EINVAL);
1972 1978          }
1973 1979          zc->zc_cookie = newstate;
1974 1980          spa_close(spa, FTAG);
1975 1981          return (error);
1976 1982  }
1977 1983  
1978 1984  static int
1979 1985  zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1980 1986  {
1981 1987          spa_t *spa;
1982 1988          int replacing = zc->zc_cookie;
1983 1989          nvlist_t *config;
1984 1990          int error;
1985 1991  
1986 1992          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1987 1993                  return (error);
1988 1994  
1989 1995          if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1990 1996              zc->zc_iflags, &config)) == 0) {
1991 1997                  error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1992 1998                  nvlist_free(config);
1993 1999          }
1994 2000  
1995 2001          spa_close(spa, FTAG);
1996 2002          return (error);
1997 2003  }
1998 2004  
1999 2005  static int
2000 2006  zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2001 2007  {
2002 2008          spa_t *spa;
2003 2009          int error;
2004 2010  
2005 2011          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2006 2012                  return (error);
2007 2013  
2008 2014          error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2009 2015  
2010 2016          spa_close(spa, FTAG);
2011 2017          return (error);
2012 2018  }
2013 2019  
2014 2020  static int
2015 2021  zfs_ioc_vdev_split(zfs_cmd_t *zc)
2016 2022  {
2017 2023          spa_t *spa;
2018 2024          nvlist_t *config, *props = NULL;
2019 2025          int error;
2020 2026          boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2021 2027  
2022 2028          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2023 2029                  return (error);
2024 2030  
2025 2031          if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2026 2032              zc->zc_iflags, &config)) {
2027 2033                  spa_close(spa, FTAG);
2028 2034                  return (error);
2029 2035          }
2030 2036  
2031 2037          if (zc->zc_nvlist_src_size != 0 && (error =
2032 2038              get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2033 2039              zc->zc_iflags, &props))) {
2034 2040                  spa_close(spa, FTAG);
2035 2041                  nvlist_free(config);
2036 2042                  return (error);
2037 2043          }
2038 2044  
2039 2045          error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2040 2046  
2041 2047          spa_close(spa, FTAG);
2042 2048  
2043 2049          nvlist_free(config);
2044 2050          nvlist_free(props);
2045 2051  
2046 2052          return (error);
2047 2053  }
2048 2054  
2049 2055  static int
2050 2056  zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2051 2057  {
2052 2058          spa_t *spa;
2053 2059          char *path = zc->zc_value;
2054 2060          uint64_t guid = zc->zc_guid;
2055 2061          int error;
2056 2062  
2057 2063          error = spa_open(zc->zc_name, &spa, FTAG);
2058 2064          if (error != 0)
2059 2065                  return (error);
2060 2066  
2061 2067          error = spa_vdev_setpath(spa, guid, path);
2062 2068          spa_close(spa, FTAG);
2063 2069          return (error);
2064 2070  }
2065 2071  
2066 2072  static int
2067 2073  zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2068 2074  {
2069 2075          spa_t *spa;
2070 2076          char *fru = zc->zc_value;
2071 2077          uint64_t guid = zc->zc_guid;
2072 2078          int error;
2073 2079  
2074 2080          error = spa_open(zc->zc_name, &spa, FTAG);
2075 2081          if (error != 0)
2076 2082                  return (error);
2077 2083  
2078 2084          error = spa_vdev_setfru(spa, guid, fru);
2079 2085          spa_close(spa, FTAG);
2080 2086          return (error);
2081 2087  }
2082 2088  
2083 2089  static int
2084 2090  zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2085 2091  {
2086 2092          int error = 0;
2087 2093          nvlist_t *nv;
2088 2094  
2089 2095          dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2090 2096  
2091 2097          if (zc->zc_nvlist_dst != 0 &&
2092 2098              (error = dsl_prop_get_all(os, &nv)) == 0) {
2093 2099                  dmu_objset_stats(os, nv);
2094 2100                  /*
2095 2101                   * NB: zvol_get_stats() will read the objset contents,
2096 2102                   * which we aren't supposed to do with a
2097 2103                   * DS_MODE_USER hold, because it could be
2098 2104                   * inconsistent.  So this is a bit of a workaround...
2099 2105                   * XXX reading with out owning
2100 2106                   */
2101 2107                  if (!zc->zc_objset_stats.dds_inconsistent &&
2102 2108                      dmu_objset_type(os) == DMU_OST_ZVOL) {
2103 2109                          error = zvol_get_stats(os, nv);
2104 2110                          if (error == EIO)
2105 2111                                  return (error);
2106 2112                          VERIFY0(error);
2107 2113                  }
2108 2114                  error = put_nvlist(zc, nv);
2109 2115                  nvlist_free(nv);
2110 2116          }
2111 2117  
2112 2118          return (error);
2113 2119  }
2114 2120  
2115 2121  /*
2116 2122   * inputs:
2117 2123   * zc_name              name of filesystem
2118 2124   * zc_nvlist_dst_size   size of buffer for property nvlist
2119 2125   *
2120 2126   * outputs:
2121 2127   * zc_objset_stats      stats
2122 2128   * zc_nvlist_dst        property nvlist
2123 2129   * zc_nvlist_dst_size   size of property nvlist
2124 2130   */
2125 2131  static int
2126 2132  zfs_ioc_objset_stats(zfs_cmd_t *zc)
2127 2133  {
2128 2134          objset_t *os;
2129 2135          int error;
2130 2136  
2131 2137          error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2132 2138          if (error == 0) {
2133 2139                  error = zfs_ioc_objset_stats_impl(zc, os);
2134 2140                  dmu_objset_rele(os, FTAG);
2135 2141          }
2136 2142  
2137 2143          return (error);
2138 2144  }
2139 2145  
2140 2146  /*
2141 2147   * inputs:
2142 2148   * zc_name              name of filesystem
2143 2149   * zc_nvlist_dst_size   size of buffer for property nvlist
2144 2150   *
2145 2151   * outputs:
2146 2152   * zc_nvlist_dst        received property nvlist
2147 2153   * zc_nvlist_dst_size   size of received property nvlist
2148 2154   *
2149 2155   * Gets received properties (distinct from local properties on or after
2150 2156   * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2151 2157   * local property values.
2152 2158   */
2153 2159  static int
2154 2160  zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2155 2161  {
2156 2162          int error = 0;
2157 2163          nvlist_t *nv;
2158 2164  
2159 2165          /*
2160 2166           * Without this check, we would return local property values if the
2161 2167           * caller has not already received properties on or after
2162 2168           * SPA_VERSION_RECVD_PROPS.
2163 2169           */
2164 2170          if (!dsl_prop_get_hasrecvd(zc->zc_name))
2165 2171                  return (SET_ERROR(ENOTSUP));
2166 2172  
2167 2173          if (zc->zc_nvlist_dst != 0 &&
2168 2174              (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2169 2175                  error = put_nvlist(zc, nv);
2170 2176                  nvlist_free(nv);
2171 2177          }
2172 2178  
2173 2179          return (error);
2174 2180  }
2175 2181  
2176 2182  static int
2177 2183  nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2178 2184  {
2179 2185          uint64_t value;
2180 2186          int error;
2181 2187  
2182 2188          /*
2183 2189           * zfs_get_zplprop() will either find a value or give us
2184 2190           * the default value (if there is one).
2185 2191           */
2186 2192          if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2187 2193                  return (error);
2188 2194          VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2189 2195          return (0);
2190 2196  }
2191 2197  
2192 2198  /*
2193 2199   * inputs:
2194 2200   * zc_name              name of filesystem
2195 2201   * zc_nvlist_dst_size   size of buffer for zpl property nvlist
2196 2202   *
2197 2203   * outputs:
2198 2204   * zc_nvlist_dst        zpl property nvlist
2199 2205   * zc_nvlist_dst_size   size of zpl property nvlist
2200 2206   */
2201 2207  static int
2202 2208  zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2203 2209  {
2204 2210          objset_t *os;
2205 2211          int err;
2206 2212  
2207 2213          /* XXX reading without owning */
2208 2214          if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2209 2215                  return (err);
2210 2216  
2211 2217          dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2212 2218  
2213 2219          /*
2214 2220           * NB: nvl_add_zplprop() will read the objset contents,
2215 2221           * which we aren't supposed to do with a DS_MODE_USER
2216 2222           * hold, because it could be inconsistent.
2217 2223           */
2218 2224          if (zc->zc_nvlist_dst != NULL &&
2219 2225              !zc->zc_objset_stats.dds_inconsistent &&
2220 2226              dmu_objset_type(os) == DMU_OST_ZFS) {
2221 2227                  nvlist_t *nv;
2222 2228  
2223 2229                  VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2224 2230                  if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2225 2231                      (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2226 2232                      (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2227 2233                      (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2228 2234                          err = put_nvlist(zc, nv);
2229 2235                  nvlist_free(nv);
2230 2236          } else {
2231 2237                  err = SET_ERROR(ENOENT);
2232 2238          }
2233 2239          dmu_objset_rele(os, FTAG);
2234 2240          return (err);
2235 2241  }
2236 2242  
2237 2243  static boolean_t
2238 2244  dataset_name_hidden(const char *name)
2239 2245  {
2240 2246          /*
2241 2247           * Skip over datasets that are not visible in this zone,
2242 2248           * internal datasets (which have a $ in their name), and
2243 2249           * temporary datasets (which have a % in their name).
2244 2250           */
2245 2251          if (strchr(name, '$') != NULL)
2246 2252                  return (B_TRUE);
2247 2253          if (strchr(name, '%') != NULL)
2248 2254                  return (B_TRUE);
2249 2255          if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2250 2256                  return (B_TRUE);
2251 2257          return (B_FALSE);
2252 2258  }
2253 2259  
2254 2260  /*
2255 2261   * inputs:
2256 2262   * zc_name              name of filesystem
2257 2263   * zc_cookie            zap cursor
2258 2264   * zc_nvlist_dst_size   size of buffer for property nvlist
2259 2265   *
2260 2266   * outputs:
2261 2267   * zc_name              name of next filesystem
2262 2268   * zc_cookie            zap cursor
2263 2269   * zc_objset_stats      stats
2264 2270   * zc_nvlist_dst        property nvlist
2265 2271   * zc_nvlist_dst_size   size of property nvlist
2266 2272   */
2267 2273  static int
2268 2274  zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2269 2275  {
2270 2276          objset_t *os;
2271 2277          int error;
2272 2278          char *p;
2273 2279          size_t orig_len = strlen(zc->zc_name);
2274 2280  
2275 2281  top:
2276 2282          if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2277 2283                  if (error == ENOENT)
2278 2284                          error = SET_ERROR(ESRCH);
2279 2285                  return (error);
2280 2286          }
2281 2287  
2282 2288          p = strrchr(zc->zc_name, '/');
2283 2289          if (p == NULL || p[1] != '\0')
2284 2290                  (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2285 2291          p = zc->zc_name + strlen(zc->zc_name);
2286 2292  
2287 2293          do {
2288 2294                  error = dmu_dir_list_next(os,
2289 2295                      sizeof (zc->zc_name) - (p - zc->zc_name), p,
2290 2296                      NULL, &zc->zc_cookie);
2291 2297                  if (error == ENOENT)
2292 2298                          error = SET_ERROR(ESRCH);
2293 2299          } while (error == 0 && dataset_name_hidden(zc->zc_name));
2294 2300          dmu_objset_rele(os, FTAG);
2295 2301  
2296 2302          /*
2297 2303           * If it's an internal dataset (ie. with a '$' in its name),
2298 2304           * don't try to get stats for it, otherwise we'll return ENOENT.
2299 2305           */
2300 2306          if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2301 2307                  error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2302 2308                  if (error == ENOENT) {
2303 2309                          /* We lost a race with destroy, get the next one. */
2304 2310                          zc->zc_name[orig_len] = '\0';
2305 2311                          goto top;
2306 2312                  }
2307 2313          }
2308 2314          return (error);
2309 2315  }
2310 2316  
2311 2317  /*
2312 2318   * inputs:
2313 2319   * zc_name              name of filesystem
2314 2320   * zc_cookie            zap cursor
2315 2321   * zc_nvlist_dst_size   size of buffer for property nvlist
2316 2322   * zc_simple            when set, only name is requested
2317 2323   *
2318 2324   * outputs:
2319 2325   * zc_name              name of next snapshot
2320 2326   * zc_objset_stats      stats
2321 2327   * zc_nvlist_dst        property nvlist
2322 2328   * zc_nvlist_dst_size   size of property nvlist
2323 2329   */
2324 2330  static int
2325 2331  zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2326 2332  {
2327 2333          objset_t *os;
2328 2334          int error;
2329 2335  
2330 2336          error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2331 2337          if (error != 0) {
2332 2338                  return (error == ENOENT ? ESRCH : error);
2333 2339          }
2334 2340  
2335 2341          /*
2336 2342           * A dataset name of maximum length cannot have any snapshots,
2337 2343           * so exit immediately.
2338 2344           */
2339 2345          if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2340 2346              ZFS_MAX_DATASET_NAME_LEN) {
2341 2347                  dmu_objset_rele(os, FTAG);
2342 2348                  return (SET_ERROR(ESRCH));
2343 2349          }
2344 2350  
2345 2351          error = dmu_snapshot_list_next(os,
2346 2352              sizeof (zc->zc_name) - strlen(zc->zc_name),
2347 2353              zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2348 2354              NULL);
2349 2355  
2350 2356          if (error == 0 && !zc->zc_simple) {
2351 2357                  dsl_dataset_t *ds;
2352 2358                  dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2353 2359  
2354 2360                  error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2355 2361                  if (error == 0) {
2356 2362                          objset_t *ossnap;
2357 2363  
2358 2364                          error = dmu_objset_from_ds(ds, &ossnap);
2359 2365                          if (error == 0)
2360 2366                                  error = zfs_ioc_objset_stats_impl(zc, ossnap);
2361 2367                          dsl_dataset_rele(ds, FTAG);
2362 2368                  }
2363 2369          } else if (error == ENOENT) {
2364 2370                  error = SET_ERROR(ESRCH);
2365 2371          }
2366 2372  
2367 2373          dmu_objset_rele(os, FTAG);
2368 2374          /* if we failed, undo the @ that we tacked on to zc_name */
2369 2375          if (error != 0)
2370 2376                  *strchr(zc->zc_name, '@') = '\0';
2371 2377          return (error);
2372 2378  }
2373 2379  
2374 2380  static int
2375 2381  zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2376 2382  {
2377 2383          const char *propname = nvpair_name(pair);
2378 2384          uint64_t *valary;
2379 2385          unsigned int vallen;
2380 2386          const char *domain;
2381 2387          char *dash;
2382 2388          zfs_userquota_prop_t type;
2383 2389          uint64_t rid;
2384 2390          uint64_t quota;
2385 2391          zfsvfs_t *zfsvfs;
2386 2392          int err;
2387 2393  
2388 2394          if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2389 2395                  nvlist_t *attrs;
2390 2396                  VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2391 2397                  if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2392 2398                      &pair) != 0)
2393 2399                          return (SET_ERROR(EINVAL));
2394 2400          }
2395 2401  
2396 2402          /*
2397 2403           * A correctly constructed propname is encoded as
2398 2404           * userquota@<rid>-<domain>.
2399 2405           */
2400 2406          if ((dash = strchr(propname, '-')) == NULL ||
2401 2407              nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2402 2408              vallen != 3)
2403 2409                  return (SET_ERROR(EINVAL));
2404 2410  
2405 2411          domain = dash + 1;
2406 2412          type = valary[0];
2407 2413          rid = valary[1];
2408 2414          quota = valary[2];
2409 2415  
2410 2416          err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2411 2417          if (err == 0) {
2412 2418                  err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2413 2419                  zfsvfs_rele(zfsvfs, FTAG);
2414 2420          }
2415 2421  
2416 2422          return (err);
2417 2423  }
2418 2424  
2419 2425  /*
2420 2426   * If the named property is one that has a special function to set its value,
2421 2427   * return 0 on success and a positive error code on failure; otherwise if it is
2422 2428   * not one of the special properties handled by this function, return -1.
2423 2429   *
2424 2430   * XXX: It would be better for callers of the property interface if we handled
2425 2431   * these special cases in dsl_prop.c (in the dsl layer).
2426 2432   */
2427 2433  static int
2428 2434  zfs_prop_set_special(const char *dsname, zprop_source_t source,
2429 2435      nvpair_t *pair)
2430 2436  {
2431 2437          const char *propname = nvpair_name(pair);
2432 2438          zfs_prop_t prop = zfs_name_to_prop(propname);
2433 2439          uint64_t intval;
2434 2440          int err = -1;
2435 2441  
2436 2442          if (prop == ZPROP_INVAL) {
2437 2443                  if (zfs_prop_userquota(propname))
2438 2444                          return (zfs_prop_set_userquota(dsname, pair));
2439 2445                  return (-1);
2440 2446          }
2441 2447  
2442 2448          if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2443 2449                  nvlist_t *attrs;
2444 2450                  VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2445 2451                  VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2446 2452                      &pair) == 0);
2447 2453          }
2448 2454  
2449 2455          if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2450 2456                  return (-1);
2451 2457  
2452 2458          VERIFY(0 == nvpair_value_uint64(pair, &intval));
2453 2459  
2454 2460          switch (prop) {
2455 2461          case ZFS_PROP_QUOTA:
2456 2462                  err = dsl_dir_set_quota(dsname, source, intval);
2457 2463                  break;
2458 2464          case ZFS_PROP_REFQUOTA:
2459 2465                  err = dsl_dataset_set_refquota(dsname, source, intval);
2460 2466                  break;
2461 2467          case ZFS_PROP_FILESYSTEM_LIMIT:
2462 2468          case ZFS_PROP_SNAPSHOT_LIMIT:
2463 2469                  if (intval == UINT64_MAX) {
2464 2470                          /* clearing the limit, just do it */
2465 2471                          err = 0;
2466 2472                  } else {
2467 2473                          err = dsl_dir_activate_fs_ss_limit(dsname);
2468 2474                  }
2469 2475                  /*
2470 2476                   * Set err to -1 to force the zfs_set_prop_nvlist code down the
2471 2477                   * default path to set the value in the nvlist.
2472 2478                   */
2473 2479                  if (err == 0)
2474 2480                          err = -1;
2475 2481                  break;
2476 2482          case ZFS_PROP_RESERVATION:
2477 2483                  err = dsl_dir_set_reservation(dsname, source, intval);
2478 2484                  break;
2479 2485          case ZFS_PROP_REFRESERVATION:
2480 2486                  err = dsl_dataset_set_refreservation(dsname, source, intval);
2481 2487                  break;
2482 2488          case ZFS_PROP_VOLSIZE:
2483 2489                  err = zvol_set_volsize(dsname, intval);
2484 2490                  break;
2485 2491          case ZFS_PROP_VERSION:
2486 2492          {
2487 2493                  zfsvfs_t *zfsvfs;
2488 2494  
2489 2495                  if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2490 2496                          break;
2491 2497  
2492 2498                  err = zfs_set_version(zfsvfs, intval);
2493 2499                  zfsvfs_rele(zfsvfs, FTAG);
2494 2500  
2495 2501                  if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2496 2502                          zfs_cmd_t *zc;
2497 2503  
2498 2504                          zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2499 2505                          (void) strcpy(zc->zc_name, dsname);
2500 2506                          (void) zfs_ioc_userspace_upgrade(zc);
2501 2507                          kmem_free(zc, sizeof (zfs_cmd_t));
2502 2508                  }
2503 2509                  break;
2504 2510          }
2505 2511          default:
2506 2512                  err = -1;
2507 2513          }
2508 2514  
2509 2515          return (err);
2510 2516  }
2511 2517  
2512 2518  /*
2513 2519   * This function is best effort. If it fails to set any of the given properties,
2514 2520   * it continues to set as many as it can and returns the last error
2515 2521   * encountered. If the caller provides a non-NULL errlist, it will be filled in
2516 2522   * with the list of names of all the properties that failed along with the
2517 2523   * corresponding error numbers.
2518 2524   *
2519 2525   * If every property is set successfully, zero is returned and errlist is not
2520 2526   * modified.
2521 2527   */
2522 2528  int
2523 2529  zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2524 2530      nvlist_t *errlist)
2525 2531  {
2526 2532          nvpair_t *pair;
2527 2533          nvpair_t *propval;
2528 2534          int rv = 0;
2529 2535          uint64_t intval;
2530 2536          char *strval;
2531 2537          nvlist_t *genericnvl = fnvlist_alloc();
2532 2538          nvlist_t *retrynvl = fnvlist_alloc();
2533 2539  
2534 2540  retry:
2535 2541          pair = NULL;
2536 2542          while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2537 2543                  const char *propname = nvpair_name(pair);
2538 2544                  zfs_prop_t prop = zfs_name_to_prop(propname);
2539 2545                  int err = 0;
2540 2546  
2541 2547                  /* decode the property value */
2542 2548                  propval = pair;
2543 2549                  if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2544 2550                          nvlist_t *attrs;
2545 2551                          attrs = fnvpair_value_nvlist(pair);
2546 2552                          if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2547 2553                              &propval) != 0)
2548 2554                                  err = SET_ERROR(EINVAL);
2549 2555                  }
2550 2556  
2551 2557                  /* Validate value type */
2552 2558                  if (err == 0 && prop == ZPROP_INVAL) {
2553 2559                          if (zfs_prop_user(propname)) {
2554 2560                                  if (nvpair_type(propval) != DATA_TYPE_STRING)
2555 2561                                          err = SET_ERROR(EINVAL);
2556 2562                          } else if (zfs_prop_userquota(propname)) {
2557 2563                                  if (nvpair_type(propval) !=
2558 2564                                      DATA_TYPE_UINT64_ARRAY)
2559 2565                                          err = SET_ERROR(EINVAL);
2560 2566                          } else {
2561 2567                                  err = SET_ERROR(EINVAL);
2562 2568                          }
2563 2569                  } else if (err == 0) {
2564 2570                          if (nvpair_type(propval) == DATA_TYPE_STRING) {
2565 2571                                  if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2566 2572                                          err = SET_ERROR(EINVAL);
2567 2573                          } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2568 2574                                  const char *unused;
2569 2575  
2570 2576                                  intval = fnvpair_value_uint64(propval);
2571 2577  
2572 2578                                  switch (zfs_prop_get_type(prop)) {
2573 2579                                  case PROP_TYPE_NUMBER:
2574 2580                                          break;
2575 2581                                  case PROP_TYPE_STRING:
2576 2582                                          err = SET_ERROR(EINVAL);
2577 2583                                          break;
2578 2584                                  case PROP_TYPE_INDEX:
2579 2585                                          if (zfs_prop_index_to_string(prop,
2580 2586                                              intval, &unused) != 0)
2581 2587                                                  err = SET_ERROR(EINVAL);
2582 2588                                          break;
2583 2589                                  default:
2584 2590                                          cmn_err(CE_PANIC,
2585 2591                                              "unknown property type");
2586 2592                                  }
2587 2593                          } else {
2588 2594                                  err = SET_ERROR(EINVAL);
2589 2595                          }
2590 2596                  }
2591 2597  
2592 2598                  /* Validate permissions */
2593 2599                  if (err == 0)
2594 2600                          err = zfs_check_settable(dsname, pair, CRED());
2595 2601  
2596 2602                  if (err == 0) {
2597 2603                          err = zfs_prop_set_special(dsname, source, pair);
2598 2604                          if (err == -1) {
2599 2605                                  /*
2600 2606                                   * For better performance we build up a list of
2601 2607                                   * properties to set in a single transaction.
2602 2608                                   */
2603 2609                                  err = nvlist_add_nvpair(genericnvl, pair);
2604 2610                          } else if (err != 0 && nvl != retrynvl) {
2605 2611                                  /*
2606 2612                                   * This may be a spurious error caused by
2607 2613                                   * receiving quota and reservation out of order.
2608 2614                                   * Try again in a second pass.
2609 2615                                   */
2610 2616                                  err = nvlist_add_nvpair(retrynvl, pair);
2611 2617                          }
2612 2618                  }
2613 2619  
2614 2620                  if (err != 0) {
2615 2621                          if (errlist != NULL)
2616 2622                                  fnvlist_add_int32(errlist, propname, err);
2617 2623                          rv = err;
2618 2624                  }
2619 2625          }
2620 2626  
2621 2627          if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2622 2628                  nvl = retrynvl;
2623 2629                  goto retry;
2624 2630          }
2625 2631  
2626 2632          if (!nvlist_empty(genericnvl) &&
2627 2633              dsl_props_set(dsname, source, genericnvl) != 0) {
2628 2634                  /*
2629 2635                   * If this fails, we still want to set as many properties as we
2630 2636                   * can, so try setting them individually.
2631 2637                   */
2632 2638                  pair = NULL;
2633 2639                  while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2634 2640                          const char *propname = nvpair_name(pair);
2635 2641                          int err = 0;
2636 2642  
2637 2643                          propval = pair;
2638 2644                          if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2639 2645                                  nvlist_t *attrs;
2640 2646                                  attrs = fnvpair_value_nvlist(pair);
2641 2647                                  propval = fnvlist_lookup_nvpair(attrs,
2642 2648                                      ZPROP_VALUE);
2643 2649                          }
2644 2650  
2645 2651                          if (nvpair_type(propval) == DATA_TYPE_STRING) {
2646 2652                                  strval = fnvpair_value_string(propval);
2647 2653                                  err = dsl_prop_set_string(dsname, propname,
2648 2654                                      source, strval);
2649 2655                          } else {
2650 2656                                  intval = fnvpair_value_uint64(propval);
2651 2657                                  err = dsl_prop_set_int(dsname, propname, source,
2652 2658                                      intval);
2653 2659                          }
2654 2660  
2655 2661                          if (err != 0) {
2656 2662                                  if (errlist != NULL) {
2657 2663                                          fnvlist_add_int32(errlist, propname,
2658 2664                                              err);
2659 2665                                  }
2660 2666                                  rv = err;
2661 2667                          }
2662 2668                  }
2663 2669          }
2664 2670          nvlist_free(genericnvl);
2665 2671          nvlist_free(retrynvl);
2666 2672  
2667 2673          return (rv);
2668 2674  }
2669 2675  
2670 2676  /*
2671 2677   * Check that all the properties are valid user properties.
2672 2678   */
2673 2679  static int
2674 2680  zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2675 2681  {
2676 2682          nvpair_t *pair = NULL;
2677 2683          int error = 0;
2678 2684  
2679 2685          while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2680 2686                  const char *propname = nvpair_name(pair);
2681 2687  
2682 2688                  if (!zfs_prop_user(propname) ||
2683 2689                      nvpair_type(pair) != DATA_TYPE_STRING)
2684 2690                          return (SET_ERROR(EINVAL));
2685 2691  
2686 2692                  if (error = zfs_secpolicy_write_perms(fsname,
2687 2693                      ZFS_DELEG_PERM_USERPROP, CRED()))
2688 2694                          return (error);
2689 2695  
2690 2696                  if (strlen(propname) >= ZAP_MAXNAMELEN)
2691 2697                          return (SET_ERROR(ENAMETOOLONG));
2692 2698  
2693 2699                  if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2694 2700                          return (E2BIG);
2695 2701          }
2696 2702          return (0);
2697 2703  }
2698 2704  
2699 2705  static void
2700 2706  props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2701 2707  {
2702 2708          nvpair_t *pair;
2703 2709  
2704 2710          VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2705 2711  
2706 2712          pair = NULL;
2707 2713          while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2708 2714                  if (nvlist_exists(skipped, nvpair_name(pair)))
2709 2715                          continue;
2710 2716  
2711 2717                  VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2712 2718          }
2713 2719  }
2714 2720  
2715 2721  static int
2716 2722  clear_received_props(const char *dsname, nvlist_t *props,
2717 2723      nvlist_t *skipped)
2718 2724  {
2719 2725          int err = 0;
2720 2726          nvlist_t *cleared_props = NULL;
2721 2727          props_skip(props, skipped, &cleared_props);
2722 2728          if (!nvlist_empty(cleared_props)) {
2723 2729                  /*
2724 2730                   * Acts on local properties until the dataset has received
2725 2731                   * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2726 2732                   */
2727 2733                  zprop_source_t flags = (ZPROP_SRC_NONE |
2728 2734                      (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2729 2735                  err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2730 2736          }
2731 2737          nvlist_free(cleared_props);
2732 2738          return (err);
2733 2739  }
2734 2740  
2735 2741  /*
2736 2742   * inputs:
2737 2743   * zc_name              name of filesystem
2738 2744   * zc_value             name of property to set
2739 2745   * zc_nvlist_src{_size} nvlist of properties to apply
2740 2746   * zc_cookie            received properties flag
2741 2747   *
2742 2748   * outputs:
2743 2749   * zc_nvlist_dst{_size} error for each unapplied received property
2744 2750   */
2745 2751  static int
2746 2752  zfs_ioc_set_prop(zfs_cmd_t *zc)
2747 2753  {
2748 2754          nvlist_t *nvl;
2749 2755          boolean_t received = zc->zc_cookie;
2750 2756          zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2751 2757              ZPROP_SRC_LOCAL);
2752 2758          nvlist_t *errors;
2753 2759          int error;
2754 2760  
2755 2761          if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2756 2762              zc->zc_iflags, &nvl)) != 0)
2757 2763                  return (error);
2758 2764  
2759 2765          if (received) {
2760 2766                  nvlist_t *origprops;
2761 2767  
2762 2768                  if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2763 2769                          (void) clear_received_props(zc->zc_name,
2764 2770                              origprops, nvl);
2765 2771                          nvlist_free(origprops);
2766 2772                  }
2767 2773  
2768 2774                  error = dsl_prop_set_hasrecvd(zc->zc_name);
2769 2775          }
2770 2776  
2771 2777          errors = fnvlist_alloc();
2772 2778          if (error == 0)
2773 2779                  error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2774 2780  
2775 2781          if (zc->zc_nvlist_dst != NULL && errors != NULL) {
2776 2782                  (void) put_nvlist(zc, errors);
2777 2783          }
2778 2784  
2779 2785          nvlist_free(errors);
2780 2786          nvlist_free(nvl);
2781 2787          return (error);
2782 2788  }
2783 2789  
2784 2790  /*
2785 2791   * inputs:
2786 2792   * zc_name              name of filesystem
2787 2793   * zc_value             name of property to inherit
2788 2794   * zc_cookie            revert to received value if TRUE
2789 2795   *
2790 2796   * outputs:             none
2791 2797   */
2792 2798  static int
2793 2799  zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2794 2800  {
2795 2801          const char *propname = zc->zc_value;
2796 2802          zfs_prop_t prop = zfs_name_to_prop(propname);
2797 2803          boolean_t received = zc->zc_cookie;
2798 2804          zprop_source_t source = (received
2799 2805              ? ZPROP_SRC_NONE            /* revert to received value, if any */
2800 2806              : ZPROP_SRC_INHERITED);     /* explicitly inherit */
2801 2807  
2802 2808          if (received) {
2803 2809                  nvlist_t *dummy;
2804 2810                  nvpair_t *pair;
2805 2811                  zprop_type_t type;
2806 2812                  int err;
2807 2813  
2808 2814                  /*
2809 2815                   * zfs_prop_set_special() expects properties in the form of an
2810 2816                   * nvpair with type info.
2811 2817                   */
2812 2818                  if (prop == ZPROP_INVAL) {
2813 2819                          if (!zfs_prop_user(propname))
2814 2820                                  return (SET_ERROR(EINVAL));
2815 2821  
2816 2822                          type = PROP_TYPE_STRING;
2817 2823                  } else if (prop == ZFS_PROP_VOLSIZE ||
2818 2824                      prop == ZFS_PROP_VERSION) {
2819 2825                          return (SET_ERROR(EINVAL));
2820 2826                  } else {
2821 2827                          type = zfs_prop_get_type(prop);
2822 2828                  }
2823 2829  
2824 2830                  VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2825 2831  
2826 2832                  switch (type) {
2827 2833                  case PROP_TYPE_STRING:
2828 2834                          VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2829 2835                          break;
2830 2836                  case PROP_TYPE_NUMBER:
2831 2837                  case PROP_TYPE_INDEX:
2832 2838                          VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2833 2839                          break;
2834 2840                  default:
2835 2841                          nvlist_free(dummy);
2836 2842                          return (SET_ERROR(EINVAL));
2837 2843                  }
2838 2844  
2839 2845                  pair = nvlist_next_nvpair(dummy, NULL);
2840 2846                  err = zfs_prop_set_special(zc->zc_name, source, pair);
2841 2847                  nvlist_free(dummy);
2842 2848                  if (err != -1)
2843 2849                          return (err); /* special property already handled */
2844 2850          } else {
2845 2851                  /*
2846 2852                   * Only check this in the non-received case. We want to allow
2847 2853                   * 'inherit -S' to revert non-inheritable properties like quota
2848 2854                   * and reservation to the received or default values even though
2849 2855                   * they are not considered inheritable.
2850 2856                   */
2851 2857                  if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2852 2858                          return (SET_ERROR(EINVAL));
2853 2859          }
2854 2860  
2855 2861          /* property name has been validated by zfs_secpolicy_inherit_prop() */
2856 2862          return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2857 2863  }
2858 2864  
2859 2865  static int
2860 2866  zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2861 2867  {
2862 2868          nvlist_t *props;
2863 2869          spa_t *spa;
2864 2870          int error;
2865 2871          nvpair_t *pair;
2866 2872  
2867 2873          if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2868 2874              zc->zc_iflags, &props))
2869 2875                  return (error);
2870 2876  
2871 2877          /*
2872 2878           * If the only property is the configfile, then just do a spa_lookup()
2873 2879           * to handle the faulted case.
2874 2880           */
2875 2881          pair = nvlist_next_nvpair(props, NULL);
2876 2882          if (pair != NULL && strcmp(nvpair_name(pair),
2877 2883              zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2878 2884              nvlist_next_nvpair(props, pair) == NULL) {
2879 2885                  mutex_enter(&spa_namespace_lock);
2880 2886                  if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2881 2887                          spa_configfile_set(spa, props, B_FALSE);
2882 2888                          spa_write_cachefile(spa, B_FALSE, B_TRUE);
2883 2889                  }
2884 2890                  mutex_exit(&spa_namespace_lock);
2885 2891                  if (spa != NULL) {
2886 2892                          nvlist_free(props);
2887 2893                          return (0);
2888 2894                  }
2889 2895          }
2890 2896  
2891 2897          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2892 2898                  nvlist_free(props);
2893 2899                  return (error);
2894 2900          }
2895 2901  
2896 2902          error = spa_prop_set(spa, props);
2897 2903  
2898 2904          nvlist_free(props);
2899 2905          spa_close(spa, FTAG);
2900 2906  
2901 2907          return (error);
2902 2908  }
2903 2909  
2904 2910  static int
2905 2911  zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2906 2912  {
2907 2913          spa_t *spa;
2908 2914          int error;
2909 2915          nvlist_t *nvp = NULL;
2910 2916  
2911 2917          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2912 2918                  /*
2913 2919                   * If the pool is faulted, there may be properties we can still
2914 2920                   * get (such as altroot and cachefile), so attempt to get them
2915 2921                   * anyway.
2916 2922                   */
2917 2923                  mutex_enter(&spa_namespace_lock);
2918 2924                  if ((spa = spa_lookup(zc->zc_name)) != NULL)
2919 2925                          error = spa_prop_get(spa, &nvp);
2920 2926                  mutex_exit(&spa_namespace_lock);
2921 2927          } else {
2922 2928                  error = spa_prop_get(spa, &nvp);
2923 2929                  spa_close(spa, FTAG);
2924 2930          }
2925 2931  
2926 2932          if (error == 0 && zc->zc_nvlist_dst != NULL)
2927 2933                  error = put_nvlist(zc, nvp);
2928 2934          else
2929 2935                  error = SET_ERROR(EFAULT);
2930 2936  
2931 2937          nvlist_free(nvp);
2932 2938          return (error);
2933 2939  }
2934 2940  
2935 2941  /*
2936 2942   * inputs:
2937 2943   * zc_name              name of filesystem
2938 2944   * zc_nvlist_src{_size} nvlist of delegated permissions
2939 2945   * zc_perm_action       allow/unallow flag
2940 2946   *
2941 2947   * outputs:             none
2942 2948   */
2943 2949  static int
2944 2950  zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2945 2951  {
2946 2952          int error;
2947 2953          nvlist_t *fsaclnv = NULL;
2948 2954  
2949 2955          if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2950 2956              zc->zc_iflags, &fsaclnv)) != 0)
2951 2957                  return (error);
2952 2958  
2953 2959          /*
2954 2960           * Verify nvlist is constructed correctly
2955 2961           */
2956 2962          if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2957 2963                  nvlist_free(fsaclnv);
2958 2964                  return (SET_ERROR(EINVAL));
2959 2965          }
2960 2966  
2961 2967          /*
2962 2968           * If we don't have PRIV_SYS_MOUNT, then validate
2963 2969           * that user is allowed to hand out each permission in
2964 2970           * the nvlist(s)
2965 2971           */
2966 2972  
2967 2973          error = secpolicy_zfs(CRED());
2968 2974          if (error != 0) {
2969 2975                  if (zc->zc_perm_action == B_FALSE) {
2970 2976                          error = dsl_deleg_can_allow(zc->zc_name,
2971 2977                              fsaclnv, CRED());
2972 2978                  } else {
2973 2979                          error = dsl_deleg_can_unallow(zc->zc_name,
2974 2980                              fsaclnv, CRED());
2975 2981                  }
2976 2982          }
2977 2983  
2978 2984          if (error == 0)
2979 2985                  error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2980 2986  
2981 2987          nvlist_free(fsaclnv);
2982 2988          return (error);
2983 2989  }
2984 2990  
2985 2991  /*
2986 2992   * inputs:
2987 2993   * zc_name              name of filesystem
2988 2994   *
2989 2995   * outputs:
2990 2996   * zc_nvlist_src{_size} nvlist of delegated permissions
2991 2997   */
2992 2998  static int
2993 2999  zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2994 3000  {
2995 3001          nvlist_t *nvp;
2996 3002          int error;
2997 3003  
2998 3004          if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2999 3005                  error = put_nvlist(zc, nvp);
3000 3006                  nvlist_free(nvp);
3001 3007          }
3002 3008  
3003 3009          return (error);
3004 3010  }
3005 3011  
3006 3012  /* ARGSUSED */
3007 3013  static void
3008 3014  zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3009 3015  {
3010 3016          zfs_creat_t *zct = arg;
3011 3017  
3012 3018          zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3013 3019  }
3014 3020  
3015 3021  #define ZFS_PROP_UNDEFINED      ((uint64_t)-1)
3016 3022  
3017 3023  /*
3018 3024   * inputs:
3019 3025   * os                   parent objset pointer (NULL if root fs)
3020 3026   * fuids_ok             fuids allowed in this version of the spa?
3021 3027   * sa_ok                SAs allowed in this version of the spa?
3022 3028   * createprops          list of properties requested by creator
3023 3029   *
3024 3030   * outputs:
3025 3031   * zplprops     values for the zplprops we attach to the master node object
3026 3032   * is_ci        true if requested file system will be purely case-insensitive
3027 3033   *
3028 3034   * Determine the settings for utf8only, normalization and
3029 3035   * casesensitivity.  Specific values may have been requested by the
3030 3036   * creator and/or we can inherit values from the parent dataset.  If
3031 3037   * the file system is of too early a vintage, a creator can not
3032 3038   * request settings for these properties, even if the requested
3033 3039   * setting is the default value.  We don't actually want to create dsl
3034 3040   * properties for these, so remove them from the source nvlist after
3035 3041   * processing.
3036 3042   */
3037 3043  static int
3038 3044  zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3039 3045      boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3040 3046      nvlist_t *zplprops, boolean_t *is_ci)
3041 3047  {
3042 3048          uint64_t sense = ZFS_PROP_UNDEFINED;
3043 3049          uint64_t norm = ZFS_PROP_UNDEFINED;
3044 3050          uint64_t u8 = ZFS_PROP_UNDEFINED;
3045 3051  
3046 3052          ASSERT(zplprops != NULL);
3047 3053  
3048 3054          if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3049 3055                  return (SET_ERROR(EINVAL));
3050 3056  
3051 3057          /*
3052 3058           * Pull out creator prop choices, if any.
3053 3059           */
3054 3060          if (createprops) {
3055 3061                  (void) nvlist_lookup_uint64(createprops,
3056 3062                      zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3057 3063                  (void) nvlist_lookup_uint64(createprops,
3058 3064                      zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3059 3065                  (void) nvlist_remove_all(createprops,
3060 3066                      zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3061 3067                  (void) nvlist_lookup_uint64(createprops,
3062 3068                      zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3063 3069                  (void) nvlist_remove_all(createprops,
3064 3070                      zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3065 3071                  (void) nvlist_lookup_uint64(createprops,
3066 3072                      zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3067 3073                  (void) nvlist_remove_all(createprops,
3068 3074                      zfs_prop_to_name(ZFS_PROP_CASE));
3069 3075          }
3070 3076  
3071 3077          /*
3072 3078           * If the zpl version requested is whacky or the file system
3073 3079           * or pool is version is too "young" to support normalization
3074 3080           * and the creator tried to set a value for one of the props,
3075 3081           * error out.
3076 3082           */
3077 3083          if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3078 3084              (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3079 3085              (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3080 3086              (zplver < ZPL_VERSION_NORMALIZATION &&
3081 3087              (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3082 3088              sense != ZFS_PROP_UNDEFINED)))
3083 3089                  return (SET_ERROR(ENOTSUP));
3084 3090  
3085 3091          /*
3086 3092           * Put the version in the zplprops
3087 3093           */
3088 3094          VERIFY(nvlist_add_uint64(zplprops,
3089 3095              zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3090 3096  
3091 3097          if (norm == ZFS_PROP_UNDEFINED)
3092 3098                  VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3093 3099          VERIFY(nvlist_add_uint64(zplprops,
3094 3100              zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3095 3101  
3096 3102          /*
3097 3103           * If we're normalizing, names must always be valid UTF-8 strings.
3098 3104           */
3099 3105          if (norm)
3100 3106                  u8 = 1;
3101 3107          if (u8 == ZFS_PROP_UNDEFINED)
3102 3108                  VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3103 3109          VERIFY(nvlist_add_uint64(zplprops,
3104 3110              zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3105 3111  
3106 3112          if (sense == ZFS_PROP_UNDEFINED)
3107 3113                  VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3108 3114          VERIFY(nvlist_add_uint64(zplprops,
3109 3115              zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3110 3116  
3111 3117          if (is_ci)
3112 3118                  *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3113 3119  
3114 3120          return (0);
3115 3121  }
3116 3122  
3117 3123  static int
3118 3124  zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3119 3125      nvlist_t *zplprops, boolean_t *is_ci)
3120 3126  {
3121 3127          boolean_t fuids_ok, sa_ok;
3122 3128          uint64_t zplver = ZPL_VERSION;
3123 3129          objset_t *os = NULL;
3124 3130          char parentname[ZFS_MAX_DATASET_NAME_LEN];
3125 3131          char *cp;
3126 3132          spa_t *spa;
3127 3133          uint64_t spa_vers;
3128 3134          int error;
3129 3135  
3130 3136          (void) strlcpy(parentname, dataset, sizeof (parentname));
3131 3137          cp = strrchr(parentname, '/');
3132 3138          ASSERT(cp != NULL);
3133 3139          cp[0] = '\0';
3134 3140  
3135 3141          if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3136 3142                  return (error);
3137 3143  
3138 3144          spa_vers = spa_version(spa);
3139 3145          spa_close(spa, FTAG);
3140 3146  
3141 3147          zplver = zfs_zpl_version_map(spa_vers);
3142 3148          fuids_ok = (zplver >= ZPL_VERSION_FUID);
3143 3149          sa_ok = (zplver >= ZPL_VERSION_SA);
3144 3150  
3145 3151          /*
3146 3152           * Open parent object set so we can inherit zplprop values.
3147 3153           */
3148 3154          if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3149 3155                  return (error);
3150 3156  
3151 3157          error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3152 3158              zplprops, is_ci);
3153 3159          dmu_objset_rele(os, FTAG);
3154 3160          return (error);
3155 3161  }
3156 3162  
3157 3163  static int
3158 3164  zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3159 3165      nvlist_t *zplprops, boolean_t *is_ci)
3160 3166  {
3161 3167          boolean_t fuids_ok;
3162 3168          boolean_t sa_ok;
3163 3169          uint64_t zplver = ZPL_VERSION;
3164 3170          int error;
3165 3171  
3166 3172          zplver = zfs_zpl_version_map(spa_vers);
3167 3173          fuids_ok = (zplver >= ZPL_VERSION_FUID);
3168 3174          sa_ok = (zplver >= ZPL_VERSION_SA);
3169 3175  
3170 3176          error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3171 3177              createprops, zplprops, is_ci);
3172 3178          return (error);
3173 3179  }
3174 3180  
3175 3181  /*
3176 3182   * innvl: {
3177 3183   *     "type" -> dmu_objset_type_t (int32)
3178 3184   *     (optional) "props" -> { prop -> value }
3179 3185   * }
3180 3186   *
3181 3187   * outnvl: propname -> error code (int32)
3182 3188   */
3183 3189  static int
3184 3190  zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3185 3191  {
3186 3192          int error = 0;
3187 3193          zfs_creat_t zct = { 0 };
3188 3194          nvlist_t *nvprops = NULL;
3189 3195          void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3190 3196          int32_t type32;
3191 3197          dmu_objset_type_t type;
3192 3198          boolean_t is_insensitive = B_FALSE;
3193 3199  
3194 3200          if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3195 3201                  return (SET_ERROR(EINVAL));
3196 3202          type = type32;
3197 3203          (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3198 3204  
3199 3205          switch (type) {
3200 3206          case DMU_OST_ZFS:
3201 3207                  cbfunc = zfs_create_cb;
3202 3208                  break;
3203 3209  
3204 3210          case DMU_OST_ZVOL:
3205 3211                  cbfunc = zvol_create_cb;
3206 3212                  break;
3207 3213  
3208 3214          default:
3209 3215                  cbfunc = NULL;
3210 3216                  break;
3211 3217          }
3212 3218          if (strchr(fsname, '@') ||
3213 3219              strchr(fsname, '%'))
3214 3220                  return (SET_ERROR(EINVAL));
3215 3221  
3216 3222          zct.zct_props = nvprops;
3217 3223  
3218 3224          if (cbfunc == NULL)
3219 3225                  return (SET_ERROR(EINVAL));
3220 3226  
3221 3227          if (type == DMU_OST_ZVOL) {
3222 3228                  uint64_t volsize, volblocksize;
3223 3229  
3224 3230                  if (nvprops == NULL)
3225 3231                          return (SET_ERROR(EINVAL));
3226 3232                  if (nvlist_lookup_uint64(nvprops,
3227 3233                      zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3228 3234                          return (SET_ERROR(EINVAL));
3229 3235  
3230 3236                  if ((error = nvlist_lookup_uint64(nvprops,
3231 3237                      zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3232 3238                      &volblocksize)) != 0 && error != ENOENT)
3233 3239                          return (SET_ERROR(EINVAL));
3234 3240  
3235 3241                  if (error != 0)
3236 3242                          volblocksize = zfs_prop_default_numeric(
3237 3243                              ZFS_PROP_VOLBLOCKSIZE);
3238 3244  
3239 3245                  if ((error = zvol_check_volblocksize(
3240 3246                      volblocksize)) != 0 ||
3241 3247                      (error = zvol_check_volsize(volsize,
3242 3248                      volblocksize)) != 0)
3243 3249                          return (error);
3244 3250          } else if (type == DMU_OST_ZFS) {
3245 3251                  int error;
3246 3252  
3247 3253                  /*
3248 3254                   * We have to have normalization and
3249 3255                   * case-folding flags correct when we do the
3250 3256                   * file system creation, so go figure them out
3251 3257                   * now.
3252 3258                   */
3253 3259                  VERIFY(nvlist_alloc(&zct.zct_zplprops,
3254 3260                      NV_UNIQUE_NAME, KM_SLEEP) == 0);
3255 3261                  error = zfs_fill_zplprops(fsname, nvprops,
3256 3262                      zct.zct_zplprops, &is_insensitive);
3257 3263                  if (error != 0) {
3258 3264                          nvlist_free(zct.zct_zplprops);
3259 3265                          return (error);
3260 3266                  }
3261 3267          }
3262 3268  
3263 3269          error = dmu_objset_create(fsname, type,
3264 3270              is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3265 3271          nvlist_free(zct.zct_zplprops);
3266 3272  
3267 3273          /*
3268 3274           * It would be nice to do this atomically.
3269 3275           */
3270 3276          if (error == 0) {
3271 3277                  error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3272 3278                      nvprops, outnvl);
3273 3279                  if (error != 0)
3274 3280                          (void) dsl_destroy_head(fsname);
3275 3281          }
3276 3282          return (error);
3277 3283  }
3278 3284  
3279 3285  /*
3280 3286   * innvl: {
3281 3287   *     "origin" -> name of origin snapshot
3282 3288   *     (optional) "props" -> { prop -> value }
3283 3289   * }
3284 3290   *
3285 3291   * outnvl: propname -> error code (int32)
3286 3292   */
3287 3293  static int
3288 3294  zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3289 3295  {
3290 3296          int error = 0;
3291 3297          nvlist_t *nvprops = NULL;
3292 3298          char *origin_name;
3293 3299  
3294 3300          if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3295 3301                  return (SET_ERROR(EINVAL));
3296 3302          (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3297 3303  
3298 3304          if (strchr(fsname, '@') ||
3299 3305              strchr(fsname, '%'))
3300 3306                  return (SET_ERROR(EINVAL));
3301 3307  
3302 3308          if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3303 3309                  return (SET_ERROR(EINVAL));
3304 3310          error = dmu_objset_clone(fsname, origin_name);
3305 3311          if (error != 0)
3306 3312                  return (error);
3307 3313  
3308 3314          /*
3309 3315           * It would be nice to do this atomically.
3310 3316           */
3311 3317          if (error == 0) {
3312 3318                  error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3313 3319                      nvprops, outnvl);
3314 3320                  if (error != 0)
3315 3321                          (void) dsl_destroy_head(fsname);
3316 3322          }
3317 3323          return (error);
3318 3324  }
3319 3325  
3320 3326  /* ARGSUSED */
3321 3327  static int
3322 3328  zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3323 3329  {
3324 3330          if (strchr(fsname, '@') ||
3325 3331              strchr(fsname, '%'))
3326 3332                  return (SET_ERROR(EINVAL));
3327 3333  
3328 3334          return (dmu_objset_remap_indirects(fsname));
3329 3335  }
3330 3336  
3331 3337  /*
3332 3338   * innvl: {
3333 3339   *     "snaps" -> { snapshot1, snapshot2 }
3334 3340   *     (optional) "props" -> { prop -> value (string) }
3335 3341   * }
3336 3342   *
3337 3343   * outnvl: snapshot -> error code (int32)
3338 3344   */
3339 3345  static int
3340 3346  zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3341 3347  {
3342 3348          nvlist_t *snaps;
3343 3349          nvlist_t *props = NULL;
3344 3350          int error, poollen;
3345 3351          nvpair_t *pair;
3346 3352  
3347 3353          (void) nvlist_lookup_nvlist(innvl, "props", &props);
3348 3354          if ((error = zfs_check_userprops(poolname, props)) != 0)
3349 3355                  return (error);
3350 3356  
3351 3357          if (!nvlist_empty(props) &&
3352 3358              zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3353 3359                  return (SET_ERROR(ENOTSUP));
3354 3360  
3355 3361          if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3356 3362                  return (SET_ERROR(EINVAL));
3357 3363          poollen = strlen(poolname);
3358 3364          for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3359 3365              pair = nvlist_next_nvpair(snaps, pair)) {
3360 3366                  const char *name = nvpair_name(pair);
3361 3367                  const char *cp = strchr(name, '@');
3362 3368  
3363 3369                  /*
3364 3370                   * The snap name must contain an @, and the part after it must
3365 3371                   * contain only valid characters.
3366 3372                   */
3367 3373                  if (cp == NULL ||
3368 3374                      zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3369 3375                          return (SET_ERROR(EINVAL));
3370 3376  
3371 3377                  /*
3372 3378                   * The snap must be in the specified pool.
3373 3379                   */
3374 3380                  if (strncmp(name, poolname, poollen) != 0 ||
3375 3381                      (name[poollen] != '/' && name[poollen] != '@'))
3376 3382                          return (SET_ERROR(EXDEV));
3377 3383  
3378 3384                  /* This must be the only snap of this fs. */
3379 3385                  for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3380 3386                      pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3381 3387                          if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3382 3388                              == 0) {
3383 3389                                  return (SET_ERROR(EXDEV));
3384 3390                          }
3385 3391                  }
3386 3392          }
3387 3393  
3388 3394          error = dsl_dataset_snapshot(snaps, props, outnvl);
3389 3395          return (error);
3390 3396  }
3391 3397  
3392 3398  /*
3393 3399   * innvl: "message" -> string
3394 3400   */
3395 3401  /* ARGSUSED */
3396 3402  static int
3397 3403  zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3398 3404  {
3399 3405          char *message;
3400 3406          spa_t *spa;
3401 3407          int error;
3402 3408          char *poolname;
3403 3409  
3404 3410          /*
3405 3411           * The poolname in the ioctl is not set, we get it from the TSD,
3406 3412           * which was set at the end of the last successful ioctl that allows
3407 3413           * logging.  The secpolicy func already checked that it is set.
3408 3414           * Only one log ioctl is allowed after each successful ioctl, so
3409 3415           * we clear the TSD here.
3410 3416           */
3411 3417          poolname = tsd_get(zfs_allow_log_key);
3412 3418          (void) tsd_set(zfs_allow_log_key, NULL);
3413 3419          error = spa_open(poolname, &spa, FTAG);
3414 3420          strfree(poolname);
3415 3421          if (error != 0)
3416 3422                  return (error);
3417 3423  
3418 3424          if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
3419 3425                  spa_close(spa, FTAG);
3420 3426                  return (SET_ERROR(EINVAL));
3421 3427          }
3422 3428  
3423 3429          if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3424 3430                  spa_close(spa, FTAG);
3425 3431                  return (SET_ERROR(ENOTSUP));
3426 3432          }
3427 3433  
3428 3434          error = spa_history_log(spa, message);
3429 3435          spa_close(spa, FTAG);
3430 3436          return (error);
3431 3437  }
3432 3438  
3433 3439  /*
3434 3440   * The dp_config_rwlock must not be held when calling this, because the
3435 3441   * unmount may need to write out data.
3436 3442   *
3437 3443   * This function is best-effort.  Callers must deal gracefully if it
3438 3444   * remains mounted (or is remounted after this call).
3439 3445   *
3440 3446   * Returns 0 if the argument is not a snapshot, or it is not currently a
3441 3447   * filesystem, or we were able to unmount it.  Returns error code otherwise.
3442 3448   */
3443 3449  void
3444 3450  zfs_unmount_snap(const char *snapname)
3445 3451  {
3446 3452          vfs_t *vfsp = NULL;
3447 3453          zfsvfs_t *zfsvfs = NULL;
3448 3454  
3449 3455          if (strchr(snapname, '@') == NULL)
3450 3456                  return;
3451 3457  
3452 3458          int err = getzfsvfs(snapname, &zfsvfs);
3453 3459          if (err != 0) {
3454 3460                  ASSERT3P(zfsvfs, ==, NULL);
3455 3461                  return;
3456 3462          }
3457 3463          vfsp = zfsvfs->z_vfs;
3458 3464  
3459 3465          ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3460 3466  
3461 3467          err = vn_vfswlock(vfsp->vfs_vnodecovered);
3462 3468          VFS_RELE(vfsp);
3463 3469          if (err != 0)
3464 3470                  return;
3465 3471  
3466 3472          /*
3467 3473           * Always force the unmount for snapshots.
3468 3474           */
3469 3475          (void) dounmount(vfsp, MS_FORCE, kcred);
3470 3476  }
3471 3477  
3472 3478  /* ARGSUSED */
3473 3479  static int
3474 3480  zfs_unmount_snap_cb(const char *snapname, void *arg)
3475 3481  {
3476 3482          zfs_unmount_snap(snapname);
3477 3483          return (0);
3478 3484  }
3479 3485  
3480 3486  /*
3481 3487   * When a clone is destroyed, its origin may also need to be destroyed,
3482 3488   * in which case it must be unmounted.  This routine will do that unmount
3483 3489   * if necessary.
3484 3490   */
3485 3491  void
3486 3492  zfs_destroy_unmount_origin(const char *fsname)
3487 3493  {
3488 3494          int error;
3489 3495          objset_t *os;
3490 3496          dsl_dataset_t *ds;
3491 3497  
3492 3498          error = dmu_objset_hold(fsname, FTAG, &os);
3493 3499          if (error != 0)
3494 3500                  return;
3495 3501          ds = dmu_objset_ds(os);
3496 3502          if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3497 3503                  char originname[ZFS_MAX_DATASET_NAME_LEN];
3498 3504                  dsl_dataset_name(ds->ds_prev, originname);
3499 3505                  dmu_objset_rele(os, FTAG);
3500 3506                  zfs_unmount_snap(originname);
3501 3507          } else {
3502 3508                  dmu_objset_rele(os, FTAG);
3503 3509          }
3504 3510  }
3505 3511  
3506 3512  /*
3507 3513   * innvl: {
3508 3514   *     "snaps" -> { snapshot1, snapshot2 }
3509 3515   *     (optional boolean) "defer"
3510 3516   * }
3511 3517   *
3512 3518   * outnvl: snapshot -> error code (int32)
3513 3519   *
3514 3520   */
3515 3521  /* ARGSUSED */
3516 3522  static int
3517 3523  zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3518 3524  {
3519 3525          nvlist_t *snaps;
3520 3526          nvpair_t *pair;
3521 3527          boolean_t defer;
3522 3528  
3523 3529          if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3524 3530                  return (SET_ERROR(EINVAL));
3525 3531          defer = nvlist_exists(innvl, "defer");
3526 3532  
3527 3533          for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3528 3534              pair = nvlist_next_nvpair(snaps, pair)) {
3529 3535                  zfs_unmount_snap(nvpair_name(pair));
3530 3536          }
3531 3537  
3532 3538          return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3533 3539  }
3534 3540  
3535 3541  /*
3536 3542   * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
3537 3543   * All bookmarks must be in the same pool.
3538 3544   *
3539 3545   * innvl: {
3540 3546   *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
3541 3547   * }
3542 3548   *
3543 3549   * outnvl: bookmark -> error code (int32)
3544 3550   *
3545 3551   */
3546 3552  /* ARGSUSED */
3547 3553  static int
3548 3554  zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3549 3555  {
3550 3556          for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3551 3557              pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3552 3558                  char *snap_name;
3553 3559  
3554 3560                  /*
3555 3561                   * Verify the snapshot argument.
3556 3562                   */
3557 3563                  if (nvpair_value_string(pair, &snap_name) != 0)
3558 3564                          return (SET_ERROR(EINVAL));
3559 3565  
3560 3566  
3561 3567                  /* Verify that the keys (bookmarks) are unique */
3562 3568                  for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3563 3569                      pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3564 3570                          if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3565 3571                                  return (SET_ERROR(EINVAL));
3566 3572                  }
3567 3573          }
3568 3574  
3569 3575          return (dsl_bookmark_create(innvl, outnvl));
3570 3576  }
3571 3577  
3572 3578  /*
3573 3579   * innvl: {
3574 3580   *     property 1, property 2, ...
3575 3581   * }
3576 3582   *
3577 3583   * outnvl: {
3578 3584   *     bookmark name 1 -> { property 1, property 2, ... },
3579 3585   *     bookmark name 2 -> { property 1, property 2, ... }
3580 3586   * }
3581 3587   *
3582 3588   */
3583 3589  static int
3584 3590  zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3585 3591  {
3586 3592          return (dsl_get_bookmarks(fsname, innvl, outnvl));
3587 3593  }
3588 3594  
3589 3595  /*
3590 3596   * innvl: {
3591 3597   *     bookmark name 1, bookmark name 2
3592 3598   * }
3593 3599   *
3594 3600   * outnvl: bookmark -> error code (int32)
3595 3601   *
3596 3602   */
3597 3603  static int
3598 3604  zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3599 3605      nvlist_t *outnvl)
3600 3606  {
3601 3607          int error, poollen;
3602 3608  
3603 3609          poollen = strlen(poolname);
3604 3610          for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3605 3611              pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3606 3612                  const char *name = nvpair_name(pair);
3607 3613                  const char *cp = strchr(name, '#');
3608 3614  
3609 3615                  /*
3610 3616                   * The bookmark name must contain an #, and the part after it
3611 3617                   * must contain only valid characters.
3612 3618                   */
3613 3619                  if (cp == NULL ||
3614 3620                      zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3615 3621                          return (SET_ERROR(EINVAL));
3616 3622  
3617 3623                  /*
3618 3624                   * The bookmark must be in the specified pool.
3619 3625                   */
3620 3626                  if (strncmp(name, poolname, poollen) != 0 ||
3621 3627                      (name[poollen] != '/' && name[poollen] != '#'))
3622 3628                          return (SET_ERROR(EXDEV));
3623 3629          }
3624 3630  
3625 3631          error = dsl_bookmark_destroy(innvl, outnvl);
3626 3632          return (error);
3627 3633  }
3628 3634  
3629 3635  static int
3630 3636  zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3631 3637      nvlist_t *outnvl)
3632 3638  {
3633 3639          char *program;
3634 3640          uint64_t instrlimit, memlimit;
3635 3641          boolean_t sync_flag;
3636 3642          nvpair_t *nvarg = NULL;
3637 3643  
3638 3644          if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3639 3645                  return (EINVAL);
3640 3646          }
3641 3647          if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3642 3648                  sync_flag = B_TRUE;
3643 3649          }
3644 3650          if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3645 3651                  instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3646 3652          }
3647 3653          if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3648 3654                  memlimit = ZCP_DEFAULT_MEMLIMIT;
3649 3655          }
3650 3656          if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3651 3657                  return (EINVAL);
3652 3658          }
3653 3659  
3654 3660          if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3655 3661                  return (EINVAL);
3656 3662          if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3657 3663                  return (EINVAL);
3658 3664  
3659 3665          return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3660 3666              nvarg, outnvl));
3661 3667  }
3662 3668  
3663 3669  /*
3664 3670   * innvl: unused
3665 3671   * outnvl: empty
3666 3672   */
3667 3673  /* ARGSUSED */
3668 3674  static int
3669 3675  zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3670 3676  {
3671 3677          return (spa_checkpoint(poolname));
3672 3678  }
3673 3679  
3674 3680  /*
3675 3681   * innvl: unused
3676 3682   * outnvl: empty
3677 3683   */
3678 3684  /* ARGSUSED */
3679 3685  static int
3680 3686  zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3681 3687      nvlist_t *outnvl)
3682 3688  {
3683 3689          return (spa_checkpoint_discard(poolname));
3684 3690  }
3685 3691  
3686 3692  /*
3687 3693   * inputs:
3688 3694   * zc_name              name of dataset to destroy
3689 3695   * zc_defer_destroy     mark for deferred destroy
3690 3696   *
3691 3697   * outputs:             none
3692 3698   */
3693 3699  static int
3694 3700  zfs_ioc_destroy(zfs_cmd_t *zc)
3695 3701  {
3696 3702          objset_t *os;
3697 3703          dmu_objset_type_t ost;
3698 3704          int err;
3699 3705  
3700 3706          err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3701 3707          if (err != 0)
3702 3708                  return (err);
3703 3709          ost = dmu_objset_type(os);
3704 3710          dmu_objset_rele(os, FTAG);
3705 3711  
3706 3712          if (ost == DMU_OST_ZFS)
3707 3713                  zfs_unmount_snap(zc->zc_name);
3708 3714  
3709 3715          if (strchr(zc->zc_name, '@'))
3710 3716                  err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3711 3717          else
3712 3718                  err = dsl_destroy_head(zc->zc_name);
3713 3719          if (ost == DMU_OST_ZVOL && err == 0)
3714 3720                  (void) zvol_remove_minor(zc->zc_name);
3715 3721          return (err);
3716 3722  }
3717 3723  
3718 3724  /*
3719 3725   * innvl: {
3720 3726   *     vdevs: {
3721 3727   *         guid 1, guid 2, ...
3722 3728   *     },
3723 3729   *     func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3724 3730   * }
3725 3731   *
3726 3732   * outnvl: {
3727 3733   *     [func: EINVAL (if provided command type didn't make sense)],
3728 3734   *     [vdevs: {
3729 3735   *         guid1: errno, (see function body for possible errnos)
3730 3736   *         ...
3731 3737   *     }]
3732 3738   * }
3733 3739   *
3734 3740   */
3735 3741  static int
3736 3742  zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3737 3743  {
3738 3744          spa_t *spa;
3739 3745          int error;
3740 3746  
3741 3747          error = spa_open(poolname, &spa, FTAG);
3742 3748          if (error != 0)
3743 3749                  return (error);
3744 3750  
3745 3751          uint64_t cmd_type;
3746 3752          if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3747 3753              &cmd_type) != 0) {
3748 3754                  spa_close(spa, FTAG);
3749 3755                  return (SET_ERROR(EINVAL));
3750 3756          }
3751 3757          if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3752 3758              cmd_type == POOL_INITIALIZE_DO ||
3753 3759              cmd_type == POOL_INITIALIZE_SUSPEND)) {
3754 3760                  spa_close(spa, FTAG);
3755 3761                  return (SET_ERROR(EINVAL));
3756 3762          }
3757 3763  
3758 3764          nvlist_t *vdev_guids;
3759 3765          if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3760 3766              &vdev_guids) != 0) {
3761 3767                  spa_close(spa, FTAG);
3762 3768                  return (SET_ERROR(EINVAL));
3763 3769          }
3764 3770  
3765 3771          nvlist_t *vdev_errlist = fnvlist_alloc();
3766 3772          int total_errors = 0;
3767 3773  
3768 3774          for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3769 3775              pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3770 3776                  uint64_t vdev_guid = fnvpair_value_uint64(pair);
3771 3777  
3772 3778                  error = spa_vdev_initialize(spa, vdev_guid, cmd_type);
3773 3779                  if (error != 0) {
3774 3780                          char guid_as_str[MAXNAMELEN];
3775 3781  
3776 3782                          (void) snprintf(guid_as_str, sizeof (guid_as_str),
3777 3783                              "%llu", (unsigned long long)vdev_guid);
3778 3784                          fnvlist_add_int64(vdev_errlist, guid_as_str, error);
3779 3785                          total_errors++;
3780 3786                  }
3781 3787          }
3782 3788          if (fnvlist_size(vdev_errlist) > 0) {
3783 3789                  fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3784 3790                      vdev_errlist);
3785 3791          }
3786 3792          fnvlist_free(vdev_errlist);
3787 3793  
3788 3794          spa_close(spa, FTAG);
3789 3795          return (total_errors > 0 ? EINVAL : 0);
3790 3796  }
3791 3797  
3792 3798  /*
3793 3799   * fsname is name of dataset to rollback (to most recent snapshot)
3794 3800   *
3795 3801   * innvl may contain name of expected target snapshot
3796 3802   *
3797 3803   * outnvl: "target" -> name of most recent snapshot
3798 3804   * }
3799 3805   */
3800 3806  /* ARGSUSED */
3801 3807  static int
3802 3808  zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3803 3809  {
3804 3810          zfsvfs_t *zfsvfs;
3805 3811          char *target = NULL;
3806 3812          int error;
3807 3813  
3808 3814          (void) nvlist_lookup_string(innvl, "target", &target);
3809 3815          if (target != NULL) {
3810 3816                  const char *cp = strchr(target, '@');
3811 3817  
3812 3818                  /*
3813 3819                   * The snap name must contain an @, and the part after it must
3814 3820                   * contain only valid characters.
3815 3821                   */
3816 3822                  if (cp == NULL ||
3817 3823                      zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3818 3824                          return (SET_ERROR(EINVAL));
3819 3825          }
3820 3826  
3821 3827          if (getzfsvfs(fsname, &zfsvfs) == 0) {
3822 3828                  dsl_dataset_t *ds;
3823 3829  
3824 3830                  ds = dmu_objset_ds(zfsvfs->z_os);
3825 3831                  error = zfs_suspend_fs(zfsvfs);
3826 3832                  if (error == 0) {
3827 3833                          int resume_err;
3828 3834  
3829 3835                          error = dsl_dataset_rollback(fsname, target, zfsvfs,
3830 3836                              outnvl);
3831 3837                          resume_err = zfs_resume_fs(zfsvfs, ds);
3832 3838                          error = error ? error : resume_err;
3833 3839                  }
3834 3840                  VFS_RELE(zfsvfs->z_vfs);
3835 3841          } else {
3836 3842                  error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3837 3843          }
3838 3844          return (error);
3839 3845  }
3840 3846  
3841 3847  static int
3842 3848  recursive_unmount(const char *fsname, void *arg)
3843 3849  {
3844 3850          const char *snapname = arg;
3845 3851          char fullname[ZFS_MAX_DATASET_NAME_LEN];
3846 3852  
3847 3853          (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3848 3854          zfs_unmount_snap(fullname);
3849 3855  
3850 3856          return (0);
3851 3857  }
3852 3858  
3853 3859  /*
3854 3860   * inputs:
3855 3861   * zc_name      old name of dataset
3856 3862   * zc_value     new name of dataset
3857 3863   * zc_cookie    recursive flag (only valid for snapshots)
3858 3864   *
3859 3865   * outputs:     none
3860 3866   */
3861 3867  static int
3862 3868  zfs_ioc_rename(zfs_cmd_t *zc)
3863 3869  {
3864 3870          objset_t *os;
3865 3871          dmu_objset_type_t ost;
3866 3872          boolean_t recursive = zc->zc_cookie & 1;
3867 3873          char *at;
3868 3874          int err;
3869 3875  
3870 3876          /* "zfs rename" from and to ...%recv datasets should both fail */
3871 3877          zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3872 3878          zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3873 3879          if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3874 3880              dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3875 3881              strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3876 3882                  return (SET_ERROR(EINVAL));
3877 3883  
3878 3884          err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3879 3885          if (err != 0)
3880 3886                  return (err);
3881 3887          ost = dmu_objset_type(os);
3882 3888          dmu_objset_rele(os, FTAG);
3883 3889  
3884 3890          at = strchr(zc->zc_name, '@');
3885 3891          if (at != NULL) {
3886 3892                  /* snaps must be in same fs */
3887 3893                  int error;
3888 3894  
3889 3895                  if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3890 3896                          return (SET_ERROR(EXDEV));
3891 3897                  *at = '\0';
3892 3898                  if (ost == DMU_OST_ZFS) {
3893 3899                          error = dmu_objset_find(zc->zc_name,
3894 3900                              recursive_unmount, at + 1,
3895 3901                              recursive ? DS_FIND_CHILDREN : 0);
3896 3902                          if (error != 0) {
3897 3903                                  *at = '@';
3898 3904                                  return (error);
3899 3905                          }
3900 3906                  }
3901 3907                  error = dsl_dataset_rename_snapshot(zc->zc_name,
3902 3908                      at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3903 3909                  *at = '@';
3904 3910  
3905 3911                  return (error);
3906 3912          } else {
3907 3913                  if (ost == DMU_OST_ZVOL)
3908 3914                          (void) zvol_remove_minor(zc->zc_name);
3909 3915                  return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3910 3916          }
3911 3917  }
3912 3918  
3913 3919  static int
3914 3920  zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3915 3921  {
3916 3922          const char *propname = nvpair_name(pair);
3917 3923          boolean_t issnap = (strchr(dsname, '@') != NULL);
3918 3924          zfs_prop_t prop = zfs_name_to_prop(propname);
3919 3925          uint64_t intval;
3920 3926          int err;
3921 3927  
3922 3928          if (prop == ZPROP_INVAL) {
3923 3929                  if (zfs_prop_user(propname)) {
3924 3930                          if (err = zfs_secpolicy_write_perms(dsname,
3925 3931                              ZFS_DELEG_PERM_USERPROP, cr))
3926 3932                                  return (err);
3927 3933                          return (0);
3928 3934                  }
3929 3935  
3930 3936                  if (!issnap && zfs_prop_userquota(propname)) {
3931 3937                          const char *perm = NULL;
3932 3938                          const char *uq_prefix =
3933 3939                              zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3934 3940                          const char *gq_prefix =
3935 3941                              zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3936 3942  
3937 3943                          if (strncmp(propname, uq_prefix,
3938 3944                              strlen(uq_prefix)) == 0) {
3939 3945                                  perm = ZFS_DELEG_PERM_USERQUOTA;
3940 3946                          } else if (strncmp(propname, gq_prefix,
3941 3947                              strlen(gq_prefix)) == 0) {
3942 3948                                  perm = ZFS_DELEG_PERM_GROUPQUOTA;
3943 3949                          } else {
3944 3950                                  /* USERUSED and GROUPUSED are read-only */
3945 3951                                  return (SET_ERROR(EINVAL));
3946 3952                          }
3947 3953  
3948 3954                          if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3949 3955                                  return (err);
3950 3956                          return (0);
3951 3957                  }
3952 3958  
3953 3959                  return (SET_ERROR(EINVAL));
3954 3960          }
3955 3961  
3956 3962          if (issnap)
3957 3963                  return (SET_ERROR(EINVAL));
3958 3964  
3959 3965          if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3960 3966                  /*
3961 3967                   * dsl_prop_get_all_impl() returns properties in this
3962 3968                   * format.
3963 3969                   */
3964 3970                  nvlist_t *attrs;
3965 3971                  VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3966 3972                  VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3967 3973                      &pair) == 0);
3968 3974          }
3969 3975  
3970 3976          /*
3971 3977           * Check that this value is valid for this pool version
3972 3978           */
3973 3979          switch (prop) {
3974 3980          case ZFS_PROP_COMPRESSION:
3975 3981                  /*
3976 3982                   * If the user specified gzip compression, make sure
3977 3983                   * the SPA supports it. We ignore any errors here since
3978 3984                   * we'll catch them later.
3979 3985                   */
3980 3986                  if (nvpair_value_uint64(pair, &intval) == 0) {
3981 3987                          if (intval >= ZIO_COMPRESS_GZIP_1 &&
3982 3988                              intval <= ZIO_COMPRESS_GZIP_9 &&
3983 3989                              zfs_earlier_version(dsname,
3984 3990                              SPA_VERSION_GZIP_COMPRESSION)) {
3985 3991                                  return (SET_ERROR(ENOTSUP));
3986 3992                          }
3987 3993  
3988 3994                          if (intval == ZIO_COMPRESS_ZLE &&
3989 3995                              zfs_earlier_version(dsname,
3990 3996                              SPA_VERSION_ZLE_COMPRESSION))
3991 3997                                  return (SET_ERROR(ENOTSUP));
3992 3998  
3993 3999                          if (intval == ZIO_COMPRESS_LZ4) {
3994 4000                                  spa_t *spa;
3995 4001  
3996 4002                                  if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3997 4003                                          return (err);
3998 4004  
3999 4005                                  if (!spa_feature_is_enabled(spa,
4000 4006                                      SPA_FEATURE_LZ4_COMPRESS)) {
4001 4007                                          spa_close(spa, FTAG);
4002 4008                                          return (SET_ERROR(ENOTSUP));
4003 4009                                  }
4004 4010                                  spa_close(spa, FTAG);
4005 4011                          }
4006 4012  
4007 4013                          /*
4008 4014                           * If this is a bootable dataset then
4009 4015                           * verify that the compression algorithm
4010 4016                           * is supported for booting. We must return
4011 4017                           * something other than ENOTSUP since it
4012 4018                           * implies a downrev pool version.
4013 4019                           */
4014 4020                          if (zfs_is_bootfs(dsname) &&
4015 4021                              !BOOTFS_COMPRESS_VALID(intval)) {
4016 4022                                  return (SET_ERROR(ERANGE));
4017 4023                          }
4018 4024                  }
4019 4025                  break;
4020 4026  
4021 4027          case ZFS_PROP_COPIES:
4022 4028                  if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4023 4029                          return (SET_ERROR(ENOTSUP));
4024 4030                  break;
4025 4031  
4026 4032          case ZFS_PROP_RECORDSIZE:
4027 4033                  /* Record sizes above 128k need the feature to be enabled */
4028 4034                  if (nvpair_value_uint64(pair, &intval) == 0 &&
4029 4035                      intval > SPA_OLD_MAXBLOCKSIZE) {
4030 4036                          spa_t *spa;
4031 4037  
4032 4038                          /*
4033 4039                           * We don't allow setting the property above 1MB,
4034 4040                           * unless the tunable has been changed.
4035 4041                           */
4036 4042                          if (intval > zfs_max_recordsize ||
4037 4043                              intval > SPA_MAXBLOCKSIZE)
4038 4044                                  return (SET_ERROR(ERANGE));
4039 4045  
4040 4046                          if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4041 4047                                  return (err);
4042 4048  
4043 4049                          if (!spa_feature_is_enabled(spa,
4044 4050                              SPA_FEATURE_LARGE_BLOCKS)) {
4045 4051                                  spa_close(spa, FTAG);
4046 4052                                  return (SET_ERROR(ENOTSUP));
4047 4053                          }
4048 4054                          spa_close(spa, FTAG);
4049 4055                  }
4050 4056                  break;
4051 4057  
4052 4058          case ZFS_PROP_SHARESMB:
4053 4059                  if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4054 4060                          return (SET_ERROR(ENOTSUP));
4055 4061                  break;
4056 4062  
4057 4063          case ZFS_PROP_ACLINHERIT:
4058 4064                  if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4059 4065                      nvpair_value_uint64(pair, &intval) == 0) {
4060 4066                          if (intval == ZFS_ACL_PASSTHROUGH_X &&
4061 4067                              zfs_earlier_version(dsname,
4062 4068                              SPA_VERSION_PASSTHROUGH_X))
4063 4069                                  return (SET_ERROR(ENOTSUP));
4064 4070                  }
4065 4071                  break;
4066 4072  
4067 4073          case ZFS_PROP_CHECKSUM:
4068 4074          case ZFS_PROP_DEDUP:
4069 4075          {
4070 4076                  spa_feature_t feature;
4071 4077                  spa_t *spa;
4072 4078  
4073 4079                  /* dedup feature version checks */
4074 4080                  if (prop == ZFS_PROP_DEDUP &&
4075 4081                      zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4076 4082                          return (SET_ERROR(ENOTSUP));
4077 4083  
4078 4084                  if (nvpair_value_uint64(pair, &intval) != 0)
4079 4085                          return (SET_ERROR(EINVAL));
4080 4086  
4081 4087                  /* check prop value is enabled in features */
4082 4088                  feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4083 4089                  if (feature == SPA_FEATURE_NONE)
4084 4090                          break;
4085 4091  
4086 4092                  if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4087 4093                          return (err);
4088 4094  
4089 4095                  if (!spa_feature_is_enabled(spa, feature)) {
4090 4096                          spa_close(spa, FTAG);
4091 4097                          return (SET_ERROR(ENOTSUP));
4092 4098                  }
4093 4099                  spa_close(spa, FTAG);
4094 4100                  break;
4095 4101          }
4096 4102          }
4097 4103  
4098 4104          return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4099 4105  }
4100 4106  
4101 4107  /*
4102 4108   * Checks for a race condition to make sure we don't increment a feature flag
4103 4109   * multiple times.
4104 4110   */
4105 4111  static int
4106 4112  zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4107 4113  {
4108 4114          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4109 4115          spa_feature_t *featurep = arg;
4110 4116  
4111 4117          if (!spa_feature_is_active(spa, *featurep))
4112 4118                  return (0);
4113 4119          else
4114 4120                  return (SET_ERROR(EBUSY));
4115 4121  }
4116 4122  
4117 4123  /*
4118 4124   * The callback invoked on feature activation in the sync task caused by
4119 4125   * zfs_prop_activate_feature.
4120 4126   */
4121 4127  static void
4122 4128  zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4123 4129  {
4124 4130          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4125 4131          spa_feature_t *featurep = arg;
4126 4132  
4127 4133          spa_feature_incr(spa, *featurep, tx);
4128 4134  }
4129 4135  
4130 4136  /*
4131 4137   * Activates a feature on a pool in response to a property setting. This
4132 4138   * creates a new sync task which modifies the pool to reflect the feature
4133 4139   * as being active.
4134 4140   */
4135 4141  static int
4136 4142  zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4137 4143  {
4138 4144          int err;
4139 4145  
4140 4146          /* EBUSY here indicates that the feature is already active */
4141 4147          err = dsl_sync_task(spa_name(spa),
4142 4148              zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4143 4149              &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4144 4150  
4145 4151          if (err != 0 && err != EBUSY)
4146 4152                  return (err);
4147 4153          else
4148 4154                  return (0);
4149 4155  }
4150 4156  
4151 4157  /*
4152 4158   * Removes properties from the given props list that fail permission checks
4153 4159   * needed to clear them and to restore them in case of a receive error. For each
4154 4160   * property, make sure we have both set and inherit permissions.
4155 4161   *
4156 4162   * Returns the first error encountered if any permission checks fail. If the
4157 4163   * caller provides a non-NULL errlist, it also gives the complete list of names
4158 4164   * of all the properties that failed a permission check along with the
4159 4165   * corresponding error numbers. The caller is responsible for freeing the
4160 4166   * returned errlist.
4161 4167   *
4162 4168   * If every property checks out successfully, zero is returned and the list
4163 4169   * pointed at by errlist is NULL.
4164 4170   */
4165 4171  static int
4166 4172  zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4167 4173  {
4168 4174          zfs_cmd_t *zc;
4169 4175          nvpair_t *pair, *next_pair;
4170 4176          nvlist_t *errors;
4171 4177          int err, rv = 0;
4172 4178  
4173 4179          if (props == NULL)
4174 4180                  return (0);
4175 4181  
4176 4182          VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4177 4183  
4178 4184          zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4179 4185          (void) strcpy(zc->zc_name, dataset);
4180 4186          pair = nvlist_next_nvpair(props, NULL);
4181 4187          while (pair != NULL) {
4182 4188                  next_pair = nvlist_next_nvpair(props, pair);
4183 4189  
4184 4190                  (void) strcpy(zc->zc_value, nvpair_name(pair));
4185 4191                  if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4186 4192                      (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4187 4193                          VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4188 4194                          VERIFY(nvlist_add_int32(errors,
4189 4195                              zc->zc_value, err) == 0);
4190 4196                  }
4191 4197                  pair = next_pair;
4192 4198          }
4193 4199          kmem_free(zc, sizeof (zfs_cmd_t));
4194 4200  
4195 4201          if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4196 4202                  nvlist_free(errors);
4197 4203                  errors = NULL;
4198 4204          } else {
4199 4205                  VERIFY(nvpair_value_int32(pair, &rv) == 0);
4200 4206          }
4201 4207  
4202 4208          if (errlist == NULL)
4203 4209                  nvlist_free(errors);
4204 4210          else
4205 4211                  *errlist = errors;
4206 4212  
4207 4213          return (rv);
4208 4214  }
4209 4215  
4210 4216  static boolean_t
4211 4217  propval_equals(nvpair_t *p1, nvpair_t *p2)
4212 4218  {
4213 4219          if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4214 4220                  /* dsl_prop_get_all_impl() format */
4215 4221                  nvlist_t *attrs;
4216 4222                  VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4217 4223                  VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4218 4224                      &p1) == 0);
4219 4225          }
4220 4226  
4221 4227          if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4222 4228                  nvlist_t *attrs;
4223 4229                  VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4224 4230                  VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4225 4231                      &p2) == 0);
4226 4232          }
4227 4233  
4228 4234          if (nvpair_type(p1) != nvpair_type(p2))
4229 4235                  return (B_FALSE);
4230 4236  
4231 4237          if (nvpair_type(p1) == DATA_TYPE_STRING) {
4232 4238                  char *valstr1, *valstr2;
4233 4239  
4234 4240                  VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4235 4241                  VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4236 4242                  return (strcmp(valstr1, valstr2) == 0);
4237 4243          } else {
4238 4244                  uint64_t intval1, intval2;
4239 4245  
4240 4246                  VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4241 4247                  VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4242 4248                  return (intval1 == intval2);
4243 4249          }
4244 4250  }
4245 4251  
4246 4252  /*
4247 4253   * Remove properties from props if they are not going to change (as determined
4248 4254   * by comparison with origprops). Remove them from origprops as well, since we
4249 4255   * do not need to clear or restore properties that won't change.
4250 4256   */
4251 4257  static void
4252 4258  props_reduce(nvlist_t *props, nvlist_t *origprops)
4253 4259  {
4254 4260          nvpair_t *pair, *next_pair;
4255 4261  
4256 4262          if (origprops == NULL)
4257 4263                  return; /* all props need to be received */
4258 4264  
4259 4265          pair = nvlist_next_nvpair(props, NULL);
4260 4266          while (pair != NULL) {
4261 4267                  const char *propname = nvpair_name(pair);
4262 4268                  nvpair_t *match;
4263 4269  
4264 4270                  next_pair = nvlist_next_nvpair(props, pair);
4265 4271  
4266 4272                  if ((nvlist_lookup_nvpair(origprops, propname,
4267 4273                      &match) != 0) || !propval_equals(pair, match))
4268 4274                          goto next; /* need to set received value */
4269 4275  
4270 4276                  /* don't clear the existing received value */
4271 4277                  (void) nvlist_remove_nvpair(origprops, match);
4272 4278                  /* don't bother receiving the property */
4273 4279                  (void) nvlist_remove_nvpair(props, pair);
4274 4280  next:
4275 4281                  pair = next_pair;
4276 4282          }
4277 4283  }
4278 4284  
4279 4285  /*
4280 4286   * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4281 4287   * For example, refquota cannot be set until after the receipt of a dataset,
4282 4288   * because in replication streams, an older/earlier snapshot may exceed the
4283 4289   * refquota.  We want to receive the older/earlier snapshot, but setting
4284 4290   * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4285 4291   * the older/earlier snapshot from being received (with EDQUOT).
4286 4292   *
4287 4293   * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4288 4294   *
4289 4295   * libzfs will need to be judicious handling errors encountered by props
4290 4296   * extracted by this function.
4291 4297   */
4292 4298  static nvlist_t *
4293 4299  extract_delay_props(nvlist_t *props)
4294 4300  {
4295 4301          nvlist_t *delayprops;
4296 4302          nvpair_t *nvp, *tmp;
4297 4303          static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4298 4304          int i;
4299 4305  
4300 4306          VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4301 4307  
4302 4308          for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4303 4309              nvp = nvlist_next_nvpair(props, nvp)) {
4304 4310                  /*
4305 4311                   * strcmp() is safe because zfs_prop_to_name() always returns
4306 4312                   * a bounded string.
4307 4313                   */
4308 4314                  for (i = 0; delayable[i] != 0; i++) {
4309 4315                          if (strcmp(zfs_prop_to_name(delayable[i]),
4310 4316                              nvpair_name(nvp)) == 0) {
4311 4317                                  break;
4312 4318                          }
4313 4319                  }
4314 4320                  if (delayable[i] != 0) {
4315 4321                          tmp = nvlist_prev_nvpair(props, nvp);
4316 4322                          VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4317 4323                          VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4318 4324                          nvp = tmp;
4319 4325                  }
4320 4326          }
4321 4327  
4322 4328          if (nvlist_empty(delayprops)) {
4323 4329                  nvlist_free(delayprops);
4324 4330                  delayprops = NULL;
4325 4331          }
4326 4332          return (delayprops);
4327 4333  }
4328 4334  
4329 4335  #ifdef  DEBUG
4330 4336  static boolean_t zfs_ioc_recv_inject_err;
4331 4337  #endif
4332 4338  
4333 4339  /*
4334 4340   * inputs:
4335 4341   * zc_name              name of containing filesystem
4336 4342   * zc_nvlist_src{_size} nvlist of properties to apply
4337 4343   * zc_value             name of snapshot to create
4338 4344   * zc_string            name of clone origin (if DRR_FLAG_CLONE)
4339 4345   * zc_cookie            file descriptor to recv from
4340 4346   * zc_begin_record      the BEGIN record of the stream (not byteswapped)
4341 4347   * zc_guid              force flag
4342 4348   * zc_cleanup_fd        cleanup-on-exit file descriptor
4343 4349   * zc_action_handle     handle for this guid/ds mapping (or zero on first call)
4344 4350   * zc_resumable         if data is incomplete assume sender will resume
4345 4351   *
4346 4352   * outputs:
4347 4353   * zc_cookie            number of bytes read
4348 4354   * zc_nvlist_dst{_size} error for each unapplied received property
4349 4355   * zc_obj               zprop_errflags_t
4350 4356   * zc_action_handle     handle for this guid/ds mapping
4351 4357   */
4352 4358  static int
4353 4359  zfs_ioc_recv(zfs_cmd_t *zc)
4354 4360  {
4355 4361          file_t *fp;
4356 4362          dmu_recv_cookie_t drc;
4357 4363          boolean_t force = (boolean_t)zc->zc_guid;
4358 4364          int fd;
4359 4365          int error = 0;
4360 4366          int props_error = 0;
4361 4367          nvlist_t *errors;
4362 4368          offset_t off;
4363 4369          nvlist_t *props = NULL; /* sent properties */
4364 4370          nvlist_t *origprops = NULL; /* existing properties */
4365 4371          nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4366 4372          char *origin = NULL;
4367 4373          char *tosnap;
4368 4374          char tofs[ZFS_MAX_DATASET_NAME_LEN];
4369 4375          boolean_t first_recvd_props = B_FALSE;
4370 4376  
4371 4377          if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4372 4378              strchr(zc->zc_value, '@') == NULL ||
4373 4379              strchr(zc->zc_value, '%'))
4374 4380                  return (SET_ERROR(EINVAL));
4375 4381  
4376 4382          (void) strcpy(tofs, zc->zc_value);
4377 4383          tosnap = strchr(tofs, '@');
4378 4384          *tosnap++ = '\0';
4379 4385  
4380 4386          if (zc->zc_nvlist_src != NULL &&
4381 4387              (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4382 4388              zc->zc_iflags, &props)) != 0)
4383 4389                  return (error);
4384 4390  
4385 4391          fd = zc->zc_cookie;
4386 4392          fp = getf(fd);
4387 4393          if (fp == NULL) {
4388 4394                  nvlist_free(props);
4389 4395                  return (SET_ERROR(EBADF));
4390 4396          }
4391 4397  
4392 4398          errors = fnvlist_alloc();
4393 4399  
4394 4400          if (zc->zc_string[0])
4395 4401                  origin = zc->zc_string;
4396 4402  
4397 4403          error = dmu_recv_begin(tofs, tosnap,
4398 4404              &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4399 4405          if (error != 0)
4400 4406                  goto out;
4401 4407  
4402 4408          /*
4403 4409           * Set properties before we receive the stream so that they are applied
4404 4410           * to the new data. Note that we must call dmu_recv_stream() if
4405 4411           * dmu_recv_begin() succeeds.
4406 4412           */
4407 4413          if (props != NULL && !drc.drc_newfs) {
4408 4414                  if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4409 4415                      SPA_VERSION_RECVD_PROPS &&
4410 4416                      !dsl_prop_get_hasrecvd(tofs))
4411 4417                          first_recvd_props = B_TRUE;
4412 4418  
4413 4419                  /*
4414 4420                   * If new received properties are supplied, they are to
4415 4421                   * completely replace the existing received properties, so stash
4416 4422                   * away the existing ones.
4417 4423                   */
4418 4424                  if (dsl_prop_get_received(tofs, &origprops) == 0) {
4419 4425                          nvlist_t *errlist = NULL;
4420 4426                          /*
4421 4427                           * Don't bother writing a property if its value won't
4422 4428                           * change (and avoid the unnecessary security checks).
4423 4429                           *
4424 4430                           * The first receive after SPA_VERSION_RECVD_PROPS is a
4425 4431                           * special case where we blow away all local properties
4426 4432                           * regardless.
4427 4433                           */
4428 4434                          if (!first_recvd_props)
4429 4435                                  props_reduce(props, origprops);
4430 4436                          if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4431 4437                                  (void) nvlist_merge(errors, errlist, 0);
4432 4438                          nvlist_free(errlist);
4433 4439  
4434 4440                          if (clear_received_props(tofs, origprops,
4435 4441                              first_recvd_props ? NULL : props) != 0)
4436 4442                                  zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4437 4443                  } else {
4438 4444                          zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4439 4445                  }
4440 4446          }
4441 4447  
4442 4448          if (props != NULL) {
4443 4449                  props_error = dsl_prop_set_hasrecvd(tofs);
4444 4450  
4445 4451                  if (props_error == 0) {
4446 4452                          delayprops = extract_delay_props(props);
4447 4453                          (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4448 4454                              props, errors);
4449 4455                  }
4450 4456          }
4451 4457  
4452 4458          off = fp->f_offset;
4453 4459          error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4454 4460              &zc->zc_action_handle);
4455 4461  
4456 4462          if (error == 0) {
4457 4463                  zfsvfs_t *zfsvfs = NULL;
4458 4464  
4459 4465                  if (getzfsvfs(tofs, &zfsvfs) == 0) {
4460 4466                          /* online recv */
4461 4467                          dsl_dataset_t *ds;
4462 4468                          int end_err;
4463 4469  
4464 4470                          ds = dmu_objset_ds(zfsvfs->z_os);
4465 4471                          error = zfs_suspend_fs(zfsvfs);
4466 4472                          /*
4467 4473                           * If the suspend fails, then the recv_end will
4468 4474                           * likely also fail, and clean up after itself.
4469 4475                           */
4470 4476                          end_err = dmu_recv_end(&drc, zfsvfs);
4471 4477                          if (error == 0)
4472 4478                                  error = zfs_resume_fs(zfsvfs, ds);
4473 4479                          error = error ? error : end_err;
4474 4480                          VFS_RELE(zfsvfs->z_vfs);
4475 4481                  } else {
4476 4482                          error = dmu_recv_end(&drc, NULL);
4477 4483                  }
4478 4484  
4479 4485                  /* Set delayed properties now, after we're done receiving. */
4480 4486                  if (delayprops != NULL && error == 0) {
4481 4487                          (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4482 4488                              delayprops, errors);
4483 4489                  }
4484 4490          }
4485 4491  
4486 4492          if (delayprops != NULL) {
4487 4493                  /*
4488 4494                   * Merge delayed props back in with initial props, in case
4489 4495                   * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4490 4496                   * we have to make sure clear_received_props() includes
4491 4497                   * the delayed properties).
4492 4498                   *
4493 4499                   * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4494 4500                   * using ASSERT() will be just like a VERIFY.
4495 4501                   */
4496 4502                  ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4497 4503                  nvlist_free(delayprops);
4498 4504          }
4499 4505  
4500 4506          /*
4501 4507           * Now that all props, initial and delayed, are set, report the prop
4502 4508           * errors to the caller.
4503 4509           */
4504 4510          if (zc->zc_nvlist_dst_size != 0 &&
4505 4511              (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4506 4512              put_nvlist(zc, errors) != 0)) {
4507 4513                  /*
4508 4514                   * Caller made zc->zc_nvlist_dst less than the minimum expected
4509 4515                   * size or supplied an invalid address.
4510 4516                   */
4511 4517                  props_error = SET_ERROR(EINVAL);
4512 4518          }
4513 4519  
4514 4520          zc->zc_cookie = off - fp->f_offset;
4515 4521          if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4516 4522                  fp->f_offset = off;
4517 4523  
4518 4524  #ifdef  DEBUG
4519 4525          if (zfs_ioc_recv_inject_err) {
4520 4526                  zfs_ioc_recv_inject_err = B_FALSE;
4521 4527                  error = 1;
4522 4528          }
4523 4529  #endif
4524 4530          /*
4525 4531           * On error, restore the original props.
4526 4532           */
4527 4533          if (error != 0 && props != NULL && !drc.drc_newfs) {
4528 4534                  if (clear_received_props(tofs, props, NULL) != 0) {
4529 4535                          /*
4530 4536                           * We failed to clear the received properties.
4531 4537                           * Since we may have left a $recvd value on the
4532 4538                           * system, we can't clear the $hasrecvd flag.
4533 4539                           */
4534 4540                          zc->zc_obj |= ZPROP_ERR_NORESTORE;
4535 4541                  } else if (first_recvd_props) {
4536 4542                          dsl_prop_unset_hasrecvd(tofs);
4537 4543                  }
4538 4544  
4539 4545                  if (origprops == NULL && !drc.drc_newfs) {
4540 4546                          /* We failed to stash the original properties. */
4541 4547                          zc->zc_obj |= ZPROP_ERR_NORESTORE;
4542 4548                  }
4543 4549  
4544 4550                  /*
4545 4551                   * dsl_props_set() will not convert RECEIVED to LOCAL on or
4546 4552                   * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4547 4553                   * explictly if we're restoring local properties cleared in the
4548 4554                   * first new-style receive.
4549 4555                   */
4550 4556                  if (origprops != NULL &&
4551 4557                      zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4552 4558                      ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4553 4559                      origprops, NULL) != 0) {
4554 4560                          /*
4555 4561                           * We stashed the original properties but failed to
4556 4562                           * restore them.
4557 4563                           */
4558 4564                          zc->zc_obj |= ZPROP_ERR_NORESTORE;
4559 4565                  }
4560 4566          }
4561 4567  out:
4562 4568          nvlist_free(props);
4563 4569          nvlist_free(origprops);
4564 4570          nvlist_free(errors);
4565 4571          releasef(fd);
4566 4572  
4567 4573          if (error == 0)
4568 4574                  error = props_error;
4569 4575  
4570 4576          return (error);
4571 4577  }
4572 4578  
4573 4579  /*
4574 4580   * inputs:
4575 4581   * zc_name      name of snapshot to send
4576 4582   * zc_cookie    file descriptor to send stream to
4577 4583   * zc_obj       fromorigin flag (mutually exclusive with zc_fromobj)
4578 4584   * zc_sendobj   objsetid of snapshot to send
4579 4585   * zc_fromobj   objsetid of incremental fromsnap (may be zero)
4580 4586   * zc_guid      if set, estimate size of stream only.  zc_cookie is ignored.
4581 4587   *              output size in zc_objset_type.
4582 4588   * zc_flags     lzc_send_flags
4583 4589   *
4584 4590   * outputs:
4585 4591   * zc_objset_type       estimated size, if zc_guid is set
4586 4592   */
4587 4593  static int
4588 4594  zfs_ioc_send(zfs_cmd_t *zc)
4589 4595  {
4590 4596          int error;
4591 4597          offset_t off;
4592 4598          boolean_t estimate = (zc->zc_guid != 0);
4593 4599          boolean_t embedok = (zc->zc_flags & 0x1);
4594 4600          boolean_t large_block_ok = (zc->zc_flags & 0x2);
4595 4601          boolean_t compressok = (zc->zc_flags & 0x4);
4596 4602  
4597 4603          if (zc->zc_obj != 0) {
4598 4604                  dsl_pool_t *dp;
4599 4605                  dsl_dataset_t *tosnap;
4600 4606  
4601 4607                  error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4602 4608                  if (error != 0)
4603 4609                          return (error);
4604 4610  
4605 4611                  error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4606 4612                  if (error != 0) {
4607 4613                          dsl_pool_rele(dp, FTAG);
4608 4614                          return (error);
4609 4615                  }
4610 4616  
4611 4617                  if (dsl_dir_is_clone(tosnap->ds_dir))
4612 4618                          zc->zc_fromobj =
4613 4619                              dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4614 4620                  dsl_dataset_rele(tosnap, FTAG);
4615 4621                  dsl_pool_rele(dp, FTAG);
4616 4622          }
4617 4623  
4618 4624          if (estimate) {
4619 4625                  dsl_pool_t *dp;
4620 4626                  dsl_dataset_t *tosnap;
4621 4627                  dsl_dataset_t *fromsnap = NULL;
4622 4628  
4623 4629                  error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4624 4630                  if (error != 0)
4625 4631                          return (error);
4626 4632  
4627 4633                  error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4628 4634                  if (error != 0) {
4629 4635                          dsl_pool_rele(dp, FTAG);
4630 4636                          return (error);
4631 4637                  }
4632 4638  
4633 4639                  if (zc->zc_fromobj != 0) {
4634 4640                          error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4635 4641                              FTAG, &fromsnap);
4636 4642                          if (error != 0) {
4637 4643                                  dsl_dataset_rele(tosnap, FTAG);
4638 4644                                  dsl_pool_rele(dp, FTAG);
4639 4645                                  return (error);
4640 4646                          }
4641 4647                  }
4642 4648  
4643 4649                  error = dmu_send_estimate(tosnap, fromsnap, compressok,
4644 4650                      &zc->zc_objset_type);
4645 4651  
4646 4652                  if (fromsnap != NULL)
4647 4653                          dsl_dataset_rele(fromsnap, FTAG);
4648 4654                  dsl_dataset_rele(tosnap, FTAG);
4649 4655                  dsl_pool_rele(dp, FTAG);
4650 4656          } else {
4651 4657                  file_t *fp = getf(zc->zc_cookie);
4652 4658                  if (fp == NULL)
4653 4659                          return (SET_ERROR(EBADF));
4654 4660  
4655 4661                  off = fp->f_offset;
4656 4662                  error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4657 4663                      zc->zc_fromobj, embedok, large_block_ok, compressok,
4658 4664                      zc->zc_cookie, fp->f_vnode, &off);
4659 4665  
4660 4666                  if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4661 4667                          fp->f_offset = off;
4662 4668                  releasef(zc->zc_cookie);
4663 4669          }
4664 4670          return (error);
4665 4671  }
4666 4672  
4667 4673  /*
4668 4674   * inputs:
4669 4675   * zc_name      name of snapshot on which to report progress
4670 4676   * zc_cookie    file descriptor of send stream
4671 4677   *
4672 4678   * outputs:
4673 4679   * zc_cookie    number of bytes written in send stream thus far
4674 4680   */
4675 4681  static int
4676 4682  zfs_ioc_send_progress(zfs_cmd_t *zc)
4677 4683  {
4678 4684          dsl_pool_t *dp;
4679 4685          dsl_dataset_t *ds;
4680 4686          dmu_sendarg_t *dsp = NULL;
4681 4687          int error;
4682 4688  
4683 4689          error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4684 4690          if (error != 0)
4685 4691                  return (error);
4686 4692  
4687 4693          error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4688 4694          if (error != 0) {
4689 4695                  dsl_pool_rele(dp, FTAG);
4690 4696                  return (error);
4691 4697          }
4692 4698  
4693 4699          mutex_enter(&ds->ds_sendstream_lock);
4694 4700  
4695 4701          /*
4696 4702           * Iterate over all the send streams currently active on this dataset.
4697 4703           * If there's one which matches the specified file descriptor _and_ the
4698 4704           * stream was started by the current process, return the progress of
4699 4705           * that stream.
4700 4706           */
4701 4707          for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4702 4708              dsp = list_next(&ds->ds_sendstreams, dsp)) {
4703 4709                  if (dsp->dsa_outfd == zc->zc_cookie &&
4704 4710                      dsp->dsa_proc == curproc)
4705 4711                          break;
4706 4712          }
4707 4713  
4708 4714          if (dsp != NULL)
4709 4715                  zc->zc_cookie = *(dsp->dsa_off);
4710 4716          else
4711 4717                  error = SET_ERROR(ENOENT);
4712 4718  
4713 4719          mutex_exit(&ds->ds_sendstream_lock);
4714 4720          dsl_dataset_rele(ds, FTAG);
4715 4721          dsl_pool_rele(dp, FTAG);
4716 4722          return (error);
4717 4723  }
4718 4724  
4719 4725  static int
4720 4726  zfs_ioc_inject_fault(zfs_cmd_t *zc)
4721 4727  {
4722 4728          int id, error;
4723 4729  
4724 4730          error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4725 4731              &zc->zc_inject_record);
4726 4732  
4727 4733          if (error == 0)
4728 4734                  zc->zc_guid = (uint64_t)id;
4729 4735  
4730 4736          return (error);
4731 4737  }
4732 4738  
4733 4739  static int
4734 4740  zfs_ioc_clear_fault(zfs_cmd_t *zc)
4735 4741  {
4736 4742          return (zio_clear_fault((int)zc->zc_guid));
4737 4743  }
4738 4744  
4739 4745  static int
4740 4746  zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4741 4747  {
4742 4748          int id = (int)zc->zc_guid;
4743 4749          int error;
4744 4750  
4745 4751          error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4746 4752              &zc->zc_inject_record);
4747 4753  
4748 4754          zc->zc_guid = id;
4749 4755  
4750 4756          return (error);
4751 4757  }
4752 4758  
4753 4759  static int
4754 4760  zfs_ioc_error_log(zfs_cmd_t *zc)
4755 4761  {
4756 4762          spa_t *spa;
4757 4763          int error;
4758 4764          size_t count = (size_t)zc->zc_nvlist_dst_size;
4759 4765  
4760 4766          if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4761 4767                  return (error);
4762 4768  
4763 4769          error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4764 4770              &count);
4765 4771          if (error == 0)
4766 4772                  zc->zc_nvlist_dst_size = count;
4767 4773          else
4768 4774                  zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4769 4775  
4770 4776          spa_close(spa, FTAG);
4771 4777  
4772 4778          return (error);
4773 4779  }
4774 4780  
4775 4781  static int
4776 4782  zfs_ioc_clear(zfs_cmd_t *zc)
4777 4783  {
4778 4784          spa_t *spa;
4779 4785          vdev_t *vd;
4780 4786          int error;
4781 4787  
4782 4788          /*
4783 4789           * On zpool clear we also fix up missing slogs
4784 4790           */
4785 4791          mutex_enter(&spa_namespace_lock);
4786 4792          spa = spa_lookup(zc->zc_name);
4787 4793          if (spa == NULL) {
4788 4794                  mutex_exit(&spa_namespace_lock);
4789 4795                  return (SET_ERROR(EIO));
4790 4796          }
4791 4797          if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4792 4798                  /* we need to let spa_open/spa_load clear the chains */
4793 4799                  spa_set_log_state(spa, SPA_LOG_CLEAR);
4794 4800          }
4795 4801          spa->spa_last_open_failed = 0;
4796 4802          mutex_exit(&spa_namespace_lock);
4797 4803  
4798 4804          if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4799 4805                  error = spa_open(zc->zc_name, &spa, FTAG);
4800 4806          } else {
4801 4807                  nvlist_t *policy;
4802 4808                  nvlist_t *config = NULL;
4803 4809  
4804 4810                  if (zc->zc_nvlist_src == NULL)
4805 4811                          return (SET_ERROR(EINVAL));
4806 4812  
4807 4813                  if ((error = get_nvlist(zc->zc_nvlist_src,
4808 4814                      zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4809 4815                          error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4810 4816                              policy, &config);
4811 4817                          if (config != NULL) {
4812 4818                                  int err;
4813 4819  
4814 4820                                  if ((err = put_nvlist(zc, config)) != 0)
4815 4821                                          error = err;
4816 4822                                  nvlist_free(config);
4817 4823                          }
4818 4824                          nvlist_free(policy);
4819 4825                  }
4820 4826          }
4821 4827  
4822 4828          if (error != 0)
4823 4829                  return (error);
4824 4830  
4825 4831          spa_vdev_state_enter(spa, SCL_NONE);
4826 4832  
4827 4833          if (zc->zc_guid == 0) {
4828 4834                  vd = NULL;
4829 4835          } else {
4830 4836                  vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4831 4837                  if (vd == NULL) {
4832 4838                          (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4833 4839                          spa_close(spa, FTAG);
4834 4840                          return (SET_ERROR(ENODEV));
4835 4841                  }
4836 4842          }
4837 4843  
4838 4844          vdev_clear(spa, vd);
4839 4845  
4840 4846          (void) spa_vdev_state_exit(spa, NULL, 0);
4841 4847  
4842 4848          /*
4843 4849           * Resume any suspended I/Os.
4844 4850           */
4845 4851          if (zio_resume(spa) != 0)
4846 4852                  error = SET_ERROR(EIO);
4847 4853  
4848 4854          spa_close(spa, FTAG);
4849 4855  
4850 4856          return (error);
4851 4857  }
4852 4858  
4853 4859  static int
4854 4860  zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4855 4861  {
4856 4862          spa_t *spa;
4857 4863          int error;
4858 4864  
4859 4865          error = spa_open(zc->zc_name, &spa, FTAG);
4860 4866          if (error != 0)
4861 4867                  return (error);
4862 4868  
4863 4869          spa_vdev_state_enter(spa, SCL_NONE);
4864 4870  
4865 4871          /*
4866 4872           * If a resilver is already in progress then set the
4867 4873           * spa_scrub_reopen flag to B_TRUE so that we don't restart
4868 4874           * the scan as a side effect of the reopen. Otherwise, let
4869 4875           * vdev_open() decided if a resilver is required.
4870 4876           */
4871 4877          spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4872 4878          vdev_reopen(spa->spa_root_vdev);
4873 4879          spa->spa_scrub_reopen = B_FALSE;
4874 4880  
4875 4881          (void) spa_vdev_state_exit(spa, NULL, 0);
4876 4882          spa_close(spa, FTAG);
4877 4883          return (0);
4878 4884  }
4879 4885  /*
4880 4886   * inputs:
4881 4887   * zc_name      name of filesystem
4882 4888   *
4883 4889   * outputs:
4884 4890   * zc_string    name of conflicting snapshot, if there is one
4885 4891   */
4886 4892  static int
4887 4893  zfs_ioc_promote(zfs_cmd_t *zc)
4888 4894  {
4889 4895          dsl_pool_t *dp;
4890 4896          dsl_dataset_t *ds, *ods;
4891 4897          char origin[ZFS_MAX_DATASET_NAME_LEN];
4892 4898          char *cp;
4893 4899          int error;
4894 4900  
4895 4901          zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4896 4902          if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4897 4903              strchr(zc->zc_name, '%'))
4898 4904                  return (SET_ERROR(EINVAL));
4899 4905  
4900 4906          error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4901 4907          if (error != 0)
4902 4908                  return (error);
4903 4909  
4904 4910          error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4905 4911          if (error != 0) {
4906 4912                  dsl_pool_rele(dp, FTAG);
4907 4913                  return (error);
4908 4914          }
4909 4915  
4910 4916          if (!dsl_dir_is_clone(ds->ds_dir)) {
4911 4917                  dsl_dataset_rele(ds, FTAG);
4912 4918                  dsl_pool_rele(dp, FTAG);
4913 4919                  return (SET_ERROR(EINVAL));
4914 4920          }
4915 4921  
4916 4922          error = dsl_dataset_hold_obj(dp,
4917 4923              dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4918 4924          if (error != 0) {
4919 4925                  dsl_dataset_rele(ds, FTAG);
4920 4926                  dsl_pool_rele(dp, FTAG);
4921 4927                  return (error);
4922 4928          }
4923 4929  
4924 4930          dsl_dataset_name(ods, origin);
4925 4931          dsl_dataset_rele(ods, FTAG);
4926 4932          dsl_dataset_rele(ds, FTAG);
4927 4933          dsl_pool_rele(dp, FTAG);
4928 4934  
4929 4935          /*
4930 4936           * We don't need to unmount *all* the origin fs's snapshots, but
4931 4937           * it's easier.
4932 4938           */
4933 4939          cp = strchr(origin, '@');
4934 4940          if (cp)
4935 4941                  *cp = '\0';
4936 4942          (void) dmu_objset_find(origin,
4937 4943              zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4938 4944          return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4939 4945  }
4940 4946  
4941 4947  /*
4942 4948   * Retrieve a single {user|group}{used|quota}@... property.
4943 4949   *
4944 4950   * inputs:
4945 4951   * zc_name      name of filesystem
4946 4952   * zc_objset_type zfs_userquota_prop_t
4947 4953   * zc_value     domain name (eg. "S-1-234-567-89")
4948 4954   * zc_guid      RID/UID/GID
4949 4955   *
4950 4956   * outputs:
4951 4957   * zc_cookie    property value
4952 4958   */
4953 4959  static int
4954 4960  zfs_ioc_userspace_one(zfs_cmd_t *zc)
4955 4961  {
4956 4962          zfsvfs_t *zfsvfs;
4957 4963          int error;
4958 4964  
4959 4965          if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4960 4966                  return (SET_ERROR(EINVAL));
4961 4967  
4962 4968          error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4963 4969          if (error != 0)
4964 4970                  return (error);
4965 4971  
4966 4972          error = zfs_userspace_one(zfsvfs,
4967 4973              zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4968 4974          zfsvfs_rele(zfsvfs, FTAG);
4969 4975  
4970 4976          return (error);
4971 4977  }
4972 4978  
4973 4979  /*
4974 4980   * inputs:
4975 4981   * zc_name              name of filesystem
4976 4982   * zc_cookie            zap cursor
4977 4983   * zc_objset_type       zfs_userquota_prop_t
4978 4984   * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4979 4985   *
4980 4986   * outputs:
4981 4987   * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4982 4988   * zc_cookie    zap cursor
4983 4989   */
4984 4990  static int
4985 4991  zfs_ioc_userspace_many(zfs_cmd_t *zc)
4986 4992  {
4987 4993          zfsvfs_t *zfsvfs;
4988 4994          int bufsize = zc->zc_nvlist_dst_size;
4989 4995  
4990 4996          if (bufsize <= 0)
4991 4997                  return (SET_ERROR(ENOMEM));
4992 4998  
4993 4999          int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4994 5000          if (error != 0)
4995 5001                  return (error);
4996 5002  
4997 5003          void *buf = kmem_alloc(bufsize, KM_SLEEP);
4998 5004  
4999 5005          error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5000 5006              buf, &zc->zc_nvlist_dst_size);
5001 5007  
5002 5008          if (error == 0) {
5003 5009                  error = xcopyout(buf,
5004 5010                      (void *)(uintptr_t)zc->zc_nvlist_dst,
5005 5011                      zc->zc_nvlist_dst_size);
5006 5012          }
5007 5013          kmem_free(buf, bufsize);
5008 5014          zfsvfs_rele(zfsvfs, FTAG);
5009 5015  
5010 5016          return (error);
5011 5017  }
5012 5018  
5013 5019  /*
5014 5020   * inputs:
5015 5021   * zc_name              name of filesystem
5016 5022   *
5017 5023   * outputs:
5018 5024   * none
5019 5025   */
5020 5026  static int
5021 5027  zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5022 5028  {
5023 5029          objset_t *os;
5024 5030          int error = 0;
5025 5031          zfsvfs_t *zfsvfs;
5026 5032  
5027 5033          if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5028 5034                  if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5029 5035                          /*
5030 5036                           * If userused is not enabled, it may be because the
5031 5037                           * objset needs to be closed & reopened (to grow the
5032 5038                           * objset_phys_t).  Suspend/resume the fs will do that.
5033 5039                           */
5034 5040                          dsl_dataset_t *ds, *newds;
5035 5041  
5036 5042                          ds = dmu_objset_ds(zfsvfs->z_os);
5037 5043                          error = zfs_suspend_fs(zfsvfs);
5038 5044                          if (error == 0) {
5039 5045                                  dmu_objset_refresh_ownership(ds, &newds,
5040 5046                                      zfsvfs);
5041 5047                                  error = zfs_resume_fs(zfsvfs, newds);
5042 5048                          }
5043 5049                  }
5044 5050                  if (error == 0)
5045 5051                          error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5046 5052                  VFS_RELE(zfsvfs->z_vfs);
5047 5053          } else {
5048 5054                  /* XXX kind of reading contents without owning */
5049 5055                  error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5050 5056                  if (error != 0)
5051 5057                          return (error);
5052 5058  
5053 5059                  error = dmu_objset_userspace_upgrade(os);
5054 5060                  dmu_objset_rele(os, FTAG);
5055 5061          }
5056 5062  
5057 5063          return (error);
5058 5064  }
5059 5065  
5060 5066  /*
5061 5067   * We don't want to have a hard dependency
5062 5068   * against some special symbols in sharefs
5063 5069   * nfs, and smbsrv.  Determine them if needed when
5064 5070   * the first file system is shared.
5065 5071   * Neither sharefs, nfs or smbsrv are unloadable modules.
5066 5072   */
5067 5073  int (*znfsexport_fs)(void *arg);
5068 5074  int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5069 5075  int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5070 5076  
5071 5077  int zfs_nfsshare_inited;
5072 5078  int zfs_smbshare_inited;
5073 5079  
5074 5080  ddi_modhandle_t nfs_mod;
5075 5081  ddi_modhandle_t sharefs_mod;
5076 5082  ddi_modhandle_t smbsrv_mod;
5077 5083  kmutex_t zfs_share_lock;
5078 5084  
5079 5085  static int
5080 5086  zfs_init_sharefs()
5081 5087  {
5082 5088          int error;
5083 5089  
5084 5090          ASSERT(MUTEX_HELD(&zfs_share_lock));
5085 5091          /* Both NFS and SMB shares also require sharetab support. */
5086 5092          if (sharefs_mod == NULL && ((sharefs_mod =
5087 5093              ddi_modopen("fs/sharefs",
5088 5094              KRTLD_MODE_FIRST, &error)) == NULL)) {
5089 5095                  return (SET_ERROR(ENOSYS));
5090 5096          }
5091 5097          if (zshare_fs == NULL && ((zshare_fs =
5092 5098              (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5093 5099              ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5094 5100                  return (SET_ERROR(ENOSYS));
5095 5101          }
5096 5102          return (0);
5097 5103  }
5098 5104  
5099 5105  static int
5100 5106  zfs_ioc_share(zfs_cmd_t *zc)
5101 5107  {
5102 5108          int error;
5103 5109          int opcode;
5104 5110  
5105 5111          switch (zc->zc_share.z_sharetype) {
5106 5112          case ZFS_SHARE_NFS:
5107 5113          case ZFS_UNSHARE_NFS:
5108 5114                  if (zfs_nfsshare_inited == 0) {
5109 5115                          mutex_enter(&zfs_share_lock);
5110 5116                          if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5111 5117                              KRTLD_MODE_FIRST, &error)) == NULL)) {
5112 5118                                  mutex_exit(&zfs_share_lock);
5113 5119                                  return (SET_ERROR(ENOSYS));
5114 5120                          }
5115 5121                          if (znfsexport_fs == NULL &&
5116 5122                              ((znfsexport_fs = (int (*)(void *))
5117 5123                              ddi_modsym(nfs_mod,
5118 5124                              "nfs_export", &error)) == NULL)) {
5119 5125                                  mutex_exit(&zfs_share_lock);
5120 5126                                  return (SET_ERROR(ENOSYS));
5121 5127                          }
5122 5128                          error = zfs_init_sharefs();
5123 5129                          if (error != 0) {
5124 5130                                  mutex_exit(&zfs_share_lock);
5125 5131                                  return (SET_ERROR(ENOSYS));
5126 5132                          }
5127 5133                          zfs_nfsshare_inited = 1;
5128 5134                          mutex_exit(&zfs_share_lock);
5129 5135                  }
5130 5136                  break;
5131 5137          case ZFS_SHARE_SMB:
5132 5138          case ZFS_UNSHARE_SMB:
5133 5139                  if (zfs_smbshare_inited == 0) {
5134 5140                          mutex_enter(&zfs_share_lock);
5135 5141                          if (smbsrv_mod == NULL && ((smbsrv_mod =
5136 5142                              ddi_modopen("drv/smbsrv",
5137 5143                              KRTLD_MODE_FIRST, &error)) == NULL)) {
5138 5144                                  mutex_exit(&zfs_share_lock);
5139 5145                                  return (SET_ERROR(ENOSYS));
5140 5146                          }
5141 5147                          if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5142 5148                              (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5143 5149                              "smb_server_share", &error)) == NULL)) {
5144 5150                                  mutex_exit(&zfs_share_lock);
5145 5151                                  return (SET_ERROR(ENOSYS));
5146 5152                          }
5147 5153                          error = zfs_init_sharefs();
5148 5154                          if (error != 0) {
5149 5155                                  mutex_exit(&zfs_share_lock);
5150 5156                                  return (SET_ERROR(ENOSYS));
5151 5157                          }
5152 5158                          zfs_smbshare_inited = 1;
5153 5159                          mutex_exit(&zfs_share_lock);
5154 5160                  }
5155 5161                  break;
5156 5162          default:
5157 5163                  return (SET_ERROR(EINVAL));
5158 5164          }
5159 5165  
5160 5166          switch (zc->zc_share.z_sharetype) {
5161 5167          case ZFS_SHARE_NFS:
5162 5168          case ZFS_UNSHARE_NFS:
5163 5169                  if (error =
5164 5170                      znfsexport_fs((void *)
5165 5171                      (uintptr_t)zc->zc_share.z_exportdata))
5166 5172                          return (error);
5167 5173                  break;
5168 5174          case ZFS_SHARE_SMB:
5169 5175          case ZFS_UNSHARE_SMB:
5170 5176                  if (error = zsmbexport_fs((void *)
5171 5177                      (uintptr_t)zc->zc_share.z_exportdata,
5172 5178                      zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5173 5179                      B_TRUE: B_FALSE)) {
5174 5180                          return (error);
5175 5181                  }
5176 5182                  break;
5177 5183          }
5178 5184  
5179 5185          opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5180 5186              zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5181 5187              SHAREFS_ADD : SHAREFS_REMOVE;
5182 5188  
5183 5189          /*
5184 5190           * Add or remove share from sharetab
5185 5191           */
5186 5192          error = zshare_fs(opcode,
5187 5193              (void *)(uintptr_t)zc->zc_share.z_sharedata,
5188 5194              zc->zc_share.z_sharemax);
5189 5195  
5190 5196          return (error);
5191 5197  
5192 5198  }
5193 5199  
5194 5200  ace_t full_access[] = {
5195 5201          {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5196 5202  };
5197 5203  
5198 5204  /*
5199 5205   * inputs:
5200 5206   * zc_name              name of containing filesystem
5201 5207   * zc_obj               object # beyond which we want next in-use object #
5202 5208   *
5203 5209   * outputs:
5204 5210   * zc_obj               next in-use object #
5205 5211   */
5206 5212  static int
5207 5213  zfs_ioc_next_obj(zfs_cmd_t *zc)
5208 5214  {
5209 5215          objset_t *os = NULL;
5210 5216          int error;
5211 5217  
5212 5218          error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5213 5219          if (error != 0)
5214 5220                  return (error);
5215 5221  
5216 5222          error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5217 5223              dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5218 5224  
5219 5225          dmu_objset_rele(os, FTAG);
5220 5226          return (error);
5221 5227  }
5222 5228  
5223 5229  /*
5224 5230   * inputs:
5225 5231   * zc_name              name of filesystem
5226 5232   * zc_value             prefix name for snapshot
5227 5233   * zc_cleanup_fd        cleanup-on-exit file descriptor for calling process
5228 5234   *
5229 5235   * outputs:
5230 5236   * zc_value             short name of new snapshot
5231 5237   */
5232 5238  static int
5233 5239  zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5234 5240  {
5235 5241          char *snap_name;
5236 5242          char *hold_name;
5237 5243          int error;
5238 5244          minor_t minor;
5239 5245  
5240 5246          error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5241 5247          if (error != 0)
5242 5248                  return (error);
5243 5249  
5244 5250          snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5245 5251              (u_longlong_t)ddi_get_lbolt64());
5246 5252          hold_name = kmem_asprintf("%%%s", zc->zc_value);
5247 5253  
5248 5254          error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5249 5255              hold_name);
5250 5256          if (error == 0)
5251 5257                  (void) strcpy(zc->zc_value, snap_name);
5252 5258          strfree(snap_name);
5253 5259          strfree(hold_name);
5254 5260          zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5255 5261          return (error);
5256 5262  }
5257 5263  
5258 5264  /*
5259 5265   * inputs:
5260 5266   * zc_name              name of "to" snapshot
5261 5267   * zc_value             name of "from" snapshot
5262 5268   * zc_cookie            file descriptor to write diff data on
5263 5269   *
5264 5270   * outputs:
5265 5271   * dmu_diff_record_t's to the file descriptor
5266 5272   */
5267 5273  static int
5268 5274  zfs_ioc_diff(zfs_cmd_t *zc)
5269 5275  {
5270 5276          file_t *fp;
5271 5277          offset_t off;
5272 5278          int error;
5273 5279  
5274 5280          fp = getf(zc->zc_cookie);
5275 5281          if (fp == NULL)
5276 5282                  return (SET_ERROR(EBADF));
5277 5283  
5278 5284          off = fp->f_offset;
5279 5285  
5280 5286          error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5281 5287  
5282 5288          if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5283 5289                  fp->f_offset = off;
5284 5290          releasef(zc->zc_cookie);
5285 5291  
5286 5292          return (error);
5287 5293  }
5288 5294  
5289 5295  /*
5290 5296   * Remove all ACL files in shares dir
5291 5297   */
5292 5298  static int
5293 5299  zfs_smb_acl_purge(znode_t *dzp)
5294 5300  {
5295 5301          zap_cursor_t    zc;
5296 5302          zap_attribute_t zap;
5297 5303          zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5298 5304          int error;
5299 5305  
5300 5306          for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5301 5307              (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5302 5308              zap_cursor_advance(&zc)) {
5303 5309                  if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5304 5310                      NULL, 0)) != 0)
5305 5311                          break;
5306 5312          }
5307 5313          zap_cursor_fini(&zc);
5308 5314          return (error);
5309 5315  }
5310 5316  
5311 5317  static int
5312 5318  zfs_ioc_smb_acl(zfs_cmd_t *zc)
5313 5319  {
5314 5320          vnode_t *vp;
5315 5321          znode_t *dzp;
5316 5322          vnode_t *resourcevp = NULL;
5317 5323          znode_t *sharedir;
5318 5324          zfsvfs_t *zfsvfs;
5319 5325          nvlist_t *nvlist;
5320 5326          char *src, *target;
5321 5327          vattr_t vattr;
5322 5328          vsecattr_t vsec;
5323 5329          int error = 0;
5324 5330  
5325 5331          if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5326 5332              NO_FOLLOW, NULL, &vp)) != 0)
5327 5333                  return (error);
5328 5334  
5329 5335          /* Now make sure mntpnt and dataset are ZFS */
5330 5336  
5331 5337          if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5332 5338              (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5333 5339              zc->zc_name) != 0)) {
5334 5340                  VN_RELE(vp);
5335 5341                  return (SET_ERROR(EINVAL));
5336 5342          }
5337 5343  
5338 5344          dzp = VTOZ(vp);
5339 5345          zfsvfs = dzp->z_zfsvfs;
5340 5346          ZFS_ENTER(zfsvfs);
5341 5347  
5342 5348          /*
5343 5349           * Create share dir if its missing.
5344 5350           */
5345 5351          mutex_enter(&zfsvfs->z_lock);
5346 5352          if (zfsvfs->z_shares_dir == 0) {
5347 5353                  dmu_tx_t *tx;
5348 5354  
5349 5355                  tx = dmu_tx_create(zfsvfs->z_os);
5350 5356                  dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5351 5357                      ZFS_SHARES_DIR);
5352 5358                  dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5353 5359                  error = dmu_tx_assign(tx, TXG_WAIT);
5354 5360                  if (error != 0) {
5355 5361                          dmu_tx_abort(tx);
5356 5362                  } else {
5357 5363                          error = zfs_create_share_dir(zfsvfs, tx);
5358 5364                          dmu_tx_commit(tx);
5359 5365                  }
5360 5366                  if (error != 0) {
5361 5367                          mutex_exit(&zfsvfs->z_lock);
5362 5368                          VN_RELE(vp);
5363 5369                          ZFS_EXIT(zfsvfs);
5364 5370                          return (error);
5365 5371                  }
5366 5372          }
5367 5373          mutex_exit(&zfsvfs->z_lock);
5368 5374  
5369 5375          ASSERT(zfsvfs->z_shares_dir);
5370 5376          if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5371 5377                  VN_RELE(vp);
5372 5378                  ZFS_EXIT(zfsvfs);
5373 5379                  return (error);
5374 5380          }
5375 5381  
5376 5382          switch (zc->zc_cookie) {
5377 5383          case ZFS_SMB_ACL_ADD:
5378 5384                  vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5379 5385                  vattr.va_type = VREG;
5380 5386                  vattr.va_mode = S_IFREG|0777;
5381 5387                  vattr.va_uid = 0;
5382 5388                  vattr.va_gid = 0;
5383 5389  
5384 5390                  vsec.vsa_mask = VSA_ACE;
5385 5391                  vsec.vsa_aclentp = &full_access;
5386 5392                  vsec.vsa_aclentsz = sizeof (full_access);
5387 5393                  vsec.vsa_aclcnt = 1;
5388 5394  
5389 5395                  error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5390 5396                      &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5391 5397                  if (resourcevp)
5392 5398                          VN_RELE(resourcevp);
5393 5399                  break;
5394 5400  
5395 5401          case ZFS_SMB_ACL_REMOVE:
5396 5402                  error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5397 5403                      NULL, 0);
5398 5404                  break;
5399 5405  
5400 5406          case ZFS_SMB_ACL_RENAME:
5401 5407                  if ((error = get_nvlist(zc->zc_nvlist_src,
5402 5408                      zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5403 5409                          VN_RELE(vp);
5404 5410                          VN_RELE(ZTOV(sharedir));
5405 5411                          ZFS_EXIT(zfsvfs);
5406 5412                          return (error);
5407 5413                  }
5408 5414                  if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5409 5415                      nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5410 5416                      &target)) {
5411 5417                          VN_RELE(vp);
5412 5418                          VN_RELE(ZTOV(sharedir));
5413 5419                          ZFS_EXIT(zfsvfs);
5414 5420                          nvlist_free(nvlist);
5415 5421                          return (error);
5416 5422                  }
5417 5423                  error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5418 5424                      kcred, NULL, 0);
5419 5425                  nvlist_free(nvlist);
5420 5426                  break;
5421 5427  
5422 5428          case ZFS_SMB_ACL_PURGE:
5423 5429                  error = zfs_smb_acl_purge(sharedir);
5424 5430                  break;
5425 5431  
5426 5432          default:
5427 5433                  error = SET_ERROR(EINVAL);
5428 5434                  break;
5429 5435          }
5430 5436  
5431 5437          VN_RELE(vp);
5432 5438          VN_RELE(ZTOV(sharedir));
5433 5439  
5434 5440          ZFS_EXIT(zfsvfs);
5435 5441  
5436 5442          return (error);
5437 5443  }
5438 5444  
5439 5445  /*
5440 5446   * innvl: {
5441 5447   *     "holds" -> { snapname -> holdname (string), ... }
5442 5448   *     (optional) "cleanup_fd" -> fd (int32)
5443 5449   * }
5444 5450   *
5445 5451   * outnvl: {
5446 5452   *     snapname -> error value (int32)
5447 5453   *     ...
5448 5454   * }
5449 5455   */
5450 5456  /* ARGSUSED */
5451 5457  static int
5452 5458  zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5453 5459  {
5454 5460          nvpair_t *pair;
5455 5461          nvlist_t *holds;
5456 5462          int cleanup_fd = -1;
5457 5463          int error;
5458 5464          minor_t minor = 0;
5459 5465  
5460 5466          error = nvlist_lookup_nvlist(args, "holds", &holds);
5461 5467          if (error != 0)
5462 5468                  return (SET_ERROR(EINVAL));
5463 5469  
5464 5470          /* make sure the user didn't pass us any invalid (empty) tags */
5465 5471          for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5466 5472              pair = nvlist_next_nvpair(holds, pair)) {
5467 5473                  char *htag;
5468 5474  
5469 5475                  error = nvpair_value_string(pair, &htag);
5470 5476                  if (error != 0)
5471 5477                          return (SET_ERROR(error));
5472 5478  
5473 5479                  if (strlen(htag) == 0)
5474 5480                          return (SET_ERROR(EINVAL));
5475 5481          }
5476 5482  
5477 5483          if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5478 5484                  error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5479 5485                  if (error != 0)
5480 5486                          return (error);
5481 5487          }
5482 5488  
5483 5489          error = dsl_dataset_user_hold(holds, minor, errlist);
5484 5490          if (minor != 0)
5485 5491                  zfs_onexit_fd_rele(cleanup_fd);
5486 5492          return (error);
5487 5493  }
5488 5494  
5489 5495  /*
5490 5496   * innvl is not used.
5491 5497   *
5492 5498   * outnvl: {
5493 5499   *    holdname -> time added (uint64 seconds since epoch)
5494 5500   *    ...
5495 5501   * }
5496 5502   */
5497 5503  /* ARGSUSED */
5498 5504  static int
5499 5505  zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5500 5506  {
5501 5507          return (dsl_dataset_get_holds(snapname, outnvl));
5502 5508  }
5503 5509  
5504 5510  /*
5505 5511   * innvl: {
5506 5512   *     snapname -> { holdname, ... }
5507 5513   *     ...
5508 5514   * }
5509 5515   *
5510 5516   * outnvl: {
5511 5517   *     snapname -> error value (int32)
5512 5518   *     ...
5513 5519   * }
5514 5520   */
5515 5521  /* ARGSUSED */
5516 5522  static int
5517 5523  zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5518 5524  {
5519 5525          return (dsl_dataset_user_release(holds, errlist));
5520 5526  }
5521 5527  
5522 5528  /*
5523 5529   * inputs:
5524 5530   * zc_name              name of new filesystem or snapshot
5525 5531   * zc_value             full name of old snapshot
5526 5532   *
5527 5533   * outputs:
5528 5534   * zc_cookie            space in bytes
5529 5535   * zc_objset_type       compressed space in bytes
5530 5536   * zc_perm_action       uncompressed space in bytes
5531 5537   */
5532 5538  static int
5533 5539  zfs_ioc_space_written(zfs_cmd_t *zc)
5534 5540  {
5535 5541          int error;
5536 5542          dsl_pool_t *dp;
5537 5543          dsl_dataset_t *new, *old;
5538 5544  
5539 5545          error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5540 5546          if (error != 0)
5541 5547                  return (error);
5542 5548          error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5543 5549          if (error != 0) {
5544 5550                  dsl_pool_rele(dp, FTAG);
5545 5551                  return (error);
5546 5552          }
5547 5553          error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5548 5554          if (error != 0) {
5549 5555                  dsl_dataset_rele(new, FTAG);
5550 5556                  dsl_pool_rele(dp, FTAG);
5551 5557                  return (error);
5552 5558          }
5553 5559  
5554 5560          error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5555 5561              &zc->zc_objset_type, &zc->zc_perm_action);
5556 5562          dsl_dataset_rele(old, FTAG);
5557 5563          dsl_dataset_rele(new, FTAG);
5558 5564          dsl_pool_rele(dp, FTAG);
5559 5565          return (error);
5560 5566  }
5561 5567  
5562 5568  /*
5563 5569   * innvl: {
5564 5570   *     "firstsnap" -> snapshot name
5565 5571   * }
5566 5572   *
5567 5573   * outnvl: {
5568 5574   *     "used" -> space in bytes
5569 5575   *     "compressed" -> compressed space in bytes
5570 5576   *     "uncompressed" -> uncompressed space in bytes
5571 5577   * }
5572 5578   */
5573 5579  static int
5574 5580  zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5575 5581  {
5576 5582          int error;
5577 5583          dsl_pool_t *dp;
5578 5584          dsl_dataset_t *new, *old;
5579 5585          char *firstsnap;
5580 5586          uint64_t used, comp, uncomp;
5581 5587  
5582 5588          if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5583 5589                  return (SET_ERROR(EINVAL));
5584 5590  
5585 5591          error = dsl_pool_hold(lastsnap, FTAG, &dp);
5586 5592          if (error != 0)
5587 5593                  return (error);
5588 5594  
5589 5595          error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5590 5596          if (error == 0 && !new->ds_is_snapshot) {
5591 5597                  dsl_dataset_rele(new, FTAG);
5592 5598                  error = SET_ERROR(EINVAL);
5593 5599          }
5594 5600          if (error != 0) {
5595 5601                  dsl_pool_rele(dp, FTAG);
5596 5602                  return (error);
5597 5603          }
5598 5604          error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5599 5605          if (error == 0 && !old->ds_is_snapshot) {
5600 5606                  dsl_dataset_rele(old, FTAG);
5601 5607                  error = SET_ERROR(EINVAL);
5602 5608          }
5603 5609          if (error != 0) {
5604 5610                  dsl_dataset_rele(new, FTAG);
5605 5611                  dsl_pool_rele(dp, FTAG);
5606 5612                  return (error);
5607 5613          }
5608 5614  
5609 5615          error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5610 5616          dsl_dataset_rele(old, FTAG);
5611 5617          dsl_dataset_rele(new, FTAG);
5612 5618          dsl_pool_rele(dp, FTAG);
5613 5619          fnvlist_add_uint64(outnvl, "used", used);
5614 5620          fnvlist_add_uint64(outnvl, "compressed", comp);
5615 5621          fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5616 5622          return (error);
5617 5623  }
5618 5624  
5619 5625  /*
5620 5626   * innvl: {
5621 5627   *     "fd" -> file descriptor to write stream to (int32)
5622 5628   *     (optional) "fromsnap" -> full snap name to send an incremental from
5623 5629   *     (optional) "largeblockok" -> (value ignored)
5624 5630   *         indicates that blocks > 128KB are permitted
5625 5631   *     (optional) "embedok" -> (value ignored)
5626 5632   *         presence indicates DRR_WRITE_EMBEDDED records are permitted
5627 5633   *     (optional) "compressok" -> (value ignored)
5628 5634   *         presence indicates compressed DRR_WRITE records are permitted
5629 5635   *     (optional) "resume_object" and "resume_offset" -> (uint64)
5630 5636   *         if present, resume send stream from specified object and offset.
5631 5637   * }
5632 5638   *
5633 5639   * outnvl is unused
5634 5640   */
5635 5641  /* ARGSUSED */
5636 5642  static int
5637 5643  zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5638 5644  {
5639 5645          int error;
5640 5646          offset_t off;
5641 5647          char *fromname = NULL;
5642 5648          int fd;
5643 5649          boolean_t largeblockok;
5644 5650          boolean_t embedok;
5645 5651          boolean_t compressok;
5646 5652          uint64_t resumeobj = 0;
5647 5653          uint64_t resumeoff = 0;
5648 5654  
5649 5655          error = nvlist_lookup_int32(innvl, "fd", &fd);
5650 5656          if (error != 0)
5651 5657                  return (SET_ERROR(EINVAL));
5652 5658  
5653 5659          (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5654 5660  
5655 5661          largeblockok = nvlist_exists(innvl, "largeblockok");
5656 5662          embedok = nvlist_exists(innvl, "embedok");
5657 5663          compressok = nvlist_exists(innvl, "compressok");
5658 5664  
5659 5665          (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5660 5666          (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5661 5667  
5662 5668          file_t *fp = getf(fd);
5663 5669          if (fp == NULL)
5664 5670                  return (SET_ERROR(EBADF));
5665 5671  
5666 5672          off = fp->f_offset;
5667 5673          error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5668 5674              fd, resumeobj, resumeoff, fp->f_vnode, &off);
5669 5675  
5670 5676          if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5671 5677                  fp->f_offset = off;
5672 5678          releasef(fd);
5673 5679          return (error);
5674 5680  }
5675 5681  
5676 5682  /*
5677 5683   * Determine approximately how large a zfs send stream will be -- the number
5678 5684   * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5679 5685   *
5680 5686   * innvl: {
5681 5687   *     (optional) "from" -> full snap or bookmark name to send an incremental
5682 5688   *                          from
5683 5689   *     (optional) "largeblockok" -> (value ignored)
5684 5690   *         indicates that blocks > 128KB are permitted
5685 5691   *     (optional) "embedok" -> (value ignored)
5686 5692   *         presence indicates DRR_WRITE_EMBEDDED records are permitted
5687 5693   *     (optional) "compressok" -> (value ignored)
5688 5694   *         presence indicates compressed DRR_WRITE records are permitted
5689 5695   * }
5690 5696   *
5691 5697   * outnvl: {
5692 5698   *     "space" -> bytes of space (uint64)
5693 5699   * }
5694 5700   */
5695 5701  static int
5696 5702  zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5697 5703  {
5698 5704          dsl_pool_t *dp;
5699 5705          dsl_dataset_t *tosnap;
5700 5706          int error;
5701 5707          char *fromname;
5702 5708          boolean_t compressok;
5703 5709          uint64_t space;
5704 5710  
5705 5711          error = dsl_pool_hold(snapname, FTAG, &dp);
5706 5712          if (error != 0)
5707 5713                  return (error);
5708 5714  
5709 5715          error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5710 5716          if (error != 0) {
5711 5717                  dsl_pool_rele(dp, FTAG);
5712 5718                  return (error);
5713 5719          }
5714 5720  
5715 5721          compressok = nvlist_exists(innvl, "compressok");
5716 5722  
5717 5723          error = nvlist_lookup_string(innvl, "from", &fromname);
5718 5724          if (error == 0) {
5719 5725                  if (strchr(fromname, '@') != NULL) {
5720 5726                          /*
5721 5727                           * If from is a snapshot, hold it and use the more
5722 5728                           * efficient dmu_send_estimate to estimate send space
5723 5729                           * size using deadlists.
5724 5730                           */
5725 5731                          dsl_dataset_t *fromsnap;
5726 5732                          error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5727 5733                          if (error != 0)
5728 5734                                  goto out;
5729 5735                          error = dmu_send_estimate(tosnap, fromsnap, compressok,
5730 5736                              &space);
5731 5737                          dsl_dataset_rele(fromsnap, FTAG);
5732 5738                  } else if (strchr(fromname, '#') != NULL) {
5733 5739                          /*
5734 5740                           * If from is a bookmark, fetch the creation TXG of the
5735 5741                           * snapshot it was created from and use that to find
5736 5742                           * blocks that were born after it.
5737 5743                           */
5738 5744                          zfs_bookmark_phys_t frombm;
5739 5745  
5740 5746                          error = dsl_bookmark_lookup(dp, fromname, tosnap,
5741 5747                              &frombm);
5742 5748                          if (error != 0)
5743 5749                                  goto out;
5744 5750                          error = dmu_send_estimate_from_txg(tosnap,
5745 5751                              frombm.zbm_creation_txg, compressok, &space);
5746 5752                  } else {
5747 5753                          /*
5748 5754                           * from is not properly formatted as a snapshot or
5749 5755                           * bookmark
5750 5756                           */
5751 5757                          error = SET_ERROR(EINVAL);
5752 5758                          goto out;
5753 5759                  }
5754 5760          } else {
5755 5761                  /*
5756 5762                   * If estimating the size of a full send, use dmu_send_estimate.
5757 5763                   */
5758 5764                  error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5759 5765          }
5760 5766  
5761 5767          fnvlist_add_uint64(outnvl, "space", space);
5762 5768  
5763 5769  out:
5764 5770          dsl_dataset_rele(tosnap, FTAG);
5765 5771          dsl_pool_rele(dp, FTAG);
5766 5772          return (error);
5767 5773  }
5768 5774  
5769 5775  static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5770 5776  
5771 5777  static void
5772 5778  zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5773 5779      zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5774 5780      boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5775 5781  {
5776 5782          zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5777 5783  
5778 5784          ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5779 5785          ASSERT3U(ioc, <, ZFS_IOC_LAST);
5780 5786          ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5781 5787          ASSERT3P(vec->zvec_func, ==, NULL);
5782 5788  
5783 5789          vec->zvec_legacy_func = func;
5784 5790          vec->zvec_secpolicy = secpolicy;
5785 5791          vec->zvec_namecheck = namecheck;
5786 5792          vec->zvec_allow_log = log_history;
5787 5793          vec->zvec_pool_check = pool_check;
5788 5794  }
5789 5795  
5790 5796  /*
5791 5797   * See the block comment at the beginning of this file for details on
5792 5798   * each argument to this function.
5793 5799   */
5794 5800  static void
5795 5801  zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5796 5802      zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5797 5803      zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5798 5804      boolean_t allow_log)
5799 5805  {
5800 5806          zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5801 5807  
5802 5808          ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5803 5809          ASSERT3U(ioc, <, ZFS_IOC_LAST);
5804 5810          ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5805 5811          ASSERT3P(vec->zvec_func, ==, NULL);
5806 5812  
5807 5813          /* if we are logging, the name must be valid */
5808 5814          ASSERT(!allow_log || namecheck != NO_NAME);
5809 5815  
5810 5816          vec->zvec_name = name;
5811 5817          vec->zvec_func = func;
5812 5818          vec->zvec_secpolicy = secpolicy;
5813 5819          vec->zvec_namecheck = namecheck;
5814 5820          vec->zvec_pool_check = pool_check;
5815 5821          vec->zvec_smush_outnvlist = smush_outnvlist;
5816 5822          vec->zvec_allow_log = allow_log;
5817 5823  }
5818 5824  
5819 5825  static void
5820 5826  zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5821 5827      zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5822 5828      zfs_ioc_poolcheck_t pool_check)
5823 5829  {
5824 5830          zfs_ioctl_register_legacy(ioc, func, secpolicy,
5825 5831              POOL_NAME, log_history, pool_check);
5826 5832  }
5827 5833  
5828 5834  static void
5829 5835  zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5830 5836      zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5831 5837  {
5832 5838          zfs_ioctl_register_legacy(ioc, func, secpolicy,
5833 5839              DATASET_NAME, B_FALSE, pool_check);
5834 5840  }
5835 5841  
5836 5842  static void
5837 5843  zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5838 5844  {
5839 5845          zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5840 5846              POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5841 5847  }
5842 5848  
5843 5849  static void
5844 5850  zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5845 5851      zfs_secpolicy_func_t *secpolicy)
5846 5852  {
5847 5853          zfs_ioctl_register_legacy(ioc, func, secpolicy,
5848 5854              NO_NAME, B_FALSE, POOL_CHECK_NONE);
5849 5855  }
5850 5856  
5851 5857  static void
5852 5858  zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5853 5859      zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5854 5860  {
5855 5861          zfs_ioctl_register_legacy(ioc, func, secpolicy,
5856 5862              DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5857 5863  }
5858 5864  
5859 5865  static void
5860 5866  zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5861 5867  {
5862 5868          zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5863 5869              zfs_secpolicy_read);
5864 5870  }
5865 5871  
5866 5872  static void
5867 5873  zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5868 5874      zfs_secpolicy_func_t *secpolicy)
5869 5875  {
5870 5876          zfs_ioctl_register_legacy(ioc, func, secpolicy,
5871 5877              DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5872 5878  }
5873 5879  
5874 5880  static void
5875 5881  zfs_ioctl_init(void)
5876 5882  {
5877 5883          zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5878 5884              zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5879 5885              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5880 5886  
5881 5887          zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5882 5888              zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5883 5889              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5884 5890  
5885 5891          zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5886 5892              zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5887 5893              POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5888 5894  
5889 5895          zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5890 5896              zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5891 5897              POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5892 5898  
5893 5899          zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5894 5900              zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5895 5901              POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5896 5902  
5897 5903          zfs_ioctl_register("create", ZFS_IOC_CREATE,
5898 5904              zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5899 5905              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5900 5906  
5901 5907          zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5902 5908              zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5903 5909              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5904 5910  
5905 5911          zfs_ioctl_register("remap", ZFS_IOC_REMAP,
5906 5912              zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
5907 5913              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5908 5914  
5909 5915          zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5910 5916              zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5911 5917              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5912 5918  
5913 5919          zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5914 5920              zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5915 5921              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5916 5922          zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5917 5923              zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5918 5924              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5919 5925  
5920 5926          zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5921 5927              zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5922 5928              POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5923 5929  
5924 5930          zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5925 5931              zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5926 5932              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5927 5933  
5928 5934          zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5929 5935              zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5930 5936              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5931 5937  
5932 5938          zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5933 5939              zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5934 5940              POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5935 5941  
5936 5942          zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5937 5943              zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5938 5944              POOL_NAME,
5939 5945              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5940 5946  
5941 5947          zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
5942 5948              zfs_ioc_channel_program, zfs_secpolicy_config,
5943 5949              POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
5944 5950              B_TRUE);
5945 5951  
5946 5952          zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
5947 5953              zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
5948 5954              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5949 5955  
5950 5956          zfs_ioctl_register("zpool_discard_checkpoint",
5951 5957              ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
5952 5958              zfs_secpolicy_config, POOL_NAME,
5953 5959              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5954 5960  
5955 5961          zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
5956 5962              zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
5957 5963              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5958 5964  
5959 5965          /* IOCTLS that use the legacy function signature */
5960 5966  
5961 5967          zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5962 5968              zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5963 5969  
5964 5970          zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5965 5971              zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5966 5972          zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5967 5973              zfs_ioc_pool_scan);
5968 5974          zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5969 5975              zfs_ioc_pool_upgrade);
5970 5976          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5971 5977              zfs_ioc_vdev_add);
5972 5978          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5973 5979              zfs_ioc_vdev_remove);
5974 5980          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5975 5981              zfs_ioc_vdev_set_state);
5976 5982          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5977 5983              zfs_ioc_vdev_attach);
5978 5984          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5979 5985              zfs_ioc_vdev_detach);
5980 5986          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5981 5987              zfs_ioc_vdev_setpath);
5982 5988          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5983 5989              zfs_ioc_vdev_setfru);
5984 5990          zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5985 5991              zfs_ioc_pool_set_props);
5986 5992          zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5987 5993              zfs_ioc_vdev_split);
5988 5994          zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5989 5995              zfs_ioc_pool_reguid);
5990 5996  
5991 5997          zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5992 5998              zfs_ioc_pool_configs, zfs_secpolicy_none);
5993 5999          zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5994 6000              zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5995 6001          zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5996 6002              zfs_ioc_inject_fault, zfs_secpolicy_inject);
5997 6003          zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5998 6004              zfs_ioc_clear_fault, zfs_secpolicy_inject);
5999 6005          zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6000 6006              zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6001 6007  
6002 6008          /*
6003 6009           * pool destroy, and export don't log the history as part of
6004 6010           * zfsdev_ioctl, but rather zfs_ioc_pool_export
6005 6011           * does the logging of those commands.
6006 6012           */
6007 6013          zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6008 6014              zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6009 6015          zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6010 6016              zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6011 6017  
6012 6018          zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6013 6019              zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6014 6020          zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6015 6021              zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6016 6022  
6017 6023          zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6018 6024              zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6019 6025          zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6020 6026              zfs_ioc_dsobj_to_dsname,
6021 6027              zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6022 6028          zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6023 6029              zfs_ioc_pool_get_history,
6024 6030              zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6025 6031  
6026 6032          zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6027 6033              zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6028 6034  
6029 6035          zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6030 6036              zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6031 6037          zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6032 6038              zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
6033 6039  
6034 6040          zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6035 6041              zfs_ioc_space_written);
6036 6042          zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6037 6043              zfs_ioc_objset_recvd_props);
6038 6044          zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6039 6045              zfs_ioc_next_obj);
6040 6046          zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6041 6047              zfs_ioc_get_fsacl);
6042 6048          zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6043 6049              zfs_ioc_objset_stats);
6044 6050          zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6045 6051              zfs_ioc_objset_zplprops);
6046 6052          zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6047 6053              zfs_ioc_dataset_list_next);
6048 6054          zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6049 6055              zfs_ioc_snapshot_list_next);
6050 6056          zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6051 6057              zfs_ioc_send_progress);
6052 6058  
6053 6059          zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6054 6060              zfs_ioc_diff, zfs_secpolicy_diff);
6055 6061          zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6056 6062              zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6057 6063          zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6058 6064              zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6059 6065          zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6060 6066              zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6061 6067          zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6062 6068              zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6063 6069          zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6064 6070              zfs_ioc_send, zfs_secpolicy_send);
6065 6071  
6066 6072          zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6067 6073              zfs_secpolicy_none);
6068 6074          zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6069 6075              zfs_secpolicy_destroy);
6070 6076          zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6071 6077              zfs_secpolicy_rename);
6072 6078          zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6073 6079              zfs_secpolicy_recv);
6074 6080          zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6075 6081              zfs_secpolicy_promote);
6076 6082          zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6077 6083              zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6078 6084          zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6079 6085              zfs_secpolicy_set_fsacl);
6080 6086  
6081 6087          zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6082 6088              zfs_secpolicy_share, POOL_CHECK_NONE);
6083 6089          zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6084 6090              zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6085 6091          zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6086 6092              zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6087 6093              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6088 6094          zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6089 6095              zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6090 6096              POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6091 6097  }
6092 6098  
6093 6099  int
6094 6100  pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6095 6101      zfs_ioc_poolcheck_t check)
6096 6102  {
6097 6103          spa_t *spa;
6098 6104          int error;
6099 6105  
6100 6106          ASSERT(type == POOL_NAME || type == DATASET_NAME);
6101 6107  
6102 6108          if (check & POOL_CHECK_NONE)
6103 6109                  return (0);
6104 6110  
6105 6111          error = spa_open(name, &spa, FTAG);
6106 6112          if (error == 0) {
6107 6113                  if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6108 6114                          error = SET_ERROR(EAGAIN);
6109 6115                  else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6110 6116                          error = SET_ERROR(EROFS);
6111 6117                  spa_close(spa, FTAG);
6112 6118          }
6113 6119          return (error);
6114 6120  }
6115 6121  
6116 6122  /*
6117 6123   * Find a free minor number.
6118 6124   */
6119 6125  minor_t
6120 6126  zfsdev_minor_alloc(void)
6121 6127  {
6122 6128          static minor_t last_minor;
6123 6129          minor_t m;
6124 6130  
6125 6131          ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6126 6132  
6127 6133          for (m = last_minor + 1; m != last_minor; m++) {
6128 6134                  if (m > ZFSDEV_MAX_MINOR)
6129 6135                          m = 1;
6130 6136                  if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6131 6137                          last_minor = m;
6132 6138                          return (m);
6133 6139                  }
6134 6140          }
6135 6141  
6136 6142          return (0);
6137 6143  }
6138 6144  
6139 6145  static int
6140 6146  zfs_ctldev_init(dev_t *devp)
6141 6147  {
6142 6148          minor_t minor;
6143 6149          zfs_soft_state_t *zs;
6144 6150  
6145 6151          ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6146 6152          ASSERT(getminor(*devp) == 0);
6147 6153  
6148 6154          minor = zfsdev_minor_alloc();
6149 6155          if (minor == 0)
6150 6156                  return (SET_ERROR(ENXIO));
6151 6157  
6152 6158          if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6153 6159                  return (SET_ERROR(EAGAIN));
6154 6160  
6155 6161          *devp = makedevice(getemajor(*devp), minor);
6156 6162  
6157 6163          zs = ddi_get_soft_state(zfsdev_state, minor);
6158 6164          zs->zss_type = ZSST_CTLDEV;
6159 6165          zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6160 6166  
6161 6167          return (0);
6162 6168  }
6163 6169  
6164 6170  static void
6165 6171  zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6166 6172  {
6167 6173          ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6168 6174  
6169 6175          zfs_onexit_destroy(zo);
6170 6176          ddi_soft_state_free(zfsdev_state, minor);
6171 6177  }
6172 6178  
6173 6179  void *
6174 6180  zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6175 6181  {
6176 6182          zfs_soft_state_t *zp;
6177 6183  
6178 6184          zp = ddi_get_soft_state(zfsdev_state, minor);
6179 6185          if (zp == NULL || zp->zss_type != which)
6180 6186                  return (NULL);
6181 6187  
6182 6188          return (zp->zss_data);
6183 6189  }
6184 6190  
6185 6191  static int
6186 6192  zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6187 6193  {
6188 6194          int error = 0;
6189 6195  
6190 6196          if (getminor(*devp) != 0)
6191 6197                  return (zvol_open(devp, flag, otyp, cr));
6192 6198  
6193 6199          /* This is the control device. Allocate a new minor if requested. */
6194 6200          if (flag & FEXCL) {
6195 6201                  mutex_enter(&zfsdev_state_lock);
6196 6202                  error = zfs_ctldev_init(devp);
6197 6203                  mutex_exit(&zfsdev_state_lock);
6198 6204          }
6199 6205  
6200 6206          return (error);
6201 6207  }
6202 6208  
6203 6209  static int
6204 6210  zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
6205 6211  {
6206 6212          zfs_onexit_t *zo;
6207 6213          minor_t minor = getminor(dev);
6208 6214  
6209 6215          if (minor == 0)
6210 6216                  return (0);
6211 6217  
6212 6218          mutex_enter(&zfsdev_state_lock);
6213 6219          zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6214 6220          if (zo == NULL) {
6215 6221                  mutex_exit(&zfsdev_state_lock);
6216 6222                  return (zvol_close(dev, flag, otyp, cr));
6217 6223          }
6218 6224          zfs_ctldev_destroy(zo, minor);
6219 6225          mutex_exit(&zfsdev_state_lock);
6220 6226  
6221 6227          return (0);
6222 6228  }
6223 6229  
6224 6230  static int
6225 6231  zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
6226 6232  {
6227 6233          zfs_cmd_t *zc;
6228 6234          uint_t vecnum;
6229 6235          int error, rc, len;
6230 6236          minor_t minor = getminor(dev);
6231 6237          const zfs_ioc_vec_t *vec;
6232 6238          char *saved_poolname = NULL;
6233 6239          nvlist_t *innvl = NULL;
6234 6240  
6235 6241          if (minor != 0 &&
6236 6242              zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
6237 6243                  return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6238 6244  
6239 6245          vecnum = cmd - ZFS_IOC_FIRST;
6240 6246          ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6241 6247  
6242 6248          if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6243 6249                  return (SET_ERROR(EINVAL));
6244 6250          vec = &zfs_ioc_vec[vecnum];
6245 6251  
6246 6252          zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6247 6253  
6248 6254          error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6249 6255          if (error != 0) {
6250 6256                  error = SET_ERROR(EFAULT);
6251 6257                  goto out;
6252 6258          }
6253 6259  
6254 6260          zc->zc_iflags = flag & FKIOCTL;
6255 6261          if (zc->zc_nvlist_src_size != 0) {
6256 6262                  error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6257 6263                      zc->zc_iflags, &innvl);
6258 6264                  if (error != 0)
6259 6265                          goto out;
6260 6266          }
6261 6267  
6262 6268          /*
6263 6269           * Ensure that all pool/dataset names are valid before we pass down to
6264 6270           * the lower layers.
6265 6271           */
6266 6272          zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6267 6273          switch (vec->zvec_namecheck) {
6268 6274          case POOL_NAME:
6269 6275                  if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6270 6276                          error = SET_ERROR(EINVAL);
6271 6277                  else
6272 6278                          error = pool_status_check(zc->zc_name,
6273 6279                              vec->zvec_namecheck, vec->zvec_pool_check);
6274 6280                  break;
6275 6281  
6276 6282          case DATASET_NAME:
6277 6283                  if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6278 6284                          error = SET_ERROR(EINVAL);
6279 6285                  else
6280 6286                          error = pool_status_check(zc->zc_name,
6281 6287                              vec->zvec_namecheck, vec->zvec_pool_check);
6282 6288                  break;
6283 6289  
6284 6290          case NO_NAME:
6285 6291                  break;
6286 6292          }
6287 6293  
6288 6294  
6289 6295          if (error == 0)
6290 6296                  error = vec->zvec_secpolicy(zc, innvl, cr);
6291 6297  
6292 6298          if (error != 0)
6293 6299                  goto out;
6294 6300  
6295 6301          /* legacy ioctls can modify zc_name */
6296 6302          len = strcspn(zc->zc_name, "/@#") + 1;
6297 6303          saved_poolname = kmem_alloc(len, KM_SLEEP);
6298 6304          (void) strlcpy(saved_poolname, zc->zc_name, len);
6299 6305  
6300 6306          if (vec->zvec_func != NULL) {
6301 6307                  nvlist_t *outnvl;
6302 6308                  int puterror = 0;
6303 6309                  spa_t *spa;
6304 6310                  nvlist_t *lognv = NULL;
6305 6311  
6306 6312                  ASSERT(vec->zvec_legacy_func == NULL);
6307 6313  
6308 6314                  /*
6309 6315                   * Add the innvl to the lognv before calling the func,
6310 6316                   * in case the func changes the innvl.
6311 6317                   */
6312 6318                  if (vec->zvec_allow_log) {
6313 6319                          lognv = fnvlist_alloc();
6314 6320                          fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6315 6321                              vec->zvec_name);
6316 6322                          if (!nvlist_empty(innvl)) {
6317 6323                                  fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6318 6324                                      innvl);
6319 6325                          }
6320 6326                  }
6321 6327  
6322 6328                  outnvl = fnvlist_alloc();
6323 6329                  error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6324 6330  
6325 6331                  /*
6326 6332                   * Some commands can partially execute, modfiy state, and still
6327 6333                   * return an error.  In these cases, attempt to record what
6328 6334                   * was modified.
6329 6335                   */
6330 6336                  if ((error == 0 ||
6331 6337                      (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6332 6338                      vec->zvec_allow_log &&
6333 6339                      spa_open(zc->zc_name, &spa, FTAG) == 0) {
6334 6340                          if (!nvlist_empty(outnvl)) {
6335 6341                                  fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6336 6342                                      outnvl);
6337 6343                          }
6338 6344                          if (error != 0) {
6339 6345                                  fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6340 6346                                      error);
6341 6347                          }
6342 6348                          (void) spa_history_log_nvl(spa, lognv);
6343 6349                          spa_close(spa, FTAG);
6344 6350                  }
6345 6351                  fnvlist_free(lognv);
6346 6352  
6347 6353                  if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6348 6354                          int smusherror = 0;
6349 6355                          if (vec->zvec_smush_outnvlist) {
6350 6356                                  smusherror = nvlist_smush(outnvl,
6351 6357                                      zc->zc_nvlist_dst_size);
6352 6358                          }
6353 6359                          if (smusherror == 0)
6354 6360                                  puterror = put_nvlist(zc, outnvl);
6355 6361                  }
6356 6362  
6357 6363                  if (puterror != 0)
6358 6364                          error = puterror;
6359 6365  
6360 6366                  nvlist_free(outnvl);
6361 6367          } else {
6362 6368                  error = vec->zvec_legacy_func(zc);
6363 6369          }
6364 6370  
6365 6371  out:
6366 6372          nvlist_free(innvl);
6367 6373          rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6368 6374          if (error == 0 && rc != 0)
6369 6375                  error = SET_ERROR(EFAULT);
6370 6376          if (error == 0 && vec->zvec_allow_log) {
6371 6377                  char *s = tsd_get(zfs_allow_log_key);
6372 6378                  if (s != NULL)
6373 6379                          strfree(s);
6374 6380                  (void) tsd_set(zfs_allow_log_key, saved_poolname);
6375 6381          } else {
6376 6382                  if (saved_poolname != NULL)
6377 6383                          strfree(saved_poolname);
6378 6384          }
6379 6385  
6380 6386          kmem_free(zc, sizeof (zfs_cmd_t));
6381 6387          return (error);
6382 6388  }
6383 6389  
6384 6390  static int
6385 6391  zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6386 6392  {
6387 6393          if (cmd != DDI_ATTACH)
6388 6394                  return (DDI_FAILURE);
6389 6395  
6390 6396          if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6391 6397              DDI_PSEUDO, 0) == DDI_FAILURE)
6392 6398                  return (DDI_FAILURE);
6393 6399  
6394 6400          zfs_dip = dip;
6395 6401  
6396 6402          ddi_report_dev(dip);
6397 6403  
6398 6404          return (DDI_SUCCESS);
6399 6405  }
6400 6406  
6401 6407  static int
6402 6408  zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6403 6409  {
6404 6410          if (spa_busy() || zfs_busy() || zvol_busy())
6405 6411                  return (DDI_FAILURE);
6406 6412  
6407 6413          if (cmd != DDI_DETACH)
6408 6414                  return (DDI_FAILURE);
6409 6415  
6410 6416          zfs_dip = NULL;
6411 6417  
6412 6418          ddi_prop_remove_all(dip);
6413 6419          ddi_remove_minor_node(dip, NULL);
6414 6420  
6415 6421          return (DDI_SUCCESS);
6416 6422  }
6417 6423  
6418 6424  /*ARGSUSED*/
6419 6425  static int
6420 6426  zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6421 6427  {
6422 6428          switch (infocmd) {
6423 6429          case DDI_INFO_DEVT2DEVINFO:
6424 6430                  *result = zfs_dip;
6425 6431                  return (DDI_SUCCESS);
6426 6432  
6427 6433          case DDI_INFO_DEVT2INSTANCE:
6428 6434                  *result = (void *)0;
6429 6435                  return (DDI_SUCCESS);
6430 6436          }
6431 6437  
6432 6438          return (DDI_FAILURE);
6433 6439  }
6434 6440  
6435 6441  /*
6436 6442   * OK, so this is a little weird.
6437 6443   *
6438 6444   * /dev/zfs is the control node, i.e. minor 0.
6439 6445   * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6440 6446   *
6441 6447   * /dev/zfs has basically nothing to do except serve up ioctls,
6442 6448   * so most of the standard driver entry points are in zvol.c.
6443 6449   */
6444 6450  static struct cb_ops zfs_cb_ops = {
6445 6451          zfsdev_open,    /* open */
6446 6452          zfsdev_close,   /* close */
6447 6453          zvol_strategy,  /* strategy */
6448 6454          nodev,          /* print */
6449 6455          zvol_dump,      /* dump */
6450 6456          zvol_read,      /* read */
6451 6457          zvol_write,     /* write */
6452 6458          zfsdev_ioctl,   /* ioctl */
6453 6459          nodev,          /* devmap */
6454 6460          nodev,          /* mmap */
6455 6461          nodev,          /* segmap */
6456 6462          nochpoll,       /* poll */
6457 6463          ddi_prop_op,    /* prop_op */
6458 6464          NULL,           /* streamtab */
6459 6465          D_NEW | D_MP | D_64BIT,         /* Driver compatibility flag */
6460 6466          CB_REV,         /* version */
6461 6467          nodev,          /* async read */
6462 6468          nodev,          /* async write */
6463 6469  };
6464 6470  
6465 6471  static struct dev_ops zfs_dev_ops = {
6466 6472          DEVO_REV,       /* version */
6467 6473          0,              /* refcnt */
6468 6474          zfs_info,       /* info */
6469 6475          nulldev,        /* identify */
6470 6476          nulldev,        /* probe */
6471 6477          zfs_attach,     /* attach */
6472 6478          zfs_detach,     /* detach */
6473 6479          nodev,          /* reset */
6474 6480          &zfs_cb_ops,    /* driver operations */
6475 6481          NULL,           /* no bus operations */
6476 6482          NULL,           /* power */
6477 6483          ddi_quiesce_not_needed, /* quiesce */
6478 6484  };
6479 6485  
6480 6486  static struct modldrv zfs_modldrv = {
6481 6487          &mod_driverops,
6482 6488          "ZFS storage pool",
6483 6489          &zfs_dev_ops
6484 6490  };
6485 6491  
6486 6492  static struct modlinkage modlinkage = {
6487 6493          MODREV_1,
6488 6494          (void *)&zfs_modlfs,
6489 6495          (void *)&zfs_modldrv,
6490 6496          NULL
6491 6497  };
6492 6498  
6493 6499  static void
6494 6500  zfs_allow_log_destroy(void *arg)
6495 6501  {
6496 6502          char *poolname = arg;
6497 6503          strfree(poolname);
6498 6504  }
6499 6505  
6500 6506  int
6501 6507  _init(void)
6502 6508  {
6503 6509          int error;
6504 6510  
6505 6511          spa_init(FREAD | FWRITE);
6506 6512          zfs_init();
6507 6513          zvol_init();
6508 6514          zfs_ioctl_init();
6509 6515  
6510 6516          if ((error = mod_install(&modlinkage)) != 0) {
6511 6517                  zvol_fini();
6512 6518                  zfs_fini();
6513 6519                  spa_fini();
6514 6520                  return (error);
6515 6521          }
6516 6522  
6517 6523          tsd_create(&zfs_fsyncer_key, NULL);
6518 6524          tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6519 6525          tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6520 6526  
6521 6527          error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6522 6528          ASSERT(error == 0);
6523 6529          mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6524 6530  
6525 6531          return (0);
6526 6532  }
6527 6533  
6528 6534  int
6529 6535  _fini(void)
6530 6536  {
6531 6537          int error;
6532 6538  
6533 6539          if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6534 6540                  return (SET_ERROR(EBUSY));
6535 6541  
6536 6542          if ((error = mod_remove(&modlinkage)) != 0)
6537 6543                  return (error);
6538 6544  
6539 6545          zvol_fini();
6540 6546          zfs_fini();
6541 6547          spa_fini();
6542 6548          if (zfs_nfsshare_inited)
6543 6549                  (void) ddi_modclose(nfs_mod);
6544 6550          if (zfs_smbshare_inited)
6545 6551                  (void) ddi_modclose(smbsrv_mod);
6546 6552          if (zfs_nfsshare_inited || zfs_smbshare_inited)
6547 6553                  (void) ddi_modclose(sharefs_mod);
6548 6554  
6549 6555          tsd_destroy(&zfs_fsyncer_key);
6550 6556          ldi_ident_release(zfs_li);
6551 6557          zfs_li = NULL;
6552 6558          mutex_destroy(&zfs_share_lock);
6553 6559  
6554 6560          return (error);
6555 6561  }
6556 6562  
6557 6563  int
6558 6564  _info(struct modinfo *modinfop)
6559 6565  {
6560 6566          return (mod_info(&modlinkage, modinfop));
6561 6567  }
  
    | 
      ↓ open down ↓ | 
    5010 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX