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 * Verify that the space accounting on disk matches the in-core range_trees.
503 metaslab_verify_space(metaslab_t
*msp
, uint64_t txg
)
505 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
506 uint64_t allocated
= 0;
507 uint64_t sm_free_space
, msp_free_space
;
509 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
511 if ((zfs_flags
& ZFS_DEBUG_METASLAB_VERIFY
) == 0)
515 * We can only verify the metaslab space when we're called
516 * from syncing context with a loaded metaslab that has an allocated
517 * space map. Calling this in non-syncing context does not
518 * provide a consistent view of the metaslab since we're performing
519 * allocations in the future.
521 if (txg
!= spa_syncing_txg(spa
) || msp
->ms_sm
== NULL
||
525 sm_free_space
= msp
->ms_size
- space_map_allocated(msp
->ms_sm
) -
526 space_map_alloc_delta(msp
->ms_sm
);
529 * Account for future allocations since we would have already
530 * deducted that space from the ms_freetree.
532 for (int t
= 0; t
< TXG_CONCURRENT_STATES
; t
++) {
534 range_tree_space(msp
->ms_allocating
[(txg
+ t
) & TXG_MASK
]);
537 msp_free_space
= range_tree_space(msp
->ms_allocatable
) + allocated
+
538 msp
->ms_deferspace
+ range_tree_space(msp
->ms_freed
);
540 VERIFY3U(sm_free_space
, ==, msp_free_space
);
544 * ==========================================================================
546 * ==========================================================================
549 * Update the allocatable flag and the metaslab group's capacity.
550 * The allocatable flag is set to true if the capacity is below
551 * the zfs_mg_noalloc_threshold or has a fragmentation value that is
552 * greater than zfs_mg_fragmentation_threshold. If a metaslab group
553 * transitions from allocatable to non-allocatable or vice versa then the
554 * metaslab group's class is updated to reflect the transition.
557 metaslab_group_alloc_update(metaslab_group_t
*mg
)
559 vdev_t
*vd
= mg
->mg_vd
;
560 metaslab_class_t
*mc
= mg
->mg_class
;
561 vdev_stat_t
*vs
= &vd
->vdev_stat
;
562 boolean_t was_allocatable
;
563 boolean_t was_initialized
;
565 ASSERT(vd
== vd
->vdev_top
);
566 ASSERT3U(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_READER
), ==,
569 mutex_enter(&mg
->mg_lock
);
570 was_allocatable
= mg
->mg_allocatable
;
571 was_initialized
= mg
->mg_initialized
;
573 mg
->mg_free_capacity
= ((vs
->vs_space
- vs
->vs_alloc
) * 100) /
576 mutex_enter(&mc
->mc_lock
);
579 * If the metaslab group was just added then it won't
580 * have any space until we finish syncing out this txg.
581 * At that point we will consider it initialized and available
582 * for allocations. We also don't consider non-activated
583 * metaslab groups (e.g. vdevs that are in the middle of being removed)
584 * to be initialized, because they can't be used for allocation.
586 mg
->mg_initialized
= metaslab_group_initialized(mg
);
587 if (!was_initialized
&& mg
->mg_initialized
) {
589 } else if (was_initialized
&& !mg
->mg_initialized
) {
590 ASSERT3U(mc
->mc_groups
, >, 0);
593 if (mg
->mg_initialized
)
594 mg
->mg_no_free_space
= B_FALSE
;
597 * A metaslab group is considered allocatable if it has plenty
598 * of free space or is not heavily fragmented. We only take
599 * fragmentation into account if the metaslab group has a valid
600 * fragmentation metric (i.e. a value between 0 and 100).
602 mg
->mg_allocatable
= (mg
->mg_activation_count
> 0 &&
603 mg
->mg_free_capacity
> zfs_mg_noalloc_threshold
&&
604 (mg
->mg_fragmentation
== ZFS_FRAG_INVALID
||
605 mg
->mg_fragmentation
<= zfs_mg_fragmentation_threshold
));
608 * The mc_alloc_groups maintains a count of the number of
609 * groups in this metaslab class that are still above the
610 * zfs_mg_noalloc_threshold. This is used by the allocating
611 * threads to determine if they should avoid allocations to
612 * a given group. The allocator will avoid allocations to a group
613 * if that group has reached or is below the zfs_mg_noalloc_threshold
614 * and there are still other groups that are above the threshold.
615 * When a group transitions from allocatable to non-allocatable or
616 * vice versa we update the metaslab class to reflect that change.
617 * When the mc_alloc_groups value drops to 0 that means that all
618 * groups have reached the zfs_mg_noalloc_threshold making all groups
619 * eligible for allocations. This effectively means that all devices
620 * are balanced again.
622 if (was_allocatable
&& !mg
->mg_allocatable
)
623 mc
->mc_alloc_groups
--;
624 else if (!was_allocatable
&& mg
->mg_allocatable
)
625 mc
->mc_alloc_groups
++;
626 mutex_exit(&mc
->mc_lock
);
628 mutex_exit(&mg
->mg_lock
);
632 metaslab_group_create(metaslab_class_t
*mc
, vdev_t
*vd
, int allocators
)
634 metaslab_group_t
*mg
;
636 mg
= kmem_zalloc(sizeof (metaslab_group_t
), KM_SLEEP
);
637 mutex_init(&mg
->mg_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
638 mutex_init(&mg
->mg_ms_initialize_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
639 cv_init(&mg
->mg_ms_initialize_cv
, NULL
, CV_DEFAULT
, NULL
);
640 mg
->mg_primaries
= kmem_zalloc(allocators
* sizeof (metaslab_t
*),
642 mg
->mg_secondaries
= kmem_zalloc(allocators
* sizeof (metaslab_t
*),
644 avl_create(&mg
->mg_metaslab_tree
, metaslab_compare
,
645 sizeof (metaslab_t
), offsetof(struct metaslab
, ms_group_node
));
648 mg
->mg_activation_count
= 0;
649 mg
->mg_initialized
= B_FALSE
;
650 mg
->mg_no_free_space
= B_TRUE
;
651 mg
->mg_allocators
= allocators
;
653 mg
->mg_alloc_queue_depth
= kmem_zalloc(allocators
*
654 sizeof (zfs_refcount_t
), KM_SLEEP
);
655 mg
->mg_cur_max_alloc_queue_depth
= kmem_zalloc(allocators
*
656 sizeof (uint64_t), KM_SLEEP
);
657 for (int i
= 0; i
< allocators
; i
++) {
658 zfs_refcount_create_tracked(&mg
->mg_alloc_queue_depth
[i
]);
659 mg
->mg_cur_max_alloc_queue_depth
[i
] = 0;
662 mg
->mg_taskq
= taskq_create("metaslab_group_taskq", metaslab_load_pct
,
663 maxclsyspri
, 10, INT_MAX
, TASKQ_THREADS_CPU_PCT
| TASKQ_DYNAMIC
);
669 metaslab_group_destroy(metaslab_group_t
*mg
)
671 ASSERT(mg
->mg_prev
== NULL
);
672 ASSERT(mg
->mg_next
== NULL
);
674 * We may have gone below zero with the activation count
675 * either because we never activated in the first place or
676 * because we're done, and possibly removing the vdev.
678 ASSERT(mg
->mg_activation_count
<= 0);
680 taskq_destroy(mg
->mg_taskq
);
681 avl_destroy(&mg
->mg_metaslab_tree
);
682 kmem_free(mg
->mg_primaries
, mg
->mg_allocators
* sizeof (metaslab_t
*));
683 kmem_free(mg
->mg_secondaries
, mg
->mg_allocators
*
684 sizeof (metaslab_t
*));
685 mutex_destroy(&mg
->mg_lock
);
686 mutex_destroy(&mg
->mg_ms_initialize_lock
);
687 cv_destroy(&mg
->mg_ms_initialize_cv
);
689 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
690 zfs_refcount_destroy(&mg
->mg_alloc_queue_depth
[i
]);
691 mg
->mg_cur_max_alloc_queue_depth
[i
] = 0;
693 kmem_free(mg
->mg_alloc_queue_depth
, mg
->mg_allocators
*
694 sizeof (zfs_refcount_t
));
695 kmem_free(mg
->mg_cur_max_alloc_queue_depth
, mg
->mg_allocators
*
698 kmem_free(mg
, sizeof (metaslab_group_t
));
702 metaslab_group_activate(metaslab_group_t
*mg
)
704 metaslab_class_t
*mc
= mg
->mg_class
;
705 metaslab_group_t
*mgprev
, *mgnext
;
707 ASSERT3U(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_WRITER
), !=, 0);
709 ASSERT(mc
->mc_rotor
!= mg
);
710 ASSERT(mg
->mg_prev
== NULL
);
711 ASSERT(mg
->mg_next
== NULL
);
712 ASSERT(mg
->mg_activation_count
<= 0);
714 if (++mg
->mg_activation_count
<= 0)
717 mg
->mg_aliquot
= metaslab_aliquot
* MAX(1, mg
->mg_vd
->vdev_children
);
718 metaslab_group_alloc_update(mg
);
720 if ((mgprev
= mc
->mc_rotor
) == NULL
) {
724 mgnext
= mgprev
->mg_next
;
725 mg
->mg_prev
= mgprev
;
726 mg
->mg_next
= mgnext
;
727 mgprev
->mg_next
= mg
;
728 mgnext
->mg_prev
= mg
;
734 * Passivate a metaslab group and remove it from the allocation rotor.
735 * Callers must hold both the SCL_ALLOC and SCL_ZIO lock prior to passivating
736 * a metaslab group. This function will momentarily drop spa_config_locks
737 * that are lower than the SCL_ALLOC lock (see comment below).
740 metaslab_group_passivate(metaslab_group_t
*mg
)
742 metaslab_class_t
*mc
= mg
->mg_class
;
743 spa_t
*spa
= mc
->mc_spa
;
744 metaslab_group_t
*mgprev
, *mgnext
;
745 int locks
= spa_config_held(spa
, SCL_ALL
, RW_WRITER
);
747 ASSERT3U(spa_config_held(spa
, SCL_ALLOC
| SCL_ZIO
, RW_WRITER
), ==,
748 (SCL_ALLOC
| SCL_ZIO
));
750 if (--mg
->mg_activation_count
!= 0) {
751 ASSERT(mc
->mc_rotor
!= mg
);
752 ASSERT(mg
->mg_prev
== NULL
);
753 ASSERT(mg
->mg_next
== NULL
);
754 ASSERT(mg
->mg_activation_count
< 0);
759 * The spa_config_lock is an array of rwlocks, ordered as
760 * follows (from highest to lowest):
761 * SCL_CONFIG > SCL_STATE > SCL_L2ARC > SCL_ALLOC >
762 * SCL_ZIO > SCL_FREE > SCL_VDEV
763 * (For more information about the spa_config_lock see spa_misc.c)
764 * The higher the lock, the broader its coverage. When we passivate
765 * a metaslab group, we must hold both the SCL_ALLOC and the SCL_ZIO
766 * config locks. However, the metaslab group's taskq might be trying
767 * to preload metaslabs so we must drop the SCL_ZIO lock and any
768 * lower locks to allow the I/O to complete. At a minimum,
769 * we continue to hold the SCL_ALLOC lock, which prevents any future
770 * allocations from taking place and any changes to the vdev tree.
772 spa_config_exit(spa
, locks
& ~(SCL_ZIO
- 1), spa
);
773 taskq_wait_outstanding(mg
->mg_taskq
, 0);
774 spa_config_enter(spa
, locks
& ~(SCL_ZIO
- 1), spa
, RW_WRITER
);
775 metaslab_group_alloc_update(mg
);
776 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
777 metaslab_t
*msp
= mg
->mg_primaries
[i
];
779 mutex_enter(&msp
->ms_lock
);
780 metaslab_passivate(msp
,
781 metaslab_weight_from_range_tree(msp
));
782 mutex_exit(&msp
->ms_lock
);
784 msp
= mg
->mg_secondaries
[i
];
786 mutex_enter(&msp
->ms_lock
);
787 metaslab_passivate(msp
,
788 metaslab_weight_from_range_tree(msp
));
789 mutex_exit(&msp
->ms_lock
);
793 mgprev
= mg
->mg_prev
;
794 mgnext
= mg
->mg_next
;
799 mc
->mc_rotor
= mgnext
;
800 mgprev
->mg_next
= mgnext
;
801 mgnext
->mg_prev
= mgprev
;
809 metaslab_group_initialized(metaslab_group_t
*mg
)
811 vdev_t
*vd
= mg
->mg_vd
;
812 vdev_stat_t
*vs
= &vd
->vdev_stat
;
814 return (vs
->vs_space
!= 0 && mg
->mg_activation_count
> 0);
818 metaslab_group_get_space(metaslab_group_t
*mg
)
820 return ((1ULL << mg
->mg_vd
->vdev_ms_shift
) * mg
->mg_vd
->vdev_ms_count
);
824 metaslab_group_histogram_verify(metaslab_group_t
*mg
)
827 vdev_t
*vd
= mg
->mg_vd
;
828 uint64_t ashift
= vd
->vdev_ashift
;
831 if ((zfs_flags
& ZFS_DEBUG_HISTOGRAM_VERIFY
) == 0)
834 mg_hist
= kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE
,
837 ASSERT3U(RANGE_TREE_HISTOGRAM_SIZE
, >=,
838 SPACE_MAP_HISTOGRAM_SIZE
+ ashift
);
840 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
841 metaslab_t
*msp
= vd
->vdev_ms
[m
];
843 /* skip if not active or not a member */
844 if (msp
->ms_sm
== NULL
|| msp
->ms_group
!= mg
)
847 for (i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++)
848 mg_hist
[i
+ ashift
] +=
849 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
852 for (i
= 0; i
< RANGE_TREE_HISTOGRAM_SIZE
; i
++)
853 VERIFY3U(mg_hist
[i
], ==, mg
->mg_histogram
[i
]);
855 kmem_free(mg_hist
, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE
);
859 metaslab_group_histogram_add(metaslab_group_t
*mg
, metaslab_t
*msp
)
861 metaslab_class_t
*mc
= mg
->mg_class
;
862 uint64_t ashift
= mg
->mg_vd
->vdev_ashift
;
864 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
865 if (msp
->ms_sm
== NULL
)
868 mutex_enter(&mg
->mg_lock
);
869 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++) {
870 mg
->mg_histogram
[i
+ ashift
] +=
871 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
872 mc
->mc_histogram
[i
+ ashift
] +=
873 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
875 mutex_exit(&mg
->mg_lock
);
879 metaslab_group_histogram_remove(metaslab_group_t
*mg
, metaslab_t
*msp
)
881 metaslab_class_t
*mc
= mg
->mg_class
;
882 uint64_t ashift
= mg
->mg_vd
->vdev_ashift
;
884 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
885 if (msp
->ms_sm
== NULL
)
888 mutex_enter(&mg
->mg_lock
);
889 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++) {
890 ASSERT3U(mg
->mg_histogram
[i
+ ashift
], >=,
891 msp
->ms_sm
->sm_phys
->smp_histogram
[i
]);
892 ASSERT3U(mc
->mc_histogram
[i
+ ashift
], >=,
893 msp
->ms_sm
->sm_phys
->smp_histogram
[i
]);
895 mg
->mg_histogram
[i
+ ashift
] -=
896 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
897 mc
->mc_histogram
[i
+ ashift
] -=
898 msp
->ms_sm
->sm_phys
->smp_histogram
[i
];
900 mutex_exit(&mg
->mg_lock
);
904 metaslab_group_add(metaslab_group_t
*mg
, metaslab_t
*msp
)
906 ASSERT(msp
->ms_group
== NULL
);
907 mutex_enter(&mg
->mg_lock
);
910 avl_add(&mg
->mg_metaslab_tree
, msp
);
911 mutex_exit(&mg
->mg_lock
);
913 mutex_enter(&msp
->ms_lock
);
914 metaslab_group_histogram_add(mg
, msp
);
915 mutex_exit(&msp
->ms_lock
);
919 metaslab_group_remove(metaslab_group_t
*mg
, metaslab_t
*msp
)
921 mutex_enter(&msp
->ms_lock
);
922 metaslab_group_histogram_remove(mg
, msp
);
923 mutex_exit(&msp
->ms_lock
);
925 mutex_enter(&mg
->mg_lock
);
926 ASSERT(msp
->ms_group
== mg
);
927 avl_remove(&mg
->mg_metaslab_tree
, msp
);
928 msp
->ms_group
= NULL
;
929 mutex_exit(&mg
->mg_lock
);
933 metaslab_group_sort_impl(metaslab_group_t
*mg
, metaslab_t
*msp
, uint64_t weight
)
935 ASSERT(MUTEX_HELD(&mg
->mg_lock
));
936 ASSERT(msp
->ms_group
== mg
);
937 avl_remove(&mg
->mg_metaslab_tree
, msp
);
938 msp
->ms_weight
= weight
;
939 avl_add(&mg
->mg_metaslab_tree
, msp
);
944 metaslab_group_sort(metaslab_group_t
*mg
, metaslab_t
*msp
, uint64_t weight
)
947 * Although in principle the weight can be any value, in
948 * practice we do not use values in the range [1, 511].
950 ASSERT(weight
>= SPA_MINBLOCKSIZE
|| weight
== 0);
951 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
953 mutex_enter(&mg
->mg_lock
);
954 metaslab_group_sort_impl(mg
, msp
, weight
);
955 mutex_exit(&mg
->mg_lock
);
959 * Calculate the fragmentation for a given metaslab group. We can use
960 * a simple average here since all metaslabs within the group must have
961 * the same size. The return value will be a value between 0 and 100
962 * (inclusive), or ZFS_FRAG_INVALID if less than half of the metaslab in this
963 * group have a fragmentation metric.
966 metaslab_group_fragmentation(metaslab_group_t
*mg
)
968 vdev_t
*vd
= mg
->mg_vd
;
969 uint64_t fragmentation
= 0;
970 uint64_t valid_ms
= 0;
972 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
973 metaslab_t
*msp
= vd
->vdev_ms
[m
];
975 if (msp
->ms_fragmentation
== ZFS_FRAG_INVALID
)
977 if (msp
->ms_group
!= mg
)
981 fragmentation
+= msp
->ms_fragmentation
;
984 if (valid_ms
<= mg
->mg_vd
->vdev_ms_count
/ 2)
985 return (ZFS_FRAG_INVALID
);
987 fragmentation
/= valid_ms
;
988 ASSERT3U(fragmentation
, <=, 100);
989 return (fragmentation
);
993 * Determine if a given metaslab group should skip allocations. A metaslab
994 * group should avoid allocations if its free capacity is less than the
995 * zfs_mg_noalloc_threshold or its fragmentation metric is greater than
996 * zfs_mg_fragmentation_threshold and there is at least one metaslab group
997 * that can still handle allocations. If the allocation throttle is enabled
998 * then we skip allocations to devices that have reached their maximum
999 * allocation queue depth unless the selected metaslab group is the only
1000 * eligible group remaining.
1003 metaslab_group_allocatable(metaslab_group_t
*mg
, metaslab_group_t
*rotor
,
1004 uint64_t psize
, int allocator
, int d
)
1006 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
1007 metaslab_class_t
*mc
= mg
->mg_class
;
1010 * We can only consider skipping this metaslab group if it's
1011 * in the normal metaslab class and there are other metaslab
1012 * groups to select from. Otherwise, we always consider it eligible
1015 if ((mc
!= spa_normal_class(spa
) &&
1016 mc
!= spa_special_class(spa
) &&
1017 mc
!= spa_dedup_class(spa
)) ||
1022 * If the metaslab group's mg_allocatable flag is set (see comments
1023 * in metaslab_group_alloc_update() for more information) and
1024 * the allocation throttle is disabled then allow allocations to this
1025 * device. However, if the allocation throttle is enabled then
1026 * check if we have reached our allocation limit (mg_alloc_queue_depth)
1027 * to determine if we should allow allocations to this metaslab group.
1028 * If all metaslab groups are no longer considered allocatable
1029 * (mc_alloc_groups == 0) or we're trying to allocate the smallest
1030 * gang block size then we allow allocations on this metaslab group
1031 * regardless of the mg_allocatable or throttle settings.
1033 if (mg
->mg_allocatable
) {
1034 metaslab_group_t
*mgp
;
1036 uint64_t qmax
= mg
->mg_cur_max_alloc_queue_depth
[allocator
];
1038 if (!mc
->mc_alloc_throttle_enabled
)
1042 * If this metaslab group does not have any free space, then
1043 * there is no point in looking further.
1045 if (mg
->mg_no_free_space
)
1049 * Relax allocation throttling for ditto blocks. Due to
1050 * random imbalances in allocation it tends to push copies
1051 * to one vdev, that looks a bit better at the moment.
1053 qmax
= qmax
* (4 + d
) / 4;
1055 qdepth
= zfs_refcount_count(
1056 &mg
->mg_alloc_queue_depth
[allocator
]);
1059 * If this metaslab group is below its qmax or it's
1060 * the only allocatable metasable group, then attempt
1061 * to allocate from it.
1063 if (qdepth
< qmax
|| mc
->mc_alloc_groups
== 1)
1065 ASSERT3U(mc
->mc_alloc_groups
, >, 1);
1068 * Since this metaslab group is at or over its qmax, we
1069 * need to determine if there are metaslab groups after this
1070 * one that might be able to handle this allocation. This is
1071 * racy since we can't hold the locks for all metaslab
1072 * groups at the same time when we make this check.
1074 for (mgp
= mg
->mg_next
; mgp
!= rotor
; mgp
= mgp
->mg_next
) {
1075 qmax
= mgp
->mg_cur_max_alloc_queue_depth
[allocator
];
1076 qmax
= qmax
* (4 + d
) / 4;
1077 qdepth
= zfs_refcount_count(
1078 &mgp
->mg_alloc_queue_depth
[allocator
]);
1081 * If there is another metaslab group that
1082 * might be able to handle the allocation, then
1083 * we return false so that we skip this group.
1085 if (qdepth
< qmax
&& !mgp
->mg_no_free_space
)
1090 * We didn't find another group to handle the allocation
1091 * so we can't skip this metaslab group even though
1092 * we are at or over our qmax.
1096 } else if (mc
->mc_alloc_groups
== 0 || psize
== SPA_MINBLOCKSIZE
) {
1103 * ==========================================================================
1104 * Range tree callbacks
1105 * ==========================================================================
1109 * Comparison function for the private size-ordered tree. Tree is sorted
1110 * by size, larger sizes at the end of the tree.
1113 metaslab_rangesize_compare(const void *x1
, const void *x2
)
1115 const range_seg_t
*r1
= x1
;
1116 const range_seg_t
*r2
= x2
;
1117 uint64_t rs_size1
= r1
->rs_end
- r1
->rs_start
;
1118 uint64_t rs_size2
= r2
->rs_end
- r2
->rs_start
;
1120 int cmp
= AVL_CMP(rs_size1
, rs_size2
);
1124 return (AVL_CMP(r1
->rs_start
, r2
->rs_start
));
1128 * ==========================================================================
1129 * Common allocator routines
1130 * ==========================================================================
1134 * Return the maximum contiguous segment within the metaslab.
1137 metaslab_block_maxsize(metaslab_t
*msp
)
1139 avl_tree_t
*t
= &msp
->ms_allocatable_by_size
;
1142 if (t
== NULL
|| (rs
= avl_last(t
)) == NULL
)
1145 return (rs
->rs_end
- rs
->rs_start
);
1148 static range_seg_t
*
1149 metaslab_block_find(avl_tree_t
*t
, uint64_t start
, uint64_t size
)
1151 range_seg_t
*rs
, rsearch
;
1154 rsearch
.rs_start
= start
;
1155 rsearch
.rs_end
= start
+ size
;
1157 rs
= avl_find(t
, &rsearch
, &where
);
1159 rs
= avl_nearest(t
, where
, AVL_AFTER
);
1165 #if defined(WITH_FF_BLOCK_ALLOCATOR) || \
1166 defined(WITH_DF_BLOCK_ALLOCATOR) || \
1167 defined(WITH_CF_BLOCK_ALLOCATOR)
1169 * This is a helper function that can be used by the allocator to find
1170 * a suitable block to allocate. This will search the specified AVL
1171 * tree looking for a block that matches the specified criteria.
1174 metaslab_block_picker(avl_tree_t
*t
, uint64_t *cursor
, uint64_t size
,
1177 range_seg_t
*rs
= metaslab_block_find(t
, *cursor
, size
);
1179 while (rs
!= NULL
) {
1180 uint64_t offset
= P2ROUNDUP(rs
->rs_start
, align
);
1182 if (offset
+ size
<= rs
->rs_end
) {
1183 *cursor
= offset
+ size
;
1186 rs
= AVL_NEXT(t
, rs
);
1190 * If we know we've searched the whole map (*cursor == 0), give up.
1191 * Otherwise, reset the cursor to the beginning and try again.
1197 return (metaslab_block_picker(t
, cursor
, size
, align
));
1199 #endif /* WITH_FF/DF/CF_BLOCK_ALLOCATOR */
1201 #if defined(WITH_FF_BLOCK_ALLOCATOR)
1203 * ==========================================================================
1204 * The first-fit block allocator
1205 * ==========================================================================
1208 metaslab_ff_alloc(metaslab_t
*msp
, uint64_t size
)
1211 * Find the largest power of 2 block size that evenly divides the
1212 * requested size. This is used to try to allocate blocks with similar
1213 * alignment from the same area of the metaslab (i.e. same cursor
1214 * bucket) but it does not guarantee that other allocations sizes
1215 * may exist in the same region.
1217 uint64_t align
= size
& -size
;
1218 uint64_t *cursor
= &msp
->ms_lbas
[highbit64(align
) - 1];
1219 avl_tree_t
*t
= &msp
->ms_allocatable
->rt_root
;
1221 return (metaslab_block_picker(t
, cursor
, size
, align
));
1224 static metaslab_ops_t metaslab_ff_ops
= {
1228 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_ff_ops
;
1229 #endif /* WITH_FF_BLOCK_ALLOCATOR */
1231 #if defined(WITH_DF_BLOCK_ALLOCATOR)
1233 * ==========================================================================
1234 * Dynamic block allocator -
1235 * Uses the first fit allocation scheme until space get low and then
1236 * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
1237 * and metaslab_df_free_pct to determine when to switch the allocation scheme.
1238 * ==========================================================================
1241 metaslab_df_alloc(metaslab_t
*msp
, uint64_t size
)
1244 * Find the largest power of 2 block size that evenly divides the
1245 * requested size. This is used to try to allocate blocks with similar
1246 * alignment from the same area of the metaslab (i.e. same cursor
1247 * bucket) but it does not guarantee that other allocations sizes
1248 * may exist in the same region.
1250 uint64_t align
= size
& -size
;
1251 uint64_t *cursor
= &msp
->ms_lbas
[highbit64(align
) - 1];
1252 range_tree_t
*rt
= msp
->ms_allocatable
;
1253 avl_tree_t
*t
= &rt
->rt_root
;
1254 uint64_t max_size
= metaslab_block_maxsize(msp
);
1255 int free_pct
= range_tree_space(rt
) * 100 / msp
->ms_size
;
1257 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1258 ASSERT3U(avl_numnodes(t
), ==,
1259 avl_numnodes(&msp
->ms_allocatable_by_size
));
1261 if (max_size
< size
)
1265 * If we're running low on space switch to using the size
1266 * sorted AVL tree (best-fit).
1268 if (max_size
< metaslab_df_alloc_threshold
||
1269 free_pct
< metaslab_df_free_pct
) {
1270 t
= &msp
->ms_allocatable_by_size
;
1274 return (metaslab_block_picker(t
, cursor
, size
, 1ULL));
1277 static metaslab_ops_t metaslab_df_ops
= {
1281 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_df_ops
;
1282 #endif /* WITH_DF_BLOCK_ALLOCATOR */
1284 #if defined(WITH_CF_BLOCK_ALLOCATOR)
1286 * ==========================================================================
1287 * Cursor fit block allocator -
1288 * Select the largest region in the metaslab, set the cursor to the beginning
1289 * of the range and the cursor_end to the end of the range. As allocations
1290 * are made advance the cursor. Continue allocating from the cursor until
1291 * the range is exhausted and then find a new range.
1292 * ==========================================================================
1295 metaslab_cf_alloc(metaslab_t
*msp
, uint64_t size
)
1297 range_tree_t
*rt
= msp
->ms_allocatable
;
1298 avl_tree_t
*t
= &msp
->ms_allocatable_by_size
;
1299 uint64_t *cursor
= &msp
->ms_lbas
[0];
1300 uint64_t *cursor_end
= &msp
->ms_lbas
[1];
1301 uint64_t offset
= 0;
1303 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1304 ASSERT3U(avl_numnodes(t
), ==, avl_numnodes(&rt
->rt_root
));
1306 ASSERT3U(*cursor_end
, >=, *cursor
);
1308 if ((*cursor
+ size
) > *cursor_end
) {
1311 rs
= avl_last(&msp
->ms_allocatable_by_size
);
1312 if (rs
== NULL
|| (rs
->rs_end
- rs
->rs_start
) < size
)
1315 *cursor
= rs
->rs_start
;
1316 *cursor_end
= rs
->rs_end
;
1325 static metaslab_ops_t metaslab_cf_ops
= {
1329 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_cf_ops
;
1330 #endif /* WITH_CF_BLOCK_ALLOCATOR */
1332 #if defined(WITH_NDF_BLOCK_ALLOCATOR)
1334 * ==========================================================================
1335 * New dynamic fit allocator -
1336 * Select a region that is large enough to allocate 2^metaslab_ndf_clump_shift
1337 * contiguous blocks. If no region is found then just use the largest segment
1339 * ==========================================================================
1343 * Determines desired number of contiguous blocks (2^metaslab_ndf_clump_shift)
1344 * to request from the allocator.
1346 uint64_t metaslab_ndf_clump_shift
= 4;
1349 metaslab_ndf_alloc(metaslab_t
*msp
, uint64_t size
)
1351 avl_tree_t
*t
= &msp
->ms_allocatable
->rt_root
;
1353 range_seg_t
*rs
, rsearch
;
1354 uint64_t hbit
= highbit64(size
);
1355 uint64_t *cursor
= &msp
->ms_lbas
[hbit
- 1];
1356 uint64_t max_size
= metaslab_block_maxsize(msp
);
1358 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1359 ASSERT3U(avl_numnodes(t
), ==,
1360 avl_numnodes(&msp
->ms_allocatable_by_size
));
1362 if (max_size
< size
)
1365 rsearch
.rs_start
= *cursor
;
1366 rsearch
.rs_end
= *cursor
+ size
;
1368 rs
= avl_find(t
, &rsearch
, &where
);
1369 if (rs
== NULL
|| (rs
->rs_end
- rs
->rs_start
) < size
) {
1370 t
= &msp
->ms_allocatable_by_size
;
1372 rsearch
.rs_start
= 0;
1373 rsearch
.rs_end
= MIN(max_size
,
1374 1ULL << (hbit
+ metaslab_ndf_clump_shift
));
1375 rs
= avl_find(t
, &rsearch
, &where
);
1377 rs
= avl_nearest(t
, where
, AVL_AFTER
);
1381 if ((rs
->rs_end
- rs
->rs_start
) >= size
) {
1382 *cursor
= rs
->rs_start
+ size
;
1383 return (rs
->rs_start
);
1388 static metaslab_ops_t metaslab_ndf_ops
= {
1392 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_ndf_ops
;
1393 #endif /* WITH_NDF_BLOCK_ALLOCATOR */
1397 * ==========================================================================
1399 * ==========================================================================
1403 * Wait for any in-progress metaslab loads to complete.
1406 metaslab_load_wait(metaslab_t
*msp
)
1408 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1410 while (msp
->ms_loading
) {
1411 ASSERT(!msp
->ms_loaded
);
1412 cv_wait(&msp
->ms_load_cv
, &msp
->ms_lock
);
1417 metaslab_load_impl(metaslab_t
*msp
)
1421 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1422 ASSERT(msp
->ms_loading
);
1425 * Nobody else can manipulate a loading metaslab, so it's now safe
1426 * to drop the lock. This way we don't have to hold the lock while
1427 * reading the spacemap from disk.
1429 mutex_exit(&msp
->ms_lock
);
1432 * If the space map has not been allocated yet, then treat
1433 * all the space in the metaslab as free and add it to ms_allocatable.
1435 if (msp
->ms_sm
!= NULL
) {
1436 error
= space_map_load(msp
->ms_sm
, msp
->ms_allocatable
,
1439 range_tree_add(msp
->ms_allocatable
,
1440 msp
->ms_start
, msp
->ms_size
);
1443 mutex_enter(&msp
->ms_lock
);
1448 ASSERT3P(msp
->ms_group
, !=, NULL
);
1449 msp
->ms_loaded
= B_TRUE
;
1452 * If the metaslab already has a spacemap, then we need to
1453 * remove all segments from the defer tree; otherwise, the
1454 * metaslab is completely empty and we can skip this.
1456 if (msp
->ms_sm
!= NULL
) {
1457 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1458 range_tree_walk(msp
->ms_defer
[t
],
1459 range_tree_remove
, msp
->ms_allocatable
);
1462 msp
->ms_max_size
= metaslab_block_maxsize(msp
);
1468 metaslab_load(metaslab_t
*msp
)
1470 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1473 * There may be another thread loading the same metaslab, if that's
1474 * the case just wait until the other thread is done and return.
1476 metaslab_load_wait(msp
);
1479 VERIFY(!msp
->ms_loading
);
1481 msp
->ms_loading
= B_TRUE
;
1482 int error
= metaslab_load_impl(msp
);
1483 msp
->ms_loading
= B_FALSE
;
1484 cv_broadcast(&msp
->ms_load_cv
);
1490 metaslab_unload(metaslab_t
*msp
)
1492 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1493 range_tree_vacate(msp
->ms_allocatable
, NULL
, NULL
);
1494 msp
->ms_loaded
= B_FALSE
;
1495 msp
->ms_weight
&= ~METASLAB_ACTIVE_MASK
;
1496 msp
->ms_max_size
= 0;
1500 metaslab_space_update(vdev_t
*vd
, metaslab_class_t
*mc
, int64_t alloc_delta
,
1501 int64_t defer_delta
, int64_t space_delta
)
1503 vdev_space_update(vd
, alloc_delta
, defer_delta
, space_delta
);
1505 ASSERT3P(vd
->vdev_spa
->spa_root_vdev
, ==, vd
->vdev_parent
);
1506 ASSERT(vd
->vdev_ms_count
!= 0);
1508 metaslab_class_space_update(mc
, alloc_delta
, defer_delta
, space_delta
,
1509 vdev_deflated_space(vd
, space_delta
));
1513 metaslab_init(metaslab_group_t
*mg
, uint64_t id
, uint64_t object
, uint64_t txg
,
1516 vdev_t
*vd
= mg
->mg_vd
;
1517 spa_t
*spa
= vd
->vdev_spa
;
1518 objset_t
*mos
= spa
->spa_meta_objset
;
1522 ms
= kmem_zalloc(sizeof (metaslab_t
), KM_SLEEP
);
1523 mutex_init(&ms
->ms_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1524 mutex_init(&ms
->ms_sync_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1525 cv_init(&ms
->ms_load_cv
, NULL
, CV_DEFAULT
, NULL
);
1528 ms
->ms_start
= id
<< vd
->vdev_ms_shift
;
1529 ms
->ms_size
= 1ULL << vd
->vdev_ms_shift
;
1530 ms
->ms_allocator
= -1;
1531 ms
->ms_new
= B_TRUE
;
1534 * We only open space map objects that already exist. All others
1535 * will be opened when we finally allocate an object for it.
1538 error
= space_map_open(&ms
->ms_sm
, mos
, object
, ms
->ms_start
,
1539 ms
->ms_size
, vd
->vdev_ashift
);
1542 kmem_free(ms
, sizeof (metaslab_t
));
1546 ASSERT(ms
->ms_sm
!= NULL
);
1550 * We create the main range tree here, but we don't create the
1551 * other range trees until metaslab_sync_done(). This serves
1552 * two purposes: it allows metaslab_sync_done() to detect the
1553 * addition of new space; and for debugging, it ensures that we'd
1554 * data fault on any attempt to use this metaslab before it's ready.
1556 ms
->ms_allocatable
= range_tree_create_impl(&rt_avl_ops
,
1557 &ms
->ms_allocatable_by_size
, metaslab_rangesize_compare
, 0);
1558 metaslab_group_add(mg
, ms
);
1560 metaslab_set_fragmentation(ms
);
1563 * If we're opening an existing pool (txg == 0) or creating
1564 * a new one (txg == TXG_INITIAL), all space is available now.
1565 * If we're adding space to an existing pool, the new space
1566 * does not become available until after this txg has synced.
1567 * The metaslab's weight will also be initialized when we sync
1568 * out this txg. This ensures that we don't attempt to allocate
1569 * from it before we have initialized it completely.
1571 if (txg
<= TXG_INITIAL
)
1572 metaslab_sync_done(ms
, 0);
1575 * If metaslab_debug_load is set and we're initializing a metaslab
1576 * that has an allocated space map object then load the space map
1577 * so that we can verify frees.
1579 if (metaslab_debug_load
&& ms
->ms_sm
!= NULL
) {
1580 mutex_enter(&ms
->ms_lock
);
1581 VERIFY0(metaslab_load(ms
));
1582 mutex_exit(&ms
->ms_lock
);
1586 vdev_dirty(vd
, 0, NULL
, txg
);
1587 vdev_dirty(vd
, VDD_METASLAB
, ms
, txg
);
1596 metaslab_fini(metaslab_t
*msp
)
1598 metaslab_group_t
*mg
= msp
->ms_group
;
1599 vdev_t
*vd
= mg
->mg_vd
;
1601 metaslab_group_remove(mg
, msp
);
1603 mutex_enter(&msp
->ms_lock
);
1604 VERIFY(msp
->ms_group
== NULL
);
1605 metaslab_space_update(vd
, mg
->mg_class
,
1606 -space_map_allocated(msp
->ms_sm
), 0, -msp
->ms_size
);
1608 space_map_close(msp
->ms_sm
);
1610 metaslab_unload(msp
);
1612 range_tree_destroy(msp
->ms_allocatable
);
1613 range_tree_destroy(msp
->ms_freeing
);
1614 range_tree_destroy(msp
->ms_freed
);
1616 for (int t
= 0; t
< TXG_SIZE
; t
++) {
1617 range_tree_destroy(msp
->ms_allocating
[t
]);
1620 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1621 range_tree_destroy(msp
->ms_defer
[t
]);
1623 ASSERT0(msp
->ms_deferspace
);
1625 range_tree_destroy(msp
->ms_checkpointing
);
1627 mutex_exit(&msp
->ms_lock
);
1628 cv_destroy(&msp
->ms_load_cv
);
1629 mutex_destroy(&msp
->ms_lock
);
1630 mutex_destroy(&msp
->ms_sync_lock
);
1631 ASSERT3U(msp
->ms_allocator
, ==, -1);
1633 kmem_free(msp
, sizeof (metaslab_t
));
1636 #define FRAGMENTATION_TABLE_SIZE 17
1639 * This table defines a segment size based fragmentation metric that will
1640 * allow each metaslab to derive its own fragmentation value. This is done
1641 * by calculating the space in each bucket of the spacemap histogram and
1642 * multiplying that by the fragmetation metric in this table. Doing
1643 * this for all buckets and dividing it by the total amount of free
1644 * space in this metaslab (i.e. the total free space in all buckets) gives
1645 * us the fragmentation metric. This means that a high fragmentation metric
1646 * equates to most of the free space being comprised of small segments.
1647 * Conversely, if the metric is low, then most of the free space is in
1648 * large segments. A 10% change in fragmentation equates to approximately
1649 * double the number of segments.
1651 * This table defines 0% fragmented space using 16MB segments. Testing has
1652 * shown that segments that are greater than or equal to 16MB do not suffer
1653 * from drastic performance problems. Using this value, we derive the rest
1654 * of the table. Since the fragmentation value is never stored on disk, it
1655 * is possible to change these calculations in the future.
1657 int zfs_frag_table
[FRAGMENTATION_TABLE_SIZE
] = {
1677 * Calclate the metaslab's fragmentation metric. A return value
1678 * of ZFS_FRAG_INVALID means that the metaslab has not been upgraded and does
1679 * not support this metric. Otherwise, the return value should be in the
1683 metaslab_set_fragmentation(metaslab_t
*msp
)
1685 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
1686 uint64_t fragmentation
= 0;
1688 boolean_t feature_enabled
= spa_feature_is_enabled(spa
,
1689 SPA_FEATURE_SPACEMAP_HISTOGRAM
);
1691 if (!feature_enabled
) {
1692 msp
->ms_fragmentation
= ZFS_FRAG_INVALID
;
1697 * A null space map means that the entire metaslab is free
1698 * and thus is not fragmented.
1700 if (msp
->ms_sm
== NULL
) {
1701 msp
->ms_fragmentation
= 0;
1706 * If this metaslab's space map has not been upgraded, flag it
1707 * so that we upgrade next time we encounter it.
1709 if (msp
->ms_sm
->sm_dbuf
->db_size
!= sizeof (space_map_phys_t
)) {
1710 uint64_t txg
= spa_syncing_txg(spa
);
1711 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
1714 * If we've reached the final dirty txg, then we must
1715 * be shutting down the pool. We don't want to dirty
1716 * any data past this point so skip setting the condense
1717 * flag. We can retry this action the next time the pool
1720 if (spa_writeable(spa
) && txg
< spa_final_dirty_txg(spa
)) {
1721 msp
->ms_condense_wanted
= B_TRUE
;
1722 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
+ 1);
1723 zfs_dbgmsg("txg %llu, requesting force condense: "
1724 "ms_id %llu, vdev_id %llu", txg
, msp
->ms_id
,
1727 msp
->ms_fragmentation
= ZFS_FRAG_INVALID
;
1731 for (int i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE
; i
++) {
1733 uint8_t shift
= msp
->ms_sm
->sm_shift
;
1735 int idx
= MIN(shift
- SPA_MINBLOCKSHIFT
+ i
,
1736 FRAGMENTATION_TABLE_SIZE
- 1);
1738 if (msp
->ms_sm
->sm_phys
->smp_histogram
[i
] == 0)
1741 space
= msp
->ms_sm
->sm_phys
->smp_histogram
[i
] << (i
+ shift
);
1744 ASSERT3U(idx
, <, FRAGMENTATION_TABLE_SIZE
);
1745 fragmentation
+= space
* zfs_frag_table
[idx
];
1749 fragmentation
/= total
;
1750 ASSERT3U(fragmentation
, <=, 100);
1752 msp
->ms_fragmentation
= fragmentation
;
1756 * Compute a weight -- a selection preference value -- for the given metaslab.
1757 * This is based on the amount of free space, the level of fragmentation,
1758 * the LBA range, and whether the metaslab is loaded.
1761 metaslab_space_weight(metaslab_t
*msp
)
1763 metaslab_group_t
*mg
= msp
->ms_group
;
1764 vdev_t
*vd
= mg
->mg_vd
;
1765 uint64_t weight
, space
;
1767 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1768 ASSERT(!vd
->vdev_removing
);
1771 * The baseline weight is the metaslab's free space.
1773 space
= msp
->ms_size
- space_map_allocated(msp
->ms_sm
);
1775 if (metaslab_fragmentation_factor_enabled
&&
1776 msp
->ms_fragmentation
!= ZFS_FRAG_INVALID
) {
1778 * Use the fragmentation information to inversely scale
1779 * down the baseline weight. We need to ensure that we
1780 * don't exclude this metaslab completely when it's 100%
1781 * fragmented. To avoid this we reduce the fragmented value
1784 space
= (space
* (100 - (msp
->ms_fragmentation
- 1))) / 100;
1787 * If space < SPA_MINBLOCKSIZE, then we will not allocate from
1788 * this metaslab again. The fragmentation metric may have
1789 * decreased the space to something smaller than
1790 * SPA_MINBLOCKSIZE, so reset the space to SPA_MINBLOCKSIZE
1791 * so that we can consume any remaining space.
1793 if (space
> 0 && space
< SPA_MINBLOCKSIZE
)
1794 space
= SPA_MINBLOCKSIZE
;
1799 * Modern disks have uniform bit density and constant angular velocity.
1800 * Therefore, the outer recording zones are faster (higher bandwidth)
1801 * than the inner zones by the ratio of outer to inner track diameter,
1802 * which is typically around 2:1. We account for this by assigning
1803 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
1804 * In effect, this means that we'll select the metaslab with the most
1805 * free bandwidth rather than simply the one with the most free space.
1807 if (!vd
->vdev_nonrot
&& metaslab_lba_weighting_enabled
) {
1808 weight
= 2 * weight
- (msp
->ms_id
* weight
) / vd
->vdev_ms_count
;
1809 ASSERT(weight
>= space
&& weight
<= 2 * space
);
1813 * If this metaslab is one we're actively using, adjust its
1814 * weight to make it preferable to any inactive metaslab so
1815 * we'll polish it off. If the fragmentation on this metaslab
1816 * has exceed our threshold, then don't mark it active.
1818 if (msp
->ms_loaded
&& msp
->ms_fragmentation
!= ZFS_FRAG_INVALID
&&
1819 msp
->ms_fragmentation
<= zfs_metaslab_fragmentation_threshold
) {
1820 weight
|= (msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
1823 WEIGHT_SET_SPACEBASED(weight
);
1828 * Return the weight of the specified metaslab, according to the segment-based
1829 * weighting algorithm. The metaslab must be loaded. This function can
1830 * be called within a sync pass since it relies only on the metaslab's
1831 * range tree which is always accurate when the metaslab is loaded.
1834 metaslab_weight_from_range_tree(metaslab_t
*msp
)
1836 uint64_t weight
= 0;
1837 uint32_t segments
= 0;
1839 ASSERT(msp
->ms_loaded
);
1841 for (int i
= RANGE_TREE_HISTOGRAM_SIZE
- 1; i
>= SPA_MINBLOCKSHIFT
;
1843 uint8_t shift
= msp
->ms_group
->mg_vd
->vdev_ashift
;
1844 int max_idx
= SPACE_MAP_HISTOGRAM_SIZE
+ shift
- 1;
1847 segments
+= msp
->ms_allocatable
->rt_histogram
[i
];
1850 * The range tree provides more precision than the space map
1851 * and must be downgraded so that all values fit within the
1852 * space map's histogram. This allows us to compare loaded
1853 * vs. unloaded metaslabs to determine which metaslab is
1854 * considered "best".
1859 if (segments
!= 0) {
1860 WEIGHT_SET_COUNT(weight
, segments
);
1861 WEIGHT_SET_INDEX(weight
, i
);
1862 WEIGHT_SET_ACTIVE(weight
, 0);
1870 * Calculate the weight based on the on-disk histogram. This should only
1871 * be called after a sync pass has completely finished since the on-disk
1872 * information is updated in metaslab_sync().
1875 metaslab_weight_from_spacemap(metaslab_t
*msp
)
1877 uint64_t weight
= 0;
1879 for (int i
= SPACE_MAP_HISTOGRAM_SIZE
- 1; i
>= 0; i
--) {
1880 if (msp
->ms_sm
->sm_phys
->smp_histogram
[i
] != 0) {
1881 WEIGHT_SET_COUNT(weight
,
1882 msp
->ms_sm
->sm_phys
->smp_histogram
[i
]);
1883 WEIGHT_SET_INDEX(weight
, i
+
1884 msp
->ms_sm
->sm_shift
);
1885 WEIGHT_SET_ACTIVE(weight
, 0);
1893 * Compute a segment-based weight for the specified metaslab. The weight
1894 * is determined by highest bucket in the histogram. The information
1895 * for the highest bucket is encoded into the weight value.
1898 metaslab_segment_weight(metaslab_t
*msp
)
1900 metaslab_group_t
*mg
= msp
->ms_group
;
1901 uint64_t weight
= 0;
1902 uint8_t shift
= mg
->mg_vd
->vdev_ashift
;
1904 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1907 * The metaslab is completely free.
1909 if (space_map_allocated(msp
->ms_sm
) == 0) {
1910 int idx
= highbit64(msp
->ms_size
) - 1;
1911 int max_idx
= SPACE_MAP_HISTOGRAM_SIZE
+ shift
- 1;
1913 if (idx
< max_idx
) {
1914 WEIGHT_SET_COUNT(weight
, 1ULL);
1915 WEIGHT_SET_INDEX(weight
, idx
);
1917 WEIGHT_SET_COUNT(weight
, 1ULL << (idx
- max_idx
));
1918 WEIGHT_SET_INDEX(weight
, max_idx
);
1920 WEIGHT_SET_ACTIVE(weight
, 0);
1921 ASSERT(!WEIGHT_IS_SPACEBASED(weight
));
1926 ASSERT3U(msp
->ms_sm
->sm_dbuf
->db_size
, ==, sizeof (space_map_phys_t
));
1929 * If the metaslab is fully allocated then just make the weight 0.
1931 if (space_map_allocated(msp
->ms_sm
) == msp
->ms_size
)
1934 * If the metaslab is already loaded, then use the range tree to
1935 * determine the weight. Otherwise, we rely on the space map information
1936 * to generate the weight.
1938 if (msp
->ms_loaded
) {
1939 weight
= metaslab_weight_from_range_tree(msp
);
1941 weight
= metaslab_weight_from_spacemap(msp
);
1945 * If the metaslab was active the last time we calculated its weight
1946 * then keep it active. We want to consume the entire region that
1947 * is associated with this weight.
1949 if (msp
->ms_activation_weight
!= 0 && weight
!= 0)
1950 WEIGHT_SET_ACTIVE(weight
, WEIGHT_GET_ACTIVE(msp
->ms_weight
));
1955 * Determine if we should attempt to allocate from this metaslab. If the
1956 * metaslab has a maximum size then we can quickly determine if the desired
1957 * allocation size can be satisfied. Otherwise, if we're using segment-based
1958 * weighting then we can determine the maximum allocation that this metaslab
1959 * can accommodate based on the index encoded in the weight. If we're using
1960 * space-based weights then rely on the entire weight (excluding the weight
1964 metaslab_should_allocate(metaslab_t
*msp
, uint64_t asize
)
1966 boolean_t should_allocate
;
1968 if (msp
->ms_max_size
!= 0)
1969 return (msp
->ms_max_size
>= asize
);
1971 if (!WEIGHT_IS_SPACEBASED(msp
->ms_weight
)) {
1973 * The metaslab segment weight indicates segments in the
1974 * range [2^i, 2^(i+1)), where i is the index in the weight.
1975 * Since the asize might be in the middle of the range, we
1976 * should attempt the allocation if asize < 2^(i+1).
1978 should_allocate
= (asize
<
1979 1ULL << (WEIGHT_GET_INDEX(msp
->ms_weight
) + 1));
1981 should_allocate
= (asize
<=
1982 (msp
->ms_weight
& ~METASLAB_WEIGHT_TYPE
));
1984 return (should_allocate
);
1987 metaslab_weight(metaslab_t
*msp
)
1989 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
1990 spa_t
*spa
= vd
->vdev_spa
;
1993 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1996 * If this vdev is in the process of being removed, there is nothing
1997 * for us to do here.
1999 if (vd
->vdev_removing
)
2002 metaslab_set_fragmentation(msp
);
2005 * Update the maximum size if the metaslab is loaded. This will
2006 * ensure that we get an accurate maximum size if newly freed space
2007 * has been added back into the free tree.
2010 msp
->ms_max_size
= metaslab_block_maxsize(msp
);
2013 * Segment-based weighting requires space map histogram support.
2015 if (zfs_metaslab_segment_weight_enabled
&&
2016 spa_feature_is_enabled(spa
, SPA_FEATURE_SPACEMAP_HISTOGRAM
) &&
2017 (msp
->ms_sm
== NULL
|| msp
->ms_sm
->sm_dbuf
->db_size
==
2018 sizeof (space_map_phys_t
))) {
2019 weight
= metaslab_segment_weight(msp
);
2021 weight
= metaslab_space_weight(msp
);
2027 metaslab_activate_allocator(metaslab_group_t
*mg
, metaslab_t
*msp
,
2028 int allocator
, uint64_t activation_weight
)
2031 * If we're activating for the claim code, we don't want to actually
2032 * set the metaslab up for a specific allocator.
2034 if (activation_weight
== METASLAB_WEIGHT_CLAIM
)
2036 metaslab_t
**arr
= (activation_weight
== METASLAB_WEIGHT_PRIMARY
?
2037 mg
->mg_primaries
: mg
->mg_secondaries
);
2039 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2040 mutex_enter(&mg
->mg_lock
);
2041 if (arr
[allocator
] != NULL
) {
2042 mutex_exit(&mg
->mg_lock
);
2046 arr
[allocator
] = msp
;
2047 ASSERT3S(msp
->ms_allocator
, ==, -1);
2048 msp
->ms_allocator
= allocator
;
2049 msp
->ms_primary
= (activation_weight
== METASLAB_WEIGHT_PRIMARY
);
2050 mutex_exit(&mg
->mg_lock
);
2056 metaslab_activate(metaslab_t
*msp
, int allocator
, uint64_t activation_weight
)
2058 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2060 if ((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0) {
2061 int error
= metaslab_load(msp
);
2063 metaslab_group_sort(msp
->ms_group
, msp
, 0);
2066 if ((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) != 0) {
2068 * The metaslab was activated for another allocator
2069 * while we were waiting, we should reselect.
2071 return (SET_ERROR(EBUSY
));
2073 if ((error
= metaslab_activate_allocator(msp
->ms_group
, msp
,
2074 allocator
, activation_weight
)) != 0) {
2078 msp
->ms_activation_weight
= msp
->ms_weight
;
2079 metaslab_group_sort(msp
->ms_group
, msp
,
2080 msp
->ms_weight
| activation_weight
);
2082 ASSERT(msp
->ms_loaded
);
2083 ASSERT(msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
2089 metaslab_passivate_allocator(metaslab_group_t
*mg
, metaslab_t
*msp
,
2092 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2093 if (msp
->ms_weight
& METASLAB_WEIGHT_CLAIM
) {
2094 metaslab_group_sort(mg
, msp
, weight
);
2098 mutex_enter(&mg
->mg_lock
);
2099 ASSERT3P(msp
->ms_group
, ==, mg
);
2100 if (msp
->ms_primary
) {
2101 ASSERT3U(0, <=, msp
->ms_allocator
);
2102 ASSERT3U(msp
->ms_allocator
, <, mg
->mg_allocators
);
2103 ASSERT3P(mg
->mg_primaries
[msp
->ms_allocator
], ==, msp
);
2104 ASSERT(msp
->ms_weight
& METASLAB_WEIGHT_PRIMARY
);
2105 mg
->mg_primaries
[msp
->ms_allocator
] = NULL
;
2107 ASSERT(msp
->ms_weight
& METASLAB_WEIGHT_SECONDARY
);
2108 ASSERT3P(mg
->mg_secondaries
[msp
->ms_allocator
], ==, msp
);
2109 mg
->mg_secondaries
[msp
->ms_allocator
] = NULL
;
2111 msp
->ms_allocator
= -1;
2112 metaslab_group_sort_impl(mg
, msp
, weight
);
2113 mutex_exit(&mg
->mg_lock
);
2117 metaslab_passivate(metaslab_t
*msp
, uint64_t weight
)
2119 ASSERTV(uint64_t size
= weight
& ~METASLAB_WEIGHT_TYPE
);
2122 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
2123 * this metaslab again. In that case, it had better be empty,
2124 * or we would be leaving space on the table.
2126 ASSERT(!WEIGHT_IS_SPACEBASED(msp
->ms_weight
) ||
2127 size
>= SPA_MINBLOCKSIZE
||
2128 range_tree_space(msp
->ms_allocatable
) == 0);
2129 ASSERT0(weight
& METASLAB_ACTIVE_MASK
);
2131 msp
->ms_activation_weight
= 0;
2132 metaslab_passivate_allocator(msp
->ms_group
, msp
, weight
);
2133 ASSERT((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0);
2137 * Segment-based metaslabs are activated once and remain active until
2138 * we either fail an allocation attempt (similar to space-based metaslabs)
2139 * or have exhausted the free space in zfs_metaslab_switch_threshold
2140 * buckets since the metaslab was activated. This function checks to see
2141 * if we've exhaused the zfs_metaslab_switch_threshold buckets in the
2142 * metaslab and passivates it proactively. This will allow us to select a
2143 * metaslab with a larger contiguous region, if any, remaining within this
2144 * metaslab group. If we're in sync pass > 1, then we continue using this
2145 * metaslab so that we don't dirty more block and cause more sync passes.
2148 metaslab_segment_may_passivate(metaslab_t
*msp
)
2150 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
2152 if (WEIGHT_IS_SPACEBASED(msp
->ms_weight
) || spa_sync_pass(spa
) > 1)
2156 * Since we are in the middle of a sync pass, the most accurate
2157 * information that is accessible to us is the in-core range tree
2158 * histogram; calculate the new weight based on that information.
2160 uint64_t weight
= metaslab_weight_from_range_tree(msp
);
2161 int activation_idx
= WEIGHT_GET_INDEX(msp
->ms_activation_weight
);
2162 int current_idx
= WEIGHT_GET_INDEX(weight
);
2164 if (current_idx
<= activation_idx
- zfs_metaslab_switch_threshold
)
2165 metaslab_passivate(msp
, weight
);
2169 metaslab_preload(void *arg
)
2171 metaslab_t
*msp
= arg
;
2172 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
2173 fstrans_cookie_t cookie
= spl_fstrans_mark();
2175 ASSERT(!MUTEX_HELD(&msp
->ms_group
->mg_lock
));
2177 mutex_enter(&msp
->ms_lock
);
2178 (void) metaslab_load(msp
);
2179 msp
->ms_selected_txg
= spa_syncing_txg(spa
);
2180 mutex_exit(&msp
->ms_lock
);
2181 spl_fstrans_unmark(cookie
);
2185 metaslab_group_preload(metaslab_group_t
*mg
)
2187 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
2189 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
2192 if (spa_shutting_down(spa
) || !metaslab_preload_enabled
) {
2193 taskq_wait_outstanding(mg
->mg_taskq
, 0);
2197 mutex_enter(&mg
->mg_lock
);
2200 * Load the next potential metaslabs
2202 for (msp
= avl_first(t
); msp
!= NULL
; msp
= AVL_NEXT(t
, msp
)) {
2203 ASSERT3P(msp
->ms_group
, ==, mg
);
2206 * We preload only the maximum number of metaslabs specified
2207 * by metaslab_preload_limit. If a metaslab is being forced
2208 * to condense then we preload it too. This will ensure
2209 * that force condensing happens in the next txg.
2211 if (++m
> metaslab_preload_limit
&& !msp
->ms_condense_wanted
) {
2215 VERIFY(taskq_dispatch(mg
->mg_taskq
, metaslab_preload
,
2216 msp
, TQ_SLEEP
) != TASKQID_INVALID
);
2218 mutex_exit(&mg
->mg_lock
);
2222 * Determine if the space map's on-disk footprint is past our tolerance
2223 * for inefficiency. We would like to use the following criteria to make
2226 * 1. The size of the space map object should not dramatically increase as a
2227 * result of writing out the free space range tree.
2229 * 2. The minimal on-disk space map representation is zfs_condense_pct/100
2230 * times the size than the free space range tree representation
2231 * (i.e. zfs_condense_pct = 110 and in-core = 1MB, minimal = 1.1MB).
2233 * 3. The on-disk size of the space map should actually decrease.
2235 * Unfortunately, we cannot compute the on-disk size of the space map in this
2236 * context because we cannot accurately compute the effects of compression, etc.
2237 * Instead, we apply the heuristic described in the block comment for
2238 * zfs_metaslab_condense_block_threshold - we only condense if the space used
2239 * is greater than a threshold number of blocks.
2242 metaslab_should_condense(metaslab_t
*msp
)
2244 space_map_t
*sm
= msp
->ms_sm
;
2245 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
2246 uint64_t vdev_blocksize
= 1 << vd
->vdev_ashift
;
2247 uint64_t current_txg
= spa_syncing_txg(vd
->vdev_spa
);
2249 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2250 ASSERT(msp
->ms_loaded
);
2253 * Allocations and frees in early passes are generally more space
2254 * efficient (in terms of blocks described in space map entries)
2255 * than the ones in later passes (e.g. we don't compress after
2256 * sync pass 5) and condensing a metaslab multiple times in a txg
2257 * could degrade performance.
2259 * Thus we prefer condensing each metaslab at most once every txg at
2260 * the earliest sync pass possible. If a metaslab is eligible for
2261 * condensing again after being considered for condensing within the
2262 * same txg, it will hopefully be dirty in the next txg where it will
2263 * be condensed at an earlier pass.
2265 if (msp
->ms_condense_checked_txg
== current_txg
)
2267 msp
->ms_condense_checked_txg
= current_txg
;
2270 * We always condense metaslabs that are empty and metaslabs for
2271 * which a condense request has been made.
2273 if (avl_is_empty(&msp
->ms_allocatable_by_size
) ||
2274 msp
->ms_condense_wanted
)
2277 uint64_t object_size
= space_map_length(msp
->ms_sm
);
2278 uint64_t optimal_size
= space_map_estimate_optimal_size(sm
,
2279 msp
->ms_allocatable
, SM_NO_VDEVID
);
2281 dmu_object_info_t doi
;
2282 dmu_object_info_from_db(sm
->sm_dbuf
, &doi
);
2283 uint64_t record_size
= MAX(doi
.doi_data_block_size
, vdev_blocksize
);
2285 return (object_size
>= (optimal_size
* zfs_condense_pct
/ 100) &&
2286 object_size
> zfs_metaslab_condense_block_threshold
* record_size
);
2290 * Condense the on-disk space map representation to its minimized form.
2291 * The minimized form consists of a small number of allocations followed by
2292 * the entries of the free range tree.
2295 metaslab_condense(metaslab_t
*msp
, uint64_t txg
, dmu_tx_t
*tx
)
2297 range_tree_t
*condense_tree
;
2298 space_map_t
*sm
= msp
->ms_sm
;
2300 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
2301 ASSERT(msp
->ms_loaded
);
2304 zfs_dbgmsg("condensing: txg %llu, msp[%llu] %p, vdev id %llu, "
2305 "spa %s, smp size %llu, segments %lu, forcing condense=%s", txg
,
2306 msp
->ms_id
, msp
, msp
->ms_group
->mg_vd
->vdev_id
,
2307 msp
->ms_group
->mg_vd
->vdev_spa
->spa_name
,
2308 space_map_length(msp
->ms_sm
),
2309 avl_numnodes(&msp
->ms_allocatable
->rt_root
),
2310 msp
->ms_condense_wanted
? "TRUE" : "FALSE");
2312 msp
->ms_condense_wanted
= B_FALSE
;
2315 * Create an range tree that is 100% allocated. We remove segments
2316 * that have been freed in this txg, any deferred frees that exist,
2317 * and any allocation in the future. Removing segments should be
2318 * a relatively inexpensive operation since we expect these trees to
2319 * have a small number of nodes.
2321 condense_tree
= range_tree_create(NULL
, NULL
);
2322 range_tree_add(condense_tree
, msp
->ms_start
, msp
->ms_size
);
2324 range_tree_walk(msp
->ms_freeing
, range_tree_remove
, condense_tree
);
2325 range_tree_walk(msp
->ms_freed
, range_tree_remove
, condense_tree
);
2327 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
2328 range_tree_walk(msp
->ms_defer
[t
],
2329 range_tree_remove
, condense_tree
);
2332 for (int t
= 1; t
< TXG_CONCURRENT_STATES
; t
++) {
2333 range_tree_walk(msp
->ms_allocating
[(txg
+ t
) & TXG_MASK
],
2334 range_tree_remove
, condense_tree
);
2338 * We're about to drop the metaslab's lock thus allowing
2339 * other consumers to change it's content. Set the
2340 * metaslab's ms_condensing flag to ensure that
2341 * allocations on this metaslab do not occur while we're
2342 * in the middle of committing it to disk. This is only critical
2343 * for ms_allocatable as all other range trees use per txg
2344 * views of their content.
2346 msp
->ms_condensing
= B_TRUE
;
2348 mutex_exit(&msp
->ms_lock
);
2349 space_map_truncate(sm
, zfs_metaslab_sm_blksz
, tx
);
2352 * While we would ideally like to create a space map representation
2353 * that consists only of allocation records, doing so can be
2354 * prohibitively expensive because the in-core free tree can be
2355 * large, and therefore computationally expensive to subtract
2356 * from the condense_tree. Instead we sync out two trees, a cheap
2357 * allocation only tree followed by the in-core free tree. While not
2358 * optimal, this is typically close to optimal, and much cheaper to
2361 space_map_write(sm
, condense_tree
, SM_ALLOC
, SM_NO_VDEVID
, tx
);
2362 range_tree_vacate(condense_tree
, NULL
, NULL
);
2363 range_tree_destroy(condense_tree
);
2365 space_map_write(sm
, msp
->ms_allocatable
, SM_FREE
, SM_NO_VDEVID
, tx
);
2366 mutex_enter(&msp
->ms_lock
);
2367 msp
->ms_condensing
= B_FALSE
;
2371 * Write a metaslab to disk in the context of the specified transaction group.
2374 metaslab_sync(metaslab_t
*msp
, uint64_t txg
)
2376 metaslab_group_t
*mg
= msp
->ms_group
;
2377 vdev_t
*vd
= mg
->mg_vd
;
2378 spa_t
*spa
= vd
->vdev_spa
;
2379 objset_t
*mos
= spa_meta_objset(spa
);
2380 range_tree_t
*alloctree
= msp
->ms_allocating
[txg
& TXG_MASK
];
2382 uint64_t object
= space_map_object(msp
->ms_sm
);
2384 ASSERT(!vd
->vdev_ishole
);
2387 * This metaslab has just been added so there's no work to do now.
2389 if (msp
->ms_freeing
== NULL
) {
2390 ASSERT3P(alloctree
, ==, NULL
);
2394 ASSERT3P(alloctree
, !=, NULL
);
2395 ASSERT3P(msp
->ms_freeing
, !=, NULL
);
2396 ASSERT3P(msp
->ms_freed
, !=, NULL
);
2397 ASSERT3P(msp
->ms_checkpointing
, !=, NULL
);
2400 * Normally, we don't want to process a metaslab if there are no
2401 * allocations or frees to perform. However, if the metaslab is being
2402 * forced to condense and it's loaded, we need to let it through.
2404 if (range_tree_is_empty(alloctree
) &&
2405 range_tree_is_empty(msp
->ms_freeing
) &&
2406 range_tree_is_empty(msp
->ms_checkpointing
) &&
2407 !(msp
->ms_loaded
&& msp
->ms_condense_wanted
))
2411 VERIFY(txg
<= spa_final_dirty_txg(spa
));
2414 * The only state that can actually be changing concurrently with
2415 * metaslab_sync() is the metaslab's ms_allocatable. No other
2416 * thread can be modifying this txg's alloc, freeing,
2417 * freed, or space_map_phys_t. We drop ms_lock whenever we
2418 * could call into the DMU, because the DMU can call down to us
2419 * (e.g. via zio_free()) at any time.
2421 * The spa_vdev_remove_thread() can be reading metaslab state
2422 * concurrently, and it is locked out by the ms_sync_lock. Note
2423 * that the ms_lock is insufficient for this, because it is dropped
2424 * by space_map_write().
2426 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
2428 if (msp
->ms_sm
== NULL
) {
2429 uint64_t new_object
;
2431 new_object
= space_map_alloc(mos
, zfs_metaslab_sm_blksz
, tx
);
2432 VERIFY3U(new_object
, !=, 0);
2434 VERIFY0(space_map_open(&msp
->ms_sm
, mos
, new_object
,
2435 msp
->ms_start
, msp
->ms_size
, vd
->vdev_ashift
));
2436 ASSERT(msp
->ms_sm
!= NULL
);
2439 if (!range_tree_is_empty(msp
->ms_checkpointing
) &&
2440 vd
->vdev_checkpoint_sm
== NULL
) {
2441 ASSERT(spa_has_checkpoint(spa
));
2443 uint64_t new_object
= space_map_alloc(mos
,
2444 vdev_standard_sm_blksz
, tx
);
2445 VERIFY3U(new_object
, !=, 0);
2447 VERIFY0(space_map_open(&vd
->vdev_checkpoint_sm
,
2448 mos
, new_object
, 0, vd
->vdev_asize
, vd
->vdev_ashift
));
2449 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
2452 * We save the space map object as an entry in vdev_top_zap
2453 * so it can be retrieved when the pool is reopened after an
2454 * export or through zdb.
2456 VERIFY0(zap_add(vd
->vdev_spa
->spa_meta_objset
,
2457 vd
->vdev_top_zap
, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM
,
2458 sizeof (new_object
), 1, &new_object
, tx
));
2461 mutex_enter(&msp
->ms_sync_lock
);
2462 mutex_enter(&msp
->ms_lock
);
2465 * Note: metaslab_condense() clears the space map's histogram.
2466 * Therefore we must verify and remove this histogram before
2469 metaslab_group_histogram_verify(mg
);
2470 metaslab_class_histogram_verify(mg
->mg_class
);
2471 metaslab_group_histogram_remove(mg
, msp
);
2473 if (msp
->ms_loaded
&& metaslab_should_condense(msp
)) {
2474 metaslab_condense(msp
, txg
, tx
);
2476 mutex_exit(&msp
->ms_lock
);
2477 space_map_write(msp
->ms_sm
, alloctree
, SM_ALLOC
,
2479 space_map_write(msp
->ms_sm
, msp
->ms_freeing
, SM_FREE
,
2481 mutex_enter(&msp
->ms_lock
);
2484 if (!range_tree_is_empty(msp
->ms_checkpointing
)) {
2485 ASSERT(spa_has_checkpoint(spa
));
2486 ASSERT3P(vd
->vdev_checkpoint_sm
, !=, NULL
);
2489 * Since we are doing writes to disk and the ms_checkpointing
2490 * tree won't be changing during that time, we drop the
2491 * ms_lock while writing to the checkpoint space map.
2493 mutex_exit(&msp
->ms_lock
);
2494 space_map_write(vd
->vdev_checkpoint_sm
,
2495 msp
->ms_checkpointing
, SM_FREE
, SM_NO_VDEVID
, tx
);
2496 mutex_enter(&msp
->ms_lock
);
2497 space_map_update(vd
->vdev_checkpoint_sm
);
2499 spa
->spa_checkpoint_info
.sci_dspace
+=
2500 range_tree_space(msp
->ms_checkpointing
);
2501 vd
->vdev_stat
.vs_checkpoint_space
+=
2502 range_tree_space(msp
->ms_checkpointing
);
2503 ASSERT3U(vd
->vdev_stat
.vs_checkpoint_space
, ==,
2504 -vd
->vdev_checkpoint_sm
->sm_alloc
);
2506 range_tree_vacate(msp
->ms_checkpointing
, NULL
, NULL
);
2509 if (msp
->ms_loaded
) {
2511 * When the space map is loaded, we have an accurate
2512 * histogram in the range tree. This gives us an opportunity
2513 * to bring the space map's histogram up-to-date so we clear
2514 * it first before updating it.
2516 space_map_histogram_clear(msp
->ms_sm
);
2517 space_map_histogram_add(msp
->ms_sm
, msp
->ms_allocatable
, tx
);
2520 * Since we've cleared the histogram we need to add back
2521 * any free space that has already been processed, plus
2522 * any deferred space. This allows the on-disk histogram
2523 * to accurately reflect all free space even if some space
2524 * is not yet available for allocation (i.e. deferred).
2526 space_map_histogram_add(msp
->ms_sm
, msp
->ms_freed
, tx
);
2529 * Add back any deferred free space that has not been
2530 * added back into the in-core free tree yet. This will
2531 * ensure that we don't end up with a space map histogram
2532 * that is completely empty unless the metaslab is fully
2535 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
2536 space_map_histogram_add(msp
->ms_sm
,
2537 msp
->ms_defer
[t
], tx
);
2542 * Always add the free space from this sync pass to the space
2543 * map histogram. We want to make sure that the on-disk histogram
2544 * accounts for all free space. If the space map is not loaded,
2545 * then we will lose some accuracy but will correct it the next
2546 * time we load the space map.
2548 space_map_histogram_add(msp
->ms_sm
, msp
->ms_freeing
, tx
);
2550 metaslab_group_histogram_add(mg
, msp
);
2551 metaslab_group_histogram_verify(mg
);
2552 metaslab_class_histogram_verify(mg
->mg_class
);
2555 * For sync pass 1, we avoid traversing this txg's free range tree
2556 * and instead will just swap the pointers for freeing and
2557 * freed. We can safely do this since the freed_tree is
2558 * guaranteed to be empty on the initial pass.
2560 if (spa_sync_pass(spa
) == 1) {
2561 range_tree_swap(&msp
->ms_freeing
, &msp
->ms_freed
);
2563 range_tree_vacate(msp
->ms_freeing
,
2564 range_tree_add
, msp
->ms_freed
);
2566 range_tree_vacate(alloctree
, NULL
, NULL
);
2568 ASSERT0(range_tree_space(msp
->ms_allocating
[txg
& TXG_MASK
]));
2569 ASSERT0(range_tree_space(msp
->ms_allocating
[TXG_CLEAN(txg
)
2571 ASSERT0(range_tree_space(msp
->ms_freeing
));
2572 ASSERT0(range_tree_space(msp
->ms_checkpointing
));
2574 mutex_exit(&msp
->ms_lock
);
2576 if (object
!= space_map_object(msp
->ms_sm
)) {
2577 object
= space_map_object(msp
->ms_sm
);
2578 dmu_write(mos
, vd
->vdev_ms_array
, sizeof (uint64_t) *
2579 msp
->ms_id
, sizeof (uint64_t), &object
, tx
);
2581 mutex_exit(&msp
->ms_sync_lock
);
2586 * Called after a transaction group has completely synced to mark
2587 * all of the metaslab's free space as usable.
2590 metaslab_sync_done(metaslab_t
*msp
, uint64_t txg
)
2592 metaslab_group_t
*mg
= msp
->ms_group
;
2593 vdev_t
*vd
= mg
->mg_vd
;
2594 spa_t
*spa
= vd
->vdev_spa
;
2595 range_tree_t
**defer_tree
;
2596 int64_t alloc_delta
, defer_delta
;
2597 boolean_t defer_allowed
= B_TRUE
;
2599 ASSERT(!vd
->vdev_ishole
);
2601 mutex_enter(&msp
->ms_lock
);
2604 * If this metaslab is just becoming available, initialize its
2605 * range trees and add its capacity to the vdev.
2607 if (msp
->ms_freed
== NULL
) {
2608 for (int t
= 0; t
< TXG_SIZE
; t
++) {
2609 ASSERT(msp
->ms_allocating
[t
] == NULL
);
2611 msp
->ms_allocating
[t
] = range_tree_create(NULL
, NULL
);
2614 ASSERT3P(msp
->ms_freeing
, ==, NULL
);
2615 msp
->ms_freeing
= range_tree_create(NULL
, NULL
);
2617 ASSERT3P(msp
->ms_freed
, ==, NULL
);
2618 msp
->ms_freed
= range_tree_create(NULL
, NULL
);
2620 for (int t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
2621 ASSERT(msp
->ms_defer
[t
] == NULL
);
2623 msp
->ms_defer
[t
] = range_tree_create(NULL
, NULL
);
2626 ASSERT3P(msp
->ms_checkpointing
, ==, NULL
);
2627 msp
->ms_checkpointing
= range_tree_create(NULL
, NULL
);
2629 metaslab_space_update(vd
, mg
->mg_class
, 0, 0, msp
->ms_size
);
2631 ASSERT0(range_tree_space(msp
->ms_freeing
));
2632 ASSERT0(range_tree_space(msp
->ms_checkpointing
));
2634 defer_tree
= &msp
->ms_defer
[txg
% TXG_DEFER_SIZE
];
2636 uint64_t free_space
= metaslab_class_get_space(spa_normal_class(spa
)) -
2637 metaslab_class_get_alloc(spa_normal_class(spa
));
2638 if (free_space
<= spa_get_slop_space(spa
) || vd
->vdev_removing
) {
2639 defer_allowed
= B_FALSE
;
2643 alloc_delta
= space_map_alloc_delta(msp
->ms_sm
);
2644 if (defer_allowed
) {
2645 defer_delta
= range_tree_space(msp
->ms_freed
) -
2646 range_tree_space(*defer_tree
);
2648 defer_delta
-= range_tree_space(*defer_tree
);
2651 metaslab_space_update(vd
, mg
->mg_class
, alloc_delta
+ defer_delta
,
2655 * If there's a metaslab_load() in progress, wait for it to complete
2656 * so that we have a consistent view of the in-core space map.
2658 metaslab_load_wait(msp
);
2661 * Move the frees from the defer_tree back to the free
2662 * range tree (if it's loaded). Swap the freed_tree and
2663 * the defer_tree -- this is safe to do because we've
2664 * just emptied out the defer_tree.
2666 range_tree_vacate(*defer_tree
,
2667 msp
->ms_loaded
? range_tree_add
: NULL
, msp
->ms_allocatable
);
2668 if (defer_allowed
) {
2669 range_tree_swap(&msp
->ms_freed
, defer_tree
);
2671 range_tree_vacate(msp
->ms_freed
,
2672 msp
->ms_loaded
? range_tree_add
: NULL
,
2673 msp
->ms_allocatable
);
2675 space_map_update(msp
->ms_sm
);
2677 msp
->ms_deferspace
+= defer_delta
;
2678 ASSERT3S(msp
->ms_deferspace
, >=, 0);
2679 ASSERT3S(msp
->ms_deferspace
, <=, msp
->ms_size
);
2680 if (msp
->ms_deferspace
!= 0) {
2682 * Keep syncing this metaslab until all deferred frees
2683 * are back in circulation.
2685 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
+ 1);
2689 msp
->ms_new
= B_FALSE
;
2690 mutex_enter(&mg
->mg_lock
);
2692 mutex_exit(&mg
->mg_lock
);
2695 * Calculate the new weights before unloading any metaslabs.
2696 * This will give us the most accurate weighting.
2698 metaslab_group_sort(mg
, msp
, metaslab_weight(msp
) |
2699 (msp
->ms_weight
& METASLAB_ACTIVE_MASK
));
2702 * If the metaslab is loaded and we've not tried to load or allocate
2703 * from it in 'metaslab_unload_delay' txgs, then unload it.
2705 if (msp
->ms_loaded
&&
2706 msp
->ms_initializing
== 0 &&
2707 msp
->ms_selected_txg
+ metaslab_unload_delay
< txg
) {
2709 for (int t
= 1; t
< TXG_CONCURRENT_STATES
; t
++) {
2710 VERIFY0(range_tree_space(
2711 msp
->ms_allocating
[(txg
+ t
) & TXG_MASK
]));
2713 if (msp
->ms_allocator
!= -1) {
2714 metaslab_passivate(msp
, msp
->ms_weight
&
2715 ~METASLAB_ACTIVE_MASK
);
2718 if (!metaslab_debug_unload
)
2719 metaslab_unload(msp
);
2722 ASSERT0(range_tree_space(msp
->ms_allocating
[txg
& TXG_MASK
]));
2723 ASSERT0(range_tree_space(msp
->ms_freeing
));
2724 ASSERT0(range_tree_space(msp
->ms_freed
));
2725 ASSERT0(range_tree_space(msp
->ms_checkpointing
));
2727 mutex_exit(&msp
->ms_lock
);
2731 metaslab_sync_reassess(metaslab_group_t
*mg
)
2733 spa_t
*spa
= mg
->mg_class
->mc_spa
;
2735 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
2736 metaslab_group_alloc_update(mg
);
2737 mg
->mg_fragmentation
= metaslab_group_fragmentation(mg
);
2740 * Preload the next potential metaslabs but only on active
2741 * metaslab groups. We can get into a state where the metaslab
2742 * is no longer active since we dirty metaslabs as we remove a
2743 * a device, thus potentially making the metaslab group eligible
2746 if (mg
->mg_activation_count
> 0) {
2747 metaslab_group_preload(mg
);
2749 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
2753 * When writing a ditto block (i.e. more than one DVA for a given BP) on
2754 * the same vdev as an existing DVA of this BP, then try to allocate it
2755 * on a different metaslab than existing DVAs (i.e. a unique metaslab).
2758 metaslab_is_unique(metaslab_t
*msp
, dva_t
*dva
)
2762 if (DVA_GET_ASIZE(dva
) == 0)
2765 if (msp
->ms_group
->mg_vd
->vdev_id
!= DVA_GET_VDEV(dva
))
2768 dva_ms_id
= DVA_GET_OFFSET(dva
) >> msp
->ms_group
->mg_vd
->vdev_ms_shift
;
2770 return (msp
->ms_id
!= dva_ms_id
);
2774 * ==========================================================================
2775 * Metaslab allocation tracing facility
2776 * ==========================================================================
2778 #ifdef _METASLAB_TRACING
2779 kstat_t
*metaslab_trace_ksp
;
2780 kstat_named_t metaslab_trace_over_limit
;
2783 metaslab_alloc_trace_init(void)
2785 ASSERT(metaslab_alloc_trace_cache
== NULL
);
2786 metaslab_alloc_trace_cache
= kmem_cache_create(
2787 "metaslab_alloc_trace_cache", sizeof (metaslab_alloc_trace_t
),
2788 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
2789 metaslab_trace_ksp
= kstat_create("zfs", 0, "metaslab_trace_stats",
2790 "misc", KSTAT_TYPE_NAMED
, 1, KSTAT_FLAG_VIRTUAL
);
2791 if (metaslab_trace_ksp
!= NULL
) {
2792 metaslab_trace_ksp
->ks_data
= &metaslab_trace_over_limit
;
2793 kstat_named_init(&metaslab_trace_over_limit
,
2794 "metaslab_trace_over_limit", KSTAT_DATA_UINT64
);
2795 kstat_install(metaslab_trace_ksp
);
2800 metaslab_alloc_trace_fini(void)
2802 if (metaslab_trace_ksp
!= NULL
) {
2803 kstat_delete(metaslab_trace_ksp
);
2804 metaslab_trace_ksp
= NULL
;
2806 kmem_cache_destroy(metaslab_alloc_trace_cache
);
2807 metaslab_alloc_trace_cache
= NULL
;
2811 * Add an allocation trace element to the allocation tracing list.
2814 metaslab_trace_add(zio_alloc_list_t
*zal
, metaslab_group_t
*mg
,
2815 metaslab_t
*msp
, uint64_t psize
, uint32_t dva_id
, uint64_t offset
,
2818 metaslab_alloc_trace_t
*mat
;
2820 if (!metaslab_trace_enabled
)
2824 * When the tracing list reaches its maximum we remove
2825 * the second element in the list before adding a new one.
2826 * By removing the second element we preserve the original
2827 * entry as a clue to what allocations steps have already been
2830 if (zal
->zal_size
== metaslab_trace_max_entries
) {
2831 metaslab_alloc_trace_t
*mat_next
;
2833 panic("too many entries in allocation list");
2835 atomic_inc_64(&metaslab_trace_over_limit
.value
.ui64
);
2837 mat_next
= list_next(&zal
->zal_list
, list_head(&zal
->zal_list
));
2838 list_remove(&zal
->zal_list
, mat_next
);
2839 kmem_cache_free(metaslab_alloc_trace_cache
, mat_next
);
2842 mat
= kmem_cache_alloc(metaslab_alloc_trace_cache
, KM_SLEEP
);
2843 list_link_init(&mat
->mat_list_node
);
2846 mat
->mat_size
= psize
;
2847 mat
->mat_dva_id
= dva_id
;
2848 mat
->mat_offset
= offset
;
2849 mat
->mat_weight
= 0;
2850 mat
->mat_allocator
= allocator
;
2853 mat
->mat_weight
= msp
->ms_weight
;
2856 * The list is part of the zio so locking is not required. Only
2857 * a single thread will perform allocations for a given zio.
2859 list_insert_tail(&zal
->zal_list
, mat
);
2862 ASSERT3U(zal
->zal_size
, <=, metaslab_trace_max_entries
);
2866 metaslab_trace_init(zio_alloc_list_t
*zal
)
2868 list_create(&zal
->zal_list
, sizeof (metaslab_alloc_trace_t
),
2869 offsetof(metaslab_alloc_trace_t
, mat_list_node
));
2874 metaslab_trace_fini(zio_alloc_list_t
*zal
)
2876 metaslab_alloc_trace_t
*mat
;
2878 while ((mat
= list_remove_head(&zal
->zal_list
)) != NULL
)
2879 kmem_cache_free(metaslab_alloc_trace_cache
, mat
);
2880 list_destroy(&zal
->zal_list
);
2885 #define metaslab_trace_add(zal, mg, msp, psize, id, off, alloc)
2888 metaslab_alloc_trace_init(void)
2893 metaslab_alloc_trace_fini(void)
2898 metaslab_trace_init(zio_alloc_list_t
*zal
)
2903 metaslab_trace_fini(zio_alloc_list_t
*zal
)
2907 #endif /* _METASLAB_TRACING */
2910 * ==========================================================================
2911 * Metaslab block operations
2912 * ==========================================================================
2916 metaslab_group_alloc_increment(spa_t
*spa
, uint64_t vdev
, void *tag
, int flags
,
2919 if (!(flags
& METASLAB_ASYNC_ALLOC
) ||
2920 (flags
& METASLAB_DONT_THROTTLE
))
2923 metaslab_group_t
*mg
= vdev_lookup_top(spa
, vdev
)->vdev_mg
;
2924 if (!mg
->mg_class
->mc_alloc_throttle_enabled
)
2927 (void) zfs_refcount_add(&mg
->mg_alloc_queue_depth
[allocator
], tag
);
2931 metaslab_group_increment_qdepth(metaslab_group_t
*mg
, int allocator
)
2933 uint64_t max
= mg
->mg_max_alloc_queue_depth
;
2934 uint64_t cur
= mg
->mg_cur_max_alloc_queue_depth
[allocator
];
2936 if (atomic_cas_64(&mg
->mg_cur_max_alloc_queue_depth
[allocator
],
2937 cur
, cur
+ 1) == cur
) {
2939 &mg
->mg_class
->mc_alloc_max_slots
[allocator
]);
2942 cur
= mg
->mg_cur_max_alloc_queue_depth
[allocator
];
2947 metaslab_group_alloc_decrement(spa_t
*spa
, uint64_t vdev
, void *tag
, int flags
,
2948 int allocator
, boolean_t io_complete
)
2950 if (!(flags
& METASLAB_ASYNC_ALLOC
) ||
2951 (flags
& METASLAB_DONT_THROTTLE
))
2954 metaslab_group_t
*mg
= vdev_lookup_top(spa
, vdev
)->vdev_mg
;
2955 if (!mg
->mg_class
->mc_alloc_throttle_enabled
)
2958 (void) zfs_refcount_remove(&mg
->mg_alloc_queue_depth
[allocator
], tag
);
2960 metaslab_group_increment_qdepth(mg
, allocator
);
2964 metaslab_group_alloc_verify(spa_t
*spa
, const blkptr_t
*bp
, void *tag
,
2968 const dva_t
*dva
= bp
->blk_dva
;
2969 int ndvas
= BP_GET_NDVAS(bp
);
2971 for (int d
= 0; d
< ndvas
; d
++) {
2972 uint64_t vdev
= DVA_GET_VDEV(&dva
[d
]);
2973 metaslab_group_t
*mg
= vdev_lookup_top(spa
, vdev
)->vdev_mg
;
2974 VERIFY(zfs_refcount_not_held(
2975 &mg
->mg_alloc_queue_depth
[allocator
], tag
));
2981 metaslab_block_alloc(metaslab_t
*msp
, uint64_t size
, uint64_t txg
)
2984 range_tree_t
*rt
= msp
->ms_allocatable
;
2985 metaslab_class_t
*mc
= msp
->ms_group
->mg_class
;
2987 VERIFY(!msp
->ms_condensing
);
2988 VERIFY0(msp
->ms_initializing
);
2990 start
= mc
->mc_ops
->msop_alloc(msp
, size
);
2991 if (start
!= -1ULL) {
2992 metaslab_group_t
*mg
= msp
->ms_group
;
2993 vdev_t
*vd
= mg
->mg_vd
;
2995 VERIFY0(P2PHASE(start
, 1ULL << vd
->vdev_ashift
));
2996 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
2997 VERIFY3U(range_tree_space(rt
) - size
, <=, msp
->ms_size
);
2998 range_tree_remove(rt
, start
, size
);
3000 if (range_tree_is_empty(msp
->ms_allocating
[txg
& TXG_MASK
]))
3001 vdev_dirty(mg
->mg_vd
, VDD_METASLAB
, msp
, txg
);
3003 range_tree_add(msp
->ms_allocating
[txg
& TXG_MASK
], start
, size
);
3005 /* Track the last successful allocation */
3006 msp
->ms_alloc_txg
= txg
;
3007 metaslab_verify_space(msp
, txg
);
3011 * Now that we've attempted the allocation we need to update the
3012 * metaslab's maximum block size since it may have changed.
3014 msp
->ms_max_size
= metaslab_block_maxsize(msp
);
3019 * Find the metaslab with the highest weight that is less than what we've
3020 * already tried. In the common case, this means that we will examine each
3021 * metaslab at most once. Note that concurrent callers could reorder metaslabs
3022 * by activation/passivation once we have dropped the mg_lock. If a metaslab is
3023 * activated by another thread, and we fail to allocate from the metaslab we
3024 * have selected, we may not try the newly-activated metaslab, and instead
3025 * activate another metaslab. This is not optimal, but generally does not cause
3026 * any problems (a possible exception being if every metaslab is completely full
3027 * except for the the newly-activated metaslab which we fail to examine).
3030 find_valid_metaslab(metaslab_group_t
*mg
, uint64_t activation_weight
,
3031 dva_t
*dva
, int d
, boolean_t want_unique
, uint64_t asize
, int allocator
,
3032 zio_alloc_list_t
*zal
, metaslab_t
*search
, boolean_t
*was_active
)
3035 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
3036 metaslab_t
*msp
= avl_find(t
, search
, &idx
);
3038 msp
= avl_nearest(t
, idx
, AVL_AFTER
);
3040 for (; msp
!= NULL
; msp
= AVL_NEXT(t
, msp
)) {
3042 if (!metaslab_should_allocate(msp
, asize
)) {
3043 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3044 TRACE_TOO_SMALL
, allocator
);
3049 * If the selected metaslab is condensing or being
3050 * initialized, skip it.
3052 if (msp
->ms_condensing
|| msp
->ms_initializing
> 0)
3055 *was_active
= msp
->ms_allocator
!= -1;
3057 * If we're activating as primary, this is our first allocation
3058 * from this disk, so we don't need to check how close we are.
3059 * If the metaslab under consideration was already active,
3060 * we're getting desperate enough to steal another allocator's
3061 * metaslab, so we still don't care about distances.
3063 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
|| *was_active
)
3066 for (i
= 0; i
< d
; i
++) {
3068 !metaslab_is_unique(msp
, &dva
[i
]))
3069 break; /* try another metaslab */
3076 search
->ms_weight
= msp
->ms_weight
;
3077 search
->ms_start
= msp
->ms_start
+ 1;
3078 search
->ms_allocator
= msp
->ms_allocator
;
3079 search
->ms_primary
= msp
->ms_primary
;
3086 metaslab_group_alloc_normal(metaslab_group_t
*mg
, zio_alloc_list_t
*zal
,
3087 uint64_t asize
, uint64_t txg
, boolean_t want_unique
, dva_t
*dva
,
3088 int d
, int allocator
)
3090 metaslab_t
*msp
= NULL
;
3091 uint64_t offset
= -1ULL;
3092 uint64_t activation_weight
;
3094 activation_weight
= METASLAB_WEIGHT_PRIMARY
;
3095 for (int i
= 0; i
< d
; i
++) {
3096 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
&&
3097 DVA_GET_VDEV(&dva
[i
]) == mg
->mg_vd
->vdev_id
) {
3098 activation_weight
= METASLAB_WEIGHT_SECONDARY
;
3099 } else if (activation_weight
== METASLAB_WEIGHT_SECONDARY
&&
3100 DVA_GET_VDEV(&dva
[i
]) == mg
->mg_vd
->vdev_id
) {
3101 activation_weight
= METASLAB_WEIGHT_CLAIM
;
3107 * If we don't have enough metaslabs active to fill the entire array, we
3108 * just use the 0th slot.
3110 if (mg
->mg_ms_ready
< mg
->mg_allocators
* 3)
3113 ASSERT3U(mg
->mg_vd
->vdev_ms_count
, >=, 2);
3115 metaslab_t
*search
= kmem_alloc(sizeof (*search
), KM_SLEEP
);
3116 search
->ms_weight
= UINT64_MAX
;
3117 search
->ms_start
= 0;
3119 * At the end of the metaslab tree are the already-active metaslabs,
3120 * first the primaries, then the secondaries. When we resume searching
3121 * through the tree, we need to consider ms_allocator and ms_primary so
3122 * we start in the location right after where we left off, and don't
3123 * accidentally loop forever considering the same metaslabs.
3125 search
->ms_allocator
= -1;
3126 search
->ms_primary
= B_TRUE
;
3128 boolean_t was_active
= B_FALSE
;
3130 mutex_enter(&mg
->mg_lock
);
3132 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
&&
3133 mg
->mg_primaries
[allocator
] != NULL
) {
3134 msp
= mg
->mg_primaries
[allocator
];
3135 was_active
= B_TRUE
;
3136 } else if (activation_weight
== METASLAB_WEIGHT_SECONDARY
&&
3137 mg
->mg_secondaries
[allocator
] != NULL
) {
3138 msp
= mg
->mg_secondaries
[allocator
];
3139 was_active
= B_TRUE
;
3141 msp
= find_valid_metaslab(mg
, activation_weight
, dva
, d
,
3142 want_unique
, asize
, allocator
, zal
, search
,
3146 mutex_exit(&mg
->mg_lock
);
3148 kmem_free(search
, sizeof (*search
));
3152 mutex_enter(&msp
->ms_lock
);
3154 * Ensure that the metaslab we have selected is still
3155 * capable of handling our request. It's possible that
3156 * another thread may have changed the weight while we
3157 * were blocked on the metaslab lock. We check the
3158 * active status first to see if we need to reselect
3161 if (was_active
&& !(msp
->ms_weight
& METASLAB_ACTIVE_MASK
)) {
3162 mutex_exit(&msp
->ms_lock
);
3167 * If the metaslab is freshly activated for an allocator that
3168 * isn't the one we're allocating from, or if it's a primary and
3169 * we're seeking a secondary (or vice versa), we go back and
3170 * select a new metaslab.
3172 if (!was_active
&& (msp
->ms_weight
& METASLAB_ACTIVE_MASK
) &&
3173 (msp
->ms_allocator
!= -1) &&
3174 (msp
->ms_allocator
!= allocator
|| ((activation_weight
==
3175 METASLAB_WEIGHT_PRIMARY
) != msp
->ms_primary
))) {
3176 mutex_exit(&msp
->ms_lock
);
3180 if (msp
->ms_weight
& METASLAB_WEIGHT_CLAIM
&&
3181 activation_weight
!= METASLAB_WEIGHT_CLAIM
) {
3182 metaslab_passivate(msp
, msp
->ms_weight
&
3183 ~METASLAB_WEIGHT_CLAIM
);
3184 mutex_exit(&msp
->ms_lock
);
3188 if (metaslab_activate(msp
, allocator
, activation_weight
) != 0) {
3189 mutex_exit(&msp
->ms_lock
);
3193 msp
->ms_selected_txg
= txg
;
3196 * Now that we have the lock, recheck to see if we should
3197 * continue to use this metaslab for this allocation. The
3198 * the metaslab is now loaded so metaslab_should_allocate() can
3199 * accurately determine if the allocation attempt should
3202 if (!metaslab_should_allocate(msp
, asize
)) {
3203 /* Passivate this metaslab and select a new one. */
3204 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3205 TRACE_TOO_SMALL
, allocator
);
3211 * If this metaslab is currently condensing then pick again as
3212 * we can't manipulate this metaslab until it's committed
3213 * to disk. If this metaslab is being initialized, we shouldn't
3214 * allocate from it since the allocated region might be
3215 * overwritten after allocation.
3217 if (msp
->ms_condensing
) {
3218 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3219 TRACE_CONDENSING
, allocator
);
3220 metaslab_passivate(msp
, msp
->ms_weight
&
3221 ~METASLAB_ACTIVE_MASK
);
3222 mutex_exit(&msp
->ms_lock
);
3224 } else if (msp
->ms_initializing
> 0) {
3225 metaslab_trace_add(zal
, mg
, msp
, asize
, d
,
3226 TRACE_INITIALIZING
, allocator
);
3227 metaslab_passivate(msp
, msp
->ms_weight
&
3228 ~METASLAB_ACTIVE_MASK
);
3229 mutex_exit(&msp
->ms_lock
);
3233 offset
= metaslab_block_alloc(msp
, asize
, txg
);
3234 metaslab_trace_add(zal
, mg
, msp
, asize
, d
, offset
, allocator
);
3236 if (offset
!= -1ULL) {
3237 /* Proactively passivate the metaslab, if needed */
3238 metaslab_segment_may_passivate(msp
);
3242 ASSERT(msp
->ms_loaded
);
3245 * We were unable to allocate from this metaslab so determine
3246 * a new weight for this metaslab. Now that we have loaded
3247 * the metaslab we can provide a better hint to the metaslab
3250 * For space-based metaslabs, we use the maximum block size.
3251 * This information is only available when the metaslab
3252 * is loaded and is more accurate than the generic free
3253 * space weight that was calculated by metaslab_weight().
3254 * This information allows us to quickly compare the maximum
3255 * available allocation in the metaslab to the allocation
3256 * size being requested.
3258 * For segment-based metaslabs, determine the new weight
3259 * based on the highest bucket in the range tree. We
3260 * explicitly use the loaded segment weight (i.e. the range
3261 * tree histogram) since it contains the space that is
3262 * currently available for allocation and is accurate
3263 * even within a sync pass.
3265 if (WEIGHT_IS_SPACEBASED(msp
->ms_weight
)) {
3266 uint64_t weight
= metaslab_block_maxsize(msp
);
3267 WEIGHT_SET_SPACEBASED(weight
);
3268 metaslab_passivate(msp
, weight
);
3270 metaslab_passivate(msp
,
3271 metaslab_weight_from_range_tree(msp
));
3275 * We have just failed an allocation attempt, check
3276 * that metaslab_should_allocate() agrees. Otherwise,
3277 * we may end up in an infinite loop retrying the same
3280 ASSERT(!metaslab_should_allocate(msp
, asize
));
3282 mutex_exit(&msp
->ms_lock
);
3284 mutex_exit(&msp
->ms_lock
);
3285 kmem_free(search
, sizeof (*search
));
3290 metaslab_group_alloc(metaslab_group_t
*mg
, zio_alloc_list_t
*zal
,
3291 uint64_t asize
, uint64_t txg
, boolean_t want_unique
, dva_t
*dva
,
3292 int d
, int allocator
)
3295 ASSERT(mg
->mg_initialized
);
3297 offset
= metaslab_group_alloc_normal(mg
, zal
, asize
, txg
, want_unique
,
3300 mutex_enter(&mg
->mg_lock
);
3301 if (offset
== -1ULL) {
3302 mg
->mg_failed_allocations
++;
3303 metaslab_trace_add(zal
, mg
, NULL
, asize
, d
,
3304 TRACE_GROUP_FAILURE
, allocator
);
3305 if (asize
== SPA_GANGBLOCKSIZE
) {
3307 * This metaslab group was unable to allocate
3308 * the minimum gang block size so it must be out of
3309 * space. We must notify the allocation throttle
3310 * to start skipping allocation attempts to this
3311 * metaslab group until more space becomes available.
3312 * Note: this failure cannot be caused by the
3313 * allocation throttle since the allocation throttle
3314 * is only responsible for skipping devices and
3315 * not failing block allocations.
3317 mg
->mg_no_free_space
= B_TRUE
;
3320 mg
->mg_allocations
++;
3321 mutex_exit(&mg
->mg_lock
);
3326 * Allocate a block for the specified i/o.
3329 metaslab_alloc_dva(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
,
3330 dva_t
*dva
, int d
, dva_t
*hintdva
, uint64_t txg
, int flags
,
3331 zio_alloc_list_t
*zal
, int allocator
)
3333 metaslab_group_t
*mg
, *fast_mg
, *rotor
;
3335 boolean_t try_hard
= B_FALSE
;
3337 ASSERT(!DVA_IS_VALID(&dva
[d
]));
3340 * For testing, make some blocks above a certain size be gang blocks.
3341 * This will result in more split blocks when using device removal,
3342 * and a large number of split blocks coupled with ztest-induced
3343 * damage can result in extremely long reconstruction times. This
3344 * will also test spilling from special to normal.
3346 if (psize
>= metaslab_force_ganging
&& (spa_get_random(100) < 3)) {
3347 metaslab_trace_add(zal
, NULL
, NULL
, psize
, d
, TRACE_FORCE_GANG
,
3349 return (SET_ERROR(ENOSPC
));
3353 * Start at the rotor and loop through all mgs until we find something.
3354 * Note that there's no locking on mc_rotor or mc_aliquot because
3355 * nothing actually breaks if we miss a few updates -- we just won't
3356 * allocate quite as evenly. It all balances out over time.
3358 * If we are doing ditto or log blocks, try to spread them across
3359 * consecutive vdevs. If we're forced to reuse a vdev before we've
3360 * allocated all of our ditto blocks, then try and spread them out on
3361 * that vdev as much as possible. If it turns out to not be possible,
3362 * gradually lower our standards until anything becomes acceptable.
3363 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
3364 * gives us hope of containing our fault domains to something we're
3365 * able to reason about. Otherwise, any two top-level vdev failures
3366 * will guarantee the loss of data. With consecutive allocation,
3367 * only two adjacent top-level vdev failures will result in data loss.
3369 * If we are doing gang blocks (hintdva is non-NULL), try to keep
3370 * ourselves on the same vdev as our gang block header. That
3371 * way, we can hope for locality in vdev_cache, plus it makes our
3372 * fault domains something tractable.
3375 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&hintdva
[d
]));
3378 * It's possible the vdev we're using as the hint no
3379 * longer exists or its mg has been closed (e.g. by
3380 * device removal). Consult the rotor when
3383 if (vd
!= NULL
&& vd
->vdev_mg
!= NULL
) {
3386 if (flags
& METASLAB_HINTBP_AVOID
&&
3387 mg
->mg_next
!= NULL
)
3392 } else if (d
!= 0) {
3393 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
- 1]));
3394 mg
= vd
->vdev_mg
->mg_next
;
3395 } else if (flags
& METASLAB_FASTWRITE
) {
3396 mg
= fast_mg
= mc
->mc_rotor
;
3399 if (fast_mg
->mg_vd
->vdev_pending_fastwrite
<
3400 mg
->mg_vd
->vdev_pending_fastwrite
)
3402 } while ((fast_mg
= fast_mg
->mg_next
) != mc
->mc_rotor
);
3405 ASSERT(mc
->mc_rotor
!= NULL
);
3410 * If the hint put us into the wrong metaslab class, or into a
3411 * metaslab group that has been passivated, just follow the rotor.
3413 if (mg
->mg_class
!= mc
|| mg
->mg_activation_count
<= 0)
3419 boolean_t allocatable
;
3421 ASSERT(mg
->mg_activation_count
== 1);
3425 * Don't allocate from faulted devices.
3428 spa_config_enter(spa
, SCL_ZIO
, FTAG
, RW_READER
);
3429 allocatable
= vdev_allocatable(vd
);
3430 spa_config_exit(spa
, SCL_ZIO
, FTAG
);
3432 allocatable
= vdev_allocatable(vd
);
3436 * Determine if the selected metaslab group is eligible
3437 * for allocations. If we're ganging then don't allow
3438 * this metaslab group to skip allocations since that would
3439 * inadvertently return ENOSPC and suspend the pool
3440 * even though space is still available.
3442 if (allocatable
&& !GANG_ALLOCATION(flags
) && !try_hard
) {
3443 allocatable
= metaslab_group_allocatable(mg
, rotor
,
3444 psize
, allocator
, d
);
3448 metaslab_trace_add(zal
, mg
, NULL
, psize
, d
,
3449 TRACE_NOT_ALLOCATABLE
, allocator
);
3453 ASSERT(mg
->mg_initialized
);
3456 * Avoid writing single-copy data to a failing,
3457 * non-redundant vdev, unless we've already tried all
3460 if ((vd
->vdev_stat
.vs_write_errors
> 0 ||
3461 vd
->vdev_state
< VDEV_STATE_HEALTHY
) &&
3462 d
== 0 && !try_hard
&& vd
->vdev_children
== 0) {
3463 metaslab_trace_add(zal
, mg
, NULL
, psize
, d
,
3464 TRACE_VDEV_ERROR
, allocator
);
3468 ASSERT(mg
->mg_class
== mc
);
3470 uint64_t asize
= vdev_psize_to_asize(vd
, psize
);
3471 ASSERT(P2PHASE(asize
, 1ULL << vd
->vdev_ashift
) == 0);
3474 * If we don't need to try hard, then require that the
3475 * block be on an different metaslab from any other DVAs
3476 * in this BP (unique=true). If we are trying hard, then
3477 * allow any metaslab to be used (unique=false).
3479 uint64_t offset
= metaslab_group_alloc(mg
, zal
, asize
, txg
,
3480 !try_hard
, dva
, d
, allocator
);
3482 if (offset
!= -1ULL) {
3484 * If we've just selected this metaslab group,
3485 * figure out whether the corresponding vdev is
3486 * over- or under-used relative to the pool,
3487 * and set an allocation bias to even it out.
3489 * Bias is also used to compensate for unequally
3490 * sized vdevs so that space is allocated fairly.
3492 if (mc
->mc_aliquot
== 0 && metaslab_bias_enabled
) {
3493 vdev_stat_t
*vs
= &vd
->vdev_stat
;
3494 int64_t vs_free
= vs
->vs_space
- vs
->vs_alloc
;
3495 int64_t mc_free
= mc
->mc_space
- mc
->mc_alloc
;
3499 * Calculate how much more or less we should
3500 * try to allocate from this device during
3501 * this iteration around the rotor.
3503 * This basically introduces a zero-centered
3504 * bias towards the devices with the most
3505 * free space, while compensating for vdev
3509 * vdev V1 = 16M/128M
3510 * vdev V2 = 16M/128M
3511 * ratio(V1) = 100% ratio(V2) = 100%
3513 * vdev V1 = 16M/128M
3514 * vdev V2 = 64M/128M
3515 * ratio(V1) = 127% ratio(V2) = 72%
3517 * vdev V1 = 16M/128M
3518 * vdev V2 = 64M/512M
3519 * ratio(V1) = 40% ratio(V2) = 160%
3521 ratio
= (vs_free
* mc
->mc_alloc_groups
* 100) /
3523 mg
->mg_bias
= ((ratio
- 100) *
3524 (int64_t)mg
->mg_aliquot
) / 100;
3525 } else if (!metaslab_bias_enabled
) {
3529 if ((flags
& METASLAB_FASTWRITE
) ||
3530 atomic_add_64_nv(&mc
->mc_aliquot
, asize
) >=
3531 mg
->mg_aliquot
+ mg
->mg_bias
) {
3532 mc
->mc_rotor
= mg
->mg_next
;
3536 DVA_SET_VDEV(&dva
[d
], vd
->vdev_id
);
3537 DVA_SET_OFFSET(&dva
[d
], offset
);
3538 DVA_SET_GANG(&dva
[d
],
3539 ((flags
& METASLAB_GANG_HEADER
) ? 1 : 0));
3540 DVA_SET_ASIZE(&dva
[d
], asize
);
3542 if (flags
& METASLAB_FASTWRITE
) {
3543 atomic_add_64(&vd
->vdev_pending_fastwrite
,
3550 mc
->mc_rotor
= mg
->mg_next
;
3552 } while ((mg
= mg
->mg_next
) != rotor
);
3555 * If we haven't tried hard, do so now.
3562 bzero(&dva
[d
], sizeof (dva_t
));
3564 metaslab_trace_add(zal
, rotor
, NULL
, psize
, d
, TRACE_ENOSPC
, allocator
);
3565 return (SET_ERROR(ENOSPC
));
3569 metaslab_free_concrete(vdev_t
*vd
, uint64_t offset
, uint64_t asize
,
3570 boolean_t checkpoint
)
3573 spa_t
*spa
= vd
->vdev_spa
;
3575 ASSERT(vdev_is_concrete(vd
));
3576 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
3577 ASSERT3U(offset
>> vd
->vdev_ms_shift
, <, vd
->vdev_ms_count
);
3579 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
3581 VERIFY(!msp
->ms_condensing
);
3582 VERIFY3U(offset
, >=, msp
->ms_start
);
3583 VERIFY3U(offset
+ asize
, <=, msp
->ms_start
+ msp
->ms_size
);
3584 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
3585 VERIFY0(P2PHASE(asize
, 1ULL << vd
->vdev_ashift
));
3587 metaslab_check_free_impl(vd
, offset
, asize
);
3589 mutex_enter(&msp
->ms_lock
);
3590 if (range_tree_is_empty(msp
->ms_freeing
) &&
3591 range_tree_is_empty(msp
->ms_checkpointing
)) {
3592 vdev_dirty(vd
, VDD_METASLAB
, msp
, spa_syncing_txg(spa
));
3596 ASSERT(spa_has_checkpoint(spa
));
3597 range_tree_add(msp
->ms_checkpointing
, offset
, asize
);
3599 range_tree_add(msp
->ms_freeing
, offset
, asize
);
3601 mutex_exit(&msp
->ms_lock
);
3606 metaslab_free_impl_cb(uint64_t inner_offset
, vdev_t
*vd
, uint64_t offset
,
3607 uint64_t size
, void *arg
)
3609 boolean_t
*checkpoint
= arg
;
3611 ASSERT3P(checkpoint
, !=, NULL
);
3613 if (vd
->vdev_ops
->vdev_op_remap
!= NULL
)
3614 vdev_indirect_mark_obsolete(vd
, offset
, size
);
3616 metaslab_free_impl(vd
, offset
, size
, *checkpoint
);
3620 metaslab_free_impl(vdev_t
*vd
, uint64_t offset
, uint64_t size
,
3621 boolean_t checkpoint
)
3623 spa_t
*spa
= vd
->vdev_spa
;
3625 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
3627 if (spa_syncing_txg(spa
) > spa_freeze_txg(spa
))
3630 if (spa
->spa_vdev_removal
!= NULL
&&
3631 spa
->spa_vdev_removal
->svr_vdev_id
== vd
->vdev_id
&&
3632 vdev_is_concrete(vd
)) {
3634 * Note: we check if the vdev is concrete because when
3635 * we complete the removal, we first change the vdev to be
3636 * an indirect vdev (in open context), and then (in syncing
3637 * context) clear spa_vdev_removal.
3639 free_from_removing_vdev(vd
, offset
, size
);
3640 } else if (vd
->vdev_ops
->vdev_op_remap
!= NULL
) {
3641 vdev_indirect_mark_obsolete(vd
, offset
, size
);
3642 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
,
3643 metaslab_free_impl_cb
, &checkpoint
);
3645 metaslab_free_concrete(vd
, offset
, size
, checkpoint
);
3649 typedef struct remap_blkptr_cb_arg
{
3651 spa_remap_cb_t rbca_cb
;
3652 vdev_t
*rbca_remap_vd
;
3653 uint64_t rbca_remap_offset
;
3655 } remap_blkptr_cb_arg_t
;
3658 remap_blkptr_cb(uint64_t inner_offset
, vdev_t
*vd
, uint64_t offset
,
3659 uint64_t size
, void *arg
)
3661 remap_blkptr_cb_arg_t
*rbca
= arg
;
3662 blkptr_t
*bp
= rbca
->rbca_bp
;
3664 /* We can not remap split blocks. */
3665 if (size
!= DVA_GET_ASIZE(&bp
->blk_dva
[0]))
3667 ASSERT0(inner_offset
);
3669 if (rbca
->rbca_cb
!= NULL
) {
3671 * At this point we know that we are not handling split
3672 * blocks and we invoke the callback on the previous
3673 * vdev which must be indirect.
3675 ASSERT3P(rbca
->rbca_remap_vd
->vdev_ops
, ==, &vdev_indirect_ops
);
3677 rbca
->rbca_cb(rbca
->rbca_remap_vd
->vdev_id
,
3678 rbca
->rbca_remap_offset
, size
, rbca
->rbca_cb_arg
);
3680 /* set up remap_blkptr_cb_arg for the next call */
3681 rbca
->rbca_remap_vd
= vd
;
3682 rbca
->rbca_remap_offset
= offset
;
3686 * The phys birth time is that of dva[0]. This ensures that we know
3687 * when each dva was written, so that resilver can determine which
3688 * blocks need to be scrubbed (i.e. those written during the time
3689 * the vdev was offline). It also ensures that the key used in
3690 * the ARC hash table is unique (i.e. dva[0] + phys_birth). If
3691 * we didn't change the phys_birth, a lookup in the ARC for a
3692 * remapped BP could find the data that was previously stored at
3693 * this vdev + offset.
3695 vdev_t
*oldvd
= vdev_lookup_top(vd
->vdev_spa
,
3696 DVA_GET_VDEV(&bp
->blk_dva
[0]));
3697 vdev_indirect_births_t
*vib
= oldvd
->vdev_indirect_births
;
3698 bp
->blk_phys_birth
= vdev_indirect_births_physbirth(vib
,
3699 DVA_GET_OFFSET(&bp
->blk_dva
[0]), DVA_GET_ASIZE(&bp
->blk_dva
[0]));
3701 DVA_SET_VDEV(&bp
->blk_dva
[0], vd
->vdev_id
);
3702 DVA_SET_OFFSET(&bp
->blk_dva
[0], offset
);
3706 * If the block pointer contains any indirect DVAs, modify them to refer to
3707 * concrete DVAs. Note that this will sometimes not be possible, leaving
3708 * the indirect DVA in place. This happens if the indirect DVA spans multiple
3709 * segments in the mapping (i.e. it is a "split block").
3711 * If the BP was remapped, calls the callback on the original dva (note the
3712 * callback can be called multiple times if the original indirect DVA refers
3713 * to another indirect DVA, etc).
3715 * Returns TRUE if the BP was remapped.
3718 spa_remap_blkptr(spa_t
*spa
, blkptr_t
*bp
, spa_remap_cb_t callback
, void *arg
)
3720 remap_blkptr_cb_arg_t rbca
;
3722 if (!zfs_remap_blkptr_enable
)
3725 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_OBSOLETE_COUNTS
))
3729 * Dedup BP's can not be remapped, because ddt_phys_select() depends
3730 * on DVA[0] being the same in the BP as in the DDT (dedup table).
3732 if (BP_GET_DEDUP(bp
))
3736 * Gang blocks can not be remapped, because
3737 * zio_checksum_gang_verifier() depends on the DVA[0] that's in
3738 * the BP used to read the gang block header (GBH) being the same
3739 * as the DVA[0] that we allocated for the GBH.
3745 * Embedded BP's have no DVA to remap.
3747 if (BP_GET_NDVAS(bp
) < 1)
3751 * Note: we only remap dva[0]. If we remapped other dvas, we
3752 * would no longer know what their phys birth txg is.
3754 dva_t
*dva
= &bp
->blk_dva
[0];
3756 uint64_t offset
= DVA_GET_OFFSET(dva
);
3757 uint64_t size
= DVA_GET_ASIZE(dva
);
3758 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
3760 if (vd
->vdev_ops
->vdev_op_remap
== NULL
)
3764 rbca
.rbca_cb
= callback
;
3765 rbca
.rbca_remap_vd
= vd
;
3766 rbca
.rbca_remap_offset
= offset
;
3767 rbca
.rbca_cb_arg
= arg
;
3770 * remap_blkptr_cb() will be called in order for each level of
3771 * indirection, until a concrete vdev is reached or a split block is
3772 * encountered. old_vd and old_offset are updated within the callback
3773 * as we go from the one indirect vdev to the next one (either concrete
3774 * or indirect again) in that order.
3776 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
, remap_blkptr_cb
, &rbca
);
3778 /* Check if the DVA wasn't remapped because it is a split block */
3779 if (DVA_GET_VDEV(&rbca
.rbca_bp
->blk_dva
[0]) == vd
->vdev_id
)
3786 * Undo the allocation of a DVA which happened in the given transaction group.
3789 metaslab_unalloc_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
)
3793 uint64_t vdev
= DVA_GET_VDEV(dva
);
3794 uint64_t offset
= DVA_GET_OFFSET(dva
);
3795 uint64_t size
= DVA_GET_ASIZE(dva
);
3797 ASSERT(DVA_IS_VALID(dva
));
3798 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
3800 if (txg
> spa_freeze_txg(spa
))
3803 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
|| !DVA_IS_VALID(dva
) ||
3804 (offset
>> vd
->vdev_ms_shift
) >= vd
->vdev_ms_count
) {
3805 zfs_panic_recover("metaslab_free_dva(): bad DVA %llu:%llu:%llu",
3806 (u_longlong_t
)vdev
, (u_longlong_t
)offset
,
3807 (u_longlong_t
)size
);
3811 ASSERT(!vd
->vdev_removing
);
3812 ASSERT(vdev_is_concrete(vd
));
3813 ASSERT0(vd
->vdev_indirect_config
.vic_mapping_object
);
3814 ASSERT3P(vd
->vdev_indirect_mapping
, ==, NULL
);
3816 if (DVA_GET_GANG(dva
))
3817 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
3819 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
3821 mutex_enter(&msp
->ms_lock
);
3822 range_tree_remove(msp
->ms_allocating
[txg
& TXG_MASK
],
3825 VERIFY(!msp
->ms_condensing
);
3826 VERIFY3U(offset
, >=, msp
->ms_start
);
3827 VERIFY3U(offset
+ size
, <=, msp
->ms_start
+ msp
->ms_size
);
3828 VERIFY3U(range_tree_space(msp
->ms_allocatable
) + size
, <=,
3830 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
3831 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
3832 range_tree_add(msp
->ms_allocatable
, offset
, size
);
3833 mutex_exit(&msp
->ms_lock
);
3837 * Free the block represented by the given DVA.
3840 metaslab_free_dva(spa_t
*spa
, const dva_t
*dva
, boolean_t checkpoint
)
3842 uint64_t vdev
= DVA_GET_VDEV(dva
);
3843 uint64_t offset
= DVA_GET_OFFSET(dva
);
3844 uint64_t size
= DVA_GET_ASIZE(dva
);
3845 vdev_t
*vd
= vdev_lookup_top(spa
, vdev
);
3847 ASSERT(DVA_IS_VALID(dva
));
3848 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
3850 if (DVA_GET_GANG(dva
)) {
3851 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
3854 metaslab_free_impl(vd
, offset
, size
, checkpoint
);
3858 * Reserve some allocation slots. The reservation system must be called
3859 * before we call into the allocator. If there aren't any available slots
3860 * then the I/O will be throttled until an I/O completes and its slots are
3861 * freed up. The function returns true if it was successful in placing
3865 metaslab_class_throttle_reserve(metaslab_class_t
*mc
, int slots
, int allocator
,
3866 zio_t
*zio
, int flags
)
3868 uint64_t available_slots
= 0;
3869 boolean_t slot_reserved
= B_FALSE
;
3870 uint64_t max
= mc
->mc_alloc_max_slots
[allocator
];
3872 ASSERT(mc
->mc_alloc_throttle_enabled
);
3873 mutex_enter(&mc
->mc_lock
);
3875 uint64_t reserved_slots
=
3876 zfs_refcount_count(&mc
->mc_alloc_slots
[allocator
]);
3877 if (reserved_slots
< max
)
3878 available_slots
= max
- reserved_slots
;
3880 if (slots
<= available_slots
|| GANG_ALLOCATION(flags
) ||
3881 flags
& METASLAB_MUST_RESERVE
) {
3883 * We reserve the slots individually so that we can unreserve
3884 * them individually when an I/O completes.
3886 for (int d
= 0; d
< slots
; d
++) {
3888 zfs_refcount_add(&mc
->mc_alloc_slots
[allocator
],
3891 zio
->io_flags
|= ZIO_FLAG_IO_ALLOCATING
;
3892 slot_reserved
= B_TRUE
;
3895 mutex_exit(&mc
->mc_lock
);
3896 return (slot_reserved
);
3900 metaslab_class_throttle_unreserve(metaslab_class_t
*mc
, int slots
,
3901 int allocator
, zio_t
*zio
)
3903 ASSERT(mc
->mc_alloc_throttle_enabled
);
3904 mutex_enter(&mc
->mc_lock
);
3905 for (int d
= 0; d
< slots
; d
++) {
3906 (void) zfs_refcount_remove(&mc
->mc_alloc_slots
[allocator
],
3909 mutex_exit(&mc
->mc_lock
);
3913 metaslab_claim_concrete(vdev_t
*vd
, uint64_t offset
, uint64_t size
,
3917 spa_t
*spa
= vd
->vdev_spa
;
3920 if (offset
>> vd
->vdev_ms_shift
>= vd
->vdev_ms_count
)
3921 return (SET_ERROR(ENXIO
));
3923 ASSERT3P(vd
->vdev_ms
, !=, NULL
);
3924 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
3926 mutex_enter(&msp
->ms_lock
);
3928 if ((txg
!= 0 && spa_writeable(spa
)) || !msp
->ms_loaded
) {
3929 error
= metaslab_activate(msp
, 0, METASLAB_WEIGHT_CLAIM
);
3930 if (error
== EBUSY
) {
3931 ASSERT(msp
->ms_loaded
);
3932 ASSERT(msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
3938 !range_tree_contains(msp
->ms_allocatable
, offset
, size
))
3939 error
= SET_ERROR(ENOENT
);
3941 if (error
|| txg
== 0) { /* txg == 0 indicates dry run */
3942 mutex_exit(&msp
->ms_lock
);
3946 VERIFY(!msp
->ms_condensing
);
3947 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
3948 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
3949 VERIFY3U(range_tree_space(msp
->ms_allocatable
) - size
, <=,
3951 range_tree_remove(msp
->ms_allocatable
, offset
, size
);
3953 if (spa_writeable(spa
)) { /* don't dirty if we're zdb(1M) */
3954 if (range_tree_is_empty(msp
->ms_allocating
[txg
& TXG_MASK
]))
3955 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
3956 range_tree_add(msp
->ms_allocating
[txg
& TXG_MASK
],
3960 mutex_exit(&msp
->ms_lock
);
3965 typedef struct metaslab_claim_cb_arg_t
{
3968 } metaslab_claim_cb_arg_t
;
3972 metaslab_claim_impl_cb(uint64_t inner_offset
, vdev_t
*vd
, uint64_t offset
,
3973 uint64_t size
, void *arg
)
3975 metaslab_claim_cb_arg_t
*mcca_arg
= arg
;
3977 if (mcca_arg
->mcca_error
== 0) {
3978 mcca_arg
->mcca_error
= metaslab_claim_concrete(vd
, offset
,
3979 size
, mcca_arg
->mcca_txg
);
3984 metaslab_claim_impl(vdev_t
*vd
, uint64_t offset
, uint64_t size
, uint64_t txg
)
3986 if (vd
->vdev_ops
->vdev_op_remap
!= NULL
) {
3987 metaslab_claim_cb_arg_t arg
;
3990 * Only zdb(1M) can claim on indirect vdevs. This is used
3991 * to detect leaks of mapped space (that are not accounted
3992 * for in the obsolete counts, spacemap, or bpobj).
3994 ASSERT(!spa_writeable(vd
->vdev_spa
));
3998 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
,
3999 metaslab_claim_impl_cb
, &arg
);
4001 if (arg
.mcca_error
== 0) {
4002 arg
.mcca_error
= metaslab_claim_concrete(vd
,
4005 return (arg
.mcca_error
);
4007 return (metaslab_claim_concrete(vd
, offset
, size
, txg
));
4012 * Intent log support: upon opening the pool after a crash, notify the SPA
4013 * of blocks that the intent log has allocated for immediate write, but
4014 * which are still considered free by the SPA because the last transaction
4015 * group didn't commit yet.
4018 metaslab_claim_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
)
4020 uint64_t vdev
= DVA_GET_VDEV(dva
);
4021 uint64_t offset
= DVA_GET_OFFSET(dva
);
4022 uint64_t size
= DVA_GET_ASIZE(dva
);
4025 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
) {
4026 return (SET_ERROR(ENXIO
));
4029 ASSERT(DVA_IS_VALID(dva
));
4031 if (DVA_GET_GANG(dva
))
4032 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
4034 return (metaslab_claim_impl(vd
, offset
, size
, txg
));
4038 metaslab_alloc(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
, blkptr_t
*bp
,
4039 int ndvas
, uint64_t txg
, blkptr_t
*hintbp
, int flags
,
4040 zio_alloc_list_t
*zal
, zio_t
*zio
, int allocator
)
4042 dva_t
*dva
= bp
->blk_dva
;
4043 dva_t
*hintdva
= hintbp
->blk_dva
;
4046 ASSERT(bp
->blk_birth
== 0);
4047 ASSERT(BP_PHYSICAL_BIRTH(bp
) == 0);
4049 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
4051 if (mc
->mc_rotor
== NULL
) { /* no vdevs in this class */
4052 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4053 return (SET_ERROR(ENOSPC
));
4056 ASSERT(ndvas
> 0 && ndvas
<= spa_max_replication(spa
));
4057 ASSERT(BP_GET_NDVAS(bp
) == 0);
4058 ASSERT(hintbp
== NULL
|| ndvas
<= BP_GET_NDVAS(hintbp
));
4059 ASSERT3P(zal
, !=, NULL
);
4061 for (int d
= 0; d
< ndvas
; d
++) {
4062 error
= metaslab_alloc_dva(spa
, mc
, psize
, dva
, d
, hintdva
,
4063 txg
, flags
, zal
, allocator
);
4065 for (d
--; d
>= 0; d
--) {
4066 metaslab_unalloc_dva(spa
, &dva
[d
], txg
);
4067 metaslab_group_alloc_decrement(spa
,
4068 DVA_GET_VDEV(&dva
[d
]), zio
, flags
,
4069 allocator
, B_FALSE
);
4070 bzero(&dva
[d
], sizeof (dva_t
));
4072 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4076 * Update the metaslab group's queue depth
4077 * based on the newly allocated dva.
4079 metaslab_group_alloc_increment(spa
,
4080 DVA_GET_VDEV(&dva
[d
]), zio
, flags
, allocator
);
4085 ASSERT(BP_GET_NDVAS(bp
) == ndvas
);
4087 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4089 BP_SET_BIRTH(bp
, txg
, 0);
4095 metaslab_free(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
, boolean_t now
)
4097 const dva_t
*dva
= bp
->blk_dva
;
4098 int ndvas
= BP_GET_NDVAS(bp
);
4100 ASSERT(!BP_IS_HOLE(bp
));
4101 ASSERT(!now
|| bp
->blk_birth
>= spa_syncing_txg(spa
));
4104 * If we have a checkpoint for the pool we need to make sure that
4105 * the blocks that we free that are part of the checkpoint won't be
4106 * reused until the checkpoint is discarded or we revert to it.
4108 * The checkpoint flag is passed down the metaslab_free code path
4109 * and is set whenever we want to add a block to the checkpoint's
4110 * accounting. That is, we "checkpoint" blocks that existed at the
4111 * time the checkpoint was created and are therefore referenced by
4112 * the checkpointed uberblock.
4114 * Note that, we don't checkpoint any blocks if the current
4115 * syncing txg <= spa_checkpoint_txg. We want these frees to sync
4116 * normally as they will be referenced by the checkpointed uberblock.
4118 boolean_t checkpoint
= B_FALSE
;
4119 if (bp
->blk_birth
<= spa
->spa_checkpoint_txg
&&
4120 spa_syncing_txg(spa
) > spa
->spa_checkpoint_txg
) {
4122 * At this point, if the block is part of the checkpoint
4123 * there is no way it was created in the current txg.
4126 ASSERT3U(spa_syncing_txg(spa
), ==, txg
);
4127 checkpoint
= B_TRUE
;
4130 spa_config_enter(spa
, SCL_FREE
, FTAG
, RW_READER
);
4132 for (int d
= 0; d
< ndvas
; d
++) {
4134 metaslab_unalloc_dva(spa
, &dva
[d
], txg
);
4136 ASSERT3U(txg
, ==, spa_syncing_txg(spa
));
4137 metaslab_free_dva(spa
, &dva
[d
], checkpoint
);
4141 spa_config_exit(spa
, SCL_FREE
, FTAG
);
4145 metaslab_claim(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
)
4147 const dva_t
*dva
= bp
->blk_dva
;
4148 int ndvas
= BP_GET_NDVAS(bp
);
4151 ASSERT(!BP_IS_HOLE(bp
));
4155 * First do a dry run to make sure all DVAs are claimable,
4156 * so we don't have to unwind from partial failures below.
4158 if ((error
= metaslab_claim(spa
, bp
, 0)) != 0)
4162 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
4164 for (int d
= 0; d
< ndvas
; d
++) {
4165 error
= metaslab_claim_dva(spa
, &dva
[d
], txg
);
4170 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
4172 ASSERT(error
== 0 || txg
== 0);
4178 metaslab_fastwrite_mark(spa_t
*spa
, const blkptr_t
*bp
)
4180 const dva_t
*dva
= bp
->blk_dva
;
4181 int ndvas
= BP_GET_NDVAS(bp
);
4182 uint64_t psize
= BP_GET_PSIZE(bp
);
4186 ASSERT(!BP_IS_HOLE(bp
));
4187 ASSERT(!BP_IS_EMBEDDED(bp
));
4190 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4192 for (d
= 0; d
< ndvas
; d
++) {
4193 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
4195 atomic_add_64(&vd
->vdev_pending_fastwrite
, psize
);
4198 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4202 metaslab_fastwrite_unmark(spa_t
*spa
, const blkptr_t
*bp
)
4204 const dva_t
*dva
= bp
->blk_dva
;
4205 int ndvas
= BP_GET_NDVAS(bp
);
4206 uint64_t psize
= BP_GET_PSIZE(bp
);
4210 ASSERT(!BP_IS_HOLE(bp
));
4211 ASSERT(!BP_IS_EMBEDDED(bp
));
4214 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4216 for (d
= 0; d
< ndvas
; d
++) {
4217 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
4219 ASSERT3U(vd
->vdev_pending_fastwrite
, >=, psize
);
4220 atomic_sub_64(&vd
->vdev_pending_fastwrite
, psize
);
4223 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4228 metaslab_check_free_impl_cb(uint64_t inner
, vdev_t
*vd
, uint64_t offset
,
4229 uint64_t size
, void *arg
)
4231 if (vd
->vdev_ops
== &vdev_indirect_ops
)
4234 metaslab_check_free_impl(vd
, offset
, size
);
4238 metaslab_check_free_impl(vdev_t
*vd
, uint64_t offset
, uint64_t size
)
4241 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
4243 if ((zfs_flags
& ZFS_DEBUG_ZIO_FREE
) == 0)
4246 if (vd
->vdev_ops
->vdev_op_remap
!= NULL
) {
4247 vd
->vdev_ops
->vdev_op_remap(vd
, offset
, size
,
4248 metaslab_check_free_impl_cb
, NULL
);
4252 ASSERT(vdev_is_concrete(vd
));
4253 ASSERT3U(offset
>> vd
->vdev_ms_shift
, <, vd
->vdev_ms_count
);
4254 ASSERT3U(spa_config_held(spa
, SCL_ALL
, RW_READER
), !=, 0);
4256 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
4258 mutex_enter(&msp
->ms_lock
);
4259 if (msp
->ms_loaded
) {
4260 range_tree_verify_not_present(msp
->ms_allocatable
,
4264 range_tree_verify_not_present(msp
->ms_freeing
, offset
, size
);
4265 range_tree_verify_not_present(msp
->ms_checkpointing
, offset
, size
);
4266 range_tree_verify_not_present(msp
->ms_freed
, offset
, size
);
4267 for (int j
= 0; j
< TXG_DEFER_SIZE
; j
++)
4268 range_tree_verify_not_present(msp
->ms_defer
[j
], offset
, size
);
4269 mutex_exit(&msp
->ms_lock
);
4273 metaslab_check_free(spa_t
*spa
, const blkptr_t
*bp
)
4275 if ((zfs_flags
& ZFS_DEBUG_ZIO_FREE
) == 0)
4278 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4279 for (int i
= 0; i
< BP_GET_NDVAS(bp
); i
++) {
4280 uint64_t vdev
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
4281 vdev_t
*vd
= vdev_lookup_top(spa
, vdev
);
4282 uint64_t offset
= DVA_GET_OFFSET(&bp
->blk_dva
[i
]);
4283 uint64_t size
= DVA_GET_ASIZE(&bp
->blk_dva
[i
]);
4285 if (DVA_GET_GANG(&bp
->blk_dva
[i
]))
4286 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
4288 ASSERT3P(vd
, !=, NULL
);
4290 metaslab_check_free_impl(vd
, offset
, size
);
4292 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4295 #if defined(_KERNEL)
4297 module_param(metaslab_aliquot
, ulong
, 0644);
4298 MODULE_PARM_DESC(metaslab_aliquot
,
4299 "allocation granularity (a.k.a. stripe size)");
4301 module_param(metaslab_debug_load
, int, 0644);
4302 MODULE_PARM_DESC(metaslab_debug_load
,
4303 "load all metaslabs when pool is first opened");
4305 module_param(metaslab_debug_unload
, int, 0644);
4306 MODULE_PARM_DESC(metaslab_debug_unload
,
4307 "prevent metaslabs from being unloaded");
4309 module_param(metaslab_preload_enabled
, int, 0644);
4310 MODULE_PARM_DESC(metaslab_preload_enabled
,
4311 "preload potential metaslabs during reassessment");
4313 module_param(zfs_mg_noalloc_threshold
, int, 0644);
4314 MODULE_PARM_DESC(zfs_mg_noalloc_threshold
,
4315 "percentage of free space for metaslab group to allow allocation");
4317 module_param(zfs_mg_fragmentation_threshold
, int, 0644);
4318 MODULE_PARM_DESC(zfs_mg_fragmentation_threshold
,
4319 "fragmentation for metaslab group to allow allocation");
4321 module_param(zfs_metaslab_fragmentation_threshold
, int, 0644);
4322 MODULE_PARM_DESC(zfs_metaslab_fragmentation_threshold
,
4323 "fragmentation for metaslab to allow allocation");
4325 module_param(metaslab_fragmentation_factor_enabled
, int, 0644);
4326 MODULE_PARM_DESC(metaslab_fragmentation_factor_enabled
,
4327 "use the fragmentation metric to prefer less fragmented metaslabs");
4329 module_param(metaslab_lba_weighting_enabled
, int, 0644);
4330 MODULE_PARM_DESC(metaslab_lba_weighting_enabled
,
4331 "prefer metaslabs with lower LBAs");
4333 module_param(metaslab_bias_enabled
, int, 0644);
4334 MODULE_PARM_DESC(metaslab_bias_enabled
,
4335 "enable metaslab group biasing");
4337 module_param(zfs_metaslab_segment_weight_enabled
, int, 0644);
4338 MODULE_PARM_DESC(zfs_metaslab_segment_weight_enabled
,
4339 "enable segment-based metaslab selection");
4341 module_param(zfs_metaslab_switch_threshold
, int, 0644);
4342 MODULE_PARM_DESC(zfs_metaslab_switch_threshold
,
4343 "segment-based metaslab selection maximum buckets before switching");
4345 module_param(metaslab_force_ganging
, ulong
, 0644);
4346 MODULE_PARM_DESC(metaslab_force_ganging
,
4347 "blocks larger than this size are forced to be gang blocks");