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) 2012 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/space_map.h>
30 #include <sys/metaslab_impl.h>
31 #include <sys/vdev_impl.h>
34 #define WITH_DF_BLOCK_ALLOCATOR
37 * Allow allocations to switch to gang blocks quickly. We do this to
38 * avoid having to load lots of space_maps in a given txg. There are,
39 * however, some cases where we want to avoid "fast" ganging and instead
40 * we want to do an exhaustive search of all metaslabs on this device.
41 * Currently we don't allow any gang, zil, or dump device related allocations
44 #define CAN_FASTGANG(flags) \
45 (!((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER | \
46 METASLAB_GANG_AVOID)))
48 uint64_t metaslab_aliquot = 512ULL << 10;
49 uint64_t metaslab_gang_bang = SPA_MAXBLOCKSIZE + 1; /* force gang blocks */
52 * This value defines the number of allowed allocation failures per vdev.
53 * If a device reaches this threshold in a given txg then we consider skipping
54 * allocations on that device.
56 int zfs_mg_alloc_failures;
59 * Metaslab debugging: when set, keeps all space maps in core to verify frees.
61 static int metaslab_debug = 0;
64 * Minimum size which forces the dynamic allocator to change
65 * it's allocation strategy. Once the space map cannot satisfy
66 * an allocation of this size then it switches to using more
67 * aggressive strategy (i.e search by size rather than offset).
69 uint64_t metaslab_df_alloc_threshold = SPA_MAXBLOCKSIZE;
72 * The minimum free space, in percent, which must be available
73 * in a space map to continue allocations in a first-fit fashion.
74 * Once the space_map's free space drops below this level we dynamically
75 * switch to using best-fit allocations.
77 int metaslab_df_free_pct = 4;
80 * A metaslab is considered "free" if it contains a contiguous
81 * segment which is greater than metaslab_min_alloc_size.
83 uint64_t metaslab_min_alloc_size = DMU_MAX_ACCESS;
86 * Max number of space_maps to prefetch.
88 int metaslab_prefetch_limit = SPA_DVAS_PER_BP;
91 * Percentage bonus multiplier for metaslabs that are in the bonus area.
93 int metaslab_smo_bonus_pct = 150;
96 * ==========================================================================
98 * ==========================================================================
101 metaslab_class_create(spa_t *spa, space_map_ops_t *ops)
103 metaslab_class_t *mc;
105 mc = kmem_zalloc(sizeof (metaslab_class_t), KM_SLEEP);
115 metaslab_class_destroy(metaslab_class_t *mc)
117 ASSERT(mc->mc_rotor == NULL);
118 ASSERT(mc->mc_alloc == 0);
119 ASSERT(mc->mc_deferred == 0);
120 ASSERT(mc->mc_space == 0);
121 ASSERT(mc->mc_dspace == 0);
123 kmem_free(mc, sizeof (metaslab_class_t));
127 metaslab_class_validate(metaslab_class_t *mc)
129 metaslab_group_t *mg;
133 * Must hold one of the spa_config locks.
135 ASSERT(spa_config_held(mc->mc_spa, SCL_ALL, RW_READER) ||
136 spa_config_held(mc->mc_spa, SCL_ALL, RW_WRITER));
138 if ((mg = mc->mc_rotor) == NULL)
143 ASSERT(vd->vdev_mg != NULL);
144 ASSERT3P(vd->vdev_top, ==, vd);
145 ASSERT3P(mg->mg_class, ==, mc);
146 ASSERT3P(vd->vdev_ops, !=, &vdev_hole_ops);
147 } while ((mg = mg->mg_next) != mc->mc_rotor);
153 metaslab_class_space_update(metaslab_class_t *mc, int64_t alloc_delta,
154 int64_t defer_delta, int64_t space_delta, int64_t dspace_delta)
156 atomic_add_64(&mc->mc_alloc, alloc_delta);
157 atomic_add_64(&mc->mc_deferred, defer_delta);
158 atomic_add_64(&mc->mc_space, space_delta);
159 atomic_add_64(&mc->mc_dspace, dspace_delta);
163 metaslab_class_get_alloc(metaslab_class_t *mc)
165 return (mc->mc_alloc);
169 metaslab_class_get_deferred(metaslab_class_t *mc)
171 return (mc->mc_deferred);
175 metaslab_class_get_space(metaslab_class_t *mc)
177 return (mc->mc_space);
181 metaslab_class_get_dspace(metaslab_class_t *mc)
183 return (spa_deflate(mc->mc_spa) ? mc->mc_dspace : mc->mc_space);
187 * ==========================================================================
189 * ==========================================================================
192 metaslab_compare(const void *x1, const void *x2)
194 const metaslab_t *m1 = x1;
195 const metaslab_t *m2 = x2;
197 if (m1->ms_weight < m2->ms_weight)
199 if (m1->ms_weight > m2->ms_weight)
203 * If the weights are identical, use the offset to force uniqueness.
205 if (m1->ms_map.sm_start < m2->ms_map.sm_start)
207 if (m1->ms_map.sm_start > m2->ms_map.sm_start)
210 ASSERT3P(m1, ==, m2);
216 metaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
218 metaslab_group_t *mg;
220 mg = kmem_zalloc(sizeof (metaslab_group_t), KM_SLEEP);
221 mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
222 avl_create(&mg->mg_metaslab_tree, metaslab_compare,
223 sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
226 mg->mg_activation_count = 0;
232 metaslab_group_destroy(metaslab_group_t *mg)
234 ASSERT(mg->mg_prev == NULL);
235 ASSERT(mg->mg_next == NULL);
237 * We may have gone below zero with the activation count
238 * either because we never activated in the first place or
239 * because we're done, and possibly removing the vdev.
241 ASSERT(mg->mg_activation_count <= 0);
243 avl_destroy(&mg->mg_metaslab_tree);
244 mutex_destroy(&mg->mg_lock);
245 kmem_free(mg, sizeof (metaslab_group_t));
249 metaslab_group_activate(metaslab_group_t *mg)
251 metaslab_class_t *mc = mg->mg_class;
252 metaslab_group_t *mgprev, *mgnext;
254 ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
256 ASSERT(mc->mc_rotor != mg);
257 ASSERT(mg->mg_prev == NULL);
258 ASSERT(mg->mg_next == NULL);
259 ASSERT(mg->mg_activation_count <= 0);
261 if (++mg->mg_activation_count <= 0)
264 mg->mg_aliquot = metaslab_aliquot * MAX(1, mg->mg_vd->vdev_children);
266 if ((mgprev = mc->mc_rotor) == NULL) {
270 mgnext = mgprev->mg_next;
271 mg->mg_prev = mgprev;
272 mg->mg_next = mgnext;
273 mgprev->mg_next = mg;
274 mgnext->mg_prev = mg;
280 metaslab_group_passivate(metaslab_group_t *mg)
282 metaslab_class_t *mc = mg->mg_class;
283 metaslab_group_t *mgprev, *mgnext;
285 ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
287 if (--mg->mg_activation_count != 0) {
288 ASSERT(mc->mc_rotor != mg);
289 ASSERT(mg->mg_prev == NULL);
290 ASSERT(mg->mg_next == NULL);
291 ASSERT(mg->mg_activation_count < 0);
295 mgprev = mg->mg_prev;
296 mgnext = mg->mg_next;
301 mc->mc_rotor = mgnext;
302 mgprev->mg_next = mgnext;
303 mgnext->mg_prev = mgprev;
311 metaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
313 mutex_enter(&mg->mg_lock);
314 ASSERT(msp->ms_group == NULL);
317 avl_add(&mg->mg_metaslab_tree, msp);
318 mutex_exit(&mg->mg_lock);
322 metaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
324 mutex_enter(&mg->mg_lock);
325 ASSERT(msp->ms_group == mg);
326 avl_remove(&mg->mg_metaslab_tree, msp);
327 msp->ms_group = NULL;
328 mutex_exit(&mg->mg_lock);
332 metaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
335 * Although in principle the weight can be any value, in
336 * practice we do not use values in the range [1, 510].
338 ASSERT(weight >= SPA_MINBLOCKSIZE-1 || weight == 0);
339 ASSERT(MUTEX_HELD(&msp->ms_lock));
341 mutex_enter(&mg->mg_lock);
342 ASSERT(msp->ms_group == mg);
343 avl_remove(&mg->mg_metaslab_tree, msp);
344 msp->ms_weight = weight;
345 avl_add(&mg->mg_metaslab_tree, msp);
346 mutex_exit(&mg->mg_lock);
350 * ==========================================================================
351 * Common allocator routines
352 * ==========================================================================
355 metaslab_segsize_compare(const void *x1, const void *x2)
357 const space_seg_t *s1 = x1;
358 const space_seg_t *s2 = x2;
359 uint64_t ss_size1 = s1->ss_end - s1->ss_start;
360 uint64_t ss_size2 = s2->ss_end - s2->ss_start;
362 if (ss_size1 < ss_size2)
364 if (ss_size1 > ss_size2)
367 if (s1->ss_start < s2->ss_start)
369 if (s1->ss_start > s2->ss_start)
375 #if defined(WITH_FF_BLOCK_ALLOCATOR) || \
376 defined(WITH_DF_BLOCK_ALLOCATOR) || \
377 defined(WITH_CDF_BLOCK_ALLOCATOR)
379 * This is a helper function that can be used by the allocator to find
380 * a suitable block to allocate. This will search the specified AVL
381 * tree looking for a block that matches the specified criteria.
384 metaslab_block_picker(avl_tree_t *t, uint64_t *cursor, uint64_t size,
387 space_seg_t *ss, ssearch;
390 ssearch.ss_start = *cursor;
391 ssearch.ss_end = *cursor + size;
393 ss = avl_find(t, &ssearch, &where);
395 ss = avl_nearest(t, where, AVL_AFTER);
398 uint64_t offset = P2ROUNDUP(ss->ss_start, align);
400 if (offset + size <= ss->ss_end) {
401 *cursor = offset + size;
404 ss = AVL_NEXT(t, ss);
408 * If we know we've searched the whole map (*cursor == 0), give up.
409 * Otherwise, reset the cursor to the beginning and try again.
415 return (metaslab_block_picker(t, cursor, size, align));
417 #endif /* WITH_FF/DF/CDF_BLOCK_ALLOCATOR */
420 metaslab_pp_load(space_map_t *sm)
424 ASSERT(sm->sm_ppd == NULL);
425 sm->sm_ppd = kmem_zalloc(64 * sizeof (uint64_t), KM_SLEEP);
427 sm->sm_pp_root = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
428 avl_create(sm->sm_pp_root, metaslab_segsize_compare,
429 sizeof (space_seg_t), offsetof(struct space_seg, ss_pp_node));
431 for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
432 avl_add(sm->sm_pp_root, ss);
436 metaslab_pp_unload(space_map_t *sm)
440 kmem_free(sm->sm_ppd, 64 * sizeof (uint64_t));
443 while (avl_destroy_nodes(sm->sm_pp_root, &cookie) != NULL) {
444 /* tear down the tree */
447 avl_destroy(sm->sm_pp_root);
448 kmem_free(sm->sm_pp_root, sizeof (avl_tree_t));
449 sm->sm_pp_root = NULL;
454 metaslab_pp_claim(space_map_t *sm, uint64_t start, uint64_t size)
456 /* No need to update cursor */
461 metaslab_pp_free(space_map_t *sm, uint64_t start, uint64_t size)
463 /* No need to update cursor */
467 * Return the maximum contiguous segment within the metaslab.
470 metaslab_pp_maxsize(space_map_t *sm)
472 avl_tree_t *t = sm->sm_pp_root;
475 if (t == NULL || (ss = avl_last(t)) == NULL)
478 return (ss->ss_end - ss->ss_start);
481 #if defined(WITH_FF_BLOCK_ALLOCATOR)
483 * ==========================================================================
484 * The first-fit block allocator
485 * ==========================================================================
488 metaslab_ff_alloc(space_map_t *sm, uint64_t size)
490 avl_tree_t *t = &sm->sm_root;
491 uint64_t align = size & -size;
492 uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
494 return (metaslab_block_picker(t, cursor, size, align));
499 metaslab_ff_fragmented(space_map_t *sm)
504 static space_map_ops_t metaslab_ff_ops = {
511 metaslab_ff_fragmented
514 space_map_ops_t *zfs_metaslab_ops = &metaslab_ff_ops;
515 #endif /* WITH_FF_BLOCK_ALLOCATOR */
517 #if defined(WITH_DF_BLOCK_ALLOCATOR)
519 * ==========================================================================
520 * Dynamic block allocator -
521 * Uses the first fit allocation scheme until space get low and then
522 * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
523 * and metaslab_df_free_pct to determine when to switch the allocation scheme.
524 * ==========================================================================
527 metaslab_df_alloc(space_map_t *sm, uint64_t size)
529 avl_tree_t *t = &sm->sm_root;
530 uint64_t align = size & -size;
531 uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
532 uint64_t max_size = metaslab_pp_maxsize(sm);
533 int free_pct = sm->sm_space * 100 / sm->sm_size;
535 ASSERT(MUTEX_HELD(sm->sm_lock));
536 ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
542 * If we're running low on space switch to using the size
543 * sorted AVL tree (best-fit).
545 if (max_size < metaslab_df_alloc_threshold ||
546 free_pct < metaslab_df_free_pct) {
551 return (metaslab_block_picker(t, cursor, size, 1ULL));
555 metaslab_df_fragmented(space_map_t *sm)
557 uint64_t max_size = metaslab_pp_maxsize(sm);
558 int free_pct = sm->sm_space * 100 / sm->sm_size;
560 if (max_size >= metaslab_df_alloc_threshold &&
561 free_pct >= metaslab_df_free_pct)
567 static space_map_ops_t metaslab_df_ops = {
574 metaslab_df_fragmented
577 space_map_ops_t *zfs_metaslab_ops = &metaslab_df_ops;
578 #endif /* WITH_DF_BLOCK_ALLOCATOR */
581 * ==========================================================================
582 * Other experimental allocators
583 * ==========================================================================
585 #if defined(WITH_CDF_BLOCK_ALLOCATOR)
587 metaslab_cdf_alloc(space_map_t *sm, uint64_t size)
589 avl_tree_t *t = &sm->sm_root;
590 uint64_t *cursor = (uint64_t *)sm->sm_ppd;
591 uint64_t *extent_end = (uint64_t *)sm->sm_ppd + 1;
592 uint64_t max_size = metaslab_pp_maxsize(sm);
593 uint64_t rsize = size;
596 ASSERT(MUTEX_HELD(sm->sm_lock));
597 ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
602 ASSERT3U(*extent_end, >=, *cursor);
605 * If we're running low on space switch to using the size
606 * sorted AVL tree (best-fit).
608 if ((*cursor + size) > *extent_end) {
611 *cursor = *extent_end = 0;
613 if (max_size > 2 * SPA_MAXBLOCKSIZE)
614 rsize = MIN(metaslab_min_alloc_size, max_size);
615 offset = metaslab_block_picker(t, extent_end, rsize, 1ULL);
617 *cursor = offset + size;
619 offset = metaslab_block_picker(t, cursor, rsize, 1ULL);
621 ASSERT3U(*cursor, <=, *extent_end);
626 metaslab_cdf_fragmented(space_map_t *sm)
628 uint64_t max_size = metaslab_pp_maxsize(sm);
630 if (max_size > (metaslab_min_alloc_size * 10))
635 static space_map_ops_t metaslab_cdf_ops = {
642 metaslab_cdf_fragmented
645 space_map_ops_t *zfs_metaslab_ops = &metaslab_cdf_ops;
646 #endif /* WITH_CDF_BLOCK_ALLOCATOR */
648 #if defined(WITH_NDF_BLOCK_ALLOCATOR)
649 uint64_t metaslab_ndf_clump_shift = 4;
652 metaslab_ndf_alloc(space_map_t *sm, uint64_t size)
654 avl_tree_t *t = &sm->sm_root;
656 space_seg_t *ss, ssearch;
657 uint64_t hbit = highbit(size);
658 uint64_t *cursor = (uint64_t *)sm->sm_ppd + hbit - 1;
659 uint64_t max_size = metaslab_pp_maxsize(sm);
661 ASSERT(MUTEX_HELD(sm->sm_lock));
662 ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
667 ssearch.ss_start = *cursor;
668 ssearch.ss_end = *cursor + size;
670 ss = avl_find(t, &ssearch, &where);
671 if (ss == NULL || (ss->ss_start + size > ss->ss_end)) {
674 ssearch.ss_start = 0;
675 ssearch.ss_end = MIN(max_size,
676 1ULL << (hbit + metaslab_ndf_clump_shift));
677 ss = avl_find(t, &ssearch, &where);
679 ss = avl_nearest(t, where, AVL_AFTER);
684 if (ss->ss_start + size <= ss->ss_end) {
685 *cursor = ss->ss_start + size;
686 return (ss->ss_start);
693 metaslab_ndf_fragmented(space_map_t *sm)
695 uint64_t max_size = metaslab_pp_maxsize(sm);
697 if (max_size > (metaslab_min_alloc_size << metaslab_ndf_clump_shift))
703 static space_map_ops_t metaslab_ndf_ops = {
710 metaslab_ndf_fragmented
713 space_map_ops_t *zfs_metaslab_ops = &metaslab_ndf_ops;
714 #endif /* WITH_NDF_BLOCK_ALLOCATOR */
717 * ==========================================================================
719 * ==========================================================================
722 metaslab_init(metaslab_group_t *mg, space_map_obj_t *smo,
723 uint64_t start, uint64_t size, uint64_t txg)
725 vdev_t *vd = mg->mg_vd;
728 msp = kmem_zalloc(sizeof (metaslab_t), KM_SLEEP);
729 mutex_init(&msp->ms_lock, NULL, MUTEX_DEFAULT, NULL);
731 msp->ms_smo_syncing = *smo;
734 * We create the main space map here, but we don't create the
735 * allocmaps and freemaps until metaslab_sync_done(). This serves
736 * two purposes: it allows metaslab_sync_done() to detect the
737 * addition of new space; and for debugging, it ensures that we'd
738 * data fault on any attempt to use this metaslab before it's ready.
740 space_map_create(&msp->ms_map, start, size,
741 vd->vdev_ashift, &msp->ms_lock);
743 metaslab_group_add(mg, msp);
745 if (metaslab_debug && smo->smo_object != 0) {
746 mutex_enter(&msp->ms_lock);
747 VERIFY(space_map_load(&msp->ms_map, mg->mg_class->mc_ops,
748 SM_FREE, smo, spa_meta_objset(vd->vdev_spa)) == 0);
749 mutex_exit(&msp->ms_lock);
753 * If we're opening an existing pool (txg == 0) or creating
754 * a new one (txg == TXG_INITIAL), all space is available now.
755 * If we're adding space to an existing pool, the new space
756 * does not become available until after this txg has synced.
758 if (txg <= TXG_INITIAL)
759 metaslab_sync_done(msp, 0);
762 vdev_dirty(vd, 0, NULL, txg);
763 vdev_dirty(vd, VDD_METASLAB, msp, txg);
770 metaslab_fini(metaslab_t *msp)
772 metaslab_group_t *mg = msp->ms_group;
775 vdev_space_update(mg->mg_vd,
776 -msp->ms_smo.smo_alloc, 0, -msp->ms_map.sm_size);
778 metaslab_group_remove(mg, msp);
780 mutex_enter(&msp->ms_lock);
782 space_map_unload(&msp->ms_map);
783 space_map_destroy(&msp->ms_map);
785 for (t = 0; t < TXG_SIZE; t++) {
786 space_map_destroy(&msp->ms_allocmap[t]);
787 space_map_destroy(&msp->ms_freemap[t]);
790 for (t = 0; t < TXG_DEFER_SIZE; t++)
791 space_map_destroy(&msp->ms_defermap[t]);
793 ASSERT3S(msp->ms_deferspace, ==, 0);
795 mutex_exit(&msp->ms_lock);
796 mutex_destroy(&msp->ms_lock);
798 kmem_free(msp, sizeof (metaslab_t));
801 #define METASLAB_WEIGHT_PRIMARY (1ULL << 63)
802 #define METASLAB_WEIGHT_SECONDARY (1ULL << 62)
803 #define METASLAB_ACTIVE_MASK \
804 (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
807 metaslab_weight(metaslab_t *msp)
809 metaslab_group_t *mg = msp->ms_group;
810 space_map_t *sm = &msp->ms_map;
811 space_map_obj_t *smo = &msp->ms_smo;
812 vdev_t *vd = mg->mg_vd;
813 uint64_t weight, space;
815 ASSERT(MUTEX_HELD(&msp->ms_lock));
818 * The baseline weight is the metaslab's free space.
820 space = sm->sm_size - smo->smo_alloc;
824 * Modern disks have uniform bit density and constant angular velocity.
825 * Therefore, the outer recording zones are faster (higher bandwidth)
826 * than the inner zones by the ratio of outer to inner track diameter,
827 * which is typically around 2:1. We account for this by assigning
828 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
829 * In effect, this means that we'll select the metaslab with the most
830 * free bandwidth rather than simply the one with the most free space.
832 weight = 2 * weight -
833 ((sm->sm_start >> vd->vdev_ms_shift) * weight) / vd->vdev_ms_count;
834 ASSERT(weight >= space && weight <= 2 * space);
837 * For locality, assign higher weight to metaslabs which have
838 * a lower offset than what we've already activated.
840 if (sm->sm_start <= mg->mg_bonus_area)
841 weight *= (metaslab_smo_bonus_pct / 100);
842 ASSERT(weight >= space &&
843 weight <= 2 * (metaslab_smo_bonus_pct / 100) * space);
845 if (sm->sm_loaded && !sm->sm_ops->smop_fragmented(sm)) {
847 * If this metaslab is one we're actively using, adjust its
848 * weight to make it preferable to any inactive metaslab so
849 * we'll polish it off.
851 weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
857 metaslab_prefetch(metaslab_group_t *mg)
859 spa_t *spa = mg->mg_vd->vdev_spa;
861 avl_tree_t *t = &mg->mg_metaslab_tree;
864 mutex_enter(&mg->mg_lock);
867 * Prefetch the next potential metaslabs
869 for (msp = avl_first(t), m = 0; msp; msp = AVL_NEXT(t, msp), m++) {
870 space_map_t *sm = &msp->ms_map;
871 space_map_obj_t *smo = &msp->ms_smo;
873 /* If we have reached our prefetch limit then we're done */
874 if (m >= metaslab_prefetch_limit)
877 if (!sm->sm_loaded && smo->smo_object != 0) {
878 mutex_exit(&mg->mg_lock);
879 dmu_prefetch(spa_meta_objset(spa), smo->smo_object,
880 0ULL, smo->smo_objsize);
881 mutex_enter(&mg->mg_lock);
884 mutex_exit(&mg->mg_lock);
888 metaslab_activate(metaslab_t *msp, uint64_t activation_weight)
890 metaslab_group_t *mg = msp->ms_group;
891 space_map_t *sm = &msp->ms_map;
892 space_map_ops_t *sm_ops = msp->ms_group->mg_class->mc_ops;
895 ASSERT(MUTEX_HELD(&msp->ms_lock));
897 if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
898 space_map_load_wait(sm);
899 if (!sm->sm_loaded) {
900 int error = space_map_load(sm, sm_ops, SM_FREE,
902 spa_meta_objset(msp->ms_group->mg_vd->vdev_spa));
904 metaslab_group_sort(msp->ms_group, msp, 0);
907 for (t = 0; t < TXG_DEFER_SIZE; t++)
908 space_map_walk(&msp->ms_defermap[t],
909 space_map_claim, sm);
914 * Track the bonus area as we activate new metaslabs.
916 if (sm->sm_start > mg->mg_bonus_area) {
917 mutex_enter(&mg->mg_lock);
918 mg->mg_bonus_area = sm->sm_start;
919 mutex_exit(&mg->mg_lock);
922 metaslab_group_sort(msp->ms_group, msp,
923 msp->ms_weight | activation_weight);
925 ASSERT(sm->sm_loaded);
926 ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
932 metaslab_passivate(metaslab_t *msp, uint64_t size)
935 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
936 * this metaslab again. In that case, it had better be empty,
937 * or we would be leaving space on the table.
939 ASSERT(size >= SPA_MINBLOCKSIZE || msp->ms_map.sm_space == 0);
940 metaslab_group_sort(msp->ms_group, msp, MIN(msp->ms_weight, size));
941 ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
945 * Write a metaslab to disk in the context of the specified transaction group.
948 metaslab_sync(metaslab_t *msp, uint64_t txg)
950 vdev_t *vd = msp->ms_group->mg_vd;
951 spa_t *spa = vd->vdev_spa;
952 objset_t *mos = spa_meta_objset(spa);
953 space_map_t *allocmap = &msp->ms_allocmap[txg & TXG_MASK];
954 space_map_t *freemap = &msp->ms_freemap[txg & TXG_MASK];
955 space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
956 space_map_t *sm = &msp->ms_map;
957 space_map_obj_t *smo = &msp->ms_smo_syncing;
962 ASSERT(!vd->vdev_ishole);
964 if (allocmap->sm_space == 0 && freemap->sm_space == 0)
968 * The only state that can actually be changing concurrently with
969 * metaslab_sync() is the metaslab's ms_map. No other thread can
970 * be modifying this txg's allocmap, freemap, freed_map, or smo.
971 * Therefore, we only hold ms_lock to satify space_map ASSERTs.
972 * We drop it whenever we call into the DMU, because the DMU
973 * can call down to us (e.g. via zio_free()) at any time.
976 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
978 if (smo->smo_object == 0) {
979 ASSERT(smo->smo_objsize == 0);
980 ASSERT(smo->smo_alloc == 0);
981 smo->smo_object = dmu_object_alloc(mos,
982 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
983 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
984 ASSERT(smo->smo_object != 0);
985 dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
986 (sm->sm_start >> vd->vdev_ms_shift),
987 sizeof (uint64_t), &smo->smo_object, tx);
990 mutex_enter(&msp->ms_lock);
992 space_map_walk(freemap, space_map_add, freed_map);
994 if (sm->sm_loaded && spa_sync_pass(spa) == 1 && smo->smo_objsize >=
995 2 * sizeof (uint64_t) * avl_numnodes(&sm->sm_root)) {
997 * The in-core space map representation is twice as compact
998 * as the on-disk one, so it's time to condense the latter
999 * by generating a pure allocmap from first principles.
1001 * This metaslab is 100% allocated,
1002 * minus the content of the in-core map (sm),
1003 * minus what's been freed this txg (freed_map),
1004 * minus deferred frees (ms_defermap[]),
1005 * minus allocations from txgs in the future
1006 * (because they haven't been committed yet).
1008 space_map_vacate(allocmap, NULL, NULL);
1009 space_map_vacate(freemap, NULL, NULL);
1011 space_map_add(allocmap, allocmap->sm_start, allocmap->sm_size);
1013 space_map_walk(sm, space_map_remove, allocmap);
1014 space_map_walk(freed_map, space_map_remove, allocmap);
1016 for (t = 0; t < TXG_DEFER_SIZE; t++)
1017 space_map_walk(&msp->ms_defermap[t],
1018 space_map_remove, allocmap);
1020 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
1021 space_map_walk(&msp->ms_allocmap[(txg + t) & TXG_MASK],
1022 space_map_remove, allocmap);
1024 mutex_exit(&msp->ms_lock);
1025 space_map_truncate(smo, mos, tx);
1026 mutex_enter(&msp->ms_lock);
1029 space_map_sync(allocmap, SM_ALLOC, smo, mos, tx);
1030 space_map_sync(freemap, SM_FREE, smo, mos, tx);
1032 mutex_exit(&msp->ms_lock);
1034 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1035 dmu_buf_will_dirty(db, tx);
1036 ASSERT3U(db->db_size, >=, sizeof (*smo));
1037 bcopy(smo, db->db_data, sizeof (*smo));
1038 dmu_buf_rele(db, FTAG);
1044 * Called after a transaction group has completely synced to mark
1045 * all of the metaslab's free space as usable.
1048 metaslab_sync_done(metaslab_t *msp, uint64_t txg)
1050 space_map_obj_t *smo = &msp->ms_smo;
1051 space_map_obj_t *smosync = &msp->ms_smo_syncing;
1052 space_map_t *sm = &msp->ms_map;
1053 space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
1054 space_map_t *defer_map = &msp->ms_defermap[txg % TXG_DEFER_SIZE];
1055 metaslab_group_t *mg = msp->ms_group;
1056 vdev_t *vd = mg->mg_vd;
1057 int64_t alloc_delta, defer_delta;
1060 ASSERT(!vd->vdev_ishole);
1062 mutex_enter(&msp->ms_lock);
1065 * If this metaslab is just becoming available, initialize its
1066 * allocmaps and freemaps and add its capacity to the vdev.
1068 if (freed_map->sm_size == 0) {
1069 for (t = 0; t < TXG_SIZE; t++) {
1070 space_map_create(&msp->ms_allocmap[t], sm->sm_start,
1071 sm->sm_size, sm->sm_shift, sm->sm_lock);
1072 space_map_create(&msp->ms_freemap[t], sm->sm_start,
1073 sm->sm_size, sm->sm_shift, sm->sm_lock);
1076 for (t = 0; t < TXG_DEFER_SIZE; t++)
1077 space_map_create(&msp->ms_defermap[t], sm->sm_start,
1078 sm->sm_size, sm->sm_shift, sm->sm_lock);
1080 vdev_space_update(vd, 0, 0, sm->sm_size);
1083 alloc_delta = smosync->smo_alloc - smo->smo_alloc;
1084 defer_delta = freed_map->sm_space - defer_map->sm_space;
1086 vdev_space_update(vd, alloc_delta + defer_delta, defer_delta, 0);
1088 ASSERT(msp->ms_allocmap[txg & TXG_MASK].sm_space == 0);
1089 ASSERT(msp->ms_freemap[txg & TXG_MASK].sm_space == 0);
1092 * If there's a space_map_load() in progress, wait for it to complete
1093 * so that we have a consistent view of the in-core space map.
1094 * Then, add defer_map (oldest deferred frees) to this map and
1095 * transfer freed_map (this txg's frees) to defer_map.
1097 space_map_load_wait(sm);
1098 space_map_vacate(defer_map, sm->sm_loaded ? space_map_free : NULL, sm);
1099 space_map_vacate(freed_map, space_map_add, defer_map);
1103 msp->ms_deferspace += defer_delta;
1104 ASSERT3S(msp->ms_deferspace, >=, 0);
1105 ASSERT3S(msp->ms_deferspace, <=, sm->sm_size);
1106 if (msp->ms_deferspace != 0) {
1108 * Keep syncing this metaslab until all deferred frees
1109 * are back in circulation.
1111 vdev_dirty(vd, VDD_METASLAB, msp, txg + 1);
1115 * If the map is loaded but no longer active, evict it as soon as all
1116 * future allocations have synced. (If we unloaded it now and then
1117 * loaded a moment later, the map wouldn't reflect those allocations.)
1119 if (sm->sm_loaded && (msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
1122 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
1123 if (msp->ms_allocmap[(txg + t) & TXG_MASK].sm_space)
1126 if (evictable && !metaslab_debug)
1127 space_map_unload(sm);
1130 metaslab_group_sort(mg, msp, metaslab_weight(msp));
1132 mutex_exit(&msp->ms_lock);
1136 metaslab_sync_reassess(metaslab_group_t *mg)
1138 vdev_t *vd = mg->mg_vd;
1139 int64_t failures = mg->mg_alloc_failures;
1143 * Re-evaluate all metaslabs which have lower offsets than the
1146 for (m = 0; m < vd->vdev_ms_count; m++) {
1147 metaslab_t *msp = vd->vdev_ms[m];
1149 if (msp->ms_map.sm_start > mg->mg_bonus_area)
1152 mutex_enter(&msp->ms_lock);
1153 metaslab_group_sort(mg, msp, metaslab_weight(msp));
1154 mutex_exit(&msp->ms_lock);
1157 atomic_add_64(&mg->mg_alloc_failures, -failures);
1160 * Prefetch the next potential metaslabs
1162 metaslab_prefetch(mg);
1166 metaslab_distance(metaslab_t *msp, dva_t *dva)
1168 uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
1169 uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
1170 uint64_t start = msp->ms_map.sm_start >> ms_shift;
1172 if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
1173 return (1ULL << 63);
1176 return ((start - offset) << ms_shift);
1178 return ((offset - start) << ms_shift);
1183 metaslab_group_alloc(metaslab_group_t *mg, uint64_t psize, uint64_t asize,
1184 uint64_t txg, uint64_t min_distance, dva_t *dva, int d, int flags)
1186 spa_t *spa = mg->mg_vd->vdev_spa;
1187 metaslab_t *msp = NULL;
1188 uint64_t offset = -1ULL;
1189 avl_tree_t *t = &mg->mg_metaslab_tree;
1190 uint64_t activation_weight;
1191 uint64_t target_distance;
1194 activation_weight = METASLAB_WEIGHT_PRIMARY;
1195 for (i = 0; i < d; i++) {
1196 if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
1197 activation_weight = METASLAB_WEIGHT_SECONDARY;
1203 boolean_t was_active;
1205 mutex_enter(&mg->mg_lock);
1206 for (msp = avl_first(t); msp; msp = AVL_NEXT(t, msp)) {
1207 if (msp->ms_weight < asize) {
1208 spa_dbgmsg(spa, "%s: failed to meet weight "
1209 "requirement: vdev %llu, txg %llu, mg %p, "
1210 "msp %p, psize %llu, asize %llu, "
1211 "failures %llu, weight %llu",
1212 spa_name(spa), mg->mg_vd->vdev_id, txg,
1213 mg, msp, psize, asize,
1214 mg->mg_alloc_failures, msp->ms_weight);
1215 mutex_exit(&mg->mg_lock);
1218 was_active = msp->ms_weight & METASLAB_ACTIVE_MASK;
1219 if (activation_weight == METASLAB_WEIGHT_PRIMARY)
1222 target_distance = min_distance +
1223 (msp->ms_smo.smo_alloc ? 0 : min_distance >> 1);
1225 for (i = 0; i < d; i++)
1226 if (metaslab_distance(msp, &dva[i]) <
1232 mutex_exit(&mg->mg_lock);
1237 * If we've already reached the allowable number of failed
1238 * allocation attempts on this metaslab group then we
1239 * consider skipping it. We skip it only if we're allowed
1240 * to "fast" gang, the physical size is larger than
1241 * a gang block, and we're attempting to allocate from
1242 * the primary metaslab.
1244 if (mg->mg_alloc_failures > zfs_mg_alloc_failures &&
1245 CAN_FASTGANG(flags) && psize > SPA_GANGBLOCKSIZE &&
1246 activation_weight == METASLAB_WEIGHT_PRIMARY) {
1247 spa_dbgmsg(spa, "%s: skipping metaslab group: "
1248 "vdev %llu, txg %llu, mg %p, psize %llu, "
1249 "asize %llu, failures %llu", spa_name(spa),
1250 mg->mg_vd->vdev_id, txg, mg, psize, asize,
1251 mg->mg_alloc_failures);
1255 mutex_enter(&msp->ms_lock);
1258 * Ensure that the metaslab we have selected is still
1259 * capable of handling our request. It's possible that
1260 * another thread may have changed the weight while we
1261 * were blocked on the metaslab lock.
1263 if (msp->ms_weight < asize || (was_active &&
1264 !(msp->ms_weight & METASLAB_ACTIVE_MASK) &&
1265 activation_weight == METASLAB_WEIGHT_PRIMARY)) {
1266 mutex_exit(&msp->ms_lock);
1270 if ((msp->ms_weight & METASLAB_WEIGHT_SECONDARY) &&
1271 activation_weight == METASLAB_WEIGHT_PRIMARY) {
1272 metaslab_passivate(msp,
1273 msp->ms_weight & ~METASLAB_ACTIVE_MASK);
1274 mutex_exit(&msp->ms_lock);
1278 if (metaslab_activate(msp, activation_weight) != 0) {
1279 mutex_exit(&msp->ms_lock);
1283 if ((offset = space_map_alloc(&msp->ms_map, asize)) != -1ULL)
1286 atomic_inc_64(&mg->mg_alloc_failures);
1288 metaslab_passivate(msp, space_map_maxsize(&msp->ms_map));
1290 mutex_exit(&msp->ms_lock);
1293 if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
1294 vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
1296 space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, asize);
1298 mutex_exit(&msp->ms_lock);
1304 * Allocate a block for the specified i/o.
1307 metaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
1308 dva_t *dva, int d, dva_t *hintdva, uint64_t txg, int flags)
1310 metaslab_group_t *mg, *rotor;
1314 int zio_lock = B_FALSE;
1315 boolean_t allocatable;
1316 uint64_t offset = -1ULL;
1320 ASSERT(!DVA_IS_VALID(&dva[d]));
1323 * For testing, make some blocks above a certain size be gang blocks.
1325 if (psize >= metaslab_gang_bang && (ddi_get_lbolt() & 3) == 0)
1329 * Start at the rotor and loop through all mgs until we find something.
1330 * Note that there's no locking on mc_rotor or mc_aliquot because
1331 * nothing actually breaks if we miss a few updates -- we just won't
1332 * allocate quite as evenly. It all balances out over time.
1334 * If we are doing ditto or log blocks, try to spread them across
1335 * consecutive vdevs. If we're forced to reuse a vdev before we've
1336 * allocated all of our ditto blocks, then try and spread them out on
1337 * that vdev as much as possible. If it turns out to not be possible,
1338 * gradually lower our standards until anything becomes acceptable.
1339 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
1340 * gives us hope of containing our fault domains to something we're
1341 * able to reason about. Otherwise, any two top-level vdev failures
1342 * will guarantee the loss of data. With consecutive allocation,
1343 * only two adjacent top-level vdev failures will result in data loss.
1345 * If we are doing gang blocks (hintdva is non-NULL), try to keep
1346 * ourselves on the same vdev as our gang block header. That
1347 * way, we can hope for locality in vdev_cache, plus it makes our
1348 * fault domains something tractable.
1351 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
1354 * It's possible the vdev we're using as the hint no
1355 * longer exists (i.e. removed). Consult the rotor when
1361 if (flags & METASLAB_HINTBP_AVOID &&
1362 mg->mg_next != NULL)
1367 } else if (d != 0) {
1368 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
1369 mg = vd->vdev_mg->mg_next;
1375 * If the hint put us into the wrong metaslab class, or into a
1376 * metaslab group that has been passivated, just follow the rotor.
1378 if (mg->mg_class != mc || mg->mg_activation_count <= 0)
1385 ASSERT(mg->mg_activation_count == 1);
1390 * Don't allocate from faulted devices.
1393 spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
1394 allocatable = vdev_allocatable(vd);
1395 spa_config_exit(spa, SCL_ZIO, FTAG);
1397 allocatable = vdev_allocatable(vd);
1403 * Avoid writing single-copy data to a failing vdev
1405 if ((vd->vdev_stat.vs_write_errors > 0 ||
1406 vd->vdev_state < VDEV_STATE_HEALTHY) &&
1407 d == 0 && dshift == 3) {
1412 ASSERT(mg->mg_class == mc);
1414 distance = vd->vdev_asize >> dshift;
1415 if (distance <= (1ULL << vd->vdev_ms_shift))
1420 asize = vdev_psize_to_asize(vd, psize);
1421 ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
1423 offset = metaslab_group_alloc(mg, psize, asize, txg, distance,
1425 if (offset != -1ULL) {
1427 * If we've just selected this metaslab group,
1428 * figure out whether the corresponding vdev is
1429 * over- or under-used relative to the pool,
1430 * and set an allocation bias to even it out.
1432 if (mc->mc_aliquot == 0) {
1433 vdev_stat_t *vs = &vd->vdev_stat;
1436 vu = (vs->vs_alloc * 100) / (vs->vs_space + 1);
1437 cu = (mc->mc_alloc * 100) / (mc->mc_space + 1);
1440 * Calculate how much more or less we should
1441 * try to allocate from this device during
1442 * this iteration around the rotor.
1443 * For example, if a device is 80% full
1444 * and the pool is 20% full then we should
1445 * reduce allocations by 60% on this device.
1447 * mg_bias = (20 - 80) * 512K / 100 = -307K
1449 * This reduces allocations by 307K for this
1452 mg->mg_bias = ((cu - vu) *
1453 (int64_t)mg->mg_aliquot) / 100;
1456 if (atomic_add_64_nv(&mc->mc_aliquot, asize) >=
1457 mg->mg_aliquot + mg->mg_bias) {
1458 mc->mc_rotor = mg->mg_next;
1462 DVA_SET_VDEV(&dva[d], vd->vdev_id);
1463 DVA_SET_OFFSET(&dva[d], offset);
1464 DVA_SET_GANG(&dva[d], !!(flags & METASLAB_GANG_HEADER));
1465 DVA_SET_ASIZE(&dva[d], asize);
1470 mc->mc_rotor = mg->mg_next;
1472 } while ((mg = mg->mg_next) != rotor);
1476 ASSERT(dshift < 64);
1480 if (!allocatable && !zio_lock) {
1486 bzero(&dva[d], sizeof (dva_t));
1492 * Free the block represented by DVA in the context of the specified
1493 * transaction group.
1496 metaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
1498 uint64_t vdev = DVA_GET_VDEV(dva);
1499 uint64_t offset = DVA_GET_OFFSET(dva);
1500 uint64_t size = DVA_GET_ASIZE(dva);
1504 ASSERT(DVA_IS_VALID(dva));
1506 if (txg > spa_freeze_txg(spa))
1509 if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
1510 (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
1511 cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
1512 (u_longlong_t)vdev, (u_longlong_t)offset);
1517 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1519 if (DVA_GET_GANG(dva))
1520 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
1522 mutex_enter(&msp->ms_lock);
1525 space_map_remove(&msp->ms_allocmap[txg & TXG_MASK],
1527 space_map_free(&msp->ms_map, offset, size);
1529 if (msp->ms_freemap[txg & TXG_MASK].sm_space == 0)
1530 vdev_dirty(vd, VDD_METASLAB, msp, txg);
1531 space_map_add(&msp->ms_freemap[txg & TXG_MASK], offset, size);
1534 mutex_exit(&msp->ms_lock);
1538 * Intent log support: upon opening the pool after a crash, notify the SPA
1539 * of blocks that the intent log has allocated for immediate write, but
1540 * which are still considered free by the SPA because the last transaction
1541 * group didn't commit yet.
1544 metaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
1546 uint64_t vdev = DVA_GET_VDEV(dva);
1547 uint64_t offset = DVA_GET_OFFSET(dva);
1548 uint64_t size = DVA_GET_ASIZE(dva);
1553 ASSERT(DVA_IS_VALID(dva));
1555 if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
1556 (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
1559 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1561 if (DVA_GET_GANG(dva))
1562 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
1564 mutex_enter(&msp->ms_lock);
1566 if ((txg != 0 && spa_writeable(spa)) || !msp->ms_map.sm_loaded)
1567 error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY);
1569 if (error == 0 && !space_map_contains(&msp->ms_map, offset, size))
1572 if (error || txg == 0) { /* txg == 0 indicates dry run */
1573 mutex_exit(&msp->ms_lock);
1577 space_map_claim(&msp->ms_map, offset, size);
1579 if (spa_writeable(spa)) { /* don't dirty if we're zdb(1M) */
1580 if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
1581 vdev_dirty(vd, VDD_METASLAB, msp, txg);
1582 space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, size);
1585 mutex_exit(&msp->ms_lock);
1591 metaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
1592 int ndvas, uint64_t txg, blkptr_t *hintbp, int flags)
1594 dva_t *dva = bp->blk_dva;
1595 dva_t *hintdva = hintbp->blk_dva;
1598 ASSERT(bp->blk_birth == 0);
1599 ASSERT(BP_PHYSICAL_BIRTH(bp) == 0);
1601 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
1603 if (mc->mc_rotor == NULL) { /* no vdevs in this class */
1604 spa_config_exit(spa, SCL_ALLOC, FTAG);
1608 ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
1609 ASSERT(BP_GET_NDVAS(bp) == 0);
1610 ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
1612 for (d = 0; d < ndvas; d++) {
1613 error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
1616 for (d--; d >= 0; d--) {
1617 metaslab_free_dva(spa, &dva[d], txg, B_TRUE);
1618 bzero(&dva[d], sizeof (dva_t));
1620 spa_config_exit(spa, SCL_ALLOC, FTAG);
1625 ASSERT(BP_GET_NDVAS(bp) == ndvas);
1627 spa_config_exit(spa, SCL_ALLOC, FTAG);
1629 BP_SET_BIRTH(bp, txg, txg);
1635 metaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
1637 const dva_t *dva = bp->blk_dva;
1638 int d, ndvas = BP_GET_NDVAS(bp);
1640 ASSERT(!BP_IS_HOLE(bp));
1641 ASSERT(!now || bp->blk_birth >= spa_syncing_txg(spa));
1643 spa_config_enter(spa, SCL_FREE, FTAG, RW_READER);
1645 for (d = 0; d < ndvas; d++)
1646 metaslab_free_dva(spa, &dva[d], txg, now);
1648 spa_config_exit(spa, SCL_FREE, FTAG);
1652 metaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
1654 const dva_t *dva = bp->blk_dva;
1655 int ndvas = BP_GET_NDVAS(bp);
1658 ASSERT(!BP_IS_HOLE(bp));
1662 * First do a dry run to make sure all DVAs are claimable,
1663 * so we don't have to unwind from partial failures below.
1665 if ((error = metaslab_claim(spa, bp, 0)) != 0)
1669 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
1671 for (d = 0; d < ndvas; d++)
1672 if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
1675 spa_config_exit(spa, SCL_ALLOC, FTAG);
1677 ASSERT(error == 0 || txg == 0);