Print this page
    
NEX-5366 Race between unique_insert() and unique_remove() causes ZFS fsid change
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Dan Vatca <dan.vatca@gmail.com>
6160 /usr/lib/fs/zfs/bootinstall should use bootadm
Reviewed by: Igor Kozhukhov <ikozhukhov@gmail.com>
Reviewed by: Adam Števko <adam.stevko@gmail.com>
Reviewed by: Josef Sipek <jeffpc@josefsipek.net>
Approved by: Richard Lowe <richlowe@richlowe.net>
4185 add new cryptographic checksums to ZFS: SHA-512, Skein, Edon-R (NULL is not an int)
6171 dsl_prop_unregister() slows down dataset eviction.
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Prakash Surya <prakash.surya@delphix.com>
Approved by: Dan McDonald <danmcd@omniti.com>
5610 zfs clone from different source and target pools produces coredump
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Approved by: Dan McDonald <danmcd@omniti.com>
re #12585 rb4049 ZFS++ work port - refactoring to improve separation of open/closed code, bug fixes, performance improvements - open code
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/zfs/dsl_dir.c
          +++ new/usr/src/uts/common/fs/zfs/dsl_dir.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   * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23   23   * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  24   24   * Copyright (c) 2013 Martin Matuska. All rights reserved.
  25   25   * Copyright (c) 2014 Joyent, Inc. All rights reserved.
  26   26   * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
  27   27   * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
  28   28   */
  29   29  
  30   30  #include <sys/dmu.h>
  31   31  #include <sys/dmu_objset.h>
  32   32  #include <sys/dmu_tx.h>
  33   33  #include <sys/dsl_dataset.h>
  34   34  #include <sys/dsl_dir.h>
  35   35  #include <sys/dsl_prop.h>
  36   36  #include <sys/dsl_synctask.h>
  37   37  #include <sys/dsl_deleg.h>
  38   38  #include <sys/dmu_impl.h>
  39   39  #include <sys/spa.h>
  40   40  #include <sys/metaslab.h>
  41   41  #include <sys/zap.h>
  42   42  #include <sys/zio.h>
  43   43  #include <sys/arc.h>
  44   44  #include <sys/sunddi.h>
  45   45  #include <sys/zfeature.h>
  46   46  #include <sys/policy.h>
  47   47  #include <sys/zfs_znode.h>
  48   48  #include "zfs_namecheck.h"
  49   49  #include "zfs_prop.h"
  50   50  
  51   51  /*
  52   52   * Filesystem and Snapshot Limits
  53   53   * ------------------------------
  54   54   *
  55   55   * These limits are used to restrict the number of filesystems and/or snapshots
  56   56   * that can be created at a given level in the tree or below. A typical
  57   57   * use-case is with a delegated dataset where the administrator wants to ensure
  58   58   * that a user within the zone is not creating too many additional filesystems
  59   59   * or snapshots, even though they're not exceeding their space quota.
  60   60   *
  61   61   * The filesystem and snapshot counts are stored as extensible properties. This
  62   62   * capability is controlled by a feature flag and must be enabled to be used.
  63   63   * Once enabled, the feature is not active until the first limit is set. At
  64   64   * that point, future operations to create/destroy filesystems or snapshots
  65   65   * will validate and update the counts.
  66   66   *
  67   67   * Because the count properties will not exist before the feature is active,
  68   68   * the counts are updated when a limit is first set on an uninitialized
  69   69   * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
  70   70   * all of the nested filesystems/snapshots. Thus, a new leaf node has a
  71   71   * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
  72   72   * snapshot count properties on a node indicate uninitialized counts on that
  73   73   * node.) When first setting a limit on an uninitialized node, the code starts
  74   74   * at the filesystem with the new limit and descends into all sub-filesystems
  75   75   * to add the count properties.
  76   76   *
  77   77   * In practice this is lightweight since a limit is typically set when the
  78   78   * filesystem is created and thus has no children. Once valid, changing the
  79   79   * limit value won't require a re-traversal since the counts are already valid.
  80   80   * When recursively fixing the counts, if a node with a limit is encountered
  81   81   * during the descent, the counts are known to be valid and there is no need to
  82   82   * descend into that filesystem's children. The counts on filesystems above the
  83   83   * one with the new limit will still be uninitialized, unless a limit is
  84   84   * eventually set on one of those filesystems. The counts are always recursively
  85   85   * updated when a limit is set on a dataset, unless there is already a limit.
  86   86   * When a new limit value is set on a filesystem with an existing limit, it is
  87   87   * possible for the new limit to be less than the current count at that level
  88   88   * since a user who can change the limit is also allowed to exceed the limit.
  89   89   *
  90   90   * Once the feature is active, then whenever a filesystem or snapshot is
  91   91   * created, the code recurses up the tree, validating the new count against the
  92   92   * limit at each initialized level. In practice, most levels will not have a
  93   93   * limit set. If there is a limit at any initialized level up the tree, the
  94   94   * check must pass or the creation will fail. Likewise, when a filesystem or
  95   95   * snapshot is destroyed, the counts are recursively adjusted all the way up
  96   96   * the initizized nodes in the tree. Renaming a filesystem into different point
  97   97   * in the tree will first validate, then update the counts on each branch up to
  98   98   * the common ancestor. A receive will also validate the counts and then update
  99   99   * them.
 100  100   *
 101  101   * An exception to the above behavior is that the limit is not enforced if the
 102  102   * user has permission to modify the limit. This is primarily so that
 103  103   * recursive snapshots in the global zone always work. We want to prevent a
 104  104   * denial-of-service in which a lower level delegated dataset could max out its
 105  105   * limit and thus block recursive snapshots from being taken in the global zone.
 106  106   * Because of this, it is possible for the snapshot count to be over the limit
 107  107   * and snapshots taken in the global zone could cause a lower level dataset to
 108  108   * hit or exceed its limit. The administrator taking the global zone recursive
 109  109   * snapshot should be aware of this side-effect and behave accordingly.
 110  110   * For consistency, the filesystem limit is also not enforced if the user can
 111  111   * modify the limit.
 112  112   *
 113  113   * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
 114  114   * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
 115  115   * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
 116  116   * dsl_dir_init_fs_ss_count().
 117  117   *
 118  118   * There is a special case when we receive a filesystem that already exists. In
 119  119   * this case a temporary clone name of %X is created (see dmu_recv_begin). We
  
    | 
      ↓ open down ↓ | 
    119 lines elided | 
    
      ↑ open up ↑ | 
  
 120  120   * never update the filesystem counts for temporary clones.
 121  121   *
 122  122   * Likewise, we do not update the snapshot counts for temporary snapshots,
 123  123   * such as those created by zfs diff.
 124  124   */
 125  125  
 126  126  extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
 127  127  
 128  128  static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
 129  129  
 130      -typedef struct ddulrt_arg {
 131      -        dsl_dir_t       *ddulrta_dd;
 132      -        uint64_t        ddlrta_txg;
 133      -} ddulrt_arg_t;
 134      -
 135  130  static void
 136  131  dsl_dir_evict_async(void *dbu)
 137  132  {
 138  133          dsl_dir_t *dd = dbu;
 139  134          dsl_pool_t *dp = dd->dd_pool;
 140  135          int t;
 141  136  
 142  137          dd->dd_dbuf = NULL;
 143  138  
 144  139          for (t = 0; t < TXG_SIZE; t++) {
 145  140                  ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
 146  141                  ASSERT(dd->dd_tempreserved[t] == 0);
 147  142                  ASSERT(dd->dd_space_towrite[t] == 0);
 148  143          }
 149  144  
 150  145          if (dd->dd_parent)
 151  146                  dsl_dir_async_rele(dd->dd_parent, dd);
 152  147  
 153  148          spa_async_close(dd->dd_pool->dp_spa, dd);
 154  149  
 155  150          dsl_prop_fini(dd);
 156  151          mutex_destroy(&dd->dd_lock);
 157  152          kmem_free(dd, sizeof (dsl_dir_t));
 158  153  }
 159  154  
 160  155  int
 161  156  dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
 162  157      const char *tail, void *tag, dsl_dir_t **ddp)
 163  158  {
 164  159          dmu_buf_t *dbuf;
 165  160          dsl_dir_t *dd;
 166  161          int err;
 167  162  
 168  163          ASSERT(dsl_pool_config_held(dp));
 169  164  
 170  165          err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
 171  166          if (err != 0)
 172  167                  return (err);
 173  168          dd = dmu_buf_get_user(dbuf);
 174  169  #ifdef ZFS_DEBUG
 175  170          {
 176  171                  dmu_object_info_t doi;
 177  172                  dmu_object_info_from_db(dbuf, &doi);
 178  173                  ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
 179  174                  ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
 180  175          }
 181  176  #endif
 182  177          if (dd == NULL) {
 183  178                  dsl_dir_t *winner;
 184  179  
 185  180                  dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
 186  181                  dd->dd_object = ddobj;
 187  182                  dd->dd_dbuf = dbuf;
 188  183                  dd->dd_pool = dp;
 189  184                  mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
 190  185                  dsl_prop_init(dd);
 191  186  
 192  187                  dsl_dir_snap_cmtime_update(dd);
 193  188  
 194  189                  if (dsl_dir_phys(dd)->dd_parent_obj) {
 195  190                          err = dsl_dir_hold_obj(dp,
 196  191                              dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
 197  192                              &dd->dd_parent);
 198  193                          if (err != 0)
 199  194                                  goto errout;
 200  195                          if (tail) {
 201  196  #ifdef ZFS_DEBUG
 202  197                                  uint64_t foundobj;
 203  198  
 204  199                                  err = zap_lookup(dp->dp_meta_objset,
 205  200                                      dsl_dir_phys(dd->dd_parent)->
 206  201                                      dd_child_dir_zapobj, tail,
 207  202                                      sizeof (foundobj), 1, &foundobj);
 208  203                                  ASSERT(err || foundobj == ddobj);
 209  204  #endif
 210  205                                  (void) strcpy(dd->dd_myname, tail);
 211  206                          } else {
 212  207                                  err = zap_value_search(dp->dp_meta_objset,
 213  208                                      dsl_dir_phys(dd->dd_parent)->
 214  209                                      dd_child_dir_zapobj,
 215  210                                      ddobj, 0, dd->dd_myname);
 216  211                          }
 217  212                          if (err != 0)
 218  213                                  goto errout;
 219  214                  } else {
 220  215                          (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
 221  216                  }
 222  217  
 223  218                  if (dsl_dir_is_clone(dd)) {
 224  219                          dmu_buf_t *origin_bonus;
 225  220                          dsl_dataset_phys_t *origin_phys;
 226  221  
 227  222                          /*
 228  223                           * We can't open the origin dataset, because
 229  224                           * that would require opening this dsl_dir.
 230  225                           * Just look at its phys directly instead.
 231  226                           */
 232  227                          err = dmu_bonus_hold(dp->dp_meta_objset,
 233  228                              dsl_dir_phys(dd)->dd_origin_obj, FTAG,
 234  229                              &origin_bonus);
 235  230                          if (err != 0)
 236  231                                  goto errout;
 237  232                          origin_phys = origin_bonus->db_data;
 238  233                          dd->dd_origin_txg =
 239  234                              origin_phys->ds_creation_txg;
 240  235                          dmu_buf_rele(origin_bonus, FTAG);
 241  236                  }
 242  237  
 243  238                  dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
 244  239                      &dd->dd_dbuf);
 245  240                  winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
 246  241                  if (winner != NULL) {
 247  242                          if (dd->dd_parent)
 248  243                                  dsl_dir_rele(dd->dd_parent, dd);
 249  244                          dsl_prop_fini(dd);
 250  245                          mutex_destroy(&dd->dd_lock);
 251  246                          kmem_free(dd, sizeof (dsl_dir_t));
 252  247                          dd = winner;
 253  248                  } else {
 254  249                          spa_open_ref(dp->dp_spa, dd);
 255  250                  }
 256  251          }
 257  252  
 258  253          /*
 259  254           * The dsl_dir_t has both open-to-close and instantiate-to-evict
 260  255           * holds on the spa.  We need the open-to-close holds because
 261  256           * otherwise the spa_refcnt wouldn't change when we open a
 262  257           * dir which the spa also has open, so we could incorrectly
 263  258           * think it was OK to unload/export/destroy the pool.  We need
 264  259           * the instantiate-to-evict hold because the dsl_dir_t has a
 265  260           * pointer to the dd_pool, which has a pointer to the spa_t.
 266  261           */
 267  262          spa_open_ref(dp->dp_spa, tag);
 268  263          ASSERT3P(dd->dd_pool, ==, dp);
 269  264          ASSERT3U(dd->dd_object, ==, ddobj);
 270  265          ASSERT3P(dd->dd_dbuf, ==, dbuf);
 271  266          *ddp = dd;
 272  267          return (0);
 273  268  
 274  269  errout:
 275  270          if (dd->dd_parent)
 276  271                  dsl_dir_rele(dd->dd_parent, dd);
 277  272          dsl_prop_fini(dd);
 278  273          mutex_destroy(&dd->dd_lock);
 279  274          kmem_free(dd, sizeof (dsl_dir_t));
 280  275          dmu_buf_rele(dbuf, tag);
 281  276          return (err);
 282  277  }
 283  278  
 284  279  void
 285  280  dsl_dir_rele(dsl_dir_t *dd, void *tag)
 286  281  {
 287  282          dprintf_dd(dd, "%s\n", "");
 288  283          spa_close(dd->dd_pool->dp_spa, tag);
 289  284          dmu_buf_rele(dd->dd_dbuf, tag);
 290  285  }
 291  286  
 292  287  /*
 293  288   * Remove a reference to the given dsl dir that is being asynchronously
 294  289   * released.  Async releases occur from a taskq performing eviction of
 295  290   * dsl datasets and dirs.  This process is identical to a normal release
 296  291   * with the exception of using the async API for releasing the reference on
 297  292   * the spa.
 298  293   */
 299  294  void
 300  295  dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
 301  296  {
 302  297          dprintf_dd(dd, "%s\n", "");
 303  298          spa_async_close(dd->dd_pool->dp_spa, tag);
 304  299          dmu_buf_rele(dd->dd_dbuf, tag);
 305  300  }
 306  301  
 307  302  /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
 308  303  void
 309  304  dsl_dir_name(dsl_dir_t *dd, char *buf)
 310  305  {
 311  306          if (dd->dd_parent) {
 312  307                  dsl_dir_name(dd->dd_parent, buf);
 313  308                  VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
 314  309                      ZFS_MAX_DATASET_NAME_LEN);
 315  310          } else {
 316  311                  buf[0] = '\0';
 317  312          }
 318  313          if (!MUTEX_HELD(&dd->dd_lock)) {
 319  314                  /*
 320  315                   * recursive mutex so that we can use
 321  316                   * dprintf_dd() with dd_lock held
 322  317                   */
 323  318                  mutex_enter(&dd->dd_lock);
 324  319                  VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
 325  320                      <, ZFS_MAX_DATASET_NAME_LEN);
 326  321                  mutex_exit(&dd->dd_lock);
 327  322          } else {
 328  323                  VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
 329  324                      <, ZFS_MAX_DATASET_NAME_LEN);
 330  325          }
 331  326  }
 332  327  
 333  328  /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
 334  329  int
 335  330  dsl_dir_namelen(dsl_dir_t *dd)
 336  331  {
 337  332          int result = 0;
 338  333  
 339  334          if (dd->dd_parent) {
 340  335                  /* parent's name + 1 for the "/" */
 341  336                  result = dsl_dir_namelen(dd->dd_parent) + 1;
 342  337          }
 343  338  
 344  339          if (!MUTEX_HELD(&dd->dd_lock)) {
 345  340                  /* see dsl_dir_name */
 346  341                  mutex_enter(&dd->dd_lock);
 347  342                  result += strlen(dd->dd_myname);
 348  343                  mutex_exit(&dd->dd_lock);
 349  344          } else {
 350  345                  result += strlen(dd->dd_myname);
 351  346          }
 352  347  
 353  348          return (result);
 354  349  }
 355  350  
 356  351  static int
 357  352  getcomponent(const char *path, char *component, const char **nextp)
 358  353  {
 359  354          char *p;
 360  355  
 361  356          if ((path == NULL) || (path[0] == '\0'))
 362  357                  return (SET_ERROR(ENOENT));
 363  358          /* This would be a good place to reserve some namespace... */
 364  359          p = strpbrk(path, "/@");
 365  360          if (p && (p[1] == '/' || p[1] == '@')) {
 366  361                  /* two separators in a row */
 367  362                  return (SET_ERROR(EINVAL));
 368  363          }
 369  364          if (p == NULL || p == path) {
 370  365                  /*
 371  366                   * if the first thing is an @ or /, it had better be an
 372  367                   * @ and it had better not have any more ats or slashes,
 373  368                   * and it had better have something after the @.
 374  369                   */
 375  370                  if (p != NULL &&
 376  371                      (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
 377  372                          return (SET_ERROR(EINVAL));
 378  373                  if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
 379  374                          return (SET_ERROR(ENAMETOOLONG));
 380  375                  (void) strcpy(component, path);
 381  376                  p = NULL;
 382  377          } else if (p[0] == '/') {
 383  378                  if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
 384  379                          return (SET_ERROR(ENAMETOOLONG));
 385  380                  (void) strncpy(component, path, p - path);
 386  381                  component[p - path] = '\0';
 387  382                  p++;
 388  383          } else if (p[0] == '@') {
 389  384                  /*
 390  385                   * if the next separator is an @, there better not be
 391  386                   * any more slashes.
 392  387                   */
 393  388                  if (strchr(path, '/'))
 394  389                          return (SET_ERROR(EINVAL));
 395  390                  if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
 396  391                          return (SET_ERROR(ENAMETOOLONG));
 397  392                  (void) strncpy(component, path, p - path);
 398  393                  component[p - path] = '\0';
 399  394          } else {
 400  395                  panic("invalid p=%p", (void *)p);
 401  396          }
 402  397          *nextp = p;
 403  398          return (0);
 404  399  }
 405  400  
 406  401  /*
 407  402   * Return the dsl_dir_t, and possibly the last component which couldn't
 408  403   * be found in *tail.  The name must be in the specified dsl_pool_t.  This
 409  404   * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
 410  405   * path is bogus, or if tail==NULL and we couldn't parse the whole name.
 411  406   * (*tail)[0] == '@' means that the last component is a snapshot.
 412  407   */
 413  408  int
 414  409  dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
 415  410      dsl_dir_t **ddp, const char **tailp)
 416  411  {
 417  412          char buf[ZFS_MAX_DATASET_NAME_LEN];
 418  413          const char *spaname, *next, *nextnext = NULL;
 419  414          int err;
 420  415          dsl_dir_t *dd;
 421  416          uint64_t ddobj;
 422  417  
 423  418          err = getcomponent(name, buf, &next);
 424  419          if (err != 0)
 425  420                  return (err);
 426  421  
 427  422          /* Make sure the name is in the specified pool. */
 428  423          spaname = spa_name(dp->dp_spa);
 429  424          if (strcmp(buf, spaname) != 0)
 430  425                  return (SET_ERROR(EXDEV));
 431  426  
 432  427          ASSERT(dsl_pool_config_held(dp));
 433  428  
 434  429          err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
 435  430          if (err != 0) {
 436  431                  return (err);
 437  432          }
 438  433  
 439  434          while (next != NULL) {
 440  435                  dsl_dir_t *child_dd;
 441  436                  err = getcomponent(next, buf, &nextnext);
 442  437                  if (err != 0)
 443  438                          break;
 444  439                  ASSERT(next[0] != '\0');
 445  440                  if (next[0] == '@')
 446  441                          break;
 447  442                  dprintf("looking up %s in obj%lld\n",
 448  443                      buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
 449  444  
 450  445                  err = zap_lookup(dp->dp_meta_objset,
 451  446                      dsl_dir_phys(dd)->dd_child_dir_zapobj,
 452  447                      buf, sizeof (ddobj), 1, &ddobj);
 453  448                  if (err != 0) {
 454  449                          if (err == ENOENT)
 455  450                                  err = 0;
 456  451                          break;
 457  452                  }
 458  453  
 459  454                  err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
 460  455                  if (err != 0)
 461  456                          break;
 462  457                  dsl_dir_rele(dd, tag);
 463  458                  dd = child_dd;
 464  459                  next = nextnext;
 465  460          }
 466  461  
 467  462          if (err != 0) {
 468  463                  dsl_dir_rele(dd, tag);
 469  464                  return (err);
 470  465          }
 471  466  
 472  467          /*
 473  468           * It's an error if there's more than one component left, or
 474  469           * tailp==NULL and there's any component left.
 475  470           */
 476  471          if (next != NULL &&
 477  472              (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
 478  473                  /* bad path name */
 479  474                  dsl_dir_rele(dd, tag);
 480  475                  dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
 481  476                  err = SET_ERROR(ENOENT);
 482  477          }
 483  478          if (tailp != NULL)
 484  479                  *tailp = next;
 485  480          *ddp = dd;
 486  481          return (err);
 487  482  }
 488  483  
 489  484  /*
 490  485   * If the counts are already initialized for this filesystem and its
 491  486   * descendants then do nothing, otherwise initialize the counts.
 492  487   *
 493  488   * The counts on this filesystem, and those below, may be uninitialized due to
 494  489   * either the use of a pre-existing pool which did not support the
 495  490   * filesystem/snapshot limit feature, or one in which the feature had not yet
 496  491   * been enabled.
 497  492   *
 498  493   * Recursively descend the filesystem tree and update the filesystem/snapshot
 499  494   * counts on each filesystem below, then update the cumulative count on the
 500  495   * current filesystem. If the filesystem already has a count set on it,
 501  496   * then we know that its counts, and the counts on the filesystems below it,
 502  497   * are already correct, so we don't have to update this filesystem.
 503  498   */
 504  499  static void
 505  500  dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
 506  501  {
 507  502          uint64_t my_fs_cnt = 0;
 508  503          uint64_t my_ss_cnt = 0;
 509  504          dsl_pool_t *dp = dd->dd_pool;
 510  505          objset_t *os = dp->dp_meta_objset;
 511  506          zap_cursor_t *zc;
 512  507          zap_attribute_t *za;
 513  508          dsl_dataset_t *ds;
 514  509  
 515  510          ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
 516  511          ASSERT(dsl_pool_config_held(dp));
 517  512          ASSERT(dmu_tx_is_syncing(tx));
 518  513  
 519  514          dsl_dir_zapify(dd, tx);
 520  515  
 521  516          /*
 522  517           * If the filesystem count has already been initialized then we
 523  518           * don't need to recurse down any further.
 524  519           */
 525  520          if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
 526  521                  return;
 527  522  
 528  523          zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
 529  524          za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
 530  525  
 531  526          /* Iterate my child dirs */
 532  527          for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
 533  528              zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
 534  529                  dsl_dir_t *chld_dd;
 535  530                  uint64_t count;
 536  531  
 537  532                  VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
 538  533                      &chld_dd));
 539  534  
 540  535                  /*
 541  536                   * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
 542  537                   * temporary datasets.
 543  538                   */
 544  539                  if (chld_dd->dd_myname[0] == '$' ||
 545  540                      chld_dd->dd_myname[0] == '%') {
 546  541                          dsl_dir_rele(chld_dd, FTAG);
 547  542                          continue;
 548  543                  }
 549  544  
 550  545                  my_fs_cnt++;    /* count this child */
 551  546  
 552  547                  dsl_dir_init_fs_ss_count(chld_dd, tx);
 553  548  
 554  549                  VERIFY0(zap_lookup(os, chld_dd->dd_object,
 555  550                      DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
 556  551                  my_fs_cnt += count;
 557  552                  VERIFY0(zap_lookup(os, chld_dd->dd_object,
 558  553                      DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
 559  554                  my_ss_cnt += count;
 560  555  
 561  556                  dsl_dir_rele(chld_dd, FTAG);
 562  557          }
 563  558          zap_cursor_fini(zc);
 564  559          /* Count my snapshots (we counted children's snapshots above) */
 565  560          VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
 566  561              dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
 567  562  
 568  563          for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
 569  564              zap_cursor_retrieve(zc, za) == 0;
 570  565              zap_cursor_advance(zc)) {
 571  566                  /* Don't count temporary snapshots */
 572  567                  if (za->za_name[0] != '%')
 573  568                          my_ss_cnt++;
 574  569          }
 575  570          zap_cursor_fini(zc);
 576  571  
 577  572          dsl_dataset_rele(ds, FTAG);
 578  573  
 579  574          kmem_free(zc, sizeof (zap_cursor_t));
 580  575          kmem_free(za, sizeof (zap_attribute_t));
 581  576  
 582  577          /* we're in a sync task, update counts */
 583  578          dmu_buf_will_dirty(dd->dd_dbuf, tx);
 584  579          VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
 585  580              sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
 586  581          VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
 587  582              sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
 588  583  }
 589  584  
 590  585  static int
 591  586  dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
 592  587  {
 593  588          char *ddname = (char *)arg;
 594  589          dsl_pool_t *dp = dmu_tx_pool(tx);
 595  590          dsl_dataset_t *ds;
 596  591          dsl_dir_t *dd;
 597  592          int error;
 598  593  
 599  594          error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
 600  595          if (error != 0)
 601  596                  return (error);
 602  597  
 603  598          if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
 604  599                  dsl_dataset_rele(ds, FTAG);
 605  600                  return (SET_ERROR(ENOTSUP));
 606  601          }
 607  602  
 608  603          dd = ds->ds_dir;
 609  604          if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
 610  605              dsl_dir_is_zapified(dd) &&
 611  606              zap_contains(dp->dp_meta_objset, dd->dd_object,
 612  607              DD_FIELD_FILESYSTEM_COUNT) == 0) {
 613  608                  dsl_dataset_rele(ds, FTAG);
 614  609                  return (SET_ERROR(EALREADY));
 615  610          }
 616  611  
 617  612          dsl_dataset_rele(ds, FTAG);
 618  613          return (0);
 619  614  }
 620  615  
 621  616  static void
 622  617  dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
 623  618  {
 624  619          char *ddname = (char *)arg;
 625  620          dsl_pool_t *dp = dmu_tx_pool(tx);
 626  621          dsl_dataset_t *ds;
 627  622          spa_t *spa;
 628  623  
 629  624          VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
 630  625  
 631  626          spa = dsl_dataset_get_spa(ds);
 632  627  
 633  628          if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
 634  629                  /*
 635  630                   * Since the feature was not active and we're now setting a
 636  631                   * limit, increment the feature-active counter so that the
 637  632                   * feature becomes active for the first time.
 638  633                   *
 639  634                   * We are already in a sync task so we can update the MOS.
 640  635                   */
 641  636                  spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
 642  637          }
 643  638  
 644  639          /*
 645  640           * Since we are now setting a non-UINT64_MAX limit on the filesystem,
 646  641           * we need to ensure the counts are correct. Descend down the tree from
 647  642           * this point and update all of the counts to be accurate.
 648  643           */
 649  644          dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
 650  645  
 651  646          dsl_dataset_rele(ds, FTAG);
 652  647  }
 653  648  
 654  649  /*
 655  650   * Make sure the feature is enabled and activate it if necessary.
 656  651   * Since we're setting a limit, ensure the on-disk counts are valid.
 657  652   * This is only called by the ioctl path when setting a limit value.
 658  653   *
 659  654   * We do not need to validate the new limit, since users who can change the
 660  655   * limit are also allowed to exceed the limit.
 661  656   */
 662  657  int
 663  658  dsl_dir_activate_fs_ss_limit(const char *ddname)
 664  659  {
 665  660          int error;
 666  661  
 667  662          error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
 668  663              dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
 669  664              ZFS_SPACE_CHECK_RESERVED);
 670  665  
 671  666          if (error == EALREADY)
 672  667                  error = 0;
 673  668  
 674  669          return (error);
 675  670  }
 676  671  
 677  672  /*
 678  673   * Used to determine if the filesystem_limit or snapshot_limit should be
 679  674   * enforced. We allow the limit to be exceeded if the user has permission to
 680  675   * write the property value. We pass in the creds that we got in the open
 681  676   * context since we will always be the GZ root in syncing context. We also have
 682  677   * to handle the case where we are allowed to change the limit on the current
 683  678   * dataset, but there may be another limit in the tree above.
 684  679   *
 685  680   * We can never modify these two properties within a non-global zone. In
 686  681   * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
 687  682   * can't use that function since we are already holding the dp_config_rwlock.
 688  683   * In addition, we already have the dd and dealing with snapshots is simplified
 689  684   * in this code.
 690  685   */
 691  686  
 692  687  typedef enum {
 693  688          ENFORCE_ALWAYS,
 694  689          ENFORCE_NEVER,
 695  690          ENFORCE_ABOVE
 696  691  } enforce_res_t;
 697  692  
 698  693  static enforce_res_t
 699  694  dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
 700  695  {
 701  696          enforce_res_t enforce = ENFORCE_ALWAYS;
 702  697          uint64_t obj;
 703  698          dsl_dataset_t *ds;
 704  699          uint64_t zoned;
 705  700  
 706  701          ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
 707  702              prop == ZFS_PROP_SNAPSHOT_LIMIT);
 708  703  
 709  704  #ifdef _KERNEL
 710  705          if (crgetzoneid(cr) != GLOBAL_ZONEID)
 711  706                  return (ENFORCE_ALWAYS);
 712  707  
 713  708          if (secpolicy_zfs(cr) == 0)
 714  709                  return (ENFORCE_NEVER);
 715  710  #endif
 716  711  
 717  712          if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
 718  713                  return (ENFORCE_ALWAYS);
 719  714  
 720  715          ASSERT(dsl_pool_config_held(dd->dd_pool));
 721  716  
 722  717          if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
 723  718                  return (ENFORCE_ALWAYS);
 724  719  
 725  720          if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
 726  721                  /* Only root can access zoned fs's from the GZ */
  
    | 
      ↓ open down ↓ | 
    582 lines elided | 
    
      ↑ open up ↑ | 
  
 727  722                  enforce = ENFORCE_ALWAYS;
 728  723          } else {
 729  724                  if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
 730  725                          enforce = ENFORCE_ABOVE;
 731  726          }
 732  727  
 733  728          dsl_dataset_rele(ds, FTAG);
 734  729          return (enforce);
 735  730  }
 736  731  
 737      -static void
 738      -dsl_dir_update_last_remap_txg_sync(void *varg, dmu_tx_t *tx)
 739      -{
 740      -        ddulrt_arg_t *arg = varg;
 741      -        uint64_t last_remap_txg;
 742      -        dsl_dir_t *dd = arg->ddulrta_dd;
 743      -        objset_t *mos = dd->dd_pool->dp_meta_objset;
 744      -
 745      -        dsl_dir_zapify(dd, tx);
 746      -        if (zap_lookup(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
 747      -            sizeof (last_remap_txg), 1, &last_remap_txg) != 0 ||
 748      -            last_remap_txg < arg->ddlrta_txg) {
 749      -                VERIFY0(zap_update(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
 750      -                    sizeof (arg->ddlrta_txg), 1, &arg->ddlrta_txg, tx));
 751      -        }
 752      -}
 753      -
 754      -int
 755      -dsl_dir_update_last_remap_txg(dsl_dir_t *dd, uint64_t txg)
 756      -{
 757      -        ddulrt_arg_t arg;
 758      -        arg.ddulrta_dd = dd;
 759      -        arg.ddlrta_txg = txg;
 760      -
 761      -        return (dsl_sync_task(spa_name(dd->dd_pool->dp_spa),
 762      -            NULL, dsl_dir_update_last_remap_txg_sync, &arg,
 763      -            1, ZFS_SPACE_CHECK_RESERVED));
 764      -}
 765      -
 766  732  /*
 767  733   * Check if adding additional child filesystem(s) would exceed any filesystem
 768  734   * limits or adding additional snapshot(s) would exceed any snapshot limits.
 769  735   * The prop argument indicates which limit to check.
 770  736   *
 771  737   * Note that all filesystem limits up to the root (or the highest
 772  738   * initialized) filesystem or the given ancestor must be satisfied.
 773  739   */
 774  740  int
 775  741  dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
 776  742      dsl_dir_t *ancestor, cred_t *cr)
 777  743  {
 778  744          objset_t *os = dd->dd_pool->dp_meta_objset;
 779  745          uint64_t limit, count;
 780  746          char *count_prop;
 781  747          enforce_res_t enforce;
 782  748          int err = 0;
 783  749  
 784  750          ASSERT(dsl_pool_config_held(dd->dd_pool));
 785  751          ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
 786  752              prop == ZFS_PROP_SNAPSHOT_LIMIT);
 787  753  
 788  754          /*
 789  755           * If we're allowed to change the limit, don't enforce the limit
 790  756           * e.g. this can happen if a snapshot is taken by an administrative
 791  757           * user in the global zone (i.e. a recursive snapshot by root).
 792  758           * However, we must handle the case of delegated permissions where we
 793  759           * are allowed to change the limit on the current dataset, but there
 794  760           * is another limit in the tree above.
 795  761           */
 796  762          enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
 797  763          if (enforce == ENFORCE_NEVER)
 798  764                  return (0);
 799  765  
 800  766          /*
 801  767           * e.g. if renaming a dataset with no snapshots, count adjustment
 802  768           * is 0.
 803  769           */
 804  770          if (delta == 0)
 805  771                  return (0);
 806  772  
 807  773          if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
 808  774                  /*
 809  775                   * We don't enforce the limit for temporary snapshots. This is
 810  776                   * indicated by a NULL cred_t argument.
 811  777                   */
 812  778                  if (cr == NULL)
 813  779                          return (0);
 814  780  
 815  781                  count_prop = DD_FIELD_SNAPSHOT_COUNT;
 816  782          } else {
 817  783                  count_prop = DD_FIELD_FILESYSTEM_COUNT;
 818  784          }
 819  785  
 820  786          /*
 821  787           * If an ancestor has been provided, stop checking the limit once we
 822  788           * hit that dir. We need this during rename so that we don't overcount
 823  789           * the check once we recurse up to the common ancestor.
 824  790           */
 825  791          if (ancestor == dd)
 826  792                  return (0);
 827  793  
 828  794          /*
 829  795           * If we hit an uninitialized node while recursing up the tree, we can
 830  796           * stop since we know there is no limit here (or above). The counts are
 831  797           * not valid on this node and we know we won't touch this node's counts.
 832  798           */
 833  799          if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
 834  800              count_prop, sizeof (count), 1, &count) == ENOENT)
 835  801                  return (0);
 836  802  
 837  803          err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
 838  804              B_FALSE);
 839  805          if (err != 0)
 840  806                  return (err);
 841  807  
 842  808          /* Is there a limit which we've hit? */
 843  809          if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
 844  810                  return (SET_ERROR(EDQUOT));
 845  811  
 846  812          if (dd->dd_parent != NULL)
 847  813                  err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
 848  814                      ancestor, cr);
 849  815  
 850  816          return (err);
 851  817  }
 852  818  
 853  819  /*
 854  820   * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
 855  821   * parents. When a new filesystem/snapshot is created, increment the count on
 856  822   * all parents, and when a filesystem/snapshot is destroyed, decrement the
 857  823   * count.
 858  824   */
 859  825  void
 860  826  dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
 861  827      dmu_tx_t *tx)
 862  828  {
 863  829          int err;
 864  830          objset_t *os = dd->dd_pool->dp_meta_objset;
 865  831          uint64_t count;
 866  832  
 867  833          ASSERT(dsl_pool_config_held(dd->dd_pool));
 868  834          ASSERT(dmu_tx_is_syncing(tx));
 869  835          ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
 870  836              strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
 871  837  
 872  838          /*
 873  839           * When we receive an incremental stream into a filesystem that already
 874  840           * exists, a temporary clone is created.  We don't count this temporary
 875  841           * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
 876  842           * $MOS & $ORIGIN) objsets.
 877  843           */
 878  844          if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
 879  845              strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
 880  846                  return;
 881  847  
 882  848          /*
 883  849           * e.g. if renaming a dataset with no snapshots, count adjustment is 0
 884  850           */
 885  851          if (delta == 0)
 886  852                  return;
 887  853  
 888  854          /*
 889  855           * If we hit an uninitialized node while recursing up the tree, we can
 890  856           * stop since we know the counts are not valid on this node and we
 891  857           * know we shouldn't touch this node's counts. An uninitialized count
 892  858           * on the node indicates that either the feature has not yet been
 893  859           * activated or there are no limits on this part of the tree.
 894  860           */
 895  861          if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
 896  862              prop, sizeof (count), 1, &count)) == ENOENT)
 897  863                  return;
 898  864          VERIFY0(err);
 899  865  
 900  866          count += delta;
 901  867          /* Use a signed verify to make sure we're not neg. */
 902  868          VERIFY3S(count, >=, 0);
 903  869  
 904  870          VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
 905  871              tx));
 906  872  
 907  873          /* Roll up this additional count into our ancestors */
 908  874          if (dd->dd_parent != NULL)
 909  875                  dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
 910  876  }
 911  877  
 912  878  uint64_t
 913  879  dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
 914  880      dmu_tx_t *tx)
 915  881  {
 916  882          objset_t *mos = dp->dp_meta_objset;
 917  883          uint64_t ddobj;
 918  884          dsl_dir_phys_t *ddphys;
 919  885          dmu_buf_t *dbuf;
 920  886  
 921  887          ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
 922  888              DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
 923  889          if (pds) {
 924  890                  VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
 925  891                      name, sizeof (uint64_t), 1, &ddobj, tx));
 926  892          } else {
 927  893                  /* it's the root dir */
 928  894                  VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
 929  895                      DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
 930  896          }
 931  897          VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
 932  898          dmu_buf_will_dirty(dbuf, tx);
 933  899          ddphys = dbuf->db_data;
 934  900  
 935  901          ddphys->dd_creation_time = gethrestime_sec();
 936  902          if (pds) {
 937  903                  ddphys->dd_parent_obj = pds->dd_object;
 938  904  
 939  905                  /* update the filesystem counts */
 940  906                  dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
 941  907          }
 942  908          ddphys->dd_props_zapobj = zap_create(mos,
 943  909              DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
 944  910          ddphys->dd_child_dir_zapobj = zap_create(mos,
 945  911              DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
 946  912          if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
 947  913                  ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
 948  914          dmu_buf_rele(dbuf, FTAG);
 949  915  
 950  916          return (ddobj);
 951  917  }
 952  918  
 953  919  boolean_t
 954  920  dsl_dir_is_clone(dsl_dir_t *dd)
 955  921  {
 956  922          return (dsl_dir_phys(dd)->dd_origin_obj &&
 957  923              (dd->dd_pool->dp_origin_snap == NULL ||
 958  924              dsl_dir_phys(dd)->dd_origin_obj !=
 959  925              dd->dd_pool->dp_origin_snap->ds_object));
 960  926  }
 961  927  
 962  928  
 963  929  uint64_t
 964  930  dsl_dir_get_used(dsl_dir_t *dd)
 965  931  {
 966  932          return (dsl_dir_phys(dd)->dd_used_bytes);
 967  933  }
 968  934  
 969  935  uint64_t
 970  936  dsl_dir_get_quota(dsl_dir_t *dd)
 971  937  {
 972  938          return (dsl_dir_phys(dd)->dd_quota);
 973  939  }
 974  940  
 975  941  uint64_t
 976  942  dsl_dir_get_reservation(dsl_dir_t *dd)
 977  943  {
 978  944          return (dsl_dir_phys(dd)->dd_reserved);
 979  945  }
 980  946  
 981  947  uint64_t
 982  948  dsl_dir_get_compressratio(dsl_dir_t *dd)
 983  949  {
 984  950          /* a fixed point number, 100x the ratio */
 985  951          return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
 986  952              (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
 987  953              dsl_dir_phys(dd)->dd_compressed_bytes));
 988  954  }
 989  955  
 990  956  uint64_t
 991  957  dsl_dir_get_logicalused(dsl_dir_t *dd)
 992  958  {
 993  959          return (dsl_dir_phys(dd)->dd_uncompressed_bytes);
 994  960  }
 995  961  
 996  962  uint64_t
 997  963  dsl_dir_get_usedsnap(dsl_dir_t *dd)
 998  964  {
 999  965          return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
1000  966  }
1001  967  
1002  968  uint64_t
1003  969  dsl_dir_get_usedds(dsl_dir_t *dd)
1004  970  {
1005  971          return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
1006  972  }
1007  973  
1008  974  uint64_t
1009  975  dsl_dir_get_usedrefreserv(dsl_dir_t *dd)
1010  976  {
1011  977          return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
1012  978  }
1013  979  
1014  980  uint64_t
1015  981  dsl_dir_get_usedchild(dsl_dir_t *dd)
1016  982  {
1017  983          return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
1018  984              dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
1019  985  }
1020  986  
1021  987  void
1022  988  dsl_dir_get_origin(dsl_dir_t *dd, char *buf)
1023  989  {
1024  990          dsl_dataset_t *ds;
1025  991          VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
1026  992              dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1027  993  
1028  994          dsl_dataset_name(ds, buf);
1029  995  
1030  996          dsl_dataset_rele(ds, FTAG);
1031  997  }
1032  998  
1033  999  int
1034 1000  dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)
1035 1001  {
1036 1002          if (dsl_dir_is_zapified(dd)) {
1037 1003                  objset_t *os = dd->dd_pool->dp_meta_objset;
1038 1004                  return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1039 1005                      sizeof (*count), 1, count));
1040 1006          } else {
1041 1007                  return (ENOENT);
1042 1008          }
1043 1009  }
1044 1010  
1045 1011  int
1046 1012  dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)
  
    | 
      ↓ open down ↓ | 
    271 lines elided | 
    
      ↑ open up ↑ | 
  
1047 1013  {
1048 1014          if (dsl_dir_is_zapified(dd)) {
1049 1015                  objset_t *os = dd->dd_pool->dp_meta_objset;
1050 1016                  return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1051 1017                      sizeof (*count), 1, count));
1052 1018          } else {
1053 1019                  return (ENOENT);
1054 1020          }
1055 1021  }
1056 1022  
1057      -int
1058      -dsl_dir_get_remaptxg(dsl_dir_t *dd, uint64_t *count)
1059      -{
1060      -        if (dsl_dir_is_zapified(dd)) {
1061      -                objset_t *os = dd->dd_pool->dp_meta_objset;
1062      -                return (zap_lookup(os, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1063      -                    sizeof (*count), 1, count));
1064      -        } else {
1065      -                return (ENOENT);
1066      -        }
1067      -}
1068      -
1069 1023  void
1070 1024  dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
1071 1025  {
1072 1026          mutex_enter(&dd->dd_lock);
1073 1027          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
1074 1028              dsl_dir_get_quota(dd));
1075 1029          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
1076 1030              dsl_dir_get_reservation(dd));
1077 1031          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
1078 1032              dsl_dir_get_logicalused(dd));
1079 1033          if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1080 1034                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
1081 1035                      dsl_dir_get_usedsnap(dd));
1082 1036                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
1083 1037                      dsl_dir_get_usedds(dd));
1084 1038                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
1085 1039                      dsl_dir_get_usedrefreserv(dd));
1086 1040                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
1087 1041                      dsl_dir_get_usedchild(dd));
1088 1042          }
1089 1043          mutex_exit(&dd->dd_lock);
  
    | 
      ↓ open down ↓ | 
    11 lines elided | 
    
      ↑ open up ↑ | 
  
1090 1044  
1091 1045          uint64_t count;
1092 1046          if (dsl_dir_get_filesystem_count(dd, &count) == 0) {
1093 1047                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,
1094 1048                      count);
1095 1049          }
1096 1050          if (dsl_dir_get_snapshot_count(dd, &count) == 0) {
1097 1051                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,
1098 1052                      count);
1099 1053          }
1100      -        if (dsl_dir_get_remaptxg(dd, &count) == 0) {
1101      -                dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REMAPTXG,
1102      -                    count);
1103      -        }
1104 1054  
1105 1055          if (dsl_dir_is_clone(dd)) {
1106 1056                  char buf[ZFS_MAX_DATASET_NAME_LEN];
1107 1057                  dsl_dir_get_origin(dd, buf);
1108 1058                  dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1109 1059          }
1110 1060  
1111 1061  }
1112 1062  
1113 1063  void
1114 1064  dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1115 1065  {
1116 1066          dsl_pool_t *dp = dd->dd_pool;
1117 1067  
1118 1068          ASSERT(dsl_dir_phys(dd));
1119 1069  
1120 1070          if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1121 1071                  /* up the hold count until we can be written out */
1122 1072                  dmu_buf_add_ref(dd->dd_dbuf, dd);
1123 1073          }
1124 1074  }
1125 1075  
1126 1076  static int64_t
1127 1077  parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1128 1078  {
1129 1079          uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1130 1080          uint64_t new_accounted =
1131 1081              MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1132 1082          return (new_accounted - old_accounted);
1133 1083  }
1134 1084  
1135 1085  void
1136 1086  dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1137 1087  {
1138 1088          ASSERT(dmu_tx_is_syncing(tx));
1139 1089  
1140 1090          mutex_enter(&dd->dd_lock);
1141 1091          ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1142 1092          dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1143 1093              dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1144 1094          dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1145 1095          mutex_exit(&dd->dd_lock);
1146 1096  
1147 1097          /* release the hold from dsl_dir_dirty */
1148 1098          dmu_buf_rele(dd->dd_dbuf, dd);
1149 1099  }
1150 1100  
1151 1101  static uint64_t
1152 1102  dsl_dir_space_towrite(dsl_dir_t *dd)
1153 1103  {
1154 1104          uint64_t space = 0;
1155 1105  
1156 1106          ASSERT(MUTEX_HELD(&dd->dd_lock));
1157 1107  
1158 1108          for (int i = 0; i < TXG_SIZE; i++) {
1159 1109                  space += dd->dd_space_towrite[i & TXG_MASK];
1160 1110                  ASSERT3U(dd->dd_space_towrite[i & TXG_MASK], >=, 0);
1161 1111          }
1162 1112          return (space);
1163 1113  }
1164 1114  
1165 1115  /*
1166 1116   * How much space would dd have available if ancestor had delta applied
1167 1117   * to it?  If ondiskonly is set, we're only interested in what's
1168 1118   * on-disk, not estimated pending changes.
1169 1119   */
1170 1120  uint64_t
1171 1121  dsl_dir_space_available(dsl_dir_t *dd,
1172 1122      dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1173 1123  {
1174 1124          uint64_t parentspace, myspace, quota, used;
1175 1125  
1176 1126          /*
1177 1127           * If there are no restrictions otherwise, assume we have
1178 1128           * unlimited space available.
1179 1129           */
1180 1130          quota = UINT64_MAX;
1181 1131          parentspace = UINT64_MAX;
1182 1132  
1183 1133          if (dd->dd_parent != NULL) {
1184 1134                  parentspace = dsl_dir_space_available(dd->dd_parent,
1185 1135                      ancestor, delta, ondiskonly);
1186 1136          }
1187 1137  
1188 1138          mutex_enter(&dd->dd_lock);
1189 1139          if (dsl_dir_phys(dd)->dd_quota != 0)
1190 1140                  quota = dsl_dir_phys(dd)->dd_quota;
1191 1141          used = dsl_dir_phys(dd)->dd_used_bytes;
1192 1142          if (!ondiskonly)
1193 1143                  used += dsl_dir_space_towrite(dd);
1194 1144  
1195 1145          if (dd->dd_parent == NULL) {
1196 1146                  uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1197 1147                  quota = MIN(quota, poolsize);
1198 1148          }
1199 1149  
1200 1150          if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1201 1151                  /*
1202 1152                   * We have some space reserved, in addition to what our
1203 1153                   * parent gave us.
1204 1154                   */
1205 1155                  parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1206 1156          }
1207 1157  
1208 1158          if (dd == ancestor) {
1209 1159                  ASSERT(delta <= 0);
1210 1160                  ASSERT(used >= -delta);
1211 1161                  used += delta;
1212 1162                  if (parentspace != UINT64_MAX)
1213 1163                          parentspace -= delta;
1214 1164          }
1215 1165  
1216 1166          if (used > quota) {
1217 1167                  /* over quota */
1218 1168                  myspace = 0;
1219 1169          } else {
1220 1170                  /*
1221 1171                   * the lesser of the space provided by our parent and
1222 1172                   * the space left in our quota
1223 1173                   */
1224 1174                  myspace = MIN(parentspace, quota - used);
1225 1175          }
1226 1176  
1227 1177          mutex_exit(&dd->dd_lock);
1228 1178  
1229 1179          return (myspace);
1230 1180  }
1231 1181  
1232 1182  struct tempreserve {
1233 1183          list_node_t tr_node;
1234 1184          dsl_dir_t *tr_ds;
1235 1185          uint64_t tr_size;
1236 1186  };
1237 1187  
1238 1188  static int
1239 1189  dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1240 1190      boolean_t ignorequota, list_t *tr_list,
1241 1191      dmu_tx_t *tx, boolean_t first)
1242 1192  {
1243 1193          uint64_t txg = tx->tx_txg;
1244 1194          uint64_t quota;
1245 1195          struct tempreserve *tr;
1246 1196          int retval = EDQUOT;
1247 1197          uint64_t ref_rsrv = 0;
1248 1198  
1249 1199          ASSERT3U(txg, !=, 0);
1250 1200          ASSERT3S(asize, >, 0);
1251 1201  
1252 1202          mutex_enter(&dd->dd_lock);
1253 1203  
1254 1204          /*
1255 1205           * Check against the dsl_dir's quota.  We don't add in the delta
1256 1206           * when checking for over-quota because they get one free hit.
1257 1207           */
1258 1208          uint64_t est_inflight = dsl_dir_space_towrite(dd);
1259 1209          for (int i = 0; i < TXG_SIZE; i++)
1260 1210                  est_inflight += dd->dd_tempreserved[i];
1261 1211          uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1262 1212  
1263 1213          /*
1264 1214           * On the first iteration, fetch the dataset's used-on-disk and
1265 1215           * refreservation values. Also, if checkrefquota is set, test if
1266 1216           * allocating this space would exceed the dataset's refquota.
1267 1217           */
1268 1218          if (first && tx->tx_objset) {
1269 1219                  int error;
1270 1220                  dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1271 1221  
1272 1222                  error = dsl_dataset_check_quota(ds, !netfree,
1273 1223                      asize, est_inflight, &used_on_disk, &ref_rsrv);
1274 1224                  if (error != 0) {
1275 1225                          mutex_exit(&dd->dd_lock);
1276 1226                          return (error);
1277 1227                  }
1278 1228          }
1279 1229  
1280 1230          /*
1281 1231           * If this transaction will result in a net free of space,
1282 1232           * we want to let it through.
1283 1233           */
1284 1234          if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1285 1235                  quota = UINT64_MAX;
1286 1236          else
1287 1237                  quota = dsl_dir_phys(dd)->dd_quota;
1288 1238  
1289 1239          /*
1290 1240           * Adjust the quota against the actual pool size at the root
1291 1241           * minus any outstanding deferred frees.
1292 1242           * To ensure that it's possible to remove files from a full
1293 1243           * pool without inducing transient overcommits, we throttle
1294 1244           * netfree transactions against a quota that is slightly larger,
1295 1245           * but still within the pool's allocation slop.  In cases where
1296 1246           * we're very close to full, this will allow a steady trickle of
1297 1247           * removes to get through.
1298 1248           */
1299 1249          uint64_t deferred = 0;
1300 1250          if (dd->dd_parent == NULL) {
1301 1251                  spa_t *spa = dd->dd_pool->dp_spa;
1302 1252                  uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1303 1253                  deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1304 1254                  if (poolsize - deferred < quota) {
1305 1255                          quota = poolsize - deferred;
1306 1256                          retval = ENOSPC;
1307 1257                  }
1308 1258          }
1309 1259  
1310 1260          /*
1311 1261           * If they are requesting more space, and our current estimate
1312 1262           * is over quota, they get to try again unless the actual
1313 1263           * on-disk is over quota and there are no pending changes (which
1314 1264           * may free up space for us).
1315 1265           */
1316 1266          if (used_on_disk + est_inflight >= quota) {
1317 1267                  if (est_inflight > 0 || used_on_disk < quota ||
1318 1268                      (retval == ENOSPC && used_on_disk < quota + deferred))
1319 1269                          retval = ERESTART;
1320 1270                  dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1321 1271                      "quota=%lluK tr=%lluK err=%d\n",
1322 1272                      used_on_disk>>10, est_inflight>>10,
1323 1273                      quota>>10, asize>>10, retval);
1324 1274                  mutex_exit(&dd->dd_lock);
1325 1275                  return (SET_ERROR(retval));
1326 1276          }
1327 1277  
1328 1278          /* We need to up our estimated delta before dropping dd_lock */
1329 1279          dd->dd_tempreserved[txg & TXG_MASK] += asize;
1330 1280  
1331 1281          uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1332 1282              asize - ref_rsrv);
1333 1283          mutex_exit(&dd->dd_lock);
1334 1284  
1335 1285          tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1336 1286          tr->tr_ds = dd;
1337 1287          tr->tr_size = asize;
1338 1288          list_insert_tail(tr_list, tr);
1339 1289  
1340 1290          /* see if it's OK with our parent */
1341 1291          if (dd->dd_parent != NULL && parent_rsrv != 0) {
1342 1292                  boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1343 1293  
1344 1294                  return (dsl_dir_tempreserve_impl(dd->dd_parent,
1345 1295                      parent_rsrv, netfree, ismos, tr_list, tx, B_FALSE));
1346 1296          } else {
1347 1297                  return (0);
1348 1298          }
1349 1299  }
1350 1300  
1351 1301  /*
1352 1302   * Reserve space in this dsl_dir, to be used in this tx's txg.
1353 1303   * After the space has been dirtied (and dsl_dir_willuse_space()
1354 1304   * has been called), the reservation should be canceled, using
1355 1305   * dsl_dir_tempreserve_clear().
1356 1306   */
1357 1307  int
1358 1308  dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1359 1309      boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)
1360 1310  {
1361 1311          int err;
1362 1312          list_t *tr_list;
1363 1313  
1364 1314          if (asize == 0) {
1365 1315                  *tr_cookiep = NULL;
1366 1316                  return (0);
1367 1317          }
1368 1318  
1369 1319          tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1370 1320          list_create(tr_list, sizeof (struct tempreserve),
1371 1321              offsetof(struct tempreserve, tr_node));
1372 1322          ASSERT3S(asize, >, 0);
1373 1323  
1374 1324          err = arc_tempreserve_space(lsize, tx->tx_txg);
1375 1325          if (err == 0) {
1376 1326                  struct tempreserve *tr;
1377 1327  
1378 1328                  tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1379 1329                  tr->tr_size = lsize;
1380 1330                  list_insert_tail(tr_list, tr);
1381 1331          } else {
1382 1332                  if (err == EAGAIN) {
1383 1333                          /*
1384 1334                           * If arc_memory_throttle() detected that pageout
1385 1335                           * is running and we are low on memory, we delay new
1386 1336                           * non-pageout transactions to give pageout an
1387 1337                           * advantage.
1388 1338                           *
1389 1339                           * It is unfortunate to be delaying while the caller's
1390 1340                           * locks are held.
1391 1341                           */
1392 1342                          txg_delay(dd->dd_pool, tx->tx_txg,
1393 1343                              MSEC2NSEC(10), MSEC2NSEC(10));
1394 1344                          err = SET_ERROR(ERESTART);
1395 1345                  }
1396 1346          }
1397 1347  
1398 1348          if (err == 0) {
1399 1349                  err = dsl_dir_tempreserve_impl(dd, asize, netfree,
1400 1350                      B_FALSE, tr_list, tx, B_TRUE);
1401 1351          }
1402 1352  
1403 1353          if (err != 0)
1404 1354                  dsl_dir_tempreserve_clear(tr_list, tx);
1405 1355          else
1406 1356                  *tr_cookiep = tr_list;
1407 1357  
1408 1358          return (err);
1409 1359  }
1410 1360  
1411 1361  /*
1412 1362   * Clear a temporary reservation that we previously made with
1413 1363   * dsl_dir_tempreserve_space().
1414 1364   */
1415 1365  void
1416 1366  dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1417 1367  {
1418 1368          int txgidx = tx->tx_txg & TXG_MASK;
1419 1369          list_t *tr_list = tr_cookie;
1420 1370          struct tempreserve *tr;
1421 1371  
1422 1372          ASSERT3U(tx->tx_txg, !=, 0);
1423 1373  
1424 1374          if (tr_cookie == NULL)
1425 1375                  return;
1426 1376  
1427 1377          while ((tr = list_head(tr_list)) != NULL) {
1428 1378                  if (tr->tr_ds) {
1429 1379                          mutex_enter(&tr->tr_ds->dd_lock);
1430 1380                          ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1431 1381                              tr->tr_size);
1432 1382                          tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1433 1383                          mutex_exit(&tr->tr_ds->dd_lock);
1434 1384                  } else {
1435 1385                          arc_tempreserve_clear(tr->tr_size);
1436 1386                  }
1437 1387                  list_remove(tr_list, tr);
1438 1388                  kmem_free(tr, sizeof (struct tempreserve));
1439 1389          }
1440 1390  
1441 1391          kmem_free(tr_list, sizeof (list_t));
1442 1392  }
1443 1393  
1444 1394  /*
1445 1395   * This should be called from open context when we think we're going to write
1446 1396   * or free space, for example when dirtying data. Be conservative; it's okay
1447 1397   * to write less space or free more, but we don't want to write more or free
1448 1398   * less than the amount specified.
1449 1399   */
1450 1400  void
1451 1401  dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1452 1402  {
1453 1403          int64_t parent_space;
1454 1404          uint64_t est_used;
1455 1405  
1456 1406          mutex_enter(&dd->dd_lock);
1457 1407          if (space > 0)
1458 1408                  dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1459 1409  
1460 1410          est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1461 1411          parent_space = parent_delta(dd, est_used, space);
1462 1412          mutex_exit(&dd->dd_lock);
1463 1413  
1464 1414          /* Make sure that we clean up dd_space_to* */
1465 1415          dsl_dir_dirty(dd, tx);
1466 1416  
1467 1417          /* XXX this is potentially expensive and unnecessary... */
1468 1418          if (parent_space && dd->dd_parent)
1469 1419                  dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1470 1420  }
1471 1421  
1472 1422  /* call from syncing context when we actually write/free space for this dd */
1473 1423  void
1474 1424  dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1475 1425      int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1476 1426  {
1477 1427          int64_t accounted_delta;
1478 1428  
1479 1429          /*
1480 1430           * dsl_dataset_set_refreservation_sync_impl() calls this with
1481 1431           * dd_lock held, so that it can atomically update
1482 1432           * ds->ds_reserved and the dsl_dir accounting, so that
1483 1433           * dsl_dataset_check_quota() can see dataset and dir accounting
1484 1434           * consistently.
1485 1435           */
1486 1436          boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1487 1437  
1488 1438          ASSERT(dmu_tx_is_syncing(tx));
1489 1439          ASSERT(type < DD_USED_NUM);
1490 1440  
1491 1441          dmu_buf_will_dirty(dd->dd_dbuf, tx);
1492 1442  
1493 1443          if (needlock)
1494 1444                  mutex_enter(&dd->dd_lock);
1495 1445          accounted_delta =
1496 1446              parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1497 1447          ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1498 1448          ASSERT(compressed >= 0 ||
1499 1449              dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1500 1450          ASSERT(uncompressed >= 0 ||
1501 1451              dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1502 1452          dsl_dir_phys(dd)->dd_used_bytes += used;
1503 1453          dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1504 1454          dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1505 1455  
1506 1456          if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1507 1457                  ASSERT(used > 0 ||
1508 1458                      dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1509 1459                  dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1510 1460  #ifdef DEBUG
1511 1461                  dd_used_t t;
1512 1462                  uint64_t u = 0;
1513 1463                  for (t = 0; t < DD_USED_NUM; t++)
1514 1464                          u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1515 1465                  ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1516 1466  #endif
1517 1467          }
1518 1468          if (needlock)
1519 1469                  mutex_exit(&dd->dd_lock);
1520 1470  
1521 1471          if (dd->dd_parent != NULL) {
1522 1472                  dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1523 1473                      accounted_delta, compressed, uncompressed, tx);
1524 1474                  dsl_dir_transfer_space(dd->dd_parent,
1525 1475                      used - accounted_delta,
1526 1476                      DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1527 1477          }
1528 1478  }
1529 1479  
1530 1480  void
1531 1481  dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1532 1482      dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1533 1483  {
1534 1484          ASSERT(dmu_tx_is_syncing(tx));
1535 1485          ASSERT(oldtype < DD_USED_NUM);
1536 1486          ASSERT(newtype < DD_USED_NUM);
1537 1487  
1538 1488          if (delta == 0 ||
1539 1489              !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1540 1490                  return;
1541 1491  
1542 1492          dmu_buf_will_dirty(dd->dd_dbuf, tx);
1543 1493          mutex_enter(&dd->dd_lock);
1544 1494          ASSERT(delta > 0 ?
1545 1495              dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1546 1496              dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1547 1497          ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1548 1498          dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1549 1499          dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1550 1500          mutex_exit(&dd->dd_lock);
1551 1501  }
1552 1502  
1553 1503  typedef struct dsl_dir_set_qr_arg {
1554 1504          const char *ddsqra_name;
1555 1505          zprop_source_t ddsqra_source;
1556 1506          uint64_t ddsqra_value;
1557 1507  } dsl_dir_set_qr_arg_t;
1558 1508  
1559 1509  static int
1560 1510  dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1561 1511  {
1562 1512          dsl_dir_set_qr_arg_t *ddsqra = arg;
1563 1513          dsl_pool_t *dp = dmu_tx_pool(tx);
1564 1514          dsl_dataset_t *ds;
1565 1515          int error;
1566 1516          uint64_t towrite, newval;
1567 1517  
1568 1518          error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1569 1519          if (error != 0)
1570 1520                  return (error);
1571 1521  
1572 1522          error = dsl_prop_predict(ds->ds_dir, "quota",
1573 1523              ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1574 1524          if (error != 0) {
1575 1525                  dsl_dataset_rele(ds, FTAG);
1576 1526                  return (error);
1577 1527          }
1578 1528  
1579 1529          if (newval == 0) {
1580 1530                  dsl_dataset_rele(ds, FTAG);
1581 1531                  return (0);
1582 1532          }
1583 1533  
1584 1534          mutex_enter(&ds->ds_dir->dd_lock);
1585 1535          /*
1586 1536           * If we are doing the preliminary check in open context, and
1587 1537           * there are pending changes, then don't fail it, since the
1588 1538           * pending changes could under-estimate the amount of space to be
1589 1539           * freed up.
1590 1540           */
1591 1541          towrite = dsl_dir_space_towrite(ds->ds_dir);
1592 1542          if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1593 1543              (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1594 1544              newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1595 1545                  error = SET_ERROR(ENOSPC);
1596 1546          }
1597 1547          mutex_exit(&ds->ds_dir->dd_lock);
1598 1548          dsl_dataset_rele(ds, FTAG);
1599 1549          return (error);
1600 1550  }
1601 1551  
1602 1552  static void
1603 1553  dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1604 1554  {
1605 1555          dsl_dir_set_qr_arg_t *ddsqra = arg;
1606 1556          dsl_pool_t *dp = dmu_tx_pool(tx);
1607 1557          dsl_dataset_t *ds;
1608 1558          uint64_t newval;
1609 1559  
1610 1560          VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1611 1561  
1612 1562          if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1613 1563                  dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1614 1564                      ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1615 1565                      &ddsqra->ddsqra_value, tx);
1616 1566  
1617 1567                  VERIFY0(dsl_prop_get_int_ds(ds,
1618 1568                      zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1619 1569          } else {
1620 1570                  newval = ddsqra->ddsqra_value;
1621 1571                  spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1622 1572                      zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1623 1573          }
1624 1574  
1625 1575          dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1626 1576          mutex_enter(&ds->ds_dir->dd_lock);
1627 1577          dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1628 1578          mutex_exit(&ds->ds_dir->dd_lock);
1629 1579          dsl_dataset_rele(ds, FTAG);
1630 1580  }
1631 1581  
1632 1582  int
1633 1583  dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1634 1584  {
1635 1585          dsl_dir_set_qr_arg_t ddsqra;
1636 1586  
1637 1587          ddsqra.ddsqra_name = ddname;
1638 1588          ddsqra.ddsqra_source = source;
1639 1589          ddsqra.ddsqra_value = quota;
1640 1590  
1641 1591          return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1642 1592              dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1643 1593  }
1644 1594  
1645 1595  int
1646 1596  dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1647 1597  {
1648 1598          dsl_dir_set_qr_arg_t *ddsqra = arg;
1649 1599          dsl_pool_t *dp = dmu_tx_pool(tx);
1650 1600          dsl_dataset_t *ds;
1651 1601          dsl_dir_t *dd;
1652 1602          uint64_t newval, used, avail;
1653 1603          int error;
1654 1604  
1655 1605          error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1656 1606          if (error != 0)
1657 1607                  return (error);
1658 1608          dd = ds->ds_dir;
1659 1609  
1660 1610          /*
1661 1611           * If we are doing the preliminary check in open context, the
1662 1612           * space estimates may be inaccurate.
1663 1613           */
1664 1614          if (!dmu_tx_is_syncing(tx)) {
1665 1615                  dsl_dataset_rele(ds, FTAG);
1666 1616                  return (0);
1667 1617          }
1668 1618  
1669 1619          error = dsl_prop_predict(ds->ds_dir,
1670 1620              zfs_prop_to_name(ZFS_PROP_RESERVATION),
1671 1621              ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1672 1622          if (error != 0) {
1673 1623                  dsl_dataset_rele(ds, FTAG);
1674 1624                  return (error);
1675 1625          }
1676 1626  
1677 1627          mutex_enter(&dd->dd_lock);
1678 1628          used = dsl_dir_phys(dd)->dd_used_bytes;
1679 1629          mutex_exit(&dd->dd_lock);
1680 1630  
1681 1631          if (dd->dd_parent) {
1682 1632                  avail = dsl_dir_space_available(dd->dd_parent,
1683 1633                      NULL, 0, FALSE);
1684 1634          } else {
1685 1635                  avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1686 1636          }
1687 1637  
1688 1638          if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1689 1639                  uint64_t delta = MAX(used, newval) -
1690 1640                      MAX(used, dsl_dir_phys(dd)->dd_reserved);
1691 1641  
1692 1642                  if (delta > avail ||
1693 1643                      (dsl_dir_phys(dd)->dd_quota > 0 &&
1694 1644                      newval > dsl_dir_phys(dd)->dd_quota))
1695 1645                          error = SET_ERROR(ENOSPC);
1696 1646          }
1697 1647  
1698 1648          dsl_dataset_rele(ds, FTAG);
1699 1649          return (error);
1700 1650  }
1701 1651  
1702 1652  void
1703 1653  dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1704 1654  {
1705 1655          uint64_t used;
1706 1656          int64_t delta;
1707 1657  
1708 1658          dmu_buf_will_dirty(dd->dd_dbuf, tx);
1709 1659  
1710 1660          mutex_enter(&dd->dd_lock);
1711 1661          used = dsl_dir_phys(dd)->dd_used_bytes;
1712 1662          delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1713 1663          dsl_dir_phys(dd)->dd_reserved = value;
1714 1664  
1715 1665          if (dd->dd_parent != NULL) {
1716 1666                  /* Roll up this additional usage into our ancestors */
1717 1667                  dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1718 1668                      delta, 0, 0, tx);
1719 1669          }
1720 1670          mutex_exit(&dd->dd_lock);
1721 1671  }
1722 1672  
1723 1673  
1724 1674  static void
1725 1675  dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1726 1676  {
1727 1677          dsl_dir_set_qr_arg_t *ddsqra = arg;
1728 1678          dsl_pool_t *dp = dmu_tx_pool(tx);
1729 1679          dsl_dataset_t *ds;
1730 1680          uint64_t newval;
1731 1681  
1732 1682          VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1733 1683  
1734 1684          if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1735 1685                  dsl_prop_set_sync_impl(ds,
1736 1686                      zfs_prop_to_name(ZFS_PROP_RESERVATION),
1737 1687                      ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1738 1688                      &ddsqra->ddsqra_value, tx);
1739 1689  
1740 1690                  VERIFY0(dsl_prop_get_int_ds(ds,
1741 1691                      zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1742 1692          } else {
1743 1693                  newval = ddsqra->ddsqra_value;
1744 1694                  spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1745 1695                      zfs_prop_to_name(ZFS_PROP_RESERVATION),
1746 1696                      (longlong_t)newval);
1747 1697          }
1748 1698  
1749 1699          dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1750 1700          dsl_dataset_rele(ds, FTAG);
1751 1701  }
1752 1702  
1753 1703  int
1754 1704  dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1755 1705      uint64_t reservation)
1756 1706  {
1757 1707          dsl_dir_set_qr_arg_t ddsqra;
1758 1708  
1759 1709          ddsqra.ddsqra_name = ddname;
1760 1710          ddsqra.ddsqra_source = source;
1761 1711          ddsqra.ddsqra_value = reservation;
1762 1712  
1763 1713          return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1764 1714              dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1765 1715  }
1766 1716  
1767 1717  static dsl_dir_t *
1768 1718  closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1769 1719  {
1770 1720          for (; ds1; ds1 = ds1->dd_parent) {
1771 1721                  dsl_dir_t *dd;
1772 1722                  for (dd = ds2; dd; dd = dd->dd_parent) {
1773 1723                          if (ds1 == dd)
1774 1724                                  return (dd);
1775 1725                  }
1776 1726          }
1777 1727          return (NULL);
1778 1728  }
1779 1729  
1780 1730  /*
1781 1731   * If delta is applied to dd, how much of that delta would be applied to
1782 1732   * ancestor?  Syncing context only.
1783 1733   */
1784 1734  static int64_t
1785 1735  would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1786 1736  {
1787 1737          if (dd == ancestor)
1788 1738                  return (delta);
1789 1739  
1790 1740          mutex_enter(&dd->dd_lock);
1791 1741          delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1792 1742          mutex_exit(&dd->dd_lock);
1793 1743          return (would_change(dd->dd_parent, delta, ancestor));
1794 1744  }
1795 1745  
1796 1746  typedef struct dsl_dir_rename_arg {
1797 1747          const char *ddra_oldname;
1798 1748          const char *ddra_newname;
1799 1749          cred_t *ddra_cred;
1800 1750  } dsl_dir_rename_arg_t;
1801 1751  
1802 1752  /* ARGSUSED */
1803 1753  static int
1804 1754  dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1805 1755  {
1806 1756          int *deltap = arg;
1807 1757          char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1808 1758  
1809 1759          dsl_dataset_name(ds, namebuf);
1810 1760  
1811 1761          if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN)
1812 1762                  return (SET_ERROR(ENAMETOOLONG));
1813 1763          return (0);
1814 1764  }
1815 1765  
1816 1766  static int
1817 1767  dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1818 1768  {
1819 1769          dsl_dir_rename_arg_t *ddra = arg;
1820 1770          dsl_pool_t *dp = dmu_tx_pool(tx);
1821 1771          dsl_dir_t *dd, *newparent;
1822 1772          const char *mynewname;
1823 1773          int error;
1824 1774          int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1825 1775  
1826 1776          /* target dir should exist */
1827 1777          error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1828 1778          if (error != 0)
1829 1779                  return (error);
1830 1780  
1831 1781          /* new parent should exist */
1832 1782          error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1833 1783              &newparent, &mynewname);
1834 1784          if (error != 0) {
1835 1785                  dsl_dir_rele(dd, FTAG);
1836 1786                  return (error);
1837 1787          }
1838 1788  
1839 1789          /* can't rename to different pool */
1840 1790          if (dd->dd_pool != newparent->dd_pool) {
1841 1791                  dsl_dir_rele(newparent, FTAG);
1842 1792                  dsl_dir_rele(dd, FTAG);
1843 1793                  return (SET_ERROR(ENXIO));
1844 1794          }
1845 1795  
1846 1796          /* new name should not already exist */
1847 1797          if (mynewname == NULL) {
1848 1798                  dsl_dir_rele(newparent, FTAG);
1849 1799                  dsl_dir_rele(dd, FTAG);
1850 1800                  return (SET_ERROR(EEXIST));
1851 1801          }
1852 1802  
1853 1803          /* if the name length is growing, validate child name lengths */
1854 1804          if (delta > 0) {
1855 1805                  error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1856 1806                      &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1857 1807                  if (error != 0) {
1858 1808                          dsl_dir_rele(newparent, FTAG);
1859 1809                          dsl_dir_rele(dd, FTAG);
1860 1810                          return (error);
1861 1811                  }
1862 1812          }
1863 1813  
1864 1814          if (dmu_tx_is_syncing(tx)) {
1865 1815                  if (spa_feature_is_active(dp->dp_spa,
1866 1816                      SPA_FEATURE_FS_SS_LIMIT)) {
1867 1817                          /*
1868 1818                           * Although this is the check function and we don't
1869 1819                           * normally make on-disk changes in check functions,
1870 1820                           * we need to do that here.
1871 1821                           *
1872 1822                           * Ensure this portion of the tree's counts have been
1873 1823                           * initialized in case the new parent has limits set.
1874 1824                           */
1875 1825                          dsl_dir_init_fs_ss_count(dd, tx);
1876 1826                  }
1877 1827          }
1878 1828  
1879 1829          if (newparent != dd->dd_parent) {
1880 1830                  /* is there enough space? */
1881 1831                  uint64_t myspace =
1882 1832                      MAX(dsl_dir_phys(dd)->dd_used_bytes,
1883 1833                      dsl_dir_phys(dd)->dd_reserved);
1884 1834                  objset_t *os = dd->dd_pool->dp_meta_objset;
1885 1835                  uint64_t fs_cnt = 0;
1886 1836                  uint64_t ss_cnt = 0;
1887 1837  
1888 1838                  if (dsl_dir_is_zapified(dd)) {
1889 1839                          int err;
1890 1840  
1891 1841                          err = zap_lookup(os, dd->dd_object,
1892 1842                              DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1893 1843                              &fs_cnt);
1894 1844                          if (err != ENOENT && err != 0) {
1895 1845                                  dsl_dir_rele(newparent, FTAG);
1896 1846                                  dsl_dir_rele(dd, FTAG);
1897 1847                                  return (err);
1898 1848                          }
1899 1849  
1900 1850                          /*
1901 1851                           * have to add 1 for the filesystem itself that we're
1902 1852                           * moving
1903 1853                           */
1904 1854                          fs_cnt++;
1905 1855  
1906 1856                          err = zap_lookup(os, dd->dd_object,
1907 1857                              DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1908 1858                              &ss_cnt);
1909 1859                          if (err != ENOENT && err != 0) {
1910 1860                                  dsl_dir_rele(newparent, FTAG);
1911 1861                                  dsl_dir_rele(dd, FTAG);
1912 1862                                  return (err);
1913 1863                          }
1914 1864                  }
1915 1865  
1916 1866                  /* no rename into our descendant */
1917 1867                  if (closest_common_ancestor(dd, newparent) == dd) {
1918 1868                          dsl_dir_rele(newparent, FTAG);
1919 1869                          dsl_dir_rele(dd, FTAG);
1920 1870                          return (SET_ERROR(EINVAL));
1921 1871                  }
1922 1872  
1923 1873                  error = dsl_dir_transfer_possible(dd->dd_parent,
1924 1874                      newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
1925 1875                  if (error != 0) {
1926 1876                          dsl_dir_rele(newparent, FTAG);
1927 1877                          dsl_dir_rele(dd, FTAG);
1928 1878                          return (error);
1929 1879                  }
1930 1880          }
1931 1881  
1932 1882          dsl_dir_rele(newparent, FTAG);
1933 1883          dsl_dir_rele(dd, FTAG);
1934 1884          return (0);
1935 1885  }
1936 1886  
1937 1887  static void
1938 1888  dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
1939 1889  {
1940 1890          dsl_dir_rename_arg_t *ddra = arg;
1941 1891          dsl_pool_t *dp = dmu_tx_pool(tx);
1942 1892          dsl_dir_t *dd, *newparent;
1943 1893          const char *mynewname;
1944 1894          int error;
1945 1895          objset_t *mos = dp->dp_meta_objset;
1946 1896  
1947 1897          VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
1948 1898          VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
1949 1899              &mynewname));
1950 1900  
1951 1901          /* Log this before we change the name. */
1952 1902          spa_history_log_internal_dd(dd, "rename", tx,
1953 1903              "-> %s", ddra->ddra_newname);
1954 1904  
1955 1905          if (newparent != dd->dd_parent) {
1956 1906                  objset_t *os = dd->dd_pool->dp_meta_objset;
1957 1907                  uint64_t fs_cnt = 0;
1958 1908                  uint64_t ss_cnt = 0;
1959 1909  
1960 1910                  /*
1961 1911                   * We already made sure the dd counts were initialized in the
1962 1912                   * check function.
1963 1913                   */
1964 1914                  if (spa_feature_is_active(dp->dp_spa,
1965 1915                      SPA_FEATURE_FS_SS_LIMIT)) {
1966 1916                          VERIFY0(zap_lookup(os, dd->dd_object,
1967 1917                              DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1968 1918                              &fs_cnt));
1969 1919                          /* add 1 for the filesystem itself that we're moving */
1970 1920                          fs_cnt++;
1971 1921  
1972 1922                          VERIFY0(zap_lookup(os, dd->dd_object,
1973 1923                              DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1974 1924                              &ss_cnt));
1975 1925                  }
1976 1926  
1977 1927                  dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1978 1928                      DD_FIELD_FILESYSTEM_COUNT, tx);
1979 1929                  dsl_fs_ss_count_adjust(newparent, fs_cnt,
1980 1930                      DD_FIELD_FILESYSTEM_COUNT, tx);
1981 1931  
1982 1932                  dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1983 1933                      DD_FIELD_SNAPSHOT_COUNT, tx);
1984 1934                  dsl_fs_ss_count_adjust(newparent, ss_cnt,
1985 1935                      DD_FIELD_SNAPSHOT_COUNT, tx);
1986 1936  
1987 1937                  dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1988 1938                      -dsl_dir_phys(dd)->dd_used_bytes,
1989 1939                      -dsl_dir_phys(dd)->dd_compressed_bytes,
1990 1940                      -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1991 1941                  dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1992 1942                      dsl_dir_phys(dd)->dd_used_bytes,
1993 1943                      dsl_dir_phys(dd)->dd_compressed_bytes,
1994 1944                      dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1995 1945  
1996 1946                  if (dsl_dir_phys(dd)->dd_reserved >
1997 1947                      dsl_dir_phys(dd)->dd_used_bytes) {
1998 1948                          uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1999 1949                              dsl_dir_phys(dd)->dd_used_bytes;
2000 1950  
2001 1951                          dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
2002 1952                              -unused_rsrv, 0, 0, tx);
2003 1953                          dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
2004 1954                              unused_rsrv, 0, 0, tx);
2005 1955                  }
2006 1956          }
2007 1957  
2008 1958          dmu_buf_will_dirty(dd->dd_dbuf, tx);
2009 1959  
2010 1960          /* remove from old parent zapobj */
2011 1961          error = zap_remove(mos,
2012 1962              dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
2013 1963              dd->dd_myname, tx);
2014 1964          ASSERT0(error);
2015 1965  
2016 1966          (void) strcpy(dd->dd_myname, mynewname);
2017 1967          dsl_dir_rele(dd->dd_parent, dd);
2018 1968          dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
2019 1969          VERIFY0(dsl_dir_hold_obj(dp,
2020 1970              newparent->dd_object, NULL, dd, &dd->dd_parent));
2021 1971  
2022 1972          /* add to new parent zapobj */
2023 1973          VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
2024 1974              dd->dd_myname, 8, 1, &dd->dd_object, tx));
2025 1975  
2026 1976          dsl_prop_notify_all(dd);
2027 1977  
2028 1978          dsl_dir_rele(newparent, FTAG);
2029 1979          dsl_dir_rele(dd, FTAG);
2030 1980  }
2031 1981  
2032 1982  int
2033 1983  dsl_dir_rename(const char *oldname, const char *newname)
2034 1984  {
2035 1985          dsl_dir_rename_arg_t ddra;
2036 1986  
2037 1987          ddra.ddra_oldname = oldname;
2038 1988          ddra.ddra_newname = newname;
2039 1989          ddra.ddra_cred = CRED();
2040 1990  
2041 1991          return (dsl_sync_task(oldname,
2042 1992              dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
2043 1993              3, ZFS_SPACE_CHECK_RESERVED));
2044 1994  }
2045 1995  
2046 1996  int
2047 1997  dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
2048 1998      uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
2049 1999  {
2050 2000          dsl_dir_t *ancestor;
2051 2001          int64_t adelta;
2052 2002          uint64_t avail;
2053 2003          int err;
2054 2004  
2055 2005          ancestor = closest_common_ancestor(sdd, tdd);
2056 2006          adelta = would_change(sdd, -space, ancestor);
2057 2007          avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
2058 2008          if (avail < space)
2059 2009                  return (SET_ERROR(ENOSPC));
2060 2010  
2061 2011          err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
2062 2012              ancestor, cr);
2063 2013          if (err != 0)
2064 2014                  return (err);
2065 2015          err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
2066 2016              ancestor, cr);
2067 2017          if (err != 0)
2068 2018                  return (err);
2069 2019  
2070 2020          return (0);
2071 2021  }
2072 2022  
2073 2023  timestruc_t
2074 2024  dsl_dir_snap_cmtime(dsl_dir_t *dd)
2075 2025  {
2076 2026          timestruc_t t;
2077 2027  
2078 2028          mutex_enter(&dd->dd_lock);
2079 2029          t = dd->dd_snap_cmtime;
2080 2030          mutex_exit(&dd->dd_lock);
2081 2031  
2082 2032          return (t);
2083 2033  }
2084 2034  
2085 2035  void
2086 2036  dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
2087 2037  {
2088 2038          timestruc_t t;
2089 2039  
2090 2040          gethrestime(&t);
2091 2041          mutex_enter(&dd->dd_lock);
2092 2042          dd->dd_snap_cmtime = t;
2093 2043          mutex_exit(&dd->dd_lock);
2094 2044  }
2095 2045  
2096 2046  void
2097 2047  dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
2098 2048  {
2099 2049          objset_t *mos = dd->dd_pool->dp_meta_objset;
2100 2050          dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
2101 2051  }
2102 2052  
2103 2053  boolean_t
2104 2054  dsl_dir_is_zapified(dsl_dir_t *dd)
2105 2055  {
2106 2056          dmu_object_info_t doi;
2107 2057  
2108 2058          dmu_object_info_from_db(dd->dd_dbuf, &doi);
2109 2059          return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2110 2060  }
  
    | 
      ↓ open down ↓ | 
    997 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX