fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
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 *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
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 *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
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]
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zfs_context.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_tx.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/space_map.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/metaslab_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/vdev_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zio.h>
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson#include <sys/spa_impl.h>
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson#include <sys/zfeature.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#define GANG_ALLOCATION(flags) \
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER))
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson
13506d1eefbbc37e2f12a0528831d9f6d4c361d7maybeeuint64_t metaslab_aliquot = 512ULL << 10;
e05725b117836db173257fae43fb0746eb857fb5bonwickuint64_t metaslab_gang_bang = SPA_MAXBLOCKSIZE + 1; /* force gang blocks */
13506d1eefbbc37e2f12a0528831d9f6d4c361d7maybee
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson/*
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.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilsonint zfs_condense_pct = 200;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece/*
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
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * blocks.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece */
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reeceint zfs_metaslab_condense_block_threshold = 4;
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece
22e30981d82a0b6dc89253596ededafae8655e00George Wilson/*
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.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson */
22e30981d82a0b6dc89253596ededafae8655e00George Wilsonint zfs_mg_noalloc_threshold = 0;
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
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 */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonint zfs_mg_fragmentation_threshold = 85;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
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.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonint zfs_metaslab_fragmentation_threshold = 70;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * When set will load all metaslabs when pool is first opened.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonint metaslab_debug_load = 0;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * When set will prevent metaslabs from being unloaded.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonint metaslab_debug_unload = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson/*
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).
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson */
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrensuint64_t metaslab_df_alloc_threshold = SPA_OLD_MAXBLOCKSIZE;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson/*
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.
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson */
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonint metaslab_df_free_pct = 4;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson/*
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * A metaslab is considered "free" if it contains a contiguous
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * segment which is greater than metaslab_min_alloc_size.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonuint64_t metaslab_min_alloc_size = DMU_MAX_ACCESS;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Percentage of all cpus that can be used by the metaslab taskq.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonint metaslab_load_pct = 50;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson/*
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.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonint metaslab_unload_delay = TXG_SIZE * 2;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Max number of metaslabs per group to preload.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonint metaslab_preload_limit = SPA_DVAS_PER_BP;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Enable/disable preloading of metaslab.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonboolean_t metaslab_preload_enabled = B_TRUE;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Enable/disable fragmentation weighting on metaslabs.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonboolean_t metaslab_fragmentation_factor_enabled = B_TRUE;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Enable/disable lba weighting (i.e. outer tracks are given preference).
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonboolean_t metaslab_lba_weighting_enabled = B_TRUE;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Enable/disable metaslab group biasing.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonboolean_t metaslab_bias_enabled = B_TRUE;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Enable/disable segment-based metaslab selection.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonboolean_t zfs_metaslab_segment_weight_enabled = B_TRUE;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonint zfs_metaslab_switch_threshold = 2;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Internal switch to enable/disable the metaslab allocation tracing
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * facility.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonboolean_t metaslab_trace_enabled = B_TRUE;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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 Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonuint64_t metaslab_trace_max_entries = 5000;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic uint64_t metaslab_weight(metaslab_t *);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic void metaslab_set_fragmentation(metaslab_t *);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonkmem_cache_t *metaslab_alloc_trace_cache;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Metaslab classes
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_class_t *
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_class_create(spa_t *spa, metaslab_ops_t *ops)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_class_t *mc;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc = kmem_zalloc(sizeof (metaslab_class_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson mc->mc_spa = spa;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = NULL;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson mc->mc_ops = ops;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_init(&mc->mc_lock, NULL, MUTEX_DEFAULT, NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson refcount_create_tracked(&mc->mc_alloc_slots);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (mc);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_class_destroy(metaslab_class_t *mc)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mc->mc_rotor == NULL);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mc->mc_alloc == 0);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mc->mc_deferred == 0);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mc->mc_space == 0);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mc->mc_dspace == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson refcount_destroy(&mc->mc_alloc_slots);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_destroy(&mc->mc_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(mc, sizeof (metaslab_class_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilsonint
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilsonmetaslab_class_validate(metaslab_class_t *mc)
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson{
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson metaslab_group_t *mg;
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson vdev_t *vd;
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson /*
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson * Must hold one of the spa_config locks.
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson */
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));
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson if ((mg = mc->mc_rotor) == NULL)
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson return (0);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson do {
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson vd = mg->mg_vd;
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson ASSERT(vd->vdev_mg != NULL);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson ASSERT3P(vd->vdev_top, ==, vd);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson ASSERT3P(mg->mg_class, ==, mc);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson ASSERT3P(vd->vdev_ops, !=, &vdev_hole_ops);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson } while ((mg = mg->mg_next) != mc->mc_rotor);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson return (0);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson}
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
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 Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick atomic_add_64(&mc->mc_alloc, alloc_delta);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick atomic_add_64(&mc->mc_deferred, defer_delta);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick atomic_add_64(&mc->mc_space, space_delta);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick atomic_add_64(&mc->mc_dspace, dspace_delta);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickuint64_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickmetaslab_class_get_alloc(metaslab_class_t *mc)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (mc->mc_alloc);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickuint64_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickmetaslab_class_get_deferred(metaslab_class_t *mc)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (mc->mc_deferred);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickuint64_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickmetaslab_class_get_space(metaslab_class_t *mc)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (mc->mc_space);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickuint64_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickmetaslab_class_get_dspace(metaslab_class_t *mc)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (spa_deflate(mc->mc_spa) ? mc->mc_dspace : mc->mc_space);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonvoid
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_class_histogram_verify(metaslab_class_t *mc)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson vdev_t *rvd = mc->mc_spa->spa_root_vdev;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t *mc_hist;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson int i;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if ((zfs_flags & ZFS_DEBUG_HISTOGRAM_VERIFY) == 0)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mc_hist = kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson KM_SLEEP);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson vdev_t *tvd = rvd->vdev_child[c];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Skip any holes, uninitialized top-levels, or
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * vdevs that are not in this metalab class.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (tvd->vdev_ishole || tvd->vdev_ms_shift == 0 ||
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_class != mc) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson continue;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mc_hist[i] += mg->mg_histogram[i];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson VERIFY3U(mc_hist[i], ==, mc->mc_histogram[i]);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson kmem_free(mc_hist, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
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 Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonuint64_t
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_class_fragmentation(metaslab_class_t *mc)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson vdev_t *rvd = mc->mc_spa->spa_root_vdev;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t fragmentation = 0;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson spa_config_enter(mc->mc_spa, SCL_VDEV, FTAG, RW_READER);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson vdev_t *tvd = rvd->vdev_child[c];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Skip any holes, uninitialized top-levels, or
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * vdevs that are not in this metalab class.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (tvd->vdev_ishole || tvd->vdev_ms_shift == 0 ||
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_class != mc) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson continue;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * If a metaslab group does not contain a fragmentation
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * metric then just bail out.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson spa_config_exit(mc->mc_spa, SCL_VDEV, FTAG);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return (ZFS_FRAG_INVALID);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * Determine how much this metaslab_group is contributing
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * to the overall pool fragmentation metric.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson fragmentation += mg->mg_fragmentation *
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_get_space(mg);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson fragmentation /= metaslab_class_get_space(mc);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT3U(fragmentation, <=, 100);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson spa_config_exit(mc->mc_spa, SCL_VDEV, FTAG);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return (fragmentation);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
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 Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonuint64_t
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_class_expandable_space(metaslab_class_t *mc)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson vdev_t *rvd = mc->mc_spa->spa_root_vdev;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t space = 0;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
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 vdev_t *tvd = rvd->vdev_child[c];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (tvd->vdev_ishole || tvd->vdev_ms_shift == 0 ||
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_class != mc) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson continue;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
c39a2aae1e2c439d156021edfc20910dad7f9891George Wilson /*
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 */
c39a2aae1e2c439d156021edfc20910dad7f9891George Wilson space += P2ALIGN(tvd->vdev_max_asize - tvd->vdev_asize,
c39a2aae1e2c439d156021edfc20910dad7f9891George Wilson 1ULL << tvd->vdev_ms_shift);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson spa_config_exit(mc->mc_spa, SCL_VDEV, FTAG);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return (space);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_compare(const void *x1, const void *x2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens const metaslab_t *m1 = x1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens const metaslab_t *m2 = x2;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (m1->ms_weight < m2->ms_weight)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (m1->ms_weight > m2->ms_weight)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the weights are identical, use the offset to force uniqueness.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (m1->ms_start < m2->ms_start)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (m1->ms_start > m2->ms_start)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3P(m1, ==, m2);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Verify that the space accounting on disk matches the in-core range_trees.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonvoid
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_verify_space(metaslab_t *msp, uint64_t txg)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t allocated = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t freed = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t sm_free_space, msp_free_space;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if ((zfs_flags & ZFS_DEBUG_METASLAB_VERIFY) == 0)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (txg != spa_syncing_txg(spa) || msp->ms_sm == NULL ||
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson !msp->ms_loaded)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson sm_free_space = msp->ms_size - space_map_allocated(msp->ms_sm) -
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson space_map_alloc_delta(msp->ms_sm);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Account for future allocations since we would have already
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * deducted that space from the ms_freetree.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (int t = 0; t < TXG_CONCURRENT_STATES; t++) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson allocated +=
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_space(msp->ms_alloctree[(txg + t) & TXG_MASK]);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson freed = range_tree_space(msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK]);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp_free_space = range_tree_space(msp->ms_tree) + allocated +
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_deferspace + freed;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson VERIFY3U(sm_free_space, ==, msp_free_space);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Metaslab groups
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
22e30981d82a0b6dc89253596ededafae8655e00George 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 Wilson */
22e30981d82a0b6dc89253596ededafae8655e00George Wilsonstatic void
22e30981d82a0b6dc89253596ededafae8655e00George Wilsonmetaslab_group_alloc_update(metaslab_group_t *mg)
22e30981d82a0b6dc89253596ededafae8655e00George Wilson{
22e30981d82a0b6dc89253596ededafae8655e00George Wilson vdev_t *vd = mg->mg_vd;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson metaslab_class_t *mc = mg->mg_class;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson vdev_stat_t *vs = &vd->vdev_stat;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson boolean_t was_allocatable;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson boolean_t was_initialized;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson ASSERT(vd == vd->vdev_top);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson mutex_enter(&mg->mg_lock);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson was_allocatable = mg->mg_allocatable;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson was_initialized = mg->mg_initialized;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson mg->mg_free_capacity = ((vs->vs_space - vs->vs_alloc) * 100) /
22e30981d82a0b6dc89253596ededafae8655e00George Wilson (vs->vs_space + 1);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_enter(&mc->mc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
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 */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mg->mg_initialized = metaslab_group_initialized(mg);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!was_initialized && mg->mg_initialized) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mc->mc_groups++;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson } else if (was_initialized && !mg->mg_initialized) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(mc->mc_groups, >, 0);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mc->mc_groups--;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (mg->mg_initialized)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mg->mg_no_free_space = B_FALSE;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
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).
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
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_FRAG_INVALID ||
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_fragmentation <= zfs_mg_fragmentation_threshold));
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson /*
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 */
22e30981d82a0b6dc89253596ededafae8655e00George Wilson if (was_allocatable && !mg->mg_allocatable)
22e30981d82a0b6dc89253596ededafae8655e00George Wilson mc->mc_alloc_groups--;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson else if (!was_allocatable && mg->mg_allocatable)
22e30981d82a0b6dc89253596ededafae8655e00George Wilson mc->mc_alloc_groups++;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_exit(&mc->mc_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson mutex_exit(&mg->mg_lock);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson}
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg = kmem_zalloc(sizeof (metaslab_group_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_create(&mg->mg_metaslab_tree, metaslab_compare,
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_vd = vd;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_class = mc;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_activation_count = 0;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mg->mg_initialized = B_FALSE;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mg->mg_no_free_space = B_TRUE;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson refcount_create_tracked(&mg->mg_alloc_queue_depth);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
be082110c08433beadb738ad3be035a73d995ea8George Wilson mg->mg_taskq = taskq_create("metaslab_group_taskq", metaslab_load_pct,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson minclsyspri, 10, INT_MAX, TASKQ_THREADS_CPU_PCT);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_destroy(metaslab_group_t *mg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_prev == NULL);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_next == NULL);
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley /*
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.
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley */
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley ASSERT(mg->mg_activation_count <= 0);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
be082110c08433beadb738ad3be035a73d995ea8George Wilson taskq_destroy(mg->mg_taskq);
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_destroy(&mg->mg_metaslab_tree);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_destroy(&mg->mg_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson refcount_destroy(&mg->mg_alloc_queue_depth);
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(mg, sizeof (metaslab_group_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwickvoid
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwickmetaslab_group_activate(metaslab_group_t *mg)
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick{
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick metaslab_class_t *mc = mg->mg_class;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick metaslab_group_t *mgprev, *mgnext;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mc->mc_rotor != mg);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_prev == NULL);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_next == NULL);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_activation_count <= 0);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick if (++mg->mg_activation_count <= 0)
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick return;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_aliquot = metaslab_aliquot * MAX(1, mg->mg_vd->vdev_children);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson metaslab_group_alloc_update(mg);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick if ((mgprev = mc->mc_rotor) == NULL) {
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_prev = mg;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_next = mg;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick } else {
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mgnext = mgprev->mg_next;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_prev = mgprev;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_next = mgnext;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mgprev->mg_next = mg;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mgnext->mg_prev = mg;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick }
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mc->mc_rotor = mg;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick}
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwickvoid
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwickmetaslab_group_passivate(metaslab_group_t *mg)
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick{
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick metaslab_class_t *mc = mg->mg_class;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick metaslab_group_t *mgprev, *mgnext;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick if (--mg->mg_activation_count != 0) {
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mc->mc_rotor != mg);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_prev == NULL);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_next == NULL);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_activation_count < 0);
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick return;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick }
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson taskq_wait(mg->mg_taskq);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_alloc_update(mg);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mgprev = mg->mg_prev;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mgnext = mg->mg_next;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick if (mg == mgnext) {
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mc->mc_rotor = NULL;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick } else {
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mc->mc_rotor = mgnext;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mgprev->mg_next = mgnext;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mgnext->mg_prev = mgprev;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick }
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_prev = NULL;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick mg->mg_next = NULL;
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick}
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonboolean_t
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_initialized(metaslab_group_t *mg)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson vdev_t *vd = mg->mg_vd;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson vdev_stat_t *vs = &vd->vdev_stat;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (vs->vs_space != 0 && mg->mg_activation_count > 0);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonuint64_t
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_get_space(metaslab_group_t *mg)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return ((1ULL << mg->mg_vd->vdev_ms_shift) * mg->mg_vd->vdev_ms_count);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonvoid
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_histogram_verify(metaslab_group_t *mg)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t *mg_hist;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson vdev_t *vd = mg->mg_vd;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t ashift = vd->vdev_ashift;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson int i;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if ((zfs_flags & ZFS_DEBUG_HISTOGRAM_VERIFY) == 0)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg_hist = kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson KM_SLEEP);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT3U(RANGE_TREE_HISTOGRAM_SIZE, >=,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson SPACE_MAP_HISTOGRAM_SIZE + ashift);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_t *msp = vd->vdev_ms[m];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_sm == NULL)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson continue;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg_hist[i + ashift] +=
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i ++)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson VERIFY3U(mg_hist[i], ==, mg->mg_histogram[i]);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson kmem_free(mg_hist, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_histogram_add(metaslab_group_t *mg, metaslab_t *msp)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_class_t *mc = mg->mg_class;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t ashift = mg->mg_vd->vdev_ashift;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_sm == NULL)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_enter(&mg->mg_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_histogram[i + ashift] +=
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mc->mc_histogram[i + ashift] +=
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_exit(&mg->mg_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonvoid
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_histogram_remove(metaslab_group_t *mg, metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_class_t *mc = mg->mg_class;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t ashift = mg->mg_vd->vdev_ashift;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_sm == NULL)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&mg->mg_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT3U(mg->mg_histogram[i + ashift], >=,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i]);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT3U(mc->mc_histogram[i + ashift], >=,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i]);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_histogram[i + ashift] -=
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mc->mc_histogram[i + ashift] -=
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_exit(&mg->mg_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonstatic void
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(msp->ms_group == NULL);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_enter(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp->ms_group = mg;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick msp->ms_weight = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_add(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&mg->mg_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_enter(&msp->ms_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_histogram_add(mg, msp);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_enter(&msp->ms_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_histogram_remove(mg, msp);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mutex_exit(&msp->ms_lock);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(msp->ms_group == mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_remove(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp->ms_group = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens /*
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * Although in principle the weight can be any value, in
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * practice we do not use values in the range [1, 511].
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT(weight >= SPA_MINBLOCKSIZE || weight == 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(MUTEX_HELD(&msp->ms_lock));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(msp->ms_group == mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_remove(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp->ms_weight = weight;
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_add(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
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 Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonuint64_t
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonmetaslab_group_fragmentation(metaslab_group_t *mg)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson vdev_t *vd = mg->mg_vd;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t fragmentation = 0;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t valid_ms = 0;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_t *msp = vd->vdev_ms[m];
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_fragmentation == ZFS_FRAG_INVALID)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson continue;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson valid_ms++;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson fragmentation += msp->ms_fragmentation;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (valid_ms <= vd->vdev_ms_count / 2)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return (ZFS_FRAG_INVALID);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson fragmentation /= valid_ms;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT3U(fragmentation, <=, 100);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson return (fragmentation);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson}
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson/*
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.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson */
22e30981d82a0b6dc89253596ededafae8655e00George Wilsonstatic boolean_t
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_allocatable(metaslab_group_t *mg, metaslab_group_t *rotor,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson uint64_t psize)
22e30981d82a0b6dc89253596ededafae8655e00George Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson spa_t *spa = mg->mg_vd->vdev_spa;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson metaslab_class_t *mc = mg->mg_class;
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson /*
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.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (mc != spa_normal_class(spa) || mc->mc_groups <= 1)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
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 */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (mg->mg_allocatable) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_t *mgp;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson int64_t qdepth;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson uint64_t qmax = mg->mg_max_alloc_queue_depth;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!mc->mc_alloc_throttle_enabled)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If this metaslab group does not have any free space, then
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * there is no point in looking further.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (mg->mg_no_free_space)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_FALSE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson qdepth = refcount_count(&mg->mg_alloc_queue_depth);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
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 */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (qdepth < qmax || mc->mc_alloc_groups == 1)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(mc->mc_alloc_groups, >, 1);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
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 */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (mgp = mg->mg_next; mgp != rotor; mgp = mgp->mg_next) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson qmax = mgp->mg_max_alloc_queue_depth;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson qdepth = refcount_count(&mgp->mg_alloc_queue_depth);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
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 */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (qdepth < qmax && !mgp->mg_no_free_space)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_FALSE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
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 */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson } else if (mc->mc_alloc_groups == 0 || psize == SPA_MINBLOCKSIZE) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (B_FALSE);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson}
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson/*
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Range tree callbacks
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge 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 Wilson */
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonstatic int
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rangesize_compare(const void *x1, const void *x2)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson const range_seg_t *r1 = x1;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson const range_seg_t *r2 = 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;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (rs_size1 < rs_size2)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (-1);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (rs_size1 > rs_size2)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (1);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (r1->rs_start < r2->rs_start)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (-1);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (r1->rs_start > r2->rs_start)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (1);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (0);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson}
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick/*
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.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic void
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_create(range_tree_t *rt, void *arg)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp = arg;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(rt->rt_arg, ==, msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_tree == NULL);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_create(&msp->ms_size_tree, metaslab_rangesize_compare,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson sizeof (range_seg_t), offsetof(range_seg_t, rs_pp_node));
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson}
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Destroy the block allocator specific components.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilsonstatic void
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_destroy(range_tree_t *rt, void *arg)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp = arg;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(rt->rt_arg, ==, msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(msp->ms_tree, ==, rt);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT0(avl_numnodes(&msp->ms_size_tree));
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_destroy(&msp->ms_size_tree);
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson}
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilsonstatic void
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_add(range_tree_t *rt, range_seg_t *rs, void *arg)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp = arg;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(rt->rt_arg, ==, msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(msp->ms_tree, ==, rt);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY(!msp->ms_condensing);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_add(&msp->ms_size_tree, rs);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_remove(range_tree_t *rt, range_seg_t *rs, void *arg)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp = arg;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(rt->rt_arg, ==, msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(msp->ms_tree, ==, rt);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY(!msp->ms_condensing);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_remove(&msp->ms_size_tree, rs);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_rt_vacate(range_tree_t *rt, void *arg)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp = arg;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(rt->rt_arg, ==, msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(msp->ms_tree, ==, rt);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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 */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_create(&msp->ms_size_tree, metaslab_rangesize_compare,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson sizeof (range_seg_t), offsetof(range_seg_t, rs_pp_node));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic range_tree_ops_t metaslab_rt_ops = {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_rt_create,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_rt_destroy,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_rt_add,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_rt_remove,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_rt_vacate
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson};
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Common allocator routines
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * ==========================================================================
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson/*
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * Return the maximum contiguous segment within the metaslab.
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson */
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilsonuint64_t
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_block_maxsize(metaslab_t *msp)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_tree_t *t = &msp->ms_size_tree;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_seg_t *rs;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (t == NULL || (rs = avl_last(t)) == NULL)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson return (0ULL);
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (rs->rs_end - rs->rs_start);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic range_seg_t *
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_block_find(avl_tree_t *t, uint64_t start, uint64_t size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_seg_t *rs, rsearch;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson avl_index_t where;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson rsearch.rs_start = start;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson rsearch.rs_end = start + size;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson rs = avl_find(t, &rsearch, &where);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (rs == NULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson rs = avl_nearest(t, where, AVL_AFTER);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (rs);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
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 Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic uint64_t
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_block_picker(avl_tree_t *t, uint64_t *cursor, uint64_t size,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t align)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_seg_t *rs = metaslab_block_find(t, *cursor, size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson while (rs != NULL) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t offset = P2ROUNDUP(rs->rs_start, align);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (offset + size <= rs->rs_end) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson *cursor = offset + size;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (offset);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rs = AVL_NEXT(t, rs);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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 */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (*cursor == 0)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (-1ULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson *cursor = 0;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (metaslab_block_picker(t, cursor, size, align));
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson}
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson/*
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * The first-fit block allocator
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonstatic uint64_t
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_ff_alloc(metaslab_t *msp, uint64_t size)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson uint64_t align = size & -size;
bf16b11e8deb633dd6c4296d46e92399d1582df4Matthew Ahrens uint64_t *cursor = &msp->ms_lbas[highbit64(align) - 1];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_tree_t *t = &msp->ms_tree->rt_root;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (metaslab_block_picker(t, cursor, size, align));
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson}
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic metaslab_ops_t metaslab_ff_ops = {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_ff_alloc
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson};
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson/*
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 * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilsonstatic uint64_t
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_df_alloc(metaslab_t *msp, uint64_t size)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson uint64_t align = size & -size;
bf16b11e8deb633dd6c4296d46e92399d1582df4Matthew Ahrens uint64_t *cursor = &msp->ms_lbas[highbit64(align) - 1];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t *rt = msp->ms_tree;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_tree_t *t = &rt->rt_root;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t max_size = metaslab_block_maxsize(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&msp->ms_size_tree));
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson if (max_size < size)
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson return (-1ULL);
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson /*
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * If we're running low on space switch to using the size
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson * sorted AVL tree (best-fit).
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson */
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson if (max_size < metaslab_df_alloc_threshold ||
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson free_pct < metaslab_df_free_pct) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson t = &msp->ms_size_tree;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson *cursor = 0;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson }
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson return (metaslab_block_picker(t, cursor, size, 1ULL));
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson}
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic metaslab_ops_t metaslab_df_ops = {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_df_alloc
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson};
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson/*
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 * ==========================================================================
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonstatic uint64_t
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_cf_alloc(metaslab_t *msp, uint64_t size)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t *rt = msp->ms_tree;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_tree_t *t = &msp->ms_size_tree;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t *cursor = &msp->ms_lbas[0];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t *cursor_end = &msp->ms_lbas[1];
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson uint64_t offset = 0;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&rt->rt_root));
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3U(*cursor_end, >=, *cursor);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if ((*cursor + size) > *cursor_end) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_seg_t *rs;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rs = avl_last(&msp->ms_size_tree);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (rs == NULL || (rs->rs_end - rs->rs_start) < size)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (-1ULL);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson *cursor = rs->rs_start;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson *cursor_end = rs->rs_end;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson offset = *cursor;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson *cursor += size;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (offset);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson}
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic metaslab_ops_t metaslab_cf_ops = {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_cf_alloc
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson};
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
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 */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Determines desired number of contiguous blocks (2^metaslab_ndf_clump_shift)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * to request from the allocator.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
8d18220deb04ec7b12410cd90deb4d45e66d49bfMark J Musanteuint64_t metaslab_ndf_clump_shift = 4;
8d18220deb04ec7b12410cd90deb4d45e66d49bfMark J Musante
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonstatic uint64_t
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_ndf_alloc(metaslab_t *msp, uint64_t size)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_tree_t *t = &msp->ms_tree->rt_root;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson avl_index_t where;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_seg_t *rs, rsearch;
bf16b11e8deb633dd6c4296d46e92399d1582df4Matthew Ahrens uint64_t hbit = highbit64(size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t *cursor = &msp->ms_lbas[hbit - 1];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t max_size = metaslab_block_maxsize(msp);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&msp->ms_size_tree));
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson if (max_size < size)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (-1ULL);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rsearch.rs_start = *cursor;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rsearch.rs_end = *cursor + size;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rs = avl_find(t, &rsearch, &where);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (rs == NULL || (rs->rs_end - rs->rs_start) < size) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson t = &msp->ms_size_tree;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rsearch.rs_start = 0;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rsearch.rs_end = MIN(max_size,
8d18220deb04ec7b12410cd90deb4d45e66d49bfMark J Musante 1ULL << (hbit + metaslab_ndf_clump_shift));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rs = avl_find(t, &rsearch, &where);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (rs == NULL)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rs = avl_nearest(t, where, AVL_AFTER);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(rs != NULL);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson }
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if ((rs->rs_end - rs->rs_start) >= size) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson *cursor = rs->rs_start + size;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (rs->rs_start);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson }
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson return (-1ULL);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson}
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic metaslab_ops_t metaslab_ndf_ops = {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_ndf_alloc
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick};
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_ops_t *zfs_metaslab_ops = &metaslab_df_ops;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Metaslabs
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Wait for any in-progress metaslab loads to complete.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonvoid
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_load_wait(metaslab_t *msp)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson while (msp->ms_loading) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(!msp->ms_loaded);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson cv_wait(&msp->ms_load_cv, &msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonint
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_load(metaslab_t *msp)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson int error = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson boolean_t success = B_FALSE;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(!msp->ms_loaded);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(!msp->ms_loading);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_loading = B_TRUE;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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 * ms_tree.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_sm != NULL)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson error = space_map_load(msp->ms_sm, msp->ms_tree, SM_FREE);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson else
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(msp->ms_tree, msp->ms_start, msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson success = (error == 0);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_loading = B_FALSE;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (success) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT3P(msp->ms_group, !=, NULL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_loaded = B_TRUE;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_walk(msp->ms_defertree[t],
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_remove, msp->ms_tree);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson cv_broadcast(&msp->ms_load_cv);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (error);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonvoid
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_unload(metaslab_t *msp)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_vacate(msp->ms_tree, NULL, NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_loaded = B_FALSE;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_weight &= ~METASLAB_ACTIVE_MASK;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = 0;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Suryaint
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Suryametaslab_init(metaslab_group_t *mg, uint64_t id, uint64_t object, uint64_t txg,
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya metaslab_t **msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd = mg->mg_vd;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson objset_t *mos = vd->vdev_spa->spa_meta_objset;
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya metaslab_t *ms;
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya int error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
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);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ms->ms_id = id;
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ms->ms_start = id << vd->vdev_ms_shift;
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ms->ms_size = 1ULL << vd->vdev_ms_shift;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (object != 0) {
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya error = space_map_open(&ms->ms_sm, mos, object, ms->ms_start,
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ms->ms_size, vd->vdev_ashift, &ms->ms_lock);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya if (error != 0) {
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya kmem_free(ms, sizeof (metaslab_t));
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya return (error);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya }
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ASSERT(ms->ms_sm != NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
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.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya ms->ms_tree = range_tree_create(&metaslab_rt_ops, ms, &ms->ms_lock);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya metaslab_group_add(mg, ms);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_set_fragmentation(ms);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (txg <= TXG_INITIAL)
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya metaslab_sync_done(ms, 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya if (metaslab_debug_load && ms->ms_sm != NULL) {
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya mutex_enter(&ms->ms_lock);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya VERIFY0(metaslab_load(ms));
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya mutex_exit(&ms->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (txg != 0) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_dirty(vd, 0, NULL, txg);
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya vdev_dirty(vd, VDD_METASLAB, ms, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya *msp = ms;
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya
1e9bd7ec42f2d3bf854c2da35310901194833267Prakash Surya return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_fini(metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mg = msp->ms_group;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_remove(mg, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY(msp->ms_group == NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson vdev_space_update(mg->mg_vd, -space_map_allocated(msp->ms_sm),
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson 0, -msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_close(msp->ms_sm);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_unload(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_destroy(msp->ms_tree);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley for (int t = 0; t < TXG_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_destroy(msp->ms_alloctree[t]);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_destroy(msp->ms_freetree[t]);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_destroy(msp->ms_defertree[t]);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson }
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(msp->ms_deferspace);
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson cv_destroy(&msp->ms_load_cv);
5ad820458efd0fdb914baff9c1447c22b819fa23nd mutex_destroy(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(msp, sizeof (metaslab_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson#define FRAGMENTATION_TABLE_SIZE 17
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson/*
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.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson *
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 Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilsonint zfs_frag_table[FRAGMENTATION_TABLE_SIZE] = {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 100, /* 512B */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 100, /* 1K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 98, /* 2K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 95, /* 4K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 90, /* 8K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 80, /* 16K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 70, /* 32K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 60, /* 64K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 50, /* 128K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 40, /* 256K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 30, /* 512K */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 20, /* 1M */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 15, /* 2M */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 10, /* 4M */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 5, /* 8M */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson 0 /* 16M */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson};
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
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].
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic void
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_set_fragmentation(metaslab_t *msp)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t fragmentation = 0;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t total = 0;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson boolean_t feature_enabled = spa_feature_is_enabled(spa,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson SPA_FEATURE_SPACEMAP_HISTOGRAM);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!feature_enabled) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_fragmentation = ZFS_FRAG_INVALID;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * A null space map means that the entire metaslab is free
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson * and thus is not fragmented.
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_sm == NULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_fragmentation = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
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 */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (msp->ms_sm->sm_dbuf->db_size != sizeof (space_map_phys_t)) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t txg = spa_syncing_txg(spa);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson vdev_t *vd = msp->ms_group->mg_vd;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens if (spa_writeable(spa)) {
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens msp->ms_condense_wanted = B_TRUE;
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens vdev_dirty(vd, VDD_METASLAB, msp, txg + 1);
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens spa_dbgmsg(spa, "txg %llu, requesting force condense: "
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens "msp %p, vd %p", txg, msp, vd);
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_fragmentation = ZFS_FRAG_INVALID;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint64_t space = 0;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson uint8_t shift = msp->ms_sm->sm_shift;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson int idx = MIN(shift - SPA_MINBLOCKSHIFT + i,
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson FRAGMENTATION_TABLE_SIZE - 1);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_sm->sm_phys->smp_histogram[i] == 0)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson continue;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson space = msp->ms_sm->sm_phys->smp_histogram[i] << (i + shift);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson total += space;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT3U(idx, <, FRAGMENTATION_TABLE_SIZE);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson fragmentation += space * zfs_frag_table[idx];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (total > 0)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson fragmentation /= total;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT3U(fragmentation, <=, 100);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_fragmentation = fragmentation;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson/*
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.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic uint64_t
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_space_weight(metaslab_t *msp)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm metaslab_group_t *mg = msp->ms_group;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm vdev_t *vd = mg->mg_vd;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t weight, space;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(MUTEX_HELD(&msp->ms_lock));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(!vd->vdev_removing);
9eb57f7f3fbb970d4b9b89dcd5ecf543fe2414d5George Wilson
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The baseline weight is the metaslab's free space.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space = msp->ms_size - space_map_allocated(msp->ms_sm);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (metaslab_fragmentation_factor_enabled &&
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_fragmentation != ZFS_FRAG_INVALID) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
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 * by 1.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson space = (space * (100 - (msp->ms_fragmentation - 1))) / 100;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
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.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (space > 0 && space < SPA_MINBLOCKSIZE)
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson space = SPA_MINBLOCKSIZE;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick weight = space;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
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.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (metaslab_lba_weighting_enabled) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson weight = 2 * weight - (msp->ms_id * weight) / vd->vdev_ms_count;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson ASSERT(weight >= space && weight <= 2 * space);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
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 */
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);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson }
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_SPACEBASED(weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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 Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic uint64_t
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_weight_from_range_tree(metaslab_t *msp)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t weight = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint32_t segments = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(msp->ms_loaded);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (int i = RANGE_TREE_HISTOGRAM_SIZE - 1; i >= SPA_MINBLOCKSHIFT;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson i--) {
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
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson segments <<= 1;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson segments += msp->ms_tree->rt_histogram[i];
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (i > max_idx)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson continue;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (segments != 0) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_COUNT(weight, segments);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_INDEX(weight, i);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_ACTIVE(weight, 0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson break;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic uint64_t
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_weight_from_spacemap(metaslab_t *msp)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t weight = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
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 WEIGHT_SET_COUNT(weight,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_sm->sm_phys->smp_histogram[i]);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_INDEX(weight, i +
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_sm->sm_shift);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_ACTIVE(weight, 0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson break;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic uint64_t
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_segment_weight(metaslab_t *msp)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_group_t *mg = msp->ms_group;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t weight = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint8_t shift = mg->mg_vd->vdev_ashift;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * The metaslab is completely free.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (space_map_allocated(msp->ms_sm) == 0) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson int idx = highbit64(msp->ms_size) - 1;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson int max_idx = SPACE_MAP_HISTOGRAM_SIZE + shift - 1;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (idx < max_idx) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_COUNT(weight, 1ULL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_INDEX(weight, idx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson } else {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_COUNT(weight, 1ULL << (idx - max_idx));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_INDEX(weight, max_idx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_ACTIVE(weight, 0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(!WEIGHT_IS_SPACEBASED(weight));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT3U(msp->ms_sm->sm_dbuf->db_size, ==, sizeof (space_map_phys_t));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If the metaslab is fully allocated then just make the weight 0.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (space_map_allocated(msp->ms_sm) == msp->ms_size)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_loaded) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson weight = metaslab_weight_from_range_tree(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson } else {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson weight = metaslab_weight_from_spacemap(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_activation_weight != 0 && weight != 0)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_ACTIVE(weight, WEIGHT_GET_ACTIVE(msp->ms_weight));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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 Wilson * type bit).
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonboolean_t
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_should_allocate(metaslab_t *msp, uint64_t asize)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson boolean_t should_allocate;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_max_size != 0)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (msp->ms_max_size >= asize);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!WEIGHT_IS_SPACEBASED(msp->ms_weight)) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson should_allocate = (asize <
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson 1ULL << (WEIGHT_GET_INDEX(msp->ms_weight) + 1));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson } else {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson should_allocate = (asize <=
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson (msp->ms_weight & ~METASLAB_WEIGHT_TYPE));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (should_allocate);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic uint64_t
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_weight(metaslab_t *msp)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson vdev_t *vd = msp->ms_group->mg_vd;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson spa_t *spa = vd->vdev_spa;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t weight;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * This vdev is in the process of being removed so there is nothing
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * for us to do here.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (vd->vdev_removing) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT0(space_map_allocated(msp->ms_sm));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT0(vd->vdev_ms_shift);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_set_fragmentation(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_loaded)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Segment-based weighting requires space map histogram support.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (zfs_metaslab_segment_weight_enabled &&
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM) &&
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson (msp->ms_sm == NULL || msp->ms_sm->sm_dbuf->db_size ==
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson sizeof (space_map_phys_t))) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson weight = metaslab_segment_weight(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson } else {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson weight = metaslab_space_weight(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return (weight);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic int
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilsonmetaslab_activate(metaslab_t *msp, uint64_t activation_weight)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(MUTEX_HELD(&msp->ms_lock));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_load_wait(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (!msp->ms_loaded) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson int error = metaslab_load(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (error) {
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley metaslab_group_sort(msp->ms_group, msp, 0);
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley return (error);
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_activation_weight = msp->ms_weight;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_group_sort(msp->ms_group, msp,
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm msp->ms_weight | activation_weight);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_loaded);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_passivate(metaslab_t *msp, uint64_t weight)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t size = weight & ~METASLAB_WEIGHT_TYPE;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens /*
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.
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(size >= SPA_MINBLOCKSIZE ||
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_space(msp->ms_tree) == 0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT0(weight & METASLAB_ACTIVE_MASK);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_activation_weight = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_group_sort(msp->ms_group, msp, weight);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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 Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonvoid
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_segment_may_passivate(metaslab_t *msp)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (WEIGHT_IS_SPACEBASED(msp->ms_weight) || spa_sync_pass(spa) > 1)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
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 int current_idx = WEIGHT_GET_INDEX(weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (current_idx <= activation_idx - zfs_metaslab_switch_threshold)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_passivate(msp, weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic void
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_preload(void *arg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp = arg;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
30beaff42d8240ebf5386e8b7a14e3d137a1631fGeorge Wilson ASSERT(!MUTEX_HELD(&msp->ms_group->mg_lock));
30beaff42d8240ebf5386e8b7a14e3d137a1631fGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson mutex_enter(&msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_load_wait(msp);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (!msp->ms_loaded)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson (void) metaslab_load(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_selected_txg = spa_syncing_txg(spa);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson mutex_exit(&msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonstatic void
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilsonmetaslab_group_preload(metaslab_group_t *mg)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson spa_t *spa = mg->mg_vd->vdev_spa;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_t *msp;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson avl_tree_t *t = &mg->mg_metaslab_tree;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson int m = 0;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (spa_shutting_down(spa) || !metaslab_preload_enabled) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson taskq_wait(mg->mg_taskq);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson return;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
30beaff42d8240ebf5386e8b7a14e3d137a1631fGeorge Wilson mutex_enter(&mg->mg_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
30beaff42d8240ebf5386e8b7a14e3d137a1631fGeorge Wilson * Load the next potential metaslabs
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (msp = avl_first(t); msp != NULL; msp = AVL_NEXT(t, msp)) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
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 */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (++m > metaslab_preload_limit && !msp->ms_condense_wanted) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson continue;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY(taskq_dispatch(mg->mg_taskq, metaslab_preload,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp, TQ_SLEEP) != NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson mutex_exit(&mg->mg_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson}
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson/*
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 *
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 *
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).
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson *
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * 3. The on-disk size of the space map should actually decrease.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece *
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 *
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 *
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.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilsonstatic boolean_t
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilsonmetaslab_should_condense(metaslab_t *msp)
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_t *sm = msp->ms_sm;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_seg_t *rs;
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece uint64_t size, entries, segsz, object_size, optimal_size, record_size;
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece dmu_object_info_t doi;
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece uint64_t vdev_blocksize = 1 << msp->ms_group->mg_vd->vdev_ashift;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_loaded);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
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 */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson rs = avl_last(&msp->ms_size_tree);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (rs == NULL || msp->ms_condense_wanted)
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson return (B_TRUE);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
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.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson size = (rs->rs_end - rs->rs_start) >> sm->sm_shift;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson entries = size / (MIN(size, SM_RUN_MAX));
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson segsz = entries * sizeof (uint64_t);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece optimal_size = sizeof (uint64_t) * avl_numnodes(&msp->ms_tree->rt_root);
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece object_size = space_map_length(msp->ms_sm);
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece dmu_object_info_from_db(sm->sm_dbuf, &doi);
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece record_size = MAX(doi.doi_data_block_size, vdev_blocksize);
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece return (segsz <= object_size &&
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece object_size >= (optimal_size * zfs_condense_pct / 100) &&
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece object_size > zfs_metaslab_condense_block_threshold * record_size);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson}
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson/*
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 Wilson */
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilsonstatic void
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilsonmetaslab_condense(metaslab_t *msp, uint64_t txg, dmu_tx_t *tx)
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson{
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t *freetree = msp->ms_freetree[txg & TXG_MASK];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t *condense_tree;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_t *sm = msp->ms_sm;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson ASSERT(MUTEX_HELD(&msp->ms_lock));
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson ASSERT3U(spa_sync_pass(spa), ==, 1);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_loaded);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
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 msp->ms_id, msp, msp->ms_group->mg_vd->vdev_id,
daec38ecb4fb5e73e4ca9e99be84f6b8c50c02faJoe Stein msp->ms_group->mg_vd->vdev_spa->spa_name,
daec38ecb4fb5e73e4ca9e99be84f6b8c50c02faJoe Stein space_map_length(msp->ms_sm), avl_numnodes(&msp->ms_tree->rt_root),
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_condense_wanted ? "TRUE" : "FALSE");
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson msp->ms_condense_wanted = B_FALSE;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
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.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
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);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
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.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_walk(freetree, range_tree_remove, condense_tree);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_walk(msp->ms_defertree[t],
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_remove, condense_tree);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 1; t < TXG_CONCURRENT_STATES; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_walk(msp->ms_alloctree[(txg + t) & TXG_MASK],
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_remove, condense_tree);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
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.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_condensing = B_TRUE;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson mutex_exit(&msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_truncate(sm, tx);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson mutex_enter(&msp->ms_lock);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
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
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * compute.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(sm, condense_tree, SM_ALLOC, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_vacate(condense_tree, NULL, NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_destroy(condense_tree);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(sm, msp->ms_tree, SM_FREE, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_condensing = B_FALSE;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson}
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Write a metaslab to disk in the context of the specified transaction group.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_sync(metaslab_t *msp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_group_t *mg = msp->ms_group;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson vdev_t *vd = mg->mg_vd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens spa_t *spa = vd->vdev_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick objset_t *mos = spa_meta_objset(spa);
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 range_tree_t **freed_tree =
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson &msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK];
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_t *tx;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t object = space_map_object(msp->ms_sm);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson ASSERT(!vd->vdev_ishole);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson * This metaslab has just been added so there's no work to do now.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (*freetree == NULL) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(alloctree, ==, NULL);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson return;
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson }
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(alloctree, !=, NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(*freetree, !=, NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3P(*freed_tree, !=, NULL);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson /*
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.
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (range_tree_space(alloctree) == 0 &&
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson range_tree_space(*freetree) == 0 &&
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson !msp->ms_condense_wanted)
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
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
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * any time.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_sm == NULL) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t new_object;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson new_object = space_map_alloc(mos, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY3U(new_object, !=, 0);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(space_map_open(&msp->ms_sm, mos, new_object,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_start, msp->ms_size, vd->vdev_ashift,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson &msp->ms_lock));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_sm != NULL);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley mutex_enter(&msp->ms_lock);
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens /*
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 */
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens metaslab_group_histogram_verify(mg);
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens metaslab_class_histogram_verify(mg->mg_class);
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens metaslab_group_histogram_remove(mg, msp);
b1be2892dd07cf9a97d47ad06334cdc879196aafMatthew Ahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_loaded && spa_sync_pass(spa) == 1 &&
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson metaslab_should_condense(msp)) {
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson metaslab_condense(msp, txg, tx);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson } else {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(msp->ms_sm, alloctree, SM_ALLOC, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_write(msp->ms_sm, *freetree, SM_FREE, tx);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson }
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_loaded) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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 */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_histogram_clear(msp->ms_sm);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_histogram_add(msp->ms_sm, msp->ms_tree, tx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson /*
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).
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson space_map_histogram_add(msp->ms_sm, *freed_tree, tx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 * allocated.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson space_map_histogram_add(msp->ms_sm,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_defertree[t], tx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson space_map_histogram_add(msp->ms_sm, *freetree, tx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_histogram_add(mg, msp);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_group_histogram_verify(mg);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson metaslab_class_histogram_verify(mg->mg_class);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson /*
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.
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson */
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson if (spa_sync_pass(spa) == 1) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_swap(freetree, freed_tree);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson } else {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_vacate(*freetree, range_tree_add, *freed_tree);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson range_tree_vacate(alloctree, NULL, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
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]));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (object != space_map_object(msp->ms_sm)) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson object = space_map_object(msp->ms_sm);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_id, sizeof (uint64_t), &object, tx);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick dmu_tx_commit(tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Called after a transaction group has completely synced to mark
fa9e4066f08beec538e775443c5be79dd423fcabahrens * all of the metaslab's free space as usable.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_sync_done(metaslab_t *msp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_group_t *mg = msp->ms_group;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_t *vd = mg->mg_vd;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson spa_t *spa = vd->vdev_spa;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t **freed_tree;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_t **defer_tree;
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley int64_t alloc_delta, defer_delta;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson boolean_t defer_allowed = B_TRUE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson ASSERT(!vd->vdev_ishole);
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * If this metaslab is just becoming available, initialize its
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * alloctrees, freetrees, and defertree and add its capacity to
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * the vdev.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK] == NULL) {
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley for (int t = 0; t < TXG_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_alloctree[t] == NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_freetree[t] == NULL);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_alloctree[t] = range_tree_create(NULL, msp,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson &msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_freetree[t] = range_tree_create(NULL, msp,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson &msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson for (int t = 0; t < TXG_DEFER_SIZE; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT(msp->ms_defertree[t] == NULL);
16a4a8074274d2d7cc408589cf6359f4a378c861George Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_defertree[t] = range_tree_create(NULL, msp,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson &msp->ms_lock);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson vdev_space_update(vd, 0, 0, msp->ms_size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson freed_tree = &msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson defer_tree = &msp->ms_defertree[txg % TXG_DEFER_SIZE];
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t free_space = metaslab_class_get_space(spa_normal_class(spa)) -
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_class_get_alloc(spa_normal_class(spa));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (free_space <= spa_get_slop_space(spa)) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson defer_allowed = B_FALSE;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson defer_delta = 0;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson alloc_delta = space_map_alloc_delta(msp->ms_sm);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (defer_allowed) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson defer_delta = range_tree_space(*freed_tree) -
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_space(*defer_tree);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson } else {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson defer_delta -= range_tree_space(*defer_tree);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick vdev_space_update(vd, alloc_delta + defer_delta, defer_delta, 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT0(range_tree_space(msp->ms_alloctree[txg & TXG_MASK]));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT0(range_tree_space(msp->ms_freetree[txg & TXG_MASK]));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
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.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_load_wait(msp);
9eb57f7f3fbb970d4b9b89dcd5ecf543fe2414d5George Wilson
9eb57f7f3fbb970d4b9b89dcd5ecf543fe2414d5George Wilson /*
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.
9eb57f7f3fbb970d4b9b89dcd5ecf543fe2414d5George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_vacate(*defer_tree,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_loaded ? range_tree_add : NULL, msp->ms_tree);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (defer_allowed) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_swap(freed_tree, defer_tree);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson } else {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_vacate(*freed_tree,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_loaded ? range_tree_add : NULL, msp->ms_tree);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson space_map_update(msp->ms_sm);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley msp->ms_deferspace += defer_delta;
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley ASSERT3S(msp->ms_deferspace, >=, 0);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson ASSERT3S(msp->ms_deferspace, <=, msp->ms_size);
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley if (msp->ms_deferspace != 0) {
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley /*
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley * Keep syncing this metaslab until all deferred frees
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley * are back in circulation.
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley */
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley vdev_dirty(vd, VDD_METASLAB, msp, txg + 1);
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley }
468c413a79615e77179e8d98f22a7e513a8135bdTim Haley
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Calculate the new weights before unloading any metaslabs.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * This will give us the most accurate weighting.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_group_sort(mg, msp, metaslab_weight(msp));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp->ms_loaded &&
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_selected_txg + metaslab_unload_delay < txg) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson for (int t = 1; t < TXG_CONCURRENT_STATES; t++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(range_tree_space(
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_alloctree[(txg + t) & TXG_MASK]));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (!metaslab_debug_unload)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_unload(msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonvoid
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilsonmetaslab_sync_reassess(metaslab_group_t *mg)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson{
22e30981d82a0b6dc89253596ededafae8655e00George Wilson metaslab_group_alloc_update(mg);
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_fragmentation = metaslab_group_fragmentation(mg);
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson /*
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson * Preload the next potential metaslabs
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson metaslab_group_preload(mg);
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson}
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmstatic uint64_t
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmmetaslab_distance(metaslab_t *msp, dva_t *dva)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm{
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t start = msp->ms_id;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (1ULL << 63);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (offset < start)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return ((start - offset) << ms_shift);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (offset > start)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return ((offset - start) << ms_shift);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (0);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm}
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Metaslab allocation tracing facility
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * ==========================================================================
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonkstat_t *metaslab_trace_ksp;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonkstat_named_t metaslab_trace_over_limit;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonvoid
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_alloc_trace_init(void)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(metaslab_alloc_trace_cache == NULL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_alloc_trace_cache = kmem_cache_create(
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson "metaslab_alloc_trace_cache", sizeof (metaslab_alloc_trace_t),
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson 0, NULL, NULL, NULL, NULL, NULL, 0);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_ksp = kstat_create("zfs", 0, "metaslab_trace_stats",
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson "misc", KSTAT_TYPE_NAMED, 1, KSTAT_FLAG_VIRTUAL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (metaslab_trace_ksp != NULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_ksp->ks_data = &metaslab_trace_over_limit;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kstat_named_init(&metaslab_trace_over_limit,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson "metaslab_trace_over_limit", KSTAT_DATA_UINT64);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kstat_install(metaslab_trace_ksp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonvoid
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_alloc_trace_fini(void)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (metaslab_trace_ksp != NULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kstat_delete(metaslab_trace_ksp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_ksp = NULL;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_destroy(metaslab_alloc_trace_cache);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_alloc_trace_cache = NULL;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * Add an allocation trace element to the allocation tracing list.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic void
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{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!metaslab_trace_enabled)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 * performed.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (zal->zal_size == metaslab_trace_max_entries) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_alloc_trace_t *mat_next;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson#ifdef DEBUG
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson panic("too many entries in allocation list");
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson#endif
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson atomic_inc_64(&metaslab_trace_over_limit.value.ui64);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zal->zal_size--;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat_next = list_next(&zal->zal_list, list_head(&zal->zal_list));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson list_remove(&zal->zal_list, mat_next);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_free(metaslab_alloc_trace_cache, mat_next);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_alloc_trace_t *mat =
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_alloc(metaslab_alloc_trace_cache, KM_SLEEP);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson list_link_init(&mat->mat_list_node);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat->mat_mg = mg;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat->mat_msp = msp;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat->mat_size = psize;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat->mat_dva_id = dva_id;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat->mat_offset = offset;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat->mat_weight = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp != NULL)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mat->mat_weight = msp->ms_weight;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson list_insert_tail(&zal->zal_list, mat);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zal->zal_size++;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT3U(zal->zal_size, <=, metaslab_trace_max_entries);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonvoid
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_trace_init(zio_alloc_list_t *zal)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson list_create(&zal->zal_list, sizeof (metaslab_alloc_trace_t),
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson offsetof(metaslab_alloc_trace_t, mat_list_node));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zal->zal_size = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonvoid
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_trace_fini(zio_alloc_list_t *zal)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_alloc_trace_t *mat;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson while ((mat = list_remove_head(&zal->zal_list)) != NULL)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_cache_free(metaslab_alloc_trace_cache, mat);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson list_destroy(&zal->zal_list);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zal->zal_size = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson/*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * ==========================================================================
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Metaslab block operations
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * ==========================================================================
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonstatic void
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_alloc_increment(spa_t *spa, uint64_t vdev, void *tag, int flags)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!(flags & METASLAB_ASYNC_ALLOC) ||
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags & METASLAB_DONT_THROTTLE)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!mg->mg_class->mc_alloc_throttle_enabled)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson (void) refcount_add(&mg->mg_alloc_queue_depth, tag);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonvoid
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_alloc_decrement(spa_t *spa, uint64_t vdev, void *tag, int flags)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!(flags & METASLAB_ASYNC_ALLOC) ||
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags & METASLAB_DONT_THROTTLE)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!mg->mg_class->mc_alloc_throttle_enabled)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson (void) refcount_remove(&mg->mg_alloc_queue_depth, tag);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonvoid
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_group_alloc_verify(spa_t *spa, const blkptr_t *bp, void *tag)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#ifdef ZFS_DEBUG
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson const dva_t *dva = bp->blk_dva;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson int ndvas = BP_GET_NDVAS(bp);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (int d = 0; d < ndvas; d++) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson uint64_t vdev = DVA_GET_VDEV(&dva[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));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#endif
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmstatic uint64_t
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonmetaslab_block_alloc(metaslab_t *msp, uint64_t size, uint64_t txg)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t start;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_t *rt = msp->ms_tree;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_class_t *mc = msp->ms_group->mg_class;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson VERIFY(!msp->ms_condensing);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson start = mc->mc_ops->msop_alloc(msp, size);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (start != -1ULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_group_t *mg = msp->ms_group;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson vdev_t *vd = mg->mg_vd;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
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 range_tree_remove(rt, start, size);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
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
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson range_tree_add(msp->ms_alloctree[txg & TXG_MASK], start, size);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /* Track the last successful allocation */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_alloc_txg = txg;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_verify_space(msp, txg);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (start);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic uint64_t
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)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_t *msp = NULL;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t offset = -1ULL;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t activation_weight;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t target_distance;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm int i;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm activation_weight = METASLAB_WEIGHT_PRIMARY;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson for (i = 0; i < d; i++) {
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm activation_weight = METASLAB_WEIGHT_SECONDARY;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson break;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson }
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_t *search = kmem_alloc(sizeof (*search), KM_SLEEP);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson search->ms_weight = UINT64_MAX;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson search->ms_start = 0;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick for (;;) {
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson boolean_t was_active;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson avl_tree_t *t = &mg->mg_metaslab_tree;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson avl_index_t idx;
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_enter(&mg->mg_lock);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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).
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp = avl_find(t, search, &idx);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp == NULL)
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp = avl_nearest(t, idx, AVL_AFTER);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (; msp != NULL; msp = AVL_NEXT(t, msp)) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!metaslab_should_allocate(msp, asize)) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, msp, asize, d,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson TRACE_TOO_SMALL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson continue;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson /*
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson * If the selected metaslab is condensing, skip it.
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_condensing)
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson continue;
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson
d6e555bdd793b8bc8fe57d5f12c3d69c813d0661George Wilson was_active = msp->ms_weight & METASLAB_ACTIVE_MASK;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (activation_weight == METASLAB_WEIGHT_PRIMARY)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm break;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm target_distance = min_distance +
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson (space_map_allocated(msp->ms_sm) != 0 ? 0 :
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson min_distance >> 1);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson for (i = 0; i < d; i++) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (metaslab_distance(msp, &dva[i]) <
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm target_distance)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm break;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (i == d)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm break;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_exit(&mg->mg_lock);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (msp == NULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_free(search, sizeof (*search));
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (-1ULL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson search->ms_weight = msp->ms_weight;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson search->ms_start = msp->ms_start + 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
22e30981d82a0b6dc89253596ededafae8655e00George Wilson mutex_enter(&msp->ms_lock);
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
aeb1c1b609b02f03e8e7448beb88384ebc713525gw /*
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.
aeb1c1b609b02f03e8e7448beb88384ebc713525gw */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (was_active && !(msp->ms_weight & METASLAB_ACTIVE_MASK)) {
aeb1c1b609b02f03e8e7448beb88384ebc713525gw mutex_exit(&msp->ms_lock);
aeb1c1b609b02f03e8e7448beb88384ebc713525gw continue;
aeb1c1b609b02f03e8e7448beb88384ebc713525gw }
aeb1c1b609b02f03e8e7448beb88384ebc713525gw
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if ((msp->ms_weight & METASLAB_WEIGHT_SECONDARY) &&
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm activation_weight == METASLAB_WEIGHT_PRIMARY) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm metaslab_passivate(msp,
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens msp->ms_weight & ~METASLAB_ACTIVE_MASK);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm mutex_exit(&msp->ms_lock);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm continue;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson if (metaslab_activate(msp, activation_weight) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens continue;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_selected_txg = txg;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 * proceed.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!metaslab_should_allocate(msp, asize)) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /* Passivate this metaslab and select a new one. */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, msp, asize, d,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson TRACE_TOO_SMALL);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson goto next;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson /*
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson * If this metaslab is currently condensing then pick again as
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson * we can't manipulate this metaslab until it's committed
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson * to disk.
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson */
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_condensing) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, msp, asize, d,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson TRACE_CONDENSING);
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson mutex_exit(&msp->ms_lock);
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson continue;
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson }
03f8c366886542ed249a15d755ae78ea4e775d9dGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson offset = metaslab_block_alloc(msp, asize, txg);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, msp, asize, d, offset);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (offset != -1ULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /* Proactively passivate the metaslab, if needed */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_segment_may_passivate(msp);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick break;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonnext:
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(msp->ms_loaded);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 * selector.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson *
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 *
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (WEIGHT_IS_SPACEBASED(msp->ms_weight)) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t weight = metaslab_block_maxsize(msp);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson WEIGHT_SET_SPACEBASED(weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_passivate(msp, weight);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson } else {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_passivate(msp,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_weight_from_range_tree(msp));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 * metaslab.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(!metaslab_should_allocate(msp, asize));
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mutex_exit(&msp->ms_lock);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson kmem_free(search, sizeof (*search));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson return (offset);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonstatic uint64_t
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{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t offset;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT(mg->mg_initialized);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson offset = metaslab_group_alloc_normal(mg, zal, asize, txg,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson min_distance, dva, d);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mutex_enter(&mg->mg_lock);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (offset == -1ULL) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mg->mg_failed_allocations++;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, NULL, asize, d,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson TRACE_GROUP_FAILURE);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (asize == SPA_GANGBLOCKSIZE) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mg->mg_no_free_space = B_TRUE;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mg->mg_allocations++;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson mutex_exit(&mg->mg_lock);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (offset);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson/*
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.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilsonint ditto_same_vdev_distance_shift = 3;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Allocate a block for the specified i/o.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmstatic int
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,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zio_alloc_list_t *zal)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mg, *rotor;
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson boolean_t try_hard = B_FALSE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(!DVA_IS_VALID(&dva[d]));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
e05725b117836db173257fae43fb0746eb857fb5bonwick /*
e05725b117836db173257fae43fb0746eb857fb5bonwick * For testing, make some blocks above a certain size be gang blocks.
e05725b117836db173257fae43fb0746eb857fb5bonwick */
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);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
e05725b117836db173257fae43fb0746eb857fb5bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
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.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm *
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 *
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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (hintdva) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson /*
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.
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson */
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick if (vd != NULL) {
67bd71c6cc629bab3aa0d595c624a667f1574254perrin mg = vd->vdev_mg;
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson if (flags & METASLAB_HINTBP_AVOID &&
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson mg->mg_next != NULL)
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson mg = mg->mg_next;
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson } else {
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson mg = mc->mc_rotor;
88ecc943b4eb72f7c4fbbd8435997b85ef171fc3George Wilson }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm } else if (d != 0) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm mg = vd->vdev_mg->mg_next;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm } else {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm mg = mc->mc_rotor;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin /*
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.
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin */
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick if (mg->mg_class != mc || mg->mg_activation_count <= 0)
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin mg = mc->mc_rotor;
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin rotor = mg;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmtop:
fa9e4066f08beec538e775443c5be79dd423fcabahrens do {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson boolean_t allocatable;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
a15215608b8bd90f714f6db21ee623b584607cb6Jeff Bonwick ASSERT(mg->mg_activation_count == 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens vd = mg->mg_vd;
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick
0a4e9518a44f226be6d39383330b5b1792d2f184gw /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Don't allocate from faulted devices.
0a4e9518a44f226be6d39383330b5b1792d2f184gw */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (try_hard) {
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick allocatable = vdev_allocatable(vd);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick spa_config_exit(spa, SCL_ZIO, FTAG);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick } else {
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick allocatable = vdev_allocatable(vd);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick }
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
22e30981d82a0b6dc89253596ededafae8655e00George Wilson /*
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.
22e30981d82a0b6dc89253596ededafae8655e00George Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (allocatable && !GANG_ALLOCATION(flags) && !try_hard) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson allocatable = metaslab_group_allocatable(mg, rotor,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson psize);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
22e30981d82a0b6dc89253596ededafae8655e00George Wilson
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!allocatable) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, NULL, psize, d,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson TRACE_NOT_ALLOCATABLE);
0a4e9518a44f226be6d39383330b5b1792d2f184gw goto next;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(mg->mg_initialized);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0a4e9518a44f226be6d39383330b5b1792d2f184gw /*
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.
0a4e9518a44f226be6d39383330b5b1792d2f184gw */
0a4e9518a44f226be6d39383330b5b1792d2f184gw if ((vd->vdev_stat.vs_write_errors > 0 ||
0a4e9518a44f226be6d39383330b5b1792d2f184gw vd->vdev_state < VDEV_STATE_HEALTHY) &&
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson d == 0 && !try_hard && vd->vdev_children == 0) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, mg, NULL, psize, d,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson TRACE_VDEV_ERROR);
0a4e9518a44f226be6d39383330b5b1792d2f184gw goto next;
0a4e9518a44f226be6d39383330b5b1792d2f184gw }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin ASSERT(mg->mg_class == mc);
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
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 */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t distance = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!try_hard) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson distance = vd->vdev_asize >>
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ditto_same_vdev_distance_shift;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (distance <= (1ULL << vd->vdev_ms_shift))
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson distance = 0;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t asize = vdev_psize_to_asize(vd, psize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson uint64_t offset = metaslab_group_alloc(mg, zal, asize, txg,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson distance, dva, d);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (offset != -1ULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson if (mc->mc_aliquot == 0 && metaslab_bias_enabled) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_stat_t *vs = &vd->vdev_stat;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int64_t vu, cu;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson vu = (vs->vs_alloc * 100) / (vs->vs_space + 1);
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson cu = (mc->mc_alloc * 100) / (mc->mc_space + 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
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 *
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * mg_bias = (20 - 80) * 512K / 100 = -307K
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson *
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * This reduces allocations by 307K for this
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson * iteration.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mg->mg_bias = ((cu - vu) *
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson (int64_t)mg->mg_aliquot) / 100;
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson } else if (!metaslab_bias_enabled) {
2e4c998613148111f2fc5371085331ffb39122ffGeorge Wilson mg->mg_bias = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (atomic_add_64_nv(&mc->mc_aliquot, asize) >=
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_aliquot + mg->mg_bias) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = mg->mg_next;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mc->mc_aliquot = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_VDEV(&dva[d], vd->vdev_id);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_OFFSET(&dva[d], offset);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick DVA_SET_GANG(&dva[d], !!(flags & METASLAB_GANG_HEADER));
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_ASIZE(&dva[d], asize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
0a4e9518a44f226be6d39383330b5b1792d2f184gwnext:
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = mg->mg_next;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mc->mc_aliquot = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens } while ((mg = mg->mg_next) != rotor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson /*
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson * If we haven't tried hard, do so now.
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson */
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson if (!try_hard) {
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson try_hard = B_TRUE;
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick goto top;
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick }
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm bzero(&dva[d], sizeof (dva_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_add(zal, rotor, NULL, psize, d, TRACE_ENOSPC);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Free the block represented by DVA in the context of the specified
fa9e4066f08beec538e775443c5be79dd423fcabahrens * transaction group.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickstatic void
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t vdev = DVA_GET_VDEV(dva);
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t offset = DVA_GET_OFFSET(dva);
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t size = DVA_GET_ASIZE(dva);
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_t *msp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(DVA_IS_VALID(dva));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (txg > spa_freeze_txg(spa))
fa9e4066f08beec538e775443c5be79dd423fcabahrens return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick (u_longlong_t)vdev, (u_longlong_t)offset);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (DVA_GET_GANG(dva))
fa9e4066f08beec538e775443c5be79dd423fcabahrens size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (now) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_remove(msp->ms_alloctree[txg & TXG_MASK],
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick offset, size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY(!msp->ms_condensing);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY3U(offset, >=, msp->ms_start);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY3U(offset + size, <=, msp->ms_start + msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY3U(range_tree_space(msp->ms_tree) + size, <=,
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson msp->ms_size);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(msp->ms_tree, offset, size);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson msp->ms_max_size = metaslab_block_maxsize(msp);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick } else {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (range_tree_space(msp->ms_freetree[txg & TXG_MASK]) == 0)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_dirty(vd, VDD_METASLAB, msp, txg);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(msp->ms_freetree[txg & TXG_MASK],
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson offset, size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick/*
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.
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick */
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickstatic int
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick uint64_t vdev = DVA_GET_VDEV(dva);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick uint64_t offset = DVA_GET_OFFSET(dva);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick uint64_t size = DVA_GET_ASIZE(dva);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick vdev_t *vd;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick metaslab_t *msp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int error = 0;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(DVA_IS_VALID(dva));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENXIO));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if (DVA_GET_GANG(dva))
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick mutex_enter(&msp->ms_lock);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if ((txg != 0 && spa_writeable(spa)) || !msp->ms_loaded)
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (error == 0 && !range_tree_contains(msp->ms_tree, offset, size))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(ENOENT);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (error || txg == 0) { /* txg == 0 indicates dry run */
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick mutex_exit(&msp->ms_lock);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (error);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson VERIFY(!msp->ms_condensing);
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);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
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)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vdev_dirty(vd, VDD_METASLAB, msp, txg);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_add(msp->ms_alloctree[txg & TXG_MASK], offset, size);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick mutex_exit(&msp->ms_lock);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson/*
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 Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonboolean_t
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_class_throttle_reserve(metaslab_class_t *mc, int slots, zio_t *zio,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson int flags)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson uint64_t available_slots = 0;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson boolean_t slot_reserved = B_FALSE;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(mc->mc_alloc_throttle_enabled);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_enter(&mc->mc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson uint64_t reserved_slots = refcount_count(&mc->mc_alloc_slots);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (reserved_slots < mc->mc_alloc_max_slots)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson available_slots = mc->mc_alloc_max_slots - reserved_slots;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (slots <= available_slots || GANG_ALLOCATION(flags)) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We reserve the slots individually so that we can unreserve
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * them individually when an I/O completes.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (int d = 0; d < slots; d++) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson reserved_slots = refcount_add(&mc->mc_alloc_slots, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_flags |= ZIO_FLAG_IO_ALLOCATING;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson slot_reserved = B_TRUE;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_exit(&mc->mc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (slot_reserved);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonvoid
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonmetaslab_class_throttle_unreserve(metaslab_class_t *mc, int slots, zio_t *zio)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(mc->mc_alloc_throttle_enabled);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_enter(&mc->mc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (int d = 0; d < slots; d++) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson (void) refcount_remove(&mc->mc_alloc_slots, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_exit(&mc->mc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickint
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,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zio_alloc_list_t *zal, zio_t *zio)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick dva_t *dva = bp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick dva_t *hintdva = hintbp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int error = 0;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(bp->blk_birth == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_PHYSICAL_BIRTH(bp) == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (mc->mc_rotor == NULL) { /* no vdevs in this class */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(BP_GET_NDVAS(bp) == 0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson ASSERT3P(zal, !=, NULL);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int d = 0; d < ndvas; d++) {
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson txg, flags, zal);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (error != 0) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick for (d--; d >= 0; d--) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick metaslab_free_dva(spa, &dva[d], txg, B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_alloc_decrement(spa,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson DVA_GET_VDEV(&dva[d]), zio, flags);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick bzero(&dva[d], sizeof (dva_t));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (error);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson } else {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Update the metaslab group's queue depth
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * based on the newly allocated dva.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_alloc_increment(spa,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson DVA_GET_VDEV(&dva[d]), zio, flags);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(error == 0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(BP_GET_NDVAS(bp) == ndvas);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_BIRTH(bp, txg, txg);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickvoid
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick const dva_t *dva = bp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int ndvas = BP_GET_NDVAS(bp);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(!BP_IS_HOLE(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!now || bp->blk_birth >= spa_syncing_txg(spa));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_enter(spa, SCL_FREE, FTAG, RW_READER);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int d = 0; d < ndvas; d++)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick metaslab_free_dva(spa, &dva[d], txg, now);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_exit(spa, SCL_FREE, FTAG);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickint
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick const dva_t *dva = bp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int ndvas = BP_GET_NDVAS(bp);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick int error = 0;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(!BP_IS_HOLE(bp));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (txg != 0) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
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 */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if ((error = metaslab_claim(spa, bp, 0)) != 0)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (error);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int d = 0; d < ndvas; d++)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick break;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_exit(spa, SCL_ALLOC, FTAG);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(error == 0 || txg == 0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (error);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensmetaslab_check_free(spa_t *spa, const blkptr_t *bp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if ((zfs_flags & ZFS_DEBUG_ZIO_FREE) == 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson uint64_t vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson vdev_t *vd = vdev_lookup_top(spa, vdev);
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];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson if (msp->ms_loaded)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_verify(msp->ms_tree, offset, size);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
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++)
0713e232b7712cd27d99e1e935ebb8d5de61c57dGeorge Wilson range_tree_verify(msp->ms_defertree[j], offset, size);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_config_exit(spa, SCL_VDEV, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}