fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
8d18220deb04ec7b12410cd90deb4d45e66d49bfMark J Musante * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
daec38ecb4fb5e73e4ca9e99be84f6b8c50c02faJoe Stein * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
9dc3941c735ef88de46e850f745aa556d3a071a5Sašo Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER))
e05725b117836db173257fae43fb0746eb857fb5bonwickuint64_t metaslab_gang_bang = SPA_MAXBLOCKSIZE + 1; /* force gang blocks */
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * The in-core space map representation is more compact than its on-disk form.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * The zfs_condense_pct determines how much more compact the in-core
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * space map representation must be before we compact it on-disk.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * Values should be greater than or equal to 100.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * Condensing a metaslab is not guaranteed to actually reduce the amount of
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * space used on disk. In particular, a space map uses data in increments of
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens * MAX(1 << ashift, space_map_blksize), so a metaslab might use the
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * same number of blocks after condensing. Since the goal of condensing is to
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * reduce the number of IOPs required to read the space map, we only want to
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * condense when we can be sure we will reduce the number of blocks used by the
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * space map. Unfortunately, we cannot precisely compute whether or not this is
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * the case in metaslab_should_condense since we are holding ms_lock. Instead,
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * we apply the following heuristic: do not condense a spacemap unless the
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * uncondensed size consumes greater than zfs_metaslab_condense_block_threshold
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * The zfs_mg_noalloc_threshold defines which metaslab groups should
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * be eligible for allocation. The value is defined as a percentage of
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * free space. Metaslab groups that have more free space than
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * zfs_mg_noalloc_threshold are always eligible for allocations. Once
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * a metaslab group's free space is less than or equal to the
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * zfs_mg_noalloc_threshold the allocator will avoid allocating to that
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * group unless all groups in the pool have reached zfs_mg_noalloc_threshold.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * Once all groups in the pool reach zfs_mg_noalloc_threshold then all
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * groups are allowed to accept allocations. Gang blocks are always
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * eligible to allocate on any metaslab group. The default value of 0 means
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * no metaslab group will be excluded based on this criterion.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Metaslab groups are considered eligible for allocations if their
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * fragmenation metric (measured as a percentage) is less than or equal to
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * zfs_mg_fragmentation_threshold. If a metaslab group exceeds this threshold
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * then it will be skipped unless all metaslab groups within the metaslab
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * class have also crossed this threshold.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Allow metaslabs to keep their active state as long as their fragmentation
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * percentage is less than or equal to zfs_metaslab_fragmentation_threshold. An
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * active metaslab that exceeds this threshold will no longer keep its active
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * status allowing better metaslabs to be selected.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * When set will load all metaslabs when pool is first opened.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * When set will prevent metaslabs from being unloaded.
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * Minimum size which forces the dynamic allocator to change
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * it's allocation strategy. Once the space map cannot satisfy
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * an allocation of this size then it switches to using more
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * aggressive strategy (i.e search by size rather than offset).
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrensuint64_t metaslab_df_alloc_threshold = SPA_OLD_MAXBLOCKSIZE;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * The minimum free space, in percent, which must be available
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * in a space map to continue allocations in a first-fit fashion.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Once the space map's free space drops below this level we dynamically
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * switch to using best-fit allocations.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * A metaslab is considered "free" if it contains a contiguous
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * segment which is greater than metaslab_min_alloc_size.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonuint64_t metaslab_min_alloc_size = DMU_MAX_ACCESS;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Percentage of all cpus that can be used by the metaslab taskq.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Determines how many txgs a metaslab may remain loaded without having any
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * allocations from it. As long as a metaslab continues to be used we will
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * keep it loaded.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Max number of metaslabs per group to preload.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Enable/disable preloading of metaslab.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Enable/disable fragmentation weighting on metaslabs.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonboolean_t metaslab_fragmentation_factor_enabled = B_TRUE;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Enable/disable lba weighting (i.e. outer tracks are given preference).
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonboolean_t metaslab_lba_weighting_enabled = B_TRUE;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Enable/disable metaslab group biasing.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Enable/disable segment-based metaslab selection.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonboolean_t zfs_metaslab_segment_weight_enabled = B_TRUE;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * When using segment-based metaslab selection, we will continue
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * allocating from the active metaslab until we have exhausted
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * zfs_metaslab_switch_threshold of its buckets.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Internal switch to enable/disable the metaslab allocation tracing
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Maximum entries that the metaslab allocation tracing facility will keep
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * in a given list when running in non-debug mode. We limit the number
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * of entries in non-debug mode to prevent us from using up too much memory.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * The limit should be sufficiently large that we don't expect any allocation
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * to every exceed this value. In debug mode, the system will panic if this
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * limit is ever reached allowing for further investigation.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic void metaslab_set_fragmentation(metaslab_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Metaslab classes
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_class_create(spa_t *spa, metaslab_ops_t *ops)
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc = kmem_zalloc(sizeof (metaslab_class_t), KM_SLEEP);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_init(&mc->mc_lock, NULL, MUTEX_DEFAULT, NULL);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson * Must hold one of the spa_config locks.
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson ASSERT(spa_config_held(mc->mc_spa, SCL_ALL, RW_READER) ||
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson spa_config_held(mc->mc_spa, SCL_ALL, RW_WRITER));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickmetaslab_class_space_update(metaslab_class_t *mc, int64_t alloc_delta,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int64_t defer_delta, int64_t space_delta, int64_t dspace_delta)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickmetaslab_class_get_deferred(metaslab_class_t *mc)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (spa_deflate(mc->mc_spa) ? mc->mc_dspace : mc->mc_space);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_class_histogram_verify(metaslab_class_t *mc)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if ((zfs_flags & ZFS_DEBUG_HISTOGRAM_VERIFY) == 0)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mc_hist = kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Skip any holes, uninitialized top-levels, or
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * vdevs that are not in this metalab class.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (tvd->vdev_ishole || tvd->vdev_ms_shift == 0 ||
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson VERIFY3U(mc_hist[i], ==, mc->mc_histogram[i]);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson kmem_free(mc_hist, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Calculate the metaslab class's fragmentation metric. The metric
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * is weighted based on the space contribution of each metaslab group.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * The return value will be a number between 0 and 100 (inclusive), or
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * ZFS_FRAG_INVALID if the metric has not been set. See comment above the
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * zfs_frag_table for more information about the metric.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_class_fragmentation(metaslab_class_t *mc)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson spa_config_enter(mc->mc_spa, SCL_VDEV, FTAG, RW_READER);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Skip any holes, uninitialized top-levels, or
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * vdevs that are not in this metalab class.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (tvd->vdev_ishole || tvd->vdev_ms_shift == 0 ||
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * If a metaslab group does not contain a fragmentation
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * metric then just bail out.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Determine how much this metaslab_group is contributing
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * to the overall pool fragmentation metric.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson fragmentation /= metaslab_class_get_space(mc);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Calculate the amount of expandable space that is available in
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * this metaslab class. If a device is expanded then its expandable
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * space will be the amount of allocatable space that is currently not
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * part of this metaslab class.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_class_expandable_space(metaslab_class_t *mc)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson spa_config_enter(mc->mc_spa, SCL_VDEV, FTAG, RW_READER);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (tvd->vdev_ishole || tvd->vdev_ms_shift == 0 ||
c39a2aae1e2c439d156021edfc20910dad7f9891George Wilson * Calculate if we have enough space to add additional
c39a2aae1e2c439d156021edfc20910dad7f9891George Wilson * metaslabs. We report the expandable space in terms
c39a2aae1e2c439d156021edfc20910dad7f9891George Wilson * of the metaslab size since that's the unit of expansion.
c39a2aae1e2c439d156021edfc20910dad7f9891George Wilson space += P2ALIGN(tvd->vdev_max_asize - tvd->vdev_asize,
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the weights are identical, use the offset to force uniqueness.
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Verify that the space accounting on disk matches the in-core range_trees.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_verify_space(metaslab_t *msp, uint64_t txg)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if ((zfs_flags & ZFS_DEBUG_METASLAB_VERIFY) == 0)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * We can only verify the metaslab space when we're called
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * from syncing context with a loaded metaslab that has an allocated
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * space map. Calling this in non-syncing context does not
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * provide a consistent view of the metaslab since we're performing
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * allocations in the future.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (txg != spa_syncing_txg(spa) || msp->ms_sm == NULL ||
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson sm_free_space = msp->ms_size - space_map_allocated(msp->ms_sm) -
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Account for future allocations since we would have already
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * deducted that space from the ms_freetree.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (int t = 0; t < TXG_CONCURRENT_STATES; t++) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_space(msp->ms_alloctree[(txg + t) & TXG_MASK]);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson freed = range_tree_space(msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK]);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp_free_space = range_tree_space(msp->ms_tree) + allocated +
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Metaslab groups
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * Update the allocatable flag and the metaslab group's capacity.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * The allocatable flag is set to true if the capacity is below
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * the zfs_mg_noalloc_threshold or has a fragmentation value that is
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * greater than zfs_mg_fragmentation_threshold. If a metaslab group
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * transitions from allocatable to non-allocatable or vice versa then the
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * metaslab group's class is updated to reflect the transition.
22e30981d82a0b6dc89253596ededafae8655e00George Wilsonmetaslab_group_alloc_update(metaslab_group_t *mg)
22e30981d82a0b6dc89253596ededafae8655e00George Wilson mg->mg_free_capacity = ((vs->vs_space - vs->vs_alloc) * 100) /
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If the metaslab group was just added then it won't
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * have any space until we finish syncing out this txg.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * At that point we will consider it initialized and available
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * for allocations. We also don't consider non-activated
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * metaslab groups (e.g. vdevs that are in the middle of being removed)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * to be initialized, because they can't be used for allocation.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mg->mg_initialized = metaslab_group_initialized(mg);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson } else if (was_initialized && !mg->mg_initialized) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * A metaslab group is considered allocatable if it has plenty
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * of free space or is not heavily fragmented. We only take
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * fragmentation into account if the metaslab group has a valid
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * fragmentation metric (i.e. a value between 0 and 100).
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mg->mg_allocatable = (mg->mg_activation_count > 0 &&
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mg->mg_free_capacity > zfs_mg_noalloc_threshold &&
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_fragmentation <= zfs_mg_fragmentation_threshold));
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * The mc_alloc_groups maintains a count of the number of
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * groups in this metaslab class that are still above the
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * zfs_mg_noalloc_threshold. This is used by the allocating
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * threads to determine if they should avoid allocations to
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * a given group. The allocator will avoid allocations to a group
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * if that group has reached or is below the zfs_mg_noalloc_threshold
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * and there are still other groups that are above the threshold.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * When a group transitions from allocatable to non-allocatable or
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * vice versa we update the metaslab class to reflect that change.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * When the mc_alloc_groups value drops to 0 that means that all
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * groups have reached the zfs_mg_noalloc_threshold making all groups
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * eligible for allocations. This effectively means that all devices
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * are balanced again.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson else if (!was_allocatable && mg->mg_allocatable)
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg = kmem_zalloc(sizeof (metaslab_group_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson refcount_create_tracked(&mg->mg_alloc_queue_depth);
be082110c08433beadb738ad3be035a73d995ea8George Wilson mg->mg_taskq = taskq_create("metaslab_group_taskq", metaslab_load_pct,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson minclsyspri, 10, INT_MAX, TASKQ_THREADS_CPU_PCT);
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley * We may have gone below zero with the activation count
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley * either because we never activated in the first place or
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley * because we're done, and possibly removing the vdev.
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_aliquot = metaslab_aliquot * MAX(1, mg->mg_vd->vdev_children);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_initialized(metaslab_group_t *mg)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (vs->vs_space != 0 && mg->mg_activation_count > 0);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return ((1ULL << mg->mg_vd->vdev_ms_shift) * mg->mg_vd->vdev_ms_count);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_histogram_verify(metaslab_group_t *mg)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if ((zfs_flags & ZFS_DEBUG_HISTOGRAM_VERIFY) == 0)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg_hist = kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i ++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson VERIFY3U(mg_hist[i], ==, mg->mg_histogram[i]);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson kmem_free(mg_hist, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_histogram_add(metaslab_group_t *mg, metaslab_t *msp)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_histogram_remove(metaslab_group_t *mg, metaslab_t *msp)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * Although in principle the weight can be any value, in
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * practice we do not use values in the range [1, 511].
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT(weight >= SPA_MINBLOCKSIZE || weight == 0);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Calculate the fragmentation for a given metaslab group. We can use
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * a simple average here since all metaslabs within the group must have
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * the same size. The return value will be a value between 0 and 100
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * (inclusive), or ZFS_FRAG_INVALID if less than half of the metaslab in this
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * group have a fragmentation metric.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_fragmentation(metaslab_group_t *mg)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_fragmentation == ZFS_FRAG_INVALID)
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * Determine if a given metaslab group should skip allocations. A metaslab
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * group should avoid allocations if its free capacity is less than the
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * zfs_mg_noalloc_threshold or its fragmentation metric is greater than
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * zfs_mg_fragmentation_threshold and there is at least one metaslab group
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * that can still handle allocations. If the allocation throttle is enabled
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * then we skip allocations to devices that have reached their maximum
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * allocation queue depth unless the selected metaslab group is the only
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * eligible group remaining.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_allocatable(metaslab_group_t *mg, metaslab_group_t *rotor,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We can only consider skipping this metaslab group if it's
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * in the normal metaslab class and there are other metaslab
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * groups to select from. Otherwise, we always consider it eligible
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * for allocations.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (mc != spa_normal_class(spa) || mc->mc_groups <= 1)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If the metaslab group's mg_allocatable flag is set (see comments
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * in metaslab_group_alloc_update() for more information) and
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * the allocation throttle is disabled then allow allocations to this
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * device. However, if the allocation throttle is enabled then
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * check if we have reached our allocation limit (mg_alloc_queue_depth)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * to determine if we should allow allocations to this metaslab group.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If all metaslab groups are no longer considered allocatable
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * (mc_alloc_groups == 0) or we're trying to allocate the smallest
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * gang block size then we allow allocations on this metaslab group
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * regardless of the mg_allocatable or throttle settings.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If this metaslab group does not have any free space, then
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * there is no point in looking further.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson qdepth = refcount_count(&mg->mg_alloc_queue_depth);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If this metaslab group is below its qmax or it's
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * the only allocatable metasable group, then attempt
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * to allocate from it.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (qdepth < qmax || mc->mc_alloc_groups == 1)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Since this metaslab group is at or over its qmax, we
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * need to determine if there are metaslab groups after this
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * one that might be able to handle this allocation. This is
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * racy since we can't hold the locks for all metaslab
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * groups at the same time when we make this check.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (mgp = mg->mg_next; mgp != rotor; mgp = mgp->mg_next) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson qdepth = refcount_count(&mgp->mg_alloc_queue_depth);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If there is another metaslab group that
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * might be able to handle the allocation, then
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * we return false so that we skip this group.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We didn't find another group to handle the allocation
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * so we can't skip this metaslab group even though
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * we are at or over our qmax.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson } else if (mc->mc_alloc_groups == 0 || psize == SPA_MINBLOCKSIZE) {
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Range tree callbacks
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Comparison function for the private size-ordered tree. Tree is sorted
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * by size, larger sizes at the end of the tree.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rangesize_compare(const void *x1, const void *x2)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t rs_size1 = r1->rs_end - r1->rs_start;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t rs_size2 = r2->rs_end - r2->rs_start;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Create any block allocator specific components. The current allocators
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * rely on using both a size-ordered range_tree_t and an array of uint64_t's.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_create(range_tree_t *rt, void *arg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_create(&msp->ms_size_tree, metaslab_rangesize_compare,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson sizeof (range_seg_t), offsetof(range_seg_t, rs_pp_node));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Destroy the block allocator specific components.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_destroy(range_tree_t *rt, void *arg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_add(range_tree_t *rt, range_seg_t *rs, void *arg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_remove(range_tree_t *rt, range_seg_t *rs, void *arg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_vacate(range_tree_t *rt, void *arg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Normally one would walk the tree freeing nodes along the way.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Since the nodes are shared with the range trees we can avoid
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * walking all nodes and just reinitialize the avl tree. The nodes
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * will be freed by the range tree, so we don't want to free them here.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_create(&msp->ms_size_tree, metaslab_rangesize_compare,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson sizeof (range_seg_t), offsetof(range_seg_t, rs_pp_node));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Common allocator routines
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * Return the maximum contiguous segment within the metaslab.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_block_find(avl_tree_t *t, uint64_t start, uint64_t size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * This is a helper function that can be used by the allocator to find
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * a suitable block to allocate. This will search the specified AVL
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * tree looking for a block that matches the specified criteria.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_block_picker(avl_tree_t *t, uint64_t *cursor, uint64_t size,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_seg_t *rs = metaslab_block_find(t, *cursor, size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t offset = P2ROUNDUP(rs->rs_start, align);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * If we know we've searched the whole map (*cursor == 0), give up.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Otherwise, reset the cursor to the beginning and try again.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (-1ULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (metaslab_block_picker(t, cursor, size, align));
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * The first-fit block allocator
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_ff_alloc(metaslab_t *msp, uint64_t size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Find the largest power of 2 block size that evenly divides the
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * requested size. This is used to try to allocate blocks with similar
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * alignment from the same area of the metaslab (i.e. same cursor
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * bucket) but it does not guarantee that other allocations sizes
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * may exist in the same region.
bf16b11e8deb633dd6c4296d46e92399d1582df4Matthew Ahrens uint64_t *cursor = &msp->ms_lbas[highbit64(align) - 1];
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (metaslab_block_picker(t, cursor, size, align));
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * Dynamic block allocator -
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * Uses the first fit allocation scheme until space get low and then
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * and metaslab_df_free_pct to determine when to switch the allocation scheme.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_df_alloc(metaslab_t *msp, uint64_t size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Find the largest power of 2 block size that evenly divides the
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * requested size. This is used to try to allocate blocks with similar
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * alignment from the same area of the metaslab (i.e. same cursor
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * bucket) but it does not guarantee that other allocations sizes
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * may exist in the same region.
bf16b11e8deb633dd6c4296d46e92399d1582df4Matthew Ahrens uint64_t *cursor = &msp->ms_lbas[highbit64(align) - 1];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t max_size = metaslab_block_maxsize(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&msp->ms_size_tree));
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson return (-1ULL);
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * If we're running low on space switch to using the size
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * sorted AVL tree (best-fit).
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson return (metaslab_block_picker(t, cursor, size, 1ULL));
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Cursor fit block allocator -
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Select the largest region in the metaslab, set the cursor to the beginning
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * of the range and the cursor_end to the end of the range. As allocations
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * are made advance the cursor. Continue allocating from the cursor until
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * the range is exhausted and then find a new range.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_cf_alloc(metaslab_t *msp, uint64_t size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&rt->rt_root));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (rs == NULL || (rs->rs_end - rs->rs_start) < size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (-1ULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * New dynamic fit allocator -
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Select a region that is large enough to allocate 2^metaslab_ndf_clump_shift
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * contiguous blocks. If no region is found then just use the largest segment
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * that remains.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Determines desired number of contiguous blocks (2^metaslab_ndf_clump_shift)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * to request from the allocator.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_ndf_alloc(metaslab_t *msp, uint64_t size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t max_size = metaslab_block_maxsize(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&msp->ms_size_tree));
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (-1ULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (rs == NULL || (rs->rs_end - rs->rs_start) < size) {
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (-1ULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_ops_t *zfs_metaslab_ops = &metaslab_df_ops;
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Metaslabs
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Wait for any in-progress metaslab loads to complete.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * If the space map has not been allocated yet, then treat
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * all the space in the metaslab as free and add it to the
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson error = space_map_load(msp->ms_sm, msp->ms_tree, SM_FREE);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(msp->ms_tree, msp->ms_start, msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Suryametaslab_init(metaslab_group_t *mg, uint64_t id, uint64_t object, uint64_t txg,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson objset_t *mos = vd->vdev_spa->spa_meta_objset;
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ms = kmem_zalloc(sizeof (metaslab_t), KM_SLEEP);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya mutex_init(&ms->ms_lock, NULL, MUTEX_DEFAULT, NULL);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya cv_init(&ms->ms_load_cv, NULL, CV_DEFAULT, NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * We only open space map objects that already exist. All others
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * will be opened when we finally allocate an object for it.
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya error = space_map_open(&ms->ms_sm, mos, object, ms->ms_start,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * We create the main range tree here, but we don't create the
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * alloctree and freetree until metaslab_sync_done(). This serves
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * two purposes: it allows metaslab_sync_done() to detect the
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * addition of new space; and for debugging, it ensures that we'd
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * data fault on any attempt to use this metaslab before it's ready.
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ms->ms_tree = range_tree_create(&metaslab_rt_ops, ms, &ms->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we're opening an existing pool (txg == 0) or creating
fa9e4066f08beec538e775443c5be79dd423fcabahrens * a new one (txg == TXG_INITIAL), all space is available now.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we're adding space to an existing pool, the new space
fa9e4066f08beec538e775443c5be79dd423fcabahrens * does not become available until after this txg has synced.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * The metaslab's weight will also be initialized when we sync
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * out this txg. This ensures that we don't attempt to allocate
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * from it before we have initialized it completely.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * If metaslab_debug_load is set and we're initializing a metaslab
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * that has an allocated space map object then load the its space
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * map so that can verify frees.
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya if (metaslab_debug_load && ms->ms_sm != NULL) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (txg != 0) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson vdev_space_update(mg->mg_vd, -space_map_allocated(msp->ms_sm),
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley for (int t = 0; t < TXG_SIZE; t++) {
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * This table defines a segment size based fragmentation metric that will
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * allow each metaslab to derive its own fragmentation value. This is done
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * by calculating the space in each bucket of the spacemap histogram and
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * multiplying that by the fragmetation metric in this table. Doing
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * this for all buckets and dividing it by the total amount of free
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * space in this metaslab (i.e. the total free space in all buckets) gives
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * us the fragmentation metric. This means that a high fragmentation metric
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * equates to most of the free space being comprised of small segments.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Conversely, if the metric is low, then most of the free space is in
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * large segments. A 10% change in fragmentation equates to approximately
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * double the number of segments.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * This table defines 0% fragmented space using 16MB segments. Testing has
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * shown that segments that are greater than or equal to 16MB do not suffer
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * from drastic performance problems. Using this value, we derive the rest
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * of the table. Since the fragmentation value is never stored on disk, it
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * is possible to change these calculations in the future.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonint zfs_frag_table[FRAGMENTATION_TABLE_SIZE] = {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Calclate the metaslab's fragmentation metric. A return value
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * of ZFS_FRAG_INVALID means that the metaslab has not been upgraded and does
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * not support this metric. Otherwise, the return value should be in the
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * range [0, 100].
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson boolean_t feature_enabled = spa_feature_is_enabled(spa,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * A null space map means that the entire metaslab is free
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * and thus is not fragmented.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If this metaslab's space map has not been upgraded, flag it
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * so that we upgrade next time we encounter it.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_sm->sm_dbuf->db_size != sizeof (space_map_phys_t)) {
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens spa_dbgmsg(spa, "txg %llu, requesting force condense: "
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_sm->sm_phys->smp_histogram[i] == 0)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson space = msp->ms_sm->sm_phys->smp_histogram[i] << (i + shift);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Compute a weight -- a selection preference value -- for the given metaslab.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * This is based on the amount of free space, the level of fragmentation,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * the LBA range, and whether the metaslab is loaded.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The baseline weight is the metaslab's free space.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space = msp->ms_size - space_map_allocated(msp->ms_sm);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Use the fragmentation information to inversely scale
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * down the baseline weight. We need to ensure that we
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * don't exclude this metaslab completely when it's 100%
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * fragmented. To avoid this we reduce the fragmented value
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson space = (space * (100 - (msp->ms_fragmentation - 1))) / 100;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * If space < SPA_MINBLOCKSIZE, then we will not allocate from
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * this metaslab again. The fragmentation metric may have
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * decreased the space to something smaller than
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * SPA_MINBLOCKSIZE, so reset the space to SPA_MINBLOCKSIZE
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * so that we can consume any remaining space.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * Modern disks have uniform bit density and constant angular velocity.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * Therefore, the outer recording zones are faster (higher bandwidth)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * than the inner zones by the ratio of outer to inner track diameter,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * which is typically around 2:1. We account for this by assigning
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * In effect, this means that we'll select the metaslab with the most
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * free bandwidth rather than simply the one with the most free space.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson weight = 2 * weight - (msp->ms_id * weight) / vd->vdev_ms_count;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT(weight >= space && weight <= 2 * space);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * If this metaslab is one we're actively using, adjust its
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * weight to make it preferable to any inactive metaslab so
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * we'll polish it off. If the fragmentation on this metaslab
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * has exceed our threshold, then don't mark it active.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_loaded && msp->ms_fragmentation != ZFS_FRAG_INVALID &&
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_fragmentation <= zfs_metaslab_fragmentation_threshold) {
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Return the weight of the specified metaslab, according to the segment-based
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * weighting algorithm. The metaslab must be loaded. This function can
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * be called within a sync pass since it relies only on the metaslab's
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * range tree which is always accurate when the metaslab is loaded.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_weight_from_range_tree(metaslab_t *msp)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (int i = RANGE_TREE_HISTOGRAM_SIZE - 1; i >= SPA_MINBLOCKSHIFT;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint8_t shift = msp->ms_group->mg_vd->vdev_ashift;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson int max_idx = SPACE_MAP_HISTOGRAM_SIZE + shift - 1;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * The range tree provides more precision than the space map
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * and must be downgraded so that all values fit within the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * space map's histogram. This allows us to compare loaded
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * vs. unloaded metaslabs to determine which metaslab is
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * considered "best".
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Calculate the weight based on the on-disk histogram. This should only
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * be called after a sync pass has completely finished since the on-disk
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * information is updated in metaslab_sync().
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (int i = SPACE_MAP_HISTOGRAM_SIZE - 1; i >= 0; i--) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_sm->sm_phys->smp_histogram[i] != 0) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Compute a segment-based weight for the specified metaslab. The weight
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * is determined by highest bucket in the histogram. The information
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * for the highest bucket is encoded into the weight value.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * The metaslab is completely free.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson int max_idx = SPACE_MAP_HISTOGRAM_SIZE + shift - 1;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_COUNT(weight, 1ULL << (idx - max_idx));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT3U(msp->ms_sm->sm_dbuf->db_size, ==, sizeof (space_map_phys_t));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If the metaslab is fully allocated then just make the weight 0.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (space_map_allocated(msp->ms_sm) == msp->ms_size)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If the metaslab is already loaded, then use the range tree to
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * determine the weight. Otherwise, we rely on the space map information
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * to generate the weight.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson weight = metaslab_weight_from_range_tree(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If the metaslab was active the last time we calculated its weight
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * then keep it active. We want to consume the entire region that
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * is associated with this weight.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_activation_weight != 0 && weight != 0)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_ACTIVE(weight, WEIGHT_GET_ACTIVE(msp->ms_weight));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Determine if we should attempt to allocate from this metaslab. If the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * metaslab has a maximum size then we can quickly determine if the desired
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * allocation size can be satisfied. Otherwise, if we're using segment-based
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * weighting then we can determine the maximum allocation that this metaslab
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * can accommodate based on the index encoded in the weight. If we're using
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * space-based weights then rely on the entire weight (excluding the weight
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_should_allocate(metaslab_t *msp, uint64_t asize)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * The metaslab segment weight indicates segments in the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * range [2^i, 2^(i+1)), where i is the index in the weight.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Since the asize might be in the middle of the range, we
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * should attempt the allocation if asize < 2^(i+1).
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson 1ULL << (WEIGHT_GET_INDEX(msp->ms_weight) + 1));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * This vdev is in the process of being removed so there is nothing
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * for us to do here.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Update the maximum size if the metaslab is loaded. This will
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ensure that we get an accurate maximum size if newly freed space
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * has been added back into the free tree.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Segment-based weighting requires space map histogram support.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM) &&
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson (msp->ms_sm == NULL || msp->ms_sm->sm_dbuf->db_size ==
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilsonmetaslab_activate(metaslab_t *msp, uint64_t activation_weight)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_passivate(metaslab_t *msp, uint64_t weight)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t size = weight & ~METASLAB_WEIGHT_TYPE;
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * If size < SPA_MINBLOCKSIZE, then we will not allocate from
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * this metaslab again. In that case, it had better be empty,
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * or we would be leaving space on the table.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_group_sort(msp->ms_group, msp, weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Segment-based metaslabs are activated once and remain active until
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * we either fail an allocation attempt (similar to space-based metaslabs)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * or have exhausted the free space in zfs_metaslab_switch_threshold
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * buckets since the metaslab was activated. This function checks to see
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * if we've exhaused the zfs_metaslab_switch_threshold buckets in the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * metaslab and passivates it proactively. This will allow us to select a
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * metaslabs with larger contiguous region if any remaining within this
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * metaslab group. If we're in sync pass > 1, then we continue using this
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * metaslab so that we don't dirty more block and cause more sync passes.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_segment_may_passivate(metaslab_t *msp)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (WEIGHT_IS_SPACEBASED(msp->ms_weight) || spa_sync_pass(spa) > 1)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Since we are in the middle of a sync pass, the most accurate
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * information that is accessible to us is the in-core range tree
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * histogram; calculate the new weight based on that information.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t weight = metaslab_weight_from_range_tree(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson int activation_idx = WEIGHT_GET_INDEX(msp->ms_activation_weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (current_idx <= activation_idx - zfs_metaslab_switch_threshold)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (spa_shutting_down(spa) || !metaslab_preload_enabled) {
30beaff42d8240ebf5386e8b7a14e3d137a1631fGeorge Wilson * Load the next potential metaslabs
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (msp = avl_first(t); msp != NULL; msp = AVL_NEXT(t, msp)) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * We preload only the maximum number of metaslabs specified
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * by metaslab_preload_limit. If a metaslab is being forced
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * to condense then we preload it too. This will ensure
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * that force condensing happens in the next txg.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (++m > metaslab_preload_limit && !msp->ms_condense_wanted) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY(taskq_dispatch(mg->mg_taskq, metaslab_preload,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Determine if the space map's on-disk footprint is past our tolerance
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * for inefficiency. We would like to use the following criteria to make
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * our decision:
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * 1. The size of the space map object should not dramatically increase as a
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * result of writing out the free space range tree.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * 2. The minimal on-disk space map representation is zfs_condense_pct/100
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * times the size than the free space range tree representation
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * (i.e. zfs_condense_pct = 110 and in-core = 1MB, minimal = 1.1.MB).
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * 3. The on-disk size of the space map should actually decrease.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * Checking the first condition is tricky since we don't want to walk
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * the entire AVL tree calculating the estimated on-disk size. Instead we
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * use the size-ordered range tree in the metaslab and calculate the
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * size required to write out the largest segment in our free tree. If the
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * size required to represent that segment on disk is larger than the space
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * map object then we avoid condensing this map.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * To determine the second criterion we use a best-case estimate and assume
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * each segment can be represented on-disk as a single 64-bit entry. We refer
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * to this best-case estimate as the space map's minimal form.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * Unfortunately, we cannot compute the on-disk size of the space map in this
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * context because we cannot accurately compute the effects of compression, etc.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * Instead, we apply the heuristic described in the block comment for
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * zfs_metaslab_condense_block_threshold - we only condense if the space used
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * is greater than a threshold number of blocks.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece uint64_t size, entries, segsz, object_size, optimal_size, record_size;
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece uint64_t vdev_blocksize = 1 << msp->ms_group->mg_vd->vdev_ashift;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Use the ms_size_tree range tree, which is ordered by size, to
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * obtain the largest segment in the free tree. We always condense
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * metaslabs that are empty and metaslabs for which a condense
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * request has been made.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * Calculate the number of 64-bit entries this segment would
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * require when written to disk. If this single segment would be
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * larger on-disk than the entire current on-disk structure, then
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * clearly condensing will increase the on-disk structure size.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson size = (rs->rs_end - rs->rs_start) >> sm->sm_shift;
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece optimal_size = sizeof (uint64_t) * avl_numnodes(&msp->ms_tree->rt_root);
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece record_size = MAX(doi.doi_data_block_size, vdev_blocksize);
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece object_size >= (optimal_size * zfs_condense_pct / 100) &&
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece object_size > zfs_metaslab_condense_block_threshold * record_size);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * Condense the on-disk space map representation to its minimized form.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * The minimized form consists of a small number of allocations followed by
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * the entries of the free range tree.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilsonmetaslab_condense(metaslab_t *msp, uint64_t txg, dmu_tx_t *tx)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t *freetree = msp->ms_freetree[txg & TXG_MASK];
daec38ecb4fb5e73e4ca9e99be84f6b8c50c02faJoe Stein spa_dbgmsg(spa, "condensing: txg %llu, msp[%llu] %p, vdev id %llu, "
daec38ecb4fb5e73e4ca9e99be84f6b8c50c02faJoe Stein "spa %s, smp size %llu, segments %lu, forcing condense=%s", txg,
daec38ecb4fb5e73e4ca9e99be84f6b8c50c02faJoe Stein space_map_length(msp->ms_sm), avl_numnodes(&msp->ms_tree->rt_root),
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Create an range tree that is 100% allocated. We remove segments
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * that have been freed in this txg, any deferred frees that exist,
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * and any allocation in the future. Removing segments should be
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * a relatively inexpensive operation since we expect these trees to
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * have a small number of nodes.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson condense_tree = range_tree_create(NULL, NULL, &msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(condense_tree, msp->ms_start, msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Remove what's been freed in this txg from the condense_tree.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * Since we're in sync_pass 1, we know that all the frees from
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * this txg are in the freetree.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_walk(freetree, range_tree_remove, condense_tree);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 1; t < TXG_CONCURRENT_STATES; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_walk(msp->ms_alloctree[(txg + t) & TXG_MASK],
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * We're about to drop the metaslab's lock thus allowing
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * other consumers to change it's content. Set the
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * metaslab's ms_condensing flag to ensure that
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * allocations on this metaslab do not occur while we're
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * in the middle of committing it to disk. This is only critical
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * for the ms_tree as all other range trees use per txg
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * views of their content.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * While we would ideally like to create a space map representation
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * that consists only of allocation records, doing so can be
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * prohibitively expensive because the in-core free tree can be
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * large, and therefore computationally expensive to subtract
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * from the condense_tree. Instead we sync out two trees, a cheap
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * allocation only tree followed by the in-core free tree. While not
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * optimal, this is typically close to optimal, and much cheaper to
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(sm, condense_tree, SM_ALLOC, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(sm, msp->ms_tree, SM_FREE, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Write a metaslab to disk in the context of the specified transaction group.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t *alloctree = msp->ms_alloctree[txg & TXG_MASK];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t **freetree = &msp->ms_freetree[txg & TXG_MASK];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t object = space_map_object(msp->ms_sm);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * This metaslab has just been added so there's no work to do now.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Normally, we don't want to process a metaslab if there
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * are no allocations or frees to perform. However, if the metaslab
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * is being forced to condense we need to let it through.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The only state that can actually be changing concurrently with
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * metaslab_sync() is the metaslab's ms_tree. No other thread can
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * be modifying this txg's alloctree, freetree, freed_tree, or
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * space_map_phys_t. Therefore, we only hold ms_lock to satify
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * space map ASSERTs. We drop it whenever we call into the DMU,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * because the DMU can call down to us (e.g. via zio_free()) at
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(space_map_open(&msp->ms_sm, mos, new_object,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Note: metaslab_condense() clears the space map's histogram.
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens * Therefore we must verify and remove this histogram before
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens * condensing.
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens metaslab_class_histogram_verify(mg->mg_class);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_loaded && spa_sync_pass(spa) == 1 &&
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(msp->ms_sm, alloctree, SM_ALLOC, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(msp->ms_sm, *freetree, SM_FREE, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * When the space map is loaded, we have an accruate
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * histogram in the range tree. This gives us an opportunity
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * to bring the space map's histogram up-to-date so we clear
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * it first before updating it.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_histogram_add(msp->ms_sm, msp->ms_tree, tx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Since we've cleared the histogram we need to add back
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * any free space that has already been processed, plus
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * any deferred space. This allows the on-disk histogram
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * to accurately reflect all free space even if some space
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * is not yet available for allocation (i.e. deferred).
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson space_map_histogram_add(msp->ms_sm, *freed_tree, tx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Add back any deferred free space that has not been
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * added back into the in-core free tree yet. This will
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ensure that we don't end up with a space map histogram
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * that is completely empty unless the metaslab is fully
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Always add the free space from this sync pass to the space
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * map histogram. We want to make sure that the on-disk histogram
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * accounts for all free space. If the space map is not loaded,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * then we will lose some accuracy but will correct it the next
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * time we load the space map.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson space_map_histogram_add(msp->ms_sm, *freetree, tx);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_class_histogram_verify(mg->mg_class);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * For sync pass 1, we avoid traversing this txg's free range tree
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * and instead will just swap the pointers for freetree and
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * freed_tree. We can safely do this since the freed_tree is
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * guaranteed to be empty on the initial pass.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_vacate(*freetree, range_tree_add, *freed_tree);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT0(range_tree_space(msp->ms_alloctree[txg & TXG_MASK]));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT0(range_tree_space(msp->ms_alloctree[TXG_CLEAN(txg) & TXG_MASK]));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT0(range_tree_space(msp->ms_freetree[txg & TXG_MASK]));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Called after a transaction group has completely synced to mark
fa9e4066f08beec538e775443c5be79dd423fcabahrens * all of the metaslab's free space as usable.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * If this metaslab is just becoming available, initialize its
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * alloctrees, freetrees, and defertree and add its capacity to
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK] == NULL) {
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley for (int t = 0; t < TXG_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_alloctree[t] = range_tree_create(NULL, msp,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_freetree[t] = range_tree_create(NULL, msp,
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_defertree[t] = range_tree_create(NULL, msp,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson freed_tree = &msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson defer_tree = &msp->ms_defertree[txg % TXG_DEFER_SIZE];
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t free_space = metaslab_class_get_space(spa_normal_class(spa)) -
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_class_get_alloc(spa_normal_class(spa));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson alloc_delta = space_map_alloc_delta(msp->ms_sm);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick vdev_space_update(vd, alloc_delta + defer_delta, defer_delta, 0);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT0(range_tree_space(msp->ms_alloctree[txg & TXG_MASK]));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT0(range_tree_space(msp->ms_freetree[txg & TXG_MASK]));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * If there's a metaslab_load() in progress, wait for it to complete
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * so that we have a consistent view of the in-core space map.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Move the frees from the defer_tree back to the free
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * range tree (if it's loaded). Swap the freed_tree and the
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * defer_tree -- this is safe to do because we've just emptied out
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * the defer_tree.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_loaded ? range_tree_add : NULL, msp->ms_tree);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_loaded ? range_tree_add : NULL, msp->ms_tree);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3S(msp->ms_deferspace, <=, msp->ms_size);
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley * Keep syncing this metaslab until all deferred frees
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley * are back in circulation.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Calculate the new weights before unloading any metaslabs.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * This will give us the most accurate weighting.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_group_sort(mg, msp, metaslab_weight(msp));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If the metaslab is loaded and we've not tried to load or allocate
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * from it in 'metaslab_unload_delay' txgs, then unload it.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_selected_txg + metaslab_unload_delay < txg) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 1; t < TXG_CONCURRENT_STATES; t++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_fragmentation = metaslab_group_fragmentation(mg);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Preload the next potential metaslabs
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Metaslab allocation tracing facility
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_alloc_trace_cache = kmem_cache_create(
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson "metaslab_alloc_trace_cache", sizeof (metaslab_alloc_trace_t),
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_ksp = kstat_create("zfs", 0, "metaslab_trace_stats",
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson "misc", KSTAT_TYPE_NAMED, 1, KSTAT_FLAG_VIRTUAL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_ksp->ks_data = &metaslab_trace_over_limit;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson "metaslab_trace_over_limit", KSTAT_DATA_UINT64);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_destroy(metaslab_alloc_trace_cache);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Add an allocation trace element to the allocation tracing list.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_trace_add(zio_alloc_list_t *zal, metaslab_group_t *mg,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_t *msp, uint64_t psize, uint32_t dva_id, uint64_t offset)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * When the tracing list reaches its maximum we remove
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * the second element in the list before adding a new one.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * By removing the second element we preserve the original
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * entry as a clue to what allocations steps have already been
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (zal->zal_size == metaslab_trace_max_entries) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson atomic_inc_64(&metaslab_trace_over_limit.value.ui64);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat_next = list_next(&zal->zal_list, list_head(&zal->zal_list));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_free(metaslab_alloc_trace_cache, mat_next);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_alloc(metaslab_alloc_trace_cache, KM_SLEEP);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * The list is part of the zio so locking is not required. Only
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * a single thread will perform allocations for a given zio.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT3U(zal->zal_size, <=, metaslab_trace_max_entries);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson list_create(&zal->zal_list, sizeof (metaslab_alloc_trace_t),
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson offsetof(metaslab_alloc_trace_t, mat_list_node));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson while ((mat = list_remove_head(&zal->zal_list)) != NULL)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_free(metaslab_alloc_trace_cache, mat);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * ==========================================================================
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Metaslab block operations
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * ==========================================================================
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_alloc_increment(spa_t *spa, uint64_t vdev, void *tag, int flags)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson (void) refcount_add(&mg->mg_alloc_queue_depth, tag);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_alloc_decrement(spa_t *spa, uint64_t vdev, void *tag, int flags)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson (void) refcount_remove(&mg->mg_alloc_queue_depth, tag);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_alloc_verify(spa_t *spa, const blkptr_t *bp, void *tag)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (int d = 0; d < ndvas; d++) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson VERIFY(refcount_not_held(&mg->mg_alloc_queue_depth, tag));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_block_alloc(metaslab_t *msp, uint64_t size, uint64_t txg)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_class_t *mc = msp->ms_group->mg_class;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson VERIFY0(P2PHASE(start, 1ULL << vd->vdev_ashift));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson VERIFY3U(range_tree_space(rt) - size, <=, msp->ms_size);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (range_tree_space(msp->ms_alloctree[txg & TXG_MASK]) == 0)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_add(msp->ms_alloctree[txg & TXG_MASK], start, size);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /* Track the last successful allocation */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Now that we've attempted the allocation we need to update the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * metaslab's maximum block size since it may have changed.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_group_alloc_normal(metaslab_group_t *mg, zio_alloc_list_t *zal,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t asize, uint64_t txg, uint64_t min_distance, dva_t *dva, int d)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson for (i = 0; i < d; i++) {
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_t *search = kmem_alloc(sizeof (*search), KM_SLEEP);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Find the metaslab with the highest weight that is less
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * than what we've already tried. In the common case, this
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * means that we will examine each metaslab at most once.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Note that concurrent callers could reorder metaslabs
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * by activation/passivation once we have dropped the mg_lock.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If a metaslab is activated by another thread, and we fail
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * to allocate from the metaslab we have selected, we may
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * not try the newly-activated metaslab, and instead activate
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * another metaslab. This is not optimal, but generally
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * does not cause any problems (a possible exception being
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * if every metaslab is completely full except for the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * the newly-activated metaslab which we fail to examine).
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson * If the selected metaslab is condensing, skip it.
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson was_active = msp->ms_weight & METASLAB_ACTIVE_MASK;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (i = 0; i < d; i++) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (i == d)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (-1ULL);
aeb1c1b609b02f03e8e7448beb88384ebc713525gw * Ensure that the metaslab we have selected is still
aeb1c1b609b02f03e8e7448beb88384ebc713525gw * capable of handling our request. It's possible that
aeb1c1b609b02f03e8e7448beb88384ebc713525gw * another thread may have changed the weight while we
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * were blocked on the metaslab lock. We check the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * active status first to see if we need to reselect
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * a new metaslab.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (was_active && !(msp->ms_weight & METASLAB_ACTIVE_MASK)) {
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson if (metaslab_activate(msp, activation_weight) != 0) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Now that we have the lock, recheck to see if we should
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * continue to use this metaslab for this allocation. The
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * the metaslab is now loaded so metaslab_should_allocate() can
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * accurately determine if the allocation attempt should
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /* Passivate this metaslab and select a new one. */
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson * If this metaslab is currently condensing then pick again as
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson * we can't manipulate this metaslab until it's committed
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson offset = metaslab_block_alloc(msp, asize, txg);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, msp, asize, d, offset);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /* Proactively passivate the metaslab, if needed */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * We were unable to allocate from this metaslab so determine
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * a new weight for this metaslab. Now that we have loaded
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * the metaslab we can provide a better hint to the metaslab
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * For space-based metaslabs, we use the maximum block size.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * This information is only available when the metaslab
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * is loaded and is more accurate than the generic free
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * space weight that was calculated by metaslab_weight().
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * This information allows us to quickly compare the maximum
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * available allocation in the metaslab to the allocation
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * size being requested.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * For segment-based metaslabs, determine the new weight
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * based on the highest bucket in the range tree. We
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * explicitly use the loaded segment weight (i.e. the range
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * tree histogram) since it contains the space that is
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * currently available for allocation and is accurate
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * even within a sync pass.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t weight = metaslab_block_maxsize(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * We have just failed an allocation attempt, check
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * that metaslab_should_allocate() agrees. Otherwise,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * we may end up in an infinite loop retrying the same
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(!metaslab_should_allocate(msp, asize));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_group_alloc(metaslab_group_t *mg, zio_alloc_list_t *zal,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t asize, uint64_t txg, uint64_t min_distance, dva_t *dva, int d)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson offset = metaslab_group_alloc_normal(mg, zal, asize, txg,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * This metaslab group was unable to allocate
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * the minimum gang block size so it must be out of
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * space. We must notify the allocation throttle
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * to start skipping allocation attempts to this
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * metaslab group until more space becomes available.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Note: this failure cannot be caused by the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * allocation throttle since the allocation throttle
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * is only responsible for skipping devices and
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * not failing block allocations.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If we have to write a ditto block (i.e. more than one DVA for a given BP)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * on the same vdev as an existing DVA of this BP, then try to allocate it
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * at least (vdev_asize / (2 ^ ditto_same_vdev_distance_shift)) away from the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * existing DVAs.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Allocate a block for the specified i/o.
8654d0253136055bd4cc2423d87378e8a37f2eb5perrinmetaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson dva_t *dva, int d, dva_t *hintdva, uint64_t txg, int flags,
e05725b117836db173257fae43fb0746eb857fb5bonwick * For testing, make some blocks above a certain size be gang blocks.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (psize >= metaslab_gang_bang && (ddi_get_lbolt() & 3) == 0) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, NULL, NULL, psize, d, TRACE_FORCE_GANG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Start at the rotor and loop through all mgs until we find something.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * Note that there's no locking on mc_rotor or mc_aliquot because
fa9e4066f08beec538e775443c5be79dd423fcabahrens * nothing actually breaks if we miss a few updates -- we just won't
fa9e4066f08beec538e775443c5be79dd423fcabahrens * allocate quite as evenly. It all balances out over time.
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * If we are doing ditto or log blocks, try to spread them across
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * consecutive vdevs. If we're forced to reuse a vdev before we've
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * allocated all of our ditto blocks, then try and spread them out on
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * that vdev as much as possible. If it turns out to not be possible,
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * gradually lower our standards until anything becomes acceptable.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * Also, allocating on consecutive vdevs (as opposed to random vdevs)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * gives us hope of containing our fault domains to something we're
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * able to reason about. Otherwise, any two top-level vdev failures
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * will guarantee the loss of data. With consecutive allocation,
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * only two adjacent top-level vdev failures will result in data loss.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * If we are doing gang blocks (hintdva is non-NULL), try to keep
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * ourselves on the same vdev as our gang block header. That
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * way, we can hope for locality in vdev_cache, plus it makes our
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * fault domains something tractable.
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson * It's possible the vdev we're using as the hint no
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson * longer exists (i.e. removed). Consult the rotor when
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson * all else fails.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm } else if (d != 0) {
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick * If the hint put us into the wrong metaslab class, or into a
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick * metaslab group that has been passivated, just follow the rotor.
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick if (mg->mg_class != mc || mg->mg_activation_count <= 0)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Don't allocate from faulted devices.
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * Determine if the selected metaslab group is eligible
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * for allocations. If we're ganging then don't allow
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * this metaslab group to skip allocations since that would
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * inadvertently return ENOSPC and suspend the pool
22e30981d82a0b6dc89253596ededafae8655e00George Wilson * even though space is still available.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (allocatable && !GANG_ALLOCATION(flags) && !try_hard) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson allocatable = metaslab_group_allocatable(mg, rotor,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Avoid writing single-copy data to a failing,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * non-redundant vdev, unless we've already tried all
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * other vdevs.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson d == 0 && !try_hard && vd->vdev_children == 0) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If we don't need to try hard, then require that the
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * block be 1/8th of the device away from any other DVAs
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * in this BP. If we are trying hard, allow any offset
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * to be used (distance=0).
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t asize = vdev_psize_to_asize(vd, psize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t offset = metaslab_group_alloc(mg, zal, asize, txg,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we've just selected this metaslab group,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * figure out whether the corresponding vdev is
fa9e4066f08beec538e775443c5be79dd423fcabahrens * over- or under-used relative to the pool,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and set an allocation bias to even it out.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (mc->mc_aliquot == 0 && metaslab_bias_enabled) {
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson vu = (vs->vs_alloc * 100) / (vs->vs_space + 1);
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson cu = (mc->mc_alloc * 100) / (mc->mc_space + 1);
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * Calculate how much more or less we should
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * try to allocate from this device during
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * this iteration around the rotor.
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * For example, if a device is 80% full
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * and the pool is 20% full then we should
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * reduce allocations by 60% on this device.
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * mg_bias = (20 - 80) * 512K / 100 = -307K
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * This reduces allocations by 307K for this
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (atomic_add_64_nv(&mc->mc_aliquot, asize) >=
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick DVA_SET_GANG(&dva[d], !!(flags & METASLAB_GANG_HEADER));
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If we haven't tried hard, do so now.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, rotor, NULL, psize, d, TRACE_ENOSPC);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Free the block represented by DVA in the context of the specified
fa9e4066f08beec538e775443c5be79dd423fcabahrens * transaction group.
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_remove(msp->ms_alloctree[txg & TXG_MASK],
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY3U(offset + size, <=, msp->ms_start + msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY3U(range_tree_space(msp->ms_tree) + size, <=,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (range_tree_space(msp->ms_freetree[txg & TXG_MASK]) == 0)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(msp->ms_freetree[txg & TXG_MASK],
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * Intent log support: upon opening the pool after a crash, notify the SPA
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * of blocks that the intent log has allocated for immediate write, but
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * which are still considered free by the SPA because the last transaction
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * group didn't commit yet.
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if ((txg != 0 && spa_writeable(spa)) || !msp->ms_loaded)
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (error == 0 && !range_tree_contains(msp->ms_tree, offset, size))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (error || txg == 0) { /* txg == 0 indicates dry run */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY3U(range_tree_space(msp->ms_tree) - size, <=, msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_remove(msp->ms_tree, offset, size);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick if (spa_writeable(spa)) { /* don't dirty if we're zdb(1M) */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (range_tree_space(msp->ms_alloctree[txg & TXG_MASK]) == 0)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(msp->ms_alloctree[txg & TXG_MASK], offset, size);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (0);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Reserve some allocation slots. The reservation system must be called
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * before we call into the allocator. If there aren't any available slots
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * then the I/O will be throttled until an I/O completes and its slots are
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * freed up. The function returns true if it was successful in placing
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * the reservation.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_class_throttle_reserve(metaslab_class_t *mc, int slots, zio_t *zio,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson uint64_t reserved_slots = refcount_count(&mc->mc_alloc_slots);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson available_slots = mc->mc_alloc_max_slots - reserved_slots;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (slots <= available_slots || GANG_ALLOCATION(flags)) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We reserve the slots individually so that we can unreserve
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * them individually when an I/O completes.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (int d = 0; d < slots; d++) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson reserved_slots = refcount_add(&mc->mc_alloc_slots, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_class_throttle_unreserve(metaslab_class_t *mc, int slots, zio_t *zio)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (int d = 0; d < slots; d++) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson (void) refcount_remove(&mc->mc_alloc_slots, zio);
8654d0253136055bd4cc2423d87378e8a37f2eb5perrinmetaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson int ndvas, uint64_t txg, blkptr_t *hintbp, int flags,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (mc->mc_rotor == NULL) { /* no vdevs in this class */
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int d = 0; d < ndvas; d++) {
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick for (d--; d >= 0; d--) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Update the metaslab group's queue depth
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * based on the newly allocated dva.
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!now || bp->blk_birth >= spa_syncing_txg(spa));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_enter(spa, SCL_FREE, FTAG, RW_READER);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int d = 0; d < ndvas; d++)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * First do a dry run to make sure all DVAs are claimable,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * so we don't have to unwind from partial failures below.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int d = 0; d < ndvas; d++)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensmetaslab_check_free(spa_t *spa, const blkptr_t *bp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t size = DVA_GET_ASIZE(&bp->blk_dva[i]);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_verify(msp->ms_tree, offset, size);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (int j = 0; j < TXG_SIZE; j++)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_verify(msp->ms_freetree[j], offset, size);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (int j = 0; j < TXG_DEFER_SIZE; j++)