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>
35 #define WITH_DF_BLOCK_ALLOCATOR
38 * Allow allocations to switch to gang blocks quickly. We do this to
39 * avoid having to load lots of space_maps in a given txg. There are,
40 * however, some cases where we want to avoid "fast" ganging and instead
41 * we want to do an exhaustive search of all metaslabs on this device.
42 * Currently we don't allow any gang, zil, or dump device related allocations
45 #define CAN_FASTGANG(flags) \
46 (!((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER | \
47 METASLAB_GANG_AVOID)))
49 uint64_t metaslab_aliquot
= 512ULL << 10;
50 uint64_t metaslab_gang_bang
= SPA_MAXBLOCKSIZE
+ 1; /* force gang blocks */
53 * The in-core space map representation is more compact than its on-disk form.
54 * The zfs_condense_pct determines how much more compact the in-core
55 * space_map representation must be before we compact it on-disk.
56 * Values should be greater than or equal to 100.
58 int zfs_condense_pct
= 200;
61 * This value defines the number of allowed allocation failures per vdev.
62 * If a device reaches this threshold in a given txg then we consider skipping
63 * allocations on that device.
65 int zfs_mg_alloc_failures
;
68 * Metaslab debugging: when set, keeps all space maps in core to verify frees.
70 int metaslab_debug
= 0;
73 * Minimum size which forces the dynamic allocator to change
74 * it's allocation strategy. Once the space map cannot satisfy
75 * an allocation of this size then it switches to using more
76 * aggressive strategy (i.e search by size rather than offset).
78 uint64_t metaslab_df_alloc_threshold
= SPA_MAXBLOCKSIZE
;
81 * The minimum free space, in percent, which must be available
82 * in a space map to continue allocations in a first-fit fashion.
83 * Once the space_map's free space drops below this level we dynamically
84 * switch to using best-fit allocations.
86 int metaslab_df_free_pct
= 4;
89 * A metaslab is considered "free" if it contains a contiguous
90 * segment which is greater than metaslab_min_alloc_size.
92 uint64_t metaslab_min_alloc_size
= DMU_MAX_ACCESS
;
95 * Max number of space_maps to prefetch.
97 int metaslab_prefetch_limit
= SPA_DVAS_PER_BP
;
100 * Percentage bonus multiplier for metaslabs that are in the bonus area.
102 int metaslab_smo_bonus_pct
= 150;
105 * ==========================================================================
107 * ==========================================================================
110 metaslab_class_create(spa_t
*spa
, space_map_ops_t
*ops
)
112 metaslab_class_t
*mc
;
114 mc
= kmem_zalloc(sizeof (metaslab_class_t
), KM_PUSHPAGE
);
119 mutex_init(&mc
->mc_fastwrite_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
125 metaslab_class_destroy(metaslab_class_t
*mc
)
127 ASSERT(mc
->mc_rotor
== NULL
);
128 ASSERT(mc
->mc_alloc
== 0);
129 ASSERT(mc
->mc_deferred
== 0);
130 ASSERT(mc
->mc_space
== 0);
131 ASSERT(mc
->mc_dspace
== 0);
133 mutex_destroy(&mc
->mc_fastwrite_lock
);
134 kmem_free(mc
, sizeof (metaslab_class_t
));
138 metaslab_class_validate(metaslab_class_t
*mc
)
140 metaslab_group_t
*mg
;
144 * Must hold one of the spa_config locks.
146 ASSERT(spa_config_held(mc
->mc_spa
, SCL_ALL
, RW_READER
) ||
147 spa_config_held(mc
->mc_spa
, SCL_ALL
, RW_WRITER
));
149 if ((mg
= mc
->mc_rotor
) == NULL
)
154 ASSERT(vd
->vdev_mg
!= NULL
);
155 ASSERT3P(vd
->vdev_top
, ==, vd
);
156 ASSERT3P(mg
->mg_class
, ==, mc
);
157 ASSERT3P(vd
->vdev_ops
, !=, &vdev_hole_ops
);
158 } while ((mg
= mg
->mg_next
) != mc
->mc_rotor
);
164 metaslab_class_space_update(metaslab_class_t
*mc
, int64_t alloc_delta
,
165 int64_t defer_delta
, int64_t space_delta
, int64_t dspace_delta
)
167 atomic_add_64(&mc
->mc_alloc
, alloc_delta
);
168 atomic_add_64(&mc
->mc_deferred
, defer_delta
);
169 atomic_add_64(&mc
->mc_space
, space_delta
);
170 atomic_add_64(&mc
->mc_dspace
, dspace_delta
);
174 metaslab_class_get_alloc(metaslab_class_t
*mc
)
176 return (mc
->mc_alloc
);
180 metaslab_class_get_deferred(metaslab_class_t
*mc
)
182 return (mc
->mc_deferred
);
186 metaslab_class_get_space(metaslab_class_t
*mc
)
188 return (mc
->mc_space
);
192 metaslab_class_get_dspace(metaslab_class_t
*mc
)
194 return (spa_deflate(mc
->mc_spa
) ? mc
->mc_dspace
: mc
->mc_space
);
198 * ==========================================================================
200 * ==========================================================================
203 metaslab_compare(const void *x1
, const void *x2
)
205 const metaslab_t
*m1
= x1
;
206 const metaslab_t
*m2
= x2
;
208 if (m1
->ms_weight
< m2
->ms_weight
)
210 if (m1
->ms_weight
> m2
->ms_weight
)
214 * If the weights are identical, use the offset to force uniqueness.
216 if (m1
->ms_map
->sm_start
< m2
->ms_map
->sm_start
)
218 if (m1
->ms_map
->sm_start
> m2
->ms_map
->sm_start
)
221 ASSERT3P(m1
, ==, m2
);
227 metaslab_group_create(metaslab_class_t
*mc
, vdev_t
*vd
)
229 metaslab_group_t
*mg
;
231 mg
= kmem_zalloc(sizeof (metaslab_group_t
), KM_PUSHPAGE
);
232 mutex_init(&mg
->mg_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
233 avl_create(&mg
->mg_metaslab_tree
, metaslab_compare
,
234 sizeof (metaslab_t
), offsetof(struct metaslab
, ms_group_node
));
237 mg
->mg_activation_count
= 0;
243 metaslab_group_destroy(metaslab_group_t
*mg
)
245 ASSERT(mg
->mg_prev
== NULL
);
246 ASSERT(mg
->mg_next
== NULL
);
248 * We may have gone below zero with the activation count
249 * either because we never activated in the first place or
250 * because we're done, and possibly removing the vdev.
252 ASSERT(mg
->mg_activation_count
<= 0);
254 avl_destroy(&mg
->mg_metaslab_tree
);
255 mutex_destroy(&mg
->mg_lock
);
256 kmem_free(mg
, sizeof (metaslab_group_t
));
260 metaslab_group_activate(metaslab_group_t
*mg
)
262 metaslab_class_t
*mc
= mg
->mg_class
;
263 metaslab_group_t
*mgprev
, *mgnext
;
265 ASSERT(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_WRITER
));
267 ASSERT(mc
->mc_rotor
!= mg
);
268 ASSERT(mg
->mg_prev
== NULL
);
269 ASSERT(mg
->mg_next
== NULL
);
270 ASSERT(mg
->mg_activation_count
<= 0);
272 if (++mg
->mg_activation_count
<= 0)
275 mg
->mg_aliquot
= metaslab_aliquot
* MAX(1, mg
->mg_vd
->vdev_children
);
277 if ((mgprev
= mc
->mc_rotor
) == NULL
) {
281 mgnext
= mgprev
->mg_next
;
282 mg
->mg_prev
= mgprev
;
283 mg
->mg_next
= mgnext
;
284 mgprev
->mg_next
= mg
;
285 mgnext
->mg_prev
= mg
;
291 metaslab_group_passivate(metaslab_group_t
*mg
)
293 metaslab_class_t
*mc
= mg
->mg_class
;
294 metaslab_group_t
*mgprev
, *mgnext
;
296 ASSERT(spa_config_held(mc
->mc_spa
, SCL_ALLOC
, RW_WRITER
));
298 if (--mg
->mg_activation_count
!= 0) {
299 ASSERT(mc
->mc_rotor
!= mg
);
300 ASSERT(mg
->mg_prev
== NULL
);
301 ASSERT(mg
->mg_next
== NULL
);
302 ASSERT(mg
->mg_activation_count
< 0);
306 mgprev
= mg
->mg_prev
;
307 mgnext
= mg
->mg_next
;
312 mc
->mc_rotor
= mgnext
;
313 mgprev
->mg_next
= mgnext
;
314 mgnext
->mg_prev
= mgprev
;
322 metaslab_group_add(metaslab_group_t
*mg
, metaslab_t
*msp
)
324 mutex_enter(&mg
->mg_lock
);
325 ASSERT(msp
->ms_group
== NULL
);
328 avl_add(&mg
->mg_metaslab_tree
, msp
);
329 mutex_exit(&mg
->mg_lock
);
333 metaslab_group_remove(metaslab_group_t
*mg
, metaslab_t
*msp
)
335 mutex_enter(&mg
->mg_lock
);
336 ASSERT(msp
->ms_group
== mg
);
337 avl_remove(&mg
->mg_metaslab_tree
, msp
);
338 msp
->ms_group
= NULL
;
339 mutex_exit(&mg
->mg_lock
);
343 metaslab_group_sort(metaslab_group_t
*mg
, metaslab_t
*msp
, uint64_t weight
)
346 * Although in principle the weight can be any value, in
347 * practice we do not use values in the range [1, 510].
349 ASSERT(weight
>= SPA_MINBLOCKSIZE
-1 || weight
== 0);
350 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
352 mutex_enter(&mg
->mg_lock
);
353 ASSERT(msp
->ms_group
== mg
);
354 avl_remove(&mg
->mg_metaslab_tree
, msp
);
355 msp
->ms_weight
= weight
;
356 avl_add(&mg
->mg_metaslab_tree
, msp
);
357 mutex_exit(&mg
->mg_lock
);
361 * ==========================================================================
362 * Common allocator routines
363 * ==========================================================================
366 metaslab_segsize_compare(const void *x1
, const void *x2
)
368 const space_seg_t
*s1
= x1
;
369 const space_seg_t
*s2
= x2
;
370 uint64_t ss_size1
= s1
->ss_end
- s1
->ss_start
;
371 uint64_t ss_size2
= s2
->ss_end
- s2
->ss_start
;
373 if (ss_size1
< ss_size2
)
375 if (ss_size1
> ss_size2
)
378 if (s1
->ss_start
< s2
->ss_start
)
380 if (s1
->ss_start
> s2
->ss_start
)
386 #if defined(WITH_FF_BLOCK_ALLOCATOR) || \
387 defined(WITH_DF_BLOCK_ALLOCATOR) || \
388 defined(WITH_CDF_BLOCK_ALLOCATOR)
390 * This is a helper function that can be used by the allocator to find
391 * a suitable block to allocate. This will search the specified AVL
392 * tree looking for a block that matches the specified criteria.
395 metaslab_block_picker(avl_tree_t
*t
, uint64_t *cursor
, uint64_t size
,
398 space_seg_t
*ss
, ssearch
;
401 ssearch
.ss_start
= *cursor
;
402 ssearch
.ss_end
= *cursor
+ size
;
404 ss
= avl_find(t
, &ssearch
, &where
);
406 ss
= avl_nearest(t
, where
, AVL_AFTER
);
409 uint64_t offset
= P2ROUNDUP(ss
->ss_start
, align
);
411 if (offset
+ size
<= ss
->ss_end
) {
412 *cursor
= offset
+ size
;
415 ss
= AVL_NEXT(t
, ss
);
419 * If we know we've searched the whole map (*cursor == 0), give up.
420 * Otherwise, reset the cursor to the beginning and try again.
426 return (metaslab_block_picker(t
, cursor
, size
, align
));
428 #endif /* WITH_FF/DF/CDF_BLOCK_ALLOCATOR */
431 metaslab_pp_load(space_map_t
*sm
)
435 ASSERT(sm
->sm_ppd
== NULL
);
436 sm
->sm_ppd
= kmem_zalloc(64 * sizeof (uint64_t), KM_PUSHPAGE
);
438 sm
->sm_pp_root
= kmem_alloc(sizeof (avl_tree_t
), KM_PUSHPAGE
);
439 avl_create(sm
->sm_pp_root
, metaslab_segsize_compare
,
440 sizeof (space_seg_t
), offsetof(struct space_seg
, ss_pp_node
));
442 for (ss
= avl_first(&sm
->sm_root
); ss
; ss
= AVL_NEXT(&sm
->sm_root
, ss
))
443 avl_add(sm
->sm_pp_root
, ss
);
447 metaslab_pp_unload(space_map_t
*sm
)
451 kmem_free(sm
->sm_ppd
, 64 * sizeof (uint64_t));
454 while (avl_destroy_nodes(sm
->sm_pp_root
, &cookie
) != NULL
) {
455 /* tear down the tree */
458 avl_destroy(sm
->sm_pp_root
);
459 kmem_free(sm
->sm_pp_root
, sizeof (avl_tree_t
));
460 sm
->sm_pp_root
= NULL
;
465 metaslab_pp_claim(space_map_t
*sm
, uint64_t start
, uint64_t size
)
467 /* No need to update cursor */
472 metaslab_pp_free(space_map_t
*sm
, uint64_t start
, uint64_t size
)
474 /* No need to update cursor */
478 * Return the maximum contiguous segment within the metaslab.
481 metaslab_pp_maxsize(space_map_t
*sm
)
483 avl_tree_t
*t
= sm
->sm_pp_root
;
486 if (t
== NULL
|| (ss
= avl_last(t
)) == NULL
)
489 return (ss
->ss_end
- ss
->ss_start
);
492 #if defined(WITH_FF_BLOCK_ALLOCATOR)
494 * ==========================================================================
495 * The first-fit block allocator
496 * ==========================================================================
499 metaslab_ff_alloc(space_map_t
*sm
, uint64_t size
)
501 avl_tree_t
*t
= &sm
->sm_root
;
502 uint64_t align
= size
& -size
;
503 uint64_t *cursor
= (uint64_t *)sm
->sm_ppd
+ highbit(align
) - 1;
505 return (metaslab_block_picker(t
, cursor
, size
, align
));
510 metaslab_ff_fragmented(space_map_t
*sm
)
515 static space_map_ops_t metaslab_ff_ops
= {
522 metaslab_ff_fragmented
525 space_map_ops_t
*zfs_metaslab_ops
= &metaslab_ff_ops
;
526 #endif /* WITH_FF_BLOCK_ALLOCATOR */
528 #if defined(WITH_DF_BLOCK_ALLOCATOR)
530 * ==========================================================================
531 * Dynamic block allocator -
532 * Uses the first fit allocation scheme until space get low and then
533 * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
534 * and metaslab_df_free_pct to determine when to switch the allocation scheme.
535 * ==========================================================================
538 metaslab_df_alloc(space_map_t
*sm
, uint64_t size
)
540 avl_tree_t
*t
= &sm
->sm_root
;
541 uint64_t align
= size
& -size
;
542 uint64_t *cursor
= (uint64_t *)sm
->sm_ppd
+ highbit(align
) - 1;
543 uint64_t max_size
= metaslab_pp_maxsize(sm
);
544 int free_pct
= sm
->sm_space
* 100 / sm
->sm_size
;
546 ASSERT(MUTEX_HELD(sm
->sm_lock
));
547 ASSERT3U(avl_numnodes(&sm
->sm_root
), ==, avl_numnodes(sm
->sm_pp_root
));
553 * If we're running low on space switch to using the size
554 * sorted AVL tree (best-fit).
556 if (max_size
< metaslab_df_alloc_threshold
||
557 free_pct
< metaslab_df_free_pct
) {
562 return (metaslab_block_picker(t
, cursor
, size
, 1ULL));
566 metaslab_df_fragmented(space_map_t
*sm
)
568 uint64_t max_size
= metaslab_pp_maxsize(sm
);
569 int free_pct
= sm
->sm_space
* 100 / sm
->sm_size
;
571 if (max_size
>= metaslab_df_alloc_threshold
&&
572 free_pct
>= metaslab_df_free_pct
)
578 static space_map_ops_t metaslab_df_ops
= {
585 metaslab_df_fragmented
588 space_map_ops_t
*zfs_metaslab_ops
= &metaslab_df_ops
;
589 #endif /* WITH_DF_BLOCK_ALLOCATOR */
592 * ==========================================================================
593 * Other experimental allocators
594 * ==========================================================================
596 #if defined(WITH_CDF_BLOCK_ALLOCATOR)
598 metaslab_cdf_alloc(space_map_t
*sm
, uint64_t size
)
600 avl_tree_t
*t
= &sm
->sm_root
;
601 uint64_t *cursor
= (uint64_t *)sm
->sm_ppd
;
602 uint64_t *extent_end
= (uint64_t *)sm
->sm_ppd
+ 1;
603 uint64_t max_size
= metaslab_pp_maxsize(sm
);
604 uint64_t rsize
= size
;
607 ASSERT(MUTEX_HELD(sm
->sm_lock
));
608 ASSERT3U(avl_numnodes(&sm
->sm_root
), ==, avl_numnodes(sm
->sm_pp_root
));
613 ASSERT3U(*extent_end
, >=, *cursor
);
616 * If we're running low on space switch to using the size
617 * sorted AVL tree (best-fit).
619 if ((*cursor
+ size
) > *extent_end
) {
622 *cursor
= *extent_end
= 0;
624 if (max_size
> 2 * SPA_MAXBLOCKSIZE
)
625 rsize
= MIN(metaslab_min_alloc_size
, max_size
);
626 offset
= metaslab_block_picker(t
, extent_end
, rsize
, 1ULL);
628 *cursor
= offset
+ size
;
630 offset
= metaslab_block_picker(t
, cursor
, rsize
, 1ULL);
632 ASSERT3U(*cursor
, <=, *extent_end
);
637 metaslab_cdf_fragmented(space_map_t
*sm
)
639 uint64_t max_size
= metaslab_pp_maxsize(sm
);
641 if (max_size
> (metaslab_min_alloc_size
* 10))
646 static space_map_ops_t metaslab_cdf_ops
= {
653 metaslab_cdf_fragmented
656 space_map_ops_t
*zfs_metaslab_ops
= &metaslab_cdf_ops
;
657 #endif /* WITH_CDF_BLOCK_ALLOCATOR */
659 #if defined(WITH_NDF_BLOCK_ALLOCATOR)
660 uint64_t metaslab_ndf_clump_shift
= 4;
663 metaslab_ndf_alloc(space_map_t
*sm
, uint64_t size
)
665 avl_tree_t
*t
= &sm
->sm_root
;
667 space_seg_t
*ss
, ssearch
;
668 uint64_t hbit
= highbit(size
);
669 uint64_t *cursor
= (uint64_t *)sm
->sm_ppd
+ hbit
- 1;
670 uint64_t max_size
= metaslab_pp_maxsize(sm
);
672 ASSERT(MUTEX_HELD(sm
->sm_lock
));
673 ASSERT3U(avl_numnodes(&sm
->sm_root
), ==, avl_numnodes(sm
->sm_pp_root
));
678 ssearch
.ss_start
= *cursor
;
679 ssearch
.ss_end
= *cursor
+ size
;
681 ss
= avl_find(t
, &ssearch
, &where
);
682 if (ss
== NULL
|| (ss
->ss_start
+ size
> ss
->ss_end
)) {
685 ssearch
.ss_start
= 0;
686 ssearch
.ss_end
= MIN(max_size
,
687 1ULL << (hbit
+ metaslab_ndf_clump_shift
));
688 ss
= avl_find(t
, &ssearch
, &where
);
690 ss
= avl_nearest(t
, where
, AVL_AFTER
);
695 if (ss
->ss_start
+ size
<= ss
->ss_end
) {
696 *cursor
= ss
->ss_start
+ size
;
697 return (ss
->ss_start
);
704 metaslab_ndf_fragmented(space_map_t
*sm
)
706 uint64_t max_size
= metaslab_pp_maxsize(sm
);
708 if (max_size
> (metaslab_min_alloc_size
<< metaslab_ndf_clump_shift
))
714 static space_map_ops_t metaslab_ndf_ops
= {
721 metaslab_ndf_fragmented
724 space_map_ops_t
*zfs_metaslab_ops
= &metaslab_ndf_ops
;
725 #endif /* WITH_NDF_BLOCK_ALLOCATOR */
728 * ==========================================================================
730 * ==========================================================================
733 metaslab_init(metaslab_group_t
*mg
, space_map_obj_t
*smo
,
734 uint64_t start
, uint64_t size
, uint64_t txg
)
736 vdev_t
*vd
= mg
->mg_vd
;
739 msp
= kmem_zalloc(sizeof (metaslab_t
), KM_PUSHPAGE
);
740 mutex_init(&msp
->ms_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
742 msp
->ms_smo_syncing
= *smo
;
745 * We create the main space map here, but we don't create the
746 * allocmaps and freemaps until metaslab_sync_done(). This serves
747 * two purposes: it allows metaslab_sync_done() to detect the
748 * addition of new space; and for debugging, it ensures that we'd
749 * data fault on any attempt to use this metaslab before it's ready.
751 msp
->ms_map
= kmem_zalloc(sizeof (space_map_t
), KM_PUSHPAGE
);
752 space_map_create(msp
->ms_map
, start
, size
,
753 vd
->vdev_ashift
, &msp
->ms_lock
);
755 metaslab_group_add(mg
, msp
);
757 if (metaslab_debug
&& smo
->smo_object
!= 0) {
758 mutex_enter(&msp
->ms_lock
);
759 VERIFY(space_map_load(msp
->ms_map
, mg
->mg_class
->mc_ops
,
760 SM_FREE
, smo
, spa_meta_objset(vd
->vdev_spa
)) == 0);
761 mutex_exit(&msp
->ms_lock
);
765 * If we're opening an existing pool (txg == 0) or creating
766 * a new one (txg == TXG_INITIAL), all space is available now.
767 * If we're adding space to an existing pool, the new space
768 * does not become available until after this txg has synced.
770 if (txg
<= TXG_INITIAL
)
771 metaslab_sync_done(msp
, 0);
774 vdev_dirty(vd
, 0, NULL
, txg
);
775 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
782 metaslab_fini(metaslab_t
*msp
)
784 metaslab_group_t
*mg
= msp
->ms_group
;
787 vdev_space_update(mg
->mg_vd
,
788 -msp
->ms_smo
.smo_alloc
, 0, -msp
->ms_map
->sm_size
);
790 metaslab_group_remove(mg
, msp
);
792 mutex_enter(&msp
->ms_lock
);
794 space_map_unload(msp
->ms_map
);
795 space_map_destroy(msp
->ms_map
);
796 kmem_free(msp
->ms_map
, sizeof (*msp
->ms_map
));
798 for (t
= 0; t
< TXG_SIZE
; t
++) {
799 space_map_destroy(msp
->ms_allocmap
[t
]);
800 space_map_destroy(msp
->ms_freemap
[t
]);
801 kmem_free(msp
->ms_allocmap
[t
], sizeof (*msp
->ms_allocmap
[t
]));
802 kmem_free(msp
->ms_freemap
[t
], sizeof (*msp
->ms_freemap
[t
]));
805 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
806 space_map_destroy(msp
->ms_defermap
[t
]);
807 kmem_free(msp
->ms_defermap
[t
], sizeof (*msp
->ms_defermap
[t
]));
810 ASSERT0(msp
->ms_deferspace
);
812 mutex_exit(&msp
->ms_lock
);
813 mutex_destroy(&msp
->ms_lock
);
815 kmem_free(msp
, sizeof (metaslab_t
));
818 #define METASLAB_WEIGHT_PRIMARY (1ULL << 63)
819 #define METASLAB_WEIGHT_SECONDARY (1ULL << 62)
820 #define METASLAB_ACTIVE_MASK \
821 (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
824 metaslab_weight(metaslab_t
*msp
)
826 metaslab_group_t
*mg
= msp
->ms_group
;
827 space_map_t
*sm
= msp
->ms_map
;
828 space_map_obj_t
*smo
= &msp
->ms_smo
;
829 vdev_t
*vd
= mg
->mg_vd
;
830 uint64_t weight
, space
;
832 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
835 * This vdev is in the process of being removed so there is nothing
838 if (vd
->vdev_removing
) {
839 ASSERT0(smo
->smo_alloc
);
840 ASSERT0(vd
->vdev_ms_shift
);
845 * The baseline weight is the metaslab's free space.
847 space
= sm
->sm_size
- smo
->smo_alloc
;
851 * Modern disks have uniform bit density and constant angular velocity.
852 * Therefore, the outer recording zones are faster (higher bandwidth)
853 * than the inner zones by the ratio of outer to inner track diameter,
854 * which is typically around 2:1. We account for this by assigning
855 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
856 * In effect, this means that we'll select the metaslab with the most
857 * free bandwidth rather than simply the one with the most free space.
859 weight
= 2 * weight
-
860 ((sm
->sm_start
>> vd
->vdev_ms_shift
) * weight
) / vd
->vdev_ms_count
;
861 ASSERT(weight
>= space
&& weight
<= 2 * space
);
864 * For locality, assign higher weight to metaslabs which have
865 * a lower offset than what we've already activated.
867 if (sm
->sm_start
<= mg
->mg_bonus_area
)
868 weight
*= (metaslab_smo_bonus_pct
/ 100);
869 ASSERT(weight
>= space
&&
870 weight
<= 2 * (metaslab_smo_bonus_pct
/ 100) * space
);
872 if (sm
->sm_loaded
&& !sm
->sm_ops
->smop_fragmented(sm
)) {
874 * If this metaslab is one we're actively using, adjust its
875 * weight to make it preferable to any inactive metaslab so
876 * we'll polish it off.
878 weight
|= (msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
884 metaslab_prefetch(metaslab_group_t
*mg
)
886 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
888 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
891 mutex_enter(&mg
->mg_lock
);
894 * Prefetch the next potential metaslabs
896 for (msp
= avl_first(t
), m
= 0; msp
; msp
= AVL_NEXT(t
, msp
), m
++) {
897 space_map_t
*sm
= msp
->ms_map
;
898 space_map_obj_t
*smo
= &msp
->ms_smo
;
900 /* If we have reached our prefetch limit then we're done */
901 if (m
>= metaslab_prefetch_limit
)
904 if (!sm
->sm_loaded
&& smo
->smo_object
!= 0) {
905 mutex_exit(&mg
->mg_lock
);
906 dmu_prefetch(spa_meta_objset(spa
), smo
->smo_object
,
907 0ULL, smo
->smo_objsize
);
908 mutex_enter(&mg
->mg_lock
);
911 mutex_exit(&mg
->mg_lock
);
915 metaslab_activate(metaslab_t
*msp
, uint64_t activation_weight
)
917 metaslab_group_t
*mg
= msp
->ms_group
;
918 space_map_t
*sm
= msp
->ms_map
;
919 space_map_ops_t
*sm_ops
= msp
->ms_group
->mg_class
->mc_ops
;
922 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
924 if ((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0) {
925 space_map_load_wait(sm
);
926 if (!sm
->sm_loaded
) {
927 space_map_obj_t
*smo
= &msp
->ms_smo
;
929 int error
= space_map_load(sm
, sm_ops
, SM_FREE
, smo
,
930 spa_meta_objset(msp
->ms_group
->mg_vd
->vdev_spa
));
932 metaslab_group_sort(msp
->ms_group
, msp
, 0);
935 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++)
936 space_map_walk(msp
->ms_defermap
[t
],
937 space_map_claim
, sm
);
942 * Track the bonus area as we activate new metaslabs.
944 if (sm
->sm_start
> mg
->mg_bonus_area
) {
945 mutex_enter(&mg
->mg_lock
);
946 mg
->mg_bonus_area
= sm
->sm_start
;
947 mutex_exit(&mg
->mg_lock
);
950 metaslab_group_sort(msp
->ms_group
, msp
,
951 msp
->ms_weight
| activation_weight
);
953 ASSERT(sm
->sm_loaded
);
954 ASSERT(msp
->ms_weight
& METASLAB_ACTIVE_MASK
);
960 metaslab_passivate(metaslab_t
*msp
, uint64_t size
)
963 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
964 * this metaslab again. In that case, it had better be empty,
965 * or we would be leaving space on the table.
967 ASSERT(size
>= SPA_MINBLOCKSIZE
|| msp
->ms_map
->sm_space
== 0);
968 metaslab_group_sort(msp
->ms_group
, msp
, MIN(msp
->ms_weight
, size
));
969 ASSERT((msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0);
973 * Determine if the in-core space map representation can be condensed on-disk.
974 * We would like to use the following criteria to make our decision:
976 * 1. The size of the space map object should not dramatically increase as a
977 * result of writing out our in-core free map.
979 * 2. The minimal on-disk space map representation is zfs_condense_pct/100
980 * times the size than the in-core representation (i.e. zfs_condense_pct = 110
981 * and in-core = 1MB, minimal = 1.1.MB).
983 * Checking the first condition is tricky since we don't want to walk
984 * the entire AVL tree calculating the estimated on-disk size. Instead we
985 * use the size-ordered AVL tree in the space map and calculate the
986 * size required for the largest segment in our in-core free map. If the
987 * size required to represent that segment on disk is larger than the space
988 * map object then we avoid condensing this map.
990 * To determine the second criterion we use a best-case estimate and assume
991 * each segment can be represented on-disk as a single 64-bit entry. We refer
992 * to this best-case estimate as the space map's minimal form.
995 metaslab_should_condense(metaslab_t
*msp
)
997 space_map_t
*sm
= msp
->ms_map
;
998 space_map_obj_t
*smo
= &msp
->ms_smo_syncing
;
1000 uint64_t size
, entries
, segsz
;
1002 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1003 ASSERT(sm
->sm_loaded
);
1006 * Use the sm_pp_root AVL tree, which is ordered by size, to obtain
1007 * the largest segment in the in-core free map. If the tree is
1008 * empty then we should condense the map.
1010 ss
= avl_last(sm
->sm_pp_root
);
1015 * Calculate the number of 64-bit entries this segment would
1016 * require when written to disk. If this single segment would be
1017 * larger on-disk than the entire current on-disk structure, then
1018 * clearly condensing will increase the on-disk structure size.
1020 size
= (ss
->ss_end
- ss
->ss_start
) >> sm
->sm_shift
;
1021 entries
= size
/ (MIN(size
, SM_RUN_MAX
));
1022 segsz
= entries
* sizeof (uint64_t);
1024 return (segsz
<= smo
->smo_objsize
&&
1025 smo
->smo_objsize
>= (zfs_condense_pct
*
1026 sizeof (uint64_t) * avl_numnodes(&sm
->sm_root
)) / 100);
1030 * Condense the on-disk space map representation to its minimized form.
1031 * The minimized form consists of a small number of allocations followed by
1032 * the in-core free map.
1035 metaslab_condense(metaslab_t
*msp
, uint64_t txg
, dmu_tx_t
*tx
)
1037 spa_t
*spa
= msp
->ms_group
->mg_vd
->vdev_spa
;
1038 space_map_t
*freemap
= msp
->ms_freemap
[txg
& TXG_MASK
];
1039 space_map_t condense_map
;
1040 space_map_t
*sm
= msp
->ms_map
;
1041 objset_t
*mos
= spa_meta_objset(spa
);
1042 space_map_obj_t
*smo
= &msp
->ms_smo_syncing
;
1045 ASSERT(MUTEX_HELD(&msp
->ms_lock
));
1046 ASSERT3U(spa_sync_pass(spa
), ==, 1);
1047 ASSERT(sm
->sm_loaded
);
1049 spa_dbgmsg(spa
, "condensing: txg %llu, msp[%llu] %p, "
1050 "smo size %llu, segments %lu", txg
,
1051 (msp
->ms_map
->sm_start
/ msp
->ms_map
->sm_size
), msp
,
1052 smo
->smo_objsize
, avl_numnodes(&sm
->sm_root
));
1055 * Create an map that is a 100% allocated map. We remove segments
1056 * that have been freed in this txg, any deferred frees that exist,
1057 * and any allocation in the future. Removing segments should be
1058 * a relatively inexpensive operation since we expect these maps to
1059 * a small number of nodes.
1061 space_map_create(&condense_map
, sm
->sm_start
, sm
->sm_size
,
1062 sm
->sm_shift
, sm
->sm_lock
);
1063 space_map_add(&condense_map
, condense_map
.sm_start
,
1064 condense_map
.sm_size
);
1067 * Remove what's been freed in this txg from the condense_map.
1068 * Since we're in sync_pass 1, we know that all the frees from
1069 * this txg are in the freemap.
1071 space_map_walk(freemap
, space_map_remove
, &condense_map
);
1073 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++)
1074 space_map_walk(msp
->ms_defermap
[t
],
1075 space_map_remove
, &condense_map
);
1077 for (t
= 1; t
< TXG_CONCURRENT_STATES
; t
++)
1078 space_map_walk(msp
->ms_allocmap
[(txg
+ t
) & TXG_MASK
],
1079 space_map_remove
, &condense_map
);
1082 * We're about to drop the metaslab's lock thus allowing
1083 * other consumers to change it's content. Set the
1084 * space_map's sm_condensing flag to ensure that
1085 * allocations on this metaslab do not occur while we're
1086 * in the middle of committing it to disk. This is only critical
1087 * for the ms_map as all other space_maps use per txg
1088 * views of their content.
1090 sm
->sm_condensing
= B_TRUE
;
1092 mutex_exit(&msp
->ms_lock
);
1093 space_map_truncate(smo
, mos
, tx
);
1094 mutex_enter(&msp
->ms_lock
);
1097 * While we would ideally like to create a space_map representation
1098 * that consists only of allocation records, doing so can be
1099 * prohibitively expensive because the in-core free map can be
1100 * large, and therefore computationally expensive to subtract
1101 * from the condense_map. Instead we sync out two maps, a cheap
1102 * allocation only map followed by the in-core free map. While not
1103 * optimal, this is typically close to optimal, and much cheaper to
1106 space_map_sync(&condense_map
, SM_ALLOC
, smo
, mos
, tx
);
1107 space_map_vacate(&condense_map
, NULL
, NULL
);
1108 space_map_destroy(&condense_map
);
1110 space_map_sync(sm
, SM_FREE
, smo
, mos
, tx
);
1111 sm
->sm_condensing
= B_FALSE
;
1113 spa_dbgmsg(spa
, "condensed: txg %llu, msp[%llu] %p, "
1114 "smo size %llu", txg
,
1115 (msp
->ms_map
->sm_start
/ msp
->ms_map
->sm_size
), msp
,
1120 * Write a metaslab to disk in the context of the specified transaction group.
1123 metaslab_sync(metaslab_t
*msp
, uint64_t txg
)
1125 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
1126 spa_t
*spa
= vd
->vdev_spa
;
1127 objset_t
*mos
= spa_meta_objset(spa
);
1128 space_map_t
*allocmap
= msp
->ms_allocmap
[txg
& TXG_MASK
];
1129 space_map_t
**freemap
= &msp
->ms_freemap
[txg
& TXG_MASK
];
1130 space_map_t
**freed_map
= &msp
->ms_freemap
[TXG_CLEAN(txg
) & TXG_MASK
];
1131 space_map_t
*sm
= msp
->ms_map
;
1132 space_map_obj_t
*smo
= &msp
->ms_smo_syncing
;
1136 ASSERT(!vd
->vdev_ishole
);
1139 * This metaslab has just been added so there's no work to do now.
1141 if (*freemap
== NULL
) {
1142 ASSERT3P(allocmap
, ==, NULL
);
1146 ASSERT3P(allocmap
, !=, NULL
);
1147 ASSERT3P(*freemap
, !=, NULL
);
1148 ASSERT3P(*freed_map
, !=, NULL
);
1150 if (allocmap
->sm_space
== 0 && (*freemap
)->sm_space
== 0)
1154 * The only state that can actually be changing concurrently with
1155 * metaslab_sync() is the metaslab's ms_map. No other thread can
1156 * be modifying this txg's allocmap, freemap, freed_map, or smo.
1157 * Therefore, we only hold ms_lock to satify space_map ASSERTs.
1158 * We drop it whenever we call into the DMU, because the DMU
1159 * can call down to us (e.g. via zio_free()) at any time.
1162 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
), txg
);
1164 if (smo
->smo_object
== 0) {
1165 ASSERT(smo
->smo_objsize
== 0);
1166 ASSERT(smo
->smo_alloc
== 0);
1167 smo
->smo_object
= dmu_object_alloc(mos
,
1168 DMU_OT_SPACE_MAP
, 1 << SPACE_MAP_BLOCKSHIFT
,
1169 DMU_OT_SPACE_MAP_HEADER
, sizeof (*smo
), tx
);
1170 ASSERT(smo
->smo_object
!= 0);
1171 dmu_write(mos
, vd
->vdev_ms_array
, sizeof (uint64_t) *
1172 (sm
->sm_start
>> vd
->vdev_ms_shift
),
1173 sizeof (uint64_t), &smo
->smo_object
, tx
);
1176 mutex_enter(&msp
->ms_lock
);
1178 if (sm
->sm_loaded
&& spa_sync_pass(spa
) == 1 &&
1179 metaslab_should_condense(msp
)) {
1180 metaslab_condense(msp
, txg
, tx
);
1182 space_map_sync(allocmap
, SM_ALLOC
, smo
, mos
, tx
);
1183 space_map_sync(*freemap
, SM_FREE
, smo
, mos
, tx
);
1186 space_map_vacate(allocmap
, NULL
, NULL
);
1189 * For sync pass 1, we avoid walking the entire space map and
1190 * instead will just swap the pointers for freemap and
1191 * freed_map. We can safely do this since the freed_map is
1192 * guaranteed to be empty on the initial pass.
1194 if (spa_sync_pass(spa
) == 1) {
1195 ASSERT0((*freed_map
)->sm_space
);
1196 ASSERT0(avl_numnodes(&(*freed_map
)->sm_root
));
1197 space_map_swap(freemap
, freed_map
);
1199 space_map_vacate(*freemap
, space_map_add
, *freed_map
);
1202 ASSERT0(msp
->ms_allocmap
[txg
& TXG_MASK
]->sm_space
);
1203 ASSERT0(msp
->ms_freemap
[txg
& TXG_MASK
]->sm_space
);
1205 mutex_exit(&msp
->ms_lock
);
1207 VERIFY0(dmu_bonus_hold(mos
, smo
->smo_object
, FTAG
, &db
));
1208 dmu_buf_will_dirty(db
, tx
);
1209 ASSERT3U(db
->db_size
, >=, sizeof (*smo
));
1210 bcopy(smo
, db
->db_data
, sizeof (*smo
));
1211 dmu_buf_rele(db
, FTAG
);
1217 * Called after a transaction group has completely synced to mark
1218 * all of the metaslab's free space as usable.
1221 metaslab_sync_done(metaslab_t
*msp
, uint64_t txg
)
1223 space_map_obj_t
*smo
= &msp
->ms_smo
;
1224 space_map_obj_t
*smosync
= &msp
->ms_smo_syncing
;
1225 space_map_t
*sm
= msp
->ms_map
;
1226 space_map_t
**freed_map
= &msp
->ms_freemap
[TXG_CLEAN(txg
) & TXG_MASK
];
1227 space_map_t
**defer_map
= &msp
->ms_defermap
[txg
% TXG_DEFER_SIZE
];
1228 metaslab_group_t
*mg
= msp
->ms_group
;
1229 vdev_t
*vd
= mg
->mg_vd
;
1230 int64_t alloc_delta
, defer_delta
;
1233 ASSERT(!vd
->vdev_ishole
);
1235 mutex_enter(&msp
->ms_lock
);
1238 * If this metaslab is just becoming available, initialize its
1239 * allocmaps, freemaps, and defermap and add its capacity to the vdev.
1241 if (*freed_map
== NULL
) {
1242 ASSERT(*defer_map
== NULL
);
1243 for (t
= 0; t
< TXG_SIZE
; t
++) {
1244 msp
->ms_allocmap
[t
] = kmem_zalloc(sizeof (space_map_t
),
1246 space_map_create(msp
->ms_allocmap
[t
], sm
->sm_start
,
1247 sm
->sm_size
, sm
->sm_shift
, sm
->sm_lock
);
1248 msp
->ms_freemap
[t
] = kmem_zalloc(sizeof (space_map_t
),
1250 space_map_create(msp
->ms_freemap
[t
], sm
->sm_start
,
1251 sm
->sm_size
, sm
->sm_shift
, sm
->sm_lock
);
1254 for (t
= 0; t
< TXG_DEFER_SIZE
; t
++) {
1255 msp
->ms_defermap
[t
] = kmem_zalloc(sizeof (space_map_t
),
1257 space_map_create(msp
->ms_defermap
[t
], sm
->sm_start
,
1258 sm
->sm_size
, sm
->sm_shift
, sm
->sm_lock
);
1261 freed_map
= &msp
->ms_freemap
[TXG_CLEAN(txg
) & TXG_MASK
];
1262 defer_map
= &msp
->ms_defermap
[txg
% TXG_DEFER_SIZE
];
1264 vdev_space_update(vd
, 0, 0, sm
->sm_size
);
1267 alloc_delta
= smosync
->smo_alloc
- smo
->smo_alloc
;
1268 defer_delta
= (*freed_map
)->sm_space
- (*defer_map
)->sm_space
;
1270 vdev_space_update(vd
, alloc_delta
+ defer_delta
, defer_delta
, 0);
1272 ASSERT(msp
->ms_allocmap
[txg
& TXG_MASK
]->sm_space
== 0);
1273 ASSERT(msp
->ms_freemap
[txg
& TXG_MASK
]->sm_space
== 0);
1276 * If there's a space_map_load() in progress, wait for it to complete
1277 * so that we have a consistent view of the in-core space map.
1279 space_map_load_wait(sm
);
1282 * Move the frees from the defer_map to this map (if it's loaded).
1283 * Swap the freed_map and the defer_map -- this is safe to do
1284 * because we've just emptied out the defer_map.
1286 space_map_vacate(*defer_map
, sm
->sm_loaded
? space_map_free
: NULL
, sm
);
1287 ASSERT0((*defer_map
)->sm_space
);
1288 ASSERT0(avl_numnodes(&(*defer_map
)->sm_root
));
1289 space_map_swap(freed_map
, defer_map
);
1293 msp
->ms_deferspace
+= defer_delta
;
1294 ASSERT3S(msp
->ms_deferspace
, >=, 0);
1295 ASSERT3S(msp
->ms_deferspace
, <=, sm
->sm_size
);
1296 if (msp
->ms_deferspace
!= 0) {
1298 * Keep syncing this metaslab until all deferred frees
1299 * are back in circulation.
1301 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
+ 1);
1305 * If the map is loaded but no longer active, evict it as soon as all
1306 * future allocations have synced. (If we unloaded it now and then
1307 * loaded a moment later, the map wouldn't reflect those allocations.)
1309 if (sm
->sm_loaded
&& (msp
->ms_weight
& METASLAB_ACTIVE_MASK
) == 0) {
1312 for (t
= 1; t
< TXG_CONCURRENT_STATES
; t
++)
1313 if (msp
->ms_allocmap
[(txg
+ t
) & TXG_MASK
]->sm_space
)
1316 if (evictable
&& !metaslab_debug
)
1317 space_map_unload(sm
);
1320 metaslab_group_sort(mg
, msp
, metaslab_weight(msp
));
1322 mutex_exit(&msp
->ms_lock
);
1326 metaslab_sync_reassess(metaslab_group_t
*mg
)
1328 vdev_t
*vd
= mg
->mg_vd
;
1329 int64_t failures
= mg
->mg_alloc_failures
;
1333 * Re-evaluate all metaslabs which have lower offsets than the
1336 for (m
= 0; m
< vd
->vdev_ms_count
; m
++) {
1337 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1339 if (msp
->ms_map
->sm_start
> mg
->mg_bonus_area
)
1342 mutex_enter(&msp
->ms_lock
);
1343 metaslab_group_sort(mg
, msp
, metaslab_weight(msp
));
1344 mutex_exit(&msp
->ms_lock
);
1347 atomic_add_64(&mg
->mg_alloc_failures
, -failures
);
1350 * Prefetch the next potential metaslabs
1352 metaslab_prefetch(mg
);
1356 metaslab_distance(metaslab_t
*msp
, dva_t
*dva
)
1358 uint64_t ms_shift
= msp
->ms_group
->mg_vd
->vdev_ms_shift
;
1359 uint64_t offset
= DVA_GET_OFFSET(dva
) >> ms_shift
;
1360 uint64_t start
= msp
->ms_map
->sm_start
>> ms_shift
;
1362 if (msp
->ms_group
->mg_vd
->vdev_id
!= DVA_GET_VDEV(dva
))
1363 return (1ULL << 63);
1366 return ((start
- offset
) << ms_shift
);
1368 return ((offset
- start
) << ms_shift
);
1373 metaslab_group_alloc(metaslab_group_t
*mg
, uint64_t psize
, uint64_t asize
,
1374 uint64_t txg
, uint64_t min_distance
, dva_t
*dva
, int d
, int flags
)
1376 spa_t
*spa
= mg
->mg_vd
->vdev_spa
;
1377 metaslab_t
*msp
= NULL
;
1378 uint64_t offset
= -1ULL;
1379 avl_tree_t
*t
= &mg
->mg_metaslab_tree
;
1380 uint64_t activation_weight
;
1381 uint64_t target_distance
;
1384 activation_weight
= METASLAB_WEIGHT_PRIMARY
;
1385 for (i
= 0; i
< d
; i
++) {
1386 if (DVA_GET_VDEV(&dva
[i
]) == mg
->mg_vd
->vdev_id
) {
1387 activation_weight
= METASLAB_WEIGHT_SECONDARY
;
1393 boolean_t was_active
;
1395 mutex_enter(&mg
->mg_lock
);
1396 for (msp
= avl_first(t
); msp
; msp
= AVL_NEXT(t
, msp
)) {
1397 if (msp
->ms_weight
< asize
) {
1398 spa_dbgmsg(spa
, "%s: failed to meet weight "
1399 "requirement: vdev %llu, txg %llu, mg %p, "
1400 "msp %p, psize %llu, asize %llu, "
1401 "failures %llu, weight %llu",
1402 spa_name(spa
), mg
->mg_vd
->vdev_id
, txg
,
1403 mg
, msp
, psize
, asize
,
1404 mg
->mg_alloc_failures
, msp
->ms_weight
);
1405 mutex_exit(&mg
->mg_lock
);
1410 * If the selected metaslab is condensing, skip it.
1412 if (msp
->ms_map
->sm_condensing
)
1415 was_active
= msp
->ms_weight
& METASLAB_ACTIVE_MASK
;
1416 if (activation_weight
== METASLAB_WEIGHT_PRIMARY
)
1419 target_distance
= min_distance
+
1420 (msp
->ms_smo
.smo_alloc
? 0 : min_distance
>> 1);
1422 for (i
= 0; i
< d
; i
++)
1423 if (metaslab_distance(msp
, &dva
[i
]) <
1429 mutex_exit(&mg
->mg_lock
);
1434 * If we've already reached the allowable number of failed
1435 * allocation attempts on this metaslab group then we
1436 * consider skipping it. We skip it only if we're allowed
1437 * to "fast" gang, the physical size is larger than
1438 * a gang block, and we're attempting to allocate from
1439 * the primary metaslab.
1441 if (mg
->mg_alloc_failures
> zfs_mg_alloc_failures
&&
1442 CAN_FASTGANG(flags
) && psize
> SPA_GANGBLOCKSIZE
&&
1443 activation_weight
== METASLAB_WEIGHT_PRIMARY
) {
1444 spa_dbgmsg(spa
, "%s: skipping metaslab group: "
1445 "vdev %llu, txg %llu, mg %p, psize %llu, "
1446 "asize %llu, failures %llu", spa_name(spa
),
1447 mg
->mg_vd
->vdev_id
, txg
, mg
, psize
, asize
,
1448 mg
->mg_alloc_failures
);
1452 mutex_enter(&msp
->ms_lock
);
1455 * Ensure that the metaslab we have selected is still
1456 * capable of handling our request. It's possible that
1457 * another thread may have changed the weight while we
1458 * were blocked on the metaslab lock.
1460 if (msp
->ms_weight
< asize
|| (was_active
&&
1461 !(msp
->ms_weight
& METASLAB_ACTIVE_MASK
) &&
1462 activation_weight
== METASLAB_WEIGHT_PRIMARY
)) {
1463 mutex_exit(&msp
->ms_lock
);
1467 if ((msp
->ms_weight
& METASLAB_WEIGHT_SECONDARY
) &&
1468 activation_weight
== METASLAB_WEIGHT_PRIMARY
) {
1469 metaslab_passivate(msp
,
1470 msp
->ms_weight
& ~METASLAB_ACTIVE_MASK
);
1471 mutex_exit(&msp
->ms_lock
);
1475 if (metaslab_activate(msp
, activation_weight
) != 0) {
1476 mutex_exit(&msp
->ms_lock
);
1481 * If this metaslab is currently condensing then pick again as
1482 * we can't manipulate this metaslab until it's committed
1485 if (msp
->ms_map
->sm_condensing
) {
1486 mutex_exit(&msp
->ms_lock
);
1490 if ((offset
= space_map_alloc(msp
->ms_map
, asize
)) != -1ULL)
1493 atomic_inc_64(&mg
->mg_alloc_failures
);
1495 metaslab_passivate(msp
, space_map_maxsize(msp
->ms_map
));
1497 mutex_exit(&msp
->ms_lock
);
1500 if (msp
->ms_allocmap
[txg
& TXG_MASK
]->sm_space
== 0)
1501 vdev_dirty(mg
->mg_vd
, VDD_METASLAB
, msp
, txg
);
1503 space_map_add(msp
->ms_allocmap
[txg
& TXG_MASK
], offset
, asize
);
1505 mutex_exit(&msp
->ms_lock
);
1511 * Allocate a block for the specified i/o.
1514 metaslab_alloc_dva(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
,
1515 dva_t
*dva
, int d
, dva_t
*hintdva
, uint64_t txg
, int flags
)
1517 metaslab_group_t
*mg
, *fast_mg
, *rotor
;
1521 int zio_lock
= B_FALSE
;
1522 boolean_t allocatable
;
1523 uint64_t offset
= -1ULL;
1527 ASSERT(!DVA_IS_VALID(&dva
[d
]));
1530 * For testing, make some blocks above a certain size be gang blocks.
1532 if (psize
>= metaslab_gang_bang
&& (ddi_get_lbolt() & 3) == 0)
1533 return (SET_ERROR(ENOSPC
));
1535 if (flags
& METASLAB_FASTWRITE
)
1536 mutex_enter(&mc
->mc_fastwrite_lock
);
1539 * Start at the rotor and loop through all mgs until we find something.
1540 * Note that there's no locking on mc_rotor or mc_aliquot because
1541 * nothing actually breaks if we miss a few updates -- we just won't
1542 * allocate quite as evenly. It all balances out over time.
1544 * If we are doing ditto or log blocks, try to spread them across
1545 * consecutive vdevs. If we're forced to reuse a vdev before we've
1546 * allocated all of our ditto blocks, then try and spread them out on
1547 * that vdev as much as possible. If it turns out to not be possible,
1548 * gradually lower our standards until anything becomes acceptable.
1549 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
1550 * gives us hope of containing our fault domains to something we're
1551 * able to reason about. Otherwise, any two top-level vdev failures
1552 * will guarantee the loss of data. With consecutive allocation,
1553 * only two adjacent top-level vdev failures will result in data loss.
1555 * If we are doing gang blocks (hintdva is non-NULL), try to keep
1556 * ourselves on the same vdev as our gang block header. That
1557 * way, we can hope for locality in vdev_cache, plus it makes our
1558 * fault domains something tractable.
1561 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&hintdva
[d
]));
1564 * It's possible the vdev we're using as the hint no
1565 * longer exists (i.e. removed). Consult the rotor when
1571 if (flags
& METASLAB_HINTBP_AVOID
&&
1572 mg
->mg_next
!= NULL
)
1577 } else if (d
!= 0) {
1578 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
- 1]));
1579 mg
= vd
->vdev_mg
->mg_next
;
1580 } else if (flags
& METASLAB_FASTWRITE
) {
1581 mg
= fast_mg
= mc
->mc_rotor
;
1584 if (fast_mg
->mg_vd
->vdev_pending_fastwrite
<
1585 mg
->mg_vd
->vdev_pending_fastwrite
)
1587 } while ((fast_mg
= fast_mg
->mg_next
) != mc
->mc_rotor
);
1594 * If the hint put us into the wrong metaslab class, or into a
1595 * metaslab group that has been passivated, just follow the rotor.
1597 if (mg
->mg_class
!= mc
|| mg
->mg_activation_count
<= 0)
1604 ASSERT(mg
->mg_activation_count
== 1);
1609 * Don't allocate from faulted devices.
1612 spa_config_enter(spa
, SCL_ZIO
, FTAG
, RW_READER
);
1613 allocatable
= vdev_allocatable(vd
);
1614 spa_config_exit(spa
, SCL_ZIO
, FTAG
);
1616 allocatable
= vdev_allocatable(vd
);
1622 * Avoid writing single-copy data to a failing vdev
1624 if ((vd
->vdev_stat
.vs_write_errors
> 0 ||
1625 vd
->vdev_state
< VDEV_STATE_HEALTHY
) &&
1626 d
== 0 && dshift
== 3) {
1631 ASSERT(mg
->mg_class
== mc
);
1633 distance
= vd
->vdev_asize
>> dshift
;
1634 if (distance
<= (1ULL << vd
->vdev_ms_shift
))
1639 asize
= vdev_psize_to_asize(vd
, psize
);
1640 ASSERT(P2PHASE(asize
, 1ULL << vd
->vdev_ashift
) == 0);
1642 offset
= metaslab_group_alloc(mg
, psize
, asize
, txg
, distance
,
1644 if (offset
!= -1ULL) {
1646 * If we've just selected this metaslab group,
1647 * figure out whether the corresponding vdev is
1648 * over- or under-used relative to the pool,
1649 * and set an allocation bias to even it out.
1651 if (mc
->mc_aliquot
== 0) {
1652 vdev_stat_t
*vs
= &vd
->vdev_stat
;
1655 vu
= (vs
->vs_alloc
* 100) / (vs
->vs_space
+ 1);
1656 cu
= (mc
->mc_alloc
* 100) / (mc
->mc_space
+ 1);
1659 * Calculate how much more or less we should
1660 * try to allocate from this device during
1661 * this iteration around the rotor.
1662 * For example, if a device is 80% full
1663 * and the pool is 20% full then we should
1664 * reduce allocations by 60% on this device.
1666 * mg_bias = (20 - 80) * 512K / 100 = -307K
1668 * This reduces allocations by 307K for this
1671 mg
->mg_bias
= ((cu
- vu
) *
1672 (int64_t)mg
->mg_aliquot
) / 100;
1675 if ((flags
& METASLAB_FASTWRITE
) ||
1676 atomic_add_64_nv(&mc
->mc_aliquot
, asize
) >=
1677 mg
->mg_aliquot
+ mg
->mg_bias
) {
1678 mc
->mc_rotor
= mg
->mg_next
;
1682 DVA_SET_VDEV(&dva
[d
], vd
->vdev_id
);
1683 DVA_SET_OFFSET(&dva
[d
], offset
);
1684 DVA_SET_GANG(&dva
[d
], !!(flags
& METASLAB_GANG_HEADER
));
1685 DVA_SET_ASIZE(&dva
[d
], asize
);
1687 if (flags
& METASLAB_FASTWRITE
) {
1688 atomic_add_64(&vd
->vdev_pending_fastwrite
,
1690 mutex_exit(&mc
->mc_fastwrite_lock
);
1696 mc
->mc_rotor
= mg
->mg_next
;
1698 } while ((mg
= mg
->mg_next
) != rotor
);
1702 ASSERT(dshift
< 64);
1706 if (!allocatable
&& !zio_lock
) {
1712 bzero(&dva
[d
], sizeof (dva_t
));
1714 if (flags
& METASLAB_FASTWRITE
)
1715 mutex_exit(&mc
->mc_fastwrite_lock
);
1717 return (SET_ERROR(ENOSPC
));
1721 * Free the block represented by DVA in the context of the specified
1722 * transaction group.
1725 metaslab_free_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
, boolean_t now
)
1727 uint64_t vdev
= DVA_GET_VDEV(dva
);
1728 uint64_t offset
= DVA_GET_OFFSET(dva
);
1729 uint64_t size
= DVA_GET_ASIZE(dva
);
1733 ASSERT(DVA_IS_VALID(dva
));
1735 if (txg
> spa_freeze_txg(spa
))
1738 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
||
1739 (offset
>> vd
->vdev_ms_shift
) >= vd
->vdev_ms_count
) {
1740 cmn_err(CE_WARN
, "metaslab_free_dva(): bad DVA %llu:%llu",
1741 (u_longlong_t
)vdev
, (u_longlong_t
)offset
);
1746 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
1748 if (DVA_GET_GANG(dva
))
1749 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
1751 mutex_enter(&msp
->ms_lock
);
1754 space_map_remove(msp
->ms_allocmap
[txg
& TXG_MASK
],
1756 space_map_free(msp
->ms_map
, offset
, size
);
1758 if (msp
->ms_freemap
[txg
& TXG_MASK
]->sm_space
== 0)
1759 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
1760 space_map_add(msp
->ms_freemap
[txg
& TXG_MASK
], offset
, size
);
1763 mutex_exit(&msp
->ms_lock
);
1767 * Intent log support: upon opening the pool after a crash, notify the SPA
1768 * of blocks that the intent log has allocated for immediate write, but
1769 * which are still considered free by the SPA because the last transaction
1770 * group didn't commit yet.
1773 metaslab_claim_dva(spa_t
*spa
, const dva_t
*dva
, uint64_t txg
)
1775 uint64_t vdev
= DVA_GET_VDEV(dva
);
1776 uint64_t offset
= DVA_GET_OFFSET(dva
);
1777 uint64_t size
= DVA_GET_ASIZE(dva
);
1782 ASSERT(DVA_IS_VALID(dva
));
1784 if ((vd
= vdev_lookup_top(spa
, vdev
)) == NULL
||
1785 (offset
>> vd
->vdev_ms_shift
) >= vd
->vdev_ms_count
)
1786 return (SET_ERROR(ENXIO
));
1788 msp
= vd
->vdev_ms
[offset
>> vd
->vdev_ms_shift
];
1790 if (DVA_GET_GANG(dva
))
1791 size
= vdev_psize_to_asize(vd
, SPA_GANGBLOCKSIZE
);
1793 mutex_enter(&msp
->ms_lock
);
1795 if ((txg
!= 0 && spa_writeable(spa
)) || !msp
->ms_map
->sm_loaded
)
1796 error
= metaslab_activate(msp
, METASLAB_WEIGHT_SECONDARY
);
1798 if (error
== 0 && !space_map_contains(msp
->ms_map
, offset
, size
))
1799 error
= SET_ERROR(ENOENT
);
1801 if (error
|| txg
== 0) { /* txg == 0 indicates dry run */
1802 mutex_exit(&msp
->ms_lock
);
1806 space_map_claim(msp
->ms_map
, offset
, size
);
1808 if (spa_writeable(spa
)) { /* don't dirty if we're zdb(1M) */
1809 if (msp
->ms_allocmap
[txg
& TXG_MASK
]->sm_space
== 0)
1810 vdev_dirty(vd
, VDD_METASLAB
, msp
, txg
);
1811 space_map_add(msp
->ms_allocmap
[txg
& TXG_MASK
], offset
, size
);
1814 mutex_exit(&msp
->ms_lock
);
1820 metaslab_alloc(spa_t
*spa
, metaslab_class_t
*mc
, uint64_t psize
, blkptr_t
*bp
,
1821 int ndvas
, uint64_t txg
, blkptr_t
*hintbp
, int flags
)
1823 dva_t
*dva
= bp
->blk_dva
;
1824 dva_t
*hintdva
= hintbp
->blk_dva
;
1827 ASSERT(bp
->blk_birth
== 0);
1828 ASSERT(BP_PHYSICAL_BIRTH(bp
) == 0);
1830 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
1832 if (mc
->mc_rotor
== NULL
) { /* no vdevs in this class */
1833 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1834 return (SET_ERROR(ENOSPC
));
1837 ASSERT(ndvas
> 0 && ndvas
<= spa_max_replication(spa
));
1838 ASSERT(BP_GET_NDVAS(bp
) == 0);
1839 ASSERT(hintbp
== NULL
|| ndvas
<= BP_GET_NDVAS(hintbp
));
1841 for (d
= 0; d
< ndvas
; d
++) {
1842 error
= metaslab_alloc_dva(spa
, mc
, psize
, dva
, d
, hintdva
,
1845 for (d
--; d
>= 0; d
--) {
1846 metaslab_free_dva(spa
, &dva
[d
], txg
, B_TRUE
);
1847 bzero(&dva
[d
], sizeof (dva_t
));
1849 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1854 ASSERT(BP_GET_NDVAS(bp
) == ndvas
);
1856 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1858 BP_SET_BIRTH(bp
, txg
, txg
);
1864 metaslab_free(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
, boolean_t now
)
1866 const dva_t
*dva
= bp
->blk_dva
;
1867 int d
, ndvas
= BP_GET_NDVAS(bp
);
1869 ASSERT(!BP_IS_HOLE(bp
));
1870 ASSERT(!now
|| bp
->blk_birth
>= spa_syncing_txg(spa
));
1872 spa_config_enter(spa
, SCL_FREE
, FTAG
, RW_READER
);
1874 for (d
= 0; d
< ndvas
; d
++)
1875 metaslab_free_dva(spa
, &dva
[d
], txg
, now
);
1877 spa_config_exit(spa
, SCL_FREE
, FTAG
);
1881 metaslab_claim(spa_t
*spa
, const blkptr_t
*bp
, uint64_t txg
)
1883 const dva_t
*dva
= bp
->blk_dva
;
1884 int ndvas
= BP_GET_NDVAS(bp
);
1887 ASSERT(!BP_IS_HOLE(bp
));
1891 * First do a dry run to make sure all DVAs are claimable,
1892 * so we don't have to unwind from partial failures below.
1894 if ((error
= metaslab_claim(spa
, bp
, 0)) != 0)
1898 spa_config_enter(spa
, SCL_ALLOC
, FTAG
, RW_READER
);
1900 for (d
= 0; d
< ndvas
; d
++)
1901 if ((error
= metaslab_claim_dva(spa
, &dva
[d
], txg
)) != 0)
1904 spa_config_exit(spa
, SCL_ALLOC
, FTAG
);
1906 ASSERT(error
== 0 || txg
== 0);
1911 void metaslab_fastwrite_mark(spa_t
*spa
, const blkptr_t
*bp
)
1913 const dva_t
*dva
= bp
->blk_dva
;
1914 int ndvas
= BP_GET_NDVAS(bp
);
1915 uint64_t psize
= BP_GET_PSIZE(bp
);
1919 ASSERT(!BP_IS_HOLE(bp
));
1922 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1924 for (d
= 0; d
< ndvas
; d
++) {
1925 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
1927 atomic_add_64(&vd
->vdev_pending_fastwrite
, psize
);
1930 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1933 void metaslab_fastwrite_unmark(spa_t
*spa
, const blkptr_t
*bp
)
1935 const dva_t
*dva
= bp
->blk_dva
;
1936 int ndvas
= BP_GET_NDVAS(bp
);
1937 uint64_t psize
= BP_GET_PSIZE(bp
);
1941 ASSERT(!BP_IS_HOLE(bp
));
1944 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1946 for (d
= 0; d
< ndvas
; d
++) {
1947 if ((vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[d
]))) == NULL
)
1949 ASSERT3U(vd
->vdev_pending_fastwrite
, >=, psize
);
1950 atomic_sub_64(&vd
->vdev_pending_fastwrite
, psize
);
1953 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1957 checkmap(space_map_t
*sm
, uint64_t off
, uint64_t size
)
1962 mutex_enter(sm
->sm_lock
);
1963 ss
= space_map_find(sm
, off
, size
, &where
);
1965 panic("freeing free block; ss=%p", (void *)ss
);
1966 mutex_exit(sm
->sm_lock
);
1970 metaslab_check_free(spa_t
*spa
, const blkptr_t
*bp
)
1974 if ((zfs_flags
& ZFS_DEBUG_ZIO_FREE
) == 0)
1977 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1978 for (i
= 0; i
< BP_GET_NDVAS(bp
); i
++) {
1979 uint64_t vdid
= DVA_GET_VDEV(&bp
->blk_dva
[i
]);
1980 vdev_t
*vd
= vdev_lookup_top(spa
, vdid
);
1981 uint64_t off
= DVA_GET_OFFSET(&bp
->blk_dva
[i
]);
1982 uint64_t size
= DVA_GET_ASIZE(&bp
->blk_dva
[i
]);
1983 metaslab_t
*ms
= vd
->vdev_ms
[off
>> vd
->vdev_ms_shift
];
1985 if (ms
->ms_map
->sm_loaded
)
1986 checkmap(ms
->ms_map
, off
, size
);
1988 for (j
= 0; j
< TXG_SIZE
; j
++)
1989 checkmap(ms
->ms_freemap
[j
], off
, size
);
1990 for (j
= 0; j
< TXG_DEFER_SIZE
; j
++)
1991 checkmap(ms
->ms_defermap
[j
], off
, size
);
1993 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1996 #if defined(_KERNEL) && defined(HAVE_SPL)
1997 module_param(metaslab_debug
, int, 0644);
1998 MODULE_PARM_DESC(metaslab_debug
, "keep space maps in core to verify frees");
1999 #endif /* _KERNEL && HAVE_SPL */