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