Print this page
    
10592 misc. metaslab and vdev related ZoL bug fixes
Portions contributed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed by: Giuseppe Di Natale <guss80@gmail.com>
Reviewed by: George Melikov <mail@gmelikov.ru>
Reviewed by: Paul Dagnelie <pcd@delphix.com>
Reviewed by: Matt Ahrens <mahrens@delphix.com>
Reviewed by: Pavel Zakharov <pavel.zakharov@delphix.com>
Reviewed by: Tony Hutter <hutter2@llnl.gov>
Reviewed by: Kody Kantor <kody.kantor@joyent.com>
Approved by: Dan McDonald <danmcd@joyent.com>
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/zfs/vdev_removal.c
          +++ new/usr/src/uts/common/fs/zfs/vdev_removal.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
  25   25   */
  26   26  
  27   27  #include <sys/zfs_context.h>
  28   28  #include <sys/spa_impl.h>
  29   29  #include <sys/dmu.h>
  30   30  #include <sys/dmu_tx.h>
  31   31  #include <sys/zap.h>
  32   32  #include <sys/vdev_impl.h>
  33   33  #include <sys/metaslab.h>
  34   34  #include <sys/metaslab_impl.h>
  35   35  #include <sys/uberblock_impl.h>
  36   36  #include <sys/txg.h>
  37   37  #include <sys/avl.h>
  38   38  #include <sys/bpobj.h>
  39   39  #include <sys/dsl_pool.h>
  40   40  #include <sys/dsl_synctask.h>
  41   41  #include <sys/dsl_dir.h>
  42   42  #include <sys/arc.h>
  43   43  #include <sys/zfeature.h>
  44   44  #include <sys/vdev_indirect_births.h>
  45   45  #include <sys/vdev_indirect_mapping.h>
  46   46  #include <sys/abd.h>
  47   47  #include <sys/vdev_initialize.h>
  48   48  
  49   49  /*
  50   50   * This file contains the necessary logic to remove vdevs from a
  51   51   * storage pool.  Currently, the only devices that can be removed
  52   52   * are log, cache, and spare devices; and top level vdevs from a pool
  53   53   * w/o raidz.  (Note that members of a mirror can also be removed
  54   54   * by the detach operation.)
  55   55   *
  56   56   * Log vdevs are removed by evacuating them and then turning the vdev
  57   57   * into a hole vdev while holding spa config locks.
  58   58   *
  59   59   * Top level vdevs are removed and converted into an indirect vdev via
  60   60   * a multi-step process:
  61   61   *
  62   62   *  - Disable allocations from this device (spa_vdev_remove_top).
  63   63   *
  64   64   *  - From a new thread (spa_vdev_remove_thread), copy data from
  65   65   *    the removing vdev to a different vdev.  The copy happens in open
  66   66   *    context (spa_vdev_copy_impl) and issues a sync task
  67   67   *    (vdev_mapping_sync) so the sync thread can update the partial
  68   68   *    indirect mappings in core and on disk.
  69   69   *
  70   70   *  - If a free happens during a removal, it is freed from the
  71   71   *    removing vdev, and if it has already been copied, from the new
  72   72   *    location as well (free_from_removing_vdev).
  73   73   *
  74   74   *  - After the removal is completed, the copy thread converts the vdev
  75   75   *    into an indirect vdev (vdev_remove_complete) before instructing
  76   76   *    the sync thread to destroy the space maps and finish the removal
  77   77   *    (spa_finish_removal).
  78   78   */
  79   79  
  80   80  typedef struct vdev_copy_arg {
  81   81          metaslab_t      *vca_msp;
  82   82          uint64_t        vca_outstanding_bytes;
  83   83          kcondvar_t      vca_cv;
  84   84          kmutex_t        vca_lock;
  85   85  } vdev_copy_arg_t;
  86   86  
  87   87  /*
  88   88   * The maximum amount of memory we can use for outstanding i/o while
  89   89   * doing a device removal.  This determines how much i/o we can have
  90   90   * in flight concurrently.
  91   91   */
  92   92  int zfs_remove_max_copy_bytes = 64 * 1024 * 1024;
  93   93  
  94   94  /*
  95   95   * The largest contiguous segment that we will attempt to allocate when
  96   96   * removing a device.  This can be no larger than SPA_MAXBLOCKSIZE.  If
  97   97   * there is a performance problem with attempting to allocate large blocks,
  98   98   * consider decreasing this.
  99   99   *
 100  100   * Note: we will issue I/Os of up to this size.  The mpt driver does not
 101  101   * respond well to I/Os larger than 1MB, so we set this to 1MB.  (When
 102  102   * mpt processes an I/O larger than 1MB, it needs to do an allocation of
 103  103   * 2 physically contiguous pages; if this allocation fails, mpt will drop
 104  104   * the I/O and hang the device.)
 105  105   */
 106  106  int zfs_remove_max_segment = 1024 * 1024;
 107  107  
 108  108  /*
 109  109   * Allow a remap segment to span free chunks of at most this size. The main
 110  110   * impact of a larger span is that we will read and write larger, more
 111  111   * contiguous chunks, with more "unnecessary" data -- trading off bandwidth
 112  112   * for iops.  The value here was chosen to align with
 113  113   * zfs_vdev_read_gap_limit, which is a similar concept when doing regular
 114  114   * reads (but there's no reason it has to be the same).
 115  115   *
 116  116   * Additionally, a higher span will have the following relatively minor
 117  117   * effects:
 118  118   *  - the mapping will be smaller, since one entry can cover more allocated
 119  119   *    segments
 120  120   *  - more of the fragmentation in the removing device will be preserved
 121  121   *  - we'll do larger allocations, which may fail and fall back on smaller
 122  122   *    allocations
 123  123   */
 124  124  int vdev_removal_max_span = 32 * 1024;
 125  125  
 126  126  /*
 127  127   * This is used by the test suite so that it can ensure that certain
 128  128   * actions happen while in the middle of a removal.
 129  129   */
 130  130  uint64_t zfs_remove_max_bytes_pause = UINT64_MAX;
 131  131  
 132  132  #define VDEV_REMOVAL_ZAP_OBJS   "lzap"
 133  133  
 134  134  static void spa_vdev_remove_thread(void *arg);
 135  135  
 136  136  static void
 137  137  spa_sync_removing_state(spa_t *spa, dmu_tx_t *tx)
 138  138  {
 139  139          VERIFY0(zap_update(spa->spa_dsl_pool->dp_meta_objset,
 140  140              DMU_POOL_DIRECTORY_OBJECT,
 141  141              DMU_POOL_REMOVING, sizeof (uint64_t),
 142  142              sizeof (spa->spa_removing_phys) / sizeof (uint64_t),
 143  143              &spa->spa_removing_phys, tx));
 144  144  }
 145  145  
 146  146  static nvlist_t *
 147  147  spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
 148  148  {
 149  149          for (int i = 0; i < count; i++) {
 150  150                  uint64_t guid =
 151  151                      fnvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID);
 152  152  
 153  153                  if (guid == target_guid)
 154  154                          return (nvpp[i]);
 155  155          }
 156  156  
 157  157          return (NULL);
 158  158  }
 159  159  
 160  160  static void
 161  161  spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
 162  162      nvlist_t *dev_to_remove)
 163  163  {
 164  164          nvlist_t **newdev = NULL;
 165  165  
 166  166          if (count > 1)
 167  167                  newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
 168  168  
 169  169          for (int i = 0, j = 0; i < count; i++) {
 170  170                  if (dev[i] == dev_to_remove)
 171  171                          continue;
 172  172                  VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
 173  173          }
 174  174  
 175  175          VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
 176  176          VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
 177  177  
 178  178          for (int i = 0; i < count - 1; i++)
 179  179                  nvlist_free(newdev[i]);
 180  180  
 181  181          if (count > 1)
 182  182                  kmem_free(newdev, (count - 1) * sizeof (void *));
 183  183  }
 184  184  
 185  185  static spa_vdev_removal_t *
 186  186  spa_vdev_removal_create(vdev_t *vd)
 187  187  {
 188  188          spa_vdev_removal_t *svr = kmem_zalloc(sizeof (*svr), KM_SLEEP);
 189  189          mutex_init(&svr->svr_lock, NULL, MUTEX_DEFAULT, NULL);
 190  190          cv_init(&svr->svr_cv, NULL, CV_DEFAULT, NULL);
 191  191          svr->svr_allocd_segs = range_tree_create(NULL, NULL);
 192  192          svr->svr_vdev_id = vd->vdev_id;
 193  193  
 194  194          for (int i = 0; i < TXG_SIZE; i++) {
 195  195                  svr->svr_frees[i] = range_tree_create(NULL, NULL);
 196  196                  list_create(&svr->svr_new_segments[i],
 197  197                      sizeof (vdev_indirect_mapping_entry_t),
 198  198                      offsetof(vdev_indirect_mapping_entry_t, vime_node));
 199  199          }
 200  200  
 201  201          return (svr);
 202  202  }
 203  203  
 204  204  void
 205  205  spa_vdev_removal_destroy(spa_vdev_removal_t *svr)
 206  206  {
 207  207          for (int i = 0; i < TXG_SIZE; i++) {
 208  208                  ASSERT0(svr->svr_bytes_done[i]);
 209  209                  ASSERT0(svr->svr_max_offset_to_sync[i]);
 210  210                  range_tree_destroy(svr->svr_frees[i]);
 211  211                  list_destroy(&svr->svr_new_segments[i]);
 212  212          }
 213  213  
 214  214          range_tree_destroy(svr->svr_allocd_segs);
 215  215          mutex_destroy(&svr->svr_lock);
 216  216          cv_destroy(&svr->svr_cv);
 217  217          kmem_free(svr, sizeof (*svr));
 218  218  }
 219  219  
 220  220  /*
 221  221   * This is called as a synctask in the txg in which we will mark this vdev
 222  222   * as removing (in the config stored in the MOS).
 223  223   *
 224  224   * It begins the evacuation of a toplevel vdev by:
 225  225   * - initializing the spa_removing_phys which tracks this removal
 226  226   * - computing the amount of space to remove for accounting purposes
 227  227   * - dirtying all dbufs in the spa_config_object
 228  228   * - creating the spa_vdev_removal
 229  229   * - starting the spa_vdev_remove_thread
 230  230   */
 231  231  static void
 232  232  vdev_remove_initiate_sync(void *arg, dmu_tx_t *tx)
 233  233  {
 234  234          int vdev_id = (uintptr_t)arg;
 235  235          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
 236  236          vdev_t *vd = vdev_lookup_top(spa, vdev_id);
 237  237          vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
 238  238          objset_t *mos = spa->spa_dsl_pool->dp_meta_objset;
 239  239          spa_vdev_removal_t *svr = NULL;
 240  240          uint64_t txg = dmu_tx_get_txg(tx);
 241  241  
 242  242          ASSERT3P(vd->vdev_ops, !=, &vdev_raidz_ops);
 243  243          svr = spa_vdev_removal_create(vd);
 244  244  
 245  245          ASSERT(vd->vdev_removing);
 246  246          ASSERT3P(vd->vdev_indirect_mapping, ==, NULL);
 247  247  
 248  248          spa_feature_incr(spa, SPA_FEATURE_DEVICE_REMOVAL, tx);
 249  249          if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
 250  250                  /*
 251  251                   * By activating the OBSOLETE_COUNTS feature, we prevent
 252  252                   * the pool from being downgraded and ensure that the
 253  253                   * refcounts are precise.
 254  254                   */
 255  255                  spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
 256  256                  uint64_t one = 1;
 257  257                  VERIFY0(zap_add(spa->spa_meta_objset, vd->vdev_top_zap,
 258  258                      VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, sizeof (one), 1,
 259  259                      &one, tx));
 260  260                  ASSERT3U(vdev_obsolete_counts_are_precise(vd), !=, 0);
 261  261          }
 262  262  
 263  263          vic->vic_mapping_object = vdev_indirect_mapping_alloc(mos, tx);
 264  264          vd->vdev_indirect_mapping =
 265  265              vdev_indirect_mapping_open(mos, vic->vic_mapping_object);
 266  266          vic->vic_births_object = vdev_indirect_births_alloc(mos, tx);
 267  267          vd->vdev_indirect_births =
 268  268              vdev_indirect_births_open(mos, vic->vic_births_object);
 269  269          spa->spa_removing_phys.sr_removing_vdev = vd->vdev_id;
 270  270          spa->spa_removing_phys.sr_start_time = gethrestime_sec();
 271  271          spa->spa_removing_phys.sr_end_time = 0;
 272  272          spa->spa_removing_phys.sr_state = DSS_SCANNING;
 273  273          spa->spa_removing_phys.sr_to_copy = 0;
 274  274          spa->spa_removing_phys.sr_copied = 0;
 275  275  
  
    | 
      ↓ open down ↓ | 
    275 lines elided | 
    
      ↑ open up ↑ | 
  
 276  276          /*
 277  277           * Note: We can't use vdev_stat's vs_alloc for sr_to_copy, because
 278  278           * there may be space in the defer tree, which is free, but still
 279  279           * counted in vs_alloc.
 280  280           */
 281  281          for (uint64_t i = 0; i < vd->vdev_ms_count; i++) {
 282  282                  metaslab_t *ms = vd->vdev_ms[i];
 283  283                  if (ms->ms_sm == NULL)
 284  284                          continue;
 285  285  
 286      -                /*
 287      -                 * Sync tasks happen before metaslab_sync(), therefore
 288      -                 * smp_alloc and sm_alloc must be the same.
 289      -                 */
 290      -                ASSERT3U(space_map_allocated(ms->ms_sm), ==,
 291      -                    ms->ms_sm->sm_phys->smp_alloc);
 292      -
 293  286                  spa->spa_removing_phys.sr_to_copy +=
 294      -                    space_map_allocated(ms->ms_sm);
      287 +                    metaslab_allocated_space(ms);
 295  288  
 296  289                  /*
 297  290                   * Space which we are freeing this txg does not need to
 298  291                   * be copied.
 299  292                   */
 300  293                  spa->spa_removing_phys.sr_to_copy -=
 301  294                      range_tree_space(ms->ms_freeing);
 302  295  
 303  296                  ASSERT0(range_tree_space(ms->ms_freed));
 304  297                  for (int t = 0; t < TXG_SIZE; t++)
 305  298                          ASSERT0(range_tree_space(ms->ms_allocating[t]));
 306  299          }
 307  300  
 308  301          /*
 309  302           * Sync tasks are called before metaslab_sync(), so there should
 310  303           * be no already-synced metaslabs in the TXG_CLEAN list.
 311  304           */
 312  305          ASSERT3P(txg_list_head(&vd->vdev_ms_list, TXG_CLEAN(txg)), ==, NULL);
 313  306  
 314  307          spa_sync_removing_state(spa, tx);
 315  308  
 316  309          /*
 317  310           * All blocks that we need to read the most recent mapping must be
 318  311           * stored on concrete vdevs.  Therefore, we must dirty anything that
 319  312           * is read before spa_remove_init().  Specifically, the
 320  313           * spa_config_object.  (Note that although we already modified the
 321  314           * spa_config_object in spa_sync_removing_state, that may not have
 322  315           * modified all blocks of the object.)
 323  316           */
 324  317          dmu_object_info_t doi;
 325  318          VERIFY0(dmu_object_info(mos, DMU_POOL_DIRECTORY_OBJECT, &doi));
 326  319          for (uint64_t offset = 0; offset < doi.doi_max_offset; ) {
 327  320                  dmu_buf_t *dbuf;
 328  321                  VERIFY0(dmu_buf_hold(mos, DMU_POOL_DIRECTORY_OBJECT,
 329  322                      offset, FTAG, &dbuf, 0));
 330  323                  dmu_buf_will_dirty(dbuf, tx);
 331  324                  offset += dbuf->db_size;
 332  325                  dmu_buf_rele(dbuf, FTAG);
 333  326          }
 334  327  
 335  328          /*
 336  329           * Now that we've allocated the im_object, dirty the vdev to ensure
 337  330           * that the object gets written to the config on disk.
 338  331           */
 339  332          vdev_config_dirty(vd);
 340  333  
 341  334          zfs_dbgmsg("starting removal thread for vdev %llu (%p) in txg %llu "
 342  335              "im_obj=%llu", vd->vdev_id, vd, dmu_tx_get_txg(tx),
 343  336              vic->vic_mapping_object);
 344  337  
 345  338          spa_history_log_internal(spa, "vdev remove started", tx,
 346  339              "%s vdev %llu %s", spa_name(spa), vd->vdev_id,
 347  340              (vd->vdev_path != NULL) ? vd->vdev_path : "-");
 348  341          /*
 349  342           * Setting spa_vdev_removal causes subsequent frees to call
 350  343           * free_from_removing_vdev().  Note that we don't need any locking
 351  344           * because we are the sync thread, and metaslab_free_impl() is only
 352  345           * called from syncing context (potentially from a zio taskq thread,
 353  346           * but in any case only when there are outstanding free i/os, which
 354  347           * there are not).
 355  348           */
 356  349          ASSERT3P(spa->spa_vdev_removal, ==, NULL);
 357  350          spa->spa_vdev_removal = svr;
 358  351          svr->svr_thread = thread_create(NULL, 0,
 359  352              spa_vdev_remove_thread, spa, 0, &p0, TS_RUN, minclsyspri);
 360  353  }
 361  354  
 362  355  /*
 363  356   * When we are opening a pool, we must read the mapping for each
 364  357   * indirect vdev in order from most recently removed to least
 365  358   * recently removed.  We do this because the blocks for the mapping
 366  359   * of older indirect vdevs may be stored on more recently removed vdevs.
 367  360   * In order to read each indirect mapping object, we must have
 368  361   * initialized all more recently removed vdevs.
 369  362   */
 370  363  int
 371  364  spa_remove_init(spa_t *spa)
 372  365  {
 373  366          int error;
 374  367  
 375  368          error = zap_lookup(spa->spa_dsl_pool->dp_meta_objset,
 376  369              DMU_POOL_DIRECTORY_OBJECT,
 377  370              DMU_POOL_REMOVING, sizeof (uint64_t),
 378  371              sizeof (spa->spa_removing_phys) / sizeof (uint64_t),
 379  372              &spa->spa_removing_phys);
 380  373  
 381  374          if (error == ENOENT) {
 382  375                  spa->spa_removing_phys.sr_state = DSS_NONE;
 383  376                  spa->spa_removing_phys.sr_removing_vdev = -1;
 384  377                  spa->spa_removing_phys.sr_prev_indirect_vdev = -1;
 385  378                  spa->spa_indirect_vdevs_loaded = B_TRUE;
 386  379                  return (0);
 387  380          } else if (error != 0) {
 388  381                  return (error);
 389  382          }
 390  383  
 391  384          if (spa->spa_removing_phys.sr_state == DSS_SCANNING) {
 392  385                  /*
 393  386                   * We are currently removing a vdev.  Create and
 394  387                   * initialize a spa_vdev_removal_t from the bonus
 395  388                   * buffer of the removing vdevs vdev_im_object, and
 396  389                   * initialize its partial mapping.
 397  390                   */
 398  391                  spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
 399  392                  vdev_t *vd = vdev_lookup_top(spa,
 400  393                      spa->spa_removing_phys.sr_removing_vdev);
 401  394  
 402  395                  if (vd == NULL) {
 403  396                          spa_config_exit(spa, SCL_STATE, FTAG);
 404  397                          return (EINVAL);
 405  398                  }
 406  399  
 407  400                  vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
 408  401  
 409  402                  ASSERT(vdev_is_concrete(vd));
 410  403                  spa_vdev_removal_t *svr = spa_vdev_removal_create(vd);
 411  404                  ASSERT3U(svr->svr_vdev_id, ==, vd->vdev_id);
 412  405                  ASSERT(vd->vdev_removing);
 413  406  
 414  407                  vd->vdev_indirect_mapping = vdev_indirect_mapping_open(
 415  408                      spa->spa_meta_objset, vic->vic_mapping_object);
 416  409                  vd->vdev_indirect_births = vdev_indirect_births_open(
 417  410                      spa->spa_meta_objset, vic->vic_births_object);
 418  411                  spa_config_exit(spa, SCL_STATE, FTAG);
 419  412  
 420  413                  spa->spa_vdev_removal = svr;
 421  414          }
 422  415  
 423  416          spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
 424  417          uint64_t indirect_vdev_id =
 425  418              spa->spa_removing_phys.sr_prev_indirect_vdev;
 426  419          while (indirect_vdev_id != UINT64_MAX) {
 427  420                  vdev_t *vd = vdev_lookup_top(spa, indirect_vdev_id);
 428  421                  vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
 429  422  
 430  423                  ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
 431  424                  vd->vdev_indirect_mapping = vdev_indirect_mapping_open(
 432  425                      spa->spa_meta_objset, vic->vic_mapping_object);
 433  426                  vd->vdev_indirect_births = vdev_indirect_births_open(
 434  427                      spa->spa_meta_objset, vic->vic_births_object);
 435  428  
 436  429                  indirect_vdev_id = vic->vic_prev_indirect_vdev;
 437  430          }
 438  431          spa_config_exit(spa, SCL_STATE, FTAG);
 439  432  
 440  433          /*
 441  434           * Now that we've loaded all the indirect mappings, we can allow
 442  435           * reads from other blocks (e.g. via predictive prefetch).
 443  436           */
 444  437          spa->spa_indirect_vdevs_loaded = B_TRUE;
 445  438          return (0);
 446  439  }
 447  440  
 448  441  void
 449  442  spa_restart_removal(spa_t *spa)
 450  443  {
 451  444          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
 452  445  
 453  446          if (svr == NULL)
 454  447                  return;
 455  448  
 456  449          /*
 457  450           * In general when this function is called there is no
 458  451           * removal thread running. The only scenario where this
 459  452           * is not true is during spa_import() where this function
 460  453           * is called twice [once from spa_import_impl() and
 461  454           * spa_async_resume()]. Thus, in the scenario where we
 462  455           * import a pool that has an ongoing removal we don't
 463  456           * want to spawn a second thread.
 464  457           */
 465  458          if (svr->svr_thread != NULL)
 466  459                  return;
 467  460  
 468  461          if (!spa_writeable(spa))
 469  462                  return;
 470  463  
 471  464          zfs_dbgmsg("restarting removal of %llu", svr->svr_vdev_id);
 472  465          svr->svr_thread = thread_create(NULL, 0, spa_vdev_remove_thread, spa,
 473  466              0, &p0, TS_RUN, minclsyspri);
 474  467  }
 475  468  
 476  469  /*
 477  470   * Process freeing from a device which is in the middle of being removed.
 478  471   * We must handle this carefully so that we attempt to copy freed data,
 479  472   * and we correctly free already-copied data.
 480  473   */
 481  474  void
 482  475  free_from_removing_vdev(vdev_t *vd, uint64_t offset, uint64_t size)
 483  476  {
 484  477          spa_t *spa = vd->vdev_spa;
 485  478          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
 486  479          vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
 487  480          uint64_t txg = spa_syncing_txg(spa);
 488  481          uint64_t max_offset_yet = 0;
 489  482  
 490  483          ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
 491  484          ASSERT3U(vd->vdev_indirect_config.vic_mapping_object, ==,
 492  485              vdev_indirect_mapping_object(vim));
 493  486          ASSERT3U(vd->vdev_id, ==, svr->svr_vdev_id);
 494  487  
 495  488          mutex_enter(&svr->svr_lock);
 496  489  
 497  490          /*
 498  491           * Remove the segment from the removing vdev's spacemap.  This
 499  492           * ensures that we will not attempt to copy this space (if the
 500  493           * removal thread has not yet visited it), and also ensures
 501  494           * that we know what is actually allocated on the new vdevs
 502  495           * (needed if we cancel the removal).
 503  496           *
 504  497           * Note: we must do the metaslab_free_concrete() with the svr_lock
 505  498           * held, so that the remove_thread can not load this metaslab and then
 506  499           * visit this offset between the time that we metaslab_free_concrete()
 507  500           * and when we check to see if it has been visited.
 508  501           *
 509  502           * Note: The checkpoint flag is set to false as having/taking
 510  503           * a checkpoint and removing a device can't happen at the same
 511  504           * time.
 512  505           */
 513  506          ASSERT(!spa_has_checkpoint(spa));
 514  507          metaslab_free_concrete(vd, offset, size, B_FALSE);
 515  508  
 516  509          uint64_t synced_size = 0;
 517  510          uint64_t synced_offset = 0;
 518  511          uint64_t max_offset_synced = vdev_indirect_mapping_max_offset(vim);
 519  512          if (offset < max_offset_synced) {
 520  513                  /*
 521  514                   * The mapping for this offset is already on disk.
 522  515                   * Free from the new location.
 523  516                   *
 524  517                   * Note that we use svr_max_synced_offset because it is
 525  518                   * updated atomically with respect to the in-core mapping.
 526  519                   * By contrast, vim_max_offset is not.
 527  520                   *
 528  521                   * This block may be split between a synced entry and an
 529  522                   * in-flight or unvisited entry.  Only process the synced
 530  523                   * portion of it here.
 531  524                   */
 532  525                  synced_size = MIN(size, max_offset_synced - offset);
 533  526                  synced_offset = offset;
 534  527  
 535  528                  ASSERT3U(max_offset_yet, <=, max_offset_synced);
 536  529                  max_offset_yet = max_offset_synced;
 537  530  
 538  531                  DTRACE_PROBE3(remove__free__synced,
 539  532                      spa_t *, spa,
 540  533                      uint64_t, offset,
 541  534                      uint64_t, synced_size);
 542  535  
 543  536                  size -= synced_size;
 544  537                  offset += synced_size;
 545  538          }
 546  539  
 547  540          /*
 548  541           * Look at all in-flight txgs starting from the currently syncing one
 549  542           * and see if a section of this free is being copied. By starting from
 550  543           * this txg and iterating forward, we might find that this region
 551  544           * was copied in two different txgs and handle it appropriately.
 552  545           */
 553  546          for (int i = 0; i < TXG_CONCURRENT_STATES; i++) {
 554  547                  int txgoff = (txg + i) & TXG_MASK;
 555  548                  if (size > 0 && offset < svr->svr_max_offset_to_sync[txgoff]) {
 556  549                          /*
 557  550                           * The mapping for this offset is in flight, and
 558  551                           * will be synced in txg+i.
 559  552                           */
 560  553                          uint64_t inflight_size = MIN(size,
 561  554                              svr->svr_max_offset_to_sync[txgoff] - offset);
 562  555  
 563  556                          DTRACE_PROBE4(remove__free__inflight,
 564  557                              spa_t *, spa,
 565  558                              uint64_t, offset,
 566  559                              uint64_t, inflight_size,
 567  560                              uint64_t, txg + i);
 568  561  
 569  562                          /*
 570  563                           * We copy data in order of increasing offset.
 571  564                           * Therefore the max_offset_to_sync[] must increase
 572  565                           * (or be zero, indicating that nothing is being
 573  566                           * copied in that txg).
 574  567                           */
 575  568                          if (svr->svr_max_offset_to_sync[txgoff] != 0) {
 576  569                                  ASSERT3U(svr->svr_max_offset_to_sync[txgoff],
 577  570                                      >=, max_offset_yet);
 578  571                                  max_offset_yet =
 579  572                                      svr->svr_max_offset_to_sync[txgoff];
 580  573                          }
 581  574  
 582  575                          /*
 583  576                           * We've already committed to copying this segment:
 584  577                           * we have allocated space elsewhere in the pool for
 585  578                           * it and have an IO outstanding to copy the data. We
 586  579                           * cannot free the space before the copy has
 587  580                           * completed, or else the copy IO might overwrite any
 588  581                           * new data. To free that space, we record the
 589  582                           * segment in the appropriate svr_frees tree and free
 590  583                           * the mapped space later, in the txg where we have
 591  584                           * completed the copy and synced the mapping (see
 592  585                           * vdev_mapping_sync).
 593  586                           */
 594  587                          range_tree_add(svr->svr_frees[txgoff],
 595  588                              offset, inflight_size);
 596  589                          size -= inflight_size;
 597  590                          offset += inflight_size;
 598  591  
 599  592                          /*
 600  593                           * This space is already accounted for as being
 601  594                           * done, because it is being copied in txg+i.
 602  595                           * However, if i!=0, then it is being copied in
 603  596                           * a future txg.  If we crash after this txg
 604  597                           * syncs but before txg+i syncs, then the space
 605  598                           * will be free.  Therefore we must account
 606  599                           * for the space being done in *this* txg
 607  600                           * (when it is freed) rather than the future txg
 608  601                           * (when it will be copied).
 609  602                           */
 610  603                          ASSERT3U(svr->svr_bytes_done[txgoff], >=,
 611  604                              inflight_size);
 612  605                          svr->svr_bytes_done[txgoff] -= inflight_size;
 613  606                          svr->svr_bytes_done[txg & TXG_MASK] += inflight_size;
 614  607                  }
 615  608          }
 616  609          ASSERT0(svr->svr_max_offset_to_sync[TXG_CLEAN(txg) & TXG_MASK]);
 617  610  
 618  611          if (size > 0) {
 619  612                  /*
 620  613                   * The copy thread has not yet visited this offset.  Ensure
 621  614                   * that it doesn't.
 622  615                   */
 623  616  
 624  617                  DTRACE_PROBE3(remove__free__unvisited,
 625  618                      spa_t *, spa,
 626  619                      uint64_t, offset,
 627  620                      uint64_t, size);
 628  621  
 629  622                  if (svr->svr_allocd_segs != NULL)
 630  623                          range_tree_clear(svr->svr_allocd_segs, offset, size);
 631  624  
 632  625                  /*
 633  626                   * Since we now do not need to copy this data, for
 634  627                   * accounting purposes we have done our job and can count
 635  628                   * it as completed.
 636  629                   */
 637  630                  svr->svr_bytes_done[txg & TXG_MASK] += size;
 638  631          }
 639  632          mutex_exit(&svr->svr_lock);
 640  633  
 641  634          /*
 642  635           * Now that we have dropped svr_lock, process the synced portion
 643  636           * of this free.
 644  637           */
 645  638          if (synced_size > 0) {
 646  639                  vdev_indirect_mark_obsolete(vd, synced_offset, synced_size);
 647  640  
 648  641                  /*
 649  642                   * Note: this can only be called from syncing context,
 650  643                   * and the vdev_indirect_mapping is only changed from the
 651  644                   * sync thread, so we don't need svr_lock while doing
 652  645                   * metaslab_free_impl_cb.
 653  646                   */
 654  647                  boolean_t checkpoint = B_FALSE;
 655  648                  vdev_indirect_ops.vdev_op_remap(vd, synced_offset, synced_size,
 656  649                      metaslab_free_impl_cb, &checkpoint);
 657  650          }
 658  651  }
 659  652  
 660  653  /*
 661  654   * Stop an active removal and update the spa_removing phys.
 662  655   */
 663  656  static void
 664  657  spa_finish_removal(spa_t *spa, dsl_scan_state_t state, dmu_tx_t *tx)
 665  658  {
 666  659          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
 667  660          ASSERT3U(dmu_tx_get_txg(tx), ==, spa_syncing_txg(spa));
 668  661  
 669  662          /* Ensure the removal thread has completed before we free the svr. */
 670  663          spa_vdev_remove_suspend(spa);
 671  664  
 672  665          ASSERT(state == DSS_FINISHED || state == DSS_CANCELED);
 673  666  
 674  667          if (state == DSS_FINISHED) {
 675  668                  spa_removing_phys_t *srp = &spa->spa_removing_phys;
 676  669                  vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
 677  670                  vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
 678  671  
 679  672                  if (srp->sr_prev_indirect_vdev != UINT64_MAX) {
 680  673                          vdev_t *pvd = vdev_lookup_top(spa,
 681  674                              srp->sr_prev_indirect_vdev);
 682  675                          ASSERT3P(pvd->vdev_ops, ==, &vdev_indirect_ops);
 683  676                  }
 684  677  
 685  678                  vic->vic_prev_indirect_vdev = srp->sr_prev_indirect_vdev;
 686  679                  srp->sr_prev_indirect_vdev = vd->vdev_id;
 687  680          }
 688  681          spa->spa_removing_phys.sr_state = state;
 689  682          spa->spa_removing_phys.sr_end_time = gethrestime_sec();
 690  683  
 691  684          spa->spa_vdev_removal = NULL;
 692  685          spa_vdev_removal_destroy(svr);
 693  686  
 694  687          spa_sync_removing_state(spa, tx);
 695  688  
 696  689          vdev_config_dirty(spa->spa_root_vdev);
 697  690  }
 698  691  
 699  692  static void
 700  693  free_mapped_segment_cb(void *arg, uint64_t offset, uint64_t size)
 701  694  {
 702  695          vdev_t *vd = arg;
 703  696          vdev_indirect_mark_obsolete(vd, offset, size);
 704  697          boolean_t checkpoint = B_FALSE;
 705  698          vdev_indirect_ops.vdev_op_remap(vd, offset, size,
 706  699              metaslab_free_impl_cb, &checkpoint);
 707  700  }
 708  701  
 709  702  /*
 710  703   * On behalf of the removal thread, syncs an incremental bit more of
 711  704   * the indirect mapping to disk and updates the in-memory mapping.
 712  705   * Called as a sync task in every txg that the removal thread makes progress.
 713  706   */
 714  707  static void
 715  708  vdev_mapping_sync(void *arg, dmu_tx_t *tx)
 716  709  {
 717  710          spa_vdev_removal_t *svr = arg;
 718  711          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
 719  712          vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
 720  713          vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
 721  714          uint64_t txg = dmu_tx_get_txg(tx);
 722  715          vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
 723  716  
 724  717          ASSERT(vic->vic_mapping_object != 0);
 725  718          ASSERT3U(txg, ==, spa_syncing_txg(spa));
 726  719  
 727  720          vdev_indirect_mapping_add_entries(vim,
 728  721              &svr->svr_new_segments[txg & TXG_MASK], tx);
 729  722          vdev_indirect_births_add_entry(vd->vdev_indirect_births,
 730  723              vdev_indirect_mapping_max_offset(vim), dmu_tx_get_txg(tx), tx);
 731  724  
 732  725          /*
 733  726           * Free the copied data for anything that was freed while the
 734  727           * mapping entries were in flight.
 735  728           */
 736  729          mutex_enter(&svr->svr_lock);
 737  730          range_tree_vacate(svr->svr_frees[txg & TXG_MASK],
 738  731              free_mapped_segment_cb, vd);
 739  732          ASSERT3U(svr->svr_max_offset_to_sync[txg & TXG_MASK], >=,
 740  733              vdev_indirect_mapping_max_offset(vim));
 741  734          svr->svr_max_offset_to_sync[txg & TXG_MASK] = 0;
 742  735          mutex_exit(&svr->svr_lock);
 743  736  
 744  737          spa_sync_removing_state(spa, tx);
 745  738  }
 746  739  
 747  740  typedef struct vdev_copy_segment_arg {
 748  741          spa_t *vcsa_spa;
 749  742          dva_t *vcsa_dest_dva;
 750  743          uint64_t vcsa_txg;
 751  744          range_tree_t *vcsa_obsolete_segs;
 752  745  } vdev_copy_segment_arg_t;
 753  746  
 754  747  static void
 755  748  unalloc_seg(void *arg, uint64_t start, uint64_t size)
 756  749  {
 757  750          vdev_copy_segment_arg_t *vcsa = arg;
 758  751          spa_t *spa = vcsa->vcsa_spa;
 759  752          blkptr_t bp = { 0 };
 760  753  
 761  754          BP_SET_BIRTH(&bp, TXG_INITIAL, TXG_INITIAL);
 762  755          BP_SET_LSIZE(&bp, size);
 763  756          BP_SET_PSIZE(&bp, size);
 764  757          BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
 765  758          BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_OFF);
 766  759          BP_SET_TYPE(&bp, DMU_OT_NONE);
 767  760          BP_SET_LEVEL(&bp, 0);
 768  761          BP_SET_DEDUP(&bp, 0);
 769  762          BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
 770  763  
 771  764          DVA_SET_VDEV(&bp.blk_dva[0], DVA_GET_VDEV(vcsa->vcsa_dest_dva));
 772  765          DVA_SET_OFFSET(&bp.blk_dva[0],
 773  766              DVA_GET_OFFSET(vcsa->vcsa_dest_dva) + start);
 774  767          DVA_SET_ASIZE(&bp.blk_dva[0], size);
 775  768  
 776  769          zio_free(spa, vcsa->vcsa_txg, &bp);
 777  770  }
 778  771  
 779  772  /*
 780  773   * All reads and writes associated with a call to spa_vdev_copy_segment()
 781  774   * are done.
 782  775   */
 783  776  static void
 784  777  spa_vdev_copy_segment_done(zio_t *zio)
 785  778  {
 786  779          vdev_copy_segment_arg_t *vcsa = zio->io_private;
 787  780  
 788  781          range_tree_vacate(vcsa->vcsa_obsolete_segs,
 789  782              unalloc_seg, vcsa);
 790  783          range_tree_destroy(vcsa->vcsa_obsolete_segs);
 791  784          kmem_free(vcsa, sizeof (*vcsa));
 792  785  
 793  786          spa_config_exit(zio->io_spa, SCL_STATE, zio->io_spa);
 794  787  }
 795  788  
 796  789  /*
 797  790   * The write of the new location is done.
 798  791   */
 799  792  static void
 800  793  spa_vdev_copy_segment_write_done(zio_t *zio)
 801  794  {
 802  795          vdev_copy_arg_t *vca = zio->io_private;
 803  796  
 804  797          abd_free(zio->io_abd);
 805  798  
 806  799          mutex_enter(&vca->vca_lock);
 807  800          vca->vca_outstanding_bytes -= zio->io_size;
 808  801          cv_signal(&vca->vca_cv);
 809  802          mutex_exit(&vca->vca_lock);
 810  803  }
 811  804  
 812  805  /*
 813  806   * The read of the old location is done.  The parent zio is the write to
 814  807   * the new location.  Allow it to start.
 815  808   */
 816  809  static void
 817  810  spa_vdev_copy_segment_read_done(zio_t *zio)
 818  811  {
 819  812          zio_nowait(zio_unique_parent(zio));
 820  813  }
 821  814  
 822  815  /*
 823  816   * If the old and new vdevs are mirrors, we will read both sides of the old
 824  817   * mirror, and write each copy to the corresponding side of the new mirror.
 825  818   * If the old and new vdevs have a different number of children, we will do
 826  819   * this as best as possible.  Since we aren't verifying checksums, this
 827  820   * ensures that as long as there's a good copy of the data, we'll have a
 828  821   * good copy after the removal, even if there's silent damage to one side
 829  822   * of the mirror. If we're removing a mirror that has some silent damage,
 830  823   * we'll have exactly the same damage in the new location (assuming that
 831  824   * the new location is also a mirror).
 832  825   *
 833  826   * We accomplish this by creating a tree of zio_t's, with as many writes as
 834  827   * there are "children" of the new vdev (a non-redundant vdev counts as one
 835  828   * child, a 2-way mirror has 2 children, etc). Each write has an associated
 836  829   * read from a child of the old vdev. Typically there will be the same
 837  830   * number of children of the old and new vdevs.  However, if there are more
 838  831   * children of the new vdev, some child(ren) of the old vdev will be issued
 839  832   * multiple reads.  If there are more children of the old vdev, some copies
 840  833   * will be dropped.
 841  834   *
 842  835   * For example, the tree of zio_t's for a 2-way mirror is:
 843  836   *
 844  837   *                            null
 845  838   *                           /    \
 846  839   *    write(new vdev, child 0)      write(new vdev, child 1)
 847  840   *      |                             |
 848  841   *    read(old vdev, child 0)       read(old vdev, child 1)
 849  842   *
 850  843   * Child zio's complete before their parents complete.  However, zio's
 851  844   * created with zio_vdev_child_io() may be issued before their children
 852  845   * complete.  In this case we need to make sure that the children (reads)
 853  846   * complete before the parents (writes) are *issued*.  We do this by not
 854  847   * calling zio_nowait() on each write until its corresponding read has
 855  848   * completed.
 856  849   *
 857  850   * The spa_config_lock must be held while zio's created by
 858  851   * zio_vdev_child_io() are in progress, to ensure that the vdev tree does
 859  852   * not change (e.g. due to a concurrent "zpool attach/detach"). The "null"
 860  853   * zio is needed to release the spa_config_lock after all the reads and
 861  854   * writes complete. (Note that we can't grab the config lock for each read,
 862  855   * because it is not reentrant - we could deadlock with a thread waiting
 863  856   * for a write lock.)
 864  857   */
 865  858  static void
 866  859  spa_vdev_copy_one_child(vdev_copy_arg_t *vca, zio_t *nzio,
 867  860      vdev_t *source_vd, uint64_t source_offset,
 868  861      vdev_t *dest_child_vd, uint64_t dest_offset, int dest_id, uint64_t size)
 869  862  {
 870  863          ASSERT3U(spa_config_held(nzio->io_spa, SCL_ALL, RW_READER), !=, 0);
 871  864  
 872  865          mutex_enter(&vca->vca_lock);
 873  866          vca->vca_outstanding_bytes += size;
 874  867          mutex_exit(&vca->vca_lock);
 875  868  
 876  869          abd_t *abd = abd_alloc_for_io(size, B_FALSE);
 877  870  
 878  871          vdev_t *source_child_vd;
 879  872          if (source_vd->vdev_ops == &vdev_mirror_ops && dest_id != -1) {
 880  873                  /*
 881  874                   * Source and dest are both mirrors.  Copy from the same
 882  875                   * child id as we are copying to (wrapping around if there
 883  876                   * are more dest children than source children).
 884  877                   */
 885  878                  source_child_vd =
 886  879                      source_vd->vdev_child[dest_id % source_vd->vdev_children];
 887  880          } else {
 888  881                  source_child_vd = source_vd;
 889  882          }
 890  883  
 891  884          zio_t *write_zio = zio_vdev_child_io(nzio, NULL,
 892  885              dest_child_vd, dest_offset, abd, size,
 893  886              ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL,
 894  887              ZIO_FLAG_CANFAIL,
 895  888              spa_vdev_copy_segment_write_done, vca);
 896  889  
 897  890          zio_nowait(zio_vdev_child_io(write_zio, NULL,
 898  891              source_child_vd, source_offset, abd, size,
 899  892              ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL,
 900  893              ZIO_FLAG_CANFAIL,
 901  894              spa_vdev_copy_segment_read_done, vca));
 902  895  }
 903  896  
 904  897  /*
 905  898   * Allocate a new location for this segment, and create the zio_t's to
 906  899   * read from the old location and write to the new location.
 907  900   */
 908  901  static int
 909  902  spa_vdev_copy_segment(vdev_t *vd, range_tree_t *segs,
 910  903      uint64_t maxalloc, uint64_t txg,
 911  904      vdev_copy_arg_t *vca, zio_alloc_list_t *zal)
 912  905  {
 913  906          metaslab_group_t *mg = vd->vdev_mg;
 914  907          spa_t *spa = vd->vdev_spa;
 915  908          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
 916  909          vdev_indirect_mapping_entry_t *entry;
 917  910          dva_t dst = { 0 };
 918  911          uint64_t start = range_tree_min(segs);
 919  912  
 920  913          ASSERT3U(maxalloc, <=, SPA_MAXBLOCKSIZE);
 921  914  
 922  915          uint64_t size = range_tree_span(segs);
 923  916          if (range_tree_span(segs) > maxalloc) {
 924  917                  /*
 925  918                   * We can't allocate all the segments.  Prefer to end
 926  919                   * the allocation at the end of a segment, thus avoiding
 927  920                   * additional split blocks.
 928  921                   */
 929  922                  range_seg_t search;
 930  923                  avl_index_t where;
 931  924                  search.rs_start = start + maxalloc;
 932  925                  search.rs_end = search.rs_start;
 933  926                  range_seg_t *rs = avl_find(&segs->rt_root, &search, &where);
 934  927                  if (rs == NULL) {
 935  928                          rs = avl_nearest(&segs->rt_root, where, AVL_BEFORE);
 936  929                  } else {
 937  930                          rs = AVL_PREV(&segs->rt_root, rs);
 938  931                  }
 939  932                  if (rs != NULL) {
 940  933                          size = rs->rs_end - start;
 941  934                  } else {
 942  935                          /*
 943  936                           * There are no segments that end before maxalloc.
 944  937                           * I.e. the first segment is larger than maxalloc,
 945  938                           * so we must split it.
 946  939                           */
 947  940                          size = maxalloc;
 948  941                  }
 949  942          }
 950  943          ASSERT3U(size, <=, maxalloc);
 951  944  
 952  945          /*
 953  946           * An allocation class might not have any remaining vdevs or space
 954  947           */
 955  948          metaslab_class_t *mc = mg->mg_class;
 956  949          if (mc != spa_normal_class(spa) && mc->mc_groups <= 1)
 957  950                  mc = spa_normal_class(spa);
 958  951          int error = metaslab_alloc_dva(spa, mc, size, &dst, 0, NULL, txg, 0,
 959  952              zal, 0);
 960  953          if (error == ENOSPC && mc != spa_normal_class(spa)) {
 961  954                  error = metaslab_alloc_dva(spa, spa_normal_class(spa), size,
 962  955                      &dst, 0, NULL, txg, 0, zal, 0);
 963  956          }
 964  957          if (error != 0)
 965  958                  return (error);
 966  959  
 967  960          /*
 968  961           * Determine the ranges that are not actually needed.  Offsets are
 969  962           * relative to the start of the range to be copied (i.e. relative to the
 970  963           * local variable "start").
 971  964           */
 972  965          range_tree_t *obsolete_segs = range_tree_create(NULL, NULL);
 973  966  
 974  967          range_seg_t *rs = avl_first(&segs->rt_root);
 975  968          ASSERT3U(rs->rs_start, ==, start);
 976  969          uint64_t prev_seg_end = rs->rs_end;
 977  970          while ((rs = AVL_NEXT(&segs->rt_root, rs)) != NULL) {
 978  971                  if (rs->rs_start >= start + size) {
 979  972                          break;
 980  973                  } else {
 981  974                          range_tree_add(obsolete_segs,
 982  975                              prev_seg_end - start,
 983  976                              rs->rs_start - prev_seg_end);
 984  977                  }
 985  978                  prev_seg_end = rs->rs_end;
 986  979          }
 987  980          /* We don't end in the middle of an obsolete range */
 988  981          ASSERT3U(start + size, <=, prev_seg_end);
 989  982  
 990  983          range_tree_clear(segs, start, size);
 991  984  
 992  985          /*
 993  986           * We can't have any padding of the allocated size, otherwise we will
 994  987           * misunderstand what's allocated, and the size of the mapping.
 995  988           * The caller ensures this will be true by passing in a size that is
 996  989           * aligned to the worst (highest) ashift in the pool.
 997  990           */
 998  991          ASSERT3U(DVA_GET_ASIZE(&dst), ==, size);
 999  992  
1000  993          entry = kmem_zalloc(sizeof (vdev_indirect_mapping_entry_t), KM_SLEEP);
1001  994          DVA_MAPPING_SET_SRC_OFFSET(&entry->vime_mapping, start);
1002  995          entry->vime_mapping.vimep_dst = dst;
1003  996          if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
1004  997                  entry->vime_obsolete_count = range_tree_space(obsolete_segs);
1005  998          }
1006  999  
1007 1000          vdev_copy_segment_arg_t *vcsa = kmem_zalloc(sizeof (*vcsa), KM_SLEEP);
1008 1001          vcsa->vcsa_dest_dva = &entry->vime_mapping.vimep_dst;
1009 1002          vcsa->vcsa_obsolete_segs = obsolete_segs;
1010 1003          vcsa->vcsa_spa = spa;
1011 1004          vcsa->vcsa_txg = txg;
1012 1005  
1013 1006          /*
1014 1007           * See comment before spa_vdev_copy_one_child().
1015 1008           */
1016 1009          spa_config_enter(spa, SCL_STATE, spa, RW_READER);
1017 1010          zio_t *nzio = zio_null(spa->spa_txg_zio[txg & TXG_MASK], spa, NULL,
1018 1011              spa_vdev_copy_segment_done, vcsa, 0);
1019 1012          vdev_t *dest_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dst));
1020 1013          if (dest_vd->vdev_ops == &vdev_mirror_ops) {
1021 1014                  for (int i = 0; i < dest_vd->vdev_children; i++) {
1022 1015                          vdev_t *child = dest_vd->vdev_child[i];
1023 1016                          spa_vdev_copy_one_child(vca, nzio, vd, start,
1024 1017                              child, DVA_GET_OFFSET(&dst), i, size);
1025 1018                  }
1026 1019          } else {
1027 1020                  spa_vdev_copy_one_child(vca, nzio, vd, start,
1028 1021                      dest_vd, DVA_GET_OFFSET(&dst), -1, size);
1029 1022          }
1030 1023          zio_nowait(nzio);
1031 1024  
1032 1025          list_insert_tail(&svr->svr_new_segments[txg & TXG_MASK], entry);
1033 1026          ASSERT3U(start + size, <=, vd->vdev_ms_count << vd->vdev_ms_shift);
1034 1027          vdev_dirty(vd, 0, NULL, txg);
1035 1028  
1036 1029          return (0);
1037 1030  }
1038 1031  
1039 1032  /*
1040 1033   * Complete the removal of a toplevel vdev. This is called as a
1041 1034   * synctask in the same txg that we will sync out the new config (to the
1042 1035   * MOS object) which indicates that this vdev is indirect.
1043 1036   */
1044 1037  static void
1045 1038  vdev_remove_complete_sync(void *arg, dmu_tx_t *tx)
1046 1039  {
1047 1040          spa_vdev_removal_t *svr = arg;
1048 1041          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1049 1042          vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
1050 1043  
1051 1044          ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
1052 1045  
1053 1046          for (int i = 0; i < TXG_SIZE; i++) {
1054 1047                  ASSERT0(svr->svr_bytes_done[i]);
1055 1048          }
1056 1049  
1057 1050          ASSERT3U(spa->spa_removing_phys.sr_copied, ==,
1058 1051              spa->spa_removing_phys.sr_to_copy);
1059 1052  
1060 1053          vdev_destroy_spacemaps(vd, tx);
1061 1054  
1062 1055          /* destroy leaf zaps, if any */
1063 1056          ASSERT3P(svr->svr_zaplist, !=, NULL);
1064 1057          for (nvpair_t *pair = nvlist_next_nvpair(svr->svr_zaplist, NULL);
1065 1058              pair != NULL;
1066 1059              pair = nvlist_next_nvpair(svr->svr_zaplist, pair)) {
1067 1060                  vdev_destroy_unlink_zap(vd, fnvpair_value_uint64(pair), tx);
1068 1061          }
1069 1062          fnvlist_free(svr->svr_zaplist);
1070 1063  
1071 1064          spa_finish_removal(dmu_tx_pool(tx)->dp_spa, DSS_FINISHED, tx);
1072 1065          /* vd->vdev_path is not available here */
1073 1066          spa_history_log_internal(spa, "vdev remove completed",  tx,
1074 1067              "%s vdev %llu", spa_name(spa), vd->vdev_id);
1075 1068  }
1076 1069  
1077 1070  static void
1078 1071  vdev_remove_enlist_zaps(vdev_t *vd, nvlist_t *zlist)
1079 1072  {
1080 1073          ASSERT3P(zlist, !=, NULL);
1081 1074          ASSERT3P(vd->vdev_ops, !=, &vdev_raidz_ops);
1082 1075  
1083 1076          if (vd->vdev_leaf_zap != 0) {
1084 1077                  char zkey[32];
1085 1078                  (void) snprintf(zkey, sizeof (zkey), "%s-%"PRIu64,
1086 1079                      VDEV_REMOVAL_ZAP_OBJS, vd->vdev_leaf_zap);
1087 1080                  fnvlist_add_uint64(zlist, zkey, vd->vdev_leaf_zap);
1088 1081          }
1089 1082  
1090 1083          for (uint64_t id = 0; id < vd->vdev_children; id++) {
1091 1084                  vdev_remove_enlist_zaps(vd->vdev_child[id], zlist);
1092 1085          }
1093 1086  }
1094 1087  
1095 1088  static void
1096 1089  vdev_remove_replace_with_indirect(vdev_t *vd, uint64_t txg)
1097 1090  {
1098 1091          vdev_t *ivd;
1099 1092          dmu_tx_t *tx;
1100 1093          spa_t *spa = vd->vdev_spa;
1101 1094          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
1102 1095  
1103 1096          /*
1104 1097           * First, build a list of leaf zaps to be destroyed.
1105 1098           * This is passed to the sync context thread,
1106 1099           * which does the actual unlinking.
1107 1100           */
1108 1101          svr->svr_zaplist = fnvlist_alloc();
1109 1102          vdev_remove_enlist_zaps(vd, svr->svr_zaplist);
1110 1103  
1111 1104          ivd = vdev_add_parent(vd, &vdev_indirect_ops);
1112 1105          ivd->vdev_removing = 0;
1113 1106  
1114 1107          vd->vdev_leaf_zap = 0;
1115 1108  
1116 1109          vdev_remove_child(ivd, vd);
1117 1110          vdev_compact_children(ivd);
1118 1111  
1119 1112          ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
1120 1113  
1121 1114          tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1122 1115          dsl_sync_task_nowait(spa->spa_dsl_pool, vdev_remove_complete_sync, svr,
1123 1116              0, ZFS_SPACE_CHECK_NONE, tx);
1124 1117          dmu_tx_commit(tx);
1125 1118  
1126 1119          /*
1127 1120           * Indicate that this thread has exited.
1128 1121           * After this, we can not use svr.
1129 1122           */
1130 1123          mutex_enter(&svr->svr_lock);
1131 1124          svr->svr_thread = NULL;
1132 1125          cv_broadcast(&svr->svr_cv);
1133 1126          mutex_exit(&svr->svr_lock);
1134 1127  }
1135 1128  
1136 1129  /*
1137 1130   * Complete the removal of a toplevel vdev. This is called in open
1138 1131   * context by the removal thread after we have copied all vdev's data.
1139 1132   */
1140 1133  static void
1141 1134  vdev_remove_complete(spa_t *spa)
1142 1135  {
1143 1136          uint64_t txg;
1144 1137  
1145 1138          /*
1146 1139           * Wait for any deferred frees to be synced before we call
1147 1140           * vdev_metaslab_fini()
1148 1141           */
1149 1142          txg_wait_synced(spa->spa_dsl_pool, 0);
1150 1143          txg = spa_vdev_enter(spa);
1151 1144          vdev_t *vd = vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
1152 1145          ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
1153 1146  
1154 1147          sysevent_t *ev = spa_event_create(spa, vd, NULL,
1155 1148              ESC_ZFS_VDEV_REMOVE_DEV);
1156 1149  
1157 1150          zfs_dbgmsg("finishing device removal for vdev %llu in txg %llu",
1158 1151              vd->vdev_id, txg);
1159 1152  
1160 1153          /*
1161 1154           * Discard allocation state.
1162 1155           */
1163 1156          if (vd->vdev_mg != NULL) {
1164 1157                  vdev_metaslab_fini(vd);
1165 1158                  metaslab_group_destroy(vd->vdev_mg);
1166 1159                  vd->vdev_mg = NULL;
1167 1160          }
1168 1161          ASSERT0(vd->vdev_stat.vs_space);
1169 1162          ASSERT0(vd->vdev_stat.vs_dspace);
1170 1163  
1171 1164          vdev_remove_replace_with_indirect(vd, txg);
1172 1165  
1173 1166          /*
1174 1167           * We now release the locks, allowing spa_sync to run and finish the
1175 1168           * removal via vdev_remove_complete_sync in syncing context.
1176 1169           *
1177 1170           * Note that we hold on to the vdev_t that has been replaced.  Since
1178 1171           * it isn't part of the vdev tree any longer, it can't be concurrently
1179 1172           * manipulated, even while we don't have the config lock.
1180 1173           */
1181 1174          (void) spa_vdev_exit(spa, NULL, txg, 0);
1182 1175  
1183 1176          /*
1184 1177           * Top ZAP should have been transferred to the indirect vdev in
1185 1178           * vdev_remove_replace_with_indirect.
1186 1179           */
1187 1180          ASSERT0(vd->vdev_top_zap);
1188 1181  
1189 1182          /*
1190 1183           * Leaf ZAP should have been moved in vdev_remove_replace_with_indirect.
1191 1184           */
1192 1185          ASSERT0(vd->vdev_leaf_zap);
1193 1186  
1194 1187          txg = spa_vdev_enter(spa);
1195 1188          (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
1196 1189          /*
1197 1190           * Request to update the config and the config cachefile.
1198 1191           */
1199 1192          vdev_config_dirty(spa->spa_root_vdev);
1200 1193          (void) spa_vdev_exit(spa, vd, txg, 0);
1201 1194  
1202 1195          spa_event_post(ev);
1203 1196  }
1204 1197  
1205 1198  /*
1206 1199   * Evacuates a segment of size at most max_alloc from the vdev
1207 1200   * via repeated calls to spa_vdev_copy_segment. If an allocation
1208 1201   * fails, the pool is probably too fragmented to handle such a
1209 1202   * large size, so decrease max_alloc so that the caller will not try
1210 1203   * this size again this txg.
1211 1204   */
1212 1205  static void
1213 1206  spa_vdev_copy_impl(vdev_t *vd, spa_vdev_removal_t *svr, vdev_copy_arg_t *vca,
1214 1207      uint64_t *max_alloc, dmu_tx_t *tx)
1215 1208  {
1216 1209          uint64_t txg = dmu_tx_get_txg(tx);
1217 1210          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1218 1211  
1219 1212          mutex_enter(&svr->svr_lock);
1220 1213  
1221 1214          /*
1222 1215           * Determine how big of a chunk to copy.  We can allocate up
1223 1216           * to max_alloc bytes, and we can span up to vdev_removal_max_span
1224 1217           * bytes of unallocated space at a time.  "segs" will track the
1225 1218           * allocated segments that we are copying.  We may also be copying
1226 1219           * free segments (of up to vdev_removal_max_span bytes).
1227 1220           */
1228 1221          range_tree_t *segs = range_tree_create(NULL, NULL);
1229 1222          for (;;) {
1230 1223                  range_seg_t *rs = avl_first(&svr->svr_allocd_segs->rt_root);
1231 1224                  if (rs == NULL)
1232 1225                          break;
1233 1226  
1234 1227                  uint64_t seg_length;
1235 1228  
1236 1229                  if (range_tree_is_empty(segs)) {
1237 1230                          /* need to truncate the first seg based on max_alloc */
1238 1231                          seg_length =
1239 1232                              MIN(rs->rs_end - rs->rs_start, *max_alloc);
1240 1233                  } else {
1241 1234                          if (rs->rs_start - range_tree_max(segs) >
1242 1235                              vdev_removal_max_span) {
1243 1236                                  /*
1244 1237                                   * Including this segment would cause us to
1245 1238                                   * copy a larger unneeded chunk than is allowed.
1246 1239                                   */
1247 1240                                  break;
1248 1241                          } else if (rs->rs_end - range_tree_min(segs) >
1249 1242                              *max_alloc) {
1250 1243                                  /*
1251 1244                                   * This additional segment would extend past
1252 1245                                   * max_alloc. Rather than splitting this
1253 1246                                   * segment, leave it for the next mapping.
1254 1247                                   */
1255 1248                                  break;
1256 1249                          } else {
1257 1250                                  seg_length = rs->rs_end - rs->rs_start;
1258 1251                          }
1259 1252                  }
1260 1253  
1261 1254                  range_tree_add(segs, rs->rs_start, seg_length);
1262 1255                  range_tree_remove(svr->svr_allocd_segs,
1263 1256                      rs->rs_start, seg_length);
1264 1257          }
1265 1258  
1266 1259          if (range_tree_is_empty(segs)) {
1267 1260                  mutex_exit(&svr->svr_lock);
1268 1261                  range_tree_destroy(segs);
1269 1262                  return;
1270 1263          }
1271 1264  
1272 1265          if (svr->svr_max_offset_to_sync[txg & TXG_MASK] == 0) {
1273 1266                  dsl_sync_task_nowait(dmu_tx_pool(tx), vdev_mapping_sync,
1274 1267                      svr, 0, ZFS_SPACE_CHECK_NONE, tx);
1275 1268          }
1276 1269  
1277 1270          svr->svr_max_offset_to_sync[txg & TXG_MASK] = range_tree_max(segs);
1278 1271  
1279 1272          /*
1280 1273           * Note: this is the amount of *allocated* space
1281 1274           * that we are taking care of each txg.
1282 1275           */
1283 1276          svr->svr_bytes_done[txg & TXG_MASK] += range_tree_space(segs);
1284 1277  
1285 1278          mutex_exit(&svr->svr_lock);
1286 1279  
1287 1280          zio_alloc_list_t zal;
1288 1281          metaslab_trace_init(&zal);
1289 1282          uint64_t thismax = SPA_MAXBLOCKSIZE;
1290 1283          while (!range_tree_is_empty(segs)) {
1291 1284                  int error = spa_vdev_copy_segment(vd,
1292 1285                      segs, thismax, txg, vca, &zal);
1293 1286  
1294 1287                  if (error == ENOSPC) {
1295 1288                          /*
1296 1289                           * Cut our segment in half, and don't try this
1297 1290                           * segment size again this txg.  Note that the
1298 1291                           * allocation size must be aligned to the highest
1299 1292                           * ashift in the pool, so that the allocation will
1300 1293                           * not be padded out to a multiple of the ashift,
1301 1294                           * which could cause us to think that this mapping
1302 1295                           * is larger than we intended.
1303 1296                           */
1304 1297                          ASSERT3U(spa->spa_max_ashift, >=, SPA_MINBLOCKSHIFT);
1305 1298                          ASSERT3U(spa->spa_max_ashift, ==, spa->spa_min_ashift);
1306 1299                          uint64_t attempted =
1307 1300                              MIN(range_tree_span(segs), thismax);
1308 1301                          thismax = P2ROUNDUP(attempted / 2,
1309 1302                              1 << spa->spa_max_ashift);
1310 1303                          /*
1311 1304                           * The minimum-size allocation can not fail.
1312 1305                           */
1313 1306                          ASSERT3U(attempted, >, 1 << spa->spa_max_ashift);
1314 1307                          *max_alloc = attempted - (1 << spa->spa_max_ashift);
1315 1308                  } else {
1316 1309                          ASSERT0(error);
1317 1310  
1318 1311                          /*
1319 1312                           * We've performed an allocation, so reset the
1320 1313                           * alloc trace list.
1321 1314                           */
1322 1315                          metaslab_trace_fini(&zal);
1323 1316                          metaslab_trace_init(&zal);
1324 1317                  }
1325 1318          }
1326 1319          metaslab_trace_fini(&zal);
1327 1320          range_tree_destroy(segs);
1328 1321  }
1329 1322  
1330 1323  /*
1331 1324   * The removal thread operates in open context.  It iterates over all
1332 1325   * allocated space in the vdev, by loading each metaslab's spacemap.
1333 1326   * For each contiguous segment of allocated space (capping the segment
1334 1327   * size at SPA_MAXBLOCKSIZE), we:
1335 1328   *    - Allocate space for it on another vdev.
1336 1329   *    - Create a new mapping from the old location to the new location
1337 1330   *      (as a record in svr_new_segments).
1338 1331   *    - Initiate a logical read zio to get the data off the removing disk.
1339 1332   *    - In the read zio's done callback, initiate a logical write zio to
1340 1333   *      write it to the new vdev.
1341 1334   * Note that all of this will take effect when a particular TXG syncs.
1342 1335   * The sync thread ensures that all the phys reads and writes for the syncing
1343 1336   * TXG have completed (see spa_txg_zio) and writes the new mappings to disk
1344 1337   * (see vdev_mapping_sync()).
1345 1338   */
1346 1339  static void
1347 1340  spa_vdev_remove_thread(void *arg)
1348 1341  {
1349 1342          spa_t *spa = arg;
1350 1343          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
1351 1344          vdev_copy_arg_t vca;
1352 1345          uint64_t max_alloc = zfs_remove_max_segment;
1353 1346          uint64_t last_txg = 0;
1354 1347  
1355 1348          spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1356 1349          vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
1357 1350          vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
1358 1351          uint64_t start_offset = vdev_indirect_mapping_max_offset(vim);
1359 1352  
1360 1353          ASSERT3P(vd->vdev_ops, !=, &vdev_indirect_ops);
1361 1354          ASSERT(vdev_is_concrete(vd));
1362 1355          ASSERT(vd->vdev_removing);
1363 1356          ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
1364 1357          ASSERT(vim != NULL);
1365 1358  
1366 1359          mutex_init(&vca.vca_lock, NULL, MUTEX_DEFAULT, NULL);
1367 1360          cv_init(&vca.vca_cv, NULL, CV_DEFAULT, NULL);
1368 1361          vca.vca_outstanding_bytes = 0;
1369 1362  
1370 1363          mutex_enter(&svr->svr_lock);
1371 1364  
1372 1365          /*
1373 1366           * Start from vim_max_offset so we pick up where we left off
1374 1367           * if we are restarting the removal after opening the pool.
1375 1368           */
1376 1369          uint64_t msi;
1377 1370          for (msi = start_offset >> vd->vdev_ms_shift;
1378 1371              msi < vd->vdev_ms_count && !svr->svr_thread_exit; msi++) {
1379 1372                  metaslab_t *msp = vd->vdev_ms[msi];
1380 1373                  ASSERT3U(msi, <=, vd->vdev_ms_count);
1381 1374  
1382 1375                  ASSERT0(range_tree_space(svr->svr_allocd_segs));
1383 1376  
1384 1377                  mutex_enter(&msp->ms_sync_lock);
1385 1378                  mutex_enter(&msp->ms_lock);
1386 1379  
1387 1380                  /*
1388 1381                   * Assert nothing in flight -- ms_*tree is empty.
1389 1382                   */
1390 1383                  for (int i = 0; i < TXG_SIZE; i++) {
1391 1384                          ASSERT0(range_tree_space(msp->ms_allocating[i]));
1392 1385                  }
1393 1386  
  
    | 
      ↓ open down ↓ | 
    1089 lines elided | 
    
      ↑ open up ↑ | 
  
1394 1387                  /*
1395 1388                   * If the metaslab has ever been allocated from (ms_sm!=NULL),
1396 1389                   * read the allocated segments from the space map object
1397 1390                   * into svr_allocd_segs. Since we do this while holding
1398 1391                   * svr_lock and ms_sync_lock, concurrent frees (which
1399 1392                   * would have modified the space map) will wait for us
1400 1393                   * to finish loading the spacemap, and then take the
1401 1394                   * appropriate action (see free_from_removing_vdev()).
1402 1395                   */
1403 1396                  if (msp->ms_sm != NULL) {
1404      -                        space_map_t *sm = NULL;
     1397 +                        VERIFY0(space_map_load(msp->ms_sm,
     1398 +                            svr->svr_allocd_segs, SM_ALLOC));
1405 1399  
1406      -                        /*
1407      -                         * We have to open a new space map here, because
1408      -                         * ms_sm's sm_length and sm_alloc may not reflect
1409      -                         * what's in the object contents, if we are in between
1410      -                         * metaslab_sync() and metaslab_sync_done().
1411      -                         */
1412      -                        VERIFY0(space_map_open(&sm,
1413      -                            spa->spa_dsl_pool->dp_meta_objset,
1414      -                            msp->ms_sm->sm_object, msp->ms_sm->sm_start,
1415      -                            msp->ms_sm->sm_size, msp->ms_sm->sm_shift));
1416      -                        space_map_update(sm);
1417      -                        VERIFY0(space_map_load(sm, svr->svr_allocd_segs,
1418      -                            SM_ALLOC));
1419      -                        space_map_close(sm);
1420      -
1421 1400                          range_tree_walk(msp->ms_freeing,
1422 1401                              range_tree_remove, svr->svr_allocd_segs);
1423 1402  
1424 1403                          /*
1425 1404                           * When we are resuming from a paused removal (i.e.
1426 1405                           * when importing a pool with a removal in progress),
1427 1406                           * discard any state that we have already processed.
1428 1407                           */
1429 1408                          range_tree_clear(svr->svr_allocd_segs, 0, start_offset);
1430 1409                  }
1431 1410                  mutex_exit(&msp->ms_lock);
1432 1411                  mutex_exit(&msp->ms_sync_lock);
1433 1412  
1434 1413                  vca.vca_msp = msp;
1435 1414                  zfs_dbgmsg("copying %llu segments for metaslab %llu",
1436 1415                      avl_numnodes(&svr->svr_allocd_segs->rt_root),
1437 1416                      msp->ms_id);
1438 1417  
1439 1418                  while (!svr->svr_thread_exit &&
1440 1419                      !range_tree_is_empty(svr->svr_allocd_segs)) {
1441 1420  
1442 1421                          mutex_exit(&svr->svr_lock);
1443 1422  
1444 1423                          /*
1445 1424                           * We need to periodically drop the config lock so that
1446 1425                           * writers can get in.  Additionally, we can't wait
1447 1426                           * for a txg to sync while holding a config lock
1448 1427                           * (since a waiting writer could cause a 3-way deadlock
1449 1428                           * with the sync thread, which also gets a config
1450 1429                           * lock for reader).  So we can't hold the config lock
1451 1430                           * while calling dmu_tx_assign().
1452 1431                           */
1453 1432                          spa_config_exit(spa, SCL_CONFIG, FTAG);
1454 1433  
1455 1434                          /*
1456 1435                           * This delay will pause the removal around the point
1457 1436                           * specified by zfs_remove_max_bytes_pause. We do this
1458 1437                           * solely from the test suite or during debugging.
1459 1438                           */
1460 1439                          uint64_t bytes_copied =
1461 1440                              spa->spa_removing_phys.sr_copied;
1462 1441                          for (int i = 0; i < TXG_SIZE; i++)
1463 1442                                  bytes_copied += svr->svr_bytes_done[i];
1464 1443                          while (zfs_remove_max_bytes_pause <= bytes_copied &&
1465 1444                              !svr->svr_thread_exit)
1466 1445                                  delay(hz);
1467 1446  
1468 1447                          mutex_enter(&vca.vca_lock);
1469 1448                          while (vca.vca_outstanding_bytes >
1470 1449                              zfs_remove_max_copy_bytes) {
1471 1450                                  cv_wait(&vca.vca_cv, &vca.vca_lock);
1472 1451                          }
1473 1452                          mutex_exit(&vca.vca_lock);
1474 1453  
1475 1454                          dmu_tx_t *tx =
1476 1455                              dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
1477 1456  
1478 1457                          VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
1479 1458                          uint64_t txg = dmu_tx_get_txg(tx);
1480 1459  
1481 1460                          /*
1482 1461                           * Reacquire the vdev_config lock.  The vdev_t
1483 1462                           * that we're removing may have changed, e.g. due
1484 1463                           * to a vdev_attach or vdev_detach.
1485 1464                           */
1486 1465                          spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1487 1466                          vd = vdev_lookup_top(spa, svr->svr_vdev_id);
1488 1467  
1489 1468                          if (txg != last_txg)
1490 1469                                  max_alloc = zfs_remove_max_segment;
1491 1470                          last_txg = txg;
1492 1471  
1493 1472                          spa_vdev_copy_impl(vd, svr, &vca, &max_alloc, tx);
1494 1473  
1495 1474                          dmu_tx_commit(tx);
1496 1475                          mutex_enter(&svr->svr_lock);
1497 1476                  }
1498 1477          }
1499 1478  
1500 1479          mutex_exit(&svr->svr_lock);
1501 1480  
1502 1481          spa_config_exit(spa, SCL_CONFIG, FTAG);
1503 1482  
1504 1483          /*
1505 1484           * Wait for all copies to finish before cleaning up the vca.
1506 1485           */
1507 1486          txg_wait_synced(spa->spa_dsl_pool, 0);
1508 1487          ASSERT0(vca.vca_outstanding_bytes);
1509 1488  
1510 1489          mutex_destroy(&vca.vca_lock);
1511 1490          cv_destroy(&vca.vca_cv);
1512 1491  
1513 1492          if (svr->svr_thread_exit) {
1514 1493                  mutex_enter(&svr->svr_lock);
1515 1494                  range_tree_vacate(svr->svr_allocd_segs, NULL, NULL);
1516 1495                  svr->svr_thread = NULL;
1517 1496                  cv_broadcast(&svr->svr_cv);
1518 1497                  mutex_exit(&svr->svr_lock);
1519 1498          } else {
1520 1499                  ASSERT0(range_tree_space(svr->svr_allocd_segs));
1521 1500                  vdev_remove_complete(spa);
1522 1501          }
1523 1502  }
1524 1503  
1525 1504  void
1526 1505  spa_vdev_remove_suspend(spa_t *spa)
1527 1506  {
1528 1507          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
1529 1508  
1530 1509          if (svr == NULL)
1531 1510                  return;
1532 1511  
1533 1512          mutex_enter(&svr->svr_lock);
1534 1513          svr->svr_thread_exit = B_TRUE;
1535 1514          while (svr->svr_thread != NULL)
1536 1515                  cv_wait(&svr->svr_cv, &svr->svr_lock);
1537 1516          svr->svr_thread_exit = B_FALSE;
1538 1517          mutex_exit(&svr->svr_lock);
1539 1518  }
1540 1519  
1541 1520  /* ARGSUSED */
1542 1521  static int
1543 1522  spa_vdev_remove_cancel_check(void *arg, dmu_tx_t *tx)
1544 1523  {
1545 1524          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1546 1525  
1547 1526          if (spa->spa_vdev_removal == NULL)
1548 1527                  return (ENOTACTIVE);
1549 1528          return (0);
1550 1529  }
1551 1530  
1552 1531  /*
1553 1532   * Cancel a removal by freeing all entries from the partial mapping
1554 1533   * and marking the vdev as no longer being removing.
1555 1534   */
1556 1535  /* ARGSUSED */
1557 1536  static void
1558 1537  spa_vdev_remove_cancel_sync(void *arg, dmu_tx_t *tx)
1559 1538  {
1560 1539          spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1561 1540          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
1562 1541          vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
1563 1542          vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
1564 1543          vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
1565 1544          objset_t *mos = spa->spa_meta_objset;
1566 1545  
1567 1546          ASSERT3P(svr->svr_thread, ==, NULL);
1568 1547  
1569 1548          spa_feature_decr(spa, SPA_FEATURE_DEVICE_REMOVAL, tx);
1570 1549          if (vdev_obsolete_counts_are_precise(vd)) {
1571 1550                  spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
1572 1551                  VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap,
1573 1552                      VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, tx));
1574 1553          }
1575 1554  
1576 1555          if (vdev_obsolete_sm_object(vd) != 0) {
1577 1556                  ASSERT(vd->vdev_obsolete_sm != NULL);
1578 1557                  ASSERT3U(vdev_obsolete_sm_object(vd), ==,
1579 1558                      space_map_object(vd->vdev_obsolete_sm));
1580 1559  
1581 1560                  space_map_free(vd->vdev_obsolete_sm, tx);
1582 1561                  VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap,
1583 1562                      VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, tx));
1584 1563                  space_map_close(vd->vdev_obsolete_sm);
1585 1564                  vd->vdev_obsolete_sm = NULL;
1586 1565                  spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
1587 1566          }
1588 1567          for (int i = 0; i < TXG_SIZE; i++) {
1589 1568                  ASSERT(list_is_empty(&svr->svr_new_segments[i]));
1590 1569                  ASSERT3U(svr->svr_max_offset_to_sync[i], <=,
1591 1570                      vdev_indirect_mapping_max_offset(vim));
1592 1571          }
1593 1572  
1594 1573          for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
1595 1574                  metaslab_t *msp = vd->vdev_ms[msi];
1596 1575  
1597 1576                  if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
1598 1577                          break;
1599 1578  
1600 1579                  ASSERT0(range_tree_space(svr->svr_allocd_segs));
1601 1580  
1602 1581                  mutex_enter(&msp->ms_lock);
1603 1582  
  
    | 
      ↓ open down ↓ | 
    173 lines elided | 
    
      ↑ open up ↑ | 
  
1604 1583                  /*
1605 1584                   * Assert nothing in flight -- ms_*tree is empty.
1606 1585                   */
1607 1586                  for (int i = 0; i < TXG_SIZE; i++)
1608 1587                          ASSERT0(range_tree_space(msp->ms_allocating[i]));
1609 1588                  for (int i = 0; i < TXG_DEFER_SIZE; i++)
1610 1589                          ASSERT0(range_tree_space(msp->ms_defer[i]));
1611 1590                  ASSERT0(range_tree_space(msp->ms_freed));
1612 1591  
1613 1592                  if (msp->ms_sm != NULL) {
1614      -                        /*
1615      -                         * Assert that the in-core spacemap has the same
1616      -                         * length as the on-disk one, so we can use the
1617      -                         * existing in-core spacemap to load it from disk.
1618      -                         */
1619      -                        ASSERT3U(msp->ms_sm->sm_alloc, ==,
1620      -                            msp->ms_sm->sm_phys->smp_alloc);
1621      -                        ASSERT3U(msp->ms_sm->sm_length, ==,
1622      -                            msp->ms_sm->sm_phys->smp_objsize);
1623      -
1624 1593                          mutex_enter(&svr->svr_lock);
1625 1594                          VERIFY0(space_map_load(msp->ms_sm,
1626 1595                              svr->svr_allocd_segs, SM_ALLOC));
1627 1596                          range_tree_walk(msp->ms_freeing,
1628 1597                              range_tree_remove, svr->svr_allocd_segs);
1629 1598  
1630 1599                          /*
1631 1600                           * Clear everything past what has been synced,
1632 1601                           * because we have not allocated mappings for it yet.
1633 1602                           */
1634 1603                          uint64_t syncd = vdev_indirect_mapping_max_offset(vim);
1635 1604                          uint64_t sm_end = msp->ms_sm->sm_start +
1636 1605                              msp->ms_sm->sm_size;
1637 1606                          if (sm_end > syncd)
1638 1607                                  range_tree_clear(svr->svr_allocd_segs,
1639 1608                                      syncd, sm_end - syncd);
1640 1609  
1641 1610                          mutex_exit(&svr->svr_lock);
1642 1611                  }
1643 1612                  mutex_exit(&msp->ms_lock);
1644 1613  
1645 1614                  mutex_enter(&svr->svr_lock);
1646 1615                  range_tree_vacate(svr->svr_allocd_segs,
1647 1616                      free_mapped_segment_cb, vd);
1648 1617                  mutex_exit(&svr->svr_lock);
1649 1618          }
1650 1619  
1651 1620          /*
1652 1621           * Note: this must happen after we invoke free_mapped_segment_cb,
1653 1622           * because it adds to the obsolete_segments.
1654 1623           */
1655 1624          range_tree_vacate(vd->vdev_obsolete_segments, NULL, NULL);
1656 1625  
1657 1626          ASSERT3U(vic->vic_mapping_object, ==,
1658 1627              vdev_indirect_mapping_object(vd->vdev_indirect_mapping));
1659 1628          vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
1660 1629          vd->vdev_indirect_mapping = NULL;
1661 1630          vdev_indirect_mapping_free(mos, vic->vic_mapping_object, tx);
1662 1631          vic->vic_mapping_object = 0;
1663 1632  
1664 1633          ASSERT3U(vic->vic_births_object, ==,
1665 1634              vdev_indirect_births_object(vd->vdev_indirect_births));
1666 1635          vdev_indirect_births_close(vd->vdev_indirect_births);
1667 1636          vd->vdev_indirect_births = NULL;
1668 1637          vdev_indirect_births_free(mos, vic->vic_births_object, tx);
1669 1638          vic->vic_births_object = 0;
1670 1639  
1671 1640          /*
1672 1641           * We may have processed some frees from the removing vdev in this
1673 1642           * txg, thus increasing svr_bytes_done; discard that here to
1674 1643           * satisfy the assertions in spa_vdev_removal_destroy().
1675 1644           * Note that future txg's can not have any bytes_done, because
1676 1645           * future TXG's are only modified from open context, and we have
1677 1646           * already shut down the copying thread.
1678 1647           */
1679 1648          svr->svr_bytes_done[dmu_tx_get_txg(tx) & TXG_MASK] = 0;
1680 1649          spa_finish_removal(spa, DSS_CANCELED, tx);
1681 1650  
1682 1651          vd->vdev_removing = B_FALSE;
1683 1652          vdev_config_dirty(vd);
1684 1653  
1685 1654          zfs_dbgmsg("canceled device removal for vdev %llu in %llu",
1686 1655              vd->vdev_id, dmu_tx_get_txg(tx));
1687 1656          spa_history_log_internal(spa, "vdev remove canceled", tx,
1688 1657              "%s vdev %llu %s", spa_name(spa),
1689 1658              vd->vdev_id, (vd->vdev_path != NULL) ? vd->vdev_path : "-");
1690 1659  }
1691 1660  
1692 1661  int
1693 1662  spa_vdev_remove_cancel(spa_t *spa)
1694 1663  {
1695 1664          spa_vdev_remove_suspend(spa);
1696 1665  
1697 1666          if (spa->spa_vdev_removal == NULL)
1698 1667                  return (ENOTACTIVE);
1699 1668  
1700 1669          uint64_t vdid = spa->spa_vdev_removal->svr_vdev_id;
1701 1670  
1702 1671          int error = dsl_sync_task(spa->spa_name, spa_vdev_remove_cancel_check,
1703 1672              spa_vdev_remove_cancel_sync, NULL, 0,
1704 1673              ZFS_SPACE_CHECK_EXTRA_RESERVED);
1705 1674  
  
    | 
      ↓ open down ↓ | 
    72 lines elided | 
    
      ↑ open up ↑ | 
  
1706 1675          if (error == 0) {
1707 1676                  spa_config_enter(spa, SCL_ALLOC | SCL_VDEV, FTAG, RW_WRITER);
1708 1677                  vdev_t *vd = vdev_lookup_top(spa, vdid);
1709 1678                  metaslab_group_activate(vd->vdev_mg);
1710 1679                  spa_config_exit(spa, SCL_ALLOC | SCL_VDEV, FTAG);
1711 1680          }
1712 1681  
1713 1682          return (error);
1714 1683  }
1715 1684  
1716      -/*
1717      - * Called every sync pass of every txg if there's a svr.
1718      - */
1719 1685  void
1720 1686  svr_sync(spa_t *spa, dmu_tx_t *tx)
1721 1687  {
1722 1688          spa_vdev_removal_t *svr = spa->spa_vdev_removal;
1723 1689          int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1724 1690  
1725 1691          /*
1726 1692           * This check is necessary so that we do not dirty the
1727 1693           * DIRECTORY_OBJECT via spa_sync_removing_state() when there
1728 1694           * is nothing to do.  Dirtying it every time would prevent us
1729 1695           * from syncing-to-convergence.
1730 1696           */
1731 1697          if (svr->svr_bytes_done[txgoff] == 0)
1732 1698                  return;
1733 1699  
1734 1700          /*
1735 1701           * Update progress accounting.
1736 1702           */
1737 1703          spa->spa_removing_phys.sr_copied += svr->svr_bytes_done[txgoff];
1738 1704          svr->svr_bytes_done[txgoff] = 0;
1739 1705  
1740 1706          spa_sync_removing_state(spa, tx);
1741 1707  }
1742 1708  
1743 1709  static void
1744 1710  vdev_remove_make_hole_and_free(vdev_t *vd)
1745 1711  {
1746 1712          uint64_t id = vd->vdev_id;
1747 1713          spa_t *spa = vd->vdev_spa;
1748 1714          vdev_t *rvd = spa->spa_root_vdev;
1749 1715          boolean_t last_vdev = (id == (rvd->vdev_children - 1));
1750 1716  
1751 1717          ASSERT(MUTEX_HELD(&spa_namespace_lock));
1752 1718          ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1753 1719  
1754 1720          vdev_free(vd);
1755 1721  
1756 1722          if (last_vdev) {
1757 1723                  vdev_compact_children(rvd);
1758 1724          } else {
1759 1725                  vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
1760 1726                  vdev_add_child(rvd, vd);
1761 1727          }
1762 1728          vdev_config_dirty(rvd);
1763 1729  
1764 1730          /*
1765 1731           * Reassess the health of our root vdev.
1766 1732           */
1767 1733          vdev_reopen(rvd);
1768 1734  }
1769 1735  
1770 1736  /*
1771 1737   * Remove a log device.  The config lock is held for the specified TXG.
  
    | 
      ↓ open down ↓ | 
    43 lines elided | 
    
      ↑ open up ↑ | 
  
1772 1738   */
1773 1739  static int
1774 1740  spa_vdev_remove_log(vdev_t *vd, uint64_t *txg)
1775 1741  {
1776 1742          metaslab_group_t *mg = vd->vdev_mg;
1777 1743          spa_t *spa = vd->vdev_spa;
1778 1744          int error = 0;
1779 1745  
1780 1746          ASSERT(vd->vdev_islog);
1781 1747          ASSERT(vd == vd->vdev_top);
     1748 +        ASSERT(MUTEX_HELD(&spa_namespace_lock));
1782 1749  
1783 1750          /*
1784 1751           * Stop allocating from this vdev.
1785 1752           */
1786 1753          metaslab_group_passivate(mg);
1787 1754  
1788 1755          /*
1789 1756           * Wait for the youngest allocations and frees to sync,
1790 1757           * and then wait for the deferral of those frees to finish.
1791 1758           */
1792 1759          spa_vdev_config_exit(spa, NULL,
1793 1760              *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
1794 1761  
1795 1762          /*
1796      -         * Evacuate the device.  We don't hold the config lock as writer
1797      -         * since we need to do I/O but we do keep the
     1763 +         * Evacuate the device.  We don't hold the config lock as
     1764 +         * writer since we need to do I/O but we do keep the
1798 1765           * spa_namespace_lock held.  Once this completes the device
1799 1766           * should no longer have any blocks allocated on it.
1800 1767           */
1801      -        if (vd->vdev_islog) {
1802      -                if (vd->vdev_stat.vs_alloc != 0)
1803      -                        error = spa_reset_logs(spa);
1804      -        }
     1768 +        ASSERT(MUTEX_HELD(&spa_namespace_lock));
     1769 +        if (vd->vdev_stat.vs_alloc != 0)
     1770 +                error = spa_reset_logs(spa);
1805 1771  
1806 1772          *txg = spa_vdev_config_enter(spa);
1807 1773  
1808 1774          if (error != 0) {
1809 1775                  metaslab_group_activate(mg);
1810 1776                  return (error);
1811 1777          }
1812 1778          ASSERT0(vd->vdev_stat.vs_alloc);
1813 1779  
1814 1780          /*
1815 1781           * The evacuation succeeded.  Remove any remaining MOS metadata
1816 1782           * associated with this vdev, and wait for these changes to sync.
1817 1783           */
1818 1784          vd->vdev_removing = B_TRUE;
1819 1785  
1820 1786          vdev_dirty_leaves(vd, VDD_DTL, *txg);
1821 1787          vdev_config_dirty(vd);
1822 1788  
     1789 +        vdev_metaslab_fini(vd);
     1790 +
1823 1791          spa_history_log_internal(spa, "vdev remove", NULL,
1824 1792              "%s vdev %llu (log) %s", spa_name(spa), vd->vdev_id,
1825 1793              (vd->vdev_path != NULL) ? vd->vdev_path : "-");
1826 1794  
1827 1795          /* Make sure these changes are sync'ed */
1828 1796          spa_vdev_config_exit(spa, NULL, *txg, 0, FTAG);
1829 1797  
1830 1798          /* Stop initializing */
1831 1799          (void) vdev_initialize_stop_all(vd, VDEV_INITIALIZE_CANCELED);
1832 1800  
1833 1801          *txg = spa_vdev_config_enter(spa);
1834 1802  
1835 1803          sysevent_t *ev = spa_event_create(spa, vd, NULL,
1836 1804              ESC_ZFS_VDEV_REMOVE_DEV);
1837 1805          ASSERT(MUTEX_HELD(&spa_namespace_lock));
1838 1806          ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1839 1807  
1840 1808          /* The top ZAP should have been destroyed by vdev_remove_empty. */
1841 1809          ASSERT0(vd->vdev_top_zap);
  
    | 
      ↓ open down ↓ | 
    9 lines elided | 
    
      ↑ open up ↑ | 
  
1842 1810          /* The leaf ZAP should have been destroyed by vdev_dtl_sync. */
1843 1811          ASSERT0(vd->vdev_leaf_zap);
1844 1812  
1845 1813          (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
1846 1814  
1847 1815          if (list_link_active(&vd->vdev_state_dirty_node))
1848 1816                  vdev_state_clean(vd);
1849 1817          if (list_link_active(&vd->vdev_config_dirty_node))
1850 1818                  vdev_config_clean(vd);
1851 1819  
     1820 +        ASSERT0(vd->vdev_stat.vs_alloc);
     1821 +
1852 1822          /*
1853 1823           * Clean up the vdev namespace.
1854 1824           */
1855 1825          vdev_remove_make_hole_and_free(vd);
1856 1826  
1857 1827          if (ev != NULL)
1858 1828                  spa_event_post(ev);
1859 1829  
1860 1830          return (0);
1861 1831  }
1862 1832  
1863 1833  static int
1864 1834  spa_vdev_remove_top_check(vdev_t *vd)
1865 1835  {
1866 1836          spa_t *spa = vd->vdev_spa;
1867 1837  
1868 1838          if (vd != vd->vdev_top)
1869 1839                  return (SET_ERROR(ENOTSUP));
1870 1840  
1871 1841          if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REMOVAL))
1872 1842                  return (SET_ERROR(ENOTSUP));
1873 1843  
1874 1844          /* available space in the pool's normal class */
1875 1845          uint64_t available = dsl_dir_space_available(
1876 1846              spa->spa_dsl_pool->dp_root_dir, NULL, 0, B_TRUE);
1877 1847  
1878 1848          metaslab_class_t *mc = vd->vdev_mg->mg_class;
1879 1849  
1880 1850          /*
1881 1851           * When removing a vdev from an allocation class that has
1882 1852           * remaining vdevs, include available space from the class.
1883 1853           */
1884 1854          if (mc != spa_normal_class(spa) && mc->mc_groups > 1) {
1885 1855                  uint64_t class_avail = metaslab_class_get_space(mc) -
1886 1856                      metaslab_class_get_alloc(mc);
1887 1857  
1888 1858                  /* add class space, adjusted for overhead */
1889 1859                  available += (class_avail * 94) / 100;
1890 1860          }
1891 1861  
1892 1862          /*
1893 1863           * There has to be enough free space to remove the
1894 1864           * device and leave double the "slop" space (i.e. we
1895 1865           * must leave at least 3% of the pool free, in addition to
1896 1866           * the normal slop space).
1897 1867           */
1898 1868          if (available < vd->vdev_stat.vs_dspace + spa_get_slop_space(spa)) {
1899 1869                  return (SET_ERROR(ENOSPC));
1900 1870          }
1901 1871  
1902 1872          /*
1903 1873           * There can not be a removal in progress.
1904 1874           */
1905 1875          if (spa->spa_removing_phys.sr_state == DSS_SCANNING)
1906 1876                  return (SET_ERROR(EBUSY));
1907 1877  
1908 1878          /*
1909 1879           * The device must have all its data.
1910 1880           */
1911 1881          if (!vdev_dtl_empty(vd, DTL_MISSING) ||
1912 1882              !vdev_dtl_empty(vd, DTL_OUTAGE))
1913 1883                  return (SET_ERROR(EBUSY));
1914 1884  
1915 1885          /*
1916 1886           * The device must be healthy.
1917 1887           */
1918 1888          if (!vdev_readable(vd))
1919 1889                  return (SET_ERROR(EIO));
1920 1890  
1921 1891          /*
1922 1892           * All vdevs in normal class must have the same ashift.
1923 1893           */
1924 1894          if (spa->spa_max_ashift != spa->spa_min_ashift) {
1925 1895                  return (SET_ERROR(EINVAL));
1926 1896          }
1927 1897  
1928 1898          /*
1929 1899           * All vdevs in normal class must have the same ashift
1930 1900           * and not be raidz.
1931 1901           */
1932 1902          vdev_t *rvd = spa->spa_root_vdev;
1933 1903          int num_indirect = 0;
1934 1904          for (uint64_t id = 0; id < rvd->vdev_children; id++) {
1935 1905                  vdev_t *cvd = rvd->vdev_child[id];
1936 1906                  if (cvd->vdev_ashift != 0 && !cvd->vdev_islog)
1937 1907                          ASSERT3U(cvd->vdev_ashift, ==, spa->spa_max_ashift);
1938 1908                  if (cvd->vdev_ops == &vdev_indirect_ops)
1939 1909                          num_indirect++;
1940 1910                  if (!vdev_is_concrete(cvd))
1941 1911                          continue;
1942 1912                  if (cvd->vdev_ops == &vdev_raidz_ops)
1943 1913                          return (SET_ERROR(EINVAL));
1944 1914                  /*
1945 1915                   * Need the mirror to be mirror of leaf vdevs only
1946 1916                   */
1947 1917                  if (cvd->vdev_ops == &vdev_mirror_ops) {
1948 1918                          for (uint64_t cid = 0;
1949 1919                              cid < cvd->vdev_children; cid++) {
1950 1920                                  vdev_t *tmp = cvd->vdev_child[cid];
1951 1921                                  if (!tmp->vdev_ops->vdev_op_leaf)
1952 1922                                          return (SET_ERROR(EINVAL));
1953 1923                          }
1954 1924                  }
1955 1925          }
1956 1926  
1957 1927          return (0);
1958 1928  }
1959 1929  
1960 1930  /*
1961 1931   * Initiate removal of a top-level vdev, reducing the total space in the pool.
1962 1932   * The config lock is held for the specified TXG.  Once initiated,
1963 1933   * evacuation of all allocated space (copying it to other vdevs) happens
1964 1934   * in the background (see spa_vdev_remove_thread()), and can be canceled
1965 1935   * (see spa_vdev_remove_cancel()).  If successful, the vdev will
1966 1936   * be transformed to an indirect vdev (see spa_vdev_remove_complete()).
1967 1937   */
1968 1938  static int
1969 1939  spa_vdev_remove_top(vdev_t *vd, uint64_t *txg)
1970 1940  {
1971 1941          spa_t *spa = vd->vdev_spa;
1972 1942          int error;
1973 1943  
1974 1944          /*
1975 1945           * Check for errors up-front, so that we don't waste time
1976 1946           * passivating the metaslab group and clearing the ZIL if there
1977 1947           * are errors.
1978 1948           */
1979 1949          error = spa_vdev_remove_top_check(vd);
1980 1950          if (error != 0)
1981 1951                  return (error);
1982 1952  
1983 1953          /*
1984 1954           * Stop allocating from this vdev.  Note that we must check
1985 1955           * that this is not the only device in the pool before
1986 1956           * passivating, otherwise we will not be able to make
1987 1957           * progress because we can't allocate from any vdevs.
1988 1958           * The above check for sufficient free space serves this
1989 1959           * purpose.
1990 1960           */
1991 1961          metaslab_group_t *mg = vd->vdev_mg;
1992 1962          metaslab_group_passivate(mg);
1993 1963  
1994 1964          /*
1995 1965           * Wait for the youngest allocations and frees to sync,
1996 1966           * and then wait for the deferral of those frees to finish.
1997 1967           */
1998 1968          spa_vdev_config_exit(spa, NULL,
1999 1969              *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
2000 1970  
2001 1971          /*
2002 1972           * We must ensure that no "stubby" log blocks are allocated
2003 1973           * on the device to be removed.  These blocks could be
2004 1974           * written at any time, including while we are in the middle
2005 1975           * of copying them.
2006 1976           */
2007 1977          error = spa_reset_logs(spa);
2008 1978  
2009 1979          /*
2010 1980           * We stop any initializing that is currently in progress but leave
2011 1981           * the state as "active". This will allow the initializing to resume
2012 1982           * if the removal is canceled sometime later.
2013 1983           */
2014 1984          vdev_initialize_stop_all(vd, VDEV_INITIALIZE_ACTIVE);
2015 1985  
2016 1986          *txg = spa_vdev_config_enter(spa);
2017 1987  
2018 1988          /*
2019 1989           * Things might have changed while the config lock was dropped
2020 1990           * (e.g. space usage).  Check for errors again.
2021 1991           */
2022 1992          if (error == 0)
2023 1993                  error = spa_vdev_remove_top_check(vd);
2024 1994  
2025 1995          if (error != 0) {
2026 1996                  metaslab_group_activate(mg);
2027 1997                  spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART);
2028 1998                  return (error);
2029 1999          }
2030 2000  
2031 2001          vd->vdev_removing = B_TRUE;
2032 2002  
2033 2003          vdev_dirty_leaves(vd, VDD_DTL, *txg);
2034 2004          vdev_config_dirty(vd);
2035 2005          dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, *txg);
2036 2006          dsl_sync_task_nowait(spa->spa_dsl_pool,
2037 2007              vdev_remove_initiate_sync,
2038 2008              (void *)(uintptr_t)vd->vdev_id, 0, ZFS_SPACE_CHECK_NONE, tx);
2039 2009          dmu_tx_commit(tx);
2040 2010  
2041 2011          return (0);
2042 2012  }
2043 2013  
2044 2014  /*
2045 2015   * Remove a device from the pool.
2046 2016   *
2047 2017   * Removing a device from the vdev namespace requires several steps
2048 2018   * and can take a significant amount of time.  As a result we use
2049 2019   * the spa_vdev_config_[enter/exit] functions which allow us to
2050 2020   * grab and release the spa_config_lock while still holding the namespace
2051 2021   * lock.  During each step the configuration is synced out.
2052 2022   */
2053 2023  int
2054 2024  spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
2055 2025  {
2056 2026          vdev_t *vd;
2057 2027          nvlist_t **spares, **l2cache, *nv;
2058 2028          uint64_t txg = 0;
2059 2029          uint_t nspares, nl2cache;
2060 2030          int error = 0;
2061 2031          boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
2062 2032          sysevent_t *ev = NULL;
2063 2033  
2064 2034          ASSERT(spa_writeable(spa));
2065 2035  
2066 2036          if (!locked)
2067 2037                  txg = spa_vdev_enter(spa);
2068 2038  
2069 2039          ASSERT(MUTEX_HELD(&spa_namespace_lock));
2070 2040          if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
2071 2041                  error = (spa_has_checkpoint(spa)) ?
2072 2042                      ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
2073 2043  
2074 2044                  if (!locked)
2075 2045                          return (spa_vdev_exit(spa, NULL, txg, error));
2076 2046  
2077 2047                  return (error);
2078 2048          }
2079 2049  
2080 2050          vd = spa_lookup_by_guid(spa, guid, B_FALSE);
2081 2051  
2082 2052          if (spa->spa_spares.sav_vdevs != NULL &&
2083 2053              nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2084 2054              ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
2085 2055              (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
2086 2056                  /*
2087 2057                   * Only remove the hot spare if it's not currently in use
2088 2058                   * in this pool.
2089 2059                   */
2090 2060                  if (vd == NULL || unspare) {
2091 2061                          char *nvstr = fnvlist_lookup_string(nv,
2092 2062                              ZPOOL_CONFIG_PATH);
2093 2063                          spa_history_log_internal(spa, "vdev remove", NULL,
2094 2064                              "%s vdev (%s) %s", spa_name(spa),
2095 2065                              VDEV_TYPE_SPARE, nvstr);
2096 2066                          if (vd == NULL)
2097 2067                                  vd = spa_lookup_by_guid(spa, guid, B_TRUE);
2098 2068                          ev = spa_event_create(spa, vd, NULL,
2099 2069                              ESC_ZFS_VDEV_REMOVE_AUX);
2100 2070                          spa_vdev_remove_aux(spa->spa_spares.sav_config,
2101 2071                              ZPOOL_CONFIG_SPARES, spares, nspares, nv);
2102 2072                          spa_load_spares(spa);
2103 2073                          spa->spa_spares.sav_sync = B_TRUE;
2104 2074                  } else {
2105 2075                          error = SET_ERROR(EBUSY);
2106 2076                  }
2107 2077          } else if (spa->spa_l2cache.sav_vdevs != NULL &&
2108 2078              nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2109 2079              ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
2110 2080              (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
2111 2081                  char *nvstr = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
2112 2082                  spa_history_log_internal(spa, "vdev remove", NULL,
2113 2083                      "%s vdev (%s) %s", spa_name(spa), VDEV_TYPE_L2CACHE, nvstr);
2114 2084                  /*
2115 2085                   * Cache devices can always be removed.
2116 2086                   */
2117 2087                  vd = spa_lookup_by_guid(spa, guid, B_TRUE);
2118 2088                  ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_AUX);
2119 2089                  spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
2120 2090                      ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
2121 2091                  spa_load_l2cache(spa);
2122 2092                  spa->spa_l2cache.sav_sync = B_TRUE;
2123 2093          } else if (vd != NULL && vd->vdev_islog) {
2124 2094                  ASSERT(!locked);
2125 2095                  error = spa_vdev_remove_log(vd, &txg);
2126 2096          } else if (vd != NULL) {
2127 2097                  ASSERT(!locked);
2128 2098                  error = spa_vdev_remove_top(vd, &txg);
2129 2099          } else {
2130 2100                  /*
2131 2101                   * There is no vdev of any kind with the specified guid.
2132 2102                   */
2133 2103                  error = SET_ERROR(ENOENT);
2134 2104          }
2135 2105  
2136 2106          if (!locked)
2137 2107                  error = spa_vdev_exit(spa, NULL, txg, error);
2138 2108  
2139 2109          if (ev != NULL) {
2140 2110                  if (error != 0) {
2141 2111                          spa_event_discard(ev);
2142 2112                  } else {
2143 2113                          spa_event_post(ev);
2144 2114                  }
2145 2115          }
2146 2116  
2147 2117          return (error);
2148 2118  }
2149 2119  
2150 2120  int
2151 2121  spa_removal_get_stats(spa_t *spa, pool_removal_stat_t *prs)
2152 2122  {
2153 2123          prs->prs_state = spa->spa_removing_phys.sr_state;
2154 2124  
2155 2125          if (prs->prs_state == DSS_NONE)
2156 2126                  return (SET_ERROR(ENOENT));
2157 2127  
2158 2128          prs->prs_removing_vdev = spa->spa_removing_phys.sr_removing_vdev;
2159 2129          prs->prs_start_time = spa->spa_removing_phys.sr_start_time;
2160 2130          prs->prs_end_time = spa->spa_removing_phys.sr_end_time;
2161 2131          prs->prs_to_copy = spa->spa_removing_phys.sr_to_copy;
2162 2132          prs->prs_copied = spa->spa_removing_phys.sr_copied;
2163 2133  
2164 2134          if (spa->spa_vdev_removal != NULL) {
2165 2135                  for (int i = 0; i < TXG_SIZE; i++) {
2166 2136                          prs->prs_copied +=
2167 2137                              spa->spa_vdev_removal->svr_bytes_done[i];
2168 2138                  }
2169 2139          }
2170 2140  
2171 2141          prs->prs_mapping_memory = 0;
2172 2142          uint64_t indirect_vdev_id =
2173 2143              spa->spa_removing_phys.sr_prev_indirect_vdev;
2174 2144          while (indirect_vdev_id != -1) {
2175 2145                  vdev_t *vd = spa->spa_root_vdev->vdev_child[indirect_vdev_id];
2176 2146                  vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
2177 2147                  vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
2178 2148  
2179 2149                  ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2180 2150                  prs->prs_mapping_memory += vdev_indirect_mapping_size(vim);
2181 2151                  indirect_vdev_id = vic->vic_prev_indirect_vdev;
2182 2152          }
2183 2153  
2184 2154          return (0);
2185 2155  }
  
    | 
      ↓ open down ↓ | 
    324 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX