4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 * Copyright (c) 2017, Intel Corporation.
28 #include <sys/zfs_context.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/space_map.h>
32 #include <sys/metaslab_impl.h>
33 #include <sys/vdev_impl.h>
35 #include <sys/spa_impl.h>
36 #include <sys/zfeature.h>
37 #include <sys/vdev_indirect_mapping.h>
40 #define WITH_DF_BLOCK_ALLOCATOR
42 #define GANG_ALLOCATION(flags) \
43 ((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER))
46 * Metaslab granularity, in bytes. This is roughly similar to what would be
47 * referred to as the "stripe size" in traditional RAID arrays. In normal
48 * operation, we will try to write this amount of data to a top-level vdev
49 * before moving on to the next one.
51 unsigned long metaslab_aliquot
= 512 << 10;
54 * For testing, make some blocks above a certain size be gang blocks.
56 unsigned long metaslab_force_ganging
= SPA_MAXBLOCKSIZE
+ 1;
59 * Since we can touch multiple metaslabs (and their respective space maps)
60 * with each transaction group, we benefit from having a smaller space map
61 * block size since it allows us to issue more I/O operations scattered
64 int zfs_metaslab_sm_blksz
= (1 << 12);
67 * The in-core space map representation is more compact than its on-disk form.
68 * The zfs_condense_pct determines how much more compact the in-core
69 * space map representation must be before we compact it on-disk.
70 * Values should be greater than or equal to 100.
72 int zfs_condense_pct
= 200;
75 * Condensing a metaslab is not guaranteed to actually reduce the amount of
76 * space used on disk. In particular, a space map uses data in increments of
77 * MAX(1 << ashift, space_map_blksz), so a metaslab might use the
78 * same number of blocks after condensing. Since the goal of condensing is to
79 * reduce the number of IOPs required to read the space map, we only want to
80 * condense when we can be sure we will reduce the number of blocks used by the
81 * space map. Unfortunately, we cannot precisely compute whether or not this is
82 * the case in metaslab_should_condense since we are holding ms_lock. Instead,
83 * we apply the following heuristic: do not condense a spacemap unless the
84 * uncondensed size consumes greater than zfs_metaslab_condense_block_threshold
87 int zfs_metaslab_condense_block_threshold
= 4;
90 * The zfs_mg_noalloc_threshold defines which metaslab groups should
91 * be eligible for allocation. The value is defined as a percentage of
92 * free space. Metaslab groups that have more free space than
93 * zfs_mg_noalloc_threshold are always eligible for allocations. Once
94 * a metaslab group's free space is less than or equal to the
95 * zfs_mg_noalloc_threshold the allocator will avoid allocating to that
96 * group unless all groups in the pool have reached zfs_mg_noalloc_threshold.
97 * Once all groups in the pool reach zfs_mg_noalloc_threshold then all
98 * groups are allowed to accept allocations. Gang blocks are always
99 * eligible to allocate on any metaslab group. The default value of 0 means
100 * no metaslab group will be excluded based on this criterion.
102 int zfs_mg_noalloc_threshold
= 0;
105 * Metaslab groups are considered eligible for allocations if their
106 * fragmenation metric (measured as a percentage) is less than or equal to
107 * zfs_mg_fragmentation_threshold. If a metaslab group exceeds this threshold
108 * then it will be skipped unless all metaslab groups within the metaslab
109 * class have also crossed this threshold.
111 int zfs_mg_fragmentation_threshold
= 85;
114 * Allow metaslabs to keep their active state as long as their fragmentation
115 * percentage is less than or equal to zfs_metaslab_fragmentation_threshold. An
116 * active metaslab that exceeds this threshold will no longer keep its active
117 * status allowing better metaslabs to be selected.
119 int zfs_metaslab_fragmentation_threshold
= 70;
122 * When set will load all metaslabs when pool is first opened.
124 int metaslab_debug_load
= 0;
127 * When set will prevent metaslabs from being unloaded.
129 int metaslab_debug_unload
= 0;
132 * Minimum size which forces the dynamic allocator to change
133 * it's allocation strategy. Once the space map cannot satisfy
134 * an allocation of this size then it switches to using more
135 * aggressive strategy (i.e search by size rather than offset).
137 uint64_t metaslab_df_alloc_threshold
= SPA_OLD_MAXBLOCKSIZE
;
140 * The minimum free space, in percent, which must be available
141 * in a space map to continue allocations in a first-fit fashion.
142 * Once the space map's free space drops below this level we dynamically
143 * switch to using best-fit allocations.
145 int metaslab_df_free_pct
= 4;
148 * Percentage of all cpus that can be used by the metaslab taskq.
150 int metaslab_load_pct
= 50;
153 * Determines how many txgs a metaslab may remain loaded without having any
154 * allocations from it. As long as a metaslab continues to be used we will
157 int metaslab_unload_delay
= TXG_SIZE
* 2;
160 * Max number of metaslabs per group to preload.
162 int metaslab_preload_limit
= SPA_DVAS_PER_BP
;
165 * Enable/disable preloading of metaslab.
167 int metaslab_preload_enabled
= B_TRUE
;
170 * Enable/disable fragmentation weighting on metaslabs.
172 int metaslab_fragmentation_factor_enabled
= B_TRUE
;
175 * Enable/disable lba weighting (i.e. outer tracks are given preference).
177 int metaslab_lba_weighting_enabled
= B_TRUE
;
180 * Enable/disable metaslab group biasing.
182 int metaslab_bias_enabled
= B_TRUE
;
186 * Enable/disable remapping of indirect DVAs to their concrete vdevs.
188 boolean_t zfs_remap_blkptr_enable
= B_TRUE
;
191 * Enable/disable segment-based metaslab selection.
193 int zfs_metaslab_segment_weight_enabled
= B_TRUE
;
196 * When using segment-based metaslab selection, we will continue
197 * allocating from the active metaslab until we have exhausted
198 * zfs_metaslab_switch_threshold of its buckets.
200 int zfs_metaslab_switch_threshold
= 2;
203 * Internal switch to enable/disable the metaslab allocation tracing
206 #ifdef _METASLAB_TRACING
207 boolean_t metaslab_trace_enabled
= B_TRUE
;
211 * Maximum entries that the metaslab allocation tracing facility will keep
212 * in a given list when running in non-debug mode. We limit the number
213 * of entries in non-debug mode to prevent us from using up too much memory.
214 * The limit should be sufficiently large that we don't expect any allocation
215 * to every exceed this value. In debug mode, the system will panic if this
216 * limit is ever reached allowing for further investigation.
218 #ifdef _METASLAB_TRACING
219 uint64_t metaslab_trace_max_entries
= 5000;
222 static uint64_t metaslab_weight(metaslab_t
*);
223 static void metaslab_set_fragmentation(metaslab_t
*);
224 static void metaslab_free_impl(vdev_t
*, uint64_t, uint64_t, boolean_t
);
225 static void metaslab_check_free_impl(vdev_t
*, uint64_t, uint64_t);
227 static void metaslab_passivate(metaslab_t
*msp
, uint64_t weight
);
228 static uint64_t metaslab_weight_from_range_tree(metaslab_t
*msp
);
229 #ifdef _METASLAB_TRACING
230 kmem_cache_t
*metaslab_alloc_trace_cache
;
234 * ==========================================================================
236 * ==========================================================================
239 metaslab_class_create(spa_t
*spa
, metaslab_ops_t
*ops
)
241 metaslab_class_t
*mc
;
243 mc
= kmem_zalloc(sizeof (metaslab_class_t
), KM_SLEEP
);
248 mutex_init(&mc
->mc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
249 mc
->mc_alloc_slots
= kmem_zalloc(spa
->spa_alloc_count
*
250 sizeof (zfs_refcount_t
), KM_SLEEP
);
251 mc
->mc_alloc_max_slots
= kmem_zalloc(spa
->spa_alloc_count
*
252 sizeof (uint64_t), KM_SLEEP
);
253 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++)
254 zfs_refcount_create_tracked(&mc
->mc_alloc_slots
[i
]);
260 metaslab_class_destroy(metaslab_class_t
*mc
)
262 ASSERT(mc
->mc_rotor
== NULL
);
263 ASSERT(mc
->mc_alloc
== 0);
264 ASSERT(mc
->mc_deferred
== 0);
265 ASSERT(mc
->mc_space
== 0);
266 ASSERT(mc
->mc_dspace
== 0);
268 for (int i
= 0; i
< mc
->mc_spa
->spa_alloc_count
; i
++)
269 zfs_refcount_destroy(&mc
->mc_alloc_slots
[i
]);
270 kmem_free(mc
->mc_alloc_slots
, mc
->mc_spa
->spa_alloc_count
*
271 sizeof (zfs_refcount_t
));
272 kmem_free(mc
->mc_alloc_max_slots
, mc
->mc_spa
->spa_alloc_count
*
274 mutex_destroy(&mc
->mc_lock
);
275 kmem_free(mc
, sizeof (metaslab_class_t
));
279 metaslab_class_validate(metaslab_class_t
*mc
)
281 metaslab_group_t
*mg
;
285 * Must hold one of the spa_config locks.
287 ASSERT(spa_config_held(mc
->mc_spa
, SCL_ALL
, RW_READER
) ||
288 spa_config_held(mc
->mc_spa
, SCL_ALL
, RW_WRITER
));
290 if ((mg
= mc
->mc_rotor
) == NULL
)
295 ASSERT(vd
->vdev_mg
!= NULL
);
296 ASSERT3P(vd
->vdev_top
, ==, vd
);
297 ASSERT3P(mg
->mg_class
, ==, mc
);
298 ASSERT3P(vd
->vdev_ops
, !=, &vdev_hole_ops
);
299 } while ((mg
= mg
->mg_next
) != mc
->mc_rotor
);
305 metaslab_class_space_update(metaslab_class_t
*mc
, int64_t alloc_delta
,
306 int64_t defer_delta
, int64_t space_delta
, int64_t dspace_delta
)
308 atomic_add_64(&mc
->mc_alloc
, alloc_delta
);
309 atomic_add_64(&mc
->mc_deferred
, defer_delta
);
310 atomic_add_64(&mc
->mc_space
, space_delta
);
311 atomic_add_64(&mc
->mc_dspace
, dspace_delta
);
315 metaslab_class_get_alloc(metaslab_class_t
*mc
)
317 return (mc
->mc_alloc
);
321 metaslab_class_get_deferred(metaslab_class_t
*mc
)
323 return (mc
->mc_deferred
);
327 metaslab_class_get_space(metaslab_class_t
*mc
)
329 return (mc
->mc_space
);
333 metaslab_class_get_dspace(metaslab_class_t
*mc
)
335 return (spa_deflate(mc
->mc_spa
) ? mc
->mc_dspace
: mc
->mc_space
);
339 metaslab_class_histogram_verify(metaslab_class_t
*mc
)
341 spa_t
*spa
= mc
->mc_spa
;
342 vdev_t
*rvd
= spa
->spa_root_vdev
;
346 if ((zfs_flags
& ZFS_DEBUG_HISTOGRAM_VERIFY
) == 0)
349 mc_hist
= kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE
,
352 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
353 vdev_t
*tvd
= rvd
->vdev_child
[c
];
354 metaslab_group_t
*mg
= tvd
->vdev_mg
;
357 * Skip any holes, uninitialized top-levels, or
358 * vdevs that are not in this metalab class.
360 if (!vdev_is_concrete(tvd
) || tvd
->vdev_ms_shift
== 0 ||
361 mg
->mg_class
!= mc
) {
365 for (i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
366 mc_hist
[i
] += mg
->mg_histogram
[i
];
369 for (i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
370 VERIFY3U(mc_hist
[i
], ==, mc
->mc_histogram
[i
]);
372 kmem_free(mc_hist
, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE
);
376 * Calculate the metaslab class's fragmentation metric. The metric
377 * is weighted based on the space contribution of each metaslab group.
378 * The return value will be a number between 0 and 100 (inclusive), or
379 * ZFS_FRAG_INVALID if the metric has not been set. See comment above the
380 * zfs_frag_table for more information about the metric.
383 metaslab_class_fragmentation(metaslab_class_t
*mc
)
385 vdev_t
*rvd
= mc
->mc_spa
->spa_root_vdev
;
386 uint64_t fragmentation
= 0;
388 spa_config_enter(mc
->mc_spa
, SCL_VDEV
, FTAG
, RW_READER
);
390 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
391 vdev_t
*tvd
= rvd
->vdev_child
[c
];
392 metaslab_group_t
*mg
= tvd
->vdev_mg
;
395 * Skip any holes, uninitialized top-levels,
396 * or vdevs that are not in this metalab class.
398 if (!vdev_is_concrete(tvd
) || tvd
->vdev_ms_shift
== 0 ||
399 mg
->mg_class
!= mc
) {
404 * If a metaslab group does not contain a fragmentation
405 * metric then just bail out.
407 if (mg
->mg_fragmentation
== ZFS_FRAG_INVALID
) {
408 spa_config_exit(mc
->mc_spa
, SCL_VDEV
, FTAG
);
409 return (ZFS_FRAG_INVALID
);
413 * Determine how much this metaslab_group is contributing
414 * to the overall pool fragmentation metric.
416 fragmentation
+= mg
->mg_fragmentation
*
417 metaslab_group_get_space(mg
);
419 fragmentation
/= metaslab_class_get_space(mc
);
421 ASSERT3U(fragmentation
, <=, 100);
422 spa_config_exit(mc
->mc_spa
, SCL_VDEV
, FTAG
);
423 return (fragmentation
);
427 * Calculate the amount of expandable space that is available in
428 * this metaslab class. If a device is expanded then its expandable
429 * space will be the amount of allocatable space that is currently not
430 * part of this metaslab class.
433 metaslab_class_expandable_space(metaslab_class_t
*mc
)
435 vdev_t
*rvd
= mc
->mc_spa
->spa_root_vdev
;
438 spa_config_enter(mc
->mc_spa
, SCL_VDEV
, FTAG
, RW_READER
);
439 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
440 vdev_t
*tvd
= rvd
->vdev_child
[c
];
441 metaslab_group_t
*mg
= tvd
->vdev_mg
;
443 if (!vdev_is_concrete(tvd
) || tvd
->vdev_ms_shift
== 0 ||
444 mg
->mg_class
!= mc
) {
449 * Calculate if we have enough space to add additional
450 * metaslabs. We report the expandable space in terms
451 * of the metaslab size since that's the unit of expansion.
453 space
+= P2ALIGN(tvd
->vdev_max_asize
- tvd
->vdev_asize
,
454 1ULL << tvd
->vdev_ms_shift
);
456 spa_config_exit(mc
->mc_spa
, SCL_VDEV
, FTAG
);
461 metaslab_compare(const void *x1
, const void *x2
)
463 const metaslab_t
*m1
= (const metaslab_t
*)x1
;
464 const metaslab_t
*m2
= (const metaslab_t
*)x2
;
468 if (m1
->ms_allocator
!= -1 && m1
->ms_primary
)
470 else if (m1
->ms_allocator
!= -1 && !m1
->ms_primary
)
472 if (m2
->ms_allocator
!= -1 && m2
->ms_primary
)
474 else if (m2
->ms_allocator
!= -1 && !m2
->ms_primary
)
478 * Sort inactive metaslabs first, then primaries, then secondaries. When
479 * selecting a metaslab to allocate from, an allocator first tries its
480 * primary, then secondary active metaslab. If it doesn't have active
481 * metaslabs, or can't allocate from them, it searches for an inactive
482 * metaslab to activate. If it can't find a suitable one, it will steal
483 * a primary or secondary metaslab from another allocator.
490 int cmp
= AVL_CMP(m2
->ms_weight
, m1
->ms_weight
);
494 IMPLY(AVL_CMP(m1
->ms_start
, m2
->ms_start
) == 0, m1
== m2
);
496 return (AVL_CMP(m1
->ms_start
, m2
->ms_start
));
500 metaslab_allocated_space(metaslab_t
*msp
)
502 return (msp
->ms_allocated_space
);
506 * Verify that the space accounting on disk matches the in-core range_trees.
509 metaslab_verify_space(metaslab_t
*msp
, uint64_t txg
)
511 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
512 uint64_t allocating
= 0;
513 uint64_t sm_free_space
, msp_free_space
;
515 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
516 ASSERT(!msp
->ms_condensing
);
518 if ((zfs_flags
& ZFS_DEBUG_METASLAB_VERIFY
) == 0)
522 * We can only verify the metaslab space when we're called
523 * from syncing context with a loaded metaslab that has an
524 * allocated space map. Calling this in non-syncing context
525 * does not provide a consistent view of the metaslab since
526 * we're performing allocations in the future.
528 if (txg
!= spa_syncing_txg(spa
) || msp
->ms_sm
== NULL
||
533 * Even though the smp_alloc field can get negative (e.g.
534 * see vdev_checkpoint_sm), that should never be the case
535 * when it come's to a metaslab's space map.
537 ASSERT3S(space_map_allocated(msp
->ms_sm
), >=, 0);
539 sm_free_space
= msp
->ms_size
- metaslab_allocated_space(msp
);
542 * Account for future allocations since we would have
543 * already deducted that space from the ms_allocatable.
545 for (int t
= 0; t
< TXG_CONCURRENT_STATES
; t
++) {
547 range_tree_space(msp
->ms_allocating
[(txg
+ t
) & TXG_MASK
]);
550 ASSERT3U(msp
->ms_deferspace
, ==,
551 range_tree_space(msp
->ms_defer
[0]) +
552 range_tree_space(msp
->ms_defer
[1]));
554 msp_free_space
= range_tree_space(msp
->ms_allocatable
) + allocating
+
555 msp
->ms_deferspace
+ range_tree_space(msp
->ms_freed
);
557 VERIFY3U(sm_free_space
, ==, msp_free_space
);
561 * ==========================================================================
563 * ==========================================================================
566 * Update the allocatable flag and the metaslab group's capacity.
567 * The allocatable flag is set to true if the capacity is below
568 * the zfs_mg_noalloc_threshold or has a fragmentation value that is
569 * greater than zfs_mg_fragmentation_threshold. If a metaslab group
570 * transitions from allocatable to non-allocatable or vice versa then the
571 * metaslab group's class is updated to reflect the transition.
574 metaslab_group_alloc_update(metaslab_group_t
*mg
)
576 vdev_t
*vd
= mg
->mg_vd
;
577 metaslab_class_t
*mc
= mg
->mg_class
;
578 vdev_stat_t
*vs
= &vd
->vdev_stat
;
579 boolean_t was_allocatable
;
580 boolean_t was_initialized
;
582 ASSERT(vd
== vd
->vdev_top
);
583 ASSERT3U(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_READER
), ==,
586 mutex_enter(&mg
->mg_lock
);
587 was_allocatable
= mg
->mg_allocatable
;
588 was_initialized
= mg
->mg_initialized
;
590 mg
->mg_free_capacity
= ((vs
->vs_space
- vs
->vs_alloc
) * 100) /
593 mutex_enter(&mc
->mc_lock
);
596 * If the metaslab group was just added then it won't
597 * have any space until we finish syncing out this txg.
598 * At that point we will consider it initialized and available
599 * for allocations. We also don't consider non-activated
600 * metaslab groups (e.g. vdevs that are in the middle of being removed)
601 * to be initialized, because they can't be used for allocation.
603 mg
->mg_initialized
= metaslab_group_initialized(mg
);
604 if (!was_initialized
&& mg
->mg_initialized
) {
606 } else if (was_initialized
&& !mg
->mg_initialized
) {
607 ASSERT3U(mc
->mc_groups
, >, 0);
610 if (mg
->mg_initialized
)
611 mg
->mg_no_free_space
= B_FALSE
;
614 * A metaslab group is considered allocatable if it has plenty
615 * of free space or is not heavily fragmented. We only take
616 * fragmentation into account if the metaslab group has a valid
617 * fragmentation metric (i.e. a value between 0 and 100).
619 mg
->mg_allocatable
= (mg
->mg_activation_count
> 0 &&
620 mg
->mg_free_capacity
> zfs_mg_noalloc_threshold
&&
621 (mg
->mg_fragmentation
== ZFS_FRAG_INVALID
||
622 mg
->mg_fragmentation
<= zfs_mg_fragmentation_threshold
));
625 * The mc_alloc_groups maintains a count of the number of
626 * groups in this metaslab class that are still above the
627 * zfs_mg_noalloc_threshold. This is used by the allocating
628 * threads to determine if they should avoid allocations to
629 * a given group. The allocator will avoid allocations to a group
630 * if that group has reached or is below the zfs_mg_noalloc_threshold
631 * and there are still other groups that are above the threshold.
632 * When a group transitions from allocatable to non-allocatable or
633 * vice versa we update the metaslab class to reflect that change.
634 * When the mc_alloc_groups value drops to 0 that means that all
635 * groups have reached the zfs_mg_noalloc_threshold making all groups
636 * eligible for allocations. This effectively means that all devices
637 * are balanced again.
639 if (was_allocatable
&& !mg
->mg_allocatable
)
640 mc
->mc_alloc_groups
--;
641 else if (!was_allocatable
&& mg
->mg_allocatable
)
642 mc
->mc_alloc_groups
++;
643 mutex_exit(&mc
->mc_lock
);
645 mutex_exit(&mg
->mg_lock
);
649 metaslab_group_create(metaslab_class_t
*mc
, vdev_t
*vd
, int allocators
)
651 metaslab_group_t
*mg
;
653 mg
= kmem_zalloc(sizeof (metaslab_group_t
), KM_SLEEP
);
654 mutex_init(&mg
->mg_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
655 mutex_init(&mg
->mg_ms_initialize_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
656 cv_init(&mg
->mg_ms_initialize_cv
, NULL
, CV_DEFAULT
, NULL
);
657 mg
->mg_primaries
= kmem_zalloc(allocators
* sizeof (metaslab_t
*),
659 mg
->mg_secondaries
= kmem_zalloc(allocators
* sizeof (metaslab_t
*),
661 avl_create(&mg
->mg_metaslab_tree
, metaslab_compare
,
662 sizeof (metaslab_t
), offsetof(struct metaslab
, ms_group_node
));
665 mg
->mg_activation_count
= 0;
666 mg
->mg_initialized
= B_FALSE
;
667 mg
->mg_no_free_space
= B_TRUE
;
668 mg
->mg_allocators
= allocators
;
670 mg
->mg_alloc_queue_depth
= kmem_zalloc(allocators
*
671 sizeof (zfs_refcount_t
), KM_SLEEP
);
672 mg
->mg_cur_max_alloc_queue_depth
= kmem_zalloc(allocators
*
673 sizeof (uint64_t), KM_SLEEP
);
674 for (int i
= 0; i
< allocators
; i
++) {
675 zfs_refcount_create_tracked(&mg
->mg_alloc_queue_depth
[i
]);
676 mg
->mg_cur_max_alloc_queue_depth
[i
] = 0;
679 mg
->mg_taskq
= taskq_create("metaslab_group_taskq", metaslab_load_pct
,
680 maxclsyspri
, 10, INT_MAX
, TASKQ_THREADS_CPU_PCT
| TASKQ_DYNAMIC
);
686 metaslab_group_destroy(metaslab_group_t
*mg
)
688 ASSERT(mg
->mg_prev
== NULL
);
689 ASSERT(mg
->mg_next
== NULL
);
691 * We may have gone below zero with the activation count
692 * either because we never activated in the first place or
693 * because we're done, and possibly removing the vdev.
695 ASSERT(mg
->mg_activation_count
<= 0);
697 taskq_destroy(mg
->mg_taskq
);
698 avl_destroy(&mg
->mg_metaslab_tree
);
699 kmem_free(mg
->mg_primaries
, mg
->mg_allocators
* sizeof (metaslab_t
*));
700 kmem_free(mg
->mg_secondaries
, mg
->mg_allocators
*
701 sizeof (metaslab_t
*));
702 mutex_destroy(&mg
->mg_lock
);
703 mutex_destroy(&mg
->mg_ms_initialize_lock
);
704 cv_destroy(&mg
->mg_ms_initialize_cv
);
706 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
707 zfs_refcount_destroy(&mg
->mg_alloc_queue_depth
[i
]);
708 mg
->mg_cur_max_alloc_queue_depth
[i
] = 0;
710 kmem_free(mg
->mg_alloc_queue_depth
, mg
->mg_allocators
*
711 sizeof (zfs_refcount_t
));
712 kmem_free(mg
->mg_cur_max_alloc_queue_depth
, mg
->mg_allocators
*
715 kmem_free(mg
, sizeof (metaslab_group_t
));
719 metaslab_group_activate(metaslab_group_t
*mg
)
721 metaslab_class_t
*mc
= mg
->mg_class
;
722 metaslab_group_t
*mgprev
, *mgnext
;
724 ASSERT3U(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_WRITER
), !=, 0);
726 ASSERT(mc
->mc_rotor
!= mg
);
727 ASSERT(mg
->mg_prev
== NULL
);
728 ASSERT(mg
->mg_next
== NULL
);
729 ASSERT(mg
->mg_activation_count
<= 0);
731 if (++mg
->mg_activation_count
<= 0)
734 mg
->mg_aliquot
= metaslab_aliquot
* MAX(1, mg
->mg_vd
->vdev_children
);
735 metaslab_group_alloc_update(mg
);
737 if ((mgprev
= mc
->mc_rotor
) == NULL
) {
741 mgnext
= mgprev
->mg_next
;
742 mg
->mg_prev
= mgprev
;
743 mg
->mg_next
= mgnext
;
744 mgprev
->mg_next
= mg
;
745 mgnext
->mg_prev
= mg
;
751 * Passivate a metaslab group and remove it from the allocation rotor.
752 * Callers must hold both the SCL_ALLOC and SCL_ZIO lock prior to passivating
753 * a metaslab group. This function will momentarily drop spa_config_locks
754 * that are lower than the SCL_ALLOC lock (see comment below).
757 metaslab_group_passivate(metaslab_group_t
*mg
)
759 metaslab_class_t
*mc
= mg
->mg_class
;
760 spa_t
*spa
= mc
->mc_spa
;
761 metaslab_group_t
*mgprev
, *mgnext
;
762 int locks
= spa_config_held(spa
, SCL_ALL
, RW_WRITER
);
764 ASSERT3U(spa_config_held(spa
, SCL_ALLOC
| SCL_ZIO
, RW_WRITER
), ==,
765 (SCL_ALLOC
| SCL_ZIO
));
767 if (--mg
->mg_activation_count
!= 0) {
768 ASSERT(mc
->mc_rotor
!= mg
);
769 ASSERT(mg
->mg_prev
== NULL
);
770 ASSERT(mg
->mg_next
== NULL
);
771 ASSERT(mg
->mg_activation_count
< 0);
776 * The spa_config_lock is an array of rwlocks, ordered as
777 * follows (from highest to lowest):
778 * SCL_CONFIG > SCL_STATE > SCL_L2ARC > SCL_ALLOC >
779 * SCL_ZIO > SCL_FREE > SCL_VDEV
780 * (For more information about the spa_config_lock see spa_misc.c)
781 * The higher the lock, the broader its coverage. When we passivate
782 * a metaslab group, we must hold both the SCL_ALLOC and the SCL_ZIO
783 * config locks. However, the metaslab group's taskq might be trying
784 * to preload metaslabs so we must drop the SCL_ZIO lock and any
785 * lower locks to allow the I/O to complete. At a minimum,
786 * we continue to hold the SCL_ALLOC lock, which prevents any future
787 * allocations from taking place and any changes to the vdev tree.
789 spa_config_exit(spa
, locks
& ~(SCL_ZIO
- 1), spa
);
790 taskq_wait_outstanding(mg
->mg_taskq
, 0);
791 spa_config_enter(spa
, locks
& ~(SCL_ZIO
- 1), spa
, RW_WRITER
);
792 metaslab_group_alloc_update(mg
);
793 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
794 metaslab_t
*msp
= mg
->mg_primaries
[i
];
796 mutex_enter(&msp
->ms_lock
);
797 metaslab_passivate(msp
,
798 metaslab_weight_from_range_tree(msp
));
799 mutex_exit(&msp
->ms_lock
);
801 msp
= mg
->mg_secondaries
[i
];
803 mutex_enter(&msp
->ms_lock
);
804 metaslab_passivate(msp
,
805 metaslab_weight_from_range_tree(msp
));
806 mutex_exit(&msp
->ms_lock
);
810 mgprev
= mg
->mg_prev
;
811 mgnext
= mg
->mg_next
;
816 mc
->mc_rotor
= mgnext
;
817 mgprev
->mg_next
= mgnext
;
818 mgnext
->mg_prev
= mgprev
;
826 metaslab_group_initialized(metaslab_group_t
*mg
)
828 vdev_t
*vd
= mg
->mg_vd
;
829 vdev_stat_t
*vs
= &vd
->vdev_stat
;
831 return (vs
->vs_space
!= 0 && mg
->mg_activation_count
> 0);
835 metaslab_group_get_space(metaslab_group_t
*mg
)
837 return ((1ULL << mg
->mg_vd
->vdev_ms_shift
) * mg
->mg_vd
->vdev_ms_count
);
841 metaslab_group_histogram_verify(metaslab_group_t
*mg
)
844 vdev_t
*vd
= mg
->mg_vd
;
845 uint64_t ashift
= vd
->vdev_ashift
;
848 if ((zfs_flags
& ZFS_DEBUG_HISTOGRAM_VERIFY
) == 0)
851 mg_hist
= kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE
,
854 ASSERT3U(RANGE_TREE_HISTOGRAM_SIZE
, >=,
855 SPACE_MAP_HISTOGRAM_SIZE
+ ashift
);
857 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
858 metaslab_t
*msp
= vd
->vdev_ms
[m
];
861 /* skip if not active or not a member */
862 if (msp
->ms_sm
== NULL
|| msp
->ms_group
!= mg
)
865 for (i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++)
866 mg_hist
[i
+ ashift
] +=
867 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
870 for (i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
871 VERIFY3U(mg_hist
[i
], ==, mg
->mg_histogram
[i
]);
873 kmem_free(mg_hist
, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE
);
877 metaslab_group_histogram_add(metaslab_group_t
*mg
, metaslab_t
*msp
)
879 metaslab_class_t
*mc
= mg
->mg_class
;
880 uint64_t ashift
= mg
->mg_vd
->vdev_ashift
;
882 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
883 if (msp
->ms_sm
== NULL
)
886 mutex_enter(&mg
->mg_lock
);
887 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++) {
888 mg
->mg_histogram
[i
+ ashift
] +=
889 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
890 mc
->mc_histogram
[i
+ ashift
] +=
891 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
893 mutex_exit(&mg
->mg_lock
);
897 metaslab_group_histogram_remove(metaslab_group_t
*mg
, metaslab_t
*msp
)
899 metaslab_class_t
*mc
= mg
->mg_class
;
900 uint64_t ashift
= mg
->mg_vd
->vdev_ashift
;
902 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
903 if (msp
->ms_sm
== NULL
)
906 mutex_enter(&mg
->mg_lock
);
907 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++) {
908 ASSERT3U(mg
->mg_histogram
[i
+ ashift
], >=,
909 msp
->ms_sm
->sm_phys
->smp_histogram
[i
]);
910 ASSERT3U(mc
->mc_histogram
[i
+ ashift
], >=,
911 msp
->ms_sm
->sm_phys
->smp_histogram
[i
]);
913 mg
->mg_histogram
[i
+ ashift
] -=
914 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
915 mc
->mc_histogram
[i
+ ashift
] -=
916 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
918 mutex_exit(&mg
->mg_lock
);
922 metaslab_group_add(metaslab_group_t
*mg
, metaslab_t
*msp
)
924 ASSERT(msp
->ms_group
== NULL
);
925 mutex_enter(&mg
->mg_lock
);
928 avl_add(&mg
->mg_metaslab_tree
, msp
);
929 mutex_exit(&mg
->mg_lock
);
931 mutex_enter(&msp
->ms_lock
);
932 metaslab_group_histogram_add(mg
, msp
);
933 mutex_exit(&msp
->ms_lock
);
937 metaslab_group_remove(metaslab_group_t
*mg
, metaslab_t
*msp
)
939 mutex_enter(&msp
->ms_lock
);
940 metaslab_group_histogram_remove(mg
, msp
);
941 mutex_exit(&msp
->ms_lock
);
943 mutex_enter(&mg
->mg_lock
);
944 ASSERT(msp
->ms_group
== mg
);
945 avl_remove(&mg
->mg_metaslab_tree
, msp
);
946 msp
->ms_group
= NULL
;
947 mutex_exit(&mg
->mg_lock
);
951 metaslab_group_sort_impl(metaslab_group_t
*mg
, metaslab_t
*msp
, uint64_t weight
)
953 ASSERT(MUTEX_HELD(&mg
->mg_lock
));
954 ASSERT(msp
->ms_group
== mg
);
955 avl_remove(&mg
->mg_metaslab_tree
, msp
);
956 msp
->ms_weight
= weight
;
957 avl_add(&mg
->mg_metaslab_tree
, msp
);
962 metaslab_group_sort(metaslab_group_t
*mg
, metaslab_t
*msp
, uint64_t weight
)
965 * Although in principle the weight can be any value, in
966 * practice we do not use values in the range [1, 511].
968 ASSERT(weight
>= SPA_MINBLOCKSIZE
|| weight
== 0);
969 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
971 mutex_enter(&mg
->mg_lock
);
972 metaslab_group_sort_impl(mg
, msp
, weight
);
973 mutex_exit(&mg
->mg_lock
);
977 * Calculate the fragmentation for a given metaslab group. We can use
978 * a simple average here since all metaslabs within the group must have
979 * the same size. The return value will be a value between 0 and 100
980 * (inclusive), or ZFS_FRAG_INVALID if less than half of the metaslab in this
981 * group have a fragmentation metric.
984 metaslab_group_fragmentation(metaslab_group_t
*mg
)
986 vdev_t
*vd
= mg
->mg_vd
;
987 uint64_t fragmentation
= 0;
988 uint64_t valid_ms
= 0;
990 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
991 metaslab_t
*msp
= vd
->vdev_ms
[m
];
993 if (msp
->ms_fragmentation
== ZFS_FRAG_INVALID
)
995 if (msp
->ms_group
!= mg
)
999 fragmentation
+= msp
->ms_fragmentation
;
1002 if (valid_ms
<= mg
->mg_vd
->vdev_ms_count
/ 2)
1003 return (ZFS_FRAG_INVALID
);
1005 fragmentation
/= valid_ms
;
1006 ASSERT3U(fragmentation
, <=, 100);
1007 return (fragmentation
);
1011 * Determine if a given metaslab group should skip allocations. A metaslab
1012 * group should avoid allocations if its free capacity is less than the
1013 * zfs_mg_noalloc_threshold or its fragmentation metric is greater than
1014 * zfs_mg_fragmentation_threshold and there is at least one metaslab group
1015 * that can still handle allocations. If the allocation throttle is enabled
1016 * then we skip allocations to devices that have reached their maximum
1017 * allocation queue depth unless the selected metaslab group is the only
1018 * eligible group remaining.
1021 metaslab_group_allocatable(metaslab_group_t
*mg
, metaslab_group_t
*rotor
,
1022 uint64_t psize
, int allocator
, int d
)
1024 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
1025 metaslab_class_t
*mc
= mg
->mg_class
;
1028 * We can only consider skipping this metaslab group if it's
1029 * in the normal metaslab class and there are other metaslab
1030 * groups to select from. Otherwise, we always consider it eligible
1033 if ((mc
!= spa_normal_class(spa
) &&
1034 mc
!= spa_special_class(spa
) &&
1035 mc
!= spa_dedup_class(spa
)) ||
1040 * If the metaslab group's mg_allocatable flag is set (see comments
1041 * in metaslab_group_alloc_update() for more information) and
1042 * the allocation throttle is disabled then allow allocations to this
1043 * device. However, if the allocation throttle is enabled then
1044 * check if we have reached our allocation limit (mg_alloc_queue_depth)
1045 * to determine if we should allow allocations to this metaslab group.
1046 * If all metaslab groups are no longer considered allocatable
1047 * (mc_alloc_groups == 0) or we're trying to allocate the smallest
1048 * gang block size then we allow allocations on this metaslab group
1049 * regardless of the mg_allocatable or throttle settings.
1051 if (mg
->mg_allocatable
) {
1052 metaslab_group_t
*mgp
;
1054 uint64_t qmax
= mg
->mg_cur_max_alloc_queue_depth
[allocator
];
1056 if (!mc
->mc_alloc_throttle_enabled
)
1060 * If this metaslab group does not have any free space, then
1061 * there is no point in looking further.
1063 if (mg
->mg_no_free_space
)
1067 * Relax allocation throttling for ditto blocks. Due to
1068 * random imbalances in allocation it tends to push copies
1069 * to one vdev, that looks a bit better at the moment.
1071 qmax
= qmax
* (4 + d
) / 4;
1073 qdepth
= zfs_refcount_count(
1074 &mg
->mg_alloc_queue_depth
[allocator
]);
1077 * If this metaslab group is below its qmax or it's
1078 * the only allocatable metasable group, then attempt
1079 * to allocate from it.
1081 if (qdepth
< qmax
|| mc
->mc_alloc_groups
== 1)
1083 ASSERT3U(mc
->mc_alloc_groups
, >, 1);
1086 * Since this metaslab group is at or over its qmax, we
1087 * need to determine if there are metaslab groups after this
1088 * one that might be able to handle this allocation. This is
1089 * racy since we can't hold the locks for all metaslab
1090 * groups at the same time when we make this check.
1092 for (mgp
= mg
->mg_next
; mgp
!= rotor
; mgp
= mgp
->mg_next
) {
1093 qmax
= mgp
->mg_cur_max_alloc_queue_depth
[allocator
];
1094 qmax
= qmax
* (4 + d
) / 4;
1095 qdepth
= zfs_refcount_count(
1096 &mgp
->mg_alloc_queue_depth
[allocator
]);
1099 * If there is another metaslab group that
1100 * might be able to handle the allocation, then
1101 * we return false so that we skip this group.
1103 if (qdepth
< qmax
&& !mgp
->mg_no_free_space
)
1108 * We didn't find another group to handle the allocation
1109 * so we can't skip this metaslab group even though
1110 * we are at or over our qmax.
1114 } else if (mc
->mc_alloc_groups
== 0 || psize
== SPA_MINBLOCKSIZE
) {
1121 * ==========================================================================
1122 * Range tree callbacks
1123 * ==========================================================================
1127 * Comparison function for the private size-ordered tree. Tree is sorted
1128 * by size, larger sizes at the end of the tree.
1131 metaslab_rangesize_compare(const void *x1
, const void *x2
)
1133 const range_seg_t
*r1
= x1
;
1134 const range_seg_t
*r2
= x2
;
1135 uint64_t rs_size1
= r1
->rs_end
- r1
->rs_start
;
1136 uint64_t rs_size2
= r2
->rs_end
- r2
->rs_start
;
1138 int cmp
= AVL_CMP(rs_size1
, rs_size2
);
1142 return (AVL_CMP(r1
->rs_start
, r2
->rs_start
));
1146 * ==========================================================================
1147 * Common allocator routines
1148 * ==========================================================================
1152 * Return the maximum contiguous segment within the metaslab.
1155 metaslab_block_maxsize(metaslab_t
*msp
)
1157 avl_tree_t
*t
= &msp
->ms_allocatable_by_size
;
1160 if (t
== NULL
|| (rs
= avl_last(t
)) == NULL
)
1163 return (rs
->rs_end
- rs
->rs_start
);
1166 static range_seg_t
*
1167 metaslab_block_find(avl_tree_t
*t
, uint64_t start
, uint64_t size
)
1169 range_seg_t
*rs
, rsearch
;
1172 rsearch
.rs_start
= start
;
1173 rsearch
.rs_end
= start
+ size
;
1175 rs
= avl_find(t
, &rsearch
, &where
);
1177 rs
= avl_nearest(t
, where
, AVL_AFTER
);
1183 #if defined(WITH_FF_BLOCK_ALLOCATOR) || \
1184 defined(WITH_DF_BLOCK_ALLOCATOR) || \
1185 defined(WITH_CF_BLOCK_ALLOCATOR)
1187 * This is a helper function that can be used by the allocator to find
1188 * a suitable block to allocate. This will search the specified AVL
1189 * tree looking for a block that matches the specified criteria.
1192 metaslab_block_picker(avl_tree_t
*t
, uint64_t *cursor
, uint64_t size
,
1195 range_seg_t
*rs
= metaslab_block_find(t
, *cursor
, size
);
1197 while (rs
!= NULL
) {
1198 uint64_t offset
= P2ROUNDUP(rs
->rs_start
, align
);
1200 if (offset
+ size
<= rs
->rs_end
) {
1201 *cursor
= offset
+ size
;
1204 rs
= AVL_NEXT(t
, rs
);
1208 * If we know we've searched the whole map (*cursor == 0), give up.
1209 * Otherwise, reset the cursor to the beginning and try again.
1215 return (metaslab_block_picker(t
, cursor
, size
, align
));
1217 #endif /* WITH_FF/DF/CF_BLOCK_ALLOCATOR */
1219 #if defined(WITH_FF_BLOCK_ALLOCATOR)
1221 * ==========================================================================
1222 * The first-fit block allocator
1223 * ==========================================================================
1226 metaslab_ff_alloc(metaslab_t
*msp
, uint64_t size
)
1229 * Find the largest power of 2 block size that evenly divides the
1230 * requested size. This is used to try to allocate blocks with similar
1231 * alignment from the same area of the metaslab (i.e. same cursor
1232 * bucket) but it does not guarantee that other allocations sizes
1233 * may exist in the same region.
1235 uint64_t align
= size
& -size
;
1236 uint64_t *cursor
= &msp
->ms_lbas
[highbit64(align
) - 1];
1237 avl_tree_t
*t
= &msp
->ms_allocatable
->rt_root
;
1239 return (metaslab_block_picker(t
, cursor
, size
, align
));
1242 static metaslab_ops_t metaslab_ff_ops
= {
1246 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_ff_ops
;
1247 #endif /* WITH_FF_BLOCK_ALLOCATOR */
1249 #if defined(WITH_DF_BLOCK_ALLOCATOR)
1251 * ==========================================================================
1252 * Dynamic block allocator -
1253 * Uses the first fit allocation scheme until space get low and then
1254 * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
1255 * and metaslab_df_free_pct to determine when to switch the allocation scheme.
1256 * ==========================================================================
1259 metaslab_df_alloc(metaslab_t
*msp
, uint64_t size
)
1262 * Find the largest power of 2 block size that evenly divides the
1263 * requested size. This is used to try to allocate blocks with similar
1264 * alignment from the same area of the metaslab (i.e. same cursor
1265 * bucket) but it does not guarantee that other allocations sizes
1266 * may exist in the same region.
1268 uint64_t align
= size
& -size
;
1269 uint64_t *cursor
= &msp
->ms_lbas
[highbit64(align
) - 1];
1270 range_tree_t
*rt
= msp
->ms_allocatable
;
1271 avl_tree_t
*t
= &rt
->rt_root
;
1272 uint64_t max_size
= metaslab_block_maxsize(msp
);
1273 int free_pct
= range_tree_space(rt
) * 100 / msp
->ms_size
;
1275 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1276 ASSERT3U(avl_numnodes(t
), ==,
1277 avl_numnodes(&msp
->ms_allocatable_by_size
));
1279 if (max_size
< size
)
1283 * If we're running low on space switch to using the size
1284 * sorted AVL tree (best-fit).
1286 if (max_size
< metaslab_df_alloc_threshold
||
1287 free_pct
< metaslab_df_free_pct
) {
1288 t
= &msp
->ms_allocatable_by_size
;
1292 return (metaslab_block_picker(t
, cursor
, size
, 1ULL));
1295 static metaslab_ops_t metaslab_df_ops
= {
1299 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_df_ops
;
1300 #endif /* WITH_DF_BLOCK_ALLOCATOR */
1302 #if defined(WITH_CF_BLOCK_ALLOCATOR)
1304 * ==========================================================================
1305 * Cursor fit block allocator -
1306 * Select the largest region in the metaslab, set the cursor to the beginning
1307 * of the range and the cursor_end to the end of the range. As allocations
1308 * are made advance the cursor. Continue allocating from the cursor until
1309 * the range is exhausted and then find a new range.
1310 * ==========================================================================
1313 metaslab_cf_alloc(metaslab_t
*msp
, uint64_t size
)
1315 range_tree_t
*rt
= msp
->ms_allocatable
;
1316 avl_tree_t
*t
= &msp
->ms_allocatable_by_size
;
1317 uint64_t *cursor
= &msp
->ms_lbas
[0];
1318 uint64_t *cursor_end
= &msp
->ms_lbas
[1];
1319 uint64_t offset
= 0;
1321 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1322 ASSERT3U(avl_numnodes(t
), ==, avl_numnodes(&rt
->rt_root
));
1324 ASSERT3U(*cursor_end
, >=, *cursor
);
1326 if ((*cursor
+ size
) > *cursor_end
) {
1329 rs
= avl_last(&msp
->ms_allocatable_by_size
);
1330 if (rs
== NULL
|| (rs
->rs_end
- rs
->rs_start
) < size
)
1333 *cursor
= rs
->rs_start
;
1334 *cursor_end
= rs
->rs_end
;
1343 static metaslab_ops_t metaslab_cf_ops
= {
1347 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_cf_ops
;
1348 #endif /* WITH_CF_BLOCK_ALLOCATOR */
1350 #if defined(WITH_NDF_BLOCK_ALLOCATOR)
1352 * ==========================================================================
1353 * New dynamic fit allocator -
1354 * Select a region that is large enough to allocate 2^metaslab_ndf_clump_shift
1355 * contiguous blocks. If no region is found then just use the largest segment
1357 * ==========================================================================
1361 * Determines desired number of contiguous blocks (2^metaslab_ndf_clump_shift)
1362 * to request from the allocator.
1364 uint64_t metaslab_ndf_clump_shift
= 4;
1367 metaslab_ndf_alloc(metaslab_t
*msp
, uint64_t size
)
1369 avl_tree_t
*t
= &msp
->ms_allocatable
->rt_root
;
1371 range_seg_t
*rs
, rsearch
;
1372 uint64_t hbit
= highbit64(size
);
1373 uint64_t *cursor
= &msp
->ms_lbas
[hbit
- 1];
1374 uint64_t max_size
= metaslab_block_maxsize(msp
);
1376 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1377 ASSERT3U(avl_numnodes(t
), ==,
1378 avl_numnodes(&msp
->ms_allocatable_by_size
));
1380 if (max_size
< size
)
1383 rsearch
.rs_start
= *cursor
;
1384 rsearch
.rs_end
= *cursor
+ size
;
1386 rs
= avl_find(t
, &rsearch
, &where
);
1387 if (rs
== NULL
|| (rs
->rs_end
- rs
->rs_start
) < size
) {
1388 t
= &msp
->ms_allocatable_by_size
;
1390 rsearch
.rs_start
= 0;
1391 rsearch
.rs_end
= MIN(max_size
,
1392 1ULL << (hbit
+ metaslab_ndf_clump_shift
));
1393 rs
= avl_find(t
, &rsearch
, &where
);
1395 rs
= avl_nearest(t
, where
, AVL_AFTER
);
1399 if ((rs
->rs_end
- rs
->rs_start
) >= size
) {
1400 *cursor
= rs
->rs_start
+ size
;
1401 return (rs
->rs_start
);
1406 static metaslab_ops_t metaslab_ndf_ops
= {
1410 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_ndf_ops
;
1411 #endif /* WITH_NDF_BLOCK_ALLOCATOR */
1415 * ==========================================================================
1417 * ==========================================================================
1421 metaslab_aux_histograms_clear(metaslab_t
*msp
)
1424 * Auxiliary histograms are only cleared when resetting them,
1425 * which can only happen while the metaslab is loaded.
1427 ASSERT(msp
->ms_loaded
);
1429 bzero(msp
->ms_synchist
, sizeof (msp
->ms_synchist
));
1430 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++)
1431 bzero(msp
->ms_deferhist
[t
], sizeof (msp
->ms_deferhist
[t
]));
1435 metaslab_aux_histogram_add(uint64_t *histogram
, uint64_t shift
,
1439 * This is modeled after space_map_histogram_add(), so refer to that
1440 * function for implementation details. We want this to work like
1441 * the space map histogram, and not the range tree histogram, as we
1442 * are essentially constructing a delta that will be later subtracted
1443 * from the space map histogram.
1446 for (int i
= shift
; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++) {
1447 ASSERT3U(i
, >=, idx
+ shift
);
1448 histogram
[idx
] += rt
->rt_histogram
[i
] << (i
- idx
- shift
);
1450 if (idx
< SPACE_MAP_HISTOGRAM_SIZE
- 1) {
1451 ASSERT3U(idx
+ shift
, ==, i
);
1453 ASSERT3U(idx
, <, SPACE_MAP_HISTOGRAM_SIZE
);
1459 * Called at every sync pass that the metaslab gets synced.
1461 * The reason is that we want our auxiliary histograms to be updated
1462 * wherever the metaslab's space map histogram is updated. This way
1463 * we stay consistent on which parts of the metaslab space map's
1464 * histogram are currently not available for allocations (e.g because
1465 * they are in the defer, freed, and freeing trees).
1468 metaslab_aux_histograms_update(metaslab_t
*msp
)
1470 space_map_t
*sm
= msp
->ms_sm
;
1474 * This is similar to the metaslab's space map histogram updates
1475 * that take place in metaslab_sync(). The only difference is that
1476 * we only care about segments that haven't made it into the
1477 * ms_allocatable tree yet.
1479 if (msp
->ms_loaded
) {
1480 metaslab_aux_histograms_clear(msp
);
1482 metaslab_aux_histogram_add(msp
->ms_synchist
,
1483 sm
->sm_shift
, msp
->ms_freed
);
1485 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1486 metaslab_aux_histogram_add(msp
->ms_deferhist
[t
],
1487 sm
->sm_shift
, msp
->ms_defer
[t
]);
1491 metaslab_aux_histogram_add(msp
->ms_synchist
,
1492 sm
->sm_shift
, msp
->ms_freeing
);
1496 * Called every time we are done syncing (writing to) the metaslab,
1497 * i.e. at the end of each sync pass.
1498 * [see the comment in metaslab_impl.h for ms_synchist, ms_deferhist]
1501 metaslab_aux_histograms_update_done(metaslab_t
*msp
, boolean_t defer_allowed
)
1503 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
1504 space_map_t
*sm
= msp
->ms_sm
;
1508 * We came here from metaslab_init() when creating/opening a
1509 * pool, looking at a metaslab that hasn't had any allocations
1516 * This is similar to the actions that we take for the ms_freed
1517 * and ms_defer trees in metaslab_sync_done().
1519 uint64_t hist_index
= spa_syncing_txg(spa
) % TXG_DEFER_SIZE
;
1520 if (defer_allowed
) {
1521 bcopy(msp
->ms_synchist
, msp
->ms_deferhist
[hist_index
],
1522 sizeof (msp
->ms_synchist
));
1524 bzero(msp
->ms_deferhist
[hist_index
],
1525 sizeof (msp
->ms_deferhist
[hist_index
]));
1527 bzero(msp
->ms_synchist
, sizeof (msp
->ms_synchist
));
1531 * Ensure that the metaslab's weight and fragmentation are consistent
1532 * with the contents of the histogram (either the range tree's histogram
1533 * or the space map's depending whether the metaslab is loaded).
1536 metaslab_verify_weight_and_frag(metaslab_t
*msp
)
1538 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1540 if ((zfs_flags
& ZFS_DEBUG_METASLAB_VERIFY
) == 0)
1543 /* see comment in metaslab_verify_unflushed_changes() */
1544 if (msp
->ms_group
== NULL
)
1548 * Devices being removed always return a weight of 0 and leave
1549 * fragmentation and ms_max_size as is - there is nothing for
1550 * us to verify here.
1552 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
1553 if (vd
->vdev_removing
)
1557 * If the metaslab is dirty it probably means that we've done
1558 * some allocations or frees that have changed our histograms
1559 * and thus the weight.
1561 for (int t
= 0; t
< TXG_SIZE
; t
++) {
1562 if (txg_list_member(&vd
->vdev_ms_list
, msp
, t
))
1567 * This verification checks that our in-memory state is consistent
1568 * with what's on disk. If the pool is read-only then there aren't
1569 * any changes and we just have the initially-loaded state.
1571 if (!spa_writeable(msp
->ms_group
->mg_vd
->vdev_spa
))
1574 /* some extra verification for in-core tree if you can */
1575 if (msp
->ms_loaded
) {
1576 range_tree_stat_verify(msp
->ms_allocatable
);
1577 VERIFY(space_map_histogram_verify(msp
->ms_sm
,
1578 msp
->ms_allocatable
));
1581 uint64_t weight
= msp
->ms_weight
;
1582 uint64_t was_active
= msp
->ms_weight
& METASLAB_ACTIVE_MASK
;
1583 boolean_t space_based
= WEIGHT_IS_SPACEBASED(msp
->ms_weight
);
1584 uint64_t frag
= msp
->ms_fragmentation
;
1585 uint64_t max_segsize
= msp
->ms_max_size
;
1588 msp
->ms_fragmentation
= 0;
1589 msp
->ms_max_size
= 0;
1592 * This function is used for verification purposes. Regardless of
1593 * whether metaslab_weight() thinks this metaslab should be active or
1594 * not, we want to ensure that the actual weight (and therefore the
1595 * value of ms_weight) would be the same if it was to be recalculated
1598 msp
->ms_weight
= metaslab_weight(msp
) | was_active
;
1600 VERIFY3U(max_segsize
, ==, msp
->ms_max_size
);
1603 * If the weight type changed then there is no point in doing
1604 * verification. Revert fields to their original values.
1606 if ((space_based
&& !WEIGHT_IS_SPACEBASED(msp
->ms_weight
)) ||
1607 (!space_based
&& WEIGHT_IS_SPACEBASED(msp
->ms_weight
))) {
1608 msp
->ms_fragmentation
= frag
;
1609 msp
->ms_weight
= weight
;
1613 VERIFY3U(msp
->ms_fragmentation
, ==, frag
);
1614 VERIFY3U(msp
->ms_weight
, ==, weight
);
1618 * Wait for any in-progress metaslab loads to complete.
1621 metaslab_load_wait(metaslab_t
*msp
)
1623 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1625 while (msp
->ms_loading
) {
1626 ASSERT(!msp
->ms_loaded
);
1627 cv_wait(&msp
->ms_load_cv
, &msp
->ms_lock
);
1632 metaslab_load_impl(metaslab_t
*msp
)
1636 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1637 ASSERT(msp
->ms_loading
);
1638 ASSERT(!msp
->ms_condensing
);
1641 * We temporarily drop the lock to unblock other operations while we
1642 * are reading the space map. Therefore, metaslab_sync() and
1643 * metaslab_sync_done() can run at the same time as we do.
1645 * metaslab_sync() can append to the space map while we are loading.
1646 * Therefore we load only entries that existed when we started the
1647 * load. Additionally, metaslab_sync_done() has to wait for the load
1648 * to complete because there are potential races like metaslab_load()
1649 * loading parts of the space map that are currently being appended
1650 * by metaslab_sync(). If we didn't, the ms_allocatable would have
1651 * entries that metaslab_sync_done() would try to re-add later.
1653 * That's why before dropping the lock we remember the synced length
1654 * of the metaslab and read up to that point of the space map,
1655 * ignoring entries appended by metaslab_sync() that happen after we
1658 uint64_t length
= msp
->ms_synced_length
;
1659 mutex_exit(&msp
->ms_lock
);
1661 if (msp
->ms_sm
!= NULL
) {
1662 error
= space_map_load_length(msp
->ms_sm
, msp
->ms_allocatable
,
1666 * The space map has not been allocated yet, so treat
1667 * all the space in the metaslab as free and add it to the
1668 * ms_allocatable tree.
1670 range_tree_add(msp
->ms_allocatable
,
1671 msp
->ms_start
, msp
->ms_size
);
1675 * We need to grab the ms_sync_lock to prevent metaslab_sync() from
1676 * changing the ms_sm and the metaslab's range trees while we are
1677 * about to use them and populate the ms_allocatable. The ms_lock
1678 * is insufficient for this because metaslab_sync() doesn't hold
1679 * the ms_lock while writing the ms_checkpointing tree to disk.
1681 mutex_enter(&msp
->ms_sync_lock
);
1682 mutex_enter(&msp
->ms_lock
);
1683 ASSERT(!msp
->ms_condensing
);
1686 mutex_exit(&msp
->ms_sync_lock
);
1690 ASSERT3P(msp
->ms_group
, !=, NULL
);
1691 msp
->ms_loaded
= B_TRUE
;
1694 * The ms_allocatable contains the segments that exist in the
1695 * ms_defer trees [see ms_synced_length]. Thus we need to remove
1696 * them from ms_allocatable as they will be added again in
1697 * metaslab_sync_done().
1699 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1700 range_tree_walk(msp
->ms_defer
[t
],
1701 range_tree_remove
, msp
->ms_allocatable
);
1705 * Call metaslab_recalculate_weight_and_sort() now that the
1706 * metaslab is loaded so we get the metaslab's real weight.
1708 * Unless this metaslab was created with older software and
1709 * has not yet been converted to use segment-based weight, we
1710 * expect the new weight to be better or equal to the weight
1711 * that the metaslab had while it was not loaded. This is
1712 * because the old weight does not take into account the
1713 * consolidation of adjacent segments between TXGs. [see
1714 * comment for ms_synchist and ms_deferhist[] for more info]
1716 uint64_t weight
= msp
->ms_weight
;
1717 metaslab_recalculate_weight_and_sort(msp
);
1718 if (!WEIGHT_IS_SPACEBASED(weight
))
1719 ASSERT3U(weight
, <=, msp
->ms_weight
);
1720 msp
->ms_max_size
= metaslab_block_maxsize(msp
);
1722 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
1723 metaslab_verify_space(msp
, spa_syncing_txg(spa
));
1724 mutex_exit(&msp
->ms_sync_lock
);
1730 metaslab_load(metaslab_t
*msp
)
1732 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1735 * There may be another thread loading the same metaslab, if that's
1736 * the case just wait until the other thread is done and return.
1738 metaslab_load_wait(msp
);
1741 VERIFY(!msp
->ms_loading
);
1742 ASSERT(!msp
->ms_condensing
);
1744 msp
->ms_loading
= B_TRUE
;
1745 int error
= metaslab_load_impl(msp
);
1746 msp
->ms_loading
= B_FALSE
;
1747 cv_broadcast(&msp
->ms_load_cv
);
1753 metaslab_unload(metaslab_t
*msp
)
1755 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1757 metaslab_verify_weight_and_frag(msp
);
1759 range_tree_vacate(msp
->ms_allocatable
, NULL
, NULL
);
1760 msp
->ms_loaded
= B_FALSE
;
1762 msp
->ms_weight
&= ~METASLAB_ACTIVE_MASK
;
1763 msp
->ms_max_size
= 0;
1766 * We explicitly recalculate the metaslab's weight based on its space
1767 * map (as it is now not loaded). We want unload metaslabs to always
1768 * have their weights calculated from the space map histograms, while
1769 * loaded ones have it calculated from their in-core range tree
1770 * [see metaslab_load()]. This way, the weight reflects the information
1771 * available in-core, whether it is loaded or not
1773 * If ms_group == NULL means that we came here from metaslab_fini(),
1774 * at which point it doesn't make sense for us to do the recalculation
1777 if (msp
->ms_group
!= NULL
)
1778 metaslab_recalculate_weight_and_sort(msp
);
1782 metaslab_space_update(vdev_t
*vd
, metaslab_class_t
*mc
, int64_t alloc_delta
,
1783 int64_t defer_delta
, int64_t space_delta
)
1785 vdev_space_update(vd
, alloc_delta
, defer_delta
, space_delta
);
1787 ASSERT3P(vd
->vdev_spa
->spa_root_vdev
, ==, vd
->vdev_parent
);
1788 ASSERT(vd
->vdev_ms_count
!= 0);
1790 metaslab_class_space_update(mc
, alloc_delta
, defer_delta
, space_delta
,
1791 vdev_deflated_space(vd
, space_delta
));
1795 metaslab_init(metaslab_group_t
*mg
, uint64_t id
, uint64_t object
, uint64_t txg
,
1798 vdev_t
*vd
= mg
->mg_vd
;
1799 spa_t
*spa
= vd
->vdev_spa
;
1800 objset_t
*mos
= spa
->spa_meta_objset
;
1804 ms
= kmem_zalloc(sizeof (metaslab_t
), KM_SLEEP
);
1805 mutex_init(&ms
->ms_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1806 mutex_init(&ms
->ms_sync_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1807 cv_init(&ms
->ms_load_cv
, NULL
, CV_DEFAULT
, NULL
);
1810 ms
->ms_start
= id
<< vd
->vdev_ms_shift
;
1811 ms
->ms_size
= 1ULL << vd
->vdev_ms_shift
;
1812 ms
->ms_allocator
= -1;
1813 ms
->ms_new
= B_TRUE
;
1816 * We only open space map objects that already exist. All others
1817 * will be opened when we finally allocate an object for it.
1820 * When called from vdev_expand(), we can't call into the DMU as
1821 * we are holding the spa_config_lock as a writer and we would
1822 * deadlock [see relevant comment in vdev_metaslab_init()]. in
1823 * that case, the object parameter is zero though, so we won't
1824 * call into the DMU.
1827 error
= space_map_open(&ms
->ms_sm
, mos
, object
, ms
->ms_start
,
1828 ms
->ms_size
, vd
->vdev_ashift
);
1831 kmem_free(ms
, sizeof (metaslab_t
));
1835 ASSERT(ms
->ms_sm
!= NULL
);
1836 ms
->ms_allocated_space
= space_map_allocated(ms
->ms_sm
);
1840 * We create the ms_allocatable here, but we don't create the
1841 * other range trees until metaslab_sync_done(). This serves
1842 * two purposes: it allows metaslab_sync_done() to detect the
1843 * addition of new space; and for debugging, it ensures that
1844 * we'd data fault on any attempt to use this metaslab before
1847 ms
->ms_allocatable
= range_tree_create_impl(&rt_avl_ops
,
1848 &ms
->ms_allocatable_by_size
, metaslab_rangesize_compare
, 0);
1849 metaslab_group_add(mg
, ms
);
1851 metaslab_set_fragmentation(ms
);
1854 * If we're opening an existing pool (txg == 0) or creating
1855 * a new one (txg == TXG_INITIAL), all space is available now.
1856 * If we're adding space to an existing pool, the new space
1857 * does not become available until after this txg has synced.
1858 * The metaslab's weight will also be initialized when we sync
1859 * out this txg. This ensures that we don't attempt to allocate
1860 * from it before we have initialized it completely.
1862 if (txg
<= TXG_INITIAL
) {
1863 metaslab_sync_done(ms
, 0);
1864 metaslab_space_update(vd
, mg
->mg_class
,
1865 metaslab_allocated_space(ms
), 0, 0);
1869 * If metaslab_debug_load is set and we're initializing a metaslab
1870 * that has an allocated space map object then load the space map
1871 * so that we can verify frees.
1873 if (metaslab_debug_load
&& ms
->ms_sm
!= NULL
) {
1874 mutex_enter(&ms
->ms_lock
);
1875 VERIFY0(metaslab_load(ms
));
1876 mutex_exit(&ms
->ms_lock
);
1880 vdev_dirty(vd
, 0, NULL
, txg
);
1881 vdev_dirty(vd
, VDD_METASLAB
, ms
, txg
);
1890 metaslab_fini(metaslab_t
*msp
)
1892 metaslab_group_t
*mg
= msp
->ms_group
;
1893 vdev_t
*vd
= mg
->mg_vd
;
1895 metaslab_group_remove(mg
, msp
);
1897 mutex_enter(&msp
->ms_lock
);
1898 VERIFY(msp
->ms_group
== NULL
);
1899 metaslab_space_update(vd
, mg
->mg_class
,
1900 -metaslab_allocated_space(msp
), 0, -msp
->ms_size
);
1902 space_map_close(msp
->ms_sm
);
1904 metaslab_unload(msp
);
1906 range_tree_destroy(msp
->ms_allocatable
);
1907 range_tree_destroy(msp
->ms_freeing
);
1908 range_tree_destroy(msp
->ms_freed
);
1910 for (int t
= 0; t
< TXG_SIZE
; t
++) {
1911 range_tree_destroy(msp
->ms_allocating
[t
]);
1914 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1915 range_tree_destroy(msp
->ms_defer
[t
]);
1917 ASSERT0(msp
->ms_deferspace
);
1919 range_tree_destroy(msp
->ms_checkpointing
);
1921 for (int t
= 0; t
< TXG_SIZE
; t
++)
1922 ASSERT(!txg_list_member(&vd
->vdev_ms_list
, msp
, t
));
1924 mutex_exit(&msp
->ms_lock
);
1925 cv_destroy(&msp
->ms_load_cv
);
1926 mutex_destroy(&msp
->ms_lock
);
1927 mutex_destroy(&msp
->ms_sync_lock
);
1928 ASSERT3U(msp
->ms_allocator
, ==, -1);
1930 kmem_free(msp
, sizeof (metaslab_t
));
1933 #define FRAGMENTATION_TABLE_SIZE 17
1936 * This table defines a segment size based fragmentation metric that will
1937 * allow each metaslab to derive its own fragmentation value. This is done
1938 * by calculating the space in each bucket of the spacemap histogram and
1939 * multiplying that by the fragmentation metric in this table. Doing
1940 * this for all buckets and dividing it by the total amount of free
1941 * space in this metaslab (i.e. the total free space in all buckets) gives
1942 * us the fragmentation metric. This means that a high fragmentation metric
1943 * equates to most of the free space being comprised of small segments.
1944 * Conversely, if the metric is low, then most of the free space is in
1945 * large segments. A 10% change in fragmentation equates to approximately
1946 * double the number of segments.
1948 * This table defines 0% fragmented space using 16MB segments. Testing has
1949 * shown that segments that are greater than or equal to 16MB do not suffer
1950 * from drastic performance problems. Using this value, we derive the rest
1951 * of the table. Since the fragmentation value is never stored on disk, it
1952 * is possible to change these calculations in the future.
1954 int zfs_frag_table
[FRAGMENTATION_TABLE_SIZE
] = {
1974 * Calculate the metaslab's fragmentation metric and set ms_fragmentation.
1975 * Setting this value to ZFS_FRAG_INVALID means that the metaslab has not
1976 * been upgraded and does not support this metric. Otherwise, the return
1977 * value should be in the range [0, 100].
1980 metaslab_set_fragmentation(metaslab_t
*msp
)
1982 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
1983 uint64_t fragmentation
= 0;
1985 boolean_t feature_enabled
= spa_feature_is_enabled(spa
,
1986 SPA_FEATURE_SPACEMAP_HISTOGRAM
);
1988 if (!feature_enabled
) {
1989 msp
->ms_fragmentation
= ZFS_FRAG_INVALID
;
1994 * A null space map means that the entire metaslab is free
1995 * and thus is not fragmented.
1997 if (msp
->ms_sm
== NULL
) {
1998 msp
->ms_fragmentation
= 0;
2003 * If this metaslab's space map has not been upgraded, flag it
2004 * so that we upgrade next time we encounter it.
2006 if (msp
->ms_sm
->sm_dbuf
->db_size
!= sizeof (space_map_phys_t
)) {
2007 uint64_t txg
= spa_syncing_txg(spa
);
2008 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
2011 * If we've reached the final dirty txg, then we must
2012 * be shutting down the pool. We don't want to dirty
2013 * any data past this point so skip setting the condense
2014 * flag. We can retry this action the next time the pool
2017 if (spa_writeable(spa
) && txg
< spa_final_dirty_txg(spa
)) {
2018 msp
->ms_condense_wanted
= B_TRUE
;
2019 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
+ 1);
2020 zfs_dbgmsg("txg %llu, requesting force condense: "
2021 "ms_id %llu, vdev_id %llu", txg
, msp
->ms_id
,
2024 msp
->ms_fragmentation
= ZFS_FRAG_INVALID
;
2028 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++) {
2030 uint8_t shift
= msp
->ms_sm
->sm_shift
;
2032 int idx
= MIN(shift
- SPA_MINBLOCKSHIFT
+ i
,
2033 FRAGMENTATION_TABLE_SIZE
- 1);
2035 if (msp
->ms_sm
->sm_phys
->smp_histogram
[i
] == 0)
2038 space
= msp
->ms_sm
->sm_phys
->smp_histogram
[i
] << (i
+ shift
);
2041 ASSERT3U(idx
, <, FRAGMENTATION_TABLE_SIZE
);
2042 fragmentation
+= space
* zfs_frag_table
[idx
];
2046 fragmentation
/= total
;
2047 ASSERT3U(fragmentation
, <=, 100);
2049 msp
->ms_fragmentation
= fragmentation
;
2053 * Compute a weight -- a selection preference value -- for the given metaslab.
2054 * This is based on the amount of free space, the level of fragmentation,
2055 * the LBA range, and whether the metaslab is loaded.
2058 metaslab_space_weight(metaslab_t
*msp
)
2060 metaslab_group_t
*mg
= msp
->ms_group
;
2061 vdev_t
*vd
= mg
->mg_vd
;
2062 uint64_t weight
, space
;
2064 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2065 ASSERT(!vd
->vdev_removing
);
2068 * The baseline weight is the metaslab's free space.
2070 space
= msp
->ms_size
- metaslab_allocated_space(msp
);
2072 if (metaslab_fragmentation_factor_enabled
&&
2073 msp
->ms_fragmentation
!= ZFS_FRAG_INVALID
) {
2075 * Use the fragmentation information to inversely scale
2076 * down the baseline weight. We need to ensure that we
2077 * don't exclude this metaslab completely when it's 100%
2078 * fragmented. To avoid this we reduce the fragmented value
2081 space
= (space
* (100 - (msp
->ms_fragmentation
- 1))) / 100;
2084 * If space < SPA_MINBLOCKSIZE, then we will not allocate from
2085 * this metaslab again. The fragmentation metric may have
2086 * decreased the space to something smaller than
2087 * SPA_MINBLOCKSIZE, so reset the space to SPA_MINBLOCKSIZE
2088 * so that we can consume any remaining space.
2090 if (space
> 0 && space
< SPA_MINBLOCKSIZE
)
2091 space
= SPA_MINBLOCKSIZE
;
2096 * Modern disks have uniform bit density and constant angular velocity.
2097 * Therefore, the outer recording zones are faster (higher bandwidth)
2098 * than the inner zones by the ratio of outer to inner track diameter,
2099 * which is typically around 2:1. We account for this by assigning
2100 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
2101 * In effect, this means that we'll select the metaslab with the most
2102 * free bandwidth rather than simply the one with the most free space.
2104 if (!vd
->vdev_nonrot
&& metaslab_lba_weighting_enabled
) {
2105 weight
= 2 * weight
- (msp
->ms_id
* weight
) / vd
->vdev_ms_count
;
2106 ASSERT(weight
>= space
&& weight
<= 2 * space
);
2110 * If this metaslab is one we're actively using, adjust its
2111 * weight to make it preferable to any inactive metaslab so
2112 * we'll polish it off. If the fragmentation on this metaslab
2113 * has exceed our threshold, then don't mark it active.
2115 if (msp
->ms_loaded
&& msp
->ms_fragmentation
!= ZFS_FRAG_INVALID
&&
2116 msp
->ms_fragmentation
<= zfs_metaslab_fragmentation_threshold
) {
2117 weight
|= (msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
2120 WEIGHT_SET_SPACEBASED(weight
);
2125 * Return the weight of the specified metaslab, according to the segment-based
2126 * weighting algorithm. The metaslab must be loaded. This function can
2127 * be called within a sync pass since it relies only on the metaslab's
2128 * range tree which is always accurate when the metaslab is loaded.
2131 metaslab_weight_from_range_tree(metaslab_t
*msp
)
2133 uint64_t weight
= 0;
2134 uint32_t segments
= 0;
2136 ASSERT(msp
->ms_loaded
);
2138 for (int i
= RANGE_TREE_HISTOGRAM_SIZE
- 1; i
>= SPA_MINBLOCKSHIFT
;
2140 uint8_t shift
= msp
->ms_group
->mg_vd
->vdev_ashift
;
2141 int max_idx
= SPACE_MAP_HISTOGRAM_SIZE
+ shift
- 1;
2144 segments
+= msp
->ms_allocatable
->rt_histogram
[i
];
2147 * The range tree provides more precision than the space map
2148 * and must be downgraded so that all values fit within the
2149 * space map's histogram. This allows us to compare loaded
2150 * vs. unloaded metaslabs to determine which metaslab is
2151 * considered "best".
2156 if (segments
!= 0) {
2157 WEIGHT_SET_COUNT(weight
, segments
);
2158 WEIGHT_SET_INDEX(weight
, i
);
2159 WEIGHT_SET_ACTIVE(weight
, 0);
2167 * Calculate the weight based on the on-disk histogram. This should only
2168 * be called after a sync pass has completely finished since the on-disk
2169 * information is updated in metaslab_sync().
2172 metaslab_weight_from_spacemap(metaslab_t
*msp
)
2174 space_map_t
*sm
= msp
->ms_sm
;
2175 ASSERT(!msp
->ms_loaded
);
2177 ASSERT3U(space_map_object(sm
), !=, 0);
2178 ASSERT3U(sm
->sm_dbuf
->db_size
, ==, sizeof (space_map_phys_t
));
2181 * Create a joint histogram from all the segments that have made
2182 * it to the metaslab's space map histogram, that are not yet
2183 * available for allocation because they are still in the freeing
2184 * pipeline (e.g. freeing, freed, and defer trees). Then subtract
2185 * these segments from the space map's histogram to get a more
2188 uint64_t deferspace_histogram
[SPACE_MAP_HISTOGRAM_SIZE
] = {0};
2189 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++)
2190 deferspace_histogram
[i
] += msp
->ms_synchist
[i
];
2191 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
2192 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++) {
2193 deferspace_histogram
[i
] += msp
->ms_deferhist
[t
][i
];
2197 uint64_t weight
= 0;
2198 for (int i
= SPACE_MAP_HISTOGRAM_SIZE
- 1; i
>= 0; i
--) {
2199 ASSERT3U(sm
->sm_phys
->smp_histogram
[i
], >=,
2200 deferspace_histogram
[i
]);
2202 sm
->sm_phys
->smp_histogram
[i
] - deferspace_histogram
[i
];
2204 WEIGHT_SET_COUNT(weight
, count
);
2205 WEIGHT_SET_INDEX(weight
, i
+ sm
->sm_shift
);
2206 WEIGHT_SET_ACTIVE(weight
, 0);
2214 * Compute a segment-based weight for the specified metaslab. The weight
2215 * is determined by highest bucket in the histogram. The information
2216 * for the highest bucket is encoded into the weight value.
2219 metaslab_segment_weight(metaslab_t
*msp
)
2221 metaslab_group_t
*mg
= msp
->ms_group
;
2222 uint64_t weight
= 0;
2223 uint8_t shift
= mg
->mg_vd
->vdev_ashift
;
2225 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2228 * The metaslab is completely free.
2230 if (metaslab_allocated_space(msp
) == 0) {
2231 int idx
= highbit64(msp
->ms_size
) - 1;
2232 int max_idx
= SPACE_MAP_HISTOGRAM_SIZE
+ shift
- 1;
2234 if (idx
< max_idx
) {
2235 WEIGHT_SET_COUNT(weight
, 1ULL);
2236 WEIGHT_SET_INDEX(weight
, idx
);
2238 WEIGHT_SET_COUNT(weight
, 1ULL << (idx
- max_idx
));
2239 WEIGHT_SET_INDEX(weight
, max_idx
);
2241 WEIGHT_SET_ACTIVE(weight
, 0);
2242 ASSERT(!WEIGHT_IS_SPACEBASED(weight
));
2247 ASSERT3U(msp
->ms_sm
->sm_dbuf
->db_size
, ==, sizeof (space_map_phys_t
));
2250 * If the metaslab is fully allocated then just make the weight 0.
2252 if (metaslab_allocated_space(msp
) == msp
->ms_size
)
2255 * If the metaslab is already loaded, then use the range tree to
2256 * determine the weight. Otherwise, we rely on the space map information
2257 * to generate the weight.
2259 if (msp
->ms_loaded
) {
2260 weight
= metaslab_weight_from_range_tree(msp
);
2262 weight
= metaslab_weight_from_spacemap(msp
);
2266 * If the metaslab was active the last time we calculated its weight
2267 * then keep it active. We want to consume the entire region that
2268 * is associated with this weight.
2270 if (msp
->ms_activation_weight
!= 0 && weight
!= 0)
2271 WEIGHT_SET_ACTIVE(weight
, WEIGHT_GET_ACTIVE(msp
->ms_weight
));
2276 * Determine if we should attempt to allocate from this metaslab. If the
2277 * metaslab has a maximum size then we can quickly determine if the desired
2278 * allocation size can be satisfied. Otherwise, if we're using segment-based
2279 * weighting then we can determine the maximum allocation that this metaslab
2280 * can accommodate based on the index encoded in the weight. If we're using
2281 * space-based weights then rely on the entire weight (excluding the weight
2285 metaslab_should_allocate(metaslab_t
*msp
, uint64_t asize
)
2287 boolean_t should_allocate
;
2289 if (msp
->ms_max_size
!= 0)
2290 return (msp
->ms_max_size
>= asize
);
2292 if (!WEIGHT_IS_SPACEBASED(msp
->ms_weight
)) {
2294 * The metaslab segment weight indicates segments in the
2295 * range [2^i, 2^(i+1)), where i is the index in the weight.
2296 * Since the asize might be in the middle of the range, we
2297 * should attempt the allocation if asize < 2^(i+1).
2299 should_allocate
= (asize
<
2300 1ULL << (WEIGHT_GET_INDEX(msp
->ms_weight
) + 1));
2302 should_allocate
= (asize
<=
2303 (msp
->ms_weight
& ~METASLAB_WEIGHT_TYPE
));
2305 return (should_allocate
);
2308 metaslab_weight(metaslab_t
*msp
)
2310 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
2311 spa_t
*spa
= vd
->vdev_spa
;
2314 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2317 * If this vdev is in the process of being removed, there is nothing
2318 * for us to do here.
2320 if (vd
->vdev_removing
)
2323 metaslab_set_fragmentation(msp
);
2326 * Update the maximum size if the metaslab is loaded. This will
2327 * ensure that we get an accurate maximum size if newly freed space
2328 * has been added back into the free tree.
2331 msp
->ms_max_size
= metaslab_block_maxsize(msp
);
2333 ASSERT0(msp
->ms_max_size
);
2336 * Segment-based weighting requires space map histogram support.
2338 if (zfs_metaslab_segment_weight_enabled
&&
2339 spa_feature_is_enabled(spa
, SPA_FEATURE_SPACEMAP_HISTOGRAM
) &&
2340 (msp
->ms_sm
== NULL
|| msp
->ms_sm
->sm_dbuf
->db_size
==
2341 sizeof (space_map_phys_t
))) {
2342 weight
= metaslab_segment_weight(msp
);
2344 weight
= metaslab_space_weight(msp
);
2350 metaslab_recalculate_weight_and_sort(metaslab_t
*msp
)
2352 /* note: we preserve the mask (e.g. indication of primary, etc..) */
2353 uint64_t was_active
= msp
->ms_weight
& METASLAB_ACTIVE_MASK
;
2354 metaslab_group_sort(msp
->ms_group
, msp
,
2355 metaslab_weight(msp
) | was_active
);
2359 metaslab_activate_allocator(metaslab_group_t
*mg
, metaslab_t
*msp
,
2360 int allocator
, uint64_t activation_weight
)
2363 * If we're activating for the claim code, we don't want to actually
2364 * set the metaslab up for a specific allocator.
2366 if (activation_weight
== METASLAB_WEIGHT_CLAIM
)
2368 metaslab_t
**arr
= (activation_weight
== METASLAB_WEIGHT_PRIMARY
?
2369 mg
->mg_primaries
: mg
->mg_secondaries
);
2371 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2372 mutex_enter(&mg
->mg_lock
);
2373 if (arr
[allocator
] != NULL
) {
2374 mutex_exit(&mg
->mg_lock
);
2378 arr
[allocator
] = msp
;
2379 ASSERT3S(msp
->ms_allocator
, ==, -1);
2380 msp
->ms_allocator
= allocator
;
2381 msp
->ms_primary
= (activation_weight
== METASLAB_WEIGHT_PRIMARY
);
2382 mutex_exit(&mg
->mg_lock
);
2388 metaslab_activate(metaslab_t
*msp
, int allocator
, uint64_t activation_weight
)
2390 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2392 if ((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0) {
2393 int error
= metaslab_load(msp
);
2395 metaslab_group_sort(msp
->ms_group
, msp
, 0);
2398 if ((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) != 0) {
2400 * The metaslab was activated for another allocator
2401 * while we were waiting, we should reselect.
2403 return (SET_ERROR(EBUSY
));
2405 if ((error
= metaslab_activate_allocator(msp
->ms_group
, msp
,
2406 allocator
, activation_weight
)) != 0) {
2410 msp
->ms_activation_weight
= msp
->ms_weight
;
2411 metaslab_group_sort(msp
->ms_group
, msp
,
2412 msp
->ms_weight
| activation_weight
);
2414 ASSERT(msp
->ms_loaded
);
2415 ASSERT(msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
2421 metaslab_passivate_allocator(metaslab_group_t
*mg
, metaslab_t
*msp
,
2424 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2425 if (msp
->ms_weight
& METASLAB_WEIGHT_CLAIM
) {
2426 metaslab_group_sort(mg
, msp
, weight
);
2430 mutex_enter(&mg
->mg_lock
);
2431 ASSERT3P(msp
->ms_group
, ==, mg
);
2432 if (msp
->ms_primary
) {
2433 ASSERT3U(0, <=, msp
->ms_allocator
);
2434 ASSERT3U(msp
->ms_allocator
, <, mg
->mg_allocators
);
2435 ASSERT3P(mg
->mg_primaries
[msp
->ms_allocator
], ==, msp
);
2436 ASSERT(msp
->ms_weight
& METASLAB_WEIGHT_PRIMARY
);
2437 mg
->mg_primaries
[msp
->ms_allocator
] = NULL
;
2439 ASSERT(msp
->ms_weight
& METASLAB_WEIGHT_SECONDARY
);
2440 ASSERT3P(mg
->mg_secondaries
[msp
->ms_allocator
], ==, msp
);
2441 mg
->mg_secondaries
[msp
->ms_allocator
] = NULL
;
2443 msp
->ms_allocator
= -1;
2444 metaslab_group_sort_impl(mg
, msp
, weight
);
2445 mutex_exit(&mg
->mg_lock
);
2449 metaslab_passivate(metaslab_t
*msp
, uint64_t weight
)
2451 ASSERTV(uint64_t size
= weight
& ~METASLAB_WEIGHT_TYPE
);
2454 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
2455 * this metaslab again. In that case, it had better be empty,
2456 * or we would be leaving space on the table.
2458 ASSERT(!WEIGHT_IS_SPACEBASED(msp
->ms_weight
) ||
2459 size
>= SPA_MINBLOCKSIZE
||
2460 range_tree_space(msp
->ms_allocatable
) == 0);
2461 ASSERT0(weight
& METASLAB_ACTIVE_MASK
);
2463 msp
->ms_activation_weight
= 0;
2464 metaslab_passivate_allocator(msp
->ms_group
, msp
, weight
);
2465 ASSERT((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0);
2469 * Segment-based metaslabs are activated once and remain active until
2470 * we either fail an allocation attempt (similar to space-based metaslabs)
2471 * or have exhausted the free space in zfs_metaslab_switch_threshold
2472 * buckets since the metaslab was activated. This function checks to see
2473 * if we've exhaused the zfs_metaslab_switch_threshold buckets in the
2474 * metaslab and passivates it proactively. This will allow us to select a
2475 * metaslab with a larger contiguous region, if any, remaining within this
2476 * metaslab group. If we're in sync pass > 1, then we continue using this
2477 * metaslab so that we don't dirty more block and cause more sync passes.
2480 metaslab_segment_may_passivate(metaslab_t
*msp
)
2482 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
2484 if (WEIGHT_IS_SPACEBASED(msp
->ms_weight
) || spa_sync_pass(spa
) > 1)
2488 * Since we are in the middle of a sync pass, the most accurate
2489 * information that is accessible to us is the in-core range tree
2490 * histogram; calculate the new weight based on that information.
2492 uint64_t weight
= metaslab_weight_from_range_tree(msp
);
2493 int activation_idx
= WEIGHT_GET_INDEX(msp
->ms_activation_weight
);
2494 int current_idx
= WEIGHT_GET_INDEX(weight
);
2496 if (current_idx
<= activation_idx
- zfs_metaslab_switch_threshold
)
2497 metaslab_passivate(msp
, weight
);
2501 metaslab_preload(void *arg
)
2503 metaslab_t
*msp
= arg
;
2504 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
2505 fstrans_cookie_t cookie
= spl_fstrans_mark();
2507 ASSERT(!MUTEX_HELD(&msp
->ms_group
->mg_lock
));
2509 mutex_enter(&msp
->ms_lock
);
2510 (void) metaslab_load(msp
);
2511 msp
->ms_selected_txg
= spa_syncing_txg(spa
);
2512 mutex_exit(&msp
->ms_lock
);
2513 spl_fstrans_unmark(cookie
);
2517 metaslab_group_preload(metaslab_group_t
*mg
)
2519 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
2521 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
2524 if (spa_shutting_down(spa
) || !metaslab_preload_enabled
) {
2525 taskq_wait_outstanding(mg
->mg_taskq
, 0);
2529 mutex_enter(&mg
->mg_lock
);
2532 * Load the next potential metaslabs
2534 for (msp
= avl_first(t
); msp
!= NULL
; msp
= AVL_NEXT(t
, msp
)) {
2535 ASSERT3P(msp
->ms_group
, ==, mg
);
2538 * We preload only the maximum number of metaslabs specified
2539 * by metaslab_preload_limit. If a metaslab is being forced
2540 * to condense then we preload it too. This will ensure
2541 * that force condensing happens in the next txg.
2543 if (++m
> metaslab_preload_limit
&& !msp
->ms_condense_wanted
) {
2547 VERIFY(taskq_dispatch(mg
->mg_taskq
, metaslab_preload
,
2548 msp
, TQ_SLEEP
) != TASKQID_INVALID
);
2550 mutex_exit(&mg
->mg_lock
);
2554 * Determine if the space map's on-disk footprint is past our tolerance
2555 * for inefficiency. We would like to use the following criteria to make
2558 * 1. The size of the space map object should not dramatically increase as a
2559 * result of writing out the free space range tree.
2561 * 2. The minimal on-disk space map representation is zfs_condense_pct/100
2562 * times the size than the free space range tree representation
2563 * (i.e. zfs_condense_pct = 110 and in-core = 1MB, minimal = 1.1MB).
2565 * 3. The on-disk size of the space map should actually decrease.
2567 * Unfortunately, we cannot compute the on-disk size of the space map in this
2568 * context because we cannot accurately compute the effects of compression, etc.
2569 * Instead, we apply the heuristic described in the block comment for
2570 * zfs_metaslab_condense_block_threshold - we only condense if the space used
2571 * is greater than a threshold number of blocks.
2574 metaslab_should_condense(metaslab_t
*msp
)
2576 space_map_t
*sm
= msp
->ms_sm
;
2577 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
2578 uint64_t vdev_blocksize
= 1 << vd
->vdev_ashift
;
2579 uint64_t current_txg
= spa_syncing_txg(vd
->vdev_spa
);
2581 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2582 ASSERT(msp
->ms_loaded
);
2585 * Allocations and frees in early passes are generally more space
2586 * efficient (in terms of blocks described in space map entries)
2587 * than the ones in later passes (e.g. we don't compress after
2588 * sync pass 5) and condensing a metaslab multiple times in a txg
2589 * could degrade performance.
2591 * Thus we prefer condensing each metaslab at most once every txg at
2592 * the earliest sync pass possible. If a metaslab is eligible for
2593 * condensing again after being considered for condensing within the
2594 * same txg, it will hopefully be dirty in the next txg where it will
2595 * be condensed at an earlier pass.
2597 if (msp
->ms_condense_checked_txg
== current_txg
)
2599 msp
->ms_condense_checked_txg
= current_txg
;
2602 * We always condense metaslabs that are empty and metaslabs for
2603 * which a condense request has been made.
2605 if (avl_is_empty(&msp
->ms_allocatable_by_size
) ||
2606 msp
->ms_condense_wanted
)
2609 uint64_t object_size
= space_map_length(msp
->ms_sm
);
2610 uint64_t optimal_size
= space_map_estimate_optimal_size(sm
,
2611 msp
->ms_allocatable
, SM_NO_VDEVID
);
2613 dmu_object_info_t doi
;
2614 dmu_object_info_from_db(sm
->sm_dbuf
, &doi
);
2615 uint64_t record_size
= MAX(doi
.doi_data_block_size
, vdev_blocksize
);
2617 return (object_size
>= (optimal_size
* zfs_condense_pct
/ 100) &&
2618 object_size
> zfs_metaslab_condense_block_threshold
* record_size
);
2622 * Condense the on-disk space map representation to its minimized form.
2623 * The minimized form consists of a small number of allocations followed by
2624 * the entries of the free range tree.
2627 metaslab_condense(metaslab_t
*msp
, uint64_t txg
, dmu_tx_t
*tx
)
2629 range_tree_t
*condense_tree
;
2630 space_map_t
*sm
= msp
->ms_sm
;
2632 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2633 ASSERT(msp
->ms_loaded
);
2636 zfs_dbgmsg("condensing: txg %llu, msp[%llu] %p, vdev id %llu, "
2637 "spa %s, smp size %llu, segments %lu, forcing condense=%s", txg
,
2638 msp
->ms_id
, msp
, msp
->ms_group
->mg_vd
->vdev_id
,
2639 msp
->ms_group
->mg_vd
->vdev_spa
->spa_name
,
2640 space_map_length(msp
->ms_sm
),
2641 avl_numnodes(&msp
->ms_allocatable
->rt_root
),
2642 msp
->ms_condense_wanted
? "TRUE" : "FALSE");
2644 msp
->ms_condense_wanted
= B_FALSE
;
2647 * Create an range tree that is 100% allocated. We remove segments
2648 * that have been freed in this txg, any deferred frees that exist,
2649 * and any allocation in the future. Removing segments should be
2650 * a relatively inexpensive operation since we expect these trees to
2651 * have a small number of nodes.
2653 condense_tree
= range_tree_create(NULL
, NULL
);
2654 range_tree_add(condense_tree
, msp
->ms_start
, msp
->ms_size
);
2656 range_tree_walk(msp
->ms_freeing
, range_tree_remove
, condense_tree
);
2657 range_tree_walk(msp
->ms_freed
, range_tree_remove
, condense_tree
);
2659 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
2660 range_tree_walk(msp
->ms_defer
[t
],
2661 range_tree_remove
, condense_tree
);
2664 for (int t
= 1; t
< TXG_CONCURRENT_STATES
; t
++) {
2665 range_tree_walk(msp
->ms_allocating
[(txg
+ t
) & TXG_MASK
],
2666 range_tree_remove
, condense_tree
);
2670 * We're about to drop the metaslab's lock thus allowing
2671 * other consumers to change it's content. Set the
2672 * metaslab's ms_condensing flag to ensure that
2673 * allocations on this metaslab do not occur while we're
2674 * in the middle of committing it to disk. This is only critical
2675 * for ms_allocatable as all other range trees use per txg
2676 * views of their content.
2678 msp
->ms_condensing
= B_TRUE
;
2680 mutex_exit(&msp
->ms_lock
);
2681 space_map_truncate(sm
, zfs_metaslab_sm_blksz
, tx
);
2684 * While we would ideally like to create a space map representation
2685 * that consists only of allocation records, doing so can be
2686 * prohibitively expensive because the in-core free tree can be
2687 * large, and therefore computationally expensive to subtract
2688 * from the condense_tree. Instead we sync out two trees, a cheap
2689 * allocation only tree followed by the in-core free tree. While not
2690 * optimal, this is typically close to optimal, and much cheaper to
2693 space_map_write(sm
, condense_tree
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
2694 range_tree_vacate(condense_tree
, NULL
, NULL
);
2695 range_tree_destroy(condense_tree
);
2697 space_map_write(sm
, msp
->ms_allocatable
, SM_FREE
, SM_NO_VDEVID
, tx
);
2698 mutex_enter(&msp
->ms_lock
);
2699 msp
->ms_condensing
= B_FALSE
;
2703 * Write a metaslab to disk in the context of the specified transaction group.
2706 metaslab_sync(metaslab_t
*msp
, uint64_t txg
)
2708 metaslab_group_t
*mg
= msp
->ms_group
;
2709 vdev_t
*vd
= mg
->mg_vd
;
2710 spa_t
*spa
= vd
->vdev_spa
;
2711 objset_t
*mos
= spa_meta_objset(spa
);
2712 range_tree_t
*alloctree
= msp
->ms_allocating
[txg
& TXG_MASK
];
2714 uint64_t object
= space_map_object(msp
->ms_sm
);
2716 ASSERT(!vd
->vdev_ishole
);
2719 * This metaslab has just been added so there's no work to do now.
2721 if (msp
->ms_freeing
== NULL
) {
2722 ASSERT3P(alloctree
, ==, NULL
);
2726 ASSERT3P(alloctree
, !=, NULL
);
2727 ASSERT3P(msp
->ms_freeing
, !=, NULL
);
2728 ASSERT3P(msp
->ms_freed
, !=, NULL
);
2729 ASSERT3P(msp
->ms_checkpointing
, !=, NULL
);
2732 * Normally, we don't want to process a metaslab if there are no
2733 * allocations or frees to perform. However, if the metaslab is being
2734 * forced to condense and it's loaded, we need to let it through.
2736 if (range_tree_is_empty(alloctree
) &&
2737 range_tree_is_empty(msp
->ms_freeing
) &&
2738 range_tree_is_empty(msp
->ms_checkpointing
) &&
2739 !(msp
->ms_loaded
&& msp
->ms_condense_wanted
))
2743 VERIFY(txg
<= spa_final_dirty_txg(spa
));
2746 * The only state that can actually be changing concurrently
2747 * with metaslab_sync() is the metaslab's ms_allocatable. No
2748 * other thread can be modifying this txg's alloc, freeing,
2749 * freed, or space_map_phys_t. We drop ms_lock whenever we
2750 * could call into the DMU, because the DMU can call down to
2751 * us (e.g. via zio_free()) at any time.
2753 * The spa_vdev_remove_thread() can be reading metaslab state
2754 * concurrently, and it is locked out by the ms_sync_lock.
2755 * Note that the ms_lock is insufficient for this, because it
2756 * is dropped by space_map_write().
2758 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2760 if (msp
->ms_sm
== NULL
) {
2761 uint64_t new_object
;
2763 new_object
= space_map_alloc(mos
, zfs_metaslab_sm_blksz
, tx
);
2764 VERIFY3U(new_object
, !=, 0);
2766 VERIFY0(space_map_open(&msp
->ms_sm
, mos
, new_object
,
2767 msp
->ms_start
, msp
->ms_size
, vd
->vdev_ashift
));
2769 ASSERT(msp
->ms_sm
!= NULL
);
2770 ASSERT0(metaslab_allocated_space(msp
));
2773 if (!range_tree_is_empty(msp
->ms_checkpointing
) &&
2774 vd
->vdev_checkpoint_sm
== NULL
) {
2775 ASSERT(spa_has_checkpoint(spa
));
2777 uint64_t new_object
= space_map_alloc(mos
,
2778 vdev_standard_sm_blksz
, tx
);
2779 VERIFY3U(new_object
, !=, 0);
2781 VERIFY0(space_map_open(&vd
->vdev_checkpoint_sm
,
2782 mos
, new_object
, 0, vd
->vdev_asize
, vd
->vdev_ashift
));
2783 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
2786 * We save the space map object as an entry in vdev_top_zap
2787 * so it can be retrieved when the pool is reopened after an
2788 * export or through zdb.
2790 VERIFY0(zap_add(vd
->vdev_spa
->spa_meta_objset
,
2791 vd
->vdev_top_zap
, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
,
2792 sizeof (new_object
), 1, &new_object
, tx
));
2795 mutex_enter(&msp
->ms_sync_lock
);
2796 mutex_enter(&msp
->ms_lock
);
2799 * Note: metaslab_condense() clears the space map's histogram.
2800 * Therefore we must verify and remove this histogram before
2803 metaslab_group_histogram_verify(mg
);
2804 metaslab_class_histogram_verify(mg
->mg_class
);
2805 metaslab_group_histogram_remove(mg
, msp
);
2807 if (msp
->ms_loaded
&& metaslab_should_condense(msp
)) {
2808 metaslab_condense(msp
, txg
, tx
);
2810 mutex_exit(&msp
->ms_lock
);
2811 space_map_write(msp
->ms_sm
, alloctree
, SM_ALLOC
,
2813 space_map_write(msp
->ms_sm
, msp
->ms_freeing
, SM_FREE
,
2815 mutex_enter(&msp
->ms_lock
);
2818 msp
->ms_allocated_space
+= range_tree_space(alloctree
);
2819 ASSERT3U(msp
->ms_allocated_space
, >=,
2820 range_tree_space(msp
->ms_freeing
));
2821 msp
->ms_allocated_space
-= range_tree_space(msp
->ms_freeing
);
2823 if (!range_tree_is_empty(msp
->ms_checkpointing
)) {
2824 ASSERT(spa_has_checkpoint(spa
));
2825 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
2828 * Since we are doing writes to disk and the ms_checkpointing
2829 * tree won't be changing during that time, we drop the
2830 * ms_lock while writing to the checkpoint space map.
2832 mutex_exit(&msp
->ms_lock
);
2833 space_map_write(vd
->vdev_checkpoint_sm
,
2834 msp
->ms_checkpointing
, SM_FREE
, SM_NO_VDEVID
, tx
);
2835 mutex_enter(&msp
->ms_lock
);
2837 spa
->spa_checkpoint_info
.sci_dspace
+=
2838 range_tree_space(msp
->ms_checkpointing
);
2839 vd
->vdev_stat
.vs_checkpoint_space
+=
2840 range_tree_space(msp
->ms_checkpointing
);
2841 ASSERT3U(vd
->vdev_stat
.vs_checkpoint_space
, ==,
2842 -space_map_allocated(vd
->vdev_checkpoint_sm
));
2844 range_tree_vacate(msp
->ms_checkpointing
, NULL
, NULL
);
2847 if (msp
->ms_loaded
) {
2849 * When the space map is loaded, we have an accurate
2850 * histogram in the range tree. This gives us an opportunity
2851 * to bring the space map's histogram up-to-date so we clear
2852 * it first before updating it.
2854 space_map_histogram_clear(msp
->ms_sm
);
2855 space_map_histogram_add(msp
->ms_sm
, msp
->ms_allocatable
, tx
);
2858 * Since we've cleared the histogram we need to add back
2859 * any free space that has already been processed, plus
2860 * any deferred space. This allows the on-disk histogram
2861 * to accurately reflect all free space even if some space
2862 * is not yet available for allocation (i.e. deferred).
2864 space_map_histogram_add(msp
->ms_sm
, msp
->ms_freed
, tx
);
2867 * Add back any deferred free space that has not been
2868 * added back into the in-core free tree yet. This will
2869 * ensure that we don't end up with a space map histogram
2870 * that is completely empty unless the metaslab is fully
2873 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
2874 space_map_histogram_add(msp
->ms_sm
,
2875 msp
->ms_defer
[t
], tx
);
2880 * Always add the free space from this sync pass to the space
2881 * map histogram. We want to make sure that the on-disk histogram
2882 * accounts for all free space. If the space map is not loaded,
2883 * then we will lose some accuracy but will correct it the next
2884 * time we load the space map.
2886 space_map_histogram_add(msp
->ms_sm
, msp
->ms_freeing
, tx
);
2887 metaslab_aux_histograms_update(msp
);
2889 metaslab_group_histogram_add(mg
, msp
);
2890 metaslab_group_histogram_verify(mg
);
2891 metaslab_class_histogram_verify(mg
->mg_class
);
2894 * For sync pass 1, we avoid traversing this txg's free range tree
2895 * and instead will just swap the pointers for freeing and freed.
2896 * We can safely do this since the freed_tree is guaranteed to be
2897 * empty on the initial pass.
2899 if (spa_sync_pass(spa
) == 1) {
2900 range_tree_swap(&msp
->ms_freeing
, &msp
->ms_freed
);
2901 ASSERT0(msp
->ms_allocated_this_txg
);
2903 range_tree_vacate(msp
->ms_freeing
,
2904 range_tree_add
, msp
->ms_freed
);
2906 msp
->ms_allocated_this_txg
+= range_tree_space(alloctree
);
2907 range_tree_vacate(alloctree
, NULL
, NULL
);
2909 ASSERT0(range_tree_space(msp
->ms_allocating
[txg
& TXG_MASK
]));
2910 ASSERT0(range_tree_space(msp
->ms_allocating
[TXG_CLEAN(txg
)
2912 ASSERT0(range_tree_space(msp
->ms_freeing
));
2913 ASSERT0(range_tree_space(msp
->ms_checkpointing
));
2915 mutex_exit(&msp
->ms_lock
);
2917 if (object
!= space_map_object(msp
->ms_sm
)) {
2918 object
= space_map_object(msp
->ms_sm
);
2919 dmu_write(mos
, vd
->vdev_ms_array
, sizeof (uint64_t) *
2920 msp
->ms_id
, sizeof (uint64_t), &object
, tx
);
2922 mutex_exit(&msp
->ms_sync_lock
);
2927 * Called after a transaction group has completely synced to mark
2928 * all of the metaslab's free space as usable.
2931 metaslab_sync_done(metaslab_t
*msp
, uint64_t txg
)
2933 metaslab_group_t
*mg
= msp
->ms_group
;
2934 vdev_t
*vd
= mg
->mg_vd
;
2935 spa_t
*spa
= vd
->vdev_spa
;
2936 range_tree_t
**defer_tree
;
2937 int64_t alloc_delta
, defer_delta
;
2938 boolean_t defer_allowed
= B_TRUE
;
2940 ASSERT(!vd
->vdev_ishole
);
2942 mutex_enter(&msp
->ms_lock
);
2945 * If this metaslab is just becoming available, initialize its
2946 * range trees and add its capacity to the vdev.
2948 if (msp
->ms_freed
== NULL
) {
2949 for (int t
= 0; t
< TXG_SIZE
; t
++) {
2950 ASSERT(msp
->ms_allocating
[t
] == NULL
);
2952 msp
->ms_allocating
[t
] = range_tree_create(NULL
, NULL
);
2955 ASSERT3P(msp
->ms_freeing
, ==, NULL
);
2956 msp
->ms_freeing
= range_tree_create(NULL
, NULL
);
2958 ASSERT3P(msp
->ms_freed
, ==, NULL
);
2959 msp
->ms_freed
= range_tree_create(NULL
, NULL
);
2961 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
2962 ASSERT(msp
->ms_defer
[t
] == NULL
);
2964 msp
->ms_defer
[t
] = range_tree_create(NULL
, NULL
);
2967 ASSERT3P(msp
->ms_checkpointing
, ==, NULL
);
2968 msp
->ms_checkpointing
= range_tree_create(NULL
, NULL
);
2970 metaslab_space_update(vd
, mg
->mg_class
, 0, 0, msp
->ms_size
);
2972 ASSERT0(range_tree_space(msp
->ms_freeing
));
2973 ASSERT0(range_tree_space(msp
->ms_checkpointing
));
2975 defer_tree
= &msp
->ms_defer
[txg
% TXG_DEFER_SIZE
];
2977 uint64_t free_space
= metaslab_class_get_space(spa_normal_class(spa
)) -
2978 metaslab_class_get_alloc(spa_normal_class(spa
));
2979 if (free_space
<= spa_get_slop_space(spa
) || vd
->vdev_removing
) {
2980 defer_allowed
= B_FALSE
;
2984 alloc_delta
= msp
->ms_allocated_this_txg
-
2985 range_tree_space(msp
->ms_freed
);
2986 if (defer_allowed
) {
2987 defer_delta
= range_tree_space(msp
->ms_freed
) -
2988 range_tree_space(*defer_tree
);
2990 defer_delta
-= range_tree_space(*defer_tree
);
2993 metaslab_space_update(vd
, mg
->mg_class
, alloc_delta
+ defer_delta
,
2997 * If there's a metaslab_load() in progress, wait for it to complete
2998 * so that we have a consistent view of the in-core space map.
3000 metaslab_load_wait(msp
);
3003 * Move the frees from the defer_tree back to the free
3004 * range tree (if it's loaded). Swap the freed_tree and
3005 * the defer_tree -- this is safe to do because we've
3006 * just emptied out the defer_tree.
3008 range_tree_vacate(*defer_tree
,
3009 msp
->ms_loaded
? range_tree_add
: NULL
, msp
->ms_allocatable
);
3010 if (defer_allowed
) {
3011 range_tree_swap(&msp
->ms_freed
, defer_tree
);
3013 range_tree_vacate(msp
->ms_freed
,
3014 msp
->ms_loaded
? range_tree_add
: NULL
,
3015 msp
->ms_allocatable
);
3018 msp
->ms_synced_length
= space_map_length(msp
->ms_sm
);
3020 msp
->ms_deferspace
+= defer_delta
;
3021 ASSERT3S(msp
->ms_deferspace
, >=, 0);
3022 ASSERT3S(msp
->ms_deferspace
, <=, msp
->ms_size
);
3023 if (msp
->ms_deferspace
!= 0) {
3025 * Keep syncing this metaslab until all deferred frees
3026 * are back in circulation.
3028 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
+ 1);
3030 metaslab_aux_histograms_update_done(msp
, defer_allowed
);
3033 msp
->ms_new
= B_FALSE
;
3034 mutex_enter(&mg
->mg_lock
);
3036 mutex_exit(&mg
->mg_lock
);
3040 * Re-sort metaslab within its group now that we've adjusted
3041 * its allocatable space.
3043 metaslab_recalculate_weight_and_sort(msp
);
3046 * If the metaslab is loaded and we've not tried to load or allocate
3047 * from it in 'metaslab_unload_delay' txgs, then unload it.
3049 if (msp
->ms_loaded
&&
3050 msp
->ms_initializing
== 0 &&
3051 msp
->ms_selected_txg
+ metaslab_unload_delay
< txg
) {
3053 for (int t
= 1; t
< TXG_CONCURRENT_STATES
; t
++) {
3054 VERIFY0(range_tree_space(
3055 msp
->ms_allocating
[(txg
+ t
) & TXG_MASK
]));
3057 if (msp
->ms_allocator
!= -1) {
3058 metaslab_passivate(msp
, msp
->ms_weight
&
3059 ~METASLAB_ACTIVE_MASK
);
3062 if (!metaslab_debug_unload
)
3063 metaslab_unload(msp
);
3066 ASSERT0(range_tree_space(msp
->ms_allocating
[txg
& TXG_MASK
]));
3067 ASSERT0(range_tree_space(msp
->ms_freeing
));
3068 ASSERT0(range_tree_space(msp
->ms_freed
));
3069 ASSERT0(range_tree_space(msp
->ms_checkpointing
));
3071 msp
->ms_allocated_this_txg
= 0;
3072 mutex_exit(&msp
->ms_lock
);
3076 metaslab_sync_reassess(metaslab_group_t
*mg
)
3078 spa_t
*spa
= mg
->mg_class
->mc_spa
;
3080 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
3081 metaslab_group_alloc_update(mg
);
3082 mg
->mg_fragmentation
= metaslab_group_fragmentation(mg
);
3085 * Preload the next potential metaslabs but only on active
3086 * metaslab groups. We can get into a state where the metaslab
3087 * is no longer active since we dirty metaslabs as we remove a
3088 * a device, thus potentially making the metaslab group eligible
3091 if (mg
->mg_activation_count
> 0) {
3092 metaslab_group_preload(mg
);
3094 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
3098 * When writing a ditto block (i.e. more than one DVA for a given BP) on
3099 * the same vdev as an existing DVA of this BP, then try to allocate it
3100 * on a different metaslab than existing DVAs (i.e. a unique metaslab).
3103 metaslab_is_unique(metaslab_t
*msp
, dva_t
*dva
)
3107 if (DVA_GET_ASIZE(dva
) == 0)
3110 if (msp
->ms_group
->mg_vd
->vdev_id
!= DVA_GET_VDEV(dva
))
3113 dva_ms_id
= DVA_GET_OFFSET(dva
) >> msp
->ms_group
->mg_vd
->vdev_ms_shift
;
3115 return (msp
->ms_id
!= dva_ms_id
);
3119 * ==========================================================================
3120 * Metaslab allocation tracing facility
3121 * ==========================================================================
3123 #ifdef _METASLAB_TRACING
3124 kstat_t
*metaslab_trace_ksp
;
3125 kstat_named_t metaslab_trace_over_limit
;
3128 metaslab_alloc_trace_init(void)
3130 ASSERT(metaslab_alloc_trace_cache
== NULL
);
3131 metaslab_alloc_trace_cache
= kmem_cache_create(
3132 "metaslab_alloc_trace_cache", sizeof (metaslab_alloc_trace_t
),
3133 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
3134 metaslab_trace_ksp
= kstat_create("zfs", 0, "metaslab_trace_stats",
3135 "misc", KSTAT_TYPE_NAMED
, 1, KSTAT_FLAG_VIRTUAL
);
3136 if (metaslab_trace_ksp
!= NULL
) {
3137 metaslab_trace_ksp
->ks_data
= &metaslab_trace_over_limit
;
3138 kstat_named_init(&metaslab_trace_over_limit
,
3139 "metaslab_trace_over_limit", KSTAT_DATA_UINT64
);
3140 kstat_install(metaslab_trace_ksp
);
3145 metaslab_alloc_trace_fini(void)
3147 if (metaslab_trace_ksp
!= NULL
) {
3148 kstat_delete(metaslab_trace_ksp
);
3149 metaslab_trace_ksp
= NULL
;
3151 kmem_cache_destroy(metaslab_alloc_trace_cache
);
3152 metaslab_alloc_trace_cache
= NULL
;
3156 * Add an allocation trace element to the allocation tracing list.
3159 metaslab_trace_add(zio_alloc_list_t
*zal
, metaslab_group_t
*mg
,
3160 metaslab_t
*msp
, uint64_t psize
, uint32_t dva_id
, uint64_t offset
,
3163 metaslab_alloc_trace_t
*mat
;
3165 if (!metaslab_trace_enabled
)
3169 * When the tracing list reaches its maximum we remove
3170 * the second element in the list before adding a new one.
3171 * By removing the second element we preserve the original
3172 * entry as a clue to what allocations steps have already been
3175 if (zal
->zal_size
== metaslab_trace_max_entries
) {
3176 metaslab_alloc_trace_t
*mat_next
;
3178 panic("too many entries in allocation list");
3180 atomic_inc_64(&metaslab_trace_over_limit
.value
.ui64
);
3182 mat_next
= list_next(&zal
->zal_list
, list_head(&zal
->zal_list
));
3183 list_remove(&zal
->zal_list
, mat_next
);
3184 kmem_cache_free(metaslab_alloc_trace_cache
, mat_next
);
3187 mat
= kmem_cache_alloc(metaslab_alloc_trace_cache
, KM_SLEEP
);
3188 list_link_init(&mat
->mat_list_node
);
3191 mat
->mat_size
= psize
;
3192 mat
->mat_dva_id
= dva_id
;
3193 mat
->mat_offset
= offset
;
3194 mat
->mat_weight
= 0;
3195 mat
->mat_allocator
= allocator
;
3198 mat
->mat_weight
= msp
->ms_weight
;
3201 * The list is part of the zio so locking is not required. Only
3202 * a single thread will perform allocations for a given zio.
3204 list_insert_tail(&zal
->zal_list
, mat
);
3207 ASSERT3U(zal
->zal_size
, <=, metaslab_trace_max_entries
);
3211 metaslab_trace_init(zio_alloc_list_t
*zal
)
3213 list_create(&zal
->zal_list
, sizeof (metaslab_alloc_trace_t
),
3214 offsetof(metaslab_alloc_trace_t
, mat_list_node
));
3219 metaslab_trace_fini(zio_alloc_list_t
*zal
)
3221 metaslab_alloc_trace_t
*mat
;
3223 while ((mat
= list_remove_head(&zal
->zal_list
)) != NULL
)
3224 kmem_cache_free(metaslab_alloc_trace_cache
, mat
);
3225 list_destroy(&zal
->zal_list
);
3230 #define metaslab_trace_add(zal, mg, msp, psize, id, off, alloc)
3233 metaslab_alloc_trace_init(void)
3238 metaslab_alloc_trace_fini(void)
3243 metaslab_trace_init(zio_alloc_list_t
*zal
)
3248 metaslab_trace_fini(zio_alloc_list_t
*zal
)
3252 #endif /* _METASLAB_TRACING */
3255 * ==========================================================================
3256 * Metaslab block operations
3257 * ==========================================================================
3261 metaslab_group_alloc_increment(spa_t
*spa
, uint64_t vdev
, void *tag
, int flags
,
3264 if (!(flags
& METASLAB_ASYNC_ALLOC
) ||
3265 (flags
& METASLAB_DONT_THROTTLE
))
3268 metaslab_group_t
*mg
= vdev_lookup_top(spa
, vdev
)->vdev_mg
;
3269 if (!mg
->mg_class
->mc_alloc_throttle_enabled
)
3272 (void) zfs_refcount_add(&mg
->mg_alloc_queue_depth
[allocator
], tag
);
3276 metaslab_group_increment_qdepth(metaslab_group_t
*mg
, int allocator
)
3278 uint64_t max
= mg
->mg_max_alloc_queue_depth
;
3279 uint64_t cur
= mg
->mg_cur_max_alloc_queue_depth
[allocator
];
3281 if (atomic_cas_64(&mg
->mg_cur_max_alloc_queue_depth
[allocator
],
3282 cur
, cur
+ 1) == cur
) {
3284 &mg
->mg_class
->mc_alloc_max_slots
[allocator
]);
3287 cur
= mg
->mg_cur_max_alloc_queue_depth
[allocator
];
3292 metaslab_group_alloc_decrement(spa_t
*spa
, uint64_t vdev
, void *tag
, int flags
,
3293 int allocator
, boolean_t io_complete
)
3295 if (!(flags
& METASLAB_ASYNC_ALLOC
) ||
3296 (flags
& METASLAB_DONT_THROTTLE
))
3299 metaslab_group_t
*mg
= vdev_lookup_top(spa
, vdev
)->vdev_mg
;
3300 if (!mg
->mg_class
->mc_alloc_throttle_enabled
)
3303 (void) zfs_refcount_remove(&mg
->mg_alloc_queue_depth
[allocator
], tag
);
3305 metaslab_group_increment_qdepth(mg
, allocator
);
3309 metaslab_group_alloc_verify(spa_t
*spa
, const blkptr_t
*bp
, void *tag
,
3313 const dva_t
*dva
= bp
->blk_dva
;
3314 int ndvas
= BP_GET_NDVAS(bp
);
3316 for (int d
= 0; d
< ndvas
; d
++) {
3317 uint64_t vdev
= DVA_GET_VDEV(&dva
[d
]);
3318 metaslab_group_t
*mg
= vdev_lookup_top(spa
, vdev
)->vdev_mg
;
3319 VERIFY(zfs_refcount_not_held(
3320 &mg
->mg_alloc_queue_depth
[allocator
], tag
));
3326 metaslab_block_alloc(metaslab_t
*msp
, uint64_t size
, uint64_t txg
)
3329 range_tree_t
*rt
= msp
->ms_allocatable
;
3330 metaslab_class_t
*mc
= msp
->ms_group
->mg_class
;
3332 VERIFY(!msp
->ms_condensing
);
3333 VERIFY0(msp
->ms_initializing
);
3335 start
= mc
->mc_ops
->msop_alloc(msp
, size
);
3336 if (start
!= -1ULL) {
3337 metaslab_group_t
*mg
= msp
->ms_group
;
3338 vdev_t
*vd
= mg
->mg_vd
;
3340 VERIFY0(P2PHASE(start
, 1ULL << vd
->vdev_ashift
));
3341 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
3342 VERIFY3U(range_tree_space(rt
) - size
, <=, msp
->ms_size
);
3343 range_tree_remove(rt
, start
, size
);
3345 if (range_tree_is_empty(msp
->ms_allocating
[txg
& TXG_MASK
]))
3346 vdev_dirty(mg
->mg_vd
, VDD_METASLAB
, msp
, txg
);
3348 range_tree_add(msp
->ms_allocating
[txg
& TXG_MASK
], start
, size
);
3350 /* Track the last successful allocation */
3351 msp
->ms_alloc_txg
= txg
;
3352 metaslab_verify_space(msp
, txg
);
3356 * Now that we've attempted the allocation we need to update the
3357 * metaslab's maximum block size since it may have changed.
3359 msp
->ms_max_size
= metaslab_block_maxsize(msp
);
3364 * Find the metaslab with the highest weight that is less than what we've
3365 * already tried. In the common case, this means that we will examine each
3366 * metaslab at most once. Note that concurrent callers could reorder metaslabs
3367 * by activation/passivation once we have dropped the mg_lock. If a metaslab is
3368 * activated by another thread, and we fail to allocate from the metaslab we
3369 * have selected, we may not try the newly-activated metaslab, and instead
3370 * activate another metaslab. This is not optimal, but generally does not cause
3371 * any problems (a possible exception being if every metaslab is completely full
3372 * except for the the newly-activated metaslab which we fail to examine).
3375 find_valid_metaslab(metaslab_group_t
*mg
, uint64_t activation_weight
,
3376 dva_t
*dva
, int d
, boolean_t want_unique
, uint64_t asize
, int allocator
,
3377 zio_alloc_list_t
*zal
, metaslab_t
*search
, boolean_t
*was_active
)
3380 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
3381 metaslab_t
*msp
= avl_find(t
, search
, &idx
);
3383 msp
= avl_nearest(t
, idx
, AVL_AFTER
);
3385 for (; msp
!= NULL
; msp
= AVL_NEXT(t
, msp
)) {
3387 if (!metaslab_should_allocate(msp
, asize
)) {
3388 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3389 TRACE_TOO_SMALL
, allocator
);
3394 * If the selected metaslab is condensing or being
3395 * initialized, skip it.
3397 if (msp
->ms_condensing
|| msp
->ms_initializing
> 0)
3400 *was_active
= msp
->ms_allocator
!= -1;
3402 * If we're activating as primary, this is our first allocation
3403 * from this disk, so we don't need to check how close we are.
3404 * If the metaslab under consideration was already active,
3405 * we're getting desperate enough to steal another allocator's
3406 * metaslab, so we still don't care about distances.
3408 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
|| *was_active
)
3411 for (i
= 0; i
< d
; i
++) {
3413 !metaslab_is_unique(msp
, &dva
[i
]))
3414 break; /* try another metaslab */
3421 search
->ms_weight
= msp
->ms_weight
;
3422 search
->ms_start
= msp
->ms_start
+ 1;
3423 search
->ms_allocator
= msp
->ms_allocator
;
3424 search
->ms_primary
= msp
->ms_primary
;
3431 metaslab_group_alloc_normal(metaslab_group_t
*mg
, zio_alloc_list_t
*zal
,
3432 uint64_t asize
, uint64_t txg
, boolean_t want_unique
, dva_t
*dva
,
3433 int d
, int allocator
)
3435 metaslab_t
*msp
= NULL
;
3436 uint64_t offset
= -1ULL;
3437 uint64_t activation_weight
;
3439 activation_weight
= METASLAB_WEIGHT_PRIMARY
;
3440 for (int i
= 0; i
< d
; i
++) {
3441 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
&&
3442 DVA_GET_VDEV(&dva
[i
]) == mg
->mg_vd
->vdev_id
) {
3443 activation_weight
= METASLAB_WEIGHT_SECONDARY
;
3444 } else if (activation_weight
== METASLAB_WEIGHT_SECONDARY
&&
3445 DVA_GET_VDEV(&dva
[i
]) == mg
->mg_vd
->vdev_id
) {
3446 activation_weight
= METASLAB_WEIGHT_CLAIM
;
3452 * If we don't have enough metaslabs active to fill the entire array, we
3453 * just use the 0th slot.
3455 if (mg
->mg_ms_ready
< mg
->mg_allocators
* 3)
3458 ASSERT3U(mg
->mg_vd
->vdev_ms_count
, >=, 2);
3460 metaslab_t
*search
= kmem_alloc(sizeof (*search
), KM_SLEEP
);
3461 search
->ms_weight
= UINT64_MAX
;
3462 search
->ms_start
= 0;
3464 * At the end of the metaslab tree are the already-active metaslabs,
3465 * first the primaries, then the secondaries. When we resume searching
3466 * through the tree, we need to consider ms_allocator and ms_primary so
3467 * we start in the location right after where we left off, and don't
3468 * accidentally loop forever considering the same metaslabs.
3470 search
->ms_allocator
= -1;
3471 search
->ms_primary
= B_TRUE
;
3473 boolean_t was_active
= B_FALSE
;
3475 mutex_enter(&mg
->mg_lock
);
3477 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
&&
3478 mg
->mg_primaries
[allocator
] != NULL
) {
3479 msp
= mg
->mg_primaries
[allocator
];
3480 was_active
= B_TRUE
;
3481 } else if (activation_weight
== METASLAB_WEIGHT_SECONDARY
&&
3482 mg
->mg_secondaries
[allocator
] != NULL
) {
3483 msp
= mg
->mg_secondaries
[allocator
];
3484 was_active
= B_TRUE
;
3486 msp
= find_valid_metaslab(mg
, activation_weight
, dva
, d
,
3487 want_unique
, asize
, allocator
, zal
, search
,
3491 mutex_exit(&mg
->mg_lock
);
3493 kmem_free(search
, sizeof (*search
));
3497 mutex_enter(&msp
->ms_lock
);
3499 * Ensure that the metaslab we have selected is still
3500 * capable of handling our request. It's possible that
3501 * another thread may have changed the weight while we
3502 * were blocked on the metaslab lock. We check the
3503 * active status first to see if we need to reselect
3506 if (was_active
&& !(msp
->ms_weight
& METASLAB_ACTIVE_MASK
)) {
3507 mutex_exit(&msp
->ms_lock
);
3512 * If the metaslab is freshly activated for an allocator that
3513 * isn't the one we're allocating from, or if it's a primary and
3514 * we're seeking a secondary (or vice versa), we go back and
3515 * select a new metaslab.
3517 if (!was_active
&& (msp
->ms_weight
& METASLAB_ACTIVE_MASK
) &&
3518 (msp
->ms_allocator
!= -1) &&
3519 (msp
->ms_allocator
!= allocator
|| ((activation_weight
==
3520 METASLAB_WEIGHT_PRIMARY
) != msp
->ms_primary
))) {
3521 mutex_exit(&msp
->ms_lock
);
3525 if (msp
->ms_weight
& METASLAB_WEIGHT_CLAIM
&&
3526 activation_weight
!= METASLAB_WEIGHT_CLAIM
) {
3527 metaslab_passivate(msp
, msp
->ms_weight
&
3528 ~METASLAB_WEIGHT_CLAIM
);
3529 mutex_exit(&msp
->ms_lock
);
3533 if (metaslab_activate(msp
, allocator
, activation_weight
) != 0) {
3534 mutex_exit(&msp
->ms_lock
);
3538 msp
->ms_selected_txg
= txg
;
3541 * Now that we have the lock, recheck to see if we should
3542 * continue to use this metaslab for this allocation. The
3543 * the metaslab is now loaded so metaslab_should_allocate() can
3544 * accurately determine if the allocation attempt should
3547 if (!metaslab_should_allocate(msp
, asize
)) {
3548 /* Passivate this metaslab and select a new one. */
3549 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3550 TRACE_TOO_SMALL
, allocator
);
3556 * If this metaslab is currently condensing then pick again as
3557 * we can't manipulate this metaslab until it's committed
3558 * to disk. If this metaslab is being initialized, we shouldn't
3559 * allocate from it since the allocated region might be
3560 * overwritten after allocation.
3562 if (msp
->ms_condensing
) {
3563 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3564 TRACE_CONDENSING
, allocator
);
3565 metaslab_passivate(msp
, msp
->ms_weight
&
3566 ~METASLAB_ACTIVE_MASK
);
3567 mutex_exit(&msp
->ms_lock
);
3569 } else if (msp
->ms_initializing
> 0) {
3570 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3571 TRACE_INITIALIZING
, allocator
);
3572 metaslab_passivate(msp
, msp
->ms_weight
&
3573 ~METASLAB_ACTIVE_MASK
);
3574 mutex_exit(&msp
->ms_lock
);
3578 offset
= metaslab_block_alloc(msp
, asize
, txg
);
3579 metaslab_trace_add(zal
, mg
, msp
, asize
, d
, offset
, allocator
);
3581 if (offset
!= -1ULL) {
3582 /* Proactively passivate the metaslab, if needed */
3583 metaslab_segment_may_passivate(msp
);
3587 ASSERT(msp
->ms_loaded
);
3590 * We were unable to allocate from this metaslab so determine
3591 * a new weight for this metaslab. Now that we have loaded
3592 * the metaslab we can provide a better hint to the metaslab
3595 * For space-based metaslabs, we use the maximum block size.
3596 * This information is only available when the metaslab
3597 * is loaded and is more accurate than the generic free
3598 * space weight that was calculated by metaslab_weight().
3599 * This information allows us to quickly compare the maximum
3600 * available allocation in the metaslab to the allocation
3601 * size being requested.
3603 * For segment-based metaslabs, determine the new weight
3604 * based on the highest bucket in the range tree. We
3605 * explicitly use the loaded segment weight (i.e. the range
3606 * tree histogram) since it contains the space that is
3607 * currently available for allocation and is accurate
3608 * even within a sync pass.
3610 if (WEIGHT_IS_SPACEBASED(msp
->ms_weight
)) {
3611 uint64_t weight
= metaslab_block_maxsize(msp
);
3612 WEIGHT_SET_SPACEBASED(weight
);
3613 metaslab_passivate(msp
, weight
);
3615 metaslab_passivate(msp
,
3616 metaslab_weight_from_range_tree(msp
));
3620 * We have just failed an allocation attempt, check
3621 * that metaslab_should_allocate() agrees. Otherwise,
3622 * we may end up in an infinite loop retrying the same
3625 ASSERT(!metaslab_should_allocate(msp
, asize
));
3627 mutex_exit(&msp
->ms_lock
);
3629 mutex_exit(&msp
->ms_lock
);
3630 kmem_free(search
, sizeof (*search
));
3635 metaslab_group_alloc(metaslab_group_t
*mg
, zio_alloc_list_t
*zal
,
3636 uint64_t asize
, uint64_t txg
, boolean_t want_unique
, dva_t
*dva
,
3637 int d
, int allocator
)
3640 ASSERT(mg
->mg_initialized
);
3642 offset
= metaslab_group_alloc_normal(mg
, zal
, asize
, txg
, want_unique
,
3645 mutex_enter(&mg
->mg_lock
);
3646 if (offset
== -1ULL) {
3647 mg
->mg_failed_allocations
++;
3648 metaslab_trace_add(zal
, mg
, NULL
, asize
, d
,
3649 TRACE_GROUP_FAILURE
, allocator
);
3650 if (asize
== SPA_GANGBLOCKSIZE
) {
3652 * This metaslab group was unable to allocate
3653 * the minimum gang block size so it must be out of
3654 * space. We must notify the allocation throttle
3655 * to start skipping allocation attempts to this
3656 * metaslab group until more space becomes available.
3657 * Note: this failure cannot be caused by the
3658 * allocation throttle since the allocation throttle
3659 * is only responsible for skipping devices and
3660 * not failing block allocations.
3662 mg
->mg_no_free_space
= B_TRUE
;
3665 mg
->mg_allocations
++;
3666 mutex_exit(&mg
->mg_lock
);
3671 * Allocate a block for the specified i/o.
3674 metaslab_alloc_dva(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
,
3675 dva_t
*dva
, int d
, dva_t
*hintdva
, uint64_t txg
, int flags
,
3676 zio_alloc_list_t
*zal
, int allocator
)
3678 metaslab_group_t
*mg
, *fast_mg
, *rotor
;
3680 boolean_t try_hard
= B_FALSE
;
3682 ASSERT(!DVA_IS_VALID(&dva
[d
]));
3685 * For testing, make some blocks above a certain size be gang blocks.
3686 * This will result in more split blocks when using device removal,
3687 * and a large number of split blocks coupled with ztest-induced
3688 * damage can result in extremely long reconstruction times. This
3689 * will also test spilling from special to normal.
3691 if (psize
>= metaslab_force_ganging
&& (spa_get_random(100) < 3)) {
3692 metaslab_trace_add(zal
, NULL
, NULL
, psize
, d
, TRACE_FORCE_GANG
,
3694 return (SET_ERROR(ENOSPC
));
3698 * Start at the rotor and loop through all mgs until we find something.
3699 * Note that there's no locking on mc_rotor or mc_aliquot because
3700 * nothing actually breaks if we miss a few updates -- we just won't
3701 * allocate quite as evenly. It all balances out over time.
3703 * If we are doing ditto or log blocks, try to spread them across
3704 * consecutive vdevs. If we're forced to reuse a vdev before we've
3705 * allocated all of our ditto blocks, then try and spread them out on
3706 * that vdev as much as possible. If it turns out to not be possible,
3707 * gradually lower our standards until anything becomes acceptable.
3708 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
3709 * gives us hope of containing our fault domains to something we're
3710 * able to reason about. Otherwise, any two top-level vdev failures
3711 * will guarantee the loss of data. With consecutive allocation,
3712 * only two adjacent top-level vdev failures will result in data loss.
3714 * If we are doing gang blocks (hintdva is non-NULL), try to keep
3715 * ourselves on the same vdev as our gang block header. That
3716 * way, we can hope for locality in vdev_cache, plus it makes our
3717 * fault domains something tractable.
3720 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&hintdva
[d
]));
3723 * It's possible the vdev we're using as the hint no
3724 * longer exists or its mg has been closed (e.g. by
3725 * device removal). Consult the rotor when
3728 if (vd
!= NULL
&& vd
->vdev_mg
!= NULL
) {
3731 if (flags
& METASLAB_HINTBP_AVOID
&&
3732 mg
->mg_next
!= NULL
)
3737 } else if (d
!= 0) {
3738 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
- 1]));
3739 mg
= vd
->vdev_mg
->mg_next
;
3740 } else if (flags
& METASLAB_FASTWRITE
) {
3741 mg
= fast_mg
= mc
->mc_rotor
;
3744 if (fast_mg
->mg_vd
->vdev_pending_fastwrite
<
3745 mg
->mg_vd
->vdev_pending_fastwrite
)
3747 } while ((fast_mg
= fast_mg
->mg_next
) != mc
->mc_rotor
);
3750 ASSERT(mc
->mc_rotor
!= NULL
);
3755 * If the hint put us into the wrong metaslab class, or into a
3756 * metaslab group that has been passivated, just follow the rotor.
3758 if (mg
->mg_class
!= mc
|| mg
->mg_activation_count
<= 0)
3764 boolean_t allocatable
;
3766 ASSERT(mg
->mg_activation_count
== 1);
3770 * Don't allocate from faulted devices.
3773 spa_config_enter(spa
, SCL_ZIO
, FTAG
, RW_READER
);
3774 allocatable
= vdev_allocatable(vd
);
3775 spa_config_exit(spa
, SCL_ZIO
, FTAG
);
3777 allocatable
= vdev_allocatable(vd
);
3781 * Determine if the selected metaslab group is eligible
3782 * for allocations. If we're ganging then don't allow
3783 * this metaslab group to skip allocations since that would
3784 * inadvertently return ENOSPC and suspend the pool
3785 * even though space is still available.
3787 if (allocatable
&& !GANG_ALLOCATION(flags
) && !try_hard
) {
3788 allocatable
= metaslab_group_allocatable(mg
, rotor
,
3789 psize
, allocator
, d
);
3793 metaslab_trace_add(zal
, mg
, NULL
, psize
, d
,
3794 TRACE_NOT_ALLOCATABLE
, allocator
);
3798 ASSERT(mg
->mg_initialized
);
3801 * Avoid writing single-copy data to a failing,
3802 * non-redundant vdev, unless we've already tried all
3805 if ((vd
->vdev_stat
.vs_write_errors
> 0 ||
3806 vd
->vdev_state
< VDEV_STATE_HEALTHY
) &&
3807 d
== 0 && !try_hard
&& vd
->vdev_children
== 0) {
3808 metaslab_trace_add(zal
, mg
, NULL
, psize
, d
,
3809 TRACE_VDEV_ERROR
, allocator
);
3813 ASSERT(mg
->mg_class
== mc
);
3815 uint64_t asize
= vdev_psize_to_asize(vd
, psize
);
3816 ASSERT(P2PHASE(asize
, 1ULL << vd
->vdev_ashift
) == 0);
3819 * If we don't need to try hard, then require that the
3820 * block be on an different metaslab from any other DVAs
3821 * in this BP (unique=true). If we are trying hard, then
3822 * allow any metaslab to be used (unique=false).
3824 uint64_t offset
= metaslab_group_alloc(mg
, zal
, asize
, txg
,
3825 !try_hard
, dva
, d
, allocator
);
3827 if (offset
!= -1ULL) {
3829 * If we've just selected this metaslab group,
3830 * figure out whether the corresponding vdev is
3831 * over- or under-used relative to the pool,
3832 * and set an allocation bias to even it out.
3834 * Bias is also used to compensate for unequally
3835 * sized vdevs so that space is allocated fairly.
3837 if (mc
->mc_aliquot
== 0 && metaslab_bias_enabled
) {
3838 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3839 int64_t vs_free
= vs
->vs_space
- vs
->vs_alloc
;
3840 int64_t mc_free
= mc
->mc_space
- mc
->mc_alloc
;
3844 * Calculate how much more or less we should
3845 * try to allocate from this device during
3846 * this iteration around the rotor.
3848 * This basically introduces a zero-centered
3849 * bias towards the devices with the most
3850 * free space, while compensating for vdev
3854 * vdev V1 = 16M/128M
3855 * vdev V2 = 16M/128M
3856 * ratio(V1) = 100% ratio(V2) = 100%
3858 * vdev V1 = 16M/128M
3859 * vdev V2 = 64M/128M
3860 * ratio(V1) = 127% ratio(V2) = 72%
3862 * vdev V1 = 16M/128M
3863 * vdev V2 = 64M/512M
3864 * ratio(V1) = 40% ratio(V2) = 160%
3866 ratio
= (vs_free
* mc
->mc_alloc_groups
* 100) /
3868 mg
->mg_bias
= ((ratio
- 100) *
3869 (int64_t)mg
->mg_aliquot
) / 100;
3870 } else if (!metaslab_bias_enabled
) {
3874 if ((flags
& METASLAB_FASTWRITE
) ||
3875 atomic_add_64_nv(&mc
->mc_aliquot
, asize
) >=
3876 mg
->mg_aliquot
+ mg
->mg_bias
) {
3877 mc
->mc_rotor
= mg
->mg_next
;
3881 DVA_SET_VDEV(&dva
[d
], vd
->vdev_id
);
3882 DVA_SET_OFFSET(&dva
[d
], offset
);
3883 DVA_SET_GANG(&dva
[d
],
3884 ((flags
& METASLAB_GANG_HEADER
) ? 1 : 0));
3885 DVA_SET_ASIZE(&dva
[d
], asize
);
3887 if (flags
& METASLAB_FASTWRITE
) {
3888 atomic_add_64(&vd
->vdev_pending_fastwrite
,
3895 mc
->mc_rotor
= mg
->mg_next
;
3897 } while ((mg
= mg
->mg_next
) != rotor
);
3900 * If we haven't tried hard, do so now.
3907 bzero(&dva
[d
], sizeof (dva_t
));
3909 metaslab_trace_add(zal
, rotor
, NULL
, psize
, d
, TRACE_ENOSPC
, allocator
);
3910 return (SET_ERROR(ENOSPC
));
3914 metaslab_free_concrete(vdev_t
*vd
, uint64_t offset
, uint64_t asize
,
3915 boolean_t checkpoint
)
3918 spa_t
*spa
= vd
->vdev_spa
;
3920 ASSERT(vdev_is_concrete(vd
));
3921 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
3922 ASSERT3U(offset
>> vd
->vdev_ms_shift
, <, vd
->vdev_ms_count
);
3924 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
3926 VERIFY(!msp
->ms_condensing
);
3927 VERIFY3U(offset
, >=, msp
->ms_start
);
3928 VERIFY3U(offset
+ asize
, <=, msp
->ms_start
+ msp
->ms_size
);
3929 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
3930 VERIFY0(P2PHASE(asize
, 1ULL << vd
->vdev_ashift
));
3932 metaslab_check_free_impl(vd
, offset
, asize
);
3934 mutex_enter(&msp
->ms_lock
);
3935 if (range_tree_is_empty(msp
->ms_freeing
) &&
3936 range_tree_is_empty(msp
->ms_checkpointing
)) {
3937 vdev_dirty(vd
, VDD_METASLAB
, msp
, spa_syncing_txg(spa
));
3941 ASSERT(spa_has_checkpoint(spa
));
3942 range_tree_add(msp
->ms_checkpointing
, offset
, asize
);
3944 range_tree_add(msp
->ms_freeing
, offset
, asize
);
3946 mutex_exit(&msp
->ms_lock
);
3951 metaslab_free_impl_cb(uint64_t inner_offset
, vdev_t
*vd
, uint64_t offset
,
3952 uint64_t size
, void *arg
)
3954 boolean_t
*checkpoint
= arg
;
3956 ASSERT3P(checkpoint
, !=, NULL
);
3958 if (vd
->vdev_ops
->vdev_op_remap
!= NULL
)
3959 vdev_indirect_mark_obsolete(vd
, offset
, size
);
3961 metaslab_free_impl(vd
, offset
, size
, *checkpoint
);
3965 metaslab_free_impl(vdev_t
*vd
, uint64_t offset
, uint64_t size
,
3966 boolean_t checkpoint
)
3968 spa_t
*spa
= vd
->vdev_spa
;
3970 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
3972 if (spa_syncing_txg(spa
) > spa_freeze_txg(spa
))
3975 if (spa
->spa_vdev_removal
!= NULL
&&
3976 spa
->spa_vdev_removal
->svr_vdev_id
== vd
->vdev_id
&&
3977 vdev_is_concrete(vd
)) {
3979 * Note: we check if the vdev is concrete because when
3980 * we complete the removal, we first change the vdev to be
3981 * an indirect vdev (in open context), and then (in syncing
3982 * context) clear spa_vdev_removal.
3984 free_from_removing_vdev(vd
, offset
, size
);
3985 } else if (vd
->vdev_ops
->vdev_op_remap
!= NULL
) {
3986 vdev_indirect_mark_obsolete(vd
, offset
, size
);
3987 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
,
3988 metaslab_free_impl_cb
, &checkpoint
);
3990 metaslab_free_concrete(vd
, offset
, size
, checkpoint
);
3994 typedef struct remap_blkptr_cb_arg
{
3996 spa_remap_cb_t rbca_cb
;
3997 vdev_t
*rbca_remap_vd
;
3998 uint64_t rbca_remap_offset
;
4000 } remap_blkptr_cb_arg_t
;
4003 remap_blkptr_cb(uint64_t inner_offset
, vdev_t
*vd
, uint64_t offset
,
4004 uint64_t size
, void *arg
)
4006 remap_blkptr_cb_arg_t
*rbca
= arg
;
4007 blkptr_t
*bp
= rbca
->rbca_bp
;
4009 /* We can not remap split blocks. */
4010 if (size
!= DVA_GET_ASIZE(&bp
->blk_dva
[0]))
4012 ASSERT0(inner_offset
);
4014 if (rbca
->rbca_cb
!= NULL
) {
4016 * At this point we know that we are not handling split
4017 * blocks and we invoke the callback on the previous
4018 * vdev which must be indirect.
4020 ASSERT3P(rbca
->rbca_remap_vd
->vdev_ops
, ==, &vdev_indirect_ops
);
4022 rbca
->rbca_cb(rbca
->rbca_remap_vd
->vdev_id
,
4023 rbca
->rbca_remap_offset
, size
, rbca
->rbca_cb_arg
);
4025 /* set up remap_blkptr_cb_arg for the next call */
4026 rbca
->rbca_remap_vd
= vd
;
4027 rbca
->rbca_remap_offset
= offset
;
4031 * The phys birth time is that of dva[0]. This ensures that we know
4032 * when each dva was written, so that resilver can determine which
4033 * blocks need to be scrubbed (i.e. those written during the time
4034 * the vdev was offline). It also ensures that the key used in
4035 * the ARC hash table is unique (i.e. dva[0] + phys_birth). If
4036 * we didn't change the phys_birth, a lookup in the ARC for a
4037 * remapped BP could find the data that was previously stored at
4038 * this vdev + offset.
4040 vdev_t
*oldvd
= vdev_lookup_top(vd
->vdev_spa
,
4041 DVA_GET_VDEV(&bp
->blk_dva
[0]));
4042 vdev_indirect_births_t
*vib
= oldvd
->vdev_indirect_births
;
4043 bp
->blk_phys_birth
= vdev_indirect_births_physbirth(vib
,
4044 DVA_GET_OFFSET(&bp
->blk_dva
[0]), DVA_GET_ASIZE(&bp
->blk_dva
[0]));
4046 DVA_SET_VDEV(&bp
->blk_dva
[0], vd
->vdev_id
);
4047 DVA_SET_OFFSET(&bp
->blk_dva
[0], offset
);
4051 * If the block pointer contains any indirect DVAs, modify them to refer to
4052 * concrete DVAs. Note that this will sometimes not be possible, leaving
4053 * the indirect DVA in place. This happens if the indirect DVA spans multiple
4054 * segments in the mapping (i.e. it is a "split block").
4056 * If the BP was remapped, calls the callback on the original dva (note the
4057 * callback can be called multiple times if the original indirect DVA refers
4058 * to another indirect DVA, etc).
4060 * Returns TRUE if the BP was remapped.
4063 spa_remap_blkptr(spa_t
*spa
, blkptr_t
*bp
, spa_remap_cb_t callback
, void *arg
)
4065 remap_blkptr_cb_arg_t rbca
;
4067 if (!zfs_remap_blkptr_enable
)
4070 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_OBSOLETE_COUNTS
))
4074 * Dedup BP's can not be remapped, because ddt_phys_select() depends
4075 * on DVA[0] being the same in the BP as in the DDT (dedup table).
4077 if (BP_GET_DEDUP(bp
))
4081 * Gang blocks can not be remapped, because
4082 * zio_checksum_gang_verifier() depends on the DVA[0] that's in
4083 * the BP used to read the gang block header (GBH) being the same
4084 * as the DVA[0] that we allocated for the GBH.
4090 * Embedded BP's have no DVA to remap.
4092 if (BP_GET_NDVAS(bp
) < 1)
4096 * Note: we only remap dva[0]. If we remapped other dvas, we
4097 * would no longer know what their phys birth txg is.
4099 dva_t
*dva
= &bp
->blk_dva
[0];
4101 uint64_t offset
= DVA_GET_OFFSET(dva
);
4102 uint64_t size
= DVA_GET_ASIZE(dva
);
4103 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
4105 if (vd
->vdev_ops
->vdev_op_remap
== NULL
)
4109 rbca
.rbca_cb
= callback
;
4110 rbca
.rbca_remap_vd
= vd
;
4111 rbca
.rbca_remap_offset
= offset
;
4112 rbca
.rbca_cb_arg
= arg
;
4115 * remap_blkptr_cb() will be called in order for each level of
4116 * indirection, until a concrete vdev is reached or a split block is
4117 * encountered. old_vd and old_offset are updated within the callback
4118 * as we go from the one indirect vdev to the next one (either concrete
4119 * or indirect again) in that order.
4121 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
, remap_blkptr_cb
, &rbca
);
4123 /* Check if the DVA wasn't remapped because it is a split block */
4124 if (DVA_GET_VDEV(&rbca
.rbca_bp
->blk_dva
[0]) == vd
->vdev_id
)
4131 * Undo the allocation of a DVA which happened in the given transaction group.
4134 metaslab_unalloc_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
)
4138 uint64_t vdev
= DVA_GET_VDEV(dva
);
4139 uint64_t offset
= DVA_GET_OFFSET(dva
);
4140 uint64_t size
= DVA_GET_ASIZE(dva
);
4142 ASSERT(DVA_IS_VALID(dva
));
4143 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
4145 if (txg
> spa_freeze_txg(spa
))
4148 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
|| !DVA_IS_VALID(dva
) ||
4149 (offset
>> vd
->vdev_ms_shift
) >= vd
->vdev_ms_count
) {
4150 zfs_panic_recover("metaslab_free_dva(): bad DVA %llu:%llu:%llu",
4151 (u_longlong_t
)vdev
, (u_longlong_t
)offset
,
4152 (u_longlong_t
)size
);
4156 ASSERT(!vd
->vdev_removing
);
4157 ASSERT(vdev_is_concrete(vd
));
4158 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
4159 ASSERT3P(vd
->vdev_indirect_mapping
, ==, NULL
);
4161 if (DVA_GET_GANG(dva
))
4162 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
4164 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
4166 mutex_enter(&msp
->ms_lock
);
4167 range_tree_remove(msp
->ms_allocating
[txg
& TXG_MASK
],
4170 VERIFY(!msp
->ms_condensing
);
4171 VERIFY3U(offset
, >=, msp
->ms_start
);
4172 VERIFY3U(offset
+ size
, <=, msp
->ms_start
+ msp
->ms_size
);
4173 VERIFY3U(range_tree_space(msp
->ms_allocatable
) + size
, <=,
4175 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
4176 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
4177 range_tree_add(msp
->ms_allocatable
, offset
, size
);
4178 mutex_exit(&msp
->ms_lock
);
4182 * Free the block represented by the given DVA.
4185 metaslab_free_dva(spa_t
*spa
, const dva_t
*dva
, boolean_t checkpoint
)
4187 uint64_t vdev
= DVA_GET_VDEV(dva
);
4188 uint64_t offset
= DVA_GET_OFFSET(dva
);
4189 uint64_t size
= DVA_GET_ASIZE(dva
);
4190 vdev_t
*vd
= vdev_lookup_top(spa
, vdev
);
4192 ASSERT(DVA_IS_VALID(dva
));
4193 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
4195 if (DVA_GET_GANG(dva
)) {
4196 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
4199 metaslab_free_impl(vd
, offset
, size
, checkpoint
);
4203 * Reserve some allocation slots. The reservation system must be called
4204 * before we call into the allocator. If there aren't any available slots
4205 * then the I/O will be throttled until an I/O completes and its slots are
4206 * freed up. The function returns true if it was successful in placing
4210 metaslab_class_throttle_reserve(metaslab_class_t
*mc
, int slots
, int allocator
,
4211 zio_t
*zio
, int flags
)
4213 uint64_t available_slots
= 0;
4214 boolean_t slot_reserved
= B_FALSE
;
4215 uint64_t max
= mc
->mc_alloc_max_slots
[allocator
];
4217 ASSERT(mc
->mc_alloc_throttle_enabled
);
4218 mutex_enter(&mc
->mc_lock
);
4220 uint64_t reserved_slots
=
4221 zfs_refcount_count(&mc
->mc_alloc_slots
[allocator
]);
4222 if (reserved_slots
< max
)
4223 available_slots
= max
- reserved_slots
;
4225 if (slots
<= available_slots
|| GANG_ALLOCATION(flags
) ||
4226 flags
& METASLAB_MUST_RESERVE
) {
4228 * We reserve the slots individually so that we can unreserve
4229 * them individually when an I/O completes.
4231 for (int d
= 0; d
< slots
; d
++) {
4233 zfs_refcount_add(&mc
->mc_alloc_slots
[allocator
],
4236 zio
->io_flags
|= ZIO_FLAG_IO_ALLOCATING
;
4237 slot_reserved
= B_TRUE
;
4240 mutex_exit(&mc
->mc_lock
);
4241 return (slot_reserved
);
4245 metaslab_class_throttle_unreserve(metaslab_class_t
*mc
, int slots
,
4246 int allocator
, zio_t
*zio
)
4248 ASSERT(mc
->mc_alloc_throttle_enabled
);
4249 mutex_enter(&mc
->mc_lock
);
4250 for (int d
= 0; d
< slots
; d
++) {
4251 (void) zfs_refcount_remove(&mc
->mc_alloc_slots
[allocator
],
4254 mutex_exit(&mc
->mc_lock
);
4258 metaslab_claim_concrete(vdev_t
*vd
, uint64_t offset
, uint64_t size
,
4262 spa_t
*spa
= vd
->vdev_spa
;
4265 if (offset
>> vd
->vdev_ms_shift
>= vd
->vdev_ms_count
)
4266 return (SET_ERROR(ENXIO
));
4268 ASSERT3P(vd
->vdev_ms
, !=, NULL
);
4269 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
4271 mutex_enter(&msp
->ms_lock
);
4273 if ((txg
!= 0 && spa_writeable(spa
)) || !msp
->ms_loaded
) {
4274 error
= metaslab_activate(msp
, 0, METASLAB_WEIGHT_CLAIM
);
4275 if (error
== EBUSY
) {
4276 ASSERT(msp
->ms_loaded
);
4277 ASSERT(msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
4283 !range_tree_contains(msp
->ms_allocatable
, offset
, size
))
4284 error
= SET_ERROR(ENOENT
);
4286 if (error
|| txg
== 0) { /* txg == 0 indicates dry run */
4287 mutex_exit(&msp
->ms_lock
);
4291 VERIFY(!msp
->ms_condensing
);
4292 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
4293 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
4294 VERIFY3U(range_tree_space(msp
->ms_allocatable
) - size
, <=,
4296 range_tree_remove(msp
->ms_allocatable
, offset
, size
);
4298 if (spa_writeable(spa
)) { /* don't dirty if we're zdb(1M) */
4299 if (range_tree_is_empty(msp
->ms_allocating
[txg
& TXG_MASK
]))
4300 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
4301 range_tree_add(msp
->ms_allocating
[txg
& TXG_MASK
],
4305 mutex_exit(&msp
->ms_lock
);
4310 typedef struct metaslab_claim_cb_arg_t
{
4313 } metaslab_claim_cb_arg_t
;
4317 metaslab_claim_impl_cb(uint64_t inner_offset
, vdev_t
*vd
, uint64_t offset
,
4318 uint64_t size
, void *arg
)
4320 metaslab_claim_cb_arg_t
*mcca_arg
= arg
;
4322 if (mcca_arg
->mcca_error
== 0) {
4323 mcca_arg
->mcca_error
= metaslab_claim_concrete(vd
, offset
,
4324 size
, mcca_arg
->mcca_txg
);
4329 metaslab_claim_impl(vdev_t
*vd
, uint64_t offset
, uint64_t size
, uint64_t txg
)
4331 if (vd
->vdev_ops
->vdev_op_remap
!= NULL
) {
4332 metaslab_claim_cb_arg_t arg
;
4335 * Only zdb(1M) can claim on indirect vdevs. This is used
4336 * to detect leaks of mapped space (that are not accounted
4337 * for in the obsolete counts, spacemap, or bpobj).
4339 ASSERT(!spa_writeable(vd
->vdev_spa
));
4343 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
,
4344 metaslab_claim_impl_cb
, &arg
);
4346 if (arg
.mcca_error
== 0) {
4347 arg
.mcca_error
= metaslab_claim_concrete(vd
,
4350 return (arg
.mcca_error
);
4352 return (metaslab_claim_concrete(vd
, offset
, size
, txg
));
4357 * Intent log support: upon opening the pool after a crash, notify the SPA
4358 * of blocks that the intent log has allocated for immediate write, but
4359 * which are still considered free by the SPA because the last transaction
4360 * group didn't commit yet.
4363 metaslab_claim_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
)
4365 uint64_t vdev
= DVA_GET_VDEV(dva
);
4366 uint64_t offset
= DVA_GET_OFFSET(dva
);
4367 uint64_t size
= DVA_GET_ASIZE(dva
);
4370 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
) {
4371 return (SET_ERROR(ENXIO
));
4374 ASSERT(DVA_IS_VALID(dva
));
4376 if (DVA_GET_GANG(dva
))
4377 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
4379 return (metaslab_claim_impl(vd
, offset
, size
, txg
));
4383 metaslab_alloc(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
, blkptr_t
*bp
,
4384 int ndvas
, uint64_t txg
, blkptr_t
*hintbp
, int flags
,
4385 zio_alloc_list_t
*zal
, zio_t
*zio
, int allocator
)
4387 dva_t
*dva
= bp
->blk_dva
;
4388 dva_t
*hintdva
= (hintbp
!= NULL
) ? hintbp
->blk_dva
: NULL
;
4391 ASSERT(bp
->blk_birth
== 0);
4392 ASSERT(BP_PHYSICAL_BIRTH(bp
) == 0);
4394 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
4396 if (mc
->mc_rotor
== NULL
) { /* no vdevs in this class */
4397 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4398 return (SET_ERROR(ENOSPC
));
4401 ASSERT(ndvas
> 0 && ndvas
<= spa_max_replication(spa
));
4402 ASSERT(BP_GET_NDVAS(bp
) == 0);
4403 ASSERT(hintbp
== NULL
|| ndvas
<= BP_GET_NDVAS(hintbp
));
4404 ASSERT3P(zal
, !=, NULL
);
4406 for (int d
= 0; d
< ndvas
; d
++) {
4407 error
= metaslab_alloc_dva(spa
, mc
, psize
, dva
, d
, hintdva
,
4408 txg
, flags
, zal
, allocator
);
4410 for (d
--; d
>= 0; d
--) {
4411 metaslab_unalloc_dva(spa
, &dva
[d
], txg
);
4412 metaslab_group_alloc_decrement(spa
,
4413 DVA_GET_VDEV(&dva
[d
]), zio
, flags
,
4414 allocator
, B_FALSE
);
4415 bzero(&dva
[d
], sizeof (dva_t
));
4417 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4421 * Update the metaslab group's queue depth
4422 * based on the newly allocated dva.
4424 metaslab_group_alloc_increment(spa
,
4425 DVA_GET_VDEV(&dva
[d
]), zio
, flags
, allocator
);
4430 ASSERT(BP_GET_NDVAS(bp
) == ndvas
);
4432 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4434 BP_SET_BIRTH(bp
, txg
, 0);
4440 metaslab_free(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
, boolean_t now
)
4442 const dva_t
*dva
= bp
->blk_dva
;
4443 int ndvas
= BP_GET_NDVAS(bp
);
4445 ASSERT(!BP_IS_HOLE(bp
));
4446 ASSERT(!now
|| bp
->blk_birth
>= spa_syncing_txg(spa
));
4449 * If we have a checkpoint for the pool we need to make sure that
4450 * the blocks that we free that are part of the checkpoint won't be
4451 * reused until the checkpoint is discarded or we revert to it.
4453 * The checkpoint flag is passed down the metaslab_free code path
4454 * and is set whenever we want to add a block to the checkpoint's
4455 * accounting. That is, we "checkpoint" blocks that existed at the
4456 * time the checkpoint was created and are therefore referenced by
4457 * the checkpointed uberblock.
4459 * Note that, we don't checkpoint any blocks if the current
4460 * syncing txg <= spa_checkpoint_txg. We want these frees to sync
4461 * normally as they will be referenced by the checkpointed uberblock.
4463 boolean_t checkpoint
= B_FALSE
;
4464 if (bp
->blk_birth
<= spa
->spa_checkpoint_txg
&&
4465 spa_syncing_txg(spa
) > spa
->spa_checkpoint_txg
) {
4467 * At this point, if the block is part of the checkpoint
4468 * there is no way it was created in the current txg.
4471 ASSERT3U(spa_syncing_txg(spa
), ==, txg
);
4472 checkpoint
= B_TRUE
;
4475 spa_config_enter(spa
, SCL_FREE
, FTAG
, RW_READER
);
4477 for (int d
= 0; d
< ndvas
; d
++) {
4479 metaslab_unalloc_dva(spa
, &dva
[d
], txg
);
4481 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
4482 metaslab_free_dva(spa
, &dva
[d
], checkpoint
);
4486 spa_config_exit(spa
, SCL_FREE
, FTAG
);
4490 metaslab_claim(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
)
4492 const dva_t
*dva
= bp
->blk_dva
;
4493 int ndvas
= BP_GET_NDVAS(bp
);
4496 ASSERT(!BP_IS_HOLE(bp
));
4500 * First do a dry run to make sure all DVAs are claimable,
4501 * so we don't have to unwind from partial failures below.
4503 if ((error
= metaslab_claim(spa
, bp
, 0)) != 0)
4507 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
4509 for (int d
= 0; d
< ndvas
; d
++) {
4510 error
= metaslab_claim_dva(spa
, &dva
[d
], txg
);
4515 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4517 ASSERT(error
== 0 || txg
== 0);
4523 metaslab_fastwrite_mark(spa_t
*spa
, const blkptr_t
*bp
)
4525 const dva_t
*dva
= bp
->blk_dva
;
4526 int ndvas
= BP_GET_NDVAS(bp
);
4527 uint64_t psize
= BP_GET_PSIZE(bp
);
4531 ASSERT(!BP_IS_HOLE(bp
));
4532 ASSERT(!BP_IS_EMBEDDED(bp
));
4535 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4537 for (d
= 0; d
< ndvas
; d
++) {
4538 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
4540 atomic_add_64(&vd
->vdev_pending_fastwrite
, psize
);
4543 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4547 metaslab_fastwrite_unmark(spa_t
*spa
, const blkptr_t
*bp
)
4549 const dva_t
*dva
= bp
->blk_dva
;
4550 int ndvas
= BP_GET_NDVAS(bp
);
4551 uint64_t psize
= BP_GET_PSIZE(bp
);
4555 ASSERT(!BP_IS_HOLE(bp
));
4556 ASSERT(!BP_IS_EMBEDDED(bp
));
4559 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4561 for (d
= 0; d
< ndvas
; d
++) {
4562 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
4564 ASSERT3U(vd
->vdev_pending_fastwrite
, >=, psize
);
4565 atomic_sub_64(&vd
->vdev_pending_fastwrite
, psize
);
4568 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4573 metaslab_check_free_impl_cb(uint64_t inner
, vdev_t
*vd
, uint64_t offset
,
4574 uint64_t size
, void *arg
)
4576 if (vd
->vdev_ops
== &vdev_indirect_ops
)
4579 metaslab_check_free_impl(vd
, offset
, size
);
4583 metaslab_check_free_impl(vdev_t
*vd
, uint64_t offset
, uint64_t size
)
4586 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
4588 if ((zfs_flags
& ZFS_DEBUG_ZIO_FREE
) == 0)
4591 if (vd
->vdev_ops
->vdev_op_remap
!= NULL
) {
4592 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
,
4593 metaslab_check_free_impl_cb
, NULL
);
4597 ASSERT(vdev_is_concrete(vd
));
4598 ASSERT3U(offset
>> vd
->vdev_ms_shift
, <, vd
->vdev_ms_count
);
4599 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
4601 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
4603 mutex_enter(&msp
->ms_lock
);
4604 if (msp
->ms_loaded
) {
4605 range_tree_verify_not_present(msp
->ms_allocatable
,
4609 range_tree_verify_not_present(msp
->ms_freeing
, offset
, size
);
4610 range_tree_verify_not_present(msp
->ms_checkpointing
, offset
, size
);
4611 range_tree_verify_not_present(msp
->ms_freed
, offset
, size
);
4612 for (int j
= 0; j
< TXG_DEFER_SIZE
; j
++)
4613 range_tree_verify_not_present(msp
->ms_defer
[j
], offset
, size
);
4614 mutex_exit(&msp
->ms_lock
);
4618 metaslab_check_free(spa_t
*spa
, const blkptr_t
*bp
)
4620 if ((zfs_flags
& ZFS_DEBUG_ZIO_FREE
) == 0)
4623 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4624 for (int i
= 0; i
< BP_GET_NDVAS(bp
); i
++) {
4625 uint64_t vdev
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
4626 vdev_t
*vd
= vdev_lookup_top(spa
, vdev
);
4627 uint64_t offset
= DVA_GET_OFFSET(&bp
->blk_dva
[i
]);
4628 uint64_t size
= DVA_GET_ASIZE(&bp
->blk_dva
[i
]);
4630 if (DVA_GET_GANG(&bp
->blk_dva
[i
]))
4631 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
4633 ASSERT3P(vd
, !=, NULL
);
4635 metaslab_check_free_impl(vd
, offset
, size
);
4637 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4640 #if defined(_KERNEL)
4642 module_param(metaslab_aliquot
, ulong
, 0644);
4643 MODULE_PARM_DESC(metaslab_aliquot
,
4644 "allocation granularity (a.k.a. stripe size)");
4646 module_param(metaslab_debug_load
, int, 0644);
4647 MODULE_PARM_DESC(metaslab_debug_load
,
4648 "load all metaslabs when pool is first opened");
4650 module_param(metaslab_debug_unload
, int, 0644);
4651 MODULE_PARM_DESC(metaslab_debug_unload
,
4652 "prevent metaslabs from being unloaded");
4654 module_param(metaslab_preload_enabled
, int, 0644);
4655 MODULE_PARM_DESC(metaslab_preload_enabled
,
4656 "preload potential metaslabs during reassessment");
4658 module_param(zfs_mg_noalloc_threshold
, int, 0644);
4659 MODULE_PARM_DESC(zfs_mg_noalloc_threshold
,
4660 "percentage of free space for metaslab group to allow allocation");
4662 module_param(zfs_mg_fragmentation_threshold
, int, 0644);
4663 MODULE_PARM_DESC(zfs_mg_fragmentation_threshold
,
4664 "fragmentation for metaslab group to allow allocation");
4666 module_param(zfs_metaslab_fragmentation_threshold
, int, 0644);
4667 MODULE_PARM_DESC(zfs_metaslab_fragmentation_threshold
,
4668 "fragmentation for metaslab to allow allocation");
4670 module_param(metaslab_fragmentation_factor_enabled
, int, 0644);
4671 MODULE_PARM_DESC(metaslab_fragmentation_factor_enabled
,
4672 "use the fragmentation metric to prefer less fragmented metaslabs");
4674 module_param(metaslab_lba_weighting_enabled
, int, 0644);
4675 MODULE_PARM_DESC(metaslab_lba_weighting_enabled
,
4676 "prefer metaslabs with lower LBAs");
4678 module_param(metaslab_bias_enabled
, int, 0644);
4679 MODULE_PARM_DESC(metaslab_bias_enabled
,
4680 "enable metaslab group biasing");
4682 module_param(zfs_metaslab_segment_weight_enabled
, int, 0644);
4683 MODULE_PARM_DESC(zfs_metaslab_segment_weight_enabled
,
4684 "enable segment-based metaslab selection");
4686 module_param(zfs_metaslab_switch_threshold
, int, 0644);
4687 MODULE_PARM_DESC(zfs_metaslab_switch_threshold
,
4688 "segment-based metaslab selection maximum buckets before switching");
4690 module_param(metaslab_force_ganging
, ulong
, 0644);
4691 MODULE_PARM_DESC(metaslab_force_ganging
,
4692 "blocks larger than this size are forced to be gang blocks");