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