xref: /freebsd-11-stable/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/metaslab.c (revision 5405301b5cb28c1451c87e77e1c2ae57111ba5e3)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  */
27 
28 #include <sys/zfs_context.h>
29 #include <sys/dmu.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/space_map.h>
32 #include <sys/metaslab_impl.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/zio.h>
35 #include <sys/spa_impl.h>
36 #include <sys/zfeature.h>
37 #include <sys/vdev_indirect_mapping.h>
38 #include <sys/zap.h>
39 
40 SYSCTL_DECL(_vfs_zfs);
41 SYSCTL_NODE(_vfs_zfs, OID_AUTO, metaslab, CTLFLAG_RW, 0, "ZFS metaslab");
42 
43 #define	GANG_ALLOCATION(flags) \
44 	((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER))
45 
46 uint64_t metaslab_aliquot = 512ULL << 10;
47 uint64_t metaslab_force_ganging = SPA_MAXBLOCKSIZE + 1;	/* force gang blocks */
48 SYSCTL_QUAD(_vfs_zfs_metaslab, OID_AUTO, force_ganging, CTLFLAG_RWTUN,
49     &metaslab_force_ganging, 0,
50     "Force gang block allocation for blocks larger than or equal to this value");
51 
52 /*
53  * Since we can touch multiple metaslabs (and their respective space maps)
54  * with each transaction group, we benefit from having a smaller space map
55  * block size since it allows us to issue more I/O operations scattered
56  * around the disk.
57  */
58 int zfs_metaslab_sm_blksz = (1 << 12);
59 SYSCTL_INT(_vfs_zfs, OID_AUTO, metaslab_sm_blksz, CTLFLAG_RDTUN,
60     &zfs_metaslab_sm_blksz, 0,
61     "Block size for metaslab DTL space map.  Power of 2 and greater than 4096.");
62 
63 /*
64  * The in-core space map representation is more compact than its on-disk form.
65  * The zfs_condense_pct determines how much more compact the in-core
66  * space map representation must be before we compact it on-disk.
67  * Values should be greater than or equal to 100.
68  */
69 int zfs_condense_pct = 200;
70 SYSCTL_INT(_vfs_zfs, OID_AUTO, condense_pct, CTLFLAG_RWTUN,
71     &zfs_condense_pct, 0,
72     "Condense on-disk spacemap when it is more than this many percents"
73     " of in-memory counterpart");
74 
75 /*
76  * Condensing a metaslab is not guaranteed to actually reduce the amount of
77  * space used on disk. In particular, a space map uses data in increments of
78  * MAX(1 << ashift, space_map_blksize), so a metaslab might use the
79  * same number of blocks after condensing. Since the goal of condensing is to
80  * reduce the number of IOPs required to read the space map, we only want to
81  * condense when we can be sure we will reduce the number of blocks used by the
82  * space map. Unfortunately, we cannot precisely compute whether or not this is
83  * the case in metaslab_should_condense since we are holding ms_lock. Instead,
84  * we apply the following heuristic: do not condense a spacemap unless the
85  * uncondensed size consumes greater than zfs_metaslab_condense_block_threshold
86  * blocks.
87  */
88 int zfs_metaslab_condense_block_threshold = 4;
89 
90 /*
91  * The zfs_mg_noalloc_threshold defines which metaslab groups should
92  * be eligible for allocation. The value is defined as a percentage of
93  * free space. Metaslab groups that have more free space than
94  * zfs_mg_noalloc_threshold are always eligible for allocations. Once
95  * a metaslab group's free space is less than or equal to the
96  * zfs_mg_noalloc_threshold the allocator will avoid allocating to that
97  * group unless all groups in the pool have reached zfs_mg_noalloc_threshold.
98  * Once all groups in the pool reach zfs_mg_noalloc_threshold then all
99  * groups are allowed to accept allocations. Gang blocks are always
100  * eligible to allocate on any metaslab group. The default value of 0 means
101  * no metaslab group will be excluded based on this criterion.
102  */
103 int zfs_mg_noalloc_threshold = 0;
104 SYSCTL_INT(_vfs_zfs, OID_AUTO, mg_noalloc_threshold, CTLFLAG_RWTUN,
105     &zfs_mg_noalloc_threshold, 0,
106     "Percentage of metaslab group size that should be free"
107     " to make it eligible for allocation");
108 
109 /*
110  * Metaslab groups are considered eligible for allocations if their
111  * fragmenation metric (measured as a percentage) is less than or equal to
112  * zfs_mg_fragmentation_threshold. If a metaslab group exceeds this threshold
113  * then it will be skipped unless all metaslab groups within the metaslab
114  * class have also crossed this threshold.
115  */
116 int zfs_mg_fragmentation_threshold = 85;
117 SYSCTL_INT(_vfs_zfs, OID_AUTO, mg_fragmentation_threshold, CTLFLAG_RWTUN,
118     &zfs_mg_fragmentation_threshold, 0,
119     "Percentage of metaslab group size that should be considered "
120     "eligible for allocations unless all metaslab groups within the metaslab class "
121     "have also crossed this threshold");
122 
123 /*
124  * Allow metaslabs to keep their active state as long as their fragmentation
125  * percentage is less than or equal to zfs_metaslab_fragmentation_threshold. An
126  * active metaslab that exceeds this threshold will no longer keep its active
127  * status allowing better metaslabs to be selected.
128  */
129 int zfs_metaslab_fragmentation_threshold = 70;
130 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, fragmentation_threshold, CTLFLAG_RWTUN,
131     &zfs_metaslab_fragmentation_threshold, 0,
132     "Maximum percentage of metaslab fragmentation level to keep their active state");
133 
134 /*
135  * When set will load all metaslabs when pool is first opened.
136  */
137 int metaslab_debug_load = 0;
138 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, debug_load, CTLFLAG_RWTUN,
139     &metaslab_debug_load, 0,
140     "Load all metaslabs when pool is first opened");
141 
142 /*
143  * When set will prevent metaslabs from being unloaded.
144  */
145 int metaslab_debug_unload = 0;
146 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, debug_unload, CTLFLAG_RWTUN,
147     &metaslab_debug_unload, 0,
148     "Prevent metaslabs from being unloaded");
149 
150 /*
151  * Minimum size which forces the dynamic allocator to change
152  * it's allocation strategy.  Once the space map cannot satisfy
153  * an allocation of this size then it switches to using more
154  * aggressive strategy (i.e search by size rather than offset).
155  */
156 uint64_t metaslab_df_alloc_threshold = SPA_OLD_MAXBLOCKSIZE;
157 SYSCTL_QUAD(_vfs_zfs_metaslab, OID_AUTO, df_alloc_threshold, CTLFLAG_RWTUN,
158     &metaslab_df_alloc_threshold, 0,
159     "Minimum size which forces the dynamic allocator to change it's allocation strategy");
160 
161 /*
162  * The minimum free space, in percent, which must be available
163  * in a space map to continue allocations in a first-fit fashion.
164  * Once the space map's free space drops below this level we dynamically
165  * switch to using best-fit allocations.
166  */
167 int metaslab_df_free_pct = 4;
168 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, df_free_pct, CTLFLAG_RWTUN,
169     &metaslab_df_free_pct, 0,
170     "The minimum free space, in percent, which must be available in a "
171     "space map to continue allocations in a first-fit fashion");
172 
173 /*
174  * A metaslab is considered "free" if it contains a contiguous
175  * segment which is greater than metaslab_min_alloc_size.
176  */
177 uint64_t metaslab_min_alloc_size = DMU_MAX_ACCESS;
178 SYSCTL_QUAD(_vfs_zfs_metaslab, OID_AUTO, min_alloc_size, CTLFLAG_RWTUN,
179     &metaslab_min_alloc_size, 0,
180     "A metaslab is considered \"free\" if it contains a contiguous "
181     "segment which is greater than vfs.zfs.metaslab.min_alloc_size");
182 
183 /*
184  * Percentage of all cpus that can be used by the metaslab taskq.
185  */
186 int metaslab_load_pct = 50;
187 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, load_pct, CTLFLAG_RWTUN,
188     &metaslab_load_pct, 0,
189     "Percentage of cpus that can be used by the metaslab taskq");
190 
191 /*
192  * Determines how many txgs a metaslab may remain loaded without having any
193  * allocations from it. As long as a metaslab continues to be used we will
194  * keep it loaded.
195  */
196 int metaslab_unload_delay = TXG_SIZE * 2;
197 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, unload_delay, CTLFLAG_RWTUN,
198     &metaslab_unload_delay, 0,
199     "Number of TXGs that an unused metaslab can be kept in memory");
200 
201 /*
202  * Max number of metaslabs per group to preload.
203  */
204 int metaslab_preload_limit = SPA_DVAS_PER_BP;
205 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, preload_limit, CTLFLAG_RWTUN,
206     &metaslab_preload_limit, 0,
207     "Max number of metaslabs per group to preload");
208 
209 /*
210  * Enable/disable preloading of metaslab.
211  */
212 boolean_t metaslab_preload_enabled = B_TRUE;
213 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, preload_enabled, CTLFLAG_RWTUN,
214     &metaslab_preload_enabled, 0,
215     "Max number of metaslabs per group to preload");
216 
217 /*
218  * Enable/disable fragmentation weighting on metaslabs.
219  */
220 boolean_t metaslab_fragmentation_factor_enabled = B_TRUE;
221 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, fragmentation_factor_enabled, CTLFLAG_RWTUN,
222     &metaslab_fragmentation_factor_enabled, 0,
223     "Enable fragmentation weighting on metaslabs");
224 
225 /*
226  * Enable/disable lba weighting (i.e. outer tracks are given preference).
227  */
228 boolean_t metaslab_lba_weighting_enabled = B_TRUE;
229 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, lba_weighting_enabled, CTLFLAG_RWTUN,
230     &metaslab_lba_weighting_enabled, 0,
231     "Enable LBA weighting (i.e. outer tracks are given preference)");
232 
233 /*
234  * Enable/disable metaslab group biasing.
235  */
236 boolean_t metaslab_bias_enabled = B_TRUE;
237 SYSCTL_INT(_vfs_zfs_metaslab, OID_AUTO, bias_enabled, CTLFLAG_RWTUN,
238     &metaslab_bias_enabled, 0,
239     "Enable metaslab group biasing");
240 
241 /*
242  * Enable/disable remapping of indirect DVAs to their concrete vdevs.
243  */
244 boolean_t zfs_remap_blkptr_enable = B_TRUE;
245 
246 /*
247  * Enable/disable segment-based metaslab selection.
248  */
249 boolean_t zfs_metaslab_segment_weight_enabled = B_TRUE;
250 
251 /*
252  * When using segment-based metaslab selection, we will continue
253  * allocating from the active metaslab until we have exhausted
254  * zfs_metaslab_switch_threshold of its buckets.
255  */
256 int zfs_metaslab_switch_threshold = 2;
257 
258 /*
259  * Internal switch to enable/disable the metaslab allocation tracing
260  * facility.
261  */
262 boolean_t metaslab_trace_enabled = B_TRUE;
263 
264 /*
265  * Maximum entries that the metaslab allocation tracing facility will keep
266  * in a given list when running in non-debug mode. We limit the number
267  * of entries in non-debug mode to prevent us from using up too much memory.
268  * The limit should be sufficiently large that we don't expect any allocation
269  * to every exceed this value. In debug mode, the system will panic if this
270  * limit is ever reached allowing for further investigation.
271  */
272 uint64_t metaslab_trace_max_entries = 5000;
273 
274 static uint64_t metaslab_weight(metaslab_t *);
275 static void metaslab_set_fragmentation(metaslab_t *);
276 static void metaslab_free_impl(vdev_t *, uint64_t, uint64_t, boolean_t);
277 static void metaslab_check_free_impl(vdev_t *, uint64_t, uint64_t);
278 static void metaslab_passivate(metaslab_t *msp, uint64_t weight);
279 static uint64_t metaslab_weight_from_range_tree(metaslab_t *msp);
280 
281 kmem_cache_t *metaslab_alloc_trace_cache;
282 
283 /*
284  * ==========================================================================
285  * Metaslab classes
286  * ==========================================================================
287  */
288 metaslab_class_t *
metaslab_class_create(spa_t * spa,metaslab_ops_t * ops)289 metaslab_class_create(spa_t *spa, metaslab_ops_t *ops)
290 {
291 	metaslab_class_t *mc;
292 
293 	mc = kmem_zalloc(sizeof (metaslab_class_t), KM_SLEEP);
294 
295 	mc->mc_spa = spa;
296 	mc->mc_rotor = NULL;
297 	mc->mc_ops = ops;
298 	mutex_init(&mc->mc_lock, NULL, MUTEX_DEFAULT, NULL);
299 	mc->mc_alloc_slots = kmem_zalloc(spa->spa_alloc_count *
300 	    sizeof (refcount_t), KM_SLEEP);
301 	mc->mc_alloc_max_slots = kmem_zalloc(spa->spa_alloc_count *
302 	    sizeof (uint64_t), KM_SLEEP);
303 	for (int i = 0; i < spa->spa_alloc_count; i++)
304 		refcount_create_tracked(&mc->mc_alloc_slots[i]);
305 
306 	return (mc);
307 }
308 
309 void
metaslab_class_destroy(metaslab_class_t * mc)310 metaslab_class_destroy(metaslab_class_t *mc)
311 {
312 	ASSERT(mc->mc_rotor == NULL);
313 	ASSERT(mc->mc_alloc == 0);
314 	ASSERT(mc->mc_deferred == 0);
315 	ASSERT(mc->mc_space == 0);
316 	ASSERT(mc->mc_dspace == 0);
317 
318 	for (int i = 0; i < mc->mc_spa->spa_alloc_count; i++)
319 		refcount_destroy(&mc->mc_alloc_slots[i]);
320 	kmem_free(mc->mc_alloc_slots, mc->mc_spa->spa_alloc_count *
321 	    sizeof (refcount_t));
322 	kmem_free(mc->mc_alloc_max_slots, mc->mc_spa->spa_alloc_count *
323 	    sizeof (uint64_t));
324 	mutex_destroy(&mc->mc_lock);
325 	kmem_free(mc, sizeof (metaslab_class_t));
326 }
327 
328 int
metaslab_class_validate(metaslab_class_t * mc)329 metaslab_class_validate(metaslab_class_t *mc)
330 {
331 	metaslab_group_t *mg;
332 	vdev_t *vd;
333 
334 	/*
335 	 * Must hold one of the spa_config locks.
336 	 */
337 	ASSERT(spa_config_held(mc->mc_spa, SCL_ALL, RW_READER) ||
338 	    spa_config_held(mc->mc_spa, SCL_ALL, RW_WRITER));
339 
340 	if ((mg = mc->mc_rotor) == NULL)
341 		return (0);
342 
343 	do {
344 		vd = mg->mg_vd;
345 		ASSERT(vd->vdev_mg != NULL);
346 		ASSERT3P(vd->vdev_top, ==, vd);
347 		ASSERT3P(mg->mg_class, ==, mc);
348 		ASSERT3P(vd->vdev_ops, !=, &vdev_hole_ops);
349 	} while ((mg = mg->mg_next) != mc->mc_rotor);
350 
351 	return (0);
352 }
353 
354 void
metaslab_class_space_update(metaslab_class_t * mc,int64_t alloc_delta,int64_t defer_delta,int64_t space_delta,int64_t dspace_delta)355 metaslab_class_space_update(metaslab_class_t *mc, int64_t alloc_delta,
356     int64_t defer_delta, int64_t space_delta, int64_t dspace_delta)
357 {
358 	atomic_add_64(&mc->mc_alloc, alloc_delta);
359 	atomic_add_64(&mc->mc_deferred, defer_delta);
360 	atomic_add_64(&mc->mc_space, space_delta);
361 	atomic_add_64(&mc->mc_dspace, dspace_delta);
362 }
363 
364 void
metaslab_class_minblocksize_update(metaslab_class_t * mc)365 metaslab_class_minblocksize_update(metaslab_class_t *mc)
366 {
367 	metaslab_group_t *mg;
368 	vdev_t *vd;
369 	uint64_t minashift = UINT64_MAX;
370 
371 	if ((mg = mc->mc_rotor) == NULL) {
372 		mc->mc_minblocksize = SPA_MINBLOCKSIZE;
373 		return;
374 	}
375 
376 	do {
377 		vd = mg->mg_vd;
378 		if (vd->vdev_ashift < minashift)
379 			minashift = vd->vdev_ashift;
380 	} while ((mg = mg->mg_next) != mc->mc_rotor);
381 
382 	mc->mc_minblocksize = 1ULL << minashift;
383 }
384 
385 uint64_t
metaslab_class_get_alloc(metaslab_class_t * mc)386 metaslab_class_get_alloc(metaslab_class_t *mc)
387 {
388 	return (mc->mc_alloc);
389 }
390 
391 uint64_t
metaslab_class_get_deferred(metaslab_class_t * mc)392 metaslab_class_get_deferred(metaslab_class_t *mc)
393 {
394 	return (mc->mc_deferred);
395 }
396 
397 uint64_t
metaslab_class_get_space(metaslab_class_t * mc)398 metaslab_class_get_space(metaslab_class_t *mc)
399 {
400 	return (mc->mc_space);
401 }
402 
403 uint64_t
metaslab_class_get_dspace(metaslab_class_t * mc)404 metaslab_class_get_dspace(metaslab_class_t *mc)
405 {
406 	return (spa_deflate(mc->mc_spa) ? mc->mc_dspace : mc->mc_space);
407 }
408 
409 uint64_t
metaslab_class_get_minblocksize(metaslab_class_t * mc)410 metaslab_class_get_minblocksize(metaslab_class_t *mc)
411 {
412 	return (mc->mc_minblocksize);
413 }
414 
415 void
metaslab_class_histogram_verify(metaslab_class_t * mc)416 metaslab_class_histogram_verify(metaslab_class_t *mc)
417 {
418 	vdev_t *rvd = mc->mc_spa->spa_root_vdev;
419 	uint64_t *mc_hist;
420 	int i;
421 
422 	if ((zfs_flags & ZFS_DEBUG_HISTOGRAM_VERIFY) == 0)
423 		return;
424 
425 	mc_hist = kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE,
426 	    KM_SLEEP);
427 
428 	for (int c = 0; c < rvd->vdev_children; c++) {
429 		vdev_t *tvd = rvd->vdev_child[c];
430 		metaslab_group_t *mg = tvd->vdev_mg;
431 
432 		/*
433 		 * Skip any holes, uninitialized top-levels, or
434 		 * vdevs that are not in this metalab class.
435 		 */
436 		if (!vdev_is_concrete(tvd) || tvd->vdev_ms_shift == 0 ||
437 		    mg->mg_class != mc) {
438 			continue;
439 		}
440 
441 		for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
442 			mc_hist[i] += mg->mg_histogram[i];
443 	}
444 
445 	for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
446 		VERIFY3U(mc_hist[i], ==, mc->mc_histogram[i]);
447 
448 	kmem_free(mc_hist, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE);
449 }
450 
451 /*
452  * Calculate the metaslab class's fragmentation metric. The metric
453  * is weighted based on the space contribution of each metaslab group.
454  * The return value will be a number between 0 and 100 (inclusive), or
455  * ZFS_FRAG_INVALID if the metric has not been set. See comment above the
456  * zfs_frag_table for more information about the metric.
457  */
458 uint64_t
metaslab_class_fragmentation(metaslab_class_t * mc)459 metaslab_class_fragmentation(metaslab_class_t *mc)
460 {
461 	vdev_t *rvd = mc->mc_spa->spa_root_vdev;
462 	uint64_t fragmentation = 0;
463 
464 	spa_config_enter(mc->mc_spa, SCL_VDEV, FTAG, RW_READER);
465 
466 	for (int c = 0; c < rvd->vdev_children; c++) {
467 		vdev_t *tvd = rvd->vdev_child[c];
468 		metaslab_group_t *mg = tvd->vdev_mg;
469 
470 		/*
471 		 * Skip any holes, uninitialized top-levels,
472 		 * or vdevs that are not in this metalab class.
473 		 */
474 		if (!vdev_is_concrete(tvd) || tvd->vdev_ms_shift == 0 ||
475 		    mg->mg_class != mc) {
476 			continue;
477 		}
478 
479 		/*
480 		 * If a metaslab group does not contain a fragmentation
481 		 * metric then just bail out.
482 		 */
483 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
484 			spa_config_exit(mc->mc_spa, SCL_VDEV, FTAG);
485 			return (ZFS_FRAG_INVALID);
486 		}
487 
488 		/*
489 		 * Determine how much this metaslab_group is contributing
490 		 * to the overall pool fragmentation metric.
491 		 */
492 		fragmentation += mg->mg_fragmentation *
493 		    metaslab_group_get_space(mg);
494 	}
495 	fragmentation /= metaslab_class_get_space(mc);
496 
497 	ASSERT3U(fragmentation, <=, 100);
498 	spa_config_exit(mc->mc_spa, SCL_VDEV, FTAG);
499 	return (fragmentation);
500 }
501 
502 /*
503  * Calculate the amount of expandable space that is available in
504  * this metaslab class. If a device is expanded then its expandable
505  * space will be the amount of allocatable space that is currently not
506  * part of this metaslab class.
507  */
508 uint64_t
metaslab_class_expandable_space(metaslab_class_t * mc)509 metaslab_class_expandable_space(metaslab_class_t *mc)
510 {
511 	vdev_t *rvd = mc->mc_spa->spa_root_vdev;
512 	uint64_t space = 0;
513 
514 	spa_config_enter(mc->mc_spa, SCL_VDEV, FTAG, RW_READER);
515 	for (int c = 0; c < rvd->vdev_children; c++) {
516 		uint64_t tspace;
517 		vdev_t *tvd = rvd->vdev_child[c];
518 		metaslab_group_t *mg = tvd->vdev_mg;
519 
520 		if (!vdev_is_concrete(tvd) || tvd->vdev_ms_shift == 0 ||
521 		    mg->mg_class != mc) {
522 			continue;
523 		}
524 
525 		/*
526 		 * Calculate if we have enough space to add additional
527 		 * metaslabs. We report the expandable space in terms
528 		 * of the metaslab size since that's the unit of expansion.
529 		 * Adjust by efi system partition size.
530 		 */
531 		tspace = tvd->vdev_max_asize - tvd->vdev_asize;
532 		if (tspace > mc->mc_spa->spa_bootsize) {
533 			tspace -= mc->mc_spa->spa_bootsize;
534 		}
535 		space += P2ALIGN(tspace, 1ULL << tvd->vdev_ms_shift);
536 	}
537 	spa_config_exit(mc->mc_spa, SCL_VDEV, FTAG);
538 	return (space);
539 }
540 
541 static int
metaslab_compare(const void * x1,const void * x2)542 metaslab_compare(const void *x1, const void *x2)
543 {
544 	const metaslab_t *m1 = (const metaslab_t *)x1;
545 	const metaslab_t *m2 = (const metaslab_t *)x2;
546 
547 	int sort1 = 0;
548 	int sort2 = 0;
549 	if (m1->ms_allocator != -1 && m1->ms_primary)
550 		sort1 = 1;
551 	else if (m1->ms_allocator != -1 && !m1->ms_primary)
552 		sort1 = 2;
553 	if (m2->ms_allocator != -1 && m2->ms_primary)
554 		sort2 = 1;
555 	else if (m2->ms_allocator != -1 && !m2->ms_primary)
556 		sort2 = 2;
557 
558 	/*
559 	 * Sort inactive metaslabs first, then primaries, then secondaries. When
560 	 * selecting a metaslab to allocate from, an allocator first tries its
561 	 * primary, then secondary active metaslab. If it doesn't have active
562 	 * metaslabs, or can't allocate from them, it searches for an inactive
563 	 * metaslab to activate. If it can't find a suitable one, it will steal
564 	 * a primary or secondary metaslab from another allocator.
565 	 */
566 	if (sort1 < sort2)
567 		return (-1);
568 	if (sort1 > sort2)
569 		return (1);
570 
571 	int cmp = AVL_CMP(m2->ms_weight, m1->ms_weight);
572 	if (likely(cmp))
573 		return (cmp);
574 
575 	IMPLY(AVL_CMP(m1->ms_start, m2->ms_start) == 0, m1 == m2);
576 
577 	return (AVL_CMP(m1->ms_start, m2->ms_start));
578 }
579 
580 /*
581  * Verify that the space accounting on disk matches the in-core range_trees.
582  */
583 void
metaslab_verify_space(metaslab_t * msp,uint64_t txg)584 metaslab_verify_space(metaslab_t *msp, uint64_t txg)
585 {
586 	spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
587 	uint64_t allocated = 0;
588 	uint64_t sm_free_space, msp_free_space;
589 
590 	ASSERT(MUTEX_HELD(&msp->ms_lock));
591 
592 	if ((zfs_flags & ZFS_DEBUG_METASLAB_VERIFY) == 0)
593 		return;
594 
595 	/*
596 	 * We can only verify the metaslab space when we're called
597 	 * from syncing context with a loaded metaslab that has an allocated
598 	 * space map. Calling this in non-syncing context does not
599 	 * provide a consistent view of the metaslab since we're performing
600 	 * allocations in the future.
601 	 */
602 	if (txg != spa_syncing_txg(spa) || msp->ms_sm == NULL ||
603 	    !msp->ms_loaded)
604 		return;
605 
606 	sm_free_space = msp->ms_size - space_map_allocated(msp->ms_sm) -
607 	    space_map_alloc_delta(msp->ms_sm);
608 
609 	/*
610 	 * Account for future allocations since we would have already
611 	 * deducted that space from the ms_freetree.
612 	 */
613 	for (int t = 0; t < TXG_CONCURRENT_STATES; t++) {
614 		allocated +=
615 		    range_tree_space(msp->ms_allocating[(txg + t) & TXG_MASK]);
616 	}
617 
618 	msp_free_space = range_tree_space(msp->ms_allocatable) + allocated +
619 	    msp->ms_deferspace + range_tree_space(msp->ms_freed);
620 
621 	VERIFY3U(sm_free_space, ==, msp_free_space);
622 }
623 
624 /*
625  * ==========================================================================
626  * Metaslab groups
627  * ==========================================================================
628  */
629 /*
630  * Update the allocatable flag and the metaslab group's capacity.
631  * The allocatable flag is set to true if the capacity is below
632  * the zfs_mg_noalloc_threshold or has a fragmentation value that is
633  * greater than zfs_mg_fragmentation_threshold. If a metaslab group
634  * transitions from allocatable to non-allocatable or vice versa then the
635  * metaslab group's class is updated to reflect the transition.
636  */
637 static void
metaslab_group_alloc_update(metaslab_group_t * mg)638 metaslab_group_alloc_update(metaslab_group_t *mg)
639 {
640 	vdev_t *vd = mg->mg_vd;
641 	metaslab_class_t *mc = mg->mg_class;
642 	vdev_stat_t *vs = &vd->vdev_stat;
643 	boolean_t was_allocatable;
644 	boolean_t was_initialized;
645 
646 	ASSERT(vd == vd->vdev_top);
647 	ASSERT3U(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_READER), ==,
648 	    SCL_ALLOC);
649 
650 	mutex_enter(&mg->mg_lock);
651 	was_allocatable = mg->mg_allocatable;
652 	was_initialized = mg->mg_initialized;
653 
654 	mg->mg_free_capacity = ((vs->vs_space - vs->vs_alloc) * 100) /
655 	    (vs->vs_space + 1);
656 
657 	mutex_enter(&mc->mc_lock);
658 
659 	/*
660 	 * If the metaslab group was just added then it won't
661 	 * have any space until we finish syncing out this txg.
662 	 * At that point we will consider it initialized and available
663 	 * for allocations.  We also don't consider non-activated
664 	 * metaslab groups (e.g. vdevs that are in the middle of being removed)
665 	 * to be initialized, because they can't be used for allocation.
666 	 */
667 	mg->mg_initialized = metaslab_group_initialized(mg);
668 	if (!was_initialized && mg->mg_initialized) {
669 		mc->mc_groups++;
670 	} else if (was_initialized && !mg->mg_initialized) {
671 		ASSERT3U(mc->mc_groups, >, 0);
672 		mc->mc_groups--;
673 	}
674 	if (mg->mg_initialized)
675 		mg->mg_no_free_space = B_FALSE;
676 
677 	/*
678 	 * A metaslab group is considered allocatable if it has plenty
679 	 * of free space or is not heavily fragmented. We only take
680 	 * fragmentation into account if the metaslab group has a valid
681 	 * fragmentation metric (i.e. a value between 0 and 100).
682 	 */
683 	mg->mg_allocatable = (mg->mg_activation_count > 0 &&
684 	    mg->mg_free_capacity > zfs_mg_noalloc_threshold &&
685 	    (mg->mg_fragmentation == ZFS_FRAG_INVALID ||
686 	    mg->mg_fragmentation <= zfs_mg_fragmentation_threshold));
687 
688 	/*
689 	 * The mc_alloc_groups maintains a count of the number of
690 	 * groups in this metaslab class that are still above the
691 	 * zfs_mg_noalloc_threshold. This is used by the allocating
692 	 * threads to determine if they should avoid allocations to
693 	 * a given group. The allocator will avoid allocations to a group
694 	 * if that group has reached or is below the zfs_mg_noalloc_threshold
695 	 * and there are still other groups that are above the threshold.
696 	 * When a group transitions from allocatable to non-allocatable or
697 	 * vice versa we update the metaslab class to reflect that change.
698 	 * When the mc_alloc_groups value drops to 0 that means that all
699 	 * groups have reached the zfs_mg_noalloc_threshold making all groups
700 	 * eligible for allocations. This effectively means that all devices
701 	 * are balanced again.
702 	 */
703 	if (was_allocatable && !mg->mg_allocatable)
704 		mc->mc_alloc_groups--;
705 	else if (!was_allocatable && mg->mg_allocatable)
706 		mc->mc_alloc_groups++;
707 	mutex_exit(&mc->mc_lock);
708 
709 	mutex_exit(&mg->mg_lock);
710 }
711 
712 metaslab_group_t *
metaslab_group_create(metaslab_class_t * mc,vdev_t * vd,int allocators)713 metaslab_group_create(metaslab_class_t *mc, vdev_t *vd, int allocators)
714 {
715 	metaslab_group_t *mg;
716 
717 	mg = kmem_zalloc(sizeof (metaslab_group_t), KM_SLEEP);
718 	mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
719 	mutex_init(&mg->mg_ms_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
720 	cv_init(&mg->mg_ms_initialize_cv, NULL, CV_DEFAULT, NULL);
721 	mg->mg_primaries = kmem_zalloc(allocators * sizeof (metaslab_t *),
722 	    KM_SLEEP);
723 	mg->mg_secondaries = kmem_zalloc(allocators * sizeof (metaslab_t *),
724 	    KM_SLEEP);
725 	avl_create(&mg->mg_metaslab_tree, metaslab_compare,
726 	    sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
727 	mg->mg_vd = vd;
728 	mg->mg_class = mc;
729 	mg->mg_activation_count = 0;
730 	mg->mg_initialized = B_FALSE;
731 	mg->mg_no_free_space = B_TRUE;
732 	mg->mg_allocators = allocators;
733 
734 	mg->mg_alloc_queue_depth = kmem_zalloc(allocators * sizeof (refcount_t),
735 	    KM_SLEEP);
736 	mg->mg_cur_max_alloc_queue_depth = kmem_zalloc(allocators *
737 	    sizeof (uint64_t), KM_SLEEP);
738 	for (int i = 0; i < allocators; i++) {
739 		refcount_create_tracked(&mg->mg_alloc_queue_depth[i]);
740 		mg->mg_cur_max_alloc_queue_depth[i] = 0;
741 	}
742 
743 	mg->mg_taskq = taskq_create("metaslab_group_taskq", metaslab_load_pct,
744 	    minclsyspri, 10, INT_MAX, TASKQ_THREADS_CPU_PCT);
745 
746 	return (mg);
747 }
748 
749 void
metaslab_group_destroy(metaslab_group_t * mg)750 metaslab_group_destroy(metaslab_group_t *mg)
751 {
752 	ASSERT(mg->mg_prev == NULL);
753 	ASSERT(mg->mg_next == NULL);
754 	/*
755 	 * We may have gone below zero with the activation count
756 	 * either because we never activated in the first place or
757 	 * because we're done, and possibly removing the vdev.
758 	 */
759 	ASSERT(mg->mg_activation_count <= 0);
760 
761 	taskq_destroy(mg->mg_taskq);
762 	avl_destroy(&mg->mg_metaslab_tree);
763 	kmem_free(mg->mg_primaries, mg->mg_allocators * sizeof (metaslab_t *));
764 	kmem_free(mg->mg_secondaries, mg->mg_allocators *
765 	    sizeof (metaslab_t *));
766 	mutex_destroy(&mg->mg_lock);
767 	mutex_destroy(&mg->mg_ms_initialize_lock);
768 	cv_destroy(&mg->mg_ms_initialize_cv);
769 
770 	for (int i = 0; i < mg->mg_allocators; i++) {
771 		refcount_destroy(&mg->mg_alloc_queue_depth[i]);
772 		mg->mg_cur_max_alloc_queue_depth[i] = 0;
773 	}
774 	kmem_free(mg->mg_alloc_queue_depth, mg->mg_allocators *
775 	    sizeof (refcount_t));
776 	kmem_free(mg->mg_cur_max_alloc_queue_depth, mg->mg_allocators *
777 	    sizeof (uint64_t));
778 
779 	kmem_free(mg, sizeof (metaslab_group_t));
780 }
781 
782 void
metaslab_group_activate(metaslab_group_t * mg)783 metaslab_group_activate(metaslab_group_t *mg)
784 {
785 	metaslab_class_t *mc = mg->mg_class;
786 	metaslab_group_t *mgprev, *mgnext;
787 
788 	ASSERT3U(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER), !=, 0);
789 
790 	ASSERT(mc->mc_rotor != mg);
791 	ASSERT(mg->mg_prev == NULL);
792 	ASSERT(mg->mg_next == NULL);
793 	ASSERT(mg->mg_activation_count <= 0);
794 
795 	if (++mg->mg_activation_count <= 0)
796 		return;
797 
798 	mg->mg_aliquot = metaslab_aliquot * MAX(1, mg->mg_vd->vdev_children);
799 	metaslab_group_alloc_update(mg);
800 
801 	if ((mgprev = mc->mc_rotor) == NULL) {
802 		mg->mg_prev = mg;
803 		mg->mg_next = mg;
804 	} else {
805 		mgnext = mgprev->mg_next;
806 		mg->mg_prev = mgprev;
807 		mg->mg_next = mgnext;
808 		mgprev->mg_next = mg;
809 		mgnext->mg_prev = mg;
810 	}
811 	mc->mc_rotor = mg;
812 	metaslab_class_minblocksize_update(mc);
813 }
814 
815 /*
816  * Passivate a metaslab group and remove it from the allocation rotor.
817  * Callers must hold both the SCL_ALLOC and SCL_ZIO lock prior to passivating
818  * a metaslab group. This function will momentarily drop spa_config_locks
819  * that are lower than the SCL_ALLOC lock (see comment below).
820  */
821 void
metaslab_group_passivate(metaslab_group_t * mg)822 metaslab_group_passivate(metaslab_group_t *mg)
823 {
824 	metaslab_class_t *mc = mg->mg_class;
825 	spa_t *spa = mc->mc_spa;
826 	metaslab_group_t *mgprev, *mgnext;
827 	int locks = spa_config_held(spa, SCL_ALL, RW_WRITER);
828 
829 	ASSERT3U(spa_config_held(spa, SCL_ALLOC | SCL_ZIO, RW_WRITER), ==,
830 	    (SCL_ALLOC | SCL_ZIO));
831 
832 	if (--mg->mg_activation_count != 0) {
833 		ASSERT(mc->mc_rotor != mg);
834 		ASSERT(mg->mg_prev == NULL);
835 		ASSERT(mg->mg_next == NULL);
836 		ASSERT(mg->mg_activation_count < 0);
837 		return;
838 	}
839 
840 	/*
841 	 * The spa_config_lock is an array of rwlocks, ordered as
842 	 * follows (from highest to lowest):
843 	 *	SCL_CONFIG > SCL_STATE > SCL_L2ARC > SCL_ALLOC >
844 	 *	SCL_ZIO > SCL_FREE > SCL_VDEV
845 	 * (For more information about the spa_config_lock see spa_misc.c)
846 	 * The higher the lock, the broader its coverage. When we passivate
847 	 * a metaslab group, we must hold both the SCL_ALLOC and the SCL_ZIO
848 	 * config locks. However, the metaslab group's taskq might be trying
849 	 * to preload metaslabs so we must drop the SCL_ZIO lock and any
850 	 * lower locks to allow the I/O to complete. At a minimum,
851 	 * we continue to hold the SCL_ALLOC lock, which prevents any future
852 	 * allocations from taking place and any changes to the vdev tree.
853 	 */
854 	spa_config_exit(spa, locks & ~(SCL_ZIO - 1), spa);
855 	taskq_wait(mg->mg_taskq);
856 	spa_config_enter(spa, locks & ~(SCL_ZIO - 1), spa, RW_WRITER);
857 	metaslab_group_alloc_update(mg);
858 	for (int i = 0; i < mg->mg_allocators; i++) {
859 		metaslab_t *msp = mg->mg_primaries[i];
860 		if (msp != NULL) {
861 			mutex_enter(&msp->ms_lock);
862 			metaslab_passivate(msp,
863 			    metaslab_weight_from_range_tree(msp));
864 			mutex_exit(&msp->ms_lock);
865 		}
866 		msp = mg->mg_secondaries[i];
867 		if (msp != NULL) {
868 			mutex_enter(&msp->ms_lock);
869 			metaslab_passivate(msp,
870 			    metaslab_weight_from_range_tree(msp));
871 			mutex_exit(&msp->ms_lock);
872 		}
873 	}
874 
875 	mgprev = mg->mg_prev;
876 	mgnext = mg->mg_next;
877 
878 	if (mg == mgnext) {
879 		mc->mc_rotor = NULL;
880 	} else {
881 		mc->mc_rotor = mgnext;
882 		mgprev->mg_next = mgnext;
883 		mgnext->mg_prev = mgprev;
884 	}
885 
886 	mg->mg_prev = NULL;
887 	mg->mg_next = NULL;
888 	metaslab_class_minblocksize_update(mc);
889 }
890 
891 boolean_t
metaslab_group_initialized(metaslab_group_t * mg)892 metaslab_group_initialized(metaslab_group_t *mg)
893 {
894 	vdev_t *vd = mg->mg_vd;
895 	vdev_stat_t *vs = &vd->vdev_stat;
896 
897 	return (vs->vs_space != 0 && mg->mg_activation_count > 0);
898 }
899 
900 uint64_t
metaslab_group_get_space(metaslab_group_t * mg)901 metaslab_group_get_space(metaslab_group_t *mg)
902 {
903 	return ((1ULL << mg->mg_vd->vdev_ms_shift) * mg->mg_vd->vdev_ms_count);
904 }
905 
906 void
metaslab_group_histogram_verify(metaslab_group_t * mg)907 metaslab_group_histogram_verify(metaslab_group_t *mg)
908 {
909 	uint64_t *mg_hist;
910 	vdev_t *vd = mg->mg_vd;
911 	uint64_t ashift = vd->vdev_ashift;
912 	int i;
913 
914 	if ((zfs_flags & ZFS_DEBUG_HISTOGRAM_VERIFY) == 0)
915 		return;
916 
917 	mg_hist = kmem_zalloc(sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE,
918 	    KM_SLEEP);
919 
920 	ASSERT3U(RANGE_TREE_HISTOGRAM_SIZE, >=,
921 	    SPACE_MAP_HISTOGRAM_SIZE + ashift);
922 
923 	for (int m = 0; m < vd->vdev_ms_count; m++) {
924 		metaslab_t *msp = vd->vdev_ms[m];
925 
926 		if (msp->ms_sm == NULL)
927 			continue;
928 
929 		for (i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++)
930 			mg_hist[i + ashift] +=
931 			    msp->ms_sm->sm_phys->smp_histogram[i];
932 	}
933 
934 	for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i ++)
935 		VERIFY3U(mg_hist[i], ==, mg->mg_histogram[i]);
936 
937 	kmem_free(mg_hist, sizeof (uint64_t) * RANGE_TREE_HISTOGRAM_SIZE);
938 }
939 
940 static void
metaslab_group_histogram_add(metaslab_group_t * mg,metaslab_t * msp)941 metaslab_group_histogram_add(metaslab_group_t *mg, metaslab_t *msp)
942 {
943 	metaslab_class_t *mc = mg->mg_class;
944 	uint64_t ashift = mg->mg_vd->vdev_ashift;
945 
946 	ASSERT(MUTEX_HELD(&msp->ms_lock));
947 	if (msp->ms_sm == NULL)
948 		return;
949 
950 	mutex_enter(&mg->mg_lock);
951 	for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
952 		mg->mg_histogram[i + ashift] +=
953 		    msp->ms_sm->sm_phys->smp_histogram[i];
954 		mc->mc_histogram[i + ashift] +=
955 		    msp->ms_sm->sm_phys->smp_histogram[i];
956 	}
957 	mutex_exit(&mg->mg_lock);
958 }
959 
960 void
metaslab_group_histogram_remove(metaslab_group_t * mg,metaslab_t * msp)961 metaslab_group_histogram_remove(metaslab_group_t *mg, metaslab_t *msp)
962 {
963 	metaslab_class_t *mc = mg->mg_class;
964 	uint64_t ashift = mg->mg_vd->vdev_ashift;
965 
966 	ASSERT(MUTEX_HELD(&msp->ms_lock));
967 	if (msp->ms_sm == NULL)
968 		return;
969 
970 	mutex_enter(&mg->mg_lock);
971 	for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
972 		ASSERT3U(mg->mg_histogram[i + ashift], >=,
973 		    msp->ms_sm->sm_phys->smp_histogram[i]);
974 		ASSERT3U(mc->mc_histogram[i + ashift], >=,
975 		    msp->ms_sm->sm_phys->smp_histogram[i]);
976 
977 		mg->mg_histogram[i + ashift] -=
978 		    msp->ms_sm->sm_phys->smp_histogram[i];
979 		mc->mc_histogram[i + ashift] -=
980 		    msp->ms_sm->sm_phys->smp_histogram[i];
981 	}
982 	mutex_exit(&mg->mg_lock);
983 }
984 
985 static void
metaslab_group_add(metaslab_group_t * mg,metaslab_t * msp)986 metaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
987 {
988 	ASSERT(msp->ms_group == NULL);
989 	mutex_enter(&mg->mg_lock);
990 	msp->ms_group = mg;
991 	msp->ms_weight = 0;
992 	avl_add(&mg->mg_metaslab_tree, msp);
993 	mutex_exit(&mg->mg_lock);
994 
995 	mutex_enter(&msp->ms_lock);
996 	metaslab_group_histogram_add(mg, msp);
997 	mutex_exit(&msp->ms_lock);
998 }
999 
1000 static void
metaslab_group_remove(metaslab_group_t * mg,metaslab_t * msp)1001 metaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
1002 {
1003 	mutex_enter(&msp->ms_lock);
1004 	metaslab_group_histogram_remove(mg, msp);
1005 	mutex_exit(&msp->ms_lock);
1006 
1007 	mutex_enter(&mg->mg_lock);
1008 	ASSERT(msp->ms_group == mg);
1009 	avl_remove(&mg->mg_metaslab_tree, msp);
1010 	msp->ms_group = NULL;
1011 	mutex_exit(&mg->mg_lock);
1012 }
1013 
1014 static void
metaslab_group_sort_impl(metaslab_group_t * mg,metaslab_t * msp,uint64_t weight)1015 metaslab_group_sort_impl(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
1016 {
1017 	ASSERT(MUTEX_HELD(&mg->mg_lock));
1018 	ASSERT(msp->ms_group == mg);
1019 	avl_remove(&mg->mg_metaslab_tree, msp);
1020 	msp->ms_weight = weight;
1021 	avl_add(&mg->mg_metaslab_tree, msp);
1022 
1023 }
1024 
1025 static void
metaslab_group_sort(metaslab_group_t * mg,metaslab_t * msp,uint64_t weight)1026 metaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
1027 {
1028 	/*
1029 	 * Although in principle the weight can be any value, in
1030 	 * practice we do not use values in the range [1, 511].
1031 	 */
1032 	ASSERT(weight >= SPA_MINBLOCKSIZE || weight == 0);
1033 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1034 
1035 	mutex_enter(&mg->mg_lock);
1036 	metaslab_group_sort_impl(mg, msp, weight);
1037 	mutex_exit(&mg->mg_lock);
1038 }
1039 
1040 /*
1041  * Calculate the fragmentation for a given metaslab group. We can use
1042  * a simple average here since all metaslabs within the group must have
1043  * the same size. The return value will be a value between 0 and 100
1044  * (inclusive), or ZFS_FRAG_INVALID if less than half of the metaslab in this
1045  * group have a fragmentation metric.
1046  */
1047 uint64_t
metaslab_group_fragmentation(metaslab_group_t * mg)1048 metaslab_group_fragmentation(metaslab_group_t *mg)
1049 {
1050 	vdev_t *vd = mg->mg_vd;
1051 	uint64_t fragmentation = 0;
1052 	uint64_t valid_ms = 0;
1053 
1054 	for (int m = 0; m < vd->vdev_ms_count; m++) {
1055 		metaslab_t *msp = vd->vdev_ms[m];
1056 
1057 		if (msp->ms_fragmentation == ZFS_FRAG_INVALID)
1058 			continue;
1059 
1060 		valid_ms++;
1061 		fragmentation += msp->ms_fragmentation;
1062 	}
1063 
1064 	if (valid_ms <= vd->vdev_ms_count / 2)
1065 		return (ZFS_FRAG_INVALID);
1066 
1067 	fragmentation /= valid_ms;
1068 	ASSERT3U(fragmentation, <=, 100);
1069 	return (fragmentation);
1070 }
1071 
1072 /*
1073  * Determine if a given metaslab group should skip allocations. A metaslab
1074  * group should avoid allocations if its free capacity is less than the
1075  * zfs_mg_noalloc_threshold or its fragmentation metric is greater than
1076  * zfs_mg_fragmentation_threshold and there is at least one metaslab group
1077  * that can still handle allocations. If the allocation throttle is enabled
1078  * then we skip allocations to devices that have reached their maximum
1079  * allocation queue depth unless the selected metaslab group is the only
1080  * eligible group remaining.
1081  */
1082 static boolean_t
metaslab_group_allocatable(metaslab_group_t * mg,metaslab_group_t * rotor,uint64_t psize,int allocator,int d)1083 metaslab_group_allocatable(metaslab_group_t *mg, metaslab_group_t *rotor,
1084     uint64_t psize, int allocator, int d)
1085 {
1086 	spa_t *spa = mg->mg_vd->vdev_spa;
1087 	metaslab_class_t *mc = mg->mg_class;
1088 
1089 	/*
1090 	 * We can only consider skipping this metaslab group if it's
1091 	 * in the normal metaslab class and there are other metaslab
1092 	 * groups to select from. Otherwise, we always consider it eligible
1093 	 * for allocations.
1094 	 */
1095 	if (mc != spa_normal_class(spa) || mc->mc_groups <= 1)
1096 		return (B_TRUE);
1097 
1098 	/*
1099 	 * If the metaslab group's mg_allocatable flag is set (see comments
1100 	 * in metaslab_group_alloc_update() for more information) and
1101 	 * the allocation throttle is disabled then allow allocations to this
1102 	 * device. However, if the allocation throttle is enabled then
1103 	 * check if we have reached our allocation limit (mg_alloc_queue_depth)
1104 	 * to determine if we should allow allocations to this metaslab group.
1105 	 * If all metaslab groups are no longer considered allocatable
1106 	 * (mc_alloc_groups == 0) or we're trying to allocate the smallest
1107 	 * gang block size then we allow allocations on this metaslab group
1108 	 * regardless of the mg_allocatable or throttle settings.
1109 	 */
1110 	if (mg->mg_allocatable) {
1111 		metaslab_group_t *mgp;
1112 		int64_t qdepth;
1113 		uint64_t qmax = mg->mg_cur_max_alloc_queue_depth[allocator];
1114 
1115 		if (!mc->mc_alloc_throttle_enabled)
1116 			return (B_TRUE);
1117 
1118 		/*
1119 		 * If this metaslab group does not have any free space, then
1120 		 * there is no point in looking further.
1121 		 */
1122 		if (mg->mg_no_free_space)
1123 			return (B_FALSE);
1124 
1125 		/*
1126 		 * Relax allocation throttling for ditto blocks.  Due to
1127 		 * random imbalances in allocation it tends to push copies
1128 		 * to one vdev, that looks a bit better at the moment.
1129 		 */
1130 		qmax = qmax * (4 + d) / 4;
1131 
1132 		qdepth = refcount_count(&mg->mg_alloc_queue_depth[allocator]);
1133 
1134 		/*
1135 		 * If this metaslab group is below its qmax or it's
1136 		 * the only allocatable metasable group, then attempt
1137 		 * to allocate from it.
1138 		 */
1139 		if (qdepth < qmax || mc->mc_alloc_groups == 1)
1140 			return (B_TRUE);
1141 		ASSERT3U(mc->mc_alloc_groups, >, 1);
1142 
1143 		/*
1144 		 * Since this metaslab group is at or over its qmax, we
1145 		 * need to determine if there are metaslab groups after this
1146 		 * one that might be able to handle this allocation. This is
1147 		 * racy since we can't hold the locks for all metaslab
1148 		 * groups at the same time when we make this check.
1149 		 */
1150 		for (mgp = mg->mg_next; mgp != rotor; mgp = mgp->mg_next) {
1151 			qmax = mgp->mg_cur_max_alloc_queue_depth[allocator];
1152 			qmax = qmax * (4 + d) / 4;
1153 			qdepth = refcount_count(
1154 			    &mgp->mg_alloc_queue_depth[allocator]);
1155 
1156 			/*
1157 			 * If there is another metaslab group that
1158 			 * might be able to handle the allocation, then
1159 			 * we return false so that we skip this group.
1160 			 */
1161 			if (qdepth < qmax && !mgp->mg_no_free_space)
1162 				return (B_FALSE);
1163 		}
1164 
1165 		/*
1166 		 * We didn't find another group to handle the allocation
1167 		 * so we can't skip this metaslab group even though
1168 		 * we are at or over our qmax.
1169 		 */
1170 		return (B_TRUE);
1171 
1172 	} else if (mc->mc_alloc_groups == 0 || psize == SPA_MINBLOCKSIZE) {
1173 		return (B_TRUE);
1174 	}
1175 	return (B_FALSE);
1176 }
1177 
1178 /*
1179  * ==========================================================================
1180  * Range tree callbacks
1181  * ==========================================================================
1182  */
1183 
1184 /*
1185  * Comparison function for the private size-ordered tree. Tree is sorted
1186  * by size, larger sizes at the end of the tree.
1187  */
1188 static int
metaslab_rangesize_compare(const void * x1,const void * x2)1189 metaslab_rangesize_compare(const void *x1, const void *x2)
1190 {
1191 	const range_seg_t *r1 = x1;
1192 	const range_seg_t *r2 = x2;
1193 	uint64_t rs_size1 = r1->rs_end - r1->rs_start;
1194 	uint64_t rs_size2 = r2->rs_end - r2->rs_start;
1195 
1196 	int cmp = AVL_CMP(rs_size1, rs_size2);
1197 	if (likely(cmp))
1198 		return (cmp);
1199 
1200 	if (r1->rs_start < r2->rs_start)
1201 		return (-1);
1202 
1203 	return (AVL_CMP(r1->rs_start, r2->rs_start));
1204 }
1205 
1206 /*
1207  * ==========================================================================
1208  * Common allocator routines
1209  * ==========================================================================
1210  */
1211 
1212 /*
1213  * Return the maximum contiguous segment within the metaslab.
1214  */
1215 uint64_t
metaslab_block_maxsize(metaslab_t * msp)1216 metaslab_block_maxsize(metaslab_t *msp)
1217 {
1218 	avl_tree_t *t = &msp->ms_allocatable_by_size;
1219 	range_seg_t *rs;
1220 
1221 	if (t == NULL || (rs = avl_last(t)) == NULL)
1222 		return (0ULL);
1223 
1224 	return (rs->rs_end - rs->rs_start);
1225 }
1226 
1227 static range_seg_t *
metaslab_block_find(avl_tree_t * t,uint64_t start,uint64_t size)1228 metaslab_block_find(avl_tree_t *t, uint64_t start, uint64_t size)
1229 {
1230 	range_seg_t *rs, rsearch;
1231 	avl_index_t where;
1232 
1233 	rsearch.rs_start = start;
1234 	rsearch.rs_end = start + size;
1235 
1236 	rs = avl_find(t, &rsearch, &where);
1237 	if (rs == NULL) {
1238 		rs = avl_nearest(t, where, AVL_AFTER);
1239 	}
1240 
1241 	return (rs);
1242 }
1243 
1244 /*
1245  * This is a helper function that can be used by the allocator to find
1246  * a suitable block to allocate. This will search the specified AVL
1247  * tree looking for a block that matches the specified criteria.
1248  */
1249 static uint64_t
metaslab_block_picker(avl_tree_t * t,uint64_t * cursor,uint64_t size,uint64_t align)1250 metaslab_block_picker(avl_tree_t *t, uint64_t *cursor, uint64_t size,
1251     uint64_t align)
1252 {
1253 	range_seg_t *rs = metaslab_block_find(t, *cursor, size);
1254 
1255 	while (rs != NULL) {
1256 		uint64_t offset = P2ROUNDUP(rs->rs_start, align);
1257 
1258 		if (offset + size <= rs->rs_end) {
1259 			*cursor = offset + size;
1260 			return (offset);
1261 		}
1262 		rs = AVL_NEXT(t, rs);
1263 	}
1264 
1265 	/*
1266 	 * If we know we've searched the whole map (*cursor == 0), give up.
1267 	 * Otherwise, reset the cursor to the beginning and try again.
1268 	 */
1269 	if (*cursor == 0)
1270 		return (-1ULL);
1271 
1272 	*cursor = 0;
1273 	return (metaslab_block_picker(t, cursor, size, align));
1274 }
1275 
1276 /*
1277  * ==========================================================================
1278  * The first-fit block allocator
1279  * ==========================================================================
1280  */
1281 static uint64_t
metaslab_ff_alloc(metaslab_t * msp,uint64_t size)1282 metaslab_ff_alloc(metaslab_t *msp, uint64_t size)
1283 {
1284 	/*
1285 	 * Find the largest power of 2 block size that evenly divides the
1286 	 * requested size. This is used to try to allocate blocks with similar
1287 	 * alignment from the same area of the metaslab (i.e. same cursor
1288 	 * bucket) but it does not guarantee that other allocations sizes
1289 	 * may exist in the same region.
1290 	 */
1291 	uint64_t align = size & -size;
1292 	uint64_t *cursor = &msp->ms_lbas[highbit64(align) - 1];
1293 	avl_tree_t *t = &msp->ms_allocatable->rt_root;
1294 
1295 	return (metaslab_block_picker(t, cursor, size, align));
1296 }
1297 
1298 static metaslab_ops_t metaslab_ff_ops = {
1299 	metaslab_ff_alloc
1300 };
1301 
1302 /*
1303  * ==========================================================================
1304  * Dynamic block allocator -
1305  * Uses the first fit allocation scheme until space get low and then
1306  * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
1307  * and metaslab_df_free_pct to determine when to switch the allocation scheme.
1308  * ==========================================================================
1309  */
1310 static uint64_t
metaslab_df_alloc(metaslab_t * msp,uint64_t size)1311 metaslab_df_alloc(metaslab_t *msp, uint64_t size)
1312 {
1313 	/*
1314 	 * Find the largest power of 2 block size that evenly divides the
1315 	 * requested size. This is used to try to allocate blocks with similar
1316 	 * alignment from the same area of the metaslab (i.e. same cursor
1317 	 * bucket) but it does not guarantee that other allocations sizes
1318 	 * may exist in the same region.
1319 	 */
1320 	uint64_t align = size & -size;
1321 	uint64_t *cursor = &msp->ms_lbas[highbit64(align) - 1];
1322 	range_tree_t *rt = msp->ms_allocatable;
1323 	avl_tree_t *t = &rt->rt_root;
1324 	uint64_t max_size = metaslab_block_maxsize(msp);
1325 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
1326 
1327 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1328 	ASSERT3U(avl_numnodes(t), ==,
1329 	    avl_numnodes(&msp->ms_allocatable_by_size));
1330 
1331 	if (max_size < size)
1332 		return (-1ULL);
1333 
1334 	/*
1335 	 * If we're running low on space switch to using the size
1336 	 * sorted AVL tree (best-fit).
1337 	 */
1338 	if (max_size < metaslab_df_alloc_threshold ||
1339 	    free_pct < metaslab_df_free_pct) {
1340 		t = &msp->ms_allocatable_by_size;
1341 		*cursor = 0;
1342 	}
1343 
1344 	return (metaslab_block_picker(t, cursor, size, 1ULL));
1345 }
1346 
1347 static metaslab_ops_t metaslab_df_ops = {
1348 	metaslab_df_alloc
1349 };
1350 
1351 /*
1352  * ==========================================================================
1353  * Cursor fit block allocator -
1354  * Select the largest region in the metaslab, set the cursor to the beginning
1355  * of the range and the cursor_end to the end of the range. As allocations
1356  * are made advance the cursor. Continue allocating from the cursor until
1357  * the range is exhausted and then find a new range.
1358  * ==========================================================================
1359  */
1360 static uint64_t
metaslab_cf_alloc(metaslab_t * msp,uint64_t size)1361 metaslab_cf_alloc(metaslab_t *msp, uint64_t size)
1362 {
1363 	range_tree_t *rt = msp->ms_allocatable;
1364 	avl_tree_t *t = &msp->ms_allocatable_by_size;
1365 	uint64_t *cursor = &msp->ms_lbas[0];
1366 	uint64_t *cursor_end = &msp->ms_lbas[1];
1367 	uint64_t offset = 0;
1368 
1369 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1370 	ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&rt->rt_root));
1371 
1372 	ASSERT3U(*cursor_end, >=, *cursor);
1373 
1374 	if ((*cursor + size) > *cursor_end) {
1375 		range_seg_t *rs;
1376 
1377 		rs = avl_last(&msp->ms_allocatable_by_size);
1378 		if (rs == NULL || (rs->rs_end - rs->rs_start) < size)
1379 			return (-1ULL);
1380 
1381 		*cursor = rs->rs_start;
1382 		*cursor_end = rs->rs_end;
1383 	}
1384 
1385 	offset = *cursor;
1386 	*cursor += size;
1387 
1388 	return (offset);
1389 }
1390 
1391 static metaslab_ops_t metaslab_cf_ops = {
1392 	metaslab_cf_alloc
1393 };
1394 
1395 /*
1396  * ==========================================================================
1397  * New dynamic fit allocator -
1398  * Select a region that is large enough to allocate 2^metaslab_ndf_clump_shift
1399  * contiguous blocks. If no region is found then just use the largest segment
1400  * that remains.
1401  * ==========================================================================
1402  */
1403 
1404 /*
1405  * Determines desired number of contiguous blocks (2^metaslab_ndf_clump_shift)
1406  * to request from the allocator.
1407  */
1408 uint64_t metaslab_ndf_clump_shift = 4;
1409 
1410 static uint64_t
metaslab_ndf_alloc(metaslab_t * msp,uint64_t size)1411 metaslab_ndf_alloc(metaslab_t *msp, uint64_t size)
1412 {
1413 	avl_tree_t *t = &msp->ms_allocatable->rt_root;
1414 	avl_index_t where;
1415 	range_seg_t *rs, rsearch;
1416 	uint64_t hbit = highbit64(size);
1417 	uint64_t *cursor = &msp->ms_lbas[hbit - 1];
1418 	uint64_t max_size = metaslab_block_maxsize(msp);
1419 
1420 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1421 	ASSERT3U(avl_numnodes(t), ==,
1422 	    avl_numnodes(&msp->ms_allocatable_by_size));
1423 
1424 	if (max_size < size)
1425 		return (-1ULL);
1426 
1427 	rsearch.rs_start = *cursor;
1428 	rsearch.rs_end = *cursor + size;
1429 
1430 	rs = avl_find(t, &rsearch, &where);
1431 	if (rs == NULL || (rs->rs_end - rs->rs_start) < size) {
1432 		t = &msp->ms_allocatable_by_size;
1433 
1434 		rsearch.rs_start = 0;
1435 		rsearch.rs_end = MIN(max_size,
1436 		    1ULL << (hbit + metaslab_ndf_clump_shift));
1437 		rs = avl_find(t, &rsearch, &where);
1438 		if (rs == NULL)
1439 			rs = avl_nearest(t, where, AVL_AFTER);
1440 		ASSERT(rs != NULL);
1441 	}
1442 
1443 	if ((rs->rs_end - rs->rs_start) >= size) {
1444 		*cursor = rs->rs_start + size;
1445 		return (rs->rs_start);
1446 	}
1447 	return (-1ULL);
1448 }
1449 
1450 static metaslab_ops_t metaslab_ndf_ops = {
1451 	metaslab_ndf_alloc
1452 };
1453 
1454 metaslab_ops_t *zfs_metaslab_ops = &metaslab_df_ops;
1455 
1456 /*
1457  * ==========================================================================
1458  * Metaslabs
1459  * ==========================================================================
1460  */
1461 
1462 /*
1463  * Wait for any in-progress metaslab loads to complete.
1464  */
1465 void
metaslab_load_wait(metaslab_t * msp)1466 metaslab_load_wait(metaslab_t *msp)
1467 {
1468 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1469 
1470 	while (msp->ms_loading) {
1471 		ASSERT(!msp->ms_loaded);
1472 		cv_wait(&msp->ms_load_cv, &msp->ms_lock);
1473 	}
1474 }
1475 
1476 int
metaslab_load(metaslab_t * msp)1477 metaslab_load(metaslab_t *msp)
1478 {
1479 	int error = 0;
1480 	boolean_t success = B_FALSE;
1481 
1482 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1483 	ASSERT(!msp->ms_loaded);
1484 	ASSERT(!msp->ms_loading);
1485 
1486 	msp->ms_loading = B_TRUE;
1487 	/*
1488 	 * Nobody else can manipulate a loading metaslab, so it's now safe
1489 	 * to drop the lock.  This way we don't have to hold the lock while
1490 	 * reading the spacemap from disk.
1491 	 */
1492 	mutex_exit(&msp->ms_lock);
1493 
1494 	/*
1495 	 * If the space map has not been allocated yet, then treat
1496 	 * all the space in the metaslab as free and add it to ms_allocatable.
1497 	 */
1498 	if (msp->ms_sm != NULL) {
1499 		error = space_map_load(msp->ms_sm, msp->ms_allocatable,
1500 		    SM_FREE);
1501 	} else {
1502 		range_tree_add(msp->ms_allocatable,
1503 		    msp->ms_start, msp->ms_size);
1504 	}
1505 
1506 	success = (error == 0);
1507 
1508 	mutex_enter(&msp->ms_lock);
1509 	msp->ms_loading = B_FALSE;
1510 
1511 	if (success) {
1512 		ASSERT3P(msp->ms_group, !=, NULL);
1513 		msp->ms_loaded = B_TRUE;
1514 
1515 		/*
1516 		 * If the metaslab already has a spacemap, then we need to
1517 		 * remove all segments from the defer tree; otherwise, the
1518 		 * metaslab is completely empty and we can skip this.
1519 		 */
1520 		if (msp->ms_sm != NULL) {
1521 			for (int t = 0; t < TXG_DEFER_SIZE; t++) {
1522 				range_tree_walk(msp->ms_defer[t],
1523 				    range_tree_remove, msp->ms_allocatable);
1524 			}
1525 		}
1526 		msp->ms_max_size = metaslab_block_maxsize(msp);
1527 	}
1528 	cv_broadcast(&msp->ms_load_cv);
1529 	return (error);
1530 }
1531 
1532 void
metaslab_unload(metaslab_t * msp)1533 metaslab_unload(metaslab_t *msp)
1534 {
1535 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1536 	range_tree_vacate(msp->ms_allocatable, NULL, NULL);
1537 	msp->ms_loaded = B_FALSE;
1538 	msp->ms_weight &= ~METASLAB_ACTIVE_MASK;
1539 	msp->ms_max_size = 0;
1540 }
1541 
1542 int
metaslab_init(metaslab_group_t * mg,uint64_t id,uint64_t object,uint64_t txg,metaslab_t ** msp)1543 metaslab_init(metaslab_group_t *mg, uint64_t id, uint64_t object, uint64_t txg,
1544     metaslab_t **msp)
1545 {
1546 	vdev_t *vd = mg->mg_vd;
1547 	objset_t *mos = vd->vdev_spa->spa_meta_objset;
1548 	metaslab_t *ms;
1549 	int error;
1550 
1551 	ms = kmem_zalloc(sizeof (metaslab_t), KM_SLEEP);
1552 	mutex_init(&ms->ms_lock, NULL, MUTEX_DEFAULT, NULL);
1553 	mutex_init(&ms->ms_sync_lock, NULL, MUTEX_DEFAULT, NULL);
1554 	cv_init(&ms->ms_load_cv, NULL, CV_DEFAULT, NULL);
1555 
1556 	ms->ms_id = id;
1557 	ms->ms_start = id << vd->vdev_ms_shift;
1558 	ms->ms_size = 1ULL << vd->vdev_ms_shift;
1559 	ms->ms_allocator = -1;
1560 	ms->ms_new = B_TRUE;
1561 
1562 	/*
1563 	 * We only open space map objects that already exist. All others
1564 	 * will be opened when we finally allocate an object for it.
1565 	 */
1566 	if (object != 0) {
1567 		error = space_map_open(&ms->ms_sm, mos, object, ms->ms_start,
1568 		    ms->ms_size, vd->vdev_ashift);
1569 
1570 		if (error != 0) {
1571 			kmem_free(ms, sizeof (metaslab_t));
1572 			return (error);
1573 		}
1574 
1575 		ASSERT(ms->ms_sm != NULL);
1576 	}
1577 
1578 	/*
1579 	 * We create the main range tree here, but we don't create the
1580 	 * other range trees until metaslab_sync_done().  This serves
1581 	 * two purposes: it allows metaslab_sync_done() to detect the
1582 	 * addition of new space; and for debugging, it ensures that we'd
1583 	 * data fault on any attempt to use this metaslab before it's ready.
1584 	 */
1585 	ms->ms_allocatable = range_tree_create_impl(&rt_avl_ops, &ms->ms_allocatable_by_size,
1586 	    metaslab_rangesize_compare, 0);
1587 	metaslab_group_add(mg, ms);
1588 
1589 	metaslab_set_fragmentation(ms);
1590 
1591 	/*
1592 	 * If we're opening an existing pool (txg == 0) or creating
1593 	 * a new one (txg == TXG_INITIAL), all space is available now.
1594 	 * If we're adding space to an existing pool, the new space
1595 	 * does not become available until after this txg has synced.
1596 	 * The metaslab's weight will also be initialized when we sync
1597 	 * out this txg. This ensures that we don't attempt to allocate
1598 	 * from it before we have initialized it completely.
1599 	 */
1600 	if (txg <= TXG_INITIAL)
1601 		metaslab_sync_done(ms, 0);
1602 
1603 	/*
1604 	 * If metaslab_debug_load is set and we're initializing a metaslab
1605 	 * that has an allocated space map object then load the its space
1606 	 * map so that can verify frees.
1607 	 */
1608 	if (metaslab_debug_load && ms->ms_sm != NULL) {
1609 		mutex_enter(&ms->ms_lock);
1610 		VERIFY0(metaslab_load(ms));
1611 		mutex_exit(&ms->ms_lock);
1612 	}
1613 
1614 	if (txg != 0) {
1615 		vdev_dirty(vd, 0, NULL, txg);
1616 		vdev_dirty(vd, VDD_METASLAB, ms, txg);
1617 	}
1618 
1619 	*msp = ms;
1620 
1621 	return (0);
1622 }
1623 
1624 void
metaslab_fini(metaslab_t * msp)1625 metaslab_fini(metaslab_t *msp)
1626 {
1627 	metaslab_group_t *mg = msp->ms_group;
1628 
1629 	metaslab_group_remove(mg, msp);
1630 
1631 	mutex_enter(&msp->ms_lock);
1632 	VERIFY(msp->ms_group == NULL);
1633 	vdev_space_update(mg->mg_vd, -space_map_allocated(msp->ms_sm),
1634 	    0, -msp->ms_size);
1635 	space_map_close(msp->ms_sm);
1636 
1637 	metaslab_unload(msp);
1638 	range_tree_destroy(msp->ms_allocatable);
1639 	range_tree_destroy(msp->ms_freeing);
1640 	range_tree_destroy(msp->ms_freed);
1641 
1642 	for (int t = 0; t < TXG_SIZE; t++) {
1643 		range_tree_destroy(msp->ms_allocating[t]);
1644 	}
1645 
1646 	for (int t = 0; t < TXG_DEFER_SIZE; t++) {
1647 		range_tree_destroy(msp->ms_defer[t]);
1648 	}
1649 	ASSERT0(msp->ms_deferspace);
1650 
1651 	range_tree_destroy(msp->ms_checkpointing);
1652 
1653 	mutex_exit(&msp->ms_lock);
1654 	cv_destroy(&msp->ms_load_cv);
1655 	mutex_destroy(&msp->ms_lock);
1656 	mutex_destroy(&msp->ms_sync_lock);
1657 	ASSERT3U(msp->ms_allocator, ==, -1);
1658 
1659 	kmem_free(msp, sizeof (metaslab_t));
1660 }
1661 
1662 #define	FRAGMENTATION_TABLE_SIZE	17
1663 
1664 /*
1665  * This table defines a segment size based fragmentation metric that will
1666  * allow each metaslab to derive its own fragmentation value. This is done
1667  * by calculating the space in each bucket of the spacemap histogram and
1668  * multiplying that by the fragmetation metric in this table. Doing
1669  * this for all buckets and dividing it by the total amount of free
1670  * space in this metaslab (i.e. the total free space in all buckets) gives
1671  * us the fragmentation metric. This means that a high fragmentation metric
1672  * equates to most of the free space being comprised of small segments.
1673  * Conversely, if the metric is low, then most of the free space is in
1674  * large segments. A 10% change in fragmentation equates to approximately
1675  * double the number of segments.
1676  *
1677  * This table defines 0% fragmented space using 16MB segments. Testing has
1678  * shown that segments that are greater than or equal to 16MB do not suffer
1679  * from drastic performance problems. Using this value, we derive the rest
1680  * of the table. Since the fragmentation value is never stored on disk, it
1681  * is possible to change these calculations in the future.
1682  */
1683 int zfs_frag_table[FRAGMENTATION_TABLE_SIZE] = {
1684 	100,	/* 512B	*/
1685 	100,	/* 1K	*/
1686 	98,	/* 2K	*/
1687 	95,	/* 4K	*/
1688 	90,	/* 8K	*/
1689 	80,	/* 16K	*/
1690 	70,	/* 32K	*/
1691 	60,	/* 64K	*/
1692 	50,	/* 128K	*/
1693 	40,	/* 256K	*/
1694 	30,	/* 512K	*/
1695 	20,	/* 1M	*/
1696 	15,	/* 2M	*/
1697 	10,	/* 4M	*/
1698 	5,	/* 8M	*/
1699 	0	/* 16M	*/
1700 };
1701 
1702 /*
1703  * Calclate the metaslab's fragmentation metric. A return value
1704  * of ZFS_FRAG_INVALID means that the metaslab has not been upgraded and does
1705  * not support this metric. Otherwise, the return value should be in the
1706  * range [0, 100].
1707  */
1708 static void
metaslab_set_fragmentation(metaslab_t * msp)1709 metaslab_set_fragmentation(metaslab_t *msp)
1710 {
1711 	spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
1712 	uint64_t fragmentation = 0;
1713 	uint64_t total = 0;
1714 	boolean_t feature_enabled = spa_feature_is_enabled(spa,
1715 	    SPA_FEATURE_SPACEMAP_HISTOGRAM);
1716 
1717 	if (!feature_enabled) {
1718 		msp->ms_fragmentation = ZFS_FRAG_INVALID;
1719 		return;
1720 	}
1721 
1722 	/*
1723 	 * A null space map means that the entire metaslab is free
1724 	 * and thus is not fragmented.
1725 	 */
1726 	if (msp->ms_sm == NULL) {
1727 		msp->ms_fragmentation = 0;
1728 		return;
1729 	}
1730 
1731 	/*
1732 	 * If this metaslab's space map has not been upgraded, flag it
1733 	 * so that we upgrade next time we encounter it.
1734 	 */
1735 	if (msp->ms_sm->sm_dbuf->db_size != sizeof (space_map_phys_t)) {
1736 		uint64_t txg = spa_syncing_txg(spa);
1737 		vdev_t *vd = msp->ms_group->mg_vd;
1738 
1739 		/*
1740 		 * If we've reached the final dirty txg, then we must
1741 		 * be shutting down the pool. We don't want to dirty
1742 		 * any data past this point so skip setting the condense
1743 		 * flag. We can retry this action the next time the pool
1744 		 * is imported.
1745 		 */
1746 		if (spa_writeable(spa) && txg < spa_final_dirty_txg(spa)) {
1747 			msp->ms_condense_wanted = B_TRUE;
1748 			vdev_dirty(vd, VDD_METASLAB, msp, txg + 1);
1749 			zfs_dbgmsg("txg %llu, requesting force condense: "
1750 			    "ms_id %llu, vdev_id %llu", txg, msp->ms_id,
1751 			    vd->vdev_id);
1752 		}
1753 		msp->ms_fragmentation = ZFS_FRAG_INVALID;
1754 		return;
1755 	}
1756 
1757 	for (int i = 0; i < SPACE_MAP_HISTOGRAM_SIZE; i++) {
1758 		uint64_t space = 0;
1759 		uint8_t shift = msp->ms_sm->sm_shift;
1760 
1761 		int idx = MIN(shift - SPA_MINBLOCKSHIFT + i,
1762 		    FRAGMENTATION_TABLE_SIZE - 1);
1763 
1764 		if (msp->ms_sm->sm_phys->smp_histogram[i] == 0)
1765 			continue;
1766 
1767 		space = msp->ms_sm->sm_phys->smp_histogram[i] << (i + shift);
1768 		total += space;
1769 
1770 		ASSERT3U(idx, <, FRAGMENTATION_TABLE_SIZE);
1771 		fragmentation += space * zfs_frag_table[idx];
1772 	}
1773 
1774 	if (total > 0)
1775 		fragmentation /= total;
1776 	ASSERT3U(fragmentation, <=, 100);
1777 
1778 	msp->ms_fragmentation = fragmentation;
1779 }
1780 
1781 /*
1782  * Compute a weight -- a selection preference value -- for the given metaslab.
1783  * This is based on the amount of free space, the level of fragmentation,
1784  * the LBA range, and whether the metaslab is loaded.
1785  */
1786 static uint64_t
metaslab_space_weight(metaslab_t * msp)1787 metaslab_space_weight(metaslab_t *msp)
1788 {
1789 	metaslab_group_t *mg = msp->ms_group;
1790 	vdev_t *vd = mg->mg_vd;
1791 	uint64_t weight, space;
1792 
1793 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1794 	ASSERT(!vd->vdev_removing);
1795 
1796 	/*
1797 	 * The baseline weight is the metaslab's free space.
1798 	 */
1799 	space = msp->ms_size - space_map_allocated(msp->ms_sm);
1800 
1801 	if (metaslab_fragmentation_factor_enabled &&
1802 	    msp->ms_fragmentation != ZFS_FRAG_INVALID) {
1803 		/*
1804 		 * Use the fragmentation information to inversely scale
1805 		 * down the baseline weight. We need to ensure that we
1806 		 * don't exclude this metaslab completely when it's 100%
1807 		 * fragmented. To avoid this we reduce the fragmented value
1808 		 * by 1.
1809 		 */
1810 		space = (space * (100 - (msp->ms_fragmentation - 1))) / 100;
1811 
1812 		/*
1813 		 * If space < SPA_MINBLOCKSIZE, then we will not allocate from
1814 		 * this metaslab again. The fragmentation metric may have
1815 		 * decreased the space to something smaller than
1816 		 * SPA_MINBLOCKSIZE, so reset the space to SPA_MINBLOCKSIZE
1817 		 * so that we can consume any remaining space.
1818 		 */
1819 		if (space > 0 && space < SPA_MINBLOCKSIZE)
1820 			space = SPA_MINBLOCKSIZE;
1821 	}
1822 	weight = space;
1823 
1824 	/*
1825 	 * Modern disks have uniform bit density and constant angular velocity.
1826 	 * Therefore, the outer recording zones are faster (higher bandwidth)
1827 	 * than the inner zones by the ratio of outer to inner track diameter,
1828 	 * which is typically around 2:1.  We account for this by assigning
1829 	 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
1830 	 * In effect, this means that we'll select the metaslab with the most
1831 	 * free bandwidth rather than simply the one with the most free space.
1832 	 */
1833 	if (!vd->vdev_nonrot && metaslab_lba_weighting_enabled) {
1834 		weight = 2 * weight - (msp->ms_id * weight) / vd->vdev_ms_count;
1835 		ASSERT(weight >= space && weight <= 2 * space);
1836 	}
1837 
1838 	/*
1839 	 * If this metaslab is one we're actively using, adjust its
1840 	 * weight to make it preferable to any inactive metaslab so
1841 	 * we'll polish it off. If the fragmentation on this metaslab
1842 	 * has exceed our threshold, then don't mark it active.
1843 	 */
1844 	if (msp->ms_loaded && msp->ms_fragmentation != ZFS_FRAG_INVALID &&
1845 	    msp->ms_fragmentation <= zfs_metaslab_fragmentation_threshold) {
1846 		weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
1847 	}
1848 
1849 	WEIGHT_SET_SPACEBASED(weight);
1850 	return (weight);
1851 }
1852 
1853 /*
1854  * Return the weight of the specified metaslab, according to the segment-based
1855  * weighting algorithm. The metaslab must be loaded. This function can
1856  * be called within a sync pass since it relies only on the metaslab's
1857  * range tree which is always accurate when the metaslab is loaded.
1858  */
1859 static uint64_t
metaslab_weight_from_range_tree(metaslab_t * msp)1860 metaslab_weight_from_range_tree(metaslab_t *msp)
1861 {
1862 	uint64_t weight = 0;
1863 	uint32_t segments = 0;
1864 
1865 	ASSERT(msp->ms_loaded);
1866 
1867 	for (int i = RANGE_TREE_HISTOGRAM_SIZE - 1; i >= SPA_MINBLOCKSHIFT;
1868 	    i--) {
1869 		uint8_t shift = msp->ms_group->mg_vd->vdev_ashift;
1870 		int max_idx = SPACE_MAP_HISTOGRAM_SIZE + shift - 1;
1871 
1872 		segments <<= 1;
1873 		segments += msp->ms_allocatable->rt_histogram[i];
1874 
1875 		/*
1876 		 * The range tree provides more precision than the space map
1877 		 * and must be downgraded so that all values fit within the
1878 		 * space map's histogram. This allows us to compare loaded
1879 		 * vs. unloaded metaslabs to determine which metaslab is
1880 		 * considered "best".
1881 		 */
1882 		if (i > max_idx)
1883 			continue;
1884 
1885 		if (segments != 0) {
1886 			WEIGHT_SET_COUNT(weight, segments);
1887 			WEIGHT_SET_INDEX(weight, i);
1888 			WEIGHT_SET_ACTIVE(weight, 0);
1889 			break;
1890 		}
1891 	}
1892 	return (weight);
1893 }
1894 
1895 /*
1896  * Calculate the weight based on the on-disk histogram. This should only
1897  * be called after a sync pass has completely finished since the on-disk
1898  * information is updated in metaslab_sync().
1899  */
1900 static uint64_t
metaslab_weight_from_spacemap(metaslab_t * msp)1901 metaslab_weight_from_spacemap(metaslab_t *msp)
1902 {
1903 	uint64_t weight = 0;
1904 
1905 	for (int i = SPACE_MAP_HISTOGRAM_SIZE - 1; i >= 0; i--) {
1906 		if (msp->ms_sm->sm_phys->smp_histogram[i] != 0) {
1907 			WEIGHT_SET_COUNT(weight,
1908 			    msp->ms_sm->sm_phys->smp_histogram[i]);
1909 			WEIGHT_SET_INDEX(weight, i +
1910 			    msp->ms_sm->sm_shift);
1911 			WEIGHT_SET_ACTIVE(weight, 0);
1912 			break;
1913 		}
1914 	}
1915 	return (weight);
1916 }
1917 
1918 /*
1919  * Compute a segment-based weight for the specified metaslab. The weight
1920  * is determined by highest bucket in the histogram. The information
1921  * for the highest bucket is encoded into the weight value.
1922  */
1923 static uint64_t
metaslab_segment_weight(metaslab_t * msp)1924 metaslab_segment_weight(metaslab_t *msp)
1925 {
1926 	metaslab_group_t *mg = msp->ms_group;
1927 	uint64_t weight = 0;
1928 	uint8_t shift = mg->mg_vd->vdev_ashift;
1929 
1930 	ASSERT(MUTEX_HELD(&msp->ms_lock));
1931 
1932 	/*
1933 	 * The metaslab is completely free.
1934 	 */
1935 	if (space_map_allocated(msp->ms_sm) == 0) {
1936 		int idx = highbit64(msp->ms_size) - 1;
1937 		int max_idx = SPACE_MAP_HISTOGRAM_SIZE + shift - 1;
1938 
1939 		if (idx < max_idx) {
1940 			WEIGHT_SET_COUNT(weight, 1ULL);
1941 			WEIGHT_SET_INDEX(weight, idx);
1942 		} else {
1943 			WEIGHT_SET_COUNT(weight, 1ULL << (idx - max_idx));
1944 			WEIGHT_SET_INDEX(weight, max_idx);
1945 		}
1946 		WEIGHT_SET_ACTIVE(weight, 0);
1947 		ASSERT(!WEIGHT_IS_SPACEBASED(weight));
1948 
1949 		return (weight);
1950 	}
1951 
1952 	ASSERT3U(msp->ms_sm->sm_dbuf->db_size, ==, sizeof (space_map_phys_t));
1953 
1954 	/*
1955 	 * If the metaslab is fully allocated then just make the weight 0.
1956 	 */
1957 	if (space_map_allocated(msp->ms_sm) == msp->ms_size)
1958 		return (0);
1959 	/*
1960 	 * If the metaslab is already loaded, then use the range tree to
1961 	 * determine the weight. Otherwise, we rely on the space map information
1962 	 * to generate the weight.
1963 	 */
1964 	if (msp->ms_loaded) {
1965 		weight = metaslab_weight_from_range_tree(msp);
1966 	} else {
1967 		weight = metaslab_weight_from_spacemap(msp);
1968 	}
1969 
1970 	/*
1971 	 * If the metaslab was active the last time we calculated its weight
1972 	 * then keep it active. We want to consume the entire region that
1973 	 * is associated with this weight.
1974 	 */
1975 	if (msp->ms_activation_weight != 0 && weight != 0)
1976 		WEIGHT_SET_ACTIVE(weight, WEIGHT_GET_ACTIVE(msp->ms_weight));
1977 	return (weight);
1978 }
1979 
1980 /*
1981  * Determine if we should attempt to allocate from this metaslab. If the
1982  * metaslab has a maximum size then we can quickly determine if the desired
1983  * allocation size can be satisfied. Otherwise, if we're using segment-based
1984  * weighting then we can determine the maximum allocation that this metaslab
1985  * can accommodate based on the index encoded in the weight. If we're using
1986  * space-based weights then rely on the entire weight (excluding the weight
1987  * type bit).
1988  */
1989 boolean_t
metaslab_should_allocate(metaslab_t * msp,uint64_t asize)1990 metaslab_should_allocate(metaslab_t *msp, uint64_t asize)
1991 {
1992 	boolean_t should_allocate;
1993 
1994 	if (msp->ms_max_size != 0)
1995 		return (msp->ms_max_size >= asize);
1996 
1997 	if (!WEIGHT_IS_SPACEBASED(msp->ms_weight)) {
1998 		/*
1999 		 * The metaslab segment weight indicates segments in the
2000 		 * range [2^i, 2^(i+1)), where i is the index in the weight.
2001 		 * Since the asize might be in the middle of the range, we
2002 		 * should attempt the allocation if asize < 2^(i+1).
2003 		 */
2004 		should_allocate = (asize <
2005 		    1ULL << (WEIGHT_GET_INDEX(msp->ms_weight) + 1));
2006 	} else {
2007 		should_allocate = (asize <=
2008 		    (msp->ms_weight & ~METASLAB_WEIGHT_TYPE));
2009 	}
2010 	return (should_allocate);
2011 }
2012 
2013 static uint64_t
metaslab_weight(metaslab_t * msp)2014 metaslab_weight(metaslab_t *msp)
2015 {
2016 	vdev_t *vd = msp->ms_group->mg_vd;
2017 	spa_t *spa = vd->vdev_spa;
2018 	uint64_t weight;
2019 
2020 	ASSERT(MUTEX_HELD(&msp->ms_lock));
2021 
2022 	/*
2023 	 * If this vdev is in the process of being removed, there is nothing
2024 	 * for us to do here.
2025 	 */
2026 	if (vd->vdev_removing)
2027 		return (0);
2028 
2029 	metaslab_set_fragmentation(msp);
2030 
2031 	/*
2032 	 * Update the maximum size if the metaslab is loaded. This will
2033 	 * ensure that we get an accurate maximum size if newly freed space
2034 	 * has been added back into the free tree.
2035 	 */
2036 	if (msp->ms_loaded)
2037 		msp->ms_max_size = metaslab_block_maxsize(msp);
2038 
2039 	/*
2040 	 * Segment-based weighting requires space map histogram support.
2041 	 */
2042 	if (zfs_metaslab_segment_weight_enabled &&
2043 	    spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM) &&
2044 	    (msp->ms_sm == NULL || msp->ms_sm->sm_dbuf->db_size ==
2045 	    sizeof (space_map_phys_t))) {
2046 		weight = metaslab_segment_weight(msp);
2047 	} else {
2048 		weight = metaslab_space_weight(msp);
2049 	}
2050 	return (weight);
2051 }
2052 
2053 static int
metaslab_activate_allocator(metaslab_group_t * mg,metaslab_t * msp,int allocator,uint64_t activation_weight)2054 metaslab_activate_allocator(metaslab_group_t *mg, metaslab_t *msp,
2055     int allocator, uint64_t activation_weight)
2056 {
2057 	/*
2058 	 * If we're activating for the claim code, we don't want to actually
2059 	 * set the metaslab up for a specific allocator.
2060 	 */
2061 	if (activation_weight == METASLAB_WEIGHT_CLAIM)
2062 		return (0);
2063 	metaslab_t **arr = (activation_weight == METASLAB_WEIGHT_PRIMARY ?
2064 	    mg->mg_primaries : mg->mg_secondaries);
2065 
2066 	ASSERT(MUTEX_HELD(&msp->ms_lock));
2067 	mutex_enter(&mg->mg_lock);
2068 	if (arr[allocator] != NULL) {
2069 		mutex_exit(&mg->mg_lock);
2070 		return (EEXIST);
2071 	}
2072 
2073 	arr[allocator] = msp;
2074 	ASSERT3S(msp->ms_allocator, ==, -1);
2075 	msp->ms_allocator = allocator;
2076 	msp->ms_primary = (activation_weight == METASLAB_WEIGHT_PRIMARY);
2077 	mutex_exit(&mg->mg_lock);
2078 
2079 	return (0);
2080 }
2081 
2082 static int
metaslab_activate(metaslab_t * msp,int allocator,uint64_t activation_weight)2083 metaslab_activate(metaslab_t *msp, int allocator, uint64_t activation_weight)
2084 {
2085 	ASSERT(MUTEX_HELD(&msp->ms_lock));
2086 
2087 	if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
2088 		int error = 0;
2089 		metaslab_load_wait(msp);
2090 		if (!msp->ms_loaded) {
2091 			if ((error = metaslab_load(msp)) != 0) {
2092 				metaslab_group_sort(msp->ms_group, msp, 0);
2093 				return (error);
2094 			}
2095 		}
2096 		if ((msp->ms_weight & METASLAB_ACTIVE_MASK) != 0) {
2097 			/*
2098 			 * The metaslab was activated for another allocator
2099 			 * while we were waiting, we should reselect.
2100 			 */
2101 			return (EBUSY);
2102 		}
2103 		if ((error = metaslab_activate_allocator(msp->ms_group, msp,
2104 		    allocator, activation_weight)) != 0) {
2105 			return (error);
2106 		}
2107 
2108 		msp->ms_activation_weight = msp->ms_weight;
2109 		metaslab_group_sort(msp->ms_group, msp,
2110 		    msp->ms_weight | activation_weight);
2111 	}
2112 	ASSERT(msp->ms_loaded);
2113 	ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
2114 
2115 	return (0);
2116 }
2117 
2118 static void
metaslab_passivate_allocator(metaslab_group_t * mg,metaslab_t * msp,uint64_t weight)2119 metaslab_passivate_allocator(metaslab_group_t *mg, metaslab_t *msp,
2120     uint64_t weight)
2121 {
2122 	ASSERT(MUTEX_HELD(&msp->ms_lock));
2123 	if (msp->ms_weight & METASLAB_WEIGHT_CLAIM) {
2124 		metaslab_group_sort(mg, msp, weight);
2125 		return;
2126 	}
2127 
2128 	mutex_enter(&mg->mg_lock);
2129 	ASSERT3P(msp->ms_group, ==, mg);
2130 	if (msp->ms_primary) {
2131 		ASSERT3U(0, <=, msp->ms_allocator);
2132 		ASSERT3U(msp->ms_allocator, <, mg->mg_allocators);
2133 		ASSERT3P(mg->mg_primaries[msp->ms_allocator], ==, msp);
2134 		ASSERT(msp->ms_weight & METASLAB_WEIGHT_PRIMARY);
2135 		mg->mg_primaries[msp->ms_allocator] = NULL;
2136 	} else {
2137 		ASSERT(msp->ms_weight & METASLAB_WEIGHT_SECONDARY);
2138 		ASSERT3P(mg->mg_secondaries[msp->ms_allocator], ==, msp);
2139 		mg->mg_secondaries[msp->ms_allocator] = NULL;
2140 	}
2141 	msp->ms_allocator = -1;
2142 	metaslab_group_sort_impl(mg, msp, weight);
2143 	mutex_exit(&mg->mg_lock);
2144 }
2145 
2146 static void
metaslab_passivate(metaslab_t * msp,uint64_t weight)2147 metaslab_passivate(metaslab_t *msp, uint64_t weight)
2148 {
2149 	uint64_t size = weight & ~METASLAB_WEIGHT_TYPE;
2150 
2151 	/*
2152 	 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
2153 	 * this metaslab again.  In that case, it had better be empty,
2154 	 * or we would be leaving space on the table.
2155 	 */
2156 	ASSERT(size >= SPA_MINBLOCKSIZE ||
2157 	    range_tree_is_empty(msp->ms_allocatable));
2158 	ASSERT0(weight & METASLAB_ACTIVE_MASK);
2159 
2160 	msp->ms_activation_weight = 0;
2161 	metaslab_passivate_allocator(msp->ms_group, msp, weight);
2162 	ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
2163 }
2164 
2165 /*
2166  * Segment-based metaslabs are activated once and remain active until
2167  * we either fail an allocation attempt (similar to space-based metaslabs)
2168  * or have exhausted the free space in zfs_metaslab_switch_threshold
2169  * buckets since the metaslab was activated. This function checks to see
2170  * if we've exhaused the zfs_metaslab_switch_threshold buckets in the
2171  * metaslab and passivates it proactively. This will allow us to select a
2172  * metaslabs with larger contiguous region if any remaining within this
2173  * metaslab group. If we're in sync pass > 1, then we continue using this
2174  * metaslab so that we don't dirty more block and cause more sync passes.
2175  */
2176 void
metaslab_segment_may_passivate(metaslab_t * msp)2177 metaslab_segment_may_passivate(metaslab_t *msp)
2178 {
2179 	spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
2180 
2181 	if (WEIGHT_IS_SPACEBASED(msp->ms_weight) || spa_sync_pass(spa) > 1)
2182 		return;
2183 
2184 	/*
2185 	 * Since we are in the middle of a sync pass, the most accurate
2186 	 * information that is accessible to us is the in-core range tree
2187 	 * histogram; calculate the new weight based on that information.
2188 	 */
2189 	uint64_t weight = metaslab_weight_from_range_tree(msp);
2190 	int activation_idx = WEIGHT_GET_INDEX(msp->ms_activation_weight);
2191 	int current_idx = WEIGHT_GET_INDEX(weight);
2192 
2193 	if (current_idx <= activation_idx - zfs_metaslab_switch_threshold)
2194 		metaslab_passivate(msp, weight);
2195 }
2196 
2197 static void
metaslab_preload(void * arg)2198 metaslab_preload(void *arg)
2199 {
2200 	metaslab_t *msp = arg;
2201 	spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
2202 
2203 	ASSERT(!MUTEX_HELD(&msp->ms_group->mg_lock));
2204 
2205 	mutex_enter(&msp->ms_lock);
2206 	metaslab_load_wait(msp);
2207 	if (!msp->ms_loaded)
2208 		(void) metaslab_load(msp);
2209 	msp->ms_selected_txg = spa_syncing_txg(spa);
2210 	mutex_exit(&msp->ms_lock);
2211 }
2212 
2213 static void
metaslab_group_preload(metaslab_group_t * mg)2214 metaslab_group_preload(metaslab_group_t *mg)
2215 {
2216 	spa_t *spa = mg->mg_vd->vdev_spa;
2217 	metaslab_t *msp;
2218 	avl_tree_t *t = &mg->mg_metaslab_tree;
2219 	int m = 0;
2220 
2221 	if (spa_shutting_down(spa) || !metaslab_preload_enabled) {
2222 		taskq_wait(mg->mg_taskq);
2223 		return;
2224 	}
2225 
2226 	mutex_enter(&mg->mg_lock);
2227 
2228 	/*
2229 	 * Load the next potential metaslabs
2230 	 */
2231 	for (msp = avl_first(t); msp != NULL; msp = AVL_NEXT(t, msp)) {
2232 		ASSERT3P(msp->ms_group, ==, mg);
2233 
2234 		/*
2235 		 * We preload only the maximum number of metaslabs specified
2236 		 * by metaslab_preload_limit. If a metaslab is being forced
2237 		 * to condense then we preload it too. This will ensure
2238 		 * that force condensing happens in the next txg.
2239 		 */
2240 		if (++m > metaslab_preload_limit && !msp->ms_condense_wanted) {
2241 			continue;
2242 		}
2243 
2244 		VERIFY(taskq_dispatch(mg->mg_taskq, metaslab_preload,
2245 		    msp, TQ_SLEEP) != 0);
2246 	}
2247 	mutex_exit(&mg->mg_lock);
2248 }
2249 
2250 /*
2251  * Determine if the space map's on-disk footprint is past our tolerance
2252  * for inefficiency. We would like to use the following criteria to make
2253  * our decision:
2254  *
2255  * 1. The size of the space map object should not dramatically increase as a
2256  * result of writing out the free space range tree.
2257  *
2258  * 2. The minimal on-disk space map representation is zfs_condense_pct/100
2259  * times the size than the free space range tree representation
2260  * (i.e. zfs_condense_pct = 110 and in-core = 1MB, minimal = 1.1MB).
2261  *
2262  * 3. The on-disk size of the space map should actually decrease.
2263  *
2264  * Unfortunately, we cannot compute the on-disk size of the space map in this
2265  * context because we cannot accurately compute the effects of compression, etc.
2266  * Instead, we apply the heuristic described in the block comment for
2267  * zfs_metaslab_condense_block_threshold - we only condense if the space used
2268  * is greater than a threshold number of blocks.
2269  */
2270 static boolean_t
metaslab_should_condense(metaslab_t * msp)2271 metaslab_should_condense(metaslab_t *msp)
2272 {
2273 	space_map_t *sm = msp->ms_sm;
2274 	vdev_t *vd = msp->ms_group->mg_vd;
2275 	uint64_t vdev_blocksize = 1 << vd->vdev_ashift;
2276 	uint64_t current_txg = spa_syncing_txg(vd->vdev_spa);
2277 
2278 	ASSERT(MUTEX_HELD(&msp->ms_lock));
2279 	ASSERT(msp->ms_loaded);
2280 
2281 	/*
2282 	 * Allocations and frees in early passes are generally more space
2283 	 * efficient (in terms of blocks described in space map entries)
2284 	 * than the ones in later passes (e.g. we don't compress after
2285 	 * sync pass 5) and condensing a metaslab multiple times in a txg
2286 	 * could degrade performance.
2287 	 *
2288 	 * Thus we prefer condensing each metaslab at most once every txg at
2289 	 * the earliest sync pass possible. If a metaslab is eligible for
2290 	 * condensing again after being considered for condensing within the
2291 	 * same txg, it will hopefully be dirty in the next txg where it will
2292 	 * be condensed at an earlier pass.
2293 	 */
2294 	if (msp->ms_condense_checked_txg == current_txg)
2295 		return (B_FALSE);
2296 	msp->ms_condense_checked_txg = current_txg;
2297 
2298 	/*
2299 	 * We always condense metaslabs that are empty and metaslabs for
2300 	 * which a condense request has been made.
2301 	 */
2302 	if (avl_is_empty(&msp->ms_allocatable_by_size) ||
2303 	    msp->ms_condense_wanted)
2304 		return (B_TRUE);
2305 
2306 	uint64_t object_size = space_map_length(msp->ms_sm);
2307 	uint64_t optimal_size = space_map_estimate_optimal_size(sm,
2308 	    msp->ms_allocatable, SM_NO_VDEVID);
2309 
2310 	dmu_object_info_t doi;
2311 	dmu_object_info_from_db(sm->sm_dbuf, &doi);
2312 	uint64_t record_size = MAX(doi.doi_data_block_size, vdev_blocksize);
2313 
2314 	return (object_size >= (optimal_size * zfs_condense_pct / 100) &&
2315 	    object_size > zfs_metaslab_condense_block_threshold * record_size);
2316 }
2317 
2318 /*
2319  * Condense the on-disk space map representation to its minimized form.
2320  * The minimized form consists of a small number of allocations followed by
2321  * the entries of the free range tree.
2322  */
2323 static void
metaslab_condense(metaslab_t * msp,uint64_t txg,dmu_tx_t * tx)2324 metaslab_condense(metaslab_t *msp, uint64_t txg, dmu_tx_t *tx)
2325 {
2326 	range_tree_t *condense_tree;
2327 	space_map_t *sm = msp->ms_sm;
2328 
2329 	ASSERT(MUTEX_HELD(&msp->ms_lock));
2330 	ASSERT(msp->ms_loaded);
2331 
2332 	zfs_dbgmsg("condensing: txg %llu, msp[%llu] %p, vdev id %llu, "
2333 	    "spa %s, smp size %llu, segments %lu, forcing condense=%s", txg,
2334 	    msp->ms_id, msp, msp->ms_group->mg_vd->vdev_id,
2335 	    msp->ms_group->mg_vd->vdev_spa->spa_name,
2336 	    space_map_length(msp->ms_sm),
2337 	    avl_numnodes(&msp->ms_allocatable->rt_root),
2338 	    msp->ms_condense_wanted ? "TRUE" : "FALSE");
2339 
2340 	msp->ms_condense_wanted = B_FALSE;
2341 
2342 	/*
2343 	 * Create an range tree that is 100% allocated. We remove segments
2344 	 * that have been freed in this txg, any deferred frees that exist,
2345 	 * and any allocation in the future. Removing segments should be
2346 	 * a relatively inexpensive operation since we expect these trees to
2347 	 * have a small number of nodes.
2348 	 */
2349 	condense_tree = range_tree_create(NULL, NULL);
2350 	range_tree_add(condense_tree, msp->ms_start, msp->ms_size);
2351 
2352 	range_tree_walk(msp->ms_freeing, range_tree_remove, condense_tree);
2353 	range_tree_walk(msp->ms_freed, range_tree_remove, condense_tree);
2354 
2355 	for (int t = 0; t < TXG_DEFER_SIZE; t++) {
2356 		range_tree_walk(msp->ms_defer[t],
2357 		    range_tree_remove, condense_tree);
2358 	}
2359 
2360 	for (int t = 1; t < TXG_CONCURRENT_STATES; t++) {
2361 		range_tree_walk(msp->ms_allocating[(txg + t) & TXG_MASK],
2362 		    range_tree_remove, condense_tree);
2363 	}
2364 
2365 	/*
2366 	 * We're about to drop the metaslab's lock thus allowing
2367 	 * other consumers to change it's content. Set the
2368 	 * metaslab's ms_condensing flag to ensure that
2369 	 * allocations on this metaslab do not occur while we're
2370 	 * in the middle of committing it to disk. This is only critical
2371 	 * for ms_allocatable as all other range trees use per txg
2372 	 * views of their content.
2373 	 */
2374 	msp->ms_condensing = B_TRUE;
2375 
2376 	mutex_exit(&msp->ms_lock);
2377 	space_map_truncate(sm, zfs_metaslab_sm_blksz, tx);
2378 
2379 	/*
2380 	 * While we would ideally like to create a space map representation
2381 	 * that consists only of allocation records, doing so can be
2382 	 * prohibitively expensive because the in-core free tree can be
2383 	 * large, and therefore computationally expensive to subtract
2384 	 * from the condense_tree. Instead we sync out two trees, a cheap
2385 	 * allocation only tree followed by the in-core free tree. While not
2386 	 * optimal, this is typically close to optimal, and much cheaper to
2387 	 * compute.
2388 	 */
2389 	space_map_write(sm, condense_tree, SM_ALLOC, SM_NO_VDEVID, tx);
2390 	range_tree_vacate(condense_tree, NULL, NULL);
2391 	range_tree_destroy(condense_tree);
2392 
2393 	space_map_write(sm, msp->ms_allocatable, SM_FREE, SM_NO_VDEVID, tx);
2394 	mutex_enter(&msp->ms_lock);
2395 	msp->ms_condensing = B_FALSE;
2396 }
2397 
2398 /*
2399  * Write a metaslab to disk in the context of the specified transaction group.
2400  */
2401 void
metaslab_sync(metaslab_t * msp,uint64_t txg)2402 metaslab_sync(metaslab_t *msp, uint64_t txg)
2403 {
2404 	metaslab_group_t *mg = msp->ms_group;
2405 	vdev_t *vd = mg->mg_vd;
2406 	spa_t *spa = vd->vdev_spa;
2407 	objset_t *mos = spa_meta_objset(spa);
2408 	range_tree_t *alloctree = msp->ms_allocating[txg & TXG_MASK];
2409 	dmu_tx_t *tx;
2410 	uint64_t object = space_map_object(msp->ms_sm);
2411 
2412 	ASSERT(!vd->vdev_ishole);
2413 
2414 	/*
2415 	 * This metaslab has just been added so there's no work to do now.
2416 	 */
2417 	if (msp->ms_freeing == NULL) {
2418 		ASSERT3P(alloctree, ==, NULL);
2419 		return;
2420 	}
2421 
2422 	ASSERT3P(alloctree, !=, NULL);
2423 	ASSERT3P(msp->ms_freeing, !=, NULL);
2424 	ASSERT3P(msp->ms_freed, !=, NULL);
2425 	ASSERT3P(msp->ms_checkpointing, !=, NULL);
2426 
2427 	/*
2428 	 * Normally, we don't want to process a metaslab if there are no
2429 	 * allocations or frees to perform. However, if the metaslab is being
2430 	 * forced to condense and it's loaded, we need to let it through.
2431 	 */
2432 	if (range_tree_is_empty(alloctree) &&
2433 	    range_tree_is_empty(msp->ms_freeing) &&
2434 	    range_tree_is_empty(msp->ms_checkpointing) &&
2435 	    !(msp->ms_loaded && msp->ms_condense_wanted))
2436 		return;
2437 
2438 
2439 	VERIFY(txg <= spa_final_dirty_txg(spa));
2440 
2441 	/*
2442 	 * The only state that can actually be changing concurrently with
2443 	 * metaslab_sync() is the metaslab's ms_allocatable.  No other
2444 	 * thread can be modifying this txg's alloc, freeing,
2445 	 * freed, or space_map_phys_t.  We drop ms_lock whenever we
2446 	 * could call into the DMU, because the DMU can call down to us
2447 	 * (e.g. via zio_free()) at any time.
2448 	 *
2449 	 * The spa_vdev_remove_thread() can be reading metaslab state
2450 	 * concurrently, and it is locked out by the ms_sync_lock.  Note
2451 	 * that the ms_lock is insufficient for this, because it is dropped
2452 	 * by space_map_write().
2453 	 */
2454 	tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2455 
2456 	if (msp->ms_sm == NULL) {
2457 		uint64_t new_object;
2458 
2459 		new_object = space_map_alloc(mos, zfs_metaslab_sm_blksz, tx);
2460 		VERIFY3U(new_object, !=, 0);
2461 
2462 		VERIFY0(space_map_open(&msp->ms_sm, mos, new_object,
2463 		    msp->ms_start, msp->ms_size, vd->vdev_ashift));
2464 		ASSERT(msp->ms_sm != NULL);
2465 	}
2466 
2467 	if (!range_tree_is_empty(msp->ms_checkpointing) &&
2468 	    vd->vdev_checkpoint_sm == NULL) {
2469 		ASSERT(spa_has_checkpoint(spa));
2470 
2471 		uint64_t new_object = space_map_alloc(mos,
2472 		    vdev_standard_sm_blksz, tx);
2473 		VERIFY3U(new_object, !=, 0);
2474 
2475 		VERIFY0(space_map_open(&vd->vdev_checkpoint_sm,
2476 		    mos, new_object, 0, vd->vdev_asize, vd->vdev_ashift));
2477 		ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
2478 
2479 		/*
2480 		 * We save the space map object as an entry in vdev_top_zap
2481 		 * so it can be retrieved when the pool is reopened after an
2482 		 * export or through zdb.
2483 		 */
2484 		VERIFY0(zap_add(vd->vdev_spa->spa_meta_objset,
2485 		    vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
2486 		    sizeof (new_object), 1, &new_object, tx));
2487 	}
2488 
2489 	mutex_enter(&msp->ms_sync_lock);
2490 	mutex_enter(&msp->ms_lock);
2491 
2492 	/*
2493 	 * Note: metaslab_condense() clears the space map's histogram.
2494 	 * Therefore we must verify and remove this histogram before
2495 	 * condensing.
2496 	 */
2497 	metaslab_group_histogram_verify(mg);
2498 	metaslab_class_histogram_verify(mg->mg_class);
2499 	metaslab_group_histogram_remove(mg, msp);
2500 
2501 	if (msp->ms_loaded && metaslab_should_condense(msp)) {
2502 		metaslab_condense(msp, txg, tx);
2503 	} else {
2504 		mutex_exit(&msp->ms_lock);
2505 		space_map_write(msp->ms_sm, alloctree, SM_ALLOC,
2506 		    SM_NO_VDEVID, tx);
2507 		space_map_write(msp->ms_sm, msp->ms_freeing, SM_FREE,
2508 		    SM_NO_VDEVID, tx);
2509 		mutex_enter(&msp->ms_lock);
2510 	}
2511 
2512 	if (!range_tree_is_empty(msp->ms_checkpointing)) {
2513 		ASSERT(spa_has_checkpoint(spa));
2514 		ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
2515 
2516 		/*
2517 		 * Since we are doing writes to disk and the ms_checkpointing
2518 		 * tree won't be changing during that time, we drop the
2519 		 * ms_lock while writing to the checkpoint space map.
2520 		 */
2521 		mutex_exit(&msp->ms_lock);
2522 		space_map_write(vd->vdev_checkpoint_sm,
2523 		    msp->ms_checkpointing, SM_FREE, SM_NO_VDEVID, tx);
2524 		mutex_enter(&msp->ms_lock);
2525 		space_map_update(vd->vdev_checkpoint_sm);
2526 
2527 		spa->spa_checkpoint_info.sci_dspace +=
2528 		    range_tree_space(msp->ms_checkpointing);
2529 		vd->vdev_stat.vs_checkpoint_space +=
2530 		    range_tree_space(msp->ms_checkpointing);
2531 		ASSERT3U(vd->vdev_stat.vs_checkpoint_space, ==,
2532 		    -vd->vdev_checkpoint_sm->sm_alloc);
2533 
2534 		range_tree_vacate(msp->ms_checkpointing, NULL, NULL);
2535 	}
2536 
2537 	if (msp->ms_loaded) {
2538 		/*
2539 		 * When the space map is loaded, we have an accurate
2540 		 * histogram in the range tree. This gives us an opportunity
2541 		 * to bring the space map's histogram up-to-date so we clear
2542 		 * it first before updating it.
2543 		 */
2544 		space_map_histogram_clear(msp->ms_sm);
2545 		space_map_histogram_add(msp->ms_sm, msp->ms_allocatable, tx);
2546 
2547 		/*
2548 		 * Since we've cleared the histogram we need to add back
2549 		 * any free space that has already been processed, plus
2550 		 * any deferred space. This allows the on-disk histogram
2551 		 * to accurately reflect all free space even if some space
2552 		 * is not yet available for allocation (i.e. deferred).
2553 		 */
2554 		space_map_histogram_add(msp->ms_sm, msp->ms_freed, tx);
2555 
2556 		/*
2557 		 * Add back any deferred free space that has not been
2558 		 * added back into the in-core free tree yet. This will
2559 		 * ensure that we don't end up with a space map histogram
2560 		 * that is completely empty unless the metaslab is fully
2561 		 * allocated.
2562 		 */
2563 		for (int t = 0; t < TXG_DEFER_SIZE; t++) {
2564 			space_map_histogram_add(msp->ms_sm,
2565 			    msp->ms_defer[t], tx);
2566 		}
2567 	}
2568 
2569 	/*
2570 	 * Always add the free space from this sync pass to the space
2571 	 * map histogram. We want to make sure that the on-disk histogram
2572 	 * accounts for all free space. If the space map is not loaded,
2573 	 * then we will lose some accuracy but will correct it the next
2574 	 * time we load the space map.
2575 	 */
2576 	space_map_histogram_add(msp->ms_sm, msp->ms_freeing, tx);
2577 
2578 	metaslab_group_histogram_add(mg, msp);
2579 	metaslab_group_histogram_verify(mg);
2580 	metaslab_class_histogram_verify(mg->mg_class);
2581 
2582 	/*
2583 	 * For sync pass 1, we avoid traversing this txg's free range tree
2584 	 * and instead will just swap the pointers for freeing and
2585 	 * freed. We can safely do this since the freed_tree is
2586 	 * guaranteed to be empty on the initial pass.
2587 	 */
2588 	if (spa_sync_pass(spa) == 1) {
2589 		range_tree_swap(&msp->ms_freeing, &msp->ms_freed);
2590 	} else {
2591 		range_tree_vacate(msp->ms_freeing,
2592 		    range_tree_add, msp->ms_freed);
2593 	}
2594 	range_tree_vacate(alloctree, NULL, NULL);
2595 
2596 	ASSERT0(range_tree_space(msp->ms_allocating[txg & TXG_MASK]));
2597 	ASSERT0(range_tree_space(msp->ms_allocating[TXG_CLEAN(txg)
2598 	    & TXG_MASK]));
2599 	ASSERT0(range_tree_space(msp->ms_freeing));
2600 	ASSERT0(range_tree_space(msp->ms_checkpointing));
2601 
2602 	mutex_exit(&msp->ms_lock);
2603 
2604 	if (object != space_map_object(msp->ms_sm)) {
2605 		object = space_map_object(msp->ms_sm);
2606 		dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
2607 		    msp->ms_id, sizeof (uint64_t), &object, tx);
2608 	}
2609 	mutex_exit(&msp->ms_sync_lock);
2610 	dmu_tx_commit(tx);
2611 }
2612 
2613 /*
2614  * Called after a transaction group has completely synced to mark
2615  * all of the metaslab's free space as usable.
2616  */
2617 void
metaslab_sync_done(metaslab_t * msp,uint64_t txg)2618 metaslab_sync_done(metaslab_t *msp, uint64_t txg)
2619 {
2620 	metaslab_group_t *mg = msp->ms_group;
2621 	vdev_t *vd = mg->mg_vd;
2622 	spa_t *spa = vd->vdev_spa;
2623 	range_tree_t **defer_tree;
2624 	int64_t alloc_delta, defer_delta;
2625 	boolean_t defer_allowed = B_TRUE;
2626 
2627 	ASSERT(!vd->vdev_ishole);
2628 
2629 	mutex_enter(&msp->ms_lock);
2630 
2631 	/*
2632 	 * If this metaslab is just becoming available, initialize its
2633 	 * range trees and add its capacity to the vdev.
2634 	 */
2635 	if (msp->ms_freed == NULL) {
2636 		for (int t = 0; t < TXG_SIZE; t++) {
2637 			ASSERT(msp->ms_allocating[t] == NULL);
2638 
2639 			msp->ms_allocating[t] = range_tree_create(NULL, NULL);
2640 		}
2641 
2642 		ASSERT3P(msp->ms_freeing, ==, NULL);
2643 		msp->ms_freeing = range_tree_create(NULL, NULL);
2644 
2645 		ASSERT3P(msp->ms_freed, ==, NULL);
2646 		msp->ms_freed = range_tree_create(NULL, NULL);
2647 
2648 		for (int t = 0; t < TXG_DEFER_SIZE; t++) {
2649 			ASSERT(msp->ms_defer[t] == NULL);
2650 
2651 			msp->ms_defer[t] = range_tree_create(NULL, NULL);
2652 		}
2653 
2654 		ASSERT3P(msp->ms_checkpointing, ==, NULL);
2655 		msp->ms_checkpointing = range_tree_create(NULL, NULL);
2656 
2657 		vdev_space_update(vd, 0, 0, msp->ms_size);
2658 	}
2659 	ASSERT0(range_tree_space(msp->ms_freeing));
2660 	ASSERT0(range_tree_space(msp->ms_checkpointing));
2661 
2662 	defer_tree = &msp->ms_defer[txg % TXG_DEFER_SIZE];
2663 
2664 	uint64_t free_space = metaslab_class_get_space(spa_normal_class(spa)) -
2665 	    metaslab_class_get_alloc(spa_normal_class(spa));
2666 	if (free_space <= spa_get_slop_space(spa) || vd->vdev_removing) {
2667 		defer_allowed = B_FALSE;
2668 	}
2669 
2670 	defer_delta = 0;
2671 	alloc_delta = space_map_alloc_delta(msp->ms_sm);
2672 	if (defer_allowed) {
2673 		defer_delta = range_tree_space(msp->ms_freed) -
2674 		    range_tree_space(*defer_tree);
2675 	} else {
2676 		defer_delta -= range_tree_space(*defer_tree);
2677 	}
2678 
2679 	vdev_space_update(vd, alloc_delta + defer_delta, defer_delta, 0);
2680 
2681 	/*
2682 	 * If there's a metaslab_load() in progress, wait for it to complete
2683 	 * so that we have a consistent view of the in-core space map.
2684 	 */
2685 	metaslab_load_wait(msp);
2686 
2687 	/*
2688 	 * Move the frees from the defer_tree back to the free
2689 	 * range tree (if it's loaded). Swap the freed_tree and
2690 	 * the defer_tree -- this is safe to do because we've
2691 	 * just emptied out the defer_tree.
2692 	 */
2693 	range_tree_vacate(*defer_tree,
2694 	    msp->ms_loaded ? range_tree_add : NULL, msp->ms_allocatable);
2695 	if (defer_allowed) {
2696 		range_tree_swap(&msp->ms_freed, defer_tree);
2697 	} else {
2698 		range_tree_vacate(msp->ms_freed,
2699 		    msp->ms_loaded ? range_tree_add : NULL,
2700 		    msp->ms_allocatable);
2701 	}
2702 	space_map_update(msp->ms_sm);
2703 
2704 	msp->ms_deferspace += defer_delta;
2705 	ASSERT3S(msp->ms_deferspace, >=, 0);
2706 	ASSERT3S(msp->ms_deferspace, <=, msp->ms_size);
2707 	if (msp->ms_deferspace != 0) {
2708 		/*
2709 		 * Keep syncing this metaslab until all deferred frees
2710 		 * are back in circulation.
2711 		 */
2712 		vdev_dirty(vd, VDD_METASLAB, msp, txg + 1);
2713 	}
2714 
2715 	if (msp->ms_new) {
2716 		msp->ms_new = B_FALSE;
2717 		mutex_enter(&mg->mg_lock);
2718 		mg->mg_ms_ready++;
2719 		mutex_exit(&mg->mg_lock);
2720 	}
2721 	/*
2722 	 * Calculate the new weights before unloading any metaslabs.
2723 	 * This will give us the most accurate weighting.
2724 	 */
2725 	metaslab_group_sort(mg, msp, metaslab_weight(msp) |
2726 	    (msp->ms_weight & METASLAB_ACTIVE_MASK));
2727 
2728 	/*
2729 	 * If the metaslab is loaded and we've not tried to load or allocate
2730 	 * from it in 'metaslab_unload_delay' txgs, then unload it.
2731 	 */
2732 	if (msp->ms_loaded &&
2733 	    msp->ms_initializing == 0 &&
2734 	    msp->ms_selected_txg + metaslab_unload_delay < txg) {
2735 		for (int t = 1; t < TXG_CONCURRENT_STATES; t++) {
2736 			VERIFY0(range_tree_space(
2737 			    msp->ms_allocating[(txg + t) & TXG_MASK]));
2738 		}
2739 		if (msp->ms_allocator != -1) {
2740 			metaslab_passivate(msp, msp->ms_weight &
2741 			    ~METASLAB_ACTIVE_MASK);
2742 		}
2743 
2744 		if (!metaslab_debug_unload)
2745 			metaslab_unload(msp);
2746 	}
2747 
2748 	ASSERT0(range_tree_space(msp->ms_allocating[txg & TXG_MASK]));
2749 	ASSERT0(range_tree_space(msp->ms_freeing));
2750 	ASSERT0(range_tree_space(msp->ms_freed));
2751 	ASSERT0(range_tree_space(msp->ms_checkpointing));
2752 
2753 	mutex_exit(&msp->ms_lock);
2754 }
2755 
2756 void
metaslab_sync_reassess(metaslab_group_t * mg)2757 metaslab_sync_reassess(metaslab_group_t *mg)
2758 {
2759 	spa_t *spa = mg->mg_class->mc_spa;
2760 
2761 	spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
2762 	metaslab_group_alloc_update(mg);
2763 	mg->mg_fragmentation = metaslab_group_fragmentation(mg);
2764 
2765 	/*
2766 	 * Preload the next potential metaslabs but only on active
2767 	 * metaslab groups. We can get into a state where the metaslab
2768 	 * is no longer active since we dirty metaslabs as we remove a
2769 	 * a device, thus potentially making the metaslab group eligible
2770 	 * for preloading.
2771 	 */
2772 	if (mg->mg_activation_count > 0) {
2773 		metaslab_group_preload(mg);
2774 	}
2775 	spa_config_exit(spa, SCL_ALLOC, FTAG);
2776 }
2777 
2778 static uint64_t
metaslab_distance(metaslab_t * msp,dva_t * dva)2779 metaslab_distance(metaslab_t *msp, dva_t *dva)
2780 {
2781 	uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
2782 	uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
2783 	uint64_t start = msp->ms_id;
2784 
2785 	if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
2786 		return (1ULL << 63);
2787 
2788 	if (offset < start)
2789 		return ((start - offset) << ms_shift);
2790 	if (offset > start)
2791 		return ((offset - start) << ms_shift);
2792 	return (0);
2793 }
2794 
2795 /*
2796  * ==========================================================================
2797  * Metaslab allocation tracing facility
2798  * ==========================================================================
2799  */
2800 kstat_t *metaslab_trace_ksp;
2801 kstat_named_t metaslab_trace_over_limit;
2802 
2803 void
metaslab_alloc_trace_init(void)2804 metaslab_alloc_trace_init(void)
2805 {
2806 	ASSERT(metaslab_alloc_trace_cache == NULL);
2807 	metaslab_alloc_trace_cache = kmem_cache_create(
2808 	    "metaslab_alloc_trace_cache", sizeof (metaslab_alloc_trace_t),
2809 	    0, NULL, NULL, NULL, NULL, NULL, 0);
2810 	metaslab_trace_ksp = kstat_create("zfs", 0, "metaslab_trace_stats",
2811 	    "misc", KSTAT_TYPE_NAMED, 1, KSTAT_FLAG_VIRTUAL);
2812 	if (metaslab_trace_ksp != NULL) {
2813 		metaslab_trace_ksp->ks_data = &metaslab_trace_over_limit;
2814 		kstat_named_init(&metaslab_trace_over_limit,
2815 		    "metaslab_trace_over_limit", KSTAT_DATA_UINT64);
2816 		kstat_install(metaslab_trace_ksp);
2817 	}
2818 }
2819 
2820 void
metaslab_alloc_trace_fini(void)2821 metaslab_alloc_trace_fini(void)
2822 {
2823 	if (metaslab_trace_ksp != NULL) {
2824 		kstat_delete(metaslab_trace_ksp);
2825 		metaslab_trace_ksp = NULL;
2826 	}
2827 	kmem_cache_destroy(metaslab_alloc_trace_cache);
2828 	metaslab_alloc_trace_cache = NULL;
2829 }
2830 
2831 /*
2832  * Add an allocation trace element to the allocation tracing list.
2833  */
2834 static void
metaslab_trace_add(zio_alloc_list_t * zal,metaslab_group_t * mg,metaslab_t * msp,uint64_t psize,uint32_t dva_id,uint64_t offset,int allocator)2835 metaslab_trace_add(zio_alloc_list_t *zal, metaslab_group_t *mg,
2836     metaslab_t *msp, uint64_t psize, uint32_t dva_id, uint64_t offset,
2837     int allocator)
2838 {
2839 	if (!metaslab_trace_enabled)
2840 		return;
2841 
2842 	/*
2843 	 * When the tracing list reaches its maximum we remove
2844 	 * the second element in the list before adding a new one.
2845 	 * By removing the second element we preserve the original
2846 	 * entry as a clue to what allocations steps have already been
2847 	 * performed.
2848 	 */
2849 	if (zal->zal_size == metaslab_trace_max_entries) {
2850 		metaslab_alloc_trace_t *mat_next;
2851 #ifdef DEBUG
2852 		panic("too many entries in allocation list");
2853 #endif
2854 		atomic_inc_64(&metaslab_trace_over_limit.value.ui64);
2855 		zal->zal_size--;
2856 		mat_next = list_next(&zal->zal_list, list_head(&zal->zal_list));
2857 		list_remove(&zal->zal_list, mat_next);
2858 		kmem_cache_free(metaslab_alloc_trace_cache, mat_next);
2859 	}
2860 
2861 	metaslab_alloc_trace_t *mat =
2862 	    kmem_cache_alloc(metaslab_alloc_trace_cache, KM_SLEEP);
2863 	list_link_init(&mat->mat_list_node);
2864 	mat->mat_mg = mg;
2865 	mat->mat_msp = msp;
2866 	mat->mat_size = psize;
2867 	mat->mat_dva_id = dva_id;
2868 	mat->mat_offset = offset;
2869 	mat->mat_weight = 0;
2870 	mat->mat_allocator = allocator;
2871 
2872 	if (msp != NULL)
2873 		mat->mat_weight = msp->ms_weight;
2874 
2875 	/*
2876 	 * The list is part of the zio so locking is not required. Only
2877 	 * a single thread will perform allocations for a given zio.
2878 	 */
2879 	list_insert_tail(&zal->zal_list, mat);
2880 	zal->zal_size++;
2881 
2882 	ASSERT3U(zal->zal_size, <=, metaslab_trace_max_entries);
2883 }
2884 
2885 void
metaslab_trace_init(zio_alloc_list_t * zal)2886 metaslab_trace_init(zio_alloc_list_t *zal)
2887 {
2888 	list_create(&zal->zal_list, sizeof (metaslab_alloc_trace_t),
2889 	    offsetof(metaslab_alloc_trace_t, mat_list_node));
2890 	zal->zal_size = 0;
2891 }
2892 
2893 void
metaslab_trace_fini(zio_alloc_list_t * zal)2894 metaslab_trace_fini(zio_alloc_list_t *zal)
2895 {
2896 	metaslab_alloc_trace_t *mat;
2897 
2898 	while ((mat = list_remove_head(&zal->zal_list)) != NULL)
2899 		kmem_cache_free(metaslab_alloc_trace_cache, mat);
2900 	list_destroy(&zal->zal_list);
2901 	zal->zal_size = 0;
2902 }
2903 
2904 /*
2905  * ==========================================================================
2906  * Metaslab block operations
2907  * ==========================================================================
2908  */
2909 
2910 static void
metaslab_group_alloc_increment(spa_t * spa,uint64_t vdev,void * tag,int flags,int allocator)2911 metaslab_group_alloc_increment(spa_t *spa, uint64_t vdev, void *tag, int flags,
2912     int allocator)
2913 {
2914 	if (!(flags & METASLAB_ASYNC_ALLOC) ||
2915 	    (flags & METASLAB_DONT_THROTTLE))
2916 		return;
2917 
2918 	metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
2919 	if (!mg->mg_class->mc_alloc_throttle_enabled)
2920 		return;
2921 
2922 	(void) refcount_add(&mg->mg_alloc_queue_depth[allocator], tag);
2923 }
2924 
2925 static void
metaslab_group_increment_qdepth(metaslab_group_t * mg,int allocator)2926 metaslab_group_increment_qdepth(metaslab_group_t *mg, int allocator)
2927 {
2928 	uint64_t max = mg->mg_max_alloc_queue_depth;
2929 	uint64_t cur = mg->mg_cur_max_alloc_queue_depth[allocator];
2930 	while (cur < max) {
2931 		if (atomic_cas_64(&mg->mg_cur_max_alloc_queue_depth[allocator],
2932 		    cur, cur + 1) == cur) {
2933 			atomic_inc_64(
2934 			    &mg->mg_class->mc_alloc_max_slots[allocator]);
2935 			return;
2936 		}
2937 		cur = mg->mg_cur_max_alloc_queue_depth[allocator];
2938 	}
2939 }
2940 
2941 void
metaslab_group_alloc_decrement(spa_t * spa,uint64_t vdev,void * tag,int flags,int allocator,boolean_t io_complete)2942 metaslab_group_alloc_decrement(spa_t *spa, uint64_t vdev, void *tag, int flags,
2943     int allocator, boolean_t io_complete)
2944 {
2945 	if (!(flags & METASLAB_ASYNC_ALLOC) ||
2946 	    (flags & METASLAB_DONT_THROTTLE))
2947 		return;
2948 
2949 	metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
2950 	if (!mg->mg_class->mc_alloc_throttle_enabled)
2951 		return;
2952 
2953 	(void) refcount_remove(&mg->mg_alloc_queue_depth[allocator], tag);
2954 	if (io_complete)
2955 		metaslab_group_increment_qdepth(mg, allocator);
2956 }
2957 
2958 void
metaslab_group_alloc_verify(spa_t * spa,const blkptr_t * bp,void * tag,int allocator)2959 metaslab_group_alloc_verify(spa_t *spa, const blkptr_t *bp, void *tag,
2960     int allocator)
2961 {
2962 #ifdef ZFS_DEBUG
2963 	const dva_t *dva = bp->blk_dva;
2964 	int ndvas = BP_GET_NDVAS(bp);
2965 
2966 	for (int d = 0; d < ndvas; d++) {
2967 		uint64_t vdev = DVA_GET_VDEV(&dva[d]);
2968 		metaslab_group_t *mg = vdev_lookup_top(spa, vdev)->vdev_mg;
2969 		VERIFY(refcount_not_held(&mg->mg_alloc_queue_depth[allocator],
2970 		    tag));
2971 	}
2972 #endif
2973 }
2974 
2975 static uint64_t
metaslab_block_alloc(metaslab_t * msp,uint64_t size,uint64_t txg)2976 metaslab_block_alloc(metaslab_t *msp, uint64_t size, uint64_t txg)
2977 {
2978 	uint64_t start;
2979 	range_tree_t *rt = msp->ms_allocatable;
2980 	metaslab_class_t *mc = msp->ms_group->mg_class;
2981 
2982 	VERIFY(!msp->ms_condensing);
2983 	VERIFY0(msp->ms_initializing);
2984 
2985 	start = mc->mc_ops->msop_alloc(msp, size);
2986 	if (start != -1ULL) {
2987 		metaslab_group_t *mg = msp->ms_group;
2988 		vdev_t *vd = mg->mg_vd;
2989 
2990 		VERIFY0(P2PHASE(start, 1ULL << vd->vdev_ashift));
2991 		VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
2992 		VERIFY3U(range_tree_space(rt) - size, <=, msp->ms_size);
2993 		range_tree_remove(rt, start, size);
2994 
2995 		if (range_tree_is_empty(msp->ms_allocating[txg & TXG_MASK]))
2996 			vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
2997 
2998 		range_tree_add(msp->ms_allocating[txg & TXG_MASK], start, size);
2999 
3000 		/* Track the last successful allocation */
3001 		msp->ms_alloc_txg = txg;
3002 		metaslab_verify_space(msp, txg);
3003 	}
3004 
3005 	/*
3006 	 * Now that we've attempted the allocation we need to update the
3007 	 * metaslab's maximum block size since it may have changed.
3008 	 */
3009 	msp->ms_max_size = metaslab_block_maxsize(msp);
3010 	return (start);
3011 }
3012 
3013 /*
3014  * Find the metaslab with the highest weight that is less than what we've
3015  * already tried.  In the common case, this means that we will examine each
3016  * metaslab at most once. Note that concurrent callers could reorder metaslabs
3017  * by activation/passivation once we have dropped the mg_lock. If a metaslab is
3018  * activated by another thread, and we fail to allocate from the metaslab we
3019  * have selected, we may not try the newly-activated metaslab, and instead
3020  * activate another metaslab.  This is not optimal, but generally does not cause
3021  * any problems (a possible exception being if every metaslab is completely full
3022  * except for the the newly-activated metaslab which we fail to examine).
3023  */
3024 static metaslab_t *
find_valid_metaslab(metaslab_group_t * mg,uint64_t activation_weight,dva_t * dva,int d,uint64_t min_distance,uint64_t asize,int allocator,zio_alloc_list_t * zal,metaslab_t * search,boolean_t * was_active)3025 find_valid_metaslab(metaslab_group_t *mg, uint64_t activation_weight,
3026     dva_t *dva, int d, uint64_t min_distance, uint64_t asize, int allocator,
3027     zio_alloc_list_t *zal, metaslab_t *search, boolean_t *was_active)
3028 {
3029 	avl_index_t idx;
3030 	avl_tree_t *t = &mg->mg_metaslab_tree;
3031 	metaslab_t *msp = avl_find(t, search, &idx);
3032 	if (msp == NULL)
3033 		msp = avl_nearest(t, idx, AVL_AFTER);
3034 
3035 	for (; msp != NULL; msp = AVL_NEXT(t, msp)) {
3036 		int i;
3037 		if (!metaslab_should_allocate(msp, asize)) {
3038 			metaslab_trace_add(zal, mg, msp, asize, d,
3039 			    TRACE_TOO_SMALL, allocator);
3040 			continue;
3041 		}
3042 
3043 		/*
3044 			 * If the selected metaslab is condensing or being
3045 			 * initialized, skip it.
3046 		 */
3047 			if (msp->ms_condensing || msp->ms_initializing > 0)
3048 			continue;
3049 
3050 		*was_active = msp->ms_allocator != -1;
3051 		/*
3052 		 * If we're activating as primary, this is our first allocation
3053 		 * from this disk, so we don't need to check how close we are.
3054 		 * If the metaslab under consideration was already active,
3055 		 * we're getting desperate enough to steal another allocator's
3056 		 * metaslab, so we still don't care about distances.
3057 		 */
3058 		if (activation_weight == METASLAB_WEIGHT_PRIMARY || *was_active)
3059 			break;
3060 
3061 		uint64_t target_distance = min_distance
3062 		    + (space_map_allocated(msp->ms_sm) != 0 ? 0 :
3063 		    min_distance >> 1);
3064 
3065 		for (i = 0; i < d; i++) {
3066 			if (metaslab_distance(msp, &dva[i]) < target_distance)
3067 				break;
3068 		}
3069 		if (i == d)
3070 			break;
3071 	}
3072 
3073 	if (msp != NULL) {
3074 		search->ms_weight = msp->ms_weight;
3075 		search->ms_start = msp->ms_start + 1;
3076 		search->ms_allocator = msp->ms_allocator;
3077 		search->ms_primary = msp->ms_primary;
3078 	}
3079 	return (msp);
3080 }
3081 
3082 /* ARGSUSED */
3083 static uint64_t
metaslab_group_alloc_normal(metaslab_group_t * mg,zio_alloc_list_t * zal,uint64_t asize,uint64_t txg,uint64_t min_distance,dva_t * dva,int d,int allocator)3084 metaslab_group_alloc_normal(metaslab_group_t *mg, zio_alloc_list_t *zal,
3085     uint64_t asize, uint64_t txg, uint64_t min_distance, dva_t *dva, int d,
3086     int allocator)
3087 {
3088 	metaslab_t *msp = NULL;
3089 	uint64_t offset = -1ULL;
3090 	uint64_t activation_weight;
3091 
3092 	activation_weight = METASLAB_WEIGHT_PRIMARY;
3093 	for (int i = 0; i < d; i++) {
3094 		if (activation_weight == METASLAB_WEIGHT_PRIMARY &&
3095 		    DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
3096 			activation_weight = METASLAB_WEIGHT_SECONDARY;
3097 		} else if (activation_weight == METASLAB_WEIGHT_SECONDARY &&
3098 		    DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
3099 			activation_weight = METASLAB_WEIGHT_CLAIM;
3100 			break;
3101 		}
3102 	}
3103 
3104 	/*
3105 	 * If we don't have enough metaslabs active to fill the entire array, we
3106 	 * just use the 0th slot.
3107 	 */
3108 	if (mg->mg_ms_ready < mg->mg_allocators * 3)
3109 		allocator = 0;
3110 
3111 	ASSERT3U(mg->mg_vd->vdev_ms_count, >=, 2);
3112 
3113 	metaslab_t *search = kmem_alloc(sizeof (*search), KM_SLEEP);
3114 	search->ms_weight = UINT64_MAX;
3115 	search->ms_start = 0;
3116 	/*
3117 	 * At the end of the metaslab tree are the already-active metaslabs,
3118 	 * first the primaries, then the secondaries. When we resume searching
3119 	 * through the tree, we need to consider ms_allocator and ms_primary so
3120 	 * we start in the location right after where we left off, and don't
3121 	 * accidentally loop forever considering the same metaslabs.
3122 	 */
3123 	search->ms_allocator = -1;
3124 	search->ms_primary = B_TRUE;
3125 	for (;;) {
3126 		boolean_t was_active = B_FALSE;
3127 
3128 		mutex_enter(&mg->mg_lock);
3129 
3130 		if (activation_weight == METASLAB_WEIGHT_PRIMARY &&
3131 		    mg->mg_primaries[allocator] != NULL) {
3132 			msp = mg->mg_primaries[allocator];
3133 			was_active = B_TRUE;
3134 		} else if (activation_weight == METASLAB_WEIGHT_SECONDARY &&
3135 		    mg->mg_secondaries[allocator] != NULL) {
3136 			msp = mg->mg_secondaries[allocator];
3137 			was_active = B_TRUE;
3138 		} else {
3139 			msp = find_valid_metaslab(mg, activation_weight, dva, d,
3140 			    min_distance, asize, allocator, zal, search,
3141 			    &was_active);
3142 		}
3143 
3144 		mutex_exit(&mg->mg_lock);
3145 		if (msp == NULL) {
3146 			kmem_free(search, sizeof (*search));
3147 			return (-1ULL);
3148 		}
3149 
3150 		mutex_enter(&msp->ms_lock);
3151 		/*
3152 		 * Ensure that the metaslab we have selected is still
3153 		 * capable of handling our request. It's possible that
3154 		 * another thread may have changed the weight while we
3155 		 * were blocked on the metaslab lock. We check the
3156 		 * active status first to see if we need to reselect
3157 		 * a new metaslab.
3158 		 */
3159 		if (was_active && !(msp->ms_weight & METASLAB_ACTIVE_MASK)) {
3160 			mutex_exit(&msp->ms_lock);
3161 			continue;
3162 		}
3163 
3164 		/*
3165 		 * If the metaslab is freshly activated for an allocator that
3166 		 * isn't the one we're allocating from, or if it's a primary and
3167 		 * we're seeking a secondary (or vice versa), we go back and
3168 		 * select a new metaslab.
3169 		 */
3170 		if (!was_active && (msp->ms_weight & METASLAB_ACTIVE_MASK) &&
3171 		    (msp->ms_allocator != -1) &&
3172 		    (msp->ms_allocator != allocator || ((activation_weight ==
3173 		    METASLAB_WEIGHT_PRIMARY) != msp->ms_primary))) {
3174 			mutex_exit(&msp->ms_lock);
3175 			continue;
3176 		}
3177 
3178 		if (msp->ms_weight & METASLAB_WEIGHT_CLAIM &&
3179 		    activation_weight != METASLAB_WEIGHT_CLAIM) {
3180 			metaslab_passivate(msp, msp->ms_weight &
3181 			    ~METASLAB_WEIGHT_CLAIM);
3182 			mutex_exit(&msp->ms_lock);
3183 			continue;
3184 		}
3185 
3186 		if (metaslab_activate(msp, allocator, activation_weight) != 0) {
3187 			mutex_exit(&msp->ms_lock);
3188 			continue;
3189 		}
3190 
3191 		msp->ms_selected_txg = txg;
3192 
3193 		/*
3194 		 * Now that we have the lock, recheck to see if we should
3195 		 * continue to use this metaslab for this allocation. The
3196 		 * the metaslab is now loaded so metaslab_should_allocate() can
3197 		 * accurately determine if the allocation attempt should
3198 		 * proceed.
3199 		 */
3200 		if (!metaslab_should_allocate(msp, asize)) {
3201 			/* Passivate this metaslab and select a new one. */
3202 			metaslab_trace_add(zal, mg, msp, asize, d,
3203 			    TRACE_TOO_SMALL, allocator);
3204 			goto next;
3205 		}
3206 
3207 		/*
3208 		 * If this metaslab is currently condensing then pick again as
3209 		 * we can't manipulate this metaslab until it's committed
3210 		 * to disk. If this metaslab is being initialized, we shouldn't
3211 		 * allocate from it since the allocated region might be
3212 		 * overwritten after allocation.
3213 		 */
3214 		if (msp->ms_condensing) {
3215 			metaslab_trace_add(zal, mg, msp, asize, d,
3216 			    TRACE_CONDENSING, allocator);
3217 			metaslab_passivate(msp, msp->ms_weight &
3218 			    ~METASLAB_ACTIVE_MASK);
3219 			mutex_exit(&msp->ms_lock);
3220 			continue;
3221 		} else if (msp->ms_initializing > 0) {
3222 			metaslab_trace_add(zal, mg, msp, asize, d,
3223 			    TRACE_INITIALIZING, allocator);
3224 			metaslab_passivate(msp, msp->ms_weight &
3225 			    ~METASLAB_ACTIVE_MASK);
3226 			mutex_exit(&msp->ms_lock);
3227 			continue;
3228 		}
3229 
3230 		offset = metaslab_block_alloc(msp, asize, txg);
3231 		metaslab_trace_add(zal, mg, msp, asize, d, offset, allocator);
3232 
3233 		if (offset != -1ULL) {
3234 			/* Proactively passivate the metaslab, if needed */
3235 			metaslab_segment_may_passivate(msp);
3236 			break;
3237 		}
3238 next:
3239 		ASSERT(msp->ms_loaded);
3240 
3241 		/*
3242 		 * We were unable to allocate from this metaslab so determine
3243 		 * a new weight for this metaslab. Now that we have loaded
3244 		 * the metaslab we can provide a better hint to the metaslab
3245 		 * selector.
3246 		 *
3247 		 * For space-based metaslabs, we use the maximum block size.
3248 		 * This information is only available when the metaslab
3249 		 * is loaded and is more accurate than the generic free
3250 		 * space weight that was calculated by metaslab_weight().
3251 		 * This information allows us to quickly compare the maximum
3252 		 * available allocation in the metaslab to the allocation
3253 		 * size being requested.
3254 		 *
3255 		 * For segment-based metaslabs, determine the new weight
3256 		 * based on the highest bucket in the range tree. We
3257 		 * explicitly use the loaded segment weight (i.e. the range
3258 		 * tree histogram) since it contains the space that is
3259 		 * currently available for allocation and is accurate
3260 		 * even within a sync pass.
3261 		 */
3262 		if (WEIGHT_IS_SPACEBASED(msp->ms_weight)) {
3263 			uint64_t weight = metaslab_block_maxsize(msp);
3264 			WEIGHT_SET_SPACEBASED(weight);
3265 			metaslab_passivate(msp, weight);
3266 		} else {
3267 			metaslab_passivate(msp,
3268 			    metaslab_weight_from_range_tree(msp));
3269 		}
3270 
3271 		/*
3272 		 * We have just failed an allocation attempt, check
3273 		 * that metaslab_should_allocate() agrees. Otherwise,
3274 		 * we may end up in an infinite loop retrying the same
3275 		 * metaslab.
3276 		 */
3277 		ASSERT(!metaslab_should_allocate(msp, asize));
3278 		mutex_exit(&msp->ms_lock);
3279 	}
3280 	mutex_exit(&msp->ms_lock);
3281 	kmem_free(search, sizeof (*search));
3282 	return (offset);
3283 }
3284 
3285 static uint64_t
metaslab_group_alloc(metaslab_group_t * mg,zio_alloc_list_t * zal,uint64_t asize,uint64_t txg,uint64_t min_distance,dva_t * dva,int d,int allocator)3286 metaslab_group_alloc(metaslab_group_t *mg, zio_alloc_list_t *zal,
3287     uint64_t asize, uint64_t txg, uint64_t min_distance, dva_t *dva, int d,
3288     int allocator)
3289 {
3290 	uint64_t offset;
3291 	ASSERT(mg->mg_initialized);
3292 
3293 	offset = metaslab_group_alloc_normal(mg, zal, asize, txg,
3294 	    min_distance, dva, d, allocator);
3295 
3296 	mutex_enter(&mg->mg_lock);
3297 	if (offset == -1ULL) {
3298 		mg->mg_failed_allocations++;
3299 		metaslab_trace_add(zal, mg, NULL, asize, d,
3300 		    TRACE_GROUP_FAILURE, allocator);
3301 		if (asize == SPA_GANGBLOCKSIZE) {
3302 			/*
3303 			 * This metaslab group was unable to allocate
3304 			 * the minimum gang block size so it must be out of
3305 			 * space. We must notify the allocation throttle
3306 			 * to start skipping allocation attempts to this
3307 			 * metaslab group until more space becomes available.
3308 			 * Note: this failure cannot be caused by the
3309 			 * allocation throttle since the allocation throttle
3310 			 * is only responsible for skipping devices and
3311 			 * not failing block allocations.
3312 			 */
3313 			mg->mg_no_free_space = B_TRUE;
3314 		}
3315 	}
3316 	mg->mg_allocations++;
3317 	mutex_exit(&mg->mg_lock);
3318 	return (offset);
3319 }
3320 
3321 /*
3322  * If we have to write a ditto block (i.e. more than one DVA for a given BP)
3323  * on the same vdev as an existing DVA of this BP, then try to allocate it
3324  * at least (vdev_asize / (2 ^ ditto_same_vdev_distance_shift)) away from the
3325  * existing DVAs.
3326  */
3327 int ditto_same_vdev_distance_shift = 3;
3328 
3329 /*
3330  * Allocate a block for the specified i/o.
3331  */
3332 int
metaslab_alloc_dva(spa_t * spa,metaslab_class_t * mc,uint64_t psize,dva_t * dva,int d,dva_t * hintdva,uint64_t txg,int flags,zio_alloc_list_t * zal,int allocator)3333 metaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
3334     dva_t *dva, int d, dva_t *hintdva, uint64_t txg, int flags,
3335     zio_alloc_list_t *zal, int allocator)
3336 {
3337 	metaslab_group_t *mg, *rotor;
3338 	vdev_t *vd;
3339 	boolean_t try_hard = B_FALSE;
3340 
3341 	ASSERT(!DVA_IS_VALID(&dva[d]));
3342 
3343 	/*
3344 	 * For testing, make some blocks above a certain size be gang blocks.
3345 	 */
3346 	if (psize >= metaslab_force_ganging && (ddi_get_lbolt() & 3) == 0) {
3347 		metaslab_trace_add(zal, NULL, NULL, psize, d, TRACE_FORCE_GANG,
3348 		    allocator);
3349 		return (SET_ERROR(ENOSPC));
3350 	}
3351 
3352 	/*
3353 	 * Start at the rotor and loop through all mgs until we find something.
3354 	 * Note that there's no locking on mc_rotor or mc_aliquot because
3355 	 * nothing actually breaks if we miss a few updates -- we just won't
3356 	 * allocate quite as evenly.  It all balances out over time.
3357 	 *
3358 	 * If we are doing ditto or log blocks, try to spread them across
3359 	 * consecutive vdevs.  If we're forced to reuse a vdev before we've
3360 	 * allocated all of our ditto blocks, then try and spread them out on
3361 	 * that vdev as much as possible.  If it turns out to not be possible,
3362 	 * gradually lower our standards until anything becomes acceptable.
3363 	 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
3364 	 * gives us hope of containing our fault domains to something we're
3365 	 * able to reason about.  Otherwise, any two top-level vdev failures
3366 	 * will guarantee the loss of data.  With consecutive allocation,
3367 	 * only two adjacent top-level vdev failures will result in data loss.
3368 	 *
3369 	 * If we are doing gang blocks (hintdva is non-NULL), try to keep
3370 	 * ourselves on the same vdev as our gang block header.  That
3371 	 * way, we can hope for locality in vdev_cache, plus it makes our
3372 	 * fault domains something tractable.
3373 	 */
3374 	if (hintdva) {
3375 		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
3376 
3377 		/*
3378 		 * It's possible the vdev we're using as the hint no
3379 		 * longer exists or its mg has been closed (e.g. by
3380 		 * device removal).  Consult the rotor when
3381 		 * all else fails.
3382 		 */
3383 		if (vd != NULL && vd->vdev_mg != NULL) {
3384 			mg = vd->vdev_mg;
3385 
3386 			if (flags & METASLAB_HINTBP_AVOID &&
3387 			    mg->mg_next != NULL)
3388 				mg = mg->mg_next;
3389 		} else {
3390 			mg = mc->mc_rotor;
3391 		}
3392 	} else if (d != 0) {
3393 		vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
3394 		mg = vd->vdev_mg->mg_next;
3395 	} else {
3396 		mg = mc->mc_rotor;
3397 	}
3398 
3399 	/*
3400 	 * If the hint put us into the wrong metaslab class, or into a
3401 	 * metaslab group that has been passivated, just follow the rotor.
3402 	 */
3403 	if (mg->mg_class != mc || mg->mg_activation_count <= 0)
3404 		mg = mc->mc_rotor;
3405 
3406 	rotor = mg;
3407 top:
3408 	do {
3409 		boolean_t allocatable;
3410 
3411 		ASSERT(mg->mg_activation_count == 1);
3412 		vd = mg->mg_vd;
3413 
3414 		/*
3415 		 * Don't allocate from faulted devices.
3416 		 */
3417 		if (try_hard) {
3418 			spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
3419 			allocatable = vdev_allocatable(vd);
3420 			spa_config_exit(spa, SCL_ZIO, FTAG);
3421 		} else {
3422 			allocatable = vdev_allocatable(vd);
3423 		}
3424 
3425 		/*
3426 		 * Determine if the selected metaslab group is eligible
3427 		 * for allocations. If we're ganging then don't allow
3428 		 * this metaslab group to skip allocations since that would
3429 		 * inadvertently return ENOSPC and suspend the pool
3430 		 * even though space is still available.
3431 		 */
3432 		if (allocatable && !GANG_ALLOCATION(flags) && !try_hard) {
3433 			allocatable = metaslab_group_allocatable(mg, rotor,
3434 			    psize, allocator, d);
3435 		}
3436 
3437 		if (!allocatable) {
3438 			metaslab_trace_add(zal, mg, NULL, psize, d,
3439 			    TRACE_NOT_ALLOCATABLE, allocator);
3440 			goto next;
3441 		}
3442 
3443 		ASSERT(mg->mg_initialized);
3444 
3445 		/*
3446 		 * Avoid writing single-copy data to a failing,
3447 		 * non-redundant vdev, unless we've already tried all
3448 		 * other vdevs.
3449 		 */
3450 		if ((vd->vdev_stat.vs_write_errors > 0 ||
3451 		    vd->vdev_state < VDEV_STATE_HEALTHY) &&
3452 		    d == 0 && !try_hard && vd->vdev_children == 0) {
3453 			metaslab_trace_add(zal, mg, NULL, psize, d,
3454 			    TRACE_VDEV_ERROR, allocator);
3455 			goto next;
3456 		}
3457 
3458 		ASSERT(mg->mg_class == mc);
3459 
3460 		/*
3461 		 * If we don't need to try hard, then require that the
3462 		 * block be 1/8th of the device away from any other DVAs
3463 		 * in this BP.  If we are trying hard, allow any offset
3464 		 * to be used (distance=0).
3465 		 */
3466 		uint64_t distance = 0;
3467 		if (!try_hard) {
3468 			distance = vd->vdev_asize >>
3469 			    ditto_same_vdev_distance_shift;
3470 			if (distance <= (1ULL << vd->vdev_ms_shift))
3471 				distance = 0;
3472 		}
3473 
3474 		uint64_t asize = vdev_psize_to_asize(vd, psize);
3475 		ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
3476 
3477 		uint64_t offset = metaslab_group_alloc(mg, zal, asize, txg,
3478 		    distance, dva, d, allocator);
3479 
3480 		if (offset != -1ULL) {
3481 			/*
3482 			 * If we've just selected this metaslab group,
3483 			 * figure out whether the corresponding vdev is
3484 			 * over- or under-used relative to the pool,
3485 			 * and set an allocation bias to even it out.
3486 			 */
3487 			if (mc->mc_aliquot == 0 && metaslab_bias_enabled) {
3488 				vdev_stat_t *vs = &vd->vdev_stat;
3489 				int64_t vu, cu;
3490 
3491 				vu = (vs->vs_alloc * 100) / (vs->vs_space + 1);
3492 				cu = (mc->mc_alloc * 100) / (mc->mc_space + 1);
3493 
3494 				/*
3495 				 * Calculate how much more or less we should
3496 				 * try to allocate from this device during
3497 				 * this iteration around the rotor.
3498 				 * For example, if a device is 80% full
3499 				 * and the pool is 20% full then we should
3500 				 * reduce allocations by 60% on this device.
3501 				 *
3502 				 * mg_bias = (20 - 80) * 512K / 100 = -307K
3503 				 *
3504 				 * This reduces allocations by 307K for this
3505 				 * iteration.
3506 				 */
3507 				mg->mg_bias = ((cu - vu) *
3508 				    (int64_t)mg->mg_aliquot) / 100;
3509 			} else if (!metaslab_bias_enabled) {
3510 				mg->mg_bias = 0;
3511 			}
3512 
3513 			if (atomic_add_64_nv(&mc->mc_aliquot, asize) >=
3514 			    mg->mg_aliquot + mg->mg_bias) {
3515 				mc->mc_rotor = mg->mg_next;
3516 				mc->mc_aliquot = 0;
3517 			}
3518 
3519 			DVA_SET_VDEV(&dva[d], vd->vdev_id);
3520 			DVA_SET_OFFSET(&dva[d], offset);
3521 			DVA_SET_GANG(&dva[d], !!(flags & METASLAB_GANG_HEADER));
3522 			DVA_SET_ASIZE(&dva[d], asize);
3523 
3524 			return (0);
3525 		}
3526 next:
3527 		mc->mc_rotor = mg->mg_next;
3528 		mc->mc_aliquot = 0;
3529 	} while ((mg = mg->mg_next) != rotor);
3530 
3531 	/*
3532 	 * If we haven't tried hard, do so now.
3533 	 */
3534 	if (!try_hard) {
3535 		try_hard = B_TRUE;
3536 		goto top;
3537 	}
3538 
3539 	bzero(&dva[d], sizeof (dva_t));
3540 
3541 	metaslab_trace_add(zal, rotor, NULL, psize, d, TRACE_ENOSPC, allocator);
3542 	return (SET_ERROR(ENOSPC));
3543 }
3544 
3545 void
metaslab_free_concrete(vdev_t * vd,uint64_t offset,uint64_t asize,boolean_t checkpoint)3546 metaslab_free_concrete(vdev_t *vd, uint64_t offset, uint64_t asize,
3547     boolean_t checkpoint)
3548 {
3549 	metaslab_t *msp;
3550 	spa_t *spa = vd->vdev_spa;
3551 
3552 	ASSERT(vdev_is_concrete(vd));
3553 	ASSERT3U(spa_config_held(spa, SCL_ALL, RW_READER), !=, 0);
3554 	ASSERT3U(offset >> vd->vdev_ms_shift, <, vd->vdev_ms_count);
3555 
3556 	msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
3557 
3558 	VERIFY(!msp->ms_condensing);
3559 	VERIFY3U(offset, >=, msp->ms_start);
3560 	VERIFY3U(offset + asize, <=, msp->ms_start + msp->ms_size);
3561 	VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
3562 	VERIFY0(P2PHASE(asize, 1ULL << vd->vdev_ashift));
3563 
3564 	metaslab_check_free_impl(vd, offset, asize);
3565 
3566 	mutex_enter(&msp->ms_lock);
3567 	if (range_tree_is_empty(msp->ms_freeing) &&
3568 	    range_tree_is_empty(msp->ms_checkpointing)) {
3569 		vdev_dirty(vd, VDD_METASLAB, msp, spa_syncing_txg(spa));
3570 	}
3571 
3572 	if (checkpoint) {
3573 		ASSERT(spa_has_checkpoint(spa));
3574 		range_tree_add(msp->ms_checkpointing, offset, asize);
3575 	} else {
3576 		range_tree_add(msp->ms_freeing, offset, asize);
3577 	}
3578 	mutex_exit(&msp->ms_lock);
3579 }
3580 
3581 /* ARGSUSED */
3582 void
metaslab_free_impl_cb(uint64_t inner_offset,vdev_t * vd,uint64_t offset,uint64_t size,void * arg)3583 metaslab_free_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
3584     uint64_t size, void *arg)
3585 {
3586 	boolean_t *checkpoint = arg;
3587 
3588 	ASSERT3P(checkpoint, !=, NULL);
3589 
3590 	if (vd->vdev_ops->vdev_op_remap != NULL)
3591 		vdev_indirect_mark_obsolete(vd, offset, size);
3592 	else
3593 		metaslab_free_impl(vd, offset, size, *checkpoint);
3594 }
3595 
3596 static void
metaslab_free_impl(vdev_t * vd,uint64_t offset,uint64_t size,boolean_t checkpoint)3597 metaslab_free_impl(vdev_t *vd, uint64_t offset, uint64_t size,
3598     boolean_t checkpoint)
3599 {
3600 	spa_t *spa = vd->vdev_spa;
3601 
3602 	ASSERT3U(spa_config_held(spa, SCL_ALL, RW_READER), !=, 0);
3603 
3604 	if (spa_syncing_txg(spa) > spa_freeze_txg(spa))
3605 		return;
3606 
3607 	if (spa->spa_vdev_removal != NULL &&
3608 	    spa->spa_vdev_removal->svr_vdev_id == vd->vdev_id &&
3609 	    vdev_is_concrete(vd)) {
3610 		/*
3611 		 * Note: we check if the vdev is concrete because when
3612 		 * we complete the removal, we first change the vdev to be
3613 		 * an indirect vdev (in open context), and then (in syncing
3614 		 * context) clear spa_vdev_removal.
3615 		 */
3616 		free_from_removing_vdev(vd, offset, size);
3617 	} else if (vd->vdev_ops->vdev_op_remap != NULL) {
3618 		vdev_indirect_mark_obsolete(vd, offset, size);
3619 		vd->vdev_ops->vdev_op_remap(vd, offset, size,
3620 		    metaslab_free_impl_cb, &checkpoint);
3621 	} else {
3622 		metaslab_free_concrete(vd, offset, size, checkpoint);
3623 	}
3624 }
3625 
3626 typedef struct remap_blkptr_cb_arg {
3627 	blkptr_t *rbca_bp;
3628 	spa_remap_cb_t rbca_cb;
3629 	vdev_t *rbca_remap_vd;
3630 	uint64_t rbca_remap_offset;
3631 	void *rbca_cb_arg;
3632 } remap_blkptr_cb_arg_t;
3633 
3634 void
remap_blkptr_cb(uint64_t inner_offset,vdev_t * vd,uint64_t offset,uint64_t size,void * arg)3635 remap_blkptr_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
3636     uint64_t size, void *arg)
3637 {
3638 	remap_blkptr_cb_arg_t *rbca = arg;
3639 	blkptr_t *bp = rbca->rbca_bp;
3640 
3641 	/* We can not remap split blocks. */
3642 	if (size != DVA_GET_ASIZE(&bp->blk_dva[0]))
3643 		return;
3644 	ASSERT0(inner_offset);
3645 
3646 	if (rbca->rbca_cb != NULL) {
3647 		/*
3648 		 * At this point we know that we are not handling split
3649 		 * blocks and we invoke the callback on the previous
3650 		 * vdev which must be indirect.
3651 		 */
3652 		ASSERT3P(rbca->rbca_remap_vd->vdev_ops, ==, &vdev_indirect_ops);
3653 
3654 		rbca->rbca_cb(rbca->rbca_remap_vd->vdev_id,
3655 		    rbca->rbca_remap_offset, size, rbca->rbca_cb_arg);
3656 
3657 		/* set up remap_blkptr_cb_arg for the next call */
3658 		rbca->rbca_remap_vd = vd;
3659 		rbca->rbca_remap_offset = offset;
3660 	}
3661 
3662 	/*
3663 	 * The phys birth time is that of dva[0].  This ensures that we know
3664 	 * when each dva was written, so that resilver can determine which
3665 	 * blocks need to be scrubbed (i.e. those written during the time
3666 	 * the vdev was offline).  It also ensures that the key used in
3667 	 * the ARC hash table is unique (i.e. dva[0] + phys_birth).  If
3668 	 * we didn't change the phys_birth, a lookup in the ARC for a
3669 	 * remapped BP could find the data that was previously stored at
3670 	 * this vdev + offset.
3671 	 */
3672 	vdev_t *oldvd = vdev_lookup_top(vd->vdev_spa,
3673 	    DVA_GET_VDEV(&bp->blk_dva[0]));
3674 	vdev_indirect_births_t *vib = oldvd->vdev_indirect_births;
3675 	bp->blk_phys_birth = vdev_indirect_births_physbirth(vib,
3676 	    DVA_GET_OFFSET(&bp->blk_dva[0]), DVA_GET_ASIZE(&bp->blk_dva[0]));
3677 
3678 	DVA_SET_VDEV(&bp->blk_dva[0], vd->vdev_id);
3679 	DVA_SET_OFFSET(&bp->blk_dva[0], offset);
3680 }
3681 
3682 /*
3683  * If the block pointer contains any indirect DVAs, modify them to refer to
3684  * concrete DVAs.  Note that this will sometimes not be possible, leaving
3685  * the indirect DVA in place.  This happens if the indirect DVA spans multiple
3686  * segments in the mapping (i.e. it is a "split block").
3687  *
3688  * If the BP was remapped, calls the callback on the original dva (note the
3689  * callback can be called multiple times if the original indirect DVA refers
3690  * to another indirect DVA, etc).
3691  *
3692  * Returns TRUE if the BP was remapped.
3693  */
3694 boolean_t
spa_remap_blkptr(spa_t * spa,blkptr_t * bp,spa_remap_cb_t callback,void * arg)3695 spa_remap_blkptr(spa_t *spa, blkptr_t *bp, spa_remap_cb_t callback, void *arg)
3696 {
3697 	remap_blkptr_cb_arg_t rbca;
3698 
3699 	if (!zfs_remap_blkptr_enable)
3700 		return (B_FALSE);
3701 
3702 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS))
3703 		return (B_FALSE);
3704 
3705 	/*
3706 	 * Dedup BP's can not be remapped, because ddt_phys_select() depends
3707 	 * on DVA[0] being the same in the BP as in the DDT (dedup table).
3708 	 */
3709 	if (BP_GET_DEDUP(bp))
3710 		return (B_FALSE);
3711 
3712 	/*
3713 	 * Gang blocks can not be remapped, because
3714 	 * zio_checksum_gang_verifier() depends on the DVA[0] that's in
3715 	 * the BP used to read the gang block header (GBH) being the same
3716 	 * as the DVA[0] that we allocated for the GBH.
3717 	 */
3718 	if (BP_IS_GANG(bp))
3719 		return (B_FALSE);
3720 
3721 	/*
3722 	 * Embedded BP's have no DVA to remap.
3723 	 */
3724 	if (BP_GET_NDVAS(bp) < 1)
3725 		return (B_FALSE);
3726 
3727 	/*
3728 	 * Note: we only remap dva[0].  If we remapped other dvas, we
3729 	 * would no longer know what their phys birth txg is.
3730 	 */
3731 	dva_t *dva = &bp->blk_dva[0];
3732 
3733 	uint64_t offset = DVA_GET_OFFSET(dva);
3734 	uint64_t size = DVA_GET_ASIZE(dva);
3735 	vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
3736 
3737 	if (vd->vdev_ops->vdev_op_remap == NULL)
3738 		return (B_FALSE);
3739 
3740 	rbca.rbca_bp = bp;
3741 	rbca.rbca_cb = callback;
3742 	rbca.rbca_remap_vd = vd;
3743 	rbca.rbca_remap_offset = offset;
3744 	rbca.rbca_cb_arg = arg;
3745 
3746 	/*
3747 	 * remap_blkptr_cb() will be called in order for each level of
3748 	 * indirection, until a concrete vdev is reached or a split block is
3749 	 * encountered. old_vd and old_offset are updated within the callback
3750 	 * as we go from the one indirect vdev to the next one (either concrete
3751 	 * or indirect again) in that order.
3752 	 */
3753 	vd->vdev_ops->vdev_op_remap(vd, offset, size, remap_blkptr_cb, &rbca);
3754 
3755 	/* Check if the DVA wasn't remapped because it is a split block */
3756 	if (DVA_GET_VDEV(&rbca.rbca_bp->blk_dva[0]) == vd->vdev_id)
3757 		return (B_FALSE);
3758 
3759 	return (B_TRUE);
3760 }
3761 
3762 /*
3763  * Undo the allocation of a DVA which happened in the given transaction group.
3764  */
3765 void
metaslab_unalloc_dva(spa_t * spa,const dva_t * dva,uint64_t txg)3766 metaslab_unalloc_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
3767 {
3768 	metaslab_t *msp;
3769 	vdev_t *vd;
3770 	uint64_t vdev = DVA_GET_VDEV(dva);
3771 	uint64_t offset = DVA_GET_OFFSET(dva);
3772 	uint64_t size = DVA_GET_ASIZE(dva);
3773 
3774 	ASSERT(DVA_IS_VALID(dva));
3775 	ASSERT3U(spa_config_held(spa, SCL_ALL, RW_READER), !=, 0);
3776 
3777 	if (txg > spa_freeze_txg(spa))
3778 		return;
3779 
3780 	if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
3781 	    (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
3782 		cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
3783 		    (u_longlong_t)vdev, (u_longlong_t)offset);
3784 		ASSERT(0);
3785 		return;
3786 	}
3787 
3788 	ASSERT(!vd->vdev_removing);
3789 	ASSERT(vdev_is_concrete(vd));
3790 	ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3791 	ASSERT3P(vd->vdev_indirect_mapping, ==, NULL);
3792 
3793 	if (DVA_GET_GANG(dva))
3794 		size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
3795 
3796 	msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
3797 
3798 	mutex_enter(&msp->ms_lock);
3799 	range_tree_remove(msp->ms_allocating[txg & TXG_MASK],
3800 	    offset, size);
3801 
3802 	VERIFY(!msp->ms_condensing);
3803 	VERIFY3U(offset, >=, msp->ms_start);
3804 	VERIFY3U(offset + size, <=, msp->ms_start + msp->ms_size);
3805 	VERIFY3U(range_tree_space(msp->ms_allocatable) + size, <=,
3806 	    msp->ms_size);
3807 	VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
3808 	VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
3809 	range_tree_add(msp->ms_allocatable, offset, size);
3810 	mutex_exit(&msp->ms_lock);
3811 }
3812 
3813 /*
3814  * Free the block represented by the given DVA.
3815  */
3816 void
metaslab_free_dva(spa_t * spa,const dva_t * dva,boolean_t checkpoint)3817 metaslab_free_dva(spa_t *spa, const dva_t *dva, boolean_t checkpoint)
3818 {
3819 	uint64_t vdev = DVA_GET_VDEV(dva);
3820 	uint64_t offset = DVA_GET_OFFSET(dva);
3821 	uint64_t size = DVA_GET_ASIZE(dva);
3822 	vdev_t *vd = vdev_lookup_top(spa, vdev);
3823 
3824 	ASSERT(DVA_IS_VALID(dva));
3825 	ASSERT3U(spa_config_held(spa, SCL_ALL, RW_READER), !=, 0);
3826 
3827 	if (DVA_GET_GANG(dva)) {
3828 		size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
3829 	}
3830 
3831 	metaslab_free_impl(vd, offset, size, checkpoint);
3832 }
3833 
3834 /*
3835  * Reserve some allocation slots. The reservation system must be called
3836  * before we call into the allocator. If there aren't any available slots
3837  * then the I/O will be throttled until an I/O completes and its slots are
3838  * freed up. The function returns true if it was successful in placing
3839  * the reservation.
3840  */
3841 boolean_t
metaslab_class_throttle_reserve(metaslab_class_t * mc,int slots,int allocator,zio_t * zio,int flags)3842 metaslab_class_throttle_reserve(metaslab_class_t *mc, int slots, int allocator,
3843     zio_t *zio, int flags)
3844 {
3845 	uint64_t available_slots = 0;
3846 	boolean_t slot_reserved = B_FALSE;
3847 	uint64_t max = mc->mc_alloc_max_slots[allocator];
3848 
3849 	ASSERT(mc->mc_alloc_throttle_enabled);
3850 	mutex_enter(&mc->mc_lock);
3851 
3852 	uint64_t reserved_slots =
3853 	    refcount_count(&mc->mc_alloc_slots[allocator]);
3854 	if (reserved_slots < max)
3855 		available_slots = max - reserved_slots;
3856 
3857 	if (slots <= available_slots || GANG_ALLOCATION(flags)) {
3858 		/*
3859 		 * We reserve the slots individually so that we can unreserve
3860 		 * them individually when an I/O completes.
3861 		 */
3862 		for (int d = 0; d < slots; d++) {
3863 			reserved_slots =
3864 			    refcount_add(&mc->mc_alloc_slots[allocator],
3865 			    zio);
3866 		}
3867 		zio->io_flags |= ZIO_FLAG_IO_ALLOCATING;
3868 		slot_reserved = B_TRUE;
3869 	}
3870 
3871 	mutex_exit(&mc->mc_lock);
3872 	return (slot_reserved);
3873 }
3874 
3875 void
metaslab_class_throttle_unreserve(metaslab_class_t * mc,int slots,int allocator,zio_t * zio)3876 metaslab_class_throttle_unreserve(metaslab_class_t *mc, int slots,
3877     int allocator, zio_t *zio)
3878 {
3879 	ASSERT(mc->mc_alloc_throttle_enabled);
3880 	mutex_enter(&mc->mc_lock);
3881 	for (int d = 0; d < slots; d++) {
3882 		(void) refcount_remove(&mc->mc_alloc_slots[allocator],
3883 		    zio);
3884 	}
3885 	mutex_exit(&mc->mc_lock);
3886 }
3887 
3888 static int
metaslab_claim_concrete(vdev_t * vd,uint64_t offset,uint64_t size,uint64_t txg)3889 metaslab_claim_concrete(vdev_t *vd, uint64_t offset, uint64_t size,
3890     uint64_t txg)
3891 {
3892 	metaslab_t *msp;
3893 	spa_t *spa = vd->vdev_spa;
3894 	int error = 0;
3895 
3896 	if (offset >> vd->vdev_ms_shift >= vd->vdev_ms_count)
3897 		return (ENXIO);
3898 
3899 	ASSERT3P(vd->vdev_ms, !=, NULL);
3900 	msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
3901 
3902 	mutex_enter(&msp->ms_lock);
3903 
3904 	if ((txg != 0 && spa_writeable(spa)) || !msp->ms_loaded)
3905 		error = metaslab_activate(msp, 0, METASLAB_WEIGHT_CLAIM);
3906 	/*
3907 	 * No need to fail in that case; someone else has activated the
3908 	 * metaslab, but that doesn't preclude us from using it.
3909 	 */
3910 	if (error == EBUSY)
3911 		error = 0;
3912 
3913 	if (error == 0 &&
3914 	    !range_tree_contains(msp->ms_allocatable, offset, size))
3915 		error = SET_ERROR(ENOENT);
3916 
3917 	if (error || txg == 0) {	/* txg == 0 indicates dry run */
3918 		mutex_exit(&msp->ms_lock);
3919 		return (error);
3920 	}
3921 
3922 	VERIFY(!msp->ms_condensing);
3923 	VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
3924 	VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
3925 	VERIFY3U(range_tree_space(msp->ms_allocatable) - size, <=,
3926 	    msp->ms_size);
3927 	range_tree_remove(msp->ms_allocatable, offset, size);
3928 
3929 	if (spa_writeable(spa)) {	/* don't dirty if we're zdb(1M) */
3930 		if (range_tree_is_empty(msp->ms_allocating[txg & TXG_MASK]))
3931 			vdev_dirty(vd, VDD_METASLAB, msp, txg);
3932 		range_tree_add(msp->ms_allocating[txg & TXG_MASK],
3933 		    offset, size);
3934 	}
3935 
3936 	mutex_exit(&msp->ms_lock);
3937 
3938 	return (0);
3939 }
3940 
3941 typedef struct metaslab_claim_cb_arg_t {
3942 	uint64_t	mcca_txg;
3943 	int		mcca_error;
3944 } metaslab_claim_cb_arg_t;
3945 
3946 /* ARGSUSED */
3947 static void
metaslab_claim_impl_cb(uint64_t inner_offset,vdev_t * vd,uint64_t offset,uint64_t size,void * arg)3948 metaslab_claim_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
3949     uint64_t size, void *arg)
3950 {
3951 	metaslab_claim_cb_arg_t *mcca_arg = arg;
3952 
3953 	if (mcca_arg->mcca_error == 0) {
3954 		mcca_arg->mcca_error = metaslab_claim_concrete(vd, offset,
3955 		    size, mcca_arg->mcca_txg);
3956 	}
3957 }
3958 
3959 int
metaslab_claim_impl(vdev_t * vd,uint64_t offset,uint64_t size,uint64_t txg)3960 metaslab_claim_impl(vdev_t *vd, uint64_t offset, uint64_t size, uint64_t txg)
3961 {
3962 	if (vd->vdev_ops->vdev_op_remap != NULL) {
3963 		metaslab_claim_cb_arg_t arg;
3964 
3965 		/*
3966 		 * Only zdb(1M) can claim on indirect vdevs.  This is used
3967 		 * to detect leaks of mapped space (that are not accounted
3968 		 * for in the obsolete counts, spacemap, or bpobj).
3969 		 */
3970 		ASSERT(!spa_writeable(vd->vdev_spa));
3971 		arg.mcca_error = 0;
3972 		arg.mcca_txg = txg;
3973 
3974 		vd->vdev_ops->vdev_op_remap(vd, offset, size,
3975 		    metaslab_claim_impl_cb, &arg);
3976 
3977 		if (arg.mcca_error == 0) {
3978 			arg.mcca_error = metaslab_claim_concrete(vd,
3979 			    offset, size, txg);
3980 		}
3981 		return (arg.mcca_error);
3982 	} else {
3983 		return (metaslab_claim_concrete(vd, offset, size, txg));
3984 	}
3985 }
3986 
3987 /*
3988  * Intent log support: upon opening the pool after a crash, notify the SPA
3989  * of blocks that the intent log has allocated for immediate write, but
3990  * which are still considered free by the SPA because the last transaction
3991  * group didn't commit yet.
3992  */
3993 static int
metaslab_claim_dva(spa_t * spa,const dva_t * dva,uint64_t txg)3994 metaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
3995 {
3996 	uint64_t vdev = DVA_GET_VDEV(dva);
3997 	uint64_t offset = DVA_GET_OFFSET(dva);
3998 	uint64_t size = DVA_GET_ASIZE(dva);
3999 	vdev_t *vd;
4000 
4001 	if ((vd = vdev_lookup_top(spa, vdev)) == NULL) {
4002 		return (SET_ERROR(ENXIO));
4003 	}
4004 
4005 	ASSERT(DVA_IS_VALID(dva));
4006 
4007 	if (DVA_GET_GANG(dva))
4008 		size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
4009 
4010 	return (metaslab_claim_impl(vd, offset, size, txg));
4011 }
4012 
4013 int
metaslab_alloc(spa_t * spa,metaslab_class_t * mc,uint64_t psize,blkptr_t * bp,int ndvas,uint64_t txg,blkptr_t * hintbp,int flags,zio_alloc_list_t * zal,zio_t * zio,int allocator)4014 metaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
4015     int ndvas, uint64_t txg, blkptr_t *hintbp, int flags,
4016     zio_alloc_list_t *zal, zio_t *zio, int allocator)
4017 {
4018 	dva_t *dva = bp->blk_dva;
4019 	dva_t *hintdva = hintbp->blk_dva;
4020 	int error = 0;
4021 
4022 	ASSERT(bp->blk_birth == 0);
4023 	ASSERT(BP_PHYSICAL_BIRTH(bp) == 0);
4024 
4025 	spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
4026 
4027 	if (mc->mc_rotor == NULL) {	/* no vdevs in this class */
4028 		spa_config_exit(spa, SCL_ALLOC, FTAG);
4029 		return (SET_ERROR(ENOSPC));
4030 	}
4031 
4032 	ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
4033 	ASSERT(BP_GET_NDVAS(bp) == 0);
4034 	ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
4035 	ASSERT3P(zal, !=, NULL);
4036 
4037 	for (int d = 0; d < ndvas; d++) {
4038 		error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
4039 		    txg, flags, zal, allocator);
4040 		if (error != 0) {
4041 			for (d--; d >= 0; d--) {
4042 				metaslab_unalloc_dva(spa, &dva[d], txg);
4043 				metaslab_group_alloc_decrement(spa,
4044 				    DVA_GET_VDEV(&dva[d]), zio, flags,
4045 				    allocator, B_FALSE);
4046 				bzero(&dva[d], sizeof (dva_t));
4047 			}
4048 			spa_config_exit(spa, SCL_ALLOC, FTAG);
4049 			return (error);
4050 		} else {
4051 			/*
4052 			 * Update the metaslab group's queue depth
4053 			 * based on the newly allocated dva.
4054 			 */
4055 			metaslab_group_alloc_increment(spa,
4056 			    DVA_GET_VDEV(&dva[d]), zio, flags, allocator);
4057 		}
4058 
4059 	}
4060 	ASSERT(error == 0);
4061 	ASSERT(BP_GET_NDVAS(bp) == ndvas);
4062 
4063 	spa_config_exit(spa, SCL_ALLOC, FTAG);
4064 
4065 	BP_SET_BIRTH(bp, txg, txg);
4066 
4067 	return (0);
4068 }
4069 
4070 void
metaslab_free(spa_t * spa,const blkptr_t * bp,uint64_t txg,boolean_t now)4071 metaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
4072 {
4073 	const dva_t *dva = bp->blk_dva;
4074 	int ndvas = BP_GET_NDVAS(bp);
4075 
4076 	ASSERT(!BP_IS_HOLE(bp));
4077 	ASSERT(!now || bp->blk_birth >= spa_syncing_txg(spa));
4078 
4079 	/*
4080 	 * If we have a checkpoint for the pool we need to make sure that
4081 	 * the blocks that we free that are part of the checkpoint won't be
4082 	 * reused until the checkpoint is discarded or we revert to it.
4083 	 *
4084 	 * The checkpoint flag is passed down the metaslab_free code path
4085 	 * and is set whenever we want to add a block to the checkpoint's
4086 	 * accounting. That is, we "checkpoint" blocks that existed at the
4087 	 * time the checkpoint was created and are therefore referenced by
4088 	 * the checkpointed uberblock.
4089 	 *
4090 	 * Note that, we don't checkpoint any blocks if the current
4091 	 * syncing txg <= spa_checkpoint_txg. We want these frees to sync
4092 	 * normally as they will be referenced by the checkpointed uberblock.
4093 	 */
4094 	boolean_t checkpoint = B_FALSE;
4095 	if (bp->blk_birth <= spa->spa_checkpoint_txg &&
4096 	    spa_syncing_txg(spa) > spa->spa_checkpoint_txg) {
4097 		/*
4098 		 * At this point, if the block is part of the checkpoint
4099 		 * there is no way it was created in the current txg.
4100 		 */
4101 		ASSERT(!now);
4102 		ASSERT3U(spa_syncing_txg(spa), ==, txg);
4103 		checkpoint = B_TRUE;
4104 	}
4105 
4106 	spa_config_enter(spa, SCL_FREE, FTAG, RW_READER);
4107 
4108 	for (int d = 0; d < ndvas; d++) {
4109 		if (now) {
4110 			metaslab_unalloc_dva(spa, &dva[d], txg);
4111 		} else {
4112 			ASSERT3U(txg, ==, spa_syncing_txg(spa));
4113 			metaslab_free_dva(spa, &dva[d], checkpoint);
4114 		}
4115 	}
4116 
4117 	spa_config_exit(spa, SCL_FREE, FTAG);
4118 }
4119 
4120 int
metaslab_claim(spa_t * spa,const blkptr_t * bp,uint64_t txg)4121 metaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
4122 {
4123 	const dva_t *dva = bp->blk_dva;
4124 	int ndvas = BP_GET_NDVAS(bp);
4125 	int error = 0;
4126 
4127 	ASSERT(!BP_IS_HOLE(bp));
4128 
4129 	if (txg != 0) {
4130 		/*
4131 		 * First do a dry run to make sure all DVAs are claimable,
4132 		 * so we don't have to unwind from partial failures below.
4133 		 */
4134 		if ((error = metaslab_claim(spa, bp, 0)) != 0)
4135 			return (error);
4136 	}
4137 
4138 	spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
4139 
4140 	for (int d = 0; d < ndvas; d++)
4141 		if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
4142 			break;
4143 
4144 	spa_config_exit(spa, SCL_ALLOC, FTAG);
4145 
4146 	ASSERT(error == 0 || txg == 0);
4147 
4148 	return (error);
4149 }
4150 
4151 /* ARGSUSED */
4152 static void
metaslab_check_free_impl_cb(uint64_t inner,vdev_t * vd,uint64_t offset,uint64_t size,void * arg)4153 metaslab_check_free_impl_cb(uint64_t inner, vdev_t *vd, uint64_t offset,
4154     uint64_t size, void *arg)
4155 {
4156 	if (vd->vdev_ops == &vdev_indirect_ops)
4157 		return;
4158 
4159 	metaslab_check_free_impl(vd, offset, size);
4160 }
4161 
4162 static void
metaslab_check_free_impl(vdev_t * vd,uint64_t offset,uint64_t size)4163 metaslab_check_free_impl(vdev_t *vd, uint64_t offset, uint64_t size)
4164 {
4165 	metaslab_t *msp;
4166 	spa_t *spa = vd->vdev_spa;
4167 
4168 	if ((zfs_flags & ZFS_DEBUG_ZIO_FREE) == 0)
4169 		return;
4170 
4171 	if (vd->vdev_ops->vdev_op_remap != NULL) {
4172 		vd->vdev_ops->vdev_op_remap(vd, offset, size,
4173 		    metaslab_check_free_impl_cb, NULL);
4174 		return;
4175 	}
4176 
4177 	ASSERT(vdev_is_concrete(vd));
4178 	ASSERT3U(offset >> vd->vdev_ms_shift, <, vd->vdev_ms_count);
4179 	ASSERT3U(spa_config_held(spa, SCL_ALL, RW_READER), !=, 0);
4180 
4181 	msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
4182 
4183 	mutex_enter(&msp->ms_lock);
4184 	if (msp->ms_loaded)
4185 		range_tree_verify(msp->ms_allocatable, offset, size);
4186 
4187 	range_tree_verify(msp->ms_freeing, offset, size);
4188 	range_tree_verify(msp->ms_checkpointing, offset, size);
4189 	range_tree_verify(msp->ms_freed, offset, size);
4190 	for (int j = 0; j < TXG_DEFER_SIZE; j++)
4191 		range_tree_verify(msp->ms_defer[j], offset, size);
4192 	mutex_exit(&msp->ms_lock);
4193 }
4194 
4195 void
metaslab_check_free(spa_t * spa,const blkptr_t * bp)4196 metaslab_check_free(spa_t *spa, const blkptr_t *bp)
4197 {
4198 	if ((zfs_flags & ZFS_DEBUG_ZIO_FREE) == 0)
4199 		return;
4200 
4201 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
4202 	for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
4203 		uint64_t vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
4204 		vdev_t *vd = vdev_lookup_top(spa, vdev);
4205 		uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
4206 		uint64_t size = DVA_GET_ASIZE(&bp->blk_dva[i]);
4207 
4208 		if (DVA_GET_GANG(&bp->blk_dva[i]))
4209 			size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
4210 
4211 		ASSERT3P(vd, !=, NULL);
4212 
4213 		metaslab_check_free_impl(vd, offset, size);
4214 	}
4215 	spa_config_exit(spa, SCL_VDEV, FTAG);
4216 }
4217