metaslab.c revision 1934e92fc930c49429ad71a8ca97340f33227e78
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#pragma ident "%Z%%M% %I% %E% SMI"
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zfs_context.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/spa_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_tx.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/space_map.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/metaslab_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/vdev_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zio.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
13506d1eefbbc37e2f12a0528831d9f6d4c361d7maybeeuint64_t metaslab_aliquot = 512ULL << 10;
13506d1eefbbc37e2f12a0528831d9f6d4c361d7maybee
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Metaslab classes
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_class_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_class_create(void)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_class_t *mc;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc = kmem_zalloc(sizeof (metaslab_class_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (mc);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_class_destroy(metaslab_class_t *mc)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens while ((mg = mc->mc_rotor) != NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_class_remove(mc, mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_destroy(mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(mc, sizeof (metaslab_class_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_class_add(metaslab_class_t *mc, metaslab_group_t *mg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mgprev, *mgnext;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(mg->mg_class == NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if ((mgprev = mc->mc_rotor) == NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_prev = mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_next = mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mgnext = mgprev->mg_next;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_prev = mgprev;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_next = mgnext;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mgprev->mg_next = mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mgnext->mg_prev = mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_class = mc;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_class_remove(metaslab_class_t *mc, metaslab_group_t *mg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mgprev, *mgnext;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(mg->mg_class == mc);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mgprev = mg->mg_prev;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mgnext = mg->mg_next;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (mg == mgnext) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = mgnext;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mgprev->mg_next = mgnext;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mgnext->mg_prev = mgprev;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_prev = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_next = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_class = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Metaslab groups
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_compare(const void *x1, const void *x2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens const metaslab_t *m1 = x1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens const metaslab_t *m2 = x2;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (m1->ms_weight < m2->ms_weight)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (m1->ms_weight > m2->ms_weight)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the weights are identical, use the offset to force uniqueness.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (m1->ms_map.sm_start < m2->ms_map.sm_start)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (m1->ms_map.sm_start > m2->ms_map.sm_start)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3P(m1, ==, m2);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg = kmem_zalloc(sizeof (metaslab_group_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_create(&mg->mg_metaslab_tree, metaslab_compare,
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
13506d1eefbbc37e2f12a0528831d9f6d4c361d7maybee mg->mg_aliquot = metaslab_aliquot * MAX(1, vd->vdev_children);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_vd = vd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_class_add(mc, mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_destroy(metaslab_group_t *mg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_destroy(&mg->mg_metaslab_tree);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_destroy(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(mg, sizeof (metaslab_group_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(msp->ms_group == NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp->ms_group = mg;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick msp->ms_weight = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_add(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(msp->ms_group == mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_remove(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp->ms_group = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens /*
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * Although in principle the weight can be any value, in
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * practice we do not use values in the range [1, 510].
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens */
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens ASSERT(weight >= SPA_MINBLOCKSIZE-1 || weight == 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(MUTEX_HELD(&msp->ms_lock));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(msp->ms_group == mg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_remove(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp->ms_weight = weight;
fa9e4066f08beec538e775443c5be79dd423fcabahrens avl_add(&mg->mg_metaslab_tree, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&mg->mg_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick/*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * ==========================================================================
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The first-fit block allocator
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * ==========================================================================
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_ff_load(space_map_t *sm)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(sm->sm_ppd == NULL);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick sm->sm_ppd = kmem_zalloc(64 * sizeof (uint64_t), KM_SLEEP);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_ff_unload(space_map_t *sm)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick kmem_free(sm->sm_ppd, 64 * sizeof (uint64_t));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick sm->sm_ppd = NULL;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic uint64_t
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_ff_alloc(space_map_t *sm, uint64_t size)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick avl_tree_t *t = &sm->sm_root;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t align = size & -size;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_seg_t *ss, ssearch;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick avl_index_t where;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ssearch.ss_start = *cursor;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ssearch.ss_end = *cursor + size;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ss = avl_find(t, &ssearch, &where);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (ss == NULL)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ss = avl_nearest(t, where, AVL_AFTER);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick while (ss != NULL) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t offset = P2ROUNDUP(ss->ss_start, align);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (offset + size <= ss->ss_end) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick *cursor = offset + size;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (offset);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ss = AVL_NEXT(t, ss);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * If we know we've searched the whole map (*cursor == 0), give up.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * Otherwise, reset the cursor to the beginning and try again.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (*cursor == 0)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (-1ULL);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick *cursor = 0;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (metaslab_ff_alloc(sm, size));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick/* ARGSUSED */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_ff_claim(space_map_t *sm, uint64_t start, uint64_t size)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /* No need to update cursor */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick/* ARGSUSED */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_ff_free(space_map_t *sm, uint64_t start, uint64_t size)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /* No need to update cursor */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic space_map_ops_t metaslab_ff_ops = {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_ff_load,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_ff_unload,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_ff_alloc,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_ff_claim,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_ff_free
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick};
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Metaslabs
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_t *
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_init(metaslab_group_t *mg, space_map_obj_t *smo,
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t start, uint64_t size, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd = mg->mg_vd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_t *msp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp = kmem_zalloc(sizeof (metaslab_t), KM_SLEEP);
5ad820458efd0fdb914baff9c1447c22b819fa23nd mutex_init(&msp->ms_lock, NULL, MUTEX_DEFAULT, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick msp->ms_smo_syncing = *smo;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * We create the main space map here, but we don't create the
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * allocmaps and freemaps until metaslab_sync_done(). This serves
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * two purposes: it allows metaslab_sync_done() to detect the
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * addition of new space; and for debugging, it ensures that we'd
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * data fault on any attempt to use this metaslab before it's ready.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_create(&msp->ms_map, start, size,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vd->vdev_ashift, &msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_group_add(mg, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we're opening an existing pool (txg == 0) or creating
fa9e4066f08beec538e775443c5be79dd423fcabahrens * a new one (txg == TXG_INITIAL), all space is available now.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we're adding space to an existing pool, the new space
fa9e4066f08beec538e775443c5be79dd423fcabahrens * does not become available until after this txg has synced.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (txg <= TXG_INITIAL)
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_sync_done(msp, 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (txg != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The vdev is dirty, but the metaslab isn't -- it just needs
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * to have metaslab_sync_done() invoked from vdev_sync_done().
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * [We could just dirty the metaslab, but that would cause us
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * to allocate a space map object for it, which is wasteful
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * and would mess up the locality logic in metaslab_weight().]
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(TXG_CLEAN(txg) == spa_last_synced_txg(vd->vdev_spa));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_dirty(vd, 0, NULL, txg);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_dirty(vd, VDD_METASLAB, msp, TXG_CLEAN(txg));
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_fini(metaslab_t *msp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mg = msp->ms_group;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick int t;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_space_update(mg->mg_vd, -msp->ms_map.sm_size,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick -msp->ms_smo.smo_alloc);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_remove(mg, msp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_unload(&msp->ms_map);
fa9e4066f08beec538e775443c5be79dd423fcabahrens space_map_destroy(&msp->ms_map);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick for (t = 0; t < TXG_SIZE; t++) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_destroy(&msp->ms_allocmap[t]);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_destroy(&msp->ms_freemap[t]);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
5ad820458efd0fdb914baff9c1447c22b819fa23nd mutex_destroy(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(msp, sizeof (metaslab_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm#define METASLAB_WEIGHT_PRIMARY (1ULL << 63)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm#define METASLAB_WEIGHT_SECONDARY (1ULL << 62)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm#define METASLAB_ACTIVE_MASK \
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm#define METASLAB_SMO_BONUS_MULTIPLIER 2
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic uint64_t
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_weight(metaslab_t *msp)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm metaslab_group_t *mg = msp->ms_group;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_t *sm = &msp->ms_map;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_obj_t *smo = &msp->ms_smo;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm vdev_t *vd = mg->mg_vd;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t weight, space;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(MUTEX_HELD(&msp->ms_lock));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The baseline weight is the metaslab's free space.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space = sm->sm_size - smo->smo_alloc;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick weight = space;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * Modern disks have uniform bit density and constant angular velocity.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * Therefore, the outer recording zones are faster (higher bandwidth)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * than the inner zones by the ratio of outer to inner track diameter,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * which is typically around 2:1. We account for this by assigning
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * In effect, this means that we'll select the metaslab with the most
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * free bandwidth rather than simply the one with the most free space.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick weight = 2 * weight -
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ((sm->sm_start >> vd->vdev_ms_shift) * weight) / vd->vdev_ms_count;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(weight >= space && weight <= 2 * space);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * For locality, assign higher weight to metaslabs we've used before.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (smo->smo_object != 0)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm weight *= METASLAB_SMO_BONUS_MULTIPLIER;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT(weight >= space &&
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm weight <= 2 * METASLAB_SMO_BONUS_MULTIPLIER * space);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * If this metaslab is one we're actively using, adjust its weight to
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * make it preferable to any inactive metaslab so we'll polish it off.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (weight);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic int
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmmetaslab_activate(metaslab_t *msp, uint64_t activation_weight)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_t *sm = &msp->ms_map;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(MUTEX_HELD(&msp->ms_lock));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick int error = space_map_load(sm, &metaslab_ff_ops,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick SM_FREE, &msp->ms_smo,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick msp->ms_group->mg_vd->vdev_spa->spa_meta_objset);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (error) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_group_sort(msp->ms_group, msp, 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (error);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_group_sort(msp->ms_group, msp,
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm msp->ms_weight | activation_weight);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(sm->sm_loaded);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick return (0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickstatic void
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwickmetaslab_passivate(metaslab_t *msp, uint64_t size)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick{
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens /*
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * If size < SPA_MINBLOCKSIZE, then we will not allocate from
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * this metaslab again. In that case, it had better be empty,
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens * or we would be leaving space on the table.
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens */
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens ASSERT(size >= SPA_MINBLOCKSIZE || msp->ms_map.sm_space == 0);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm metaslab_group_sort(msp->ms_group, msp, MIN(msp->ms_weight, size));
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick}
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Write a metaslab to disk in the context of the specified transaction group.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_sync(metaslab_t *msp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd = msp->ms_group->mg_vd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens spa_t *spa = vd->vdev_spa;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick objset_t *mos = spa->spa_meta_objset;
fa9e4066f08beec538e775443c5be79dd423fcabahrens space_map_t *allocmap = &msp->ms_allocmap[txg & TXG_MASK];
fa9e4066f08beec538e775443c5be79dd423fcabahrens space_map_t *freemap = &msp->ms_freemap[txg & TXG_MASK];
fa9e4066f08beec538e775443c5be79dd423fcabahrens space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_t *sm = &msp->ms_map;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_obj_t *smo = &msp->ms_smo_syncing;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_t *db;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_t *tx;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick int t;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The only state that can actually be changing concurrently with
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * metaslab_sync() is the metaslab's ms_map. No other thread can
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * be modifying this txg's allocmap, freemap, freed_map, or smo.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * Therefore, we only hold ms_lock to satify space_map ASSERTs.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * We drop it whenever we call into the DMU, because the DMU
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * can call down to us (e.g. via zio_free()) at any time.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (smo->smo_object == 0) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(smo->smo_objsize == 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(smo->smo_alloc == 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_exit(&msp->ms_lock);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick smo->smo_object = dmu_object_alloc(mos,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(smo->smo_object != 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick (sm->sm_start >> vd->vdev_ms_shift),
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick sizeof (uint64_t), &smo->smo_object, tx);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_enter(&msp->ms_lock);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_walk(freemap, space_map_add, freed_map);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (sm->sm_loaded && spa_sync_pass(spa) == 1 && smo->smo_objsize >=
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick 2 * sizeof (uint64_t) * avl_numnodes(&sm->sm_root)) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * The in-core space map representation is twice as compact
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * as the on-disk one, so it's time to condense the latter
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * by generating a pure allocmap from first principles.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick *
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * This metaslab is 100% allocated,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * minus the content of the in-core map (sm),
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * minus what's been freed this txg (freed_map),
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * minus allocations from txgs in the future
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * (because they haven't been committed yet).
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_vacate(allocmap, NULL, NULL);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_vacate(freemap, NULL, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_add(allocmap, allocmap->sm_start, allocmap->sm_size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_walk(sm, space_map_remove, allocmap);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_walk(freed_map, space_map_remove, allocmap);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick for (t = 1; t < TXG_CONCURRENT_STATES; t++)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_walk(&msp->ms_allocmap[(txg + t) & TXG_MASK],
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_remove, allocmap);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_exit(&msp->ms_lock);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_truncate(smo, mos, tx);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_enter(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_sync(allocmap, SM_ALLOC, smo, mos, tx);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_sync(freemap, SM_FREE, smo, mos, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick dmu_buf_will_dirty(db, tx);
1934e92fc930c49429ad71a8ca97340f33227e78maybee ASSERT3U(db->db_size, >=, sizeof (*smo));
1934e92fc930c49429ad71a8ca97340f33227e78maybee bcopy(smo, db->db_data, sizeof (*smo));
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick dmu_buf_rele(db, FTAG);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick dmu_tx_commit(tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Called after a transaction group has completely synced to mark
fa9e4066f08beec538e775443c5be79dd423fcabahrens * all of the metaslab's free space as usable.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensmetaslab_sync_done(metaslab_t *msp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_obj_t *smo = &msp->ms_smo;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_obj_t *smosync = &msp->ms_smo_syncing;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_t *sm = &msp->ms_map;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_group_t *mg = msp->ms_group;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_t *vd = mg->mg_vd;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick int t;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * If this metaslab is just becoming available, initialize its
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * allocmaps and freemaps and add its capacity to the vdev.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (freed_map->sm_size == 0) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick for (t = 0; t < TXG_SIZE; t++) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_create(&msp->ms_allocmap[t], sm->sm_start,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick sm->sm_size, sm->sm_shift, sm->sm_lock);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_create(&msp->ms_freemap[t], sm->sm_start,
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick sm->sm_size, sm->sm_shift, sm->sm_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_space_update(vd, sm->sm_size, 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_space_update(vd, 0, smosync->smo_alloc - smo->smo_alloc);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(msp->ms_allocmap[txg & TXG_MASK].sm_space == 0);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick ASSERT(msp->ms_freemap[txg & TXG_MASK].sm_space == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * If there's a space_map_load() in progress, wait for it to complete
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * so that we have a consistent view of the in-core space map.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * Then, add everything we freed in this txg to the map.
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_load_wait(sm);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_vacate(freed_map, sm->sm_loaded ? space_map_free : NULL, sm);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick *smo = *smosync;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick /*
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * If the map is loaded but no longer active, evict it as soon as all
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * future allocations have synced. (If we unloaded it now and then
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick * loaded a moment later, the map wouldn't reflect those allocations.)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick */
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (sm->sm_loaded && (msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick int evictable = 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick for (t = 1; t < TXG_CONCURRENT_STATES; t++)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (msp->ms_allocmap[(txg + t) & TXG_MASK].sm_space)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick evictable = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (evictable)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_unload(sm);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_group_sort(mg, msp, metaslab_weight(msp));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmstatic uint64_t
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmmetaslab_distance(metaslab_t *msp, dva_t *dva)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm{
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t start = msp->ms_map.sm_start >> ms_shift;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (1ULL << 63);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (offset < start)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return ((start - offset) << ms_shift);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (offset > start)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return ((offset - start) << ms_shift);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (0);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm}
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmstatic uint64_t
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmmetaslab_group_alloc(metaslab_group_t *mg, uint64_t size, uint64_t txg,
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t min_distance, dva_t *dva, int d)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick metaslab_t *msp = NULL;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t offset = -1ULL;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm avl_tree_t *t = &mg->mg_metaslab_tree;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t activation_weight;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t target_distance;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm int i;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm activation_weight = METASLAB_WEIGHT_PRIMARY;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm for (i = 0; i < d; i++)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm activation_weight = METASLAB_WEIGHT_SECONDARY;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick for (;;) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_enter(&mg->mg_lock);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm for (msp = avl_first(t); msp; msp = AVL_NEXT(t, msp)) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (msp->ms_weight < size) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm mutex_exit(&mg->mg_lock);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (-1ULL);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (activation_weight == METASLAB_WEIGHT_PRIMARY)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm break;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm target_distance = min_distance +
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm (msp->ms_smo.smo_alloc ? 0 : min_distance >> 1);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm for (i = 0; i < d; i++)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (metaslab_distance(msp, &dva[i]) <
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm target_distance)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm break;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (i == d)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm break;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_exit(&mg->mg_lock);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (msp == NULL)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (-1ULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
aeb1c1b609b02f03e8e7448beb88384ebc713525gw /*
aeb1c1b609b02f03e8e7448beb88384ebc713525gw * Ensure that the metaslab we have selected is still
aeb1c1b609b02f03e8e7448beb88384ebc713525gw * capable of handling our request. It's possible that
aeb1c1b609b02f03e8e7448beb88384ebc713525gw * another thread may have changed the weight while we
aeb1c1b609b02f03e8e7448beb88384ebc713525gw * were blocked on the metaslab lock.
aeb1c1b609b02f03e8e7448beb88384ebc713525gw */
aeb1c1b609b02f03e8e7448beb88384ebc713525gw if (msp->ms_weight < size) {
aeb1c1b609b02f03e8e7448beb88384ebc713525gw mutex_exit(&msp->ms_lock);
aeb1c1b609b02f03e8e7448beb88384ebc713525gw continue;
aeb1c1b609b02f03e8e7448beb88384ebc713525gw }
aeb1c1b609b02f03e8e7448beb88384ebc713525gw
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if ((msp->ms_weight & METASLAB_WEIGHT_SECONDARY) &&
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm activation_weight == METASLAB_WEIGHT_PRIMARY) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm metaslab_passivate(msp,
5f5f7a6f9c8e9c1587a54e690556d756ec67558cahrens msp->ms_weight & ~METASLAB_ACTIVE_MASK);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm mutex_exit(&msp->ms_lock);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm continue;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (metaslab_activate(msp, activation_weight) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens continue;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if ((offset = space_map_alloc(&msp->ms_map, size)) != -1ULL)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick break;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm metaslab_passivate(msp, size - 1);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, size);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick mutex_exit(&msp->ms_lock);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm return (offset);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Allocate a block for the specified i/o.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmstatic int
8654d0253136055bd4cc2423d87378e8a37f2eb5perrinmetaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin dva_t *dva, int d, dva_t *hintdva, uint64_t txg, boolean_t hintdva_avoid)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_group_t *mg, *rotor;
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm int dshift = 3;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm int all_zero;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t offset = -1ULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t asize;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t distance;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(!DVA_IS_VALID(&dva[d]));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Start at the rotor and loop through all mgs until we find something.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Note that there's no locking on mc_rotor or mc_allocated because
fa9e4066f08beec538e775443c5be79dd423fcabahrens * nothing actually breaks if we miss a few updates -- we just won't
fa9e4066f08beec538e775443c5be79dd423fcabahrens * allocate quite as evenly. It all balances out over time.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm *
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * If we are doing ditto or log blocks, try to spread them across
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * consecutive vdevs. If we're forced to reuse a vdev before we've
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * allocated all of our ditto blocks, then try and spread them out on
67bd71c6cc629bab3aa0d595c624a667f1574254perrin * that vdev as much as possible. If it turns out to not be possible,
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * gradually lower our standards until anything becomes acceptable.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * Also, allocating on consecutive vdevs (as opposed to random vdevs)
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * gives us hope of containing our fault domains to something we're
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * able to reason about. Otherwise, any two top-level vdev failures
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * will guarantee the loss of data. With consecutive allocation,
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * only two adjacent top-level vdev failures will result in data loss.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm *
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * If we are doing gang blocks (hintdva is non-NULL), try to keep
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * ourselves on the same vdev as our gang block header. That
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * way, we can hope for locality in vdev_cache, plus it makes our
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * fault domains something tractable.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (hintdva) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
67bd71c6cc629bab3aa0d595c624a667f1574254perrin if (hintdva_avoid)
67bd71c6cc629bab3aa0d595c624a667f1574254perrin mg = vd->vdev_mg->mg_next;
67bd71c6cc629bab3aa0d595c624a667f1574254perrin else
67bd71c6cc629bab3aa0d595c624a667f1574254perrin mg = vd->vdev_mg;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm } else if (d != 0) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm mg = vd->vdev_mg->mg_next;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm } else {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm mg = mc->mc_rotor;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin /*
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin * If the hint put us into the wrong class, just follow the rotor.
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin */
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin if (mg->mg_class != mc)
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin mg = mc->mc_rotor;
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin rotor = mg;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmtop:
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm all_zero = B_TRUE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens do {
fa9e4066f08beec538e775443c5be79dd423fcabahrens vd = mg->mg_vd;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin ASSERT(mg->mg_class == mc);
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm distance = vd->vdev_asize >> dshift;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (distance <= (1ULL << vd->vdev_ms_shift))
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm distance = 0;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm else
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm all_zero = B_FALSE;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
fa9e4066f08beec538e775443c5be79dd423fcabahrens asize = vdev_psize_to_asize(vd, psize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm offset = metaslab_group_alloc(mg, asize, txg, distance, dva, d);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (offset != -1ULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we've just selected this metaslab group,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * figure out whether the corresponding vdev is
fa9e4066f08beec538e775443c5be79dd423fcabahrens * over- or under-used relative to the pool,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and set an allocation bias to even it out.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (mc->mc_allocated == 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_stat_t *vs = &vd->vdev_stat;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t alloc, space;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int64_t vu, su;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens alloc = spa_get_alloc(spa);
fa9e4066f08beec538e775443c5be79dd423fcabahrens space = spa_get_space(spa);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Determine percent used in units of 0..1024.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (This is just to avoid floating point.)
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens vu = (vs->vs_alloc << 10) / (vs->vs_space + 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens su = (alloc << 10) / (space + 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Bias by at most +/- 25% of the aliquot.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_bias = ((su - vu) *
fa9e4066f08beec538e775443c5be79dd423fcabahrens (int64_t)mg->mg_aliquot) / (1024 * 4);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (atomic_add_64_nv(&mc->mc_allocated, asize) >=
fa9e4066f08beec538e775443c5be79dd423fcabahrens mg->mg_aliquot + mg->mg_bias) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = mg->mg_next;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_allocated = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_VDEV(&dva[d], vd->vdev_id);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_OFFSET(&dva[d], offset);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_GANG(&dva[d], 0);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_ASIZE(&dva[d], asize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_rotor = mg->mg_next;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mc->mc_allocated = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens } while ((mg = mg->mg_next) != rotor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm if (!all_zero) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm dshift++;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT(dshift < 64);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm goto top;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm bzero(&dva[d], sizeof (dva_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (ENOSPC);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Free the block represented by DVA in the context of the specified
fa9e4066f08beec538e775443c5be79dd423fcabahrens * transaction group.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickstatic void
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t vdev = DVA_GET_VDEV(dva);
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t offset = DVA_GET_OFFSET(dva);
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t size = DVA_GET_ASIZE(dva);
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens metaslab_t *msp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(DVA_IS_VALID(dva));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (txg > spa_freeze_txg(spa))
fa9e4066f08beec538e775443c5be79dd423fcabahrens return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick (u_longlong_t)vdev, (u_longlong_t)offset);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (DVA_GET_GANG(dva))
fa9e4066f08beec538e775443c5be79dd423fcabahrens size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (now) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_remove(&msp->ms_allocmap[txg & TXG_MASK],
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick offset, size);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_free(&msp->ms_map, offset, size);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick } else {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (msp->ms_freemap[txg & TXG_MASK].sm_space == 0)
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick vdev_dirty(vd, VDD_METASLAB, msp, txg);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick space_map_add(&msp->ms_freemap[txg & TXG_MASK], offset, size);
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens /*
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens * verify that this region is actually allocated in
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens * either a ms_allocmap or the ms_map
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens */
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens if (msp->ms_map.sm_loaded) {
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens boolean_t allocd = B_FALSE;
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens int i;
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens if (!space_map_contains(&msp->ms_map, offset, size)) {
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens allocd = B_TRUE;
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens } else {
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens for (i = 0; i < TXG_CONCURRENT_STATES; i++) {
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens space_map_t *sm = &msp->ms_allocmap
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens [(txg - i) & TXG_MASK];
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens if (space_map_contains(sm,
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens offset, size)) {
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens allocd = B_TRUE;
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens break;
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens }
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens }
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens }
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens if (!allocd) {
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens zfs_panic_recover("freeing free segment "
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens "(vdev=%llu offset=%llx size=%llx)",
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens (longlong_t)vdev, (longlong_t)offset,
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens (longlong_t)size);
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens }
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens }
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens
0125049cd6136d1d2ca9e982382a915b6d7916ceahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&msp->ms_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick/*
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * Intent log support: upon opening the pool after a crash, notify the SPA
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * of blocks that the intent log has allocated for immediate write, but
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * which are still considered free by the SPA because the last transaction
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick * group didn't commit yet.
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick */
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickstatic int
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick uint64_t vdev = DVA_GET_VDEV(dva);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick uint64_t offset = DVA_GET_OFFSET(dva);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick uint64_t size = DVA_GET_ASIZE(dva);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick vdev_t *vd;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick metaslab_t *msp;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int error;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(DVA_IS_VALID(dva));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (ENXIO);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if (DVA_GET_GANG(dva))
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick mutex_enter(&msp->ms_lock);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if (error) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick mutex_exit(&msp->ms_lock);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (error);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick vdev_dirty(vd, VDD_METASLAB, msp, txg);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick space_map_claim(&msp->ms_map, offset, size);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, size);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick mutex_exit(&msp->ms_lock);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickint
8654d0253136055bd4cc2423d87378e8a37f2eb5perrinmetaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin int ndvas, uint64_t txg, blkptr_t *hintbp, boolean_t hintbp_avoid)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick dva_t *dva = bp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick dva_t *hintdva = hintbp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int d;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int error = 0;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin if (mc->mc_rotor == NULL) /* no vdevs in this class */
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin return (ENOSPC);
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(BP_GET_NDVAS(bp) == 0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick for (d = 0; d < ndvas; d++) {
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
67bd71c6cc629bab3aa0d595c624a667f1574254perrin txg, hintbp_avoid);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if (error) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick for (d--; d >= 0; d--) {
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick metaslab_free_dva(spa, &dva[d], txg, B_TRUE);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick bzero(&dva[d], sizeof (dva_t));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (error);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick }
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(error == 0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(BP_GET_NDVAS(bp) == ndvas);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (0);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickvoid
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick const dva_t *dva = bp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int ndvas = BP_GET_NDVAS(bp);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int d;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(!BP_IS_HOLE(bp));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick for (d = 0; d < ndvas; d++)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick metaslab_free_dva(spa, &dva[d], txg, now);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickint
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwickmetaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick{
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick const dva_t *dva = bp->blk_dva;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int ndvas = BP_GET_NDVAS(bp);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int d, error;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick int last_error = 0;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick ASSERT(!BP_IS_HOLE(bp));
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick for (d = 0; d < ndvas; d++)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick last_error = error;
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick return (last_error);
d80c45e0f58fa434ba37259ea2e2b12e0380c19abonwick}