Print this page
4101 metaslab_debug should allow for fine-grained control
4102 space_maps should store more information about themselves
4103 space map object blocksize should be increased
4104 ::spa_space no longer works
4105 removing a mirrored log device results in a leaked object
4106 asynchronously load metaslab
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Sebastien Roy <seb@delphix.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/uts/common/fs/zfs/vdev.c
          +++ new/usr/src/uts/common/fs/zfs/vdev.c
↓ open down ↓ 28 lines elided ↑ open up ↑
  29   29  #include <sys/fm/fs/zfs.h>
  30   30  #include <sys/spa.h>
  31   31  #include <sys/spa_impl.h>
  32   32  #include <sys/dmu.h>
  33   33  #include <sys/dmu_tx.h>
  34   34  #include <sys/vdev_impl.h>
  35   35  #include <sys/uberblock_impl.h>
  36   36  #include <sys/metaslab.h>
  37   37  #include <sys/metaslab_impl.h>
  38   38  #include <sys/space_map.h>
       39 +#include <sys/space_reftree.h>
  39   40  #include <sys/zio.h>
  40   41  #include <sys/zap.h>
  41   42  #include <sys/fs/zfs.h>
  42   43  #include <sys/arc.h>
  43   44  #include <sys/zil.h>
  44   45  #include <sys/dsl_scan.h>
  45   46  
  46   47  /*
  47   48   * Virtual device management.
  48   49   */
↓ open down ↓ 263 lines elided ↑ open up ↑
 312  313          vd->vdev_guid = guid;
 313  314          vd->vdev_guid_sum = guid;
 314  315          vd->vdev_ops = ops;
 315  316          vd->vdev_state = VDEV_STATE_CLOSED;
 316  317          vd->vdev_ishole = (ops == &vdev_hole_ops);
 317  318  
 318  319          mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
 319  320          mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
 320  321          mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
 321  322          for (int t = 0; t < DTL_TYPES; t++) {
 322      -                space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
      323 +                vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
 323  324                      &vd->vdev_dtl_lock);
 324  325          }
 325  326          txg_list_create(&vd->vdev_ms_list,
 326  327              offsetof(struct metaslab, ms_txg_node));
 327  328          txg_list_create(&vd->vdev_dtl_list,
 328  329              offsetof(struct vdev, vdev_dtl_node));
 329  330          vd->vdev_stat.vs_timestamp = gethrtime();
 330  331          vdev_queue_init(vd);
 331  332          vdev_cache_init(vd);
 332  333  
↓ open down ↓ 165 lines elided ↑ open up ↑
 498  499          }
 499  500  
 500  501          /*
 501  502           * If we're a leaf vdev, try to load the DTL object and other state.
 502  503           */
 503  504          if (vd->vdev_ops->vdev_op_leaf &&
 504  505              (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
 505  506              alloctype == VDEV_ALLOC_ROOTPOOL)) {
 506  507                  if (alloctype == VDEV_ALLOC_LOAD) {
 507  508                          (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
 508      -                            &vd->vdev_dtl_smo.smo_object);
      509 +                            &vd->vdev_dtl_object);
 509  510                          (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
 510  511                              &vd->vdev_unspare);
 511  512                  }
 512  513  
 513  514                  if (alloctype == VDEV_ALLOC_ROOTPOOL) {
 514  515                          uint64_t spare = 0;
 515  516  
 516  517                          if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
 517  518                              &spare) == 0 && spare)
 518  519                                  spa_spare_add(vd);
↓ open down ↓ 101 lines elided ↑ open up ↑
 620  621  
 621  622          if (vd->vdev_isspare)
 622  623                  spa_spare_remove(vd);
 623  624          if (vd->vdev_isl2cache)
 624  625                  spa_l2cache_remove(vd);
 625  626  
 626  627          txg_list_destroy(&vd->vdev_ms_list);
 627  628          txg_list_destroy(&vd->vdev_dtl_list);
 628  629  
 629  630          mutex_enter(&vd->vdev_dtl_lock);
      631 +        space_map_close(vd->vdev_dtl_sm);
 630  632          for (int t = 0; t < DTL_TYPES; t++) {
 631      -                space_map_unload(&vd->vdev_dtl[t]);
 632      -                space_map_destroy(&vd->vdev_dtl[t]);
      633 +                range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
      634 +                range_tree_destroy(vd->vdev_dtl[t]);
 633  635          }
 634  636          mutex_exit(&vd->vdev_dtl_lock);
 635  637  
 636  638          mutex_destroy(&vd->vdev_dtl_lock);
 637  639          mutex_destroy(&vd->vdev_stat_lock);
 638  640          mutex_destroy(&vd->vdev_probe_lock);
 639  641  
 640  642          if (vd == spa->spa_root_vdev)
 641  643                  spa->spa_root_vdev = NULL;
 642  644  
↓ open down ↓ 190 lines elided ↑ open up ↑
 833  835  
 834  836          if (oldc != 0) {
 835  837                  bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
 836  838                  kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
 837  839          }
 838  840  
 839  841          vd->vdev_ms = mspp;
 840  842          vd->vdev_ms_count = newc;
 841  843  
 842  844          for (m = oldc; m < newc; m++) {
 843      -                space_map_obj_t smo = { 0, 0, 0 };
      845 +                uint64_t object = 0;
      846 +
 844  847                  if (txg == 0) {
 845      -                        uint64_t object = 0;
 846  848                          error = dmu_read(mos, vd->vdev_ms_array,
 847  849                              m * sizeof (uint64_t), sizeof (uint64_t), &object,
 848  850                              DMU_READ_PREFETCH);
 849  851                          if (error)
 850  852                                  return (error);
 851      -                        if (object != 0) {
 852      -                                dmu_buf_t *db;
 853      -                                error = dmu_bonus_hold(mos, object, FTAG, &db);
 854      -                                if (error)
 855      -                                        return (error);
 856      -                                ASSERT3U(db->db_size, >=, sizeof (smo));
 857      -                                bcopy(db->db_data, &smo, sizeof (smo));
 858      -                                ASSERT3U(smo.smo_object, ==, object);
 859      -                                dmu_buf_rele(db, FTAG);
 860      -                        }
 861  853                  }
 862      -                vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
 863      -                    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
      854 +                vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, m, object, txg);
 864  855          }
 865  856  
 866  857          if (txg == 0)
 867  858                  spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
 868  859  
 869  860          /*
 870  861           * If the vdev is being removed we don't activate
 871  862           * the metaslabs since we want to ensure that no new
 872  863           * allocations are performed on this device.
 873  864           */
↓ open down ↓ 7 lines elided ↑ open up ↑
 881  872  }
 882  873  
 883  874  void
 884  875  vdev_metaslab_fini(vdev_t *vd)
 885  876  {
 886  877          uint64_t m;
 887  878          uint64_t count = vd->vdev_ms_count;
 888  879  
 889  880          if (vd->vdev_ms != NULL) {
 890  881                  metaslab_group_passivate(vd->vdev_mg);
 891      -                for (m = 0; m < count; m++)
 892      -                        if (vd->vdev_ms[m] != NULL)
 893      -                                metaslab_fini(vd->vdev_ms[m]);
      882 +                for (m = 0; m < count; m++) {
      883 +                        metaslab_t *msp = vd->vdev_ms[m];
      884 +
      885 +                        if (msp != NULL)
      886 +                                metaslab_fini(msp);
      887 +                }
 894  888                  kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
 895  889                  vd->vdev_ms = NULL;
 896  890          }
 897  891  }
 898  892  
 899  893  typedef struct vdev_probe_stats {
 900  894          boolean_t       vps_readable;
 901  895          boolean_t       vps_writeable;
 902  896          int             vps_flags;
 903  897  } vdev_probe_stats_t;
↓ open down ↓ 630 lines elided ↑ open up ↑
1534 1528           * there are any components we can't open.
1535 1529           */
1536 1530          error = vdev_open(vd);
1537 1531  
1538 1532          if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1539 1533                  vdev_close(vd);
1540 1534                  return (error ? error : ENXIO);
1541 1535          }
1542 1536  
1543 1537          /*
1544      -         * Recursively initialize all labels.
     1538 +         * Recursively load DTLs and initialize all labels.
1545 1539           */
1546      -        if ((error = vdev_label_init(vd, txg, isreplacing ?
     1540 +        if ((error = vdev_dtl_load(vd)) != 0 ||
     1541 +            (error = vdev_label_init(vd, txg, isreplacing ?
1547 1542              VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1548 1543                  vdev_close(vd);
1549 1544                  return (error);
1550 1545          }
1551 1546  
1552 1547          return (0);
1553 1548  }
1554 1549  
1555 1550  void
1556 1551  vdev_metaslab_set_size(vdev_t *vd)
↓ open down ↓ 15 lines elided ↑ open up ↑
1572 1567  
1573 1568          if (flags & VDD_METASLAB)
1574 1569                  (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1575 1570  
1576 1571          if (flags & VDD_DTL)
1577 1572                  (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1578 1573  
1579 1574          (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1580 1575  }
1581 1576  
     1577 +void
     1578 +vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
     1579 +{
     1580 +        for (int c = 0; c < vd->vdev_children; c++)
     1581 +                vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
     1582 +
     1583 +        if (vd->vdev_ops->vdev_op_leaf)
     1584 +                vdev_dirty(vd->vdev_top, flags, vd, txg);
     1585 +}
     1586 +
1582 1587  /*
1583 1588   * DTLs.
1584 1589   *
1585 1590   * A vdev's DTL (dirty time log) is the set of transaction groups for which
1586 1591   * the vdev has less than perfect replication.  There are four kinds of DTL:
1587 1592   *
1588 1593   * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1589 1594   *
1590 1595   * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1591 1596   *
↓ open down ↓ 21 lines elided ↑ open up ↑
1613 1618   * two child DTL_MISSING maps.
1614 1619   *
1615 1620   * It should be clear from the above that to compute the DTLs and outage maps
1616 1621   * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1617 1622   * Therefore, that is all we keep on disk.  When loading the pool, or after
1618 1623   * a configuration change, we generate all other DTLs from first principles.
1619 1624   */
1620 1625  void
1621 1626  vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1622 1627  {
1623      -        space_map_t *sm = &vd->vdev_dtl[t];
     1628 +        range_tree_t *rt = vd->vdev_dtl[t];
1624 1629  
1625 1630          ASSERT(t < DTL_TYPES);
1626 1631          ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1627 1632          ASSERT(spa_writeable(vd->vdev_spa));
1628 1633  
1629      -        mutex_enter(sm->sm_lock);
1630      -        if (!space_map_contains(sm, txg, size))
1631      -                space_map_add(sm, txg, size);
1632      -        mutex_exit(sm->sm_lock);
     1634 +        mutex_enter(rt->rt_lock);
     1635 +        if (!range_tree_contains(rt, txg, size))
     1636 +                range_tree_add(rt, txg, size);
     1637 +        mutex_exit(rt->rt_lock);
1633 1638  }
1634 1639  
1635 1640  boolean_t
1636 1641  vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1637 1642  {
1638      -        space_map_t *sm = &vd->vdev_dtl[t];
     1643 +        range_tree_t *rt = vd->vdev_dtl[t];
1639 1644          boolean_t dirty = B_FALSE;
1640 1645  
1641 1646          ASSERT(t < DTL_TYPES);
1642 1647          ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1643 1648  
1644      -        mutex_enter(sm->sm_lock);
1645      -        if (sm->sm_space != 0)
1646      -                dirty = space_map_contains(sm, txg, size);
1647      -        mutex_exit(sm->sm_lock);
     1649 +        mutex_enter(rt->rt_lock);
     1650 +        if (range_tree_space(rt) != 0)
     1651 +                dirty = range_tree_contains(rt, txg, size);
     1652 +        mutex_exit(rt->rt_lock);
1648 1653  
1649 1654          return (dirty);
1650 1655  }
1651 1656  
1652 1657  boolean_t
1653 1658  vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1654 1659  {
1655      -        space_map_t *sm = &vd->vdev_dtl[t];
     1660 +        range_tree_t *rt = vd->vdev_dtl[t];
1656 1661          boolean_t empty;
1657 1662  
1658      -        mutex_enter(sm->sm_lock);
1659      -        empty = (sm->sm_space == 0);
1660      -        mutex_exit(sm->sm_lock);
     1663 +        mutex_enter(rt->rt_lock);
     1664 +        empty = (range_tree_space(rt) == 0);
     1665 +        mutex_exit(rt->rt_lock);
1661 1666  
1662 1667          return (empty);
1663 1668  }
1664 1669  
1665 1670  /*
1666 1671   * Returns the lowest txg in the DTL range.
1667 1672   */
1668 1673  static uint64_t
1669 1674  vdev_dtl_min(vdev_t *vd)
1670 1675  {
1671      -        space_seg_t *ss;
     1676 +        range_seg_t *rs;
1672 1677  
1673 1678          ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1674      -        ASSERT3U(vd->vdev_dtl[DTL_MISSING].sm_space, !=, 0);
     1679 +        ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1675 1680          ASSERT0(vd->vdev_children);
1676 1681  
1677      -        ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
1678      -        return (ss->ss_start - 1);
     1682 +        rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
     1683 +        return (rs->rs_start - 1);
1679 1684  }
1680 1685  
1681 1686  /*
1682 1687   * Returns the highest txg in the DTL.
1683 1688   */
1684 1689  static uint64_t
1685 1690  vdev_dtl_max(vdev_t *vd)
1686 1691  {
1687      -        space_seg_t *ss;
     1692 +        range_seg_t *rs;
1688 1693  
1689 1694          ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1690      -        ASSERT3U(vd->vdev_dtl[DTL_MISSING].sm_space, !=, 0);
     1695 +        ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1691 1696          ASSERT0(vd->vdev_children);
1692 1697  
1693      -        ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
1694      -        return (ss->ss_end);
     1698 +        rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
     1699 +        return (rs->rs_end);
1695 1700  }
1696 1701  
1697 1702  /*
1698 1703   * Determine if a resilvering vdev should remove any DTL entries from
1699 1704   * its range. If the vdev was resilvering for the entire duration of the
1700 1705   * scan then it should excise that range from its DTLs. Otherwise, this
1701 1706   * vdev is considered partially resilvered and should leave its DTL
1702 1707   * entries intact. The comment in vdev_dtl_reassess() describes how we
1703 1708   * excise the DTLs.
1704 1709   */
1705 1710  static boolean_t
1706 1711  vdev_dtl_should_excise(vdev_t *vd)
1707 1712  {
1708 1713          spa_t *spa = vd->vdev_spa;
1709 1714          dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1710 1715  
1711 1716          ASSERT0(scn->scn_phys.scn_errors);
1712 1717          ASSERT0(vd->vdev_children);
1713 1718  
1714 1719          if (vd->vdev_resilver_txg == 0 ||
1715      -            vd->vdev_dtl[DTL_MISSING].sm_space == 0)
     1720 +            range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1716 1721                  return (B_TRUE);
1717 1722  
1718 1723          /*
1719 1724           * When a resilver is initiated the scan will assign the scn_max_txg
1720 1725           * value to the highest txg value that exists in all DTLs. If this
1721 1726           * device's max DTL is not part of this scan (i.e. it is not in
1722 1727           * the range (scn_min_txg, scn_max_txg] then it is not eligible
1723 1728           * for excision.
1724 1729           */
1725 1730          if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
↓ open down ↓ 49 lines elided ↑ open up ↑
1775 1780                           * There's little trick here: to excise the beginning
1776 1781                           * of the DTL_MISSING map, we put it into a reference
1777 1782                           * tree and then add a segment with refcnt -1 that
1778 1783                           * covers the range [0, scrub_txg).  This means
1779 1784                           * that each txg in that range has refcnt -1 or 0.
1780 1785                           * We then add DTL_SCRUB with a refcnt of 2, so that
1781 1786                           * entries in the range [0, scrub_txg) will have a
1782 1787                           * positive refcnt -- either 1 or 2.  We then convert
1783 1788                           * the reference tree into the new DTL_MISSING map.
1784 1789                           */
1785      -                        space_map_ref_create(&reftree);
1786      -                        space_map_ref_add_map(&reftree,
1787      -                            &vd->vdev_dtl[DTL_MISSING], 1);
1788      -                        space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
1789      -                        space_map_ref_add_map(&reftree,
1790      -                            &vd->vdev_dtl[DTL_SCRUB], 2);
1791      -                        space_map_ref_generate_map(&reftree,
1792      -                            &vd->vdev_dtl[DTL_MISSING], 1);
1793      -                        space_map_ref_destroy(&reftree);
     1790 +                        space_reftree_create(&reftree);
     1791 +                        space_reftree_add_map(&reftree,
     1792 +                            vd->vdev_dtl[DTL_MISSING], 1);
     1793 +                        space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
     1794 +                        space_reftree_add_map(&reftree,
     1795 +                            vd->vdev_dtl[DTL_SCRUB], 2);
     1796 +                        space_reftree_generate_map(&reftree,
     1797 +                            vd->vdev_dtl[DTL_MISSING], 1);
     1798 +                        space_reftree_destroy(&reftree);
1794 1799                  }
1795      -                space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
1796      -                space_map_walk(&vd->vdev_dtl[DTL_MISSING],
1797      -                    space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
     1800 +                range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
     1801 +                range_tree_walk(vd->vdev_dtl[DTL_MISSING],
     1802 +                    range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1798 1803                  if (scrub_done)
1799      -                        space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
1800      -                space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
     1804 +                        range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
     1805 +                range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
1801 1806                  if (!vdev_readable(vd))
1802      -                        space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
     1807 +                        range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
1803 1808                  else
1804      -                        space_map_walk(&vd->vdev_dtl[DTL_MISSING],
1805      -                            space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
     1809 +                        range_tree_walk(vd->vdev_dtl[DTL_MISSING],
     1810 +                            range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1806 1811  
1807 1812                  /*
1808 1813                   * If the vdev was resilvering and no longer has any
1809 1814                   * DTLs then reset its resilvering flag.
1810 1815                   */
1811 1816                  if (vd->vdev_resilver_txg != 0 &&
1812      -                    vd->vdev_dtl[DTL_MISSING].sm_space == 0 &&
1813      -                    vd->vdev_dtl[DTL_OUTAGE].sm_space == 0)
     1817 +                    range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
     1818 +                    range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
1814 1819                          vd->vdev_resilver_txg = 0;
1815 1820  
1816 1821                  mutex_exit(&vd->vdev_dtl_lock);
1817 1822  
1818 1823                  if (txg != 0)
1819 1824                          vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1820 1825                  return;
1821 1826          }
1822 1827  
1823 1828          mutex_enter(&vd->vdev_dtl_lock);
↓ open down ↓ 1 lines elided ↑ open up ↑
1825 1830                  /* account for child's outage in parent's missing map */
1826 1831                  int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1827 1832                  if (t == DTL_SCRUB)
1828 1833                          continue;                       /* leaf vdevs only */
1829 1834                  if (t == DTL_PARTIAL)
1830 1835                          minref = 1;                     /* i.e. non-zero */
1831 1836                  else if (vd->vdev_nparity != 0)
1832 1837                          minref = vd->vdev_nparity + 1;  /* RAID-Z */
1833 1838                  else
1834 1839                          minref = vd->vdev_children;     /* any kind of mirror */
1835      -                space_map_ref_create(&reftree);
     1840 +                space_reftree_create(&reftree);
1836 1841                  for (int c = 0; c < vd->vdev_children; c++) {
1837 1842                          vdev_t *cvd = vd->vdev_child[c];
1838 1843                          mutex_enter(&cvd->vdev_dtl_lock);
1839      -                        space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
     1844 +                        space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
1840 1845                          mutex_exit(&cvd->vdev_dtl_lock);
1841 1846                  }
1842      -                space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
1843      -                space_map_ref_destroy(&reftree);
     1847 +                space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
     1848 +                space_reftree_destroy(&reftree);
1844 1849          }
1845 1850          mutex_exit(&vd->vdev_dtl_lock);
1846 1851  }
1847 1852  
1848      -static int
     1853 +int
1849 1854  vdev_dtl_load(vdev_t *vd)
1850 1855  {
1851 1856          spa_t *spa = vd->vdev_spa;
1852      -        space_map_obj_t *smo = &vd->vdev_dtl_smo;
1853 1857          objset_t *mos = spa->spa_meta_objset;
1854      -        dmu_buf_t *db;
1855      -        int error;
     1858 +        int error = 0;
1856 1859  
1857      -        ASSERT(vd->vdev_children == 0);
     1860 +        if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
     1861 +                ASSERT(!vd->vdev_ishole);
1858 1862  
1859      -        if (smo->smo_object == 0)
1860      -                return (0);
     1863 +                error = space_map_open(&vd->vdev_dtl_sm, mos,
     1864 +                    vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
     1865 +                if (error)
     1866 +                        return (error);
     1867 +                ASSERT(vd->vdev_dtl_sm != NULL);
1861 1868  
1862      -        ASSERT(!vd->vdev_ishole);
     1869 +                mutex_enter(&vd->vdev_dtl_lock);
1863 1870  
1864      -        if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1865      -                return (error);
     1871 +                /*
     1872 +                 * Now that we've opened the space_map we need to update
     1873 +                 * the in-core DTL.
     1874 +                 */
     1875 +                space_map_update(vd->vdev_dtl_sm);
1866 1876  
1867      -        ASSERT3U(db->db_size, >=, sizeof (*smo));
1868      -        bcopy(db->db_data, smo, sizeof (*smo));
1869      -        dmu_buf_rele(db, FTAG);
     1877 +                error = space_map_load(vd->vdev_dtl_sm,
     1878 +                    vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
     1879 +                mutex_exit(&vd->vdev_dtl_lock);
1870 1880  
1871      -        mutex_enter(&vd->vdev_dtl_lock);
1872      -        error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
1873      -            NULL, SM_ALLOC, smo, mos);
1874      -        mutex_exit(&vd->vdev_dtl_lock);
     1881 +                return (error);
     1882 +        }
1875 1883  
     1884 +        for (int c = 0; c < vd->vdev_children; c++) {
     1885 +                error = vdev_dtl_load(vd->vdev_child[c]);
     1886 +                if (error != 0)
     1887 +                        break;
     1888 +        }
     1889 +
1876 1890          return (error);
1877 1891  }
1878 1892  
1879 1893  void
1880 1894  vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1881 1895  {
1882 1896          spa_t *spa = vd->vdev_spa;
1883      -        space_map_obj_t *smo = &vd->vdev_dtl_smo;
1884      -        space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
     1897 +        range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
1885 1898          objset_t *mos = spa->spa_meta_objset;
1886      -        space_map_t smsync;
1887      -        kmutex_t smlock;
1888      -        dmu_buf_t *db;
     1899 +        range_tree_t *rtsync;
     1900 +        kmutex_t rtlock;
1889 1901          dmu_tx_t *tx;
     1902 +        uint64_t object = space_map_object(vd->vdev_dtl_sm);
1890 1903  
1891 1904          ASSERT(!vd->vdev_ishole);
     1905 +        ASSERT(vd->vdev_ops->vdev_op_leaf);
1892 1906  
1893 1907          tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1894 1908  
1895      -        if (vd->vdev_detached) {
1896      -                if (smo->smo_object != 0) {
1897      -                        int err = dmu_object_free(mos, smo->smo_object, tx);
1898      -                        ASSERT0(err);
1899      -                        smo->smo_object = 0;
1900      -                }
     1909 +        if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
     1910 +                mutex_enter(&vd->vdev_dtl_lock);
     1911 +                space_map_free(vd->vdev_dtl_sm, tx);
     1912 +                space_map_close(vd->vdev_dtl_sm);
     1913 +                vd->vdev_dtl_sm = NULL;
     1914 +                mutex_exit(&vd->vdev_dtl_lock);
1901 1915                  dmu_tx_commit(tx);
1902 1916                  return;
1903 1917          }
1904 1918  
1905      -        if (smo->smo_object == 0) {
1906      -                ASSERT(smo->smo_objsize == 0);
1907      -                ASSERT(smo->smo_alloc == 0);
1908      -                smo->smo_object = dmu_object_alloc(mos,
1909      -                    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1910      -                    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1911      -                ASSERT(smo->smo_object != 0);
1912      -                vdev_config_dirty(vd->vdev_top);
     1919 +        if (vd->vdev_dtl_sm == NULL) {
     1920 +                uint64_t new_object;
     1921 +
     1922 +                new_object = space_map_alloc(mos, tx);
     1923 +                VERIFY3U(new_object, !=, 0);
     1924 +
     1925 +                VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
     1926 +                    0, -1ULL, 0, &vd->vdev_dtl_lock));
     1927 +                ASSERT(vd->vdev_dtl_sm != NULL);
1913 1928          }
1914 1929  
1915      -        mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
     1930 +        mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
1916 1931  
1917      -        space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1918      -            &smlock);
     1932 +        rtsync = range_tree_create(NULL, NULL, &rtlock);
1919 1933  
1920      -        mutex_enter(&smlock);
     1934 +        mutex_enter(&rtlock);
1921 1935  
1922 1936          mutex_enter(&vd->vdev_dtl_lock);
1923      -        space_map_walk(sm, space_map_add, &smsync);
     1937 +        range_tree_walk(rt, range_tree_add, rtsync);
1924 1938          mutex_exit(&vd->vdev_dtl_lock);
1925 1939  
1926      -        space_map_truncate(smo, mos, tx);
1927      -        space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1928      -        space_map_vacate(&smsync, NULL, NULL);
     1940 +        space_map_truncate(vd->vdev_dtl_sm, tx);
     1941 +        space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
     1942 +        range_tree_vacate(rtsync, NULL, NULL);
1929 1943  
1930      -        space_map_destroy(&smsync);
     1944 +        range_tree_destroy(rtsync);
1931 1945  
1932      -        mutex_exit(&smlock);
1933      -        mutex_destroy(&smlock);
     1946 +        mutex_exit(&rtlock);
     1947 +        mutex_destroy(&rtlock);
1934 1948  
1935      -        VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1936      -        dmu_buf_will_dirty(db, tx);
1937      -        ASSERT3U(db->db_size, >=, sizeof (*smo));
1938      -        bcopy(smo, db->db_data, sizeof (*smo));
1939      -        dmu_buf_rele(db, FTAG);
     1949 +        /*
     1950 +         * If the object for the space map has changed then dirty
     1951 +         * the top level so that we update the config.
     1952 +         */
     1953 +        if (object != space_map_object(vd->vdev_dtl_sm)) {
     1954 +                zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
     1955 +                    "new object %llu", txg, spa_name(spa), object,
     1956 +                    space_map_object(vd->vdev_dtl_sm));
     1957 +                vdev_config_dirty(vd->vdev_top);
     1958 +        }
1940 1959  
1941 1960          dmu_tx_commit(tx);
     1961 +
     1962 +        mutex_enter(&vd->vdev_dtl_lock);
     1963 +        space_map_update(vd->vdev_dtl_sm);
     1964 +        mutex_exit(&vd->vdev_dtl_lock);
1942 1965  }
1943 1966  
1944 1967  /*
1945 1968   * Determine whether the specified vdev can be offlined/detached/removed
1946 1969   * without losing data.
1947 1970   */
1948 1971  boolean_t
1949 1972  vdev_dtl_required(vdev_t *vd)
1950 1973  {
1951 1974          spa_t *spa = vd->vdev_spa;
↓ open down ↓ 28 lines elided ↑ open up ↑
1980 2003   */
1981 2004  boolean_t
1982 2005  vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
1983 2006  {
1984 2007          boolean_t needed = B_FALSE;
1985 2008          uint64_t thismin = UINT64_MAX;
1986 2009          uint64_t thismax = 0;
1987 2010  
1988 2011          if (vd->vdev_children == 0) {
1989 2012                  mutex_enter(&vd->vdev_dtl_lock);
1990      -                if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
     2013 +                if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
1991 2014                      vdev_writeable(vd)) {
1992 2015  
1993 2016                          thismin = vdev_dtl_min(vd);
1994 2017                          thismax = vdev_dtl_max(vd);
1995 2018                          needed = B_TRUE;
1996 2019                  }
1997 2020                  mutex_exit(&vd->vdev_dtl_lock);
1998 2021          } else {
1999 2022                  for (int c = 0; c < vd->vdev_children; c++) {
2000 2023                          vdev_t *cvd = vd->vdev_child[c];
↓ open down ↓ 84 lines elided ↑ open up ↑
2085 2108  
2086 2109  void
2087 2110  vdev_remove(vdev_t *vd, uint64_t txg)
2088 2111  {
2089 2112          spa_t *spa = vd->vdev_spa;
2090 2113          objset_t *mos = spa->spa_meta_objset;
2091 2114          dmu_tx_t *tx;
2092 2115  
2093 2116          tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2094 2117  
2095      -        if (vd->vdev_dtl_smo.smo_object) {
2096      -                ASSERT0(vd->vdev_dtl_smo.smo_alloc);
2097      -                (void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
2098      -                vd->vdev_dtl_smo.smo_object = 0;
2099      -        }
2100      -
2101 2118          if (vd->vdev_ms != NULL) {
2102 2119                  for (int m = 0; m < vd->vdev_ms_count; m++) {
2103 2120                          metaslab_t *msp = vd->vdev_ms[m];
2104 2121  
2105      -                        if (msp == NULL || msp->ms_smo.smo_object == 0)
     2122 +                        if (msp == NULL || msp->ms_sm == NULL)
2106 2123                                  continue;
2107 2124  
2108      -                        ASSERT0(msp->ms_smo.smo_alloc);
2109      -                        (void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
2110      -                        msp->ms_smo.smo_object = 0;
     2125 +                        mutex_enter(&msp->ms_lock);
     2126 +                        VERIFY0(space_map_allocated(msp->ms_sm));
     2127 +                        space_map_free(msp->ms_sm, tx);
     2128 +                        space_map_close(msp->ms_sm);
     2129 +                        msp->ms_sm = NULL;
     2130 +                        mutex_exit(&msp->ms_lock);
2111 2131                  }
2112 2132          }
2113 2133  
2114 2134          if (vd->vdev_ms_array) {
2115 2135                  (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2116 2136                  vd->vdev_ms_array = 0;
2117      -                vd->vdev_ms_shift = 0;
2118 2137          }
2119 2138          dmu_tx_commit(tx);
2120 2139  }
2121 2140  
2122 2141  void
2123 2142  vdev_sync_done(vdev_t *vd, uint64_t txg)
2124 2143  {
2125 2144          metaslab_t *msp;
2126 2145          boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2127 2146  
↓ open down ↓ 1156 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX