Print this page
    
OS-1566 filesystem limits for ZFS datasets
    
      
        | Split | Close | 
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/zfs/dsl_dataset.c
          +++ new/usr/src/uts/common/fs/zfs/dsl_dataset.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23   23   * Copyright (c) 2012 by Delphix. All rights reserved.
  24   24   * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  25   25   */
  26   26  
  27   27  #include <sys/dmu_objset.h>
  28   28  #include <sys/dsl_dataset.h>
  29   29  #include <sys/dsl_dir.h>
  30   30  #include <sys/dsl_prop.h>
  31   31  #include <sys/dsl_synctask.h>
  32   32  #include <sys/dmu_traverse.h>
  33   33  #include <sys/dmu_impl.h>
  34   34  #include <sys/dmu_tx.h>
  35   35  #include <sys/arc.h>
  36   36  #include <sys/zio.h>
  37   37  #include <sys/zap.h>
  
    | ↓ open down ↓ | 37 lines elided | ↑ open up ↑ | 
  38   38  #include <sys/zfeature.h>
  39   39  #include <sys/unique.h>
  40   40  #include <sys/zfs_context.h>
  41   41  #include <sys/zfs_ioctl.h>
  42   42  #include <sys/spa.h>
  43   43  #include <sys/zfs_znode.h>
  44   44  #include <sys/zfs_onexit.h>
  45   45  #include <sys/zvol.h>
  46   46  #include <sys/dsl_scan.h>
  47   47  #include <sys/dsl_deadlist.h>
       48 +#include "zfs_prop.h"
  48   49  
  49   50  static char *dsl_reaper = "the grim reaper";
  50   51  
  51   52  static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
  52   53  static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
  53   54  static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
  54   55  
  55   56  #define SWITCH64(x, y) \
  56   57          { \
  57   58                  uint64_t __tmp = (x); \
  58   59                  (x) = (y); \
  59   60                  (y) = __tmp; \
  60   61          }
  61   62  
  62   63  #define DS_REF_MAX      (1ULL << 62)
  63   64  
  64   65  #define DSL_DEADLIST_BLOCKSIZE  SPA_MAXBLOCKSIZE
  65   66  
  66   67  #define DSL_DATASET_IS_DESTROYED(ds)    ((ds)->ds_owner == dsl_reaper)
  67   68  
  68   69  
  69   70  /*
  70   71   * Figure out how much of this delta should be propogated to the dsl_dir
  71   72   * layer.  If there's a refreservation, that space has already been
  72   73   * partially accounted for in our ancestors.
  73   74   */
  74   75  static int64_t
  75   76  parent_delta(dsl_dataset_t *ds, int64_t delta)
  76   77  {
  77   78          uint64_t old_bytes, new_bytes;
  78   79  
  79   80          if (ds->ds_reserved == 0)
  80   81                  return (delta);
  81   82  
  82   83          old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
  83   84          new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
  84   85  
  85   86          ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
  86   87          return (new_bytes - old_bytes);
  87   88  }
  88   89  
  89   90  void
  90   91  dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
  91   92  {
  92   93          int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
  93   94          int compressed = BP_GET_PSIZE(bp);
  94   95          int uncompressed = BP_GET_UCSIZE(bp);
  95   96          int64_t delta;
  96   97  
  97   98          dprintf_bp(bp, "ds=%p", ds);
  98   99  
  99  100          ASSERT(dmu_tx_is_syncing(tx));
 100  101          /* It could have been compressed away to nothing */
 101  102          if (BP_IS_HOLE(bp))
 102  103                  return;
 103  104          ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
 104  105          ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
 105  106          if (ds == NULL) {
 106  107                  dsl_pool_mos_diduse_space(tx->tx_pool,
 107  108                      used, compressed, uncompressed);
 108  109                  return;
 109  110          }
 110  111          dmu_buf_will_dirty(ds->ds_dbuf, tx);
 111  112  
 112  113          mutex_enter(&ds->ds_dir->dd_lock);
 113  114          mutex_enter(&ds->ds_lock);
 114  115          delta = parent_delta(ds, used);
 115  116          ds->ds_phys->ds_referenced_bytes += used;
 116  117          ds->ds_phys->ds_compressed_bytes += compressed;
 117  118          ds->ds_phys->ds_uncompressed_bytes += uncompressed;
 118  119          ds->ds_phys->ds_unique_bytes += used;
 119  120          mutex_exit(&ds->ds_lock);
 120  121          dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
 121  122              compressed, uncompressed, tx);
 122  123          dsl_dir_transfer_space(ds->ds_dir, used - delta,
 123  124              DD_USED_REFRSRV, DD_USED_HEAD, tx);
 124  125          mutex_exit(&ds->ds_dir->dd_lock);
 125  126  }
 126  127  
 127  128  int
 128  129  dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
 129  130      boolean_t async)
 130  131  {
 131  132          if (BP_IS_HOLE(bp))
 132  133                  return (0);
 133  134  
 134  135          ASSERT(dmu_tx_is_syncing(tx));
 135  136          ASSERT(bp->blk_birth <= tx->tx_txg);
 136  137  
 137  138          int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
 138  139          int compressed = BP_GET_PSIZE(bp);
 139  140          int uncompressed = BP_GET_UCSIZE(bp);
 140  141  
 141  142          ASSERT(used > 0);
 142  143          if (ds == NULL) {
 143  144                  dsl_free(tx->tx_pool, tx->tx_txg, bp);
 144  145                  dsl_pool_mos_diduse_space(tx->tx_pool,
 145  146                      -used, -compressed, -uncompressed);
 146  147                  return (used);
 147  148          }
 148  149          ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
 149  150  
 150  151          ASSERT(!dsl_dataset_is_snapshot(ds));
 151  152          dmu_buf_will_dirty(ds->ds_dbuf, tx);
 152  153  
 153  154          if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
 154  155                  int64_t delta;
 155  156  
 156  157                  dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
 157  158                  dsl_free(tx->tx_pool, tx->tx_txg, bp);
 158  159  
 159  160                  mutex_enter(&ds->ds_dir->dd_lock);
 160  161                  mutex_enter(&ds->ds_lock);
 161  162                  ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
 162  163                      !DS_UNIQUE_IS_ACCURATE(ds));
 163  164                  delta = parent_delta(ds, -used);
 164  165                  ds->ds_phys->ds_unique_bytes -= used;
 165  166                  mutex_exit(&ds->ds_lock);
 166  167                  dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
 167  168                      delta, -compressed, -uncompressed, tx);
 168  169                  dsl_dir_transfer_space(ds->ds_dir, -used - delta,
 169  170                      DD_USED_REFRSRV, DD_USED_HEAD, tx);
 170  171                  mutex_exit(&ds->ds_dir->dd_lock);
 171  172          } else {
 172  173                  dprintf_bp(bp, "putting on dead list: %s", "");
 173  174                  if (async) {
 174  175                          /*
 175  176                           * We are here as part of zio's write done callback,
 176  177                           * which means we're a zio interrupt thread.  We can't
 177  178                           * call dsl_deadlist_insert() now because it may block
 178  179                           * waiting for I/O.  Instead, put bp on the deferred
 179  180                           * queue and let dsl_pool_sync() finish the job.
 180  181                           */
 181  182                          bplist_append(&ds->ds_pending_deadlist, bp);
 182  183                  } else {
 183  184                          dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
 184  185                  }
 185  186                  ASSERT3U(ds->ds_prev->ds_object, ==,
 186  187                      ds->ds_phys->ds_prev_snap_obj);
 187  188                  ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
 188  189                  /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
 189  190                  if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
 190  191                      ds->ds_object && bp->blk_birth >
 191  192                      ds->ds_prev->ds_phys->ds_prev_snap_txg) {
 192  193                          dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
 193  194                          mutex_enter(&ds->ds_prev->ds_lock);
 194  195                          ds->ds_prev->ds_phys->ds_unique_bytes += used;
 195  196                          mutex_exit(&ds->ds_prev->ds_lock);
 196  197                  }
 197  198                  if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
 198  199                          dsl_dir_transfer_space(ds->ds_dir, used,
 199  200                              DD_USED_HEAD, DD_USED_SNAP, tx);
 200  201                  }
 201  202          }
 202  203          mutex_enter(&ds->ds_lock);
 203  204          ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
 204  205          ds->ds_phys->ds_referenced_bytes -= used;
 205  206          ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
 206  207          ds->ds_phys->ds_compressed_bytes -= compressed;
 207  208          ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
 208  209          ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
 209  210          mutex_exit(&ds->ds_lock);
 210  211  
 211  212          return (used);
 212  213  }
 213  214  
 214  215  uint64_t
 215  216  dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
 216  217  {
 217  218          uint64_t trysnap = 0;
 218  219  
 219  220          if (ds == NULL)
 220  221                  return (0);
 221  222          /*
 222  223           * The snapshot creation could fail, but that would cause an
 223  224           * incorrect FALSE return, which would only result in an
 224  225           * overestimation of the amount of space that an operation would
 225  226           * consume, which is OK.
 226  227           *
 227  228           * There's also a small window where we could miss a pending
 228  229           * snapshot, because we could set the sync task in the quiescing
 229  230           * phase.  So this should only be used as a guess.
 230  231           */
 231  232          if (ds->ds_trysnap_txg >
 232  233              spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
 233  234                  trysnap = ds->ds_trysnap_txg;
 234  235          return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
 235  236  }
 236  237  
 237  238  boolean_t
 238  239  dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
 239  240      uint64_t blk_birth)
 240  241  {
 241  242          if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
 242  243                  return (B_FALSE);
 243  244  
 244  245          ddt_prefetch(dsl_dataset_get_spa(ds), bp);
 245  246  
 246  247          return (B_TRUE);
 247  248  }
 248  249  
 249  250  /* ARGSUSED */
 250  251  static void
 251  252  dsl_dataset_evict(dmu_buf_t *db, void *dsv)
 252  253  {
 253  254          dsl_dataset_t *ds = dsv;
 254  255  
 255  256          ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
 256  257  
 257  258          unique_remove(ds->ds_fsid_guid);
 258  259  
 259  260          if (ds->ds_objset != NULL)
 260  261                  dmu_objset_evict(ds->ds_objset);
 261  262  
 262  263          if (ds->ds_prev) {
 263  264                  dsl_dataset_drop_ref(ds->ds_prev, ds);
 264  265                  ds->ds_prev = NULL;
 265  266          }
 266  267  
 267  268          bplist_destroy(&ds->ds_pending_deadlist);
 268  269          if (db != NULL) {
 269  270                  dsl_deadlist_close(&ds->ds_deadlist);
 270  271          } else {
 271  272                  ASSERT(ds->ds_deadlist.dl_dbuf == NULL);
 272  273                  ASSERT(!ds->ds_deadlist.dl_oldfmt);
 273  274          }
 274  275          if (ds->ds_dir)
 275  276                  dsl_dir_close(ds->ds_dir, ds);
 276  277  
 277  278          ASSERT(!list_link_active(&ds->ds_synced_link));
 278  279  
 279  280          mutex_destroy(&ds->ds_lock);
 280  281          mutex_destroy(&ds->ds_recvlock);
 281  282          mutex_destroy(&ds->ds_opening_lock);
 282  283          rw_destroy(&ds->ds_rwlock);
 283  284          cv_destroy(&ds->ds_exclusive_cv);
 284  285  
 285  286          kmem_free(ds, sizeof (dsl_dataset_t));
 286  287  }
 287  288  
 288  289  static int
 289  290  dsl_dataset_get_snapname(dsl_dataset_t *ds)
 290  291  {
 291  292          dsl_dataset_phys_t *headphys;
 292  293          int err;
 293  294          dmu_buf_t *headdbuf;
 294  295          dsl_pool_t *dp = ds->ds_dir->dd_pool;
 295  296          objset_t *mos = dp->dp_meta_objset;
 296  297  
 297  298          if (ds->ds_snapname[0])
 298  299                  return (0);
 299  300          if (ds->ds_phys->ds_next_snap_obj == 0)
 300  301                  return (0);
 301  302  
 302  303          err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
 303  304              FTAG, &headdbuf);
 304  305          if (err)
 305  306                  return (err);
 306  307          headphys = headdbuf->db_data;
 307  308          err = zap_value_search(dp->dp_meta_objset,
 308  309              headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
 309  310          dmu_buf_rele(headdbuf, FTAG);
 310  311          return (err);
 311  312  }
 312  313  
 313  314  static int
 314  315  dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
 315  316  {
 316  317          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 317  318          uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
 318  319          matchtype_t mt;
 319  320          int err;
 320  321  
 321  322          if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
 322  323                  mt = MT_FIRST;
 323  324          else
  
    | ↓ open down ↓ | 266 lines elided | ↑ open up ↑ | 
 324  325                  mt = MT_EXACT;
 325  326  
 326  327          err = zap_lookup_norm(mos, snapobj, name, 8, 1,
 327  328              value, mt, NULL, 0, NULL);
 328  329          if (err == ENOTSUP && mt == MT_FIRST)
 329  330                  err = zap_lookup(mos, snapobj, name, 8, 1, value);
 330  331          return (err);
 331  332  }
 332  333  
 333  334  static int
 334      -dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
      335 +dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx,
      336 +    boolean_t adj_cnt)
 335  337  {
 336  338          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 337  339          uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
 338  340          matchtype_t mt;
 339  341          int err;
 340  342  
 341  343          dsl_dir_snap_cmtime_update(ds->ds_dir);
 342  344  
 343  345          if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
 344  346                  mt = MT_FIRST;
 345  347          else
 346  348                  mt = MT_EXACT;
 347  349  
 348  350          err = zap_remove_norm(mos, snapobj, name, mt, tx);
 349  351          if (err == ENOTSUP && mt == MT_FIRST)
 350  352                  err = zap_remove(mos, snapobj, name, tx);
      353 +
      354 +        if (err == 0 && adj_cnt)
      355 +                dsl_snapcount_adjust(ds->ds_dir, tx, -1, B_TRUE);
      356 +
 351  357          return (err);
 352  358  }
 353  359  
 354  360  static int
 355  361  dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
 356  362      dsl_dataset_t **dsp)
 357  363  {
 358  364          objset_t *mos = dp->dp_meta_objset;
 359  365          dmu_buf_t *dbuf;
 360  366          dsl_dataset_t *ds;
 361  367          int err;
 362  368          dmu_object_info_t doi;
 363  369  
 364  370          ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
 365  371              dsl_pool_sync_context(dp));
 366  372  
 367  373          err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
 368  374          if (err)
 369  375                  return (err);
 370  376  
 371  377          /* Make sure dsobj has the correct object type. */
 372  378          dmu_object_info_from_db(dbuf, &doi);
 373  379          if (doi.doi_type != DMU_OT_DSL_DATASET)
 374  380                  return (EINVAL);
 375  381  
 376  382          ds = dmu_buf_get_user(dbuf);
 377  383          if (ds == NULL) {
 378  384                  dsl_dataset_t *winner;
 379  385  
 380  386                  ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
 381  387                  ds->ds_dbuf = dbuf;
 382  388                  ds->ds_object = dsobj;
 383  389                  ds->ds_phys = dbuf->db_data;
 384  390  
 385  391                  mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
 386  392                  mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL);
 387  393                  mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
 388  394                  mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
 389  395  
 390  396                  rw_init(&ds->ds_rwlock, 0, 0, 0);
 391  397                  cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
 392  398  
 393  399                  bplist_create(&ds->ds_pending_deadlist);
 394  400                  dsl_deadlist_open(&ds->ds_deadlist,
 395  401                      mos, ds->ds_phys->ds_deadlist_obj);
 396  402  
 397  403                  list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
 398  404                      offsetof(dmu_sendarg_t, dsa_link));
 399  405  
 400  406                  if (err == 0) {
 401  407                          err = dsl_dir_open_obj(dp,
 402  408                              ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
 403  409                  }
 404  410                  if (err) {
 405  411                          mutex_destroy(&ds->ds_lock);
 406  412                          mutex_destroy(&ds->ds_recvlock);
 407  413                          mutex_destroy(&ds->ds_opening_lock);
 408  414                          rw_destroy(&ds->ds_rwlock);
 409  415                          cv_destroy(&ds->ds_exclusive_cv);
 410  416                          bplist_destroy(&ds->ds_pending_deadlist);
 411  417                          dsl_deadlist_close(&ds->ds_deadlist);
 412  418                          kmem_free(ds, sizeof (dsl_dataset_t));
 413  419                          dmu_buf_rele(dbuf, tag);
 414  420                          return (err);
 415  421                  }
 416  422  
 417  423                  if (!dsl_dataset_is_snapshot(ds)) {
 418  424                          ds->ds_snapname[0] = '\0';
 419  425                          if (ds->ds_phys->ds_prev_snap_obj) {
 420  426                                  err = dsl_dataset_get_ref(dp,
 421  427                                      ds->ds_phys->ds_prev_snap_obj,
 422  428                                      ds, &ds->ds_prev);
 423  429                          }
 424  430                  } else {
 425  431                          if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
 426  432                                  err = dsl_dataset_get_snapname(ds);
 427  433                          if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
 428  434                                  err = zap_count(
 429  435                                      ds->ds_dir->dd_pool->dp_meta_objset,
 430  436                                      ds->ds_phys->ds_userrefs_obj,
 431  437                                      &ds->ds_userrefs);
 432  438                          }
 433  439                  }
 434  440  
 435  441                  if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
 436  442                          /*
 437  443                           * In sync context, we're called with either no lock
 438  444                           * or with the write lock.  If we're not syncing,
 439  445                           * we're always called with the read lock held.
 440  446                           */
 441  447                          boolean_t need_lock =
 442  448                              !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
 443  449                              dsl_pool_sync_context(dp);
 444  450  
 445  451                          if (need_lock)
 446  452                                  rw_enter(&dp->dp_config_rwlock, RW_READER);
 447  453  
 448  454                          err = dsl_prop_get_ds(ds,
 449  455                              "refreservation", sizeof (uint64_t), 1,
 450  456                              &ds->ds_reserved, NULL);
 451  457                          if (err == 0) {
 452  458                                  err = dsl_prop_get_ds(ds,
 453  459                                      "refquota", sizeof (uint64_t), 1,
 454  460                                      &ds->ds_quota, NULL);
 455  461                          }
 456  462  
 457  463                          if (need_lock)
 458  464                                  rw_exit(&dp->dp_config_rwlock);
 459  465                  } else {
 460  466                          ds->ds_reserved = ds->ds_quota = 0;
 461  467                  }
 462  468  
 463  469                  if (err == 0) {
 464  470                          winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
 465  471                              dsl_dataset_evict);
 466  472                  }
 467  473                  if (err || winner) {
 468  474                          bplist_destroy(&ds->ds_pending_deadlist);
 469  475                          dsl_deadlist_close(&ds->ds_deadlist);
 470  476                          if (ds->ds_prev)
 471  477                                  dsl_dataset_drop_ref(ds->ds_prev, ds);
 472  478                          dsl_dir_close(ds->ds_dir, ds);
 473  479                          mutex_destroy(&ds->ds_lock);
 474  480                          mutex_destroy(&ds->ds_recvlock);
 475  481                          mutex_destroy(&ds->ds_opening_lock);
 476  482                          rw_destroy(&ds->ds_rwlock);
 477  483                          cv_destroy(&ds->ds_exclusive_cv);
 478  484                          kmem_free(ds, sizeof (dsl_dataset_t));
 479  485                          if (err) {
 480  486                                  dmu_buf_rele(dbuf, tag);
 481  487                                  return (err);
 482  488                          }
 483  489                          ds = winner;
 484  490                  } else {
 485  491                          ds->ds_fsid_guid =
 486  492                              unique_insert(ds->ds_phys->ds_fsid_guid);
 487  493                  }
 488  494          }
 489  495          ASSERT3P(ds->ds_dbuf, ==, dbuf);
 490  496          ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
 491  497          ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
 492  498              spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
 493  499              dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
 494  500          mutex_enter(&ds->ds_lock);
 495  501          if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
 496  502                  mutex_exit(&ds->ds_lock);
 497  503                  dmu_buf_rele(ds->ds_dbuf, tag);
 498  504                  return (ENOENT);
 499  505          }
 500  506          mutex_exit(&ds->ds_lock);
 501  507          *dsp = ds;
 502  508          return (0);
 503  509  }
 504  510  
 505  511  static int
 506  512  dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
 507  513  {
 508  514          dsl_pool_t *dp = ds->ds_dir->dd_pool;
 509  515  
 510  516          /*
 511  517           * In syncing context we don't want the rwlock lock: there
 512  518           * may be an existing writer waiting for sync phase to
 513  519           * finish.  We don't need to worry about such writers, since
 514  520           * sync phase is single-threaded, so the writer can't be
 515  521           * doing anything while we are active.
 516  522           */
 517  523          if (dsl_pool_sync_context(dp)) {
 518  524                  ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
 519  525                  return (0);
 520  526          }
 521  527  
 522  528          /*
 523  529           * Normal users will hold the ds_rwlock as a READER until they
 524  530           * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
 525  531           * drop their READER lock after they set the ds_owner field.
 526  532           *
 527  533           * If the dataset is being destroyed, the destroy thread will
 528  534           * obtain a WRITER lock for exclusive access after it's done its
 529  535           * open-context work and then change the ds_owner to
 530  536           * dsl_reaper once destruction is assured.  So threads
 531  537           * may block here temporarily, until the "destructability" of
 532  538           * the dataset is determined.
 533  539           */
 534  540          ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
 535  541          mutex_enter(&ds->ds_lock);
 536  542          while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
 537  543                  rw_exit(&dp->dp_config_rwlock);
 538  544                  cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
 539  545                  if (DSL_DATASET_IS_DESTROYED(ds)) {
 540  546                          mutex_exit(&ds->ds_lock);
 541  547                          dsl_dataset_drop_ref(ds, tag);
 542  548                          rw_enter(&dp->dp_config_rwlock, RW_READER);
 543  549                          return (ENOENT);
 544  550                  }
 545  551                  /*
 546  552                   * The dp_config_rwlock lives above the ds_lock. And
 547  553                   * we need to check DSL_DATASET_IS_DESTROYED() while
 548  554                   * holding the ds_lock, so we have to drop and reacquire
 549  555                   * the ds_lock here.
 550  556                   */
 551  557                  mutex_exit(&ds->ds_lock);
 552  558                  rw_enter(&dp->dp_config_rwlock, RW_READER);
 553  559                  mutex_enter(&ds->ds_lock);
 554  560          }
 555  561          mutex_exit(&ds->ds_lock);
 556  562          return (0);
 557  563  }
 558  564  
 559  565  int
 560  566  dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
 561  567      dsl_dataset_t **dsp)
 562  568  {
 563  569          int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
 564  570  
 565  571          if (err)
 566  572                  return (err);
 567  573          return (dsl_dataset_hold_ref(*dsp, tag));
 568  574  }
 569  575  
 570  576  int
 571  577  dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok,
 572  578      void *tag, dsl_dataset_t **dsp)
 573  579  {
 574  580          int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
 575  581          if (err)
 576  582                  return (err);
 577  583          if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
 578  584                  dsl_dataset_rele(*dsp, tag);
 579  585                  *dsp = NULL;
 580  586                  return (EBUSY);
 581  587          }
 582  588          return (0);
 583  589  }
 584  590  
 585  591  int
 586  592  dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
 587  593  {
 588  594          dsl_dir_t *dd;
 589  595          dsl_pool_t *dp;
 590  596          const char *snapname;
 591  597          uint64_t obj;
 592  598          int err = 0;
 593  599  
 594  600          err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
 595  601          if (err)
 596  602                  return (err);
 597  603  
 598  604          dp = dd->dd_pool;
 599  605          obj = dd->dd_phys->dd_head_dataset_obj;
 600  606          rw_enter(&dp->dp_config_rwlock, RW_READER);
 601  607          if (obj)
 602  608                  err = dsl_dataset_get_ref(dp, obj, tag, dsp);
 603  609          else
 604  610                  err = ENOENT;
 605  611          if (err)
 606  612                  goto out;
 607  613  
 608  614          err = dsl_dataset_hold_ref(*dsp, tag);
 609  615  
 610  616          /* we may be looking for a snapshot */
 611  617          if (err == 0 && snapname != NULL) {
 612  618                  dsl_dataset_t *ds = NULL;
 613  619  
 614  620                  if (*snapname++ != '@') {
 615  621                          dsl_dataset_rele(*dsp, tag);
 616  622                          err = ENOENT;
 617  623                          goto out;
 618  624                  }
 619  625  
 620  626                  dprintf("looking for snapshot '%s'\n", snapname);
 621  627                  err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
 622  628                  if (err == 0)
 623  629                          err = dsl_dataset_get_ref(dp, obj, tag, &ds);
 624  630                  dsl_dataset_rele(*dsp, tag);
 625  631  
 626  632                  ASSERT3U((err == 0), ==, (ds != NULL));
 627  633  
 628  634                  if (ds) {
 629  635                          mutex_enter(&ds->ds_lock);
 630  636                          if (ds->ds_snapname[0] == 0)
 631  637                                  (void) strlcpy(ds->ds_snapname, snapname,
 632  638                                      sizeof (ds->ds_snapname));
 633  639                          mutex_exit(&ds->ds_lock);
 634  640                          err = dsl_dataset_hold_ref(ds, tag);
 635  641                          *dsp = err ? NULL : ds;
 636  642                  }
 637  643          }
 638  644  out:
 639  645          rw_exit(&dp->dp_config_rwlock);
 640  646          dsl_dir_close(dd, FTAG);
 641  647          return (err);
 642  648  }
 643  649  
 644  650  int
 645  651  dsl_dataset_own(const char *name, boolean_t inconsistentok,
 646  652      void *tag, dsl_dataset_t **dsp)
 647  653  {
 648  654          int err = dsl_dataset_hold(name, tag, dsp);
 649  655          if (err)
 650  656                  return (err);
 651  657          if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
 652  658                  dsl_dataset_rele(*dsp, tag);
 653  659                  return (EBUSY);
 654  660          }
 655  661          return (0);
 656  662  }
 657  663  
 658  664  void
 659  665  dsl_dataset_name(dsl_dataset_t *ds, char *name)
 660  666  {
 661  667          if (ds == NULL) {
 662  668                  (void) strcpy(name, "mos");
 663  669          } else {
 664  670                  dsl_dir_name(ds->ds_dir, name);
 665  671                  VERIFY(0 == dsl_dataset_get_snapname(ds));
 666  672                  if (ds->ds_snapname[0]) {
 667  673                          (void) strcat(name, "@");
 668  674                          /*
 669  675                           * We use a "recursive" mutex so that we
 670  676                           * can call dprintf_ds() with ds_lock held.
 671  677                           */
 672  678                          if (!MUTEX_HELD(&ds->ds_lock)) {
 673  679                                  mutex_enter(&ds->ds_lock);
 674  680                                  (void) strcat(name, ds->ds_snapname);
 675  681                                  mutex_exit(&ds->ds_lock);
 676  682                          } else {
 677  683                                  (void) strcat(name, ds->ds_snapname);
 678  684                          }
 679  685                  }
 680  686          }
 681  687  }
 682  688  
 683  689  static int
 684  690  dsl_dataset_namelen(dsl_dataset_t *ds)
 685  691  {
 686  692          int result;
 687  693  
 688  694          if (ds == NULL) {
 689  695                  result = 3;     /* "mos" */
 690  696          } else {
 691  697                  result = dsl_dir_namelen(ds->ds_dir);
 692  698                  VERIFY(0 == dsl_dataset_get_snapname(ds));
 693  699                  if (ds->ds_snapname[0]) {
 694  700                          ++result;       /* adding one for the @-sign */
 695  701                          if (!MUTEX_HELD(&ds->ds_lock)) {
 696  702                                  mutex_enter(&ds->ds_lock);
 697  703                                  result += strlen(ds->ds_snapname);
 698  704                                  mutex_exit(&ds->ds_lock);
 699  705                          } else {
 700  706                                  result += strlen(ds->ds_snapname);
 701  707                          }
 702  708                  }
 703  709          }
 704  710  
 705  711          return (result);
 706  712  }
 707  713  
 708  714  void
 709  715  dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
 710  716  {
 711  717          dmu_buf_rele(ds->ds_dbuf, tag);
 712  718  }
 713  719  
 714  720  void
 715  721  dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
 716  722  {
 717  723          if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
 718  724                  rw_exit(&ds->ds_rwlock);
 719  725          }
 720  726          dsl_dataset_drop_ref(ds, tag);
 721  727  }
 722  728  
 723  729  void
 724  730  dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
 725  731  {
 726  732          ASSERT((ds->ds_owner == tag && ds->ds_dbuf) ||
 727  733              (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
 728  734  
 729  735          mutex_enter(&ds->ds_lock);
 730  736          ds->ds_owner = NULL;
 731  737          if (RW_WRITE_HELD(&ds->ds_rwlock)) {
 732  738                  rw_exit(&ds->ds_rwlock);
 733  739                  cv_broadcast(&ds->ds_exclusive_cv);
 734  740          }
 735  741          mutex_exit(&ds->ds_lock);
 736  742          if (ds->ds_dbuf)
 737  743                  dsl_dataset_drop_ref(ds, tag);
 738  744          else
 739  745                  dsl_dataset_evict(NULL, ds);
 740  746  }
 741  747  
 742  748  boolean_t
 743  749  dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag)
 744  750  {
 745  751          boolean_t gotit = FALSE;
 746  752  
 747  753          mutex_enter(&ds->ds_lock);
 748  754          if (ds->ds_owner == NULL &&
 749  755              (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
 750  756                  ds->ds_owner = tag;
 751  757                  if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
 752  758                          rw_exit(&ds->ds_rwlock);
 753  759                  gotit = TRUE;
 754  760          }
 755  761          mutex_exit(&ds->ds_lock);
 756  762          return (gotit);
 757  763  }
 758  764  
 759  765  void
 760  766  dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
 761  767  {
 762  768          ASSERT3P(owner, ==, ds->ds_owner);
 763  769          if (!RW_WRITE_HELD(&ds->ds_rwlock))
 764  770                  rw_enter(&ds->ds_rwlock, RW_WRITER);
 765  771  }
 766  772  
 767  773  uint64_t
 768  774  dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
 769  775      uint64_t flags, dmu_tx_t *tx)
 770  776  {
 771  777          dsl_pool_t *dp = dd->dd_pool;
 772  778          dmu_buf_t *dbuf;
 773  779          dsl_dataset_phys_t *dsphys;
 774  780          uint64_t dsobj;
 775  781          objset_t *mos = dp->dp_meta_objset;
 776  782  
 777  783          if (origin == NULL)
 778  784                  origin = dp->dp_origin_snap;
 779  785  
 780  786          ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
 781  787          ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
 782  788          ASSERT(dmu_tx_is_syncing(tx));
 783  789          ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
 784  790  
 785  791          dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
 786  792              DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
 787  793          VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
 788  794          dmu_buf_will_dirty(dbuf, tx);
 789  795          dsphys = dbuf->db_data;
 790  796          bzero(dsphys, sizeof (dsl_dataset_phys_t));
 791  797          dsphys->ds_dir_obj = dd->dd_object;
 792  798          dsphys->ds_flags = flags;
 793  799          dsphys->ds_fsid_guid = unique_create();
 794  800          (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
 795  801              sizeof (dsphys->ds_guid));
 796  802          dsphys->ds_snapnames_zapobj =
 797  803              zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
 798  804              DMU_OT_NONE, 0, tx);
 799  805          dsphys->ds_creation_time = gethrestime_sec();
 800  806          dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
 801  807  
 802  808          if (origin == NULL) {
 803  809                  dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
 804  810          } else {
 805  811                  dsl_dataset_t *ohds;
 806  812  
 807  813                  dsphys->ds_prev_snap_obj = origin->ds_object;
 808  814                  dsphys->ds_prev_snap_txg =
 809  815                      origin->ds_phys->ds_creation_txg;
 810  816                  dsphys->ds_referenced_bytes =
 811  817                      origin->ds_phys->ds_referenced_bytes;
 812  818                  dsphys->ds_compressed_bytes =
 813  819                      origin->ds_phys->ds_compressed_bytes;
 814  820                  dsphys->ds_uncompressed_bytes =
 815  821                      origin->ds_phys->ds_uncompressed_bytes;
 816  822                  dsphys->ds_bp = origin->ds_phys->ds_bp;
 817  823                  dsphys->ds_flags |= origin->ds_phys->ds_flags;
 818  824  
 819  825                  dmu_buf_will_dirty(origin->ds_dbuf, tx);
 820  826                  origin->ds_phys->ds_num_children++;
 821  827  
 822  828                  VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
 823  829                      origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
 824  830                  dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
 825  831                      dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
 826  832                  dsl_dataset_rele(ohds, FTAG);
 827  833  
 828  834                  if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
 829  835                          if (origin->ds_phys->ds_next_clones_obj == 0) {
 830  836                                  origin->ds_phys->ds_next_clones_obj =
 831  837                                      zap_create(mos,
 832  838                                      DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
 833  839                          }
 834  840                          VERIFY(0 == zap_add_int(mos,
 835  841                              origin->ds_phys->ds_next_clones_obj,
 836  842                              dsobj, tx));
 837  843                  }
 838  844  
 839  845                  dmu_buf_will_dirty(dd->dd_dbuf, tx);
 840  846                  dd->dd_phys->dd_origin_obj = origin->ds_object;
 841  847                  if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
 842  848                          if (origin->ds_dir->dd_phys->dd_clones == 0) {
 843  849                                  dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
 844  850                                  origin->ds_dir->dd_phys->dd_clones =
 845  851                                      zap_create(mos,
 846  852                                      DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
 847  853                          }
 848  854                          VERIFY3U(0, ==, zap_add_int(mos,
 849  855                              origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
 850  856                  }
 851  857          }
 852  858  
 853  859          if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
 854  860                  dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
 855  861  
 856  862          dmu_buf_rele(dbuf, FTAG);
 857  863  
 858  864          dmu_buf_will_dirty(dd->dd_dbuf, tx);
 859  865          dd->dd_phys->dd_head_dataset_obj = dsobj;
 860  866  
 861  867          return (dsobj);
 862  868  }
 863  869  
 864  870  uint64_t
 865  871  dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
 866  872      dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
 867  873  {
 868  874          dsl_pool_t *dp = pdd->dd_pool;
 869  875          uint64_t dsobj, ddobj;
 870  876          dsl_dir_t *dd;
 871  877  
 872  878          ASSERT(lastname[0] != '@');
 873  879  
 874  880          ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
 875  881          VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
 876  882  
 877  883          dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
 878  884  
 879  885          dsl_deleg_set_create_perms(dd, tx, cr);
 880  886  
 881  887          dsl_dir_close(dd, FTAG);
 882  888  
 883  889          /*
 884  890           * If we are creating a clone, make sure we zero out any stale
 885  891           * data from the origin snapshots zil header.
 886  892           */
 887  893          if (origin != NULL) {
 888  894                  dsl_dataset_t *ds;
 889  895                  objset_t *os;
 890  896  
 891  897                  VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
 892  898                  VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
 893  899                  bzero(&os->os_zil_header, sizeof (os->os_zil_header));
 894  900                  dsl_dataset_dirty(ds, tx);
 895  901                  dsl_dataset_rele(ds, FTAG);
 896  902          }
 897  903  
 898  904          return (dsobj);
 899  905  }
 900  906  
 901  907  /*
 902  908   * The snapshots must all be in the same pool.
 903  909   */
 904  910  int
 905  911  dmu_snapshots_destroy_nvl(nvlist_t *snaps, boolean_t defer,
 906  912      nvlist_t *errlist)
 907  913  {
 908  914          int err;
 909  915          dsl_sync_task_t *dst;
 910  916          spa_t *spa;
 911  917          nvpair_t *pair;
 912  918          dsl_sync_task_group_t *dstg;
 913  919  
 914  920          pair = nvlist_next_nvpair(snaps, NULL);
 915  921          if (pair == NULL)
 916  922                  return (0);
 917  923  
 918  924          err = spa_open(nvpair_name(pair), &spa, FTAG);
 919  925          if (err)
 920  926                  return (err);
 921  927          dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
 922  928  
 923  929          for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
 924  930              pair = nvlist_next_nvpair(snaps, pair)) {
 925  931                  dsl_dataset_t *ds;
 926  932  
 927  933                  err = dsl_dataset_own(nvpair_name(pair), B_TRUE, dstg, &ds);
 928  934                  if (err == 0) {
 929  935                          struct dsl_ds_destroyarg *dsda;
 930  936  
 931  937                          dsl_dataset_make_exclusive(ds, dstg);
 932  938                          dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg),
 933  939                              KM_SLEEP);
 934  940                          dsda->ds = ds;
 935  941                          dsda->defer = defer;
 936  942                          dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
 937  943                              dsl_dataset_destroy_sync, dsda, dstg, 0);
 938  944                  } else if (err == ENOENT) {
 939  945                          err = 0;
 940  946                  } else {
 941  947                          fnvlist_add_int32(errlist, nvpair_name(pair), err);
 942  948                          break;
 943  949                  }
 944  950          }
 945  951  
 946  952          if (err == 0)
 947  953                  err = dsl_sync_task_group_wait(dstg);
 948  954  
 949  955          for (dst = list_head(&dstg->dstg_tasks); dst;
 950  956              dst = list_next(&dstg->dstg_tasks, dst)) {
 951  957                  struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
 952  958                  dsl_dataset_t *ds = dsda->ds;
 953  959  
 954  960                  /*
 955  961                   * Return the snapshots that triggered the error.
 956  962                   */
 957  963                  if (dst->dst_err != 0) {
 958  964                          char name[ZFS_MAXNAMELEN];
 959  965                          dsl_dataset_name(ds, name);
 960  966                          fnvlist_add_int32(errlist, name, dst->dst_err);
 961  967                  }
 962  968                  ASSERT3P(dsda->rm_origin, ==, NULL);
 963  969                  dsl_dataset_disown(ds, dstg);
 964  970                  kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
 965  971          }
 966  972  
 967  973          dsl_sync_task_group_destroy(dstg);
 968  974          spa_close(spa, FTAG);
 969  975          return (err);
 970  976  
 971  977  }
 972  978  
 973  979  static boolean_t
 974  980  dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
 975  981  {
 976  982          boolean_t might_destroy = B_FALSE;
 977  983  
 978  984          mutex_enter(&ds->ds_lock);
 979  985          if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
 980  986              DS_IS_DEFER_DESTROY(ds))
 981  987                  might_destroy = B_TRUE;
 982  988          mutex_exit(&ds->ds_lock);
 983  989  
 984  990          return (might_destroy);
 985  991  }
 986  992  
 987  993  /*
 988  994   * If we're removing a clone, and these three conditions are true:
 989  995   *      1) the clone's origin has no other children
 990  996   *      2) the clone's origin has no user references
 991  997   *      3) the clone's origin has been marked for deferred destruction
 992  998   * Then, prepare to remove the origin as part of this sync task group.
 993  999   */
 994 1000  static int
 995 1001  dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag)
 996 1002  {
 997 1003          dsl_dataset_t *ds = dsda->ds;
 998 1004          dsl_dataset_t *origin = ds->ds_prev;
 999 1005  
1000 1006          if (dsl_dataset_might_destroy_origin(origin)) {
1001 1007                  char *name;
1002 1008                  int namelen;
1003 1009                  int error;
1004 1010  
1005 1011                  namelen = dsl_dataset_namelen(origin) + 1;
1006 1012                  name = kmem_alloc(namelen, KM_SLEEP);
1007 1013                  dsl_dataset_name(origin, name);
1008 1014  #ifdef _KERNEL
1009 1015                  error = zfs_unmount_snap(name, NULL);
1010 1016                  if (error) {
1011 1017                          kmem_free(name, namelen);
1012 1018                          return (error);
1013 1019                  }
1014 1020  #endif
1015 1021                  error = dsl_dataset_own(name, B_TRUE, tag, &origin);
1016 1022                  kmem_free(name, namelen);
1017 1023                  if (error)
1018 1024                          return (error);
1019 1025                  dsda->rm_origin = origin;
1020 1026                  dsl_dataset_make_exclusive(origin, tag);
1021 1027          }
1022 1028  
1023 1029          return (0);
1024 1030  }
1025 1031  
1026 1032  /*
1027 1033   * ds must be opened as OWNER.  On return (whether successful or not),
1028 1034   * ds will be closed and caller can no longer dereference it.
1029 1035   */
1030 1036  int
1031 1037  dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
1032 1038  {
1033 1039          int err;
1034 1040          dsl_sync_task_group_t *dstg;
1035 1041          objset_t *os;
1036 1042          dsl_dir_t *dd;
1037 1043          uint64_t obj;
1038 1044          struct dsl_ds_destroyarg dsda = { 0 };
1039 1045  
1040 1046          dsda.ds = ds;
1041 1047  
1042 1048          if (dsl_dataset_is_snapshot(ds)) {
1043 1049                  /* Destroying a snapshot is simpler */
1044 1050                  dsl_dataset_make_exclusive(ds, tag);
1045 1051  
1046 1052                  dsda.defer = defer;
1047 1053                  err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1048 1054                      dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
1049 1055                      &dsda, tag, 0);
1050 1056                  ASSERT3P(dsda.rm_origin, ==, NULL);
1051 1057                  goto out;
1052 1058          } else if (defer) {
1053 1059                  err = EINVAL;
1054 1060                  goto out;
1055 1061          }
1056 1062  
1057 1063          dd = ds->ds_dir;
1058 1064  
1059 1065          if (!spa_feature_is_enabled(dsl_dataset_get_spa(ds),
1060 1066              &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1061 1067                  /*
1062 1068                   * Check for errors and mark this ds as inconsistent, in
1063 1069                   * case we crash while freeing the objects.
1064 1070                   */
1065 1071                  err = dsl_sync_task_do(dd->dd_pool,
1066 1072                      dsl_dataset_destroy_begin_check,
1067 1073                      dsl_dataset_destroy_begin_sync, ds, NULL, 0);
1068 1074                  if (err)
1069 1075                          goto out;
1070 1076  
1071 1077                  err = dmu_objset_from_ds(ds, &os);
1072 1078                  if (err)
1073 1079                          goto out;
1074 1080  
1075 1081                  /*
1076 1082                   * Remove all objects while in the open context so that
1077 1083                   * there is less work to do in the syncing context.
1078 1084                   */
1079 1085                  for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
1080 1086                      ds->ds_phys->ds_prev_snap_txg)) {
1081 1087                          /*
1082 1088                           * Ignore errors, if there is not enough disk space
1083 1089                           * we will deal with it in dsl_dataset_destroy_sync().
1084 1090                           */
1085 1091                          (void) dmu_free_object(os, obj);
1086 1092                  }
1087 1093                  if (err != ESRCH)
1088 1094                          goto out;
1089 1095  
1090 1096                  /*
1091 1097                   * Sync out all in-flight IO.
1092 1098                   */
1093 1099                  txg_wait_synced(dd->dd_pool, 0);
1094 1100  
1095 1101                  /*
1096 1102                   * If we managed to free all the objects in open
1097 1103                   * context, the user space accounting should be zero.
1098 1104                   */
1099 1105                  if (ds->ds_phys->ds_bp.blk_fill == 0 &&
1100 1106                      dmu_objset_userused_enabled(os)) {
1101 1107                          uint64_t count;
1102 1108  
1103 1109                          ASSERT(zap_count(os, DMU_USERUSED_OBJECT,
1104 1110                              &count) != 0 || count == 0);
1105 1111                          ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT,
1106 1112                              &count) != 0 || count == 0);
1107 1113                  }
1108 1114          }
1109 1115  
1110 1116          rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
1111 1117          err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
1112 1118          rw_exit(&dd->dd_pool->dp_config_rwlock);
1113 1119  
1114 1120          if (err)
1115 1121                  goto out;
1116 1122  
1117 1123          /*
1118 1124           * Blow away the dsl_dir + head dataset.
1119 1125           */
1120 1126          dsl_dataset_make_exclusive(ds, tag);
1121 1127          /*
1122 1128           * If we're removing a clone, we might also need to remove its
1123 1129           * origin.
1124 1130           */
1125 1131          do {
1126 1132                  dsda.need_prep = B_FALSE;
1127 1133                  if (dsl_dir_is_clone(dd)) {
1128 1134                          err = dsl_dataset_origin_rm_prep(&dsda, tag);
1129 1135                          if (err) {
1130 1136                                  dsl_dir_close(dd, FTAG);
1131 1137                                  goto out;
1132 1138                          }
1133 1139                  }
1134 1140  
1135 1141                  dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
1136 1142                  dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
1137 1143                      dsl_dataset_destroy_sync, &dsda, tag, 0);
1138 1144                  dsl_sync_task_create(dstg, dsl_dir_destroy_check,
1139 1145                      dsl_dir_destroy_sync, dd, FTAG, 0);
1140 1146                  err = dsl_sync_task_group_wait(dstg);
1141 1147                  dsl_sync_task_group_destroy(dstg);
1142 1148  
1143 1149                  /*
1144 1150                   * We could be racing against 'zfs release' or 'zfs destroy -d'
1145 1151                   * on the origin snap, in which case we can get EBUSY if we
1146 1152                   * needed to destroy the origin snap but were not ready to
1147 1153                   * do so.
1148 1154                   */
1149 1155                  if (dsda.need_prep) {
1150 1156                          ASSERT(err == EBUSY);
1151 1157                          ASSERT(dsl_dir_is_clone(dd));
1152 1158                          ASSERT(dsda.rm_origin == NULL);
1153 1159                  }
1154 1160          } while (dsda.need_prep);
1155 1161  
1156 1162          if (dsda.rm_origin != NULL)
1157 1163                  dsl_dataset_disown(dsda.rm_origin, tag);
1158 1164  
1159 1165          /* if it is successful, dsl_dir_destroy_sync will close the dd */
1160 1166          if (err)
1161 1167                  dsl_dir_close(dd, FTAG);
1162 1168  out:
1163 1169          dsl_dataset_disown(ds, tag);
1164 1170          return (err);
1165 1171  }
1166 1172  
1167 1173  blkptr_t *
1168 1174  dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1169 1175  {
1170 1176          return (&ds->ds_phys->ds_bp);
1171 1177  }
1172 1178  
1173 1179  void
1174 1180  dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1175 1181  {
1176 1182          ASSERT(dmu_tx_is_syncing(tx));
1177 1183          /* If it's the meta-objset, set dp_meta_rootbp */
1178 1184          if (ds == NULL) {
1179 1185                  tx->tx_pool->dp_meta_rootbp = *bp;
1180 1186          } else {
1181 1187                  dmu_buf_will_dirty(ds->ds_dbuf, tx);
1182 1188                  ds->ds_phys->ds_bp = *bp;
1183 1189          }
1184 1190  }
1185 1191  
1186 1192  spa_t *
1187 1193  dsl_dataset_get_spa(dsl_dataset_t *ds)
1188 1194  {
1189 1195          return (ds->ds_dir->dd_pool->dp_spa);
1190 1196  }
1191 1197  
1192 1198  void
1193 1199  dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1194 1200  {
1195 1201          dsl_pool_t *dp;
1196 1202  
1197 1203          if (ds == NULL) /* this is the meta-objset */
1198 1204                  return;
1199 1205  
1200 1206          ASSERT(ds->ds_objset != NULL);
1201 1207  
1202 1208          if (ds->ds_phys->ds_next_snap_obj != 0)
1203 1209                  panic("dirtying snapshot!");
1204 1210  
1205 1211          dp = ds->ds_dir->dd_pool;
1206 1212  
1207 1213          if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1208 1214                  /* up the hold count until we can be written out */
1209 1215                  dmu_buf_add_ref(ds->ds_dbuf, ds);
1210 1216          }
1211 1217  }
1212 1218  
1213 1219  boolean_t
1214 1220  dsl_dataset_is_dirty(dsl_dataset_t *ds)
1215 1221  {
1216 1222          for (int t = 0; t < TXG_SIZE; t++) {
1217 1223                  if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1218 1224                      ds, t))
1219 1225                          return (B_TRUE);
1220 1226          }
1221 1227          return (B_FALSE);
1222 1228  }
1223 1229  
1224 1230  /*
1225 1231   * The unique space in the head dataset can be calculated by subtracting
1226 1232   * the space used in the most recent snapshot, that is still being used
1227 1233   * in this file system, from the space currently in use.  To figure out
1228 1234   * the space in the most recent snapshot still in use, we need to take
1229 1235   * the total space used in the snapshot and subtract out the space that
1230 1236   * has been freed up since the snapshot was taken.
1231 1237   */
1232 1238  static void
1233 1239  dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1234 1240  {
1235 1241          uint64_t mrs_used;
1236 1242          uint64_t dlused, dlcomp, dluncomp;
1237 1243  
1238 1244          ASSERT(!dsl_dataset_is_snapshot(ds));
1239 1245  
1240 1246          if (ds->ds_phys->ds_prev_snap_obj != 0)
1241 1247                  mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
1242 1248          else
1243 1249                  mrs_used = 0;
1244 1250  
1245 1251          dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1246 1252  
1247 1253          ASSERT3U(dlused, <=, mrs_used);
1248 1254          ds->ds_phys->ds_unique_bytes =
1249 1255              ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
1250 1256  
1251 1257          if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1252 1258              SPA_VERSION_UNIQUE_ACCURATE)
1253 1259                  ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1254 1260  }
1255 1261  
1256 1262  struct killarg {
1257 1263          dsl_dataset_t *ds;
1258 1264          dmu_tx_t *tx;
1259 1265  };
1260 1266  
1261 1267  /* ARGSUSED */
1262 1268  static int
1263 1269  kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
1264 1270      const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1265 1271  {
1266 1272          struct killarg *ka = arg;
1267 1273          dmu_tx_t *tx = ka->tx;
1268 1274  
1269 1275          if (bp == NULL)
1270 1276                  return (0);
1271 1277  
1272 1278          if (zb->zb_level == ZB_ZIL_LEVEL) {
1273 1279                  ASSERT(zilog != NULL);
1274 1280                  /*
1275 1281                   * It's a block in the intent log.  It has no
1276 1282                   * accounting, so just free it.
1277 1283                   */
1278 1284                  dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
1279 1285          } else {
1280 1286                  ASSERT(zilog == NULL);
1281 1287                  ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
1282 1288                  (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
1283 1289          }
1284 1290  
1285 1291          return (0);
1286 1292  }
1287 1293  
1288 1294  /* ARGSUSED */
1289 1295  static int
1290 1296  dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
1291 1297  {
1292 1298          dsl_dataset_t *ds = arg1;
1293 1299          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1294 1300          uint64_t count;
1295 1301          int err;
1296 1302  
1297 1303          /*
1298 1304           * Can't delete a head dataset if there are snapshots of it.
1299 1305           * (Except if the only snapshots are from the branch we cloned
1300 1306           * from.)
1301 1307           */
1302 1308          if (ds->ds_prev != NULL &&
1303 1309              ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1304 1310                  return (EBUSY);
1305 1311  
1306 1312          /*
1307 1313           * This is really a dsl_dir thing, but check it here so that
1308 1314           * we'll be less likely to leave this dataset inconsistent &
1309 1315           * nearly destroyed.
1310 1316           */
1311 1317          err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
1312 1318          if (err)
1313 1319                  return (err);
1314 1320          if (count != 0)
1315 1321                  return (EEXIST);
1316 1322  
1317 1323          return (0);
1318 1324  }
1319 1325  
1320 1326  /* ARGSUSED */
1321 1327  static void
1322 1328  dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1323 1329  {
1324 1330          dsl_dataset_t *ds = arg1;
1325 1331  
1326 1332          /* Mark it as inconsistent on-disk, in case we crash */
1327 1333          dmu_buf_will_dirty(ds->ds_dbuf, tx);
1328 1334          ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1329 1335  
1330 1336          spa_history_log_internal_ds(ds, "destroy begin", tx, "");
1331 1337  }
1332 1338  
1333 1339  static int
1334 1340  dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
1335 1341      dmu_tx_t *tx)
1336 1342  {
1337 1343          dsl_dataset_t *ds = dsda->ds;
1338 1344          dsl_dataset_t *ds_prev = ds->ds_prev;
1339 1345  
1340 1346          if (dsl_dataset_might_destroy_origin(ds_prev)) {
1341 1347                  struct dsl_ds_destroyarg ndsda = {0};
1342 1348  
1343 1349                  /*
1344 1350                   * If we're not prepared to remove the origin, don't remove
1345 1351                   * the clone either.
1346 1352                   */
1347 1353                  if (dsda->rm_origin == NULL) {
1348 1354                          dsda->need_prep = B_TRUE;
1349 1355                          return (EBUSY);
1350 1356                  }
1351 1357  
1352 1358                  ndsda.ds = ds_prev;
1353 1359                  ndsda.is_origin_rm = B_TRUE;
1354 1360                  return (dsl_dataset_destroy_check(&ndsda, tag, tx));
1355 1361          }
1356 1362  
1357 1363          /*
1358 1364           * If we're not going to remove the origin after all,
1359 1365           * undo the open context setup.
1360 1366           */
1361 1367          if (dsda->rm_origin != NULL) {
1362 1368                  dsl_dataset_disown(dsda->rm_origin, tag);
1363 1369                  dsda->rm_origin = NULL;
1364 1370          }
1365 1371  
1366 1372          return (0);
1367 1373  }
1368 1374  
1369 1375  /*
1370 1376   * If you add new checks here, you may need to add
1371 1377   * additional checks to the "temporary" case in
1372 1378   * snapshot_check() in dmu_objset.c.
1373 1379   */
1374 1380  /* ARGSUSED */
1375 1381  int
1376 1382  dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
1377 1383  {
1378 1384          struct dsl_ds_destroyarg *dsda = arg1;
1379 1385          dsl_dataset_t *ds = dsda->ds;
1380 1386  
1381 1387          /* we have an owner hold, so noone else can destroy us */
1382 1388          ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
1383 1389  
1384 1390          /*
1385 1391           * Only allow deferred destroy on pools that support it.
1386 1392           * NOTE: deferred destroy is only supported on snapshots.
1387 1393           */
1388 1394          if (dsda->defer) {
1389 1395                  if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
1390 1396                      SPA_VERSION_USERREFS)
1391 1397                          return (ENOTSUP);
1392 1398                  ASSERT(dsl_dataset_is_snapshot(ds));
1393 1399                  return (0);
1394 1400          }
1395 1401  
1396 1402          /*
1397 1403           * Can't delete a head dataset if there are snapshots of it.
1398 1404           * (Except if the only snapshots are from the branch we cloned
1399 1405           * from.)
1400 1406           */
1401 1407          if (ds->ds_prev != NULL &&
1402 1408              ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1403 1409                  return (EBUSY);
1404 1410  
1405 1411          /*
1406 1412           * If we made changes this txg, traverse_dsl_dataset won't find
1407 1413           * them.  Try again.
1408 1414           */
1409 1415          if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1410 1416                  return (EAGAIN);
1411 1417  
1412 1418          if (dsl_dataset_is_snapshot(ds)) {
1413 1419                  /*
1414 1420                   * If this snapshot has an elevated user reference count,
1415 1421                   * we can't destroy it yet.
1416 1422                   */
1417 1423                  if (ds->ds_userrefs > 0 && !dsda->releasing)
1418 1424                          return (EBUSY);
1419 1425  
1420 1426                  mutex_enter(&ds->ds_lock);
1421 1427                  /*
1422 1428                   * Can't delete a branch point. However, if we're destroying
1423 1429                   * a clone and removing its origin due to it having a user
1424 1430                   * hold count of 0 and having been marked for deferred destroy,
1425 1431                   * it's OK for the origin to have a single clone.
1426 1432                   */
1427 1433                  if (ds->ds_phys->ds_num_children >
1428 1434                      (dsda->is_origin_rm ? 2 : 1)) {
1429 1435                          mutex_exit(&ds->ds_lock);
1430 1436                          return (EEXIST);
1431 1437                  }
1432 1438                  mutex_exit(&ds->ds_lock);
1433 1439          } else if (dsl_dir_is_clone(ds->ds_dir)) {
1434 1440                  return (dsl_dataset_origin_check(dsda, arg2, tx));
1435 1441          }
1436 1442  
1437 1443          /* XXX we should do some i/o error checking... */
1438 1444          return (0);
1439 1445  }
1440 1446  
1441 1447  struct refsarg {
1442 1448          kmutex_t lock;
1443 1449          boolean_t gone;
1444 1450          kcondvar_t cv;
1445 1451  };
1446 1452  
1447 1453  /* ARGSUSED */
1448 1454  static void
1449 1455  dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
1450 1456  {
1451 1457          struct refsarg *arg = argv;
1452 1458  
1453 1459          mutex_enter(&arg->lock);
1454 1460          arg->gone = TRUE;
1455 1461          cv_signal(&arg->cv);
1456 1462          mutex_exit(&arg->lock);
1457 1463  }
1458 1464  
1459 1465  static void
1460 1466  dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
1461 1467  {
1462 1468          struct refsarg arg;
1463 1469  
1464 1470          mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
1465 1471          cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
1466 1472          arg.gone = FALSE;
1467 1473          (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
1468 1474              dsl_dataset_refs_gone);
1469 1475          dmu_buf_rele(ds->ds_dbuf, tag);
1470 1476          mutex_enter(&arg.lock);
1471 1477          while (!arg.gone)
1472 1478                  cv_wait(&arg.cv, &arg.lock);
1473 1479          ASSERT(arg.gone);
1474 1480          mutex_exit(&arg.lock);
1475 1481          ds->ds_dbuf = NULL;
1476 1482          ds->ds_phys = NULL;
1477 1483          mutex_destroy(&arg.lock);
1478 1484          cv_destroy(&arg.cv);
1479 1485  }
1480 1486  
1481 1487  static void
1482 1488  remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx)
1483 1489  {
1484 1490          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1485 1491          uint64_t count;
1486 1492          int err;
1487 1493  
1488 1494          ASSERT(ds->ds_phys->ds_num_children >= 2);
1489 1495          err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
1490 1496          /*
1491 1497           * The err should not be ENOENT, but a bug in a previous version
1492 1498           * of the code could cause upgrade_clones_cb() to not set
1493 1499           * ds_next_snap_obj when it should, leading to a missing entry.
1494 1500           * If we knew that the pool was created after
1495 1501           * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1496 1502           * ENOENT.  However, at least we can check that we don't have
1497 1503           * too many entries in the next_clones_obj even after failing to
1498 1504           * remove this one.
1499 1505           */
1500 1506          if (err != ENOENT) {
1501 1507                  VERIFY0(err);
1502 1508          }
1503 1509          ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1504 1510              &count));
1505 1511          ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
1506 1512  }
1507 1513  
1508 1514  static void
1509 1515  dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
1510 1516  {
1511 1517          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1512 1518          zap_cursor_t zc;
1513 1519          zap_attribute_t za;
1514 1520  
1515 1521          /*
1516 1522           * If it is the old version, dd_clones doesn't exist so we can't
1517 1523           * find the clones, but deadlist_remove_key() is a no-op so it
1518 1524           * doesn't matter.
1519 1525           */
1520 1526          if (ds->ds_dir->dd_phys->dd_clones == 0)
1521 1527                  return;
1522 1528  
1523 1529          for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
1524 1530              zap_cursor_retrieve(&zc, &za) == 0;
1525 1531              zap_cursor_advance(&zc)) {
1526 1532                  dsl_dataset_t *clone;
1527 1533  
1528 1534                  VERIFY3U(0, ==, dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1529 1535                      za.za_first_integer, FTAG, &clone));
1530 1536                  if (clone->ds_dir->dd_origin_txg > mintxg) {
1531 1537                          dsl_deadlist_remove_key(&clone->ds_deadlist,
1532 1538                              mintxg, tx);
1533 1539                          dsl_dataset_remove_clones_key(clone, mintxg, tx);
1534 1540                  }
1535 1541                  dsl_dataset_rele(clone, FTAG);
1536 1542          }
1537 1543          zap_cursor_fini(&zc);
1538 1544  }
1539 1545  
1540 1546  struct process_old_arg {
1541 1547          dsl_dataset_t *ds;
1542 1548          dsl_dataset_t *ds_prev;
1543 1549          boolean_t after_branch_point;
1544 1550          zio_t *pio;
1545 1551          uint64_t used, comp, uncomp;
1546 1552  };
1547 1553  
1548 1554  static int
1549 1555  process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1550 1556  {
1551 1557          struct process_old_arg *poa = arg;
1552 1558          dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
1553 1559  
1554 1560          if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
1555 1561                  dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
1556 1562                  if (poa->ds_prev && !poa->after_branch_point &&
1557 1563                      bp->blk_birth >
1558 1564                      poa->ds_prev->ds_phys->ds_prev_snap_txg) {
1559 1565                          poa->ds_prev->ds_phys->ds_unique_bytes +=
1560 1566                              bp_get_dsize_sync(dp->dp_spa, bp);
1561 1567                  }
1562 1568          } else {
1563 1569                  poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
1564 1570                  poa->comp += BP_GET_PSIZE(bp);
1565 1571                  poa->uncomp += BP_GET_UCSIZE(bp);
1566 1572                  dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
1567 1573          }
1568 1574          return (0);
1569 1575  }
1570 1576  
1571 1577  static void
1572 1578  process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
1573 1579      dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
1574 1580  {
1575 1581          struct process_old_arg poa = { 0 };
1576 1582          dsl_pool_t *dp = ds->ds_dir->dd_pool;
1577 1583          objset_t *mos = dp->dp_meta_objset;
1578 1584  
1579 1585          ASSERT(ds->ds_deadlist.dl_oldfmt);
1580 1586          ASSERT(ds_next->ds_deadlist.dl_oldfmt);
1581 1587  
1582 1588          poa.ds = ds;
1583 1589          poa.ds_prev = ds_prev;
1584 1590          poa.after_branch_point = after_branch_point;
1585 1591          poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1586 1592          VERIFY3U(0, ==, bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
1587 1593              process_old_cb, &poa, tx));
1588 1594          VERIFY0(zio_wait(poa.pio));
1589 1595          ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
1590 1596  
1591 1597          /* change snapused */
1592 1598          dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1593 1599              -poa.used, -poa.comp, -poa.uncomp, tx);
1594 1600  
1595 1601          /* swap next's deadlist to our deadlist */
1596 1602          dsl_deadlist_close(&ds->ds_deadlist);
1597 1603          dsl_deadlist_close(&ds_next->ds_deadlist);
1598 1604          SWITCH64(ds_next->ds_phys->ds_deadlist_obj,
1599 1605              ds->ds_phys->ds_deadlist_obj);
1600 1606          dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1601 1607          dsl_deadlist_open(&ds_next->ds_deadlist, mos,
1602 1608              ds_next->ds_phys->ds_deadlist_obj);
1603 1609  }
1604 1610  
1605 1611  static int
1606 1612  old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
1607 1613  {
1608 1614          int err;
1609 1615          struct killarg ka;
1610 1616  
1611 1617          /*
1612 1618           * Free everything that we point to (that's born after
1613 1619           * the previous snapshot, if we are a clone)
1614 1620           *
1615 1621           * NB: this should be very quick, because we already
1616 1622           * freed all the objects in open context.
1617 1623           */
1618 1624          ka.ds = ds;
1619 1625          ka.tx = tx;
1620 1626          err = traverse_dataset(ds,
1621 1627              ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
1622 1628              kill_blkptr, &ka);
1623 1629          ASSERT0(err);
1624 1630          ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
1625 1631  
1626 1632          return (err);
1627 1633  }
1628 1634  
1629 1635  void
1630 1636  dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
1631 1637  {
1632 1638          struct dsl_ds_destroyarg *dsda = arg1;
1633 1639          dsl_dataset_t *ds = dsda->ds;
1634 1640          int err;
1635 1641          int after_branch_point = FALSE;
1636 1642          dsl_pool_t *dp = ds->ds_dir->dd_pool;
1637 1643          objset_t *mos = dp->dp_meta_objset;
1638 1644          dsl_dataset_t *ds_prev = NULL;
1639 1645          boolean_t wont_destroy;
1640 1646          uint64_t obj;
1641 1647  
1642 1648          wont_destroy = (dsda->defer &&
1643 1649              (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1));
1644 1650  
1645 1651          ASSERT(ds->ds_owner || wont_destroy);
1646 1652          ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
1647 1653          ASSERT(ds->ds_prev == NULL ||
1648 1654              ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
1649 1655          ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
1650 1656  
1651 1657          if (wont_destroy) {
1652 1658                  ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
1653 1659                  dmu_buf_will_dirty(ds->ds_dbuf, tx);
1654 1660                  ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
1655 1661                  spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
1656 1662                  return;
1657 1663          }
1658 1664  
1659 1665          /* We need to log before removing it from the namespace. */
1660 1666          spa_history_log_internal_ds(ds, "destroy", tx, "");
1661 1667  
1662 1668          /* signal any waiters that this dataset is going away */
1663 1669          mutex_enter(&ds->ds_lock);
1664 1670          ds->ds_owner = dsl_reaper;
1665 1671          cv_broadcast(&ds->ds_exclusive_cv);
1666 1672          mutex_exit(&ds->ds_lock);
1667 1673  
1668 1674          /* Remove our reservation */
1669 1675          if (ds->ds_reserved != 0) {
1670 1676                  dsl_prop_setarg_t psa;
1671 1677                  uint64_t value = 0;
1672 1678  
1673 1679                  dsl_prop_setarg_init_uint64(&psa, "refreservation",
1674 1680                      (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
1675 1681                      &value);
1676 1682                  psa.psa_effective_value = 0;    /* predict default value */
1677 1683  
1678 1684                  dsl_dataset_set_reservation_sync(ds, &psa, tx);
1679 1685                  ASSERT0(ds->ds_reserved);
1680 1686          }
1681 1687  
1682 1688          ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1683 1689  
1684 1690          dsl_scan_ds_destroyed(ds, tx);
1685 1691  
1686 1692          obj = ds->ds_object;
1687 1693  
1688 1694          if (ds->ds_phys->ds_prev_snap_obj != 0) {
1689 1695                  if (ds->ds_prev) {
1690 1696                          ds_prev = ds->ds_prev;
1691 1697                  } else {
1692 1698                          VERIFY(0 == dsl_dataset_hold_obj(dp,
1693 1699                              ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1694 1700                  }
1695 1701                  after_branch_point =
1696 1702                      (ds_prev->ds_phys->ds_next_snap_obj != obj);
1697 1703  
1698 1704                  dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1699 1705                  if (after_branch_point &&
1700 1706                      ds_prev->ds_phys->ds_next_clones_obj != 0) {
1701 1707                          remove_from_next_clones(ds_prev, obj, tx);
1702 1708                          if (ds->ds_phys->ds_next_snap_obj != 0) {
1703 1709                                  VERIFY(0 == zap_add_int(mos,
1704 1710                                      ds_prev->ds_phys->ds_next_clones_obj,
1705 1711                                      ds->ds_phys->ds_next_snap_obj, tx));
1706 1712                          }
1707 1713                  }
1708 1714                  if (after_branch_point &&
1709 1715                      ds->ds_phys->ds_next_snap_obj == 0) {
1710 1716                          /* This clone is toast. */
1711 1717                          ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1712 1718                          ds_prev->ds_phys->ds_num_children--;
1713 1719  
1714 1720                          /*
1715 1721                           * If the clone's origin has no other clones, no
1716 1722                           * user holds, and has been marked for deferred
1717 1723                           * deletion, then we should have done the necessary
1718 1724                           * destroy setup for it.
1719 1725                           */
1720 1726                          if (ds_prev->ds_phys->ds_num_children == 1 &&
1721 1727                              ds_prev->ds_userrefs == 0 &&
1722 1728                              DS_IS_DEFER_DESTROY(ds_prev)) {
1723 1729                                  ASSERT3P(dsda->rm_origin, !=, NULL);
1724 1730                          } else {
1725 1731                                  ASSERT3P(dsda->rm_origin, ==, NULL);
1726 1732                          }
1727 1733                  } else if (!after_branch_point) {
1728 1734                          ds_prev->ds_phys->ds_next_snap_obj =
1729 1735                              ds->ds_phys->ds_next_snap_obj;
1730 1736                  }
1731 1737          }
1732 1738  
1733 1739          if (dsl_dataset_is_snapshot(ds)) {
1734 1740                  dsl_dataset_t *ds_next;
1735 1741                  uint64_t old_unique;
1736 1742                  uint64_t used = 0, comp = 0, uncomp = 0;
1737 1743  
1738 1744                  VERIFY(0 == dsl_dataset_hold_obj(dp,
1739 1745                      ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1740 1746                  ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1741 1747  
1742 1748                  old_unique = ds_next->ds_phys->ds_unique_bytes;
1743 1749  
1744 1750                  dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1745 1751                  ds_next->ds_phys->ds_prev_snap_obj =
1746 1752                      ds->ds_phys->ds_prev_snap_obj;
1747 1753                  ds_next->ds_phys->ds_prev_snap_txg =
1748 1754                      ds->ds_phys->ds_prev_snap_txg;
1749 1755                  ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1750 1756                      ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1751 1757  
1752 1758  
1753 1759                  if (ds_next->ds_deadlist.dl_oldfmt) {
1754 1760                          process_old_deadlist(ds, ds_prev, ds_next,
1755 1761                              after_branch_point, tx);
1756 1762                  } else {
1757 1763                          /* Adjust prev's unique space. */
1758 1764                          if (ds_prev && !after_branch_point) {
1759 1765                                  dsl_deadlist_space_range(&ds_next->ds_deadlist,
1760 1766                                      ds_prev->ds_phys->ds_prev_snap_txg,
1761 1767                                      ds->ds_phys->ds_prev_snap_txg,
1762 1768                                      &used, &comp, &uncomp);
1763 1769                                  ds_prev->ds_phys->ds_unique_bytes += used;
1764 1770                          }
1765 1771  
1766 1772                          /* Adjust snapused. */
1767 1773                          dsl_deadlist_space_range(&ds_next->ds_deadlist,
1768 1774                              ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1769 1775                              &used, &comp, &uncomp);
1770 1776                          dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1771 1777                              -used, -comp, -uncomp, tx);
1772 1778  
1773 1779                          /* Move blocks to be freed to pool's free list. */
1774 1780                          dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
1775 1781                              &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
1776 1782                              tx);
1777 1783                          dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
1778 1784                              DD_USED_HEAD, used, comp, uncomp, tx);
1779 1785  
1780 1786                          /* Merge our deadlist into next's and free it. */
1781 1787                          dsl_deadlist_merge(&ds_next->ds_deadlist,
1782 1788                              ds->ds_phys->ds_deadlist_obj, tx);
1783 1789                  }
1784 1790                  dsl_deadlist_close(&ds->ds_deadlist);
1785 1791                  dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
1786 1792  
1787 1793                  /* Collapse range in clone heads */
1788 1794                  dsl_dataset_remove_clones_key(ds,
1789 1795                      ds->ds_phys->ds_creation_txg, tx);
1790 1796  
1791 1797                  if (dsl_dataset_is_snapshot(ds_next)) {
1792 1798                          dsl_dataset_t *ds_nextnext;
1793 1799  
1794 1800                          /*
1795 1801                           * Update next's unique to include blocks which
1796 1802                           * were previously shared by only this snapshot
1797 1803                           * and it.  Those blocks will be born after the
1798 1804                           * prev snap and before this snap, and will have
1799 1805                           * died after the next snap and before the one
1800 1806                           * after that (ie. be on the snap after next's
1801 1807                           * deadlist).
1802 1808                           */
1803 1809                          VERIFY(0 == dsl_dataset_hold_obj(dp,
1804 1810                              ds_next->ds_phys->ds_next_snap_obj,
1805 1811                              FTAG, &ds_nextnext));
1806 1812                          dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
1807 1813                              ds->ds_phys->ds_prev_snap_txg,
1808 1814                              ds->ds_phys->ds_creation_txg,
1809 1815                              &used, &comp, &uncomp);
1810 1816                          ds_next->ds_phys->ds_unique_bytes += used;
1811 1817                          dsl_dataset_rele(ds_nextnext, FTAG);
1812 1818                          ASSERT3P(ds_next->ds_prev, ==, NULL);
1813 1819  
1814 1820                          /* Collapse range in this head. */
1815 1821                          dsl_dataset_t *hds;
1816 1822                          VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
1817 1823                              ds->ds_dir->dd_phys->dd_head_dataset_obj,
1818 1824                              FTAG, &hds));
1819 1825                          dsl_deadlist_remove_key(&hds->ds_deadlist,
1820 1826                              ds->ds_phys->ds_creation_txg, tx);
1821 1827                          dsl_dataset_rele(hds, FTAG);
1822 1828  
1823 1829                  } else {
1824 1830                          ASSERT3P(ds_next->ds_prev, ==, ds);
1825 1831                          dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
1826 1832                          ds_next->ds_prev = NULL;
1827 1833                          if (ds_prev) {
1828 1834                                  VERIFY(0 == dsl_dataset_get_ref(dp,
1829 1835                                      ds->ds_phys->ds_prev_snap_obj,
1830 1836                                      ds_next, &ds_next->ds_prev));
1831 1837                          }
1832 1838  
1833 1839                          dsl_dataset_recalc_head_uniq(ds_next);
1834 1840  
1835 1841                          /*
1836 1842                           * Reduce the amount of our unconsmed refreservation
1837 1843                           * being charged to our parent by the amount of
1838 1844                           * new unique data we have gained.
1839 1845                           */
1840 1846                          if (old_unique < ds_next->ds_reserved) {
1841 1847                                  int64_t mrsdelta;
1842 1848                                  uint64_t new_unique =
1843 1849                                      ds_next->ds_phys->ds_unique_bytes;
1844 1850  
1845 1851                                  ASSERT(old_unique <= new_unique);
1846 1852                                  mrsdelta = MIN(new_unique - old_unique,
1847 1853                                      ds_next->ds_reserved - old_unique);
1848 1854                                  dsl_dir_diduse_space(ds->ds_dir,
1849 1855                                      DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
1850 1856                          }
1851 1857                  }
1852 1858                  dsl_dataset_rele(ds_next, FTAG);
1853 1859          } else {
1854 1860                  zfeature_info_t *async_destroy =
1855 1861                      &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY];
1856 1862                  objset_t *os;
1857 1863  
1858 1864                  /*
1859 1865                   * There's no next snapshot, so this is a head dataset.
1860 1866                   * Destroy the deadlist.  Unless it's a clone, the
1861 1867                   * deadlist should be empty.  (If it's a clone, it's
1862 1868                   * safe to ignore the deadlist contents.)
1863 1869                   */
1864 1870                  dsl_deadlist_close(&ds->ds_deadlist);
1865 1871                  dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
1866 1872                  ds->ds_phys->ds_deadlist_obj = 0;
1867 1873  
1868 1874                  VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
1869 1875  
1870 1876                  if (!spa_feature_is_enabled(dp->dp_spa, async_destroy)) {
1871 1877                          err = old_synchronous_dataset_destroy(ds, tx);
1872 1878                  } else {
1873 1879                          /*
1874 1880                           * Move the bptree into the pool's list of trees to
1875 1881                           * clean up and update space accounting information.
1876 1882                           */
1877 1883                          uint64_t used, comp, uncomp;
1878 1884  
1879 1885                          zil_destroy_sync(dmu_objset_zil(os), tx);
1880 1886  
1881 1887                          if (!spa_feature_is_active(dp->dp_spa, async_destroy)) {
1882 1888                                  spa_feature_incr(dp->dp_spa, async_destroy, tx);
1883 1889                                  dp->dp_bptree_obj = bptree_alloc(mos, tx);
1884 1890                                  VERIFY(zap_add(mos,
1885 1891                                      DMU_POOL_DIRECTORY_OBJECT,
1886 1892                                      DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
1887 1893                                      &dp->dp_bptree_obj, tx) == 0);
1888 1894                          }
1889 1895  
1890 1896                          used = ds->ds_dir->dd_phys->dd_used_bytes;
1891 1897                          comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
1892 1898                          uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
1893 1899  
1894 1900                          ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
1895 1901                              ds->ds_phys->ds_unique_bytes == used);
1896 1902  
1897 1903                          bptree_add(mos, dp->dp_bptree_obj,
1898 1904                              &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
1899 1905                              used, comp, uncomp, tx);
1900 1906                          dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
1901 1907                              -used, -comp, -uncomp, tx);
1902 1908                          dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
1903 1909                              used, comp, uncomp, tx);
1904 1910                  }
1905 1911  
1906 1912                  if (ds->ds_prev != NULL) {
1907 1913                          if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1908 1914                                  VERIFY3U(0, ==, zap_remove_int(mos,
1909 1915                                      ds->ds_prev->ds_dir->dd_phys->dd_clones,
1910 1916                                      ds->ds_object, tx));
1911 1917                          }
1912 1918                          dsl_dataset_rele(ds->ds_prev, ds);
1913 1919                          ds->ds_prev = ds_prev = NULL;
1914 1920                  }
1915 1921          }
1916 1922  
1917 1923          /*
1918 1924           * This must be done after the dsl_traverse(), because it will
1919 1925           * re-open the objset.
1920 1926           */
1921 1927          if (ds->ds_objset) {
1922 1928                  dmu_objset_evict(ds->ds_objset);
1923 1929                  ds->ds_objset = NULL;
1924 1930          }
1925 1931  
1926 1932          if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
1927 1933                  /* Erase the link in the dir */
1928 1934                  dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1929 1935                  ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
1930 1936                  ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1931 1937                  err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1932 1938                  ASSERT(err == 0);
1933 1939          } else {
1934 1940                  /* remove from snapshot namespace */
1935 1941                  dsl_dataset_t *ds_head;
1936 1942                  ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
1937 1943                  VERIFY(0 == dsl_dataset_hold_obj(dp,
1938 1944                      ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
1939 1945                  VERIFY(0 == dsl_dataset_get_snapname(ds));
  
    | ↓ open down ↓ | 1579 lines elided | ↑ open up ↑ | 
1940 1946  #ifdef ZFS_DEBUG
1941 1947                  {
1942 1948                          uint64_t val;
1943 1949  
1944 1950                          err = dsl_dataset_snap_lookup(ds_head,
1945 1951                              ds->ds_snapname, &val);
1946 1952                          ASSERT0(err);
1947 1953                          ASSERT3U(val, ==, obj);
1948 1954                  }
1949 1955  #endif
1950      -                err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
     1956 +                err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx,
     1957 +                    B_TRUE);
1951 1958                  ASSERT(err == 0);
1952 1959                  dsl_dataset_rele(ds_head, FTAG);
1953 1960          }
1954 1961  
1955 1962          if (ds_prev && ds->ds_prev != ds_prev)
1956 1963                  dsl_dataset_rele(ds_prev, FTAG);
1957 1964  
1958 1965          spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
1959 1966  
1960 1967          if (ds->ds_phys->ds_next_clones_obj != 0) {
1961 1968                  uint64_t count;
1962 1969                  ASSERT(0 == zap_count(mos,
1963 1970                      ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
1964 1971                  VERIFY(0 == dmu_object_free(mos,
1965 1972                      ds->ds_phys->ds_next_clones_obj, tx));
1966 1973          }
1967 1974          if (ds->ds_phys->ds_props_obj != 0)
1968 1975                  VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
1969 1976          if (ds->ds_phys->ds_userrefs_obj != 0)
1970 1977                  VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
1971 1978          dsl_dir_close(ds->ds_dir, ds);
1972 1979          ds->ds_dir = NULL;
1973 1980          dsl_dataset_drain_refs(ds, tag);
1974 1981          VERIFY(0 == dmu_object_free(mos, obj, tx));
1975 1982  
1976 1983          if (dsda->rm_origin) {
1977 1984                  /*
1978 1985                   * Remove the origin of the clone we just destroyed.
1979 1986                   */
1980 1987                  struct dsl_ds_destroyarg ndsda = {0};
1981 1988  
1982 1989                  ndsda.ds = dsda->rm_origin;
1983 1990                  dsl_dataset_destroy_sync(&ndsda, tag, tx);
1984 1991          }
1985 1992  }
1986 1993  
1987 1994  static int
1988 1995  dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1989 1996  {
1990 1997          uint64_t asize;
1991 1998  
1992 1999          if (!dmu_tx_is_syncing(tx))
1993 2000                  return (0);
1994 2001  
1995 2002          /*
1996 2003           * If there's an fs-only reservation, any blocks that might become
1997 2004           * owned by the snapshot dataset must be accommodated by space
1998 2005           * outside of the reservation.
1999 2006           */
2000 2007          ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
2001 2008          asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
2002 2009          if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
2003 2010                  return (ENOSPC);
2004 2011  
  
    | ↓ open down ↓ | 44 lines elided | ↑ open up ↑ | 
2005 2012          /*
2006 2013           * Propagate any reserved space for this snapshot to other
2007 2014           * snapshot checks in this sync group.
2008 2015           */
2009 2016          if (asize > 0)
2010 2017                  dsl_dir_willuse_space(ds->ds_dir, asize, tx);
2011 2018  
2012 2019          return (0);
2013 2020  }
2014 2021  
     2022 +/*
     2023 + * Check if adding additional snapshot(s) would exceed any snapshot limits.
     2024 + * Note that all snapshot limits up to the root dataset (i.e. the pool itself)
     2025 + * or the given ancestor must be satisfied. Note that it is valid for the
     2026 + * count to exceed the limit. This can happen if a snapshot is taken by an
     2027 + * administrative user in the global zone (e.g. a recursive snapshot by root).
     2028 + */
2015 2029  int
     2030 +dsl_snapcount_check(dsl_dir_t *dd, uint64_t cnt, dsl_dir_t *ancestor,
     2031 +    cred_t *cr)
     2032 +{
     2033 +        uint64_t limit;
     2034 +        int err = 0;
     2035 +
     2036 +        VERIFY(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
     2037 +
     2038 +        /* If we're allowed to change the limit, don't enforce the limit. */
     2039 +        if (dsl_secpolicy_write_prop(dd, ZFS_PROP_SNAPSHOT_LIMIT, cr) == 0)
     2040 +                return (0);
     2041 +
     2042 +        /*
     2043 +         * If renaming a dataset with no snapshots, count adjustment is 0.
     2044 +         */
     2045 +        if (cnt == 0)
     2046 +                return (0);
     2047 +
     2048 +        /*
     2049 +         * If an ancestor has been provided, stop checking the limit once we
     2050 +         * hit that dir. We need this during rename so that we don't overcount
     2051 +         * the check once we recurse up to the common ancestor.
     2052 +         */
     2053 +        if (ancestor == dd)
     2054 +                return (0);
     2055 +
     2056 +        /*
     2057 +         * If we hit an uninitialized node while recursing up the tree, we can
     2058 +         * stop since we know the counts are not valid on this node and we
     2059 +         * know we won't touch this node's counts. We also know that the counts
     2060 +         * on the nodes above this one are uninitialized and that there cannot
     2061 +         * be a limit set on any of those nodes.
     2062 +         */
     2063 +        if (dd->dd_phys->dd_filesystem_count == 0)
     2064 +                return (0);
     2065 +
     2066 +        err = dsl_prop_get_dd(dd, zfs_prop_to_name(ZFS_PROP_SNAPSHOT_LIMIT),
     2067 +            8, 1, &limit, NULL, B_FALSE);
     2068 +        if (err != 0)
     2069 +                return (err);
     2070 +
     2071 +        /* Is there a snapshot limit which we've hit? */
     2072 +        if ((dd->dd_phys->dd_snapshot_count + cnt) > limit)
     2073 +                return (EDQUOT);
     2074 +
     2075 +        if (dd->dd_parent != NULL)
     2076 +                err = dsl_snapcount_check(dd->dd_parent, cnt, ancestor, cr);
     2077 +
     2078 +        return (err);
     2079 +}
     2080 +
     2081 +/*
     2082 + * Adjust the snapshot count for the specified dsl_dir_t and all parents.
     2083 + * When a new snapshot is created, increment the count on all parents, and when
     2084 + * a snapshot is destroyed, decrement the count.
     2085 + */
     2086 +void
     2087 +dsl_snapcount_adjust(dsl_dir_t *dd, dmu_tx_t *tx, int64_t delta,
     2088 +    boolean_t first)
     2089 +{
     2090 +        if (first) {
     2091 +                VERIFY(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
     2092 +                VERIFY(dmu_tx_is_syncing(tx));
     2093 +        }
     2094 +
     2095 +        /*
     2096 +         * If we hit an uninitialized node while recursing up the tree, we can
     2097 +         * stop since we know the counts are not valid on this node and we
     2098 +         * know we shouldn't touch this node's counts. An uninitialized count
     2099 +         * on the node indicates that either the feature has not yet been
     2100 +         * activated or there are no limits on this part of the tree.
     2101 +         */
     2102 +        if (dd->dd_phys->dd_filesystem_count == 0)
     2103 +                return;
     2104 +
     2105 +        /* if renaming a dataset with no snapshots, count adjustment is 0 */
     2106 +        if (delta == 0)
     2107 +                return;
     2108 +
     2109 +        /*
     2110 +         * On initial entry we need to check if this feature is active, but
     2111 +         * we don't want to re-check this on each recursive call. Note: the
     2112 +         * feature cannot be active if it's not enabled. If the feature is not
     2113 +         * active, don't touch the on-disk count fields.
     2114 +         */
     2115 +        if (first) {
     2116 +                zfeature_info_t *quota_feat =
     2117 +                    &spa_feature_table[SPA_FEATURE_FS_SS_LIMIT];
     2118 +
     2119 +                if (!spa_feature_is_active(dd->dd_pool->dp_spa, quota_feat))
     2120 +                        return;
     2121 +        }
     2122 +
     2123 +        dmu_buf_will_dirty(dd->dd_dbuf, tx);
     2124 +
     2125 +        mutex_enter(&dd->dd_lock);
     2126 +
     2127 +        dd->dd_phys->dd_snapshot_count += delta;
     2128 +        VERIFY(dd->dd_phys->dd_snapshot_count >= 0);
     2129 +
     2130 +        /* Roll up this additional count into our ancestors */
     2131 +        if (dd->dd_parent != NULL)
     2132 +                dsl_snapcount_adjust(dd->dd_parent, tx, delta, B_FALSE);
     2133 +
     2134 +        mutex_exit(&dd->dd_lock);
     2135 +}
     2136 +
     2137 +int
2016 2138  dsl_dataset_snapshot_check(dsl_dataset_t *ds, const char *snapname,
2017      -    dmu_tx_t *tx)
     2139 +    uint64_t cnt, dmu_tx_t *tx, cred_t *cr)
2018 2140  {
2019 2141          int err;
2020 2142          uint64_t value;
2021 2143  
2022 2144          /*
2023 2145           * We don't allow multiple snapshots of the same txg.  If there
2024 2146           * is already one, try again.
2025 2147           */
2026 2148          if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
2027 2149                  return (EAGAIN);
2028 2150  
2029 2151          /*
2030 2152           * Check for conflicting snapshot name.
2031 2153           */
2032 2154          err = dsl_dataset_snap_lookup(ds, snapname, &value);
2033 2155          if (err == 0)
2034 2156                  return (EEXIST);
  
    | ↓ open down ↓ | 7 lines elided | ↑ open up ↑ | 
2035 2157          if (err != ENOENT)
2036 2158                  return (err);
2037 2159  
2038 2160          /*
2039 2161           * Check that the dataset's name is not too long.  Name consists
2040 2162           * of the dataset's length + 1 for the @-sign + snapshot name's length
2041 2163           */
2042 2164          if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
2043 2165                  return (ENAMETOOLONG);
2044 2166  
     2167 +        err = dsl_snapcount_check(ds->ds_dir, cnt, NULL, cr);
     2168 +        if (err)
     2169 +                return (err);
     2170 +
2045 2171          err = dsl_dataset_snapshot_reserve_space(ds, tx);
2046 2172          if (err)
2047 2173                  return (err);
2048 2174  
2049 2175          ds->ds_trysnap_txg = tx->tx_txg;
2050 2176          return (0);
2051 2177  }
2052 2178  
2053 2179  void
2054 2180  dsl_dataset_snapshot_sync(dsl_dataset_t *ds, const char *snapname,
2055 2181      dmu_tx_t *tx)
  
    | ↓ open down ↓ | 1 lines elided | ↑ open up ↑ | 
2056 2182  {
2057 2183          dsl_pool_t *dp = ds->ds_dir->dd_pool;
2058 2184          dmu_buf_t *dbuf;
2059 2185          dsl_dataset_phys_t *dsphys;
2060 2186          uint64_t dsobj, crtxg;
2061 2187          objset_t *mos = dp->dp_meta_objset;
2062 2188          int err;
2063 2189  
2064 2190          ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
2065 2191  
     2192 +        dsl_snapcount_adjust(ds->ds_dir, tx, 1, B_TRUE);
     2193 +
2066 2194          /*
2067 2195           * The origin's ds_creation_txg has to be < TXG_INITIAL
2068 2196           */
2069 2197          if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
2070 2198                  crtxg = 1;
2071 2199          else
2072 2200                  crtxg = tx->tx_txg;
2073 2201  
2074 2202          dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
2075 2203              DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
2076 2204          VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
2077 2205          dmu_buf_will_dirty(dbuf, tx);
2078 2206          dsphys = dbuf->db_data;
2079 2207          bzero(dsphys, sizeof (dsl_dataset_phys_t));
2080 2208          dsphys->ds_dir_obj = ds->ds_dir->dd_object;
2081 2209          dsphys->ds_fsid_guid = unique_create();
2082 2210          (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
2083 2211              sizeof (dsphys->ds_guid));
2084 2212          dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
2085 2213          dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
2086 2214          dsphys->ds_next_snap_obj = ds->ds_object;
2087 2215          dsphys->ds_num_children = 1;
2088 2216          dsphys->ds_creation_time = gethrestime_sec();
2089 2217          dsphys->ds_creation_txg = crtxg;
2090 2218          dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
2091 2219          dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
2092 2220          dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
2093 2221          dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
2094 2222          dsphys->ds_flags = ds->ds_phys->ds_flags;
2095 2223          dsphys->ds_bp = ds->ds_phys->ds_bp;
2096 2224          dmu_buf_rele(dbuf, FTAG);
2097 2225  
2098 2226          ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
2099 2227          if (ds->ds_prev) {
2100 2228                  uint64_t next_clones_obj =
2101 2229                      ds->ds_prev->ds_phys->ds_next_clones_obj;
2102 2230                  ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
2103 2231                      ds->ds_object ||
2104 2232                      ds->ds_prev->ds_phys->ds_num_children > 1);
2105 2233                  if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
2106 2234                          dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2107 2235                          ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
2108 2236                              ds->ds_prev->ds_phys->ds_creation_txg);
2109 2237                          ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
2110 2238                  } else if (next_clones_obj != 0) {
2111 2239                          remove_from_next_clones(ds->ds_prev,
2112 2240                              dsphys->ds_next_snap_obj, tx);
2113 2241                          VERIFY3U(0, ==, zap_add_int(mos,
2114 2242                              next_clones_obj, dsobj, tx));
2115 2243                  }
2116 2244          }
2117 2245  
2118 2246          /*
2119 2247           * If we have a reference-reservation on this dataset, we will
2120 2248           * need to increase the amount of refreservation being charged
2121 2249           * since our unique space is going to zero.
2122 2250           */
2123 2251          if (ds->ds_reserved) {
2124 2252                  int64_t delta;
2125 2253                  ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2126 2254                  delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
2127 2255                  dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
2128 2256                      delta, 0, 0, tx);
2129 2257          }
2130 2258  
2131 2259          dmu_buf_will_dirty(ds->ds_dbuf, tx);
2132 2260          zfs_dbgmsg("taking snapshot %s@%s/%llu; newkey=%llu",
2133 2261              ds->ds_dir->dd_myname, snapname, dsobj,
2134 2262              ds->ds_phys->ds_prev_snap_txg);
2135 2263          ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
2136 2264              UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
2137 2265          dsl_deadlist_close(&ds->ds_deadlist);
2138 2266          dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
2139 2267          dsl_deadlist_add_key(&ds->ds_deadlist,
2140 2268              ds->ds_phys->ds_prev_snap_txg, tx);
2141 2269  
2142 2270          ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
2143 2271          ds->ds_phys->ds_prev_snap_obj = dsobj;
2144 2272          ds->ds_phys->ds_prev_snap_txg = crtxg;
2145 2273          ds->ds_phys->ds_unique_bytes = 0;
2146 2274          if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
2147 2275                  ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
2148 2276  
2149 2277          err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
2150 2278              snapname, 8, 1, &dsobj, tx);
2151 2279          ASSERT(err == 0);
2152 2280  
2153 2281          if (ds->ds_prev)
2154 2282                  dsl_dataset_drop_ref(ds->ds_prev, ds);
2155 2283          VERIFY(0 == dsl_dataset_get_ref(dp,
2156 2284              ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
2157 2285  
2158 2286          dsl_scan_ds_snapshotted(ds, tx);
2159 2287  
2160 2288          dsl_dir_snap_cmtime_update(ds->ds_dir);
2161 2289  
2162 2290          spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
2163 2291  }
2164 2292  
2165 2293  void
2166 2294  dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
2167 2295  {
2168 2296          ASSERT(dmu_tx_is_syncing(tx));
2169 2297          ASSERT(ds->ds_objset != NULL);
2170 2298          ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
2171 2299  
2172 2300          /*
2173 2301           * in case we had to change ds_fsid_guid when we opened it,
2174 2302           * sync it out now.
2175 2303           */
2176 2304          dmu_buf_will_dirty(ds->ds_dbuf, tx);
2177 2305          ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
2178 2306  
2179 2307          dmu_objset_sync(ds->ds_objset, zio, tx);
2180 2308  }
2181 2309  
2182 2310  static void
2183 2311  get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
2184 2312  {
2185 2313          uint64_t count = 0;
2186 2314          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2187 2315          zap_cursor_t zc;
2188 2316          zap_attribute_t za;
2189 2317          nvlist_t *propval;
2190 2318          nvlist_t *val;
2191 2319  
2192 2320          rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
2193 2321          VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2194 2322          VERIFY(nvlist_alloc(&val, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2195 2323  
2196 2324          /*
2197 2325           * There may me missing entries in ds_next_clones_obj
2198 2326           * due to a bug in a previous version of the code.
2199 2327           * Only trust it if it has the right number of entries.
2200 2328           */
2201 2329          if (ds->ds_phys->ds_next_clones_obj != 0) {
2202 2330                  ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
2203 2331                      &count));
2204 2332          }
2205 2333          if (count != ds->ds_phys->ds_num_children - 1) {
2206 2334                  goto fail;
2207 2335          }
2208 2336          for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
2209 2337              zap_cursor_retrieve(&zc, &za) == 0;
2210 2338              zap_cursor_advance(&zc)) {
2211 2339                  dsl_dataset_t *clone;
2212 2340                  char buf[ZFS_MAXNAMELEN];
2213 2341                  /*
2214 2342                   * Even though we hold the dp_config_rwlock, the dataset
2215 2343                   * may fail to open, returning ENOENT.  If there is a
2216 2344                   * thread concurrently attempting to destroy this
2217 2345                   * dataset, it will have the ds_rwlock held for
2218 2346                   * RW_WRITER.  Our call to dsl_dataset_hold_obj() ->
2219 2347                   * dsl_dataset_hold_ref() will fail its
2220 2348                   * rw_tryenter(&ds->ds_rwlock, RW_READER), drop the
2221 2349                   * dp_config_rwlock, and wait for the destroy progress
2222 2350                   * and signal ds_exclusive_cv.  If the destroy was
2223 2351                   * successful, we will see that
2224 2352                   * DSL_DATASET_IS_DESTROYED(), and return ENOENT.
2225 2353                   */
2226 2354                  if (dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
2227 2355                      za.za_first_integer, FTAG, &clone) != 0)
2228 2356                          continue;
2229 2357                  dsl_dir_name(clone->ds_dir, buf);
2230 2358                  VERIFY(nvlist_add_boolean(val, buf) == 0);
2231 2359                  dsl_dataset_rele(clone, FTAG);
2232 2360          }
2233 2361          zap_cursor_fini(&zc);
2234 2362          VERIFY(nvlist_add_nvlist(propval, ZPROP_VALUE, val) == 0);
2235 2363          VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
2236 2364              propval) == 0);
2237 2365  fail:
2238 2366          nvlist_free(val);
2239 2367          nvlist_free(propval);
2240 2368          rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
2241 2369  }
2242 2370  
2243 2371  void
2244 2372  dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2245 2373  {
2246 2374          uint64_t refd, avail, uobjs, aobjs, ratio;
2247 2375  
2248 2376          ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
2249 2377              (ds->ds_phys->ds_uncompressed_bytes * 100 /
2250 2378              ds->ds_phys->ds_compressed_bytes);
2251 2379  
2252 2380          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
2253 2381  
2254 2382          if (dsl_dataset_is_snapshot(ds)) {
2255 2383                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
2256 2384                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2257 2385                      ds->ds_phys->ds_unique_bytes);
2258 2386                  get_clones_stat(ds, nv);
2259 2387          } else {
2260 2388                  dsl_dir_stats(ds->ds_dir, nv);
2261 2389          }
2262 2390  
2263 2391          dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
2264 2392          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
2265 2393          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
2266 2394  
2267 2395          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2268 2396              ds->ds_phys->ds_creation_time);
2269 2397          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2270 2398              ds->ds_phys->ds_creation_txg);
2271 2399          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2272 2400              ds->ds_quota);
2273 2401          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2274 2402              ds->ds_reserved);
2275 2403          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2276 2404              ds->ds_phys->ds_guid);
2277 2405          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2278 2406              ds->ds_phys->ds_unique_bytes);
2279 2407          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2280 2408              ds->ds_object);
2281 2409          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
2282 2410              ds->ds_userrefs);
2283 2411          dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2284 2412              DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2285 2413  
2286 2414          if (ds->ds_phys->ds_prev_snap_obj != 0) {
2287 2415                  uint64_t written, comp, uncomp;
2288 2416                  dsl_pool_t *dp = ds->ds_dir->dd_pool;
2289 2417                  dsl_dataset_t *prev;
2290 2418  
2291 2419                  rw_enter(&dp->dp_config_rwlock, RW_READER);
2292 2420                  int err = dsl_dataset_hold_obj(dp,
2293 2421                      ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
2294 2422                  rw_exit(&dp->dp_config_rwlock);
2295 2423                  if (err == 0) {
2296 2424                          err = dsl_dataset_space_written(prev, ds, &written,
2297 2425                              &comp, &uncomp);
2298 2426                          dsl_dataset_rele(prev, FTAG);
2299 2427                          if (err == 0) {
2300 2428                                  dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2301 2429                                      written);
2302 2430                          }
2303 2431                  }
2304 2432          }
2305 2433  }
2306 2434  
2307 2435  void
2308 2436  dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2309 2437  {
2310 2438          stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
2311 2439          stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
2312 2440          stat->dds_guid = ds->ds_phys->ds_guid;
2313 2441          stat->dds_origin[0] = '\0';
2314 2442          if (dsl_dataset_is_snapshot(ds)) {
2315 2443                  stat->dds_is_snapshot = B_TRUE;
2316 2444                  stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
2317 2445          } else {
2318 2446                  stat->dds_is_snapshot = B_FALSE;
2319 2447                  stat->dds_num_clones = 0;
2320 2448  
2321 2449                  rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
2322 2450                  if (dsl_dir_is_clone(ds->ds_dir)) {
2323 2451                          dsl_dataset_t *ods;
2324 2452  
2325 2453                          VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
2326 2454                              ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
2327 2455                          dsl_dataset_name(ods, stat->dds_origin);
2328 2456                          dsl_dataset_drop_ref(ods, FTAG);
2329 2457                  }
2330 2458                  rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
2331 2459          }
2332 2460  }
2333 2461  
2334 2462  uint64_t
2335 2463  dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2336 2464  {
2337 2465          return (ds->ds_fsid_guid);
2338 2466  }
2339 2467  
2340 2468  void
2341 2469  dsl_dataset_space(dsl_dataset_t *ds,
2342 2470      uint64_t *refdbytesp, uint64_t *availbytesp,
2343 2471      uint64_t *usedobjsp, uint64_t *availobjsp)
2344 2472  {
2345 2473          *refdbytesp = ds->ds_phys->ds_referenced_bytes;
2346 2474          *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2347 2475          if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
2348 2476                  *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
2349 2477          if (ds->ds_quota != 0) {
2350 2478                  /*
2351 2479                   * Adjust available bytes according to refquota
2352 2480                   */
2353 2481                  if (*refdbytesp < ds->ds_quota)
2354 2482                          *availbytesp = MIN(*availbytesp,
2355 2483                              ds->ds_quota - *refdbytesp);
2356 2484                  else
2357 2485                          *availbytesp = 0;
2358 2486          }
2359 2487          *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
2360 2488          *availobjsp = DN_MAX_OBJECT - *usedobjsp;
2361 2489  }
2362 2490  
2363 2491  boolean_t
2364 2492  dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
2365 2493  {
2366 2494          dsl_pool_t *dp = ds->ds_dir->dd_pool;
2367 2495  
2368 2496          ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
2369 2497              dsl_pool_sync_context(dp));
2370 2498          if (ds->ds_prev == NULL)
2371 2499                  return (B_FALSE);
2372 2500          if (ds->ds_phys->ds_bp.blk_birth >
2373 2501              ds->ds_prev->ds_phys->ds_creation_txg) {
2374 2502                  objset_t *os, *os_prev;
2375 2503                  /*
2376 2504                   * It may be that only the ZIL differs, because it was
2377 2505                   * reset in the head.  Don't count that as being
2378 2506                   * modified.
2379 2507                   */
2380 2508                  if (dmu_objset_from_ds(ds, &os) != 0)
2381 2509                          return (B_TRUE);
2382 2510                  if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
2383 2511                          return (B_TRUE);
2384 2512                  return (bcmp(&os->os_phys->os_meta_dnode,
2385 2513                      &os_prev->os_phys->os_meta_dnode,
2386 2514                      sizeof (os->os_phys->os_meta_dnode)) != 0);
2387 2515          }
2388 2516          return (B_FALSE);
2389 2517  }
2390 2518  
2391 2519  /* ARGSUSED */
2392 2520  static int
2393 2521  dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2394 2522  {
2395 2523          dsl_dataset_t *ds = arg1;
2396 2524          char *newsnapname = arg2;
2397 2525          dsl_dir_t *dd = ds->ds_dir;
2398 2526          dsl_dataset_t *hds;
2399 2527          uint64_t val;
2400 2528          int err;
2401 2529  
2402 2530          err = dsl_dataset_hold_obj(dd->dd_pool,
2403 2531              dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
2404 2532          if (err)
2405 2533                  return (err);
2406 2534  
2407 2535          /* new name better not be in use */
2408 2536          err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
2409 2537          dsl_dataset_rele(hds, FTAG);
2410 2538  
2411 2539          if (err == 0)
2412 2540                  err = EEXIST;
2413 2541          else if (err == ENOENT)
2414 2542                  err = 0;
2415 2543  
2416 2544          /* dataset name + 1 for the "@" + the new snapshot name must fit */
2417 2545          if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
2418 2546                  err = ENAMETOOLONG;
2419 2547  
2420 2548          return (err);
2421 2549  }
2422 2550  
2423 2551  static void
2424 2552  dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
2425 2553  {
2426 2554          dsl_dataset_t *ds = arg1;
2427 2555          const char *newsnapname = arg2;
2428 2556          dsl_dir_t *dd = ds->ds_dir;
  
    | ↓ open down ↓ | 353 lines elided | ↑ open up ↑ | 
2429 2557          objset_t *mos = dd->dd_pool->dp_meta_objset;
2430 2558          dsl_dataset_t *hds;
2431 2559          int err;
2432 2560  
2433 2561          ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2434 2562  
2435 2563          VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
2436 2564              dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2437 2565  
2438 2566          VERIFY(0 == dsl_dataset_get_snapname(ds));
2439      -        err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
     2567 +        err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx, B_FALSE);
2440 2568          ASSERT0(err);
2441 2569          mutex_enter(&ds->ds_lock);
2442 2570          (void) strcpy(ds->ds_snapname, newsnapname);
2443 2571          mutex_exit(&ds->ds_lock);
2444 2572          err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
2445 2573              ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2446 2574          ASSERT0(err);
2447 2575  
2448 2576          spa_history_log_internal_ds(ds, "rename", tx,
2449 2577              "-> @%s", newsnapname);
2450 2578          dsl_dataset_rele(hds, FTAG);
2451 2579  }
2452 2580  
2453 2581  struct renamesnaparg {
2454 2582          dsl_sync_task_group_t *dstg;
2455 2583          char failed[MAXPATHLEN];
2456 2584          char *oldsnap;
2457 2585          char *newsnap;
2458 2586  };
2459 2587  
2460 2588  static int
2461 2589  dsl_snapshot_rename_one(const char *name, void *arg)
2462 2590  {
2463 2591          struct renamesnaparg *ra = arg;
2464 2592          dsl_dataset_t *ds = NULL;
2465 2593          char *snapname;
2466 2594          int err;
2467 2595  
2468 2596          snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
2469 2597          (void) strlcpy(ra->failed, snapname, sizeof (ra->failed));
2470 2598  
2471 2599          /*
2472 2600           * For recursive snapshot renames the parent won't be changing
2473 2601           * so we just pass name for both the to/from argument.
2474 2602           */
2475 2603          err = zfs_secpolicy_rename_perms(snapname, snapname, CRED());
2476 2604          if (err != 0) {
2477 2605                  strfree(snapname);
2478 2606                  return (err == ENOENT ? 0 : err);
2479 2607          }
2480 2608  
2481 2609  #ifdef _KERNEL
2482 2610          /*
2483 2611           * For all filesystems undergoing rename, we'll need to unmount it.
2484 2612           */
2485 2613          (void) zfs_unmount_snap(snapname, NULL);
2486 2614  #endif
2487 2615          err = dsl_dataset_hold(snapname, ra->dstg, &ds);
2488 2616          strfree(snapname);
2489 2617          if (err != 0)
2490 2618                  return (err == ENOENT ? 0 : err);
2491 2619  
2492 2620          dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
2493 2621              dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
2494 2622  
2495 2623          return (0);
2496 2624  }
2497 2625  
2498 2626  static int
2499 2627  dsl_recursive_rename(char *oldname, const char *newname)
2500 2628  {
2501 2629          int err;
2502 2630          struct renamesnaparg *ra;
2503 2631          dsl_sync_task_t *dst;
2504 2632          spa_t *spa;
2505 2633          char *cp, *fsname = spa_strdup(oldname);
2506 2634          int len = strlen(oldname) + 1;
2507 2635  
2508 2636          /* truncate the snapshot name to get the fsname */
2509 2637          cp = strchr(fsname, '@');
2510 2638          *cp = '\0';
2511 2639  
2512 2640          err = spa_open(fsname, &spa, FTAG);
2513 2641          if (err) {
2514 2642                  kmem_free(fsname, len);
2515 2643                  return (err);
2516 2644          }
2517 2645          ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
2518 2646          ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
2519 2647  
2520 2648          ra->oldsnap = strchr(oldname, '@') + 1;
2521 2649          ra->newsnap = strchr(newname, '@') + 1;
2522 2650          *ra->failed = '\0';
2523 2651  
2524 2652          err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
2525 2653              DS_FIND_CHILDREN);
2526 2654          kmem_free(fsname, len);
2527 2655  
2528 2656          if (err == 0) {
2529 2657                  err = dsl_sync_task_group_wait(ra->dstg);
2530 2658          }
2531 2659  
2532 2660          for (dst = list_head(&ra->dstg->dstg_tasks); dst;
2533 2661              dst = list_next(&ra->dstg->dstg_tasks, dst)) {
2534 2662                  dsl_dataset_t *ds = dst->dst_arg1;
2535 2663                  if (dst->dst_err) {
2536 2664                          dsl_dir_name(ds->ds_dir, ra->failed);
2537 2665                          (void) strlcat(ra->failed, "@", sizeof (ra->failed));
2538 2666                          (void) strlcat(ra->failed, ra->newsnap,
2539 2667                              sizeof (ra->failed));
2540 2668                  }
2541 2669                  dsl_dataset_rele(ds, ra->dstg);
2542 2670          }
2543 2671  
2544 2672          if (err)
2545 2673                  (void) strlcpy(oldname, ra->failed, sizeof (ra->failed));
2546 2674  
2547 2675          dsl_sync_task_group_destroy(ra->dstg);
2548 2676          kmem_free(ra, sizeof (struct renamesnaparg));
2549 2677          spa_close(spa, FTAG);
2550 2678          return (err);
2551 2679  }
2552 2680  
2553 2681  static int
2554 2682  dsl_valid_rename(const char *oldname, void *arg)
2555 2683  {
2556 2684          int delta = *(int *)arg;
2557 2685  
2558 2686          if (strlen(oldname) + delta >= MAXNAMELEN)
2559 2687                  return (ENAMETOOLONG);
2560 2688  
2561 2689          return (0);
2562 2690  }
2563 2691  
2564 2692  #pragma weak dmu_objset_rename = dsl_dataset_rename
2565 2693  int
2566 2694  dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2567 2695  {
2568 2696          dsl_dir_t *dd;
2569 2697          dsl_dataset_t *ds;
2570 2698          const char *tail;
2571 2699          int err;
2572 2700  
2573 2701          err = dsl_dir_open(oldname, FTAG, &dd, &tail);
2574 2702          if (err)
2575 2703                  return (err);
2576 2704  
2577 2705          if (tail == NULL) {
2578 2706                  int delta = strlen(newname) - strlen(oldname);
2579 2707  
2580 2708                  /* if we're growing, validate child name lengths */
2581 2709                  if (delta > 0)
2582 2710                          err = dmu_objset_find(oldname, dsl_valid_rename,
2583 2711                              &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2584 2712  
2585 2713                  if (err == 0)
2586 2714                          err = dsl_dir_rename(dd, newname);
2587 2715                  dsl_dir_close(dd, FTAG);
2588 2716                  return (err);
2589 2717          }
2590 2718  
2591 2719          if (tail[0] != '@') {
2592 2720                  /* the name ended in a nonexistent component */
2593 2721                  dsl_dir_close(dd, FTAG);
2594 2722                  return (ENOENT);
2595 2723          }
2596 2724  
2597 2725          dsl_dir_close(dd, FTAG);
2598 2726  
2599 2727          /* new name must be snapshot in same filesystem */
2600 2728          tail = strchr(newname, '@');
2601 2729          if (tail == NULL)
2602 2730                  return (EINVAL);
2603 2731          tail++;
2604 2732          if (strncmp(oldname, newname, tail - newname) != 0)
2605 2733                  return (EXDEV);
2606 2734  
2607 2735          if (recursive) {
2608 2736                  err = dsl_recursive_rename(oldname, newname);
2609 2737          } else {
2610 2738                  err = dsl_dataset_hold(oldname, FTAG, &ds);
2611 2739                  if (err)
2612 2740                          return (err);
2613 2741  
2614 2742                  err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2615 2743                      dsl_dataset_snapshot_rename_check,
2616 2744                      dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
2617 2745  
2618 2746                  dsl_dataset_rele(ds, FTAG);
2619 2747          }
2620 2748  
2621 2749          return (err);
2622 2750  }
2623 2751  
  
    | ↓ open down ↓ | 174 lines elided | ↑ open up ↑ | 
2624 2752  struct promotenode {
2625 2753          list_node_t link;
2626 2754          dsl_dataset_t *ds;
2627 2755  };
2628 2756  
2629 2757  struct promotearg {
2630 2758          list_t shared_snaps, origin_snaps, clone_snaps;
2631 2759          dsl_dataset_t *origin_origin;
2632 2760          uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2633 2761          char *err_ds;
     2762 +        cred_t *cr;
2634 2763  };
2635 2764  
2636 2765  static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
2637 2766  static boolean_t snaplist_unstable(list_t *l);
2638 2767  
2639 2768  static int
2640 2769  dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
2641 2770  {
2642 2771          dsl_dataset_t *hds = arg1;
2643 2772          struct promotearg *pa = arg2;
2644 2773          struct promotenode *snap = list_head(&pa->shared_snaps);
2645 2774          dsl_dataset_t *origin_ds = snap->ds;
2646 2775          int err;
2647 2776          uint64_t unused;
2648 2777  
2649 2778          /* Check that it is a real clone */
2650 2779          if (!dsl_dir_is_clone(hds->ds_dir))
2651 2780                  return (EINVAL);
2652 2781  
2653 2782          /* Since this is so expensive, don't do the preliminary check */
2654 2783          if (!dmu_tx_is_syncing(tx))
2655 2784                  return (0);
2656 2785  
2657 2786          if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
2658 2787                  return (EXDEV);
2659 2788  
2660 2789          /* compute origin's new unique space */
2661 2790          snap = list_tail(&pa->clone_snaps);
2662 2791          ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2663 2792          dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2664 2793              origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2665 2794              &pa->unique, &unused, &unused);
2666 2795  
2667 2796          /*
2668 2797           * Walk the snapshots that we are moving
2669 2798           *
2670 2799           * Compute space to transfer.  Consider the incremental changes
2671 2800           * to used for each snapshot:
2672 2801           * (my used) = (prev's used) + (blocks born) - (blocks killed)
2673 2802           * So each snapshot gave birth to:
2674 2803           * (blocks born) = (my used) - (prev's used) + (blocks killed)
2675 2804           * So a sequence would look like:
2676 2805           * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2677 2806           * Which simplifies to:
2678 2807           * uN + kN + kN-1 + ... + k1 + k0
2679 2808           * Note however, if we stop before we reach the ORIGIN we get:
2680 2809           * uN + kN + kN-1 + ... + kM - uM-1
2681 2810           */
2682 2811          pa->used = origin_ds->ds_phys->ds_referenced_bytes;
2683 2812          pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
2684 2813          pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
2685 2814          for (snap = list_head(&pa->shared_snaps); snap;
2686 2815              snap = list_next(&pa->shared_snaps, snap)) {
2687 2816                  uint64_t val, dlused, dlcomp, dluncomp;
2688 2817                  dsl_dataset_t *ds = snap->ds;
2689 2818  
2690 2819                  /* Check that the snapshot name does not conflict */
2691 2820                  VERIFY(0 == dsl_dataset_get_snapname(ds));
2692 2821                  err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2693 2822                  if (err == 0) {
2694 2823                          err = EEXIST;
2695 2824                          goto out;
2696 2825                  }
2697 2826                  if (err != ENOENT)
2698 2827                          goto out;
2699 2828  
2700 2829                  /* The very first snapshot does not have a deadlist */
2701 2830                  if (ds->ds_phys->ds_prev_snap_obj == 0)
2702 2831                          continue;
2703 2832  
2704 2833                  dsl_deadlist_space(&ds->ds_deadlist,
2705 2834                      &dlused, &dlcomp, &dluncomp);
2706 2835                  pa->used += dlused;
2707 2836                  pa->comp += dlcomp;
2708 2837                  pa->uncomp += dluncomp;
2709 2838          }
2710 2839  
  
    | ↓ open down ↓ | 67 lines elided | ↑ open up ↑ | 
2711 2840          /*
2712 2841           * If we are a clone of a clone then we never reached ORIGIN,
2713 2842           * so we need to subtract out the clone origin's used space.
2714 2843           */
2715 2844          if (pa->origin_origin) {
2716 2845                  pa->used -= pa->origin_origin->ds_phys->ds_referenced_bytes;
2717 2846                  pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
2718 2847                  pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
2719 2848          }
2720 2849  
2721      -        /* Check that there is enough space here */
     2850 +        /* Check that there is enough space and limit headroom here */
2722 2851          err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2723      -            pa->used);
     2852 +            origin_ds->ds_dir, pa->used, pa->cr);
2724 2853          if (err)
2725 2854                  return (err);
2726 2855  
2727 2856          /*
2728 2857           * Compute the amounts of space that will be used by snapshots
2729 2858           * after the promotion (for both origin and clone).  For each,
2730 2859           * it is the amount of space that will be on all of their
2731 2860           * deadlists (that was not born before their new origin).
2732 2861           */
2733 2862          if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2734 2863                  uint64_t space;
2735 2864  
2736 2865                  /*
2737 2866                   * Note, typically this will not be a clone of a clone,
2738 2867                   * so dd_origin_txg will be < TXG_INITIAL, so
2739 2868                   * these snaplist_space() -> dsl_deadlist_space_range()
2740 2869                   * calls will be fast because they do not have to
2741 2870                   * iterate over all bps.
2742 2871                   */
2743 2872                  snap = list_head(&pa->origin_snaps);
2744 2873                  err = snaplist_space(&pa->shared_snaps,
2745 2874                      snap->ds->ds_dir->dd_origin_txg, &pa->cloneusedsnap);
2746 2875                  if (err)
2747 2876                          return (err);
2748 2877  
2749 2878                  err = snaplist_space(&pa->clone_snaps,
2750 2879                      snap->ds->ds_dir->dd_origin_txg, &space);
2751 2880                  if (err)
2752 2881                          return (err);
2753 2882                  pa->cloneusedsnap += space;
2754 2883          }
2755 2884          if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2756 2885                  err = snaplist_space(&pa->origin_snaps,
2757 2886                      origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
2758 2887                  if (err)
2759 2888                          return (err);
2760 2889          }
2761 2890  
2762 2891          return (0);
2763 2892  out:
2764 2893          pa->err_ds =  snap->ds->ds_snapname;
2765 2894          return (err);
2766 2895  }
2767 2896  
2768 2897  static void
2769 2898  dsl_dataset_promote_sync(void *arg1, void *arg2, dmu_tx_t *tx)
2770 2899  {
2771 2900          dsl_dataset_t *hds = arg1;
2772 2901          struct promotearg *pa = arg2;
2773 2902          struct promotenode *snap = list_head(&pa->shared_snaps);
2774 2903          dsl_dataset_t *origin_ds = snap->ds;
2775 2904          dsl_dataset_t *origin_head;
2776 2905          dsl_dir_t *dd = hds->ds_dir;
2777 2906          dsl_pool_t *dp = hds->ds_dir->dd_pool;
2778 2907          dsl_dir_t *odd = NULL;
2779 2908          uint64_t oldnext_obj;
2780 2909          int64_t delta;
2781 2910  
2782 2911          ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
2783 2912  
2784 2913          snap = list_head(&pa->origin_snaps);
2785 2914          origin_head = snap->ds;
2786 2915  
2787 2916          /*
2788 2917           * We need to explicitly open odd, since origin_ds's dd will be
2789 2918           * changing.
2790 2919           */
2791 2920          VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
2792 2921              NULL, FTAG, &odd));
2793 2922  
2794 2923          /* change origin's next snap */
2795 2924          dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2796 2925          oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
2797 2926          snap = list_tail(&pa->clone_snaps);
2798 2927          ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2799 2928          origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2800 2929  
2801 2930          /* change the origin's next clone */
2802 2931          if (origin_ds->ds_phys->ds_next_clones_obj) {
2803 2932                  remove_from_next_clones(origin_ds, snap->ds->ds_object, tx);
2804 2933                  VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2805 2934                      origin_ds->ds_phys->ds_next_clones_obj,
2806 2935                      oldnext_obj, tx));
2807 2936          }
2808 2937  
2809 2938          /* change origin */
2810 2939          dmu_buf_will_dirty(dd->dd_dbuf, tx);
2811 2940          ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2812 2941          dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2813 2942          dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2814 2943          dmu_buf_will_dirty(odd->dd_dbuf, tx);
2815 2944          odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2816 2945          origin_head->ds_dir->dd_origin_txg =
2817 2946              origin_ds->ds_phys->ds_creation_txg;
2818 2947  
2819 2948          /* change dd_clone entries */
2820 2949          if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2821 2950                  VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
2822 2951                      odd->dd_phys->dd_clones, hds->ds_object, tx));
2823 2952                  VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2824 2953                      pa->origin_origin->ds_dir->dd_phys->dd_clones,
2825 2954                      hds->ds_object, tx));
2826 2955  
2827 2956                  VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
2828 2957                      pa->origin_origin->ds_dir->dd_phys->dd_clones,
2829 2958                      origin_head->ds_object, tx));
2830 2959                  if (dd->dd_phys->dd_clones == 0) {
2831 2960                          dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2832 2961                              DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2833 2962                  }
2834 2963                  VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2835 2964                      dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2836 2965  
2837 2966          }
2838 2967  
2839 2968          /* move snapshots to this dir */
2840 2969          for (snap = list_head(&pa->shared_snaps); snap;
2841 2970              snap = list_next(&pa->shared_snaps, snap)) {
  
    | ↓ open down ↓ | 108 lines elided | ↑ open up ↑ | 
2842 2971                  dsl_dataset_t *ds = snap->ds;
2843 2972  
2844 2973                  /* unregister props as dsl_dir is changing */
2845 2974                  if (ds->ds_objset) {
2846 2975                          dmu_objset_evict(ds->ds_objset);
2847 2976                          ds->ds_objset = NULL;
2848 2977                  }
2849 2978                  /* move snap name entry */
2850 2979                  VERIFY(0 == dsl_dataset_get_snapname(ds));
2851 2980                  VERIFY(0 == dsl_dataset_snap_remove(origin_head,
2852      -                    ds->ds_snapname, tx));
     2981 +                    ds->ds_snapname, tx, B_TRUE));
2853 2982                  VERIFY(0 == zap_add(dp->dp_meta_objset,
2854 2983                      hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2855 2984                      8, 1, &ds->ds_object, tx));
     2985 +                dsl_snapcount_adjust(hds->ds_dir, tx, 1, B_TRUE);
2856 2986  
2857 2987                  /* change containing dsl_dir */
2858 2988                  dmu_buf_will_dirty(ds->ds_dbuf, tx);
2859 2989                  ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2860 2990                  ds->ds_phys->ds_dir_obj = dd->dd_object;
2861 2991                  ASSERT3P(ds->ds_dir, ==, odd);
2862 2992                  dsl_dir_close(ds->ds_dir, ds);
2863 2993                  VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
2864 2994                      NULL, ds, &ds->ds_dir));
2865 2995  
2866 2996                  /* move any clone references */
2867 2997                  if (ds->ds_phys->ds_next_clones_obj &&
2868 2998                      spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2869 2999                          zap_cursor_t zc;
2870 3000                          zap_attribute_t za;
2871 3001  
2872 3002                          for (zap_cursor_init(&zc, dp->dp_meta_objset,
2873 3003                              ds->ds_phys->ds_next_clones_obj);
2874 3004                              zap_cursor_retrieve(&zc, &za) == 0;
2875 3005                              zap_cursor_advance(&zc)) {
2876 3006                                  dsl_dataset_t *cnds;
2877 3007                                  uint64_t o;
2878 3008  
2879 3009                                  if (za.za_first_integer == oldnext_obj) {
2880 3010                                          /*
2881 3011                                           * We've already moved the
2882 3012                                           * origin's reference.
2883 3013                                           */
2884 3014                                          continue;
2885 3015                                  }
2886 3016  
2887 3017                                  VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
2888 3018                                      za.za_first_integer, FTAG, &cnds));
2889 3019                                  o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2890 3020  
2891 3021                                  VERIFY3U(zap_remove_int(dp->dp_meta_objset,
2892 3022                                      odd->dd_phys->dd_clones, o, tx), ==, 0);
2893 3023                                  VERIFY3U(zap_add_int(dp->dp_meta_objset,
2894 3024                                      dd->dd_phys->dd_clones, o, tx), ==, 0);
2895 3025                                  dsl_dataset_rele(cnds, FTAG);
2896 3026                          }
2897 3027                          zap_cursor_fini(&zc);
2898 3028                  }
2899 3029  
2900 3030                  ASSERT0(dsl_prop_numcb(ds));
2901 3031          }
2902 3032  
2903 3033          /*
2904 3034           * Change space accounting.
2905 3035           * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2906 3036           * both be valid, or both be 0 (resulting in delta == 0).  This
2907 3037           * is true for each of {clone,origin} independently.
2908 3038           */
2909 3039  
2910 3040          delta = pa->cloneusedsnap -
2911 3041              dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2912 3042          ASSERT3S(delta, >=, 0);
2913 3043          ASSERT3U(pa->used, >=, delta);
2914 3044          dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2915 3045          dsl_dir_diduse_space(dd, DD_USED_HEAD,
2916 3046              pa->used - delta, pa->comp, pa->uncomp, tx);
2917 3047  
2918 3048          delta = pa->originusedsnap -
2919 3049              odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2920 3050          ASSERT3S(delta, <=, 0);
2921 3051          ASSERT3U(pa->used, >=, -delta);
2922 3052          dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2923 3053          dsl_dir_diduse_space(odd, DD_USED_HEAD,
2924 3054              -pa->used - delta, -pa->comp, -pa->uncomp, tx);
2925 3055  
2926 3056          origin_ds->ds_phys->ds_unique_bytes = pa->unique;
2927 3057  
2928 3058          /* log history record */
2929 3059          spa_history_log_internal_ds(hds, "promote", tx, "");
2930 3060  
2931 3061          dsl_dir_close(odd, FTAG);
2932 3062  }
2933 3063  
2934 3064  static char *snaplist_tag = "snaplist";
2935 3065  /*
2936 3066   * Make a list of dsl_dataset_t's for the snapshots between first_obj
2937 3067   * (exclusive) and last_obj (inclusive).  The list will be in reverse
2938 3068   * order (last_obj will be the list_head()).  If first_obj == 0, do all
2939 3069   * snapshots back to this dataset's origin.
2940 3070   */
2941 3071  static int
2942 3072  snaplist_make(dsl_pool_t *dp, boolean_t own,
2943 3073      uint64_t first_obj, uint64_t last_obj, list_t *l)
2944 3074  {
2945 3075          uint64_t obj = last_obj;
2946 3076  
2947 3077          ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
2948 3078  
2949 3079          list_create(l, sizeof (struct promotenode),
2950 3080              offsetof(struct promotenode, link));
2951 3081  
2952 3082          while (obj != first_obj) {
2953 3083                  dsl_dataset_t *ds;
2954 3084                  struct promotenode *snap;
2955 3085                  int err;
2956 3086  
2957 3087                  if (own) {
2958 3088                          err = dsl_dataset_own_obj(dp, obj,
2959 3089                              0, snaplist_tag, &ds);
2960 3090                          if (err == 0)
2961 3091                                  dsl_dataset_make_exclusive(ds, snaplist_tag);
2962 3092                  } else {
2963 3093                          err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
2964 3094                  }
2965 3095                  if (err == ENOENT) {
2966 3096                          /* lost race with snapshot destroy */
2967 3097                          struct promotenode *last = list_tail(l);
2968 3098                          ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
2969 3099                          obj = last->ds->ds_phys->ds_prev_snap_obj;
2970 3100                          continue;
2971 3101                  } else if (err) {
2972 3102                          return (err);
2973 3103                  }
2974 3104  
2975 3105                  if (first_obj == 0)
2976 3106                          first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2977 3107  
2978 3108                  snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
2979 3109                  snap->ds = ds;
2980 3110                  list_insert_tail(l, snap);
2981 3111                  obj = ds->ds_phys->ds_prev_snap_obj;
2982 3112          }
2983 3113  
2984 3114          return (0);
2985 3115  }
2986 3116  
2987 3117  static int
2988 3118  snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2989 3119  {
2990 3120          struct promotenode *snap;
2991 3121  
2992 3122          *spacep = 0;
2993 3123          for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2994 3124                  uint64_t used, comp, uncomp;
2995 3125                  dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2996 3126                      mintxg, UINT64_MAX, &used, &comp, &uncomp);
2997 3127                  *spacep += used;
2998 3128          }
2999 3129          return (0);
3000 3130  }
3001 3131  
3002 3132  static void
3003 3133  snaplist_destroy(list_t *l, boolean_t own)
3004 3134  {
3005 3135          struct promotenode *snap;
3006 3136  
3007 3137          if (!l || !list_link_active(&l->list_head))
3008 3138                  return;
3009 3139  
3010 3140          while ((snap = list_tail(l)) != NULL) {
3011 3141                  list_remove(l, snap);
3012 3142                  if (own)
3013 3143                          dsl_dataset_disown(snap->ds, snaplist_tag);
3014 3144                  else
3015 3145                          dsl_dataset_rele(snap->ds, snaplist_tag);
3016 3146                  kmem_free(snap, sizeof (struct promotenode));
3017 3147          }
3018 3148          list_destroy(l);
3019 3149  }
3020 3150  
3021 3151  /*
3022 3152   * Promote a clone.  Nomenclature note:
3023 3153   * "clone" or "cds": the original clone which is being promoted
3024 3154   * "origin" or "ods": the snapshot which is originally clone's origin
3025 3155   * "origin head" or "ohds": the dataset which is the head
3026 3156   * (filesystem/volume) for the origin
3027 3157   * "origin origin": the origin of the origin's filesystem (typically
3028 3158   * NULL, indicating that the clone is not a clone of a clone).
3029 3159   */
3030 3160  int
3031 3161  dsl_dataset_promote(const char *name, char *conflsnap)
3032 3162  {
3033 3163          dsl_dataset_t *ds;
3034 3164          dsl_dir_t *dd;
3035 3165          dsl_pool_t *dp;
3036 3166          dmu_object_info_t doi;
3037 3167          struct promotearg pa = { 0 };
3038 3168          struct promotenode *snap;
3039 3169          int err;
3040 3170  
3041 3171          err = dsl_dataset_hold(name, FTAG, &ds);
3042 3172          if (err)
3043 3173                  return (err);
3044 3174          dd = ds->ds_dir;
3045 3175          dp = dd->dd_pool;
3046 3176  
3047 3177          err = dmu_object_info(dp->dp_meta_objset,
3048 3178              ds->ds_phys->ds_snapnames_zapobj, &doi);
3049 3179          if (err) {
3050 3180                  dsl_dataset_rele(ds, FTAG);
3051 3181                  return (err);
3052 3182          }
3053 3183  
3054 3184          if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
3055 3185                  dsl_dataset_rele(ds, FTAG);
3056 3186                  return (EINVAL);
3057 3187          }
3058 3188  
3059 3189          /*
3060 3190           * We are going to inherit all the snapshots taken before our
3061 3191           * origin (i.e., our new origin will be our parent's origin).
3062 3192           * Take ownership of them so that we can rename them into our
3063 3193           * namespace.
3064 3194           */
3065 3195          rw_enter(&dp->dp_config_rwlock, RW_READER);
3066 3196  
3067 3197          err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
3068 3198              &pa.shared_snaps);
3069 3199          if (err != 0)
3070 3200                  goto out;
3071 3201  
3072 3202          err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
3073 3203          if (err != 0)
3074 3204                  goto out;
3075 3205  
3076 3206          snap = list_head(&pa.shared_snaps);
3077 3207          ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
3078 3208          err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
3079 3209              snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
3080 3210          if (err != 0)
3081 3211                  goto out;
3082 3212  
  
    | ↓ open down ↓ | 217 lines elided | ↑ open up ↑ | 
3083 3213          if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
3084 3214                  err = dsl_dataset_hold_obj(dp,
3085 3215                      snap->ds->ds_dir->dd_phys->dd_origin_obj,
3086 3216                      FTAG, &pa.origin_origin);
3087 3217                  if (err != 0)
3088 3218                          goto out;
3089 3219          }
3090 3220  
3091 3221  out:
3092 3222          rw_exit(&dp->dp_config_rwlock);
     3223 +        pa.cr = CRED();
3093 3224  
3094 3225          /*
3095 3226           * Add in 128x the snapnames zapobj size, since we will be moving
3096 3227           * a bunch of snapnames to the promoted ds, and dirtying their
3097 3228           * bonus buffers.
3098 3229           */
3099 3230          if (err == 0) {
3100 3231                  err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
3101 3232                      dsl_dataset_promote_sync, ds, &pa,
3102 3233                      2 + 2 * doi.doi_physical_blocks_512);
3103 3234                  if (err && pa.err_ds && conflsnap)
3104 3235                          (void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN);
3105 3236          }
3106 3237  
3107 3238          snaplist_destroy(&pa.shared_snaps, B_TRUE);
3108 3239          snaplist_destroy(&pa.clone_snaps, B_FALSE);
3109 3240          snaplist_destroy(&pa.origin_snaps, B_FALSE);
3110 3241          if (pa.origin_origin)
3111 3242                  dsl_dataset_rele(pa.origin_origin, FTAG);
3112 3243          dsl_dataset_rele(ds, FTAG);
3113 3244          return (err);
3114 3245  }
3115 3246  
3116 3247  struct cloneswaparg {
3117 3248          dsl_dataset_t *cds; /* clone dataset */
3118 3249          dsl_dataset_t *ohds; /* origin's head dataset */
3119 3250          boolean_t force;
3120 3251          int64_t unused_refres_delta; /* change in unconsumed refreservation */
3121 3252  };
3122 3253  
3123 3254  /* ARGSUSED */
3124 3255  static int
3125 3256  dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
3126 3257  {
3127 3258          struct cloneswaparg *csa = arg1;
3128 3259  
3129 3260          /* they should both be heads */
3130 3261          if (dsl_dataset_is_snapshot(csa->cds) ||
3131 3262              dsl_dataset_is_snapshot(csa->ohds))
3132 3263                  return (EINVAL);
3133 3264  
3134 3265          /* the branch point should be just before them */
3135 3266          if (csa->cds->ds_prev != csa->ohds->ds_prev)
3136 3267                  return (EINVAL);
3137 3268  
3138 3269          /* cds should be the clone (unless they are unrelated) */
3139 3270          if (csa->cds->ds_prev != NULL &&
3140 3271              csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap &&
3141 3272              csa->ohds->ds_object !=
3142 3273              csa->cds->ds_prev->ds_phys->ds_next_snap_obj)
3143 3274                  return (EINVAL);
3144 3275  
3145 3276          /* the clone should be a child of the origin */
3146 3277          if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
3147 3278                  return (EINVAL);
3148 3279  
3149 3280          /* ohds shouldn't be modified unless 'force' */
3150 3281          if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
3151 3282                  return (ETXTBSY);
3152 3283  
3153 3284          /* adjust amount of any unconsumed refreservation */
3154 3285          csa->unused_refres_delta =
3155 3286              (int64_t)MIN(csa->ohds->ds_reserved,
3156 3287              csa->ohds->ds_phys->ds_unique_bytes) -
3157 3288              (int64_t)MIN(csa->ohds->ds_reserved,
3158 3289              csa->cds->ds_phys->ds_unique_bytes);
3159 3290  
3160 3291          if (csa->unused_refres_delta > 0 &&
3161 3292              csa->unused_refres_delta >
3162 3293              dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
3163 3294                  return (ENOSPC);
3164 3295  
3165 3296          if (csa->ohds->ds_quota != 0 &&
3166 3297              csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota)
3167 3298                  return (EDQUOT);
3168 3299  
3169 3300          return (0);
3170 3301  }
3171 3302  
3172 3303  /* ARGSUSED */
3173 3304  static void
3174 3305  dsl_dataset_clone_swap_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3175 3306  {
3176 3307          struct cloneswaparg *csa = arg1;
3177 3308          dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
3178 3309  
3179 3310          ASSERT(csa->cds->ds_reserved == 0);
3180 3311          ASSERT(csa->ohds->ds_quota == 0 ||
3181 3312              csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota);
3182 3313  
3183 3314          dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
3184 3315          dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
3185 3316  
3186 3317          if (csa->cds->ds_objset != NULL) {
3187 3318                  dmu_objset_evict(csa->cds->ds_objset);
3188 3319                  csa->cds->ds_objset = NULL;
3189 3320          }
3190 3321  
3191 3322          if (csa->ohds->ds_objset != NULL) {
3192 3323                  dmu_objset_evict(csa->ohds->ds_objset);
3193 3324                  csa->ohds->ds_objset = NULL;
3194 3325          }
3195 3326  
3196 3327          /*
3197 3328           * Reset origin's unique bytes, if it exists.
3198 3329           */
3199 3330          if (csa->cds->ds_prev) {
3200 3331                  dsl_dataset_t *origin = csa->cds->ds_prev;
3201 3332                  uint64_t comp, uncomp;
3202 3333  
3203 3334                  dmu_buf_will_dirty(origin->ds_dbuf, tx);
3204 3335                  dsl_deadlist_space_range(&csa->cds->ds_deadlist,
3205 3336                      origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
3206 3337                      &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
3207 3338          }
3208 3339  
3209 3340          /* swap blkptrs */
3210 3341          {
3211 3342                  blkptr_t tmp;
3212 3343                  tmp = csa->ohds->ds_phys->ds_bp;
3213 3344                  csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
3214 3345                  csa->cds->ds_phys->ds_bp = tmp;
3215 3346          }
3216 3347  
3217 3348          /* set dd_*_bytes */
3218 3349          {
3219 3350                  int64_t dused, dcomp, duncomp;
3220 3351                  uint64_t cdl_used, cdl_comp, cdl_uncomp;
3221 3352                  uint64_t odl_used, odl_comp, odl_uncomp;
3222 3353  
3223 3354                  ASSERT3U(csa->cds->ds_dir->dd_phys->
3224 3355                      dd_used_breakdown[DD_USED_SNAP], ==, 0);
3225 3356  
3226 3357                  dsl_deadlist_space(&csa->cds->ds_deadlist,
3227 3358                      &cdl_used, &cdl_comp, &cdl_uncomp);
3228 3359                  dsl_deadlist_space(&csa->ohds->ds_deadlist,
3229 3360                      &odl_used, &odl_comp, &odl_uncomp);
3230 3361  
3231 3362                  dused = csa->cds->ds_phys->ds_referenced_bytes + cdl_used -
3232 3363                      (csa->ohds->ds_phys->ds_referenced_bytes + odl_used);
3233 3364                  dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
3234 3365                      (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
3235 3366                  duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
3236 3367                      cdl_uncomp -
3237 3368                      (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
3238 3369  
3239 3370                  dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
3240 3371                      dused, dcomp, duncomp, tx);
3241 3372                  dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
3242 3373                      -dused, -dcomp, -duncomp, tx);
3243 3374  
3244 3375                  /*
3245 3376                   * The difference in the space used by snapshots is the
3246 3377                   * difference in snapshot space due to the head's
3247 3378                   * deadlist (since that's the only thing that's
3248 3379                   * changing that affects the snapused).
3249 3380                   */
3250 3381                  dsl_deadlist_space_range(&csa->cds->ds_deadlist,
3251 3382                      csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX,
3252 3383                      &cdl_used, &cdl_comp, &cdl_uncomp);
3253 3384                  dsl_deadlist_space_range(&csa->ohds->ds_deadlist,
3254 3385                      csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX,
3255 3386                      &odl_used, &odl_comp, &odl_uncomp);
3256 3387                  dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
3257 3388                      DD_USED_HEAD, DD_USED_SNAP, tx);
3258 3389          }
3259 3390  
3260 3391          /* swap ds_*_bytes */
3261 3392          SWITCH64(csa->ohds->ds_phys->ds_referenced_bytes,
3262 3393              csa->cds->ds_phys->ds_referenced_bytes);
3263 3394          SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
3264 3395              csa->cds->ds_phys->ds_compressed_bytes);
3265 3396          SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
3266 3397              csa->cds->ds_phys->ds_uncompressed_bytes);
3267 3398          SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
3268 3399              csa->cds->ds_phys->ds_unique_bytes);
3269 3400  
3270 3401          /* apply any parent delta for change in unconsumed refreservation */
3271 3402          dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
3272 3403              csa->unused_refres_delta, 0, 0, tx);
3273 3404  
3274 3405          /*
3275 3406           * Swap deadlists.
3276 3407           */
3277 3408          dsl_deadlist_close(&csa->cds->ds_deadlist);
3278 3409          dsl_deadlist_close(&csa->ohds->ds_deadlist);
3279 3410          SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
3280 3411              csa->cds->ds_phys->ds_deadlist_obj);
3281 3412          dsl_deadlist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
3282 3413              csa->cds->ds_phys->ds_deadlist_obj);
3283 3414          dsl_deadlist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
3284 3415              csa->ohds->ds_phys->ds_deadlist_obj);
3285 3416  
3286 3417          dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx);
3287 3418  
3288 3419          spa_history_log_internal_ds(csa->cds, "clone swap", tx,
3289 3420              "parent=%s", csa->ohds->ds_dir->dd_myname);
3290 3421  }
3291 3422  
3292 3423  /*
3293 3424   * Swap 'clone' with its origin head datasets.  Used at the end of "zfs
3294 3425   * recv" into an existing fs to swizzle the file system to the new
3295 3426   * version, and by "zfs rollback".  Can also be used to swap two
3296 3427   * independent head datasets if neither has any snapshots.
3297 3428   */
3298 3429  int
3299 3430  dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
3300 3431      boolean_t force)
3301 3432  {
3302 3433          struct cloneswaparg csa;
3303 3434          int error;
3304 3435  
3305 3436          ASSERT(clone->ds_owner);
3306 3437          ASSERT(origin_head->ds_owner);
3307 3438  retry:
3308 3439          /*
3309 3440           * Need exclusive access for the swap. If we're swapping these
3310 3441           * datasets back after an error, we already hold the locks.
3311 3442           */
3312 3443          if (!RW_WRITE_HELD(&clone->ds_rwlock))
3313 3444                  rw_enter(&clone->ds_rwlock, RW_WRITER);
3314 3445          if (!RW_WRITE_HELD(&origin_head->ds_rwlock) &&
3315 3446              !rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
3316 3447                  rw_exit(&clone->ds_rwlock);
3317 3448                  rw_enter(&origin_head->ds_rwlock, RW_WRITER);
3318 3449                  if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
3319 3450                          rw_exit(&origin_head->ds_rwlock);
3320 3451                          goto retry;
3321 3452                  }
3322 3453          }
3323 3454          csa.cds = clone;
3324 3455          csa.ohds = origin_head;
3325 3456          csa.force = force;
3326 3457          error = dsl_sync_task_do(clone->ds_dir->dd_pool,
3327 3458              dsl_dataset_clone_swap_check,
3328 3459              dsl_dataset_clone_swap_sync, &csa, NULL, 9);
3329 3460          return (error);
3330 3461  }
3331 3462  
3332 3463  /*
3333 3464   * Given a pool name and a dataset object number in that pool,
3334 3465   * return the name of that dataset.
3335 3466   */
3336 3467  int
3337 3468  dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3338 3469  {
3339 3470          spa_t *spa;
3340 3471          dsl_pool_t *dp;
3341 3472          dsl_dataset_t *ds;
3342 3473          int error;
3343 3474  
3344 3475          if ((error = spa_open(pname, &spa, FTAG)) != 0)
3345 3476                  return (error);
3346 3477          dp = spa_get_dsl(spa);
3347 3478          rw_enter(&dp->dp_config_rwlock, RW_READER);
3348 3479          if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
3349 3480                  dsl_dataset_name(ds, buf);
3350 3481                  dsl_dataset_rele(ds, FTAG);
3351 3482          }
3352 3483          rw_exit(&dp->dp_config_rwlock);
3353 3484          spa_close(spa, FTAG);
3354 3485  
3355 3486          return (error);
3356 3487  }
3357 3488  
3358 3489  int
3359 3490  dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3360 3491      uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3361 3492  {
3362 3493          int error = 0;
3363 3494  
3364 3495          ASSERT3S(asize, >, 0);
3365 3496  
3366 3497          /*
3367 3498           * *ref_rsrv is the portion of asize that will come from any
3368 3499           * unconsumed refreservation space.
3369 3500           */
3370 3501          *ref_rsrv = 0;
3371 3502  
3372 3503          mutex_enter(&ds->ds_lock);
3373 3504          /*
3374 3505           * Make a space adjustment for reserved bytes.
3375 3506           */
3376 3507          if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
3377 3508                  ASSERT3U(*used, >=,
3378 3509                      ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
3379 3510                  *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
3380 3511                  *ref_rsrv =
3381 3512                      asize - MIN(asize, parent_delta(ds, asize + inflight));
3382 3513          }
3383 3514  
3384 3515          if (!check_quota || ds->ds_quota == 0) {
3385 3516                  mutex_exit(&ds->ds_lock);
3386 3517                  return (0);
3387 3518          }
3388 3519          /*
3389 3520           * If they are requesting more space, and our current estimate
3390 3521           * is over quota, they get to try again unless the actual
3391 3522           * on-disk is over quota and there are no pending changes (which
3392 3523           * may free up space for us).
3393 3524           */
3394 3525          if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
3395 3526                  if (inflight > 0 ||
3396 3527                      ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
3397 3528                          error = ERESTART;
3398 3529                  else
3399 3530                          error = EDQUOT;
3400 3531          }
3401 3532          mutex_exit(&ds->ds_lock);
3402 3533  
3403 3534          return (error);
3404 3535  }
3405 3536  
3406 3537  /* ARGSUSED */
3407 3538  static int
3408 3539  dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
3409 3540  {
3410 3541          dsl_dataset_t *ds = arg1;
3411 3542          dsl_prop_setarg_t *psa = arg2;
3412 3543          int err;
3413 3544  
3414 3545          if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
3415 3546                  return (ENOTSUP);
3416 3547  
3417 3548          if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
3418 3549                  return (err);
3419 3550  
3420 3551          if (psa->psa_effective_value == 0)
3421 3552                  return (0);
3422 3553  
3423 3554          if (psa->psa_effective_value < ds->ds_phys->ds_referenced_bytes ||
3424 3555              psa->psa_effective_value < ds->ds_reserved)
3425 3556                  return (ENOSPC);
3426 3557  
3427 3558          return (0);
3428 3559  }
3429 3560  
3430 3561  extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *);
3431 3562  
3432 3563  void
3433 3564  dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3434 3565  {
3435 3566          dsl_dataset_t *ds = arg1;
3436 3567          dsl_prop_setarg_t *psa = arg2;
3437 3568          uint64_t effective_value = psa->psa_effective_value;
3438 3569  
3439 3570          dsl_prop_set_sync(ds, psa, tx);
3440 3571          DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3441 3572  
3442 3573          if (ds->ds_quota != effective_value) {
3443 3574                  dmu_buf_will_dirty(ds->ds_dbuf, tx);
3444 3575                  ds->ds_quota = effective_value;
3445 3576          }
3446 3577  }
3447 3578  
3448 3579  int
3449 3580  dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
3450 3581  {
3451 3582          dsl_dataset_t *ds;
3452 3583          dsl_prop_setarg_t psa;
3453 3584          int err;
3454 3585  
3455 3586          dsl_prop_setarg_init_uint64(&psa, "refquota", source, "a);
3456 3587  
3457 3588          err = dsl_dataset_hold(dsname, FTAG, &ds);
3458 3589          if (err)
3459 3590                  return (err);
3460 3591  
3461 3592          /*
3462 3593           * If someone removes a file, then tries to set the quota, we
3463 3594           * want to make sure the file freeing takes effect.
3464 3595           */
3465 3596          txg_wait_open(ds->ds_dir->dd_pool, 0);
3466 3597  
3467 3598          err = dsl_sync_task_do(ds->ds_dir->dd_pool,
3468 3599              dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
3469 3600              ds, &psa, 0);
3470 3601  
3471 3602          dsl_dataset_rele(ds, FTAG);
3472 3603          return (err);
3473 3604  }
3474 3605  
3475 3606  static int
3476 3607  dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
3477 3608  {
3478 3609          dsl_dataset_t *ds = arg1;
3479 3610          dsl_prop_setarg_t *psa = arg2;
3480 3611          uint64_t effective_value;
3481 3612          uint64_t unique;
3482 3613          int err;
3483 3614  
3484 3615          if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
3485 3616              SPA_VERSION_REFRESERVATION)
3486 3617                  return (ENOTSUP);
3487 3618  
3488 3619          if (dsl_dataset_is_snapshot(ds))
3489 3620                  return (EINVAL);
3490 3621  
3491 3622          if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
3492 3623                  return (err);
3493 3624  
3494 3625          effective_value = psa->psa_effective_value;
3495 3626  
3496 3627          /*
3497 3628           * If we are doing the preliminary check in open context, the
3498 3629           * space estimates may be inaccurate.
3499 3630           */
3500 3631          if (!dmu_tx_is_syncing(tx))
3501 3632                  return (0);
3502 3633  
3503 3634          mutex_enter(&ds->ds_lock);
3504 3635          if (!DS_UNIQUE_IS_ACCURATE(ds))
3505 3636                  dsl_dataset_recalc_head_uniq(ds);
3506 3637          unique = ds->ds_phys->ds_unique_bytes;
3507 3638          mutex_exit(&ds->ds_lock);
3508 3639  
3509 3640          if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) {
3510 3641                  uint64_t delta = MAX(unique, effective_value) -
3511 3642                      MAX(unique, ds->ds_reserved);
3512 3643  
3513 3644                  if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
3514 3645                          return (ENOSPC);
3515 3646                  if (ds->ds_quota > 0 &&
3516 3647                      effective_value > ds->ds_quota)
3517 3648                          return (ENOSPC);
3518 3649          }
3519 3650  
3520 3651          return (0);
3521 3652  }
3522 3653  
3523 3654  static void
3524 3655  dsl_dataset_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3525 3656  {
3526 3657          dsl_dataset_t *ds = arg1;
3527 3658          dsl_prop_setarg_t *psa = arg2;
3528 3659          uint64_t effective_value = psa->psa_effective_value;
3529 3660          uint64_t unique;
3530 3661          int64_t delta;
3531 3662  
3532 3663          dsl_prop_set_sync(ds, psa, tx);
3533 3664          DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3534 3665  
3535 3666          dmu_buf_will_dirty(ds->ds_dbuf, tx);
3536 3667  
3537 3668          mutex_enter(&ds->ds_dir->dd_lock);
3538 3669          mutex_enter(&ds->ds_lock);
3539 3670          ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3540 3671          unique = ds->ds_phys->ds_unique_bytes;
3541 3672          delta = MAX(0, (int64_t)(effective_value - unique)) -
3542 3673              MAX(0, (int64_t)(ds->ds_reserved - unique));
3543 3674          ds->ds_reserved = effective_value;
3544 3675          mutex_exit(&ds->ds_lock);
3545 3676  
3546 3677          dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3547 3678          mutex_exit(&ds->ds_dir->dd_lock);
3548 3679  }
3549 3680  
3550 3681  int
3551 3682  dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
3552 3683      uint64_t reservation)
3553 3684  {
3554 3685          dsl_dataset_t *ds;
3555 3686          dsl_prop_setarg_t psa;
3556 3687          int err;
3557 3688  
3558 3689          dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
3559 3690              &reservation);
3560 3691  
3561 3692          err = dsl_dataset_hold(dsname, FTAG, &ds);
3562 3693          if (err)
3563 3694                  return (err);
3564 3695  
3565 3696          err = dsl_sync_task_do(ds->ds_dir->dd_pool,
3566 3697              dsl_dataset_set_reservation_check,
3567 3698              dsl_dataset_set_reservation_sync, ds, &psa, 0);
3568 3699  
3569 3700          dsl_dataset_rele(ds, FTAG);
3570 3701          return (err);
3571 3702  }
3572 3703  
3573 3704  typedef struct zfs_hold_cleanup_arg {
3574 3705          dsl_pool_t *dp;
3575 3706          uint64_t dsobj;
3576 3707          char htag[MAXNAMELEN];
3577 3708  } zfs_hold_cleanup_arg_t;
3578 3709  
3579 3710  static void
3580 3711  dsl_dataset_user_release_onexit(void *arg)
3581 3712  {
3582 3713          zfs_hold_cleanup_arg_t *ca = arg;
3583 3714  
3584 3715          (void) dsl_dataset_user_release_tmp(ca->dp, ca->dsobj, ca->htag,
3585 3716              B_TRUE);
3586 3717          kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t));
3587 3718  }
3588 3719  
3589 3720  void
3590 3721  dsl_register_onexit_hold_cleanup(dsl_dataset_t *ds, const char *htag,
3591 3722      minor_t minor)
3592 3723  {
3593 3724          zfs_hold_cleanup_arg_t *ca;
3594 3725  
3595 3726          ca = kmem_alloc(sizeof (zfs_hold_cleanup_arg_t), KM_SLEEP);
3596 3727          ca->dp = ds->ds_dir->dd_pool;
3597 3728          ca->dsobj = ds->ds_object;
3598 3729          (void) strlcpy(ca->htag, htag, sizeof (ca->htag));
3599 3730          VERIFY3U(0, ==, zfs_onexit_add_cb(minor,
3600 3731              dsl_dataset_user_release_onexit, ca, NULL));
3601 3732  }
3602 3733  
3603 3734  /*
3604 3735   * If you add new checks here, you may need to add
3605 3736   * additional checks to the "temporary" case in
3606 3737   * snapshot_check() in dmu_objset.c.
3607 3738   */
3608 3739  static int
3609 3740  dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
3610 3741  {
3611 3742          dsl_dataset_t *ds = arg1;
3612 3743          struct dsl_ds_holdarg *ha = arg2;
3613 3744          const char *htag = ha->htag;
3614 3745          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3615 3746          int error = 0;
3616 3747  
3617 3748          if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3618 3749                  return (ENOTSUP);
3619 3750  
3620 3751          if (!dsl_dataset_is_snapshot(ds))
3621 3752                  return (EINVAL);
3622 3753  
3623 3754          /* tags must be unique */
3624 3755          mutex_enter(&ds->ds_lock);
3625 3756          if (ds->ds_phys->ds_userrefs_obj) {
3626 3757                  error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
3627 3758                      8, 1, tx);
3628 3759                  if (error == 0)
3629 3760                          error = EEXIST;
3630 3761                  else if (error == ENOENT)
3631 3762                          error = 0;
3632 3763          }
3633 3764          mutex_exit(&ds->ds_lock);
3634 3765  
3635 3766          if (error == 0 && ha->temphold &&
3636 3767              strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
3637 3768                  error = E2BIG;
3638 3769  
3639 3770          return (error);
3640 3771  }
3641 3772  
3642 3773  void
3643 3774  dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3644 3775  {
3645 3776          dsl_dataset_t *ds = arg1;
3646 3777          struct dsl_ds_holdarg *ha = arg2;
3647 3778          const char *htag = ha->htag;
3648 3779          dsl_pool_t *dp = ds->ds_dir->dd_pool;
3649 3780          objset_t *mos = dp->dp_meta_objset;
3650 3781          uint64_t now = gethrestime_sec();
3651 3782          uint64_t zapobj;
3652 3783  
3653 3784          mutex_enter(&ds->ds_lock);
3654 3785          if (ds->ds_phys->ds_userrefs_obj == 0) {
3655 3786                  /*
3656 3787                   * This is the first user hold for this dataset.  Create
3657 3788                   * the userrefs zap object.
3658 3789                   */
3659 3790                  dmu_buf_will_dirty(ds->ds_dbuf, tx);
3660 3791                  zapobj = ds->ds_phys->ds_userrefs_obj =
3661 3792                      zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
3662 3793          } else {
3663 3794                  zapobj = ds->ds_phys->ds_userrefs_obj;
3664 3795          }
3665 3796          ds->ds_userrefs++;
3666 3797          mutex_exit(&ds->ds_lock);
3667 3798  
3668 3799          VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
3669 3800  
3670 3801          if (ha->temphold) {
3671 3802                  VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
3672 3803                      htag, &now, tx));
3673 3804          }
3674 3805  
3675 3806          spa_history_log_internal_ds(ds, "hold", tx,
3676 3807              "tag = %s temp = %d holds now = %llu",
3677 3808              htag, (int)ha->temphold, ds->ds_userrefs);
3678 3809  }
3679 3810  
3680 3811  static int
3681 3812  dsl_dataset_user_hold_one(const char *dsname, void *arg)
3682 3813  {
3683 3814          struct dsl_ds_holdarg *ha = arg;
3684 3815          dsl_dataset_t *ds;
3685 3816          int error;
3686 3817          char *name;
3687 3818  
3688 3819          /* alloc a buffer to hold dsname@snapname plus terminating NULL */
3689 3820          name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3690 3821          error = dsl_dataset_hold(name, ha->dstg, &ds);
3691 3822          strfree(name);
3692 3823          if (error == 0) {
3693 3824                  ha->gotone = B_TRUE;
3694 3825                  dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
3695 3826                      dsl_dataset_user_hold_sync, ds, ha, 0);
3696 3827          } else if (error == ENOENT && ha->recursive) {
3697 3828                  error = 0;
3698 3829          } else {
3699 3830                  (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3700 3831          }
3701 3832          return (error);
3702 3833  }
3703 3834  
3704 3835  int
3705 3836  dsl_dataset_user_hold_for_send(dsl_dataset_t *ds, char *htag,
3706 3837      boolean_t temphold)
3707 3838  {
3708 3839          struct dsl_ds_holdarg *ha;
3709 3840          int error;
3710 3841  
3711 3842          ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3712 3843          ha->htag = htag;
3713 3844          ha->temphold = temphold;
3714 3845          error = dsl_sync_task_do(ds->ds_dir->dd_pool,
3715 3846              dsl_dataset_user_hold_check, dsl_dataset_user_hold_sync,
3716 3847              ds, ha, 0);
3717 3848          kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3718 3849  
3719 3850          return (error);
3720 3851  }
3721 3852  
3722 3853  int
3723 3854  dsl_dataset_user_hold(char *dsname, char *snapname, char *htag,
3724 3855      boolean_t recursive, boolean_t temphold, int cleanup_fd)
3725 3856  {
3726 3857          struct dsl_ds_holdarg *ha;
3727 3858          dsl_sync_task_t *dst;
3728 3859          spa_t *spa;
3729 3860          int error;
3730 3861          minor_t minor = 0;
3731 3862  
3732 3863          if (cleanup_fd != -1) {
3733 3864                  /* Currently we only support cleanup-on-exit of tempholds. */
3734 3865                  if (!temphold)
3735 3866                          return (EINVAL);
3736 3867                  error = zfs_onexit_fd_hold(cleanup_fd, &minor);
3737 3868                  if (error)
3738 3869                          return (error);
3739 3870          }
3740 3871  
3741 3872          ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3742 3873  
3743 3874          (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3744 3875  
3745 3876          error = spa_open(dsname, &spa, FTAG);
3746 3877          if (error) {
3747 3878                  kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3748 3879                  if (cleanup_fd != -1)
3749 3880                          zfs_onexit_fd_rele(cleanup_fd);
3750 3881                  return (error);
3751 3882          }
3752 3883  
3753 3884          ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
3754 3885          ha->htag = htag;
3755 3886          ha->snapname = snapname;
3756 3887          ha->recursive = recursive;
3757 3888          ha->temphold = temphold;
3758 3889  
3759 3890          if (recursive) {
3760 3891                  error = dmu_objset_find(dsname, dsl_dataset_user_hold_one,
3761 3892                      ha, DS_FIND_CHILDREN);
3762 3893          } else {
3763 3894                  error = dsl_dataset_user_hold_one(dsname, ha);
3764 3895          }
3765 3896          if (error == 0)
3766 3897                  error = dsl_sync_task_group_wait(ha->dstg);
3767 3898  
3768 3899          for (dst = list_head(&ha->dstg->dstg_tasks); dst;
3769 3900              dst = list_next(&ha->dstg->dstg_tasks, dst)) {
3770 3901                  dsl_dataset_t *ds = dst->dst_arg1;
3771 3902  
3772 3903                  if (dst->dst_err) {
3773 3904                          dsl_dataset_name(ds, ha->failed);
3774 3905                          *strchr(ha->failed, '@') = '\0';
3775 3906                  } else if (error == 0 && minor != 0 && temphold) {
3776 3907                          /*
3777 3908                           * If this hold is to be released upon process exit,
3778 3909                           * register that action now.
3779 3910                           */
3780 3911                          dsl_register_onexit_hold_cleanup(ds, htag, minor);
3781 3912                  }
3782 3913                  dsl_dataset_rele(ds, ha->dstg);
3783 3914          }
3784 3915  
3785 3916          if (error == 0 && recursive && !ha->gotone)
3786 3917                  error = ENOENT;
3787 3918  
3788 3919          if (error)
3789 3920                  (void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
3790 3921  
3791 3922          dsl_sync_task_group_destroy(ha->dstg);
3792 3923  
3793 3924          kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3794 3925          spa_close(spa, FTAG);
3795 3926          if (cleanup_fd != -1)
3796 3927                  zfs_onexit_fd_rele(cleanup_fd);
3797 3928          return (error);
3798 3929  }
3799 3930  
3800 3931  struct dsl_ds_releasearg {
3801 3932          dsl_dataset_t *ds;
3802 3933          const char *htag;
3803 3934          boolean_t own;          /* do we own or just hold ds? */
3804 3935  };
3805 3936  
3806 3937  static int
3807 3938  dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag,
3808 3939      boolean_t *might_destroy)
3809 3940  {
3810 3941          objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3811 3942          uint64_t zapobj;
3812 3943          uint64_t tmp;
3813 3944          int error;
3814 3945  
3815 3946          *might_destroy = B_FALSE;
3816 3947  
3817 3948          mutex_enter(&ds->ds_lock);
3818 3949          zapobj = ds->ds_phys->ds_userrefs_obj;
3819 3950          if (zapobj == 0) {
3820 3951                  /* The tag can't possibly exist */
3821 3952                  mutex_exit(&ds->ds_lock);
3822 3953                  return (ESRCH);
3823 3954          }
3824 3955  
3825 3956          /* Make sure the tag exists */
3826 3957          error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp);
3827 3958          if (error) {
3828 3959                  mutex_exit(&ds->ds_lock);
3829 3960                  if (error == ENOENT)
3830 3961                          error = ESRCH;
3831 3962                  return (error);
3832 3963          }
3833 3964  
3834 3965          if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 &&
3835 3966              DS_IS_DEFER_DESTROY(ds))
3836 3967                  *might_destroy = B_TRUE;
3837 3968  
3838 3969          mutex_exit(&ds->ds_lock);
3839 3970          return (0);
3840 3971  }
3841 3972  
3842 3973  static int
3843 3974  dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx)
3844 3975  {
3845 3976          struct dsl_ds_releasearg *ra = arg1;
3846 3977          dsl_dataset_t *ds = ra->ds;
3847 3978          boolean_t might_destroy;
3848 3979          int error;
3849 3980  
3850 3981          if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3851 3982                  return (ENOTSUP);
3852 3983  
3853 3984          error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy);
3854 3985          if (error)
3855 3986                  return (error);
3856 3987  
3857 3988          if (might_destroy) {
3858 3989                  struct dsl_ds_destroyarg dsda = {0};
3859 3990  
3860 3991                  if (dmu_tx_is_syncing(tx)) {
3861 3992                          /*
3862 3993                           * If we're not prepared to remove the snapshot,
3863 3994                           * we can't allow the release to happen right now.
3864 3995                           */
3865 3996                          if (!ra->own)
3866 3997                                  return (EBUSY);
3867 3998                  }
3868 3999                  dsda.ds = ds;
3869 4000                  dsda.releasing = B_TRUE;
3870 4001                  return (dsl_dataset_destroy_check(&dsda, tag, tx));
3871 4002          }
3872 4003  
3873 4004          return (0);
3874 4005  }
3875 4006  
3876 4007  static void
3877 4008  dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx)
3878 4009  {
3879 4010          struct dsl_ds_releasearg *ra = arg1;
3880 4011          dsl_dataset_t *ds = ra->ds;
3881 4012          dsl_pool_t *dp = ds->ds_dir->dd_pool;
3882 4013          objset_t *mos = dp->dp_meta_objset;
3883 4014          uint64_t zapobj;
3884 4015          uint64_t refs;
3885 4016          int error;
3886 4017  
3887 4018          mutex_enter(&ds->ds_lock);
3888 4019          ds->ds_userrefs--;
3889 4020          refs = ds->ds_userrefs;
3890 4021          mutex_exit(&ds->ds_lock);
3891 4022          error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
3892 4023          VERIFY(error == 0 || error == ENOENT);
3893 4024          zapobj = ds->ds_phys->ds_userrefs_obj;
3894 4025          VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
3895 4026  
3896 4027          spa_history_log_internal_ds(ds, "release", tx,
3897 4028              "tag = %s refs now = %lld", ra->htag, (longlong_t)refs);
3898 4029  
3899 4030          if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
3900 4031              DS_IS_DEFER_DESTROY(ds)) {
3901 4032                  struct dsl_ds_destroyarg dsda = {0};
3902 4033  
3903 4034                  ASSERT(ra->own);
3904 4035                  dsda.ds = ds;
3905 4036                  dsda.releasing = B_TRUE;
3906 4037                  /* We already did the destroy_check */
3907 4038                  dsl_dataset_destroy_sync(&dsda, tag, tx);
3908 4039          }
3909 4040  }
3910 4041  
3911 4042  static int
3912 4043  dsl_dataset_user_release_one(const char *dsname, void *arg)
3913 4044  {
3914 4045          struct dsl_ds_holdarg *ha = arg;
3915 4046          struct dsl_ds_releasearg *ra;
3916 4047          dsl_dataset_t *ds;
3917 4048          int error;
3918 4049          void *dtag = ha->dstg;
3919 4050          char *name;
3920 4051          boolean_t own = B_FALSE;
3921 4052          boolean_t might_destroy;
3922 4053  
3923 4054          /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
3924 4055          name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3925 4056          error = dsl_dataset_hold(name, dtag, &ds);
3926 4057          strfree(name);
3927 4058          if (error == ENOENT && ha->recursive)
3928 4059                  return (0);
3929 4060          (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3930 4061          if (error)
3931 4062                  return (error);
3932 4063  
3933 4064          ha->gotone = B_TRUE;
3934 4065  
3935 4066          ASSERT(dsl_dataset_is_snapshot(ds));
3936 4067  
3937 4068          error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy);
3938 4069          if (error) {
3939 4070                  dsl_dataset_rele(ds, dtag);
3940 4071                  return (error);
3941 4072          }
3942 4073  
3943 4074          if (might_destroy) {
3944 4075  #ifdef _KERNEL
3945 4076                  name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3946 4077                  error = zfs_unmount_snap(name, NULL);
3947 4078                  strfree(name);
3948 4079                  if (error) {
3949 4080                          dsl_dataset_rele(ds, dtag);
3950 4081                          return (error);
3951 4082                  }
3952 4083  #endif
3953 4084                  if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) {
3954 4085                          dsl_dataset_rele(ds, dtag);
3955 4086                          return (EBUSY);
3956 4087                  } else {
3957 4088                          own = B_TRUE;
3958 4089                          dsl_dataset_make_exclusive(ds, dtag);
3959 4090                  }
3960 4091          }
3961 4092  
3962 4093          ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP);
3963 4094          ra->ds = ds;
3964 4095          ra->htag = ha->htag;
3965 4096          ra->own = own;
3966 4097          dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check,
3967 4098              dsl_dataset_user_release_sync, ra, dtag, 0);
3968 4099  
3969 4100          return (0);
3970 4101  }
3971 4102  
3972 4103  int
3973 4104  dsl_dataset_user_release(char *dsname, char *snapname, char *htag,
3974 4105      boolean_t recursive)
3975 4106  {
3976 4107          struct dsl_ds_holdarg *ha;
3977 4108          dsl_sync_task_t *dst;
3978 4109          spa_t *spa;
3979 4110          int error;
3980 4111  
3981 4112  top:
3982 4113          ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3983 4114  
3984 4115          (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3985 4116  
3986 4117          error = spa_open(dsname, &spa, FTAG);
3987 4118          if (error) {
3988 4119                  kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3989 4120                  return (error);
3990 4121          }
3991 4122  
3992 4123          ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
3993 4124          ha->htag = htag;
3994 4125          ha->snapname = snapname;
3995 4126          ha->recursive = recursive;
3996 4127          if (recursive) {
3997 4128                  error = dmu_objset_find(dsname, dsl_dataset_user_release_one,
3998 4129                      ha, DS_FIND_CHILDREN);
3999 4130          } else {
4000 4131                  error = dsl_dataset_user_release_one(dsname, ha);
4001 4132          }
4002 4133          if (error == 0)
4003 4134                  error = dsl_sync_task_group_wait(ha->dstg);
4004 4135  
4005 4136          for (dst = list_head(&ha->dstg->dstg_tasks); dst;
4006 4137              dst = list_next(&ha->dstg->dstg_tasks, dst)) {
4007 4138                  struct dsl_ds_releasearg *ra = dst->dst_arg1;
4008 4139                  dsl_dataset_t *ds = ra->ds;
4009 4140  
4010 4141                  if (dst->dst_err)
4011 4142                          dsl_dataset_name(ds, ha->failed);
4012 4143  
4013 4144                  if (ra->own)
4014 4145                          dsl_dataset_disown(ds, ha->dstg);
4015 4146                  else
4016 4147                          dsl_dataset_rele(ds, ha->dstg);
4017 4148  
4018 4149                  kmem_free(ra, sizeof (struct dsl_ds_releasearg));
4019 4150          }
4020 4151  
4021 4152          if (error == 0 && recursive && !ha->gotone)
4022 4153                  error = ENOENT;
4023 4154  
4024 4155          if (error && error != EBUSY)
4025 4156                  (void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
4026 4157  
4027 4158          dsl_sync_task_group_destroy(ha->dstg);
4028 4159          kmem_free(ha, sizeof (struct dsl_ds_holdarg));
4029 4160          spa_close(spa, FTAG);
4030 4161  
4031 4162          /*
4032 4163           * We can get EBUSY if we were racing with deferred destroy and
4033 4164           * dsl_dataset_user_release_check() hadn't done the necessary
4034 4165           * open context setup.  We can also get EBUSY if we're racing
4035 4166           * with destroy and that thread is the ds_owner.  Either way
4036 4167           * the busy condition should be transient, and we should retry
4037 4168           * the release operation.
4038 4169           */
4039 4170          if (error == EBUSY)
4040 4171                  goto top;
4041 4172  
4042 4173          return (error);
4043 4174  }
4044 4175  
4045 4176  /*
4046 4177   * Called at spa_load time (with retry == B_FALSE) to release a stale
4047 4178   * temporary user hold. Also called by the onexit code (with retry == B_TRUE).
4048 4179   */
4049 4180  int
4050 4181  dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag,
4051 4182      boolean_t retry)
4052 4183  {
4053 4184          dsl_dataset_t *ds;
4054 4185          char *snap;
4055 4186          char *name;
4056 4187          int namelen;
4057 4188          int error;
4058 4189  
4059 4190          do {
4060 4191                  rw_enter(&dp->dp_config_rwlock, RW_READER);
4061 4192                  error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
4062 4193                  rw_exit(&dp->dp_config_rwlock);
4063 4194                  if (error)
4064 4195                          return (error);
4065 4196                  namelen = dsl_dataset_namelen(ds)+1;
4066 4197                  name = kmem_alloc(namelen, KM_SLEEP);
4067 4198                  dsl_dataset_name(ds, name);
4068 4199                  dsl_dataset_rele(ds, FTAG);
4069 4200  
4070 4201                  snap = strchr(name, '@');
4071 4202                  *snap = '\0';
4072 4203                  ++snap;
4073 4204                  error = dsl_dataset_user_release(name, snap, htag, B_FALSE);
4074 4205                  kmem_free(name, namelen);
4075 4206  
4076 4207                  /*
4077 4208                   * The object can't have been destroyed because we have a hold,
4078 4209                   * but it might have been renamed, resulting in ENOENT.  Retry
4079 4210                   * if we've been requested to do so.
4080 4211                   *
4081 4212                   * It would be nice if we could use the dsobj all the way
4082 4213                   * through and avoid ENOENT entirely.  But we might need to
4083 4214                   * unmount the snapshot, and there's currently no way to lookup
4084 4215                   * a vfsp using a ZFS object id.
4085 4216                   */
4086 4217          } while ((error == ENOENT) && retry);
4087 4218  
4088 4219          return (error);
4089 4220  }
4090 4221  
4091 4222  int
4092 4223  dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp)
4093 4224  {
4094 4225          dsl_dataset_t *ds;
4095 4226          int err;
4096 4227  
4097 4228          err = dsl_dataset_hold(dsname, FTAG, &ds);
4098 4229          if (err)
4099 4230                  return (err);
4100 4231  
4101 4232          VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP));
4102 4233          if (ds->ds_phys->ds_userrefs_obj != 0) {
4103 4234                  zap_attribute_t *za;
4104 4235                  zap_cursor_t zc;
4105 4236  
4106 4237                  za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
4107 4238                  for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
4108 4239                      ds->ds_phys->ds_userrefs_obj);
4109 4240                      zap_cursor_retrieve(&zc, za) == 0;
4110 4241                      zap_cursor_advance(&zc)) {
4111 4242                          VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name,
4112 4243                              za->za_first_integer));
4113 4244                  }
4114 4245                  zap_cursor_fini(&zc);
4115 4246                  kmem_free(za, sizeof (zap_attribute_t));
4116 4247          }
4117 4248          dsl_dataset_rele(ds, FTAG);
4118 4249          return (0);
4119 4250  }
4120 4251  
4121 4252  /*
4122 4253   * Note, this function is used as the callback for dmu_objset_find().  We
4123 4254   * always return 0 so that we will continue to find and process
4124 4255   * inconsistent datasets, even if we encounter an error trying to
4125 4256   * process one of them.
4126 4257   */
4127 4258  /* ARGSUSED */
4128 4259  int
4129 4260  dsl_destroy_inconsistent(const char *dsname, void *arg)
4130 4261  {
4131 4262          dsl_dataset_t *ds;
4132 4263  
4133 4264          if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) {
4134 4265                  if (DS_IS_INCONSISTENT(ds))
4135 4266                          (void) dsl_dataset_destroy(ds, FTAG, B_FALSE);
4136 4267                  else
4137 4268                          dsl_dataset_disown(ds, FTAG);
4138 4269          }
4139 4270          return (0);
4140 4271  }
4141 4272  
4142 4273  /*
4143 4274   * Return (in *usedp) the amount of space written in new that is not
4144 4275   * present in oldsnap.  New may be a snapshot or the head.  Old must be
4145 4276   * a snapshot before new, in new's filesystem (or its origin).  If not then
4146 4277   * fail and return EINVAL.
4147 4278   *
4148 4279   * The written space is calculated by considering two components:  First, we
4149 4280   * ignore any freed space, and calculate the written as new's used space
4150 4281   * minus old's used space.  Next, we add in the amount of space that was freed
4151 4282   * between the two snapshots, thus reducing new's used space relative to old's.
4152 4283   * Specifically, this is the space that was born before old->ds_creation_txg,
4153 4284   * and freed before new (ie. on new's deadlist or a previous deadlist).
4154 4285   *
4155 4286   * space freed                         [---------------------]
4156 4287   * snapshots                       ---O-------O--------O-------O------
4157 4288   *                                         oldsnap            new
4158 4289   */
4159 4290  int
4160 4291  dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
4161 4292      uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4162 4293  {
4163 4294          int err = 0;
4164 4295          uint64_t snapobj;
4165 4296          dsl_pool_t *dp = new->ds_dir->dd_pool;
4166 4297  
4167 4298          *usedp = 0;
4168 4299          *usedp += new->ds_phys->ds_referenced_bytes;
4169 4300          *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
4170 4301  
4171 4302          *compp = 0;
4172 4303          *compp += new->ds_phys->ds_compressed_bytes;
4173 4304          *compp -= oldsnap->ds_phys->ds_compressed_bytes;
4174 4305  
4175 4306          *uncompp = 0;
4176 4307          *uncompp += new->ds_phys->ds_uncompressed_bytes;
4177 4308          *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
4178 4309  
4179 4310          rw_enter(&dp->dp_config_rwlock, RW_READER);
4180 4311          snapobj = new->ds_object;
4181 4312          while (snapobj != oldsnap->ds_object) {
4182 4313                  dsl_dataset_t *snap;
4183 4314                  uint64_t used, comp, uncomp;
4184 4315  
4185 4316                  if (snapobj == new->ds_object) {
4186 4317                          snap = new;
4187 4318                  } else {
4188 4319                          err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
4189 4320                          if (err != 0)
4190 4321                                  break;
4191 4322                  }
4192 4323  
4193 4324                  if (snap->ds_phys->ds_prev_snap_txg ==
4194 4325                      oldsnap->ds_phys->ds_creation_txg) {
4195 4326                          /*
4196 4327                           * The blocks in the deadlist can not be born after
4197 4328                           * ds_prev_snap_txg, so get the whole deadlist space,
4198 4329                           * which is more efficient (especially for old-format
4199 4330                           * deadlists).  Unfortunately the deadlist code
4200 4331                           * doesn't have enough information to make this
4201 4332                           * optimization itself.
4202 4333                           */
4203 4334                          dsl_deadlist_space(&snap->ds_deadlist,
4204 4335                              &used, &comp, &uncomp);
4205 4336                  } else {
4206 4337                          dsl_deadlist_space_range(&snap->ds_deadlist,
4207 4338                              0, oldsnap->ds_phys->ds_creation_txg,
4208 4339                              &used, &comp, &uncomp);
4209 4340                  }
4210 4341                  *usedp += used;
4211 4342                  *compp += comp;
4212 4343                  *uncompp += uncomp;
4213 4344  
4214 4345                  /*
4215 4346                   * If we get to the beginning of the chain of snapshots
4216 4347                   * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
4217 4348                   * was not a snapshot of/before new.
4218 4349                   */
4219 4350                  snapobj = snap->ds_phys->ds_prev_snap_obj;
4220 4351                  if (snap != new)
4221 4352                          dsl_dataset_rele(snap, FTAG);
4222 4353                  if (snapobj == 0) {
4223 4354                          err = EINVAL;
4224 4355                          break;
4225 4356                  }
4226 4357  
4227 4358          }
4228 4359          rw_exit(&dp->dp_config_rwlock);
4229 4360          return (err);
4230 4361  }
4231 4362  
4232 4363  /*
4233 4364   * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
4234 4365   * lastsnap, and all snapshots in between are deleted.
4235 4366   *
4236 4367   * blocks that would be freed            [---------------------------]
4237 4368   * snapshots                       ---O-------O--------O-------O--------O
4238 4369   *                                        firstsnap        lastsnap
4239 4370   *
4240 4371   * This is the set of blocks that were born after the snap before firstsnap,
4241 4372   * (birth > firstsnap->prev_snap_txg) and died before the snap after the
4242 4373   * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
4243 4374   * We calculate this by iterating over the relevant deadlists (from the snap
4244 4375   * after lastsnap, backward to the snap after firstsnap), summing up the
4245 4376   * space on the deadlist that was born after the snap before firstsnap.
4246 4377   */
4247 4378  int
4248 4379  dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
4249 4380      dsl_dataset_t *lastsnap,
4250 4381      uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4251 4382  {
4252 4383          int err = 0;
4253 4384          uint64_t snapobj;
4254 4385          dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
4255 4386  
4256 4387          ASSERT(dsl_dataset_is_snapshot(firstsnap));
4257 4388          ASSERT(dsl_dataset_is_snapshot(lastsnap));
4258 4389  
4259 4390          /*
4260 4391           * Check that the snapshots are in the same dsl_dir, and firstsnap
4261 4392           * is before lastsnap.
4262 4393           */
4263 4394          if (firstsnap->ds_dir != lastsnap->ds_dir ||
4264 4395              firstsnap->ds_phys->ds_creation_txg >
4265 4396              lastsnap->ds_phys->ds_creation_txg)
4266 4397                  return (EINVAL);
4267 4398  
4268 4399          *usedp = *compp = *uncompp = 0;
4269 4400  
4270 4401          rw_enter(&dp->dp_config_rwlock, RW_READER);
4271 4402          snapobj = lastsnap->ds_phys->ds_next_snap_obj;
4272 4403          while (snapobj != firstsnap->ds_object) {
4273 4404                  dsl_dataset_t *ds;
4274 4405                  uint64_t used, comp, uncomp;
4275 4406  
4276 4407                  err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
4277 4408                  if (err != 0)
4278 4409                          break;
4279 4410  
4280 4411                  dsl_deadlist_space_range(&ds->ds_deadlist,
4281 4412                      firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
4282 4413                      &used, &comp, &uncomp);
4283 4414                  *usedp += used;
4284 4415                  *compp += comp;
4285 4416                  *uncompp += uncomp;
4286 4417  
4287 4418                  snapobj = ds->ds_phys->ds_prev_snap_obj;
4288 4419                  ASSERT3U(snapobj, !=, 0);
4289 4420                  dsl_dataset_rele(ds, FTAG);
4290 4421          }
4291 4422          rw_exit(&dp->dp_config_rwlock);
4292 4423          return (err);
4293 4424  }
  
    | ↓ open down ↓ | 1191 lines elided | ↑ open up ↑ | 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX