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