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) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
27 #include <sys/zfs_context.h>
29 #include <sys/dmu_tx.h>
30 #include <sys/space_map.h>
31 #include <sys/metaslab_impl.h>
32 #include <sys/vdev_impl.h>
34 #include <sys/spa_impl.h>
36 #define WITH_DF_BLOCK_ALLOCATOR
39 * Allow allocations to switch to gang blocks quickly. We do this to
40 * avoid having to load lots of space_maps in a given txg. There are,
41 * however, some cases where we want to avoid "fast" ganging and instead
42 * we want to do an exhaustive search of all metaslabs on this device.
43 * Currently we don't allow any gang, zil, or dump device related allocations
46 #define CAN_FASTGANG(flags) \
47 (!((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER | \
48 METASLAB_GANG_AVOID)))
50 #define METASLAB_WEIGHT_PRIMARY (1ULL << 63)
51 #define METASLAB_WEIGHT_SECONDARY (1ULL << 62)
52 #define METASLAB_ACTIVE_MASK \
53 (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
55 uint64_t metaslab_aliquot
= 512ULL << 10;
56 uint64_t metaslab_gang_bang
= SPA_MAXBLOCKSIZE
+ 1; /* force gang blocks */
59 * The in-core space map representation is more compact than its on-disk form.
60 * The zfs_condense_pct determines how much more compact the in-core
61 * space_map representation must be before we compact it on-disk.
62 * Values should be greater than or equal to 100.
64 int zfs_condense_pct
= 200;
67 * This value defines the number of allowed allocation failures per vdev.
68 * If a device reaches this threshold in a given txg then we consider skipping
69 * allocations on that device. The value of zfs_mg_alloc_failures is computed
70 * in zio_init() unless it has been overridden in /etc/system.
72 int zfs_mg_alloc_failures
= 0;
75 * The zfs_mg_noalloc_threshold defines which metaslab groups should
76 * be eligible for allocation. The value is defined as a percentage of
77 * a free space. Metaslab groups that have more free space than
78 * zfs_mg_noalloc_threshold are always eligible for allocations. Once
79 * a metaslab group's free space is less than or equal to the
80 * zfs_mg_noalloc_threshold the allocator will avoid allocating to that
81 * group unless all groups in the pool have reached zfs_mg_noalloc_threshold.
82 * Once all groups in the pool reach zfs_mg_noalloc_threshold then all
83 * groups are allowed to accept allocations. Gang blocks are always
84 * eligible to allocate on any metaslab group. The default value of 0 means
85 * no metaslab group will be excluded based on this criterion.
87 int zfs_mg_noalloc_threshold
= 0;
90 * When set will load all metaslabs when pool is first opened.
92 int metaslab_debug_load
= 0;
95 * When set will prevent metaslabs from being unloaded.
97 int metaslab_debug_unload
= 0;
100 * Minimum size which forces the dynamic allocator to change
101 * it's allocation strategy. Once the space map cannot satisfy
102 * an allocation of this size then it switches to using more
103 * aggressive strategy (i.e search by size rather than offset).
105 uint64_t metaslab_df_alloc_threshold
= SPA_MAXBLOCKSIZE
;
108 * The minimum free space, in percent, which must be available
109 * in a space map to continue allocations in a first-fit fashion.
110 * Once the space_map's free space drops below this level we dynamically
111 * switch to using best-fit allocations.
113 int metaslab_df_free_pct
= 4;
116 * A metaslab is considered "free" if it contains a contiguous
117 * segment which is greater than metaslab_min_alloc_size.
119 uint64_t metaslab_min_alloc_size
= DMU_MAX_ACCESS
;
122 * Percentage of all cpus that can be used by the metaslab taskq.
124 int metaslab_load_pct
= 50;
127 * Determines how many txgs a metaslab may remain loaded without having any
128 * allocations from it. As long as a metaslab continues to be used we will
131 int metaslab_unload_delay
= TXG_SIZE
* 2;
134 * Should we be willing to write data to degraded vdevs?
136 boolean_t zfs_write_to_degraded
= B_FALSE
;
139 * Max number of metaslabs per group to preload.
141 int metaslab_preload_limit
= SPA_DVAS_PER_BP
;
144 * Enable/disable preloading of metaslab.
146 boolean_t metaslab_preload_enabled
= B_TRUE
;
149 * Enable/disable additional weight factor for each metaslab.
151 boolean_t metaslab_weight_factor_enable
= B_FALSE
;
155 * ==========================================================================
157 * ==========================================================================
160 metaslab_class_create(spa_t
*spa
, metaslab_ops_t
*ops
)
162 metaslab_class_t
*mc
;
164 mc
= kmem_zalloc(sizeof (metaslab_class_t
), KM_PUSHPAGE
);
169 mutex_init(&mc
->mc_fastwrite_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
175 metaslab_class_destroy(metaslab_class_t
*mc
)
177 ASSERT(mc
->mc_rotor
== NULL
);
178 ASSERT(mc
->mc_alloc
== 0);
179 ASSERT(mc
->mc_deferred
== 0);
180 ASSERT(mc
->mc_space
== 0);
181 ASSERT(mc
->mc_dspace
== 0);
183 mutex_destroy(&mc
->mc_fastwrite_lock
);
184 kmem_free(mc
, sizeof (metaslab_class_t
));
188 metaslab_class_validate(metaslab_class_t
*mc
)
190 metaslab_group_t
*mg
;
194 * Must hold one of the spa_config locks.
196 ASSERT(spa_config_held(mc
->mc_spa
, SCL_ALL
, RW_READER
) ||
197 spa_config_held(mc
->mc_spa
, SCL_ALL
, RW_WRITER
));
199 if ((mg
= mc
->mc_rotor
) == NULL
)
204 ASSERT(vd
->vdev_mg
!= NULL
);
205 ASSERT3P(vd
->vdev_top
, ==, vd
);
206 ASSERT3P(mg
->mg_class
, ==, mc
);
207 ASSERT3P(vd
->vdev_ops
, !=, &vdev_hole_ops
);
208 } while ((mg
= mg
->mg_next
) != mc
->mc_rotor
);
214 metaslab_class_space_update(metaslab_class_t
*mc
, int64_t alloc_delta
,
215 int64_t defer_delta
, int64_t space_delta
, int64_t dspace_delta
)
217 atomic_add_64(&mc
->mc_alloc
, alloc_delta
);
218 atomic_add_64(&mc
->mc_deferred
, defer_delta
);
219 atomic_add_64(&mc
->mc_space
, space_delta
);
220 atomic_add_64(&mc
->mc_dspace
, dspace_delta
);
224 metaslab_class_get_alloc(metaslab_class_t
*mc
)
226 return (mc
->mc_alloc
);
230 metaslab_class_get_deferred(metaslab_class_t
*mc
)
232 return (mc
->mc_deferred
);
236 metaslab_class_get_space(metaslab_class_t
*mc
)
238 return (mc
->mc_space
);
242 metaslab_class_get_dspace(metaslab_class_t
*mc
)
244 return (spa_deflate(mc
->mc_spa
) ? mc
->mc_dspace
: mc
->mc_space
);
248 * ==========================================================================
250 * ==========================================================================
253 metaslab_compare(const void *x1
, const void *x2
)
255 const metaslab_t
*m1
= x1
;
256 const metaslab_t
*m2
= x2
;
258 if (m1
->ms_weight
< m2
->ms_weight
)
260 if (m1
->ms_weight
> m2
->ms_weight
)
264 * If the weights are identical, use the offset to force uniqueness.
266 if (m1
->ms_start
< m2
->ms_start
)
268 if (m1
->ms_start
> m2
->ms_start
)
271 ASSERT3P(m1
, ==, m2
);
277 * Update the allocatable flag and the metaslab group's capacity.
278 * The allocatable flag is set to true if the capacity is below
279 * the zfs_mg_noalloc_threshold. If a metaslab group transitions
280 * from allocatable to non-allocatable or vice versa then the metaslab
281 * group's class is updated to reflect the transition.
284 metaslab_group_alloc_update(metaslab_group_t
*mg
)
286 vdev_t
*vd
= mg
->mg_vd
;
287 metaslab_class_t
*mc
= mg
->mg_class
;
288 vdev_stat_t
*vs
= &vd
->vdev_stat
;
289 boolean_t was_allocatable
;
291 ASSERT(vd
== vd
->vdev_top
);
293 mutex_enter(&mg
->mg_lock
);
294 was_allocatable
= mg
->mg_allocatable
;
296 mg
->mg_free_capacity
= ((vs
->vs_space
- vs
->vs_alloc
) * 100) /
299 mg
->mg_allocatable
= (mg
->mg_free_capacity
> zfs_mg_noalloc_threshold
);
302 * The mc_alloc_groups maintains a count of the number of
303 * groups in this metaslab class that are still above the
304 * zfs_mg_noalloc_threshold. This is used by the allocating
305 * threads to determine if they should avoid allocations to
306 * a given group. The allocator will avoid allocations to a group
307 * if that group has reached or is below the zfs_mg_noalloc_threshold
308 * and there are still other groups that are above the threshold.
309 * When a group transitions from allocatable to non-allocatable or
310 * vice versa we update the metaslab class to reflect that change.
311 * When the mc_alloc_groups value drops to 0 that means that all
312 * groups have reached the zfs_mg_noalloc_threshold making all groups
313 * eligible for allocations. This effectively means that all devices
314 * are balanced again.
316 if (was_allocatable
&& !mg
->mg_allocatable
)
317 mc
->mc_alloc_groups
--;
318 else if (!was_allocatable
&& mg
->mg_allocatable
)
319 mc
->mc_alloc_groups
++;
320 mutex_exit(&mg
->mg_lock
);
324 metaslab_group_create(metaslab_class_t
*mc
, vdev_t
*vd
)
326 metaslab_group_t
*mg
;
328 mg
= kmem_zalloc(sizeof (metaslab_group_t
), KM_PUSHPAGE
);
329 mutex_init(&mg
->mg_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
330 avl_create(&mg
->mg_metaslab_tree
, metaslab_compare
,
331 sizeof (metaslab_t
), offsetof(struct metaslab
, ms_group_node
));
334 mg
->mg_activation_count
= 0;
336 mg
->mg_taskq
= taskq_create("metaslab_group_taskq", metaslab_load_pct
,
337 minclsyspri
, 10, INT_MAX
, TASKQ_THREADS_CPU_PCT
);
343 metaslab_group_destroy(metaslab_group_t
*mg
)
345 ASSERT(mg
->mg_prev
== NULL
);
346 ASSERT(mg
->mg_next
== NULL
);
348 * We may have gone below zero with the activation count
349 * either because we never activated in the first place or
350 * because we're done, and possibly removing the vdev.
352 ASSERT(mg
->mg_activation_count
<= 0);
354 taskq_destroy(mg
->mg_taskq
);
355 avl_destroy(&mg
->mg_metaslab_tree
);
356 mutex_destroy(&mg
->mg_lock
);
357 kmem_free(mg
, sizeof (metaslab_group_t
));
361 metaslab_group_activate(metaslab_group_t
*mg
)
363 metaslab_class_t
*mc
= mg
->mg_class
;
364 metaslab_group_t
*mgprev
, *mgnext
;
366 ASSERT(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_WRITER
));
368 ASSERT(mc
->mc_rotor
!= mg
);
369 ASSERT(mg
->mg_prev
== NULL
);
370 ASSERT(mg
->mg_next
== NULL
);
371 ASSERT(mg
->mg_activation_count
<= 0);
373 if (++mg
->mg_activation_count
<= 0)
376 mg
->mg_aliquot
= metaslab_aliquot
* MAX(1, mg
->mg_vd
->vdev_children
);
377 metaslab_group_alloc_update(mg
);
379 if ((mgprev
= mc
->mc_rotor
) == NULL
) {
383 mgnext
= mgprev
->mg_next
;
384 mg
->mg_prev
= mgprev
;
385 mg
->mg_next
= mgnext
;
386 mgprev
->mg_next
= mg
;
387 mgnext
->mg_prev
= mg
;
393 metaslab_group_passivate(metaslab_group_t
*mg
)
395 metaslab_class_t
*mc
= mg
->mg_class
;
396 metaslab_group_t
*mgprev
, *mgnext
;
398 ASSERT(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_WRITER
));
400 if (--mg
->mg_activation_count
!= 0) {
401 ASSERT(mc
->mc_rotor
!= mg
);
402 ASSERT(mg
->mg_prev
== NULL
);
403 ASSERT(mg
->mg_next
== NULL
);
404 ASSERT(mg
->mg_activation_count
< 0);
408 taskq_wait(mg
->mg_taskq
);
410 mgprev
= mg
->mg_prev
;
411 mgnext
= mg
->mg_next
;
416 mc
->mc_rotor
= mgnext
;
417 mgprev
->mg_next
= mgnext
;
418 mgnext
->mg_prev
= mgprev
;
426 metaslab_group_add(metaslab_group_t
*mg
, metaslab_t
*msp
)
428 mutex_enter(&mg
->mg_lock
);
429 ASSERT(msp
->ms_group
== NULL
);
432 avl_add(&mg
->mg_metaslab_tree
, msp
);
433 mutex_exit(&mg
->mg_lock
);
437 metaslab_group_remove(metaslab_group_t
*mg
, metaslab_t
*msp
)
439 mutex_enter(&mg
->mg_lock
);
440 ASSERT(msp
->ms_group
== mg
);
441 avl_remove(&mg
->mg_metaslab_tree
, msp
);
442 msp
->ms_group
= NULL
;
443 mutex_exit(&mg
->mg_lock
);
447 metaslab_group_sort(metaslab_group_t
*mg
, metaslab_t
*msp
, uint64_t weight
)
450 * Although in principle the weight can be any value, in
451 * practice we do not use values in the range [1, 510].
453 ASSERT(weight
>= SPA_MINBLOCKSIZE
-1 || weight
== 0);
454 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
456 mutex_enter(&mg
->mg_lock
);
457 ASSERT(msp
->ms_group
== mg
);
458 avl_remove(&mg
->mg_metaslab_tree
, msp
);
459 msp
->ms_weight
= weight
;
460 avl_add(&mg
->mg_metaslab_tree
, msp
);
461 mutex_exit(&mg
->mg_lock
);
465 * Determine if a given metaslab group should skip allocations. A metaslab
466 * group should avoid allocations if its used capacity has crossed the
467 * zfs_mg_noalloc_threshold and there is at least one metaslab group
468 * that can still handle allocations.
471 metaslab_group_allocatable(metaslab_group_t
*mg
)
473 vdev_t
*vd
= mg
->mg_vd
;
474 spa_t
*spa
= vd
->vdev_spa
;
475 metaslab_class_t
*mc
= mg
->mg_class
;
478 * A metaslab group is considered allocatable if its free capacity
479 * is greater than the set value of zfs_mg_noalloc_threshold, it's
480 * associated with a slog, or there are no other metaslab groups
481 * with free capacity greater than zfs_mg_noalloc_threshold.
483 return (mg
->mg_free_capacity
> zfs_mg_noalloc_threshold
||
484 mc
!= spa_normal_class(spa
) || mc
->mc_alloc_groups
== 0);
488 * ==========================================================================
489 * Range tree callbacks
490 * ==========================================================================
494 * Comparison function for the private size-ordered tree. Tree is sorted
495 * by size, larger sizes at the end of the tree.
498 metaslab_rangesize_compare(const void *x1
, const void *x2
)
500 const range_seg_t
*r1
= x1
;
501 const range_seg_t
*r2
= x2
;
502 uint64_t rs_size1
= r1
->rs_end
- r1
->rs_start
;
503 uint64_t rs_size2
= r2
->rs_end
- r2
->rs_start
;
505 if (rs_size1
< rs_size2
)
507 if (rs_size1
> rs_size2
)
510 if (r1
->rs_start
< r2
->rs_start
)
513 if (r1
->rs_start
> r2
->rs_start
)
520 * Create any block allocator specific components. The current allocators
521 * rely on using both a size-ordered range_tree_t and an array of uint64_t's.
524 metaslab_rt_create(range_tree_t
*rt
, void *arg
)
526 metaslab_t
*msp
= arg
;
528 ASSERT3P(rt
->rt_arg
, ==, msp
);
529 ASSERT(msp
->ms_tree
== NULL
);
531 avl_create(&msp
->ms_size_tree
, metaslab_rangesize_compare
,
532 sizeof (range_seg_t
), offsetof(range_seg_t
, rs_pp_node
));
536 * Destroy the block allocator specific components.
539 metaslab_rt_destroy(range_tree_t
*rt
, void *arg
)
541 metaslab_t
*msp
= arg
;
543 ASSERT3P(rt
->rt_arg
, ==, msp
);
544 ASSERT3P(msp
->ms_tree
, ==, rt
);
545 ASSERT0(avl_numnodes(&msp
->ms_size_tree
));
547 avl_destroy(&msp
->ms_size_tree
);
551 metaslab_rt_add(range_tree_t
*rt
, range_seg_t
*rs
, void *arg
)
553 metaslab_t
*msp
= arg
;
555 ASSERT3P(rt
->rt_arg
, ==, msp
);
556 ASSERT3P(msp
->ms_tree
, ==, rt
);
557 VERIFY(!msp
->ms_condensing
);
558 avl_add(&msp
->ms_size_tree
, rs
);
562 metaslab_rt_remove(range_tree_t
*rt
, range_seg_t
*rs
, void *arg
)
564 metaslab_t
*msp
= arg
;
566 ASSERT3P(rt
->rt_arg
, ==, msp
);
567 ASSERT3P(msp
->ms_tree
, ==, rt
);
568 VERIFY(!msp
->ms_condensing
);
569 avl_remove(&msp
->ms_size_tree
, rs
);
573 metaslab_rt_vacate(range_tree_t
*rt
, void *arg
)
575 metaslab_t
*msp
= arg
;
577 ASSERT3P(rt
->rt_arg
, ==, msp
);
578 ASSERT3P(msp
->ms_tree
, ==, rt
);
581 * Normally one would walk the tree freeing nodes along the way.
582 * Since the nodes are shared with the range trees we can avoid
583 * walking all nodes and just reinitialize the avl tree. The nodes
584 * will be freed by the range tree, so we don't want to free them here.
586 avl_create(&msp
->ms_size_tree
, metaslab_rangesize_compare
,
587 sizeof (range_seg_t
), offsetof(range_seg_t
, rs_pp_node
));
590 static range_tree_ops_t metaslab_rt_ops
= {
599 * ==========================================================================
600 * Metaslab block operations
601 * ==========================================================================
605 * Return the maximum contiguous segment within the metaslab.
608 metaslab_block_maxsize(metaslab_t
*msp
)
610 avl_tree_t
*t
= &msp
->ms_size_tree
;
613 if (t
== NULL
|| (rs
= avl_last(t
)) == NULL
)
616 return (rs
->rs_end
- rs
->rs_start
);
620 metaslab_block_alloc(metaslab_t
*msp
, uint64_t size
)
623 range_tree_t
*rt
= msp
->ms_tree
;
625 VERIFY(!msp
->ms_condensing
);
627 start
= msp
->ms_ops
->msop_alloc(msp
, size
);
628 if (start
!= -1ULL) {
629 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
631 VERIFY0(P2PHASE(start
, 1ULL << vd
->vdev_ashift
));
632 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
633 VERIFY3U(range_tree_space(rt
) - size
, <=, msp
->ms_size
);
634 range_tree_remove(rt
, start
, size
);
640 * ==========================================================================
641 * Common allocator routines
642 * ==========================================================================
645 #if defined(WITH_FF_BLOCK_ALLOCATOR) || \
646 defined(WITH_DF_BLOCK_ALLOCATOR) || \
647 defined(WITH_CF_BLOCK_ALLOCATOR)
649 * This is a helper function that can be used by the allocator to find
650 * a suitable block to allocate. This will search the specified AVL
651 * tree looking for a block that matches the specified criteria.
654 metaslab_block_picker(avl_tree_t
*t
, uint64_t *cursor
, uint64_t size
,
657 range_seg_t
*rs
, rsearch
;
660 rsearch
.rs_start
= *cursor
;
661 rsearch
.rs_end
= *cursor
+ size
;
663 rs
= avl_find(t
, &rsearch
, &where
);
665 rs
= avl_nearest(t
, where
, AVL_AFTER
);
668 uint64_t offset
= P2ROUNDUP(rs
->rs_start
, align
);
670 if (offset
+ size
<= rs
->rs_end
) {
671 *cursor
= offset
+ size
;
674 rs
= AVL_NEXT(t
, rs
);
678 * If we know we've searched the whole map (*cursor == 0), give up.
679 * Otherwise, reset the cursor to the beginning and try again.
685 return (metaslab_block_picker(t
, cursor
, size
, align
));
687 #endif /* WITH_FF/DF/CF_BLOCK_ALLOCATOR */
689 #if defined(WITH_FF_BLOCK_ALLOCATOR)
691 * ==========================================================================
692 * The first-fit block allocator
693 * ==========================================================================
696 metaslab_ff_alloc(metaslab_t
*msp
, uint64_t size
)
699 * Find the largest power of 2 block size that evenly divides the
700 * requested size. This is used to try to allocate blocks with similar
701 * alignment from the same area of the metaslab (i.e. same cursor
702 * bucket) but it does not guarantee that other allocations sizes
703 * may exist in the same region.
705 uint64_t align
= size
& -size
;
706 uint64_t *cursor
= &msp
->ms_lbas
[highbit(align
) - 1];
707 avl_tree_t
*t
= &msp
->ms_tree
->rt_root
;
709 return (metaslab_block_picker(t
, cursor
, size
, align
));
714 metaslab_ff_fragmented(metaslab_t
*msp
)
719 static metaslab_ops_t metaslab_ff_ops
= {
721 metaslab_ff_fragmented
724 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_ff_ops
;
725 #endif /* WITH_FF_BLOCK_ALLOCATOR */
727 #if defined(WITH_DF_BLOCK_ALLOCATOR)
729 * ==========================================================================
730 * Dynamic block allocator -
731 * Uses the first fit allocation scheme until space get low and then
732 * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
733 * and metaslab_df_free_pct to determine when to switch the allocation scheme.
734 * ==========================================================================
737 metaslab_df_alloc(metaslab_t
*msp
, uint64_t size
)
740 * Find the largest power of 2 block size that evenly divides the
741 * requested size. This is used to try to allocate blocks with similar
742 * alignment from the same area of the metaslab (i.e. same cursor
743 * bucket) but it does not guarantee that other allocations sizes
744 * may exist in the same region.
746 uint64_t align
= size
& -size
;
747 uint64_t *cursor
= &msp
->ms_lbas
[highbit(align
) - 1];
748 range_tree_t
*rt
= msp
->ms_tree
;
749 avl_tree_t
*t
= &rt
->rt_root
;
750 uint64_t max_size
= metaslab_block_maxsize(msp
);
751 int free_pct
= range_tree_space(rt
) * 100 / msp
->ms_size
;
753 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
754 ASSERT3U(avl_numnodes(t
), ==, avl_numnodes(&msp
->ms_size_tree
));
760 * If we're running low on space switch to using the size
761 * sorted AVL tree (best-fit).
763 if (max_size
< metaslab_df_alloc_threshold
||
764 free_pct
< metaslab_df_free_pct
) {
765 t
= &msp
->ms_size_tree
;
769 return (metaslab_block_picker(t
, cursor
, size
, 1ULL));
773 metaslab_df_fragmented(metaslab_t
*msp
)
775 range_tree_t
*rt
= msp
->ms_tree
;
776 uint64_t max_size
= metaslab_block_maxsize(msp
);
777 int free_pct
= range_tree_space(rt
) * 100 / msp
->ms_size
;
779 if (max_size
>= metaslab_df_alloc_threshold
&&
780 free_pct
>= metaslab_df_free_pct
)
787 static metaslab_ops_t metaslab_df_ops
= {
789 metaslab_df_fragmented
792 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_df_ops
;
793 #endif /* WITH_DF_BLOCK_ALLOCATOR */
795 #if defined(WITH_CF_BLOCK_ALLOCATOR)
797 * ==========================================================================
798 * Cursor fit block allocator -
799 * Select the largest region in the metaslab, set the cursor to the beginning
800 * of the range and the cursor_end to the end of the range. As allocations
801 * are made advance the cursor. Continue allocating from the cursor until
802 * the range is exhausted and then find a new range.
803 * ==========================================================================
806 metaslab_cf_alloc(metaslab_t
*msp
, uint64_t size
)
808 range_tree_t
*rt
= msp
->ms_tree
;
809 avl_tree_t
*t
= &msp
->ms_size_tree
;
810 uint64_t *cursor
= &msp
->ms_lbas
[0];
811 uint64_t *cursor_end
= &msp
->ms_lbas
[1];
814 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
815 ASSERT3U(avl_numnodes(t
), ==, avl_numnodes(&rt
->rt_root
));
817 ASSERT3U(*cursor_end
, >=, *cursor
);
819 if ((*cursor
+ size
) > *cursor_end
) {
822 rs
= avl_last(&msp
->ms_size_tree
);
823 if (rs
== NULL
|| (rs
->rs_end
- rs
->rs_start
) < size
)
826 *cursor
= rs
->rs_start
;
827 *cursor_end
= rs
->rs_end
;
837 metaslab_cf_fragmented(metaslab_t
*msp
)
839 return (metaslab_block_maxsize(msp
) < metaslab_min_alloc_size
);
842 static metaslab_ops_t metaslab_cf_ops
= {
844 metaslab_cf_fragmented
847 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_cf_ops
;
848 #endif /* WITH_CF_BLOCK_ALLOCATOR */
850 #if defined(WITH_NDF_BLOCK_ALLOCATOR)
852 * ==========================================================================
853 * New dynamic fit allocator -
854 * Select a region that is large enough to allocate 2^metaslab_ndf_clump_shift
855 * contiguous blocks. If no region is found then just use the largest segment
857 * ==========================================================================
861 * Determines desired number of contiguous blocks (2^metaslab_ndf_clump_shift)
862 * to request from the allocator.
864 uint64_t metaslab_ndf_clump_shift
= 4;
867 metaslab_ndf_alloc(metaslab_t
*msp
, uint64_t size
)
869 avl_tree_t
*t
= &msp
->ms_tree
->rt_root
;
871 range_seg_t
*rs
, rsearch
;
872 uint64_t hbit
= highbit(size
);
873 uint64_t *cursor
= &msp
->ms_lbas
[hbit
- 1];
874 uint64_t max_size
= metaslab_block_maxsize(msp
);
876 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
877 ASSERT3U(avl_numnodes(t
), ==, avl_numnodes(&msp
->ms_size_tree
));
882 rsearch
.rs_start
= *cursor
;
883 rsearch
.rs_end
= *cursor
+ size
;
885 rs
= avl_find(t
, &rsearch
, &where
);
886 if (rs
== NULL
|| (rs
->rs_end
- rs
->rs_start
) < size
) {
887 t
= &msp
->ms_size_tree
;
889 rsearch
.rs_start
= 0;
890 rsearch
.rs_end
= MIN(max_size
,
891 1ULL << (hbit
+ metaslab_ndf_clump_shift
));
892 rs
= avl_find(t
, &rsearch
, &where
);
894 rs
= avl_nearest(t
, where
, AVL_AFTER
);
898 if ((rs
->rs_end
- rs
->rs_start
) >= size
) {
899 *cursor
= rs
->rs_start
+ size
;
900 return (rs
->rs_start
);
906 metaslab_ndf_fragmented(metaslab_t
*msp
)
908 return (metaslab_block_maxsize(msp
) <=
909 (metaslab_min_alloc_size
<< metaslab_ndf_clump_shift
));
912 static metaslab_ops_t metaslab_ndf_ops
= {
914 metaslab_ndf_fragmented
917 metaslab_ops_t
*zfs_metaslab_ops
= &metaslab_ndf_ops
;
918 #endif /* WITH_NDF_BLOCK_ALLOCATOR */
922 * ==========================================================================
924 * ==========================================================================
928 * Wait for any in-progress metaslab loads to complete.
931 metaslab_load_wait(metaslab_t
*msp
)
933 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
935 while (msp
->ms_loading
) {
936 ASSERT(!msp
->ms_loaded
);
937 cv_wait(&msp
->ms_load_cv
, &msp
->ms_lock
);
942 metaslab_load(metaslab_t
*msp
)
947 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
948 ASSERT(!msp
->ms_loaded
);
949 ASSERT(!msp
->ms_loading
);
951 msp
->ms_loading
= B_TRUE
;
954 * If the space map has not been allocated yet, then treat
955 * all the space in the metaslab as free and add it to the
958 if (msp
->ms_sm
!= NULL
)
959 error
= space_map_load(msp
->ms_sm
, msp
->ms_tree
, SM_FREE
);
961 range_tree_add(msp
->ms_tree
, msp
->ms_start
, msp
->ms_size
);
963 msp
->ms_loaded
= (error
== 0);
964 msp
->ms_loading
= B_FALSE
;
966 if (msp
->ms_loaded
) {
967 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
968 range_tree_walk(msp
->ms_defertree
[t
],
969 range_tree_remove
, msp
->ms_tree
);
972 cv_broadcast(&msp
->ms_load_cv
);
977 metaslab_unload(metaslab_t
*msp
)
979 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
980 range_tree_vacate(msp
->ms_tree
, NULL
, NULL
);
981 msp
->ms_loaded
= B_FALSE
;
982 msp
->ms_weight
&= ~METASLAB_ACTIVE_MASK
;
986 metaslab_init(metaslab_group_t
*mg
, uint64_t id
, uint64_t object
, uint64_t txg
)
988 vdev_t
*vd
= mg
->mg_vd
;
989 objset_t
*mos
= vd
->vdev_spa
->spa_meta_objset
;
992 msp
= kmem_zalloc(sizeof (metaslab_t
), KM_PUSHPAGE
);
993 mutex_init(&msp
->ms_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
994 cv_init(&msp
->ms_load_cv
, NULL
, CV_DEFAULT
, NULL
);
996 msp
->ms_start
= id
<< vd
->vdev_ms_shift
;
997 msp
->ms_size
= 1ULL << vd
->vdev_ms_shift
;
1000 * We only open space map objects that already exist. All others
1001 * will be opened when we finally allocate an object for it.
1004 VERIFY0(space_map_open(&msp
->ms_sm
, mos
, object
, msp
->ms_start
,
1005 msp
->ms_size
, vd
->vdev_ashift
, &msp
->ms_lock
));
1006 ASSERT(msp
->ms_sm
!= NULL
);
1010 * We create the main range tree here, but we don't create the
1011 * alloctree and freetree until metaslab_sync_done(). This serves
1012 * two purposes: it allows metaslab_sync_done() to detect the
1013 * addition of new space; and for debugging, it ensures that we'd
1014 * data fault on any attempt to use this metaslab before it's ready.
1016 msp
->ms_tree
= range_tree_create(&metaslab_rt_ops
, msp
, &msp
->ms_lock
);
1017 metaslab_group_add(mg
, msp
);
1019 msp
->ms_ops
= mg
->mg_class
->mc_ops
;
1022 * If we're opening an existing pool (txg == 0) or creating
1023 * a new one (txg == TXG_INITIAL), all space is available now.
1024 * If we're adding space to an existing pool, the new space
1025 * does not become available until after this txg has synced.
1027 if (txg
<= TXG_INITIAL
)
1028 metaslab_sync_done(msp
, 0);
1031 * If metaslab_debug_load is set and we're initializing a metaslab
1032 * that has an allocated space_map object then load the its space
1033 * map so that can verify frees.
1035 if (metaslab_debug_load
&& msp
->ms_sm
!= NULL
) {
1036 mutex_enter(&msp
->ms_lock
);
1037 VERIFY0(metaslab_load(msp
));
1038 mutex_exit(&msp
->ms_lock
);
1042 vdev_dirty(vd
, 0, NULL
, txg
);
1043 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
1050 metaslab_fini(metaslab_t
*msp
)
1054 metaslab_group_t
*mg
= msp
->ms_group
;
1056 metaslab_group_remove(mg
, msp
);
1058 mutex_enter(&msp
->ms_lock
);
1060 VERIFY(msp
->ms_group
== NULL
);
1061 vdev_space_update(mg
->mg_vd
, -space_map_allocated(msp
->ms_sm
),
1063 space_map_close(msp
->ms_sm
);
1065 metaslab_unload(msp
);
1066 range_tree_destroy(msp
->ms_tree
);
1068 for (t
= 0; t
< TXG_SIZE
; t
++) {
1069 range_tree_destroy(msp
->ms_alloctree
[t
]);
1070 range_tree_destroy(msp
->ms_freetree
[t
]);
1073 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1074 range_tree_destroy(msp
->ms_defertree
[t
]);
1077 ASSERT0(msp
->ms_deferspace
);
1079 mutex_exit(&msp
->ms_lock
);
1080 cv_destroy(&msp
->ms_load_cv
);
1081 mutex_destroy(&msp
->ms_lock
);
1083 kmem_free(msp
, sizeof (metaslab_t
));
1087 * Apply a weighting factor based on the histogram information for this
1088 * metaslab. The current weighting factor is somewhat arbitrary and requires
1089 * additional investigation. The implementation provides a measure of
1090 * "weighted" free space and gives a higher weighting for larger contiguous
1091 * regions. The weighting factor is determined by counting the number of
1092 * sm_shift sectors that exist in each region represented by the histogram.
1093 * That value is then multiplied by the power of 2 exponent and the sm_shift
1096 * For example, assume the 2^21 histogram bucket has 4 2MB regions and the
1097 * metaslab has an sm_shift value of 9 (512B):
1099 * 1) calculate the number of sm_shift sectors in the region:
1100 * 2^21 / 2^9 = 2^12 = 4096 * 4 (number of regions) = 16384
1101 * 2) multiply by the power of 2 exponent and the sm_shift value:
1102 * 16384 * 21 * 9 = 3096576
1103 * This value will be added to the weighting of the metaslab.
1106 metaslab_weight_factor(metaslab_t
*msp
)
1108 uint64_t factor
= 0;
1113 * A null space map means that the entire metaslab is free,
1114 * calculate a weight factor that spans the entire size of the
1117 if (msp
->ms_sm
== NULL
) {
1118 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
1120 i
= highbit(msp
->ms_size
) - 1;
1121 sectors
= msp
->ms_size
>> vd
->vdev_ashift
;
1122 return (sectors
* i
* vd
->vdev_ashift
);
1125 if (msp
->ms_sm
->sm_dbuf
->db_size
!= sizeof (space_map_phys_t
))
1128 for (i
= 0; i
< SPACE_MAP_HISTOGRAM_SIZE(msp
->ms_sm
); i
++) {
1129 if (msp
->ms_sm
->sm_phys
->smp_histogram
[i
] == 0)
1133 * Determine the number of sm_shift sectors in the region
1134 * indicated by the histogram. For example, given an
1135 * sm_shift value of 9 (512 bytes) and i = 4 then we know
1136 * that we're looking at an 8K region in the histogram
1137 * (i.e. 9 + 4 = 13, 2^13 = 8192). To figure out the
1138 * number of sm_shift sectors (512 bytes in this example),
1139 * we would take 8192 / 512 = 16. Since the histogram
1140 * is offset by sm_shift we can simply use the value of
1141 * of i to calculate this (i.e. 2^i = 16 where i = 4).
1143 sectors
= msp
->ms_sm
->sm_phys
->smp_histogram
[i
] << i
;
1144 factor
+= (i
+ msp
->ms_sm
->sm_shift
) * sectors
;
1146 return (factor
* msp
->ms_sm
->sm_shift
);
1150 metaslab_weight(metaslab_t
*msp
)
1152 metaslab_group_t
*mg
= msp
->ms_group
;
1153 vdev_t
*vd
= mg
->mg_vd
;
1154 uint64_t weight
, space
;
1156 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1159 * This vdev is in the process of being removed so there is nothing
1160 * for us to do here.
1162 if (vd
->vdev_removing
) {
1163 ASSERT0(space_map_allocated(msp
->ms_sm
));
1164 ASSERT0(vd
->vdev_ms_shift
);
1169 * The baseline weight is the metaslab's free space.
1171 space
= msp
->ms_size
- space_map_allocated(msp
->ms_sm
);
1175 * Modern disks have uniform bit density and constant angular velocity.
1176 * Therefore, the outer recording zones are faster (higher bandwidth)
1177 * than the inner zones by the ratio of outer to inner track diameter,
1178 * which is typically around 2:1. We account for this by assigning
1179 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
1180 * In effect, this means that we'll select the metaslab with the most
1181 * free bandwidth rather than simply the one with the most free space.
1183 weight
= 2 * weight
- (msp
->ms_id
* weight
) / vd
->vdev_ms_count
;
1184 ASSERT(weight
>= space
&& weight
<= 2 * space
);
1186 msp
->ms_factor
= metaslab_weight_factor(msp
);
1187 if (metaslab_weight_factor_enable
)
1188 weight
+= msp
->ms_factor
;
1190 if (msp
->ms_loaded
&& !msp
->ms_ops
->msop_fragmented(msp
)) {
1192 * If this metaslab is one we're actively using, adjust its
1193 * weight to make it preferable to any inactive metaslab so
1194 * we'll polish it off.
1196 weight
|= (msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
1203 metaslab_activate(metaslab_t
*msp
, uint64_t activation_weight
)
1205 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1207 if ((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0) {
1208 metaslab_load_wait(msp
);
1209 if (!msp
->ms_loaded
) {
1210 int error
= metaslab_load(msp
);
1212 metaslab_group_sort(msp
->ms_group
, msp
, 0);
1217 metaslab_group_sort(msp
->ms_group
, msp
,
1218 msp
->ms_weight
| activation_weight
);
1220 ASSERT(msp
->ms_loaded
);
1221 ASSERT(msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
1227 metaslab_passivate(metaslab_t
*msp
, uint64_t size
)
1230 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
1231 * this metaslab again. In that case, it had better be empty,
1232 * or we would be leaving space on the table.
1234 ASSERT(size
>= SPA_MINBLOCKSIZE
|| range_tree_space(msp
->ms_tree
) == 0);
1235 metaslab_group_sort(msp
->ms_group
, msp
, MIN(msp
->ms_weight
, size
));
1236 ASSERT((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0);
1240 metaslab_preload(void *arg
)
1242 metaslab_t
*msp
= arg
;
1243 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
1245 mutex_enter(&msp
->ms_lock
);
1246 metaslab_load_wait(msp
);
1247 if (!msp
->ms_loaded
)
1248 (void) metaslab_load(msp
);
1251 * Set the ms_access_txg value so that we don't unload it right away.
1253 msp
->ms_access_txg
= spa_syncing_txg(spa
) + metaslab_unload_delay
+ 1;
1254 mutex_exit(&msp
->ms_lock
);
1258 metaslab_group_preload(metaslab_group_t
*mg
)
1260 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
1262 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
1265 if (spa_shutting_down(spa
) || !metaslab_preload_enabled
) {
1266 taskq_wait(mg
->mg_taskq
);
1269 mutex_enter(&mg
->mg_lock
);
1272 * Prefetch the next potential metaslabs
1274 for (msp
= avl_first(t
); msp
!= NULL
; msp
= AVL_NEXT(t
, msp
)) {
1276 /* If we have reached our preload limit then we're done */
1277 if (++m
> metaslab_preload_limit
)
1280 VERIFY(taskq_dispatch(mg
->mg_taskq
, metaslab_preload
,
1281 msp
, TQ_PUSHPAGE
) != 0);
1283 mutex_exit(&mg
->mg_lock
);
1287 * Determine if the space map's on-disk footprint is past our tolerance
1288 * for inefficiency. We would like to use the following criteria to make
1291 * 1. The size of the space map object should not dramatically increase as a
1292 * result of writing out the free space range tree.
1294 * 2. The minimal on-disk space map representation is zfs_condense_pct/100
1295 * times the size than the free space range tree representation
1296 * (i.e. zfs_condense_pct = 110 and in-core = 1MB, minimal = 1.1.MB).
1298 * Checking the first condition is tricky since we don't want to walk
1299 * the entire AVL tree calculating the estimated on-disk size. Instead we
1300 * use the size-ordered range tree in the metaslab and calculate the
1301 * size required to write out the largest segment in our free tree. If the
1302 * size required to represent that segment on disk is larger than the space
1303 * map object then we avoid condensing this map.
1305 * To determine the second criterion we use a best-case estimate and assume
1306 * each segment can be represented on-disk as a single 64-bit entry. We refer
1307 * to this best-case estimate as the space map's minimal form.
1310 metaslab_should_condense(metaslab_t
*msp
)
1312 space_map_t
*sm
= msp
->ms_sm
;
1314 uint64_t size
, entries
, segsz
;
1316 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1317 ASSERT(msp
->ms_loaded
);
1320 * Use the ms_size_tree range tree, which is ordered by size, to
1321 * obtain the largest segment in the free tree. If the tree is empty
1322 * then we should condense the map.
1324 rs
= avl_last(&msp
->ms_size_tree
);
1329 * Calculate the number of 64-bit entries this segment would
1330 * require when written to disk. If this single segment would be
1331 * larger on-disk than the entire current on-disk structure, then
1332 * clearly condensing will increase the on-disk structure size.
1334 size
= (rs
->rs_end
- rs
->rs_start
) >> sm
->sm_shift
;
1335 entries
= size
/ (MIN(size
, SM_RUN_MAX
));
1336 segsz
= entries
* sizeof (uint64_t);
1338 return (segsz
<= space_map_length(msp
->ms_sm
) &&
1339 space_map_length(msp
->ms_sm
) >= (zfs_condense_pct
*
1340 sizeof (uint64_t) * avl_numnodes(&msp
->ms_tree
->rt_root
)) / 100);
1344 * Condense the on-disk space map representation to its minimized form.
1345 * The minimized form consists of a small number of allocations followed by
1346 * the entries of the free range tree.
1349 metaslab_condense(metaslab_t
*msp
, uint64_t txg
, dmu_tx_t
*tx
)
1351 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
1352 range_tree_t
*freetree
= msp
->ms_freetree
[txg
& TXG_MASK
];
1353 range_tree_t
*condense_tree
;
1354 space_map_t
*sm
= msp
->ms_sm
;
1357 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1358 ASSERT3U(spa_sync_pass(spa
), ==, 1);
1359 ASSERT(msp
->ms_loaded
);
1361 spa_dbgmsg(spa
, "condensing: txg %llu, msp[%llu] %p, "
1362 "smp size %llu, segments %lu", txg
, msp
->ms_id
, msp
,
1363 space_map_length(msp
->ms_sm
), avl_numnodes(&msp
->ms_tree
->rt_root
));
1366 * Create an range tree that is 100% allocated. We remove segments
1367 * that have been freed in this txg, any deferred frees that exist,
1368 * and any allocation in the future. Removing segments should be
1369 * a relatively inexpensive operation since we expect these trees to
1370 * have a small number of nodes.
1372 condense_tree
= range_tree_create(NULL
, NULL
, &msp
->ms_lock
);
1373 range_tree_add(condense_tree
, msp
->ms_start
, msp
->ms_size
);
1376 * Remove what's been freed in this txg from the condense_tree.
1377 * Since we're in sync_pass 1, we know that all the frees from
1378 * this txg are in the freetree.
1380 range_tree_walk(freetree
, range_tree_remove
, condense_tree
);
1382 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1383 range_tree_walk(msp
->ms_defertree
[t
],
1384 range_tree_remove
, condense_tree
);
1387 for (t
= 1; t
< TXG_CONCURRENT_STATES
; t
++) {
1388 range_tree_walk(msp
->ms_alloctree
[(txg
+ t
) & TXG_MASK
],
1389 range_tree_remove
, condense_tree
);
1393 * We're about to drop the metaslab's lock thus allowing
1394 * other consumers to change it's content. Set the
1395 * metaslab's ms_condensing flag to ensure that
1396 * allocations on this metaslab do not occur while we're
1397 * in the middle of committing it to disk. This is only critical
1398 * for the ms_tree as all other range trees use per txg
1399 * views of their content.
1401 msp
->ms_condensing
= B_TRUE
;
1403 mutex_exit(&msp
->ms_lock
);
1404 space_map_truncate(sm
, tx
);
1405 mutex_enter(&msp
->ms_lock
);
1408 * While we would ideally like to create a space_map representation
1409 * that consists only of allocation records, doing so can be
1410 * prohibitively expensive because the in-core free tree can be
1411 * large, and therefore computationally expensive to subtract
1412 * from the condense_tree. Instead we sync out two trees, a cheap
1413 * allocation only tree followed by the in-core free tree. While not
1414 * optimal, this is typically close to optimal, and much cheaper to
1417 space_map_write(sm
, condense_tree
, SM_ALLOC
, tx
);
1418 range_tree_vacate(condense_tree
, NULL
, NULL
);
1419 range_tree_destroy(condense_tree
);
1421 space_map_write(sm
, msp
->ms_tree
, SM_FREE
, tx
);
1422 msp
->ms_condensing
= B_FALSE
;
1426 * Write a metaslab to disk in the context of the specified transaction group.
1429 metaslab_sync(metaslab_t
*msp
, uint64_t txg
)
1431 metaslab_group_t
*mg
= msp
->ms_group
;
1432 vdev_t
*vd
= mg
->mg_vd
;
1433 spa_t
*spa
= vd
->vdev_spa
;
1434 objset_t
*mos
= spa_meta_objset(spa
);
1435 range_tree_t
*alloctree
= msp
->ms_alloctree
[txg
& TXG_MASK
];
1436 range_tree_t
**freetree
= &msp
->ms_freetree
[txg
& TXG_MASK
];
1437 range_tree_t
**freed_tree
=
1438 &msp
->ms_freetree
[TXG_CLEAN(txg
) & TXG_MASK
];
1440 uint64_t object
= space_map_object(msp
->ms_sm
);
1442 ASSERT(!vd
->vdev_ishole
);
1445 * This metaslab has just been added so there's no work to do now.
1447 if (*freetree
== NULL
) {
1448 ASSERT3P(alloctree
, ==, NULL
);
1452 ASSERT3P(alloctree
, !=, NULL
);
1453 ASSERT3P(*freetree
, !=, NULL
);
1454 ASSERT3P(*freed_tree
, !=, NULL
);
1456 if (range_tree_space(alloctree
) == 0 &&
1457 range_tree_space(*freetree
) == 0)
1461 * The only state that can actually be changing concurrently with
1462 * metaslab_sync() is the metaslab's ms_tree. No other thread can
1463 * be modifying this txg's alloctree, freetree, freed_tree, or
1464 * space_map_phys_t. Therefore, we only hold ms_lock to satify
1465 * space_map ASSERTs. We drop it whenever we call into the DMU,
1466 * because the DMU can call down to us (e.g. via zio_free()) at
1470 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
1472 if (msp
->ms_sm
== NULL
) {
1473 uint64_t new_object
;
1475 new_object
= space_map_alloc(mos
, tx
);
1476 VERIFY3U(new_object
, !=, 0);
1478 VERIFY0(space_map_open(&msp
->ms_sm
, mos
, new_object
,
1479 msp
->ms_start
, msp
->ms_size
, vd
->vdev_ashift
,
1481 ASSERT(msp
->ms_sm
!= NULL
);
1484 mutex_enter(&msp
->ms_lock
);
1486 if (msp
->ms_loaded
&& spa_sync_pass(spa
) == 1 &&
1487 metaslab_should_condense(msp
)) {
1488 metaslab_condense(msp
, txg
, tx
);
1490 space_map_write(msp
->ms_sm
, alloctree
, SM_ALLOC
, tx
);
1491 space_map_write(msp
->ms_sm
, *freetree
, SM_FREE
, tx
);
1494 range_tree_vacate(alloctree
, NULL
, NULL
);
1496 if (msp
->ms_loaded
) {
1498 * When the space map is loaded, we have an accruate
1499 * histogram in the range tree. This gives us an opportunity
1500 * to bring the space map's histogram up-to-date so we clear
1501 * it first before updating it.
1503 space_map_histogram_clear(msp
->ms_sm
);
1504 space_map_histogram_add(msp
->ms_sm
, msp
->ms_tree
, tx
);
1507 * Since the space map is not loaded we simply update the
1508 * exisiting histogram with what was freed in this txg. This
1509 * means that the on-disk histogram may not have an accurate
1510 * view of the free space but it's close enough to allow
1511 * us to make allocation decisions.
1513 space_map_histogram_add(msp
->ms_sm
, *freetree
, tx
);
1517 * For sync pass 1, we avoid traversing this txg's free range tree
1518 * and instead will just swap the pointers for freetree and
1519 * freed_tree. We can safely do this since the freed_tree is
1520 * guaranteed to be empty on the initial pass.
1522 if (spa_sync_pass(spa
) == 1) {
1523 range_tree_swap(freetree
, freed_tree
);
1525 range_tree_vacate(*freetree
, range_tree_add
, *freed_tree
);
1528 ASSERT0(range_tree_space(msp
->ms_alloctree
[txg
& TXG_MASK
]));
1529 ASSERT0(range_tree_space(msp
->ms_freetree
[txg
& TXG_MASK
]));
1531 mutex_exit(&msp
->ms_lock
);
1533 if (object
!= space_map_object(msp
->ms_sm
)) {
1534 object
= space_map_object(msp
->ms_sm
);
1535 dmu_write(mos
, vd
->vdev_ms_array
, sizeof (uint64_t) *
1536 msp
->ms_id
, sizeof (uint64_t), &object
, tx
);
1542 * Called after a transaction group has completely synced to mark
1543 * all of the metaslab's free space as usable.
1546 metaslab_sync_done(metaslab_t
*msp
, uint64_t txg
)
1548 metaslab_group_t
*mg
= msp
->ms_group
;
1549 vdev_t
*vd
= mg
->mg_vd
;
1550 range_tree_t
**freed_tree
;
1551 range_tree_t
**defer_tree
;
1552 int64_t alloc_delta
, defer_delta
;
1555 ASSERT(!vd
->vdev_ishole
);
1557 mutex_enter(&msp
->ms_lock
);
1560 * If this metaslab is just becoming available, initialize its
1561 * alloctrees, freetrees, and defertree and add its capacity to
1564 if (msp
->ms_freetree
[TXG_CLEAN(txg
) & TXG_MASK
] == NULL
) {
1565 for (t
= 0; t
< TXG_SIZE
; t
++) {
1566 ASSERT(msp
->ms_alloctree
[t
] == NULL
);
1567 ASSERT(msp
->ms_freetree
[t
] == NULL
);
1569 msp
->ms_alloctree
[t
] = range_tree_create(NULL
, msp
,
1571 msp
->ms_freetree
[t
] = range_tree_create(NULL
, msp
,
1575 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1576 ASSERT(msp
->ms_defertree
[t
] == NULL
);
1578 msp
->ms_defertree
[t
] = range_tree_create(NULL
, msp
,
1582 vdev_space_update(vd
, 0, 0, msp
->ms_size
);
1585 freed_tree
= &msp
->ms_freetree
[TXG_CLEAN(txg
) & TXG_MASK
];
1586 defer_tree
= &msp
->ms_defertree
[txg
% TXG_DEFER_SIZE
];
1588 alloc_delta
= space_map_alloc_delta(msp
->ms_sm
);
1589 defer_delta
= range_tree_space(*freed_tree
) -
1590 range_tree_space(*defer_tree
);
1592 vdev_space_update(vd
, alloc_delta
+ defer_delta
, defer_delta
, 0);
1594 ASSERT0(range_tree_space(msp
->ms_alloctree
[txg
& TXG_MASK
]));
1595 ASSERT0(range_tree_space(msp
->ms_freetree
[txg
& TXG_MASK
]));
1598 * If there's a metaslab_load() in progress, wait for it to complete
1599 * so that we have a consistent view of the in-core space map.
1601 metaslab_load_wait(msp
);
1604 * Move the frees from the defer_tree back to the free
1605 * range tree (if it's loaded). Swap the freed_tree and the
1606 * defer_tree -- this is safe to do because we've just emptied out
1609 range_tree_vacate(*defer_tree
,
1610 msp
->ms_loaded
? range_tree_add
: NULL
, msp
->ms_tree
);
1611 range_tree_swap(freed_tree
, defer_tree
);
1613 space_map_update(msp
->ms_sm
);
1615 msp
->ms_deferspace
+= defer_delta
;
1616 ASSERT3S(msp
->ms_deferspace
, >=, 0);
1617 ASSERT3S(msp
->ms_deferspace
, <=, msp
->ms_size
);
1618 if (msp
->ms_deferspace
!= 0) {
1620 * Keep syncing this metaslab until all deferred frees
1621 * are back in circulation.
1623 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
+ 1);
1626 if (msp
->ms_loaded
&& msp
->ms_access_txg
< txg
) {
1627 for (t
= 1; t
< TXG_CONCURRENT_STATES
; t
++) {
1628 VERIFY0(range_tree_space(
1629 msp
->ms_alloctree
[(txg
+ t
) & TXG_MASK
]));
1632 if (!metaslab_debug_unload
)
1633 metaslab_unload(msp
);
1636 metaslab_group_sort(mg
, msp
, metaslab_weight(msp
));
1637 mutex_exit(&msp
->ms_lock
);
1642 metaslab_sync_reassess(metaslab_group_t
*mg
)
1644 int64_t failures
= mg
->mg_alloc_failures
;
1646 metaslab_group_alloc_update(mg
);
1647 atomic_add_64(&mg
->mg_alloc_failures
, -failures
);
1650 * Preload the next potential metaslabs
1652 metaslab_group_preload(mg
);
1656 metaslab_distance(metaslab_t
*msp
, dva_t
*dva
)
1658 uint64_t ms_shift
= msp
->ms_group
->mg_vd
->vdev_ms_shift
;
1659 uint64_t offset
= DVA_GET_OFFSET(dva
) >> ms_shift
;
1660 uint64_t start
= msp
->ms_id
;
1662 if (msp
->ms_group
->mg_vd
->vdev_id
!= DVA_GET_VDEV(dva
))
1663 return (1ULL << 63);
1666 return ((start
- offset
) << ms_shift
);
1668 return ((offset
- start
) << ms_shift
);
1673 metaslab_group_alloc(metaslab_group_t
*mg
, uint64_t psize
, uint64_t asize
,
1674 uint64_t txg
, uint64_t min_distance
, dva_t
*dva
, int d
, int flags
)
1676 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
1677 metaslab_t
*msp
= NULL
;
1678 uint64_t offset
= -1ULL;
1679 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
1680 uint64_t activation_weight
;
1681 uint64_t target_distance
;
1684 activation_weight
= METASLAB_WEIGHT_PRIMARY
;
1685 for (i
= 0; i
< d
; i
++) {
1686 if (DVA_GET_VDEV(&dva
[i
]) == mg
->mg_vd
->vdev_id
) {
1687 activation_weight
= METASLAB_WEIGHT_SECONDARY
;
1693 boolean_t was_active
;
1695 mutex_enter(&mg
->mg_lock
);
1696 for (msp
= avl_first(t
); msp
; msp
= AVL_NEXT(t
, msp
)) {
1697 if (msp
->ms_weight
< asize
) {
1698 spa_dbgmsg(spa
, "%s: failed to meet weight "
1699 "requirement: vdev %llu, txg %llu, mg %p, "
1700 "msp %p, psize %llu, asize %llu, "
1701 "failures %llu, weight %llu",
1702 spa_name(spa
), mg
->mg_vd
->vdev_id
, txg
,
1703 mg
, msp
, psize
, asize
,
1704 mg
->mg_alloc_failures
, msp
->ms_weight
);
1705 mutex_exit(&mg
->mg_lock
);
1710 * If the selected metaslab is condensing, skip it.
1712 if (msp
->ms_condensing
)
1715 was_active
= msp
->ms_weight
& METASLAB_ACTIVE_MASK
;
1716 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
)
1719 target_distance
= min_distance
+
1720 (space_map_allocated(msp
->ms_sm
) != 0 ? 0 :
1723 for (i
= 0; i
< d
; i
++)
1724 if (metaslab_distance(msp
, &dva
[i
]) <
1730 mutex_exit(&mg
->mg_lock
);
1734 mutex_enter(&msp
->ms_lock
);
1737 * If we've already reached the allowable number of failed
1738 * allocation attempts on this metaslab group then we
1739 * consider skipping it. We skip it only if we're allowed
1740 * to "fast" gang, the physical size is larger than
1741 * a gang block, and we're attempting to allocate from
1742 * the primary metaslab.
1744 if (mg
->mg_alloc_failures
> zfs_mg_alloc_failures
&&
1745 CAN_FASTGANG(flags
) && psize
> SPA_GANGBLOCKSIZE
&&
1746 activation_weight
== METASLAB_WEIGHT_PRIMARY
) {
1747 spa_dbgmsg(spa
, "%s: skipping metaslab group: "
1748 "vdev %llu, txg %llu, mg %p, msp[%llu] %p, "
1749 "psize %llu, asize %llu, failures %llu",
1750 spa_name(spa
), mg
->mg_vd
->vdev_id
, txg
, mg
,
1751 msp
->ms_id
, msp
, psize
, asize
,
1752 mg
->mg_alloc_failures
);
1753 mutex_exit(&msp
->ms_lock
);
1758 * Ensure that the metaslab we have selected is still
1759 * capable of handling our request. It's possible that
1760 * another thread may have changed the weight while we
1761 * were blocked on the metaslab lock.
1763 if (msp
->ms_weight
< asize
|| (was_active
&&
1764 !(msp
->ms_weight
& METASLAB_ACTIVE_MASK
) &&
1765 activation_weight
== METASLAB_WEIGHT_PRIMARY
)) {
1766 mutex_exit(&msp
->ms_lock
);
1770 if ((msp
->ms_weight
& METASLAB_WEIGHT_SECONDARY
) &&
1771 activation_weight
== METASLAB_WEIGHT_PRIMARY
) {
1772 metaslab_passivate(msp
,
1773 msp
->ms_weight
& ~METASLAB_ACTIVE_MASK
);
1774 mutex_exit(&msp
->ms_lock
);
1778 if (metaslab_activate(msp
, activation_weight
) != 0) {
1779 mutex_exit(&msp
->ms_lock
);
1784 * If this metaslab is currently condensing then pick again as
1785 * we can't manipulate this metaslab until it's committed
1788 if (msp
->ms_condensing
) {
1789 mutex_exit(&msp
->ms_lock
);
1793 if ((offset
= metaslab_block_alloc(msp
, asize
)) != -1ULL)
1796 atomic_inc_64(&mg
->mg_alloc_failures
);
1798 metaslab_passivate(msp
, metaslab_block_maxsize(msp
));
1799 mutex_exit(&msp
->ms_lock
);
1802 if (range_tree_space(msp
->ms_alloctree
[txg
& TXG_MASK
]) == 0)
1803 vdev_dirty(mg
->mg_vd
, VDD_METASLAB
, msp
, txg
);
1805 range_tree_add(msp
->ms_alloctree
[txg
& TXG_MASK
], offset
, asize
);
1806 msp
->ms_access_txg
= txg
+ metaslab_unload_delay
;
1808 mutex_exit(&msp
->ms_lock
);
1814 * Allocate a block for the specified i/o.
1817 metaslab_alloc_dva(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
,
1818 dva_t
*dva
, int d
, dva_t
*hintdva
, uint64_t txg
, int flags
)
1820 metaslab_group_t
*mg
, *fast_mg
, *rotor
;
1824 int zio_lock
= B_FALSE
;
1825 boolean_t allocatable
;
1826 uint64_t offset
= -1ULL;
1830 ASSERT(!DVA_IS_VALID(&dva
[d
]));
1833 * For testing, make some blocks above a certain size be gang blocks.
1835 if (psize
>= metaslab_gang_bang
&& (ddi_get_lbolt() & 3) == 0)
1836 return (SET_ERROR(ENOSPC
));
1838 if (flags
& METASLAB_FASTWRITE
)
1839 mutex_enter(&mc
->mc_fastwrite_lock
);
1842 * Start at the rotor and loop through all mgs until we find something.
1843 * Note that there's no locking on mc_rotor or mc_aliquot because
1844 * nothing actually breaks if we miss a few updates -- we just won't
1845 * allocate quite as evenly. It all balances out over time.
1847 * If we are doing ditto or log blocks, try to spread them across
1848 * consecutive vdevs. If we're forced to reuse a vdev before we've
1849 * allocated all of our ditto blocks, then try and spread them out on
1850 * that vdev as much as possible. If it turns out to not be possible,
1851 * gradually lower our standards until anything becomes acceptable.
1852 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
1853 * gives us hope of containing our fault domains to something we're
1854 * able to reason about. Otherwise, any two top-level vdev failures
1855 * will guarantee the loss of data. With consecutive allocation,
1856 * only two adjacent top-level vdev failures will result in data loss.
1858 * If we are doing gang blocks (hintdva is non-NULL), try to keep
1859 * ourselves on the same vdev as our gang block header. That
1860 * way, we can hope for locality in vdev_cache, plus it makes our
1861 * fault domains something tractable.
1864 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&hintdva
[d
]));
1867 * It's possible the vdev we're using as the hint no
1868 * longer exists (i.e. removed). Consult the rotor when
1874 if (flags
& METASLAB_HINTBP_AVOID
&&
1875 mg
->mg_next
!= NULL
)
1880 } else if (d
!= 0) {
1881 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
- 1]));
1882 mg
= vd
->vdev_mg
->mg_next
;
1883 } else if (flags
& METASLAB_FASTWRITE
) {
1884 mg
= fast_mg
= mc
->mc_rotor
;
1887 if (fast_mg
->mg_vd
->vdev_pending_fastwrite
<
1888 mg
->mg_vd
->vdev_pending_fastwrite
)
1890 } while ((fast_mg
= fast_mg
->mg_next
) != mc
->mc_rotor
);
1897 * If the hint put us into the wrong metaslab class, or into a
1898 * metaslab group that has been passivated, just follow the rotor.
1900 if (mg
->mg_class
!= mc
|| mg
->mg_activation_count
<= 0)
1907 ASSERT(mg
->mg_activation_count
== 1);
1912 * Don't allocate from faulted devices.
1915 spa_config_enter(spa
, SCL_ZIO
, FTAG
, RW_READER
);
1916 allocatable
= vdev_allocatable(vd
);
1917 spa_config_exit(spa
, SCL_ZIO
, FTAG
);
1919 allocatable
= vdev_allocatable(vd
);
1923 * Determine if the selected metaslab group is eligible
1924 * for allocations. If we're ganging or have requested
1925 * an allocation for the smallest gang block size
1926 * then we don't want to avoid allocating to the this
1927 * metaslab group. If we're in this condition we should
1928 * try to allocate from any device possible so that we
1929 * don't inadvertently return ENOSPC and suspend the pool
1930 * even though space is still available.
1932 if (allocatable
&& CAN_FASTGANG(flags
) &&
1933 psize
> SPA_GANGBLOCKSIZE
)
1934 allocatable
= metaslab_group_allocatable(mg
);
1940 * Avoid writing single-copy data to a failing vdev
1941 * unless the user instructs us that it is okay.
1943 if ((vd
->vdev_stat
.vs_write_errors
> 0 ||
1944 vd
->vdev_state
< VDEV_STATE_HEALTHY
) &&
1945 d
== 0 && dshift
== 3 &&
1946 !(zfs_write_to_degraded
&& vd
->vdev_state
==
1947 VDEV_STATE_DEGRADED
)) {
1952 ASSERT(mg
->mg_class
== mc
);
1954 distance
= vd
->vdev_asize
>> dshift
;
1955 if (distance
<= (1ULL << vd
->vdev_ms_shift
))
1960 asize
= vdev_psize_to_asize(vd
, psize
);
1961 ASSERT(P2PHASE(asize
, 1ULL << vd
->vdev_ashift
) == 0);
1963 offset
= metaslab_group_alloc(mg
, psize
, asize
, txg
, distance
,
1965 if (offset
!= -1ULL) {
1967 * If we've just selected this metaslab group,
1968 * figure out whether the corresponding vdev is
1969 * over- or under-used relative to the pool,
1970 * and set an allocation bias to even it out.
1972 if (mc
->mc_aliquot
== 0) {
1973 vdev_stat_t
*vs
= &vd
->vdev_stat
;
1976 vu
= (vs
->vs_alloc
* 100) / (vs
->vs_space
+ 1);
1977 cu
= (mc
->mc_alloc
* 100) / (mc
->mc_space
+ 1);
1980 * Calculate how much more or less we should
1981 * try to allocate from this device during
1982 * this iteration around the rotor.
1983 * For example, if a device is 80% full
1984 * and the pool is 20% full then we should
1985 * reduce allocations by 60% on this device.
1987 * mg_bias = (20 - 80) * 512K / 100 = -307K
1989 * This reduces allocations by 307K for this
1992 mg
->mg_bias
= ((cu
- vu
) *
1993 (int64_t)mg
->mg_aliquot
) / 100;
1996 if ((flags
& METASLAB_FASTWRITE
) ||
1997 atomic_add_64_nv(&mc
->mc_aliquot
, asize
) >=
1998 mg
->mg_aliquot
+ mg
->mg_bias
) {
1999 mc
->mc_rotor
= mg
->mg_next
;
2003 DVA_SET_VDEV(&dva
[d
], vd
->vdev_id
);
2004 DVA_SET_OFFSET(&dva
[d
], offset
);
2005 DVA_SET_GANG(&dva
[d
], !!(flags
& METASLAB_GANG_HEADER
));
2006 DVA_SET_ASIZE(&dva
[d
], asize
);
2008 if (flags
& METASLAB_FASTWRITE
) {
2009 atomic_add_64(&vd
->vdev_pending_fastwrite
,
2011 mutex_exit(&mc
->mc_fastwrite_lock
);
2017 mc
->mc_rotor
= mg
->mg_next
;
2019 } while ((mg
= mg
->mg_next
) != rotor
);
2023 ASSERT(dshift
< 64);
2027 if (!allocatable
&& !zio_lock
) {
2033 bzero(&dva
[d
], sizeof (dva_t
));
2035 if (flags
& METASLAB_FASTWRITE
)
2036 mutex_exit(&mc
->mc_fastwrite_lock
);
2038 return (SET_ERROR(ENOSPC
));
2042 * Free the block represented by DVA in the context of the specified
2043 * transaction group.
2046 metaslab_free_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
, boolean_t now
)
2048 uint64_t vdev
= DVA_GET_VDEV(dva
);
2049 uint64_t offset
= DVA_GET_OFFSET(dva
);
2050 uint64_t size
= DVA_GET_ASIZE(dva
);
2054 ASSERT(DVA_IS_VALID(dva
));
2056 if (txg
> spa_freeze_txg(spa
))
2059 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
||
2060 (offset
>> vd
->vdev_ms_shift
) >= vd
->vdev_ms_count
) {
2061 cmn_err(CE_WARN
, "metaslab_free_dva(): bad DVA %llu:%llu",
2062 (u_longlong_t
)vdev
, (u_longlong_t
)offset
);
2067 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
2069 if (DVA_GET_GANG(dva
))
2070 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
2072 mutex_enter(&msp
->ms_lock
);
2075 range_tree_remove(msp
->ms_alloctree
[txg
& TXG_MASK
],
2078 VERIFY(!msp
->ms_condensing
);
2079 VERIFY3U(offset
, >=, msp
->ms_start
);
2080 VERIFY3U(offset
+ size
, <=, msp
->ms_start
+ msp
->ms_size
);
2081 VERIFY3U(range_tree_space(msp
->ms_tree
) + size
, <=,
2083 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
2084 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
2085 range_tree_add(msp
->ms_tree
, offset
, size
);
2087 if (range_tree_space(msp
->ms_freetree
[txg
& TXG_MASK
]) == 0)
2088 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
2089 range_tree_add(msp
->ms_freetree
[txg
& TXG_MASK
],
2093 mutex_exit(&msp
->ms_lock
);
2097 * Intent log support: upon opening the pool after a crash, notify the SPA
2098 * of blocks that the intent log has allocated for immediate write, but
2099 * which are still considered free by the SPA because the last transaction
2100 * group didn't commit yet.
2103 metaslab_claim_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
)
2105 uint64_t vdev
= DVA_GET_VDEV(dva
);
2106 uint64_t offset
= DVA_GET_OFFSET(dva
);
2107 uint64_t size
= DVA_GET_ASIZE(dva
);
2112 ASSERT(DVA_IS_VALID(dva
));
2114 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
||
2115 (offset
>> vd
->vdev_ms_shift
) >= vd
->vdev_ms_count
)
2116 return (SET_ERROR(ENXIO
));
2118 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
2120 if (DVA_GET_GANG(dva
))
2121 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
2123 mutex_enter(&msp
->ms_lock
);
2125 if ((txg
!= 0 && spa_writeable(spa
)) || !msp
->ms_loaded
)
2126 error
= metaslab_activate(msp
, METASLAB_WEIGHT_SECONDARY
);
2128 if (error
== 0 && !range_tree_contains(msp
->ms_tree
, offset
, size
))
2129 error
= SET_ERROR(ENOENT
);
2131 if (error
|| txg
== 0) { /* txg == 0 indicates dry run */
2132 mutex_exit(&msp
->ms_lock
);
2136 VERIFY(!msp
->ms_condensing
);
2137 VERIFY0(P2PHASE(offset
, 1ULL << vd
->vdev_ashift
));
2138 VERIFY0(P2PHASE(size
, 1ULL << vd
->vdev_ashift
));
2139 VERIFY3U(range_tree_space(msp
->ms_tree
) - size
, <=, msp
->ms_size
);
2140 range_tree_remove(msp
->ms_tree
, offset
, size
);
2142 if (spa_writeable(spa
)) { /* don't dirty if we're zdb(1M) */
2143 if (range_tree_space(msp
->ms_alloctree
[txg
& TXG_MASK
]) == 0)
2144 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
2145 range_tree_add(msp
->ms_alloctree
[txg
& TXG_MASK
], offset
, size
);
2148 mutex_exit(&msp
->ms_lock
);
2154 metaslab_alloc(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
, blkptr_t
*bp
,
2155 int ndvas
, uint64_t txg
, blkptr_t
*hintbp
, int flags
)
2157 dva_t
*dva
= bp
->blk_dva
;
2158 dva_t
*hintdva
= hintbp
->blk_dva
;
2161 ASSERT(bp
->blk_birth
== 0);
2162 ASSERT(BP_PHYSICAL_BIRTH(bp
) == 0);
2164 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
2166 if (mc
->mc_rotor
== NULL
) { /* no vdevs in this class */
2167 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
2168 return (SET_ERROR(ENOSPC
));
2171 ASSERT(ndvas
> 0 && ndvas
<= spa_max_replication(spa
));
2172 ASSERT(BP_GET_NDVAS(bp
) == 0);
2173 ASSERT(hintbp
== NULL
|| ndvas
<= BP_GET_NDVAS(hintbp
));
2175 for (d
= 0; d
< ndvas
; d
++) {
2176 error
= metaslab_alloc_dva(spa
, mc
, psize
, dva
, d
, hintdva
,
2179 for (d
--; d
>= 0; d
--) {
2180 metaslab_free_dva(spa
, &dva
[d
], txg
, B_TRUE
);
2181 bzero(&dva
[d
], sizeof (dva_t
));
2183 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
2188 ASSERT(BP_GET_NDVAS(bp
) == ndvas
);
2190 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
2192 BP_SET_BIRTH(bp
, txg
, txg
);
2198 metaslab_free(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
, boolean_t now
)
2200 const dva_t
*dva
= bp
->blk_dva
;
2201 int d
, ndvas
= BP_GET_NDVAS(bp
);
2203 ASSERT(!BP_IS_HOLE(bp
));
2204 ASSERT(!now
|| bp
->blk_birth
>= spa_syncing_txg(spa
));
2206 spa_config_enter(spa
, SCL_FREE
, FTAG
, RW_READER
);
2208 for (d
= 0; d
< ndvas
; d
++)
2209 metaslab_free_dva(spa
, &dva
[d
], txg
, now
);
2211 spa_config_exit(spa
, SCL_FREE
, FTAG
);
2215 metaslab_claim(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
)
2217 const dva_t
*dva
= bp
->blk_dva
;
2218 int ndvas
= BP_GET_NDVAS(bp
);
2221 ASSERT(!BP_IS_HOLE(bp
));
2225 * First do a dry run to make sure all DVAs are claimable,
2226 * so we don't have to unwind from partial failures below.
2228 if ((error
= metaslab_claim(spa
, bp
, 0)) != 0)
2232 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
2234 for (d
= 0; d
< ndvas
; d
++)
2235 if ((error
= metaslab_claim_dva(spa
, &dva
[d
], txg
)) != 0)
2238 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
2240 ASSERT(error
== 0 || txg
== 0);
2246 metaslab_fastwrite_mark(spa_t
*spa
, const blkptr_t
*bp
)
2248 const dva_t
*dva
= bp
->blk_dva
;
2249 int ndvas
= BP_GET_NDVAS(bp
);
2250 uint64_t psize
= BP_GET_PSIZE(bp
);
2254 ASSERT(!BP_IS_HOLE(bp
));
2257 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2259 for (d
= 0; d
< ndvas
; d
++) {
2260 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
2262 atomic_add_64(&vd
->vdev_pending_fastwrite
, psize
);
2265 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2269 metaslab_fastwrite_unmark(spa_t
*spa
, const blkptr_t
*bp
)
2271 const dva_t
*dva
= bp
->blk_dva
;
2272 int ndvas
= BP_GET_NDVAS(bp
);
2273 uint64_t psize
= BP_GET_PSIZE(bp
);
2277 ASSERT(!BP_IS_HOLE(bp
));
2280 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2282 for (d
= 0; d
< ndvas
; d
++) {
2283 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
2285 ASSERT3U(vd
->vdev_pending_fastwrite
, >=, psize
);
2286 atomic_sub_64(&vd
->vdev_pending_fastwrite
, psize
);
2289 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2293 metaslab_check_free(spa_t
*spa
, const blkptr_t
*bp
)
2297 if ((zfs_flags
& ZFS_DEBUG_ZIO_FREE
) == 0)
2300 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2301 for (i
= 0; i
< BP_GET_NDVAS(bp
); i
++) {
2302 uint64_t vdev
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
2303 vdev_t
*vd
= vdev_lookup_top(spa
, vdev
);
2304 uint64_t offset
= DVA_GET_OFFSET(&bp
->blk_dva
[i
]);
2305 uint64_t size
= DVA_GET_ASIZE(&bp
->blk_dva
[i
]);
2306 metaslab_t
*msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
2309 range_tree_verify(msp
->ms_tree
, offset
, size
);
2311 for (j
= 0; j
< TXG_SIZE
; j
++)
2312 range_tree_verify(msp
->ms_freetree
[j
], offset
, size
);
2313 for (j
= 0; j
< TXG_DEFER_SIZE
; j
++)
2314 range_tree_verify(msp
->ms_defertree
[j
], offset
, size
);
2316 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2319 #if defined(_KERNEL) && defined(HAVE_SPL)
2320 module_param(metaslab_debug_load
, int, 0644);
2321 module_param(metaslab_debug_unload
, int, 0644);
2322 MODULE_PARM_DESC(metaslab_debug_load
,
2323 "load all metaslabs when pool is first opened");
2324 MODULE_PARM_DESC(metaslab_debug_unload
,
2325 "prevent metaslabs from being unloaded");
2327 module_param(zfs_mg_noalloc_threshold
, int, 0644);
2328 MODULE_PARM_DESC(zfs_mg_noalloc_threshold
,
2329 "percentage of free space for metaslab group to allow allocation");
2330 #endif /* _KERNEL && HAVE_SPL */